spi-sh-msiof.c 34 KB

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