PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/arm/kernel/ecard.c

https://github.com/Mengqi/linux-2.6
C | 1241 lines | 885 code | 203 blank | 153 comment | 155 complexity | 24ac212abb4799eb32cd6f3613b640d6 MD5 | raw file
  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, 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. #ifndef IO_EC_MEMC8_BASE
  576. #define IO_EC_MEMC8_BASE 0
  577. #endif
  578. static 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(struct seq_file *m, ecard_t *ec)
  616. {
  617. seq_printf(m, " %d: %s ", ec->slot_no, ec->easi ? "EASI" : " ");
  618. if (ec->cid.id == 0) {
  619. struct in_chunk_dir incd;
  620. seq_printf(m, "[%04X:%04X] ",
  621. ec->cid.manufacturer, ec->cid.product);
  622. if (!ec->card_desc && ec->cid.cd &&
  623. ecard_readchunk(&incd, ec, 0xf5, 0)) {
  624. ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
  625. if (ec->card_desc)
  626. strcpy((char *)ec->card_desc, incd.d.string);
  627. }
  628. seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
  629. } else
  630. seq_printf(m, "Simple card %d\n", ec->cid.id);
  631. return 0;
  632. }
  633. static int ecard_devices_proc_show(struct seq_file *m, void *v)
  634. {
  635. ecard_t *ec = cards;
  636. while (ec) {
  637. ecard_prints(m, ec);
  638. ec = ec->next;
  639. }
  640. return 0;
  641. }
  642. static int ecard_devices_proc_open(struct inode *inode, struct file *file)
  643. {
  644. return single_open(file, ecard_devices_proc_show, NULL);
  645. }
  646. static const struct file_operations bus_ecard_proc_fops = {
  647. .owner = THIS_MODULE,
  648. .open = ecard_devices_proc_open,
  649. .read = seq_read,
  650. .llseek = seq_lseek,
  651. .release = single_release,
  652. };
  653. static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
  654. static void ecard_proc_init(void)
  655. {
  656. proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
  657. proc_create("devices", 0, proc_bus_ecard_dir, &bus_ecard_proc_fops);
  658. }
  659. #define ec_set_resource(ec,nr,st,sz) \
  660. do { \
  661. (ec)->resource[nr].name = dev_name(&ec->dev); \
  662. (ec)->resource[nr].start = st; \
  663. (ec)->resource[nr].end = (st) + (sz) - 1; \
  664. (ec)->resource[nr].flags = IORESOURCE_MEM; \
  665. } while (0)
  666. static void __init ecard_free_card(struct expansion_card *ec)
  667. {
  668. int i;
  669. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  670. if (ec->resource[i].flags)
  671. release_resource(&ec->resource[i]);
  672. kfree(ec);
  673. }
  674. static struct expansion_card *__init ecard_alloc_card(int type, int slot)
  675. {
  676. struct expansion_card *ec;
  677. unsigned long base;
  678. int i;
  679. ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
  680. if (!ec) {
  681. ec = ERR_PTR(-ENOMEM);
  682. goto nomem;
  683. }
  684. ec->slot_no = slot;
  685. ec->easi = type == ECARD_EASI;
  686. ec->irq = NO_IRQ;
  687. ec->fiq = NO_IRQ;
  688. ec->dma = NO_DMA;
  689. ec->ops = &ecard_default_ops;
  690. dev_set_name(&ec->dev, "ecard%d", slot);
  691. ec->dev.parent = NULL;
  692. ec->dev.bus = &ecard_bus_type;
  693. ec->dev.dma_mask = &ec->dma_mask;
  694. ec->dma_mask = (u64)0xffffffff;
  695. ec->dev.coherent_dma_mask = ec->dma_mask;
  696. if (slot < 4) {
  697. ec_set_resource(ec, ECARD_RES_MEMC,
  698. PODSLOT_MEMC_BASE + (slot << 14),
  699. PODSLOT_MEMC_SIZE);
  700. base = PODSLOT_IOC0_BASE + (slot << 14);
  701. } else
  702. base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
  703. #ifdef CONFIG_ARCH_RPC
  704. if (slot < 8) {
  705. ec_set_resource(ec, ECARD_RES_EASI,
  706. PODSLOT_EASI_BASE + (slot << 24),
  707. PODSLOT_EASI_SIZE);
  708. }
  709. if (slot == 8) {
  710. ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
  711. } else
  712. #endif
  713. for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
  714. ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
  715. base + (i << 19), PODSLOT_IOC_SIZE);
  716. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  717. if (ec->resource[i].flags &&
  718. request_resource(&iomem_resource, &ec->resource[i])) {
  719. dev_err(&ec->dev, "resource(s) not available\n");
  720. ec->resource[i].end -= ec->resource[i].start;
  721. ec->resource[i].start = 0;
  722. ec->resource[i].flags = 0;
  723. }
  724. }
  725. nomem:
  726. return ec;
  727. }
  728. static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
  729. {
  730. struct expansion_card *ec = ECARD_DEV(dev);
  731. return sprintf(buf, "%u\n", ec->irq);
  732. }
  733. static ssize_t ecard_show_dma(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->dma);
  737. }
  738. static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
  739. {
  740. struct expansion_card *ec = ECARD_DEV(dev);
  741. char *str = buf;
  742. int i;
  743. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  744. str += sprintf(str, "%08x %08x %08lx\n",
  745. ec->resource[i].start,
  746. ec->resource[i].end,
  747. ec->resource[i].flags);
  748. return str - buf;
  749. }
  750. static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
  751. {
  752. struct expansion_card *ec = ECARD_DEV(dev);
  753. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  754. }
  755. static ssize_t ecard_show_device(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.product);
  759. }
  760. static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
  761. {
  762. struct expansion_card *ec = ECARD_DEV(dev);
  763. return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
  764. }
  765. static struct device_attribute ecard_dev_attrs[] = {
  766. __ATTR(device, S_IRUGO, ecard_show_device, NULL),
  767. __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
  768. __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
  769. __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
  770. __ATTR(type, S_IRUGO, ecard_show_type, NULL),
  771. __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
  772. __ATTR_NULL,
  773. };
  774. int ecard_request_resources(struct expansion_card *ec)
  775. {
  776. int i, err = 0;
  777. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  778. if (ecard_resource_end(ec, i) &&
  779. !request_mem_region(ecard_resource_start(ec, i),
  780. ecard_resource_len(ec, i),
  781. ec->dev.driver->name)) {
  782. err = -EBUSY;
  783. break;
  784. }
  785. }
  786. if (err) {
  787. while (i--)
  788. if (ecard_resource_end(ec, i))
  789. release_mem_region(ecard_resource_start(ec, i),
  790. ecard_resource_len(ec, i));
  791. }
  792. return err;
  793. }
  794. EXPORT_SYMBOL(ecard_request_resources);
  795. void ecard_release_resources(struct expansion_card *ec)
  796. {
  797. int i;
  798. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  799. if (ecard_resource_end(ec, i))
  800. release_mem_region(ecard_resource_start(ec, i),
  801. ecard_resource_len(ec, i));
  802. }
  803. EXPORT_SYMBOL(ecard_release_resources);
  804. void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
  805. {
  806. ec->irq_data = irq_data;
  807. barrier();
  808. ec->ops = ops;
  809. }
  810. EXPORT_SYMBOL(ecard_setirq);
  811. void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
  812. unsigned long offset, unsigned long maxsize)
  813. {
  814. unsigned long start = ecard_resource_start(ec, res);
  815. unsigned long end = ecard_resource_end(ec, res);
  816. if (offset > (end - start))
  817. return NULL;
  818. start += offset;
  819. if (maxsize && end - start > maxsize)
  820. end = start + maxsize;
  821. return devm_ioremap(&ec->dev, start, end - start);
  822. }
  823. EXPORT_SYMBOL(ecardm_iomap);
  824. /*
  825. * Probe for an expansion card.
  826. *
  827. * If bit 1 of the first byte of the card is set, then the
  828. * card does not exist.
  829. */
  830. static int __init
  831. ecard_probe(int slot, card_type_t type)
  832. {
  833. ecard_t **ecp;
  834. ecard_t *ec;
  835. struct ex_ecid cid;
  836. int i, rc;
  837. ec = ecard_alloc_card(type, slot);
  838. if (IS_ERR(ec)) {
  839. rc = PTR_ERR(ec);
  840. goto nomem;
  841. }
  842. rc = -ENODEV;
  843. if ((ec->podaddr = __ecard_address(ec, type, ECARD_SYNC)) == 0)
  844. goto nodev;
  845. cid.r_zero = 1;
  846. ecard_readbytes(&cid, ec, 0, 16, 0);
  847. if (cid.r_zero)
  848. goto nodev;
  849. ec->cid.id = cid.r_id;
  850. ec->cid.cd = cid.r_cd;
  851. ec->cid.is = cid.r_is;
  852. ec->cid.w = cid.r_w;
  853. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  854. ec->cid.product = ecard_getu16(cid.r_prod);
  855. ec->cid.country = cid.r_country;
  856. ec->cid.irqmask = cid.r_irqmask;
  857. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  858. ec->cid.fiqmask = cid.r_fiqmask;
  859. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  860. ec->fiqaddr =
  861. ec->irqaddr = (void __iomem *)ioaddr(ec->podaddr);
  862. if (ec->cid.is) {
  863. ec->irqmask = ec->cid.irqmask;
  864. ec->irqaddr += ec->cid.irqoff;
  865. ec->fiqmask = ec->cid.fiqmask;
  866. ec->fiqaddr += ec->cid.fiqoff;
  867. } else {
  868. ec->irqmask = 1;
  869. ec->fiqmask = 4;
  870. }
  871. for (i = 0; i < ARRAY_SIZE(blacklist); i++)
  872. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  873. blacklist[i].product == ec->cid.product) {
  874. ec->card_desc = blacklist[i].type;
  875. break;
  876. }
  877. /*
  878. * hook the interrupt handlers
  879. */
  880. if (slot < 8) {
  881. ec->irq = 32 + slot;
  882. irq_set_chip_and_handler(ec->irq, &ecard_chip,
  883. handle_level_irq);
  884. set_irq_flags(ec->irq, IRQF_VALID);
  885. }
  886. #ifdef IO_EC_MEMC8_BASE
  887. if (slot == 8)
  888. ec->irq = 11;
  889. #endif
  890. #ifdef CONFIG_ARCH_RPC
  891. /* On RiscPC, only first two slots have DMA capability */
  892. if (slot < 2)
  893. ec->dma = 2 + slot;
  894. #endif
  895. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  896. *ecp = ec;
  897. slot_to_expcard[slot] = ec;
  898. device_register(&ec->dev);
  899. return 0;
  900. nodev:
  901. ecard_free_card(ec);
  902. nomem:
  903. return rc;
  904. }
  905. /*
  906. * Initialise the expansion card system.
  907. * Locate all hardware - interrupt management and
  908. * actual cards.
  909. */
  910. static int __init ecard_init(void)
  911. {
  912. struct task_struct *task;
  913. int slot, irqhw;
  914. task = kthread_run(ecard_task, NULL, "kecardd");
  915. if (IS_ERR(task)) {
  916. printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
  917. PTR_ERR(task));
  918. return PTR_ERR(task);
  919. }
  920. printk("Probing expansion cards\n");
  921. for (slot = 0; slot < 8; slot ++) {
  922. if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
  923. ecard_probe(slot, ECARD_IOC);
  924. }
  925. #ifdef IO_EC_MEMC8_BASE
  926. ecard_probe(8, ECARD_IOC);
  927. #endif
  928. irqhw = ecard_probeirqhw();
  929. irq_set_chained_handler(IRQ_EXPANSIONCARD,
  930. irqhw ? ecard_irqexp_handler : ecard_irq_handler);
  931. ecard_proc_init();
  932. return 0;
  933. }
  934. subsys_initcall(ecard_init);
  935. /*
  936. * ECARD "bus"
  937. */
  938. static const struct ecard_id *
  939. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  940. {
  941. int i;
  942. for (i = 0; ids[i].manufacturer != 65535; i++)
  943. if (ec->cid.manufacturer == ids[i].manufacturer &&
  944. ec->cid.product == ids[i].product)
  945. return ids + i;
  946. return NULL;
  947. }
  948. static int ecard_drv_probe(struct device *dev)
  949. {
  950. struct expansion_card *ec = ECARD_DEV(dev);
  951. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  952. const struct ecard_id *id;
  953. int ret;
  954. id = ecard_match_device(drv->id_table, ec);
  955. ec->claimed = 1;
  956. ret = drv->probe(ec, id);
  957. if (ret)
  958. ec->claimed = 0;
  959. return ret;
  960. }
  961. static int ecard_drv_remove(struct device *dev)
  962. {
  963. struct expansion_card *ec = ECARD_DEV(dev);
  964. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  965. drv->remove(ec);
  966. ec->claimed = 0;
  967. /*
  968. * Restore the default operations. We ensure that the
  969. * ops are set before we change the data.
  970. */
  971. ec->ops = &ecard_default_ops;
  972. barrier();
  973. ec->irq_data = NULL;
  974. return 0;
  975. }
  976. /*
  977. * Before rebooting, we must make sure that the expansion card is in a
  978. * sensible state, so it can be re-detected. This means that the first
  979. * page of the ROM must be visible. We call the expansion cards reset
  980. * handler, if any.
  981. */
  982. static void ecard_drv_shutdown(struct device *dev)
  983. {
  984. struct expansion_card *ec = ECARD_DEV(dev);
  985. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  986. struct ecard_request req;
  987. if (dev->driver) {
  988. if (drv->shutdown)
  989. drv->shutdown(ec);
  990. ec->claimed = 0;
  991. }
  992. /*
  993. * If this card has a loader, call the reset handler.
  994. */
  995. if (ec->loader) {
  996. req.fn = ecard_task_reset;
  997. req.ec = ec;
  998. ecard_call(&req);
  999. }
  1000. }
  1001. int ecard_register_driver(struct ecard_driver *drv)
  1002. {
  1003. drv->drv.bus = &ecard_bus_type;
  1004. return driver_register(&drv->drv);
  1005. }
  1006. void ecard_remove_driver(struct ecard_driver *drv)
  1007. {
  1008. driver_unregister(&drv->drv);
  1009. }
  1010. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  1011. {
  1012. struct expansion_card *ec = ECARD_DEV(_dev);
  1013. struct ecard_driver *drv = ECARD_DRV(_drv);
  1014. int ret;
  1015. if (drv->id_table) {
  1016. ret = ecard_match_device(drv->id_table, ec) != NULL;
  1017. } else {
  1018. ret = ec->cid.id == drv->id;
  1019. }
  1020. return ret;
  1021. }
  1022. struct bus_type ecard_bus_type = {
  1023. .name = "ecard",
  1024. .dev_attrs = ecard_dev_attrs,
  1025. .match = ecard_match,
  1026. .probe = ecard_drv_probe,
  1027. .remove = ecard_drv_remove,
  1028. .shutdown = ecard_drv_shutdown,
  1029. };
  1030. static int ecard_bus_init(void)
  1031. {
  1032. return bus_register(&ecard_bus_type);
  1033. }
  1034. postcore_initcall(ecard_bus_init);
  1035. EXPORT_SYMBOL(ecard_readchunk);
  1036. EXPORT_SYMBOL(ecard_register_driver);
  1037. EXPORT_SYMBOL(ecard_remove_driver);
  1038. EXPORT_SYMBOL(ecard_bus_type);