ecard.c 25.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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 36 37 38 39
/*
 *  linux/arch/arm/kernel/ecard.c
 *
 *  Copyright 1995-2001 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.
 *
 *  Find all installed expansion cards, and handle interrupts from them.
 *
 *  Created from information from Acorns RiscOS3 PRMs
 *
 *  08-Dec-1996	RMK	Added code for the 9'th expansion card - the ether
 *			podule slot.
 *  06-May-1997	RMK	Added blacklist for cards whose loader doesn't work.
 *  12-Sep-1997	RMK	Created new handling of interrupt enables/disables
 *			- cards can now register their own routine to control
 *			interrupts (recommended).
 *  29-Sep-1997	RMK	Expansion card interrupt hardware not being re-enabled
 *			on reset from Linux. (Caused cards not to respond
 *			under RiscOS without hard reset).
 *  15-Feb-1998	RMK	Added DMA support
 *  12-Sep-1998	RMK	Added EASI support
 *  10-Jan-1999	RMK	Run loaders in a simulated RISC OS environment.
 *  17-Apr-1999	RMK	Support for EASI Type C cycles.
 */
#define ECARD_C

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/completion.h>
#include <linux/reboot.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
40
#include <linux/seq_file.h>
Linus Torvalds's avatar
Linus Torvalds committed
41 42
#include <linux/device.h>
#include <linux/init.h>
43
#include <linux/mutex.h>
44
#include <linux/kthread.h>
45
#include <linux/irq.h>
46
#include <linux/io.h>
Linus Torvalds's avatar
Linus Torvalds committed
47 48 49

#include <asm/dma.h>
#include <asm/ecard.h>
50
#include <mach/hardware.h>
Linus Torvalds's avatar
Linus Torvalds committed
51 52 53 54 55
#include <asm/irq.h>
#include <asm/mmu_context.h>
#include <asm/mach/irq.h>
#include <asm/tlbflush.h>

56 57
#include "ecard.h"

Linus Torvalds's avatar
Linus Torvalds committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
struct ecard_request {
	void		(*fn)(struct ecard_request *);
	ecard_t		*ec;
	unsigned int	address;
	unsigned int	length;
	unsigned int	use_loader;
	void		*buffer;
	struct completion *complete;
};

struct expcard_blacklist {
	unsigned short	 manufacturer;
	unsigned short	 product;
	const char	*type;
};

static ecard_t *cards;
static ecard_t *slot_to_expcard[MAX_ECARDS];
static unsigned int ectcr;

/* List of descriptions of cards which don't have an extended
 * identification, or chunk directories containing a description.
 */
