spi-rspi.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  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. /* Resets sequencer */
  247. rspi_write8(rspi, 0, RSPI_SPSCR);
  248. rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
  249. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  250. /* Sets RSPI mode */
  251. rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
  252. return 0;
  253. }
  254. /*
  255. * functions for RSPI on RZ
  256. */
  257. static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size)
  258. {
  259. int spbr;
  260. int div = 0;
  261. unsigned long clksrc;
  262. /* Sets output mode, MOSI signal, and (optionally) loopback */
  263. rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
  264. clksrc = clk_get_rate(rspi->clk);
  265. while (div < 3) {
  266. if (rspi->max_speed_hz >= clksrc/4) /* 4=(CLK/2)/2 */
  267. break;
  268. div++;
  269. clksrc /= 2;
  270. }
  271. /* Sets transfer bit rate */
  272. spbr = DIV_ROUND_UP(clksrc, 2 * rspi->max_speed_hz) - 1;
  273. rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
  274. rspi->spcmd |= div << 2;
  275. /* Disable dummy transmission, set byte access */
  276. rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR);
  277. rspi->byte_access = 1;
  278. /* Sets RSPCK, SSL, next-access delay value */
  279. rspi_write8(rspi, 0x00, RSPI_SPCKD);
  280. rspi_write8(rspi, 0x00, RSPI_SSLND);
  281. rspi_write8(rspi, 0x00, RSPI_SPND);
  282. /* Resets sequencer */
  283. rspi_write8(rspi, 0, RSPI_SPSCR);
  284. rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size);
  285. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  286. /* Sets RSPI mode */
  287. rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR);
  288. return 0;
  289. }
  290. /*
  291. * functions for QSPI
  292. */
  293. static int qspi_set_config_register(struct rspi_data *rspi, int access_size)
  294. {
  295. int spbr;
  296. /* Sets output mode, MOSI signal, and (optionally) loopback */
  297. rspi_write8(rspi, rspi->sppcr, RSPI_SPPCR);
  298. /* Sets transfer bit rate */
  299. spbr = DIV_ROUND_UP(clk_get_rate(rspi->clk), 2 * rspi->max_speed_hz);
  300. rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR);
  301. /* Disable dummy transmission, set byte access */
  302. rspi_write8(rspi, 0, RSPI_SPDCR);
  303. rspi->byte_access = 1;
  304. /* Sets RSPCK, SSL, next-access delay value */
  305. rspi_write8(rspi, 0x00, RSPI_SPCKD);
  306. rspi_write8(rspi, 0x00, RSPI_SSLND);
  307. rspi_write8(rspi, 0x00, RSPI_SPND);
  308. /* Data Length Setting */
  309. if (access_size == 8)
  310. rspi->spcmd |= SPCMD_SPB_8BIT;
  311. else if (access_size == 16)
  312. rspi->spcmd |= SPCMD_SPB_16BIT;
  313. else
  314. rspi->spcmd |= SPCMD_SPB_32BIT;
  315. rspi->spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | SPCMD_SPNDEN;
  316. /* Resets transfer data length */
  317. rspi_write32(rspi, 0, QSPI_SPBMUL0);
  318. /* Resets transmit and receive buffer */
  319. rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
  320. /* Sets buffer to allow normal operation */
  321. rspi_write8(rspi, 0x00, QSPI_SPBFCR);
  322. /* Resets sequencer */
  323. rspi_write8(rspi, 0, RSPI_SPSCR);
  324. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  325. /* Enables SPI function in master mode */
  326. rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR);
  327. return 0;
  328. }
  329. static void qspi_update(const struct rspi_data *rspi, u8 mask, u8 val, u8 reg)
  330. {
  331. u8 data;
  332. data = rspi_read8(rspi, reg);
  333. data &= ~mask;
  334. data |= (val & mask);
  335. rspi_write8(rspi, data, reg);
  336. }
  337. static unsigned int qspi_set_send_trigger(struct rspi_data *rspi,
  338. unsigned int len)
  339. {
  340. unsigned int n;
  341. n = min(len, QSPI_BUFFER_SIZE);
  342. if (len >= QSPI_BUFFER_SIZE) {
  343. /* sets triggering number to 32 bytes */
  344. qspi_update(rspi, SPBFCR_TXTRG_MASK,
  345. SPBFCR_TXTRG_32B, QSPI_SPBFCR);
  346. } else {
  347. /* sets triggering number to 1 byte */
  348. qspi_update(rspi, SPBFCR_TXTRG_MASK,
  349. SPBFCR_TXTRG_1B, QSPI_SPBFCR);
  350. }
  351. return n;
  352. }
  353. static void qspi_set_receive_trigger(struct rspi_data *rspi, unsigned int len)
  354. {
  355. unsigned int n;
  356. n = min(len, QSPI_BUFFER_SIZE);
  357. if (len >= QSPI_BUFFER_SIZE) {
  358. /* sets triggering number to 32 bytes */
  359. qspi_update(rspi, SPBFCR_RXTRG_MASK,
  360. SPBFCR_RXTRG_32B, QSPI_SPBFCR);
  361. } else {
  362. /* sets triggering number to 1 byte */
  363. qspi_update(rspi, SPBFCR_RXTRG_MASK,
  364. SPBFCR_RXTRG_1B, QSPI_SPBFCR);
  365. }
  366. }
  367. #define set_config_register(spi, n) spi->ops->set_config_register(spi, n)
  368. static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable)
  369. {
  370. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR);
  371. }
  372. static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable)
  373. {
  374. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR);
  375. }
  376. static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask,
  377. u8 enable_bit)
  378. {
  379. int ret;
  380. rspi->spsr = rspi_read8(rspi, RSPI_SPSR);
  381. if (rspi->spsr & wait_mask)
  382. return 0;
  383. rspi_enable_irq(rspi, enable_bit);
  384. ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ);
  385. if (ret == 0 && !(rspi->spsr & wait_mask))
  386. return -ETIMEDOUT;
  387. return 0;
  388. }
  389. static inline int rspi_wait_for_tx_empty(struct rspi_data *rspi)
  390. {
  391. return rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE);
  392. }
  393. static inline int rspi_wait_for_rx_full(struct rspi_data *rspi)
  394. {
  395. return rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE);
  396. }
  397. static int rspi_data_out(struct rspi_data *rspi, u8 data)
  398. {
  399. int error = rspi_wait_for_tx_empty(rspi);
  400. if (error < 0) {
  401. dev_err(&rspi->master->dev, "transmit timeout\n");
  402. return error;
  403. }
  404. rspi_write_data(rspi, data);
  405. return 0;
  406. }
  407. static int rspi_data_in(struct rspi_data *rspi)
  408. {
  409. int error;
  410. u8 data;
  411. error = rspi_wait_for_rx_full(rspi);
  412. if (error < 0) {
  413. dev_err(&rspi->master->dev, "receive timeout\n");
  414. return error;
  415. }
  416. data = rspi_read_data(rspi);
  417. return data;
  418. }
  419. static int rspi_pio_transfer(struct rspi_data *rspi, const u8 *tx, u8 *rx,
  420. unsigned int n)
  421. {
  422. while (n-- > 0) {
  423. if (tx) {
  424. int ret = rspi_data_out(rspi, *tx++);
  425. if (ret < 0)
  426. return ret;
  427. }
  428. if (rx) {
  429. int ret = rspi_data_in(rspi);
  430. if (ret < 0)
  431. return ret;
  432. *rx++ = ret;
  433. }
  434. }
  435. return 0;
  436. }
  437. static void rspi_dma_complete(void *arg)
  438. {
  439. struct rspi_data *rspi = arg;
  440. rspi->dma_callbacked = 1;
  441. wake_up_interruptible(&rspi->wait);
  442. }
  443. static int rspi_dma_transfer(struct rspi_data *rspi, struct sg_table *tx,
  444. struct sg_table *rx)
  445. {
  446. struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
  447. u8 irq_mask = 0;
  448. unsigned int other_irq = 0;
  449. dma_cookie_t cookie;
  450. int ret;
  451. /* First prepare and submit the DMA request(s), as this may fail */
  452. if (rx) {
  453. desc_rx = dmaengine_prep_slave_sg(rspi->master->dma_rx,
  454. rx->sgl, rx->nents, DMA_FROM_DEVICE,
  455. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  456. if (!desc_rx) {
  457. ret = -EAGAIN;
  458. goto no_dma_rx;
  459. }
  460. desc_rx->callback = rspi_dma_complete;
  461. desc_rx->callback_param = rspi;
  462. cookie = dmaengine_submit(desc_rx);
  463. if (dma_submit_error(cookie)) {
  464. ret = cookie;
  465. goto no_dma_rx;
  466. }
  467. irq_mask |= SPCR_SPRIE;
  468. }
  469. if (tx) {
  470. desc_tx = dmaengine_prep_slave_sg(rspi->master->dma_tx,
  471. tx->sgl, tx->nents, DMA_TO_DEVICE,
  472. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  473. if (!desc_tx) {
  474. ret = -EAGAIN;
  475. goto no_dma_tx;
  476. }
  477. if (rx) {
  478. /* No callback */
  479. desc_tx->callback = NULL;
  480. } else {
  481. desc_tx->callback = rspi_dma_complete;
  482. desc_tx->callback_param = rspi;
  483. }
  484. cookie = dmaengine_submit(desc_tx);
  485. if (dma_submit_error(cookie)) {
  486. ret = cookie;
  487. goto no_dma_tx;
  488. }
  489. irq_mask |= SPCR_SPTIE;
  490. }
  491. /*
  492. * DMAC needs SPxIE, but if SPxIE is set, the IRQ routine will be
  493. * called. So, this driver disables the IRQ while DMA transfer.
  494. */
  495. if (tx)
  496. disable_irq(other_irq = rspi->tx_irq);
  497. if (rx && rspi->rx_irq != other_irq)
  498. disable_irq(rspi->rx_irq);
  499. rspi_enable_irq(rspi, irq_mask);
  500. rspi->dma_callbacked = 0;
  501. /* Now start DMA */
  502. if (rx)
  503. dma_async_issue_pending(rspi->master->dma_rx);
  504. if (tx)
  505. dma_async_issue_pending(rspi->master->dma_tx);
  506. ret = wait_event_interruptible_timeout(rspi->wait,
  507. rspi->dma_callbacked, HZ);
  508. if (ret > 0 && rspi->dma_callbacked) {
  509. ret = 0;
  510. } else {
  511. if (!ret) {
  512. dev_err(&rspi->master->dev, "DMA timeout\n");
  513. ret = -ETIMEDOUT;
  514. }
  515. if (tx)
  516. dmaengine_terminate_all(rspi->master->dma_tx);
  517. if (rx)
  518. dmaengine_terminate_all(rspi->master->dma_rx);
  519. }
  520. rspi_disable_irq(rspi, irq_mask);
  521. if (tx)
  522. enable_irq(rspi->tx_irq);
  523. if (rx && rspi->rx_irq != other_irq)
  524. enable_irq(rspi->rx_irq);
  525. return ret;
  526. no_dma_tx:
  527. if (rx)
  528. dmaengine_terminate_all(rspi->master->dma_rx);
  529. no_dma_rx:
  530. if (ret == -EAGAIN) {
  531. pr_warn_once("%s %s: DMA not available, falling back to PIO\n",
  532. dev_driver_string(&rspi->master->dev),
  533. dev_name(&rspi->master->dev));
  534. }
  535. return ret;
  536. }
  537. static void rspi_receive_init(const struct rspi_data *rspi)
  538. {
  539. u8 spsr;
  540. spsr = rspi_read8(rspi, RSPI_SPSR);
  541. if (spsr & SPSR_SPRF)
  542. rspi_read_data(rspi); /* dummy read */
  543. if (spsr & SPSR_OVRF)
  544. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF,
  545. RSPI_SPSR);
  546. }
  547. static void rspi_rz_receive_init(const struct rspi_data *rspi)
  548. {
  549. rspi_receive_init(rspi);
  550. rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR);
  551. rspi_write8(rspi, 0, RSPI_SPBFCR);
  552. }
  553. static void qspi_receive_init(const struct rspi_data *rspi)
  554. {
  555. u8 spsr;
  556. spsr = rspi_read8(rspi, RSPI_SPSR);
  557. if (spsr & SPSR_SPRF)
  558. rspi_read_data(rspi); /* dummy read */
  559. rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR);
  560. rspi_write8(rspi, 0, QSPI_SPBFCR);
  561. }
  562. static bool __rspi_can_dma(const struct rspi_data *rspi,
  563. const struct spi_transfer *xfer)
  564. {
  565. return xfer->len > rspi->ops->fifo_size;
  566. }
  567. static bool rspi_can_dma(struct spi_master *master, struct spi_device *spi,
  568. struct spi_transfer *xfer)
  569. {
  570. struct rspi_data *rspi = spi_master_get_devdata(master);
  571. return __rspi_can_dma(rspi, xfer);
  572. }
  573. static int rspi_dma_check_then_transfer(struct rspi_data *rspi,
  574. struct spi_transfer *xfer)
  575. {
  576. if (!rspi->master->can_dma || !__rspi_can_dma(rspi, xfer))
  577. return -EAGAIN;
  578. /* rx_buf can be NULL on RSPI on SH in TX-only Mode */
  579. return rspi_dma_transfer(rspi, &xfer->tx_sg,
  580. xfer->rx_buf ? &xfer->rx_sg : NULL);
  581. }
  582. static int rspi_common_transfer(struct rspi_data *rspi,
  583. struct spi_transfer *xfer)
  584. {
  585. int ret;
  586. ret = rspi_dma_check_then_transfer(rspi, xfer);
  587. if (ret != -EAGAIN)
  588. return ret;
  589. ret = rspi_pio_transfer(rspi, xfer->tx_buf, xfer->rx_buf, xfer->len);
  590. if (ret < 0)
  591. return ret;
  592. /* Wait for the last transmission */
  593. rspi_wait_for_tx_empty(rspi);
  594. return 0;
  595. }
  596. static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi,
  597. struct spi_transfer *xfer)
  598. {
  599. struct rspi_data *rspi = spi_master_get_devdata(master);
  600. u8 spcr;
  601. spcr = rspi_read8(rspi, RSPI_SPCR);
  602. if (xfer->rx_buf) {
  603. rspi_receive_init(rspi);
  604. spcr &= ~SPCR_TXMD;
  605. } else {
  606. spcr |= SPCR_TXMD;
  607. }
  608. rspi_write8(rspi, spcr, RSPI_SPCR);
  609. return rspi_common_transfer(rspi, xfer);
  610. }
  611. static int rspi_rz_transfer_one(struct spi_master *master,
  612. struct spi_device *spi,
  613. struct spi_transfer *xfer)
  614. {
  615. struct rspi_data *rspi = spi_master_get_devdata(master);
  616. rspi_rz_receive_init(rspi);
  617. return rspi_common_transfer(rspi, xfer);
  618. }
  619. static int qspi_trigger_transfer_out_in(struct rspi_data *rspi, const u8 *tx,
  620. u8 *rx, unsigned int len)
  621. {
  622. unsigned int i, n;
  623. int ret;
  624. while (len > 0) {
  625. n = qspi_set_send_trigger(rspi, len);
  626. qspi_set_receive_trigger(rspi, len);
  627. if (n == QSPI_BUFFER_SIZE) {
  628. ret = rspi_wait_for_tx_empty(rspi);
  629. if (ret < 0) {
  630. dev_err(&rspi->master->dev, "transmit timeout\n");
  631. return ret;
  632. }
  633. for (i = 0; i < n; i++)
  634. rspi_write_data(rspi, *tx++);
  635. ret = rspi_wait_for_rx_full(rspi);
  636. if (ret < 0) {
  637. dev_err(&rspi->master->dev, "receive timeout\n");
  638. return ret;
  639. }
  640. for (i = 0; i < n; i++)
  641. *rx++ = rspi_read_data(rspi);
  642. } else {
  643. ret = rspi_pio_transfer(rspi, tx, rx, n);
  644. if (ret < 0)
  645. return ret;
  646. }
  647. len -= n;
  648. }
  649. return 0;
  650. }
  651. static int qspi_transfer_out_in(struct rspi_data *rspi,
  652. struct spi_transfer *xfer)
  653. {
  654. int ret;
  655. qspi_receive_init(rspi);
  656. ret = rspi_dma_check_then_transfer(rspi, xfer);
  657. if (ret != -EAGAIN)
  658. return ret;
  659. return qspi_trigger_transfer_out_in(rspi, xfer->tx_buf,
  660. xfer->rx_buf, xfer->len);
  661. }
  662. static int qspi_transfer_out(struct rspi_data *rspi, struct spi_transfer *xfer)
  663. {
  664. int ret;
  665. if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
  666. ret = rspi_dma_transfer(rspi, &xfer->tx_sg, NULL);
  667. if (ret != -EAGAIN)
  668. return ret;
  669. }
  670. ret = rspi_pio_transfer(rspi, xfer->tx_buf, NULL, xfer->len);
  671. if (ret < 0)
  672. return ret;
  673. /* Wait for the last transmission */
  674. rspi_wait_for_tx_empty(rspi);
  675. return 0;
  676. }
  677. static int qspi_transfer_in(struct rspi_data *rspi, struct spi_transfer *xfer)
  678. {
  679. if (rspi->master->can_dma && __rspi_can_dma(rspi, xfer)) {
  680. int ret = rspi_dma_transfer(rspi, NULL, &xfer->rx_sg);
  681. if (ret != -EAGAIN)
  682. return ret;
  683. }
  684. return rspi_pio_transfer(rspi, NULL, xfer->rx_buf, xfer->len);
  685. }
  686. static int qspi_transfer_one(struct spi_master *master, struct spi_device *spi,
  687. struct spi_transfer *xfer)
  688. {
  689. struct rspi_data *rspi = spi_master_get_devdata(master);
  690. if (spi->mode & SPI_LOOP) {
  691. return qspi_transfer_out_in(rspi, xfer);
  692. } else if (xfer->tx_nbits > SPI_NBITS_SINGLE) {
  693. /* Quad or Dual SPI Write */
  694. return qspi_transfer_out(rspi, xfer);
  695. } else if (xfer->rx_nbits > SPI_NBITS_SINGLE) {
  696. /* Quad or Dual SPI Read */
  697. return qspi_transfer_in(rspi, xfer);
  698. } else {
  699. /* Single SPI Transfer */
  700. return qspi_transfer_out_in(rspi, xfer);
  701. }
  702. }
  703. static int rspi_setup(struct spi_device *spi)
  704. {
  705. struct rspi_data *rspi = spi_master_get_devdata(spi->master);
  706. rspi->max_speed_hz = spi->max_speed_hz;
  707. rspi->spcmd = SPCMD_SSLKP;
  708. if (spi->mode & SPI_CPOL)
  709. rspi->spcmd |= SPCMD_CPOL;
  710. if (spi->mode & SPI_CPHA)
  711. rspi->spcmd |= SPCMD_CPHA;
  712. /* CMOS output mode and MOSI signal from previous transfer */
  713. rspi->sppcr = 0;
  714. if (spi->mode & SPI_LOOP)
  715. rspi->sppcr |= SPPCR_SPLP;
  716. set_config_register(rspi, 8);
  717. return 0;
  718. }
  719. static u16 qspi_transfer_mode(const struct spi_transfer *xfer)
  720. {
  721. if (xfer->tx_buf)
  722. switch (xfer->tx_nbits) {
  723. case SPI_NBITS_QUAD:
  724. return SPCMD_SPIMOD_QUAD;
  725. case SPI_NBITS_DUAL:
  726. return SPCMD_SPIMOD_DUAL;
  727. default:
  728. return 0;
  729. }
  730. if (xfer->rx_buf)
  731. switch (xfer->rx_nbits) {
  732. case SPI_NBITS_QUAD:
  733. return SPCMD_SPIMOD_QUAD | SPCMD_SPRW;
  734. case SPI_NBITS_DUAL:
  735. return SPCMD_SPIMOD_DUAL | SPCMD_SPRW;
  736. default:
  737. return 0;
  738. }
  739. return 0;
  740. }
  741. static int qspi_setup_sequencer(struct rspi_data *rspi,
  742. const struct spi_message *msg)
  743. {
  744. const struct spi_transfer *xfer;
  745. unsigned int i = 0, len = 0;
  746. u16 current_mode = 0xffff, mode;
  747. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  748. mode = qspi_transfer_mode(xfer);
  749. if (mode == current_mode) {
  750. len += xfer->len;
  751. continue;
  752. }
  753. /* Transfer mode change */
  754. if (i) {
  755. /* Set transfer data length of previous transfer */
  756. rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
  757. }
  758. if (i >= QSPI_NUM_SPCMD) {
  759. dev_err(&msg->spi->dev,
  760. "Too many different transfer modes");
  761. return -EINVAL;
  762. }
  763. /* Program transfer mode for this transfer */
  764. rspi_write16(rspi, rspi->spcmd | mode, RSPI_SPCMD(i));
  765. current_mode = mode;
  766. len = xfer->len;
  767. i++;
  768. }
  769. if (i) {
  770. /* Set final transfer data length and sequence length */
  771. rspi_write32(rspi, len, QSPI_SPBMUL(i - 1));
  772. rspi_write8(rspi, i - 1, RSPI_SPSCR);
  773. }
  774. return 0;
  775. }
  776. static int rspi_prepare_message(struct spi_master *master,
  777. struct spi_message *msg)
  778. {
  779. struct rspi_data *rspi = spi_master_get_devdata(master);
  780. int ret;
  781. if (msg->spi->mode &
  782. (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)) {
  783. /* Setup sequencer for messages with multiple transfer modes */
  784. ret = qspi_setup_sequencer(rspi, msg);
  785. if (ret < 0)
  786. return ret;
  787. }
  788. /* Enable SPI function in master mode */
  789. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR);
  790. return 0;
  791. }
  792. static int rspi_unprepare_message(struct spi_master *master,
  793. struct spi_message *msg)
  794. {
  795. struct rspi_data *rspi = spi_master_get_devdata(master);
  796. /* Disable SPI function */
  797. rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR);
  798. /* Reset sequencer for Single SPI Transfers */
  799. rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0);
  800. rspi_write8(rspi, 0, RSPI_SPSCR);
  801. return 0;
  802. }
  803. static irqreturn_t rspi_irq_mux(int irq, void *_sr)
  804. {
  805. struct rspi_data *rspi = _sr;
  806. u8 spsr;
  807. irqreturn_t ret = IRQ_NONE;
  808. u8 disable_irq = 0;
  809. rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
  810. if (spsr & SPSR_SPRF)
  811. disable_irq |= SPCR_SPRIE;
  812. if (spsr & SPSR_SPTEF)
  813. disable_irq |= SPCR_SPTIE;
  814. if (disable_irq) {
  815. ret = IRQ_HANDLED;
  816. rspi_disable_irq(rspi, disable_irq);
  817. wake_up(&rspi->wait);
  818. }
  819. return ret;
  820. }
  821. static irqreturn_t rspi_irq_rx(int irq, void *_sr)
  822. {
  823. struct rspi_data *rspi = _sr;
  824. u8 spsr;
  825. rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
  826. if (spsr & SPSR_SPRF) {
  827. rspi_disable_irq(rspi, SPCR_SPRIE);
  828. wake_up(&rspi->wait);
  829. return IRQ_HANDLED;
  830. }
  831. return 0;
  832. }
  833. static irqreturn_t rspi_irq_tx(int irq, void *_sr)
  834. {
  835. struct rspi_data *rspi = _sr;
  836. u8 spsr;
  837. rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR);
  838. if (spsr & SPSR_SPTEF) {
  839. rspi_disable_irq(rspi, SPCR_SPTIE);
  840. wake_up(&rspi->wait);
  841. return IRQ_HANDLED;
  842. }
  843. return 0;
  844. }
  845. static struct dma_chan *rspi_request_dma_chan(struct device *dev,
  846. enum dma_transfer_direction dir,
  847. unsigned int id,
  848. dma_addr_t port_addr)
  849. {
  850. dma_cap_mask_t mask;
  851. struct dma_chan *chan;
  852. struct dma_slave_config cfg;
  853. int ret;
  854. dma_cap_zero(mask);
  855. dma_cap_set(DMA_SLAVE, mask);
  856. chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  857. (void *)(unsigned long)id, dev,
  858. dir == DMA_MEM_TO_DEV ? "tx" : "rx");
  859. if (!chan) {
  860. dev_warn(dev, "dma_request_slave_channel_compat failed\n");
  861. return NULL;
  862. }
  863. memset(&cfg, 0, sizeof(cfg));
  864. cfg.direction = dir;
  865. if (dir == DMA_MEM_TO_DEV) {
  866. cfg.dst_addr = port_addr;
  867. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  868. } else {
  869. cfg.src_addr = port_addr;
  870. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  871. }
  872. ret = dmaengine_slave_config(chan, &cfg);
  873. if (ret) {
  874. dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
  875. dma_release_channel(chan);
  876. return NULL;
  877. }
  878. return chan;
  879. }
  880. static int rspi_request_dma(struct device *dev, struct spi_master *master,
  881. const struct resource *res)
  882. {
  883. const struct rspi_plat_data *rspi_pd = dev_get_platdata(dev);
  884. unsigned int dma_tx_id, dma_rx_id;
  885. if (dev->of_node) {
  886. /* In the OF case we will get the slave IDs from the DT */
  887. dma_tx_id = 0;
  888. dma_rx_id = 0;
  889. } else if (rspi_pd && rspi_pd->dma_tx_id && rspi_pd->dma_rx_id) {
  890. dma_tx_id = rspi_pd->dma_tx_id;
  891. dma_rx_id = rspi_pd->dma_rx_id;
  892. } else {
  893. /* The driver assumes no error. */
  894. return 0;
  895. }
  896. master->dma_tx = rspi_request_dma_chan(dev, DMA_MEM_TO_DEV, dma_tx_id,
  897. res->start + RSPI_SPDR);
  898. if (!master->dma_tx)
  899. return -ENODEV;
  900. master->dma_rx = rspi_request_dma_chan(dev, DMA_DEV_TO_MEM, dma_rx_id,
  901. res->start + RSPI_SPDR);
  902. if (!master->dma_rx) {
  903. dma_release_channel(master->dma_tx);
  904. master->dma_tx = NULL;
  905. return -ENODEV;
  906. }
  907. master->can_dma = rspi_can_dma;
  908. dev_info(dev, "DMA available");
  909. return 0;
  910. }
  911. static void rspi_release_dma(struct spi_master *master)
  912. {
  913. if (master->dma_tx)
  914. dma_release_channel(master->dma_tx);
  915. if (master->dma_rx)
  916. dma_release_channel(master->dma_rx);
  917. }
  918. static int rspi_remove(struct platform_device *pdev)
  919. {
  920. struct rspi_data *rspi = platform_get_drvdata(pdev);
  921. rspi_release_dma(rspi->master);
  922. pm_runtime_disable(&pdev->dev);
  923. return 0;
  924. }
  925. static const struct spi_ops rspi_ops = {
  926. .set_config_register = rspi_set_config_register,
  927. .transfer_one = rspi_transfer_one,
  928. .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
  929. .flags = SPI_MASTER_MUST_TX,
  930. .fifo_size = 8,
  931. };
  932. static const struct spi_ops rspi_rz_ops = {
  933. .set_config_register = rspi_rz_set_config_register,
  934. .transfer_one = rspi_rz_transfer_one,
  935. .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP,
  936. .flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
  937. .fifo_size = 8, /* 8 for TX, 32 for RX */
  938. };
  939. static const struct spi_ops qspi_ops = {
  940. .set_config_register = qspi_set_config_register,
  941. .transfer_one = qspi_transfer_one,
  942. .mode_bits = SPI_CPHA | SPI_CPOL | SPI_LOOP |
  943. SPI_TX_DUAL | SPI_TX_QUAD |
  944. SPI_RX_DUAL | SPI_RX_QUAD,
  945. .flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX,
  946. .fifo_size = 32,
  947. };
  948. #ifdef CONFIG_OF
  949. static const struct of_device_id rspi_of_match[] = {
  950. /* RSPI on legacy SH */
  951. { .compatible = "renesas,rspi", .data = &rspi_ops },
  952. /* RSPI on RZ/A1H */
  953. { .compatible = "renesas,rspi-rz", .data = &rspi_rz_ops },
  954. /* QSPI on R-Car Gen2 */
  955. { .compatible = "renesas,qspi", .data = &qspi_ops },
  956. { /* sentinel */ }
  957. };
  958. MODULE_DEVICE_TABLE(of, rspi_of_match);
  959. static int rspi_parse_dt(struct device *dev, struct spi_master *master)
  960. {
  961. u32 num_cs;
  962. int error;
  963. /* Parse DT properties */
  964. error = of_property_read_u32(dev->of_node, "num-cs", &num_cs);
  965. if (error) {
  966. dev_err(dev, "of_property_read_u32 num-cs failed %d\n", error);
  967. return error;
  968. }
  969. master->num_chipselect = num_cs;
  970. return 0;
  971. }
  972. #else
  973. #define rspi_of_match NULL
  974. static inline int rspi_parse_dt(struct device *dev, struct spi_master *master)
  975. {
  976. return -EINVAL;
  977. }
  978. #endif /* CONFIG_OF */
  979. static int rspi_request_irq(struct device *dev, unsigned int irq,
  980. irq_handler_t handler, const char *suffix,
  981. void *dev_id)
  982. {
  983. const char *name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
  984. dev_name(dev), suffix);
  985. if (!name)
  986. return -ENOMEM;
  987. return devm_request_irq(dev, irq, handler, 0, name, dev_id);
  988. }
  989. static int rspi_probe(struct platform_device *pdev)
  990. {
  991. struct resource *res;
  992. struct spi_master *master;
  993. struct rspi_data *rspi;
  994. int ret;
  995. const struct of_device_id *of_id;
  996. const struct rspi_plat_data *rspi_pd;
  997. const struct spi_ops *ops;
  998. master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
  999. if (master == NULL) {
  1000. dev_err(&pdev->dev, "spi_alloc_master error.\n");
  1001. return -ENOMEM;
  1002. }
  1003. of_id = of_match_device(rspi_of_match, &pdev->dev);
  1004. if (of_id) {
  1005. ops = of_id->data;
  1006. ret = rspi_parse_dt(&pdev->dev, master);
  1007. if (ret)
  1008. goto error1;
  1009. } else {
  1010. ops = (struct spi_ops *)pdev->id_entry->driver_data;
  1011. rspi_pd = dev_get_platdata(&pdev->dev);
  1012. if (rspi_pd && rspi_pd->num_chipselect)
  1013. master->num_chipselect = rspi_pd->num_chipselect;
  1014. else
  1015. master->num_chipselect = 2; /* default */
  1016. }
  1017. /* ops parameter check */
  1018. if (!ops->set_config_register) {
  1019. dev_err(&pdev->dev, "there is no set_config_register\n");
  1020. ret = -ENODEV;
  1021. goto error1;
  1022. }
  1023. rspi = spi_master_get_devdata(master);
  1024. platform_set_drvdata(pdev, rspi);
  1025. rspi->ops = ops;
  1026. rspi->master = master;
  1027. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1028. rspi->addr = devm_ioremap_resource(&pdev->dev, res);
  1029. if (IS_ERR(rspi->addr)) {
  1030. ret = PTR_ERR(rspi->addr);
  1031. goto error1;
  1032. }
  1033. rspi->clk = devm_clk_get(&pdev->dev, NULL);
  1034. if (IS_ERR(rspi->clk)) {
  1035. dev_err(&pdev->dev, "cannot get clock\n");
  1036. ret = PTR_ERR(rspi->clk);
  1037. goto error1;
  1038. }
  1039. pm_runtime_enable(&pdev->dev);
  1040. init_waitqueue_head(&rspi->wait);
  1041. master->bus_num = pdev->id;
  1042. master->setup = rspi_setup;
  1043. master->auto_runtime_pm = true;
  1044. master->transfer_one = ops->transfer_one;
  1045. master->prepare_message = rspi_prepare_message;
  1046. master->unprepare_message = rspi_unprepare_message;
  1047. master->mode_bits = ops->mode_bits;
  1048. master->flags = ops->flags;
  1049. master->dev.of_node = pdev->dev.of_node;
  1050. ret = platform_get_irq_byname(pdev, "rx");
  1051. if (ret < 0) {
  1052. ret = platform_get_irq_byname(pdev, "mux");
  1053. if (ret < 0)
  1054. ret = platform_get_irq(pdev, 0);
  1055. if (ret >= 0)
  1056. rspi->rx_irq = rspi->tx_irq = ret;
  1057. } else {
  1058. rspi->rx_irq = ret;
  1059. ret = platform_get_irq_byname(pdev, "tx");
  1060. if (ret >= 0)
  1061. rspi->tx_irq = ret;
  1062. }
  1063. if (ret < 0) {
  1064. dev_err(&pdev->dev, "platform_get_irq error\n");
  1065. goto error2;
  1066. }
  1067. if (rspi->rx_irq == rspi->tx_irq) {
  1068. /* Single multiplexed interrupt */
  1069. ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
  1070. "mux", rspi);
  1071. } else {
  1072. /* Multi-interrupt mode, only SPRI and SPTI are used */
  1073. ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
  1074. "rx", rspi);
  1075. if (!ret)
  1076. ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
  1077. rspi_irq_tx, "tx", rspi);
  1078. }
  1079. if (ret < 0) {
  1080. dev_err(&pdev->dev, "request_irq error\n");
  1081. goto error2;
  1082. }
  1083. ret = rspi_request_dma(&pdev->dev, master, res);
  1084. if (ret < 0)
  1085. dev_warn(&pdev->dev, "DMA not available, using PIO\n");
  1086. ret = devm_spi_register_master(&pdev->dev, master);
  1087. if (ret < 0) {
  1088. dev_err(&pdev->dev, "spi_register_master error.\n");
  1089. goto error3;
  1090. }
  1091. dev_info(&pdev->dev, "probed\n");
  1092. return 0;
  1093. error3:
  1094. rspi_release_dma(master);
  1095. error2:
  1096. pm_runtime_disable(&pdev->dev);
  1097. error1:
  1098. spi_master_put(master);
  1099. return ret;
  1100. }
  1101. static const struct platform_device_id spi_driver_ids[] = {
  1102. { "rspi", (kernel_ulong_t)&rspi_ops },
  1103. { "rspi-rz", (kernel_ulong_t)&rspi_rz_ops },
  1104. { "qspi", (kernel_ulong_t)&qspi_ops },
  1105. {},
  1106. };
  1107. MODULE_DEVICE_TABLE(platform, spi_driver_ids);
  1108. #ifdef CONFIG_PM_SLEEP
  1109. static int rspi_suspend(struct device *dev)
  1110. {
  1111. struct platform_device *pdev = to_platform_device(dev);
  1112. struct rspi_data *rspi = platform_get_drvdata(pdev);
  1113. return spi_master_suspend(rspi->master);
  1114. }
  1115. static int rspi_resume(struct device *dev)
  1116. {
  1117. struct platform_device *pdev = to_platform_device(dev);
  1118. struct rspi_data *rspi = platform_get_drvdata(pdev);
  1119. return spi_master_resume(rspi->master);
  1120. }
  1121. static SIMPLE_DEV_PM_OPS(rspi_pm_ops, rspi_suspend, rspi_resume);
  1122. #define DEV_PM_OPS &rspi_pm_ops
  1123. #else
  1124. #define DEV_PM_OPS NULL
  1125. #endif /* CONFIG_PM_SLEEP */
  1126. static struct platform_driver rspi_driver = {
  1127. .probe = rspi_probe,
  1128. .remove = rspi_remove,
  1129. .id_table = spi_driver_ids,
  1130. .driver = {
  1131. .name = "renesas_spi",
  1132. .pm = DEV_PM_OPS,
  1133. .of_match_table = of_match_ptr(rspi_of_match),
  1134. },
  1135. };
  1136. module_platform_driver(rspi_driver);
  1137. MODULE_DESCRIPTION("Renesas RSPI bus driver");
  1138. MODULE_LICENSE("GPL v2");
  1139. MODULE_AUTHOR("Yoshihiro Shimoda");
  1140. MODULE_ALIAS("platform:rspi");