/arch/arm/mach-rpc/ecard.c

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