0050-SPI-ralink-add-Ralink-SoC-spi-driver.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. From fc006d0622ab8c43086b2c9018c03012db332033 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Sun, 27 Jul 2014 11:15:12 +0100
  4. Subject: [PATCH 50/57] SPI: ralink: add Ralink SoC spi driver
  5. Add the driver needed to make SPI work on Ralink SoC.
  6. Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
  7. Acked-by: John Crispin <blogic@openwrt.org>
  8. ---
  9. drivers/spi/Kconfig | 6 +
  10. drivers/spi/Makefile | 1 +
  11. drivers/spi/spi-rt2880.c | 432 ++++++++++++++++++++++++++++++++++++++++++++++
  12. 3 files changed, 439 insertions(+)
  13. create mode 100644 drivers/spi/spi-rt2880.c
  14. --- a/drivers/spi/Kconfig
  15. +++ b/drivers/spi/Kconfig
  16. @@ -433,6 +433,12 @@ config SPI_QUP
  17. This driver can also be built as a module. If so, the module
  18. will be called spi_qup.
  19. +config SPI_RT2880
  20. + tristate "Ralink RT288x SPI Controller"
  21. + depends on RALINK
  22. + help
  23. + This selects a driver for the Ralink RT288x/RT305x SPI Controller.
  24. +
  25. config SPI_S3C24XX
  26. tristate "Samsung S3C24XX series SPI"
  27. depends on ARCH_S3C24XX
  28. --- a/drivers/spi/Makefile
  29. +++ b/drivers/spi/Makefile
  30. @@ -65,6 +65,7 @@ obj-$(CONFIG_SPI_PXA2XX_PCI) += spi-pxa
  31. obj-$(CONFIG_SPI_QUP) += spi-qup.o
  32. obj-$(CONFIG_SPI_ROCKCHIP) += spi-rockchip.o
  33. obj-$(CONFIG_SPI_RSPI) += spi-rspi.o
  34. +obj-$(CONFIG_SPI_RT2880) += spi-rt2880.o
  35. obj-$(CONFIG_SPI_S3C24XX) += spi-s3c24xx-hw.o
  36. spi-s3c24xx-hw-y := spi-s3c24xx.o
  37. spi-s3c24xx-hw-$(CONFIG_SPI_S3C24XX_FIQ) += spi-s3c24xx-fiq.o
  38. --- /dev/null
  39. +++ b/drivers/spi/spi-rt2880.c
  40. @@ -0,0 +1,432 @@
  41. +/*
  42. + * spi-rt2880.c -- Ralink RT288x/RT305x SPI controller driver
  43. + *
  44. + * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
  45. + * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
  46. + *
  47. + * Some parts are based on spi-orion.c:
  48. + * Author: Shadi Ammouri <shadi@marvell.com>
  49. + * Copyright (C) 2007-2008 Marvell Ltd.
  50. + *
  51. + * This program is free software; you can redistribute it and/or modify
  52. + * it under the terms of the GNU General Public License version 2 as
  53. + * published by the Free Software Foundation.
  54. + */
  55. +
  56. +#include <linux/init.h>
  57. +#include <linux/module.h>
  58. +#include <linux/clk.h>
  59. +#include <linux/err.h>
  60. +#include <linux/delay.h>
  61. +#include <linux/io.h>
  62. +#include <linux/reset.h>
  63. +#include <linux/spi/spi.h>
  64. +#include <linux/platform_device.h>
  65. +
  66. +#define DRIVER_NAME "spi-rt2880"
  67. +/* only one slave is supported*/
  68. +#define RALINK_NUM_CHIPSELECTS 1
  69. +/* in usec */
  70. +#define RALINK_SPI_WAIT_MAX_LOOP 2000
  71. +
  72. +#define RAMIPS_SPI_STAT 0x00
  73. +#define RAMIPS_SPI_CFG 0x10
  74. +#define RAMIPS_SPI_CTL 0x14
  75. +#define RAMIPS_SPI_DATA 0x20
  76. +#define RAMIPS_SPI_FIFO_STAT 0x38
  77. +
  78. +/* SPISTAT register bit field */
  79. +#define SPISTAT_BUSY BIT(0)
  80. +
  81. +/* SPICFG register bit field */
  82. +#define SPICFG_LSBFIRST 0
  83. +#define SPICFG_MSBFIRST BIT(8)
  84. +#define SPICFG_SPICLKPOL BIT(6)
  85. +#define SPICFG_RXCLKEDGE_FALLING BIT(5)
  86. +#define SPICFG_TXCLKEDGE_FALLING BIT(4)
  87. +#define SPICFG_SPICLK_PRESCALE_MASK 0x7
  88. +#define SPICFG_SPICLK_DIV2 0
  89. +#define SPICFG_SPICLK_DIV4 1
  90. +#define SPICFG_SPICLK_DIV8 2
  91. +#define SPICFG_SPICLK_DIV16 3
  92. +#define SPICFG_SPICLK_DIV32 4
  93. +#define SPICFG_SPICLK_DIV64 5
  94. +#define SPICFG_SPICLK_DIV128 6
  95. +#define SPICFG_SPICLK_DISABLE 7
  96. +
  97. +/* SPICTL register bit field */
  98. +#define SPICTL_HIZSDO BIT(3)
  99. +#define SPICTL_STARTWR BIT(2)
  100. +#define SPICTL_STARTRD BIT(1)
  101. +#define SPICTL_SPIENA BIT(0)
  102. +
  103. +/* SPIFIFOSTAT register bit field */
  104. +#define SPIFIFOSTAT_TXFULL BIT(17)
  105. +
  106. +struct rt2880_spi {
  107. + struct spi_master *master;
  108. + void __iomem *base;
  109. + unsigned int sys_freq;
  110. + unsigned int speed;
  111. + struct clk *clk;
  112. + spinlock_t lock;
  113. +};
  114. +
  115. +static inline struct rt2880_spi *spidev_to_rt2880_spi(struct spi_device *spi)
  116. +{
  117. + return spi_master_get_devdata(spi->master);
  118. +}
  119. +
  120. +static inline u32 rt2880_spi_read(struct rt2880_spi *rs, u32 reg)
  121. +{
  122. + return ioread32(rs->base + reg);
  123. +}
  124. +
  125. +static inline void rt2880_spi_write(struct rt2880_spi *rs, u32 reg, u32 val)
  126. +{
  127. + iowrite32(val, rs->base + reg);
  128. +}
  129. +
  130. +static inline void rt2880_spi_setbits(struct rt2880_spi *rs, u32 reg, u32 mask)
  131. +{
  132. + void __iomem *addr = rs->base + reg;
  133. + unsigned long flags;
  134. + u32 val;
  135. +
  136. + spin_lock_irqsave(&rs->lock, flags);
  137. + val = ioread32(addr);
  138. + val |= mask;
  139. + iowrite32(val, addr);
  140. + spin_unlock_irqrestore(&rs->lock, flags);
  141. +}
  142. +
  143. +static inline void rt2880_spi_clrbits(struct rt2880_spi *rs, u32 reg, u32 mask)
  144. +{
  145. + void __iomem *addr = rs->base + reg;
  146. + unsigned long flags;
  147. + u32 val;
  148. +
  149. + spin_lock_irqsave(&rs->lock, flags);
  150. + val = ioread32(addr);
  151. + val &= ~mask;
  152. + iowrite32(val, addr);
  153. + spin_unlock_irqrestore(&rs->lock, flags);
  154. +}
  155. +
  156. +static int rt2880_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  157. +{
  158. + struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  159. + u32 rate;
  160. + u32 prescale;
  161. + u32 reg;
  162. +
  163. + dev_dbg(&spi->dev, "speed:%u\n", speed);
  164. +
  165. + /*
  166. + * the supported rates are: 2, 4, 8, ... 128
  167. + * round up as we look for equal or less speed
  168. + */
  169. + rate = DIV_ROUND_UP(rs->sys_freq, speed);
  170. + dev_dbg(&spi->dev, "rate-1:%u\n", rate);
  171. + rate = roundup_pow_of_two(rate);
  172. + dev_dbg(&spi->dev, "rate-2:%u\n", rate);
  173. +
  174. + /* check if requested speed is too small */
  175. + if (rate > 128)
  176. + return -EINVAL;
  177. +
  178. + if (rate < 2)
  179. + rate = 2;
  180. +
  181. + /* Convert the rate to SPI clock divisor value. */
  182. + prescale = ilog2(rate / 2);
  183. + dev_dbg(&spi->dev, "prescale:%u\n", prescale);
  184. +
  185. + reg = rt2880_spi_read(rs, RAMIPS_SPI_CFG);
  186. + reg = ((reg & ~SPICFG_SPICLK_PRESCALE_MASK) | prescale);
  187. + rt2880_spi_write(rs, RAMIPS_SPI_CFG, reg);
  188. + rs->speed = speed;
  189. + return 0;
  190. +}
  191. +
  192. +/*
  193. + * called only when no transfer is active on the bus
  194. + */
  195. +static int
  196. +rt2880_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  197. +{
  198. + struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  199. + unsigned int speed = spi->max_speed_hz;
  200. + int rc;
  201. +
  202. + if ((t != NULL) && t->speed_hz)
  203. + speed = t->speed_hz;
  204. +
  205. + if (rs->speed != speed) {
  206. + dev_dbg(&spi->dev, "speed_hz:%u\n", speed);
  207. + rc = rt2880_spi_baudrate_set(spi, speed);
  208. + if (rc)
  209. + return rc;
  210. + }
  211. +
  212. + return 0;
  213. +}
  214. +
  215. +static void rt2880_spi_set_cs(struct rt2880_spi *rs, int enable)
  216. +{
  217. + if (enable)
  218. + rt2880_spi_clrbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
  219. + else
  220. + rt2880_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
  221. +}
  222. +
  223. +static inline int rt2880_spi_wait_till_ready(struct rt2880_spi *rs)
  224. +{
  225. + int i;
  226. +
  227. + for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
  228. + u32 status;
  229. +
  230. + status = rt2880_spi_read(rs, RAMIPS_SPI_STAT);
  231. + if ((status & SPISTAT_BUSY) == 0)
  232. + return 0;
  233. +
  234. + cpu_relax();
  235. + udelay(1);
  236. + }
  237. +
  238. + return -ETIMEDOUT;
  239. +}
  240. +
  241. +static unsigned int
  242. +rt2880_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  243. +{
  244. + struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  245. + unsigned count = 0;
  246. + u8 *rx = xfer->rx_buf;
  247. + const u8 *tx = xfer->tx_buf;
  248. + int err;
  249. +
  250. + dev_dbg(&spi->dev, "read (%d): %s %s\n", xfer->len,
  251. + (tx != NULL) ? "tx" : " ",
  252. + (rx != NULL) ? "rx" : " ");
  253. +
  254. + if (tx) {
  255. + for (count = 0; count < xfer->len; count++) {
  256. + rt2880_spi_write(rs, RAMIPS_SPI_DATA, tx[count]);
  257. + rt2880_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_STARTWR);
  258. + err = rt2880_spi_wait_till_ready(rs);
  259. + if (err) {
  260. + dev_err(&spi->dev, "TX failed, err=%d\n", err);
  261. + goto out;
  262. + }
  263. + }
  264. + }
  265. +
  266. + if (rx) {
  267. + for (count = 0; count < xfer->len; count++) {
  268. + rt2880_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_STARTRD);
  269. + err = rt2880_spi_wait_till_ready(rs);
  270. + if (err) {
  271. + dev_err(&spi->dev, "RX failed, err=%d\n", err);
  272. + goto out;
  273. + }
  274. + rx[count] = (u8) rt2880_spi_read(rs, RAMIPS_SPI_DATA);
  275. + }
  276. + }
  277. +
  278. +out:
  279. + return count;
  280. +}
  281. +
  282. +static int rt2880_spi_transfer_one_message(struct spi_master *master,
  283. + struct spi_message *m)
  284. +{
  285. + struct rt2880_spi *rs = spi_master_get_devdata(master);
  286. + struct spi_device *spi = m->spi;
  287. + struct spi_transfer *t = NULL;
  288. + int par_override = 0;
  289. + int status = 0;
  290. + int cs_active = 0;
  291. +
  292. + /* Load defaults */
  293. + status = rt2880_spi_setup_transfer(spi, NULL);
  294. + if (status < 0)
  295. + goto msg_done;
  296. +
  297. + list_for_each_entry(t, &m->transfers, transfer_list) {
  298. + if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
  299. + dev_err(&spi->dev,
  300. + "message rejected: invalid transfer data buffers\n");
  301. + status = -EIO;
  302. + goto msg_done;
  303. + }
  304. +
  305. + if (t->speed_hz && t->speed_hz < (rs->sys_freq / 128)) {
  306. + dev_err(&spi->dev,
  307. + "message rejected: device min speed (%d Hz) exceeds required transfer speed (%d Hz)\n",
  308. + (rs->sys_freq / 128), t->speed_hz);
  309. + status = -EIO;
  310. + goto msg_done;
  311. + }
  312. +
  313. + if (par_override || t->speed_hz || t->bits_per_word) {
  314. + par_override = 1;
  315. + status = rt2880_spi_setup_transfer(spi, t);
  316. + if (status < 0)
  317. + goto msg_done;
  318. + if (!t->speed_hz && !t->bits_per_word)
  319. + par_override = 0;
  320. + }
  321. +
  322. + if (!cs_active) {
  323. + rt2880_spi_set_cs(rs, 1);
  324. + cs_active = 1;
  325. + }
  326. +
  327. + if (t->len)
  328. + m->actual_length += rt2880_spi_write_read(spi, t);
  329. +
  330. + if (t->delay_usecs)
  331. + udelay(t->delay_usecs);
  332. +
  333. + if (t->cs_change) {
  334. + rt2880_spi_set_cs(rs, 0);
  335. + cs_active = 0;
  336. + }
  337. + }
  338. +
  339. +msg_done:
  340. + if (cs_active)
  341. + rt2880_spi_set_cs(rs, 0);
  342. +
  343. + m->status = status;
  344. + spi_finalize_current_message(master);
  345. +
  346. + return 0;
  347. +}
  348. +
  349. +static int rt2880_spi_setup(struct spi_device *spi)
  350. +{
  351. + struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  352. +
  353. + if ((spi->max_speed_hz == 0) ||
  354. + (spi->max_speed_hz > (rs->sys_freq / 2)))
  355. + spi->max_speed_hz = (rs->sys_freq / 2);
  356. +
  357. + if (spi->max_speed_hz < (rs->sys_freq / 128)) {
  358. + dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
  359. + spi->max_speed_hz);
  360. + return -EINVAL;
  361. + }
  362. +
  363. + /*
  364. + * baudrate & width will be set rt2880_spi_setup_transfer
  365. + */
  366. + return 0;
  367. +}
  368. +
  369. +static void rt2880_spi_reset(struct rt2880_spi *rs)
  370. +{
  371. + rt2880_spi_write(rs, RAMIPS_SPI_CFG,
  372. + SPICFG_MSBFIRST | SPICFG_TXCLKEDGE_FALLING |
  373. + SPICFG_SPICLK_DIV16 | SPICFG_SPICLKPOL);
  374. + rt2880_spi_write(rs, RAMIPS_SPI_CTL, SPICTL_HIZSDO | SPICTL_SPIENA);
  375. +}
  376. +
  377. +static int rt2880_spi_probe(struct platform_device *pdev)
  378. +{
  379. + struct spi_master *master;
  380. + struct rt2880_spi *rs;
  381. + unsigned long flags;
  382. + void __iomem *base;
  383. + struct resource *r;
  384. + int status = 0;
  385. + struct clk *clk;
  386. +
  387. + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  388. + base = devm_ioremap_resource(&pdev->dev, r);
  389. + if (IS_ERR(base))
  390. + return PTR_ERR(base);
  391. +
  392. + clk = devm_clk_get(&pdev->dev, NULL);
  393. + if (IS_ERR(clk)) {
  394. + dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
  395. + status);
  396. + return PTR_ERR(clk);
  397. + }
  398. +
  399. + status = clk_prepare_enable(clk);
  400. + if (status)
  401. + return status;
  402. +
  403. + master = spi_alloc_master(&pdev->dev, sizeof(*rs));
  404. + if (master == NULL) {
  405. + dev_dbg(&pdev->dev, "master allocation failed\n");
  406. + return -ENOMEM;
  407. + }
  408. +
  409. + /* we support only mode 0, and no options */
  410. + master->mode_bits = 0;
  411. +
  412. + master->setup = rt2880_spi_setup;
  413. + master->transfer_one_message = rt2880_spi_transfer_one_message;
  414. + master->num_chipselect = RALINK_NUM_CHIPSELECTS;
  415. + master->bits_per_word_mask = SPI_BPW_MASK(8);
  416. + master->dev.of_node = pdev->dev.of_node;
  417. +
  418. + dev_set_drvdata(&pdev->dev, master);
  419. +
  420. + rs = spi_master_get_devdata(master);
  421. + rs->base = base;
  422. + rs->clk = clk;
  423. + rs->master = master;
  424. + rs->sys_freq = clk_get_rate(rs->clk);
  425. + dev_dbg(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
  426. + spin_lock_irqsave(&rs->lock, flags);
  427. +
  428. + device_reset(&pdev->dev);
  429. +
  430. + rt2880_spi_reset(rs);
  431. +
  432. + return spi_register_master(master);
  433. +}
  434. +
  435. +static int rt2880_spi_remove(struct platform_device *pdev)
  436. +{
  437. + struct spi_master *master;
  438. + struct rt2880_spi *rs;
  439. +
  440. + master = dev_get_drvdata(&pdev->dev);
  441. + rs = spi_master_get_devdata(master);
  442. +
  443. + clk_disable(rs->clk);
  444. + spi_unregister_master(master);
  445. +
  446. + return 0;
  447. +}
  448. +
  449. +MODULE_ALIAS("platform:" DRIVER_NAME);
  450. +
  451. +static const struct of_device_id rt2880_spi_match[] = {
  452. + { .compatible = "ralink,rt2880-spi" },
  453. + {},
  454. +};
  455. +MODULE_DEVICE_TABLE(of, rt2880_spi_match);
  456. +
  457. +static struct platform_driver rt2880_spi_driver = {
  458. + .driver = {
  459. + .name = DRIVER_NAME,
  460. + .owner = THIS_MODULE,
  461. + .of_match_table = rt2880_spi_match,
  462. + },
  463. + .probe = rt2880_spi_probe,
  464. + .remove = rt2880_spi_remove,
  465. +};
  466. +
  467. +module_platform_driver(rt2880_spi_driver);
  468. +
  469. +MODULE_DESCRIPTION("Ralink SPI driver");
  470. +MODULE_AUTHOR("Sergiy <piratfm@gmail.com>");
  471. +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
  472. +MODULE_LICENSE("GPL");