spi-rspi.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /*
  2. * SH RSPI driver
  3. *
  4. * Copyright (C) 2012, 2013 Renesas Solutions Corp.
  5. * Copyright (C) 2014 Glider bvba
  6. *
  7. * Based on spi-sh.c:
  8. * Copyright (C) 2011 Renesas Solutions Corp.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/sched.h>
  22. #include <linux/errno.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/io.h>
  26. #include <linux/clk.h>
  27. #include <linux/dmaengine.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/of_device.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/sh_dma.h>
  32. #include <linux/spi/spi.h>
  33. #include <linux/spi/rspi.h>
  34. #define RSPI_SPCR 0x00 /* Control Register */
  35. #define RSPI_SSLP 0x01 /* Slave Select Polarity Register */
  36. #define RSPI_SPPCR 0x02 /* Pin Control Register */
  37. #define RSPI_SPSR 0x03 /* Status Register */
  38. #define RSPI_SPDR 0x04 /* Data Register */
  39. #define RSPI_SPSCR 0x08 /* Sequence Control Register */
  40. #define RSPI_SPSSR 0x09 /* Sequence Status Register */
  41. #define RSPI_SPBR 0x0a /* Bit Rate Register */
  42. #define RSPI_SPDCR 0x0b /* Data Control Register */
  43. #define RSPI_SPCKD 0x0c /* Clock Delay Register */
  44. #define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */
  45. #define RSPI_SPND 0x0e /* Next-Access Delay Register */
  46. #define RSPI_SPCR2 0x0f /* Control Register 2 (SH only) */
  47. #define RSPI_SPCMD0 0x10 /* Command Register 0 */
  48. #define RSPI_SPCMD1 0x12 /* Command Register 1 */
  49. #define RSPI_SPCMD2 0x14 /* Command Register 2 */
  50. #define RSPI_SPCMD3 0x16 /* Command Register 3 */
  51. #define RSPI_SPCMD4 0x18 /* Command Register 4 */
  52. #define RSPI_SPCMD5 0x1a /* Command Register 5 */
  53. #define RSPI_SPCMD6 0x1c /* Command Register 6 */
  54. #define RSPI_SPCMD7 0x1e /* Command Register 7 */
  55. #define RSPI_SPCMD(i) (RSPI_SPCMD0 + (i) * 2)
  56. #define RSPI_NUM_SPCMD 8
  57. #define RSPI_RZ_NUM_SPCMD 4
  58. #define QSPI_NUM_SPCMD 4
  59. /* RSPI on RZ only */
  60. #define RSPI_SPBFCR 0x20 /* Buffer Control Register */
  61. #define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */
  62. /* QSPI only */
  63. #define QSPI_SPBFCR 0x18 /* Buffer Control Register */
  64. #define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */
  65. #define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */
  66. #define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */
  67. #define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */
  68. #define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */
  69. #define QSPI_SPBMUL(i) (QSPI_SPBMUL0 + (i) * 4)
  70. /* SPCR - Control Register */
  71. #define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */
  72. #define SPCR_SPE 0x40 /* Function Enable */
  73. #define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */
  74. #define SPCR_SPEIE 0x10 /* Error Interrupt Enable */
  75. #define SPCR_MSTR 0x08 /* Master/Slave Mode Select */
  76. #define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */
  77. /* RSPI on SH only */
  78. #define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */
  79. #define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */
  80. /* QSPI on R-Car Gen2 only */
  81. #define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */
  82. #define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */
  83. /* SSLP - Slave Select Polarity Register */
  84. #define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */
  85. #define SSLP_SSL0P 0x01 /* SSL0 Signal Polarity Setting */
  86. /* SPPCR - Pin Control Register */
  87. #define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */
  88. #define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */
  89. #define SPPCR_SPOM 0x04
  90. #define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */
  91. #define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */
  92. #define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */
  93. #define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */
  94. /* SPSR - Status Register */
  95. #define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */
  96. #define SPSR_TEND 0x40 /* Transmit End */
  97. #define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */
  98. #define SPSR_PERF 0x08 /* Parity Error Flag */
  99. #define SPSR_MODF 0x04 /* Mode Fault Error Flag */
  100. #define SPSR_IDLNF 0x02 /* RSPI Idle Flag */
  101. #define SPSR_OVRF 0x01 /* Overrun Error Flag (RSPI only) */
  102. /* SPSCR - Sequence Control Register */
  103. #define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */
  104. /* SPSSR - Sequence Status Register */
  105. #define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */
  106. #define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */
  107. /* SPDCR - Data Control Register */
  108. #define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */
  109. #define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */
  110. #define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */
  111. #define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0)
  112. #define SPDCR_SPLWORD SPDCR_SPLW1
  113. #define SPDCR_SPLBYTE SPDCR_SPLW0
  114. #define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */
  115. #define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select (SH) */
  116. #define SPDCR_SLSEL1 0x08
  117. #define SPDCR_SLSEL0 0x04
  118. #define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select (SH) */
  119. #define SPDCR_SPFC1 0x02
  120. #define SPDCR_SPFC0 0x01
  121. #define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) (SH) */
  122. /* SPCKD - Clock Delay Register */
  123. #define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */
  124. /* SSLND - Slave Select Negation Delay Register */
  125. #define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */
  126. /* SPND - Next-Access Delay Register */
  127. #define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */
  128. /* SPCR2 - Control Register 2 */
  129. #define SPCR2_PTE 0x08 /* Parity Self-Test Enable */
  130. #define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */
  131. #define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */
  132. #define SPCR2_SPPE 0x01 /* Parity Enable */
  133. /* SPCMDn - Command Registers */
  134. #define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */
  135. #define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */
  136. #define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */
  137. #define SPCMD_LSBF 0x1000 /* LSB First */
  138. #define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */
  139. #define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK)
  140. #define SPCMD_SPB_8BIT 0x0000 /* QSPI only */
  141. #define SPCMD_SPB_16BIT 0x0100
  142. #define SPCMD_SPB_20BIT 0x0000
  143. #define SPCMD_SPB_24BIT 0x0100
  144. #define SPCMD_SPB_32BIT 0x0200
  145. #define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */
  146. #define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */
  147. #define SPCMD_SPIMOD1 0x0040
  148. #define SPCMD_SPIMOD0 0x0020
  149. #define SPCMD_SPIMOD_SINGLE 0
  150. #define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0
  151. #define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1
  152. #define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */
  153. #define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */
  154. #define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */
  155. #define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */
  156. #define SPCMD_CPHA 0x0001 /* Clock Phase Setting */
  157. /* SPBFCR - Buffer Control Register */
  158. #define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset */
  159. #define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset */
  160. #define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */
  161. #define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */
  162. /* QSPI on R-Car Gen2 */
  163. #define SPBFCR_TXTRG_1B 0x00 /* 31 bytes (1 byte available) */
  164. #define SPBFCR_TXTRG_32B 0x30 /* 0 byte (32 bytes available) */
  165. #define SPBFCR_RXTRG_1B 0x00 /* 1 byte (31 bytes available) */
  166. #define SPBFCR_RXTRG_32B 0x07 /* 32 bytes (0 byte available) */
  167. #define QSPI_BUFFER_SIZE 32u
  168. struct rspi_data {
  169. void __iomem *addr;
  170. u32 max_speed_hz;
  171. struct spi_master *master;
  172. wait_queue_head_t wait;
  173. struct clk *clk;
  174. u16 spcmd;
  175. u8 spsr;
  176. u8 sppcr;
  177. int rx_irq, tx_irq;
  178. const struct spi_ops *ops;
  179. unsigned dma_callbacked:1;
  180. unsigned byte_access:1;
  181. };
  182. static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset)
  183. {
  184. iowrite8(data, rspi->addr + offset);
  185. }
  186. static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset)
  187. {
  188. iowrite16(data, rspi->addr + offset);
  189. }
  190. static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset)
  191. {
  192. iowrite32(data, rspi->addr + offset);
  193. }
  194. static u8 rspi_read8(const struct rspi_data *rspi, u16 offset)
  195. {
  196. return ioread8(rspi->addr + offset);
  197. }
  198. static u16 rspi_read16(const struct rspi_data *rspi, u16 offset)
  199. {
  200. return ioread16(rspi->addr + offset);
  201. }
  202. static void rspi_write_data(const struct rspi_data *rspi, u16 data)
  203. {
  204. if (rspi->byte_access)
  205. rspi_write8(rspi, data, RSPI_SPDR);
  206. else /* 16 bit */
  207. rspi_write16(rspi, data, RSPI_SPDR);
  208. }
  209. static u16 rspi_read_data(const struct rspi_data *rspi)
  210. {
  211. if (rspi->byte_access)
  212. return rspi_read8(rspi, RSPI_SPDR);
  213. else /* 16 bit */
  214. return rspi_read16(rspi, RSPI_SPDR);
  215. }
  216. /* optional functions */
  217. struct spi_ops {
  218. int (*set_config_register)(struct rspi_data *rspi, int access_size);
  219. int (*transfer_one)(struct spi_master *master, struct spi_device *spi,
  220. struct spi_transfer *xfer);
  221. u16 mode_bits;
  222. u16 flags;
  223. u16 fifo_size;
  224. };
  225. /*
  226. * functions for RSPI on legacy SH
  227. */
  228. static int rspi_set_config_register(struct rspi_data *rspi, int access_size)
  229. {
  230. int spbr;
  231. /* Sets output mode, MOSI signal, and (optionally) loopback */
  232. rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
  233. /* Sets transfer bit rate */
  234. spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk),
  235. 2 * rspi->max_speed_hz) - 1;
  236. rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
  237. /* Disable dummy transmission, set 16-bit word access, 1 frame */
  238. rspi_write8(rspi, 0, RSPI_SPDCR);
  239. rspi->byte_access = 0;
  240. /* Sets RSPCK, SSL, next-access delay value */
  241. rspi_write8(rspi, 0x00, RSPI_SPCKD);
  242. rspi_write8(rspi, 0x00, RSPI_SSLND);
  243. rspi_write8(rspi, 0x00, RSPI_SPND);
  244. /* Sets parity, interrupt mask */
  245. rspi_write8(rspi, 0x00, RSPI_SPCR2);
  246. /* Sets SPCMD */
  247. rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
  248. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  249. /* Sets RSPI mode */
  250. rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
  251. return 0;
  252. }
  253. /*
  254. * functions for RSPI on RZ
  255. */
  256. static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
  257. {
  258. int spbr;
  259. /* Sets output mode, MOSI signal, and (optionally) loopback */
  260. rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
  261. /* Sets transfer bit rate */
  262. spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk),
  263. 2 * rspi->max_speed_hz) - 1;
  264. rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
  265. /* Disable dummy transmission, set byte access */
  266. rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR);
  267. rspi->byte_access = 1;
  268. /* Sets RSPCK, SSL, next-access delay value */
  269. rspi_write8(rspi, 0x00, RSPI_SPCKD);
  270. rspi_write8(rspi, 0x00, RSPI_SSLND);
  271. rspi_write8(rspi, 0x00, RSPI_SPND);
  272. /* Sets SPCMD */
  273. rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
  274. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  275. /* Sets RSPI mode */
  276. rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
  277. return 0;
  278. }
  279. /*
  280. * functions for QSPI
  281. */
  282. static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
  283. {
  284. int spbr;
  285. /* Sets output mode, MOSI signal, and (optionally) loopback */
  286. rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
  287. /* Sets transfer bit rate */
  288. spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->max_speed_hz);
  289. rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
  290. /* Disable dummy transmission, set byte access */
  291. rspi_write8(rspi, 0, RSPI_SPDCR);
  292. rspi->byte_access = 1;
  293. /* Sets RSPCK, SSL, next-access delay value */
  294. rspi_write8(rspi, 0x00, RSPI_SPCKD);
  295. rspi_write8(rspi, 0x00, RSPI_SSLND);
  296. rspi_write8(rspi, 0x00, RSPI_SPND);
  297. /* Data Length Setting */
  298. if (access_size == 8)
  299. rspi->spcmd |= SPCMD_SPB_8BIT;
  300. else if (access_size == 16)
  301. rspi->spcmd |= SPCMD_SPB_16BIT;
  302. else
  303. rspi->spcmd |= SPCMD_SPB_32BIT;
  304. rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
  305. /* Resets transfer data length */
  306. rspi_write32(rspi, 0, QSPI_SPBMUL0);
  307. /* Resets transmit and receive buffer */
  308. rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
  309. /* Sets buffer to allow normal operation */
  310. rspi_write8(rspi, 0x00, QSPI_SPBFCR);
  311. /* Sets SPCMD */
  312. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  313. /* Enables SPI function in master mode */
  314. rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
  315. return 0;
  316. }
  317. static void qspi_update(const struct rspi_data *rspi, u8 mask, u8 val, u8 reg)
  318. {
  319. u8 data;
  320. data = rspi_read8(rspi, reg);
  321. data &= ~mask;
  322. data |= (val & mask);
  323. rspi_write8(rspi, data, reg);
  324. }
  325. static int qspi_set_send_trigger(struct rspi_data *rspi, unsigned int len)
  326. {
  327. unsigned int n;
  328. n = min(len, QSPI_BUFFER_SIZE);
  329. if (len >= QSPI_BUFFER_SIZE) {
  330. /* sets triggering number to 32 bytes */
  331. qspi_update(rspi, SPBFCR_TXTRG_MASK,
  332. SPBFCR_TXTRG_32B, QSPI_SPBFCR);
  333. } else {
  334. /* sets triggering number to 1 byte */
  335. qspi_update(rspi, SPBFCR_TXTRG_MASK,
  336. SPBFCR_TXTRG_1B, QSPI_SPBFCR);
  337. }
  338. return n;
  339. }
  340. static void qspi_set_receive_trigger(struct rspi_data *rspi, unsigned int len)
  341. {
  342. unsigned int n;
  343. n = min(len, QSPI_BUFFER_SIZE);
  344. if (len >= QSPI_BUFFER_SIZE) {
  345. /* sets triggering number to 32 bytes */
  346. qspi_update(rspi, SPBFCR_RXTRG_MASK,
  347. SPBFCR_RXTRG_32B, QSPI_SPBFCR);
  348. } else {
  349. /* sets triggering number to 1 byte */
  350. qspi_update(rspi, SPBFCR_RXTRG_MASK,
  351. SPBFCR_RXTRG_1B, QSPI_SPBFCR);
  352. }
  353. }
  354. #define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
  355. static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
  356. {
  357. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
  358. }
  359. static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
  360. {
  361. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
  362. }
  363. static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
  364. u8 enable_bit)
  365. {
  366. int ret;
  367. rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
  368. if (rspi->spsr & wait_mask)
  369. return 0;
  370. rspi_enable_irq(rspi, enable_bit);
  371. ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
  372. if (ret == 0 && !(rspi->spsr & wait_mask))
  373. return -ETIMEDOUT;
  374. return 0;
  375. }
  376. static inline int rspi_wait_for_tx_empty(struct rspi_data *rspi)
  377. {
  378. return rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
  379. }
  380. static inline int rspi_wait_for_rx_full(struct rspi_data *rspi)
  381. {
  382. return rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE);
  383. }
  384. static int rspi_data_out(struct rspi_data *rspi, u8 data)
  385. {
  386. int error = rspi_wait_for_tx_empty(rspi);
  387. if (error < 0) {
  388. dev_err(&rspi->master->dev, "transmit timeout\n");
  389. return error;
  390. }
  391. rspi_write_data(rspi, data);
  392. return 0;
  393. }
  394. static int rspi_data_in(struct rspi_data *rspi)
  395. {
  396. int error;
  397. u8 data;
  398. error = rspi_wait_for_rx_full(rspi);
  399. if (error < 0) {
  400. dev_err(&rspi->master->dev, "receive timeout\n");
  401. return error;
  402. }
  403. data = rspi_read_data(rspi);
  404. return data;
  405. }
  406. static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx,
  407. unsigned int n)
  408. {
  409. while (n-- > 0) {
  410. if (tx) {
  411. int ret = rspi_data_out(rspi, *tx++);
  412. if (ret < 0)
  413. return ret;
  414. }
  415. if (rx) {
  416. int ret = rspi_data_in(rspi);
  417. if (ret < 0)
  418. return ret;
  419. *rx++ = ret;
  420. }
  421. }
  422. return 0;
  423. }
  424. static void rspi_dma_complete(void *arg)
  425. {
  426. struct rspi_data *rspi = arg;
  427. rspi->dma_callbacked = 1;
  428. wake_up_interruptible(&rspi->wait);
  429. }
  430. static int rspi_dma_transfer(struct rspi_data *rspi, struct sg_table *tx,
  431. struct sg_table *rx)
  432. {
  433. struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
  434. u8 irq_mask = 0;
  435. unsigned int other_irq = 0;
  436. dma_cookie_t cookie;
  437. int ret;
  438. /* First prepare and submit the DMA request(s), as this may fail */
  439. if (rx) {
  440. desc_rx = dmaengine_prep_slave_sg(rspi->master->dma_rx,
  441. rx->sgl, rx->nents, DMA_FROM_DEVICE,
  442. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  443. if (!desc_rx) {
  444. ret = -EAGAIN;
  445. goto no_dma_rx;
  446. }
  447. desc_rx->callback = rspi_dma_complete;
  448. desc_rx->callback_param = rspi;
  449. cookie = dmaengine_submit(desc_rx);
  450. if (dma_submit_error(cookie)) {
  451. ret = cookie;
  452. goto no_dma_rx;
  453. }
  454. irq_mask |= SPCR_SPRIE;
  455. }
  456. if (tx) {
  457. desc_tx = dmaengine_prep_slave_sg(rspi->master->dma_tx,
  458. tx->sgl, tx->nents, DMA_TO_DEVICE,
  459. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  460. if (!desc_tx) {
  461. ret = -EAGAIN;
  462. goto no_dma_tx;
  463. }
  464. if (rx) {
  465. /* No callback */
  466. desc_tx->callback = NULL;
  467. } else {
  468. desc_tx->callback = rspi_dma_complete;
  469. desc_tx->callback_param = rspi;
  470. }
  471. cookie = dmaengine_submit(desc_tx);
  472. if (dma_submit_error(cookie)) {
  473. ret = cookie;
  474. goto no_dma_tx;
  475. }
  476. irq_mask |= SPCR_SPTIE;
  477. }
  478. /*
  479. * DMAC needs SPxIE, but if SPxIE is set, the IRQ routine will be
  480. * called. So, this driver disables the IRQ while DMA transfer.
  481. */
  482. if (tx)
  483. disable_irq(other_irq = rspi->tx_irq);
  484. if (rx && rspi->rx_irq != other_irq)
  485. disable_irq(rspi->rx_irq);
  486. rspi_enable_irq(rspi, irq_mask);
  487. rspi->dma_callbacked = 0;
  488. /* Now start DMA */
  489. if (rx)
  490. dma_async_issue_pending(rspi->master->dma_rx);
  491. if (tx)
  492. dma_async_issue_pending(rspi->master->dma_tx);
  493. ret = wait_event_interruptible_timeout(rspi->wait,
  494. rspi->dma_callbacked, HZ);
  495. if (ret > 0 && rspi->dma_callbacked)
  496. ret = 0;
  497. else if (!ret) {
  498. dev_err(&rspi->master->dev, "DMA timeout\n");
  499. ret = -ETIMEDOUT;
  500. if (tx)
  501. dmaengine_terminate_all(rspi->master->dma_tx);
  502. if (rx)
  503. dmaengine_terminate_all(rspi->master->dma_rx);
  504. }
  505. rspi_disable_irq(rspi, irq_mask);
  506. if (tx)
  507. enable_irq(rspi->tx_irq);
  508. if (rx && rspi->rx_irq != other_irq)
  509. enable_irq(rspi->rx_irq);
  510. return ret;
  511. no_dma_tx:
  512. if (rx)
  513. dmaengine_terminate_all(rspi->master->dma_rx);
  514. no_dma_rx:
  515. if (ret == -EAGAIN) {
  516. pr_warn_once("%s %s: DMA not available, falling back to PIO\n",
  517. dev_driver_string(&rspi->master->dev),
  518. dev_name(&rspi->master->dev));
  519. }
  520. return ret;
  521. }
  522. static void rspi_receive_init(const struct rspi_data *rspi)
  523. {
  524. u8 spsr;
  525. spsr = rspi_read8(rspi, RSPI_SPSR);
  526. if (spsr & SPSR_SPRF)
  527. rspi_read_data(rspi); /* dummy read */
  528. if (spsr & SPSR_OVRF)
  529. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
  530. RSPI_SPSR);
  531. }
  532. static void rspi_rz_receive_init(const struct rspi_data *rspi)
  533. {
  534. rspi_receive_init(rspi);
  535. rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR);
  536. rspi_write8(rspi, 0, RSPI_SPBFCR);
  537. }
  538. static void qspi_receive_init(const struct rspi_data *rspi)
  539. {
  540. u8 spsr;
  541. spsr = rspi_read8(rspi, RSPI_SPSR);
  542. if (spsr & SPSR_SPRF)
  543. rspi_read_data(rspi); /* dummy read */
  544. rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
  545. rspi_write8(rspi, 0, QSPI_SPBFCR);
  546. }
  547. static bool __rspi_can_dma(const struct rspi_data *rspi,
  548. const struct spi_transfer *xfer)
  549. {
  550. return xfer->len > rspi->ops->fifo_size;
  551. }
  552. static bool rspi_can_dma(struct spi_master *master, struct spi_device *spi,
  553. struct spi_transfer *xfer)
  554. {
  555. struct rspi_data *rspi = spi_master_get_devdata(master);
  556. return __rspi_can_dma(rspi, xfer);
  557. }
  558. static int rspi_dma_check_then_transfer(struct rspi_data *rspi,
  559. struct spi_transfer *xfer)
  560. {
  561. if (!rspi->master->can_dma || !__rspi_can_dma(rspi, xfer))
  562. return -EAGAIN;
  563. /* rx_buf can be NULL on RSPI on SH in TX-only Mode */
  564. return rspi_dma_transfer(rspi, &xfer->tx_sg,
  565. xfer->rx_buf ? &xfer->rx_sg : NULL);
  566. }
  567. static int rspi_common_transfer(struct rspi_data *rspi,
  568. struct spi_transfer *xfer)
  569. {
  570. int ret;
  571. ret = rspi_dma_check_then_transfer(rspi, xfer);
  572. if (ret != -EAGAIN)
  573. return ret;
  574. ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
  575. if (ret < 0)
  576. return ret;
  577. /* Wait for the last transmission */
  578. rspi_wait_for_tx_empty(rspi);
  579. return 0;
  580. }
  581. static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
  582. struct spi_transfer *xfer)
  583. {
  584. struct rspi_data *rspi = spi_master_get_devdata(master);
  585. u8 spcr;
  586. spcr = rspi_read8(rspi, RSPI_SPCR);
  587. if (xfer->rx_buf) {
  588. rspi_receive_init(rspi);
  589. spcr &= ~SPCR_TXMD;
  590. } else {
  591. spcr |= SPCR_TXMD;
  592. }
  593. rspi_write8(rspi, spcr, RSPI_SPCR);
  594. return rspi_common_transfer(rspi, xfer);
  595. }
  596. static int rspi_rz_transfer_one(struct spi_master *master,
  597. struct spi_device *spi,
  598. struct spi_transfer *xfer)
  599. {
  600. struct rspi_data *rspi = spi_master_get_devdata(master);
  601. rspi_rz_receive_init(rspi);
  602. return rspi_common_transfer(rspi, xfer);
  603. }
  604. static int qspi_trigger_transfer_out_in(struct rspi_data *rspi, const u8 *tx,
  605. u8 *rx, unsigned int len)
  606. {
  607. int i, n, ret;
  608. int error;
  609. while (len > 0) {
  610. n = qspi_set_send_trigger(rspi, len);
  611. qspi_set_receive_trigger(rspi, len);
  612. if (n == QSPI_BUFFER_SIZE) {
  613. error = rspi_wait_for_tx_empty(rspi);
  614. if (error < 0) {
  615. dev_err(&rspi->master->dev, "transmit timeout\n");
  616. return error;
  617. }
  618. for (i = 0; i < n; i++)
  619. rspi_write_data(rspi, *tx++);
  620. error = rspi_wait_for_rx_full(rspi);
  621. if (error < 0) {
  622. dev_err(&rspi->master->dev, "receive timeout\n");
  623. return error;
  624. }
  625. for (i = 0; i < n; i++)
  626. *rx++ = rspi_read_data(rspi);
  627. } else {
  628. ret = rspi_pio_transfer(rspi, tx, rx, n);
  629. if (ret < 0)
  630. return ret;
  631. }
  632. len -= n;
  633. }
  634. return 0;
  635. }
  636. static int qspi_transfer_out_in(struct rspi_data *rspi,
  637. struct spi_transfer *xfer)
  638. {
  639. int ret;
  640. qspi_receive_init(rspi);
  641. ret = rspi_dma_check_then_transfer(rspi, xfer);
  642. if (ret != -EAGAIN)
  643. return ret;
  644. return qspi_trigger_transfer_out_in(rspi, xfer->tx_buf,
  645. xfer->rx_buf, xfer->len);
  646. }
  647. static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
  648. {
  649. int ret;
  650. if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
  651. ret = rspi_dma_transfer(rspi, &xfer->tx_sg, NULL);
  652. if (ret != -EAGAIN)
  653. return ret;
  654. }
  655. ret = rspi_pio_transfer(rspi, xfer->tx_buf, NULL, xfer->len);
  656. if (ret < 0)
  657. return ret;
  658. /* Wait for the last transmission */
  659. rspi_wait_for_tx_empty(rspi);
  660. return 0;
  661. }
  662. static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
  663. {
  664. if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
  665. int ret = rspi_dma_transfer(rspi, NULL, &xfer->rx_sg);
  666. if (ret != -EAGAIN)
  667. return ret;
  668. }
  669. return rspi_pio_transfer(rspi, NULL, xfer->rx_buf, xfer->len);
  670. }
  671. static int qspi_transfer_one(struct spi_master *master, struct spi_device *spi,
  672. struct spi_transfer *xfer)
  673. {
  674. struct rspi_data *rspi = spi_master_get_devdata(master);
  675. if (spi->mode & SPI_LOOP) {
  676. return qspi_transfer_out_in(rspi, xfer);
  677. } else if (xfer->tx_nbits > SPI_NBITS_SINGLE) {
  678. /* Quad or Dual SPI Write */
  679. return qspi_transfer_out(rspi, xfer);
  680. } else if (xfer->rx_nbits > SPI_NBITS_SINGLE) {
  681. /* Quad or Dual SPI Read */
  682. return qspi_transfer_in(rspi, xfer);
  683. } else {
  684. /* Single SPI Transfer */
  685. return qspi_transfer_out_in(rspi, xfer);
  686. }
  687. }
  688. static int rspi_setup(struct spi_device *spi)
  689. {
  690. struct rspi_data *rspi = spi_master_get_devdata(spi->master);
  691. rspi->max_speed_hz = spi->max_speed_hz;
  692. rspi->spcmd = SPCMD_SSLKP;
  693. if (spi->mode & SPI_CPOL)
  694. rspi->spcmd |= SPCMD_CPOL;
  695. if (spi->mode & SPI_CPHA)
  696. rspi->spcmd |= SPCMD_CPHA;
  697. /* CMOS output mode and MOSI signal from previous transfer */
  698. rspi->sppcr = 0;
  699. if (spi->mode & SPI_LOOP)
  700. rspi->sppcr |= SPPCR_SPLP;
  701. set_config_register(rspi, 8);
  702. return 0;
  703. }
  704. static u16 qspi_transfer_mode(const struct spi_transfer *xfer)
  705. {
  706. if (xfer->tx_buf)
  707. switch (xfer->tx_nbits) {
  708. case SPI_NBITS_QUAD:
  709. return SPCMD_SPIMOD_QUAD;
  710. case SPI_NBITS_DUAL:
  711. return SPCMD_SPIMOD_DUAL;
  712. default:
  713. return 0;
  714. }
  715. if (xfer->rx_buf)
  716. switch (xfer->rx_nbits) {
  717. case SPI_NBITS_QUAD:
  718. return SPCMD_SPIMOD_QUAD | SPCMD_SPRW;
  719. case SPI_NBITS_DUAL:
  720. return SPCMD_SPIMOD_DUAL | SPCMD_SPRW;
  721. default:
  722. return 0;
  723. }
  724. return 0;
  725. }
  726. static int qspi_setup_sequencer(struct rspi_data *rspi,
  727. const struct spi_message *msg)
  728. {
  729. const struct spi_transfer *xfer;
  730. unsigned int i = 0, len = 0;
  731. u16 current_mode = 0xffff, mode;
  732. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  733. mode = qspi_transfer_mode(xfer);
  734. if (mode == current_mode) {
  735. len += xfer->len;
  736. continue;
  737. }
  738. /* Transfer mode change */
  739. if (i) {
  740. /* Set transfer data length of previous transfer */
  741. rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
  742. }
  743. if (i >= QSPI_NUM_SPCMD) {
  744. dev_err(&msg->spi->dev,
  745. "Too many different transfer modes");
  746. return -EINVAL;
  747. }
  748. /* Program transfer mode for this transfer */
  749. rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i));
  750. current_mode = mode;
  751. len = xfer->len;
  752. i++;
  753. }
  754. if (i) {
  755. /* Set final transfer data length and sequence length */
  756. rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
  757. rspi_write8(rspi, i - 1, RSPI_SPSCR);
  758. }
  759. return 0;
  760. }
  761. static int rspi_prepare_message(struct spi_master *master,
  762. struct spi_message *msg)
  763. {
  764. struct rspi_data *rspi = spi_master_get_devdata(master);
  765. int ret;
  766. if (msg->spi->mode &
  767. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) {
  768. /* Setup sequencer for messages with multiple transfer modes */
  769. ret = qspi_setup_sequencer(rspi, msg);
  770. if (ret < 0)
  771. return ret;
  772. }
  773. /* Enable SPI function in master mode */
  774. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
  775. return 0;
  776. }
  777. static int rspi_unprepare_message(struct spi_master *master,
  778. struct spi_message *msg)
  779. {
  780. struct rspi_data *rspi = spi_master_get_devdata(master);
  781. /* Disable SPI function */
  782. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
  783. /* Reset sequencer for Single SPI Transfers */
  784. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  785. rspi_write8(rspi, 0, RSPI_SPSCR);
  786. return 0;
  787. }
  788. static irqreturn_t rspi_irq_mux(int irq, void *_sr)
  789. {
  790. struct rspi_data *rspi = _sr;
  791. u8 spsr;
  792. irqreturn_t ret = IRQ_NONE;
  793. u8 disable_irq = 0;
  794. rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
  795. if (spsr & SPSR_SPRF)
  796. disable_irq |= SPCR_SPRIE;
  797. if (spsr & SPSR_SPTEF)
  798. disable_irq |= SPCR_SPTIE;
  799. if (disable_irq) {
  800. ret = IRQ_HANDLED;
  801. rspi_disable_irq(rspi, disable_irq);
  802. wake_up(&rspi->wait);
  803. }
  804. return ret;
  805. }
  806. static irqreturn_t rspi_irq_rx(int irq, void *_sr)
  807. {
  808. struct rspi_data *rspi = _sr;
  809. u8 spsr;
  810. rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
  811. if (spsr & SPSR_SPRF) {
  812. rspi_disable_irq(rspi, SPCR_SPRIE);
  813. wake_up(&rspi->wait);
  814. return IRQ_HANDLED;
  815. }
  816. return 0;
  817. }
  818. static irqreturn_t rspi_irq_tx(int irq, void *_sr)
  819. {
  820. struct rspi_data *rspi = _sr;
  821. u8 spsr;
  822. rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
  823. if (spsr & SPSR_SPTEF) {
  824. rspi_disable_irq(rspi, SPCR_SPTIE);
  825. wake_up(&rspi->wait);
  826. return IRQ_HANDLED;
  827. }
  828. return 0;
  829. }
  830. static struct dma_chan *rspi_request_dma_chan(struct device *dev,
  831. enum dma_transfer_direction dir,
  832. unsigned int id,
  833. dma_addr_t port_addr)
  834. {
  835. dma_cap_mask_t mask;
  836. struct dma_chan *chan;
  837. struct dma_slave_config cfg;
  838. int ret;
  839. dma_cap_zero(mask);
  840. dma_cap_set(DMA_SLAVE, mask);
  841. chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  842. (void *)(unsigned long)id, dev,
  843. dir == DMA_MEM_TO_DEV ? "tx" : "rx");
  844. if (!chan) {
  845. dev_warn(dev, "dma_request_slave_channel_compat failed\n");
  846. return NULL;
  847. }
  848. memset(&cfg, 0, sizeof(cfg));
  849. cfg.direction = dir;
  850. if (dir == DMA_MEM_TO_DEV) {
  851. cfg.dst_addr = port_addr;
  852. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  853. } else {
  854. cfg.src_addr = port_addr;
  855. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  856. }
  857. ret = dmaengine_slave_config(chan, &cfg);
  858. if (ret) {
  859. dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
  860. dma_release_channel(chan);
  861. return NULL;
  862. }
  863. return chan;
  864. }
  865. static int rspi_request_dma(struct device *dev, struct spi_master *master,
  866. const struct resource *res)
  867. {
  868. const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev);
  869. unsigned int dma_tx_id, dma_rx_id;
  870. if (dev->of_node) {
  871. /* In the OF case we will get the slave IDs from the DT */
  872. dma_tx_id = 0;
  873. dma_rx_id = 0;
  874. } else if (rspi_pd && rspi_pd->dma_tx_id && rspi_pd->dma_rx_id) {
  875. dma_tx_id = rspi_pd->dma_tx_id;
  876. dma_rx_id = rspi_pd->dma_rx_id;
  877. } else {
  878. /* The driver assumes no error. */
  879. return 0;
  880. }
  881. master->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV, dma_tx_id,
  882. res->start + RSPI_SPDR);
  883. if (!master->dma_tx)
  884. return -ENODEV;
  885. master->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM, dma_rx_id,
  886. res->start + RSPI_SPDR);
  887. if (!master->dma_rx) {
  888. dma_release_channel(master->dma_tx);
  889. master->dma_tx = NULL;
  890. return -ENODEV;
  891. }
  892. master->can_dma = rspi_can_dma;
  893. dev_info(dev, "DMA available");
  894. return 0;
  895. }
  896. static void rspi_release_dma(struct spi_master *master)
  897. {
  898. if (master->dma_tx)
  899. dma_release_channel(master->dma_tx);
  900. if (master->dma_rx)
  901. dma_release_channel(master->dma_rx);
  902. }
  903. static int rspi_remove(struct platform_device *pdev)
  904. {
  905. struct rspi_data *rspi = platform_get_drvdata(pdev);
  906. rspi_release_dma(rspi->master);
  907. pm_runtime_disable(&pdev->dev);
  908. return 0;
  909. }
  910. static const struct spi_ops rspi_ops = {
  911. .set_config_register = rspi_set_config_register,
  912. .transfer_one = rspi_transfer_one,
  913. .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
  914. .flags = SPI_MASTER_MUST_TX,
  915. .fifo_size = 8,
  916. };
  917. static const struct spi_ops rspi_rz_ops = {
  918. .set_config_register = rspi_rz_set_config_register,
  919. .transfer_one = rspi_rz_transfer_one,
  920. .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
  921. .flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
  922. .fifo_size = 8, /* 8 for TX, 32 for RX */
  923. };
  924. static const struct spi_ops qspi_ops = {
  925. .set_config_register = qspi_set_config_register,
  926. .transfer_one = qspi_transfer_one,
  927. .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP |
  928. SPI_TX_DUAL | SPI_TX_QUAD |
  929. SPI_RX_DUAL | SPI_RX_QUAD,
  930. .flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
  931. .fifo_size = 32,
  932. };
  933. #ifdef CONFIG_OF
  934. static const struct of_device_id rspi_of_match[] = {
  935. /* RSPI on legacy SH */
  936. { .compatible = "renesas,rspi", .data = &rspi_ops },
  937. /* RSPI on RZ/A1H */
  938. { .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops },
  939. /* QSPI on R-Car Gen2 */
  940. { .compatible = "renesas,qspi", .data = &qspi_ops },
  941. { /* sentinel */ }
  942. };
  943. MODULE_DEVICE_TABLE(of, rspi_of_match);
  944. static int rspi_parse_dt(struct device *dev, struct spi_master *master)
  945. {
  946. u32 num_cs;
  947. int error;
  948. /* Parse DT properties */
  949. error = of_property_read_u32(dev->of_node, "num-cs", &num_cs);
  950. if (error) {
  951. dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error);
  952. return error;
  953. }
  954. master->num_chipselect = num_cs;
  955. return 0;
  956. }
  957. #else
  958. #define rspi_of_match NULL
  959. static inline int rspi_parse_dt(struct device *dev, struct spi_master *master)
  960. {
  961. return -EINVAL;
  962. }
  963. #endif /* CONFIG_OF */
  964. static int rspi_request_irq(struct device *dev, unsigned int irq,
  965. irq_handler_t handler, const char *suffix,
  966. void *dev_id)
  967. {
  968. const char *name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
  969. dev_name(dev), suffix);
  970. if (!name)
  971. return -ENOMEM;
  972. return devm_request_irq(dev, irq, handler, 0, name, dev_id);
  973. }
  974. static int rspi_probe(struct platform_device *pdev)
  975. {
  976. struct resource *res;
  977. struct spi_master *master;
  978. struct rspi_data *rspi;
  979. int ret;
  980. const struct of_device_id *of_id;
  981. const struct rspi_plat_data *rspi_pd;
  982. const struct spi_ops *ops;
  983. master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
  984. if (master == NULL) {
  985. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  986. return -ENOMEM;
  987. }
  988. of_id = of_match_device(rspi_of_match, &pdev->dev);
  989. if (of_id) {
  990. ops = of_id->data;
  991. ret = rspi_parse_dt(&pdev->dev, master);
  992. if (ret)
  993. goto error1;
  994. } else {
  995. ops = (struct spi_ops *)pdev->id_entry->driver_data;
  996. rspi_pd = dev_get_platdata(&pdev->dev);
  997. if (rspi_pd && rspi_pd->num_chipselect)
  998. master->num_chipselect = rspi_pd->num_chipselect;
  999. else
  1000. master->num_chipselect = 2; /* default */
  1001. }
  1002. /* ops parameter check */
  1003. if (!ops->set_config_register) {
  1004. dev_err(&pdev->dev, "there is no set_config_register\n");
  1005. ret = -ENODEV;
  1006. goto error1;
  1007. }
  1008. rspi = spi_master_get_devdata(master);
  1009. platform_set_drvdata(pdev, rspi);
  1010. rspi->ops = ops;
  1011. rspi->master = master;
  1012. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1013. rspi->addr = devm_ioremap_resource(&pdev->dev, res);
  1014. if (IS_ERR(rspi->addr)) {
  1015. ret = PTR_ERR(rspi->addr);
  1016. goto error1;
  1017. }
  1018. rspi->clk = devm_clk_get(&pdev->dev, NULL);
  1019. if (IS_ERR(rspi->clk)) {
  1020. dev_err(&pdev->dev, "cannot get clock\n");
  1021. ret = PTR_ERR(rspi->clk);
  1022. goto error1;
  1023. }
  1024. pm_runtime_enable(&pdev->dev);
  1025. init_waitqueue_head(&rspi->wait);
  1026. master->bus_num = pdev->id;
  1027. master->setup = rspi_setup;
  1028. master->auto_runtime_pm = true;
  1029. master->transfer_one = ops->transfer_one;
  1030. master->prepare_message = rspi_prepare_message;
  1031. master->unprepare_message = rspi_unprepare_message;
  1032. master->mode_bits = ops->mode_bits;
  1033. master->flags = ops->flags;
  1034. master->dev.of_node = pdev->dev.of_node;
  1035. ret = platform_get_irq_byname(pdev, "rx");
  1036. if (ret < 0) {
  1037. ret = platform_get_irq_byname(pdev, "mux");
  1038. if (ret < 0)
  1039. ret = platform_get_irq(pdev, 0);
  1040. if (ret >= 0)
  1041. rspi->rx_irq = rspi->tx_irq = ret;
  1042. } else {
  1043. rspi->rx_irq = ret;
  1044. ret = platform_get_irq_byname(pdev, "tx");
  1045. if (ret >= 0)
  1046. rspi->tx_irq = ret;
  1047. }
  1048. if (ret < 0) {
  1049. dev_err(&pdev->dev, "platform_get_irq error\n");
  1050. goto error2;
  1051. }
  1052. if (rspi->rx_irq == rspi->tx_irq) {
  1053. /* Single multiplexed interrupt */
  1054. ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
  1055. "mux", rspi);
  1056. } else {
  1057. /* Multi-interrupt mode, only SPRI and SPTI are used */
  1058. ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
  1059. "rx", rspi);
  1060. if (!ret)
  1061. ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
  1062. rspi_irq_tx, "tx", rspi);
  1063. }
  1064. if (ret < 0) {
  1065. dev_err(&pdev->dev, "request_irq error\n");
  1066. goto error2;
  1067. }
  1068. ret = rspi_request_dma(&pdev->dev, master, res);
  1069. if (ret < 0)
  1070. dev_warn(&pdev->dev, "DMA not available, using PIO\n");
  1071. ret = devm_spi_register_master(&pdev->dev, master);
  1072. if (ret < 0) {
  1073. dev_err(&pdev->dev, "spi_register_master error.\n");
  1074. goto error3;
  1075. }
  1076. dev_info(&pdev->dev, "probed\n");
  1077. return 0;
  1078. error3:
  1079. rspi_release_dma(master);
  1080. error2:
  1081. pm_runtime_disable(&pdev->dev);
  1082. error1:
  1083. spi_master_put(master);
  1084. return ret;
  1085. }
  1086. static const struct platform_device_id spi_driver_ids[] = {
  1087. { "rspi", (kernel_ulong_t)&rspi_ops },
  1088. { "rspi-rz", (kernel_ulong_t)&rspi_rz_ops },
  1089. { "qspi", (kernel_ulong_t)&qspi_ops },
  1090. {},
  1091. };
  1092. MODULE_DEVICE_TABLE(platform, spi_driver_ids);
  1093. static struct platform_driver rspi_driver = {
  1094. .probe = rspi_probe,
  1095. .remove = rspi_remove,
  1096. .id_table = spi_driver_ids,
  1097. .driver = {
  1098. .name = "renesas_spi",
  1099. .of_match_table = of_match_ptr(rspi_of_match),
  1100. },
  1101. };
  1102. module_platform_driver(rspi_driver);
  1103. MODULE_DESCRIPTION("Renesas RSPI bus driver");
  1104. MODULE_LICENSE("GPL v2");
  1105. MODULE_AUTHOR("Yoshihiro Shimoda");
  1106. MODULE_ALIAS("platform:rspi");