31 #include <linux/kernel.h>
33 #include <linux/module.h>
38 #include <linux/slab.h>
40 #include <linux/types.h>
43 #define DRIVER_NAME "arasan_cf"
44 #define TIMEOUT msecs_to_jiffies(3000)
50 #define BIN_AUDIO_OUT (1 << 1)
51 #define CARD_DETECT1 (1 << 2)
52 #define CARD_DETECT2 (1 << 3)
53 #define INP_ACK (1 << 4)
54 #define CARD_READY (1 << 5)
55 #define IO_READY (1 << 6)
56 #define B16_IO_PORT_SEL (1 << 7)
61 #define CARD_DETECT_IRQ (1)
62 #define STATUS_CHNG_IRQ (1 << 1)
63 #define MEM_MODE_IRQ (1 << 2)
64 #define IO_MODE_IRQ (1 << 3)
65 #define TRUE_IDE_MODE_IRQ (1 << 8)
66 #define PIO_XFER_ERR_IRQ (1 << 9)
67 #define BUF_AVAIL_IRQ (1 << 10)
68 #define XFER_DONE_IRQ (1 << 11)
69 #define IGNORED_IRQS (STATUS_CHNG_IRQ | MEM_MODE_IRQ | IO_MODE_IRQ |\
71 #define TRUE_IDE_IRQS (CARD_DETECT_IRQ | PIO_XFER_ERR_IRQ |\
72 BUF_AVAIL_IRQ | XFER_DONE_IRQ)
75 #define CARD_MODE_MASK (0x3)
76 #define MEM_MODE (0x0)
78 #define TRUE_IDE_MODE (0x2)
80 #define CARD_TYPE_MASK (1 << 2)
82 #define CF_PLUS_CARD (1 << 2)
84 #define CARD_RESET (1 << 3)
85 #define CFHOST_ENB (1 << 4)
86 #define OUTPUTS_TRISTATE (1 << 5)
87 #define ULTRA_DMA_ENB (1 << 8)
88 #define MULTI_WORD_DMA_ENB (1 << 9)
89 #define DRQ_BLOCK_SIZE_MASK (0x3 << 11)
90 #define DRQ_BLOCK_SIZE_512 (0)
91 #define DRQ_BLOCK_SIZE_1024 (1 << 11)
92 #define DRQ_BLOCK_SIZE_2048 (2 << 11)
93 #define DRQ_BLOCK_SIZE_4096 (3 << 11)
96 #define CF_IF_CLK_MASK (0XF)
99 #define MEM_MODE_TIMING_MASK (0x3)
100 #define MEM_MODE_TIMING_250NS (0x0)
101 #define MEM_MODE_TIMING_120NS (0x1)
102 #define MEM_MODE_TIMING_100NS (0x2)
103 #define MEM_MODE_TIMING_80NS (0x3)
105 #define IO_MODE_TIMING_MASK (0x3 << 2)
106 #define IO_MODE_TIMING_250NS (0x0 << 2)
107 #define IO_MODE_TIMING_120NS (0x1 << 2)
108 #define IO_MODE_TIMING_100NS (0x2 << 2)
109 #define IO_MODE_TIMING_80NS (0x3 << 2)
111 #define TRUEIDE_PIO_TIMING_MASK (0x7 << 4)
112 #define TRUEIDE_PIO_TIMING_SHIFT 4
114 #define TRUEIDE_MWORD_DMA_TIMING_MASK (0x7 << 7)
115 #define TRUEIDE_MWORD_DMA_TIMING_SHIFT 7
117 #define ULTRA_DMA_TIMING_MASK (0x7 << 10)
118 #define ULTRA_DMA_TIMING_SHIFT 10
120 #define XFER_ADDR 0x014
121 #define XFER_ADDR_MASK (0x7FF)
122 #define MAX_XFER_COUNT 0x20000u
124 #define XFER_CTR 0x01C
125 #define XFER_COUNT_MASK (0x3FFFF)
126 #define ADDR_INC_DISABLE (1 << 24)
127 #define XFER_WIDTH_MASK (1 << 25)
128 #define XFER_WIDTH_8B (0)
129 #define XFER_WIDTH_16B (1 << 25)
131 #define MEM_TYPE_MASK (1 << 26)
132 #define MEM_TYPE_COMMON (0)
133 #define MEM_TYPE_ATTRIBUTE (1 << 26)
135 #define MEM_IO_XFER_MASK (1 << 27)
137 #define IO_XFER (1 << 27)
139 #define DMA_XFER_MODE (1 << 28)
141 #define AHB_BUS_NORMAL_PIO_OPRTN (~(1 << 29))
142 #define XFER_DIR_MASK (1 << 30)
143 #define XFER_READ (0)
144 #define XFER_WRITE (1 << 30)
146 #define XFER_START (1 << 31)
148 #define WRITE_PORT 0x024
150 #define READ_PORT 0x028
152 #define ATA_DATA_PORT 0x030
153 #define ATA_DATA_PORT_MASK (0xFFFF)
155 #define ATA_ERR_FTR 0x034
167 #define ATA_STS_CMD 0x04C
169 #define ATA_ASTS_DCTR 0x050
171 #define EXT_WRITE_PORT 0x200
173 #define EXT_READ_PORT 0x400
174 #define FIFO_SIZE 0x200u
176 #define GIRQ_STS 0x800
178 #define GIRQ_STS_EN 0x804
180 #define GIRQ_SGN_EN 0x808
182 #define GIRQ_XD (1 << 1)
225 .dma_boundary = 0xFFFFFFFF
UL,
232 dev_dbg(dev,
": =========== REGISTER DUMP ===========");
243 dev_dbg(dev,
": =====================================");
267 static inline void cf_card_reset(
struct arasan_cf_dev *acdev)
276 static inline void cf_ctrl_reset(
struct arasan_cf_dev *acdev)
284 static void cf_card_detect(
struct arasan_cf_dev *acdev,
bool hotplugged)
295 cf_card_reset(acdev);
303 ata_ehi_hotplugged(ehi);
314 ret = clk_prepare_enable(acdev->
clk);
333 cf_ginterrupt_enable(acdev, 1);
334 spin_unlock_irqrestore(&acdev->
host->lock, flags);
344 cf_ginterrupt_enable(acdev, 0);
346 cf_card_reset(acdev);
349 spin_unlock_irqrestore(&acdev->
host->lock, flags);
350 clk_disable_unprepare(acdev->
clk);
353 static void dma_callback(
void *dev)
377 spin_unlock_irqrestore(&acdev->
host->lock, flags);
385 dev_err(acdev->
host->dev,
"%s TimeOut", rw ?
"write" :
"read");
406 tx = chan->
device->device_prep_dma_memcpy(chan, dest, src, len, flags);
408 dev_err(acdev->
host->dev,
"device_prep_dma_memcpy failed\n");
422 chan->
device->device_issue_pending(chan);
427 dev_err(acdev->
host->dev,
"wait_for_completion_timeout\n");
437 u32 xfer_cnt, sglen, dma_len, xfer_ctr;
465 spin_unlock_irqrestore(&acdev->
host->lock, flags);
471 ret = wait4buf(acdev);
478 ret =
dma_xfer(acdev, src, dest, dma_len);
494 ret = wait4buf(acdev);
505 spin_unlock_irqrestore(&acdev->
host->lock, flags);
536 dev_err(acdev->
host->dev,
"Unable to get dma_chan\n");
537 goto chan_request_fail;
541 ret = sg_xfer(acdev, sg);
553 status =
ioread8(qc->
ap->ioaddr.altstatus_addr);
554 spin_unlock_irqrestore(&acdev->
host->lock, flags);
571 cf_ctrl_reset(acdev);
572 spin_unlock_irqrestore(qc->
ap->lock, flags);
577 static void delayed_finish(
struct work_struct *work)
586 status =
ioread8(qc->
ap->ioaddr.altstatus_addr);
587 spin_unlock_irqrestore(&acdev->
host->lock, flags);
595 static irqreturn_t arasan_cf_interrupt(
int irq,
void *dev)
614 cf_card_detect(acdev, 1);
615 spin_unlock_irqrestore(&acdev->
host->lock, flags);
623 spin_unlock_irqrestore(&acdev->
host->lock, flags);
629 spin_unlock_irqrestore(&acdev->
host->lock, flags);
647 static void arasan_cf_freeze(
struct ata_port *ap)
654 cf_ctrl_reset(acdev);
683 acdev->
qc->ap->ops->sff_exec_command(acdev->
qc->ap, &acdev->
qc->tf);
693 if (!ata_is_dma(qc->
tf.protocol))
702 switch (qc->
tf.protocol) {
706 ap->
ops->sff_tf_load(ap, &qc->
tf);
709 arasan_cf_dma_start(acdev);
742 cf_interrupt_enable(acdev, BUF_AVAIL_IRQ | XFER_DONE_IRQ, 0);
743 cf_interrupt_enable(acdev, PIO_XFER_ERR_IRQ, 1);
744 spin_unlock_irqrestore(&acdev->
host->lock, flags);
769 spin_unlock_irqrestore(&acdev->
host->lock, flags);
777 cf_interrupt_enable(acdev, PIO_XFER_ERR_IRQ, 0);
778 cf_interrupt_enable(acdev, BUF_AVAIL_IRQ | XFER_DONE_IRQ, 1);
779 spin_unlock_irqrestore(&acdev->
host->lock, flags);
784 .freeze = arasan_cf_freeze,
787 .set_piomode = arasan_cf_set_piomode,
788 .set_dmamode = arasan_cf_set_dmamode,
807 dev_warn(&pdev->
dev,
"Failed to get memory region resource\n");
820 irq_handler = arasan_cf_interrupt;
833 if (IS_ERR(acdev->
clk)) {
835 return PTR_ERR(acdev->
clk);
849 ap->
ops = &arasan_cf_ops;
889 (
unsigned long long) res->
start, acdev->
vbase);
891 ret = cf_init(acdev);
895 cf_card_detect(acdev, 0);
917 #ifdef CONFIG_PM_SLEEP
918 static int arasan_cf_suspend(
struct device *dev)
931 static int arasan_cf_resume(
struct device *dev)
937 ata_host_resume(host);
946 static const struct of_device_id arasan_cf_id_table[] = {
954 .probe = arasan_cf_probe,
959 .pm = &arasan_cf_pm_ops,