ecard.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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/device.h>
  40. #include <linux/init.h>
  41. #include <linux/mutex.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 DEFINE_MUTEX(ecard_mutex);
  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_ONSTACK(completion);
  257. req->complete = &completion;
  258. mutex_lock(&ecard_mutex);
  259. ecard_req = req;
  260. wake_up(&ecard_wait);
  261. /*
  262. * Now wait for kecardd to run.
  263. */
  264. wait_for_completion(&completion);
  265. mutex_unlock(&ecard_mutex);
  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 = 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 irq_chip ecard_chip = {
  406. .name = "ECARD",
  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 irq_desc *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 irq_desc *desc)
  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 irq_desc *d = irq_desc + ec->irq;
  501. desc_handle_irq(ec->irq, d);
  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 irq_desc *desc)
  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 irq_desc *d = irq_desc + 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);
  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 = kzalloc(sizeof(ecard_t), GFP_KERNEL);
  684. if (!ec) {
  685. ec = ERR_PTR(-ENOMEM);
  686. goto nomem;
  687. }
  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. ec->dev.coherent_dma_mask = ec->dma_mask;
  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, "%08x %08x %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 < ARRAY_SIZE(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, handle_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);