ecard.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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 <linux/kthread.h>
  43. #include <linux/io.h>
  44. #include <asm/dma.h>
  45. #include <asm/ecard.h>
  46. #include <asm/hardware.h>
  47. #include <asm/irq.h>
  48. #include <asm/mmu_context.h>
  49. #include <asm/mach/irq.h>
  50. #include <asm/tlbflush.h>
  51. #include "ecard.h"
  52. #ifndef CONFIG_ARCH_RPC
  53. #define HAVE_EXPMASK
  54. #endif
  55. struct ecard_request {
  56. void (*fn)(struct ecard_request *);
  57. ecard_t *ec;
  58. unsigned int address;
  59. unsigned int length;
  60. unsigned int use_loader;
  61. void *buffer;
  62. struct completion *complete;
  63. };
  64. struct expcard_blacklist {
  65. unsigned short manufacturer;
  66. unsigned short product;
  67. const char *type;
  68. };
  69. static ecard_t *cards;
  70. static ecard_t *slot_to_expcard[MAX_ECARDS];
  71. static unsigned int ectcr;
  72. #ifdef HAS_EXPMASK
  73. static unsigned int have_expmask;
  74. #endif
  75. /* List of descriptions of cards which don't have an extended
  76. * identification, or chunk directories containing a description.
  77. */
  78. static struct expcard_blacklist __initdata blacklist[] = {
  79. { MANU_ACORN, PROD_ACORN_ETHER1, "Acorn Ether1" }
  80. };
  81. asmlinkage extern int
  82. ecard_loader_reset(unsigned long base, loader_t loader);
  83. asmlinkage extern int
  84. ecard_loader_read(int off, unsigned long base, loader_t loader);
  85. static inline unsigned short ecard_getu16(unsigned char *v)
  86. {
  87. return v[0] | v[1] << 8;
  88. }
  89. static inline signed long ecard_gets24(unsigned char *v)
  90. {
  91. return v[0] | v[1] << 8 | v[2] << 16 | ((v[2] & 0x80) ? 0xff000000 : 0);
  92. }
  93. static inline ecard_t *slot_to_ecard(unsigned int slot)
  94. {
  95. return slot < MAX_ECARDS ? slot_to_expcard[slot] : NULL;
  96. }
  97. /* ===================== Expansion card daemon ======================== */
  98. /*
  99. * Since the loader programs on the expansion cards need to be run
  100. * in a specific environment, create a separate task with this
  101. * environment up, and pass requests to this task as and when we
  102. * need to.
  103. *
  104. * This should allow 99% of loaders to be called from Linux.
  105. *
  106. * From a security standpoint, we trust the card vendors. This
  107. * may be a misplaced trust.
  108. */
  109. static void ecard_task_reset(struct ecard_request *req)
  110. {
  111. struct expansion_card *ec = req->ec;
  112. struct resource *res;
  113. res = ec->slot_no == 8
  114. ? &ec->resource[ECARD_RES_MEMC]
  115. : ec->easi
  116. ? &ec->resource[ECARD_RES_EASI]
  117. : &ec->resource[ECARD_RES_IOCSYNC];
  118. ecard_loader_reset(res->start, ec->loader);
  119. }
  120. static void ecard_task_readbytes(struct ecard_request *req)
  121. {
  122. struct expansion_card *ec = req->ec;
  123. unsigned char *buf = req->buffer;
  124. unsigned int len = req->length;
  125. unsigned int off = req->address;
  126. if (ec->slot_no == 8) {
  127. void __iomem *base = (void __iomem *)
  128. ec->resource[ECARD_RES_MEMC].start;
  129. /*
  130. * The card maintains an index which increments the address
  131. * into a 4096-byte page on each access. We need to keep
  132. * track of the counter.
  133. */
  134. static unsigned int index;
  135. unsigned int page;
  136. page = (off >> 12) * 4;
  137. if (page > 256 * 4)
  138. return;
  139. off &= 4095;
  140. /*
  141. * If we are reading offset 0, or our current index is
  142. * greater than the offset, reset the hardware index counter.
  143. */
  144. if (off == 0 || index > off) {
  145. writeb(0, base);
  146. index = 0;
  147. }
  148. /*
  149. * Increment the hardware index counter until we get to the
  150. * required offset. The read bytes are discarded.
  151. */
  152. while (index < off) {
  153. readb(base + page);
  154. index += 1;
  155. }
  156. while (len--) {
  157. *buf++ = readb(base + page);
  158. index += 1;
  159. }
  160. } else {
  161. unsigned long base = (ec->easi
  162. ? &ec->resource[ECARD_RES_EASI]
  163. : &ec->resource[ECARD_RES_IOCSYNC])->start;
  164. void __iomem *pbase = (void __iomem *)base;
  165. if (!req->use_loader || !ec->loader) {
  166. off *= 4;
  167. while (len--) {
  168. *buf++ = readb(pbase + off);
  169. off += 4;
  170. }
  171. } else {
  172. while(len--) {
  173. /*
  174. * The following is required by some
  175. * expansion card loader programs.
  176. */
  177. *(unsigned long *)0x108 = 0;
  178. *buf++ = ecard_loader_read(off++, base,
  179. ec->loader);
  180. }
  181. }
  182. }
  183. }
  184. static DECLARE_WAIT_QUEUE_HEAD(ecard_wait);
  185. static struct ecard_request *ecard_req;
  186. static DEFINE_MUTEX(ecard_mutex);
  187. /*
  188. * Set up the expansion card daemon's page tables.
  189. */
  190. static void ecard_init_pgtables(struct mm_struct *mm)
  191. {
  192. struct vm_area_struct vma;
  193. /* We want to set up the page tables for the following mapping:
  194. * Virtual Physical
  195. * 0x03000000 0x03000000
  196. * 0x03010000 unmapped
  197. * 0x03210000 0x03210000
  198. * 0x03400000 unmapped
  199. * 0x08000000 0x08000000
  200. * 0x10000000 unmapped
  201. *
  202. * FIXME: we don't follow this 100% yet.
  203. */
  204. pgd_t *src_pgd, *dst_pgd;
  205. src_pgd = pgd_offset(mm, (unsigned long)IO_BASE);
  206. dst_pgd = pgd_offset(mm, IO_START);
  207. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (IO_SIZE / PGDIR_SIZE));
  208. src_pgd = pgd_offset(mm, EASI_BASE);
  209. dst_pgd = pgd_offset(mm, EASI_START);
  210. memcpy(dst_pgd, src_pgd, sizeof(pgd_t) * (EASI_SIZE / PGDIR_SIZE));
  211. vma.vm_mm = mm;
  212. flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
  213. flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
  214. }
  215. static int ecard_init_mm(void)
  216. {
  217. struct mm_struct * mm = mm_alloc();
  218. struct mm_struct *active_mm = current->active_mm;
  219. if (!mm)
  220. return -ENOMEM;
  221. current->mm = mm;
  222. current->active_mm = mm;
  223. activate_mm(active_mm, mm);
  224. mmdrop(active_mm);
  225. ecard_init_pgtables(mm);
  226. return 0;
  227. }
  228. static int
  229. ecard_task(void * unused)
  230. {
  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_ONSTACK(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 = 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 irq_chip ecard_chip = {
  407. .name = "ECARD",
  408. .ack = ecard_irq_mask,
  409. .mask = ecard_irq_mask,
  410. .unmask = ecard_irq_unmask,
  411. };
  412. void ecard_enablefiq(unsigned int fiqnr)
  413. {
  414. ecard_t *ec = slot_to_ecard(fiqnr);
  415. if (ec) {
  416. if (!ec->ops)
  417. ec->ops = &ecard_default_ops;
  418. if (ec->claimed && ec->ops->fiqenable)
  419. ec->ops->fiqenable(ec, fiqnr);
  420. else
  421. printk(KERN_ERR "ecard: rejecting request to "
  422. "enable FIQs for %d\n", fiqnr);
  423. }
  424. }
  425. void ecard_disablefiq(unsigned int fiqnr)
  426. {
  427. ecard_t *ec = slot_to_ecard(fiqnr);
  428. if (ec) {
  429. if (!ec->ops)
  430. ec->ops = &ecard_default_ops;
  431. if (ec->ops->fiqdisable)
  432. ec->ops->fiqdisable(ec, fiqnr);
  433. }
  434. }
  435. static void ecard_dump_irq_state(void)
  436. {
  437. ecard_t *ec;
  438. printk("Expansion card IRQ state:\n");
  439. for (ec = cards; ec; ec = ec->next) {
  440. if (ec->slot_no == 8)
  441. continue;
  442. printk(" %d: %sclaimed, ",
  443. ec->slot_no, ec->claimed ? "" : "not ");
  444. if (ec->ops && ec->ops->irqpending &&
  445. ec->ops != &ecard_default_ops)
  446. printk("irq %spending\n",
  447. ec->ops->irqpending(ec) ? "" : "not ");
  448. else
  449. printk("irqaddr %p, mask = %02X, status = %02X\n",
  450. ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
  451. }
  452. }
  453. static void ecard_check_lockup(struct irq_desc *desc)
  454. {
  455. static unsigned long last;
  456. static int lockup;
  457. /*
  458. * If the timer interrupt has not run since the last million
  459. * unrecognised expansion card interrupts, then there is
  460. * something seriously wrong. Disable the expansion card
  461. * interrupts so at least we can continue.
  462. *
  463. * Maybe we ought to start a timer to re-enable them some time
  464. * later?
  465. */
  466. if (last == jiffies) {
  467. lockup += 1;
  468. if (lockup > 1000000) {
  469. printk(KERN_ERR "\nInterrupt lockup detected - "
  470. "disabling all expansion card interrupts\n");
  471. desc->chip->mask(IRQ_EXPANSIONCARD);
  472. ecard_dump_irq_state();
  473. }
  474. } else
  475. lockup = 0;
  476. /*
  477. * If we did not recognise the source of this interrupt,
  478. * warn the user, but don't flood the user with these messages.
  479. */
  480. if (!last || time_after(jiffies, last + 5*HZ)) {
  481. last = jiffies;
  482. printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
  483. ecard_dump_irq_state();
  484. }
  485. }
  486. static void
  487. ecard_irq_handler(unsigned int irq, struct irq_desc *desc)
  488. {
  489. ecard_t *ec;
  490. int called = 0;
  491. desc->chip->mask(irq);
  492. for (ec = cards; ec; ec = ec->next) {
  493. int pending;
  494. if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
  495. continue;
  496. if (ec->ops && ec->ops->irqpending)
  497. pending = ec->ops->irqpending(ec);
  498. else
  499. pending = ecard_default_ops.irqpending(ec);
  500. if (pending) {
  501. struct irq_desc *d = irq_desc + ec->irq;
  502. desc_handle_irq(ec->irq, d);
  503. called ++;
  504. }
  505. }
  506. desc->chip->unmask(irq);
  507. if (called == 0)
  508. ecard_check_lockup(desc);
  509. }
  510. #ifdef HAS_EXPMASK
  511. static unsigned char priority_masks[] =
  512. {
  513. 0xf0, 0xf1, 0xf3, 0xf7, 0xff, 0xff, 0xff, 0xff
  514. };
  515. static unsigned char first_set[] =
  516. {
  517. 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00,
  518. 0x03, 0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x00
  519. };
  520. static void
  521. ecard_irqexp_handler(unsigned int irq, struct irq_desc *desc)
  522. {
  523. const unsigned int statusmask = 15;
  524. unsigned int status;
  525. status = __raw_readb(EXPMASK_STATUS) & statusmask;
  526. if (status) {
  527. unsigned int slot = first_set[status];
  528. ecard_t *ec = slot_to_ecard(slot);
  529. if (ec->claimed) {
  530. struct irq_desc *d = irq_desc + ec->irq;
  531. /*
  532. * this ugly code is so that we can operate a
  533. * prioritorising system:
  534. *
  535. * Card 0 highest priority
  536. * Card 1
  537. * Card 2
  538. * Card 3 lowest priority
  539. *
  540. * Serial cards should go in 0/1, ethernet/scsi in 2/3
  541. * otherwise you will lose serial data at high speeds!
  542. */
  543. desc_handle_irq(ec->irq, d);
  544. } else {
  545. printk(KERN_WARNING "card%d: interrupt from unclaimed "
  546. "card???\n", slot);
  547. have_expmask &= ~(1 << slot);
  548. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  549. }
  550. } else
  551. printk(KERN_WARNING "Wild interrupt from backplane (masks)\n");
  552. }
  553. static int __init ecard_probeirqhw(void)
  554. {
  555. ecard_t *ec;
  556. int found;
  557. __raw_writeb(0x00, EXPMASK_ENABLE);
  558. __raw_writeb(0xff, EXPMASK_STATUS);
  559. found = (__raw_readb(EXPMASK_STATUS) & 15) == 0;
  560. __raw_writeb(0xff, EXPMASK_ENABLE);
  561. if (found) {
  562. printk(KERN_DEBUG "Expansion card interrupt "
  563. "management hardware found\n");
  564. /* for each card present, set a bit to '1' */
  565. have_expmask = 0x80000000;
  566. for (ec = cards; ec; ec = ec->next)
  567. have_expmask |= 1 << ec->slot_no;
  568. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  569. }
  570. return found;
  571. }
  572. #else
  573. #define ecard_irqexp_handler NULL
  574. #define ecard_probeirqhw() (0)
  575. #endif
  576. #ifndef IO_EC_MEMC8_BASE
  577. #define IO_EC_MEMC8_BASE 0
  578. #endif
  579. unsigned int __ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
  580. {
  581. unsigned long address = 0;
  582. int slot = ec->slot_no;
  583. if (ec->slot_no == 8)
  584. return IO_EC_MEMC8_BASE;
  585. ectcr &= ~(1 << slot);
  586. switch (type) {
  587. case ECARD_MEMC:
  588. if (slot < 4)
  589. address = IO_EC_MEMC_BASE + (slot << 12);
  590. break;
  591. case ECARD_IOC:
  592. if (slot < 4)
  593. address = IO_EC_IOC_BASE + (slot << 12);
  594. #ifdef IO_EC_IOC4_BASE
  595. else
  596. address = IO_EC_IOC4_BASE + ((slot - 4) << 12);
  597. #endif
  598. if (address)
  599. address += speed << 17;
  600. break;
  601. #ifdef IO_EC_EASI_BASE
  602. case ECARD_EASI:
  603. address = IO_EC_EASI_BASE + (slot << 22);
  604. if (speed == ECARD_FAST)
  605. ectcr |= 1 << slot;
  606. break;
  607. #endif
  608. default:
  609. break;
  610. }
  611. #ifdef IOMD_ECTCR
  612. iomd_writeb(ectcr, IOMD_ECTCR);
  613. #endif
  614. return address;
  615. }
  616. static int ecard_prints(char *buffer, ecard_t *ec)
  617. {
  618. char *start = buffer;
  619. buffer += sprintf(buffer, " %d: %s ", ec->slot_no,
  620. ec->easi ? "EASI" : " ");
  621. if (ec->cid.id == 0) {
  622. struct in_chunk_dir incd;
  623. buffer += sprintf(buffer, "[%04X:%04X] ",
  624. ec->cid.manufacturer, ec->cid.product);
  625. if (!ec->card_desc && ec->cid.cd &&
  626. ecard_readchunk(&incd, ec, 0xf5, 0)) {
  627. ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
  628. if (ec->card_desc)
  629. strcpy((char *)ec->card_desc, incd.d.string);
  630. }
  631. buffer += sprintf(buffer, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
  632. } else
  633. buffer += sprintf(buffer, "Simple card %d\n", ec->cid.id);
  634. return buffer - start;
  635. }
  636. static int get_ecard_dev_info(char *buf, char **start, off_t pos, int count)
  637. {
  638. ecard_t *ec = cards;
  639. off_t at = 0;
  640. int len, cnt;
  641. cnt = 0;
  642. while (ec && count > cnt) {
  643. len = ecard_prints(buf, ec);
  644. at += len;
  645. if (at >= pos) {
  646. if (!*start) {
  647. *start = buf + (pos - (at - len));
  648. cnt = at - pos;
  649. } else
  650. cnt += len;
  651. buf += len;
  652. }
  653. ec = ec->next;
  654. }
  655. return (count > cnt) ? cnt : count;
  656. }
  657. static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
  658. static void ecard_proc_init(void)
  659. {
  660. proc_bus_ecard_dir = proc_mkdir("ecard", proc_bus);
  661. create_proc_info_entry("devices", 0, proc_bus_ecard_dir,
  662. get_ecard_dev_info);
  663. }
  664. #define ec_set_resource(ec,nr,st,sz) \
  665. do { \
  666. (ec)->resource[nr].name = ec->dev.bus_id; \
  667. (ec)->resource[nr].start = st; \
  668. (ec)->resource[nr].end = (st) + (sz) - 1; \
  669. (ec)->resource[nr].flags = IORESOURCE_MEM; \
  670. } while (0)
  671. static void __init ecard_free_card(struct expansion_card *ec)
  672. {
  673. int i;
  674. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  675. if (ec->resource[i].flags)
  676. release_resource(&ec->resource[i]);
  677. kfree(ec);
  678. }
  679. static struct expansion_card *__init ecard_alloc_card(int type, int slot)
  680. {
  681. struct expansion_card *ec;
  682. unsigned long base;
  683. int i;
  684. ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
  685. if (!ec) {
  686. ec = ERR_PTR(-ENOMEM);
  687. goto nomem;
  688. }
  689. ec->slot_no = slot;
  690. ec->easi = type == ECARD_EASI;
  691. ec->irq = NO_IRQ;
  692. ec->fiq = NO_IRQ;
  693. ec->dma = NO_DMA;
  694. ec->ops = &ecard_default_ops;
  695. snprintf(ec->dev.bus_id, sizeof(ec->dev.bus_id), "ecard%d", slot);
  696. ec->dev.parent = NULL;
  697. ec->dev.bus = &ecard_bus_type;
  698. ec->dev.dma_mask = &ec->dma_mask;
  699. ec->dma_mask = (u64)0xffffffff;
  700. ec->dev.coherent_dma_mask = ec->dma_mask;
  701. if (slot < 4) {
  702. ec_set_resource(ec, ECARD_RES_MEMC,
  703. PODSLOT_MEMC_BASE + (slot << 14),
  704. PODSLOT_MEMC_SIZE);
  705. base = PODSLOT_IOC0_BASE + (slot << 14);
  706. } else
  707. base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
  708. #ifdef CONFIG_ARCH_RPC
  709. if (slot < 8) {
  710. ec_set_resource(ec, ECARD_RES_EASI,
  711. PODSLOT_EASI_BASE + (slot << 24),
  712. PODSLOT_EASI_SIZE);
  713. }
  714. if (slot == 8) {
  715. ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
  716. } else
  717. #endif
  718. for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
  719. ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
  720. base + (i << 19), PODSLOT_IOC_SIZE);
  721. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  722. if (ec->resource[i].flags &&
  723. request_resource(&iomem_resource, &ec->resource[i])) {
  724. printk(KERN_ERR "%s: resource(s) not available\n",
  725. ec->dev.bus_id);
  726. ec->resource[i].end -= ec->resource[i].start;
  727. ec->resource[i].start = 0;
  728. ec->resource[i].flags = 0;
  729. }
  730. }
  731. nomem:
  732. return ec;
  733. }
  734. static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
  735. {
  736. struct expansion_card *ec = ECARD_DEV(dev);
  737. return sprintf(buf, "%u\n", ec->irq);
  738. }
  739. static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
  740. {
  741. struct expansion_card *ec = ECARD_DEV(dev);
  742. return sprintf(buf, "%u\n", ec->dma);
  743. }
  744. static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
  745. {
  746. struct expansion_card *ec = ECARD_DEV(dev);
  747. char *str = buf;
  748. int i;
  749. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  750. str += sprintf(str, "%08x %08x %08lx\n",
  751. ec->resource[i].start,
  752. ec->resource[i].end,
  753. ec->resource[i].flags);
  754. return str - buf;
  755. }
  756. static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
  757. {
  758. struct expansion_card *ec = ECARD_DEV(dev);
  759. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  760. }
  761. static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
  762. {
  763. struct expansion_card *ec = ECARD_DEV(dev);
  764. return sprintf(buf, "%u\n", ec->cid.product);
  765. }
  766. static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
  767. {
  768. struct expansion_card *ec = ECARD_DEV(dev);
  769. return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
  770. }
  771. static struct device_attribute ecard_dev_attrs[] = {
  772. __ATTR(device, S_IRUGO, ecard_show_device, NULL),
  773. __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
  774. __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
  775. __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
  776. __ATTR(type, S_IRUGO, ecard_show_type, NULL),
  777. __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
  778. __ATTR_NULL,
  779. };
  780. int ecard_request_resources(struct expansion_card *ec)
  781. {
  782. int i, err = 0;
  783. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  784. if (ecard_resource_end(ec, i) &&
  785. !request_mem_region(ecard_resource_start(ec, i),
  786. ecard_resource_len(ec, i),
  787. ec->dev.driver->name)) {
  788. err = -EBUSY;
  789. break;
  790. }
  791. }
  792. if (err) {
  793. while (i--)
  794. if (ecard_resource_end(ec, i))
  795. release_mem_region(ecard_resource_start(ec, i),
  796. ecard_resource_len(ec, i));
  797. }
  798. return err;
  799. }
  800. EXPORT_SYMBOL(ecard_request_resources);
  801. void ecard_release_resources(struct expansion_card *ec)
  802. {
  803. int i;
  804. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  805. if (ecard_resource_end(ec, i))
  806. release_mem_region(ecard_resource_start(ec, i),
  807. ecard_resource_len(ec, i));
  808. }
  809. EXPORT_SYMBOL(ecard_release_resources);
  810. void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
  811. {
  812. ec->irq_data = irq_data;
  813. barrier();
  814. ec->ops = ops;
  815. }
  816. EXPORT_SYMBOL(ecard_setirq);
  817. void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
  818. unsigned long offset, unsigned long maxsize)
  819. {
  820. unsigned long start = ecard_resource_start(ec, res);
  821. unsigned long end = ecard_resource_end(ec, res);
  822. if (offset > (end - start))
  823. return NULL;
  824. start += offset;
  825. if (maxsize && end - start > maxsize)
  826. end = start + maxsize;
  827. return devm_ioremap(&ec->dev, start, end - start);
  828. }
  829. EXPORT_SYMBOL(ecardm_iomap);
  830. /*
  831. * Probe for an expansion card.
  832. *
  833. * If bit 1 of the first byte of the card is set, then the
  834. * card does not exist.
  835. */
  836. static int __init
  837. ecard_probe(int slot, card_type_t type)
  838. {
  839. ecard_t **ecp;
  840. ecard_t *ec;
  841. struct ex_ecid cid;
  842. int i, rc;
  843. ec = ecard_alloc_card(type, slot);
  844. if (IS_ERR(ec)) {
  845. rc = PTR_ERR(ec);
  846. goto nomem;
  847. }
  848. rc = -ENODEV;
  849. if ((ec->podaddr = ecard_address(ec, type, ECARD_SYNC)) == 0)
  850. goto nodev;
  851. cid.r_zero = 1;
  852. ecard_readbytes(&cid, ec, 0, 16, 0);
  853. if (cid.r_zero)
  854. goto nodev;
  855. ec->cid.id = cid.r_id;
  856. ec->cid.cd = cid.r_cd;
  857. ec->cid.is = cid.r_is;
  858. ec->cid.w = cid.r_w;
  859. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  860. ec->cid.product = ecard_getu16(cid.r_prod);
  861. ec->cid.country = cid.r_country;
  862. ec->cid.irqmask = cid.r_irqmask;
  863. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  864. ec->cid.fiqmask = cid.r_fiqmask;
  865. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  866. ec->fiqaddr =
  867. ec->irqaddr = (void __iomem *)ioaddr(ec->podaddr);
  868. if (ec->cid.is) {
  869. ec->irqmask = ec->cid.irqmask;
  870. ec->irqaddr += ec->cid.irqoff;
  871. ec->fiqmask = ec->cid.fiqmask;
  872. ec->fiqaddr += ec->cid.fiqoff;
  873. } else {
  874. ec->irqmask = 1;
  875. ec->fiqmask = 4;
  876. }
  877. for (i = 0; i < ARRAY_SIZE(blacklist); i++)
  878. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  879. blacklist[i].product == ec->cid.product) {
  880. ec->card_desc = blacklist[i].type;
  881. break;
  882. }
  883. /*
  884. * hook the interrupt handlers
  885. */
  886. if (slot < 8) {
  887. ec->irq = 32 + slot;
  888. set_irq_chip(ec->irq, &ecard_chip);
  889. set_irq_handler(ec->irq, handle_level_irq);
  890. set_irq_flags(ec->irq, IRQF_VALID);
  891. }
  892. #ifdef IO_EC_MEMC8_BASE
  893. if (slot == 8)
  894. ec->irq = 11;
  895. #endif
  896. #ifdef CONFIG_ARCH_RPC
  897. /* On RiscPC, only first two slots have DMA capability */
  898. if (slot < 2)
  899. ec->dma = 2 + slot;
  900. #endif
  901. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  902. *ecp = ec;
  903. slot_to_expcard[slot] = ec;
  904. device_register(&ec->dev);
  905. return 0;
  906. nodev:
  907. ecard_free_card(ec);
  908. nomem:
  909. return rc;
  910. }
  911. /*
  912. * Initialise the expansion card system.
  913. * Locate all hardware - interrupt management and
  914. * actual cards.
  915. */
  916. static int __init ecard_init(void)
  917. {
  918. struct task_struct *task;
  919. int slot, irqhw;
  920. task = kthread_run(ecard_task, NULL, "kecardd");
  921. if (IS_ERR(task)) {
  922. printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
  923. PTR_ERR(task));
  924. return PTR_ERR(task);
  925. }
  926. printk("Probing expansion cards\n");
  927. for (slot = 0; slot < 8; slot ++) {
  928. if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
  929. ecard_probe(slot, ECARD_IOC);
  930. }
  931. #ifdef IO_EC_MEMC8_BASE
  932. ecard_probe(8, ECARD_IOC);
  933. #endif
  934. irqhw = ecard_probeirqhw();
  935. set_irq_chained_handler(IRQ_EXPANSIONCARD,
  936. irqhw ? ecard_irqexp_handler : ecard_irq_handler);
  937. ecard_proc_init();
  938. return 0;
  939. }
  940. subsys_initcall(ecard_init);
  941. /*
  942. * ECARD "bus"
  943. */
  944. static const struct ecard_id *
  945. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  946. {
  947. int i;
  948. for (i = 0; ids[i].manufacturer != 65535; i++)
  949. if (ec->cid.manufacturer == ids[i].manufacturer &&
  950. ec->cid.product == ids[i].product)
  951. return ids + i;
  952. return NULL;
  953. }
  954. static int ecard_drv_probe(struct device *dev)
  955. {
  956. struct expansion_card *ec = ECARD_DEV(dev);
  957. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  958. const struct ecard_id *id;
  959. int ret;
  960. id = ecard_match_device(drv->id_table, ec);
  961. ecard_claim(ec);
  962. ret = drv->probe(ec, id);
  963. if (ret)
  964. ecard_release(ec);
  965. return ret;
  966. }
  967. static int ecard_drv_remove(struct device *dev)
  968. {
  969. struct expansion_card *ec = ECARD_DEV(dev);
  970. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  971. drv->remove(ec);
  972. ecard_release(ec);
  973. /*
  974. * Restore the default operations. We ensure that the
  975. * ops are set before we change the data.
  976. */
  977. ec->ops = &ecard_default_ops;
  978. barrier();
  979. ec->irq_data = NULL;
  980. return 0;
  981. }
  982. /*
  983. * Before rebooting, we must make sure that the expansion card is in a
  984. * sensible state, so it can be re-detected. This means that the first
  985. * page of the ROM must be visible. We call the expansion cards reset
  986. * handler, if any.
  987. */
  988. static void ecard_drv_shutdown(struct device *dev)
  989. {
  990. struct expansion_card *ec = ECARD_DEV(dev);
  991. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  992. struct ecard_request req;
  993. if (dev->driver) {
  994. if (drv->shutdown)
  995. drv->shutdown(ec);
  996. ecard_release(ec);
  997. }
  998. /*
  999. * If this card has a loader, call the reset handler.
  1000. */
  1001. if (ec->loader) {
  1002. req.fn = ecard_task_reset;
  1003. req.ec = ec;
  1004. ecard_call(&req);
  1005. }
  1006. }
  1007. int ecard_register_driver(struct ecard_driver *drv)
  1008. {
  1009. drv->drv.bus = &ecard_bus_type;
  1010. return driver_register(&drv->drv);
  1011. }
  1012. void ecard_remove_driver(struct ecard_driver *drv)
  1013. {
  1014. driver_unregister(&drv->drv);
  1015. }
  1016. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  1017. {
  1018. struct expansion_card *ec = ECARD_DEV(_dev);
  1019. struct ecard_driver *drv = ECARD_DRV(_drv);
  1020. int ret;
  1021. if (drv->id_table) {
  1022. ret = ecard_match_device(drv->id_table, ec) != NULL;
  1023. } else {
  1024. ret = ec->cid.id == drv->id;
  1025. }
  1026. return ret;
  1027. }
  1028. struct bus_type ecard_bus_type = {
  1029. .name = "ecard",
  1030. .dev_attrs = ecard_dev_attrs,
  1031. .match = ecard_match,
  1032. .probe = ecard_drv_probe,
  1033. .remove = ecard_drv_remove,
  1034. .shutdown = ecard_drv_shutdown,
  1035. };
  1036. static int ecard_bus_init(void)
  1037. {
  1038. return bus_register(&ecard_bus_type);
  1039. }
  1040. postcore_initcall(ecard_bus_init);
  1041. EXPORT_SYMBOL(ecard_readchunk);
  1042. EXPORT_SYMBOL(__ecard_address);
  1043. EXPORT_SYMBOL(ecard_register_driver);
  1044. EXPORT_SYMBOL(ecard_remove_driver);
  1045. EXPORT_SYMBOL(ecard_bus_type);