2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/init.h>
20 #include <linux/module.h>
21 #include <linux/device.h>
22 #include <linux/ioport.h>
23 #include <linux/errno.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/spi/spi.h>
28 #include <linux/workqueue.h>
29 #include <linux/delay.h>
30 #include <linux/clk.h>
31 #include <linux/gpio.h>
35 #include <asm/delay.h>
38 #include <mach/regs-ssp.h>
40 #include <mach/pxa2xx_spi.h>
42 MODULE_AUTHOR("Stephen Street");
43 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
44 MODULE_LICENSE("GPL");
45 MODULE_ALIAS("platform:pxa2xx-spi");
49 #define RX_THRESH_DFLT 8
50 #define TX_THRESH_DFLT 8
51 #define TIMOUT_DFLT 1000
53 #define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
54 #define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
55 #define IS_DMA_ALIGNED(x) ((((u32)(x)) & 0x07) == 0)
56 #define MAX_DMA_LEN 8191
57 #define DMA_ALIGNMENT 8
60 * for testing SSCR1 changes that require SSP restart, basically
61 * everything except the service and interrupt enables, the pxa270 developer
62 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
63 * list, but the PXA255 dev man says all bits without really meaning the
64 * service and interrupt enables
66 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
67 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
68 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
69 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
70 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
71 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
73 #define DEFINE_SSP_REG(reg, off) \
74 static inline u32 read_##reg(void const __iomem *p) \
75 { return __raw_readl(p + (off)); } \
77 static inline void write_##reg(u32 v, void __iomem *p) \
78 { __raw_writel(v, p + (off)); }
80 DEFINE_SSP_REG(SSCR0, 0x00)
81 DEFINE_SSP_REG(SSCR1, 0x04)
82 DEFINE_SSP_REG(SSSR, 0x08)
83 DEFINE_SSP_REG(SSITR, 0x0c)
84 DEFINE_SSP_REG(SSDR, 0x10)
85 DEFINE_SSP_REG(SSTO, 0x28)
86 DEFINE_SSP_REG(SSPSP, 0x2c)
88 #define START_STATE ((void*)0)
89 #define RUNNING_STATE ((void*)1)
90 #define DONE_STATE ((void*)2)
91 #define ERROR_STATE ((void*)-1)
93 #define QUEUE_RUNNING 0
94 #define QUEUE_STOPPED 1
97 /* Driver model hookup */
98 struct platform_device *pdev;
101 struct ssp_device *ssp;
103 /* SPI framework hookup */
104 enum pxa_ssp_type ssp_type;
105 struct spi_master *master;
108 struct pxa2xx_spi_master *master_info;
110 /* DMA setup stuff */
115 /* SSP register addresses */
116 void __iomem *ioaddr;
125 /* Driver message queue */
126 struct workqueue_struct *workqueue;
127 struct work_struct pump_messages;
129 struct list_head queue;
133 /* Message Transfer pump */
134 struct tasklet_struct pump_transfers;
136 /* Current message transfer state info */
137 struct spi_message* cur_msg;
138 struct spi_transfer* cur_transfer;
139 struct chip_data *cur_chip;
152 int (*write)(struct driver_data *drv_data);
153 int (*read)(struct driver_data *drv_data);
154 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
155 void (*cs_control)(u32 command);
172 int gpio_cs_inverted;
173 int (*write)(struct driver_data *drv_data);
174 int (*read)(struct driver_data *drv_data);
175 void (*cs_control)(u32 command);
178 static void pump_messages(struct work_struct *work);
180 static void cs_assert(struct driver_data *drv_data)
182 struct chip_data *chip = drv_data->cur_chip;
184 if (chip->cs_control) {
185 chip->cs_control(PXA2XX_CS_ASSERT);
189 if (gpio_is_valid(chip->gpio_cs))
190 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
193 static void cs_deassert(struct driver_data *drv_data)
195 struct chip_data *chip = drv_data->cur_chip;
197 if (chip->cs_control) {
198 chip->cs_control(PXA2XX_CS_ASSERT);
202 if (gpio_is_valid(chip->gpio_cs))
203 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
206 static int flush(struct driver_data *drv_data)
208 unsigned long limit = loops_per_jiffy << 1;
210 void __iomem *reg = drv_data->ioaddr;
213 while (read_SSSR(reg) & SSSR_RNE) {
216 } while ((read_SSSR(reg) & SSSR_BSY) && limit--);
217 write_SSSR(SSSR_ROR, reg);
222 static int null_writer(struct driver_data *drv_data)
224 void __iomem *reg = drv_data->ioaddr;
225 u8 n_bytes = drv_data->n_bytes;
227 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
228 || (drv_data->tx == drv_data->tx_end))
232 drv_data->tx += n_bytes;
237 static int null_reader(struct driver_data *drv_data)
239 void __iomem *reg = drv_data->ioaddr;
240 u8 n_bytes = drv_data->n_bytes;
242 while ((read_SSSR(reg) & SSSR_RNE)
243 && (drv_data->rx < drv_data->rx_end)) {
245 drv_data->rx += n_bytes;
248 return drv_data->rx == drv_data->rx_end;
251 static int u8_writer(struct driver_data *drv_data)
253 void __iomem *reg = drv_data->ioaddr;
255 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
256 || (drv_data->tx == drv_data->tx_end))
259 write_SSDR(*(u8 *)(drv_data->tx), reg);
265 static int u8_reader(struct driver_data *drv_data)
267 void __iomem *reg = drv_data->ioaddr;
269 while ((read_SSSR(reg) & SSSR_RNE)
270 && (drv_data->rx < drv_data->rx_end)) {
271 *(u8 *)(drv_data->rx) = read_SSDR(reg);
275 return drv_data->rx == drv_data->rx_end;
278 static int u16_writer(struct driver_data *drv_data)
280 void __iomem *reg = drv_data->ioaddr;
282 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
283 || (drv_data->tx == drv_data->tx_end))
286 write_SSDR(*(u16 *)(drv_data->tx), reg);
292 static int u16_reader(struct driver_data *drv_data)
294 void __iomem *reg = drv_data->ioaddr;
296 while ((read_SSSR(reg) & SSSR_RNE)
297 && (drv_data->rx < drv_data->rx_end)) {
298 *(u16 *)(drv_data->rx) = read_SSDR(reg);
302 return drv_data->rx == drv_data->rx_end;
305 static int u32_writer(struct driver_data *drv_data)
307 void __iomem *reg = drv_data->ioaddr;
309 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
310 || (drv_data->tx == drv_data->tx_end))
313 write_SSDR(*(u32 *)(drv_data->tx), reg);
319 static int u32_reader(struct driver_data *drv_data)
321 void __iomem *reg = drv_data->ioaddr;
323 while ((read_SSSR(reg) & SSSR_RNE)
324 && (drv_data->rx < drv_data->rx_end)) {
325 *(u32 *)(drv_data->rx) = read_SSDR(reg);
329 return drv_data->rx == drv_data->rx_end;
332 static void *next_transfer(struct driver_data *drv_data)
334 struct spi_message *msg = drv_data->cur_msg;
335 struct spi_transfer *trans = drv_data->cur_transfer;
337 /* Move to next transfer */
338 if (trans->transfer_list.next != &msg->transfers) {
339 drv_data->cur_transfer =
340 list_entry(trans->transfer_list.next,
343 return RUNNING_STATE;
348 static int map_dma_buffers(struct driver_data *drv_data)
350 struct spi_message *msg = drv_data->cur_msg;
351 struct device *dev = &msg->spi->dev;
353 if (!drv_data->cur_chip->enable_dma)
356 if (msg->is_dma_mapped)
357 return drv_data->rx_dma && drv_data->tx_dma;
359 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
362 /* Modify setup if rx buffer is null */
363 if (drv_data->rx == NULL) {
364 *drv_data->null_dma_buf = 0;
365 drv_data->rx = drv_data->null_dma_buf;
366 drv_data->rx_map_len = 4;
368 drv_data->rx_map_len = drv_data->len;
371 /* Modify setup if tx buffer is null */
372 if (drv_data->tx == NULL) {
373 *drv_data->null_dma_buf = 0;
374 drv_data->tx = drv_data->null_dma_buf;
375 drv_data->tx_map_len = 4;
377 drv_data->tx_map_len = drv_data->len;
379 /* Stream map the tx buffer. Always do DMA_TO_DEVICE first
380 * so we flush the cache *before* invalidating it, in case
381 * the tx and rx buffers overlap.
383 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
384 drv_data->tx_map_len, DMA_TO_DEVICE);
385 if (dma_mapping_error(dev, drv_data->tx_dma))
388 /* Stream map the rx buffer */
389 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
390 drv_data->rx_map_len, DMA_FROM_DEVICE);
391 if (dma_mapping_error(dev, drv_data->rx_dma)) {
392 dma_unmap_single(dev, drv_data->tx_dma,
393 drv_data->tx_map_len, DMA_TO_DEVICE);
400 static void unmap_dma_buffers(struct driver_data *drv_data)
404 if (!drv_data->dma_mapped)
407 if (!drv_data->cur_msg->is_dma_mapped) {
408 dev = &drv_data->cur_msg->spi->dev;
409 dma_unmap_single(dev, drv_data->rx_dma,
410 drv_data->rx_map_len, DMA_FROM_DEVICE);
411 dma_unmap_single(dev, drv_data->tx_dma,
412 drv_data->tx_map_len, DMA_TO_DEVICE);
415 drv_data->dma_mapped = 0;
418 /* caller already set message->status; dma and pio irqs are blocked */
419 static void giveback(struct driver_data *drv_data)
421 struct spi_transfer* last_transfer;
423 struct spi_message *msg;
425 spin_lock_irqsave(&drv_data->lock, flags);
426 msg = drv_data->cur_msg;
427 drv_data->cur_msg = NULL;
428 drv_data->cur_transfer = NULL;
429 queue_work(drv_data->workqueue, &drv_data->pump_messages);
430 spin_unlock_irqrestore(&drv_data->lock, flags);
432 last_transfer = list_entry(msg->transfers.prev,
436 /* Delay if requested before any change in chip select */
437 if (last_transfer->delay_usecs)
438 udelay(last_transfer->delay_usecs);
440 /* Drop chip select UNLESS cs_change is true or we are returning
441 * a message with an error, or next message is for another chip
443 if (!last_transfer->cs_change)
444 cs_deassert(drv_data);
446 struct spi_message *next_msg;
448 /* Holding of cs was hinted, but we need to make sure
449 * the next message is for the same chip. Don't waste
450 * time with the following tests unless this was hinted.
452 * We cannot postpone this until pump_messages, because
453 * after calling msg->complete (below) the driver that
454 * sent the current message could be unloaded, which
455 * could invalidate the cs_control() callback...
458 /* get a pointer to the next message, if any */
459 spin_lock_irqsave(&drv_data->lock, flags);
460 if (list_empty(&drv_data->queue))
463 next_msg = list_entry(drv_data->queue.next,
464 struct spi_message, queue);
465 spin_unlock_irqrestore(&drv_data->lock, flags);
467 /* see if the next and current messages point
470 if (next_msg && next_msg->spi != msg->spi)
472 if (!next_msg || msg->state == ERROR_STATE)
473 cs_deassert(drv_data);
478 msg->complete(msg->context);
480 drv_data->cur_chip = NULL;
483 static int wait_ssp_rx_stall(void const __iomem *ioaddr)
485 unsigned long limit = loops_per_jiffy << 1;
487 while ((read_SSSR(ioaddr) & SSSR_BSY) && limit--)
493 static int wait_dma_channel_stop(int channel)
495 unsigned long limit = loops_per_jiffy << 1;
497 while (!(DCSR(channel) & DCSR_STOPSTATE) && limit--)
503 static void dma_error_stop(struct driver_data *drv_data, const char *msg)
505 void __iomem *reg = drv_data->ioaddr;
508 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
509 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
510 write_SSSR(drv_data->clear_sr, reg);
511 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
512 if (drv_data->ssp_type != PXA25x_SSP)
515 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
517 unmap_dma_buffers(drv_data);
519 dev_err(&drv_data->pdev->dev, "%s\n", msg);
521 drv_data->cur_msg->state = ERROR_STATE;
522 tasklet_schedule(&drv_data->pump_transfers);
525 static void dma_transfer_complete(struct driver_data *drv_data)
527 void __iomem *reg = drv_data->ioaddr;
528 struct spi_message *msg = drv_data->cur_msg;
530 /* Clear and disable interrupts on SSP and DMA channels*/
531 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
532 write_SSSR(drv_data->clear_sr, reg);
533 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
534 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
536 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
537 dev_err(&drv_data->pdev->dev,
538 "dma_handler: dma rx channel stop failed\n");
540 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
541 dev_err(&drv_data->pdev->dev,
542 "dma_transfer: ssp rx stall failed\n");
544 unmap_dma_buffers(drv_data);
546 /* update the buffer pointer for the amount completed in dma */
547 drv_data->rx += drv_data->len -
548 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
550 /* read trailing data from fifo, it does not matter how many
551 * bytes are in the fifo just read until buffer is full
552 * or fifo is empty, which ever occurs first */
553 drv_data->read(drv_data);
555 /* return count of what was actually read */
556 msg->actual_length += drv_data->len -
557 (drv_data->rx_end - drv_data->rx);
559 /* Transfer delays and chip select release are
560 * handled in pump_transfers or giveback
563 /* Move to next transfer */
564 msg->state = next_transfer(drv_data);
566 /* Schedule transfer tasklet */
567 tasklet_schedule(&drv_data->pump_transfers);
570 static void dma_handler(int channel, void *data)
572 struct driver_data *drv_data = data;
573 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
575 if (irq_status & DCSR_BUSERR) {
577 if (channel == drv_data->tx_channel)
578 dma_error_stop(drv_data,
580 "bad bus address on tx channel");
582 dma_error_stop(drv_data,
584 "bad bus address on rx channel");
588 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
589 if ((channel == drv_data->tx_channel)
590 && (irq_status & DCSR_ENDINTR)
591 && (drv_data->ssp_type == PXA25x_SSP)) {
593 /* Wait for rx to stall */
594 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
595 dev_err(&drv_data->pdev->dev,
596 "dma_handler: ssp rx stall failed\n");
598 /* finish this transfer, start the next */
599 dma_transfer_complete(drv_data);
603 static irqreturn_t dma_transfer(struct driver_data *drv_data)
606 void __iomem *reg = drv_data->ioaddr;
608 irq_status = read_SSSR(reg) & drv_data->mask_sr;
609 if (irq_status & SSSR_ROR) {
610 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
614 /* Check for false positive timeout */
615 if ((irq_status & SSSR_TINT)
616 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
617 write_SSSR(SSSR_TINT, reg);
621 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
623 /* Clear and disable timeout interrupt, do the rest in
624 * dma_transfer_complete */
625 if (drv_data->ssp_type != PXA25x_SSP)
628 /* finish this transfer, start the next */
629 dma_transfer_complete(drv_data);
634 /* Opps problem detected */
638 static void int_error_stop(struct driver_data *drv_data, const char* msg)
640 void __iomem *reg = drv_data->ioaddr;
642 /* Stop and reset SSP */
643 write_SSSR(drv_data->clear_sr, reg);
644 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
645 if (drv_data->ssp_type != PXA25x_SSP)
648 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
650 dev_err(&drv_data->pdev->dev, "%s\n", msg);
652 drv_data->cur_msg->state = ERROR_STATE;
653 tasklet_schedule(&drv_data->pump_transfers);
656 static void int_transfer_complete(struct driver_data *drv_data)
658 void __iomem *reg = drv_data->ioaddr;
661 write_SSSR(drv_data->clear_sr, reg);
662 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
663 if (drv_data->ssp_type != PXA25x_SSP)
666 /* Update total byte transfered return count actual bytes read */
667 drv_data->cur_msg->actual_length += drv_data->len -
668 (drv_data->rx_end - drv_data->rx);
670 /* Transfer delays and chip select release are
671 * handled in pump_transfers or giveback
674 /* Move to next transfer */
675 drv_data->cur_msg->state = next_transfer(drv_data);
677 /* Schedule transfer tasklet */
678 tasklet_schedule(&drv_data->pump_transfers);
681 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
683 void __iomem *reg = drv_data->ioaddr;
685 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
686 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
688 u32 irq_status = read_SSSR(reg) & irq_mask;
690 if (irq_status & SSSR_ROR) {
691 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
695 if (irq_status & SSSR_TINT) {
696 write_SSSR(SSSR_TINT, reg);
697 if (drv_data->read(drv_data)) {
698 int_transfer_complete(drv_data);
703 /* Drain rx fifo, Fill tx fifo and prevent overruns */
705 if (drv_data->read(drv_data)) {
706 int_transfer_complete(drv_data);
709 } while (drv_data->write(drv_data));
711 if (drv_data->read(drv_data)) {
712 int_transfer_complete(drv_data);
716 if (drv_data->tx == drv_data->tx_end) {
717 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
718 /* PXA25x_SSP has no timeout, read trailing bytes */
719 if (drv_data->ssp_type == PXA25x_SSP) {
720 if (!wait_ssp_rx_stall(reg))
722 int_error_stop(drv_data, "interrupt_transfer: "
726 if (!drv_data->read(drv_data))
728 int_error_stop(drv_data,
729 "interrupt_transfer: "
730 "trailing byte read failed");
733 int_transfer_complete(drv_data);
737 /* We did something */
741 static irqreturn_t ssp_int(int irq, void *dev_id)
743 struct driver_data *drv_data = dev_id;
744 void __iomem *reg = drv_data->ioaddr;
746 if (!drv_data->cur_msg) {
748 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
749 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
750 if (drv_data->ssp_type != PXA25x_SSP)
752 write_SSSR(drv_data->clear_sr, reg);
754 dev_err(&drv_data->pdev->dev, "bad message state "
755 "in interrupt handler\n");
761 return drv_data->transfer_handler(drv_data);
764 static int set_dma_burst_and_threshold(struct chip_data *chip,
765 struct spi_device *spi,
766 u8 bits_per_word, u32 *burst_code,
769 struct pxa2xx_spi_chip *chip_info =
770 (struct pxa2xx_spi_chip *)spi->controller_data;
777 /* Set the threshold (in registers) to equal the same amount of data
778 * as represented by burst size (in bytes). The computation below
779 * is (burst_size rounded up to nearest 8 byte, word or long word)
780 * divided by (bytes/register); the tx threshold is the inverse of
781 * the rx, so that there will always be enough data in the rx fifo
782 * to satisfy a burst, and there will always be enough space in the
783 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
784 * there is not enough space), there must always remain enough empty
785 * space in the rx fifo for any data loaded to the tx fifo.
786 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
787 * will be 8, or half the fifo;
788 * The threshold can only be set to 2, 4 or 8, but not 16, because
789 * to burst 16 to the tx fifo, the fifo would have to be empty;
790 * however, the minimum fifo trigger level is 1, and the tx will
791 * request service when the fifo is at this level, with only 15 spaces.
794 /* find bytes/word */
795 if (bits_per_word <= 8)
797 else if (bits_per_word <= 16)
802 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
804 req_burst_size = chip_info->dma_burst_size;
806 switch (chip->dma_burst_size) {
808 /* if the default burst size is not set,
810 chip->dma_burst_size = DCMD_BURST8;
822 if (req_burst_size <= 8) {
823 *burst_code = DCMD_BURST8;
825 } else if (req_burst_size <= 16) {
826 if (bytes_per_word == 1) {
827 /* don't burst more than 1/2 the fifo */
828 *burst_code = DCMD_BURST8;
832 *burst_code = DCMD_BURST16;
836 if (bytes_per_word == 1) {
837 /* don't burst more than 1/2 the fifo */
838 *burst_code = DCMD_BURST8;
841 } else if (bytes_per_word == 2) {
842 /* don't burst more than 1/2 the fifo */
843 *burst_code = DCMD_BURST16;
847 *burst_code = DCMD_BURST32;
852 thresh_words = burst_bytes / bytes_per_word;
854 /* thresh_words will be between 2 and 8 */
855 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
856 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
861 static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
863 unsigned long ssp_clk = clk_get_rate(ssp->clk);
865 if (ssp->type == PXA25x_SSP)
866 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
868 return ((ssp_clk / rate - 1) & 0xfff) << 8;
871 static void pump_transfers(unsigned long data)
873 struct driver_data *drv_data = (struct driver_data *)data;
874 struct spi_message *message = NULL;
875 struct spi_transfer *transfer = NULL;
876 struct spi_transfer *previous = NULL;
877 struct chip_data *chip = NULL;
878 struct ssp_device *ssp = drv_data->ssp;
879 void __iomem *reg = drv_data->ioaddr;
885 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
886 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
888 /* Get current state information */
889 message = drv_data->cur_msg;
890 transfer = drv_data->cur_transfer;
891 chip = drv_data->cur_chip;
893 /* Handle for abort */
894 if (message->state == ERROR_STATE) {
895 message->status = -EIO;
900 /* Handle end of message */
901 if (message->state == DONE_STATE) {
907 /* Delay if requested at end of transfer before CS change */
908 if (message->state == RUNNING_STATE) {
909 previous = list_entry(transfer->transfer_list.prev,
912 if (previous->delay_usecs)
913 udelay(previous->delay_usecs);
915 /* Drop chip select only if cs_change is requested */
916 if (previous->cs_change)
917 cs_deassert(drv_data);
920 /* Check for transfers that need multiple DMA segments */
921 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
923 /* reject already-mapped transfers; PIO won't always work */
924 if (message->is_dma_mapped
925 || transfer->rx_dma || transfer->tx_dma) {
926 dev_err(&drv_data->pdev->dev,
927 "pump_transfers: mapped transfer length "
928 "of %u is greater than %d\n",
929 transfer->len, MAX_DMA_LEN);
930 message->status = -EINVAL;
935 /* warn ... we force this to PIO mode */
936 if (printk_ratelimit())
937 dev_warn(&message->spi->dev, "pump_transfers: "
938 "DMA disabled for transfer length %ld "
940 (long)drv_data->len, MAX_DMA_LEN);
943 /* Setup the transfer state based on the type of transfer */
944 if (flush(drv_data) == 0) {
945 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
946 message->status = -EIO;
950 drv_data->n_bytes = chip->n_bytes;
951 drv_data->dma_width = chip->dma_width;
952 drv_data->tx = (void *)transfer->tx_buf;
953 drv_data->tx_end = drv_data->tx + transfer->len;
954 drv_data->rx = transfer->rx_buf;
955 drv_data->rx_end = drv_data->rx + transfer->len;
956 drv_data->rx_dma = transfer->rx_dma;
957 drv_data->tx_dma = transfer->tx_dma;
958 drv_data->len = transfer->len & DCMD_LENGTH;
959 drv_data->write = drv_data->tx ? chip->write : null_writer;
960 drv_data->read = drv_data->rx ? chip->read : null_reader;
962 /* Change speed and bit per word on a per transfer */
964 if (transfer->speed_hz || transfer->bits_per_word) {
966 bits = chip->bits_per_word;
967 speed = chip->speed_hz;
969 if (transfer->speed_hz)
970 speed = transfer->speed_hz;
972 if (transfer->bits_per_word)
973 bits = transfer->bits_per_word;
975 clk_div = ssp_get_clk_div(ssp, speed);
978 drv_data->n_bytes = 1;
979 drv_data->dma_width = DCMD_WIDTH1;
980 drv_data->read = drv_data->read != null_reader ?
981 u8_reader : null_reader;
982 drv_data->write = drv_data->write != null_writer ?
983 u8_writer : null_writer;
984 } else if (bits <= 16) {
985 drv_data->n_bytes = 2;
986 drv_data->dma_width = DCMD_WIDTH2;
987 drv_data->read = drv_data->read != null_reader ?
988 u16_reader : null_reader;
989 drv_data->write = drv_data->write != null_writer ?
990 u16_writer : null_writer;
991 } else if (bits <= 32) {
992 drv_data->n_bytes = 4;
993 drv_data->dma_width = DCMD_WIDTH4;
994 drv_data->read = drv_data->read != null_reader ?
995 u32_reader : null_reader;
996 drv_data->write = drv_data->write != null_writer ?
997 u32_writer : null_writer;
999 /* if bits/word is changed in dma mode, then must check the
1000 * thresholds and burst also */
1001 if (chip->enable_dma) {
1002 if (set_dma_burst_and_threshold(chip, message->spi,
1005 if (printk_ratelimit())
1006 dev_warn(&message->spi->dev,
1008 "DMA burst size reduced to "
1009 "match bits_per_word\n");
1014 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
1016 | (bits > 16 ? SSCR0_EDSS : 0);
1019 message->state = RUNNING_STATE;
1021 /* Try to map dma buffer and do a dma transfer if successful, but
1022 * only if the length is non-zero and less than MAX_DMA_LEN.
1024 * Zero-length non-descriptor DMA is illegal on PXA2xx; force use
1025 * of PIO instead. Care is needed above because the transfer may
1026 * have have been passed with buffers that are already dma mapped.
1027 * A zero-length transfer in PIO mode will not try to write/read
1028 * to/from the buffers
1030 * REVISIT large transfers are exactly where we most want to be
1031 * using DMA. If this happens much, split those transfers into
1032 * multiple DMA segments rather than forcing PIO.
1034 drv_data->dma_mapped = 0;
1035 if (drv_data->len > 0 && drv_data->len <= MAX_DMA_LEN)
1036 drv_data->dma_mapped = map_dma_buffers(drv_data);
1037 if (drv_data->dma_mapped) {
1039 /* Ensure we have the correct interrupt handler */
1040 drv_data->transfer_handler = dma_transfer;
1042 /* Setup rx DMA Channel */
1043 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
1044 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
1045 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
1046 if (drv_data->rx == drv_data->null_dma_buf)
1047 /* No target address increment */
1048 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
1049 | drv_data->dma_width
1053 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
1055 | drv_data->dma_width
1059 /* Setup tx DMA Channel */
1060 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
1061 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
1062 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
1063 if (drv_data->tx == drv_data->null_dma_buf)
1064 /* No source address increment */
1065 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
1066 | drv_data->dma_width
1070 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
1072 | drv_data->dma_width
1076 /* Enable dma end irqs on SSP to detect end of transfer */
1077 if (drv_data->ssp_type == PXA25x_SSP)
1078 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
1080 /* Clear status and start DMA engine */
1081 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1082 write_SSSR(drv_data->clear_sr, reg);
1083 DCSR(drv_data->rx_channel) |= DCSR_RUN;
1084 DCSR(drv_data->tx_channel) |= DCSR_RUN;
1086 /* Ensure we have the correct interrupt handler */
1087 drv_data->transfer_handler = interrupt_transfer;
1090 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1091 write_SSSR(drv_data->clear_sr, reg);
1094 /* see if we need to reload the config registers */
1095 if ((read_SSCR0(reg) != cr0)
1096 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
1097 (cr1 & SSCR1_CHANGE_MASK)) {
1099 /* stop the SSP, and update the other bits */
1100 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
1101 if (drv_data->ssp_type != PXA25x_SSP)
1102 write_SSTO(chip->timeout, reg);
1103 /* first set CR1 without interrupt and service enables */
1104 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1105 /* restart the SSP */
1106 write_SSCR0(cr0, reg);
1109 if (drv_data->ssp_type != PXA25x_SSP)
1110 write_SSTO(chip->timeout, reg);
1113 cs_assert(drv_data);
1115 /* after chip select, release the data by enabling service
1116 * requests and interrupts, without changing any mode bits */
1117 write_SSCR1(cr1, reg);
1120 static void pump_messages(struct work_struct *work)
1122 struct driver_data *drv_data =
1123 container_of(work, struct driver_data, pump_messages);
1124 unsigned long flags;
1126 /* Lock queue and check for queue work */
1127 spin_lock_irqsave(&drv_data->lock, flags);
1128 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1130 spin_unlock_irqrestore(&drv_data->lock, flags);
1134 /* Make sure we are not already running a message */
1135 if (drv_data->cur_msg) {
1136 spin_unlock_irqrestore(&drv_data->lock, flags);
1140 /* Extract head of queue */
1141 drv_data->cur_msg = list_entry(drv_data->queue.next,
1142 struct spi_message, queue);
1143 list_del_init(&drv_data->cur_msg->queue);
1145 /* Initial message state*/
1146 drv_data->cur_msg->state = START_STATE;
1147 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1148 struct spi_transfer,
1151 /* prepare to setup the SSP, in pump_transfers, using the per
1152 * chip configuration */
1153 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
1155 /* Mark as busy and launch transfers */
1156 tasklet_schedule(&drv_data->pump_transfers);
1159 spin_unlock_irqrestore(&drv_data->lock, flags);
1162 static int transfer(struct spi_device *spi, struct spi_message *msg)
1164 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1165 unsigned long flags;
1167 spin_lock_irqsave(&drv_data->lock, flags);
1169 if (drv_data->run == QUEUE_STOPPED) {
1170 spin_unlock_irqrestore(&drv_data->lock, flags);
1174 msg->actual_length = 0;
1175 msg->status = -EINPROGRESS;
1176 msg->state = START_STATE;
1178 list_add_tail(&msg->queue, &drv_data->queue);
1180 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1181 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1183 spin_unlock_irqrestore(&drv_data->lock, flags);
1188 /* the spi->mode bits understood by this driver: */
1189 #define MODEBITS (SPI_CPOL | SPI_CPHA)
1191 static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1192 struct pxa2xx_spi_chip *chip_info)
1196 if (chip == NULL || chip_info == NULL)
1199 /* NOTE: setup() can be called multiple times, possibly with
1200 * different chip_info, release previously requested GPIO
1202 if (gpio_is_valid(chip->gpio_cs))
1203 gpio_free(chip->gpio_cs);
1205 /* If (*cs_control) is provided, ignore GPIO chip select */
1206 if (chip_info->cs_control) {
1207 chip->cs_control = chip_info->cs_control;
1211 if (gpio_is_valid(chip_info->gpio_cs)) {
1212 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1214 dev_err(&spi->dev, "failed to request chip select "
1215 "GPIO%d\n", chip_info->gpio_cs);
1219 chip->gpio_cs = chip_info->gpio_cs;
1220 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1222 err = gpio_direction_output(chip->gpio_cs,
1223 !chip->gpio_cs_inverted);
1229 static int setup(struct spi_device *spi)
1231 struct pxa2xx_spi_chip *chip_info = NULL;
1232 struct chip_data *chip;
1233 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1234 struct ssp_device *ssp = drv_data->ssp;
1235 unsigned int clk_div;
1236 uint tx_thres = TX_THRESH_DFLT;
1237 uint rx_thres = RX_THRESH_DFLT;
1239 if (!spi->bits_per_word)
1240 spi->bits_per_word = 8;
1242 if (drv_data->ssp_type != PXA25x_SSP
1243 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1244 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1245 "b/w not 4-32 for type non-PXA25x_SSP\n",
1246 drv_data->ssp_type, spi->bits_per_word);
1249 else if (drv_data->ssp_type == PXA25x_SSP
1250 && (spi->bits_per_word < 4
1251 || spi->bits_per_word > 16)) {
1252 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1253 "b/w not 4-16 for type PXA25x_SSP\n",
1254 drv_data->ssp_type, spi->bits_per_word);
1258 if (spi->mode & ~MODEBITS) {
1259 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
1260 spi->mode & ~MODEBITS);
1264 /* Only alloc on first setup */
1265 chip = spi_get_ctldata(spi);
1267 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1270 "failed setup: can't allocate chip data\n");
1275 chip->enable_dma = 0;
1276 chip->timeout = TIMOUT_DFLT;
1277 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1281 /* protocol drivers may change the chip settings, so...
1282 * if chip_info exists, use it */
1283 chip_info = spi->controller_data;
1285 /* chip_info isn't always needed */
1288 if (chip_info->timeout)
1289 chip->timeout = chip_info->timeout;
1290 if (chip_info->tx_threshold)
1291 tx_thres = chip_info->tx_threshold;
1292 if (chip_info->rx_threshold)
1293 rx_thres = chip_info->rx_threshold;
1294 chip->enable_dma = drv_data->master_info->enable_dma;
1295 chip->dma_threshold = 0;
1296 if (chip_info->enable_loopback)
1297 chip->cr1 = SSCR1_LBM;
1300 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1301 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1303 /* set dma burst and threshold outside of chip_info path so that if
1304 * chip_info goes away after setting chip->enable_dma, the
1305 * burst and threshold can still respond to changes in bits_per_word */
1306 if (chip->enable_dma) {
1307 /* set up legal burst and threshold for dma */
1308 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1309 &chip->dma_burst_size,
1310 &chip->dma_threshold)) {
1311 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1312 "to match bits_per_word\n");
1316 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
1317 chip->speed_hz = spi->max_speed_hz;
1321 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1322 spi->bits_per_word - 16 : spi->bits_per_word)
1324 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
1325 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1326 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1327 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1329 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1330 if (drv_data->ssp_type != PXA25x_SSP)
1331 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d, %s\n",
1333 clk_get_rate(ssp->clk)
1334 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1336 chip->enable_dma ? "DMA" : "PIO");
1338 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d, %s\n",
1340 clk_get_rate(ssp->clk) / 2
1341 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1343 chip->enable_dma ? "DMA" : "PIO");
1345 if (spi->bits_per_word <= 8) {
1347 chip->dma_width = DCMD_WIDTH1;
1348 chip->read = u8_reader;
1349 chip->write = u8_writer;
1350 } else if (spi->bits_per_word <= 16) {
1352 chip->dma_width = DCMD_WIDTH2;
1353 chip->read = u16_reader;
1354 chip->write = u16_writer;
1355 } else if (spi->bits_per_word <= 32) {
1356 chip->cr0 |= SSCR0_EDSS;
1358 chip->dma_width = DCMD_WIDTH4;
1359 chip->read = u32_reader;
1360 chip->write = u32_writer;
1362 dev_err(&spi->dev, "invalid wordsize\n");
1365 chip->bits_per_word = spi->bits_per_word;
1367 spi_set_ctldata(spi, chip);
1369 return setup_cs(spi, chip, chip_info);
1372 static void cleanup(struct spi_device *spi)
1374 struct chip_data *chip = spi_get_ctldata(spi);
1376 if (gpio_is_valid(chip->gpio_cs))
1377 gpio_free(chip->gpio_cs);
1382 static int __init init_queue(struct driver_data *drv_data)
1384 INIT_LIST_HEAD(&drv_data->queue);
1385 spin_lock_init(&drv_data->lock);
1387 drv_data->run = QUEUE_STOPPED;
1390 tasklet_init(&drv_data->pump_transfers,
1391 pump_transfers, (unsigned long)drv_data);
1393 INIT_WORK(&drv_data->pump_messages, pump_messages);
1394 drv_data->workqueue = create_singlethread_workqueue(
1395 dev_name(drv_data->master->dev.parent));
1396 if (drv_data->workqueue == NULL)
1402 static int start_queue(struct driver_data *drv_data)
1404 unsigned long flags;
1406 spin_lock_irqsave(&drv_data->lock, flags);
1408 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1409 spin_unlock_irqrestore(&drv_data->lock, flags);
1413 drv_data->run = QUEUE_RUNNING;
1414 drv_data->cur_msg = NULL;
1415 drv_data->cur_transfer = NULL;
1416 drv_data->cur_chip = NULL;
1417 spin_unlock_irqrestore(&drv_data->lock, flags);
1419 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1424 static int stop_queue(struct driver_data *drv_data)
1426 unsigned long flags;
1427 unsigned limit = 500;
1430 spin_lock_irqsave(&drv_data->lock, flags);
1432 /* This is a bit lame, but is optimized for the common execution path.
1433 * A wait_queue on the drv_data->busy could be used, but then the common
1434 * execution path (pump_messages) would be required to call wake_up or
1435 * friends on every SPI message. Do this instead */
1436 drv_data->run = QUEUE_STOPPED;
1437 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1438 spin_unlock_irqrestore(&drv_data->lock, flags);
1440 spin_lock_irqsave(&drv_data->lock, flags);
1443 if (!list_empty(&drv_data->queue) || drv_data->busy)
1446 spin_unlock_irqrestore(&drv_data->lock, flags);
1451 static int destroy_queue(struct driver_data *drv_data)
1455 status = stop_queue(drv_data);
1456 /* we are unloading the module or failing to load (only two calls
1457 * to this routine), and neither call can handle a return value.
1458 * However, destroy_workqueue calls flush_workqueue, and that will
1459 * block until all work is done. If the reason that stop_queue
1460 * timed out is that the work will never finish, then it does no
1461 * good to call destroy_workqueue, so return anyway. */
1465 destroy_workqueue(drv_data->workqueue);
1470 static int __init pxa2xx_spi_probe(struct platform_device *pdev)
1472 struct device *dev = &pdev->dev;
1473 struct pxa2xx_spi_master *platform_info;
1474 struct spi_master *master;
1475 struct driver_data *drv_data;
1476 struct ssp_device *ssp;
1479 platform_info = dev->platform_data;
1481 ssp = ssp_request(pdev->id, pdev->name);
1483 dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id);
1487 /* Allocate master with space for drv_data and null dma buffer */
1488 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1490 dev_err(&pdev->dev, "cannot alloc spi_master\n");
1494 drv_data = spi_master_get_devdata(master);
1495 drv_data->master = master;
1496 drv_data->master_info = platform_info;
1497 drv_data->pdev = pdev;
1498 drv_data->ssp = ssp;
1500 master->bus_num = pdev->id;
1501 master->num_chipselect = platform_info->num_chipselect;
1502 master->dma_alignment = DMA_ALIGNMENT;
1503 master->cleanup = cleanup;
1504 master->setup = setup;
1505 master->transfer = transfer;
1507 drv_data->ssp_type = ssp->type;
1508 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1509 sizeof(struct driver_data)), 8);
1511 drv_data->ioaddr = ssp->mmio_base;
1512 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1513 if (ssp->type == PXA25x_SSP) {
1514 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1515 drv_data->dma_cr1 = 0;
1516 drv_data->clear_sr = SSSR_ROR;
1517 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1519 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1520 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1521 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1522 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1525 status = request_irq(ssp->irq, ssp_int, 0, dev_name(dev), drv_data);
1527 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1528 goto out_error_master_alloc;
1531 /* Setup DMA if requested */
1532 drv_data->tx_channel = -1;
1533 drv_data->rx_channel = -1;
1534 if (platform_info->enable_dma) {
1536 /* Get two DMA channels (rx and tx) */
1537 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1541 if (drv_data->rx_channel < 0) {
1542 dev_err(dev, "problem (%d) requesting rx channel\n",
1543 drv_data->rx_channel);
1545 goto out_error_irq_alloc;
1547 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1551 if (drv_data->tx_channel < 0) {
1552 dev_err(dev, "problem (%d) requesting tx channel\n",
1553 drv_data->tx_channel);
1555 goto out_error_dma_alloc;
1558 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1559 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
1562 /* Enable SOC clock */
1563 clk_enable(ssp->clk);
1565 /* Load default SSP configuration */
1566 write_SSCR0(0, drv_data->ioaddr);
1567 write_SSCR1(SSCR1_RxTresh(RX_THRESH_DFLT) |
1568 SSCR1_TxTresh(TX_THRESH_DFLT),
1570 write_SSCR0(SSCR0_SerClkDiv(2)
1572 | SSCR0_DataSize(8),
1574 if (drv_data->ssp_type != PXA25x_SSP)
1575 write_SSTO(0, drv_data->ioaddr);
1576 write_SSPSP(0, drv_data->ioaddr);
1578 /* Initial and start queue */
1579 status = init_queue(drv_data);
1581 dev_err(&pdev->dev, "problem initializing queue\n");
1582 goto out_error_clock_enabled;
1584 status = start_queue(drv_data);
1586 dev_err(&pdev->dev, "problem starting queue\n");
1587 goto out_error_clock_enabled;
1590 /* Register with the SPI framework */
1591 platform_set_drvdata(pdev, drv_data);
1592 status = spi_register_master(master);
1594 dev_err(&pdev->dev, "problem registering spi master\n");
1595 goto out_error_queue_alloc;
1600 out_error_queue_alloc:
1601 destroy_queue(drv_data);
1603 out_error_clock_enabled:
1604 clk_disable(ssp->clk);
1606 out_error_dma_alloc:
1607 if (drv_data->tx_channel != -1)
1608 pxa_free_dma(drv_data->tx_channel);
1609 if (drv_data->rx_channel != -1)
1610 pxa_free_dma(drv_data->rx_channel);
1612 out_error_irq_alloc:
1613 free_irq(ssp->irq, drv_data);
1615 out_error_master_alloc:
1616 spi_master_put(master);
1621 static int pxa2xx_spi_remove(struct platform_device *pdev)
1623 struct driver_data *drv_data = platform_get_drvdata(pdev);
1624 struct ssp_device *ssp;
1629 ssp = drv_data->ssp;
1631 /* Remove the queue */
1632 status = destroy_queue(drv_data);
1634 /* the kernel does not check the return status of this
1635 * this routine (mod->exit, within the kernel). Therefore
1636 * nothing is gained by returning from here, the module is
1637 * going away regardless, and we should not leave any more
1638 * resources allocated than necessary. We cannot free the
1639 * message memory in drv_data->queue, but we can release the
1640 * resources below. I think the kernel should honor -EBUSY
1642 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1643 "complete, message memory not freed\n");
1645 /* Disable the SSP at the peripheral and SOC level */
1646 write_SSCR0(0, drv_data->ioaddr);
1647 clk_disable(ssp->clk);
1650 if (drv_data->master_info->enable_dma) {
1651 DRCMR(ssp->drcmr_rx) = 0;
1652 DRCMR(ssp->drcmr_tx) = 0;
1653 pxa_free_dma(drv_data->tx_channel);
1654 pxa_free_dma(drv_data->rx_channel);
1658 free_irq(ssp->irq, drv_data);
1663 /* Disconnect from the SPI framework */
1664 spi_unregister_master(drv_data->master);
1666 /* Prevent double remove */
1667 platform_set_drvdata(pdev, NULL);
1672 static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1676 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1677 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1682 static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1684 struct driver_data *drv_data = platform_get_drvdata(pdev);
1685 struct ssp_device *ssp = drv_data->ssp;
1688 status = stop_queue(drv_data);
1691 write_SSCR0(0, drv_data->ioaddr);
1692 clk_disable(ssp->clk);
1697 static int pxa2xx_spi_resume(struct platform_device *pdev)
1699 struct driver_data *drv_data = platform_get_drvdata(pdev);
1700 struct ssp_device *ssp = drv_data->ssp;
1703 /* Enable the SSP clock */
1704 clk_enable(ssp->clk);
1706 /* Start the queue running */
1707 status = start_queue(drv_data);
1709 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1716 #define pxa2xx_spi_suspend NULL
1717 #define pxa2xx_spi_resume NULL
1718 #endif /* CONFIG_PM */
1720 static struct platform_driver driver = {
1722 .name = "pxa2xx-spi",
1723 .owner = THIS_MODULE,
1725 .remove = pxa2xx_spi_remove,
1726 .shutdown = pxa2xx_spi_shutdown,
1727 .suspend = pxa2xx_spi_suspend,
1728 .resume = pxa2xx_spi_resume,
1731 static int __init pxa2xx_spi_init(void)
1733 return platform_driver_probe(&driver, pxa2xx_spi_probe);
1735 module_init(pxa2xx_spi_init);
1737 static void __exit pxa2xx_spi_exit(void)
1739 platform_driver_unregister(&driver);
1741 module_exit(pxa2xx_spi_exit);