23 #include <linux/pci.h>
25 #include <linux/module.h>
30 #define DRV_NAME "pch-dma"
32 #define DMA_CTL0_DISABLE 0x0
33 #define DMA_CTL0_SG 0x1
34 #define DMA_CTL0_ONESHOT 0x2
35 #define DMA_CTL0_MODE_MASK_BITS 0x3
36 #define DMA_CTL0_DIR_SHIFT_BITS 2
37 #define DMA_CTL0_BITS_PER_CH 4
39 #define DMA_CTL2_START_SHIFT_BITS 8
40 #define DMA_CTL2_IRQ_ENABLE_MASK ((1UL << DMA_CTL2_START_SHIFT_BITS) - 1)
42 #define DMA_STATUS_IDLE 0x0
43 #define DMA_STATUS_DESC_READ 0x1
44 #define DMA_STATUS_WAIT 0x2
45 #define DMA_STATUS_ACCESS 0x3
46 #define DMA_STATUS_BITS_PER_CH 2
47 #define DMA_STATUS_MASK_BITS 0x3
48 #define DMA_STATUS_SHIFT_BITS 16
49 #define DMA_STATUS_IRQ(x) (0x1 << (x))
50 #define DMA_STATUS0_ERR(x) (0x1 << ((x) + 8))
51 #define DMA_STATUS2_ERR(x) (0x1 << (x))
53 #define DMA_DESC_WIDTH_SHIFT_BITS 12
54 #define DMA_DESC_WIDTH_1_BYTE (0x3 << DMA_DESC_WIDTH_SHIFT_BITS)
55 #define DMA_DESC_WIDTH_2_BYTES (0x2 << DMA_DESC_WIDTH_SHIFT_BITS)
56 #define DMA_DESC_WIDTH_4_BYTES (0x0 << DMA_DESC_WIDTH_SHIFT_BITS)
57 #define DMA_DESC_MAX_COUNT_1_BYTE 0x3FF
58 #define DMA_DESC_MAX_COUNT_2_BYTES 0x3FF
59 #define DMA_DESC_MAX_COUNT_4_BYTES 0x7FF
60 #define DMA_DESC_END_WITHOUT_IRQ 0x0
61 #define DMA_DESC_END_WITH_IRQ 0x1
62 #define DMA_DESC_FOLLOW_WITHOUT_IRQ 0x2
63 #define DMA_DESC_FOLLOW_WITH_IRQ 0x3
65 #define MAX_CHAN_NR 12
67 #define DMA_MASK_CTL0_MODE 0x33333333
68 #define DMA_MASK_CTL2_MODE 0x00003333
70 static unsigned int init_nr_desc_per_channel = 64;
73 "initial descriptors per channel (default: 64)");
116 #define PDC_DEV_ADDR 0x00
117 #define PDC_MEM_ADDR 0x04
118 #define PDC_SIZE 0x08
119 #define PDC_NEXT 0x0C
121 #define channel_readl(pdc, name) \
122 readl((pdc)->membase + PDC_##name)
123 #define channel_writel(pdc, name, val) \
124 writel((val), (pdc)->membase + PDC_##name)
135 #define PCH_DMA_CTL0 0x00
136 #define PCH_DMA_CTL1 0x04
137 #define PCH_DMA_CTL2 0x08
138 #define PCH_DMA_CTL3 0x0C
139 #define PCH_DMA_STS0 0x10
140 #define PCH_DMA_STS1 0x14
141 #define PCH_DMA_STS2 0x18
143 #define dma_readl(pd, name) \
144 readl((pd)->membase + PCH_DMA_##name)
145 #define dma_writel(pd, name, val) \
146 writel((val), (pd)->membase + PCH_DMA_##name)
166 return &chan->
dev->device;
171 return chan->
dev->device.parent;
204 val &= ~(0x1 <<
pos);
208 dev_dbg(chan2dev(chan),
"pdc_enable_irq: chan %d -> %x\n",
212 static void pdc_set_dir(
struct dma_chan *chan)
256 dev_dbg(chan2dev(chan),
"pdc_set_dir: chan %d -> %x\n",
271 DMA_CTL0_DIR_SHIFT_BITS);
282 DMA_CTL0_DIR_SHIFT_BITS);
290 dev_dbg(chan2dev(chan),
"pdc_set_mode: chan %d -> %x\n",
318 if (pd_chan->
chan.chan_id < 8)
319 sts = pdc_get_status0(pd_chan);
321 sts = pdc_get_status2(pd_chan);
332 if (!pdc_is_idle(pd_chan)) {
334 "BUG: Attempt to start non-idle channel\n");
338 dev_dbg(chan2dev(&pd_chan->
chan),
"chan %d -> dev_addr: %x\n",
339 pd_chan->
chan.chan_id, desc->
regs.dev_addr);
340 dev_dbg(chan2dev(&pd_chan->
chan),
"chan %d -> mem_addr: %x\n",
341 pd_chan->
chan.chan_id, desc->
regs.mem_addr);
342 dev_dbg(chan2dev(&pd_chan->
chan),
"chan %d -> size: %x\n",
343 pd_chan->
chan.chan_id, desc->
regs.size);
344 dev_dbg(chan2dev(&pd_chan->
chan),
"chan %d -> next: %x\n",
345 pd_chan->
chan.chan_id, desc->
regs.next);
347 if (list_empty(&desc->
tx_list)) {
359 static void pdc_chain_complete(
struct pch_dma_chan *pd_chan,
373 static void pdc_complete_all(
struct pch_dma_chan *pd_chan)
378 BUG_ON(!pdc_is_idle(pd_chan));
380 if (!list_empty(&pd_chan->
queue))
381 pdc_dostart(pd_chan, pdc_first_queued(pd_chan));
387 pdc_chain_complete(pd_chan, desc);
394 bad_desc = pdc_first_active(pd_chan);
397 list_splice_init(&pd_chan->queue, pd_chan->active_list.prev);
399 if (!list_empty(&pd_chan->active_list))
400 pdc_dostart(pd_chan, pdc_first_active(pd_chan));
402 dev_crit(chan2dev(&pd_chan->chan),
"Bad descriptor submitted\n");
403 dev_crit(chan2dev(&pd_chan->chan),
"descriptor cookie: %d\n",
404 bad_desc->
txd.cookie);
406 pdc_chain_complete(pd_chan, bad_desc);
409 static void pdc_advance_work(
struct pch_dma_chan *pd_chan)
413 pdc_complete_all(pd_chan);
415 pdc_chain_complete(pd_chan, pdc_first_active(pd_chan));
416 pdc_dostart(pd_chan, pdc_first_active(pd_chan));
426 spin_lock(&pd_chan->
lock);
427 cookie = dma_cookie_assign(txd);
431 pdc_dostart(pd_chan, desc);
436 spin_unlock(&pd_chan->
lock);
446 desc = pci_pool_alloc(pd->
pool, flags, &addr);
449 INIT_LIST_HEAD(&desc->
tx_list);
451 desc->
txd.tx_submit = pd_tx_submit;
465 spin_lock(&pd_chan->
lock);
468 if (async_tx_test_ack(&desc->
txd)) {
473 dev_dbg(chan2dev(&pd_chan->
chan),
"desc %p not ACKed\n", desc);
475 spin_unlock(&pd_chan->
lock);
476 dev_dbg(chan2dev(&pd_chan->
chan),
"scanned %d descriptors\n", i);
481 spin_lock(&pd_chan->
lock);
483 spin_unlock(&pd_chan->
lock);
486 "failed to alloc desc\n");
497 spin_lock(&pd_chan->
lock);
500 spin_unlock(&pd_chan->
lock);
504 static int pd_alloc_chan_resources(
struct dma_chan *chan)
511 if (!pdc_is_idle(pd_chan)) {
512 dev_dbg(chan2dev(chan),
"DMA channel not idle ?\n");
519 for (i = 0; i < init_nr_desc_per_channel; i++) {
524 "Only allocated %d initial descriptors\n", i);
531 spin_lock_irq(&pd_chan->
lock);
532 list_splice(&tmp_list, &pd_chan->
free_list);
534 dma_cookie_init(chan);
535 spin_unlock_irq(&pd_chan->
lock);
537 pdc_enable_irq(chan, 1);
542 static void pd_free_chan_resources(
struct dma_chan *chan)
549 BUG_ON(!pdc_is_idle(pd_chan));
553 spin_lock_irq(&pd_chan->
lock);
554 list_splice_init(&pd_chan->
free_list, &tmp_list);
556 spin_unlock_irq(&pd_chan->
lock);
559 pci_pool_free(pd->
pool, desc, desc->txd.
phys);
561 pdc_enable_irq(chan, 0);
570 spin_lock_irq(&pd_chan->
lock);
571 ret = dma_cookie_status(chan, cookie, txstate);
572 spin_unlock_irq(&pd_chan->
lock);
577 static void pd_issue_pending(
struct dma_chan *chan)
581 if (pdc_is_idle(pd_chan)) {
582 spin_lock(&pd_chan->
lock);
583 pdc_advance_work(pd_chan);
584 spin_unlock(&pd_chan->
lock);
603 dev_info(chan2dev(chan),
"prep_slave_sg: length is zero!\n");
618 desc = pdc_desc_get(pd_chan);
628 switch (pd_slave->
width) {
651 prev->
regs.next |= desc->
txd.phys;
669 dev_err(chan2dev(chan),
"failed to get desc or wrong parameters\n");
670 pdc_desc_put(pd_chan, first);
684 spin_lock_irq(&pd_chan->
lock);
689 list_splice_init(&pd_chan->
queue, &
list);
692 pdc_chain_complete(pd_chan, desc);
694 spin_unlock_irq(&pd_chan->lock);
699 static
void pdc_tasklet(
unsigned long data)
704 if (!pdc_is_idle(pd_chan)) {
706 "BUG: handle non-idle channel in tasklet\n");
712 pdc_handle_error(pd_chan);
714 pdc_advance_work(pd_chan);
715 spin_unlock_irqrestore(&pd_chan->
lock, flags);
731 dev_dbg(pd->
dma.dev,
"pd_irq sts0: %x\n", sts0);
733 for (i = 0; i < pd->
dma.chancnt; i++) {
741 tasklet_schedule(&pd_chan->
tasklet);
749 tasklet_schedule(&pd_chan->
tasklet);
765 static void pch_dma_save_regs(
struct pch_dma *pd)
777 pd_chan = to_pd_chan(chan);
788 static void pch_dma_restore_regs(
struct pch_dma *pd)
800 pd_chan = to_pd_chan(chan);
813 struct pch_dma *pd = pci_get_drvdata(pdev);
816 pch_dma_save_regs(pd);
825 static int pch_dma_resume(
struct pci_dev *pdev)
827 struct pch_dma *pd = pci_get_drvdata(pdev);
835 dev_dbg(&pdev->
dev,
"failed to enable device\n");
840 pch_dma_restore_regs(pd);
851 unsigned int nr_channels;
855 nr_channels =
id->driver_data;
860 pci_set_drvdata(pdev, pd);
864 dev_err(&pdev->
dev,
"Cannot enable PCI device\n");
869 dev_err(&pdev->
dev,
"Cannot find proper base address\n");
870 goto err_disable_pdev;
875 dev_err(&pdev->
dev,
"Cannot obtain PCI resources\n");
876 goto err_disable_pdev;
881 dev_err(&pdev->
dev,
"Cannot set proper DMA config\n");
885 regs = pd->
membase = pci_iomap(pdev, 1, 0);
887 dev_err(&pdev->
dev,
"Cannot map MMIO registers\n");
896 dev_err(&pdev->
dev,
"Failed to request IRQ\n");
900 pd->
pool = pci_pool_create(
"pch_dma_desc_pool", pdev,
903 dev_err(&pdev->
dev,
"Failed to alloc DMA descriptors\n");
910 INIT_LIST_HEAD(&pd->
dma.channels);
912 for (i = 0; i < nr_channels; i++) {
915 pd_chan->
chan.device = &pd->
dma;
916 dma_cookie_init(&pd_chan->
chan);
923 INIT_LIST_HEAD(&pd_chan->
queue);
927 (
unsigned long)pd_chan);
935 pd->
dma.device_alloc_chan_resources = pd_alloc_chan_resources;
936 pd->
dma.device_free_chan_resources = pd_free_chan_resources;
937 pd->
dma.device_tx_status = pd_tx_status;
938 pd->
dma.device_issue_pending = pd_issue_pending;
939 pd->
dma.device_prep_slave_sg = pd_prep_slave_sg;
940 pd->
dma.device_control = pd_device_control;
944 dev_err(&pdev->
dev,
"Failed to register DMA device\n");
951 pci_pool_destroy(pd->
pool);
966 struct pch_dma *pd = pci_get_drvdata(pdev);
975 pd_chan = to_pd_chan(chan);
977 tasklet_disable(&pd_chan->
tasklet);
981 pci_pool_destroy(pd->
pool);
991 #define PCI_VENDOR_ID_ROHM 0x10DB
992 #define PCI_DEVICE_ID_EG20T_PCH_DMA_8CH 0x8810
993 #define PCI_DEVICE_ID_EG20T_PCH_DMA_4CH 0x8815
994 #define PCI_DEVICE_ID_ML7213_DMA1_8CH 0x8026
995 #define PCI_DEVICE_ID_ML7213_DMA2_8CH 0x802B
996 #define PCI_DEVICE_ID_ML7213_DMA3_4CH 0x8034
997 #define PCI_DEVICE_ID_ML7213_DMA4_12CH 0x8032
998 #define PCI_DEVICE_ID_ML7223_DMA1_4CH 0x800B
999 #define PCI_DEVICE_ID_ML7223_DMA2_4CH 0x800E
1000 #define PCI_DEVICE_ID_ML7223_DMA3_4CH 0x8017
1001 #define PCI_DEVICE_ID_ML7223_DMA4_4CH 0x803B
1002 #define PCI_DEVICE_ID_ML7831_DMA1_8CH 0x8810
1003 #define PCI_DEVICE_ID_ML7831_DMA2_4CH 0x8815
1023 .id_table = pch_dma_id_table,
1024 .probe = pch_dma_probe,
1027 .suspend = pch_dma_suspend,
1028 .resume = pch_dma_resume,
1032 static int __init pch_dma_init(
void)
1034 return pci_register_driver(&pch_dma_driver);
1037 static void __exit pch_dma_exit(
void)
1046 "DMA controller driver");