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