comempci.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*****************************************************************************/
  2. /*
  3. * comemlite.c -- PCI access code for embedded CO-MEM Lite PCI controller.
  4. *
  5. * (C) Copyright 1999-2003, Greg Ungerer (gerg@snapgear.com).
  6. * (C) Copyright 2000, Lineo (www.lineo.com)
  7. */
  8. /*****************************************************************************/
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/pci.h>
  12. #include <linux/ptrace.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/sched.h>
  16. #include <asm/coldfire.h>
  17. #include <asm/mcfsim.h>
  18. #include <asm/irq.h>
  19. #include <asm/anchor.h>
  20. #ifdef CONFIG_eLIA
  21. #include <asm/elia.h>
  22. #endif
  23. /*****************************************************************************/
  24. /*
  25. * Debug configuration defines. DEBUGRES sets debugging output for
  26. * the resource allocation phase. DEBUGPCI traces on pcibios_ function
  27. * calls, and DEBUGIO traces all accesses to devices on the PCI bus.
  28. */
  29. /*#define DEBUGRES 1*/
  30. /*#define DEBUGPCI 1*/
  31. /*#define DEBUGIO 1*/
  32. /*****************************************************************************/
  33. /*
  34. * PCI markers for bus present and active slots.
  35. */
  36. int pci_bus_is_present = 0;
  37. unsigned long pci_slotmask = 0;
  38. /*
  39. * We may or may not need to swap the bytes of PCI bus tranfers.
  40. * The endianess is re-roder automatically by the CO-MEM, but it
  41. * will get the wrong byte order for a pure data stream.
  42. */
  43. #define pci_byteswap 0
  44. /*
  45. * Resource tracking. The CO-MEM part creates a virtual address
  46. * space that all the PCI devices live in - it is not in any way
  47. * directly mapped into the ColdFire address space. So we can
  48. * really assign any resources we like to devices, as long as
  49. * they do not clash with other PCI devices.
  50. */
  51. unsigned int pci_iobase = PCIBIOS_MIN_IO; /* Arbitrary start address */
  52. unsigned int pci_membase = PCIBIOS_MIN_MEM; /* Arbitrary start address */
  53. #define PCI_MINIO 0x100 /* 256 byte minimum I/O */
  54. #define PCI_MINMEM 0x00010000 /* 64k minimum chunk */
  55. /*
  56. * The CO-MEM's shared memory segment is visible inside the PCI
  57. * memory address space. We need to keep track of the address that
  58. * this is mapped at, to setup the bus masters pointers.
  59. */
  60. unsigned int pci_shmemaddr;
  61. /*****************************************************************************/
  62. void pci_interrupt(int irq, void *id, struct pt_regs *fp);
  63. /*****************************************************************************/
  64. /*
  65. * Some platforms have custom ways of reseting the PCI bus.
  66. */
  67. void pci_resetbus(void)
  68. {
  69. #ifdef CONFIG_eLIA
  70. int i;
  71. #ifdef DEBUGPCI
  72. printk(KERN_DEBUG "pci_resetbus()\n");
  73. #endif
  74. *((volatile unsigned short *) (MCF_MBAR+MCFSIM_PADDR)) |= eLIA_PCIRESET;
  75. for (i = 0; (i < 1000); i++) {
  76. *((volatile unsigned short *) (MCF_MBAR + MCFSIM_PADAT)) =
  77. (ppdata | eLIA_PCIRESET);
  78. }
  79. *((volatile unsigned short *) (MCF_MBAR + MCFSIM_PADAT)) = ppdata;
  80. #endif
  81. }
  82. /*****************************************************************************/
  83. int pcibios_assign_resource_slot(int slot)
  84. {
  85. volatile unsigned long *rp;
  86. volatile unsigned char *ip;
  87. unsigned int idsel, addr, val, align, i;
  88. int bar;
  89. #ifdef DEBUGPCI
  90. printk(KERN_INFO "pcibios_assign_resource_slot(slot=%x)\n", slot);
  91. #endif
  92. rp = (volatile unsigned long *) COMEM_BASE;
  93. idsel = COMEM_DA_ADDR(0x1 << (slot + 16));
  94. /* Try to assign resource to each BAR */
  95. for (bar = 0; (bar < 6); bar++) {
  96. addr = COMEM_PCIBUS + PCI_BASE_ADDRESS_0 + (bar * 4);
  97. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
  98. val = rp[LREG(addr)];
  99. #ifdef DEBUGRES
  100. printk(KERN_DEBUG "-----------------------------------"
  101. "-------------------------------------\n");
  102. printk(KERN_DEBUG "BAR[%d]: read=%08x ", bar, val);
  103. #endif
  104. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
  105. rp[LREG(addr)] = 0xffffffff;
  106. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
  107. val = rp[LREG(addr)];
  108. #ifdef DEBUGRES
  109. printk(KERN_DEBUG "write=%08x ", val);
  110. #endif
  111. if (val == 0) {
  112. #ifdef DEBUGRES
  113. printk(KERN_DEBUG "\n");
  114. #endif
  115. continue;
  116. }
  117. /* Determine space required by BAR */
  118. /* FIXME: this should go backwords from 0x80000000... */
  119. for (i = 0; (i < 32); i++) {
  120. if ((0x1 << i) & (val & 0xfffffffc))
  121. break;
  122. }
  123. #ifdef DEBUGRES
  124. printk(KERN_DEBUG "size=%08x(%d)\n", (0x1 << i), i);
  125. #endif
  126. i = 0x1 << i;
  127. /* Assign a resource */
  128. if (val & PCI_BASE_ADDRESS_SPACE_IO) {
  129. if (i < PCI_MINIO)
  130. i = PCI_MINIO;
  131. #ifdef DEBUGRES
  132. printk(KERN_DEBUG "BAR[%d]: IO size=%08x iobase=%08x\n",
  133. bar, i, pci_iobase);
  134. #endif
  135. if (i > 0xffff) {
  136. /* Invalid size?? */
  137. val = 0 | PCI_BASE_ADDRESS_SPACE_IO;
  138. #ifdef DEBUGRES
  139. printk(KERN_DEBUG "BAR[%d]: too big for IO??\n", bar);
  140. #endif
  141. } else {
  142. /* Check for un-alignment */
  143. if ((align = pci_iobase % i))
  144. pci_iobase += (i - align);
  145. val = pci_iobase | PCI_BASE_ADDRESS_SPACE_IO;
  146. pci_iobase += i;
  147. }
  148. } else {
  149. if (i < PCI_MINMEM)
  150. i = PCI_MINMEM;
  151. #ifdef DEBUGRES
  152. printk(KERN_DEBUG "BAR[%d]: MEMORY size=%08x membase=%08x\n",
  153. bar, i, pci_membase);
  154. #endif
  155. /* Check for un-alignment */
  156. if ((align = pci_membase % i))
  157. pci_membase += (i - align);
  158. val = pci_membase | PCI_BASE_ADDRESS_SPACE_MEMORY;
  159. pci_membase += i;
  160. }
  161. /* Write resource back into BAR register */
  162. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
  163. rp[LREG(addr)] = val;
  164. #ifdef DEBUGRES
  165. printk(KERN_DEBUG "BAR[%d]: assigned bar=%08x\n", bar, val);
  166. #endif
  167. }
  168. #ifdef DEBUGRES
  169. printk(KERN_DEBUG "-----------------------------------"
  170. "-------------------------------------\n");
  171. #endif
  172. /* Assign IRQ if one is wanted... */
  173. ip = (volatile unsigned char *) (COMEM_BASE + COMEM_PCIBUS);
  174. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
  175. addr = (PCI_INTERRUPT_PIN & 0xfc) + (~PCI_INTERRUPT_PIN & 0x03);
  176. if (ip[addr]) {
  177. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
  178. addr = (PCI_INTERRUPT_LINE & 0xfc)+(~PCI_INTERRUPT_LINE & 0x03);
  179. ip[addr] = 25;
  180. #ifdef DEBUGRES
  181. printk(KERN_DEBUG "IRQ LINE=25\n");
  182. #endif
  183. }
  184. return(0);
  185. }
  186. /*****************************************************************************/
  187. int pcibios_enable_slot(int slot)
  188. {
  189. volatile unsigned long *rp;
  190. volatile unsigned short *wp;
  191. unsigned int idsel, addr;
  192. unsigned short cmd;
  193. #ifdef DEBUGPCI
  194. printk(KERN_DEBUG "pcibios_enbale_slot(slot=%x)\n", slot);
  195. #endif
  196. rp = (volatile unsigned long *) COMEM_BASE;
  197. wp = (volatile unsigned short *) COMEM_BASE;
  198. idsel = COMEM_DA_ADDR(0x1 << (slot + 16));
  199. /* Get current command settings */
  200. addr = COMEM_PCIBUS + PCI_COMMAND;
  201. addr = (addr & ~0x3) + (~addr & 0x02);
  202. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGRD | idsel;
  203. cmd = wp[WREG(addr)];
  204. /*val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);*/
  205. /* Enable I/O and memory accesses to this device */
  206. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_CFGWR | idsel;
  207. cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  208. wp[WREG(addr)] = cmd;
  209. return(0);
  210. }
  211. /*****************************************************************************/
  212. void pcibios_assign_resources(void)
  213. {
  214. volatile unsigned long *rp;
  215. unsigned long sel, id;
  216. int slot;
  217. rp = (volatile unsigned long *) COMEM_BASE;
  218. /*
  219. * Do a quick scan of the PCI bus and see what is here.
  220. */
  221. for (slot = COMEM_MINDEV; (slot <= COMEM_MAXDEV); slot++) {
  222. sel = COMEM_DA_CFGRD | COMEM_DA_ADDR(0x1 << (slot + 16));
  223. rp[LREG(COMEM_DAHBASE)] = sel;
  224. rp[LREG(COMEM_PCIBUS)] = 0; /* Clear bus */
  225. id = rp[LREG(COMEM_PCIBUS)];
  226. if ((id != 0) && ((id & 0xffff0000) != (sel & 0xffff0000))) {
  227. printk(KERN_INFO "PCI: slot=%d id=%08x\n", slot, (int) id);
  228. pci_slotmask |= 0x1 << slot;
  229. pcibios_assign_resource_slot(slot);
  230. pcibios_enable_slot(slot);
  231. }
  232. }
  233. }
  234. /*****************************************************************************/
  235. int pcibios_init(void)
  236. {
  237. volatile unsigned long *rp;
  238. unsigned long sel, id;
  239. int slot;
  240. #ifdef DEBUGPCI
  241. printk(KERN_DEBUG "pcibios_init()\n");
  242. #endif
  243. pci_resetbus();
  244. /*
  245. * Do some sort of basic check to see if the CO-MEM part
  246. * is present... This works ok, but I think we really need
  247. * something better...
  248. */
  249. rp = (volatile unsigned long *) COMEM_BASE;
  250. if ((rp[LREG(COMEM_LBUSCFG)] & 0xff) != 0x50) {
  251. printk(KERN_INFO "PCI: no PCI bus present\n");
  252. return(0);
  253. }
  254. #ifdef COMEM_BRIDGEDEV
  255. /*
  256. * Setup the PCI bridge device first. It needs resources too,
  257. * so that bus masters can get to its shared memory.
  258. */
  259. slot = COMEM_BRIDGEDEV;
  260. sel = COMEM_DA_CFGRD | COMEM_DA_ADDR(0x1 << (slot + 16));
  261. rp[LREG(COMEM_DAHBASE)] = sel;
  262. rp[LREG(COMEM_PCIBUS)] = 0; /* Clear bus */
  263. id = rp[LREG(COMEM_PCIBUS)];
  264. if ((id == 0) || ((id & 0xffff0000) == (sel & 0xffff0000))) {
  265. printk(KERN_INFO "PCI: no PCI bus bridge present\n");
  266. return(0);
  267. }
  268. printk(KERN_INFO "PCI: bridge device at slot=%d id=%08x\n", slot, (int) id);
  269. pci_slotmask |= 0x1 << slot;
  270. pci_shmemaddr = pci_membase;
  271. pcibios_assign_resource_slot(slot);
  272. pcibios_enable_slot(slot);
  273. #endif
  274. pci_bus_is_present = 1;
  275. /* Get PCI irq for local vectoring */
  276. if (request_irq(COMEM_IRQ, pci_interrupt, 0, "PCI bridge", NULL)) {
  277. printk(KERN_WARNING "PCI: failed to acquire interrupt %d\n", COMEM_IRQ);
  278. } else {
  279. mcf_autovector(COMEM_IRQ);
  280. }
  281. pcibios_assign_resources();
  282. return(0);
  283. }
  284. /*****************************************************************************/
  285. char *pcibios_setup(char *option)
  286. {
  287. /* Nothing for us to handle. */
  288. return(option);
  289. }
  290. /*****************************************************************************/
  291. void pcibios_fixup_bus(struct pci_bus *b)
  292. {
  293. }
  294. /*****************************************************************************/
  295. void pcibios_align_resource(void *data, struct resource *res,
  296. resource_size_t size, resource_size_t align)
  297. {
  298. }
  299. /*****************************************************************************/
  300. int pcibios_enable_device(struct pci_dev *dev, int mask)
  301. {
  302. int slot;
  303. slot = PCI_SLOT(dev->devfn);
  304. if ((dev->bus == 0) && (pci_slotmask & (1 << slot)))
  305. pcibios_enable_slot(slot);
  306. return(0);
  307. }
  308. /*****************************************************************************/
  309. void pcibios_update_resource(struct pci_dev *dev, struct resource *root, struct resource *r, int resource)
  310. {
  311. printk(KERN_WARNING "%s(%d): no support for changing PCI resources...\n",
  312. __FILE__, __LINE__);
  313. }
  314. /*****************************************************************************/
  315. /*
  316. * Local routines to interrcept the standard I/O and vector handling
  317. * code. Don't include this 'till now - initialization code above needs
  318. * access to the real code too.
  319. */
  320. #include <asm/mcfpci.h>
  321. /*****************************************************************************/
  322. void pci_outb(unsigned char val, unsigned int addr)
  323. {
  324. volatile unsigned long *rp;
  325. volatile unsigned char *bp;
  326. #ifdef DEBUGIO
  327. printk(KERN_DEBUG "pci_outb(val=%02x,addr=%x)\n", val, addr);
  328. #endif
  329. rp = (volatile unsigned long *) COMEM_BASE;
  330. bp = (volatile unsigned char *) COMEM_BASE;
  331. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
  332. addr = (addr & ~0x3) + (~addr & 0x03);
  333. bp[(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
  334. }
  335. /*****************************************************************************/
  336. void pci_outw(unsigned short val, unsigned int addr)
  337. {
  338. volatile unsigned long *rp;
  339. volatile unsigned short *sp;
  340. #ifdef DEBUGIO
  341. printk(KERN_DEBUG "pci_outw(val=%04x,addr=%x)\n", val, addr);
  342. #endif
  343. rp = (volatile unsigned long *) COMEM_BASE;
  344. sp = (volatile unsigned short *) COMEM_BASE;
  345. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
  346. addr = (addr & ~0x3) + (~addr & 0x02);
  347. if (pci_byteswap)
  348. val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);
  349. sp[WREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
  350. }
  351. /*****************************************************************************/
  352. void pci_outl(unsigned int val, unsigned int addr)
  353. {
  354. volatile unsigned long *rp;
  355. volatile unsigned int *lp;
  356. #ifdef DEBUGIO
  357. printk(KERN_DEBUG "pci_outl(val=%08x,addr=%x)\n", val, addr);
  358. #endif
  359. rp = (volatile unsigned long *) COMEM_BASE;
  360. lp = (volatile unsigned int *) COMEM_BASE;
  361. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(addr);
  362. if (pci_byteswap)
  363. val = (val << 24) | ((val & 0x0000ff00) << 8) |
  364. ((val & 0x00ff0000) >> 8) | (val >> 24);
  365. lp[LREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))] = val;
  366. }
  367. /*****************************************************************************/
  368. unsigned long pci_blmask[] = {
  369. 0x000000e0,
  370. 0x000000d0,
  371. 0x000000b0,
  372. 0x00000070
  373. };
  374. unsigned char pci_inb(unsigned int addr)
  375. {
  376. volatile unsigned long *rp;
  377. volatile unsigned char *bp;
  378. unsigned long r;
  379. unsigned char val;
  380. #ifdef DEBUGIO
  381. printk(KERN_DEBUG "pci_inb(addr=%x)\n", addr);
  382. #endif
  383. rp = (volatile unsigned long *) COMEM_BASE;
  384. bp = (volatile unsigned char *) COMEM_BASE;
  385. r = COMEM_DA_IORD | COMEM_DA_ADDR(addr) | pci_blmask[(addr & 0x3)];
  386. rp[LREG(COMEM_DAHBASE)] = r;
  387. addr = (addr & ~0x3) + (~addr & 0x3);
  388. val = bp[(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
  389. return(val);
  390. }
  391. /*****************************************************************************/
  392. unsigned long pci_bwmask[] = {
  393. 0x000000c0,
  394. 0x000000c0,
  395. 0x00000030,
  396. 0x00000030
  397. };
  398. unsigned short pci_inw(unsigned int addr)
  399. {
  400. volatile unsigned long *rp;
  401. volatile unsigned short *sp;
  402. unsigned long r;
  403. unsigned short val;
  404. #ifdef DEBUGIO
  405. printk(KERN_DEBUG "pci_inw(addr=%x)", addr);
  406. #endif
  407. rp = (volatile unsigned long *) COMEM_BASE;
  408. r = COMEM_DA_IORD | COMEM_DA_ADDR(addr) | pci_bwmask[(addr & 0x3)];
  409. rp[LREG(COMEM_DAHBASE)] = r;
  410. sp = (volatile unsigned short *) COMEM_BASE;
  411. addr = (addr & ~0x3) + (~addr & 0x02);
  412. val = sp[WREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
  413. if (pci_byteswap)
  414. val = ((val & 0xff) << 8) | ((val >> 8) & 0xff);
  415. #ifdef DEBUGIO
  416. printk(KERN_DEBUG "=%04x\n", val);
  417. #endif
  418. return(val);
  419. }
  420. /*****************************************************************************/
  421. unsigned int pci_inl(unsigned int addr)
  422. {
  423. volatile unsigned long *rp;
  424. volatile unsigned int *lp;
  425. unsigned int val;
  426. #ifdef DEBUGIO
  427. printk(KERN_DEBUG "pci_inl(addr=%x)", addr);
  428. #endif
  429. rp = (volatile unsigned long *) COMEM_BASE;
  430. lp = (volatile unsigned int *) COMEM_BASE;
  431. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(addr);
  432. val = lp[LREG(COMEM_PCIBUS + COMEM_DA_OFFSET(addr))];
  433. if (pci_byteswap)
  434. val = (val << 24) | ((val & 0x0000ff00) << 8) |
  435. ((val & 0x00ff0000) >> 8) | (val >> 24);
  436. #ifdef DEBUGIO
  437. printk(KERN_DEBUG "=%08x\n", val);
  438. #endif
  439. return(val);
  440. }
  441. /*****************************************************************************/
  442. void pci_outsb(void *addr, void *buf, int len)
  443. {
  444. volatile unsigned long *rp;
  445. volatile unsigned char *bp;
  446. unsigned char *dp = (unsigned char *) buf;
  447. unsigned int a = (unsigned int) addr;
  448. #ifdef DEBUGIO
  449. printk(KERN_DEBUG "pci_outsb(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
  450. #endif
  451. rp = (volatile unsigned long *) COMEM_BASE;
  452. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
  453. a = (a & ~0x3) + (~a & 0x03);
  454. bp = (volatile unsigned char *)
  455. (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
  456. while (len--)
  457. *bp = *dp++;
  458. }
  459. /*****************************************************************************/
  460. void pci_outsw(void *addr, void *buf, int len)
  461. {
  462. volatile unsigned long *rp;
  463. volatile unsigned short *wp;
  464. unsigned short w, *dp = (unsigned short *) buf;
  465. unsigned int a = (unsigned int) addr;
  466. #ifdef DEBUGIO
  467. printk(KERN_DEBUG "pci_outsw(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
  468. #endif
  469. rp = (volatile unsigned long *) COMEM_BASE;
  470. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
  471. a = (a & ~0x3) + (~a & 0x2);
  472. wp = (volatile unsigned short *)
  473. (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
  474. while (len--) {
  475. w = *dp++;
  476. if (pci_byteswap)
  477. w = ((w & 0xff) << 8) | ((w >> 8) & 0xff);
  478. *wp = w;
  479. }
  480. }
  481. /*****************************************************************************/
  482. void pci_outsl(void *addr, void *buf, int len)
  483. {
  484. volatile unsigned long *rp;
  485. volatile unsigned long *lp;
  486. unsigned long l, *dp = (unsigned long *) buf;
  487. unsigned int a = (unsigned int) addr;
  488. #ifdef DEBUGIO
  489. printk(KERN_DEBUG "pci_outsl(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
  490. #endif
  491. rp = (volatile unsigned long *) COMEM_BASE;
  492. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IOWR | COMEM_DA_ADDR(a);
  493. lp = (volatile unsigned long *)
  494. (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
  495. while (len--) {
  496. l = *dp++;
  497. if (pci_byteswap)
  498. l = (l << 24) | ((l & 0x0000ff00) << 8) |
  499. ((l & 0x00ff0000) >> 8) | (l >> 24);
  500. *lp = l;
  501. }
  502. }
  503. /*****************************************************************************/
  504. void pci_insb(void *addr, void *buf, int len)
  505. {
  506. volatile unsigned long *rp;
  507. volatile unsigned char *bp;
  508. unsigned char *dp = (unsigned char *) buf;
  509. unsigned int a = (unsigned int) addr;
  510. #ifdef DEBUGIO
  511. printk(KERN_DEBUG "pci_insb(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
  512. #endif
  513. rp = (volatile unsigned long *) COMEM_BASE;
  514. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
  515. a = (a & ~0x3) + (~a & 0x03);
  516. bp = (volatile unsigned char *)
  517. (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
  518. while (len--)
  519. *dp++ = *bp;
  520. }
  521. /*****************************************************************************/
  522. void pci_insw(void *addr, void *buf, int len)
  523. {
  524. volatile unsigned long *rp;
  525. volatile unsigned short *wp;
  526. unsigned short w, *dp = (unsigned short *) buf;
  527. unsigned int a = (unsigned int) addr;
  528. #ifdef DEBUGIO
  529. printk(KERN_DEBUG "pci_insw(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
  530. #endif
  531. rp = (volatile unsigned long *) COMEM_BASE;
  532. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
  533. a = (a & ~0x3) + (~a & 0x2);
  534. wp = (volatile unsigned short *)
  535. (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
  536. while (len--) {
  537. w = *wp;
  538. if (pci_byteswap)
  539. w = ((w & 0xff) << 8) | ((w >> 8) & 0xff);
  540. *dp++ = w;
  541. }
  542. }
  543. /*****************************************************************************/
  544. void pci_insl(void *addr, void *buf, int len)
  545. {
  546. volatile unsigned long *rp;
  547. volatile unsigned long *lp;
  548. unsigned long l, *dp = (unsigned long *) buf;
  549. unsigned int a = (unsigned int) addr;
  550. #ifdef DEBUGIO
  551. printk(KERN_DEBUG "pci_insl(addr=%x,buf=%x,len=%d)\n", (int)addr, (int)buf, len);
  552. #endif
  553. rp = (volatile unsigned long *) COMEM_BASE;
  554. rp[LREG(COMEM_DAHBASE)] = COMEM_DA_IORD | COMEM_DA_ADDR(a);
  555. lp = (volatile unsigned long *)
  556. (COMEM_BASE + COMEM_PCIBUS + COMEM_DA_OFFSET(a));
  557. while (len--) {
  558. l = *lp;
  559. if (pci_byteswap)
  560. l = (l << 24) | ((l & 0x0000ff00) << 8) |
  561. ((l & 0x00ff0000) >> 8) | (l >> 24);
  562. *dp++ = l;
  563. }
  564. }
  565. /*****************************************************************************/
  566. struct pci_localirqlist {
  567. void (*handler)(int, void *, struct pt_regs *);
  568. const char *device;
  569. void *dev_id;
  570. };
  571. struct pci_localirqlist pci_irqlist[COMEM_MAXPCI];
  572. /*****************************************************************************/
  573. int pci_request_irq(unsigned int irq,
  574. void (*handler)(int, void *, struct pt_regs *),
  575. unsigned long flags, const char *device, void *dev_id)
  576. {
  577. int i;
  578. #ifdef DEBUGIO
  579. printk(KERN_DEBUG "pci_request_irq(irq=%d,handler=%x,flags=%x,device=%s,"
  580. "dev_id=%x)\n", irq, (int) handler, (int) flags, device,
  581. (int) dev_id);
  582. #endif
  583. /* Check if this interrupt handler is already lodged */
  584. for (i = 0; (i < COMEM_MAXPCI); i++) {
  585. if (pci_irqlist[i].handler == handler)
  586. return(0);
  587. }
  588. /* Find a free spot to put this handler */
  589. for (i = 0; (i < COMEM_MAXPCI); i++) {
  590. if (pci_irqlist[i].handler == 0) {
  591. pci_irqlist[i].handler = handler;
  592. pci_irqlist[i].device = device;
  593. pci_irqlist[i].dev_id = dev_id;
  594. return(0);
  595. }
  596. }
  597. /* Couldn't fit?? */
  598. return(1);
  599. }
  600. /*****************************************************************************/
  601. void pci_free_irq(unsigned int irq, void *dev_id)
  602. {
  603. int i;
  604. #ifdef DEBUGIO
  605. printk(KERN_DEBUG "pci_free_irq(irq=%d,dev_id=%x)\n", irq, (int) dev_id);
  606. #endif
  607. if (dev_id == (void *) NULL)
  608. return;
  609. /* Check if this interrupt handler is lodged */
  610. for (i = 0; (i < COMEM_MAXPCI); i++) {
  611. if (pci_irqlist[i].dev_id == dev_id) {
  612. pci_irqlist[i].handler = NULL;
  613. pci_irqlist[i].device = NULL;
  614. pci_irqlist[i].dev_id = NULL;
  615. break;
  616. }
  617. }
  618. }
  619. /*****************************************************************************/
  620. void pci_interrupt(int irq, void *id, struct pt_regs *fp)
  621. {
  622. int i;
  623. #ifdef DEBUGIO
  624. printk(KERN_DEBUG "pci_interrupt(irq=%d,id=%x,fp=%x)\n", irq, (int) id, (int) fp);
  625. #endif
  626. for (i = 0; (i < COMEM_MAXPCI); i++) {
  627. if (pci_irqlist[i].handler)
  628. (*pci_irqlist[i].handler)(irq,pci_irqlist[i].dev_id,fp);
  629. }
  630. }
  631. /*****************************************************************************/
  632. /*
  633. * The shared memory region is broken up into contiguous 512 byte
  634. * regions for easy allocation... This is not an optimal solution
  635. * but it makes allocation and freeing regions really easy.
  636. */
  637. #define PCI_MEMSLOTSIZE 512
  638. #define PCI_MEMSLOTS (COMEM_SHMEMSIZE / PCI_MEMSLOTSIZE)
  639. char pci_shmemmap[PCI_MEMSLOTS];
  640. void *pci_bmalloc(int size)
  641. {
  642. int i, j, nrslots;
  643. #ifdef DEBUGIO
  644. printk(KERN_DEBUG "pci_bmalloc(size=%d)\n", size);
  645. #endif
  646. if (size <= 0)
  647. return((void *) NULL);
  648. nrslots = (size - 1) / PCI_MEMSLOTSIZE;
  649. for (i = 0; (i < (PCI_MEMSLOTS-nrslots)); i++) {
  650. if (pci_shmemmap[i] == 0) {
  651. for (j = i+1; (j < (i+nrslots)); j++) {
  652. if (pci_shmemmap[j])
  653. goto restart;
  654. }
  655. for (j = i; (j <= i+nrslots); j++)
  656. pci_shmemmap[j] = 1;
  657. break;
  658. }
  659. restart:
  660. }
  661. return((void *) (COMEM_BASE + COMEM_SHMEM + (i * PCI_MEMSLOTSIZE)));
  662. }
  663. /*****************************************************************************/
  664. void pci_bmfree(void *mp, int size)
  665. {
  666. int i, j, nrslots;
  667. #ifdef DEBUGIO
  668. printk(KERN_DEBUG "pci_bmfree(mp=%x,size=%d)\n", (int) mp, size);
  669. #endif
  670. nrslots = size / PCI_MEMSLOTSIZE;
  671. i = (((unsigned long) mp) - (COMEM_BASE + COMEM_SHMEM)) /
  672. PCI_MEMSLOTSIZE;
  673. for (j = i; (j < (i+nrslots)); j++)
  674. pci_shmemmap[j] = 0;
  675. }
  676. /*****************************************************************************/
  677. unsigned long pci_virt_to_bus(volatile void *address)
  678. {
  679. unsigned long l;
  680. #ifdef DEBUGIO
  681. printk(KERN_DEBUG "pci_virt_to_bus(address=%x)", (int) address);
  682. #endif
  683. l = ((unsigned long) address) - COMEM_BASE;
  684. #ifdef DEBUGIO
  685. printk(KERN_DEBUG "=%x\n", (int) (l+pci_shmemaddr));
  686. #endif
  687. return(l + pci_shmemaddr);
  688. }
  689. /*****************************************************************************/
  690. void *pci_bus_to_virt(unsigned long address)
  691. {
  692. unsigned long l;
  693. #ifdef DEBUGIO
  694. printk(KERN_DEBUG "pci_bus_to_virt(address=%x)", (int) address);
  695. #endif
  696. l = address - pci_shmemaddr;
  697. #ifdef DEBUGIO
  698. printk(KERN_DEBUG "=%x\n", (int) (address + COMEM_BASE));
  699. #endif
  700. return((void *) (address + COMEM_BASE));
  701. }
  702. /*****************************************************************************/
  703. void pci_bmcpyto(void *dst, void *src, int len)
  704. {
  705. unsigned long *dp, *sp, val;
  706. unsigned char *dcp, *scp;
  707. int i, j;
  708. #ifdef DEBUGIO
  709. printk(KERN_DEBUG "pci_bmcpyto(dst=%x,src=%x,len=%d)\n", (int)dst, (int)src, len);
  710. #endif
  711. dp = (unsigned long *) dst;
  712. sp = (unsigned long *) src;
  713. i = len >> 2;
  714. #if 0
  715. printk(KERN_INFO "DATA:");
  716. scp = (unsigned char *) sp;
  717. for (i = 0; (i < len); i++) {
  718. if ((i % 16) == 0) printk(KERN_INFO "\n%04x: ", i);
  719. printk(KERN_INFO "%02x ", *scp++);
  720. }
  721. printk(KERN_INFO "\n");
  722. #endif
  723. for (j = 0; (i >= 0); i--, j++) {
  724. val = *sp++;
  725. val = (val << 24) | ((val & 0x0000ff00) << 8) |
  726. ((val & 0x00ff0000) >> 8) | (val >> 24);
  727. *dp++ = val;
  728. }
  729. if (len & 0x3) {
  730. dcp = (unsigned char *) dp;
  731. scp = ((unsigned char *) sp) + 3;
  732. for (i = 0; (i < (len & 0x3)); i++)
  733. *dcp++ = *scp--;
  734. }
  735. }
  736. /*****************************************************************************/
  737. void pci_bmcpyfrom(void *dst, void *src, int len)
  738. {
  739. unsigned long *dp, *sp, val;
  740. unsigned char *dcp, *scp;
  741. int i;
  742. #ifdef DEBUGIO
  743. printk(KERN_DEBUG "pci_bmcpyfrom(dst=%x,src=%x,len=%d)\n",(int)dst,(int)src,len);
  744. #endif
  745. dp = (unsigned long *) dst;
  746. sp = (unsigned long *) src;
  747. i = len >> 2;
  748. for (; (i >= 0); i--) {
  749. val = *sp++;
  750. val = (val << 24) | ((val & 0x0000ff00) << 8) |
  751. ((val & 0x00ff0000) >> 8) | (val >> 24);
  752. *dp++ = val;
  753. }
  754. if (len & 0x3) {
  755. dcp = ((unsigned char *) dp) + 3;
  756. scp = (unsigned char *) sp;
  757. for (i = 0; (i < (len & 0x3)); i++)
  758. *dcp++ = *scp--;
  759. }
  760. #if 0
  761. printk(KERN_INFO "DATA:");
  762. dcp = (unsigned char *) dst;
  763. for (i = 0; (i < len); i++) {
  764. if ((i % 16) == 0) printk(KERN_INFO "\n%04x: ", i);
  765. printk(KERN_INFO "%02x ", *dcp++);
  766. }
  767. printk(KERN_INFO "\n");
  768. #endif
  769. }
  770. /*****************************************************************************/
  771. void *pci_alloc_consistent(struct pci_dev *dev, size_t size, dma_addr_t *dma_addr)
  772. {
  773. void *mp;
  774. if ((mp = pci_bmalloc(size)) != NULL) {
  775. dma_addr = mp - (COMEM_BASE + COMEM_SHMEM);
  776. return(mp);
  777. }
  778. *dma_addr = (dma_addr_t) NULL;
  779. return(NULL);
  780. }
  781. /*****************************************************************************/
  782. void pci_free_consistent(struct pci_dev *dev, size_t size, void *cpu_addr, dma_addr_t dma_addr)
  783. {
  784. pci_bmfree(cpu_addr, size);
  785. }
  786. /*****************************************************************************/