static struct expcard_blacklist __initdata blacklist[] = {
	{ MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
};

asmlinkage extern int
ecard_loader_reset(unsigned long base, loader_t loader);
asmlinkage extern int
ecard_loader_read(int off, unsigned long base, loader_t loader);

static inline unsigned short ecard_getu16(unsigned char *v)
{
	return v[0] | v[1] << 8;
}

static inline signed long ecard_gets24(unsigned char *v)
{
	return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
}

static inline ecard_t *slot_to_ecard(unsigned int slot)
{
	return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
}

/* ===================== Expansion card daemon ======================== */
/*
 * Since the loader programs on the expansion cards need to be run
 * in a specific environment, create a separate task with this
 * environment up, and pass requests to this task as and when we
 * need to.
 *
 * This should allow 99% of loaders to be called from Linux.
 *
 * From a security standpoint, we trust the card vendors.  This
 * may be a misplaced trust.
 */
static void ecard_task_reset(struct ecard_request *req)
{
	struct expansion_card *ec = req->ec;
	struct resource *res;

	res = ec->slot_no == 8
		? &ec->resource[ECARD_RES_MEMC]
124
		: ec->easi
Linus Torvalds's avatar
Linus Torvalds committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
		  ? &ec->resource[ECARD_RES_EASI]
		  : &ec->resource[ECARD_RES_IOCSYNC];

	ecard_loader_reset(res->start, ec->loader);
}

static void ecard_task_readbytes(struct ecard_request *req)
{
	struct expansion_card *ec = req->ec;
	unsigned char *buf = req->buffer;
	unsigned int len = req->length;
	unsigned int off = req->address;

	if (ec->slot_no == 8) {
		void __iomem *base = (void __iomem *)
				ec->resource[ECARD_RES_MEMC].start;

		/*
		 * The card maintains an index which increments the address
		 * into a 4096-byte page on each access.  We need to keep
		 * track of the counter.
		 */
		static unsigned int index;
		unsigned int page;

		page = (off >> 12) * 4;
		if (page > 256 * 4)
			return;

		off &= 4095;

		/*
		 * If we are reading offset 0, or our current index is
		 * greater than the offset, reset the hardware index counter.
		 */
		if (off == 0 || index > off) {
			writeb(0, base);
			index = 0;
		}

		/*
		 * Increment the hardware index counter until we get to the
		 * required offset.  The read bytes are discarded.
		 */
		while (index < off) {
			readb(base + page);
			index += 1;
		}

		while (len--) {
			*buf++ = readb(base + page);
			index += 1;
		}
	} else {
179
		unsigned long base = (ec->easi
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
			 ? &ec->resource[ECARD_RES_EASI]
			 : &ec->resource[ECARD_RES_IOCSYNC])->start;
		void __iomem *pbase = (void __iomem *)base;

		if (!req->use_loader || !ec->loader) {
			off *= 4;
			while (len--) {
				*buf++ = readb(pbase + off);
				off += 4;
			}
		} else {
			while(len--) {
				/*
				 * The following is required by some
				 * expansion card loader programs.
				 */
				*(unsigned long *)0x108 = 0;
				*buf++ = ecard_loader_read(off++, base,
							   ec->loader);
			}
		}
	}

}

static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
static struct ecard_request *ecard_req;
207
static DEFINE_MUTEX(ecard_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

/*
 * Set up the expansion card daemon's page tables.
 */
static void ecard_init_pgtables(struct mm_struct *mm)
{
	struct vm_area_struct vma;

	/* We want to set up the page tables for the following mapping:
	 *  Virtual	Physical
	 *  0x03000000	0x03000000
	 *  0x03010000	unmapped
	 *  0x03210000	0x03210000
	 *  0x03400000	unmapped
	 *  0x08000000	0x08000000
	 *  0x10000000	unmapped
	 *
	 * FIXME: we don't follow this 100% yet.
	 */
	pgd_t *src_pgd, *dst_pgd;

	src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
	dst_pgd = pgd_offset(mm, IO_START);

	memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));

234
	src_pgd = pgd_offset(mm, (unsigned long)EASI_BASE);
Linus Torvalds's avatar
Linus Torvalds committed
235 236 237 238
	dst_pgd = pgd_offset(mm, EASI_START);

	memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));

239
	vma.vm_flags = VM_EXEC;
Linus Torvalds's avatar
Linus Torvalds committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
	vma.vm_mm = mm;

	flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
	flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
}

static int ecard_init_mm(void)
{
	struct mm_struct * mm = mm_alloc();
	struct mm_struct *active_mm = current->active_mm;

	if (!mm)
		return -ENOMEM;

	current->mm = mm;
	current->active_mm = mm;
	activate_mm(active_mm, mm);
	mmdrop(active_mm);
	ecard_init_pgtables(mm);
	return 0;
}

static int
ecard_task(void * unused)
{
	/*
	 * Allocate a mm.  We're not a lazy-TLB kernel task since we need
	 * to set page table entries where the user space would be.  Note
	 * that this also creates the page tables.  Failure is not an
	 * option here.
	 */
	if (ecard_init_mm())
		panic("kecardd: unable to alloc mm\n");

	while (1) {
		struct ecard_request *req;

		wait_event_interruptible(ecard_wait, ecard_req != NULL);

		req = xchg(&ecard_req, NULL);
		if (req != NULL) {
			req->fn(req);
			complete(req->complete);
		}
	}
}

/*
 * Wake the expansion card daemon to action our request.
 *
 * FIXME: The test here is not sufficient to detect if the
 * kcardd is running.
 */
