0006-spi-add-rb4xx-SPI-driver.patch 13 KB

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