0061-SPI-ralink-add-mt7621-SoC-spi-driver.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. --- a/drivers/spi/Kconfig
  2. +++ b/drivers/spi/Kconfig
  3. @@ -439,6 +439,12 @@ config SPI_RT2880
  4. help
  5. This selects a driver for the Ralink RT288x/RT305x SPI Controller.
  6. +config SPI_MT7621
  7. + tristate "MediaTek MT7621 SPI Controller"
  8. + depends on RALINK
  9. + help
  10. + This selects a driver for the MediaTek MT7621 SPI Controller.
  11. +
  12. config SPI_S3C24XX
  13. tristate "Samsung S3C24XX series SPI"
  14. depends on ARCH_S3C24XX
  15. --- a/drivers/spi/Makefile
  16. +++ b/drivers/spi/Makefile
  17. @@ -46,6 +46,7 @@ obj-$(CONFIG_SPI_LM70_LLP) += spi-lm70l
  18. obj-$(CONFIG_SPI_MPC512x_PSC) += spi-mpc512x-psc.o
  19. obj-$(CONFIG_SPI_MPC52xx_PSC) += spi-mpc52xx-psc.o
  20. obj-$(CONFIG_SPI_MPC52xx) += spi-mpc52xx.o
  21. +obj-$(CONFIG_SPI_MT7621) += spi-mt7621.o
  22. obj-$(CONFIG_SPI_MXS) += spi-mxs.o
  23. obj-$(CONFIG_SPI_NUC900) += spi-nuc900.o
  24. obj-$(CONFIG_SPI_OC_TINY) += spi-oc-tiny.o
  25. --- /dev/null
  26. +++ b/drivers/spi/spi-mt7621.c
  27. @@ -0,0 +1,480 @@
  28. +/*
  29. + * spi-mt7621.c -- MediaTek MT7621 SPI controller driver
  30. + *
  31. + * Copyright (C) 2011 Sergiy <[email protected]>
  32. + * Copyright (C) 2011-2013 Gabor Juhos <[email protected]>
  33. + * Copyright (C) 2014-2015 Felix Fietkau <[email protected]>
  34. + *
  35. + * Some parts are based on spi-orion.c:
  36. + * Author: Shadi Ammouri <[email protected]>
  37. + * Copyright (C) 2007-2008 Marvell Ltd.
  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. +#include <linux/init.h>
  45. +#include <linux/module.h>
  46. +#include <linux/clk.h>
  47. +#include <linux/err.h>
  48. +#include <linux/delay.h>
  49. +#include <linux/io.h>
  50. +#include <linux/reset.h>
  51. +#include <linux/spi/spi.h>
  52. +#include <linux/of_device.h>
  53. +#include <linux/platform_device.h>
  54. +#include <linux/swab.h>
  55. +
  56. +#include <ralink_regs.h>
  57. +
  58. +#define SPI_BPW_MASK(bits) BIT((bits) - 1)
  59. +
  60. +#define DRIVER_NAME "spi-mt7621"
  61. +/* in usec */
  62. +#define RALINK_SPI_WAIT_MAX_LOOP 2000
  63. +
  64. +/* SPISTAT register bit field */
  65. +#define SPISTAT_BUSY BIT(0)
  66. +
  67. +#define MT7621_SPI_TRANS 0x00
  68. +#define SPITRANS_BUSY BIT(16)
  69. +
  70. +#define MT7621_SPI_OPCODE 0x04
  71. +#define MT7621_SPI_DATA0 0x08
  72. +#define MT7621_SPI_DATA4 0x18
  73. +#define SPI_CTL_TX_RX_CNT_MASK 0xff
  74. +#define SPI_CTL_START BIT(8)
  75. +
  76. +#define MT7621_SPI_POLAR 0x38
  77. +#define MT7621_SPI_MASTER 0x28
  78. +#define MT7621_SPI_MOREBUF 0x2c
  79. +#define MT7621_SPI_SPACE 0x3c
  80. +
  81. +#define MT7621_CPHA BIT(5)
  82. +#define MT7621_CPOL BIT(4)
  83. +#define MT7621_LSB_FIRST BIT(3)
  84. +
  85. +#define RT2880_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH)
  86. +
  87. +struct mt7621_spi;
  88. +
  89. +struct mt7621_spi {
  90. + struct spi_master *master;
  91. + void __iomem *base;
  92. + unsigned int sys_freq;
  93. + unsigned int speed;
  94. + struct clk *clk;
  95. + spinlock_t lock;
  96. +
  97. + struct mt7621_spi_ops *ops;
  98. +};
  99. +
  100. +static inline struct mt7621_spi *spidev_to_mt7621_spi(struct spi_device *spi)
  101. +{
  102. + return spi_master_get_devdata(spi->master);
  103. +}
  104. +
  105. +static inline u32 mt7621_spi_read(struct mt7621_spi *rs, u32 reg)
  106. +{
  107. + return ioread32(rs->base + reg);
  108. +}
  109. +
  110. +static inline void mt7621_spi_write(struct mt7621_spi *rs, u32 reg, u32 val)
  111. +{
  112. + iowrite32(val, rs->base + reg);
  113. +}
  114. +
  115. +static void mt7621_spi_reset(struct mt7621_spi *rs, int duplex)
  116. +{
  117. + u32 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
  118. +
  119. + master |= 7 << 29;
  120. + master |= 1 << 2;
  121. + if (duplex)
  122. + master |= 1 << 10;
  123. + else
  124. + master &= ~(1 << 10);
  125. +
  126. + mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
  127. +}
  128. +
  129. +static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
  130. +{
  131. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  132. + int cs = spi->chip_select;
  133. + u32 polar = 0;
  134. +
  135. + mt7621_spi_reset(rs, cs);
  136. + if (enable)
  137. + polar = BIT(cs);
  138. + mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
  139. +}
  140. +
  141. +static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
  142. +{
  143. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  144. + u32 rate;
  145. + u32 reg;
  146. +
  147. + dev_dbg(&spi->dev, "speed:%u\n", speed);
  148. +
  149. + rate = DIV_ROUND_UP(rs->sys_freq, speed);
  150. + dev_dbg(&spi->dev, "rate-1:%u\n", rate);
  151. +
  152. + if (rate > 4097)
  153. + return -EINVAL;
  154. +
  155. + if (rate < 2)
  156. + rate = 2;
  157. +
  158. + reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
  159. + reg &= ~(0xfff << 16);
  160. + reg |= (rate - 2) << 16;
  161. + rs->speed = speed;
  162. +
  163. + reg &= ~MT7621_LSB_FIRST;
  164. + if (spi->mode & SPI_LSB_FIRST)
  165. + reg |= MT7621_LSB_FIRST;
  166. +
  167. + reg &= ~(MT7621_CPHA | MT7621_CPOL);
  168. + switch(spi->mode & (SPI_CPOL | SPI_CPHA)) {
  169. + case SPI_MODE_0:
  170. + break;
  171. + case SPI_MODE_1:
  172. + reg |= MT7621_CPHA;
  173. + break;
  174. + case SPI_MODE_2:
  175. + reg |= MT7621_CPOL;
  176. + break;
  177. + case SPI_MODE_3:
  178. + reg |= MT7621_CPOL | MT7621_CPHA;
  179. + break;
  180. + }
  181. + mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
  182. +
  183. + return 0;
  184. +}
  185. +
  186. +static inline int mt7621_spi_wait_till_ready(struct spi_device *spi)
  187. +{
  188. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  189. + int i;
  190. +
  191. + for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
  192. + u32 status;
  193. +
  194. + status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
  195. + if ((status & SPITRANS_BUSY) == 0) {
  196. + return 0;
  197. + }
  198. + cpu_relax();
  199. + udelay(1);
  200. + }
  201. +
  202. + return -ETIMEDOUT;
  203. +}
  204. +
  205. +static int mt7621_spi_transfer_half_duplex(struct spi_master *master,
  206. + struct spi_message *m)
  207. +{
  208. + struct mt7621_spi *rs = spi_master_get_devdata(master);
  209. + struct spi_device *spi = m->spi;
  210. + unsigned int speed = spi->max_speed_hz;
  211. + struct spi_transfer *t = NULL;
  212. + int status = 0;
  213. + int i, len = 0;
  214. + int rx_len = 0;
  215. + u32 data[9] = { 0 };
  216. + u32 val;
  217. +
  218. + mt7621_spi_wait_till_ready(spi);
  219. +
  220. + list_for_each_entry(t, &m->transfers, transfer_list) {
  221. + const u8 *buf = t->tx_buf;
  222. +
  223. + if (t->rx_buf)
  224. + rx_len += t->len;
  225. +
  226. + if (!buf)
  227. + continue;
  228. +
  229. + if (WARN_ON(len + t->len > 36)) {
  230. + status = -EIO;
  231. + goto msg_done;
  232. + }
  233. +
  234. + for (i = 0; i < t->len; i++, len++)
  235. + data[len / 4] |= buf[i] << (8 * (len & 3));
  236. + }
  237. +
  238. + if (WARN_ON(rx_len > 32)) {
  239. + status = -EIO;
  240. + goto msg_done;
  241. + }
  242. +
  243. + if (mt7621_spi_prepare(spi, speed)) {
  244. + status = -EIO;
  245. + goto msg_done;
  246. + }
  247. + data[0] = swab32(data[0]);
  248. + if (len < 4)
  249. + data[0] >>= (4 - len) * 8;
  250. +
  251. + for (i = 0; i < len; i += 4)
  252. + mt7621_spi_write(rs, MT7621_SPI_OPCODE + i, data[i / 4]);
  253. +
  254. + val = (min_t(int, len, 4) * 8) << 24;
  255. + if (len > 4)
  256. + val |= (len - 4) * 8;
  257. + val |= (rx_len * 8) << 12;
  258. + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
  259. +
  260. + mt7621_spi_set_cs(spi, 1);
  261. +
  262. + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
  263. + val |= SPI_CTL_START;
  264. + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
  265. +
  266. + mt7621_spi_wait_till_ready(spi);
  267. +
  268. + mt7621_spi_set_cs(spi, 0);
  269. +
  270. + for (i = 0; i < rx_len; i += 4)
  271. + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
  272. +
  273. + m->actual_length = len + rx_len;
  274. +
  275. + len = 0;
  276. + list_for_each_entry(t, &m->transfers, transfer_list) {
  277. + u8 *buf = t->rx_buf;
  278. +
  279. + if (!buf)
  280. + continue;
  281. +
  282. + for (i = 0; i < t->len; i++, len++)
  283. + buf[i] = data[len / 4] >> (8 * (len & 3));
  284. + }
  285. +
  286. +msg_done:
  287. + m->status = status;
  288. + spi_finalize_current_message(master);
  289. +
  290. + return 0;
  291. +}
  292. +
  293. +static int mt7621_spi_transfer_full_duplex(struct spi_master *master,
  294. + struct spi_message *m)
  295. +{
  296. + struct mt7621_spi *rs = spi_master_get_devdata(master);
  297. + struct spi_device *spi = m->spi;
  298. + unsigned int speed = spi->max_speed_hz;
  299. + struct spi_transfer *t = NULL;
  300. + int status = 0;
  301. + int i, len = 0;
  302. + int rx_len = 0;
  303. + u32 data[9] = { 0 };
  304. + u32 val = 0;
  305. +
  306. + mt7621_spi_wait_till_ready(spi);
  307. +
  308. + list_for_each_entry(t, &m->transfers, transfer_list) {
  309. + const u8 *buf = t->tx_buf;
  310. +
  311. + if (t->rx_buf)
  312. + rx_len += t->len;
  313. +
  314. + if (!buf)
  315. + continue;
  316. +
  317. + if (WARN_ON(len + t->len > 16)) {
  318. + status = -EIO;
  319. + goto msg_done;
  320. + }
  321. +
  322. + for (i = 0; i < t->len; i++, len++)
  323. + data[len / 4] |= buf[i] << (8 * (len & 3));
  324. + if (speed > t->speed_hz)
  325. + speed = t->speed_hz;
  326. + }
  327. +
  328. + if (WARN_ON(rx_len > 16)) {
  329. + status = -EIO;
  330. + goto msg_done;
  331. + }
  332. +
  333. + if (mt7621_spi_prepare(spi, speed)) {
  334. + status = -EIO;
  335. + goto msg_done;
  336. + }
  337. +
  338. + for (i = 0; i < len; i += 4)
  339. + mt7621_spi_write(rs, MT7621_SPI_DATA0 + i, data[i / 4]);
  340. +
  341. + val |= len * 8;
  342. + val |= (rx_len * 8) << 12;
  343. + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
  344. +
  345. + mt7621_spi_set_cs(spi, 1);
  346. +
  347. + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
  348. + val |= SPI_CTL_START;
  349. + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
  350. +
  351. + mt7621_spi_wait_till_ready(spi);
  352. +
  353. + mt7621_spi_set_cs(spi, 0);
  354. +
  355. + for (i = 0; i < rx_len; i += 4)
  356. + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA4 + i);
  357. +
  358. + m->actual_length = rx_len;
  359. +
  360. + len = 0;
  361. + list_for_each_entry(t, &m->transfers, transfer_list) {
  362. + u8 *buf = t->rx_buf;
  363. +
  364. + if (!buf)
  365. + continue;
  366. +
  367. + for (i = 0; i < t->len; i++, len++)
  368. + buf[i] = data[len / 4] >> (8 * (len & 3));
  369. + }
  370. +
  371. +msg_done:
  372. + m->status = status;
  373. + spi_finalize_current_message(master);
  374. +
  375. + return 0;
  376. +}
  377. +
  378. +static int mt7621_spi_transfer_one_message(struct spi_master *master,
  379. + struct spi_message *m)
  380. +{
  381. + struct spi_device *spi = m->spi;
  382. + int cs = spi->chip_select;
  383. +
  384. + if (cs)
  385. + return mt7621_spi_transfer_full_duplex(master, m);
  386. + return mt7621_spi_transfer_half_duplex(master, m);
  387. +}
  388. +
  389. +static int mt7621_spi_setup(struct spi_device *spi)
  390. +{
  391. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  392. +
  393. + if ((spi->max_speed_hz == 0) ||
  394. + (spi->max_speed_hz > (rs->sys_freq / 2)))
  395. + spi->max_speed_hz = (rs->sys_freq / 2);
  396. +
  397. + if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
  398. + dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
  399. + spi->max_speed_hz);
  400. + return -EINVAL;
  401. + }
  402. +
  403. + return 0;
  404. +}
  405. +
  406. +static const struct of_device_id mt7621_spi_match[] = {
  407. + { .compatible = "ralink,mt7621-spi" },
  408. + {},
  409. +};
  410. +MODULE_DEVICE_TABLE(of, mt7621_spi_match);
  411. +
  412. +static int mt7621_spi_probe(struct platform_device *pdev)
  413. +{
  414. + const struct of_device_id *match;
  415. + struct spi_master *master;
  416. + struct mt7621_spi *rs;
  417. + unsigned long flags;
  418. + void __iomem *base;
  419. + struct resource *r;
  420. + int status = 0;
  421. + struct clk *clk;
  422. + struct mt7621_spi_ops *ops;
  423. +
  424. + match = of_match_device(mt7621_spi_match, &pdev->dev);
  425. + if (!match)
  426. + return -EINVAL;
  427. + ops = (struct mt7621_spi_ops *)match->data;
  428. +
  429. + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  430. + base = devm_ioremap_resource(&pdev->dev, r);
  431. + if (IS_ERR(base))
  432. + return PTR_ERR(base);
  433. +
  434. + clk = devm_clk_get(&pdev->dev, NULL);
  435. + if (IS_ERR(clk)) {
  436. + dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
  437. + status);
  438. + return PTR_ERR(clk);
  439. + }
  440. +
  441. + status = clk_prepare_enable(clk);
  442. + if (status)
  443. + return status;
  444. +
  445. + master = spi_alloc_master(&pdev->dev, sizeof(*rs));
  446. + if (master == NULL) {
  447. + dev_info(&pdev->dev, "master allocation failed\n");
  448. + return -ENOMEM;
  449. + }
  450. +
  451. + master->mode_bits = RT2880_SPI_MODE_BITS;
  452. +
  453. + master->setup = mt7621_spi_setup;
  454. + master->transfer_one_message = mt7621_spi_transfer_one_message;
  455. + master->bits_per_word_mask = SPI_BPW_MASK(8);
  456. + master->dev.of_node = pdev->dev.of_node;
  457. + master->num_chipselect = 2;
  458. +
  459. + dev_set_drvdata(&pdev->dev, master);
  460. +
  461. + rs = spi_master_get_devdata(master);
  462. + rs->base = base;
  463. + rs->clk = clk;
  464. + rs->master = master;
  465. + rs->sys_freq = clk_get_rate(rs->clk);
  466. + rs->ops = ops;
  467. + dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
  468. + spin_lock_irqsave(&rs->lock, flags);
  469. +
  470. + device_reset(&pdev->dev);
  471. +
  472. + mt7621_spi_reset(rs, 0);
  473. +
  474. + return spi_register_master(master);
  475. +}
  476. +
  477. +static int mt7621_spi_remove(struct platform_device *pdev)
  478. +{
  479. + struct spi_master *master;
  480. + struct mt7621_spi *rs;
  481. +
  482. + master = dev_get_drvdata(&pdev->dev);
  483. + rs = spi_master_get_devdata(master);
  484. +
  485. + clk_disable(rs->clk);
  486. + spi_unregister_master(master);
  487. +
  488. + return 0;
  489. +}
  490. +
  491. +MODULE_ALIAS("platform:" DRIVER_NAME);
  492. +
  493. +static struct platform_driver mt7621_spi_driver = {
  494. + .driver = {
  495. + .name = DRIVER_NAME,
  496. + .owner = THIS_MODULE,
  497. + .of_match_table = mt7621_spi_match,
  498. + },
  499. + .probe = mt7621_spi_probe,
  500. + .remove = mt7621_spi_remove,
  501. +};
  502. +
  503. +module_platform_driver(mt7621_spi_driver);
  504. +
  505. +MODULE_DESCRIPTION("MT7621 SPI driver");
  506. +MODULE_AUTHOR("Felix Fietkau <[email protected]>");
  507. +MODULE_LICENSE("GPL");