spi-sh-msiof.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * SuperH MSIOF SPI Master Interface
  3. *
  4. * Copyright (c) 2009 Magnus Damm
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/bitmap.h>
  12. #include <linux/clk.h>
  13. #include <linux/completion.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/pm_runtime.h>
  25. #include <linux/spi/sh_msiof.h>
  26. #include <linux/spi/spi.h>
  27. #include <asm/unaligned.h>
  28. struct sh_msiof_chipdata {
  29. u16 tx_fifo_size;
  30. u16 rx_fifo_size;
  31. u16 master_flags;
  32. };
  33. struct sh_msiof_spi_priv {
  34. void __iomem *mapbase;
  35. struct clk *clk;
  36. struct platform_device *pdev;
  37. const struct sh_msiof_chipdata *chipdata;
  38. struct sh_msiof_spi_info *info;
  39. struct completion done;
  40. int tx_fifo_size;
  41. int rx_fifo_size;
  42. };
  43. #define TMDR1 0x00 /* Transmit Mode Register 1 */
  44. #define TMDR2 0x04 /* Transmit Mode Register 2 */
  45. #define TMDR3 0x08 /* Transmit Mode Register 3 */
  46. #define RMDR1 0x10 /* Receive Mode Register 1 */
  47. #define RMDR2 0x14 /* Receive Mode Register 2 */
  48. #define RMDR3 0x18 /* Receive Mode Register 3 */
  49. #define TSCR 0x20 /* Transmit Clock Select Register */
  50. #define RSCR 0x22 /* Receive Clock Select Register (SH, A1, APE6) */
  51. #define CTR 0x28 /* Control Register */
  52. #define FCTR 0x30 /* FIFO Control Register */
  53. #define STR 0x40 /* Status Register */
  54. #define IER 0x44 /* Interrupt Enable Register */
  55. #define TDR1 0x48 /* Transmit Control Data Register 1 (SH, A1) */
  56. #define TDR2 0x4c /* Transmit Control Data Register 2 (SH, A1) */
  57. #define TFDR 0x50 /* Transmit FIFO Data Register */
  58. #define RDR1 0x58 /* Receive Control Data Register 1 (SH, A1) */
  59. #define RDR2 0x5c /* Receive Control Data Register 2 (SH, A1) */
  60. #define RFDR 0x60 /* Receive FIFO Data Register */
  61. /* TMDR1 and RMDR1 */
  62. #define MDR1_TRMD 0x80000000 /* Transfer Mode (1 = Master mode) */
  63. #define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */
  64. #define MDR1_SYNCMD_SPI 0x20000000 /* Level mode/SPI */
  65. #define MDR1_SYNCMD_LR 0x30000000 /* L/R mode */
  66. #define MDR1_SYNCAC_SHIFT 25 /* Sync Polarity (1 = Active-low) */
  67. #define MDR1_BITLSB_SHIFT 24 /* MSB/LSB First (1 = LSB first) */
  68. #define MDR1_FLD_MASK 0x000000c0 /* Frame Sync Signal Interval (0-3) */
  69. #define MDR1_FLD_SHIFT 2
  70. #define MDR1_XXSTP 0x00000001 /* Transmission/Reception Stop on FIFO */
  71. /* TMDR1 */
  72. #define TMDR1_PCON 0x40000000 /* Transfer Signal Connection */
  73. /* TMDR2 and RMDR2 */
  74. #define MDR2_BITLEN1(i) (((i) - 1) << 24) /* Data Size (8-32 bits) */
  75. #define MDR2_WDLEN1(i) (((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */
  76. #define MDR2_GRPMASK1 0x00000001 /* Group Output Mask 1 (SH, A1) */
  77. /* TSCR and RSCR */
  78. #define SCR_BRPS_MASK 0x1f00 /* Prescaler Setting (1-32) */
  79. #define SCR_BRPS(i) (((i) - 1) << 8)
  80. #define SCR_BRDV_MASK 0x0007 /* Baud Rate Generator's Division Ratio */
  81. #define SCR_BRDV_DIV_2 0x0000
  82. #define SCR_BRDV_DIV_4 0x0001
  83. #define SCR_BRDV_DIV_8 0x0002
  84. #define SCR_BRDV_DIV_16 0x0003
  85. #define SCR_BRDV_DIV_32 0x0004
  86. #define SCR_BRDV_DIV_1 0x0007
  87. /* CTR */
  88. #define CTR_TSCKIZ_MASK 0xc0000000 /* Transmit Clock I/O Polarity Select */
  89. #define CTR_TSCKIZ_SCK 0x80000000 /* Disable SCK when TX disabled */
  90. #define CTR_TSCKIZ_POL_SHIFT 30 /* Transmit Clock Polarity */
  91. #define CTR_RSCKIZ_MASK 0x30000000 /* Receive Clock Polarity Select */
  92. #define CTR_RSCKIZ_SCK 0x20000000 /* Must match CTR_TSCKIZ_SCK */
  93. #define CTR_RSCKIZ_POL_SHIFT 28 /* Receive Clock Polarity */
  94. #define CTR_TEDG_SHIFT 27 /* Transmit Timing (1 = falling edge) */
  95. #define CTR_REDG_SHIFT 26 /* Receive Timing (1 = falling edge) */
  96. #define CTR_TXDIZ_MASK 0x00c00000 /* Pin Output When TX is Disabled */
  97. #define CTR_TXDIZ_LOW 0x00000000 /* 0 */
  98. #define CTR_TXDIZ_HIGH 0x00400000 /* 1 */
  99. #define CTR_TXDIZ_HIZ 0x00800000 /* High-impedance */
  100. #define CTR_TSCKE 0x00008000 /* Transmit Serial Clock Output Enable */
  101. #define CTR_TFSE 0x00004000 /* Transmit Frame Sync Signal Output Enable */
  102. #define CTR_TXE 0x00000200 /* Transmit Enable */
  103. #define CTR_RXE 0x00000100 /* Receive Enable */
  104. /* STR and IER */
  105. #define STR_TEOF 0x00800000 /* Frame Transmission End */
  106. #define STR_REOF 0x00000080 /* Frame Reception End */
  107. static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
  108. {
  109. switch (reg_offs) {
  110. case TSCR:
  111. case RSCR:
  112. return ioread16(p->mapbase + reg_offs);
  113. default:
  114. return ioread32(p->mapbase + reg_offs);
  115. }
  116. }
  117. static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
  118. u32 value)
  119. {
  120. switch (reg_offs) {
  121. case TSCR:
  122. case RSCR:
  123. iowrite16(value, p->mapbase + reg_offs);
  124. break;
  125. default:
  126. iowrite32(value, p->mapbase + reg_offs);
  127. break;
  128. }
  129. }
  130. static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
  131. u32 clr, u32 set)
  132. {
  133. u32 mask = clr | set;
  134. u32 data;
  135. int k;
  136. data = sh_msiof_read(p, CTR);
  137. data &= ~clr;
  138. data |= set;
  139. sh_msiof_write(p, CTR, data);
  140. for (k = 100; k > 0; k--) {
  141. if ((sh_msiof_read(p, CTR) & mask) == set)
  142. break;
  143. udelay(10);
  144. }
  145. return k > 0 ? 0 : -ETIMEDOUT;
  146. }
  147. static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
  148. {
  149. struct sh_msiof_spi_priv *p = data;
  150. /* just disable the interrupt and wake up */
  151. sh_msiof_write(p, IER, 0);
  152. complete(&p->done);
  153. return IRQ_HANDLED;
  154. }
  155. static struct {
  156. unsigned short div;
  157. unsigned short scr;
  158. } const sh_msiof_spi_clk_table[] = {
  159. { 1, SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
  160. { 2, SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
  161. { 4, SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
  162. { 8, SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
  163. { 16, SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
  164. { 32, SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
  165. { 64, SCR_BRPS(32) | SCR_BRDV_DIV_2 },
  166. { 128, SCR_BRPS(32) | SCR_BRDV_DIV_4 },
  167. { 256, SCR_BRPS(32) | SCR_BRDV_DIV_8 },
  168. { 512, SCR_BRPS(32) | SCR_BRDV_DIV_16 },
  169. { 1024, SCR_BRPS(32) | SCR_BRDV_DIV_32 },
  170. };
  171. static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
  172. unsigned long parent_rate, u32 spi_hz)
  173. {
  174. unsigned long div = 1024;
  175. size_t k;
  176. if (!WARN_ON(!spi_hz || !parent_rate))
  177. div = DIV_ROUND_UP(parent_rate, spi_hz);
  178. /* TODO: make more fine grained */
  179. for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
  180. if (sh_msiof_spi_clk_table[k].div >= div)
  181. break;
  182. }
  183. k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
  184. sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
  185. if (!(p->chipdata->master_flags & SPI_MASTER_MUST_TX))
  186. sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
  187. }
  188. static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
  189. u32 cpol, u32 cpha,
  190. u32 tx_hi_z, u32 lsb_first, u32 cs_high)
  191. {
  192. u32 tmp;
  193. int edge;
  194. /*
  195. * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
  196. * 0 0 10 10 1 1
  197. * 0 1 10 10 0 0
  198. * 1 0 11 11 0 0
  199. * 1 1 11 11 1 1
  200. */
  201. sh_msiof_write(p, FCTR, 0);
  202. tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP;
  203. tmp |= !cs_high << MDR1_SYNCAC_SHIFT;
  204. tmp |= lsb_first << MDR1_BITLSB_SHIFT;
  205. sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON);
  206. if (p->chipdata->master_flags & SPI_MASTER_MUST_TX) {
  207. /* These bits are reserved if RX needs TX */
  208. tmp &= ~0x0000ffff;
  209. }
  210. sh_msiof_write(p, RMDR1, tmp);
  211. tmp = 0;
  212. tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT;
  213. tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT;
  214. edge = cpol ^ !cpha;
  215. tmp |= edge << CTR_TEDG_SHIFT;
  216. tmp |= edge << CTR_REDG_SHIFT;
  217. tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW;
  218. sh_msiof_write(p, CTR, tmp);
  219. }
  220. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  221. const void *tx_buf, void *rx_buf,
  222. u32 bits, u32 words)
  223. {
  224. u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words);
  225. if (tx_buf || (p->chipdata->master_flags & SPI_MASTER_MUST_TX))
  226. sh_msiof_write(p, TMDR2, dr2);
  227. else
  228. sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1);
  229. if (rx_buf)
  230. sh_msiof_write(p, RMDR2, dr2);
  231. sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
  232. }
  233. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  234. {
  235. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  236. }
  237. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  238. const void *tx_buf, int words, int fs)
  239. {
  240. const u8 *buf_8 = tx_buf;
  241. int k;
  242. for (k = 0; k < words; k++)
  243. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  244. }
  245. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  246. const void *tx_buf, int words, int fs)
  247. {
  248. const u16 *buf_16 = tx_buf;
  249. int k;
  250. for (k = 0; k < words; k++)
  251. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  252. }
  253. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  254. const void *tx_buf, int words, int fs)
  255. {
  256. const u16 *buf_16 = tx_buf;
  257. int k;
  258. for (k = 0; k < words; k++)
  259. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  260. }
  261. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  262. const void *tx_buf, int words, int fs)
  263. {
  264. const u32 *buf_32 = tx_buf;
  265. int k;
  266. for (k = 0; k < words; k++)
  267. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  268. }
  269. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  270. const void *tx_buf, int words, int fs)
  271. {
  272. const u32 *buf_32 = tx_buf;
  273. int k;
  274. for (k = 0; k < words; k++)
  275. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  276. }
  277. static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
  278. const void *tx_buf, int words, int fs)
  279. {
  280. const u32 *buf_32 = tx_buf;
  281. int k;
  282. for (k = 0; k < words; k++)
  283. sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
  284. }
  285. static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
  286. const void *tx_buf, int words, int fs)
  287. {
  288. const u32 *buf_32 = tx_buf;
  289. int k;
  290. for (k = 0; k < words; k++)
  291. sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
  292. }
  293. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  294. void *rx_buf, int words, int fs)
  295. {
  296. u8 *buf_8 = rx_buf;
  297. int k;
  298. for (k = 0; k < words; k++)
  299. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  300. }
  301. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  302. void *rx_buf, int words, int fs)
  303. {
  304. u16 *buf_16 = rx_buf;
  305. int k;
  306. for (k = 0; k < words; k++)
  307. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  308. }
  309. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  310. void *rx_buf, int words, int fs)
  311. {
  312. u16 *buf_16 = rx_buf;
  313. int k;
  314. for (k = 0; k < words; k++)
  315. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  316. }
  317. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  318. void *rx_buf, int words, int fs)
  319. {
  320. u32 *buf_32 = rx_buf;
  321. int k;
  322. for (k = 0; k < words; k++)
  323. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  324. }
  325. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  326. void *rx_buf, int words, int fs)
  327. {
  328. u32 *buf_32 = rx_buf;
  329. int k;
  330. for (k = 0; k < words; k++)
  331. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  332. }
  333. static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
  334. void *rx_buf, int words, int fs)
  335. {
  336. u32 *buf_32 = rx_buf;
  337. int k;
  338. for (k = 0; k < words; k++)
  339. buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
  340. }
  341. static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
  342. void *rx_buf, int words, int fs)
  343. {
  344. u32 *buf_32 = rx_buf;
  345. int k;
  346. for (k = 0; k < words; k++)
  347. put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
  348. }
  349. static int sh_msiof_spi_setup(struct spi_device *spi)
  350. {
  351. struct device_node *np = spi->master->dev.of_node;
  352. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  353. if (!np) {
  354. /*
  355. * Use spi->controller_data for CS (same strategy as spi_gpio),
  356. * if any. otherwise let HW control CS
  357. */
  358. spi->cs_gpio = (uintptr_t)spi->controller_data;
  359. }
  360. /* Configure pins before deasserting CS */
  361. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  362. !!(spi->mode & SPI_CPHA),
  363. !!(spi->mode & SPI_3WIRE),
  364. !!(spi->mode & SPI_LSB_FIRST),
  365. !!(spi->mode & SPI_CS_HIGH));
  366. if (spi->cs_gpio >= 0)
  367. gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  368. return 0;
  369. }
  370. static int sh_msiof_prepare_message(struct spi_master *master,
  371. struct spi_message *msg)
  372. {
  373. struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
  374. const struct spi_device *spi = msg->spi;
  375. /* Configure pins before asserting CS */
  376. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  377. !!(spi->mode & SPI_CPHA),
  378. !!(spi->mode & SPI_3WIRE),
  379. !!(spi->mode & SPI_LSB_FIRST),
  380. !!(spi->mode & SPI_CS_HIGH));
  381. return 0;
  382. }
  383. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  384. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  385. const void *, int, int),
  386. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  387. void *, int, int),
  388. const void *tx_buf, void *rx_buf,
  389. int words, int bits)
  390. {
  391. int fifo_shift;
  392. int ret;
  393. /* limit maximum word transfer to rx/tx fifo size */
  394. if (tx_buf)
  395. words = min_t(int, words, p->tx_fifo_size);
  396. if (rx_buf)
  397. words = min_t(int, words, p->rx_fifo_size);
  398. /* the fifo contents need shifting */
  399. fifo_shift = 32 - bits;
  400. /* setup msiof transfer mode registers */
  401. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  402. /* write tx fifo */
  403. if (tx_buf)
  404. tx_fifo(p, tx_buf, words, fifo_shift);
  405. /* setup clock and rx/tx signals */
  406. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  407. if (rx_buf)
  408. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  409. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  410. /* start by setting frame bit */
  411. reinit_completion(&p->done);
  412. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  413. if (ret) {
  414. dev_err(&p->pdev->dev, "failed to start hardware\n");
  415. goto err;
  416. }
  417. /* wait for tx fifo to be emptied / rx fifo to be filled */
  418. wait_for_completion(&p->done);
  419. /* read rx fifo */
  420. if (rx_buf)
  421. rx_fifo(p, rx_buf, words, fifo_shift);
  422. /* clear status bits */
  423. sh_msiof_reset_str(p);
  424. /* shut down frame, rx/tx and clock signals */
  425. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  426. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  427. if (rx_buf)
  428. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  429. ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  430. if (ret) {
  431. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  432. goto err;
  433. }
  434. return words;
  435. err:
  436. sh_msiof_write(p, IER, 0);
  437. return ret;
  438. }
  439. static int sh_msiof_transfer_one(struct spi_master *master,
  440. struct spi_device *spi,
  441. struct spi_transfer *t)
  442. {
  443. struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
  444. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  445. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  446. int bits;
  447. int bytes_per_word;
  448. int bytes_done;
  449. int words;
  450. int n;
  451. bool swab;
  452. bits = t->bits_per_word;
  453. if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
  454. bits = 32;
  455. swab = true;
  456. } else {
  457. swab = false;
  458. }
  459. /* setup bytes per word and fifo read/write functions */
  460. if (bits <= 8) {
  461. bytes_per_word = 1;
  462. tx_fifo = sh_msiof_spi_write_fifo_8;
  463. rx_fifo = sh_msiof_spi_read_fifo_8;
  464. } else if (bits <= 16) {
  465. bytes_per_word = 2;
  466. if ((unsigned long)t->tx_buf & 0x01)
  467. tx_fifo = sh_msiof_spi_write_fifo_16u;
  468. else
  469. tx_fifo = sh_msiof_spi_write_fifo_16;
  470. if ((unsigned long)t->rx_buf & 0x01)
  471. rx_fifo = sh_msiof_spi_read_fifo_16u;
  472. else
  473. rx_fifo = sh_msiof_spi_read_fifo_16;
  474. } else if (swab) {
  475. bytes_per_word = 4;
  476. if ((unsigned long)t->tx_buf & 0x03)
  477. tx_fifo = sh_msiof_spi_write_fifo_s32u;
  478. else
  479. tx_fifo = sh_msiof_spi_write_fifo_s32;
  480. if ((unsigned long)t->rx_buf & 0x03)
  481. rx_fifo = sh_msiof_spi_read_fifo_s32u;
  482. else
  483. rx_fifo = sh_msiof_spi_read_fifo_s32;
  484. } else {
  485. bytes_per_word = 4;
  486. if ((unsigned long)t->tx_buf & 0x03)
  487. tx_fifo = sh_msiof_spi_write_fifo_32u;
  488. else
  489. tx_fifo = sh_msiof_spi_write_fifo_32;
  490. if ((unsigned long)t->rx_buf & 0x03)
  491. rx_fifo = sh_msiof_spi_read_fifo_32u;
  492. else
  493. rx_fifo = sh_msiof_spi_read_fifo_32;
  494. }
  495. /* setup clocks (clock already enabled in chipselect()) */
  496. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
  497. /* transfer in fifo sized chunks */
  498. words = t->len / bytes_per_word;
  499. bytes_done = 0;
  500. while (bytes_done < t->len) {
  501. void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
  502. const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
  503. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
  504. tx_buf,
  505. rx_buf,
  506. words, bits);
  507. if (n < 0)
  508. break;
  509. bytes_done += n * bytes_per_word;
  510. words -= n;
  511. }
  512. return 0;
  513. }
  514. static const struct sh_msiof_chipdata sh_data = {
  515. .tx_fifo_size = 64,
  516. .rx_fifo_size = 64,
  517. .master_flags = 0,
  518. };
  519. static const struct sh_msiof_chipdata r8a779x_data = {
  520. .tx_fifo_size = 64,
  521. .rx_fifo_size = 256,
  522. .master_flags = SPI_MASTER_MUST_TX,
  523. };
  524. static const struct of_device_id sh_msiof_match[] = {
  525. { .compatible = "renesas,sh-msiof", .data = &sh_data },
  526. { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data },
  527. { .compatible = "renesas,msiof-r8a7790", .data = &r8a779x_data },
  528. { .compatible = "renesas,msiof-r8a7791", .data = &r8a779x_data },
  529. {},
  530. };
  531. MODULE_DEVICE_TABLE(of, sh_msiof_match);
  532. #ifdef CONFIG_OF
  533. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  534. {
  535. struct sh_msiof_spi_info *info;
  536. struct device_node *np = dev->of_node;
  537. u32 num_cs = 1;
  538. info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
  539. if (!info)
  540. return NULL;
  541. /* Parse the MSIOF properties */
  542. of_property_read_u32(np, "num-cs", &num_cs);
  543. of_property_read_u32(np, "renesas,tx-fifo-size",
  544. &info->tx_fifo_override);
  545. of_property_read_u32(np, "renesas,rx-fifo-size",
  546. &info->rx_fifo_override);
  547. info->num_chipselect = num_cs;
  548. return info;
  549. }
  550. #else
  551. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  552. {
  553. return NULL;
  554. }
  555. #endif
  556. static int sh_msiof_spi_probe(struct platform_device *pdev)
  557. {
  558. struct resource *r;
  559. struct spi_master *master;
  560. const struct of_device_id *of_id;
  561. struct sh_msiof_spi_priv *p;
  562. int i;
  563. int ret;
  564. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  565. if (master == NULL) {
  566. dev_err(&pdev->dev, "failed to allocate spi master\n");
  567. return -ENOMEM;
  568. }
  569. p = spi_master_get_devdata(master);
  570. platform_set_drvdata(pdev, p);
  571. of_id = of_match_device(sh_msiof_match, &pdev->dev);
  572. if (of_id) {
  573. p->chipdata = of_id->data;
  574. p->info = sh_msiof_spi_parse_dt(&pdev->dev);
  575. } else {
  576. p->chipdata = (const void *)pdev->id_entry->driver_data;
  577. p->info = dev_get_platdata(&pdev->dev);
  578. }
  579. if (!p->info) {
  580. dev_err(&pdev->dev, "failed to obtain device info\n");
  581. ret = -ENXIO;
  582. goto err1;
  583. }
  584. init_completion(&p->done);
  585. p->clk = devm_clk_get(&pdev->dev, NULL);
  586. if (IS_ERR(p->clk)) {
  587. dev_err(&pdev->dev, "cannot get clock\n");
  588. ret = PTR_ERR(p->clk);
  589. goto err1;
  590. }
  591. i = platform_get_irq(pdev, 0);
  592. if (i < 0) {
  593. dev_err(&pdev->dev, "cannot get platform IRQ\n");
  594. ret = -ENOENT;
  595. goto err1;
  596. }
  597. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  598. p->mapbase = devm_ioremap_resource(&pdev->dev, r);
  599. if (IS_ERR(p->mapbase)) {
  600. ret = PTR_ERR(p->mapbase);
  601. goto err1;
  602. }
  603. ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
  604. dev_name(&pdev->dev), p);
  605. if (ret) {
  606. dev_err(&pdev->dev, "unable to request irq\n");
  607. goto err1;
  608. }
  609. p->pdev = pdev;
  610. pm_runtime_enable(&pdev->dev);
  611. /* Platform data may override FIFO sizes */
  612. p->tx_fifo_size = p->chipdata->tx_fifo_size;
  613. p->rx_fifo_size = p->chipdata->rx_fifo_size;
  614. if (p->info->tx_fifo_override)
  615. p->tx_fifo_size = p->info->tx_fifo_override;
  616. if (p->info->rx_fifo_override)
  617. p->rx_fifo_size = p->info->rx_fifo_override;
  618. /* init master code */
  619. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  620. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  621. master->flags = p->chipdata->master_flags;
  622. master->bus_num = pdev->id;
  623. master->dev.of_node = pdev->dev.of_node;
  624. master->num_chipselect = p->info->num_chipselect;
  625. master->setup = sh_msiof_spi_setup;
  626. master->prepare_message = sh_msiof_prepare_message;
  627. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
  628. master->auto_runtime_pm = true;
  629. master->transfer_one = sh_msiof_transfer_one;
  630. ret = devm_spi_register_master(&pdev->dev, master);
  631. if (ret < 0) {
  632. dev_err(&pdev->dev, "spi_register_master error.\n");
  633. goto err2;
  634. }
  635. return 0;
  636. err2:
  637. pm_runtime_disable(&pdev->dev);
  638. err1:
  639. spi_master_put(master);
  640. return ret;
  641. }
  642. static int sh_msiof_spi_remove(struct platform_device *pdev)
  643. {
  644. pm_runtime_disable(&pdev->dev);
  645. return 0;
  646. }
  647. static struct platform_device_id spi_driver_ids[] = {
  648. { "spi_sh_msiof", (kernel_ulong_t)&sh_data },
  649. { "spi_r8a7790_msiof", (kernel_ulong_t)&r8a779x_data },
  650. { "spi_r8a7791_msiof", (kernel_ulong_t)&r8a779x_data },
  651. {},
  652. };
  653. MODULE_DEVICE_TABLE(platform, spi_driver_ids);
  654. static struct platform_driver sh_msiof_spi_drv = {
  655. .probe = sh_msiof_spi_probe,
  656. .remove = sh_msiof_spi_remove,
  657. .id_table = spi_driver_ids,
  658. .driver = {
  659. .name = "spi_sh_msiof",
  660. .owner = THIS_MODULE,
  661. .of_match_table = of_match_ptr(sh_msiof_match),
  662. },
  663. };
  664. module_platform_driver(sh_msiof_spi_drv);
  665. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  666. MODULE_AUTHOR("Magnus Damm");
  667. MODULE_LICENSE("GPL v2");
  668. MODULE_ALIAS("platform:spi_sh_msiof");