/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 20114* Ben Gray <[email protected]>.5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/systm.h>31#include <sys/bus.h>32#include <sys/kernel.h>33#include <sys/lock.h>34#include <sys/interrupt.h>35#include <sys/module.h>36#include <sys/malloc.h>37#include <sys/mutex.h>38#include <sys/rman.h>39#include <sys/queue.h>40#include <sys/taskqueue.h>41#include <sys/timetc.h>42#include <machine/bus.h>43#include <machine/intr.h>4445#include <dev/ofw/openfirm.h>46#include <dev/ofw/ofw_bus.h>47#include <dev/ofw/ofw_bus_subr.h>4849#include <arm/ti/ti_cpuid.h>50#include <arm/ti/ti_sysc.h>51#include <arm/ti/ti_sdma.h>52#include <arm/ti/ti_sdmareg.h>5354/**55* Kernel functions for using the DMA controller56*57*58* DMA TRANSFERS:59* A DMA transfer block consists of a number of frames (FN). Each frame60* consists of a number of elements, and each element can have a size of 8, 16,61* or 32 bits.62*63* OMAP44xx and newer chips support linked list (aka scatter gather) transfers,64* where a linked list of source/destination pairs can be placed in memory65* for the H/W to process. Earlier chips only allowed you to chain multiple66* channels together. However currently this linked list feature is not67* supported by the driver.68*69*/7071/**72* Data structure per DMA channel.73*74*75*/76struct ti_sdma_channel {77/*78* The configuration registers for the given channel, these are modified79* by the set functions and only written to the actual registers when a80* transaction is started.81*/82uint32_t reg_csdp;83uint32_t reg_ccr;84uint32_t reg_cicr;8586/* Set when one of the configuration registers above change */87uint32_t need_reg_write;8889/* Callback function used when an interrupt is tripped on the given channel */90void (*callback)(unsigned int ch, uint32_t ch_status, void *data);9192/* Callback data passed in the callback ... duh */93void* callback_data;9495};9697/**98* DMA driver context, allocated and stored globally, this driver is not99* intetned to ever be unloaded (see ti_sdma_sc).100*101*/102struct ti_sdma_softc {103device_t sc_dev;104struct resource* sc_irq_res;105struct resource* sc_mem_res;106107/*108* I guess in theory we should have a mutex per DMA channel for register109* modifications. But since we know we are never going to be run on a SMP110* system, we can use just the single lock for all channels.111*/112struct mtx sc_mtx;113114/* Stores the H/W revision read from the registers */115uint32_t sc_hw_rev;116117/*118* Bits in the sc_active_channels data field indicate if the channel has119* been activated.120*/121uint32_t sc_active_channels;122123struct ti_sdma_channel sc_channel[NUM_DMA_CHANNELS];124125};126127static struct ti_sdma_softc *ti_sdma_sc = NULL;128129/**130* Macros for driver mutex locking131*/132#define TI_SDMA_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)133#define TI_SDMA_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)134#define TI_SDMA_LOCK_INIT(_sc) \135mtx_init(&_sc->sc_mtx, device_get_nameunit(_sc->sc_dev), \136"ti_sdma", MTX_SPIN)137#define TI_SDMA_LOCK_DESTROY(_sc) mtx_destroy(&_sc->sc_mtx);138#define TI_SDMA_ASSERT_LOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_OWNED);139#define TI_SDMA_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->sc_mtx, MA_NOTOWNED);140141/**142* Function prototypes143*144*/145static void ti_sdma_intr(void *);146147/**148* ti_sdma_read_4 - reads a 32-bit value from one of the DMA registers149* @sc: DMA device context150* @off: The offset of a register from the DMA register address range151*152*153* RETURNS:154* 32-bit value read from the register.155*/156static inline uint32_t157ti_sdma_read_4(struct ti_sdma_softc *sc, bus_size_t off)158{159return bus_read_4(sc->sc_mem_res, off);160}161162/**163* ti_sdma_write_4 - writes a 32-bit value to one of the DMA registers164* @sc: DMA device context165* @off: The offset of a register from the DMA register address range166*167*168* RETURNS:169* 32-bit value read from the register.170*/171static inline void172ti_sdma_write_4(struct ti_sdma_softc *sc, bus_size_t off, uint32_t val)173{174bus_write_4(sc->sc_mem_res, off, val);175}176177/**178* ti_sdma_is_omap3_rev - returns true if H/W is from OMAP3 series179* @sc: DMA device context180*181*/182static inline int183ti_sdma_is_omap3_rev(struct ti_sdma_softc *sc)184{185return (sc->sc_hw_rev == DMA4_OMAP3_REV);186}187188/**189* ti_sdma_is_omap4_rev - returns true if H/W is from OMAP4 series190* @sc: DMA device context191*192*/193static inline int194ti_sdma_is_omap4_rev(struct ti_sdma_softc *sc)195{196return (sc->sc_hw_rev == DMA4_OMAP4_REV);197}198199/**200* ti_sdma_intr - interrupt handler for all 4 DMA IRQs201* @arg: ignored202*203* Called when any of the four DMA IRQs are triggered.204*205* LOCKING:206* DMA registers protected by internal mutex207*208* RETURNS:209* nothing210*/211static void212ti_sdma_intr(void *arg)213{214struct ti_sdma_softc *sc = ti_sdma_sc;215uint32_t intr;216uint32_t csr;217unsigned int ch, j;218struct ti_sdma_channel* channel;219220TI_SDMA_LOCK(sc);221222for (j = 0; j < NUM_DMA_IRQS; j++) {223/* Get the flag interrupts (enabled) */224intr = ti_sdma_read_4(sc, DMA4_IRQSTATUS_L(j));225intr &= ti_sdma_read_4(sc, DMA4_IRQENABLE_L(j));226if (intr == 0x00000000)227continue;228229/* Loop through checking the status bits */230for (ch = 0; ch < NUM_DMA_CHANNELS; ch++) {231if (intr & (1 << ch)) {232channel = &sc->sc_channel[ch];233234/* Read the CSR regsiter and verify we don't have a spurious IRQ */235csr = ti_sdma_read_4(sc, DMA4_CSR(ch));236if (csr == 0) {237device_printf(sc->sc_dev, "Spurious DMA IRQ for channel "238"%d\n", ch);239continue;240}241242/* Sanity check this channel is active */243if ((sc->sc_active_channels & (1 << ch)) == 0) {244device_printf(sc->sc_dev, "IRQ %d for a non-activated "245"channel %d\n", j, ch);246continue;247}248249/* Check the status error codes */250if (csr & DMA4_CSR_DROP)251device_printf(sc->sc_dev, "Synchronization event drop "252"occurred during the transfer on channel %u\n",253ch);254if (csr & DMA4_CSR_SECURE_ERR)255device_printf(sc->sc_dev, "Secure transaction error event "256"on channel %u\n", ch);257if (csr & DMA4_CSR_MISALIGNED_ADRS_ERR)258device_printf(sc->sc_dev, "Misaligned address error event "259"on channel %u\n", ch);260if (csr & DMA4_CSR_TRANS_ERR) {261device_printf(sc->sc_dev, "Transaction error event on "262"channel %u\n", ch);263/*264* Apparently according to linux code, there is an errata265* that says the channel is not disabled upon this error.266* They explicitly disable the channel here .. since I267* haven't seen the errata, I'm going to ignore for now.268*/269}270271/* Clear the status flags for the IRQ */272ti_sdma_write_4(sc, DMA4_CSR(ch), DMA4_CSR_CLEAR_MASK);273ti_sdma_write_4(sc, DMA4_IRQSTATUS_L(j), (1 << ch));274275/* Call the callback for the given channel */276if (channel->callback)277channel->callback(ch, csr, channel->callback_data);278}279}280}281282TI_SDMA_UNLOCK(sc);283284return;285}286287/**288* ti_sdma_activate_channel - activates a DMA channel289* @ch: upon return contains the channel allocated290* @callback: a callback function to associate with the channel291* @data: optional data supplied when the callback is called292*293* Simply activates a channel be enabling and writing default values to the294* channel's register set. It doesn't start a transaction, just populates the295* internal data structures and sets defaults.296*297* Note this function doesn't enable interrupts, for that you need to call298* ti_sdma_enable_channel_irq(). If not using IRQ to detect the end of the299* transfer, you can use ti_sdma_status_poll() to detect a change in the300* status.301*302* A channel must be activated before any of the other DMA functions can be303* called on it.304*305* LOCKING:306* DMA registers protected by internal mutex307*308* RETURNS:309* 0 on success, otherwise an error code310*/311int312ti_sdma_activate_channel(unsigned int *ch,313void (*callback)(unsigned int ch, uint32_t status, void *data),314void *data)315{316struct ti_sdma_softc *sc = ti_sdma_sc;317struct ti_sdma_channel *channel = NULL;318uint32_t addr;319unsigned int i;320321/* Sanity check */322if (sc == NULL)323return (ENOMEM);324325if (ch == NULL)326return (EINVAL);327328TI_SDMA_LOCK(sc);329330/* Check to see if all channels are in use */331if (sc->sc_active_channels == 0xffffffff) {332TI_SDMA_UNLOCK(sc);333return (ENOMEM);334}335336/* Find the first non-active channel */337for (i = 0; i < NUM_DMA_CHANNELS; i++) {338if (!(sc->sc_active_channels & (0x1 << i))) {339sc->sc_active_channels |= (0x1 << i);340*ch = i;341break;342}343}344345/* Get the channel struct and populate the fields */346channel = &sc->sc_channel[*ch];347348channel->callback = callback;349channel->callback_data = data;350351channel->need_reg_write = 1;352353/* Set the default configuration for the DMA channel */354channel->reg_csdp = DMA4_CSDP_DATA_TYPE(0x2)355| DMA4_CSDP_SRC_BURST_MODE(0)356| DMA4_CSDP_DST_BURST_MODE(0)357| DMA4_CSDP_SRC_ENDIANISM(0)358| DMA4_CSDP_DST_ENDIANISM(0)359| DMA4_CSDP_WRITE_MODE(0)360| DMA4_CSDP_SRC_PACKED(0)361| DMA4_CSDP_DST_PACKED(0);362363channel->reg_ccr = DMA4_CCR_DST_ADDRESS_MODE(1)364| DMA4_CCR_SRC_ADDRESS_MODE(1)365| DMA4_CCR_READ_PRIORITY(0)366| DMA4_CCR_WRITE_PRIORITY(0)367| DMA4_CCR_SYNC_TRIGGER(0)368| DMA4_CCR_FRAME_SYNC(0)369| DMA4_CCR_BLOCK_SYNC(0);370371channel->reg_cicr = DMA4_CICR_TRANS_ERR_IE372| DMA4_CICR_SECURE_ERR_IE373| DMA4_CICR_SUPERVISOR_ERR_IE374| DMA4_CICR_MISALIGNED_ADRS_ERR_IE;375376/* Clear all the channel registers, this should abort any transaction */377for (addr = DMA4_CCR(*ch); addr <= DMA4_COLOR(*ch); addr += 4)378ti_sdma_write_4(sc, addr, 0x00000000);379380TI_SDMA_UNLOCK(sc);381382return 0;383}384385/**386* ti_sdma_deactivate_channel - deactivates a channel387* @ch: the channel to deactivate388*389*390*391* LOCKING:392* DMA registers protected by internal mutex393*394* RETURNS:395* EH_HANDLED or EH_NOT_HANDLED396*/397int398ti_sdma_deactivate_channel(unsigned int ch)399{400struct ti_sdma_softc *sc = ti_sdma_sc;401unsigned int j;402unsigned int addr;403404/* Sanity check */405if (sc == NULL)406return (ENOMEM);407408TI_SDMA_LOCK(sc);409410/* First check if the channel is currently active */411if ((sc->sc_active_channels & (1 << ch)) == 0) {412TI_SDMA_UNLOCK(sc);413return (EBUSY);414}415416/* Mark the channel as inactive */417sc->sc_active_channels &= ~(1 << ch);418419/* Disable all DMA interrupts for the channel. */420ti_sdma_write_4(sc, DMA4_CICR(ch), 0);421422/* Make sure the DMA transfer is stopped. */423ti_sdma_write_4(sc, DMA4_CCR(ch), 0);424425/* Clear the CSR register and IRQ status register */426ti_sdma_write_4(sc, DMA4_CSR(ch), DMA4_CSR_CLEAR_MASK);427for (j = 0; j < NUM_DMA_IRQS; j++) {428ti_sdma_write_4(sc, DMA4_IRQSTATUS_L(j), (1 << ch));429}430431/* Clear all the channel registers, this should abort any transaction */432for (addr = DMA4_CCR(ch); addr <= DMA4_COLOR(ch); addr += 4)433ti_sdma_write_4(sc, addr, 0x00000000);434435TI_SDMA_UNLOCK(sc);436437return 0;438}439440/**441* ti_sdma_disable_channel_irq - disables IRQ's on the given channel442* @ch: the channel to disable IRQ's on443*444* Disable interrupt generation for the given channel.445*446* LOCKING:447* DMA registers protected by internal mutex448*449* RETURNS:450* EH_HANDLED or EH_NOT_HANDLED451*/452int453ti_sdma_disable_channel_irq(unsigned int ch)454{455struct ti_sdma_softc *sc = ti_sdma_sc;456uint32_t irq_enable;457unsigned int j;458459/* Sanity check */460if (sc == NULL)461return (ENOMEM);462463TI_SDMA_LOCK(sc);464465if ((sc->sc_active_channels & (1 << ch)) == 0) {466TI_SDMA_UNLOCK(sc);467return (EINVAL);468}469470/* Disable all the individual error conditions */471sc->sc_channel[ch].reg_cicr = 0x0000;472ti_sdma_write_4(sc, DMA4_CICR(ch), 0x0000);473474/* Disable the channel interrupt enable */475for (j = 0; j < NUM_DMA_IRQS; j++) {476irq_enable = ti_sdma_read_4(sc, DMA4_IRQENABLE_L(j));477irq_enable &= ~(1 << ch);478479ti_sdma_write_4(sc, DMA4_IRQENABLE_L(j), irq_enable);480}481482/* Indicate the registers need to be rewritten on the next transaction */483sc->sc_channel[ch].need_reg_write = 1;484485TI_SDMA_UNLOCK(sc);486487return (0);488}489490/**491* ti_sdma_disable_channel_irq - enables IRQ's on the given channel492* @ch: the channel to enable IRQ's on493* @flags: bitmask of interrupt types to enable494*495* Flags can be a bitmask of the following options:496* DMA_IRQ_FLAG_DROP497* DMA_IRQ_FLAG_HALF_FRAME_COMPL498* DMA_IRQ_FLAG_FRAME_COMPL499* DMA_IRQ_FLAG_START_LAST_FRAME500* DMA_IRQ_FLAG_BLOCK_COMPL501* DMA_IRQ_FLAG_ENDOF_PKT502* DMA_IRQ_FLAG_DRAIN503*504*505* LOCKING:506* DMA registers protected by internal mutex507*508* RETURNS:509* EH_HANDLED or EH_NOT_HANDLED510*/511int512ti_sdma_enable_channel_irq(unsigned int ch, uint32_t flags)513{514struct ti_sdma_softc *sc = ti_sdma_sc;515uint32_t irq_enable;516517/* Sanity check */518if (sc == NULL)519return (ENOMEM);520521TI_SDMA_LOCK(sc);522523if ((sc->sc_active_channels & (1 << ch)) == 0) {524TI_SDMA_UNLOCK(sc);525return (EINVAL);526}527528/* Always enable the error interrupts if we have interrupts enabled */529flags |= DMA4_CICR_TRANS_ERR_IE | DMA4_CICR_SECURE_ERR_IE |530DMA4_CICR_SUPERVISOR_ERR_IE | DMA4_CICR_MISALIGNED_ADRS_ERR_IE;531532sc->sc_channel[ch].reg_cicr = flags;533534/* Write the values to the register */535ti_sdma_write_4(sc, DMA4_CICR(ch), flags);536537/* Enable the channel interrupt enable */538irq_enable = ti_sdma_read_4(sc, DMA4_IRQENABLE_L(0));539irq_enable |= (1 << ch);540541ti_sdma_write_4(sc, DMA4_IRQENABLE_L(0), irq_enable);542543/* Indicate the registers need to be rewritten on the next transaction */544sc->sc_channel[ch].need_reg_write = 1;545546TI_SDMA_UNLOCK(sc);547548return (0);549}550551/**552* ti_sdma_get_channel_status - returns the status of a given channel553* @ch: the channel number to get the status of554* @status: upon return will contain the status bitmask, see below for possible555* values.556*557* DMA_STATUS_DROP558* DMA_STATUS_HALF559* DMA_STATUS_FRAME560* DMA_STATUS_LAST561* DMA_STATUS_BLOCK562* DMA_STATUS_SYNC563* DMA_STATUS_PKT564* DMA_STATUS_TRANS_ERR565* DMA_STATUS_SECURE_ERR566* DMA_STATUS_SUPERVISOR_ERR567* DMA_STATUS_MISALIGNED_ADRS_ERR568* DMA_STATUS_DRAIN_END569*570*571* LOCKING:572* DMA registers protected by internal mutex573*574* RETURNS:575* EH_HANDLED or EH_NOT_HANDLED576*/577int578ti_sdma_get_channel_status(unsigned int ch, uint32_t *status)579{580struct ti_sdma_softc *sc = ti_sdma_sc;581uint32_t csr;582583/* Sanity check */584if (sc == NULL)585return (ENOMEM);586587TI_SDMA_LOCK(sc);588589if ((sc->sc_active_channels & (1 << ch)) == 0) {590TI_SDMA_UNLOCK(sc);591return (EINVAL);592}593594TI_SDMA_UNLOCK(sc);595596csr = ti_sdma_read_4(sc, DMA4_CSR(ch));597598if (status != NULL)599*status = csr;600601return (0);602}603604/**605* ti_sdma_start_xfer - starts a DMA transfer606* @ch: the channel number to set the endianness of607* @src_paddr: the source phsyical address608* @dst_paddr: the destination phsyical address609* @frmcnt: the number of frames per block610* @elmcnt: the number of elements in a frame, an element is either an 8, 16611* or 32-bit value as defined by ti_sdma_set_xfer_burst()612*613*614* LOCKING:615* DMA registers protected by internal mutex616*617* RETURNS:618* EH_HANDLED or EH_NOT_HANDLED619*/620int621ti_sdma_start_xfer(unsigned int ch, unsigned int src_paddr,622unsigned long dst_paddr,623unsigned int frmcnt, unsigned int elmcnt)624{625struct ti_sdma_softc *sc = ti_sdma_sc;626struct ti_sdma_channel *channel;627uint32_t ccr;628629/* Sanity check */630if (sc == NULL)631return (ENOMEM);632633TI_SDMA_LOCK(sc);634635if ((sc->sc_active_channels & (1 << ch)) == 0) {636TI_SDMA_UNLOCK(sc);637return (EINVAL);638}639640channel = &sc->sc_channel[ch];641642/* a) Write the CSDP register */643ti_sdma_write_4(sc, DMA4_CSDP(ch),644channel->reg_csdp | DMA4_CSDP_WRITE_MODE(1));645646/* b) Set the number of element per frame CEN[23:0] */647ti_sdma_write_4(sc, DMA4_CEN(ch), elmcnt);648649/* c) Set the number of frame per block CFN[15:0] */650ti_sdma_write_4(sc, DMA4_CFN(ch), frmcnt);651652/* d) Set the Source/dest start address index CSSA[31:0]/CDSA[31:0] */653ti_sdma_write_4(sc, DMA4_CSSA(ch), src_paddr);654ti_sdma_write_4(sc, DMA4_CDSA(ch), dst_paddr);655656/* e) Write the CCR register */657ti_sdma_write_4(sc, DMA4_CCR(ch), channel->reg_ccr);658659/* f) - Set the source element index increment CSEI[15:0] */660ti_sdma_write_4(sc, DMA4_CSE(ch), 0x0001);661662/* - Set the source frame index increment CSFI[15:0] */663ti_sdma_write_4(sc, DMA4_CSF(ch), 0x0001);664665/* - Set the destination element index increment CDEI[15:0]*/666ti_sdma_write_4(sc, DMA4_CDE(ch), 0x0001);667668/* - Set the destination frame index increment CDFI[31:0] */669ti_sdma_write_4(sc, DMA4_CDF(ch), 0x0001);670671/* Clear the status register */672ti_sdma_write_4(sc, DMA4_CSR(ch), 0x1FFE);673674/* Write the start-bit and away we go */675ccr = ti_sdma_read_4(sc, DMA4_CCR(ch));676ccr |= (1 << 7);677ti_sdma_write_4(sc, DMA4_CCR(ch), ccr);678679/* Clear the reg write flag */680channel->need_reg_write = 0;681682TI_SDMA_UNLOCK(sc);683684return (0);685}686687/**688* ti_sdma_start_xfer_packet - starts a packet DMA transfer689* @ch: the channel number to use for the transfer690* @src_paddr: the source physical address691* @dst_paddr: the destination physical address692* @frmcnt: the number of frames to transfer693* @elmcnt: the number of elements in a frame, an element is either an 8, 16694* or 32-bit value as defined by ti_sdma_set_xfer_burst()695* @pktsize: the number of elements in each transfer packet696*697* The @frmcnt and @elmcnt define the overall number of bytes to transfer,698* typically @frmcnt is 1 and @elmcnt contains the total number of elements.699* @pktsize is the size of each individual packet, there might be multiple700* packets per transfer. i.e. for the following with element size of 32-bits701*702* frmcnt = 1, elmcnt = 512, pktsize = 128703*704* Total transfer bytes = 1 * 512 = 512 elements or 2048 bytes705* Packets transferred = 128 / 512 = 4706*707*708* LOCKING:709* DMA registers protected by internal mutex710*711* RETURNS:712* EH_HANDLED or EH_NOT_HANDLED713*/714int715ti_sdma_start_xfer_packet(unsigned int ch, unsigned int src_paddr,716unsigned long dst_paddr, unsigned int frmcnt,717unsigned int elmcnt, unsigned int pktsize)718{719struct ti_sdma_softc *sc = ti_sdma_sc;720struct ti_sdma_channel *channel;721uint32_t ccr;722723/* Sanity check */724if (sc == NULL)725return (ENOMEM);726727TI_SDMA_LOCK(sc);728729if ((sc->sc_active_channels & (1 << ch)) == 0) {730TI_SDMA_UNLOCK(sc);731return (EINVAL);732}733734channel = &sc->sc_channel[ch];735736/* a) Write the CSDP register */737if (channel->need_reg_write)738ti_sdma_write_4(sc, DMA4_CSDP(ch),739channel->reg_csdp | DMA4_CSDP_WRITE_MODE(1));740741/* b) Set the number of elements to transfer CEN[23:0] */742ti_sdma_write_4(sc, DMA4_CEN(ch), elmcnt);743744/* c) Set the number of frames to transfer CFN[15:0] */745ti_sdma_write_4(sc, DMA4_CFN(ch), frmcnt);746747/* d) Set the Source/dest start address index CSSA[31:0]/CDSA[31:0] */748ti_sdma_write_4(sc, DMA4_CSSA(ch), src_paddr);749ti_sdma_write_4(sc, DMA4_CDSA(ch), dst_paddr);750751/* e) Write the CCR register */752ti_sdma_write_4(sc, DMA4_CCR(ch),753channel->reg_ccr | DMA4_CCR_PACKET_TRANS);754755/* f) - Set the source element index increment CSEI[15:0] */756ti_sdma_write_4(sc, DMA4_CSE(ch), 0x0001);757758/* - Set the packet size, this is dependent on the sync source */759if (channel->reg_ccr & DMA4_CCR_SEL_SRC_DST_SYNC(1))760ti_sdma_write_4(sc, DMA4_CSF(ch), pktsize);761else762ti_sdma_write_4(sc, DMA4_CDF(ch), pktsize);763764/* - Set the destination frame index increment CDFI[31:0] */765ti_sdma_write_4(sc, DMA4_CDE(ch), 0x0001);766767/* Clear the status register */768ti_sdma_write_4(sc, DMA4_CSR(ch), 0x1FFE);769770/* Write the start-bit and away we go */771ccr = ti_sdma_read_4(sc, DMA4_CCR(ch));772ccr |= (1 << 7);773ti_sdma_write_4(sc, DMA4_CCR(ch), ccr);774775/* Clear the reg write flag */776channel->need_reg_write = 0;777778TI_SDMA_UNLOCK(sc);779780return (0);781}782783/**784* ti_sdma_stop_xfer - stops any currently active transfers785* @ch: the channel number to set the endianness of786*787* This function call is effectively a NOP if no transaction is in progress.788*789* LOCKING:790* DMA registers protected by internal mutex791*792* RETURNS:793* EH_HANDLED or EH_NOT_HANDLED794*/795int796ti_sdma_stop_xfer(unsigned int ch)797{798struct ti_sdma_softc *sc = ti_sdma_sc;799unsigned int j;800801/* Sanity check */802if (sc == NULL)803return (ENOMEM);804805TI_SDMA_LOCK(sc);806807if ((sc->sc_active_channels & (1 << ch)) == 0) {808TI_SDMA_UNLOCK(sc);809return (EINVAL);810}811812/* Disable all DMA interrupts for the channel. */813ti_sdma_write_4(sc, DMA4_CICR(ch), 0);814815/* Make sure the DMA transfer is stopped. */816ti_sdma_write_4(sc, DMA4_CCR(ch), 0);817818/* Clear the CSR register and IRQ status register */819ti_sdma_write_4(sc, DMA4_CSR(ch), DMA4_CSR_CLEAR_MASK);820for (j = 0; j < NUM_DMA_IRQS; j++) {821ti_sdma_write_4(sc, DMA4_IRQSTATUS_L(j), (1 << ch));822}823824/* Configuration registers need to be re-written on the next xfer */825sc->sc_channel[ch].need_reg_write = 1;826827TI_SDMA_UNLOCK(sc);828829return (0);830}831832/**833* ti_sdma_set_xfer_endianess - sets the endianness of subsequent transfers834* @ch: the channel number to set the endianness of835* @src: the source endianness (either DMA_ENDIAN_LITTLE or DMA_ENDIAN_BIG)836* @dst: the destination endianness (either DMA_ENDIAN_LITTLE or DMA_ENDIAN_BIG)837*838*839* LOCKING:840* DMA registers protected by internal mutex841*842* RETURNS:843* EH_HANDLED or EH_NOT_HANDLED844*/845int846ti_sdma_set_xfer_endianess(unsigned int ch, unsigned int src, unsigned int dst)847{848struct ti_sdma_softc *sc = ti_sdma_sc;849850/* Sanity check */851if (sc == NULL)852return (ENOMEM);853854TI_SDMA_LOCK(sc);855856if ((sc->sc_active_channels & (1 << ch)) == 0) {857TI_SDMA_UNLOCK(sc);858return (EINVAL);859}860861sc->sc_channel[ch].reg_csdp &= ~DMA4_CSDP_SRC_ENDIANISM(1);862sc->sc_channel[ch].reg_csdp |= DMA4_CSDP_SRC_ENDIANISM(src);863864sc->sc_channel[ch].reg_csdp &= ~DMA4_CSDP_DST_ENDIANISM(1);865sc->sc_channel[ch].reg_csdp |= DMA4_CSDP_DST_ENDIANISM(dst);866867sc->sc_channel[ch].need_reg_write = 1;868869TI_SDMA_UNLOCK(sc);870871return 0;872}873874/**875* ti_sdma_set_xfer_burst - sets the source and destination element size876* @ch: the channel number to set the burst settings of877* @src: the source endianness (either DMA_BURST_NONE, DMA_BURST_16, DMA_BURST_32878* or DMA_BURST_64)879* @dst: the destination endianness (either DMA_BURST_NONE, DMA_BURST_16,880* DMA_BURST_32 or DMA_BURST_64)881*882* This function sets the size of the elements for all subsequent transfers.883*884* LOCKING:885* DMA registers protected by internal mutex886*887* RETURNS:888* EH_HANDLED or EH_NOT_HANDLED889*/890int891ti_sdma_set_xfer_burst(unsigned int ch, unsigned int src, unsigned int dst)892{893struct ti_sdma_softc *sc = ti_sdma_sc;894895/* Sanity check */896if (sc == NULL)897return (ENOMEM);898899TI_SDMA_LOCK(sc);900901if ((sc->sc_active_channels & (1 << ch)) == 0) {902TI_SDMA_UNLOCK(sc);903return (EINVAL);904}905906sc->sc_channel[ch].reg_csdp &= ~DMA4_CSDP_SRC_BURST_MODE(0x3);907sc->sc_channel[ch].reg_csdp |= DMA4_CSDP_SRC_BURST_MODE(src);908909sc->sc_channel[ch].reg_csdp &= ~DMA4_CSDP_DST_BURST_MODE(0x3);910sc->sc_channel[ch].reg_csdp |= DMA4_CSDP_DST_BURST_MODE(dst);911912sc->sc_channel[ch].need_reg_write = 1;913914TI_SDMA_UNLOCK(sc);915916return 0;917}918919/**920* ti_sdma_set_xfer_data_type - driver attach function921* @ch: the channel number to set the endianness of922* @type: the xfer data type (either DMA_DATA_8BITS_SCALAR, DMA_DATA_16BITS_SCALAR923* or DMA_DATA_32BITS_SCALAR)924*925*926* LOCKING:927* DMA registers protected by internal mutex928*929* RETURNS:930* EH_HANDLED or EH_NOT_HANDLED931*/932int933ti_sdma_set_xfer_data_type(unsigned int ch, unsigned int type)934{935struct ti_sdma_softc *sc = ti_sdma_sc;936937/* Sanity check */938if (sc == NULL)939return (ENOMEM);940941TI_SDMA_LOCK(sc);942943if ((sc->sc_active_channels & (1 << ch)) == 0) {944TI_SDMA_UNLOCK(sc);945return (EINVAL);946}947948sc->sc_channel[ch].reg_csdp &= ~DMA4_CSDP_DATA_TYPE(0x3);949sc->sc_channel[ch].reg_csdp |= DMA4_CSDP_DATA_TYPE(type);950951sc->sc_channel[ch].need_reg_write = 1;952953TI_SDMA_UNLOCK(sc);954955return 0;956}957958/**959* ti_sdma_set_callback - driver attach function960* @dev: dma device handle961*962*963*964* LOCKING:965* DMA registers protected by internal mutex966*967* RETURNS:968* EH_HANDLED or EH_NOT_HANDLED969*/970int971ti_sdma_set_callback(unsigned int ch,972void (*callback)(unsigned int ch, uint32_t status, void *data),973void *data)974{975struct ti_sdma_softc *sc = ti_sdma_sc;976977/* Sanity check */978if (sc == NULL)979return (ENOMEM);980981TI_SDMA_LOCK(sc);982983if ((sc->sc_active_channels & (1 << ch)) == 0) {984TI_SDMA_UNLOCK(sc);985return (EINVAL);986}987988sc->sc_channel[ch].callback = callback;989sc->sc_channel[ch].callback_data = data;990991sc->sc_channel[ch].need_reg_write = 1;992993TI_SDMA_UNLOCK(sc);994995return 0;996}997998/**999* ti_sdma_sync_params - sets channel sync settings1000* @ch: the channel number to set the sync on1001* @trigger: the number of the sync trigger, this depends on what other H/W1002* module is triggering/receiving the DMA transactions1003* @mode: flags describing the sync mode to use, it may have one or more of1004* the following bits set; TI_SDMA_SYNC_FRAME,1005* TI_SDMA_SYNC_BLOCK, TI_SDMA_SYNC_TRIG_ON_SRC.1006*1007*1008*1009* LOCKING:1010* DMA registers protected by internal mutex1011*1012* RETURNS:1013* EH_HANDLED or EH_NOT_HANDLED1014*/1015int1016ti_sdma_sync_params(unsigned int ch, unsigned int trigger, unsigned int mode)1017{1018struct ti_sdma_softc *sc = ti_sdma_sc;1019uint32_t ccr;10201021/* Sanity check */1022if (sc == NULL)1023return (ENOMEM);10241025TI_SDMA_LOCK(sc);10261027if ((sc->sc_active_channels & (1 << ch)) == 0) {1028TI_SDMA_UNLOCK(sc);1029return (EINVAL);1030}10311032ccr = sc->sc_channel[ch].reg_ccr;10331034ccr &= ~DMA4_CCR_SYNC_TRIGGER(0x7F);1035ccr |= DMA4_CCR_SYNC_TRIGGER(trigger + 1);10361037if (mode & TI_SDMA_SYNC_FRAME)1038ccr |= DMA4_CCR_FRAME_SYNC(1);1039else1040ccr &= ~DMA4_CCR_FRAME_SYNC(1);10411042if (mode & TI_SDMA_SYNC_BLOCK)1043ccr |= DMA4_CCR_BLOCK_SYNC(1);1044else1045ccr &= ~DMA4_CCR_BLOCK_SYNC(1);10461047if (mode & TI_SDMA_SYNC_TRIG_ON_SRC)1048ccr |= DMA4_CCR_SEL_SRC_DST_SYNC(1);1049else1050ccr &= ~DMA4_CCR_SEL_SRC_DST_SYNC(1);10511052sc->sc_channel[ch].reg_ccr = ccr;10531054sc->sc_channel[ch].need_reg_write = 1;10551056TI_SDMA_UNLOCK(sc);10571058return 0;1059}10601061/**1062* ti_sdma_set_addr_mode - driver attach function1063* @ch: the channel number to set the endianness of1064* @rd_mode: the xfer source addressing mode (either DMA_ADDR_CONSTANT,1065* DMA_ADDR_POST_INCREMENT, DMA_ADDR_SINGLE_INDEX or1066* DMA_ADDR_DOUBLE_INDEX)1067* @wr_mode: the xfer destination addressing mode (either DMA_ADDR_CONSTANT,1068* DMA_ADDR_POST_INCREMENT, DMA_ADDR_SINGLE_INDEX or1069* DMA_ADDR_DOUBLE_INDEX)1070*1071*1072* LOCKING:1073* DMA registers protected by internal mutex1074*1075* RETURNS:1076* EH_HANDLED or EH_NOT_HANDLED1077*/1078int1079ti_sdma_set_addr_mode(unsigned int ch, unsigned int src_mode,1080unsigned int dst_mode)1081{1082struct ti_sdma_softc *sc = ti_sdma_sc;1083uint32_t ccr;10841085/* Sanity check */1086if (sc == NULL)1087return (ENOMEM);10881089TI_SDMA_LOCK(sc);10901091if ((sc->sc_active_channels & (1 << ch)) == 0) {1092TI_SDMA_UNLOCK(sc);1093return (EINVAL);1094}10951096ccr = sc->sc_channel[ch].reg_ccr;10971098ccr &= ~DMA4_CCR_SRC_ADDRESS_MODE(0x3);1099ccr |= DMA4_CCR_SRC_ADDRESS_MODE(src_mode);11001101ccr &= ~DMA4_CCR_DST_ADDRESS_MODE(0x3);1102ccr |= DMA4_CCR_DST_ADDRESS_MODE(dst_mode);11031104sc->sc_channel[ch].reg_ccr = ccr;11051106sc->sc_channel[ch].need_reg_write = 1;11071108TI_SDMA_UNLOCK(sc);11091110return 0;1111}11121113/**1114* ti_sdma_probe - driver probe function1115* @dev: dma device handle1116*1117*1118*1119* RETURNS:1120* Always returns 0.1121*/1122static int1123ti_sdma_probe(device_t dev)1124{11251126if (!ofw_bus_status_okay(dev))1127return (ENXIO);11281129if (!ofw_bus_is_compatible(dev, "ti,omap4430-sdma"))1130return (ENXIO);11311132device_set_desc(dev, "TI sDMA Controller");1133return (0);1134}11351136/**1137* ti_sdma_attach - driver attach function1138* @dev: dma device handle1139*1140* Initialises memory mapping/pointers to the DMA register set and requests1141* IRQs. This is effectively the setup function for the driver.1142*1143* RETURNS:1144* 0 on success or a negative error code failure.1145*/1146static int1147ti_sdma_attach(device_t dev)1148{1149struct ti_sdma_softc *sc = device_get_softc(dev);1150unsigned int timeout;1151unsigned int i;1152int rid;1153void *ihl;1154int err;11551156/* Setup the basics */1157sc->sc_dev = dev;11581159/* No channels active at the moment */1160sc->sc_active_channels = 0x00000000;11611162/* Mutex to protect the shared data structures */1163TI_SDMA_LOCK_INIT(sc);11641165/* Get the memory resource for the register mapping */1166rid = 0;1167sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);1168if (sc->sc_mem_res == NULL)1169panic("%s: Cannot map registers", device_get_name(dev));11701171/* Enable the interface and functional clocks */1172ti_sysc_clock_enable(device_get_parent(dev));11731174/* Read the sDMA revision register and sanity check it's known */1175sc->sc_hw_rev = ti_sdma_read_4(sc,1176ti_sysc_get_rev_address_offset_host(device_get_parent(dev)));1177device_printf(dev, "sDMA revision %08x\n", sc->sc_hw_rev);11781179if (!ti_sdma_is_omap4_rev(sc) && !ti_sdma_is_omap3_rev(sc)) {1180device_printf(sc->sc_dev, "error - unknown sDMA H/W revision\n");1181return (EINVAL);1182}11831184/* Disable all interrupts */1185for (i = 0; i < NUM_DMA_IRQS; i++) {1186ti_sdma_write_4(sc, DMA4_IRQENABLE_L(i), 0x00000000);1187}11881189/* Soft-reset is only supported on pre-OMAP44xx devices */1190if (ti_sdma_is_omap3_rev(sc)) {1191/* Soft-reset */1192ti_sdma_write_4(sc, DMA4_OCP_SYSCONFIG, 0x0002);11931194/* Set the timeout to 100ms*/1195timeout = (hz < 10) ? 1 : ((100 * hz) / 1000);11961197/* Wait for DMA reset to complete */1198while ((ti_sdma_read_4(sc, DMA4_SYSSTATUS) & 0x1) == 0x0) {1199/* Sleep for a tick */1200pause("DMARESET", 1);12011202if (timeout-- == 0) {1203device_printf(sc->sc_dev, "sDMA reset operation timed out\n");1204return (EINVAL);1205}1206}1207}12081209/*1210* Install interrupt handlers for the for possible interrupts. Any channel1211* can trip one of the four IRQs1212*/1213rid = 0;1214sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,1215RF_ACTIVE | RF_SHAREABLE);1216if (sc->sc_irq_res == NULL)1217panic("Unable to setup the dma irq handler.\n");12181219err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,1220NULL, ti_sdma_intr, NULL, &ihl);1221if (err)1222panic("%s: Cannot register IRQ", device_get_name(dev));12231224/* Store the DMA structure globally ... this driver should never be unloaded */1225ti_sdma_sc = sc;12261227return (0);1228}12291230static device_method_t ti_sdma_methods[] = {1231DEVMETHOD(device_probe, ti_sdma_probe),1232DEVMETHOD(device_attach, ti_sdma_attach),1233{0, 0},1234};12351236static driver_t ti_sdma_driver = {1237"ti_sdma",1238ti_sdma_methods,1239sizeof(struct ti_sdma_softc),1240};12411242DRIVER_MODULE(ti_sdma, simplebus, ti_sdma_driver, 0, 0);1243MODULE_DEPEND(ti_sdma, ti_sysc, 1, 1, 1);124412451246