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