12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646 |
- // SPDX-License-Identifier: GPL-2.0
- // (C) 2017-2018 Synopsys, Inc. (www.synopsys.com)
- /*
- * Synopsys DesignWare AXI DMA Controller driver.
- *
- * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
- */
- #include <linux/bitops.h>
- #include <linux/delay.h>
- #include <linux/device.h>
- #include <linux/dmaengine.h>
- #include <linux/dmapool.h>
- #include <linux/dma-mapping.h>
- #include <linux/err.h>
- #include <linux/interrupt.h>
- #include <linux/io.h>
- #include <linux/iopoll.h>
- #include <linux/io-64-nonatomic-lo-hi.h>
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/of.h>
- #include <linux/of_dma.h>
- #include <linux/platform_device.h>
- #include <linux/pm_runtime.h>
- #include <linux/property.h>
- #include <linux/slab.h>
- #include <linux/types.h>
- #include "ark-axi-dma.h"
- #include "dmaengine.h"
- #include "virt-dma.h"
- /*
- * The set of bus widths supported by the DMA controller. DW AXI DMAC supports
- * master data bus width up to 512 bits (for both AXI master interfaces), but
- * it depends on IP block configurarion.
- */
- #define AXI_DMA_BUSWIDTHS \
- (DMA_SLAVE_BUSWIDTH_1_BYTE | \
- DMA_SLAVE_BUSWIDTH_2_BYTES | \
- DMA_SLAVE_BUSWIDTH_4_BYTES | \
- DMA_SLAVE_BUSWIDTH_8_BYTES | \
- DMA_SLAVE_BUSWIDTH_16_BYTES | \
- DMA_SLAVE_BUSWIDTH_32_BYTES | \
- DMA_SLAVE_BUSWIDTH_64_BYTES)
- static inline void
- axi_dma_iowrite32(struct axi_dma_chip *chip, u32 reg, u32 val)
- {
- iowrite32(val, chip->regs + reg);
- }
- static inline u32 axi_dma_ioread32(struct axi_dma_chip *chip, u32 reg)
- {
- return ioread32(chip->regs + reg);
- }
- static inline void
- axi_chan_iowrite32(struct axi_dma_chan *chan, u32 reg, u32 val)
- {
- iowrite32(val, chan->chan_regs + reg);
- }
- static inline u32 axi_chan_ioread32(struct axi_dma_chan *chan, u32 reg)
- {
- return ioread32(chan->chan_regs + reg);
- }
- static inline void
- axi_chan_iowrite64(struct axi_dma_chan *chan, u32 reg, u64 val)
- {
- /*
- * We split one 64 bit write for two 32 bit write as some HW doesn't
- * support 64 bit access.
- */
- iowrite32(lower_32_bits(val), chan->chan_regs + reg);
- iowrite32(upper_32_bits(val), chan->chan_regs + reg + 4);
- }
- static inline void axi_dma_disable(struct axi_dma_chip *chip)
- {
- u32 val;
- val = axi_dma_ioread32(chip, DMAC_CFG);
- val &= ~DMAC_EN_MASK;
- axi_dma_iowrite32(chip, DMAC_CFG, val);
- }
- static inline void axi_dma_enable(struct axi_dma_chip *chip)
- {
- u32 val;
- val = axi_dma_ioread32(chip, DMAC_CFG);
- val |= DMAC_EN_MASK;
- axi_dma_iowrite32(chip, DMAC_CFG, val);
- }
- static inline void axi_dma_irq_disable(struct axi_dma_chip *chip)
- {
- u32 val;
- val = axi_dma_ioread32(chip, DMAC_CFG);
- val &= ~INT_EN_MASK;
- axi_dma_iowrite32(chip, DMAC_CFG, val);
- }
- static inline void axi_dma_irq_enable(struct axi_dma_chip *chip)
- {
- u32 val;
- val = axi_dma_ioread32(chip, DMAC_CFG);
- val |= INT_EN_MASK;
- axi_dma_iowrite32(chip, DMAC_CFG, val);
- }
- static inline void axi_chan_irq_disable(struct axi_dma_chan *chan, u32 irq_mask)
- {
- u32 val;
- if (likely(irq_mask == DWAXIDMAC_IRQ_ALL)) {
- axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, DWAXIDMAC_IRQ_NONE);
- } else {
- val = axi_chan_ioread32(chan, CH_INTSTATUS_ENA);
- val &= ~irq_mask;
- axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, val);
- }
- }
- static inline void axi_chan_irq_set(struct axi_dma_chan *chan, u32 irq_mask)
- {
- axi_chan_iowrite32(chan, CH_INTSTATUS_ENA, irq_mask);
- }
- static inline void axi_chan_irq_sig_set(struct axi_dma_chan *chan, u32 irq_mask)
- {
- axi_chan_iowrite32(chan, CH_INTSIGNAL_ENA, irq_mask);
- }
- static inline void axi_chan_irq_clear(struct axi_dma_chan *chan, u32 irq_mask)
- {
- axi_chan_iowrite32(chan, CH_INTCLEAR, irq_mask);
- }
- static inline u32 axi_chan_irq_read(struct axi_dma_chan *chan)
- {
- return axi_chan_ioread32(chan, CH_INTSTATUS);
- }
- static inline void axi_chan_disable(struct axi_dma_chan *chan)
- {
- u32 val;
- val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
- val &= ~(BIT(chan->id) << DMAC_CHAN_EN_SHIFT);
- val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
- axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
- }
- static inline void axi_chan_enable(struct axi_dma_chan *chan)
- {
- u32 val;
- val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
- val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT |
- BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
- axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
- }
- static inline bool axi_chan_is_hw_enable(struct axi_dma_chan *chan)
- {
- u32 val;
- val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
- return !!(val & (BIT(chan->id) << DMAC_CHAN_EN_SHIFT));
- }
- static inline void axi_chan_abort(struct axi_dma_chan *chan)
- {
- u32 val;
- val = axi_dma_ioread32(chan->chip, DMAC_CHEN_H);
- val |= BIT(chan->id) << DMAC_CHAN_EN_SHIFT;
- val |= BIT(chan->id) << DMAC_CHAN_EN_WE_SHIFT;
- axi_dma_iowrite32(chan->chip, DMAC_CHEN_H, val);
- }
- static void axi_dma_hw_init(struct axi_dma_chip *chip)
- {
- u32 i;
- for (i = 0; i < chip->dw->hdata->nr_channels; i++) {
- axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
- axi_chan_disable(&chip->dw->chan[i]);
- }
- }
- static u32 axi_chan_get_xfer_width(struct axi_dma_chan *chan, dma_addr_t src,
- dma_addr_t dst, size_t len)
- {
- u32 max_width = chan->chip->dw->hdata->m_data_width;
- return __ffs(src | dst | len | BIT(max_width));
- }
- static inline const char *axi_chan_name(struct axi_dma_chan *chan)
- {
- return dma_chan_name(&chan->vc.chan);
- }
- static struct axi_dma_desc *axi_desc_alloc(u32 num)
- {
- struct axi_dma_desc *desc;
- desc = kzalloc(sizeof(*desc), GFP_NOWAIT);
- if (!desc)
- return NULL;
- desc->hw_desc = kcalloc(num, sizeof(*desc->hw_desc), GFP_NOWAIT);
- if (!desc->hw_desc) {
- kfree(desc);
- return NULL;
- }
- return desc;
- }
- static struct axi_dma_lli *axi_desc_get(struct axi_dma_chan *chan,
- dma_addr_t *addr)
- {
- struct axi_dma_lli *lli;
- dma_addr_t phys;
- lli = dma_pool_zalloc(chan->desc_pool, GFP_NOWAIT, &phys);
- if (unlikely(!lli)) {
- dev_err(chan2dev(chan), "%s: not enough descriptors available\n",
- axi_chan_name(chan));
- return NULL;
- }
- atomic_inc(&chan->descs_allocated);
- *addr = phys;
- return lli;
- }
- static void axi_desc_put(struct axi_dma_desc *desc)
- {
- struct axi_dma_chan *chan = desc->chan;
- int count = atomic_read(&chan->descs_allocated);
- struct axi_dma_hw_desc *hw_desc;
- int descs_put;
- for (descs_put = 0; descs_put < count; descs_put++) {
- hw_desc = &desc->hw_desc[descs_put];
- dma_pool_free(chan->desc_pool, hw_desc->lli, hw_desc->llp);
- }
- kfree(desc->hw_desc);
- kfree(desc);
- atomic_sub(descs_put, &chan->descs_allocated);
- dev_vdbg(chan2dev(chan), "%s: %d descs put, %d still allocated\n",
- axi_chan_name(chan), descs_put,
- atomic_read(&chan->descs_allocated));
- }
- static void vchan_desc_put(struct virt_dma_desc *vdesc)
- {
- axi_desc_put(vd_to_axi_desc(vdesc));
- }
- /* Returns how many bytes were already received from source */
- static inline u32 dma_chan_get_sent(struct axi_dma_chan *chan)
- {
- u32 block_ts = axi_chan_ioread32(chan, CH_STATUS);
- u32 ctllo = axi_chan_ioread32(chan, CH_CTL_L);
- return (block_ts & DMAX_MAX_BLK_MASK) * (1 << (ctllo >> CH_CTL_L_SRC_WIDTH_POS & 7));
- }
- static enum dma_status
- dma_chan_tx_status(struct dma_chan *dchan, dma_cookie_t cookie,
- struct dma_tx_state *txstate)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- struct virt_dma_desc *vdesc;
- enum dma_status status;
- u32 completed_length;
- unsigned long flags;
- u32 completed_blocks;
- size_t bytes = 0;
- u32 length;
- u32 len;
- status = dma_cookie_status(dchan, cookie, txstate);
- if (status == DMA_COMPLETE || !txstate)
- return status;
- spin_lock_irqsave(&chan->vc.lock, flags);
- vdesc = vchan_find_desc(&chan->vc, cookie);
- if (vdesc) {
- length = vd_to_axi_desc(vdesc)->length;
- len = vd_to_axi_desc(vdesc)->hw_desc[0].len;
- if (length == len) {
- bytes = length - dma_chan_get_sent(chan);
- } else {
- if (chan->cyclic) {
- int count = atomic_read(&chan->descs_allocated);
- struct axi_dma_desc *desc = vd_to_axi_desc(vdesc);
- u32 sentf, sentr;
- u64 llp;
- int i;
- sentf = dma_chan_get_sent(chan);
- llp = lo_hi_readq(chan->chan_regs + CH_LLP);
- sentr = dma_chan_get_sent(chan);
- if (sentr < sentf)
- llp = lo_hi_readq(chan->chan_regs + CH_LLP);
- bytes = length;
- for (i = 0; i < count; i++) {
- if (llp == DWC_LLP_LOC(desc->hw_desc[i].lli->llp)) {
- bytes -= sentr;
- break;
- } else {
- bytes -= desc->hw_desc[i].len;
- }
- }
- } else {
- completed_blocks = vd_to_axi_desc(vdesc)->completed_blocks;
- completed_length = completed_blocks * len;
- bytes = length - completed_length;
- }
- }
- } else {
- bytes = 0;//vd_to_axi_desc(vdesc)->length;
- }
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- dma_set_residue(txstate, bytes);
- if (chan->is_paused && status == DMA_IN_PROGRESS)
- status = DMA_PAUSED;
- return status;
- }
- static void write_desc_llp(struct axi_dma_hw_desc *desc, dma_addr_t adr)
- {
- desc->lli->llp = cpu_to_le64(adr);
- }
- static void write_chan_llp(struct axi_dma_chan *chan, dma_addr_t adr)
- {
- axi_chan_iowrite64(chan, CH_LLP, adr);
- }
- /* Called in chan locked context */
- static void axi_chan_block_xfer_start(struct axi_dma_chan *chan,
- struct axi_dma_desc *first)
- {
- u32 priority = chan->chip->dw->hdata->priority[chan->id];
- u32 reg, irq_mask;
- u8 lms = 0; /* Select AXI0 master for LLI fetching */
- if (unlikely(axi_chan_is_hw_enable(chan))) {
- dev_err(chan2dev(chan), "%s is non-idle!\n",
- axi_chan_name(chan));
- return;
- }
- axi_dma_enable(chan->chip);
- axi_dma_irq_enable(chan->chip);
- reg = (DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_DST_MULTBLK_TYPE_POS |
- DWAXIDMAC_MBLK_TYPE_LL << CH_CFG_L_SRC_MULTBLK_TYPE_POS);
- if (chan->direction == DMA_MEM_TO_DEV)
- reg |= (chan->hw_handshake_num << CH_CFG_L_DST_PER_POS);
- else if (chan->direction == DMA_DEV_TO_MEM)
- reg |= (chan->hw_handshake_num << CH_CFG_L_SRC_PER_POS);
- axi_chan_iowrite32(chan, CH_CFG_L, reg);
- reg = (DWAXIDMAC_TT_FC_MEM_TO_MEM_DMAC << CH_CFG_H_TT_FC_POS |
- priority << CH_CFG_H_PRIORITY_POS |
- DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_DST_POS |
- DWAXIDMAC_HS_SEL_HW << CH_CFG_H_HS_SEL_SRC_POS);
- switch (chan->direction) {
- case DMA_MEM_TO_DEV:
- reg |= (chan->config.device_fc ?
- DWAXIDMAC_TT_FC_MEM_TO_PER_DST :
- DWAXIDMAC_TT_FC_MEM_TO_PER_DMAC)
- << CH_CFG_H_TT_FC_POS;
- break;
- case DMA_DEV_TO_MEM:
- reg |= (chan->config.device_fc ?
- DWAXIDMAC_TT_FC_PER_TO_MEM_SRC :
- DWAXIDMAC_TT_FC_PER_TO_MEM_DMAC)
- << CH_CFG_H_TT_FC_POS;
- break;
- default:
- break;
- }
- axi_chan_iowrite32(chan, CH_CFG_H, reg);
- write_chan_llp(chan, first->hw_desc[0].llp | lms);
- irq_mask = DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_ALL_ERR;
- if (chan->cyclic)
- irq_mask |= DWAXIDMAC_IRQ_BLOCK_TRF;
- axi_chan_irq_sig_set(chan, irq_mask);
- /* Generate 'suspend' status but don't generate interrupt */
- irq_mask |= DWAXIDMAC_IRQ_SUSPENDED;
- axi_chan_irq_set(chan, irq_mask);
- axi_chan_enable(chan);
- }
- static void axi_chan_start_first_queued(struct axi_dma_chan *chan)
- {
- struct axi_dma_desc *desc;
- struct virt_dma_desc *vd;
- vd = vchan_next_desc(&chan->vc);
- if (!vd)
- return;
- desc = vd_to_axi_desc(vd);
- dev_vdbg(chan2dev(chan), "%s: started %u\n", axi_chan_name(chan),
- vd->tx.cookie);
- axi_chan_block_xfer_start(chan, desc);
- }
- static void dma_chan_issue_pending(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- unsigned long flags;
- spin_lock_irqsave(&chan->vc.lock, flags);
- if (vchan_issue_pending(&chan->vc))
- axi_chan_start_first_queued(chan);
- #ifdef DMA_GUARD_TIMER
- if (chan->cyclic) {
- ktime_t kt = ktime_set(0, DMA_GUARD_TIMER_PERIOD);
- hrtimer_start(&chan->hrt, kt, HRTIMER_MODE_REL);
- }
- #endif
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- }
- static void dw_axi_dma_synchronize(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- vchan_synchronize(&chan->vc);
- }
- #ifdef DMA_GUARD_TIMER
- static enum hrtimer_restart dma_chan_hrtimer_callback(struct hrtimer *hrt)
- {
- struct axi_dma_chan *chan = container_of(hrt, struct axi_dma_chan, hrt);
- printk(KERN_ALERT "dma_chan_hrtimer_callback.\n");
- if (axi_chan_is_hw_enable(chan)) {
- axi_chan_abort(chan);
- udelay(100);
- if (!axi_chan_is_hw_enable(chan)) {
- struct virt_dma_desc *vd = vchan_next_desc(&chan->vc);
- int count = atomic_read(&chan->descs_allocated);
- struct axi_dma_desc *desc;
- int i;
- if (vd) {
- desc = vd_to_axi_desc(vd);
- if (desc) {
- for (i = 0; i < count; i++)
- desc->hw_desc[i].lli->ctl_hi |= CH_CTL_H_LLI_VALID;
- /* Try to restart the controller */
- axi_chan_start_first_queued(chan);
- }
- }
- }
- }
- hrtimer_forward_now(hrt, ns_to_ktime(DMA_GUARD_TIMER_PERIOD));
- return HRTIMER_RESTART;
- }
- #endif
- static int dma_chan_alloc_chan_resources(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- /* ASSERT: channel is idle */
- if (axi_chan_is_hw_enable(chan)) {
- dev_err(chan2dev(chan), "%s is non-idle!\n",
- axi_chan_name(chan));
- return -EBUSY;
- }
- #ifdef DMA_GUARD_TIMER
- /* initialize rt timer */
- hrtimer_init(&chan->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
- chan->hrt.function = dma_chan_hrtimer_callback;
- #endif
- /* LLI address must be aligned to a 64-byte boundary */
- chan->desc_pool = dma_pool_create(dev_name(chan2dev(chan)),
- chan->chip->dev,
- sizeof(struct axi_dma_lli),
- 64, 0);
- if (!chan->desc_pool) {
- dev_err(chan2dev(chan), "No memory for descriptors\n");
- return -ENOMEM;
- }
- dev_vdbg(dchan2dev(dchan), "%s: allocating\n", axi_chan_name(chan));
- pm_runtime_get(chan->chip->dev);
- return 0;
- }
- static void dma_chan_free_chan_resources(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- /* ASSERT: channel is idle */
- if (axi_chan_is_hw_enable(chan))
- dev_err(dchan2dev(dchan), "%s is non-idle!\n",
- axi_chan_name(chan));
- #ifdef DMA_GUARD_TIMER
- if (chan->cyclic)
- hrtimer_cancel(&chan->hrt);
- #endif
- axi_chan_disable(chan);
- axi_chan_irq_disable(chan, DWAXIDMAC_IRQ_ALL);
- vchan_free_chan_resources(&chan->vc);
- dma_pool_destroy(chan->desc_pool);
- chan->desc_pool = NULL;
- dev_vdbg(dchan2dev(dchan),
- "%s: free resources, descriptor still allocated: %u\n",
- axi_chan_name(chan), atomic_read(&chan->descs_allocated));
- pm_runtime_put(chan->chip->dev);
- }
- /*
- * If DW_axi_dmac sees CHx_CTL.ShadowReg_Or_LLI_Last bit of the fetched LLI
- * as 1, it understands that the current block is the final block in the
- * transfer and completes the DMA transfer operation at the end of current
- * block transfer.
- */
- static void set_desc_last(struct axi_dma_hw_desc *desc)
- {
- u32 val;
- val = le32_to_cpu(desc->lli->ctl_hi);
- val |= CH_CTL_H_LLI_LAST;
- desc->lli->ctl_hi = cpu_to_le32(val);
- }
- static void write_desc_sar(struct axi_dma_hw_desc *desc, dma_addr_t adr)
- {
- desc->lli->sar = cpu_to_le64(adr);
- }
- static void write_desc_dar(struct axi_dma_hw_desc *desc, dma_addr_t adr)
- {
- desc->lli->dar = cpu_to_le64(adr);
- }
- static void set_desc_src_master(struct axi_dma_hw_desc *desc)
- {
- u32 val;
- /* Select AXI0 for source master */
- val = le32_to_cpu(desc->lli->ctl_lo);
- val &= ~CH_CTL_L_SRC_MAST;
- desc->lli->ctl_lo = cpu_to_le32(val);
- }
- static void set_desc_dest_master(struct axi_dma_hw_desc *hw_desc,
- struct axi_dma_desc *desc)
- {
- u32 val;
- /* Select AXI1 for source master if available */
- val = le32_to_cpu(hw_desc->lli->ctl_lo);
- if (desc->chan->chip->dw->hdata->nr_masters > 1)
- val |= CH_CTL_L_DST_MAST;
- else
- val &= ~CH_CTL_L_DST_MAST;
- hw_desc->lli->ctl_lo = cpu_to_le32(val);
- }
- static int dw_axi_dma_set_hw_desc(struct axi_dma_chan *chan,
- struct axi_dma_hw_desc *hw_desc,
- dma_addr_t mem_addr, size_t len)
- {
- unsigned int data_width = BIT(chan->chip->dw->hdata->m_data_width);
- unsigned int reg_width;
- unsigned int mem_width;
- dma_addr_t device_addr;
- size_t axi_block_ts;
- size_t block_ts;
- u32 ctllo, ctlhi;
- u32 burst_len;
- u8 dmsize, smsize;
- u8 dms, sms;
- axi_block_ts = chan->chip->dw->hdata->block_size[chan->id];
- mem_width = __ffs(data_width | mem_addr | len);
- if (mem_width > DWAXIDMAC_TRANS_WIDTH_32)
- mem_width = DWAXIDMAC_TRANS_WIDTH_32;
- if (!IS_ALIGNED(mem_addr, 4)) {
- dev_err(chan->chip->dev, "invalid buffer alignment\n");
- return -EINVAL;
- }
- switch (chan->direction) {
- case DMA_MEM_TO_DEV:
- reg_width = __ffs(chan->config.dst_addr_width);
- device_addr = chan->config.dst_addr;
- ctllo = reg_width << CH_CTL_L_DST_WIDTH_POS |
- mem_width << CH_CTL_L_SRC_WIDTH_POS |
- DWAXIDMAC_CH_CTL_L_NOINC << CH_CTL_L_DST_INC_POS |
- DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS;
- block_ts = len >> mem_width;
- break;
- case DMA_DEV_TO_MEM:
- reg_width = __ffs(chan->config.src_addr_width);
- device_addr = chan->config.src_addr;
- ctllo = reg_width << CH_CTL_L_SRC_WIDTH_POS |
- mem_width << CH_CTL_L_DST_WIDTH_POS |
- DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
- DWAXIDMAC_CH_CTL_L_NOINC << CH_CTL_L_SRC_INC_POS;
- block_ts = len >> reg_width;
- break;
- default:
- return -EINVAL;
- }
- if (block_ts > axi_block_ts)
- return -EINVAL;
- hw_desc->lli = axi_desc_get(chan, &hw_desc->llp);
- if (unlikely(!hw_desc->lli))
- return -ENOMEM;
- ctlhi = CH_CTL_H_LLI_VALID;
- if (chan->cyclic)
- ctlhi |= CH_CTL_H_IOC_BLKTFR_EN;
- if (chan->chip->dw->hdata->restrict_axi_burst_len) {
- burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
- ctlhi |= CH_CTL_H_ARLEN_EN | CH_CTL_H_AWLEN_EN |
- burst_len << CH_CTL_H_ARLEN_POS |
- burst_len << CH_CTL_H_AWLEN_POS;
- }
- hw_desc->lli->ctl_hi = cpu_to_le32(ctlhi);
- if (chan->direction == DMA_MEM_TO_DEV) {
- write_desc_sar(hw_desc, mem_addr);
- write_desc_dar(hw_desc, device_addr);
- } else {
- write_desc_sar(hw_desc, device_addr);
- write_desc_dar(hw_desc, mem_addr);
- }
- hw_desc->lli->block_ts_lo = cpu_to_le32(block_ts - 1);
- dmsize = is_slave_direction(chan->direction) ?
- chan->config.dst_maxburst : DWAXIDMAC_BURST_TRANS_LEN_4;
- smsize = is_slave_direction(chan->direction) ?
- chan->config.src_maxburst : DWAXIDMAC_BURST_TRANS_LEN_4;
- ctllo |= dmsize << CH_CTL_L_DST_MSIZE_POS | smsize << CH_CTL_L_SRC_MSIZE_POS;
- dms = chan->direction == DMA_MEM_TO_DEV ? chan->p_master : chan->m_master;
- sms = chan->direction == DMA_DEV_TO_MEM ? chan->p_master : chan->m_master;
- ctllo |= DWC_CTLL_DMS(dms) | DWC_CTLL_SMS(sms);
- hw_desc->lli->ctl_lo = cpu_to_le32(ctllo);
- set_desc_src_master(hw_desc);
- hw_desc->len = len;
- return 0;
- }
- static size_t calculate_block_len(struct axi_dma_chan *chan,
- dma_addr_t dma_addr, size_t buf_len,
- enum dma_transfer_direction direction)
- {
- u32 data_width, reg_width, mem_width;
- size_t axi_block_ts, block_len;
- axi_block_ts = chan->chip->dw->hdata->block_size[chan->id];
- switch (direction) {
- case DMA_MEM_TO_DEV:
- data_width = BIT(chan->chip->dw->hdata->m_data_width);
- mem_width = __ffs(data_width | dma_addr | buf_len);
- if (mem_width > DWAXIDMAC_TRANS_WIDTH_32)
- mem_width = DWAXIDMAC_TRANS_WIDTH_32;
- block_len = axi_block_ts << mem_width;
- break;
- case DMA_DEV_TO_MEM:
- reg_width = __ffs(chan->config.src_addr_width);
- block_len = axi_block_ts << reg_width;
- break;
- default:
- block_len = 0;
- }
- return block_len;
- }
- static struct dma_async_tx_descriptor *
- dw_axi_dma_chan_prep_cyclic(struct dma_chan *dchan, dma_addr_t dma_addr,
- size_t buf_len, size_t period_len,
- enum dma_transfer_direction direction,
- unsigned long flags)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- struct axi_dma_hw_desc *hw_desc = NULL;
- struct axi_dma_desc *desc = NULL;
- dma_addr_t src_addr = dma_addr;
- u32 num_periods, num_segments;
- size_t axi_block_len;
- u32 total_segments;
- u32 segment_len;
- unsigned int i;
- int status;
- u64 llp = 0;
- u8 lms = DWC_LLP_LMS(chan->m_master);
- num_periods = buf_len / period_len;
- axi_block_len = calculate_block_len(chan, dma_addr, buf_len, direction);
- if (axi_block_len == 0)
- return NULL;
- num_segments = DIV_ROUND_UP(period_len, axi_block_len);
- segment_len = DIV_ROUND_UP(period_len, num_segments);
- total_segments = num_periods * num_segments;
- desc = axi_desc_alloc(total_segments);
- if (unlikely(!desc))
- goto err_desc_get;
- chan->direction = direction;
- desc->chan = chan;
- chan->cyclic = true;
- desc->length = 0;
- desc->period_len = period_len;
- for (i = 0; i < total_segments; i++) {
- hw_desc = &desc->hw_desc[i];
- status = dw_axi_dma_set_hw_desc(chan, hw_desc, src_addr,
- segment_len);
- if (status < 0)
- goto err_desc_get;
- desc->length += hw_desc->len;
- /* Set end-of-link to the linked descriptor, so that cyclic
- * callback function can be triggered during interrupt.
- */
- //set_desc_last(hw_desc);
- src_addr += segment_len;
- }
- llp = desc->hw_desc[0].llp;
- /* Managed transfer list */
- do {
- hw_desc = &desc->hw_desc[--total_segments];
- write_desc_llp(hw_desc, llp | lms);
- llp = hw_desc->llp;
- } while (total_segments);
- return vchan_tx_prep(&chan->vc, &desc->vd, flags);
- err_desc_get:
- if (desc)
- axi_desc_put(desc);
- return NULL;
- }
- static struct dma_async_tx_descriptor *
- dw_axi_dma_chan_prep_slave_sg(struct dma_chan *dchan, struct scatterlist *sgl,
- unsigned int sg_len,
- enum dma_transfer_direction direction,
- unsigned long flags, void *context)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- struct axi_dma_hw_desc *hw_desc = NULL;
- struct axi_dma_desc *desc = NULL;
- u32 num_segments, segment_len;
- unsigned int loop = 0;
- struct scatterlist *sg;
- size_t axi_block_len;
- u32 len, num_sgs = 0;
- unsigned int i;
- dma_addr_t mem;
- int status;
- u64 llp = 0;
- u8 lms = DWC_LLP_LMS(chan->m_master);
- if (unlikely(!is_slave_direction(direction) || !sg_len))
- return NULL;
- mem = sg_dma_address(sgl);
- len = sg_dma_len(sgl);
- axi_block_len = calculate_block_len(chan, mem, len, direction);
- if (axi_block_len == 0)
- return NULL;
- for_each_sg(sgl, sg, sg_len, i)
- num_sgs += DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
- desc = axi_desc_alloc(num_sgs);
- if (unlikely(!desc))
- goto err_desc_get;
- desc->chan = chan;
- desc->length = 0;
- chan->direction = direction;
- for_each_sg(sgl, sg, sg_len, i) {
- mem = sg_dma_address(sg);
- len = sg_dma_len(sg);
- num_segments = DIV_ROUND_UP(sg_dma_len(sg), axi_block_len);
- segment_len = DIV_ROUND_UP(sg_dma_len(sg), num_segments);
- do {
- hw_desc = &desc->hw_desc[loop++];
- status = dw_axi_dma_set_hw_desc(chan, hw_desc, mem, segment_len);
- if (status < 0)
- goto err_desc_get;
- desc->length += hw_desc->len;
- len -= segment_len;
- mem += segment_len;
- } while (len >= segment_len);
- }
- /* Set end-of-link to the last link descriptor of list */
- set_desc_last(&desc->hw_desc[num_sgs - 1]);
- /* Managed transfer list */
- do {
- hw_desc = &desc->hw_desc[--num_sgs];
- write_desc_llp(hw_desc, llp | lms);
- llp = hw_desc->llp;
- } while (num_sgs);
- return vchan_tx_prep(&chan->vc, &desc->vd, flags);
- err_desc_get:
- if (desc)
- axi_desc_put(desc);
- return NULL;
- }
- static struct dma_async_tx_descriptor *
- dma_chan_prep_dma_memcpy(struct dma_chan *dchan, dma_addr_t dst_adr,
- dma_addr_t src_adr, size_t len, unsigned long flags)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- size_t block_ts, max_block_ts, xfer_len;
- struct axi_dma_hw_desc *hw_desc = NULL;
- struct axi_dma_desc *desc = NULL;
- u32 xfer_width, reg, num;
- u64 llp = 0;
- u8 lms = 0; /* Select AXI0 master for LLI fetching */
- dev_dbg(chan2dev(chan), "%s: memcpy: src: %pad dst: %pad length: %zd flags: %#lx",
- axi_chan_name(chan), &src_adr, &dst_adr, len, flags);
- max_block_ts = chan->chip->dw->hdata->block_size[chan->id];
- xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, len);
- num = DIV_ROUND_UP(len, max_block_ts << xfer_width);
- desc = axi_desc_alloc(num);
- if (unlikely(!desc))
- goto err_desc_get;
- desc->chan = chan;
- num = 0;
- desc->length = 0;
- while (len) {
- xfer_len = len;
- hw_desc = &desc->hw_desc[num];
- /*
- * Take care for the alignment.
- * Actually source and destination widths can be different, but
- * make them same to be simpler.
- */
- xfer_width = axi_chan_get_xfer_width(chan, src_adr, dst_adr, xfer_len);
- /*
- * block_ts indicates the total number of data of width
- * to be transferred in a DMA block transfer.
- * BLOCK_TS register should be set to block_ts - 1
- */
- block_ts = xfer_len >> xfer_width;
- if (block_ts > max_block_ts) {
- block_ts = max_block_ts;
- xfer_len = max_block_ts << xfer_width;
- }
- hw_desc->lli = axi_desc_get(chan, &hw_desc->llp);
- if (unlikely(!hw_desc->lli))
- goto err_desc_get;
- write_desc_sar(hw_desc, src_adr);
- write_desc_dar(hw_desc, dst_adr);
- hw_desc->lli->block_ts_lo = cpu_to_le32(block_ts - 1);
- reg = CH_CTL_H_LLI_VALID;
- if (chan->chip->dw->hdata->restrict_axi_burst_len) {
- u32 burst_len = chan->chip->dw->hdata->axi_rw_burst_len;
- reg |= (CH_CTL_H_ARLEN_EN |
- burst_len << CH_CTL_H_ARLEN_POS |
- CH_CTL_H_AWLEN_EN |
- burst_len << CH_CTL_H_AWLEN_POS);
- }
- hw_desc->lli->ctl_hi = cpu_to_le32(reg);
- reg = (DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_DST_MSIZE_POS |
- DWAXIDMAC_BURST_TRANS_LEN_4 << CH_CTL_L_SRC_MSIZE_POS |
- xfer_width << CH_CTL_L_DST_WIDTH_POS |
- xfer_width << CH_CTL_L_SRC_WIDTH_POS |
- DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_DST_INC_POS |
- DWAXIDMAC_CH_CTL_L_INC << CH_CTL_L_SRC_INC_POS);
- hw_desc->lli->ctl_lo = cpu_to_le32(reg);
- set_desc_src_master(hw_desc);
- set_desc_dest_master(hw_desc, desc);
- hw_desc->len = xfer_len;
- desc->length += hw_desc->len;
- /* update the length and addresses for the next loop cycle */
- len -= xfer_len;
- dst_adr += xfer_len;
- src_adr += xfer_len;
- num++;
- }
- /* Set end-of-link to the last link descriptor of list */
- set_desc_last(&desc->hw_desc[num - 1]);
- /* Managed transfer list */
- do {
- hw_desc = &desc->hw_desc[--num];
- write_desc_llp(hw_desc, llp | lms);
- llp = hw_desc->llp;
- } while (num);
- return vchan_tx_prep(&chan->vc, &desc->vd, flags);
- err_desc_get:
- if (desc)
- axi_desc_put(desc);
- return NULL;
- }
- /*
- * Fix sconfig's burst size according to dw_dmac. We need to convert them as:
- * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3.
- *
- * NOTE: burst size 2 is not supported by controller.
- *
- * This can be done by finding least significant bit set: n & (n - 1)
- */
- static inline void convert_burst(u32 *maxburst)
- {
- if (*maxburst > 1)
- *maxburst = fls(*maxburst) - 2;
- else
- *maxburst = 0;
- }
- static int dw_axi_dma_chan_slave_config(struct dma_chan *dchan,
- struct dma_slave_config *config)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- memcpy(&chan->config, config, sizeof(*config));
- if (chan->config.src_maxburst == 0)
- chan->config.src_maxburst = chan->config.dst_maxburst;
- if (chan->config.dst_maxburst == 0)
- chan->config.dst_maxburst = chan->config.src_maxburst;
- convert_burst(&chan->config.src_maxburst);
- convert_burst(&chan->config.dst_maxburst);
- return 0;
- }
- static void axi_chan_dump_lli(struct axi_dma_chan *chan,
- struct axi_dma_hw_desc *desc)
- {
- dev_err(dchan2dev(&chan->vc.chan),
- "SAR: 0x%llx DAR: 0x%llx LLP: 0x%llx BTS 0x%x CTL: 0x%x:%08x",
- le64_to_cpu(desc->lli->sar),
- le64_to_cpu(desc->lli->dar),
- le64_to_cpu(desc->lli->llp),
- le32_to_cpu(desc->lli->block_ts_lo),
- le32_to_cpu(desc->lli->ctl_hi),
- le32_to_cpu(desc->lli->ctl_lo));
- }
- static void axi_chan_list_dump_lli(struct axi_dma_chan *chan,
- struct axi_dma_desc *desc_head)
- {
- int count = atomic_read(&chan->descs_allocated);
- int i;
- for (i = 0; i < count; i++)
- axi_chan_dump_lli(chan, &desc_head->hw_desc[i]);
- }
- static noinline void axi_chan_handle_err(struct axi_dma_chan *chan, u32 status)
- {
- struct virt_dma_desc *vd;
- unsigned long flags;
- printk(KERN_ALERT "axi_chan_handle_err status=0x%x.\n", status);
- spin_lock_irqsave(&chan->vc.lock, flags);
- #ifdef DMA_GUARD_TIMER
- if (chan->cyclic)
- hrtimer_cancel(&chan->hrt);
- #endif
- axi_chan_disable(chan);
- /* The bad descriptor currently is in the head of vc list */
- vd = vchan_next_desc(&chan->vc);
- if (!vd) {
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- return;
- }
- if (!chan->cyclic) {
- /* Remove the completed descriptor from issued list */
- list_del(&vd->node);
- vchan_cookie_complete(vd);
- }
- /* WARN about bad descriptor */
- dev_err(chan2dev(chan),
- "Bad descriptor submitted for %s, cookie: %d, irq: 0x%08x\n",
- axi_chan_name(chan), vd->tx.cookie, status);
- axi_chan_list_dump_lli(chan, vd_to_axi_desc(vd));
- if (chan->cyclic && (status & DWAXIDMAC_IRQ_INVALID_ERR)) {
- int count = atomic_read(&chan->descs_allocated);
- struct axi_dma_desc *desc;
- int i;
- desc = vd_to_axi_desc(vd);
- if (desc) {
- for (i = 0; i < count; i++)
- desc->hw_desc[i].lli->ctl_hi |= CH_CTL_H_LLI_VALID;
- }
- axi_chan_enable(chan);
- } else {
- /* Try to restart the controller */
- axi_chan_start_first_queued(chan);
- }
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- }
- static void axi_chan_block_xfer_complete(struct axi_dma_chan *chan)
- {
- int count = atomic_read(&chan->descs_allocated);
- struct axi_dma_hw_desc *hw_desc;
- struct axi_dma_desc *desc;
- struct virt_dma_desc *vd;
- unsigned long flags;
- u64 llp;
- int i;
- spin_lock_irqsave(&chan->vc.lock, flags);
- /* The completed descriptor currently is in the head of vc list */
- vd = vchan_next_desc(&chan->vc);
- if (!vd) {
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- return;
- }
- if (chan->cyclic) {
- #ifdef DMA_GUARD_TIMER
- ktime_t kt = ktime_set(0, DMA_GUARD_TIMER_PERIOD);
- hrtimer_start(&chan->hrt, kt, HRTIMER_MODE_REL);
- #endif
- desc = vd_to_axi_desc(vd);
- if (desc) {
- llp = lo_hi_readq(chan->chan_regs + CH_LLP);
- for (i = 0; i < count; i++) {
- hw_desc = &desc->hw_desc[i];
- hw_desc->lli->ctl_hi |= CH_CTL_H_LLI_VALID;
- if (llp == DWC_LLP_LOC(hw_desc->llp)) {
- axi_chan_irq_clear(chan, hw_desc->lli->status_lo);
- if (i == 0)
- desc->completed_blocks = count - 1;
- else
- desc->completed_blocks = i - 1;
- if (((hw_desc->len * (i + 1)) % desc->period_len) == 0)
- vchan_cyclic_callback(vd);
- break;
- }
- }
- //axi_chan_enable(chan);
- }
- } else {
- if (unlikely(axi_chan_is_hw_enable(chan))) {
- dev_err(chan2dev(chan), "BUG: %s caught DWAXIDMAC_IRQ_DMA_TRF, but channel not idle!\n",
- axi_chan_name(chan));
- axi_chan_disable(chan);
- }
- /* Remove the completed descriptor from issued list before completing */
- list_del(&vd->node);
- vchan_cookie_complete(vd);
- /* Submit queued descriptors after processing the completed ones */
- axi_chan_start_first_queued(chan);
- }
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- }
- static irqreturn_t dw_axi_dma_interrupt(int irq, void *dev_id)
- {
- struct axi_dma_chip *chip = dev_id;
- struct dw_axi_dma *dw = chip->dw;
- struct axi_dma_chan *chan;
- u32 status, i;
- /* Disable DMAC inerrupts. We'll enable them after processing chanels */
- axi_dma_irq_disable(chip);
- /* Poll, clear and process every chanel interrupt status */
- for (i = 0; i < dw->hdata->nr_channels; i++) {
- chan = &dw->chan[i];
- status = axi_chan_irq_read(chan);
- axi_chan_irq_clear(chan, status);
- dev_vdbg(chip->dev, "%s %u IRQ status: 0x%08x\n",
- axi_chan_name(chan), i, status);
- if (status & DWAXIDMAC_IRQ_ALL_ERR)
- axi_chan_handle_err(chan, status);
- else if (status & (DWAXIDMAC_IRQ_DMA_TRF | DWAXIDMAC_IRQ_BLOCK_TRF))
- axi_chan_block_xfer_complete(chan);
- }
- /* Re-enable interrupts */
- axi_dma_irq_enable(chip);
- return IRQ_HANDLED;
- }
- static int dma_chan_terminate_all(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- u32 chan_active = BIT(chan->id) << DMAC_CHAN_EN_SHIFT;
- unsigned long flags;
- u32 val;
- int ret;
- LIST_HEAD(head);
- #ifdef DMA_GUARD_TIMER
- if (chan->cyclic)
- hrtimer_cancel(&chan->hrt);
- #endif
- axi_chan_disable(chan);
- ret = readl_poll_timeout_atomic(chan->chip->regs + DMAC_CHEN, val,
- !(val & chan_active), 1000, 10000);
- if (ret == -ETIMEDOUT) {
- dev_warn(dchan2dev(dchan),
- "%s failed to stop\n", axi_chan_name(chan));
- axi_chan_abort(chan);
- }
- spin_lock_irqsave(&chan->vc.lock, flags);
- vchan_get_all_descriptors(&chan->vc, &head);
- chan->cyclic = false;
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- vchan_dma_desc_free_list(&chan->vc, &head);
- dev_vdbg(dchan2dev(dchan), "terminated: %s\n", axi_chan_name(chan));
- return 0;
- }
- static int dma_chan_pause(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- unsigned long flags;
- unsigned int timeout = 20; /* timeout iterations */
- u32 val;
- spin_lock_irqsave(&chan->vc.lock, flags);
- #ifdef DMA_GUARD_TIMER
- if (chan->cyclic)
- hrtimer_cancel(&chan->hrt);
- #endif
- val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
- val |= BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT |
- BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT;
- axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
- do {
- if (axi_chan_irq_read(chan) & DWAXIDMAC_IRQ_SUSPENDED)
- break;
- udelay(2);
- } while (--timeout);
- axi_chan_irq_clear(chan, DWAXIDMAC_IRQ_SUSPENDED);
- chan->is_paused = true;
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- return timeout ? 0 : -EAGAIN;
- }
- /* Called in chan locked context */
- static inline void axi_chan_resume(struct axi_dma_chan *chan)
- {
- u32 val;
- val = axi_dma_ioread32(chan->chip, DMAC_CHEN);
- val &= ~(BIT(chan->id) << DMAC_CHAN_SUSP_SHIFT);
- val |= (BIT(chan->id) << DMAC_CHAN_SUSP_WE_SHIFT);
- axi_dma_iowrite32(chan->chip, DMAC_CHEN, val);
- chan->is_paused = false;
- }
- static int dma_chan_resume(struct dma_chan *dchan)
- {
- struct axi_dma_chan *chan = dchan_to_axi_dma_chan(dchan);
- unsigned long flags;
- spin_lock_irqsave(&chan->vc.lock, flags);
- if (chan->is_paused)
- axi_chan_resume(chan);
- #ifdef DMA_GUARD_TIMER
- if (chan->cyclic) {
- ktime_t kt = ktime_set(0, DMA_GUARD_TIMER_PERIOD);
- hrtimer_start(&chan->hrt, kt, HRTIMER_MODE_REL);
- }
- #endif
- spin_unlock_irqrestore(&chan->vc.lock, flags);
- return 0;
- }
- static int axi_dma_suspend(struct axi_dma_chip *chip)
- {
- axi_dma_irq_disable(chip);
- axi_dma_disable(chip);
- clk_disable_unprepare(chip->core_clk);
- clk_disable_unprepare(chip->cfgr_clk);
- return 0;
- }
- static int axi_dma_resume(struct axi_dma_chip *chip)
- {
- int ret;
- ret = clk_prepare_enable(chip->cfgr_clk);
- if (ret < 0)
- return ret;
- ret = clk_prepare_enable(chip->core_clk);
- if (ret < 0)
- return ret;
- axi_dma_enable(chip);
- axi_dma_irq_enable(chip);
- return 0;
- }
- static int __maybe_unused axi_dma_runtime_suspend(struct device *dev)
- {
- struct axi_dma_chip *chip = dev_get_drvdata(dev);
- return axi_dma_suspend(chip);
- }
- static int __maybe_unused axi_dma_runtime_resume(struct device *dev)
- {
- struct axi_dma_chip *chip = dev_get_drvdata(dev);
- return axi_dma_resume(chip);
- }
- static struct dma_chan *dw_axi_dma_of_xlate(struct of_phandle_args *dma_spec,
- struct of_dma *ofdma)
- {
- struct dw_axi_dma *dw = ofdma->of_dma_data;
- struct axi_dma_chan *chan;
- struct dma_chan *dchan;
- dchan = dma_get_any_slave_channel(&dw->dma);
- if (!dchan)
- return NULL;
- chan = dchan_to_axi_dma_chan(dchan);
- chan->hw_handshake_num = dma_spec->args[0];
- chan->m_master = dma_spec->args[1];
- chan->p_master = dma_spec->args[2];
- if (WARN_ON(chan->hw_handshake_num >= DMAC_MAX_NR_REQUESTS ||
- chan->m_master >= chan->chip->dw->hdata->nr_masters ||
- chan->p_master >= chan->chip->dw->hdata->nr_masters))
- return NULL;
- return dchan;
- }
- static int parse_device_properties(struct axi_dma_chip *chip)
- {
- struct device *dev = chip->dev;
- u32 tmp, carr[DMAC_MAX_CHANNELS];
- int ret;
- ret = device_property_read_u32(dev, "dma-channels", &tmp);
- if (ret)
- return ret;
- if (tmp == 0 || tmp > DMAC_MAX_CHANNELS)
- return -EINVAL;
- chip->dw->hdata->nr_channels = tmp;
- ret = device_property_read_u32(dev, "snps,dma-masters", &tmp);
- if (ret)
- return ret;
- if (tmp == 0 || tmp > DMAC_MAX_MASTERS)
- return -EINVAL;
- chip->dw->hdata->nr_masters = tmp;
- ret = device_property_read_u32(dev, "snps,data-width", &tmp);
- if (ret)
- return ret;
- if (tmp > DWAXIDMAC_TRANS_WIDTH_MAX)
- return -EINVAL;
- chip->dw->hdata->m_data_width = tmp;
- ret = device_property_read_u32_array(dev, "snps,block-size", carr,
- chip->dw->hdata->nr_channels);
- if (ret)
- return ret;
- for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
- if (carr[tmp] == 0 || carr[tmp] > DMAC_MAX_BLK_SIZE)
- return -EINVAL;
- chip->dw->hdata->block_size[tmp] = carr[tmp];
- }
- ret = device_property_read_u32_array(dev, "snps,priority", carr,
- chip->dw->hdata->nr_channels);
- if (ret)
- return ret;
- /* Priority value must be programmed within [0:nr_channels-1] range */
- for (tmp = 0; tmp < chip->dw->hdata->nr_channels; tmp++) {
- if (carr[tmp] >= chip->dw->hdata->nr_channels)
- return -EINVAL;
- chip->dw->hdata->priority[tmp] = carr[tmp];
- }
- /* axi-max-burst-len is optional property */
- ret = device_property_read_u32(dev, "snps,axi-max-burst-len", &tmp);
- if (!ret) {
- if (tmp > DWAXIDMAC_ARWLEN_MAX + 1)
- return -EINVAL;
- if (tmp < DWAXIDMAC_ARWLEN_MIN + 1)
- return -EINVAL;
- chip->dw->hdata->restrict_axi_burst_len = true;
- chip->dw->hdata->axi_rw_burst_len = tmp;
- }
- return 0;
- }
- static int dw_probe(struct platform_device *pdev)
- {
- //struct device_node *node = pdev->dev.of_node;
- struct axi_dma_chip *chip;
- struct resource *mem;
- struct dw_axi_dma *dw;
- struct dw_axi_dma_hcfg *hdata;
- u32 i;
- int ret;
- printk(KERN_ALERT "################dw_probe################\n");
- chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
- if (!chip)
- return -ENOMEM;
- dw = devm_kzalloc(&pdev->dev, sizeof(*dw), GFP_KERNEL);
- if (!dw)
- return -ENOMEM;
- hdata = devm_kzalloc(&pdev->dev, sizeof(*hdata), GFP_KERNEL);
- if (!hdata)
- return -ENOMEM;
- chip->dw = dw;
- chip->dev = &pdev->dev;
- chip->dw->hdata = hdata;
- chip->irq = platform_get_irq(pdev, 0);
- if (chip->irq < 0)
- return chip->irq;
- mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- chip->regs = devm_ioremap_resource(chip->dev, mem);
- if (IS_ERR(chip->regs))
- return PTR_ERR(chip->regs);
- chip->core_clk = devm_clk_get(chip->dev, "core-clk");
- if (IS_ERR(chip->core_clk))
- return PTR_ERR(chip->core_clk);
- chip->cfgr_clk = devm_clk_get(chip->dev, "cfgr-clk");
- if (IS_ERR(chip->cfgr_clk))
- return PTR_ERR(chip->cfgr_clk);
- ret = parse_device_properties(chip);
- if (ret)
- return ret;
- dw->chan = devm_kcalloc(chip->dev, hdata->nr_channels,
- sizeof(*dw->chan), GFP_KERNEL);
- if (!dw->chan)
- return -ENOMEM;
- ret = devm_request_irq(chip->dev, chip->irq, dw_axi_dma_interrupt,
- IRQF_SHARED, KBUILD_MODNAME, chip);
- if (ret)
- return ret;
- INIT_LIST_HEAD(&dw->dma.channels);
- for (i = 0; i < hdata->nr_channels; i++) {
- struct axi_dma_chan *chan = &dw->chan[i];
- chan->chip = chip;
- chan->id = i;
- chan->chan_regs = chip->regs + COMMON_REG_LEN + i * CHAN_REG_LEN;
- atomic_set(&chan->descs_allocated, 0);
- chan->vc.desc_free = vchan_desc_put;
- vchan_init(&chan->vc, &dw->dma);
- }
- /* Set capabilities */
- dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask);
- dma_cap_set(DMA_SLAVE, dw->dma.cap_mask);
- dma_cap_set(DMA_CYCLIC, dw->dma.cap_mask);
- /* DMA capabilities */
- dw->dma.chancnt = hdata->nr_channels;
- dw->dma.max_burst = hdata->axi_rw_burst_len;
- dw->dma.src_addr_widths = AXI_DMA_BUSWIDTHS;
- dw->dma.dst_addr_widths = AXI_DMA_BUSWIDTHS;
- dw->dma.directions = BIT(DMA_MEM_TO_MEM);
- dw->dma.directions |= BIT(DMA_MEM_TO_DEV) | BIT(DMA_DEV_TO_MEM);
- dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
- dw->dma.dev = chip->dev;
- dw->dma.device_tx_status = dma_chan_tx_status;
- dw->dma.device_issue_pending = dma_chan_issue_pending;
- dw->dma.device_terminate_all = dma_chan_terminate_all;
- dw->dma.device_pause = dma_chan_pause;
- dw->dma.device_resume = dma_chan_resume;
- dw->dma.device_alloc_chan_resources = dma_chan_alloc_chan_resources;
- dw->dma.device_free_chan_resources = dma_chan_free_chan_resources;
- dw->dma.device_prep_dma_memcpy = dma_chan_prep_dma_memcpy;
- dw->dma.device_synchronize = dw_axi_dma_synchronize;
- dw->dma.device_config = dw_axi_dma_chan_slave_config;
- dw->dma.device_prep_slave_sg = dw_axi_dma_chan_prep_slave_sg;
- dw->dma.device_prep_dma_cyclic = dw_axi_dma_chan_prep_cyclic;
- /*
- * Synopsis DesignWare AxiDMA datasheet mentioned Maximum
- * supported blocks is 1024. Device register width is 4 bytes.
- * Therefore, set constraint to 1024 * 4.
- */
- dw->dma.dev->dma_parms = &dw->dma_parms;
- dma_set_max_seg_size(&pdev->dev, MAX_BLOCK_SIZE);
- platform_set_drvdata(pdev, chip);
- pm_runtime_enable(chip->dev);
- /*
- * We can't just call pm_runtime_get here instead of
- * pm_runtime_get_noresume + axi_dma_resume because we need
- * driver to work also without Runtime PM.
- */
- pm_runtime_get_noresume(chip->dev);
- ret = axi_dma_resume(chip);
- if (ret < 0)
- goto err_pm_disable;
- axi_dma_hw_init(chip);
- pm_runtime_put(chip->dev);
- ret = dmaenginem_async_device_register(&dw->dma);
- if (ret)
- goto err_pm_disable;
- /* Register with OF helpers for DMA lookups */
- ret = of_dma_controller_register(pdev->dev.of_node,
- dw_axi_dma_of_xlate, dw);
- if (ret < 0)
- dev_warn(&pdev->dev,
- "Failed to register OF DMA controller, fallback to MEM_TO_MEM mode\n");
- dev_info(chip->dev, "DesignWare AXI DMA Controller, %d channels\n",
- dw->hdata->nr_channels);
- return 0;
- err_pm_disable:
- pm_runtime_disable(chip->dev);
- return ret;
- }
- static int dw_remove(struct platform_device *pdev)
- {
- struct axi_dma_chip *chip = platform_get_drvdata(pdev);
- struct dw_axi_dma *dw = chip->dw;
- struct axi_dma_chan *chan, *_chan;
- u32 i;
- /* Enable clk before accessing to registers */
- clk_prepare_enable(chip->cfgr_clk);
- clk_prepare_enable(chip->core_clk);
- axi_dma_irq_disable(chip);
- for (i = 0; i < dw->hdata->nr_channels; i++) {
- #ifdef DMA_GUARD_TIMER
- if (chip->dw->chan[i].cyclic)
- hrtimer_cancel(&chip->dw->chan[i].hrt);
- #endif
- axi_chan_disable(&chip->dw->chan[i]);
- axi_chan_irq_disable(&chip->dw->chan[i], DWAXIDMAC_IRQ_ALL);
- }
- axi_dma_disable(chip);
- pm_runtime_disable(chip->dev);
- axi_dma_suspend(chip);
- devm_free_irq(chip->dev, chip->irq, chip);
- of_dma_controller_free(chip->dev->of_node);
- list_for_each_entry_safe(chan, _chan, &dw->dma.channels,
- vc.chan.device_node) {
- list_del(&chan->vc.chan.device_node);
- tasklet_kill(&chan->vc.task);
- }
- return 0;
- }
- static const struct dev_pm_ops dw_axi_dma_pm_ops = {
- SET_RUNTIME_PM_OPS(axi_dma_runtime_suspend, axi_dma_runtime_resume, NULL)
- };
- static const struct of_device_id dw_dma_of_id_table[] = {
- { .compatible = "snps,axi-dma-1.01a" },
- {}
- };
- MODULE_DEVICE_TABLE(of, dw_dma_of_id_table);
- static struct platform_driver dw_driver = {
- .probe = dw_probe,
- .remove = dw_remove,
- .driver = {
- .name = KBUILD_MODNAME,
- .of_match_table = dw_dma_of_id_table,
- .pm = &dw_axi_dma_pm_ops,
- },
- };
- module_platform_driver(dw_driver);
- MODULE_LICENSE("GPL v2");
- MODULE_DESCRIPTION("Synopsys DesignWare AXI DMA Controller platform driver");
- MODULE_AUTHOR("Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>");
|