ecard.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  1. /*
  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. #include <linux/config.h>
  30. #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>
  40. #include <linux/device.h>
  41. #include <linux/init.h>
  42. #include <asm/dma.h>
  43. #include <asm/ecard.h>
  44. #include <asm/hardware.h>
  45. #include <asm/io.h>
  46. #include <asm/irq.h>
  47. #include <asm/mmu_context.h>
  48. #include <asm/mach/irq.h>
  49. #include <asm/tlbflush.h>
  50. #ifndef CONFIG_ARCH_RPC
  51. #define HAVE_EXPMASK
  52. #endif
  53. struct ecard_request {
  54. void (*fn)(struct ecard_request *);
  55. ecard_t *ec;
  56. unsigned int address;
  57. unsigned int length;
  58. unsigned int use_loader;
  59. void *buffer;
  60. struct completion *complete;
  61. };
  62. struct expcard_blacklist {
  63. unsigned short manufacturer;
  64. unsigned short product;
  65. const char *type;
  66. };
  67. static ecard_t *cards;
  68. static ecard_t *slot_to_expcard[MAX_ECARDS];
  69. static unsigned int ectcr;
  70. #ifdef HAS_EXPMASK
  71. static unsigned int have_expmask;
  72. #endif
  73. /* List of descriptions of cards which don't have an extended
  74. * identification, or chunk directories containing a description.
  75. */
  76. static struct expcard_blacklist __initdata blacklist[] = {
  77. { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
  78. };
  79. asmlinkage extern int
  80. ecard_loader_reset(unsigned long base, loader_t loader);
  81. asmlinkage extern int
  82. ecard_loader_read(int off, unsigned long base, loader_t loader);
  83. static inline unsigned short ecard_getu16(unsigned char *v)
  84. {
  85. return v[0] | v[1] << 8;
  86. }
  87. static inline signed long ecard_gets24(unsigned char *v)
  88. {
  89. return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
  90. }
  91. static inline ecard_t *slot_to_ecard(unsigned int slot)
  92. {
  93. return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
  94. }
  95. /* ===================== Expansion card daemon ======================== */
  96. /*
  97. * Since the loader programs on the expansion cards need to be run
  98. * in a specific environment, create a separate task with this
  99. * environment up, and pass requests to this task as and when we
  100. * need to.
  101. *
  102. * This should allow 99% of loaders to be called from Linux.
  103. *
  104. * From a security standpoint, we trust the card vendors. This
  105. * may be a misplaced trust.
  106. */
  107. static void ecard_task_reset(struct ecard_request *req)
  108. {
  109. struct expansion_card *ec = req->ec;
  110. struct resource *res;
  111. res = ec->slot_no == 8
  112. ? &ec->resource[ECARD_RES_MEMC]
  113. : ec->type == ECARD_EASI
  114. ? &ec->resource[ECARD_RES_EASI]
  115. : &ec->resource[ECARD_RES_IOCSYNC];
  116. ecard_loader_reset(res->start, ec->loader);
  117. }
  118. static void ecard_task_readbytes(struct ecard_request *req)
  119. {
  120. struct expansion_card *ec = req->ec;
  121. unsigned char *buf = req->buffer;
  122. unsigned int len = req->length;
  123. unsigned int off = req->address;
  124. if (ec->slot_no == 8) {
  125. void __iomem *base = (void __iomem *)
  126. ec->resource[ECARD_RES_MEMC].start;
  127. /*
  128. * The card maintains an index which increments the address
  129. * into a 4096-byte page on each access. We need to keep
  130. * track of the counter.
  131. */
  132. static unsigned int index;
  133. unsigned int page;
  134. page = (off >> 12) * 4;
  135. if (page > 256 * 4)
  136. return;
  137. off &= 4095;
  138. /*
  139. * If we are reading offset 0, or our current index is
  140. * greater than the offset, reset the hardware index counter.
  141. */
  142. if (off == 0 || index > off) {
  143. writeb(0, base);
  144. index = 0;
  145. }
  146. /*
  147. * Increment the hardware index counter until we get to the
  148. * required offset. The read bytes are discarded.
  149. */
  150. while (index < off) {
  151. readb(base + page);
  152. index += 1;
  153. }
  154. while (len--) {
  155. *buf++ = readb(base + page);
  156. index += 1;
  157. }
  158. } else {
  159. unsigned long base = (ec->type == ECARD_EASI
  160. ? &ec->resource[ECARD_RES_EASI]
  161. : &ec->resource[ECARD_RES_IOCSYNC])->start;
  162. void __iomem *pbase = (void __iomem *)base;
  163. if (!req->use_loader || !ec->loader) {
  164. off *= 4;
  165. while (len--) {
  166. *buf++ = readb(pbase + off);
  167. off += 4;
  168. }
  169. } else {
  170. while(len--) {
  171. /*
  172. * The following is required by some
  173. * expansion card loader programs.
  174. */
  175. *(unsigned long *)0x108 = 0;
  176. *buf++ = ecard_loader_read(off++, base,
  177. ec->loader);
  178. }
  179. }
  180. }
  181. }
  182. static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
  183. static struct ecard_request *ecard_req;
  184. static DECLARE_MUTEX(ecard_sem);
  185. /*
  186. * Set up the expansion card daemon's page tables.
  187. */
  188. static void ecard_init_pgtables(struct mm_struct *mm)
  189. {
  190. struct vm_area_struct vma;
  191. /* We want to set up the page tables for the following mapping:
  192. * Virtual Physical
  193. * 0x03000000 0x03000000
  194. * 0x03010000 unmapped
  195. * 0x03210000 0x03210000
  196. * 0x03400000 unmapped
  197. * 0x08000000 0x08000000
  198. * 0x10000000 unmapped
  199. *
  200. * FIXME: we don't follow this 100% yet.
  201. */
  202. pgd_t *src_pgd, *dst_pgd;
  203. src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
  204. dst_pgd = pgd_offset(mm, IO_START);
  205. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
  206. src_pgd = pgd_offset(mm, EASI_BASE);
  207. dst_pgd = pgd_offset(mm, EASI_START);
  208. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
  209. vma.vm_mm = mm;
  210. flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
  211. flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
  212. }
  213. static int ecard_init_mm(void)
  214. {
  215. struct mm_struct * mm = mm_alloc();
  216. struct mm_struct *active_mm = current->active_mm;
  217. if (!mm)
  218. return -ENOMEM;
  219. current->mm = mm;
  220. current->active_mm = mm;
  221. activate_mm(active_mm, mm);
  222. mmdrop(active_mm);
  223. ecard_init_pgtables(mm);
  224. return 0;
  225. }
  226. static int
  227. ecard_task(void * unused)
  228. {
  229. daemonize("kecardd");
  230. /*
  231. * Allocate a mm. We're not a lazy-TLB kernel task since we need
  232. * to set page table entries where the user space would be. Note
  233. * that this also creates the page tables. Failure is not an
  234. * option here.
  235. */
  236. if (ecard_init_mm())
  237. panic("kecardd: unable to alloc mm\n");
  238. while (1) {
  239. struct ecard_request *req;
  240. wait_event_interruptible(ecard_wait, ecard_req != NULL);
  241. req = xchg(&ecard_req, NULL);
  242. if (req != NULL) {
  243. req->fn(req);
  244. complete(req->complete);
  245. }
  246. }
  247. }
  248. /*
  249. * Wake the expansion card daemon to action our request.
  250. *
  251. * FIXME: The test here is not sufficient to detect if the
  252. * kcardd is running.
  253. */
  254. static void ecard_call(struct ecard_request *req)
  255. {
  256. DECLARE_COMPLETION(completion);
  257. req->complete = &completion;
  258. down(&ecard_sem);
  259. ecard_req = req;
  260. wake_up(&ecard_wait);
  261. /*
  262. * Now wait for kecardd to run.
  263. */
  264. wait_for_completion(&completion);
  265. up(&ecard_sem);
  266. }
  267. /* ======================= Mid-level card control ===================== */
  268. static void
  269. ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
  270. {
  271. struct ecard_request req;
  272. req.fn = ecard_task_readbytes;
  273. req.ec = ec;
  274. req.address = off;
  275. req.length = len;
  276. req.use_loader = useld;
  277. req.buffer = addr;
  278. ecard_call(&req);
  279. }
  280. int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
  281. {
  282. struct ex_chunk_dir excd;
  283. int index = 16;
  284. int useld = 0;
  285. if (!ec->cid.cd)
  286. return 0;
  287. while(1) {
  288. ecard_readbytes(&excd, ec, index, 8, useld);
  289. index += 8;
  290. if (c_id(&excd) == 0) {
  291. if (!useld && ec->loader) {
  292. useld = 1;
  293. index = 0;
  294. continue;
  295. }
  296. return 0;
  297. }
  298. if (c_id(&excd) == 0xf0) { /* link */
  299. index = c_start(&excd);
  300. continue;
  301. }
  302. if (c_id(&excd) == 0x80) { /* loader */
  303. if (!ec->loader) {
  304. ec->loader = (loader_t)kmalloc(c_len(&excd),
  305. GFP_KERNEL);
  306. if (ec->loader)
  307. ecard_readbytes(ec->loader, ec,
  308. (int)c_start(&excd),
  309. c_len(&excd), useld);
  310. else
  311. return 0;
  312. }
  313. continue;
  314. }
  315. if (c_id(&excd) == id && num-- == 0)
  316. break;
  317. }
  318. if (c_id(&excd) & 0x80) {
  319. switch (c_id(&excd) & 0x70) {
  320. case 0x70:
  321. ecard_readbytes((unsigned char *)excd.d.string, ec,
  322. (int)c_start(&excd), c_len(&excd),
  323. useld);
  324. break;
  325. case 0x00:
  326. break;
  327. }
  328. }
  329. cd->start_offset = c_start(&excd);
  330. memcpy(cd->d.string, excd.d.string, 256);
  331. return 1;
  332. }
  333. /* ======================= Interrupt control ============================ */
  334. static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
  335. {
  336. #ifdef HAS_EXPMASK
  337. if (irqnr < 4 && have_expmask) {
  338. have_expmask |= 1 << irqnr;
  339. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  340. }
  341. #endif
  342. }
  343. static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
  344. {
  345. #ifdef HAS_EXPMASK
  346. if (irqnr < 4 && have_expmask) {
  347. have_expmask &= ~(1 << irqnr);
  348. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  349. }
  350. #endif
  351. }
  352. static int ecard_def_irq_pending(ecard_t *ec)
  353. {
  354. return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
  355. }
  356. static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
  357. {
  358. panic("ecard_def_fiq_enable called - impossible");
  359. }
  360. static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
  361. {
  362. panic("ecard_def_fiq_disable called - impossible");
  363. }
  364. static int ecard_def_fiq_pending(ecard_t *ec)
  365. {
  366. return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
  367. }
  368. static expansioncard_ops_t ecard_default_ops = {
  369. ecard_def_irq_enable,
  370. ecard_def_irq_disable,
  371. ecard_def_irq_pending,
  372. ecard_def_fiq_enable,
  373. ecard_def_fiq_disable,
  374. ecard_def_fiq_pending
  375. };
  376. /*
  377. * Enable and disable interrupts from expansion cards.
  378. * (interrupts are disabled for these functions).
  379. *
  380. * They are not meant to be called directly, but via enable/disable_irq.
  381. */
  382. static void ecard_irq_unmask(unsigned int irqnr)
  383. {
  384. ecard_t *ec = slot_to_ecard(irqnr - 32);
  385. if (ec) {
  386. if (!ec->ops)
  387. ec->ops = &ecard_default_ops;
  388. if (ec->claimed && ec->ops->irqenable)
  389. ec->ops->irqenable(ec, irqnr);
  390. else
  391. printk(KERN_ERR "ecard: rejecting request to "
  392. "enable IRQs for %d\n", irqnr);
  393. }
  394. }
  395. static void ecard_irq_mask(unsigned int irqnr)
  396. {
  397. ecard_t *ec = slot_to_ecard(irqnr - 32);
  398. if (ec) {
  399. if (!ec->ops)
  400. ec->ops = &ecard_default_ops;
  401. if (ec->ops && ec->ops->irqdisable)
  402. ec->ops->irqdisable(ec, irqnr);
  403. }
  404. }
  405. static struct irqchip ecard_chip = {
  406. .ack = ecard_irq_mask,
  407. .mask = ecard_irq_mask,
  408. .unmask = ecard_irq_unmask,
  409. };
  410. void ecard_enablefiq(unsigned int fiqnr)
  411. {
  412. ecard_t *ec = slot_to_ecard(fiqnr);
  413. if (ec) {
  414. if (!ec->ops)
  415. ec->ops = &ecard_default_ops;
  416. if (ec->claimed && ec->ops->fiqenable)
  417. ec->ops->fiqenable(ec, fiqnr);
  418. else
  419. printk(KERN_ERR "ecard: rejecting request to "
  420. "enable FIQs for %d\n", fiqnr);
  421. }
  422. }
  423. void ecard_disablefiq(unsigned int fiqnr)
  424. {
  425. ecard_t *ec = slot_to_ecard(fiqnr);
  426. if (ec) {
  427. if (!ec->ops)
  428. ec->ops = &ecard_default_ops;
  429. if (ec->ops->fiqdisable)
  430. ec->ops->fiqdisable(ec, fiqnr);
  431. }
  432. }
  433. static void ecard_dump_irq_state(void)
  434. {
  435. ecard_t *ec;
  436. printk("Expansion card IRQ state:\n");
  437. for (ec = cards; ec; ec = ec->next) {
  438. if (ec->slot_no == 8)
  439. continue;
  440. printk(" %d: %sclaimed, ",
  441. ec->slot_no, ec->claimed ? "" : "not ");
  442. if (ec->ops && ec->ops->irqpending &&
  443. ec->ops != &ecard_default_ops)
  444. printk("irq %spending\n",
  445. ec->ops->irqpending(ec) ? "" : "not ");
  446. else
  447. printk("irqaddr %p, mask = %02X, status = %02X\n",
  448. ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
  449. }
  450. }
  451. static void ecard_check_lockup(struct irqdesc *desc)
  452. {
  453. static unsigned long last;
  454. static int lockup;
  455. /*
  456. * If the timer interrupt has not run since the last million
  457. * unrecognised expansion card interrupts, then there is
  458. * something seriously wrong. Disable the expansion card
  459. * interrupts so at least we can continue.
  460. *
  461. * Maybe we ought to start a timer to re-enable them some time
  462. * later?
  463. */
  464. if (last == jiffies) {
  465. lockup += 1;
  466. if (lockup > 1000000) {
  467. printk(KERN_ERR "\nInterrupt lockup detected - "
  468. "disabling all expansion card interrupts\n");
  469. desc->chip->mask(IRQ_EXPANSIONCARD);
  470. ecard_dump_irq_state();
  471. }
  472. } else
  473. lockup = 0;
  474. /*
  475. * If we did not recognise the source of this interrupt,
  476. * warn the user, but don't flood the user with these messages.
  477. */
  478. if (!last || time_after(jiffies, last + 5*HZ)) {
  479. last = jiffies;
  480. printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
  481. ecard_dump_irq_state();
  482. }
  483. }
  484. static void
  485. ecard_irq_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  486. {
  487. ecard_t *ec;
  488. int called = 0;
  489. desc->chip->mask(irq);
  490. for (ec = cards; ec; ec = ec->next) {
  491. int pending;
  492. if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
  493. continue;
  494. if (ec->ops && ec->ops->irqpending)
  495. pending = ec->ops->irqpending(ec);
  496. else
  497. pending = ecard_default_ops.irqpending(ec);
  498. if (pending) {
  499. struct irqdesc *d = irq_desc + ec->irq;
  500. desc_handle_irq(ec->irq, d, regs);
  501. called ++;
  502. }
  503. }
  504. desc->chip->unmask(irq);
  505. if (called == 0)
  506. ecard_check_lockup(desc);
  507. }
  508. #ifdef HAS_EXPMASK
  509. static unsigned char priority_masks[] =
  510. {
  511. 0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
  512. };
  513. static unsigned char first_set[] =
  514. {
  515. 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
  516. 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
  517. };
  518. static void
  519. ecard_irqexp_handler(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs)
  520. {
  521. const unsigned int statusmask = 15;
  522. unsigned int status;
  523. status = __raw_readb(EXPMASK_STATUS) & statusmask;
  524. if (status) {
  525. unsigned int slot = first_set[status];
  526. ecard_t *ec = slot_to_ecard(slot);
  527. if (ec->claimed) {
  528. struct irqdesc *d = irqdesc + ec->irq;
  529. /*
  530. * this ugly code is so that we can operate a
  531. * prioritorising system:
  532. *
  533. * Card 0 highest priority
  534. * Card 1
  535. * Card 2
  536. * Card 3 lowest priority
  537. *
  538. * Serial cards should go in 0/1, ethernet/scsi in 2/3
  539. * otherwise you will lose serial data at high speeds!
  540. */
  541. desc_handle_irq(ec->irq, d, regs);
  542. } else {
  543. printk(KERN_WARNING "card%d: interrupt from unclaimed "
  544. "card???\n", slot);
  545. have_expmask &= ~(1 << slot);
  546. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  547. }
  548. } else
  549. printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
  550. }
  551. static int __init ecard_probeirqhw(void)
  552. {
  553. ecard_t *ec;
  554. int found;
  555. __raw_writeb(0x00, EXPMASK_ENABLE);
  556. __raw_writeb(0xff, EXPMASK_STATUS);
  557. found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
  558. __raw_writeb(0xff, EXPMASK_ENABLE);
  559. if (found) {
  560. printk(KERN_DEBUG "Expansion card interrupt "
  561. "management hardware found\n");
  562. /* for each card present, set a bit to '1' */
  563. have_expmask = 0x80000000;
  564. for (ec = cards; ec; ec = ec->next)
  565. have_expmask |= 1 << ec->slot_no;
  566. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  567. }
  568. return found;
  569. }
  570. #else
  571. #define ecard_irqexp_handler NULL
  572. #define ecard_probeirqhw() (0)
  573. #endif
  574. #ifndef IO_EC_MEMC8_BASE
  575. #define IO_EC_MEMC8_BASE 0
  576. #endif
  577. unsigned int __ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
  578. {
  579. unsigned long address = 0;
  580. int slot = ec->slot_no;
  581. if (ec->slot_no == 8)
  582. return IO_EC_MEMC8_BASE;
  583. ectcr &= ~(1 << slot);
  584. switch (type) {
  585. case ECARD_MEMC:
  586. if (slot < 4)
  587. address = IO_EC_MEMC_BASE + (slot << 12);
  588. break;
  589. case ECARD_IOC:
  590. if (slot < 4)
  591. address = IO_EC_IOC_BASE + (slot << 12);
  592. #ifdef IO_EC_IOC4_BASE
  593. else
  594. address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
  595. #endif
  596. if (address)
  597. address += speed << 17;
  598. break;
  599. #ifdef IO_EC_EASI_BASE
  600. case ECARD_EASI:
  601. address = IO_EC_EASI_BASE + (slot << 22);
  602. if (speed == ECARD_FAST)
  603. ectcr |= 1 << slot;
  604. break;
  605. #endif
  606. default:
  607. break;
  608. }
  609. #ifdef IOMD_ECTCR
  610. iomd_writeb(ectcr, IOMD_ECTCR);
  611. #endif
  612. return address;
  613. }
  614. static int ecard_prints(char *buffer, ecard_t *ec)
  615. {
  616. char *start = buffer;
  617. buffer += sprintf(buffer, " %d: %s ", ec->slot_no,
  618. ec->type == ECARD_EASI ? "EASI" : " ");
  619. if (ec->cid.id == 0) {
  620. struct in_chunk_dir incd;
  621. buffer += sprintf(buffer, "[%04X:%04X] ",
  622. ec->cid.manufacturer, ec->cid.product);
  623. if (!ec->card_desc && ec->cid.cd &&
  624. ecard_readchunk(&incd, ec, 0xf5, 0)) {
  625. ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
  626. if (ec->card_desc)
  627. strcpy((char *)ec->card_desc, incd.d.string);
  628. }
  629. buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
  630. } else
  631. buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
  632. return buffer - start;
  633. }
  634. static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
  635. {
  636. ecard_t *ec = cards;
  637. off_t at = 0;
  638. int len, cnt;
  639. cnt = 0;
  640. while (ec && count > cnt) {
  641. len = ecard_prints(buf, ec);
  642. at += len;
  643. if (at >= pos) {
  644. if (!*start) {
  645. *start = buf + (pos - (at - len));
  646. cnt = at - pos;
  647. } else
  648. cnt += len;
  649. buf += len;
  650. }
  651. ec = ec->next;
  652. }
  653. return (count > cnt) ? cnt : count;
  654. }
  655. static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
  656. static void ecard_proc_init(void)
  657. {
  658. proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
  659. create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
  660. get_ecard_dev_info);
  661. }
  662. #define ec_set_resource(ec,nr,st,sz) \
  663. do { \
  664. (ec)->resource[nr].name = ec->dev.bus_id; \
  665. (ec)->resource[nr].start = st; \
  666. (ec)->resource[nr].end = (st) + (sz) - 1; \
  667. (ec)->resource[nr].flags = IORESOURCE_MEM; \
  668. } while (0)
  669. static void __init ecard_free_card(struct expansion_card *ec)
  670. {
  671. int i;
  672. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  673. if (ec->resource[i].flags)
  674. release_resource(&ec->resource[i]);
  675. kfree(ec);
  676. }
  677. static struct expansion_card *__init ecard_alloc_card(int type, int slot)
  678. {
  679. struct expansion_card *ec;
  680. unsigned long base;
  681. int i;
  682. ec = kmalloc(sizeof(ecard_t), GFP_KERNEL);
  683. if (!ec) {
  684. ec = ERR_PTR(-ENOMEM);
  685. goto nomem;
  686. }
  687. memset(ec, 0, sizeof(ecard_t));
  688. ec->slot_no = slot;
  689. ec->type = type;
  690. ec->irq = NO_IRQ;
  691. ec->fiq = NO_IRQ;
  692. ec->dma = NO_DMA;
  693. ec->ops = &ecard_default_ops;
  694. snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
  695. ec->dev.parent = NULL;
  696. ec->dev.bus = &ecard_bus_type;
  697. ec->dev.dma_mask = &ec->dma_mask;
  698. ec->dma_mask = (u64)0xffffffff;
  699. if (slot < 4) {
  700. ec_set_resource(ec, ECARD_RES_MEMC,
  701. PODSLOT_MEMC_BASE + (slot << 14),
  702. PODSLOT_MEMC_SIZE);
  703. base = PODSLOT_IOC0_BASE + (slot << 14);
  704. } else
  705. base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
  706. #ifdef CONFIG_ARCH_RPC
  707. if (slot < 8) {
  708. ec_set_resource(ec, ECARD_RES_EASI,
  709. PODSLOT_EASI_BASE + (slot << 24),
  710. PODSLOT_EASI_SIZE);
  711. }
  712. if (slot == 8) {
  713. ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
  714. } else
  715. #endif
  716. for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
  717. ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
  718. base + (i << 19), PODSLOT_IOC_SIZE);
  719. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  720. if (ec->resource[i].flags &&
  721. request_resource(&iomem_resource, &ec->resource[i])) {
  722. printk(KERN_ERR "%s: resource(s) not available\n",
  723. ec->dev.bus_id);
  724. ec->resource[i].end -= ec->resource[i].start;
  725. ec->resource[i].start = 0;
  726. ec->resource[i].flags = 0;
  727. }
  728. }
  729. nomem:
  730. return ec;
  731. }
  732. static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
  733. {
  734. struct expansion_card *ec = ECARD_DEV(dev);
  735. return sprintf(buf, "%u\n", ec->irq);
  736. }
  737. static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
  738. {
  739. struct expansion_card *ec = ECARD_DEV(dev);
  740. return sprintf(buf, "%u\n", ec->dma);
  741. }
  742. static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
  743. {
  744. struct expansion_card *ec = ECARD_DEV(dev);
  745. char *str = buf;
  746. int i;
  747. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  748. str += sprintf(str, "%08lx %08lx %08lx\n",
  749. ec->resource[i].start,
  750. ec->resource[i].end,
  751. ec->resource[i].flags);
  752. return str - buf;
  753. }
  754. static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
  755. {
  756. struct expansion_card *ec = ECARD_DEV(dev);
  757. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  758. }
  759. static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
  760. {
  761. struct expansion_card *ec = ECARD_DEV(dev);
  762. return sprintf(buf, "%u\n", ec->cid.product);
  763. }
  764. static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
  765. {
  766. struct expansion_card *ec = ECARD_DEV(dev);
  767. return sprintf(buf, "%s\n", ec->type == ECARD_EASI ? "EASI" : "IOC");
  768. }
  769. static struct device_attribute ecard_dev_attrs[] = {
  770. __ATTR(device, S_IRUGO, ecard_show_device, NULL),
  771. __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
  772. __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
  773. __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
  774. __ATTR(type, S_IRUGO, ecard_show_type, NULL),
  775. __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
  776. __ATTR_NULL,
  777. };
  778. int ecard_request_resources(struct expansion_card *ec)
  779. {
  780. int i, err = 0;
  781. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  782. if (ecard_resource_end(ec, i) &&
  783. !request_mem_region(ecard_resource_start(ec, i),
  784. ecard_resource_len(ec, i),
  785. ec->dev.driver->name)) {
  786. err = -EBUSY;
  787. break;
  788. }
  789. }
  790. if (err) {
  791. while (i--)
  792. if (ecard_resource_end(ec, i))
  793. release_mem_region(ecard_resource_start(ec, i),
  794. ecard_resource_len(ec, i));
  795. }
  796. return err;
  797. }
  798. EXPORT_SYMBOL(ecard_request_resources);
  799. void ecard_release_resources(struct expansion_card *ec)
  800. {
  801. int i;
  802. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  803. if (ecard_resource_end(ec, i))
  804. release_mem_region(ecard_resource_start(ec, i),
  805. ecard_resource_len(ec, i));
  806. }
  807. EXPORT_SYMBOL(ecard_release_resources);
  808. /*
  809. * Probe for an expansion card.
  810. *
  811. * If bit 1 of the first byte of the card is set, then the
  812. * card does not exist.
  813. */
  814. static int __init
  815. ecard_probe(int slot, card_type_t type)
  816. {
  817. ecard_t **ecp;
  818. ecard_t *ec;
  819. struct ex_ecid cid;
  820. int i, rc;
  821. ec = ecard_alloc_card(type, slot);
  822. if (IS_ERR(ec)) {
  823. rc = PTR_ERR(ec);
  824. goto nomem;
  825. }
  826. rc = -ENODEV;
  827. if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
  828. goto nodev;
  829. cid.r_zero = 1;
  830. ecard_readbytes(&cid, ec, 0, 16, 0);
  831. if (cid.r_zero)
  832. goto nodev;
  833. ec->cid.id = cid.r_id;
  834. ec->cid.cd = cid.r_cd;
  835. ec->cid.is = cid.r_is;
  836. ec->cid.w = cid.r_w;
  837. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  838. ec->cid.product = ecard_getu16(cid.r_prod);
  839. ec->cid.country = cid.r_country;
  840. ec->cid.irqmask = cid.r_irqmask;
  841. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  842. ec->cid.fiqmask = cid.r_fiqmask;
  843. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  844. ec->fiqaddr =
  845. ec->irqaddr = (void __iomem *)ioaddr(ec->podaddr);
  846. if (ec->cid.is) {
  847. ec->irqmask = ec->cid.irqmask;
  848. ec->irqaddr += ec->cid.irqoff;
  849. ec->fiqmask = ec->cid.fiqmask;
  850. ec->fiqaddr += ec->cid.fiqoff;
  851. } else {
  852. ec->irqmask = 1;
  853. ec->fiqmask = 4;
  854. }
  855. for (i = 0; i < sizeof(blacklist) / sizeof(*blacklist); i++)
  856. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  857. blacklist[i].product == ec->cid.product) {
  858. ec->card_desc = blacklist[i].type;
  859. break;
  860. }
  861. /*
  862. * hook the interrupt handlers
  863. */
  864. if (slot < 8) {
  865. ec->irq = 32 + slot;
  866. set_irq_chip(ec->irq, &ecard_chip);
  867. set_irq_handler(ec->irq, do_level_IRQ);
  868. set_irq_flags(ec->irq, IRQF_VALID);
  869. }
  870. #ifdef IO_EC_MEMC8_BASE
  871. if (slot == 8)
  872. ec->irq = 11;
  873. #endif
  874. #ifdef CONFIG_ARCH_RPC
  875. /* On RiscPC, only first two slots have DMA capability */
  876. if (slot < 2)
  877. ec->dma = 2 + slot;
  878. #endif
  879. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  880. *ecp = ec;
  881. slot_to_expcard[slot] = ec;
  882. device_register(&ec->dev);
  883. return 0;
  884. nodev:
  885. ecard_free_card(ec);
  886. nomem:
  887. return rc;
  888. }
  889. /*
  890. * Initialise the expansion card system.
  891. * Locate all hardware - interrupt management and
  892. * actual cards.
  893. */
  894. static int __init ecard_init(void)
  895. {
  896. int slot, irqhw, ret;
  897. ret = kernel_thread(ecard_task, NULL, CLONE_KERNEL);
  898. if (ret < 0) {
  899. printk(KERN_ERR "Ecard: unable to create kernel thread: %d\n",
  900. ret);
  901. return ret;
  902. }
  903. printk("Probing expansion cards\n");
  904. for (slot = 0; slot < 8; slot ++) {
  905. if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
  906. ecard_probe(slot, ECARD_IOC);
  907. }
  908. #ifdef IO_EC_MEMC8_BASE
  909. ecard_probe(8, ECARD_IOC);
  910. #endif
  911. irqhw = ecard_probeirqhw();
  912. set_irq_chained_handler(IRQ_EXPANSIONCARD,
  913. irqhw ? ecard_irqexp_handler : ecard_irq_handler);
  914. ecard_proc_init();
  915. return 0;
  916. }
  917. subsys_initcall(ecard_init);
  918. /*
  919. * ECARD "bus"
  920. */
  921. static const struct ecard_id *
  922. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  923. {
  924. int i;
  925. for (i = 0; ids[i].manufacturer != 65535; i++)
  926. if (ec->cid.manufacturer == ids[i].manufacturer &&
  927. ec->cid.product == ids[i].product)
  928. return ids + i;
  929. return NULL;
  930. }
  931. static int ecard_drv_probe(struct device *dev)
  932. {
  933. struct expansion_card *ec = ECARD_DEV(dev);
  934. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  935. const struct ecard_id *id;
  936. int ret;
  937. id = ecard_match_device(drv->id_table, ec);
  938. ecard_claim(ec);
  939. ret = drv->probe(ec, id);
  940. if (ret)
  941. ecard_release(ec);
  942. return ret;
  943. }
  944. static int ecard_drv_remove(struct device *dev)
  945. {
  946. struct expansion_card *ec = ECARD_DEV(dev);
  947. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  948. drv->remove(ec);
  949. ecard_release(ec);
  950. return 0;
  951. }
  952. /*
  953. * Before rebooting, we must make sure that the expansion card is in a
  954. * sensible state, so it can be re-detected. This means that the first
  955. * page of the ROM must be visible. We call the expansion cards reset
  956. * handler, if any.
  957. */
  958. static void ecard_drv_shutdown(struct device *dev)
  959. {
  960. struct expansion_card *ec = ECARD_DEV(dev);
  961. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  962. struct ecard_request req;
  963. if (drv->shutdown)
  964. drv->shutdown(ec);
  965. ecard_release(ec);
  966. /*
  967. * If this card has a loader, call the reset handler.
  968. */
  969. if (ec->loader) {
  970. req.fn = ecard_task_reset;
  971. req.ec = ec;
  972. ecard_call(&req);
  973. }
  974. }
  975. int ecard_register_driver(struct ecard_driver *drv)
  976. {
  977. drv->drv.bus = &ecard_bus_type;
  978. drv->drv.probe = ecard_drv_probe;
  979. drv->drv.remove = ecard_drv_remove;
  980. drv->drv.shutdown = ecard_drv_shutdown;
  981. return driver_register(&drv->drv);
  982. }
  983. void ecard_remove_driver(struct ecard_driver *drv)
  984. {
  985. driver_unregister(&drv->drv);
  986. }
  987. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  988. {
  989. struct expansion_card *ec = ECARD_DEV(_dev);
  990. struct ecard_driver *drv = ECARD_DRV(_drv);
  991. int ret;
  992. if (drv->id_table) {
  993. ret = ecard_match_device(drv->id_table, ec) != NULL;
  994. } else {
  995. ret = ec->cid.id == drv->id;
  996. }
  997. return ret;
  998. }
  999. struct bus_type ecard_bus_type = {
  1000. .name = "ecard",
  1001. .dev_attrs = ecard_dev_attrs,
  1002. .match = ecard_match,
  1003. };
  1004. static int ecard_bus_init(void)
  1005. {
  1006. return bus_register(&ecard_bus_type);
  1007. }
  1008. postcore_initcall(ecard_bus_init);
  1009. EXPORT_SYMBOL(ecard_readchunk);
  1010. EXPORT_SYMBOL(__ecard_address);
  1011. EXPORT_SYMBOL(ecard_register_driver);
  1012. EXPORT_SYMBOL(ecard_remove_driver);
  1013. EXPORT_SYMBOL(ecard_bus_type);