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
26
27
28
29
30
31
32
33
34
35
36
37
38#include <common.h>
39#include <mpc5xx.h>
40#include <asm/5xx_immap.h>
41#include <linux/ctype.h>
42#include <malloc.h>
43#include <post.h>
44#include <net.h>
45
46#if defined(CONFIG_SPI)
47
48#undef DEBUG
49
50#define SPI_EEPROM_WREN 0x06
51#define SPI_EEPROM_RDSR 0x05
52#define SPI_EEPROM_READ 0x03
53#define SPI_EEPROM_WRITE 0x02
54
55
56#ifdef DEBUG
57
58#define DPRINT(a) printf a;
59
60
61
62static const char * const hex_digit = "0123456789ABCDEF";
63
64static char quickhex (int i)
65{
66 return hex_digit[i];
67}
68
69static void memdump (void *pv, int num)
70{
71 int i;
72 unsigned char *pc = (unsigned char *) pv;
73
74 for (i = 0; i < num; i++)
75 printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f));
76 printf ("\t");
77 for (i = 0; i < num; i++)
78 printf ("%c", isprint (pc[i]) ? pc[i] : '.');
79 printf ("\n");
80}
81#else
82
83#define DPRINT(a)
84
85#endif
86
87
88
89
90void spi_init (void);
91
92ssize_t spi_read (uchar *, int, uchar *, int);
93ssize_t spi_write (uchar *, int, uchar *, int);
94ssize_t spi_xfer (size_t);
95
96
97
98
99
100
101
102
103
104
105
106
107void spi_init_f (void)
108{
109 int i;
110
111 volatile immap_t *immr;
112 volatile qsmcm5xx_t *qsmcm;
113
114 immr = (immap_t *) CONFIG_SYS_IMMR;
115 qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;
116
117 qsmcm->qsmcm_qsmcr = 0;
118 qsmcm->qsmcm_qspi_il = 0;
119
120
121
122
123
124
125
126
127
128
129
130
131 qsmcm->qsmcm_pqspar = 0x3 | (CONFIG_SYS_SPI_CS_USED << 3);
132
133
134
135
136
137
138
139
140
141
142
143 qsmcm->qsmcm_ddrqs = 0x7E;
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 qsmcm->qsmcm_portqs |= (CONFIG_SYS_SPI_CS_BASE << 3);
164
165
166
167
168
169
170
171
172
173 qsmcm->qsmcm_spcr0=0xA00E;
174
175
176
177
178
179
180 qsmcm->qsmcm_spcr1=0x7F00;
181
182
183
184
185
186
187
188
189
190 qsmcm->qsmcm_spcr2=0x0000;
191
192
193
194
195
196
197
198 qsmcm->qsmcm_spcr3=0x00;
199
200
201
202
203
204
205
206 qsmcm->qsmcm_spsr=0xE0;
207
208
209
210 for(i=0;i<32;i++) {
211 qsmcm->qsmcm_recram[i]=0x0000;
212 qsmcm->qsmcm_tranram[i]=0x0000;
213 qsmcm->qsmcm_comdram[i]=0x00;
214 }
215 return;
216}
217
218
219
220
221
222
223void spi_init_r (void)
224{
225 return;
226
227}
228
229
230
231
232ssize_t short_spi_write (uchar *addr, int alen, uchar *buffer, int len)
233{
234 int i,dlen;
235 volatile immap_t *immr;
236 volatile qsmcm5xx_t *qsmcm;
237
238 immr = (immap_t *) CONFIG_SYS_IMMR;
239 qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;
240 for(i=0;i<32;i++) {
241 qsmcm->qsmcm_recram[i]=0x0000;
242 qsmcm->qsmcm_tranram[i]=0x0000;
243 qsmcm->qsmcm_comdram[i]=0x00;
244 }
245 qsmcm->qsmcm_tranram[0] = SPI_EEPROM_WREN;
246 spi_xfer(1);
247 i=0;
248 qsmcm->qsmcm_tranram[i++] = SPI_EEPROM_WRITE;
249 qsmcm->qsmcm_tranram[i++] = addr[0];
250 qsmcm->qsmcm_tranram[i++] = addr[1];
251
252 for(dlen=0;dlen<len;dlen++) {
253 qsmcm->qsmcm_tranram[i+dlen] = buffer[dlen];
254 }
255
256 spi_xfer(i+dlen);
257
258 for (i = 0; i < 1000; i++) {
259 qsmcm->qsmcm_tranram[0] = SPI_EEPROM_RDSR;
260 qsmcm->qsmcm_tranram[1] = 0;
261 spi_xfer(2);
262 if (!(qsmcm->qsmcm_recram[1] & 1)) {
263 break;
264 }
265 udelay(1000);
266 }
267 if (i >= 1000) {
268 printf ("*** spi_write: Time out while writing!\n");
269 }
270 return len;
271}
272
273#define TRANSFER_LEN 16
274
275ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
276{
277 int index,i,newlen;
278 uchar newaddr[2];
279 int curraddr;
280
281 curraddr=(addr[alen-2]<<8)+addr[alen-1];
282 i=len;
283 index=0;
284 do {
285 newaddr[1]=(curraddr & 0xff);
286 newaddr[0]=((curraddr>>8) & 0xff);
287 if(i>TRANSFER_LEN) {
288 newlen=TRANSFER_LEN;
289 i-=TRANSFER_LEN;
290 }
291 else {
292 newlen=i;
293 i=0;
294 }
295 short_spi_write (newaddr, 2, &buffer[index], newlen);
296 index+=newlen;
297 curraddr+=newlen;
298 }while(i);
299 return (len);
300}
301
302
303
304
305ssize_t short_spi_read (uchar *addr, int alen, uchar *buffer, int len)
306{
307 int i;
308 volatile immap_t *immr;
309 volatile qsmcm5xx_t *qsmcm;
310
311 immr = (immap_t *) CONFIG_SYS_IMMR;
312 qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;
313
314 for(i=0;i<32;i++) {
315 qsmcm->qsmcm_recram[i]=0x0000;
316 qsmcm->qsmcm_tranram[i]=0x0000;
317 qsmcm->qsmcm_comdram[i]=0x00;
318 }
319 i=0;
320 qsmcm->qsmcm_tranram[i++] = (SPI_EEPROM_READ);
321 qsmcm->qsmcm_tranram[i++] = addr[0] & 0xff;
322 qsmcm->qsmcm_tranram[i++] = addr[1] & 0xff;
323 spi_xfer(3 + len);
324 for(i=0;i<len;i++) {
325 *buffer++=(char)qsmcm->qsmcm_recram[i+3];
326 }
327 return len;
328}
329
330ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
331{
332 int index,i,newlen;
333 uchar newaddr[2];
334 int curraddr;
335
336 curraddr=(addr[alen-2]<<8)+addr[alen-1];
337 i=len;
338 index=0;
339 do {
340 newaddr[1]=(curraddr & 0xff);
341 newaddr[0]=((curraddr>>8) & 0xff);
342 if(i>TRANSFER_LEN) {
343 newlen=TRANSFER_LEN;
344 i-=TRANSFER_LEN;
345 }
346 else {
347 newlen=i;
348 i=0;
349 }
350 short_spi_read (newaddr, 2, &buffer[index], newlen);
351 index+=newlen;
352 curraddr+=newlen;
353 }while(i);
354 return (len);
355}
356
357
358
359
360ssize_t spi_xfer (size_t count)
361{
362 volatile immap_t *immr;
363 volatile qsmcm5xx_t *qsmcm;
364 int i;
365 int tm;
366 ushort status;
367 immr = (immap_t *) CONFIG_SYS_IMMR;
368 qsmcm = (qsmcm5xx_t *)&immr->im_qsmcm;
369 DPRINT (("*** spi_xfer entered count %d***\n",count));
370
371
372 for(i=0;i<(count-1);i++)
373 qsmcm->qsmcm_comdram[i] = 0x80 | CONFIG_SYS_SPI_CS_ACT;
374
375 qsmcm->qsmcm_comdram[i] = CONFIG_SYS_SPI_CS_ACT;
376 qsmcm->qsmcm_spcr2=((count-1)&0x1F)<<8;
377
378 DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n", count));
379
380 qsmcm->qsmcm_spsr=0xE0;
381
382
383 DPRINT (("*** spi_xfer: Performing transfer ...\n"));
384 qsmcm->qsmcm_spcr1 |= 0x8000;
385
386
387
388
389
390 for (tm=0; tm<1000; ++tm) {
391 status=qsmcm->qsmcm_spcr1;
392 if((status & 0x8000)==0)
393 break;
394 udelay (1000);
395 }
396 if (tm >= 1000) {
397 printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
398 }
399#ifdef DEBUG
400 printf ("\nspi_xfer: txbuf after xfer\n");
401 memdump ((void *) qsmcm->qsmcm_tranram, 32);
402 printf ("spi_xfer: rxbuf after xfer\n");
403 memdump ((void *) qsmcm->qsmcm_recram, 32);
404 printf ("\nspi_xfer: commbuf after xfer\n");
405 memdump ((void *) qsmcm->qsmcm_comdram, 32);
406 printf ("\n");
407#endif
408
409 return count;
410}
411
412#endif
413