musbhsdma.c 11.7 KB
Newer Older
F
Felipe Balbi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * 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>
36
#include <linux/slab.h>
F
Felipe Balbi 已提交
37
#include "musb_core.h"
38
#include "musbhsdma.h"
F
Felipe Balbi 已提交
39 40 41 42 43 44 45

static int dma_controller_start(struct dma_controller *c)
{
	/* nothing to do */
	return 0;
}

46
static void dma_channel_release(struct dma_channel *channel);
F
Felipe Balbi 已提交
47 48 49

static int dma_controller_stop(struct dma_controller *c)
{
50 51 52 53 54
	struct musb_dma_controller *controller = container_of(c,
			struct musb_dma_controller, controller);
	struct musb *musb = controller->private_data;
	struct dma_channel *channel;
	u8 bit;
F
Felipe Balbi 已提交
55

56
	if (controller->used_channels != 0) {
F
Felipe Balbi 已提交
57 58 59
		dev_err(musb->controller,
			"Stopping DMA controller while channel active\n");

60 61 62 63
		for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
			if (controller->used_channels & (1 << bit)) {
				channel = &controller->channel[bit].channel;
				dma_channel_release(channel);
F
Felipe Balbi 已提交
64

65
				if (!controller->used_channels)
F
Felipe Balbi 已提交
66 67 68 69
					break;
			}
		}
	}
70

F
Felipe Balbi 已提交
71 72 73 74 75 76
	return 0;
}

static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
				struct musb_hw_ep *hw_ep, u8 transmit)
{
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	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;
94
			channel->max_len = 0x100000;
F
Felipe Balbi 已提交
95
			/* Tx => mode 1; Rx => mode 0 */
96 97
			channel->desired_mode = transmit;
			channel->actual_len = 0;
F
Felipe Balbi 已提交
98 99 100
			break;
		}
	}
101 102

	return channel;
F
Felipe Balbi 已提交
103 104
}

105
static void dma_channel_release(struct dma_channel *channel)
F
Felipe Balbi 已提交
106
{
107
	struct musb_dma_channel *musb_channel = channel->private_data;
F
Felipe Balbi 已提交
108

109 110 111
	channel->actual_len = 0;
	musb_channel->start_addr = 0;
	musb_channel->len = 0;
F
Felipe Balbi 已提交
112

113 114
	musb_channel->controller->used_channels &=
		~(1 << musb_channel->idx);
F
Felipe Balbi 已提交
115

116
	channel->status = MUSB_DMA_STATUS_UNKNOWN;
F
Felipe Balbi 已提交
117 118
}

