blob: 649dcb5a603c9eed48b9ae565caaeb7839540574 [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>
Jean-Christophe PLAGNIOL-VILLARDbcd23602012-10-30 05:12:23 +080022#include <linux/platform_data/atmel.h>
Nicolas Ferre1ccc4042013-04-03 13:59:19 +080023#include <linux/platform_data/dma-atmel.h>
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +010024#include <linux/of.h>
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -080025
Wenyou Yangd4820b72013-03-19 15:42:15 +080026#include <linux/io.h>
27#include <linux/gpio.h>
Wenyou Yang5bdfd492014-03-05 09:58:49 +080028#include <linux/pinctrl/consumer.h>
David Brownellbb2d1c32007-02-20 13:58:19 -080029
Grant Likelyca632f52011-06-06 01:16:30 -060030/* SPI register offsets */
31#define SPI_CR 0x0000
32#define SPI_MR 0x0004
33#define SPI_RDR 0x0008
34#define SPI_TDR 0x000c
35#define SPI_SR 0x0010
36#define SPI_IER 0x0014
37#define SPI_IDR 0x0018
38#define SPI_IMR 0x001c
39#define SPI_CSR0 0x0030
40#define SPI_CSR1 0x0034
41#define SPI_CSR2 0x0038
42#define SPI_CSR3 0x003c
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
64
65/* Bitfields in MR */
66#define SPI_MSTR_OFFSET 0
67#define SPI_MSTR_SIZE 1
68#define SPI_PS_OFFSET 1
69#define SPI_PS_SIZE 1
70#define SPI_PCSDEC_OFFSET 2
71#define SPI_PCSDEC_SIZE 1
72#define SPI_FDIV_OFFSET 3
73#define SPI_FDIV_SIZE 1
74#define SPI_MODFDIS_OFFSET 4
75#define SPI_MODFDIS_SIZE 1
Wenyou Yangd4820b72013-03-19 15:42:15 +080076#define SPI_WDRBT_OFFSET 5
77#define SPI_WDRBT_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -060078#define SPI_LLB_OFFSET 7
79#define SPI_LLB_SIZE 1
80#define SPI_PCS_OFFSET 16
81#define SPI_PCS_SIZE 4
82#define SPI_DLYBCS_OFFSET 24
83#define SPI_DLYBCS_SIZE 8
84
85/* Bitfields in RDR */
86#define SPI_RD_OFFSET 0
87#define SPI_RD_SIZE 16
88
89/* Bitfields in TDR */
90#define SPI_TD_OFFSET 0
91#define SPI_TD_SIZE 16
92
93/* Bitfields in SR */
94#define SPI_RDRF_OFFSET 0
95#define SPI_RDRF_SIZE 1
96#define SPI_TDRE_OFFSET 1
97#define SPI_TDRE_SIZE 1
98#define SPI_MODF_OFFSET 2
99#define SPI_MODF_SIZE 1
100#define SPI_OVRES_OFFSET 3
101#define SPI_OVRES_SIZE 1
102#define SPI_ENDRX_OFFSET 4
103#define SPI_ENDRX_SIZE 1
104#define SPI_ENDTX_OFFSET 5
105#define SPI_ENDTX_SIZE 1
106#define SPI_RXBUFF_OFFSET 6
107#define SPI_RXBUFF_SIZE 1
108#define SPI_TXBUFE_OFFSET 7
109#define SPI_TXBUFE_SIZE 1
110#define SPI_NSSR_OFFSET 8
111#define SPI_NSSR_SIZE 1
112#define SPI_TXEMPTY_OFFSET 9
113#define SPI_TXEMPTY_SIZE 1
114#define SPI_SPIENS_OFFSET 16
115#define SPI_SPIENS_SIZE 1
116
117/* Bitfields in CSR0 */
118#define SPI_CPOL_OFFSET 0
119#define SPI_CPOL_SIZE 1
120#define SPI_NCPHA_OFFSET 1
121#define SPI_NCPHA_SIZE 1
122#define SPI_CSAAT_OFFSET 3
123#define SPI_CSAAT_SIZE 1
124#define SPI_BITS_OFFSET 4
125#define SPI_BITS_SIZE 4
126#define SPI_SCBR_OFFSET 8
127#define SPI_SCBR_SIZE 8
128#define SPI_DLYBS_OFFSET 16
129#define SPI_DLYBS_SIZE 8
130#define SPI_DLYBCT_OFFSET 24
131#define SPI_DLYBCT_SIZE 8
132
133/* Bitfields in RCR */
134#define SPI_RXCTR_OFFSET 0
135#define SPI_RXCTR_SIZE 16
136
137/* Bitfields in TCR */
138#define SPI_TXCTR_OFFSET 0
139#define SPI_TXCTR_SIZE 16
140
141/* Bitfields in RNCR */
142#define SPI_RXNCR_OFFSET 0
143#define SPI_RXNCR_SIZE 16
144
145/* Bitfields in TNCR */
146#define SPI_TXNCR_OFFSET 0
147#define SPI_TXNCR_SIZE 16
148
149/* Bitfields in PTCR */
150#define SPI_RXTEN_OFFSET 0
151#define SPI_RXTEN_SIZE 1
152#define SPI_RXTDIS_OFFSET 1
153#define SPI_RXTDIS_SIZE 1
154#define SPI_TXTEN_OFFSET 8
155#define SPI_TXTEN_SIZE 1
156#define SPI_TXTDIS_OFFSET 9
157#define SPI_TXTDIS_SIZE 1
158
159/* Constants for BITS */
160#define SPI_BITS_8_BPT 0
161#define SPI_BITS_9_BPT 1
162#define SPI_BITS_10_BPT 2
163#define SPI_BITS_11_BPT 3
164#define SPI_BITS_12_BPT 4
165#define SPI_BITS_13_BPT 5
166#define SPI_BITS_14_BPT 6
167#define SPI_BITS_15_BPT 7
168#define SPI_BITS_16_BPT 8
169
170/* Bit manipulation macros */
171#define SPI_BIT(name) \
172 (1 << SPI_##name##_OFFSET)
Sachin Kamata536d762013-09-10 17:06:27 +0530173#define SPI_BF(name, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600174 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
Sachin Kamata536d762013-09-10 17:06:27 +0530175#define SPI_BFEXT(name, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600176 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
Sachin Kamata536d762013-09-10 17:06:27 +0530177#define SPI_BFINS(name, value, old) \
178 (((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
179 | SPI_BF(name, value))
Grant Likelyca632f52011-06-06 01:16:30 -0600180
181/* Register access macros */
Sachin Kamata536d762013-09-10 17:06:27 +0530182#define spi_readl(port, reg) \
Grant Likelyca632f52011-06-06 01:16:30 -0600183 __raw_readl((port)->regs + SPI_##reg)
Sachin Kamata536d762013-09-10 17:06:27 +0530184#define spi_writel(port, reg, value) \
Grant Likelyca632f52011-06-06 01:16:30 -0600185 __raw_writel((value), (port)->regs + SPI_##reg)
186
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800187/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
188 * cache operations; better heuristics consider wordsize and bitrate.
189 */
190#define DMA_MIN_BYTES 16
191
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800192#define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000))
193
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800194struct atmel_spi_dma {
195 struct dma_chan *chan_rx;
196 struct dma_chan *chan_tx;
197 struct scatterlist sgrx;
198 struct scatterlist sgtx;
199 struct dma_async_tx_descriptor *data_desc_rx;
200 struct dma_async_tx_descriptor *data_desc_tx;
201
202 struct at_dma_slave dma_slave;
203};
204
Wenyou Yangd4820b72013-03-19 15:42:15 +0800205struct atmel_spi_caps {
206 bool is_spi2;
207 bool has_wdrbt;
208 bool has_dma_support;
209};
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800210
211/*
212 * The core SPI transfer engine just talks to a register bank to set up
213 * DMA transfers; transfer queue progress is driven by IRQs. The clock
214 * framework provides the base clock, subdivided for each spi_device.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800215 */
216struct atmel_spi {
217 spinlock_t lock;
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800218 unsigned long flags;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800219
Nicolas Ferredfab30e2013-04-03 13:57:42 +0800220 phys_addr_t phybase;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800221 void __iomem *regs;
222 int irq;
223 struct clk *clk;
224 struct platform_device *pdev;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800225
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800226 struct spi_transfer *current_transfer;
Axel Lin0c3b9742014-03-27 09:26:38 +0800227 int current_remaining_bytes;
Nicolas Ferre823cd042013-03-19 15:45:01 +0800228 int done_status;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800229
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800230 struct completion xfer_completion;
231
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800232 /* scratch buffer */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800233 void *buffer;
234 dma_addr_t buffer_dma;
Wenyou Yangd4820b72013-03-19 15:42:15 +0800235
236 struct atmel_spi_caps caps;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800237
238 bool use_dma;
239 bool use_pdc;
240 /* dmaengine data */
241 struct atmel_spi_dma dma;
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800242
243 bool keep_cs;
244 bool cs_active;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800245};
246
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800247/* Controller-specific per-slave state */
248struct atmel_spi_device {
249 unsigned int npcs_pin;
250 u32 csr;
251};
252
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800253#define BUFFER_SIZE PAGE_SIZE
254#define INVALID_DMA_ADDRESS 0xffffffff
255
256/*
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800257 * Version 2 of the SPI controller has
258 * - CR.LASTXFER
259 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
260 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
261 * - SPI_CSRx.CSAAT
262 * - SPI_CSRx.SBCR allows faster clocking
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800263 */
Wenyou Yangd4820b72013-03-19 15:42:15 +0800264static bool atmel_spi_is_v2(struct atmel_spi *as)
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800265{
Wenyou Yangd4820b72013-03-19 15:42:15 +0800266 return as->caps.is_spi2;
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800267}
268
269/*
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800270 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
271 * they assume that spi slave device state will not change on deselect, so
David Brownelldefbd3b2007-07-17 04:04:08 -0700272 * that automagic deselection is OK. ("NPCSx rises if no data is to be
273 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
274 * controllers have CSAAT and friends.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800275 *
David Brownelldefbd3b2007-07-17 04:04:08 -0700276 * Since the CSAAT functionality is a bit weird on newer controllers as
277 * well, we use GPIO to control nCSx pins on all controllers, updating
278 * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
279 * support active-high chipselects despite the controller's belief that
280 * only active-low devices/systems exists.
281 *
282 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
283 * right when driven with GPIO. ("Mode Fault does not allow more than one
284 * Master on Chip Select 0.") No workaround exists for that ... so for
285 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
286 * and (c) will trigger that first erratum in some cases.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800287 */
288
David Brownelldefbd3b2007-07-17 04:04:08 -0700289static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800290{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800291 struct atmel_spi_device *asd = spi->controller_state;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800292 unsigned active = spi->mode & SPI_CS_HIGH;
David Brownelldefbd3b2007-07-17 04:04:08 -0700293 u32 mr;
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800294
Wenyou Yangd4820b72013-03-19 15:42:15 +0800295 if (atmel_spi_is_v2(as)) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800296 spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
297 /* For the low SPI version, there is a issue that PDC transfer
298 * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800299 */
300 spi_writel(as, CSR0, asd->csr);
Wenyou Yangd4820b72013-03-19 15:42:15 +0800301 if (as->caps.has_wdrbt) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800302 spi_writel(as, MR,
303 SPI_BF(PCS, ~(0x01 << spi->chip_select))
304 | SPI_BIT(WDRBT)
305 | SPI_BIT(MODFDIS)
306 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800307 } else {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800308 spi_writel(as, MR,
309 SPI_BF(PCS, ~(0x01 << spi->chip_select))
310 | SPI_BIT(MODFDIS)
311 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800312 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800313
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800314 mr = spi_readl(as, MR);
315 gpio_set_value(asd->npcs_pin, active);
316 } else {
317 u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
318 int i;
319 u32 csr;
320
321 /* Make sure clock polarity is correct */
322 for (i = 0; i < spi->master->num_chipselect; i++) {
323 csr = spi_readl(as, CSR0 + 4 * i);
324 if ((csr ^ cpol) & SPI_BIT(CPOL))
325 spi_writel(as, CSR0 + 4 * i,
326 csr ^ SPI_BIT(CPOL));
327 }
328
329 mr = spi_readl(as, MR);
330 mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
331 if (spi->chip_select != 0)
332 gpio_set_value(asd->npcs_pin, active);
333 spi_writel(as, MR, mr);
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800334 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800335
David Brownelldefbd3b2007-07-17 04:04:08 -0700336 dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800337 asd->npcs_pin, active ? " (high)" : "",
David Brownelldefbd3b2007-07-17 04:04:08 -0700338 mr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800339}
340
David Brownelldefbd3b2007-07-17 04:04:08 -0700341static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800342{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800343 struct atmel_spi_device *asd = spi->controller_state;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800344 unsigned active = spi->mode & SPI_CS_HIGH;
David Brownelldefbd3b2007-07-17 04:04:08 -0700345 u32 mr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800346
David Brownelldefbd3b2007-07-17 04:04:08 -0700347 /* only deactivate *this* device; sometimes transfers to
348 * another device may be active when this routine is called.
349 */
350 mr = spi_readl(as, MR);
351 if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
352 mr = SPI_BFINS(PCS, 0xf, mr);
353 spi_writel(as, MR, mr);
354 }
355
356 dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800357 asd->npcs_pin, active ? " (low)" : "",
David Brownelldefbd3b2007-07-17 04:04:08 -0700358 mr);
359
Wenyou Yangd4820b72013-03-19 15:42:15 +0800360 if (atmel_spi_is_v2(as) || spi->chip_select != 0)
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800361 gpio_set_value(asd->npcs_pin, !active);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800362}
363
Mark Brown6c07ef22013-07-28 14:32:27 +0100364static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock)
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800365{
366 spin_lock_irqsave(&as->lock, as->flags);
367}
368
Mark Brown6c07ef22013-07-28 14:32:27 +0100369static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock)
Nicolas Ferre8aad7922013-04-03 13:58:36 +0800370{
371 spin_unlock_irqrestore(&as->lock, as->flags);
372}
373
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800374static inline bool atmel_spi_use_dma(struct atmel_spi *as,
375 struct spi_transfer *xfer)
376{
377 return as->use_dma && xfer->len >= DMA_MIN_BYTES;
378}
379
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800380static int atmel_spi_dma_slave_config(struct atmel_spi *as,
381 struct dma_slave_config *slave_config,
382 u8 bits_per_word)
383{
384 int err = 0;
385
386 if (bits_per_word > 8) {
387 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
388 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES;
389 } else {
390 slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
391 slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
392 }
393
394 slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR;
395 slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR;
396 slave_config->src_maxburst = 1;
397 slave_config->dst_maxburst = 1;
398 slave_config->device_fc = false;
399
400 slave_config->direction = DMA_MEM_TO_DEV;
401 if (dmaengine_slave_config(as->dma.chan_tx, slave_config)) {
402 dev_err(&as->pdev->dev,
403 "failed to configure tx dma channel\n");
404 err = -EINVAL;
405 }
406
407 slave_config->direction = DMA_DEV_TO_MEM;
408 if (dmaengine_slave_config(as->dma.chan_rx, slave_config)) {
409 dev_err(&as->pdev->dev,
410 "failed to configure rx dma channel\n");
411 err = -EINVAL;
412 }
413
414 return err;
415}
416
Richard Genoud2f767a92013-05-31 17:01:59 +0200417static bool filter(struct dma_chan *chan, void *pdata)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800418{
Richard Genoud2f767a92013-05-31 17:01:59 +0200419 struct atmel_spi_dma *sl_pdata = pdata;
420 struct at_dma_slave *sl;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800421
Richard Genoud2f767a92013-05-31 17:01:59 +0200422 if (!sl_pdata)
423 return false;
424
425 sl = &sl_pdata->dma_slave;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800426 if (sl->dma_dev == chan->device->dev) {
427 chan->private = sl;
428 return true;
429 } else {
430 return false;
431 }
432}
433
434static int atmel_spi_configure_dma(struct atmel_spi *as)
435{
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800436 struct dma_slave_config slave_config;
Richard Genoud2f767a92013-05-31 17:01:59 +0200437 struct device *dev = &as->pdev->dev;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800438 int err;
439
Richard Genoud2f767a92013-05-31 17:01:59 +0200440 dma_cap_mask_t mask;
441 dma_cap_zero(mask);
442 dma_cap_set(DMA_SLAVE, mask);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800443
Richard Genoud2f767a92013-05-31 17:01:59 +0200444 as->dma.chan_tx = dma_request_slave_channel_compat(mask, filter,
445 &as->dma,
446 dev, "tx");
447 if (!as->dma.chan_tx) {
448 dev_err(dev,
449 "DMA TX channel not available, SPI unable to use DMA\n");
450 err = -EBUSY;
451 goto error;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800452 }
Richard Genoud2f767a92013-05-31 17:01:59 +0200453
454 as->dma.chan_rx = dma_request_slave_channel_compat(mask, filter,
455 &as->dma,
456 dev, "rx");
457
458 if (!as->dma.chan_rx) {
459 dev_err(dev,
460 "DMA RX channel not available, SPI unable to use DMA\n");
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800461 err = -EBUSY;
462 goto error;
463 }
464
465 err = atmel_spi_dma_slave_config(as, &slave_config, 8);
466 if (err)
467 goto error;
468
469 dev_info(&as->pdev->dev,
470 "Using %s (tx) and %s (rx) for DMA transfers\n",
471 dma_chan_name(as->dma.chan_tx),
472 dma_chan_name(as->dma.chan_rx));
473 return 0;
474error:
475 if (as->dma.chan_rx)
476 dma_release_channel(as->dma.chan_rx);
477 if (as->dma.chan_tx)
478 dma_release_channel(as->dma.chan_tx);
479 return err;
480}
481
482static void atmel_spi_stop_dma(struct atmel_spi *as)
483{
484 if (as->dma.chan_rx)
Vinod Koul5398ad62014-10-11 21:10:35 +0530485 dmaengine_terminate_all(as->dma.chan_rx);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800486 if (as->dma.chan_tx)
Vinod Koul5398ad62014-10-11 21:10:35 +0530487 dmaengine_terminate_all(as->dma.chan_tx);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800488}
489
490static void atmel_spi_release_dma(struct atmel_spi *as)
491{
492 if (as->dma.chan_rx)
493 dma_release_channel(as->dma.chan_rx);
494 if (as->dma.chan_tx)
495 dma_release_channel(as->dma.chan_tx);
496}
497
498/* This function is called by the DMA driver from tasklet context */
499static void dma_callback(void *data)
500{
501 struct spi_master *master = data;
502 struct atmel_spi *as = spi_master_get_devdata(master);
503
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800504 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800505}
506
507/*
508 * Next transfer using PIO.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800509 */
510static void atmel_spi_next_xfer_pio(struct spi_master *master,
511 struct spi_transfer *xfer)
512{
513 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800514 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800515
516 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n");
517
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800518 /* Make sure data is not remaining in RDR */
519 spi_readl(as, RDR);
520 while (spi_readl(as, SR) & SPI_BIT(RDRF)) {
521 spi_readl(as, RDR);
522 cpu_relax();
523 }
524
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800525 if (xfer->tx_buf) {
Richard Genoudf557c982013-05-02 19:25:11 +0800526 if (xfer->bits_per_word > 8)
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800527 spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos));
Richard Genoudf557c982013-05-02 19:25:11 +0800528 else
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800529 spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos));
530 } else {
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800531 spi_writel(as, TDR, 0);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800532 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800533
534 dev_dbg(master->dev.parent,
Richard Genoudf557c982013-05-02 19:25:11 +0800535 " start pio xfer %p: len %u tx %p rx %p bitpw %d\n",
536 xfer, xfer->len, xfer->tx_buf, xfer->rx_buf,
537 xfer->bits_per_word);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800538
539 /* Enable relevant interrupts */
540 spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES));
541}
542
543/*
544 * Submit next transfer for DMA.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800545 */
546static int atmel_spi_next_xfer_dma_submit(struct spi_master *master,
547 struct spi_transfer *xfer,
548 u32 *plen)
549{
550 struct atmel_spi *as = spi_master_get_devdata(master);
551 struct dma_chan *rxchan = as->dma.chan_rx;
552 struct dma_chan *txchan = as->dma.chan_tx;
553 struct dma_async_tx_descriptor *rxdesc;
554 struct dma_async_tx_descriptor *txdesc;
555 struct dma_slave_config slave_config;
556 dma_cookie_t cookie;
557 u32 len = *plen;
558
559 dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n");
560
561 /* Check that the channels are available */
562 if (!rxchan || !txchan)
563 return -ENODEV;
564
565 /* release lock for DMA operations */
566 atmel_spi_unlock(as);
567
568 /* prepare the RX dma transfer */
569 sg_init_table(&as->dma.sgrx, 1);
570 if (xfer->rx_buf) {
571 as->dma.sgrx.dma_address = xfer->rx_dma + xfer->len - *plen;
572 } else {
573 as->dma.sgrx.dma_address = as->buffer_dma;
574 if (len > BUFFER_SIZE)
575 len = BUFFER_SIZE;
576 }
577
578 /* prepare the TX dma transfer */
579 sg_init_table(&as->dma.sgtx, 1);
580 if (xfer->tx_buf) {
581 as->dma.sgtx.dma_address = xfer->tx_dma + xfer->len - *plen;
582 } else {
583 as->dma.sgtx.dma_address = as->buffer_dma;
584 if (len > BUFFER_SIZE)
585 len = BUFFER_SIZE;
586 memset(as->buffer, 0, len);
587 }
588
589 sg_dma_len(&as->dma.sgtx) = len;
590 sg_dma_len(&as->dma.sgrx) = len;
591
592 *plen = len;
593
594 if (atmel_spi_dma_slave_config(as, &slave_config, 8))
595 goto err_exit;
596
597 /* Send both scatterlists */
Geert Uytterhoevenef40eb32014-07-11 18:13:28 +0200598 rxdesc = dmaengine_prep_slave_sg(rxchan, &as->dma.sgrx, 1,
599 DMA_FROM_DEVICE,
600 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800601 if (!rxdesc)
602 goto err_dma;
603
Geert Uytterhoevenef40eb32014-07-11 18:13:28 +0200604 txdesc = dmaengine_prep_slave_sg(txchan, &as->dma.sgtx, 1,
605 DMA_TO_DEVICE,
606 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800607 if (!txdesc)
608 goto err_dma;
609
610 dev_dbg(master->dev.parent,
Emil Goode2de024b2013-07-30 19:35:35 +0200611 " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
612 xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma,
613 xfer->rx_buf, (unsigned long long)xfer->rx_dma);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800614
615 /* Enable relevant interrupts */
616 spi_writel(as, IER, SPI_BIT(OVRES));
617
618 /* Put the callback on the RX transfer only, that should finish last */
619 rxdesc->callback = dma_callback;
620 rxdesc->callback_param = master;
621
622 /* Submit and fire RX and TX with TX last so we're ready to read! */
623 cookie = rxdesc->tx_submit(rxdesc);
624 if (dma_submit_error(cookie))
625 goto err_dma;
626 cookie = txdesc->tx_submit(txdesc);
627 if (dma_submit_error(cookie))
628 goto err_dma;
629 rxchan->device->device_issue_pending(rxchan);
630 txchan->device->device_issue_pending(txchan);
631
632 /* take back lock */
633 atmel_spi_lock(as);
634 return 0;
635
636err_dma:
637 spi_writel(as, IDR, SPI_BIT(OVRES));
638 atmel_spi_stop_dma(as);
639err_exit:
640 atmel_spi_lock(as);
641 return -ENOMEM;
642}
643
Silvester Erdeg154443c2008-02-06 01:38:12 -0800644static void atmel_spi_next_xfer_data(struct spi_master *master,
645 struct spi_transfer *xfer,
646 dma_addr_t *tx_dma,
647 dma_addr_t *rx_dma,
648 u32 *plen)
649{
650 struct atmel_spi *as = spi_master_get_devdata(master);
651 u32 len = *plen;
652
653 /* use scratch buffer only when rx or tx data is unspecified */
654 if (xfer->rx_buf)
Ben Nizette6aed4ee2009-12-14 22:20:20 -0800655 *rx_dma = xfer->rx_dma + xfer->len - *plen;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800656 else {
657 *rx_dma = as->buffer_dma;
658 if (len > BUFFER_SIZE)
659 len = BUFFER_SIZE;
660 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800661
Silvester Erdeg154443c2008-02-06 01:38:12 -0800662 if (xfer->tx_buf)
Ben Nizette6aed4ee2009-12-14 22:20:20 -0800663 *tx_dma = xfer->tx_dma + xfer->len - *plen;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800664 else {
665 *tx_dma = as->buffer_dma;
666 if (len > BUFFER_SIZE)
667 len = BUFFER_SIZE;
668 memset(as->buffer, 0, len);
669 dma_sync_single_for_device(&as->pdev->dev,
670 as->buffer_dma, len, DMA_TO_DEVICE);
671 }
672
673 *plen = len;
674}
675
Richard Genoudd3b72c72013-11-07 10:34:06 +0100676static int atmel_spi_set_xfer_speed(struct atmel_spi *as,
677 struct spi_device *spi,
678 struct spi_transfer *xfer)
679{
680 u32 scbr, csr;
681 unsigned long bus_hz;
682
683 /* v1 chips start out at half the peripheral bus speed. */
684 bus_hz = clk_get_rate(as->clk);
685 if (!atmel_spi_is_v2(as))
686 bus_hz /= 2;
687
688 /*
689 * Calculate the lowest divider that satisfies the
690 * constraint, assuming div32/fdiv/mbz == 0.
691 */
692 if (xfer->speed_hz)
693 scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz);
694 else
695 /*
696 * This can happend if max_speed is null.
697 * In this case, we set the lowest possible speed
698 */
699 scbr = 0xff;
700
701 /*
702 * If the resulting divider doesn't fit into the
703 * register bitfield, we can't satisfy the constraint.
704 */
705 if (scbr >= (1 << SPI_SCBR_SIZE)) {
706 dev_err(&spi->dev,
707 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
708 xfer->speed_hz, scbr, bus_hz/255);
709 return -EINVAL;
710 }
711 if (scbr == 0) {
712 dev_err(&spi->dev,
713 "setup: %d Hz too high, scbr %u; max %ld Hz\n",
714 xfer->speed_hz, scbr, bus_hz);
715 return -EINVAL;
716 }
717 csr = spi_readl(as, CSR0 + 4 * spi->chip_select);
718 csr = SPI_BFINS(SCBR, scbr, csr);
719 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
720
721 return 0;
722}
723
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800724/*
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800725 * Submit next transfer for PDC.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800726 * lock is held, spi irq is blocked
727 */
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800728static void atmel_spi_pdc_next_xfer(struct spi_master *master,
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800729 struct spi_message *msg,
730 struct spi_transfer *xfer)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800731{
732 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800733 u32 len;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800734 dma_addr_t tx_dma, rx_dma;
735
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800736 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
Silvester Erdeg154443c2008-02-06 01:38:12 -0800737
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800738 len = as->current_remaining_bytes;
739 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
740 as->current_remaining_bytes -= len;
Gerard Kamdc329442008-08-04 13:41:12 -0700741
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800742 spi_writel(as, RPR, rx_dma);
743 spi_writel(as, TPR, tx_dma);
744
745 if (msg->spi->bits_per_word > 8)
746 len >>= 1;
747 spi_writel(as, RCR, len);
748 spi_writel(as, TCR, len);
749
750 dev_dbg(&msg->spi->dev,
751 " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
752 xfer, xfer->len, xfer->tx_buf,
753 (unsigned long long)xfer->tx_dma, xfer->rx_buf,
754 (unsigned long long)xfer->rx_dma);
755
756 if (as->current_remaining_bytes) {
757 len = as->current_remaining_bytes;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800758 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800759 as->current_remaining_bytes -= len;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800760
761 spi_writel(as, RNPR, rx_dma);
762 spi_writel(as, TNPR, tx_dma);
763
764 if (msg->spi->bits_per_word > 8)
765 len >>= 1;
766 spi_writel(as, RNCR, len);
767 spi_writel(as, TNCR, len);
Haavard Skinnemoen8bacb212008-02-06 01:38:13 -0800768
769 dev_dbg(&msg->spi->dev,
Emil Goode2de024b2013-07-30 19:35:35 +0200770 " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
771 xfer, xfer->len, xfer->tx_buf,
772 (unsigned long long)xfer->tx_dma, xfer->rx_buf,
773 (unsigned long long)xfer->rx_dma);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800774 }
775
Silvester Erdeg154443c2008-02-06 01:38:12 -0800776 /* REVISIT: We're waiting for ENDRX before we start the next
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800777 * transfer because we need to handle some difficult timing
778 * issues otherwise. If we wait for ENDTX in one transfer and
779 * then starts waiting for ENDRX in the next, it's difficult
780 * to tell the difference between the ENDRX interrupt we're
781 * actually waiting for and the ENDRX interrupt of the
782 * previous transfer.
783 *
784 * It should be doable, though. Just not now...
785 */
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800786 spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800787 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
788}
789
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800790/*
David Brownell8da08592007-07-17 04:04:07 -0700791 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
792 * - The buffer is either valid for CPU access, else NULL
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400793 * - If the buffer is valid, so is its DMA address
David Brownell8da08592007-07-17 04:04:07 -0700794 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400795 * This driver manages the dma address unless message->is_dma_mapped.
David Brownell8da08592007-07-17 04:04:07 -0700796 */
797static int
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800798atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
799{
David Brownell8da08592007-07-17 04:04:07 -0700800 struct device *dev = &as->pdev->dev;
801
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800802 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
David Brownell8da08592007-07-17 04:04:07 -0700803 if (xfer->tx_buf) {
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800804 /* tx_buf is a const void* where we need a void * for the dma
805 * mapping */
806 void *nonconst_tx = (void *)xfer->tx_buf;
807
David Brownell8da08592007-07-17 04:04:07 -0700808 xfer->tx_dma = dma_map_single(dev,
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800809 nonconst_tx, xfer->len,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800810 DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700811 if (dma_mapping_error(dev, xfer->tx_dma))
David Brownell8da08592007-07-17 04:04:07 -0700812 return -ENOMEM;
813 }
814 if (xfer->rx_buf) {
815 xfer->rx_dma = dma_map_single(dev,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800816 xfer->rx_buf, xfer->len,
817 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700818 if (dma_mapping_error(dev, xfer->rx_dma)) {
David Brownell8da08592007-07-17 04:04:07 -0700819 if (xfer->tx_buf)
820 dma_unmap_single(dev,
821 xfer->tx_dma, xfer->len,
822 DMA_TO_DEVICE);
823 return -ENOMEM;
824 }
825 }
826 return 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800827}
828
829static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
830 struct spi_transfer *xfer)
831{
832 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700833 dma_unmap_single(master->dev.parent, xfer->tx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800834 xfer->len, DMA_TO_DEVICE);
835 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700836 dma_unmap_single(master->dev.parent, xfer->rx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800837 xfer->len, DMA_FROM_DEVICE);
838}
839
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800840static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as)
841{
842 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
843}
844
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800845/* Called from IRQ
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800846 *
847 * Must update "current_remaining_bytes" to keep track of data
848 * to transfer.
849 */
850static void
851atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer)
852{
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800853 u8 *rxp;
Richard Genoudf557c982013-05-02 19:25:11 +0800854 u16 *rxp16;
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800855 unsigned long xfer_pos = xfer->len - as->current_remaining_bytes;
856
857 if (xfer->rx_buf) {
Richard Genoudf557c982013-05-02 19:25:11 +0800858 if (xfer->bits_per_word > 8) {
859 rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos);
860 *rxp16 = spi_readl(as, RDR);
861 } else {
862 rxp = ((u8 *)xfer->rx_buf) + xfer_pos;
863 *rxp = spi_readl(as, RDR);
864 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800865 } else {
866 spi_readl(as, RDR);
867 }
Richard Genoudf557c982013-05-02 19:25:11 +0800868 if (xfer->bits_per_word > 8) {
Alexandre Bellonib112f052014-05-06 17:44:41 +0200869 if (as->current_remaining_bytes > 2)
870 as->current_remaining_bytes -= 2;
871 else
Richard Genoudf557c982013-05-02 19:25:11 +0800872 as->current_remaining_bytes = 0;
873 } else {
874 as->current_remaining_bytes--;
875 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800876}
877
878/* Interrupt
879 *
880 * No need for locking in this Interrupt handler: done_status is the
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800881 * only information modified.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800882 */
883static irqreturn_t
884atmel_spi_pio_interrupt(int irq, void *dev_id)
885{
886 struct spi_master *master = dev_id;
887 struct atmel_spi *as = spi_master_get_devdata(master);
888 u32 status, pending, imr;
889 struct spi_transfer *xfer;
890 int ret = IRQ_NONE;
891
892 imr = spi_readl(as, IMR);
893 status = spi_readl(as, SR);
894 pending = status & imr;
895
896 if (pending & SPI_BIT(OVRES)) {
897 ret = IRQ_HANDLED;
898 spi_writel(as, IDR, SPI_BIT(OVRES));
899 dev_warn(master->dev.parent, "overrun\n");
900
901 /*
902 * When we get an overrun, we disregard the current
903 * transfer. Data will not be copied back from any
904 * bounce buffer and msg->actual_len will not be
905 * updated with the last xfer.
906 *
907 * We will also not process any remaning transfers in
908 * the message.
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800909 */
910 as->done_status = -EIO;
911 smp_wmb();
912
913 /* Clear any overrun happening while cleaning up */
914 spi_readl(as, SR);
915
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800916 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800917
918 } else if (pending & SPI_BIT(RDRF)) {
919 atmel_spi_lock(as);
920
921 if (as->current_remaining_bytes) {
922 ret = IRQ_HANDLED;
923 xfer = as->current_transfer;
924 atmel_spi_pump_pio_data(as, xfer);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800925 if (!as->current_remaining_bytes)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800926 spi_writel(as, IDR, pending);
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800927
928 complete(&as->xfer_completion);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800929 }
930
931 atmel_spi_unlock(as);
932 } else {
933 WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending);
934 ret = IRQ_HANDLED;
935 spi_writel(as, IDR, pending);
936 }
937
938 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800939}
940
941static irqreturn_t
Nicolas Ferre1ccc4042013-04-03 13:59:19 +0800942atmel_spi_pdc_interrupt(int irq, void *dev_id)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800943{
944 struct spi_master *master = dev_id;
945 struct atmel_spi *as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800946 u32 status, pending, imr;
947 int ret = IRQ_NONE;
948
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800949 imr = spi_readl(as, IMR);
950 status = spi_readl(as, SR);
951 pending = status & imr;
952
953 if (pending & SPI_BIT(OVRES)) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800954
955 ret = IRQ_HANDLED;
956
Gerard Kamdc329442008-08-04 13:41:12 -0700957 spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800958 | SPI_BIT(OVRES)));
959
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800960 /* Clear any overrun happening while cleaning up */
961 spi_readl(as, SR);
962
Nicolas Ferre823cd042013-03-19 15:45:01 +0800963 as->done_status = -EIO;
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800964
965 complete(&as->xfer_completion);
966
Gerard Kamdc329442008-08-04 13:41:12 -0700967 } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800968 ret = IRQ_HANDLED;
969
970 spi_writel(as, IDR, pending);
971
Wenyou Yang8090d6d2014-01-09 13:19:15 +0800972 complete(&as->xfer_completion);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800973 }
974
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800975 return ret;
976}
977
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800978static int atmel_spi_setup(struct spi_device *spi)
979{
980 struct atmel_spi *as;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800981 struct atmel_spi_device *asd;
Richard Genoudd3b72c72013-11-07 10:34:06 +0100982 u32 csr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800983 unsigned int bits = spi->bits_per_word;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800984 unsigned int npcs_pin;
985 int ret;
986
987 as = spi_master_get_devdata(spi->master);
988
David Brownelldefbd3b2007-07-17 04:04:08 -0700989 /* see notes above re chipselect */
Wenyou Yangd4820b72013-03-19 15:42:15 +0800990 if (!atmel_spi_is_v2(as)
David Brownelldefbd3b2007-07-17 04:04:08 -0700991 && spi->chip_select == 0
992 && (spi->mode & SPI_CS_HIGH)) {
993 dev_dbg(&spi->dev, "setup: can't be active-high\n");
994 return -EINVAL;
995 }
996
Richard Genoudd3b72c72013-11-07 10:34:06 +0100997 csr = SPI_BF(BITS, bits - 8);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800998 if (spi->mode & SPI_CPOL)
999 csr |= SPI_BIT(CPOL);
1000 if (!(spi->mode & SPI_CPHA))
1001 csr |= SPI_BIT(NCPHA);
1002
Haavard Skinnemoen1eed29d2008-02-06 01:38:11 -08001003 /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
1004 *
1005 * DLYBCT would add delays between words, slowing down transfers.
1006 * It could potentially be useful to cope with DMA bottlenecks, but
1007 * in those cases it's probably best to just use a lower bitrate.
1008 */
1009 csr |= SPI_BF(DLYBS, 0);
1010 csr |= SPI_BF(DLYBCT, 0);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001011
1012 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
Mark Brown67f08d62014-08-01 17:43:03 +01001013 npcs_pin = (unsigned long)spi->controller_data;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001014
1015 if (gpio_is_valid(spi->cs_gpio))
1016 npcs_pin = spi->cs_gpio;
1017
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001018 asd = spi->controller_state;
1019 if (!asd) {
1020 asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
1021 if (!asd)
1022 return -ENOMEM;
1023
Kay Sievers6c7377a2009-03-24 16:38:21 -07001024 ret = gpio_request(npcs_pin, dev_name(&spi->dev));
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001025 if (ret) {
1026 kfree(asd);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001027 return ret;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001028 }
1029
1030 asd->npcs_pin = npcs_pin;
1031 spi->controller_state = asd;
David Brownell28735a72007-03-16 13:38:14 -08001032 gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001033 }
1034
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001035 asd->csr = csr;
1036
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001037 dev_dbg(&spi->dev,
Richard Genoudd3b72c72013-11-07 10:34:06 +01001038 "setup: bpw %u mode 0x%x -> csr%d %08x\n",
1039 bits, spi->mode, spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001040
Wenyou Yangd4820b72013-03-19 15:42:15 +08001041 if (!atmel_spi_is_v2(as))
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001042 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001043
1044 return 0;
1045}
1046
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001047static int atmel_spi_one_transfer(struct spi_master *master,
1048 struct spi_message *msg,
1049 struct spi_transfer *xfer)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001050{
1051 struct atmel_spi *as;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001052 struct spi_device *spi = msg->spi;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +02001053 u8 bits;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001054 u32 len;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +02001055 struct atmel_spi_device *asd;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001056 int timeout;
1057 int ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001058
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001059 as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001060
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001061 if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
1062 dev_dbg(&spi->dev, "missing rx or tx buf\n");
1063 return -EINVAL;
1064 }
1065
1066 if (xfer->bits_per_word) {
1067 asd = spi->controller_state;
1068 bits = (asd->csr >> 4) & 0xf;
1069 if (bits != xfer->bits_per_word - 8) {
1070 dev_dbg(&spi->dev,
1071 "you can't yet change bits_per_word in transfers\n");
1072 return -ENOPROTOOPT;
1073 }
1074 }
1075
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001076 /*
1077 * DMA map early, for performance (empties dcache ASAP) and
1078 * better fault reporting.
1079 */
1080 if ((!msg->is_dma_mapped)
1081 && (atmel_spi_use_dma(as, xfer) || as->use_pdc)) {
1082 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
1083 return -ENOMEM;
1084 }
1085
1086 atmel_spi_set_xfer_speed(as, msg->spi, xfer);
1087
1088 as->done_status = 0;
1089 as->current_transfer = xfer;
1090 as->current_remaining_bytes = xfer->len;
1091 while (as->current_remaining_bytes) {
1092 reinit_completion(&as->xfer_completion);
1093
1094 if (as->use_pdc) {
1095 atmel_spi_pdc_next_xfer(master, msg, xfer);
1096 } else if (atmel_spi_use_dma(as, xfer)) {
1097 len = as->current_remaining_bytes;
1098 ret = atmel_spi_next_xfer_dma_submit(master,
1099 xfer, &len);
1100 if (ret) {
1101 dev_err(&spi->dev,
1102 "unable to use DMA, fallback to PIO\n");
1103 atmel_spi_next_xfer_pio(master, xfer);
1104 } else {
1105 as->current_remaining_bytes -= len;
Axel Lin0c3b9742014-03-27 09:26:38 +08001106 if (as->current_remaining_bytes < 0)
1107 as->current_remaining_bytes = 0;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001108 }
1109 } else {
1110 atmel_spi_next_xfer_pio(master, xfer);
1111 }
1112
Alexander Stein16760142014-04-13 12:45:10 +02001113 /* interrupts are disabled, so free the lock for schedule */
1114 atmel_spi_unlock(as);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001115 ret = wait_for_completion_timeout(&as->xfer_completion,
1116 SPI_DMA_TIMEOUT);
Alexander Stein16760142014-04-13 12:45:10 +02001117 atmel_spi_lock(as);
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001118 if (WARN_ON(ret == 0)) {
1119 dev_err(&spi->dev,
1120 "spi trasfer timeout, err %d\n", ret);
1121 as->done_status = -EIO;
1122 } else {
1123 ret = 0;
1124 }
1125
1126 if (as->done_status)
1127 break;
1128 }
1129
1130 if (as->done_status) {
1131 if (as->use_pdc) {
1132 dev_warn(master->dev.parent,
1133 "overrun (%u/%u remaining)\n",
1134 spi_readl(as, TCR), spi_readl(as, RCR));
1135
1136 /*
1137 * Clean up DMA registers and make sure the data
1138 * registers are empty.
1139 */
1140 spi_writel(as, RNCR, 0);
1141 spi_writel(as, TNCR, 0);
1142 spi_writel(as, RCR, 0);
1143 spi_writel(as, TCR, 0);
1144 for (timeout = 1000; timeout; timeout--)
1145 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
1146 break;
1147 if (!timeout)
1148 dev_warn(master->dev.parent,
1149 "timeout waiting for TXEMPTY");
1150 while (spi_readl(as, SR) & SPI_BIT(RDRF))
1151 spi_readl(as, RDR);
1152
1153 /* Clear any overrun happening while cleaning up */
1154 spi_readl(as, SR);
1155
1156 } else if (atmel_spi_use_dma(as, xfer)) {
1157 atmel_spi_stop_dma(as);
1158 }
1159
1160 if (!msg->is_dma_mapped
1161 && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
1162 atmel_spi_dma_unmap_xfer(master, xfer);
1163
1164 return 0;
1165
1166 } else {
1167 /* only update length if no error */
1168 msg->actual_length += xfer->len;
1169 }
1170
1171 if (!msg->is_dma_mapped
1172 && (atmel_spi_use_dma(as, xfer) || as->use_pdc))
1173 atmel_spi_dma_unmap_xfer(master, xfer);
1174
1175 if (xfer->delay_usecs)
1176 udelay(xfer->delay_usecs);
1177
1178 if (xfer->cs_change) {
1179 if (list_is_last(&xfer->transfer_list,
1180 &msg->transfers)) {
1181 as->keep_cs = true;
1182 } else {
1183 as->cs_active = !as->cs_active;
1184 if (as->cs_active)
1185 cs_activate(as, msg->spi);
1186 else
1187 cs_deactivate(as, msg->spi);
1188 }
1189 }
1190
1191 return 0;
1192}
1193
1194static int atmel_spi_transfer_one_message(struct spi_master *master,
1195 struct spi_message *msg)
1196{
1197 struct atmel_spi *as;
1198 struct spi_transfer *xfer;
1199 struct spi_device *spi = msg->spi;
1200 int ret = 0;
1201
1202 as = spi_master_get_devdata(master);
1203
1204 dev_dbg(&spi->dev, "new message %p submitted for %s\n",
1205 msg, dev_name(&spi->dev));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001206
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001207 atmel_spi_lock(as);
1208 cs_activate(as, spi);
1209
1210 as->cs_active = true;
1211 as->keep_cs = false;
1212
1213 msg->status = 0;
1214 msg->actual_length = 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001215
1216 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001217 ret = atmel_spi_one_transfer(master, msg, xfer);
1218 if (ret)
1219 goto msg_done;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001220 }
1221
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001222 if (as->use_pdc)
1223 atmel_spi_disable_pdc_transfer(as);
1224
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001225 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001226 dev_dbg(&spi->dev,
Randy Dunlap54f4c512014-03-21 08:53:41 -07001227 " xfer %p: len %u tx %p/%pad rx %p/%pad\n",
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001228 xfer, xfer->len,
Randy Dunlap54f4c512014-03-21 08:53:41 -07001229 xfer->tx_buf, &xfer->tx_dma,
1230 xfer->rx_buf, &xfer->rx_dma);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001231 }
1232
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001233msg_done:
1234 if (!as->keep_cs)
1235 cs_deactivate(as, msg->spi);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001236
Nicolas Ferre8aad7922013-04-03 13:58:36 +08001237 atmel_spi_unlock(as);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001238
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001239 msg->status = as->done_status;
1240 spi_finalize_current_message(spi->master);
1241
1242 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001243}
1244
David Brownellbb2d1c32007-02-20 13:58:19 -08001245static void atmel_spi_cleanup(struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001246{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001247 struct atmel_spi_device *asd = spi->controller_state;
Mark Brown67f08d62014-08-01 17:43:03 +01001248 unsigned gpio = (unsigned long) spi->controller_data;
David Brownelldefbd3b2007-07-17 04:04:08 -07001249
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001250 if (!asd)
David Brownelldefbd3b2007-07-17 04:04:08 -07001251 return;
1252
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001253 spi->controller_state = NULL;
David Brownelldefbd3b2007-07-17 04:04:08 -07001254 gpio_free(gpio);
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -08001255 kfree(asd);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001256}
1257
Wenyou Yangd4820b72013-03-19 15:42:15 +08001258static inline unsigned int atmel_get_version(struct atmel_spi *as)
1259{
1260 return spi_readl(as, VERSION) & 0x00000fff;
1261}
1262
1263static void atmel_get_caps(struct atmel_spi *as)
1264{
1265 unsigned int version;
1266
1267 version = atmel_get_version(as);
1268 dev_info(&as->pdev->dev, "version: 0x%x\n", version);
1269
1270 as->caps.is_spi2 = version > 0x121;
1271 as->caps.has_wdrbt = version >= 0x210;
1272 as->caps.has_dma_support = version >= 0x212;
1273}
1274
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001275/*-------------------------------------------------------------------------*/
1276
Grant Likelyfd4a3192012-12-07 16:57:14 +00001277static int atmel_spi_probe(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001278{
1279 struct resource *regs;
1280 int irq;
1281 struct clk *clk;
1282 int ret;
1283 struct spi_master *master;
1284 struct atmel_spi *as;
1285
Wenyou Yang5bdfd492014-03-05 09:58:49 +08001286 /* Select default pin state */
1287 pinctrl_pm_select_default_state(&pdev->dev);
1288
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001289 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1290 if (!regs)
1291 return -ENXIO;
1292
1293 irq = platform_get_irq(pdev, 0);
1294 if (irq < 0)
1295 return irq;
1296
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001297 clk = devm_clk_get(&pdev->dev, "spi_clk");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001298 if (IS_ERR(clk))
1299 return PTR_ERR(clk);
1300
1301 /* setup spi core then atmel-specific driver state */
1302 ret = -ENOMEM;
Sachin Kamata536d762013-09-10 17:06:27 +05301303 master = spi_alloc_master(&pdev->dev, sizeof(*as));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001304 if (!master)
1305 goto out_free;
1306
David Brownelle7db06b2009-06-17 16:26:04 -07001307 /* the spi->mode bits understood by this driver: */
1308 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001309 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16);
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001310 master->dev.of_node = pdev->dev.of_node;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001311 master->bus_num = pdev->id;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001312 master->num_chipselect = master->dev.of_node ? 0 : 4;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001313 master->setup = atmel_spi_setup;
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001314 master->transfer_one_message = atmel_spi_transfer_one_message;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001315 master->cleanup = atmel_spi_cleanup;
1316 platform_set_drvdata(pdev, master);
1317
1318 as = spi_master_get_devdata(master);
1319
David Brownell8da08592007-07-17 04:04:07 -07001320 /*
1321 * Scratch buffer is used for throwaway rx and tx data.
1322 * It's coherent to minimize dcache pollution.
1323 */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001324 as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
1325 &as->buffer_dma, GFP_KERNEL);
1326 if (!as->buffer)
1327 goto out_free;
1328
1329 spin_lock_init(&as->lock);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001330
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001331 as->pdev = pdev;
Mark Brown31407472013-10-16 13:22:35 +01001332 as->regs = devm_ioremap_resource(&pdev->dev, regs);
Wei Yongjun543c9542013-10-21 11:12:02 +08001333 if (IS_ERR(as->regs)) {
1334 ret = PTR_ERR(as->regs);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001335 goto out_free_buffer;
Wei Yongjun543c9542013-10-21 11:12:02 +08001336 }
Nicolas Ferredfab30e2013-04-03 13:57:42 +08001337 as->phybase = regs->start;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001338 as->irq = irq;
1339 as->clk = clk;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001340
Wenyou Yang8090d6d2014-01-09 13:19:15 +08001341 init_completion(&as->xfer_completion);
1342
Wenyou Yangd4820b72013-03-19 15:42:15 +08001343 atmel_get_caps(as);
1344
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001345 as->use_dma = false;
1346 as->use_pdc = false;
1347 if (as->caps.has_dma_support) {
1348 if (atmel_spi_configure_dma(as) == 0)
1349 as->use_dma = true;
1350 } else {
1351 as->use_pdc = true;
1352 }
1353
1354 if (as->caps.has_dma_support && !as->use_dma)
1355 dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n");
1356
1357 if (as->use_pdc) {
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001358 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt,
1359 0, dev_name(&pdev->dev), master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001360 } else {
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001361 ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt,
1362 0, dev_name(&pdev->dev), master);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001363 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001364 if (ret)
1365 goto out_unmap_regs;
1366
1367 /* Initialize the hardware */
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001368 ret = clk_prepare_enable(clk);
1369 if (ret)
Sachin Kamatde8cc232013-09-10 17:06:26 +05301370 goto out_free_irq;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001371 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001372 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Wenyou Yangd4820b72013-03-19 15:42:15 +08001373 if (as->caps.has_wdrbt) {
1374 spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
1375 | SPI_BIT(MSTR));
1376 } else {
1377 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
1378 }
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001379
1380 if (as->use_pdc)
1381 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001382 spi_writel(as, CR, SPI_BIT(SPIEN));
1383
1384 /* go! */
1385 dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
1386 (unsigned long)regs->start, irq);
1387
Jingoo Han9f87d6f2013-12-04 14:07:51 +09001388 ret = devm_spi_register_master(&pdev->dev, master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001389 if (ret)
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001390 goto out_free_dma;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001391
1392 return 0;
1393
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001394out_free_dma:
1395 if (as->use_dma)
1396 atmel_spi_release_dma(as);
1397
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001398 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001399 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001400 clk_disable_unprepare(clk);
Sachin Kamatde8cc232013-09-10 17:06:26 +05301401out_free_irq:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001402out_unmap_regs:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001403out_free_buffer:
1404 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
1405 as->buffer_dma);
1406out_free:
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001407 spi_master_put(master);
1408 return ret;
1409}
1410
Grant Likelyfd4a3192012-12-07 16:57:14 +00001411static int atmel_spi_remove(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001412{
1413 struct spi_master *master = platform_get_drvdata(pdev);
1414 struct atmel_spi *as = spi_master_get_devdata(master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001415
1416 /* reset the hardware and block queue progress */
1417 spin_lock_irq(&as->lock);
Nicolas Ferre1ccc4042013-04-03 13:59:19 +08001418 if (as->use_dma) {
1419 atmel_spi_stop_dma(as);
1420 atmel_spi_release_dma(as);
1421 }
1422
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001423 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001424 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001425 spi_readl(as, SR);
1426 spin_unlock_irq(&as->lock);
1427
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001428 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
1429 as->buffer_dma);
1430
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001431 clk_disable_unprepare(as->clk);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001432
1433 return 0;
1434}
1435
Jingoo Hanec60dd32013-09-09 17:54:12 +09001436#ifdef CONFIG_PM_SLEEP
1437static int atmel_spi_suspend(struct device *dev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001438{
Jingoo Hanec60dd32013-09-09 17:54:12 +09001439 struct spi_master *master = dev_get_drvdata(dev);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001440 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yangba938f32014-03-05 11:29:01 +08001441 int ret;
1442
1443 /* Stop the queue running */
1444 ret = spi_master_suspend(master);
1445 if (ret) {
1446 dev_warn(dev, "cannot suspend master\n");
1447 return ret;
1448 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001449
Boris BREZILLONdfec4a62013-07-16 17:16:22 +02001450 clk_disable_unprepare(as->clk);
Wenyou Yang5bdfd492014-03-05 09:58:49 +08001451
1452 pinctrl_pm_select_sleep_state(dev);
1453
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001454 return 0;
1455}
1456
Jingoo Hanec60dd32013-09-09 17:54:12 +09001457static int atmel_spi_resume(struct device *dev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001458{
Jingoo Hanec60dd32013-09-09 17:54:12 +09001459 struct spi_master *master = dev_get_drvdata(dev);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001460 struct atmel_spi *as = spi_master_get_devdata(master);
Wenyou Yangba938f32014-03-05 11:29:01 +08001461 int ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001462
Wenyou Yang5bdfd492014-03-05 09:58:49 +08001463 pinctrl_pm_select_default_state(dev);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001464
Jingoo Hanec60dd32013-09-09 17:54:12 +09001465 clk_prepare_enable(as->clk);
Wenyou Yangba938f32014-03-05 11:29:01 +08001466
1467 /* Start the queue running */
1468 ret = spi_master_resume(master);
1469 if (ret)
1470 dev_err(dev, "problem starting queue (%d)\n", ret);
1471
1472 return ret;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001473}
1474
Jingoo Hanec60dd32013-09-09 17:54:12 +09001475static SIMPLE_DEV_PM_OPS(atmel_spi_pm_ops, atmel_spi_suspend, atmel_spi_resume);
1476
1477#define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001478#else
Jingoo Hanec60dd32013-09-09 17:54:12 +09001479#define ATMEL_SPI_PM_OPS NULL
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001480#endif
1481
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001482#if defined(CONFIG_OF)
1483static const struct of_device_id atmel_spi_dt_ids[] = {
1484 { .compatible = "atmel,at91rm9200-spi" },
1485 { /* sentinel */ }
1486};
1487
1488MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
1489#endif
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001490
1491static struct platform_driver atmel_spi_driver = {
1492 .driver = {
1493 .name = "atmel_spi",
1494 .owner = THIS_MODULE,
Jingoo Hanec60dd32013-09-09 17:54:12 +09001495 .pm = ATMEL_SPI_PM_OPS,
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001496 .of_match_table = of_match_ptr(atmel_spi_dt_ids),
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001497 },
Jean-Christophe PLAGNIOL-VILLARD1cb201a2011-11-04 01:20:21 +08001498 .probe = atmel_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +00001499 .remove = atmel_spi_remove,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001500};
Grant Likely940ab882011-10-05 11:29:49 -06001501module_platform_driver(atmel_spi_driver);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001502
1503MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001504MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001505MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001506MODULE_ALIAS("platform:atmel_spi");