Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1 | /* |
| 2 | * SH RSPI driver |
| 3 | * |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 4 | * Copyright (C) 2012, 2013 Renesas Solutions Corp. |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 5 | * |
| 6 | * Based on spi-sh.c: |
| 7 | * Copyright (C) 2011 Renesas Solutions Corp. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; version 2 of the License. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/errno.h> |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 28 | #include <linux/interrupt.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/io.h> |
| 31 | #include <linux/clk.h> |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 32 | #include <linux/dmaengine.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | #include <linux/sh_dma.h> |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 35 | #include <linux/spi/spi.h> |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 36 | #include <linux/spi/rspi.h> |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 37 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 38 | #define RSPI_SPCR 0x00 /* Control Register */ |
| 39 | #define RSPI_SSLP 0x01 /* Slave Select Polarity Register */ |
| 40 | #define RSPI_SPPCR 0x02 /* Pin Control Register */ |
| 41 | #define RSPI_SPSR 0x03 /* Status Register */ |
| 42 | #define RSPI_SPDR 0x04 /* Data Register */ |
| 43 | #define RSPI_SPSCR 0x08 /* Sequence Control Register */ |
| 44 | #define RSPI_SPSSR 0x09 /* Sequence Status Register */ |
| 45 | #define RSPI_SPBR 0x0a /* Bit Rate Register */ |
| 46 | #define RSPI_SPDCR 0x0b /* Data Control Register */ |
| 47 | #define RSPI_SPCKD 0x0c /* Clock Delay Register */ |
| 48 | #define RSPI_SSLND 0x0d /* Slave Select Negation Delay Register */ |
| 49 | #define RSPI_SPND 0x0e /* Next-Access Delay Register */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 50 | #define RSPI_SPCR2 0x0f /* Control Register 2 (SH only) */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 51 | #define RSPI_SPCMD0 0x10 /* Command Register 0 */ |
| 52 | #define RSPI_SPCMD1 0x12 /* Command Register 1 */ |
| 53 | #define RSPI_SPCMD2 0x14 /* Command Register 2 */ |
| 54 | #define RSPI_SPCMD3 0x16 /* Command Register 3 */ |
| 55 | #define RSPI_SPCMD4 0x18 /* Command Register 4 */ |
| 56 | #define RSPI_SPCMD5 0x1a /* Command Register 5 */ |
| 57 | #define RSPI_SPCMD6 0x1c /* Command Register 6 */ |
| 58 | #define RSPI_SPCMD7 0x1e /* Command Register 7 */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 59 | |
| 60 | /* RSPI on RZ only */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 61 | #define RSPI_SPBFCR 0x20 /* Buffer Control Register */ |
| 62 | #define RSPI_SPBFDR 0x22 /* Buffer Data Count Setting Register */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 63 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 64 | /* QSPI only */ |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 65 | #define QSPI_SPBFCR 0x18 /* Buffer Control Register */ |
| 66 | #define QSPI_SPBDCR 0x1a /* Buffer Data Count Register */ |
| 67 | #define QSPI_SPBMUL0 0x1c /* Transfer Data Length Multiplier Setting Register 0 */ |
| 68 | #define QSPI_SPBMUL1 0x20 /* Transfer Data Length Multiplier Setting Register 1 */ |
| 69 | #define QSPI_SPBMUL2 0x24 /* Transfer Data Length Multiplier Setting Register 2 */ |
| 70 | #define QSPI_SPBMUL3 0x28 /* Transfer Data Length Multiplier Setting Register 3 */ |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 71 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 72 | /* SPCR - Control Register */ |
| 73 | #define SPCR_SPRIE 0x80 /* Receive Interrupt Enable */ |
| 74 | #define SPCR_SPE 0x40 /* Function Enable */ |
| 75 | #define SPCR_SPTIE 0x20 /* Transmit Interrupt Enable */ |
| 76 | #define SPCR_SPEIE 0x10 /* Error Interrupt Enable */ |
| 77 | #define SPCR_MSTR 0x08 /* Master/Slave Mode Select */ |
| 78 | #define SPCR_MODFEN 0x04 /* Mode Fault Error Detection Enable */ |
| 79 | /* RSPI on SH only */ |
| 80 | #define SPCR_TXMD 0x02 /* TX Only Mode (vs. Full Duplex) */ |
| 81 | #define SPCR_SPMS 0x01 /* 3-wire Mode (vs. 4-wire) */ |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 82 | /* QSPI on R-Car M2 only */ |
| 83 | #define SPCR_WSWAP 0x02 /* Word Swap of read-data for DMAC */ |
| 84 | #define SPCR_BSWAP 0x01 /* Byte Swap of read-data for DMAC */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 85 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 86 | /* SSLP - Slave Select Polarity Register */ |
| 87 | #define SSLP_SSL1P 0x02 /* SSL1 Signal Polarity Setting */ |
| 88 | #define SSLP_SSL0P 0x01 /* SSL0 Signal Polarity Setting */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 89 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 90 | /* SPPCR - Pin Control Register */ |
| 91 | #define SPPCR_MOIFE 0x20 /* MOSI Idle Value Fixing Enable */ |
| 92 | #define SPPCR_MOIFV 0x10 /* MOSI Idle Fixed Value */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 93 | #define SPPCR_SPOM 0x04 |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 94 | #define SPPCR_SPLP2 0x02 /* Loopback Mode 2 (non-inverting) */ |
| 95 | #define SPPCR_SPLP 0x01 /* Loopback Mode (inverting) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 96 | |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 97 | #define SPPCR_IO3FV 0x04 /* Single-/Dual-SPI Mode IO3 Output Fixed Value */ |
| 98 | #define SPPCR_IO2FV 0x04 /* Single-/Dual-SPI Mode IO2 Output Fixed Value */ |
| 99 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 100 | /* SPSR - Status Register */ |
| 101 | #define SPSR_SPRF 0x80 /* Receive Buffer Full Flag */ |
| 102 | #define SPSR_TEND 0x40 /* Transmit End */ |
| 103 | #define SPSR_SPTEF 0x20 /* Transmit Buffer Empty Flag */ |
| 104 | #define SPSR_PERF 0x08 /* Parity Error Flag */ |
| 105 | #define SPSR_MODF 0x04 /* Mode Fault Error Flag */ |
| 106 | #define SPSR_IDLNF 0x02 /* RSPI Idle Flag */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 107 | #define SPSR_OVRF 0x01 /* Overrun Error Flag (RSPI only) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 108 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 109 | /* SPSCR - Sequence Control Register */ |
| 110 | #define SPSCR_SPSLN_MASK 0x07 /* Sequence Length Specification */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 111 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 112 | /* SPSSR - Sequence Status Register */ |
| 113 | #define SPSSR_SPECM_MASK 0x70 /* Command Error Mask */ |
| 114 | #define SPSSR_SPCP_MASK 0x07 /* Command Pointer Mask */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 115 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 116 | /* SPDCR - Data Control Register */ |
| 117 | #define SPDCR_TXDMY 0x80 /* Dummy Data Transmission Enable */ |
| 118 | #define SPDCR_SPLW1 0x40 /* Access Width Specification (RZ) */ |
| 119 | #define SPDCR_SPLW0 0x20 /* Access Width Specification (RZ) */ |
| 120 | #define SPDCR_SPLLWORD (SPDCR_SPLW1 | SPDCR_SPLW0) |
| 121 | #define SPDCR_SPLWORD SPDCR_SPLW1 |
| 122 | #define SPDCR_SPLBYTE SPDCR_SPLW0 |
| 123 | #define SPDCR_SPLW 0x20 /* Access Width Specification (SH) */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 124 | #define SPDCR_SPRDTD 0x10 /* Receive Transmit Data Select (SH) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 125 | #define SPDCR_SLSEL1 0x08 |
| 126 | #define SPDCR_SLSEL0 0x04 |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 127 | #define SPDCR_SLSEL_MASK 0x0c /* SSL1 Output Select (SH) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 128 | #define SPDCR_SPFC1 0x02 |
| 129 | #define SPDCR_SPFC0 0x01 |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 130 | #define SPDCR_SPFC_MASK 0x03 /* Frame Count Setting (1-4) (SH) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 131 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 132 | /* SPCKD - Clock Delay Register */ |
| 133 | #define SPCKD_SCKDL_MASK 0x07 /* Clock Delay Setting (1-8) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 134 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 135 | /* SSLND - Slave Select Negation Delay Register */ |
| 136 | #define SSLND_SLNDL_MASK 0x07 /* SSL Negation Delay Setting (1-8) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 137 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 138 | /* SPND - Next-Access Delay Register */ |
| 139 | #define SPND_SPNDL_MASK 0x07 /* Next-Access Delay Setting (1-8) */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 140 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 141 | /* SPCR2 - Control Register 2 */ |
| 142 | #define SPCR2_PTE 0x08 /* Parity Self-Test Enable */ |
| 143 | #define SPCR2_SPIE 0x04 /* Idle Interrupt Enable */ |
| 144 | #define SPCR2_SPOE 0x02 /* Odd Parity Enable (vs. Even) */ |
| 145 | #define SPCR2_SPPE 0x01 /* Parity Enable */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 146 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 147 | /* SPCMDn - Command Registers */ |
| 148 | #define SPCMD_SCKDEN 0x8000 /* Clock Delay Setting Enable */ |
| 149 | #define SPCMD_SLNDEN 0x4000 /* SSL Negation Delay Setting Enable */ |
| 150 | #define SPCMD_SPNDEN 0x2000 /* Next-Access Delay Enable */ |
| 151 | #define SPCMD_LSBF 0x1000 /* LSB First */ |
| 152 | #define SPCMD_SPB_MASK 0x0f00 /* Data Length Setting */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 153 | #define SPCMD_SPB_8_TO_16(bit) (((bit - 1) << 8) & SPCMD_SPB_MASK) |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 154 | #define SPCMD_SPB_8BIT 0x0000 /* qspi only */ |
| 155 | #define SPCMD_SPB_16BIT 0x0100 |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 156 | #define SPCMD_SPB_20BIT 0x0000 |
| 157 | #define SPCMD_SPB_24BIT 0x0100 |
| 158 | #define SPCMD_SPB_32BIT 0x0200 |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 159 | #define SPCMD_SSLKP 0x0080 /* SSL Signal Level Keeping */ |
Geert Uytterhoeven | fbe5072 | 2014-01-12 11:27:38 +0100 | [diff] [blame] | 160 | #define SPCMD_SPIMOD_MASK 0x0060 /* SPI Operating Mode (QSPI only) */ |
| 161 | #define SPCMD_SPIMOD1 0x0040 |
| 162 | #define SPCMD_SPIMOD0 0x0020 |
| 163 | #define SPCMD_SPIMOD_SINGLE 0 |
| 164 | #define SPCMD_SPIMOD_DUAL SPCMD_SPIMOD0 |
| 165 | #define SPCMD_SPIMOD_QUAD SPCMD_SPIMOD1 |
| 166 | #define SPCMD_SPRW 0x0010 /* SPI Read/Write Access (Dual/Quad) */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 167 | #define SPCMD_SSLA_MASK 0x0030 /* SSL Assert Signal Setting (RSPI) */ |
| 168 | #define SPCMD_BRDV_MASK 0x000c /* Bit Rate Division Setting */ |
| 169 | #define SPCMD_CPOL 0x0002 /* Clock Polarity Setting */ |
| 170 | #define SPCMD_CPHA 0x0001 /* Clock Phase Setting */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 171 | |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 172 | /* SPBFCR - Buffer Control Register */ |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 173 | #define SPBFCR_TXRST 0x80 /* Transmit Buffer Data Reset */ |
| 174 | #define SPBFCR_RXRST 0x40 /* Receive Buffer Data Reset */ |
Geert Uytterhoeven | 6ab4865 | 2014-01-12 11:27:37 +0100 | [diff] [blame] | 175 | #define SPBFCR_TXTRG_MASK 0x30 /* Transmit Buffer Data Triggering Number */ |
| 176 | #define SPBFCR_RXTRG_MASK 0x07 /* Receive Buffer Data Triggering Number */ |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 177 | |
Geert Uytterhoeven | 2aae80b | 2013-12-24 10:49:33 +0100 | [diff] [blame] | 178 | #define DUMMY_DATA 0x00 |
| 179 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 180 | struct rspi_data { |
| 181 | void __iomem *addr; |
| 182 | u32 max_speed_hz; |
| 183 | struct spi_master *master; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 184 | wait_queue_head_t wait; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 185 | struct clk *clk; |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 186 | u8 spsr; |
Geert Uytterhoeven | 348e515 | 2014-01-12 11:27:43 +0100 | [diff] [blame] | 187 | u16 spcmd; |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 188 | int rx_irq, tx_irq; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 189 | const struct spi_ops *ops; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 190 | |
| 191 | /* for dmaengine */ |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 192 | struct dma_chan *chan_tx; |
| 193 | struct dma_chan *chan_rx; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 194 | |
| 195 | unsigned dma_width_16bit:1; |
| 196 | unsigned dma_callbacked:1; |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 197 | unsigned byte_access:1; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 198 | }; |
| 199 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 200 | static void rspi_write8(const struct rspi_data *rspi, u8 data, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 201 | { |
| 202 | iowrite8(data, rspi->addr + offset); |
| 203 | } |
| 204 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 205 | static void rspi_write16(const struct rspi_data *rspi, u16 data, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 206 | { |
| 207 | iowrite16(data, rspi->addr + offset); |
| 208 | } |
| 209 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 210 | static void rspi_write32(const struct rspi_data *rspi, u32 data, u16 offset) |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 211 | { |
| 212 | iowrite32(data, rspi->addr + offset); |
| 213 | } |
| 214 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 215 | static u8 rspi_read8(const struct rspi_data *rspi, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 216 | { |
| 217 | return ioread8(rspi->addr + offset); |
| 218 | } |
| 219 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 220 | static u16 rspi_read16(const struct rspi_data *rspi, u16 offset) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 221 | { |
| 222 | return ioread16(rspi->addr + offset); |
| 223 | } |
| 224 | |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 225 | static void rspi_write_data(const struct rspi_data *rspi, u16 data) |
| 226 | { |
| 227 | if (rspi->byte_access) |
| 228 | rspi_write8(rspi, data, RSPI_SPDR); |
| 229 | else /* 16 bit */ |
| 230 | rspi_write16(rspi, data, RSPI_SPDR); |
| 231 | } |
| 232 | |
| 233 | static u16 rspi_read_data(const struct rspi_data *rspi) |
| 234 | { |
| 235 | if (rspi->byte_access) |
| 236 | return rspi_read8(rspi, RSPI_SPDR); |
| 237 | else /* 16 bit */ |
| 238 | return rspi_read16(rspi, RSPI_SPDR); |
| 239 | } |
| 240 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 241 | /* optional functions */ |
| 242 | struct spi_ops { |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 243 | int (*set_config_register)(struct rspi_data *rspi, int access_size); |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 244 | int (*transfer_one)(struct spi_master *master, struct spi_device *spi, |
| 245 | struct spi_transfer *xfer); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | /* |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 249 | * functions for RSPI on legacy SH |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 250 | */ |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 251 | static int rspi_set_config_register(struct rspi_data *rspi, int access_size) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 252 | { |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 253 | int spbr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 254 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 255 | /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */ |
| 256 | rspi_write8(rspi, 0x00, RSPI_SPPCR); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 257 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 258 | /* Sets transfer bit rate */ |
| 259 | spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1; |
| 260 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); |
| 261 | |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 262 | /* Disable dummy transmission, set 16-bit word access, 1 frame */ |
| 263 | rspi_write8(rspi, 0, RSPI_SPDCR); |
| 264 | rspi->byte_access = 0; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 265 | |
| 266 | /* Sets RSPCK, SSL, next-access delay value */ |
| 267 | rspi_write8(rspi, 0x00, RSPI_SPCKD); |
| 268 | rspi_write8(rspi, 0x00, RSPI_SSLND); |
| 269 | rspi_write8(rspi, 0x00, RSPI_SPND); |
| 270 | |
| 271 | /* Sets parity, interrupt mask */ |
| 272 | rspi_write8(rspi, 0x00, RSPI_SPCR2); |
| 273 | |
| 274 | /* Sets SPCMD */ |
Geert Uytterhoeven | 348e515 | 2014-01-12 11:27:43 +0100 | [diff] [blame] | 275 | rspi_write16(rspi, SPCMD_SPB_8_TO_16(access_size) | rspi->spcmd, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 276 | RSPI_SPCMD0); |
| 277 | |
| 278 | /* Sets RSPI mode */ |
| 279 | rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR); |
| 280 | |
| 281 | return 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 282 | } |
| 283 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 284 | /* |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 285 | * functions for RSPI on RZ |
| 286 | */ |
| 287 | static int rspi_rz_set_config_register(struct rspi_data *rspi, int access_size) |
| 288 | { |
| 289 | int spbr; |
| 290 | |
| 291 | /* Sets output mode */ |
| 292 | rspi_write8(rspi, 0x00, RSPI_SPPCR); |
| 293 | |
| 294 | /* Sets transfer bit rate */ |
| 295 | spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz) - 1; |
| 296 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); |
| 297 | |
| 298 | /* Disable dummy transmission, set byte access */ |
| 299 | rspi_write8(rspi, SPDCR_SPLBYTE, RSPI_SPDCR); |
| 300 | rspi->byte_access = 1; |
| 301 | |
| 302 | /* Sets RSPCK, SSL, next-access delay value */ |
| 303 | rspi_write8(rspi, 0x00, RSPI_SPCKD); |
| 304 | rspi_write8(rspi, 0x00, RSPI_SSLND); |
| 305 | rspi_write8(rspi, 0x00, RSPI_SPND); |
| 306 | |
| 307 | /* Sets SPCMD */ |
| 308 | rspi->spcmd |= SPCMD_SPB_8_TO_16(access_size); |
| 309 | rspi_write16(rspi, rspi->spcmd, RSPI_SPCMD0); |
| 310 | |
| 311 | /* Sets RSPI mode */ |
| 312 | rspi_write8(rspi, SPCR_MSTR, RSPI_SPCR); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | /* |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 318 | * functions for QSPI |
| 319 | */ |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 320 | static int qspi_set_config_register(struct rspi_data *rspi, int access_size) |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 321 | { |
| 322 | u16 spcmd; |
| 323 | int spbr; |
| 324 | |
| 325 | /* Sets output mode(CMOS) and MOSI signal(from previous transfer) */ |
| 326 | rspi_write8(rspi, 0x00, RSPI_SPPCR); |
| 327 | |
| 328 | /* Sets transfer bit rate */ |
| 329 | spbr = clk_get_rate(rspi->clk) / (2 * rspi->max_speed_hz); |
| 330 | rspi_write8(rspi, clamp(spbr, 0, 255), RSPI_SPBR); |
| 331 | |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 332 | /* Disable dummy transmission, set byte access */ |
| 333 | rspi_write8(rspi, 0, RSPI_SPDCR); |
| 334 | rspi->byte_access = 1; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 335 | |
| 336 | /* Sets RSPCK, SSL, next-access delay value */ |
| 337 | rspi_write8(rspi, 0x00, RSPI_SPCKD); |
| 338 | rspi_write8(rspi, 0x00, RSPI_SSLND); |
| 339 | rspi_write8(rspi, 0x00, RSPI_SPND); |
| 340 | |
| 341 | /* Data Length Setting */ |
| 342 | if (access_size == 8) |
| 343 | spcmd = SPCMD_SPB_8BIT; |
| 344 | else if (access_size == 16) |
| 345 | spcmd = SPCMD_SPB_16BIT; |
Laurent Pinchart | 8e1c809 | 2013-11-27 01:41:44 +0100 | [diff] [blame] | 346 | else |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 347 | spcmd = SPCMD_SPB_32BIT; |
| 348 | |
Geert Uytterhoeven | 348e515 | 2014-01-12 11:27:43 +0100 | [diff] [blame] | 349 | spcmd |= SPCMD_SCKDEN | SPCMD_SLNDEN | rspi->spcmd | SPCMD_SPNDEN; |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 350 | |
| 351 | /* Resets transfer data length */ |
| 352 | rspi_write32(rspi, 0, QSPI_SPBMUL0); |
| 353 | |
| 354 | /* Resets transmit and receive buffer */ |
| 355 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR); |
| 356 | /* Sets buffer to allow normal operation */ |
| 357 | rspi_write8(rspi, 0x00, QSPI_SPBFCR); |
| 358 | |
| 359 | /* Sets SPCMD */ |
| 360 | rspi_write16(rspi, spcmd, RSPI_SPCMD0); |
| 361 | |
| 362 | /* Enables SPI function in a master mode */ |
| 363 | rspi_write8(rspi, SPCR_SPE | SPCR_MSTR, RSPI_SPCR); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | #define set_config_register(spi, n) spi->ops->set_config_register(spi, n) |
| 369 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 370 | static void rspi_enable_irq(const struct rspi_data *rspi, u8 enable) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 371 | { |
| 372 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | enable, RSPI_SPCR); |
| 373 | } |
| 374 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 375 | static void rspi_disable_irq(const struct rspi_data *rspi, u8 disable) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 376 | { |
| 377 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~disable, RSPI_SPCR); |
| 378 | } |
| 379 | |
| 380 | static int rspi_wait_for_interrupt(struct rspi_data *rspi, u8 wait_mask, |
| 381 | u8 enable_bit) |
| 382 | { |
| 383 | int ret; |
| 384 | |
| 385 | rspi->spsr = rspi_read8(rspi, RSPI_SPSR); |
| 386 | rspi_enable_irq(rspi, enable_bit); |
| 387 | ret = wait_event_timeout(rspi->wait, rspi->spsr & wait_mask, HZ); |
| 388 | if (ret == 0 && !(rspi->spsr & wait_mask)) |
| 389 | return -ETIMEDOUT; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Geert Uytterhoeven | 35301c9 | 2014-01-24 09:43:54 +0100 | [diff] [blame] | 394 | static int rspi_data_out(struct rspi_data *rspi, u8 data) |
| 395 | { |
| 396 | if (rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE) < 0) { |
| 397 | dev_err(&rspi->master->dev, "transmit timeout\n"); |
| 398 | return -ETIMEDOUT; |
| 399 | } |
| 400 | rspi_write_data(rspi, data); |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int rspi_data_in(struct rspi_data *rspi) |
| 405 | { |
| 406 | u8 data; |
| 407 | |
| 408 | if (rspi_wait_for_interrupt(rspi, SPSR_SPRF, SPCR_SPRIE) < 0) { |
| 409 | dev_err(&rspi->master->dev, "receive timeout\n"); |
| 410 | return -ETIMEDOUT; |
| 411 | } |
| 412 | data = rspi_read_data(rspi); |
| 413 | return data; |
| 414 | } |
| 415 | |
| 416 | static int rspi_data_out_in(struct rspi_data *rspi, u8 data) |
| 417 | { |
| 418 | int ret; |
| 419 | |
| 420 | ret = rspi_data_out(rspi, data); |
| 421 | if (ret < 0) |
| 422 | return ret; |
| 423 | |
| 424 | return rspi_data_in(rspi); |
| 425 | } |
| 426 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 427 | static void rspi_dma_complete(void *arg) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 428 | { |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 429 | struct rspi_data *rspi = arg; |
| 430 | |
| 431 | rspi->dma_callbacked = 1; |
| 432 | wake_up_interruptible(&rspi->wait); |
| 433 | } |
| 434 | |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 435 | static int rspi_dma_map_sg(struct scatterlist *sg, const void *buf, |
| 436 | unsigned len, struct dma_chan *chan, |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 437 | enum dma_transfer_direction dir) |
| 438 | { |
| 439 | sg_init_table(sg, 1); |
| 440 | sg_set_buf(sg, buf, len); |
| 441 | sg_dma_len(sg) = len; |
| 442 | return dma_map_sg(chan->device->dev, sg, 1, dir); |
| 443 | } |
| 444 | |
| 445 | static void rspi_dma_unmap_sg(struct scatterlist *sg, struct dma_chan *chan, |
| 446 | enum dma_transfer_direction dir) |
| 447 | { |
| 448 | dma_unmap_sg(chan->device->dev, sg, 1, dir); |
| 449 | } |
| 450 | |
| 451 | static void rspi_memory_to_8bit(void *buf, const void *data, unsigned len) |
| 452 | { |
| 453 | u16 *dst = buf; |
| 454 | const u8 *src = data; |
| 455 | |
| 456 | while (len) { |
| 457 | *dst++ = (u16)(*src++); |
| 458 | len--; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | static void rspi_memory_from_8bit(void *buf, const void *data, unsigned len) |
| 463 | { |
| 464 | u8 *dst = buf; |
| 465 | const u16 *src = data; |
| 466 | |
| 467 | while (len) { |
| 468 | *dst++ = (u8)*src++; |
| 469 | len--; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | static int rspi_send_dma(struct rspi_data *rspi, struct spi_transfer *t) |
| 474 | { |
| 475 | struct scatterlist sg; |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 476 | const void *buf = NULL; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 477 | struct dma_async_tx_descriptor *desc; |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 478 | unsigned int len; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 479 | int ret = 0; |
| 480 | |
| 481 | if (rspi->dma_width_16bit) { |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 482 | void *tmp; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 483 | /* |
| 484 | * If DMAC bus width is 16-bit, the driver allocates a dummy |
| 485 | * buffer. And, the driver converts original data into the |
| 486 | * DMAC data as the following format: |
| 487 | * original data: 1st byte, 2nd byte ... |
| 488 | * DMAC data: 1st byte, dummy, 2nd byte, dummy ... |
| 489 | */ |
| 490 | len = t->len * 2; |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 491 | tmp = kmalloc(len, GFP_KERNEL); |
| 492 | if (!tmp) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 493 | return -ENOMEM; |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 494 | rspi_memory_to_8bit(tmp, t->tx_buf, t->len); |
| 495 | buf = tmp; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 496 | } else { |
| 497 | len = t->len; |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 498 | buf = t->tx_buf; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | if (!rspi_dma_map_sg(&sg, buf, len, rspi->chan_tx, DMA_TO_DEVICE)) { |
| 502 | ret = -EFAULT; |
| 503 | goto end_nomap; |
| 504 | } |
| 505 | desc = dmaengine_prep_slave_sg(rspi->chan_tx, &sg, 1, DMA_TO_DEVICE, |
| 506 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 507 | if (!desc) { |
| 508 | ret = -EIO; |
| 509 | goto end; |
| 510 | } |
| 511 | |
| 512 | /* |
| 513 | * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be |
| 514 | * called. So, this driver disables the IRQ while DMA transfer. |
| 515 | */ |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 516 | disable_irq(rspi->tx_irq); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 517 | |
| 518 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_TXMD, RSPI_SPCR); |
| 519 | rspi_enable_irq(rspi, SPCR_SPTIE); |
| 520 | rspi->dma_callbacked = 0; |
| 521 | |
| 522 | desc->callback = rspi_dma_complete; |
| 523 | desc->callback_param = rspi; |
| 524 | dmaengine_submit(desc); |
| 525 | dma_async_issue_pending(rspi->chan_tx); |
| 526 | |
| 527 | ret = wait_event_interruptible_timeout(rspi->wait, |
| 528 | rspi->dma_callbacked, HZ); |
| 529 | if (ret > 0 && rspi->dma_callbacked) |
| 530 | ret = 0; |
| 531 | else if (!ret) |
| 532 | ret = -ETIMEDOUT; |
| 533 | rspi_disable_irq(rspi, SPCR_SPTIE); |
| 534 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 535 | enable_irq(rspi->tx_irq); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 536 | |
| 537 | end: |
| 538 | rspi_dma_unmap_sg(&sg, rspi->chan_tx, DMA_TO_DEVICE); |
| 539 | end_nomap: |
| 540 | if (rspi->dma_width_16bit) |
| 541 | kfree(buf); |
| 542 | |
| 543 | return ret; |
| 544 | } |
| 545 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 546 | static void rspi_receive_init(const struct rspi_data *rspi) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 547 | { |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 548 | u8 spsr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 549 | |
| 550 | spsr = rspi_read8(rspi, RSPI_SPSR); |
| 551 | if (spsr & SPSR_SPRF) |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 552 | rspi_read_data(rspi); /* dummy read */ |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 553 | if (spsr & SPSR_OVRF) |
| 554 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPSR) & ~SPSR_OVRF, |
Geert Uytterhoeven | df900e6 | 2013-12-23 19:34:24 +0100 | [diff] [blame] | 555 | RSPI_SPSR); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 556 | } |
| 557 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 558 | static void rspi_rz_receive_init(const struct rspi_data *rspi) |
| 559 | { |
| 560 | rspi_receive_init(rspi); |
| 561 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, RSPI_SPBFCR); |
| 562 | rspi_write8(rspi, 0, RSPI_SPBFCR); |
| 563 | } |
| 564 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 565 | static void qspi_receive_init(const struct rspi_data *rspi) |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 566 | { |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 567 | u8 spsr; |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 568 | |
| 569 | spsr = rspi_read8(rspi, RSPI_SPSR); |
| 570 | if (spsr & SPSR_SPRF) |
Geert Uytterhoeven | 74da768 | 2014-01-24 09:43:53 +0100 | [diff] [blame] | 571 | rspi_read_data(rspi); /* dummy read */ |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 572 | rspi_write8(rspi, SPBFCR_TXRST | SPBFCR_RXRST, QSPI_SPBFCR); |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 573 | rspi_write8(rspi, 0, QSPI_SPBFCR); |
Hiep Cao Minh | cb52c67 | 2013-10-10 17:14:03 +0900 | [diff] [blame] | 574 | } |
| 575 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 576 | static int rspi_receive_dma(struct rspi_data *rspi, struct spi_transfer *t) |
| 577 | { |
| 578 | struct scatterlist sg, sg_dummy; |
| 579 | void *dummy = NULL, *rx_buf = NULL; |
| 580 | struct dma_async_tx_descriptor *desc, *desc_dummy; |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 581 | unsigned int len; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 582 | int ret = 0; |
| 583 | |
| 584 | if (rspi->dma_width_16bit) { |
| 585 | /* |
| 586 | * If DMAC bus width is 16-bit, the driver allocates a dummy |
| 587 | * buffer. And, finally the driver converts the DMAC data into |
| 588 | * actual data as the following format: |
| 589 | * DMAC data: 1st byte, dummy, 2nd byte, dummy ... |
| 590 | * actual data: 1st byte, 2nd byte ... |
| 591 | */ |
| 592 | len = t->len * 2; |
| 593 | rx_buf = kmalloc(len, GFP_KERNEL); |
| 594 | if (!rx_buf) |
| 595 | return -ENOMEM; |
| 596 | } else { |
| 597 | len = t->len; |
| 598 | rx_buf = t->rx_buf; |
| 599 | } |
| 600 | |
| 601 | /* prepare dummy transfer to generate SPI clocks */ |
| 602 | dummy = kzalloc(len, GFP_KERNEL); |
| 603 | if (!dummy) { |
| 604 | ret = -ENOMEM; |
| 605 | goto end_nomap; |
| 606 | } |
| 607 | if (!rspi_dma_map_sg(&sg_dummy, dummy, len, rspi->chan_tx, |
| 608 | DMA_TO_DEVICE)) { |
| 609 | ret = -EFAULT; |
| 610 | goto end_nomap; |
| 611 | } |
| 612 | desc_dummy = dmaengine_prep_slave_sg(rspi->chan_tx, &sg_dummy, 1, |
| 613 | DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 614 | if (!desc_dummy) { |
| 615 | ret = -EIO; |
| 616 | goto end_dummy_mapped; |
| 617 | } |
| 618 | |
| 619 | /* prepare receive transfer */ |
| 620 | if (!rspi_dma_map_sg(&sg, rx_buf, len, rspi->chan_rx, |
| 621 | DMA_FROM_DEVICE)) { |
| 622 | ret = -EFAULT; |
| 623 | goto end_dummy_mapped; |
| 624 | |
| 625 | } |
| 626 | desc = dmaengine_prep_slave_sg(rspi->chan_rx, &sg, 1, DMA_FROM_DEVICE, |
| 627 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 628 | if (!desc) { |
| 629 | ret = -EIO; |
| 630 | goto end; |
| 631 | } |
| 632 | |
| 633 | rspi_receive_init(rspi); |
| 634 | |
| 635 | /* |
| 636 | * DMAC needs SPTIE, but if SPTIE is set, this IRQ routine will be |
| 637 | * called. So, this driver disables the IRQ while DMA transfer. |
| 638 | */ |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 639 | disable_irq(rspi->tx_irq); |
| 640 | if (rspi->rx_irq != rspi->tx_irq) |
| 641 | disable_irq(rspi->rx_irq); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 642 | |
| 643 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_TXMD, RSPI_SPCR); |
| 644 | rspi_enable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE); |
| 645 | rspi->dma_callbacked = 0; |
| 646 | |
| 647 | desc->callback = rspi_dma_complete; |
| 648 | desc->callback_param = rspi; |
| 649 | dmaengine_submit(desc); |
| 650 | dma_async_issue_pending(rspi->chan_rx); |
| 651 | |
| 652 | desc_dummy->callback = NULL; /* No callback */ |
| 653 | dmaengine_submit(desc_dummy); |
| 654 | dma_async_issue_pending(rspi->chan_tx); |
| 655 | |
| 656 | ret = wait_event_interruptible_timeout(rspi->wait, |
| 657 | rspi->dma_callbacked, HZ); |
| 658 | if (ret > 0 && rspi->dma_callbacked) |
| 659 | ret = 0; |
| 660 | else if (!ret) |
| 661 | ret = -ETIMEDOUT; |
| 662 | rspi_disable_irq(rspi, SPCR_SPTIE | SPCR_SPRIE); |
| 663 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 664 | enable_irq(rspi->tx_irq); |
| 665 | if (rspi->rx_irq != rspi->tx_irq) |
| 666 | enable_irq(rspi->rx_irq); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 667 | |
| 668 | end: |
| 669 | rspi_dma_unmap_sg(&sg, rspi->chan_rx, DMA_FROM_DEVICE); |
| 670 | end_dummy_mapped: |
| 671 | rspi_dma_unmap_sg(&sg_dummy, rspi->chan_tx, DMA_TO_DEVICE); |
| 672 | end_nomap: |
| 673 | if (rspi->dma_width_16bit) { |
| 674 | if (!ret) |
| 675 | rspi_memory_from_8bit(t->rx_buf, rx_buf, t->len); |
| 676 | kfree(rx_buf); |
| 677 | } |
| 678 | kfree(dummy); |
| 679 | |
| 680 | return ret; |
| 681 | } |
| 682 | |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 683 | static int rspi_is_dma(const struct rspi_data *rspi, struct spi_transfer *t) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 684 | { |
| 685 | if (t->tx_buf && rspi->chan_tx) |
| 686 | return 1; |
| 687 | /* If the module receives data by DMAC, it also needs TX DMAC */ |
| 688 | if (t->rx_buf && rspi->chan_tx && rspi->chan_rx) |
| 689 | return 1; |
| 690 | |
| 691 | return 0; |
| 692 | } |
| 693 | |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 694 | static int rspi_transfer_out_in(struct rspi_data *rspi, |
| 695 | struct spi_transfer *xfer) |
| 696 | { |
| 697 | int remain = xfer->len, ret; |
| 698 | const u8 *tx_buf = xfer->tx_buf; |
| 699 | u8 *rx_buf = xfer->rx_buf; |
| 700 | u8 spcr, data; |
| 701 | |
| 702 | rspi_receive_init(rspi); |
| 703 | |
| 704 | spcr = rspi_read8(rspi, RSPI_SPCR); |
| 705 | if (rx_buf) |
| 706 | spcr &= ~SPCR_TXMD; |
| 707 | else |
| 708 | spcr |= SPCR_TXMD; |
| 709 | rspi_write8(rspi, spcr, RSPI_SPCR); |
| 710 | |
| 711 | while (remain > 0) { |
| 712 | data = tx_buf ? *tx_buf++ : DUMMY_DATA; |
| 713 | ret = rspi_data_out(rspi, data); |
| 714 | if (ret < 0) |
| 715 | return ret; |
| 716 | if (rx_buf) { |
| 717 | ret = rspi_data_in(rspi); |
| 718 | if (ret < 0) |
| 719 | return ret; |
| 720 | *rx_buf++ = ret; |
| 721 | } |
| 722 | remain--; |
| 723 | } |
| 724 | |
| 725 | /* Wait for the last transmission */ |
| 726 | rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE); |
| 727 | |
| 728 | return 0; |
| 729 | } |
| 730 | |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 731 | static int rspi_transfer_one(struct spi_master *master, struct spi_device *spi, |
| 732 | struct spi_transfer *xfer) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 733 | { |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 734 | struct rspi_data *rspi = spi_master_get_devdata(master); |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 735 | int ret; |
| 736 | |
| 737 | if (!rspi_is_dma(rspi, xfer)) |
| 738 | return rspi_transfer_out_in(rspi, xfer); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 739 | |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 740 | if (xfer->tx_buf) { |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 741 | ret = rspi_send_dma(rspi, xfer); |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 742 | if (ret < 0) |
| 743 | return ret; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 744 | } |
Geert Uytterhoeven | 8449fd7 | 2014-01-24 09:43:56 +0100 | [diff] [blame] | 745 | if (xfer->rx_buf) |
| 746 | return rspi_receive_dma(rspi, xfer); |
| 747 | |
| 748 | return 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 749 | } |
| 750 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 751 | static int rspi_rz_transfer_out_in(struct rspi_data *rspi, |
| 752 | struct spi_transfer *xfer) |
| 753 | { |
| 754 | int remain = xfer->len, ret; |
| 755 | const u8 *tx_buf = xfer->tx_buf; |
| 756 | u8 *rx_buf = xfer->rx_buf; |
| 757 | u8 data; |
| 758 | |
| 759 | rspi_rz_receive_init(rspi); |
| 760 | |
| 761 | while (remain > 0) { |
| 762 | data = tx_buf ? *tx_buf++ : DUMMY_DATA; |
| 763 | ret = rspi_data_out_in(rspi, data); |
| 764 | if (ret < 0) |
| 765 | return ret; |
| 766 | if (rx_buf) |
| 767 | *rx_buf++ = ret; |
| 768 | remain--; |
| 769 | } |
| 770 | |
| 771 | /* Wait for the last transmission */ |
| 772 | rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE); |
| 773 | |
| 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | static int rspi_rz_transfer_one(struct spi_master *master, |
| 778 | struct spi_device *spi, |
| 779 | struct spi_transfer *xfer) |
| 780 | { |
| 781 | struct rspi_data *rspi = spi_master_get_devdata(master); |
| 782 | |
| 783 | return rspi_rz_transfer_out_in(rspi, xfer); |
| 784 | } |
| 785 | |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 786 | static int qspi_transfer_out_in(struct rspi_data *rspi, |
| 787 | struct spi_transfer *xfer) |
| 788 | { |
| 789 | int remain = xfer->len, ret; |
| 790 | const u8 *tx_buf = xfer->tx_buf; |
| 791 | u8 *rx_buf = xfer->rx_buf; |
| 792 | u8 data; |
| 793 | |
| 794 | qspi_receive_init(rspi); |
| 795 | |
| 796 | while (remain > 0) { |
| 797 | data = tx_buf ? *tx_buf++ : DUMMY_DATA; |
| 798 | ret = rspi_data_out_in(rspi, data); |
| 799 | if (ret < 0) |
| 800 | return ret; |
| 801 | if (rx_buf) |
| 802 | *rx_buf++ = ret; |
| 803 | remain--; |
| 804 | } |
| 805 | |
| 806 | /* Wait for the last transmission */ |
| 807 | rspi_wait_for_interrupt(rspi, SPSR_SPTEF, SPCR_SPTIE); |
| 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 812 | static int qspi_transfer_one(struct spi_master *master, struct spi_device *spi, |
| 813 | struct spi_transfer *xfer) |
| 814 | { |
| 815 | struct rspi_data *rspi = spi_master_get_devdata(master); |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 816 | |
Geert Uytterhoeven | 340a15e | 2014-01-24 09:43:57 +0100 | [diff] [blame] | 817 | return qspi_transfer_out_in(rspi, xfer); |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 820 | static int rspi_setup(struct spi_device *spi) |
| 821 | { |
| 822 | struct rspi_data *rspi = spi_master_get_devdata(spi->master); |
| 823 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 824 | rspi->max_speed_hz = spi->max_speed_hz; |
| 825 | |
Geert Uytterhoeven | 348e515 | 2014-01-12 11:27:43 +0100 | [diff] [blame] | 826 | rspi->spcmd = SPCMD_SSLKP; |
| 827 | if (spi->mode & SPI_CPOL) |
| 828 | rspi->spcmd |= SPCMD_CPOL; |
| 829 | if (spi->mode & SPI_CPHA) |
| 830 | rspi->spcmd |= SPCMD_CPHA; |
| 831 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 832 | set_config_register(rspi, 8); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 833 | |
| 834 | return 0; |
| 835 | } |
| 836 | |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 837 | static void rspi_cleanup(struct spi_device *spi) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 838 | { |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 839 | } |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 840 | |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 841 | static int rspi_prepare_message(struct spi_master *master, |
| 842 | struct spi_message *message) |
| 843 | { |
| 844 | struct rspi_data *rspi = spi_master_get_devdata(master); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 845 | |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 846 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) | SPCR_SPE, RSPI_SPCR); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 850 | static int rspi_unprepare_message(struct spi_master *master, |
| 851 | struct spi_message *message) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 852 | { |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 853 | struct rspi_data *rspi = spi_master_get_devdata(master); |
| 854 | |
| 855 | rspi_write8(rspi, rspi_read8(rspi, RSPI_SPCR) & ~SPCR_SPE, RSPI_SPCR); |
| 856 | return 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 857 | } |
| 858 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 859 | static irqreturn_t rspi_irq_mux(int irq, void *_sr) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 860 | { |
Geert Uytterhoeven | c132f09 | 2013-12-24 10:49:31 +0100 | [diff] [blame] | 861 | struct rspi_data *rspi = _sr; |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 862 | u8 spsr; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 863 | irqreturn_t ret = IRQ_NONE; |
Geert Uytterhoeven | 97b95c1 | 2013-12-24 10:49:34 +0100 | [diff] [blame] | 864 | u8 disable_irq = 0; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 865 | |
| 866 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); |
| 867 | if (spsr & SPSR_SPRF) |
| 868 | disable_irq |= SPCR_SPRIE; |
| 869 | if (spsr & SPSR_SPTEF) |
| 870 | disable_irq |= SPCR_SPTIE; |
| 871 | |
| 872 | if (disable_irq) { |
| 873 | ret = IRQ_HANDLED; |
| 874 | rspi_disable_irq(rspi, disable_irq); |
| 875 | wake_up(&rspi->wait); |
| 876 | } |
| 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 881 | static irqreturn_t rspi_irq_rx(int irq, void *_sr) |
| 882 | { |
| 883 | struct rspi_data *rspi = _sr; |
| 884 | u8 spsr; |
| 885 | |
| 886 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); |
| 887 | if (spsr & SPSR_SPRF) { |
| 888 | rspi_disable_irq(rspi, SPCR_SPRIE); |
| 889 | wake_up(&rspi->wait); |
| 890 | return IRQ_HANDLED; |
| 891 | } |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static irqreturn_t rspi_irq_tx(int irq, void *_sr) |
| 897 | { |
| 898 | struct rspi_data *rspi = _sr; |
| 899 | u8 spsr; |
| 900 | |
| 901 | rspi->spsr = spsr = rspi_read8(rspi, RSPI_SPSR); |
| 902 | if (spsr & SPSR_SPTEF) { |
| 903 | rspi_disable_irq(rspi, SPCR_SPTIE); |
| 904 | wake_up(&rspi->wait); |
| 905 | return IRQ_HANDLED; |
| 906 | } |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 911 | static int rspi_request_dma(struct rspi_data *rspi, |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 912 | struct platform_device *pdev) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 913 | { |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 914 | const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev); |
Guennadi Liakhovetski | e2b0509 | 2013-08-02 15:03:42 +0200 | [diff] [blame] | 915 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 916 | dma_cap_mask_t mask; |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 917 | struct dma_slave_config cfg; |
| 918 | int ret; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 919 | |
Guennadi Liakhovetski | e2b0509 | 2013-08-02 15:03:42 +0200 | [diff] [blame] | 920 | if (!res || !rspi_pd) |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 921 | return 0; /* The driver assumes no error. */ |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 922 | |
| 923 | rspi->dma_width_16bit = rspi_pd->dma_width_16bit; |
| 924 | |
| 925 | /* If the module receives data by DMAC, it also needs TX DMAC */ |
| 926 | if (rspi_pd->dma_rx_id && rspi_pd->dma_tx_id) { |
| 927 | dma_cap_zero(mask); |
| 928 | dma_cap_set(DMA_SLAVE, mask); |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 929 | rspi->chan_rx = dma_request_channel(mask, shdma_chan_filter, |
| 930 | (void *)rspi_pd->dma_rx_id); |
| 931 | if (rspi->chan_rx) { |
| 932 | cfg.slave_id = rspi_pd->dma_rx_id; |
| 933 | cfg.direction = DMA_DEV_TO_MEM; |
Guennadi Liakhovetski | e2b0509 | 2013-08-02 15:03:42 +0200 | [diff] [blame] | 934 | cfg.dst_addr = 0; |
| 935 | cfg.src_addr = res->start + RSPI_SPDR; |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 936 | ret = dmaengine_slave_config(rspi->chan_rx, &cfg); |
| 937 | if (!ret) |
| 938 | dev_info(&pdev->dev, "Use DMA when rx.\n"); |
| 939 | else |
| 940 | return ret; |
| 941 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 942 | } |
| 943 | if (rspi_pd->dma_tx_id) { |
| 944 | dma_cap_zero(mask); |
| 945 | dma_cap_set(DMA_SLAVE, mask); |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 946 | rspi->chan_tx = dma_request_channel(mask, shdma_chan_filter, |
| 947 | (void *)rspi_pd->dma_tx_id); |
| 948 | if (rspi->chan_tx) { |
| 949 | cfg.slave_id = rspi_pd->dma_tx_id; |
| 950 | cfg.direction = DMA_MEM_TO_DEV; |
Guennadi Liakhovetski | e2b0509 | 2013-08-02 15:03:42 +0200 | [diff] [blame] | 951 | cfg.dst_addr = res->start + RSPI_SPDR; |
| 952 | cfg.src_addr = 0; |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 953 | ret = dmaengine_slave_config(rspi->chan_tx, &cfg); |
| 954 | if (!ret) |
| 955 | dev_info(&pdev->dev, "Use DMA when tx\n"); |
| 956 | else |
| 957 | return ret; |
| 958 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 959 | } |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 960 | |
| 961 | return 0; |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 962 | } |
| 963 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 964 | static void rspi_release_dma(struct rspi_data *rspi) |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 965 | { |
| 966 | if (rspi->chan_tx) |
| 967 | dma_release_channel(rspi->chan_tx); |
| 968 | if (rspi->chan_rx) |
| 969 | dma_release_channel(rspi->chan_rx); |
| 970 | } |
| 971 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 972 | static int rspi_remove(struct platform_device *pdev) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 973 | { |
Laurent Pinchart | 5ffbe2d | 2013-11-27 01:41:45 +0100 | [diff] [blame] | 974 | struct rspi_data *rspi = platform_get_drvdata(pdev); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 975 | |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 976 | rspi_release_dma(rspi); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 977 | clk_disable(rspi->clk); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 982 | static int rspi_request_irq(struct device *dev, unsigned int irq, |
| 983 | irq_handler_t handler, const char *suffix, |
| 984 | void *dev_id) |
| 985 | { |
| 986 | const char *base = dev_name(dev); |
| 987 | size_t len = strlen(base) + strlen(suffix) + 2; |
| 988 | char *name = devm_kzalloc(dev, len, GFP_KERNEL); |
| 989 | if (!name) |
| 990 | return -ENOMEM; |
| 991 | snprintf(name, len, "%s:%s", base, suffix); |
| 992 | return devm_request_irq(dev, irq, handler, 0, name, dev_id); |
| 993 | } |
| 994 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 995 | static int rspi_probe(struct platform_device *pdev) |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 996 | { |
| 997 | struct resource *res; |
| 998 | struct spi_master *master; |
| 999 | struct rspi_data *rspi; |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1000 | int ret; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1001 | char clk_name[16]; |
Geert Uytterhoeven | baf588f | 2013-12-24 10:49:32 +0100 | [diff] [blame] | 1002 | const struct rspi_plat_data *rspi_pd = dev_get_platdata(&pdev->dev); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1003 | const struct spi_ops *ops; |
| 1004 | const struct platform_device_id *id_entry = pdev->id_entry; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1005 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1006 | ops = (struct spi_ops *)id_entry->driver_data; |
| 1007 | /* ops parameter check */ |
| 1008 | if (!ops->set_config_register) { |
| 1009 | dev_err(&pdev->dev, "there is no set_config_register\n"); |
| 1010 | return -ENODEV; |
| 1011 | } |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1012 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1013 | master = spi_alloc_master(&pdev->dev, sizeof(struct rspi_data)); |
| 1014 | if (master == NULL) { |
| 1015 | dev_err(&pdev->dev, "spi_alloc_master error.\n"); |
| 1016 | return -ENOMEM; |
| 1017 | } |
| 1018 | |
| 1019 | rspi = spi_master_get_devdata(master); |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 1020 | platform_set_drvdata(pdev, rspi); |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1021 | rspi->ops = ops; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1022 | rspi->master = master; |
Laurent Pinchart | 5d79e9a | 2013-11-27 01:41:46 +0100 | [diff] [blame] | 1023 | |
| 1024 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1025 | rspi->addr = devm_ioremap_resource(&pdev->dev, res); |
| 1026 | if (IS_ERR(rspi->addr)) { |
| 1027 | ret = PTR_ERR(rspi->addr); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1028 | goto error1; |
| 1029 | } |
| 1030 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1031 | snprintf(clk_name, sizeof(clk_name), "%s%d", id_entry->name, pdev->id); |
Laurent Pinchart | 5d79e9a | 2013-11-27 01:41:46 +0100 | [diff] [blame] | 1032 | rspi->clk = devm_clk_get(&pdev->dev, clk_name); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1033 | if (IS_ERR(rspi->clk)) { |
| 1034 | dev_err(&pdev->dev, "cannot get clock\n"); |
| 1035 | ret = PTR_ERR(rspi->clk); |
Laurent Pinchart | 5d79e9a | 2013-11-27 01:41:46 +0100 | [diff] [blame] | 1036 | goto error1; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1037 | } |
| 1038 | clk_enable(rspi->clk); |
| 1039 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1040 | init_waitqueue_head(&rspi->wait); |
| 1041 | |
Geert Uytterhoeven | efd85ac | 2013-12-23 19:34:23 +0100 | [diff] [blame] | 1042 | if (rspi_pd && rspi_pd->num_chipselect) |
| 1043 | master->num_chipselect = rspi_pd->num_chipselect; |
| 1044 | else |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1045 | master->num_chipselect = 2; /* default */ |
| 1046 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1047 | master->bus_num = pdev->id; |
| 1048 | master->setup = rspi_setup; |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 1049 | master->transfer_one = ops->transfer_one; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1050 | master->cleanup = rspi_cleanup; |
Geert Uytterhoeven | 79d2349 | 2014-01-24 09:43:52 +0100 | [diff] [blame] | 1051 | master->prepare_message = rspi_prepare_message; |
| 1052 | master->unprepare_message = rspi_unprepare_message; |
Geert Uytterhoeven | 348e515 | 2014-01-12 11:27:43 +0100 | [diff] [blame] | 1053 | master->mode_bits = SPI_CPHA | SPI_CPOL; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1054 | |
Geert Uytterhoeven | 9372220 | 2014-01-24 09:43:58 +0100 | [diff] [blame] | 1055 | ret = platform_get_irq_byname(pdev, "rx"); |
| 1056 | if (ret < 0) { |
| 1057 | ret = platform_get_irq_byname(pdev, "mux"); |
| 1058 | if (ret < 0) |
| 1059 | ret = platform_get_irq(pdev, 0); |
| 1060 | if (ret >= 0) |
| 1061 | rspi->rx_irq = rspi->tx_irq = ret; |
| 1062 | } else { |
| 1063 | rspi->rx_irq = ret; |
| 1064 | ret = platform_get_irq_byname(pdev, "tx"); |
| 1065 | if (ret >= 0) |
| 1066 | rspi->tx_irq = ret; |
| 1067 | } |
| 1068 | if (ret < 0) { |
| 1069 | dev_err(&pdev->dev, "platform_get_irq error\n"); |
| 1070 | goto error2; |
| 1071 | } |
| 1072 | |
| 1073 | if (rspi->rx_irq == rspi->tx_irq) { |
| 1074 | /* Single multiplexed interrupt */ |
| 1075 | ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_mux, |
| 1076 | "mux", rspi); |
| 1077 | } else { |
| 1078 | /* Multi-interrupt mode, only SPRI and SPTI are used */ |
| 1079 | ret = rspi_request_irq(&pdev->dev, rspi->rx_irq, rspi_irq_rx, |
| 1080 | "rx", rspi); |
| 1081 | if (!ret) |
| 1082 | ret = rspi_request_irq(&pdev->dev, rspi->tx_irq, |
| 1083 | rspi_irq_tx, "tx", rspi); |
| 1084 | } |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1085 | if (ret < 0) { |
| 1086 | dev_err(&pdev->dev, "request_irq error\n"); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1087 | goto error2; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1088 | } |
| 1089 | |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 1090 | ret = rspi_request_dma(rspi, pdev); |
| 1091 | if (ret < 0) { |
| 1092 | dev_err(&pdev->dev, "rspi_request_dma failed.\n"); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1093 | goto error3; |
Shimoda, Yoshihiro | 0243c536 | 2012-08-02 17:17:33 +0900 | [diff] [blame] | 1094 | } |
Shimoda, Yoshihiro | a3633fe | 2012-04-20 14:50:36 +0900 | [diff] [blame] | 1095 | |
Jingoo Han | 9e03d05 | 2013-12-04 14:13:50 +0900 | [diff] [blame] | 1096 | ret = devm_spi_register_master(&pdev->dev, master); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1097 | if (ret < 0) { |
| 1098 | dev_err(&pdev->dev, "spi_register_master error.\n"); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1099 | goto error3; |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | dev_info(&pdev->dev, "probed\n"); |
| 1103 | |
| 1104 | return 0; |
| 1105 | |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1106 | error3: |
Laurent Pinchart | 5d79e9a | 2013-11-27 01:41:46 +0100 | [diff] [blame] | 1107 | rspi_release_dma(rspi); |
Geert Uytterhoeven | fcb4ed7 | 2014-01-14 10:20:33 +0100 | [diff] [blame] | 1108 | error2: |
| 1109 | clk_disable(rspi->clk); |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1110 | error1: |
| 1111 | spi_master_put(master); |
| 1112 | |
| 1113 | return ret; |
| 1114 | } |
| 1115 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1116 | static struct spi_ops rspi_ops = { |
| 1117 | .set_config_register = rspi_set_config_register, |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 1118 | .transfer_one = rspi_transfer_one, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1119 | }; |
| 1120 | |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 1121 | static struct spi_ops rspi_rz_ops = { |
| 1122 | .set_config_register = rspi_rz_set_config_register, |
| 1123 | .transfer_one = rspi_rz_transfer_one, |
| 1124 | }; |
| 1125 | |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1126 | static struct spi_ops qspi_ops = { |
| 1127 | .set_config_register = qspi_set_config_register, |
Geert Uytterhoeven | eb557f7 | 2014-01-24 09:43:55 +0100 | [diff] [blame] | 1128 | .transfer_one = qspi_transfer_one, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1129 | }; |
| 1130 | |
| 1131 | static struct platform_device_id spi_driver_ids[] = { |
| 1132 | { "rspi", (kernel_ulong_t)&rspi_ops }, |
Geert Uytterhoeven | 862d357 | 2014-01-24 09:43:59 +0100 | [diff] [blame^] | 1133 | { "rspi-rz", (kernel_ulong_t)&rspi_rz_ops }, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1134 | { "qspi", (kernel_ulong_t)&qspi_ops }, |
| 1135 | {}, |
| 1136 | }; |
| 1137 | |
| 1138 | MODULE_DEVICE_TABLE(platform, spi_driver_ids); |
| 1139 | |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1140 | static struct platform_driver rspi_driver = { |
| 1141 | .probe = rspi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1142 | .remove = rspi_remove, |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1143 | .id_table = spi_driver_ids, |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1144 | .driver = { |
Hiep Cao Minh | 5ce0ba8 | 2013-09-03 13:10:26 +0900 | [diff] [blame] | 1145 | .name = "renesas_spi", |
Shimoda, Yoshihiro | 0b2182d | 2012-03-07 14:46:25 +0900 | [diff] [blame] | 1146 | .owner = THIS_MODULE, |
| 1147 | }, |
| 1148 | }; |
| 1149 | module_platform_driver(rspi_driver); |
| 1150 | |
| 1151 | MODULE_DESCRIPTION("Renesas RSPI bus driver"); |
| 1152 | MODULE_LICENSE("GPL v2"); |
| 1153 | MODULE_AUTHOR("Yoshihiro Shimoda"); |
| 1154 | MODULE_ALIAS("platform:rspi"); |