Path: blob/master/arch/mips/alchemy/common/dbdma.c
10819 views
/*1*2* BRIEF MODULE DESCRIPTION3* The Descriptor Based DMA channel manager that first appeared4* on the Au1550. I started with dma.c, but I think all that is5* left is this initial comment :-)6*7* Copyright 2004 Embedded Edge, LLC8* [email protected]9*10* This program is free software; you can redistribute it and/or modify it11* under the terms of the GNU General Public License as published by the12* Free Software Foundation; either version 2 of the License, or (at your13* option) any later version.14*15* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED16* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF17* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN18* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF21* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON22* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*26* You should have received a copy of the GNU General Public License along27* with this program; if not, write to the Free Software Foundation, Inc.,28* 675 Mass Ave, Cambridge, MA 02139, USA.29*30*/3132#include <linux/init.h>33#include <linux/kernel.h>34#include <linux/slab.h>35#include <linux/spinlock.h>36#include <linux/interrupt.h>37#include <linux/module.h>38#include <linux/syscore_ops.h>39#include <asm/mach-au1x00/au1000.h>40#include <asm/mach-au1x00/au1xxx_dbdma.h>4142#if defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200)4344/*45* The Descriptor Based DMA supports up to 16 channels.46*47* There are 32 devices defined. We keep an internal structure48* of devices using these channels, along with additional49* information.50*51* We allocate the descriptors and allow access to them through various52* functions. The drivers allocate the data buffers and assign them53* to the descriptors.54*/55static DEFINE_SPINLOCK(au1xxx_dbdma_spin_lock);5657/* I couldn't find a macro that did this... */58#define ALIGN_ADDR(x, a) ((((u32)(x)) + (a-1)) & ~(a-1))5960static dbdma_global_t *dbdma_gptr =61(dbdma_global_t *)KSEG1ADDR(AU1550_DBDMA_CONF_PHYS_ADDR);62static int dbdma_initialized;6364static dbdev_tab_t dbdev_tab[] = {65#ifdef CONFIG_SOC_AU155066/* UARTS */67{ DSCR_CMD0_UART0_TX, DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },68{ DSCR_CMD0_UART0_RX, DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },69{ DSCR_CMD0_UART3_TX, DEV_FLAGS_OUT, 0, 8, 0x11400004, 0, 0 },70{ DSCR_CMD0_UART3_RX, DEV_FLAGS_IN, 0, 8, 0x11400000, 0, 0 },7172/* EXT DMA */73{ DSCR_CMD0_DMA_REQ0, 0, 0, 0, 0x00000000, 0, 0 },74{ DSCR_CMD0_DMA_REQ1, 0, 0, 0, 0x00000000, 0, 0 },75{ DSCR_CMD0_DMA_REQ2, 0, 0, 0, 0x00000000, 0, 0 },76{ DSCR_CMD0_DMA_REQ3, 0, 0, 0, 0x00000000, 0, 0 },7778/* USB DEV */79{ DSCR_CMD0_USBDEV_RX0, DEV_FLAGS_IN, 4, 8, 0x10200000, 0, 0 },80{ DSCR_CMD0_USBDEV_TX0, DEV_FLAGS_OUT, 4, 8, 0x10200004, 0, 0 },81{ DSCR_CMD0_USBDEV_TX1, DEV_FLAGS_OUT, 4, 8, 0x10200008, 0, 0 },82{ DSCR_CMD0_USBDEV_TX2, DEV_FLAGS_OUT, 4, 8, 0x1020000c, 0, 0 },83{ DSCR_CMD0_USBDEV_RX3, DEV_FLAGS_IN, 4, 8, 0x10200010, 0, 0 },84{ DSCR_CMD0_USBDEV_RX4, DEV_FLAGS_IN, 4, 8, 0x10200014, 0, 0 },8586/* PSC 0 */87{ DSCR_CMD0_PSC0_TX, DEV_FLAGS_OUT, 0, 0, 0x11a0001c, 0, 0 },88{ DSCR_CMD0_PSC0_RX, DEV_FLAGS_IN, 0, 0, 0x11a0001c, 0, 0 },8990/* PSC 1 */91{ DSCR_CMD0_PSC1_TX, DEV_FLAGS_OUT, 0, 0, 0x11b0001c, 0, 0 },92{ DSCR_CMD0_PSC1_RX, DEV_FLAGS_IN, 0, 0, 0x11b0001c, 0, 0 },9394/* PSC 2 */95{ DSCR_CMD0_PSC2_TX, DEV_FLAGS_OUT, 0, 0, 0x10a0001c, 0, 0 },96{ DSCR_CMD0_PSC2_RX, DEV_FLAGS_IN, 0, 0, 0x10a0001c, 0, 0 },9798/* PSC 3 */99{ DSCR_CMD0_PSC3_TX, DEV_FLAGS_OUT, 0, 0, 0x10b0001c, 0, 0 },100{ DSCR_CMD0_PSC3_RX, DEV_FLAGS_IN, 0, 0, 0x10b0001c, 0, 0 },101102{ DSCR_CMD0_PCI_WRITE, 0, 0, 0, 0x00000000, 0, 0 }, /* PCI */103{ DSCR_CMD0_NAND_FLASH, 0, 0, 0, 0x00000000, 0, 0 }, /* NAND */104105/* MAC 0 */106{ DSCR_CMD0_MAC0_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },107{ DSCR_CMD0_MAC0_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },108109/* MAC 1 */110{ DSCR_CMD0_MAC1_RX, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },111{ DSCR_CMD0_MAC1_TX, DEV_FLAGS_OUT, 0, 0, 0x00000000, 0, 0 },112113#endif /* CONFIG_SOC_AU1550 */114115#ifdef CONFIG_SOC_AU1200116{ DSCR_CMD0_UART0_TX, DEV_FLAGS_OUT, 0, 8, 0x11100004, 0, 0 },117{ DSCR_CMD0_UART0_RX, DEV_FLAGS_IN, 0, 8, 0x11100000, 0, 0 },118{ DSCR_CMD0_UART1_TX, DEV_FLAGS_OUT, 0, 8, 0x11200004, 0, 0 },119{ DSCR_CMD0_UART1_RX, DEV_FLAGS_IN, 0, 8, 0x11200000, 0, 0 },120121{ DSCR_CMD0_DMA_REQ0, 0, 0, 0, 0x00000000, 0, 0 },122{ DSCR_CMD0_DMA_REQ1, 0, 0, 0, 0x00000000, 0, 0 },123124{ DSCR_CMD0_MAE_BE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },125{ DSCR_CMD0_MAE_FE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },126{ DSCR_CMD0_MAE_BOTH, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },127{ DSCR_CMD0_LCD, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },128129{ DSCR_CMD0_SDMS_TX0, DEV_FLAGS_OUT, 4, 8, 0x10600000, 0, 0 },130{ DSCR_CMD0_SDMS_RX0, DEV_FLAGS_IN, 4, 8, 0x10600004, 0, 0 },131{ DSCR_CMD0_SDMS_TX1, DEV_FLAGS_OUT, 4, 8, 0x10680000, 0, 0 },132{ DSCR_CMD0_SDMS_RX1, DEV_FLAGS_IN, 4, 8, 0x10680004, 0, 0 },133134{ DSCR_CMD0_AES_RX, DEV_FLAGS_IN , 4, 32, 0x10300008, 0, 0 },135{ DSCR_CMD0_AES_TX, DEV_FLAGS_OUT, 4, 32, 0x10300004, 0, 0 },136137{ DSCR_CMD0_PSC0_TX, DEV_FLAGS_OUT, 0, 16, 0x11a0001c, 0, 0 },138{ DSCR_CMD0_PSC0_RX, DEV_FLAGS_IN, 0, 16, 0x11a0001c, 0, 0 },139{ DSCR_CMD0_PSC0_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },140141{ DSCR_CMD0_PSC1_TX, DEV_FLAGS_OUT, 0, 16, 0x11b0001c, 0, 0 },142{ DSCR_CMD0_PSC1_RX, DEV_FLAGS_IN, 0, 16, 0x11b0001c, 0, 0 },143{ DSCR_CMD0_PSC1_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },144145{ DSCR_CMD0_CIM_RXA, DEV_FLAGS_IN, 0, 32, 0x14004020, 0, 0 },146{ DSCR_CMD0_CIM_RXB, DEV_FLAGS_IN, 0, 32, 0x14004040, 0, 0 },147{ DSCR_CMD0_CIM_RXC, DEV_FLAGS_IN, 0, 32, 0x14004060, 0, 0 },148{ DSCR_CMD0_CIM_SYNC, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },149150{ DSCR_CMD0_NAND_FLASH, DEV_FLAGS_IN, 0, 0, 0x00000000, 0, 0 },151152#endif /* CONFIG_SOC_AU1200 */153154{ DSCR_CMD0_THROTTLE, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },155{ DSCR_CMD0_ALWAYS, DEV_FLAGS_ANYUSE, 0, 0, 0x00000000, 0, 0 },156157/* Provide 16 user definable device types */158{ ~0, 0, 0, 0, 0, 0, 0 },159{ ~0, 0, 0, 0, 0, 0, 0 },160{ ~0, 0, 0, 0, 0, 0, 0 },161{ ~0, 0, 0, 0, 0, 0, 0 },162{ ~0, 0, 0, 0, 0, 0, 0 },163{ ~0, 0, 0, 0, 0, 0, 0 },164{ ~0, 0, 0, 0, 0, 0, 0 },165{ ~0, 0, 0, 0, 0, 0, 0 },166{ ~0, 0, 0, 0, 0, 0, 0 },167{ ~0, 0, 0, 0, 0, 0, 0 },168{ ~0, 0, 0, 0, 0, 0, 0 },169{ ~0, 0, 0, 0, 0, 0, 0 },170{ ~0, 0, 0, 0, 0, 0, 0 },171{ ~0, 0, 0, 0, 0, 0, 0 },172{ ~0, 0, 0, 0, 0, 0, 0 },173{ ~0, 0, 0, 0, 0, 0, 0 },174};175176#define DBDEV_TAB_SIZE ARRAY_SIZE(dbdev_tab)177178179static chan_tab_t *chan_tab_ptr[NUM_DBDMA_CHANS];180181static dbdev_tab_t *find_dbdev_id(u32 id)182{183int i;184dbdev_tab_t *p;185for (i = 0; i < DBDEV_TAB_SIZE; ++i) {186p = &dbdev_tab[i];187if (p->dev_id == id)188return p;189}190return NULL;191}192193void *au1xxx_ddma_get_nextptr_virt(au1x_ddma_desc_t *dp)194{195return phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));196}197EXPORT_SYMBOL(au1xxx_ddma_get_nextptr_virt);198199u32 au1xxx_ddma_add_device(dbdev_tab_t *dev)200{201u32 ret = 0;202dbdev_tab_t *p;203static u16 new_id = 0x1000;204205p = find_dbdev_id(~0);206if (NULL != p) {207memcpy(p, dev, sizeof(dbdev_tab_t));208p->dev_id = DSCR_DEV2CUSTOM_ID(new_id, dev->dev_id);209ret = p->dev_id;210new_id++;211#if 0212printk(KERN_DEBUG "add_device: id:%x flags:%x padd:%x\n",213p->dev_id, p->dev_flags, p->dev_physaddr);214#endif215}216217return ret;218}219EXPORT_SYMBOL(au1xxx_ddma_add_device);220221void au1xxx_ddma_del_device(u32 devid)222{223dbdev_tab_t *p = find_dbdev_id(devid);224225if (p != NULL) {226memset(p, 0, sizeof(dbdev_tab_t));227p->dev_id = ~0;228}229}230EXPORT_SYMBOL(au1xxx_ddma_del_device);231232/* Allocate a channel and return a non-zero descriptor if successful. */233u32 au1xxx_dbdma_chan_alloc(u32 srcid, u32 destid,234void (*callback)(int, void *), void *callparam)235{236unsigned long flags;237u32 used, chan;238u32 dcp;239int i;240dbdev_tab_t *stp, *dtp;241chan_tab_t *ctp;242au1x_dma_chan_t *cp;243244/*245* We do the intialization on the first channel allocation.246* We have to wait because of the interrupt handler initialization247* which can't be done successfully during board set up.248*/249if (!dbdma_initialized)250return 0;251252stp = find_dbdev_id(srcid);253if (stp == NULL)254return 0;255dtp = find_dbdev_id(destid);256if (dtp == NULL)257return 0;258259used = 0;260261/* Check to see if we can get both channels. */262spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);263if (!(stp->dev_flags & DEV_FLAGS_INUSE) ||264(stp->dev_flags & DEV_FLAGS_ANYUSE)) {265/* Got source */266stp->dev_flags |= DEV_FLAGS_INUSE;267if (!(dtp->dev_flags & DEV_FLAGS_INUSE) ||268(dtp->dev_flags & DEV_FLAGS_ANYUSE)) {269/* Got destination */270dtp->dev_flags |= DEV_FLAGS_INUSE;271} else {272/* Can't get dest. Release src. */273stp->dev_flags &= ~DEV_FLAGS_INUSE;274used++;275}276} else277used++;278spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);279280if (used)281return 0;282283/* Let's see if we can allocate a channel for it. */284ctp = NULL;285chan = 0;286spin_lock_irqsave(&au1xxx_dbdma_spin_lock, flags);287for (i = 0; i < NUM_DBDMA_CHANS; i++)288if (chan_tab_ptr[i] == NULL) {289/*290* If kmalloc fails, it is caught below same291* as a channel not available.292*/293ctp = kmalloc(sizeof(chan_tab_t), GFP_ATOMIC);294chan_tab_ptr[i] = ctp;295break;296}297spin_unlock_irqrestore(&au1xxx_dbdma_spin_lock, flags);298299if (ctp != NULL) {300memset(ctp, 0, sizeof(chan_tab_t));301ctp->chan_index = chan = i;302dcp = KSEG1ADDR(AU1550_DBDMA_PHYS_ADDR);303dcp += (0x0100 * chan);304ctp->chan_ptr = (au1x_dma_chan_t *)dcp;305cp = (au1x_dma_chan_t *)dcp;306ctp->chan_src = stp;307ctp->chan_dest = dtp;308ctp->chan_callback = callback;309ctp->chan_callparam = callparam;310311/* Initialize channel configuration. */312i = 0;313if (stp->dev_intlevel)314i |= DDMA_CFG_SED;315if (stp->dev_intpolarity)316i |= DDMA_CFG_SP;317if (dtp->dev_intlevel)318i |= DDMA_CFG_DED;319if (dtp->dev_intpolarity)320i |= DDMA_CFG_DP;321if ((stp->dev_flags & DEV_FLAGS_SYNC) ||322(dtp->dev_flags & DEV_FLAGS_SYNC))323i |= DDMA_CFG_SYNC;324cp->ddma_cfg = i;325au_sync();326327/*328* Return a non-zero value that can be used to find the channel329* information in subsequent operations.330*/331return (u32)(&chan_tab_ptr[chan]);332}333334/* Release devices */335stp->dev_flags &= ~DEV_FLAGS_INUSE;336dtp->dev_flags &= ~DEV_FLAGS_INUSE;337338return 0;339}340EXPORT_SYMBOL(au1xxx_dbdma_chan_alloc);341342/*343* Set the device width if source or destination is a FIFO.344* Should be 8, 16, or 32 bits.345*/346u32 au1xxx_dbdma_set_devwidth(u32 chanid, int bits)347{348u32 rv;349chan_tab_t *ctp;350dbdev_tab_t *stp, *dtp;351352ctp = *((chan_tab_t **)chanid);353stp = ctp->chan_src;354dtp = ctp->chan_dest;355rv = 0;356357if (stp->dev_flags & DEV_FLAGS_IN) { /* Source in fifo */358rv = stp->dev_devwidth;359stp->dev_devwidth = bits;360}361if (dtp->dev_flags & DEV_FLAGS_OUT) { /* Destination out fifo */362rv = dtp->dev_devwidth;363dtp->dev_devwidth = bits;364}365366return rv;367}368EXPORT_SYMBOL(au1xxx_dbdma_set_devwidth);369370/* Allocate a descriptor ring, initializing as much as possible. */371u32 au1xxx_dbdma_ring_alloc(u32 chanid, int entries)372{373int i;374u32 desc_base, srcid, destid;375u32 cmd0, cmd1, src1, dest1;376u32 src0, dest0;377chan_tab_t *ctp;378dbdev_tab_t *stp, *dtp;379au1x_ddma_desc_t *dp;380381/*382* I guess we could check this to be within the383* range of the table......384*/385ctp = *((chan_tab_t **)chanid);386stp = ctp->chan_src;387dtp = ctp->chan_dest;388389/*390* The descriptors must be 32-byte aligned. There is a391* possibility the allocation will give us such an address,392* and if we try that first we are likely to not waste larger393* slabs of memory.394*/395desc_base = (u32)kmalloc(entries * sizeof(au1x_ddma_desc_t),396GFP_KERNEL|GFP_DMA);397if (desc_base == 0)398return 0;399400if (desc_base & 0x1f) {401/*402* Lost....do it again, allocate extra, and round403* the address base.404*/405kfree((const void *)desc_base);406i = entries * sizeof(au1x_ddma_desc_t);407i += (sizeof(au1x_ddma_desc_t) - 1);408desc_base = (u32)kmalloc(i, GFP_KERNEL|GFP_DMA);409if (desc_base == 0)410return 0;411412ctp->cdb_membase = desc_base;413desc_base = ALIGN_ADDR(desc_base, sizeof(au1x_ddma_desc_t));414} else415ctp->cdb_membase = desc_base;416417dp = (au1x_ddma_desc_t *)desc_base;418419/* Keep track of the base descriptor. */420ctp->chan_desc_base = dp;421422/* Initialize the rings with as much information as we know. */423srcid = stp->dev_id;424destid = dtp->dev_id;425426cmd0 = cmd1 = src1 = dest1 = 0;427src0 = dest0 = 0;428429cmd0 |= DSCR_CMD0_SID(srcid);430cmd0 |= DSCR_CMD0_DID(destid);431cmd0 |= DSCR_CMD0_IE | DSCR_CMD0_CV;432cmd0 |= DSCR_CMD0_ST(DSCR_CMD0_ST_NOCHANGE);433434/* Is it mem to mem transfer? */435if (((DSCR_CUSTOM2DEV_ID(srcid) == DSCR_CMD0_THROTTLE) ||436(DSCR_CUSTOM2DEV_ID(srcid) == DSCR_CMD0_ALWAYS)) &&437((DSCR_CUSTOM2DEV_ID(destid) == DSCR_CMD0_THROTTLE) ||438(DSCR_CUSTOM2DEV_ID(destid) == DSCR_CMD0_ALWAYS)))439cmd0 |= DSCR_CMD0_MEM;440441switch (stp->dev_devwidth) {442case 8:443cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_BYTE);444break;445case 16:446cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_HALFWORD);447break;448case 32:449default:450cmd0 |= DSCR_CMD0_SW(DSCR_CMD0_WORD);451break;452}453454switch (dtp->dev_devwidth) {455case 8:456cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_BYTE);457break;458case 16:459cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_HALFWORD);460break;461case 32:462default:463cmd0 |= DSCR_CMD0_DW(DSCR_CMD0_WORD);464break;465}466467/*468* If the device is marked as an in/out FIFO, ensure it is469* set non-coherent.470*/471if (stp->dev_flags & DEV_FLAGS_IN)472cmd0 |= DSCR_CMD0_SN; /* Source in FIFO */473if (dtp->dev_flags & DEV_FLAGS_OUT)474cmd0 |= DSCR_CMD0_DN; /* Destination out FIFO */475476/*477* Set up source1. For now, assume no stride and increment.478* A channel attribute update can change this later.479*/480switch (stp->dev_tsize) {481case 1:482src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE1);483break;484case 2:485src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE2);486break;487case 4:488src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE4);489break;490case 8:491default:492src1 |= DSCR_SRC1_STS(DSCR_xTS_SIZE8);493break;494}495496/* If source input is FIFO, set static address. */497if (stp->dev_flags & DEV_FLAGS_IN) {498if (stp->dev_flags & DEV_FLAGS_BURSTABLE)499src1 |= DSCR_SRC1_SAM(DSCR_xAM_BURST);500else501src1 |= DSCR_SRC1_SAM(DSCR_xAM_STATIC);502}503504if (stp->dev_physaddr)505src0 = stp->dev_physaddr;506507/*508* Set up dest1. For now, assume no stride and increment.509* A channel attribute update can change this later.510*/511switch (dtp->dev_tsize) {512case 1:513dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE1);514break;515case 2:516dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE2);517break;518case 4:519dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE4);520break;521case 8:522default:523dest1 |= DSCR_DEST1_DTS(DSCR_xTS_SIZE8);524break;525}526527/* If destination output is FIFO, set static address. */528if (dtp->dev_flags & DEV_FLAGS_OUT) {529if (dtp->dev_flags & DEV_FLAGS_BURSTABLE)530dest1 |= DSCR_DEST1_DAM(DSCR_xAM_BURST);531else532dest1 |= DSCR_DEST1_DAM(DSCR_xAM_STATIC);533}534535if (dtp->dev_physaddr)536dest0 = dtp->dev_physaddr;537538#if 0539printk(KERN_DEBUG "did:%x sid:%x cmd0:%x cmd1:%x source0:%x "540"source1:%x dest0:%x dest1:%x\n",541dtp->dev_id, stp->dev_id, cmd0, cmd1, src0,542src1, dest0, dest1);543#endif544for (i = 0; i < entries; i++) {545dp->dscr_cmd0 = cmd0;546dp->dscr_cmd1 = cmd1;547dp->dscr_source0 = src0;548dp->dscr_source1 = src1;549dp->dscr_dest0 = dest0;550dp->dscr_dest1 = dest1;551dp->dscr_stat = 0;552dp->sw_context = 0;553dp->sw_status = 0;554dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(dp + 1));555dp++;556}557558/* Make last descrptor point to the first. */559dp--;560dp->dscr_nxtptr = DSCR_NXTPTR(virt_to_phys(ctp->chan_desc_base));561ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;562563return (u32)ctp->chan_desc_base;564}565EXPORT_SYMBOL(au1xxx_dbdma_ring_alloc);566567/*568* Put a source buffer into the DMA ring.569* This updates the source pointer and byte count. Normally used570* for memory to fifo transfers.571*/572u32 au1xxx_dbdma_put_source(u32 chanid, dma_addr_t buf, int nbytes, u32 flags)573{574chan_tab_t *ctp;575au1x_ddma_desc_t *dp;576577/*578* I guess we could check this to be within the579* range of the table......580*/581ctp = *(chan_tab_t **)chanid;582583/*584* We should have multiple callers for a particular channel,585* an interrupt doesn't affect this pointer nor the descriptor,586* so no locking should be needed.587*/588dp = ctp->put_ptr;589590/*591* If the descriptor is valid, we are way ahead of the DMA592* engine, so just return an error condition.593*/594if (dp->dscr_cmd0 & DSCR_CMD0_V)595return 0;596597/* Load up buffer address and byte count. */598dp->dscr_source0 = buf & ~0UL;599dp->dscr_cmd1 = nbytes;600/* Check flags */601if (flags & DDMA_FLAGS_IE)602dp->dscr_cmd0 |= DSCR_CMD0_IE;603if (flags & DDMA_FLAGS_NOIE)604dp->dscr_cmd0 &= ~DSCR_CMD0_IE;605606/*607* There is an errata on the Au1200/Au1550 parts that could result608* in "stale" data being DMA'ed. It has to do with the snoop logic on609* the cache eviction buffer. DMA_NONCOHERENT is on by default for610* these parts. If it is fixed in the future, these dma_cache_inv will611* just be nothing more than empty macros. See io.h.612*/613dma_cache_wback_inv((unsigned long)buf, nbytes);614dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */615au_sync();616dma_cache_wback_inv((unsigned long)dp, sizeof(*dp));617ctp->chan_ptr->ddma_dbell = 0;618619/* Get next descriptor pointer. */620ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));621622/* Return something non-zero. */623return nbytes;624}625EXPORT_SYMBOL(au1xxx_dbdma_put_source);626627/* Put a destination buffer into the DMA ring.628* This updates the destination pointer and byte count. Normally used629* to place an empty buffer into the ring for fifo to memory transfers.630*/631u32 au1xxx_dbdma_put_dest(u32 chanid, dma_addr_t buf, int nbytes, u32 flags)632{633chan_tab_t *ctp;634au1x_ddma_desc_t *dp;635636/* I guess we could check this to be within the637* range of the table......638*/639ctp = *((chan_tab_t **)chanid);640641/* We should have multiple callers for a particular channel,642* an interrupt doesn't affect this pointer nor the descriptor,643* so no locking should be needed.644*/645dp = ctp->put_ptr;646647/* If the descriptor is valid, we are way ahead of the DMA648* engine, so just return an error condition.649*/650if (dp->dscr_cmd0 & DSCR_CMD0_V)651return 0;652653/* Load up buffer address and byte count */654655/* Check flags */656if (flags & DDMA_FLAGS_IE)657dp->dscr_cmd0 |= DSCR_CMD0_IE;658if (flags & DDMA_FLAGS_NOIE)659dp->dscr_cmd0 &= ~DSCR_CMD0_IE;660661dp->dscr_dest0 = buf & ~0UL;662dp->dscr_cmd1 = nbytes;663#if 0664printk(KERN_DEBUG "cmd0:%x cmd1:%x source0:%x source1:%x dest0:%x dest1:%x\n",665dp->dscr_cmd0, dp->dscr_cmd1, dp->dscr_source0,666dp->dscr_source1, dp->dscr_dest0, dp->dscr_dest1);667#endif668/*669* There is an errata on the Au1200/Au1550 parts that could result in670* "stale" data being DMA'ed. It has to do with the snoop logic on the671* cache eviction buffer. DMA_NONCOHERENT is on by default for these672* parts. If it is fixed in the future, these dma_cache_inv will just673* be nothing more than empty macros. See io.h.674*/675dma_cache_inv((unsigned long)buf, nbytes);676dp->dscr_cmd0 |= DSCR_CMD0_V; /* Let it rip */677au_sync();678dma_cache_wback_inv((unsigned long)dp, sizeof(*dp));679ctp->chan_ptr->ddma_dbell = 0;680681/* Get next descriptor pointer. */682ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));683684/* Return something non-zero. */685return nbytes;686}687EXPORT_SYMBOL(au1xxx_dbdma_put_dest);688689/*690* Get a destination buffer into the DMA ring.691* Normally used to get a full buffer from the ring during fifo692* to memory transfers. This does not set the valid bit, you will693* have to put another destination buffer to keep the DMA going.694*/695u32 au1xxx_dbdma_get_dest(u32 chanid, void **buf, int *nbytes)696{697chan_tab_t *ctp;698au1x_ddma_desc_t *dp;699u32 rv;700701/*702* I guess we could check this to be within the703* range of the table......704*/705ctp = *((chan_tab_t **)chanid);706707/*708* We should have multiple callers for a particular channel,709* an interrupt doesn't affect this pointer nor the descriptor,710* so no locking should be needed.711*/712dp = ctp->get_ptr;713714/*715* If the descriptor is valid, we are way ahead of the DMA716* engine, so just return an error condition.717*/718if (dp->dscr_cmd0 & DSCR_CMD0_V)719return 0;720721/* Return buffer address and byte count. */722*buf = (void *)(phys_to_virt(dp->dscr_dest0));723*nbytes = dp->dscr_cmd1;724rv = dp->dscr_stat;725726/* Get next descriptor pointer. */727ctp->get_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));728729/* Return something non-zero. */730return rv;731}732EXPORT_SYMBOL_GPL(au1xxx_dbdma_get_dest);733734void au1xxx_dbdma_stop(u32 chanid)735{736chan_tab_t *ctp;737au1x_dma_chan_t *cp;738int halt_timeout = 0;739740ctp = *((chan_tab_t **)chanid);741742cp = ctp->chan_ptr;743cp->ddma_cfg &= ~DDMA_CFG_EN; /* Disable channel */744au_sync();745while (!(cp->ddma_stat & DDMA_STAT_H)) {746udelay(1);747halt_timeout++;748if (halt_timeout > 100) {749printk(KERN_WARNING "warning: DMA channel won't halt\n");750break;751}752}753/* clear current desc valid and doorbell */754cp->ddma_stat |= (DDMA_STAT_DB | DDMA_STAT_V);755au_sync();756}757EXPORT_SYMBOL(au1xxx_dbdma_stop);758759/*760* Start using the current descriptor pointer. If the DBDMA encounters761* a non-valid descriptor, it will stop. In this case, we can just762* continue by adding a buffer to the list and starting again.763*/764void au1xxx_dbdma_start(u32 chanid)765{766chan_tab_t *ctp;767au1x_dma_chan_t *cp;768769ctp = *((chan_tab_t **)chanid);770cp = ctp->chan_ptr;771cp->ddma_desptr = virt_to_phys(ctp->cur_ptr);772cp->ddma_cfg |= DDMA_CFG_EN; /* Enable channel */773au_sync();774cp->ddma_dbell = 0;775au_sync();776}777EXPORT_SYMBOL(au1xxx_dbdma_start);778779void au1xxx_dbdma_reset(u32 chanid)780{781chan_tab_t *ctp;782au1x_ddma_desc_t *dp;783784au1xxx_dbdma_stop(chanid);785786ctp = *((chan_tab_t **)chanid);787ctp->get_ptr = ctp->put_ptr = ctp->cur_ptr = ctp->chan_desc_base;788789/* Run through the descriptors and reset the valid indicator. */790dp = ctp->chan_desc_base;791792do {793dp->dscr_cmd0 &= ~DSCR_CMD0_V;794/*795* Reset our software status -- this is used to determine796* if a descriptor is in use by upper level software. Since797* posting can reset 'V' bit.798*/799dp->sw_status = 0;800dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));801} while (dp != ctp->chan_desc_base);802}803EXPORT_SYMBOL(au1xxx_dbdma_reset);804805u32 au1xxx_get_dma_residue(u32 chanid)806{807chan_tab_t *ctp;808au1x_dma_chan_t *cp;809u32 rv;810811ctp = *((chan_tab_t **)chanid);812cp = ctp->chan_ptr;813814/* This is only valid if the channel is stopped. */815rv = cp->ddma_bytecnt;816au_sync();817818return rv;819}820EXPORT_SYMBOL_GPL(au1xxx_get_dma_residue);821822void au1xxx_dbdma_chan_free(u32 chanid)823{824chan_tab_t *ctp;825dbdev_tab_t *stp, *dtp;826827ctp = *((chan_tab_t **)chanid);828stp = ctp->chan_src;829dtp = ctp->chan_dest;830831au1xxx_dbdma_stop(chanid);832833kfree((void *)ctp->cdb_membase);834835stp->dev_flags &= ~DEV_FLAGS_INUSE;836dtp->dev_flags &= ~DEV_FLAGS_INUSE;837chan_tab_ptr[ctp->chan_index] = NULL;838839kfree(ctp);840}841EXPORT_SYMBOL(au1xxx_dbdma_chan_free);842843static irqreturn_t dbdma_interrupt(int irq, void *dev_id)844{845u32 intstat;846u32 chan_index;847chan_tab_t *ctp;848au1x_ddma_desc_t *dp;849au1x_dma_chan_t *cp;850851intstat = dbdma_gptr->ddma_intstat;852au_sync();853chan_index = __ffs(intstat);854855ctp = chan_tab_ptr[chan_index];856cp = ctp->chan_ptr;857dp = ctp->cur_ptr;858859/* Reset interrupt. */860cp->ddma_irq = 0;861au_sync();862863if (ctp->chan_callback)864ctp->chan_callback(irq, ctp->chan_callparam);865866ctp->cur_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));867return IRQ_RETVAL(1);868}869870void au1xxx_dbdma_dump(u32 chanid)871{872chan_tab_t *ctp;873au1x_ddma_desc_t *dp;874dbdev_tab_t *stp, *dtp;875au1x_dma_chan_t *cp;876u32 i = 0;877878ctp = *((chan_tab_t **)chanid);879stp = ctp->chan_src;880dtp = ctp->chan_dest;881cp = ctp->chan_ptr;882883printk(KERN_DEBUG "Chan %x, stp %x (dev %d) dtp %x (dev %d)\n",884(u32)ctp, (u32)stp, stp - dbdev_tab, (u32)dtp,885dtp - dbdev_tab);886printk(KERN_DEBUG "desc base %x, get %x, put %x, cur %x\n",887(u32)(ctp->chan_desc_base), (u32)(ctp->get_ptr),888(u32)(ctp->put_ptr), (u32)(ctp->cur_ptr));889890printk(KERN_DEBUG "dbdma chan %x\n", (u32)cp);891printk(KERN_DEBUG "cfg %08x, desptr %08x, statptr %08x\n",892cp->ddma_cfg, cp->ddma_desptr, cp->ddma_statptr);893printk(KERN_DEBUG "dbell %08x, irq %08x, stat %08x, bytecnt %08x\n",894cp->ddma_dbell, cp->ddma_irq, cp->ddma_stat,895cp->ddma_bytecnt);896897/* Run through the descriptors */898dp = ctp->chan_desc_base;899900do {901printk(KERN_DEBUG "Dp[%d]= %08x, cmd0 %08x, cmd1 %08x\n",902i++, (u32)dp, dp->dscr_cmd0, dp->dscr_cmd1);903printk(KERN_DEBUG "src0 %08x, src1 %08x, dest0 %08x, dest1 %08x\n",904dp->dscr_source0, dp->dscr_source1,905dp->dscr_dest0, dp->dscr_dest1);906printk(KERN_DEBUG "stat %08x, nxtptr %08x\n",907dp->dscr_stat, dp->dscr_nxtptr);908dp = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));909} while (dp != ctp->chan_desc_base);910}911912/* Put a descriptor into the DMA ring.913* This updates the source/destination pointers and byte count.914*/915u32 au1xxx_dbdma_put_dscr(u32 chanid, au1x_ddma_desc_t *dscr)916{917chan_tab_t *ctp;918au1x_ddma_desc_t *dp;919u32 nbytes = 0;920921/*922* I guess we could check this to be within the923* range of the table......924*/925ctp = *((chan_tab_t **)chanid);926927/*928* We should have multiple callers for a particular channel,929* an interrupt doesn't affect this pointer nor the descriptor,930* so no locking should be needed.931*/932dp = ctp->put_ptr;933934/*935* If the descriptor is valid, we are way ahead of the DMA936* engine, so just return an error condition.937*/938if (dp->dscr_cmd0 & DSCR_CMD0_V)939return 0;940941/* Load up buffer addresses and byte count. */942dp->dscr_dest0 = dscr->dscr_dest0;943dp->dscr_source0 = dscr->dscr_source0;944dp->dscr_dest1 = dscr->dscr_dest1;945dp->dscr_source1 = dscr->dscr_source1;946dp->dscr_cmd1 = dscr->dscr_cmd1;947nbytes = dscr->dscr_cmd1;948/* Allow the caller to specifiy if an interrupt is generated */949dp->dscr_cmd0 &= ~DSCR_CMD0_IE;950dp->dscr_cmd0 |= dscr->dscr_cmd0 | DSCR_CMD0_V;951ctp->chan_ptr->ddma_dbell = 0;952953/* Get next descriptor pointer. */954ctp->put_ptr = phys_to_virt(DSCR_GET_NXTPTR(dp->dscr_nxtptr));955956/* Return something non-zero. */957return nbytes;958}959960961static unsigned long alchemy_dbdma_pm_data[NUM_DBDMA_CHANS + 1][6];962963static int alchemy_dbdma_suspend(void)964{965int i;966void __iomem *addr;967968addr = (void __iomem *)KSEG1ADDR(AU1550_DBDMA_CONF_PHYS_ADDR);969alchemy_dbdma_pm_data[0][0] = __raw_readl(addr + 0x00);970alchemy_dbdma_pm_data[0][1] = __raw_readl(addr + 0x04);971alchemy_dbdma_pm_data[0][2] = __raw_readl(addr + 0x08);972alchemy_dbdma_pm_data[0][3] = __raw_readl(addr + 0x0c);973974/* save channel configurations */975addr = (void __iomem *)KSEG1ADDR(AU1550_DBDMA_PHYS_ADDR);976for (i = 1; i <= NUM_DBDMA_CHANS; i++) {977alchemy_dbdma_pm_data[i][0] = __raw_readl(addr + 0x00);978alchemy_dbdma_pm_data[i][1] = __raw_readl(addr + 0x04);979alchemy_dbdma_pm_data[i][2] = __raw_readl(addr + 0x08);980alchemy_dbdma_pm_data[i][3] = __raw_readl(addr + 0x0c);981alchemy_dbdma_pm_data[i][4] = __raw_readl(addr + 0x10);982alchemy_dbdma_pm_data[i][5] = __raw_readl(addr + 0x14);983984/* halt channel */985__raw_writel(alchemy_dbdma_pm_data[i][0] & ~1, addr + 0x00);986wmb();987while (!(__raw_readl(addr + 0x14) & 1))988wmb();989990addr += 0x100; /* next channel base */991}992/* disable channel interrupts */993addr = (void __iomem *)KSEG1ADDR(AU1550_DBDMA_CONF_PHYS_ADDR);994__raw_writel(0, addr + 0x0c);995wmb();996997return 0;998}9991000static void alchemy_dbdma_resume(void)1001{1002int i;1003void __iomem *addr;10041005addr = (void __iomem *)KSEG1ADDR(AU1550_DBDMA_CONF_PHYS_ADDR);1006__raw_writel(alchemy_dbdma_pm_data[0][0], addr + 0x00);1007__raw_writel(alchemy_dbdma_pm_data[0][1], addr + 0x04);1008__raw_writel(alchemy_dbdma_pm_data[0][2], addr + 0x08);1009__raw_writel(alchemy_dbdma_pm_data[0][3], addr + 0x0c);10101011/* restore channel configurations */1012addr = (void __iomem *)KSEG1ADDR(AU1550_DBDMA_PHYS_ADDR);1013for (i = 1; i <= NUM_DBDMA_CHANS; i++) {1014__raw_writel(alchemy_dbdma_pm_data[i][0], addr + 0x00);1015__raw_writel(alchemy_dbdma_pm_data[i][1], addr + 0x04);1016__raw_writel(alchemy_dbdma_pm_data[i][2], addr + 0x08);1017__raw_writel(alchemy_dbdma_pm_data[i][3], addr + 0x0c);1018__raw_writel(alchemy_dbdma_pm_data[i][4], addr + 0x10);1019__raw_writel(alchemy_dbdma_pm_data[i][5], addr + 0x14);1020wmb();1021addr += 0x100; /* next channel base */1022}1023}10241025static struct syscore_ops alchemy_dbdma_syscore_ops = {1026.suspend = alchemy_dbdma_suspend,1027.resume = alchemy_dbdma_resume,1028};10291030static int __init au1xxx_dbdma_init(void)1031{1032int irq_nr, ret;10331034dbdma_gptr->ddma_config = 0;1035dbdma_gptr->ddma_throttle = 0;1036dbdma_gptr->ddma_inten = 0xffff;1037au_sync();10381039switch (alchemy_get_cputype()) {1040case ALCHEMY_CPU_AU1550:1041irq_nr = AU1550_DDMA_INT;1042break;1043case ALCHEMY_CPU_AU1200:1044irq_nr = AU1200_DDMA_INT;1045break;1046default:1047return -ENODEV;1048}10491050ret = request_irq(irq_nr, dbdma_interrupt, IRQF_DISABLED,1051"Au1xxx dbdma", (void *)dbdma_gptr);1052if (ret)1053printk(KERN_ERR "Cannot grab DBDMA interrupt!\n");1054else {1055dbdma_initialized = 1;1056printk(KERN_INFO "Alchemy DBDMA initialized\n");1057register_syscore_ops(&alchemy_dbdma_syscore_ops);1058}10591060return ret;1061}1062subsys_initcall(au1xxx_dbdma_init);10631064#endif /* defined(CONFIG_SOC_AU1550) || defined(CONFIG_SOC_AU1200) */106510661067