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 remaining_bytes;
57 dma_addr_t buffer_dma;
60 #define BUFFER_SIZE PAGE_SIZE
61 #define INVALID_DMA_ADDRESS 0xffffffff
64 * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby
65 * they assume that spi slave device state will not change on deselect, so
66 * that automagic deselection is OK. ("NPCSx rises if no data is to be
67 * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer
68 * controllers have CSAAT and friends.
70 * Since the CSAAT functionality is a bit weird on newer controllers as
71 * well, we use GPIO to control nCSx pins on all controllers, updating
72 * MR.PCS to avoid confusing the controller. Using GPIOs also lets us
73 * support active-high chipselects despite the controller's belief that
74 * only active-low devices/systems exists.
76 * However, at91rm9200 has a second erratum whereby nCS0 doesn't work
77 * right when driven with GPIO. ("Mode Fault does not allow more than one
78 * Master on Chip Select 0.") No workaround exists for that ... so for
79 * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH,
80 * and (c) will trigger that first erratum in some cases.
83 static void cs_activate(struct atmel_spi *as, struct spi_device *spi)
85 unsigned gpio = (unsigned) spi->controller_data;
86 unsigned active = spi->mode & SPI_CS_HIGH;
89 mr = spi_readl(as, MR);
90 mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr);
92 dev_dbg(&spi->dev, "activate %u%s, mr %08x\n",
93 gpio, active ? " (high)" : "",
96 if (!(cpu_is_at91rm9200() && spi->chip_select == 0))
97 gpio_set_value(gpio, active);
98 spi_writel(as, MR, mr);
101 static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi)
103 unsigned gpio = (unsigned) spi->controller_data;
104 unsigned active = spi->mode & SPI_CS_HIGH;
107 /* only deactivate *this* device; sometimes transfers to
108 * another device may be active when this routine is called.
110 mr = spi_readl(as, MR);
111 if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) {
112 mr = SPI_BFINS(PCS, 0xf, mr);
113 spi_writel(as, MR, mr);
116 dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n",
117 gpio, active ? " (low)" : "",
120 if (!(cpu_is_at91rm9200() && spi->chip_select == 0))
121 gpio_set_value(gpio, !active);
125 * Submit next transfer for DMA.
126 * lock is held, spi irq is blocked
128 static void atmel_spi_next_xfer(struct spi_master *master,
129 struct spi_message *msg)
131 struct atmel_spi *as = spi_master_get_devdata(master);
132 struct spi_transfer *xfer;
134 dma_addr_t tx_dma, rx_dma;
136 xfer = as->current_transfer;
137 if (!xfer || as->remaining_bytes == 0) {
139 xfer = list_entry(xfer->transfer_list.next,
140 struct spi_transfer, transfer_list);
142 xfer = list_entry(msg->transfers.next,
143 struct spi_transfer, transfer_list);
144 as->remaining_bytes = xfer->len;
145 as->current_transfer = xfer;
148 len = as->remaining_bytes;
150 tx_dma = xfer->tx_dma + xfer->len - len;
151 rx_dma = xfer->rx_dma + xfer->len - len;
153 /* use scratch buffer only when rx or tx data is unspecified */
155 rx_dma = as->buffer_dma;
156 if (len > BUFFER_SIZE)
160 tx_dma = as->buffer_dma;
161 if (len > BUFFER_SIZE)
163 memset(as->buffer, 0, len);
164 dma_sync_single_for_device(&as->pdev->dev,
165 as->buffer_dma, len, DMA_TO_DEVICE);
168 spi_writel(as, RPR, rx_dma);
169 spi_writel(as, TPR, tx_dma);
171 as->remaining_bytes -= len;
172 if (msg->spi->bits_per_word > 8)
175 /* REVISIT: when xfer->delay_usecs == 0, the PDC "next transfer"
176 * mechanism might help avoid the IRQ latency between transfers
177 * (and improve the nCS0 errata handling on at91rm9200 chips)
179 * We're also waiting for ENDRX before we start the next
180 * transfer because we need to handle some difficult timing
181 * issues otherwise. If we wait for ENDTX in one transfer and
182 * then starts waiting for ENDRX in the next, it's difficult
183 * to tell the difference between the ENDRX interrupt we're
184 * actually waiting for and the ENDRX interrupt of the
187 * It should be doable, though. Just not now...
189 spi_writel(as, TNCR, 0);
190 spi_writel(as, RNCR, 0);
191 spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
193 dev_dbg(&msg->spi->dev,
194 " start xfer %p: len %u tx %p/%08x rx %p/%08x imr %03x\n",
195 xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
196 xfer->rx_buf, xfer->rx_dma, spi_readl(as, IMR));
198 spi_writel(as, RCR, len);
199 spi_writel(as, TCR, len);
200 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
203 static void atmel_spi_next_message(struct spi_master *master)
205 struct atmel_spi *as = spi_master_get_devdata(master);
206 struct spi_message *msg;
207 struct spi_device *spi;
209 BUG_ON(as->current_transfer);
211 msg = list_entry(as->queue.next, struct spi_message, queue);
214 dev_dbg(master->dev.parent, "start message %p for %s\n",
215 msg, spi->dev.bus_id);
217 /* select chip if it's not still active */
219 if (as->stay != spi) {
220 cs_deactivate(as, as->stay);
221 cs_activate(as, spi);
225 cs_activate(as, spi);
227 atmel_spi_next_xfer(master, msg);
231 * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma:
232 * - The buffer is either valid for CPU access, else NULL
233 * - If the buffer is valid, so is its DMA addresss
235 * This driver manages the dma addresss unless message->is_dma_mapped.
238 atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
240 struct device *dev = &as->pdev->dev;
242 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
244 xfer->tx_dma = dma_map_single(dev,
245 (void *) xfer->tx_buf, xfer->len,
247 if (dma_mapping_error(xfer->tx_dma))
251 xfer->rx_dma = dma_map_single(dev,
252 xfer->rx_buf, xfer->len,
254 if (dma_mapping_error(xfer->rx_dma)) {
256 dma_unmap_single(dev,
257 xfer->tx_dma, xfer->len,
265 static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
266 struct spi_transfer *xfer)
268 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
269 dma_unmap_single(master->dev.parent, xfer->tx_dma,
270 xfer->len, DMA_TO_DEVICE);
271 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
272 dma_unmap_single(master->dev.parent, xfer->rx_dma,
273 xfer->len, DMA_FROM_DEVICE);
277 atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
278 struct spi_message *msg, int status, int stay)
280 if (!stay || status < 0)
281 cs_deactivate(as, msg->spi);
285 list_del(&msg->queue);
286 msg->status = status;
288 dev_dbg(master->dev.parent,
289 "xfer complete: %u bytes transferred\n",
292 spin_unlock(&as->lock);
293 msg->complete(msg->context);
294 spin_lock(&as->lock);
296 as->current_transfer = NULL;
298 /* continue if needed */
299 if (list_empty(&as->queue) || as->stopping)
300 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
302 atmel_spi_next_message(master);
306 atmel_spi_interrupt(int irq, void *dev_id)
308 struct spi_master *master = dev_id;
309 struct atmel_spi *as = spi_master_get_devdata(master);
310 struct spi_message *msg;
311 struct spi_transfer *xfer;
312 u32 status, pending, imr;
315 spin_lock(&as->lock);
317 xfer = as->current_transfer;
318 msg = list_entry(as->queue.next, struct spi_message, queue);
320 imr = spi_readl(as, IMR);
321 status = spi_readl(as, SR);
322 pending = status & imr;
324 if (pending & SPI_BIT(OVRES)) {
329 spi_writel(as, IDR, (SPI_BIT(ENDTX) | SPI_BIT(ENDRX)
333 * When we get an overrun, we disregard the current
334 * transfer. Data will not be copied back from any
335 * bounce buffer and msg->actual_len will not be
336 * updated with the last xfer.
338 * We will also not process any remaning transfers in
341 * First, stop the transfer and unmap the DMA buffers.
343 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
344 if (!msg->is_dma_mapped)
345 atmel_spi_dma_unmap_xfer(master, xfer);
347 /* REVISIT: udelay in irq is unfriendly */
348 if (xfer->delay_usecs)
349 udelay(xfer->delay_usecs);
351 dev_warn(master->dev.parent, "fifo overrun (%u/%u remaining)\n",
352 spi_readl(as, TCR), spi_readl(as, RCR));
355 * Clean up DMA registers and make sure the data
356 * registers are empty.
358 spi_writel(as, RNCR, 0);
359 spi_writel(as, TNCR, 0);
360 spi_writel(as, RCR, 0);
361 spi_writel(as, TCR, 0);
362 for (timeout = 1000; timeout; timeout--)
363 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
366 dev_warn(master->dev.parent,
367 "timeout waiting for TXEMPTY");
368 while (spi_readl(as, SR) & SPI_BIT(RDRF))
371 /* Clear any overrun happening while cleaning up */
374 atmel_spi_msg_done(master, as, msg, -EIO, 0);
375 } else if (pending & SPI_BIT(ENDRX)) {
378 spi_writel(as, IDR, pending);
380 if (as->remaining_bytes == 0) {
381 msg->actual_length += xfer->len;
383 if (!msg->is_dma_mapped)
384 atmel_spi_dma_unmap_xfer(master, xfer);
386 /* REVISIT: udelay in irq is unfriendly */
387 if (xfer->delay_usecs)
388 udelay(xfer->delay_usecs);
390 if (msg->transfers.prev == &xfer->transfer_list) {
391 /* report completed message */
392 atmel_spi_msg_done(master, as, msg, 0,
395 if (xfer->cs_change) {
396 cs_deactivate(as, msg->spi);
398 cs_activate(as, msg->spi);
402 * Not done yet. Submit the next transfer.
404 * FIXME handle protocol options for xfer
406 atmel_spi_next_xfer(master, msg);
410 * Keep going, we still have data to send in
411 * the current transfer.
413 atmel_spi_next_xfer(master, msg);
417 spin_unlock(&as->lock);
422 /* the spi->mode bits understood by this driver: */
423 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
425 static int atmel_spi_setup(struct spi_device *spi)
427 struct atmel_spi *as;
429 unsigned int bits = spi->bits_per_word;
430 unsigned long bus_hz, sck_hz;
431 unsigned int npcs_pin;
434 as = spi_master_get_devdata(spi->master);
439 if (spi->chip_select > spi->master->num_chipselect) {
441 "setup: invalid chipselect %u (%u defined)\n",
442 spi->chip_select, spi->master->num_chipselect);
448 if (bits < 8 || bits > 16) {
450 "setup: invalid bits_per_word %u (8 to 16)\n",
455 if (spi->mode & ~MODEBITS) {
456 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
457 spi->mode & ~MODEBITS);
461 /* see notes above re chipselect */
462 if (cpu_is_at91rm9200()
463 && spi->chip_select == 0
464 && (spi->mode & SPI_CS_HIGH)) {
465 dev_dbg(&spi->dev, "setup: can't be active-high\n");
469 /* speed zero convention is used by some upper layers */
470 bus_hz = clk_get_rate(as->clk);
471 if (spi->max_speed_hz) {
472 /* assume div32/fdiv/mbz == 0 */
475 scbr = ((bus_hz + spi->max_speed_hz - 1)
476 / spi->max_speed_hz);
477 if (scbr >= (1 << SPI_SCBR_SIZE)) {
479 "setup: %d Hz too slow, scbr %u; min %ld Hz\n",
480 spi->max_speed_hz, scbr, bus_hz/255);
485 sck_hz = bus_hz / scbr;
487 csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
488 if (spi->mode & SPI_CPOL)
489 csr |= SPI_BIT(CPOL);
490 if (!(spi->mode & SPI_CPHA))
491 csr |= SPI_BIT(NCPHA);
493 /* TODO: DLYBS and DLYBCT */
494 csr |= SPI_BF(DLYBS, 10);
495 csr |= SPI_BF(DLYBCT, 10);
497 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
498 npcs_pin = (unsigned int)spi->controller_data;
499 if (!spi->controller_state) {
500 ret = gpio_request(npcs_pin, spi->dev.bus_id);
503 spi->controller_state = (void *)npcs_pin;
504 gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH));
508 spin_lock_irqsave(&as->lock, flags);
511 cs_deactivate(as, spi);
512 spin_unlock_irqrestore(&as->lock, flags);
516 "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
517 sck_hz, bits, spi->mode, spi->chip_select, csr);
519 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
524 static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
526 struct atmel_spi *as;
527 struct spi_transfer *xfer;
529 struct device *controller = spi->master->dev.parent;
531 as = spi_master_get_devdata(spi->master);
533 dev_dbg(controller, "new message %p submitted for %s\n",
534 msg, spi->dev.bus_id);
536 if (unlikely(list_empty(&msg->transfers)
537 || !spi->max_speed_hz))
543 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
544 if (!(xfer->tx_buf || xfer->rx_buf)) {
545 dev_dbg(&spi->dev, "missing rx or tx buf\n");
549 /* FIXME implement these protocol options!! */
550 if (xfer->bits_per_word || xfer->speed_hz) {
551 dev_dbg(&spi->dev, "no protocol options yet\n");
556 * DMA map early, for performance (empties dcache ASAP) and
557 * better fault reporting. This is a DMA-only driver.
559 * NOTE that if dma_unmap_single() ever starts to do work on
560 * platforms supported by this driver, we would need to clean
561 * up mappings for previously-mapped transfers.
563 if (!msg->is_dma_mapped) {
564 if (atmel_spi_dma_map_xfer(as, xfer) < 0)
570 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
572 " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
574 xfer->tx_buf, xfer->tx_dma,
575 xfer->rx_buf, xfer->rx_dma);
579 msg->status = -EINPROGRESS;
580 msg->actual_length = 0;
582 spin_lock_irqsave(&as->lock, flags);
583 list_add_tail(&msg->queue, &as->queue);
584 if (!as->current_transfer)
585 atmel_spi_next_message(spi->master);
586 spin_unlock_irqrestore(&as->lock, flags);
591 static void atmel_spi_cleanup(struct spi_device *spi)
593 struct atmel_spi *as = spi_master_get_devdata(spi->master);
594 unsigned gpio = (unsigned) spi->controller_data;
597 if (!spi->controller_state)
600 spin_lock_irqsave(&as->lock, flags);
601 if (as->stay == spi) {
603 cs_deactivate(as, spi);
605 spin_unlock_irqrestore(&as->lock, flags);
610 /*-------------------------------------------------------------------------*/
612 static int __init atmel_spi_probe(struct platform_device *pdev)
614 struct resource *regs;
618 struct spi_master *master;
619 struct atmel_spi *as;
621 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
625 irq = platform_get_irq(pdev, 0);
629 clk = clk_get(&pdev->dev, "spi_clk");
633 /* setup spi core then atmel-specific driver state */
635 master = spi_alloc_master(&pdev->dev, sizeof *as);
639 master->bus_num = pdev->id;
640 master->num_chipselect = 4;
641 master->setup = atmel_spi_setup;
642 master->transfer = atmel_spi_transfer;
643 master->cleanup = atmel_spi_cleanup;
644 platform_set_drvdata(pdev, master);
646 as = spi_master_get_devdata(master);
649 * Scratch buffer is used for throwaway rx and tx data.
650 * It's coherent to minimize dcache pollution.
652 as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
653 &as->buffer_dma, GFP_KERNEL);
657 spin_lock_init(&as->lock);
658 INIT_LIST_HEAD(&as->queue);
660 as->regs = ioremap(regs->start, (regs->end - regs->start) + 1);
662 goto out_free_buffer;
665 if (!cpu_is_at91rm9200())
668 ret = request_irq(irq, atmel_spi_interrupt, 0,
669 pdev->dev.bus_id, master);
673 /* Initialize the hardware */
675 spi_writel(as, CR, SPI_BIT(SWRST));
676 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
677 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
678 spi_writel(as, CR, SPI_BIT(SPIEN));
681 dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
682 (unsigned long)regs->start, irq);
684 ret = spi_register_master(master);
691 spi_writel(as, CR, SPI_BIT(SWRST));
693 free_irq(irq, master);
697 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
701 spi_master_put(master);
705 static int __exit atmel_spi_remove(struct platform_device *pdev)
707 struct spi_master *master = platform_get_drvdata(pdev);
708 struct atmel_spi *as = spi_master_get_devdata(master);
709 struct spi_message *msg;
711 /* reset the hardware and block queue progress */
712 spin_lock_irq(&as->lock);
714 spi_writel(as, CR, SPI_BIT(SWRST));
716 spin_unlock_irq(&as->lock);
718 /* Terminate remaining queued transfers */
719 list_for_each_entry(msg, &as->queue, queue) {
720 /* REVISIT unmapping the dma is a NOP on ARM and AVR32
721 * but we shouldn't depend on that...
723 msg->status = -ESHUTDOWN;
724 msg->complete(msg->context);
727 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
730 clk_disable(as->clk);
732 free_irq(as->irq, master);
735 spi_unregister_master(master);
742 static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
744 struct spi_master *master = platform_get_drvdata(pdev);
745 struct atmel_spi *as = spi_master_get_devdata(master);
747 clk_disable(as->clk);
751 static int atmel_spi_resume(struct platform_device *pdev)
753 struct spi_master *master = platform_get_drvdata(pdev);
754 struct atmel_spi *as = spi_master_get_devdata(master);
761 #define atmel_spi_suspend NULL
762 #define atmel_spi_resume NULL
766 static struct platform_driver atmel_spi_driver = {
769 .owner = THIS_MODULE,
771 .suspend = atmel_spi_suspend,
772 .resume = atmel_spi_resume,
773 .remove = __exit_p(atmel_spi_remove),
776 static int __init atmel_spi_init(void)
778 return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe);
780 module_init(atmel_spi_init);
782 static void __exit atmel_spi_exit(void)
784 platform_driver_unregister(&atmel_spi_driver);
786 module_exit(atmel_spi_exit);
788 MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
789 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
790 MODULE_LICENSE("GPL");