dma.c 8.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 *  linux/arch/arm/mach-rpc/dma.c
 *
 *  Copyright (C) 1998 Russell King
 *
 * 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.
 *
 *  DMA functions specific to RiscPC architecture
 */
#include <linux/mman.h>
#include <linux/init.h>
#include <linux/interrupt.h>
15
#include <linux/dma-mapping.h>
16
#include <linux/io.h>
L
Linus Torvalds 已提交
17 18 19 20 21

#include <asm/page.h>
#include <asm/dma.h>
#include <asm/fiq.h>
#include <asm/irq.h>
22
#include <mach/hardware.h>
L
Linus Torvalds 已提交
23 24 25 26 27
#include <asm/uaccess.h>

#include <asm/mach/dma.h>
#include <asm/hardware/iomd.h>

28 29 30 31 32 33
struct iomd_dma {
	struct dma_struct	dma;
	unsigned int		state;
	unsigned long		base;		/* Controller base address */
	int			irq;		/* Controller IRQ */
	struct scatterlist	cur_sg;		/* Current controller buffer */
34 35
	dma_addr_t		dma_addr;
	unsigned int		dma_len;
36 37
};

L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#if 0
typedef enum {
	dma_size_8	= 1,
	dma_size_16	= 2,
	dma_size_32	= 4,
	dma_size_128	= 16
} dma_size_t;
#endif

#define TRANSFER_SIZE	2

#define CURA	(0)
#define ENDA	(IOMD_IO0ENDA - IOMD_IO0CURA)
#define CURB	(IOMD_IO0CURB - IOMD_IO0CURA)
#define ENDB	(IOMD_IO0ENDB - IOMD_IO0CURA)
#define CR	(IOMD_IO0CR - IOMD_IO0CURA)
#define ST	(IOMD_IO0ST - IOMD_IO0CURA)

56
static void iomd_get_next_sg(struct scatterlist *sg, struct iomd_dma *idma)
L
Linus Torvalds 已提交
57 58 59
{
	unsigned long end, offset, flags = 0;

60
	if (idma->dma.sg) {
61
		sg->dma_address = idma->dma_addr;
L
Linus Torvalds 已提交
62 63
		offset = sg->dma_address & ~PAGE_MASK;

64
		end = offset + idma->dma_len;
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73

		if (end > PAGE_SIZE)
			end = PAGE_SIZE;

		if (offset + TRANSFER_SIZE >= end)
			flags |= DMA_END_L;

		sg->length = end - TRANSFER_SIZE;

74 75
		idma->dma_len -= end - offset;
		idma->dma_addr += end - offset;
L
Linus Torvalds 已提交
76

77
		if (idma->dma_len == 0) {
78
			if (idma->dma.sgcount > 1) {
79
				idma->dma.sg = sg_next(idma->dma.sg);
80 81
				idma->dma_addr = idma->dma.sg->dma_address;
				idma->dma_len = idma->dma.sg->length;
82
				idma->dma.sgcount--;
L
Linus Torvalds 已提交
83
			} else {
84
				idma->dma.sg = NULL;
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92 93 94 95 96
				flags |= DMA_END_S;
			}
		}
	} else {
		flags = DMA_END_S | DMA_END_L;
		sg->dma_address = 0;
		sg->length = 0;
	}

	sg->length |= flags;
}

97
static irqreturn_t iomd_dma_handle(int irq, void *dev_id)
L
Linus Torvalds 已提交
98
{
99 100
	struct iomd_dma *idma = dev_id;
	unsigned long base = idma->base;
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108

	do {
		unsigned int status;

		status = iomd_readb(base + ST);
		if (!(status & DMA_ST_INT))
			return IRQ_HANDLED;

109 110
		if ((idma->state ^ status) & DMA_ST_AB)
			iomd_get_next_sg(&idma->cur_sg, idma);
L
Linus Torvalds 已提交
111 112 113 114

		switch (status & (DMA_ST_OFL | DMA_ST_AB)) {
		case DMA_ST_OFL:			/* OIA */
		case DMA_ST_AB:				/* .IB */
115 116 117
			iomd_writel(idma->cur_sg.dma_address, base + CURA);
			iomd_writel(idma->cur_sg.length, base + ENDA);
			idma->state = DMA_ST_AB;
L
Linus Torvalds 已提交
118 119 120 121
			break;

		case DMA_ST_OFL | DMA_ST_AB:		/* OIB */
		case 0:					/* .IA */
122 123 124
			iomd_writel(idma->cur_sg.dma_address, base + CURB);
			iomd_writel(idma->cur_sg.length, base + ENDB);
			idma->state = 0;
L
Linus Torvalds 已提交
125 126 127 128
			break;
		}

		if (status & DMA_ST_OFL &&
129
		    idma->cur_sg.length == (DMA_END_S|DMA_END_L))
L
Linus Torvalds 已提交
130 131 132
			break;
	} while (1);

133
	idma->state = ~DMA_ST_AB;
L
Linus Torvalds 已提交
134 135 136 137 138
	disable_irq(irq);

	return IRQ_HANDLED;
}

