ecard.c 27 KB

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