1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25
26#include <malloc.h>
27#include <spi.h>
28#include <asm/mpc8xxx_spi.h>
29
30#define SPI_EV_NE (0x80000000 >> 22)
31#define SPI_EV_NF (0x80000000 >> 23)
32
33#define SPI_MODE_LOOP (0x80000000 >> 1)
34#define SPI_MODE_REV (0x80000000 >> 5)
35#define SPI_MODE_MS (0x80000000 >> 6)
36#define SPI_MODE_EN (0x80000000 >> 7)
37
38#define SPI_TIMEOUT 1000
39
40struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
41 unsigned int max_hz, unsigned int mode)
42{
43 struct spi_slave *slave;
44
45 if (!spi_cs_is_valid(bus, cs))
46 return NULL;
47
48 slave = malloc(sizeof(struct spi_slave));
49 if (!slave)
50 return NULL;
51
52 slave->bus = bus;
53 slave->cs = cs;
54
55
56
57
58
59
60 return slave;
61}
62
63void spi_free_slave(struct spi_slave *slave)
64{
65 free(slave);
66}
67
68void spi_init(void)
69{
70 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
71
72
73
74
75
76 spi->mode = SPI_MODE_REV | SPI_MODE_MS | SPI_MODE_EN;
77 spi->mode = (spi->mode & 0xfff0ffff) | (1 << 16);
78
79 spi->event = 0xffffffff;
80 spi->mask = 0x00000000;
81 spi->com = 0;
82}
83
84int spi_claim_bus(struct spi_slave *slave)
85{
86 return 0;
87}
88
89void spi_release_bus(struct spi_slave *slave)
90{
91
92}
93
94int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
95 void *din, unsigned long flags)
96{
97 volatile spi8xxx_t *spi = &((immap_t *) (CONFIG_SYS_IMMR))->spi;
98 unsigned int tmpdout, tmpdin, event;
99 int numBlks = bitlen / 32 + (bitlen % 32 ? 1 : 0);
100 int tm, isRead = 0;
101 unsigned char charSize = 32;
102
103 debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
104 slave->bus, slave->cs, *(uint *) dout, *(uint *) din, bitlen);
105
106 if (flags & SPI_XFER_BEGIN)
107 spi_cs_activate(slave);
108
109 spi->event = 0xffffffff;
110
111
112 while (numBlks--) {
113 tmpdout = 0;
114 charSize = (bitlen >= 32 ? 32 : bitlen);
115
116
117 tmpdout = *(u32 *) dout >> (32 - charSize);
118
119
120
121
122
123
124
125
126
127 if (bitlen <= 16) {
128 if (bitlen <= 4)
129 spi->mode = (spi->mode & 0xff0fffff) |
130 (3 << 20);
131 else
132 spi->mode = (spi->mode & 0xff0fffff) |
133 ((bitlen - 1) << 20);
134 } else {
135 spi->mode = (spi->mode & 0xff0fffff);
136
137 bitlen -= 32;
138 dout += 4;
139 }
140
141 spi->tx = tmpdout;
142 debug("*** spi_xfer: ... %08x written\n", tmpdout);
143
144
145
146
147
148
149 for (tm = 0, isRead = 0; tm < SPI_TIMEOUT; ++tm) {
150 event = spi->event;
151 if (event & SPI_EV_NE) {
152 tmpdin = spi->rx;
153 spi->event |= SPI_EV_NE;
154 isRead = 1;
155
156 *(u32 *) din = (tmpdin << (32 - charSize));
157 if (charSize == 32) {
158
159 din += 4;
160 }
161 }
162
163
164
165
166
167
168 if (isRead && (event & SPI_EV_NF))
169 break;
170 }
171 if (tm >= SPI_TIMEOUT)
172 puts("*** spi_xfer: Time out during SPI transfer");
173
174 debug("*** spi_xfer: transfer ended. Value=%08x\n", tmpdin);
175 }
176
177 if (flags & SPI_XFER_END)
178 spi_cs_deactivate(slave);
179
180 return 0;
181}
182