spi: sh-msiof: Improve bindings
[cascardo/linux.git] / drivers / spi / spi-sh-msiof.c
1 /*
2  * SuperH MSIOF SPI Master Interface
3  *
4  * Copyright (c) 2009 Magnus Damm
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
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/err.h>
17 #include <linux/gpio.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26
27 #include <linux/spi/sh_msiof.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/spi_bitbang.h>
30
31 #include <asm/unaligned.h>
32
33 struct sh_msiof_spi_priv {
34         struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */
35         void __iomem *mapbase;
36         struct clk *clk;
37         struct platform_device *pdev;
38         struct sh_msiof_spi_info *info;
39         struct completion done;
40         unsigned long flags;
41         int tx_fifo_size;
42         int rx_fifo_size;
43 };
44
45 #define TMDR1   0x00    /* Transmit Mode Register 1 */
46 #define TMDR2   0x04    /* Transmit Mode Register 2 */
47 #define TMDR3   0x08    /* Transmit Mode Register 3 */
48 #define RMDR1   0x10    /* Receive Mode Register 1 */
49 #define RMDR2   0x14    /* Receive Mode Register 2 */
50 #define RMDR3   0x18    /* Receive Mode Register 3 */
51 #define TSCR    0x20    /* Transmit Clock Select Register */
52 #define RSCR    0x22    /* Receive Clock Select Register (SH, A1, APE6) */
53 #define CTR     0x28    /* Control Register */
54 #define FCTR    0x30    /* FIFO Control Register */
55 #define STR     0x40    /* Status Register */
56 #define IER     0x44    /* Interrupt Enable Register */
57 #define TDR1    0x48    /* Transmit Control Data Register 1 (SH, A1) */
58 #define TDR2    0x4c    /* Transmit Control Data Register 2 (SH, A1) */
59 #define TFDR    0x50    /* Transmit FIFO Data Register */
60 #define RDR1    0x58    /* Receive Control Data Register 1 (SH, A1) */
61 #define RDR2    0x5c    /* Receive Control Data Register 2 (SH, A1) */
62 #define RFDR    0x60    /* Receive FIFO Data Register */
63
64 /* TMDR1 and RMDR1 */
65 #define MDR1_TRMD        0x80000000 /* Transfer Mode (1 = Master mode) */
66 #define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */
67 #define MDR1_SYNCMD_SPI  0x20000000 /*   Level mode/SPI */
68 #define MDR1_SYNCMD_LR   0x30000000 /*   L/R mode */
69 #define MDR1_SYNCAC_SHIFT        25 /* Sync Polarity (1 = Active-low) */
70 #define MDR1_BITLSB_SHIFT        24 /* MSB/LSB First (1 = LSB first) */
71 #define MDR1_FLD_MASK    0x000000c0 /* Frame Sync Signal Interval (0-3) */
72 #define MDR1_FLD_SHIFT            2
73 #define MDR1_XXSTP       0x00000001 /* Transmission/Reception Stop on FIFO */
74 /* TMDR1 */
75 #define TMDR1_PCON       0x40000000 /* Transfer Signal Connection */
76
77 /* TMDR2 and RMDR2 */
78 #define MDR2_BITLEN1(i) (((i) - 1) << 24) /* Data Size (8-32 bits) */
79 #define MDR2_WDLEN1(i)  (((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */
80 #define MDR2_GRPMASK1   0x00000001 /* Group Output Mask 1 (SH, A1) */
81
82 /* TSCR and RSCR */
83 #define SCR_BRPS_MASK       0x1f00 /* Prescaler Setting (1-32) */
84 #define SCR_BRPS(i)     (((i) - 1) << 8)
85 #define SCR_BRDV_MASK       0x0007 /* Baud Rate Generator's Division Ratio */
86 #define SCR_BRDV_DIV_2      0x0000
87 #define SCR_BRDV_DIV_4      0x0001
88 #define SCR_BRDV_DIV_8      0x0002
89 #define SCR_BRDV_DIV_16     0x0003
90 #define SCR_BRDV_DIV_32     0x0004
91 #define SCR_BRDV_DIV_1      0x0007
92
93 /* CTR */
94 #define CTR_TSCKIZ_MASK 0xc0000000 /* Transmit Clock I/O Polarity Select */
95 #define CTR_TSCKIZ_SCK  0x80000000 /*   Disable SCK when TX disabled */
96 #define CTR_TSCKIZ_POL_SHIFT    30 /*   Transmit Clock Polarity */
97 #define CTR_RSCKIZ_MASK 0x30000000 /* Receive Clock Polarity Select */
98 #define CTR_RSCKIZ_SCK  0x20000000 /*   Must match CTR_TSCKIZ_SCK */
99 #define CTR_RSCKIZ_POL_SHIFT    28 /*   Receive Clock Polarity */
100 #define CTR_TEDG_SHIFT          27 /* Transmit Timing (1 = falling edge) */
101 #define CTR_REDG_SHIFT          26 /* Receive Timing (1 = falling edge) */
102 #define CTR_TXDIZ_MASK  0x00c00000 /* Pin Output When TX is Disabled */
103 #define CTR_TXDIZ_LOW   0x00000000 /*   0 */
104 #define CTR_TXDIZ_HIGH  0x00400000 /*   1 */
105 #define CTR_TXDIZ_HIZ   0x00800000 /*   High-impedance */
106 #define CTR_TSCKE       0x00008000 /* Transmit Serial Clock Output Enable */
107 #define CTR_TFSE        0x00004000 /* Transmit Frame Sync Signal Output Enable */
108 #define CTR_TXE         0x00000200 /* Transmit Enable */
109 #define CTR_RXE         0x00000100 /* Receive Enable */
110
111 /* STR and IER */
112 #define STR_TEOF        0x00800000 /* Frame Transmission End */
113 #define STR_REOF        0x00000080 /* Frame Reception End */
114
115
116 #define DEFAULT_TX_FIFO_SIZE    64
117 #define DEFAULT_RX_FIFO_SIZE    64
118
119
120 static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
121 {
122         switch (reg_offs) {
123         case TSCR:
124         case RSCR:
125                 return ioread16(p->mapbase + reg_offs);
126         default:
127                 return ioread32(p->mapbase + reg_offs);
128         }
129 }
130
131 static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
132                            u32 value)
133 {
134         switch (reg_offs) {
135         case TSCR:
136         case RSCR:
137                 iowrite16(value, p->mapbase + reg_offs);
138                 break;
139         default:
140                 iowrite32(value, p->mapbase + reg_offs);
141                 break;
142         }
143 }
144
145 static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
146                                     u32 clr, u32 set)
147 {
148         u32 mask = clr | set;
149         u32 data;
150         int k;
151
152         data = sh_msiof_read(p, CTR);
153         data &= ~clr;
154         data |= set;
155         sh_msiof_write(p, CTR, data);
156
157         for (k = 100; k > 0; k--) {
158                 if ((sh_msiof_read(p, CTR) & mask) == set)
159                         break;
160
161                 udelay(10);
162         }
163
164         return k > 0 ? 0 : -ETIMEDOUT;
165 }
166
167 static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
168 {
169         struct sh_msiof_spi_priv *p = data;
170
171         /* just disable the interrupt and wake up */
172         sh_msiof_write(p, IER, 0);
173         complete(&p->done);
174
175         return IRQ_HANDLED;
176 }
177
178 static struct {
179         unsigned short div;
180         unsigned short scr;
181 } const sh_msiof_spi_clk_table[] = {
182         { 1,    SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
183         { 2,    SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
184         { 4,    SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
185         { 8,    SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
186         { 16,   SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
187         { 32,   SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
188         { 64,   SCR_BRPS(32) | SCR_BRDV_DIV_2 },
189         { 128,  SCR_BRPS(32) | SCR_BRDV_DIV_4 },
190         { 256,  SCR_BRPS(32) | SCR_BRDV_DIV_8 },
191         { 512,  SCR_BRPS(32) | SCR_BRDV_DIV_16 },
192         { 1024, SCR_BRPS(32) | SCR_BRDV_DIV_32 },
193 };
194
195 static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
196                                       unsigned long parent_rate, u32 spi_hz)
197 {
198         unsigned long div = 1024;
199         size_t k;
200
201         if (!WARN_ON(!spi_hz || !parent_rate))
202                 div = DIV_ROUND_UP(parent_rate, spi_hz);
203
204         /* TODO: make more fine grained */
205
206         for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
207                 if (sh_msiof_spi_clk_table[k].div >= div)
208                         break;
209         }
210
211         k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_clk_table) - 1);
212
213         sh_msiof_write(p, TSCR, sh_msiof_spi_clk_table[k].scr);
214         sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr);
215 }
216
217 static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
218                                       u32 cpol, u32 cpha,
219                                       u32 tx_hi_z, u32 lsb_first, u32 cs_high)
220 {
221         u32 tmp;
222         int edge;
223
224         /*
225          * CPOL CPHA     TSCKIZ RSCKIZ TEDG REDG
226          *    0    0         10     10    1    1
227          *    0    1         10     10    0    0
228          *    1    0         11     11    0    0
229          *    1    1         11     11    1    1
230          */
231         sh_msiof_write(p, FCTR, 0);
232
233         tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP;
234         tmp |= !cs_high << MDR1_SYNCAC_SHIFT;
235         tmp |= lsb_first << MDR1_BITLSB_SHIFT;
236         sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON);
237         sh_msiof_write(p, RMDR1, tmp);
238
239         tmp = 0;
240         tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT;
241         tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT;
242
243         edge = cpol ^ !cpha;
244
245         tmp |= edge << CTR_TEDG_SHIFT;
246         tmp |= edge << CTR_REDG_SHIFT;
247         tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW;
248         sh_msiof_write(p, CTR, tmp);
249 }
250
251 static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
252                                        const void *tx_buf, void *rx_buf,
253                                        u32 bits, u32 words)
254 {
255         u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words);
256
257         if (tx_buf)
258                 sh_msiof_write(p, TMDR2, dr2);
259         else
260                 sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1);
261
262         if (rx_buf)
263                 sh_msiof_write(p, RMDR2, dr2);
264
265         sh_msiof_write(p, IER, STR_TEOF | STR_REOF);
266 }
267
268 static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
269 {
270         sh_msiof_write(p, STR, sh_msiof_read(p, STR));
271 }
272
273 static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
274                                       const void *tx_buf, int words, int fs)
275 {
276         const u8 *buf_8 = tx_buf;
277         int k;
278
279         for (k = 0; k < words; k++)
280                 sh_msiof_write(p, TFDR, buf_8[k] << fs);
281 }
282
283 static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
284                                        const void *tx_buf, int words, int fs)
285 {
286         const u16 *buf_16 = tx_buf;
287         int k;
288
289         for (k = 0; k < words; k++)
290                 sh_msiof_write(p, TFDR, buf_16[k] << fs);
291 }
292
293 static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
294                                         const void *tx_buf, int words, int fs)
295 {
296         const u16 *buf_16 = tx_buf;
297         int k;
298
299         for (k = 0; k < words; k++)
300                 sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs);
301 }
302
303 static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
304                                        const void *tx_buf, int words, int fs)
305 {
306         const u32 *buf_32 = tx_buf;
307         int k;
308
309         for (k = 0; k < words; k++)
310                 sh_msiof_write(p, TFDR, buf_32[k] << fs);
311 }
312
313 static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
314                                         const void *tx_buf, int words, int fs)
315 {
316         const u32 *buf_32 = tx_buf;
317         int k;
318
319         for (k = 0; k < words; k++)
320                 sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs);
321 }
322
323 static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
324                                         const void *tx_buf, int words, int fs)
325 {
326         const u32 *buf_32 = tx_buf;
327         int k;
328
329         for (k = 0; k < words; k++)
330                 sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs));
331 }
332
333 static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
334                                          const void *tx_buf, int words, int fs)
335 {
336         const u32 *buf_32 = tx_buf;
337         int k;
338
339         for (k = 0; k < words; k++)
340                 sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs));
341 }
342
343 static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
344                                      void *rx_buf, int words, int fs)
345 {
346         u8 *buf_8 = rx_buf;
347         int k;
348
349         for (k = 0; k < words; k++)
350                 buf_8[k] = sh_msiof_read(p, RFDR) >> fs;
351 }
352
353 static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
354                                       void *rx_buf, int words, int fs)
355 {
356         u16 *buf_16 = rx_buf;
357         int k;
358
359         for (k = 0; k < words; k++)
360                 buf_16[k] = sh_msiof_read(p, RFDR) >> fs;
361 }
362
363 static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
364                                        void *rx_buf, int words, int fs)
365 {
366         u16 *buf_16 = rx_buf;
367         int k;
368
369         for (k = 0; k < words; k++)
370                 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]);
371 }
372
373 static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
374                                       void *rx_buf, int words, int fs)
375 {
376         u32 *buf_32 = rx_buf;
377         int k;
378
379         for (k = 0; k < words; k++)
380                 buf_32[k] = sh_msiof_read(p, RFDR) >> fs;
381 }
382
383 static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
384                                        void *rx_buf, int words, int fs)
385 {
386         u32 *buf_32 = rx_buf;
387         int k;
388
389         for (k = 0; k < words; k++)
390                 put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]);
391 }
392
393 static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
394                                        void *rx_buf, int words, int fs)
395 {
396         u32 *buf_32 = rx_buf;
397         int k;
398
399         for (k = 0; k < words; k++)
400                 buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs);
401 }
402
403 static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
404                                        void *rx_buf, int words, int fs)
405 {
406         u32 *buf_32 = rx_buf;
407         int k;
408
409         for (k = 0; k < words; k++)
410                 put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]);
411 }
412
413 static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
414 {
415         int bits;
416
417         bits = t ? t->bits_per_word : 0;
418         if (!bits)
419                 bits = spi->bits_per_word;
420         return bits;
421 }
422
423 static u32 sh_msiof_spi_hz(struct spi_device *spi, struct spi_transfer *t)
424 {
425         u32 hz;
426
427         hz = t ? t->speed_hz : 0;
428         if (!hz)
429                 hz = spi->max_speed_hz;
430         return hz;
431 }
432
433 static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
434                                        struct spi_transfer *t)
435 {
436         int bits;
437
438         /* noting to check hz values against since parent clock is disabled */
439
440         bits = sh_msiof_spi_bits(spi, t);
441         if (bits < 8)
442                 return -EINVAL;
443         if (bits > 32)
444                 return -EINVAL;
445
446         return spi_bitbang_setup_transfer(spi, t);
447 }
448
449 static int sh_msiof_spi_setup(struct spi_device *spi)
450 {
451         struct device_node      *np = spi->master->dev.of_node;
452
453         if (!np) {
454                 /*
455                  * Use spi->controller_data for CS (same strategy as spi_gpio),
456                  * if any. otherwise let HW control CS
457                  */
458                 spi->cs_gpio = (uintptr_t)spi->controller_data;
459         }
460
461         return spi_bitbang_setup(spi);
462 }
463
464 static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
465 {
466         struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
467         int value;
468
469         /* chip select is active low unless SPI_CS_HIGH is set */
470         if (spi->mode & SPI_CS_HIGH)
471                 value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0;
472         else
473                 value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1;
474
475         if (is_on == BITBANG_CS_ACTIVE) {
476                 if (!test_and_set_bit(0, &p->flags)) {
477                         pm_runtime_get_sync(&p->pdev->dev);
478                         clk_enable(p->clk);
479                 }
480
481                 /* Configure pins before asserting CS */
482                 sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL),
483                                           !!(spi->mode & SPI_CPHA),
484                                           !!(spi->mode & SPI_3WIRE),
485                                           !!(spi->mode & SPI_LSB_FIRST),
486                                           !!(spi->mode & SPI_CS_HIGH));
487         }
488
489         if (spi->cs_gpio >= 0)
490                 gpio_set_value(spi->cs_gpio, value);
491
492         if (is_on == BITBANG_CS_INACTIVE) {
493                 if (test_and_clear_bit(0, &p->flags)) {
494                         clk_disable(p->clk);
495                         pm_runtime_put(&p->pdev->dev);
496                 }
497         }
498 }
499
500 static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
501                                   void (*tx_fifo)(struct sh_msiof_spi_priv *,
502                                                   const void *, int, int),
503                                   void (*rx_fifo)(struct sh_msiof_spi_priv *,
504                                                   void *, int, int),
505                                   const void *tx_buf, void *rx_buf,
506                                   int words, int bits)
507 {
508         int fifo_shift;
509         int ret;
510
511         /* limit maximum word transfer to rx/tx fifo size */
512         if (tx_buf)
513                 words = min_t(int, words, p->tx_fifo_size);
514         if (rx_buf)
515                 words = min_t(int, words, p->rx_fifo_size);
516
517         /* the fifo contents need shifting */
518         fifo_shift = 32 - bits;
519
520         /* setup msiof transfer mode registers */
521         sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
522
523         /* write tx fifo */
524         if (tx_buf)
525                 tx_fifo(p, tx_buf, words, fifo_shift);
526
527         /* setup clock and rx/tx signals */
528         ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE);
529         if (rx_buf)
530                 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE);
531         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE);
532
533         /* start by setting frame bit */
534         reinit_completion(&p->done);
535         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE);
536         if (ret) {
537                 dev_err(&p->pdev->dev, "failed to start hardware\n");
538                 goto err;
539         }
540
541         /* wait for tx fifo to be emptied / rx fifo to be filled */
542         wait_for_completion(&p->done);
543
544         /* read rx fifo */
545         if (rx_buf)
546                 rx_fifo(p, rx_buf, words, fifo_shift);
547
548         /* clear status bits */
549         sh_msiof_reset_str(p);
550
551         /* shut down frame, rx/tx and clock signals */
552         ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
553         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
554         if (rx_buf)
555                 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0);
556         ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0);
557         if (ret) {
558                 dev_err(&p->pdev->dev, "failed to shut down hardware\n");
559                 goto err;
560         }
561
562         return words;
563
564  err:
565         sh_msiof_write(p, IER, 0);
566         return ret;
567 }
568
569 static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t)
570 {
571         struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
572         void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
573         void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
574         int bits;
575         int bytes_per_word;
576         int bytes_done;
577         int words;
578         int n;
579         bool swab;
580
581         bits = sh_msiof_spi_bits(spi, t);
582
583         if (bits <= 8 && t->len > 15 && !(t->len & 3)) {
584                 bits = 32;
585                 swab = true;
586         } else {
587                 swab = false;
588         }
589
590         /* setup bytes per word and fifo read/write functions */
591         if (bits <= 8) {
592                 bytes_per_word = 1;
593                 tx_fifo = sh_msiof_spi_write_fifo_8;
594                 rx_fifo = sh_msiof_spi_read_fifo_8;
595         } else if (bits <= 16) {
596                 bytes_per_word = 2;
597                 if ((unsigned long)t->tx_buf & 0x01)
598                         tx_fifo = sh_msiof_spi_write_fifo_16u;
599                 else
600                         tx_fifo = sh_msiof_spi_write_fifo_16;
601
602                 if ((unsigned long)t->rx_buf & 0x01)
603                         rx_fifo = sh_msiof_spi_read_fifo_16u;
604                 else
605                         rx_fifo = sh_msiof_spi_read_fifo_16;
606         } else if (swab) {
607                 bytes_per_word = 4;
608                 if ((unsigned long)t->tx_buf & 0x03)
609                         tx_fifo = sh_msiof_spi_write_fifo_s32u;
610                 else
611                         tx_fifo = sh_msiof_spi_write_fifo_s32;
612
613                 if ((unsigned long)t->rx_buf & 0x03)
614                         rx_fifo = sh_msiof_spi_read_fifo_s32u;
615                 else
616                         rx_fifo = sh_msiof_spi_read_fifo_s32;
617         } else {
618                 bytes_per_word = 4;
619                 if ((unsigned long)t->tx_buf & 0x03)
620                         tx_fifo = sh_msiof_spi_write_fifo_32u;
621                 else
622                         tx_fifo = sh_msiof_spi_write_fifo_32;
623
624                 if ((unsigned long)t->rx_buf & 0x03)
625                         rx_fifo = sh_msiof_spi_read_fifo_32u;
626                 else
627                         rx_fifo = sh_msiof_spi_read_fifo_32;
628         }
629
630         /* setup clocks (clock already enabled in chipselect()) */
631         sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk),
632                                   sh_msiof_spi_hz(spi, t));
633
634         /* transfer in fifo sized chunks */
635         words = t->len / bytes_per_word;
636         bytes_done = 0;
637
638         while (bytes_done < t->len) {
639                 void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL;
640                 const void *tx_buf = t->tx_buf ? t->tx_buf + bytes_done : NULL;
641                 n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo,
642                                            tx_buf,
643                                            rx_buf,
644                                            words, bits);
645                 if (n < 0)
646                         break;
647
648                 bytes_done += n * bytes_per_word;
649                 words -= n;
650         }
651
652         return bytes_done;
653 }
654
655 static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
656                                   u32 word, u8 bits)
657 {
658         BUG(); /* unused but needed by bitbang code */
659         return 0;
660 }
661
662 #ifdef CONFIG_OF
663 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
664 {
665         struct sh_msiof_spi_info *info;
666         struct device_node *np = dev->of_node;
667         u32 num_cs = 1;
668
669         info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
670         if (!info) {
671                 dev_err(dev, "failed to allocate setup data\n");
672                 return NULL;
673         }
674
675         /* Parse the MSIOF properties */
676         of_property_read_u32(np, "num-cs", &num_cs);
677         of_property_read_u32(np, "renesas,tx-fifo-size",
678                                         &info->tx_fifo_override);
679         of_property_read_u32(np, "renesas,rx-fifo-size",
680                                         &info->rx_fifo_override);
681
682         info->num_chipselect = num_cs;
683
684         return info;
685 }
686 #else
687 static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
688 {
689         return NULL;
690 }
691 #endif
692
693 static int sh_msiof_spi_probe(struct platform_device *pdev)
694 {
695         struct resource *r;
696         struct spi_master *master;
697         struct sh_msiof_spi_priv *p;
698         int i;
699         int ret;
700
701         master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv));
702         if (master == NULL) {
703                 dev_err(&pdev->dev, "failed to allocate spi master\n");
704                 return -ENOMEM;
705         }
706
707         p = spi_master_get_devdata(master);
708
709         platform_set_drvdata(pdev, p);
710         if (pdev->dev.of_node)
711                 p->info = sh_msiof_spi_parse_dt(&pdev->dev);
712         else
713                 p->info = dev_get_platdata(&pdev->dev);
714
715         if (!p->info) {
716                 dev_err(&pdev->dev, "failed to obtain device info\n");
717                 ret = -ENXIO;
718                 goto err1;
719         }
720
721         init_completion(&p->done);
722
723         p->clk = devm_clk_get(&pdev->dev, NULL);
724         if (IS_ERR(p->clk)) {
725                 dev_err(&pdev->dev, "cannot get clock\n");
726                 ret = PTR_ERR(p->clk);
727                 goto err1;
728         }
729
730         i = platform_get_irq(pdev, 0);
731         if (i < 0) {
732                 dev_err(&pdev->dev, "cannot get platform IRQ\n");
733                 ret = -ENOENT;
734                 goto err1;
735         }
736
737         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
738         p->mapbase = devm_ioremap_resource(&pdev->dev, r);
739         if (IS_ERR(p->mapbase)) {
740                 ret = PTR_ERR(p->mapbase);
741                 goto err1;
742         }
743
744         ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
745                                dev_name(&pdev->dev), p);
746         if (ret) {
747                 dev_err(&pdev->dev, "unable to request irq\n");
748                 goto err1;
749         }
750
751         ret = clk_prepare(p->clk);
752         if (ret < 0) {
753                 dev_err(&pdev->dev, "unable to prepare clock\n");
754                 goto err1;
755         }
756
757         p->pdev = pdev;
758         pm_runtime_enable(&pdev->dev);
759
760         /* The standard version of MSIOF use 64 word FIFOs */
761         p->tx_fifo_size = DEFAULT_TX_FIFO_SIZE;
762         p->rx_fifo_size = DEFAULT_RX_FIFO_SIZE;
763
764         /* Platform data may override FIFO sizes */
765         if (p->info->tx_fifo_override)
766                 p->tx_fifo_size = p->info->tx_fifo_override;
767         if (p->info->rx_fifo_override)
768                 p->rx_fifo_size = p->info->rx_fifo_override;
769
770         /* init master and bitbang code */
771         master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
772         master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
773         master->flags = 0;
774         master->bus_num = pdev->id;
775         master->dev.of_node = pdev->dev.of_node;
776         master->num_chipselect = p->info->num_chipselect;
777         master->setup = sh_msiof_spi_setup;
778         master->cleanup = spi_bitbang_cleanup;
779
780         p->bitbang.master = master;
781         p->bitbang.chipselect = sh_msiof_spi_chipselect;
782         p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
783         p->bitbang.txrx_bufs = sh_msiof_spi_txrx;
784         p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word;
785         p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word;
786         p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word;
787         p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word;
788
789         ret = spi_bitbang_start(&p->bitbang);
790         if (ret == 0)
791                 return 0;
792
793         pm_runtime_disable(&pdev->dev);
794         clk_unprepare(p->clk);
795  err1:
796         spi_master_put(master);
797         return ret;
798 }
799
800 static int sh_msiof_spi_remove(struct platform_device *pdev)
801 {
802         struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
803         int ret;
804
805         ret = spi_bitbang_stop(&p->bitbang);
806         if (!ret) {
807                 pm_runtime_disable(&pdev->dev);
808                 clk_unprepare(p->clk);
809                 spi_master_put(p->bitbang.master);
810         }
811         return ret;
812 }
813
814 #ifdef CONFIG_OF
815 static const struct of_device_id sh_msiof_match[] = {
816         { .compatible = "renesas,sh-msiof", },
817         { .compatible = "renesas,sh-mobile-msiof", },
818         {},
819 };
820 MODULE_DEVICE_TABLE(of, sh_msiof_match);
821 #endif
822
823 static struct platform_driver sh_msiof_spi_drv = {
824         .probe          = sh_msiof_spi_probe,
825         .remove         = sh_msiof_spi_remove,
826         .driver         = {
827                 .name           = "spi_sh_msiof",
828                 .owner          = THIS_MODULE,
829                 .of_match_table = of_match_ptr(sh_msiof_match),
830         },
831 };
832 module_platform_driver(sh_msiof_spi_drv);
833
834 MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver");
835 MODULE_AUTHOR("Magnus Damm");
836 MODULE_LICENSE("GPL v2");
837 MODULE_ALIAS("platform:spi_sh_msiof");