Path: blob/master/arch/blackfin/kernel/bfin_dma_5xx.c
10817 views
/*1* bfin_dma_5xx.c - Blackfin DMA implementation2*3* Copyright 2004-2008 Analog Devices Inc.4*5* Licensed under the GPL-2 or later.6*/78#include <linux/errno.h>9#include <linux/interrupt.h>10#include <linux/kernel.h>11#include <linux/module.h>12#include <linux/param.h>13#include <linux/proc_fs.h>14#include <linux/sched.h>15#include <linux/seq_file.h>16#include <linux/spinlock.h>1718#include <asm/blackfin.h>19#include <asm/cacheflush.h>20#include <asm/dma.h>21#include <asm/uaccess.h>22#include <asm/early_printk.h>2324/*25* To make sure we work around 05000119 - we always check DMA_DONE bit,26* never the DMA_RUN bit27*/2829struct dma_channel dma_ch[MAX_DMA_CHANNELS];30EXPORT_SYMBOL(dma_ch);3132static int __init blackfin_dma_init(void)33{34int i;3536printk(KERN_INFO "Blackfin DMA Controller\n");373839#if ANOMALY_0500048040bfin_write_DMAC_TC_PER(0x0111);41#endif4243for (i = 0; i < MAX_DMA_CHANNELS; i++) {44atomic_set(&dma_ch[i].chan_status, 0);45dma_ch[i].regs = dma_io_base_addr[i];46}47/* Mark MEMDMA Channel 0 as requested since we're using it internally */48request_dma(CH_MEM_STREAM0_DEST, "Blackfin dma_memcpy");49request_dma(CH_MEM_STREAM0_SRC, "Blackfin dma_memcpy");5051#if defined(CONFIG_DEB_DMA_URGENT)52bfin_write_EBIU_DDRQUE(bfin_read_EBIU_DDRQUE()53| DEB1_URGENT | DEB2_URGENT | DEB3_URGENT);54#endif5556return 0;57}58arch_initcall(blackfin_dma_init);5960#ifdef CONFIG_PROC_FS61static int proc_dma_show(struct seq_file *m, void *v)62{63int i;6465for (i = 0; i < MAX_DMA_CHANNELS; ++i)66if (dma_channel_active(i))67seq_printf(m, "%2d: %s\n", i, dma_ch[i].device_id);6869return 0;70}7172static int proc_dma_open(struct inode *inode, struct file *file)73{74return single_open(file, proc_dma_show, NULL);75}7677static const struct file_operations proc_dma_operations = {78.open = proc_dma_open,79.read = seq_read,80.llseek = seq_lseek,81.release = single_release,82};8384static int __init proc_dma_init(void)85{86return proc_create("dma", 0, NULL, &proc_dma_operations) != NULL;87}88late_initcall(proc_dma_init);89#endif9091static void set_dma_peripheral_map(unsigned int channel, const char *device_id)92{93#ifdef CONFIG_BF54x94unsigned int per_map;9596switch (channel) {97case CH_UART2_RX: per_map = 0xC << 12; break;98case CH_UART2_TX: per_map = 0xD << 12; break;99case CH_UART3_RX: per_map = 0xE << 12; break;100case CH_UART3_TX: per_map = 0xF << 12; break;101default: return;102}103104if (strncmp(device_id, "BFIN_UART", 9) == 0)105dma_ch[channel].regs->peripheral_map = per_map;106#endif107}108109/**110* request_dma - request a DMA channel111*112* Request the specific DMA channel from the system if it's available.113*/114int request_dma(unsigned int channel, const char *device_id)115{116pr_debug("request_dma() : BEGIN\n");117118if (device_id == NULL)119printk(KERN_WARNING "request_dma(%u): no device_id given\n", channel);120121#if defined(CONFIG_BF561) && ANOMALY_05000182122if (channel >= CH_IMEM_STREAM0_DEST && channel <= CH_IMEM_STREAM1_DEST) {123if (get_cclk() > 500000000) {124printk(KERN_WARNING125"Request IMDMA failed due to ANOMALY 05000182\n");126return -EFAULT;127}128}129#endif130131if (atomic_cmpxchg(&dma_ch[channel].chan_status, 0, 1)) {132pr_debug("DMA CHANNEL IN USE\n");133return -EBUSY;134}135136set_dma_peripheral_map(channel, device_id);137dma_ch[channel].device_id = device_id;138dma_ch[channel].irq = 0;139140/* This is to be enabled by putting a restriction -141* you have to request DMA, before doing any operations on142* descriptor/channel143*/144pr_debug("request_dma() : END\n");145return 0;146}147EXPORT_SYMBOL(request_dma);148149int set_dma_callback(unsigned int channel, irq_handler_t callback, void *data)150{151int ret;152unsigned int irq;153154BUG_ON(channel >= MAX_DMA_CHANNELS || !callback ||155!atomic_read(&dma_ch[channel].chan_status));156157irq = channel2irq(channel);158ret = request_irq(irq, callback, 0, dma_ch[channel].device_id, data);159if (ret)160return ret;161162dma_ch[channel].irq = irq;163dma_ch[channel].data = data;164165return 0;166}167EXPORT_SYMBOL(set_dma_callback);168169/**170* clear_dma_buffer - clear DMA fifos for specified channel171*172* Set the Buffer Clear bit in the Configuration register of specific DMA173* channel. This will stop the descriptor based DMA operation.174*/175static void clear_dma_buffer(unsigned int channel)176{177dma_ch[channel].regs->cfg |= RESTART;178SSYNC();179dma_ch[channel].regs->cfg &= ~RESTART;180}181182void free_dma(unsigned int channel)183{184pr_debug("freedma() : BEGIN\n");185BUG_ON(channel >= MAX_DMA_CHANNELS ||186!atomic_read(&dma_ch[channel].chan_status));187188/* Halt the DMA */189disable_dma(channel);190clear_dma_buffer(channel);191192if (dma_ch[channel].irq)193free_irq(dma_ch[channel].irq, dma_ch[channel].data);194195/* Clear the DMA Variable in the Channel */196atomic_set(&dma_ch[channel].chan_status, 0);197198pr_debug("freedma() : END\n");199}200EXPORT_SYMBOL(free_dma);201202#ifdef CONFIG_PM203# ifndef MAX_DMA_SUSPEND_CHANNELS204# define MAX_DMA_SUSPEND_CHANNELS MAX_DMA_CHANNELS205# endif206int blackfin_dma_suspend(void)207{208int i;209210for (i = 0; i < MAX_DMA_CHANNELS; ++i) {211if (dma_ch[i].regs->cfg & DMAEN) {212printk(KERN_ERR "DMA Channel %d failed to suspend\n", i);213return -EBUSY;214}215216if (i < MAX_DMA_SUSPEND_CHANNELS)217dma_ch[i].saved_peripheral_map = dma_ch[i].regs->peripheral_map;218}219220return 0;221}222223void blackfin_dma_resume(void)224{225int i;226227for (i = 0; i < MAX_DMA_CHANNELS; ++i) {228dma_ch[i].regs->cfg = 0;229230if (i < MAX_DMA_SUSPEND_CHANNELS)231dma_ch[i].regs->peripheral_map = dma_ch[i].saved_peripheral_map;232}233}234#endif235236/**237* blackfin_dma_early_init - minimal DMA init238*239* Setup a few DMA registers so we can safely do DMA transfers early on in240* the kernel booting process. Really this just means using dma_memcpy().241*/242void __init blackfin_dma_early_init(void)243{244early_shadow_stamp();245bfin_write_MDMA_S0_CONFIG(0);246bfin_write_MDMA_S1_CONFIG(0);247}248249void __init early_dma_memcpy(void *pdst, const void *psrc, size_t size)250{251unsigned long dst = (unsigned long)pdst;252unsigned long src = (unsigned long)psrc;253struct dma_register *dst_ch, *src_ch;254255early_shadow_stamp();256257/* We assume that everything is 4 byte aligned, so include258* a basic sanity check259*/260BUG_ON(dst % 4);261BUG_ON(src % 4);262BUG_ON(size % 4);263264src_ch = 0;265/* Find an avalible memDMA channel */266while (1) {267if (src_ch == (struct dma_register *)MDMA_S0_NEXT_DESC_PTR) {268dst_ch = (struct dma_register *)MDMA_D1_NEXT_DESC_PTR;269src_ch = (struct dma_register *)MDMA_S1_NEXT_DESC_PTR;270} else {271dst_ch = (struct dma_register *)MDMA_D0_NEXT_DESC_PTR;272src_ch = (struct dma_register *)MDMA_S0_NEXT_DESC_PTR;273}274275if (!bfin_read16(&src_ch->cfg))276break;277else if (bfin_read16(&dst_ch->irq_status) & DMA_DONE) {278bfin_write16(&src_ch->cfg, 0);279break;280}281}282283/* Force a sync in case a previous config reset on this channel284* occurred. This is needed so subsequent writes to DMA registers285* are not spuriously lost/corrupted.286*/287__builtin_bfin_ssync();288289/* Destination */290bfin_write32(&dst_ch->start_addr, dst);291bfin_write16(&dst_ch->x_count, size >> 2);292bfin_write16(&dst_ch->x_modify, 1 << 2);293bfin_write16(&dst_ch->irq_status, DMA_DONE | DMA_ERR);294295/* Source */296bfin_write32(&src_ch->start_addr, src);297bfin_write16(&src_ch->x_count, size >> 2);298bfin_write16(&src_ch->x_modify, 1 << 2);299bfin_write16(&src_ch->irq_status, DMA_DONE | DMA_ERR);300301/* Enable */302bfin_write16(&src_ch->cfg, DMAEN | WDSIZE_32);303bfin_write16(&dst_ch->cfg, WNR | DI_EN | DMAEN | WDSIZE_32);304305/* Since we are atomic now, don't use the workaround ssync */306__builtin_bfin_ssync();307}308309void __init early_dma_memcpy_done(void)310{311early_shadow_stamp();312313while ((bfin_read_MDMA_S0_CONFIG() && !(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE)) ||314(bfin_read_MDMA_S1_CONFIG() && !(bfin_read_MDMA_D1_IRQ_STATUS() & DMA_DONE)))315continue;316317bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);318bfin_write_MDMA_D1_IRQ_STATUS(DMA_DONE | DMA_ERR);319/*320* Now that DMA is done, we would normally flush cache, but321* i/d cache isn't running this early, so we don't bother,322* and just clear out the DMA channel for next time323*/324bfin_write_MDMA_S0_CONFIG(0);325bfin_write_MDMA_S1_CONFIG(0);326bfin_write_MDMA_D0_CONFIG(0);327bfin_write_MDMA_D1_CONFIG(0);328329__builtin_bfin_ssync();330}331332/**333* __dma_memcpy - program the MDMA registers334*335* Actually program MDMA0 and wait for the transfer to finish. Disable IRQs336* while programming registers so that everything is fully configured. Wait337* for DMA to finish with IRQs enabled. If interrupted, the initial DMA_DONE338* check will make sure we don't clobber any existing transfer.339*/340static void __dma_memcpy(u32 daddr, s16 dmod, u32 saddr, s16 smod, size_t cnt, u32 conf)341{342static DEFINE_SPINLOCK(mdma_lock);343unsigned long flags;344345spin_lock_irqsave(&mdma_lock, flags);346347/* Force a sync in case a previous config reset on this channel348* occurred. This is needed so subsequent writes to DMA registers349* are not spuriously lost/corrupted. Do it under irq lock and350* without the anomaly version (because we are atomic already).351*/352__builtin_bfin_ssync();353354if (bfin_read_MDMA_S0_CONFIG())355while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))356continue;357358if (conf & DMA2D) {359/* For larger bit sizes, we've already divided down cnt so it360* is no longer a multiple of 64k. So we have to break down361* the limit here so it is a multiple of the incoming size.362* There is no limitation here in terms of total size other363* than the hardware though as the bits lost in the shift are364* made up by MODIFY (== we can hit the whole address space).365* X: (2^(16 - 0)) * 1 == (2^(16 - 1)) * 2 == (2^(16 - 2)) * 4366*/367u32 shift = abs(dmod) >> 1;368size_t ycnt = cnt >> (16 - shift);369cnt = 1 << (16 - shift);370bfin_write_MDMA_D0_Y_COUNT(ycnt);371bfin_write_MDMA_S0_Y_COUNT(ycnt);372bfin_write_MDMA_D0_Y_MODIFY(dmod);373bfin_write_MDMA_S0_Y_MODIFY(smod);374}375376bfin_write_MDMA_D0_START_ADDR(daddr);377bfin_write_MDMA_D0_X_COUNT(cnt);378bfin_write_MDMA_D0_X_MODIFY(dmod);379bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);380381bfin_write_MDMA_S0_START_ADDR(saddr);382bfin_write_MDMA_S0_X_COUNT(cnt);383bfin_write_MDMA_S0_X_MODIFY(smod);384bfin_write_MDMA_S0_IRQ_STATUS(DMA_DONE | DMA_ERR);385386bfin_write_MDMA_S0_CONFIG(DMAEN | conf);387bfin_write_MDMA_D0_CONFIG(WNR | DI_EN | DMAEN | conf);388389spin_unlock_irqrestore(&mdma_lock, flags);390391SSYNC();392393while (!(bfin_read_MDMA_D0_IRQ_STATUS() & DMA_DONE))394if (bfin_read_MDMA_S0_CONFIG())395continue;396else397return;398399bfin_write_MDMA_D0_IRQ_STATUS(DMA_DONE | DMA_ERR);400401bfin_write_MDMA_S0_CONFIG(0);402bfin_write_MDMA_D0_CONFIG(0);403}404405/**406* _dma_memcpy - translate C memcpy settings into MDMA settings407*408* Handle all the high level steps before we touch the MDMA registers. So409* handle direction, tweaking of sizes, and formatting of addresses.410*/411static void *_dma_memcpy(void *pdst, const void *psrc, size_t size)412{413u32 conf, shift;414s16 mod;415unsigned long dst = (unsigned long)pdst;416unsigned long src = (unsigned long)psrc;417418if (size == 0)419return NULL;420421if (dst % 4 == 0 && src % 4 == 0 && size % 4 == 0) {422conf = WDSIZE_32;423shift = 2;424} else if (dst % 2 == 0 && src % 2 == 0 && size % 2 == 0) {425conf = WDSIZE_16;426shift = 1;427} else {428conf = WDSIZE_8;429shift = 0;430}431432/* If the two memory regions have a chance of overlapping, make433* sure the memcpy still works as expected. Do this by having the434* copy run backwards instead.435*/436mod = 1 << shift;437if (src < dst) {438mod *= -1;439dst += size + mod;440src += size + mod;441}442size >>= shift;443444if (size > 0x10000)445conf |= DMA2D;446447__dma_memcpy(dst, mod, src, mod, size, conf);448449return pdst;450}451452/**453* dma_memcpy - DMA memcpy under mutex lock454*455* Do not check arguments before starting the DMA memcpy. Break the transfer456* up into two pieces. The first transfer is in multiples of 64k and the457* second transfer is the piece smaller than 64k.458*/459void *dma_memcpy(void *pdst, const void *psrc, size_t size)460{461unsigned long dst = (unsigned long)pdst;462unsigned long src = (unsigned long)psrc;463464if (bfin_addr_dcacheable(src))465blackfin_dcache_flush_range(src, src + size);466467if (bfin_addr_dcacheable(dst))468blackfin_dcache_invalidate_range(dst, dst + size);469470return dma_memcpy_nocache(pdst, psrc, size);471}472EXPORT_SYMBOL(dma_memcpy);473474/**475* dma_memcpy_nocache - DMA memcpy under mutex lock476* - No cache flush/invalidate477*478* Do not check arguments before starting the DMA memcpy. Break the transfer479* up into two pieces. The first transfer is in multiples of 64k and the480* second transfer is the piece smaller than 64k.481*/482void *dma_memcpy_nocache(void *pdst, const void *psrc, size_t size)483{484size_t bulk, rest;485486bulk = size & ~0xffff;487rest = size - bulk;488if (bulk)489_dma_memcpy(pdst, psrc, bulk);490_dma_memcpy(pdst + bulk, psrc + bulk, rest);491return pdst;492}493EXPORT_SYMBOL(dma_memcpy_nocache);494495/**496* safe_dma_memcpy - DMA memcpy w/argument checking497*498* Verify arguments are safe before heading to dma_memcpy().499*/500void *safe_dma_memcpy(void *dst, const void *src, size_t size)501{502if (!access_ok(VERIFY_WRITE, dst, size))503return NULL;504if (!access_ok(VERIFY_READ, src, size))505return NULL;506return dma_memcpy(dst, src, size);507}508EXPORT_SYMBOL(safe_dma_memcpy);509510static void _dma_out(unsigned long addr, unsigned long buf, unsigned short len,511u16 size, u16 dma_size)512{513blackfin_dcache_flush_range(buf, buf + len * size);514__dma_memcpy(addr, 0, buf, size, len, dma_size);515}516517static void _dma_in(unsigned long addr, unsigned long buf, unsigned short len,518u16 size, u16 dma_size)519{520blackfin_dcache_invalidate_range(buf, buf + len * size);521__dma_memcpy(buf, size, addr, 0, len, dma_size);522}523524#define MAKE_DMA_IO(io, bwl, isize, dmasize, cnst) \525void dma_##io##s##bwl(unsigned long addr, cnst void *buf, unsigned short len) \526{ \527_dma_##io(addr, (unsigned long)buf, len, isize, WDSIZE_##dmasize); \528} \529EXPORT_SYMBOL(dma_##io##s##bwl)530MAKE_DMA_IO(out, b, 1, 8, const);531MAKE_DMA_IO(in, b, 1, 8, );532MAKE_DMA_IO(out, w, 2, 16, const);533MAKE_DMA_IO(in, w, 2, 16, );534MAKE_DMA_IO(out, l, 4, 32, const);535MAKE_DMA_IO(in, l, 4, 32, );536537538