spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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-2003 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. /*
  11. * MPC8260 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 <asm/cpm_8260.h>
  20. #include <linux/ctype.h>
  21. #include <malloc.h>
  22. #include <post.h>
  23. #include <net.h>
  24. #if defined(CONFIG_SPI)
  25. /* Warning:
  26. * You cannot enable DEBUG for early system initalization, i. e. when
  27. * this driver is used to read environment parameters like "baudrate"
  28. * from EEPROM which are used to initialize the serial port which is
  29. * needed to print the debug messages...
  30. */
  31. #undef DEBUG
  32. #define SPI_EEPROM_WREN 0x06
  33. #define SPI_EEPROM_RDSR 0x05
  34. #define SPI_EEPROM_READ 0x03
  35. #define SPI_EEPROM_WRITE 0x02
  36. /* ---------------------------------------------------------------
  37. * Offset for initial SPI buffers in DPRAM:
  38. * We need a 520 byte scratch DPRAM area to use at an early stage.
  39. * It is used between the two initialization calls (spi_init_f()
  40. * and spi_init_r()).
  41. * The value 0x2000 makes it far enough from the start of the data
  42. * area (as well as from the stack pointer).
  43. * --------------------------------------------------------------- */
  44. #ifndef CONFIG_SYS_SPI_INIT_OFFSET
  45. #define CONFIG_SYS_SPI_INIT_OFFSET 0x2000
  46. #endif
  47. #define CPM_SPI_BASE 0x100
  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 *)&((immap_t *)CONFIG_SYS_IMMR)->im_dprambase
  88. [CONFIG_SYS_SPI_INIT_OFFSET];
  89. static uchar *txbuf =
  90. (uchar *)&((immap_t *)CONFIG_SYS_IMMR)->im_dprambase
  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 cpm8260_t *cp;
  107. volatile cbd_t *tbdf, *rbdf;
  108. immr = (immap_t *) CONFIG_SYS_IMMR;
  109. cp = (cpm8260_t *) &immr->im_cpm;
  110. immr->im_dprambase16[PROFF_SPI_BASE / sizeof(u16)] = PROFF_SPI;
  111. spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
  112. /* 1 */
  113. /* ------------------------------------------------
  114. * Initialize Port D SPI pins
  115. * (we are only in Master Mode !)
  116. * ------------------------------------------------ */
  117. /* --------------------------------------------
  118. * GPIO or per. Function
  119. * PPARD[16] = 1 [0x00008000] (SPIMISO)
  120. * PPARD[17] = 1 [0x00004000] (SPIMOSI)
  121. * PPARD[18] = 1 [0x00002000] (SPICLK)
  122. * PPARD[12] = 0 [0x00080000] -> GPIO: (CS for ATC EEPROM)
  123. * -------------------------------------------- */
  124. immr->im_ioport.iop_ppard |= 0x0000E000; /* set bits */
  125. immr->im_ioport.iop_ppard &= ~0x00080000; /* reset bit */
  126. /* ----------------------------------------------
  127. * In/Out or per. Function 0/1
  128. * PDIRD[16] = 0 [0x00008000] -> PERI1: SPIMISO
  129. * PDIRD[17] = 0 [0x00004000] -> PERI1: SPIMOSI
  130. * PDIRD[18] = 0 [0x00002000] -> PERI1: SPICLK
  131. * PDIRD[12] = 1 [0x00080000] -> GPIO OUT: CS for ATC EEPROM
  132. * ---------------------------------------------- */
  133. immr->im_ioport.iop_pdird &= ~0x0000E000;
  134. immr->im_ioport.iop_pdird |= 0x00080000;
  135. /* ----------------------------------------------
  136. * special option reg.
  137. * PSORD[16] = 1 [0x00008000] -> SPIMISO
  138. * PSORD[17] = 1 [0x00004000] -> SPIMOSI
  139. * PSORD[18] = 1 [0x00002000] -> SPICLK
  140. * ---------------------------------------------- */
  141. immr->im_ioport.iop_psord |= 0x0000E000;
  142. /* Initialize the parameter ram.
  143. * We need to make sure many things are initialized to zero
  144. */
  145. spi->spi_rstate = 0;
  146. spi->spi_rdp = 0;
  147. spi->spi_rbptr = 0;
  148. spi->spi_rbc = 0;
  149. spi->spi_rxtmp = 0;
  150. spi->spi_tstate = 0;
  151. spi->spi_tdp = 0;
  152. spi->spi_tbptr = 0;
  153. spi->spi_tbc = 0;
  154. spi->spi_txtmp = 0;
  155. /* Allocate space for one transmit and one receive buffer
  156. * descriptor in the DP ram
  157. */
  158. #ifdef CONFIG_SYS_ALLOC_DPRAM
  159. dpaddr = m8260_cpm_dpalloc (sizeof(cbd_t)*2, 8);
  160. #else
  161. dpaddr = CPM_SPI_BASE;
  162. #endif
  163. /* 3 */
  164. /* Set up the SPI parameters in the parameter ram */
  165. spi->spi_rbase = dpaddr;
  166. spi->spi_tbase = dpaddr + sizeof (cbd_t);
  167. /***********IMPORTANT******************/
  168. /*
  169. * Setting transmit and receive buffer descriptor pointers
  170. * initially to rbase and tbase. Only the microcode patches
  171. * documentation talks about initializing this pointer. This
  172. * is missing from the sample I2C driver. If you dont
  173. * initialize these pointers, the kernel hangs.
  174. */
  175. spi->spi_rbptr = spi->spi_rbase;
  176. spi->spi_tbptr = spi->spi_tbase;
  177. /* 4 */
  178. /* Init SPI Tx + Rx Parameters */
  179. while (cp->cp_cpcr & CPM_CR_FLG)
  180. ;
  181. cp->cp_cpcr = mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK,
  182. 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  183. while (cp->cp_cpcr & CPM_CR_FLG)
  184. ;
  185. /* 6 */
  186. /* Set to big endian. */
  187. spi->spi_tfcr = CPMFCR_EB;
  188. spi->spi_rfcr = CPMFCR_EB;
  189. /* 7 */
  190. /* Set maximum receive size. */
  191. spi->spi_mrblr = MAX_BUFFER;
  192. /* 8 + 9 */
  193. /* tx and rx buffer descriptors */
  194. tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
  195. rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
  196. tbdf->cbd_sc &= ~BD_SC_READY;
  197. rbdf->cbd_sc &= ~BD_SC_EMPTY;
  198. /* Set the bd's rx and tx buffer address pointers */
  199. rbdf->cbd_bufaddr = (ulong) rxbuf;
  200. tbdf->cbd_bufaddr = (ulong) txbuf;
  201. /* 10 + 11 */
  202. immr->im_spi.spi_spie = SPI_EMASK; /* Clear all SPI events */
  203. immr->im_spi.spi_spim = 0x00; /* Mask all SPI events */
  204. return;
  205. }
  206. /* **************************************************************************
  207. *
  208. * Function: spi_init_r
  209. *
  210. * Description: Init SPI-Controller (RAM part) -
  211. * The malloc engine is ready and we can move our buffers to
  212. * normal RAM
  213. *
  214. * return: ---
  215. *
  216. * *********************************************************************** */
  217. void spi_init_r (void)
  218. {
  219. volatile spi_t *spi;
  220. volatile immap_t *immr;
  221. volatile cbd_t *tbdf, *rbdf;
  222. immr = (immap_t *) CONFIG_SYS_IMMR;
  223. spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
  224. /* tx and rx buffer descriptors */
  225. tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
  226. rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
  227. /* Allocate memory for RX and TX buffers */
  228. rxbuf = (uchar *) malloc (MAX_BUFFER);
  229. txbuf = (uchar *) malloc (MAX_BUFFER);
  230. rbdf->cbd_bufaddr = (ulong) rxbuf;
  231. tbdf->cbd_bufaddr = (ulong) txbuf;
  232. return;
  233. }
  234. /****************************************************************************
  235. * Function: spi_write
  236. **************************************************************************** */
  237. ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
  238. {
  239. int i;
  240. memset(rxbuf, 0, MAX_BUFFER);
  241. memset(txbuf, 0, MAX_BUFFER);
  242. *txbuf = SPI_EEPROM_WREN; /* write enable */
  243. spi_xfer(1);
  244. memcpy(txbuf, addr, alen);
  245. *txbuf = SPI_EEPROM_WRITE; /* WRITE memory array */
  246. memcpy(alen + txbuf, buffer, len);
  247. spi_xfer(alen + len);
  248. /* ignore received data */
  249. for (i = 0; i < 1000; i++) {
  250. *txbuf = SPI_EEPROM_RDSR; /* read status */
  251. txbuf[1] = 0;
  252. spi_xfer(2);
  253. if (!(rxbuf[1] & 1)) {
  254. break;
  255. }
  256. udelay(1000);
  257. }
  258. if (i >= 1000) {
  259. printf ("*** spi_write: Time out while writing!\n");
  260. }
  261. return len;
  262. }
  263. /****************************************************************************
  264. * Function: spi_read
  265. **************************************************************************** */
  266. ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
  267. {
  268. memset(rxbuf, 0, MAX_BUFFER);
  269. memset(txbuf, 0, MAX_BUFFER);
  270. memcpy(txbuf, addr, alen);
  271. *txbuf = SPI_EEPROM_READ; /* READ memory array */
  272. /*
  273. * There is a bug in 860T (?) that cuts the last byte of input
  274. * if we're reading into DPRAM. The solution we choose here is
  275. * to always read len+1 bytes (we have one extra byte at the
  276. * end of the buffer).
  277. */
  278. spi_xfer(alen + len + 1);
  279. memcpy(buffer, alen + rxbuf, len);
  280. return len;
  281. }
  282. /****************************************************************************
  283. * Function: spi_xfer
  284. **************************************************************************** */
  285. ssize_t spi_xfer (size_t count)
  286. {
  287. volatile immap_t *immr;
  288. volatile spi_t *spi;
  289. cbd_t *tbdf, *rbdf;
  290. int tm;
  291. DPRINT (("*** spi_xfer entered ***\n"));
  292. immr = (immap_t *) CONFIG_SYS_IMMR;
  293. spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
  294. tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
  295. rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
  296. /* Board-specific: Set CS for device (ATC EEPROM) */
  297. immr->im_ioport.iop_pdatd &= ~0x00080000;
  298. /* Setting tx bd status and data length */
  299. tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP;
  300. tbdf->cbd_datlen = count;
  301. DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
  302. tbdf->cbd_datlen));
  303. /* Setting rx bd status and data length */
  304. rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
  305. rbdf->cbd_datlen = 0; /* rx length has no significance */
  306. immr->im_spi.spi_spmode = SPMODE_REV |
  307. SPMODE_MSTR |
  308. SPMODE_EN |
  309. SPMODE_LEN(8) | /* 8 Bits per char */
  310. SPMODE_PM(0x8) ; /* medium speed */
  311. immr->im_spi.spi_spie = SPI_EMASK; /* Clear all SPI events */
  312. immr->im_spi.spi_spim = 0x00; /* Mask all SPI events */
  313. /* start spi transfer */
  314. DPRINT (("*** spi_xfer: Performing transfer ...\n"));
  315. immr->im_spi.spi_spcom |= SPI_STR; /* Start transmit */
  316. /* --------------------------------
  317. * Wait for SPI transmit to get out
  318. * or time out (1 second = 1000 ms)
  319. * -------------------------------- */
  320. for (tm=0; tm<1000; ++tm) {
  321. if (immr->im_spi.spi_spie & SPI_TXB) { /* Tx Buffer Empty */
  322. DPRINT (("*** spi_xfer: Tx buffer empty\n"));
  323. break;
  324. }
  325. if ((tbdf->cbd_sc & BD_SC_READY) == 0) {
  326. DPRINT (("*** spi_xfer: Tx BD done\n"));
  327. break;
  328. }
  329. udelay (1000);
  330. }
  331. if (tm >= 1000) {
  332. printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
  333. }
  334. DPRINT (("*** spi_xfer: ... transfer ended\n"));
  335. #ifdef DEBUG
  336. printf ("\nspi_xfer: txbuf after xfer\n");
  337. memdump ((void *) txbuf, 16); /* dump of txbuf before transmit */
  338. printf ("spi_xfer: rxbuf after xfer\n");
  339. memdump ((void *) rxbuf, 16); /* dump of rxbuf after transmit */
  340. printf ("\n");
  341. #endif
  342. /* Clear CS for device */
  343. immr->im_ioport.iop_pdatd |= 0x00080000;
  344. return count;
  345. }
  346. #endif /* CONFIG_SPI */