ecard.c 27 KB

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