blob: 0bd792020471b3d4e8922c405eec80ad9ea45cb2 [file] [log] [blame]
Mika Westerberg011f23a2010-05-06 04:47:04 +00001/*
2 * Driver for Cirrus Logic EP93xx SPI controller.
3 *
Mika Westerberg626a96d2011-05-29 13:10:06 +03004 * Copyright (C) 2010-2011 Mika Westerberg
Mika Westerberg011f23a2010-05-06 04:47:04 +00005 *
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 Westerberg626a96d2011-05-29 13:10:06 +030024#include <linux/dmaengine.h>
Mika Westerberg011f23a2010-05-06 04:47:04 +000025#include <linux/bitops.h>
26#include <linux/interrupt.h>
Mika Westerberg5bdb76132011-10-15 21:40:09 +030027#include <linux/module.h>
Mika Westerberg011f23a2010-05-06 04:47:04 +000028#include <linux/platform_device.h>
Mika Westerberg011f23a2010-05-06 04:47:04 +000029#include <linux/sched.h>
Mika Westerberg626a96d2011-05-29 13:10:06 +030030#include <linux/scatterlist.h>
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -070031#include <linux/gpio.h>
Mika Westerberg011f23a2010-05-06 04:47:04 +000032#include <linux/spi/spi.h>
33
Arnd Bergmanna3b29242012-08-24 15:12:11 +020034#include <linux/platform_data/dma-ep93xx.h>
35#include <linux/platform_data/spi-ep93xx.h>
Mika Westerberg011f23a2010-05-06 04:47:04 +000036
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 Westerberg011f23a2010-05-06 04:47:04 +000073 * @pdev: pointer to platform device
74 * @clk: clock for the controller
H Hartley Sweeten12329782017-08-09 08:51:25 +120075 * @mmio: pointer to ioremap()'d registers
Mika Westerberg626a96d2011-05-29 13:10:06 +030076 * @sspdr_phys: physical address of the SSPDR register
Mika Westerberg011f23a2010-05-06 04:47:04 +000077 * @wait: wait here until given transfer is completed
Mika Westerberg011f23a2010-05-06 04:47:04 +000078 * @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 Westerberg626a96d2011-05-29 13:10:06 +030083 * @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 Westerberg011f23a2010-05-06 04:47:04 +000091 */
92struct ep93xx_spi {
Mika Westerberg011f23a2010-05-06 04:47:04 +000093 const struct platform_device *pdev;
94 struct clk *clk;
H Hartley Sweeten12329782017-08-09 08:51:25 +120095 void __iomem *mmio;
Mika Westerberg626a96d2011-05-29 13:10:06 +030096 unsigned long sspdr_phys;
Mika Westerberg011f23a2010-05-06 04:47:04 +000097 struct completion wait;
Mika Westerberg011f23a2010-05-06 04:47:04 +000098 struct spi_message *current_msg;
99 size_t tx;
100 size_t rx;
101 size_t fifo_level;
Mika Westerberg626a96d2011-05-29 13:10:06 +0300102 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 Westerberg011f23a2010-05-06 04:47:04 +0000109};
110
Mika Westerberg011f23a2010-05-06 04:47:04 +0000111/* converts bits per word to CR0.DSS value */
112#define bits_per_word_to_dss(bpw) ((bpw) - 1)
113
Mika Westerberg011f23a2010-05-06 04:47:04 +0000114static int ep93xx_spi_enable(const struct ep93xx_spi *espi)
115{
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200116 u32 val;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000117 int err;
118
119 err = clk_enable(espi->clk);
120 if (err)
121 return err;
122
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200123 val = readl(espi->mmio + SSPCR1);
124 val |= SSPCR1_SSE;
125 writel(val, espi->mmio + SSPCR1);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000126
127 return 0;
128}
129
130static void ep93xx_spi_disable(const struct ep93xx_spi *espi)
131{
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200132 u32 val;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000133
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200134 val = readl(espi->mmio + SSPCR1);
135 val &= ~SSPCR1_SSE;
136 writel(val, espi->mmio + SSPCR1);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000137
138 clk_disable(espi->clk);
139}
140
141static void ep93xx_spi_enable_interrupts(const struct ep93xx_spi *espi)
142{
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200143 u32 val;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000144
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200145 val = readl(espi->mmio + SSPCR1);
146 val |= (SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
147 writel(val, espi->mmio + SSPCR1);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000148}
149
150static void ep93xx_spi_disable_interrupts(const struct ep93xx_spi *espi)
151{
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200152 u32 val;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000153
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200154 val = readl(espi->mmio + SSPCR1);
155 val &= ~(SSPCR1_RORIE | SSPCR1_TIE | SSPCR1_RIE);
156 writel(val, espi->mmio + SSPCR1);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000157}
158
159/**
160 * ep93xx_spi_calc_divisors() - calculates SPI clock divisors
161 * @espi: ep93xx SPI controller struct
Mika Westerberg011f23a2010-05-06 04:47:04 +0000162 * @rate: desired SPI output clock rate
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700163 * @div_cpsr: pointer to return the cpsr (pre-scaler) divider
164 * @div_scr: pointer to return the scr divider
Mika Westerberg011f23a2010-05-06 04:47:04 +0000165 */
166static int ep93xx_spi_calc_divisors(const struct ep93xx_spi *espi,
Axel Lin56fc0b42014-02-08 23:52:26 +0800167 u32 rate, u8 *div_cpsr, u8 *div_scr)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000168{
Axel Lin56fc0b42014-02-08 23:52:26 +0800169 struct spi_master *master = platform_get_drvdata(espi->pdev);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000170 unsigned long spi_clk_rate = clk_get_rate(espi->clk);
171 int cpsr, scr;
172
173 /*
174 * Make sure that max value is between values supported by the
175 * controller. Note that minimum value is already checked in
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700176 * ep93xx_spi_transfer_one_message().
Mika Westerberg011f23a2010-05-06 04:47:04 +0000177 */
Axel Lin56fc0b42014-02-08 23:52:26 +0800178 rate = clamp(rate, master->min_speed_hz, master->max_speed_hz);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000179
180 /*
181 * Calculate divisors so that we can get speed according the
182 * following formula:
183 * rate = spi_clock_rate / (cpsr * (1 + scr))
184 *
185 * cpsr must be even number and starts from 2, scr can be any number
186 * between 0 and 255.
187 */
188 for (cpsr = 2; cpsr <= 254; cpsr += 2) {
189 for (scr = 0; scr <= 255; scr++) {
190 if ((spi_clk_rate / (cpsr * (scr + 1))) <= rate) {
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700191 *div_scr = (u8)scr;
192 *div_cpsr = (u8)cpsr;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000193 return 0;
194 }
195 }
196 }
197
198 return -EINVAL;
199}
200
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700201static void ep93xx_spi_cs_control(struct spi_device *spi, bool enable)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000202{
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700203 if (spi->mode & SPI_CS_HIGH)
204 enable = !enable;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000205
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700206 if (gpio_is_valid(spi->cs_gpio))
207 gpio_set_value(spi->cs_gpio, !enable);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000208}
209
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700210static int ep93xx_spi_chip_setup(const struct ep93xx_spi *espi,
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700211 struct spi_device *spi,
212 struct spi_transfer *xfer)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000213{
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700214 u8 dss = bits_per_word_to_dss(xfer->bits_per_word);
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700215 u8 div_cpsr = 0;
216 u8 div_scr = 0;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000217 u16 cr0;
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700218 int err;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000219
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700220 err = ep93xx_spi_calc_divisors(espi, xfer->speed_hz,
221 &div_cpsr, &div_scr);
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700222 if (err)
223 return err;
224
225 cr0 = div_scr << SSPCR0_SCR_SHIFT;
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700226 cr0 |= (spi->mode & (SPI_CPHA | SPI_CPOL)) << SSPCR0_MODE_SHIFT;
H Hartley Sweetend9b65df2013-07-02 10:09:29 -0700227 cr0 |= dss;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000228
229 dev_dbg(&espi->pdev->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700230 spi->mode, div_cpsr, div_scr, dss);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300231 dev_dbg(&espi->pdev->dev, "setup: cr0 %#x\n", cr0);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000232
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200233 writel(div_cpsr, espi->mmio + SSPCPSR);
234 writel(cr0, espi->mmio + SSPCR0);
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700235
236 return 0;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000237}
238
Mika Westerberg011f23a2010-05-06 04:47:04 +0000239static void ep93xx_do_write(struct ep93xx_spi *espi, struct spi_transfer *t)
240{
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200241 u32 val = 0;
242
H Hartley Sweeten701c3582013-07-02 10:07:01 -0700243 if (t->bits_per_word > 8) {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000244 if (t->tx_buf)
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200245 val = ((u16 *)t->tx_buf)[espi->tx];
246 espi->tx += 2;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000247 } else {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000248 if (t->tx_buf)
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200249 val = ((u8 *)t->tx_buf)[espi->tx];
250 espi->tx += 1;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000251 }
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200252 writel(val, espi->mmio + SSPDR);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000253}
254
255static void ep93xx_do_read(struct ep93xx_spi *espi, struct spi_transfer *t)
256{
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200257 u32 val;
258
259 val = readl(espi->mmio + SSPDR);
H Hartley Sweeten701c3582013-07-02 10:07:01 -0700260 if (t->bits_per_word > 8) {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000261 if (t->rx_buf)
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200262 ((u16 *)t->rx_buf)[espi->rx] = val;
263 espi->rx += 2;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000264 } else {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000265 if (t->rx_buf)
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200266 ((u8 *)t->rx_buf)[espi->rx] = val;
267 espi->rx += 1;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000268 }
269}
270
271/**
272 * ep93xx_spi_read_write() - perform next RX/TX transfer
273 * @espi: ep93xx SPI controller struct
274 *
275 * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If
276 * called several times, the whole transfer will be completed. Returns
277 * %-EINPROGRESS when current transfer was not yet completed otherwise %0.
278 *
279 * When this function is finished, RX FIFO should be empty and TX FIFO should be
280 * full.
281 */
282static int ep93xx_spi_read_write(struct ep93xx_spi *espi)
283{
284 struct spi_message *msg = espi->current_msg;
285 struct spi_transfer *t = msg->state;
286
287 /* read as long as RX FIFO has frames in it */
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200288 while ((readl(espi->mmio + SSPSR) & SSPSR_RNE)) {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000289 ep93xx_do_read(espi, t);
290 espi->fifo_level--;
291 }
292
293 /* write as long as TX FIFO has room */
294 while (espi->fifo_level < SPI_FIFO_SIZE && espi->tx < t->len) {
295 ep93xx_do_write(espi, t);
296 espi->fifo_level++;
297 }
298
Mika Westerberg626a96d2011-05-29 13:10:06 +0300299 if (espi->rx == t->len)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000300 return 0;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000301
302 return -EINPROGRESS;
303}
304
Mika Westerberg626a96d2011-05-29 13:10:06 +0300305static void ep93xx_spi_pio_transfer(struct ep93xx_spi *espi)
306{
307 /*
308 * Now everything is set up for the current transfer. We prime the TX
309 * FIFO, enable interrupts, and wait for the transfer to complete.
310 */
311 if (ep93xx_spi_read_write(espi)) {
312 ep93xx_spi_enable_interrupts(espi);
313 wait_for_completion(&espi->wait);
314 }
315}
316
317/**
318 * ep93xx_spi_dma_prepare() - prepares a DMA transfer
319 * @espi: ep93xx SPI controller struct
320 * @dir: DMA transfer direction
321 *
322 * Function configures the DMA, maps the buffer and prepares the DMA
323 * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR
324 * in case of failure.
325 */
326static struct dma_async_tx_descriptor *
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700327ep93xx_spi_dma_prepare(struct ep93xx_spi *espi, enum dma_transfer_direction dir)
Mika Westerberg626a96d2011-05-29 13:10:06 +0300328{
329 struct spi_transfer *t = espi->current_msg->state;
330 struct dma_async_tx_descriptor *txd;
331 enum dma_slave_buswidth buswidth;
332 struct dma_slave_config conf;
333 struct scatterlist *sg;
334 struct sg_table *sgt;
335 struct dma_chan *chan;
336 const void *buf, *pbuf;
337 size_t len = t->len;
338 int i, ret, nents;
339
H Hartley Sweeten701c3582013-07-02 10:07:01 -0700340 if (t->bits_per_word > 8)
Mika Westerberg626a96d2011-05-29 13:10:06 +0300341 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
342 else
343 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
344
345 memset(&conf, 0, sizeof(conf));
346 conf.direction = dir;
347
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700348 if (dir == DMA_DEV_TO_MEM) {
Mika Westerberg626a96d2011-05-29 13:10:06 +0300349 chan = espi->dma_rx;
350 buf = t->rx_buf;
351 sgt = &espi->rx_sgt;
352
353 conf.src_addr = espi->sspdr_phys;
354 conf.src_addr_width = buswidth;
355 } else {
356 chan = espi->dma_tx;
357 buf = t->tx_buf;
358 sgt = &espi->tx_sgt;
359
360 conf.dst_addr = espi->sspdr_phys;
361 conf.dst_addr_width = buswidth;
362 }
363
364 ret = dmaengine_slave_config(chan, &conf);
365 if (ret)
366 return ERR_PTR(ret);
367
368 /*
369 * We need to split the transfer into PAGE_SIZE'd chunks. This is
370 * because we are using @espi->zeropage to provide a zero RX buffer
371 * for the TX transfers and we have only allocated one page for that.
372 *
373 * For performance reasons we allocate a new sg_table only when
374 * needed. Otherwise we will re-use the current one. Eventually the
375 * last sg_table is released in ep93xx_spi_release_dma().
376 */
377
378 nents = DIV_ROUND_UP(len, PAGE_SIZE);
379 if (nents != sgt->nents) {
380 sg_free_table(sgt);
381
382 ret = sg_alloc_table(sgt, nents, GFP_KERNEL);
383 if (ret)
384 return ERR_PTR(ret);
385 }
386
387 pbuf = buf;
388 for_each_sg(sgt->sgl, sg, sgt->nents, i) {
389 size_t bytes = min_t(size_t, len, PAGE_SIZE);
390
391 if (buf) {
392 sg_set_page(sg, virt_to_page(pbuf), bytes,
393 offset_in_page(pbuf));
394 } else {
395 sg_set_page(sg, virt_to_page(espi->zeropage),
396 bytes, 0);
397 }
398
399 pbuf += bytes;
400 len -= bytes;
401 }
402
403 if (WARN_ON(len)) {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300404 dev_warn(&espi->pdev->dev, "len = %zu expected 0!\n", len);
Mika Westerberg626a96d2011-05-29 13:10:06 +0300405 return ERR_PTR(-EINVAL);
406 }
407
408 nents = dma_map_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
409 if (!nents)
410 return ERR_PTR(-ENOMEM);
411
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700412 txd = dmaengine_prep_slave_sg(chan, sgt->sgl, nents, dir, DMA_CTRL_ACK);
Mika Westerberg626a96d2011-05-29 13:10:06 +0300413 if (!txd) {
414 dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
415 return ERR_PTR(-ENOMEM);
416 }
417 return txd;
418}
419
420/**
421 * ep93xx_spi_dma_finish() - finishes with a DMA transfer
422 * @espi: ep93xx SPI controller struct
423 * @dir: DMA transfer direction
424 *
425 * Function finishes with the DMA transfer. After this, the DMA buffer is
426 * unmapped.
427 */
428static void ep93xx_spi_dma_finish(struct ep93xx_spi *espi,
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700429 enum dma_transfer_direction dir)
Mika Westerberg626a96d2011-05-29 13:10:06 +0300430{
431 struct dma_chan *chan;
432 struct sg_table *sgt;
433
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700434 if (dir == DMA_DEV_TO_MEM) {
Mika Westerberg626a96d2011-05-29 13:10:06 +0300435 chan = espi->dma_rx;
436 sgt = &espi->rx_sgt;
437 } else {
438 chan = espi->dma_tx;
439 sgt = &espi->tx_sgt;
440 }
441
442 dma_unmap_sg(chan->device->dev, sgt->sgl, sgt->nents, dir);
443}
444
445static void ep93xx_spi_dma_callback(void *callback_param)
446{
447 complete(callback_param);
448}
449
450static void ep93xx_spi_dma_transfer(struct ep93xx_spi *espi)
451{
452 struct spi_message *msg = espi->current_msg;
453 struct dma_async_tx_descriptor *rxd, *txd;
454
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700455 rxd = ep93xx_spi_dma_prepare(espi, DMA_DEV_TO_MEM);
Mika Westerberg626a96d2011-05-29 13:10:06 +0300456 if (IS_ERR(rxd)) {
457 dev_err(&espi->pdev->dev, "DMA RX failed: %ld\n", PTR_ERR(rxd));
458 msg->status = PTR_ERR(rxd);
459 return;
460 }
461
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700462 txd = ep93xx_spi_dma_prepare(espi, DMA_MEM_TO_DEV);
Mika Westerberg626a96d2011-05-29 13:10:06 +0300463 if (IS_ERR(txd)) {
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700464 ep93xx_spi_dma_finish(espi, DMA_DEV_TO_MEM);
Fabio Estevamf7aa23c2016-05-23 23:24:21 -0300465 dev_err(&espi->pdev->dev, "DMA TX failed: %ld\n", PTR_ERR(txd));
Mika Westerberg626a96d2011-05-29 13:10:06 +0300466 msg->status = PTR_ERR(txd);
467 return;
468 }
469
470 /* We are ready when RX is done */
471 rxd->callback = ep93xx_spi_dma_callback;
472 rxd->callback_param = &espi->wait;
473
474 /* Now submit both descriptors and wait while they finish */
475 dmaengine_submit(rxd);
476 dmaengine_submit(txd);
477
478 dma_async_issue_pending(espi->dma_rx);
479 dma_async_issue_pending(espi->dma_tx);
480
481 wait_for_completion(&espi->wait);
482
H Hartley Sweetend4b9b572012-04-17 18:46:36 -0700483 ep93xx_spi_dma_finish(espi, DMA_MEM_TO_DEV);
484 ep93xx_spi_dma_finish(espi, DMA_DEV_TO_MEM);
Mika Westerberg626a96d2011-05-29 13:10:06 +0300485}
486
Mika Westerberg011f23a2010-05-06 04:47:04 +0000487/**
488 * ep93xx_spi_process_transfer() - processes one SPI transfer
489 * @espi: ep93xx SPI controller struct
490 * @msg: current message
491 * @t: transfer to process
492 *
493 * This function processes one SPI transfer given in @t. Function waits until
494 * transfer is complete (may sleep) and updates @msg->status based on whether
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300495 * transfer was successfully processed or not.
Mika Westerberg011f23a2010-05-06 04:47:04 +0000496 */
497static void ep93xx_spi_process_transfer(struct ep93xx_spi *espi,
498 struct spi_message *msg,
499 struct spi_transfer *t)
500{
H Hartley Sweeten4870c212013-06-28 11:43:34 -0700501 int err;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000502
503 msg->state = t;
504
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700505 err = ep93xx_spi_chip_setup(espi, msg->spi, t);
H Hartley Sweeten4870c212013-06-28 11:43:34 -0700506 if (err) {
H Hartley Sweetenf7ef1da2013-07-02 10:10:29 -0700507 dev_err(&espi->pdev->dev,
508 "failed to setup chip for transfer\n");
H Hartley Sweeten4870c212013-06-28 11:43:34 -0700509 msg->status = err;
510 return;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000511 }
512
513 espi->rx = 0;
514 espi->tx = 0;
515
516 /*
Mika Westerberg626a96d2011-05-29 13:10:06 +0300517 * There is no point of setting up DMA for the transfers which will
518 * fit into the FIFO and can be transferred with a single interrupt.
519 * So in these cases we will be using PIO and don't bother for DMA.
Mika Westerberg011f23a2010-05-06 04:47:04 +0000520 */
Mika Westerberg626a96d2011-05-29 13:10:06 +0300521 if (espi->dma_rx && t->len > SPI_FIFO_SIZE)
522 ep93xx_spi_dma_transfer(espi);
523 else
524 ep93xx_spi_pio_transfer(espi);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000525
526 /*
527 * In case of error during transmit, we bail out from processing
528 * the message.
529 */
530 if (msg->status)
531 return;
532
Mika Westerberg626a96d2011-05-29 13:10:06 +0300533 msg->actual_length += t->len;
534
Mika Westerberg011f23a2010-05-06 04:47:04 +0000535 /*
536 * After this transfer is finished, perform any possible
537 * post-transfer actions requested by the protocol driver.
538 */
539 if (t->delay_usecs) {
540 set_current_state(TASK_UNINTERRUPTIBLE);
541 schedule_timeout(usecs_to_jiffies(t->delay_usecs));
542 }
543 if (t->cs_change) {
544 if (!list_is_last(&t->transfer_list, &msg->transfers)) {
545 /*
546 * In case protocol driver is asking us to drop the
547 * chipselect briefly, we let the scheduler to handle
548 * any "delay" here.
549 */
550 ep93xx_spi_cs_control(msg->spi, false);
551 cond_resched();
552 ep93xx_spi_cs_control(msg->spi, true);
553 }
554 }
Mika Westerberg011f23a2010-05-06 04:47:04 +0000555}
556
557/*
558 * ep93xx_spi_process_message() - process one SPI message
559 * @espi: ep93xx SPI controller struct
560 * @msg: message to process
561 *
562 * This function processes a single SPI message. We go through all transfers in
563 * the message and pass them to ep93xx_spi_process_transfer(). Chipselect is
564 * asserted during the whole message (unless per transfer cs_change is set).
565 *
566 * @msg->status contains %0 in case of success or negative error code in case of
567 * failure.
568 */
569static void ep93xx_spi_process_message(struct ep93xx_spi *espi,
570 struct spi_message *msg)
571{
572 unsigned long timeout;
573 struct spi_transfer *t;
574 int err;
575
576 /*
577 * Enable the SPI controller and its clock.
578 */
579 err = ep93xx_spi_enable(espi);
580 if (err) {
581 dev_err(&espi->pdev->dev, "failed to enable SPI controller\n");
582 msg->status = err;
583 return;
584 }
585
586 /*
587 * Just to be sure: flush any data from RX FIFO.
588 */
589 timeout = jiffies + msecs_to_jiffies(SPI_TIMEOUT);
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200590 while (readl(espi->mmio + SSPSR) & SSPSR_RNE) {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000591 if (time_after(jiffies, timeout)) {
592 dev_warn(&espi->pdev->dev,
593 "timeout while flushing RX FIFO\n");
594 msg->status = -ETIMEDOUT;
595 return;
596 }
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200597 readl(espi->mmio + SSPDR);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000598 }
599
600 /*
601 * We explicitly handle FIFO level. This way we don't have to check TX
602 * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
603 */
604 espi->fifo_level = 0;
605
606 /*
H Hartley Sweeten4870c212013-06-28 11:43:34 -0700607 * Assert the chipselect.
Mika Westerberg011f23a2010-05-06 04:47:04 +0000608 */
Mika Westerberg011f23a2010-05-06 04:47:04 +0000609 ep93xx_spi_cs_control(msg->spi, true);
610
611 list_for_each_entry(t, &msg->transfers, transfer_list) {
612 ep93xx_spi_process_transfer(espi, msg, t);
613 if (msg->status)
614 break;
615 }
616
617 /*
618 * Now the whole message is transferred (or failed for some reason). We
619 * deselect the device and disable the SPI controller.
620 */
621 ep93xx_spi_cs_control(msg->spi, false);
622 ep93xx_spi_disable(espi);
623}
624
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700625static int ep93xx_spi_transfer_one_message(struct spi_master *master,
626 struct spi_message *msg)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000627{
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700628 struct ep93xx_spi *espi = spi_master_get_devdata(master);
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700629
630 msg->state = NULL;
631 msg->status = 0;
632 msg->actual_length = 0;
633
Mika Westerberg011f23a2010-05-06 04:47:04 +0000634 espi->current_msg = msg;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000635 ep93xx_spi_process_message(espi, msg);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000636 espi->current_msg = NULL;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000637
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700638 spi_finalize_current_message(master);
639
640 return 0;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000641}
642
643static irqreturn_t ep93xx_spi_interrupt(int irq, void *dev_id)
644{
645 struct ep93xx_spi *espi = dev_id;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000646
647 /*
648 * If we got ROR (receive overrun) interrupt we know that something is
649 * wrong. Just abort the message.
650 */
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200651 if (readl(espi->mmio + SSPIIR) & SSPIIR_RORIS) {
Mika Westerberg011f23a2010-05-06 04:47:04 +0000652 /* clear the overrun interrupt */
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200653 writel(0, espi->mmio + SSPICR);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000654 dev_warn(&espi->pdev->dev,
655 "receive overrun, aborting the message\n");
656 espi->current_msg->status = -EIO;
657 } else {
658 /*
659 * Interrupt is either RX (RIS) or TX (TIS). For both cases we
660 * simply execute next data transfer.
661 */
662 if (ep93xx_spi_read_write(espi)) {
663 /*
664 * In normal case, there still is some processing left
665 * for current transfer. Let's wait for the next
666 * interrupt then.
667 */
668 return IRQ_HANDLED;
669 }
670 }
671
672 /*
673 * Current transfer is finished, either with error or with success. In
674 * any case we disable interrupts and notify the worker to handle
675 * any post-processing of the message.
676 */
677 ep93xx_spi_disable_interrupts(espi);
678 complete(&espi->wait);
679 return IRQ_HANDLED;
680}
681
Mika Westerberg626a96d2011-05-29 13:10:06 +0300682static bool ep93xx_spi_dma_filter(struct dma_chan *chan, void *filter_param)
683{
684 if (ep93xx_dma_chan_is_m2p(chan))
685 return false;
686
687 chan->private = filter_param;
688 return true;
689}
690
691static int ep93xx_spi_setup_dma(struct ep93xx_spi *espi)
692{
693 dma_cap_mask_t mask;
694 int ret;
695
696 espi->zeropage = (void *)get_zeroed_page(GFP_KERNEL);
697 if (!espi->zeropage)
698 return -ENOMEM;
699
700 dma_cap_zero(mask);
701 dma_cap_set(DMA_SLAVE, mask);
702
703 espi->dma_rx_data.port = EP93XX_DMA_SSP;
Vinod Koula485df42011-10-14 10:47:38 +0530704 espi->dma_rx_data.direction = DMA_DEV_TO_MEM;
Mika Westerberg626a96d2011-05-29 13:10:06 +0300705 espi->dma_rx_data.name = "ep93xx-spi-rx";
706
707 espi->dma_rx = dma_request_channel(mask, ep93xx_spi_dma_filter,
708 &espi->dma_rx_data);
709 if (!espi->dma_rx) {
710 ret = -ENODEV;
711 goto fail_free_page;
712 }
713
714 espi->dma_tx_data.port = EP93XX_DMA_SSP;
Vinod Koula485df42011-10-14 10:47:38 +0530715 espi->dma_tx_data.direction = DMA_MEM_TO_DEV;
Mika Westerberg626a96d2011-05-29 13:10:06 +0300716 espi->dma_tx_data.name = "ep93xx-spi-tx";
717
718 espi->dma_tx = dma_request_channel(mask, ep93xx_spi_dma_filter,
719 &espi->dma_tx_data);
720 if (!espi->dma_tx) {
721 ret = -ENODEV;
722 goto fail_release_rx;
723 }
724
725 return 0;
726
727fail_release_rx:
728 dma_release_channel(espi->dma_rx);
729 espi->dma_rx = NULL;
730fail_free_page:
731 free_page((unsigned long)espi->zeropage);
732
733 return ret;
734}
735
736static void ep93xx_spi_release_dma(struct ep93xx_spi *espi)
737{
738 if (espi->dma_rx) {
739 dma_release_channel(espi->dma_rx);
740 sg_free_table(&espi->rx_sgt);
741 }
742 if (espi->dma_tx) {
743 dma_release_channel(espi->dma_tx);
744 sg_free_table(&espi->tx_sgt);
745 }
746
747 if (espi->zeropage)
748 free_page((unsigned long)espi->zeropage);
749}
750
Grant Likelyfd4a3192012-12-07 16:57:14 +0000751static int ep93xx_spi_probe(struct platform_device *pdev)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000752{
753 struct spi_master *master;
754 struct ep93xx_spi_info *info;
755 struct ep93xx_spi *espi;
756 struct resource *res;
Hannu Heikkinen6d6467e2012-05-09 17:26:26 +0300757 int irq;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000758 int error;
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700759 int i;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000760
Jingoo Han8074cf02013-07-30 16:58:59 +0900761 info = dev_get_platdata(&pdev->dev);
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700762 if (!info) {
763 dev_err(&pdev->dev, "missing platform data\n");
764 return -EINVAL;
765 }
Mika Westerberg011f23a2010-05-06 04:47:04 +0000766
H Hartley Sweeten48a77762013-07-02 10:07:53 -0700767 irq = platform_get_irq(pdev, 0);
768 if (irq < 0) {
769 dev_err(&pdev->dev, "failed to get irq resources\n");
770 return -EBUSY;
771 }
772
773 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
774 if (!res) {
775 dev_err(&pdev->dev, "unable to get iomem resource\n");
776 return -ENODEV;
777 }
778
Mika Westerberg011f23a2010-05-06 04:47:04 +0000779 master = spi_alloc_master(&pdev->dev, sizeof(*espi));
H Hartley Sweetenb2d185e2013-07-02 10:08:59 -0700780 if (!master)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000781 return -ENOMEM;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000782
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700783 master->transfer_one_message = ep93xx_spi_transfer_one_message;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000784 master->bus_num = pdev->id;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000785 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -0600786 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000787
H Hartley Sweeten55f0cd3f2017-02-16 13:07:37 -0700788 master->num_chipselect = info->num_chipselect;
789 master->cs_gpios = devm_kzalloc(&master->dev,
790 sizeof(int) * master->num_chipselect,
791 GFP_KERNEL);
792 if (!master->cs_gpios) {
793 error = -ENOMEM;
794 goto fail_release_master;
795 }
796
797 for (i = 0; i < master->num_chipselect; i++) {
798 master->cs_gpios[i] = info->chipselect[i];
799
800 if (!gpio_is_valid(master->cs_gpios[i]))
801 continue;
802
803 error = devm_gpio_request_one(&pdev->dev, master->cs_gpios[i],
804 GPIOF_OUT_INIT_HIGH,
805 "ep93xx-spi");
806 if (error) {
807 dev_err(&pdev->dev, "could not request cs gpio %d\n",
808 master->cs_gpios[i]);
809 goto fail_release_master;
810 }
811 }
812
Mika Westerberg011f23a2010-05-06 04:47:04 +0000813 platform_set_drvdata(pdev, master);
814
815 espi = spi_master_get_devdata(master);
816
H Hartley Sweetene6eb8d92013-07-02 10:08:21 -0700817 espi->clk = devm_clk_get(&pdev->dev, NULL);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000818 if (IS_ERR(espi->clk)) {
819 dev_err(&pdev->dev, "unable to get spi clock\n");
820 error = PTR_ERR(espi->clk);
821 goto fail_release_master;
822 }
823
Mika Westerberg011f23a2010-05-06 04:47:04 +0000824 init_completion(&espi->wait);
825
826 /*
827 * Calculate maximum and minimum supported clock rates
828 * for the controller.
829 */
Axel Lin56fc0b42014-02-08 23:52:26 +0800830 master->max_speed_hz = clk_get_rate(espi->clk) / 2;
831 master->min_speed_hz = clk_get_rate(espi->clk) / (254 * 256);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000832 espi->pdev = pdev;
833
Mika Westerberg626a96d2011-05-29 13:10:06 +0300834 espi->sspdr_phys = res->start + SSPDR;
Hannu Heikkinen6d6467e2012-05-09 17:26:26 +0300835
H Hartley Sweeten12329782017-08-09 08:51:25 +1200836 espi->mmio = devm_ioremap_resource(&pdev->dev, res);
837 if (IS_ERR(espi->mmio)) {
838 error = PTR_ERR(espi->mmio);
H Hartley Sweetene6eb8d92013-07-02 10:08:21 -0700839 goto fail_release_master;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000840 }
841
Hannu Heikkinen6d6467e2012-05-09 17:26:26 +0300842 error = devm_request_irq(&pdev->dev, irq, ep93xx_spi_interrupt,
843 0, "ep93xx-spi", espi);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000844 if (error) {
845 dev_err(&pdev->dev, "failed to request irq\n");
H Hartley Sweetene6eb8d92013-07-02 10:08:21 -0700846 goto fail_release_master;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000847 }
848
Mika Westerberg626a96d2011-05-29 13:10:06 +0300849 if (info->use_dma && ep93xx_spi_setup_dma(espi))
850 dev_warn(&pdev->dev, "DMA setup failed. Falling back to PIO\n");
851
Mika Westerberg011f23a2010-05-06 04:47:04 +0000852 /* make sure that the hardware is disabled */
H Hartley Sweeten8447e472017-08-09 08:51:26 +1200853 writel(0, espi->mmio + SSPCR1);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000854
Jingoo Han434eaf32013-09-24 13:30:41 +0900855 error = devm_spi_register_master(&pdev->dev, master);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000856 if (error) {
857 dev_err(&pdev->dev, "failed to register SPI master\n");
H Hartley Sweeten84ddb3c2013-07-08 09:12:37 -0700858 goto fail_free_dma;
Mika Westerberg011f23a2010-05-06 04:47:04 +0000859 }
860
861 dev_info(&pdev->dev, "EP93xx SPI Controller at 0x%08lx irq %d\n",
Hannu Heikkinen6d6467e2012-05-09 17:26:26 +0300862 (unsigned long)res->start, irq);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000863
864 return 0;
865
Mika Westerberg626a96d2011-05-29 13:10:06 +0300866fail_free_dma:
867 ep93xx_spi_release_dma(espi);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000868fail_release_master:
869 spi_master_put(master);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000870
871 return error;
872}
873
Grant Likelyfd4a3192012-12-07 16:57:14 +0000874static int ep93xx_spi_remove(struct platform_device *pdev)
Mika Westerberg011f23a2010-05-06 04:47:04 +0000875{
876 struct spi_master *master = platform_get_drvdata(pdev);
877 struct ep93xx_spi *espi = spi_master_get_devdata(master);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000878
Mika Westerberg626a96d2011-05-29 13:10:06 +0300879 ep93xx_spi_release_dma(espi);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000880
Mika Westerberg011f23a2010-05-06 04:47:04 +0000881 return 0;
882}
883
884static struct platform_driver ep93xx_spi_driver = {
885 .driver = {
886 .name = "ep93xx-spi",
Mika Westerberg011f23a2010-05-06 04:47:04 +0000887 },
Grant Likely940ab882011-10-05 11:29:49 -0600888 .probe = ep93xx_spi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000889 .remove = ep93xx_spi_remove,
Mika Westerberg011f23a2010-05-06 04:47:04 +0000890};
Grant Likely940ab882011-10-05 11:29:49 -0600891module_platform_driver(ep93xx_spi_driver);
Mika Westerberg011f23a2010-05-06 04:47:04 +0000892
893MODULE_DESCRIPTION("EP93xx SPI Controller driver");
894MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
895MODULE_LICENSE("GPL");
896MODULE_ALIAS("platform:ep93xx-spi");