2 * Topcliff PCH DMA controller driver
3 * Copyright (c) 2010 Intel Corporation
4 * Copyright (C) 2011 OKI SEMICONDUCTOR CO., LTD.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/dmaengine.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/init.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/module.h>
26 #include <linux/pch_dma.h>
28 #define DRV_NAME "pch-dma"
30 #define DMA_CTL0_DISABLE 0x0
31 #define DMA_CTL0_SG 0x1
32 #define DMA_CTL0_ONESHOT 0x2
33 #define DMA_CTL0_MODE_MASK_BITS 0x3
34 #define DMA_CTL0_DIR_SHIFT_BITS 2
35 #define DMA_CTL0_BITS_PER_CH 4
37 #define DMA_CTL2_START_SHIFT_BITS 8
38 #define DMA_CTL2_IRQ_ENABLE_MASK ((1UL << DMA_CTL2_START_SHIFT_BITS) - 1)
40 #define DMA_STATUS_IDLE 0x0
41 #define DMA_STATUS_DESC_READ 0x1
42 #define DMA_STATUS_WAIT 0x2
43 #define DMA_STATUS_ACCESS 0x3
44 #define DMA_STATUS_BITS_PER_CH 2
45 #define DMA_STATUS_MASK_BITS 0x3
46 #define DMA_STATUS_SHIFT_BITS 16
47 #define DMA_STATUS_IRQ(x) (0x1 << (x))
48 #define DMA_STATUS_ERR(x) (0x1 << ((x) + 8))
50 #define DMA_DESC_WIDTH_SHIFT_BITS 12
51 #define DMA_DESC_WIDTH_1_BYTE (0x3 << DMA_DESC_WIDTH_SHIFT_BITS)
52 #define DMA_DESC_WIDTH_2_BYTES (0x2 << DMA_DESC_WIDTH_SHIFT_BITS)
53 #define DMA_DESC_WIDTH_4_BYTES (0x0 << DMA_DESC_WIDTH_SHIFT_BITS)
54 #define DMA_DESC_MAX_COUNT_1_BYTE 0x3FF
55 #define DMA_DESC_MAX_COUNT_2_BYTES 0x3FF
56 #define DMA_DESC_MAX_COUNT_4_BYTES 0x7FF
57 #define DMA_DESC_END_WITHOUT_IRQ 0x0
58 #define DMA_DESC_END_WITH_IRQ 0x1
59 #define DMA_DESC_FOLLOW_WITHOUT_IRQ 0x2
60 #define DMA_DESC_FOLLOW_WITH_IRQ 0x3
64 static unsigned int init_nr_desc_per_channel
= 64;
65 module_param(init_nr_desc_per_channel
, uint
, 0644);
66 MODULE_PARM_DESC(init_nr_desc_per_channel
,
67 "initial descriptors per channel (default: 64)");
69 struct pch_dma_desc_regs
{
85 struct pch_dma_desc_regs desc
[MAX_CHAN_NR
];
89 struct pch_dma_desc_regs regs
;
90 struct dma_async_tx_descriptor txd
;
91 struct list_head desc_node
;
92 struct list_head tx_list
;
97 void __iomem
*membase
;
98 enum dma_data_direction dir
;
99 struct tasklet_struct tasklet
;
100 unsigned long err_status
;
104 dma_cookie_t completed_cookie
;
105 struct list_head active_list
;
106 struct list_head queue
;
107 struct list_head free_list
;
108 unsigned int descs_allocated
;
111 #define PDC_DEV_ADDR 0x00
112 #define PDC_MEM_ADDR 0x04
113 #define PDC_SIZE 0x08
114 #define PDC_NEXT 0x0C
116 #define channel_readl(pdc, name) \
117 readl((pdc)->membase + PDC_##name)
118 #define channel_writel(pdc, name, val) \
119 writel((val), (pdc)->membase + PDC_##name)
122 struct dma_device dma
;
123 void __iomem
*membase
;
124 struct pci_pool
*pool
;
125 struct pch_dma_regs regs
;
126 struct pch_dma_desc_regs ch_regs
[MAX_CHAN_NR
];
127 struct pch_dma_chan channels
[MAX_CHAN_NR
];
130 #define PCH_DMA_CTL0 0x00
131 #define PCH_DMA_CTL1 0x04
132 #define PCH_DMA_CTL2 0x08
133 #define PCH_DMA_CTL3 0x0C
134 #define PCH_DMA_STS0 0x10
135 #define PCH_DMA_STS1 0x14
137 #define dma_readl(pd, name) \
138 readl((pd)->membase + PCH_DMA_##name)
139 #define dma_writel(pd, name, val) \
140 writel((val), (pd)->membase + PCH_DMA_##name)
143 struct pch_dma_desc
*to_pd_desc(struct dma_async_tx_descriptor
*txd
)
145 return container_of(txd
, struct pch_dma_desc
, txd
);
148 static inline struct pch_dma_chan
*to_pd_chan(struct dma_chan
*chan
)
150 return container_of(chan
, struct pch_dma_chan
, chan
);
153 static inline struct pch_dma
*to_pd(struct dma_device
*ddev
)
155 return container_of(ddev
, struct pch_dma
, dma
);
158 static inline struct device
*chan2dev(struct dma_chan
*chan
)
160 return &chan
->dev
->device
;
163 static inline struct device
*chan2parent(struct dma_chan
*chan
)
165 return chan
->dev
->device
.parent
;
169 struct pch_dma_desc
*pdc_first_active(struct pch_dma_chan
*pd_chan
)
171 return list_first_entry(&pd_chan
->active_list
,
172 struct pch_dma_desc
, desc_node
);
176 struct pch_dma_desc
*pdc_first_queued(struct pch_dma_chan
*pd_chan
)
178 return list_first_entry(&pd_chan
->queue
,
179 struct pch_dma_desc
, desc_node
);
182 static void pdc_enable_irq(struct dma_chan
*chan
, int enable
)
184 struct pch_dma
*pd
= to_pd(chan
->device
);
187 val
= dma_readl(pd
, CTL2
);
190 val
|= 0x1 << chan
->chan_id
;
192 val
&= ~(0x1 << chan
->chan_id
);
194 dma_writel(pd
, CTL2
, val
);
196 dev_dbg(chan2dev(chan
), "pdc_enable_irq: chan %d -> %x\n",
200 static void pdc_set_dir(struct dma_chan
*chan
)
202 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
203 struct pch_dma
*pd
= to_pd(chan
->device
);
206 if (chan
->chan_id
< 8) {
207 val
= dma_readl(pd
, CTL0
);
209 if (pd_chan
->dir
== DMA_TO_DEVICE
)
210 val
|= 0x1 << (DMA_CTL0_BITS_PER_CH
* chan
->chan_id
+
211 DMA_CTL0_DIR_SHIFT_BITS
);
213 val
&= ~(0x1 << (DMA_CTL0_BITS_PER_CH
* chan
->chan_id
+
214 DMA_CTL0_DIR_SHIFT_BITS
));
216 dma_writel(pd
, CTL0
, val
);
218 int ch
= chan
->chan_id
- 8; /* ch8-->0 ch9-->1 ... ch11->3 */
219 val
= dma_readl(pd
, CTL3
);
221 if (pd_chan
->dir
== DMA_TO_DEVICE
)
222 val
|= 0x1 << (DMA_CTL0_BITS_PER_CH
* ch
+
223 DMA_CTL0_DIR_SHIFT_BITS
);
225 val
&= ~(0x1 << (DMA_CTL0_BITS_PER_CH
* ch
+
226 DMA_CTL0_DIR_SHIFT_BITS
));
228 dma_writel(pd
, CTL3
, val
);
231 dev_dbg(chan2dev(chan
), "pdc_set_dir: chan %d -> %x\n",
235 static void pdc_set_mode(struct dma_chan
*chan
, u32 mode
)
237 struct pch_dma
*pd
= to_pd(chan
->device
);
240 if (chan
->chan_id
< 8) {
241 val
= dma_readl(pd
, CTL0
);
243 val
&= ~(DMA_CTL0_MODE_MASK_BITS
<<
244 (DMA_CTL0_BITS_PER_CH
* chan
->chan_id
));
245 val
|= mode
<< (DMA_CTL0_BITS_PER_CH
* chan
->chan_id
);
247 dma_writel(pd
, CTL0
, val
);
249 int ch
= chan
->chan_id
- 8; /* ch8-->0 ch9-->1 ... ch11->3 */
251 val
= dma_readl(pd
, CTL3
);
253 val
&= ~(DMA_CTL0_MODE_MASK_BITS
<<
254 (DMA_CTL0_BITS_PER_CH
* ch
));
255 val
|= mode
<< (DMA_CTL0_BITS_PER_CH
* ch
);
257 dma_writel(pd
, CTL3
, val
);
261 dev_dbg(chan2dev(chan
), "pdc_set_mode: chan %d -> %x\n",
265 static u32
pdc_get_status(struct pch_dma_chan
*pd_chan
)
267 struct pch_dma
*pd
= to_pd(pd_chan
->chan
.device
);
270 val
= dma_readl(pd
, STS0
);
271 return DMA_STATUS_MASK_BITS
& (val
>> (DMA_STATUS_SHIFT_BITS
+
272 DMA_STATUS_BITS_PER_CH
* pd_chan
->chan
.chan_id
));
275 static bool pdc_is_idle(struct pch_dma_chan
*pd_chan
)
277 if (pdc_get_status(pd_chan
) == DMA_STATUS_IDLE
)
283 static void pdc_dostart(struct pch_dma_chan
*pd_chan
, struct pch_dma_desc
* desc
)
285 if (!pdc_is_idle(pd_chan
)) {
286 dev_err(chan2dev(&pd_chan
->chan
),
287 "BUG: Attempt to start non-idle channel\n");
291 dev_dbg(chan2dev(&pd_chan
->chan
), "chan %d -> dev_addr: %x\n",
292 pd_chan
->chan
.chan_id
, desc
->regs
.dev_addr
);
293 dev_dbg(chan2dev(&pd_chan
->chan
), "chan %d -> mem_addr: %x\n",
294 pd_chan
->chan
.chan_id
, desc
->regs
.mem_addr
);
295 dev_dbg(chan2dev(&pd_chan
->chan
), "chan %d -> size: %x\n",
296 pd_chan
->chan
.chan_id
, desc
->regs
.size
);
297 dev_dbg(chan2dev(&pd_chan
->chan
), "chan %d -> next: %x\n",
298 pd_chan
->chan
.chan_id
, desc
->regs
.next
);
300 if (list_empty(&desc
->tx_list
)) {
301 channel_writel(pd_chan
, DEV_ADDR
, desc
->regs
.dev_addr
);
302 channel_writel(pd_chan
, MEM_ADDR
, desc
->regs
.mem_addr
);
303 channel_writel(pd_chan
, SIZE
, desc
->regs
.size
);
304 channel_writel(pd_chan
, NEXT
, desc
->regs
.next
);
305 pdc_set_mode(&pd_chan
->chan
, DMA_CTL0_ONESHOT
);
307 channel_writel(pd_chan
, NEXT
, desc
->txd
.phys
);
308 pdc_set_mode(&pd_chan
->chan
, DMA_CTL0_SG
);
312 static void pdc_chain_complete(struct pch_dma_chan
*pd_chan
,
313 struct pch_dma_desc
*desc
)
315 struct dma_async_tx_descriptor
*txd
= &desc
->txd
;
316 dma_async_tx_callback callback
= txd
->callback
;
317 void *param
= txd
->callback_param
;
319 list_splice_init(&desc
->tx_list
, &pd_chan
->free_list
);
320 list_move(&desc
->desc_node
, &pd_chan
->free_list
);
326 static void pdc_complete_all(struct pch_dma_chan
*pd_chan
)
328 struct pch_dma_desc
*desc
, *_d
;
331 BUG_ON(!pdc_is_idle(pd_chan
));
333 if (!list_empty(&pd_chan
->queue
))
334 pdc_dostart(pd_chan
, pdc_first_queued(pd_chan
));
336 list_splice_init(&pd_chan
->active_list
, &list
);
337 list_splice_init(&pd_chan
->queue
, &pd_chan
->active_list
);
339 list_for_each_entry_safe(desc
, _d
, &list
, desc_node
)
340 pdc_chain_complete(pd_chan
, desc
);
343 static void pdc_handle_error(struct pch_dma_chan
*pd_chan
)
345 struct pch_dma_desc
*bad_desc
;
347 bad_desc
= pdc_first_active(pd_chan
);
348 list_del(&bad_desc
->desc_node
);
350 list_splice_init(&pd_chan
->queue
, pd_chan
->active_list
.prev
);
352 if (!list_empty(&pd_chan
->active_list
))
353 pdc_dostart(pd_chan
, pdc_first_active(pd_chan
));
355 dev_crit(chan2dev(&pd_chan
->chan
), "Bad descriptor submitted\n");
356 dev_crit(chan2dev(&pd_chan
->chan
), "descriptor cookie: %d\n",
357 bad_desc
->txd
.cookie
);
359 pdc_chain_complete(pd_chan
, bad_desc
);
362 static void pdc_advance_work(struct pch_dma_chan
*pd_chan
)
364 if (list_empty(&pd_chan
->active_list
) ||
365 list_is_singular(&pd_chan
->active_list
)) {
366 pdc_complete_all(pd_chan
);
368 pdc_chain_complete(pd_chan
, pdc_first_active(pd_chan
));
369 pdc_dostart(pd_chan
, pdc_first_active(pd_chan
));
373 static dma_cookie_t
pdc_assign_cookie(struct pch_dma_chan
*pd_chan
,
374 struct pch_dma_desc
*desc
)
376 dma_cookie_t cookie
= pd_chan
->chan
.cookie
;
381 pd_chan
->chan
.cookie
= cookie
;
382 desc
->txd
.cookie
= cookie
;
387 static dma_cookie_t
pd_tx_submit(struct dma_async_tx_descriptor
*txd
)
389 struct pch_dma_desc
*desc
= to_pd_desc(txd
);
390 struct pch_dma_chan
*pd_chan
= to_pd_chan(txd
->chan
);
393 spin_lock(&pd_chan
->lock
);
394 cookie
= pdc_assign_cookie(pd_chan
, desc
);
396 if (list_empty(&pd_chan
->active_list
)) {
397 list_add_tail(&desc
->desc_node
, &pd_chan
->active_list
);
398 pdc_dostart(pd_chan
, desc
);
400 list_add_tail(&desc
->desc_node
, &pd_chan
->queue
);
403 spin_unlock(&pd_chan
->lock
);
407 static struct pch_dma_desc
*pdc_alloc_desc(struct dma_chan
*chan
, gfp_t flags
)
409 struct pch_dma_desc
*desc
= NULL
;
410 struct pch_dma
*pd
= to_pd(chan
->device
);
413 desc
= pci_pool_alloc(pd
->pool
, flags
, &addr
);
415 memset(desc
, 0, sizeof(struct pch_dma_desc
));
416 INIT_LIST_HEAD(&desc
->tx_list
);
417 dma_async_tx_descriptor_init(&desc
->txd
, chan
);
418 desc
->txd
.tx_submit
= pd_tx_submit
;
419 desc
->txd
.flags
= DMA_CTRL_ACK
;
420 desc
->txd
.phys
= addr
;
426 static struct pch_dma_desc
*pdc_desc_get(struct pch_dma_chan
*pd_chan
)
428 struct pch_dma_desc
*desc
, *_d
;
429 struct pch_dma_desc
*ret
= NULL
;
432 spin_lock(&pd_chan
->lock
);
433 list_for_each_entry_safe(desc
, _d
, &pd_chan
->free_list
, desc_node
) {
435 if (async_tx_test_ack(&desc
->txd
)) {
436 list_del(&desc
->desc_node
);
440 dev_dbg(chan2dev(&pd_chan
->chan
), "desc %p not ACKed\n", desc
);
442 spin_unlock(&pd_chan
->lock
);
443 dev_dbg(chan2dev(&pd_chan
->chan
), "scanned %d descriptors\n", i
);
446 ret
= pdc_alloc_desc(&pd_chan
->chan
, GFP_NOIO
);
448 spin_lock(&pd_chan
->lock
);
449 pd_chan
->descs_allocated
++;
450 spin_unlock(&pd_chan
->lock
);
452 dev_err(chan2dev(&pd_chan
->chan
),
453 "failed to alloc desc\n");
460 static void pdc_desc_put(struct pch_dma_chan
*pd_chan
,
461 struct pch_dma_desc
*desc
)
464 spin_lock(&pd_chan
->lock
);
465 list_splice_init(&desc
->tx_list
, &pd_chan
->free_list
);
466 list_add(&desc
->desc_node
, &pd_chan
->free_list
);
467 spin_unlock(&pd_chan
->lock
);
471 static int pd_alloc_chan_resources(struct dma_chan
*chan
)
473 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
474 struct pch_dma_desc
*desc
;
478 if (!pdc_is_idle(pd_chan
)) {
479 dev_dbg(chan2dev(chan
), "DMA channel not idle ?\n");
483 if (!list_empty(&pd_chan
->free_list
))
484 return pd_chan
->descs_allocated
;
486 for (i
= 0; i
< init_nr_desc_per_channel
; i
++) {
487 desc
= pdc_alloc_desc(chan
, GFP_KERNEL
);
490 dev_warn(chan2dev(chan
),
491 "Only allocated %d initial descriptors\n", i
);
495 list_add_tail(&desc
->desc_node
, &tmp_list
);
498 spin_lock_bh(&pd_chan
->lock
);
499 list_splice(&tmp_list
, &pd_chan
->free_list
);
500 pd_chan
->descs_allocated
= i
;
501 pd_chan
->completed_cookie
= chan
->cookie
= 1;
502 spin_unlock_bh(&pd_chan
->lock
);
504 pdc_enable_irq(chan
, 1);
506 return pd_chan
->descs_allocated
;
509 static void pd_free_chan_resources(struct dma_chan
*chan
)
511 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
512 struct pch_dma
*pd
= to_pd(chan
->device
);
513 struct pch_dma_desc
*desc
, *_d
;
516 BUG_ON(!pdc_is_idle(pd_chan
));
517 BUG_ON(!list_empty(&pd_chan
->active_list
));
518 BUG_ON(!list_empty(&pd_chan
->queue
));
520 spin_lock_bh(&pd_chan
->lock
);
521 list_splice_init(&pd_chan
->free_list
, &tmp_list
);
522 pd_chan
->descs_allocated
= 0;
523 spin_unlock_bh(&pd_chan
->lock
);
525 list_for_each_entry_safe(desc
, _d
, &tmp_list
, desc_node
)
526 pci_pool_free(pd
->pool
, desc
, desc
->txd
.phys
);
528 pdc_enable_irq(chan
, 0);
531 static enum dma_status
pd_tx_status(struct dma_chan
*chan
, dma_cookie_t cookie
,
532 struct dma_tx_state
*txstate
)
534 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
535 dma_cookie_t last_used
;
536 dma_cookie_t last_completed
;
539 spin_lock_bh(&pd_chan
->lock
);
540 last_completed
= pd_chan
->completed_cookie
;
541 last_used
= chan
->cookie
;
542 spin_unlock_bh(&pd_chan
->lock
);
544 ret
= dma_async_is_complete(cookie
, last_completed
, last_used
);
546 dma_set_tx_state(txstate
, last_completed
, last_used
, 0);
551 static void pd_issue_pending(struct dma_chan
*chan
)
553 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
555 if (pdc_is_idle(pd_chan
)) {
556 spin_lock(&pd_chan
->lock
);
557 pdc_advance_work(pd_chan
);
558 spin_unlock(&pd_chan
->lock
);
562 static struct dma_async_tx_descriptor
*pd_prep_slave_sg(struct dma_chan
*chan
,
563 struct scatterlist
*sgl
, unsigned int sg_len
,
564 enum dma_data_direction direction
, unsigned long flags
)
566 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
567 struct pch_dma_slave
*pd_slave
= chan
->private;
568 struct pch_dma_desc
*first
= NULL
;
569 struct pch_dma_desc
*prev
= NULL
;
570 struct pch_dma_desc
*desc
= NULL
;
571 struct scatterlist
*sg
;
575 if (unlikely(!sg_len
)) {
576 dev_info(chan2dev(chan
), "prep_slave_sg: length is zero!\n");
580 if (direction
== DMA_FROM_DEVICE
)
581 reg
= pd_slave
->rx_reg
;
582 else if (direction
== DMA_TO_DEVICE
)
583 reg
= pd_slave
->tx_reg
;
587 pd_chan
->dir
= direction
;
590 for_each_sg(sgl
, sg
, sg_len
, i
) {
591 desc
= pdc_desc_get(pd_chan
);
596 desc
->regs
.dev_addr
= reg
;
597 desc
->regs
.mem_addr
= sg_phys(sg
);
598 desc
->regs
.size
= sg_dma_len(sg
);
599 desc
->regs
.next
= DMA_DESC_FOLLOW_WITHOUT_IRQ
;
601 switch (pd_slave
->width
) {
602 case PCH_DMA_WIDTH_1_BYTE
:
603 if (desc
->regs
.size
> DMA_DESC_MAX_COUNT_1_BYTE
)
605 desc
->regs
.size
|= DMA_DESC_WIDTH_1_BYTE
;
607 case PCH_DMA_WIDTH_2_BYTES
:
608 if (desc
->regs
.size
> DMA_DESC_MAX_COUNT_2_BYTES
)
610 desc
->regs
.size
|= DMA_DESC_WIDTH_2_BYTES
;
612 case PCH_DMA_WIDTH_4_BYTES
:
613 if (desc
->regs
.size
> DMA_DESC_MAX_COUNT_4_BYTES
)
615 desc
->regs
.size
|= DMA_DESC_WIDTH_4_BYTES
;
624 prev
->regs
.next
|= desc
->txd
.phys
;
625 list_add_tail(&desc
->desc_node
, &first
->tx_list
);
631 if (flags
& DMA_PREP_INTERRUPT
)
632 desc
->regs
.next
= DMA_DESC_END_WITH_IRQ
;
634 desc
->regs
.next
= DMA_DESC_END_WITHOUT_IRQ
;
636 first
->txd
.cookie
= -EBUSY
;
637 desc
->txd
.flags
= flags
;
642 dev_err(chan2dev(chan
), "failed to get desc or wrong parameters\n");
643 pdc_desc_put(pd_chan
, first
);
647 static int pd_device_control(struct dma_chan
*chan
, enum dma_ctrl_cmd cmd
,
650 struct pch_dma_chan
*pd_chan
= to_pd_chan(chan
);
651 struct pch_dma_desc
*desc
, *_d
;
654 if (cmd
!= DMA_TERMINATE_ALL
)
657 spin_lock_bh(&pd_chan
->lock
);
659 pdc_set_mode(&pd_chan
->chan
, DMA_CTL0_DISABLE
);
661 list_splice_init(&pd_chan
->active_list
, &list
);
662 list_splice_init(&pd_chan
->queue
, &list
);
664 list_for_each_entry_safe(desc
, _d
, &list
, desc_node
)
665 pdc_chain_complete(pd_chan
, desc
);
667 spin_unlock_bh(&pd_chan
->lock
);
672 static void pdc_tasklet(unsigned long data
)
674 struct pch_dma_chan
*pd_chan
= (struct pch_dma_chan
*)data
;
677 if (!pdc_is_idle(pd_chan
)) {
678 dev_err(chan2dev(&pd_chan
->chan
),
679 "BUG: handle non-idle channel in tasklet\n");
683 spin_lock_irqsave(&pd_chan
->lock
, flags
);
684 if (test_and_clear_bit(0, &pd_chan
->err_status
))
685 pdc_handle_error(pd_chan
);
687 pdc_advance_work(pd_chan
);
688 spin_unlock_irqrestore(&pd_chan
->lock
, flags
);
691 static irqreturn_t
pd_irq(int irq
, void *devid
)
693 struct pch_dma
*pd
= (struct pch_dma
*)devid
;
694 struct pch_dma_chan
*pd_chan
;
699 sts0
= dma_readl(pd
, STS0
);
701 dev_dbg(pd
->dma
.dev
, "pd_irq sts0: %x\n", sts0
);
703 for (i
= 0; i
< pd
->dma
.chancnt
; i
++) {
704 pd_chan
= &pd
->channels
[i
];
706 if (sts0
& DMA_STATUS_IRQ(i
)) {
707 if (sts0
& DMA_STATUS_ERR(i
))
708 set_bit(0, &pd_chan
->err_status
);
710 tasklet_schedule(&pd_chan
->tasklet
);
716 /* clear interrupt bits in status register */
717 dma_writel(pd
, STS0
, sts0
);
723 static void pch_dma_save_regs(struct pch_dma
*pd
)
725 struct pch_dma_chan
*pd_chan
;
726 struct dma_chan
*chan
, *_c
;
729 pd
->regs
.dma_ctl0
= dma_readl(pd
, CTL0
);
730 pd
->regs
.dma_ctl1
= dma_readl(pd
, CTL1
);
731 pd
->regs
.dma_ctl2
= dma_readl(pd
, CTL2
);
732 pd
->regs
.dma_ctl3
= dma_readl(pd
, CTL3
);
734 list_for_each_entry_safe(chan
, _c
, &pd
->dma
.channels
, device_node
) {
735 pd_chan
= to_pd_chan(chan
);
737 pd
->ch_regs
[i
].dev_addr
= channel_readl(pd_chan
, DEV_ADDR
);
738 pd
->ch_regs
[i
].mem_addr
= channel_readl(pd_chan
, MEM_ADDR
);
739 pd
->ch_regs
[i
].size
= channel_readl(pd_chan
, SIZE
);
740 pd
->ch_regs
[i
].next
= channel_readl(pd_chan
, NEXT
);
746 static void pch_dma_restore_regs(struct pch_dma
*pd
)
748 struct pch_dma_chan
*pd_chan
;
749 struct dma_chan
*chan
, *_c
;
752 dma_writel(pd
, CTL0
, pd
->regs
.dma_ctl0
);
753 dma_writel(pd
, CTL1
, pd
->regs
.dma_ctl1
);
754 dma_writel(pd
, CTL2
, pd
->regs
.dma_ctl2
);
755 dma_writel(pd
, CTL3
, pd
->regs
.dma_ctl3
);
757 list_for_each_entry_safe(chan
, _c
, &pd
->dma
.channels
, device_node
) {
758 pd_chan
= to_pd_chan(chan
);
760 channel_writel(pd_chan
, DEV_ADDR
, pd
->ch_regs
[i
].dev_addr
);
761 channel_writel(pd_chan
, MEM_ADDR
, pd
->ch_regs
[i
].mem_addr
);
762 channel_writel(pd_chan
, SIZE
, pd
->ch_regs
[i
].size
);
763 channel_writel(pd_chan
, NEXT
, pd
->ch_regs
[i
].next
);
769 static int pch_dma_suspend(struct pci_dev
*pdev
, pm_message_t state
)
771 struct pch_dma
*pd
= pci_get_drvdata(pdev
);
774 pch_dma_save_regs(pd
);
776 pci_save_state(pdev
);
777 pci_disable_device(pdev
);
778 pci_set_power_state(pdev
, pci_choose_state(pdev
, state
));
783 static int pch_dma_resume(struct pci_dev
*pdev
)
785 struct pch_dma
*pd
= pci_get_drvdata(pdev
);
788 pci_set_power_state(pdev
, PCI_D0
);
789 pci_restore_state(pdev
);
791 err
= pci_enable_device(pdev
);
793 dev_dbg(&pdev
->dev
, "failed to enable device\n");
798 pch_dma_restore_regs(pd
);
804 static int __devinit
pch_dma_probe(struct pci_dev
*pdev
,
805 const struct pci_device_id
*id
)
808 struct pch_dma_regs
*regs
;
809 unsigned int nr_channels
;
813 nr_channels
= id
->driver_data
;
814 pd
= kzalloc(sizeof(struct pch_dma
)+
815 sizeof(struct pch_dma_chan
) * nr_channels
, GFP_KERNEL
);
819 pci_set_drvdata(pdev
, pd
);
821 err
= pci_enable_device(pdev
);
823 dev_err(&pdev
->dev
, "Cannot enable PCI device\n");
827 if (!(pci_resource_flags(pdev
, 1) & IORESOURCE_MEM
)) {
828 dev_err(&pdev
->dev
, "Cannot find proper base address\n");
829 goto err_disable_pdev
;
832 err
= pci_request_regions(pdev
, DRV_NAME
);
834 dev_err(&pdev
->dev
, "Cannot obtain PCI resources\n");
835 goto err_disable_pdev
;
838 err
= pci_set_dma_mask(pdev
, DMA_BIT_MASK(32));
840 dev_err(&pdev
->dev
, "Cannot set proper DMA config\n");
844 regs
= pd
->membase
= pci_iomap(pdev
, 1, 0);
846 dev_err(&pdev
->dev
, "Cannot map MMIO registers\n");
851 pci_set_master(pdev
);
853 err
= request_irq(pdev
->irq
, pd_irq
, IRQF_SHARED
, DRV_NAME
, pd
);
855 dev_err(&pdev
->dev
, "Failed to request IRQ\n");
859 pd
->pool
= pci_pool_create("pch_dma_desc_pool", pdev
,
860 sizeof(struct pch_dma_desc
), 4, 0);
862 dev_err(&pdev
->dev
, "Failed to alloc DMA descriptors\n");
867 pd
->dma
.dev
= &pdev
->dev
;
868 pd
->dma
.chancnt
= nr_channels
;
870 INIT_LIST_HEAD(&pd
->dma
.channels
);
872 for (i
= 0; i
< nr_channels
; i
++) {
873 struct pch_dma_chan
*pd_chan
= &pd
->channels
[i
];
875 pd_chan
->chan
.device
= &pd
->dma
;
876 pd_chan
->chan
.cookie
= 1;
877 pd_chan
->chan
.chan_id
= i
;
879 pd_chan
->membase
= ®s
->desc
[i
];
881 spin_lock_init(&pd_chan
->lock
);
883 INIT_LIST_HEAD(&pd_chan
->active_list
);
884 INIT_LIST_HEAD(&pd_chan
->queue
);
885 INIT_LIST_HEAD(&pd_chan
->free_list
);
887 tasklet_init(&pd_chan
->tasklet
, pdc_tasklet
,
888 (unsigned long)pd_chan
);
889 list_add_tail(&pd_chan
->chan
.device_node
, &pd
->dma
.channels
);
892 dma_cap_zero(pd
->dma
.cap_mask
);
893 dma_cap_set(DMA_PRIVATE
, pd
->dma
.cap_mask
);
894 dma_cap_set(DMA_SLAVE
, pd
->dma
.cap_mask
);
896 pd
->dma
.device_alloc_chan_resources
= pd_alloc_chan_resources
;
897 pd
->dma
.device_free_chan_resources
= pd_free_chan_resources
;
898 pd
->dma
.device_tx_status
= pd_tx_status
;
899 pd
->dma
.device_issue_pending
= pd_issue_pending
;
900 pd
->dma
.device_prep_slave_sg
= pd_prep_slave_sg
;
901 pd
->dma
.device_control
= pd_device_control
;
903 err
= dma_async_device_register(&pd
->dma
);
905 dev_err(&pdev
->dev
, "Failed to register DMA device\n");
912 pci_pool_destroy(pd
->pool
);
914 free_irq(pdev
->irq
, pd
);
916 pci_iounmap(pdev
, pd
->membase
);
918 pci_release_regions(pdev
);
920 pci_disable_device(pdev
);
925 static void __devexit
pch_dma_remove(struct pci_dev
*pdev
)
927 struct pch_dma
*pd
= pci_get_drvdata(pdev
);
928 struct pch_dma_chan
*pd_chan
;
929 struct dma_chan
*chan
, *_c
;
932 dma_async_device_unregister(&pd
->dma
);
934 list_for_each_entry_safe(chan
, _c
, &pd
->dma
.channels
,
936 pd_chan
= to_pd_chan(chan
);
938 tasklet_disable(&pd_chan
->tasklet
);
939 tasklet_kill(&pd_chan
->tasklet
);
942 pci_pool_destroy(pd
->pool
);
943 free_irq(pdev
->irq
, pd
);
944 pci_iounmap(pdev
, pd
->membase
);
945 pci_release_regions(pdev
);
946 pci_disable_device(pdev
);
951 /* PCI Device ID of DMA device */
952 #define PCI_VENDOR_ID_ROHM 0x10DB
953 #define PCI_DEVICE_ID_EG20T_PCH_DMA_8CH 0x8810
954 #define PCI_DEVICE_ID_EG20T_PCH_DMA_4CH 0x8815
955 #define PCI_DEVICE_ID_ML7213_DMA1_8CH 0x8026
956 #define PCI_DEVICE_ID_ML7213_DMA2_8CH 0x802B
957 #define PCI_DEVICE_ID_ML7213_DMA3_4CH 0x8034
958 #define PCI_DEVICE_ID_ML7213_DMA4_12CH 0x8032
959 #define PCI_DEVICE_ID_ML7223_DMA1_4CH 0x800B
960 #define PCI_DEVICE_ID_ML7223_DMA2_4CH 0x800E
961 #define PCI_DEVICE_ID_ML7223_DMA3_4CH 0x8017
962 #define PCI_DEVICE_ID_ML7223_DMA4_4CH 0x803B
964 DEFINE_PCI_DEVICE_TABLE(pch_dma_id_table
) = {
965 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_EG20T_PCH_DMA_8CH
), 8 },
966 { PCI_VDEVICE(INTEL
, PCI_DEVICE_ID_EG20T_PCH_DMA_4CH
), 4 },
967 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7213_DMA1_8CH
), 8}, /* UART Video */
968 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7213_DMA2_8CH
), 8}, /* PCMIF SPI */
969 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7213_DMA3_4CH
), 4}, /* FPGA */
970 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7213_DMA4_12CH
), 12}, /* I2S */
971 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7223_DMA1_4CH
), 4}, /* UART */
972 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7223_DMA2_4CH
), 4}, /* Video SPI */
973 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7223_DMA3_4CH
), 4}, /* Security */
974 { PCI_VDEVICE(ROHM
, PCI_DEVICE_ID_ML7223_DMA4_4CH
), 4}, /* FPGA */
978 static struct pci_driver pch_dma_driver
= {
980 .id_table
= pch_dma_id_table
,
981 .probe
= pch_dma_probe
,
982 .remove
= __devexit_p(pch_dma_remove
),
984 .suspend
= pch_dma_suspend
,
985 .resume
= pch_dma_resume
,
989 static int __init
pch_dma_init(void)
991 return pci_register_driver(&pch_dma_driver
);
994 static void __exit
pch_dma_exit(void)
996 pci_unregister_driver(&pch_dma_driver
);
999 module_init(pch_dma_init
);
1000 module_exit(pch_dma_exit
);
1002 MODULE_DESCRIPTION("Intel EG20T PCH / OKI SEMICONDUCTOR ML7213 IOH "
1003 "DMA controller driver");
1004 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
1005 MODULE_LICENSE("GPL v2");