spi-altera.c 7.4 KB

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