mpc8xx_spi.c 10 KB

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