spi-ath79.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SPI controller driver for the Atheros AR71XX/AR724X/AR913X SoCs
  4. *
  5. * Copyright (C) 2009-2011 Gabor Juhos <[email protected]>
  6. *
  7. * This driver has been based on the spi-gpio.c:
  8. * Copyright (C) 2006,2008 David Brownell
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/delay.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/io.h>
  16. #include <linux/spi/spi.h>
  17. #include <linux/spi/spi-mem.h>
  18. #include <linux/spi/spi_bitbang.h>
  19. #include <linux/bitops.h>
  20. #include <linux/clk.h>
  21. #include <linux/err.h>
  22. #define DRV_NAME "ath79-spi"
  23. #define ATH79_SPI_RRW_DELAY_FACTOR 12000
  24. #define MHZ (1000 * 1000)
  25. #define AR71XX_SPI_REG_FS 0x00 /* Function Select */
  26. #define AR71XX_SPI_REG_CTRL 0x04 /* SPI Control */
  27. #define AR71XX_SPI_REG_IOC 0x08 /* SPI I/O Control */
  28. #define AR71XX_SPI_REG_RDS 0x0c /* Read Data Shift */
  29. #define AR71XX_SPI_FS_GPIO BIT(0) /* Enable GPIO mode */
  30. #define AR71XX_SPI_IOC_DO BIT(0) /* Data Out pin */
  31. #define AR71XX_SPI_IOC_CLK BIT(8) /* CLK pin */
  32. #define AR71XX_SPI_IOC_CS(n) BIT(16 + (n))
  33. struct ath79_spi {
  34. struct spi_bitbang bitbang;
  35. u32 ioc_base;
  36. u32 reg_ctrl;
  37. void __iomem *base;
  38. struct clk *clk;
  39. unsigned int rrw_delay;
  40. };
  41. static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned int reg)
  42. {
  43. return ioread32(sp->base + reg);
  44. }
  45. static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned int reg, u32 val)
  46. {
  47. iowrite32(val, sp->base + reg);
  48. }
  49. static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi)
  50. {
  51. return spi_master_get_devdata(spi->master);
  52. }
  53. static inline void ath79_spi_delay(struct ath79_spi *sp, unsigned int nsecs)
  54. {
  55. if (nsecs > sp->rrw_delay)
  56. ndelay(nsecs - sp->rrw_delay);
  57. }
  58. static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
  59. {
  60. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  61. int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
  62. u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
  63. if (cs_high)
  64. sp->ioc_base |= cs_bit;
  65. else
  66. sp->ioc_base &= ~cs_bit;
  67. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  68. }
  69. static void ath79_spi_enable(struct ath79_spi *sp)
  70. {
  71. /* enable GPIO mode */
  72. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
  73. /* save CTRL register */
  74. sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
  75. sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
  76. /* clear clk and mosi in the base state */
  77. sp->ioc_base &= ~(AR71XX_SPI_IOC_DO | AR71XX_SPI_IOC_CLK);
  78. /* TODO: setup speed? */
  79. ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
  80. }
  81. static void ath79_spi_disable(struct ath79_spi *sp)
  82. {
  83. /* restore CTRL register */
  84. ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
  85. /* disable GPIO mode */
  86. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
  87. }
  88. static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned int nsecs,
  89. u32 word, u8 bits, unsigned flags)
  90. {
  91. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  92. u32 ioc = sp->ioc_base;
  93. /* clock starts at inactive polarity */
  94. for (word <<= (32 - bits); likely(bits); bits--) {
  95. u32 out;
  96. if (word & (1 << 31))
  97. out = ioc | AR71XX_SPI_IOC_DO;
  98. else
  99. out = ioc & ~AR71XX_SPI_IOC_DO;
  100. /* setup MSB (to slave) on trailing edge */
  101. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
  102. ath79_spi_delay(sp, nsecs);
  103. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
  104. ath79_spi_delay(sp, nsecs);
  105. if (bits == 1)
  106. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
  107. word <<= 1;
  108. }
  109. return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
  110. }
  111. static int ath79_exec_mem_op(struct spi_mem *mem,
  112. const struct spi_mem_op *op)
  113. {
  114. struct ath79_spi *sp = ath79_spidev_to_sp(mem->spi);
  115. /* Ensures that reading is performed on device connected to hardware cs0 */
  116. if (mem->spi->chip_select || mem->spi->cs_gpiod)
  117. return -ENOTSUPP;
  118. /* Only use for fast-read op. */
  119. if (op->cmd.opcode != 0x0b || op->data.dir != SPI_MEM_DATA_IN ||
  120. op->addr.nbytes != 3 || op->dummy.nbytes != 1)
  121. return -ENOTSUPP;
  122. /* disable GPIO mode */
  123. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
  124. memcpy_fromio(op->data.buf.in, sp->base + op->addr.val, op->data.nbytes);
  125. /* enable GPIO mode */
  126. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
  127. /* restore IOC register */
  128. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  129. return 0;
  130. }
  131. static const struct spi_controller_mem_ops ath79_mem_ops = {
  132. .exec_op = ath79_exec_mem_op,
  133. };
  134. static int ath79_spi_probe(struct platform_device *pdev)
  135. {
  136. struct spi_master *master;
  137. struct ath79_spi *sp;
  138. unsigned long rate;
  139. int ret;
  140. master = spi_alloc_master(&pdev->dev, sizeof(*sp));
  141. if (master == NULL) {
  142. dev_err(&pdev->dev, "failed to allocate spi master\n");
  143. return -ENOMEM;
  144. }
  145. sp = spi_master_get_devdata(master);
  146. master->dev.of_node = pdev->dev.of_node;
  147. platform_set_drvdata(pdev, sp);
  148. master->use_gpio_descriptors = true;
  149. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  150. master->flags = SPI_MASTER_GPIO_SS;
  151. master->num_chipselect = 3;
  152. master->mem_ops = &ath79_mem_ops;
  153. sp->bitbang.master = master;
  154. sp->bitbang.chipselect = ath79_spi_chipselect;
  155. sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
  156. sp->bitbang.flags = SPI_CS_HIGH;
  157. sp->base = devm_platform_ioremap_resource(pdev, 0);
  158. if (IS_ERR(sp->base)) {
  159. ret = PTR_ERR(sp->base);
  160. goto err_put_master;
  161. }
  162. sp->clk = devm_clk_get(&pdev->dev, "ahb");
  163. if (IS_ERR(sp->clk)) {
  164. ret = PTR_ERR(sp->clk);
  165. goto err_put_master;
  166. }
  167. ret = clk_prepare_enable(sp->clk);
  168. if (ret)
  169. goto err_put_master;
  170. rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
  171. if (!rate) {
  172. ret = -EINVAL;
  173. goto err_clk_disable;
  174. }
  175. sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
  176. dev_dbg(&pdev->dev, "register read/write delay is %u nsecs\n",
  177. sp->rrw_delay);
  178. ath79_spi_enable(sp);
  179. ret = spi_bitbang_start(&sp->bitbang);
  180. if (ret)
  181. goto err_disable;
  182. return 0;
  183. err_disable:
  184. ath79_spi_disable(sp);
  185. err_clk_disable:
  186. clk_disable_unprepare(sp->clk);
  187. err_put_master:
  188. spi_master_put(sp->bitbang.master);
  189. return ret;
  190. }
  191. static int ath79_spi_remove(struct platform_device *pdev)
  192. {
  193. struct ath79_spi *sp = platform_get_drvdata(pdev);
  194. spi_bitbang_stop(&sp->bitbang);
  195. ath79_spi_disable(sp);
  196. clk_disable_unprepare(sp->clk);
  197. spi_master_put(sp->bitbang.master);
  198. return 0;
  199. }
  200. static void ath79_spi_shutdown(struct platform_device *pdev)
  201. {
  202. ath79_spi_remove(pdev);
  203. }
  204. static const struct of_device_id ath79_spi_of_match[] = {
  205. { .compatible = "qca,ar7100-spi", },
  206. { },
  207. };
  208. MODULE_DEVICE_TABLE(of, ath79_spi_of_match);
  209. static struct platform_driver ath79_spi_driver = {
  210. .probe = ath79_spi_probe,
  211. .remove = ath79_spi_remove,
  212. .shutdown = ath79_spi_shutdown,
  213. .driver = {
  214. .name = DRV_NAME,
  215. .of_match_table = ath79_spi_of_match,
  216. },
  217. };
  218. module_platform_driver(ath79_spi_driver);
  219. MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
  220. MODULE_AUTHOR("Gabor Juhos <[email protected]>");
  221. MODULE_LICENSE("GPL v2");
  222. MODULE_ALIAS("platform:" DRV_NAME);