spi-rspi.c 36 KB

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