ecard.c 28 KB

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