ecard.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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);