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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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,394 @@
  28. +/*
  29. + * spi-mt7621.c -- MediaTek MT7621 SPI controller driver
  30. + *
  31. + * Copyright (C) 2011 Sergiy <piratfm@gmail.com>
  32. + * Copyright (C) 2011-2013 Gabor Juhos <juhosg@openwrt.org>
  33. + * Copyright (C) 2014-2015 Felix Fietkau <nbd@openwrt.org>
  34. + *
  35. + * Some parts are based on spi-orion.c:
  36. + * Author: Shadi Ammouri <shadi@marvell.com>
  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)
  116. +{
  117. + u32 master = mt7621_spi_read(rs, MT7621_SPI_MASTER);
  118. +
  119. + master |= 7 << 29;
  120. + master |= 1 << 2;
  121. + master &= ~(1 << 10);
  122. +
  123. + mt7621_spi_write(rs, MT7621_SPI_MASTER, master);
  124. +}
  125. +
  126. +static void mt7621_spi_set_cs(struct spi_device *spi, int enable)
  127. +{
  128. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  129. + int cs = spi->chip_select;
  130. + u32 polar = 0;
  131. +
  132. + mt7621_spi_reset(rs);
  133. + if (enable)
  134. + polar = BIT(cs);
  135. + mt7621_spi_write(rs, MT7621_SPI_POLAR, polar);
  136. +}
  137. +
  138. +static int mt7621_spi_prepare(struct spi_device *spi, unsigned int speed)
  139. +{
  140. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  141. + u32 rate;
  142. + u32 reg;
  143. +
  144. + dev_dbg(&spi->dev, "speed:%u\n", speed);
  145. +
  146. + rate = DIV_ROUND_UP(rs->sys_freq, speed);
  147. + dev_dbg(&spi->dev, "rate-1:%u\n", rate);
  148. +
  149. + if (rate > 4097)
  150. + return -EINVAL;
  151. +
  152. + if (rate < 2)
  153. + rate = 2;
  154. +
  155. + reg = mt7621_spi_read(rs, MT7621_SPI_MASTER);
  156. + reg &= ~(0xfff << 16);
  157. + reg |= (rate - 2) << 16;
  158. + rs->speed = speed;
  159. +
  160. + reg &= ~MT7621_LSB_FIRST;
  161. + if (spi->mode & SPI_LSB_FIRST)
  162. + reg |= MT7621_LSB_FIRST;
  163. +
  164. + reg &= ~(MT7621_CPHA | MT7621_CPOL);
  165. + switch(spi->mode & (SPI_CPOL | SPI_CPHA)) {
  166. + case SPI_MODE_0:
  167. + break;
  168. + case SPI_MODE_1:
  169. + reg |= MT7621_CPHA;
  170. + break;
  171. + case SPI_MODE_2:
  172. + reg |= MT7621_CPOL;
  173. + break;
  174. + case SPI_MODE_3:
  175. + reg |= MT7621_CPOL | MT7621_CPHA;
  176. + break;
  177. + }
  178. + mt7621_spi_write(rs, MT7621_SPI_MASTER, reg);
  179. +
  180. + return 0;
  181. +}
  182. +
  183. +static inline int mt7621_spi_wait_till_ready(struct spi_device *spi)
  184. +{
  185. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  186. + int i;
  187. +
  188. + for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
  189. + u32 status;
  190. +
  191. + status = mt7621_spi_read(rs, MT7621_SPI_TRANS);
  192. + if ((status & SPITRANS_BUSY) == 0) {
  193. + return 0;
  194. + }
  195. + cpu_relax();
  196. + udelay(1);
  197. + }
  198. +
  199. + return -ETIMEDOUT;
  200. +}
  201. +
  202. +static int mt7621_spi_transfer_half_duplex(struct spi_master *master,
  203. + struct spi_message *m)
  204. +{
  205. + struct mt7621_spi *rs = spi_master_get_devdata(master);
  206. + struct spi_device *spi = m->spi;
  207. + unsigned int speed = spi->max_speed_hz;
  208. + struct spi_transfer *t = NULL;
  209. + int status = 0;
  210. + int i, len = 0;
  211. + int rx_len = 0;
  212. + u32 data[9] = { 0 };
  213. + u32 val;
  214. +
  215. + mt7621_spi_wait_till_ready(spi);
  216. +
  217. + list_for_each_entry(t, &m->transfers, transfer_list) {
  218. + const u8 *buf = t->tx_buf;
  219. + int rlen = t->len;
  220. +
  221. + if (t->rx_buf)
  222. + rx_len += rlen;
  223. +
  224. + if (!buf)
  225. + continue;
  226. +
  227. + if (t->speed_hz < speed)
  228. + speed = t->speed_hz;
  229. +
  230. + if (WARN_ON(len + rlen > 36)) {
  231. + status = -EIO;
  232. + goto msg_done;
  233. + }
  234. +
  235. + for (i = 0; i < rlen; i++, len++)
  236. + data[len / 4] |= buf[i] << (8 * (len & 3));
  237. + }
  238. +
  239. + if (WARN_ON(rx_len > 32)) {
  240. + status = -EIO;
  241. + goto msg_done;
  242. + }
  243. +
  244. + if (mt7621_spi_prepare(spi, speed)) {
  245. + status = -EIO;
  246. + goto msg_done;
  247. + }
  248. + data[0] = swab32(data[0]);
  249. + if (len < 4)
  250. + data[0] >>= (4 - len) * 8;
  251. +
  252. + for (i = 0; i < len; i += 4)
  253. + mt7621_spi_write(rs, MT7621_SPI_OPCODE + i, data[i / 4]);
  254. +
  255. + val = (min_t(int, len, 4) * 8) << 24;
  256. + if (len > 4)
  257. + val |= (len - 4) * 8;
  258. + val |= (rx_len * 8) << 12;
  259. + mt7621_spi_write(rs, MT7621_SPI_MOREBUF, val);
  260. +
  261. + mt7621_spi_set_cs(spi, 1);
  262. +
  263. + val = mt7621_spi_read(rs, MT7621_SPI_TRANS);
  264. + val |= SPI_CTL_START;
  265. + mt7621_spi_write(rs, MT7621_SPI_TRANS, val);
  266. +
  267. + mt7621_spi_wait_till_ready(spi);
  268. +
  269. + mt7621_spi_set_cs(spi, 0);
  270. +
  271. + for (i = 0; i < rx_len; i += 4)
  272. + data[i / 4] = mt7621_spi_read(rs, MT7621_SPI_DATA0 + i);
  273. +
  274. + m->actual_length = len + rx_len;
  275. +
  276. + len = 0;
  277. + list_for_each_entry(t, &m->transfers, transfer_list) {
  278. + u8 *buf = t->rx_buf;
  279. +
  280. + if (!buf)
  281. + continue;
  282. +
  283. + for (i = 0; i < t->len; i++, len++)
  284. + buf[i] = data[len / 4] >> (8 * (len & 3));
  285. + }
  286. +
  287. +msg_done:
  288. + m->status = status;
  289. + spi_finalize_current_message(master);
  290. +
  291. + return 0;
  292. +}
  293. +
  294. +static int mt7621_spi_transfer_one_message(struct spi_master *master,
  295. + struct spi_message *m)
  296. +{
  297. + struct spi_device *spi = m->spi;
  298. + int cs = spi->chip_select;
  299. +
  300. + return mt7621_spi_transfer_half_duplex(master, m);
  301. +}
  302. +
  303. +static int mt7621_spi_setup(struct spi_device *spi)
  304. +{
  305. + struct mt7621_spi *rs = spidev_to_mt7621_spi(spi);
  306. +
  307. + if ((spi->max_speed_hz == 0) ||
  308. + (spi->max_speed_hz > (rs->sys_freq / 2)))
  309. + spi->max_speed_hz = (rs->sys_freq / 2);
  310. +
  311. + if (spi->max_speed_hz < (rs->sys_freq / 4097)) {
  312. + dev_err(&spi->dev, "setup: requested speed is too low %d Hz\n",
  313. + spi->max_speed_hz);
  314. + return -EINVAL;
  315. + }
  316. +
  317. + return 0;
  318. +}
  319. +
  320. +static const struct of_device_id mt7621_spi_match[] = {
  321. + { .compatible = "ralink,mt7621-spi" },
  322. + {},
  323. +};
  324. +MODULE_DEVICE_TABLE(of, mt7621_spi_match);
  325. +
  326. +static int mt7621_spi_probe(struct platform_device *pdev)
  327. +{
  328. + const struct of_device_id *match;
  329. + struct spi_master *master;
  330. + struct mt7621_spi *rs;
  331. + unsigned long flags;
  332. + void __iomem *base;
  333. + struct resource *r;
  334. + int status = 0;
  335. + struct clk *clk;
  336. + struct mt7621_spi_ops *ops;
  337. +
  338. + match = of_match_device(mt7621_spi_match, &pdev->dev);
  339. + if (!match)
  340. + return -EINVAL;
  341. + ops = (struct mt7621_spi_ops *)match->data;
  342. +
  343. + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  344. + base = devm_ioremap_resource(&pdev->dev, r);
  345. + if (IS_ERR(base))
  346. + return PTR_ERR(base);
  347. +
  348. + clk = devm_clk_get(&pdev->dev, NULL);
  349. + if (IS_ERR(clk)) {
  350. + dev_err(&pdev->dev, "unable to get SYS clock, err=%d\n",
  351. + status);
  352. + return PTR_ERR(clk);
  353. + }
  354. +
  355. + status = clk_prepare_enable(clk);
  356. + if (status)
  357. + return status;
  358. +
  359. + master = spi_alloc_master(&pdev->dev, sizeof(*rs));
  360. + if (master == NULL) {
  361. + dev_info(&pdev->dev, "master allocation failed\n");
  362. + return -ENOMEM;
  363. + }
  364. +
  365. + master->mode_bits = RT2880_SPI_MODE_BITS;
  366. +
  367. + master->setup = mt7621_spi_setup;
  368. + master->transfer_one_message = mt7621_spi_transfer_one_message;
  369. + master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
  370. + master->dev.of_node = pdev->dev.of_node;
  371. + master->num_chipselect = 2;
  372. +
  373. + dev_set_drvdata(&pdev->dev, master);
  374. +
  375. + rs = spi_master_get_devdata(master);
  376. + rs->base = base;
  377. + rs->clk = clk;
  378. + rs->master = master;
  379. + rs->sys_freq = clk_get_rate(rs->clk);
  380. + rs->ops = ops;
  381. + dev_info(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
  382. + spin_lock_irqsave(&rs->lock, flags);
  383. +
  384. + device_reset(&pdev->dev);
  385. +
  386. + mt7621_spi_reset(rs);
  387. +
  388. + return spi_register_master(master);
  389. +}
  390. +
  391. +static int mt7621_spi_remove(struct platform_device *pdev)
  392. +{
  393. + struct spi_master *master;
  394. + struct mt7621_spi *rs;
  395. +
  396. + master = dev_get_drvdata(&pdev->dev);
  397. + rs = spi_master_get_devdata(master);
  398. +
  399. + clk_disable(rs->clk);
  400. + spi_unregister_master(master);
  401. +
  402. + return 0;
  403. +}
  404. +
  405. +MODULE_ALIAS("platform:" DRIVER_NAME);
  406. +
  407. +static struct platform_driver mt7621_spi_driver = {
  408. + .driver = {
  409. + .name = DRIVER_NAME,
  410. + .owner = THIS_MODULE,
  411. + .of_match_table = mt7621_spi_match,
  412. + },
  413. + .probe = mt7621_spi_probe,
  414. + .remove = mt7621_spi_remove,
  415. +};
  416. +
  417. +module_platform_driver(mt7621_spi_driver);
  418. +
  419. +MODULE_DESCRIPTION("MT7621 SPI driver");
  420. +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  421. +MODULE_LICENSE("GPL");