spi-altera.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/interrupt.h>
  16. #include <linux/errno.h>
  17. #include <linux/module.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 inline unsigned int hw_txbyte(struct altera_spi *hw, int count)
  92. {
  93. if (hw->tx) {
  94. switch (hw->bytes_per_word) {
  95. case 1:
  96. return hw->tx[count];
  97. case 2:
  98. return (hw->tx[count * 2]
  99. | (hw->tx[count * 2 + 1] << 8));
  100. }
  101. }
  102. return 0;
  103. }
  104. static int altera_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  105. {
  106. struct altera_spi *hw = altera_spi_to_hw(spi);
  107. hw->tx = t->tx_buf;
  108. hw->rx = t->rx_buf;
  109. hw->count = 0;
  110. hw->bytes_per_word = DIV_ROUND_UP(t->bits_per_word, 8);
  111. hw->len = t->len / hw->bytes_per_word;
  112. if (hw->irq >= 0) {
  113. /* enable receive interrupt */
  114. hw->imr |= ALTERA_SPI_CONTROL_IRRDY_MSK;
  115. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  116. /* send the first byte */
  117. writel(hw_txbyte(hw, 0), hw->base + ALTERA_SPI_TXDATA);
  118. wait_for_completion(&hw->done);
  119. /* disable receive interrupt */
  120. hw->imr &= ~ALTERA_SPI_CONTROL_IRRDY_MSK;
  121. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  122. } else {
  123. while (hw->count < hw->len) {
  124. unsigned int rxd;
  125. writel(hw_txbyte(hw, hw->count),
  126. hw->base + ALTERA_SPI_TXDATA);
  127. while (!(readl(hw->base + ALTERA_SPI_STATUS) &
  128. ALTERA_SPI_STATUS_RRDY_MSK))
  129. cpu_relax();
  130. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  131. if (hw->rx) {
  132. switch (hw->bytes_per_word) {
  133. case 1:
  134. hw->rx[hw->count] = rxd;
  135. break;
  136. case 2:
  137. hw->rx[hw->count * 2] = rxd;
  138. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  139. break;
  140. }
  141. }
  142. hw->count++;
  143. }
  144. }
  145. return hw->count * hw->bytes_per_word;
  146. }
  147. static irqreturn_t altera_spi_irq(int irq, void *dev)
  148. {
  149. struct altera_spi *hw = dev;
  150. unsigned int rxd;
  151. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  152. if (hw->rx) {
  153. switch (hw->bytes_per_word) {
  154. case 1:
  155. hw->rx[hw->count] = rxd;
  156. break;
  157. case 2:
  158. hw->rx[hw->count * 2] = rxd;
  159. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  160. break;
  161. }
  162. }
  163. hw->count++;
  164. if (hw->count < hw->len)
  165. writel(hw_txbyte(hw, hw->count), hw->base + ALTERA_SPI_TXDATA);
  166. else
  167. complete(&hw->done);
  168. return IRQ_HANDLED;
  169. }
  170. static int altera_spi_probe(struct platform_device *pdev)
  171. {
  172. struct altera_spi *hw;
  173. struct spi_master *master;
  174. struct resource *res;
  175. int err = -ENODEV;
  176. master = spi_alloc_master(&pdev->dev, sizeof(struct altera_spi));
  177. if (!master)
  178. return err;
  179. /* setup the master state. */
  180. master->bus_num = pdev->id;
  181. master->num_chipselect = 16;
  182. master->mode_bits = SPI_CS_HIGH;
  183. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
  184. master->dev.of_node = pdev->dev.of_node;
  185. hw = spi_master_get_devdata(master);
  186. platform_set_drvdata(pdev, hw);
  187. /* setup the state for the bitbang driver */
  188. hw->bitbang.master = master;
  189. hw->bitbang.chipselect = altera_spi_chipsel;
  190. hw->bitbang.txrx_bufs = altera_spi_txrx;
  191. /* find and map our resources */
  192. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  193. hw->base = devm_ioremap_resource(&pdev->dev, res);
  194. if (IS_ERR(hw->base)) {
  195. err = PTR_ERR(hw->base);
  196. goto exit;
  197. }
  198. /* program defaults into the registers */
  199. hw->imr = 0; /* disable spi interrupts */
  200. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  201. writel(0, hw->base + ALTERA_SPI_STATUS); /* clear status reg */
  202. if (readl(hw->base + ALTERA_SPI_STATUS) & ALTERA_SPI_STATUS_RRDY_MSK)
  203. readl(hw->base + ALTERA_SPI_RXDATA); /* flush rxdata */
  204. /* irq is optional */
  205. hw->irq = platform_get_irq(pdev, 0);
  206. if (hw->irq >= 0) {
  207. init_completion(&hw->done);
  208. err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
  209. pdev->name, hw);
  210. if (err)
  211. goto exit;
  212. }
  213. /* register our spi controller */
  214. err = spi_bitbang_start(&hw->bitbang);
  215. if (err)
  216. goto exit;
  217. dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
  218. return 0;
  219. exit:
  220. spi_master_put(master);
  221. return err;
  222. }
  223. static int altera_spi_remove(struct platform_device *dev)
  224. {
  225. struct altera_spi *hw = platform_get_drvdata(dev);
  226. struct spi_master *master = hw->bitbang.master;
  227. spi_bitbang_stop(&hw->bitbang);
  228. spi_master_put(master);
  229. return 0;
  230. }
  231. #ifdef CONFIG_OF
  232. static const struct of_device_id altera_spi_match[] = {
  233. { .compatible = "ALTR,spi-1.0", },
  234. { .compatible = "altr,spi-1.0", },
  235. {},
  236. };
  237. MODULE_DEVICE_TABLE(of, altera_spi_match);
  238. #endif /* CONFIG_OF */
  239. static struct platform_driver altera_spi_driver = {
  240. .probe = altera_spi_probe,
  241. .remove = altera_spi_remove,
  242. .driver = {
  243. .name = DRV_NAME,
  244. .pm = NULL,
  245. .of_match_table = of_match_ptr(altera_spi_match),
  246. },
  247. };
  248. module_platform_driver(altera_spi_driver);
  249. MODULE_DESCRIPTION("Altera SPI driver");
  250. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  251. MODULE_LICENSE("GPL");
  252. MODULE_ALIAS("platform:" DRV_NAME);