static void ecard_call(struct ecard_request *req)
{
295
	DECLARE_COMPLETION_ONSTACK(completion);
Linus Torvalds's avatar
Linus Torvalds committed
296 297 298

	req->complete = &completion;

299
	mutex_lock(&ecard_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
300 301 302 303 304 305 306
	ecard_req = req;
	wake_up(&ecard_wait);

	/*
	 * Now wait for kecardd to run.
	 */
	wait_for_completion(&completion);
307
	mutex_unlock(&ecard_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
}

/* ======================= Mid-level card control ===================== */

static void
ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
{
	struct ecard_request req;

	req.fn		= ecard_task_readbytes;
	req.ec		= ec;
	req.address	= off;
	req.length	= len;
	req.use_loader	= useld;
	req.buffer	= addr;

	ecard_call(&req);
}

int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
{
	struct ex_chunk_dir excd;
	int index = 16;
	int useld = 0;

	if (!ec->cid.cd)
		return 0;

	while(1) {
		ecard_readbytes(&excd, ec, index, 8, useld);
		index += 8;
		if (c_id(&excd) == 0) {
			if (!useld && ec->loader) {
				useld = 1;
				index = 0;
				continue;
			}
			return 0;
		}
		if (c_id(&excd) == 0xf0) { /* link */
			index = c_start(&excd);
			continue;
		}
		if (c_id(&excd) == 0x80) { /* loader */
			if (!ec->loader) {
353
				ec->loader = kmalloc(c_len(&excd),
Linus Torvalds's avatar
Linus Torvalds committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
							       GFP_KERNEL);
				if (ec->loader)
					ecard_readbytes(ec->loader, ec,
							(int)c_start(&excd),
							c_len(&excd), useld);
				else
					return 0;
			}
			continue;
		}
		if (c_id(&excd) == id && num-- == 0)
			break;
	}

	if (c_id(&excd) & 0x80) {
		switch (c_id(&excd) & 0x70) {
		case 0x70:
			ecard_readbytes((unsigned char *)excd.d.string, ec,
					(int)c_start(&excd), c_len(&excd),
					useld);
			break;
		case 0x00:
			break;
		}
	}
	cd->start_offset = c_start(&excd);
	memcpy(cd->d.string, excd.d.string, 256);
	return 1;
}

/* ======================= Interrupt control ============================ */

static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
{
}

static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
{
}

static int ecard_def_irq_pending(ecard_t *ec)
{
	return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
}

static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
{
	panic("ecard_def_fiq_enable called - impossible");
}

static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
{
	panic("ecard_def_fiq_disable called - impossible");
}

static int ecard_def_fiq_pending(ecard_t *ec)
{
	return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
}

static expansioncard_ops_t ecard_default_ops = {
	ecard_def_irq_enable,
	ecard_def_irq_disable,
	ecard_def_irq_pending,
	ecard_def_fiq_enable,
	ecard_def_fiq_disable,
	ecard_def_fiq_pending
};

/*
 * Enable and disable interrupts from expansion cards.
 * (interrupts are disabled for these functions).
 *
 * They are not meant to be called directly, but via enable/disable_irq.
 */
429
static void ecard_irq_unmask(struct irq_data *d)
Linus Torvalds's avatar
Linus Torvalds committed
430
{
431
	ecard_t *ec = irq_data_get_irq_chip_data(d);
Linus Torvalds's avatar
Linus Torvalds committed
432 433 434 435 436 437

	if (ec) {
		if (!ec->ops)
			ec->ops = &ecard_default_ops;

		if (ec->claimed && ec->ops->irqenable)
438
			ec->ops->irqenable(ec, d->irq);
Linus Torvalds's avatar
Linus Torvalds committed
439 440
		else
			printk(KERN_ERR "ecard: rejecting request to "
441
				"enable IRQs for %d\n", d->irq);
Linus Torvalds's avatar
Linus Torvalds committed
442 443 444
	}
}

445
static void ecard_irq_mask(struct irq_data *d)
Linus Torvalds's avatar
Linus Torvalds committed
446
{
447
	ecard_t *ec = irq_data_get_irq_chip_data(d);
Linus Torvalds's avatar
Linus Torvalds committed
448 449 450 451 452 453

	if (ec) {
		if (!ec->ops)
			ec->ops = &ecard_default_ops;

		if (ec->ops && ec->ops->irqdisable)
454
			ec->ops->irqdisable(ec, d->irq);
Linus Torvalds's avatar
Linus Torvalds committed
455 456 457
	}
}

458
static struct irq_chip ecard_chip = {
459 460 461 462
	.name		= "ECARD",
	.irq_ack	= ecard_irq_mask,
	.irq_mask	= ecard_irq_mask,
	.irq_unmask	= ecard_irq_unmask,
Linus Torvalds's avatar
Linus Torvalds committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
};

void ecard_enablefiq(unsigned int fiqnr)
{
	ecard_t *ec = slot_to_ecard(fiqnr);

	if (ec) {
		if (!ec->ops)
			ec->ops = &ecard_default_ops;

		if (ec->claimed && ec->ops->fiqenable)
			ec->ops->fiqenable(ec, fiqnr);
		else
			printk(KERN_ERR "ecard: rejecting request to "
				"enable FIQs for %d\n", fiqnr);
	}
}

void ecard_disablefiq(unsigned int fiqnr)
{
	ecard_t *ec = slot_to_ecard(fiqnr);

	if (ec) {
		if (!ec->ops)
			ec->ops = &ecard_default_ops;

		if (ec->ops->fiqdisable)
			ec->ops->fiqdisable(ec, fiqnr);
	}
}

static void ecard_dump_irq_state(void)
{
	ecard_t *ec;

	printk("Expansion card IRQ state:\n");

	for (ec = cards; ec; ec = ec->next) {
		if (ec->slot_no == 8)
			continue;

		printk("  %d: %sclaimed, ",
		       ec->slot_no, ec->claimed ? "" : "not ");

		if (ec->ops && ec->ops->irqpending &&
		    ec->ops != &ecard_default_ops)
			printk("irq %spending\n",
			       ec->ops->irqpending(ec) ? "" : "not ");
		else
			printk("irqaddr %p, mask = %02X, status = %02X\n",
			       ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
	}
}

517
static void ecard_check_lockup(struct irq_desc *desc)
Linus Torvalds's avatar
Linus Torvalds committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
{
	static unsigned long last;
	static int lockup;

	/*
	 * If the timer interrupt has not run since the last million
	 * unrecognised expansion card interrupts, then there is
	 * something seriously wrong.  Disable the expansion card
	 * interrupts so at least we can continue.
	 *
	 * Maybe we ought to start a timer to re-enable them some time
	 * later?
	 */
	if (last == jiffies) {
		lockup += 1;
		if (lockup > 1000000) {
			printk(KERN_ERR "\nInterrupt lockup detected - "
			       "disabling all expansion card interrupts\n");

537
			desc->irq_data.chip->irq_mask(&desc->irq_data);
Linus Torvalds's avatar
Linus Torvalds committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
			ecard_dump_irq_state();
		}
	} else
		lockup = 0;

	/*
	 * If we did not recognise the source of this interrupt,
	 * warn the user, but don't flood the user with these messages.
	 */
	if (!last || time_after(jiffies, last + 5*HZ)) {
		last = jiffies;
		printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
		ecard_dump_irq_state();
	}
}

554
static void ecard_irq_handler(struct irq_desc *desc)
Linus Torvalds's avatar
Linus Torvalds committed
555 556 557 558
{
	ecard_t *ec;
	int called = 0;

559
	desc->irq_data.chip->irq_mask(&desc->irq_data);
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562
	for (ec = cards; ec; ec = ec->next) {
		int pending;

563
		if (!ec->claimed || !ec->irq || ec->slot_no == 8)
Linus Torvalds's avatar
Linus Torvalds committed
564 565 566 567 568 569 570 571
			continue;

		if (ec->ops && ec->ops->irqpending)
			pending = ec->ops->irqpending(ec);
		else
			pending = ecard_default_ops.irqpending(ec);

		if (pending) {
572
			generic_handle_irq(ec->irq);
Linus Torvalds's avatar
Linus Torvalds committed
573 574 575
			called ++;
		}
	}
576
	desc->irq_data.chip->irq_unmask(&desc->irq_data);
Linus Torvalds's avatar
Linus Torvalds committed
577 578 579 580 581

	if (called == 0)
		ecard_check_lockup(desc);
}

582
static void __iomem *__ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
Linus Torvalds's avatar
Linus Torvalds committed
583
{
584
	void __iomem *address = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
585 586 587
	int slot = ec->slot_no;

	if (ec->slot_no == 8)
588
		return ECARD_MEMC8_BASE;
Linus Torvalds's avatar
Linus Torvalds committed
589 590 591 592 593 594

	ectcr &= ~(1 << slot);

	switch (type) {
	case ECARD_MEMC:
		if (slot < 4)
595
			address = ECARD_MEMC_BASE + (slot << 14);
Linus Torvalds's avatar
Linus Torvalds committed
596 597 598 599
		break;

	case ECARD_IOC:
		if (slot < 4)
600
			address = ECARD_IOC_BASE + (slot << 14);
Linus Torvalds's avatar
Linus Torvalds committed
601
		else
602
			address = ECARD_IOC4_BASE + ((slot - 4) << 14);
Linus Torvalds's avatar
Linus Torvalds committed
603
		if (address)
604
			address += speed << 19;
Linus Torvalds's avatar
Linus Torvalds committed
605 606 607
		break;

	case ECARD_EASI:
608
		address = ECARD_EASI_BASE + (slot << 24);
Linus Torvalds's avatar
Linus Torvalds committed
609 610 611
		if (speed == ECARD_FAST)
			ectcr |= 1 << slot;
		break;
612

Linus Torvalds's avatar
Linus Torvalds committed
613 614 615 616 617 618 619
	default:
		break;
	}

#ifdef IOMD_ECTCR
	iomd_writeb(ectcr, IOMD_ECTCR);
#endif
620
	return address;
Linus Torvalds's avatar
Linus Torvalds committed
621 622
}

623
static int ecard_prints(struct seq_file *m, ecard_t *ec)
Linus Torvalds's avatar
Linus Torvalds committed
624
{
625
	seq_printf(m, "  %d: %s ", ec->slot_no, ec->easi ? "EASI" : "    ");
Linus Torvalds's avatar
Linus Torvalds committed
626 627 628 629

	if (ec->cid.id == 0) {
		struct in_chunk_dir incd;

630
		seq_printf(m, "[%04X:%04X] ",
Linus Torvalds's avatar
Linus Torvalds committed
631 632 633 634 635 636 637 638 639 640
			ec->cid.manufacturer, ec->cid.product);

		if (!ec->card_desc && ec->cid.cd &&
		    ecard_readchunk(&incd, ec, 0xf5, 0)) {
			ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);

			if (ec->card_desc)
				strcpy((char *)ec->card_desc, incd.d.string);
		}

641
		seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
Linus Torvalds's avatar
Linus Torvalds committed
642
	} else
643
		seq_printf(m, "Simple card %d\n", ec->cid.id);
Linus Torvalds's avatar
Linus Torvalds committed
644

645
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
646 647
}

648
static int ecard_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds's avatar
Linus Torvalds committed
649 650
{
	ecard_t *ec = cards;
651 652 653

	while (ec) {
		ecard_prints(m, ec);
Linus Torvalds's avatar
Linus Torvalds committed
654 655
		ec = ec->next;
	}
656
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
657 658
}

659 660 661 662 663 664 665 666 667 668 669 670 671
static int ecard_devices_proc_open(struct inode *inode, struct file *file)
{
	return single_open(file, ecard_devices_proc_show, NULL);
}

static const struct file_operations bus_ecard_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= ecard_devices_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};

Linus Torvalds's avatar
Linus Torvalds committed
672 673 674 675
static struct proc_dir_entry *proc_bus_ecard_dir = NULL;

static void ecard_proc_init(void)
{
Alexey Dobriyan's avatar
Alexey Dobriyan committed
676
	proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
677
	proc_create("devices", 0, proc_bus_ecard_dir, &bus_ecard_proc_fops);
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680 681
}

#define ec_set_resource(ec,nr,st,sz)				\
	do {							\
682
		(ec)->resource[nr].name = dev_name(&ec->dev);	\
Linus Torvalds's avatar
Linus Torvalds committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
		(ec)->resource[nr].start = st;			\
		(ec)->resource[nr].end = (st) + (sz) - 1;	\
		(ec)->resource[nr].flags = IORESOURCE_MEM;	\
	} while (0)

static void __init ecard_free_card(struct expansion_card *ec)
{
	int i;

	for (i = 0; i < ECARD_NUM_RESOURCES; i++)
		if (ec->resource[i].flags)
			release_resource(&ec->resource[i]);

	kfree(ec);
}

static struct expansion_card *__init ecard_alloc_card(int type, int slot)
{
	struct expansion_card *ec;
	unsigned long base;
	int i;

705
	ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
706 707 708 709 710 711
	if (!ec) {
		ec = ERR_PTR(-ENOMEM);
		goto nomem;
	}

	ec->slot_no = slot;
712
	ec->easi = type == ECARD_EASI;
713 714
	ec->irq = 0;
	ec->fiq = 0;
Linus Torvalds's avatar
Linus Torvalds committed
715 716 717
	ec->dma = NO_DMA;
	ec->ops = &ecard_default_ops;

718
	dev_set_name(&ec->dev, "ecard%d", slot);
Linus Torvalds's avatar
Linus Torvalds committed
719 720 721 722
	ec->dev.parent = NULL;
	ec->dev.bus = &ecard_bus_type;
	ec->dev.dma_mask = &ec->dma_mask;
	ec->dma_mask = (u64)0xffffffff;
723
	ec->dev.coherent_dma_mask = ec->dma_mask;
Linus Torvalds's avatar
Linus Torvalds committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

	if (slot < 4) {
		ec_set_resource(ec, ECARD_RES_MEMC,
				PODSLOT_MEMC_BASE + (slot << 14),
				PODSLOT_MEMC_SIZE);
		base = PODSLOT_IOC0_BASE + (slot << 14);
	} else
		base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);

#ifdef CONFIG_ARCH_RPC
	if (slot < 8) {
		ec_set_resource(ec, ECARD_RES_EASI,
				PODSLOT_EASI_BASE + (slot << 24),
				PODSLOT_EASI_SIZE);
	}

	if (slot == 8) {
		ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
	} else
#endif

	for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
		ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
				base + (i << 19), PODSLOT_IOC_SIZE);

	for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
		if (ec->resource[i].flags &&
		    request_resource(&iomem_resource, &ec->resource[i])) {
752
			dev_err(&ec->dev, "resource(s) not available\n");
Linus Torvalds's avatar
Linus Torvalds committed
753 754 755 756 757 758 759 760 761 762
			ec->resource[i].end -= ec->resource[i].start;
			ec->resource[i].start = 0;
			ec->resource[i].flags = 0;
		}
	}

 nomem:
	return ec;
}

