aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--drivers/spi/spi-sh-msiof.c184
1 files changed, 123 insertions, 61 deletions
diff --git a/drivers/spi/spi-sh-msiof.c b/drivers/spi/spi-sh-msiof.c
index 81cc02f5f9b0..6e2ba62ceb63 100644
--- a/drivers/spi/spi-sh-msiof.c
+++ b/drivers/spi/spi-sh-msiof.c
@@ -42,32 +42,80 @@ struct sh_msiof_spi_priv {
42 int rx_fifo_size; 42 int rx_fifo_size;
43}; 43};
44 44
45#define TMDR1 0x00 45#define TMDR1 0x00 /* Transmit Mode Register 1 */
46#define TMDR2 0x04 46#define TMDR2 0x04 /* Transmit Mode Register 2 */
47#define TMDR3 0x08 47#define TMDR3 0x08 /* Transmit Mode Register 3 */
48#define RMDR1 0x10 48#define RMDR1 0x10 /* Receive Mode Register 1 */
49#define RMDR2 0x14 49#define RMDR2 0x14 /* Receive Mode Register 2 */
50#define RMDR3 0x18 50#define RMDR3 0x18 /* Receive Mode Register 3 */
51#define TSCR 0x20 51#define TSCR 0x20 /* Transmit Clock Select Register */
52#define RSCR 0x22 52#define RSCR 0x22 /* Receive Clock Select Register (SH, A1, APE6) */
53#define CTR 0x28 53#define CTR 0x28 /* Control Register */
54#define FCTR 0x30 54#define FCTR 0x30 /* FIFO Control Register */
55#define STR 0x40 55#define STR 0x40 /* Status Register */
56#define IER 0x44 56#define IER 0x44 /* Interrupt Enable Register */
57#define TDR1 0x48 57#define TDR1 0x48 /* Transmit Control Data Register 1 (SH, A1) */
58#define TDR2 0x4c 58#define TDR2 0x4c /* Transmit Control Data Register 2 (SH, A1) */
59#define TFDR 0x50 59#define TFDR 0x50 /* Transmit FIFO Data Register */
60#define RDR1 0x58 60#define RDR1 0x58 /* Receive Control Data Register 1 (SH, A1) */
61#define RDR2 0x5c 61#define RDR2 0x5c /* Receive Control Data Register 2 (SH, A1) */
62#define RFDR 0x60 62#define RFDR 0x60 /* Receive FIFO Data Register */
63 63
64#define CTR_TSCKE (1 << 15) 64/* TMDR1 and RMDR1 */
65#define CTR_TFSE (1 << 14) 65#define MDR1_TRMD 0x80000000 /* Transfer Mode (1 = Master mode) */
66#define CTR_TXE (1 << 9) 66#define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */
67#define CTR_RXE (1 << 8) 67#define MDR1_SYNCMD_SPI 0x20000000 /* Level mode/SPI */
68 68#define MDR1_SYNCMD_LR 0x30000000 /* L/R mode */
69#define STR_TEOF (1 << 23) 69#define MDR1_SYNCAC_SHIFT 25 /* Sync Polarity (1 = Active-low) */
70#define STR_REOF (1 << 7) 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
71 119
72static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs) 120static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
73{ 121{
@@ -131,22 +179,21 @@ static struct {
131 unsigned short div; 179 unsigned short div;
132 unsigned short scr; 180 unsigned short scr;
133} const sh_msiof_spi_clk_table[] = { 181} const sh_msiof_spi_clk_table[] = {
134 { 1, 0x0007 }, 182 { 1, SCR_BRPS( 1) | SCR_BRDV_DIV_1 },
135 { 2, 0x0000 }, 183 { 2, SCR_BRPS( 1) | SCR_BRDV_DIV_2 },
136 { 4, 0x0001 }, 184 { 4, SCR_BRPS( 1) | SCR_BRDV_DIV_4 },
137 { 8, 0x0002 }, 185 { 8, SCR_BRPS( 1) | SCR_BRDV_DIV_8 },
138 { 16, 0x0003 }, 186 { 16, SCR_BRPS( 1) | SCR_BRDV_DIV_16 },
139 { 32, 0x0004 }, 187 { 32, SCR_BRPS( 1) | SCR_BRDV_DIV_32 },
140 { 64, 0x1f00 }, 188 { 64, SCR_BRPS(32) | SCR_BRDV_DIV_2 },
141 { 128, 0x1f01 }, 189 { 128, SCR_BRPS(32) | SCR_BRDV_DIV_4 },
142 { 256, 0x1f02 }, 190 { 256, SCR_BRPS(32) | SCR_BRDV_DIV_8 },
143 { 512, 0x1f03 }, 191 { 512, SCR_BRPS(32) | SCR_BRDV_DIV_16 },
144 { 1024, 0x1f04 }, 192 { 1024, SCR_BRPS(32) | SCR_BRDV_DIV_32 },
145}; 193};
146 194
147static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p, 195static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
148 unsigned long parent_rate, 196 unsigned long parent_rate, u32 spi_hz)
149 unsigned long spi_hz)
150{ 197{
151 unsigned long div = 1024; 198 unsigned long div = 1024;
152 size_t k; 199 size_t k;
@@ -183,21 +230,21 @@ static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p,
183 */ 230 */
184 sh_msiof_write(p, FCTR, 0); 231 sh_msiof_write(p, FCTR, 0);
185 232
186 tmp = 0; 233 tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP;
187 tmp |= !cs_high << 25; 234 tmp |= !cs_high << MDR1_SYNCAC_SHIFT;
188 tmp |= lsb_first << 24; 235 tmp |= lsb_first << MDR1_BITLSB_SHIFT;
189 sh_msiof_write(p, TMDR1, 0xe0000005 | tmp); 236 sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON);
190 sh_msiof_write(p, RMDR1, 0x20000005 | tmp); 237 sh_msiof_write(p, RMDR1, tmp);
191 238
192 tmp = 0xa0000000; 239 tmp = 0;
193 tmp |= cpol << 30; /* TSCKIZ */ 240 tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT;
194 tmp |= cpol << 28; /* RSCKIZ */ 241 tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT;
195 242
196 edge = cpol ^ !cpha; 243 edge = cpol ^ !cpha;
197 244
198 tmp |= edge << 27; /* TEDG */ 245 tmp |= edge << CTR_TEDG_SHIFT;
199 tmp |= edge << 26; /* REDG */ 246 tmp |= edge << CTR_REDG_SHIFT;
200 tmp |= (tx_hi_z ? 2 : 0) << 22; /* TXDIZ */ 247 tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW;
201 sh_msiof_write(p, CTR, tmp); 248 sh_msiof_write(p, CTR, tmp);
202} 249}
203 250
@@ -205,12 +252,12 @@ static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
205 const void *tx_buf, void *rx_buf, 252 const void *tx_buf, void *rx_buf,
206 u32 bits, u32 words) 253 u32 bits, u32 words)
207{ 254{
208 u32 dr2 = ((bits - 1) << 24) | ((words - 1) << 16); 255 u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words);
209 256
210 if (tx_buf) 257 if (tx_buf)
211 sh_msiof_write(p, TMDR2, dr2); 258 sh_msiof_write(p, TMDR2, dr2);
212 else 259 else
213 sh_msiof_write(p, TMDR2, dr2 | 1); 260 sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1);
214 261
215 if (rx_buf) 262 if (rx_buf)
216 sh_msiof_write(p, RMDR2, dr2); 263 sh_msiof_write(p, RMDR2, dr2);
@@ -373,10 +420,9 @@ static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t)
373 return bits; 420 return bits;
374} 421}
375 422
376static unsigned long sh_msiof_spi_hz(struct spi_device *spi, 423static u32 sh_msiof_spi_hz(struct spi_device *spi, struct spi_transfer *t)
377 struct spi_transfer *t)
378{ 424{
379 unsigned long hz; 425 u32 hz;
380 426
381 hz = t ? t->speed_hz : 0; 427 hz = t ? t->speed_hz : 0;
382 if (!hz) 428 if (!hz)
@@ -400,6 +446,21 @@ static int sh_msiof_spi_setup_transfer(struct spi_device *spi,
400 return spi_bitbang_setup_transfer(spi, t); 446 return spi_bitbang_setup_transfer(spi, t);
401} 447}
402 448
449static 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
403static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on) 464static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
404{ 465{
405 struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master); 466 struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master);
@@ -425,8 +486,8 @@ static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on)
425 !!(spi->mode & SPI_CS_HIGH)); 486 !!(spi->mode & SPI_CS_HIGH));
426 } 487 }
427 488
428 /* use spi->controller data for CS (same strategy as spi_gpio) */ 489 if (spi->cs_gpio >= 0)
429 gpio_set_value((uintptr_t)spi->controller_data, value); 490 gpio_set_value(spi->cs_gpio, value);
430 491
431 if (is_on == BITBANG_CS_INACTIVE) { 492 if (is_on == BITBANG_CS_INACTIVE) {
432 if (test_and_clear_bit(0, &p->flags)) { 493 if (test_and_clear_bit(0, &p->flags)) {
@@ -487,7 +548,7 @@ static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
487 /* clear status bits */ 548 /* clear status bits */
488 sh_msiof_reset_str(p); 549 sh_msiof_reset_str(p);
489 550
490 /* shut down frame, tx/tx and clock signals */ 551 /* shut down frame, rx/tx and clock signals */
491 ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0); 552 ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0);
492 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0); 553 ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0);
493 if (rx_buf) 554 if (rx_buf)
@@ -697,8 +758,8 @@ static int sh_msiof_spi_probe(struct platform_device *pdev)
697 pm_runtime_enable(&pdev->dev); 758 pm_runtime_enable(&pdev->dev);
698 759
699 /* The standard version of MSIOF use 64 word FIFOs */ 760 /* The standard version of MSIOF use 64 word FIFOs */
700 p->tx_fifo_size = 64; 761 p->tx_fifo_size = DEFAULT_TX_FIFO_SIZE;
701 p->rx_fifo_size = 64; 762 p->rx_fifo_size = DEFAULT_RX_FIFO_SIZE;
702 763
703 /* Platform data may override FIFO sizes */ 764 /* Platform data may override FIFO sizes */
704 if (p->info->tx_fifo_override) 765 if (p->info->tx_fifo_override)
@@ -711,8 +772,9 @@ static int sh_msiof_spi_probe(struct platform_device *pdev)
711 master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE; 772 master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
712 master->flags = 0; 773 master->flags = 0;
713 master->bus_num = pdev->id; 774 master->bus_num = pdev->id;
775 master->dev.of_node = pdev->dev.of_node;
714 master->num_chipselect = p->info->num_chipselect; 776 master->num_chipselect = p->info->num_chipselect;
715 master->setup = spi_bitbang_setup; 777 master->setup = sh_msiof_spi_setup;
716 master->cleanup = spi_bitbang_cleanup; 778 master->cleanup = spi_bitbang_cleanup;
717 779
718 p->bitbang.master = master; 780 p->bitbang.master = master;