R
Russell King 已提交
139
static int iomd_request_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
140
{
141 142 143
	struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);

	return request_irq(idma->irq, iomd_dma_handle,
144
			   0, idma->dma.device_id, idma);
L
Linus Torvalds 已提交
145 146
}

R
Russell King 已提交
147
static void iomd_free_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
148
{
149 150 151
	struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);

	free_irq(idma->irq, idma);
L
Linus Torvalds 已提交
152 153
}

R
Russell King 已提交
154
static void iomd_enable_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
155
{
156 157
	struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
	unsigned long dma_base = idma->base;
L
Linus Torvalds 已提交
158 159
	unsigned int ctrl = TRANSFER_SIZE | DMA_CR_E;

160 161
	if (idma->dma.invalid) {
		idma->dma.invalid = 0;
L
Linus Torvalds 已提交
162 163 164 165 166

		/*
		 * Cope with ISA-style drivers which expect cache
		 * coherence.
		 */
167 168 169 170 171 172 173
		if (!idma->dma.sg) {
			idma->dma.sg = &idma->dma.buf;
			idma->dma.sgcount = 1;
			idma->dma.buf.length = idma->dma.count;
			idma->dma.buf.dma_address = dma_map_single(NULL,
				idma->dma.addr, idma->dma.count,
				idma->dma.dma_mode == DMA_MODE_READ ?
174
				DMA_FROM_DEVICE : DMA_TO_DEVICE);
L
Linus Torvalds 已提交
175 176 177
		}

		iomd_writeb(DMA_CR_C, dma_base + CR);
178
		idma->state = DMA_ST_AB;
L
Linus Torvalds 已提交
179
	}
180 181

	if (idma->dma.dma_mode == DMA_MODE_READ)
L
Linus Torvalds 已提交
182 183 184
		ctrl |= DMA_CR_D;

	iomd_writeb(ctrl, dma_base + CR);
185
	enable_irq(idma->irq);
L
Linus Torvalds 已提交
186 187
}

R
Russell King 已提交
188
static void iomd_disable_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
189
{
190 191
	struct iomd_dma *idma = container_of(dma, struct iomd_dma, dma);
	unsigned long dma_base = idma->base;
L
Linus Torvalds 已提交
192 193 194
	unsigned long flags;

	local_irq_save(flags);
195 196
	if (idma->state != ~DMA_ST_AB)
		disable_irq(idma->irq);
L
Linus Torvalds 已提交
197 198 199 200
	iomd_writeb(0, dma_base + CR);
	local_irq_restore(flags);
}

R
Russell King 已提交
201
static int iomd_set_dma_speed(unsigned int chan, dma_t *dma, int cycle)
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
{
	int tcr, speed;

	if (cycle < 188)
		speed = 3;
	else if (cycle <= 250)
		speed = 2;
	else if (cycle < 438)
		speed = 1;
	else
		speed = 0;

	tcr = iomd_readb(IOMD_DMATCR);
	speed &= 3;

R
Russell King 已提交
217
	switch (chan) {
L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	case DMA_0:
		tcr = (tcr & ~0x03) | speed;
		break;

	case DMA_1:
		tcr = (tcr & ~0x0c) | (speed << 2);
		break;

	case DMA_2:
		tcr = (tcr & ~0x30) | (speed << 4);
		break;

	case DMA_3:
		tcr = (tcr & ~0xc0) | (speed << 6);
		break;

	default:
		break;
	}

	iomd_writeb(tcr, IOMD_DMATCR);

	return speed;
}

static struct dma_ops iomd_dma_ops = {
	.type		= "IOMD",
	.request	= iomd_request_dma,
	.free		= iomd_free_dma,
	.enable		= iomd_enable_dma,
	.disable	= iomd_disable_dma,
	.setspeed	= iomd_set_dma_speed,
};

static struct fiq_handler fh = {
	.name	= "floppydma"
};

256 257 258 259 260
struct floppy_dma {
	struct dma_struct	dma;
	unsigned int		fiq;
};

R
Russell King 已提交
261
static void floppy_enable_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
262
{
263
	struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
L
Linus Torvalds 已提交
264 265 266 267
	void *fiqhandler_start;
	unsigned int fiqhandler_length;
	struct pt_regs regs;

268
	if (fdma->dma.sg)
L
Linus Torvalds 已提交
269 270
		BUG();

271
	if (fdma->dma.dma_mode == DMA_MODE_READ) {
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280
		extern unsigned char floppy_fiqin_start, floppy_fiqin_end;
		fiqhandler_start = &floppy_fiqin_start;
		fiqhandler_length = &floppy_fiqin_end - &floppy_fiqin_start;
	} else {
		extern unsigned char floppy_fiqout_start, floppy_fiqout_end;
		fiqhandler_start = &floppy_fiqout_start;
		fiqhandler_length = &floppy_fiqout_end - &floppy_fiqout_start;
	}

281 282
	regs.ARM_r9  = fdma->dma.count;
	regs.ARM_r10 = (unsigned long)fdma->dma.addr;
L
Linus Torvalds 已提交
283 284 285 286 287 288 289 290 291
	regs.ARM_fp  = (unsigned long)FLOPPYDMA_BASE;

	if (claim_fiq(&fh)) {
		printk("floppydma: couldn't claim FIQ.\n");
		return;
	}

	set_fiq_handler(fiqhandler_start, fiqhandler_length);
	set_fiq_regs(&regs);
292
	enable_fiq(fdma->fiq);
L
Linus Torvalds 已提交
293 294
}

