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 5 6 #include <linux/bitops.h> 7 #include <linux/dmaengine.h> 8 #include <linux/errno.h> 9 #include <linux/slab.h> 10 #include <linux/types.h> 11 12 #include "../dmaengine.h" 13 #include "internal.h" 14 15 static void dw_dma_initialize_chan(struct dw_dma_chan *dwc) 16 { 17 struct dw_dma *dw = to_dw_dma(dwc->chan.device); 18 u32 cfghi = is_slave_direction(dwc->direction) ? 0 : DWC_CFGH_FIFO_MODE; 19 u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority); 20 bool hs_polarity = dwc->dws.hs_polarity; 21 22 cfghi |= DWC_CFGH_DST_PER(dwc->dws.dst_id); 23 cfghi |= DWC_CFGH_SRC_PER(dwc->dws.src_id); 24 cfghi |= DWC_CFGH_PROTCTL(dw->pdata->protctl); 25 26 /* Set polarity of handshake interface */ 27 cfglo |= hs_polarity ? DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL : 0; 28 29 channel_writel(dwc, CFG_LO, cfglo); 30 channel_writel(dwc, CFG_HI, cfghi); 31 } 32 33 static void dw_dma_suspend_chan(struct dw_dma_chan *dwc, bool drain) 34 { 35 u32 cfglo = channel_readl(dwc, CFG_LO); 36 37 channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP); 38 } 39 40 static void dw_dma_resume_chan(struct dw_dma_chan *dwc, bool drain) 41 { 42 u32 cfglo = channel_readl(dwc, CFG_LO); 43 44 channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP); 45 } 46 47 static u32 dw_dma_bytes2block(struct dw_dma_chan *dwc, 48 size_t bytes, unsigned int width, size_t *len) 49 { 50 u32 block; 51 52 if ((bytes >> width) > dwc->block_size) { 53 block = dwc->block_size; 54 *len = dwc->block_size << width; 55 } else { 56 block = bytes >> width; 57 *len = bytes; 58 } 59 60 return block; 61 } 62 63 static size_t dw_dma_block2bytes(struct dw_dma_chan *dwc, u32 block, u32 width) 64 { 65 return DWC_CTLH_BLOCK_TS(block) << width; 66 } 67 68 static inline u8 dw_dma_encode_maxburst(u32 maxburst) 69 { 70 /* 71 * Fix burst size according to dw_dmac. We need to convert them as: 72 * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3. 73 */ 74 return maxburst > 1 ? fls(maxburst) - 2 : 0; 75 } 76 77 static u32 dw_dma_prepare_ctllo(struct dw_dma_chan *dwc) 78 { 79 struct dma_slave_config *sconfig = &dwc->dma_sconfig; 80 u8 smsize = 0, dmsize = 0; 81 u8 sms, dms; 82 83 if (dwc->direction == DMA_MEM_TO_DEV) { 84 sms = dwc->dws.m_master; 85 dms = dwc->dws.p_master; 86 dmsize = dw_dma_encode_maxburst(sconfig->dst_maxburst); 87 } else if (dwc->direction == DMA_DEV_TO_MEM) { 88 sms = dwc->dws.p_master; 89 dms = dwc->dws.m_master; 90 smsize = dw_dma_encode_maxburst(sconfig->src_maxburst); 91 } else /* DMA_MEM_TO_MEM */ { 92 sms = dwc->dws.m_master; 93 dms = dwc->dws.m_master; 94 } 95 96 return DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN | 97 DWC_CTLL_DST_MSIZE(dmsize) | DWC_CTLL_SRC_MSIZE(smsize) | 98 DWC_CTLL_DMS(dms) | DWC_CTLL_SMS(sms); 99 } 100 101 static void dw_dma_set_device_name(struct dw_dma *dw, int id) 102 { 103 snprintf(dw->name, sizeof(dw->name), "dw:dmac%d", id); 104 } 105 106 static void dw_dma_disable(struct dw_dma *dw) 107 { 108 do_dw_dma_off(dw); 109 } 110 111 static void dw_dma_enable(struct dw_dma *dw) 112 { 113 do_dw_dma_on(dw); 114 } 115 116 int dw_dma_probe(struct dw_dma_chip *chip) 117 { 118 struct dw_dma *dw; 119 120 dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL); 121 if (!dw) 122 return -ENOMEM; 123 124 /* Channel operations */ 125 dw->initialize_chan = dw_dma_initialize_chan; 126 dw->suspend_chan = dw_dma_suspend_chan; 127 dw->resume_chan = dw_dma_resume_chan; 128 dw->prepare_ctllo = dw_dma_prepare_ctllo; 129 dw->bytes2block = dw_dma_bytes2block; 130 dw->block2bytes = dw_dma_block2bytes; 131 132 /* Device operations */ 133 dw->set_device_name = dw_dma_set_device_name; 134 dw->disable = dw_dma_disable; 135 dw->enable = dw_dma_enable; 136 137 chip->dw = dw; 138 return do_dma_probe(chip); 139 } 140 EXPORT_SYMBOL_GPL(dw_dma_probe); 141 142 int dw_dma_remove(struct dw_dma_chip *chip) 143 { 144 return do_dma_remove(chip); 145 } 146 EXPORT_SYMBOL_GPL(dw_dma_remove); 147