763
static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
764 765 766 767 768
{
	struct expansion_card *ec = ECARD_DEV(dev);
	return sprintf(buf, "%u\n", ec->irq);
}

769
static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
770 771 772 773 774
{
	struct expansion_card *ec = ECARD_DEV(dev);
	return sprintf(buf, "%u\n", ec->dma);
}

775
static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
776 777 778 779 780 781
{
	struct expansion_card *ec = ECARD_DEV(dev);
	char *str = buf;
	int i;

	for (i = 0; i < ECARD_NUM_RESOURCES; i++)
782
		str += sprintf(str, "%08x %08x %08lx\n",
Linus Torvalds's avatar
Linus Torvalds committed
783 784 785 786 787 788 789
				ec->resource[i].start,
				ec->resource[i].end,
				ec->resource[i].flags);

	return str - buf;
}

790
static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
791 792 793 794 795
{
	struct expansion_card *ec = ECARD_DEV(dev);
	return sprintf(buf, "%u\n", ec->cid.manufacturer);
}

796
static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
797 798 799 800 801
{
	struct expansion_card *ec = ECARD_DEV(dev);
	return sprintf(buf, "%u\n", ec->cid.product);
}

802
static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
803 804
{
	struct expansion_card *ec = ECARD_DEV(dev);
805
	return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
Linus Torvalds's avatar
Linus Torvalds committed
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
}

