2 * Driver for Cirrus Logic EP93xx SPI controller.
4 * Copyright (C) 2010-2011 Mika Westerberg
6 * Explicit FIFO handling code was inspired by amba-pl022 driver.
8 * Chip select support using other than built-in GPIOs by H. Hartley Sweeten.
10 * For more information about the SPI controller see documentation on Cirrus
12 * http://www.cirrus.com/en/pubs/manual/EP93xx_Users_Guide_UM1.pdf
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
20 #include <linux/clk.h>
21 #include <linux/err.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/dmaengine.h>
25 #include <linux/bitops.h>
26 #include <linux/interrupt.h>
27 #include <linux/module.h>
28 #include <linux/platform_device.h>
29 #include <linux/sched.h>
30 #include <linux/scatterlist.h>
31 #include <linux/spi/spi.h>
33 #include <linux/platform_data/dma-ep93xx.h>
34 #include <linux/platform_data/spi-ep93xx.h>
37 #define SSPCR0_MODE_SHIFT 6
38 #define SSPCR0_SCR_SHIFT 8
41 #define SSPCR1_RIE BIT(0)
42 #define SSPCR1_TIE BIT(1)
43 #define SSPCR1_RORIE BIT(2)
44 #define SSPCR1_LBM BIT(3)
45 #define SSPCR1_SSE BIT(4)
46 #define SSPCR1_MS BIT(5)
47 #define SSPCR1_SOD BIT(6)
52 #define SSPSR_TFE BIT(0)
53 #define SSPSR_TNF BIT(1)
54 #define SSPSR_RNE BIT(2)
55 #define SSPSR_RFF BIT(3)
56 #define SSPSR_BSY BIT(4)
57 #define SSPCPSR 0x0010
60 #define SSPIIR_RIS BIT(0)
61 #define SSPIIR_TIS BIT(1)
62 #define SSPIIR_RORIS BIT(2)
65 /* timeout in milliseconds */
67 /* maximum depth of RX/TX FIFO */
68 #define SPI_FIFO_SIZE 8
71 * struct ep93xx_spi - EP93xx SPI controller structure
72 * @pdev: pointer to platform device
73 * @clk: clock for the controller
74 * @regs_base: pointer to ioremap()'d registers
75 * @sspdr_phys: physical address of the SSPDR register
76 * @min_rate: minimum clock rate (in Hz) supported by the controller
77 * @max_rate: maximum clock rate (in Hz) supported by the controller
78 * @wait: wait here until given transfer is completed
79 * @current_msg: message that is currently processed (or %NULL if none)
80 * @tx: current byte in transfer to transmit
81 * @rx: current byte in transfer to receive
82 * @fifo_level: how full is FIFO (%0..%SPI_FIFO_SIZE - %1). Receiving one
83 * frame decreases this level and sending one frame increases it.
84 * @dma_rx: RX DMA channel
85 * @dma_tx: TX DMA channel
86 * @dma_rx_data: RX parameters passed to the DMA engine
87 * @dma_tx_data: TX parameters passed to the DMA engine
88 * @rx_sgt: sg table for RX transfers
89 * @tx_sgt: sg table for TX transfers
90 * @zeropage: dummy page used as RX buffer when only TX buffer is passed in by
94 const struct platform_device
*pdev
;
96 void __iomem
*regs_base
;
97 unsigned long sspdr_phys
;
98 unsigned long min_rate
;
99 unsigned long max_rate
;
100 struct completion wait
;
101 struct spi_message
*current_msg
;
105 struct dma_chan
*dma_rx
;
106 struct dma_chan
*dma_tx
;
107 struct ep93xx_dma_data dma_rx_data
;
108 struct ep93xx_dma_data dma_tx_data
;
109 struct sg_table rx_sgt
;
110 struct sg_table tx_sgt
;
115 * struct ep93xx_spi_chip - SPI device hardware settings
116 * @spi: back pointer to the SPI device
117 * @ops: private chip operations
119 struct ep93xx_spi_chip
{
120 const struct spi_device
*spi
;
121 struct ep93xx_spi_chip_ops
*ops
;
124 /* converts bits per word to CR0.DSS value */
125 #define bits_per_word_to_dss(bpw) ((bpw) - 1)
127 static void ep93xx_spi_write_u8(const struct ep93xx_spi
*espi
,
130 writeb(value
, espi
->regs_base
+ reg
);
133 static u8
ep93xx_spi_read_u8(const struct ep93xx_spi
*spi
, u16 reg
)
135 return readb(spi
->regs_base
+ reg
);
138 static void ep93xx_spi_write_u16(const struct ep93xx_spi
*espi
,
141 writew(value
, espi
->regs_base
+ reg
);
144 static u16
ep93xx_spi_read_u16(const struct ep93xx_spi
*spi
, u16 reg
)
146 return readw(spi
->regs_base
+ reg
);
149 static int ep93xx_spi_enable(const struct ep93xx_spi
*espi
)
154 err
= clk_enable(espi
->clk
);
158 regval
= ep93xx_spi_read_u8(espi
, SSPCR1
);
159 regval
|= SSPCR1_SSE
;
160 ep93xx_spi_write_u8(espi
, SSPCR1
, regval
);
165 static void ep93xx_spi_disable(const struct ep93xx_spi
*espi
)
169 regval
= ep93xx_spi_read_u8(espi
, SSPCR1
);
170 regval
&= ~SSPCR1_SSE
;
171 ep93xx_spi_write_u8(espi
, SSPCR1
, regval
);
173 clk_disable(espi
->clk
);
176 static void ep93xx_spi_enable_interrupts(const struct ep93xx_spi
*espi
)
180 regval
= ep93xx_spi_read_u8(espi
, SSPCR1
);
181 regval
|= (SSPCR1_RORIE
| SSPCR1_TIE
| SSPCR1_RIE
);
182 ep93xx_spi_write_u8(espi
, SSPCR1
, regval
);
185 static void ep93xx_spi_disable_interrupts(const struct ep93xx_spi
*espi
)
189 regval
= ep93xx_spi_read_u8(espi
, SSPCR1
);
190 regval
&= ~(SSPCR1_RORIE
| SSPCR1_TIE
| SSPCR1_RIE
);
191 ep93xx_spi_write_u8(espi
, SSPCR1
, regval
);
195 * ep93xx_spi_calc_divisors() - calculates SPI clock divisors
196 * @espi: ep93xx SPI controller struct
197 * @rate: desired SPI output clock rate
198 * @div_cpsr: pointer to return the cpsr (pre-scaler) divider
199 * @div_scr: pointer to return the scr divider
201 static int ep93xx_spi_calc_divisors(const struct ep93xx_spi
*espi
,
203 u8
*div_cpsr
, u8
*div_scr
)
205 unsigned long spi_clk_rate
= clk_get_rate(espi
->clk
);
209 * Make sure that max value is between values supported by the
210 * controller. Note that minimum value is already checked in
211 * ep93xx_spi_transfer_one_message().
213 rate
= clamp(rate
, espi
->min_rate
, espi
->max_rate
);
216 * Calculate divisors so that we can get speed according the
218 * rate = spi_clock_rate / (cpsr * (1 + scr))
220 * cpsr must be even number and starts from 2, scr can be any number
223 for (cpsr
= 2; cpsr
<= 254; cpsr
+= 2) {
224 for (scr
= 0; scr
<= 255; scr
++) {
225 if ((spi_clk_rate
/ (cpsr
* (scr
+ 1))) <= rate
) {
227 *div_cpsr
= (u8
)cpsr
;
236 static void ep93xx_spi_cs_control(struct spi_device
*spi
, bool control
)
238 struct ep93xx_spi_chip
*chip
= spi_get_ctldata(spi
);
239 int value
= (spi
->mode
& SPI_CS_HIGH
) ? control
: !control
;
241 if (chip
->ops
&& chip
->ops
->cs_control
)
242 chip
->ops
->cs_control(spi
, value
);
246 * ep93xx_spi_setup() - setup an SPI device
247 * @spi: SPI device to setup
249 * This function sets up SPI device mode, speed etc. Can be called multiple
250 * times for a single device. Returns %0 in case of success, negative error in
251 * case of failure. When this function returns success, the device is
254 static int ep93xx_spi_setup(struct spi_device
*spi
)
256 struct ep93xx_spi
*espi
= spi_master_get_devdata(spi
->master
);
257 struct ep93xx_spi_chip
*chip
;
259 chip
= spi_get_ctldata(spi
);
261 dev_dbg(&espi
->pdev
->dev
, "initial setup for %s\n",
264 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
269 chip
->ops
= spi
->controller_data
;
271 if (chip
->ops
&& chip
->ops
->setup
) {
272 int ret
= chip
->ops
->setup(spi
);
279 spi_set_ctldata(spi
, chip
);
282 ep93xx_spi_cs_control(spi
, false);
287 * ep93xx_spi_cleanup() - cleans up master controller specific state
288 * @spi: SPI device to cleanup
290 * This function releases master controller specific state for given @spi
293 static void ep93xx_spi_cleanup(struct spi_device
*spi
)
295 struct ep93xx_spi_chip
*chip
;
297 chip
= spi_get_ctldata(spi
);
299 if (chip
->ops
&& chip
->ops
->cleanup
)
300 chip
->ops
->cleanup(spi
);
301 spi_set_ctldata(spi
, NULL
);
307 * ep93xx_spi_chip_setup() - configures hardware according to given @chip
308 * @espi: ep93xx SPI controller struct
309 * @chip: chip specific settings
310 * @speed_hz: transfer speed
311 * @bits_per_word: transfer bits_per_word
313 static int ep93xx_spi_chip_setup(const struct ep93xx_spi
*espi
,
314 const struct ep93xx_spi_chip
*chip
,
315 u32 speed_hz
, u8 bits_per_word
)
317 u8 dss
= bits_per_word_to_dss(bits_per_word
);
323 err
= ep93xx_spi_calc_divisors(espi
, speed_hz
, &div_cpsr
, &div_scr
);
327 cr0
= div_scr
<< SSPCR0_SCR_SHIFT
;
328 cr0
|= (chip
->spi
->mode
& (SPI_CPHA
|SPI_CPOL
)) << SSPCR0_MODE_SHIFT
;
331 dev_dbg(&espi
->pdev
->dev
, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
332 chip
->spi
->mode
, div_cpsr
, div_scr
, dss
);
333 dev_dbg(&espi
->pdev
->dev
, "setup: cr0 %#x\n", cr0
);
335 ep93xx_spi_write_u8(espi
, SSPCPSR
, div_cpsr
);
336 ep93xx_spi_write_u16(espi
, SSPCR0
, cr0
);
341 static void ep93xx_do_write(struct ep93xx_spi
*espi
, struct spi_transfer
*t
)
343 if (t
->bits_per_word
> 8) {
347 tx_val
= ((u16
*)t
->tx_buf
)[espi
->tx
];
348 ep93xx_spi_write_u16(espi
, SSPDR
, tx_val
);
349 espi
->tx
+= sizeof(tx_val
);
354 tx_val
= ((u8
*)t
->tx_buf
)[espi
->tx
];
355 ep93xx_spi_write_u8(espi
, SSPDR
, tx_val
);
356 espi
->tx
+= sizeof(tx_val
);
360 static void ep93xx_do_read(struct ep93xx_spi
*espi
, struct spi_transfer
*t
)
362 if (t
->bits_per_word
> 8) {
365 rx_val
= ep93xx_spi_read_u16(espi
, SSPDR
);
367 ((u16
*)t
->rx_buf
)[espi
->rx
] = rx_val
;
368 espi
->rx
+= sizeof(rx_val
);
372 rx_val
= ep93xx_spi_read_u8(espi
, SSPDR
);
374 ((u8
*)t
->rx_buf
)[espi
->rx
] = rx_val
;
375 espi
->rx
+= sizeof(rx_val
);
380 * ep93xx_spi_read_write() - perform next RX/TX transfer
381 * @espi: ep93xx SPI controller struct
383 * This function transfers next bytes (or half-words) to/from RX/TX FIFOs. If
384 * called several times, the whole transfer will be completed. Returns
385 * %-EINPROGRESS when current transfer was not yet completed otherwise %0.
387 * When this function is finished, RX FIFO should be empty and TX FIFO should be
390 static int ep93xx_spi_read_write(struct ep93xx_spi
*espi
)
392 struct spi_message
*msg
= espi
->current_msg
;
393 struct spi_transfer
*t
= msg
->state
;
395 /* read as long as RX FIFO has frames in it */
396 while ((ep93xx_spi_read_u8(espi
, SSPSR
) & SSPSR_RNE
)) {
397 ep93xx_do_read(espi
, t
);
401 /* write as long as TX FIFO has room */
402 while (espi
->fifo_level
< SPI_FIFO_SIZE
&& espi
->tx
< t
->len
) {
403 ep93xx_do_write(espi
, t
);
407 if (espi
->rx
== t
->len
)
413 static void ep93xx_spi_pio_transfer(struct ep93xx_spi
*espi
)
416 * Now everything is set up for the current transfer. We prime the TX
417 * FIFO, enable interrupts, and wait for the transfer to complete.
419 if (ep93xx_spi_read_write(espi
)) {
420 ep93xx_spi_enable_interrupts(espi
);
421 wait_for_completion(&espi
->wait
);
426 * ep93xx_spi_dma_prepare() - prepares a DMA transfer
427 * @espi: ep93xx SPI controller struct
428 * @dir: DMA transfer direction
430 * Function configures the DMA, maps the buffer and prepares the DMA
431 * descriptor. Returns a valid DMA descriptor in case of success and ERR_PTR
432 * in case of failure.
434 static struct dma_async_tx_descriptor
*
435 ep93xx_spi_dma_prepare(struct ep93xx_spi
*espi
, enum dma_transfer_direction dir
)
437 struct spi_transfer
*t
= espi
->current_msg
->state
;
438 struct dma_async_tx_descriptor
*txd
;
439 enum dma_slave_buswidth buswidth
;
440 struct dma_slave_config conf
;
441 struct scatterlist
*sg
;
442 struct sg_table
*sgt
;
443 struct dma_chan
*chan
;
444 const void *buf
, *pbuf
;
448 if (t
->bits_per_word
> 8)
449 buswidth
= DMA_SLAVE_BUSWIDTH_2_BYTES
;
451 buswidth
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
453 memset(&conf
, 0, sizeof(conf
));
454 conf
.direction
= dir
;
456 if (dir
== DMA_DEV_TO_MEM
) {
461 conf
.src_addr
= espi
->sspdr_phys
;
462 conf
.src_addr_width
= buswidth
;
468 conf
.dst_addr
= espi
->sspdr_phys
;
469 conf
.dst_addr_width
= buswidth
;
472 ret
= dmaengine_slave_config(chan
, &conf
);
477 * We need to split the transfer into PAGE_SIZE'd chunks. This is
478 * because we are using @espi->zeropage to provide a zero RX buffer
479 * for the TX transfers and we have only allocated one page for that.
481 * For performance reasons we allocate a new sg_table only when
482 * needed. Otherwise we will re-use the current one. Eventually the
483 * last sg_table is released in ep93xx_spi_release_dma().
486 nents
= DIV_ROUND_UP(len
, PAGE_SIZE
);
487 if (nents
!= sgt
->nents
) {
490 ret
= sg_alloc_table(sgt
, nents
, GFP_KERNEL
);
496 for_each_sg(sgt
->sgl
, sg
, sgt
->nents
, i
) {
497 size_t bytes
= min_t(size_t, len
, PAGE_SIZE
);
500 sg_set_page(sg
, virt_to_page(pbuf
), bytes
,
501 offset_in_page(pbuf
));
503 sg_set_page(sg
, virt_to_page(espi
->zeropage
),
512 dev_warn(&espi
->pdev
->dev
, "len = %zu expected 0!\n", len
);
513 return ERR_PTR(-EINVAL
);
516 nents
= dma_map_sg(chan
->device
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
518 return ERR_PTR(-ENOMEM
);
520 txd
= dmaengine_prep_slave_sg(chan
, sgt
->sgl
, nents
, dir
, DMA_CTRL_ACK
);
522 dma_unmap_sg(chan
->device
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
523 return ERR_PTR(-ENOMEM
);
529 * ep93xx_spi_dma_finish() - finishes with a DMA transfer
530 * @espi: ep93xx SPI controller struct
531 * @dir: DMA transfer direction
533 * Function finishes with the DMA transfer. After this, the DMA buffer is
536 static void ep93xx_spi_dma_finish(struct ep93xx_spi
*espi
,
537 enum dma_transfer_direction dir
)
539 struct dma_chan
*chan
;
540 struct sg_table
*sgt
;
542 if (dir
== DMA_DEV_TO_MEM
) {
550 dma_unmap_sg(chan
->device
->dev
, sgt
->sgl
, sgt
->nents
, dir
);
553 static void ep93xx_spi_dma_callback(void *callback_param
)
555 complete(callback_param
);
558 static void ep93xx_spi_dma_transfer(struct ep93xx_spi
*espi
)
560 struct spi_message
*msg
= espi
->current_msg
;
561 struct dma_async_tx_descriptor
*rxd
, *txd
;
563 rxd
= ep93xx_spi_dma_prepare(espi
, DMA_DEV_TO_MEM
);
565 dev_err(&espi
->pdev
->dev
, "DMA RX failed: %ld\n", PTR_ERR(rxd
));
566 msg
->status
= PTR_ERR(rxd
);
570 txd
= ep93xx_spi_dma_prepare(espi
, DMA_MEM_TO_DEV
);
572 ep93xx_spi_dma_finish(espi
, DMA_DEV_TO_MEM
);
573 dev_err(&espi
->pdev
->dev
, "DMA TX failed: %ld\n", PTR_ERR(rxd
));
574 msg
->status
= PTR_ERR(txd
);
578 /* We are ready when RX is done */
579 rxd
->callback
= ep93xx_spi_dma_callback
;
580 rxd
->callback_param
= &espi
->wait
;
582 /* Now submit both descriptors and wait while they finish */
583 dmaengine_submit(rxd
);
584 dmaengine_submit(txd
);
586 dma_async_issue_pending(espi
->dma_rx
);
587 dma_async_issue_pending(espi
->dma_tx
);
589 wait_for_completion(&espi
->wait
);
591 ep93xx_spi_dma_finish(espi
, DMA_MEM_TO_DEV
);
592 ep93xx_spi_dma_finish(espi
, DMA_DEV_TO_MEM
);
596 * ep93xx_spi_process_transfer() - processes one SPI transfer
597 * @espi: ep93xx SPI controller struct
598 * @msg: current message
599 * @t: transfer to process
601 * This function processes one SPI transfer given in @t. Function waits until
602 * transfer is complete (may sleep) and updates @msg->status based on whether
603 * transfer was successfully processed or not.
605 static void ep93xx_spi_process_transfer(struct ep93xx_spi
*espi
,
606 struct spi_message
*msg
,
607 struct spi_transfer
*t
)
609 struct ep93xx_spi_chip
*chip
= spi_get_ctldata(msg
->spi
);
614 err
= ep93xx_spi_chip_setup(espi
, chip
, t
->speed_hz
, t
->bits_per_word
);
616 dev_err(&espi
->pdev
->dev
,
617 "failed to setup chip for transfer\n");
626 * There is no point of setting up DMA for the transfers which will
627 * fit into the FIFO and can be transferred with a single interrupt.
628 * So in these cases we will be using PIO and don't bother for DMA.
630 if (espi
->dma_rx
&& t
->len
> SPI_FIFO_SIZE
)
631 ep93xx_spi_dma_transfer(espi
);
633 ep93xx_spi_pio_transfer(espi
);
636 * In case of error during transmit, we bail out from processing
642 msg
->actual_length
+= t
->len
;
645 * After this transfer is finished, perform any possible
646 * post-transfer actions requested by the protocol driver.
648 if (t
->delay_usecs
) {
649 set_current_state(TASK_UNINTERRUPTIBLE
);
650 schedule_timeout(usecs_to_jiffies(t
->delay_usecs
));
653 if (!list_is_last(&t
->transfer_list
, &msg
->transfers
)) {
655 * In case protocol driver is asking us to drop the
656 * chipselect briefly, we let the scheduler to handle
659 ep93xx_spi_cs_control(msg
->spi
, false);
661 ep93xx_spi_cs_control(msg
->spi
, true);
667 * ep93xx_spi_process_message() - process one SPI message
668 * @espi: ep93xx SPI controller struct
669 * @msg: message to process
671 * This function processes a single SPI message. We go through all transfers in
672 * the message and pass them to ep93xx_spi_process_transfer(). Chipselect is
673 * asserted during the whole message (unless per transfer cs_change is set).
675 * @msg->status contains %0 in case of success or negative error code in case of
678 static void ep93xx_spi_process_message(struct ep93xx_spi
*espi
,
679 struct spi_message
*msg
)
681 unsigned long timeout
;
682 struct spi_transfer
*t
;
686 * Enable the SPI controller and its clock.
688 err
= ep93xx_spi_enable(espi
);
690 dev_err(&espi
->pdev
->dev
, "failed to enable SPI controller\n");
696 * Just to be sure: flush any data from RX FIFO.
698 timeout
= jiffies
+ msecs_to_jiffies(SPI_TIMEOUT
);
699 while (ep93xx_spi_read_u16(espi
, SSPSR
) & SSPSR_RNE
) {
700 if (time_after(jiffies
, timeout
)) {
701 dev_warn(&espi
->pdev
->dev
,
702 "timeout while flushing RX FIFO\n");
703 msg
->status
= -ETIMEDOUT
;
706 ep93xx_spi_read_u16(espi
, SSPDR
);
710 * We explicitly handle FIFO level. This way we don't have to check TX
711 * FIFO status using %SSPSR_TNF bit which may cause RX FIFO overruns.
713 espi
->fifo_level
= 0;
716 * Assert the chipselect.
718 ep93xx_spi_cs_control(msg
->spi
, true);
720 list_for_each_entry(t
, &msg
->transfers
, transfer_list
) {
721 ep93xx_spi_process_transfer(espi
, msg
, t
);
727 * Now the whole message is transferred (or failed for some reason). We
728 * deselect the device and disable the SPI controller.
730 ep93xx_spi_cs_control(msg
->spi
, false);
731 ep93xx_spi_disable(espi
);
734 static int ep93xx_spi_transfer_one_message(struct spi_master
*master
,
735 struct spi_message
*msg
)
737 struct ep93xx_spi
*espi
= spi_master_get_devdata(master
);
738 struct spi_transfer
*t
;
740 /* first validate each transfer */
741 list_for_each_entry(t
, &msg
->transfers
, transfer_list
) {
742 if (t
->speed_hz
< espi
->min_rate
)
748 msg
->actual_length
= 0;
750 espi
->current_msg
= msg
;
751 ep93xx_spi_process_message(espi
, msg
);
752 espi
->current_msg
= NULL
;
754 spi_finalize_current_message(master
);
759 static irqreturn_t
ep93xx_spi_interrupt(int irq
, void *dev_id
)
761 struct ep93xx_spi
*espi
= dev_id
;
762 u8 irq_status
= ep93xx_spi_read_u8(espi
, SSPIIR
);
765 * If we got ROR (receive overrun) interrupt we know that something is
766 * wrong. Just abort the message.
768 if (unlikely(irq_status
& SSPIIR_RORIS
)) {
769 /* clear the overrun interrupt */
770 ep93xx_spi_write_u8(espi
, SSPICR
, 0);
771 dev_warn(&espi
->pdev
->dev
,
772 "receive overrun, aborting the message\n");
773 espi
->current_msg
->status
= -EIO
;
776 * Interrupt is either RX (RIS) or TX (TIS). For both cases we
777 * simply execute next data transfer.
779 if (ep93xx_spi_read_write(espi
)) {
781 * In normal case, there still is some processing left
782 * for current transfer. Let's wait for the next
790 * Current transfer is finished, either with error or with success. In
791 * any case we disable interrupts and notify the worker to handle
792 * any post-processing of the message.
794 ep93xx_spi_disable_interrupts(espi
);
795 complete(&espi
->wait
);
799 static bool ep93xx_spi_dma_filter(struct dma_chan
*chan
, void *filter_param
)
801 if (ep93xx_dma_chan_is_m2p(chan
))
804 chan
->private = filter_param
;
808 static int ep93xx_spi_setup_dma(struct ep93xx_spi
*espi
)
813 espi
->zeropage
= (void *)get_zeroed_page(GFP_KERNEL
);
818 dma_cap_set(DMA_SLAVE
, mask
);
820 espi
->dma_rx_data
.port
= EP93XX_DMA_SSP
;
821 espi
->dma_rx_data
.direction
= DMA_DEV_TO_MEM
;
822 espi
->dma_rx_data
.name
= "ep93xx-spi-rx";
824 espi
->dma_rx
= dma_request_channel(mask
, ep93xx_spi_dma_filter
,
831 espi
->dma_tx_data
.port
= EP93XX_DMA_SSP
;
832 espi
->dma_tx_data
.direction
= DMA_MEM_TO_DEV
;
833 espi
->dma_tx_data
.name
= "ep93xx-spi-tx";
835 espi
->dma_tx
= dma_request_channel(mask
, ep93xx_spi_dma_filter
,
839 goto fail_release_rx
;
845 dma_release_channel(espi
->dma_rx
);
848 free_page((unsigned long)espi
->zeropage
);
853 static void ep93xx_spi_release_dma(struct ep93xx_spi
*espi
)
856 dma_release_channel(espi
->dma_rx
);
857 sg_free_table(&espi
->rx_sgt
);
860 dma_release_channel(espi
->dma_tx
);
861 sg_free_table(&espi
->tx_sgt
);
865 free_page((unsigned long)espi
->zeropage
);
868 static int ep93xx_spi_probe(struct platform_device
*pdev
)
870 struct spi_master
*master
;
871 struct ep93xx_spi_info
*info
;
872 struct ep93xx_spi
*espi
;
873 struct resource
*res
;
877 info
= dev_get_platdata(&pdev
->dev
);
879 irq
= platform_get_irq(pdev
, 0);
881 dev_err(&pdev
->dev
, "failed to get irq resources\n");
885 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
887 dev_err(&pdev
->dev
, "unable to get iomem resource\n");
891 master
= spi_alloc_master(&pdev
->dev
, sizeof(*espi
));
895 master
->setup
= ep93xx_spi_setup
;
896 master
->transfer_one_message
= ep93xx_spi_transfer_one_message
;
897 master
->cleanup
= ep93xx_spi_cleanup
;
898 master
->bus_num
= pdev
->id
;
899 master
->num_chipselect
= info
->num_chipselect
;
900 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
901 master
->bits_per_word_mask
= SPI_BPW_RANGE_MASK(4, 16);
903 platform_set_drvdata(pdev
, master
);
905 espi
= spi_master_get_devdata(master
);
907 espi
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
908 if (IS_ERR(espi
->clk
)) {
909 dev_err(&pdev
->dev
, "unable to get spi clock\n");
910 error
= PTR_ERR(espi
->clk
);
911 goto fail_release_master
;
914 init_completion(&espi
->wait
);
917 * Calculate maximum and minimum supported clock rates
918 * for the controller.
920 espi
->max_rate
= clk_get_rate(espi
->clk
) / 2;
921 espi
->min_rate
= clk_get_rate(espi
->clk
) / (254 * 256);
924 espi
->sspdr_phys
= res
->start
+ SSPDR
;
926 espi
->regs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
927 if (IS_ERR(espi
->regs_base
)) {
928 error
= PTR_ERR(espi
->regs_base
);
929 goto fail_release_master
;
932 error
= devm_request_irq(&pdev
->dev
, irq
, ep93xx_spi_interrupt
,
933 0, "ep93xx-spi", espi
);
935 dev_err(&pdev
->dev
, "failed to request irq\n");
936 goto fail_release_master
;
939 if (info
->use_dma
&& ep93xx_spi_setup_dma(espi
))
940 dev_warn(&pdev
->dev
, "DMA setup failed. Falling back to PIO\n");
942 /* make sure that the hardware is disabled */
943 ep93xx_spi_write_u8(espi
, SSPCR1
, 0);
945 error
= devm_spi_register_master(&pdev
->dev
, master
);
947 dev_err(&pdev
->dev
, "failed to register SPI master\n");
951 dev_info(&pdev
->dev
, "EP93xx SPI Controller at 0x%08lx irq %d\n",
952 (unsigned long)res
->start
, irq
);
957 ep93xx_spi_release_dma(espi
);
959 spi_master_put(master
);
964 static int ep93xx_spi_remove(struct platform_device
*pdev
)
966 struct spi_master
*master
= platform_get_drvdata(pdev
);
967 struct ep93xx_spi
*espi
= spi_master_get_devdata(master
);
969 ep93xx_spi_release_dma(espi
);
974 static struct platform_driver ep93xx_spi_driver
= {
976 .name
= "ep93xx-spi",
977 .owner
= THIS_MODULE
,
979 .probe
= ep93xx_spi_probe
,
980 .remove
= ep93xx_spi_remove
,
982 module_platform_driver(ep93xx_spi_driver
);
984 MODULE_DESCRIPTION("EP93xx SPI Controller driver");
985 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@iki.fi>");
986 MODULE_LICENSE("GPL");
987 MODULE_ALIAS("platform:ep93xx-spi");