R
Russell King 已提交
295
static void floppy_disable_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
296
{
297 298
	struct floppy_dma *fdma = container_of(dma, struct floppy_dma, dma);
	disable_fiq(fdma->fiq);
L
Linus Torvalds 已提交
299 300 301
	release_fiq(&fh);
}

R
Russell King 已提交
302
static int floppy_get_residue(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
{
	struct pt_regs regs;
	get_fiq_regs(&regs);
	return regs.ARM_r9;
}

static struct dma_ops floppy_dma_ops = {
	.type		= "FIQDMA",
	.enable		= floppy_enable_dma,
	.disable	= floppy_disable_dma,
	.residue	= floppy_get_residue,
};

/*
 * This is virtual DMA - we don't need anything here.
 */
R
Russell King 已提交
319
static void sound_enable_disable_dma(unsigned int chan, dma_t *dma)
L
Linus Torvalds 已提交
320 321 322 323 324 325 326 327 328
{
}

static struct dma_ops sound_dma_ops = {
	.type		= "VIRTUAL",
	.enable		= sound_enable_disable_dma,
	.disable	= sound_enable_disable_dma,
};

329
static struct iomd_dma iomd_dma[6];
R
Russell King 已提交
330

331 332 333 334 335
static struct floppy_dma floppy_dma = {
	.dma		= {
		.d_ops	= &floppy_dma_ops,
	},
	.fiq		= FIQ_FLOPPYDATA,
R
Russell King 已提交
336 337 338 339 340 341 342
};

static dma_t sound_dma = {
	.d_ops		= &sound_dma_ops,
};

static int __init rpc_dma_init(void)
L
Linus Torvalds 已提交
343
{
R
Russell King 已提交
344 345 346
	unsigned int i;
	int ret;

L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354 355 356 357 358
	iomd_writeb(0, IOMD_IO0CR);
	iomd_writeb(0, IOMD_IO1CR);
	iomd_writeb(0, IOMD_IO2CR);
	iomd_writeb(0, IOMD_IO3CR);

	iomd_writeb(0xa0, IOMD_DMATCR);

	/*
	 * Setup DMA channels 2,3 to be for podules
	 * and channels 0,1 for internal devices
	 */
	iomd_writeb(DMA_EXT_IO3|DMA_EXT_IO2, IOMD_DMAEXT);
R
Russell King 已提交
359

360 361 362 363 364 365 366 367 368 369 370 371
	iomd_dma[DMA_0].base	= IOMD_IO0CURA;
	iomd_dma[DMA_0].irq	= IRQ_DMA0;
	iomd_dma[DMA_1].base	= IOMD_IO1CURA;
	iomd_dma[DMA_1].irq	= IRQ_DMA1;
	iomd_dma[DMA_2].base	= IOMD_IO2CURA;
	iomd_dma[DMA_2].irq	= IRQ_DMA2;
	iomd_dma[DMA_3].base	= IOMD_IO3CURA;
	iomd_dma[DMA_3].irq	= IRQ_DMA3;
	iomd_dma[DMA_S0].base	= IOMD_SD0CURA;
	iomd_dma[DMA_S0].irq	= IRQ_DMAS0;
	iomd_dma[DMA_S1].base	= IOMD_SD1CURA;
	iomd_dma[DMA_S1].irq	= IRQ_DMAS1;
R
Russell King 已提交
372 373

	for (i = DMA_0; i <= DMA_S1; i++) {
374
		iomd_dma[i].dma.d_ops = &iomd_dma_ops;
R
Russell King 已提交
375

376
		ret = isa_dma_add(i, &iomd_dma[i].dma);
R
Russell King 已提交
377 378 379 380
		if (ret)
			printk("IOMDDMA%u: unable to register: %d\n", i, ret);
	}

381
	ret = isa_dma_add(DMA_VIRTUAL_FLOPPY, &floppy_dma.dma);
R
Russell King 已提交
382 383 384 385 386 387
	if (ret)
		printk("IOMDFLOPPY: unable to register: %d\n", ret);
	ret = isa_dma_add(DMA_VIRTUAL_SOUND, &sound_dma);
	if (ret)
		printk("IOMDSOUND: unable to register: %d\n", ret);
	return 0;
L
Linus Torvalds 已提交
388
}
R
Russell King 已提交
389
core_initcall(rpc_dma_init);