1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2007-2008 Atmel Corporation
3 // Copyright (C) 2010-2011 ST Microelectronics
4 // Copyright (C) 2013,2018 Intel Corporation
6 #include <linux/bitops.h>
7 #include <linux/errno.h>
8 #include <linux/slab.h>
9 #include <linux/types.h>
13 static void dw_dma_initialize_chan(struct dw_dma_chan *dwc)
15 struct dw_dma *dw = to_dw_dma(dwc->chan.device);
16 u32 cfghi = DWC_CFGH_FIFO_MODE;
17 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
18 bool hs_polarity = dwc->dws.hs_polarity;
20 cfghi |= DWC_CFGH_DST_PER(dwc->dws.dst_id);
21 cfghi |= DWC_CFGH_SRC_PER(dwc->dws.src_id);
22 cfghi |= DWC_CFGH_PROTCTL(dw->pdata->protctl);
24 /* Set polarity of handshake interface */
25 cfglo |= hs_polarity ? DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL : 0;
27 channel_writel(dwc, CFG_LO, cfglo);
28 channel_writel(dwc, CFG_HI, cfghi);
31 static void dw_dma_suspend_chan(struct dw_dma_chan *dwc, bool drain)
33 u32 cfglo = channel_readl(dwc, CFG_LO);
35 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
38 static u32 dw_dma_bytes2block(struct dw_dma_chan *dwc,
39 size_t bytes, unsigned int width, size_t *len)
43 if ((bytes >> width) > dwc->block_size) {
44 block = dwc->block_size;
45 *len = dwc->block_size << width;
47 block = bytes >> width;
54 static size_t dw_dma_block2bytes(struct dw_dma_chan *dwc, u32 block, u32 width)
56 return DWC_CTLH_BLOCK_TS(block) << width;
59 static void dw_dma_encode_maxburst(struct dw_dma_chan *dwc, u32 *maxburst)
62 * Fix burst size according to dw_dmac. We need to convert them as:
63 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
65 *maxburst = *maxburst > 1 ? fls(*maxburst) - 2 : 0;
68 static void dw_dma_set_device_name(struct dw_dma *dw, int id)
70 snprintf(dw->name, sizeof(dw->name), "dw:dmac%d", id);
73 static void dw_dma_disable(struct dw_dma *dw)
78 static void dw_dma_enable(struct dw_dma *dw)
83 int dw_dma_probe(struct dw_dma_chip *chip)
87 dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
91 /* Channel operations */
92 dw->initialize_chan = dw_dma_initialize_chan;
93 dw->suspend_chan = dw_dma_suspend_chan;
94 dw->encode_maxburst = dw_dma_encode_maxburst;
95 dw->bytes2block = dw_dma_bytes2block;
96 dw->block2bytes = dw_dma_block2bytes;
98 /* Device operations */
99 dw->set_device_name = dw_dma_set_device_name;
100 dw->disable = dw_dma_disable;
101 dw->enable = dw_dma_enable;
104 return do_dma_probe(chip);
106 EXPORT_SYMBOL_GPL(dw_dma_probe);
108 int dw_dma_remove(struct dw_dma_chip *chip)
110 return do_dma_remove(chip);
112 EXPORT_SYMBOL_GPL(dw_dma_remove);