Release 4.7 drivers/usb/musb/musbhsdma.c
  
  
/*
 * MUSB OTG driver - support for Mentor's DMA controller
 *
 * Copyright 2005 Mentor Graphics Corporation
 * Copyright (C) 2005-2007 by Texas Instruments
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include "musb_core.h"
#include "musbhsdma.h"
static void dma_channel_release(struct dma_channel *channel);
static void dma_controller_stop(struct musb_dma_controller *controller)
{
	struct musb *musb = controller->private_data;
	struct dma_channel *channel;
	u8 bit;
	if (controller->used_channels != 0) {
		dev_err(musb->controller,
			"Stopping DMA controller while channel active\n");
		for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
			if (controller->used_channels & (1 << bit)) {
				channel = &controller->channel[bit].channel;
				dma_channel_release(channel);
				if (!controller->used_channels)
					break;
			}
		}
	}
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 98 | 97.03% | 2 | 66.67% | 
| sebastian andrzej siewior | sebastian andrzej siewior | 3 | 2.97% | 1 | 33.33% | 
 | Total | 101 | 100.00% | 3 | 100.00% | 
static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
				struct musb_hw_ep *hw_ep, u8 transmit)
{
	struct musb_dma_controller *controller = container_of(c,
			struct musb_dma_controller, controller);
	struct musb_dma_channel *musb_channel = NULL;
	struct dma_channel *channel = NULL;
	u8 bit;
	for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
		if (!(controller->used_channels & (1 << bit))) {
			controller->used_channels |= (1 << bit);
			musb_channel = &(controller->channel[bit]);
			musb_channel->controller = controller;
			musb_channel->idx = bit;
			musb_channel->epnum = hw_ep->epnum;
			musb_channel->transmit = transmit;
			channel = &(musb_channel->channel);
			channel->private_data = musb_channel;
			channel->status = MUSB_DMA_STATUS_FREE;
			channel->max_len = 0x100000;
			/* Tx => mode 1; Rx => mode 0 */
			channel->desired_mode = transmit;
			channel->actual_len = 0;
			break;
		}
	}
	return channel;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 176 | 99.44% | 2 | 66.67% | 
| anil shetty | anil shetty | 1 | 0.56% | 1 | 33.33% | 
 | Total | 177 | 100.00% | 3 | 100.00% | 