static struct device_attribute ecard_dev_attrs[] = {
	__ATTR(device,   S_IRUGO, ecard_show_device,    NULL),
	__ATTR(dma,      S_IRUGO, ecard_show_dma,       NULL),
	__ATTR(irq,      S_IRUGO, ecard_show_irq,       NULL),
	__ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
	__ATTR(type,     S_IRUGO, ecard_show_type,      NULL),
	__ATTR(vendor,   S_IRUGO, ecard_show_vendor,    NULL),
	__ATTR_NULL,
};


int ecard_request_resources(struct expansion_card *ec)
{
	int i, err = 0;

	for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
		if (ecard_resource_end(ec, i) &&
		    !request_mem_region(ecard_resource_start(ec, i),
					ecard_resource_len(ec, i),
					ec->dev.driver->name)) {
			err = -EBUSY;
			break;
		}
	}

	if (err) {
		while (i--)
			if (ecard_resource_end(ec, i))
				release_mem_region(ecard_resource_start(ec, i),
						   ecard_resource_len(ec, i));
	}
	return err;
}
EXPORT_SYMBOL(ecard_request_resources);

void ecard_release_resources(struct expansion_card *ec)
{
	int i;

	for (i = 0; i < ECARD_NUM_RESOURCES; i++)
		if (ecard_resource_end(ec, i))
			release_mem_region(ecard_resource_start(ec, i),
					   ecard_resource_len(ec, i));
}
EXPORT_SYMBOL(ecard_release_resources);

