spi_altera.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. * Altera SPI driver
  3. *
  4. * Copyright (C) 2008 Thomas Chou <thomas@wytron.com.tw>
  5. *
  6. * Based on spi_s3c24xx.c, which is:
  7. * Copyright (c) 2006 Ben Dooks
  8. * Copyright (c) 2006 Simtec Electronics
  9. * Ben Dooks <ben@simtec.co.uk>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/errno.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/spi_bitbang.h>
  21. #include <linux/io.h>
  22. #include <linux/of.h>
  23. #define DRV_NAME "spi_altera"
  24. #define ALTERA_SPI_RXDATA 0
  25. #define ALTERA_SPI_TXDATA 4
  26. #define ALTERA_SPI_STATUS 8
  27. #define ALTERA_SPI_CONTROL 12
  28. #define ALTERA_SPI_SLAVE_SEL 20
  29. #define ALTERA_SPI_STATUS_ROE_MSK 0x8
  30. #define ALTERA_SPI_STATUS_TOE_MSK 0x10
  31. #define ALTERA_SPI_STATUS_TMT_MSK 0x20
  32. #define ALTERA_SPI_STATUS_TRDY_MSK 0x40
  33. #define ALTERA_SPI_STATUS_RRDY_MSK 0x80
  34. #define ALTERA_SPI_STATUS_E_MSK 0x100
  35. #define ALTERA_SPI_CONTROL_IROE_MSK 0x8
  36. #define ALTERA_SPI_CONTROL_ITOE_MSK 0x10
  37. #define ALTERA_SPI_CONTROL_ITRDY_MSK 0x40
  38. #define ALTERA_SPI_CONTROL_IRRDY_MSK 0x80
  39. #define ALTERA_SPI_CONTROL_IE_MSK 0x100
  40. #define ALTERA_SPI_CONTROL_SSO_MSK 0x400
  41. struct altera_spi {
  42. /* bitbang has to be first */
  43. struct spi_bitbang bitbang;
  44. struct completion done;
  45. void __iomem *base;
  46. int irq;
  47. int len;
  48. int count;
  49. int bytes_per_word;
  50. unsigned long imr;
  51. /* data buffers */
  52. const unsigned char *tx;
  53. unsigned char *rx;
  54. };
  55. static inline struct altera_spi *altera_spi_to_hw(struct spi_device *sdev)
  56. {
  57. return spi_master_get_devdata(sdev->master);
  58. }
  59. static void altera_spi_chipsel(struct spi_device *spi, int value)
  60. {
  61. struct altera_spi *hw = altera_spi_to_hw(spi);
  62. if (spi->mode & SPI_CS_HIGH) {
  63. switch (value) {
  64. case BITBANG_CS_INACTIVE:
  65. writel(1 << spi->chip_select,
  66. hw->base + ALTERA_SPI_SLAVE_SEL);
  67. hw->imr |= ALTERA_SPI_CONTROL_SSO_MSK;
  68. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  69. break;
  70. case BITBANG_CS_ACTIVE:
  71. hw->imr &= ~ALTERA_SPI_CONTROL_SSO_MSK;
  72. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  73. writel(0, hw->base + ALTERA_SPI_SLAVE_SEL);
  74. break;
  75. }
  76. } else {
  77. switch (value) {
  78. case BITBANG_CS_INACTIVE:
  79. hw->imr &= ~ALTERA_SPI_CONTROL_SSO_MSK;
  80. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  81. break;
  82. case BITBANG_CS_ACTIVE:
  83. writel(1 << spi->chip_select,
  84. hw->base + ALTERA_SPI_SLAVE_SEL);
  85. hw->imr |= ALTERA_SPI_CONTROL_SSO_MSK;
  86. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  87. break;
  88. }
  89. }
  90. }
  91. static int altera_spi_setupxfer(struct spi_device *spi, struct spi_transfer *t)
  92. {
  93. return 0;
  94. }
  95. static int altera_spi_setup(struct spi_device *spi)
  96. {
  97. return 0;
  98. }
  99. static inline unsigned int hw_txbyte(struct altera_spi *hw, int count)
  100. {
  101. if (hw->tx) {
  102. switch (hw->bytes_per_word) {
  103. case 1:
  104. return hw->tx[count];
  105. case 2:
  106. return (hw->tx[count * 2]
  107. | (hw->tx[count * 2 + 1] << 8));
  108. }
  109. }
  110. return 0;
  111. }
  112. static int altera_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  113. {
  114. struct altera_spi *hw = altera_spi_to_hw(spi);
  115. hw->tx = t->tx_buf;
  116. hw->rx = t->rx_buf;
  117. hw->count = 0;
  118. hw->bytes_per_word = (t->bits_per_word ? : spi->bits_per_word) / 8;
  119. hw->len = t->len / hw->bytes_per_word;
  120. if (hw->irq >= 0) {
  121. /* enable receive interrupt */
  122. hw->imr |= ALTERA_SPI_CONTROL_IRRDY_MSK;
  123. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  124. /* send the first byte */
  125. writel(hw_txbyte(hw, 0), hw->base + ALTERA_SPI_TXDATA);
  126. wait_for_completion(&hw->done);
  127. /* disable receive interrupt */
  128. hw->imr &= ~ALTERA_SPI_CONTROL_IRRDY_MSK;
  129. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  130. } else {
  131. /* send the first byte */
  132. writel(hw_txbyte(hw, 0), hw->base + ALTERA_SPI_TXDATA);
  133. while (1) {
  134. unsigned int rxd;
  135. while (!(readl(hw->base + ALTERA_SPI_STATUS) &
  136. ALTERA_SPI_STATUS_RRDY_MSK))
  137. cpu_relax();
  138. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  139. if (hw->rx) {
  140. switch (hw->bytes_per_word) {
  141. case 1:
  142. hw->rx[hw->count] = rxd;
  143. break;
  144. case 2:
  145. hw->rx[hw->count * 2] = rxd;
  146. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  147. break;
  148. }
  149. }
  150. hw->count++;
  151. if (hw->count < hw->len)
  152. writel(hw_txbyte(hw, hw->count),
  153. hw->base + ALTERA_SPI_TXDATA);
  154. else
  155. break;
  156. }
  157. }
  158. return hw->count * hw->bytes_per_word;
  159. }
  160. static irqreturn_t altera_spi_irq(int irq, void *dev)
  161. {
  162. struct altera_spi *hw = dev;
  163. unsigned int rxd;
  164. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  165. if (hw->rx) {
  166. switch (hw->bytes_per_word) {
  167. case 1:
  168. hw->rx[hw->count] = rxd;
  169. break;
  170. case 2:
  171. hw->rx[hw->count * 2] = rxd;
  172. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  173. break;
  174. }
  175. }
  176. hw->count++;
  177. if (hw->count < hw->len)
  178. writel(hw_txbyte(hw, hw->count), hw->base + ALTERA_SPI_TXDATA);
  179. else
  180. complete(&hw->done);
  181. return IRQ_HANDLED;
  182. }
  183. static int __devinit altera_spi_probe(struct platform_device *pdev)
  184. {
  185. struct altera_spi_platform_data *platp = pdev->dev.platform_data;
  186. struct altera_spi *hw;
  187. struct spi_master *master;
  188. struct resource *res;
  189. int err = -ENODEV;
  190. master = spi_alloc_master(&pdev->dev, sizeof(struct altera_spi));
  191. if (!master)
  192. return err;
  193. /* setup the master state. */
  194. master->bus_num = pdev->id;
  195. master->num_chipselect = 16;
  196. master->mode_bits = SPI_CS_HIGH;
  197. master->setup = altera_spi_setup;
  198. hw = spi_master_get_devdata(master);
  199. platform_set_drvdata(pdev, hw);
  200. /* setup the state for the bitbang driver */
  201. hw->bitbang.master = spi_master_get(master);
  202. if (!hw->bitbang.master)
  203. return err;
  204. hw->bitbang.setup_transfer = altera_spi_setupxfer;
  205. hw->bitbang.chipselect = altera_spi_chipsel;
  206. hw->bitbang.txrx_bufs = altera_spi_txrx;
  207. /* find and map our resources */
  208. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  209. if (!res)
  210. goto exit_busy;
  211. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  212. pdev->name))
  213. goto exit_busy;
  214. hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
  215. resource_size(res));
  216. if (!hw->base)
  217. goto exit_busy;
  218. /* program defaults into the registers */
  219. hw->imr = 0; /* disable spi interrupts */
  220. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  221. writel(0, hw->base + ALTERA_SPI_STATUS); /* clear status reg */
  222. if (readl(hw->base + ALTERA_SPI_STATUS) & ALTERA_SPI_STATUS_RRDY_MSK)
  223. readl(hw->base + ALTERA_SPI_RXDATA); /* flush rxdata */
  224. /* irq is optional */
  225. hw->irq = platform_get_irq(pdev, 0);
  226. if (hw->irq >= 0) {
  227. init_completion(&hw->done);
  228. err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
  229. pdev->name, hw);
  230. if (err)
  231. goto exit;
  232. }
  233. /* find platform data */
  234. if (!platp)
  235. hw->bitbang.master->dev.of_node = pdev->dev.of_node;
  236. /* register our spi controller */
  237. err = spi_bitbang_start(&hw->bitbang);
  238. if (err)
  239. goto exit;
  240. dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
  241. return 0;
  242. exit_busy:
  243. err = -EBUSY;
  244. exit:
  245. platform_set_drvdata(pdev, NULL);
  246. spi_master_put(master);
  247. return err;
  248. }
  249. static int __devexit altera_spi_remove(struct platform_device *dev)
  250. {
  251. struct altera_spi *hw = platform_get_drvdata(dev);
  252. struct spi_master *master = hw->bitbang.master;
  253. spi_bitbang_stop(&hw->bitbang);
  254. platform_set_drvdata(dev, NULL);
  255. spi_master_put(master);
  256. return 0;
  257. }
  258. #ifdef CONFIG_OF
  259. static const struct of_device_id altera_spi_match[] = {
  260. { .compatible = "ALTR,spi-1.0", },
  261. {},
  262. };
  263. MODULE_DEVICE_TABLE(of, altera_spi_match);
  264. #else /* CONFIG_OF */
  265. #define altera_spi_match NULL
  266. #endif /* CONFIG_OF */
  267. static struct platform_driver altera_spi_driver = {
  268. .probe = altera_spi_probe,
  269. .remove = __devexit_p(altera_spi_remove),
  270. .driver = {
  271. .name = DRV_NAME,
  272. .owner = THIS_MODULE,
  273. .pm = NULL,
  274. .of_match_table = altera_spi_match,
  275. },
  276. };
  277. static int __init altera_spi_init(void)
  278. {
  279. return platform_driver_register(&altera_spi_driver);
  280. }
  281. module_init(altera_spi_init);
  282. static void __exit altera_spi_exit(void)
  283. {
  284. platform_driver_unregister(&altera_spi_driver);
  285. }
  286. module_exit(altera_spi_exit);
  287. MODULE_DESCRIPTION("Altera SPI driver");
  288. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  289. MODULE_LICENSE("GPL");
  290. MODULE_ALIAS("platform:" DRV_NAME);