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