ecard.c 27 KB

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