spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. #ifdef CONFIG_SPI
  26. #define SPI_EEPROM_WREN 0x06
  27. #define SPI_EEPROM_RDSR 0x05
  28. #define SPI_EEPROM_READ 0x03
  29. #define SPI_EEPROM_WRITE 0x02
  30. /* ---------------------------------------------------------------
  31. * Offset for initial SPI buffers in DPRAM:
  32. * We need a 520 byte scratch DPRAM area to use at an early stage.
  33. * It is used between the two initialization calls (spi_init_f()
  34. * and spi_init_r()).
  35. * The value 0xb00 makes it far enough from the start of the data
  36. * area (as well as from the stack pointer).
  37. * --------------------------------------------------------------- */
  38. #ifndef CONFIG_SYS_SPI_INIT_OFFSET
  39. #define CONFIG_SYS_SPI_INIT_OFFSET 0xB00
  40. #endif
  41. #define CPM_SPI_BASE_RX CPM_SPI_BASE
  42. #define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
  43. /* -------------------
  44. * Function prototypes
  45. * ------------------- */
  46. ssize_t spi_xfer(size_t);
  47. /* -------------------
  48. * Variables
  49. * ------------------- */
  50. #define MAX_BUFFER 0x104
  51. /* ----------------------------------------------------------------------
  52. * Initially we place the RX and TX buffers at a fixed location in DPRAM!
  53. * ---------------------------------------------------------------------- */
  54. static uchar *rxbuf =
  55. (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
  56. [CONFIG_SYS_SPI_INIT_OFFSET];
  57. static uchar *txbuf =
  58. (uchar *)&((cpm8xx_t *)&((immap_t *)CONFIG_SYS_IMMR)->im_cpm)->cp_dpmem
  59. [CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
  60. /* **************************************************************************
  61. *
  62. * Function: spi_init_f
  63. *
  64. * Description: Init SPI-Controller (ROM part)
  65. *
  66. * return: ---
  67. *
  68. * *********************************************************************** */
  69. void spi_init_f(void)
  70. {
  71. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  72. cpm8xx_t __iomem *cp = &immr->im_cpm;
  73. spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
  74. cbd_t __iomem *tbdf, *rbdf;
  75. /* Disable relocation */
  76. out_be16(&spi->spi_rpbase, 0);
  77. /* 1 */
  78. /* ------------------------------------------------
  79. * Initialize Port B SPI pins -> page 34-8 MPC860UM
  80. * (we are only in Master Mode !)
  81. * ------------------------------------------------ */
  82. /* --------------------------------------------
  83. * GPIO or per. Function
  84. * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
  85. * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
  86. * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
  87. * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
  88. * -------------------------------------------- */
  89. clrsetbits_be32(&cp->cp_pbpar, 0x00000001, 0x0000000E); /* set bits */
  90. /* ----------------------------------------------
  91. * In/Out or per. Function 0/1
  92. * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
  93. * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
  94. * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
  95. * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
  96. * ---------------------------------------------- */
  97. setbits_be32(&cp->cp_pbdir, 0x0000000F);
  98. /* ----------------------------------------------
  99. * open drain or active output
  100. * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
  101. * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
  102. * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
  103. * PBODR[31] = 0 [0x00000001] -> active output GPIO OUT: CS for PCUE/CCM
  104. * ---------------------------------------------- */
  105. clrsetbits_be16(&cp->cp_pbodr, 0x00000007, 0x00000008);
  106. /* Initialize the parameter ram.
  107. * We need to make sure many things are initialized to zero
  108. */
  109. out_be32(&spi->spi_rstate, 0);
  110. out_be32(&spi->spi_rdp, 0);
  111. out_be16(&spi->spi_rbptr, 0);
  112. out_be16(&spi->spi_rbc, 0);
  113. out_be32(&spi->spi_rxtmp, 0);
  114. out_be32(&spi->spi_tstate, 0);
  115. out_be32(&spi->spi_tdp, 0);
  116. out_be16(&spi->spi_tbptr, 0);
  117. out_be16(&spi->spi_tbc, 0);
  118. out_be32(&spi->spi_txtmp, 0);
  119. /* 3 */
  120. /* Set up the SPI parameters in the parameter ram */
  121. out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
  122. out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
  123. /***********IMPORTANT******************/
  124. /*
  125. * Setting transmit and receive buffer descriptor pointers
  126. * initially to rbase and tbase. Only the microcode patches
  127. * documentation talks about initializing this pointer. This
  128. * is missing from the sample I2C driver. If you dont
  129. * initialize these pointers, the kernel hangs.
  130. */
  131. out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
  132. out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
  133. /* 4 */
  134. /* Init SPI Tx + Rx Parameters */
  135. while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
  136. ;
  137. out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
  138. CPM_CR_FLG);
  139. while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
  140. ;
  141. /* 5 */
  142. /* Set SDMA configuration register */
  143. out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
  144. /* 6 */
  145. /* Set to big endian. */
  146. out_8(&spi->spi_tfcr, SMC_EB);
  147. out_8(&spi->spi_rfcr, SMC_EB);
  148. /* 7 */
  149. /* Set maximum receive size. */
  150. out_be16(&spi->spi_mrblr, MAX_BUFFER);
  151. /* 8 + 9 */
  152. /* tx and rx buffer descriptors */
  153. tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
  154. rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
  155. clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
  156. clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
  157. /* Set the bd's rx and tx buffer address pointers */
  158. out_be32(&rbdf->cbd_bufaddr, (ulong)rxbuf);
  159. out_be32(&tbdf->cbd_bufaddr, (ulong)txbuf);
  160. /* 10 + 11 */
  161. out_8(&cp->cp_spim, 0); /* Mask all SPI events */
  162. out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
  163. return;
  164. }
  165. /* **************************************************************************
  166. *
  167. * Function: spi_init_r
  168. *
  169. * Description: Init SPI-Controller (RAM part) -
  170. * The malloc engine is ready and we can move our buffers to
  171. * normal RAM
  172. *
  173. * return: ---
  174. *
  175. * *********************************************************************** */
  176. void spi_init_r(void)
  177. {
  178. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  179. cpm8xx_t __iomem *cp = &immr->im_cpm;
  180. spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
  181. cbd_t __iomem *tbdf, *rbdf;
  182. /* Disable relocation */
  183. out_be16(&spi->spi_rpbase, 0);
  184. /* tx and rx buffer descriptors */
  185. tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
  186. rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
  187. /* Allocate memory for RX and TX buffers */
  188. rxbuf = (uchar *)malloc(MAX_BUFFER);
  189. txbuf = (uchar *)malloc(MAX_BUFFER);
  190. out_be32(&rbdf->cbd_bufaddr, (ulong)rxbuf);
  191. out_be32(&tbdf->cbd_bufaddr, (ulong)txbuf);
  192. return;
  193. }
  194. /****************************************************************************
  195. * Function: spi_write
  196. **************************************************************************** */
  197. ssize_t spi_write(uchar *addr, int alen, uchar *buffer, int len)
  198. {
  199. int i;
  200. memset(rxbuf, 0, MAX_BUFFER);
  201. memset(txbuf, 0, MAX_BUFFER);
  202. *txbuf = SPI_EEPROM_WREN; /* write enable */
  203. spi_xfer(1);
  204. memcpy(txbuf, addr, alen);
  205. *txbuf = SPI_EEPROM_WRITE; /* WRITE memory array */
  206. memcpy(alen + txbuf, buffer, len);
  207. spi_xfer(alen + len);
  208. /* ignore received data */
  209. for (i = 0; i < 1000; i++) {
  210. *txbuf = SPI_EEPROM_RDSR; /* read status */
  211. txbuf[1] = 0;
  212. spi_xfer(2);
  213. if (!(rxbuf[1] & 1))
  214. break;
  215. udelay(1000);
  216. }
  217. if (i >= 1000)
  218. printf("*** spi_write: Time out while writing!\n");
  219. return len;
  220. }
  221. /****************************************************************************
  222. * Function: spi_read
  223. **************************************************************************** */
  224. ssize_t spi_read(uchar *addr, int alen, uchar *buffer, int len)
  225. {
  226. memset(rxbuf, 0, MAX_BUFFER);
  227. memset(txbuf, 0, MAX_BUFFER);
  228. memcpy(txbuf, addr, alen);
  229. *txbuf = SPI_EEPROM_READ; /* READ memory array */
  230. /*
  231. * There is a bug in 860T (?) that cuts the last byte of input
  232. * if we're reading into DPRAM. The solution we choose here is
  233. * to always read len+1 bytes (we have one extra byte at the
  234. * end of the buffer).
  235. */
  236. spi_xfer(alen + len + 1);
  237. memcpy(buffer, alen + rxbuf, len);
  238. return len;
  239. }
  240. /****************************************************************************
  241. * Function: spi_xfer
  242. **************************************************************************** */
  243. ssize_t spi_xfer(size_t count)
  244. {
  245. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  246. cpm8xx_t __iomem *cp = &immr->im_cpm;
  247. spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
  248. cbd_t __iomem *tbdf, *rbdf;
  249. int tm;
  250. /* Disable relocation */
  251. out_be16(&spi->spi_rpbase, 0);
  252. tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
  253. rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
  254. /* Set CS for device */
  255. clrbits_be32(&cp->cp_pbdat, 0x0001);
  256. /* Setting tx bd status and data length */
  257. out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
  258. out_be16(&tbdf->cbd_datlen, count);
  259. /* Setting rx bd status and data length */
  260. out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
  261. out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
  262. clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
  263. SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
  264. out_8(&cp->cp_spim, 0); /* Mask all SPI events */
  265. out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
  266. /* start spi transfer */
  267. setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
  268. /* --------------------------------
  269. * Wait for SPI transmit to get out
  270. * or time out (1 second = 1000 ms)
  271. * -------------------------------- */
  272. for (tm = 0; tm < 1000; ++tm) {
  273. if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
  274. break;
  275. if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
  276. break;
  277. udelay(1000);
  278. }
  279. if (tm >= 1000)
  280. printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
  281. /* Clear CS for device */
  282. setbits_be32(&cp->cp_pbdat, 0x0001);
  283. return count;
  284. }
  285. #endif /* CONFIG_SPI */