Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 // Copyright (C) 2007-2008 Atmel Corporation
0003 // Copyright (C) 2010-2011 ST Microelectronics
0004 // Copyright (C) 2013,2018 Intel Corporation
0005 
0006 #include <linux/bitops.h>
0007 #include <linux/dmaengine.h>
0008 #include <linux/errno.h>
0009 #include <linux/slab.h>
0010 #include <linux/types.h>
0011 
0012 #include "internal.h"
0013 
0014 static void dw_dma_initialize_chan(struct dw_dma_chan *dwc)
0015 {
0016     struct dw_dma *dw = to_dw_dma(dwc->chan.device);
0017     u32 cfghi = is_slave_direction(dwc->direction) ? 0 : DWC_CFGH_FIFO_MODE;
0018     u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority);
0019     bool hs_polarity = dwc->dws.hs_polarity;
0020 
0021     cfghi |= DWC_CFGH_DST_PER(dwc->dws.dst_id);
0022     cfghi |= DWC_CFGH_SRC_PER(dwc->dws.src_id);
0023     cfghi |= DWC_CFGH_PROTCTL(dw->pdata->protctl);
0024 
0025     /* Set polarity of handshake interface */
0026     cfglo |= hs_polarity ? DWC_CFGL_HS_DST_POL | DWC_CFGL_HS_SRC_POL : 0;
0027 
0028     channel_writel(dwc, CFG_LO, cfglo);
0029     channel_writel(dwc, CFG_HI, cfghi);
0030 }
0031 
0032 static void dw_dma_suspend_chan(struct dw_dma_chan *dwc, bool drain)
0033 {
0034     u32 cfglo = channel_readl(dwc, CFG_LO);
0035 
0036     channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP);
0037 }
0038 
0039 static void dw_dma_resume_chan(struct dw_dma_chan *dwc, bool drain)
0040 {
0041     u32 cfglo = channel_readl(dwc, CFG_LO);
0042 
0043     channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP);
0044 }
0045 
0046 static u32 dw_dma_bytes2block(struct dw_dma_chan *dwc,
0047                   size_t bytes, unsigned int width, size_t *len)
0048 {
0049     u32 block;
0050 
0051     if ((bytes >> width) > dwc->block_size) {
0052         block = dwc->block_size;
0053         *len = dwc->block_size << width;
0054     } else {
0055         block = bytes >> width;
0056         *len = bytes;
0057     }
0058 
0059     return block;
0060 }
0061 
0062 static size_t dw_dma_block2bytes(struct dw_dma_chan *dwc, u32 block, u32 width)
0063 {
0064     return DWC_CTLH_BLOCK_TS(block) << width;
0065 }
0066 
0067 static u32 dw_dma_prepare_ctllo(struct dw_dma_chan *dwc)
0068 {
0069     struct dma_slave_config *sconfig = &dwc->dma_sconfig;
0070     u8 smsize = (dwc->direction == DMA_DEV_TO_MEM) ? sconfig->src_maxburst : 0;
0071     u8 dmsize = (dwc->direction == DMA_MEM_TO_DEV) ? sconfig->dst_maxburst : 0;
0072     u8 p_master = dwc->dws.p_master;
0073     u8 m_master = dwc->dws.m_master;
0074     u8 dms = (dwc->direction == DMA_MEM_TO_DEV) ? p_master : m_master;
0075     u8 sms = (dwc->direction == DMA_DEV_TO_MEM) ? p_master : m_master;
0076 
0077     return DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN |
0078            DWC_CTLL_DST_MSIZE(dmsize) | DWC_CTLL_SRC_MSIZE(smsize) |
0079            DWC_CTLL_DMS(dms) | DWC_CTLL_SMS(sms);
0080 }
0081 
0082 static void dw_dma_encode_maxburst(struct dw_dma_chan *dwc, u32 *maxburst)
0083 {
0084     /*
0085      * Fix burst size according to dw_dmac. We need to convert them as:
0086      * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
0087      */
0088     *maxburst = *maxburst > 1 ? fls(*maxburst) - 2 : 0;
0089 }
0090 
0091 static void dw_dma_set_device_name(struct dw_dma *dw, int id)
0092 {
0093     snprintf(dw->name, sizeof(dw->name), "dw:dmac%d", id);
0094 }
0095 
0096 static void dw_dma_disable(struct dw_dma *dw)
0097 {
0098     do_dw_dma_off(dw);
0099 }
0100 
0101 static void dw_dma_enable(struct dw_dma *dw)
0102 {
0103     do_dw_dma_on(dw);
0104 }
0105 
0106 int dw_dma_probe(struct dw_dma_chip *chip)
0107 {
0108     struct dw_dma *dw;
0109 
0110     dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL);
0111     if (!dw)
0112         return -ENOMEM;
0113 
0114     /* Channel operations */
0115     dw->initialize_chan = dw_dma_initialize_chan;
0116     dw->suspend_chan = dw_dma_suspend_chan;
0117     dw->resume_chan = dw_dma_resume_chan;
0118     dw->prepare_ctllo = dw_dma_prepare_ctllo;
0119     dw->encode_maxburst = dw_dma_encode_maxburst;
0120     dw->bytes2block = dw_dma_bytes2block;
0121     dw->block2bytes = dw_dma_block2bytes;
0122 
0123     /* Device operations */
0124     dw->set_device_name = dw_dma_set_device_name;
0125     dw->disable = dw_dma_disable;
0126     dw->enable = dw_dma_enable;
0127 
0128     chip->dw = dw;
0129     return do_dma_probe(chip);
0130 }
0131 EXPORT_SYMBOL_GPL(dw_dma_probe);
0132 
0133 int dw_dma_remove(struct dw_dma_chip *chip)
0134 {
0135     return do_dma_remove(chip);
0136 }
0137 EXPORT_SYMBOL_GPL(dw_dma_remove);