PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/mturquette/linux
C | 1139 lines | 808 code | 191 blank | 140 comment | 144 complexity | ec1d3dbbedbfa10bc9a4211e18073d23 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 ecard_irq_handler(struct irq_desc *desc)
  472. {
  473. ecard_t *ec;
  474. int called = 0;
  475. desc->irq_data.chip->irq_mask(&desc->irq_data);
  476. for (ec = cards; ec; ec = ec->next) {
  477. int pending;
  478. if (!ec->claimed || !ec->irq || ec->slot_no == 8)
  479. continue;
  480. if (ec->ops && ec->ops->irqpending)
  481. pending = ec->ops->irqpending(ec);
  482. else
  483. pending = ecard_default_ops.irqpending(ec);
  484. if (pending) {
  485. generic_handle_irq(ec->irq);
  486. called ++;
  487. }
  488. }
  489. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  490. if (called == 0)
  491. ecard_check_lockup(desc);
  492. }
  493. static void __iomem *__ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
  494. {
  495. void __iomem *address = NULL;
  496. int slot = ec->slot_no;
  497. if (ec->slot_no == 8)
  498. return ECARD_MEMC8_BASE;
  499. ectcr &= ~(1 << slot);
  500. switch (type) {
  501. case ECARD_MEMC:
  502. if (slot < 4)
  503. address = ECARD_MEMC_BASE + (slot << 14);
  504. break;
  505. case ECARD_IOC:
  506. if (slot < 4)
  507. address = ECARD_IOC_BASE + (slot << 14);
  508. else
  509. address = ECARD_IOC4_BASE + ((slot - 4) << 14);
  510. if (address)
  511. address += speed << 19;
  512. break;
  513. case ECARD_EASI:
  514. address = ECARD_EASI_BASE + (slot << 24);
  515. if (speed == ECARD_FAST)
  516. ectcr |= 1 << slot;
  517. break;
  518. default:
  519. break;
  520. }
  521. #ifdef IOMD_ECTCR
  522. iomd_writeb(ectcr, IOMD_ECTCR);
  523. #endif
  524. return address;
  525. }
  526. static int ecard_prints(struct seq_file *m, ecard_t *ec)
  527. {
  528. seq_printf(m, " %d: %s ", ec->slot_no, ec->easi ? "EASI" : " ");
  529. if (ec->cid.id == 0) {
  530. struct in_chunk_dir incd;
  531. seq_printf(m, "[%04X:%04X] ",
  532. ec->cid.manufacturer, ec->cid.product);
  533. if (!ec->card_desc && ec->cid.cd &&
  534. ecard_readchunk(&incd, ec, 0xf5, 0)) {
  535. ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
  536. if (ec->card_desc)
  537. strcpy((char *)ec->card_desc, incd.d.string);
  538. }
  539. seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
  540. } else
  541. seq_printf(m, "Simple card %d\n", ec->cid.id);
  542. return 0;
  543. }
  544. static int ecard_devices_proc_show(struct seq_file *m, void *v)
  545. {
  546. ecard_t *ec = cards;
  547. while (ec) {
  548. ecard_prints(m, ec);
  549. ec = ec->next;
  550. }
  551. return 0;
  552. }
  553. static int ecard_devices_proc_open(struct inode *inode, struct file *file)
  554. {
  555. return single_open(file, ecard_devices_proc_show, NULL);
  556. }
  557. static const struct file_operations bus_ecard_proc_fops = {
  558. .owner = THIS_MODULE,
  559. .open = ecard_devices_proc_open,
  560. .read = seq_read,
  561. .llseek = seq_lseek,
  562. .release = single_release,
  563. };
  564. static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
  565. static void ecard_proc_init(void)
  566. {
  567. proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
  568. proc_create("devices", 0, proc_bus_ecard_dir, &bus_ecard_proc_fops);
  569. }
  570. #define ec_set_resource(ec,nr,st,sz) \
  571. do { \
  572. (ec)->resource[nr].name = dev_name(&ec->dev); \
  573. (ec)->resource[nr].start = st; \
  574. (ec)->resource[nr].end = (st) + (sz) - 1; \
  575. (ec)->resource[nr].flags = IORESOURCE_MEM; \
  576. } while (0)
  577. static void __init ecard_free_card(struct expansion_card *ec)
  578. {
  579. int i;
  580. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  581. if (ec->resource[i].flags)
  582. release_resource(&ec->resource[i]);
  583. kfree(ec);
  584. }
  585. static struct expansion_card *__init ecard_alloc_card(int type, int slot)
  586. {
  587. struct expansion_card *ec;
  588. unsigned long base;
  589. int i;
  590. ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
  591. if (!ec) {
  592. ec = ERR_PTR(-ENOMEM);
  593. goto nomem;
  594. }
  595. ec->slot_no = slot;
  596. ec->easi = type == ECARD_EASI;
  597. ec->irq = 0;
  598. ec->fiq = 0;
  599. ec->dma = NO_DMA;
  600. ec->ops = &ecard_default_ops;
  601. dev_set_name(&ec->dev, "ecard%d", slot);
  602. ec->dev.parent = NULL;
  603. ec->dev.bus = &ecard_bus_type;
  604. ec->dev.dma_mask = &ec->dma_mask;
  605. ec->dma_mask = (u64)0xffffffff;
  606. ec->dev.coherent_dma_mask = ec->dma_mask;
  607. if (slot < 4) {
  608. ec_set_resource(ec, ECARD_RES_MEMC,
  609. PODSLOT_MEMC_BASE + (slot << 14),
  610. PODSLOT_MEMC_SIZE);
  611. base = PODSLOT_IOC0_BASE + (slot << 14);
  612. } else
  613. base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
  614. #ifdef CONFIG_ARCH_RPC
  615. if (slot < 8) {
  616. ec_set_resource(ec, ECARD_RES_EASI,
  617. PODSLOT_EASI_BASE + (slot << 24),
  618. PODSLOT_EASI_SIZE);
  619. }
  620. if (slot == 8) {
  621. ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
  622. } else
  623. #endif
  624. for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
  625. ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
  626. base + (i << 19), PODSLOT_IOC_SIZE);
  627. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  628. if (ec->resource[i].flags &&
  629. request_resource(&iomem_resource, &ec->resource[i])) {
  630. dev_err(&ec->dev, "resource(s) not available\n");
  631. ec->resource[i].end -= ec->resource[i].start;
  632. ec->resource[i].start = 0;
  633. ec->resource[i].flags = 0;
  634. }
  635. }
  636. nomem:
  637. return ec;
  638. }
  639. static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
  640. {
  641. struct expansion_card *ec = ECARD_DEV(dev);
  642. return sprintf(buf, "%u\n", ec->irq);
  643. }
  644. static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
  645. {
  646. struct expansion_card *ec = ECARD_DEV(dev);
  647. return sprintf(buf, "%u\n", ec->dma);
  648. }
  649. static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
  650. {
  651. struct expansion_card *ec = ECARD_DEV(dev);
  652. char *str = buf;
  653. int i;
  654. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  655. str += sprintf(str, "%08x %08x %08lx\n",
  656. ec->resource[i].start,
  657. ec->resource[i].end,
  658. ec->resource[i].flags);
  659. return str - buf;
  660. }
  661. static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
  662. {
  663. struct expansion_card *ec = ECARD_DEV(dev);
  664. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  665. }
  666. static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
  667. {
  668. struct expansion_card *ec = ECARD_DEV(dev);
  669. return sprintf(buf, "%u\n", ec->cid.product);
  670. }
  671. static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
  672. {
  673. struct expansion_card *ec = ECARD_DEV(dev);
  674. return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
  675. }
  676. static struct device_attribute ecard_dev_attrs[] = {
  677. __ATTR(device, S_IRUGO, ecard_show_device, NULL),
  678. __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
  679. __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
  680. __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
  681. __ATTR(type, S_IRUGO, ecard_show_type, NULL),
  682. __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
  683. __ATTR_NULL,
  684. };
  685. int ecard_request_resources(struct expansion_card *ec)
  686. {
  687. int i, err = 0;
  688. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  689. if (ecard_resource_end(ec, i) &&
  690. !request_mem_region(ecard_resource_start(ec, i),
  691. ecard_resource_len(ec, i),
  692. ec->dev.driver->name)) {
  693. err = -EBUSY;
  694. break;
  695. }
  696. }
  697. if (err) {
  698. while (i--)
  699. if (ecard_resource_end(ec, i))
  700. release_mem_region(ecard_resource_start(ec, i),
  701. ecard_resource_len(ec, i));
  702. }
  703. return err;
  704. }
  705. EXPORT_SYMBOL(ecard_request_resources);
  706. void ecard_release_resources(struct expansion_card *ec)
  707. {
  708. int i;
  709. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  710. if (ecard_resource_end(ec, i))
  711. release_mem_region(ecard_resource_start(ec, i),
  712. ecard_resource_len(ec, i));
  713. }
  714. EXPORT_SYMBOL(ecard_release_resources);
  715. void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
  716. {
  717. ec->irq_data = irq_data;
  718. barrier();
  719. ec->ops = ops;
  720. }
  721. EXPORT_SYMBOL(ecard_setirq);
  722. void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
  723. unsigned long offset, unsigned long maxsize)
  724. {
  725. unsigned long start = ecard_resource_start(ec, res);
  726. unsigned long end = ecard_resource_end(ec, res);
  727. if (offset > (end - start))
  728. return NULL;
  729. start += offset;
  730. if (maxsize && end - start > maxsize)
  731. end = start + maxsize;
  732. return devm_ioremap(&ec->dev, start, end - start);
  733. }
  734. EXPORT_SYMBOL(ecardm_iomap);
  735. /*
  736. * Probe for an expansion card.
  737. *
  738. * If bit 1 of the first byte of the card is set, then the
  739. * card does not exist.
  740. */
  741. static int __init ecard_probe(int slot, unsigned irq, card_type_t type)
  742. {
  743. ecard_t **ecp;
  744. ecard_t *ec;
  745. struct ex_ecid cid;
  746. void __iomem *addr;
  747. int i, rc;
  748. ec = ecard_alloc_card(type, slot);
  749. if (IS_ERR(ec)) {
  750. rc = PTR_ERR(ec);
  751. goto nomem;
  752. }
  753. rc = -ENODEV;
  754. if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
  755. goto nodev;
  756. cid.r_zero = 1;
  757. ecard_readbytes(&cid, ec, 0, 16, 0);
  758. if (cid.r_zero)
  759. goto nodev;
  760. ec->cid.id = cid.r_id;
  761. ec->cid.cd = cid.r_cd;
  762. ec->cid.is = cid.r_is;
  763. ec->cid.w = cid.r_w;
  764. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  765. ec->cid.product = ecard_getu16(cid.r_prod);
  766. ec->cid.country = cid.r_country;
  767. ec->cid.irqmask = cid.r_irqmask;
  768. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  769. ec->cid.fiqmask = cid.r_fiqmask;
  770. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  771. ec->fiqaddr =
  772. ec->irqaddr = addr;
  773. if (ec->cid.is) {
  774. ec->irqmask = ec->cid.irqmask;
  775. ec->irqaddr += ec->cid.irqoff;
  776. ec->fiqmask = ec->cid.fiqmask;
  777. ec->fiqaddr += ec->cid.fiqoff;
  778. } else {
  779. ec->irqmask = 1;
  780. ec->fiqmask = 4;
  781. }
  782. for (i = 0; i < ARRAY_SIZE(blacklist); i++)
  783. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  784. blacklist[i].product == ec->cid.product) {
  785. ec->card_desc = blacklist[i].type;
  786. break;
  787. }
  788. ec->irq = irq;
  789. /*
  790. * hook the interrupt handlers
  791. */
  792. if (slot < 8) {
  793. irq_set_chip_and_handler(ec->irq, &ecard_chip,
  794. handle_level_irq);
  795. irq_set_chip_data(ec->irq, ec);
  796. irq_clear_status_flags(ec->irq, IRQ_NOREQUEST);
  797. }
  798. #ifdef CONFIG_ARCH_RPC
  799. /* On RiscPC, only first two slots have DMA capability */
  800. if (slot < 2)
  801. ec->dma = 2 + slot;
  802. #endif
  803. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  804. *ecp = ec;
  805. slot_to_expcard[slot] = ec;
  806. rc = device_register(&ec->dev);
  807. if (rc)
  808. goto nodev;
  809. return 0;
  810. nodev:
  811. ecard_free_card(ec);
  812. nomem:
  813. return rc;
  814. }
  815. /*
  816. * Initialise the expansion card system.
  817. * Locate all hardware - interrupt management and
  818. * actual cards.
  819. */
  820. static int __init ecard_init(void)
  821. {
  822. struct task_struct *task;
  823. int slot, irqbase;
  824. irqbase = irq_alloc_descs(-1, 0, 8, -1);
  825. if (irqbase < 0)
  826. return irqbase;
  827. task = kthread_run(ecard_task, NULL, "kecardd");
  828. if (IS_ERR(task)) {
  829. printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
  830. PTR_ERR(task));
  831. irq_free_descs(irqbase, 8);
  832. return PTR_ERR(task);
  833. }
  834. printk("Probing expansion cards\n");
  835. for (slot = 0; slot < 8; slot ++) {
  836. if (ecard_probe(slot, irqbase + slot, ECARD_EASI) == -ENODEV)
  837. ecard_probe(slot, irqbase + slot, ECARD_IOC);
  838. }
  839. ecard_probe(8, 11, ECARD_IOC);
  840. irq_set_chained_handler(IRQ_EXPANSIONCARD, ecard_irq_handler);
  841. ecard_proc_init();
  842. return 0;
  843. }
  844. subsys_initcall(ecard_init);
  845. /*
  846. * ECARD "bus"
  847. */
  848. static const struct ecard_id *
  849. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  850. {
  851. int i;
  852. for (i = 0; ids[i].manufacturer != 65535; i++)
  853. if (ec->cid.manufacturer == ids[i].manufacturer &&
  854. ec->cid.product == ids[i].product)
  855. return ids + i;
  856. return NULL;
  857. }
  858. static int ecard_drv_probe(struct device *dev)
  859. {
  860. struct expansion_card *ec = ECARD_DEV(dev);
  861. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  862. const struct ecard_id *id;
  863. int ret;
  864. id = ecard_match_device(drv->id_table, ec);
  865. ec->claimed = 1;
  866. ret = drv->probe(ec, id);
  867. if (ret)
  868. ec->claimed = 0;
  869. return ret;
  870. }
  871. static int ecard_drv_remove(struct device *dev)
  872. {
  873. struct expansion_card *ec = ECARD_DEV(dev);
  874. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  875. drv->remove(ec);
  876. ec->claimed = 0;
  877. /*
  878. * Restore the default operations. We ensure that the
  879. * ops are set before we change the data.
  880. */
  881. ec->ops = &ecard_default_ops;
  882. barrier();
  883. ec->irq_data = NULL;
  884. return 0;
  885. }
  886. /*
  887. * Before rebooting, we must make sure that the expansion card is in a
  888. * sensible state, so it can be re-detected. This means that the first
  889. * page of the ROM must be visible. We call the expansion cards reset
  890. * handler, if any.
  891. */
  892. static void ecard_drv_shutdown(struct device *dev)
  893. {
  894. struct expansion_card *ec = ECARD_DEV(dev);
  895. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  896. struct ecard_request req;
  897. if (dev->driver) {
  898. if (drv->shutdown)
  899. drv->shutdown(ec);
  900. ec->claimed = 0;
  901. }
  902. /*
  903. * If this card has a loader, call the reset handler.
  904. */
  905. if (ec->loader) {
  906. req.fn = ecard_task_reset;
  907. req.ec = ec;
  908. ecard_call(&req);
  909. }
  910. }
  911. int ecard_register_driver(struct ecard_driver *drv)
  912. {
  913. drv->drv.bus = &ecard_bus_type;
  914. return driver_register(&drv->drv);
  915. }
  916. void ecard_remove_driver(struct ecard_driver *drv)
  917. {
  918. driver_unregister(&drv->drv);
  919. }
  920. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  921. {
  922. struct expansion_card *ec = ECARD_DEV(_dev);
  923. struct ecard_driver *drv = ECARD_DRV(_drv);
  924. int ret;
  925. if (drv->id_table) {
  926. ret = ecard_match_device(drv->id_table, ec) != NULL;
  927. } else {
  928. ret = ec->cid.id == drv->id;
  929. }
  930. return ret;
  931. }
  932. struct bus_type ecard_bus_type = {
  933. .name = "ecard",
  934. .dev_attrs = ecard_dev_attrs,
  935. .match = ecard_match,
  936. .probe = ecard_drv_probe,
  937. .remove = ecard_drv_remove,
  938. .shutdown = ecard_drv_shutdown,
  939. };
  940. static int ecard_bus_init(void)
  941. {
  942. return bus_register(&ecard_bus_type);
  943. }
  944. postcore_initcall(ecard_bus_init);
  945. EXPORT_SYMBOL(ecard_readchunk);
  946. EXPORT_SYMBOL(ecard_register_driver);
  947. EXPORT_SYMBOL(ecard_remove_driver);
  948. EXPORT_SYMBOL(ecard_bus_type);