mpc8xx_spi.c 10 KB

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