spi-altera.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 = 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. /* send the first byte */
  125. writel(hw_txbyte(hw, 0), hw->base + ALTERA_SPI_TXDATA);
  126. while (1) {
  127. unsigned int rxd;
  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. if (hw->count < hw->len)
  145. writel(hw_txbyte(hw, hw->count),
  146. hw->base + ALTERA_SPI_TXDATA);
  147. else
  148. break;
  149. }
  150. }
  151. return hw->count * hw->bytes_per_word;
  152. }
  153. static irqreturn_t altera_spi_irq(int irq, void *dev)
  154. {
  155. struct altera_spi *hw = dev;
  156. unsigned int rxd;
  157. rxd = readl(hw->base + ALTERA_SPI_RXDATA);
  158. if (hw->rx) {
  159. switch (hw->bytes_per_word) {
  160. case 1:
  161. hw->rx[hw->count] = rxd;
  162. break;
  163. case 2:
  164. hw->rx[hw->count * 2] = rxd;
  165. hw->rx[hw->count * 2 + 1] = rxd >> 8;
  166. break;
  167. }
  168. }
  169. hw->count++;
  170. if (hw->count < hw->len)
  171. writel(hw_txbyte(hw, hw->count), hw->base + ALTERA_SPI_TXDATA);
  172. else
  173. complete(&hw->done);
  174. return IRQ_HANDLED;
  175. }
  176. static int altera_spi_probe(struct platform_device *pdev)
  177. {
  178. struct altera_spi_platform_data *platp = pdev->dev.platform_data;
  179. struct altera_spi *hw;
  180. struct spi_master *master;
  181. struct resource *res;
  182. int err = -ENODEV;
  183. master = spi_alloc_master(&pdev->dev, sizeof(struct altera_spi));
  184. if (!master)
  185. return err;
  186. /* setup the master state. */
  187. master->bus_num = pdev->id;
  188. master->num_chipselect = 16;
  189. master->mode_bits = SPI_CS_HIGH;
  190. hw = spi_master_get_devdata(master);
  191. platform_set_drvdata(pdev, hw);
  192. /* setup the state for the bitbang driver */
  193. hw->bitbang.master = spi_master_get(master);
  194. if (!hw->bitbang.master)
  195. return err;
  196. hw->bitbang.chipselect = altera_spi_chipsel;
  197. hw->bitbang.txrx_bufs = altera_spi_txrx;
  198. /* find and map our resources */
  199. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  200. if (!res)
  201. goto exit_busy;
  202. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  203. pdev->name))
  204. goto exit_busy;
  205. hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
  206. resource_size(res));
  207. if (!hw->base)
  208. goto exit_busy;
  209. /* program defaults into the registers */
  210. hw->imr = 0; /* disable spi interrupts */
  211. writel(hw->imr, hw->base + ALTERA_SPI_CONTROL);
  212. writel(0, hw->base + ALTERA_SPI_STATUS); /* clear status reg */
  213. if (readl(hw->base + ALTERA_SPI_STATUS) & ALTERA_SPI_STATUS_RRDY_MSK)
  214. readl(hw->base + ALTERA_SPI_RXDATA); /* flush rxdata */
  215. /* irq is optional */
  216. hw->irq = platform_get_irq(pdev, 0);
  217. if (hw->irq >= 0) {
  218. init_completion(&hw->done);
  219. err = devm_request_irq(&pdev->dev, hw->irq, altera_spi_irq, 0,
  220. pdev->name, hw);
  221. if (err)
  222. goto exit;
  223. }
  224. /* find platform data */
  225. if (!platp)
  226. hw->bitbang.master->dev.of_node = pdev->dev.of_node;
  227. /* register our spi controller */
  228. err = spi_bitbang_start(&hw->bitbang);
  229. if (err)
  230. goto exit;
  231. dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
  232. return 0;
  233. exit_busy:
  234. err = -EBUSY;
  235. exit:
  236. platform_set_drvdata(pdev, NULL);
  237. spi_master_put(master);
  238. return err;
  239. }
  240. static int altera_spi_remove(struct platform_device *dev)
  241. {
  242. struct altera_spi *hw = platform_get_drvdata(dev);
  243. struct spi_master *master = hw->bitbang.master;
  244. spi_bitbang_stop(&hw->bitbang);
  245. platform_set_drvdata(dev, NULL);
  246. spi_master_put(master);
  247. return 0;
  248. }
  249. #ifdef CONFIG_OF
  250. static const struct of_device_id altera_spi_match[] = {
  251. { .compatible = "ALTR,spi-1.0", },
  252. {},
  253. };
  254. MODULE_DEVICE_TABLE(of, altera_spi_match);
  255. #endif /* CONFIG_OF */
  256. static struct platform_driver altera_spi_driver = {
  257. .probe = altera_spi_probe,
  258. .remove = altera_spi_remove,
  259. .driver = {
  260. .name = DRV_NAME,
  261. .owner = THIS_MODULE,
  262. .pm = NULL,
  263. .of_match_table = of_match_ptr(altera_spi_match),
  264. },
  265. };
  266. module_platform_driver(altera_spi_driver);
  267. MODULE_DESCRIPTION("Altera SPI driver");
  268. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  269. MODULE_LICENSE("GPL");
  270. MODULE_ALIAS("platform:" DRV_NAME);