Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Driver for Cirrus Logic EP93xx SPI controller. |
| 3 | * |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 4 | * Copyright (C) 2010-2011 Mika Westerberg |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 5 | * |
| 6 | * Explicit FIFO handling code was inspired by amba-pl022 driver. |
| 7 | * |
| 8 | * Chip select support using other than built-in GPIOs by H. Hartley Sweeten. |
| 9 | * |
| 10 | * For more information about the SPI controller see documentation on Cirrus |
| 11 | * Logic web site: |
| 12 | * http://www.cirrus.com/en/pubs/manual/EP93xx_Users_Guide_UM1.pdf |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License version 2 as |
| 16 | * published by the Free Software Foundation. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/io.h> |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/device.h> |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 24 | #include <linux/dmaengine.h> |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 25 | #include <linux/bitops.h> |
| 26 | #include <linux/interrupt.h> |
Mika Westerberg | 5bdb7613 | 2011-10-15 21:40:09 +0300 | [diff] [blame] | 27 | #include <linux/module.h> |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 29 | #include <linux/sched.h> |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 30 | #include <linux/scatterlist.h> |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 31 | #include <linux/gpio.h> |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 32 | #include <linux/spi/spi.h> |
| 33 | |
Arnd Bergmann | a3b29245 | 2012-08-24 15:12:11 +0200 | [diff] [blame] | 34 | #include <linux/platform_data/dma-ep93xx.h> |
| 35 | #include <linux/platform_data/spi-ep93xx.h> |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 36 | |
| 37 | #define SSPCR0 0x0000 |
| 38 | #define SSPCR0_MODE_SHIFT 6 |
| 39 | #define SSPCR0_SCR_SHIFT 8 |
| 40 | |
| 41 | #define SSPCR1 0x0004 |
| 42 | #define SSPCR1_RIE BIT(0) |
| 43 | #define SSPCR1_TIE BIT(1) |
| 44 | #define SSPCR1_RORIE BIT(2) |
| 45 | #define SSPCR1_LBM BIT(3) |
| 46 | #define SSPCR1_SSE BIT(4) |
| 47 | #define SSPCR1_MS BIT(5) |
| 48 | #define SSPCR1_SOD BIT(6) |
| 49 | |
| 50 | #define SSPDR 0x0008 |
| 51 | |
| 52 | #define SSPSR 0x000c |
| 53 | #define SSPSR_TFE BIT(0) |
| 54 | #define SSPSR_TNF BIT(1) |
| 55 | #define SSPSR_RNE BIT(2) |
| 56 | #define SSPSR_RFF BIT(3) |
| 57 | #define SSPSR_BSY BIT(4) |
| 58 | #define SSPCPSR 0x0010 |
| 59 | |
| 60 | #define SSPIIR 0x0014 |
| 61 | #define SSPIIR_RIS BIT(0) |
| 62 | #define SSPIIR_TIS BIT(1) |
| 63 | #define SSPIIR_RORIS BIT(2) |
| 64 | #define SSPICR SSPIIR |
| 65 | |
| 66 | /* timeout in milliseconds */ |
| 67 | #define SPI_TIMEOUT 5 |
| 68 | /* maximum depth of RX/TX FIFO */ |
| 69 | #define SPI_FIFO_SIZE 8 |
| 70 | |
| 71 | /** |
| 72 | * struct ep93xx_spi - EP93xx SPI controller structure |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 73 | * @pdev: pointer to platform device |
| 74 | * @clk: clock for the controller |
| 75 | * @regs_base: pointer to ioremap()'d registers |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 76 | * @sspdr_phys: physical address of the SSPDR register |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 77 | * @wait: wait here until given transfer is completed |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 78 | * @current_msg: message that is currently processed (or %NULL if none) |
| 79 | * @tx: current byte in transfer to transmit |
| 80 | * @rx: current byte in transfer to receive |
| 81 | * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one |
| 82 | * frame decreases this level and sending one frame increases it. |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 83 | * @dma_rx: RX DMA channel |
| 84 | * @dma_tx: TX DMA channel |
| 85 | * @dma_rx_data: RX parameters passed to the DMA engine |
| 86 | * @dma_tx_data: TX parameters passed to the DMA engine |
| 87 | * @rx_sgt: sg table for RX transfers |
| 88 | * @tx_sgt: sg table for TX transfers |
| 89 | * @zeropage: dummy page used as RX buffer when only TX buffer is passed in by |
| 90 | * the client |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 91 | */ |
| 92 | struct ep93xx_spi { |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 93 | const struct platform_device *pdev; |
| 94 | struct clk *clk; |
| 95 | void __iomem *regs_base; |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 96 | unsigned long sspdr_phys; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 97 | struct completion wait; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 98 | struct spi_message *current_msg; |
| 99 | size_t tx; |
| 100 | size_t rx; |
| 101 | size_t fifo_level; |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 102 | struct dma_chan *dma_rx; |
| 103 | struct dma_chan *dma_tx; |
| 104 | struct ep93xx_dma_data dma_rx_data; |
| 105 | struct ep93xx_dma_data dma_tx_data; |
| 106 | struct sg_table rx_sgt; |
| 107 | struct sg_table tx_sgt; |
| 108 | void *zeropage; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 111 | /* converts bits per word to CR0.DSS value */ |
| 112 | #define bits_per_word_to_dss(bpw) ((bpw) - 1) |
| 113 | |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 114 | static void ep93xx_spi_write_u8(const struct ep93xx_spi *espi, |
| 115 | u16 reg, u8 value) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 116 | { |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 117 | writeb(value, espi->regs_base + reg); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 118 | } |
| 119 | |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 120 | static u8 ep93xx_spi_read_u8(const struct ep93xx_spi *spi, u16 reg) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 121 | { |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 122 | return readb(spi->regs_base + reg); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 123 | } |
| 124 | |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 125 | static void ep93xx_spi_write_u16(const struct ep93xx_spi *espi, |
| 126 | u16 reg, u16 value) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 127 | { |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 128 | writew(value, espi->regs_base + reg); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 129 | } |
| 130 | |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 131 | static u16 ep93xx_spi_read_u16(const struct ep93xx_spi *spi, u16 reg) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 132 | { |
H Hartley Sweeten | 8d7586b | 2013-07-02 10:06:26 -0700 | [diff] [blame] | 133 | return readw(spi->regs_base + reg); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | static int ep93xx_spi_enable(const struct ep93xx_spi *espi) |
| 137 | { |
| 138 | u8 regval; |
| 139 | int err; |
| 140 | |
| 141 | err = clk_enable(espi->clk); |
| 142 | if (err) |
| 143 | return err; |
| 144 | |
| 145 | regval = ep93xx_spi_read_u8(espi, SSPCR1); |
| 146 | regval |= SSPCR1_SSE; |
| 147 | ep93xx_spi_write_u8(espi, SSPCR1, regval); |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | static void ep93xx_spi_disable(const struct ep93xx_spi *espi) |
| 153 | { |
| 154 | u8 regval; |
| 155 | |
| 156 | regval = ep93xx_spi_read_u8(espi, SSPCR1); |
| 157 | regval &= ~SSPCR1_SSE; |
| 158 | ep93xx_spi_write_u8(espi, SSPCR1, regval); |
| 159 | |
| 160 | clk_disable(espi->clk); |
| 161 | } |
| 162 | |
| 163 | static void ep93xx_spi_enable_interrupts(const struct ep93xx_spi *espi) |
| 164 | { |
| 165 | u8 regval; |
| 166 | |
| 167 | regval = ep93xx_spi_read_u8(espi, SSPCR1); |
| 168 | regval |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE); |
| 169 | ep93xx_spi_write_u8(espi, SSPCR1, regval); |
| 170 | } |
| 171 | |
| 172 | static void ep93xx_spi_disable_interrupts(const struct ep93xx_spi *espi) |
| 173 | { |
| 174 | u8 regval; |
| 175 | |
| 176 | regval = ep93xx_spi_read_u8(espi, SSPCR1); |
| 177 | regval &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE); |
| 178 | ep93xx_spi_write_u8(espi, SSPCR1, regval); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * ep93xx_spi_calc_divisors() - calculates SPI clock divisors |
| 183 | * @espi: ep93xx SPI controller struct |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 184 | * @rate: desired SPI output clock rate |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 185 | * @div_cpsr: pointer to return the cpsr (pre-scaler) divider |
| 186 | * @div_scr: pointer to return the scr divider |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 187 | */ |
| 188 | static int ep93xx_spi_calc_divisors(const struct ep93xx_spi *espi, |
Axel Lin | 56fc0b4 | 2014-02-08 23:52:26 +0800 | [diff] [blame] | 189 | u32 rate, u8 *div_cpsr, u8 *div_scr) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 190 | { |
Axel Lin | 56fc0b4 | 2014-02-08 23:52:26 +0800 | [diff] [blame] | 191 | struct spi_master *master = platform_get_drvdata(espi->pdev); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 192 | unsigned long spi_clk_rate = clk_get_rate(espi->clk); |
| 193 | int cpsr, scr; |
| 194 | |
| 195 | /* |
| 196 | * Make sure that max value is between values supported by the |
| 197 | * controller. Note that minimum value is already checked in |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 198 | * ep93xx_spi_transfer_one_message(). |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 199 | */ |
Axel Lin | 56fc0b4 | 2014-02-08 23:52:26 +0800 | [diff] [blame] | 200 | rate = clamp(rate, master->min_speed_hz, master->max_speed_hz); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | * Calculate divisors so that we can get speed according the |
| 204 | * following formula: |
| 205 | * rate = spi_clock_rate / (cpsr * (1 + scr)) |
| 206 | * |
| 207 | * cpsr must be even number and starts from 2, scr can be any number |
| 208 | * between 0 and 255. |
| 209 | */ |
| 210 | for (cpsr = 2; cpsr <= 254; cpsr += 2) { |
| 211 | for (scr = 0; scr <= 255; scr++) { |
| 212 | if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) { |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 213 | *div_scr = (u8)scr; |
| 214 | *div_cpsr = (u8)cpsr; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | return -EINVAL; |
| 221 | } |
| 222 | |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 223 | static void ep93xx_spi_cs_control(struct spi_device *spi, bool enable) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 224 | { |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 225 | if (spi->mode & SPI_CS_HIGH) |
| 226 | enable = !enable; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 227 | |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 228 | if (gpio_is_valid(spi->cs_gpio)) |
| 229 | gpio_set_value(spi->cs_gpio, !enable); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 230 | } |
| 231 | |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 232 | static int ep93xx_spi_chip_setup(const struct ep93xx_spi *espi, |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 233 | struct spi_device *spi, |
| 234 | struct spi_transfer *xfer) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 235 | { |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 236 | u8 dss = bits_per_word_to_dss(xfer->bits_per_word); |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 237 | u8 div_cpsr = 0; |
| 238 | u8 div_scr = 0; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 239 | u16 cr0; |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 240 | int err; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 241 | |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 242 | err = ep93xx_spi_calc_divisors(espi, xfer->speed_hz, |
| 243 | &div_cpsr, &div_scr); |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 244 | if (err) |
| 245 | return err; |
| 246 | |
| 247 | cr0 = div_scr << SSPCR0_SCR_SHIFT; |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 248 | cr0 |= (spi->mode & (SPI_CPHA | SPI_CPOL)) << SSPCR0_MODE_SHIFT; |
H Hartley Sweeten | d9b65df | 2013-07-02 10:09:29 -0700 | [diff] [blame] | 249 | cr0 |= dss; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 250 | |
| 251 | dev_dbg(&espi->pdev->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n", |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 252 | spi->mode, div_cpsr, div_scr, dss); |
Jarkko Nikula | a1829d2 | 2013-10-11 13:53:59 +0300 | [diff] [blame] | 253 | dev_dbg(&espi->pdev->dev, "setup: cr0 %#x\n", cr0); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 254 | |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 255 | ep93xx_spi_write_u8(espi, SSPCPSR, div_cpsr); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 256 | ep93xx_spi_write_u16(espi, SSPCR0, cr0); |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 257 | |
| 258 | return 0; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 261 | static void ep93xx_do_write(struct ep93xx_spi *espi, struct spi_transfer *t) |
| 262 | { |
H Hartley Sweeten | 701c358 | 2013-07-02 10:07:01 -0700 | [diff] [blame] | 263 | if (t->bits_per_word > 8) { |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 264 | u16 tx_val = 0; |
| 265 | |
| 266 | if (t->tx_buf) |
| 267 | tx_val = ((u16 *)t->tx_buf)[espi->tx]; |
| 268 | ep93xx_spi_write_u16(espi, SSPDR, tx_val); |
| 269 | espi->tx += sizeof(tx_val); |
| 270 | } else { |
| 271 | u8 tx_val = 0; |
| 272 | |
| 273 | if (t->tx_buf) |
| 274 | tx_val = ((u8 *)t->tx_buf)[espi->tx]; |
| 275 | ep93xx_spi_write_u8(espi, SSPDR, tx_val); |
| 276 | espi->tx += sizeof(tx_val); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | static void ep93xx_do_read(struct ep93xx_spi *espi, struct spi_transfer *t) |
| 281 | { |
H Hartley Sweeten | 701c358 | 2013-07-02 10:07:01 -0700 | [diff] [blame] | 282 | if (t->bits_per_word > 8) { |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 283 | u16 rx_val; |
| 284 | |
| 285 | rx_val = ep93xx_spi_read_u16(espi, SSPDR); |
| 286 | if (t->rx_buf) |
| 287 | ((u16 *)t->rx_buf)[espi->rx] = rx_val; |
| 288 | espi->rx += sizeof(rx_val); |
| 289 | } else { |
| 290 | u8 rx_val; |
| 291 | |
| 292 | rx_val = ep93xx_spi_read_u8(espi, SSPDR); |
| 293 | if (t->rx_buf) |
| 294 | ((u8 *)t->rx_buf)[espi->rx] = rx_val; |
| 295 | espi->rx += sizeof(rx_val); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * ep93xx_spi_read_write() - perform next RX/TX transfer |
| 301 | * @espi: ep93xx SPI controller struct |
| 302 | * |
| 303 | * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If |
| 304 | * called several times, the whole transfer will be completed. Returns |
| 305 | * %-EINPROGRESS when current transfer was not yet completed otherwise %0. |
| 306 | * |
| 307 | * When this function is finished, RX FIFO should be empty and TX FIFO should be |
| 308 | * full. |
| 309 | */ |
| 310 | static int ep93xx_spi_read_write(struct ep93xx_spi *espi) |
| 311 | { |
| 312 | struct spi_message *msg = espi->current_msg; |
| 313 | struct spi_transfer *t = msg->state; |
| 314 | |
| 315 | /* read as long as RX FIFO has frames in it */ |
| 316 | while ((ep93xx_spi_read_u8(espi, SSPSR) & SSPSR_RNE)) { |
| 317 | ep93xx_do_read(espi, t); |
| 318 | espi->fifo_level--; |
| 319 | } |
| 320 | |
| 321 | /* write as long as TX FIFO has room */ |
| 322 | while (espi->fifo_level < SPI_FIFO_SIZE && espi->tx < t->len) { |
| 323 | ep93xx_do_write(espi, t); |
| 324 | espi->fifo_level++; |
| 325 | } |
| 326 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 327 | if (espi->rx == t->len) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 328 | return 0; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 329 | |
| 330 | return -EINPROGRESS; |
| 331 | } |
| 332 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 333 | static void ep93xx_spi_pio_transfer(struct ep93xx_spi *espi) |
| 334 | { |
| 335 | /* |
| 336 | * Now everything is set up for the current transfer. We prime the TX |
| 337 | * FIFO, enable interrupts, and wait for the transfer to complete. |
| 338 | */ |
| 339 | if (ep93xx_spi_read_write(espi)) { |
| 340 | ep93xx_spi_enable_interrupts(espi); |
| 341 | wait_for_completion(&espi->wait); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * ep93xx_spi_dma_prepare() - prepares a DMA transfer |
| 347 | * @espi: ep93xx SPI controller struct |
| 348 | * @dir: DMA transfer direction |
| 349 | * |
| 350 | * Function configures the DMA, maps the buffer and prepares the DMA |
| 351 | * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR |
| 352 | * in case of failure. |
| 353 | */ |
| 354 | static struct dma_async_tx_descriptor * |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 355 | ep93xx_spi_dma_prepare(struct ep93xx_spi *espi, enum dma_transfer_direction dir) |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 356 | { |
| 357 | struct spi_transfer *t = espi->current_msg->state; |
| 358 | struct dma_async_tx_descriptor *txd; |
| 359 | enum dma_slave_buswidth buswidth; |
| 360 | struct dma_slave_config conf; |
| 361 | struct scatterlist *sg; |
| 362 | struct sg_table *sgt; |
| 363 | struct dma_chan *chan; |
| 364 | const void *buf, *pbuf; |
| 365 | size_t len = t->len; |
| 366 | int i, ret, nents; |
| 367 | |
H Hartley Sweeten | 701c358 | 2013-07-02 10:07:01 -0700 | [diff] [blame] | 368 | if (t->bits_per_word > 8) |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 369 | buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 370 | else |
| 371 | buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 372 | |
| 373 | memset(&conf, 0, sizeof(conf)); |
| 374 | conf.direction = dir; |
| 375 | |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 376 | if (dir == DMA_DEV_TO_MEM) { |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 377 | chan = espi->dma_rx; |
| 378 | buf = t->rx_buf; |
| 379 | sgt = &espi->rx_sgt; |
| 380 | |
| 381 | conf.src_addr = espi->sspdr_phys; |
| 382 | conf.src_addr_width = buswidth; |
| 383 | } else { |
| 384 | chan = espi->dma_tx; |
| 385 | buf = t->tx_buf; |
| 386 | sgt = &espi->tx_sgt; |
| 387 | |
| 388 | conf.dst_addr = espi->sspdr_phys; |
| 389 | conf.dst_addr_width = buswidth; |
| 390 | } |
| 391 | |
| 392 | ret = dmaengine_slave_config(chan, &conf); |
| 393 | if (ret) |
| 394 | return ERR_PTR(ret); |
| 395 | |
| 396 | /* |
| 397 | * We need to split the transfer into PAGE_SIZE'd chunks. This is |
| 398 | * because we are using @espi->zeropage to provide a zero RX buffer |
| 399 | * for the TX transfers and we have only allocated one page for that. |
| 400 | * |
| 401 | * For performance reasons we allocate a new sg_table only when |
| 402 | * needed. Otherwise we will re-use the current one. Eventually the |
| 403 | * last sg_table is released in ep93xx_spi_release_dma(). |
| 404 | */ |
| 405 | |
| 406 | nents = DIV_ROUND_UP(len, PAGE_SIZE); |
| 407 | if (nents != sgt->nents) { |
| 408 | sg_free_table(sgt); |
| 409 | |
| 410 | ret = sg_alloc_table(sgt, nents, GFP_KERNEL); |
| 411 | if (ret) |
| 412 | return ERR_PTR(ret); |
| 413 | } |
| 414 | |
| 415 | pbuf = buf; |
| 416 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 417 | size_t bytes = min_t(size_t, len, PAGE_SIZE); |
| 418 | |
| 419 | if (buf) { |
| 420 | sg_set_page(sg, virt_to_page(pbuf), bytes, |
| 421 | offset_in_page(pbuf)); |
| 422 | } else { |
| 423 | sg_set_page(sg, virt_to_page(espi->zeropage), |
| 424 | bytes, 0); |
| 425 | } |
| 426 | |
| 427 | pbuf += bytes; |
| 428 | len -= bytes; |
| 429 | } |
| 430 | |
| 431 | if (WARN_ON(len)) { |
Jarkko Nikula | a1829d2 | 2013-10-11 13:53:59 +0300 | [diff] [blame] | 432 | dev_warn(&espi->pdev->dev, "len = %zu expected 0!\n", len); |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 433 | return ERR_PTR(-EINVAL); |
| 434 | } |
| 435 | |
| 436 | nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir); |
| 437 | if (!nents) |
| 438 | return ERR_PTR(-ENOMEM); |
| 439 | |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 440 | txd = dmaengine_prep_slave_sg(chan, sgt->sgl, nents, dir, DMA_CTRL_ACK); |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 441 | if (!txd) { |
| 442 | dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir); |
| 443 | return ERR_PTR(-ENOMEM); |
| 444 | } |
| 445 | return txd; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * ep93xx_spi_dma_finish() - finishes with a DMA transfer |
| 450 | * @espi: ep93xx SPI controller struct |
| 451 | * @dir: DMA transfer direction |
| 452 | * |
| 453 | * Function finishes with the DMA transfer. After this, the DMA buffer is |
| 454 | * unmapped. |
| 455 | */ |
| 456 | static void ep93xx_spi_dma_finish(struct ep93xx_spi *espi, |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 457 | enum dma_transfer_direction dir) |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 458 | { |
| 459 | struct dma_chan *chan; |
| 460 | struct sg_table *sgt; |
| 461 | |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 462 | if (dir == DMA_DEV_TO_MEM) { |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 463 | chan = espi->dma_rx; |
| 464 | sgt = &espi->rx_sgt; |
| 465 | } else { |
| 466 | chan = espi->dma_tx; |
| 467 | sgt = &espi->tx_sgt; |
| 468 | } |
| 469 | |
| 470 | dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir); |
| 471 | } |
| 472 | |
| 473 | static void ep93xx_spi_dma_callback(void *callback_param) |
| 474 | { |
| 475 | complete(callback_param); |
| 476 | } |
| 477 | |
| 478 | static void ep93xx_spi_dma_transfer(struct ep93xx_spi *espi) |
| 479 | { |
| 480 | struct spi_message *msg = espi->current_msg; |
| 481 | struct dma_async_tx_descriptor *rxd, *txd; |
| 482 | |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 483 | rxd = ep93xx_spi_dma_prepare(espi, DMA_DEV_TO_MEM); |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 484 | if (IS_ERR(rxd)) { |
| 485 | dev_err(&espi->pdev->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd)); |
| 486 | msg->status = PTR_ERR(rxd); |
| 487 | return; |
| 488 | } |
| 489 | |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 490 | txd = ep93xx_spi_dma_prepare(espi, DMA_MEM_TO_DEV); |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 491 | if (IS_ERR(txd)) { |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 492 | ep93xx_spi_dma_finish(espi, DMA_DEV_TO_MEM); |
Fabio Estevam | f7aa23c | 2016-05-23 23:24:21 -0300 | [diff] [blame] | 493 | dev_err(&espi->pdev->dev, "DMA TX failed: %ld\n", PTR_ERR(txd)); |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 494 | msg->status = PTR_ERR(txd); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | /* We are ready when RX is done */ |
| 499 | rxd->callback = ep93xx_spi_dma_callback; |
| 500 | rxd->callback_param = &espi->wait; |
| 501 | |
| 502 | /* Now submit both descriptors and wait while they finish */ |
| 503 | dmaengine_submit(rxd); |
| 504 | dmaengine_submit(txd); |
| 505 | |
| 506 | dma_async_issue_pending(espi->dma_rx); |
| 507 | dma_async_issue_pending(espi->dma_tx); |
| 508 | |
| 509 | wait_for_completion(&espi->wait); |
| 510 | |
H Hartley Sweeten | d4b9b57 | 2012-04-17 18:46:36 -0700 | [diff] [blame] | 511 | ep93xx_spi_dma_finish(espi, DMA_MEM_TO_DEV); |
| 512 | ep93xx_spi_dma_finish(espi, DMA_DEV_TO_MEM); |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 513 | } |
| 514 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 515 | /** |
| 516 | * ep93xx_spi_process_transfer() - processes one SPI transfer |
| 517 | * @espi: ep93xx SPI controller struct |
| 518 | * @msg: current message |
| 519 | * @t: transfer to process |
| 520 | * |
| 521 | * This function processes one SPI transfer given in @t. Function waits until |
| 522 | * transfer is complete (may sleep) and updates @msg->status based on whether |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 523 | * transfer was successfully processed or not. |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 524 | */ |
| 525 | static void ep93xx_spi_process_transfer(struct ep93xx_spi *espi, |
| 526 | struct spi_message *msg, |
| 527 | struct spi_transfer *t) |
| 528 | { |
H Hartley Sweeten | 4870c21 | 2013-06-28 11:43:34 -0700 | [diff] [blame] | 529 | int err; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 530 | |
| 531 | msg->state = t; |
| 532 | |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 533 | err = ep93xx_spi_chip_setup(espi, msg->spi, t); |
H Hartley Sweeten | 4870c21 | 2013-06-28 11:43:34 -0700 | [diff] [blame] | 534 | if (err) { |
H Hartley Sweeten | f7ef1da | 2013-07-02 10:10:29 -0700 | [diff] [blame] | 535 | dev_err(&espi->pdev->dev, |
| 536 | "failed to setup chip for transfer\n"); |
H Hartley Sweeten | 4870c21 | 2013-06-28 11:43:34 -0700 | [diff] [blame] | 537 | msg->status = err; |
| 538 | return; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | espi->rx = 0; |
| 542 | espi->tx = 0; |
| 543 | |
| 544 | /* |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 545 | * There is no point of setting up DMA for the transfers which will |
| 546 | * fit into the FIFO and can be transferred with a single interrupt. |
| 547 | * So in these cases we will be using PIO and don't bother for DMA. |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 548 | */ |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 549 | if (espi->dma_rx && t->len > SPI_FIFO_SIZE) |
| 550 | ep93xx_spi_dma_transfer(espi); |
| 551 | else |
| 552 | ep93xx_spi_pio_transfer(espi); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 553 | |
| 554 | /* |
| 555 | * In case of error during transmit, we bail out from processing |
| 556 | * the message. |
| 557 | */ |
| 558 | if (msg->status) |
| 559 | return; |
| 560 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 561 | msg->actual_length += t->len; |
| 562 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 563 | /* |
| 564 | * After this transfer is finished, perform any possible |
| 565 | * post-transfer actions requested by the protocol driver. |
| 566 | */ |
| 567 | if (t->delay_usecs) { |
| 568 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 569 | schedule_timeout(usecs_to_jiffies(t->delay_usecs)); |
| 570 | } |
| 571 | if (t->cs_change) { |
| 572 | if (!list_is_last(&t->transfer_list, &msg->transfers)) { |
| 573 | /* |
| 574 | * In case protocol driver is asking us to drop the |
| 575 | * chipselect briefly, we let the scheduler to handle |
| 576 | * any "delay" here. |
| 577 | */ |
| 578 | ep93xx_spi_cs_control(msg->spi, false); |
| 579 | cond_resched(); |
| 580 | ep93xx_spi_cs_control(msg->spi, true); |
| 581 | } |
| 582 | } |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* |
| 586 | * ep93xx_spi_process_message() - process one SPI message |
| 587 | * @espi: ep93xx SPI controller struct |
| 588 | * @msg: message to process |
| 589 | * |
| 590 | * This function processes a single SPI message. We go through all transfers in |
| 591 | * the message and pass them to ep93xx_spi_process_transfer(). Chipselect is |
| 592 | * asserted during the whole message (unless per transfer cs_change is set). |
| 593 | * |
| 594 | * @msg->status contains %0 in case of success or negative error code in case of |
| 595 | * failure. |
| 596 | */ |
| 597 | static void ep93xx_spi_process_message(struct ep93xx_spi *espi, |
| 598 | struct spi_message *msg) |
| 599 | { |
| 600 | unsigned long timeout; |
| 601 | struct spi_transfer *t; |
| 602 | int err; |
| 603 | |
| 604 | /* |
| 605 | * Enable the SPI controller and its clock. |
| 606 | */ |
| 607 | err = ep93xx_spi_enable(espi); |
| 608 | if (err) { |
| 609 | dev_err(&espi->pdev->dev, "failed to enable SPI controller\n"); |
| 610 | msg->status = err; |
| 611 | return; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * Just to be sure: flush any data from RX FIFO. |
| 616 | */ |
| 617 | timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT); |
| 618 | while (ep93xx_spi_read_u16(espi, SSPSR) & SSPSR_RNE) { |
| 619 | if (time_after(jiffies, timeout)) { |
| 620 | dev_warn(&espi->pdev->dev, |
| 621 | "timeout while flushing RX FIFO\n"); |
| 622 | msg->status = -ETIMEDOUT; |
| 623 | return; |
| 624 | } |
| 625 | ep93xx_spi_read_u16(espi, SSPDR); |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * We explicitly handle FIFO level. This way we don't have to check TX |
| 630 | * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns. |
| 631 | */ |
| 632 | espi->fifo_level = 0; |
| 633 | |
| 634 | /* |
H Hartley Sweeten | 4870c21 | 2013-06-28 11:43:34 -0700 | [diff] [blame] | 635 | * Assert the chipselect. |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 636 | */ |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 637 | ep93xx_spi_cs_control(msg->spi, true); |
| 638 | |
| 639 | list_for_each_entry(t, &msg->transfers, transfer_list) { |
| 640 | ep93xx_spi_process_transfer(espi, msg, t); |
| 641 | if (msg->status) |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | /* |
| 646 | * Now the whole message is transferred (or failed for some reason). We |
| 647 | * deselect the device and disable the SPI controller. |
| 648 | */ |
| 649 | ep93xx_spi_cs_control(msg->spi, false); |
| 650 | ep93xx_spi_disable(espi); |
| 651 | } |
| 652 | |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 653 | static int ep93xx_spi_transfer_one_message(struct spi_master *master, |
| 654 | struct spi_message *msg) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 655 | { |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 656 | struct ep93xx_spi *espi = spi_master_get_devdata(master); |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 657 | |
| 658 | msg->state = NULL; |
| 659 | msg->status = 0; |
| 660 | msg->actual_length = 0; |
| 661 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 662 | espi->current_msg = msg; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 663 | ep93xx_spi_process_message(espi, msg); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 664 | espi->current_msg = NULL; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 665 | |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 666 | spi_finalize_current_message(master); |
| 667 | |
| 668 | return 0; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id) |
| 672 | { |
| 673 | struct ep93xx_spi *espi = dev_id; |
| 674 | u8 irq_status = ep93xx_spi_read_u8(espi, SSPIIR); |
| 675 | |
| 676 | /* |
| 677 | * If we got ROR (receive overrun) interrupt we know that something is |
| 678 | * wrong. Just abort the message. |
| 679 | */ |
| 680 | if (unlikely(irq_status & SSPIIR_RORIS)) { |
| 681 | /* clear the overrun interrupt */ |
| 682 | ep93xx_spi_write_u8(espi, SSPICR, 0); |
| 683 | dev_warn(&espi->pdev->dev, |
| 684 | "receive overrun, aborting the message\n"); |
| 685 | espi->current_msg->status = -EIO; |
| 686 | } else { |
| 687 | /* |
| 688 | * Interrupt is either RX (RIS) or TX (TIS). For both cases we |
| 689 | * simply execute next data transfer. |
| 690 | */ |
| 691 | if (ep93xx_spi_read_write(espi)) { |
| 692 | /* |
| 693 | * In normal case, there still is some processing left |
| 694 | * for current transfer. Let's wait for the next |
| 695 | * interrupt then. |
| 696 | */ |
| 697 | return IRQ_HANDLED; |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * Current transfer is finished, either with error or with success. In |
| 703 | * any case we disable interrupts and notify the worker to handle |
| 704 | * any post-processing of the message. |
| 705 | */ |
| 706 | ep93xx_spi_disable_interrupts(espi); |
| 707 | complete(&espi->wait); |
| 708 | return IRQ_HANDLED; |
| 709 | } |
| 710 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 711 | static bool ep93xx_spi_dma_filter(struct dma_chan *chan, void *filter_param) |
| 712 | { |
| 713 | if (ep93xx_dma_chan_is_m2p(chan)) |
| 714 | return false; |
| 715 | |
| 716 | chan->private = filter_param; |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | static int ep93xx_spi_setup_dma(struct ep93xx_spi *espi) |
| 721 | { |
| 722 | dma_cap_mask_t mask; |
| 723 | int ret; |
| 724 | |
| 725 | espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL); |
| 726 | if (!espi->zeropage) |
| 727 | return -ENOMEM; |
| 728 | |
| 729 | dma_cap_zero(mask); |
| 730 | dma_cap_set(DMA_SLAVE, mask); |
| 731 | |
| 732 | espi->dma_rx_data.port = EP93XX_DMA_SSP; |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 733 | espi->dma_rx_data.direction = DMA_DEV_TO_MEM; |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 734 | espi->dma_rx_data.name = "ep93xx-spi-rx"; |
| 735 | |
| 736 | espi->dma_rx = dma_request_channel(mask, ep93xx_spi_dma_filter, |
| 737 | &espi->dma_rx_data); |
| 738 | if (!espi->dma_rx) { |
| 739 | ret = -ENODEV; |
| 740 | goto fail_free_page; |
| 741 | } |
| 742 | |
| 743 | espi->dma_tx_data.port = EP93XX_DMA_SSP; |
Vinod Koul | a485df4 | 2011-10-14 10:47:38 +0530 | [diff] [blame] | 744 | espi->dma_tx_data.direction = DMA_MEM_TO_DEV; |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 745 | espi->dma_tx_data.name = "ep93xx-spi-tx"; |
| 746 | |
| 747 | espi->dma_tx = dma_request_channel(mask, ep93xx_spi_dma_filter, |
| 748 | &espi->dma_tx_data); |
| 749 | if (!espi->dma_tx) { |
| 750 | ret = -ENODEV; |
| 751 | goto fail_release_rx; |
| 752 | } |
| 753 | |
| 754 | return 0; |
| 755 | |
| 756 | fail_release_rx: |
| 757 | dma_release_channel(espi->dma_rx); |
| 758 | espi->dma_rx = NULL; |
| 759 | fail_free_page: |
| 760 | free_page((unsigned long)espi->zeropage); |
| 761 | |
| 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | static void ep93xx_spi_release_dma(struct ep93xx_spi *espi) |
| 766 | { |
| 767 | if (espi->dma_rx) { |
| 768 | dma_release_channel(espi->dma_rx); |
| 769 | sg_free_table(&espi->rx_sgt); |
| 770 | } |
| 771 | if (espi->dma_tx) { |
| 772 | dma_release_channel(espi->dma_tx); |
| 773 | sg_free_table(&espi->tx_sgt); |
| 774 | } |
| 775 | |
| 776 | if (espi->zeropage) |
| 777 | free_page((unsigned long)espi->zeropage); |
| 778 | } |
| 779 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 780 | static int ep93xx_spi_probe(struct platform_device *pdev) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 781 | { |
| 782 | struct spi_master *master; |
| 783 | struct ep93xx_spi_info *info; |
| 784 | struct ep93xx_spi *espi; |
| 785 | struct resource *res; |
Hannu Heikkinen | 6d6467e | 2012-05-09 17:26:26 +0300 | [diff] [blame] | 786 | int irq; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 787 | int error; |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 788 | int i; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 789 | |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 790 | info = dev_get_platdata(&pdev->dev); |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 791 | if (!info) { |
| 792 | dev_err(&pdev->dev, "missing platform data\n"); |
| 793 | return -EINVAL; |
| 794 | } |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 795 | |
H Hartley Sweeten | 48a7776 | 2013-07-02 10:07:53 -0700 | [diff] [blame] | 796 | irq = platform_get_irq(pdev, 0); |
| 797 | if (irq < 0) { |
| 798 | dev_err(&pdev->dev, "failed to get irq resources\n"); |
| 799 | return -EBUSY; |
| 800 | } |
| 801 | |
| 802 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 803 | if (!res) { |
| 804 | dev_err(&pdev->dev, "unable to get iomem resource\n"); |
| 805 | return -ENODEV; |
| 806 | } |
| 807 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 808 | master = spi_alloc_master(&pdev->dev, sizeof(*espi)); |
H Hartley Sweeten | b2d185e | 2013-07-02 10:08:59 -0700 | [diff] [blame] | 809 | if (!master) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 810 | return -ENOMEM; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 811 | |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 812 | master->transfer_one_message = ep93xx_spi_transfer_one_message; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 813 | master->bus_num = pdev->id; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 814 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 815 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 816 | |
H Hartley Sweeten | 55f0cd3 | 2017-02-16 13:07:37 -0700 | [diff] [blame] | 817 | master->num_chipselect = info->num_chipselect; |
| 818 | master->cs_gpios = devm_kzalloc(&master->dev, |
| 819 | sizeof(int) * master->num_chipselect, |
| 820 | GFP_KERNEL); |
| 821 | if (!master->cs_gpios) { |
| 822 | error = -ENOMEM; |
| 823 | goto fail_release_master; |
| 824 | } |
| 825 | |
| 826 | for (i = 0; i < master->num_chipselect; i++) { |
| 827 | master->cs_gpios[i] = info->chipselect[i]; |
| 828 | |
| 829 | if (!gpio_is_valid(master->cs_gpios[i])) |
| 830 | continue; |
| 831 | |
| 832 | error = devm_gpio_request_one(&pdev->dev, master->cs_gpios[i], |
| 833 | GPIOF_OUT_INIT_HIGH, |
| 834 | "ep93xx-spi"); |
| 835 | if (error) { |
| 836 | dev_err(&pdev->dev, "could not request cs gpio %d\n", |
| 837 | master->cs_gpios[i]); |
| 838 | goto fail_release_master; |
| 839 | } |
| 840 | } |
| 841 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 842 | platform_set_drvdata(pdev, master); |
| 843 | |
| 844 | espi = spi_master_get_devdata(master); |
| 845 | |
H Hartley Sweeten | e6eb8d9 | 2013-07-02 10:08:21 -0700 | [diff] [blame] | 846 | espi->clk = devm_clk_get(&pdev->dev, NULL); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 847 | if (IS_ERR(espi->clk)) { |
| 848 | dev_err(&pdev->dev, "unable to get spi clock\n"); |
| 849 | error = PTR_ERR(espi->clk); |
| 850 | goto fail_release_master; |
| 851 | } |
| 852 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 853 | init_completion(&espi->wait); |
| 854 | |
| 855 | /* |
| 856 | * Calculate maximum and minimum supported clock rates |
| 857 | * for the controller. |
| 858 | */ |
Axel Lin | 56fc0b4 | 2014-02-08 23:52:26 +0800 | [diff] [blame] | 859 | master->max_speed_hz = clk_get_rate(espi->clk) / 2; |
| 860 | master->min_speed_hz = clk_get_rate(espi->clk) / (254 * 256); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 861 | espi->pdev = pdev; |
| 862 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 863 | espi->sspdr_phys = res->start + SSPDR; |
Hannu Heikkinen | 6d6467e | 2012-05-09 17:26:26 +0300 | [diff] [blame] | 864 | |
Thierry Reding | b0ee560 | 2013-01-21 11:09:18 +0100 | [diff] [blame] | 865 | espi->regs_base = devm_ioremap_resource(&pdev->dev, res); |
| 866 | if (IS_ERR(espi->regs_base)) { |
| 867 | error = PTR_ERR(espi->regs_base); |
H Hartley Sweeten | e6eb8d9 | 2013-07-02 10:08:21 -0700 | [diff] [blame] | 868 | goto fail_release_master; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Hannu Heikkinen | 6d6467e | 2012-05-09 17:26:26 +0300 | [diff] [blame] | 871 | error = devm_request_irq(&pdev->dev, irq, ep93xx_spi_interrupt, |
| 872 | 0, "ep93xx-spi", espi); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 873 | if (error) { |
| 874 | dev_err(&pdev->dev, "failed to request irq\n"); |
H Hartley Sweeten | e6eb8d9 | 2013-07-02 10:08:21 -0700 | [diff] [blame] | 875 | goto fail_release_master; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 878 | if (info->use_dma && ep93xx_spi_setup_dma(espi)) |
| 879 | dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n"); |
| 880 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 881 | /* make sure that the hardware is disabled */ |
| 882 | ep93xx_spi_write_u8(espi, SSPCR1, 0); |
| 883 | |
Jingoo Han | 434eaf3 | 2013-09-24 13:30:41 +0900 | [diff] [blame] | 884 | error = devm_spi_register_master(&pdev->dev, master); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 885 | if (error) { |
| 886 | dev_err(&pdev->dev, "failed to register SPI master\n"); |
H Hartley Sweeten | 84ddb3c | 2013-07-08 09:12:37 -0700 | [diff] [blame] | 887 | goto fail_free_dma; |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n", |
Hannu Heikkinen | 6d6467e | 2012-05-09 17:26:26 +0300 | [diff] [blame] | 891 | (unsigned long)res->start, irq); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 892 | |
| 893 | return 0; |
| 894 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 895 | fail_free_dma: |
| 896 | ep93xx_spi_release_dma(espi); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 897 | fail_release_master: |
| 898 | spi_master_put(master); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 899 | |
| 900 | return error; |
| 901 | } |
| 902 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 903 | static int ep93xx_spi_remove(struct platform_device *pdev) |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 904 | { |
| 905 | struct spi_master *master = platform_get_drvdata(pdev); |
| 906 | struct ep93xx_spi *espi = spi_master_get_devdata(master); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 907 | |
Mika Westerberg | 626a96d | 2011-05-29 13:10:06 +0300 | [diff] [blame] | 908 | ep93xx_spi_release_dma(espi); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 909 | |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
| 913 | static struct platform_driver ep93xx_spi_driver = { |
| 914 | .driver = { |
| 915 | .name = "ep93xx-spi", |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 916 | }, |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 917 | .probe = ep93xx_spi_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 918 | .remove = ep93xx_spi_remove, |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 919 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 920 | module_platform_driver(ep93xx_spi_driver); |
Mika Westerberg | 011f23a | 2010-05-06 04:47:04 +0000 | [diff] [blame] | 921 | |
| 922 | MODULE_DESCRIPTION("EP93xx SPI Controller driver"); |
| 923 | MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>"); |
| 924 | MODULE_LICENSE("GPL"); |
| 925 | MODULE_ALIAS("platform:ep93xx-spi"); |