spi-rspi.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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. /* Sets RSPI mode */
  326. rspi_write8(rspi, 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_DEV_TO_MEM,
  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_MEM_TO_DEV,
  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 rspi_plat_data *rspi_pd;
  1035. const struct spi_ops *ops;
  1036. master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data));
  1037. if (master == NULL)
  1038. return -ENOMEM;
  1039. ops = of_device_get_match_data(&pdev->dev);
  1040. if (ops) {
  1041. ret = rspi_parse_dt(&pdev->dev, master);
  1042. if (ret)
  1043. goto error1;
  1044. } else {
  1045. ops = (struct spi_ops *)pdev->id_entry->driver_data;
  1046. rspi_pd = dev_get_platdata(&pdev->dev);
  1047. if (rspi_pd && rspi_pd->num_chipselect)
  1048. master->num_chipselect = rspi_pd->num_chipselect;
  1049. else
  1050. master->num_chipselect = 2; /* default */
  1051. }
  1052. /* ops parameter check */
  1053. if (!ops->set_config_register) {
  1054. dev_err(&pdev->dev, "there is no set_config_register\n");
  1055. ret = -ENODEV;
  1056. goto error1;
  1057. }
  1058. rspi = spi_master_get_devdata(master);
  1059. platform_set_drvdata(pdev, rspi);
  1060. rspi->ops = ops;
  1061. rspi->master = master;
  1062. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1063. rspi->addr = devm_ioremap_resource(&pdev->dev, res);
  1064. if (IS_ERR(rspi->addr)) {
  1065. ret = PTR_ERR(rspi->addr);
  1066. goto error1;
  1067. }
  1068. rspi->clk = devm_clk_get(&pdev->dev, NULL);
  1069. if (IS_ERR(rspi->clk)) {
  1070. dev_err(&pdev->dev, "cannot get clock\n");
  1071. ret = PTR_ERR(rspi->clk);
  1072. goto error1;
  1073. }
  1074. pm_runtime_enable(&pdev->dev);
  1075. init_waitqueue_head(&rspi->wait);
  1076. master->bus_num = pdev->id;
  1077. master->setup = rspi_setup;
  1078. master->auto_runtime_pm = true;
  1079. master->transfer_one = ops->transfer_one;
  1080. master->prepare_message = rspi_prepare_message;
  1081. master->unprepare_message = rspi_unprepare_message;
  1082. master->mode_bits = ops->mode_bits;
  1083. master->flags = ops->flags;
  1084. master->dev.of_node = pdev->dev.of_node;
  1085. ret = platform_get_irq_byname(pdev, "rx");
  1086. if (ret < 0) {
  1087. ret = platform_get_irq_byname(pdev, "mux");
  1088. if (ret < 0)
  1089. ret = platform_get_irq(pdev, 0);
  1090. if (ret >= 0)
  1091. rspi->rx_irq = rspi->tx_irq = ret;
  1092. } else {
  1093. rspi->rx_irq = ret;
  1094. ret = platform_get_irq_byname(pdev, "tx");
  1095. if (ret >= 0)
  1096. rspi->tx_irq = ret;
  1097. }
  1098. if (ret < 0) {
  1099. dev_err(&pdev->dev, "platform_get_irq error\n");
  1100. goto error2;
  1101. }
  1102. if (rspi->rx_irq == rspi->tx_irq) {
  1103. /* Single multiplexed interrupt */
  1104. ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux,
  1105. "mux", rspi);
  1106. } else {
  1107. /* Multi-interrupt mode, only SPRI and SPTI are used */
  1108. ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx,
  1109. "rx", rspi);
  1110. if (!ret)
  1111. ret = rspi_request_irq(&pdev->dev, rspi->tx_irq,
  1112. rspi_irq_tx, "tx", rspi);
  1113. }
  1114. if (ret < 0) {
  1115. dev_err(&pdev->dev, "request_irq error\n");
  1116. goto error2;
  1117. }
  1118. ret = rspi_request_dma(&pdev->dev, master, res);
  1119. if (ret < 0)
  1120. dev_warn(&pdev->dev, "DMA not available, using PIO\n");
  1121. ret = devm_spi_register_master(&pdev->dev, master);
  1122. if (ret < 0) {
  1123. dev_err(&pdev->dev, "spi_register_master error.\n");
  1124. goto error3;
  1125. }
  1126. dev_info(&pdev->dev, "probed\n");
  1127. return 0;
  1128. error3:
  1129. rspi_release_dma(master);
  1130. error2:
  1131. pm_runtime_disable(&pdev->dev);
  1132. error1:
  1133. spi_master_put(master);
  1134. return ret;
  1135. }
  1136. static const struct platform_device_id spi_driver_ids[] = {
  1137. { "rspi", (kernel_ulong_t)&rspi_ops },
  1138. { "rspi-rz", (kernel_ulong_t)&rspi_rz_ops },
  1139. { "qspi", (kernel_ulong_t)&qspi_ops },
  1140. {},
  1141. };
  1142. MODULE_DEVICE_TABLE(platform, spi_driver_ids);
  1143. #ifdef CONFIG_PM_SLEEP
  1144. static int rspi_suspend(struct device *dev)
  1145. {
  1146. struct platform_device *pdev = to_platform_device(dev);
  1147. struct rspi_data *rspi = platform_get_drvdata(pdev);
  1148. return spi_master_suspend(rspi->master);
  1149. }
  1150. static int rspi_resume(struct device *dev)
  1151. {
  1152. struct platform_device *pdev = to_platform_device(dev);
  1153. struct rspi_data *rspi = platform_get_drvdata(pdev);
  1154. return spi_master_resume(rspi->master);
  1155. }
  1156. static SIMPLE_DEV_PM_OPS(rspi_pm_ops, rspi_suspend, rspi_resume);
  1157. #define DEV_PM_OPS &rspi_pm_ops
  1158. #else
  1159. #define DEV_PM_OPS NULL
  1160. #endif /* CONFIG_PM_SLEEP */
  1161. static struct platform_driver rspi_driver = {
  1162. .probe = rspi_probe,
  1163. .remove = rspi_remove,
  1164. .id_table = spi_driver_ids,
  1165. .driver = {
  1166. .name = "renesas_spi",
  1167. .pm = DEV_PM_OPS,
  1168. .of_match_table = of_match_ptr(rspi_of_match),
  1169. },
  1170. };
  1171. module_platform_driver(rspi_driver);
  1172. MODULE_DESCRIPTION("Renesas RSPI bus driver");
  1173. MODULE_LICENSE("GPL v2");
  1174. MODULE_AUTHOR("Yoshihiro Shimoda");
  1175. MODULE_ALIAS("platform:rspi");