1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core driver for the High Speed UART DMA
5 * Copyright (C) 2015 Intel Corporation
6 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8 * Partially based on the bits found in drivers/tty/serial/mfd.c.
12 * DMA channel allocation:
13 * 1. Even number chans are used for DMA Read (UART TX), odd chans for DMA
15 * 2. 0/1 channel are assigned to port 0, 2/3 chan to port 1, 4/5 chan to
19 #include <linux/bits.h>
20 #include <linux/delay.h>
21 #include <linux/device.h>
22 #include <linux/dmaengine.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/percpu-defs.h>
29 #include <linux/scatterlist.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/spinlock.h>
36 #define HSU_DMA_BUSWIDTHS \
37 BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \
38 BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \
39 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \
40 BIT(DMA_SLAVE_BUSWIDTH_3_BYTES) | \
41 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | \
42 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES) | \
43 BIT(DMA_SLAVE_BUSWIDTH_16_BYTES)
45 static inline void hsu_chan_disable(struct hsu_dma_chan
*hsuc
)
47 hsu_chan_writel(hsuc
, HSU_CH_CR
, 0);
50 static inline void hsu_chan_enable(struct hsu_dma_chan
*hsuc
)
52 u32 cr
= HSU_CH_CR_CHA
;
54 if (hsuc
->direction
== DMA_MEM_TO_DEV
)
56 else if (hsuc
->direction
== DMA_DEV_TO_MEM
)
59 hsu_chan_writel(hsuc
, HSU_CH_CR
, cr
);
62 static void hsu_dma_chan_start(struct hsu_dma_chan
*hsuc
)
64 struct dma_slave_config
*config
= &hsuc
->config
;
65 struct hsu_dma_desc
*desc
= hsuc
->desc
;
66 u32 bsr
= 0, mtsr
= 0; /* to shut the compiler up */
67 u32 dcr
= HSU_CH_DCR_CHSOE
| HSU_CH_DCR_CHEI
;
68 unsigned int i
, count
;
70 if (hsuc
->direction
== DMA_MEM_TO_DEV
) {
71 bsr
= config
->dst_maxburst
;
72 mtsr
= config
->dst_addr_width
;
73 } else if (hsuc
->direction
== DMA_DEV_TO_MEM
) {
74 bsr
= config
->src_maxburst
;
75 mtsr
= config
->src_addr_width
;
78 hsu_chan_disable(hsuc
);
80 hsu_chan_writel(hsuc
, HSU_CH_DCR
, 0);
81 hsu_chan_writel(hsuc
, HSU_CH_BSR
, bsr
);
82 hsu_chan_writel(hsuc
, HSU_CH_MTSR
, mtsr
);
85 count
= desc
->nents
- desc
->active
;
86 for (i
= 0; i
< count
&& i
< HSU_DMA_CHAN_NR_DESC
; i
++) {
87 hsu_chan_writel(hsuc
, HSU_CH_DxSAR(i
), desc
->sg
[i
].addr
);
88 hsu_chan_writel(hsuc
, HSU_CH_DxTSR(i
), desc
->sg
[i
].len
);
90 /* Prepare value for DCR */
91 dcr
|= HSU_CH_DCR_DESCA(i
);
92 dcr
|= HSU_CH_DCR_CHTOI(i
); /* timeout bit, see HSU Errata 1 */
96 /* Only for the last descriptor in the chain */
97 dcr
|= HSU_CH_DCR_CHSOD(count
- 1);
98 dcr
|= HSU_CH_DCR_CHDI(count
- 1);
100 hsu_chan_writel(hsuc
, HSU_CH_DCR
, dcr
);
102 hsu_chan_enable(hsuc
);
105 static void hsu_dma_stop_channel(struct hsu_dma_chan
*hsuc
)
107 hsu_chan_disable(hsuc
);
108 hsu_chan_writel(hsuc
, HSU_CH_DCR
, 0);
111 static void hsu_dma_start_channel(struct hsu_dma_chan
*hsuc
)
113 hsu_dma_chan_start(hsuc
);
116 static void hsu_dma_start_transfer(struct hsu_dma_chan
*hsuc
)
118 struct virt_dma_desc
*vdesc
;
120 /* Get the next descriptor */
121 vdesc
= vchan_next_desc(&hsuc
->vchan
);
127 list_del(&vdesc
->node
);
128 hsuc
->desc
= to_hsu_dma_desc(vdesc
);
130 /* Start the channel with a new descriptor */
131 hsu_dma_start_channel(hsuc
);
135 * hsu_dma_get_status() - get DMA channel status
136 * @chip: HSUART DMA chip
137 * @nr: DMA channel number
138 * @status: pointer for DMA Channel Status Register value
141 * The function reads and clears the DMA Channel Status Register, checks
142 * if it was a timeout interrupt and returns a corresponding value.
144 * Caller should provide a valid pointer for the DMA Channel Status
145 * Register value that will be returned in @status.
148 * 1 for DMA timeout status, 0 for other DMA status, or error code for
149 * invalid parameters or no interrupt pending.
151 int hsu_dma_get_status(struct hsu_dma_chip
*chip
, unsigned short nr
,
154 struct hsu_dma_chan
*hsuc
;
159 if (nr
>= chip
->hsu
->nr_channels
)
162 hsuc
= &chip
->hsu
->chan
[nr
];
165 * No matter what situation, need read clear the IRQ status
166 * There is a bug, see Errata 5, HSD 2900918
168 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
169 sr
= hsu_chan_readl(hsuc
, HSU_CH_SR
);
170 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
172 /* Check if any interrupt is pending */
173 sr
&= ~(HSU_CH_SR_DESCE_ANY
| HSU_CH_SR_CDESC_ANY
);
177 /* Timeout IRQ, need wait some time, see Errata 2 */
178 if (sr
& HSU_CH_SR_DESCTO_ANY
)
182 * At this point, at least one of Descriptor Time Out, Channel Error
183 * or Descriptor Done bits must be set. Clear the Descriptor Time Out
184 * bits and if sr is still non-zero, it must be channel error or
185 * descriptor done which are higher priority than timeout and handled
186 * in hsu_dma_do_irq(). Else, it must be a timeout.
188 sr
&= ~HSU_CH_SR_DESCTO_ANY
;
194 EXPORT_SYMBOL_GPL(hsu_dma_get_status
);
197 * hsu_dma_do_irq() - DMA interrupt handler
198 * @chip: HSUART DMA chip
199 * @nr: DMA channel number
200 * @status: Channel Status Register value
203 * This function handles Channel Error and Descriptor Done interrupts.
204 * This function should be called after determining that the DMA interrupt
205 * is not a normal timeout interrupt, ie. hsu_dma_get_status() returned 0.
208 * 0 for invalid channel number, 1 otherwise.
210 int hsu_dma_do_irq(struct hsu_dma_chip
*chip
, unsigned short nr
, u32 status
)
212 struct dma_chan_percpu
*stat
;
213 struct hsu_dma_chan
*hsuc
;
214 struct hsu_dma_desc
*desc
;
218 if (nr
>= chip
->hsu
->nr_channels
)
221 hsuc
= &chip
->hsu
->chan
[nr
];
222 stat
= this_cpu_ptr(hsuc
->vchan
.chan
.local
);
224 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
227 if (status
& HSU_CH_SR_CHE
) {
228 desc
->status
= DMA_ERROR
;
229 } else if (desc
->active
< desc
->nents
) {
230 hsu_dma_start_channel(hsuc
);
232 vchan_cookie_complete(&desc
->vdesc
);
233 desc
->status
= DMA_COMPLETE
;
234 stat
->bytes_transferred
+= desc
->length
;
235 hsu_dma_start_transfer(hsuc
);
238 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
242 EXPORT_SYMBOL_GPL(hsu_dma_do_irq
);
244 static struct hsu_dma_desc
*hsu_dma_alloc_desc(unsigned int nents
)
246 struct hsu_dma_desc
*desc
;
248 desc
= kzalloc(sizeof(*desc
), GFP_NOWAIT
);
252 desc
->sg
= kcalloc(nents
, sizeof(*desc
->sg
), GFP_NOWAIT
);
261 static void hsu_dma_desc_free(struct virt_dma_desc
*vdesc
)
263 struct hsu_dma_desc
*desc
= to_hsu_dma_desc(vdesc
);
269 static struct dma_async_tx_descriptor
*hsu_dma_prep_slave_sg(
270 struct dma_chan
*chan
, struct scatterlist
*sgl
,
271 unsigned int sg_len
, enum dma_transfer_direction direction
,
272 unsigned long flags
, void *context
)
274 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
275 struct hsu_dma_desc
*desc
;
276 struct scatterlist
*sg
;
279 desc
= hsu_dma_alloc_desc(sg_len
);
283 for_each_sg(sgl
, sg
, sg_len
, i
) {
284 desc
->sg
[i
].addr
= sg_dma_address(sg
);
285 desc
->sg
[i
].len
= sg_dma_len(sg
);
287 desc
->length
+= sg_dma_len(sg
);
290 desc
->nents
= sg_len
;
291 desc
->direction
= direction
;
292 /* desc->active = 0 by kzalloc */
293 desc
->status
= DMA_IN_PROGRESS
;
295 return vchan_tx_prep(&hsuc
->vchan
, &desc
->vdesc
, flags
);
298 static void hsu_dma_issue_pending(struct dma_chan
*chan
)
300 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
303 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
304 if (vchan_issue_pending(&hsuc
->vchan
) && !hsuc
->desc
)
305 hsu_dma_start_transfer(hsuc
);
306 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
309 static size_t hsu_dma_active_desc_size(struct hsu_dma_chan
*hsuc
)
311 struct hsu_dma_desc
*desc
= hsuc
->desc
;
315 for (i
= desc
->active
; i
< desc
->nents
; i
++)
316 bytes
+= desc
->sg
[i
].len
;
318 i
= HSU_DMA_CHAN_NR_DESC
- 1;
320 bytes
+= hsu_chan_readl(hsuc
, HSU_CH_DxTSR(i
));
326 static enum dma_status
hsu_dma_tx_status(struct dma_chan
*chan
,
327 dma_cookie_t cookie
, struct dma_tx_state
*state
)
329 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
330 struct virt_dma_desc
*vdesc
;
331 enum dma_status status
;
335 status
= dma_cookie_status(chan
, cookie
, state
);
336 if (status
== DMA_COMPLETE
)
339 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
340 vdesc
= vchan_find_desc(&hsuc
->vchan
, cookie
);
341 if (hsuc
->desc
&& cookie
== hsuc
->desc
->vdesc
.tx
.cookie
) {
342 bytes
= hsu_dma_active_desc_size(hsuc
);
343 dma_set_residue(state
, bytes
);
344 status
= hsuc
->desc
->status
;
346 bytes
= to_hsu_dma_desc(vdesc
)->length
;
347 dma_set_residue(state
, bytes
);
349 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
354 static int hsu_dma_slave_config(struct dma_chan
*chan
,
355 struct dma_slave_config
*config
)
357 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
359 memcpy(&hsuc
->config
, config
, sizeof(hsuc
->config
));
364 static int hsu_dma_pause(struct dma_chan
*chan
)
366 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
369 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
370 if (hsuc
->desc
&& hsuc
->desc
->status
== DMA_IN_PROGRESS
) {
371 hsu_chan_disable(hsuc
);
372 hsuc
->desc
->status
= DMA_PAUSED
;
374 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
379 static int hsu_dma_resume(struct dma_chan
*chan
)
381 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
384 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
385 if (hsuc
->desc
&& hsuc
->desc
->status
== DMA_PAUSED
) {
386 hsuc
->desc
->status
= DMA_IN_PROGRESS
;
387 hsu_chan_enable(hsuc
);
389 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
394 static int hsu_dma_terminate_all(struct dma_chan
*chan
)
396 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
400 spin_lock_irqsave(&hsuc
->vchan
.lock
, flags
);
402 hsu_dma_stop_channel(hsuc
);
404 hsu_dma_desc_free(&hsuc
->desc
->vdesc
);
408 vchan_get_all_descriptors(&hsuc
->vchan
, &head
);
409 spin_unlock_irqrestore(&hsuc
->vchan
.lock
, flags
);
410 vchan_dma_desc_free_list(&hsuc
->vchan
, &head
);
415 static void hsu_dma_free_chan_resources(struct dma_chan
*chan
)
417 vchan_free_chan_resources(to_virt_chan(chan
));
420 static void hsu_dma_synchronize(struct dma_chan
*chan
)
422 struct hsu_dma_chan
*hsuc
= to_hsu_dma_chan(chan
);
424 vchan_synchronize(&hsuc
->vchan
);
427 int hsu_dma_probe(struct hsu_dma_chip
*chip
)
430 void __iomem
*addr
= chip
->regs
+ chip
->offset
;
434 hsu
= devm_kzalloc(chip
->dev
, sizeof(*hsu
), GFP_KERNEL
);
440 /* Calculate nr_channels from the IO space length */
441 hsu
->nr_channels
= (chip
->length
- chip
->offset
) / HSU_DMA_CHAN_LENGTH
;
443 hsu
->chan
= devm_kcalloc(chip
->dev
, hsu
->nr_channels
,
444 sizeof(*hsu
->chan
), GFP_KERNEL
);
448 INIT_LIST_HEAD(&hsu
->dma
.channels
);
449 for (i
= 0; i
< hsu
->nr_channels
; i
++) {
450 struct hsu_dma_chan
*hsuc
= &hsu
->chan
[i
];
452 hsuc
->vchan
.desc_free
= hsu_dma_desc_free
;
453 vchan_init(&hsuc
->vchan
, &hsu
->dma
);
455 hsuc
->direction
= (i
& 0x1) ? DMA_DEV_TO_MEM
: DMA_MEM_TO_DEV
;
456 hsuc
->reg
= addr
+ i
* HSU_DMA_CHAN_LENGTH
;
459 dma_cap_set(DMA_SLAVE
, hsu
->dma
.cap_mask
);
460 dma_cap_set(DMA_PRIVATE
, hsu
->dma
.cap_mask
);
462 hsu
->dma
.device_free_chan_resources
= hsu_dma_free_chan_resources
;
464 hsu
->dma
.device_prep_slave_sg
= hsu_dma_prep_slave_sg
;
466 hsu
->dma
.device_issue_pending
= hsu_dma_issue_pending
;
467 hsu
->dma
.device_tx_status
= hsu_dma_tx_status
;
469 hsu
->dma
.device_config
= hsu_dma_slave_config
;
470 hsu
->dma
.device_pause
= hsu_dma_pause
;
471 hsu
->dma
.device_resume
= hsu_dma_resume
;
472 hsu
->dma
.device_terminate_all
= hsu_dma_terminate_all
;
473 hsu
->dma
.device_synchronize
= hsu_dma_synchronize
;
475 hsu
->dma
.src_addr_widths
= HSU_DMA_BUSWIDTHS
;
476 hsu
->dma
.dst_addr_widths
= HSU_DMA_BUSWIDTHS
;
477 hsu
->dma
.directions
= BIT(DMA_DEV_TO_MEM
) | BIT(DMA_MEM_TO_DEV
);
478 hsu
->dma
.residue_granularity
= DMA_RESIDUE_GRANULARITY_BURST
;
480 hsu
->dma
.dev
= chip
->dev
;
482 dma_set_max_seg_size(hsu
->dma
.dev
, HSU_CH_DxTSR_MASK
);
484 ret
= dma_async_device_register(&hsu
->dma
);
488 dev_info(chip
->dev
, "Found HSU DMA, %d channels\n", hsu
->nr_channels
);
491 EXPORT_SYMBOL_GPL(hsu_dma_probe
);
493 int hsu_dma_remove(struct hsu_dma_chip
*chip
)
495 struct hsu_dma
*hsu
= chip
->hsu
;
498 dma_async_device_unregister(&hsu
->dma
);
500 for (i
= 0; i
< hsu
->nr_channels
; i
++) {
501 struct hsu_dma_chan
*hsuc
= &hsu
->chan
[i
];
503 tasklet_kill(&hsuc
->vchan
.task
);
508 EXPORT_SYMBOL_GPL(hsu_dma_remove
);
510 MODULE_LICENSE("GPL v2");
511 MODULE_DESCRIPTION("High Speed UART DMA core driver");
512 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");