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