2 * Copyright (c) 2001 Navin Boppuri / Prashant Patel
3 * <nboppuri@trinetcommunication.com>,
4 * <pmpatel@trinetcommunication.com>
5 * Copyright (c) 2001 Gerd Mennchen <Gerd.Mennchen@icn.siemens.de>
6 * Copyright (c) 2001 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
8 * SPDX-License-Identifier: GPL-2.0+
12 * MPC8xx CPM SPI interface.
14 * Parts of this code are probably not portable and/or specific to
15 * the board which I used for the tests. Please send fixes/complaints
23 #include <linux/ctype.h>
28 #if (defined(CONFIG_SPI)) || (CONFIG_POST & CONFIG_SYS_POST_SPI)
31 * You cannot enable DEBUG for early system initalization, i. e. when
32 * this driver is used to read environment parameters like "baudrate"
33 * from EEPROM which are used to initialize the serial port which is
34 * needed to print the debug messages...
38 #define SPI_EEPROM_WREN 0x06
39 #define SPI_EEPROM_RDSR 0x05
40 #define SPI_EEPROM_READ 0x03
41 #define SPI_EEPROM_WRITE 0x02
43 /* ---------------------------------------------------------------
44 * Offset for initial SPI buffers in DPRAM:
45 * We need a 520 byte scratch DPRAM area to use at an early stage.
46 * It is used between the two initialization calls (spi_init_f()
48 * The value 0xb00 makes it far enough from the start of the data
49 * area (as well as from the stack pointer).
50 * --------------------------------------------------------------- */
51 #ifndef CONFIG_SYS_SPI_INIT_OFFSET
52 #define CONFIG_SYS_SPI_INIT_OFFSET 0xB00
57 #define DPRINT(a) printf a;
58 /* -----------------------------------------------
59 * Helper functions to peek into tx and rx buffers
60 * ----------------------------------------------- */
61 static const char * const hex_digit
= "0123456789ABCDEF";
63 static char quickhex (int i
)
68 static void memdump (void *pv
, int num
)
71 unsigned char *pc
= (unsigned char *) pv
;
73 for (i
= 0; i
< num
; i
++)
74 printf ("%c%c ", quickhex (pc
[i
] >> 4), quickhex (pc
[i
] & 0x0f));
76 for (i
= 0; i
< num
; i
++)
77 printf ("%c", isprint (pc
[i
]) ? pc
[i
] : '.');
86 /* -------------------
88 * ------------------- */
91 ssize_t
spi_read (uchar
*, int, uchar
*, int);
92 ssize_t
spi_write (uchar
*, int, uchar
*, int);
93 ssize_t
spi_xfer (size_t);
95 /* -------------------
97 * ------------------- */
99 #define MAX_BUFFER 0x104
101 /* ----------------------------------------------------------------------
102 * Initially we place the RX and TX buffers at a fixed location in DPRAM!
103 * ---------------------------------------------------------------------- */
104 static uchar
*rxbuf
=
105 (uchar
*)&((cpm8xx_t
*)&((immap_t
*)CONFIG_SYS_IMMR
)->im_cpm
)->cp_dpmem
106 [CONFIG_SYS_SPI_INIT_OFFSET
];
107 static uchar
*txbuf
=
108 (uchar
*)&((cpm8xx_t
*)&((immap_t
*)CONFIG_SYS_IMMR
)->im_cpm
)->cp_dpmem
109 [CONFIG_SYS_SPI_INIT_OFFSET
+MAX_BUFFER
];
111 /* **************************************************************************
113 * Function: spi_init_f
115 * Description: Init SPI-Controller (ROM part)
119 * *********************************************************************** */
120 void spi_init_f (void)
125 volatile immap_t
*immr
;
126 volatile cpm8xx_t
*cp
;
127 volatile cbd_t
*tbdf
, *rbdf
;
129 immr
= (immap_t
*) CONFIG_SYS_IMMR
;
130 cp
= (cpm8xx_t
*) &immr
->im_cpm
;
132 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
133 spi
= (spi_t
*)&cp
->cp_dpmem
[spi
->spi_rpbase
];
135 spi
= (spi_t
*)&cp
->cp_dparam
[PROFF_SPI
];
136 /* Disable relocation */
141 /* ------------------------------------------------
142 * Initialize Port B SPI pins -> page 34-8 MPC860UM
143 * (we are only in Master Mode !)
144 * ------------------------------------------------ */
146 /* --------------------------------------------
147 * GPIO or per. Function
148 * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
149 * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
150 * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
151 * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
152 * -------------------------------------------- */
153 cp
->cp_pbpar
|= 0x0000000E; /* set bits */
154 cp
->cp_pbpar
&= ~0x00000001; /* reset bit */
156 /* ----------------------------------------------
157 * In/Out or per. Function 0/1
158 * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
159 * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
160 * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
161 * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
162 * ---------------------------------------------- */
163 cp
->cp_pbdir
|= 0x0000000F;
165 /* ----------------------------------------------
166 * open drain or active output
167 * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
168 * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
169 * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
170 * PBODR[31] = 0 [0x00000001] -> active output: GPIO OUT: CS for PCUE/CCM
171 * ---------------------------------------------- */
173 cp
->cp_pbodr
|= 0x00000008;
174 cp
->cp_pbodr
&= ~0x00000007;
176 /* Initialize the parameter ram.
177 * We need to make sure many things are initialized to zero
190 /* Allocate space for one transmit and one receive buffer
191 * descriptor in the DP ram
193 #ifdef CONFIG_SYS_ALLOC_DPRAM
194 dpaddr
= dpram_alloc_align (sizeof(cbd_t
)*2, 8);
196 dpaddr
= CPM_SPI_BASE
;
200 /* Set up the SPI parameters in the parameter ram */
201 spi
->spi_rbase
= dpaddr
;
202 spi
->spi_tbase
= dpaddr
+ sizeof (cbd_t
);
204 /***********IMPORTANT******************/
207 * Setting transmit and receive buffer descriptor pointers
208 * initially to rbase and tbase. Only the microcode patches
209 * documentation talks about initializing this pointer. This
210 * is missing from the sample I2C driver. If you dont
211 * initialize these pointers, the kernel hangs.
213 spi
->spi_rbptr
= spi
->spi_rbase
;
214 spi
->spi_tbptr
= spi
->spi_tbase
;
217 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
219 * Initialize required parameters if using microcode patch.
224 /* Init SPI Tx + Rx Parameters */
225 while (cp
->cp_cpcr
& CPM_CR_FLG
)
227 cp
->cp_cpcr
= mk_cr_cmd(CPM_CR_CH_SPI
, CPM_CR_INIT_TRX
) | CPM_CR_FLG
;
228 while (cp
->cp_cpcr
& CPM_CR_FLG
)
230 #endif /* CONFIG_SYS_SPI_UCODE_PATCH */
233 /* Set SDMA configuration register */
234 immr
->im_siu_conf
.sc_sdcr
= 0x0001;
237 /* Set to big endian. */
238 spi
->spi_tfcr
= SMC_EB
;
239 spi
->spi_rfcr
= SMC_EB
;
242 /* Set maximum receive size. */
243 spi
->spi_mrblr
= MAX_BUFFER
;
246 /* tx and rx buffer descriptors */
247 tbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_tbase
];
248 rbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_rbase
];
250 tbdf
->cbd_sc
&= ~BD_SC_READY
;
251 rbdf
->cbd_sc
&= ~BD_SC_EMPTY
;
253 /* Set the bd's rx and tx buffer address pointers */
254 rbdf
->cbd_bufaddr
= (ulong
) rxbuf
;
255 tbdf
->cbd_bufaddr
= (ulong
) txbuf
;
258 cp
->cp_spim
= 0; /* Mask all SPI events */
259 cp
->cp_spie
= SPI_EMASK
; /* Clear all SPI events */
264 /* **************************************************************************
266 * Function: spi_init_r
268 * Description: Init SPI-Controller (RAM part) -
269 * The malloc engine is ready and we can move our buffers to
274 * *********************************************************************** */
275 void spi_init_r (void)
277 volatile cpm8xx_t
*cp
;
279 volatile immap_t
*immr
;
280 volatile cbd_t
*tbdf
, *rbdf
;
282 immr
= (immap_t
*) CONFIG_SYS_IMMR
;
283 cp
= (cpm8xx_t
*) &immr
->im_cpm
;
285 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
286 spi
= (spi_t
*)&cp
->cp_dpmem
[spi
->spi_rpbase
];
288 spi
= (spi_t
*)&cp
->cp_dparam
[PROFF_SPI
];
289 /* Disable relocation */
293 /* tx and rx buffer descriptors */
294 tbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_tbase
];
295 rbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_rbase
];
297 /* Allocate memory for RX and TX buffers */
298 rxbuf
= (uchar
*) malloc (MAX_BUFFER
);
299 txbuf
= (uchar
*) malloc (MAX_BUFFER
);
301 rbdf
->cbd_bufaddr
= (ulong
) rxbuf
;
302 tbdf
->cbd_bufaddr
= (ulong
) txbuf
;
307 /****************************************************************************
308 * Function: spi_write
309 **************************************************************************** */
310 ssize_t
spi_write (uchar
*addr
, int alen
, uchar
*buffer
, int len
)
314 memset(rxbuf
, 0, MAX_BUFFER
);
315 memset(txbuf
, 0, MAX_BUFFER
);
316 *txbuf
= SPI_EEPROM_WREN
; /* write enable */
318 memcpy(txbuf
, addr
, alen
);
319 *txbuf
= SPI_EEPROM_WRITE
; /* WRITE memory array */
320 memcpy(alen
+ txbuf
, buffer
, len
);
321 spi_xfer(alen
+ len
);
322 /* ignore received data */
323 for (i
= 0; i
< 1000; i
++) {
324 *txbuf
= SPI_EEPROM_RDSR
; /* read status */
327 if (!(rxbuf
[1] & 1)) {
333 printf ("*** spi_write: Time out while writing!\n");
339 /****************************************************************************
341 **************************************************************************** */
342 ssize_t
spi_read (uchar
*addr
, int alen
, uchar
*buffer
, int len
)
344 memset(rxbuf
, 0, MAX_BUFFER
);
345 memset(txbuf
, 0, MAX_BUFFER
);
346 memcpy(txbuf
, addr
, alen
);
347 *txbuf
= SPI_EEPROM_READ
; /* READ memory array */
350 * There is a bug in 860T (?) that cuts the last byte of input
351 * if we're reading into DPRAM. The solution we choose here is
352 * to always read len+1 bytes (we have one extra byte at the
353 * end of the buffer).
355 spi_xfer(alen
+ len
+ 1);
356 memcpy(buffer
, alen
+ rxbuf
, len
);
361 /****************************************************************************
363 **************************************************************************** */
364 ssize_t
spi_xfer (size_t count
)
366 volatile immap_t
*immr
;
367 volatile cpm8xx_t
*cp
;
373 DPRINT (("*** spi_xfer entered ***\n"));
375 immr
= (immap_t
*) CONFIG_SYS_IMMR
;
376 cp
= (cpm8xx_t
*) &immr
->im_cpm
;
378 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
379 spi
= (spi_t
*)&cp
->cp_dpmem
[spi
->spi_rpbase
];
381 spi
= (spi_t
*)&cp
->cp_dparam
[PROFF_SPI
];
382 /* Disable relocation */
386 tbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_tbase
];
387 rbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_rbase
];
389 /* Set CS for device */
390 cp
->cp_pbdat
&= ~0x0001;
392 /* Setting tx bd status and data length */
393 tbdf
->cbd_sc
= BD_SC_READY
| BD_SC_LAST
| BD_SC_WRAP
;
394 tbdf
->cbd_datlen
= count
;
396 DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
399 /* Setting rx bd status and data length */
400 rbdf
->cbd_sc
= BD_SC_EMPTY
| BD_SC_WRAP
;
401 rbdf
->cbd_datlen
= 0; /* rx length has no significance */
403 loop
= cp
->cp_spmode
& SPMODE_LOOP
;
404 cp
->cp_spmode
= /*SPMODE_DIV16 |*/ /* BRG/16 mode not used here */
409 SPMODE_LEN(8) | /* 8 Bits per char */
410 SPMODE_PM(0x8) ; /* medium speed */
411 cp
->cp_spim
= 0; /* Mask all SPI events */
412 cp
->cp_spie
= SPI_EMASK
; /* Clear all SPI events */
414 /* start spi transfer */
415 DPRINT (("*** spi_xfer: Performing transfer ...\n"));
416 cp
->cp_spcom
|= SPI_STR
; /* Start transmit */
418 /* --------------------------------
419 * Wait for SPI transmit to get out
420 * or time out (1 second = 1000 ms)
421 * -------------------------------- */
422 for (tm
=0; tm
<1000; ++tm
) {
423 if (cp
->cp_spie
& SPI_TXB
) { /* Tx Buffer Empty */
424 DPRINT (("*** spi_xfer: Tx buffer empty\n"));
427 if ((tbdf
->cbd_sc
& BD_SC_READY
) == 0) {
428 DPRINT (("*** spi_xfer: Tx BD done\n"));
434 printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
436 DPRINT (("*** spi_xfer: ... transfer ended\n"));
439 printf ("\nspi_xfer: txbuf after xfer\n");
440 memdump ((void *) txbuf
, 16); /* dump of txbuf before transmit */
441 printf ("spi_xfer: rxbuf after xfer\n");
442 memdump ((void *) rxbuf
, 16); /* dump of rxbuf after transmit */
446 /* Clear CS for device */
447 cp
->cp_pbdat
|= 0x0001;
451 #endif /* CONFIG_SPI || (CONFIG_POST & CONFIG_SYS_POST_SPI) */
456 * The Serial Peripheral Interface (SPI) is tested in the local loopback mode.
457 * The interface is configured accordingly and several packets
458 * are transfered. The configurable test parameters are:
459 * TEST_MIN_LENGTH - minimum size of packet to transfer
460 * TEST_MAX_LENGTH - maximum size of packet to transfer
461 * TEST_NUM - number of tests
464 #if CONFIG_POST & CONFIG_SYS_POST_SPI
466 #define TEST_MIN_LENGTH 1
467 #define TEST_MAX_LENGTH MAX_BUFFER
470 static void packet_fill (char * packet
, int length
)
472 char c
= (char) length
;
475 for (i
= 0; i
< length
; i
++)
481 static int packet_check (char * packet
, int length
)
483 char c
= (char) length
;
486 for (i
= 0; i
< length
; i
++) {
487 if (packet
[i
] != c
++) return -1;
493 int spi_post_test (int flags
)
496 volatile immap_t
*immr
= (immap_t
*) CONFIG_SYS_IMMR
;
497 volatile cpm8xx_t
*cp
= (cpm8xx_t
*) & immr
->im_cpm
;
504 cp
->cp_spmode
|= SPMODE_LOOP
;
506 for (i
= 0; i
< TEST_NUM
; i
++) {
507 for (l
= TEST_MIN_LENGTH
; l
<= TEST_MAX_LENGTH
; l
+= 8) {
508 packet_fill ((char *)txbuf
, l
);
512 if (packet_check ((char *)rxbuf
, l
) < 0) {
522 cp
->cp_spmode
&= ~SPMODE_LOOP
;
525 * SCC2 parameter RAM space overlaps
526 * the SPI parameter RAM space. So we need to restore
527 * the SCC2 configuration if it is used by UART.
530 #if !defined(CONFIG_8xx_CONS_NONE)
531 serial_reinit_all ();
535 post_log ("SPI test failed\n");
540 #endif /* CONFIG_POST & CONFIG_SYS_POST_SPI */