1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <common.h>
26#include <asm/io.h>
27#include <malloc.h>
28#include <spi.h>
29#include <mpc5xxx.h>
30
31void spi_init(void)
32{
33 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
34
35
36
37
38 out_8(&spi->ddr, 0x0F);
39 out_8(&spi->pdr, 0x00);
40
41 out_8(&spi->cr1, SPI_CR_MSTR | SPI_CR_SPE);
42 out_8(&spi->cr2, 0x00);
43 out_8(&spi->brr, 0x77);
44}
45
46struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
47 unsigned int max_hz, unsigned int mode)
48{
49 struct spi_slave *slave;
50
51 slave = malloc(sizeof(struct spi_slave));
52 if (!slave)
53 return NULL;
54
55 slave->bus = bus;
56 slave->cs = cs;
57
58 return slave;
59}
60
61void spi_free_slave(struct spi_slave *slave)
62{
63 free(slave);
64}
65
66int spi_claim_bus(struct spi_slave *slave)
67{
68 return 0;
69}
70
71void spi_release_bus(struct spi_slave *slave)
72{
73 return;
74}
75
76int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
77 void *din, unsigned long flags)
78{
79 struct mpc5xxx_spi *spi = (struct mpc5xxx_spi *)MPC5XXX_SPI;
80 int i, iter = bitlen >> 3;
81 const uchar *txp = dout;
82 uchar *rxp = din;
83
84 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
85 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
86
87 if (flags & SPI_XFER_BEGIN)
88 setbits_8(&spi->pdr, SPI_PDR_SS);
89
90 for (i = 0; i < iter; i++) {
91 udelay(1000);
92 debug("spi_xfer: sending %x\n", txp[i]);
93 out_8(&spi->dr, txp[i]);
94 while (!(in_8(&spi->sr) & SPI_SR_SPIF)) {
95 udelay(1000);
96 if (in_8(&spi->sr) & SPI_SR_WCOL) {
97 rxp[i] = in_8(&spi->dr);
98 puts("spi_xfer: write collision\n");
99 return -1;
100 }
101 }
102 rxp[i] = in_8(&spi->dr);
103 debug("spi_xfer: received %x\n", rxp[i]);
104 }
105 if (flags & SPI_XFER_END)
106 clrbits_8(&spi->pdr, SPI_PDR_SS);
107
108 return 0;
109}
110