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> |
| 18 | #include <linux/init.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/kernel.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 22 | #include <linux/module.h> |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 23 | #include <linux/of.h> |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 24 | #include <linux/of_device.h> |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pm_runtime.h> |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 27 | |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 28 | #include <linux/spi/sh_msiof.h> |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 29 | #include <linux/spi/spi.h> |
| 30 | #include <linux/spi/spi_bitbang.h> |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 31 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
| 33 | |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 34 | |
| 35 | struct sh_msiof_chipdata { |
| 36 | u16 tx_fifo_size; |
| 37 | u16 rx_fifo_size; |
| 38 | }; |
| 39 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 40 | struct sh_msiof_spi_priv { |
| 41 | struct spi_bitbang bitbang; /* must be first for spi_bitbang.c */ |
| 42 | void __iomem *mapbase; |
| 43 | struct clk *clk; |
| 44 | struct platform_device *pdev; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 45 | const struct sh_msiof_chipdata *chipdata; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 46 | struct sh_msiof_spi_info *info; |
| 47 | struct completion done; |
| 48 | unsigned long flags; |
| 49 | int tx_fifo_size; |
| 50 | int rx_fifo_size; |
| 51 | }; |
| 52 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 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 */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 71 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 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 */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 84 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 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 | |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 124 | 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] | 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 | |
| 135 | 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] | 136 | u32 value) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 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 | |
| 149 | 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] | 150 | u32 clr, u32 set) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 151 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 152 | u32 mask = clr | set; |
| 153 | u32 data; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 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 | |
| 171 | static 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 | |
| 182 | static struct { |
| 183 | unsigned short div; |
| 184 | unsigned short scr; |
| 185 | } const sh_msiof_spi_clk_table[] = { |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 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 }, |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | 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] | 200 | unsigned long parent_rate, u32 spi_hz) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 201 | { |
| 202 | unsigned long div = 1024; |
| 203 | size_t k; |
| 204 | |
| 205 | if (!WARN_ON(!spi_hz || !parent_rate)) |
Takashi Yoshii | e4d313f | 2013-12-02 03:19:13 +0900 | [diff] [blame] | 206 | div = DIV_ROUND_UP(parent_rate, spi_hz); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 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); |
| 218 | sh_msiof_write(p, RSCR, sh_msiof_spi_clk_table[k].scr); |
| 219 | } |
| 220 | |
| 221 | 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] | 222 | u32 cpol, u32 cpha, |
Takashi Yoshii | 50a7799 | 2013-12-02 03:19:15 +0900 | [diff] [blame] | 223 | u32 tx_hi_z, u32 lsb_first, u32 cs_high) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 224 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 225 | u32 tmp; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 226 | int edge; |
| 227 | |
| 228 | /* |
Markus Pietrek | e8708ef | 2010-02-02 11:29:15 +0900 | [diff] [blame] | 229 | * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG |
| 230 | * 0 0 10 10 1 1 |
| 231 | * 0 1 10 10 0 0 |
| 232 | * 1 0 11 11 0 0 |
| 233 | * 1 1 11 11 1 1 |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 234 | */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 235 | sh_msiof_write(p, FCTR, 0); |
Takashi Yoshii | 50a7799 | 2013-12-02 03:19:15 +0900 | [diff] [blame] | 236 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 237 | tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP; |
| 238 | tmp |= !cs_high << MDR1_SYNCAC_SHIFT; |
| 239 | tmp |= lsb_first << MDR1_BITLSB_SHIFT; |
| 240 | sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON); |
| 241 | sh_msiof_write(p, RMDR1, tmp); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 242 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 243 | tmp = 0; |
| 244 | tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT; |
| 245 | tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 246 | |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 247 | edge = cpol ^ !cpha; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 248 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 249 | tmp |= edge << CTR_TEDG_SHIFT; |
| 250 | tmp |= edge << CTR_REDG_SHIFT; |
| 251 | tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 252 | sh_msiof_write(p, CTR, tmp); |
| 253 | } |
| 254 | |
| 255 | static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p, |
| 256 | const void *tx_buf, void *rx_buf, |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 257 | u32 bits, u32 words) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 258 | { |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 259 | u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 260 | |
| 261 | if (tx_buf) |
| 262 | sh_msiof_write(p, TMDR2, dr2); |
| 263 | else |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 264 | sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 265 | |
| 266 | if (rx_buf) |
| 267 | sh_msiof_write(p, RMDR2, dr2); |
| 268 | |
| 269 | sh_msiof_write(p, IER, STR_TEOF | STR_REOF); |
| 270 | } |
| 271 | |
| 272 | static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p) |
| 273 | { |
| 274 | sh_msiof_write(p, STR, sh_msiof_read(p, STR)); |
| 275 | } |
| 276 | |
| 277 | static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p, |
| 278 | const void *tx_buf, int words, int fs) |
| 279 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 280 | const u8 *buf_8 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 281 | int k; |
| 282 | |
| 283 | for (k = 0; k < words; k++) |
| 284 | sh_msiof_write(p, TFDR, buf_8[k] << fs); |
| 285 | } |
| 286 | |
| 287 | static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p, |
| 288 | const void *tx_buf, int words, int fs) |
| 289 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 290 | const u16 *buf_16 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 291 | int k; |
| 292 | |
| 293 | for (k = 0; k < words; k++) |
| 294 | sh_msiof_write(p, TFDR, buf_16[k] << fs); |
| 295 | } |
| 296 | |
| 297 | static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p, |
| 298 | const void *tx_buf, int words, int fs) |
| 299 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 300 | const u16 *buf_16 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 301 | int k; |
| 302 | |
| 303 | for (k = 0; k < words; k++) |
| 304 | sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs); |
| 305 | } |
| 306 | |
| 307 | static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p, |
| 308 | const void *tx_buf, int words, int fs) |
| 309 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 310 | const u32 *buf_32 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 311 | int k; |
| 312 | |
| 313 | for (k = 0; k < words; k++) |
| 314 | sh_msiof_write(p, TFDR, buf_32[k] << fs); |
| 315 | } |
| 316 | |
| 317 | static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p, |
| 318 | const void *tx_buf, int words, int fs) |
| 319 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 320 | const u32 *buf_32 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 321 | int k; |
| 322 | |
| 323 | for (k = 0; k < words; k++) |
| 324 | sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs); |
| 325 | } |
| 326 | |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 327 | static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p, |
| 328 | const void *tx_buf, int words, int fs) |
| 329 | { |
| 330 | const u32 *buf_32 = tx_buf; |
| 331 | int k; |
| 332 | |
| 333 | for (k = 0; k < words; k++) |
| 334 | sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs)); |
| 335 | } |
| 336 | |
| 337 | static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p, |
| 338 | const void *tx_buf, int words, int fs) |
| 339 | { |
| 340 | const u32 *buf_32 = tx_buf; |
| 341 | int k; |
| 342 | |
| 343 | for (k = 0; k < words; k++) |
| 344 | sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs)); |
| 345 | } |
| 346 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 347 | static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p, |
| 348 | void *rx_buf, int words, int fs) |
| 349 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 350 | u8 *buf_8 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 351 | int k; |
| 352 | |
| 353 | for (k = 0; k < words; k++) |
| 354 | buf_8[k] = sh_msiof_read(p, RFDR) >> fs; |
| 355 | } |
| 356 | |
| 357 | static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p, |
| 358 | void *rx_buf, int words, int fs) |
| 359 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 360 | u16 *buf_16 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 361 | int k; |
| 362 | |
| 363 | for (k = 0; k < words; k++) |
| 364 | buf_16[k] = sh_msiof_read(p, RFDR) >> fs; |
| 365 | } |
| 366 | |
| 367 | static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p, |
| 368 | void *rx_buf, int words, int fs) |
| 369 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 370 | u16 *buf_16 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 371 | int k; |
| 372 | |
| 373 | for (k = 0; k < words; k++) |
| 374 | put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]); |
| 375 | } |
| 376 | |
| 377 | static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p, |
| 378 | void *rx_buf, int words, int fs) |
| 379 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 380 | u32 *buf_32 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 381 | int k; |
| 382 | |
| 383 | for (k = 0; k < words; k++) |
| 384 | buf_32[k] = sh_msiof_read(p, RFDR) >> fs; |
| 385 | } |
| 386 | |
| 387 | static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p, |
| 388 | void *rx_buf, int words, int fs) |
| 389 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 390 | u32 *buf_32 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 391 | int k; |
| 392 | |
| 393 | for (k = 0; k < words; k++) |
| 394 | put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]); |
| 395 | } |
| 396 | |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 397 | static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p, |
| 398 | void *rx_buf, int words, int fs) |
| 399 | { |
| 400 | u32 *buf_32 = rx_buf; |
| 401 | int k; |
| 402 | |
| 403 | for (k = 0; k < words; k++) |
| 404 | buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs); |
| 405 | } |
| 406 | |
| 407 | static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p, |
| 408 | void *rx_buf, int words, int fs) |
| 409 | { |
| 410 | u32 *buf_32 = rx_buf; |
| 411 | int k; |
| 412 | |
| 413 | for (k = 0; k < words; k++) |
| 414 | put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]); |
| 415 | } |
| 416 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 417 | static int sh_msiof_spi_bits(struct spi_device *spi, struct spi_transfer *t) |
| 418 | { |
| 419 | int bits; |
| 420 | |
| 421 | bits = t ? t->bits_per_word : 0; |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 422 | if (!bits) |
| 423 | bits = spi->bits_per_word; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 424 | return bits; |
| 425 | } |
| 426 | |
Geert Uytterhoeven | 6a85fc5 | 2014-02-20 15:43:02 +0100 | [diff] [blame] | 427 | static u32 sh_msiof_spi_hz(struct spi_device *spi, struct spi_transfer *t) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 428 | { |
Geert Uytterhoeven | 6a85fc5 | 2014-02-20 15:43:02 +0100 | [diff] [blame] | 429 | u32 hz; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 430 | |
| 431 | hz = t ? t->speed_hz : 0; |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 432 | if (!hz) |
| 433 | hz = spi->max_speed_hz; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 434 | return hz; |
| 435 | } |
| 436 | |
| 437 | static int sh_msiof_spi_setup_transfer(struct spi_device *spi, |
| 438 | struct spi_transfer *t) |
| 439 | { |
| 440 | int bits; |
| 441 | |
| 442 | /* noting to check hz values against since parent clock is disabled */ |
| 443 | |
| 444 | bits = sh_msiof_spi_bits(spi, t); |
| 445 | if (bits < 8) |
| 446 | return -EINVAL; |
| 447 | if (bits > 32) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | return spi_bitbang_setup_transfer(spi, t); |
| 451 | } |
| 452 | |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 453 | static int sh_msiof_spi_setup(struct spi_device *spi) |
| 454 | { |
| 455 | struct device_node *np = spi->master->dev.of_node; |
| 456 | |
| 457 | if (!np) { |
| 458 | /* |
| 459 | * Use spi->controller_data for CS (same strategy as spi_gpio), |
| 460 | * if any. otherwise let HW control CS |
| 461 | */ |
| 462 | spi->cs_gpio = (uintptr_t)spi->controller_data; |
| 463 | } |
| 464 | |
| 465 | return spi_bitbang_setup(spi); |
| 466 | } |
| 467 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 468 | static void sh_msiof_spi_chipselect(struct spi_device *spi, int is_on) |
| 469 | { |
| 470 | struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master); |
| 471 | int value; |
| 472 | |
| 473 | /* chip select is active low unless SPI_CS_HIGH is set */ |
| 474 | if (spi->mode & SPI_CS_HIGH) |
| 475 | value = (is_on == BITBANG_CS_ACTIVE) ? 1 : 0; |
| 476 | else |
| 477 | value = (is_on == BITBANG_CS_ACTIVE) ? 0 : 1; |
| 478 | |
| 479 | if (is_on == BITBANG_CS_ACTIVE) { |
| 480 | if (!test_and_set_bit(0, &p->flags)) { |
| 481 | pm_runtime_get_sync(&p->pdev->dev); |
| 482 | clk_enable(p->clk); |
| 483 | } |
| 484 | |
| 485 | /* Configure pins before asserting CS */ |
| 486 | sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL), |
| 487 | !!(spi->mode & SPI_CPHA), |
| 488 | !!(spi->mode & SPI_3WIRE), |
Takashi Yoshii | 50a7799 | 2013-12-02 03:19:15 +0900 | [diff] [blame] | 489 | !!(spi->mode & SPI_LSB_FIRST), |
| 490 | !!(spi->mode & SPI_CS_HIGH)); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 493 | if (spi->cs_gpio >= 0) |
| 494 | gpio_set_value(spi->cs_gpio, value); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 495 | |
| 496 | if (is_on == BITBANG_CS_INACTIVE) { |
| 497 | if (test_and_clear_bit(0, &p->flags)) { |
| 498 | clk_disable(p->clk); |
| 499 | pm_runtime_put(&p->pdev->dev); |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p, |
| 505 | void (*tx_fifo)(struct sh_msiof_spi_priv *, |
| 506 | const void *, int, int), |
| 507 | void (*rx_fifo)(struct sh_msiof_spi_priv *, |
| 508 | void *, int, int), |
| 509 | const void *tx_buf, void *rx_buf, |
| 510 | int words, int bits) |
| 511 | { |
| 512 | int fifo_shift; |
| 513 | int ret; |
| 514 | |
| 515 | /* limit maximum word transfer to rx/tx fifo size */ |
| 516 | if (tx_buf) |
| 517 | words = min_t(int, words, p->tx_fifo_size); |
| 518 | if (rx_buf) |
| 519 | words = min_t(int, words, p->rx_fifo_size); |
| 520 | |
| 521 | /* the fifo contents need shifting */ |
| 522 | fifo_shift = 32 - bits; |
| 523 | |
| 524 | /* setup msiof transfer mode registers */ |
| 525 | sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words); |
| 526 | |
| 527 | /* write tx fifo */ |
| 528 | if (tx_buf) |
| 529 | tx_fifo(p, tx_buf, words, fifo_shift); |
| 530 | |
| 531 | /* setup clock and rx/tx signals */ |
| 532 | ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE); |
| 533 | if (rx_buf) |
| 534 | ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_RXE); |
| 535 | ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TXE); |
| 536 | |
| 537 | /* start by setting frame bit */ |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 538 | reinit_completion(&p->done); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 539 | ret = ret ? ret : sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE); |
| 540 | if (ret) { |
| 541 | dev_err(&p->pdev->dev, "failed to start hardware\n"); |
| 542 | goto err; |
| 543 | } |
| 544 | |
| 545 | /* wait for tx fifo to be emptied / rx fifo to be filled */ |
| 546 | wait_for_completion(&p->done); |
| 547 | |
| 548 | /* read rx fifo */ |
| 549 | if (rx_buf) |
| 550 | rx_fifo(p, rx_buf, words, fifo_shift); |
| 551 | |
| 552 | /* clear status bits */ |
| 553 | sh_msiof_reset_str(p); |
| 554 | |
Geert Uytterhoeven | a669c11 | 2014-02-20 15:43:01 +0100 | [diff] [blame] | 555 | /* shut down frame, rx/tx and clock signals */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 556 | ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0); |
| 557 | ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TXE, 0); |
| 558 | if (rx_buf) |
| 559 | ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_RXE, 0); |
| 560 | ret = ret ? ret : sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0); |
| 561 | if (ret) { |
| 562 | dev_err(&p->pdev->dev, "failed to shut down hardware\n"); |
| 563 | goto err; |
| 564 | } |
| 565 | |
| 566 | return words; |
| 567 | |
| 568 | err: |
| 569 | sh_msiof_write(p, IER, 0); |
| 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | static int sh_msiof_spi_txrx(struct spi_device *spi, struct spi_transfer *t) |
| 574 | { |
| 575 | struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master); |
| 576 | void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int); |
| 577 | void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int); |
| 578 | int bits; |
| 579 | int bytes_per_word; |
| 580 | int bytes_done; |
| 581 | int words; |
| 582 | int n; |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 583 | bool swab; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 584 | |
| 585 | bits = sh_msiof_spi_bits(spi, t); |
| 586 | |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 587 | if (bits <= 8 && t->len > 15 && !(t->len & 3)) { |
| 588 | bits = 32; |
| 589 | swab = true; |
| 590 | } else { |
| 591 | swab = false; |
| 592 | } |
| 593 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 594 | /* setup bytes per word and fifo read/write functions */ |
| 595 | if (bits <= 8) { |
| 596 | bytes_per_word = 1; |
| 597 | tx_fifo = sh_msiof_spi_write_fifo_8; |
| 598 | rx_fifo = sh_msiof_spi_read_fifo_8; |
| 599 | } else if (bits <= 16) { |
| 600 | bytes_per_word = 2; |
| 601 | if ((unsigned long)t->tx_buf & 0x01) |
| 602 | tx_fifo = sh_msiof_spi_write_fifo_16u; |
| 603 | else |
| 604 | tx_fifo = sh_msiof_spi_write_fifo_16; |
| 605 | |
| 606 | if ((unsigned long)t->rx_buf & 0x01) |
| 607 | rx_fifo = sh_msiof_spi_read_fifo_16u; |
| 608 | else |
| 609 | rx_fifo = sh_msiof_spi_read_fifo_16; |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 610 | } else if (swab) { |
| 611 | bytes_per_word = 4; |
| 612 | if ((unsigned long)t->tx_buf & 0x03) |
| 613 | tx_fifo = sh_msiof_spi_write_fifo_s32u; |
| 614 | else |
| 615 | tx_fifo = sh_msiof_spi_write_fifo_s32; |
| 616 | |
| 617 | if ((unsigned long)t->rx_buf & 0x03) |
| 618 | rx_fifo = sh_msiof_spi_read_fifo_s32u; |
| 619 | else |
| 620 | rx_fifo = sh_msiof_spi_read_fifo_s32; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 621 | } else { |
| 622 | bytes_per_word = 4; |
| 623 | if ((unsigned long)t->tx_buf & 0x03) |
| 624 | tx_fifo = sh_msiof_spi_write_fifo_32u; |
| 625 | else |
| 626 | tx_fifo = sh_msiof_spi_write_fifo_32; |
| 627 | |
| 628 | if ((unsigned long)t->rx_buf & 0x03) |
| 629 | rx_fifo = sh_msiof_spi_read_fifo_32u; |
| 630 | else |
| 631 | rx_fifo = sh_msiof_spi_read_fifo_32; |
| 632 | } |
| 633 | |
| 634 | /* setup clocks (clock already enabled in chipselect()) */ |
| 635 | sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), |
| 636 | sh_msiof_spi_hz(spi, t)); |
| 637 | |
| 638 | /* transfer in fifo sized chunks */ |
| 639 | words = t->len / bytes_per_word; |
| 640 | bytes_done = 0; |
| 641 | |
| 642 | while (bytes_done < t->len) { |
Guennadi Liakhovetski | 8a6afb9 | 2011-01-21 16:56:47 +0100 | [diff] [blame] | 643 | void *rx_buf = t->rx_buf ? t->rx_buf + bytes_done : NULL; |
| 644 | 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] | 645 | n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo, |
Guennadi Liakhovetski | 8a6afb9 | 2011-01-21 16:56:47 +0100 | [diff] [blame] | 646 | tx_buf, |
| 647 | rx_buf, |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 648 | words, bits); |
| 649 | if (n < 0) |
| 650 | break; |
| 651 | |
| 652 | bytes_done += n * bytes_per_word; |
| 653 | words -= n; |
| 654 | } |
| 655 | |
| 656 | return bytes_done; |
| 657 | } |
| 658 | |
| 659 | static u32 sh_msiof_spi_txrx_word(struct spi_device *spi, unsigned nsecs, |
| 660 | u32 word, u8 bits) |
| 661 | { |
| 662 | BUG(); /* unused but needed by bitbang code */ |
| 663 | return 0; |
| 664 | } |
| 665 | |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 666 | static const struct sh_msiof_chipdata sh_data = { |
| 667 | .tx_fifo_size = 64, |
| 668 | .rx_fifo_size = 64, |
| 669 | }; |
| 670 | |
| 671 | static const struct of_device_id sh_msiof_match[] = { |
| 672 | { .compatible = "renesas,sh-msiof", .data = &sh_data }, |
| 673 | { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data }, |
| 674 | {}, |
| 675 | }; |
| 676 | MODULE_DEVICE_TABLE(of, sh_msiof_match); |
| 677 | |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 678 | #ifdef CONFIG_OF |
| 679 | static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev) |
| 680 | { |
| 681 | struct sh_msiof_spi_info *info; |
| 682 | struct device_node *np = dev->of_node; |
Geert Uytterhoeven | 32d3b2d | 2014-02-25 11:21:08 +0100 | [diff] [blame] | 683 | u32 num_cs = 1; |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 684 | |
| 685 | info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL); |
| 686 | if (!info) { |
| 687 | dev_err(dev, "failed to allocate setup data\n"); |
| 688 | return NULL; |
| 689 | } |
| 690 | |
| 691 | /* Parse the MSIOF properties */ |
| 692 | of_property_read_u32(np, "num-cs", &num_cs); |
| 693 | of_property_read_u32(np, "renesas,tx-fifo-size", |
| 694 | &info->tx_fifo_override); |
| 695 | of_property_read_u32(np, "renesas,rx-fifo-size", |
| 696 | &info->rx_fifo_override); |
| 697 | |
| 698 | info->num_chipselect = num_cs; |
| 699 | |
| 700 | return info; |
| 701 | } |
| 702 | #else |
| 703 | static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev) |
| 704 | { |
| 705 | return NULL; |
| 706 | } |
| 707 | #endif |
| 708 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 709 | static int sh_msiof_spi_probe(struct platform_device *pdev) |
| 710 | { |
| 711 | struct resource *r; |
| 712 | struct spi_master *master; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 713 | const struct of_device_id *of_id; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 714 | struct sh_msiof_spi_priv *p; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 715 | int i; |
| 716 | int ret; |
| 717 | |
| 718 | master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv)); |
| 719 | if (master == NULL) { |
| 720 | dev_err(&pdev->dev, "failed to allocate spi master\n"); |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 721 | return -ENOMEM; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | p = spi_master_get_devdata(master); |
| 725 | |
| 726 | platform_set_drvdata(pdev, p); |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 727 | |
| 728 | of_id = of_match_device(sh_msiof_match, &pdev->dev); |
| 729 | if (of_id) { |
| 730 | p->chipdata = of_id->data; |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 731 | p->info = sh_msiof_spi_parse_dt(&pdev->dev); |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 732 | } else { |
| 733 | p->chipdata = (const void *)pdev->id_entry->driver_data; |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 734 | p->info = dev_get_platdata(&pdev->dev); |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 735 | } |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 736 | |
| 737 | if (!p->info) { |
| 738 | dev_err(&pdev->dev, "failed to obtain device info\n"); |
| 739 | ret = -ENXIO; |
| 740 | goto err1; |
| 741 | } |
| 742 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 743 | init_completion(&p->done); |
| 744 | |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 745 | p->clk = devm_clk_get(&pdev->dev, NULL); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 746 | if (IS_ERR(p->clk)) { |
Bastian Hecht | 078b6ea | 2012-11-07 12:40:04 +0100 | [diff] [blame] | 747 | dev_err(&pdev->dev, "cannot get clock\n"); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 748 | ret = PTR_ERR(p->clk); |
| 749 | goto err1; |
| 750 | } |
| 751 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 752 | i = platform_get_irq(pdev, 0); |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 753 | if (i < 0) { |
| 754 | dev_err(&pdev->dev, "cannot get platform IRQ\n"); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 755 | ret = -ENOENT; |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 756 | goto err1; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 759 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 760 | p->mapbase = devm_ioremap_resource(&pdev->dev, r); |
| 761 | if (IS_ERR(p->mapbase)) { |
| 762 | ret = PTR_ERR(p->mapbase); |
| 763 | goto err1; |
| 764 | } |
| 765 | |
| 766 | ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0, |
| 767 | dev_name(&pdev->dev), p); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 768 | if (ret) { |
| 769 | dev_err(&pdev->dev, "unable to request irq\n"); |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 770 | goto err1; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 771 | } |
| 772 | |
Laurent Pinchart | 5c32d29 | 2013-11-28 02:39:43 +0100 | [diff] [blame] | 773 | ret = clk_prepare(p->clk); |
| 774 | if (ret < 0) { |
| 775 | dev_err(&pdev->dev, "unable to prepare clock\n"); |
| 776 | goto err1; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | p->pdev = pdev; |
| 780 | pm_runtime_enable(&pdev->dev); |
| 781 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 782 | /* Platform data may override FIFO sizes */ |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 783 | p->tx_fifo_size = p->chipdata->tx_fifo_size; |
| 784 | p->rx_fifo_size = p->chipdata->rx_fifo_size; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 785 | if (p->info->tx_fifo_override) |
| 786 | p->tx_fifo_size = p->info->tx_fifo_override; |
| 787 | if (p->info->rx_fifo_override) |
| 788 | p->rx_fifo_size = p->info->rx_fifo_override; |
| 789 | |
| 790 | /* init master and bitbang code */ |
| 791 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 792 | master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE; |
| 793 | master->flags = 0; |
| 794 | master->bus_num = pdev->id; |
Geert Uytterhoeven | f7c05e8 | 2014-02-20 15:43:00 +0100 | [diff] [blame] | 795 | master->dev.of_node = pdev->dev.of_node; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 796 | master->num_chipselect = p->info->num_chipselect; |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 797 | master->setup = sh_msiof_spi_setup; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 798 | master->cleanup = spi_bitbang_cleanup; |
| 799 | |
| 800 | p->bitbang.master = master; |
| 801 | p->bitbang.chipselect = sh_msiof_spi_chipselect; |
| 802 | p->bitbang.setup_transfer = sh_msiof_spi_setup_transfer; |
| 803 | p->bitbang.txrx_bufs = sh_msiof_spi_txrx; |
| 804 | p->bitbang.txrx_word[SPI_MODE_0] = sh_msiof_spi_txrx_word; |
| 805 | p->bitbang.txrx_word[SPI_MODE_1] = sh_msiof_spi_txrx_word; |
| 806 | p->bitbang.txrx_word[SPI_MODE_2] = sh_msiof_spi_txrx_word; |
| 807 | p->bitbang.txrx_word[SPI_MODE_3] = sh_msiof_spi_txrx_word; |
| 808 | |
| 809 | ret = spi_bitbang_start(&p->bitbang); |
| 810 | if (ret == 0) |
| 811 | return 0; |
| 812 | |
| 813 | pm_runtime_disable(&pdev->dev); |
Laurent Pinchart | 5c32d29 | 2013-11-28 02:39:43 +0100 | [diff] [blame] | 814 | clk_unprepare(p->clk); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 815 | err1: |
| 816 | spi_master_put(master); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 817 | return ret; |
| 818 | } |
| 819 | |
| 820 | static int sh_msiof_spi_remove(struct platform_device *pdev) |
| 821 | { |
| 822 | struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev); |
| 823 | int ret; |
| 824 | |
| 825 | ret = spi_bitbang_stop(&p->bitbang); |
| 826 | if (!ret) { |
| 827 | pm_runtime_disable(&pdev->dev); |
Laurent Pinchart | 5c32d29 | 2013-11-28 02:39:43 +0100 | [diff] [blame] | 828 | clk_unprepare(p->clk); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 829 | spi_master_put(p->bitbang.master); |
| 830 | } |
| 831 | return ret; |
| 832 | } |
| 833 | |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 834 | static struct platform_device_id spi_driver_ids[] = { |
| 835 | { "spi_sh_msiof", (kernel_ulong_t)&sh_data }, |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 836 | {}, |
| 837 | }; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 838 | MODULE_DEVICE_TABLE(platform, spi_driver_ids); |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 839 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 840 | static struct platform_driver sh_msiof_spi_drv = { |
| 841 | .probe = sh_msiof_spi_probe, |
| 842 | .remove = sh_msiof_spi_remove, |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame^] | 843 | .id_table = spi_driver_ids, |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 844 | .driver = { |
| 845 | .name = "spi_sh_msiof", |
| 846 | .owner = THIS_MODULE, |
Sachin Kamat | 691ee4e | 2013-03-14 15:31:51 +0530 | [diff] [blame] | 847 | .of_match_table = of_match_ptr(sh_msiof_match), |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 848 | }, |
| 849 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 850 | module_platform_driver(sh_msiof_spi_drv); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 851 | |
| 852 | MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver"); |
| 853 | MODULE_AUTHOR("Magnus Damm"); |
| 854 | MODULE_LICENSE("GPL v2"); |
| 855 | MODULE_ALIAS("platform:spi_sh_msiof"); |