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/err.h>
19 #include <linux/interrupt.h>
20 #include <linux/spi/spi.h>
23 #include <asm/arch/board.h>
24 #include <asm/arch/gpio.h>
25 #include <asm/arch/cpu.h>
27 #include "atmel_spi.h"
30 * The core SPI transfer engine just talks to a register bank to set up
31 * DMA transfers; transfer queue progress is driven by IRQs. The clock
32 * framework provides the base clock, subdivided for each spi_device.
34 * Newer controllers, marked with "new_1" flag, have:
36 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
37 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
39 * - SPI_CSRx.SBCR allows faster clocking
47 struct platform_device *pdev;
49 struct spi_device *stay;
52 struct list_head queue;
53 struct spi_transfer *current_transfer;
54 unsigned long current_remaining_bytes;
55 struct spi_transfer *next_transfer;
56 unsigned long next_remaining_bytes;
59 dma_addr_t buffer_dma;
62 #define BUFFER_SIZE PAGE_SIZE
63 #define INVALID_DMA_ADDRESS 0xffffffff
66 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
67 * they assume that spi slave device state will not change on deselect, so
68 * that automagic deselection is OK. ("NPCSx rises if no data is to be
69 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
70 * controllers have CSAAT and friends.
72 * Since the CSAAT functionality is a bit weird on newer controllers as
73 * well, we use GPIO to control nCSx pins on all controllers, updating
74 * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
75 * support active-high chipselects despite the controller's belief that
76 * only active-low devices/systems exists.
78 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
79 * right when driven with GPIO. ("Mode Fault does not allow more than one
80 * Master on Chip Select 0.") No workaround exists for that ... so for
81 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
82 * and (c) will trigger that first erratum in some cases.
85 static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
87 unsigned gpio = (unsigned) spi->controller_data;
88 unsigned active = spi->mode & SPI_CS_HIGH;
91 mr = spi_readl(as, MR);
92 mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
94 dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
95 gpio, active ? " (high)" : "",
98 if (!(cpu_is_at91rm9200() && spi->chip_select == 0))
99 gpio_set_value(gpio, active);
100 spi_writel(as, MR, mr);
103 static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
105 unsigned gpio = (unsigned) spi->controller_data;
106 unsigned active = spi->mode & SPI_CS_HIGH;
109 /* only deactivate *this* device; sometimes transfers to
110 * another device may be active when this routine is called.
112 mr = spi_readl(as, MR);
113 if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
114 mr = SPI_BFINS(PCS, 0xf, mr);
115 spi_writel(as, MR, mr);
118 dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
119 gpio, active ? " (low)" : "",
122 if (!(cpu_is_at91rm9200() && spi->chip_select == 0))
123 gpio_set_value(gpio, !active);
126 static inline int atmel_spi_xfer_is_last(struct spi_message *msg,
127 struct spi_transfer *xfer)
129 return msg->transfers.prev == &xfer->transfer_list;
132 static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer)
134 return xfer->delay_usecs == 0 && !xfer->cs_change;
137 static void atmel_spi_next_xfer_data(struct spi_master *master,
138 struct spi_transfer *xfer,
143 struct atmel_spi *as = spi_master_get_devdata(master);
146 /* use scratch buffer only when rx or tx data is unspecified */
148 *rx_dma = xfer->rx_dma + xfer->len - len;
150 *rx_dma = as->buffer_dma;
151 if (len > BUFFER_SIZE)
155 *tx_dma = xfer->tx_dma + xfer->len - len;
157 *tx_dma = as->buffer_dma;
158 if (len > BUFFER_SIZE)
160 memset(as->buffer, 0, len);
161 dma_sync_single_for_device(&as->pdev->dev,
162 as->buffer_dma, len, DMA_TO_DEVICE);
169 * Submit next transfer for DMA.
170 * lock is held, spi irq is blocked
172 static void atmel_spi_next_xfer(struct spi_master *master,
173 struct spi_message *msg)
175 struct atmel_spi *as = spi_master_get_devdata(master);
176 struct spi_transfer *xfer;
177 u32 len, remaining, total;
178 dma_addr_t tx_dma, rx_dma;
180 if (!as->current_transfer)
181 xfer = list_entry(msg->transfers.next,
182 struct spi_transfer, transfer_list);
183 else if (!as->next_transfer)
184 xfer = list_entry(as->current_transfer->transfer_list.next,
185 struct spi_transfer, transfer_list);
191 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
192 remaining = xfer->len - len;
194 spi_writel(as, RPR, rx_dma);
195 spi_writel(as, TPR, tx_dma);
197 if (msg->spi->bits_per_word > 8)
199 spi_writel(as, RCR, len);
200 spi_writel(as, TCR, len);
202 dev_dbg(&msg->spi->dev,
203 " start xfer %p: len %u tx %p/%08x rx %p/%08x\n",
204 xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
205 xfer->rx_buf, xfer->rx_dma);
207 xfer = as->next_transfer;
208 remaining = as->next_remaining_bytes;
211 as->current_transfer = xfer;
212 as->current_remaining_bytes = remaining;
216 else if (!atmel_spi_xfer_is_last(msg, xfer)
217 && atmel_spi_xfer_can_be_chained(xfer)) {
218 xfer = list_entry(xfer->transfer_list.next,
219 struct spi_transfer, transfer_list);
224 as->next_transfer = xfer;
228 atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len);
229 as->next_remaining_bytes = total - len;
231 spi_writel(as, RNPR, rx_dma);
232 spi_writel(as, TNPR, tx_dma);
234 if (msg->spi->bits_per_word > 8)
236 spi_writel(as, RNCR, len);
237 spi_writel(as, TNCR, len);
239 dev_dbg(&msg->spi->dev,
240 " next xfer %p: len %u tx %p/%08x rx %p/%08x\n",
241 xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
242 xfer->rx_buf, xfer->rx_dma);
244 spi_writel(as, RNCR, 0);
245 spi_writel(as, TNCR, 0);
248 /* REVISIT: We're waiting for ENDRX before we start the next
249 * transfer because we need to handle some difficult timing
250 * issues otherwise. If we wait for ENDTX in one transfer and
251 * then starts waiting for ENDRX in the next, it's difficult
252 * to tell the difference between the ENDRX interrupt we're
253 * actually waiting for and the ENDRX interrupt of the
256 * It should be doable, though. Just not now...
258 spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
259 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
262 static void atmel_spi_next_message(struct spi_master *master)
264 struct atmel_spi *as = spi_master_get_devdata(master);
265 struct spi_message *msg;
266 struct spi_device *spi;
268 BUG_ON(as->current_transfer);
270 msg = list_entry(as->queue.next, struct spi_message, queue);
273 dev_dbg(master->dev.parent, "start message %p for %s\n",
274 msg, spi->dev.bus_id);
276 /* select chip if it's not still active */
278 if (as->stay != spi) {
279 cs_deactivate(as, as->stay);
280 cs_activate(as, spi);
284 cs_activate(as, spi);
286 atmel_spi_next_xfer(master, msg);
290 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
291 * - The buffer is either valid for CPU access, else NULL
292 * - If the buffer is valid, so is its DMA addresss
294 * This driver manages the dma addresss unless message->is_dma_mapped.
297 atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
299 struct device *dev = &as->pdev->dev;
301 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
303 xfer->tx_dma = dma_map_single(dev,
304 (void *) xfer->tx_buf, xfer->len,
306 if (dma_mapping_error(xfer->tx_dma))
310 xfer->rx_dma = dma_map_single(dev,
311 xfer->rx_buf, xfer->len,
313 if (dma_mapping_error(xfer->rx_dma)) {
315 dma_unmap_single(dev,
316 xfer->tx_dma, xfer->len,
324 static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
325 struct spi_transfer *xfer)
327 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
328 dma_unmap_single(master->dev.parent, xfer->tx_dma,
329 xfer->len, DMA_TO_DEVICE);
330 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
331 dma_unmap_single(master->dev.parent, xfer->rx_dma,
332 xfer->len, DMA_FROM_DEVICE);
336 atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
337 struct spi_message *msg, int status, int stay)
339 if (!stay || status < 0)
340 cs_deactivate(as, msg->spi);
344 list_del(&msg->queue);
345 msg->status = status;
347 dev_dbg(master->dev.parent,
348 "xfer complete: %u bytes transferred\n",
351 spin_unlock(&as->lock);
352 msg->complete(msg->context);
353 spin_lock(&as->lock);
355 as->current_transfer = NULL;
356 as->next_transfer = NULL;
358 /* continue if needed */
359 if (list_empty(&as->queue) || as->stopping)
360 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
362 atmel_spi_next_message(master);
366 atmel_spi_interrupt(int irq, void *dev_id)
368 struct spi_master *master = dev_id;
369 struct atmel_spi *as = spi_master_get_devdata(master);
370 struct spi_message *msg;
371 struct spi_transfer *xfer;
372 u32 status, pending, imr;
375 spin_lock(&as->lock);
377 xfer = as->current_transfer;
378 msg = list_entry(as->queue.next, struct spi_message, queue);
380 imr = spi_readl(as, IMR);
381 status = spi_readl(as, SR);
382 pending = status & imr;
384 if (pending & SPI_BIT(OVRES)) {
389 spi_writel(as, IDR, (SPI_BIT(ENDTX) | SPI_BIT(ENDRX)
393 * When we get an overrun, we disregard the current
394 * transfer. Data will not be copied back from any
395 * bounce buffer and msg->actual_len will not be
396 * updated with the last xfer.
398 * We will also not process any remaning transfers in
401 * First, stop the transfer and unmap the DMA buffers.
403 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
404 if (!msg->is_dma_mapped)
405 atmel_spi_dma_unmap_xfer(master, xfer);
407 /* REVISIT: udelay in irq is unfriendly */
408 if (xfer->delay_usecs)
409 udelay(xfer->delay_usecs);
411 dev_warn(master->dev.parent, "fifo overrun (%u/%u remaining)\n",
412 spi_readl(as, TCR), spi_readl(as, RCR));
415 * Clean up DMA registers and make sure the data
416 * registers are empty.
418 spi_writel(as, RNCR, 0);
419 spi_writel(as, TNCR, 0);
420 spi_writel(as, RCR, 0);
421 spi_writel(as, TCR, 0);
422 for (timeout = 1000; timeout; timeout--)
423 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
426 dev_warn(master->dev.parent,
427 "timeout waiting for TXEMPTY");
428 while (spi_readl(as, SR) & SPI_BIT(RDRF))
431 /* Clear any overrun happening while cleaning up */
434 atmel_spi_msg_done(master, as, msg, -EIO, 0);
435 } else if (pending & SPI_BIT(ENDRX)) {
438 spi_writel(as, IDR, pending);
440 if (as->current_remaining_bytes == 0) {
441 msg->actual_length += xfer->len;
443 if (!msg->is_dma_mapped)
444 atmel_spi_dma_unmap_xfer(master, xfer);
446 /* REVISIT: udelay in irq is unfriendly */
447 if (xfer->delay_usecs)
448 udelay(xfer->delay_usecs);
450 if (atmel_spi_xfer_is_last(msg, xfer)) {
451 /* report completed message */
452 atmel_spi_msg_done(master, as, msg, 0,
455 if (xfer->cs_change) {
456 cs_deactivate(as, msg->spi);
458 cs_activate(as, msg->spi);
462 * Not done yet. Submit the next transfer.
464 * FIXME handle protocol options for xfer
466 atmel_spi_next_xfer(master, msg);
470 * Keep going, we still have data to send in
471 * the current transfer.
473 atmel_spi_next_xfer(master, msg);
477 spin_unlock(&as->lock);
482 /* the spi->mode bits understood by this driver: */
483 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
485 static int atmel_spi_setup(struct spi_device *spi)
487 struct atmel_spi *as;
489 unsigned int bits = spi->bits_per_word;
490 unsigned long bus_hz, sck_hz;
491 unsigned int npcs_pin;
494 as = spi_master_get_devdata(spi->master);
499 if (spi->chip_select > spi->master->num_chipselect) {
501 "setup: invalid chipselect %u (%u defined)\n",
502 spi->chip_select, spi->master->num_chipselect);
508 if (bits < 8 || bits > 16) {
510 "setup: invalid bits_per_word %u (8 to 16)\n",
515 if (spi->mode & ~MODEBITS) {
516 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
517 spi->mode & ~MODEBITS);
521 /* see notes above re chipselect */
522 if (cpu_is_at91rm9200()
523 && spi->chip_select == 0
524 && (spi->mode & SPI_CS_HIGH)) {
525 dev_dbg(&spi->dev, "setup: can't be active-high\n");
529 /* speed zero convention is used by some upper layers */
530 bus_hz = clk_get_rate(as->clk);
531 if (spi->max_speed_hz) {
532 /* assume div32/fdiv/mbz == 0 */
535 scbr = ((bus_hz + spi->max_speed_hz - 1)
536 / spi->max_speed_hz);
537 if (scbr >= (1 << SPI_SCBR_SIZE)) {
539 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
540 spi->max_speed_hz, scbr, bus_hz/255);
545 sck_hz = bus_hz / scbr;
547 csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
548 if (spi->mode & SPI_CPOL)
549 csr |= SPI_BIT(CPOL);
550 if (!(spi->mode & SPI_CPHA))
551 csr |= SPI_BIT(NCPHA);
553 /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs.
555 * DLYBCT would add delays between words, slowing down transfers.
556 * It could potentially be useful to cope with DMA bottlenecks, but
557 * in those cases it's probably best to just use a lower bitrate.
559 csr |= SPI_BF(DLYBS, 0);
560 csr |= SPI_BF(DLYBCT, 0);
562 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
563 npcs_pin = (unsigned int)spi->controller_data;
564 if (!spi->controller_state) {
565 ret = gpio_request(npcs_pin, spi->dev.bus_id);
568 spi->controller_state = (void *)npcs_pin;
569 gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
573 spin_lock_irqsave(&as->lock, flags);
576 cs_deactivate(as, spi);
577 spin_unlock_irqrestore(&as->lock, flags);
581 "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
582 sck_hz, bits, spi->mode, spi->chip_select, csr);
584 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
589 static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
591 struct atmel_spi *as;
592 struct spi_transfer *xfer;
594 struct device *controller = spi->master->dev.parent;
596 as = spi_master_get_devdata(spi->master);
598 dev_dbg(controller, "new message %p submitted for %s\n",
599 msg, spi->dev.bus_id);
601 if (unlikely(list_empty(&msg->transfers)
602 || !spi->max_speed_hz))
608 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
609 if (!(xfer->tx_buf || xfer->rx_buf)) {
610 dev_dbg(&spi->dev, "missing rx or tx buf\n");
614 /* FIXME implement these protocol options!! */
615 if (xfer->bits_per_word || xfer->speed_hz) {
616 dev_dbg(&spi->dev, "no protocol options yet\n");
621 * DMA map early, for performance (empties dcache ASAP) and
622 * better fault reporting. This is a DMA-only driver.
624 * NOTE that if dma_unmap_single() ever starts to do work on
625 * platforms supported by this driver, we would need to clean
626 * up mappings for previously-mapped transfers.
628 if (!msg->is_dma_mapped) {
629 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
635 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
637 " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
639 xfer->tx_buf, xfer->tx_dma,
640 xfer->rx_buf, xfer->rx_dma);
644 msg->status = -EINPROGRESS;
645 msg->actual_length = 0;
647 spin_lock_irqsave(&as->lock, flags);
648 list_add_tail(&msg->queue, &as->queue);
649 if (!as->current_transfer)
650 atmel_spi_next_message(spi->master);
651 spin_unlock_irqrestore(&as->lock, flags);
656 static void atmel_spi_cleanup(struct spi_device *spi)
658 struct atmel_spi *as = spi_master_get_devdata(spi->master);
659 unsigned gpio = (unsigned) spi->controller_data;
662 if (!spi->controller_state)
665 spin_lock_irqsave(&as->lock, flags);
666 if (as->stay == spi) {
668 cs_deactivate(as, spi);
670 spin_unlock_irqrestore(&as->lock, flags);
675 /*-------------------------------------------------------------------------*/
677 static int __init atmel_spi_probe(struct platform_device *pdev)
679 struct resource *regs;
683 struct spi_master *master;
684 struct atmel_spi *as;
686 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
690 irq = platform_get_irq(pdev, 0);
694 clk = clk_get(&pdev->dev, "spi_clk");
698 /* setup spi core then atmel-specific driver state */
700 master = spi_alloc_master(&pdev->dev, sizeof *as);
704 master->bus_num = pdev->id;
705 master->num_chipselect = 4;
706 master->setup = atmel_spi_setup;
707 master->transfer = atmel_spi_transfer;
708 master->cleanup = atmel_spi_cleanup;
709 platform_set_drvdata(pdev, master);
711 as = spi_master_get_devdata(master);
714 * Scratch buffer is used for throwaway rx and tx data.
715 * It's coherent to minimize dcache pollution.
717 as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
718 &as->buffer_dma, GFP_KERNEL);
722 spin_lock_init(&as->lock);
723 INIT_LIST_HEAD(&as->queue);
725 as->regs = ioremap(regs->start, (regs->end - regs->start) + 1);
727 goto out_free_buffer;
730 if (!cpu_is_at91rm9200())
733 ret = request_irq(irq, atmel_spi_interrupt, 0,
734 pdev->dev.bus_id, master);
738 /* Initialize the hardware */
740 spi_writel(as, CR, SPI_BIT(SWRST));
741 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
742 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
743 spi_writel(as, CR, SPI_BIT(SPIEN));
746 dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
747 (unsigned long)regs->start, irq);
749 ret = spi_register_master(master);
756 spi_writel(as, CR, SPI_BIT(SWRST));
758 free_irq(irq, master);
762 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
766 spi_master_put(master);
770 static int __exit atmel_spi_remove(struct platform_device *pdev)
772 struct spi_master *master = platform_get_drvdata(pdev);
773 struct atmel_spi *as = spi_master_get_devdata(master);
774 struct spi_message *msg;
776 /* reset the hardware and block queue progress */
777 spin_lock_irq(&as->lock);
779 spi_writel(as, CR, SPI_BIT(SWRST));
781 spin_unlock_irq(&as->lock);
783 /* Terminate remaining queued transfers */
784 list_for_each_entry(msg, &as->queue, queue) {
785 /* REVISIT unmapping the dma is a NOP on ARM and AVR32
786 * but we shouldn't depend on that...
788 msg->status = -ESHUTDOWN;
789 msg->complete(msg->context);
792 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
795 clk_disable(as->clk);
797 free_irq(as->irq, master);
800 spi_unregister_master(master);
807 static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
809 struct spi_master *master = platform_get_drvdata(pdev);
810 struct atmel_spi *as = spi_master_get_devdata(master);
812 clk_disable(as->clk);
816 static int atmel_spi_resume(struct platform_device *pdev)
818 struct spi_master *master = platform_get_drvdata(pdev);
819 struct atmel_spi *as = spi_master_get_devdata(master);
826 #define atmel_spi_suspend NULL
827 #define atmel_spi_resume NULL
831 static struct platform_driver atmel_spi_driver = {
834 .owner = THIS_MODULE,
836 .suspend = atmel_spi_suspend,
837 .resume = atmel_spi_resume,
838 .remove = __exit_p(atmel_spi_remove),
841 static int __init atmel_spi_init(void)
843 return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe);
845 module_init(atmel_spi_init);
847 static void __exit atmel_spi_exit(void)
849 platform_driver_unregister(&atmel_spi_driver);
851 module_exit(atmel_spi_exit);
853 MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
854 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
855 MODULE_LICENSE("GPL");