119
static void configure_channel(struct dma_channel *channel,
F
Felipe Balbi 已提交
120 121 122
				u16 packet_sz, u8 mode,
				dma_addr_t dma_addr, u32 len)
{
123 124
	struct musb_dma_channel *musb_channel = channel->private_data;
	struct musb_dma_controller *controller = musb_channel->controller;
125
	struct musb *musb = controller->private_data;
126 127
	void __iomem *mbase = controller->base;
	u8 bchannel = musb_channel->idx;
F
Felipe Balbi 已提交
128 129
	u16 csr = 0;

130
	dev_dbg(musb->controller, "%p, pkt_sz %d, addr 0x%x, len %d, mode %d\n",
131
			channel, packet_sz, dma_addr, len, mode);
F
Felipe Balbi 已提交
132 133 134 135 136

	if (mode) {
		csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
		BUG_ON(len < packet_sz);
	}
137 138
	csr |= MUSB_HSDMA_BURSTMODE_INCR16
				<< MUSB_HSDMA_BURSTMODE_SHIFT;
F
Felipe Balbi 已提交
139

140
	csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
F
Felipe Balbi 已提交
141 142
		| (1 << MUSB_HSDMA_ENABLE_SHIFT)
		| (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
143
		| (musb_channel->transmit
F
Felipe Balbi 已提交
144 145 146 147
				? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
				: 0);

	/* address/count */
148 149
	musb_write_hsdma_addr(mbase, bchannel, dma_addr);
	musb_write_hsdma_count(mbase, bchannel, len);
F
Felipe Balbi 已提交
150 151 152

	/* control (this should start things) */
	musb_writew(mbase,
153
		MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
F
Felipe Balbi 已提交
154 155 156
		csr);
}

157
static int dma_channel_program(struct dma_channel *channel,
F
Felipe Balbi 已提交
158 159 160
				u16 packet_sz, u8 mode,
				dma_addr_t dma_addr, u32 len)
{
161
	struct musb_dma_channel *musb_channel = channel->private_data;
162 163
	struct musb_dma_controller *controller = musb_channel->controller;
	struct musb *musb = controller->private_data;
F
Felipe Balbi 已提交
164

165
	dev_dbg(musb->controller, "ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
166 167
		musb_channel->epnum,
		musb_channel->transmit ? "Tx" : "Rx",
F
Felipe Balbi 已提交
168 169
		packet_sz, dma_addr, len, mode);

170 171
	BUG_ON(channel->status == MUSB_DMA_STATUS_UNKNOWN ||
		channel->status == MUSB_DMA_STATUS_BUSY);
F
Felipe Balbi 已提交
172

173 174 175 176 177 178 179 180
	/* 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;
	}

181 182 183 184 185 186 187 188 189 190 191 192
	/*
	 * 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;

193 194 195 196 197
	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;
F
Felipe Balbi 已提交
198

199
	configure_channel(channel, packet_sz, mode, dma_addr, len);
F
Felipe Balbi 已提交
200 201 202 203

	return true;
}

204
static int dma_channel_abort(struct dma_channel *channel)
F
Felipe Balbi 已提交
205
{
206 207 208 209
	struct musb_dma_channel *musb_channel = channel->private_data;
	void __iomem *mbase = musb_channel->controller->base;

	u8 bchannel = musb_channel->idx;
210
	int offset;
F
Felipe Balbi 已提交
211 212
	u16 csr;

213 214
	if (channel->status == MUSB_DMA_STATUS_BUSY) {
		if (musb_channel->transmit) {
215 216 217 218 219 220 221 222 223 224 225 226
			offset = MUSB_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);
F
Felipe Balbi 已提交
227
		} else {
228 229 230 231
			offset = MUSB_EP_OFFSET(musb_channel->epnum,
						MUSB_RXCSR);

			csr = musb_readw(mbase, offset);
F
Felipe Balbi 已提交
232 233 234
			csr &= ~(MUSB_RXCSR_AUTOCLEAR |
				 MUSB_RXCSR_DMAENAB |
				 MUSB_RXCSR_DMAMODE);
235
			musb_writew(mbase, offset, csr);
F
Felipe Balbi 已提交
236 237 238
		}

		musb_writew(mbase,
239
			MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL),
F
Felipe Balbi 已提交
240
			0);
241 242
		musb_write_hsdma_addr(mbase, bchannel, 0);
		musb_write_hsdma_count(mbase, bchannel, 0);
243
		channel->status = MUSB_DMA_STATUS_FREE;
F
Felipe Balbi 已提交
244
	}
245

F
Felipe Balbi 已提交
246 247 248 249 250
	return 0;
}

static irqreturn_t dma_controller_irq(int irq, void *private_data)
{
251 252 253 254 255 256 257
	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;

F
Felipe Balbi 已提交
258
	irqreturn_t retval = IRQ_NONE;
259

F
Felipe Balbi 已提交
260 261
	unsigned long flags;

262 263 264
	u8 bchannel;
	u8 int_hsdma;

265
	u32 addr, count;
266 267
	u16 csr;

F
Felipe Balbi 已提交
268 269 270 271
	spin_lock_irqsave(&musb->lock, flags);

	int_hsdma = musb_readb(mbase, MUSB_HSDMA_INTR);

272 273 274 275 276
#ifdef CONFIG_BLACKFIN
	/* Clear DMA interrupt flags */
	musb_writeb(mbase, MUSB_HSDMA_INTR, int_hsdma);
#endif

277
	if (!int_hsdma) {
278
		dev_dbg(musb->controller, "spurious DMA irq\n");
279 280 281 282 283 284 285 286 287 288 289 290 291

		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);
			}
		}

292
		dev_dbg(musb->controller, "int_hsdma = 0x%x\n", int_hsdma);
293 294 295 296 297

		if (!int_hsdma)
			goto done;
	}

298 299 300 301 302
	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;
