PageRenderTime 128ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/bdas/linux
C | 1140 lines | 809 code | 191 blank | 140 comment | 144 complexity | e6f15aa5ab8e97494d41bb78b8a6ac5b 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/irq.h>
  45. #include <linux/io.h>
  46. #include <asm/dma.h>
  47. #include <asm/ecard.h>
  48. #include <mach/hardware.h>
  49. #include <asm/irq.h>
  50. #include <asm/mmu_context.h>
  51. #include <asm/mach/irq.h>
  52. #include <asm/tlbflush.h>
  53. #include "ecard.h"
  54. struct ecard_request {
  55. void (*fn)(struct ecard_request *);
  56. ecard_t *ec;
  57. unsigned int address;
  58. unsigned int length;
  59. unsigned int use_loader;
  60. void *buffer;
  61. struct completion *complete;
  62. };
  63. struct expcard_blacklist {
  64. unsigned short manufacturer;
  65. unsigned short product;
  66. const char *type;
  67. };
  68. static ecard_t *cards;
  69. static ecard_t *slot_to_expcard[MAX_ECARDS];
  70. static unsigned int ectcr;
  71. /* List of descriptions of cards which don't have an extended
  72. * identification, or chunk directories containing a description.
  73. */
  74. static struct expcard_blacklist __initdata blacklist[] = {
  75. { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
  76. };
  77. asmlinkage extern int
  78. ecard_loader_reset(unsigned long base, loader_t loader);
  79. asmlinkage extern int
  80. ecard_loader_read(int off, unsigned long base, loader_t loader);
  81. static inline unsigned short ecard_getu16(unsigned char *v)
  82. {
  83. return v[0] | v[1] << 8;
  84. }
  85. static inline signed long ecard_gets24(unsigned char *v)
  86. {
  87. return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
  88. }
  89. static inline ecard_t *slot_to_ecard(unsigned int slot)
  90. {
  91. return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
  92. }
  93. /* ===================== Expansion card daemon ======================== */
  94. /*
  95. * Since the loader programs on the expansion cards need to be run
  96. * in a specific environment, create a separate task with this
  97. * environment up, and pass requests to this task as and when we
  98. * need to.
  99. *
  100. * This should allow 99% of loaders to be called from Linux.
  101. *
  102. * From a security standpoint, we trust the card vendors. This
  103. * may be a misplaced trust.
  104. */
  105. static void ecard_task_reset(struct ecard_request *req)
  106. {
  107. struct expansion_card *ec = req->ec;
  108. struct resource *res;
  109. res = ec->slot_no == 8
  110. ? &ec->resource[ECARD_RES_MEMC]
  111. : ec->easi
  112. ? &ec->resource[ECARD_RES_EASI]
  113. : &ec->resource[ECARD_RES_IOCSYNC];
  114. ecard_loader_reset(res->start, ec->loader);
  115. }
  116. static void ecard_task_readbytes(struct ecard_request *req)
  117. {
  118. struct expansion_card *ec = req->ec;
  119. unsigned char *buf = req->buffer;
  120. unsigned int len = req->length;
  121. unsigned int off = req->address;
  122. if (ec->slot_no == 8) {
  123. void __iomem *base = (void __iomem *)
  124. ec->resource[ECARD_RES_MEMC].start;
  125. /*
  126. * The card maintains an index which increments the address
  127. * into a 4096-byte page on each access. We need to keep
  128. * track of the counter.
  129. */
  130. static unsigned int index;
  131. unsigned int page;
  132. page = (off >> 12) * 4;
  133. if (page > 256 * 4)
  134. return;
  135. off &= 4095;
  136. /*
  137. * If we are reading offset 0, or our current index is
  138. * greater than the offset, reset the hardware index counter.
  139. */
  140. if (off == 0 || index > off) {
  141. writeb(0, base);
  142. index = 0;
  143. }
  144. /*
  145. * Increment the hardware index counter until we get to the
  146. * required offset. The read bytes are discarded.
  147. */
  148. while (index < off) {
  149. readb(base + page);
  150. index += 1;
  151. }
  152. while (len--) {
  153. *buf++ = readb(base + page);
  154. index += 1;
  155. }
  156. } else {
  157. unsigned long base = (ec->easi
  158. ? &ec->resource[ECARD_RES_EASI]
  159. : &ec->resource[ECARD_RES_IOCSYNC])->start;
  160. void __iomem *pbase = (void __iomem *)base;
  161. if (!req->use_loader || !ec->loader) {
  162. off *= 4;
  163. while (len--) {
  164. *buf++ = readb(pbase + off);
  165. off += 4;
  166. }
  167. } else {
  168. while(len--) {
  169. /*
  170. * The following is required by some
  171. * expansion card loader programs.
  172. */
  173. *(unsigned long *)0x108 = 0;
  174. *buf++ = ecard_loader_read(off++, base,
  175. ec->loader);
  176. }
  177. }
  178. }
  179. }
  180. static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
  181. static struct ecard_request *ecard_req;
  182. static DEFINE_MUTEX(ecard_mutex);
  183. /*
  184. * Set up the expansion card daemon's page tables.
  185. */
  186. static void ecard_init_pgtables(struct mm_struct *mm)
  187. {
  188. struct vm_area_struct vma;
  189. /* We want to set up the page tables for the following mapping:
  190. * Virtual Physical
  191. * 0x03000000 0x03000000
  192. * 0x03010000 unmapped
  193. * 0x03210000 0x03210000
  194. * 0x03400000 unmapped
  195. * 0x08000000 0x08000000
  196. * 0x10000000 unmapped
  197. *
  198. * FIXME: we don't follow this 100% yet.
  199. */
  200. pgd_t *src_pgd, *dst_pgd;
  201. src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
  202. dst_pgd = pgd_offset(mm, IO_START);
  203. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
  204. src_pgd = pgd_offset(mm, (unsigned long)EASI_BASE);
  205. dst_pgd = pgd_offset(mm, EASI_START);
  206. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
  207. vma.vm_flags = VM_EXEC;
  208. vma.vm_mm = mm;
  209. flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
  210. flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
  211. }
  212. static int ecard_init_mm(void)
  213. {
  214. struct mm_struct * mm = mm_alloc();
  215. struct mm_struct *active_mm = current->active_mm;
  216. if (!mm)
  217. return -ENOMEM;
  218. current->mm = mm;
  219. current->active_mm = mm;
  220. activate_mm(active_mm, mm);
  221. mmdrop(active_mm);
  222. ecard_init_pgtables(mm);
  223. return 0;
  224. }
  225. static int
  226. ecard_task(void * unused)
  227. {
  228. /*
  229. * Allocate a mm. We're not a lazy-TLB kernel task since we need
  230. * to set page table entries where the user space would be. Note
  231. * that this also creates the page tables. Failure is not an
  232. * option here.
  233. */
  234. if (ecard_init_mm())
  235. panic("kecardd: unable to alloc mm\n");
  236. while (1) {
  237. struct ecard_request *req;
  238. wait_event_interruptible(ecard_wait, ecard_req != NULL);
  239. req = xchg(&ecard_req, NULL);
  240. if (req != NULL) {
  241. req->fn(req);
  242. complete(req->complete);
  243. }
  244. }
  245. }
  246. /*
  247. * Wake the expansion card daemon to action our request.
  248. *
  249. * FIXME: The test here is not sufficient to detect if the
  250. * kcardd is running.
  251. */
  252. static void ecard_call(struct ecard_request *req)
  253. {
  254. DECLARE_COMPLETION_ONSTACK(completion);
  255. req->complete = &completion;
  256. mutex_lock(&ecard_mutex);
  257. ecard_req = req;
  258. wake_up(&ecard_wait);
  259. /*
  260. * Now wait for kecardd to run.
  261. */
  262. wait_for_completion(&completion);
  263. mutex_unlock(&ecard_mutex);
  264. }
  265. /* ======================= Mid-level card control ===================== */
  266. static void
  267. ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
  268. {
  269. struct ecard_request req;
  270. req.fn = ecard_task_readbytes;
  271. req.ec = ec;
  272. req.address = off;
  273. req.length = len;
  274. req.use_loader = useld;
  275. req.buffer = addr;
  276. ecard_call(&req);
  277. }
  278. int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
  279. {
  280. struct ex_chunk_dir excd;
  281. int index = 16;
  282. int useld = 0;
  283. if (!ec->cid.cd)
  284. return 0;
  285. while(1) {
  286. ecard_readbytes(&excd, ec, index, 8, useld);
  287. index += 8;
  288. if (c_id(&excd) == 0) {
  289. if (!useld && ec->loader) {
  290. useld = 1;
  291. index = 0;
  292. continue;
  293. }
  294. return 0;
  295. }
  296. if (c_id(&excd) == 0xf0) { /* link */
  297. index = c_start(&excd);
  298. continue;
  299. }
  300. if (c_id(&excd) == 0x80) { /* loader */
  301. if (!ec->loader) {
  302. ec->loader = kmalloc(c_len(&excd),
  303. GFP_KERNEL);
  304. if (ec->loader)
  305. ecard_readbytes(ec->loader, ec,
  306. (int)c_start(&excd),
  307. c_len(&excd), useld);
  308. else
  309. return 0;
  310. }
  311. continue;
  312. }
  313. if (c_id(&excd) == id && num-- == 0)
  314. break;
  315. }
  316. if (c_id(&excd) & 0x80) {
  317. switch (c_id(&excd) & 0x70) {
  318. case 0x70:
  319. ecard_readbytes((unsigned char *)excd.d.string, ec,
  320. (int)c_start(&excd), c_len(&excd),
  321. useld);
  322. break;
  323. case 0x00:
  324. break;
  325. }
  326. }
  327. cd->start_offset = c_start(&excd);
  328. memcpy(cd->d.string, excd.d.string, 256);
  329. return 1;
  330. }
  331. /* ======================= Interrupt control ============================ */
  332. static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
  333. {
  334. }
  335. static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
  336. {
  337. }
  338. static int ecard_def_irq_pending(ecard_t *ec)
  339. {
  340. return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
  341. }
  342. static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
  343. {
  344. panic("ecard_def_fiq_enable called - impossible");
  345. }
  346. static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
  347. {
  348. panic("ecard_def_fiq_disable called - impossible");
  349. }
  350. static int ecard_def_fiq_pending(ecard_t *ec)
  351. {
  352. return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
  353. }
  354. static expansioncard_ops_t ecard_default_ops = {
  355. ecard_def_irq_enable,
  356. ecard_def_irq_disable,
  357. ecard_def_irq_pending,
  358. ecard_def_fiq_enable,
  359. ecard_def_fiq_disable,
  360. ecard_def_fiq_pending
  361. };
  362. /*
  363. * Enable and disable interrupts from expansion cards.
  364. * (interrupts are disabled for these functions).
  365. *
  366. * They are not meant to be called directly, but via enable/disable_irq.
  367. */
  368. static void ecard_irq_unmask(struct irq_data *d)
  369. {
  370. ecard_t *ec = irq_data_get_irq_chip_data(d);
  371. if (ec) {
  372. if (!ec->ops)
  373. ec->ops = &ecard_default_ops;
  374. if (ec->claimed && ec->ops->irqenable)
  375. ec->ops->irqenable(ec, d->irq);
  376. else
  377. printk(KERN_ERR "ecard: rejecting request to "
  378. "enable IRQs for %d\n", d->irq);
  379. }
  380. }
  381. static void ecard_irq_mask(struct irq_data *d)
  382. {
  383. ecard_t *ec = irq_data_get_irq_chip_data(d);
  384. if (ec) {
  385. if (!ec->ops)
  386. ec->ops = &ecard_default_ops;
  387. if (ec->ops && ec->ops->irqdisable)
  388. ec->ops->irqdisable(ec, d->irq);
  389. }
  390. }
  391. static struct irq_chip ecard_chip = {
  392. .name = "ECARD",
  393. .irq_ack = ecard_irq_mask,
  394. .irq_mask = ecard_irq_mask,
  395. .irq_unmask = ecard_irq_unmask,
  396. };
  397. void ecard_enablefiq(unsigned int fiqnr)
  398. {
  399. ecard_t *ec = slot_to_ecard(fiqnr);
  400. if (ec) {
  401. if (!ec->ops)
  402. ec->ops = &ecard_default_ops;
  403. if (ec->claimed && ec->ops->fiqenable)
  404. ec->ops->fiqenable(ec, fiqnr);
  405. else
  406. printk(KERN_ERR "ecard: rejecting request to "
  407. "enable FIQs for %d\n", fiqnr);
  408. }
  409. }
  410. void ecard_disablefiq(unsigned int fiqnr)
  411. {
  412. ecard_t *ec = slot_to_ecard(fiqnr);
  413. if (ec) {
  414. if (!ec->ops)
  415. ec->ops = &ecard_default_ops;
  416. if (ec->ops->fiqdisable)
  417. ec->ops->fiqdisable(ec, fiqnr);
  418. }
  419. }
  420. static void ecard_dump_irq_state(void)
  421. {
  422. ecard_t *ec;
  423. printk("Expansion card IRQ state:\n");
  424. for (ec = cards; ec; ec = ec->next) {
  425. if (ec->slot_no == 8)
  426. continue;
  427. printk(" %d: %sclaimed, ",
  428. ec->slot_no, ec->claimed ? "" : "not ");
  429. if (ec->ops && ec->ops->irqpending &&
  430. ec->ops != &ecard_default_ops)
  431. printk("irq %spending\n",
  432. ec->ops->irqpending(ec) ? "" : "not ");
  433. else
  434. printk("irqaddr %p, mask = %02X, status = %02X\n",
  435. ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
  436. }
  437. }
  438. static void ecard_check_lockup(struct irq_desc *desc)
  439. {
  440. static unsigned long last;
  441. static int lockup;
  442. /*
  443. * If the timer interrupt has not run since the last million
  444. * unrecognised expansion card interrupts, then there is
  445. * something seriously wrong. Disable the expansion card
  446. * interrupts so at least we can continue.
  447. *
  448. * Maybe we ought to start a timer to re-enable them some time
  449. * later?
  450. */
  451. if (last == jiffies) {
  452. lockup += 1;
  453. if (lockup > 1000000) {
  454. printk(KERN_ERR "\nInterrupt lockup detected - "
  455. "disabling all expansion card interrupts\n");
  456. desc->irq_data.chip->irq_mask(&desc->irq_data);
  457. ecard_dump_irq_state();
  458. }
  459. } else
  460. lockup = 0;
  461. /*
  462. * If we did not recognise the source of this interrupt,
  463. * warn the user, but don't flood the user with these messages.
  464. */
  465. if (!last || time_after(jiffies, last + 5*HZ)) {
  466. last = jiffies;
  467. printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
  468. ecard_dump_irq_state();
  469. }
  470. }
  471. static void
  472. ecard_irq_handler(unsigned int irq, 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 ecard_show_irq(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 ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
  646. {
  647. struct expansion_card *ec = ECARD_DEV(dev);
  648. return sprintf(buf, "%u\n", ec->dma);
  649. }
  650. static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
  651. {
  652. struct expansion_card *ec = ECARD_DEV(dev);
  653. char *str = buf;
  654. int i;
  655. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  656. str += sprintf(str, "%08x %08x %08lx\n",
  657. ec->resource[i].start,
  658. ec->resource[i].end,
  659. ec->resource[i].flags);
  660. return str - buf;
  661. }
  662. static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
  663. {
  664. struct expansion_card *ec = ECARD_DEV(dev);
  665. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  666. }
  667. static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
  668. {
  669. struct expansion_card *ec = ECARD_DEV(dev);
  670. return sprintf(buf, "%u\n", ec->cid.product);
  671. }
  672. static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
  673. {
  674. struct expansion_card *ec = ECARD_DEV(dev);
  675. return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
  676. }
  677. static struct device_attribute ecard_dev_attrs[] = {
  678. __ATTR(device, S_IRUGO, ecard_show_device, NULL),
  679. __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
  680. __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
  681. __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
  682. __ATTR(type, S_IRUGO, ecard_show_type, NULL),
  683. __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
  684. __ATTR_NULL,
  685. };
  686. int ecard_request_resources(struct expansion_card *ec)
  687. {
  688. int i, err = 0;
  689. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  690. if (ecard_resource_end(ec, i) &&
  691. !request_mem_region(ecard_resource_start(ec, i),
  692. ecard_resource_len(ec, i),
  693. ec->dev.driver->name)) {
  694. err = -EBUSY;
  695. break;
  696. }
  697. }
  698. if (err) {
  699. while (i--)
  700. if (ecard_resource_end(ec, i))
  701. release_mem_region(ecard_resource_start(ec, i),
  702. ecard_resource_len(ec, i));
  703. }
  704. return err;
  705. }
  706. EXPORT_SYMBOL(ecard_request_resources);
  707. void ecard_release_resources(struct expansion_card *ec)
  708. {
  709. int i;
  710. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  711. if (ecard_resource_end(ec, i))
  712. release_mem_region(ecard_resource_start(ec, i),
  713. ecard_resource_len(ec, i));
  714. }
  715. EXPORT_SYMBOL(ecard_release_resources);
  716. void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
  717. {
  718. ec->irq_data = irq_data;
  719. barrier();
  720. ec->ops = ops;
  721. }
  722. EXPORT_SYMBOL(ecard_setirq);
  723. void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
  724. unsigned long offset, unsigned long maxsize)
  725. {
  726. unsigned long start = ecard_resource_start(ec, res);
  727. unsigned long end = ecard_resource_end(ec, res);
  728. if (offset > (end - start))
  729. return NULL;
  730. start += offset;
  731. if (maxsize && end - start > maxsize)
  732. end = start + maxsize;
  733. return devm_ioremap(&ec->dev, start, end - start);
  734. }
  735. EXPORT_SYMBOL(ecardm_iomap);
  736. /*
  737. * Probe for an expansion card.
  738. *
  739. * If bit 1 of the first byte of the card is set, then the
  740. * card does not exist.
  741. */
  742. static int __init ecard_probe(int slot, unsigned irq, card_type_t type)
  743. {
  744. ecard_t **ecp;
  745. ecard_t *ec;
  746. struct ex_ecid cid;
  747. void __iomem *addr;
  748. int i, rc;
  749. ec = ecard_alloc_card(type, slot);
  750. if (IS_ERR(ec)) {
  751. rc = PTR_ERR(ec);
  752. goto nomem;
  753. }
  754. rc = -ENODEV;
  755. if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
  756. goto nodev;
  757. cid.r_zero = 1;
  758. ecard_readbytes(&cid, ec, 0, 16, 0);
  759. if (cid.r_zero)
  760. goto nodev;
  761. ec->cid.id = cid.r_id;
  762. ec->cid.cd = cid.r_cd;
  763. ec->cid.is = cid.r_is;
  764. ec->cid.w = cid.r_w;
  765. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  766. ec->cid.product = ecard_getu16(cid.r_prod);
  767. ec->cid.country = cid.r_country;
  768. ec->cid.irqmask = cid.r_irqmask;
  769. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  770. ec->cid.fiqmask = cid.r_fiqmask;
  771. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  772. ec->fiqaddr =
  773. ec->irqaddr = addr;
  774. if (ec->cid.is) {
  775. ec->irqmask = ec->cid.irqmask;
  776. ec->irqaddr += ec->cid.irqoff;
  777. ec->fiqmask = ec->cid.fiqmask;
  778. ec->fiqaddr += ec->cid.fiqoff;
  779. } else {
  780. ec->irqmask = 1;
  781. ec->fiqmask = 4;
  782. }
  783. for (i = 0; i < ARRAY_SIZE(blacklist); i++)
  784. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  785. blacklist[i].product == ec->cid.product) {
  786. ec->card_desc = blacklist[i].type;
  787. break;
  788. }
  789. ec->irq = irq;
  790. /*
  791. * hook the interrupt handlers
  792. */
  793. if (slot < 8) {
  794. irq_set_chip_and_handler(ec->irq, &ecard_chip,
  795. handle_level_irq);
  796. irq_set_chip_data(ec->irq, ec);
  797. set_irq_flags(ec->irq, IRQF_VALID);
  798. }
  799. #ifdef CONFIG_ARCH_RPC
  800. /* On RiscPC, only first two slots have DMA capability */
  801. if (slot < 2)
  802. ec->dma = 2 + slot;
  803. #endif
  804. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  805. *ecp = ec;
  806. slot_to_expcard[slot] = ec;
  807. rc = device_register(&ec->dev);
  808. if (rc)
  809. goto nodev;
  810. return 0;
  811. nodev:
  812. ecard_free_card(ec);
  813. nomem:
  814. return rc;
  815. }
  816. /*
  817. * Initialise the expansion card system.
  818. * Locate all hardware - interrupt management and
  819. * actual cards.
  820. */
  821. static int __init ecard_init(void)
  822. {
  823. struct task_struct *task;
  824. int slot, irqbase;
  825. irqbase = irq_alloc_descs(-1, 0, 8, -1);
  826. if (irqbase < 0)
  827. return irqbase;
  828. task = kthread_run(ecard_task, NULL, "kecardd");
  829. if (IS_ERR(task)) {
  830. printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
  831. PTR_ERR(task));
  832. irq_free_descs(irqbase, 8);
  833. return PTR_ERR(task);
  834. }
  835. printk("Probing expansion cards\n");
  836. for (slot = 0; slot < 8; slot ++) {
  837. if (ecard_probe(slot, irqbase + slot, ECARD_EASI) == -ENODEV)
  838. ecard_probe(slot, irqbase + slot, ECARD_IOC);
  839. }
  840. ecard_probe(8, 11, ECARD_IOC);
  841. irq_set_chained_handler(IRQ_EXPANSIONCARD, ecard_irq_handler);
  842. ecard_proc_init();
  843. return 0;
  844. }
  845. subsys_initcall(ecard_init);
  846. /*
  847. * ECARD "bus"
  848. */
  849. static const struct ecard_id *
  850. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  851. {
  852. int i;
  853. for (i = 0; ids[i].manufacturer != 65535; i++)
  854. if (ec->cid.manufacturer == ids[i].manufacturer &&
  855. ec->cid.product == ids[i].product)
  856. return ids + i;
  857. return NULL;
  858. }
  859. static int ecard_drv_probe(struct device *dev)
  860. {
  861. struct expansion_card *ec = ECARD_DEV(dev);
  862. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  863. const struct ecard_id *id;
  864. int ret;
  865. id = ecard_match_device(drv->id_table, ec);
  866. ec->claimed = 1;
  867. ret = drv->probe(ec, id);
  868. if (ret)
  869. ec->claimed = 0;
  870. return ret;
  871. }
  872. static int ecard_drv_remove(struct device *dev)
  873. {
  874. struct expansion_card *ec = ECARD_DEV(dev);
  875. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  876. drv->remove(ec);
  877. ec->claimed = 0;
  878. /*
  879. * Restore the default operations. We ensure that the
  880. * ops are set before we change the data.
  881. */
  882. ec->ops = &ecard_default_ops;
  883. barrier();
  884. ec->irq_data = NULL;
  885. return 0;
  886. }
  887. /*
  888. * Before rebooting, we must make sure that the expansion card is in a
  889. * sensible state, so it can be re-detected. This means that the first
  890. * page of the ROM must be visible. We call the expansion cards reset
  891. * handler, if any.
  892. */
  893. static void ecard_drv_shutdown(struct device *dev)
  894. {
  895. struct expansion_card *ec = ECARD_DEV(dev);
  896. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  897. struct ecard_request req;
  898. if (dev->driver) {
  899. if (drv->shutdown)
  900. drv->shutdown(ec);
  901. ec->claimed = 0;
  902. }
  903. /*
  904. * If this card has a loader, call the reset handler.
  905. */
  906. if (ec->loader) {
  907. req.fn = ecard_task_reset;
  908. req.ec = ec;
  909. ecard_call(&req);
  910. }
  911. }
  912. int ecard_register_driver(struct ecard_driver *drv)
  913. {
  914. drv->drv.bus = &ecard_bus_type;
  915. return driver_register(&drv->drv);
  916. }
  917. void ecard_remove_driver(struct ecard_driver *drv)
  918. {
  919. driver_unregister(&drv->drv);
  920. }
  921. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  922. {
  923. struct expansion_card *ec = ECARD_DEV(_dev);
  924. struct ecard_driver *drv = ECARD_DRV(_drv);
  925. int ret;
  926. if (drv->id_table) {
  927. ret = ecard_match_device(drv->id_table, ec) != NULL;
  928. } else {
  929. ret = ec->cid.id == drv->id;
  930. }
  931. return ret;
  932. }
  933. struct bus_type ecard_bus_type = {
  934. .name = "ecard",
  935. .dev_attrs = ecard_dev_attrs,
  936. .match = ecard_match,
  937. .probe = ecard_drv_probe,
  938. .remove = ecard_drv_remove,
  939. .shutdown = ecard_drv_shutdown,
  940. };
  941. static int ecard_bus_init(void)
  942. {
  943. return bus_register(&ecard_bus_type);
  944. }
  945. postcore_initcall(ecard_bus_init);
  946. EXPORT_SYMBOL(ecard_readchunk);
  947. EXPORT_SYMBOL(ecard_register_driver);
  948. EXPORT_SYMBOL(ecard_remove_driver);
  949. EXPORT_SYMBOL(ecard_bus_type);