0144-SPI-ralink-add-Ralink-SoC-spi-driver.patch 13 KB

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