spi-jcore.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * J-Core SPI controller driver
  3. *
  4. * Copyright (C) 2012-2016 Smart Energy Instruments, Inc.
  5. *
  6. * Current version by Rich Felker
  7. * Based loosely on initial version by Oleksandr G Zhadan
  8. *
  9. */
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/errno.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/spi/spi.h>
  16. #include <linux/clk.h>
  17. #include <linux/err.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <linux/delay.h>
  21. #define DRV_NAME "jcore_spi"
  22. #define CTRL_REG 0x0
  23. #define DATA_REG 0x4
  24. #define JCORE_SPI_CTRL_XMIT 0x02
  25. #define JCORE_SPI_STAT_BUSY 0x02
  26. #define JCORE_SPI_CTRL_LOOP 0x08
  27. #define JCORE_SPI_CTRL_CS_BITS 0x15
  28. #define JCORE_SPI_WAIT_RDY_MAX_LOOP 2000000
  29. struct jcore_spi {
  30. struct spi_master *master;
  31. void __iomem *base;
  32. unsigned int cs_reg;
  33. unsigned int speed_reg;
  34. unsigned int speed_hz;
  35. unsigned int clock_freq;
  36. };
  37. static int jcore_spi_wait(void __iomem *ctrl_reg)
  38. {
  39. unsigned timeout = JCORE_SPI_WAIT_RDY_MAX_LOOP;
  40. do {
  41. if (!(readl(ctrl_reg) & JCORE_SPI_STAT_BUSY))
  42. return 0;
  43. cpu_relax();
  44. } while (--timeout);
  45. return -EBUSY;
  46. }
  47. static void jcore_spi_program(struct jcore_spi *hw)
  48. {
  49. void __iomem *ctrl_reg = hw->base + CTRL_REG;
  50. if (jcore_spi_wait(ctrl_reg))
  51. dev_err(hw->master->dev.parent,
  52. "timeout waiting to program ctrl reg.\n");
  53. writel(hw->cs_reg | hw->speed_reg, ctrl_reg);
  54. }
  55. static void jcore_spi_chipsel(struct spi_device *spi, bool value)
  56. {
  57. struct jcore_spi *hw = spi_master_get_devdata(spi->master);
  58. u32 csbit = 1U << (2 * spi->chip_select);
  59. dev_dbg(hw->master->dev.parent, "chipselect %d\n", spi->chip_select);
  60. if (value)
  61. hw->cs_reg |= csbit;
  62. else
  63. hw->cs_reg &= ~csbit;
  64. jcore_spi_program(hw);
  65. }
  66. static void jcore_spi_baudrate(struct jcore_spi *hw, int speed)
  67. {
  68. if (speed == hw->speed_hz) return;
  69. hw->speed_hz = speed;
  70. if (speed >= hw->clock_freq / 2)
  71. hw->speed_reg = 0;
  72. else
  73. hw->speed_reg = ((hw->clock_freq / 2 / speed) - 1) << 27;
  74. jcore_spi_program(hw);
  75. dev_dbg(hw->master->dev.parent, "speed=%d reg=0x%x\n",
  76. speed, hw->speed_reg);
  77. }
  78. static int jcore_spi_txrx(struct spi_master *master, struct spi_device *spi,
  79. struct spi_transfer *t)
  80. {
  81. struct jcore_spi *hw = spi_master_get_devdata(master);
  82. void __iomem *ctrl_reg = hw->base + CTRL_REG;
  83. void __iomem *data_reg = hw->base + DATA_REG;
  84. u32 xmit;
  85. /* data buffers */
  86. const unsigned char *tx;
  87. unsigned char *rx;
  88. unsigned int len;
  89. unsigned int count;
  90. jcore_spi_baudrate(hw, t->speed_hz);
  91. xmit = hw->cs_reg | hw->speed_reg | JCORE_SPI_CTRL_XMIT;
  92. tx = t->tx_buf;
  93. rx = t->rx_buf;
  94. len = t->len;
  95. for (count = 0; count < len; count++) {
  96. if (jcore_spi_wait(ctrl_reg))
  97. break;
  98. writel(tx ? *tx++ : 0, data_reg);
  99. writel(xmit, ctrl_reg);
  100. if (jcore_spi_wait(ctrl_reg))
  101. break;
  102. if (rx)
  103. *rx++ = readl(data_reg);
  104. }
  105. spi_finalize_current_transfer(master);
  106. if (count < len)
  107. return -EREMOTEIO;
  108. return 0;
  109. }
  110. static int jcore_spi_probe(struct platform_device *pdev)
  111. {
  112. struct device_node *node = pdev->dev.of_node;
  113. struct jcore_spi *hw;
  114. struct spi_master *master;
  115. struct resource *res;
  116. u32 clock_freq;
  117. struct clk *clk;
  118. int err = -ENODEV;
  119. master = spi_alloc_master(&pdev->dev, sizeof(struct jcore_spi));
  120. if (!master)
  121. return err;
  122. /* Setup the master state. */
  123. master->num_chipselect = 3;
  124. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  125. master->transfer_one = jcore_spi_txrx;
  126. master->set_cs = jcore_spi_chipsel;
  127. master->dev.of_node = node;
  128. master->bus_num = pdev->id;
  129. hw = spi_master_get_devdata(master);
  130. hw->master = master;
  131. platform_set_drvdata(pdev, hw);
  132. /* Find and map our resources */
  133. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  134. if (!res)
  135. goto exit_busy;
  136. if (!devm_request_mem_region(&pdev->dev, res->start,
  137. resource_size(res), pdev->name))
  138. goto exit_busy;
  139. hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
  140. resource_size(res));
  141. if (!hw->base)
  142. goto exit_busy;
  143. /*
  144. * The SPI clock rate controlled via a configurable clock divider
  145. * which is applied to the reference clock. A 50 MHz reference is
  146. * most suitable for obtaining standard SPI clock rates, but some
  147. * designs may have a different reference clock, and the DT must
  148. * make the driver aware so that it can properly program the
  149. * requested rate. If the clock is omitted, 50 MHz is assumed.
  150. */
  151. clock_freq = 50000000;
  152. clk = devm_clk_get(&pdev->dev, "ref_clk");
  153. if (!IS_ERR(clk)) {
  154. if (clk_prepare_enable(clk) == 0) {
  155. clock_freq = clk_get_rate(clk);
  156. clk_disable_unprepare(clk);
  157. } else
  158. dev_warn(&pdev->dev, "could not enable ref_clk\n");
  159. }
  160. hw->clock_freq = clock_freq;
  161. /* Initialize all CS bits to high. */
  162. hw->cs_reg = JCORE_SPI_CTRL_CS_BITS;
  163. jcore_spi_baudrate(hw, 400000);
  164. /* Register our spi controller */
  165. err = devm_spi_register_master(&pdev->dev, master);
  166. if (err)
  167. goto exit;
  168. return 0;
  169. exit_busy:
  170. err = -EBUSY;
  171. exit:
  172. spi_master_put(master);
  173. return err;
  174. }
  175. static const struct of_device_id jcore_spi_of_match[] = {
  176. { .compatible = "jcore,spi2" },
  177. {},
  178. };
  179. MODULE_DEVICE_TABLE(of, jcore_spi_of_match);
  180. static struct platform_driver jcore_spi_driver = {
  181. .probe = jcore_spi_probe,
  182. .driver = {
  183. .name = DRV_NAME,
  184. .of_match_table = jcore_spi_of_match,
  185. },
  186. };
  187. module_platform_driver(jcore_spi_driver);
  188. MODULE_DESCRIPTION("J-Core SPI driver");
  189. MODULE_AUTHOR("Rich Felker <dalias@libc.org>");
  190. MODULE_LICENSE("GPL");
  191. MODULE_ALIAS("platform:" DRV_NAME);