]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/dma/intel_mid_dma.c
dmaengine: move last completed cookie into generic dma_chan structure
[mirror_ubuntu-artful-kernel.git] / drivers / dma / intel_mid_dma.c
1 /*
2 * intel_mid_dma.c - Intel Langwell DMA Drivers
3 *
4 * Copyright (C) 2008-10 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * The driver design is based on dw_dmac driver
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 *
24 *
25 */
26 #include <linux/pci.h>
27 #include <linux/interrupt.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/intel_mid_dma.h>
30 #include <linux/module.h>
31
32 #define MAX_CHAN 4 /*max ch across controllers*/
33 #include "intel_mid_dma_regs.h"
34
35 #define INTEL_MID_DMAC1_ID 0x0814
36 #define INTEL_MID_DMAC2_ID 0x0813
37 #define INTEL_MID_GP_DMAC2_ID 0x0827
38 #define INTEL_MFLD_DMAC1_ID 0x0830
39 #define LNW_PERIPHRAL_MASK_BASE 0xFFAE8008
40 #define LNW_PERIPHRAL_MASK_SIZE 0x10
41 #define LNW_PERIPHRAL_STATUS 0x0
42 #define LNW_PERIPHRAL_MASK 0x8
43
44 struct intel_mid_dma_probe_info {
45 u8 max_chan;
46 u8 ch_base;
47 u16 block_size;
48 u32 pimr_mask;
49 };
50
51 #define INFO(_max_chan, _ch_base, _block_size, _pimr_mask) \
52 ((kernel_ulong_t)&(struct intel_mid_dma_probe_info) { \
53 .max_chan = (_max_chan), \
54 .ch_base = (_ch_base), \
55 .block_size = (_block_size), \
56 .pimr_mask = (_pimr_mask), \
57 })
58
59 /*****************************************************************************
60 Utility Functions*/
61 /**
62 * get_ch_index - convert status to channel
63 * @status: status mask
64 * @base: dma ch base value
65 *
66 * Modify the status mask and return the channel index needing
67 * attention (or -1 if neither)
68 */
69 static int get_ch_index(int *status, unsigned int base)
70 {
71 int i;
72 for (i = 0; i < MAX_CHAN; i++) {
73 if (*status & (1 << (i + base))) {
74 *status = *status & ~(1 << (i + base));
75 pr_debug("MDMA: index %d New status %x\n", i, *status);
76 return i;
77 }
78 }
79 return -1;
80 }
81
82 /**
83 * get_block_ts - calculates dma transaction length
84 * @len: dma transfer length
85 * @tx_width: dma transfer src width
86 * @block_size: dma controller max block size
87 *
88 * Based on src width calculate the DMA trsaction length in data items
89 * return data items or FFFF if exceeds max length for block
90 */
91 static int get_block_ts(int len, int tx_width, int block_size)
92 {
93 int byte_width = 0, block_ts = 0;
94
95 switch (tx_width) {
96 case DMA_SLAVE_BUSWIDTH_1_BYTE:
97 byte_width = 1;
98 break;
99 case DMA_SLAVE_BUSWIDTH_2_BYTES:
100 byte_width = 2;
101 break;
102 case DMA_SLAVE_BUSWIDTH_4_BYTES:
103 default:
104 byte_width = 4;
105 break;
106 }
107
108 block_ts = len/byte_width;
109 if (block_ts > block_size)
110 block_ts = 0xFFFF;
111 return block_ts;
112 }
113
114 /*****************************************************************************
115 DMAC1 interrupt Functions*/
116
117 /**
118 * dmac1_mask_periphral_intr - mask the periphral interrupt
119 * @mid: dma device for which masking is required
120 *
121 * Masks the DMA periphral interrupt
122 * this is valid for DMAC1 family controllers only
123 * This controller should have periphral mask registers already mapped
124 */
125 static void dmac1_mask_periphral_intr(struct middma_device *mid)
126 {
127 u32 pimr;
128
129 if (mid->pimr_mask) {
130 pimr = readl(mid->mask_reg + LNW_PERIPHRAL_MASK);
131 pimr |= mid->pimr_mask;
132 writel(pimr, mid->mask_reg + LNW_PERIPHRAL_MASK);
133 }
134 return;
135 }
136
137 /**
138 * dmac1_unmask_periphral_intr - unmask the periphral interrupt
139 * @midc: dma channel for which masking is required
140 *
141 * UnMasks the DMA periphral interrupt,
142 * this is valid for DMAC1 family controllers only
143 * This controller should have periphral mask registers already mapped
144 */
145 static void dmac1_unmask_periphral_intr(struct intel_mid_dma_chan *midc)
146 {
147 u32 pimr;
148 struct middma_device *mid = to_middma_device(midc->chan.device);
149
150 if (mid->pimr_mask) {
151 pimr = readl(mid->mask_reg + LNW_PERIPHRAL_MASK);
152 pimr &= ~mid->pimr_mask;
153 writel(pimr, mid->mask_reg + LNW_PERIPHRAL_MASK);
154 }
155 return;
156 }
157
158 /**
159 * enable_dma_interrupt - enable the periphral interrupt
160 * @midc: dma channel for which enable interrupt is required
161 *
162 * Enable the DMA periphral interrupt,
163 * this is valid for DMAC1 family controllers only
164 * This controller should have periphral mask registers already mapped
165 */
166 static void enable_dma_interrupt(struct intel_mid_dma_chan *midc)
167 {
168 dmac1_unmask_periphral_intr(midc);
169
170 /*en ch interrupts*/
171 iowrite32(UNMASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_TFR);
172 iowrite32(UNMASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_ERR);
173 return;
174 }
175
176 /**
177 * disable_dma_interrupt - disable the periphral interrupt
178 * @midc: dma channel for which disable interrupt is required
179 *
180 * Disable the DMA periphral interrupt,
181 * this is valid for DMAC1 family controllers only
182 * This controller should have periphral mask registers already mapped
183 */
184 static void disable_dma_interrupt(struct intel_mid_dma_chan *midc)
185 {
186 /*Check LPE PISR, make sure fwd is disabled*/
187 iowrite32(MASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_BLOCK);
188 iowrite32(MASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_TFR);
189 iowrite32(MASK_INTR_REG(midc->ch_id), midc->dma_base + MASK_ERR);
190 return;
191 }
192
193 /*****************************************************************************
194 DMA channel helper Functions*/
195 /**
196 * mid_desc_get - get a descriptor
197 * @midc: dma channel for which descriptor is required
198 *
199 * Obtain a descriptor for the channel. Returns NULL if none are free.
200 * Once the descriptor is returned it is private until put on another
201 * list or freed
202 */
203 static struct intel_mid_dma_desc *midc_desc_get(struct intel_mid_dma_chan *midc)
204 {
205 struct intel_mid_dma_desc *desc, *_desc;
206 struct intel_mid_dma_desc *ret = NULL;
207
208 spin_lock_bh(&midc->lock);
209 list_for_each_entry_safe(desc, _desc, &midc->free_list, desc_node) {
210 if (async_tx_test_ack(&desc->txd)) {
211 list_del(&desc->desc_node);
212 ret = desc;
213 break;
214 }
215 }
216 spin_unlock_bh(&midc->lock);
217 return ret;
218 }
219
220 /**
221 * mid_desc_put - put a descriptor
222 * @midc: dma channel for which descriptor is required
223 * @desc: descriptor to put
224 *
225 * Return a descriptor from lwn_desc_get back to the free pool
226 */
227 static void midc_desc_put(struct intel_mid_dma_chan *midc,
228 struct intel_mid_dma_desc *desc)
229 {
230 if (desc) {
231 spin_lock_bh(&midc->lock);
232 list_add_tail(&desc->desc_node, &midc->free_list);
233 spin_unlock_bh(&midc->lock);
234 }
235 }
236 /**
237 * midc_dostart - begin a DMA transaction
238 * @midc: channel for which txn is to be started
239 * @first: first descriptor of series
240 *
241 * Load a transaction into the engine. This must be called with midc->lock
242 * held and bh disabled.
243 */
244 static void midc_dostart(struct intel_mid_dma_chan *midc,
245 struct intel_mid_dma_desc *first)
246 {
247 struct middma_device *mid = to_middma_device(midc->chan.device);
248
249 /* channel is idle */
250 if (midc->busy && test_ch_en(midc->dma_base, midc->ch_id)) {
251 /*error*/
252 pr_err("ERR_MDMA: channel is busy in start\n");
253 /* The tasklet will hopefully advance the queue... */
254 return;
255 }
256 midc->busy = true;
257 /*write registers and en*/
258 iowrite32(first->sar, midc->ch_regs + SAR);
259 iowrite32(first->dar, midc->ch_regs + DAR);
260 iowrite32(first->lli_phys, midc->ch_regs + LLP);
261 iowrite32(first->cfg_hi, midc->ch_regs + CFG_HIGH);
262 iowrite32(first->cfg_lo, midc->ch_regs + CFG_LOW);
263 iowrite32(first->ctl_lo, midc->ch_regs + CTL_LOW);
264 iowrite32(first->ctl_hi, midc->ch_regs + CTL_HIGH);
265 pr_debug("MDMA:TX SAR %x,DAR %x,CFGL %x,CFGH %x,CTLH %x, CTLL %x\n",
266 (int)first->sar, (int)first->dar, first->cfg_hi,
267 first->cfg_lo, first->ctl_hi, first->ctl_lo);
268 first->status = DMA_IN_PROGRESS;
269
270 iowrite32(ENABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);
271 }
272
273 /**
274 * midc_descriptor_complete - process completed descriptor
275 * @midc: channel owning the descriptor
276 * @desc: the descriptor itself
277 *
278 * Process a completed descriptor and perform any callbacks upon
279 * the completion. The completion handling drops the lock during the
280 * callbacks but must be called with the lock held.
281 */
282 static void midc_descriptor_complete(struct intel_mid_dma_chan *midc,
283 struct intel_mid_dma_desc *desc)
284 __releases(&midc->lock) __acquires(&midc->lock)
285 {
286 struct dma_async_tx_descriptor *txd = &desc->txd;
287 dma_async_tx_callback callback_txd = NULL;
288 struct intel_mid_dma_lli *llitem;
289 void *param_txd = NULL;
290
291 midc->chan.completed_cookie = txd->cookie;
292 callback_txd = txd->callback;
293 param_txd = txd->callback_param;
294
295 if (desc->lli != NULL) {
296 /*clear the DONE bit of completed LLI in memory*/
297 llitem = desc->lli + desc->current_lli;
298 llitem->ctl_hi &= CLEAR_DONE;
299 if (desc->current_lli < desc->lli_length-1)
300 (desc->current_lli)++;
301 else
302 desc->current_lli = 0;
303 }
304 spin_unlock_bh(&midc->lock);
305 if (callback_txd) {
306 pr_debug("MDMA: TXD callback set ... calling\n");
307 callback_txd(param_txd);
308 }
309 if (midc->raw_tfr) {
310 desc->status = DMA_SUCCESS;
311 if (desc->lli != NULL) {
312 pci_pool_free(desc->lli_pool, desc->lli,
313 desc->lli_phys);
314 pci_pool_destroy(desc->lli_pool);
315 desc->lli = NULL;
316 }
317 list_move(&desc->desc_node, &midc->free_list);
318 midc->busy = false;
319 }
320 spin_lock_bh(&midc->lock);
321
322 }
323 /**
324 * midc_scan_descriptors - check the descriptors in channel
325 * mark completed when tx is completete
326 * @mid: device
327 * @midc: channel to scan
328 *
329 * Walk the descriptor chain for the device and process any entries
330 * that are complete.
331 */
332 static void midc_scan_descriptors(struct middma_device *mid,
333 struct intel_mid_dma_chan *midc)
334 {
335 struct intel_mid_dma_desc *desc = NULL, *_desc = NULL;
336
337 /*tx is complete*/
338 list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
339 if (desc->status == DMA_IN_PROGRESS)
340 midc_descriptor_complete(midc, desc);
341 }
342 return;
343 }
344 /**
345 * midc_lli_fill_sg - Helper function to convert
346 * SG list to Linked List Items.
347 *@midc: Channel
348 *@desc: DMA descriptor
349 *@sglist: Pointer to SG list
350 *@sglen: SG list length
351 *@flags: DMA transaction flags
352 *
353 * Walk through the SG list and convert the SG list into Linked
354 * List Items (LLI).
355 */
356 static int midc_lli_fill_sg(struct intel_mid_dma_chan *midc,
357 struct intel_mid_dma_desc *desc,
358 struct scatterlist *sglist,
359 unsigned int sglen,
360 unsigned int flags)
361 {
362 struct intel_mid_dma_slave *mids;
363 struct scatterlist *sg;
364 dma_addr_t lli_next, sg_phy_addr;
365 struct intel_mid_dma_lli *lli_bloc_desc;
366 union intel_mid_dma_ctl_lo ctl_lo;
367 union intel_mid_dma_ctl_hi ctl_hi;
368 int i;
369
370 pr_debug("MDMA: Entered midc_lli_fill_sg\n");
371 mids = midc->mid_slave;
372
373 lli_bloc_desc = desc->lli;
374 lli_next = desc->lli_phys;
375
376 ctl_lo.ctl_lo = desc->ctl_lo;
377 ctl_hi.ctl_hi = desc->ctl_hi;
378 for_each_sg(sglist, sg, sglen, i) {
379 /*Populate CTL_LOW and LLI values*/
380 if (i != sglen - 1) {
381 lli_next = lli_next +
382 sizeof(struct intel_mid_dma_lli);
383 } else {
384 /*Check for circular list, otherwise terminate LLI to ZERO*/
385 if (flags & DMA_PREP_CIRCULAR_LIST) {
386 pr_debug("MDMA: LLI is configured in circular mode\n");
387 lli_next = desc->lli_phys;
388 } else {
389 lli_next = 0;
390 ctl_lo.ctlx.llp_dst_en = 0;
391 ctl_lo.ctlx.llp_src_en = 0;
392 }
393 }
394 /*Populate CTL_HI values*/
395 ctl_hi.ctlx.block_ts = get_block_ts(sg->length,
396 desc->width,
397 midc->dma->block_size);
398 /*Populate SAR and DAR values*/
399 sg_phy_addr = sg_phys(sg);
400 if (desc->dirn == DMA_MEM_TO_DEV) {
401 lli_bloc_desc->sar = sg_phy_addr;
402 lli_bloc_desc->dar = mids->dma_slave.dst_addr;
403 } else if (desc->dirn == DMA_DEV_TO_MEM) {
404 lli_bloc_desc->sar = mids->dma_slave.src_addr;
405 lli_bloc_desc->dar = sg_phy_addr;
406 }
407 /*Copy values into block descriptor in system memroy*/
408 lli_bloc_desc->llp = lli_next;
409 lli_bloc_desc->ctl_lo = ctl_lo.ctl_lo;
410 lli_bloc_desc->ctl_hi = ctl_hi.ctl_hi;
411
412 lli_bloc_desc++;
413 }
414 /*Copy very first LLI values to descriptor*/
415 desc->ctl_lo = desc->lli->ctl_lo;
416 desc->ctl_hi = desc->lli->ctl_hi;
417 desc->sar = desc->lli->sar;
418 desc->dar = desc->lli->dar;
419
420 return 0;
421 }
422 /*****************************************************************************
423 DMA engine callback Functions*/
424 /**
425 * intel_mid_dma_tx_submit - callback to submit DMA transaction
426 * @tx: dma engine descriptor
427 *
428 * Submit the DMA trasaction for this descriptor, start if ch idle
429 */
430 static dma_cookie_t intel_mid_dma_tx_submit(struct dma_async_tx_descriptor *tx)
431 {
432 struct intel_mid_dma_desc *desc = to_intel_mid_dma_desc(tx);
433 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(tx->chan);
434 dma_cookie_t cookie;
435
436 spin_lock_bh(&midc->lock);
437 cookie = midc->chan.cookie;
438
439 if (++cookie < 0)
440 cookie = 1;
441
442 midc->chan.cookie = cookie;
443 desc->txd.cookie = cookie;
444
445
446 if (list_empty(&midc->active_list))
447 list_add_tail(&desc->desc_node, &midc->active_list);
448 else
449 list_add_tail(&desc->desc_node, &midc->queue);
450
451 midc_dostart(midc, desc);
452 spin_unlock_bh(&midc->lock);
453
454 return cookie;
455 }
456
457 /**
458 * intel_mid_dma_issue_pending - callback to issue pending txn
459 * @chan: chan where pending trascation needs to be checked and submitted
460 *
461 * Call for scan to issue pending descriptors
462 */
463 static void intel_mid_dma_issue_pending(struct dma_chan *chan)
464 {
465 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
466
467 spin_lock_bh(&midc->lock);
468 if (!list_empty(&midc->queue))
469 midc_scan_descriptors(to_middma_device(chan->device), midc);
470 spin_unlock_bh(&midc->lock);
471 }
472
473 /**
474 * intel_mid_dma_tx_status - Return status of txn
475 * @chan: chan for where status needs to be checked
476 * @cookie: cookie for txn
477 * @txstate: DMA txn state
478 *
479 * Return status of DMA txn
480 */
481 static enum dma_status intel_mid_dma_tx_status(struct dma_chan *chan,
482 dma_cookie_t cookie,
483 struct dma_tx_state *txstate)
484 {
485 dma_cookie_t last_used;
486 dma_cookie_t last_complete;
487 int ret;
488
489 last_complete = chan->completed_cookie;
490 last_used = chan->cookie;
491
492 ret = dma_async_is_complete(cookie, last_complete, last_used);
493 if (ret != DMA_SUCCESS) {
494 spin_lock_bh(&midc->lock);
495 midc_scan_descriptors(to_middma_device(chan->device), midc);
496 spin_unlock_bh(&midc->lock);
497
498 last_complete = chan->completed_cookie;
499 last_used = chan->cookie;
500
501 ret = dma_async_is_complete(cookie, last_complete, last_used);
502 }
503
504 if (txstate) {
505 txstate->last = last_complete;
506 txstate->used = last_used;
507 txstate->residue = 0;
508 }
509 return ret;
510 }
511
512 static int dma_slave_control(struct dma_chan *chan, unsigned long arg)
513 {
514 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
515 struct dma_slave_config *slave = (struct dma_slave_config *)arg;
516 struct intel_mid_dma_slave *mid_slave;
517
518 BUG_ON(!midc);
519 BUG_ON(!slave);
520 pr_debug("MDMA: slave control called\n");
521
522 mid_slave = to_intel_mid_dma_slave(slave);
523
524 BUG_ON(!mid_slave);
525
526 midc->mid_slave = mid_slave;
527 return 0;
528 }
529 /**
530 * intel_mid_dma_device_control - DMA device control
531 * @chan: chan for DMA control
532 * @cmd: control cmd
533 * @arg: cmd arg value
534 *
535 * Perform DMA control command
536 */
537 static int intel_mid_dma_device_control(struct dma_chan *chan,
538 enum dma_ctrl_cmd cmd, unsigned long arg)
539 {
540 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
541 struct middma_device *mid = to_middma_device(chan->device);
542 struct intel_mid_dma_desc *desc, *_desc;
543 union intel_mid_dma_cfg_lo cfg_lo;
544
545 if (cmd == DMA_SLAVE_CONFIG)
546 return dma_slave_control(chan, arg);
547
548 if (cmd != DMA_TERMINATE_ALL)
549 return -ENXIO;
550
551 spin_lock_bh(&midc->lock);
552 if (midc->busy == false) {
553 spin_unlock_bh(&midc->lock);
554 return 0;
555 }
556 /*Suspend and disable the channel*/
557 cfg_lo.cfg_lo = ioread32(midc->ch_regs + CFG_LOW);
558 cfg_lo.cfgx.ch_susp = 1;
559 iowrite32(cfg_lo.cfg_lo, midc->ch_regs + CFG_LOW);
560 iowrite32(DISABLE_CHANNEL(midc->ch_id), mid->dma_base + DMA_CHAN_EN);
561 midc->busy = false;
562 /* Disable interrupts */
563 disable_dma_interrupt(midc);
564 midc->descs_allocated = 0;
565
566 spin_unlock_bh(&midc->lock);
567 list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
568 if (desc->lli != NULL) {
569 pci_pool_free(desc->lli_pool, desc->lli,
570 desc->lli_phys);
571 pci_pool_destroy(desc->lli_pool);
572 desc->lli = NULL;
573 }
574 list_move(&desc->desc_node, &midc->free_list);
575 }
576 return 0;
577 }
578
579
580 /**
581 * intel_mid_dma_prep_memcpy - Prep memcpy txn
582 * @chan: chan for DMA transfer
583 * @dest: destn address
584 * @src: src address
585 * @len: DMA transfer len
586 * @flags: DMA flags
587 *
588 * Perform a DMA memcpy. Note we support slave periphral DMA transfers only
589 * The periphral txn details should be filled in slave structure properly
590 * Returns the descriptor for this txn
591 */
592 static struct dma_async_tx_descriptor *intel_mid_dma_prep_memcpy(
593 struct dma_chan *chan, dma_addr_t dest,
594 dma_addr_t src, size_t len, unsigned long flags)
595 {
596 struct intel_mid_dma_chan *midc;
597 struct intel_mid_dma_desc *desc = NULL;
598 struct intel_mid_dma_slave *mids;
599 union intel_mid_dma_ctl_lo ctl_lo;
600 union intel_mid_dma_ctl_hi ctl_hi;
601 union intel_mid_dma_cfg_lo cfg_lo;
602 union intel_mid_dma_cfg_hi cfg_hi;
603 enum dma_slave_buswidth width;
604
605 pr_debug("MDMA: Prep for memcpy\n");
606 BUG_ON(!chan);
607 if (!len)
608 return NULL;
609
610 midc = to_intel_mid_dma_chan(chan);
611 BUG_ON(!midc);
612
613 mids = midc->mid_slave;
614 BUG_ON(!mids);
615
616 pr_debug("MDMA:called for DMA %x CH %d Length %zu\n",
617 midc->dma->pci_id, midc->ch_id, len);
618 pr_debug("MDMA:Cfg passed Mode %x, Dirn %x, HS %x, Width %x\n",
619 mids->cfg_mode, mids->dma_slave.direction,
620 mids->hs_mode, mids->dma_slave.src_addr_width);
621
622 /*calculate CFG_LO*/
623 if (mids->hs_mode == LNW_DMA_SW_HS) {
624 cfg_lo.cfg_lo = 0;
625 cfg_lo.cfgx.hs_sel_dst = 1;
626 cfg_lo.cfgx.hs_sel_src = 1;
627 } else if (mids->hs_mode == LNW_DMA_HW_HS)
628 cfg_lo.cfg_lo = 0x00000;
629
630 /*calculate CFG_HI*/
631 if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {
632 /*SW HS only*/
633 cfg_hi.cfg_hi = 0;
634 } else {
635 cfg_hi.cfg_hi = 0;
636 if (midc->dma->pimr_mask) {
637 cfg_hi.cfgx.protctl = 0x0; /*default value*/
638 cfg_hi.cfgx.fifo_mode = 1;
639 if (mids->dma_slave.direction == DMA_MEM_TO_DEV) {
640 cfg_hi.cfgx.src_per = 0;
641 if (mids->device_instance == 0)
642 cfg_hi.cfgx.dst_per = 3;
643 if (mids->device_instance == 1)
644 cfg_hi.cfgx.dst_per = 1;
645 } else if (mids->dma_slave.direction == DMA_DEV_TO_MEM) {
646 if (mids->device_instance == 0)
647 cfg_hi.cfgx.src_per = 2;
648 if (mids->device_instance == 1)
649 cfg_hi.cfgx.src_per = 0;
650 cfg_hi.cfgx.dst_per = 0;
651 }
652 } else {
653 cfg_hi.cfgx.protctl = 0x1; /*default value*/
654 cfg_hi.cfgx.src_per = cfg_hi.cfgx.dst_per =
655 midc->ch_id - midc->dma->chan_base;
656 }
657 }
658
659 /*calculate CTL_HI*/
660 ctl_hi.ctlx.reser = 0;
661 ctl_hi.ctlx.done = 0;
662 width = mids->dma_slave.src_addr_width;
663
664 ctl_hi.ctlx.block_ts = get_block_ts(len, width, midc->dma->block_size);
665 pr_debug("MDMA:calc len %d for block size %d\n",
666 ctl_hi.ctlx.block_ts, midc->dma->block_size);
667 /*calculate CTL_LO*/
668 ctl_lo.ctl_lo = 0;
669 ctl_lo.ctlx.int_en = 1;
670 ctl_lo.ctlx.dst_msize = mids->dma_slave.src_maxburst;
671 ctl_lo.ctlx.src_msize = mids->dma_slave.dst_maxburst;
672
673 /*
674 * Here we need some translation from "enum dma_slave_buswidth"
675 * to the format for our dma controller
676 * standard intel_mid_dmac's format
677 * 1 Byte 0b000
678 * 2 Bytes 0b001
679 * 4 Bytes 0b010
680 */
681 ctl_lo.ctlx.dst_tr_width = mids->dma_slave.dst_addr_width / 2;
682 ctl_lo.ctlx.src_tr_width = mids->dma_slave.src_addr_width / 2;
683
684 if (mids->cfg_mode == LNW_DMA_MEM_TO_MEM) {
685 ctl_lo.ctlx.tt_fc = 0;
686 ctl_lo.ctlx.sinc = 0;
687 ctl_lo.ctlx.dinc = 0;
688 } else {
689 if (mids->dma_slave.direction == DMA_MEM_TO_DEV) {
690 ctl_lo.ctlx.sinc = 0;
691 ctl_lo.ctlx.dinc = 2;
692 ctl_lo.ctlx.tt_fc = 1;
693 } else if (mids->dma_slave.direction == DMA_DEV_TO_MEM) {
694 ctl_lo.ctlx.sinc = 2;
695 ctl_lo.ctlx.dinc = 0;
696 ctl_lo.ctlx.tt_fc = 2;
697 }
698 }
699
700 pr_debug("MDMA:Calc CTL LO %x, CTL HI %x, CFG LO %x, CFG HI %x\n",
701 ctl_lo.ctl_lo, ctl_hi.ctl_hi, cfg_lo.cfg_lo, cfg_hi.cfg_hi);
702
703 enable_dma_interrupt(midc);
704
705 desc = midc_desc_get(midc);
706 if (desc == NULL)
707 goto err_desc_get;
708 desc->sar = src;
709 desc->dar = dest ;
710 desc->len = len;
711 desc->cfg_hi = cfg_hi.cfg_hi;
712 desc->cfg_lo = cfg_lo.cfg_lo;
713 desc->ctl_lo = ctl_lo.ctl_lo;
714 desc->ctl_hi = ctl_hi.ctl_hi;
715 desc->width = width;
716 desc->dirn = mids->dma_slave.direction;
717 desc->lli_phys = 0;
718 desc->lli = NULL;
719 desc->lli_pool = NULL;
720 return &desc->txd;
721
722 err_desc_get:
723 pr_err("ERR_MDMA: Failed to get desc\n");
724 midc_desc_put(midc, desc);
725 return NULL;
726 }
727 /**
728 * intel_mid_dma_prep_slave_sg - Prep slave sg txn
729 * @chan: chan for DMA transfer
730 * @sgl: scatter gather list
731 * @sg_len: length of sg txn
732 * @direction: DMA transfer dirtn
733 * @flags: DMA flags
734 *
735 * Prepares LLI based periphral transfer
736 */
737 static struct dma_async_tx_descriptor *intel_mid_dma_prep_slave_sg(
738 struct dma_chan *chan, struct scatterlist *sgl,
739 unsigned int sg_len, enum dma_transfer_direction direction,
740 unsigned long flags)
741 {
742 struct intel_mid_dma_chan *midc = NULL;
743 struct intel_mid_dma_slave *mids = NULL;
744 struct intel_mid_dma_desc *desc = NULL;
745 struct dma_async_tx_descriptor *txd = NULL;
746 union intel_mid_dma_ctl_lo ctl_lo;
747
748 pr_debug("MDMA: Prep for slave SG\n");
749
750 if (!sg_len) {
751 pr_err("MDMA: Invalid SG length\n");
752 return NULL;
753 }
754 midc = to_intel_mid_dma_chan(chan);
755 BUG_ON(!midc);
756
757 mids = midc->mid_slave;
758 BUG_ON(!mids);
759
760 if (!midc->dma->pimr_mask) {
761 /* We can still handle sg list with only one item */
762 if (sg_len == 1) {
763 txd = intel_mid_dma_prep_memcpy(chan,
764 mids->dma_slave.dst_addr,
765 mids->dma_slave.src_addr,
766 sgl->length,
767 flags);
768 return txd;
769 } else {
770 pr_warn("MDMA: SG list is not supported by this controller\n");
771 return NULL;
772 }
773 }
774
775 pr_debug("MDMA: SG Length = %d, direction = %d, Flags = %#lx\n",
776 sg_len, direction, flags);
777
778 txd = intel_mid_dma_prep_memcpy(chan, 0, 0, sgl->length, flags);
779 if (NULL == txd) {
780 pr_err("MDMA: Prep memcpy failed\n");
781 return NULL;
782 }
783
784 desc = to_intel_mid_dma_desc(txd);
785 desc->dirn = direction;
786 ctl_lo.ctl_lo = desc->ctl_lo;
787 ctl_lo.ctlx.llp_dst_en = 1;
788 ctl_lo.ctlx.llp_src_en = 1;
789 desc->ctl_lo = ctl_lo.ctl_lo;
790 desc->lli_length = sg_len;
791 desc->current_lli = 0;
792 /* DMA coherent memory pool for LLI descriptors*/
793 desc->lli_pool = pci_pool_create("intel_mid_dma_lli_pool",
794 midc->dma->pdev,
795 (sizeof(struct intel_mid_dma_lli)*sg_len),
796 32, 0);
797 if (NULL == desc->lli_pool) {
798 pr_err("MID_DMA:LLI pool create failed\n");
799 return NULL;
800 }
801
802 desc->lli = pci_pool_alloc(desc->lli_pool, GFP_KERNEL, &desc->lli_phys);
803 if (!desc->lli) {
804 pr_err("MID_DMA: LLI alloc failed\n");
805 pci_pool_destroy(desc->lli_pool);
806 return NULL;
807 }
808
809 midc_lli_fill_sg(midc, desc, sgl, sg_len, flags);
810 if (flags & DMA_PREP_INTERRUPT) {
811 iowrite32(UNMASK_INTR_REG(midc->ch_id),
812 midc->dma_base + MASK_BLOCK);
813 pr_debug("MDMA:Enabled Block interrupt\n");
814 }
815 return &desc->txd;
816 }
817
818 /**
819 * intel_mid_dma_free_chan_resources - Frees dma resources
820 * @chan: chan requiring attention
821 *
822 * Frees the allocated resources on this DMA chan
823 */
824 static void intel_mid_dma_free_chan_resources(struct dma_chan *chan)
825 {
826 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
827 struct middma_device *mid = to_middma_device(chan->device);
828 struct intel_mid_dma_desc *desc, *_desc;
829
830 if (true == midc->busy) {
831 /*trying to free ch in use!!!!!*/
832 pr_err("ERR_MDMA: trying to free ch in use\n");
833 }
834 spin_lock_bh(&midc->lock);
835 midc->descs_allocated = 0;
836 list_for_each_entry_safe(desc, _desc, &midc->active_list, desc_node) {
837 list_del(&desc->desc_node);
838 pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
839 }
840 list_for_each_entry_safe(desc, _desc, &midc->free_list, desc_node) {
841 list_del(&desc->desc_node);
842 pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
843 }
844 list_for_each_entry_safe(desc, _desc, &midc->queue, desc_node) {
845 list_del(&desc->desc_node);
846 pci_pool_free(mid->dma_pool, desc, desc->txd.phys);
847 }
848 spin_unlock_bh(&midc->lock);
849 midc->in_use = false;
850 midc->busy = false;
851 /* Disable CH interrupts */
852 iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_BLOCK);
853 iowrite32(MASK_INTR_REG(midc->ch_id), mid->dma_base + MASK_ERR);
854 pm_runtime_put(&mid->pdev->dev);
855 }
856
857 /**
858 * intel_mid_dma_alloc_chan_resources - Allocate dma resources
859 * @chan: chan requiring attention
860 *
861 * Allocates DMA resources on this chan
862 * Return the descriptors allocated
863 */
864 static int intel_mid_dma_alloc_chan_resources(struct dma_chan *chan)
865 {
866 struct intel_mid_dma_chan *midc = to_intel_mid_dma_chan(chan);
867 struct middma_device *mid = to_middma_device(chan->device);
868 struct intel_mid_dma_desc *desc;
869 dma_addr_t phys;
870 int i = 0;
871
872 pm_runtime_get_sync(&mid->pdev->dev);
873
874 if (mid->state == SUSPENDED) {
875 if (dma_resume(&mid->pdev->dev)) {
876 pr_err("ERR_MDMA: resume failed");
877 return -EFAULT;
878 }
879 }
880
881 /* ASSERT: channel is idle */
882 if (test_ch_en(mid->dma_base, midc->ch_id)) {
883 /*ch is not idle*/
884 pr_err("ERR_MDMA: ch not idle\n");
885 pm_runtime_put(&mid->pdev->dev);
886 return -EIO;
887 }
888 chan->completed_cookie = chan->cookie = 1;
889
890 spin_lock_bh(&midc->lock);
891 while (midc->descs_allocated < DESCS_PER_CHANNEL) {
892 spin_unlock_bh(&midc->lock);
893 desc = pci_pool_alloc(mid->dma_pool, GFP_KERNEL, &phys);
894 if (!desc) {
895 pr_err("ERR_MDMA: desc failed\n");
896 pm_runtime_put(&mid->pdev->dev);
897 return -ENOMEM;
898 /*check*/
899 }
900 dma_async_tx_descriptor_init(&desc->txd, chan);
901 desc->txd.tx_submit = intel_mid_dma_tx_submit;
902 desc->txd.flags = DMA_CTRL_ACK;
903 desc->txd.phys = phys;
904 spin_lock_bh(&midc->lock);
905 i = ++midc->descs_allocated;
906 list_add_tail(&desc->desc_node, &midc->free_list);
907 }
908 spin_unlock_bh(&midc->lock);
909 midc->in_use = true;
910 midc->busy = false;
911 pr_debug("MID_DMA: Desc alloc done ret: %d desc\n", i);
912 return i;
913 }
914
915 /**
916 * midc_handle_error - Handle DMA txn error
917 * @mid: controller where error occurred
918 * @midc: chan where error occurred
919 *
920 * Scan the descriptor for error
921 */
922 static void midc_handle_error(struct middma_device *mid,
923 struct intel_mid_dma_chan *midc)
924 {
925 midc_scan_descriptors(mid, midc);
926 }
927
928 /**
929 * dma_tasklet - DMA interrupt tasklet
930 * @data: tasklet arg (the controller structure)
931 *
932 * Scan the controller for interrupts for completion/error
933 * Clear the interrupt and call for handling completion/error
934 */
935 static void dma_tasklet(unsigned long data)
936 {
937 struct middma_device *mid = NULL;
938 struct intel_mid_dma_chan *midc = NULL;
939 u32 status, raw_tfr, raw_block;
940 int i;
941
942 mid = (struct middma_device *)data;
943 if (mid == NULL) {
944 pr_err("ERR_MDMA: tasklet Null param\n");
945 return;
946 }
947 pr_debug("MDMA: in tasklet for device %x\n", mid->pci_id);
948 raw_tfr = ioread32(mid->dma_base + RAW_TFR);
949 raw_block = ioread32(mid->dma_base + RAW_BLOCK);
950 status = raw_tfr | raw_block;
951 status &= mid->intr_mask;
952 while (status) {
953 /*txn interrupt*/
954 i = get_ch_index(&status, mid->chan_base);
955 if (i < 0) {
956 pr_err("ERR_MDMA:Invalid ch index %x\n", i);
957 return;
958 }
959 midc = &mid->ch[i];
960 if (midc == NULL) {
961 pr_err("ERR_MDMA:Null param midc\n");
962 return;
963 }
964 pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",
965 status, midc->ch_id, i);
966 midc->raw_tfr = raw_tfr;
967 midc->raw_block = raw_block;
968 spin_lock_bh(&midc->lock);
969 /*clearing this interrupts first*/
970 iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_TFR);
971 if (raw_block) {
972 iowrite32((1 << midc->ch_id),
973 mid->dma_base + CLEAR_BLOCK);
974 }
975 midc_scan_descriptors(mid, midc);
976 pr_debug("MDMA:Scan of desc... complete, unmasking\n");
977 iowrite32(UNMASK_INTR_REG(midc->ch_id),
978 mid->dma_base + MASK_TFR);
979 if (raw_block) {
980 iowrite32(UNMASK_INTR_REG(midc->ch_id),
981 mid->dma_base + MASK_BLOCK);
982 }
983 spin_unlock_bh(&midc->lock);
984 }
985
986 status = ioread32(mid->dma_base + RAW_ERR);
987 status &= mid->intr_mask;
988 while (status) {
989 /*err interrupt*/
990 i = get_ch_index(&status, mid->chan_base);
991 if (i < 0) {
992 pr_err("ERR_MDMA:Invalid ch index %x\n", i);
993 return;
994 }
995 midc = &mid->ch[i];
996 if (midc == NULL) {
997 pr_err("ERR_MDMA:Null param midc\n");
998 return;
999 }
1000 pr_debug("MDMA:Tx complete interrupt %x, Ch No %d Index %d\n",
1001 status, midc->ch_id, i);
1002
1003 iowrite32((1 << midc->ch_id), mid->dma_base + CLEAR_ERR);
1004 spin_lock_bh(&midc->lock);
1005 midc_handle_error(mid, midc);
1006 iowrite32(UNMASK_INTR_REG(midc->ch_id),
1007 mid->dma_base + MASK_ERR);
1008 spin_unlock_bh(&midc->lock);
1009 }
1010 pr_debug("MDMA:Exiting takslet...\n");
1011 return;
1012 }
1013
1014 static void dma_tasklet1(unsigned long data)
1015 {
1016 pr_debug("MDMA:in takslet1...\n");
1017 return dma_tasklet(data);
1018 }
1019
1020 static void dma_tasklet2(unsigned long data)
1021 {
1022 pr_debug("MDMA:in takslet2...\n");
1023 return dma_tasklet(data);
1024 }
1025
1026 /**
1027 * intel_mid_dma_interrupt - DMA ISR
1028 * @irq: IRQ where interrupt occurred
1029 * @data: ISR cllback data (the controller structure)
1030 *
1031 * See if this is our interrupt if so then schedule the tasklet
1032 * otherwise ignore
1033 */
1034 static irqreturn_t intel_mid_dma_interrupt(int irq, void *data)
1035 {
1036 struct middma_device *mid = data;
1037 u32 tfr_status, err_status;
1038 int call_tasklet = 0;
1039
1040 tfr_status = ioread32(mid->dma_base + RAW_TFR);
1041 err_status = ioread32(mid->dma_base + RAW_ERR);
1042 if (!tfr_status && !err_status)
1043 return IRQ_NONE;
1044
1045 /*DMA Interrupt*/
1046 pr_debug("MDMA:Got an interrupt on irq %d\n", irq);
1047 pr_debug("MDMA: Status %x, Mask %x\n", tfr_status, mid->intr_mask);
1048 tfr_status &= mid->intr_mask;
1049 if (tfr_status) {
1050 /*need to disable intr*/
1051 iowrite32((tfr_status << INT_MASK_WE), mid->dma_base + MASK_TFR);
1052 iowrite32((tfr_status << INT_MASK_WE), mid->dma_base + MASK_BLOCK);
1053 pr_debug("MDMA: Calling tasklet %x\n", tfr_status);
1054 call_tasklet = 1;
1055 }
1056 err_status &= mid->intr_mask;
1057 if (err_status) {
1058 iowrite32((err_status << INT_MASK_WE),
1059 mid->dma_base + MASK_ERR);
1060 call_tasklet = 1;
1061 }
1062 if (call_tasklet)
1063 tasklet_schedule(&mid->tasklet);
1064
1065 return IRQ_HANDLED;
1066 }
1067
1068 static irqreturn_t intel_mid_dma_interrupt1(int irq, void *data)
1069 {
1070 return intel_mid_dma_interrupt(irq, data);
1071 }
1072
1073 static irqreturn_t intel_mid_dma_interrupt2(int irq, void *data)
1074 {
1075 return intel_mid_dma_interrupt(irq, data);
1076 }
1077
1078 /**
1079 * mid_setup_dma - Setup the DMA controller
1080 * @pdev: Controller PCI device structure
1081 *
1082 * Initialize the DMA controller, channels, registers with DMA engine,
1083 * ISR. Initialize DMA controller channels.
1084 */
1085 static int mid_setup_dma(struct pci_dev *pdev)
1086 {
1087 struct middma_device *dma = pci_get_drvdata(pdev);
1088 int err, i;
1089
1090 /* DMA coherent memory pool for DMA descriptor allocations */
1091 dma->dma_pool = pci_pool_create("intel_mid_dma_desc_pool", pdev,
1092 sizeof(struct intel_mid_dma_desc),
1093 32, 0);
1094 if (NULL == dma->dma_pool) {
1095 pr_err("ERR_MDMA:pci_pool_create failed\n");
1096 err = -ENOMEM;
1097 goto err_dma_pool;
1098 }
1099
1100 INIT_LIST_HEAD(&dma->common.channels);
1101 dma->pci_id = pdev->device;
1102 if (dma->pimr_mask) {
1103 dma->mask_reg = ioremap(LNW_PERIPHRAL_MASK_BASE,
1104 LNW_PERIPHRAL_MASK_SIZE);
1105 if (dma->mask_reg == NULL) {
1106 pr_err("ERR_MDMA:Can't map periphral intr space !!\n");
1107 err = -ENOMEM;
1108 goto err_ioremap;
1109 }
1110 } else
1111 dma->mask_reg = NULL;
1112
1113 pr_debug("MDMA:Adding %d channel for this controller\n", dma->max_chan);
1114 /*init CH structures*/
1115 dma->intr_mask = 0;
1116 dma->state = RUNNING;
1117 for (i = 0; i < dma->max_chan; i++) {
1118 struct intel_mid_dma_chan *midch = &dma->ch[i];
1119
1120 midch->chan.device = &dma->common;
1121 midch->chan.cookie = 1;
1122 midch->ch_id = dma->chan_base + i;
1123 pr_debug("MDMA:Init CH %d, ID %d\n", i, midch->ch_id);
1124
1125 midch->dma_base = dma->dma_base;
1126 midch->ch_regs = dma->dma_base + DMA_CH_SIZE * midch->ch_id;
1127 midch->dma = dma;
1128 dma->intr_mask |= 1 << (dma->chan_base + i);
1129 spin_lock_init(&midch->lock);
1130
1131 INIT_LIST_HEAD(&midch->active_list);
1132 INIT_LIST_HEAD(&midch->queue);
1133 INIT_LIST_HEAD(&midch->free_list);
1134 /*mask interrupts*/
1135 iowrite32(MASK_INTR_REG(midch->ch_id),
1136 dma->dma_base + MASK_BLOCK);
1137 iowrite32(MASK_INTR_REG(midch->ch_id),
1138 dma->dma_base + MASK_SRC_TRAN);
1139 iowrite32(MASK_INTR_REG(midch->ch_id),
1140 dma->dma_base + MASK_DST_TRAN);
1141 iowrite32(MASK_INTR_REG(midch->ch_id),
1142 dma->dma_base + MASK_ERR);
1143 iowrite32(MASK_INTR_REG(midch->ch_id),
1144 dma->dma_base + MASK_TFR);
1145
1146 disable_dma_interrupt(midch);
1147 list_add_tail(&midch->chan.device_node, &dma->common.channels);
1148 }
1149 pr_debug("MDMA: Calc Mask as %x for this controller\n", dma->intr_mask);
1150
1151 /*init dma structure*/
1152 dma_cap_zero(dma->common.cap_mask);
1153 dma_cap_set(DMA_MEMCPY, dma->common.cap_mask);
1154 dma_cap_set(DMA_SLAVE, dma->common.cap_mask);
1155 dma_cap_set(DMA_PRIVATE, dma->common.cap_mask);
1156 dma->common.dev = &pdev->dev;
1157
1158 dma->common.device_alloc_chan_resources =
1159 intel_mid_dma_alloc_chan_resources;
1160 dma->common.device_free_chan_resources =
1161 intel_mid_dma_free_chan_resources;
1162
1163 dma->common.device_tx_status = intel_mid_dma_tx_status;
1164 dma->common.device_prep_dma_memcpy = intel_mid_dma_prep_memcpy;
1165 dma->common.device_issue_pending = intel_mid_dma_issue_pending;
1166 dma->common.device_prep_slave_sg = intel_mid_dma_prep_slave_sg;
1167 dma->common.device_control = intel_mid_dma_device_control;
1168
1169 /*enable dma cntrl*/
1170 iowrite32(REG_BIT0, dma->dma_base + DMA_CFG);
1171
1172 /*register irq */
1173 if (dma->pimr_mask) {
1174 pr_debug("MDMA:Requesting irq shared for DMAC1\n");
1175 err = request_irq(pdev->irq, intel_mid_dma_interrupt1,
1176 IRQF_SHARED, "INTEL_MID_DMAC1", dma);
1177 if (0 != err)
1178 goto err_irq;
1179 } else {
1180 dma->intr_mask = 0x03;
1181 pr_debug("MDMA:Requesting irq for DMAC2\n");
1182 err = request_irq(pdev->irq, intel_mid_dma_interrupt2,
1183 IRQF_SHARED, "INTEL_MID_DMAC2", dma);
1184 if (0 != err)
1185 goto err_irq;
1186 }
1187 /*register device w/ engine*/
1188 err = dma_async_device_register(&dma->common);
1189 if (0 != err) {
1190 pr_err("ERR_MDMA:device_register failed: %d\n", err);
1191 goto err_engine;
1192 }
1193 if (dma->pimr_mask) {
1194 pr_debug("setting up tasklet1 for DMAC1\n");
1195 tasklet_init(&dma->tasklet, dma_tasklet1, (unsigned long)dma);
1196 } else {
1197 pr_debug("setting up tasklet2 for DMAC2\n");
1198 tasklet_init(&dma->tasklet, dma_tasklet2, (unsigned long)dma);
1199 }
1200 return 0;
1201
1202 err_engine:
1203 free_irq(pdev->irq, dma);
1204 err_irq:
1205 if (dma->mask_reg)
1206 iounmap(dma->mask_reg);
1207 err_ioremap:
1208 pci_pool_destroy(dma->dma_pool);
1209 err_dma_pool:
1210 pr_err("ERR_MDMA:setup_dma failed: %d\n", err);
1211 return err;
1212
1213 }
1214
1215 /**
1216 * middma_shutdown - Shutdown the DMA controller
1217 * @pdev: Controller PCI device structure
1218 *
1219 * Called by remove
1220 * Unregister DMa controller, clear all structures and free interrupt
1221 */
1222 static void middma_shutdown(struct pci_dev *pdev)
1223 {
1224 struct middma_device *device = pci_get_drvdata(pdev);
1225
1226 dma_async_device_unregister(&device->common);
1227 pci_pool_destroy(device->dma_pool);
1228 if (device->mask_reg)
1229 iounmap(device->mask_reg);
1230 if (device->dma_base)
1231 iounmap(device->dma_base);
1232 free_irq(pdev->irq, device);
1233 return;
1234 }
1235
1236 /**
1237 * intel_mid_dma_probe - PCI Probe
1238 * @pdev: Controller PCI device structure
1239 * @id: pci device id structure
1240 *
1241 * Initialize the PCI device, map BARs, query driver data.
1242 * Call setup_dma to complete contoller and chan initilzation
1243 */
1244 static int __devinit intel_mid_dma_probe(struct pci_dev *pdev,
1245 const struct pci_device_id *id)
1246 {
1247 struct middma_device *device;
1248 u32 base_addr, bar_size;
1249 struct intel_mid_dma_probe_info *info;
1250 int err;
1251
1252 pr_debug("MDMA: probe for %x\n", pdev->device);
1253 info = (void *)id->driver_data;
1254 pr_debug("MDMA: CH %d, base %d, block len %d, Periphral mask %x\n",
1255 info->max_chan, info->ch_base,
1256 info->block_size, info->pimr_mask);
1257
1258 err = pci_enable_device(pdev);
1259 if (err)
1260 goto err_enable_device;
1261
1262 err = pci_request_regions(pdev, "intel_mid_dmac");
1263 if (err)
1264 goto err_request_regions;
1265
1266 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1267 if (err)
1268 goto err_set_dma_mask;
1269
1270 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1271 if (err)
1272 goto err_set_dma_mask;
1273
1274 device = kzalloc(sizeof(*device), GFP_KERNEL);
1275 if (!device) {
1276 pr_err("ERR_MDMA:kzalloc failed probe\n");
1277 err = -ENOMEM;
1278 goto err_kzalloc;
1279 }
1280 device->pdev = pci_dev_get(pdev);
1281
1282 base_addr = pci_resource_start(pdev, 0);
1283 bar_size = pci_resource_len(pdev, 0);
1284 device->dma_base = ioremap_nocache(base_addr, DMA_REG_SIZE);
1285 if (!device->dma_base) {
1286 pr_err("ERR_MDMA:ioremap failed\n");
1287 err = -ENOMEM;
1288 goto err_ioremap;
1289 }
1290 pci_set_drvdata(pdev, device);
1291 pci_set_master(pdev);
1292 device->max_chan = info->max_chan;
1293 device->chan_base = info->ch_base;
1294 device->block_size = info->block_size;
1295 device->pimr_mask = info->pimr_mask;
1296
1297 err = mid_setup_dma(pdev);
1298 if (err)
1299 goto err_dma;
1300
1301 pm_runtime_put_noidle(&pdev->dev);
1302 pm_runtime_allow(&pdev->dev);
1303 return 0;
1304
1305 err_dma:
1306 iounmap(device->dma_base);
1307 err_ioremap:
1308 pci_dev_put(pdev);
1309 kfree(device);
1310 err_kzalloc:
1311 err_set_dma_mask:
1312 pci_release_regions(pdev);
1313 pci_disable_device(pdev);
1314 err_request_regions:
1315 err_enable_device:
1316 pr_err("ERR_MDMA:Probe failed %d\n", err);
1317 return err;
1318 }
1319
1320 /**
1321 * intel_mid_dma_remove - PCI remove
1322 * @pdev: Controller PCI device structure
1323 *
1324 * Free up all resources and data
1325 * Call shutdown_dma to complete contoller and chan cleanup
1326 */
1327 static void __devexit intel_mid_dma_remove(struct pci_dev *pdev)
1328 {
1329 struct middma_device *device = pci_get_drvdata(pdev);
1330
1331 pm_runtime_get_noresume(&pdev->dev);
1332 pm_runtime_forbid(&pdev->dev);
1333 middma_shutdown(pdev);
1334 pci_dev_put(pdev);
1335 kfree(device);
1336 pci_release_regions(pdev);
1337 pci_disable_device(pdev);
1338 }
1339
1340 /* Power Management */
1341 /*
1342 * dma_suspend - PCI suspend function
1343 *
1344 * @pci: PCI device structure
1345 * @state: PM message
1346 *
1347 * This function is called by OS when a power event occurs
1348 */
1349 static int dma_suspend(struct device *dev)
1350 {
1351 struct pci_dev *pci = to_pci_dev(dev);
1352 int i;
1353 struct middma_device *device = pci_get_drvdata(pci);
1354 pr_debug("MDMA: dma_suspend called\n");
1355
1356 for (i = 0; i < device->max_chan; i++) {
1357 if (device->ch[i].in_use)
1358 return -EAGAIN;
1359 }
1360 dmac1_mask_periphral_intr(device);
1361 device->state = SUSPENDED;
1362 pci_save_state(pci);
1363 pci_disable_device(pci);
1364 pci_set_power_state(pci, PCI_D3hot);
1365 return 0;
1366 }
1367
1368 /**
1369 * dma_resume - PCI resume function
1370 *
1371 * @pci: PCI device structure
1372 *
1373 * This function is called by OS when a power event occurs
1374 */
1375 int dma_resume(struct device *dev)
1376 {
1377 struct pci_dev *pci = to_pci_dev(dev);
1378 int ret;
1379 struct middma_device *device = pci_get_drvdata(pci);
1380
1381 pr_debug("MDMA: dma_resume called\n");
1382 pci_set_power_state(pci, PCI_D0);
1383 pci_restore_state(pci);
1384 ret = pci_enable_device(pci);
1385 if (ret) {
1386 pr_err("MDMA: device can't be enabled for %x\n", pci->device);
1387 return ret;
1388 }
1389 device->state = RUNNING;
1390 iowrite32(REG_BIT0, device->dma_base + DMA_CFG);
1391 return 0;
1392 }
1393
1394 static int dma_runtime_suspend(struct device *dev)
1395 {
1396 struct pci_dev *pci_dev = to_pci_dev(dev);
1397 struct middma_device *device = pci_get_drvdata(pci_dev);
1398
1399 device->state = SUSPENDED;
1400 return 0;
1401 }
1402
1403 static int dma_runtime_resume(struct device *dev)
1404 {
1405 struct pci_dev *pci_dev = to_pci_dev(dev);
1406 struct middma_device *device = pci_get_drvdata(pci_dev);
1407
1408 device->state = RUNNING;
1409 iowrite32(REG_BIT0, device->dma_base + DMA_CFG);
1410 return 0;
1411 }
1412
1413 static int dma_runtime_idle(struct device *dev)
1414 {
1415 struct pci_dev *pdev = to_pci_dev(dev);
1416 struct middma_device *device = pci_get_drvdata(pdev);
1417 int i;
1418
1419 for (i = 0; i < device->max_chan; i++) {
1420 if (device->ch[i].in_use)
1421 return -EAGAIN;
1422 }
1423
1424 return pm_schedule_suspend(dev, 0);
1425 }
1426
1427 /******************************************************************************
1428 * PCI stuff
1429 */
1430 static struct pci_device_id intel_mid_dma_ids[] = {
1431 { PCI_VDEVICE(INTEL, INTEL_MID_DMAC1_ID), INFO(2, 6, 4095, 0x200020)},
1432 { PCI_VDEVICE(INTEL, INTEL_MID_DMAC2_ID), INFO(2, 0, 2047, 0)},
1433 { PCI_VDEVICE(INTEL, INTEL_MID_GP_DMAC2_ID), INFO(2, 0, 2047, 0)},
1434 { PCI_VDEVICE(INTEL, INTEL_MFLD_DMAC1_ID), INFO(4, 0, 4095, 0x400040)},
1435 { 0, }
1436 };
1437 MODULE_DEVICE_TABLE(pci, intel_mid_dma_ids);
1438
1439 static const struct dev_pm_ops intel_mid_dma_pm = {
1440 .runtime_suspend = dma_runtime_suspend,
1441 .runtime_resume = dma_runtime_resume,
1442 .runtime_idle = dma_runtime_idle,
1443 .suspend = dma_suspend,
1444 .resume = dma_resume,
1445 };
1446
1447 static struct pci_driver intel_mid_dma_pci_driver = {
1448 .name = "Intel MID DMA",
1449 .id_table = intel_mid_dma_ids,
1450 .probe = intel_mid_dma_probe,
1451 .remove = __devexit_p(intel_mid_dma_remove),
1452 #ifdef CONFIG_PM
1453 .driver = {
1454 .pm = &intel_mid_dma_pm,
1455 },
1456 #endif
1457 };
1458
1459 static int __init intel_mid_dma_init(void)
1460 {
1461 pr_debug("INFO_MDMA: LNW DMA Driver Version %s\n",
1462 INTEL_MID_DMA_DRIVER_VERSION);
1463 return pci_register_driver(&intel_mid_dma_pci_driver);
1464 }
1465 fs_initcall(intel_mid_dma_init);
1466
1467 static void __exit intel_mid_dma_exit(void)
1468 {
1469 pci_unregister_driver(&intel_mid_dma_pci_driver);
1470 }
1471 module_exit(intel_mid_dma_exit);
1472
1473 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
1474 MODULE_DESCRIPTION("Intel (R) MID DMAC Driver");
1475 MODULE_LICENSE("GPL v2");
1476 MODULE_VERSION(INTEL_MID_DMA_DRIVER_VERSION);