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/config.h>
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/types.h>
  33. #include <linux/sched.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/device.h>
  41. #include <linux/init.h>
  42. #include <linux/mutex.h>
  43. #include <asm/dma.h>
  44. #include <asm/ecard.h>
  45. #include <asm/hardware.h>
  46. #include <asm/io.h>
  47. #include <asm/irq.h>
  48. #include <asm/mmu_context.h>
  49. #include <asm/mach/irq.h>
  50. #include <asm/tlbflush.h>
  51. #ifndef CONFIG_ARCH_RPC
  52. #define HAVE_EXPMASK
  53. #endif
  54. struct ecard_request {
  55. void (*fn)(struct ecard_request *);
  56. ecard_t *ec;
  57. unsigned int address;
  58. unsigned int length;
  59. unsigned int use_loader;
  60. void *buffer;
  61. struct completion *complete;
  62. };
  63. struct expcard_blacklist {
  64. unsigned short manufacturer;
  65. unsigned short product;
  66. const char *type;
  67. };
  68. static ecard_t *cards;
  69. static ecard_t *slot_to_expcard[MAX_ECARDS];
  70. static unsigned int ectcr;
  71. #ifdef HAS_EXPMASK
  72. static unsigned int have_expmask;
  73. #endif
  74. /* List of descriptions of cards which don't have an extended
  75. * identification, or chunk directories containing a description.
  76. */
  77. static struct expcard_blacklist __initdata blacklist[] = {
  78. { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
  79. };
  80. asmlinkage extern int
  81. ecard_loader_reset(unsigned long base, loader_t loader);
  82. asmlinkage extern int
  83. ecard_loader_read(int off, unsigned long base, loader_t loader);
  84. static inline unsigned short ecard_getu16(unsigned char *v)
  85. {
  86. return v[0] | v[1] << 8;
  87. }
  88. static inline signed long ecard_gets24(unsigned char *v)
  89. {
  90. return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
  91. }
  92. static inline ecard_t *slot_to_ecard(unsigned int slot)
  93. {
  94. return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
  95. }
  96. /* ===================== Expansion card daemon ======================== */
  97. /*
  98. * Since the loader programs on the expansion cards need to be run
  99. * in a specific environment, create a separate task with this
  100. * environment up, and pass requests to this task as and when we
  101. * need to.
  102. *
  103. * This should allow 99% of loaders to be called from Linux.
  104. *
  105. * From a security standpoint, we trust the card vendors. This
  106. * may be a misplaced trust.
  107. */
  108. static void ecard_task_reset(struct ecard_request *req)
  109. {
  110. struct expansion_card *ec = req->ec;
  111. struct resource *res;
  112. res = ec->slot_no == 8
  113. ? &ec->resource[ECARD_RES_MEMC]
  114. : ec->type == ECARD_EASI
  115. ? &ec->resource[ECARD_RES_EASI]
  116. : &ec->resource[ECARD_RES_IOCSYNC];
  117. ecard_loader_reset(res->start, ec->loader);
  118. }
  119. static void ecard_task_readbytes(struct ecard_request *req)
  120. {
  121. struct expansion_card *ec = req->ec;
  122. unsigned char *buf = req->buffer;
  123. unsigned int len = req->length;
  124. unsigned int off = req->address;
  125. if (ec->slot_no == 8) {
  126. void __iomem *base = (void __iomem *)
  127. ec->resource[ECARD_RES_MEMC].start;
  128. /*
  129. * The card maintains an index which increments the address
  130. * into a 4096-byte page on each access. We need to keep
  131. * track of the counter.
  132. */
  133. static unsigned int index;
  134. unsigned int page;
  135. page = (off >> 12) * 4;
  136. if (page > 256 * 4)
  137. return;
  138. off &= 4095;
  139. /*
  140. * If we are reading offset 0, or our current index is
  141. * greater than the offset, reset the hardware index counter.
  142. */
  143. if (off == 0 || index > off) {
  144. writeb(0, base);
  145. index = 0;
  146. }
  147. /*
  148. * Increment the hardware index counter until we get to the
  149. * required offset. The read bytes are discarded.
  150. */
  151. while (index < off) {
  152. readb(base + page);
  153. index += 1;
  154. }
  155. while (len--) {
  156. *buf++ = readb(base + page);
  157. index += 1;
  158. }
  159. } else {
  160. unsigned long base = (ec->type == ECARD_EASI
  161. ? &ec->resource[ECARD_RES_EASI]
  162. : &ec->resource[ECARD_RES_IOCSYNC])->start;
  163. void __iomem *pbase = (void __iomem *)base;
  164. if (!req->use_loader || !ec->loader) {
  165. off *= 4;
  166. while (len--) {
  167. *buf++ = readb(pbase + off);
  168. off += 4;
  169. }
  170. } else {
  171. while(len--) {
  172. /*
  173. * The following is required by some
  174. * expansion card loader programs.
  175. */
  176. *(unsigned long *)0x108 = 0;
  177. *buf++ = ecard_loader_read(off++, base,
  178. ec->loader);
  179. }
  180. }
  181. }
  182. }
  183. static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
  184. static struct ecard_request *ecard_req;
  185. static DEFINE_MUTEX(ecard_mutex);
  186. /*
  187. * Set up the expansion card daemon's page tables.
  188. */
  189. static void ecard_init_pgtables(struct mm_struct *mm)
  190. {
  191. struct vm_area_struct vma;
  192. /* We want to set up the page tables for the following mapping:
  193. * Virtual Physical
  194. * 0x03000000 0x03000000
  195. * 0x03010000 unmapped
  196. * 0x03210000 0x03210000
  197. * 0x03400000 unmapped
  198. * 0x08000000 0x08000000
  199. * 0x10000000 unmapped
  200. *
  201. * FIXME: we don't follow this 100% yet.
  202. */
  203. pgd_t *src_pgd, *dst_pgd;
  204. src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
  205. dst_pgd = pgd_offset(mm, IO_START);
  206. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
  207. src_pgd = pgd_offset(mm, EASI_BASE);
  208. dst_pgd = pgd_offset(mm, EASI_START);
  209. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
  210. vma.vm_mm = mm;
  211. flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
  212. flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
  213. }
  214. static int ecard_init_mm(void)
  215. {
  216. struct mm_struct * mm = mm_alloc();
  217. struct mm_struct *active_mm = current->active_mm;
  218. if (!mm)
  219. return -ENOMEM;
  220. current->mm = mm;
  221. current->active_mm = mm;
  222. activate_mm(active_mm, mm);
  223. mmdrop(active_mm);
  224. ecard_init_pgtables(mm);
  225. return 0;
  226. }
  227. static int
  228. ecard_task(void * unused)
  229. {
  230. daemonize("kecardd");
  231. /*
  232. * Allocate a mm. We're not a lazy-TLB kernel task since we need
  233. * to set page table entries where the user space would be. Note
  234. * that this also creates the page tables. Failure is not an
  235. * option here.
  236. */
  237. if (ecard_init_mm())
  238. panic("kecardd: unable to alloc mm\n");
  239. while (1) {
  240. struct ecard_request *req;
  241. wait_event_interruptible(ecard_wait, ecard_req != NULL);
  242. req = xchg(&ecard_req, NULL);
  243. if (req != NULL) {
  244. req->fn(req);
  245. complete(req->complete);
  246. }
  247. }
  248. }
  249. /*
  250. * Wake the expansion card daemon to action our request.
  251. *
  252. * FIXME: The test here is not sufficient to detect if the
  253. * kcardd is running.
  254. */
  255. static void ecard_call(struct ecard_request *req)
  256. {
  257. DECLARE_COMPLETION(completion);
  258. req->complete = &completion;
  259. mutex_lock(&ecard_mutex);
  260. ecard_req = req;
  261. wake_up(&ecard_wait);
  262. /*
  263. * Now wait for kecardd to run.
  264. */
  265. wait_for_completion(&completion);
  266. mutex_unlock(&ecard_mutex);
  267. }
  268. /* ======================= Mid-level card control ===================== */
  269. static void
  270. ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
  271. {
  272. struct ecard_request req;
  273. req.fn = ecard_task_readbytes;
  274. req.ec = ec;
  275. req.address = off;
  276. req.length = len;
  277. req.use_loader = useld;
  278. req.buffer = addr;
  279. ecard_call(&req);
  280. }
  281. int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
  282. {
  283. struct ex_chunk_dir excd;
  284. int index = 16;
  285. int useld = 0;
  286. if (!ec->cid.cd)
  287. return 0;
  288. while(1) {
  289. ecard_readbytes(&excd, ec, index, 8, useld);
  290. index += 8;
  291. if (c_id(&excd) == 0) {
  292. if (!useld && ec->loader) {
  293. useld = 1;
  294. index = 0;
  295. continue;
  296. }
  297. return 0;
  298. }
  299. if (c_id(&excd) == 0xf0) { /* link */
  300. index = c_start(&excd);
  301. continue;
  302. }
  303. if (c_id(&excd) == 0x80) { /* loader */
  304. if (!ec->loader) {
  305. ec->loader = (loader_t)kmalloc(c_len(&excd),
  306. GFP_KERNEL);
  307. if (ec->loader)
  308. ecard_readbytes(ec->loader, ec,
  309. (int)c_start(&excd),
  310. c_len(&excd), useld);
  311. else
  312. return 0;
  313. }
  314. continue;
  315. }
  316. if (c_id(&excd) == id && num-- == 0)
  317. break;
  318. }
  319. if (c_id(&excd) & 0x80) {
  320. switch (c_id(&excd) & 0x70) {
  321. case 0x70:
  322. ecard_readbytes((unsigned char *)excd.d.string, ec,
  323. (int)c_start(&excd), c_len(&excd),
  324. useld);
  325. break;
  326. case 0x00:
  327. break;
  328. }
  329. }
  330. cd->start_offset = c_start(&excd);
  331. memcpy(cd->d.string, excd.d.string, 256);
  332. return 1;
  333. }
  334. /* ======================= Interrupt control ============================ */
  335. static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
  336. {
  337. #ifdef HAS_EXPMASK
  338. if (irqnr < 4 && have_expmask) {
  339. have_expmask |= 1 << irqnr;
  340. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  341. }
  342. #endif
  343. }
  344. static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
  345. {
  346. #ifdef HAS_EXPMASK
  347. if (irqnr < 4 && have_expmask) {
  348. have_expmask &= ~(1 << irqnr);
  349. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  350. }
  351. #endif
  352. }
  353. static int ecard_def_irq_pending(ecard_t *ec)
  354. {
  355. return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
  356. }
  357. static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
  358. {
  359. panic("ecard_def_fiq_enable called - impossible");
  360. }
  361. static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
  362. {
  363. panic("ecard_def_fiq_disable called - impossible");
  364. }
  365. static int ecard_def_fiq_pending(ecard_t *ec)
  366. {
  367. return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
  368. }
  369. static expansioncard_ops_t ecard_default_ops = {
  370. ecard_def_irq_enable,
  371. ecard_def_irq_disable,
  372. ecard_def_irq_pending,
  373. ecard_def_fiq_enable,
  374. ecard_def_fiq_disable,
  375. ecard_def_fiq_pending
  376. };
  377. /*
  378. * Enable and disable interrupts from expansion cards.
  379. * (interrupts are disabled for these functions).
  380. *
  381. * They are not meant to be called directly, but via enable/disable_irq.
  382. */
  383. static void ecard_irq_unmask(unsigned int irqnr)
  384. {
  385. ecard_t *ec = slot_to_ecard(irqnr - 32);
  386. if (ec) {
  387. if (!ec->ops)
  388. ec->ops = &ecard_default_ops;
  389. if (ec->claimed && ec->ops->irqenable)
  390. ec->ops->irqenable(ec, irqnr);
  391. else
  392. printk(KERN_ERR "ecard: rejecting request to "
  393. "enable IRQs for %d\n", irqnr);
  394. }
  395. }
  396. static void ecard_irq_mask(unsigned int irqnr)
  397. {
  398. ecard_t *ec = slot_to_ecard(irqnr - 32);
  399. if (ec) {
  400. if (!ec->ops)
  401. ec->ops = &ecard_default_ops;
  402. if (ec->ops && ec->ops->irqdisable)
  403. ec->ops->irqdisable(ec, irqnr);
  404. }
  405. }
  406. static struct irqchip ecard_chip = {
  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 irqdesc *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 irqdesc *desc, struct pt_regs *regs)
  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 irqdesc *d = irq_desc + ec->irq;
  501. desc_handle_irq(ec->irq, d, regs);
  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 irqdesc *desc, struct pt_regs *regs)
  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 irqdesc *d = irqdesc + 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, regs);
  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, "%08lx %08lx %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 < sizeof(blacklist) / sizeof(*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, do_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);