2 * Driver for Atmel AT32 and AT91 SPI Controllers
4 * Copyright (C) 2006 Atmel Corporation
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.
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/dmaengine.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
21 #include <linux/spi/spi.h>
22 #include <linux/slab.h>
23 #include <linux/platform_data/atmel.h>
24 #include <linux/platform_data/dma-atmel.h>
28 #include <linux/gpio.h>
30 /* SPI register offsets */
33 #define SPI_RDR 0x0008
34 #define SPI_TDR 0x000c
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
43 #define SPI_VERSION 0x00fc
44 #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
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
66 #define SPI_MSTR_OFFSET 0
67 #define SPI_MSTR_SIZE 1
68 #define SPI_PS_OFFSET 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
76 #define SPI_WDRBT_OFFSET 5
77 #define SPI_WDRBT_SIZE 1
78 #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
85 /* Bitfields in RDR */
86 #define SPI_RD_OFFSET 0
87 #define SPI_RD_SIZE 16
89 /* Bitfields in TDR */
90 #define SPI_TD_OFFSET 0
91 #define SPI_TD_SIZE 16
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
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
133 /* Bitfields in RCR */
134 #define SPI_RXCTR_OFFSET 0
135 #define SPI_RXCTR_SIZE 16
137 /* Bitfields in TCR */
138 #define SPI_TXCTR_OFFSET 0
139 #define SPI_TXCTR_SIZE 16
141 /* Bitfields in RNCR */
142 #define SPI_RXNCR_OFFSET 0
143 #define SPI_RXNCR_SIZE 16
145 /* Bitfields in TNCR */
146 #define SPI_TXNCR_OFFSET 0
147 #define SPI_TXNCR_SIZE 16
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
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
170 /* Bit manipulation macros */
171 #define SPI_BIT(name) \
172 (1 << SPI_##name##_OFFSET)
173 #define SPI_BF(name,value) \
174 (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET)
175 #define SPI_BFEXT(name,value) \
176 (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1))
177 #define SPI_BFINS(name,value,old) \
178 ( ((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \
179 | SPI_BF(name,value))
181 /* Register access macros */
182 #define spi_readl(port,reg) \
183 __raw_readl((port)->regs + SPI_##reg)
184 #define spi_writel(port,reg,value) \
185 __raw_writel((value), (port)->regs + SPI_##reg)
187 /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
188 * cache operations; better heuristics consider wordsize and bitrate.
190 #define DMA_MIN_BYTES 16
192 struct atmel_spi_dma
{
193 struct dma_chan
*chan_rx
;
194 struct dma_chan
*chan_tx
;
195 struct scatterlist sgrx
;
196 struct scatterlist sgtx
;
197 struct dma_async_tx_descriptor
*data_desc_rx
;
198 struct dma_async_tx_descriptor
*data_desc_tx
;
200 struct at_dma_slave dma_slave
;
203 struct atmel_spi_caps
{
206 bool has_dma_support
;
210 * The core SPI transfer engine just talks to a register bank to set up
211 * DMA transfers; transfer queue progress is driven by IRQs. The clock
212 * framework provides the base clock, subdivided for each spi_device.
222 struct platform_device
*pdev
;
223 struct spi_device
*stay
;
226 struct list_head queue
;
227 struct tasklet_struct tasklet
;
228 struct spi_transfer
*current_transfer
;
229 unsigned long current_remaining_bytes
;
230 struct spi_transfer
*next_transfer
;
231 unsigned long next_remaining_bytes
;
236 dma_addr_t buffer_dma
;
238 struct atmel_spi_caps caps
;
243 struct atmel_spi_dma dma
;
246 /* Controller-specific per-slave state */
247 struct atmel_spi_device
{
248 unsigned int npcs_pin
;
252 #define BUFFER_SIZE PAGE_SIZE
253 #define INVALID_DMA_ADDRESS 0xffffffff
256 * Version 2 of the SPI controller has
258 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
259 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
261 * - SPI_CSRx.SBCR allows faster clocking
263 static bool atmel_spi_is_v2(struct atmel_spi
*as
)
265 return as
->caps
.is_spi2
;
269 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
270 * they assume that spi slave device state will not change on deselect, so
271 * that automagic deselection is OK. ("NPCSx rises if no data is to be
272 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
273 * controllers have CSAAT and friends.
275 * Since the CSAAT functionality is a bit weird on newer controllers as
276 * well, we use GPIO to control nCSx pins on all controllers, updating
277 * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
278 * support active-high chipselects despite the controller's belief that
279 * only active-low devices/systems exists.
281 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
282 * right when driven with GPIO. ("Mode Fault does not allow more than one
283 * Master on Chip Select 0.") No workaround exists for that ... so for
284 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
285 * and (c) will trigger that first erratum in some cases.
288 static void cs_activate(struct atmel_spi
*as
, struct spi_device
*spi
)
290 struct atmel_spi_device
*asd
= spi
->controller_state
;
291 unsigned active
= spi
->mode
& SPI_CS_HIGH
;
294 if (atmel_spi_is_v2(as
)) {
295 spi_writel(as
, CSR0
+ 4 * spi
->chip_select
, asd
->csr
);
296 /* For the low SPI version, there is a issue that PDC transfer
297 * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS
299 spi_writel(as
, CSR0
, asd
->csr
);
300 if (as
->caps
.has_wdrbt
) {
302 SPI_BF(PCS
, ~(0x01 << spi
->chip_select
))
308 SPI_BF(PCS
, ~(0x01 << spi
->chip_select
))
313 mr
= spi_readl(as
, MR
);
314 gpio_set_value(asd
->npcs_pin
, active
);
316 u32 cpol
= (spi
->mode
& SPI_CPOL
) ? SPI_BIT(CPOL
) : 0;
320 /* Make sure clock polarity is correct */
321 for (i
= 0; i
< spi
->master
->num_chipselect
; i
++) {
322 csr
= spi_readl(as
, CSR0
+ 4 * i
);
323 if ((csr
^ cpol
) & SPI_BIT(CPOL
))
324 spi_writel(as
, CSR0
+ 4 * i
,
325 csr
^ SPI_BIT(CPOL
));
328 mr
= spi_readl(as
, MR
);
329 mr
= SPI_BFINS(PCS
, ~(1 << spi
->chip_select
), mr
);
330 if (spi
->chip_select
!= 0)
331 gpio_set_value(asd
->npcs_pin
, active
);
332 spi_writel(as
, MR
, mr
);
335 dev_dbg(&spi
->dev
, "activate %u%s, mr %08x\n",
336 asd
->npcs_pin
, active
? " (high)" : "",
340 static void cs_deactivate(struct atmel_spi
*as
, struct spi_device
*spi
)
342 struct atmel_spi_device
*asd
= spi
->controller_state
;
343 unsigned active
= spi
->mode
& SPI_CS_HIGH
;
346 /* only deactivate *this* device; sometimes transfers to
347 * another device may be active when this routine is called.
349 mr
= spi_readl(as
, MR
);
350 if (~SPI_BFEXT(PCS
, mr
) & (1 << spi
->chip_select
)) {
351 mr
= SPI_BFINS(PCS
, 0xf, mr
);
352 spi_writel(as
, MR
, mr
);
355 dev_dbg(&spi
->dev
, "DEactivate %u%s, mr %08x\n",
356 asd
->npcs_pin
, active
? " (low)" : "",
359 if (atmel_spi_is_v2(as
) || spi
->chip_select
!= 0)
360 gpio_set_value(asd
->npcs_pin
, !active
);
363 static void atmel_spi_lock(struct atmel_spi
*as
) __acquires(&as
->lock
)
365 spin_lock_irqsave(&as
->lock
, as
->flags
);
368 static void atmel_spi_unlock(struct atmel_spi
*as
) __releases(&as
->lock
)
370 spin_unlock_irqrestore(&as
->lock
, as
->flags
);
373 static inline bool atmel_spi_use_dma(struct atmel_spi
*as
,
374 struct spi_transfer
*xfer
)
376 return as
->use_dma
&& xfer
->len
>= DMA_MIN_BYTES
;
379 static inline int atmel_spi_xfer_is_last(struct spi_message
*msg
,
380 struct spi_transfer
*xfer
)
382 return msg
->transfers
.prev
== &xfer
->transfer_list
;
385 static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer
*xfer
)
387 return xfer
->delay_usecs
== 0 && !xfer
->cs_change
;
390 static int atmel_spi_dma_slave_config(struct atmel_spi
*as
,
391 struct dma_slave_config
*slave_config
,
396 if (bits_per_word
> 8) {
397 slave_config
->dst_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
398 slave_config
->src_addr_width
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
400 slave_config
->dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
401 slave_config
->src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
404 slave_config
->dst_addr
= (dma_addr_t
)as
->phybase
+ SPI_TDR
;
405 slave_config
->src_addr
= (dma_addr_t
)as
->phybase
+ SPI_RDR
;
406 slave_config
->src_maxburst
= 1;
407 slave_config
->dst_maxburst
= 1;
408 slave_config
->device_fc
= false;
410 slave_config
->direction
= DMA_MEM_TO_DEV
;
411 if (dmaengine_slave_config(as
->dma
.chan_tx
, slave_config
)) {
412 dev_err(&as
->pdev
->dev
,
413 "failed to configure tx dma channel\n");
417 slave_config
->direction
= DMA_DEV_TO_MEM
;
418 if (dmaengine_slave_config(as
->dma
.chan_rx
, slave_config
)) {
419 dev_err(&as
->pdev
->dev
,
420 "failed to configure rx dma channel\n");
427 static bool filter(struct dma_chan
*chan
, void *pdata
)
429 struct atmel_spi_dma
*sl_pdata
= pdata
;
430 struct at_dma_slave
*sl
;
435 sl
= &sl_pdata
->dma_slave
;
436 if (sl
->dma_dev
== chan
->device
->dev
) {
444 static int atmel_spi_configure_dma(struct atmel_spi
*as
)
446 struct dma_slave_config slave_config
;
447 struct device
*dev
= &as
->pdev
->dev
;
452 dma_cap_set(DMA_SLAVE
, mask
);
454 as
->dma
.chan_tx
= dma_request_slave_channel_compat(mask
, filter
,
457 if (!as
->dma
.chan_tx
) {
459 "DMA TX channel not available, SPI unable to use DMA\n");
464 as
->dma
.chan_rx
= dma_request_slave_channel_compat(mask
, filter
,
468 if (!as
->dma
.chan_rx
) {
470 "DMA RX channel not available, SPI unable to use DMA\n");
475 err
= atmel_spi_dma_slave_config(as
, &slave_config
, 8);
479 dev_info(&as
->pdev
->dev
,
480 "Using %s (tx) and %s (rx) for DMA transfers\n",
481 dma_chan_name(as
->dma
.chan_tx
),
482 dma_chan_name(as
->dma
.chan_rx
));
486 dma_release_channel(as
->dma
.chan_rx
);
488 dma_release_channel(as
->dma
.chan_tx
);
492 static void atmel_spi_stop_dma(struct atmel_spi
*as
)
495 as
->dma
.chan_rx
->device
->device_control(as
->dma
.chan_rx
,
496 DMA_TERMINATE_ALL
, 0);
498 as
->dma
.chan_tx
->device
->device_control(as
->dma
.chan_tx
,
499 DMA_TERMINATE_ALL
, 0);
502 static void atmel_spi_release_dma(struct atmel_spi
*as
)
505 dma_release_channel(as
->dma
.chan_rx
);
507 dma_release_channel(as
->dma
.chan_tx
);
510 /* This function is called by the DMA driver from tasklet context */
511 static void dma_callback(void *data
)
513 struct spi_master
*master
= data
;
514 struct atmel_spi
*as
= spi_master_get_devdata(master
);
516 /* trigger SPI tasklet */
517 tasklet_schedule(&as
->tasklet
);
521 * Next transfer using PIO.
522 * lock is held, spi tasklet is blocked
524 static void atmel_spi_next_xfer_pio(struct spi_master
*master
,
525 struct spi_transfer
*xfer
)
527 struct atmel_spi
*as
= spi_master_get_devdata(master
);
529 dev_vdbg(master
->dev
.parent
, "atmel_spi_next_xfer_pio\n");
531 as
->current_remaining_bytes
= xfer
->len
;
533 /* Make sure data is not remaining in RDR */
535 while (spi_readl(as
, SR
) & SPI_BIT(RDRF
)) {
541 if (xfer
->bits_per_word
> 8)
542 spi_writel(as
, TDR
, *(u16
*)(xfer
->tx_buf
));
544 spi_writel(as
, TDR
, *(u8
*)(xfer
->tx_buf
));
546 spi_writel(as
, TDR
, 0);
548 dev_dbg(master
->dev
.parent
,
549 " start pio xfer %p: len %u tx %p rx %p bitpw %d\n",
550 xfer
, xfer
->len
, xfer
->tx_buf
, xfer
->rx_buf
,
551 xfer
->bits_per_word
);
553 /* Enable relevant interrupts */
554 spi_writel(as
, IER
, SPI_BIT(RDRF
) | SPI_BIT(OVRES
));
558 * Submit next transfer for DMA.
559 * lock is held, spi tasklet is blocked
561 static int atmel_spi_next_xfer_dma_submit(struct spi_master
*master
,
562 struct spi_transfer
*xfer
,
565 struct atmel_spi
*as
= spi_master_get_devdata(master
);
566 struct dma_chan
*rxchan
= as
->dma
.chan_rx
;
567 struct dma_chan
*txchan
= as
->dma
.chan_tx
;
568 struct dma_async_tx_descriptor
*rxdesc
;
569 struct dma_async_tx_descriptor
*txdesc
;
570 struct dma_slave_config slave_config
;
574 dev_vdbg(master
->dev
.parent
, "atmel_spi_next_xfer_dma_submit\n");
576 /* Check that the channels are available */
577 if (!rxchan
|| !txchan
)
580 /* release lock for DMA operations */
581 atmel_spi_unlock(as
);
583 /* prepare the RX dma transfer */
584 sg_init_table(&as
->dma
.sgrx
, 1);
586 as
->dma
.sgrx
.dma_address
= xfer
->rx_dma
+ xfer
->len
- *plen
;
588 as
->dma
.sgrx
.dma_address
= as
->buffer_dma
;
589 if (len
> BUFFER_SIZE
)
593 /* prepare the TX dma transfer */
594 sg_init_table(&as
->dma
.sgtx
, 1);
596 as
->dma
.sgtx
.dma_address
= xfer
->tx_dma
+ xfer
->len
- *plen
;
598 as
->dma
.sgtx
.dma_address
= as
->buffer_dma
;
599 if (len
> BUFFER_SIZE
)
601 memset(as
->buffer
, 0, len
);
604 sg_dma_len(&as
->dma
.sgtx
) = len
;
605 sg_dma_len(&as
->dma
.sgrx
) = len
;
609 if (atmel_spi_dma_slave_config(as
, &slave_config
,
610 xfer
->bits_per_word
))
613 /* Send both scatterlists */
614 rxdesc
= rxchan
->device
->device_prep_slave_sg(rxchan
,
618 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
,
623 txdesc
= txchan
->device
->device_prep_slave_sg(txchan
,
627 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
,
632 dev_dbg(master
->dev
.parent
,
633 " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
634 xfer
, xfer
->len
, xfer
->tx_buf
, (unsigned long long)xfer
->tx_dma
,
635 xfer
->rx_buf
, (unsigned long long)xfer
->rx_dma
);
637 /* Enable relevant interrupts */
638 spi_writel(as
, IER
, SPI_BIT(OVRES
));
640 /* Put the callback on the RX transfer only, that should finish last */
641 rxdesc
->callback
= dma_callback
;
642 rxdesc
->callback_param
= master
;
644 /* Submit and fire RX and TX with TX last so we're ready to read! */
645 cookie
= rxdesc
->tx_submit(rxdesc
);
646 if (dma_submit_error(cookie
))
648 cookie
= txdesc
->tx_submit(txdesc
);
649 if (dma_submit_error(cookie
))
651 rxchan
->device
->device_issue_pending(rxchan
);
652 txchan
->device
->device_issue_pending(txchan
);
659 spi_writel(as
, IDR
, SPI_BIT(OVRES
));
660 atmel_spi_stop_dma(as
);
666 static void atmel_spi_next_xfer_data(struct spi_master
*master
,
667 struct spi_transfer
*xfer
,
672 struct atmel_spi
*as
= spi_master_get_devdata(master
);
675 /* use scratch buffer only when rx or tx data is unspecified */
677 *rx_dma
= xfer
->rx_dma
+ xfer
->len
- *plen
;
679 *rx_dma
= as
->buffer_dma
;
680 if (len
> BUFFER_SIZE
)
685 *tx_dma
= xfer
->tx_dma
+ xfer
->len
- *plen
;
687 *tx_dma
= as
->buffer_dma
;
688 if (len
> BUFFER_SIZE
)
690 memset(as
->buffer
, 0, len
);
691 dma_sync_single_for_device(&as
->pdev
->dev
,
692 as
->buffer_dma
, len
, DMA_TO_DEVICE
);
699 * Submit next transfer for PDC.
700 * lock is held, spi irq is blocked
702 static void atmel_spi_pdc_next_xfer(struct spi_master
*master
,
703 struct spi_message
*msg
)
705 struct atmel_spi
*as
= spi_master_get_devdata(master
);
706 struct spi_transfer
*xfer
;
709 dma_addr_t tx_dma
, rx_dma
;
711 if (!as
->current_transfer
)
712 xfer
= list_entry(msg
->transfers
.next
,
713 struct spi_transfer
, transfer_list
);
714 else if (!as
->next_transfer
)
715 xfer
= list_entry(as
->current_transfer
->transfer_list
.next
,
716 struct spi_transfer
, transfer_list
);
721 spi_writel(as
, PTCR
, SPI_BIT(RXTDIS
) | SPI_BIT(TXTDIS
));
724 atmel_spi_next_xfer_data(master
, xfer
, &tx_dma
, &rx_dma
, &len
);
725 remaining
= xfer
->len
- len
;
727 spi_writel(as
, RPR
, rx_dma
);
728 spi_writel(as
, TPR
, tx_dma
);
730 if (msg
->spi
->bits_per_word
> 8)
732 spi_writel(as
, RCR
, len
);
733 spi_writel(as
, TCR
, len
);
735 dev_dbg(&msg
->spi
->dev
,
736 " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
737 xfer
, xfer
->len
, xfer
->tx_buf
,
738 (unsigned long long)xfer
->tx_dma
, xfer
->rx_buf
,
739 (unsigned long long)xfer
->rx_dma
);
741 xfer
= as
->next_transfer
;
742 remaining
= as
->next_remaining_bytes
;
745 as
->current_transfer
= xfer
;
746 as
->current_remaining_bytes
= remaining
;
750 else if (!atmel_spi_xfer_is_last(msg
, xfer
)
751 && atmel_spi_xfer_can_be_chained(xfer
)) {
752 xfer
= list_entry(xfer
->transfer_list
.next
,
753 struct spi_transfer
, transfer_list
);
758 as
->next_transfer
= xfer
;
764 atmel_spi_next_xfer_data(master
, xfer
, &tx_dma
, &rx_dma
, &len
);
765 as
->next_remaining_bytes
= total
- len
;
767 spi_writel(as
, RNPR
, rx_dma
);
768 spi_writel(as
, TNPR
, tx_dma
);
770 if (msg
->spi
->bits_per_word
> 8)
772 spi_writel(as
, RNCR
, len
);
773 spi_writel(as
, TNCR
, len
);
775 dev_dbg(&msg
->spi
->dev
,
776 " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n",
777 xfer
, xfer
->len
, xfer
->tx_buf
,
778 (unsigned long long)xfer
->tx_dma
, xfer
->rx_buf
,
779 (unsigned long long)xfer
->rx_dma
);
780 ieval
= SPI_BIT(ENDRX
) | SPI_BIT(OVRES
);
782 spi_writel(as
, RNCR
, 0);
783 spi_writel(as
, TNCR
, 0);
784 ieval
= SPI_BIT(RXBUFF
) | SPI_BIT(ENDRX
) | SPI_BIT(OVRES
);
787 /* REVISIT: We're waiting for ENDRX before we start the next
788 * transfer because we need to handle some difficult timing
789 * issues otherwise. If we wait for ENDTX in one transfer and
790 * then starts waiting for ENDRX in the next, it's difficult
791 * to tell the difference between the ENDRX interrupt we're
792 * actually waiting for and the ENDRX interrupt of the
795 * It should be doable, though. Just not now...
797 spi_writel(as
, IER
, ieval
);
798 spi_writel(as
, PTCR
, SPI_BIT(TXTEN
) | SPI_BIT(RXTEN
));
802 * Choose way to submit next transfer and start it.
803 * lock is held, spi tasklet is blocked
805 static void atmel_spi_dma_next_xfer(struct spi_master
*master
,
806 struct spi_message
*msg
)
808 struct atmel_spi
*as
= spi_master_get_devdata(master
);
809 struct spi_transfer
*xfer
;
812 remaining
= as
->current_remaining_bytes
;
814 xfer
= as
->current_transfer
;
817 if (!as
->current_transfer
)
818 xfer
= list_entry(msg
->transfers
.next
,
819 struct spi_transfer
, transfer_list
);
822 as
->current_transfer
->transfer_list
.next
,
823 struct spi_transfer
, transfer_list
);
825 as
->current_transfer
= xfer
;
829 if (atmel_spi_use_dma(as
, xfer
)) {
831 if (!atmel_spi_next_xfer_dma_submit(master
, xfer
, &len
)) {
832 as
->current_remaining_bytes
= total
- len
;
835 dev_err(&msg
->spi
->dev
, "unable to use DMA, fallback to PIO\n");
839 /* use PIO if error appened using DMA */
840 atmel_spi_next_xfer_pio(master
, xfer
);
843 static void atmel_spi_next_message(struct spi_master
*master
)
845 struct atmel_spi
*as
= spi_master_get_devdata(master
);
846 struct spi_message
*msg
;
847 struct spi_device
*spi
;
849 BUG_ON(as
->current_transfer
);
851 msg
= list_entry(as
->queue
.next
, struct spi_message
, queue
);
854 dev_dbg(master
->dev
.parent
, "start message %p for %s\n",
855 msg
, dev_name(&spi
->dev
));
857 /* select chip if it's not still active */
859 if (as
->stay
!= spi
) {
860 cs_deactivate(as
, as
->stay
);
861 cs_activate(as
, spi
);
865 cs_activate(as
, spi
);
868 atmel_spi_pdc_next_xfer(master
, msg
);
870 atmel_spi_dma_next_xfer(master
, msg
);
874 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
875 * - The buffer is either valid for CPU access, else NULL
876 * - If the buffer is valid, so is its DMA address
878 * This driver manages the dma address unless message->is_dma_mapped.
881 atmel_spi_dma_map_xfer(struct atmel_spi
*as
, struct spi_transfer
*xfer
)
883 struct device
*dev
= &as
->pdev
->dev
;
885 xfer
->tx_dma
= xfer
->rx_dma
= INVALID_DMA_ADDRESS
;
887 /* tx_buf is a const void* where we need a void * for the dma
889 void *nonconst_tx
= (void *)xfer
->tx_buf
;
891 xfer
->tx_dma
= dma_map_single(dev
,
892 nonconst_tx
, xfer
->len
,
894 if (dma_mapping_error(dev
, xfer
->tx_dma
))
898 xfer
->rx_dma
= dma_map_single(dev
,
899 xfer
->rx_buf
, xfer
->len
,
901 if (dma_mapping_error(dev
, xfer
->rx_dma
)) {
903 dma_unmap_single(dev
,
904 xfer
->tx_dma
, xfer
->len
,
912 static void atmel_spi_dma_unmap_xfer(struct spi_master
*master
,
913 struct spi_transfer
*xfer
)
915 if (xfer
->tx_dma
!= INVALID_DMA_ADDRESS
)
916 dma_unmap_single(master
->dev
.parent
, xfer
->tx_dma
,
917 xfer
->len
, DMA_TO_DEVICE
);
918 if (xfer
->rx_dma
!= INVALID_DMA_ADDRESS
)
919 dma_unmap_single(master
->dev
.parent
, xfer
->rx_dma
,
920 xfer
->len
, DMA_FROM_DEVICE
);
923 static void atmel_spi_disable_pdc_transfer(struct atmel_spi
*as
)
925 spi_writel(as
, PTCR
, SPI_BIT(RXTDIS
) | SPI_BIT(TXTDIS
));
929 atmel_spi_msg_done(struct spi_master
*master
, struct atmel_spi
*as
,
930 struct spi_message
*msg
, int stay
)
932 if (!stay
|| as
->done_status
< 0)
933 cs_deactivate(as
, msg
->spi
);
937 list_del(&msg
->queue
);
938 msg
->status
= as
->done_status
;
940 dev_dbg(master
->dev
.parent
,
941 "xfer complete: %u bytes transferred\n",
944 atmel_spi_unlock(as
);
945 msg
->complete(msg
->context
);
948 as
->current_transfer
= NULL
;
949 as
->next_transfer
= NULL
;
952 /* continue if needed */
953 if (list_empty(&as
->queue
) || as
->stopping
) {
955 atmel_spi_disable_pdc_transfer(as
);
957 atmel_spi_next_message(master
);
964 * Must update "current_remaining_bytes" to keep track of data
968 atmel_spi_pump_pio_data(struct atmel_spi
*as
, struct spi_transfer
*xfer
)
974 unsigned long xfer_pos
= xfer
->len
- as
->current_remaining_bytes
;
977 if (xfer
->bits_per_word
> 8) {
978 rxp16
= (u16
*)(((u8
*)xfer
->rx_buf
) + xfer_pos
);
979 *rxp16
= spi_readl(as
, RDR
);
981 rxp
= ((u8
*)xfer
->rx_buf
) + xfer_pos
;
982 *rxp
= spi_readl(as
, RDR
);
987 if (xfer
->bits_per_word
> 8) {
988 as
->current_remaining_bytes
-= 2;
989 if (as
->current_remaining_bytes
< 0)
990 as
->current_remaining_bytes
= 0;
992 as
->current_remaining_bytes
--;
995 if (as
->current_remaining_bytes
) {
997 if (xfer
->bits_per_word
> 8) {
998 txp16
= (u16
*)(((u8
*)xfer
->tx_buf
)
1000 spi_writel(as
, TDR
, *txp16
);
1002 txp
= ((u8
*)xfer
->tx_buf
) + xfer_pos
+ 1;
1003 spi_writel(as
, TDR
, *txp
);
1006 spi_writel(as
, TDR
, 0);
1012 * Called from DMA callback + pio transfer and overrun IRQ.
1014 static void atmel_spi_tasklet_func(unsigned long data
)
1016 struct spi_master
*master
= (struct spi_master
*)data
;
1017 struct atmel_spi
*as
= spi_master_get_devdata(master
);
1018 struct spi_message
*msg
;
1019 struct spi_transfer
*xfer
;
1021 dev_vdbg(master
->dev
.parent
, "atmel_spi_tasklet_func\n");
1025 xfer
= as
->current_transfer
;
1028 /* already been there */
1031 msg
= list_entry(as
->queue
.next
, struct spi_message
, queue
);
1033 if (as
->current_remaining_bytes
== 0) {
1034 if (as
->done_status
< 0) {
1035 /* error happened (overrun) */
1036 if (atmel_spi_use_dma(as
, xfer
))
1037 atmel_spi_stop_dma(as
);
1039 /* only update length if no error */
1040 msg
->actual_length
+= xfer
->len
;
1043 if (atmel_spi_use_dma(as
, xfer
))
1044 if (!msg
->is_dma_mapped
)
1045 atmel_spi_dma_unmap_xfer(master
, xfer
);
1047 if (xfer
->delay_usecs
)
1048 udelay(xfer
->delay_usecs
);
1050 if (atmel_spi_xfer_is_last(msg
, xfer
) || as
->done_status
< 0) {
1051 /* report completed (or erroneous) message */
1052 atmel_spi_msg_done(master
, as
, msg
, xfer
->cs_change
);
1054 if (xfer
->cs_change
) {
1055 cs_deactivate(as
, msg
->spi
);
1057 cs_activate(as
, msg
->spi
);
1061 * Not done yet. Submit the next transfer.
1063 * FIXME handle protocol options for xfer
1065 atmel_spi_dma_next_xfer(master
, msg
);
1069 * Keep going, we still have data to send in
1070 * the current transfer.
1072 atmel_spi_dma_next_xfer(master
, msg
);
1076 atmel_spi_unlock(as
);
1081 * No need for locking in this Interrupt handler: done_status is the
1082 * only information modified. What we need is the update of this field
1083 * before tasklet runs. This is ensured by using barrier.
1086 atmel_spi_pio_interrupt(int irq
, void *dev_id
)
1088 struct spi_master
*master
= dev_id
;
1089 struct atmel_spi
*as
= spi_master_get_devdata(master
);
1090 u32 status
, pending
, imr
;
1091 struct spi_transfer
*xfer
;
1094 imr
= spi_readl(as
, IMR
);
1095 status
= spi_readl(as
, SR
);
1096 pending
= status
& imr
;
1098 if (pending
& SPI_BIT(OVRES
)) {
1100 spi_writel(as
, IDR
, SPI_BIT(OVRES
));
1101 dev_warn(master
->dev
.parent
, "overrun\n");
1104 * When we get an overrun, we disregard the current
1105 * transfer. Data will not be copied back from any
1106 * bounce buffer and msg->actual_len will not be
1107 * updated with the last xfer.
1109 * We will also not process any remaning transfers in
1112 * All actions are done in tasklet with done_status indication
1114 as
->done_status
= -EIO
;
1117 /* Clear any overrun happening while cleaning up */
1120 tasklet_schedule(&as
->tasklet
);
1122 } else if (pending
& SPI_BIT(RDRF
)) {
1125 if (as
->current_remaining_bytes
) {
1127 xfer
= as
->current_transfer
;
1128 atmel_spi_pump_pio_data(as
, xfer
);
1129 if (!as
->current_remaining_bytes
) {
1130 /* no more data to xfer, kick tasklet */
1131 spi_writel(as
, IDR
, pending
);
1132 tasklet_schedule(&as
->tasklet
);
1136 atmel_spi_unlock(as
);
1138 WARN_ONCE(pending
, "IRQ not handled, pending = %x\n", pending
);
1140 spi_writel(as
, IDR
, pending
);
1147 atmel_spi_pdc_interrupt(int irq
, void *dev_id
)
1149 struct spi_master
*master
= dev_id
;
1150 struct atmel_spi
*as
= spi_master_get_devdata(master
);
1151 struct spi_message
*msg
;
1152 struct spi_transfer
*xfer
;
1153 u32 status
, pending
, imr
;
1158 xfer
= as
->current_transfer
;
1159 msg
= list_entry(as
->queue
.next
, struct spi_message
, queue
);
1161 imr
= spi_readl(as
, IMR
);
1162 status
= spi_readl(as
, SR
);
1163 pending
= status
& imr
;
1165 if (pending
& SPI_BIT(OVRES
)) {
1170 spi_writel(as
, IDR
, (SPI_BIT(RXBUFF
) | SPI_BIT(ENDRX
)
1174 * When we get an overrun, we disregard the current
1175 * transfer. Data will not be copied back from any
1176 * bounce buffer and msg->actual_len will not be
1177 * updated with the last xfer.
1179 * We will also not process any remaning transfers in
1182 * First, stop the transfer and unmap the DMA buffers.
1184 spi_writel(as
, PTCR
, SPI_BIT(RXTDIS
) | SPI_BIT(TXTDIS
));
1185 if (!msg
->is_dma_mapped
)
1186 atmel_spi_dma_unmap_xfer(master
, xfer
);
1188 /* REVISIT: udelay in irq is unfriendly */
1189 if (xfer
->delay_usecs
)
1190 udelay(xfer
->delay_usecs
);
1192 dev_warn(master
->dev
.parent
, "overrun (%u/%u remaining)\n",
1193 spi_readl(as
, TCR
), spi_readl(as
, RCR
));
1196 * Clean up DMA registers and make sure the data
1197 * registers are empty.
1199 spi_writel(as
, RNCR
, 0);
1200 spi_writel(as
, TNCR
, 0);
1201 spi_writel(as
, RCR
, 0);
1202 spi_writel(as
, TCR
, 0);
1203 for (timeout
= 1000; timeout
; timeout
--)
1204 if (spi_readl(as
, SR
) & SPI_BIT(TXEMPTY
))
1207 dev_warn(master
->dev
.parent
,
1208 "timeout waiting for TXEMPTY");
1209 while (spi_readl(as
, SR
) & SPI_BIT(RDRF
))
1212 /* Clear any overrun happening while cleaning up */
1215 as
->done_status
= -EIO
;
1216 atmel_spi_msg_done(master
, as
, msg
, 0);
1217 } else if (pending
& (SPI_BIT(RXBUFF
) | SPI_BIT(ENDRX
))) {
1220 spi_writel(as
, IDR
, pending
);
1222 if (as
->current_remaining_bytes
== 0) {
1223 msg
->actual_length
+= xfer
->len
;
1225 if (!msg
->is_dma_mapped
)
1226 atmel_spi_dma_unmap_xfer(master
, xfer
);
1228 /* REVISIT: udelay in irq is unfriendly */
1229 if (xfer
->delay_usecs
)
1230 udelay(xfer
->delay_usecs
);
1232 if (atmel_spi_xfer_is_last(msg
, xfer
)) {
1233 /* report completed message */
1234 atmel_spi_msg_done(master
, as
, msg
,
1237 if (xfer
->cs_change
) {
1238 cs_deactivate(as
, msg
->spi
);
1240 cs_activate(as
, msg
->spi
);
1244 * Not done yet. Submit the next transfer.
1246 * FIXME handle protocol options for xfer
1248 atmel_spi_pdc_next_xfer(master
, msg
);
1252 * Keep going, we still have data to send in
1253 * the current transfer.
1255 atmel_spi_pdc_next_xfer(master
, msg
);
1259 atmel_spi_unlock(as
);
1264 static int atmel_spi_setup(struct spi_device
*spi
)
1266 struct atmel_spi
*as
;
1267 struct atmel_spi_device
*asd
;
1269 unsigned int bits
= spi
->bits_per_word
;
1270 unsigned long bus_hz
;
1271 unsigned int npcs_pin
;
1274 as
= spi_master_get_devdata(spi
->master
);
1279 if (spi
->chip_select
> spi
->master
->num_chipselect
) {
1281 "setup: invalid chipselect %u (%u defined)\n",
1282 spi
->chip_select
, spi
->master
->num_chipselect
);
1286 /* see notes above re chipselect */
1287 if (!atmel_spi_is_v2(as
)
1288 && spi
->chip_select
== 0
1289 && (spi
->mode
& SPI_CS_HIGH
)) {
1290 dev_dbg(&spi
->dev
, "setup: can't be active-high\n");
1294 /* v1 chips start out at half the peripheral bus speed. */
1295 bus_hz
= clk_get_rate(as
->clk
);
1296 if (!atmel_spi_is_v2(as
))
1299 if (spi
->max_speed_hz
) {
1301 * Calculate the lowest divider that satisfies the
1302 * constraint, assuming div32/fdiv/mbz == 0.
1304 scbr
= DIV_ROUND_UP(bus_hz
, spi
->max_speed_hz
);
1307 * If the resulting divider doesn't fit into the
1308 * register bitfield, we can't satisfy the constraint.
1310 if (scbr
>= (1 << SPI_SCBR_SIZE
)) {
1312 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
1313 spi
->max_speed_hz
, scbr
, bus_hz
/255);
1317 /* speed zero means "as slow as possible" */
1320 csr
= SPI_BF(SCBR
, scbr
) | SPI_BF(BITS
, bits
- 8);
1321 if (spi
->mode
& SPI_CPOL
)
1322 csr
|= SPI_BIT(CPOL
);
1323 if (!(spi
->mode
& SPI_CPHA
))
1324 csr
|= SPI_BIT(NCPHA
);
1326 /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
1328 * DLYBCT would add delays between words, slowing down transfers.
1329 * It could potentially be useful to cope with DMA bottlenecks, but
1330 * in those cases it's probably best to just use a lower bitrate.
1332 csr
|= SPI_BF(DLYBS
, 0);
1333 csr
|= SPI_BF(DLYBCT
, 0);
1335 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
1336 npcs_pin
= (unsigned int)spi
->controller_data
;
1338 if (gpio_is_valid(spi
->cs_gpio
))
1339 npcs_pin
= spi
->cs_gpio
;
1341 asd
= spi
->controller_state
;
1343 asd
= kzalloc(sizeof(struct atmel_spi_device
), GFP_KERNEL
);
1347 ret
= gpio_request(npcs_pin
, dev_name(&spi
->dev
));
1353 asd
->npcs_pin
= npcs_pin
;
1354 spi
->controller_state
= asd
;
1355 gpio_direction_output(npcs_pin
, !(spi
->mode
& SPI_CS_HIGH
));
1358 if (as
->stay
== spi
)
1360 cs_deactivate(as
, spi
);
1361 atmel_spi_unlock(as
);
1367 "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
1368 bus_hz
/ scbr
, bits
, spi
->mode
, spi
->chip_select
, csr
);
1370 if (!atmel_spi_is_v2(as
))
1371 spi_writel(as
, CSR0
+ 4 * spi
->chip_select
, csr
);
1376 static int atmel_spi_transfer(struct spi_device
*spi
, struct spi_message
*msg
)
1378 struct atmel_spi
*as
;
1379 struct spi_transfer
*xfer
;
1380 struct device
*controller
= spi
->master
->dev
.parent
;
1382 struct atmel_spi_device
*asd
;
1384 as
= spi_master_get_devdata(spi
->master
);
1386 dev_dbg(controller
, "new message %p submitted for %s\n",
1387 msg
, dev_name(&spi
->dev
));
1389 if (unlikely(list_empty(&msg
->transfers
)))
1395 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
1396 if (!(xfer
->tx_buf
|| xfer
->rx_buf
) && xfer
->len
) {
1397 dev_dbg(&spi
->dev
, "missing rx or tx buf\n");
1401 if (xfer
->bits_per_word
) {
1402 asd
= spi
->controller_state
;
1403 bits
= (asd
->csr
>> 4) & 0xf;
1404 if (bits
!= xfer
->bits_per_word
- 8) {
1405 dev_dbg(&spi
->dev
, "you can't yet change "
1406 "bits_per_word in transfers\n");
1407 return -ENOPROTOOPT
;
1411 if (xfer
->bits_per_word
> 8) {
1412 if (xfer
->len
% 2) {
1413 dev_dbg(&spi
->dev
, "buffer len should be 16 bits aligned\n");
1418 /* FIXME implement these protocol options!! */
1419 if (xfer
->speed_hz
< spi
->max_speed_hz
) {
1420 dev_dbg(&spi
->dev
, "can't change speed in transfer\n");
1421 return -ENOPROTOOPT
;
1425 * DMA map early, for performance (empties dcache ASAP) and
1426 * better fault reporting.
1428 if ((!msg
->is_dma_mapped
) && (atmel_spi_use_dma(as
, xfer
)
1430 if (atmel_spi_dma_map_xfer(as
, xfer
) < 0)
1436 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
1438 " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
1440 xfer
->tx_buf
, xfer
->tx_dma
,
1441 xfer
->rx_buf
, xfer
->rx_dma
);
1445 msg
->status
= -EINPROGRESS
;
1446 msg
->actual_length
= 0;
1449 list_add_tail(&msg
->queue
, &as
->queue
);
1450 if (!as
->current_transfer
)
1451 atmel_spi_next_message(spi
->master
);
1452 atmel_spi_unlock(as
);
1457 static void atmel_spi_cleanup(struct spi_device
*spi
)
1459 struct atmel_spi
*as
= spi_master_get_devdata(spi
->master
);
1460 struct atmel_spi_device
*asd
= spi
->controller_state
;
1461 unsigned gpio
= (unsigned) spi
->controller_data
;
1467 if (as
->stay
== spi
) {
1469 cs_deactivate(as
, spi
);
1471 atmel_spi_unlock(as
);
1473 spi
->controller_state
= NULL
;
1478 static inline unsigned int atmel_get_version(struct atmel_spi
*as
)
1480 return spi_readl(as
, VERSION
) & 0x00000fff;
1483 static void atmel_get_caps(struct atmel_spi
*as
)
1485 unsigned int version
;
1487 version
= atmel_get_version(as
);
1488 dev_info(&as
->pdev
->dev
, "version: 0x%x\n", version
);
1490 as
->caps
.is_spi2
= version
> 0x121;
1491 as
->caps
.has_wdrbt
= version
>= 0x210;
1492 as
->caps
.has_dma_support
= version
>= 0x212;
1495 /*-------------------------------------------------------------------------*/
1497 static int atmel_spi_probe(struct platform_device
*pdev
)
1499 struct resource
*regs
;
1503 struct spi_master
*master
;
1504 struct atmel_spi
*as
;
1506 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1510 irq
= platform_get_irq(pdev
, 0);
1514 clk
= clk_get(&pdev
->dev
, "spi_clk");
1516 return PTR_ERR(clk
);
1518 /* setup spi core then atmel-specific driver state */
1520 master
= spi_alloc_master(&pdev
->dev
, sizeof *as
);
1524 /* the spi->mode bits understood by this driver: */
1525 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1526 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(8, 16);
1527 master
->dev
.of_node
= pdev
->dev
.of_node
;
1528 master
->bus_num
= pdev
->id
;
1529 master
->num_chipselect
= master
->dev
.of_node
? 0 : 4;
1530 master
->setup
= atmel_spi_setup
;
1531 master
->transfer
= atmel_spi_transfer
;
1532 master
->cleanup
= atmel_spi_cleanup
;
1533 platform_set_drvdata(pdev
, master
);
1535 as
= spi_master_get_devdata(master
);
1538 * Scratch buffer is used for throwaway rx and tx data.
1539 * It's coherent to minimize dcache pollution.
1541 as
->buffer
= dma_alloc_coherent(&pdev
->dev
, BUFFER_SIZE
,
1542 &as
->buffer_dma
, GFP_KERNEL
);
1546 spin_lock_init(&as
->lock
);
1547 INIT_LIST_HEAD(&as
->queue
);
1550 as
->regs
= ioremap(regs
->start
, resource_size(regs
));
1552 goto out_free_buffer
;
1553 as
->phybase
= regs
->start
;
1559 as
->use_dma
= false;
1560 as
->use_pdc
= false;
1561 if (as
->caps
.has_dma_support
) {
1562 if (atmel_spi_configure_dma(as
) == 0)
1568 if (as
->caps
.has_dma_support
&& !as
->use_dma
)
1569 dev_info(&pdev
->dev
, "Atmel SPI Controller using PIO only\n");
1572 ret
= request_irq(irq
, atmel_spi_pdc_interrupt
, 0,
1573 dev_name(&pdev
->dev
), master
);
1575 tasklet_init(&as
->tasklet
, atmel_spi_tasklet_func
,
1576 (unsigned long)master
);
1578 ret
= request_irq(irq
, atmel_spi_pio_interrupt
, 0,
1579 dev_name(&pdev
->dev
), master
);
1582 goto out_unmap_regs
;
1584 /* Initialize the hardware */
1585 ret
= clk_prepare_enable(clk
);
1588 spi_writel(as
, CR
, SPI_BIT(SWRST
));
1589 spi_writel(as
, CR
, SPI_BIT(SWRST
)); /* AT91SAM9263 Rev B workaround */
1590 if (as
->caps
.has_wdrbt
) {
1591 spi_writel(as
, MR
, SPI_BIT(WDRBT
) | SPI_BIT(MODFDIS
)
1594 spi_writel(as
, MR
, SPI_BIT(MSTR
) | SPI_BIT(MODFDIS
));
1598 spi_writel(as
, PTCR
, SPI_BIT(RXTDIS
) | SPI_BIT(TXTDIS
));
1599 spi_writel(as
, CR
, SPI_BIT(SPIEN
));
1602 dev_info(&pdev
->dev
, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
1603 (unsigned long)regs
->start
, irq
);
1605 ret
= spi_register_master(master
);
1613 atmel_spi_release_dma(as
);
1615 spi_writel(as
, CR
, SPI_BIT(SWRST
));
1616 spi_writel(as
, CR
, SPI_BIT(SWRST
)); /* AT91SAM9263 Rev B workaround */
1617 clk_disable_unprepare(clk
);
1619 free_irq(irq
, master
);
1624 tasklet_kill(&as
->tasklet
);
1625 dma_free_coherent(&pdev
->dev
, BUFFER_SIZE
, as
->buffer
,
1629 spi_master_put(master
);
1633 static int atmel_spi_remove(struct platform_device
*pdev
)
1635 struct spi_master
*master
= platform_get_drvdata(pdev
);
1636 struct atmel_spi
*as
= spi_master_get_devdata(master
);
1637 struct spi_message
*msg
;
1638 struct spi_transfer
*xfer
;
1640 /* reset the hardware and block queue progress */
1641 spin_lock_irq(&as
->lock
);
1644 atmel_spi_stop_dma(as
);
1645 atmel_spi_release_dma(as
);
1648 spi_writel(as
, CR
, SPI_BIT(SWRST
));
1649 spi_writel(as
, CR
, SPI_BIT(SWRST
)); /* AT91SAM9263 Rev B workaround */
1651 spin_unlock_irq(&as
->lock
);
1653 /* Terminate remaining queued transfers */
1654 list_for_each_entry(msg
, &as
->queue
, queue
) {
1655 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
1656 if (!msg
->is_dma_mapped
1657 && (atmel_spi_use_dma(as
, xfer
)
1659 atmel_spi_dma_unmap_xfer(master
, xfer
);
1661 msg
->status
= -ESHUTDOWN
;
1662 msg
->complete(msg
->context
);
1666 tasklet_kill(&as
->tasklet
);
1667 dma_free_coherent(&pdev
->dev
, BUFFER_SIZE
, as
->buffer
,
1670 clk_disable_unprepare(as
->clk
);
1672 free_irq(as
->irq
, master
);
1675 spi_unregister_master(master
);
1682 static int atmel_spi_suspend(struct platform_device
*pdev
, pm_message_t mesg
)
1684 struct spi_master
*master
= platform_get_drvdata(pdev
);
1685 struct atmel_spi
*as
= spi_master_get_devdata(master
);
1687 clk_disable_unprepare(as
->clk
);
1691 static int atmel_spi_resume(struct platform_device
*pdev
)
1693 struct spi_master
*master
= platform_get_drvdata(pdev
);
1694 struct atmel_spi
*as
= spi_master_get_devdata(master
);
1696 return clk_prepare_enable(as
->clk
);
1701 #define atmel_spi_suspend NULL
1702 #define atmel_spi_resume NULL
1705 #if defined(CONFIG_OF)
1706 static const struct of_device_id atmel_spi_dt_ids
[] = {
1707 { .compatible
= "atmel,at91rm9200-spi" },
1711 MODULE_DEVICE_TABLE(of
, atmel_spi_dt_ids
);
1714 static struct platform_driver atmel_spi_driver
= {
1716 .name
= "atmel_spi",
1717 .owner
= THIS_MODULE
,
1718 .of_match_table
= of_match_ptr(atmel_spi_dt_ids
),
1720 .suspend
= atmel_spi_suspend
,
1721 .resume
= atmel_spi_resume
,
1722 .probe
= atmel_spi_probe
,
1723 .remove
= atmel_spi_remove
,
1725 module_platform_driver(atmel_spi_driver
);
1727 MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
1728 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1729 MODULE_LICENSE("GPL");
1730 MODULE_ALIAS("platform:atmel_spi");