2 * Core driver for the High Speed UART DMA
4 * Copyright (C) 2015 Intel Corporation
5 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
7 * Partially based on the bits found in drivers/tty/serial/mfd.c.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * DMA channel allocation:
16 * 1. Even number chans are used for DMA Read (UART TX), odd chans for DMA
18 * 2. 0/1 channel are assigned to port 0, 2/3 chan to port 1, 4/5 chan to
22 #include <linux/delay.h>
23 #include <linux/dmaengine.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
31 #define HSU_DMA_BUSWIDTHS \
32 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
33 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
34 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
35 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
36 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
37 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES) | \
38 BIT(DMA_SLAVE_BUSWIDTH_16_BYTES)
40 static inline void hsu_chan_disable(struct hsu_dma_chan
*hsuc
)
42 hsu_chan_writel(hsuc
, HSU_CH_CR
, 0);
45 static inline void hsu_chan_enable(struct hsu_dma_chan
*hsuc
)
47 u32 cr
= HSU_CH_CR_CHA
;
49 if (hsuc
->direction
== DMA_MEM_TO_DEV
)
51 else if (hsuc
->direction
== DMA_DEV_TO_MEM
)
54 hsu_chan_writel(hsuc
, HSU_CH_CR
, cr
);
57 static void hsu_dma_chan_start(struct hsu_dma_chan
*hsuc
)
59 struct dma_slave_config
*config
= &hsuc
->config
;
60 struct hsu_dma_desc
*desc
= hsuc
->desc
;
61 u32 bsr
= 0, mtsr
= 0; /* to shut the compiler up */
62 u32 dcr
= HSU_CH_DCR_CHSOE
| HSU_CH_DCR_CHEI
;
63 unsigned int i
, count
;
65 if (hsuc
->direction
== DMA_MEM_TO_DEV
) {
66 bsr
= config
->dst_maxburst
;
67 mtsr
= config
->src_addr_width
;
68 } else if (hsuc
->direction
== DMA_DEV_TO_MEM
) {
69 bsr
= config
->src_maxburst
;
70 mtsr
= config
->dst_addr_width
;
73 hsu_chan_disable(hsuc
);
75 hsu_chan_writel(hsuc
, HSU_CH_DCR
, 0);
76 hsu_chan_writel(hsuc
, HSU_CH_BSR
, bsr
);
77 hsu_chan_writel(hsuc
, HSU_CH_MTSR
, mtsr
);
80 count
= (desc
->nents
- desc
->active
) % HSU_DMA_CHAN_NR_DESC
;
81 for (i
= 0; i
< count
; i
++) {
82 hsu_chan_writel(hsuc
, HSU_CH_DxSAR(i
), desc
->sg
[i
].addr
);
83 hsu_chan_writel(hsuc
, HSU_CH_DxTSR(i
), desc
->sg
[i
].len
);
85 /* Prepare value for DCR */
86 dcr
|= HSU_CH_DCR_DESCA(i
);
87 dcr
|= HSU_CH_DCR_CHTOI(i
); /* timeout bit, see HSU Errata 1 */
91 /* Only for the last descriptor in the chain */
92 dcr
|= HSU_CH_DCR_CHSOD(count
- 1);
93 dcr
|= HSU_CH_DCR_CHDI(count
- 1);
95 hsu_chan_writel(hsuc
, HSU_CH_DCR
, dcr
);
97 hsu_chan_enable(hsuc
);
100 static void hsu_dma_stop_channel(struct hsu_dma_chan
*hsuc
)
102 hsu_chan_disable(hsuc
);
103 hsu_chan_writel(hsuc
, HSU_CH_DCR
, 0);
106 static void hsu_dma_start_channel(struct hsu_dma_chan
*hsuc
)
108 hsu_dma_chan_start(hsuc
);
111 static void hsu_dma_start_transfer(struct hsu_dma_chan
*hsuc
)
113 struct virt_dma_desc
*vdesc
;
115 /* Get the next descriptor */
116 vdesc
= vchan_next_desc(&hsuc
->vchan
);
122 list_del(&vdesc
->node
);
123 hsuc
->desc
= to_hsu_dma_desc(vdesc
);
125 /* Start the channel with a new descriptor */
126 hsu_dma_start_channel(hsuc
);
129 static u32
hsu_dma_chan_get_sr(struct hsu_dma_chan
*hsuc
)
134 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
135 sr
= hsu_chan_readl(hsuc
, HSU_CH_SR
);
136 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
138 return sr
& ~(HSU_CH_SR_DESCE_ANY
| HSU_CH_SR_CDESC_ANY
);
141 irqreturn_t
hsu_dma_irq(struct hsu_dma_chip
*chip
, unsigned short nr
)
143 struct hsu_dma_chan
*hsuc
;
144 struct hsu_dma_desc
*desc
;
149 if (nr
>= chip
->hsu
->nr_channels
)
152 hsuc
= &chip
->hsu
->chan
[nr
];
155 * No matter what situation, need read clear the IRQ status
156 * There is a bug, see Errata 5, HSD 2900918
158 sr
= hsu_dma_chan_get_sr(hsuc
);
162 /* Timeout IRQ, need wait some time, see Errata 2 */
163 if (hsuc
->direction
== DMA_DEV_TO_MEM
&& (sr
& HSU_CH_SR_DESCTO_ANY
))
166 sr
&= ~HSU_CH_SR_DESCTO_ANY
;
170 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
173 if (sr
& HSU_CH_SR_CHE
) {
174 desc
->status
= DMA_ERROR
;
175 } else if (desc
->active
< desc
->nents
) {
176 hsu_dma_start_channel(hsuc
);
178 vchan_cookie_complete(&desc
->vdesc
);
179 desc
->status
= DMA_COMPLETE
;
180 hsu_dma_start_transfer(hsuc
);
183 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
187 EXPORT_SYMBOL_GPL(hsu_dma_irq
);
189 static struct hsu_dma_desc
*hsu_dma_alloc_desc(unsigned int nents
)
191 struct hsu_dma_desc
*desc
;
193 desc
= kzalloc(sizeof(*desc
), GFP_NOWAIT
);
197 desc
->sg
= kcalloc(nents
, sizeof(*desc
->sg
), GFP_NOWAIT
);
206 static void hsu_dma_desc_free(struct virt_dma_desc
*vdesc
)
208 struct hsu_dma_desc
*desc
= to_hsu_dma_desc(vdesc
);
214 static struct dma_async_tx_descriptor
*hsu_dma_prep_slave_sg(
215 struct dma_chan
*chan
, struct scatterlist
*sgl
,
216 unsigned int sg_len
, enum dma_transfer_direction direction
,
217 unsigned long flags
, void *context
)
219 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
220 struct hsu_dma_desc
*desc
;
221 struct scatterlist
*sg
;
224 desc
= hsu_dma_alloc_desc(sg_len
);
228 for_each_sg(sgl
, sg
, sg_len
, i
) {
229 desc
->sg
[i
].addr
= sg_dma_address(sg
);
230 desc
->sg
[i
].len
= sg_dma_len(sg
);
232 desc
->length
+= sg_dma_len(sg
);
235 desc
->nents
= sg_len
;
236 desc
->direction
= direction
;
237 /* desc->active = 0 by kzalloc */
238 desc
->status
= DMA_IN_PROGRESS
;
240 return vchan_tx_prep(&hsuc
->vchan
, &desc
->vdesc
, flags
);
243 static void hsu_dma_issue_pending(struct dma_chan
*chan
)
245 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
248 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
249 if (vchan_issue_pending(&hsuc
->vchan
) && !hsuc
->desc
)
250 hsu_dma_start_transfer(hsuc
);
251 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
254 static size_t hsu_dma_active_desc_size(struct hsu_dma_chan
*hsuc
)
256 struct hsu_dma_desc
*desc
= hsuc
->desc
;
260 for (i
= desc
->active
; i
< desc
->nents
; i
++)
261 bytes
+= desc
->sg
[i
].len
;
263 i
= HSU_DMA_CHAN_NR_DESC
- 1;
265 bytes
+= hsu_chan_readl(hsuc
, HSU_CH_DxTSR(i
));
271 static enum dma_status
hsu_dma_tx_status(struct dma_chan
*chan
,
272 dma_cookie_t cookie
, struct dma_tx_state
*state
)
274 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
275 struct virt_dma_desc
*vdesc
;
276 enum dma_status status
;
280 status
= dma_cookie_status(chan
, cookie
, state
);
281 if (status
== DMA_COMPLETE
)
284 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
285 vdesc
= vchan_find_desc(&hsuc
->vchan
, cookie
);
286 if (hsuc
->desc
&& cookie
== hsuc
->desc
->vdesc
.tx
.cookie
) {
287 bytes
= hsu_dma_active_desc_size(hsuc
);
288 dma_set_residue(state
, bytes
);
289 status
= hsuc
->desc
->status
;
291 bytes
= to_hsu_dma_desc(vdesc
)->length
;
292 dma_set_residue(state
, bytes
);
294 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
299 static int hsu_dma_slave_config(struct dma_chan
*chan
,
300 struct dma_slave_config
*config
)
302 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
304 /* Check if chan will be configured for slave transfers */
305 if (!is_slave_direction(config
->direction
))
308 memcpy(&hsuc
->config
, config
, sizeof(hsuc
->config
));
313 static int hsu_dma_pause(struct dma_chan
*chan
)
315 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
318 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
319 if (hsuc
->desc
&& hsuc
->desc
->status
== DMA_IN_PROGRESS
) {
320 hsu_chan_disable(hsuc
);
321 hsuc
->desc
->status
= DMA_PAUSED
;
323 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
328 static int hsu_dma_resume(struct dma_chan
*chan
)
330 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
333 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
334 if (hsuc
->desc
&& hsuc
->desc
->status
== DMA_PAUSED
) {
335 hsuc
->desc
->status
= DMA_IN_PROGRESS
;
336 hsu_chan_enable(hsuc
);
338 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
343 static int hsu_dma_terminate_all(struct dma_chan
*chan
)
345 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
349 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
351 hsu_dma_stop_channel(hsuc
);
353 hsu_dma_desc_free(&hsuc
->desc
->vdesc
);
357 vchan_get_all_descriptors(&hsuc
->vchan
, &head
);
358 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
359 vchan_dma_desc_free_list(&hsuc
->vchan
, &head
);
364 static void hsu_dma_free_chan_resources(struct dma_chan
*chan
)
366 vchan_free_chan_resources(to_virt_chan(chan
));
369 int hsu_dma_probe(struct hsu_dma_chip
*chip
)
372 void __iomem
*addr
= chip
->regs
+ chip
->offset
;
376 hsu
= devm_kzalloc(chip
->dev
, sizeof(*hsu
), GFP_KERNEL
);
382 /* Calculate nr_channels from the IO space length */
383 hsu
->nr_channels
= (chip
->length
- chip
->offset
) / HSU_DMA_CHAN_LENGTH
;
385 hsu
->chan
= devm_kcalloc(chip
->dev
, hsu
->nr_channels
,
386 sizeof(*hsu
->chan
), GFP_KERNEL
);
390 INIT_LIST_HEAD(&hsu
->dma
.channels
);
391 for (i
= 0; i
< hsu
->nr_channels
; i
++) {
392 struct hsu_dma_chan
*hsuc
= &hsu
->chan
[i
];
394 hsuc
->vchan
.desc_free
= hsu_dma_desc_free
;
395 vchan_init(&hsuc
->vchan
, &hsu
->dma
);
397 hsuc
->direction
= (i
& 0x1) ? DMA_DEV_TO_MEM
: DMA_MEM_TO_DEV
;
398 hsuc
->reg
= addr
+ i
* HSU_DMA_CHAN_LENGTH
;
401 dma_cap_set(DMA_SLAVE
, hsu
->dma
.cap_mask
);
402 dma_cap_set(DMA_PRIVATE
, hsu
->dma
.cap_mask
);
404 hsu
->dma
.device_free_chan_resources
= hsu_dma_free_chan_resources
;
406 hsu
->dma
.device_prep_slave_sg
= hsu_dma_prep_slave_sg
;
408 hsu
->dma
.device_issue_pending
= hsu_dma_issue_pending
;
409 hsu
->dma
.device_tx_status
= hsu_dma_tx_status
;
411 hsu
->dma
.device_config
= hsu_dma_slave_config
;
412 hsu
->dma
.device_pause
= hsu_dma_pause
;
413 hsu
->dma
.device_resume
= hsu_dma_resume
;
414 hsu
->dma
.device_terminate_all
= hsu_dma_terminate_all
;
416 hsu
->dma
.src_addr_widths
= HSU_DMA_BUSWIDTHS
;
417 hsu
->dma
.dst_addr_widths
= HSU_DMA_BUSWIDTHS
;
418 hsu
->dma
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
419 hsu
->dma
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
421 hsu
->dma
.dev
= chip
->dev
;
423 ret
= dma_async_device_register(&hsu
->dma
);
427 dev_info(chip
->dev
, "Found HSU DMA, %d channels\n", hsu
->nr_channels
);
430 EXPORT_SYMBOL_GPL(hsu_dma_probe
);
432 int hsu_dma_remove(struct hsu_dma_chip
*chip
)
434 struct hsu_dma
*hsu
= chip
->hsu
;
437 dma_async_device_unregister(&hsu
->dma
);
439 for (i
= 0; i
< hsu
->nr_channels
; i
++) {
440 struct hsu_dma_chan
*hsuc
= &hsu
->chan
[i
];
442 tasklet_kill(&hsuc
->vchan
.task
);
447 EXPORT_SYMBOL_GPL(hsu_dma_remove
);
449 MODULE_LICENSE("GPL v2");
450 MODULE_DESCRIPTION("High Speed UART DMA core driver");
451 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");