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 |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 5 | * Copyright (C) 2014 Glider bvba |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | */ |
| 12 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 13 | #include <linux/bitmap.h> |
| 14 | #include <linux/clk.h> |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 15 | #include <linux/completion.h> |
| 16 | #include <linux/delay.h> |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/dmaengine.h> |
Magnus Damm | ac48eee | 2010-01-20 13:49:45 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 20 | #include <linux/gpio.h> |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/io.h> |
| 23 | #include <linux/kernel.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 25 | #include <linux/of.h> |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 26 | #include <linux/of_device.h> |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
| 28 | #include <linux/pm_runtime.h> |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 29 | #include <linux/sh_dma.h> |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 30 | |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 31 | #include <linux/spi/sh_msiof.h> |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 32 | #include <linux/spi/spi.h> |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 33 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 34 | #include <asm/unaligned.h> |
| 35 | |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 36 | |
| 37 | struct sh_msiof_chipdata { |
| 38 | u16 tx_fifo_size; |
| 39 | u16 rx_fifo_size; |
Geert Uytterhoeven | beb74bb | 2014-02-25 11:21:10 +0100 | [diff] [blame] | 40 | u16 master_flags; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 43 | struct sh_msiof_spi_priv { |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 44 | struct spi_master *master; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 45 | void __iomem *mapbase; |
| 46 | struct clk *clk; |
| 47 | struct platform_device *pdev; |
| 48 | struct sh_msiof_spi_info *info; |
| 49 | struct completion done; |
Koji Matsuoka | fe78d0b | 2015-06-15 02:25:05 +0900 | [diff] [blame] | 50 | unsigned int tx_fifo_size; |
| 51 | unsigned int rx_fifo_size; |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 52 | void *tx_dma_page; |
| 53 | void *rx_dma_page; |
| 54 | dma_addr_t tx_dma_addr; |
| 55 | dma_addr_t rx_dma_addr; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 58 | #define TMDR1 0x00 /* Transmit Mode Register 1 */ |
| 59 | #define TMDR2 0x04 /* Transmit Mode Register 2 */ |
| 60 | #define TMDR3 0x08 /* Transmit Mode Register 3 */ |
| 61 | #define RMDR1 0x10 /* Receive Mode Register 1 */ |
| 62 | #define RMDR2 0x14 /* Receive Mode Register 2 */ |
| 63 | #define RMDR3 0x18 /* Receive Mode Register 3 */ |
| 64 | #define TSCR 0x20 /* Transmit Clock Select Register */ |
| 65 | #define RSCR 0x22 /* Receive Clock Select Register (SH, A1, APE6) */ |
| 66 | #define CTR 0x28 /* Control Register */ |
| 67 | #define FCTR 0x30 /* FIFO Control Register */ |
| 68 | #define STR 0x40 /* Status Register */ |
| 69 | #define IER 0x44 /* Interrupt Enable Register */ |
| 70 | #define TDR1 0x48 /* Transmit Control Data Register 1 (SH, A1) */ |
| 71 | #define TDR2 0x4c /* Transmit Control Data Register 2 (SH, A1) */ |
| 72 | #define TFDR 0x50 /* Transmit FIFO Data Register */ |
| 73 | #define RDR1 0x58 /* Receive Control Data Register 1 (SH, A1) */ |
| 74 | #define RDR2 0x5c /* Receive Control Data Register 2 (SH, A1) */ |
| 75 | #define RFDR 0x60 /* Receive FIFO Data Register */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 76 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 77 | /* TMDR1 and RMDR1 */ |
| 78 | #define MDR1_TRMD 0x80000000 /* Transfer Mode (1 = Master mode) */ |
| 79 | #define MDR1_SYNCMD_MASK 0x30000000 /* SYNC Mode */ |
| 80 | #define MDR1_SYNCMD_SPI 0x20000000 /* Level mode/SPI */ |
| 81 | #define MDR1_SYNCMD_LR 0x30000000 /* L/R mode */ |
| 82 | #define MDR1_SYNCAC_SHIFT 25 /* Sync Polarity (1 = Active-low) */ |
| 83 | #define MDR1_BITLSB_SHIFT 24 /* MSB/LSB First (1 = LSB first) */ |
Yoshihiro Shimoda | 3110628 | 2014-12-19 17:15:53 +0900 | [diff] [blame] | 84 | #define MDR1_DTDL_SHIFT 20 /* Data Pin Bit Delay for MSIOF_SYNC */ |
| 85 | #define MDR1_SYNCDL_SHIFT 16 /* Frame Sync Signal Timing Delay */ |
Yoshihiro Shimoda | 6d40530 | 2015-01-06 19:01:26 +0900 | [diff] [blame] | 86 | #define MDR1_FLD_MASK 0x0000000c /* Frame Sync Signal Interval (0-3) */ |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 87 | #define MDR1_FLD_SHIFT 2 |
| 88 | #define MDR1_XXSTP 0x00000001 /* Transmission/Reception Stop on FIFO */ |
| 89 | /* TMDR1 */ |
| 90 | #define TMDR1_PCON 0x40000000 /* Transfer Signal Connection */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 91 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 92 | /* TMDR2 and RMDR2 */ |
| 93 | #define MDR2_BITLEN1(i) (((i) - 1) << 24) /* Data Size (8-32 bits) */ |
| 94 | #define MDR2_WDLEN1(i) (((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */ |
| 95 | #define MDR2_GRPMASK1 0x00000001 /* Group Output Mask 1 (SH, A1) */ |
| 96 | |
| 97 | /* TSCR and RSCR */ |
| 98 | #define SCR_BRPS_MASK 0x1f00 /* Prescaler Setting (1-32) */ |
| 99 | #define SCR_BRPS(i) (((i) - 1) << 8) |
| 100 | #define SCR_BRDV_MASK 0x0007 /* Baud Rate Generator's Division Ratio */ |
| 101 | #define SCR_BRDV_DIV_2 0x0000 |
| 102 | #define SCR_BRDV_DIV_4 0x0001 |
| 103 | #define SCR_BRDV_DIV_8 0x0002 |
| 104 | #define SCR_BRDV_DIV_16 0x0003 |
| 105 | #define SCR_BRDV_DIV_32 0x0004 |
| 106 | #define SCR_BRDV_DIV_1 0x0007 |
| 107 | |
| 108 | /* CTR */ |
| 109 | #define CTR_TSCKIZ_MASK 0xc0000000 /* Transmit Clock I/O Polarity Select */ |
| 110 | #define CTR_TSCKIZ_SCK 0x80000000 /* Disable SCK when TX disabled */ |
| 111 | #define CTR_TSCKIZ_POL_SHIFT 30 /* Transmit Clock Polarity */ |
| 112 | #define CTR_RSCKIZ_MASK 0x30000000 /* Receive Clock Polarity Select */ |
| 113 | #define CTR_RSCKIZ_SCK 0x20000000 /* Must match CTR_TSCKIZ_SCK */ |
| 114 | #define CTR_RSCKIZ_POL_SHIFT 28 /* Receive Clock Polarity */ |
| 115 | #define CTR_TEDG_SHIFT 27 /* Transmit Timing (1 = falling edge) */ |
| 116 | #define CTR_REDG_SHIFT 26 /* Receive Timing (1 = falling edge) */ |
| 117 | #define CTR_TXDIZ_MASK 0x00c00000 /* Pin Output When TX is Disabled */ |
| 118 | #define CTR_TXDIZ_LOW 0x00000000 /* 0 */ |
| 119 | #define CTR_TXDIZ_HIGH 0x00400000 /* 1 */ |
| 120 | #define CTR_TXDIZ_HIZ 0x00800000 /* High-impedance */ |
| 121 | #define CTR_TSCKE 0x00008000 /* Transmit Serial Clock Output Enable */ |
| 122 | #define CTR_TFSE 0x00004000 /* Transmit Frame Sync Signal Output Enable */ |
| 123 | #define CTR_TXE 0x00000200 /* Transmit Enable */ |
| 124 | #define CTR_RXE 0x00000100 /* Receive Enable */ |
| 125 | |
Geert Uytterhoeven | 2e2b368 | 2014-06-20 12:16:16 +0200 | [diff] [blame] | 126 | /* FCTR */ |
| 127 | #define FCTR_TFWM_MASK 0xe0000000 /* Transmit FIFO Watermark */ |
| 128 | #define FCTR_TFWM_64 0x00000000 /* Transfer Request when 64 empty stages */ |
| 129 | #define FCTR_TFWM_32 0x20000000 /* Transfer Request when 32 empty stages */ |
| 130 | #define FCTR_TFWM_24 0x40000000 /* Transfer Request when 24 empty stages */ |
| 131 | #define FCTR_TFWM_16 0x60000000 /* Transfer Request when 16 empty stages */ |
| 132 | #define FCTR_TFWM_12 0x80000000 /* Transfer Request when 12 empty stages */ |
| 133 | #define FCTR_TFWM_8 0xa0000000 /* Transfer Request when 8 empty stages */ |
| 134 | #define FCTR_TFWM_4 0xc0000000 /* Transfer Request when 4 empty stages */ |
| 135 | #define FCTR_TFWM_1 0xe0000000 /* Transfer Request when 1 empty stage */ |
| 136 | #define FCTR_TFUA_MASK 0x07f00000 /* Transmit FIFO Usable Area */ |
| 137 | #define FCTR_TFUA_SHIFT 20 |
| 138 | #define FCTR_TFUA(i) ((i) << FCTR_TFUA_SHIFT) |
| 139 | #define FCTR_RFWM_MASK 0x0000e000 /* Receive FIFO Watermark */ |
| 140 | #define FCTR_RFWM_1 0x00000000 /* Transfer Request when 1 valid stages */ |
| 141 | #define FCTR_RFWM_4 0x00002000 /* Transfer Request when 4 valid stages */ |
| 142 | #define FCTR_RFWM_8 0x00004000 /* Transfer Request when 8 valid stages */ |
| 143 | #define FCTR_RFWM_16 0x00006000 /* Transfer Request when 16 valid stages */ |
| 144 | #define FCTR_RFWM_32 0x00008000 /* Transfer Request when 32 valid stages */ |
| 145 | #define FCTR_RFWM_64 0x0000a000 /* Transfer Request when 64 valid stages */ |
| 146 | #define FCTR_RFWM_128 0x0000c000 /* Transfer Request when 128 valid stages */ |
| 147 | #define FCTR_RFWM_256 0x0000e000 /* Transfer Request when 256 valid stages */ |
| 148 | #define FCTR_RFUA_MASK 0x00001ff0 /* Receive FIFO Usable Area (0x40 = full) */ |
| 149 | #define FCTR_RFUA_SHIFT 4 |
| 150 | #define FCTR_RFUA(i) ((i) << FCTR_RFUA_SHIFT) |
| 151 | |
| 152 | /* STR */ |
| 153 | #define STR_TFEMP 0x20000000 /* Transmit FIFO Empty */ |
| 154 | #define STR_TDREQ 0x10000000 /* Transmit Data Transfer Request */ |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 155 | #define STR_TEOF 0x00800000 /* Frame Transmission End */ |
Geert Uytterhoeven | 2e2b368 | 2014-06-20 12:16:16 +0200 | [diff] [blame] | 156 | #define STR_TFSERR 0x00200000 /* Transmit Frame Synchronization Error */ |
| 157 | #define STR_TFOVF 0x00100000 /* Transmit FIFO Overflow */ |
| 158 | #define STR_TFUDF 0x00080000 /* Transmit FIFO Underflow */ |
| 159 | #define STR_RFFUL 0x00002000 /* Receive FIFO Full */ |
| 160 | #define STR_RDREQ 0x00001000 /* Receive Data Transfer Request */ |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 161 | #define STR_REOF 0x00000080 /* Frame Reception End */ |
Geert Uytterhoeven | 2e2b368 | 2014-06-20 12:16:16 +0200 | [diff] [blame] | 162 | #define STR_RFSERR 0x00000020 /* Receive Frame Synchronization Error */ |
| 163 | #define STR_RFUDF 0x00000010 /* Receive FIFO Underflow */ |
| 164 | #define STR_RFOVF 0x00000008 /* Receive FIFO Overflow */ |
| 165 | |
| 166 | /* IER */ |
| 167 | #define IER_TDMAE 0x80000000 /* Transmit Data DMA Transfer Req. Enable */ |
| 168 | #define IER_TFEMPE 0x20000000 /* Transmit FIFO Empty Enable */ |
| 169 | #define IER_TDREQE 0x10000000 /* Transmit Data Transfer Request Enable */ |
| 170 | #define IER_TEOFE 0x00800000 /* Frame Transmission End Enable */ |
| 171 | #define IER_TFSERRE 0x00200000 /* Transmit Frame Sync Error Enable */ |
| 172 | #define IER_TFOVFE 0x00100000 /* Transmit FIFO Overflow Enable */ |
| 173 | #define IER_TFUDFE 0x00080000 /* Transmit FIFO Underflow Enable */ |
| 174 | #define IER_RDMAE 0x00008000 /* Receive Data DMA Transfer Req. Enable */ |
| 175 | #define IER_RFFULE 0x00002000 /* Receive FIFO Full Enable */ |
| 176 | #define IER_RDREQE 0x00001000 /* Receive Data Transfer Request Enable */ |
| 177 | #define IER_REOFE 0x00000080 /* Frame Reception End Enable */ |
| 178 | #define IER_RFSERRE 0x00000020 /* Receive Frame Sync Error Enable */ |
| 179 | #define IER_RFUDFE 0x00000010 /* Receive FIFO Underflow Enable */ |
| 180 | #define IER_RFOVFE 0x00000008 /* Receive FIFO Overflow Enable */ |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 181 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 182 | |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | switch (reg_offs) { |
| 186 | case TSCR: |
| 187 | case RSCR: |
| 188 | return ioread16(p->mapbase + reg_offs); |
| 189 | default: |
| 190 | return ioread32(p->mapbase + reg_offs); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | 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] | 195 | u32 value) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 196 | { |
| 197 | switch (reg_offs) { |
| 198 | case TSCR: |
| 199 | case RSCR: |
| 200 | iowrite16(value, p->mapbase + reg_offs); |
| 201 | break; |
| 202 | default: |
| 203 | iowrite32(value, p->mapbase + reg_offs); |
| 204 | break; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | 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] | 209 | u32 clr, u32 set) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 210 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 211 | u32 mask = clr | set; |
| 212 | u32 data; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 213 | int k; |
| 214 | |
| 215 | data = sh_msiof_read(p, CTR); |
| 216 | data &= ~clr; |
| 217 | data |= set; |
| 218 | sh_msiof_write(p, CTR, data); |
| 219 | |
| 220 | for (k = 100; k > 0; k--) { |
| 221 | if ((sh_msiof_read(p, CTR) & mask) == set) |
| 222 | break; |
| 223 | |
| 224 | udelay(10); |
| 225 | } |
| 226 | |
| 227 | return k > 0 ? 0 : -ETIMEDOUT; |
| 228 | } |
| 229 | |
| 230 | static irqreturn_t sh_msiof_spi_irq(int irq, void *data) |
| 231 | { |
| 232 | struct sh_msiof_spi_priv *p = data; |
| 233 | |
| 234 | /* just disable the interrupt and wake up */ |
| 235 | sh_msiof_write(p, IER, 0); |
| 236 | complete(&p->done); |
| 237 | |
| 238 | return IRQ_HANDLED; |
| 239 | } |
| 240 | |
| 241 | static struct { |
| 242 | unsigned short div; |
Nobuhiro Iwamatsu | 65d5665 | 2015-01-30 15:11:54 +0900 | [diff] [blame] | 243 | unsigned short brdv; |
| 244 | } const sh_msiof_spi_div_table[] = { |
| 245 | { 1, SCR_BRDV_DIV_1 }, |
| 246 | { 2, SCR_BRDV_DIV_2 }, |
| 247 | { 4, SCR_BRDV_DIV_4 }, |
| 248 | { 8, SCR_BRDV_DIV_8 }, |
| 249 | { 16, SCR_BRDV_DIV_16 }, |
| 250 | { 32, SCR_BRDV_DIV_32 }, |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | 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] | 254 | unsigned long parent_rate, u32 spi_hz) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 255 | { |
| 256 | unsigned long div = 1024; |
Nobuhiro Iwamatsu | 65d5665 | 2015-01-30 15:11:54 +0900 | [diff] [blame] | 257 | u32 brps, scr; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 258 | size_t k; |
| 259 | |
| 260 | if (!WARN_ON(!spi_hz || !parent_rate)) |
Takashi Yoshii | e4d313f | 2013-12-02 03:19:13 +0900 | [diff] [blame] | 261 | div = DIV_ROUND_UP(parent_rate, spi_hz); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 262 | |
Nobuhiro Iwamatsu | 65d5665 | 2015-01-30 15:11:54 +0900 | [diff] [blame] | 263 | for (k = 0; k < ARRAY_SIZE(sh_msiof_spi_div_table); k++) { |
| 264 | brps = DIV_ROUND_UP(div, sh_msiof_spi_div_table[k].div); |
| 265 | if (brps <= 32) /* max of brdv is 32 */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 266 | break; |
| 267 | } |
| 268 | |
Nobuhiro Iwamatsu | 65d5665 | 2015-01-30 15:11:54 +0900 | [diff] [blame] | 269 | k = min_t(int, k, ARRAY_SIZE(sh_msiof_spi_div_table) - 1); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 270 | |
Nobuhiro Iwamatsu | 65d5665 | 2015-01-30 15:11:54 +0900 | [diff] [blame] | 271 | scr = sh_msiof_spi_div_table[k].brdv | SCR_BRPS(brps); |
| 272 | sh_msiof_write(p, TSCR, scr); |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 273 | if (!(p->master->flags & SPI_MASTER_MUST_TX)) |
Nobuhiro Iwamatsu | 65d5665 | 2015-01-30 15:11:54 +0900 | [diff] [blame] | 274 | sh_msiof_write(p, RSCR, scr); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Yoshihiro Shimoda | 3110628 | 2014-12-19 17:15:53 +0900 | [diff] [blame] | 277 | static u32 sh_msiof_get_delay_bit(u32 dtdl_or_syncdl) |
| 278 | { |
| 279 | /* |
| 280 | * DTDL/SYNCDL bit : p->info->dtdl or p->info->syncdl |
| 281 | * b'000 : 0 |
| 282 | * b'001 : 100 |
| 283 | * b'010 : 200 |
| 284 | * b'011 (SYNCDL only) : 300 |
| 285 | * b'101 : 50 |
| 286 | * b'110 : 150 |
| 287 | */ |
| 288 | if (dtdl_or_syncdl % 100) |
| 289 | return dtdl_or_syncdl / 100 + 5; |
| 290 | else |
| 291 | return dtdl_or_syncdl / 100; |
| 292 | } |
| 293 | |
| 294 | static u32 sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv *p) |
| 295 | { |
| 296 | u32 val; |
| 297 | |
| 298 | if (!p->info) |
| 299 | return 0; |
| 300 | |
| 301 | /* check if DTDL and SYNCDL is allowed value */ |
| 302 | if (p->info->dtdl > 200 || p->info->syncdl > 300) { |
| 303 | dev_warn(&p->pdev->dev, "DTDL or SYNCDL is too large\n"); |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | /* check if the sum of DTDL and SYNCDL becomes an integer value */ |
| 308 | if ((p->info->dtdl + p->info->syncdl) % 100) { |
| 309 | dev_warn(&p->pdev->dev, "the sum of DTDL/SYNCDL is not good\n"); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | val = sh_msiof_get_delay_bit(p->info->dtdl) << MDR1_DTDL_SHIFT; |
| 314 | val |= sh_msiof_get_delay_bit(p->info->syncdl) << MDR1_SYNCDL_SHIFT; |
| 315 | |
| 316 | return val; |
| 317 | } |
| 318 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 319 | 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] | 320 | u32 cpol, u32 cpha, |
Takashi Yoshii | 50a7799 | 2013-12-02 03:19:15 +0900 | [diff] [blame] | 321 | u32 tx_hi_z, u32 lsb_first, u32 cs_high) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 322 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 323 | u32 tmp; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 324 | int edge; |
| 325 | |
| 326 | /* |
Markus Pietrek | e8708ef | 2010-02-02 11:29:15 +0900 | [diff] [blame] | 327 | * CPOL CPHA TSCKIZ RSCKIZ TEDG REDG |
| 328 | * 0 0 10 10 1 1 |
| 329 | * 0 1 10 10 0 0 |
| 330 | * 1 0 11 11 0 0 |
| 331 | * 1 1 11 11 1 1 |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 332 | */ |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 333 | tmp = MDR1_SYNCMD_SPI | 1 << MDR1_FLD_SHIFT | MDR1_XXSTP; |
| 334 | tmp |= !cs_high << MDR1_SYNCAC_SHIFT; |
| 335 | tmp |= lsb_first << MDR1_BITLSB_SHIFT; |
Yoshihiro Shimoda | 3110628 | 2014-12-19 17:15:53 +0900 | [diff] [blame] | 336 | tmp |= sh_msiof_spi_get_dtdl_and_syncdl(p); |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 337 | sh_msiof_write(p, TMDR1, tmp | MDR1_TRMD | TMDR1_PCON); |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 338 | if (p->master->flags & SPI_MASTER_MUST_TX) { |
Geert Uytterhoeven | beb74bb | 2014-02-25 11:21:10 +0100 | [diff] [blame] | 339 | /* These bits are reserved if RX needs TX */ |
| 340 | tmp &= ~0x0000ffff; |
| 341 | } |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 342 | sh_msiof_write(p, RMDR1, tmp); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 343 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 344 | tmp = 0; |
| 345 | tmp |= CTR_TSCKIZ_SCK | cpol << CTR_TSCKIZ_POL_SHIFT; |
| 346 | tmp |= CTR_RSCKIZ_SCK | cpol << CTR_RSCKIZ_POL_SHIFT; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 347 | |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 348 | edge = cpol ^ !cpha; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 349 | |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 350 | tmp |= edge << CTR_TEDG_SHIFT; |
| 351 | tmp |= edge << CTR_REDG_SHIFT; |
| 352 | tmp |= tx_hi_z ? CTR_TXDIZ_HIZ : CTR_TXDIZ_LOW; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 353 | sh_msiof_write(p, CTR, tmp); |
| 354 | } |
| 355 | |
| 356 | static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p, |
| 357 | const void *tx_buf, void *rx_buf, |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 358 | u32 bits, u32 words) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 359 | { |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 360 | u32 dr2 = MDR2_BITLEN1(bits) | MDR2_WDLEN1(words); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 361 | |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 362 | if (tx_buf || (p->master->flags & SPI_MASTER_MUST_TX)) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 363 | sh_msiof_write(p, TMDR2, dr2); |
| 364 | else |
Geert Uytterhoeven | 01cfef5 | 2014-02-20 15:43:03 +0100 | [diff] [blame] | 365 | sh_msiof_write(p, TMDR2, dr2 | MDR2_GRPMASK1); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 366 | |
| 367 | if (rx_buf) |
| 368 | sh_msiof_write(p, RMDR2, dr2); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p) |
| 372 | { |
| 373 | sh_msiof_write(p, STR, sh_msiof_read(p, STR)); |
| 374 | } |
| 375 | |
| 376 | static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p, |
| 377 | const void *tx_buf, int words, int fs) |
| 378 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 379 | const u8 *buf_8 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 380 | int k; |
| 381 | |
| 382 | for (k = 0; k < words; k++) |
| 383 | sh_msiof_write(p, TFDR, buf_8[k] << fs); |
| 384 | } |
| 385 | |
| 386 | static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p, |
| 387 | const void *tx_buf, int words, int fs) |
| 388 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 389 | const u16 *buf_16 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 390 | int k; |
| 391 | |
| 392 | for (k = 0; k < words; k++) |
| 393 | sh_msiof_write(p, TFDR, buf_16[k] << fs); |
| 394 | } |
| 395 | |
| 396 | static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p, |
| 397 | const void *tx_buf, int words, int fs) |
| 398 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 399 | const u16 *buf_16 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 400 | int k; |
| 401 | |
| 402 | for (k = 0; k < words; k++) |
| 403 | sh_msiof_write(p, TFDR, get_unaligned(&buf_16[k]) << fs); |
| 404 | } |
| 405 | |
| 406 | static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p, |
| 407 | const void *tx_buf, int words, int fs) |
| 408 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 409 | const u32 *buf_32 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 410 | int k; |
| 411 | |
| 412 | for (k = 0; k < words; k++) |
| 413 | sh_msiof_write(p, TFDR, buf_32[k] << fs); |
| 414 | } |
| 415 | |
| 416 | static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p, |
| 417 | const void *tx_buf, int words, int fs) |
| 418 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 419 | const u32 *buf_32 = tx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 420 | int k; |
| 421 | |
| 422 | for (k = 0; k < words; k++) |
| 423 | sh_msiof_write(p, TFDR, get_unaligned(&buf_32[k]) << fs); |
| 424 | } |
| 425 | |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 426 | static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p, |
| 427 | const void *tx_buf, int words, int fs) |
| 428 | { |
| 429 | const u32 *buf_32 = tx_buf; |
| 430 | int k; |
| 431 | |
| 432 | for (k = 0; k < words; k++) |
| 433 | sh_msiof_write(p, TFDR, swab32(buf_32[k] << fs)); |
| 434 | } |
| 435 | |
| 436 | static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p, |
| 437 | const void *tx_buf, int words, int fs) |
| 438 | { |
| 439 | const u32 *buf_32 = tx_buf; |
| 440 | int k; |
| 441 | |
| 442 | for (k = 0; k < words; k++) |
| 443 | sh_msiof_write(p, TFDR, swab32(get_unaligned(&buf_32[k]) << fs)); |
| 444 | } |
| 445 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 446 | static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p, |
| 447 | void *rx_buf, int words, int fs) |
| 448 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 449 | u8 *buf_8 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 450 | int k; |
| 451 | |
| 452 | for (k = 0; k < words; k++) |
| 453 | buf_8[k] = sh_msiof_read(p, RFDR) >> fs; |
| 454 | } |
| 455 | |
| 456 | static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p, |
| 457 | void *rx_buf, int words, int fs) |
| 458 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 459 | u16 *buf_16 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 460 | int k; |
| 461 | |
| 462 | for (k = 0; k < words; k++) |
| 463 | buf_16[k] = sh_msiof_read(p, RFDR) >> fs; |
| 464 | } |
| 465 | |
| 466 | static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p, |
| 467 | void *rx_buf, int words, int fs) |
| 468 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 469 | u16 *buf_16 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 470 | int k; |
| 471 | |
| 472 | for (k = 0; k < words; k++) |
| 473 | put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_16[k]); |
| 474 | } |
| 475 | |
| 476 | static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p, |
| 477 | void *rx_buf, int words, int fs) |
| 478 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 479 | u32 *buf_32 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 480 | int k; |
| 481 | |
| 482 | for (k = 0; k < words; k++) |
| 483 | buf_32[k] = sh_msiof_read(p, RFDR) >> fs; |
| 484 | } |
| 485 | |
| 486 | static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p, |
| 487 | void *rx_buf, int words, int fs) |
| 488 | { |
Guennadi Liakhovetski | e2dbf5e | 2011-01-21 16:56:37 +0100 | [diff] [blame] | 489 | u32 *buf_32 = rx_buf; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 490 | int k; |
| 491 | |
| 492 | for (k = 0; k < words; k++) |
| 493 | put_unaligned(sh_msiof_read(p, RFDR) >> fs, &buf_32[k]); |
| 494 | } |
| 495 | |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 496 | static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p, |
| 497 | void *rx_buf, int words, int fs) |
| 498 | { |
| 499 | u32 *buf_32 = rx_buf; |
| 500 | int k; |
| 501 | |
| 502 | for (k = 0; k < words; k++) |
| 503 | buf_32[k] = swab32(sh_msiof_read(p, RFDR) >> fs); |
| 504 | } |
| 505 | |
| 506 | static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p, |
| 507 | void *rx_buf, int words, int fs) |
| 508 | { |
| 509 | u32 *buf_32 = rx_buf; |
| 510 | int k; |
| 511 | |
| 512 | for (k = 0; k < words; k++) |
| 513 | put_unaligned(swab32(sh_msiof_read(p, RFDR) >> fs), &buf_32[k]); |
| 514 | } |
| 515 | |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 516 | static int sh_msiof_spi_setup(struct spi_device *spi) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 517 | { |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 518 | struct device_node *np = spi->master->dev.of_node; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 519 | struct sh_msiof_spi_priv *p = spi_master_get_devdata(spi->master); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 520 | |
Hisashi Nakamura | 01576056 | 2014-12-15 23:01:11 +0900 | [diff] [blame] | 521 | pm_runtime_get_sync(&p->pdev->dev); |
| 522 | |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 523 | if (!np) { |
| 524 | /* |
| 525 | * Use spi->controller_data for CS (same strategy as spi_gpio), |
| 526 | * if any. otherwise let HW control CS |
| 527 | */ |
| 528 | spi->cs_gpio = (uintptr_t)spi->controller_data; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Geert Uytterhoeven | c833ff7 | 2014-02-25 11:21:11 +0100 | [diff] [blame] | 531 | /* Configure pins before deasserting CS */ |
| 532 | sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL), |
| 533 | !!(spi->mode & SPI_CPHA), |
| 534 | !!(spi->mode & SPI_3WIRE), |
| 535 | !!(spi->mode & SPI_LSB_FIRST), |
| 536 | !!(spi->mode & SPI_CS_HIGH)); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 537 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 538 | if (spi->cs_gpio >= 0) |
| 539 | gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); |
| 540 | |
Hisashi Nakamura | 01576056 | 2014-12-15 23:01:11 +0900 | [diff] [blame] | 541 | |
Geert Uytterhoeven | c8935ef | 2015-01-07 16:37:25 +0100 | [diff] [blame] | 542 | pm_runtime_put(&p->pdev->dev); |
Hisashi Nakamura | 01576056 | 2014-12-15 23:01:11 +0900 | [diff] [blame] | 543 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 544 | return 0; |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 545 | } |
| 546 | |
Geert Uytterhoeven | c833ff7 | 2014-02-25 11:21:11 +0100 | [diff] [blame] | 547 | static int sh_msiof_prepare_message(struct spi_master *master, |
| 548 | struct spi_message *msg) |
| 549 | { |
| 550 | struct sh_msiof_spi_priv *p = spi_master_get_devdata(master); |
| 551 | const struct spi_device *spi = msg->spi; |
| 552 | |
Geert Uytterhoeven | c833ff7 | 2014-02-25 11:21:11 +0100 | [diff] [blame] | 553 | /* Configure pins before asserting CS */ |
| 554 | sh_msiof_spi_set_pin_regs(p, !!(spi->mode & SPI_CPOL), |
| 555 | !!(spi->mode & SPI_CPHA), |
| 556 | !!(spi->mode & SPI_3WIRE), |
| 557 | !!(spi->mode & SPI_LSB_FIRST), |
| 558 | !!(spi->mode & SPI_CS_HIGH)); |
| 559 | return 0; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Geert Uytterhoeven | 76c02e7 | 2014-06-20 12:16:17 +0200 | [diff] [blame] | 562 | static int sh_msiof_spi_start(struct sh_msiof_spi_priv *p, void *rx_buf) |
| 563 | { |
| 564 | int ret; |
| 565 | |
| 566 | /* setup clock and rx/tx signals */ |
| 567 | ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TSCKE); |
| 568 | if (rx_buf && !ret) |
| 569 | ret = sh_msiof_modify_ctr_wait(p, 0, CTR_RXE); |
| 570 | if (!ret) |
| 571 | ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TXE); |
| 572 | |
| 573 | /* start by setting frame bit */ |
| 574 | if (!ret) |
| 575 | ret = sh_msiof_modify_ctr_wait(p, 0, CTR_TFSE); |
| 576 | |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | static int sh_msiof_spi_stop(struct sh_msiof_spi_priv *p, void *rx_buf) |
| 581 | { |
| 582 | int ret; |
| 583 | |
| 584 | /* shut down frame, rx/tx and clock signals */ |
| 585 | ret = sh_msiof_modify_ctr_wait(p, CTR_TFSE, 0); |
| 586 | if (!ret) |
| 587 | ret = sh_msiof_modify_ctr_wait(p, CTR_TXE, 0); |
| 588 | if (rx_buf && !ret) |
| 589 | ret = sh_msiof_modify_ctr_wait(p, CTR_RXE, 0); |
| 590 | if (!ret) |
| 591 | ret = sh_msiof_modify_ctr_wait(p, CTR_TSCKE, 0); |
| 592 | |
| 593 | return ret; |
| 594 | } |
| 595 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 596 | static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p, |
| 597 | void (*tx_fifo)(struct sh_msiof_spi_priv *, |
| 598 | const void *, int, int), |
| 599 | void (*rx_fifo)(struct sh_msiof_spi_priv *, |
| 600 | void *, int, int), |
| 601 | const void *tx_buf, void *rx_buf, |
| 602 | int words, int bits) |
| 603 | { |
| 604 | int fifo_shift; |
| 605 | int ret; |
| 606 | |
| 607 | /* limit maximum word transfer to rx/tx fifo size */ |
| 608 | if (tx_buf) |
| 609 | words = min_t(int, words, p->tx_fifo_size); |
| 610 | if (rx_buf) |
| 611 | words = min_t(int, words, p->rx_fifo_size); |
| 612 | |
| 613 | /* the fifo contents need shifting */ |
| 614 | fifo_shift = 32 - bits; |
| 615 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 616 | /* default FIFO watermarks for PIO */ |
| 617 | sh_msiof_write(p, FCTR, 0); |
| 618 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 619 | /* setup msiof transfer mode registers */ |
| 620 | sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 621 | sh_msiof_write(p, IER, IER_TEOFE | IER_REOFE); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 622 | |
| 623 | /* write tx fifo */ |
| 624 | if (tx_buf) |
| 625 | tx_fifo(p, tx_buf, words, fifo_shift); |
| 626 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 627 | reinit_completion(&p->done); |
Geert Uytterhoeven | 76c02e7 | 2014-06-20 12:16:17 +0200 | [diff] [blame] | 628 | |
| 629 | ret = sh_msiof_spi_start(p, rx_buf); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 630 | if (ret) { |
| 631 | dev_err(&p->pdev->dev, "failed to start hardware\n"); |
Geert Uytterhoeven | 75b82e2 | 2014-06-20 12:16:18 +0200 | [diff] [blame] | 632 | goto stop_ier; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* wait for tx fifo to be emptied / rx fifo to be filled */ |
Nicholas Mc Guire | 19f0ad0 | 2015-02-02 03:30:34 -0500 | [diff] [blame] | 636 | if (!wait_for_completion_timeout(&p->done, HZ)) { |
Geert Uytterhoeven | 75b82e2 | 2014-06-20 12:16:18 +0200 | [diff] [blame] | 637 | dev_err(&p->pdev->dev, "PIO timeout\n"); |
| 638 | ret = -ETIMEDOUT; |
| 639 | goto stop_reset; |
| 640 | } |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 641 | |
| 642 | /* read rx fifo */ |
| 643 | if (rx_buf) |
| 644 | rx_fifo(p, rx_buf, words, fifo_shift); |
| 645 | |
| 646 | /* clear status bits */ |
| 647 | sh_msiof_reset_str(p); |
| 648 | |
Geert Uytterhoeven | 76c02e7 | 2014-06-20 12:16:17 +0200 | [diff] [blame] | 649 | ret = sh_msiof_spi_stop(p, rx_buf); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 650 | if (ret) { |
| 651 | dev_err(&p->pdev->dev, "failed to shut down hardware\n"); |
Geert Uytterhoeven | 75b82e2 | 2014-06-20 12:16:18 +0200 | [diff] [blame] | 652 | return ret; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | return words; |
| 656 | |
Geert Uytterhoeven | 75b82e2 | 2014-06-20 12:16:18 +0200 | [diff] [blame] | 657 | stop_reset: |
| 658 | sh_msiof_reset_str(p); |
| 659 | sh_msiof_spi_stop(p, rx_buf); |
| 660 | stop_ier: |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 661 | sh_msiof_write(p, IER, 0); |
| 662 | return ret; |
| 663 | } |
| 664 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 665 | static void sh_msiof_dma_complete(void *arg) |
| 666 | { |
| 667 | struct sh_msiof_spi_priv *p = arg; |
| 668 | |
| 669 | sh_msiof_write(p, IER, 0); |
| 670 | complete(&p->done); |
| 671 | } |
| 672 | |
| 673 | static int sh_msiof_dma_once(struct sh_msiof_spi_priv *p, const void *tx, |
| 674 | void *rx, unsigned int len) |
| 675 | { |
| 676 | u32 ier_bits = 0; |
| 677 | struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL; |
| 678 | dma_cookie_t cookie; |
| 679 | int ret; |
| 680 | |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 681 | /* First prepare and submit the DMA request(s), as this may fail */ |
| 682 | if (rx) { |
| 683 | ier_bits |= IER_RDREQE | IER_RDMAE; |
| 684 | desc_rx = dmaengine_prep_slave_single(p->master->dma_rx, |
| 685 | p->rx_dma_addr, len, DMA_FROM_DEVICE, |
| 686 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Geert Uytterhoeven | a5e7c71 | 2014-08-07 14:07:42 +0200 | [diff] [blame] | 687 | if (!desc_rx) |
| 688 | return -EAGAIN; |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 689 | |
| 690 | desc_rx->callback = sh_msiof_dma_complete; |
| 691 | desc_rx->callback_param = p; |
| 692 | cookie = dmaengine_submit(desc_rx); |
Geert Uytterhoeven | a5e7c71 | 2014-08-07 14:07:42 +0200 | [diff] [blame] | 693 | if (dma_submit_error(cookie)) |
| 694 | return cookie; |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 695 | } |
| 696 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 697 | if (tx) { |
| 698 | ier_bits |= IER_TDREQE | IER_TDMAE; |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 699 | dma_sync_single_for_device(p->master->dma_tx->device->dev, |
| 700 | p->tx_dma_addr, len, DMA_TO_DEVICE); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 701 | desc_tx = dmaengine_prep_slave_single(p->master->dma_tx, |
| 702 | p->tx_dma_addr, len, DMA_TO_DEVICE, |
| 703 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 704 | if (!desc_tx) { |
| 705 | ret = -EAGAIN; |
| 706 | goto no_dma_tx; |
| 707 | } |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 708 | |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 709 | if (rx) { |
| 710 | /* No callback */ |
| 711 | desc_tx->callback = NULL; |
| 712 | } else { |
| 713 | desc_tx->callback = sh_msiof_dma_complete; |
| 714 | desc_tx->callback_param = p; |
| 715 | } |
| 716 | cookie = dmaengine_submit(desc_tx); |
| 717 | if (dma_submit_error(cookie)) { |
| 718 | ret = cookie; |
| 719 | goto no_dma_tx; |
| 720 | } |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 721 | } |
Geert Uytterhoeven | 279d237 | 2014-07-09 12:26:23 +0200 | [diff] [blame] | 722 | |
| 723 | /* 1 stage FIFO watermarks for DMA */ |
| 724 | sh_msiof_write(p, FCTR, FCTR_TFWM_1 | FCTR_RFWM_1); |
| 725 | |
| 726 | /* setup msiof transfer mode registers (32-bit words) */ |
| 727 | sh_msiof_spi_set_mode_regs(p, tx, rx, 32, len / 4); |
| 728 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 729 | sh_msiof_write(p, IER, ier_bits); |
| 730 | |
| 731 | reinit_completion(&p->done); |
| 732 | |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 733 | /* Now start DMA */ |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 734 | if (rx) |
Geert Uytterhoeven | 7a9f957 | 2014-08-07 14:07:43 +0200 | [diff] [blame] | 735 | dma_async_issue_pending(p->master->dma_rx); |
| 736 | if (tx) |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 737 | dma_async_issue_pending(p->master->dma_tx); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 738 | |
| 739 | ret = sh_msiof_spi_start(p, rx); |
| 740 | if (ret) { |
| 741 | dev_err(&p->pdev->dev, "failed to start hardware\n"); |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 742 | goto stop_dma; |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /* wait for tx fifo to be emptied / rx fifo to be filled */ |
Nicholas Mc Guire | 19f0ad0 | 2015-02-02 03:30:34 -0500 | [diff] [blame] | 746 | if (!wait_for_completion_timeout(&p->done, HZ)) { |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 747 | dev_err(&p->pdev->dev, "DMA timeout\n"); |
| 748 | ret = -ETIMEDOUT; |
| 749 | goto stop_reset; |
| 750 | } |
| 751 | |
| 752 | /* clear status bits */ |
| 753 | sh_msiof_reset_str(p); |
| 754 | |
| 755 | ret = sh_msiof_spi_stop(p, rx); |
| 756 | if (ret) { |
| 757 | dev_err(&p->pdev->dev, "failed to shut down hardware\n"); |
| 758 | return ret; |
| 759 | } |
| 760 | |
| 761 | if (rx) |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 762 | dma_sync_single_for_cpu(p->master->dma_rx->device->dev, |
| 763 | p->rx_dma_addr, len, |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 764 | DMA_FROM_DEVICE); |
| 765 | |
| 766 | return 0; |
| 767 | |
| 768 | stop_reset: |
| 769 | sh_msiof_reset_str(p); |
| 770 | sh_msiof_spi_stop(p, rx); |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 771 | stop_dma: |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 772 | if (tx) |
| 773 | dmaengine_terminate_all(p->master->dma_tx); |
Geert Uytterhoeven | 3e81b59 | 2014-08-06 14:59:03 +0200 | [diff] [blame] | 774 | no_dma_tx: |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 775 | if (rx) |
| 776 | dmaengine_terminate_all(p->master->dma_rx); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 777 | sh_msiof_write(p, IER, 0); |
| 778 | return ret; |
| 779 | } |
| 780 | |
| 781 | static void copy_bswap32(u32 *dst, const u32 *src, unsigned int words) |
| 782 | { |
| 783 | /* src or dst can be unaligned, but not both */ |
| 784 | if ((unsigned long)src & 3) { |
| 785 | while (words--) { |
| 786 | *dst++ = swab32(get_unaligned(src)); |
| 787 | src++; |
| 788 | } |
| 789 | } else if ((unsigned long)dst & 3) { |
| 790 | while (words--) { |
| 791 | put_unaligned(swab32(*src++), dst); |
| 792 | dst++; |
| 793 | } |
| 794 | } else { |
| 795 | while (words--) |
| 796 | *dst++ = swab32(*src++); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | static void copy_wswap32(u32 *dst, const u32 *src, unsigned int words) |
| 801 | { |
| 802 | /* src or dst can be unaligned, but not both */ |
| 803 | if ((unsigned long)src & 3) { |
| 804 | while (words--) { |
| 805 | *dst++ = swahw32(get_unaligned(src)); |
| 806 | src++; |
| 807 | } |
| 808 | } else if ((unsigned long)dst & 3) { |
| 809 | while (words--) { |
| 810 | put_unaligned(swahw32(*src++), dst); |
| 811 | dst++; |
| 812 | } |
| 813 | } else { |
| 814 | while (words--) |
| 815 | *dst++ = swahw32(*src++); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | static void copy_plain32(u32 *dst, const u32 *src, unsigned int words) |
| 820 | { |
| 821 | memcpy(dst, src, words * 4); |
| 822 | } |
| 823 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 824 | static int sh_msiof_transfer_one(struct spi_master *master, |
| 825 | struct spi_device *spi, |
| 826 | struct spi_transfer *t) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 827 | { |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 828 | struct sh_msiof_spi_priv *p = spi_master_get_devdata(master); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 829 | void (*copy32)(u32 *, const u32 *, unsigned int); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 830 | void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int); |
| 831 | void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int); |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 832 | const void *tx_buf = t->tx_buf; |
| 833 | void *rx_buf = t->rx_buf; |
| 834 | unsigned int len = t->len; |
| 835 | unsigned int bits = t->bits_per_word; |
| 836 | unsigned int bytes_per_word; |
| 837 | unsigned int words; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 838 | int n; |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 839 | bool swab; |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 840 | int ret; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 841 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 842 | /* setup clocks (clock already enabled in chipselect()) */ |
| 843 | sh_msiof_spi_set_clk_regs(p, clk_get_rate(p->clk), t->speed_hz); |
| 844 | |
| 845 | while (master->dma_tx && len > 15) { |
| 846 | /* |
| 847 | * DMA supports 32-bit words only, hence pack 8-bit and 16-bit |
| 848 | * words, with byte resp. word swapping. |
| 849 | */ |
Koji Matsuoka | fe78d0b | 2015-06-15 02:25:05 +0900 | [diff] [blame] | 850 | unsigned int l = 0; |
| 851 | |
| 852 | if (tx_buf) |
| 853 | l = min(len, p->tx_fifo_size * 4); |
| 854 | if (rx_buf) |
| 855 | l = min(len, p->rx_fifo_size * 4); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 856 | |
| 857 | if (bits <= 8) { |
| 858 | if (l & 3) |
| 859 | break; |
| 860 | copy32 = copy_bswap32; |
| 861 | } else if (bits <= 16) { |
| 862 | if (l & 1) |
| 863 | break; |
| 864 | copy32 = copy_wswap32; |
| 865 | } else { |
| 866 | copy32 = copy_plain32; |
| 867 | } |
| 868 | |
| 869 | if (tx_buf) |
| 870 | copy32(p->tx_dma_page, tx_buf, l / 4); |
| 871 | |
| 872 | ret = sh_msiof_dma_once(p, tx_buf, rx_buf, l); |
Geert Uytterhoeven | 279d237 | 2014-07-09 12:26:23 +0200 | [diff] [blame] | 873 | if (ret == -EAGAIN) { |
| 874 | pr_warn_once("%s %s: DMA not available, falling back to PIO\n", |
| 875 | dev_driver_string(&p->pdev->dev), |
| 876 | dev_name(&p->pdev->dev)); |
| 877 | break; |
| 878 | } |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 879 | if (ret) |
| 880 | return ret; |
| 881 | |
| 882 | if (rx_buf) { |
| 883 | copy32(rx_buf, p->rx_dma_page, l / 4); |
| 884 | rx_buf += l; |
| 885 | } |
| 886 | if (tx_buf) |
| 887 | tx_buf += l; |
| 888 | |
| 889 | len -= l; |
| 890 | if (!len) |
| 891 | return 0; |
| 892 | } |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 893 | |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 894 | if (bits <= 8 && len > 15 && !(len & 3)) { |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 895 | bits = 32; |
| 896 | swab = true; |
| 897 | } else { |
| 898 | swab = false; |
| 899 | } |
| 900 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 901 | /* setup bytes per word and fifo read/write functions */ |
| 902 | if (bits <= 8) { |
| 903 | bytes_per_word = 1; |
| 904 | tx_fifo = sh_msiof_spi_write_fifo_8; |
| 905 | rx_fifo = sh_msiof_spi_read_fifo_8; |
| 906 | } else if (bits <= 16) { |
| 907 | bytes_per_word = 2; |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 908 | if ((unsigned long)tx_buf & 0x01) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 909 | tx_fifo = sh_msiof_spi_write_fifo_16u; |
| 910 | else |
| 911 | tx_fifo = sh_msiof_spi_write_fifo_16; |
| 912 | |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 913 | if ((unsigned long)rx_buf & 0x01) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 914 | rx_fifo = sh_msiof_spi_read_fifo_16u; |
| 915 | else |
| 916 | rx_fifo = sh_msiof_spi_read_fifo_16; |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 917 | } else if (swab) { |
| 918 | bytes_per_word = 4; |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 919 | if ((unsigned long)tx_buf & 0x03) |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 920 | tx_fifo = sh_msiof_spi_write_fifo_s32u; |
| 921 | else |
| 922 | tx_fifo = sh_msiof_spi_write_fifo_s32; |
| 923 | |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 924 | if ((unsigned long)rx_buf & 0x03) |
Guennadi Liakhovetski | 9dabb3f | 2011-01-21 16:56:42 +0100 | [diff] [blame] | 925 | rx_fifo = sh_msiof_spi_read_fifo_s32u; |
| 926 | else |
| 927 | rx_fifo = sh_msiof_spi_read_fifo_s32; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 928 | } else { |
| 929 | bytes_per_word = 4; |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 930 | if ((unsigned long)tx_buf & 0x03) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 931 | tx_fifo = sh_msiof_spi_write_fifo_32u; |
| 932 | else |
| 933 | tx_fifo = sh_msiof_spi_write_fifo_32; |
| 934 | |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 935 | if ((unsigned long)rx_buf & 0x03) |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 936 | rx_fifo = sh_msiof_spi_read_fifo_32u; |
| 937 | else |
| 938 | rx_fifo = sh_msiof_spi_read_fifo_32; |
| 939 | } |
| 940 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 941 | /* transfer in fifo sized chunks */ |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 942 | words = len / bytes_per_word; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 943 | |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 944 | while (words > 0) { |
| 945 | n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo, tx_buf, rx_buf, |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 946 | words, bits); |
| 947 | if (n < 0) |
Geert Uytterhoeven | 75b82e2 | 2014-06-20 12:16:18 +0200 | [diff] [blame] | 948 | return n; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 949 | |
Geert Uytterhoeven | 0312d59 | 2014-06-20 12:16:19 +0200 | [diff] [blame] | 950 | if (tx_buf) |
| 951 | tx_buf += n * bytes_per_word; |
| 952 | if (rx_buf) |
| 953 | rx_buf += n * bytes_per_word; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 954 | words -= n; |
| 955 | } |
| 956 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 957 | return 0; |
| 958 | } |
| 959 | |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 960 | static const struct sh_msiof_chipdata sh_data = { |
| 961 | .tx_fifo_size = 64, |
| 962 | .rx_fifo_size = 64, |
Geert Uytterhoeven | beb74bb | 2014-02-25 11:21:10 +0100 | [diff] [blame] | 963 | .master_flags = 0, |
| 964 | }; |
| 965 | |
| 966 | static const struct sh_msiof_chipdata r8a779x_data = { |
| 967 | .tx_fifo_size = 64, |
Koji Matsuoka | fe78d0b | 2015-06-15 02:25:05 +0900 | [diff] [blame] | 968 | .rx_fifo_size = 64, |
Geert Uytterhoeven | beb74bb | 2014-02-25 11:21:10 +0100 | [diff] [blame] | 969 | .master_flags = SPI_MASTER_MUST_TX, |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 970 | }; |
| 971 | |
| 972 | static const struct of_device_id sh_msiof_match[] = { |
| 973 | { .compatible = "renesas,sh-msiof", .data = &sh_data }, |
| 974 | { .compatible = "renesas,sh-mobile-msiof", .data = &sh_data }, |
Geert Uytterhoeven | beb74bb | 2014-02-25 11:21:10 +0100 | [diff] [blame] | 975 | { .compatible = "renesas,msiof-r8a7790", .data = &r8a779x_data }, |
| 976 | { .compatible = "renesas,msiof-r8a7791", .data = &r8a779x_data }, |
Geert Uytterhoeven | e221cc9 | 2014-08-28 10:11:03 +0200 | [diff] [blame] | 977 | { .compatible = "renesas,msiof-r8a7792", .data = &r8a779x_data }, |
| 978 | { .compatible = "renesas,msiof-r8a7793", .data = &r8a779x_data }, |
| 979 | { .compatible = "renesas,msiof-r8a7794", .data = &r8a779x_data }, |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 980 | {}, |
| 981 | }; |
| 982 | MODULE_DEVICE_TABLE(of, sh_msiof_match); |
| 983 | |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 984 | #ifdef CONFIG_OF |
| 985 | static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev) |
| 986 | { |
| 987 | struct sh_msiof_spi_info *info; |
| 988 | struct device_node *np = dev->of_node; |
Geert Uytterhoeven | 32d3b2d | 2014-02-25 11:21:08 +0100 | [diff] [blame] | 989 | u32 num_cs = 1; |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 990 | |
| 991 | info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL); |
Jingoo Han | 1e8231b | 2014-04-29 17:21:25 +0900 | [diff] [blame] | 992 | if (!info) |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 993 | return NULL; |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 994 | |
| 995 | /* Parse the MSIOF properties */ |
| 996 | of_property_read_u32(np, "num-cs", &num_cs); |
| 997 | of_property_read_u32(np, "renesas,tx-fifo-size", |
| 998 | &info->tx_fifo_override); |
| 999 | of_property_read_u32(np, "renesas,rx-fifo-size", |
| 1000 | &info->rx_fifo_override); |
Yoshihiro Shimoda | 3110628 | 2014-12-19 17:15:53 +0900 | [diff] [blame] | 1001 | of_property_read_u32(np, "renesas,dtdl", &info->dtdl); |
| 1002 | of_property_read_u32(np, "renesas,syncdl", &info->syncdl); |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 1003 | |
| 1004 | info->num_chipselect = num_cs; |
| 1005 | |
| 1006 | return info; |
| 1007 | } |
| 1008 | #else |
| 1009 | static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev) |
| 1010 | { |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | #endif |
| 1014 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1015 | static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev, |
| 1016 | enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr) |
| 1017 | { |
| 1018 | dma_cap_mask_t mask; |
| 1019 | struct dma_chan *chan; |
| 1020 | struct dma_slave_config cfg; |
| 1021 | int ret; |
| 1022 | |
| 1023 | dma_cap_zero(mask); |
| 1024 | dma_cap_set(DMA_SLAVE, mask); |
| 1025 | |
Geert Uytterhoeven | a6be4de | 2014-08-06 14:59:05 +0200 | [diff] [blame] | 1026 | chan = dma_request_slave_channel_compat(mask, shdma_chan_filter, |
| 1027 | (void *)(unsigned long)id, dev, |
| 1028 | dir == DMA_MEM_TO_DEV ? "tx" : "rx"); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1029 | if (!chan) { |
Geert Uytterhoeven | a6be4de | 2014-08-06 14:59:05 +0200 | [diff] [blame] | 1030 | dev_warn(dev, "dma_request_slave_channel_compat failed\n"); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1031 | return NULL; |
| 1032 | } |
| 1033 | |
| 1034 | memset(&cfg, 0, sizeof(cfg)); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1035 | cfg.direction = dir; |
Geert Uytterhoeven | 52fba2b | 2014-08-06 14:59:04 +0200 | [diff] [blame] | 1036 | if (dir == DMA_MEM_TO_DEV) { |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1037 | cfg.dst_addr = port_addr; |
Geert Uytterhoeven | 52fba2b | 2014-08-06 14:59:04 +0200 | [diff] [blame] | 1038 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1039 | } else { |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1040 | cfg.src_addr = port_addr; |
Geert Uytterhoeven | 52fba2b | 2014-08-06 14:59:04 +0200 | [diff] [blame] | 1041 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 1042 | } |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1043 | |
| 1044 | ret = dmaengine_slave_config(chan, &cfg); |
| 1045 | if (ret) { |
| 1046 | dev_warn(dev, "dmaengine_slave_config failed %d\n", ret); |
| 1047 | dma_release_channel(chan); |
| 1048 | return NULL; |
| 1049 | } |
| 1050 | |
| 1051 | return chan; |
| 1052 | } |
| 1053 | |
| 1054 | static int sh_msiof_request_dma(struct sh_msiof_spi_priv *p) |
| 1055 | { |
| 1056 | struct platform_device *pdev = p->pdev; |
| 1057 | struct device *dev = &pdev->dev; |
| 1058 | const struct sh_msiof_spi_info *info = dev_get_platdata(dev); |
Geert Uytterhoeven | a6be4de | 2014-08-06 14:59:05 +0200 | [diff] [blame] | 1059 | unsigned int dma_tx_id, dma_rx_id; |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1060 | const struct resource *res; |
| 1061 | struct spi_master *master; |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1062 | struct device *tx_dev, *rx_dev; |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1063 | |
Geert Uytterhoeven | a6be4de | 2014-08-06 14:59:05 +0200 | [diff] [blame] | 1064 | if (dev->of_node) { |
| 1065 | /* In the OF case we will get the slave IDs from the DT */ |
| 1066 | dma_tx_id = 0; |
| 1067 | dma_rx_id = 0; |
| 1068 | } else if (info && info->dma_tx_id && info->dma_rx_id) { |
| 1069 | dma_tx_id = info->dma_tx_id; |
| 1070 | dma_rx_id = info->dma_rx_id; |
| 1071 | } else { |
| 1072 | /* The driver assumes no error */ |
| 1073 | return 0; |
| 1074 | } |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1075 | |
| 1076 | /* The DMA engine uses the second register set, if present */ |
| 1077 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1078 | if (!res) |
| 1079 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1080 | |
| 1081 | master = p->master; |
| 1082 | master->dma_tx = sh_msiof_request_dma_chan(dev, DMA_MEM_TO_DEV, |
Geert Uytterhoeven | a6be4de | 2014-08-06 14:59:05 +0200 | [diff] [blame] | 1083 | dma_tx_id, |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1084 | res->start + TFDR); |
| 1085 | if (!master->dma_tx) |
| 1086 | return -ENODEV; |
| 1087 | |
| 1088 | master->dma_rx = sh_msiof_request_dma_chan(dev, DMA_DEV_TO_MEM, |
Geert Uytterhoeven | a6be4de | 2014-08-06 14:59:05 +0200 | [diff] [blame] | 1089 | dma_rx_id, |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1090 | res->start + RFDR); |
| 1091 | if (!master->dma_rx) |
| 1092 | goto free_tx_chan; |
| 1093 | |
| 1094 | p->tx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA); |
| 1095 | if (!p->tx_dma_page) |
| 1096 | goto free_rx_chan; |
| 1097 | |
| 1098 | p->rx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA); |
| 1099 | if (!p->rx_dma_page) |
| 1100 | goto free_tx_page; |
| 1101 | |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1102 | tx_dev = master->dma_tx->device->dev; |
| 1103 | p->tx_dma_addr = dma_map_single(tx_dev, p->tx_dma_page, PAGE_SIZE, |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1104 | DMA_TO_DEVICE); |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1105 | if (dma_mapping_error(tx_dev, p->tx_dma_addr)) |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1106 | goto free_rx_page; |
| 1107 | |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1108 | rx_dev = master->dma_rx->device->dev; |
| 1109 | p->rx_dma_addr = dma_map_single(rx_dev, p->rx_dma_page, PAGE_SIZE, |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1110 | DMA_FROM_DEVICE); |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1111 | if (dma_mapping_error(rx_dev, p->rx_dma_addr)) |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1112 | goto unmap_tx_page; |
| 1113 | |
| 1114 | dev_info(dev, "DMA available"); |
| 1115 | return 0; |
| 1116 | |
| 1117 | unmap_tx_page: |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1118 | dma_unmap_single(tx_dev, p->tx_dma_addr, PAGE_SIZE, DMA_TO_DEVICE); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1119 | free_rx_page: |
| 1120 | free_page((unsigned long)p->rx_dma_page); |
| 1121 | free_tx_page: |
| 1122 | free_page((unsigned long)p->tx_dma_page); |
| 1123 | free_rx_chan: |
| 1124 | dma_release_channel(master->dma_rx); |
| 1125 | free_tx_chan: |
| 1126 | dma_release_channel(master->dma_tx); |
| 1127 | master->dma_tx = NULL; |
| 1128 | return -ENODEV; |
| 1129 | } |
| 1130 | |
| 1131 | static void sh_msiof_release_dma(struct sh_msiof_spi_priv *p) |
| 1132 | { |
| 1133 | struct spi_master *master = p->master; |
| 1134 | struct device *dev; |
| 1135 | |
| 1136 | if (!master->dma_tx) |
| 1137 | return; |
| 1138 | |
| 1139 | dev = &p->pdev->dev; |
Geert Uytterhoeven | 5dabcf2 | 2014-07-11 17:56:22 +0200 | [diff] [blame] | 1140 | dma_unmap_single(master->dma_rx->device->dev, p->rx_dma_addr, |
| 1141 | PAGE_SIZE, DMA_FROM_DEVICE); |
| 1142 | dma_unmap_single(master->dma_tx->device->dev, p->tx_dma_addr, |
| 1143 | PAGE_SIZE, DMA_TO_DEVICE); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1144 | free_page((unsigned long)p->rx_dma_page); |
| 1145 | free_page((unsigned long)p->tx_dma_page); |
| 1146 | dma_release_channel(master->dma_rx); |
| 1147 | dma_release_channel(master->dma_tx); |
| 1148 | } |
| 1149 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1150 | static int sh_msiof_spi_probe(struct platform_device *pdev) |
| 1151 | { |
| 1152 | struct resource *r; |
| 1153 | struct spi_master *master; |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 1154 | const struct sh_msiof_chipdata *chipdata; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1155 | const struct of_device_id *of_id; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1156 | struct sh_msiof_spi_priv *p; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1157 | int i; |
| 1158 | int ret; |
| 1159 | |
| 1160 | master = spi_alloc_master(&pdev->dev, sizeof(struct sh_msiof_spi_priv)); |
| 1161 | if (master == NULL) { |
| 1162 | dev_err(&pdev->dev, "failed to allocate spi master\n"); |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 1163 | return -ENOMEM; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | p = spi_master_get_devdata(master); |
| 1167 | |
| 1168 | platform_set_drvdata(pdev, p); |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1169 | p->master = master; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1170 | |
| 1171 | of_id = of_match_device(sh_msiof_match, &pdev->dev); |
| 1172 | if (of_id) { |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 1173 | chipdata = of_id->data; |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 1174 | p->info = sh_msiof_spi_parse_dt(&pdev->dev); |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1175 | } else { |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 1176 | chipdata = (const void *)pdev->id_entry->driver_data; |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 1177 | p->info = dev_get_platdata(&pdev->dev); |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1178 | } |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 1179 | |
| 1180 | if (!p->info) { |
| 1181 | dev_err(&pdev->dev, "failed to obtain device info\n"); |
| 1182 | ret = -ENXIO; |
| 1183 | goto err1; |
| 1184 | } |
| 1185 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1186 | init_completion(&p->done); |
| 1187 | |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 1188 | p->clk = devm_clk_get(&pdev->dev, NULL); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1189 | if (IS_ERR(p->clk)) { |
Bastian Hecht | 078b6ea | 2012-11-07 12:40:04 +0100 | [diff] [blame] | 1190 | dev_err(&pdev->dev, "cannot get clock\n"); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1191 | ret = PTR_ERR(p->clk); |
| 1192 | goto err1; |
| 1193 | } |
| 1194 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1195 | i = platform_get_irq(pdev, 0); |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 1196 | if (i < 0) { |
| 1197 | dev_err(&pdev->dev, "cannot get platform IRQ\n"); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1198 | ret = -ENOENT; |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 1199 | goto err1; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 1202 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1203 | p->mapbase = devm_ioremap_resource(&pdev->dev, r); |
| 1204 | if (IS_ERR(p->mapbase)) { |
| 1205 | ret = PTR_ERR(p->mapbase); |
| 1206 | goto err1; |
| 1207 | } |
| 1208 | |
| 1209 | ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0, |
| 1210 | dev_name(&pdev->dev), p); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1211 | if (ret) { |
| 1212 | dev_err(&pdev->dev, "unable to request irq\n"); |
Laurent Pinchart | b4dd05de3 | 2013-11-28 02:39:42 +0100 | [diff] [blame] | 1213 | goto err1; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | p->pdev = pdev; |
| 1217 | pm_runtime_enable(&pdev->dev); |
| 1218 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1219 | /* Platform data may override FIFO sizes */ |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 1220 | p->tx_fifo_size = chipdata->tx_fifo_size; |
| 1221 | p->rx_fifo_size = chipdata->rx_fifo_size; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1222 | if (p->info->tx_fifo_override) |
| 1223 | p->tx_fifo_size = p->info->tx_fifo_override; |
| 1224 | if (p->info->rx_fifo_override) |
| 1225 | p->rx_fifo_size = p->info->rx_fifo_override; |
| 1226 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1227 | /* init master code */ |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1228 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 1229 | master->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE; |
Geert Uytterhoeven | a6802cc | 2016-06-22 14:50:03 +0200 | [diff] [blame] | 1230 | master->flags = chipdata->master_flags; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1231 | master->bus_num = pdev->id; |
Geert Uytterhoeven | f7c05e8 | 2014-02-20 15:43:00 +0100 | [diff] [blame] | 1232 | master->dev.of_node = pdev->dev.of_node; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1233 | master->num_chipselect = p->info->num_chipselect; |
Geert Uytterhoeven | 8d19534 | 2014-02-20 15:43:04 +0100 | [diff] [blame] | 1234 | master->setup = sh_msiof_spi_setup; |
Geert Uytterhoeven | c833ff7 | 2014-02-25 11:21:11 +0100 | [diff] [blame] | 1235 | master->prepare_message = sh_msiof_prepare_message; |
Geert Uytterhoeven | 2416289 | 2014-02-25 11:21:12 +0100 | [diff] [blame] | 1236 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32); |
Geert Uytterhoeven | e2a0ba5 | 2014-03-11 10:59:11 +0100 | [diff] [blame] | 1237 | master->auto_runtime_pm = true; |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1238 | master->transfer_one = sh_msiof_transfer_one; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1239 | |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1240 | ret = sh_msiof_request_dma(p); |
| 1241 | if (ret < 0) |
| 1242 | dev_warn(&pdev->dev, "DMA not available, using PIO\n"); |
| 1243 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1244 | ret = devm_spi_register_master(&pdev->dev, master); |
| 1245 | if (ret < 0) { |
| 1246 | dev_err(&pdev->dev, "spi_register_master error.\n"); |
| 1247 | goto err2; |
| 1248 | } |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1249 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1250 | return 0; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1251 | |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1252 | err2: |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1253 | sh_msiof_release_dma(p); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1254 | pm_runtime_disable(&pdev->dev); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1255 | err1: |
| 1256 | spi_master_put(master); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1257 | return ret; |
| 1258 | } |
| 1259 | |
| 1260 | static int sh_msiof_spi_remove(struct platform_device *pdev) |
| 1261 | { |
Geert Uytterhoeven | b0d0ce8 | 2014-06-30 12:10:24 +0200 | [diff] [blame] | 1262 | struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev); |
| 1263 | |
| 1264 | sh_msiof_release_dma(p); |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1265 | pm_runtime_disable(&pdev->dev); |
Geert Uytterhoeven | 1bd6363bc0 | 2014-02-25 11:21:13 +0100 | [diff] [blame] | 1266 | return 0; |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Krzysztof Kozlowski | 3789c8520 | 2015-05-02 00:44:07 +0900 | [diff] [blame] | 1269 | static const struct platform_device_id spi_driver_ids[] = { |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1270 | { "spi_sh_msiof", (kernel_ulong_t)&sh_data }, |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 1271 | {}, |
| 1272 | }; |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1273 | MODULE_DEVICE_TABLE(platform, spi_driver_ids); |
Bastian Hecht | cf9c86e | 2012-12-12 12:54:48 +0100 | [diff] [blame] | 1274 | |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1275 | static struct platform_driver sh_msiof_spi_drv = { |
| 1276 | .probe = sh_msiof_spi_probe, |
| 1277 | .remove = sh_msiof_spi_remove, |
Geert Uytterhoeven | 50a7e23 | 2014-02-25 11:21:09 +0100 | [diff] [blame] | 1278 | .id_table = spi_driver_ids, |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1279 | .driver = { |
| 1280 | .name = "spi_sh_msiof", |
Sachin Kamat | 691ee4e | 2013-03-14 15:31:51 +0530 | [diff] [blame] | 1281 | .of_match_table = of_match_ptr(sh_msiof_match), |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1282 | }, |
| 1283 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 1284 | module_platform_driver(sh_msiof_spi_drv); |
Magnus Damm | 8051eff | 2009-11-26 11:10:05 +0000 | [diff] [blame] | 1285 | |
| 1286 | MODULE_DESCRIPTION("SuperH MSIOF SPI Master Interface Driver"); |
| 1287 | MODULE_AUTHOR("Magnus Damm"); |
| 1288 | MODULE_LICENSE("GPL v2"); |
| 1289 | MODULE_ALIAS("platform:spi_sh_msiof"); |