spi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 cpm8xx_t *cp;
  123. volatile cbd_t *tbdf, *rbdf;
  124. immr = (immap_t *) CONFIG_SYS_IMMR;
  125. cp = (cpm8xx_t *) &immr->im_cpm;
  126. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  127. spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
  128. #else
  129. spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
  130. /* Disable relocation */
  131. spi->spi_rpbase = 0;
  132. #endif
  133. /* 1 */
  134. /* ------------------------------------------------
  135. * Initialize Port B SPI pins -> page 34-8 MPC860UM
  136. * (we are only in Master Mode !)
  137. * ------------------------------------------------ */
  138. /* --------------------------------------------
  139. * GPIO or per. Function
  140. * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
  141. * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
  142. * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
  143. * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
  144. * -------------------------------------------- */
  145. cp->cp_pbpar |= 0x0000000E; /* set bits */
  146. cp->cp_pbpar &= ~0x00000001; /* reset bit */
  147. /* ----------------------------------------------
  148. * In/Out or per. Function 0/1
  149. * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
  150. * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
  151. * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
  152. * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
  153. * ---------------------------------------------- */
  154. cp->cp_pbdir |= 0x0000000F;
  155. /* ----------------------------------------------
  156. * open drain or active output
  157. * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
  158. * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
  159. * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
  160. * PBODR[31] = 0 [0x00000001] -> active output: GPIO OUT: CS for PCUE/CCM
  161. * ---------------------------------------------- */
  162. cp->cp_pbodr |= 0x00000008;
  163. cp->cp_pbodr &= ~0x00000007;
  164. /* Initialize the parameter ram.
  165. * We need to make sure many things are initialized to zero
  166. */
  167. spi->spi_rstate = 0;
  168. spi->spi_rdp = 0;
  169. spi->spi_rbptr = 0;
  170. spi->spi_rbc = 0;
  171. spi->spi_rxtmp = 0;
  172. spi->spi_tstate = 0;
  173. spi->spi_tdp = 0;
  174. spi->spi_tbptr = 0;
  175. spi->spi_tbc = 0;
  176. spi->spi_txtmp = 0;
  177. /* Allocate space for one transmit and one receive buffer
  178. * descriptor in the DP ram
  179. */
  180. #ifdef CONFIG_SYS_ALLOC_DPRAM
  181. dpaddr = dpram_alloc_align (sizeof(cbd_t)*2, 8);
  182. #else
  183. dpaddr = CPM_SPI_BASE;
  184. #endif
  185. /* 3 */
  186. /* Set up the SPI parameters in the parameter ram */
  187. spi->spi_rbase = dpaddr;
  188. spi->spi_tbase = dpaddr + sizeof (cbd_t);
  189. /***********IMPORTANT******************/
  190. /*
  191. * Setting transmit and receive buffer descriptor pointers
  192. * initially to rbase and tbase. Only the microcode patches
  193. * documentation talks about initializing this pointer. This
  194. * is missing from the sample I2C driver. If you dont
  195. * initialize these pointers, the kernel hangs.
  196. */
  197. spi->spi_rbptr = spi->spi_rbase;
  198. spi->spi_tbptr = spi->spi_tbase;
  199. /* 4 */
  200. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  201. /*
  202. * Initialize required parameters if using microcode patch.
  203. */
  204. spi->spi_rstate = 0;
  205. spi->spi_tstate = 0;
  206. #else
  207. /* Init SPI Tx + Rx Parameters */
  208. while (cp->cp_cpcr & CPM_CR_FLG)
  209. ;
  210. cp->cp_cpcr = mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  211. while (cp->cp_cpcr & CPM_CR_FLG)
  212. ;
  213. #endif /* CONFIG_SYS_SPI_UCODE_PATCH */
  214. /* 5 */
  215. /* Set SDMA configuration register */
  216. immr->im_siu_conf.sc_sdcr = 0x0001;
  217. /* 6 */
  218. /* Set to big endian. */
  219. spi->spi_tfcr = SMC_EB;
  220. spi->spi_rfcr = SMC_EB;
  221. /* 7 */
  222. /* Set maximum receive size. */
  223. spi->spi_mrblr = MAX_BUFFER;
  224. /* 8 + 9 */
  225. /* tx and rx buffer descriptors */
  226. tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
  227. rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
  228. tbdf->cbd_sc &= ~BD_SC_READY;
  229. rbdf->cbd_sc &= ~BD_SC_EMPTY;
  230. /* Set the bd's rx and tx buffer address pointers */
  231. rbdf->cbd_bufaddr = (ulong) rxbuf;
  232. tbdf->cbd_bufaddr = (ulong) txbuf;
  233. /* 10 + 11 */
  234. cp->cp_spim = 0; /* Mask all SPI events */
  235. cp->cp_spie = SPI_EMASK; /* Clear all SPI events */
  236. return;
  237. }
  238. /* **************************************************************************
  239. *
  240. * Function: spi_init_r
  241. *
  242. * Description: Init SPI-Controller (RAM part) -
  243. * The malloc engine is ready and we can move our buffers to
  244. * normal RAM
  245. *
  246. * return: ---
  247. *
  248. * *********************************************************************** */
  249. void spi_init_r (void)
  250. {
  251. volatile cpm8xx_t *cp;
  252. volatile spi_t *spi;
  253. volatile immap_t *immr;
  254. volatile cbd_t *tbdf, *rbdf;
  255. immr = (immap_t *) CONFIG_SYS_IMMR;
  256. cp = (cpm8xx_t *) &immr->im_cpm;
  257. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  258. spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
  259. #else
  260. spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
  261. /* Disable relocation */
  262. spi->spi_rpbase = 0;
  263. #endif
  264. /* tx and rx buffer descriptors */
  265. tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
  266. rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
  267. /* Allocate memory for RX and TX buffers */
  268. rxbuf = (uchar *) malloc (MAX_BUFFER);
  269. txbuf = (uchar *) malloc (MAX_BUFFER);
  270. rbdf->cbd_bufaddr = (ulong) rxbuf;
  271. tbdf->cbd_bufaddr = (ulong) txbuf;
  272. return;
  273. }
  274. /****************************************************************************
  275. * Function: spi_write
  276. **************************************************************************** */
  277. ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
  278. {
  279. int i;
  280. memset(rxbuf, 0, MAX_BUFFER);
  281. memset(txbuf, 0, MAX_BUFFER);
  282. *txbuf = SPI_EEPROM_WREN; /* write enable */
  283. spi_xfer(1);
  284. memcpy(txbuf, addr, alen);
  285. *txbuf = SPI_EEPROM_WRITE; /* WRITE memory array */
  286. memcpy(alen + txbuf, buffer, len);
  287. spi_xfer(alen + len);
  288. /* ignore received data */
  289. for (i = 0; i < 1000; i++) {
  290. *txbuf = SPI_EEPROM_RDSR; /* read status */
  291. txbuf[1] = 0;
  292. spi_xfer(2);
  293. if (!(rxbuf[1] & 1)) {
  294. break;
  295. }
  296. udelay(1000);
  297. }
  298. if (i >= 1000) {
  299. printf ("*** spi_write: Time out while writing!\n");
  300. }
  301. return len;
  302. }
  303. /****************************************************************************
  304. * Function: spi_read
  305. **************************************************************************** */
  306. ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
  307. {
  308. memset(rxbuf, 0, MAX_BUFFER);
  309. memset(txbuf, 0, MAX_BUFFER);
  310. memcpy(txbuf, addr, alen);
  311. *txbuf = SPI_EEPROM_READ; /* READ memory array */
  312. /*
  313. * There is a bug in 860T (?) that cuts the last byte of input
  314. * if we're reading into DPRAM. The solution we choose here is
  315. * to always read len+1 bytes (we have one extra byte at the
  316. * end of the buffer).
  317. */
  318. spi_xfer(alen + len + 1);
  319. memcpy(buffer, alen + rxbuf, len);
  320. return len;
  321. }
  322. /****************************************************************************
  323. * Function: spi_xfer
  324. **************************************************************************** */
  325. ssize_t spi_xfer (size_t count)
  326. {
  327. volatile immap_t *immr;
  328. volatile cpm8xx_t *cp;
  329. volatile spi_t *spi;
  330. cbd_t *tbdf, *rbdf;
  331. ushort loop;
  332. int tm;
  333. DPRINT (("*** spi_xfer entered ***\n"));
  334. immr = (immap_t *) CONFIG_SYS_IMMR;
  335. cp = (cpm8xx_t *) &immr->im_cpm;
  336. #ifdef CONFIG_SYS_SPI_UCODE_PATCH
  337. spi = (spi_t *)&cp->cp_dpmem[spi->spi_rpbase];
  338. #else
  339. spi = (spi_t *)&cp->cp_dparam[PROFF_SPI];
  340. /* Disable relocation */
  341. spi->spi_rpbase = 0;
  342. #endif
  343. tbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_tbase];
  344. rbdf = (cbd_t *) & cp->cp_dpmem[spi->spi_rbase];
  345. /* Set CS for device */
  346. cp->cp_pbdat &= ~0x0001;
  347. /* Setting tx bd status and data length */
  348. tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP;
  349. tbdf->cbd_datlen = count;
  350. DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
  351. tbdf->cbd_datlen));
  352. /* Setting rx bd status and data length */
  353. rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
  354. rbdf->cbd_datlen = 0; /* rx length has no significance */
  355. loop = cp->cp_spmode & SPMODE_LOOP;
  356. cp->cp_spmode = /*SPMODE_DIV16 |*/ /* BRG/16 mode not used here */
  357. loop |
  358. SPMODE_REV |
  359. SPMODE_MSTR |
  360. SPMODE_EN |
  361. SPMODE_LEN(8) | /* 8 Bits per char */
  362. SPMODE_PM(0x8) ; /* medium speed */
  363. cp->cp_spim = 0; /* Mask all SPI events */
  364. cp->cp_spie = SPI_EMASK; /* Clear all SPI events */
  365. /* start spi transfer */
  366. DPRINT (("*** spi_xfer: Performing transfer ...\n"));
  367. cp->cp_spcom |= SPI_STR; /* Start transmit */
  368. /* --------------------------------
  369. * Wait for SPI transmit to get out
  370. * or time out (1 second = 1000 ms)
  371. * -------------------------------- */
  372. for (tm=0; tm<1000; ++tm) {
  373. if (cp->cp_spie & SPI_TXB) { /* Tx Buffer Empty */
  374. DPRINT (("*** spi_xfer: Tx buffer empty\n"));
  375. break;
  376. }
  377. if ((tbdf->cbd_sc & BD_SC_READY) == 0) {
  378. DPRINT (("*** spi_xfer: Tx BD done\n"));
  379. break;
  380. }
  381. udelay (1000);
  382. }
  383. if (tm >= 1000) {
  384. printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
  385. }
  386. DPRINT (("*** spi_xfer: ... transfer ended\n"));
  387. #ifdef DEBUG
  388. printf ("\nspi_xfer: txbuf after xfer\n");
  389. memdump ((void *) txbuf, 16); /* dump of txbuf before transmit */
  390. printf ("spi_xfer: rxbuf after xfer\n");
  391. memdump ((void *) rxbuf, 16); /* dump of rxbuf after transmit */
  392. printf ("\n");
  393. #endif
  394. /* Clear CS for device */
  395. cp->cp_pbdat |= 0x0001;
  396. return count;
  397. }
  398. #endif /* CONFIG_SPI || (CONFIG_POST & CONFIG_SYS_POST_SPI) */
  399. /*
  400. * SPI test
  401. *
  402. * The Serial Peripheral Interface (SPI) is tested in the local loopback mode.
  403. * The interface is configured accordingly and several packets
  404. * are transfered. The configurable test parameters are:
  405. * TEST_MIN_LENGTH - minimum size of packet to transfer
  406. * TEST_MAX_LENGTH - maximum size of packet to transfer
  407. * TEST_NUM - number of tests
  408. */
  409. #if CONFIG_POST & CONFIG_SYS_POST_SPI
  410. #define TEST_MIN_LENGTH 1
  411. #define TEST_MAX_LENGTH MAX_BUFFER
  412. #define TEST_NUM 1
  413. static void packet_fill (char * packet, int length)
  414. {
  415. char c = (char) length;
  416. int i;
  417. for (i = 0; i < length; i++)
  418. {
  419. packet[i] = c++;
  420. }
  421. }
  422. static int packet_check (char * packet, int length)
  423. {
  424. char c = (char) length;
  425. int i;
  426. for (i = 0; i < length; i++) {
  427. if (packet[i] != c++) return -1;
  428. }
  429. return 0;
  430. }
  431. int spi_post_test (int flags)
  432. {
  433. int res = -1;
  434. volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
  435. volatile cpm8xx_t *cp = (cpm8xx_t *) & immr->im_cpm;
  436. int i;
  437. int l;
  438. spi_init_f ();
  439. spi_init_r ();
  440. cp->cp_spmode |= SPMODE_LOOP;
  441. for (i = 0; i < TEST_NUM; i++) {
  442. for (l = TEST_MIN_LENGTH; l <= TEST_MAX_LENGTH; l += 8) {
  443. packet_fill ((char *)txbuf, l);
  444. spi_xfer (l);
  445. if (packet_check ((char *)rxbuf, l) < 0) {
  446. goto Done;
  447. }
  448. }
  449. }
  450. res = 0;
  451. Done:
  452. cp->cp_spmode &= ~SPMODE_LOOP;
  453. /*
  454. * SCC2 parameter RAM space overlaps
  455. * the SPI parameter RAM space. So we need to restore
  456. * the SCC2 configuration if it is used by UART.
  457. */
  458. #if !defined(CONFIG_8xx_CONS_NONE)
  459. serial_reinit_all ();
  460. #endif
  461. if (res != 0) {
  462. post_log ("SPI test failed\n");
  463. }
  464. return res;
  465. }
  466. #endif /* CONFIG_POST & CONFIG_SYS_POST_SPI */