spi-altera.c 7.9 KB

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