spi-rspi.c 34 KB

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