blob: fcb1d59f7aeccdf9cd3ebc641630e4f4b4be504f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/kernel/ecard.c
3 *
4 * Copyright 1995-2001 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Find all installed expansion cards, and handle interrupts from them.
11 *
12 * Created from information from Acorns RiscOS3 PRMs
13 *
14 * 08-Dec-1996 RMK Added code for the 9'th expansion card - the ether
15 * podule slot.
16 * 06-May-1997 RMK Added blacklist for cards whose loader doesn't work.
17 * 12-Sep-1997 RMK Created new handling of interrupt enables/disables
18 * - cards can now register their own routine to control
19 * interrupts (recommended).
20 * 29-Sep-1997 RMK Expansion card interrupt hardware not being re-enabled
21 * on reset from Linux. (Caused cards not to respond
22 * under RiscOS without hard reset).
23 * 15-Feb-1998 RMK Added DMA support
24 * 12-Sep-1998 RMK Added EASI support
25 * 10-Jan-1999 RMK Run loaders in a simulated RISC OS environment.
26 * 17-Apr-1999 RMK Support for EASI Type C cycles.
27 */
28#define ECARD_C
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/types.h>
33#include <linux/sched.h>
34#include <linux/interrupt.h>
35#include <linux/completion.h>
36#include <linux/reboot.h>
37#include <linux/mm.h>
38#include <linux/slab.h>
39#include <linux/proc_fs.h>
Alexey Dobriyan9b001212008-04-29 01:01:49 -070040#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/device.h>
42#include <linux/init.h>
Arjan van de Ven00431702006-01-12 18:42:23 +000043#include <linux/mutex.h>
Eric W. Biederman134c99e2007-04-26 00:04:40 -070044#include <linux/kthread.h>
Russell Kingb4ac0842012-03-01 17:50:33 +000045#include <linux/irq.h>
Russell King10bdaaa2007-05-10 18:40:51 +010046#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#include <asm/dma.h>
49#include <asm/ecard.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010050#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <asm/irq.h>
52#include <asm/mmu_context.h>
53#include <asm/mach/irq.h>
54#include <asm/tlbflush.h>
55
Russell Kingc0b04d12007-05-03 10:20:47 +010056#include "ecard.h"
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct ecard_request {
59 void (*fn)(struct ecard_request *);
60 ecard_t *ec;
61 unsigned int address;
62 unsigned int length;
63 unsigned int use_loader;
64 void *buffer;
65 struct completion *complete;
66};
67
68struct expcard_blacklist {
69 unsigned short manufacturer;
70 unsigned short product;
71 const char *type;
72};
73
74static ecard_t *cards;
75static ecard_t *slot_to_expcard[MAX_ECARDS];
76static unsigned int ectcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/* List of descriptions of cards which don't have an extended
79 * identification, or chunk directories containing a description.
80 */
81static struct expcard_blacklist __initdata blacklist[] = {
82 { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
83};
84
85asmlinkage extern int
86ecard_loader_reset(unsigned long base, loader_t loader);
87asmlinkage extern int
88ecard_loader_read(int off, unsigned long base, loader_t loader);
89
90static inline unsigned short ecard_getu16(unsigned char *v)
91{
92 return v[0] | v[1] << 8;
93}
94
95static inline signed long ecard_gets24(unsigned char *v)
96{
97 return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
98}
99
100static inline ecard_t *slot_to_ecard(unsigned int slot)
101{
102 return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
103}
104
105/* ===================== Expansion card daemon ======================== */
106/*
107 * Since the loader programs on the expansion cards need to be run
108 * in a specific environment, create a separate task with this
109 * environment up, and pass requests to this task as and when we
110 * need to.
111 *
112 * This should allow 99% of loaders to be called from Linux.
113 *
114 * From a security standpoint, we trust the card vendors. This
115 * may be a misplaced trust.
116 */
117static void ecard_task_reset(struct ecard_request *req)
118{
119 struct expansion_card *ec = req->ec;
120 struct resource *res;
121
122 res = ec->slot_no == 8
123 ? &ec->resource[ECARD_RES_MEMC]
Russell King5559bca2007-05-03 10:47:37 +0100124 : ec->easi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 ? &ec->resource[ECARD_RES_EASI]
126 : &ec->resource[ECARD_RES_IOCSYNC];
127
128 ecard_loader_reset(res->start, ec->loader);
129}
130
131static void ecard_task_readbytes(struct ecard_request *req)
132{
133 struct expansion_card *ec = req->ec;
134 unsigned char *buf = req->buffer;
135 unsigned int len = req->length;
136 unsigned int off = req->address;
137
138 if (ec->slot_no == 8) {
139 void __iomem *base = (void __iomem *)
140 ec->resource[ECARD_RES_MEMC].start;
141
142 /*
143 * The card maintains an index which increments the address
144 * into a 4096-byte page on each access. We need to keep
145 * track of the counter.
146 */
147 static unsigned int index;
148 unsigned int page;
149
150 page = (off >> 12) * 4;
151 if (page > 256 * 4)
152 return;
153
154 off &= 4095;
155
156 /*
157 * If we are reading offset 0, or our current index is
158 * greater than the offset, reset the hardware index counter.
159 */
160 if (off == 0 || index > off) {
161 writeb(0, base);
162 index = 0;
163 }
164
165 /*
166 * Increment the hardware index counter until we get to the
167 * required offset. The read bytes are discarded.
168 */
169 while (index < off) {
170 readb(base + page);
171 index += 1;
172 }
173
174 while (len--) {
175 *buf++ = readb(base + page);
176 index += 1;
177 }
178 } else {
Russell King5559bca2007-05-03 10:47:37 +0100179 unsigned long base = (ec->easi
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 ? &ec->resource[ECARD_RES_EASI]
181 : &ec->resource[ECARD_RES_IOCSYNC])->start;
182 void __iomem *pbase = (void __iomem *)base;
183
184 if (!req->use_loader || !ec->loader) {
185 off *= 4;
186 while (len--) {
187 *buf++ = readb(pbase + off);
188 off += 4;
189 }
190 } else {
191 while(len--) {
192 /*
193 * The following is required by some
194 * expansion card loader programs.
195 */
196 *(unsigned long *)0x108 = 0;
197 *buf++ = ecard_loader_read(off++, base,
198 ec->loader);
199 }
200 }
201 }
202
203}
204
205static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
206static struct ecard_request *ecard_req;
Arjan van de Ven00431702006-01-12 18:42:23 +0000207static DEFINE_MUTEX(ecard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * Set up the expansion card daemon's page tables.
211 */
212static void ecard_init_pgtables(struct mm_struct *mm)
213{
214 struct vm_area_struct vma;
215
216 /* We want to set up the page tables for the following mapping:
217 * Virtual Physical
218 * 0x03000000 0x03000000
219 * 0x03010000 unmapped
220 * 0x03210000 0x03210000
221 * 0x03400000 unmapped
222 * 0x08000000 0x08000000
223 * 0x10000000 unmapped
224 *
225 * FIXME: we don't follow this 100% yet.
226 */
227 pgd_t *src_pgd, *dst_pgd;
228
229 src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
230 dst_pgd = pgd_offset(mm, IO_START);
231
232 memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
233
Russell King5e4cdb82011-07-07 11:40:52 +0100234 src_pgd = pgd_offset(mm, (unsigned long)EASI_BASE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 dst_pgd = pgd_offset(mm, EASI_START);
236
237 memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
238
Russell King81caaf22012-03-05 23:29:51 +0000239 vma.vm_flags = VM_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 vma.vm_mm = mm;
241
242 flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
243 flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
244}
245
246static int ecard_init_mm(void)
247{
248 struct mm_struct * mm = mm_alloc();
249 struct mm_struct *active_mm = current->active_mm;
250
251 if (!mm)
252 return -ENOMEM;
253
254 current->mm = mm;
255 current->active_mm = mm;
256 activate_mm(active_mm, mm);
257 mmdrop(active_mm);
258 ecard_init_pgtables(mm);
259 return 0;
260}
261
262static int
263ecard_task(void * unused)
264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
266 * Allocate a mm. We're not a lazy-TLB kernel task since we need
267 * to set page table entries where the user space would be. Note
268 * that this also creates the page tables. Failure is not an
269 * option here.
270 */
271 if (ecard_init_mm())
272 panic("kecardd: unable to alloc mm\n");
273
274 while (1) {
275 struct ecard_request *req;
276
277 wait_event_interruptible(ecard_wait, ecard_req != NULL);
278
279 req = xchg(&ecard_req, NULL);
280 if (req != NULL) {
281 req->fn(req);
282 complete(req->complete);
283 }
284 }
285}
286
287/*
288 * Wake the expansion card daemon to action our request.
289 *
290 * FIXME: The test here is not sufficient to detect if the
291 * kcardd is running.
292 */
293static void ecard_call(struct ecard_request *req)
294{
Peter Zijlstra6e9a4732006-09-30 23:28:10 -0700295 DECLARE_COMPLETION_ONSTACK(completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 req->complete = &completion;
298
Arjan van de Ven00431702006-01-12 18:42:23 +0000299 mutex_lock(&ecard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 ecard_req = req;
301 wake_up(&ecard_wait);
302
303 /*
304 * Now wait for kecardd to run.
305 */
306 wait_for_completion(&completion);
Arjan van de Ven00431702006-01-12 18:42:23 +0000307 mutex_unlock(&ecard_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
310/* ======================= Mid-level card control ===================== */
311
312static void
313ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
314{
315 struct ecard_request req;
316
317 req.fn = ecard_task_readbytes;
318 req.ec = ec;
319 req.address = off;
320 req.length = len;
321 req.use_loader = useld;
322 req.buffer = addr;
323
324 ecard_call(&req);
325}
326
327int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
328{
329 struct ex_chunk_dir excd;
330 int index = 16;
331 int useld = 0;
332
333 if (!ec->cid.cd)
334 return 0;
335
336 while(1) {
337 ecard_readbytes(&excd, ec, index, 8, useld);
338 index += 8;
339 if (c_id(&excd) == 0) {
340 if (!useld && ec->loader) {
341 useld = 1;
342 index = 0;
343 continue;
344 }
345 return 0;
346 }
347 if (c_id(&excd) == 0xf0) { /* link */
348 index = c_start(&excd);
349 continue;
350 }
351 if (c_id(&excd) == 0x80) { /* loader */
352 if (!ec->loader) {
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800353 ec->loader = kmalloc(c_len(&excd),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 GFP_KERNEL);
355 if (ec->loader)
356 ecard_readbytes(ec->loader, ec,
357 (int)c_start(&excd),
358 c_len(&excd), useld);
359 else
360 return 0;
361 }
362 continue;
363 }
364 if (c_id(&excd) == id && num-- == 0)
365 break;
366 }
367
368 if (c_id(&excd) & 0x80) {
369 switch (c_id(&excd) & 0x70) {
370 case 0x70:
371 ecard_readbytes((unsigned char *)excd.d.string, ec,
372 (int)c_start(&excd), c_len(&excd),
373 useld);
374 break;
375 case 0x00:
376 break;
377 }
378 }
379 cd->start_offset = c_start(&excd);
380 memcpy(cd->d.string, excd.d.string, 256);
381 return 1;
382}
383
384/* ======================= Interrupt control ============================ */
385
386static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
387{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
391{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394static int ecard_def_irq_pending(ecard_t *ec)
395{
396 return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
397}
398
399static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
400{
401 panic("ecard_def_fiq_enable called - impossible");
402}
403
404static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
405{
406 panic("ecard_def_fiq_disable called - impossible");
407}
408
409static int ecard_def_fiq_pending(ecard_t *ec)
410{
411 return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
412}
413
414static expansioncard_ops_t ecard_default_ops = {
415 ecard_def_irq_enable,
416 ecard_def_irq_disable,
417 ecard_def_irq_pending,
418 ecard_def_fiq_enable,
419 ecard_def_fiq_disable,
420 ecard_def_fiq_pending
421};
422
423/*
424 * Enable and disable interrupts from expansion cards.
425 * (interrupts are disabled for these functions).
426 *
427 * They are not meant to be called directly, but via enable/disable_irq.
428 */
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100429static void ecard_irq_unmask(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Russell Kingc402c112012-03-01 17:54:11 +0000431 ecard_t *ec = irq_data_get_irq_chip_data(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (ec) {
434 if (!ec->ops)
435 ec->ops = &ecard_default_ops;
436
437 if (ec->claimed && ec->ops->irqenable)
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100438 ec->ops->irqenable(ec, d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 else
440 printk(KERN_ERR "ecard: rejecting request to "
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100441 "enable IRQs for %d\n", d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443}
444
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100445static void ecard_irq_mask(struct irq_data *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Russell Kingc402c112012-03-01 17:54:11 +0000447 ecard_t *ec = irq_data_get_irq_chip_data(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (ec) {
450 if (!ec->ops)
451 ec->ops = &ecard_default_ops;
452
453 if (ec->ops && ec->ops->irqdisable)
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100454 ec->ops->irqdisable(ec, d->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456}
457
David Brownell38c677c2006-08-01 22:26:25 +0100458static struct irq_chip ecard_chip = {
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100459 .name = "ECARD",
460 .irq_ack = ecard_irq_mask,
461 .irq_mask = ecard_irq_mask,
462 .irq_unmask = ecard_irq_unmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463};
464
465void ecard_enablefiq(unsigned int fiqnr)
466{
467 ecard_t *ec = slot_to_ecard(fiqnr);
468
469 if (ec) {
470 if (!ec->ops)
471 ec->ops = &ecard_default_ops;
472
473 if (ec->claimed && ec->ops->fiqenable)
474 ec->ops->fiqenable(ec, fiqnr);
475 else
476 printk(KERN_ERR "ecard: rejecting request to "
477 "enable FIQs for %d\n", fiqnr);
478 }
479}
480
481void ecard_disablefiq(unsigned int fiqnr)
482{
483 ecard_t *ec = slot_to_ecard(fiqnr);
484
485 if (ec) {
486 if (!ec->ops)
487 ec->ops = &ecard_default_ops;
488
489 if (ec->ops->fiqdisable)
490 ec->ops->fiqdisable(ec, fiqnr);
491 }
492}
493
494static void ecard_dump_irq_state(void)
495{
496 ecard_t *ec;
497
498 printk("Expansion card IRQ state:\n");
499
500 for (ec = cards; ec; ec = ec->next) {
501 if (ec->slot_no == 8)
502 continue;
503
504 printk(" %d: %sclaimed, ",
505 ec->slot_no, ec->claimed ? "" : "not ");
506
507 if (ec->ops && ec->ops->irqpending &&
508 ec->ops != &ecard_default_ops)
509 printk("irq %spending\n",
510 ec->ops->irqpending(ec) ? "" : "not ");
511 else
512 printk("irqaddr %p, mask = %02X, status = %02X\n",
513 ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
514 }
515}
516
Russell King10dd5ce2006-11-23 11:41:32 +0000517static void ecard_check_lockup(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 static unsigned long last;
520 static int lockup;
521
522 /*
523 * If the timer interrupt has not run since the last million
524 * unrecognised expansion card interrupts, then there is
525 * something seriously wrong. Disable the expansion card
526 * interrupts so at least we can continue.
527 *
528 * Maybe we ought to start a timer to re-enable them some time
529 * later?
530 */
531 if (last == jiffies) {
532 lockup += 1;
533 if (lockup > 1000000) {
534 printk(KERN_ERR "\nInterrupt lockup detected - "
535 "disabling all expansion card interrupts\n");
536
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100537 desc->irq_data.chip->irq_mask(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 ecard_dump_irq_state();
539 }
540 } else
541 lockup = 0;
542
543 /*
544 * If we did not recognise the source of this interrupt,
545 * warn the user, but don't flood the user with these messages.
546 */
547 if (!last || time_after(jiffies, last + 5*HZ)) {
548 last = jiffies;
549 printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
550 ecard_dump_irq_state();
551 }
552}
553
554static void
Russell King10dd5ce2006-11-23 11:41:32 +0000555ecard_irq_handler(unsigned int irq, struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 ecard_t *ec;
558 int called = 0;
559
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100560 desc->irq_data.chip->irq_mask(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 for (ec = cards; ec; ec = ec->next) {
562 int pending;
563
Russell King41569e32012-01-18 15:41:44 +0000564 if (!ec->claimed || !ec->irq || ec->slot_no == 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 continue;
566
567 if (ec->ops && ec->ops->irqpending)
568 pending = ec->ops->irqpending(ec);
569 else
570 pending = ecard_default_ops.irqpending(ec);
571
572 if (pending) {
Dmitry Baryshkovd8aa0252008-10-09 13:36:24 +0100573 generic_handle_irq(ec->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 called ++;
575 }
576 }
Lennert Buytenhek4a87bac2010-11-29 10:21:01 +0100577 desc->irq_data.chip->irq_unmask(&desc->irq_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (called == 0)
580 ecard_check_lockup(desc);
581}
582
Russell King1ace7562011-07-07 10:56:41 +0100583static void __iomem *__ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Russell King06cf0b52011-07-07 11:07:36 +0100585 void __iomem *address = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 int slot = ec->slot_no;
587
588 if (ec->slot_no == 8)
Russell King06cf0b52011-07-07 11:07:36 +0100589 return ECARD_MEMC8_BASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 ectcr &= ~(1 << slot);
592
593 switch (type) {
594 case ECARD_MEMC:
595 if (slot < 4)
Russell King06cf0b52011-07-07 11:07:36 +0100596 address = ECARD_MEMC_BASE + (slot << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 break;
598
599 case ECARD_IOC:
600 if (slot < 4)
Russell King06cf0b52011-07-07 11:07:36 +0100601 address = ECARD_IOC_BASE + (slot << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 else
Russell King06cf0b52011-07-07 11:07:36 +0100603 address = ECARD_IOC4_BASE + ((slot - 4) << 14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (address)
Russell King06cf0b52011-07-07 11:07:36 +0100605 address += speed << 19;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 case ECARD_EASI:
Russell King06cf0b52011-07-07 11:07:36 +0100609 address = ECARD_EASI_BASE + (slot << 24);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (speed == ECARD_FAST)
611 ectcr |= 1 << slot;
612 break;
Russell King06cf0b52011-07-07 11:07:36 +0100613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 default:
615 break;
616 }
617
618#ifdef IOMD_ECTCR
619 iomd_writeb(ectcr, IOMD_ECTCR);
620#endif
Russell King06cf0b52011-07-07 11:07:36 +0100621 return address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700624static int ecard_prints(struct seq_file *m, ecard_t *ec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700626 seq_printf(m, " %d: %s ", ec->slot_no, ec->easi ? "EASI" : " ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (ec->cid.id == 0) {
629 struct in_chunk_dir incd;
630
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700631 seq_printf(m, "[%04X:%04X] ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 ec->cid.manufacturer, ec->cid.product);
633
634 if (!ec->card_desc && ec->cid.cd &&
635 ecard_readchunk(&incd, ec, 0xf5, 0)) {
636 ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
637
638 if (ec->card_desc)
639 strcpy((char *)ec->card_desc, incd.d.string);
640 }
641
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700642 seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 } else
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700644 seq_printf(m, "Simple card %d\n", ec->cid.id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700649static int ecard_devices_proc_show(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
651 ecard_t *ec = cards;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700653 while (ec) {
654 ecard_prints(m, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 ec = ec->next;
656 }
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700657 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700660static int ecard_devices_proc_open(struct inode *inode, struct file *file)
661{
662 return single_open(file, ecard_devices_proc_show, NULL);
663}
664
665static const struct file_operations bus_ecard_proc_fops = {
666 .owner = THIS_MODULE,
667 .open = ecard_devices_proc_open,
668 .read = seq_read,
669 .llseek = seq_lseek,
670 .release = single_release,
671};
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
674
675static void ecard_proc_init(void)
676{
Alexey Dobriyan9c370662008-04-29 01:01:41 -0700677 proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
Alexey Dobriyan9b001212008-04-29 01:01:49 -0700678 proc_create("devices", 0, proc_bus_ecard_dir, &bus_ecard_proc_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
681#define ec_set_resource(ec,nr,st,sz) \
682 do { \
Kay Sievers3f978702008-05-30 17:42:11 +0200683 (ec)->resource[nr].name = dev_name(&ec->dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 (ec)->resource[nr].start = st; \
685 (ec)->resource[nr].end = (st) + (sz) - 1; \
686 (ec)->resource[nr].flags = IORESOURCE_MEM; \
687 } while (0)
688
689static void __init ecard_free_card(struct expansion_card *ec)
690{
691 int i;
692
693 for (i = 0; i < ECARD_NUM_RESOURCES; i++)
694 if (ec->resource[i].flags)
695 release_resource(&ec->resource[i]);
696
697 kfree(ec);
698}
699
700static struct expansion_card *__init ecard_alloc_card(int type, int slot)
701{
702 struct expansion_card *ec;
703 unsigned long base;
704 int i;
705
Russell Kingd2a02b92006-03-20 19:46:41 +0000706 ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!ec) {
708 ec = ERR_PTR(-ENOMEM);
709 goto nomem;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ec->slot_no = slot;
Russell King5559bca2007-05-03 10:47:37 +0100713 ec->easi = type == ECARD_EASI;
Russell King41569e32012-01-18 15:41:44 +0000714 ec->irq = 0;
715 ec->fiq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 ec->dma = NO_DMA;
717 ec->ops = &ecard_default_ops;
718
Kay Sievers1d559e22009-01-06 10:44:43 -0800719 dev_set_name(&ec->dev, "ecard%d", slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ec->dev.parent = NULL;
721 ec->dev.bus = &ecard_bus_type;
722 ec->dev.dma_mask = &ec->dma_mask;
723 ec->dma_mask = (u64)0xffffffff;
Russell King69f4f332007-04-02 13:53:15 +0100724 ec->dev.coherent_dma_mask = ec->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (slot < 4) {
727 ec_set_resource(ec, ECARD_RES_MEMC,
728 PODSLOT_MEMC_BASE + (slot << 14),
729 PODSLOT_MEMC_SIZE);
730 base = PODSLOT_IOC0_BASE + (slot << 14);
731 } else
732 base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
733
734#ifdef CONFIG_ARCH_RPC
735 if (slot < 8) {
736 ec_set_resource(ec, ECARD_RES_EASI,
737 PODSLOT_EASI_BASE + (slot << 24),
738 PODSLOT_EASI_SIZE);
739 }
740
741 if (slot == 8) {
742 ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
743 } else
744#endif
745
746 for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
747 ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
748 base + (i << 19), PODSLOT_IOC_SIZE);
749
750 for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
751 if (ec->resource[i].flags &&
752 request_resource(&iomem_resource, &ec->resource[i])) {
Greg Kroah-Hartmanfc3a8822008-05-02 06:02:41 +0200753 dev_err(&ec->dev, "resource(s) not available\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ec->resource[i].end -= ec->resource[i].start;
755 ec->resource[i].start = 0;
756 ec->resource[i].flags = 0;
757 }
758 }
759
760 nomem:
761 return ec;
762}
763
Yani Ioannouff381d22005-05-17 06:40:51 -0400764static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct expansion_card *ec = ECARD_DEV(dev);
767 return sprintf(buf, "%u\n", ec->irq);
768}
769
Yani Ioannouff381d22005-05-17 06:40:51 -0400770static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct expansion_card *ec = ECARD_DEV(dev);
773 return sprintf(buf, "%u\n", ec->dma);
774}
775
Yani Ioannouff381d22005-05-17 06:40:51 -0400776static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct expansion_card *ec = ECARD_DEV(dev);
779 char *str = buf;
780 int i;
781
782 for (i = 0; i < ECARD_NUM_RESOURCES; i++)
Russell Kingc9e41432006-07-03 13:29:03 +0100783 str += sprintf(str, "%08x %08x %08lx\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 ec->resource[i].start,
785 ec->resource[i].end,
786 ec->resource[i].flags);
787
788 return str - buf;
789}
790
Yani Ioannouff381d22005-05-17 06:40:51 -0400791static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct expansion_card *ec = ECARD_DEV(dev);
794 return sprintf(buf, "%u\n", ec->cid.manufacturer);
795}
796
Yani Ioannouff381d22005-05-17 06:40:51 -0400797static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct expansion_card *ec = ECARD_DEV(dev);
800 return sprintf(buf, "%u\n", ec->cid.product);
801}
802
Yani Ioannouff381d22005-05-17 06:40:51 -0400803static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
805 struct expansion_card *ec = ECARD_DEV(dev);
Russell King5559bca2007-05-03 10:47:37 +0100806 return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static struct device_attribute ecard_dev_attrs[] = {
810 __ATTR(device, S_IRUGO, ecard_show_device, NULL),
811 __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
812 __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
813 __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
814 __ATTR(type, S_IRUGO, ecard_show_type, NULL),
815 __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
816 __ATTR_NULL,
817};
818
819
820int ecard_request_resources(struct expansion_card *ec)
821{
822 int i, err = 0;
823
824 for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
825 if (ecard_resource_end(ec, i) &&
826 !request_mem_region(ecard_resource_start(ec, i),
827 ecard_resource_len(ec, i),
828 ec->dev.driver->name)) {
829 err = -EBUSY;
830 break;
831 }
832 }
833
834 if (err) {
835 while (i--)
836 if (ecard_resource_end(ec, i))
837 release_mem_region(ecard_resource_start(ec, i),
838 ecard_resource_len(ec, i));
839 }
840 return err;
841}
842EXPORT_SYMBOL(ecard_request_resources);
843
844void ecard_release_resources(struct expansion_card *ec)
845{
846 int i;
847
848 for (i = 0; i < ECARD_NUM_RESOURCES; i++)
849 if (ecard_resource_end(ec, i))
850 release_mem_region(ecard_resource_start(ec, i),
851 ecard_resource_len(ec, i));
852}
853EXPORT_SYMBOL(ecard_release_resources);
854
Russell Kingc7b87f32007-05-10 16:46:13 +0100855void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
856{
857 ec->irq_data = irq_data;
858 barrier();
859 ec->ops = ops;
860}
861EXPORT_SYMBOL(ecard_setirq);
862
Russell King10bdaaa2007-05-10 18:40:51 +0100863void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
864 unsigned long offset, unsigned long maxsize)
865{
866 unsigned long start = ecard_resource_start(ec, res);
867 unsigned long end = ecard_resource_end(ec, res);
868
869 if (offset > (end - start))
870 return NULL;
871
872 start += offset;
873 if (maxsize && end - start > maxsize)
874 end = start + maxsize;
875
876 return devm_ioremap(&ec->dev, start, end - start);
877}
878EXPORT_SYMBOL(ecardm_iomap);
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * Probe for an expansion card.
882 *
883 * If bit 1 of the first byte of the card is set, then the
884 * card does not exist.
885 */
Russell Kingb4ac0842012-03-01 17:50:33 +0000886static int __init ecard_probe(int slot, unsigned irq, card_type_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
888 ecard_t **ecp;
889 ecard_t *ec;
890 struct ex_ecid cid;
Russell King1ace7562011-07-07 10:56:41 +0100891 void __iomem *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 int i, rc;
893
894 ec = ecard_alloc_card(type, slot);
895 if (IS_ERR(ec)) {
896 rc = PTR_ERR(ec);
897 goto nomem;
898 }
899
900 rc = -ENODEV;
Russell King1ace7562011-07-07 10:56:41 +0100901 if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto nodev;
903
904 cid.r_zero = 1;
905 ecard_readbytes(&cid, ec, 0, 16, 0);
906 if (cid.r_zero)
907 goto nodev;
908
909 ec->cid.id = cid.r_id;
910 ec->cid.cd = cid.r_cd;
911 ec->cid.is = cid.r_is;
912 ec->cid.w = cid.r_w;
913 ec->cid.manufacturer = ecard_getu16(cid.r_manu);
914 ec->cid.product = ecard_getu16(cid.r_prod);
915 ec->cid.country = cid.r_country;
916 ec->cid.irqmask = cid.r_irqmask;
917 ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
918 ec->cid.fiqmask = cid.r_fiqmask;
919 ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
920 ec->fiqaddr =
Russell King1ace7562011-07-07 10:56:41 +0100921 ec->irqaddr = addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (ec->cid.is) {
924 ec->irqmask = ec->cid.irqmask;
925 ec->irqaddr += ec->cid.irqoff;
926 ec->fiqmask = ec->cid.fiqmask;
927 ec->fiqaddr += ec->cid.fiqoff;
928 } else {
929 ec->irqmask = 1;
930 ec->fiqmask = 4;
931 }
932
Ahmed S. Darwisheeea82f2007-02-05 16:10:25 -0800933 for (i = 0; i < ARRAY_SIZE(blacklist); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (blacklist[i].manufacturer == ec->cid.manufacturer &&
935 blacklist[i].product == ec->cid.product) {
936 ec->card_desc = blacklist[i].type;
937 break;
938 }
939
Russell Kingb4ac0842012-03-01 17:50:33 +0000940 ec->irq = irq;
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /*
943 * hook the interrupt handlers
944 */
945 if (slot < 8) {
Thomas Gleixnerf38c02f2011-03-24 13:35:09 +0100946 irq_set_chip_and_handler(ec->irq, &ecard_chip,
947 handle_level_irq);
Russell Kingc402c112012-03-01 17:54:11 +0000948 irq_set_chip_data(ec->irq, ec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 set_irq_flags(ec->irq, IRQF_VALID);
950 }
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952#ifdef CONFIG_ARCH_RPC
953 /* On RiscPC, only first two slots have DMA capability */
954 if (slot < 2)
955 ec->dma = 2 + slot;
956#endif
957
958 for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
959
960 *ecp = ec;
961 slot_to_expcard[slot] = ec;
962
Arnd Bergmann5f078092012-04-30 16:26:01 +0000963 rc = device_register(&ec->dev);
964 if (rc)
965 goto nodev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
967 return 0;
968
969 nodev:
970 ecard_free_card(ec);
971 nomem:
972 return rc;
973}
974
975/*
976 * Initialise the expansion card system.
977 * Locate all hardware - interrupt management and
978 * actual cards.
979 */
980static int __init ecard_init(void)
981{
Eric W. Biederman134c99e2007-04-26 00:04:40 -0700982 struct task_struct *task;
Russell King530c2ea2012-03-01 18:46:46 +0000983 int slot, irqbase;
Russell Kingb4ac0842012-03-01 17:50:33 +0000984
985 irqbase = irq_alloc_descs(-1, 0, 8, -1);
986 if (irqbase < 0)
987 return irqbase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Eric W. Biederman134c99e2007-04-26 00:04:40 -0700989 task = kthread_run(ecard_task, NULL, "kecardd");
990 if (IS_ERR(task)) {
Russell Kinge6aeb472007-05-03 10:55:46 +0100991 printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
Eric W. Biederman134c99e2007-04-26 00:04:40 -0700992 PTR_ERR(task));
Russell Kingb4ac0842012-03-01 17:50:33 +0000993 irq_free_descs(irqbase, 8);
Eric W. Biederman134c99e2007-04-26 00:04:40 -0700994 return PTR_ERR(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996
997 printk("Probing expansion cards\n");
998
999 for (slot = 0; slot < 8; slot ++) {
Russell Kingb4ac0842012-03-01 17:50:33 +00001000 if (ecard_probe(slot, irqbase + slot, ECARD_EASI) == -ENODEV)
1001 ecard_probe(slot, irqbase + slot, ECARD_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003
Russell Kingb4ac0842012-03-01 17:50:33 +00001004 ecard_probe(8, 11, ECARD_IOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Russell King530c2ea2012-03-01 18:46:46 +00001006 irq_set_chained_handler(IRQ_EXPANSIONCARD, ecard_irq_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 ecard_proc_init();
1009
1010 return 0;
1011}
1012
1013subsys_initcall(ecard_init);
1014
1015/*
1016 * ECARD "bus"
1017 */
1018static const struct ecard_id *
1019ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
1020{
1021 int i;
1022
1023 for (i = 0; ids[i].manufacturer != 65535; i++)
1024 if (ec->cid.manufacturer == ids[i].manufacturer &&
1025 ec->cid.product == ids[i].product)
1026 return ids + i;
1027
1028 return NULL;
1029}
1030
1031static int ecard_drv_probe(struct device *dev)
1032{
1033 struct expansion_card *ec = ECARD_DEV(dev);
1034 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1035 const struct ecard_id *id;
1036 int ret;
1037
1038 id = ecard_match_device(drv->id_table, ec);
1039
Russell King9ecba1f2008-04-19 16:01:38 +01001040 ec->claimed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 ret = drv->probe(ec, id);
1042 if (ret)
Russell King9ecba1f2008-04-19 16:01:38 +01001043 ec->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return ret;
1045}
1046
1047static int ecard_drv_remove(struct device *dev)
1048{
1049 struct expansion_card *ec = ECARD_DEV(dev);
1050 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1051
1052 drv->remove(ec);
Russell King9ecba1f2008-04-19 16:01:38 +01001053 ec->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Russell Kingc7b87f32007-05-10 16:46:13 +01001055 /*
1056 * Restore the default operations. We ensure that the
1057 * ops are set before we change the data.
1058 */
1059 ec->ops = &ecard_default_ops;
1060 barrier();
1061 ec->irq_data = NULL;
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return 0;
1064}
1065
1066/*
1067 * Before rebooting, we must make sure that the expansion card is in a
1068 * sensible state, so it can be re-detected. This means that the first
1069 * page of the ROM must be visible. We call the expansion cards reset
1070 * handler, if any.
1071 */
1072static void ecard_drv_shutdown(struct device *dev)
1073{
1074 struct expansion_card *ec = ECARD_DEV(dev);
1075 struct ecard_driver *drv = ECARD_DRV(dev->driver);
1076 struct ecard_request req;
1077
Russell Kinge08b7542006-01-05 14:30:57 +00001078 if (dev->driver) {
1079 if (drv->shutdown)
1080 drv->shutdown(ec);
Russell King9ecba1f2008-04-19 16:01:38 +01001081 ec->claimed = 0;
Russell Kinge08b7542006-01-05 14:30:57 +00001082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
1085 * If this card has a loader, call the reset handler.
1086 */
1087 if (ec->loader) {
1088 req.fn = ecard_task_reset;
1089 req.ec = ec;
1090 ecard_call(&req);
1091 }
1092}
1093
1094int ecard_register_driver(struct ecard_driver *drv)
1095{
1096 drv->drv.bus = &ecard_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 return driver_register(&drv->drv);
1099}
1100
1101void ecard_remove_driver(struct ecard_driver *drv)
1102{
1103 driver_unregister(&drv->drv);
1104}
1105
1106static int ecard_match(struct device *_dev, struct device_driver *_drv)
1107{
1108 struct expansion_card *ec = ECARD_DEV(_dev);
1109 struct ecard_driver *drv = ECARD_DRV(_drv);
1110 int ret;
1111
1112 if (drv->id_table) {
1113 ret = ecard_match_device(drv->id_table, ec) != NULL;
1114 } else {
1115 ret = ec->cid.id == drv->id;
1116 }
1117
1118 return ret;
1119}
1120
1121struct bus_type ecard_bus_type = {
1122 .name = "ecard",
1123 .dev_attrs = ecard_dev_attrs,
1124 .match = ecard_match,
Russell Kinge08b7542006-01-05 14:30:57 +00001125 .probe = ecard_drv_probe,
1126 .remove = ecard_drv_remove,
1127 .shutdown = ecard_drv_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128};
1129
1130static int ecard_bus_init(void)
1131{
1132 return bus_register(&ecard_bus_type);
1133}
1134
1135postcore_initcall(ecard_bus_init);
1136
1137EXPORT_SYMBOL(ecard_readchunk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138EXPORT_SYMBOL(ecard_register_driver);
1139EXPORT_SYMBOL(ecard_remove_driver);
1140EXPORT_SYMBOL(ecard_bus_type);