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>
26 #ifdef CONFIG_ARCH_AT91
27 #include <asm/arch/cpu.h>
30 #include "atmel_spi.h"
33 * The core SPI transfer engine just talks to a register bank to set up
34 * DMA transfers; transfer queue progress is driven by IRQs. The clock
35 * framework provides the base clock, subdivided for each spi_device.
37 * Newer controllers, marked with "new_1" flag, have:
39 * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero)
40 * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs)
42 * - SPI_CSRx.SBCR allows faster clocking
50 struct platform_device *pdev;
54 struct list_head queue;
55 struct spi_transfer *current_transfer;
56 unsigned long 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. Not so! Workaround uses nCSx pins
69 * as GPIOs; or newer controllers have CSAAT and friends.
71 * Since the CSAAT functionality is a bit weird on newer controllers
72 * as well, we use GPIO to control nCSx pins on all controllers.
75 static inline void cs_activate(struct spi_device *spi)
77 unsigned gpio = (unsigned) spi->controller_data;
78 unsigned active = spi->mode & SPI_CS_HIGH;
80 dev_dbg(&spi->dev, "activate %u%s\n", gpio, active ? " (high)" : "");
81 gpio_set_value(gpio, active);
84 static inline void cs_deactivate(struct spi_device *spi)
86 unsigned gpio = (unsigned) spi->controller_data;
87 unsigned active = spi->mode & SPI_CS_HIGH;
89 dev_dbg(&spi->dev, "DEactivate %u%s\n", gpio, active ? " (low)" : "");
90 gpio_set_value(gpio, !active);
94 * Submit next transfer for DMA.
95 * lock is held, spi irq is blocked
97 static void atmel_spi_next_xfer(struct spi_master *master,
98 struct spi_message *msg)
100 struct atmel_spi *as = spi_master_get_devdata(master);
101 struct spi_transfer *xfer;
103 dma_addr_t tx_dma, rx_dma;
105 xfer = as->current_transfer;
106 if (!xfer || as->remaining_bytes == 0) {
108 xfer = list_entry(xfer->transfer_list.next,
109 struct spi_transfer, transfer_list);
111 xfer = list_entry(msg->transfers.next,
112 struct spi_transfer, transfer_list);
113 as->remaining_bytes = xfer->len;
114 as->current_transfer = xfer;
117 len = as->remaining_bytes;
119 tx_dma = xfer->tx_dma;
120 rx_dma = xfer->rx_dma;
122 /* use scratch buffer only when rx or tx data is unspecified */
123 if (rx_dma == INVALID_DMA_ADDRESS) {
124 rx_dma = as->buffer_dma;
125 if (len > BUFFER_SIZE)
128 if (tx_dma == INVALID_DMA_ADDRESS) {
129 tx_dma = as->buffer_dma;
130 if (len > BUFFER_SIZE)
132 memset(as->buffer, 0, len);
133 dma_sync_single_for_device(&as->pdev->dev,
134 as->buffer_dma, len, DMA_TO_DEVICE);
137 spi_writel(as, RPR, rx_dma);
138 spi_writel(as, TPR, tx_dma);
140 as->remaining_bytes -= len;
141 if (msg->spi->bits_per_word > 8)
144 /* REVISIT: when xfer->delay_usecs == 0, the PDC "next transfer"
145 * mechanism might help avoid the IRQ latency between transfers
147 * We're also waiting for ENDRX before we start the next
148 * transfer because we need to handle some difficult timing
149 * issues otherwise. If we wait for ENDTX in one transfer and
150 * then starts waiting for ENDRX in the next, it's difficult
151 * to tell the difference between the ENDRX interrupt we're
152 * actually waiting for and the ENDRX interrupt of the
155 * It should be doable, though. Just not now...
157 spi_writel(as, TNCR, 0);
158 spi_writel(as, RNCR, 0);
159 spi_writel(as, IER, SPI_BIT(ENDRX) | SPI_BIT(OVRES));
161 dev_dbg(&msg->spi->dev,
162 " start xfer %p: len %u tx %p/%08x rx %p/%08x imr %03x\n",
163 xfer, xfer->len, xfer->tx_buf, xfer->tx_dma,
164 xfer->rx_buf, xfer->rx_dma, spi_readl(as, IMR));
166 spi_writel(as, TCR, len);
167 spi_writel(as, RCR, len);
168 spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN));
171 static void atmel_spi_next_message(struct spi_master *master)
173 struct atmel_spi *as = spi_master_get_devdata(master);
174 struct spi_message *msg;
177 BUG_ON(as->current_transfer);
179 msg = list_entry(as->queue.next, struct spi_message, queue);
181 /* Select the chip */
182 mr = spi_readl(as, MR);
183 mr = SPI_BFINS(PCS, ~(1 << msg->spi->chip_select), mr);
184 spi_writel(as, MR, mr);
185 cs_activate(msg->spi);
187 atmel_spi_next_xfer(master, msg);
191 atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer)
193 xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS;
195 xfer->tx_dma = dma_map_single(&as->pdev->dev,
196 (void *) xfer->tx_buf, xfer->len,
199 xfer->rx_dma = dma_map_single(&as->pdev->dev,
200 xfer->rx_buf, xfer->len,
204 static void atmel_spi_dma_unmap_xfer(struct spi_master *master,
205 struct spi_transfer *xfer)
207 if (xfer->tx_dma != INVALID_DMA_ADDRESS)
208 dma_unmap_single(master->cdev.dev, xfer->tx_dma,
209 xfer->len, DMA_TO_DEVICE);
210 if (xfer->rx_dma != INVALID_DMA_ADDRESS)
211 dma_unmap_single(master->cdev.dev, xfer->rx_dma,
212 xfer->len, DMA_FROM_DEVICE);
216 atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as,
217 struct spi_message *msg, int status)
219 cs_deactivate(msg->spi);
220 list_del(&msg->queue);
221 msg->status = status;
223 dev_dbg(master->cdev.dev,
224 "xfer complete: %u bytes transferred\n",
227 spin_unlock(&as->lock);
228 msg->complete(msg->context);
229 spin_lock(&as->lock);
231 as->current_transfer = NULL;
233 /* continue if needed */
234 if (list_empty(&as->queue) || as->stopping)
235 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
237 atmel_spi_next_message(master);
241 atmel_spi_interrupt(int irq, void *dev_id)
243 struct spi_master *master = dev_id;
244 struct atmel_spi *as = spi_master_get_devdata(master);
245 struct spi_message *msg;
246 struct spi_transfer *xfer;
247 u32 status, pending, imr;
250 spin_lock(&as->lock);
252 xfer = as->current_transfer;
253 msg = list_entry(as->queue.next, struct spi_message, queue);
255 imr = spi_readl(as, IMR);
256 status = spi_readl(as, SR);
257 pending = status & imr;
259 if (pending & SPI_BIT(OVRES)) {
264 spi_writel(as, IDR, (SPI_BIT(ENDTX) | SPI_BIT(ENDRX)
268 * When we get an overrun, we disregard the current
269 * transfer. Data will not be copied back from any
270 * bounce buffer and msg->actual_len will not be
271 * updated with the last xfer.
273 * We will also not process any remaning transfers in
276 * First, stop the transfer and unmap the DMA buffers.
278 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
279 if (!msg->is_dma_mapped)
280 atmel_spi_dma_unmap_xfer(master, xfer);
282 /* REVISIT: udelay in irq is unfriendly */
283 if (xfer->delay_usecs)
284 udelay(xfer->delay_usecs);
286 dev_warn(master->cdev.dev, "fifo overrun (%u/%u remaining)\n",
287 spi_readl(as, TCR), spi_readl(as, RCR));
290 * Clean up DMA registers and make sure the data
291 * registers are empty.
293 spi_writel(as, RNCR, 0);
294 spi_writel(as, TNCR, 0);
295 spi_writel(as, RCR, 0);
296 spi_writel(as, TCR, 0);
297 for (timeout = 1000; timeout; timeout--)
298 if (spi_readl(as, SR) & SPI_BIT(TXEMPTY))
301 dev_warn(master->cdev.dev,
302 "timeout waiting for TXEMPTY");
303 while (spi_readl(as, SR) & SPI_BIT(RDRF))
306 /* Clear any overrun happening while cleaning up */
309 atmel_spi_msg_done(master, as, msg, -EIO);
310 } else if (pending & SPI_BIT(ENDRX)) {
313 spi_writel(as, IDR, pending);
315 if (as->remaining_bytes == 0) {
316 msg->actual_length += xfer->len;
318 if (!msg->is_dma_mapped)
319 atmel_spi_dma_unmap_xfer(master, xfer);
321 /* REVISIT: udelay in irq is unfriendly */
322 if (xfer->delay_usecs)
323 udelay(xfer->delay_usecs);
325 if (msg->transfers.prev == &xfer->transfer_list) {
326 /* report completed message */
327 atmel_spi_msg_done(master, as, msg, 0);
329 if (xfer->cs_change) {
330 cs_deactivate(msg->spi);
332 cs_activate(msg->spi);
336 * Not done yet. Submit the next transfer.
338 * FIXME handle protocol options for xfer
340 atmel_spi_next_xfer(master, msg);
344 * Keep going, we still have data to send in
345 * the current transfer.
347 atmel_spi_next_xfer(master, msg);
351 spin_unlock(&as->lock);
356 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
358 static int atmel_spi_setup(struct spi_device *spi)
360 struct atmel_spi *as;
362 unsigned int bits = spi->bits_per_word;
363 unsigned long bus_hz, sck_hz;
364 unsigned int npcs_pin;
367 as = spi_master_get_devdata(spi->master);
372 if (spi->chip_select > spi->master->num_chipselect) {
374 "setup: invalid chipselect %u (%u defined)\n",
375 spi->chip_select, spi->master->num_chipselect);
381 if (bits < 8 || bits > 16) {
383 "setup: invalid bits_per_word %u (8 to 16)\n",
388 if (spi->mode & ~MODEBITS) {
389 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
390 spi->mode & ~MODEBITS);
394 /* speed zero convention is used by some upper layers */
395 bus_hz = clk_get_rate(as->clk);
396 if (spi->max_speed_hz) {
397 /* assume div32/fdiv/mbz == 0 */
400 scbr = ((bus_hz + spi->max_speed_hz - 1)
401 / spi->max_speed_hz);
402 if (scbr >= (1 << SPI_SCBR_SIZE)) {
403 dev_dbg(&spi->dev, "setup: %d Hz too slow, scbr %u\n",
404 spi->max_speed_hz, scbr);
409 sck_hz = bus_hz / scbr;
411 csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8);
412 if (spi->mode & SPI_CPOL)
413 csr |= SPI_BIT(CPOL);
414 if (!(spi->mode & SPI_CPHA))
415 csr |= SPI_BIT(NCPHA);
417 /* TODO: DLYBS and DLYBCT */
418 csr |= SPI_BF(DLYBS, 10);
419 csr |= SPI_BF(DLYBCT, 10);
421 /* chipselect must have been muxed as GPIO (e.g. in board setup) */
422 npcs_pin = (unsigned int)spi->controller_data;
423 if (!spi->controller_state) {
424 ret = gpio_request(npcs_pin, "spi_npcs");
427 spi->controller_state = (void *)npcs_pin;
428 gpio_direction_output(npcs_pin);
432 "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n",
433 sck_hz, bits, spi->mode, spi->chip_select, csr);
435 spi_writel(as, CSR0 + 4 * spi->chip_select, csr);
440 static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg)
442 struct atmel_spi *as;
443 struct spi_transfer *xfer;
445 struct device *controller = spi->master->cdev.dev;
447 as = spi_master_get_devdata(spi->master);
449 dev_dbg(controller, "new message %p submitted for %s\n",
450 msg, spi->dev.bus_id);
452 if (unlikely(list_empty(&msg->transfers)
453 || !spi->max_speed_hz))
459 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
460 if (!(xfer->tx_buf || xfer->rx_buf)) {
461 dev_dbg(&spi->dev, "missing rx or tx buf\n");
465 /* FIXME implement these protocol options!! */
466 if (xfer->bits_per_word || xfer->speed_hz) {
467 dev_dbg(&spi->dev, "no protocol options yet\n");
472 /* scrub dcache "early" */
473 if (!msg->is_dma_mapped) {
474 list_for_each_entry(xfer, &msg->transfers, transfer_list)
475 atmel_spi_dma_map_xfer(as, xfer);
478 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
480 " xfer %p: len %u tx %p/%08x rx %p/%08x\n",
482 xfer->tx_buf, xfer->tx_dma,
483 xfer->rx_buf, xfer->rx_dma);
486 msg->status = -EINPROGRESS;
487 msg->actual_length = 0;
489 spin_lock_irqsave(&as->lock, flags);
490 list_add_tail(&msg->queue, &as->queue);
491 if (!as->current_transfer)
492 atmel_spi_next_message(spi->master);
493 spin_unlock_irqrestore(&as->lock, flags);
498 static void atmel_spi_cleanup(struct spi_device *spi)
500 if (spi->controller_state)
501 gpio_free((unsigned int)spi->controller_data);
504 /*-------------------------------------------------------------------------*/
506 static int __init atmel_spi_probe(struct platform_device *pdev)
508 struct resource *regs;
512 struct spi_master *master;
513 struct atmel_spi *as;
515 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
519 irq = platform_get_irq(pdev, 0);
523 clk = clk_get(&pdev->dev, "spi_clk");
527 /* setup spi core then atmel-specific driver state */
529 master = spi_alloc_master(&pdev->dev, sizeof *as);
533 master->bus_num = pdev->id;
534 master->num_chipselect = 4;
535 master->setup = atmel_spi_setup;
536 master->transfer = atmel_spi_transfer;
537 master->cleanup = atmel_spi_cleanup;
538 platform_set_drvdata(pdev, master);
540 as = spi_master_get_devdata(master);
542 as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE,
543 &as->buffer_dma, GFP_KERNEL);
547 spin_lock_init(&as->lock);
548 INIT_LIST_HEAD(&as->queue);
550 as->regs = ioremap(regs->start, (regs->end - regs->start) + 1);
552 goto out_free_buffer;
555 #ifdef CONFIG_ARCH_AT91
556 if (!cpu_is_at91rm9200())
560 ret = request_irq(irq, atmel_spi_interrupt, 0,
561 pdev->dev.bus_id, master);
565 /* Initialize the hardware */
567 spi_writel(as, CR, SPI_BIT(SWRST));
568 spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS));
569 spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS));
570 spi_writel(as, CR, SPI_BIT(SPIEN));
573 dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n",
574 (unsigned long)regs->start, irq);
576 ret = spi_register_master(master);
583 spi_writel(as, CR, SPI_BIT(SWRST));
585 free_irq(irq, master);
589 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
593 spi_master_put(master);
597 static int __exit atmel_spi_remove(struct platform_device *pdev)
599 struct spi_master *master = platform_get_drvdata(pdev);
600 struct atmel_spi *as = spi_master_get_devdata(master);
601 struct spi_message *msg;
603 /* reset the hardware and block queue progress */
604 spin_lock_irq(&as->lock);
606 spi_writel(as, CR, SPI_BIT(SWRST));
608 spin_unlock_irq(&as->lock);
610 /* Terminate remaining queued transfers */
611 list_for_each_entry(msg, &as->queue, queue) {
612 /* REVISIT unmapping the dma is a NOP on ARM and AVR32
613 * but we shouldn't depend on that...
615 msg->status = -ESHUTDOWN;
616 msg->complete(msg->context);
619 dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer,
622 clk_disable(as->clk);
624 free_irq(as->irq, master);
627 spi_unregister_master(master);
634 static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg)
636 struct spi_master *master = platform_get_drvdata(pdev);
637 struct atmel_spi *as = spi_master_get_devdata(master);
639 clk_disable(as->clk);
643 static int atmel_spi_resume(struct platform_device *pdev)
645 struct spi_master *master = platform_get_drvdata(pdev);
646 struct atmel_spi *as = spi_master_get_devdata(master);
653 #define atmel_spi_suspend NULL
654 #define atmel_spi_resume NULL
658 static struct platform_driver atmel_spi_driver = {
661 .owner = THIS_MODULE,
663 .suspend = atmel_spi_suspend,
664 .resume = atmel_spi_resume,
665 .remove = __exit_p(atmel_spi_remove),
668 static int __init atmel_spi_init(void)
670 return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe);
672 module_init(atmel_spi_init);
674 static void __exit atmel_spi_exit(void)
676 platform_driver_unregister(&atmel_spi_driver);
678 module_exit(atmel_spi_exit);
680 MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver");
681 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
682 MODULE_LICENSE("GPL");