static void dma_channel_release(struct dma_channel *channel)
{
	struct musb_dma_channel *musb_channel = channel->private_data;
	channel->actual_len = 0;
	musb_channel->start_addr = 0;
	musb_channel->len = 0;
	musb_channel->controller->used_channels &=
		~(1 << musb_channel->idx);
	channel->status = MUSB_DMA_STATUS_UNKNOWN;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 59 | 100.00% | 2 | 100.00% | 
 | Total | 59 | 100.00% | 2 | 100.00% | 
static void configure_channel(struct dma_channel *channel,
				u16 packet_sz, u8 mode,
				dma_addr_t dma_addr, u32 len)
{
	struct musb_dma_channel *musb_channel = channel->private_data;
	struct musb_dma_controller *controller = musb_channel->controller;
	struct musb *musb = controller->private_data;
	void __iomem *mbase = controller->base;
	u8 bchannel = musb_channel->idx;
	u16 csr = 0;
	dev_dbg(musb->controller, "%p, pkt_sz %d, addr %pad, len %d, mode %d\n",
			channel, packet_sz, &dma_addr, len, mode);
	if (mode) {
		csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
		BUG_ON(len < packet_sz);
	}
	csr |= MUSB_HSDMA_BURSTMODE_INCR16
				<< MUSB_HSDMA_BURSTMODE_SHIFT;
	csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
		| (1 << MUSB_HSDMA_ENABLE_SHIFT)
		| (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
		| (musb_channel->transmit
				? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
				: 0);
	/* address/count */
	musb_write_hsdma_addr(mbase, bchannel, dma_addr);
	musb_write_hsdma_count(mbase, bchannel, len);
	/* control (this should start things) */
	musb_writew(mbase,
		MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
		csr);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 181 | 97.31% | 3 | 50.00% | 
| bryan wu | bryan wu | 2 | 1.08% | 1 | 16.67% | 
| arnd bergmann | arnd bergmann | 2 | 1.08% | 1 | 16.67% | 
| hema kalliguddi | hema kalliguddi | 1 | 0.54% | 1 | 16.67% | 
 | Total | 186 | 100.00% | 6 | 100.00% | 
static int dma_channel_program(struct dma_channel *channel,
				u16 packet_sz, u8 mode,
				dma_addr_t dma_addr, u32 len)
{
	struct musb_dma_channel *musb_channel = channel->private_data;
	struct musb_dma_controller *controller = musb_channel->controller;
	struct musb *musb = controller->private_data;
	dev_dbg(musb->controller, "ep%d-%s pkt_sz %d, dma_addr %pad length %d, mode %d\n",
		musb_channel->epnum,
		musb_channel->transmit ? "Tx" : "Rx",
		packet_sz, &dma_addr, len, mode);
	BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
		channel->status == MUSB_DMA_STATUS_BUSY);
	/* Let targets check/tweak the arguments */
	if (musb->ops->adjust_channel_params) {
		int ret = musb->ops->adjust_channel_params(channel,
			packet_sz, &mode, &dma_addr, &len);
		if (ret)
			return ret;
	}
	/*
         * The DMA engine in RTL1.8 and above cannot handle
         * DMA addresses that are not aligned to a 4 byte boundary.
         * It ends up masking the last two bits of the address
         * programmed in DMA_ADDR.
         *
         * Fail such DMA transfers, so that the backup PIO mode
         * can carry out the transfer
         */
	if ((musb->hwvers >= MUSB_HWVERS_1800) && (dma_addr % 4))
		return false;
	channel->actual_len = 0;
	musb_channel->start_addr = dma_addr;
	musb_channel->len = len;
	musb_channel->max_packet_sz = packet_sz;
	channel->status = MUSB_DMA_STATUS_BUSY;
	configure_channel(channel, packet_sz, mode, dma_addr, len);
	return true;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 120 | 59.41% | 3 | 42.86% | 
| mike frysinger | mike frysinger | 41 | 20.30% | 1 | 14.29% | 
| anand gadiyar | anand gadiyar | 39 | 19.31% | 2 | 28.57% | 
| arnd bergmann | arnd bergmann | 2 | 0.99% | 1 | 14.29% | 
 | Total | 202 | 100.00% | 7 | 100.00% | 
static int dma_channel_abort(struct dma_channel *channel)
{
	struct musb_dma_channel *musb_channel = channel->private_data;
	void __iomem *mbase = musb_channel->controller->base;
	struct musb *musb = musb_channel->controller->private_data;
	u8 bchannel = musb_channel->idx;
	int offset;
	u16 csr;
	if (channel->status == MUSB_DMA_STATUS_BUSY) {
		if (musb_channel->transmit) {
			offset = musb->io.ep_offset(musb_channel->epnum,
						MUSB_TXCSR);
			/*
                         * The programming guide says that we must clear
                         * the DMAENAB bit before the DMAMODE bit...
                         */
			csr = musb_readw(mbase, offset);
			csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
			musb_writew(mbase, offset, csr);
			csr &= ~MUSB_TXCSR_DMAMODE;
			musb_writew(mbase, offset, csr);
		} else {
			offset = musb->io.ep_offset(musb_channel->epnum,
						MUSB_RXCSR);
			csr = musb_readw(mbase, offset);
			csr &= ~(MUSB_RXCSR_AUTOCLEAR |
				 MUSB_RXCSR_DMAENAB |
				 MUSB_RXCSR_DMAMODE);
			musb_writew(mbase, offset, csr);
		}
		musb_writew(mbase,
			MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
			0);
		musb_write_hsdma_addr(mbase, bchannel, 0);
		musb_write_hsdma_count(mbase, bchannel, 0);
		channel->status = MUSB_DMA_STATUS_FREE;
	}
	return 0;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 159 | 72.94% | 2 | 40.00% | 
| sergei shtylyov | sergei shtylyov | 36 | 16.51% | 1 | 20.00% | 
| tony lindgren | tony lindgren | 21 | 9.63% | 1 | 20.00% | 
| bryan wu | bryan wu | 2 | 0.92% | 1 | 20.00% | 
 | Total | 218 | 100.00% | 5 | 100.00% | 
static irqreturn_t dma_controller_irq(int irq, void *private_data)
{
	struct musb_dma_controller *controller = private_data;
	struct musb *musb = controller->private_data;
	struct musb_dma_channel *musb_channel;
	struct dma_channel *channel;
	void __iomem *mbase = controller->base;
	irqreturn_t retval = IRQ_NONE;
	unsigned long flags;
	u8 bchannel;
	u8 int_hsdma;
	u32 addr, count;
	u16 csr;
	spin_lock_irqsave(&musb->lock, flags);
	int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);
#ifdef CONFIG_BLACKFIN
	/* Clear DMA interrupt flags */
	musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
#endif
	if (!int_hsdma) {
		dev_dbg(musb->controller, "spurious DMA irq\n");
		for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
			musb_channel = (struct musb_dma_channel *)
					&(controller->channel[bchannel]);
			channel = &musb_channel->channel;
			if (channel->status == MUSB_DMA_STATUS_BUSY) {
				count = musb_read_hsdma_count(mbase, bchannel);
				if (count == 0)
					int_hsdma |= (1 << bchannel);
			}
		}
		dev_dbg(musb->controller, "int_hsdma = 0x%x\n", int_hsdma);
		if (!int_hsdma)
			goto done;
	}
	for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
		if (int_hsdma & (1 << bchannel)) {
			musb_channel = (struct musb_dma_channel *)
					&(controller->channel[bchannel]);
			channel = &musb_channel->channel;
			csr = musb_readw(mbase,
					MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
							MUSB_HSDMA_CONTROL));
			if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
				musb_channel->channel.status =
					MUSB_DMA_STATUS_BUS_ABORT;
			} else {
				u8 devctl;
				addr = musb_read_hsdma_addr(mbase,
						bchannel);
				channel->actual_len = addr
					- musb_channel->start_addr;
				dev_dbg(musb->controller, "ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
					channel, musb_channel->start_addr,
					addr, channel->actual_len,
					musb_channel->len,
					(channel->actual_len
						< musb_channel->len) ?
					"=> reconfig 0" : "=> complete");
				devctl = musb_readb(mbase, MUSB_DEVCTL);
				channel->status = MUSB_DMA_STATUS_FREE;
				/* completed */
				if ((devctl & MUSB_DEVCTL_HM)
					&& (musb_channel->transmit)
					&& ((channel->desired_mode == 0)
					    || (channel->actual_len &
					    (musb_channel->max_packet_sz - 1)))
				    ) {
					u8  epnum  = musb_channel->epnum;
					int offset = musb->io.ep_offset(epnum,
								    MUSB_TXCSR);
					u16 txcsr;
					/*
                                         * The programming guide says that we
                                         * must clear DMAENAB before DMAMODE.
                                         */
					musb_ep_select(mbase, epnum);
					txcsr = musb_readw(mbase, offset);
					txcsr &= ~(MUSB_TXCSR_DMAENAB
							| MUSB_TXCSR_AUTOSET);
					musb_writew(mbase, offset, txcsr);
					/* Send out the packet */
					txcsr &= ~MUSB_TXCSR_DMAMODE;
					txcsr |=  MUSB_TXCSR_TXPKTRDY;
					musb_writew(mbase, offset, txcsr);
				}
				musb_dma_completion(musb, musb_channel->epnum,
						    musb_channel->transmit);
			}
		}
	}
	retval = IRQ_HANDLED;
