blob: 8f20d4f75e4a55e600754348510e41ef3ce6cce2 [file] [log] [blame]
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001/*
2 * Driver for Atmel AT32 and AT91 SPI Controllers
3 *
4 * Copyright (C) 2006 Atmel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/kernel.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -080012#include <linux/clk.h>
13#include <linux/module.h>
14#include <linux/platform_device.h>
15#include <linux/delay.h>
16#include <linux/dma-mapping.h>
Nicolas Ferre1ccc4042013-04-03 13:59:19 +080017#include <linux/dmaengine.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -080018#include <linux/err.h>
19#include <linux/interrupt.h>
20#include <linux/spi/spi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Nicolas Ferre1ccc4042013-04-03 13:59:19 +080022#include <linux/platform_data/dma-atmel.h>
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +010023#include <linux/of.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -080024
Wenyou Yangd4820b72013-03-19 15:42:15 +080025#include <linux/io.h>
26#include <linux/gpio.h>
Nicolas Ferre96106202016-11-08 18:48:52 +010027#include <linux/of_gpio.h>
Wenyou Yang5bdfd492014-03-05 09:58:49 +080028#include <linux/pinctrl/consumer.h>
Wenyou Yangce0c4ca2014-10-16 17:23:10 +080029#include <linux/pm_runtime.h>
David Brownellbb2d1c32007-02-20 13:58:19 -080030
Grant Likelyca632f52011-06-06 01:16:30 -060031/* SPI register offsets */
32#define SPI_CR 0x0000
33#define SPI_MR 0x0004
34#define SPI_RDR 0x0008
35#define SPI_TDR 0x000c
36#define SPI_SR 0x0010
37#define SPI_IER 0x0014
38#define SPI_IDR 0x0018
39#define SPI_IMR 0x001c
40#define SPI_CSR0 0x0030
41#define SPI_CSR1 0x0034
42#define SPI_CSR2 0x0038
43#define SPI_CSR3 0x003c
Cyrille Pitchen11f27642015-06-16 12:09:31 +020044#define SPI_FMR 0x0040
45#define SPI_FLR 0x0044
Wenyou Yangd4820b72013-03-19 15:42:15 +080046#define SPI_VERSION 0x00fc
Grant Likelyca632f52011-06-06 01:16:30 -060047#define SPI_RPR 0x0100
48#define SPI_RCR 0x0104
49#define SPI_TPR 0x0108
50#define SPI_TCR 0x010c
51#define SPI_RNPR 0x0110
52#define SPI_RNCR 0x0114
53#define SPI_TNPR 0x0118
54#define SPI_TNCR 0x011c
55#define SPI_PTCR 0x0120
56#define SPI_PTSR 0x0124
57
58/* Bitfields in CR */
59#define SPI_SPIEN_OFFSET 0
60#define SPI_SPIEN_SIZE 1
61#define SPI_SPIDIS_OFFSET 1
62#define SPI_SPIDIS_SIZE 1
63#define SPI_SWRST_OFFSET 7
64#define SPI_SWRST_SIZE 1
65#define SPI_LASTXFER_OFFSET 24
66#define SPI_LASTXFER_SIZE 1
Cyrille Pitchen11f27642015-06-16 12:09:31 +020067#define SPI_TXFCLR_OFFSET 16
68#define SPI_TXFCLR_SIZE 1
69#define SPI_RXFCLR_OFFSET 17
70#define SPI_RXFCLR_SIZE 1
71#define SPI_FIFOEN_OFFSET 30
72#define SPI_FIFOEN_SIZE 1
73#define SPI_FIFODIS_OFFSET 31
74#define SPI_FIFODIS_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -060075
76/* Bitfields in MR */
77#define SPI_MSTR_OFFSET 0
78#define SPI_MSTR_SIZE 1
79#define SPI_PS_OFFSET 1
80#define SPI_PS_SIZE 1
81#define SPI_PCSDEC_OFFSET 2
82#define SPI_PCSDEC_SIZE 1
83#define SPI_FDIV_OFFSET 3
84#define SPI_FDIV_SIZE 1
85#define SPI_MODFDIS_OFFSET 4
86#define SPI_MODFDIS_SIZE 1
Wenyou Yangd4820b72013-03-19 15:42:15 +080087#define SPI_WDRBT_OFFSET 5
88#define SPI_WDRBT_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -060089#define SPI_LLB_OFFSET 7
90#define SPI_LLB_SIZE 1
91#define SPI_PCS_OFFSET 16
92#define SPI_PCS_SIZE 4
93#define SPI_DLYBCS_OFFSET 24
94#define SPI_DLYBCS_SIZE 8
95
96/* Bitfields in RDR */
97#define SPI_RD_OFFSET 0
98#define SPI_RD_SIZE 16
99
100/* Bitfields in TDR */
101#define SPI_TD_OFFSET 0
102#define SPI_TD_SIZE 16
103
104/* Bitfields in SR */
105#define SPI_RDRF_OFFSET 0
106#define SPI_RDRF_SIZE 1
107#define SPI_TDRE_OFFSET 1
108#define SPI_TDRE_SIZE 1
109#define SPI_MODF_OFFSET 2
110#define SPI_MODF_SIZE 1
111#define SPI_OVRES_OFFSET 3
112#define SPI_OVRES_SIZE 1
113#define SPI_ENDRX_OFFSET 4
114#define SPI_ENDRX_SIZE 1
115#define SPI_ENDTX_OFFSET 5
116#define SPI_ENDTX_SIZE 1
117#define SPI_RXBUFF_OFFSET 6
118#define SPI_RXBUFF_SIZE 1
119#define SPI_TXBUFE_OFFSET 7
120#define SPI_TXBUFE_SIZE 1
121#define SPI_NSSR_OFFSET 8
122#define SPI_NSSR_SIZE 1
123#define SPI_TXEMPTY_OFFSET 9
124#define SPI_TXEMPTY_SIZE 1
125#define SPI_SPIENS_OFFSET 16
126#define SPI_SPIENS_SIZE 1
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200127#define SPI_TXFEF_OFFSET 24
128#define SPI_TXFEF_SIZE 1
129#define SPI_TXFFF_OFFSET 25
130#define SPI_TXFFF_SIZE 1
131#define SPI_TXFTHF_OFFSET 26
132#define SPI_TXFTHF_SIZE 1
133#define SPI_RXFEF_OFFSET 27
134#define SPI_RXFEF_SIZE 1
135#define SPI_RXFFF_OFFSET 28
136#define SPI_RXFFF_SIZE 1
137#define SPI_RXFTHF_OFFSET 29
138#define SPI_RXFTHF_SIZE 1
139#define SPI_TXFPTEF_OFFSET 30
140#define SPI_TXFPTEF_SIZE 1
141#define SPI_RXFPTEF_OFFSET 31
142#define SPI_RXFPTEF_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -0600143
144/* Bitfields in CSR0 */
145#define SPI_CPOL_OFFSET 0
146#define SPI_CPOL_SIZE 1
147#define SPI_NCPHA_OFFSET 1
148#define SPI_NCPHA_SIZE 1
149#define SPI_CSAAT_OFFSET 3
150#define SPI_CSAAT_SIZE 1
151#define SPI_BITS_OFFSET 4
152#define SPI_BITS_SIZE 4
153#define SPI_SCBR_OFFSET 8
154#define SPI_SCBR_SIZE 8
155#define SPI_DLYBS_OFFSET 16
156#define SPI_DLYBS_SIZE 8
157#define SPI_DLYBCT_OFFSET 24
158#define SPI_DLYBCT_SIZE 8
159
160/* Bitfields in RCR */
161#define SPI_RXCTR_OFFSET 0
162#define SPI_RXCTR_SIZE 16
163
164/* Bitfields in TCR */
165#define SPI_TXCTR_OFFSET 0
166#define SPI_TXCTR_SIZE 16
167
168/* Bitfields in RNCR */
169#define SPI_RXNCR_OFFSET 0
170#define SPI_RXNCR_SIZE 16
171
172/* Bitfields in TNCR */
173#define SPI_TXNCR_OFFSET 0
174#define SPI_TXNCR_SIZE 16
175
176/* Bitfields in PTCR */
177#define SPI_RXTEN_OFFSET 0
178#define SPI_RXTEN_SIZE 1
179#define SPI_RXTDIS_OFFSET 1
180#define SPI_RXTDIS_SIZE 1
181#define SPI_TXTEN_OFFSET 8
182#define SPI_TXTEN_SIZE 1
183#define SPI_TXTDIS_OFFSET 9
184#define SPI_TXTDIS_SIZE 1
185
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200186/* Bitfields in FMR */
187#define SPI_TXRDYM_OFFSET 0
188#define SPI_TXRDYM_SIZE 2
189#define SPI_RXRDYM_OFFSET 4
190#define SPI_RXRDYM_SIZE 2
191#define SPI_TXFTHRES_OFFSET 16
192#define SPI_TXFTHRES_SIZE 6
193#define SPI_RXFTHRES_OFFSET 24
194#define SPI_RXFTHRES_SIZE 6
195
196/* Bitfields in FLR */
197#define SPI_TXFL_OFFSET 0
198#define SPI_TXFL_SIZE 6
199#define SPI_RXFL_OFFSET 16
200#define SPI_RXFL_SIZE 6
201
Grant Likelyca632f52011-06-06 01:16:30 -0600202/* Constants for BITS */
203#define SPI_BITS_8_BPT 0
204#define SPI_BITS_9_BPT 1
205#define SPI_BITS_10_BPT 2
206#define SPI_BITS_11_BPT 3
207#define SPI_BITS_12_BPT 4
208#define SPI_BITS_13_BPT 5
209#define SPI_BITS_14_BPT 6
210#define SPI_BITS_15_BPT 7
211#define SPI_BITS_16_BPT 8
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200212#define SPI_ONE_DATA 0
213#define SPI_TWO_DATA 1
214#define SPI_FOUR_DATA 2
Grant Likelyca632f52011-06-06 01:16:30 -0600215
216/* Bit manipulation macros */
217#define SPI_BIT(name) \
218 (1 << SPI_##name##_OFFSET)
Sachin Kamata536d762013-09-10 17:06:27 +0530219#define SPI_BF(name, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600220 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
Sachin Kamata536d762013-09-10 17:06:27 +0530221#define SPI_BFEXT(name, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600222 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
Sachin Kamata536d762013-09-10 17:06:27 +0530223#define SPI_BFINS(name, value, old) \
224 (((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
225 | SPI_BF(name, value))
Grant Likelyca632f52011-06-06 01:16:30 -0600226
227/* Register access macros */
Ben Dooksea467322015-03-18 15:53:08 +0000228#ifdef CONFIG_AVR32
Sachin Kamata536d762013-09-10 17:06:27 +0530229#define spi_readl(port, reg) \
Grant Likelyca632f52011-06-06 01:16:30 -0600230 __raw_readl((port)->regs + SPI_##reg)
Sachin Kamata536d762013-09-10 17:06:27 +0530231#define spi_writel(port, reg, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600232 __raw_writel((value), (port)->regs + SPI_##reg)
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200233
234#define spi_readw(port, reg) \
235 __raw_readw((port)->regs + SPI_##reg)
236#define spi_writew(port, reg, value) \
237 __raw_writew((value), (port)->regs + SPI_##reg)
238
239#define spi_readb(port, reg) \
240 __raw_readb((port)->regs + SPI_##reg)
241#define spi_writeb(port, reg, value) \
242 __raw_writeb((value), (port)->regs + SPI_##reg)
Ben Dooksea467322015-03-18 15:53:08 +0000243#else
244#define spi_readl(port, reg) \
245 readl_relaxed((port)->regs + SPI_##reg)
246#define spi_writel(port, reg, value) \
247 writel_relaxed((value), (port)->regs + SPI_##reg)
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200248
249#define spi_readw(port, reg) \
250 readw_relaxed((port)->regs + SPI_##reg)
251#define spi_writew(port, reg, value) \
252 writew_relaxed((value), (port)->regs + SPI_##reg)
253
254#define spi_readb(port, reg) \
255 readb_relaxed((port)->regs + SPI_##reg)
256#define spi_writeb(port, reg, value) \
257 writeb_relaxed((value), (port)->regs + SPI_##reg)
Ben Dooksea467322015-03-18 15:53:08 +0000258#endif
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800259/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
260 * cache operations; better heuristics consider wordsize and bitrate.
261 */
262#define DMA_MIN_BYTES 16
263
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800264#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
265
Wenyou Yangce0c4ca2014-10-16 17:23:10 +0800266#define AUTOSUSPEND_TIMEOUT 2000
267
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800268struct atmel_spi_dma {
269 struct dma_chan *chan_rx;
270 struct dma_chan *chan_tx;
271 struct scatterlist sgrx;
272 struct scatterlist sgtx;
273 struct dma_async_tx_descriptor *data_desc_rx;
274 struct dma_async_tx_descriptor *data_desc_tx;
275
276 struct at_dma_slave dma_slave;
277};
278
Wenyou Yangd4820b72013-03-19 15:42:15 +0800279struct atmel_spi_caps {
280 bool is_spi2;
281 bool has_wdrbt;
282 bool has_dma_support;
283};
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800284
285/*
286 * The core SPI transfer engine just talks to a register bank to set up
287 * DMA transfers; transfer queue progress is driven by IRQs. The clock
288 * framework provides the base clock, subdivided for each spi_device.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800289 */
290struct atmel_spi {
291 spinlock_t lock;
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800292 unsigned long flags;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800293
Nicolas Ferredfab30e2013-04-03 13:57:42 +0800294 phys_addr_t phybase;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800295 void __iomem *regs;
296 int irq;
297 struct clk *clk;
298 struct platform_device *pdev;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800299
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800300 struct spi_transfer *current_transfer;
Axel Lin0c3b9742014-03-27 09:26:38 +0800301 int current_remaining_bytes;
Nicolas Ferre823cd042013-03-19 15:45:01 +0800302 int done_status;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800303
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800304 struct completion xfer_completion;
305
Wenyou Yangd4820b72013-03-19 15:42:15 +0800306 struct atmel_spi_caps caps;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800307
308 bool use_dma;
309 bool use_pdc;
Cyrille Pitchen48203032015-06-09 13:53:52 +0200310 bool use_cs_gpios;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800311 /* dmaengine data */
312 struct atmel_spi_dma dma;
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800313
314 bool keep_cs;
315 bool cs_active;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200316
317 u32 fifo_size;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800318};
319
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800320/* Controller-specific per-slave state */
321struct atmel_spi_device {
322 unsigned int npcs_pin;
323 u32 csr;
324};
325
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100326#define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800327#define INVALID_DMA_ADDRESS 0xffffffff
328
329/*
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800330 * Version 2 of the SPI controller has
331 * - CR.LASTXFER
332 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
333 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
334 * - SPI_CSRx.CSAAT
335 * - SPI_CSRx.SBCR allows faster clocking
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800336 */
Wenyou Yangd4820b72013-03-19 15:42:15 +0800337static bool atmel_spi_is_v2(struct atmel_spi *as)
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800338{
Wenyou Yangd4820b72013-03-19 15:42:15 +0800339 return as->caps.is_spi2;
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800340}
341
342/*
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800343 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
344 * they assume that spi slave device state will not change on deselect, so
David Brownelldefbd3b2007-07-17 04:04:08 -0700345 * that automagic deselection is OK. ("NPCSx rises if no data is to be
346 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
347 * controllers have CSAAT and friends.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800348 *
David Brownelldefbd3b2007-07-17 04:04:08 -0700349 * Since the CSAAT functionality is a bit weird on newer controllers as
350 * well, we use GPIO to control nCSx pins on all controllers, updating
351 * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
352 * support active-high chipselects despite the controller's belief that
353 * only active-low devices/systems exists.
354 *
355 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
356 * right when driven with GPIO. ("Mode Fault does not allow more than one
357 * Master on Chip Select 0.") No workaround exists for that ... so for
358 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
359 * and (c) will trigger that first erratum in some cases.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800360 */
361
David Brownelldefbd3b2007-07-17 04:04:08 -0700362static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800363{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800364 struct atmel_spi_device *asd = spi->controller_state;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800365 unsigned active = spi->mode & SPI_CS_HIGH;
David Brownelldefbd3b2007-07-17 04:04:08 -0700366 u32 mr;
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800367
Wenyou Yangd4820b72013-03-19 15:42:15 +0800368 if (atmel_spi_is_v2(as)) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800369 spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
370 /* For the low SPI version, there is a issue that PDC transfer
371 * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800372 */
373 spi_writel(as, CSR0, asd->csr);
Wenyou Yangd4820b72013-03-19 15:42:15 +0800374 if (as->caps.has_wdrbt) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800375 spi_writel(as, MR,
376 SPI_BF(PCS, ~(0x01 << spi->chip_select))
377 | SPI_BIT(WDRBT)
378 | SPI_BIT(MODFDIS)
379 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800380 } else {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800381 spi_writel(as, MR,
382 SPI_BF(PCS, ~(0x01 << spi->chip_select))
383 | SPI_BIT(MODFDIS)
384 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800385 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800386
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800387 mr = spi_readl(as, MR);
Cyrille Pitchen48203032015-06-09 13:53:52 +0200388 if (as->use_cs_gpios)
389 gpio_set_value(asd->npcs_pin, active);
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800390 } else {
391 u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
392 int i;
393 u32 csr;
394
395 /* Make sure clock polarity is correct */
396 for (i = 0; i < spi->master->num_chipselect; i++) {
397 csr = spi_readl(as, CSR0 + 4 * i);
398 if ((csr ^ cpol) & SPI_BIT(CPOL))
399 spi_writel(as, CSR0 + 4 * i,
400 csr ^ SPI_BIT(CPOL));
401 }
402
403 mr = spi_readl(as, MR);
404 mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
Cyrille Pitchen48203032015-06-09 13:53:52 +0200405 if (as->use_cs_gpios && spi->chip_select != 0)
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800406 gpio_set_value(asd->npcs_pin, active);
407 spi_writel(as, MR, mr);
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800408 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800409
David Brownelldefbd3b2007-07-17 04:04:08 -0700410 dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800411 asd->npcs_pin, active ? " (high)" : "",
David Brownelldefbd3b2007-07-17 04:04:08 -0700412 mr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800413}
414
David Brownelldefbd3b2007-07-17 04:04:08 -0700415static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800416{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800417 struct atmel_spi_device *asd = spi->controller_state;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800418 unsigned active = spi->mode & SPI_CS_HIGH;
David Brownelldefbd3b2007-07-17 04:04:08 -0700419 u32 mr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800420
David Brownelldefbd3b2007-07-17 04:04:08 -0700421 /* only deactivate *this* device; sometimes transfers to
422 * another device may be active when this routine is called.
423 */
424 mr = spi_readl(as, MR);
425 if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
426 mr = SPI_BFINS(PCS, 0xf, mr);
427 spi_writel(as, MR, mr);
428 }
429
430 dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800431 asd->npcs_pin, active ? " (low)" : "",
David Brownelldefbd3b2007-07-17 04:04:08 -0700432 mr);
433
Cyrille Pitchen48203032015-06-09 13:53:52 +0200434 if (!as->use_cs_gpios)
435 spi_writel(as, CR, SPI_BIT(LASTXFER));
436 else if (atmel_spi_is_v2(as) || spi->chip_select != 0)
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800437 gpio_set_value(asd->npcs_pin, !active);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800438}
439
Mark Brown6c07ef22013-07-28 14:32:27 +0100440static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock)
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800441{
442 spin_lock_irqsave(&as->lock, as->flags);
443}
444
Mark Brown6c07ef22013-07-28 14:32:27 +0100445static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock)
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800446{
447 spin_unlock_irqrestore(&as->lock, as->flags);
448}
449
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800450static inline bool atmel_spi_use_dma(struct atmel_spi *as,
451 struct spi_transfer *xfer)
452{
453 return as->use_dma && xfer->len >= DMA_MIN_BYTES;
454}
455
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800456static int atmel_spi_dma_slave_config(struct atmel_spi *as,
457 struct dma_slave_config *slave_config,
458 u8 bits_per_word)
459{
460 int err = 0;
461
462 if (bits_per_word > 8) {
463 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
464 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
465 } else {
466 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
467 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
468 }
469
470 slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
471 slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
472 slave_config->src_maxburst = 1;
473 slave_config->dst_maxburst = 1;
474 slave_config->device_fc = false;
475
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200476 /*
477 * This driver uses fixed peripheral select mode (PS bit set to '0' in
478 * the Mode Register).
479 * So according to the datasheet, when FIFOs are available (and
480 * enabled), the Transmit FIFO operates in Multiple Data Mode.
481 * In this mode, up to 2 data, not 4, can be written into the Transmit
482 * Data Register in a single access.
483 * However, the first data has to be written into the lowest 16 bits and
484 * the second data into the highest 16 bits of the Transmit
485 * Data Register. For 8bit data (the most frequent case), it would
486 * require to rework tx_buf so each data would actualy fit 16 bits.
487 * So we'd rather write only one data at the time. Hence the transmit
488 * path works the same whether FIFOs are available (and enabled) or not.
489 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800490 slave_config->direction = DMA_MEM_TO_DEV;
491 if (dmaengine_slave_config(as->dma.chan_tx, slave_config)) {
492 dev_err(&as->pdev->dev,
493 "failed to configure tx dma channel\n");
494 err = -EINVAL;
495 }
496
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200497 /*
498 * This driver configures the spi controller for master mode (MSTR bit
499 * set to '1' in the Mode Register).
500 * So according to the datasheet, when FIFOs are available (and
501 * enabled), the Receive FIFO operates in Single Data Mode.
502 * So the receive path works the same whether FIFOs are available (and
503 * enabled) or not.
504 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800505 slave_config->direction = DMA_DEV_TO_MEM;
506 if (dmaengine_slave_config(as->dma.chan_rx, slave_config)) {
507 dev_err(&as->pdev->dev,
508 "failed to configure rx dma channel\n");
509 err = -EINVAL;
510 }
511
512 return err;
513}
514
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800515static int atmel_spi_configure_dma(struct atmel_spi *as)
516{
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800517 struct dma_slave_config slave_config;
Richard Genoud2f767a92013-05-31 17:01:59 +0200518 struct device *dev = &as->pdev->dev;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800519 int err;
520
Richard Genoud2f767a92013-05-31 17:01:59 +0200521 dma_cap_mask_t mask;
522 dma_cap_zero(mask);
523 dma_cap_set(DMA_SLAVE, mask);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800524
Ludovic Desroches5e9af372014-11-14 17:12:54 +0100525 as->dma.chan_tx = dma_request_slave_channel_reason(dev, "tx");
526 if (IS_ERR(as->dma.chan_tx)) {
527 err = PTR_ERR(as->dma.chan_tx);
528 if (err == -EPROBE_DEFER) {
529 dev_warn(dev, "no DMA channel available at the moment\n");
530 return err;
531 }
Richard Genoud2f767a92013-05-31 17:01:59 +0200532 dev_err(dev,
533 "DMA TX channel not available, SPI unable to use DMA\n");
534 err = -EBUSY;
535 goto error;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800536 }
Richard Genoud2f767a92013-05-31 17:01:59 +0200537
Ludovic Desroches5e9af372014-11-14 17:12:54 +0100538 /*
539 * No reason to check EPROBE_DEFER here since we have already requested
540 * tx channel. If it fails here, it's for another reason.
541 */
Ludovic Desroches7758e392014-11-14 17:12:53 +0100542 as->dma.chan_rx = dma_request_slave_channel(dev, "rx");
Richard Genoud2f767a92013-05-31 17:01:59 +0200543
544 if (!as->dma.chan_rx) {
545 dev_err(dev,
546 "DMA RX channel not available, SPI unable to use DMA\n");
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800547 err = -EBUSY;
548 goto error;
549 }
550
551 err = atmel_spi_dma_slave_config(as, &slave_config, 8);
552 if (err)
553 goto error;
554
555 dev_info(&as->pdev->dev,
556 "Using %s (tx) and %s (rx) for DMA transfers\n",
557 dma_chan_name(as->dma.chan_tx),
558 dma_chan_name(as->dma.chan_rx));
559 return 0;
560error:
561 if (as->dma.chan_rx)
562 dma_release_channel(as->dma.chan_rx);
Ludovic Desroches5e9af372014-11-14 17:12:54 +0100563 if (!IS_ERR(as->dma.chan_tx))
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800564 dma_release_channel(as->dma.chan_tx);
565 return err;
566}
567
568static void atmel_spi_stop_dma(struct atmel_spi *as)
569{
570 if (as->dma.chan_rx)
Vinod Koul5398ad62014-10-11 21:10:35 +0530571 dmaengine_terminate_all(as->dma.chan_rx);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800572 if (as->dma.chan_tx)
Vinod Koul5398ad62014-10-11 21:10:35 +0530573 dmaengine_terminate_all(as->dma.chan_tx);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800574}
575
576static void atmel_spi_release_dma(struct atmel_spi *as)
577{
578 if (as->dma.chan_rx)
579 dma_release_channel(as->dma.chan_rx);
580 if (as->dma.chan_tx)
581 dma_release_channel(as->dma.chan_tx);
582}
583
584/* This function is called by the DMA driver from tasklet context */
585static void dma_callback(void *data)
586{
587 struct spi_master *master = data;
588 struct atmel_spi *as = spi_master_get_devdata(master);
589
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800590 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800591}
592
593/*
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200594 * Next transfer using PIO without FIFO.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800595 */
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200596static void atmel_spi_next_xfer_single(struct spi_master *master,
597 struct spi_transfer *xfer)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800598{
599 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800600 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800601
602 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n");
603
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800604 /* Make sure data is not remaining in RDR */
605 spi_readl(as, RDR);
606 while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
607 spi_readl(as, RDR);
608 cpu_relax();
609 }
610
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100611 if (xfer->bits_per_word > 8)
612 spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos));
613 else
614 spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos));
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800615
616 dev_dbg(master->dev.parent,
Richard Genoudf557c982013-05-02 19:25:11 +0800617 " start pio xfer %p: len %u tx %p rx %p bitpw %d\n",
618 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
619 xfer->bits_per_word);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800620
621 /* Enable relevant interrupts */
622 spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
623}
624
625/*
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200626 * Next transfer using PIO with FIFO.
627 */
628static void atmel_spi_next_xfer_fifo(struct spi_master *master,
629 struct spi_transfer *xfer)
630{
631 struct atmel_spi *as = spi_master_get_devdata(master);
632 u32 current_remaining_data, num_data;
633 u32 offset = xfer->len - as->current_remaining_bytes;
634 const u16 *words = (const u16 *)((u8 *)xfer->tx_buf + offset);
635 const u8 *bytes = (const u8 *)((u8 *)xfer->tx_buf + offset);
636 u16 td0, td1;
637 u32 fifomr;
638
639 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_fifo\n");
640
641 /* Compute the number of data to transfer in the current iteration */
642 current_remaining_data = ((xfer->bits_per_word > 8) ?
643 ((u32)as->current_remaining_bytes >> 1) :
644 (u32)as->current_remaining_bytes);
645 num_data = min(current_remaining_data, as->fifo_size);
646
647 /* Flush RX and TX FIFOs */
648 spi_writel(as, CR, SPI_BIT(RXFCLR) | SPI_BIT(TXFCLR));
649 while (spi_readl(as, FLR))
650 cpu_relax();
651
652 /* Set RX FIFO Threshold to the number of data to transfer */
653 fifomr = spi_readl(as, FMR);
654 spi_writel(as, FMR, SPI_BFINS(RXFTHRES, num_data, fifomr));
655
656 /* Clear FIFO flags in the Status Register, especially RXFTHF */
657 (void)spi_readl(as, SR);
658
659 /* Fill TX FIFO */
660 while (num_data >= 2) {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100661 if (xfer->bits_per_word > 8) {
662 td0 = *words++;
663 td1 = *words++;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200664 } else {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100665 td0 = *bytes++;
666 td1 = *bytes++;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200667 }
668
669 spi_writel(as, TDR, (td1 << 16) | td0);
670 num_data -= 2;
671 }
672
673 if (num_data) {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100674 if (xfer->bits_per_word > 8)
675 td0 = *words++;
676 else
677 td0 = *bytes++;
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200678
679 spi_writew(as, TDR, td0);
680 num_data--;
681 }
682
683 dev_dbg(master->dev.parent,
684 " start fifo xfer %p: len %u tx %p rx %p bitpw %d\n",
685 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
686 xfer->bits_per_word);
687
688 /*
689 * Enable RX FIFO Threshold Flag interrupt to be notified about
690 * transfer completion.
691 */
692 spi_writel(as, IER, SPI_BIT(RXFTHF) | SPI_BIT(OVRES));
693}
694
695/*
696 * Next transfer using PIO.
697 */
698static void atmel_spi_next_xfer_pio(struct spi_master *master,
699 struct spi_transfer *xfer)
700{
701 struct atmel_spi *as = spi_master_get_devdata(master);
702
703 if (as->fifo_size)
704 atmel_spi_next_xfer_fifo(master, xfer);
705 else
706 atmel_spi_next_xfer_single(master, xfer);
707}
708
709/*
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800710 * Submit next transfer for DMA.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800711 */
712static int atmel_spi_next_xfer_dma_submit(struct spi_master *master,
713 struct spi_transfer *xfer,
714 u32 *plen)
715{
716 struct atmel_spi *as = spi_master_get_devdata(master);
717 struct dma_chan *rxchan = as->dma.chan_rx;
718 struct dma_chan *txchan = as->dma.chan_tx;
719 struct dma_async_tx_descriptor *rxdesc;
720 struct dma_async_tx_descriptor *txdesc;
721 struct dma_slave_config slave_config;
722 dma_cookie_t cookie;
723 u32 len = *plen;
724
725 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n");
726
727 /* Check that the channels are available */
728 if (!rxchan || !txchan)
729 return -ENODEV;
730
731 /* release lock for DMA operations */
732 atmel_spi_unlock(as);
733
734 /* prepare the RX dma transfer */
735 sg_init_table(&as->dma.sgrx, 1);
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100736 as->dma.sgrx.dma_address = xfer->rx_dma + xfer->len - *plen;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800737
738 /* prepare the TX dma transfer */
739 sg_init_table(&as->dma.sgtx, 1);
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100740 as->dma.sgtx.dma_address = xfer->tx_dma + xfer->len - *plen;
741
742 if (len > master->max_dma_len)
743 len = master->max_dma_len;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800744
745 sg_dma_len(&as->dma.sgtx) = len;
746 sg_dma_len(&as->dma.sgrx) = len;
747
748 *plen = len;
749
David Mosberger-Tang06515f82015-10-20 14:26:47 +0200750 if (atmel_spi_dma_slave_config(as, &slave_config,
751 xfer->bits_per_word))
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800752 goto err_exit;
753
754 /* Send both scatterlists */
Geert Uytterhoevenef40eb32014-07-11 18:13:28 +0200755 rxdesc = dmaengine_prep_slave_sg(rxchan, &as->dma.sgrx, 1,
756 DMA_FROM_DEVICE,
757 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800758 if (!rxdesc)
759 goto err_dma;
760
Geert Uytterhoevenef40eb32014-07-11 18:13:28 +0200761 txdesc = dmaengine_prep_slave_sg(txchan, &as->dma.sgtx, 1,
762 DMA_TO_DEVICE,
763 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800764 if (!txdesc)
765 goto err_dma;
766
767 dev_dbg(master->dev.parent,
Emil Goode2de024b2013-07-30 19:35:35 +0200768 " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
769 xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
770 xfer->rx_buf, (unsigned long long)xfer->rx_dma);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800771
772 /* Enable relevant interrupts */
773 spi_writel(as, IER, SPI_BIT(OVRES));
774
775 /* Put the callback on the RX transfer only, that should finish last */
776 rxdesc->callback = dma_callback;
777 rxdesc->callback_param = master;
778
779 /* Submit and fire RX and TX with TX last so we're ready to read! */
780 cookie = rxdesc->tx_submit(rxdesc);
781 if (dma_submit_error(cookie))
782 goto err_dma;
783 cookie = txdesc->tx_submit(txdesc);
784 if (dma_submit_error(cookie))
785 goto err_dma;
786 rxchan->device->device_issue_pending(rxchan);
787 txchan->device->device_issue_pending(txchan);
788
789 /* take back lock */
790 atmel_spi_lock(as);
791 return 0;
792
793err_dma:
794 spi_writel(as, IDR, SPI_BIT(OVRES));
795 atmel_spi_stop_dma(as);
796err_exit:
797 atmel_spi_lock(as);
798 return -ENOMEM;
799}
800
Silvester Erdeg154443c2008-02-06 01:38:12 -0800801static void atmel_spi_next_xfer_data(struct spi_master *master,
802 struct spi_transfer *xfer,
803 dma_addr_t *tx_dma,
804 dma_addr_t *rx_dma,
805 u32 *plen)
806{
807 struct atmel_spi *as = spi_master_get_devdata(master);
808 u32 len = *plen;
809
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100810 *rx_dma = xfer->rx_dma + xfer->len - *plen;
811 *tx_dma = xfer->tx_dma + xfer->len - *plen;
812 if (len > master->max_dma_len)
813 len = master->max_dma_len;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800814
815 *plen = len;
816}
817
Richard Genoudd3b72c72013-11-07 10:34:06 +0100818static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
819 struct spi_device *spi,
820 struct spi_transfer *xfer)
821{
822 u32 scbr, csr;
823 unsigned long bus_hz;
824
825 /* v1 chips start out at half the peripheral bus speed. */
826 bus_hz = clk_get_rate(as->clk);
827 if (!atmel_spi_is_v2(as))
828 bus_hz /= 2;
829
830 /*
831 * Calculate the lowest divider that satisfies the
832 * constraint, assuming div32/fdiv/mbz == 0.
833 */
Jarkko Nikulae8646582015-09-25 09:03:01 +0300834 scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
Richard Genoudd3b72c72013-11-07 10:34:06 +0100835
836 /*
837 * If the resulting divider doesn't fit into the
838 * register bitfield, we can't satisfy the constraint.
839 */
840 if (scbr >= (1 << SPI_SCBR_SIZE)) {
841 dev_err(&spi->dev,
842 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
843 xfer->speed_hz, scbr, bus_hz/255);
844 return -EINVAL;
845 }
846 if (scbr == 0) {
847 dev_err(&spi->dev,
848 "setup: %d Hz too high, scbr %u; max %ld Hz\n",
849 xfer->speed_hz, scbr, bus_hz);
850 return -EINVAL;
851 }
852 csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
853 csr = SPI_BFINS(SCBR, scbr, csr);
854 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
855
856 return 0;
857}
858
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800859/*
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800860 * Submit next transfer for PDC.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800861 * lock is held, spi irq is blocked
862 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800863static void atmel_spi_pdc_next_xfer(struct spi_master *master,
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800864 struct spi_message *msg,
865 struct spi_transfer *xfer)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800866{
867 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800868 u32 len;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800869 dma_addr_t tx_dma, rx_dma;
870
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800871 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
Silvester Erdeg154443c2008-02-06 01:38:12 -0800872
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800873 len = as->current_remaining_bytes;
874 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
875 as->current_remaining_bytes -= len;
Gerard Kamdc329442008-08-04 13:41:12 -0700876
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800877 spi_writel(as, RPR, rx_dma);
878 spi_writel(as, TPR, tx_dma);
879
880 if (msg->spi->bits_per_word > 8)
881 len >>= 1;
882 spi_writel(as, RCR, len);
883 spi_writel(as, TCR, len);
884
885 dev_dbg(&msg->spi->dev,
886 " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
887 xfer, xfer->len, xfer->tx_buf,
888 (unsigned long long)xfer->tx_dma, xfer->rx_buf,
889 (unsigned long long)xfer->rx_dma);
890
891 if (as->current_remaining_bytes) {
892 len = as->current_remaining_bytes;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800893 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800894 as->current_remaining_bytes -= len;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800895
896 spi_writel(as, RNPR, rx_dma);
897 spi_writel(as, TNPR, tx_dma);
898
899 if (msg->spi->bits_per_word > 8)
900 len >>= 1;
901 spi_writel(as, RNCR, len);
902 spi_writel(as, TNCR, len);
Haavard Skinnemoen8bacb212008-02-06 01:38:13 -0800903
904 dev_dbg(&msg->spi->dev,
Emil Goode2de024b2013-07-30 19:35:35 +0200905 " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
906 xfer, xfer->len, xfer->tx_buf,
907 (unsigned long long)xfer->tx_dma, xfer->rx_buf,
908 (unsigned long long)xfer->rx_dma);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800909 }
910
Torsten Fleischer76e1d142015-02-24 16:32:57 +0100911 /* REVISIT: We're waiting for RXBUFF before we start the next
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800912 * transfer because we need to handle some difficult timing
Torsten Fleischer76e1d142015-02-24 16:32:57 +0100913 * issues otherwise. If we wait for TXBUFE in one transfer and
914 * then starts waiting for RXBUFF in the next, it's difficult
915 * to tell the difference between the RXBUFF interrupt we're
916 * actually waiting for and the RXBUFF interrupt of the
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800917 * previous transfer.
918 *
919 * It should be doable, though. Just not now...
920 */
Torsten Fleischer76e1d142015-02-24 16:32:57 +0100921 spi_writel(as, IER, SPI_BIT(RXBUFF) | SPI_BIT(OVRES));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800922 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
923}
924
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800925/*
David Brownell8da08592007-07-17 04:04:07 -0700926 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
927 * - The buffer is either valid for CPU access, else NULL
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400928 * - If the buffer is valid, so is its DMA address
David Brownell8da08592007-07-17 04:04:07 -0700929 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400930 * This driver manages the dma address unless message->is_dma_mapped.
David Brownell8da08592007-07-17 04:04:07 -0700931 */
932static int
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800933atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
934{
David Brownell8da08592007-07-17 04:04:07 -0700935 struct device *dev = &as->pdev->dev;
936
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800937 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
David Brownell8da08592007-07-17 04:04:07 -0700938 if (xfer->tx_buf) {
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800939 /* tx_buf is a const void* where we need a void * for the dma
940 * mapping */
941 void *nonconst_tx = (void *)xfer->tx_buf;
942
David Brownell8da08592007-07-17 04:04:07 -0700943 xfer->tx_dma = dma_map_single(dev,
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800944 nonconst_tx, xfer->len,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800945 DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700946 if (dma_mapping_error(dev, xfer->tx_dma))
David Brownell8da08592007-07-17 04:04:07 -0700947 return -ENOMEM;
948 }
949 if (xfer->rx_buf) {
950 xfer->rx_dma = dma_map_single(dev,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800951 xfer->rx_buf, xfer->len,
952 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700953 if (dma_mapping_error(dev, xfer->rx_dma)) {
David Brownell8da08592007-07-17 04:04:07 -0700954 if (xfer->tx_buf)
955 dma_unmap_single(dev,
956 xfer->tx_dma, xfer->len,
957 DMA_TO_DEVICE);
958 return -ENOMEM;
959 }
960 }
961 return 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800962}
963
964static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
965 struct spi_transfer *xfer)
966{
967 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700968 dma_unmap_single(master->dev.parent, xfer->tx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800969 xfer->len, DMA_TO_DEVICE);
970 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700971 dma_unmap_single(master->dev.parent, xfer->rx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800972 xfer->len, DMA_FROM_DEVICE);
973}
974
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800975static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
976{
977 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
978}
979
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800980static void
Cyrille Pitchen11f27642015-06-16 12:09:31 +0200981atmel_spi_pump_single_data(struct atmel_spi *as, struct spi_transfer *xfer)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800982{
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800983 u8 *rxp;
Richard Genoudf557c982013-05-02 19:25:11 +0800984 u16 *rxp16;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800985 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
986
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100987 if (xfer->bits_per_word > 8) {
988 rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
989 *rxp16 = spi_readl(as, RDR);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800990 } else {
Nicolas Ferre7910d9a2016-11-24 12:24:58 +0100991 rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
992 *rxp = spi_readl(as, RDR);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800993 }
Richard Genoudf557c982013-05-02 19:25:11 +0800994 if (xfer->bits_per_word > 8) {
Alexandre Bellonib112f052014-05-06 17:44:41 +0200995 if (as->current_remaining_bytes > 2)
996 as->current_remaining_bytes -= 2;
997 else
Richard Genoudf557c982013-05-02 19:25:11 +0800998 as->current_remaining_bytes = 0;
999 } else {
1000 as->current_remaining_bytes--;
1001 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001002}
1003
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001004static void
1005atmel_spi_pump_fifo_data(struct atmel_spi *as, struct spi_transfer *xfer)
1006{
1007 u32 fifolr = spi_readl(as, FLR);
1008 u32 num_bytes, num_data = SPI_BFEXT(RXFL, fifolr);
1009 u32 offset = xfer->len - as->current_remaining_bytes;
1010 u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset);
1011 u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset);
1012 u16 rd; /* RD field is the lowest 16 bits of RDR */
1013
1014 /* Update the number of remaining bytes to transfer */
1015 num_bytes = ((xfer->bits_per_word > 8) ?
1016 (num_data << 1) :
1017 num_data);
1018
1019 if (as->current_remaining_bytes > num_bytes)
1020 as->current_remaining_bytes -= num_bytes;
1021 else
1022 as->current_remaining_bytes = 0;
1023
1024 /* Handle odd number of bytes when data are more than 8bit width */
1025 if (xfer->bits_per_word > 8)
1026 as->current_remaining_bytes &= ~0x1;
1027
1028 /* Read data */
1029 while (num_data) {
1030 rd = spi_readl(as, RDR);
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001031 if (xfer->bits_per_word > 8)
1032 *words++ = rd;
1033 else
1034 *bytes++ = rd;
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001035 num_data--;
1036 }
1037}
1038
1039/* Called from IRQ
1040 *
1041 * Must update "current_remaining_bytes" to keep track of data
1042 * to transfer.
1043 */
1044static void
1045atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
1046{
1047 if (as->fifo_size)
1048 atmel_spi_pump_fifo_data(as, xfer);
1049 else
1050 atmel_spi_pump_single_data(as, xfer);
1051}
1052
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001053/* Interrupt
1054 *
1055 * No need for locking in this Interrupt handler: done_status is the
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001056 * only information modified.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001057 */
1058static irqreturn_t
1059atmel_spi_pio_interrupt(int irq, void *dev_id)
1060{
1061 struct spi_master *master = dev_id;
1062 struct atmel_spi *as = spi_master_get_devdata(master);
1063 u32 status, pending, imr;
1064 struct spi_transfer *xfer;
1065 int ret = IRQ_NONE;
1066
1067 imr = spi_readl(as, IMR);
1068 status = spi_readl(as, SR);
1069 pending = status & imr;
1070
1071 if (pending & SPI_BIT(OVRES)) {
1072 ret = IRQ_HANDLED;
1073 spi_writel(as, IDR, SPI_BIT(OVRES));
1074 dev_warn(master->dev.parent, "overrun\n");
1075
1076 /*
1077 * When we get an overrun, we disregard the current
1078 * transfer. Data will not be copied back from any
1079 * bounce buffer and msg->actual_len will not be
1080 * updated with the last xfer.
1081 *
1082 * We will also not process any remaning transfers in
1083 * the message.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001084 */
1085 as->done_status = -EIO;
1086 smp_wmb();
1087
1088 /* Clear any overrun happening while cleaning up */
1089 spi_readl(as, SR);
1090
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001091 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001092
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001093 } else if (pending & (SPI_BIT(RDRF) | SPI_BIT(RXFTHF))) {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001094 atmel_spi_lock(as);
1095
1096 if (as->current_remaining_bytes) {
1097 ret = IRQ_HANDLED;
1098 xfer = as->current_transfer;
1099 atmel_spi_pump_pio_data(as, xfer);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001100 if (!as->current_remaining_bytes)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001101 spi_writel(as, IDR, pending);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001102
1103 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001104 }
1105
1106 atmel_spi_unlock(as);
1107 } else {
1108 WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
1109 ret = IRQ_HANDLED;
1110 spi_writel(as, IDR, pending);
1111 }
1112
1113 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001114}
1115
1116static irqreturn_t
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001117atmel_spi_pdc_interrupt(int irq, void *dev_id)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001118{
1119 struct spi_master *master = dev_id;
1120 struct atmel_spi *as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001121 u32 status, pending, imr;
1122 int ret = IRQ_NONE;
1123
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001124 imr = spi_readl(as, IMR);
1125 status = spi_readl(as, SR);
1126 pending = status & imr;
1127
1128 if (pending & SPI_BIT(OVRES)) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001129
1130 ret = IRQ_HANDLED;
1131
Gerard Kamdc329442008-08-04 13:41:12 -07001132 spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001133 | SPI_BIT(OVRES)));
1134
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001135 /* Clear any overrun happening while cleaning up */
1136 spi_readl(as, SR);
1137
Nicolas Ferre823cd042013-03-19 15:45:01 +08001138 as->done_status = -EIO;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001139
1140 complete(&as->xfer_completion);
1141
Gerard Kamdc329442008-08-04 13:41:12 -07001142 } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001143 ret = IRQ_HANDLED;
1144
1145 spi_writel(as, IDR, pending);
1146
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001147 complete(&as->xfer_completion);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001148 }
1149
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001150 return ret;
1151}
1152
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001153static int atmel_spi_setup(struct spi_device *spi)
1154{
1155 struct atmel_spi *as;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001156 struct atmel_spi_device *asd;
Richard Genoudd3b72c72013-11-07 10:34:06 +01001157 u32 csr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001158 unsigned int bits = spi->bits_per_word;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001159 unsigned int npcs_pin;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001160
1161 as = spi_master_get_devdata(spi->master);
1162
David Brownelldefbd3b2007-07-17 04:04:08 -07001163 /* see notes above re chipselect */
Wenyou Yangd4820b72013-03-19 15:42:15 +08001164 if (!atmel_spi_is_v2(as)
David Brownelldefbd3b2007-07-17 04:04:08 -07001165 && spi->chip_select == 0
1166 && (spi->mode & SPI_CS_HIGH)) {
1167 dev_dbg(&spi->dev, "setup: can't be active-high\n");
1168 return -EINVAL;
1169 }
1170
Richard Genoudd3b72c72013-11-07 10:34:06 +01001171 csr = SPI_BF(BITS, bits - 8);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001172 if (spi->mode & SPI_CPOL)
1173 csr |= SPI_BIT(CPOL);
1174 if (!(spi->mode & SPI_CPHA))
1175 csr |= SPI_BIT(NCPHA);
Cyrille Pitchen48203032015-06-09 13:53:52 +02001176 if (!as->use_cs_gpios)
1177 csr |= SPI_BIT(CSAAT);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001178
Haavard Skinnemoen1eed29d2008-02-06 01:38:11 -08001179 /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
1180 *
1181 * DLYBCT would add delays between words, slowing down transfers.
1182 * It could potentially be useful to cope with DMA bottlenecks, but
1183 * in those cases it's probably best to just use a lower bitrate.
1184 */
1185 csr |= SPI_BF(DLYBS, 0);
1186 csr |= SPI_BF(DLYBCT, 0);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001187
1188 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
Mark Brown67f08d62014-08-01 17:43:03 +01001189 npcs_pin = (unsigned long)spi->controller_data;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001190
Cyrille Pitchen48203032015-06-09 13:53:52 +02001191 if (!as->use_cs_gpios)
1192 npcs_pin = spi->chip_select;
1193 else if (gpio_is_valid(spi->cs_gpio))
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001194 npcs_pin = spi->cs_gpio;
1195
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001196 asd = spi->controller_state;
1197 if (!asd) {
1198 asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
1199 if (!asd)
1200 return -ENOMEM;
1201
Nicolas Ferre96106202016-11-08 18:48:52 +01001202 if (as->use_cs_gpios)
Cyrille Pitchen48203032015-06-09 13:53:52 +02001203 gpio_direction_output(npcs_pin,
1204 !(spi->mode & SPI_CS_HIGH));
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001205
1206 asd->npcs_pin = npcs_pin;
1207 spi->controller_state = asd;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001208 }
1209
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001210 asd->csr = csr;
1211
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001212 dev_dbg(&spi->dev,
Richard Genoudd3b72c72013-11-07 10:34:06 +01001213 "setup: bpw %u mode 0x%x -> csr%d %08x\n",
1214 bits, spi->mode, spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001215
Wenyou Yangd4820b72013-03-19 15:42:15 +08001216 if (!atmel_spi_is_v2(as))
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001217 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001218
1219 return 0;
1220}
1221
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001222static int atmel_spi_one_transfer(struct spi_master *master,
1223 struct spi_message *msg,
1224 struct spi_transfer *xfer)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001225{
1226 struct atmel_spi *as;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001227 struct spi_device *spi = msg->spi;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +02001228 u8 bits;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001229 u32 len;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +02001230 struct atmel_spi_device *asd;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001231 int timeout;
1232 int ret;
Nicholas Mc Guire1369dea2015-02-02 10:43:31 -05001233 unsigned long dma_timeout;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001234
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001235 as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001236
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001237 if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1238 dev_dbg(&spi->dev, "missing rx or tx buf\n");
1239 return -EINVAL;
1240 }
1241
Jarkko Nikulae8646582015-09-25 09:03:01 +03001242 asd = spi->controller_state;
1243 bits = (asd->csr >> 4) & 0xf;
1244 if (bits != xfer->bits_per_word - 8) {
1245 dev_dbg(&spi->dev,
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001246 "you can't yet change bits_per_word in transfers\n");
Jarkko Nikulae8646582015-09-25 09:03:01 +03001247 return -ENOPROTOOPT;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001248 }
1249
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001250 /*
1251 * DMA map early, for performance (empties dcache ASAP) and
1252 * better fault reporting.
1253 */
1254 if ((!msg->is_dma_mapped)
1255 && (atmel_spi_use_dma(as, xfer) || as->use_pdc)) {
1256 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
1257 return -ENOMEM;
1258 }
1259
1260 atmel_spi_set_xfer_speed(as, msg->spi, xfer);
1261
1262 as->done_status = 0;
1263 as->current_transfer = xfer;
1264 as->current_remaining_bytes = xfer->len;
1265 while (as->current_remaining_bytes) {
1266 reinit_completion(&as->xfer_completion);
1267
1268 if (as->use_pdc) {
1269 atmel_spi_pdc_next_xfer(master, msg, xfer);
1270 } else if (atmel_spi_use_dma(as, xfer)) {
1271 len = as->current_remaining_bytes;
1272 ret = atmel_spi_next_xfer_dma_submit(master,
1273 xfer, &len);
1274 if (ret) {
1275 dev_err(&spi->dev,
1276 "unable to use DMA, fallback to PIO\n");
1277 atmel_spi_next_xfer_pio(master, xfer);
1278 } else {
1279 as->current_remaining_bytes -= len;
Axel Lin0c3b9742014-03-27 09:26:38 +08001280 if (as->current_remaining_bytes < 0)
1281 as->current_remaining_bytes = 0;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001282 }
1283 } else {
1284 atmel_spi_next_xfer_pio(master, xfer);
1285 }
1286
Alexander Stein16760142014-04-13 12:45:10 +02001287 /* interrupts are disabled, so free the lock for schedule */
1288 atmel_spi_unlock(as);
Nicholas Mc Guire1369dea2015-02-02 10:43:31 -05001289 dma_timeout = wait_for_completion_timeout(&as->xfer_completion,
1290 SPI_DMA_TIMEOUT);
Alexander Stein16760142014-04-13 12:45:10 +02001291 atmel_spi_lock(as);
Nicholas Mc Guire1369dea2015-02-02 10:43:31 -05001292 if (WARN_ON(dma_timeout == 0)) {
1293 dev_err(&spi->dev, "spi transfer timeout\n");
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001294 as->done_status = -EIO;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001295 }
1296
1297 if (as->done_status)
1298 break;
1299 }
1300
1301 if (as->done_status) {
1302 if (as->use_pdc) {
1303 dev_warn(master->dev.parent,
1304 "overrun (%u/%u remaining)\n",
1305 spi_readl(as, TCR), spi_readl(as, RCR));
1306
1307 /*
1308 * Clean up DMA registers and make sure the data
1309 * registers are empty.
1310 */
1311 spi_writel(as, RNCR, 0);
1312 spi_writel(as, TNCR, 0);
1313 spi_writel(as, RCR, 0);
1314 spi_writel(as, TCR, 0);
1315 for (timeout = 1000; timeout; timeout--)
1316 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
1317 break;
1318 if (!timeout)
1319 dev_warn(master->dev.parent,
1320 "timeout waiting for TXEMPTY");
1321 while (spi_readl(as, SR) & SPI_BIT(RDRF))
1322 spi_readl(as, RDR);
1323
1324 /* Clear any overrun happening while cleaning up */
1325 spi_readl(as, SR);
1326
1327 } else if (atmel_spi_use_dma(as, xfer)) {
1328 atmel_spi_stop_dma(as);
1329 }
1330
1331 if (!msg->is_dma_mapped
1332 && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
1333 atmel_spi_dma_unmap_xfer(master, xfer);
1334
1335 return 0;
1336
1337 } else {
1338 /* only update length if no error */
1339 msg->actual_length += xfer->len;
1340 }
1341
1342 if (!msg->is_dma_mapped
1343 && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
1344 atmel_spi_dma_unmap_xfer(master, xfer);
1345
1346 if (xfer->delay_usecs)
1347 udelay(xfer->delay_usecs);
1348
1349 if (xfer->cs_change) {
1350 if (list_is_last(&xfer->transfer_list,
1351 &msg->transfers)) {
1352 as->keep_cs = true;
1353 } else {
1354 as->cs_active = !as->cs_active;
1355 if (as->cs_active)
1356 cs_activate(as, msg->spi);
1357 else
1358 cs_deactivate(as, msg->spi);
1359 }
1360 }
1361
1362 return 0;
1363}
1364
1365static int atmel_spi_transfer_one_message(struct spi_master *master,
1366 struct spi_message *msg)
1367{
1368 struct atmel_spi *as;
1369 struct spi_transfer *xfer;
1370 struct spi_device *spi = msg->spi;
1371 int ret = 0;
1372
1373 as = spi_master_get_devdata(master);
1374
1375 dev_dbg(&spi->dev, "new message %p submitted for %s\n",
1376 msg, dev_name(&spi->dev));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001377
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001378 atmel_spi_lock(as);
1379 cs_activate(as, spi);
1380
1381 as->cs_active = true;
1382 as->keep_cs = false;
1383
1384 msg->status = 0;
1385 msg->actual_length = 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001386
1387 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001388 ret = atmel_spi_one_transfer(master, msg, xfer);
1389 if (ret)
1390 goto msg_done;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001391 }
1392
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001393 if (as->use_pdc)
1394 atmel_spi_disable_pdc_transfer(as);
1395
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001396 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001397 dev_dbg(&spi->dev,
Randy Dunlap54f4c512014-03-21 08:53:41 -07001398 " xfer %p: len %u tx %p/%pad rx %p/%pad\n",
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001399 xfer, xfer->len,
Randy Dunlap54f4c512014-03-21 08:53:41 -07001400 xfer->tx_buf, &xfer->tx_dma,
1401 xfer->rx_buf, &xfer->rx_dma);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001402 }
1403
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001404msg_done:
1405 if (!as->keep_cs)
1406 cs_deactivate(as, msg->spi);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001407
Nicolas Ferre8aad7922013-04-03 13:58:36 +08001408 atmel_spi_unlock(as);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001409
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001410 msg->status = as->done_status;
1411 spi_finalize_current_message(spi->master);
1412
1413 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001414}
1415
David Brownellbb2d1c32007-02-20 13:58:19 -08001416static void atmel_spi_cleanup(struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001417{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001418 struct atmel_spi_device *asd = spi->controller_state;
David Brownelldefbd3b2007-07-17 04:04:08 -07001419
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001420 if (!asd)
David Brownelldefbd3b2007-07-17 04:04:08 -07001421 return;
1422
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001423 spi->controller_state = NULL;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001424 kfree(asd);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001425}
1426
Wenyou Yangd4820b72013-03-19 15:42:15 +08001427static inline unsigned int atmel_get_version(struct atmel_spi *as)
1428{
1429 return spi_readl(as, VERSION) & 0x00000fff;
1430}
1431
1432static void atmel_get_caps(struct atmel_spi *as)
1433{
1434 unsigned int version;
1435
1436 version = atmel_get_version(as);
1437 dev_info(&as->pdev->dev, "version: 0x%x\n", version);
1438
1439 as->caps.is_spi2 = version > 0x121;
1440 as->caps.has_wdrbt = version >= 0x210;
1441 as->caps.has_dma_support = version >= 0x212;
1442}
1443
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001444/*-------------------------------------------------------------------------*/
Nicolas Ferre96106202016-11-08 18:48:52 +01001445static int atmel_spi_gpio_cs(struct platform_device *pdev)
1446{
1447 struct spi_master *master = platform_get_drvdata(pdev);
1448 struct atmel_spi *as = spi_master_get_devdata(master);
1449 struct device_node *np = master->dev.of_node;
1450 int i;
1451 int ret = 0;
1452 int nb = 0;
1453
1454 if (!as->use_cs_gpios)
1455 return 0;
1456
1457 if (!np)
1458 return 0;
1459
1460 nb = of_gpio_named_count(np, "cs-gpios");
1461 for (i = 0; i < nb; i++) {
1462 int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
1463 "cs-gpios", i);
1464
Dan Carpenterb52b3482016-11-14 17:26:44 +03001465 if (cs_gpio == -EPROBE_DEFER)
1466 return cs_gpio;
Nicolas Ferre96106202016-11-08 18:48:52 +01001467
Dan Carpenterb52b3482016-11-14 17:26:44 +03001468 if (gpio_is_valid(cs_gpio)) {
1469 ret = devm_gpio_request(&pdev->dev, cs_gpio,
1470 dev_name(&pdev->dev));
1471 if (ret)
1472 return ret;
1473 }
Nicolas Ferre96106202016-11-08 18:48:52 +01001474 }
1475
1476 return 0;
1477}
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001478
Grant Likelyfd4a3192012-12-07 16:57:14 +00001479static int atmel_spi_probe(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001480{
1481 struct resource *regs;
1482 int irq;
1483 struct clk *clk;
1484 int ret;
1485 struct spi_master *master;
1486 struct atmel_spi *as;
1487
Wenyou Yang5bdfd492014-03-05 09:58:49 +08001488 /* Select default pin state */
1489 pinctrl_pm_select_default_state(&pdev->dev);
1490
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001491 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1492 if (!regs)
1493 return -ENXIO;
1494
1495 irq = platform_get_irq(pdev, 0);
1496 if (irq < 0)
1497 return irq;
1498
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001499 clk = devm_clk_get(&pdev->dev, "spi_clk");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001500 if (IS_ERR(clk))
1501 return PTR_ERR(clk);
1502
1503 /* setup spi core then atmel-specific driver state */
1504 ret = -ENOMEM;
Sachin Kamata536d762013-09-10 17:06:27 +05301505 master = spi_alloc_master(&pdev->dev, sizeof(*as));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001506 if (!master)
1507 goto out_free;
1508
David Brownelle7db06b2009-06-17 16:26:04 -07001509 /* the spi->mode bits understood by this driver: */
1510 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001511 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001512 master->dev.of_node = pdev->dev.of_node;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001513 master->bus_num = pdev->id;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001514 master->num_chipselect = master->dev.of_node ? 0 : 4;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001515 master->setup = atmel_spi_setup;
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001516 master->flags = (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001517 master->transfer_one_message = atmel_spi_transfer_one_message;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001518 master->cleanup = atmel_spi_cleanup;
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001519 master->auto_runtime_pm = true;
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001520 master->max_dma_len = SPI_MAX_DMA_XFER;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001521 platform_set_drvdata(pdev, master);
1522
1523 as = spi_master_get_devdata(master);
1524
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001525 spin_lock_init(&as->lock);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001526
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001527 as->pdev = pdev;
Mark Brown31407472013-10-16 13:22:35 +01001528 as->regs = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun543c9542013-10-21 11:12:02 +08001529 if (IS_ERR(as->regs)) {
1530 ret = PTR_ERR(as->regs);
Nicolas Ferre7910d9a2016-11-24 12:24:58 +01001531 goto out_unmap_regs;
Wei Yongjun543c9542013-10-21 11:12:02 +08001532 }
Nicolas Ferredfab30e2013-04-03 13:57:42 +08001533 as->phybase = regs->start;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001534 as->irq = irq;
1535 as->clk = clk;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001536
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001537 init_completion(&as->xfer_completion);
1538
Wenyou Yangd4820b72013-03-19 15:42:15 +08001539 atmel_get_caps(as);
1540
Cyrille Pitchen48203032015-06-09 13:53:52 +02001541 as->use_cs_gpios = true;
1542 if (atmel_spi_is_v2(as) &&
Cyrille Pitchen70f340d2016-01-27 17:48:32 +01001543 pdev->dev.of_node &&
Cyrille Pitchen48203032015-06-09 13:53:52 +02001544 !of_get_property(pdev->dev.of_node, "cs-gpios", NULL)) {
1545 as->use_cs_gpios = false;
1546 master->num_chipselect = 4;
1547 }
1548
Nicolas Ferre96106202016-11-08 18:48:52 +01001549 ret = atmel_spi_gpio_cs(pdev);
1550 if (ret)
1551 goto out_unmap_regs;
1552
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001553 as->use_dma = false;
1554 as->use_pdc = false;
1555 if (as->caps.has_dma_support) {
Ludovic Desroches5e9af372014-11-14 17:12:54 +01001556 ret = atmel_spi_configure_dma(as);
1557 if (ret == 0)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001558 as->use_dma = true;
Ludovic Desroches5e9af372014-11-14 17:12:54 +01001559 else if (ret == -EPROBE_DEFER)
1560 return ret;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001561 } else {
1562 as->use_pdc = true;
1563 }
1564
1565 if (as->caps.has_dma_support && !as->use_dma)
1566 dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
1567
1568 if (as->use_pdc) {
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001569 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
1570 0, dev_name(&pdev->dev), master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001571 } else {
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001572 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
1573 0, dev_name(&pdev->dev), master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001574 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001575 if (ret)
1576 goto out_unmap_regs;
1577
1578 /* Initialize the hardware */
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001579 ret = clk_prepare_enable(clk);
1580 if (ret)
Sachin Kamatde8cc232013-09-10 17:06:26 +05301581 goto out_free_irq;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001582 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001583 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Wenyou Yangd4820b72013-03-19 15:42:15 +08001584 if (as->caps.has_wdrbt) {
1585 spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
1586 | SPI_BIT(MSTR));
1587 } else {
1588 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
1589 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001590
1591 if (as->use_pdc)
1592 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001593 spi_writel(as, CR, SPI_BIT(SPIEN));
1594
Cyrille Pitchen11f27642015-06-16 12:09:31 +02001595 as->fifo_size = 0;
1596 if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
1597 &as->fifo_size)) {
1598 dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size);
1599 spi_writel(as, CR, SPI_BIT(FIFOEN));
1600 }
1601
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001602 pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
1603 pm_runtime_use_autosuspend(&pdev->dev);
1604 pm_runtime_set_active(&pdev->dev);
1605 pm_runtime_enable(&pdev->dev);
1606
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001607 ret = devm_spi_register_master(&pdev->dev, master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001608 if (ret)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001609 goto out_free_dma;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001610
Nicolas Ferrece24a512016-11-24 12:24:57 +01001611 /* go! */
1612 dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
1613 (unsigned long)regs->start, irq);
1614
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001615 return 0;
1616
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001617out_free_dma:
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001618 pm_runtime_disable(&pdev->dev);
1619 pm_runtime_set_suspended(&pdev->dev);
1620
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001621 if (as->use_dma)
1622 atmel_spi_release_dma(as);
1623
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001624 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001625 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001626 clk_disable_unprepare(clk);
Sachin Kamatde8cc232013-09-10 17:06:26 +05301627out_free_irq:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001628out_unmap_regs:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001629out_free:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001630 spi_master_put(master);
1631 return ret;
1632}
1633
Grant Likelyfd4a3192012-12-07 16:57:14 +00001634static int atmel_spi_remove(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001635{
1636 struct spi_master *master = platform_get_drvdata(pdev);
1637 struct atmel_spi *as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001638
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001639 pm_runtime_get_sync(&pdev->dev);
1640
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001641 /* reset the hardware and block queue progress */
1642 spin_lock_irq(&as->lock);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001643 if (as->use_dma) {
1644 atmel_spi_stop_dma(as);
1645 atmel_spi_release_dma(as);
1646 }
1647
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001648 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001649 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001650 spi_readl(as, SR);
1651 spin_unlock_irq(&as->lock);
1652
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001653 clk_disable_unprepare(as->clk);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001654
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001655 pm_runtime_put_noidle(&pdev->dev);
1656 pm_runtime_disable(&pdev->dev);
1657
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001658 return 0;
1659}
1660
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001661#ifdef CONFIG_PM
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001662static int atmel_spi_runtime_suspend(struct device *dev)
1663{
1664 struct spi_master *master = dev_get_drvdata(dev);
1665 struct atmel_spi *as = spi_master_get_devdata(master);
Jingoo Hanec60dd32013-09-09 17:54:12 +09001666
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001667 clk_disable_unprepare(as->clk);
1668 pinctrl_pm_select_sleep_state(dev);
1669
1670 return 0;
1671}
1672
1673static int atmel_spi_runtime_resume(struct device *dev)
1674{
1675 struct spi_master *master = dev_get_drvdata(dev);
1676 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001677
1678 pinctrl_pm_select_default_state(dev);
1679
Fengguang Wud0de6ff2014-10-17 00:18:56 +08001680 return clk_prepare_enable(as->clk);
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001681}
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001682
Alexandre Bellonid6305262015-09-10 10:19:52 +02001683#ifdef CONFIG_PM_SLEEP
Wenyou Yangc1ee8f32014-10-21 11:43:34 +08001684static int atmel_spi_suspend(struct device *dev)
1685{
1686 struct spi_master *master = dev_get_drvdata(dev);
1687 int ret;
1688
1689 /* Stop the queue running */
1690 ret = spi_master_suspend(master);
1691 if (ret) {
1692 dev_warn(dev, "cannot suspend master\n");
1693 return ret;
1694 }
1695
1696 if (!pm_runtime_suspended(dev))
1697 atmel_spi_runtime_suspend(dev);
1698
1699 return 0;
1700}
1701
1702static int atmel_spi_resume(struct device *dev)
1703{
1704 struct spi_master *master = dev_get_drvdata(dev);
1705 int ret;
1706
1707 if (!pm_runtime_suspended(dev)) {
1708 ret = atmel_spi_runtime_resume(dev);
1709 if (ret)
1710 return ret;
1711 }
1712
1713 /* Start the queue running */
1714 ret = spi_master_resume(master);
1715 if (ret)
1716 dev_err(dev, "problem starting queue (%d)\n", ret);
1717
1718 return ret;
1719}
Alexandre Bellonid6305262015-09-10 10:19:52 +02001720#endif
Wenyou Yangce0c4ca2014-10-16 17:23:10 +08001721
1722static const struct dev_pm_ops atmel_spi_pm_ops = {
1723 SET_SYSTEM_SLEEP_PM_OPS(atmel_spi_suspend, atmel_spi_resume)
1724 SET_RUNTIME_PM_OPS(atmel_spi_runtime_suspend,
1725 atmel_spi_runtime_resume, NULL)
1726};
Jingoo Hanec60dd32013-09-09 17:54:12 +09001727#define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001728#else
Jingoo Hanec60dd32013-09-09 17:54:12 +09001729#define ATMEL_SPI_PM_OPS NULL
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001730#endif
1731
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001732#if defined(CONFIG_OF)
1733static const struct of_device_id atmel_spi_dt_ids[] = {
1734 { .compatible = "atmel,at91rm9200-spi" },
1735 { /* sentinel */ }
1736};
1737
1738MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
1739#endif
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001740
1741static struct platform_driver atmel_spi_driver = {
1742 .driver = {
1743 .name = "atmel_spi",
Jingoo Hanec60dd32013-09-09 17:54:12 +09001744 .pm = ATMEL_SPI_PM_OPS,
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001745 .of_match_table = of_match_ptr(atmel_spi_dt_ids),
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001746 },
Jean-Christophe PLAGNIOL-VILLARD1cb201a2011-11-04 01:20:21 +08001747 .probe = atmel_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +00001748 .remove = atmel_spi_remove,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001749};
Grant Likely940ab882011-10-05 11:29:49 -06001750module_platform_driver(atmel_spi_driver);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001751
1752MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001753MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001754MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001755MODULE_ALIAS("platform:atmel_spi");