12 #include <linux/bitmap.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
30 #include <asm/unaligned.h>
63 #define CTR_TSCKE (1 << 15)
64 #define CTR_TFSE (1 << 14)
65 #define CTR_TXE (1 << 9)
66 #define CTR_RXE (1 << 8)
68 #define STR_TEOF (1 << 23)
69 #define STR_REOF (1 << 7)
103 data = sh_msiof_read(p,
CTR);
106 sh_msiof_write(p,
CTR, data);
108 for (k = 100; k > 0; k--) {
109 if ((sh_msiof_read(p,
CTR) & mask) ==
set)
118 static irqreturn_t sh_msiof_spi_irq(
int irq,
void *data)
123 sh_msiof_write(p,
IER, 0);
132 }
const sh_msiof_spi_clk_table[] = {
147 unsigned long parent_rate,
148 unsigned long spi_hz)
150 unsigned long div = 1024;
153 if (!
WARN_ON(!spi_hz || !parent_rate))
154 div = parent_rate / spi_hz;
158 for (k = 0; k <
ARRAY_SIZE(sh_msiof_spi_clk_table); k++) {
159 if (sh_msiof_spi_clk_table[k].div >= div)
165 sh_msiof_write(p,
TSCR, sh_msiof_spi_clk_table[k].
scr);
166 sh_msiof_write(p,
RSCR, sh_msiof_spi_clk_table[k].
scr);
171 u32 tx_hi_z,
u32 lsb_first)
183 sh_msiof_write(p,
FCTR, 0);
184 sh_msiof_write(p,
TMDR1, 0xe2000005 | (lsb_first << 24));
185 sh_msiof_write(p,
RMDR1, 0x22000005 | (lsb_first << 24));
195 tmp |= (tx_hi_z ? 2 : 0) << 22;
196 sh_msiof_write(p,
CTR, tmp);
200 const void *
tx_buf,
void *rx_buf,
203 u32 dr2 = ((bits - 1) << 24) | ((words - 1) << 16);
206 sh_msiof_write(p,
TMDR2, dr2);
208 sh_msiof_write(p,
TMDR2, dr2 | 1);
211 sh_msiof_write(p,
RMDR2, dr2);
218 sh_msiof_write(p,
STR, sh_msiof_read(p,
STR));
222 const void *tx_buf,
int words,
int fs)
224 const u8 *buf_8 = tx_buf;
227 for (k = 0; k < words; k++)
228 sh_msiof_write(p,
TFDR, buf_8[k] << fs);
232 const void *tx_buf,
int words,
int fs)
234 const u16 *buf_16 = tx_buf;
237 for (k = 0; k < words; k++)
238 sh_msiof_write(p,
TFDR, buf_16[k] << fs);
242 const void *tx_buf,
int words,
int fs)
244 const u16 *buf_16 = tx_buf;
247 for (k = 0; k < words; k++)
252 const void *tx_buf,
int words,
int fs)
254 const u32 *buf_32 = tx_buf;
257 for (k = 0; k < words; k++)
258 sh_msiof_write(p,
TFDR, buf_32[k] << fs);
262 const void *tx_buf,
int words,
int fs)
264 const u32 *buf_32 = tx_buf;
267 for (k = 0; k < words; k++)
272 const void *tx_buf,
int words,
int fs)
274 const u32 *buf_32 = tx_buf;
277 for (k = 0; k < words; k++)
278 sh_msiof_write(p,
TFDR,
swab32(buf_32[k] << fs));
282 const void *tx_buf,
int words,
int fs)
284 const u32 *buf_32 = tx_buf;
287 for (k = 0; k < words; k++)
292 void *rx_buf,
int words,
int fs)
297 for (k = 0; k < words; k++)
298 buf_8[k] = sh_msiof_read(p,
RFDR) >>
fs;
302 void *rx_buf,
int words,
int fs)
304 u16 *buf_16 = rx_buf;
307 for (k = 0; k < words; k++)
308 buf_16[k] = sh_msiof_read(p,
RFDR) >>
fs;
312 void *rx_buf,
int words,
int fs)
314 u16 *buf_16 = rx_buf;
317 for (k = 0; k < words; k++)
322 void *rx_buf,
int words,
int fs)
324 u32 *buf_32 = rx_buf;
327 for (k = 0; k < words; k++)
328 buf_32[k] = sh_msiof_read(p,
RFDR) >>
fs;
332 void *rx_buf,
int words,
int fs)
334 u32 *buf_32 = rx_buf;
337 for (k = 0; k < words; k++)
342 void *rx_buf,
int words,
int fs)
344 u32 *buf_32 = rx_buf;
347 for (k = 0; k < words; k++)
348 buf_32[k] =
swab32(sh_msiof_read(p,
RFDR) >> fs);
352 void *rx_buf,
int words,
int fs)
354 u32 *buf_32 = rx_buf;
357 for (k = 0; k < words; k++)
371 static unsigned long sh_msiof_spi_hz(
struct spi_device *spi,
382 static int sh_msiof_spi_setup_transfer(
struct spi_device *spi,
389 bits = sh_msiof_spi_bits(spi, t);
398 static void sh_msiof_spi_chipselect(
struct spi_device *spi,
int is_on)
411 pm_runtime_get_sync(&p->
pdev->dev);
428 pm_runtime_put(&p->
pdev->dev);
435 const void *,
int,
int),
438 const void *tx_buf,
void *rx_buf,
451 fifo_shift = 32 -
bits;
454 sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
458 tx_fifo(p, tx_buf, words, fifo_shift);
461 ret = sh_msiof_modify_ctr_wait(p, 0,
CTR_TSCKE);
463 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0,
CTR_RXE);
464 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0,
CTR_TXE);
468 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0,
CTR_TFSE);
470 dev_err(&p->
pdev->dev,
"failed to start hardware\n");
479 rx_fifo(p, rx_buf, words, fifo_shift);
482 sh_msiof_reset_str(p);
485 ret = sh_msiof_modify_ctr_wait(p,
CTR_TFSE, 0);
486 ret = ret ? ret : sh_msiof_modify_ctr_wait(p,
CTR_TXE, 0);
488 ret = ret ? ret : sh_msiof_modify_ctr_wait(p,
CTR_RXE, 0);
489 ret = ret ? ret : sh_msiof_modify_ctr_wait(p,
CTR_TSCKE, 0);
491 dev_err(&p->
pdev->dev,
"failed to shut down hardware\n");
498 sh_msiof_write(p,
IER, 0);
514 bits = sh_msiof_spi_bits(spi, t);
516 if (bits <= 8 && t->len > 15 && !(t->
len & 3)) {
526 tx_fifo = sh_msiof_spi_write_fifo_8;
527 rx_fifo = sh_msiof_spi_read_fifo_8;
528 }
else if (bits <= 16) {
530 if ((
unsigned long)t->
tx_buf & 0x01)
531 tx_fifo = sh_msiof_spi_write_fifo_16u;
533 tx_fifo = sh_msiof_spi_write_fifo_16;
535 if ((
unsigned long)t->
rx_buf & 0x01)
536 rx_fifo = sh_msiof_spi_read_fifo_16u;
538 rx_fifo = sh_msiof_spi_read_fifo_16;
541 if ((
unsigned long)t->
tx_buf & 0x03)
542 tx_fifo = sh_msiof_spi_write_fifo_s32u;
544 tx_fifo = sh_msiof_spi_write_fifo_s32;
546 if ((
unsigned long)t->
rx_buf & 0x03)
547 rx_fifo = sh_msiof_spi_read_fifo_s32u;
549 rx_fifo = sh_msiof_spi_read_fifo_s32;
552 if ((
unsigned long)t->
tx_buf & 0x03)
553 tx_fifo = sh_msiof_spi_write_fifo_32u;
555 tx_fifo = sh_msiof_spi_write_fifo_32;
557 if ((
unsigned long)t->
rx_buf & 0x03)
558 rx_fifo = sh_msiof_spi_read_fifo_32u;
560 rx_fifo = sh_msiof_spi_read_fifo_32;
565 sh_msiof_spi_hz(spi, t));
568 words = t->
len / bytes_per_word;
571 while (bytes_done < t->len) {
581 bytes_done += n * bytes_per_word;
605 if (master ==
NULL) {
606 dev_err(&pdev->
dev,
"failed to allocate spi master\n");
611 p = spi_master_get_devdata(master);
613 platform_set_drvdata(pdev, p);
614 p->
info = pdev->
dev.platform_data;
615 init_completion(&p->
done);
617 snprintf(clk_name,
sizeof(clk_name),
"msiof%d", pdev->
id);
619 if (IS_ERR(p->
clk)) {
620 dev_err(&pdev->
dev,
"cannot get clock \"%s\"\n", clk_name);
621 ret = PTR_ERR(p->
clk);
628 dev_err(&pdev->
dev,
"cannot get platform resources\n");
640 dev_name(&pdev->
dev), p);
642 dev_err(&pdev->
dev,
"unable to request irq\n");
654 if (p->
info->tx_fifo_override)
656 if (p->
info->rx_fifo_override)
669 p->
bitbang.chipselect = sh_msiof_spi_chipselect;
670 p->
bitbang.setup_transfer = sh_msiof_spi_setup_transfer;
671 p->
bitbang.txrx_bufs = sh_msiof_spi_txrx;
681 pm_runtime_disable(&pdev->
dev);
687 spi_master_put(master);
699 pm_runtime_disable(&pdev->
dev);
703 spi_master_put(p->
bitbang.master);
708 static int sh_msiof_spi_runtime_nop(
struct device *
dev)
720 static struct dev_pm_ops sh_msiof_spi_dev_pm_ops = {
721 .runtime_suspend = sh_msiof_spi_runtime_nop,
722 .runtime_resume = sh_msiof_spi_runtime_nop,
726 .probe = sh_msiof_spi_probe,
727 .remove = sh_msiof_spi_remove,
729 .name =
"spi_sh_msiof",
731 .pm = &sh_msiof_spi_dev_pm_ops,