spi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  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>.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * MPC8xx CPM SPI interface.
  12. *
  13. * Parts of this code are probably not portable and/or specific to
  14. * the board which I used for the tests. Please send fixes/complaints
  15. * to wd@denx.de
  16. *
  17. */
  18. #include <common.h>
  19. #include <mpc8xx.h>
  20. #include <commproc.h>
  21. #include <linux/ctype.h>
  22. #include <malloc.h>
  23. #include <post.h>
  24. #include <serial.h>
  25. #if (defined(CONFIG_SPI)) || (CONFIG_POST & CONFIG_SYS_POST_SPI)
  26. /* Warning:
  27. * You cannot enable DEBUG for early system initalization, i. e. when
  28. * this driver is used to read environment parameters like "baudrate"
  29. * from EEPROM which are used to initialize the serial port which is
  30. * needed to print the debug messages...
  31. */
  32. #undef DEBUG
  33. #define SPI_EEPROM_WREN 0x06
  34. #define SPI_EEPROM_RDSR 0x05
  35. #define SPI_EEPROM_READ 0x03
  36. #define SPI_EEPROM_WRITE 0x02
  37. /* ---------------------------------------------------------------
  38. * Offset for initial SPI buffers in DPRAM:
  39. * We need a 520 byte scratch DPRAM area to use at an early stage.
  40. * It is used between the two initialization calls (spi_init_f()
  41. * and spi_init_r()).
  42. * The value 0xb00 makes it far enough from the start of the data
  43. * area (as well as from the stack pointer).
  44. * --------------------------------------------------------------- */
  45. #ifndef CONFIG_SYS_SPI_INIT_OFFSET
  46. #define CONFIG_SYS_SPI_INIT_OFFSET 0xB00
  47. #endif
  48. #ifdef DEBUG
  49. #define DPRINT(a) printf a;
  50. /* -----------------------------------------------
  51. * Helper functions to peek into tx and rx buffers
  52. * ----------------------------------------------- */
  53. static const char * const hex_digit = "0123456789ABCDEF";
  54. static char quickhex (int i)
  55. {
  56. return hex_digit[i];
  57. }
  58. static void memdump (void *pv, int num)
  59. {
  60. int i;
  61. unsigned char *pc = (unsigned char *) pv;
  62. for (i = 0; i < num; i++)
  63. printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f));
  64. printf ("\t");
  65. for (i = 0; i < num; i++)
  66. printf ("%c", isprint (pc[i]) ? pc[i] : '.');
  67. printf ("\n");
  68. }
  69. #else /* !DEBUG */
  70. #define DPRINT(a)
  71. #endif /* DEBUG */
  72. /* -------------------
  73. * Function prototypes
  74. * ------------------- */
  75. void spi_init (void);
  76. ssize_t spi_read (uchar *, int, uchar *, int);
  77. ssize_t spi_write (uchar *, int, uchar *, int);
  78. ssize_t spi_xfer (size_t);
  79. /* -------------------
  80. * Variables
  81. * ------------------- */
  82. #define MAX_BUFFER 0x104
  83. /* ----------------------------------------------------------------------
  84. * Initially we place the RX and TX buffers at a fixed location in DPRAM!
  85. * ---------------------------------------------------------------------- */
  86. static uchar *rxbuf =
  87. (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
  88. [CONFIG_SYS_SPI_INIT_OFFSET];
  89. static uchar *txbuf =
  90. (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
  91. [CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
  92. /* **************************************************************************
  93. *
  94. * Function: spi_init_f
  95. *
  96. * Description: Init SPI-Controller (ROM part)
  97. *
  98. * return: ---
  99. *
  100. * *********************************************************************** */
  101. void spi_init_f (void)
  102. {
  103. unsigned int dpaddr;
  104. volatile spi_t *spi;
  105. volatile immap_t *immr;
  106. volatile cpm8xx_t *cp;
  107. volatile cbd_t *tbdf, *rbdf;
  108. immr = (immap_t *) CONFIG_SYS_IMMR;
  109. cp = (cpm8xx_t *) &immr->im_cpm;
  110. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  111. spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
  112. #else
  113. spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
  114. /* Disable relocation */
  115. spi->spi_rpbase = 0;
  116. #endif
  117. /* 1 */
  118. /* ------------------------------------------------
  119. * Initialize Port B SPI pins -> page 34-8 MPC860UM
  120. * (we are only in Master Mode !)
  121. * ------------------------------------------------ */
  122. /* --------------------------------------------
  123. * GPIO or per. Function
  124. * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
  125. * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
  126. * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
  127. * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
  128. * -------------------------------------------- */
  129. cp->cp_pbpar |= 0x0000000E; /* set bits */
  130. cp->cp_pbpar &= ~0x00000001; /* reset bit */
  131. /* ----------------------------------------------
  132. * In/Out or per. Function 0/1
  133. * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
  134. * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
  135. * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
  136. * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
  137. * ---------------------------------------------- */
  138. cp->cp_pbdir |= 0x0000000F;
  139. /* ----------------------------------------------
  140. * open drain or active output
  141. * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
  142. * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
  143. * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
  144. * PBODR[31] = 0 [0x00000001] -> active output: GPIO OUT: CS for PCUE/CCM
  145. * ---------------------------------------------- */
  146. cp->cp_pbodr |= 0x00000008;
  147. cp->cp_pbodr &= ~0x00000007;
  148. /* Initialize the parameter ram.
  149. * We need to make sure many things are initialized to zero
  150. */
  151. spi->spi_rstate = 0;
  152. spi->spi_rdp = 0;
  153. spi->spi_rbptr = 0;
  154. spi->spi_rbc = 0;
  155. spi->spi_rxtmp = 0;
  156. spi->spi_tstate = 0;
  157. spi->spi_tdp = 0;
  158. spi->spi_tbptr = 0;
  159. spi->spi_tbc = 0;
  160. spi->spi_txtmp = 0;
  161. /* Allocate space for one transmit and one receive buffer
  162. * descriptor in the DP ram
  163. */
  164. #ifdef CONFIG_SYS_ALLOC_DPRAM
  165. dpaddr = dpram_alloc_align (sizeof(cbd_t)*2, 8);
  166. #else
  167. dpaddr = CPM_SPI_BASE;
  168. #endif
  169. /* 3 */
  170. /* Set up the SPI parameters in the parameter ram */
  171. spi->spi_rbase = dpaddr;
  172. spi->spi_tbase = dpaddr + sizeof (cbd_t);
  173. /***********IMPORTANT******************/
  174. /*
  175. * Setting transmit and receive buffer descriptor pointers
  176. * initially to rbase and tbase. Only the microcode patches
  177. * documentation talks about initializing this pointer. This
  178. * is missing from the sample I2C driver. If you dont
  179. * initialize these pointers, the kernel hangs.
  180. */
  181. spi->spi_rbptr = spi->spi_rbase;
  182. spi->spi_tbptr = spi->spi_tbase;
  183. /* 4 */
  184. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  185. /*
  186. * Initialize required parameters if using microcode patch.
  187. */
  188. spi->spi_rstate = 0;
  189. spi->spi_tstate = 0;
  190. #else
  191. /* Init SPI Tx + Rx Parameters */
  192. while (cp->cp_cpcr & CPM_CR_FLG)
  193. ;
  194. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  195. while (cp->cp_cpcr & CPM_CR_FLG)
  196. ;
  197. #endif /* CONFIG_SYS_SPI_UCODE_PATCH */
  198. /* 5 */
  199. /* Set SDMA configuration register */
  200. immr->im_siu_conf.sc_sdcr = 0x0001;
  201. /* 6 */
  202. /* Set to big endian. */
  203. spi->spi_tfcr = SMC_EB;
  204. spi->spi_rfcr = SMC_EB;
  205. /* 7 */
  206. /* Set maximum receive size. */
  207. spi->spi_mrblr = MAX_BUFFER;
  208. /* 8 + 9 */
  209. /* tx and rx buffer descriptors */
  210. tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
  211. rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
  212. tbdf->cbd_sc &= ~BD_SC_READY;
  213. rbdf->cbd_sc &= ~BD_SC_EMPTY;
  214. /* Set the bd's rx and tx buffer address pointers */
  215. rbdf->cbd_bufaddr = (ulong) rxbuf;
  216. tbdf->cbd_bufaddr = (ulong) txbuf;
  217. /* 10 + 11 */
  218. cp->cp_spim = 0; /* Mask all SPI events */
  219. cp->cp_spie = SPI_EMASK; /* Clear all SPI events */
  220. return;
  221. }
  222. /* **************************************************************************
  223. *
  224. * Function: spi_init_r
  225. *
  226. * Description: Init SPI-Controller (RAM part) -
  227. * The malloc engine is ready and we can move our buffers to
  228. * normal RAM
  229. *
  230. * return: ---
  231. *
  232. * *********************************************************************** */
  233. void spi_init_r (void)
  234. {
  235. volatile cpm8xx_t *cp;
  236. volatile spi_t *spi;
  237. volatile immap_t *immr;
  238. volatile cbd_t *tbdf, *rbdf;
  239. immr = (immap_t *) CONFIG_SYS_IMMR;
  240. cp = (cpm8xx_t *) &immr->im_cpm;
  241. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  242. spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
  243. #else
  244. spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
  245. /* Disable relocation */
  246. spi->spi_rpbase = 0;
  247. #endif
  248. /* tx and rx buffer descriptors */
  249. tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
  250. rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
  251. /* Allocate memory for RX and TX buffers */
  252. rxbuf = (uchar *) malloc (MAX_BUFFER);
  253. txbuf = (uchar *) malloc (MAX_BUFFER);
  254. rbdf->cbd_bufaddr = (ulong) rxbuf;
  255. tbdf->cbd_bufaddr = (ulong) txbuf;
  256. return;
  257. }
  258. /****************************************************************************
  259. * Function: spi_write
  260. **************************************************************************** */
  261. ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
  262. {
  263. int i;
  264. memset(rxbuf, 0, MAX_BUFFER);
  265. memset(txbuf, 0, MAX_BUFFER);
  266. *txbuf = SPI_EEPROM_WREN; /* write enable */
  267. spi_xfer(1);
  268. memcpy(txbuf, addr, alen);
  269. *txbuf = SPI_EEPROM_WRITE; /* WRITE memory array */
  270. memcpy(alen + txbuf, buffer, len);
  271. spi_xfer(alen + len);
  272. /* ignore received data */
  273. for (i = 0; i < 1000; i++) {
  274. *txbuf = SPI_EEPROM_RDSR; /* read status */
  275. txbuf[1] = 0;
  276. spi_xfer(2);
  277. if (!(rxbuf[1] & 1)) {
  278. break;
  279. }
  280. udelay(1000);
  281. }
  282. if (i >= 1000) {
  283. printf ("*** spi_write: Time out while writing!\n");
  284. }
  285. return len;
  286. }
  287. /****************************************************************************
  288. * Function: spi_read
  289. **************************************************************************** */
  290. ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
  291. {
  292. memset(rxbuf, 0, MAX_BUFFER);
  293. memset(txbuf, 0, MAX_BUFFER);
  294. memcpy(txbuf, addr, alen);
  295. *txbuf = SPI_EEPROM_READ; /* READ memory array */
  296. /*
  297. * There is a bug in 860T (?) that cuts the last byte of input
  298. * if we're reading into DPRAM. The solution we choose here is
  299. * to always read len+1 bytes (we have one extra byte at the
  300. * end of the buffer).
  301. */
  302. spi_xfer(alen + len + 1);
  303. memcpy(buffer, alen + rxbuf, len);
  304. return len;
  305. }
  306. /****************************************************************************
  307. * Function: spi_xfer
  308. **************************************************************************** */
  309. ssize_t spi_xfer (size_t count)
  310. {
  311. volatile immap_t *immr;
  312. volatile cpm8xx_t *cp;
  313. volatile spi_t *spi;
  314. cbd_t *tbdf, *rbdf;
  315. ushort loop;
  316. int tm;
  317. DPRINT (("*** spi_xfer entered ***\n"));
  318. immr = (immap_t *) CONFIG_SYS_IMMR;
  319. cp = (cpm8xx_t *) &immr->im_cpm;
  320. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  321. spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
  322. #else
  323. spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
  324. /* Disable relocation */
  325. spi->spi_rpbase = 0;
  326. #endif
  327. tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
  328. rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
  329. /* Set CS for device */
  330. cp->cp_pbdat &= ~0x0001;
  331. /* Setting tx bd status and data length */
  332. tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP;
  333. tbdf->cbd_datlen = count;
  334. DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
  335. tbdf->cbd_datlen));
  336. /* Setting rx bd status and data length */
  337. rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
  338. rbdf->cbd_datlen = 0; /* rx length has no significance */
  339. loop = cp->cp_spmode & SPMODE_LOOP;
  340. cp->cp_spmode = /*SPMODE_DIV16 |*/ /* BRG/16 mode not used here */
  341. loop |
  342. SPMODE_REV |
  343. SPMODE_MSTR |
  344. SPMODE_EN |
  345. SPMODE_LEN(8) | /* 8 Bits per char */
  346. SPMODE_PM(0x8) ; /* medium speed */
  347. cp->cp_spim = 0; /* Mask all SPI events */
  348. cp->cp_spie = SPI_EMASK; /* Clear all SPI events */
  349. /* start spi transfer */
  350. DPRINT (("*** spi_xfer: Performing transfer ...\n"));
  351. cp->cp_spcom |= SPI_STR; /* Start transmit */
  352. /* --------------------------------
  353. * Wait for SPI transmit to get out
  354. * or time out (1 second = 1000 ms)
  355. * -------------------------------- */
  356. for (tm=0; tm<1000; ++tm) {
  357. if (cp->cp_spie & SPI_TXB) { /* Tx Buffer Empty */
  358. DPRINT (("*** spi_xfer: Tx buffer empty\n"));
  359. break;
  360. }
  361. if ((tbdf->cbd_sc & BD_SC_READY) == 0) {
  362. DPRINT (("*** spi_xfer: Tx BD done\n"));
  363. break;
  364. }
  365. udelay (1000);
  366. }
  367. if (tm >= 1000) {
  368. printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
  369. }
  370. DPRINT (("*** spi_xfer: ... transfer ended\n"));
  371. #ifdef DEBUG
  372. printf ("\nspi_xfer: txbuf after xfer\n");
  373. memdump ((void *) txbuf, 16); /* dump of txbuf before transmit */
  374. printf ("spi_xfer: rxbuf after xfer\n");
  375. memdump ((void *) rxbuf, 16); /* dump of rxbuf after transmit */
  376. printf ("\n");
  377. #endif
  378. /* Clear CS for device */
  379. cp->cp_pbdat |= 0x0001;
  380. return count;
  381. }
  382. #endif /* CONFIG_SPI || (CONFIG_POST & CONFIG_SYS_POST_SPI) */
  383. /*
  384. * SPI test
  385. *
  386. * The Serial Peripheral Interface (SPI) is tested in the local loopback mode.
  387. * The interface is configured accordingly and several packets
  388. * are transferred. The configurable test parameters are:
  389. * TEST_MIN_LENGTH - minimum size of packet to transfer
  390. * TEST_MAX_LENGTH - maximum size of packet to transfer
  391. * TEST_NUM - number of tests
  392. */
  393. #if CONFIG_POST & CONFIG_SYS_POST_SPI
  394. #define TEST_MIN_LENGTH 1
  395. #define TEST_MAX_LENGTH MAX_BUFFER
  396. #define TEST_NUM 1
  397. static void packet_fill (char * packet, int length)
  398. {
  399. char c = (char) length;
  400. int i;
  401. for (i = 0; i < length; i++)
  402. {
  403. packet[i] = c++;
  404. }
  405. }
  406. static int packet_check (char * packet, int length)
  407. {
  408. char c = (char) length;
  409. int i;
  410. for (i = 0; i < length; i++) {
  411. if (packet[i] != c++) return -1;
  412. }
  413. return 0;
  414. }
  415. int spi_post_test (int flags)
  416. {
  417. int res = -1;
  418. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  419. volatile cpm8xx_t *cp = (cpm8xx_t *) & immr->im_cpm;
  420. int i;
  421. int l;
  422. spi_init_f ();
  423. spi_init_r ();
  424. cp->cp_spmode |= SPMODE_LOOP;
  425. for (i = 0; i < TEST_NUM; i++) {
  426. for (l = TEST_MIN_LENGTH; l <= TEST_MAX_LENGTH; l += 8) {
  427. packet_fill ((char *)txbuf, l);
  428. spi_xfer (l);
  429. if (packet_check ((char *)rxbuf, l) < 0) {
  430. goto Done;
  431. }
  432. }
  433. }
  434. res = 0;
  435. Done:
  436. cp->cp_spmode &= ~SPMODE_LOOP;
  437. /*
  438. * SCC2 parameter RAM space overlaps
  439. * the SPI parameter RAM space. So we need to restore
  440. * the SCC2 configuration if it is used by UART.
  441. */
  442. #if !defined(CONFIG_8xx_CONS_NONE)
  443. serial_reinit_all ();
  444. #endif
  445. if (res != 0) {
  446. post_log ("SPI test failed\n");
  447. }
  448. return res;
  449. }
  450. #endif /* CONFIG_POST & CONFIG_SYS_POST_SPI */