spi-oc-tiny.c 10 KB

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