0043-spi-add-mt7621-support.patch 12 KB

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