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");
47 MODULE_ALIAS("platform:pxa2xx-spi");
51 #define DMA_INT_MASK (DCSR_ENDINTR | DCSR_STARTINTR | DCSR_BUSERR)
52 #define RESET_DMA_CHANNEL (DCSR_NODESC | DMA_INT_MASK)
53 #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07)==0)
56 * for testing SSCR1 changes that require SSP restart, basically
57 * everything except the service and interrupt enables, the pxa270 developer
58 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
59 * list, but the PXA255 dev man says all bits without really meaning the
60 * service and interrupt enables
62 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
63 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
64 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
65 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
66 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
67 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
69 #define DEFINE_SSP_REG(reg, off) \
70 static inline u32 read_##reg(void *p) { return __raw_readl(p + (off)); } \
71 static inline void write_##reg(u32 v, void *p) { __raw_writel(v, p + (off)); }
73 DEFINE_SSP_REG(SSCR0, 0x00)
74 DEFINE_SSP_REG(SSCR1, 0x04)
75 DEFINE_SSP_REG(SSSR, 0x08)
76 DEFINE_SSP_REG(SSITR, 0x0c)
77 DEFINE_SSP_REG(SSDR, 0x10)
78 DEFINE_SSP_REG(SSTO, 0x28)
79 DEFINE_SSP_REG(SSPSP, 0x2c)
81 #define START_STATE ((void*)0)
82 #define RUNNING_STATE ((void*)1)
83 #define DONE_STATE ((void*)2)
84 #define ERROR_STATE ((void*)-1)
86 #define QUEUE_RUNNING 0
87 #define QUEUE_STOPPED 1
90 /* Driver model hookup */
91 struct platform_device *pdev;
94 struct ssp_device *ssp;
96 /* SPI framework hookup */
97 enum pxa_ssp_type ssp_type;
98 struct spi_master *master;
101 struct pxa2xx_spi_master *master_info;
103 /* DMA setup stuff */
108 /* SSP register addresses */
118 /* Driver message queue */
119 struct workqueue_struct *workqueue;
120 struct work_struct pump_messages;
122 struct list_head queue;
126 /* Message Transfer pump */
127 struct tasklet_struct pump_transfers;
129 /* Current message transfer state info */
130 struct spi_message* cur_msg;
131 struct spi_transfer* cur_transfer;
132 struct chip_data *cur_chip;
146 int (*write)(struct driver_data *drv_data);
147 int (*read)(struct driver_data *drv_data);
148 irqreturn_t (*transfer_handler)(struct driver_data *drv_data);
149 void (*cs_control)(u32 command);
165 int (*write)(struct driver_data *drv_data);
166 int (*read)(struct driver_data *drv_data);
167 void (*cs_control)(u32 command);
170 static void pump_messages(struct work_struct *work);
172 static int flush(struct driver_data *drv_data)
174 unsigned long limit = loops_per_jiffy << 1;
176 void *reg = drv_data->ioaddr;
179 while (read_SSSR(reg) & SSSR_RNE) {
182 } while ((read_SSSR(reg) & SSSR_BSY) && limit--);
183 write_SSSR(SSSR_ROR, reg);
188 static void null_cs_control(u32 command)
192 static int null_writer(struct driver_data *drv_data)
194 void *reg = drv_data->ioaddr;
195 u8 n_bytes = drv_data->n_bytes;
197 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
198 || (drv_data->tx == drv_data->tx_end))
202 drv_data->tx += n_bytes;
207 static int null_reader(struct driver_data *drv_data)
209 void *reg = drv_data->ioaddr;
210 u8 n_bytes = drv_data->n_bytes;
212 while ((read_SSSR(reg) & SSSR_RNE)
213 && (drv_data->rx < drv_data->rx_end)) {
215 drv_data->rx += n_bytes;
218 return drv_data->rx == drv_data->rx_end;
221 static int u8_writer(struct driver_data *drv_data)
223 void *reg = drv_data->ioaddr;
225 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
226 || (drv_data->tx == drv_data->tx_end))
229 write_SSDR(*(u8 *)(drv_data->tx), reg);
235 static int u8_reader(struct driver_data *drv_data)
237 void *reg = drv_data->ioaddr;
239 while ((read_SSSR(reg) & SSSR_RNE)
240 && (drv_data->rx < drv_data->rx_end)) {
241 *(u8 *)(drv_data->rx) = read_SSDR(reg);
245 return drv_data->rx == drv_data->rx_end;
248 static int u16_writer(struct driver_data *drv_data)
250 void *reg = drv_data->ioaddr;
252 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
253 || (drv_data->tx == drv_data->tx_end))
256 write_SSDR(*(u16 *)(drv_data->tx), reg);
262 static int u16_reader(struct driver_data *drv_data)
264 void *reg = drv_data->ioaddr;
266 while ((read_SSSR(reg) & SSSR_RNE)
267 && (drv_data->rx < drv_data->rx_end)) {
268 *(u16 *)(drv_data->rx) = read_SSDR(reg);
272 return drv_data->rx == drv_data->rx_end;
275 static int u32_writer(struct driver_data *drv_data)
277 void *reg = drv_data->ioaddr;
279 if (((read_SSSR(reg) & 0x00000f00) == 0x00000f00)
280 || (drv_data->tx == drv_data->tx_end))
283 write_SSDR(*(u32 *)(drv_data->tx), reg);
289 static int u32_reader(struct driver_data *drv_data)
291 void *reg = drv_data->ioaddr;
293 while ((read_SSSR(reg) & SSSR_RNE)
294 && (drv_data->rx < drv_data->rx_end)) {
295 *(u32 *)(drv_data->rx) = read_SSDR(reg);
299 return drv_data->rx == drv_data->rx_end;
302 static void *next_transfer(struct driver_data *drv_data)
304 struct spi_message *msg = drv_data->cur_msg;
305 struct spi_transfer *trans = drv_data->cur_transfer;
307 /* Move to next transfer */
308 if (trans->transfer_list.next != &msg->transfers) {
309 drv_data->cur_transfer =
310 list_entry(trans->transfer_list.next,
313 return RUNNING_STATE;
318 static int map_dma_buffers(struct driver_data *drv_data)
320 struct spi_message *msg = drv_data->cur_msg;
321 struct device *dev = &msg->spi->dev;
323 if (!drv_data->cur_chip->enable_dma)
326 if (msg->is_dma_mapped)
327 return drv_data->rx_dma && drv_data->tx_dma;
329 if (!IS_DMA_ALIGNED(drv_data->rx) || !IS_DMA_ALIGNED(drv_data->tx))
332 /* Modify setup if rx buffer is null */
333 if (drv_data->rx == NULL) {
334 *drv_data->null_dma_buf = 0;
335 drv_data->rx = drv_data->null_dma_buf;
336 drv_data->rx_map_len = 4;
338 drv_data->rx_map_len = drv_data->len;
341 /* Modify setup if tx buffer is null */
342 if (drv_data->tx == NULL) {
343 *drv_data->null_dma_buf = 0;
344 drv_data->tx = drv_data->null_dma_buf;
345 drv_data->tx_map_len = 4;
347 drv_data->tx_map_len = drv_data->len;
349 /* Stream map the rx buffer */
350 drv_data->rx_dma = dma_map_single(dev, drv_data->rx,
351 drv_data->rx_map_len,
353 if (dma_mapping_error(drv_data->rx_dma))
356 /* Stream map the tx buffer */
357 drv_data->tx_dma = dma_map_single(dev, drv_data->tx,
358 drv_data->tx_map_len,
361 if (dma_mapping_error(drv_data->tx_dma)) {
362 dma_unmap_single(dev, drv_data->rx_dma,
363 drv_data->rx_map_len, DMA_FROM_DEVICE);
370 static void unmap_dma_buffers(struct driver_data *drv_data)
374 if (!drv_data->dma_mapped)
377 if (!drv_data->cur_msg->is_dma_mapped) {
378 dev = &drv_data->cur_msg->spi->dev;
379 dma_unmap_single(dev, drv_data->rx_dma,
380 drv_data->rx_map_len, DMA_FROM_DEVICE);
381 dma_unmap_single(dev, drv_data->tx_dma,
382 drv_data->tx_map_len, DMA_TO_DEVICE);
385 drv_data->dma_mapped = 0;
388 /* caller already set message->status; dma and pio irqs are blocked */
389 static void giveback(struct driver_data *drv_data)
391 struct spi_transfer* last_transfer;
393 struct spi_message *msg;
395 spin_lock_irqsave(&drv_data->lock, flags);
396 msg = drv_data->cur_msg;
397 drv_data->cur_msg = NULL;
398 drv_data->cur_transfer = NULL;
399 drv_data->cur_chip = NULL;
400 queue_work(drv_data->workqueue, &drv_data->pump_messages);
401 spin_unlock_irqrestore(&drv_data->lock, flags);
403 last_transfer = list_entry(msg->transfers.prev,
407 if (!last_transfer->cs_change)
408 drv_data->cs_control(PXA2XX_CS_DEASSERT);
412 msg->complete(msg->context);
415 static int wait_ssp_rx_stall(void *ioaddr)
417 unsigned long limit = loops_per_jiffy << 1;
419 while ((read_SSSR(ioaddr) & SSSR_BSY) && limit--)
425 static int wait_dma_channel_stop(int channel)
427 unsigned long limit = loops_per_jiffy << 1;
429 while (!(DCSR(channel) & DCSR_STOPSTATE) && limit--)
435 void dma_error_stop(struct driver_data *drv_data, const char *msg)
437 void *reg = drv_data->ioaddr;
440 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
441 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
442 write_SSSR(drv_data->clear_sr, reg);
443 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
444 if (drv_data->ssp_type != PXA25x_SSP)
447 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
449 unmap_dma_buffers(drv_data);
451 dev_err(&drv_data->pdev->dev, "%s\n", msg);
453 drv_data->cur_msg->state = ERROR_STATE;
454 tasklet_schedule(&drv_data->pump_transfers);
457 static void dma_transfer_complete(struct driver_data *drv_data)
459 void *reg = drv_data->ioaddr;
460 struct spi_message *msg = drv_data->cur_msg;
462 /* Clear and disable interrupts on SSP and DMA channels*/
463 write_SSCR1(read_SSCR1(reg) & ~drv_data->dma_cr1, reg);
464 write_SSSR(drv_data->clear_sr, reg);
465 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
466 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
468 if (wait_dma_channel_stop(drv_data->rx_channel) == 0)
469 dev_err(&drv_data->pdev->dev,
470 "dma_handler: dma rx channel stop failed\n");
472 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
473 dev_err(&drv_data->pdev->dev,
474 "dma_transfer: ssp rx stall failed\n");
476 unmap_dma_buffers(drv_data);
478 /* update the buffer pointer for the amount completed in dma */
479 drv_data->rx += drv_data->len -
480 (DCMD(drv_data->rx_channel) & DCMD_LENGTH);
482 /* read trailing data from fifo, it does not matter how many
483 * bytes are in the fifo just read until buffer is full
484 * or fifo is empty, which ever occurs first */
485 drv_data->read(drv_data);
487 /* return count of what was actually read */
488 msg->actual_length += drv_data->len -
489 (drv_data->rx_end - drv_data->rx);
491 /* Release chip select if requested, transfer delays are
492 * handled in pump_transfers */
493 if (drv_data->cs_change)
494 drv_data->cs_control(PXA2XX_CS_DEASSERT);
496 /* Move to next transfer */
497 msg->state = next_transfer(drv_data);
499 /* Schedule transfer tasklet */
500 tasklet_schedule(&drv_data->pump_transfers);
503 static void dma_handler(int channel, void *data)
505 struct driver_data *drv_data = data;
506 u32 irq_status = DCSR(channel) & DMA_INT_MASK;
508 if (irq_status & DCSR_BUSERR) {
510 if (channel == drv_data->tx_channel)
511 dma_error_stop(drv_data,
513 "bad bus address on tx channel");
515 dma_error_stop(drv_data,
517 "bad bus address on rx channel");
521 /* PXA255x_SSP has no timeout interrupt, wait for tailing bytes */
522 if ((channel == drv_data->tx_channel)
523 && (irq_status & DCSR_ENDINTR)
524 && (drv_data->ssp_type == PXA25x_SSP)) {
526 /* Wait for rx to stall */
527 if (wait_ssp_rx_stall(drv_data->ioaddr) == 0)
528 dev_err(&drv_data->pdev->dev,
529 "dma_handler: ssp rx stall failed\n");
531 /* finish this transfer, start the next */
532 dma_transfer_complete(drv_data);
536 static irqreturn_t dma_transfer(struct driver_data *drv_data)
539 void *reg = drv_data->ioaddr;
541 irq_status = read_SSSR(reg) & drv_data->mask_sr;
542 if (irq_status & SSSR_ROR) {
543 dma_error_stop(drv_data, "dma_transfer: fifo overrun");
547 /* Check for false positive timeout */
548 if ((irq_status & SSSR_TINT)
549 && (DCSR(drv_data->tx_channel) & DCSR_RUN)) {
550 write_SSSR(SSSR_TINT, reg);
554 if (irq_status & SSSR_TINT || drv_data->rx == drv_data->rx_end) {
556 /* Clear and disable timeout interrupt, do the rest in
557 * dma_transfer_complete */
558 if (drv_data->ssp_type != PXA25x_SSP)
561 /* finish this transfer, start the next */
562 dma_transfer_complete(drv_data);
567 /* Opps problem detected */
571 static void int_error_stop(struct driver_data *drv_data, const char* msg)
573 void *reg = drv_data->ioaddr;
575 /* Stop and reset SSP */
576 write_SSSR(drv_data->clear_sr, reg);
577 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
578 if (drv_data->ssp_type != PXA25x_SSP)
581 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
583 dev_err(&drv_data->pdev->dev, "%s\n", msg);
585 drv_data->cur_msg->state = ERROR_STATE;
586 tasklet_schedule(&drv_data->pump_transfers);
589 static void int_transfer_complete(struct driver_data *drv_data)
591 void *reg = drv_data->ioaddr;
594 write_SSSR(drv_data->clear_sr, reg);
595 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
596 if (drv_data->ssp_type != PXA25x_SSP)
599 /* Update total byte transfered return count actual bytes read */
600 drv_data->cur_msg->actual_length += drv_data->len -
601 (drv_data->rx_end - drv_data->rx);
603 /* Release chip select if requested, transfer delays are
604 * handled in pump_transfers */
605 if (drv_data->cs_change)
606 drv_data->cs_control(PXA2XX_CS_DEASSERT);
608 /* Move to next transfer */
609 drv_data->cur_msg->state = next_transfer(drv_data);
611 /* Schedule transfer tasklet */
612 tasklet_schedule(&drv_data->pump_transfers);
615 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
617 void *reg = drv_data->ioaddr;
619 u32 irq_mask = (read_SSCR1(reg) & SSCR1_TIE) ?
620 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
622 u32 irq_status = read_SSSR(reg) & irq_mask;
624 if (irq_status & SSSR_ROR) {
625 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
629 if (irq_status & SSSR_TINT) {
630 write_SSSR(SSSR_TINT, reg);
631 if (drv_data->read(drv_data)) {
632 int_transfer_complete(drv_data);
637 /* Drain rx fifo, Fill tx fifo and prevent overruns */
639 if (drv_data->read(drv_data)) {
640 int_transfer_complete(drv_data);
643 } while (drv_data->write(drv_data));
645 if (drv_data->read(drv_data)) {
646 int_transfer_complete(drv_data);
650 if (drv_data->tx == drv_data->tx_end) {
651 write_SSCR1(read_SSCR1(reg) & ~SSCR1_TIE, reg);
652 /* PXA25x_SSP has no timeout, read trailing bytes */
653 if (drv_data->ssp_type == PXA25x_SSP) {
654 if (!wait_ssp_rx_stall(reg))
656 int_error_stop(drv_data, "interrupt_transfer: "
660 if (!drv_data->read(drv_data))
662 int_error_stop(drv_data,
663 "interrupt_transfer: "
664 "trailing byte read failed");
667 int_transfer_complete(drv_data);
671 /* We did something */
675 static irqreturn_t ssp_int(int irq, void *dev_id)
677 struct driver_data *drv_data = dev_id;
678 void *reg = drv_data->ioaddr;
680 if (!drv_data->cur_msg) {
682 write_SSCR0(read_SSCR0(reg) & ~SSCR0_SSE, reg);
683 write_SSCR1(read_SSCR1(reg) & ~drv_data->int_cr1, reg);
684 if (drv_data->ssp_type != PXA25x_SSP)
686 write_SSSR(drv_data->clear_sr, reg);
688 dev_err(&drv_data->pdev->dev, "bad message state "
689 "in interrupt handler\n");
695 return drv_data->transfer_handler(drv_data);
698 int set_dma_burst_and_threshold(struct chip_data *chip, struct spi_device *spi,
699 u8 bits_per_word, u32 *burst_code,
702 struct pxa2xx_spi_chip *chip_info =
703 (struct pxa2xx_spi_chip *)spi->controller_data;
710 /* Set the threshold (in registers) to equal the same amount of data
711 * as represented by burst size (in bytes). The computation below
712 * is (burst_size rounded up to nearest 8 byte, word or long word)
713 * divided by (bytes/register); the tx threshold is the inverse of
714 * the rx, so that there will always be enough data in the rx fifo
715 * to satisfy a burst, and there will always be enough space in the
716 * tx fifo to accept a burst (a tx burst will overwrite the fifo if
717 * there is not enough space), there must always remain enough empty
718 * space in the rx fifo for any data loaded to the tx fifo.
719 * Whenever burst_size (in bytes) equals bits/word, the fifo threshold
720 * will be 8, or half the fifo;
721 * The threshold can only be set to 2, 4 or 8, but not 16, because
722 * to burst 16 to the tx fifo, the fifo would have to be empty;
723 * however, the minimum fifo trigger level is 1, and the tx will
724 * request service when the fifo is at this level, with only 15 spaces.
727 /* find bytes/word */
728 if (bits_per_word <= 8)
730 else if (bits_per_word <= 16)
735 /* use struct pxa2xx_spi_chip->dma_burst_size if available */
737 req_burst_size = chip_info->dma_burst_size;
739 switch (chip->dma_burst_size) {
741 /* if the default burst size is not set,
743 chip->dma_burst_size = DCMD_BURST8;
755 if (req_burst_size <= 8) {
756 *burst_code = DCMD_BURST8;
758 } else if (req_burst_size <= 16) {
759 if (bytes_per_word == 1) {
760 /* don't burst more than 1/2 the fifo */
761 *burst_code = DCMD_BURST8;
765 *burst_code = DCMD_BURST16;
769 if (bytes_per_word == 1) {
770 /* don't burst more than 1/2 the fifo */
771 *burst_code = DCMD_BURST8;
774 } else if (bytes_per_word == 2) {
775 /* don't burst more than 1/2 the fifo */
776 *burst_code = DCMD_BURST16;
780 *burst_code = DCMD_BURST32;
785 thresh_words = burst_bytes / bytes_per_word;
787 /* thresh_words will be between 2 and 8 */
788 *threshold = (SSCR1_RxTresh(thresh_words) & SSCR1_RFT)
789 | (SSCR1_TxTresh(16-thresh_words) & SSCR1_TFT);
794 static unsigned int ssp_get_clk_div(struct ssp_device *ssp, int rate)
796 unsigned long ssp_clk = clk_get_rate(ssp->clk);
798 if (ssp->type == PXA25x_SSP)
799 return ((ssp_clk / (2 * rate) - 1) & 0xff) << 8;
801 return ((ssp_clk / rate - 1) & 0xfff) << 8;
804 static void pump_transfers(unsigned long data)
806 struct driver_data *drv_data = (struct driver_data *)data;
807 struct spi_message *message = NULL;
808 struct spi_transfer *transfer = NULL;
809 struct spi_transfer *previous = NULL;
810 struct chip_data *chip = NULL;
811 struct ssp_device *ssp = drv_data->ssp;
812 void *reg = drv_data->ioaddr;
818 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
819 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
821 /* Get current state information */
822 message = drv_data->cur_msg;
823 transfer = drv_data->cur_transfer;
824 chip = drv_data->cur_chip;
826 /* Handle for abort */
827 if (message->state == ERROR_STATE) {
828 message->status = -EIO;
833 /* Handle end of message */
834 if (message->state == DONE_STATE) {
840 /* Delay if requested at end of transfer*/
841 if (message->state == RUNNING_STATE) {
842 previous = list_entry(transfer->transfer_list.prev,
845 if (previous->delay_usecs)
846 udelay(previous->delay_usecs);
849 /* Check transfer length */
850 if (transfer->len > 8191)
852 dev_warn(&drv_data->pdev->dev, "pump_transfers: transfer "
853 "length greater than 8191\n");
854 message->status = -EINVAL;
859 /* Setup the transfer state based on the type of transfer */
860 if (flush(drv_data) == 0) {
861 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
862 message->status = -EIO;
866 drv_data->n_bytes = chip->n_bytes;
867 drv_data->dma_width = chip->dma_width;
868 drv_data->cs_control = chip->cs_control;
869 drv_data->tx = (void *)transfer->tx_buf;
870 drv_data->tx_end = drv_data->tx + transfer->len;
871 drv_data->rx = transfer->rx_buf;
872 drv_data->rx_end = drv_data->rx + transfer->len;
873 drv_data->rx_dma = transfer->rx_dma;
874 drv_data->tx_dma = transfer->tx_dma;
875 drv_data->len = transfer->len & DCMD_LENGTH;
876 drv_data->write = drv_data->tx ? chip->write : null_writer;
877 drv_data->read = drv_data->rx ? chip->read : null_reader;
878 drv_data->cs_change = transfer->cs_change;
880 /* Change speed and bit per word on a per transfer */
882 if (transfer->speed_hz || transfer->bits_per_word) {
884 bits = chip->bits_per_word;
885 speed = chip->speed_hz;
887 if (transfer->speed_hz)
888 speed = transfer->speed_hz;
890 if (transfer->bits_per_word)
891 bits = transfer->bits_per_word;
893 clk_div = ssp_get_clk_div(ssp, speed);
896 drv_data->n_bytes = 1;
897 drv_data->dma_width = DCMD_WIDTH1;
898 drv_data->read = drv_data->read != null_reader ?
899 u8_reader : null_reader;
900 drv_data->write = drv_data->write != null_writer ?
901 u8_writer : null_writer;
902 } else if (bits <= 16) {
903 drv_data->n_bytes = 2;
904 drv_data->dma_width = DCMD_WIDTH2;
905 drv_data->read = drv_data->read != null_reader ?
906 u16_reader : null_reader;
907 drv_data->write = drv_data->write != null_writer ?
908 u16_writer : null_writer;
909 } else if (bits <= 32) {
910 drv_data->n_bytes = 4;
911 drv_data->dma_width = DCMD_WIDTH4;
912 drv_data->read = drv_data->read != null_reader ?
913 u32_reader : null_reader;
914 drv_data->write = drv_data->write != null_writer ?
915 u32_writer : null_writer;
917 /* if bits/word is changed in dma mode, then must check the
918 * thresholds and burst also */
919 if (chip->enable_dma) {
920 if (set_dma_burst_and_threshold(chip, message->spi,
923 if (printk_ratelimit())
924 dev_warn(&message->spi->dev,
926 "DMA burst size reduced to "
927 "match bits_per_word\n");
932 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
934 | (bits > 16 ? SSCR0_EDSS : 0);
937 message->state = RUNNING_STATE;
939 /* Try to map dma buffer and do a dma transfer if successful */
940 if ((drv_data->dma_mapped = map_dma_buffers(drv_data))) {
942 /* Ensure we have the correct interrupt handler */
943 drv_data->transfer_handler = dma_transfer;
945 /* Setup rx DMA Channel */
946 DCSR(drv_data->rx_channel) = RESET_DMA_CHANNEL;
947 DSADR(drv_data->rx_channel) = drv_data->ssdr_physical;
948 DTADR(drv_data->rx_channel) = drv_data->rx_dma;
949 if (drv_data->rx == drv_data->null_dma_buf)
950 /* No target address increment */
951 DCMD(drv_data->rx_channel) = DCMD_FLOWSRC
952 | drv_data->dma_width
956 DCMD(drv_data->rx_channel) = DCMD_INCTRGADDR
958 | drv_data->dma_width
962 /* Setup tx DMA Channel */
963 DCSR(drv_data->tx_channel) = RESET_DMA_CHANNEL;
964 DSADR(drv_data->tx_channel) = drv_data->tx_dma;
965 DTADR(drv_data->tx_channel) = drv_data->ssdr_physical;
966 if (drv_data->tx == drv_data->null_dma_buf)
967 /* No source address increment */
968 DCMD(drv_data->tx_channel) = DCMD_FLOWTRG
969 | drv_data->dma_width
973 DCMD(drv_data->tx_channel) = DCMD_INCSRCADDR
975 | drv_data->dma_width
979 /* Enable dma end irqs on SSP to detect end of transfer */
980 if (drv_data->ssp_type == PXA25x_SSP)
981 DCMD(drv_data->tx_channel) |= DCMD_ENDIRQEN;
983 /* Clear status and start DMA engine */
984 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
985 write_SSSR(drv_data->clear_sr, reg);
986 DCSR(drv_data->rx_channel) |= DCSR_RUN;
987 DCSR(drv_data->tx_channel) |= DCSR_RUN;
989 /* Ensure we have the correct interrupt handler */
990 drv_data->transfer_handler = interrupt_transfer;
993 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
994 write_SSSR(drv_data->clear_sr, reg);
997 /* see if we need to reload the config registers */
998 if ((read_SSCR0(reg) != cr0)
999 || (read_SSCR1(reg) & SSCR1_CHANGE_MASK) !=
1000 (cr1 & SSCR1_CHANGE_MASK)) {
1002 /* stop the SSP, and update the other bits */
1003 write_SSCR0(cr0 & ~SSCR0_SSE, reg);
1004 if (drv_data->ssp_type != PXA25x_SSP)
1005 write_SSTO(chip->timeout, reg);
1006 /* first set CR1 without interrupt and service enables */
1007 write_SSCR1(cr1 & SSCR1_CHANGE_MASK, reg);
1008 /* restart the SSP */
1009 write_SSCR0(cr0, reg);
1012 if (drv_data->ssp_type != PXA25x_SSP)
1013 write_SSTO(chip->timeout, reg);
1016 /* FIXME, need to handle cs polarity,
1017 * this driver uses struct pxa2xx_spi_chip.cs_control to
1018 * specify a CS handling function, and it ignores most
1019 * struct spi_device.mode[s], including SPI_CS_HIGH */
1020 drv_data->cs_control(PXA2XX_CS_ASSERT);
1022 /* after chip select, release the data by enabling service
1023 * requests and interrupts, without changing any mode bits */
1024 write_SSCR1(cr1, reg);
1027 static void pump_messages(struct work_struct *work)
1029 struct driver_data *drv_data =
1030 container_of(work, struct driver_data, pump_messages);
1031 unsigned long flags;
1033 /* Lock queue and check for queue work */
1034 spin_lock_irqsave(&drv_data->lock, flags);
1035 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
1037 spin_unlock_irqrestore(&drv_data->lock, flags);
1041 /* Make sure we are not already running a message */
1042 if (drv_data->cur_msg) {
1043 spin_unlock_irqrestore(&drv_data->lock, flags);
1047 /* Extract head of queue */
1048 drv_data->cur_msg = list_entry(drv_data->queue.next,
1049 struct spi_message, queue);
1050 list_del_init(&drv_data->cur_msg->queue);
1052 /* Initial message state*/
1053 drv_data->cur_msg->state = START_STATE;
1054 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1055 struct spi_transfer,
1058 /* prepare to setup the SSP, in pump_transfers, using the per
1059 * chip configuration */
1060 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
1062 /* Mark as busy and launch transfers */
1063 tasklet_schedule(&drv_data->pump_transfers);
1066 spin_unlock_irqrestore(&drv_data->lock, flags);
1069 static int transfer(struct spi_device *spi, struct spi_message *msg)
1071 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1072 unsigned long flags;
1074 spin_lock_irqsave(&drv_data->lock, flags);
1076 if (drv_data->run == QUEUE_STOPPED) {
1077 spin_unlock_irqrestore(&drv_data->lock, flags);
1081 msg->actual_length = 0;
1082 msg->status = -EINPROGRESS;
1083 msg->state = START_STATE;
1085 list_add_tail(&msg->queue, &drv_data->queue);
1087 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1088 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1090 spin_unlock_irqrestore(&drv_data->lock, flags);
1095 /* the spi->mode bits understood by this driver: */
1096 #define MODEBITS (SPI_CPOL | SPI_CPHA)
1098 static int setup(struct spi_device *spi)
1100 struct pxa2xx_spi_chip *chip_info = NULL;
1101 struct chip_data *chip;
1102 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1103 struct ssp_device *ssp = drv_data->ssp;
1104 unsigned int clk_div;
1106 if (!spi->bits_per_word)
1107 spi->bits_per_word = 8;
1109 if (drv_data->ssp_type != PXA25x_SSP
1110 && (spi->bits_per_word < 4 || spi->bits_per_word > 32)) {
1111 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1112 "b/w not 4-32 for type non-PXA25x_SSP\n",
1113 drv_data->ssp_type, spi->bits_per_word);
1116 else if (drv_data->ssp_type == PXA25x_SSP
1117 && (spi->bits_per_word < 4
1118 || spi->bits_per_word > 16)) {
1119 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d "
1120 "b/w not 4-16 for type PXA25x_SSP\n",
1121 drv_data->ssp_type, spi->bits_per_word);
1125 if (spi->mode & ~MODEBITS) {
1126 dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n",
1127 spi->mode & ~MODEBITS);
1131 /* Only alloc on first setup */
1132 chip = spi_get_ctldata(spi);
1134 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1137 "failed setup: can't allocate chip data\n");
1141 chip->cs_control = null_cs_control;
1142 chip->enable_dma = 0;
1143 chip->timeout = 1000;
1144 chip->threshold = SSCR1_RxTresh(1) | SSCR1_TxTresh(1);
1145 chip->dma_burst_size = drv_data->master_info->enable_dma ?
1149 /* protocol drivers may change the chip settings, so...
1150 * if chip_info exists, use it */
1151 chip_info = spi->controller_data;
1153 /* chip_info isn't always needed */
1156 if (chip_info->cs_control)
1157 chip->cs_control = chip_info->cs_control;
1159 chip->timeout = chip_info->timeout;
1161 chip->threshold = (SSCR1_RxTresh(chip_info->rx_threshold) &
1163 (SSCR1_TxTresh(chip_info->tx_threshold) &
1166 chip->enable_dma = chip_info->dma_burst_size != 0
1167 && drv_data->master_info->enable_dma;
1168 chip->dma_threshold = 0;
1170 if (chip_info->enable_loopback)
1171 chip->cr1 = SSCR1_LBM;
1174 /* set dma burst and threshold outside of chip_info path so that if
1175 * chip_info goes away after setting chip->enable_dma, the
1176 * burst and threshold can still respond to changes in bits_per_word */
1177 if (chip->enable_dma) {
1178 /* set up legal burst and threshold for dma */
1179 if (set_dma_burst_and_threshold(chip, spi, spi->bits_per_word,
1180 &chip->dma_burst_size,
1181 &chip->dma_threshold)) {
1182 dev_warn(&spi->dev, "in setup: DMA burst size reduced "
1183 "to match bits_per_word\n");
1187 clk_div = ssp_get_clk_div(ssp, spi->max_speed_hz);
1188 chip->speed_hz = spi->max_speed_hz;
1192 | SSCR0_DataSize(spi->bits_per_word > 16 ?
1193 spi->bits_per_word - 16 : spi->bits_per_word)
1195 | (spi->bits_per_word > 16 ? SSCR0_EDSS : 0);
1196 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1197 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1198 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1200 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1201 if (drv_data->ssp_type != PXA25x_SSP)
1202 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d\n",
1204 clk_get_rate(ssp->clk)
1205 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1208 dev_dbg(&spi->dev, "%d bits/word, %ld Hz, mode %d\n",
1210 clk_get_rate(ssp->clk)
1211 / (1 + ((chip->cr0 & SSCR0_SCR) >> 8)),
1214 if (spi->bits_per_word <= 8) {
1216 chip->dma_width = DCMD_WIDTH1;
1217 chip->read = u8_reader;
1218 chip->write = u8_writer;
1219 } else if (spi->bits_per_word <= 16) {
1221 chip->dma_width = DCMD_WIDTH2;
1222 chip->read = u16_reader;
1223 chip->write = u16_writer;
1224 } else if (spi->bits_per_word <= 32) {
1225 chip->cr0 |= SSCR0_EDSS;
1227 chip->dma_width = DCMD_WIDTH4;
1228 chip->read = u32_reader;
1229 chip->write = u32_writer;
1231 dev_err(&spi->dev, "invalid wordsize\n");
1234 chip->bits_per_word = spi->bits_per_word;
1236 spi_set_ctldata(spi, chip);
1241 static void cleanup(struct spi_device *spi)
1243 struct chip_data *chip = spi_get_ctldata(spi);
1248 static int __init init_queue(struct driver_data *drv_data)
1250 INIT_LIST_HEAD(&drv_data->queue);
1251 spin_lock_init(&drv_data->lock);
1253 drv_data->run = QUEUE_STOPPED;
1256 tasklet_init(&drv_data->pump_transfers,
1257 pump_transfers, (unsigned long)drv_data);
1259 INIT_WORK(&drv_data->pump_messages, pump_messages);
1260 drv_data->workqueue = create_singlethread_workqueue(
1261 drv_data->master->dev.parent->bus_id);
1262 if (drv_data->workqueue == NULL)
1268 static int start_queue(struct driver_data *drv_data)
1270 unsigned long flags;
1272 spin_lock_irqsave(&drv_data->lock, flags);
1274 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1275 spin_unlock_irqrestore(&drv_data->lock, flags);
1279 drv_data->run = QUEUE_RUNNING;
1280 drv_data->cur_msg = NULL;
1281 drv_data->cur_transfer = NULL;
1282 drv_data->cur_chip = NULL;
1283 spin_unlock_irqrestore(&drv_data->lock, flags);
1285 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1290 static int stop_queue(struct driver_data *drv_data)
1292 unsigned long flags;
1293 unsigned limit = 500;
1296 spin_lock_irqsave(&drv_data->lock, flags);
1298 /* This is a bit lame, but is optimized for the common execution path.
1299 * A wait_queue on the drv_data->busy could be used, but then the common
1300 * execution path (pump_messages) would be required to call wake_up or
1301 * friends on every SPI message. Do this instead */
1302 drv_data->run = QUEUE_STOPPED;
1303 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1304 spin_unlock_irqrestore(&drv_data->lock, flags);
1306 spin_lock_irqsave(&drv_data->lock, flags);
1309 if (!list_empty(&drv_data->queue) || drv_data->busy)
1312 spin_unlock_irqrestore(&drv_data->lock, flags);
1317 static int destroy_queue(struct driver_data *drv_data)
1321 status = stop_queue(drv_data);
1322 /* we are unloading the module or failing to load (only two calls
1323 * to this routine), and neither call can handle a return value.
1324 * However, destroy_workqueue calls flush_workqueue, and that will
1325 * block until all work is done. If the reason that stop_queue
1326 * timed out is that the work will never finish, then it does no
1327 * good to call destroy_workqueue, so return anyway. */
1331 destroy_workqueue(drv_data->workqueue);
1336 static int __init pxa2xx_spi_probe(struct platform_device *pdev)
1338 struct device *dev = &pdev->dev;
1339 struct pxa2xx_spi_master *platform_info;
1340 struct spi_master *master;
1341 struct driver_data *drv_data = 0;
1342 struct ssp_device *ssp;
1345 platform_info = dev->platform_data;
1347 ssp = ssp_request(pdev->id, pdev->name);
1349 dev_err(&pdev->dev, "failed to request SSP%d\n", pdev->id);
1353 /* Allocate master with space for drv_data and null dma buffer */
1354 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1356 dev_err(&pdev->dev, "can not alloc spi_master\n");
1360 drv_data = spi_master_get_devdata(master);
1361 drv_data->master = master;
1362 drv_data->master_info = platform_info;
1363 drv_data->pdev = pdev;
1364 drv_data->ssp = ssp;
1366 master->bus_num = pdev->id;
1367 master->num_chipselect = platform_info->num_chipselect;
1368 master->cleanup = cleanup;
1369 master->setup = setup;
1370 master->transfer = transfer;
1372 drv_data->ssp_type = ssp->type;
1373 drv_data->null_dma_buf = (u32 *)ALIGN((u32)(drv_data +
1374 sizeof(struct driver_data)), 8);
1376 drv_data->ioaddr = ssp->mmio_base;
1377 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1378 if (ssp->type == PXA25x_SSP) {
1379 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1380 drv_data->dma_cr1 = 0;
1381 drv_data->clear_sr = SSSR_ROR;
1382 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1384 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1385 drv_data->dma_cr1 = SSCR1_TSRE | SSCR1_RSRE | SSCR1_TINTE;
1386 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1387 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1390 status = request_irq(ssp->irq, ssp_int, 0, dev->bus_id, drv_data);
1392 dev_err(&pdev->dev, "can not get IRQ\n");
1393 goto out_error_master_alloc;
1396 /* Setup DMA if requested */
1397 drv_data->tx_channel = -1;
1398 drv_data->rx_channel = -1;
1399 if (platform_info->enable_dma) {
1401 /* Get two DMA channels (rx and tx) */
1402 drv_data->rx_channel = pxa_request_dma("pxa2xx_spi_ssp_rx",
1406 if (drv_data->rx_channel < 0) {
1407 dev_err(dev, "problem (%d) requesting rx channel\n",
1408 drv_data->rx_channel);
1410 goto out_error_irq_alloc;
1412 drv_data->tx_channel = pxa_request_dma("pxa2xx_spi_ssp_tx",
1416 if (drv_data->tx_channel < 0) {
1417 dev_err(dev, "problem (%d) requesting tx channel\n",
1418 drv_data->tx_channel);
1420 goto out_error_dma_alloc;
1423 DRCMR(ssp->drcmr_rx) = DRCMR_MAPVLD | drv_data->rx_channel;
1424 DRCMR(ssp->drcmr_tx) = DRCMR_MAPVLD | drv_data->tx_channel;
1427 /* Enable SOC clock */
1428 clk_enable(ssp->clk);
1430 /* Load default SSP configuration */
1431 write_SSCR0(0, drv_data->ioaddr);
1432 write_SSCR1(SSCR1_RxTresh(4) | SSCR1_TxTresh(12), drv_data->ioaddr);
1433 write_SSCR0(SSCR0_SerClkDiv(2)
1435 | SSCR0_DataSize(8),
1437 if (drv_data->ssp_type != PXA25x_SSP)
1438 write_SSTO(0, drv_data->ioaddr);
1439 write_SSPSP(0, drv_data->ioaddr);
1441 /* Initial and start queue */
1442 status = init_queue(drv_data);
1444 dev_err(&pdev->dev, "problem initializing queue\n");
1445 goto out_error_clock_enabled;
1447 status = start_queue(drv_data);
1449 dev_err(&pdev->dev, "problem starting queue\n");
1450 goto out_error_clock_enabled;
1453 /* Register with the SPI framework */
1454 platform_set_drvdata(pdev, drv_data);
1455 status = spi_register_master(master);
1457 dev_err(&pdev->dev, "problem registering spi master\n");
1458 goto out_error_queue_alloc;
1463 out_error_queue_alloc:
1464 destroy_queue(drv_data);
1466 out_error_clock_enabled:
1467 clk_disable(ssp->clk);
1469 out_error_dma_alloc:
1470 if (drv_data->tx_channel != -1)
1471 pxa_free_dma(drv_data->tx_channel);
1472 if (drv_data->rx_channel != -1)
1473 pxa_free_dma(drv_data->rx_channel);
1475 out_error_irq_alloc:
1476 free_irq(ssp->irq, drv_data);
1478 out_error_master_alloc:
1479 spi_master_put(master);
1484 static int pxa2xx_spi_remove(struct platform_device *pdev)
1486 struct driver_data *drv_data = platform_get_drvdata(pdev);
1487 struct ssp_device *ssp = drv_data->ssp;
1493 /* Remove the queue */
1494 status = destroy_queue(drv_data);
1496 /* the kernel does not check the return status of this
1497 * this routine (mod->exit, within the kernel). Therefore
1498 * nothing is gained by returning from here, the module is
1499 * going away regardless, and we should not leave any more
1500 * resources allocated than necessary. We cannot free the
1501 * message memory in drv_data->queue, but we can release the
1502 * resources below. I think the kernel should honor -EBUSY
1504 dev_err(&pdev->dev, "pxa2xx_spi_remove: workqueue will not "
1505 "complete, message memory not freed\n");
1507 /* Disable the SSP at the peripheral and SOC level */
1508 write_SSCR0(0, drv_data->ioaddr);
1509 clk_disable(ssp->clk);
1512 if (drv_data->master_info->enable_dma) {
1513 DRCMR(ssp->drcmr_rx) = 0;
1514 DRCMR(ssp->drcmr_tx) = 0;
1515 pxa_free_dma(drv_data->tx_channel);
1516 pxa_free_dma(drv_data->rx_channel);
1520 free_irq(ssp->irq, drv_data);
1525 /* Disconnect from the SPI framework */
1526 spi_unregister_master(drv_data->master);
1528 /* Prevent double remove */
1529 platform_set_drvdata(pdev, NULL);
1534 static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1538 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1539 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1544 static int pxa2xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1546 struct driver_data *drv_data = platform_get_drvdata(pdev);
1547 struct ssp_device *ssp = drv_data->ssp;
1550 status = stop_queue(drv_data);
1553 write_SSCR0(0, drv_data->ioaddr);
1554 clk_disable(ssp->clk);
1559 static int pxa2xx_spi_resume(struct platform_device *pdev)
1561 struct driver_data *drv_data = platform_get_drvdata(pdev);
1562 struct ssp_device *ssp = drv_data->ssp;
1565 /* Enable the SSP clock */
1566 clk_disable(ssp->clk);
1568 /* Start the queue running */
1569 status = start_queue(drv_data);
1571 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1578 #define pxa2xx_spi_suspend NULL
1579 #define pxa2xx_spi_resume NULL
1580 #endif /* CONFIG_PM */
1582 static struct platform_driver driver = {
1584 .name = "pxa2xx-spi",
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);