854 855 856 857 858 859 860 861
void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
{
	ec->irq_data = irq_data;
	barrier();
	ec->ops = ops;
}
EXPORT_SYMBOL(ecard_setirq);

862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
			   unsigned long offset, unsigned long maxsize)
{
	unsigned long start = ecard_resource_start(ec, res);
	unsigned long end = ecard_resource_end(ec, res);

	if (offset > (end - start))
		return NULL;

	start += offset;
	if (maxsize && end - start > maxsize)
		end = start + maxsize;
	
	return devm_ioremap(&ec->dev, start, end - start);
}
EXPORT_SYMBOL(ecardm_iomap);

Linus Torvalds's avatar
Linus Torvalds committed
879 880 881 882 883 884
/*
 * Probe for an expansion card.
 *
 * If bit 1 of the first byte of the card is set, then the
 * card does not exist.
 */
885
static int __init ecard_probe(int slot, unsigned irq, card_type_t type)
Linus Torvalds's avatar
Linus Torvalds committed
886 887 888 889
{
	ecard_t **ecp;
	ecard_t *ec;
	struct ex_ecid cid;
890
	void __iomem *addr;
Linus Torvalds's avatar
Linus Torvalds committed
891 892 893 894 895 896 897 898 899
	int i, rc;

	ec = ecard_alloc_card(type, slot);
	if (IS_ERR(ec)) {
		rc = PTR_ERR(ec);
		goto nomem;
	}

	rc = -ENODEV;
900
	if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
		goto nodev;

	cid.r_zero = 1;
	ecard_readbytes(&cid, ec, 0, 16, 0);
	if (cid.r_zero)
		goto nodev;

	ec->cid.id	= cid.r_id;
	ec->cid.cd	= cid.r_cd;
	ec->cid.is	= cid.r_is;
	ec->cid.w	= cid.r_w;
	ec->cid.manufacturer = ecard_getu16(cid.r_manu);
	ec->cid.product = ecard_getu16(cid.r_prod);
	ec->cid.country = cid.r_country;
	ec->cid.irqmask = cid.r_irqmask;
	ec->cid.irqoff  = ecard_gets24(cid.r_irqoff);
	ec->cid.fiqmask = cid.r_fiqmask;
	ec->cid.fiqoff  = ecard_gets24(cid.r_fiqoff);
	ec->fiqaddr	=
920
	ec->irqaddr	= addr;
Linus Torvalds's avatar
Linus Torvalds committed
921 922 923 924 925 926 927 928 929 930 931

	if (ec->cid.is) {
		ec->irqmask = ec->cid.irqmask;
		ec->irqaddr += ec->cid.irqoff;
		ec->fiqmask = ec->cid.fiqmask;
		ec->fiqaddr += ec->cid.fiqoff;
	} else {
		ec->irqmask = 1;
		ec->fiqmask = 4;
	}

932
	for (i = 0; i < ARRAY_SIZE(blacklist); i++)
Linus Torvalds's avatar
Linus Torvalds committed
933 934 935 936 937 938
		if (blacklist[i].manufacturer == ec->cid.manufacturer &&
		    blacklist[i].product == ec->cid.product) {
			ec->card_desc = blacklist[i].type;
			break;
		}

939 940
	ec->irq = irq;

Linus Torvalds's avatar
Linus Torvalds committed
941 942 943 944
	/*
	 * hook the interrupt handlers
	 */
	if (slot < 8) {
945 946
		irq_set_chip_and_handler(ec->irq, &ecard_chip,
					 handle_level_irq);
947
		irq_set_chip_data(ec->irq, ec);
948
		irq_clear_status_flags(ec->irq, IRQ_NOREQUEST);
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951 952 953 954 955 956 957 958 959 960 961
	}

#ifdef CONFIG_ARCH_RPC
	/* On RiscPC, only first two slots have DMA capability */
	if (slot < 2)
		ec->dma = 2 + slot;
#endif

	for (ecp = &cards; *ecp; ecp = &(*ecp)->next);

	*ecp = ec;
	slot_to_expcard[slot] = ec;

962 963 964
	rc = device_register(&ec->dev);
	if (rc)
		goto nodev;
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

	return 0;

 nodev:
	ecard_free_card(ec);
 nomem:
	return rc;
}

