1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * SuperH MSIOF SPI Controller Interface
4 *
5 * Copyright (c) 2009 Magnus Damm
6 * Copyright (C) 2014 Renesas Electronics Corporation
7 * Copyright (C) 2014-2017 Glider bvba
8 */
9
10 #include <linux/bitmap.h>
11 #include <linux/clk.h>
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/dmaengine.h>
16 #include <linux/err.h>
17 #include <linux/interrupt.h>
18 #include <linux/io.h>
19 #include <linux/iopoll.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/of_graph.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/sh_dma.h>
27
28 #include <linux/spi/sh_msiof.h>
29 #include <linux/spi/spi.h>
30
31 #include <linux/unaligned.h>
32
33 #define SH_MSIOF_FLAG_FIXED_DTDL_200 BIT(0)
34
35 struct sh_msiof_chipdata {
36 u32 bits_per_word_mask;
37 u16 tx_fifo_size;
38 u16 rx_fifo_size;
39 u16 ctlr_flags;
40 u16 min_div_pow;
41 u32 flags;
42 };
43
44 struct sh_msiof_spi_priv {
45 struct spi_controller *ctlr;
46 void __iomem *mapbase;
47 struct clk *clk;
48 struct platform_device *pdev;
49 struct sh_msiof_spi_info *info;
50 struct completion done;
51 struct completion done_txdma;
52 unsigned int tx_fifo_size;
53 unsigned int rx_fifo_size;
54 unsigned int min_div_pow;
55 void *tx_dma_page;
56 void *rx_dma_page;
57 dma_addr_t tx_dma_addr;
58 dma_addr_t rx_dma_addr;
59 bool native_cs_inited;
60 bool native_cs_high;
61 bool target_aborted;
62 };
63
64 #define MAX_SS 3 /* Maximum number of native chip selects */
65
sh_msiof_read(struct sh_msiof_spi_priv * p,int reg_offs)66 static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
67 {
68 switch (reg_offs) {
69 case SITSCR:
70 case SIRSCR:
71 return ioread16(p->mapbase + reg_offs);
72 default:
73 return ioread32(p->mapbase + reg_offs);
74 }
75 }
76
sh_msiof_write(struct sh_msiof_spi_priv * p,int reg_offs,u32 value)77 static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
78 u32 value)
79 {
80 switch (reg_offs) {
81 case SITSCR:
82 case SIRSCR:
83 iowrite16(value, p->mapbase + reg_offs);
84 break;
85 default:
86 iowrite32(value, p->mapbase + reg_offs);
87 break;
88 }
89 }
90
sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv * p,u32 clr,u32 set)91 static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
92 u32 clr, u32 set)
93 {
94 u32 mask = clr | set;
95 u32 data;
96
97 data = sh_msiof_read(p, SICTR);
98 data &= ~clr;
99 data |= set;
100 sh_msiof_write(p, SICTR, data);
101
102 return readl_poll_timeout_atomic(p->mapbase + SICTR, data,
103 (data & mask) == set, 1, 100);
104 }
105
sh_msiof_spi_irq(int irq,void * data)106 static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
107 {
108 struct sh_msiof_spi_priv *p = data;
109
110 /* just disable the interrupt and wake up */
111 sh_msiof_write(p, SIIER, 0);
112 complete(&p->done);
113
114 return IRQ_HANDLED;
115 }
116
sh_msiof_spi_reset_regs(struct sh_msiof_spi_priv * p)117 static int sh_msiof_spi_reset_regs(struct sh_msiof_spi_priv *p)
118 {
119 u32 mask = SICTR_TXRST | SICTR_RXRST;
120 u32 data;
121
122 data = sh_msiof_read(p, SICTR);
123 data |= mask;
124 sh_msiof_write(p, SICTR, data);
125
126 return readl_poll_timeout_atomic(p->mapbase + SICTR, data,
127 !(data & mask), 1, 100);
128 }
129
sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv * p,struct spi_transfer * t)130 static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
131 struct spi_transfer *t)
132 {
133 unsigned long parent_rate = clk_get_rate(p->clk);
134 unsigned int div_pow = p->min_div_pow;
135 u32 spi_hz = t->speed_hz;
136 unsigned long div;
137 u32 brps, scr;
138
139 if (!spi_hz || !parent_rate) {
140 WARN(1, "Invalid clock rate parameters %lu and %u\n",
141 parent_rate, spi_hz);
142 return;
143 }
144
145 div = DIV_ROUND_UP(parent_rate, spi_hz);
146 if (div <= 1024) {
147 /* SISCR_BRDV_DIV_1 is valid only if BRPS is x 1/1 or x 1/2 */
148 if (!div_pow && div <= 32 && div > 2)
149 div_pow = 1;
150
151 if (div_pow)
152 brps = (div + 1) >> div_pow;
153 else
154 brps = div;
155
156 for (; brps > 32; div_pow++)
157 brps = (brps + 1) >> 1;
158 } else {
159 /* Set transfer rate composite divisor to 2^5 * 32 = 1024 */
160 dev_err(&p->pdev->dev,
161 "Requested SPI transfer rate %d is too low\n", spi_hz);
162 div_pow = 5;
163 brps = 32;
164 }
165
166 t->effective_speed_hz = parent_rate / (brps << div_pow);
167
168 /* div_pow == 0 maps to SISCR_BRDV_DIV_1 == all ones */
169 scr = FIELD_PREP(SISCR_BRDV, div_pow - 1) |
170 FIELD_PREP(SISCR_BRPS, brps - 1);
171 sh_msiof_write(p, SITSCR, scr);
172 if (!(p->ctlr->flags & SPI_CONTROLLER_MUST_TX))
173 sh_msiof_write(p, SIRSCR, scr);
174 }
175
sh_msiof_get_delay_bit(u32 dtdl_or_syncdl)176 static u32 sh_msiof_get_delay_bit(u32 dtdl_or_syncdl)
177 {
178 /*
179 * DTDL/SYNCDL bit : p->info->dtdl or p->info->syncdl
180 * b'000 : 0
181 * b'001 : 100
182 * b'010 : 200
183 * b'011 (SYNCDL only) : 300
184 * b'101 : 50
185 * b'110 : 150
186 */
187 if (dtdl_or_syncdl % 100)
188 return dtdl_or_syncdl / 100 + 5;
189 else
190 return dtdl_or_syncdl / 100;
191 }
192
sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv * p)193 static u32 sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv *p)
194 {
195 u32 val;
196
197 if (!p->info)
198 return 0;
199
200 /* check if DTDL and SYNCDL is allowed value */
201 if (p->info->dtdl > 200 || p->info->syncdl > 300) {
202 dev_warn(&p->pdev->dev, "DTDL or SYNCDL is too large\n");
203 return 0;
204 }
205
206 /* check if the sum of DTDL and SYNCDL becomes an integer value */
207 if ((p->info->dtdl + p->info->syncdl) % 100) {
208 dev_warn(&p->pdev->dev, "the sum of DTDL/SYNCDL is not good\n");
209 return 0;
210 }
211
212 val = FIELD_PREP(SIMDR1_DTDL, sh_msiof_get_delay_bit(p->info->dtdl)) |
213 FIELD_PREP(SIMDR1_SYNCDL,
214 sh_msiof_get_delay_bit(p->info->syncdl));
215
216 return val;
217 }
218
sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv * p,u32 ss,bool cpol,bool cpha,bool tx_hi_z,bool lsb_first,bool cs_high)219 static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p, u32 ss,
220 bool cpol, bool cpha, bool tx_hi_z,
221 bool lsb_first, bool cs_high)
222 {
223 bool edge;
224 u32 tmp;
225
226 /*
227 * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG
228 * 0 0 10 10 1 1
229 * 0 1 10 10 0 0
230 * 1 0 11 11 0 0
231 * 1 1 11 11 1 1
232 */
233 tmp = FIELD_PREP(SIMDR1_SYNCMD, SIMDR1_SYNCMD_SPI) |
234 FIELD_PREP(SIMDR1_FLD, 1) | SIMDR1_XXSTP |
235 FIELD_PREP(SIMDR1_SYNCAC, !cs_high) |
236 FIELD_PREP(SIMDR1_BITLSB, lsb_first);
237 tmp |= sh_msiof_spi_get_dtdl_and_syncdl(p);
238 if (spi_controller_is_target(p->ctlr)) {
239 sh_msiof_write(p, SITMDR1, tmp | SITMDR1_PCON);
240 } else {
241 sh_msiof_write(p, SITMDR1,
242 tmp | SIMDR1_TRMD | SITMDR1_PCON |
243 FIELD_PREP(SITMDR1_SYNCCH,
244 ss < MAX_SS ? ss : 0));
245 }
246 if (p->ctlr->flags & SPI_CONTROLLER_MUST_TX) {
247 /* These bits are reserved if RX needs TX */
248 tmp &= ~0x0000ffff;
249 }
250 sh_msiof_write(p, SIRMDR1, tmp);
251
252 tmp = 0;
253 tmp |= SICTR_TSCKIZ_SCK | FIELD_PREP(SICTR_TSCKIZ_POL, cpol);
254 tmp |= SICTR_RSCKIZ_SCK | FIELD_PREP(SICTR_RSCKIZ_POL, cpol);
255
256 edge = cpol ^ !cpha;
257
258 tmp |= FIELD_PREP(SICTR_TEDG, edge);
259 tmp |= FIELD_PREP(SICTR_REDG, edge);
260 tmp |= FIELD_PREP(SICTR_TXDIZ,
261 tx_hi_z ? SICTR_TXDIZ_HIZ : SICTR_TXDIZ_LOW);
262 sh_msiof_write(p, SICTR, tmp);
263 }
264
sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv * p,const void * tx_buf,void * rx_buf,u32 bits,u32 words1,u32 words2)265 static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
266 const void *tx_buf, void *rx_buf,
267 u32 bits, u32 words1, u32 words2)
268 {
269 u32 dr2 = FIELD_PREP(SIMDR2_GRP, words2 ? 1 : 0) |
270 FIELD_PREP(SIMDR2_BITLEN1, bits - 1) |
271 FIELD_PREP(SIMDR2_WDLEN1, words1 - 1);
272
273 if (tx_buf || (p->ctlr->flags & SPI_CONTROLLER_MUST_TX))
274 sh_msiof_write(p, SITMDR2, dr2);
275 else
276 sh_msiof_write(p, SITMDR2, dr2 | SIMDR2_GRPMASK);
277
278 if (rx_buf)
279 sh_msiof_write(p, SIRMDR2, dr2);
280
281 if (words2) {
282 u32 dr3 = FIELD_PREP(SIMDR3_BITLEN2, bits - 1) |
283 FIELD_PREP(SIMDR3_WDLEN2, words2 - 1);
284
285 sh_msiof_write(p, SITMDR3, dr3);
286 if (rx_buf)
287 sh_msiof_write(p, SIRMDR3, dr3);
288 }
289 }
290
sh_msiof_reset_str(struct sh_msiof_spi_priv * p)291 static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
292 {
293 sh_msiof_write(p, SISTR,
294 sh_msiof_read(p, SISTR) & ~(SISTR_TDREQ | SISTR_RDREQ));
295 }
296
sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)297 static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
298 const void *tx_buf, unsigned int words,
299 unsigned int fs)
300 {
301 const u8 *buf_8 = tx_buf;
302 unsigned int k;
303
304 for (k = 0; k < words; k++)
305 sh_msiof_write(p, SITFDR, buf_8[k] << fs);
306 }
307
sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)308 static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
309 const void *tx_buf, unsigned int words,
310 unsigned int fs)
311 {
312 const u16 *buf_16 = tx_buf;
313 unsigned int k;
314
315 for (k = 0; k < words; k++)
316 sh_msiof_write(p, SITFDR, buf_16[k] << fs);
317 }
318
sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)319 static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
320 const void *tx_buf, unsigned int words,
321 unsigned int fs)
322 {
323 const u16 *buf_16 = tx_buf;
324 unsigned int k;
325
326 for (k = 0; k < words; k++)
327 sh_msiof_write(p, SITFDR, get_unaligned(&buf_16[k]) << fs);
328 }
329
sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)330 static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
331 const void *tx_buf, unsigned int words,
332 unsigned int fs)
333 {
334 const u32 *buf_32 = tx_buf;
335 unsigned int k;
336
337 for (k = 0; k < words; k++)
338 sh_msiof_write(p, SITFDR, buf_32[k] << fs);
339 }
340
sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)341 static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
342 const void *tx_buf, unsigned int words,
343 unsigned int fs)
344 {
345 const u32 *buf_32 = tx_buf;
346 unsigned int k;
347
348 for (k = 0; k < words; k++)
349 sh_msiof_write(p, SITFDR, get_unaligned(&buf_32[k]) << fs);
350 }
351
sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)352 static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
353 const void *tx_buf, unsigned int words,
354 unsigned int fs)
355 {
356 const u32 *buf_32 = tx_buf;
357 unsigned int k;
358
359 for (k = 0; k < words; k++)
360 sh_msiof_write(p, SITFDR, swab32(buf_32[k] << fs));
361 }
362
sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv * p,const void * tx_buf,unsigned int words,unsigned int fs)363 static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
364 const void *tx_buf,
365 unsigned int words, unsigned int fs)
366 {
367 const u32 *buf_32 = tx_buf;
368 unsigned int k;
369
370 for (k = 0; k < words; k++)
371 sh_msiof_write(p, SITFDR, swab32(get_unaligned(&buf_32[k]) << fs));
372 }
373
sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)374 static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
375 void *rx_buf, unsigned int words,
376 unsigned int fs)
377 {
378 u8 *buf_8 = rx_buf;
379 unsigned int k;
380
381 for (k = 0; k < words; k++)
382 buf_8[k] = sh_msiof_read(p, SIRFDR) >> fs;
383 }
384
sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)385 static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
386 void *rx_buf, unsigned int words,
387 unsigned int fs)
388 {
389 u16 *buf_16 = rx_buf;
390 unsigned int k;
391
392 for (k = 0; k < words; k++)
393 buf_16[k] = sh_msiof_read(p, SIRFDR) >> fs;
394 }
395
sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)396 static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
397 void *rx_buf, unsigned int words,
398 unsigned int fs)
399 {
400 u16 *buf_16 = rx_buf;
401 unsigned int k;
402
403 for (k = 0; k < words; k++)
404 put_unaligned(sh_msiof_read(p, SIRFDR) >> fs, &buf_16[k]);
405 }
406
sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)407 static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
408 void *rx_buf, unsigned int words,
409 unsigned int fs)
410 {
411 u32 *buf_32 = rx_buf;
412 unsigned int k;
413
414 for (k = 0; k < words; k++)
415 buf_32[k] = sh_msiof_read(p, SIRFDR) >> fs;
416 }
417
sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)418 static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
419 void *rx_buf, unsigned int words,
420 unsigned int fs)
421 {
422 u32 *buf_32 = rx_buf;
423 unsigned int k;
424
425 for (k = 0; k < words; k++)
426 put_unaligned(sh_msiof_read(p, SIRFDR) >> fs, &buf_32[k]);
427 }
428
sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)429 static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
430 void *rx_buf, unsigned int words,
431 unsigned int fs)
432 {
433 u32 *buf_32 = rx_buf;
434 unsigned int k;
435
436 for (k = 0; k < words; k++)
437 buf_32[k] = swab32(sh_msiof_read(p, SIRFDR) >> fs);
438 }
439
sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv * p,void * rx_buf,unsigned int words,unsigned int fs)440 static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
441 void *rx_buf, unsigned int words,
442 unsigned int fs)
443 {
444 u32 *buf_32 = rx_buf;
445 unsigned int k;
446
447 for (k = 0; k < words; k++)
448 put_unaligned(swab32(sh_msiof_read(p, SIRFDR) >> fs), &buf_32[k]);
449 }
450
sh_msiof_spi_setup(struct spi_device * spi)451 static int sh_msiof_spi_setup(struct spi_device *spi)
452 {
453 struct sh_msiof_spi_priv *p =
454 spi_controller_get_devdata(spi->controller);
455 u32 clr, set, tmp;
456
457 if (spi_get_csgpiod(spi, 0) || spi_controller_is_target(p->ctlr))
458 return 0;
459
460 if (p->native_cs_inited &&
461 (p->native_cs_high == !!(spi->mode & SPI_CS_HIGH)))
462 return 0;
463
464 /* Configure native chip select mode/polarity early */
465 clr = SIMDR1_SYNCMD;
466 set = FIELD_PREP(SIMDR1_SYNCMD, SIMDR1_SYNCMD_SPI);
467 if (spi->mode & SPI_CS_HIGH)
468 clr |= SIMDR1_SYNCAC;
469 else
470 set |= SIMDR1_SYNCAC;
471 pm_runtime_get_sync(&p->pdev->dev);
472 tmp = sh_msiof_read(p, SITMDR1) & ~clr;
473 sh_msiof_write(p, SITMDR1, tmp | set | SIMDR1_TRMD | SITMDR1_PCON);
474 tmp = sh_msiof_read(p, SIRMDR1) & ~clr;
475 sh_msiof_write(p, SIRMDR1, tmp | set);
476 pm_runtime_put(&p->pdev->dev);
477 p->native_cs_high = spi->mode & SPI_CS_HIGH;
478 p->native_cs_inited = true;
479 return 0;
480 }
481
sh_msiof_prepare_message(struct spi_controller * ctlr,struct spi_message * msg)482 static int sh_msiof_prepare_message(struct spi_controller *ctlr,
483 struct spi_message *msg)
484 {
485 struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr);
486 const struct spi_device *spi = msg->spi;
487 bool cs_high;
488 u32 ss;
489
490 /* Configure pins before asserting CS */
491 if (spi_get_csgpiod(spi, 0)) {
492 ss = ctlr->unused_native_cs;
493 cs_high = p->native_cs_high;
494 } else {
495 ss = spi_get_chipselect(spi, 0);
496 cs_high = spi->mode & SPI_CS_HIGH;
497 }
498 sh_msiof_spi_set_pin_regs(p, ss, spi->mode & SPI_CPOL,
499 spi->mode & SPI_CPHA, spi->mode & SPI_3WIRE,
500 spi->mode & SPI_LSB_FIRST, cs_high);
501 return 0;
502 }
503
sh_msiof_spi_start(struct sh_msiof_spi_priv * p,void * rx_buf)504 static int sh_msiof_spi_start(struct sh_msiof_spi_priv *p, void *rx_buf)
505 {
506 bool target = spi_controller_is_target(p->ctlr);
507 int ret = 0;
508
509 /* setup clock and rx/tx signals */
510 if (!target)
511 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TSCKE);
512 if (rx_buf && !ret)
513 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_RXE);
514 if (!ret)
515 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TXE);
516
517 /* start by setting frame bit */
518 if (!ret && !target)
519 ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TFSE);
520
521 return ret;
522 }
523
sh_msiof_spi_stop(struct sh_msiof_spi_priv * p,void * rx_buf)524 static int sh_msiof_spi_stop(struct sh_msiof_spi_priv *p, void *rx_buf)
525 {
526 bool target = spi_controller_is_target(p->ctlr);
527 int ret = 0;
528
529 /* shut down frame, rx/tx and clock signals */
530 if (!target)
531 ret = sh_msiof_modify_ctr_wait(p, SICTR_TFSE, 0);
532 if (!ret)
533 ret = sh_msiof_modify_ctr_wait(p, SICTR_TXE, 0);
534 if (rx_buf && !ret)
535 ret = sh_msiof_modify_ctr_wait(p, SICTR_RXE, 0);
536 if (!ret && !target)
537 ret = sh_msiof_modify_ctr_wait(p, SICTR_TSCKE, 0);
538
539 return ret;
540 }
541
sh_msiof_target_abort(struct spi_controller * ctlr)542 static int sh_msiof_target_abort(struct spi_controller *ctlr)
543 {
544 struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr);
545
546 p->target_aborted = true;
547 complete(&p->done);
548 complete(&p->done_txdma);
549 return 0;
550 }
551
sh_msiof_wait_for_completion(struct sh_msiof_spi_priv * p,struct completion * x)552 static int sh_msiof_wait_for_completion(struct sh_msiof_spi_priv *p,
553 struct completion *x)
554 {
555 if (spi_controller_is_target(p->ctlr)) {
556 if (wait_for_completion_interruptible(x) ||
557 p->target_aborted) {
558 dev_dbg(&p->pdev->dev, "interrupted\n");
559 return -EINTR;
560 }
561 } else {
562 if (!wait_for_completion_timeout(x, HZ)) {
563 dev_err(&p->pdev->dev, "timeout\n");
564 return -ETIMEDOUT;
565 }
566 }
567
568 return 0;
569 }
570
sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv * p,void (* tx_fifo)(struct sh_msiof_spi_priv *,const void *,unsigned int,unsigned int),void (* rx_fifo)(struct sh_msiof_spi_priv *,void *,unsigned int,unsigned int),const void * tx_buf,void * rx_buf,unsigned int words,unsigned int bits)571 static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
572 void (*tx_fifo)(struct sh_msiof_spi_priv *,
573 const void *, unsigned int,
574 unsigned int),
575 void (*rx_fifo)(struct sh_msiof_spi_priv *,
576 void *, unsigned int,
577 unsigned int),
578 const void *tx_buf, void *rx_buf,
579 unsigned int words, unsigned int bits)
580 {
581 unsigned int fifo_shift;
582 int ret;
583
584 /* limit maximum word transfer to rx/tx fifo size */
585 if (tx_buf)
586 words = min(words, p->tx_fifo_size);
587 if (rx_buf)
588 words = min(words, p->rx_fifo_size);
589
590 /* the fifo contents need shifting */
591 fifo_shift = 32 - bits;
592
593 /* default FIFO watermarks for PIO */
594 sh_msiof_write(p, SIFCTR, 0);
595
596 /* setup msiof transfer mode registers */
597 sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words, 0);
598 sh_msiof_write(p, SIIER, SIIER_TEOFE | SIIER_REOFE);
599
600 /* write tx fifo */
601 if (tx_buf)
602 tx_fifo(p, tx_buf, words, fifo_shift);
603
604 reinit_completion(&p->done);
605 p->target_aborted = false;
606
607 ret = sh_msiof_spi_start(p, rx_buf);
608 if (ret) {
609 dev_err(&p->pdev->dev, "failed to start hardware\n");
610 goto stop_ier;
611 }
612
613 /* wait for tx fifo to be emptied / rx fifo to be filled */
614 ret = sh_msiof_wait_for_completion(p, &p->done);
615 if (ret)
616 goto stop_reset;
617
618 /* read rx fifo */
619 if (rx_buf)
620 rx_fifo(p, rx_buf, words, fifo_shift);
621
622 /* clear status bits */
623 sh_msiof_reset_str(p);
624
625 ret = sh_msiof_spi_stop(p, rx_buf);
626 if (ret) {
627 dev_err(&p->pdev->dev, "failed to shut down hardware\n");
628 return ret;
629 }
630
631 return words;
632
633 stop_reset:
634 sh_msiof_reset_str(p);
635 sh_msiof_spi_stop(p, rx_buf);
636 stop_ier:
637 sh_msiof_write(p, SIIER, 0);
638 return ret;
639 }
640
sh_msiof_dma_complete(void * arg)641 static void sh_msiof_dma_complete(void *arg)
642 {
643 complete(arg);
644 }
645
sh_msiof_dma_once(struct sh_msiof_spi_priv * p,const void * tx,void * rx,unsigned int len,unsigned int max_wdlen)646 static int sh_msiof_dma_once(struct sh_msiof_spi_priv *p, const void *tx,
647 void *rx, unsigned int len,
648 unsigned int max_wdlen)
649 {
650 u32 ier_bits = 0;
651 struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
652 unsigned int words1, words2;
653 dma_cookie_t cookie;
654 int ret;
655
656 /* First prepare and submit the DMA request(s), as this may fail */
657 if (rx) {
658 ier_bits |= SIIER_RDREQE | SIIER_RDMAE;
659 desc_rx = dmaengine_prep_slave_single(p->ctlr->dma_rx,
660 p->rx_dma_addr, len, DMA_DEV_TO_MEM,
661 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
662 if (!desc_rx)
663 return -EAGAIN;
664
665 desc_rx->callback = sh_msiof_dma_complete;
666 desc_rx->callback_param = &p->done;
667 cookie = dmaengine_submit(desc_rx);
668 if (dma_submit_error(cookie))
669 return cookie;
670 }
671
672 if (tx) {
673 ier_bits |= SIIER_TDREQE | SIIER_TDMAE;
674 dma_sync_single_for_device(p->ctlr->dma_tx->device->dev,
675 p->tx_dma_addr, len, DMA_TO_DEVICE);
676 desc_tx = dmaengine_prep_slave_single(p->ctlr->dma_tx,
677 p->tx_dma_addr, len, DMA_MEM_TO_DEV,
678 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
679 if (!desc_tx) {
680 ret = -EAGAIN;
681 goto no_dma_tx;
682 }
683
684 desc_tx->callback = sh_msiof_dma_complete;
685 desc_tx->callback_param = &p->done_txdma;
686 cookie = dmaengine_submit(desc_tx);
687 if (dma_submit_error(cookie)) {
688 ret = cookie;
689 goto no_dma_tx;
690 }
691 }
692
693 /* 1 stage FIFO watermarks for DMA */
694 sh_msiof_write(p, SIFCTR,
695 FIELD_PREP(SIFCTR_TFWM, SIFCTR_TFWM_1) |
696 FIELD_PREP(SIFCTR_RFWM, SIFCTR_RFWM_1));
697
698 /* setup msiof transfer mode registers (32-bit words) */
699 words1 = min(len / 4, max_wdlen);
700 words2 = len / 4 - words1;
701 sh_msiof_spi_set_mode_regs(p, tx, rx, 32, words1, words2);
702
703 sh_msiof_write(p, SIIER, ier_bits);
704
705 reinit_completion(&p->done);
706 if (tx)
707 reinit_completion(&p->done_txdma);
708 p->target_aborted = false;
709
710 /* Now start DMA */
711 if (rx)
712 dma_async_issue_pending(p->ctlr->dma_rx);
713 if (tx)
714 dma_async_issue_pending(p->ctlr->dma_tx);
715
716 ret = sh_msiof_spi_start(p, rx);
717 if (ret) {
718 dev_err(&p->pdev->dev, "failed to start hardware\n");
719 goto stop_dma;
720 }
721
722 if (tx) {
723 /* wait for tx DMA completion */
724 ret = sh_msiof_wait_for_completion(p, &p->done_txdma);
725 if (ret)
726 goto stop_reset;
727 }
728
729 if (rx) {
730 /* wait for rx DMA completion */
731 ret = sh_msiof_wait_for_completion(p, &p->done);
732 if (ret)
733 goto stop_reset;
734
735 sh_msiof_write(p, SIIER, 0);
736 } else {
737 /* wait for tx fifo to be emptied */
738 sh_msiof_write(p, SIIER, SIIER_TEOFE);
739 ret = sh_msiof_wait_for_completion(p, &p->done);
740 if (ret)
741 goto stop_reset;
742 }
743
744 /* clear status bits */
745 sh_msiof_reset_str(p);
746
747 ret = sh_msiof_spi_stop(p, rx);
748 if (ret) {
749 dev_err(&p->pdev->dev, "failed to shut down hardware\n");
750 return ret;
751 }
752
753 if (rx)
754 dma_sync_single_for_cpu(p->ctlr->dma_rx->device->dev,
755 p->rx_dma_addr, len, DMA_FROM_DEVICE);
756
757 return 0;
758
759 stop_reset:
760 sh_msiof_reset_str(p);
761 sh_msiof_spi_stop(p, rx);
762 stop_dma:
763 if (tx)
764 dmaengine_terminate_sync(p->ctlr->dma_tx);
765 no_dma_tx:
766 if (rx)
767 dmaengine_terminate_sync(p->ctlr->dma_rx);
768 sh_msiof_write(p, SIIER, 0);
769 return ret;
770 }
771
copy_bswap32(u32 * dst,const u32 * src,unsigned int words)772 static void copy_bswap32(u32 *dst, const u32 *src, unsigned int words)
773 {
774 /* src or dst can be unaligned, but not both */
775 if ((unsigned long)src & 3) {
776 while (words--) {
777 *dst++ = swab32(get_unaligned(src));
778 src++;
779 }
780 } else if ((unsigned long)dst & 3) {
781 while (words--) {
782 put_unaligned(swab32(*src++), dst);
783 dst++;
784 }
785 } else {
786 while (words--)
787 *dst++ = swab32(*src++);
788 }
789 }
790
copy_wswap32(u32 * dst,const u32 * src,unsigned int words)791 static void copy_wswap32(u32 *dst, const u32 *src, unsigned int words)
792 {
793 /* src or dst can be unaligned, but not both */
794 if ((unsigned long)src & 3) {
795 while (words--) {
796 *dst++ = swahw32(get_unaligned(src));
797 src++;
798 }
799 } else if ((unsigned long)dst & 3) {
800 while (words--) {
801 put_unaligned(swahw32(*src++), dst);
802 dst++;
803 }
804 } else {
805 while (words--)
806 *dst++ = swahw32(*src++);
807 }
808 }
809
copy_plain32(u32 * dst,const u32 * src,unsigned int words)810 static void copy_plain32(u32 *dst, const u32 *src, unsigned int words)
811 {
812 memcpy(dst, src, words * 4);
813 }
814
sh_msiof_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * t)815 static int sh_msiof_transfer_one(struct spi_controller *ctlr,
816 struct spi_device *spi,
817 struct spi_transfer *t)
818 {
819 struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr);
820 unsigned int max_wdlen = FIELD_MAX(SIMDR2_WDLEN1) + 1;
821 void (*copy32)(u32 *, const u32 *, unsigned int);
822 void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, unsigned int,
823 unsigned int);
824 void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, unsigned int,
825 unsigned int);
826 const void *tx_buf = t->tx_buf;
827 void *rx_buf = t->rx_buf;
828 unsigned int len = t->len;
829 unsigned int bits = t->bits_per_word;
830 unsigned int bytes_per_word;
831 unsigned int words;
832 int n;
833 bool swab;
834 int ret;
835
836 /* reset registers */
837 ret = sh_msiof_spi_reset_regs(p);
838 if (ret)
839 return ret;
840
841 /* setup clocks (clock already enabled in chipselect()) */
842 if (!spi_controller_is_target(p->ctlr))
843 sh_msiof_spi_set_clk_regs(p, t);
844
845 if (tx_buf)
846 max_wdlen = min(max_wdlen, p->tx_fifo_size);
847 if (rx_buf)
848 max_wdlen = min(max_wdlen, p->rx_fifo_size);
849
850 while (ctlr->dma_tx && len > 15) {
851 /*
852 * DMA supports 32-bit words only, hence pack 8-bit and 16-bit
853 * words, with byte resp. word swapping.
854 */
855 unsigned int l = min(round_down(len, 4), 2 * max_wdlen * 4);
856
857 if (bits <= 8) {
858 copy32 = copy_bswap32;
859 } else if (bits <= 16) {
860 copy32 = copy_wswap32;
861 } else {
862 copy32 = copy_plain32;
863 }
864
865 if (tx_buf)
866 copy32(p->tx_dma_page, tx_buf, l / 4);
867
868 ret = sh_msiof_dma_once(p, tx_buf, rx_buf, l, max_wdlen);
869 if (ret == -EAGAIN) {
870 dev_warn_once(&p->pdev->dev,
871 "DMA not available, falling back to PIO\n");
872 break;
873 }
874 if (ret)
875 return ret;
876
877 if (rx_buf) {
878 copy32(rx_buf, p->rx_dma_page, l / 4);
879 rx_buf += l;
880 }
881 if (tx_buf)
882 tx_buf += l;
883
884 len -= l;
885 if (!len)
886 return 0;
887 }
888
889 if (bits <= 8 && len > 15) {
890 bits = 32;
891 swab = true;
892 } else {
893 swab = false;
894 }
895
896 /* setup bytes per word and fifo read/write functions */
897 if (bits <= 8) {
898 bytes_per_word = 1;
899 tx_fifo = sh_msiof_spi_write_fifo_8;
900 rx_fifo = sh_msiof_spi_read_fifo_8;
901 } else if (bits <= 16) {
902 bytes_per_word = 2;
903 if ((unsigned long)tx_buf & 0x01)
904 tx_fifo = sh_msiof_spi_write_fifo_16u;
905 else
906 tx_fifo = sh_msiof_spi_write_fifo_16;
907
908 if ((unsigned long)rx_buf & 0x01)
909 rx_fifo = sh_msiof_spi_read_fifo_16u;
910 else
911 rx_fifo = sh_msiof_spi_read_fifo_16;
912 } else if (swab) {
913 bytes_per_word = 4;
914 if ((unsigned long)tx_buf & 0x03)
915 tx_fifo = sh_msiof_spi_write_fifo_s32u;
916 else
917 tx_fifo = sh_msiof_spi_write_fifo_s32;
918
919 if ((unsigned long)rx_buf & 0x03)
920 rx_fifo = sh_msiof_spi_read_fifo_s32u;
921 else
922 rx_fifo = sh_msiof_spi_read_fifo_s32;
923 } else {
924 bytes_per_word = 4;
925 if ((unsigned long)tx_buf & 0x03)
926 tx_fifo = sh_msiof_spi_write_fifo_32u;
927 else
928 tx_fifo = sh_msiof_spi_write_fifo_32;
929
930 if ((unsigned long)rx_buf & 0x03)
931 rx_fifo = sh_msiof_spi_read_fifo_32u;
932 else
933 rx_fifo = sh_msiof_spi_read_fifo_32;
934 }
935
936 /* transfer in fifo sized chunks */
937 words = len / bytes_per_word;
938
939 while (words > 0) {
940 n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo, tx_buf, rx_buf,
941 words, bits);
942 if (n < 0)
943 return n;
944
945 if (tx_buf)
946 tx_buf += n * bytes_per_word;
947 if (rx_buf)
948 rx_buf += n * bytes_per_word;
949 words -= n;
950
951 if (words == 0 && (len % bytes_per_word)) {
952 words = len % bytes_per_word;
953 bits = t->bits_per_word;
954 bytes_per_word = 1;
955 tx_fifo = sh_msiof_spi_write_fifo_8;
956 rx_fifo = sh_msiof_spi_read_fifo_8;
957 }
958 }
959
960 return 0;
961 }
962
963 static const struct sh_msiof_chipdata sh_data = {
964 .bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32),
965 .tx_fifo_size = 64,
966 .rx_fifo_size = 64,
967 .ctlr_flags = 0,
968 .min_div_pow = 0,
969 };
970
971 static const struct sh_msiof_chipdata rcar_gen2_data = {
972 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
973 SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
974 .tx_fifo_size = 64,
975 .rx_fifo_size = 128,
976 .ctlr_flags = SPI_CONTROLLER_MUST_TX,
977 .min_div_pow = 0,
978 };
979
980 static const struct sh_msiof_chipdata rcar_gen3_data = {
981 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
982 SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
983 .tx_fifo_size = 64,
984 .rx_fifo_size = 256,
985 .ctlr_flags = SPI_CONTROLLER_MUST_TX,
986 .min_div_pow = 1,
987 };
988
989 static const struct sh_msiof_chipdata rcar_gen4_data = {
990 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
991 SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
992 .tx_fifo_size = 256,
993 .rx_fifo_size = 256,
994 .ctlr_flags = SPI_CONTROLLER_MUST_TX,
995 .min_div_pow = 1,
996 };
997
998 static const struct sh_msiof_chipdata rcar_r8a7795_data = {
999 .bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
1000 SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
1001 .tx_fifo_size = 64,
1002 .rx_fifo_size = 256,
1003 .ctlr_flags = SPI_CONTROLLER_MUST_TX,
1004 .min_div_pow = 1,
1005 .flags = SH_MSIOF_FLAG_FIXED_DTDL_200,
1006 };
1007
1008 static const struct of_device_id sh_msiof_match[] __maybe_unused = {
1009 { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data },
1010 { .compatible = "renesas,rcar-gen2-msiof", .data = &rcar_gen2_data },
1011 { .compatible = "renesas,msiof-r8a7795", .data = &rcar_r8a7795_data },
1012 { .compatible = "renesas,rcar-gen3-msiof", .data = &rcar_gen3_data },
1013 { .compatible = "renesas,msiof-r8a779a0", .data = &rcar_gen3_data },
1014 { .compatible = "renesas,msiof-r8a779f0", .data = &rcar_gen3_data },
1015 { .compatible = "renesas,rcar-gen4-msiof", .data = &rcar_gen4_data },
1016 { .compatible = "renesas,sh-msiof", .data = &sh_data }, /* Deprecated */
1017 { /* sentinel */ }
1018 };
1019 MODULE_DEVICE_TABLE(of, sh_msiof_match);
1020
1021 #ifdef CONFIG_OF
sh_msiof_spi_parse_dt(struct device * dev)1022 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
1023 {
1024 struct sh_msiof_spi_info *info;
1025 struct device_node *np = dev->of_node;
1026 u32 num_cs = 1;
1027
1028 info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
1029 if (!info)
1030 return NULL;
1031
1032 info->mode = of_property_read_bool(np, "spi-slave") ? MSIOF_SPI_TARGET
1033 : MSIOF_SPI_HOST;
1034
1035 /* Parse the MSIOF properties */
1036 if (info->mode == MSIOF_SPI_HOST)
1037 of_property_read_u32(np, "num-cs", &num_cs);
1038 of_property_read_u32(np, "renesas,tx-fifo-size",
1039 &info->tx_fifo_override);
1040 of_property_read_u32(np, "renesas,rx-fifo-size",
1041 &info->rx_fifo_override);
1042 of_property_read_u32(np, "renesas,dtdl", &info->dtdl);
1043 of_property_read_u32(np, "renesas,syncdl", &info->syncdl);
1044
1045 info->num_chipselect = num_cs;
1046
1047 return info;
1048 }
1049 #else
sh_msiof_spi_parse_dt(struct device * dev)1050 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
1051 {
1052 return NULL;
1053 }
1054 #endif
1055
sh_msiof_request_dma_chan(struct device * dev,enum dma_transfer_direction dir,unsigned int id,dma_addr_t port_addr)1056 static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev,
1057 enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr)
1058 {
1059 dma_cap_mask_t mask;
1060 struct dma_chan *chan;
1061 struct dma_slave_config cfg;
1062 int ret;
1063
1064 dma_cap_zero(mask);
1065 dma_cap_set(DMA_SLAVE, mask);
1066
1067 chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1068 (void *)(unsigned long)id, dev,
1069 dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1070 if (!chan) {
1071 dev_warn(dev, "dma_request_slave_channel_compat failed\n");
1072 return NULL;
1073 }
1074
1075 memset(&cfg, 0, sizeof(cfg));
1076 cfg.direction = dir;
1077 if (dir == DMA_MEM_TO_DEV) {
1078 cfg.dst_addr = port_addr;
1079 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1080 } else {
1081 cfg.src_addr = port_addr;
1082 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1083 }
1084
1085 ret = dmaengine_slave_config(chan, &cfg);
1086 if (ret) {
1087 dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
1088 dma_release_channel(chan);
1089 return NULL;
1090 }
1091
1092 return chan;
1093 }
1094
sh_msiof_request_dma(struct sh_msiof_spi_priv * p)1095 static int sh_msiof_request_dma(struct sh_msiof_spi_priv *p)
1096 {
1097 struct platform_device *pdev = p->pdev;
1098 struct device *dev = &pdev->dev;
1099 const struct sh_msiof_spi_info *info = p->info;
1100 unsigned int dma_tx_id, dma_rx_id;
1101 const struct resource *res;
1102 struct spi_controller *ctlr;
1103 struct device *tx_dev, *rx_dev;
1104
1105 if (dev->of_node) {
1106 /* In the OF case we will get the slave IDs from the DT */
1107 dma_tx_id = 0;
1108 dma_rx_id = 0;
1109 } else if (info && info->dma_tx_id && info->dma_rx_id) {
1110 dma_tx_id = info->dma_tx_id;
1111 dma_rx_id = info->dma_rx_id;
1112 } else {
1113 /* The driver assumes no error */
1114 return 0;
1115 }
1116
1117 /* The DMA engine uses the second register set, if present */
1118 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1119 if (!res)
1120 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1121
1122 ctlr = p->ctlr;
1123 ctlr->dma_tx = sh_msiof_request_dma_chan(dev, DMA_MEM_TO_DEV,
1124 dma_tx_id, res->start + SITFDR);
1125 if (!ctlr->dma_tx)
1126 return -ENODEV;
1127
1128 ctlr->dma_rx = sh_msiof_request_dma_chan(dev, DMA_DEV_TO_MEM,
1129 dma_rx_id, res->start + SIRFDR);
1130 if (!ctlr->dma_rx)
1131 goto free_tx_chan;
1132
1133 p->tx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
1134 if (!p->tx_dma_page)
1135 goto free_rx_chan;
1136
1137 p->rx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
1138 if (!p->rx_dma_page)
1139 goto free_tx_page;
1140
1141 tx_dev = ctlr->dma_tx->device->dev;
1142 p->tx_dma_addr = dma_map_single(tx_dev, p->tx_dma_page, PAGE_SIZE,
1143 DMA_TO_DEVICE);
1144 if (dma_mapping_error(tx_dev, p->tx_dma_addr))
1145 goto free_rx_page;
1146
1147 rx_dev = ctlr->dma_rx->device->dev;
1148 p->rx_dma_addr = dma_map_single(rx_dev, p->rx_dma_page, PAGE_SIZE,
1149 DMA_FROM_DEVICE);
1150 if (dma_mapping_error(rx_dev, p->rx_dma_addr))
1151 goto unmap_tx_page;
1152
1153 dev_info(dev, "DMA available");
1154 return 0;
1155
1156 unmap_tx_page:
1157 dma_unmap_single(tx_dev, p->tx_dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
1158 free_rx_page:
1159 free_page((unsigned long)p->rx_dma_page);
1160 free_tx_page:
1161 free_page((unsigned long)p->tx_dma_page);
1162 free_rx_chan:
1163 dma_release_channel(ctlr->dma_rx);
1164 free_tx_chan:
1165 dma_release_channel(ctlr->dma_tx);
1166 ctlr->dma_tx = NULL;
1167 return -ENODEV;
1168 }
1169
sh_msiof_release_dma(struct sh_msiof_spi_priv * p)1170 static void sh_msiof_release_dma(struct sh_msiof_spi_priv *p)
1171 {
1172 struct spi_controller *ctlr = p->ctlr;
1173
1174 if (!ctlr->dma_tx)
1175 return;
1176
1177 dma_unmap_single(ctlr->dma_rx->device->dev, p->rx_dma_addr, PAGE_SIZE,
1178 DMA_FROM_DEVICE);
1179 dma_unmap_single(ctlr->dma_tx->device->dev, p->tx_dma_addr, PAGE_SIZE,
1180 DMA_TO_DEVICE);
1181 free_page((unsigned long)p->rx_dma_page);
1182 free_page((unsigned long)p->tx_dma_page);
1183 dma_release_channel(ctlr->dma_rx);
1184 dma_release_channel(ctlr->dma_tx);
1185 }
1186
sh_msiof_spi_probe(struct platform_device * pdev)1187 static int sh_msiof_spi_probe(struct platform_device *pdev)
1188 {
1189 struct spi_controller *ctlr;
1190 const struct sh_msiof_chipdata *chipdata;
1191 struct sh_msiof_spi_info *info;
1192 struct sh_msiof_spi_priv *p;
1193 struct device *dev = &pdev->dev;
1194 unsigned long clksrc;
1195 int i;
1196 int ret;
1197
1198 /* Check whether MSIOF is used as I2S mode or SPI mode by checking "port" node */
1199 struct device_node *port __free(device_node) = of_graph_get_next_port(dev->of_node, NULL);
1200 if (port) /* It was MSIOF-I2S */
1201 return -ENODEV;
1202
1203 chipdata = of_device_get_match_data(dev);
1204 if (chipdata) {
1205 info = sh_msiof_spi_parse_dt(dev);
1206 } else {
1207 chipdata = (const void *)pdev->id_entry->driver_data;
1208 info = dev_get_platdata(dev);
1209 }
1210
1211 if (!info) {
1212 dev_err(dev, "failed to obtain device info\n");
1213 return -ENXIO;
1214 }
1215
1216 if (chipdata->flags & SH_MSIOF_FLAG_FIXED_DTDL_200)
1217 info->dtdl = 200;
1218
1219 if (info->mode == MSIOF_SPI_TARGET)
1220 ctlr = devm_spi_alloc_target(dev, sizeof(struct sh_msiof_spi_priv));
1221 else
1222 ctlr = devm_spi_alloc_host(dev, sizeof(struct sh_msiof_spi_priv));
1223 if (ctlr == NULL)
1224 return -ENOMEM;
1225
1226 p = spi_controller_get_devdata(ctlr);
1227
1228 platform_set_drvdata(pdev, p);
1229 p->ctlr = ctlr;
1230 p->info = info;
1231 p->min_div_pow = chipdata->min_div_pow;
1232
1233 init_completion(&p->done);
1234 init_completion(&p->done_txdma);
1235
1236 p->clk = devm_clk_get(dev, NULL);
1237 if (IS_ERR(p->clk)) {
1238 dev_err(dev, "cannot get clock\n");
1239 return PTR_ERR(p->clk);
1240 }
1241
1242 i = platform_get_irq(pdev, 0);
1243 if (i < 0)
1244 return i;
1245
1246 p->mapbase = devm_platform_ioremap_resource(pdev, 0);
1247 if (IS_ERR(p->mapbase))
1248 return PTR_ERR(p->mapbase);
1249
1250 ret = devm_request_irq(dev, i, sh_msiof_spi_irq, 0, dev_name(dev), p);
1251 if (ret) {
1252 dev_err(dev, "unable to request irq\n");
1253 return ret;
1254 }
1255
1256 p->pdev = pdev;
1257 pm_runtime_enable(dev);
1258
1259 /* Platform data may override FIFO sizes */
1260 p->tx_fifo_size = chipdata->tx_fifo_size;
1261 p->rx_fifo_size = chipdata->rx_fifo_size;
1262 if (p->info->tx_fifo_override)
1263 p->tx_fifo_size = p->info->tx_fifo_override;
1264 if (p->info->rx_fifo_override)
1265 p->rx_fifo_size = p->info->rx_fifo_override;
1266
1267 /* init controller code */
1268 ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1269 ctlr->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
1270 clksrc = clk_get_rate(p->clk);
1271 ctlr->min_speed_hz = DIV_ROUND_UP(clksrc, 1024);
1272 ctlr->max_speed_hz = DIV_ROUND_UP(clksrc, 1 << p->min_div_pow);
1273 ctlr->flags = chipdata->ctlr_flags;
1274 ctlr->bus_num = pdev->id;
1275 ctlr->num_chipselect = p->info->num_chipselect;
1276 ctlr->setup = sh_msiof_spi_setup;
1277 ctlr->prepare_message = sh_msiof_prepare_message;
1278 ctlr->target_abort = sh_msiof_target_abort;
1279 ctlr->bits_per_word_mask = chipdata->bits_per_word_mask;
1280 ctlr->auto_runtime_pm = true;
1281 ctlr->transfer_one = sh_msiof_transfer_one;
1282 ctlr->use_gpio_descriptors = true;
1283 ctlr->max_native_cs = MAX_SS;
1284
1285 ret = sh_msiof_request_dma(p);
1286 if (ret < 0)
1287 dev_warn(dev, "DMA not available, using PIO\n");
1288
1289 ret = spi_register_controller(ctlr);
1290 if (ret < 0) {
1291 dev_err(dev, "failed to register controller\n");
1292 goto err2;
1293 }
1294
1295 return 0;
1296
1297 err2:
1298 sh_msiof_release_dma(p);
1299 pm_runtime_disable(dev);
1300
1301 return ret;
1302 }
1303
sh_msiof_spi_remove(struct platform_device * pdev)1304 static void sh_msiof_spi_remove(struct platform_device *pdev)
1305 {
1306 struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
1307
1308 spi_unregister_controller(p->ctlr);
1309
1310 sh_msiof_release_dma(p);
1311 pm_runtime_disable(&pdev->dev);
1312 }
1313
1314 static const struct platform_device_id spi_driver_ids[] = {
1315 { .name = "spi_sh_msiof", .driver_data = (kernel_ulong_t)&sh_data },
1316 { }
1317 };
1318 MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1319
sh_msiof_spi_suspend(struct device * dev)1320 static int sh_msiof_spi_suspend(struct device *dev)
1321 {
1322 struct sh_msiof_spi_priv *p = dev_get_drvdata(dev);
1323
1324 return spi_controller_suspend(p->ctlr);
1325 }
1326
sh_msiof_spi_resume(struct device * dev)1327 static int sh_msiof_spi_resume(struct device *dev)
1328 {
1329 struct sh_msiof_spi_priv *p = dev_get_drvdata(dev);
1330
1331 return spi_controller_resume(p->ctlr);
1332 }
1333
1334 static DEFINE_SIMPLE_DEV_PM_OPS(sh_msiof_spi_pm_ops, sh_msiof_spi_suspend,
1335 sh_msiof_spi_resume);
1336
1337 static struct platform_driver sh_msiof_spi_drv = {
1338 .probe = sh_msiof_spi_probe,
1339 .remove = sh_msiof_spi_remove,
1340 .id_table = spi_driver_ids,
1341 .driver = {
1342 .name = "spi_sh_msiof",
1343 .pm = pm_sleep_ptr(&sh_msiof_spi_pm_ops),
1344 .of_match_table = of_match_ptr(sh_msiof_match),
1345 },
1346 };
1347 module_platform_driver(sh_msiof_spi_drv);
1348
1349 MODULE_DESCRIPTION("SuperH MSIOF SPI Controller Interface Driver");
1350 MODULE_AUTHOR("Magnus Damm");
1351 MODULE_LICENSE("GPL v2");
1352