ecard.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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 <mach/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, (unsigned long)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_flags = VM_EXEC;
  213. vma.vm_mm = mm;
  214. flush_tlb_range(&vma, IO_START, IO_START + IO_SIZE);
  215. flush_tlb_range(&vma, EASI_START, EASI_START + EASI_SIZE);
  216. }
  217. static int ecard_init_mm(void)
  218. {
  219. struct mm_struct * mm = mm_alloc();
  220. struct mm_struct *active_mm = current->active_mm;
  221. if (!mm)
  222. return -ENOMEM;
  223. current->mm = mm;
  224. current->active_mm = mm;
  225. activate_mm(active_mm, mm);
  226. mmdrop(active_mm);
  227. ecard_init_pgtables(mm);
  228. return 0;
  229. }
  230. static int
  231. ecard_task(void * unused)
  232. {
  233. /*
  234. * Allocate a mm. We're not a lazy-TLB kernel task since we need
  235. * to set page table entries where the user space would be. Note
  236. * that this also creates the page tables. Failure is not an
  237. * option here.
  238. */
  239. if (ecard_init_mm())
  240. panic("kecardd: unable to alloc mm\n");
  241. while (1) {
  242. struct ecard_request *req;
  243. wait_event_interruptible(ecard_wait, ecard_req != NULL);
  244. req = xchg(&ecard_req, NULL);
  245. if (req != NULL) {
  246. req->fn(req);
  247. complete(req->complete);
  248. }
  249. }
  250. }
  251. /*
  252. * Wake the expansion card daemon to action our request.
  253. *
  254. * FIXME: The test here is not sufficient to detect if the
  255. * kcardd is running.
  256. */
  257. static void ecard_call(struct ecard_request *req)
  258. {
  259. DECLARE_COMPLETION_ONSTACK(completion);
  260. req->complete = &completion;
  261. mutex_lock(&ecard_mutex);
  262. ecard_req = req;
  263. wake_up(&ecard_wait);
  264. /*
  265. * Now wait for kecardd to run.
  266. */
  267. wait_for_completion(&completion);
  268. mutex_unlock(&ecard_mutex);
  269. }
  270. /* ======================= Mid-level card control ===================== */
  271. static void
  272. ecard_readbytes(void *addr, ecard_t *ec, int off, int len, int useld)
  273. {
  274. struct ecard_request req;
  275. req.fn = ecard_task_readbytes;
  276. req.ec = ec;
  277. req.address = off;
  278. req.length = len;
  279. req.use_loader = useld;
  280. req.buffer = addr;
  281. ecard_call(&req);
  282. }
  283. int ecard_readchunk(struct in_chunk_dir *cd, ecard_t *ec, int id, int num)
  284. {
  285. struct ex_chunk_dir excd;
  286. int index = 16;
  287. int useld = 0;
  288. if (!ec->cid.cd)
  289. return 0;
  290. while(1) {
  291. ecard_readbytes(&excd, ec, index, 8, useld);
  292. index += 8;
  293. if (c_id(&excd) == 0) {
  294. if (!useld && ec->loader) {
  295. useld = 1;
  296. index = 0;
  297. continue;
  298. }
  299. return 0;
  300. }
  301. if (c_id(&excd) == 0xf0) { /* link */
  302. index = c_start(&excd);
  303. continue;
  304. }
  305. if (c_id(&excd) == 0x80) { /* loader */
  306. if (!ec->loader) {
  307. ec->loader = kmalloc(c_len(&excd),
  308. GFP_KERNEL);
  309. if (ec->loader)
  310. ecard_readbytes(ec->loader, ec,
  311. (int)c_start(&excd),
  312. c_len(&excd), useld);
  313. else
  314. return 0;
  315. }
  316. continue;
  317. }
  318. if (c_id(&excd) == id && num-- == 0)
  319. break;
  320. }
  321. if (c_id(&excd) & 0x80) {
  322. switch (c_id(&excd) & 0x70) {
  323. case 0x70:
  324. ecard_readbytes((unsigned char *)excd.d.string, ec,
  325. (int)c_start(&excd), c_len(&excd),
  326. useld);
  327. break;
  328. case 0x00:
  329. break;
  330. }
  331. }
  332. cd->start_offset = c_start(&excd);
  333. memcpy(cd->d.string, excd.d.string, 256);
  334. return 1;
  335. }
  336. /* ======================= Interrupt control ============================ */
  337. static void ecard_def_irq_enable(ecard_t *ec, int irqnr)
  338. {
  339. #ifdef HAS_EXPMASK
  340. if (irqnr < 4 && have_expmask) {
  341. have_expmask |= 1 << irqnr;
  342. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  343. }
  344. #endif
  345. }
  346. static void ecard_def_irq_disable(ecard_t *ec, int irqnr)
  347. {
  348. #ifdef HAS_EXPMASK
  349. if (irqnr < 4 && have_expmask) {
  350. have_expmask &= ~(1 << irqnr);
  351. __raw_writeb(have_expmask, EXPMASK_ENABLE);
  352. }
  353. #endif
  354. }
  355. static int ecard_def_irq_pending(ecard_t *ec)
  356. {
  357. return !ec->irqmask || readb(ec->irqaddr) & ec->irqmask;
  358. }
  359. static void ecard_def_fiq_enable(ecard_t *ec, int fiqnr)
  360. {
  361. panic("ecard_def_fiq_enable called - impossible");
  362. }
  363. static void ecard_def_fiq_disable(ecard_t *ec, int fiqnr)
  364. {
  365. panic("ecard_def_fiq_disable called - impossible");
  366. }
  367. static int ecard_def_fiq_pending(ecard_t *ec)
  368. {
  369. return !ec->fiqmask || readb(ec->fiqaddr) & ec->fiqmask;
  370. }
  371. static expansioncard_ops_t ecard_default_ops = {
  372. ecard_def_irq_enable,
  373. ecard_def_irq_disable,
  374. ecard_def_irq_pending,
  375. ecard_def_fiq_enable,
  376. ecard_def_fiq_disable,
  377. ecard_def_fiq_pending
  378. };
  379. /*
  380. * Enable and disable interrupts from expansion cards.
  381. * (interrupts are disabled for these functions).
  382. *
  383. * They are not meant to be called directly, but via enable/disable_irq.
  384. */
  385. static void ecard_irq_unmask(struct irq_data *d)
  386. {
  387. ecard_t *ec = slot_to_ecard(d->irq - 32);
  388. if (ec) {
  389. if (!ec->ops)
  390. ec->ops = &ecard_default_ops;
  391. if (ec->claimed && ec->ops->irqenable)
  392. ec->ops->irqenable(ec, d->irq);
  393. else
  394. printk(KERN_ERR "ecard: rejecting request to "
  395. "enable IRQs for %d\n", d->irq);
  396. }
  397. }
  398. static void ecard_irq_mask(struct irq_data *d)
  399. {
  400. ecard_t *ec = slot_to_ecard(d->irq - 32);
  401. if (ec) {
  402. if (!ec->ops)
  403. ec->ops = &ecard_default_ops;
  404. if (ec->ops && ec->ops->irqdisable)
  405. ec->ops->irqdisable(ec, d->irq);
  406. }
  407. }
  408. static struct irq_chip ecard_chip = {
  409. .name = "ECARD",
  410. .irq_ack = ecard_irq_mask,
  411. .irq_mask = ecard_irq_mask,
  412. .irq_unmask = ecard_irq_unmask,
  413. };
  414. void ecard_enablefiq(unsigned int fiqnr)
  415. {
  416. ecard_t *ec = slot_to_ecard(fiqnr);
  417. if (ec) {
  418. if (!ec->ops)
  419. ec->ops = &ecard_default_ops;
  420. if (ec->claimed && ec->ops->fiqenable)
  421. ec->ops->fiqenable(ec, fiqnr);
  422. else
  423. printk(KERN_ERR "ecard: rejecting request to "
  424. "enable FIQs for %d\n", fiqnr);
  425. }
  426. }
  427. void ecard_disablefiq(unsigned int fiqnr)
  428. {
  429. ecard_t *ec = slot_to_ecard(fiqnr);
  430. if (ec) {
  431. if (!ec->ops)
  432. ec->ops = &ecard_default_ops;
  433. if (ec->ops->fiqdisable)
  434. ec->ops->fiqdisable(ec, fiqnr);
  435. }
  436. }
  437. static void ecard_dump_irq_state(void)
  438. {
  439. ecard_t *ec;
  440. printk("Expansion card IRQ state:\n");
  441. for (ec = cards; ec; ec = ec->next) {
  442. if (ec->slot_no == 8)
  443. continue;
  444. printk(" %d: %sclaimed, ",
  445. ec->slot_no, ec->claimed ? "" : "not ");
  446. if (ec->ops && ec->ops->irqpending &&
  447. ec->ops != &ecard_default_ops)
  448. printk("irq %spending\n",
  449. ec->ops->irqpending(ec) ? "" : "not ");
  450. else
  451. printk("irqaddr %p, mask = %02X, status = %02X\n",
  452. ec->irqaddr, ec->irqmask, readb(ec->irqaddr));
  453. }
  454. }
  455. static void ecard_check_lockup(struct irq_desc *desc)
  456. {
  457. static unsigned long last;
  458. static int lockup;
  459. /*
  460. * If the timer interrupt has not run since the last million
  461. * unrecognised expansion card interrupts, then there is
  462. * something seriously wrong. Disable the expansion card
  463. * interrupts so at least we can continue.
  464. *
  465. * Maybe we ought to start a timer to re-enable them some time
  466. * later?
  467. */
  468. if (last == jiffies) {
  469. lockup += 1;
  470. if (lockup > 1000000) {
  471. printk(KERN_ERR "\nInterrupt lockup detected - "
  472. "disabling all expansion card interrupts\n");
  473. desc->irq_data.chip->irq_mask(&desc->irq_data);
  474. ecard_dump_irq_state();
  475. }
  476. } else
  477. lockup = 0;
  478. /*
  479. * If we did not recognise the source of this interrupt,
  480. * warn the user, but don't flood the user with these messages.
  481. */
  482. if (!last || time_after(jiffies, last + 5*HZ)) {
  483. last = jiffies;
  484. printk(KERN_WARNING "Unrecognised interrupt from backplane\n");
  485. ecard_dump_irq_state();
  486. }
  487. }
  488. static void
  489. ecard_irq_handler(unsigned int irq, struct irq_desc *desc)
  490. {
  491. ecard_t *ec;
  492. int called = 0;
  493. desc->irq_data.chip->irq_mask(&desc->irq_data);
  494. for (ec = cards; ec; ec = ec->next) {
  495. int pending;
  496. if (!ec->claimed || ec->irq == NO_IRQ || ec->slot_no == 8)
  497. continue;
  498. if (ec->ops && ec->ops->irqpending)
  499. pending = ec->ops->irqpending(ec);
  500. else
  501. pending = ecard_default_ops.irqpending(ec);
  502. if (pending) {
  503. generic_handle_irq(ec->irq);
  504. called ++;
  505. }
  506. }
  507. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  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. /*
  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. generic_handle_irq(ec->irq);
  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. static void __iomem *__ecard_address(ecard_t *ec, card_type_t type, card_speed_t speed)
  577. {
  578. void __iomem *address = NULL;
  579. int slot = ec->slot_no;
  580. if (ec->slot_no == 8)
  581. return ECARD_MEMC8_BASE;
  582. ectcr &= ~(1 << slot);
  583. switch (type) {
  584. case ECARD_MEMC:
  585. if (slot < 4)
  586. address = ECARD_MEMC_BASE + (slot << 14);
  587. break;
  588. case ECARD_IOC:
  589. if (slot < 4)
  590. address = ECARD_IOC_BASE + (slot << 14);
  591. else
  592. address = ECARD_IOC4_BASE + ((slot - 4) << 14);
  593. if (address)
  594. address += speed << 19;
  595. break;
  596. case ECARD_EASI:
  597. address = ECARD_EASI_BASE + (slot << 24);
  598. if (speed == ECARD_FAST)
  599. ectcr |= 1 << slot;
  600. break;
  601. default:
  602. break;
  603. }
  604. #ifdef IOMD_ECTCR
  605. iomd_writeb(ectcr, IOMD_ECTCR);
  606. #endif
  607. return address;
  608. }
  609. static int ecard_prints(struct seq_file *m, ecard_t *ec)
  610. {
  611. seq_printf(m, " %d: %s ", ec->slot_no, ec->easi ? "EASI" : " ");
  612. if (ec->cid.id == 0) {
  613. struct in_chunk_dir incd;
  614. seq_printf(m, "[%04X:%04X] ",
  615. ec->cid.manufacturer, ec->cid.product);
  616. if (!ec->card_desc && ec->cid.cd &&
  617. ecard_readchunk(&incd, ec, 0xf5, 0)) {
  618. ec->card_desc = kmalloc(strlen(incd.d.string)+1, GFP_KERNEL);
  619. if (ec->card_desc)
  620. strcpy((char *)ec->card_desc, incd.d.string);
  621. }
  622. seq_printf(m, "%s\n", ec->card_desc ? ec->card_desc : "*unknown*");
  623. } else
  624. seq_printf(m, "Simple card %d\n", ec->cid.id);
  625. return 0;
  626. }
  627. static int ecard_devices_proc_show(struct seq_file *m, void *v)
  628. {
  629. ecard_t *ec = cards;
  630. while (ec) {
  631. ecard_prints(m, ec);
  632. ec = ec->next;
  633. }
  634. return 0;
  635. }
  636. static int ecard_devices_proc_open(struct inode *inode, struct file *file)
  637. {
  638. return single_open(file, ecard_devices_proc_show, NULL);
  639. }
  640. static const struct file_operations bus_ecard_proc_fops = {
  641. .owner = THIS_MODULE,
  642. .open = ecard_devices_proc_open,
  643. .read = seq_read,
  644. .llseek = seq_lseek,
  645. .release = single_release,
  646. };
  647. static struct proc_dir_entry *proc_bus_ecard_dir = NULL;
  648. static void ecard_proc_init(void)
  649. {
  650. proc_bus_ecard_dir = proc_mkdir("bus/ecard", NULL);
  651. proc_create("devices", 0, proc_bus_ecard_dir, &bus_ecard_proc_fops);
  652. }
  653. #define ec_set_resource(ec,nr,st,sz) \
  654. do { \
  655. (ec)->resource[nr].name = dev_name(&ec->dev); \
  656. (ec)->resource[nr].start = st; \
  657. (ec)->resource[nr].end = (st) + (sz) - 1; \
  658. (ec)->resource[nr].flags = IORESOURCE_MEM; \
  659. } while (0)
  660. static void __init ecard_free_card(struct expansion_card *ec)
  661. {
  662. int i;
  663. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  664. if (ec->resource[i].flags)
  665. release_resource(&ec->resource[i]);
  666. kfree(ec);
  667. }
  668. static struct expansion_card *__init ecard_alloc_card(int type, int slot)
  669. {
  670. struct expansion_card *ec;
  671. unsigned long base;
  672. int i;
  673. ec = kzalloc(sizeof(ecard_t), GFP_KERNEL);
  674. if (!ec) {
  675. ec = ERR_PTR(-ENOMEM);
  676. goto nomem;
  677. }
  678. ec->slot_no = slot;
  679. ec->easi = type == ECARD_EASI;
  680. ec->irq = NO_IRQ;
  681. ec->fiq = NO_IRQ;
  682. ec->dma = NO_DMA;
  683. ec->ops = &ecard_default_ops;
  684. dev_set_name(&ec->dev, "ecard%d", slot);
  685. ec->dev.parent = NULL;
  686. ec->dev.bus = &ecard_bus_type;
  687. ec->dev.dma_mask = &ec->dma_mask;
  688. ec->dma_mask = (u64)0xffffffff;
  689. ec->dev.coherent_dma_mask = ec->dma_mask;
  690. if (slot < 4) {
  691. ec_set_resource(ec, ECARD_RES_MEMC,
  692. PODSLOT_MEMC_BASE + (slot << 14),
  693. PODSLOT_MEMC_SIZE);
  694. base = PODSLOT_IOC0_BASE + (slot << 14);
  695. } else
  696. base = PODSLOT_IOC4_BASE + ((slot - 4) << 14);
  697. #ifdef CONFIG_ARCH_RPC
  698. if (slot < 8) {
  699. ec_set_resource(ec, ECARD_RES_EASI,
  700. PODSLOT_EASI_BASE + (slot << 24),
  701. PODSLOT_EASI_SIZE);
  702. }
  703. if (slot == 8) {
  704. ec_set_resource(ec, ECARD_RES_MEMC, NETSLOT_BASE, NETSLOT_SIZE);
  705. } else
  706. #endif
  707. for (i = 0; i <= ECARD_RES_IOCSYNC - ECARD_RES_IOCSLOW; i++)
  708. ec_set_resource(ec, i + ECARD_RES_IOCSLOW,
  709. base + (i << 19), PODSLOT_IOC_SIZE);
  710. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  711. if (ec->resource[i].flags &&
  712. request_resource(&iomem_resource, &ec->resource[i])) {
  713. dev_err(&ec->dev, "resource(s) not available\n");
  714. ec->resource[i].end -= ec->resource[i].start;
  715. ec->resource[i].start = 0;
  716. ec->resource[i].flags = 0;
  717. }
  718. }
  719. nomem:
  720. return ec;
  721. }
  722. static ssize_t ecard_show_irq(struct device *dev, struct device_attribute *attr, char *buf)
  723. {
  724. struct expansion_card *ec = ECARD_DEV(dev);
  725. return sprintf(buf, "%u\n", ec->irq);
  726. }
  727. static ssize_t ecard_show_dma(struct device *dev, struct device_attribute *attr, char *buf)
  728. {
  729. struct expansion_card *ec = ECARD_DEV(dev);
  730. return sprintf(buf, "%u\n", ec->dma);
  731. }
  732. static ssize_t ecard_show_resources(struct device *dev, struct device_attribute *attr, char *buf)
  733. {
  734. struct expansion_card *ec = ECARD_DEV(dev);
  735. char *str = buf;
  736. int i;
  737. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  738. str += sprintf(str, "%08x %08x %08lx\n",
  739. ec->resource[i].start,
  740. ec->resource[i].end,
  741. ec->resource[i].flags);
  742. return str - buf;
  743. }
  744. static ssize_t ecard_show_vendor(struct device *dev, struct device_attribute *attr, char *buf)
  745. {
  746. struct expansion_card *ec = ECARD_DEV(dev);
  747. return sprintf(buf, "%u\n", ec->cid.manufacturer);
  748. }
  749. static ssize_t ecard_show_device(struct device *dev, struct device_attribute *attr, char *buf)
  750. {
  751. struct expansion_card *ec = ECARD_DEV(dev);
  752. return sprintf(buf, "%u\n", ec->cid.product);
  753. }
  754. static ssize_t ecard_show_type(struct device *dev, struct device_attribute *attr, char *buf)
  755. {
  756. struct expansion_card *ec = ECARD_DEV(dev);
  757. return sprintf(buf, "%s\n", ec->easi ? "EASI" : "IOC");
  758. }
  759. static struct device_attribute ecard_dev_attrs[] = {
  760. __ATTR(device, S_IRUGO, ecard_show_device, NULL),
  761. __ATTR(dma, S_IRUGO, ecard_show_dma, NULL),
  762. __ATTR(irq, S_IRUGO, ecard_show_irq, NULL),
  763. __ATTR(resource, S_IRUGO, ecard_show_resources, NULL),
  764. __ATTR(type, S_IRUGO, ecard_show_type, NULL),
  765. __ATTR(vendor, S_IRUGO, ecard_show_vendor, NULL),
  766. __ATTR_NULL,
  767. };
  768. int ecard_request_resources(struct expansion_card *ec)
  769. {
  770. int i, err = 0;
  771. for (i = 0; i < ECARD_NUM_RESOURCES; i++) {
  772. if (ecard_resource_end(ec, i) &&
  773. !request_mem_region(ecard_resource_start(ec, i),
  774. ecard_resource_len(ec, i),
  775. ec->dev.driver->name)) {
  776. err = -EBUSY;
  777. break;
  778. }
  779. }
  780. if (err) {
  781. while (i--)
  782. if (ecard_resource_end(ec, i))
  783. release_mem_region(ecard_resource_start(ec, i),
  784. ecard_resource_len(ec, i));
  785. }
  786. return err;
  787. }
  788. EXPORT_SYMBOL(ecard_request_resources);
  789. void ecard_release_resources(struct expansion_card *ec)
  790. {
  791. int i;
  792. for (i = 0; i < ECARD_NUM_RESOURCES; i++)
  793. if (ecard_resource_end(ec, i))
  794. release_mem_region(ecard_resource_start(ec, i),
  795. ecard_resource_len(ec, i));
  796. }
  797. EXPORT_SYMBOL(ecard_release_resources);
  798. void ecard_setirq(struct expansion_card *ec, const struct expansion_card_ops *ops, void *irq_data)
  799. {
  800. ec->irq_data = irq_data;
  801. barrier();
  802. ec->ops = ops;
  803. }
  804. EXPORT_SYMBOL(ecard_setirq);
  805. void __iomem *ecardm_iomap(struct expansion_card *ec, unsigned int res,
  806. unsigned long offset, unsigned long maxsize)
  807. {
  808. unsigned long start = ecard_resource_start(ec, res);
  809. unsigned long end = ecard_resource_end(ec, res);
  810. if (offset > (end - start))
  811. return NULL;
  812. start += offset;
  813. if (maxsize && end - start > maxsize)
  814. end = start + maxsize;
  815. return devm_ioremap(&ec->dev, start, end - start);
  816. }
  817. EXPORT_SYMBOL(ecardm_iomap);
  818. /*
  819. * Probe for an expansion card.
  820. *
  821. * If bit 1 of the first byte of the card is set, then the
  822. * card does not exist.
  823. */
  824. static int __init
  825. ecard_probe(int slot, card_type_t type)
  826. {
  827. ecard_t **ecp;
  828. ecard_t *ec;
  829. struct ex_ecid cid;
  830. void __iomem *addr;
  831. int i, rc;
  832. ec = ecard_alloc_card(type, slot);
  833. if (IS_ERR(ec)) {
  834. rc = PTR_ERR(ec);
  835. goto nomem;
  836. }
  837. rc = -ENODEV;
  838. if ((addr = __ecard_address(ec, type, ECARD_SYNC)) == NULL)
  839. goto nodev;
  840. cid.r_zero = 1;
  841. ecard_readbytes(&cid, ec, 0, 16, 0);
  842. if (cid.r_zero)
  843. goto nodev;
  844. ec->cid.id = cid.r_id;
  845. ec->cid.cd = cid.r_cd;
  846. ec->cid.is = cid.r_is;
  847. ec->cid.w = cid.r_w;
  848. ec->cid.manufacturer = ecard_getu16(cid.r_manu);
  849. ec->cid.product = ecard_getu16(cid.r_prod);
  850. ec->cid.country = cid.r_country;
  851. ec->cid.irqmask = cid.r_irqmask;
  852. ec->cid.irqoff = ecard_gets24(cid.r_irqoff);
  853. ec->cid.fiqmask = cid.r_fiqmask;
  854. ec->cid.fiqoff = ecard_gets24(cid.r_fiqoff);
  855. ec->fiqaddr =
  856. ec->irqaddr = addr;
  857. if (ec->cid.is) {
  858. ec->irqmask = ec->cid.irqmask;
  859. ec->irqaddr += ec->cid.irqoff;
  860. ec->fiqmask = ec->cid.fiqmask;
  861. ec->fiqaddr += ec->cid.fiqoff;
  862. } else {
  863. ec->irqmask = 1;
  864. ec->fiqmask = 4;
  865. }
  866. for (i = 0; i < ARRAY_SIZE(blacklist); i++)
  867. if (blacklist[i].manufacturer == ec->cid.manufacturer &&
  868. blacklist[i].product == ec->cid.product) {
  869. ec->card_desc = blacklist[i].type;
  870. break;
  871. }
  872. /*
  873. * hook the interrupt handlers
  874. */
  875. if (slot < 8) {
  876. ec->irq = 32 + slot;
  877. irq_set_chip_and_handler(ec->irq, &ecard_chip,
  878. handle_level_irq);
  879. set_irq_flags(ec->irq, IRQF_VALID);
  880. }
  881. if (slot == 8)
  882. ec->irq = 11;
  883. #ifdef CONFIG_ARCH_RPC
  884. /* On RiscPC, only first two slots have DMA capability */
  885. if (slot < 2)
  886. ec->dma = 2 + slot;
  887. #endif
  888. for (ecp = &cards; *ecp; ecp = &(*ecp)->next);
  889. *ecp = ec;
  890. slot_to_expcard[slot] = ec;
  891. device_register(&ec->dev);
  892. return 0;
  893. nodev:
  894. ecard_free_card(ec);
  895. nomem:
  896. return rc;
  897. }
  898. /*
  899. * Initialise the expansion card system.
  900. * Locate all hardware - interrupt management and
  901. * actual cards.
  902. */
  903. static int __init ecard_init(void)
  904. {
  905. struct task_struct *task;
  906. int slot, irqhw;
  907. task = kthread_run(ecard_task, NULL, "kecardd");
  908. if (IS_ERR(task)) {
  909. printk(KERN_ERR "Ecard: unable to create kernel thread: %ld\n",
  910. PTR_ERR(task));
  911. return PTR_ERR(task);
  912. }
  913. printk("Probing expansion cards\n");
  914. for (slot = 0; slot < 8; slot ++) {
  915. if (ecard_probe(slot, ECARD_EASI) == -ENODEV)
  916. ecard_probe(slot, ECARD_IOC);
  917. }
  918. ecard_probe(8, ECARD_IOC);
  919. irqhw = ecard_probeirqhw();
  920. irq_set_chained_handler(IRQ_EXPANSIONCARD,
  921. irqhw ? ecard_irqexp_handler : ecard_irq_handler);
  922. ecard_proc_init();
  923. return 0;
  924. }
  925. subsys_initcall(ecard_init);
  926. /*
  927. * ECARD "bus"
  928. */
  929. static const struct ecard_id *
  930. ecard_match_device(const struct ecard_id *ids, struct expansion_card *ec)
  931. {
  932. int i;
  933. for (i = 0; ids[i].manufacturer != 65535; i++)
  934. if (ec->cid.manufacturer == ids[i].manufacturer &&
  935. ec->cid.product == ids[i].product)
  936. return ids + i;
  937. return NULL;
  938. }
  939. static int ecard_drv_probe(struct device *dev)
  940. {
  941. struct expansion_card *ec = ECARD_DEV(dev);
  942. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  943. const struct ecard_id *id;
  944. int ret;
  945. id = ecard_match_device(drv->id_table, ec);
  946. ec->claimed = 1;
  947. ret = drv->probe(ec, id);
  948. if (ret)
  949. ec->claimed = 0;
  950. return ret;
  951. }
  952. static int ecard_drv_remove(struct device *dev)
  953. {
  954. struct expansion_card *ec = ECARD_DEV(dev);
  955. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  956. drv->remove(ec);
  957. ec->claimed = 0;
  958. /*
  959. * Restore the default operations. We ensure that the
  960. * ops are set before we change the data.
  961. */
  962. ec->ops = &ecard_default_ops;
  963. barrier();
  964. ec->irq_data = NULL;
  965. return 0;
  966. }
  967. /*
  968. * Before rebooting, we must make sure that the expansion card is in a
  969. * sensible state, so it can be re-detected. This means that the first
  970. * page of the ROM must be visible. We call the expansion cards reset
  971. * handler, if any.
  972. */
  973. static void ecard_drv_shutdown(struct device *dev)
  974. {
  975. struct expansion_card *ec = ECARD_DEV(dev);
  976. struct ecard_driver *drv = ECARD_DRV(dev->driver);
  977. struct ecard_request req;
  978. if (dev->driver) {
  979. if (drv->shutdown)
  980. drv->shutdown(ec);
  981. ec->claimed = 0;
  982. }
  983. /*
  984. * If this card has a loader, call the reset handler.
  985. */
  986. if (ec->loader) {
  987. req.fn = ecard_task_reset;
  988. req.ec = ec;
  989. ecard_call(&req);
  990. }
  991. }
  992. int ecard_register_driver(struct ecard_driver *drv)
  993. {
  994. drv->drv.bus = &ecard_bus_type;
  995. return driver_register(&drv->drv);
  996. }
  997. void ecard_remove_driver(struct ecard_driver *drv)
  998. {
  999. driver_unregister(&drv->drv);
  1000. }
  1001. static int ecard_match(struct device *_dev, struct device_driver *_drv)
  1002. {
  1003. struct expansion_card *ec = ECARD_DEV(_dev);
  1004. struct ecard_driver *drv = ECARD_DRV(_drv);
  1005. int ret;
  1006. if (drv->id_table) {
  1007. ret = ecard_match_device(drv->id_table, ec) != NULL;
  1008. } else {
  1009. ret = ec->cid.id == drv->id;
  1010. }
  1011. return ret;
  1012. }
  1013. struct bus_type ecard_bus_type = {
  1014. .name = "ecard",
  1015. .dev_attrs = ecard_dev_attrs,
  1016. .match = ecard_match,
  1017. .probe = ecard_drv_probe,
  1018. .remove = ecard_drv_remove,
  1019. .shutdown = ecard_drv_shutdown,
  1020. };
  1021. static int ecard_bus_init(void)
  1022. {
  1023. return bus_register(&ecard_bus_type);
  1024. }
  1025. postcore_initcall(ecard_bus_init);
  1026. EXPORT_SYMBOL(ecard_readchunk);
  1027. EXPORT_SYMBOL(ecard_register_driver);
  1028. EXPORT_SYMBOL(ecard_remove_driver);
  1029. EXPORT_SYMBOL(ecard_bus_type);