blob: ab2ed75d42f45f29214645a46d4f46bab8156b6b [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>
12#include <linux/init.h>
13#include <linux/clk.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/delay.h>
17#include <linux/dma-mapping.h>
18#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>
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>
David Brownellbb2d1c32007-02-20 13:58:19 -080027
Grant Likelyca632f52011-06-06 01:16:30 -060028/* SPI register offsets */
29#define SPI_CR 0x0000
30#define SPI_MR 0x0004
31#define SPI_RDR 0x0008
32#define SPI_TDR 0x000c
33#define SPI_SR 0x0010
34#define SPI_IER 0x0014
35#define SPI_IDR 0x0018
36#define SPI_IMR 0x001c
37#define SPI_CSR0 0x0030
38#define SPI_CSR1 0x0034
39#define SPI_CSR2 0x0038
40#define SPI_CSR3 0x003c
Wenyou Yangd4820b72013-03-19 15:42:15 +080041#define SPI_VERSION 0x00fc
Grant Likelyca632f52011-06-06 01:16:30 -060042#define SPI_RPR 0x0100
43#define SPI_RCR 0x0104
44#define SPI_TPR 0x0108
45#define SPI_TCR 0x010c
46#define SPI_RNPR 0x0110
47#define SPI_RNCR 0x0114
48#define SPI_TNPR 0x0118
49#define SPI_TNCR 0x011c
50#define SPI_PTCR 0x0120
51#define SPI_PTSR 0x0124
52
53/* Bitfields in CR */
54#define SPI_SPIEN_OFFSET 0
55#define SPI_SPIEN_SIZE 1
56#define SPI_SPIDIS_OFFSET 1
57#define SPI_SPIDIS_SIZE 1
58#define SPI_SWRST_OFFSET 7
59#define SPI_SWRST_SIZE 1
60#define SPI_LASTXFER_OFFSET 24
61#define SPI_LASTXFER_SIZE 1
62
63/* Bitfields in MR */
64#define SPI_MSTR_OFFSET 0
65#define SPI_MSTR_SIZE 1
66#define SPI_PS_OFFSET 1
67#define SPI_PS_SIZE 1
68#define SPI_PCSDEC_OFFSET 2
69#define SPI_PCSDEC_SIZE 1
70#define SPI_FDIV_OFFSET 3
71#define SPI_FDIV_SIZE 1
72#define SPI_MODFDIS_OFFSET 4
73#define SPI_MODFDIS_SIZE 1
Wenyou Yangd4820b72013-03-19 15:42:15 +080074#define SPI_WDRBT_OFFSET 5
75#define SPI_WDRBT_SIZE 1
Grant Likelyca632f52011-06-06 01:16:30 -060076#define SPI_LLB_OFFSET 7
77#define SPI_LLB_SIZE 1
78#define SPI_PCS_OFFSET 16
79#define SPI_PCS_SIZE 4
80#define SPI_DLYBCS_OFFSET 24
81#define SPI_DLYBCS_SIZE 8
82
83/* Bitfields in RDR */
84#define SPI_RD_OFFSET 0
85#define SPI_RD_SIZE 16
86
87/* Bitfields in TDR */
88#define SPI_TD_OFFSET 0
89#define SPI_TD_SIZE 16
90
91/* Bitfields in SR */
92#define SPI_RDRF_OFFSET 0
93#define SPI_RDRF_SIZE 1
94#define SPI_TDRE_OFFSET 1
95#define SPI_TDRE_SIZE 1
96#define SPI_MODF_OFFSET 2
97#define SPI_MODF_SIZE 1
98#define SPI_OVRES_OFFSET 3
99#define SPI_OVRES_SIZE 1
100#define SPI_ENDRX_OFFSET 4
101#define SPI_ENDRX_SIZE 1
102#define SPI_ENDTX_OFFSET 5
103#define SPI_ENDTX_SIZE 1
104#define SPI_RXBUFF_OFFSET 6
105#define SPI_RXBUFF_SIZE 1
106#define SPI_TXBUFE_OFFSET 7
107#define SPI_TXBUFE_SIZE 1
108#define SPI_NSSR_OFFSET 8
109#define SPI_NSSR_SIZE 1
110#define SPI_TXEMPTY_OFFSET 9
111#define SPI_TXEMPTY_SIZE 1
112#define SPI_SPIENS_OFFSET 16
113#define SPI_SPIENS_SIZE 1
114
115/* Bitfields in CSR0 */
116#define SPI_CPOL_OFFSET 0
117#define SPI_CPOL_SIZE 1
118#define SPI_NCPHA_OFFSET 1
119#define SPI_NCPHA_SIZE 1
120#define SPI_CSAAT_OFFSET 3
121#define SPI_CSAAT_SIZE 1
122#define SPI_BITS_OFFSET 4
123#define SPI_BITS_SIZE 4
124#define SPI_SCBR_OFFSET 8
125#define SPI_SCBR_SIZE 8
126#define SPI_DLYBS_OFFSET 16
127#define SPI_DLYBS_SIZE 8
128#define SPI_DLYBCT_OFFSET 24
129#define SPI_DLYBCT_SIZE 8
130
131/* Bitfields in RCR */
132#define SPI_RXCTR_OFFSET 0
133#define SPI_RXCTR_SIZE 16
134
135/* Bitfields in TCR */
136#define SPI_TXCTR_OFFSET 0
137#define SPI_TXCTR_SIZE 16
138
139/* Bitfields in RNCR */
140#define SPI_RXNCR_OFFSET 0
141#define SPI_RXNCR_SIZE 16
142
143/* Bitfields in TNCR */
144#define SPI_TXNCR_OFFSET 0
145#define SPI_TXNCR_SIZE 16
146
147/* Bitfields in PTCR */
148#define SPI_RXTEN_OFFSET 0
149#define SPI_RXTEN_SIZE 1
150#define SPI_RXTDIS_OFFSET 1
151#define SPI_RXTDIS_SIZE 1
152#define SPI_TXTEN_OFFSET 8
153#define SPI_TXTEN_SIZE 1
154#define SPI_TXTDIS_OFFSET 9
155#define SPI_TXTDIS_SIZE 1
156
157/* Constants for BITS */
158#define SPI_BITS_8_BPT 0
159#define SPI_BITS_9_BPT 1
160#define SPI_BITS_10_BPT 2
161#define SPI_BITS_11_BPT 3
162#define SPI_BITS_12_BPT 4
163#define SPI_BITS_13_BPT 5
164#define SPI_BITS_14_BPT 6
165#define SPI_BITS_15_BPT 7
166#define SPI_BITS_16_BPT 8
167
168/* Bit manipulation macros */
169#define SPI_BIT(name) \
170 (1 << SPI_##name##_OFFSET)
171#define SPI_BF(name,value) \
172 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
173#define SPI_BFEXT(name,value) \
174 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
175#define SPI_BFINS(name,value,old) \
176 ( ((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
177 | SPI_BF(name,value))
178
179/* Register access macros */
180#define spi_readl(port,reg) \
181 __raw_readl((port)->regs + SPI_##reg)
182#define spi_writel(port,reg,value) \
183 __raw_writel((value), (port)->regs + SPI_##reg)
184
Wenyou Yangd4820b72013-03-19 15:42:15 +0800185struct atmel_spi_caps {
186 bool is_spi2;
187 bool has_wdrbt;
188 bool has_dma_support;
189};
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800190
191/*
192 * The core SPI transfer engine just talks to a register bank to set up
193 * DMA transfers; transfer queue progress is driven by IRQs. The clock
194 * framework provides the base clock, subdivided for each spi_device.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800195 */
196struct atmel_spi {
197 spinlock_t lock;
198
199 void __iomem *regs;
200 int irq;
201 struct clk *clk;
202 struct platform_device *pdev;
David Brownelldefbd3b2007-07-17 04:04:08 -0700203 struct spi_device *stay;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800204
205 u8 stopping;
206 struct list_head queue;
207 struct spi_transfer *current_transfer;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800208 unsigned long current_remaining_bytes;
209 struct spi_transfer *next_transfer;
210 unsigned long next_remaining_bytes;
Nicolas Ferre823cd042013-03-19 15:45:01 +0800211 int done_status;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800212
213 void *buffer;
214 dma_addr_t buffer_dma;
Wenyou Yangd4820b72013-03-19 15:42:15 +0800215
216 struct atmel_spi_caps caps;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800217};
218
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800219/* Controller-specific per-slave state */
220struct atmel_spi_device {
221 unsigned int npcs_pin;
222 u32 csr;
223};
224
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800225#define BUFFER_SIZE PAGE_SIZE
226#define INVALID_DMA_ADDRESS 0xffffffff
227
228/*
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800229 * Version 2 of the SPI controller has
230 * - CR.LASTXFER
231 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
232 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
233 * - SPI_CSRx.CSAAT
234 * - SPI_CSRx.SBCR allows faster clocking
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800235 */
Wenyou Yangd4820b72013-03-19 15:42:15 +0800236static bool atmel_spi_is_v2(struct atmel_spi *as)
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800237{
Wenyou Yangd4820b72013-03-19 15:42:15 +0800238 return as->caps.is_spi2;
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800239}
240
241/*
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800242 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
243 * they assume that spi slave device state will not change on deselect, so
David Brownelldefbd3b2007-07-17 04:04:08 -0700244 * that automagic deselection is OK. ("NPCSx rises if no data is to be
245 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
246 * controllers have CSAAT and friends.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800247 *
David Brownelldefbd3b2007-07-17 04:04:08 -0700248 * Since the CSAAT functionality is a bit weird on newer controllers as
249 * well, we use GPIO to control nCSx pins on all controllers, updating
250 * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
251 * support active-high chipselects despite the controller's belief that
252 * only active-low devices/systems exists.
253 *
254 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
255 * right when driven with GPIO. ("Mode Fault does not allow more than one
256 * Master on Chip Select 0.") No workaround exists for that ... so for
257 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
258 * and (c) will trigger that first erratum in some cases.
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800259 */
260
David Brownelldefbd3b2007-07-17 04:04:08 -0700261static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800262{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800263 struct atmel_spi_device *asd = spi->controller_state;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800264 unsigned active = spi->mode & SPI_CS_HIGH;
David Brownelldefbd3b2007-07-17 04:04:08 -0700265 u32 mr;
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800266
Wenyou Yangd4820b72013-03-19 15:42:15 +0800267 if (atmel_spi_is_v2(as)) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800268 spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr);
269 /* For the low SPI version, there is a issue that PDC transfer
270 * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800271 */
272 spi_writel(as, CSR0, asd->csr);
Wenyou Yangd4820b72013-03-19 15:42:15 +0800273 if (as->caps.has_wdrbt) {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800274 spi_writel(as, MR,
275 SPI_BF(PCS, ~(0x01 << spi->chip_select))
276 | SPI_BIT(WDRBT)
277 | SPI_BIT(MODFDIS)
278 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800279 } else {
Wenyou Yang97ed4652013-03-19 15:43:01 +0800280 spi_writel(as, MR,
281 SPI_BF(PCS, ~(0x01 << spi->chip_select))
282 | SPI_BIT(MODFDIS)
283 | SPI_BIT(MSTR));
Wenyou Yangd4820b72013-03-19 15:42:15 +0800284 }
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800285 mr = spi_readl(as, MR);
286 gpio_set_value(asd->npcs_pin, active);
287 } else {
288 u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0;
289 int i;
290 u32 csr;
291
292 /* Make sure clock polarity is correct */
293 for (i = 0; i < spi->master->num_chipselect; i++) {
294 csr = spi_readl(as, CSR0 + 4 * i);
295 if ((csr ^ cpol) & SPI_BIT(CPOL))
296 spi_writel(as, CSR0 + 4 * i,
297 csr ^ SPI_BIT(CPOL));
298 }
299
300 mr = spi_readl(as, MR);
301 mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
302 if (spi->chip_select != 0)
303 gpio_set_value(asd->npcs_pin, active);
304 spi_writel(as, MR, mr);
Atsushi Nemotof6febcc2008-02-23 15:23:39 -0800305 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800306
David Brownelldefbd3b2007-07-17 04:04:08 -0700307 dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800308 asd->npcs_pin, active ? " (high)" : "",
David Brownelldefbd3b2007-07-17 04:04:08 -0700309 mr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800310}
311
David Brownelldefbd3b2007-07-17 04:04:08 -0700312static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800313{
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800314 struct atmel_spi_device *asd = spi->controller_state;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800315 unsigned active = spi->mode & SPI_CS_HIGH;
David Brownelldefbd3b2007-07-17 04:04:08 -0700316 u32 mr;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800317
David Brownelldefbd3b2007-07-17 04:04:08 -0700318 /* only deactivate *this* device; sometimes transfers to
319 * another device may be active when this routine is called.
320 */
321 mr = spi_readl(as, MR);
322 if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
323 mr = SPI_BFINS(PCS, 0xf, mr);
324 spi_writel(as, MR, mr);
325 }
326
327 dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800328 asd->npcs_pin, active ? " (low)" : "",
David Brownelldefbd3b2007-07-17 04:04:08 -0700329 mr);
330
Wenyou Yangd4820b72013-03-19 15:42:15 +0800331 if (atmel_spi_is_v2(as) || spi->chip_select != 0)
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800332 gpio_set_value(asd->npcs_pin, !active);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800333}
334
Silvester Erdeg154443c2008-02-06 01:38:12 -0800335static inline int atmel_spi_xfer_is_last(struct spi_message *msg,
336 struct spi_transfer *xfer)
337{
338 return msg->transfers.prev == &xfer->transfer_list;
339}
340
341static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer)
342{
343 return xfer->delay_usecs == 0 && !xfer->cs_change;
344}
345
346static void atmel_spi_next_xfer_data(struct spi_master *master,
347 struct spi_transfer *xfer,
348 dma_addr_t *tx_dma,
349 dma_addr_t *rx_dma,
350 u32 *plen)
351{
352 struct atmel_spi *as = spi_master_get_devdata(master);
353 u32 len = *plen;
354
355 /* use scratch buffer only when rx or tx data is unspecified */
356 if (xfer->rx_buf)
Ben Nizette6aed4ee2009-12-14 22:20:20 -0800357 *rx_dma = xfer->rx_dma + xfer->len - *plen;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800358 else {
359 *rx_dma = as->buffer_dma;
360 if (len > BUFFER_SIZE)
361 len = BUFFER_SIZE;
362 }
363 if (xfer->tx_buf)
Ben Nizette6aed4ee2009-12-14 22:20:20 -0800364 *tx_dma = xfer->tx_dma + xfer->len - *plen;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800365 else {
366 *tx_dma = as->buffer_dma;
367 if (len > BUFFER_SIZE)
368 len = BUFFER_SIZE;
369 memset(as->buffer, 0, len);
370 dma_sync_single_for_device(&as->pdev->dev,
371 as->buffer_dma, len, DMA_TO_DEVICE);
372 }
373
374 *plen = len;
375}
376
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800377/*
378 * Submit next transfer for DMA.
379 * lock is held, spi irq is blocked
380 */
381static void atmel_spi_next_xfer(struct spi_master *master,
382 struct spi_message *msg)
383{
384 struct atmel_spi *as = spi_master_get_devdata(master);
385 struct spi_transfer *xfer;
Gerard Kamdc329442008-08-04 13:41:12 -0700386 u32 len, remaining;
387 u32 ieval;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800388 dma_addr_t tx_dma, rx_dma;
389
Silvester Erdeg154443c2008-02-06 01:38:12 -0800390 if (!as->current_transfer)
391 xfer = list_entry(msg->transfers.next,
392 struct spi_transfer, transfer_list);
393 else if (!as->next_transfer)
394 xfer = list_entry(as->current_transfer->transfer_list.next,
395 struct spi_transfer, transfer_list);
396 else
397 xfer = NULL;
398
399 if (xfer) {
Gerard Kamdc329442008-08-04 13:41:12 -0700400 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
401
Silvester Erdeg154443c2008-02-06 01:38:12 -0800402 len = xfer->len;
403 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
404 remaining = xfer->len - len;
405
406 spi_writel(as, RPR, rx_dma);
407 spi_writel(as, TPR, tx_dma);
408
409 if (msg->spi->bits_per_word > 8)
410 len >>= 1;
411 spi_writel(as, RCR, len);
412 spi_writel(as, TCR, len);
Haavard Skinnemoen8bacb212008-02-06 01:38:13 -0800413
414 dev_dbg(&msg->spi->dev,
415 " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
416 xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
417 xfer->rx_buf, xfer->rx_dma);
Silvester Erdeg154443c2008-02-06 01:38:12 -0800418 } else {
419 xfer = as->next_transfer;
420 remaining = as->next_remaining_bytes;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800421 }
422
Silvester Erdeg154443c2008-02-06 01:38:12 -0800423 as->current_transfer = xfer;
424 as->current_remaining_bytes = remaining;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800425
Silvester Erdeg154443c2008-02-06 01:38:12 -0800426 if (remaining > 0)
427 len = remaining;
Haavard Skinnemoen8bacb212008-02-06 01:38:13 -0800428 else if (!atmel_spi_xfer_is_last(msg, xfer)
429 && atmel_spi_xfer_can_be_chained(xfer)) {
Silvester Erdeg154443c2008-02-06 01:38:12 -0800430 xfer = list_entry(xfer->transfer_list.next,
431 struct spi_transfer, transfer_list);
432 len = xfer->len;
433 } else
434 xfer = NULL;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800435
Silvester Erdeg154443c2008-02-06 01:38:12 -0800436 as->next_transfer = xfer;
437
438 if (xfer) {
Gerard Kamdc329442008-08-04 13:41:12 -0700439 u32 total;
440
Silvester Erdeg154443c2008-02-06 01:38:12 -0800441 total = len;
442 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
443 as->next_remaining_bytes = total - len;
444
445 spi_writel(as, RNPR, rx_dma);
446 spi_writel(as, TNPR, tx_dma);
447
448 if (msg->spi->bits_per_word > 8)
449 len >>= 1;
450 spi_writel(as, RNCR, len);
451 spi_writel(as, TNCR, len);
Haavard Skinnemoen8bacb212008-02-06 01:38:13 -0800452
453 dev_dbg(&msg->spi->dev,
454 " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
455 xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
456 xfer->rx_buf, xfer->rx_dma);
Gerard Kamdc329442008-08-04 13:41:12 -0700457 ieval = SPI_BIT(ENDRX) | SPI_BIT(OVRES);
Silvester Erdeg154443c2008-02-06 01:38:12 -0800458 } else {
459 spi_writel(as, RNCR, 0);
460 spi_writel(as, TNCR, 0);
Gerard Kamdc329442008-08-04 13:41:12 -0700461 ieval = SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) | SPI_BIT(OVRES);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800462 }
463
Silvester Erdeg154443c2008-02-06 01:38:12 -0800464 /* REVISIT: We're waiting for ENDRX before we start the next
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800465 * transfer because we need to handle some difficult timing
466 * issues otherwise. If we wait for ENDTX in one transfer and
467 * then starts waiting for ENDRX in the next, it's difficult
468 * to tell the difference between the ENDRX interrupt we're
469 * actually waiting for and the ENDRX interrupt of the
470 * previous transfer.
471 *
472 * It should be doable, though. Just not now...
473 */
Gerard Kamdc329442008-08-04 13:41:12 -0700474 spi_writel(as, IER, ieval);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800475 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
476}
477
478static void atmel_spi_next_message(struct spi_master *master)
479{
480 struct atmel_spi *as = spi_master_get_devdata(master);
481 struct spi_message *msg;
David Brownelldefbd3b2007-07-17 04:04:08 -0700482 struct spi_device *spi;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800483
484 BUG_ON(as->current_transfer);
485
486 msg = list_entry(as->queue.next, struct spi_message, queue);
David Brownelldefbd3b2007-07-17 04:04:08 -0700487 spi = msg->spi;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800488
Tony Jones49dce682007-10-16 01:27:48 -0700489 dev_dbg(master->dev.parent, "start message %p for %s\n",
Kay Sievers6c7377a2009-03-24 16:38:21 -0700490 msg, dev_name(&spi->dev));
David Brownelldefbd3b2007-07-17 04:04:08 -0700491
492 /* select chip if it's not still active */
493 if (as->stay) {
494 if (as->stay != spi) {
495 cs_deactivate(as, as->stay);
496 cs_activate(as, spi);
497 }
498 as->stay = NULL;
499 } else
500 cs_activate(as, spi);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800501
502 atmel_spi_next_xfer(master, msg);
503}
504
David Brownell8da08592007-07-17 04:04:07 -0700505/*
506 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
507 * - The buffer is either valid for CPU access, else NULL
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400508 * - If the buffer is valid, so is its DMA address
David Brownell8da08592007-07-17 04:04:07 -0700509 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400510 * This driver manages the dma address unless message->is_dma_mapped.
David Brownell8da08592007-07-17 04:04:07 -0700511 */
512static int
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800513atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
514{
David Brownell8da08592007-07-17 04:04:07 -0700515 struct device *dev = &as->pdev->dev;
516
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800517 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
David Brownell8da08592007-07-17 04:04:07 -0700518 if (xfer->tx_buf) {
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800519 /* tx_buf is a const void* where we need a void * for the dma
520 * mapping */
521 void *nonconst_tx = (void *)xfer->tx_buf;
522
David Brownell8da08592007-07-17 04:04:07 -0700523 xfer->tx_dma = dma_map_single(dev,
Jean-Christophe PLAGNIOL-VILLARD214b5742010-11-20 14:52:53 +0800524 nonconst_tx, xfer->len,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800525 DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700526 if (dma_mapping_error(dev, xfer->tx_dma))
David Brownell8da08592007-07-17 04:04:07 -0700527 return -ENOMEM;
528 }
529 if (xfer->rx_buf) {
530 xfer->rx_dma = dma_map_single(dev,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800531 xfer->rx_buf, xfer->len,
532 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700533 if (dma_mapping_error(dev, xfer->rx_dma)) {
David Brownell8da08592007-07-17 04:04:07 -0700534 if (xfer->tx_buf)
535 dma_unmap_single(dev,
536 xfer->tx_dma, xfer->len,
537 DMA_TO_DEVICE);
538 return -ENOMEM;
539 }
540 }
541 return 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800542}
543
544static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
545 struct spi_transfer *xfer)
546{
547 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700548 dma_unmap_single(master->dev.parent, xfer->tx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800549 xfer->len, DMA_TO_DEVICE);
550 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
Tony Jones49dce682007-10-16 01:27:48 -0700551 dma_unmap_single(master->dev.parent, xfer->rx_dma,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800552 xfer->len, DMA_FROM_DEVICE);
553}
554
555static void
556atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
Nicolas Ferre823cd042013-03-19 15:45:01 +0800557 struct spi_message *msg, int stay)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800558{
Nicolas Ferre823cd042013-03-19 15:45:01 +0800559 if (!stay || as->done_status < 0)
David Brownelldefbd3b2007-07-17 04:04:08 -0700560 cs_deactivate(as, msg->spi);
561 else
562 as->stay = msg->spi;
563
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800564 list_del(&msg->queue);
Nicolas Ferre823cd042013-03-19 15:45:01 +0800565 msg->status = as->done_status;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800566
Tony Jones49dce682007-10-16 01:27:48 -0700567 dev_dbg(master->dev.parent,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800568 "xfer complete: %u bytes transferred\n",
569 msg->actual_length);
570
571 spin_unlock(&as->lock);
572 msg->complete(msg->context);
573 spin_lock(&as->lock);
574
575 as->current_transfer = NULL;
Silvester Erdeg154443c2008-02-06 01:38:12 -0800576 as->next_transfer = NULL;
Nicolas Ferre823cd042013-03-19 15:45:01 +0800577 as->done_status = 0;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800578
579 /* continue if needed */
580 if (list_empty(&as->queue) || as->stopping)
581 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
582 else
583 atmel_spi_next_message(master);
584}
585
586static irqreturn_t
587atmel_spi_interrupt(int irq, void *dev_id)
588{
589 struct spi_master *master = dev_id;
590 struct atmel_spi *as = spi_master_get_devdata(master);
591 struct spi_message *msg;
592 struct spi_transfer *xfer;
593 u32 status, pending, imr;
594 int ret = IRQ_NONE;
595
596 spin_lock(&as->lock);
597
598 xfer = as->current_transfer;
599 msg = list_entry(as->queue.next, struct spi_message, queue);
600
601 imr = spi_readl(as, IMR);
602 status = spi_readl(as, SR);
603 pending = status & imr;
604
605 if (pending & SPI_BIT(OVRES)) {
606 int timeout;
607
608 ret = IRQ_HANDLED;
609
Gerard Kamdc329442008-08-04 13:41:12 -0700610 spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800611 | SPI_BIT(OVRES)));
612
613 /*
614 * When we get an overrun, we disregard the current
615 * transfer. Data will not be copied back from any
616 * bounce buffer and msg->actual_len will not be
617 * updated with the last xfer.
618 *
619 * We will also not process any remaning transfers in
620 * the message.
621 *
622 * First, stop the transfer and unmap the DMA buffers.
623 */
624 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
625 if (!msg->is_dma_mapped)
626 atmel_spi_dma_unmap_xfer(master, xfer);
627
628 /* REVISIT: udelay in irq is unfriendly */
629 if (xfer->delay_usecs)
630 udelay(xfer->delay_usecs);
631
Gerard Kamdc329442008-08-04 13:41:12 -0700632 dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n",
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800633 spi_readl(as, TCR), spi_readl(as, RCR));
634
635 /*
636 * Clean up DMA registers and make sure the data
637 * registers are empty.
638 */
639 spi_writel(as, RNCR, 0);
640 spi_writel(as, TNCR, 0);
641 spi_writel(as, RCR, 0);
642 spi_writel(as, TCR, 0);
643 for (timeout = 1000; timeout; timeout--)
644 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
645 break;
646 if (!timeout)
Tony Jones49dce682007-10-16 01:27:48 -0700647 dev_warn(master->dev.parent,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800648 "timeout waiting for TXEMPTY");
649 while (spi_readl(as, SR) & SPI_BIT(RDRF))
650 spi_readl(as, RDR);
651
652 /* Clear any overrun happening while cleaning up */
653 spi_readl(as, SR);
654
Nicolas Ferre823cd042013-03-19 15:45:01 +0800655 as->done_status = -EIO;
656 atmel_spi_msg_done(master, as, msg, 0);
Gerard Kamdc329442008-08-04 13:41:12 -0700657 } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800658 ret = IRQ_HANDLED;
659
660 spi_writel(as, IDR, pending);
661
Silvester Erdeg154443c2008-02-06 01:38:12 -0800662 if (as->current_remaining_bytes == 0) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800663 msg->actual_length += xfer->len;
664
665 if (!msg->is_dma_mapped)
666 atmel_spi_dma_unmap_xfer(master, xfer);
667
668 /* REVISIT: udelay in irq is unfriendly */
669 if (xfer->delay_usecs)
670 udelay(xfer->delay_usecs);
671
Silvester Erdeg154443c2008-02-06 01:38:12 -0800672 if (atmel_spi_xfer_is_last(msg, xfer)) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800673 /* report completed message */
Nicolas Ferre823cd042013-03-19 15:45:01 +0800674 atmel_spi_msg_done(master, as, msg,
David Brownelldefbd3b2007-07-17 04:04:08 -0700675 xfer->cs_change);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800676 } else {
677 if (xfer->cs_change) {
David Brownelldefbd3b2007-07-17 04:04:08 -0700678 cs_deactivate(as, msg->spi);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800679 udelay(1);
David Brownelldefbd3b2007-07-17 04:04:08 -0700680 cs_activate(as, msg->spi);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800681 }
682
683 /*
684 * Not done yet. Submit the next transfer.
685 *
686 * FIXME handle protocol options for xfer
687 */
688 atmel_spi_next_xfer(master, msg);
689 }
690 } else {
691 /*
692 * Keep going, we still have data to send in
693 * the current transfer.
694 */
695 atmel_spi_next_xfer(master, msg);
696 }
697 }
698
699 spin_unlock(&as->lock);
700
701 return ret;
702}
703
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800704static int atmel_spi_setup(struct spi_device *spi)
705{
706 struct atmel_spi *as;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800707 struct atmel_spi_device *asd;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800708 u32 scbr, csr;
709 unsigned int bits = spi->bits_per_word;
Haavard Skinnemoen592e7bf2008-04-30 00:52:17 -0700710 unsigned long bus_hz;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800711 unsigned int npcs_pin;
712 int ret;
713
714 as = spi_master_get_devdata(spi->master);
715
716 if (as->stopping)
717 return -ESHUTDOWN;
718
719 if (spi->chip_select > spi->master->num_chipselect) {
720 dev_dbg(&spi->dev,
721 "setup: invalid chipselect %u (%u defined)\n",
722 spi->chip_select, spi->master->num_chipselect);
723 return -EINVAL;
724 }
725
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800726 if (bits < 8 || bits > 16) {
727 dev_dbg(&spi->dev,
728 "setup: invalid bits_per_word %u (8 to 16)\n",
729 bits);
730 return -EINVAL;
731 }
732
David Brownelldefbd3b2007-07-17 04:04:08 -0700733 /* see notes above re chipselect */
Wenyou Yangd4820b72013-03-19 15:42:15 +0800734 if (!atmel_spi_is_v2(as)
David Brownelldefbd3b2007-07-17 04:04:08 -0700735 && spi->chip_select == 0
736 && (spi->mode & SPI_CS_HIGH)) {
737 dev_dbg(&spi->dev, "setup: can't be active-high\n");
738 return -EINVAL;
739 }
740
Haavard Skinnemoen5bfa26c2009-01-06 14:41:42 -0800741 /* v1 chips start out at half the peripheral bus speed. */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800742 bus_hz = clk_get_rate(as->clk);
Wenyou Yangd4820b72013-03-19 15:42:15 +0800743 if (!atmel_spi_is_v2(as))
Haavard Skinnemoen592e7bf2008-04-30 00:52:17 -0700744 bus_hz /= 2;
745
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800746 if (spi->max_speed_hz) {
Haavard Skinnemoen592e7bf2008-04-30 00:52:17 -0700747 /*
748 * Calculate the lowest divider that satisfies the
749 * constraint, assuming div32/fdiv/mbz == 0.
750 */
751 scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz);
752
753 /*
754 * If the resulting divider doesn't fit into the
755 * register bitfield, we can't satisfy the constraint.
756 */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800757 if (scbr >= (1 << SPI_SCBR_SIZE)) {
David Brownell8da08592007-07-17 04:04:07 -0700758 dev_dbg(&spi->dev,
759 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
760 spi->max_speed_hz, scbr, bus_hz/255);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800761 return -EINVAL;
762 }
763 } else
Haavard Skinnemoen592e7bf2008-04-30 00:52:17 -0700764 /* speed zero means "as slow as possible" */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800765 scbr = 0xff;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800766
767 csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
768 if (spi->mode & SPI_CPOL)
769 csr |= SPI_BIT(CPOL);
770 if (!(spi->mode & SPI_CPHA))
771 csr |= SPI_BIT(NCPHA);
772
Haavard Skinnemoen1eed29d2008-02-06 01:38:11 -0800773 /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
774 *
775 * DLYBCT would add delays between words, slowing down transfers.
776 * It could potentially be useful to cope with DMA bottlenecks, but
777 * in those cases it's probably best to just use a lower bitrate.
778 */
779 csr |= SPI_BF(DLYBS, 0);
780 csr |= SPI_BF(DLYBCT, 0);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800781
782 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
783 npcs_pin = (unsigned int)spi->controller_data;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +0100784
785 if (gpio_is_valid(spi->cs_gpio))
786 npcs_pin = spi->cs_gpio;
787
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800788 asd = spi->controller_state;
789 if (!asd) {
790 asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL);
791 if (!asd)
792 return -ENOMEM;
793
Kay Sievers6c7377a2009-03-24 16:38:21 -0700794 ret = gpio_request(npcs_pin, dev_name(&spi->dev));
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800795 if (ret) {
796 kfree(asd);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800797 return ret;
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800798 }
799
800 asd->npcs_pin = npcs_pin;
801 spi->controller_state = asd;
David Brownell28735a72007-03-16 13:38:14 -0800802 gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
David Brownelldefbd3b2007-07-17 04:04:08 -0700803 } else {
804 unsigned long flags;
805
806 spin_lock_irqsave(&as->lock, flags);
807 if (as->stay == spi)
808 as->stay = NULL;
809 cs_deactivate(as, spi);
810 spin_unlock_irqrestore(&as->lock, flags);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800811 }
812
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800813 asd->csr = csr;
814
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800815 dev_dbg(&spi->dev,
816 "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
Haavard Skinnemoen592e7bf2008-04-30 00:52:17 -0700817 bus_hz / scbr, bits, spi->mode, spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800818
Wenyou Yangd4820b72013-03-19 15:42:15 +0800819 if (!atmel_spi_is_v2(as))
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800820 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800821
822 return 0;
823}
824
825static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
826{
827 struct atmel_spi *as;
828 struct spi_transfer *xfer;
829 unsigned long flags;
Tony Jones49dce682007-10-16 01:27:48 -0700830 struct device *controller = spi->master->dev.parent;
Matthias Bruggerb9d228f2010-10-13 17:51:02 +0200831 u8 bits;
832 struct atmel_spi_device *asd;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800833
834 as = spi_master_get_devdata(spi->master);
835
836 dev_dbg(controller, "new message %p submitted for %s\n",
Kay Sievers6c7377a2009-03-24 16:38:21 -0700837 msg, dev_name(&spi->dev));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800838
Stanislaw Gruszka5b96f172009-01-15 13:50:44 -0800839 if (unlikely(list_empty(&msg->transfers)))
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800840 return -EINVAL;
841
842 if (as->stopping)
843 return -ESHUTDOWN;
844
845 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Atsushi Nemoto06719812008-04-28 02:14:19 -0700846 if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800847 dev_dbg(&spi->dev, "missing rx or tx buf\n");
848 return -EINVAL;
849 }
850
Matthias Bruggerb9d228f2010-10-13 17:51:02 +0200851 if (xfer->bits_per_word) {
852 asd = spi->controller_state;
853 bits = (asd->csr >> 4) & 0xf;
854 if (bits != xfer->bits_per_word - 8) {
855 dev_dbg(&spi->dev, "you can't yet change "
Matthias Bruggeree2007d2010-10-16 01:39:49 +0200856 "bits_per_word in transfers\n");
Matthias Bruggerb9d228f2010-10-13 17:51:02 +0200857 return -ENOPROTOOPT;
858 }
859 }
860
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800861 /* FIXME implement these protocol options!! */
Matthias Bruggerb9d228f2010-10-13 17:51:02 +0200862 if (xfer->speed_hz) {
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800863 dev_dbg(&spi->dev, "no protocol options yet\n");
864 return -ENOPROTOOPT;
865 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800866
David Brownell8da08592007-07-17 04:04:07 -0700867 /*
868 * DMA map early, for performance (empties dcache ASAP) and
869 * better fault reporting. This is a DMA-only driver.
870 *
871 * NOTE that if dma_unmap_single() ever starts to do work on
872 * platforms supported by this driver, we would need to clean
873 * up mappings for previously-mapped transfers.
874 */
875 if (!msg->is_dma_mapped) {
876 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
877 return -ENOMEM;
878 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800879 }
880
David Brownelldefbd3b2007-07-17 04:04:08 -0700881#ifdef VERBOSE
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800882 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
883 dev_dbg(controller,
884 " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
885 xfer, xfer->len,
886 xfer->tx_buf, xfer->tx_dma,
887 xfer->rx_buf, xfer->rx_dma);
888 }
David Brownelldefbd3b2007-07-17 04:04:08 -0700889#endif
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800890
891 msg->status = -EINPROGRESS;
892 msg->actual_length = 0;
893
894 spin_lock_irqsave(&as->lock, flags);
895 list_add_tail(&msg->queue, &as->queue);
896 if (!as->current_transfer)
897 atmel_spi_next_message(spi->master);
898 spin_unlock_irqrestore(&as->lock, flags);
899
900 return 0;
901}
902
David Brownellbb2d1c32007-02-20 13:58:19 -0800903static void atmel_spi_cleanup(struct spi_device *spi)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800904{
David Brownelldefbd3b2007-07-17 04:04:08 -0700905 struct atmel_spi *as = spi_master_get_devdata(spi->master);
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800906 struct atmel_spi_device *asd = spi->controller_state;
David Brownelldefbd3b2007-07-17 04:04:08 -0700907 unsigned gpio = (unsigned) spi->controller_data;
908 unsigned long flags;
909
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800910 if (!asd)
David Brownelldefbd3b2007-07-17 04:04:08 -0700911 return;
912
913 spin_lock_irqsave(&as->lock, flags);
914 if (as->stay == spi) {
915 as->stay = NULL;
916 cs_deactivate(as, spi);
917 }
918 spin_unlock_irqrestore(&as->lock, flags);
919
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800920 spi->controller_state = NULL;
David Brownelldefbd3b2007-07-17 04:04:08 -0700921 gpio_free(gpio);
Haavard Skinnemoen5ee36c92009-01-06 14:41:43 -0800922 kfree(asd);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800923}
924
Wenyou Yangd4820b72013-03-19 15:42:15 +0800925static inline unsigned int atmel_get_version(struct atmel_spi *as)
926{
927 return spi_readl(as, VERSION) & 0x00000fff;
928}
929
930static void atmel_get_caps(struct atmel_spi *as)
931{
932 unsigned int version;
933
934 version = atmel_get_version(as);
935 dev_info(&as->pdev->dev, "version: 0x%x\n", version);
936
937 as->caps.is_spi2 = version > 0x121;
938 as->caps.has_wdrbt = version >= 0x210;
939 as->caps.has_dma_support = version >= 0x212;
940}
941
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800942/*-------------------------------------------------------------------------*/
943
Grant Likelyfd4a3192012-12-07 16:57:14 +0000944static int atmel_spi_probe(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800945{
946 struct resource *regs;
947 int irq;
948 struct clk *clk;
949 int ret;
950 struct spi_master *master;
951 struct atmel_spi *as;
952
953 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
954 if (!regs)
955 return -ENXIO;
956
957 irq = platform_get_irq(pdev, 0);
958 if (irq < 0)
959 return irq;
960
961 clk = clk_get(&pdev->dev, "spi_clk");
962 if (IS_ERR(clk))
963 return PTR_ERR(clk);
964
965 /* setup spi core then atmel-specific driver state */
966 ret = -ENOMEM;
967 master = spi_alloc_master(&pdev->dev, sizeof *as);
968 if (!master)
969 goto out_free;
970
David Brownelle7db06b2009-06-17 16:26:04 -0700971 /* the spi->mode bits understood by this driver: */
972 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
973
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +0100974 master->dev.of_node = pdev->dev.of_node;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800975 master->bus_num = pdev->id;
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +0100976 master->num_chipselect = master->dev.of_node ? 0 : 4;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800977 master->setup = atmel_spi_setup;
978 master->transfer = atmel_spi_transfer;
979 master->cleanup = atmel_spi_cleanup;
980 platform_set_drvdata(pdev, master);
981
982 as = spi_master_get_devdata(master);
983
David Brownell8da08592007-07-17 04:04:07 -0700984 /*
985 * Scratch buffer is used for throwaway rx and tx data.
986 * It's coherent to minimize dcache pollution.
987 */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800988 as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
989 &as->buffer_dma, GFP_KERNEL);
990 if (!as->buffer)
991 goto out_free;
992
993 spin_lock_init(&as->lock);
994 INIT_LIST_HEAD(&as->queue);
995 as->pdev = pdev;
hartleys905aa0a2009-12-14 22:22:25 +0000996 as->regs = ioremap(regs->start, resource_size(regs));
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -0800997 if (!as->regs)
998 goto out_free_buffer;
999 as->irq = irq;
1000 as->clk = clk;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001001
Wenyou Yangd4820b72013-03-19 15:42:15 +08001002 atmel_get_caps(as);
1003
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001004 ret = request_irq(irq, atmel_spi_interrupt, 0,
Kay Sievers6c7377a2009-03-24 16:38:21 -07001005 dev_name(&pdev->dev), master);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001006 if (ret)
1007 goto out_unmap_regs;
1008
1009 /* Initialize the hardware */
1010 clk_enable(clk);
1011 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001012 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Wenyou Yangd4820b72013-03-19 15:42:15 +08001013 if (as->caps.has_wdrbt) {
1014 spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS)
1015 | SPI_BIT(MSTR));
1016 } else {
1017 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
1018 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001019 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
1020 spi_writel(as, CR, SPI_BIT(SPIEN));
1021
1022 /* go! */
1023 dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
1024 (unsigned long)regs->start, irq);
1025
1026 ret = spi_register_master(master);
1027 if (ret)
1028 goto out_reset_hw;
1029
1030 return 0;
1031
1032out_reset_hw:
1033 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001034 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001035 clk_disable(clk);
1036 free_irq(irq, master);
1037out_unmap_regs:
1038 iounmap(as->regs);
1039out_free_buffer:
1040 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
1041 as->buffer_dma);
1042out_free:
1043 clk_put(clk);
1044 spi_master_put(master);
1045 return ret;
1046}
1047
Grant Likelyfd4a3192012-12-07 16:57:14 +00001048static int atmel_spi_remove(struct platform_device *pdev)
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001049{
1050 struct spi_master *master = platform_get_drvdata(pdev);
1051 struct atmel_spi *as = spi_master_get_devdata(master);
1052 struct spi_message *msg;
Nicolas Ferre1888e8f2013-03-19 15:44:22 +08001053 struct spi_transfer *xfer;
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001054
1055 /* reset the hardware and block queue progress */
1056 spin_lock_irq(&as->lock);
1057 as->stopping = 1;
1058 spi_writel(as, CR, SPI_BIT(SWRST));
Jean-Christophe Lallemand50d7d5b2008-11-12 13:27:00 -08001059 spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001060 spi_readl(as, SR);
1061 spin_unlock_irq(&as->lock);
1062
1063 /* Terminate remaining queued transfers */
1064 list_for_each_entry(msg, &as->queue, queue) {
Nicolas Ferre1888e8f2013-03-19 15:44:22 +08001065 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
1066 if (!msg->is_dma_mapped)
1067 atmel_spi_dma_unmap_xfer(master, xfer);
1068 }
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001069 msg->status = -ESHUTDOWN;
1070 msg->complete(msg->context);
1071 }
1072
1073 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
1074 as->buffer_dma);
1075
1076 clk_disable(as->clk);
1077 clk_put(as->clk);
1078 free_irq(as->irq, master);
1079 iounmap(as->regs);
1080
1081 spi_unregister_master(master);
1082
1083 return 0;
1084}
1085
1086#ifdef CONFIG_PM
1087
1088static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
1089{
1090 struct spi_master *master = platform_get_drvdata(pdev);
1091 struct atmel_spi *as = spi_master_get_devdata(master);
1092
1093 clk_disable(as->clk);
1094 return 0;
1095}
1096
1097static int atmel_spi_resume(struct platform_device *pdev)
1098{
1099 struct spi_master *master = platform_get_drvdata(pdev);
1100 struct atmel_spi *as = spi_master_get_devdata(master);
1101
1102 clk_enable(as->clk);
1103 return 0;
1104}
1105
1106#else
1107#define atmel_spi_suspend NULL
1108#define atmel_spi_resume NULL
1109#endif
1110
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001111#if defined(CONFIG_OF)
1112static const struct of_device_id atmel_spi_dt_ids[] = {
1113 { .compatible = "atmel,at91rm9200-spi" },
1114 { /* sentinel */ }
1115};
1116
1117MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids);
1118#endif
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001119
1120static struct platform_driver atmel_spi_driver = {
1121 .driver = {
1122 .name = "atmel_spi",
1123 .owner = THIS_MODULE,
Jean-Christophe PLAGNIOL-VILLARD850a5b62012-11-23 13:44:39 +01001124 .of_match_table = of_match_ptr(atmel_spi_dt_ids),
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001125 },
1126 .suspend = atmel_spi_suspend,
1127 .resume = atmel_spi_resume,
Jean-Christophe PLAGNIOL-VILLARD1cb201a2011-11-04 01:20:21 +08001128 .probe = atmel_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +00001129 .remove = atmel_spi_remove,
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001130};
Grant Likely940ab882011-10-05 11:29:49 -06001131module_platform_driver(atmel_spi_driver);
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001132
1133MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
Jean Delvaree05503e2011-05-18 16:49:24 +02001134MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
Haavard Skinnemoen754ce4f2007-02-14 00:33:09 -08001135MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001136MODULE_ALIAS("platform:atmel_spi");