F
Felipe Balbi 已提交
303 304

			csr = musb_readw(mbase,
305
					MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
F
Felipe Balbi 已提交
306 307
							MUSB_HSDMA_CONTROL));

308 309
			if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
				musb_channel->channel.status =
F
Felipe Balbi 已提交
310
					MUSB_DMA_STATUS_BUS_ABORT;
311
			} else {
F
Felipe Balbi 已提交
312 313
				u8 devctl;

314 315
				addr = musb_read_hsdma_addr(mbase,
						bchannel);
316 317
				channel->actual_len = addr
					- musb_channel->start_addr;
F
Felipe Balbi 已提交
318

319
				dev_dbg(musb->controller, "ch %p, 0x%x -> 0x%x (%zu / %d) %s\n",
320 321 322 323 324
					channel, musb_channel->start_addr,
					addr, channel->actual_len,
					musb_channel->len,
					(channel->actual_len
						< musb_channel->len) ?
F
Felipe Balbi 已提交
325 326 327 328
					"=> reconfig 0" : "=> complete");

				devctl = musb_readb(mbase, MUSB_DEVCTL);

329
				channel->status = MUSB_DMA_STATUS_FREE;
F
Felipe Balbi 已提交
330 331 332

				/* completed */
				if ((devctl & MUSB_DEVCTL_HM)
333 334 335 336
					&& (musb_channel->transmit)
					&& ((channel->desired_mode == 0)
					    || (channel->actual_len &
					    (musb_channel->max_packet_sz - 1)))
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
				    ) {
					u8  epnum  = musb_channel->epnum;
					int offset = MUSB_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);
F
Felipe Balbi 已提交
352
					/* Send out the packet */
353 354 355
					txcsr &= ~MUSB_TXCSR_DMAMODE;
					txcsr |=  MUSB_TXCSR_TXPKTRDY;
					musb_writew(mbase, offset, txcsr);
356
				}
357 358
				musb_dma_completion(musb, musb_channel->epnum,
						    musb_channel->transmit);
F
Felipe Balbi 已提交
359 360 361
			}
		}
	}
362

F
Felipe Balbi 已提交
363 364 365 366 367 368 369 370
	retval = IRQ_HANDLED;
done:
	spin_unlock_irqrestore(&musb->lock, flags);
	return retval;
}

void dma_controller_destroy(struct dma_controller *c)
{
371 372
	struct musb_dma_controller *controller = container_of(c,
			struct musb_dma_controller, controller);
F
Felipe Balbi 已提交
373 374 375 376 377 378 379 380 381 382 383

	if (!controller)
		return;

	if (controller->irq)
		free_irq(controller->irq, c);

	kfree(controller);
}

struct dma_controller *__init
384
dma_controller_create(struct musb *musb, void __iomem *base)
F
Felipe Balbi 已提交
385 386 387 388
{
	struct musb_dma_controller *controller;
	struct device *dev = musb->controller;
	struct platform_device *pdev = to_platform_device(dev);
389
	int irq = platform_get_irq_byname(pdev, "dma");
F
Felipe Balbi 已提交
390

391
	if (irq <= 0) {
F
Felipe Balbi 已提交
392 393 394 395
		dev_err(dev, "No DMA interrupt line!\n");
		return NULL;
	}

396
	controller = kzalloc(sizeof(*controller), GFP_KERNEL);
F
Felipe Balbi 已提交
397 398 399
	if (!controller)
		return NULL;

400 401 402
	controller->channel_count = MUSB_HSDMA_CHANNELS;
	controller->private_data = musb;
	controller->base = base;
F
Felipe Balbi 已提交
403

404 405 406 407 408 409
	controller->controller.start = dma_controller_start;
	controller->controller.stop = dma_controller_stop;
	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;
F
Felipe Balbi 已提交
410

Y
Yong Zhang 已提交
411
	if (request_irq(irq, dma_controller_irq, 0,
412
			dev_name(musb->controller), &controller->controller)) {
F
Felipe Balbi 已提交
413
		dev_err(dev, "request_irq %d failed!\n", irq);
414 415
		dma_controller_destroy(&controller->controller);

F
Felipe Balbi 已提交
416 417 418 419 420
		return NULL;
	}

	controller->irq = irq;

421
	return &controller->controller;
F
Felipe Balbi 已提交
422
}