blob: 35c143cb88da1c676d47f714f3fed146010083fc [file] [log] [blame]
Yong Wang0c42bd02010-07-30 16:23:03 +08001/*
2 * Topcliff PCH DMA controller driver
3 * Copyright (c) 2010 Intel Corporation
Tomoya MORINAGAe79e72b2011-11-17 16:14:22 +09004 * Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
Yong Wang0c42bd02010-07-30 16:23:03 +08005 *
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.
9 *
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.
14 *
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.
18 */
19
20#include <linux/dmaengine.h>
21#include <linux/dma-mapping.h>
22#include <linux/init.h>
23#include <linux/pci.h>
Vinod Koula15783c2014-05-22 18:50:49 +053024#include <linux/slab.h>
Yong Wang0c42bd02010-07-30 16:23:03 +080025#include <linux/interrupt.h>
26#include <linux/module.h>
27#include <linux/pch_dma.h>
28
Russell King - ARM Linuxd2ebfb32012-03-06 22:34:26 +000029#include "dmaengine.h"
30
Yong Wang0c42bd02010-07-30 16:23:03 +080031#define DRV_NAME "pch-dma"
32
33#define DMA_CTL0_DISABLE 0x0
34#define DMA_CTL0_SG 0x1
35#define DMA_CTL0_ONESHOT 0x2
36#define DMA_CTL0_MODE_MASK_BITS 0x3
37#define DMA_CTL0_DIR_SHIFT_BITS 2
38#define DMA_CTL0_BITS_PER_CH 4
39
40#define DMA_CTL2_START_SHIFT_BITS 8
41#define DMA_CTL2_IRQ_ENABLE_MASK ((1UL << DMA_CTL2_START_SHIFT_BITS) - 1)
42
43#define DMA_STATUS_IDLE 0x0
44#define DMA_STATUS_DESC_READ 0x1
45#define DMA_STATUS_WAIT 0x2
46#define DMA_STATUS_ACCESS 0x3
47#define DMA_STATUS_BITS_PER_CH 2
48#define DMA_STATUS_MASK_BITS 0x3
49#define DMA_STATUS_SHIFT_BITS 16
50#define DMA_STATUS_IRQ(x) (0x1 << (x))
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +090051#define DMA_STATUS0_ERR(x) (0x1 << ((x) + 8))
52#define DMA_STATUS2_ERR(x) (0x1 << (x))
Yong Wang0c42bd02010-07-30 16:23:03 +080053
54#define DMA_DESC_WIDTH_SHIFT_BITS 12
55#define DMA_DESC_WIDTH_1_BYTE (0x3 << DMA_DESC_WIDTH_SHIFT_BITS)
56#define DMA_DESC_WIDTH_2_BYTES (0x2 << DMA_DESC_WIDTH_SHIFT_BITS)
57#define DMA_DESC_WIDTH_4_BYTES (0x0 << DMA_DESC_WIDTH_SHIFT_BITS)
58#define DMA_DESC_MAX_COUNT_1_BYTE 0x3FF
59#define DMA_DESC_MAX_COUNT_2_BYTES 0x3FF
60#define DMA_DESC_MAX_COUNT_4_BYTES 0x7FF
61#define DMA_DESC_END_WITHOUT_IRQ 0x0
62#define DMA_DESC_END_WITH_IRQ 0x1
63#define DMA_DESC_FOLLOW_WITHOUT_IRQ 0x2
64#define DMA_DESC_FOLLOW_WITH_IRQ 0x3
65
Tomoya MORINAGAc43f1502011-10-11 21:43:21 +090066#define MAX_CHAN_NR 12
Yong Wang0c42bd02010-07-30 16:23:03 +080067
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +090068#define DMA_MASK_CTL0_MODE 0x33333333
69#define DMA_MASK_CTL2_MODE 0x00003333
70
Yong Wang0c42bd02010-07-30 16:23:03 +080071static unsigned int init_nr_desc_per_channel = 64;
72module_param(init_nr_desc_per_channel, uint, 0644);
73MODULE_PARM_DESC(init_nr_desc_per_channel,
74 "initial descriptors per channel (default: 64)");
75
76struct pch_dma_desc_regs {
77 u32 dev_addr;
78 u32 mem_addr;
79 u32 size;
80 u32 next;
81};
82
83struct pch_dma_regs {
84 u32 dma_ctl0;
85 u32 dma_ctl1;
86 u32 dma_ctl2;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +090087 u32 dma_ctl3;
Yong Wang0c42bd02010-07-30 16:23:03 +080088 u32 dma_sts0;
89 u32 dma_sts1;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +090090 u32 dma_sts2;
Yong Wang0c42bd02010-07-30 16:23:03 +080091 u32 reserved3;
Tomoya MORINAGA26d890f2011-02-18 10:01:21 +053092 struct pch_dma_desc_regs desc[MAX_CHAN_NR];
Yong Wang0c42bd02010-07-30 16:23:03 +080093};
94
95struct pch_dma_desc {
96 struct pch_dma_desc_regs regs;
97 struct dma_async_tx_descriptor txd;
98 struct list_head desc_node;
99 struct list_head tx_list;
100};
101
102struct pch_dma_chan {
103 struct dma_chan chan;
104 void __iomem *membase;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530105 enum dma_transfer_direction dir;
Yong Wang0c42bd02010-07-30 16:23:03 +0800106 struct tasklet_struct tasklet;
107 unsigned long err_status;
108
109 spinlock_t lock;
110
Yong Wang0c42bd02010-07-30 16:23:03 +0800111 struct list_head active_list;
112 struct list_head queue;
113 struct list_head free_list;
114 unsigned int descs_allocated;
115};
116
117#define PDC_DEV_ADDR 0x00
118#define PDC_MEM_ADDR 0x04
119#define PDC_SIZE 0x08
120#define PDC_NEXT 0x0C
121
122#define channel_readl(pdc, name) \
123 readl((pdc)->membase + PDC_##name)
124#define channel_writel(pdc, name, val) \
125 writel((val), (pdc)->membase + PDC_##name)
126
127struct pch_dma {
128 struct dma_device dma;
129 void __iomem *membase;
130 struct pci_pool *pool;
131 struct pch_dma_regs regs;
132 struct pch_dma_desc_regs ch_regs[MAX_CHAN_NR];
Tomoya MORINAGA26d890f2011-02-18 10:01:21 +0530133 struct pch_dma_chan channels[MAX_CHAN_NR];
Yong Wang0c42bd02010-07-30 16:23:03 +0800134};
135
136#define PCH_DMA_CTL0 0x00
137#define PCH_DMA_CTL1 0x04
138#define PCH_DMA_CTL2 0x08
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900139#define PCH_DMA_CTL3 0x0C
Yong Wang0c42bd02010-07-30 16:23:03 +0800140#define PCH_DMA_STS0 0x10
141#define PCH_DMA_STS1 0x14
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900142#define PCH_DMA_STS2 0x18
Yong Wang0c42bd02010-07-30 16:23:03 +0800143
144#define dma_readl(pd, name) \
Yong Wang61cd2202010-08-05 10:38:43 +0800145 readl((pd)->membase + PCH_DMA_##name)
Yong Wang0c42bd02010-07-30 16:23:03 +0800146#define dma_writel(pd, name, val) \
Yong Wang61cd2202010-08-05 10:38:43 +0800147 writel((val), (pd)->membase + PCH_DMA_##name)
Yong Wang0c42bd02010-07-30 16:23:03 +0800148
Tomoya MORINAGA08645fd2011-05-09 16:09:36 +0900149static inline
150struct pch_dma_desc *to_pd_desc(struct dma_async_tx_descriptor *txd)
Yong Wang0c42bd02010-07-30 16:23:03 +0800151{
152 return container_of(txd, struct pch_dma_desc, txd);
153}
154
155static inline struct pch_dma_chan *to_pd_chan(struct dma_chan *chan)
156{
157 return container_of(chan, struct pch_dma_chan, chan);
158}
159
160static inline struct pch_dma *to_pd(struct dma_device *ddev)
161{
162 return container_of(ddev, struct pch_dma, dma);
163}
164
165static inline struct device *chan2dev(struct dma_chan *chan)
166{
167 return &chan->dev->device;
168}
169
170static inline struct device *chan2parent(struct dma_chan *chan)
171{
172 return chan->dev->device.parent;
173}
174
Tomoya MORINAGA08645fd2011-05-09 16:09:36 +0900175static inline
176struct pch_dma_desc *pdc_first_active(struct pch_dma_chan *pd_chan)
Yong Wang0c42bd02010-07-30 16:23:03 +0800177{
178 return list_first_entry(&pd_chan->active_list,
179 struct pch_dma_desc, desc_node);
180}
181
Tomoya MORINAGA08645fd2011-05-09 16:09:36 +0900182static inline
183struct pch_dma_desc *pdc_first_queued(struct pch_dma_chan *pd_chan)
Yong Wang0c42bd02010-07-30 16:23:03 +0800184{
185 return list_first_entry(&pd_chan->queue,
186 struct pch_dma_desc, desc_node);
187}
188
189static void pdc_enable_irq(struct dma_chan *chan, int enable)
190{
191 struct pch_dma *pd = to_pd(chan->device);
192 u32 val;
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900193 int pos;
194
195 if (chan->chan_id < 8)
196 pos = chan->chan_id;
197 else
198 pos = chan->chan_id + 8;
Yong Wang0c42bd02010-07-30 16:23:03 +0800199
200 val = dma_readl(pd, CTL2);
201
202 if (enable)
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900203 val |= 0x1 << pos;
Yong Wang0c42bd02010-07-30 16:23:03 +0800204 else
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900205 val &= ~(0x1 << pos);
Yong Wang0c42bd02010-07-30 16:23:03 +0800206
207 dma_writel(pd, CTL2, val);
208
209 dev_dbg(chan2dev(chan), "pdc_enable_irq: chan %d -> %x\n",
210 chan->chan_id, val);
211}
212
213static void pdc_set_dir(struct dma_chan *chan)
214{
215 struct pch_dma_chan *pd_chan = to_pd_chan(chan);
216 struct pch_dma *pd = to_pd(chan->device);
217 u32 val;
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900218 u32 mask_mode;
219 u32 mask_ctl;
Yong Wang0c42bd02010-07-30 16:23:03 +0800220
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900221 if (chan->chan_id < 8) {
222 val = dma_readl(pd, CTL0);
Yong Wang0c42bd02010-07-30 16:23:03 +0800223
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900224 mask_mode = DMA_CTL0_MODE_MASK_BITS <<
225 (DMA_CTL0_BITS_PER_CH * chan->chan_id);
226 mask_ctl = DMA_MASK_CTL0_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
227 (DMA_CTL0_BITS_PER_CH * chan->chan_id));
228 val &= mask_mode;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530229 if (pd_chan->dir == DMA_MEM_TO_DEV)
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900230 val |= 0x1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +
231 DMA_CTL0_DIR_SHIFT_BITS);
232 else
233 val &= ~(0x1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +
234 DMA_CTL0_DIR_SHIFT_BITS));
Yong Wang0c42bd02010-07-30 16:23:03 +0800235
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900236 val |= mask_ctl;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900237 dma_writel(pd, CTL0, val);
238 } else {
239 int ch = chan->chan_id - 8; /* ch8-->0 ch9-->1 ... ch11->3 */
240 val = dma_readl(pd, CTL3);
241
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900242 mask_mode = DMA_CTL0_MODE_MASK_BITS <<
243 (DMA_CTL0_BITS_PER_CH * ch);
244 mask_ctl = DMA_MASK_CTL2_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
245 (DMA_CTL0_BITS_PER_CH * ch));
246 val &= mask_mode;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530247 if (pd_chan->dir == DMA_MEM_TO_DEV)
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900248 val |= 0x1 << (DMA_CTL0_BITS_PER_CH * ch +
249 DMA_CTL0_DIR_SHIFT_BITS);
250 else
251 val &= ~(0x1 << (DMA_CTL0_BITS_PER_CH * ch +
252 DMA_CTL0_DIR_SHIFT_BITS));
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900253 val |= mask_ctl;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900254 dma_writel(pd, CTL3, val);
255 }
Yong Wang0c42bd02010-07-30 16:23:03 +0800256
257 dev_dbg(chan2dev(chan), "pdc_set_dir: chan %d -> %x\n",
258 chan->chan_id, val);
259}
260
261static void pdc_set_mode(struct dma_chan *chan, u32 mode)
262{
263 struct pch_dma *pd = to_pd(chan->device);
264 u32 val;
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900265 u32 mask_ctl;
266 u32 mask_dir;
Yong Wang0c42bd02010-07-30 16:23:03 +0800267
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900268 if (chan->chan_id < 8) {
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900269 mask_ctl = DMA_MASK_CTL0_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
270 (DMA_CTL0_BITS_PER_CH * chan->chan_id));
271 mask_dir = 1 << (DMA_CTL0_BITS_PER_CH * chan->chan_id +\
272 DMA_CTL0_DIR_SHIFT_BITS);
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900273 val = dma_readl(pd, CTL0);
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900274 val &= mask_dir;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900275 val |= mode << (DMA_CTL0_BITS_PER_CH * chan->chan_id);
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900276 val |= mask_ctl;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900277 dma_writel(pd, CTL0, val);
278 } else {
279 int ch = chan->chan_id - 8; /* ch8-->0 ch9-->1 ... ch11->3 */
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900280 mask_ctl = DMA_MASK_CTL2_MODE & ~(DMA_CTL0_MODE_MASK_BITS <<
281 (DMA_CTL0_BITS_PER_CH * ch));
282 mask_dir = 1 << (DMA_CTL0_BITS_PER_CH * ch +\
283 DMA_CTL0_DIR_SHIFT_BITS);
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900284 val = dma_readl(pd, CTL3);
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900285 val &= mask_dir;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900286 val |= mode << (DMA_CTL0_BITS_PER_CH * ch);
Tomoya MORINAGA0b052f42011-07-14 09:52:38 +0900287 val |= mask_ctl;
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900288 dma_writel(pd, CTL3, val);
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900289 }
Yong Wang0c42bd02010-07-30 16:23:03 +0800290
291 dev_dbg(chan2dev(chan), "pdc_set_mode: chan %d -> %x\n",
292 chan->chan_id, val);
293}
294
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900295static u32 pdc_get_status0(struct pch_dma_chan *pd_chan)
Yong Wang0c42bd02010-07-30 16:23:03 +0800296{
297 struct pch_dma *pd = to_pd(pd_chan->chan.device);
298 u32 val;
299
300 val = dma_readl(pd, STS0);
301 return DMA_STATUS_MASK_BITS & (val >> (DMA_STATUS_SHIFT_BITS +
302 DMA_STATUS_BITS_PER_CH * pd_chan->chan.chan_id));
303}
304
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900305static u32 pdc_get_status2(struct pch_dma_chan *pd_chan)
306{
307 struct pch_dma *pd = to_pd(pd_chan->chan.device);
308 u32 val;
309
310 val = dma_readl(pd, STS2);
311 return DMA_STATUS_MASK_BITS & (val >> (DMA_STATUS_SHIFT_BITS +
312 DMA_STATUS_BITS_PER_CH * (pd_chan->chan.chan_id - 8)));
313}
314
Yong Wang0c42bd02010-07-30 16:23:03 +0800315static bool pdc_is_idle(struct pch_dma_chan *pd_chan)
316{
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900317 u32 sts;
318
319 if (pd_chan->chan.chan_id < 8)
320 sts = pdc_get_status0(pd_chan);
321 else
322 sts = pdc_get_status2(pd_chan);
323
324
325 if (sts == DMA_STATUS_IDLE)
Yong Wang0c42bd02010-07-30 16:23:03 +0800326 return true;
327 else
328 return false;
329}
330
331static void pdc_dostart(struct pch_dma_chan *pd_chan, struct pch_dma_desc* desc)
332{
Yong Wang0c42bd02010-07-30 16:23:03 +0800333 if (!pdc_is_idle(pd_chan)) {
334 dev_err(chan2dev(&pd_chan->chan),
335 "BUG: Attempt to start non-idle channel\n");
336 return;
337 }
338
Yong Wang0c42bd02010-07-30 16:23:03 +0800339 dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> dev_addr: %x\n",
340 pd_chan->chan.chan_id, desc->regs.dev_addr);
341 dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> mem_addr: %x\n",
342 pd_chan->chan.chan_id, desc->regs.mem_addr);
343 dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> size: %x\n",
344 pd_chan->chan.chan_id, desc->regs.size);
345 dev_dbg(chan2dev(&pd_chan->chan), "chan %d -> next: %x\n",
346 pd_chan->chan.chan_id, desc->regs.next);
347
Tomoya MORINAGA943d8d82010-12-01 19:49:48 +0900348 if (list_empty(&desc->tx_list)) {
349 channel_writel(pd_chan, DEV_ADDR, desc->regs.dev_addr);
350 channel_writel(pd_chan, MEM_ADDR, desc->regs.mem_addr);
351 channel_writel(pd_chan, SIZE, desc->regs.size);
352 channel_writel(pd_chan, NEXT, desc->regs.next);
Yong Wang0c42bd02010-07-30 16:23:03 +0800353 pdc_set_mode(&pd_chan->chan, DMA_CTL0_ONESHOT);
Tomoya MORINAGA943d8d82010-12-01 19:49:48 +0900354 } else {
355 channel_writel(pd_chan, NEXT, desc->txd.phys);
Yong Wang0c42bd02010-07-30 16:23:03 +0800356 pdc_set_mode(&pd_chan->chan, DMA_CTL0_SG);
Tomoya MORINAGA943d8d82010-12-01 19:49:48 +0900357 }
Yong Wang0c42bd02010-07-30 16:23:03 +0800358}
359
360static void pdc_chain_complete(struct pch_dma_chan *pd_chan,
361 struct pch_dma_desc *desc)
362{
363 struct dma_async_tx_descriptor *txd = &desc->txd;
364 dma_async_tx_callback callback = txd->callback;
365 void *param = txd->callback_param;
366
367 list_splice_init(&desc->tx_list, &pd_chan->free_list);
368 list_move(&desc->desc_node, &pd_chan->free_list);
369
370 if (callback)
371 callback(param);
372}
373
374static void pdc_complete_all(struct pch_dma_chan *pd_chan)
375{
376 struct pch_dma_desc *desc, *_d;
377 LIST_HEAD(list);
378
379 BUG_ON(!pdc_is_idle(pd_chan));
380
381 if (!list_empty(&pd_chan->queue))
382 pdc_dostart(pd_chan, pdc_first_queued(pd_chan));
383
384 list_splice_init(&pd_chan->active_list, &list);
385 list_splice_init(&pd_chan->queue, &pd_chan->active_list);
386
387 list_for_each_entry_safe(desc, _d, &list, desc_node)
388 pdc_chain_complete(pd_chan, desc);
389}
390
391static void pdc_handle_error(struct pch_dma_chan *pd_chan)
392{
393 struct pch_dma_desc *bad_desc;
394
395 bad_desc = pdc_first_active(pd_chan);
396 list_del(&bad_desc->desc_node);
397
398 list_splice_init(&pd_chan->queue, pd_chan->active_list.prev);
399
400 if (!list_empty(&pd_chan->active_list))
401 pdc_dostart(pd_chan, pdc_first_active(pd_chan));
402
403 dev_crit(chan2dev(&pd_chan->chan), "Bad descriptor submitted\n");
404 dev_crit(chan2dev(&pd_chan->chan), "descriptor cookie: %d\n",
405 bad_desc->txd.cookie);
406
407 pdc_chain_complete(pd_chan, bad_desc);
408}
409
410static void pdc_advance_work(struct pch_dma_chan *pd_chan)
411{
412 if (list_empty(&pd_chan->active_list) ||
413 list_is_singular(&pd_chan->active_list)) {
414 pdc_complete_all(pd_chan);
415 } else {
416 pdc_chain_complete(pd_chan, pdc_first_active(pd_chan));
417 pdc_dostart(pd_chan, pdc_first_active(pd_chan));
418 }
419}
420
Yong Wang0c42bd02010-07-30 16:23:03 +0800421static dma_cookie_t pd_tx_submit(struct dma_async_tx_descriptor *txd)
422{
423 struct pch_dma_desc *desc = to_pd_desc(txd);
424 struct pch_dma_chan *pd_chan = to_pd_chan(txd->chan);
425 dma_cookie_t cookie;
426
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530427 spin_lock(&pd_chan->lock);
Russell King - ARM Linux884485e2012-03-06 22:34:46 +0000428 cookie = dma_cookie_assign(txd);
Yong Wang0c42bd02010-07-30 16:23:03 +0800429
430 if (list_empty(&pd_chan->active_list)) {
431 list_add_tail(&desc->desc_node, &pd_chan->active_list);
432 pdc_dostart(pd_chan, desc);
433 } else {
434 list_add_tail(&desc->desc_node, &pd_chan->queue);
435 }
436
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530437 spin_unlock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800438 return 0;
439}
440
441static struct pch_dma_desc *pdc_alloc_desc(struct dma_chan *chan, gfp_t flags)
442{
443 struct pch_dma_desc *desc = NULL;
444 struct pch_dma *pd = to_pd(chan->device);
445 dma_addr_t addr;
446
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530447 desc = pci_pool_alloc(pd->pool, flags, &addr);
Yong Wang0c42bd02010-07-30 16:23:03 +0800448 if (desc) {
449 memset(desc, 0, sizeof(struct pch_dma_desc));
450 INIT_LIST_HEAD(&desc->tx_list);
451 dma_async_tx_descriptor_init(&desc->txd, chan);
452 desc->txd.tx_submit = pd_tx_submit;
453 desc->txd.flags = DMA_CTRL_ACK;
454 desc->txd.phys = addr;
455 }
456
457 return desc;
458}
459
460static struct pch_dma_desc *pdc_desc_get(struct pch_dma_chan *pd_chan)
461{
462 struct pch_dma_desc *desc, *_d;
463 struct pch_dma_desc *ret = NULL;
Liu Yuan364de772011-04-02 14:20:47 +0800464 int i = 0;
Yong Wang0c42bd02010-07-30 16:23:03 +0800465
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530466 spin_lock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800467 list_for_each_entry_safe(desc, _d, &pd_chan->free_list, desc_node) {
468 i++;
469 if (async_tx_test_ack(&desc->txd)) {
470 list_del(&desc->desc_node);
471 ret = desc;
472 break;
473 }
474 dev_dbg(chan2dev(&pd_chan->chan), "desc %p not ACKed\n", desc);
475 }
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530476 spin_unlock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800477 dev_dbg(chan2dev(&pd_chan->chan), "scanned %d descriptors\n", i);
478
479 if (!ret) {
Tomoya MORINAGA5c1ef592013-02-12 11:25:33 +0900480 ret = pdc_alloc_desc(&pd_chan->chan, GFP_ATOMIC);
Yong Wang0c42bd02010-07-30 16:23:03 +0800481 if (ret) {
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530482 spin_lock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800483 pd_chan->descs_allocated++;
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530484 spin_unlock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800485 } else {
486 dev_err(chan2dev(&pd_chan->chan),
487 "failed to alloc desc\n");
488 }
489 }
490
491 return ret;
492}
493
494static void pdc_desc_put(struct pch_dma_chan *pd_chan,
495 struct pch_dma_desc *desc)
496{
497 if (desc) {
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530498 spin_lock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800499 list_splice_init(&desc->tx_list, &pd_chan->free_list);
500 list_add(&desc->desc_node, &pd_chan->free_list);
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530501 spin_unlock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800502 }
503}
504
505static int pd_alloc_chan_resources(struct dma_chan *chan)
506{
507 struct pch_dma_chan *pd_chan = to_pd_chan(chan);
508 struct pch_dma_desc *desc;
509 LIST_HEAD(tmp_list);
510 int i;
511
512 if (!pdc_is_idle(pd_chan)) {
513 dev_dbg(chan2dev(chan), "DMA channel not idle ?\n");
514 return -EIO;
515 }
516
517 if (!list_empty(&pd_chan->free_list))
518 return pd_chan->descs_allocated;
519
520 for (i = 0; i < init_nr_desc_per_channel; i++) {
521 desc = pdc_alloc_desc(chan, GFP_KERNEL);
522
523 if (!desc) {
524 dev_warn(chan2dev(chan),
525 "Only allocated %d initial descriptors\n", i);
526 break;
527 }
528
529 list_add_tail(&desc->desc_node, &tmp_list);
530 }
531
Alexander Stein70f18912011-06-22 17:05:33 +0200532 spin_lock_irq(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800533 list_splice(&tmp_list, &pd_chan->free_list);
534 pd_chan->descs_allocated = i;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +0000535 dma_cookie_init(chan);
Alexander Stein70f18912011-06-22 17:05:33 +0200536 spin_unlock_irq(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800537
538 pdc_enable_irq(chan, 1);
Yong Wang0c42bd02010-07-30 16:23:03 +0800539
540 return pd_chan->descs_allocated;
541}
542
543static void pd_free_chan_resources(struct dma_chan *chan)
544{
545 struct pch_dma_chan *pd_chan = to_pd_chan(chan);
546 struct pch_dma *pd = to_pd(chan->device);
547 struct pch_dma_desc *desc, *_d;
548 LIST_HEAD(tmp_list);
549
550 BUG_ON(!pdc_is_idle(pd_chan));
551 BUG_ON(!list_empty(&pd_chan->active_list));
552 BUG_ON(!list_empty(&pd_chan->queue));
553
Alexander Stein70f18912011-06-22 17:05:33 +0200554 spin_lock_irq(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800555 list_splice_init(&pd_chan->free_list, &tmp_list);
556 pd_chan->descs_allocated = 0;
Alexander Stein70f18912011-06-22 17:05:33 +0200557 spin_unlock_irq(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800558
559 list_for_each_entry_safe(desc, _d, &tmp_list, desc_node)
560 pci_pool_free(pd->pool, desc, desc->txd.phys);
561
562 pdc_enable_irq(chan, 0);
563}
564
565static enum dma_status pd_tx_status(struct dma_chan *chan, dma_cookie_t cookie,
566 struct dma_tx_state *txstate)
567{
Andy Shevchenkoda0a9082013-05-27 15:14:38 +0300568 return dma_cookie_status(chan, cookie, txstate);
Yong Wang0c42bd02010-07-30 16:23:03 +0800569}
570
571static void pd_issue_pending(struct dma_chan *chan)
572{
573 struct pch_dma_chan *pd_chan = to_pd_chan(chan);
574
575 if (pdc_is_idle(pd_chan)) {
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530576 spin_lock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800577 pdc_advance_work(pd_chan);
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530578 spin_unlock(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800579 }
580}
581
582static struct dma_async_tx_descriptor *pd_prep_slave_sg(struct dma_chan *chan,
583 struct scatterlist *sgl, unsigned int sg_len,
Alexandre Bounine185ecb52012-03-08 15:35:13 -0500584 enum dma_transfer_direction direction, unsigned long flags,
585 void *context)
Yong Wang0c42bd02010-07-30 16:23:03 +0800586{
587 struct pch_dma_chan *pd_chan = to_pd_chan(chan);
588 struct pch_dma_slave *pd_slave = chan->private;
589 struct pch_dma_desc *first = NULL;
590 struct pch_dma_desc *prev = NULL;
591 struct pch_dma_desc *desc = NULL;
592 struct scatterlist *sg;
593 dma_addr_t reg;
594 int i;
595
596 if (unlikely(!sg_len)) {
597 dev_info(chan2dev(chan), "prep_slave_sg: length is zero!\n");
598 return NULL;
599 }
600
Vinod Kouldb8196d2011-10-13 22:34:23 +0530601 if (direction == DMA_DEV_TO_MEM)
Yong Wang0c42bd02010-07-30 16:23:03 +0800602 reg = pd_slave->rx_reg;
Vinod Kouldb8196d2011-10-13 22:34:23 +0530603 else if (direction == DMA_MEM_TO_DEV)
Yong Wang0c42bd02010-07-30 16:23:03 +0800604 reg = pd_slave->tx_reg;
605 else
606 return NULL;
607
Tomoya MORINAGAc8fcba62011-05-09 16:09:35 +0900608 pd_chan->dir = direction;
609 pdc_set_dir(chan);
610
Yong Wang0c42bd02010-07-30 16:23:03 +0800611 for_each_sg(sgl, sg, sg_len, i) {
612 desc = pdc_desc_get(pd_chan);
613
614 if (!desc)
615 goto err_desc_get;
616
617 desc->regs.dev_addr = reg;
Lars-Peter Clausencbb796c2012-04-25 20:50:51 +0200618 desc->regs.mem_addr = sg_dma_address(sg);
Yong Wang0c42bd02010-07-30 16:23:03 +0800619 desc->regs.size = sg_dma_len(sg);
620 desc->regs.next = DMA_DESC_FOLLOW_WITHOUT_IRQ;
621
622 switch (pd_slave->width) {
623 case PCH_DMA_WIDTH_1_BYTE:
624 if (desc->regs.size > DMA_DESC_MAX_COUNT_1_BYTE)
625 goto err_desc_get;
626 desc->regs.size |= DMA_DESC_WIDTH_1_BYTE;
627 break;
628 case PCH_DMA_WIDTH_2_BYTES:
629 if (desc->regs.size > DMA_DESC_MAX_COUNT_2_BYTES)
630 goto err_desc_get;
631 desc->regs.size |= DMA_DESC_WIDTH_2_BYTES;
632 break;
633 case PCH_DMA_WIDTH_4_BYTES:
634 if (desc->regs.size > DMA_DESC_MAX_COUNT_4_BYTES)
635 goto err_desc_get;
636 desc->regs.size |= DMA_DESC_WIDTH_4_BYTES;
637 break;
638 default:
639 goto err_desc_get;
640 }
641
Yong Wang0c42bd02010-07-30 16:23:03 +0800642 if (!first) {
643 first = desc;
644 } else {
645 prev->regs.next |= desc->txd.phys;
646 list_add_tail(&desc->desc_node, &first->tx_list);
647 }
648
649 prev = desc;
650 }
651
652 if (flags & DMA_PREP_INTERRUPT)
653 desc->regs.next = DMA_DESC_END_WITH_IRQ;
654 else
655 desc->regs.next = DMA_DESC_END_WITHOUT_IRQ;
656
657 first->txd.cookie = -EBUSY;
658 desc->txd.flags = flags;
659
660 return &first->txd;
661
662err_desc_get:
663 dev_err(chan2dev(chan), "failed to get desc or wrong parameters\n");
664 pdc_desc_put(pd_chan, first);
665 return NULL;
666}
667
Maxime Ripardc91781b2014-11-17 14:42:40 +0100668static int pd_device_terminate_all(struct dma_chan *chan)
Yong Wang0c42bd02010-07-30 16:23:03 +0800669{
670 struct pch_dma_chan *pd_chan = to_pd_chan(chan);
671 struct pch_dma_desc *desc, *_d;
672 LIST_HEAD(list);
673
Alexander Stein70f18912011-06-22 17:05:33 +0200674 spin_lock_irq(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800675
676 pdc_set_mode(&pd_chan->chan, DMA_CTL0_DISABLE);
677
678 list_splice_init(&pd_chan->active_list, &list);
679 list_splice_init(&pd_chan->queue, &list);
680
681 list_for_each_entry_safe(desc, _d, &list, desc_node)
682 pdc_chain_complete(pd_chan, desc);
683
Alexander Stein70f18912011-06-22 17:05:33 +0200684 spin_unlock_irq(&pd_chan->lock);
Yong Wang0c42bd02010-07-30 16:23:03 +0800685
Yong Wang0c42bd02010-07-30 16:23:03 +0800686 return 0;
687}
688
689static void pdc_tasklet(unsigned long data)
690{
691 struct pch_dma_chan *pd_chan = (struct pch_dma_chan *)data;
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530692 unsigned long flags;
Yong Wang0c42bd02010-07-30 16:23:03 +0800693
694 if (!pdc_is_idle(pd_chan)) {
695 dev_err(chan2dev(&pd_chan->chan),
696 "BUG: handle non-idle channel in tasklet\n");
697 return;
698 }
699
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530700 spin_lock_irqsave(&pd_chan->lock, flags);
Yong Wang0c42bd02010-07-30 16:23:03 +0800701 if (test_and_clear_bit(0, &pd_chan->err_status))
702 pdc_handle_error(pd_chan);
703 else
704 pdc_advance_work(pd_chan);
Tomoya MORINAGAc5a9f9d2011-02-18 10:01:20 +0530705 spin_unlock_irqrestore(&pd_chan->lock, flags);
Yong Wang0c42bd02010-07-30 16:23:03 +0800706}
707
708static irqreturn_t pd_irq(int irq, void *devid)
709{
710 struct pch_dma *pd = (struct pch_dma *)devid;
711 struct pch_dma_chan *pd_chan;
712 u32 sts0;
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900713 u32 sts2;
Yong Wang0c42bd02010-07-30 16:23:03 +0800714 int i;
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900715 int ret0 = IRQ_NONE;
716 int ret2 = IRQ_NONE;
Yong Wang0c42bd02010-07-30 16:23:03 +0800717
718 sts0 = dma_readl(pd, STS0);
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900719 sts2 = dma_readl(pd, STS2);
Yong Wang0c42bd02010-07-30 16:23:03 +0800720
721 dev_dbg(pd->dma.dev, "pd_irq sts0: %x\n", sts0);
722
723 for (i = 0; i < pd->dma.chancnt; i++) {
724 pd_chan = &pd->channels[i];
725
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900726 if (i < 8) {
727 if (sts0 & DMA_STATUS_IRQ(i)) {
728 if (sts0 & DMA_STATUS0_ERR(i))
729 set_bit(0, &pd_chan->err_status);
Yong Wang0c42bd02010-07-30 16:23:03 +0800730
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900731 tasklet_schedule(&pd_chan->tasklet);
732 ret0 = IRQ_HANDLED;
733 }
734 } else {
735 if (sts2 & DMA_STATUS_IRQ(i - 8)) {
736 if (sts2 & DMA_STATUS2_ERR(i))
737 set_bit(0, &pd_chan->err_status);
738
739 tasklet_schedule(&pd_chan->tasklet);
740 ret2 = IRQ_HANDLED;
741 }
Yong Wang0c42bd02010-07-30 16:23:03 +0800742 }
Yong Wang0c42bd02010-07-30 16:23:03 +0800743 }
744
745 /* clear interrupt bits in status register */
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900746 if (ret0)
747 dma_writel(pd, STS0, sts0);
748 if (ret2)
749 dma_writel(pd, STS2, sts2);
Yong Wang0c42bd02010-07-30 16:23:03 +0800750
Tomoya MORINAGAc3d49132011-05-31 10:34:45 +0900751 return ret0 | ret2;
Yong Wang0c42bd02010-07-30 16:23:03 +0800752}
753
Rakib Mullick0b863b32011-03-06 17:26:10 +0600754#ifdef CONFIG_PM
Yong Wang0c42bd02010-07-30 16:23:03 +0800755static void pch_dma_save_regs(struct pch_dma *pd)
756{
757 struct pch_dma_chan *pd_chan;
758 struct dma_chan *chan, *_c;
759 int i = 0;
760
761 pd->regs.dma_ctl0 = dma_readl(pd, CTL0);
762 pd->regs.dma_ctl1 = dma_readl(pd, CTL1);
763 pd->regs.dma_ctl2 = dma_readl(pd, CTL2);
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900764 pd->regs.dma_ctl3 = dma_readl(pd, CTL3);
Yong Wang0c42bd02010-07-30 16:23:03 +0800765
766 list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
767 pd_chan = to_pd_chan(chan);
768
769 pd->ch_regs[i].dev_addr = channel_readl(pd_chan, DEV_ADDR);
770 pd->ch_regs[i].mem_addr = channel_readl(pd_chan, MEM_ADDR);
771 pd->ch_regs[i].size = channel_readl(pd_chan, SIZE);
772 pd->ch_regs[i].next = channel_readl(pd_chan, NEXT);
773
774 i++;
775 }
776}
777
778static void pch_dma_restore_regs(struct pch_dma *pd)
779{
780 struct pch_dma_chan *pd_chan;
781 struct dma_chan *chan, *_c;
782 int i = 0;
783
784 dma_writel(pd, CTL0, pd->regs.dma_ctl0);
785 dma_writel(pd, CTL1, pd->regs.dma_ctl1);
786 dma_writel(pd, CTL2, pd->regs.dma_ctl2);
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900787 dma_writel(pd, CTL3, pd->regs.dma_ctl3);
Yong Wang0c42bd02010-07-30 16:23:03 +0800788
789 list_for_each_entry_safe(chan, _c, &pd->dma.channels, device_node) {
790 pd_chan = to_pd_chan(chan);
791
792 channel_writel(pd_chan, DEV_ADDR, pd->ch_regs[i].dev_addr);
793 channel_writel(pd_chan, MEM_ADDR, pd->ch_regs[i].mem_addr);
794 channel_writel(pd_chan, SIZE, pd->ch_regs[i].size);
795 channel_writel(pd_chan, NEXT, pd->ch_regs[i].next);
796
797 i++;
798 }
799}
800
801static int pch_dma_suspend(struct pci_dev *pdev, pm_message_t state)
802{
803 struct pch_dma *pd = pci_get_drvdata(pdev);
804
805 if (pd)
806 pch_dma_save_regs(pd);
807
808 pci_save_state(pdev);
809 pci_disable_device(pdev);
810 pci_set_power_state(pdev, pci_choose_state(pdev, state));
811
812 return 0;
813}
814
815static int pch_dma_resume(struct pci_dev *pdev)
816{
817 struct pch_dma *pd = pci_get_drvdata(pdev);
818 int err;
819
820 pci_set_power_state(pdev, PCI_D0);
821 pci_restore_state(pdev);
822
823 err = pci_enable_device(pdev);
824 if (err) {
825 dev_dbg(&pdev->dev, "failed to enable device\n");
826 return err;
827 }
828
829 if (pd)
830 pch_dma_restore_regs(pd);
831
832 return 0;
833}
Rakib Mullick0b863b32011-03-06 17:26:10 +0600834#endif
Yong Wang0c42bd02010-07-30 16:23:03 +0800835
Bill Pemberton463a1f82012-11-19 13:22:55 -0500836static int pch_dma_probe(struct pci_dev *pdev,
Yong Wang0c42bd02010-07-30 16:23:03 +0800837 const struct pci_device_id *id)
838{
839 struct pch_dma *pd;
840 struct pch_dma_regs *regs;
841 unsigned int nr_channels;
842 int err;
843 int i;
844
845 nr_channels = id->driver_data;
Tomoya MORINAGA01631243d2011-10-12 09:38:35 +0900846 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
Yong Wang0c42bd02010-07-30 16:23:03 +0800847 if (!pd)
848 return -ENOMEM;
849
850 pci_set_drvdata(pdev, pd);
851
852 err = pci_enable_device(pdev);
853 if (err) {
854 dev_err(&pdev->dev, "Cannot enable PCI device\n");
855 goto err_free_mem;
856 }
857
858 if (!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
859 dev_err(&pdev->dev, "Cannot find proper base address\n");
Wei Yongjun27abb2f2013-07-17 08:34:59 +0800860 err = -ENODEV;
Yong Wang0c42bd02010-07-30 16:23:03 +0800861 goto err_disable_pdev;
862 }
863
864 err = pci_request_regions(pdev, DRV_NAME);
865 if (err) {
866 dev_err(&pdev->dev, "Cannot obtain PCI resources\n");
867 goto err_disable_pdev;
868 }
869
870 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
871 if (err) {
872 dev_err(&pdev->dev, "Cannot set proper DMA config\n");
873 goto err_free_res;
874 }
875
876 regs = pd->membase = pci_iomap(pdev, 1, 0);
877 if (!pd->membase) {
878 dev_err(&pdev->dev, "Cannot map MMIO registers\n");
879 err = -ENOMEM;
880 goto err_free_res;
881 }
882
883 pci_set_master(pdev);
884
885 err = request_irq(pdev->irq, pd_irq, IRQF_SHARED, DRV_NAME, pd);
886 if (err) {
887 dev_err(&pdev->dev, "Failed to request IRQ\n");
888 goto err_iounmap;
889 }
890
891 pd->pool = pci_pool_create("pch_dma_desc_pool", pdev,
892 sizeof(struct pch_dma_desc), 4, 0);
893 if (!pd->pool) {
894 dev_err(&pdev->dev, "Failed to alloc DMA descriptors\n");
895 err = -ENOMEM;
896 goto err_free_irq;
897 }
898
899 pd->dma.dev = &pdev->dev;
Yong Wang0c42bd02010-07-30 16:23:03 +0800900
901 INIT_LIST_HEAD(&pd->dma.channels);
902
903 for (i = 0; i < nr_channels; i++) {
904 struct pch_dma_chan *pd_chan = &pd->channels[i];
905
906 pd_chan->chan.device = &pd->dma;
Russell King - ARM Linuxd3ee98cdc2012-03-06 22:35:47 +0000907 dma_cookie_init(&pd_chan->chan);
Yong Wang0c42bd02010-07-30 16:23:03 +0800908
909 pd_chan->membase = &regs->desc[i];
910
Yong Wang0c42bd02010-07-30 16:23:03 +0800911 spin_lock_init(&pd_chan->lock);
912
913 INIT_LIST_HEAD(&pd_chan->active_list);
914 INIT_LIST_HEAD(&pd_chan->queue);
915 INIT_LIST_HEAD(&pd_chan->free_list);
916
917 tasklet_init(&pd_chan->tasklet, pdc_tasklet,
918 (unsigned long)pd_chan);
919 list_add_tail(&pd_chan->chan.device_node, &pd->dma.channels);
920 }
921
922 dma_cap_zero(pd->dma.cap_mask);
923 dma_cap_set(DMA_PRIVATE, pd->dma.cap_mask);
924 dma_cap_set(DMA_SLAVE, pd->dma.cap_mask);
925
926 pd->dma.device_alloc_chan_resources = pd_alloc_chan_resources;
927 pd->dma.device_free_chan_resources = pd_free_chan_resources;
928 pd->dma.device_tx_status = pd_tx_status;
929 pd->dma.device_issue_pending = pd_issue_pending;
930 pd->dma.device_prep_slave_sg = pd_prep_slave_sg;
Maxime Ripardc91781b2014-11-17 14:42:40 +0100931 pd->dma.device_terminate_all = pd_device_terminate_all;
Yong Wang0c42bd02010-07-30 16:23:03 +0800932
933 err = dma_async_device_register(&pd->dma);
934 if (err) {
935 dev_err(&pdev->dev, "Failed to register DMA device\n");
936 goto err_free_pool;
937 }
938
939 return 0;
940
941err_free_pool:
942 pci_pool_destroy(pd->pool);
943err_free_irq:
944 free_irq(pdev->irq, pd);
945err_iounmap:
946 pci_iounmap(pdev, pd->membase);
947err_free_res:
948 pci_release_regions(pdev);
949err_disable_pdev:
950 pci_disable_device(pdev);
951err_free_mem:
952 return err;
953}
954
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -0800955static void pch_dma_remove(struct pci_dev *pdev)
Yong Wang0c42bd02010-07-30 16:23:03 +0800956{
957 struct pch_dma *pd = pci_get_drvdata(pdev);
958 struct pch_dma_chan *pd_chan;
959 struct dma_chan *chan, *_c;
960
961 if (pd) {
962 dma_async_device_unregister(&pd->dma);
963
Vinod Koul9068b032014-03-06 12:24:08 +0530964 free_irq(pdev->irq, pd);
965
Yong Wang0c42bd02010-07-30 16:23:03 +0800966 list_for_each_entry_safe(chan, _c, &pd->dma.channels,
967 device_node) {
968 pd_chan = to_pd_chan(chan);
969
Yong Wang0c42bd02010-07-30 16:23:03 +0800970 tasklet_kill(&pd_chan->tasklet);
971 }
972
973 pci_pool_destroy(pd->pool);
Yong Wang0c42bd02010-07-30 16:23:03 +0800974 pci_iounmap(pdev, pd->membase);
975 pci_release_regions(pdev);
976 pci_disable_device(pdev);
977 kfree(pd);
978 }
979}
980
981/* PCI Device ID of DMA device */
Tomoya MORINAGA2cdf2452011-01-05 17:43:52 +0900982#define PCI_VENDOR_ID_ROHM 0x10DB
983#define PCI_DEVICE_ID_EG20T_PCH_DMA_8CH 0x8810
984#define PCI_DEVICE_ID_EG20T_PCH_DMA_4CH 0x8815
985#define PCI_DEVICE_ID_ML7213_DMA1_8CH 0x8026
986#define PCI_DEVICE_ID_ML7213_DMA2_8CH 0x802B
987#define PCI_DEVICE_ID_ML7213_DMA3_4CH 0x8034
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +0900988#define PCI_DEVICE_ID_ML7213_DMA4_12CH 0x8032
Tomoya MORINAGAc0dfc042011-05-09 16:09:39 +0900989#define PCI_DEVICE_ID_ML7223_DMA1_4CH 0x800B
990#define PCI_DEVICE_ID_ML7223_DMA2_4CH 0x800E
991#define PCI_DEVICE_ID_ML7223_DMA3_4CH 0x8017
992#define PCI_DEVICE_ID_ML7223_DMA4_4CH 0x803B
Tomoya MORINAGAca7fe2d2011-11-17 16:14:23 +0900993#define PCI_DEVICE_ID_ML7831_DMA1_8CH 0x8810
994#define PCI_DEVICE_ID_ML7831_DMA2_4CH 0x8815
Yong Wang0c42bd02010-07-30 16:23:03 +0800995
Michele Curti345e3122014-12-02 18:07:56 +0100996static const struct pci_device_id pch_dma_id_table[] = {
Tomoya MORINAGA2cdf2452011-01-05 17:43:52 +0900997 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_8CH), 8 },
998 { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_EG20T_PCH_DMA_4CH), 4 },
999 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA1_8CH), 8}, /* UART Video */
1000 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA2_8CH), 8}, /* PCMIF SPI */
1001 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA3_4CH), 4}, /* FPGA */
Tomoya MORINAGA194f5f22011-05-09 16:09:38 +09001002 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7213_DMA4_12CH), 12}, /* I2S */
Tomoya MORINAGAc0dfc042011-05-09 16:09:39 +09001003 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA1_4CH), 4}, /* UART */
1004 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA2_4CH), 4}, /* Video SPI */
1005 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA3_4CH), 4}, /* Security */
1006 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7223_DMA4_4CH), 4}, /* FPGA */
Tomoya MORINAGAca7fe2d2011-11-17 16:14:23 +09001007 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA1_8CH), 8}, /* UART */
1008 { PCI_VDEVICE(ROHM, PCI_DEVICE_ID_ML7831_DMA2_4CH), 4}, /* SPI */
Dzianis Kahanovich87acf5a2010-10-27 20:33:05 -06001009 { 0, },
Yong Wang0c42bd02010-07-30 16:23:03 +08001010};
1011
1012static struct pci_driver pch_dma_driver = {
1013 .name = DRV_NAME,
1014 .id_table = pch_dma_id_table,
1015 .probe = pch_dma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05001016 .remove = pch_dma_remove,
Yong Wang0c42bd02010-07-30 16:23:03 +08001017#ifdef CONFIG_PM
1018 .suspend = pch_dma_suspend,
1019 .resume = pch_dma_resume,
1020#endif
1021};
1022
Wei Yongjun53b99892012-10-10 21:04:58 +08001023module_pci_driver(pch_dma_driver);
Yong Wang0c42bd02010-07-30 16:23:03 +08001024
Tomoya MORINAGAca7fe2d2011-11-17 16:14:23 +09001025MODULE_DESCRIPTION("Intel EG20T PCH / LAPIS Semicon ML7213/ML7223/ML7831 IOH "
Tomoya MORINAGA2cdf2452011-01-05 17:43:52 +09001026 "DMA controller driver");
Yong Wang0c42bd02010-07-30 16:23:03 +08001027MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
1028MODULE_LICENSE("GPL v2");
Ben Hutchings58ddff22013-09-02 00:02:06 +01001029MODULE_DEVICE_TABLE(pci, pch_dma_id_table);