ecard.c 25 KB

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