spi-ath79.c 7.3 KB

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