spi-sh-msiof.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  1. /*
  2. * SuperH MSIOF SPI Master Interface
  3. *
  4. * Copyright (c) 2009 Magnus Damm
  5. * Copyright (C) 2014 Glider bvba
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/bitmap.h>
  13. #include <linux/clk.h>
  14. #include <linux/completion.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/err.h>
  19. #include <linux/gpio.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/sh_dma.h>
  29. #include <linux/spi/sh_msiof.h>
  30. #include <linux/spi/spi.h>
  31. #include <asm/unaligned.h>
  32. struct sh_msiof_chipdata {
  33. u16 tx_fifo_size;
  34. u16 rx_fifo_size;
  35. u16 master_flags;
  36. };
  37. struct sh_msiof_spi_priv {
  38. struct spi_master *master;
  39. void __iomem *mapbase;
  40. struct clk *clk;
  41. struct platform_device *pdev;
  42. const struct sh_msiof_chipdata *chipdata;
  43. struct sh_msiof_spi_info *info;
  44. struct completion done;
  45. int tx_fifo_size;
  46. int rx_fifo_size;
  47. void *tx_dma_page;
  48. void *rx_dma_page;
  49. dma_addr_t tx_dma_addr;
  50. dma_addr_t rx_dma_addr;
  51. };
  52. #define TMDR1 0x00 /* Transmit Mode Register 1 */
  53. #define TMDR2 0x04 /* Transmit Mode Register 2 */
  54. #define TMDR3 0x08 /* Transmit Mode Register 3 */
  55. #define RMDR1 0x10 /* Receive Mode Register 1 */
  56. #define RMDR2 0x14 /* Receive Mode Register 2 */
  57. #define RMDR3 0x18 /* Receive Mode Register 3 */
  58. #define TSCR 0x20 /* Transmit Clock Select Register */
  59. #define RSCR 0x22 /* Receive Clock Select Register (SH, A1, APE6) */
  60. #define CTR 0x28 /* Control Register */
  61. #define FCTR 0x30 /* FIFO Control Register */
  62. #define STR 0x40 /* Status Register */
  63. #define IER 0x44 /* Interrupt Enable Register */
  64. #define TDR1 0x48 /* Transmit Control Data Register 1 (SH, A1) */
  65. #define TDR2 0x4c /* Transmit Control Data Register 2 (SH, A1) */
  66. #define TFDR 0x50 /* Transmit FIFO Data Register */
  67. #define RDR1 0x58 /* Receive Control Data Register 1 (SH, A1) */
  68. #define RDR2 0x5c /* Receive Control Data Register 2 (SH, A1) */
  69. #define RFDR 0x60 /* Receive FIFO Data Register */
  70. /* TMDR1 and RMDR1 */
  71. #define MDR1_TRMD 0x80000000 /* Transfer Mode (1 = Master mode) */
  72. #define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */
  73. #define MDR1_SYNCMD_SPI 0x20000000 /* Level mode/SPI */
  74. #define MDR1_SYNCMD_LR 0x30000000 /* L/R mode */
  75. #define MDR1_SYNCAC_SHIFT 25 /* Sync Polarity (1 = Active-low) */
  76. #define MDR1_BITLSB_SHIFT 24 /* MSB/LSB First (1 = LSB first) */
  77. #define MDR1_DTDL_SHIFT 20 /* Data Pin Bit Delay for MSIOF_SYNC */
  78. #define MDR1_SYNCDL_SHIFT 16 /* Frame Sync Signal Timing Delay */
  79. #define MDR1_FLD_MASK 0x0000000c /* Frame Sync Signal Interval (0-3) */
  80. #define MDR1_FLD_SHIFT 2
  81. #define MDR1_XXSTP 0x00000001 /* Transmission/Reception Stop on FIFO */
  82. /* TMDR1 */
  83. #define TMDR1_PCON 0x40000000 /* Transfer Signal Connection */
  84. /* TMDR2 and RMDR2 */
  85. #define MDR2_BITLEN1(i) (((i) - 1) << 24) /* Data Size (8-32 bits) */
  86. #define MDR2_WDLEN1(i) (((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */
  87. #define MDR2_GRPMASK1 0x00000001 /* Group Output Mask 1 (SH, A1) */
  88. #define MAX_WDLEN 256U
  89. /* TSCR and RSCR */
  90. #define SCR_BRPS_MASK 0x1f00 /* Prescaler Setting (1-32) */
  91. #define SCR_BRPS(i) (((i) - 1) << 8)
  92. #define SCR_BRDV_MASK 0x0007 /* Baud Rate Generator's Division Ratio */
  93. #define SCR_BRDV_DIV_2 0x0000
  94. #define SCR_BRDV_DIV_4 0x0001
  95. #define SCR_BRDV_DIV_8 0x0002
  96. #define SCR_BRDV_DIV_16 0x0003
  97. #define SCR_BRDV_DIV_32 0x0004
  98. #define SCR_BRDV_DIV_1 0x0007
  99. /* CTR */
  100. #define CTR_TSCKIZ_MASK 0xc0000000 /* Transmit Clock I/O Polarity Select */
  101. #define CTR_TSCKIZ_SCK 0x80000000 /* Disable SCK when TX disabled */
  102. #define CTR_TSCKIZ_POL_SHIFT 30 /* Transmit Clock Polarity */
  103. #define CTR_RSCKIZ_MASK 0x30000000 /* Receive Clock Polarity Select */
  104. #define CTR_RSCKIZ_SCK 0x20000000 /* Must match CTR_TSCKIZ_SCK */
  105. #define CTR_RSCKIZ_POL_SHIFT 28 /* Receive Clock Polarity */
  106. #define CTR_TEDG_SHIFT 27 /* Transmit Timing (1 = falling edge) */
  107. #define CTR_REDG_SHIFT 26 /* Receive Timing (1 = falling edge) */
  108. #define CTR_TXDIZ_MASK 0x00c00000 /* Pin Output When TX is Disabled */
  109. #define CTR_TXDIZ_LOW 0x00000000 /* 0 */
  110. #define CTR_TXDIZ_HIGH 0x00400000 /* 1 */
  111. #define CTR_TXDIZ_HIZ 0x00800000 /* High-impedance */
  112. #define CTR_TSCKE 0x00008000 /* Transmit Serial Clock Output Enable */
  113. #define CTR_TFSE 0x00004000 /* Transmit Frame Sync Signal Output Enable */
  114. #define CTR_TXE 0x00000200 /* Transmit Enable */
  115. #define CTR_RXE 0x00000100 /* Receive Enable */
  116. /* FCTR */
  117. #define FCTR_TFWM_MASK 0xe0000000 /* Transmit FIFO Watermark */
  118. #define FCTR_TFWM_64 0x00000000 /* Transfer Request when 64 empty stages */
  119. #define FCTR_TFWM_32 0x20000000 /* Transfer Request when 32 empty stages */
  120. #define FCTR_TFWM_24 0x40000000 /* Transfer Request when 24 empty stages */
  121. #define FCTR_TFWM_16 0x60000000 /* Transfer Request when 16 empty stages */
  122. #define FCTR_TFWM_12 0x80000000 /* Transfer Request when 12 empty stages */
  123. #define FCTR_TFWM_8 0xa0000000 /* Transfer Request when 8 empty stages */
  124. #define FCTR_TFWM_4 0xc0000000 /* Transfer Request when 4 empty stages */
  125. #define FCTR_TFWM_1 0xe0000000 /* Transfer Request when 1 empty stage */
  126. #define FCTR_TFUA_MASK 0x07f00000 /* Transmit FIFO Usable Area */
  127. #define FCTR_TFUA_SHIFT 20
  128. #define FCTR_TFUA(i) ((i) << FCTR_TFUA_SHIFT)
  129. #define FCTR_RFWM_MASK 0x0000e000 /* Receive FIFO Watermark */
  130. #define FCTR_RFWM_1 0x00000000 /* Transfer Request when 1 valid stages */
  131. #define FCTR_RFWM_4 0x00002000 /* Transfer Request when 4 valid stages */
  132. #define FCTR_RFWM_8 0x00004000 /* Transfer Request when 8 valid stages */
  133. #define FCTR_RFWM_16 0x00006000 /* Transfer Request when 16 valid stages */
  134. #define FCTR_RFWM_32 0x00008000 /* Transfer Request when 32 valid stages */
  135. #define FCTR_RFWM_64 0x0000a000 /* Transfer Request when 64 valid stages */
  136. #define FCTR_RFWM_128 0x0000c000 /* Transfer Request when 128 valid stages */
  137. #define FCTR_RFWM_256 0x0000e000 /* Transfer Request when 256 valid stages */
  138. #define FCTR_RFUA_MASK 0x00001ff0 /* Receive FIFO Usable Area (0x40 = full) */
  139. #define FCTR_RFUA_SHIFT 4
  140. #define FCTR_RFUA(i) ((i) << FCTR_RFUA_SHIFT)
  141. /* STR */
  142. #define STR_TFEMP 0x20000000 /* Transmit FIFO Empty */
  143. #define STR_TDREQ 0x10000000 /* Transmit Data Transfer Request */
  144. #define STR_TEOF 0x00800000 /* Frame Transmission End */
  145. #define STR_TFSERR 0x00200000 /* Transmit Frame Synchronization Error */
  146. #define STR_TFOVF 0x00100000 /* Transmit FIFO Overflow */
  147. #define STR_TFUDF 0x00080000 /* Transmit FIFO Underflow */
  148. #define STR_RFFUL 0x00002000 /* Receive FIFO Full */
  149. #define STR_RDREQ 0x00001000 /* Receive Data Transfer Request */
  150. #define STR_REOF 0x00000080 /* Frame Reception End */
  151. #define STR_RFSERR 0x00000020 /* Receive Frame Synchronization Error */
  152. #define STR_RFUDF 0x00000010 /* Receive FIFO Underflow */
  153. #define STR_RFOVF 0x00000008 /* Receive FIFO Overflow */
  154. /* IER */
  155. #define IER_TDMAE 0x80000000 /* Transmit Data DMA Transfer Req. Enable */
  156. #define IER_TFEMPE 0x20000000 /* Transmit FIFO Empty Enable */
  157. #define IER_TDREQE 0x10000000 /* Transmit Data Transfer Request Enable */
  158. #define IER_TEOFE 0x00800000 /* Frame Transmission End Enable */
  159. #define IER_TFSERRE 0x00200000 /* Transmit Frame Sync Error Enable */
  160. #define IER_TFOVFE 0x00100000 /* Transmit FIFO Overflow Enable */
  161. #define IER_TFUDFE 0x00080000 /* Transmit FIFO Underflow Enable */
  162. #define IER_RDMAE 0x00008000 /* Receive Data DMA Transfer Req. Enable */
  163. #define IER_RFFULE 0x00002000 /* Receive FIFO Full Enable */
  164. #define IER_RDREQE 0x00001000 /* Receive Data Transfer Request Enable */
  165. #define IER_REOFE 0x00000080 /* Frame Reception End Enable */
  166. #define IER_RFSERRE 0x00000020 /* Receive Frame Sync Error Enable */
  167. #define IER_RFUDFE 0x00000010 /* Receive FIFO Underflow Enable */
  168. #define IER_RFOVFE 0x00000008 /* Receive FIFO Overflow Enable */
  169. static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
  170. {
  171. switch (reg_offs) {
  172. case TSCR:
  173. case RSCR:
  174. return ioread16(p->mapbase + reg_offs);
  175. default:
  176. return ioread32(p->mapbase + reg_offs);
  177. }
  178. }
  179. static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
  180. u32 value)
  181. {
  182. switch (reg_offs) {
  183. case TSCR:
  184. case RSCR:
  185. iowrite16(value, p->mapbase + reg_offs);
  186. break;
  187. default:
  188. iowrite32(value, p->mapbase + reg_offs);
  189. break;
  190. }
  191. }
  192. static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
  193. u32 clr, u32 set)
  194. {
  195. u32 mask = clr | set;
  196. u32 data;
  197. int k;
  198. data = sh_msiof_read(p, CTR);
  199. data &= ~clr;
  200. data |= set;
  201. sh_msiof_write(p, CTR, data);
  202. for (k = 100; k > 0; k--) {
  203. if ((sh_msiof_read(p, CTR) & mask) == set)
  204. break;
  205. udelay(10);
  206. }
  207. return k > 0 ? 0 : -ETIMEDOUT;
  208. }
  209. static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
  210. {
  211. struct sh_msiof_spi_priv *p = data;
  212. /* just disable the interrupt and wake up */
  213. sh_msiof_write(p, IER, 0);
  214. complete(&p->done);
  215. return IRQ_HANDLED;
  216. }
  217. static struct {
  218. unsigned short div;
  219. unsigned short brdv;
  220. } const sh_msiof_spi_div_table[] = {
  221. { 1, SCR_BRDV_DIV_1 },
  222. { 2, SCR_BRDV_DIV_2 },
  223. { 4, SCR_BRDV_DIV_4 },
  224. { 8, SCR_BRDV_DIV_8 },
  225. { 16, SCR_BRDV_DIV_16 },
  226. { 32, SCR_BRDV_DIV_32 },
  227. };
  228. static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
  229. unsigned long parent_rate, u32 spi_hz)
  230. {
  231. unsigned long div = 1024;
  232. u32 brps, scr;
  233. size_t k;
  234. if (!WARN_ON(!spi_hz || !parent_rate))
  235. div = DIV_ROUND_UP(parent_rate, spi_hz);
  236. for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_div_table); k++) {
  237. brps = DIV_ROUND_UP(div, sh_msiof_spi_div_table[k].div);
  238. if (brps <= 32) /* max of brdv is 32 */
  239. break;
  240. }
  241. k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_div_table) - 1);
  242. scr = sh_msiof_spi_div_table[k].brdv | SCR_BRPS(brps);
  243. sh_msiof_write(p, TSCR, scr);
  244. if (!(p->chipdata->master_flags & SPI_MASTER_MUST_TX))
  245. sh_msiof_write(p, RSCR, scr);
  246. }
  247. static u32 sh_msiof_get_delay_bit(u32 dtdl_or_syncdl)
  248. {
  249. /*
  250. * DTDL/SYNCDL bit : p->info->dtdl or p->info->syncdl
  251. * b'000 : 0
  252. * b'001 : 100
  253. * b'010 : 200
  254. * b'011 (SYNCDL only) : 300
  255. * b'101 : 50
  256. * b'110 : 150
  257. */
  258. if (dtdl_or_syncdl % 100)
  259. return dtdl_or_syncdl / 100 + 5;
  260. else
  261. return dtdl_or_syncdl / 100;
  262. }
  263. static u32 sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv *p)
  264. {
  265. u32 val;
  266. if (!p->info)
  267. return 0;
  268. /* check if DTDL and SYNCDL is allowed value */
  269. if (p->info->dtdl > 200 || p->info->syncdl > 300) {
  270. dev_warn(&p->pdev->dev, "DTDL or SYNCDL is too large\n");
  271. return 0;
  272. }
  273. /* check if the sum of DTDL and SYNCDL becomes an integer value */
  274. if ((p->info->dtdl + p->info->syncdl) % 100) {
  275. dev_warn(&p->pdev->dev, "the sum of DTDL/SYNCDL is not good\n");
  276. return 0;
  277. }
  278. val = sh_msiof_get_delay_bit(p->info->dtdl) << MDR1_DTDL_SHIFT;
  279. val |= sh_msiof_get_delay_bit(p->info->syncdl) << MDR1_SYNCDL_SHIFT;
  280. return val;
  281. }
  282. static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
  283. u32 cpol, u32 cpha,
  284. u32 tx_hi_z, u32 lsb_first, u32 cs_high)
  285. {
  286. u32 tmp;
  287. int edge;
  288. /*
  289. * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
  290. * 0 0 10 10 1 1
  291. * 0 1 10 10 0 0
  292. * 1 0 11 11 0 0
  293. * 1 1 11 11 1 1
  294. */
  295. tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP;
  296. tmp |= !cs_high << MDR1_SYNCAC_SHIFT;
  297. tmp |= lsb_first << MDR1_BITLSB_SHIFT;
  298. tmp |= sh_msiof_spi_get_dtdl_and_syncdl(p);
  299. sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON);
  300. if (p->chipdata->master_flags & SPI_MASTER_MUST_TX) {
  301. /* These bits are reserved if RX needs TX */
  302. tmp &= ~0x0000ffff;
  303. }
  304. sh_msiof_write(p, RMDR1, tmp);
  305. tmp = 0;
  306. tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT;
  307. tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT;
  308. edge = cpol ^ !cpha;
  309. tmp |= edge << CTR_TEDG_SHIFT;
  310. tmp |= edge << CTR_REDG_SHIFT;
  311. tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW;
  312. sh_msiof_write(p, CTR, tmp);
  313. }
  314. static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
  315. const void *tx_buf, void *rx_buf,
  316. u32 bits, u32 words)
  317. {
  318. u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words);
  319. if (tx_buf || (p->chipdata->master_flags & SPI_MASTER_MUST_TX))
  320. sh_msiof_write(p, TMDR2, dr2);
  321. else
  322. sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1);
  323. if (rx_buf)
  324. sh_msiof_write(p, RMDR2, dr2);
  325. }
  326. static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
  327. {
  328. sh_msiof_write(p, STR, sh_msiof_read(p, STR));
  329. }
  330. static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
  331. const void *tx_buf, int words, int fs)
  332. {
  333. const u8 *buf_8 = tx_buf;
  334. int k;
  335. for (k = 0; k < words; k++)
  336. sh_msiof_write(p, TFDR, buf_8[k] << fs);
  337. }
  338. static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
  339. const void *tx_buf, int words, int fs)
  340. {
  341. const u16 *buf_16 = tx_buf;
  342. int k;
  343. for (k = 0; k < words; k++)
  344. sh_msiof_write(p, TFDR, buf_16[k] << fs);
  345. }
  346. static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
  347. const void *tx_buf, int words, int fs)
  348. {
  349. const u16 *buf_16 = tx_buf;
  350. int k;
  351. for (k = 0; k < words; k++)
  352. sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
  353. }
  354. static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
  355. const void *tx_buf, int words, int fs)
  356. {
  357. const u32 *buf_32 = tx_buf;
  358. int k;
  359. for (k = 0; k < words; k++)
  360. sh_msiof_write(p, TFDR, buf_32[k] << fs);
  361. }
  362. static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
  363. const void *tx_buf, int words, int fs)
  364. {
  365. const u32 *buf_32 = tx_buf;
  366. int k;
  367. for (k = 0; k < words; k++)
  368. sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
  369. }
  370. static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
  371. const void *tx_buf, int words, int fs)
  372. {
  373. const u32 *buf_32 = tx_buf;
  374. int k;
  375. for (k = 0; k < words; k++)
  376. sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
  377. }
  378. static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
  379. const void *tx_buf, int words, int fs)
  380. {
  381. const u32 *buf_32 = tx_buf;
  382. int k;
  383. for (k = 0; k < words; k++)
  384. sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
  385. }
  386. static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
  387. void *rx_buf, int words, int fs)
  388. {
  389. u8 *buf_8 = rx_buf;
  390. int k;
  391. for (k = 0; k < words; k++)
  392. buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
  393. }
  394. static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
  395. void *rx_buf, int words, int fs)
  396. {
  397. u16 *buf_16 = rx_buf;
  398. int k;
  399. for (k = 0; k < words; k++)
  400. buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
  401. }
  402. static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
  403. void *rx_buf, int words, int fs)
  404. {
  405. u16 *buf_16 = rx_buf;
  406. int k;
  407. for (k = 0; k < words; k++)
  408. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
  409. }
  410. static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
  411. void *rx_buf, int words, int fs)
  412. {
  413. u32 *buf_32 = rx_buf;
  414. int k;
  415. for (k = 0; k < words; k++)
  416. buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
  417. }
  418. static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
  419. void *rx_buf, int words, int fs)
  420. {
  421. u32 *buf_32 = rx_buf;
  422. int k;
  423. for (k = 0; k < words; k++)
  424. put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
  425. }
  426. static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
  427. void *rx_buf, int words, int fs)
  428. {
  429. u32 *buf_32 = rx_buf;
  430. int k;
  431. for (k = 0; k < words; k++)
  432. buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
  433. }
  434. static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
  435. void *rx_buf, int words, int fs)
  436. {
  437. u32 *buf_32 = rx_buf;
  438. int k;
  439. for (k = 0; k < words; k++)
  440. put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
  441. }
  442. static int sh_msiof_spi_setup(struct spi_device *spi)
  443. {
  444. struct device_node *np = spi->master->dev.of_node;
  445. struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
  446. pm_runtime_get_sync(&p->pdev->dev);
  447. if (!np) {
  448. /*
  449. * Use spi->controller_data for CS (same strategy as spi_gpio),
  450. * if any. otherwise let HW control CS
  451. */
  452. spi->cs_gpio = (uintptr_t)spi->controller_data;
  453. }
  454. /* Configure pins before deasserting CS */
  455. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  456. !!(spi->mode & SPI_CPHA),
  457. !!(spi->mode & SPI_3WIRE),
  458. !!(spi->mode & SPI_LSB_FIRST),
  459. !!(spi->mode & SPI_CS_HIGH));
  460. if (spi->cs_gpio >= 0)
  461. gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
  462. pm_runtime_put(&p->pdev->dev);
  463. return 0;
  464. }
  465. static int sh_msiof_prepare_message(struct spi_master *master,
  466. struct spi_message *msg)
  467. {
  468. struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
  469. const struct spi_device *spi = msg->spi;
  470. /* Configure pins before asserting CS */
  471. sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
  472. !!(spi->mode & SPI_CPHA),
  473. !!(spi->mode & SPI_3WIRE),
  474. !!(spi->mode & SPI_LSB_FIRST),
  475. !!(spi->mode & SPI_CS_HIGH));
  476. return 0;
  477. }
  478. static int sh_msiof_spi_start(struct sh_msiof_spi_priv *p, void *rx_buf)
  479. {
  480. int ret;
  481. /* setup clock and rx/tx signals */
  482. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
  483. if (rx_buf && !ret)
  484. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
  485. if (!ret)
  486. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
  487. /* start by setting frame bit */
  488. if (!ret)
  489. ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
  490. return ret;
  491. }
  492. static int sh_msiof_spi_stop(struct sh_msiof_spi_priv *p, void *rx_buf)
  493. {
  494. int ret;
  495. /* shut down frame, rx/tx and clock signals */
  496. ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
  497. if (!ret)
  498. ret = sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
  499. if (rx_buf && !ret)
  500. ret = sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
  501. if (!ret)
  502. ret = sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
  503. return ret;
  504. }
  505. static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
  506. void (*tx_fifo)(struct sh_msiof_spi_priv *,
  507. const void *, int, int),
  508. void (*rx_fifo)(struct sh_msiof_spi_priv *,
  509. void *, int, int),
  510. const void *tx_buf, void *rx_buf,
  511. int words, int bits)
  512. {
  513. int fifo_shift;
  514. int ret;
  515. /* limit maximum word transfer to rx/tx fifo size */
  516. if (tx_buf)
  517. words = min_t(int, words, p->tx_fifo_size);
  518. if (rx_buf)
  519. words = min_t(int, words, p->rx_fifo_size);
  520. /* the fifo contents need shifting */
  521. fifo_shift = 32 - bits;
  522. /* default FIFO watermarks for PIO */
  523. sh_msiof_write(p, FCTR, 0);
  524. /* setup msiof transfer mode registers */
  525. sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
  526. sh_msiof_write(p, IER, IER_TEOFE | IER_REOFE);
  527. /* write tx fifo */
  528. if (tx_buf)
  529. tx_fifo(p, tx_buf, words, fifo_shift);
  530. reinit_completion(&p->done);
  531. ret = sh_msiof_spi_start(p, rx_buf);
  532. if (ret) {
  533. dev_err(&p->pdev->dev, "failed to start hardware\n");
  534. goto stop_ier;
  535. }
  536. /* wait for tx fifo to be emptied / rx fifo to be filled */
  537. if (!wait_for_completion_timeout(&p->done, HZ)) {
  538. dev_err(&p->pdev->dev, "PIO timeout\n");
  539. ret = -ETIMEDOUT;
  540. goto stop_reset;
  541. }
  542. /* read rx fifo */
  543. if (rx_buf)
  544. rx_fifo(p, rx_buf, words, fifo_shift);
  545. /* clear status bits */
  546. sh_msiof_reset_str(p);
  547. ret = sh_msiof_spi_stop(p, rx_buf);
  548. if (ret) {
  549. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  550. return ret;
  551. }
  552. return words;
  553. stop_reset:
  554. sh_msiof_reset_str(p);
  555. sh_msiof_spi_stop(p, rx_buf);
  556. stop_ier:
  557. sh_msiof_write(p, IER, 0);
  558. return ret;
  559. }
  560. static void sh_msiof_dma_complete(void *arg)
  561. {
  562. struct sh_msiof_spi_priv *p = arg;
  563. sh_msiof_write(p, IER, 0);
  564. complete(&p->done);
  565. }
  566. static int sh_msiof_dma_once(struct sh_msiof_spi_priv *p, const void *tx,
  567. void *rx, unsigned int len)
  568. {
  569. u32 ier_bits = 0;
  570. struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
  571. dma_cookie_t cookie;
  572. int ret;
  573. /* First prepare and submit the DMA request(s), as this may fail */
  574. if (rx) {
  575. ier_bits |= IER_RDREQE | IER_RDMAE;
  576. desc_rx = dmaengine_prep_slave_single(p->master->dma_rx,
  577. p->rx_dma_addr, len, DMA_FROM_DEVICE,
  578. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  579. if (!desc_rx)
  580. return -EAGAIN;
  581. desc_rx->callback = sh_msiof_dma_complete;
  582. desc_rx->callback_param = p;
  583. cookie = dmaengine_submit(desc_rx);
  584. if (dma_submit_error(cookie))
  585. return cookie;
  586. }
  587. if (tx) {
  588. ier_bits |= IER_TDREQE | IER_TDMAE;
  589. dma_sync_single_for_device(p->master->dma_tx->device->dev,
  590. p->tx_dma_addr, len, DMA_TO_DEVICE);
  591. desc_tx = dmaengine_prep_slave_single(p->master->dma_tx,
  592. p->tx_dma_addr, len, DMA_TO_DEVICE,
  593. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  594. if (!desc_tx) {
  595. ret = -EAGAIN;
  596. goto no_dma_tx;
  597. }
  598. if (rx) {
  599. /* No callback */
  600. desc_tx->callback = NULL;
  601. } else {
  602. desc_tx->callback = sh_msiof_dma_complete;
  603. desc_tx->callback_param = p;
  604. }
  605. cookie = dmaengine_submit(desc_tx);
  606. if (dma_submit_error(cookie)) {
  607. ret = cookie;
  608. goto no_dma_tx;
  609. }
  610. }
  611. /* 1 stage FIFO watermarks for DMA */
  612. sh_msiof_write(p, FCTR, FCTR_TFWM_1 | FCTR_RFWM_1);
  613. /* setup msiof transfer mode registers (32-bit words) */
  614. sh_msiof_spi_set_mode_regs(p, tx, rx, 32, len / 4);
  615. sh_msiof_write(p, IER, ier_bits);
  616. reinit_completion(&p->done);
  617. /* Now start DMA */
  618. if (rx)
  619. dma_async_issue_pending(p->master->dma_rx);
  620. if (tx)
  621. dma_async_issue_pending(p->master->dma_tx);
  622. ret = sh_msiof_spi_start(p, rx);
  623. if (ret) {
  624. dev_err(&p->pdev->dev, "failed to start hardware\n");
  625. goto stop_dma;
  626. }
  627. /* wait for tx fifo to be emptied / rx fifo to be filled */
  628. if (!wait_for_completion_timeout(&p->done, HZ)) {
  629. dev_err(&p->pdev->dev, "DMA timeout\n");
  630. ret = -ETIMEDOUT;
  631. goto stop_reset;
  632. }
  633. /* clear status bits */
  634. sh_msiof_reset_str(p);
  635. ret = sh_msiof_spi_stop(p, rx);
  636. if (ret) {
  637. dev_err(&p->pdev->dev, "failed to shut down hardware\n");
  638. return ret;
  639. }
  640. if (rx)
  641. dma_sync_single_for_cpu(p->master->dma_rx->device->dev,
  642. p->rx_dma_addr, len,
  643. DMA_FROM_DEVICE);
  644. return 0;
  645. stop_reset:
  646. sh_msiof_reset_str(p);
  647. sh_msiof_spi_stop(p, rx);
  648. stop_dma:
  649. if (tx)
  650. dmaengine_terminate_all(p->master->dma_tx);
  651. no_dma_tx:
  652. if (rx)
  653. dmaengine_terminate_all(p->master->dma_rx);
  654. sh_msiof_write(p, IER, 0);
  655. return ret;
  656. }
  657. static void copy_bswap32(u32 *dst, const u32 *src, unsigned int words)
  658. {
  659. /* src or dst can be unaligned, but not both */
  660. if ((unsigned long)src & 3) {
  661. while (words--) {
  662. *dst++ = swab32(get_unaligned(src));
  663. src++;
  664. }
  665. } else if ((unsigned long)dst & 3) {
  666. while (words--) {
  667. put_unaligned(swab32(*src++), dst);
  668. dst++;
  669. }
  670. } else {
  671. while (words--)
  672. *dst++ = swab32(*src++);
  673. }
  674. }
  675. static void copy_wswap32(u32 *dst, const u32 *src, unsigned int words)
  676. {
  677. /* src or dst can be unaligned, but not both */
  678. if ((unsigned long)src & 3) {
  679. while (words--) {
  680. *dst++ = swahw32(get_unaligned(src));
  681. src++;
  682. }
  683. } else if ((unsigned long)dst & 3) {
  684. while (words--) {
  685. put_unaligned(swahw32(*src++), dst);
  686. dst++;
  687. }
  688. } else {
  689. while (words--)
  690. *dst++ = swahw32(*src++);
  691. }
  692. }
  693. static void copy_plain32(u32 *dst, const u32 *src, unsigned int words)
  694. {
  695. memcpy(dst, src, words * 4);
  696. }
  697. static int sh_msiof_transfer_one(struct spi_master *master,
  698. struct spi_device *spi,
  699. struct spi_transfer *t)
  700. {
  701. struct sh_msiof_spi_priv *p = spi_master_get_devdata(master);
  702. void (*copy32)(u32 *, const u32 *, unsigned int);
  703. void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
  704. void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
  705. const void *tx_buf = t->tx_buf;
  706. void *rx_buf = t->rx_buf;
  707. unsigned int len = t->len;
  708. unsigned int bits = t->bits_per_word;
  709. unsigned int bytes_per_word;
  710. unsigned int words;
  711. int n;
  712. bool swab;
  713. int ret;
  714. /* setup clocks (clock already enabled in chipselect()) */
  715. sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz);
  716. while (master->dma_tx && len > 15) {
  717. /*
  718. * DMA supports 32-bit words only, hence pack 8-bit and 16-bit
  719. * words, with byte resp. word swapping.
  720. */
  721. unsigned int l = min(len, MAX_WDLEN * 4);
  722. if (bits <= 8) {
  723. if (l & 3)
  724. break;
  725. copy32 = copy_bswap32;
  726. } else if (bits <= 16) {
  727. if (l & 1)
  728. break;
  729. copy32 = copy_wswap32;
  730. } else {
  731. copy32 = copy_plain32;
  732. }
  733. if (tx_buf)
  734. copy32(p->tx_dma_page, tx_buf, l / 4);
  735. ret = sh_msiof_dma_once(p, tx_buf, rx_buf, l);
  736. if (ret == -EAGAIN) {
  737. pr_warn_once("%s %s: DMA not available, falling back to PIO\n",
  738. dev_driver_string(&p->pdev->dev),
  739. dev_name(&p->pdev->dev));
  740. break;
  741. }
  742. if (ret)
  743. return ret;
  744. if (rx_buf) {
  745. copy32(rx_buf, p->rx_dma_page, l / 4);
  746. rx_buf += l;
  747. }
  748. if (tx_buf)
  749. tx_buf += l;
  750. len -= l;
  751. if (!len)
  752. return 0;
  753. }
  754. if (bits <= 8 && len > 15 && !(len & 3)) {
  755. bits = 32;
  756. swab = true;
  757. } else {
  758. swab = false;
  759. }
  760. /* setup bytes per word and fifo read/write functions */
  761. if (bits <= 8) {
  762. bytes_per_word = 1;
  763. tx_fifo = sh_msiof_spi_write_fifo_8;
  764. rx_fifo = sh_msiof_spi_read_fifo_8;
  765. } else if (bits <= 16) {
  766. bytes_per_word = 2;
  767. if ((unsigned long)tx_buf & 0x01)
  768. tx_fifo = sh_msiof_spi_write_fifo_16u;
  769. else
  770. tx_fifo = sh_msiof_spi_write_fifo_16;
  771. if ((unsigned long)rx_buf & 0x01)
  772. rx_fifo = sh_msiof_spi_read_fifo_16u;
  773. else
  774. rx_fifo = sh_msiof_spi_read_fifo_16;
  775. } else if (swab) {
  776. bytes_per_word = 4;
  777. if ((unsigned long)tx_buf & 0x03)
  778. tx_fifo = sh_msiof_spi_write_fifo_s32u;
  779. else
  780. tx_fifo = sh_msiof_spi_write_fifo_s32;
  781. if ((unsigned long)rx_buf & 0x03)
  782. rx_fifo = sh_msiof_spi_read_fifo_s32u;
  783. else
  784. rx_fifo = sh_msiof_spi_read_fifo_s32;
  785. } else {
  786. bytes_per_word = 4;
  787. if ((unsigned long)tx_buf & 0x03)
  788. tx_fifo = sh_msiof_spi_write_fifo_32u;
  789. else
  790. tx_fifo = sh_msiof_spi_write_fifo_32;
  791. if ((unsigned long)rx_buf & 0x03)
  792. rx_fifo = sh_msiof_spi_read_fifo_32u;
  793. else
  794. rx_fifo = sh_msiof_spi_read_fifo_32;
  795. }
  796. /* transfer in fifo sized chunks */
  797. words = len / bytes_per_word;
  798. while (words > 0) {
  799. n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo, tx_buf, rx_buf,
  800. words, bits);
  801. if (n < 0)
  802. return n;
  803. if (tx_buf)
  804. tx_buf += n * bytes_per_word;
  805. if (rx_buf)
  806. rx_buf += n * bytes_per_word;
  807. words -= n;
  808. }
  809. return 0;
  810. }
  811. static const struct sh_msiof_chipdata sh_data = {
  812. .tx_fifo_size = 64,
  813. .rx_fifo_size = 64,
  814. .master_flags = 0,
  815. };
  816. static const struct sh_msiof_chipdata r8a779x_data = {
  817. .tx_fifo_size = 64,
  818. .rx_fifo_size = 256,
  819. .master_flags = SPI_MASTER_MUST_TX,
  820. };
  821. static const struct of_device_id sh_msiof_match[] = {
  822. { .compatible = "renesas,sh-msiof", .data = &sh_data },
  823. { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data },
  824. { .compatible = "renesas,msiof-r8a7790", .data = &r8a779x_data },
  825. { .compatible = "renesas,msiof-r8a7791", .data = &r8a779x_data },
  826. { .compatible = "renesas,msiof-r8a7792", .data = &r8a779x_data },
  827. { .compatible = "renesas,msiof-r8a7793", .data = &r8a779x_data },
  828. { .compatible = "renesas,msiof-r8a7794", .data = &r8a779x_data },
  829. {},
  830. };
  831. MODULE_DEVICE_TABLE(of, sh_msiof_match);
  832. #ifdef CONFIG_OF
  833. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  834. {
  835. struct sh_msiof_spi_info *info;
  836. struct device_node *np = dev->of_node;
  837. u32 num_cs = 1;
  838. info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
  839. if (!info)
  840. return NULL;
  841. /* Parse the MSIOF properties */
  842. of_property_read_u32(np, "num-cs", &num_cs);
  843. of_property_read_u32(np, "renesas,tx-fifo-size",
  844. &info->tx_fifo_override);
  845. of_property_read_u32(np, "renesas,rx-fifo-size",
  846. &info->rx_fifo_override);
  847. of_property_read_u32(np, "renesas,dtdl", &info->dtdl);
  848. of_property_read_u32(np, "renesas,syncdl", &info->syncdl);
  849. info->num_chipselect = num_cs;
  850. return info;
  851. }
  852. #else
  853. static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
  854. {
  855. return NULL;
  856. }
  857. #endif
  858. static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev,
  859. enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr)
  860. {
  861. dma_cap_mask_t mask;
  862. struct dma_chan *chan;
  863. struct dma_slave_config cfg;
  864. int ret;
  865. dma_cap_zero(mask);
  866. dma_cap_set(DMA_SLAVE, mask);
  867. chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
  868. (void *)(unsigned long)id, dev,
  869. dir == DMA_MEM_TO_DEV ? "tx" : "rx");
  870. if (!chan) {
  871. dev_warn(dev, "dma_request_slave_channel_compat failed\n");
  872. return NULL;
  873. }
  874. memset(&cfg, 0, sizeof(cfg));
  875. cfg.direction = dir;
  876. if (dir == DMA_MEM_TO_DEV) {
  877. cfg.dst_addr = port_addr;
  878. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  879. } else {
  880. cfg.src_addr = port_addr;
  881. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  882. }
  883. ret = dmaengine_slave_config(chan, &cfg);
  884. if (ret) {
  885. dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
  886. dma_release_channel(chan);
  887. return NULL;
  888. }
  889. return chan;
  890. }
  891. static int sh_msiof_request_dma(struct sh_msiof_spi_priv *p)
  892. {
  893. struct platform_device *pdev = p->pdev;
  894. struct device *dev = &pdev->dev;
  895. const struct sh_msiof_spi_info *info = dev_get_platdata(dev);
  896. unsigned int dma_tx_id, dma_rx_id;
  897. const struct resource *res;
  898. struct spi_master *master;
  899. struct device *tx_dev, *rx_dev;
  900. if (dev->of_node) {
  901. /* In the OF case we will get the slave IDs from the DT */
  902. dma_tx_id = 0;
  903. dma_rx_id = 0;
  904. } else if (info && info->dma_tx_id && info->dma_rx_id) {
  905. dma_tx_id = info->dma_tx_id;
  906. dma_rx_id = info->dma_rx_id;
  907. } else {
  908. /* The driver assumes no error */
  909. return 0;
  910. }
  911. /* The DMA engine uses the second register set, if present */
  912. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  913. if (!res)
  914. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  915. master = p->master;
  916. master->dma_tx = sh_msiof_request_dma_chan(dev, DMA_MEM_TO_DEV,
  917. dma_tx_id,
  918. res->start + TFDR);
  919. if (!master->dma_tx)
  920. return -ENODEV;
  921. master->dma_rx = sh_msiof_request_dma_chan(dev, DMA_DEV_TO_MEM,
  922. dma_rx_id,
  923. res->start + RFDR);
  924. if (!master->dma_rx)
  925. goto free_tx_chan;
  926. p->tx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  927. if (!p->tx_dma_page)
  928. goto free_rx_chan;
  929. p->rx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
  930. if (!p->rx_dma_page)
  931. goto free_tx_page;
  932. tx_dev = master->dma_tx->device->dev;
  933. p->tx_dma_addr = dma_map_single(tx_dev, p->tx_dma_page, PAGE_SIZE,
  934. DMA_TO_DEVICE);
  935. if (dma_mapping_error(tx_dev, p->tx_dma_addr))
  936. goto free_rx_page;
  937. rx_dev = master->dma_rx->device->dev;
  938. p->rx_dma_addr = dma_map_single(rx_dev, p->rx_dma_page, PAGE_SIZE,
  939. DMA_FROM_DEVICE);
  940. if (dma_mapping_error(rx_dev, p->rx_dma_addr))
  941. goto unmap_tx_page;
  942. dev_info(dev, "DMA available");
  943. return 0;
  944. unmap_tx_page:
  945. dma_unmap_single(tx_dev, p->tx_dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
  946. free_rx_page:
  947. free_page((unsigned long)p->rx_dma_page);
  948. free_tx_page:
  949. free_page((unsigned long)p->tx_dma_page);
  950. free_rx_chan:
  951. dma_release_channel(master->dma_rx);
  952. free_tx_chan:
  953. dma_release_channel(master->dma_tx);
  954. master->dma_tx = NULL;
  955. return -ENODEV;
  956. }
  957. static void sh_msiof_release_dma(struct sh_msiof_spi_priv *p)
  958. {
  959. struct spi_master *master = p->master;
  960. struct device *dev;
  961. if (!master->dma_tx)
  962. return;
  963. dev = &p->pdev->dev;
  964. dma_unmap_single(master->dma_rx->device->dev, p->rx_dma_addr,
  965. PAGE_SIZE, DMA_FROM_DEVICE);
  966. dma_unmap_single(master->dma_tx->device->dev, p->tx_dma_addr,
  967. PAGE_SIZE, DMA_TO_DEVICE);
  968. free_page((unsigned long)p->rx_dma_page);
  969. free_page((unsigned long)p->tx_dma_page);
  970. dma_release_channel(master->dma_rx);
  971. dma_release_channel(master->dma_tx);
  972. }
  973. static int sh_msiof_spi_probe(struct platform_device *pdev)
  974. {
  975. struct resource *r;
  976. struct spi_master *master;
  977. const struct of_device_id *of_id;
  978. struct sh_msiof_spi_priv *p;
  979. int i;
  980. int ret;
  981. master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
  982. if (master == NULL) {
  983. dev_err(&pdev->dev, "failed to allocate spi master\n");
  984. return -ENOMEM;
  985. }
  986. p = spi_master_get_devdata(master);
  987. platform_set_drvdata(pdev, p);
  988. p->master = master;
  989. of_id = of_match_device(sh_msiof_match, &pdev->dev);
  990. if (of_id) {
  991. p->chipdata = of_id->data;
  992. p->info = sh_msiof_spi_parse_dt(&pdev->dev);
  993. } else {
  994. p->chipdata = (const void *)pdev->id_entry->driver_data;
  995. p->info = dev_get_platdata(&pdev->dev);
  996. }
  997. if (!p->info) {
  998. dev_err(&pdev->dev, "failed to obtain device info\n");
  999. ret = -ENXIO;
  1000. goto err1;
  1001. }
  1002. init_completion(&p->done);
  1003. p->clk = devm_clk_get(&pdev->dev, NULL);
  1004. if (IS_ERR(p->clk)) {
  1005. dev_err(&pdev->dev, "cannot get clock\n");
  1006. ret = PTR_ERR(p->clk);
  1007. goto err1;
  1008. }
  1009. i = platform_get_irq(pdev, 0);
  1010. if (i < 0) {
  1011. dev_err(&pdev->dev, "cannot get platform IRQ\n");
  1012. ret = -ENOENT;
  1013. goto err1;
  1014. }
  1015. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1016. p->mapbase = devm_ioremap_resource(&pdev->dev, r);
  1017. if (IS_ERR(p->mapbase)) {
  1018. ret = PTR_ERR(p->mapbase);
  1019. goto err1;
  1020. }
  1021. ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
  1022. dev_name(&pdev->dev), p);
  1023. if (ret) {
  1024. dev_err(&pdev->dev, "unable to request irq\n");
  1025. goto err1;
  1026. }
  1027. p->pdev = pdev;
  1028. pm_runtime_enable(&pdev->dev);
  1029. /* Platform data may override FIFO sizes */
  1030. p->tx_fifo_size = p->chipdata->tx_fifo_size;
  1031. p->rx_fifo_size = p->chipdata->rx_fifo_size;
  1032. if (p->info->tx_fifo_override)
  1033. p->tx_fifo_size = p->info->tx_fifo_override;
  1034. if (p->info->rx_fifo_override)
  1035. p->rx_fifo_size = p->info->rx_fifo_override;
  1036. /* init master code */
  1037. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  1038. master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
  1039. master->flags = p->chipdata->master_flags;
  1040. master->bus_num = pdev->id;
  1041. master->dev.of_node = pdev->dev.of_node;
  1042. master->num_chipselect = p->info->num_chipselect;
  1043. master->setup = sh_msiof_spi_setup;
  1044. master->prepare_message = sh_msiof_prepare_message;
  1045. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
  1046. master->auto_runtime_pm = true;
  1047. master->transfer_one = sh_msiof_transfer_one;
  1048. ret = sh_msiof_request_dma(p);
  1049. if (ret < 0)
  1050. dev_warn(&pdev->dev, "DMA not available, using PIO\n");
  1051. ret = devm_spi_register_master(&pdev->dev, master);
  1052. if (ret < 0) {
  1053. dev_err(&pdev->dev, "spi_register_master error.\n");
  1054. goto err2;
  1055. }
  1056. return 0;
  1057. err2:
  1058. sh_msiof_release_dma(p);
  1059. pm_runtime_disable(&pdev->dev);
  1060. err1:
  1061. spi_master_put(master);
  1062. return ret;
  1063. }
  1064. static int sh_msiof_spi_remove(struct platform_device *pdev)
  1065. {
  1066. struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
  1067. sh_msiof_release_dma(p);
  1068. pm_runtime_disable(&pdev->dev);
  1069. return 0;
  1070. }
  1071. static const struct platform_device_id spi_driver_ids[] = {
  1072. { "spi_sh_msiof", (kernel_ulong_t)&sh_data },
  1073. { "spi_r8a7790_msiof", (kernel_ulong_t)&r8a779x_data },
  1074. { "spi_r8a7791_msiof", (kernel_ulong_t)&r8a779x_data },
  1075. { "spi_r8a7792_msiof", (kernel_ulong_t)&r8a779x_data },
  1076. { "spi_r8a7793_msiof", (kernel_ulong_t)&r8a779x_data },
  1077. { "spi_r8a7794_msiof", (kernel_ulong_t)&r8a779x_data },
  1078. {},
  1079. };
  1080. MODULE_DEVICE_TABLE(platform, spi_driver_ids);
  1081. static struct platform_driver sh_msiof_spi_drv = {
  1082. .probe = sh_msiof_spi_probe,
  1083. .remove = sh_msiof_spi_remove,
  1084. .id_table = spi_driver_ids,
  1085. .driver = {
  1086. .name = "spi_sh_msiof",
  1087. .of_match_table = of_match_ptr(sh_msiof_match),
  1088. },
  1089. };
  1090. module_platform_driver(sh_msiof_spi_drv);
  1091. MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
  1092. MODULE_AUTHOR("Magnus Damm");
  1093. MODULE_LICENSE("GPL v2");
  1094. MODULE_ALIAS("platform:spi_sh_msiof");