ath79_spi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/init.h>
  16. #include <linux/delay.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/io.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/spi/spi_bitbang.h>
  23. #include <linux/bitops.h>
  24. #include <linux/gpio.h>
  25. #include <asm/mach-ath79/ar71xx_regs.h>
  26. #include <asm/mach-ath79/ath79_spi_platform.h>
  27. #define DRV_NAME "ath79-spi"
  28. struct ath79_spi {
  29. struct spi_bitbang bitbang;
  30. u32 ioc_base;
  31. u32 reg_ctrl;
  32. void __iomem *base;
  33. };
  34. static inline u32 ath79_spi_rr(struct ath79_spi *sp, unsigned reg)
  35. {
  36. return ioread32(sp->base + reg);
  37. }
  38. static inline void ath79_spi_wr(struct ath79_spi *sp, unsigned reg, u32 val)
  39. {
  40. iowrite32(val, sp->base + reg);
  41. }
  42. static inline struct ath79_spi *ath79_spidev_to_sp(struct spi_device *spi)
  43. {
  44. return spi_master_get_devdata(spi->master);
  45. }
  46. static void ath79_spi_chipselect(struct spi_device *spi, int is_active)
  47. {
  48. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  49. int cs_high = (spi->mode & SPI_CS_HIGH) ? is_active : !is_active;
  50. if (is_active) {
  51. /* set initial clock polarity */
  52. if (spi->mode & SPI_CPOL)
  53. sp->ioc_base |= AR71XX_SPI_IOC_CLK;
  54. else
  55. sp->ioc_base &= ~AR71XX_SPI_IOC_CLK;
  56. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  57. }
  58. if (spi->chip_select) {
  59. struct ath79_spi_controller_data *cdata = spi->controller_data;
  60. /* SPI is normally active-low */
  61. gpio_set_value(cdata->gpio, cs_high);
  62. } else {
  63. if (cs_high)
  64. sp->ioc_base |= AR71XX_SPI_IOC_CS0;
  65. else
  66. sp->ioc_base &= ~AR71XX_SPI_IOC_CS0;
  67. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  68. }
  69. }
  70. static int ath79_spi_setup_cs(struct spi_device *spi)
  71. {
  72. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  73. struct ath79_spi_controller_data *cdata;
  74. cdata = spi->controller_data;
  75. if (spi->chip_select && !cdata)
  76. return -EINVAL;
  77. /* enable GPIO mode */
  78. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, AR71XX_SPI_FS_GPIO);
  79. /* save CTRL register */
  80. sp->reg_ctrl = ath79_spi_rr(sp, AR71XX_SPI_REG_CTRL);
  81. sp->ioc_base = ath79_spi_rr(sp, AR71XX_SPI_REG_IOC);
  82. /* TODO: setup speed? */
  83. ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, 0x43);
  84. if (spi->chip_select) {
  85. int status = 0;
  86. status = gpio_request(cdata->gpio, dev_name(&spi->dev));
  87. if (status)
  88. return status;
  89. status = gpio_direction_output(cdata->gpio,
  90. spi->mode & SPI_CS_HIGH);
  91. if (status) {
  92. gpio_free(cdata->gpio);
  93. return status;
  94. }
  95. } else {
  96. if (spi->mode & SPI_CS_HIGH)
  97. sp->ioc_base |= AR71XX_SPI_IOC_CS0;
  98. else
  99. sp->ioc_base &= ~AR71XX_SPI_IOC_CS0;
  100. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, sp->ioc_base);
  101. }
  102. return 0;
  103. }
  104. static void ath79_spi_cleanup_cs(struct spi_device *spi)
  105. {
  106. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  107. if (spi->chip_select) {
  108. struct ath79_spi_controller_data *cdata = spi->controller_data;
  109. gpio_free(cdata->gpio);
  110. }
  111. /* restore CTRL register */
  112. ath79_spi_wr(sp, AR71XX_SPI_REG_CTRL, sp->reg_ctrl);
  113. /* disable GPIO mode */
  114. ath79_spi_wr(sp, AR71XX_SPI_REG_FS, 0);
  115. }
  116. static int ath79_spi_setup(struct spi_device *spi)
  117. {
  118. int status = 0;
  119. if (spi->bits_per_word > 32)
  120. return -EINVAL;
  121. if (!spi->controller_state) {
  122. status = ath79_spi_setup_cs(spi);
  123. if (status)
  124. return status;
  125. }
  126. status = spi_bitbang_setup(spi);
  127. if (status && !spi->controller_state)
  128. ath79_spi_cleanup_cs(spi);
  129. return status;
  130. }
  131. static void ath79_spi_cleanup(struct spi_device *spi)
  132. {
  133. ath79_spi_cleanup_cs(spi);
  134. spi_bitbang_cleanup(spi);
  135. }
  136. static u32 ath79_spi_txrx_mode0(struct spi_device *spi, unsigned nsecs,
  137. u32 word, u8 bits)
  138. {
  139. struct ath79_spi *sp = ath79_spidev_to_sp(spi);
  140. u32 ioc = sp->ioc_base;
  141. /* clock starts at inactive polarity */
  142. for (word <<= (32 - bits); likely(bits); bits--) {
  143. u32 out;
  144. if (word & (1 << 31))
  145. out = ioc | AR71XX_SPI_IOC_DO;
  146. else
  147. out = ioc & ~AR71XX_SPI_IOC_DO;
  148. /* setup MSB (to slave) on trailing edge */
  149. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out);
  150. ath79_spi_wr(sp, AR71XX_SPI_REG_IOC, out | AR71XX_SPI_IOC_CLK);
  151. word <<= 1;
  152. }
  153. return ath79_spi_rr(sp, AR71XX_SPI_REG_RDS);
  154. }
  155. static __devinit int ath79_spi_probe(struct platform_device *pdev)
  156. {
  157. struct spi_master *master;
  158. struct ath79_spi *sp;
  159. struct ath79_spi_platform_data *pdata;
  160. struct resource *r;
  161. int ret;
  162. master = spi_alloc_master(&pdev->dev, sizeof(*sp));
  163. if (master == NULL) {
  164. dev_err(&pdev->dev, "failed to allocate spi master\n");
  165. return -ENOMEM;
  166. }
  167. sp = spi_master_get_devdata(master);
  168. platform_set_drvdata(pdev, sp);
  169. pdata = pdev->dev.platform_data;
  170. master->setup = ath79_spi_setup;
  171. master->cleanup = ath79_spi_cleanup;
  172. if (pdata) {
  173. master->bus_num = pdata->bus_num;
  174. master->num_chipselect = pdata->num_chipselect;
  175. } else {
  176. master->bus_num = -1;
  177. master->num_chipselect = 1;
  178. }
  179. sp->bitbang.master = spi_master_get(master);
  180. sp->bitbang.chipselect = ath79_spi_chipselect;
  181. sp->bitbang.txrx_word[SPI_MODE_0] = ath79_spi_txrx_mode0;
  182. sp->bitbang.setup_transfer = spi_bitbang_setup_transfer;
  183. sp->bitbang.flags = SPI_CS_HIGH;
  184. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  185. if (r == NULL) {
  186. ret = -ENOENT;
  187. goto err_put_master;
  188. }
  189. sp->base = ioremap(r->start, r->end - r->start + 1);
  190. if (!sp->base) {
  191. ret = -ENXIO;
  192. goto err_put_master;
  193. }
  194. ret = spi_bitbang_start(&sp->bitbang);
  195. if (ret)
  196. goto err_unmap;
  197. return 0;
  198. err_unmap:
  199. iounmap(sp->base);
  200. err_put_master:
  201. platform_set_drvdata(pdev, NULL);
  202. spi_master_put(sp->bitbang.master);
  203. return ret;
  204. }
  205. static __devexit int ath79_spi_remove(struct platform_device *pdev)
  206. {
  207. struct ath79_spi *sp = platform_get_drvdata(pdev);
  208. spi_bitbang_stop(&sp->bitbang);
  209. iounmap(sp->base);
  210. platform_set_drvdata(pdev, NULL);
  211. spi_master_put(sp->bitbang.master);
  212. return 0;
  213. }
  214. static struct platform_driver ath79_spi_driver = {
  215. .probe = ath79_spi_probe,
  216. .remove = __devexit_p(ath79_spi_remove),
  217. .driver = {
  218. .name = DRV_NAME,
  219. .owner = THIS_MODULE,
  220. },
  221. };
  222. static __init int ath79_spi_init(void)
  223. {
  224. return platform_driver_register(&ath79_spi_driver);
  225. }
  226. module_init(ath79_spi_init);
  227. static __exit void ath79_spi_exit(void)
  228. {
  229. platform_driver_unregister(&ath79_spi_driver);
  230. }
  231. module_exit(ath79_spi_exit);
  232. MODULE_DESCRIPTION("SPI controller driver for Atheros AR71XX/AR724X/AR913X");
  233. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  234. MODULE_LICENSE("GPL v2");
  235. MODULE_ALIAS("platform:" DRV_NAME);