blob: 19600de404228f45105b2d3d2a1a3b5079cf4167 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08002/*
3 * Driver for Atmel AT32 and AT91 SPI Controllers
4 *
5 * Copyright (C) 2006 Atmel Corporation
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08006 */
7
8#include <linux/kernel.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08009#include <linux/clk.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/delay.h>
13#include <linux/dma-mapping.h>
Nicolas Ferre1ccc4042013-04-03 13:59:19 +080014#include <linux/dmaengine.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -080015#include <linux/err.h>
16#include <linux/interrupt.h>
17#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Nicolas Ferre1ccc4042013-04-03 13:59:19 +080019#include <linux/platform_data/dma-atmel.h>
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +010020#include <linux/of.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -080021
Wenyou Yangd4820b72013-03-19 15:42:15 +080022#include <linux/io.h>
Linus Walleijefc92fb2019-01-07 16:51:52 +010023#include <linux/gpio/consumer.h>
Wenyou Yang5bdfd492014-03-05 09:58:49 +080024#include <linux/pinctrl/consumer.h>
Wenyou Yangce0c4ca2014-10-16 17:23:10 +080025#include <linux/pm_runtime.h>
Uwe Kleine-König3c0448d2019-08-01 22:47:10 +020026#include <trace/events/spi.h>
David Brownellbb2d1c32007-02-20 13:58:19 -080027
Grant Likelyca632f52011-06-06 01:16:30 -060028/* SPI register offsets */
29#define SPI_CR 0x0000
30#define SPI_MR 0x0004
31#define SPI_RDR 0x0008
32#define SPI_TDR 0x000c
33#define SPI_SR 0x0010
34#define SPI_IER 0x0014
35#define SPI_IDR 0x0018
36#define SPI_IMR 0x001c
37#define SPI_CSR0 0x0030
38#define SPI_CSR1 0x0034
39#define SPI_CSR2 0x0038
40#define SPI_CSR3 0x003c
Cyrille Pitchen11f27642015-06-16 12:09:31 +020041#define SPI_FMR 0x0040
42#define SPI_FLR 0x0044
Wenyou Yangd4820b72013-03-19 15:42:15 +080043#define SPI_VERSION 0x00fc
Grant Likelyca632f52011-06-06 01:16:30 -060044#define SPI_RPR 0x0100
45#define SPI_RCR 0x0104
46#define SPI_TPR 0x0108
47#define SPI_TCR 0x010c
48#define SPI_RNPR 0x0110
49#define SPI_RNCR 0x0114
50#define SPI_TNPR 0x0118
51#define SPI_TNCR 0x011c
52#define SPI_PTCR 0x0120
53#define SPI_PTSR 0x0124
54
55/* Bitfields in CR */
56#define SPI_SPIEN_OFFSET 0
57#define SPI_SPIEN_SIZE 1
58#define SPI_SPIDIS_OFFSET 1
59#define SPI_SPIDIS_SIZE 1
60#define SPI_SWRST_OFFSET 7
61#define SPI_SWRST_SIZE 1
62#define SPI_LASTXFER_OFFSET 24
63#define SPI_LASTXFER_SIZE 1
Cyrille Pitchen11f27642015-06-16 12:09:31 +020064#define SPI_TXFCLR_OFFSET 16
65#define SPI_TXFCLR_SIZE 1
66#define SPI_RXFCLR_OFFSET 17
67#define SPI_RXFCLR_SIZE 1
68#define SPI_FIFOEN_OFFSET 30
69#define SPI_FIFOEN_SIZE 1
70#define SPI_FIFODIS_OFFSET 31
71#define SPI_FIFODIS_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -060072
73/* Bitfields in MR */
74#define SPI_MSTR_OFFSET 0
75#define SPI_MSTR_SIZE 1
76#define SPI_PS_OFFSET 1
77#define SPI_PS_SIZE 1
78#define SPI_PCSDEC_OFFSET 2
79#define SPI_PCSDEC_SIZE 1
80#define SPI_FDIV_OFFSET 3
81#define SPI_FDIV_SIZE 1
82#define SPI_MODFDIS_OFFSET 4
83#define SPI_MODFDIS_SIZE 1
Wenyou Yangd4820b72013-03-19 15:42:15 +080084#define SPI_WDRBT_OFFSET 5
85#define SPI_WDRBT_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -060086#define SPI_LLB_OFFSET 7
87#define SPI_LLB_SIZE 1
88#define SPI_PCS_OFFSET 16
89#define SPI_PCS_SIZE 4
90#define SPI_DLYBCS_OFFSET 24
91#define SPI_DLYBCS_SIZE 8
92
93/* Bitfields in RDR */
94#define SPI_RD_OFFSET 0
95#define SPI_RD_SIZE 16
96
97/* Bitfields in TDR */
98#define SPI_TD_OFFSET 0
99#define SPI_TD_SIZE 16
100
101/* Bitfields in SR */
102#define SPI_RDRF_OFFSET 0
103#define SPI_RDRF_SIZE 1
104#define SPI_TDRE_OFFSET 1
105#define SPI_TDRE_SIZE 1
106#define SPI_MODF_OFFSET 2
107#define SPI_MODF_SIZE 1
108#define SPI_OVRES_OFFSET 3
109#define SPI_OVRES_SIZE 1
110#define SPI_ENDRX_OFFSET 4
111#define SPI_ENDRX_SIZE 1
112#define SPI_ENDTX_OFFSET 5
113#define SPI_ENDTX_SIZE 1
114#define SPI_RXBUFF_OFFSET 6
115#define SPI_RXBUFF_SIZE 1
116#define SPI_TXBUFE_OFFSET 7
117#define SPI_TXBUFE_SIZE 1
118#define SPI_NSSR_OFFSET 8
119#define SPI_NSSR_SIZE 1
120#define SPI_TXEMPTY_OFFSET 9
121#define SPI_TXEMPTY_SIZE 1
122#define SPI_SPIENS_OFFSET 16
123#define SPI_SPIENS_SIZE 1
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200124#define SPI_TXFEF_OFFSET 24
125#define SPI_TXFEF_SIZE 1
126#define SPI_TXFFF_OFFSET 25
127#define SPI_TXFFF_SIZE 1
128#define SPI_TXFTHF_OFFSET 26
129#define SPI_TXFTHF_SIZE 1
130#define SPI_RXFEF_OFFSET 27
131#define SPI_RXFEF_SIZE 1
132#define SPI_RXFFF_OFFSET 28
133#define SPI_RXFFF_SIZE 1
134#define SPI_RXFTHF_OFFSET 29
135#define SPI_RXFTHF_SIZE 1
136#define SPI_TXFPTEF_OFFSET 30
137#define SPI_TXFPTEF_SIZE 1
138#define SPI_RXFPTEF_OFFSET 31
139#define SPI_RXFPTEF_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -0600140
141/* Bitfields in CSR0 */
142#define SPI_CPOL_OFFSET 0
143#define SPI_CPOL_SIZE 1
144#define SPI_NCPHA_OFFSET 1
145#define SPI_NCPHA_SIZE 1
146#define SPI_CSAAT_OFFSET 3
147#define SPI_CSAAT_SIZE 1
148#define SPI_BITS_OFFSET 4
149#define SPI_BITS_SIZE 4
150#define SPI_SCBR_OFFSET 8
151#define SPI_SCBR_SIZE 8
152#define SPI_DLYBS_OFFSET 16
153#define SPI_DLYBS_SIZE 8
154#define SPI_DLYBCT_OFFSET 24
155#define SPI_DLYBCT_SIZE 8
156
157/* Bitfields in RCR */
158#define SPI_RXCTR_OFFSET 0
159#define SPI_RXCTR_SIZE 16
160
161/* Bitfields in TCR */
162#define SPI_TXCTR_OFFSET 0
163#define SPI_TXCTR_SIZE 16
164
165/* Bitfields in RNCR */
166#define SPI_RXNCR_OFFSET 0
167#define SPI_RXNCR_SIZE 16
168
169/* Bitfields in TNCR */
170#define SPI_TXNCR_OFFSET 0
171#define SPI_TXNCR_SIZE 16
172
173/* Bitfields in PTCR */
174#define SPI_RXTEN_OFFSET 0
175#define SPI_RXTEN_SIZE 1
176#define SPI_RXTDIS_OFFSET 1
177#define SPI_RXTDIS_SIZE 1
178#define SPI_TXTEN_OFFSET 8
179#define SPI_TXTEN_SIZE 1
180#define SPI_TXTDIS_OFFSET 9
181#define SPI_TXTDIS_SIZE 1
182
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200183/* Bitfields in FMR */
184#define SPI_TXRDYM_OFFSET 0
185#define SPI_TXRDYM_SIZE 2
186#define SPI_RXRDYM_OFFSET 4
187#define SPI_RXRDYM_SIZE 2
188#define SPI_TXFTHRES_OFFSET 16
189#define SPI_TXFTHRES_SIZE 6
190#define SPI_RXFTHRES_OFFSET 24
191#define SPI_RXFTHRES_SIZE 6
192
193/* Bitfields in FLR */
194#define SPI_TXFL_OFFSET 0
195#define SPI_TXFL_SIZE 6
196#define SPI_RXFL_OFFSET 16
197#define SPI_RXFL_SIZE 6
198
Grant Likelyca632f52011-06-06 01:16:30 -0600199/* Constants for BITS */
200#define SPI_BITS_8_BPT 0
201#define SPI_BITS_9_BPT 1
202#define SPI_BITS_10_BPT 2
203#define SPI_BITS_11_BPT 3
204#define SPI_BITS_12_BPT 4
205#define SPI_BITS_13_BPT 5
206#define SPI_BITS_14_BPT 6
207#define SPI_BITS_15_BPT 7
208#define SPI_BITS_16_BPT 8
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200209#define SPI_ONE_DATA 0
210#define SPI_TWO_DATA 1
211#define SPI_FOUR_DATA 2
Grant Likelyca632f52011-06-06 01:16:30 -0600212
213/* Bit manipulation macros */
214#define SPI_BIT(name) \
215 (1 << SPI_##name##_OFFSET)
Sachin Kamata536d762013-09-10 17:06:27 +0530216#define SPI_BF(name, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600217 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
Sachin Kamata536d762013-09-10 17:06:27 +0530218#define SPI_BFEXT(name, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600219 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
Sachin Kamata536d762013-09-10 17:06:27 +0530220#define SPI_BFINS(name, value, old) \
221 (((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
222 | SPI_BF(name, value))
Grant Likelyca632f52011-06-06 01:16:30 -0600223
224/* Register access macros */
Ben Dooksea467322015-03-18 15:53:08 +0000225#define spi_readl(port, reg) \
226 readl_relaxed((port)->regs + SPI_##reg)
227#define spi_writel(port, reg, value) \
228 writel_relaxed((value), (port)->regs + SPI_##reg)
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200229#define spi_writew(port, reg, value) \
230 writew_relaxed((value), (port)->regs + SPI_##reg)
231
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800232/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
233 * cache operations; better heuristics consider wordsize and bitrate.
234 */
235#define DMA_MIN_BYTES 16
236
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800237#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
238
Wenyou Yangce0c4ca2014-10-16 17:23:10 +0800239#define AUTOSUSPEND_TIMEOUT 2000
240
Wenyou Yangd4820b72013-03-19 15:42:15 +0800241struct atmel_spi_caps {
242 bool is_spi2;
243 bool has_wdrbt;
244 bool has_dma_support;
Cyrille Pitchen70945762017-06-23 17:39:16 +0200245 bool has_pdc_support;
Wenyou Yangd4820b72013-03-19 15:42:15 +0800246};
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800247
248/*
249 * The core SPI transfer engine just talks to a register bank to set up
250 * DMA transfers; transfer queue progress is driven by IRQs. The clock
251 * framework provides the base clock, subdivided for each spi_device.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800252 */
253struct atmel_spi {
254 spinlock_t lock;
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800255 unsigned long flags;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800256
Nicolas Ferredfab30e2013-04-03 13:57:42 +0800257 phys_addr_t phybase;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800258 void __iomem *regs;
259 int irq;
260 struct clk *clk;
261 struct platform_device *pdev;
Ben Whitten39fe33f2016-11-14 15:13:20 +0000262 unsigned long spi_clk;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800263
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800264 struct spi_transfer *current_transfer;
Axel Lin0c3b9742014-03-27 09:26:38 +0800265 int current_remaining_bytes;
Nicolas Ferre823cd042013-03-19 15:45:01 +0800266 int done_status;
Radu Pireaa9889ed2017-12-19 17:17:59 +0200267 dma_addr_t dma_addr_rx_bbuf;
268 dma_addr_t dma_addr_tx_bbuf;
269 void *addr_rx_bbuf;
270 void *addr_tx_bbuf;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800271
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800272 struct completion xfer_completion;
273
Wenyou Yangd4820b72013-03-19 15:42:15 +0800274 struct atmel_spi_caps caps;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800275
276 bool use_dma;
277 bool use_pdc;
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800278
279 bool keep_cs;
280 bool cs_active;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200281
282 u32 fifo_size;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800283};
284
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800285/* Controller-specific per-slave state */
286struct atmel_spi_device {
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800287 u32 csr;
288};
289
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100290#define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800291#define INVALID_DMA_ADDRESS 0xffffffff
292
293/*
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800294 * Version 2 of the SPI controller has
295 * - CR.LASTXFER
296 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
297 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
298 * - SPI_CSRx.CSAAT
299 * - SPI_CSRx.SBCR allows faster clocking
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800300 */
Wenyou Yangd4820b72013-03-19 15:42:15 +0800301static bool atmel_spi_is_v2(struct atmel_spi *as)
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800302{
Wenyou Yangd4820b72013-03-19 15:42:15 +0800303 return as->caps.is_spi2;
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800304}
305
306/*
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800307 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
308 * they assume that spi slave device state will not change on deselect, so
David Brownelldefbd3b2007-07-17 04:04:08 -0700309 * that automagic deselection is OK. ("NPCSx rises if no data is to be
310 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
311 * controllers have CSAAT and friends.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800312 *
Gregory CLEMENT4d8672d2019-10-17 16:18:40 +0200313 * Even controller newer than ar91rm9200, using GPIOs can make sens as
314 * it lets us support active-high chipselects despite the controller's
315 * belief that only active-low devices/systems exists.
David Brownelldefbd3b2007-07-17 04:04:08 -0700316 *
317 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
318 * right when driven with GPIO. ("Mode Fault does not allow more than one
319 * Master on Chip Select 0.") No workaround exists for that ... so for
320 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
321 * and (c) will trigger that first erratum in some cases.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800322 */
323
David Brownelldefbd3b2007-07-17 04:04:08 -0700324static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800325{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800326 struct atmel_spi_device *asd = spi->controller_state;
David Brownelldefbd3b2007-07-17 04:04:08 -0700327 u32 mr;
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800328
Wenyou Yangd4820b72013-03-19 15:42:15 +0800329 if (atmel_spi_is_v2(as)) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800330 spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
331 /* For the low SPI version, there is a issue that PDC transfer
332 * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800333 */
334 spi_writel(as, CSR0, asd->csr);
Wenyou Yangd4820b72013-03-19 15:42:15 +0800335 if (as->caps.has_wdrbt) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800336 spi_writel(as, MR,
337 SPI_BF(PCS, ~(0x01 << spi->chip_select))
338 | SPI_BIT(WDRBT)
339 | SPI_BIT(MODFDIS)
340 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800341 } else {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800342 spi_writel(as, MR,
343 SPI_BF(PCS, ~(0x01 << spi->chip_select))
344 | SPI_BIT(MODFDIS)
345 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800346 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800347
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800348 mr = spi_readl(as, MR);
Gregory CLEMENT60086e22019-10-17 16:18:43 +0200349 if (spi->cs_gpiod)
350 gpiod_set_value(spi->cs_gpiod, 1);
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800351 } else {
352 u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
353 int i;
354 u32 csr;
355
356 /* Make sure clock polarity is correct */
357 for (i = 0; i < spi->master->num_chipselect; i++) {
358 csr = spi_readl(as, CSR0 + 4 * i);
359 if ((csr ^ cpol) & SPI_BIT(CPOL))
360 spi_writel(as, CSR0 + 4 * i,
361 csr ^ SPI_BIT(CPOL));
362 }
363
364 mr = spi_readl(as, MR);
365 mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
Gregory CLEMENT60086e22019-10-17 16:18:43 +0200366 if (spi->cs_gpiod && spi->chip_select != 0)
367 gpiod_set_value(spi->cs_gpiod, 1);
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800368 spi_writel(as, MR, mr);
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800369 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800370
Linus Walleijefc92fb2019-01-07 16:51:52 +0100371 dev_dbg(&spi->dev, "activate NPCS, mr %08x\n", mr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800372}
373
David Brownelldefbd3b2007-07-17 04:04:08 -0700374static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800375{
David Brownelldefbd3b2007-07-17 04:04:08 -0700376 u32 mr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800377
David Brownelldefbd3b2007-07-17 04:04:08 -0700378 /* only deactivate *this* device; sometimes transfers to
379 * another device may be active when this routine is called.
380 */
381 mr = spi_readl(as, MR);
382 if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
383 mr = SPI_BFINS(PCS, 0xf, mr);
384 spi_writel(as, MR, mr);
385 }
386
Linus Walleijefc92fb2019-01-07 16:51:52 +0100387 dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr);
David Brownelldefbd3b2007-07-17 04:04:08 -0700388
Gregory CLEMENT60086e22019-10-17 16:18:43 +0200389 if (!spi->cs_gpiod)
Cyrille Pitchen48203032015-06-09 13:53:52 +0200390 spi_writel(as, CR, SPI_BIT(LASTXFER));
391 else if (atmel_spi_is_v2(as) || spi->chip_select != 0)
Gregory CLEMENT60086e22019-10-17 16:18:43 +0200392 gpiod_set_value(spi->cs_gpiod, 0);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800393}
394
Mark Brown6c07ef22013-07-28 14:32:27 +0100395static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock)
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800396{
397 spin_lock_irqsave(&as->lock, as->flags);
398}
399
Mark Brown6c07ef22013-07-28 14:32:27 +0100400static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock)
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800401{
402 spin_unlock_irqrestore(&as->lock, as->flags);
403}
404
Radu Pireaa9889ed2017-12-19 17:17:59 +0200405static inline bool atmel_spi_is_vmalloc_xfer(struct spi_transfer *xfer)
406{
407 return is_vmalloc_addr(xfer->tx_buf) || is_vmalloc_addr(xfer->rx_buf);
408}
409
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800410static inline bool atmel_spi_use_dma(struct atmel_spi *as,
411 struct spi_transfer *xfer)
412{
413 return as->use_dma && xfer->len >= DMA_MIN_BYTES;
414}
415
Cyrille Pitchen04242ca2016-11-24 12:24:59 +0100416static bool atmel_spi_can_dma(struct spi_master *master,
417 struct spi_device *spi,
418 struct spi_transfer *xfer)
419{
420 struct atmel_spi *as = spi_master_get_devdata(master);
421
Radu Pireaa9889ed2017-12-19 17:17:59 +0200422 if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5))
423 return atmel_spi_use_dma(as, xfer) &&
424 !atmel_spi_is_vmalloc_xfer(xfer);
425 else
426 return atmel_spi_use_dma(as, xfer);
427
Cyrille Pitchen04242ca2016-11-24 12:24:59 +0100428}
429
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800430static int atmel_spi_dma_slave_config(struct atmel_spi *as,
431 struct dma_slave_config *slave_config,
432 u8 bits_per_word)
433{
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100434 struct spi_master *master = platform_get_drvdata(as->pdev);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800435 int err = 0;
436
437 if (bits_per_word > 8) {
438 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
439 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
440 } else {
441 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
442 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
443 }
444
445 slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
446 slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
447 slave_config->src_maxburst = 1;
448 slave_config->dst_maxburst = 1;
449 slave_config->device_fc = false;
450
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200451 /*
452 * This driver uses fixed peripheral select mode (PS bit set to '0' in
453 * the Mode Register).
454 * So according to the datasheet, when FIFOs are available (and
455 * enabled), the Transmit FIFO operates in Multiple Data Mode.
456 * In this mode, up to 2 data, not 4, can be written into the Transmit
457 * Data Register in a single access.
458 * However, the first data has to be written into the lowest 16 bits and
459 * the second data into the highest 16 bits of the Transmit
460 * Data Register. For 8bit data (the most frequent case), it would
461 * require to rework tx_buf so each data would actualy fit 16 bits.
462 * So we'd rather write only one data at the time. Hence the transmit
463 * path works the same whether FIFOs are available (and enabled) or not.
464 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800465 slave_config->direction = DMA_MEM_TO_DEV;
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100466 if (dmaengine_slave_config(master->dma_tx, slave_config)) {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800467 dev_err(&as->pdev->dev,
468 "failed to configure tx dma channel\n");
469 err = -EINVAL;
470 }
471
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200472 /*
473 * This driver configures the spi controller for master mode (MSTR bit
474 * set to '1' in the Mode Register).
475 * So according to the datasheet, when FIFOs are available (and
476 * enabled), the Receive FIFO operates in Single Data Mode.
477 * So the receive path works the same whether FIFOs are available (and
478 * enabled) or not.
479 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800480 slave_config->direction = DMA_DEV_TO_MEM;
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100481 if (dmaengine_slave_config(master->dma_rx, slave_config)) {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800482 dev_err(&as->pdev->dev,
483 "failed to configure rx dma channel\n");
484 err = -EINVAL;
485 }
486
487 return err;
488}
489
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100490static int atmel_spi_configure_dma(struct spi_master *master,
491 struct atmel_spi *as)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800492{
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800493 struct dma_slave_config slave_config;
Richard Genoud2f767a92013-05-31 17:01:59 +0200494 struct device *dev = &as->pdev->dev;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800495 int err;
496
Richard Genoud2f767a92013-05-31 17:01:59 +0200497 dma_cap_mask_t mask;
498 dma_cap_zero(mask);
499 dma_cap_set(DMA_SLAVE, mask);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800500
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100501 master->dma_tx = dma_request_slave_channel_reason(dev, "tx");
502 if (IS_ERR(master->dma_tx)) {
503 err = PTR_ERR(master->dma_tx);
Ludovic Desroches5e9af372014-11-14 17:12:54 +0100504 if (err == -EPROBE_DEFER) {
505 dev_warn(dev, "no DMA channel available at the moment\n");
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100506 goto error_clear;
Ludovic Desroches5e9af372014-11-14 17:12:54 +0100507 }
Richard Genoud2f767a92013-05-31 17:01:59 +0200508 dev_err(dev,
509 "DMA TX channel not available, SPI unable to use DMA\n");
510 err = -EBUSY;
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100511 goto error_clear;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800512 }
Richard Genoud2f767a92013-05-31 17:01:59 +0200513
Ludovic Desroches5e9af372014-11-14 17:12:54 +0100514 /*
515 * No reason to check EPROBE_DEFER here since we have already requested
516 * tx channel. If it fails here, it's for another reason.
517 */
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100518 master->dma_rx = dma_request_slave_channel(dev, "rx");
Richard Genoud2f767a92013-05-31 17:01:59 +0200519
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100520 if (!master->dma_rx) {
Richard Genoud2f767a92013-05-31 17:01:59 +0200521 dev_err(dev,
522 "DMA RX channel not available, SPI unable to use DMA\n");
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800523 err = -EBUSY;
524 goto error;
525 }
526
527 err = atmel_spi_dma_slave_config(as, &slave_config, 8);
528 if (err)
529 goto error;
530
531 dev_info(&as->pdev->dev,
532 "Using %s (tx) and %s (rx) for DMA transfers\n",
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100533 dma_chan_name(master->dma_tx),
534 dma_chan_name(master->dma_rx));
535
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800536 return 0;
537error:
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100538 if (master->dma_rx)
539 dma_release_channel(master->dma_rx);
540 if (!IS_ERR(master->dma_tx))
541 dma_release_channel(master->dma_tx);
542error_clear:
543 master->dma_tx = master->dma_rx = NULL;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800544 return err;
545}
546
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100547static void atmel_spi_stop_dma(struct spi_master *master)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800548{
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100549 if (master->dma_rx)
550 dmaengine_terminate_all(master->dma_rx);
551 if (master->dma_tx)
552 dmaengine_terminate_all(master->dma_tx);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800553}
554
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100555static void atmel_spi_release_dma(struct spi_master *master)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800556{
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100557 if (master->dma_rx) {
558 dma_release_channel(master->dma_rx);
559 master->dma_rx = NULL;
560 }
561 if (master->dma_tx) {
562 dma_release_channel(master->dma_tx);
563 master->dma_tx = NULL;
564 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800565}
566
567/* This function is called by the DMA driver from tasklet context */
568static void dma_callback(void *data)
569{
570 struct spi_master *master = data;
571 struct atmel_spi *as = spi_master_get_devdata(master);
572
Radu Pireaa9889ed2017-12-19 17:17:59 +0200573 if (is_vmalloc_addr(as->current_transfer->rx_buf) &&
574 IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
575 memcpy(as->current_transfer->rx_buf, as->addr_rx_bbuf,
576 as->current_transfer->len);
577 }
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800578 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800579}
580
581/*
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200582 * Next transfer using PIO without FIFO.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800583 */
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200584static void atmel_spi_next_xfer_single(struct spi_master *master,
585 struct spi_transfer *xfer)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800586{
587 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800588 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800589
590 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n");
591
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800592 /* Make sure data is not remaining in RDR */
593 spi_readl(as, RDR);
594 while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
595 spi_readl(as, RDR);
596 cpu_relax();
597 }
598
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100599 if (xfer->bits_per_word > 8)
600 spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos));
601 else
602 spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos));
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800603
604 dev_dbg(master->dev.parent,
Richard Genoudf557c982013-05-02 19:25:11 +0800605 " start pio xfer %p: len %u tx %p rx %p bitpw %d\n",
606 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
607 xfer->bits_per_word);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800608
609 /* Enable relevant interrupts */
610 spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
611}
612
613/*
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200614 * Next transfer using PIO with FIFO.
615 */
616static void atmel_spi_next_xfer_fifo(struct spi_master *master,
617 struct spi_transfer *xfer)
618{
619 struct atmel_spi *as = spi_master_get_devdata(master);
620 u32 current_remaining_data, num_data;
621 u32 offset = xfer->len - as->current_remaining_bytes;
622 const u16 *words = (const u16 *)((u8 *)xfer->tx_buf + offset);
623 const u8 *bytes = (const u8 *)((u8 *)xfer->tx_buf + offset);
624 u16 td0, td1;
625 u32 fifomr;
626
627 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_fifo\n");
628
629 /* Compute the number of data to transfer in the current iteration */
630 current_remaining_data = ((xfer->bits_per_word > 8) ?
631 ((u32)as->current_remaining_bytes >> 1) :
632 (u32)as->current_remaining_bytes);
633 num_data = min(current_remaining_data, as->fifo_size);
634
635 /* Flush RX and TX FIFOs */
636 spi_writel(as, CR, SPI_BIT(RXFCLR) | SPI_BIT(TXFCLR));
637 while (spi_readl(as, FLR))
638 cpu_relax();
639
640 /* Set RX FIFO Threshold to the number of data to transfer */
641 fifomr = spi_readl(as, FMR);
642 spi_writel(as, FMR, SPI_BFINS(RXFTHRES, num_data, fifomr));
643
644 /* Clear FIFO flags in the Status Register, especially RXFTHF */
645 (void)spi_readl(as, SR);
646
647 /* Fill TX FIFO */
648 while (num_data >= 2) {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100649 if (xfer->bits_per_word > 8) {
650 td0 = *words++;
651 td1 = *words++;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200652 } else {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100653 td0 = *bytes++;
654 td1 = *bytes++;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200655 }
656
657 spi_writel(as, TDR, (td1 << 16) | td0);
658 num_data -= 2;
659 }
660
661 if (num_data) {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100662 if (xfer->bits_per_word > 8)
663 td0 = *words++;
664 else
665 td0 = *bytes++;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200666
667 spi_writew(as, TDR, td0);
668 num_data--;
669 }
670
671 dev_dbg(master->dev.parent,
672 " start fifo xfer %p: len %u tx %p rx %p bitpw %d\n",
673 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
674 xfer->bits_per_word);
675
676 /*
677 * Enable RX FIFO Threshold Flag interrupt to be notified about
678 * transfer completion.
679 */
680 spi_writel(as, IER, SPI_BIT(RXFTHF) | SPI_BIT(OVRES));
681}
682
683/*
684 * Next transfer using PIO.
685 */
686static void atmel_spi_next_xfer_pio(struct spi_master *master,
687 struct spi_transfer *xfer)
688{
689 struct atmel_spi *as = spi_master_get_devdata(master);
690
691 if (as->fifo_size)
692 atmel_spi_next_xfer_fifo(master, xfer);
693 else
694 atmel_spi_next_xfer_single(master, xfer);
695}
696
697/*
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800698 * Submit next transfer for DMA.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800699 */
700static int atmel_spi_next_xfer_dma_submit(struct spi_master *master,
701 struct spi_transfer *xfer,
702 u32 *plen)
703{
704 struct atmel_spi *as = spi_master_get_devdata(master);
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100705 struct dma_chan *rxchan = master->dma_rx;
706 struct dma_chan *txchan = master->dma_tx;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800707 struct dma_async_tx_descriptor *rxdesc;
708 struct dma_async_tx_descriptor *txdesc;
709 struct dma_slave_config slave_config;
710 dma_cookie_t cookie;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800711
712 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n");
713
714 /* Check that the channels are available */
715 if (!rxchan || !txchan)
716 return -ENODEV;
717
718 /* release lock for DMA operations */
719 atmel_spi_unlock(as);
720
Cyrille Pitchen04242ca2016-11-24 12:24:59 +0100721 *plen = xfer->len;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800722
David Mosberger-Tang06515f82015-10-20 14:26:47 +0200723 if (atmel_spi_dma_slave_config(as, &slave_config,
724 xfer->bits_per_word))
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800725 goto err_exit;
726
727 /* Send both scatterlists */
Radu Pireaa9889ed2017-12-19 17:17:59 +0200728 if (atmel_spi_is_vmalloc_xfer(xfer) &&
729 IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
730 rxdesc = dmaengine_prep_slave_single(rxchan,
731 as->dma_addr_rx_bbuf,
732 xfer->len,
Stefan Agner35732572018-03-24 11:48:00 +0100733 DMA_DEV_TO_MEM,
Radu Pireaa9889ed2017-12-19 17:17:59 +0200734 DMA_PREP_INTERRUPT |
735 DMA_CTRL_ACK);
736 } else {
737 rxdesc = dmaengine_prep_slave_sg(rxchan,
738 xfer->rx_sg.sgl,
739 xfer->rx_sg.nents,
Stefan Agner35732572018-03-24 11:48:00 +0100740 DMA_DEV_TO_MEM,
Radu Pireaa9889ed2017-12-19 17:17:59 +0200741 DMA_PREP_INTERRUPT |
742 DMA_CTRL_ACK);
743 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800744 if (!rxdesc)
745 goto err_dma;
746
Radu Pireaa9889ed2017-12-19 17:17:59 +0200747 if (atmel_spi_is_vmalloc_xfer(xfer) &&
748 IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
749 memcpy(as->addr_tx_bbuf, xfer->tx_buf, xfer->len);
750 txdesc = dmaengine_prep_slave_single(txchan,
751 as->dma_addr_tx_bbuf,
Stefan Agner35732572018-03-24 11:48:00 +0100752 xfer->len, DMA_MEM_TO_DEV,
Radu Pireaa9889ed2017-12-19 17:17:59 +0200753 DMA_PREP_INTERRUPT |
754 DMA_CTRL_ACK);
755 } else {
756 txdesc = dmaengine_prep_slave_sg(txchan,
757 xfer->tx_sg.sgl,
758 xfer->tx_sg.nents,
Stefan Agner35732572018-03-24 11:48:00 +0100759 DMA_MEM_TO_DEV,
Radu Pireaa9889ed2017-12-19 17:17:59 +0200760 DMA_PREP_INTERRUPT |
761 DMA_CTRL_ACK);
762 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800763 if (!txdesc)
764 goto err_dma;
765
766 dev_dbg(master->dev.parent,
Emil Goode2de024b2013-07-30 19:35:35 +0200767 " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
768 xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
769 xfer->rx_buf, (unsigned long long)xfer->rx_dma);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800770
771 /* Enable relevant interrupts */
772 spi_writel(as, IER, SPI_BIT(OVRES));
773
774 /* Put the callback on the RX transfer only, that should finish last */
775 rxdesc->callback = dma_callback;
776 rxdesc->callback_param = master;
777
778 /* Submit and fire RX and TX with TX last so we're ready to read! */
779 cookie = rxdesc->tx_submit(rxdesc);
780 if (dma_submit_error(cookie))
781 goto err_dma;
782 cookie = txdesc->tx_submit(txdesc);
783 if (dma_submit_error(cookie))
784 goto err_dma;
785 rxchan->device->device_issue_pending(rxchan);
786 txchan->device->device_issue_pending(txchan);
787
788 /* take back lock */
789 atmel_spi_lock(as);
790 return 0;
791
792err_dma:
793 spi_writel(as, IDR, SPI_BIT(OVRES));
Nicolas Ferre768f3d92016-11-24 12:25:01 +0100794 atmel_spi_stop_dma(master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800795err_exit:
796 atmel_spi_lock(as);
797 return -ENOMEM;
798}
799
Silvester Erdeg154443c2008-02-06 01:38:12 -0800800static void atmel_spi_next_xfer_data(struct spi_master *master,
801 struct spi_transfer *xfer,
802 dma_addr_t *tx_dma,
803 dma_addr_t *rx_dma,
804 u32 *plen)
805{
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100806 *rx_dma = xfer->rx_dma + xfer->len - *plen;
807 *tx_dma = xfer->tx_dma + xfer->len - *plen;
Cyrille Pitchen04242ca2016-11-24 12:24:59 +0100808 if (*plen > master->max_dma_len)
809 *plen = master->max_dma_len;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800810}
811
Richard Genoudd3b72c72013-11-07 10:34:06 +0100812static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
813 struct spi_device *spi,
814 struct spi_transfer *xfer)
815{
816 u32 scbr, csr;
817 unsigned long bus_hz;
818
819 /* v1 chips start out at half the peripheral bus speed. */
Ben Whitten39fe33f2016-11-14 15:13:20 +0000820 bus_hz = as->spi_clk;
Richard Genoudd3b72c72013-11-07 10:34:06 +0100821 if (!atmel_spi_is_v2(as))
822 bus_hz /= 2;
823
824 /*
825 * Calculate the lowest divider that satisfies the
826 * constraint, assuming div32/fdiv/mbz == 0.
827 */
Jarkko Nikulae8646582015-09-25 09:03:01 +0300828 scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
Richard Genoudd3b72c72013-11-07 10:34:06 +0100829
830 /*
831 * If the resulting divider doesn't fit into the
832 * register bitfield, we can't satisfy the constraint.
833 */
834 if (scbr >= (1 << SPI_SCBR_SIZE)) {
835 dev_err(&spi->dev,
836 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
837 xfer->speed_hz, scbr, bus_hz/255);
838 return -EINVAL;
839 }
840 if (scbr == 0) {
841 dev_err(&spi->dev,
842 "setup: %d Hz too high, scbr %u; max %ld Hz\n",
843 xfer->speed_hz, scbr, bus_hz);
844 return -EINVAL;
845 }
846 csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
847 csr = SPI_BFINS(SCBR, scbr, csr);
848 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
849
850 return 0;
851}
852
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800853/*
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800854 * Submit next transfer for PDC.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800855 * lock is held, spi irq is blocked
856 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800857static void atmel_spi_pdc_next_xfer(struct spi_master *master,
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800858 struct spi_message *msg,
859 struct spi_transfer *xfer)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800860{
861 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800862 u32 len;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800863 dma_addr_t tx_dma, rx_dma;
864
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800865 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
Silvester Erdeg154443c2008-02-06 01:38:12 -0800866
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800867 len = as->current_remaining_bytes;
868 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
869 as->current_remaining_bytes -= len;
Gerard Kamdc329442008-08-04 13:41:12 -0700870
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800871 spi_writel(as, RPR, rx_dma);
872 spi_writel(as, TPR, tx_dma);
873
874 if (msg->spi->bits_per_word > 8)
875 len >>= 1;
876 spi_writel(as, RCR, len);
877 spi_writel(as, TCR, len);
878
879 dev_dbg(&msg->spi->dev,
880 " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
881 xfer, xfer->len, xfer->tx_buf,
882 (unsigned long long)xfer->tx_dma, xfer->rx_buf,
883 (unsigned long long)xfer->rx_dma);
884
885 if (as->current_remaining_bytes) {
886 len = as->current_remaining_bytes;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800887 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800888 as->current_remaining_bytes -= len;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800889
890 spi_writel(as, RNPR, rx_dma);
891 spi_writel(as, TNPR, tx_dma);
892
893 if (msg->spi->bits_per_word > 8)
894 len >>= 1;
895 spi_writel(as, RNCR, len);
896 spi_writel(as, TNCR, len);
Haavard Skinnemoen8bacb212008-02-06 01:38:13 -0800897
898 dev_dbg(&msg->spi->dev,
Emil Goode2de024b2013-07-30 19:35:35 +0200899 " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
900 xfer, xfer->len, xfer->tx_buf,
901 (unsigned long long)xfer->tx_dma, xfer->rx_buf,
902 (unsigned long long)xfer->rx_dma);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800903 }
904
Torsten Fleischer76e1d142015-02-24 16:32:57 +0100905 /* REVISIT: We're waiting for RXBUFF before we start the next
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800906 * transfer because we need to handle some difficult timing
Torsten Fleischer76e1d142015-02-24 16:32:57 +0100907 * issues otherwise. If we wait for TXBUFE in one transfer and
908 * then starts waiting for RXBUFF in the next, it's difficult
909 * to tell the difference between the RXBUFF interrupt we're
910 * actually waiting for and the RXBUFF interrupt of the
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800911 * previous transfer.
912 *
913 * It should be doable, though. Just not now...
914 */
Torsten Fleischer76e1d142015-02-24 16:32:57 +0100915 spi_writel(as, IER, SPI_BIT(RXBUFF) | SPI_BIT(OVRES));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800916 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
917}
918
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800919/*
David Brownell8da08592007-07-17 04:04:07 -0700920 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
921 * - The buffer is either valid for CPU access, else NULL
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400922 * - If the buffer is valid, so is its DMA address
David Brownell8da08592007-07-17 04:04:07 -0700923 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400924 * This driver manages the dma address unless message->is_dma_mapped.
David Brownell8da08592007-07-17 04:04:07 -0700925 */
926static int
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800927atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
928{
David Brownell8da08592007-07-17 04:04:07 -0700929 struct device *dev = &as->pdev->dev;
930
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800931 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
David Brownell8da08592007-07-17 04:04:07 -0700932 if (xfer->tx_buf) {
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800933 /* tx_buf is a const void* where we need a void * for the dma
934 * mapping */
935 void *nonconst_tx = (void *)xfer->tx_buf;
936
David Brownell8da08592007-07-17 04:04:07 -0700937 xfer->tx_dma = dma_map_single(dev,
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800938 nonconst_tx, xfer->len,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800939 DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700940 if (dma_mapping_error(dev, xfer->tx_dma))
David Brownell8da08592007-07-17 04:04:07 -0700941 return -ENOMEM;
942 }
943 if (xfer->rx_buf) {
944 xfer->rx_dma = dma_map_single(dev,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800945 xfer->rx_buf, xfer->len,
946 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700947 if (dma_mapping_error(dev, xfer->rx_dma)) {
David Brownell8da08592007-07-17 04:04:07 -0700948 if (xfer->tx_buf)
949 dma_unmap_single(dev,
950 xfer->tx_dma, xfer->len,
951 DMA_TO_DEVICE);
952 return -ENOMEM;
953 }
954 }
955 return 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800956}
957
958static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
959 struct spi_transfer *xfer)
960{
961 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700962 dma_unmap_single(master->dev.parent, xfer->tx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800963 xfer->len, DMA_TO_DEVICE);
964 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700965 dma_unmap_single(master->dev.parent, xfer->rx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800966 xfer->len, DMA_FROM_DEVICE);
967}
968
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800969static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
970{
971 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
972}
973
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800974static void
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200975atmel_spi_pump_single_data(struct atmel_spi *as, struct spi_transfer *xfer)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800976{
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800977 u8 *rxp;
Richard Genoudf557c982013-05-02 19:25:11 +0800978 u16 *rxp16;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800979 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
980
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100981 if (xfer->bits_per_word > 8) {
982 rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
983 *rxp16 = spi_readl(as, RDR);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800984 } else {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100985 rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
986 *rxp = spi_readl(as, RDR);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800987 }
Richard Genoudf557c982013-05-02 19:25:11 +0800988 if (xfer->bits_per_word > 8) {
Alexandre Bellonib112f052014-05-06 17:44:41 +0200989 if (as->current_remaining_bytes > 2)
990 as->current_remaining_bytes -= 2;
991 else
Richard Genoudf557c982013-05-02 19:25:11 +0800992 as->current_remaining_bytes = 0;
993 } else {
994 as->current_remaining_bytes--;
995 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800996}
997
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200998static void
999atmel_spi_pump_fifo_data(struct atmel_spi *as, struct spi_transfer *xfer)
1000{
1001 u32 fifolr = spi_readl(as, FLR);
1002 u32 num_bytes, num_data = SPI_BFEXT(RXFL, fifolr);
1003 u32 offset = xfer->len - as->current_remaining_bytes;
1004 u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset);
1005 u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset);
1006 u16 rd; /* RD field is the lowest 16 bits of RDR */
1007
1008 /* Update the number of remaining bytes to transfer */
1009 num_bytes = ((xfer->bits_per_word > 8) ?
1010 (num_data << 1) :
1011 num_data);
1012
1013 if (as->current_remaining_bytes > num_bytes)
1014 as->current_remaining_bytes -= num_bytes;
1015 else
1016 as->current_remaining_bytes = 0;
1017
1018 /* Handle odd number of bytes when data are more than 8bit width */
1019 if (xfer->bits_per_word > 8)
1020 as->current_remaining_bytes &= ~0x1;
1021
1022 /* Read data */
1023 while (num_data) {
1024 rd = spi_readl(as, RDR);
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001025 if (xfer->bits_per_word > 8)
1026 *words++ = rd;
1027 else
1028 *bytes++ = rd;
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001029 num_data--;
1030 }
1031}
1032
1033/* Called from IRQ
1034 *
1035 * Must update "current_remaining_bytes" to keep track of data
1036 * to transfer.
1037 */
1038static void
1039atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
1040{
1041 if (as->fifo_size)
1042 atmel_spi_pump_fifo_data(as, xfer);
1043 else
1044 atmel_spi_pump_single_data(as, xfer);
1045}
1046
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001047/* Interrupt
1048 *
1049 * No need for locking in this Interrupt handler: done_status is the
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001050 * only information modified.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001051 */
1052static irqreturn_t
1053atmel_spi_pio_interrupt(int irq, void *dev_id)
1054{
1055 struct spi_master *master = dev_id;
1056 struct atmel_spi *as = spi_master_get_devdata(master);
1057 u32 status, pending, imr;
1058 struct spi_transfer *xfer;
1059 int ret = IRQ_NONE;
1060
1061 imr = spi_readl(as, IMR);
1062 status = spi_readl(as, SR);
1063 pending = status & imr;
1064
1065 if (pending & SPI_BIT(OVRES)) {
1066 ret = IRQ_HANDLED;
1067 spi_writel(as, IDR, SPI_BIT(OVRES));
1068 dev_warn(master->dev.parent, "overrun\n");
1069
1070 /*
1071 * When we get an overrun, we disregard the current
1072 * transfer. Data will not be copied back from any
1073 * bounce buffer and msg->actual_len will not be
1074 * updated with the last xfer.
1075 *
1076 * We will also not process any remaning transfers in
1077 * the message.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001078 */
1079 as->done_status = -EIO;
1080 smp_wmb();
1081
1082 /* Clear any overrun happening while cleaning up */
1083 spi_readl(as, SR);
1084
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001085 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001086
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001087 } else if (pending & (SPI_BIT(RDRF) | SPI_BIT(RXFTHF))) {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001088 atmel_spi_lock(as);
1089
1090 if (as->current_remaining_bytes) {
1091 ret = IRQ_HANDLED;
1092 xfer = as->current_transfer;
1093 atmel_spi_pump_pio_data(as, xfer);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001094 if (!as->current_remaining_bytes)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001095 spi_writel(as, IDR, pending);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001096
1097 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001098 }
1099
1100 atmel_spi_unlock(as);
1101 } else {
1102 WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
1103 ret = IRQ_HANDLED;
1104 spi_writel(as, IDR, pending);
1105 }
1106
1107 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001108}
1109
1110static irqreturn_t
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001111atmel_spi_pdc_interrupt(int irq, void *dev_id)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001112{
1113 struct spi_master *master = dev_id;
1114 struct atmel_spi *as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001115 u32 status, pending, imr;
1116 int ret = IRQ_NONE;
1117
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001118 imr = spi_readl(as, IMR);
1119 status = spi_readl(as, SR);
1120 pending = status & imr;
1121
1122 if (pending & SPI_BIT(OVRES)) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001123
1124 ret = IRQ_HANDLED;
1125
Gerard Kamdc329442008-08-04 13:41:12 -07001126 spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001127 | SPI_BIT(OVRES)));
1128
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001129 /* Clear any overrun happening while cleaning up */
1130 spi_readl(as, SR);
1131
Nicolas Ferre823cd042013-03-19 15:45:01 +08001132 as->done_status = -EIO;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001133
1134 complete(&as->xfer_completion);
1135
Gerard Kamdc329442008-08-04 13:41:12 -07001136 } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001137 ret = IRQ_HANDLED;
1138
1139 spi_writel(as, IDR, pending);
1140
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001141 complete(&as->xfer_completion);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001142 }
1143
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001144 return ret;
1145}
1146
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03001147static int atmel_word_delay_csr(struct spi_device *spi, struct atmel_spi *as)
1148{
1149 struct spi_delay *delay = &spi->word_delay;
1150 u32 value = delay->value;
1151
1152 switch (delay->unit) {
1153 case SPI_DELAY_UNIT_NSECS:
1154 value /= 1000;
1155 break;
1156 case SPI_DELAY_UNIT_USECS:
1157 break;
1158 default:
1159 return -EINVAL;
1160 }
1161
1162 return (as->spi_clk / 1000000 * value) >> 5;
1163}
1164
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001165static int atmel_spi_setup(struct spi_device *spi)
1166{
1167 struct atmel_spi *as;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001168 struct atmel_spi_device *asd;
Richard Genoudd3b72c72013-11-07 10:34:06 +01001169 u32 csr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001170 unsigned int bits = spi->bits_per_word;
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03001171 int word_delay_csr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001172
1173 as = spi_master_get_devdata(spi->master);
1174
David Brownelldefbd3b2007-07-17 04:04:08 -07001175 /* see notes above re chipselect */
Gregory CLEMENT585d18f2019-10-17 16:18:42 +02001176 if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH)) {
Gregory CLEMENT7cbb16b2019-10-17 16:18:41 +02001177 dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n");
David Brownelldefbd3b2007-07-17 04:04:08 -07001178 return -EINVAL;
1179 }
1180
Richard Genoudd3b72c72013-11-07 10:34:06 +01001181 csr = SPI_BF(BITS, bits - 8);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001182 if (spi->mode & SPI_CPOL)
1183 csr |= SPI_BIT(CPOL);
1184 if (!(spi->mode & SPI_CPHA))
1185 csr |= SPI_BIT(NCPHA);
1186
Gregory CLEMENT585d18f2019-10-17 16:18:42 +02001187 if (!spi->cs_gpiod)
1188 csr |= SPI_BIT(CSAAT);
Haavard Skinnemoen1eed29d2008-02-06 01:38:11 -08001189 csr |= SPI_BF(DLYBS, 0);
Jonas Bonn473a78a2019-01-30 09:40:05 +01001190
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03001191 word_delay_csr = atmel_word_delay_csr(spi, as);
1192 if (word_delay_csr < 0)
1193 return word_delay_csr;
1194
Jonas Bonn473a78a2019-01-30 09:40:05 +01001195 /* DLYBCT adds delays between words. This is useful for slow devices
1196 * that need a bit of time to setup the next transfer.
1197 */
Alexandru Ardelean6c613f62019-09-26 13:51:35 +03001198 csr |= SPI_BF(DLYBCT, word_delay_csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001199
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001200 asd = spi->controller_state;
1201 if (!asd) {
1202 asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
1203 if (!asd)
1204 return -ENOMEM;
1205
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001206 spi->controller_state = asd;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001207 }
1208
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001209 asd->csr = csr;
1210
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001211 dev_dbg(&spi->dev,
Richard Genoudd3b72c72013-11-07 10:34:06 +01001212 "setup: bpw %u mode 0x%x -> csr%d %08x\n",
1213 bits, spi->mode, spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001214
Wenyou Yangd4820b72013-03-19 15:42:15 +08001215 if (!atmel_spi_is_v2(as))
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001216 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001217
1218 return 0;
1219}
1220
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001221static int atmel_spi_one_transfer(struct spi_master *master,
1222 struct spi_message *msg,
1223 struct spi_transfer *xfer)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001224{
1225 struct atmel_spi *as;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001226 struct spi_device *spi = msg->spi;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +02001227 u8 bits;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001228 u32 len;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +02001229 struct atmel_spi_device *asd;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001230 int timeout;
1231 int ret;
Nicholas Mc Guire1369dea2015-02-02 10:43:31 -05001232 unsigned long dma_timeout;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001233
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001234 as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001235
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001236 if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1237 dev_dbg(&spi->dev, "missing rx or tx buf\n");
1238 return -EINVAL;
1239 }
1240
Jarkko Nikulae8646582015-09-25 09:03:01 +03001241 asd = spi->controller_state;
1242 bits = (asd->csr >> 4) & 0xf;
1243 if (bits != xfer->bits_per_word - 8) {
1244 dev_dbg(&spi->dev,
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001245 "you can't yet change bits_per_word in transfers\n");
Jarkko Nikulae8646582015-09-25 09:03:01 +03001246 return -ENOPROTOOPT;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001247 }
1248
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001249 /*
1250 * DMA map early, for performance (empties dcache ASAP) and
1251 * better fault reporting.
1252 */
1253 if ((!msg->is_dma_mapped)
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001254 && as->use_pdc) {
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001255 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
1256 return -ENOMEM;
1257 }
1258
1259 atmel_spi_set_xfer_speed(as, msg->spi, xfer);
1260
1261 as->done_status = 0;
1262 as->current_transfer = xfer;
1263 as->current_remaining_bytes = xfer->len;
1264 while (as->current_remaining_bytes) {
1265 reinit_completion(&as->xfer_completion);
1266
1267 if (as->use_pdc) {
1268 atmel_spi_pdc_next_xfer(master, msg, xfer);
1269 } else if (atmel_spi_use_dma(as, xfer)) {
1270 len = as->current_remaining_bytes;
1271 ret = atmel_spi_next_xfer_dma_submit(master,
1272 xfer, &len);
1273 if (ret) {
1274 dev_err(&spi->dev,
1275 "unable to use DMA, fallback to PIO\n");
1276 atmel_spi_next_xfer_pio(master, xfer);
1277 } else {
1278 as->current_remaining_bytes -= len;
Axel Lin0c3b9742014-03-27 09:26:38 +08001279 if (as->current_remaining_bytes < 0)
1280 as->current_remaining_bytes = 0;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001281 }
1282 } else {
1283 atmel_spi_next_xfer_pio(master, xfer);
1284 }
1285
Alexander Stein16760142014-04-13 12:45:10 +02001286 /* interrupts are disabled, so free the lock for schedule */
1287 atmel_spi_unlock(as);
Nicholas Mc Guire1369dea2015-02-02 10:43:31 -05001288 dma_timeout = wait_for_completion_timeout(&as->xfer_completion,
1289 SPI_DMA_TIMEOUT);
Alexander Stein16760142014-04-13 12:45:10 +02001290 atmel_spi_lock(as);
Nicholas Mc Guire1369dea2015-02-02 10:43:31 -05001291 if (WARN_ON(dma_timeout == 0)) {
1292 dev_err(&spi->dev, "spi transfer timeout\n");
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001293 as->done_status = -EIO;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001294 }
1295
1296 if (as->done_status)
1297 break;
1298 }
1299
1300 if (as->done_status) {
1301 if (as->use_pdc) {
1302 dev_warn(master->dev.parent,
1303 "overrun (%u/%u remaining)\n",
1304 spi_readl(as, TCR), spi_readl(as, RCR));
1305
1306 /*
1307 * Clean up DMA registers and make sure the data
1308 * registers are empty.
1309 */
1310 spi_writel(as, RNCR, 0);
1311 spi_writel(as, TNCR, 0);
1312 spi_writel(as, RCR, 0);
1313 spi_writel(as, TCR, 0);
1314 for (timeout = 1000; timeout; timeout--)
1315 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
1316 break;
1317 if (!timeout)
1318 dev_warn(master->dev.parent,
1319 "timeout waiting for TXEMPTY");
1320 while (spi_readl(as, SR) & SPI_BIT(RDRF))
1321 spi_readl(as, RDR);
1322
1323 /* Clear any overrun happening while cleaning up */
1324 spi_readl(as, SR);
1325
1326 } else if (atmel_spi_use_dma(as, xfer)) {
Nicolas Ferre768f3d92016-11-24 12:25:01 +01001327 atmel_spi_stop_dma(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001328 }
1329
1330 if (!msg->is_dma_mapped
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001331 && as->use_pdc)
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001332 atmel_spi_dma_unmap_xfer(master, xfer);
1333
1334 return 0;
1335
1336 } else {
1337 /* only update length if no error */
1338 msg->actual_length += xfer->len;
1339 }
1340
1341 if (!msg->is_dma_mapped
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001342 && as->use_pdc)
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001343 atmel_spi_dma_unmap_xfer(master, xfer);
1344
Alexandru Ardeleane74dc5c2019-09-26 13:51:37 +03001345 spi_transfer_delay_exec(xfer);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001346
1347 if (xfer->cs_change) {
1348 if (list_is_last(&xfer->transfer_list,
1349 &msg->transfers)) {
1350 as->keep_cs = true;
1351 } else {
1352 as->cs_active = !as->cs_active;
1353 if (as->cs_active)
1354 cs_activate(as, msg->spi);
1355 else
1356 cs_deactivate(as, msg->spi);
1357 }
1358 }
1359
1360 return 0;
1361}
1362
1363static int atmel_spi_transfer_one_message(struct spi_master *master,
1364 struct spi_message *msg)
1365{
1366 struct atmel_spi *as;
1367 struct spi_transfer *xfer;
1368 struct spi_device *spi = msg->spi;
1369 int ret = 0;
1370
1371 as = spi_master_get_devdata(master);
1372
1373 dev_dbg(&spi->dev, "new message %p submitted for %s\n",
1374 msg, dev_name(&spi->dev));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001375
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001376 atmel_spi_lock(as);
1377 cs_activate(as, spi);
1378
1379 as->cs_active = true;
1380 as->keep_cs = false;
1381
1382 msg->status = 0;
1383 msg->actual_length = 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001384
1385 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Uwe Kleine-König3c0448d2019-08-01 22:47:10 +02001386 trace_spi_transfer_start(msg, xfer);
1387
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001388 ret = atmel_spi_one_transfer(master, msg, xfer);
1389 if (ret)
1390 goto msg_done;
Uwe Kleine-König3c0448d2019-08-01 22:47:10 +02001391
1392 trace_spi_transfer_stop(msg, xfer);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001393 }
1394
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001395 if (as->use_pdc)
1396 atmel_spi_disable_pdc_transfer(as);
1397
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001398 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001399 dev_dbg(&spi->dev,
Randy Dunlap54f4c512014-03-21 08:53:41 -07001400 " xfer %p: len %u tx %p/%pad rx %p/%pad\n",
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001401 xfer, xfer->len,
Randy Dunlap54f4c512014-03-21 08:53:41 -07001402 xfer->tx_buf, &xfer->tx_dma,
1403 xfer->rx_buf, &xfer->rx_dma);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001404 }
1405
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001406msg_done:
1407 if (!as->keep_cs)
1408 cs_deactivate(as, msg->spi);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001409
Nicolas Ferre8aad7922013-04-03 13:58:36 +08001410 atmel_spi_unlock(as);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001411
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001412 msg->status = as->done_status;
1413 spi_finalize_current_message(spi->master);
1414
1415 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001416}
1417
David Brownellbb2d1c32007-02-20 13:58:19 -08001418static void atmel_spi_cleanup(struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001419{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001420 struct atmel_spi_device *asd = spi->controller_state;
David Brownelldefbd3b2007-07-17 04:04:08 -07001421
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001422 if (!asd)
David Brownelldefbd3b2007-07-17 04:04:08 -07001423 return;
1424
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001425 spi->controller_state = NULL;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001426 kfree(asd);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001427}
1428
Wenyou Yangd4820b72013-03-19 15:42:15 +08001429static inline unsigned int atmel_get_version(struct atmel_spi *as)
1430{
1431 return spi_readl(as, VERSION) & 0x00000fff;
1432}
1433
1434static void atmel_get_caps(struct atmel_spi *as)
1435{
1436 unsigned int version;
1437
1438 version = atmel_get_version(as);
Wenyou Yangd4820b72013-03-19 15:42:15 +08001439
1440 as->caps.is_spi2 = version > 0x121;
1441 as->caps.has_wdrbt = version >= 0x210;
1442 as->caps.has_dma_support = version >= 0x212;
Cyrille Pitchen70945762017-06-23 17:39:16 +02001443 as->caps.has_pdc_support = version < 0x212;
Wenyou Yangd4820b72013-03-19 15:42:15 +08001444}
1445
Quentin Schulz05514c82017-04-12 09:05:19 +02001446static void atmel_spi_init(struct atmel_spi *as)
1447{
1448 spi_writel(as, CR, SPI_BIT(SWRST));
1449 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Eugen Hristev95813292018-02-27 12:25:07 +02001450
1451 /* It is recommended to enable FIFOs first thing after reset */
1452 if (as->fifo_size)
1453 spi_writel(as, CR, SPI_BIT(FIFOEN));
1454
Quentin Schulz05514c82017-04-12 09:05:19 +02001455 if (as->caps.has_wdrbt) {
1456 spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
1457 | SPI_BIT(MSTR));
1458 } else {
1459 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
1460 }
1461
1462 if (as->use_pdc)
1463 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
1464 spi_writel(as, CR, SPI_BIT(SPIEN));
Quentin Schulz05514c82017-04-12 09:05:19 +02001465}
1466
Grant Likelyfd4a3192012-12-07 16:57:14 +00001467static int atmel_spi_probe(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001468{
1469 struct resource *regs;
1470 int irq;
1471 struct clk *clk;
1472 int ret;
1473 struct spi_master *master;
1474 struct atmel_spi *as;
1475
Wenyou Yang5bdfd492014-03-05 09:58:49 +08001476 /* Select default pin state */
1477 pinctrl_pm_select_default_state(&pdev->dev);
1478
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001479 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1480 if (!regs)
1481 return -ENXIO;
1482
1483 irq = platform_get_irq(pdev, 0);
1484 if (irq < 0)
1485 return irq;
1486
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001487 clk = devm_clk_get(&pdev->dev, "spi_clk");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001488 if (IS_ERR(clk))
1489 return PTR_ERR(clk);
1490
1491 /* setup spi core then atmel-specific driver state */
1492 ret = -ENOMEM;
Sachin Kamata536d762013-09-10 17:06:27 +05301493 master = spi_alloc_master(&pdev->dev, sizeof(*as));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001494 if (!master)
1495 goto out_free;
1496
David Brownelle7db06b2009-06-17 16:26:04 -07001497 /* the spi->mode bits understood by this driver: */
Linus Walleijefc92fb2019-01-07 16:51:52 +01001498 master->use_gpio_descriptors = true;
David Brownelle7db06b2009-06-17 16:26:04 -07001499 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001500 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001501 master->dev.of_node = pdev->dev.of_node;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001502 master->bus_num = pdev->id;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001503 master->num_chipselect = master->dev.of_node ? 0 : 4;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001504 master->setup = atmel_spi_setup;
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001505 master->flags = (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001506 master->transfer_one_message = atmel_spi_transfer_one_message;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001507 master->cleanup = atmel_spi_cleanup;
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001508 master->auto_runtime_pm = true;
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001509 master->max_dma_len = SPI_MAX_DMA_XFER;
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001510 master->can_dma = atmel_spi_can_dma;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001511 platform_set_drvdata(pdev, master);
1512
1513 as = spi_master_get_devdata(master);
1514
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001515 spin_lock_init(&as->lock);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001516
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001517 as->pdev = pdev;
Mark Brown31407472013-10-16 13:22:35 +01001518 as->regs = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun543c9542013-10-21 11:12:02 +08001519 if (IS_ERR(as->regs)) {
1520 ret = PTR_ERR(as->regs);
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001521 goto out_unmap_regs;
Wei Yongjun543c9542013-10-21 11:12:02 +08001522 }
Nicolas Ferredfab30e2013-04-03 13:57:42 +08001523 as->phybase = regs->start;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001524 as->irq = irq;
1525 as->clk = clk;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001526
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001527 init_completion(&as->xfer_completion);
1528
Wenyou Yangd4820b72013-03-19 15:42:15 +08001529 atmel_get_caps(as);
1530
Linus Walleijefc92fb2019-01-07 16:51:52 +01001531 /*
1532 * If there are chip selects in the device tree, those will be
1533 * discovered by the SPI core when registering the SPI master
1534 * and assigned to each SPI device.
1535 */
Cyrille Pitchen48203032015-06-09 13:53:52 +02001536 if (atmel_spi_is_v2(as) &&
Cyrille Pitchen70f340d2016-01-27 17:48:32 +01001537 pdev->dev.of_node &&
Gregory CLEMENT585d18f2019-10-17 16:18:42 +02001538 !of_get_property(pdev->dev.of_node, "cs-gpios", NULL))
Cyrille Pitchen48203032015-06-09 13:53:52 +02001539 master->num_chipselect = 4;
Cyrille Pitchen48203032015-06-09 13:53:52 +02001540
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001541 as->use_dma = false;
1542 as->use_pdc = false;
1543 if (as->caps.has_dma_support) {
Nicolas Ferre768f3d92016-11-24 12:25:01 +01001544 ret = atmel_spi_configure_dma(master, as);
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001545 if (ret == 0) {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001546 as->use_dma = true;
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001547 } else if (ret == -EPROBE_DEFER) {
Ludovic Desroches5e9af372014-11-14 17:12:54 +01001548 return ret;
Cyrille Pitchen04242ca2016-11-24 12:24:59 +01001549 }
Cyrille Pitchen70945762017-06-23 17:39:16 +02001550 } else if (as->caps.has_pdc_support) {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001551 as->use_pdc = true;
1552 }
1553
Radu Pireaa9889ed2017-12-19 17:17:59 +02001554 if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
1555 as->addr_rx_bbuf = dma_alloc_coherent(&pdev->dev,
1556 SPI_MAX_DMA_XFER,
1557 &as->dma_addr_rx_bbuf,
1558 GFP_KERNEL | GFP_DMA);
1559 if (!as->addr_rx_bbuf) {
1560 as->use_dma = false;
1561 } else {
1562 as->addr_tx_bbuf = dma_alloc_coherent(&pdev->dev,
1563 SPI_MAX_DMA_XFER,
1564 &as->dma_addr_tx_bbuf,
1565 GFP_KERNEL | GFP_DMA);
1566 if (!as->addr_tx_bbuf) {
1567 as->use_dma = false;
1568 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER,
1569 as->addr_rx_bbuf,
1570 as->dma_addr_rx_bbuf);
1571 }
1572 }
1573 if (!as->use_dma)
1574 dev_info(master->dev.parent,
1575 " can not allocate dma coherent memory\n");
1576 }
1577
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001578 if (as->caps.has_dma_support && !as->use_dma)
1579 dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
1580
1581 if (as->use_pdc) {
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001582 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
1583 0, dev_name(&pdev->dev), master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001584 } else {
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001585 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
1586 0, dev_name(&pdev->dev), master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001587 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001588 if (ret)
1589 goto out_unmap_regs;
1590
1591 /* Initialize the hardware */
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001592 ret = clk_prepare_enable(clk);
1593 if (ret)
Sachin Kamatde8cc232013-09-10 17:06:26 +05301594 goto out_free_irq;
Ben Whitten39fe33f2016-11-14 15:13:20 +00001595
1596 as->spi_clk = clk_get_rate(clk);
1597
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001598 as->fifo_size = 0;
1599 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
1600 &as->fifo_size)) {
1601 dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size);
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001602 }
1603
Quentin Schulz05514c82017-04-12 09:05:19 +02001604 atmel_spi_init(as);
1605
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001606 pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
1607 pm_runtime_use_autosuspend(&pdev->dev);
1608 pm_runtime_set_active(&pdev->dev);
1609 pm_runtime_enable(&pdev->dev);
1610
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001611 ret = devm_spi_register_master(&pdev->dev, master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001612 if (ret)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001613 goto out_free_dma;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001614
Nicolas Ferrece24a512016-11-24 12:24:57 +01001615 /* go! */
Baruch Siach6aba9c62017-05-30 08:33:30 +03001616 dev_info(&pdev->dev, "Atmel SPI Controller version 0x%x at 0x%08lx (irq %d)\n",
1617 atmel_get_version(as), (unsigned long)regs->start,
1618 irq);
Nicolas Ferrece24a512016-11-24 12:24:57 +01001619
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001620 return 0;
1621
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001622out_free_dma:
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001623 pm_runtime_disable(&pdev->dev);
1624 pm_runtime_set_suspended(&pdev->dev);
1625
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001626 if (as->use_dma)
Nicolas Ferre768f3d92016-11-24 12:25:01 +01001627 atmel_spi_release_dma(master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001628
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001629 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001630 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001631 clk_disable_unprepare(clk);
Sachin Kamatde8cc232013-09-10 17:06:26 +05301632out_free_irq:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001633out_unmap_regs:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001634out_free:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001635 spi_master_put(master);
1636 return ret;
1637}
1638
Grant Likelyfd4a3192012-12-07 16:57:14 +00001639static int atmel_spi_remove(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001640{
1641 struct spi_master *master = platform_get_drvdata(pdev);
1642 struct atmel_spi *as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001643
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001644 pm_runtime_get_sync(&pdev->dev);
1645
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001646 /* reset the hardware and block queue progress */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001647 if (as->use_dma) {
Nicolas Ferre768f3d92016-11-24 12:25:01 +01001648 atmel_spi_stop_dma(master);
1649 atmel_spi_release_dma(master);
Radu Pireaa9889ed2017-12-19 17:17:59 +02001650 if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
1651 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER,
1652 as->addr_tx_bbuf,
1653 as->dma_addr_tx_bbuf);
1654 dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER,
1655 as->addr_rx_bbuf,
1656 as->dma_addr_rx_bbuf);
1657 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001658 }
1659
Radu Pirea66e900a2017-12-15 17:40:17 +02001660 spin_lock_irq(&as->lock);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001661 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001662 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001663 spi_readl(as, SR);
1664 spin_unlock_irq(&as->lock);
1665
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001666 clk_disable_unprepare(as->clk);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001667
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001668 pm_runtime_put_noidle(&pdev->dev);
1669 pm_runtime_disable(&pdev->dev);
1670
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001671 return 0;
1672}
1673
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001674#ifdef CONFIG_PM
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001675static int atmel_spi_runtime_suspend(struct device *dev)
1676{
1677 struct spi_master *master = dev_get_drvdata(dev);
1678 struct atmel_spi *as = spi_master_get_devdata(master);
Jingoo Hanec60dd32013-09-09 17:54:12 +09001679
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001680 clk_disable_unprepare(as->clk);
1681 pinctrl_pm_select_sleep_state(dev);
1682
1683 return 0;
1684}
1685
1686static int atmel_spi_runtime_resume(struct device *dev)
1687{
1688 struct spi_master *master = dev_get_drvdata(dev);
1689 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001690
1691 pinctrl_pm_select_default_state(dev);
1692
Fengguang Wud0de6ff2014-10-17 00:18:56 +08001693 return clk_prepare_enable(as->clk);
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001694}
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001695
Alexandre Bellonid6305262015-09-10 10:19:52 +02001696#ifdef CONFIG_PM_SLEEP
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001697static int atmel_spi_suspend(struct device *dev)
1698{
1699 struct spi_master *master = dev_get_drvdata(dev);
1700 int ret;
1701
1702 /* Stop the queue running */
1703 ret = spi_master_suspend(master);
Geert Uytterhoeven7c5d8a22018-09-05 10:51:57 +02001704 if (ret)
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001705 return ret;
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001706
1707 if (!pm_runtime_suspended(dev))
1708 atmel_spi_runtime_suspend(dev);
1709
1710 return 0;
1711}
1712
1713static int atmel_spi_resume(struct device *dev)
1714{
1715 struct spi_master *master = dev_get_drvdata(dev);
Quentin Schulze5380072017-04-14 10:22:43 +02001716 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001717 int ret;
1718
Quentin Schulze5380072017-04-14 10:22:43 +02001719 ret = clk_prepare_enable(as->clk);
1720 if (ret)
1721 return ret;
1722
1723 atmel_spi_init(as);
1724
1725 clk_disable_unprepare(as->clk);
1726
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001727 if (!pm_runtime_suspended(dev)) {
1728 ret = atmel_spi_runtime_resume(dev);
1729 if (ret)
1730 return ret;
1731 }
1732
1733 /* Start the queue running */
Geert Uytterhoeven7c5d8a22018-09-05 10:51:57 +02001734 return spi_master_resume(master);
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001735}
Alexandre Bellonid6305262015-09-10 10:19:52 +02001736#endif
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001737
1738static const struct dev_pm_ops atmel_spi_pm_ops = {
1739 SET_SYSTEM_SLEEP_PM_OPS(atmel_spi_suspend, atmel_spi_resume)
1740 SET_RUNTIME_PM_OPS(atmel_spi_runtime_suspend,
1741 atmel_spi_runtime_resume, NULL)
1742};
Jingoo Hanec60dd32013-09-09 17:54:12 +09001743#define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001744#else
Jingoo Hanec60dd32013-09-09 17:54:12 +09001745#define ATMEL_SPI_PM_OPS NULL
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001746#endif
1747
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001748#if defined(CONFIG_OF)
1749static const struct of_device_id atmel_spi_dt_ids[] = {
1750 { .compatible = "atmel,at91rm9200-spi" },
1751 { /* sentinel */ }
1752};
1753
1754MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
1755#endif
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001756
1757static struct platform_driver atmel_spi_driver = {
1758 .driver = {
1759 .name = "atmel_spi",
Jingoo Hanec60dd32013-09-09 17:54:12 +09001760 .pm = ATMEL_SPI_PM_OPS,
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001761 .of_match_table = of_match_ptr(atmel_spi_dt_ids),
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001762 },
Jean-Christophe PLAGNIOL-VILLARD1cb201a2011-11-04 01:20:21 +08001763 .probe = atmel_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +00001764 .remove = atmel_spi_remove,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001765};
Grant Likely940ab882011-10-05 11:29:49 -06001766module_platform_driver(atmel_spi_driver);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001767
1768MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001769MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001770MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001771MODULE_ALIAS("platform:atmel_spi");