done:
	spin_unlock_irqrestore(&musb->lock, flags);
	return retval;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 347 | 65.84% | 3 | 33.33% | 
| anand gadiyar | anand gadiyar | 101 | 19.17% | 1 | 11.11% | 
| sergei shtylyov | sergei shtylyov | 57 | 10.82% | 1 | 11.11% | 
| cliff cai | cliff cai | 15 | 2.85% | 1 | 11.11% | 
| tony lindgren | tony lindgren | 5 | 0.95% | 1 | 11.11% | 
| bryan wu | bryan wu | 1 | 0.19% | 1 | 11.11% | 
| mike frysinger | mike frysinger | 1 | 0.19% | 1 | 11.11% | 
 | Total | 527 | 100.00% | 9 | 100.00% | 
void musbhs_dma_controller_destroy(struct dma_controller *c)
{
	struct musb_dma_controller *controller = container_of(c,
			struct musb_dma_controller, controller);
	dma_controller_stop(controller);
	if (controller->irq)
		free_irq(controller->irq, c);
	kfree(controller);
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 44 | 88.00% | 2 | 50.00% | 
| sebastian andrzej siewior | sebastian andrzej siewior | 5 | 10.00% | 1 | 25.00% | 
| tony lindgren | tony lindgren | 1 | 2.00% | 1 | 25.00% | 
 | Total | 50 | 100.00% | 4 | 100.00% | 
EXPORT_SYMBOL_GPL(musbhs_dma_controller_destroy);
struct dma_controller *musbhs_dma_controller_create(struct musb *musb,
						    void __iomem *base)
{
	struct musb_dma_controller *controller;
	struct device *dev = musb->controller;
	struct platform_device *pdev = to_platform_device(dev);
	int irq = platform_get_irq_byname(pdev, "dma");
	if (irq <= 0) {
		dev_err(dev, "No DMA interrupt line!\n");
		return NULL;
	}
	controller = kzalloc(sizeof(*controller), GFP_KERNEL);
	if (!controller)
		return NULL;
	controller->channel_count = MUSB_HSDMA_CHANNELS;
	controller->private_data = musb;
	controller->base = base;
	controller->controller.channel_alloc = dma_channel_allocate;
	controller->controller.channel_release = dma_channel_release;
	controller->controller.channel_program = dma_channel_program;
	controller->controller.channel_abort = dma_channel_abort;
	if (request_irq(irq, dma_controller_irq, 0,
			dev_name(musb->controller), &controller->controller)) {
		dev_err(dev, "request_irq %d failed!\n", irq);
		musb_dma_controller_destroy(&controller->controller);
		return NULL;
	}
	controller->irq = irq;
	return &controller->controller;
}
Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 188 | 95.43% | 2 | 28.57% | 
| kay sievers | kay sievers | 3 | 1.52% | 1 | 14.29% | 
| tony lindgren | tony lindgren | 2 | 1.02% | 1 | 14.29% | 
| hema kalliguddi | hema kalliguddi | 2 | 1.02% | 1 | 14.29% | 
| sergei shtylyov | sergei shtylyov | 1 | 0.51% | 1 | 14.29% | 
| yong zhang | yong zhang | 1 | 0.51% | 1 | 14.29% | 
 | Total | 197 | 100.00% | 7 | 100.00% | 
EXPORT_SYMBOL_GPL(musbhs_dma_controller_create);
Overall Contributors
 | Person | Tokens | Prop | Commits | CommitProp | 
| felipe balbi | felipe balbi | 1397 | 79.56% | 3 | 13.64% | 
| anand gadiyar | anand gadiyar | 140 | 7.97% | 3 | 13.64% | 
| sergei shtylyov | sergei shtylyov | 94 | 5.35% | 2 | 9.09% | 
| mike frysinger | mike frysinger | 42 | 2.39% | 2 | 9.09% | 
| tony lindgren | tony lindgren | 39 | 2.22% | 2 | 9.09% | 
| cliff cai | cliff cai | 15 | 0.85% | 1 | 4.55% | 
| sebastian andrzej siewior | sebastian andrzej siewior | 8 | 0.46% | 1 | 4.55% | 
| bryan wu | bryan wu | 6 | 0.34% | 1 | 4.55% | 
| arnd bergmann | arnd bergmann | 4 | 0.23% | 1 | 4.55% | 
| kay sievers | kay sievers | 3 | 0.17% | 1 | 4.55% | 
| tejun heo | tejun heo | 3 | 0.17% | 1 | 4.55% | 
| hema kalliguddi | hema kalliguddi | 3 | 0.17% | 2 | 9.09% | 
| anil shetty | anil shetty | 1 | 0.06% | 1 | 4.55% | 
| yong zhang | yong zhang | 1 | 0.06% | 1 | 4.55% | 
 | Total | 1756 | 100.00% | 22 | 100.00% | 
  
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.