/*
 * Initialise the expansion card system.
 * Locate all hardware - interrupt management and
 * actual cards.
 */
static int __init ecard_init(void)
{
981
	struct task_struct *task;
982
	int slot, irqbase;
983 984 985 986

	irqbase = irq_alloc_descs(-1, 0, 8, -1);
	if (irqbase < 0)
		return irqbase;
Linus Torvalds's avatar
Linus Torvalds committed
987

988 989
	task = kthread_run(ecard_task, NULL, "kecardd");
	if (IS_ERR(task)) {
990
		printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
991
		       PTR_ERR(task));
992
		irq_free_descs(irqbase, 8);
993
		return PTR_ERR(task);
Linus Torvalds's avatar
Linus Torvalds committed
994 995 996 997 998
	}

	printk("Probing expansion cards\n");

	for (slot = 0; slot < 8; slot ++) {
999 1000
		if (ecard_probe(slot, irqbase + slot, ECARD_EASI) == -ENODEV)
			ecard_probe(slot, irqbase + slot, ECARD_IOC);
Linus Torvalds's avatar
Linus Torvalds committed
1001 1002
	}

1003
	ecard_probe(8, 11, ECARD_IOC);
Linus Torvalds's avatar
Linus Torvalds committed
1004

1005
	irq_set_chained_handler(IRQ_EXPANSIONCARD, ecard_irq_handler);
