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>
34 #include <asm/hardware.h>
35 #include <asm/delay.h>
38 #include <asm/arch/hardware.h>
39 #include <asm/arch/pxa-regs.h>
40 #include <asm/arch/regs-ssp.h>
41 #include <asm/arch/ssp.h>
42 #include <asm/arch/pxa2xx_spi.h>
44 MODULE_AUTHOR("Stephen Street");
45 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
46 MODULE_LICENSE("GPL");
50 #define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
51 #define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
52 #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07)==0)
55 * for testing SSCR1 changes that require SSP restart, basically
56 * everything except the service and interrupt enables, the pxa270 developer
57 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
58 * list, but the PXA255 dev man says all bits without really meaning the
59 * service and interrupt enables
61 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
62 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
63 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
64 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
65 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
66 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
68 #define DEFINE_SSP_REG(reg, off) \
69 static inline u32 read_##reg(void *p) { return __raw_readl(p + (off)); } \
70 static inline void write_##reg(u32 v, void *p) { __raw_writel(v, p + (off)); }
72 DEFINE_SSP_REG(SSCR0, 0x00)
73 DEFINE_SSP_REG(SSCR1, 0x04)
74 DEFINE_SSP_REG(SSSR, 0x08)
75 DEFINE_SSP_REG(SSITR, 0x0c)
76 DEFINE_SSP_REG(SSDR, 0x10)
77 DEFINE_SSP_REG(SSTO, 0x28)
78 DEFINE_SSP_REG(SSPSP, 0x2c)
80 #define START_STATE ((void*)0)
81 #define RUNNING_STATE ((void*)1)
82 #define DONE_STATE ((void*)2)
83 #define ERROR_STATE ((void*)-1)
85 #define QUEUE_RUNNING 0
86 #define QUEUE_STOPPED 1
89 /* Driver model hookup */
90 struct platform_device *pdev;
93 struct ssp_device *ssp;
95 /* SPI framework hookup */
96 enum pxa_ssp_type ssp_type;
97 struct spi_master *master;
100 struct pxa2xx_spi_master *master_info;
102 /* DMA setup stuff */
107 /* SSP register addresses */
117 /* Driver message queue */
118 struct workqueue_struct *workqueue;
119 struct work_struct pump_messages;
121 struct list_head queue;
125 /* Message Transfer pump */
126 struct tasklet_struct pump_transfers;
128 /* Current message transfer state info */
129 struct spi_message* cur_msg;
130 struct spi_transfer* cur_transfer;
131 struct chip_data *cur_chip;
145 int (*write)(struct driver_data *drv_data);
146 int (*read)(struct driver_data *drv_data);
147 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
148 void (*cs_control)(u32 command);
164 int (*write)(struct driver_data *drv_data);
165 int (*read)(struct driver_data *drv_data);
166 void (*cs_control)(u32 command);
169 static void pump_messages(struct work_struct *work);
171 static int flush(struct driver_data *drv_data)
173 unsigned long limit = loops_per_jiffy << 1;
175 void *reg = drv_data->ioaddr;
178 while (read_SSSR(reg) & SSSR_RNE) {
181 } while ((read_SSSR(reg) & SSSR_BSY) && limit--);
182 write_SSSR(SSSR_ROR, reg);
187 static void null_cs_control(u32 command)
191 static int null_writer(struct driver_data *drv_data)
193 void *reg = drv_data->ioaddr;
194 u8 n_bytes = drv_data->n_bytes;
196 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
197 || (drv_data->tx == drv_data->tx_end))
201 drv_data->tx += n_bytes;
206 static int null_reader(struct driver_data *drv_data)
208 void *reg = drv_data->ioaddr;
209 u8 n_bytes = drv_data->n_bytes;
211 while ((read_SSSR(reg) & SSSR_RNE)
212 && (drv_data->rx < drv_data->rx_end)) {
214 drv_data->rx += n_bytes;
217 return drv_data->rx == drv_data->rx_end;
220 static int u8_writer(struct driver_data *drv_data)
222 void *reg = drv_data->ioaddr;
224 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
225 || (drv_data->tx == drv_data->tx_end))
228 write_SSDR(*(u8 *)(drv_data->tx), reg);
234 static int u8_reader(struct driver_data *drv_data)
236 void *reg = drv_data->ioaddr;
238 while ((read_SSSR(reg) & SSSR_RNE)
239 && (drv_data->rx < drv_data->rx_end)) {
240 *(u8 *)(drv_data->rx) = read_SSDR(reg);
244 return drv_data->rx == drv_data->rx_end;
247 static int u16_writer(struct driver_data *drv_data)
249 void *reg = drv_data->ioaddr;
251 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
252 || (drv_data->tx == drv_data->tx_end))
255 write_SSDR(*(u16 *)(drv_data->tx), reg);
261 static int u16_reader(struct driver_data *drv_data)
263 void *reg = drv_data->ioaddr;
265 while ((read_SSSR(reg) & SSSR_RNE)
266 && (drv_data->rx < drv_data->rx_end)) {
267 *(u16 *)(drv_data->rx) = read_SSDR(reg);
271 return drv_data->rx == drv_data->rx_end;
274 static int u32_writer(struct driver_data *drv_data)
276 void *reg = drv_data->ioaddr;
278 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
279 || (drv_data->tx == drv_data->tx_end))
282 write_SSDR(*(u32 *)(drv_data->tx), reg);
288 static int u32_reader(struct driver_data *drv_data)
290 void *reg = drv_data->ioaddr;
292 while ((read_SSSR(reg) & SSSR_RNE)
293 && (drv_data->rx < drv_data->rx_end)) {
294 *(u32 *)(drv_data->rx) = read_SSDR(reg);
298 return drv_data->rx == drv_data->rx_end;
301 static void *next_transfer(struct driver_data *drv_data)
303 struct spi_message *msg = drv_data->cur_msg;
304 struct spi_transfer *trans = drv_data->cur_transfer;
306 /* Move to next transfer */
307 if (trans->transfer_list.next != &msg->transfers) {
308 drv_data->cur_transfer =
309 list_entry(trans->transfer_list.next,
312 return RUNNING_STATE;
317 static int map_dma_buffers(struct driver_data *drv_data)
319 struct spi_message *msg = drv_data->cur_msg;
320 struct device *dev = &msg->spi->dev;
322 if (!drv_data->cur_chip->enable_dma)
325 if (msg->is_dma_mapped)
326 return drv_data->rx_dma && drv_data->tx_dma;
328 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
331 /* Modify setup if rx buffer is null */
332 if (drv_data->rx == NULL) {
333 *drv_data->null_dma_buf = 0;
334 drv_data->rx = drv_data->null_dma_buf;
335 drv_data->rx_map_len = 4;
337 drv_data->rx_map_len = drv_data->len;
340 /* Modify setup if tx buffer is null */
341 if (drv_data->tx == NULL) {
342 *drv_data->null_dma_buf = 0;
343 drv_data->tx = drv_data->null_dma_buf;
344 drv_data->tx_map_len = 4;
346 drv_data->tx_map_len = drv_data->len;
348 /* Stream map the rx buffer */
349 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
350 drv_data->rx_map_len,
352 if (dma_mapping_error(drv_data->rx_dma))
355 /* Stream map the tx buffer */
356 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
357 drv_data->tx_map_len,
360 if (dma_mapping_error(drv_data->tx_dma)) {
361 dma_unmap_single(dev, drv_data->rx_dma,
362 drv_data->rx_map_len, DMA_FROM_DEVICE);
369 static void unmap_dma_buffers(struct driver_data *drv_data)
373 if (!drv_data->dma_mapped)
376 if (!drv_data->cur_msg->is_dma_mapped) {
377 dev = &drv_data->cur_msg->spi->dev;
378 dma_unmap_single(dev, drv_data->rx_dma,
379 drv_data->rx_map_len, DMA_FROM_DEVICE);
380 dma_unmap_single(dev, drv_data->tx_dma,
381 drv_data->tx_map_len, DMA_TO_DEVICE);
384 drv_data->dma_mapped = 0;
387 /* caller already set message->status; dma and pio irqs are blocked */
388 static void giveback(struct driver_data *drv_data)
390 struct spi_transfer* last_transfer;
392 struct spi_message *msg;
394 spin_lock_irqsave(&drv_data->lock, flags);
395 msg = drv_data->cur_msg;
396 drv_data->cur_msg = NULL;
397 drv_data->cur_transfer = NULL;
398 drv_data->cur_chip = NULL;
399 queue_work(drv_data->workqueue, &drv_data->pump_messages);
400 spin_unlock_irqrestore(&drv_data->lock, flags);
402 last_transfer = list_entry(msg->transfers.prev,
406 if (!last_transfer->cs_change)
407 drv_data->cs_control(PXA2XX_CS_DEASSERT);
411 msg->complete(msg->context);
414 static int wait_ssp_rx_stall(void *ioaddr)
416 unsigned long limit = loops_per_jiffy << 1;
418 while ((read_SSSR(ioaddr) & SSSR_BSY) && limit--)
424 static int wait_dma_channel_stop(int channel)
426 unsigned long limit = loops_per_jiffy << 1;
428 while (!(DCSR(channel) & DCSR_STOPSTATE) && limit--)
434 void dma_error_stop(struct driver_data *drv_data, const char *msg)
436 void *reg = drv_data->ioaddr;
439 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
440 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
441 write_SSSR(drv_data->clear_sr, reg);
442 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
443 if (drv_data->ssp_type != PXA25x_SSP)
446 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
448 unmap_dma_buffers(drv_data);
450 dev_err(&drv_data->pdev->dev, "%s\n", msg);
452 drv_data->cur_msg->state = ERROR_STATE;
453 tasklet_schedule(&drv_data->pump_transfers);
456 static void dma_transfer_complete(struct driver_data *drv_data)
458 void *reg = drv_data->ioaddr;
459 struct spi_message *msg = drv_data->cur_msg;
461 /* Clear and disable interrupts on SSP and DMA channels*/
462 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
463 write_SSSR(drv_data->clear_sr, reg);
464 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
465 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
467 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
468 dev_err(&drv_data->pdev->dev,
469 "dma_handler: dma rx channel stop failed\n");
471 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
472 dev_err(&drv_data->pdev->dev,
473 "dma_transfer: ssp rx stall failed\n");
475 unmap_dma_buffers(drv_data);
477 /* update the buffer pointer for the amount completed in dma */
478 drv_data->rx += drv_data->len -
479 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
481 /* read trailing data from fifo, it does not matter how many
482 * bytes are in the fifo just read until buffer is full
483 * or fifo is empty, which ever occurs first */
484 drv_data->read(drv_data);
486 /* return count of what was actually read */
487 msg->actual_length += drv_data->len -
488 (drv_data->rx_end - drv_data->rx);
490 /* Release chip select if requested, transfer delays are
491 * handled in pump_transfers */
492 if (drv_data->cs_change)
493 drv_data->cs_control(PXA2XX_CS_DEASSERT);
495 /* Move to next transfer */
496 msg->state = next_transfer(drv_data);
498 /* Schedule transfer tasklet */
499 tasklet_schedule(&drv_data->pump_transfers);
502 static void dma_handler(int channel, void *data)
504 struct driver_data *drv_data = data;
505 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
507 if (irq_status & DCSR_BUSERR) {
509 if (channel == drv_data->tx_channel)
510 dma_error_stop(drv_data,
512 "bad bus address on tx channel");
514 dma_error_stop(drv_data,
516 "bad bus address on rx channel");
520 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
521 if ((channel == drv_data->tx_channel)
522 && (irq_status & DCSR_ENDINTR)
523 && (drv_data->ssp_type == PXA25x_SSP)) {
525 /* Wait for rx to stall */
526 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
527 dev_err(&drv_data->pdev->dev,
528 "dma_handler: ssp rx stall failed\n");
530 /* finish this transfer, start the next */
531 dma_transfer_complete(drv_data);
535 static irqreturn_t dma_transfer(struct driver_data *drv_data)
538 void *reg = drv_data->ioaddr;
540 irq_status = read_SSSR(reg) & drv_data->mask_sr;
541 if (irq_status & SSSR_ROR) {
542 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
546 /* Check for false positive timeout */
547 if ((irq_status & SSSR_TINT)
548 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
549 write_SSSR(SSSR_TINT, reg);
553 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
555 /* Clear and disable timeout interrupt, do the rest in
556 * dma_transfer_complete */
557 if (drv_data->ssp_type != PXA25x_SSP)
560 /* finish this transfer, start the next */
561 dma_transfer_complete(drv_data);
566 /* Opps problem detected */
570 static void int_error_stop(struct driver_data *drv_data, const char* msg)
572 void *reg = drv_data->ioaddr;
574 /* Stop and reset SSP */
575 write_SSSR(drv_data->clear_sr, reg);
576 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
577 if (drv_data->ssp_type != PXA25x_SSP)
580 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
582 dev_err(&drv_data->pdev->dev, "%s\n", msg);
584 drv_data->cur_msg->state = ERROR_STATE;
585 tasklet_schedule(&drv_data->pump_transfers);
588 static void int_transfer_complete(struct driver_data *drv_data)
590 void *reg = drv_data->ioaddr;
593 write_SSSR(drv_data->clear_sr, reg);
594 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
595 if (drv_data->ssp_type != PXA25x_SSP)
598 /* Update total byte transfered return count actual bytes read */
599 drv_data->cur_msg->actual_length += drv_data->len -
600 (drv_data->rx_end - drv_data->rx);
602 /* Release chip select if requested, transfer delays are
603 * handled in pump_transfers */
604 if (drv_data->cs_change)
605 drv_data->cs_control(PXA2XX_CS_DEASSERT);
607 /* Move to next transfer */
608 drv_data->cur_msg->state = next_transfer(drv_data);
610 /* Schedule transfer tasklet */
611 tasklet_schedule(&drv_data->pump_transfers);
614 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
616 void *reg = drv_data->ioaddr;
618 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
619 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
621 u32 irq_status = read_SSSR(reg) & irq_mask;
623 if (irq_status & SSSR_ROR) {
624 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
628 if (irq_status & SSSR_TINT) {
629 write_SSSR(SSSR_TINT, reg);
630 if (drv_data->read(drv_data)) {
631 int_transfer_complete(drv_data);
636 /* Drain rx fifo, Fill tx fifo and prevent overruns */
638 if (drv_data->read(drv_data)) {
639 int_transfer_complete(drv_data);
642 } while (drv_data->write(drv_data));
644 if (drv_data->read(drv_data)) {
645 int_transfer_complete(drv_data);
649 if (drv_data->tx == drv_data->tx_end) {
650 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
651 /* PXA25x_SSP has no timeout, read trailing bytes */
652 if (drv_data->ssp_type == PXA25x_SSP) {
653 if (!wait_ssp_rx_stall(reg))
655 int_error_stop(drv_data, "interrupt_transfer: "
659 if (!drv_data->read(drv_data))
661 int_error_stop(drv_data,
662 "interrupt_transfer: "
663 "trailing byte read failed");
666 int_transfer_complete(drv_data);
670 /* We did something */
674 static irqreturn_t ssp_int(int irq, void *dev_id)
676 struct driver_data *drv_data = dev_id;
677 void *reg = drv_data->ioaddr;
679 if (!drv_data->cur_msg) {
681 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
682 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
683 if (drv_data->ssp_type != PXA25x_SSP)
685 write_SSSR(drv_data->clear_sr, reg);
687 dev_err(&drv_data->pdev->dev, "bad message state "
688 "in interrupt handler\n");
694 return drv_data->transfer_handler(drv_data);
697 int set_dma_burst_and_threshold(struct chip_data *chip, struct spi_device *spi,
698 u8 bits_per_word, u32 *burst_code,
701 struct pxa2xx_spi_chip *chip_info =
702 (struct pxa2xx_spi_chip *)spi->controller_data;
709 /* Set the threshold (in registers) to equal the same amount of data
710 * as represented by burst size (in bytes). The computation below
711 * is (burst_size rounded up to nearest 8 byte, word or long word)
712 * divided by (bytes/register); the tx threshold is the inverse of
713 * the rx, so that there will always be enough data in the rx fifo
714 * to satisfy a burst, and there will always be enough space in the
715 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
716 * there is not enough space), there must always remain enough empty
717 * space in the rx fifo for any data loaded to the tx fifo.
718 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
719 * will be 8, or half the fifo;
720 * The threshold can only be set to 2, 4 or 8, but not 16, because
721 * to burst 16 to the tx fifo, the fifo would have to be empty;
722 * however, the minimum fifo trigger level is 1, and the tx will
723 * request service when the fifo is at this level, with only 15 spaces.
726 /* find bytes/word */
727 if (bits_per_word <= 8)
729 else if (bits_per_word <= 16)
734 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
736 req_burst_size = chip_info->dma_burst_size;
738 switch (chip->dma_burst_size) {
740 /* if the default burst size is not set,
742 chip->dma_burst_size = DCMD_BURST8;
754 if (req_burst_size <= 8) {
755 *burst_code = DCMD_BURST8;
757 } else if (req_burst_size <= 16) {
758 if (bytes_per_word == 1) {
759 /* don't burst more than 1/2 the fifo */
760 *burst_code = DCMD_BURST8;
764 *burst_code = DCMD_BURST16;
768 if (bytes_per_word == 1) {
769 /* don't burst more than 1/2 the fifo */
770 *burst_code = DCMD_BURST8;
773 } else if (bytes_per_word == 2) {
774 /* don't burst more than 1/2 the fifo */
775 *burst_code = DCMD_BURST16;
779 *burst_code = DCMD_BURST32;
784 thresh_words = burst_bytes / bytes_per_word;
786 /* thresh_words will be between 2 and 8 */
787 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
788 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
793 static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
795 unsigned long ssp_clk = clk_get_rate(ssp->clk);
797 if (ssp->type == PXA25x_SSP)
798 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
800 return ((ssp_clk / rate - 1) & 0xfff) << 8;
803 static void pump_transfers(unsigned long data)
805 struct driver_data *drv_data = (struct driver_data *)data;
806 struct spi_message *message = NULL;
807 struct spi_transfer *transfer = NULL;
808 struct spi_transfer *previous = NULL;
809 struct chip_data *chip = NULL;
810 struct ssp_device *ssp = drv_data->ssp;
811 void *reg = drv_data->ioaddr;
817 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
818 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
820 /* Get current state information */
821 message = drv_data->cur_msg;
822 transfer = drv_data->cur_transfer;
823 chip = drv_data->cur_chip;
825 /* Handle for abort */
826 if (message->state == ERROR_STATE) {
827 message->status = -EIO;
832 /* Handle end of message */
833 if (message->state == DONE_STATE) {
839 /* Delay if requested at end of transfer*/
840 if (message->state == RUNNING_STATE) {
841 previous = list_entry(transfer->transfer_list.prev,
844 if (previous->delay_usecs)
845 udelay(previous->delay_usecs);
848 /* Check transfer length */
849 if (transfer->len > 8191)
851 dev_warn(&drv_data->pdev->dev, "pump_transfers: transfer "
852 "length greater than 8191\n");
853 message->status = -EINVAL;
858 /* Setup the transfer state based on the type of transfer */
859 if (flush(drv_data) == 0) {
860 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
861 message->status = -EIO;
865 drv_data->n_bytes = chip->n_bytes;
866 drv_data->dma_width = chip->dma_width;
867 drv_data->cs_control = chip->cs_control;
868 drv_data->tx = (void *)transfer->tx_buf;
869 drv_data->tx_end = drv_data->tx + transfer->len;
870 drv_data->rx = transfer->rx_buf;
871 drv_data->rx_end = drv_data->rx + transfer->len;
872 drv_data->rx_dma = transfer->rx_dma;
873 drv_data->tx_dma = transfer->tx_dma;
874 drv_data->len = transfer->len & DCMD_LENGTH;
875 drv_data->write = drv_data->tx ? chip->write : null_writer;
876 drv_data->read = drv_data->rx ? chip->read : null_reader;
877 drv_data->cs_change = transfer->cs_change;
879 /* Change speed and bit per word on a per transfer */
881 if (transfer->speed_hz || transfer->bits_per_word) {
883 bits = chip->bits_per_word;
884 speed = chip->speed_hz;
886 if (transfer->speed_hz)
887 speed = transfer->speed_hz;
889 if (transfer->bits_per_word)
890 bits = transfer->bits_per_word;
892 clk_div = ssp_get_clk_div(ssp, speed);
895 drv_data->n_bytes = 1;
896 drv_data->dma_width = DCMD_WIDTH1;
897 drv_data->read = drv_data->read != null_reader ?
898 u8_reader : null_reader;
899 drv_data->write = drv_data->write != null_writer ?
900 u8_writer : null_writer;
901 } else if (bits <= 16) {
902 drv_data->n_bytes = 2;
903 drv_data->dma_width = DCMD_WIDTH2;
904 drv_data->read = drv_data->read != null_reader ?
905 u16_reader : null_reader;
906 drv_data->write = drv_data->write != null_writer ?
907 u16_writer : null_writer;
908 } else if (bits <= 32) {
909 drv_data->n_bytes = 4;
910 drv_data->dma_width = DCMD_WIDTH4;
911 drv_data->read = drv_data->read != null_reader ?
912 u32_reader : null_reader;
913 drv_data->write = drv_data->write != null_writer ?
914 u32_writer : null_writer;
916 /* if bits/word is changed in dma mode, then must check the
917 * thresholds and burst also */
918 if (chip->enable_dma) {
919 if (set_dma_burst_and_threshold(chip, message->spi,
922 if (printk_ratelimit())
923 dev_warn(&message->spi->dev,
925 "DMA burst size reduced to "
926 "match bits_per_word\n");
931 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
933 | (bits > 16 ? SSCR0_EDSS : 0);
936 message->state = RUNNING_STATE;
938 /* Try to map dma buffer and do a dma transfer if successful */
939 if ((drv_data->dma_mapped = map_dma_buffers(drv_data))) {
941 /* Ensure we have the correct interrupt handler */
942 drv_data->transfer_handler = dma_transfer;
944 /* Setup rx DMA Channel */
945 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
946 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
947 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
948 if (drv_data->rx == drv_data->null_dma_buf)
949 /* No target address increment */
950 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
951 | drv_data->dma_width
955 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
957 | drv_data->dma_width
961 /* Setup tx DMA Channel */
962 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
963 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
964 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
965 if (drv_data->tx == drv_data->null_dma_buf)
966 /* No source address increment */
967 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
968 | drv_data->dma_width
972 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
974 | drv_data->dma_width
978 /* Enable dma end irqs on SSP to detect end of transfer */
979 if (drv_data->ssp_type == PXA25x_SSP)
980 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
982 /* Clear status and start DMA engine */
983 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
984 write_SSSR(drv_data->clear_sr, reg);
985 DCSR(drv_data->rx_channel) |= DCSR_RUN;
986 DCSR(drv_data->tx_channel) |= DCSR_RUN;
988 /* Ensure we have the correct interrupt handler */
989 drv_data->transfer_handler = interrupt_transfer;
992 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
993 write_SSSR(drv_data->clear_sr, reg);
996 /* see if we need to reload the config registers */
997 if ((read_SSCR0(reg) != cr0)
998 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
999 (cr1 & SSCR1_CHANGE_MASK)) {
1001 /* stop the SSP, and update the other bits */
1002 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
1003 if (drv_data->ssp_type != PXA25x_SSP)
1004 write_SSTO(chip->timeout, reg);
1005 /* first set CR1 without interrupt and service enables */
1006 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1007 /* restart the SSP */
1008 write_SSCR0(cr0, reg);
1011 if (drv_data->ssp_type != PXA25x_SSP)
1012 write_SSTO(chip->timeout, reg);
1015 /* FIXME, need to handle cs polarity,
1016 * this driver uses struct pxa2xx_spi_chip.cs_control to
1017 * specify a CS handling function, and it ignores most
1018 * struct spi_device.mode[s], including SPI_CS_HIGH */
1019 drv_data->cs_control(PXA2XX_CS_ASSERT);
1021 /* after chip select, release the data by enabling service
1022 * requests and interrupts, without changing any mode bits */
1023 write_SSCR1(cr1, reg);
1026 static void pump_messages(struct work_struct *work)
1028 struct driver_data *drv_data =
1029 container_of(work, struct driver_data, pump_messages);
1030 unsigned long flags;
1032 /* Lock queue and check for queue work */
1033 spin_lock_irqsave(&drv_data->lock, flags);
1034 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1036 spin_unlock_irqrestore(&drv_data->lock, flags);
1040 /* Make sure we are not already running a message */
1041 if (drv_data->cur_msg) {
1042 spin_unlock_irqrestore(&drv_data->lock, flags);
1046 /* Extract head of queue */
1047 drv_data->cur_msg = list_entry(drv_data->queue.next,
1048 struct spi_message, queue);
1049 list_del_init(&drv_data->cur_msg->queue);
1051 /* Initial message state*/
1052 drv_data->cur_msg->state = START_STATE;
1053 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1054 struct spi_transfer,
1057 /* prepare to setup the SSP, in pump_transfers, using the per
1058 * chip configuration */
1059 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
1061 /* Mark as busy and launch transfers */
1062 tasklet_schedule(&drv_data->pump_transfers);
1065 spin_unlock_irqrestore(&drv_data->lock, flags);
1068 static int transfer(struct spi_device *spi, struct spi_message *msg)
1070 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1071 unsigned long flags;
1073 spin_lock_irqsave(&drv_data->lock, flags);
1075 if (drv_data->run == QUEUE_STOPPED) {
1076 spin_unlock_irqrestore(&drv_data->lock, flags);
1080 msg->actual_length = 0;
1081 msg->status = -EINPROGRESS;
1082 msg->state = START_STATE;
1084 list_add_tail(&msg->queue, &drv_data->queue);
1086 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1087 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1089 spin_unlock_irqrestore(&drv_data->lock, flags);
1094 /* the spi->mode bits understood by this driver: */
1095 #define MODEBITS (SPI_CPOL | SPI_CPHA)
1097 static int setup(struct spi_device *spi)
1099 struct pxa2xx_spi_chip *chip_info = NULL;
1100 struct chip_data *chip;
1101 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1102 struct ssp_device *ssp = drv_data->ssp;
1103 unsigned int clk_div;
1105 if (!spi->bits_per_word)
1106 spi->bits_per_word = 8;
1108 if (drv_data->ssp_type != PXA25x_SSP
1109 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1110 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1111 "b/w not 4-32 for type non-PXA25x_SSP\n",
1112 drv_data->ssp_type, spi->bits_per_word);
1115 else if (drv_data->ssp_type == PXA25x_SSP
1116 && (spi->bits_per_word < 4
1117 || spi->bits_per_word > 16)) {
1118 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1119 "b/w not 4-16 for type PXA25x_SSP\n",
1120 drv_data->ssp_type, spi->bits_per_word);
1124 if (spi->mode & ~MODEBITS) {
1125 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
1126 spi->mode & ~MODEBITS);
1130 /* Only alloc on first setup */
1131 chip = spi_get_ctldata(spi);
1133 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1136 "failed setup: can't allocate chip data\n");
1140 chip->cs_control = null_cs_control;
1141 chip->enable_dma = 0;
1142 chip->timeout = 1000;
1143 chip->threshold = SSCR1_RxTresh(1) | SSCR1_TxTresh(1);
1144 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1148 /* protocol drivers may change the chip settings, so...
1149 * if chip_info exists, use it */
1150 chip_info = spi->controller_data;
1152 /* chip_info isn't always needed */
1155 if (chip_info->cs_control)
1156 chip->cs_control = chip_info->cs_control;
1158 chip->timeout = chip_info->timeout;
1160 chip->threshold = (SSCR1_RxTresh(chip_info->rx_threshold) &
1162 (SSCR1_TxTresh(chip_info->tx_threshold) &
1165 chip->enable_dma = chip_info->dma_burst_size != 0
1166 && drv_data->master_info->enable_dma;
1167 chip->dma_threshold = 0;
1169 if (chip_info->enable_loopback)
1170 chip->cr1 = SSCR1_LBM;
1173 /* set dma burst and threshold outside of chip_info path so that if
1174 * chip_info goes away after setting chip->enable_dma, the
1175 * burst and threshold can still respond to changes in bits_per_word */
1176 if (chip->enable_dma) {
1177 /* set up legal burst and threshold for dma */
1178 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1179 &chip->dma_burst_size,
1180 &chip->dma_threshold)) {
1181 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1182 "to match bits_per_word\n");
1186 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
1187 chip->speed_hz = spi->max_speed_hz;
1191 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1192 spi->bits_per_word - 16 : spi->bits_per_word)
1194 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
1195 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1196 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1197 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1199 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1200 if (drv_data->ssp_type != PXA25x_SSP)
1201 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d\n",
1203 clk_get_rate(ssp->clk)
1204 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1207 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d\n",
1209 clk_get_rate(ssp->clk)
1210 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1213 if (spi->bits_per_word <= 8) {
1215 chip->dma_width = DCMD_WIDTH1;
1216 chip->read = u8_reader;
1217 chip->write = u8_writer;
1218 } else if (spi->bits_per_word <= 16) {
1220 chip->dma_width = DCMD_WIDTH2;
1221 chip->read = u16_reader;
1222 chip->write = u16_writer;
1223 } else if (spi->bits_per_word <= 32) {
1224 chip->cr0 |= SSCR0_EDSS;
1226 chip->dma_width = DCMD_WIDTH4;
1227 chip->read = u32_reader;
1228 chip->write = u32_writer;
1230 dev_err(&spi->dev, "invalid wordsize\n");
1233 chip->bits_per_word = spi->bits_per_word;
1235 spi_set_ctldata(spi, chip);
1240 static void cleanup(struct spi_device *spi)
1242 struct chip_data *chip = spi_get_ctldata(spi);
1247 static int __init init_queue(struct driver_data *drv_data)
1249 INIT_LIST_HEAD(&drv_data->queue);
1250 spin_lock_init(&drv_data->lock);
1252 drv_data->run = QUEUE_STOPPED;
1255 tasklet_init(&drv_data->pump_transfers,
1256 pump_transfers, (unsigned long)drv_data);
1258 INIT_WORK(&drv_data->pump_messages, pump_messages);
1259 drv_data->workqueue = create_singlethread_workqueue(
1260 drv_data->master->dev.parent->bus_id);
1261 if (drv_data->workqueue == NULL)
1267 static int start_queue(struct driver_data *drv_data)
1269 unsigned long flags;
1271 spin_lock_irqsave(&drv_data->lock, flags);
1273 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1274 spin_unlock_irqrestore(&drv_data->lock, flags);
1278 drv_data->run = QUEUE_RUNNING;
1279 drv_data->cur_msg = NULL;
1280 drv_data->cur_transfer = NULL;
1281 drv_data->cur_chip = NULL;
1282 spin_unlock_irqrestore(&drv_data->lock, flags);
1284 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1289 static int stop_queue(struct driver_data *drv_data)
1291 unsigned long flags;
1292 unsigned limit = 500;
1295 spin_lock_irqsave(&drv_data->lock, flags);
1297 /* This is a bit lame, but is optimized for the common execution path.
1298 * A wait_queue on the drv_data->busy could be used, but then the common
1299 * execution path (pump_messages) would be required to call wake_up or
1300 * friends on every SPI message. Do this instead */
1301 drv_data->run = QUEUE_STOPPED;
1302 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1303 spin_unlock_irqrestore(&drv_data->lock, flags);
1305 spin_lock_irqsave(&drv_data->lock, flags);
1308 if (!list_empty(&drv_data->queue) || drv_data->busy)
1311 spin_unlock_irqrestore(&drv_data->lock, flags);
1316 static int destroy_queue(struct driver_data *drv_data)
1320 status = stop_queue(drv_data);
1321 /* we are unloading the module or failing to load (only two calls
1322 * to this routine), and neither call can handle a return value.
1323 * However, destroy_workqueue calls flush_workqueue, and that will
1324 * block until all work is done. If the reason that stop_queue
1325 * timed out is that the work will never finish, then it does no
1326 * good to call destroy_workqueue, so return anyway. */
1330 destroy_workqueue(drv_data->workqueue);
1335 static int __init pxa2xx_spi_probe(struct platform_device *pdev)
1337 struct device *dev = &pdev->dev;
1338 struct pxa2xx_spi_master *platform_info;
1339 struct spi_master *master;
1340 struct driver_data *drv_data = 0;
1341 struct ssp_device *ssp;
1344 platform_info = dev->platform_data;
1346 ssp = ssp_request(pdev->id, pdev->name);
1348 dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id);
1352 /* Allocate master with space for drv_data and null dma buffer */
1353 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1355 dev_err(&pdev->dev, "can not alloc spi_master\n");
1359 drv_data = spi_master_get_devdata(master);
1360 drv_data->master = master;
1361 drv_data->master_info = platform_info;
1362 drv_data->pdev = pdev;
1363 drv_data->ssp = ssp;
1365 master->bus_num = pdev->id;
1366 master->num_chipselect = platform_info->num_chipselect;
1367 master->cleanup = cleanup;
1368 master->setup = setup;
1369 master->transfer = transfer;
1371 drv_data->ssp_type = ssp->type;
1372 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1373 sizeof(struct driver_data)), 8);
1375 drv_data->ioaddr = ssp->mmio_base;
1376 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1377 if (ssp->type == PXA25x_SSP) {
1378 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1379 drv_data->dma_cr1 = 0;
1380 drv_data->clear_sr = SSSR_ROR;
1381 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1383 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1384 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1385 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1386 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1389 status = request_irq(ssp->irq, ssp_int, 0, dev->bus_id, drv_data);
1391 dev_err(&pdev->dev, "can not get IRQ\n");
1392 goto out_error_master_alloc;
1395 /* Setup DMA if requested */
1396 drv_data->tx_channel = -1;
1397 drv_data->rx_channel = -1;
1398 if (platform_info->enable_dma) {
1400 /* Get two DMA channels (rx and tx) */
1401 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1405 if (drv_data->rx_channel < 0) {
1406 dev_err(dev, "problem (%d) requesting rx channel\n",
1407 drv_data->rx_channel);
1409 goto out_error_irq_alloc;
1411 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1415 if (drv_data->tx_channel < 0) {
1416 dev_err(dev, "problem (%d) requesting tx channel\n",
1417 drv_data->tx_channel);
1419 goto out_error_dma_alloc;
1422 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1423 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
1426 /* Enable SOC clock */
1427 clk_enable(ssp->clk);
1429 /* Load default SSP configuration */
1430 write_SSCR0(0, drv_data->ioaddr);
1431 write_SSCR1(SSCR1_RxTresh(4) | SSCR1_TxTresh(12), drv_data->ioaddr);
1432 write_SSCR0(SSCR0_SerClkDiv(2)
1434 | SSCR0_DataSize(8),
1436 if (drv_data->ssp_type != PXA25x_SSP)
1437 write_SSTO(0, drv_data->ioaddr);
1438 write_SSPSP(0, drv_data->ioaddr);
1440 /* Initial and start queue */
1441 status = init_queue(drv_data);
1443 dev_err(&pdev->dev, "problem initializing queue\n");
1444 goto out_error_clock_enabled;
1446 status = start_queue(drv_data);
1448 dev_err(&pdev->dev, "problem starting queue\n");
1449 goto out_error_clock_enabled;
1452 /* Register with the SPI framework */
1453 platform_set_drvdata(pdev, drv_data);
1454 status = spi_register_master(master);
1456 dev_err(&pdev->dev, "problem registering spi master\n");
1457 goto out_error_queue_alloc;
1462 out_error_queue_alloc:
1463 destroy_queue(drv_data);
1465 out_error_clock_enabled:
1466 clk_disable(ssp->clk);
1468 out_error_dma_alloc:
1469 if (drv_data->tx_channel != -1)
1470 pxa_free_dma(drv_data->tx_channel);
1471 if (drv_data->rx_channel != -1)
1472 pxa_free_dma(drv_data->rx_channel);
1474 out_error_irq_alloc:
1475 free_irq(ssp->irq, drv_data);
1477 out_error_master_alloc:
1478 spi_master_put(master);
1483 static int pxa2xx_spi_remove(struct platform_device *pdev)
1485 struct driver_data *drv_data = platform_get_drvdata(pdev);
1486 struct ssp_device *ssp = drv_data->ssp;
1492 /* Remove the queue */
1493 status = destroy_queue(drv_data);
1495 /* the kernel does not check the return status of this
1496 * this routine (mod->exit, within the kernel). Therefore
1497 * nothing is gained by returning from here, the module is
1498 * going away regardless, and we should not leave any more
1499 * resources allocated than necessary. We cannot free the
1500 * message memory in drv_data->queue, but we can release the
1501 * resources below. I think the kernel should honor -EBUSY
1503 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1504 "complete, message memory not freed\n");
1506 /* Disable the SSP at the peripheral and SOC level */
1507 write_SSCR0(0, drv_data->ioaddr);
1508 clk_disable(ssp->clk);
1511 if (drv_data->master_info->enable_dma) {
1512 DRCMR(ssp->drcmr_rx) = 0;
1513 DRCMR(ssp->drcmr_tx) = 0;
1514 pxa_free_dma(drv_data->tx_channel);
1515 pxa_free_dma(drv_data->rx_channel);
1519 free_irq(ssp->irq, drv_data);
1524 /* Disconnect from the SPI framework */
1525 spi_unregister_master(drv_data->master);
1527 /* Prevent double remove */
1528 platform_set_drvdata(pdev, NULL);
1533 static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1537 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1538 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1543 static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1545 struct driver_data *drv_data = platform_get_drvdata(pdev);
1546 struct ssp_device *ssp = drv_data->ssp;
1549 status = stop_queue(drv_data);
1552 write_SSCR0(0, drv_data->ioaddr);
1553 clk_disable(ssp->clk);
1558 static int pxa2xx_spi_resume(struct platform_device *pdev)
1560 struct driver_data *drv_data = platform_get_drvdata(pdev);
1561 struct ssp_device *ssp = drv_data->ssp;
1564 /* Enable the SSP clock */
1565 clk_disable(ssp->clk);
1567 /* Start the queue running */
1568 status = start_queue(drv_data);
1570 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1577 #define pxa2xx_spi_suspend NULL
1578 #define pxa2xx_spi_resume NULL
1579 #endif /* CONFIG_PM */
1581 static struct platform_driver driver = {
1583 .name = "pxa2xx-spi",
1584 .bus = &platform_bus_type,
1585 .owner = THIS_MODULE,
1587 .remove = pxa2xx_spi_remove,
1588 .shutdown = pxa2xx_spi_shutdown,
1589 .suspend = pxa2xx_spi_suspend,
1590 .resume = pxa2xx_spi_resume,
1593 static int __init pxa2xx_spi_init(void)
1595 return platform_driver_probe(&driver, pxa2xx_spi_probe);
1597 module_init(pxa2xx_spi_init);
1599 static void __exit pxa2xx_spi_exit(void)
1601 platform_driver_unregister(&driver);
1603 module_exit(pxa2xx_spi_exit);