spi-ath79.c 7.2 KB

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