mpc8xx_spi.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <dm.h>
  19. #include <mpc8xx.h>
  20. #include <spi.h>
  21. #include <asm/cpm_8xx.h>
  22. #include <asm/io.h>
  23. #define CPM_SPI_BASE_RX CPM_SPI_BASE
  24. #define CPM_SPI_BASE_TX (CPM_SPI_BASE + sizeof(cbd_t))
  25. #define MAX_BUFFER 0x104
  26. static int mpc8xx_spi_probe(struct udevice *dev)
  27. {
  28. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  29. cpm8xx_t __iomem *cp = &immr->im_cpm;
  30. spi_t __iomem *spi = (spi_t __iomem *)&cp->cp_dparam[PROFF_SPI];
  31. cbd_t __iomem *tbdf, *rbdf;
  32. /* Disable relocation */
  33. out_be16(&spi->spi_rpbase, 0);
  34. /* 1 */
  35. /* ------------------------------------------------
  36. * Initialize Port B SPI pins -> page 34-8 MPC860UM
  37. * (we are only in Master Mode !)
  38. * ------------------------------------------------ */
  39. /* --------------------------------------------
  40. * GPIO or per. Function
  41. * PBPAR[28] = 1 [0x00000008] -> PERI: (SPIMISO)
  42. * PBPAR[29] = 1 [0x00000004] -> PERI: (SPIMOSI)
  43. * PBPAR[30] = 1 [0x00000002] -> PERI: (SPICLK)
  44. * PBPAR[31] = 0 [0x00000001] -> GPIO: (CS for PCUE/CCM-EEPROM)
  45. * -------------------------------------------- */
  46. clrsetbits_be32(&cp->cp_pbpar, 0x00000001, 0x0000000E); /* set bits */
  47. /* ----------------------------------------------
  48. * In/Out or per. Function 0/1
  49. * PBDIR[28] = 1 [0x00000008] -> PERI1: SPIMISO
  50. * PBDIR[29] = 1 [0x00000004] -> PERI1: SPIMOSI
  51. * PBDIR[30] = 1 [0x00000002] -> PERI1: SPICLK
  52. * PBDIR[31] = 1 [0x00000001] -> GPIO OUT: CS for PCUE/CCM-EEPROM
  53. * ---------------------------------------------- */
  54. setbits_be32(&cp->cp_pbdir, 0x0000000F);
  55. /* ----------------------------------------------
  56. * open drain or active output
  57. * PBODR[28] = 1 [0x00000008] -> open drain: SPIMISO
  58. * PBODR[29] = 0 [0x00000004] -> active output SPIMOSI
  59. * PBODR[30] = 0 [0x00000002] -> active output: SPICLK
  60. * PBODR[31] = 0 [0x00000001] -> active output GPIO OUT: CS for PCUE/CCM
  61. * ---------------------------------------------- */
  62. clrsetbits_be16(&cp->cp_pbodr, 0x00000007, 0x00000008);
  63. /* Initialize the parameter ram.
  64. * We need to make sure many things are initialized to zero
  65. */
  66. out_be32(&spi->spi_rstate, 0);
  67. out_be32(&spi->spi_rdp, 0);
  68. out_be16(&spi->spi_rbptr, 0);
  69. out_be16(&spi->spi_rbc, 0);
  70. out_be32(&spi->spi_rxtmp, 0);
  71. out_be32(&spi->spi_tstate, 0);
  72. out_be32(&spi->spi_tdp, 0);
  73. out_be16(&spi->spi_tbptr, 0);
  74. out_be16(&spi->spi_tbc, 0);
  75. out_be32(&spi->spi_txtmp, 0);
  76. /* 3 */
  77. /* Set up the SPI parameters in the parameter ram */
  78. out_be16(&spi->spi_rbase, CPM_SPI_BASE_RX);
  79. out_be16(&spi->spi_tbase, CPM_SPI_BASE_TX);
  80. /***********IMPORTANT******************/
  81. /*
  82. * Setting transmit and receive buffer descriptor pointers
  83. * initially to rbase and tbase. Only the microcode patches
  84. * documentation talks about initializing this pointer. This
  85. * is missing from the sample I2C driver. If you dont
  86. * initialize these pointers, the kernel hangs.
  87. */
  88. out_be16(&spi->spi_rbptr, CPM_SPI_BASE_RX);
  89. out_be16(&spi->spi_tbptr, CPM_SPI_BASE_TX);
  90. /* 4 */
  91. /* Init SPI Tx + Rx Parameters */
  92. while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
  93. ;
  94. out_be16(&cp->cp_cpcr, mk_cr_cmd(CPM_CR_CH_SPI, CPM_CR_INIT_TRX) |
  95. CPM_CR_FLG);
  96. while (in_be16(&cp->cp_cpcr) & CPM_CR_FLG)
  97. ;
  98. /* 5 */
  99. /* Set SDMA configuration register */
  100. out_be32(&immr->im_siu_conf.sc_sdcr, 0x0001);
  101. /* 6 */
  102. /* Set to big endian. */
  103. out_8(&spi->spi_tfcr, SMC_EB);
  104. out_8(&spi->spi_rfcr, SMC_EB);
  105. /* 7 */
  106. /* Set maximum receive size. */
  107. out_be16(&spi->spi_mrblr, MAX_BUFFER);
  108. /* 8 + 9 */
  109. /* tx and rx buffer descriptors */
  110. tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
  111. rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
  112. clrbits_be16(&tbdf->cbd_sc, BD_SC_READY);
  113. clrbits_be16(&rbdf->cbd_sc, BD_SC_EMPTY);
  114. /* 10 + 11 */
  115. out_8(&cp->cp_spim, 0); /* Mask all SPI events */
  116. out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
  117. return 0;
  118. }
  119. static int mpc8xx_spi_xfer(struct udevice *dev, unsigned int bitlen,
  120. const void *dout, void *din, unsigned long flags)
  121. {
  122. immap_t __iomem *immr = (immap_t __iomem *)CONFIG_SYS_IMMR;
  123. cpm8xx_t __iomem *cp = &immr->im_cpm;
  124. cbd_t __iomem *tbdf, *rbdf;
  125. int tm;
  126. size_t count = (bitlen + 7) / 8;
  127. if (count > MAX_BUFFER)
  128. return -EINVAL;
  129. tbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_TX];
  130. rbdf = (cbd_t __iomem *)&cp->cp_dpmem[CPM_SPI_BASE_RX];
  131. /* Set CS for device */
  132. clrbits_be32(&cp->cp_pbdat, 0x0001);
  133. /* Setting tx bd status and data length */
  134. out_be32(&tbdf->cbd_bufaddr, (ulong)dout);
  135. out_be16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_LAST | BD_SC_WRAP);
  136. out_be16(&tbdf->cbd_datlen, count);
  137. /* Setting rx bd status and data length */
  138. out_be32(&rbdf->cbd_bufaddr, (ulong)din);
  139. out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_WRAP);
  140. out_be16(&rbdf->cbd_datlen, 0); /* rx length has no significance */
  141. clrsetbits_be16(&cp->cp_spmode, ~SPMODE_LOOP, SPMODE_REV | SPMODE_MSTR |
  142. SPMODE_EN | SPMODE_LEN(8) | SPMODE_PM(0x8));
  143. out_8(&cp->cp_spim, 0); /* Mask all SPI events */
  144. out_8(&cp->cp_spie, SPI_EMASK); /* Clear all SPI events */
  145. /* start spi transfer */
  146. setbits_8(&cp->cp_spcom, SPI_STR); /* Start transmit */
  147. /* --------------------------------
  148. * Wait for SPI transmit to get out
  149. * or time out (1 second = 1000 ms)
  150. * -------------------------------- */
  151. for (tm = 0; tm < 1000; ++tm) {
  152. if (in_8(&cp->cp_spie) & SPI_TXB) /* Tx Buffer Empty */
  153. break;
  154. if ((in_be16(&tbdf->cbd_sc) & BD_SC_READY) == 0)
  155. break;
  156. udelay(1000);
  157. }
  158. if (tm >= 1000)
  159. printf("*** spi_xfer: Time out while xferring to/from SPI!\n");
  160. /* Clear CS for device */
  161. setbits_be32(&cp->cp_pbdat, 0x0001);
  162. return count;
  163. }
  164. static const struct dm_spi_ops mpc8xx_spi_ops = {
  165. .xfer = mpc8xx_spi_xfer,
  166. };
  167. static const struct udevice_id mpc8xx_spi_ids[] = {
  168. { .compatible = "fsl,mpc8xx-spi" },
  169. { }
  170. };
  171. U_BOOT_DRIVER(mpc8xx_spi) = {
  172. .name = "mpc8xx_spi",
  173. .id = UCLASS_SPI,
  174. .of_match = mpc8xx_spi_ids,
  175. .ops = &mpc8xx_spi_ops,
  176. .probe = mpc8xx_spi_probe,
  177. };