ecard.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 = TLB_FLUSH_VMA(mm, VM_EXEC);
  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. flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
  209. flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
  210. }
  211. static int ecard_init_mm(void)
  212. {
  213. struct mm_struct * mm = mm_alloc();
  214. struct mm_struct *active_mm = current->active_mm;
  215. if (!mm)
  216. return -ENOMEM;
  217. current->mm = mm;
  218. current->active_mm = mm;
  219. activate_mm(active_mm, mm);
  220. mmdrop(active_mm);
  221. ecard_init_pgtables(mm);
  222. return 0;
  223. }
  224. static int
  225. ecard_task(void * unused)
  226. {
  227. /*
  228. * Allocate a mm. We're not a lazy-TLB kernel task since we need
  229. * to set page table entries where the user space would be. Note
  230. * that this also creates the page tables. Failure is not an
  231. * option here.
  232. */
  233. if (ecard_init_mm())
  234. panic("kecardd: unable to alloc mm\n");
  235. while (1) {
  236. struct ecard_request *req;
  237. wait_event_interruptible(ecard_wait, ecard_req != NULL);
  238. req = xchg(&ecard_req, NULL);
  239. if (req != NULL) {
  240. req->fn(req);
  241. complete(req->complete);
  242. }
  243. }
  244. }
  245. /*
  246. * Wake the expansion card daemon to action our request.
  247. *
  248. * FIXME: The test here is not sufficient to detect if the
  249. * kcardd is running.
  250. */
  251. static void ecard_call(struct ecard_request *req)
  252. {
  253. DECLARE_COMPLETION_ONSTACK(completion);
  254. req->complete = &completion;
  255. mutex_lock(&ecard_mutex);
  256. ecard_req = req;
  257. wake_up(&ecard_wait);
  258. /*
  259. * Now wait for kecardd to run.
  260. */
  261. wait_for_completion(&completion);
  262. mutex_unlock(&ecard_mutex);
  263. }
  264. /* ======================= Mid-level card control ===================== */
  265. static void
  266. ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
  267. {
  268. struct ecard_request req;
  269. req.fn = ecard_task_readbytes;
  270. req.ec = ec;
  271. req.address = off;
  272. req.length = len;
  273. req.use_loader = useld;
  274. req.buffer = addr;
  275. ecard_call(&req);
  276. }
  277. int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
  278. {
  279. struct ex_chunk_dir excd;
  280. int index = 16;
  281. int useld = 0;
  282. if (!ec->cid.cd)
  283. return 0;
  284. while(1) {
  285. ecard_readbytes(&excd, ec, index, 8, useld);
  286. index += 8;
  287. if (c_id(&excd) == 0) {
  288. if (!useld && ec->loader) {
  289. useld = 1;
  290. index = 0;
  291. continue;
  292. }
  293. return 0;
  294. }
  295. if (c_id(&excd) == 0xf0) { /* link */
  296. index = c_start(&excd);
  297. continue;
  298. }
  299. if (c_id(&excd) == 0x80) { /* loader */
  300. if (!ec->loader) {
  301. ec->loader = kmalloc(c_len(&excd),
  302. GFP_KERNEL);
  303. if (ec->loader)
  304. ecard_readbytes(ec->loader, ec,
  305. (int)c_start(&excd),
  306. c_len(&excd), useld);
  307. else
  308. return 0;
  309. }
  310. continue;
  311. }
  312. if (c_id(&excd) == id && num-- == 0)
  313. break;
  314. }
  315. if (c_id(&excd) & 0x80) {
  316. switch (c_id(&excd) & 0x70) {
  317. case 0x70:
  318. ecard_readbytes((unsigned char *)excd.d.string, ec,
  319. (int)c_start(&excd), c_len(&excd),
  320. useld);
  321. break;
  322. case 0x00:
  323. break;
  324. }
  325. }
  326. cd->start_offset = c_start(&excd);
  327. memcpy(cd->d.string, excd.d.string, 256);
  328. return 1;
  329. }
  330. /* ======================= Interrupt control ============================ */
  331. static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
  332. {
  333. }
  334. static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
  335. {
  336. }
  337. static int ecard_def_irq_pending(ecard_t *ec)
  338. {
  339. return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
  340. }
  341. static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
  342. {
  343. panic("ecard_def_fiq_enable called - impossible");
  344. }
  345. static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
  346. {
  347. panic("ecard_def_fiq_disable called - impossible");
  348. }
  349. static int ecard_def_fiq_pending(ecard_t *ec)
  350. {
  351. return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
  352. }
  353. static expansioncard_ops_t ecard_default_ops = {
  354. ecard_def_irq_enable,
  355. ecard_def_irq_disable,
  356. ecard_def_irq_pending,
  357. ecard_def_fiq_enable,
  358. ecard_def_fiq_disable,
  359. ecard_def_fiq_pending
  360. };
  361. /*
  362. * Enable and disable interrupts from expansion cards.
  363. * (interrupts are disabled for these functions).
  364. *
  365. * They are not meant to be called directly, but via enable/disable_irq.
  366. */
  367. static void ecard_irq_unmask(struct irq_data *d)
  368. {
  369. ecard_t *ec = irq_data_get_irq_chip_data(d);
  370. if (ec) {
  371. if (!ec->ops)
  372. ec->ops = &ecard_default_ops;
  373. if (ec->claimed && ec->ops->irqenable)
  374. ec->ops->irqenable(ec, d->irq);
  375. else
  376. printk(KERN_ERR "ecard: rejecting request to "
  377. "enable IRQs for %d\n", d->irq);
  378. }
  379. }
  380. static void ecard_irq_mask(struct irq_data *d)
  381. {
  382. ecard_t *ec = irq_data_get_irq_chip_data(d);
  383. if (ec) {
  384. if (!ec->ops)
  385. ec->ops = &ecard_default_ops;
  386. if (ec->ops && ec->ops->irqdisable)
  387. ec->ops->irqdisable(ec, d->irq);
  388. }
  389. }
  390. static struct irq_chip ecard_chip = {
  391. .name = "ECARD",
  392. .irq_ack = ecard_irq_mask,
  393. .irq_mask = ecard_irq_mask,
  394. .irq_unmask = ecard_irq_unmask,
  395. };
  396. void ecard_enablefiq(unsigned int fiqnr)
  397. {
  398. ecard_t *ec = slot_to_ecard(fiqnr);
  399. if (ec) {
  400. if (!ec->ops)
  401. ec->ops = &ecard_default_ops;
  402. if (ec->claimed && ec->ops->fiqenable)
  403. ec->ops->fiqenable(ec, fiqnr);
  404. else
  405. printk(KERN_ERR "ecard: rejecting request to "
  406. "enable FIQs for %d\n", fiqnr);
  407. }
  408. }
  409. void ecard_disablefiq(unsigned int fiqnr)
  410. {
  411. ecard_t *ec = slot_to_ecard(fiqnr);
  412. if (ec) {
  413. if (!ec->ops)
  414. ec->ops = &ecard_default_ops;
  415. if (ec->ops->fiqdisable)
  416. ec->ops->fiqdisable(ec, fiqnr);
  417. }
  418. }
  419. static void ecard_dump_irq_state(void)
  420. {
  421. ecard_t *ec;
  422. printk("Expansion card IRQ state:\n");
  423. for (ec = cards; ec; ec = ec->next) {
  424. if (ec->slot_no == 8)
  425. continue;
  426. printk(" %d: %sclaimed, ",
  427. ec->slot_no, ec->claimed ? "" : "not ");
  428. if (ec->ops && ec->ops->irqpending &&
  429. ec->ops != &ecard_default_ops)
  430. printk("irq %spending\n",
  431. ec->ops->irqpending(ec) ? "" : "not ");
  432. else
  433. printk("irqaddr %p, mask = %02X, status = %02X\n",
  434. ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
  435. }
  436. }
  437. static void ecard_check_lockup(struct irq_desc *desc)
  438. {
  439. static unsigned long last;
  440. static int lockup;
  441. /*
  442. * If the timer interrupt has not run since the last million
  443. * unrecognised expansion card interrupts, then there is
  444. * something seriously wrong. Disable the expansion card
  445. * interrupts so at least we can continue.
  446. *
  447. * Maybe we ought to start a timer to re-enable them some time
  448. * later?
  449. */
  450. if (last == jiffies) {
  451. lockup += 1;
  452. if (lockup > 1000000) {
  453. printk(KERN_ERR "\nInterrupt lockup detected - "
  454. "disabling all expansion card interrupts\n");
  455. desc->irq_data.chip->irq_mask(&desc->irq_data);
  456. ecard_dump_irq_state();
  457. }
  458. } else
  459. lockup = 0;
  460. /*
  461. * If we did not recognise the source of this interrupt,
  462. * warn the user, but don't flood the user with these messages.
  463. */
  464. if (!last || time_after(jiffies, last + 5*HZ)) {
  465. last = jiffies;
  466. printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
  467. ecard_dump_irq_state();
  468. }
  469. }
  470. static void ecard_irq_handler(struct irq_desc *desc)
  471. {
  472. ecard_t *ec;
  473. int called = 0;
  474. desc->irq_data.chip->irq_mask(&desc->irq_data);
  475. for (ec = cards; ec; ec = ec->next) {
  476. int pending;
  477. if (!ec->claimed || !ec->irq || ec->slot_no == 8)
  478. continue;
  479. if (ec->ops && ec->ops->irqpending)
  480. pending = ec->ops->irqpending(ec);
  481. else
  482. pending = ecard_default_ops.irqpending(ec);
  483. if (pending) {
  484. generic_handle_irq(ec->irq);
  485. called ++;
  486. }
  487. }
  488. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  489. if (called == 0)
  490. ecard_check_lockup(desc);
  491. }
  492. static void __iomem *__ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
  493. {
  494. void __iomem *address = NULL;
  495. int slot = ec->slot_no;
  496. if (ec->slot_no == 8)
  497. return ECARD_MEMC8_BASE;
  498. ectcr &= ~(1 << slot);
  499. switch (type) {
  500. case ECARD_MEMC:
  501. if (slot < 4)
  502. address = ECARD_MEMC_BASE + (slot << 14);
  503. break;
  504. case ECARD_IOC:
  505. if (slot < 4)
  506. address = ECARD_IOC_BASE + (slot << 14);
  507. else
  508. address = ECARD_IOC4_BASE + ((slot - 4) << 14);
  509. if (address)
  510. address += speed << 19;
  511. break;
  512. case ECARD_EASI:
  513. address = ECARD_EASI_BASE + (slot << 24);
  514. if (speed == ECARD_FAST)
  515. ectcr |= 1 << slot;
  516. break;
  517. default:
  518. break;
  519. }
  520. #ifdef IOMD_ECTCR
  521. iomd_writeb(ectcr, IOMD_ECTCR);
  522. #endif
  523. return address;
  524. }
  525. static int ecard_prints(struct seq_file *m, ecard_t *ec)
  526. {
  527. seq_printf(m, " %d: %s ", ec->slot_no, ec->easi ? "EASI" : " ");
  528. if (ec->cid.id == 0) {
  529. struct in_chunk_dir incd;
  530. seq_printf(m, "[%04X:%04X] ",
  531. ec->cid.manufacturer, ec->cid.product);
  532. if (!ec->card_desc && ec->cid.cd &&
  533. ecard_readchunk(&incd, ec, 0xf5, 0)) {
  534. ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
  535. if (ec->card_desc)
  536. strcpy((char *)ec->card_desc, incd.d.string);
  537. }
  538. seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
  539. } else
  540. seq_printf(m, "Simple card %d\n", ec->cid.id);
  541. return 0;
  542. }
  543. static int ecard_devices_proc_show(struct seq_file *m, void *v)
  544. {
  545. ecard_t *ec = cards;
  546. while (ec) {
  547. ecard_prints(m, ec);
  548. ec = ec->next;
  549. }
  550. return 0;
  551. }
  552. static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
  553. static void ecard_proc_init(void)
  554. {
  555. proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
  556. proc_create_single("devices", 0, proc_bus_ecard_dir,
  557. ecard_devices_proc_show);
  558. }
  559. #define ec_set_resource(ec,nr,st,sz) \
  560. do { \
  561. (ec)->resource[nr].name = dev_name(&ec->dev); \
  562. (ec)->resource[nr].start = st; \
  563. (ec)->resource[nr].end = (st) + (sz) - 1; \
  564. (ec)->resource[nr].flags = IORESOURCE_MEM; \
  565. } while (0)
  566. static void __init ecard_free_card(struct expansion_card *ec)
  567. {
  568. int i;
  569. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  570. if (ec->resource[i].flags)
  571. release_resource(&ec->resource[i]);
  572. kfree(ec);
  573. }
  574. static struct expansion_card *__init ecard_alloc_card(int type, int slot)
  575. {
  576. struct expansion_card *ec;
  577. unsigned long base;
  578. int i;
  579. ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
  580. if (!ec) {
  581. ec = ERR_PTR(-ENOMEM);
  582. goto nomem;
  583. }
  584. ec->slot_no = slot;
  585. ec->easi = type == ECARD_EASI;
  586. ec->irq = 0;
  587. ec->fiq = 0;
  588. ec->dma = NO_DMA;
  589. ec->ops = &ecard_default_ops;
  590. dev_set_name(&ec->dev, "ecard%d", slot);
  591. ec->dev.parent = NULL;
  592. ec->dev.bus = &ecard_bus_type;
  593. ec->dev.dma_mask = &ec->dma_mask;
  594. ec->dma_mask = (u64)0xffffffff;
  595. ec->dev.coherent_dma_mask = ec->dma_mask;
  596. if (slot < 4) {
  597. ec_set_resource(ec, ECARD_RES_MEMC,
  598. PODSLOT_MEMC_BASE + (slot << 14),
  599. PODSLOT_MEMC_SIZE);
  600. base = PODSLOT_IOC0_BASE + (slot << 14);
  601. } else
  602. base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
  603. #ifdef CONFIG_ARCH_RPC
  604. if (slot < 8) {
  605. ec_set_resource(ec, ECARD_RES_EASI,
  606. PODSLOT_EASI_BASE + (slot << 24),
  607. PODSLOT_EASI_SIZE);
  608. }
  609. if (slot == 8) {
  610. ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
  611. } else
  612. #endif
  613. for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
  614. ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
  615. base + (i << 19), PODSLOT_IOC_SIZE);
  616. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  617. if (ec->resource[i].flags &&
  618. request_resource(&iomem_resource, &ec->resource[i])) {
  619. dev_err(&ec->dev, "resource(s) not available\n");
  620. ec->resource[i].end -= ec->resource[i].start;
  621. ec->resource[i].start = 0;
  622. ec->resource[i].flags = 0;
  623. }
  624. }
  625. nomem:
  626. return ec;
  627. }
  628. static ssize_t irq_show(struct device *dev, struct device_attribute *attr, char *buf)
  629. {
  630. struct expansion_card *ec = ECARD_DEV(dev);
  631. return sprintf(buf, "%u\n", ec->irq);
  632. }
  633. static DEVICE_ATTR_RO(irq);
  634. static ssize_t dma_show(struct device *dev, struct device_attribute *attr, char *buf)
  635. {
  636. struct expansion_card *ec = ECARD_DEV(dev);
  637. return sprintf(buf, "%u\n", ec->dma);
  638. }
  639. static DEVICE_ATTR_RO(dma);
  640. static ssize_t resource_show(struct device *dev, struct device_attribute *attr, char *buf)
  641. {
  642. struct expansion_card *ec = ECARD_DEV(dev);
  643. char *str = buf;
  644. int i;
  645. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  646. str += sprintf(str, "%08x %08x %08lx\n",
  647. ec->resource[i].start,
  648. ec->resource[i].end,
  649. ec->resource[i].flags);
  650. return str - buf;
  651. }
  652. static DEVICE_ATTR_RO(resource);
  653. static ssize_t vendor_show(struct device *dev, struct device_attribute *attr, char *buf)
  654. {
  655. struct expansion_card *ec = ECARD_DEV(dev);
  656. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  657. }
  658. static DEVICE_ATTR_RO(vendor);
  659. static ssize_t device_show(struct device *dev, struct device_attribute *attr, char *buf)
  660. {
  661. struct expansion_card *ec = ECARD_DEV(dev);
  662. return sprintf(buf, "%u\n", ec->cid.product);
  663. }
  664. static DEVICE_ATTR_RO(device);
  665. static ssize_t type_show(struct device *dev, struct device_attribute *attr, char *buf)
  666. {
  667. struct expansion_card *ec = ECARD_DEV(dev);
  668. return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
  669. }
  670. static DEVICE_ATTR_RO(type);
  671. static struct attribute *ecard_dev_attrs[] = {
  672. &dev_attr_device.attr,
  673. &dev_attr_dma.attr,
  674. &dev_attr_irq.attr,
  675. &dev_attr_resource.attr,
  676. &dev_attr_type.attr,
  677. &dev_attr_vendor.attr,
  678. NULL,
  679. };
  680. ATTRIBUTE_GROUPS(ecard_dev);
  681. int ecard_request_resources(struct expansion_card *ec)
  682. {
  683. int i, err = 0;
  684. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  685. if (ecard_resource_end(ec, i) &&
  686. !request_mem_region(ecard_resource_start(ec, i),
  687. ecard_resource_len(ec, i),
  688. ec->dev.driver->name)) {
  689. err = -EBUSY;
  690. break;
  691. }
  692. }
  693. if (err) {
  694. while (i--)
  695. if (ecard_resource_end(ec, i))
  696. release_mem_region(ecard_resource_start(ec, i),
  697. ecard_resource_len(ec, i));
  698. }
  699. return err;
  700. }
  701. EXPORT_SYMBOL(ecard_request_resources);
  702. void ecard_release_resources(struct expansion_card *ec)
  703. {
  704. int i;
  705. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  706. if (ecard_resource_end(ec, i))
  707. release_mem_region(ecard_resource_start(ec, i),
  708. ecard_resource_len(ec, i));
  709. }
  710. EXPORT_SYMBOL(ecard_release_resources);
  711. void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
  712. {
  713. ec->irq_data = irq_data;
  714. barrier();
  715. ec->ops = ops;
  716. }
  717. EXPORT_SYMBOL(ecard_setirq);
  718. void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
  719. unsigned long offset, unsigned long maxsize)
  720. {
  721. unsigned long start = ecard_resource_start(ec, res);
  722. unsigned long end = ecard_resource_end(ec, res);
  723. if (offset > (end - start))
  724. return NULL;
  725. start += offset;
  726. if (maxsize && end - start > maxsize)
  727. end = start + maxsize;
  728. return devm_ioremap(&ec->dev, start, end - start);
  729. }
  730. EXPORT_SYMBOL(ecardm_iomap);
  731. /*
  732. * Probe for an expansion card.
  733. *
  734. * If bit 1 of the first byte of the card is set, then the
  735. * card does not exist.
  736. */
  737. static int __init ecard_probe(int slot, unsigned irq, card_type_t type)
  738. {
  739. ecard_t **ecp;
  740. ecard_t *ec;
  741. struct ex_ecid cid;
  742. void __iomem *addr;
  743. int i, rc;
  744. ec = ecard_alloc_card(type, slot);
  745. if (IS_ERR(ec)) {
  746. rc = PTR_ERR(ec);
  747. goto nomem;
  748. }
  749. rc = -ENODEV;
  750. if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
  751. goto nodev;
  752. cid.r_zero = 1;
  753. ecard_readbytes(&cid, ec, 0, 16, 0);
  754. if (cid.r_zero)
  755. goto nodev;
  756. ec->cid.id = cid.r_id;
  757. ec->cid.cd = cid.r_cd;
  758. ec->cid.is = cid.r_is;
  759. ec->cid.w = cid.r_w;
  760. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  761. ec->cid.product = ecard_getu16(cid.r_prod);
  762. ec->cid.country = cid.r_country;
  763. ec->cid.irqmask = cid.r_irqmask;
  764. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  765. ec->cid.fiqmask = cid.r_fiqmask;
  766. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  767. ec->fiqaddr =
  768. ec->irqaddr = addr;
  769. if (ec->cid.is) {
  770. ec->irqmask = ec->cid.irqmask;
  771. ec->irqaddr += ec->cid.irqoff;
  772. ec->fiqmask = ec->cid.fiqmask;
  773. ec->fiqaddr += ec->cid.fiqoff;
  774. } else {
  775. ec->irqmask = 1;
  776. ec->fiqmask = 4;
  777. }
  778. for (i = 0; i < ARRAY_SIZE(blacklist); i++)
  779. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  780. blacklist[i].product == ec->cid.product) {
  781. ec->card_desc = blacklist[i].type;
  782. break;
  783. }
  784. ec->irq = irq;
  785. /*
  786. * hook the interrupt handlers
  787. */
  788. if (slot < 8) {
  789. irq_set_chip_and_handler(ec->irq, &ecard_chip,
  790. handle_level_irq);
  791. irq_set_chip_data(ec->irq, ec);
  792. irq_clear_status_flags(ec->irq, IRQ_NOREQUEST);
  793. }
  794. #ifdef CONFIG_ARCH_RPC
  795. /* On RiscPC, only first two slots have DMA capability */
  796. if (slot < 2)
  797. ec->dma = 2 + slot;
  798. #endif
  799. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  800. *ecp = ec;
  801. slot_to_expcard[slot] = ec;
  802. rc = device_register(&ec->dev);
  803. if (rc)
  804. goto nodev;
  805. return 0;
  806. nodev:
  807. ecard_free_card(ec);
  808. nomem:
  809. return rc;
  810. }
  811. /*
  812. * Initialise the expansion card system.
  813. * Locate all hardware - interrupt management and
  814. * actual cards.
  815. */
  816. static int __init ecard_init(void)
  817. {
  818. struct task_struct *task;
  819. int slot, irqbase;
  820. irqbase = irq_alloc_descs(-1, 0, 8, -1);
  821. if (irqbase < 0)
  822. return irqbase;
  823. task = kthread_run(ecard_task, NULL, "kecardd");
  824. if (IS_ERR(task)) {
  825. printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
  826. PTR_ERR(task));
  827. irq_free_descs(irqbase, 8);
  828. return PTR_ERR(task);
  829. }
  830. printk("Probing expansion cards\n");
  831. for (slot = 0; slot < 8; slot ++) {
  832. if (ecard_probe(slot, irqbase + slot, ECARD_EASI) == -ENODEV)
  833. ecard_probe(slot, irqbase + slot, ECARD_IOC);
  834. }
  835. ecard_probe(8, 11, ECARD_IOC);
  836. irq_set_chained_handler(IRQ_EXPANSIONCARD, ecard_irq_handler);
  837. ecard_proc_init();
  838. return 0;
  839. }
  840. subsys_initcall(ecard_init);
  841. /*
  842. * ECARD "bus"
  843. */
  844. static const struct ecard_id *
  845. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  846. {
  847. int i;
  848. for (i = 0; ids[i].manufacturer != 65535; i++)
  849. if (ec->cid.manufacturer == ids[i].manufacturer &&
  850. ec->cid.product == ids[i].product)
  851. return ids + i;
  852. return NULL;
  853. }
  854. static int ecard_drv_probe(struct device *dev)
  855. {
  856. struct expansion_card *ec = ECARD_DEV(dev);
  857. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  858. const struct ecard_id *id;
  859. int ret;
  860. id = ecard_match_device(drv->id_table, ec);
  861. ec->claimed = 1;
  862. ret = drv->probe(ec, id);
  863. if (ret)
  864. ec->claimed = 0;
  865. return ret;
  866. }
  867. static int ecard_drv_remove(struct device *dev)
  868. {
  869. struct expansion_card *ec = ECARD_DEV(dev);
  870. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  871. drv->remove(ec);
  872. ec->claimed = 0;
  873. /*
  874. * Restore the default operations. We ensure that the
  875. * ops are set before we change the data.
  876. */
  877. ec->ops = &ecard_default_ops;
  878. barrier();
  879. ec->irq_data = NULL;
  880. return 0;
  881. }
  882. /*
  883. * Before rebooting, we must make sure that the expansion card is in a
  884. * sensible state, so it can be re-detected. This means that the first
  885. * page of the ROM must be visible. We call the expansion cards reset
  886. * handler, if any.
  887. */
  888. static void ecard_drv_shutdown(struct device *dev)
  889. {
  890. struct expansion_card *ec = ECARD_DEV(dev);
  891. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  892. struct ecard_request req;
  893. if (dev->driver) {
  894. if (drv->shutdown)
  895. drv->shutdown(ec);
  896. ec->claimed = 0;
  897. }
  898. /*
  899. * If this card has a loader, call the reset handler.
  900. */
  901. if (ec->loader) {
  902. req.fn = ecard_task_reset;
  903. req.ec = ec;
  904. ecard_call(&req);
  905. }
  906. }
  907. int ecard_register_driver(struct ecard_driver *drv)
  908. {
  909. drv->drv.bus = &ecard_bus_type;
  910. return driver_register(&drv->drv);
  911. }
  912. void ecard_remove_driver(struct ecard_driver *drv)
  913. {
  914. driver_unregister(&drv->drv);
  915. }
  916. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  917. {
  918. struct expansion_card *ec = ECARD_DEV(_dev);
  919. struct ecard_driver *drv = ECARD_DRV(_drv);
  920. int ret;
  921. if (drv->id_table) {
  922. ret = ecard_match_device(drv->id_table, ec) != NULL;
  923. } else {
  924. ret = ec->cid.id == drv->id;
  925. }
  926. return ret;
  927. }
  928. struct bus_type ecard_bus_type = {
  929. .name = "ecard",
  930. .dev_groups = ecard_dev_groups,
  931. .match = ecard_match,
  932. .probe = ecard_drv_probe,
  933. .remove = ecard_drv_remove,
  934. .shutdown = ecard_drv_shutdown,
  935. };
  936. static int ecard_bus_init(void)
  937. {
  938. return bus_register(&ecard_bus_type);
  939. }
  940. postcore_initcall(ecard_bus_init);
  941. EXPORT_SYMBOL(ecard_readchunk);
  942. EXPORT_SYMBOL(ecard_register_driver);
  943. EXPORT_SYMBOL(ecard_remove_driver);
  944. EXPORT_SYMBOL(ecard_bus_type);