1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <common.h>
21#include <asm/cpm_8260.h>
22#include <linux/ctype.h>
23#include <malloc.h>
24#include <post.h>
25#include <net.h>
26
27#if defined(CONFIG_SPI)
28
29
30
31
32
33
34
35#undef DEBUG
36
37#define SPI_EEPROM_WREN 0x06
38#define SPI_EEPROM_RDSR 0x05
39#define SPI_EEPROM_READ 0x03
40#define SPI_EEPROM_WRITE 0x02
41
42
43
44
45
46
47
48
49
50#ifndef CONFIG_SYS_SPI_INIT_OFFSET
51#define CONFIG_SYS_SPI_INIT_OFFSET 0x2000
52#endif
53
54#define CPM_SPI_BASE 0x100
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#define MAX_BUFFER 0x104
101
102
103
104
105static uchar *rxbuf =
106 (uchar *)&((immap_t *)CONFIG_SYS_IMMR)->im_dprambase
107 [CONFIG_SYS_SPI_INIT_OFFSET];
108static uchar *txbuf =
109 (uchar *)&((immap_t *)CONFIG_SYS_IMMR)->im_dprambase
110 [CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
111
112
113
114
115
116
117
118
119
120
121void spi_init_f (void)
122{
123 unsigned int dpaddr;
124
125 volatile spi_t *spi;
126 volatile immap_t *immr;
127 volatile cpm8260_t *cp;
128 volatile cbd_t *tbdf, *rbdf;
129
130 immr = (immap_t *) CONFIG_SYS_IMMR;
131 cp = (cpm8260_t *) &immr->im_cpm;
132
133 immr->im_dprambase16[PROFF_SPI_BASE / sizeof(u16)] = PROFF_SPI;
134 spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 immr->im_ioport.iop_ppard |= 0x0000E000;
150 immr->im_ioport.iop_ppard &= ~0x00080000;
151
152
153
154
155
156
157
158
159 immr->im_ioport.iop_pdird &= ~0x0000E000;
160 immr->im_ioport.iop_pdird |= 0x00080000;
161
162
163
164
165
166
167
168 immr->im_ioport.iop_psord |= 0x0000E000;
169
170
171
172
173 spi->spi_rstate = 0;
174 spi->spi_rdp = 0;
175 spi->spi_rbptr = 0;
176 spi->spi_rbc = 0;
177 spi->spi_rxtmp = 0;
178 spi->spi_tstate = 0;
179 spi->spi_tdp = 0;
180 spi->spi_tbptr = 0;
181 spi->spi_tbc = 0;
182 spi->spi_txtmp = 0;
183
184 dpaddr = CPM_SPI_BASE;
185
186
187
188 spi->spi_rbase = dpaddr;
189 spi->spi_tbase = dpaddr + sizeof (cbd_t);
190
191
192
193
194
195
196
197
198
199
200 spi->spi_rbptr = spi->spi_rbase;
201 spi->spi_tbptr = spi->spi_tbase;
202
203
204
205 while (cp->cp_cpcr & CPM_CR_FLG)
206 ;
207 cp->cp_cpcr = mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK,
208 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
209 while (cp->cp_cpcr & CPM_CR_FLG)
210 ;
211
212
213
214 spi->spi_tfcr = CPMFCR_EB;
215 spi->spi_rfcr = CPMFCR_EB;
216
217
218
219 spi->spi_mrblr = MAX_BUFFER;
220
221
222
223 tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
224 rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
225
226 tbdf->cbd_sc &= ~BD_SC_READY;
227 rbdf->cbd_sc &= ~BD_SC_EMPTY;
228
229
230 rbdf->cbd_bufaddr = (ulong) rxbuf;
231 tbdf->cbd_bufaddr = (ulong) txbuf;
232
233
234 immr->im_spi.spi_spie = SPI_EMASK;
235 immr->im_spi.spi_spim = 0x00;
236
237
238 return;
239}
240
241
242
243
244
245
246
247
248
249
250
251
252void spi_init_r (void)
253{
254 volatile spi_t *spi;
255 volatile immap_t *immr;
256 volatile cbd_t *tbdf, *rbdf;
257
258 immr = (immap_t *) CONFIG_SYS_IMMR;
259
260 spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
261
262
263 tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
264 rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
265
266
267 rxbuf = (uchar *) malloc (MAX_BUFFER);
268 txbuf = (uchar *) malloc (MAX_BUFFER);
269
270 rbdf->cbd_bufaddr = (ulong) rxbuf;
271 tbdf->cbd_bufaddr = (ulong) txbuf;
272
273 return;
274}
275
276
277
278
279ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
280{
281 int i;
282
283 memset(rxbuf, 0, MAX_BUFFER);
284 memset(txbuf, 0, MAX_BUFFER);
285 *txbuf = SPI_EEPROM_WREN;
286 spi_xfer(1);
287 memcpy(txbuf, addr, alen);
288 *txbuf = SPI_EEPROM_WRITE;
289 memcpy(alen + txbuf, buffer, len);
290 spi_xfer(alen + len);
291
292 for (i = 0; i < 1000; i++) {
293 *txbuf = SPI_EEPROM_RDSR;
294 txbuf[1] = 0;
295 spi_xfer(2);
296 if (!(rxbuf[1] & 1)) {
297 break;
298 }
299 udelay(1000);
300 }
301 if (i >= 1000) {
302 printf ("*** spi_write: Time out while writing!\n");
303 }
304
305 return len;
306}
307
308
309
310
311ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
312{
313 memset(rxbuf, 0, MAX_BUFFER);
314 memset(txbuf, 0, MAX_BUFFER);
315 memcpy(txbuf, addr, alen);
316 *txbuf = SPI_EEPROM_READ;
317
318
319
320
321
322
323
324 spi_xfer(alen + len + 1);
325 memcpy(buffer, alen + rxbuf, len);
326
327 return len;
328}
329
330
331
332
333ssize_t spi_xfer (size_t count)
334{
335 volatile immap_t *immr;
336 volatile spi_t *spi;
337 cbd_t *tbdf, *rbdf;
338 int tm;
339
340 DPRINT (("*** spi_xfer entered ***\n"));
341
342 immr = (immap_t *) CONFIG_SYS_IMMR;
343
344 spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
345
346 tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
347 rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
348
349
350 immr->im_ioport.iop_pdatd &= ~0x00080000;
351
352
353 tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP;
354 tbdf->cbd_datlen = count;
355
356 DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
357 tbdf->cbd_datlen));
358
359
360 rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
361 rbdf->cbd_datlen = 0;
362
363 immr->im_spi.spi_spmode = SPMODE_REV |
364 SPMODE_MSTR |
365 SPMODE_EN |
366 SPMODE_LEN(8) |
367 SPMODE_PM(0x8) ;
368 immr->im_spi.spi_spie = SPI_EMASK;
369 immr->im_spi.spi_spim = 0x00;
370
371
372 DPRINT (("*** spi_xfer: Performing transfer ...\n"));
373 immr->im_spi.spi_spcom |= SPI_STR;
374
375
376
377
378
379 for (tm=0; tm<1000; ++tm) {
380 if (immr->im_spi.spi_spie & SPI_TXB) {
381 DPRINT (("*** spi_xfer: Tx buffer empty\n"));
382 break;
383 }
384 if ((tbdf->cbd_sc & BD_SC_READY) == 0) {
385 DPRINT (("*** spi_xfer: Tx BD done\n"));
386 break;
387 }
388 udelay (1000);
389 }
390 if (tm >= 1000) {
391 printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
392 }
393 DPRINT (("*** spi_xfer: ... transfer ended\n"));
394
395#ifdef DEBUG
396 printf ("\nspi_xfer: txbuf after xfer\n");
397 memdump ((void *) txbuf, 16);
398 printf ("spi_xfer: rxbuf after xfer\n");
399 memdump ((void *) rxbuf, 16);
400 printf ("\n");
401#endif
402
403
404 immr->im_ioport.iop_pdatd |= 0x00080000;
405
406 return count;
407}
408#endif
409