0051-rt5350-spi-second-device.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. From 27b11d4f1888e1a3d6d75b46d4d5a4d86fc03891 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Wed, 6 Aug 2014 10:53:40 +0200
  4. Subject: [PATCH 51/57] SPI: MIPS: ralink: add rt5350 dual SPI support
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. Signed-off-by: Felix Fietkau <nbd@openwrt.org>
  7. ---
  8. drivers/spi/spi-rt2880.c | 218 +++++++++++++++++++++++++++++++++++++++++++---
  9. 1 file changed, 205 insertions(+), 13 deletions(-)
  10. --- a/drivers/spi/spi-rt2880.c
  11. +++ b/drivers/spi/spi-rt2880.c
  12. @@ -21,19 +21,25 @@
  13. #include <linux/io.h>
  14. #include <linux/reset.h>
  15. #include <linux/spi/spi.h>
  16. +#include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. +#include <ralink_regs.h>
  19. +
  20. +#define SPI_BPW_MASK(bits) BIT((bits) - 1)
  21. +
  22. #define DRIVER_NAME "spi-rt2880"
  23. -/* only one slave is supported*/
  24. -#define RALINK_NUM_CHIPSELECTS 1
  25. /* in usec */
  26. #define RALINK_SPI_WAIT_MAX_LOOP 2000
  27. -#define RAMIPS_SPI_STAT 0x00
  28. -#define RAMIPS_SPI_CFG 0x10
  29. -#define RAMIPS_SPI_CTL 0x14
  30. -#define RAMIPS_SPI_DATA 0x20
  31. -#define RAMIPS_SPI_FIFO_STAT 0x38
  32. +#define RAMIPS_SPI_DEV_OFFSET 0x40
  33. +
  34. +#define RAMIPS_SPI_STAT(cs) (0x00 + (cs * RAMIPS_SPI_DEV_OFFSET))
  35. +#define RAMIPS_SPI_CFG(cs) (0x10 + (cs * RAMIPS_SPI_DEV_OFFSET))
  36. +#define RAMIPS_SPI_CTL(cs) (0x14 + (cs * RAMIPS_SPI_DEV_OFFSET))
  37. +#define RAMIPS_SPI_DATA(cs) (0x20 + (cs * RAMIPS_SPI_DEV_OFFSET))
  38. +#define RAMIPS_SPI_FIFO_STAT(cs) (0x38 + (cs * RAMIPS_SPI_DEV_OFFSET))
  39. +#define RAMIPS_SPI_ARBITER 0xF0
  40. /* SPISTAT register bit field */
  41. #define SPISTAT_BUSY BIT(0)
  42. @@ -63,6 +69,19 @@
  43. /* SPIFIFOSTAT register bit field */
  44. #define SPIFIFOSTAT_TXFULL BIT(17)
  45. +#define SPICTL_ARB_EN BIT(31)
  46. +#define SPI1_POR BIT(1)
  47. +#define SPI0_POR BIT(0)
  48. +
  49. +#define RT2880_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH)
  50. +
  51. +struct rt2880_spi;
  52. +
  53. +struct rt2880_spi_ops {
  54. + void (*init_hw)(struct rt2880_spi *rs);
  55. + int num_cs;
  56. +};
  57. +
  58. struct rt2880_spi {
  59. struct spi_master *master;
  60. void __iomem *base;
  61. @@ -70,6 +89,8 @@ struct rt2880_spi {
  62. unsigned int speed;
  63. struct clk *clk;
  64. spinlock_t lock;
  65. +
  66. + struct rt2880_spi_ops *ops;
  67. };
  68. static inline struct rt2880_spi *spidev_to_rt2880_spi(struct spi_device *spi)
  69. @@ -115,6 +136,7 @@ static inline void rt2880_spi_clrbits(st
  70. static int rt2880_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
  71. {
  72. + int cs = spi->chip_select;
  73. struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  74. u32 rate;
  75. u32 prescale;
  76. @@ -142,9 +164,9 @@ static int rt2880_spi_baudrate_set(struc
  77. prescale = ilog2(rate / 2);
  78. dev_dbg(&spi->dev, "prescale:%u\n", prescale);
  79. - reg = rt2880_spi_read(rs, RAMIPS_SPI_CFG);
  80. + reg = rt2880_spi_read(rs, RAMIPS_SPI_CFG(cs));
  81. reg = ((reg & ~SPICFG_SPICLK_PRESCALE_MASK) | prescale);
  82. - rt2880_spi_write(rs, RAMIPS_SPI_CFG, reg);
  83. + rt2880_spi_write(rs, RAMIPS_SPI_CFG(cs), reg);
  84. rs->speed = speed;
  85. return 0;
  86. }
  87. @@ -157,7 +179,8 @@ rt2880_spi_setup_transfer(struct spi_dev
  88. {
  89. struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  90. unsigned int speed = spi->max_speed_hz;
  91. - int rc;
  92. + int rc, cs = spi->chip_select;
  93. + u32 reg;
  94. if ((t != NULL) && t->speed_hz)
  95. speed = t->speed_hz;
  96. @@ -169,25 +192,68 @@ rt2880_spi_setup_transfer(struct spi_dev
  97. return rc;
  98. }
  99. + reg = rt2880_spi_read(rs, RAMIPS_SPI_CFG(cs));
  100. +
  101. + reg = (reg & ~SPICFG_MSBFIRST);
  102. + if (!(spi->mode & SPI_LSB_FIRST))
  103. + reg |= SPICFG_MSBFIRST;
  104. +
  105. + reg = (reg & ~(SPICFG_SPICLKPOL | SPICFG_RXCLKEDGE_FALLING |SPICFG_TXCLKEDGE_FALLING));
  106. + switch(spi->mode & (SPI_CPOL | SPI_CPHA)) {
  107. + case SPI_MODE_0:
  108. + reg |= SPICFG_SPICLKPOL | SPICFG_TXCLKEDGE_FALLING;
  109. + break;
  110. + case SPI_MODE_1:
  111. + reg |= SPICFG_SPICLKPOL | SPICFG_RXCLKEDGE_FALLING;
  112. + break;
  113. + case SPI_MODE_2:
  114. + reg |= SPICFG_RXCLKEDGE_FALLING;
  115. + break;
  116. + case SPI_MODE_3:
  117. + reg |= SPICFG_TXCLKEDGE_FALLING;
  118. + break;
  119. + }
  120. +
  121. + rt2880_spi_write(rs, RAMIPS_SPI_CFG(cs), reg);
  122. +
  123. + reg = SPICTL_ARB_EN;
  124. + if (spi->mode & SPI_CS_HIGH) {
  125. + switch(cs) {
  126. + case 0:
  127. + reg |= SPI0_POR;
  128. + break;
  129. + case 1:
  130. + reg |= SPI1_POR;
  131. + break;
  132. + }
  133. + }
  134. +
  135. + rt2880_spi_write(rs, RAMIPS_SPI_ARBITER, reg);
  136. +
  137. return 0;
  138. }
  139. -static void rt2880_spi_set_cs(struct rt2880_spi *rs, int enable)
  140. +static void rt2880_spi_set_cs(struct spi_device *spi, int enable)
  141. {
  142. + struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  143. + int cs = spi->chip_select;
  144. +
  145. if (enable)
  146. - rt2880_spi_clrbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
  147. + rt2880_spi_clrbits(rs, RAMIPS_SPI_CTL(cs), SPICTL_SPIENA);
  148. else
  149. - rt2880_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_SPIENA);
  150. + rt2880_spi_setbits(rs, RAMIPS_SPI_CTL(cs), SPICTL_SPIENA);
  151. }
  152. -static inline int rt2880_spi_wait_till_ready(struct rt2880_spi *rs)
  153. +static inline int rt2880_spi_wait_till_ready(struct spi_device *spi)
  154. {
  155. + struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  156. + int cs = spi->chip_select;
  157. int i;
  158. for (i = 0; i < RALINK_SPI_WAIT_MAX_LOOP; i++) {
  159. u32 status;
  160. - status = rt2880_spi_read(rs, RAMIPS_SPI_STAT);
  161. + status = rt2880_spi_read(rs, RAMIPS_SPI_STAT(cs));
  162. if ((status & SPISTAT_BUSY) == 0)
  163. return 0;
  164. @@ -199,9 +265,10 @@ static inline int rt2880_spi_wait_till_r
  165. }
  166. static unsigned int
  167. -rt2880_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
  168. +rt2880_spi_write_read(struct spi_device *spi, struct list_head *list, struct spi_transfer *xfer)
  169. {
  170. struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  171. + int cs = spi->chip_select;
  172. unsigned count = 0;
  173. u8 *rx = xfer->rx_buf;
  174. const u8 *tx = xfer->tx_buf;
  175. @@ -213,9 +280,9 @@ rt2880_spi_write_read(struct spi_device
  176. if (tx) {
  177. for (count = 0; count < xfer->len; count++) {
  178. - rt2880_spi_write(rs, RAMIPS_SPI_DATA, tx[count]);
  179. - rt2880_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_STARTWR);
  180. - err = rt2880_spi_wait_till_ready(rs);
  181. + rt2880_spi_write(rs, RAMIPS_SPI_DATA(cs), tx[count]);
  182. + rt2880_spi_setbits(rs, RAMIPS_SPI_CTL(cs), SPICTL_STARTWR);
  183. + err = rt2880_spi_wait_till_ready(spi);
  184. if (err) {
  185. dev_err(&spi->dev, "TX failed, err=%d\n", err);
  186. goto out;
  187. @@ -225,13 +292,13 @@ rt2880_spi_write_read(struct spi_device
  188. if (rx) {
  189. for (count = 0; count < xfer->len; count++) {
  190. - rt2880_spi_setbits(rs, RAMIPS_SPI_CTL, SPICTL_STARTRD);
  191. - err = rt2880_spi_wait_till_ready(rs);
  192. + rt2880_spi_setbits(rs, RAMIPS_SPI_CTL(cs), SPICTL_STARTRD);
  193. + err = rt2880_spi_wait_till_ready(spi);
  194. if (err) {
  195. dev_err(&spi->dev, "RX failed, err=%d\n", err);
  196. goto out;
  197. }
  198. - rx[count] = (u8) rt2880_spi_read(rs, RAMIPS_SPI_DATA);
  199. + rx[count] = (u8) rt2880_spi_read(rs, RAMIPS_SPI_DATA(cs));
  200. }
  201. }
  202. @@ -280,25 +347,25 @@ static int rt2880_spi_transfer_one_messa
  203. }
  204. if (!cs_active) {
  205. - rt2880_spi_set_cs(rs, 1);
  206. + rt2880_spi_set_cs(spi, 1);
  207. cs_active = 1;
  208. }
  209. if (t->len)
  210. - m->actual_length += rt2880_spi_write_read(spi, t);
  211. + m->actual_length += rt2880_spi_write_read(spi, &m->transfers, t);
  212. if (t->delay_usecs)
  213. udelay(t->delay_usecs);
  214. if (t->cs_change) {
  215. - rt2880_spi_set_cs(rs, 0);
  216. + rt2880_spi_set_cs(spi, 0);
  217. cs_active = 0;
  218. }
  219. }
  220. msg_done:
  221. if (cs_active)
  222. - rt2880_spi_set_cs(rs, 0);
  223. + rt2880_spi_set_cs(spi, 0);
  224. m->status = status;
  225. spi_finalize_current_message(master);
  226. @@ -311,7 +378,7 @@ static int rt2880_spi_setup(struct spi_d
  227. struct rt2880_spi *rs = spidev_to_rt2880_spi(spi);
  228. if ((spi->max_speed_hz == 0) ||
  229. - (spi->max_speed_hz > (rs->sys_freq / 2)))
  230. + (spi->max_speed_hz > (rs->sys_freq / 2)))
  231. spi->max_speed_hz = (rs->sys_freq / 2);
  232. if (spi->max_speed_hz < (rs->sys_freq / 128)) {
  233. @@ -328,14 +395,47 @@ static int rt2880_spi_setup(struct spi_d
  234. static void rt2880_spi_reset(struct rt2880_spi *rs)
  235. {
  236. - rt2880_spi_write(rs, RAMIPS_SPI_CFG,
  237. + rt2880_spi_write(rs, RAMIPS_SPI_CFG(0),
  238. SPICFG_MSBFIRST | SPICFG_TXCLKEDGE_FALLING |
  239. SPICFG_SPICLK_DIV16 | SPICFG_SPICLKPOL);
  240. - rt2880_spi_write(rs, RAMIPS_SPI_CTL, SPICTL_HIZSDO | SPICTL_SPIENA);
  241. + rt2880_spi_write(rs, RAMIPS_SPI_CTL(0), SPICTL_HIZSDO | SPICTL_SPIENA);
  242. }
  243. +static void rt5350_spi_reset(struct rt2880_spi *rs)
  244. +{
  245. + int cs;
  246. +
  247. + rt2880_spi_write(rs, RAMIPS_SPI_ARBITER,
  248. + SPICTL_ARB_EN);
  249. +
  250. + for (cs = 0; cs < rs->ops->num_cs; cs++) {
  251. + rt2880_spi_write(rs, RAMIPS_SPI_CFG(cs),
  252. + SPICFG_MSBFIRST | SPICFG_TXCLKEDGE_FALLING |
  253. + SPICFG_SPICLK_DIV16 | SPICFG_SPICLKPOL);
  254. + rt2880_spi_write(rs, RAMIPS_SPI_CTL(cs), SPICTL_HIZSDO | SPICTL_SPIENA);
  255. + }
  256. +}
  257. +
  258. +static struct rt2880_spi_ops spi_ops[] = {
  259. + {
  260. + .init_hw = rt2880_spi_reset,
  261. + .num_cs = 1,
  262. + }, {
  263. + .init_hw = rt5350_spi_reset,
  264. + .num_cs = 2,
  265. + },
  266. +};
  267. +
  268. +static const struct of_device_id rt2880_spi_match[] = {
  269. + { .compatible = "ralink,rt2880-spi", .data = &spi_ops[0]},
  270. + { .compatible = "ralink,rt5350-spi", .data = &spi_ops[1]},
  271. + {},
  272. +};
  273. +MODULE_DEVICE_TABLE(of, rt2880_spi_match);
  274. +
  275. static int rt2880_spi_probe(struct platform_device *pdev)
  276. {
  277. + const struct of_device_id *match;
  278. struct spi_master *master;
  279. struct rt2880_spi *rs;
  280. unsigned long flags;
  281. @@ -343,6 +443,12 @@ static int rt2880_spi_probe(struct platf
  282. struct resource *r;
  283. int status = 0;
  284. struct clk *clk;
  285. + struct rt2880_spi_ops *ops;
  286. +
  287. + match = of_match_device(rt2880_spi_match, &pdev->dev);
  288. + if (!match)
  289. + return -EINVAL;
  290. + ops = (struct rt2880_spi_ops *)match->data;
  291. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  292. base = devm_ioremap_resource(&pdev->dev, r);
  293. @@ -366,14 +472,13 @@ static int rt2880_spi_probe(struct platf
  294. return -ENOMEM;
  295. }
  296. - /* we support only mode 0, and no options */
  297. - master->mode_bits = 0;
  298. + master->mode_bits = RT2880_SPI_MODE_BITS;
  299. master->setup = rt2880_spi_setup;
  300. master->transfer_one_message = rt2880_spi_transfer_one_message;
  301. - master->num_chipselect = RALINK_NUM_CHIPSELECTS;
  302. master->bits_per_word_mask = SPI_BPW_MASK(8);
  303. master->dev.of_node = pdev->dev.of_node;
  304. + master->num_chipselect = ops->num_cs;
  305. dev_set_drvdata(&pdev->dev, master);
  306. @@ -382,12 +487,13 @@ static int rt2880_spi_probe(struct platf
  307. rs->clk = clk;
  308. rs->master = master;
  309. rs->sys_freq = clk_get_rate(rs->clk);
  310. + rs->ops = ops;
  311. dev_dbg(&pdev->dev, "sys_freq: %u\n", rs->sys_freq);
  312. spin_lock_irqsave(&rs->lock, flags);
  313. device_reset(&pdev->dev);
  314. - rt2880_spi_reset(rs);
  315. + rs->ops->init_hw(rs);
  316. return spi_register_master(master);
  317. }
  318. @@ -408,12 +514,6 @@ static int rt2880_spi_remove(struct plat
  319. MODULE_ALIAS("platform:" DRIVER_NAME);
  320. -static const struct of_device_id rt2880_spi_match[] = {
  321. - { .compatible = "ralink,rt2880-spi" },
  322. - {},
  323. -};
  324. -MODULE_DEVICE_TABLE(of, rt2880_spi_match);
  325. -
  326. static struct platform_driver rt2880_spi_driver = {
  327. .driver = {
  328. .name = DRIVER_NAME,