spi_oc_tiny.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * OpenCores tiny SPI master driver
  3. *
  4. * http://opencores.org/project,tiny_spi
  5. *
  6. * Copyright (C) 2011 Thomas Chou <thomas@wytron.com.tw>
  7. *
  8. * Based on spi_s3c24xx.c, which is:
  9. * Copyright (c) 2006 Ben Dooks
  10. * Copyright (c) 2006 Simtec Electronics
  11. * Ben Dooks <ben@simtec.co.uk>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/errno.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/spi/spi_bitbang.h>
  23. #include <linux/spi/spi_oc_tiny.h>
  24. #include <linux/io.h>
  25. #include <linux/gpio.h>
  26. #include <linux/of.h>
  27. #define DRV_NAME "spi_oc_tiny"
  28. #define TINY_SPI_RXDATA 0
  29. #define TINY_SPI_TXDATA 4
  30. #define TINY_SPI_STATUS 8
  31. #define TINY_SPI_CONTROL 12
  32. #define TINY_SPI_BAUD 16
  33. #define TINY_SPI_STATUS_TXE 0x1
  34. #define TINY_SPI_STATUS_TXR 0x2
  35. struct tiny_spi {
  36. /* bitbang has to be first */
  37. struct spi_bitbang bitbang;
  38. struct completion done;
  39. void __iomem *base;
  40. int irq;
  41. unsigned int freq;
  42. unsigned int baudwidth;
  43. unsigned int baud;
  44. unsigned int speed_hz;
  45. unsigned int mode;
  46. unsigned int len;
  47. unsigned int txc, rxc;
  48. const u8 *txp;
  49. u8 *rxp;
  50. unsigned int gpio_cs_count;
  51. int *gpio_cs;
  52. };
  53. static inline struct tiny_spi *tiny_spi_to_hw(struct spi_device *sdev)
  54. {
  55. return spi_master_get_devdata(sdev->master);
  56. }
  57. static unsigned int tiny_spi_baud(struct spi_device *spi, unsigned int hz)
  58. {
  59. struct tiny_spi *hw = tiny_spi_to_hw(spi);
  60. return min(DIV_ROUND_UP(hw->freq, hz * 2), (1U << hw->baudwidth)) - 1;
  61. }
  62. static void tiny_spi_chipselect(struct spi_device *spi, int is_active)
  63. {
  64. struct tiny_spi *hw = tiny_spi_to_hw(spi);
  65. if (hw->gpio_cs_count) {
  66. gpio_set_value(hw->gpio_cs[spi->chip_select],
  67. (spi->mode & SPI_CS_HIGH) ? is_active : !is_active);
  68. }
  69. }
  70. static int tiny_spi_setup_transfer(struct spi_device *spi,
  71. struct spi_transfer *t)
  72. {
  73. struct tiny_spi *hw = tiny_spi_to_hw(spi);
  74. unsigned int baud = hw->baud;
  75. if (t) {
  76. if (t->speed_hz && t->speed_hz != hw->speed_hz)
  77. baud = tiny_spi_baud(spi, t->speed_hz);
  78. }
  79. writel(baud, hw->base + TINY_SPI_BAUD);
  80. writel(hw->mode, hw->base + TINY_SPI_CONTROL);
  81. return 0;
  82. }
  83. static int tiny_spi_setup(struct spi_device *spi)
  84. {
  85. struct tiny_spi *hw = tiny_spi_to_hw(spi);
  86. if (spi->max_speed_hz != hw->speed_hz) {
  87. hw->speed_hz = spi->max_speed_hz;
  88. hw->baud = tiny_spi_baud(spi, hw->speed_hz);
  89. }
  90. hw->mode = spi->mode & (SPI_CPOL | SPI_CPHA);
  91. return 0;
  92. }
  93. static inline void tiny_spi_wait_txr(struct tiny_spi *hw)
  94. {
  95. while (!(readb(hw->base + TINY_SPI_STATUS) &
  96. TINY_SPI_STATUS_TXR))
  97. cpu_relax();
  98. }
  99. static inline void tiny_spi_wait_txe(struct tiny_spi *hw)
  100. {
  101. while (!(readb(hw->base + TINY_SPI_STATUS) &
  102. TINY_SPI_STATUS_TXE))
  103. cpu_relax();
  104. }
  105. static int tiny_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
  106. {
  107. struct tiny_spi *hw = tiny_spi_to_hw(spi);
  108. const u8 *txp = t->tx_buf;
  109. u8 *rxp = t->rx_buf;
  110. unsigned int i;
  111. if (hw->irq >= 0) {
  112. /* use intrrupt driven data transfer */
  113. hw->len = t->len;
  114. hw->txp = t->tx_buf;
  115. hw->rxp = t->rx_buf;
  116. hw->txc = 0;
  117. hw->rxc = 0;
  118. /* send the first byte */
  119. if (t->len > 1) {
  120. writeb(hw->txp ? *hw->txp++ : 0,
  121. hw->base + TINY_SPI_TXDATA);
  122. hw->txc++;
  123. writeb(hw->txp ? *hw->txp++ : 0,
  124. hw->base + TINY_SPI_TXDATA);
  125. hw->txc++;
  126. writeb(TINY_SPI_STATUS_TXR, hw->base + TINY_SPI_STATUS);
  127. } else {
  128. writeb(hw->txp ? *hw->txp++ : 0,
  129. hw->base + TINY_SPI_TXDATA);
  130. hw->txc++;
  131. writeb(TINY_SPI_STATUS_TXE, hw->base + TINY_SPI_STATUS);
  132. }
  133. wait_for_completion(&hw->done);
  134. } else if (txp && rxp) {
  135. /* we need to tighten the transfer loop */
  136. writeb(*txp++, hw->base + TINY_SPI_TXDATA);
  137. if (t->len > 1) {
  138. writeb(*txp++, hw->base + TINY_SPI_TXDATA);
  139. for (i = 2; i < t->len; i++) {
  140. u8 rx, tx = *txp++;
  141. tiny_spi_wait_txr(hw);
  142. rx = readb(hw->base + TINY_SPI_TXDATA);
  143. writeb(tx, hw->base + TINY_SPI_TXDATA);
  144. *rxp++ = rx;
  145. }
  146. tiny_spi_wait_txr(hw);
  147. *rxp++ = readb(hw->base + TINY_SPI_TXDATA);
  148. }
  149. tiny_spi_wait_txe(hw);
  150. *rxp++ = readb(hw->base + TINY_SPI_RXDATA);
  151. } else if (rxp) {
  152. writeb(0, hw->base + TINY_SPI_TXDATA);
  153. if (t->len > 1) {
  154. writeb(0,
  155. hw->base + TINY_SPI_TXDATA);
  156. for (i = 2; i < t->len; i++) {
  157. u8 rx;
  158. tiny_spi_wait_txr(hw);
  159. rx = readb(hw->base + TINY_SPI_TXDATA);
  160. writeb(0, hw->base + TINY_SPI_TXDATA);
  161. *rxp++ = rx;
  162. }
  163. tiny_spi_wait_txr(hw);
  164. *rxp++ = readb(hw->base + TINY_SPI_TXDATA);
  165. }
  166. tiny_spi_wait_txe(hw);
  167. *rxp++ = readb(hw->base + TINY_SPI_RXDATA);
  168. } else if (txp) {
  169. writeb(*txp++, hw->base + TINY_SPI_TXDATA);
  170. if (t->len > 1) {
  171. writeb(*txp++, hw->base + TINY_SPI_TXDATA);
  172. for (i = 2; i < t->len; i++) {
  173. u8 tx = *txp++;
  174. tiny_spi_wait_txr(hw);
  175. writeb(tx, hw->base + TINY_SPI_TXDATA);
  176. }
  177. }
  178. tiny_spi_wait_txe(hw);
  179. } else {
  180. writeb(0, hw->base + TINY_SPI_TXDATA);
  181. if (t->len > 1) {
  182. writeb(0, hw->base + TINY_SPI_TXDATA);
  183. for (i = 2; i < t->len; i++) {
  184. tiny_spi_wait_txr(hw);
  185. writeb(0, hw->base + TINY_SPI_TXDATA);
  186. }
  187. }
  188. tiny_spi_wait_txe(hw);
  189. }
  190. return t->len;
  191. }
  192. static irqreturn_t tiny_spi_irq(int irq, void *dev)
  193. {
  194. struct tiny_spi *hw = dev;
  195. writeb(0, hw->base + TINY_SPI_STATUS);
  196. if (hw->rxc + 1 == hw->len) {
  197. if (hw->rxp)
  198. *hw->rxp++ = readb(hw->base + TINY_SPI_RXDATA);
  199. hw->rxc++;
  200. complete(&hw->done);
  201. } else {
  202. if (hw->rxp)
  203. *hw->rxp++ = readb(hw->base + TINY_SPI_TXDATA);
  204. hw->rxc++;
  205. if (hw->txc < hw->len) {
  206. writeb(hw->txp ? *hw->txp++ : 0,
  207. hw->base + TINY_SPI_TXDATA);
  208. hw->txc++;
  209. writeb(TINY_SPI_STATUS_TXR,
  210. hw->base + TINY_SPI_STATUS);
  211. } else {
  212. writeb(TINY_SPI_STATUS_TXE,
  213. hw->base + TINY_SPI_STATUS);
  214. }
  215. }
  216. return IRQ_HANDLED;
  217. }
  218. #ifdef CONFIG_OF
  219. #include <linux/of_gpio.h>
  220. static int __devinit tiny_spi_of_probe(struct platform_device *pdev)
  221. {
  222. struct tiny_spi *hw = platform_get_drvdata(pdev);
  223. struct device_node *np = pdev->dev.of_node;
  224. unsigned int i;
  225. const __be32 *val;
  226. int len;
  227. if (!np)
  228. return 0;
  229. hw->gpio_cs_count = of_gpio_count(np);
  230. if (hw->gpio_cs_count) {
  231. hw->gpio_cs = devm_kzalloc(&pdev->dev,
  232. hw->gpio_cs_count * sizeof(unsigned int),
  233. GFP_KERNEL);
  234. if (!hw->gpio_cs)
  235. return -ENOMEM;
  236. }
  237. for (i = 0; i < hw->gpio_cs_count; i++) {
  238. hw->gpio_cs[i] = of_get_gpio_flags(np, i, NULL);
  239. if (hw->gpio_cs[i] < 0)
  240. return -ENODEV;
  241. }
  242. hw->bitbang.master->dev.of_node = pdev->dev.of_node;
  243. val = of_get_property(pdev->dev.of_node,
  244. "clock-frequency", &len);
  245. if (val && len >= sizeof(__be32))
  246. hw->freq = be32_to_cpup(val);
  247. val = of_get_property(pdev->dev.of_node, "baud-width", &len);
  248. if (val && len >= sizeof(__be32))
  249. hw->baudwidth = be32_to_cpup(val);
  250. return 0;
  251. }
  252. #else /* !CONFIG_OF */
  253. static int __devinit tiny_spi_of_probe(struct platform_device *pdev)
  254. {
  255. return 0;
  256. }
  257. #endif /* CONFIG_OF */
  258. static int __devinit tiny_spi_probe(struct platform_device *pdev)
  259. {
  260. struct tiny_spi_platform_data *platp = pdev->dev.platform_data;
  261. struct tiny_spi *hw;
  262. struct spi_master *master;
  263. struct resource *res;
  264. unsigned int i;
  265. int err = -ENODEV;
  266. master = spi_alloc_master(&pdev->dev, sizeof(struct tiny_spi));
  267. if (!master)
  268. return err;
  269. /* setup the master state. */
  270. master->bus_num = pdev->id;
  271. master->num_chipselect = 255;
  272. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  273. master->setup = tiny_spi_setup;
  274. hw = spi_master_get_devdata(master);
  275. platform_set_drvdata(pdev, hw);
  276. /* setup the state for the bitbang driver */
  277. hw->bitbang.master = spi_master_get(master);
  278. if (!hw->bitbang.master)
  279. return err;
  280. hw->bitbang.setup_transfer = tiny_spi_setup_transfer;
  281. hw->bitbang.chipselect = tiny_spi_chipselect;
  282. hw->bitbang.txrx_bufs = tiny_spi_txrx_bufs;
  283. /* find and map our resources */
  284. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  285. if (!res)
  286. goto exit_busy;
  287. if (!devm_request_mem_region(&pdev->dev, res->start, resource_size(res),
  288. pdev->name))
  289. goto exit_busy;
  290. hw->base = devm_ioremap_nocache(&pdev->dev, res->start,
  291. resource_size(res));
  292. if (!hw->base)
  293. goto exit_busy;
  294. /* irq is optional */
  295. hw->irq = platform_get_irq(pdev, 0);
  296. if (hw->irq >= 0) {
  297. init_completion(&hw->done);
  298. err = devm_request_irq(&pdev->dev, hw->irq, tiny_spi_irq, 0,
  299. pdev->name, hw);
  300. if (err)
  301. goto exit;
  302. }
  303. /* find platform data */
  304. if (platp) {
  305. hw->gpio_cs_count = platp->gpio_cs_count;
  306. hw->gpio_cs = platp->gpio_cs;
  307. if (platp->gpio_cs_count && !platp->gpio_cs)
  308. goto exit_busy;
  309. hw->freq = platp->freq;
  310. hw->baudwidth = platp->baudwidth;
  311. } else {
  312. err = tiny_spi_of_probe(pdev);
  313. if (err)
  314. goto exit;
  315. }
  316. for (i = 0; i < hw->gpio_cs_count; i++) {
  317. err = gpio_request(hw->gpio_cs[i], dev_name(&pdev->dev));
  318. if (err)
  319. goto exit_gpio;
  320. gpio_direction_output(hw->gpio_cs[i], 1);
  321. }
  322. hw->bitbang.master->num_chipselect = max(1U, hw->gpio_cs_count);
  323. /* register our spi controller */
  324. err = spi_bitbang_start(&hw->bitbang);
  325. if (err)
  326. goto exit;
  327. dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
  328. return 0;
  329. exit_gpio:
  330. while (i-- > 0)
  331. gpio_free(hw->gpio_cs[i]);
  332. exit_busy:
  333. err = -EBUSY;
  334. exit:
  335. platform_set_drvdata(pdev, NULL);
  336. spi_master_put(master);
  337. return err;
  338. }
  339. static int __devexit tiny_spi_remove(struct platform_device *pdev)
  340. {
  341. struct tiny_spi *hw = platform_get_drvdata(pdev);
  342. struct spi_master *master = hw->bitbang.master;
  343. unsigned int i;
  344. spi_bitbang_stop(&hw->bitbang);
  345. for (i = 0; i < hw->gpio_cs_count; i++)
  346. gpio_free(hw->gpio_cs[i]);
  347. platform_set_drvdata(pdev, NULL);
  348. spi_master_put(master);
  349. return 0;
  350. }
  351. #ifdef CONFIG_OF
  352. static const struct of_device_id tiny_spi_match[] = {
  353. { .compatible = "opencores,tiny-spi-rtlsvn2", },
  354. {},
  355. };
  356. MODULE_DEVICE_TABLE(of, tiny_spi_match);
  357. #else /* CONFIG_OF */
  358. #define tiny_spi_match NULL
  359. #endif /* CONFIG_OF */
  360. static struct platform_driver tiny_spi_driver = {
  361. .probe = tiny_spi_probe,
  362. .remove = __devexit_p(tiny_spi_remove),
  363. .driver = {
  364. .name = DRV_NAME,
  365. .owner = THIS_MODULE,
  366. .pm = NULL,
  367. .of_match_table = tiny_spi_match,
  368. },
  369. };
  370. static int __init tiny_spi_init(void)
  371. {
  372. return platform_driver_register(&tiny_spi_driver);
  373. }
  374. module_init(tiny_spi_init);
  375. static void __exit tiny_spi_exit(void)
  376. {
  377. platform_driver_unregister(&tiny_spi_driver);
  378. }
  379. module_exit(tiny_spi_exit);
  380. MODULE_DESCRIPTION("OpenCores tiny SPI driver");
  381. MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
  382. MODULE_LICENSE("GPL");
  383. MODULE_ALIAS("platform:" DRV_NAME);