spi-altera.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/spi/spi_bitbang.h>
  22. #include <linux/io.h>
  23. #include <linux/of.h>
  24. #define DRV_NAME "spi_altera"
  25. #define ALTERA_SPI_RXDATA 0
  26. #define ALTERA_SPI_TXDATA 4
  27. #define ALTERA_SPI_STATUS 8
  28. #define ALTERA_SPI_CONTROL 12
  29. #define ALTERA_SPI_SLAVE_SEL 20
  30. #define ALTERA_SPI_STATUS_ROE_MSK 0x8
  31. #define ALTERA_SPI_STATUS_TOE_MSK 0x10
  32. #define ALTERA_SPI_STATUS_TMT_MSK 0x20
  33. #define ALTERA_SPI_STATUS_TRDY_MSK 0x40
  34. #define ALTERA_SPI_STATUS_RRDY_MSK 0x80
  35. #define ALTERA_SPI_STATUS_E_MSK 0x100
  36. #define ALTERA_SPI_CONTROL_IROE_MSK 0x8
  37. #define ALTERA_SPI_CONTROL_ITOE_MSK 0x10
  38. #define ALTERA_SPI_CONTROL_ITRDY_MSK 0x40
  39. #define ALTERA_SPI_CONTROL_IRRDY_MSK 0x80
  40. #define ALTERA_SPI_CONTROL_IE_MSK 0x100
  41. #define ALTERA_SPI_CONTROL_SSO_MSK 0x400
  42. struct altera_spi {
  43. /* bitbang has to be first */
  44. struct spi_bitbang bitbang;
  45. struct completion done;
  46. void __iomem *base;
  47. int irq;
  48. int len;
  49. int count;
  50. int bytes_per_word;
  51. unsigned long imr;
  52. /* data buffers */
  53. const unsigned char *tx;
  54. unsigned char *rx;
  55. };
  56. static inline struct altera_spi *altera_spi_to_hw(struct spi_device *sdev)
  57. {
  58. return spi_master_get_devdata(sdev->master);
  59. }
  60. static void altera_spi_chipsel(struct spi_device *spi, int value)
  61. {
  62. struct altera_spi *hw = altera_spi_to_hw(spi);
  63. if (spi->mode & SPI_CS_HIGH) {
  64. switch (value) {
  65. case BITBANG_CS_INACTIVE:
  66. writel(1 << spi->chip_select,
  67. hw->base + ALTERA_SPI_SLAVE_SEL);
  68. hw->imr |= ALTERA_SPI_CONTROL_SSO_MSK;
  69. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  70. break;
  71. case BITBANG_CS_ACTIVE:
  72. hw->imr &= ~ALTERA_SPI_CONTROL_SSO_MSK;
  73. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  74. writel(0, hw->base + ALTERA_SPI_SLAVE_SEL);
  75. break;
  76. }
  77. } else {
  78. switch (value) {
  79. case BITBANG_CS_INACTIVE:
  80. hw->imr &= ~ALTERA_SPI_CONTROL_SSO_MSK;
  81. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  82. break;
  83. case BITBANG_CS_ACTIVE:
  84. writel(1 << spi->chip_select,
  85. hw->base + ALTERA_SPI_SLAVE_SEL);
  86. hw->imr |= ALTERA_SPI_CONTROL_SSO_MSK;
  87. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  88. break;
  89. }
  90. }
  91. }
  92. static inline unsigned int hw_txbyte(struct altera_spi *hw, int count)
  93. {
  94. if (hw->tx) {
  95. switch (hw->bytes_per_word) {
  96. case 1:
  97. return hw->tx[count];
  98. case 2:
  99. return (hw->tx[count * 2]
  100. | (hw->tx[count * 2 + 1] << 8));
  101. }
  102. }
  103. return 0;
  104. }
  105. static int altera_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
  106. {
  107. struct altera_spi *hw = altera_spi_to_hw(spi);
  108. hw->tx = t->tx_buf;
  109. hw->rx = t->rx_buf;
  110. hw->count = 0;
  111. hw->bytes_per_word = DIV_ROUND_UP(t->bits_per_word, 8);
  112. hw->len = t->len / hw->bytes_per_word;
  113. if (hw->irq >= 0) {
  114. /* enable receive interrupt */
  115. hw->imr |= ALTERA_SPI_CONTROL_IRRDY_MSK;
  116. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  117. /* send the first byte */
  118. writel(hw_txbyte(hw, 0), hw->base + ALTERA_SPI_TXDATA);
  119. wait_for_completion(&hw->done);
  120. /* disable receive interrupt */
  121. hw->imr &= ~ALTERA_SPI_CONTROL_IRRDY_MSK;
  122. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  123. } else {
  124. while (hw->count < hw->len) {
  125. unsigned int rxd;
  126. writel(hw_txbyte(hw, hw->count),
  127. hw->base + ALTERA_SPI_TXDATA);
  128. while (!(readl(hw->base + ALTERA_SPI_STATUS) &
  129. ALTERA_SPI_STATUS_RRDY_MSK))
  130. cpu_relax();
  131. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  132. if (hw->rx) {
  133. switch (hw->bytes_per_word) {
  134. case 1:
  135. hw->rx[hw->count] = rxd;
  136. break;
  137. case 2:
  138. hw->rx[hw->count * 2] = rxd;
  139. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  140. break;
  141. }
  142. }
  143. hw->count++;
  144. }
  145. }
  146. return hw->count * hw->bytes_per_word;
  147. }
  148. static irqreturn_t altera_spi_irq(int irq, void *dev)
  149. {
  150. struct altera_spi *hw = dev;
  151. unsigned int rxd;
  152. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  153. if (hw->rx) {
  154. switch (hw->bytes_per_word) {
  155. case 1:
  156. hw->rx[hw->count] = rxd;
  157. break;
  158. case 2:
  159. hw->rx[hw->count * 2] = rxd;
  160. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  161. break;
  162. }
  163. }
  164. hw->count++;
  165. if (hw->count < hw->len)
  166. writel(hw_txbyte(hw, hw->count), hw->base + ALTERA_SPI_TXDATA);
  167. else
  168. complete(&hw->done);
  169. return IRQ_HANDLED;
  170. }
  171. static int altera_spi_probe(struct platform_device *pdev)
  172. {
  173. struct altera_spi_platform_data *platp = dev_get_platdata(&pdev->dev);
  174. struct altera_spi *hw;
  175. struct spi_master *master;
  176. struct resource *res;
  177. int err = -ENODEV;
  178. master = spi_alloc_master(&pdev->dev, sizeof(struct altera_spi));
  179. if (!master)
  180. return err;
  181. /* setup the master state. */
  182. master->bus_num = pdev->id;
  183. master->num_chipselect = 16;
  184. master->mode_bits = SPI_CS_HIGH;
  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 = spi_master_get(master);
  189. if (!hw->bitbang.master)
  190. return err;
  191. hw->bitbang.chipselect = altera_spi_chipsel;
  192. hw->bitbang.txrx_bufs = altera_spi_txrx;
  193. /* find and map our resources */
  194. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  195. hw->base = devm_ioremap_resource(&pdev->dev, res);
  196. if (IS_ERR(hw->base)) {
  197. err = PTR_ERR(hw->base);
  198. goto exit;
  199. }
  200. /* program defaults into the registers */
  201. hw->imr = 0; /* disable spi interrupts */
  202. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  203. writel(0, hw->base + ALTERA_SPI_STATUS); /* clear status reg */
  204. if (readl(hw->base + ALTERA_SPI_STATUS) & ALTERA_SPI_STATUS_RRDY_MSK)
  205. readl(hw->base + ALTERA_SPI_RXDATA); /* flush rxdata */
  206. /* irq is optional */
  207. hw->irq = platform_get_irq(pdev, 0);
  208. if (hw->irq >= 0) {
  209. init_completion(&hw->done);
  210. err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
  211. pdev->name, hw);
  212. if (err)
  213. goto exit;
  214. }
  215. /* find platform data */
  216. if (!platp)
  217. hw->bitbang.master->dev.of_node = pdev->dev.of_node;
  218. /* register our spi controller */
  219. err = spi_bitbang_start(&hw->bitbang);
  220. if (err)
  221. goto exit;
  222. dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
  223. return 0;
  224. exit:
  225. spi_master_put(master);
  226. return err;
  227. }
  228. static int altera_spi_remove(struct platform_device *dev)
  229. {
  230. struct altera_spi *hw = platform_get_drvdata(dev);
  231. struct spi_master *master = hw->bitbang.master;
  232. spi_bitbang_stop(&hw->bitbang);
  233. spi_master_put(master);
  234. return 0;
  235. }
  236. #ifdef CONFIG_OF
  237. static const struct of_device_id altera_spi_match[] = {
  238. { .compatible = "ALTR,spi-1.0", },
  239. { .compatible = "altr,spi-1.0", },
  240. {},
  241. };
  242. MODULE_DEVICE_TABLE(of, altera_spi_match);
  243. #endif /* CONFIG_OF */
  244. static struct platform_driver altera_spi_driver = {
  245. .probe = altera_spi_probe,
  246. .remove = altera_spi_remove,
  247. .driver = {
  248. .name = DRV_NAME,
  249. .owner = THIS_MODULE,
  250. .pm = NULL,
  251. .of_match_table = of_match_ptr(altera_spi_match),
  252. },
  253. };
  254. module_platform_driver(altera_spi_driver);
  255. MODULE_DESCRIPTION("Altera SPI driver");
  256. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  257. MODULE_LICENSE("GPL");
  258. MODULE_ALIAS("platform:" DRV_NAME);