spi.c 15 KB

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