spi-ath79.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * SPI controller driver for the Atheros AR71XX/AR724X/AR913X SoCs
  3. *
  4. * Copyright (C) 2009-2011 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This driver has been based on the spi-gpio.c:
  7. * Copyright (C) 2006,2008 David Brownell
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/delay.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/io.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spi_bitbang.h>
  22. #include <linux/bitops.h>
  23. #include <linux/gpio.h>
  24. #include <linux/clk.h>
  25. #include <linux/err.h>
  26. #include <asm/mach-ath79/ar71xx_regs.h>
  27. #include <asm/mach-ath79/ath79_spi_platform.h>
  28. #define DRV_NAME "ath79-spi"
  29. #define ATH79_SPI_RRW_DELAY_FACTOR 12000
  30. #define MHZ (1000 * 1000)
  31. struct ath79_spi {
  32. struct spi_bitbang bitbang;
  33. u32 ioc_base;
  34. u32 reg_ctrl;
  35. void __iomem *base;
  36. struct clk *clk;
  37. unsigned int rrw_delay;
  38. };
  39. static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned int reg)
  40. {
  41. return ioread32(sp->base + reg);
  42. }
  43. static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned int reg, u32 val)
  44. {
  45. iowrite32(val, sp->base + reg);
  46. }
  47. static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi)
  48. {
  49. return spi_master_get_devdata(spi->master);
  50. }
  51. static inline void ath79_spi_delay(struct ath79_spi *sp, unsigned int nsecs)
  52. {
  53. if (nsecs > sp->rrw_delay)
  54. ndelay(nsecs - sp->rrw_delay);
  55. }
  56. static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
  57. {
  58. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  59. int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
  60. if (is_active) {
  61. /* set initial clock polarity */
  62. if (spi->mode & SPI_CPOL)
  63. sp->ioc_base |= AR71XX_SPI_IOC_CLK;
  64. else
  65. sp->ioc_base &= ~AR71XX_SPI_IOC_CLK;
  66. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  67. }
  68. if (gpio_is_valid(spi->cs_gpio)) {
  69. /* SPI is normally active-low */
  70. gpio_set_value_cansleep(spi->cs_gpio, cs_high);
  71. } else {
  72. u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
  73. if (cs_high)
  74. sp->ioc_base |= cs_bit;
  75. else
  76. sp->ioc_base &= ~cs_bit;
  77. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  78. }
  79. }
  80. static void ath79_spi_enable(struct ath79_spi *sp)
  81. {
  82. /* enable GPIO mode */
  83. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
  84. /* save CTRL register */
  85. sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
  86. sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
  87. /* TODO: setup speed? */
  88. ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
  89. }
  90. static void ath79_spi_disable(struct ath79_spi *sp)
  91. {
  92. /* restore CTRL register */
  93. ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
  94. /* disable GPIO mode */
  95. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
  96. }
  97. static int ath79_spi_setup_cs(struct spi_device *spi)
  98. {
  99. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  100. int status;
  101. status = 0;
  102. if (gpio_is_valid(spi->cs_gpio)) {
  103. unsigned long flags;
  104. flags = GPIOF_DIR_OUT;
  105. if (spi->mode & SPI_CS_HIGH)
  106. flags |= GPIOF_INIT_LOW;
  107. else
  108. flags |= GPIOF_INIT_HIGH;
  109. status = gpio_request_one(spi->cs_gpio, flags,
  110. dev_name(&spi->dev));
  111. } else {
  112. u32 cs_bit = AR71XX_SPI_IOC_CS(spi->chip_select);
  113. if (spi->mode & SPI_CS_HIGH)
  114. sp->ioc_base &= ~cs_bit;
  115. else
  116. sp->ioc_base |= cs_bit;
  117. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  118. }
  119. return status;
  120. }
  121. static void ath79_spi_cleanup_cs(struct spi_device *spi)
  122. {
  123. if (gpio_is_valid(spi->cs_gpio))
  124. gpio_free(spi->cs_gpio);
  125. }
  126. static int ath79_spi_setup(struct spi_device *spi)
  127. {
  128. int status = 0;
  129. if (!spi->controller_state) {
  130. status = ath79_spi_setup_cs(spi);
  131. if (status)
  132. return status;
  133. }
  134. status = spi_bitbang_setup(spi);
  135. if (status && !spi->controller_state)
  136. ath79_spi_cleanup_cs(spi);
  137. return status;
  138. }
  139. static void ath79_spi_cleanup(struct spi_device *spi)
  140. {
  141. ath79_spi_cleanup_cs(spi);
  142. spi_bitbang_cleanup(spi);
  143. }
  144. static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned int nsecs,
  145. u32 word, u8 bits, unsigned flags)
  146. {
  147. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  148. u32 ioc = sp->ioc_base;
  149. /* clock starts at inactive polarity */
  150. for (word <<= (32 - bits); likely(bits); bits--) {
  151. u32 out;
  152. if (word & (1 << 31))
  153. out = ioc | AR71XX_SPI_IOC_DO;
  154. else
  155. out = ioc & ~AR71XX_SPI_IOC_DO;
  156. /* setup MSB (to slave) on trailing edge */
  157. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
  158. ath79_spi_delay(sp, nsecs);
  159. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
  160. ath79_spi_delay(sp, nsecs);
  161. if (bits == 1)
  162. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
  163. word <<= 1;
  164. }
  165. return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
  166. }
  167. static int ath79_spi_probe(struct platform_device *pdev)
  168. {
  169. struct spi_master *master;
  170. struct ath79_spi *sp;
  171. struct ath79_spi_platform_data *pdata;
  172. struct resource *r;
  173. unsigned long rate;
  174. int ret;
  175. master = spi_alloc_master(&pdev->dev, sizeof(*sp));
  176. if (master == NULL) {
  177. dev_err(&pdev->dev, "failed to allocate spi master\n");
  178. return -ENOMEM;
  179. }
  180. sp = spi_master_get_devdata(master);
  181. master->dev.of_node = pdev->dev.of_node;
  182. platform_set_drvdata(pdev, sp);
  183. pdata = dev_get_platdata(&pdev->dev);
  184. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
  185. master->setup = ath79_spi_setup;
  186. master->cleanup = ath79_spi_cleanup;
  187. if (pdata) {
  188. master->bus_num = pdata->bus_num;
  189. master->num_chipselect = pdata->num_chipselect;
  190. }
  191. sp->bitbang.master = master;
  192. sp->bitbang.chipselect = ath79_spi_chipselect;
  193. sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
  194. sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  195. sp->bitbang.flags = SPI_CS_HIGH;
  196. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  197. sp->base = devm_ioremap_resource(&pdev->dev, r);
  198. if (IS_ERR(sp->base)) {
  199. ret = PTR_ERR(sp->base);
  200. goto err_put_master;
  201. }
  202. sp->clk = devm_clk_get(&pdev->dev, "ahb");
  203. if (IS_ERR(sp->clk)) {
  204. ret = PTR_ERR(sp->clk);
  205. goto err_put_master;
  206. }
  207. ret = clk_prepare_enable(sp->clk);
  208. if (ret)
  209. goto err_put_master;
  210. rate = DIV_ROUND_UP(clk_get_rate(sp->clk), MHZ);
  211. if (!rate) {
  212. ret = -EINVAL;
  213. goto err_clk_disable;
  214. }
  215. sp->rrw_delay = ATH79_SPI_RRW_DELAY_FACTOR / rate;
  216. dev_dbg(&pdev->dev, "register read/write delay is %u nsecs\n",
  217. sp->rrw_delay);
  218. ath79_spi_enable(sp);
  219. ret = spi_bitbang_start(&sp->bitbang);
  220. if (ret)
  221. goto err_disable;
  222. return 0;
  223. err_disable:
  224. ath79_spi_disable(sp);
  225. err_clk_disable:
  226. clk_disable_unprepare(sp->clk);
  227. err_put_master:
  228. spi_master_put(sp->bitbang.master);
  229. return ret;
  230. }
  231. static int ath79_spi_remove(struct platform_device *pdev)
  232. {
  233. struct ath79_spi *sp = platform_get_drvdata(pdev);
  234. spi_bitbang_stop(&sp->bitbang);
  235. ath79_spi_disable(sp);
  236. clk_disable_unprepare(sp->clk);
  237. spi_master_put(sp->bitbang.master);
  238. return 0;
  239. }
  240. static void ath79_spi_shutdown(struct platform_device *pdev)
  241. {
  242. ath79_spi_remove(pdev);
  243. }
  244. static const struct of_device_id ath79_spi_of_match[] = {
  245. { .compatible = "qca,ar7100-spi", },
  246. { },
  247. };
  248. MODULE_DEVICE_TABLE(of, ath79_spi_of_match);
  249. static struct platform_driver ath79_spi_driver = {
  250. .probe = ath79_spi_probe,
  251. .remove = ath79_spi_remove,
  252. .shutdown = ath79_spi_shutdown,
  253. .driver = {
  254. .name = DRV_NAME,
  255. .of_match_table = ath79_spi_of_match,
  256. },
  257. };
  258. module_platform_driver(ath79_spi_driver);
  259. MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
  260. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  261. MODULE_LICENSE("GPL v2");
  262. MODULE_ALIAS("platform:" DRV_NAME);