Linus Torvalds's avatar
Linus Torvalds committed
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038

	ecard_proc_init();

	return 0;
}

subsys_initcall(ecard_init);

/*
 *	ECARD "bus"
 */
static const struct ecard_id *
ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
{
	int i;

	for (i = 0; ids[i].manufacturer != 65535; i++)
		if (ec->cid.manufacturer == ids[i].manufacturer &&
		    ec->cid.product == ids[i].product)
			return ids + i;

	return NULL;
}

static int ecard_drv_probe(struct device *dev)
{
	struct expansion_card *ec = ECARD_DEV(dev);
	struct ecard_driver *drv = ECARD_DRV(dev->driver);
	const struct ecard_id *id;
	int ret;

	id = ecard_match_device(drv->id_table, ec);

1039
	ec->claimed = 1;
Linus Torvalds's avatar
Linus Torvalds committed
1040 1041
	ret = drv->probe(ec, id);
	if (ret)
1042
		ec->claimed = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050 1051
	return ret;
}

static int ecard_drv_remove(struct device *dev)
{
	struct expansion_card *ec = ECARD_DEV(dev);
	struct ecard_driver *drv = ECARD_DRV(dev->driver);

	drv->remove(ec);
1052
	ec->claimed = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1053

1054 1055 1056 1057 1058 1059 1060 1061
	/*
	 * Restore the default operations.  We ensure that the
	 * ops are set before we change the data.
	 */
	ec->ops = &ecard_default_ops;
	barrier();
	ec->irq_data = NULL;

Linus Torvalds's avatar
Linus Torvalds committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	return 0;
}

/*
 * Before rebooting, we must make sure that the expansion card is in a
 * sensible state, so it can be re-detected.  This means that the first
 * page of the ROM must be visible.  We call the expansion cards reset
 * handler, if any.
 */
static void ecard_drv_shutdown(struct device *dev)
{
	struct expansion_card *ec = ECARD_DEV(dev);
	struct ecard_driver *drv = ECARD_DRV(dev->driver);
	struct ecard_request req;

1077 1078 1079
	if (dev->driver) {
		if (drv->shutdown)
			drv->shutdown(ec);
1080
		ec->claimed = 0;
1081
	}
Linus Torvalds's avatar
Linus Torvalds committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123

	/*
	 * If this card has a loader, call the reset handler.
	 */
	if (ec->loader) {
		req.fn = ecard_task_reset;
		req.ec = ec;
		ecard_call(&req);
	}
}

int ecard_register_driver(struct ecard_driver *drv)
{
	drv->drv.bus = &ecard_bus_type;

	return driver_register(&drv->drv);
}

void ecard_remove_driver(struct ecard_driver *drv)
{
	driver_unregister(&drv->drv);
}

static int ecard_match(struct device *_dev, struct device_driver *_drv)
{
	struct expansion_card *ec = ECARD_DEV(_dev);
	struct ecard_driver *drv = ECARD_DRV(_drv);
	int ret;

	if (drv->id_table) {
		ret = ecard_match_device(drv->id_table, ec) != NULL;
	} else {
		ret = ec->cid.id == drv->id;
	}

	return ret;
}

struct bus_type ecard_bus_type = {
	.name		= "ecard",
	.dev_attrs	= ecard_dev_attrs,
	.match		= ecard_match,
1124 1125 1126
	.probe		= ecard_drv_probe,
	.remove		= ecard_drv_remove,
	.shutdown	= ecard_drv_shutdown,
Linus Torvalds's avatar
Linus Torvalds committed
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
};

static int ecard_bus_init(void)
{
	return bus_register(&ecard_bus_type);
}

postcore_initcall(ecard_bus_init);

EXPORT_SYMBOL(ecard_readchunk);
EXPORT_SYMBOL(ecard_register_driver);
EXPORT_SYMBOL(ecard_remove_driver);
EXPORT_SYMBOL(ecard_bus_type);