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 dpaddr
= CPM_SPI_BASE
;
193 /* Set up the SPI parameters in the parameter ram */
194 spi
->spi_rbase
= dpaddr
;
195 spi
->spi_tbase
= dpaddr
+ sizeof (cbd_t
);
197 /***********IMPORTANT******************/
200 * Setting transmit and receive buffer descriptor pointers
201 * initially to rbase and tbase. Only the microcode patches
202 * documentation talks about initializing this pointer. This
203 * is missing from the sample I2C driver. If you dont
204 * initialize these pointers, the kernel hangs.
206 spi
->spi_rbptr
= spi
->spi_rbase
;
207 spi
->spi_tbptr
= spi
->spi_tbase
;
210 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
212 * Initialize required parameters if using microcode patch.
217 /* Init SPI Tx + Rx Parameters */
218 while (cp
->cp_cpcr
& CPM_CR_FLG
)
220 cp
->cp_cpcr
= mk_cr_cmd(CPM_CR_CH_SPI
, CPM_CR_INIT_TRX
) | CPM_CR_FLG
;
221 while (cp
->cp_cpcr
& CPM_CR_FLG
)
223 #endif /* CONFIG_SYS_SPI_UCODE_PATCH */
226 /* Set SDMA configuration register */
227 immr
->im_siu_conf
.sc_sdcr
= 0x0001;
230 /* Set to big endian. */
231 spi
->spi_tfcr
= SMC_EB
;
232 spi
->spi_rfcr
= SMC_EB
;
235 /* Set maximum receive size. */
236 spi
->spi_mrblr
= MAX_BUFFER
;
239 /* tx and rx buffer descriptors */
240 tbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_tbase
];
241 rbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_rbase
];
243 tbdf
->cbd_sc
&= ~BD_SC_READY
;
244 rbdf
->cbd_sc
&= ~BD_SC_EMPTY
;
246 /* Set the bd's rx and tx buffer address pointers */
247 rbdf
->cbd_bufaddr
= (ulong
) rxbuf
;
248 tbdf
->cbd_bufaddr
= (ulong
) txbuf
;
251 cp
->cp_spim
= 0; /* Mask all SPI events */
252 cp
->cp_spie
= SPI_EMASK
; /* Clear all SPI events */
257 /* **************************************************************************
259 * Function: spi_init_r
261 * Description: Init SPI-Controller (RAM part) -
262 * The malloc engine is ready and we can move our buffers to
267 * *********************************************************************** */
268 void spi_init_r (void)
270 volatile cpm8xx_t
*cp
;
272 volatile immap_t
*immr
;
273 volatile cbd_t
*tbdf
, *rbdf
;
275 immr
= (immap_t
*) CONFIG_SYS_IMMR
;
276 cp
= (cpm8xx_t
*) &immr
->im_cpm
;
278 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
279 spi
= (spi_t
*)&cp
->cp_dpmem
[spi
->spi_rpbase
];
281 spi
= (spi_t
*)&cp
->cp_dparam
[PROFF_SPI
];
282 /* Disable relocation */
286 /* tx and rx buffer descriptors */
287 tbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_tbase
];
288 rbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_rbase
];
290 /* Allocate memory for RX and TX buffers */
291 rxbuf
= (uchar
*) malloc (MAX_BUFFER
);
292 txbuf
= (uchar
*) malloc (MAX_BUFFER
);
294 rbdf
->cbd_bufaddr
= (ulong
) rxbuf
;
295 tbdf
->cbd_bufaddr
= (ulong
) txbuf
;
300 /****************************************************************************
301 * Function: spi_write
302 **************************************************************************** */
303 ssize_t
spi_write (uchar
*addr
, int alen
, uchar
*buffer
, int len
)
307 memset(rxbuf
, 0, MAX_BUFFER
);
308 memset(txbuf
, 0, MAX_BUFFER
);
309 *txbuf
= SPI_EEPROM_WREN
; /* write enable */
311 memcpy(txbuf
, addr
, alen
);
312 *txbuf
= SPI_EEPROM_WRITE
; /* WRITE memory array */
313 memcpy(alen
+ txbuf
, buffer
, len
);
314 spi_xfer(alen
+ len
);
315 /* ignore received data */
316 for (i
= 0; i
< 1000; i
++) {
317 *txbuf
= SPI_EEPROM_RDSR
; /* read status */
320 if (!(rxbuf
[1] & 1)) {
326 printf ("*** spi_write: Time out while writing!\n");
332 /****************************************************************************
334 **************************************************************************** */
335 ssize_t
spi_read (uchar
*addr
, int alen
, uchar
*buffer
, int len
)
337 memset(rxbuf
, 0, MAX_BUFFER
);
338 memset(txbuf
, 0, MAX_BUFFER
);
339 memcpy(txbuf
, addr
, alen
);
340 *txbuf
= SPI_EEPROM_READ
; /* READ memory array */
343 * There is a bug in 860T (?) that cuts the last byte of input
344 * if we're reading into DPRAM. The solution we choose here is
345 * to always read len+1 bytes (we have one extra byte at the
346 * end of the buffer).
348 spi_xfer(alen
+ len
+ 1);
349 memcpy(buffer
, alen
+ rxbuf
, len
);
354 /****************************************************************************
356 **************************************************************************** */
357 ssize_t
spi_xfer (size_t count
)
359 volatile immap_t
*immr
;
360 volatile cpm8xx_t
*cp
;
366 DPRINT (("*** spi_xfer entered ***\n"));
368 immr
= (immap_t
*) CONFIG_SYS_IMMR
;
369 cp
= (cpm8xx_t
*) &immr
->im_cpm
;
371 #ifdef CONFIG_SYS_SPI_UCODE_PATCH
372 spi
= (spi_t
*)&cp
->cp_dpmem
[spi
->spi_rpbase
];
374 spi
= (spi_t
*)&cp
->cp_dparam
[PROFF_SPI
];
375 /* Disable relocation */
379 tbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_tbase
];
380 rbdf
= (cbd_t
*) & cp
->cp_dpmem
[spi
->spi_rbase
];
382 /* Set CS for device */
383 cp
->cp_pbdat
&= ~0x0001;
385 /* Setting tx bd status and data length */
386 tbdf
->cbd_sc
= BD_SC_READY
| BD_SC_LAST
| BD_SC_WRAP
;
387 tbdf
->cbd_datlen
= count
;
389 DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
392 /* Setting rx bd status and data length */
393 rbdf
->cbd_sc
= BD_SC_EMPTY
| BD_SC_WRAP
;
394 rbdf
->cbd_datlen
= 0; /* rx length has no significance */
396 loop
= cp
->cp_spmode
& SPMODE_LOOP
;
397 cp
->cp_spmode
= /*SPMODE_DIV16 |*/ /* BRG/16 mode not used here */
402 SPMODE_LEN(8) | /* 8 Bits per char */
403 SPMODE_PM(0x8) ; /* medium speed */
404 cp
->cp_spim
= 0; /* Mask all SPI events */
405 cp
->cp_spie
= SPI_EMASK
; /* Clear all SPI events */
407 /* start spi transfer */
408 DPRINT (("*** spi_xfer: Performing transfer ...\n"));
409 cp
->cp_spcom
|= SPI_STR
; /* Start transmit */
411 /* --------------------------------
412 * Wait for SPI transmit to get out
413 * or time out (1 second = 1000 ms)
414 * -------------------------------- */
415 for (tm
=0; tm
<1000; ++tm
) {
416 if (cp
->cp_spie
& SPI_TXB
) { /* Tx Buffer Empty */
417 DPRINT (("*** spi_xfer: Tx buffer empty\n"));
420 if ((tbdf
->cbd_sc
& BD_SC_READY
) == 0) {
421 DPRINT (("*** spi_xfer: Tx BD done\n"));
427 printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
429 DPRINT (("*** spi_xfer: ... transfer ended\n"));
432 printf ("\nspi_xfer: txbuf after xfer\n");
433 memdump ((void *) txbuf
, 16); /* dump of txbuf before transmit */
434 printf ("spi_xfer: rxbuf after xfer\n");
435 memdump ((void *) rxbuf
, 16); /* dump of rxbuf after transmit */
439 /* Clear CS for device */
440 cp
->cp_pbdat
|= 0x0001;
444 #endif /* CONFIG_SPI || (CONFIG_POST & CONFIG_SYS_POST_SPI) */
449 * The Serial Peripheral Interface (SPI) is tested in the local loopback mode.
450 * The interface is configured accordingly and several packets
451 * are transferred. The configurable test parameters are:
452 * TEST_MIN_LENGTH - minimum size of packet to transfer
453 * TEST_MAX_LENGTH - maximum size of packet to transfer
454 * TEST_NUM - number of tests
457 #if CONFIG_POST & CONFIG_SYS_POST_SPI
459 #define TEST_MIN_LENGTH 1
460 #define TEST_MAX_LENGTH MAX_BUFFER
463 static void packet_fill (char * packet
, int length
)
465 char c
= (char) length
;
468 for (i
= 0; i
< length
; i
++)
474 static int packet_check (char * packet
, int length
)
476 char c
= (char) length
;
479 for (i
= 0; i
< length
; i
++) {
480 if (packet
[i
] != c
++) return -1;
486 int spi_post_test (int flags
)
489 volatile immap_t
*immr
= (immap_t
*) CONFIG_SYS_IMMR
;
490 volatile cpm8xx_t
*cp
= (cpm8xx_t
*) & immr
->im_cpm
;
497 cp
->cp_spmode
|= SPMODE_LOOP
;
499 for (i
= 0; i
< TEST_NUM
; i
++) {
500 for (l
= TEST_MIN_LENGTH
; l
<= TEST_MAX_LENGTH
; l
+= 8) {
501 packet_fill ((char *)txbuf
, l
);
505 if (packet_check ((char *)rxbuf
, l
) < 0) {
515 cp
->cp_spmode
&= ~SPMODE_LOOP
;
518 * SCC2 parameter RAM space overlaps
519 * the SPI parameter RAM space. So we need to restore
520 * the SCC2 configuration if it is used by UART.
523 #if !defined(CONFIG_8xx_CONS_NONE)
524 serial_reinit_all ();
528 post_log ("SPI test failed\n");
533 #endif /* CONFIG_POST & CONFIG_SYS_POST_SPI */