spi-rspi.c 35 KB

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