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