spi-rb4xx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /*
  2. * SPI controller driver for the Mikrotik RB4xx boards
  3. *
  4. * Copyright (C) 2010 Gabor Juhos <juhosg@openwrt.org>
  5. *
  6. * This file was based on the patches for Linux 2.6.27.39 published by
  7. * MikroTik for their RouterBoard 4xx series devices.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/delay.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/spi/spi.h>
  24. #include <asm/mach-ath79/ar71xx_regs.h>
  25. #include <asm/mach-ath79/ath79.h>
  26. #define DRV_NAME "rb4xx-spi"
  27. #define DRV_DESC "Mikrotik RB4xx SPI controller driver"
  28. #define DRV_VERSION "0.1.0"
  29. #define SPI_CTRL_FASTEST 0x40
  30. #define SPI_FLASH_HZ 33333334
  31. #define SPI_CPLD_HZ 33333334
  32. #define CPLD_CMD_READ_FAST 0x0b
  33. #undef RB4XX_SPI_DEBUG
  34. struct rb4xx_spi {
  35. void __iomem *base;
  36. struct spi_master *master;
  37. unsigned spi_ctrl_flash;
  38. unsigned spi_ctrl_fread;
  39. struct clk *ahb_clk;
  40. unsigned long ahb_freq;
  41. spinlock_t lock;
  42. struct list_head queue;
  43. int busy:1;
  44. int cs_wait;
  45. };
  46. static unsigned spi_clk_low = AR71XX_SPI_IOC_CS1;
  47. #ifdef RB4XX_SPI_DEBUG
  48. static inline void do_spi_delay(void)
  49. {
  50. ndelay(20000);
  51. }
  52. #else
  53. static inline void do_spi_delay(void) { }
  54. #endif
  55. static inline void do_spi_init(struct spi_device *spi)
  56. {
  57. unsigned cs = AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1;
  58. if (!(spi->mode & SPI_CS_HIGH))
  59. cs ^= (spi->chip_select == 2) ? AR71XX_SPI_IOC_CS1 :
  60. AR71XX_SPI_IOC_CS0;
  61. spi_clk_low = cs;
  62. }
  63. static inline void do_spi_finish(void __iomem *base)
  64. {
  65. do_spi_delay();
  66. __raw_writel(AR71XX_SPI_IOC_CS0 | AR71XX_SPI_IOC_CS1,
  67. base + AR71XX_SPI_REG_IOC);
  68. }
  69. static inline void do_spi_clk(void __iomem *base, int bit)
  70. {
  71. unsigned bval = spi_clk_low | ((bit & 1) ? AR71XX_SPI_IOC_DO : 0);
  72. do_spi_delay();
  73. __raw_writel(bval, base + AR71XX_SPI_REG_IOC);
  74. do_spi_delay();
  75. __raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
  76. }
  77. static void do_spi_byte(void __iomem *base, unsigned char byte)
  78. {
  79. do_spi_clk(base, byte >> 7);
  80. do_spi_clk(base, byte >> 6);
  81. do_spi_clk(base, byte >> 5);
  82. do_spi_clk(base, byte >> 4);
  83. do_spi_clk(base, byte >> 3);
  84. do_spi_clk(base, byte >> 2);
  85. do_spi_clk(base, byte >> 1);
  86. do_spi_clk(base, byte);
  87. pr_debug("spi_byte sent 0x%02x got 0x%02x\n",
  88. (unsigned)byte,
  89. (unsigned char)__raw_readl(base + AR71XX_SPI_REG_RDS));
  90. }
  91. static inline void do_spi_clk_fast(void __iomem *base, unsigned bit1,
  92. unsigned bit2)
  93. {
  94. unsigned bval = (spi_clk_low |
  95. ((bit1 & 1) ? AR71XX_SPI_IOC_DO : 0) |
  96. ((bit2 & 1) ? AR71XX_SPI_IOC_CS2 : 0));
  97. do_spi_delay();
  98. __raw_writel(bval, base + AR71XX_SPI_REG_IOC);
  99. do_spi_delay();
  100. __raw_writel(bval | AR71XX_SPI_IOC_CLK, base + AR71XX_SPI_REG_IOC);
  101. }
  102. static void do_spi_byte_fast(void __iomem *base, unsigned char byte)
  103. {
  104. do_spi_clk_fast(base, byte >> 7, byte >> 6);
  105. do_spi_clk_fast(base, byte >> 5, byte >> 4);
  106. do_spi_clk_fast(base, byte >> 3, byte >> 2);
  107. do_spi_clk_fast(base, byte >> 1, byte >> 0);
  108. pr_debug("spi_byte_fast sent 0x%02x got 0x%02x\n",
  109. (unsigned)byte,
  110. (unsigned char) __raw_readl(base + AR71XX_SPI_REG_RDS));
  111. }
  112. static int rb4xx_spi_txrx(void __iomem *base, struct spi_transfer *t)
  113. {
  114. const unsigned char *rxv_ptr = NULL;
  115. const unsigned char *tx_ptr = t->tx_buf;
  116. unsigned char *rx_ptr = t->rx_buf;
  117. unsigned i;
  118. pr_debug("spi_txrx len %u tx %u rx %u\n",
  119. t->len,
  120. (t->tx_buf ? 1 : 0),
  121. (t->rx_buf ? 1 : 0));
  122. if (t->verify) {
  123. rxv_ptr = tx_ptr;
  124. tx_ptr = NULL;
  125. }
  126. for (i = 0; i < t->len; ++i) {
  127. unsigned char sdata = tx_ptr ? tx_ptr[i] : 0;
  128. if (t->fast_write)
  129. do_spi_byte_fast(base, sdata);
  130. else
  131. do_spi_byte(base, sdata);
  132. if (rx_ptr) {
  133. rx_ptr[i] = __raw_readl(base + AR71XX_SPI_REG_RDS) & 0xff;
  134. } else if (rxv_ptr) {
  135. unsigned char c = __raw_readl(base + AR71XX_SPI_REG_RDS);
  136. if (rxv_ptr[i] != c)
  137. return i;
  138. }
  139. }
  140. return i;
  141. }
  142. static int rb4xx_spi_read_fast(struct rb4xx_spi *rbspi,
  143. struct spi_message *m)
  144. {
  145. struct spi_transfer *t;
  146. const unsigned char *tx_ptr;
  147. unsigned addr;
  148. void __iomem *base = rbspi->base;
  149. /* check for exactly two transfers */
  150. if (list_empty(&m->transfers) ||
  151. list_is_last(m->transfers.next, &m->transfers) ||
  152. !list_is_last(m->transfers.next->next, &m->transfers)) {
  153. return -1;
  154. }
  155. /* first transfer contains command and address */
  156. t = list_entry(m->transfers.next,
  157. struct spi_transfer, transfer_list);
  158. if (t->len != 5 || t->tx_buf == NULL)
  159. return -1;
  160. tx_ptr = t->tx_buf;
  161. if (tx_ptr[0] != CPLD_CMD_READ_FAST)
  162. return -1;
  163. addr = tx_ptr[1];
  164. addr = tx_ptr[2] | (addr << 8);
  165. addr = tx_ptr[3] | (addr << 8);
  166. addr += (unsigned) base;
  167. m->actual_length += t->len;
  168. /* second transfer contains data itself */
  169. t = list_entry(m->transfers.next->next,
  170. struct spi_transfer, transfer_list);
  171. if (t->tx_buf && !t->verify)
  172. return -1;
  173. __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
  174. __raw_writel(rbspi->spi_ctrl_fread, base + AR71XX_SPI_REG_CTRL);
  175. __raw_writel(0, base + AR71XX_SPI_REG_FS);
  176. if (t->rx_buf) {
  177. memcpy(t->rx_buf, (const void *)addr, t->len);
  178. } else if (t->tx_buf) {
  179. unsigned char buf[t->len];
  180. memcpy(buf, (const void *)addr, t->len);
  181. if (memcmp(t->tx_buf, buf, t->len) != 0)
  182. m->status = -EMSGSIZE;
  183. }
  184. m->actual_length += t->len;
  185. if (rbspi->spi_ctrl_flash != rbspi->spi_ctrl_fread) {
  186. __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
  187. __raw_writel(rbspi->spi_ctrl_flash, base + AR71XX_SPI_REG_CTRL);
  188. __raw_writel(0, base + AR71XX_SPI_REG_FS);
  189. }
  190. return 0;
  191. }
  192. static int rb4xx_spi_msg(struct rb4xx_spi *rbspi, struct spi_message *m)
  193. {
  194. struct spi_transfer *t = NULL;
  195. void __iomem *base = rbspi->base;
  196. m->status = 0;
  197. if (list_empty(&m->transfers))
  198. return -1;
  199. if (m->fast_read)
  200. if (rb4xx_spi_read_fast(rbspi, m) == 0)
  201. return -1;
  202. __raw_writel(AR71XX_SPI_FS_GPIO, base + AR71XX_SPI_REG_FS);
  203. __raw_writel(SPI_CTRL_FASTEST, base + AR71XX_SPI_REG_CTRL);
  204. do_spi_init(m->spi);
  205. list_for_each_entry(t, &m->transfers, transfer_list) {
  206. int len;
  207. len = rb4xx_spi_txrx(base, t);
  208. if (len != t->len) {
  209. m->status = -EMSGSIZE;
  210. break;
  211. }
  212. m->actual_length += len;
  213. if (t->cs_change) {
  214. if (list_is_last(&t->transfer_list, &m->transfers)) {
  215. /* wait for continuation */
  216. return m->spi->chip_select;
  217. }
  218. do_spi_finish(base);
  219. ndelay(100);
  220. }
  221. }
  222. do_spi_finish(base);
  223. __raw_writel(rbspi->spi_ctrl_flash, base + AR71XX_SPI_REG_CTRL);
  224. __raw_writel(0, base + AR71XX_SPI_REG_FS);
  225. return -1;
  226. }
  227. static void rb4xx_spi_process_queue_locked(struct rb4xx_spi *rbspi,
  228. unsigned long *flags)
  229. {
  230. int cs = rbspi->cs_wait;
  231. rbspi->busy = 1;
  232. while (!list_empty(&rbspi->queue)) {
  233. struct spi_message *m;
  234. list_for_each_entry(m, &rbspi->queue, queue)
  235. if (cs < 0 || cs == m->spi->chip_select)
  236. break;
  237. if (&m->queue == &rbspi->queue)
  238. break;
  239. list_del_init(&m->queue);
  240. spin_unlock_irqrestore(&rbspi->lock, *flags);
  241. cs = rb4xx_spi_msg(rbspi, m);
  242. m->complete(m->context);
  243. spin_lock_irqsave(&rbspi->lock, *flags);
  244. }
  245. rbspi->cs_wait = cs;
  246. rbspi->busy = 0;
  247. if (cs >= 0) {
  248. /* TODO: add timer to unlock cs after 1s inactivity */
  249. }
  250. }
  251. static int rb4xx_spi_transfer(struct spi_device *spi,
  252. struct spi_message *m)
  253. {
  254. struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
  255. unsigned long flags;
  256. m->actual_length = 0;
  257. m->status = -EINPROGRESS;
  258. spin_lock_irqsave(&rbspi->lock, flags);
  259. list_add_tail(&m->queue, &rbspi->queue);
  260. if (rbspi->busy ||
  261. (rbspi->cs_wait >= 0 && rbspi->cs_wait != m->spi->chip_select)) {
  262. /* job will be done later */
  263. spin_unlock_irqrestore(&rbspi->lock, flags);
  264. return 0;
  265. }
  266. /* process job in current context */
  267. rb4xx_spi_process_queue_locked(rbspi, &flags);
  268. spin_unlock_irqrestore(&rbspi->lock, flags);
  269. return 0;
  270. }
  271. static int rb4xx_spi_setup(struct spi_device *spi)
  272. {
  273. struct rb4xx_spi *rbspi = spi_master_get_devdata(spi->master);
  274. unsigned long flags;
  275. if (spi->mode & ~(SPI_CS_HIGH)) {
  276. dev_err(&spi->dev, "mode %x not supported\n",
  277. (unsigned) spi->mode);
  278. return -EINVAL;
  279. }
  280. if (spi->bits_per_word != 8 && spi->bits_per_word != 0) {
  281. dev_err(&spi->dev, "bits_per_word %u not supported\n",
  282. (unsigned) spi->bits_per_word);
  283. return -EINVAL;
  284. }
  285. spin_lock_irqsave(&rbspi->lock, flags);
  286. if (rbspi->cs_wait == spi->chip_select && !rbspi->busy) {
  287. rbspi->cs_wait = -1;
  288. rb4xx_spi_process_queue_locked(rbspi, &flags);
  289. }
  290. spin_unlock_irqrestore(&rbspi->lock, flags);
  291. return 0;
  292. }
  293. static unsigned get_spi_ctrl(struct rb4xx_spi *rbspi, unsigned hz_max,
  294. const char *name)
  295. {
  296. unsigned div;
  297. div = (rbspi->ahb_freq - 1) / (2 * hz_max);
  298. /*
  299. * CPU has a bug at (div == 0) - first bit read is random
  300. */
  301. if (div == 0)
  302. ++div;
  303. if (name) {
  304. unsigned ahb_khz = (rbspi->ahb_freq + 500) / 1000;
  305. unsigned div_real = 2 * (div + 1);
  306. pr_debug("rb4xx: %s SPI clock %u kHz (AHB %u kHz / %u)\n",
  307. name,
  308. ahb_khz / div_real,
  309. ahb_khz, div_real);
  310. }
  311. return SPI_CTRL_FASTEST + div;
  312. }
  313. static int rb4xx_spi_probe(struct platform_device *pdev)
  314. {
  315. struct spi_master *master;
  316. struct rb4xx_spi *rbspi;
  317. struct resource *r;
  318. int err = 0;
  319. master = spi_alloc_master(&pdev->dev, sizeof(*rbspi));
  320. if (master == NULL) {
  321. dev_err(&pdev->dev, "no memory for spi_master\n");
  322. err = -ENOMEM;
  323. goto err_out;
  324. }
  325. master->bus_num = 0;
  326. master->num_chipselect = 3;
  327. master->setup = rb4xx_spi_setup;
  328. master->transfer = rb4xx_spi_transfer;
  329. rbspi = spi_master_get_devdata(master);
  330. rbspi->ahb_clk = clk_get(&pdev->dev, "ahb");
  331. if (IS_ERR(rbspi->ahb_clk)) {
  332. err = PTR_ERR(rbspi->ahb_clk);
  333. goto err_put_master;
  334. }
  335. err = clk_enable(rbspi->ahb_clk);
  336. if (err)
  337. goto err_clk_put;
  338. rbspi->ahb_freq = clk_get_rate(rbspi->ahb_clk);
  339. if (!rbspi->ahb_freq) {
  340. err = -EINVAL;
  341. goto err_clk_disable;
  342. }
  343. platform_set_drvdata(pdev, rbspi);
  344. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  345. if (r == NULL) {
  346. err = -ENOENT;
  347. goto err_clk_disable;
  348. }
  349. rbspi->base = ioremap(r->start, r->end - r->start + 1);
  350. if (!rbspi->base) {
  351. err = -ENXIO;
  352. goto err_clk_disable;
  353. }
  354. rbspi->master = master;
  355. rbspi->spi_ctrl_flash = get_spi_ctrl(rbspi, SPI_FLASH_HZ, "FLASH");
  356. rbspi->spi_ctrl_fread = get_spi_ctrl(rbspi, SPI_CPLD_HZ, "CPLD");
  357. rbspi->cs_wait = -1;
  358. spin_lock_init(&rbspi->lock);
  359. INIT_LIST_HEAD(&rbspi->queue);
  360. err = spi_register_master(master);
  361. if (err) {
  362. dev_err(&pdev->dev, "failed to register SPI master\n");
  363. goto err_iounmap;
  364. }
  365. return 0;
  366. err_iounmap:
  367. iounmap(rbspi->base);
  368. err_clk_disable:
  369. clk_disable(rbspi->ahb_clk);
  370. err_clk_put:
  371. clk_put(rbspi->ahb_clk);
  372. err_put_master:
  373. platform_set_drvdata(pdev, NULL);
  374. spi_master_put(master);
  375. err_out:
  376. return err;
  377. }
  378. static int rb4xx_spi_remove(struct platform_device *pdev)
  379. {
  380. struct rb4xx_spi *rbspi = platform_get_drvdata(pdev);
  381. iounmap(rbspi->base);
  382. clk_disable(rbspi->ahb_clk);
  383. clk_put(rbspi->ahb_clk);
  384. platform_set_drvdata(pdev, NULL);
  385. spi_master_put(rbspi->master);
  386. return 0;
  387. }
  388. static struct platform_driver rb4xx_spi_drv = {
  389. .probe = rb4xx_spi_probe,
  390. .remove = rb4xx_spi_remove,
  391. .driver = {
  392. .name = DRV_NAME,
  393. .owner = THIS_MODULE,
  394. },
  395. };
  396. static int __init rb4xx_spi_init(void)
  397. {
  398. return platform_driver_register(&rb4xx_spi_drv);
  399. }
  400. subsys_initcall(rb4xx_spi_init);
  401. static void __exit rb4xx_spi_exit(void)
  402. {
  403. platform_driver_unregister(&rb4xx_spi_drv);
  404. }
  405. module_exit(rb4xx_spi_exit);
  406. MODULE_DESCRIPTION(DRV_DESC);
  407. MODULE_VERSION(DRV_VERSION);
  408. MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  409. MODULE_LICENSE("GPL v2");