pci.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * arch/xtensa/pcibios.c
  3. *
  4. * PCI bios-type initialisation for PCI machines
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Copyright (C) 2001-2005 Tensilica Inc.
  12. *
  13. * Based largely on work from Cort (ppc/kernel/pci.c)
  14. * IO functions copied from sparc.
  15. *
  16. * Chris Zankel <chris@zankel.net>
  17. *
  18. */
  19. #include <linux/config.h>
  20. #include <linux/kernel.h>
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/string.h>
  24. #include <linux/init.h>
  25. #include <linux/sched.h>
  26. #include <linux/errno.h>
  27. #include <linux/bootmem.h>
  28. #include <asm/pci-bridge.h>
  29. #include <asm/platform.h>
  30. #undef DEBUG
  31. #ifdef DEBUG
  32. #define DBG(x...) printk(x)
  33. #else
  34. #define DBG(x...)
  35. #endif
  36. /* PCI Controller */
  37. /*
  38. * pcibios_alloc_controller
  39. * pcibios_enable_device
  40. * pcibios_fixups
  41. * pcibios_align_resource
  42. * pcibios_fixup_bus
  43. * pcibios_setup
  44. * pci_bus_add_device
  45. * pci_mmap_page_range
  46. */
  47. struct pci_controller* pci_ctrl_head;
  48. struct pci_controller** pci_ctrl_tail = &pci_ctrl_head;
  49. static int pci_bus_count;
  50. static void pcibios_fixup_resources(struct pci_dev* dev);
  51. #if 0 // FIXME
  52. struct pci_fixup pcibios_fixups[] = {
  53. { DECLARE_PCI_FIXUP_HEADER, PCI_ANY_ID, PCI_ANY_ID, pcibios_fixup_resources },
  54. { 0 }
  55. };
  56. #endif
  57. void
  58. pcibios_update_resource(struct pci_dev *dev, struct resource *root,
  59. struct resource *res, int resource)
  60. {
  61. u32 new, check, mask;
  62. int reg;
  63. struct pci_controller* pci_ctrl = dev->sysdata;
  64. new = res->start;
  65. if (pci_ctrl && res->flags & IORESOURCE_IO) {
  66. new -= pci_ctrl->io_space.base;
  67. }
  68. new |= (res->flags & PCI_REGION_FLAG_MASK);
  69. if (resource < 6) {
  70. reg = PCI_BASE_ADDRESS_0 + 4*resource;
  71. } else if (resource == PCI_ROM_RESOURCE) {
  72. res->flags |= PCI_ROM_ADDRESS_ENABLE;
  73. reg = dev->rom_base_reg;
  74. } else {
  75. /* Somebody might have asked allocation of a non-standard resource */
  76. return;
  77. }
  78. pci_write_config_dword(dev, reg, new);
  79. pci_read_config_dword(dev, reg, &check);
  80. mask = (new & PCI_BASE_ADDRESS_SPACE_IO) ?
  81. PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK;
  82. if ((new ^ check) & mask) {
  83. printk(KERN_ERR "PCI: Error while updating region "
  84. "%s/%d (%08x != %08x)\n", dev->slot_name, resource,
  85. new, check);
  86. }
  87. }
  88. /*
  89. * We need to avoid collisions with `mirrored' VGA ports
  90. * and other strange ISA hardware, so we always want the
  91. * addresses to be allocated in the 0x000-0x0ff region
  92. * modulo 0x400.
  93. *
  94. * Why? Because some silly external IO cards only decode
  95. * the low 10 bits of the IO address. The 0x00-0xff region
  96. * is reserved for motherboard devices that decode all 16
  97. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  98. * but we want to try to avoid allocating at 0x2900-0x2bff
  99. * which might have be mirrored at 0x0100-0x03ff..
  100. */
  101. void
  102. pcibios_align_resource(void *data, struct resource *res, unsigned long size,
  103. unsigned long align)
  104. {
  105. struct pci_dev *dev = data;
  106. if (res->flags & IORESOURCE_IO) {
  107. unsigned long start = res->start;
  108. if (size > 0x100) {
  109. printk(KERN_ERR "PCI: I/O Region %s/%d too large"
  110. " (%ld bytes)\n", dev->slot_name,
  111. dev->resource - res, size);
  112. }
  113. if (start & 0x300) {
  114. start = (start + 0x3ff) & ~0x3ff;
  115. res->start = start;
  116. }
  117. }
  118. }
  119. int
  120. pcibios_enable_resources(struct pci_dev *dev, int mask)
  121. {
  122. u16 cmd, old_cmd;
  123. int idx;
  124. struct resource *r;
  125. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  126. old_cmd = cmd;
  127. for(idx=0; idx<6; idx++) {
  128. r = &dev->resource[idx];
  129. if (!r->start && r->end) {
  130. printk (KERN_ERR "PCI: Device %s not available because "
  131. "of resource collisions\n", dev->slot_name);
  132. return -EINVAL;
  133. }
  134. if (r->flags & IORESOURCE_IO)
  135. cmd |= PCI_COMMAND_IO;
  136. if (r->flags & IORESOURCE_MEM)
  137. cmd |= PCI_COMMAND_MEMORY;
  138. }
  139. if (dev->resource[PCI_ROM_RESOURCE].start)
  140. cmd |= PCI_COMMAND_MEMORY;
  141. if (cmd != old_cmd) {
  142. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  143. dev->slot_name, old_cmd, cmd);
  144. pci_write_config_word(dev, PCI_COMMAND, cmd);
  145. }
  146. return 0;
  147. }
  148. struct pci_controller * __init pcibios_alloc_controller(void)
  149. {
  150. struct pci_controller *pci_ctrl;
  151. pci_ctrl = (struct pci_controller *)alloc_bootmem(sizeof(*pci_ctrl));
  152. memset(pci_ctrl, 0, sizeof(struct pci_controller));
  153. *pci_ctrl_tail = pci_ctrl;
  154. pci_ctrl_tail = &pci_ctrl->next;
  155. return pci_ctrl;
  156. }
  157. static int __init pcibios_init(void)
  158. {
  159. struct pci_controller *pci_ctrl;
  160. struct pci_bus *bus;
  161. int next_busno = 0, i;
  162. printk("PCI: Probing PCI hardware\n");
  163. /* Scan all of the recorded PCI controllers. */
  164. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  165. pci_ctrl->last_busno = 0xff;
  166. bus = pci_scan_bus(pci_ctrl->first_busno, pci_ctrl->ops,
  167. pci_ctrl);
  168. if (pci_ctrl->io_resource.flags) {
  169. unsigned long offs;
  170. offs = (unsigned long)pci_ctrl->io_space.base;
  171. pci_ctrl->io_resource.start += offs;
  172. pci_ctrl->io_resource.end += offs;
  173. bus->resource[0] = &pci_ctrl->io_resource;
  174. }
  175. for (i = 0; i < 3; ++i)
  176. if (pci_ctrl->mem_resources[i].flags)
  177. bus->resource[i+1] =&pci_ctrl->mem_resources[i];
  178. pci_ctrl->bus = bus;
  179. pci_ctrl->last_busno = bus->subordinate;
  180. if (next_busno <= pci_ctrl->last_busno)
  181. next_busno = pci_ctrl->last_busno+1;
  182. }
  183. pci_bus_count = next_busno;
  184. return platform_pcibios_fixup();
  185. }
  186. subsys_initcall(pcibios_init);
  187. void __init pcibios_fixup_bus(struct pci_bus *bus)
  188. {
  189. struct pci_controller *pci_ctrl = bus->sysdata;
  190. struct resource *res;
  191. unsigned long io_offset;
  192. int i;
  193. io_offset = (unsigned long)pci_ctrl->io_space.base;
  194. if (bus->parent == NULL) {
  195. /* this is a host bridge - fill in its resources */
  196. pci_ctrl->bus = bus;
  197. bus->resource[0] = res = &pci_ctrl->io_resource;
  198. if (!res->flags) {
  199. if (io_offset)
  200. printk (KERN_ERR "I/O resource not set for host"
  201. " bridge %d\n", pci_ctrl->index);
  202. res->start = 0;
  203. res->end = IO_SPACE_LIMIT;
  204. res->flags = IORESOURCE_IO;
  205. }
  206. res->start += io_offset;
  207. res->end += io_offset;
  208. for (i = 0; i < 3; i++) {
  209. res = &pci_ctrl->mem_resources[i];
  210. if (!res->flags) {
  211. if (i > 0)
  212. continue;
  213. printk(KERN_ERR "Memory resource not set for "
  214. "host bridge %d\n", pci_ctrl->index);
  215. res->start = 0;
  216. res->end = ~0U;
  217. res->flags = IORESOURCE_MEM;
  218. }
  219. bus->resource[i+1] = res;
  220. }
  221. } else {
  222. /* This is a subordinate bridge */
  223. pci_read_bridge_bases(bus);
  224. for (i = 0; i < 4; i++) {
  225. if ((res = bus->resource[i]) == NULL || !res->flags)
  226. continue;
  227. if (io_offset && (res->flags & IORESOURCE_IO)) {
  228. res->start += io_offset;
  229. res->end += io_offset;
  230. }
  231. }
  232. }
  233. }
  234. char __init *pcibios_setup(char *str)
  235. {
  236. return str;
  237. }
  238. /* the next one is stolen from the alpha port... */
  239. void __init
  240. pcibios_update_irq(struct pci_dev *dev, int irq)
  241. {
  242. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  243. }
  244. int pcibios_enable_device(struct pci_dev *dev, int mask)
  245. {
  246. u16 cmd, old_cmd;
  247. int idx;
  248. struct resource *r;
  249. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  250. old_cmd = cmd;
  251. for (idx=0; idx<6; idx++) {
  252. r = &dev->resource[idx];
  253. if (!r->start && r->end) {
  254. printk(KERN_ERR "PCI: Device %s not available because "
  255. "of resource collisions\n", dev->slot_name);
  256. return -EINVAL;
  257. }
  258. if (r->flags & IORESOURCE_IO)
  259. cmd |= PCI_COMMAND_IO;
  260. if (r->flags & IORESOURCE_MEM)
  261. cmd |= PCI_COMMAND_MEMORY;
  262. }
  263. if (cmd != old_cmd) {
  264. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  265. dev->slot_name, old_cmd, cmd);
  266. pci_write_config_word(dev, PCI_COMMAND, cmd);
  267. }
  268. return 0;
  269. }
  270. #ifdef CONFIG_PROC_FS
  271. /*
  272. * Return the index of the PCI controller for device pdev.
  273. */
  274. int
  275. pci_controller_num(struct pci_dev *dev)
  276. {
  277. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  278. return pci_ctrl->index;
  279. }
  280. #endif /* CONFIG_PROC_FS */
  281. static void
  282. pcibios_fixup_resources(struct pci_dev *dev)
  283. {
  284. struct pci_controller* pci_ctrl = (struct pci_controller *)dev->sysdata;
  285. int i;
  286. unsigned long offset;
  287. if (!pci_ctrl) {
  288. printk(KERN_ERR "No pci_ctrl for PCI dev %s!\n",dev->slot_name);
  289. return;
  290. }
  291. for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
  292. struct resource *res = dev->resource + i;
  293. if (!res->start || !res->flags)
  294. continue;
  295. if (res->end == 0xffffffff) {
  296. DBG("PCI:%s Resource %d [%08lx-%08lx] is unassigned\n",
  297. dev->slot_name, i, res->start, res->end);
  298. res->end -= res->start;
  299. res->start = 0;
  300. continue;
  301. }
  302. offset = 0;
  303. if (res->flags & IORESOURCE_IO)
  304. offset = (unsigned long) pci_ctrl->io_space.base;
  305. else if (res->flags & IORESOURCE_MEM)
  306. offset = (unsigned long) pci_ctrl->mem_space.base;
  307. if (offset != 0) {
  308. res->start += offset;
  309. res->end += offset;
  310. #ifdef DEBUG
  311. printk("Fixup res %d (%lx) of dev %s: %lx -> %lx\n",
  312. i, res->flags, dev->slot_name,
  313. res->start - offset, res->start);
  314. #endif
  315. }
  316. }
  317. }
  318. /*
  319. * Platform support for /proc/bus/pci/X/Y mmap()s,
  320. * modelled on the sparc64 implementation by Dave Miller.
  321. * -- paulus.
  322. */
  323. /*
  324. * Adjust vm_pgoff of VMA such that it is the physical page offset
  325. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  326. *
  327. * Basically, the user finds the base address for his device which he wishes
  328. * to mmap. They read the 32-bit value from the config space base register,
  329. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  330. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  331. *
  332. * Returns negative error code on failure, zero on success.
  333. */
  334. static __inline__ int
  335. __pci_mmap_make_offset(struct pci_dev *dev, struct vm_area_struct *vma,
  336. enum pci_mmap_state mmap_state)
  337. {
  338. struct pci_controller *pci_ctrl = (struct pci_controller*) dev->sysdata;
  339. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  340. unsigned long io_offset = 0;
  341. int i, res_bit;
  342. if (pci_ctrl == 0)
  343. return -EINVAL; /* should never happen */
  344. /* If memory, add on the PCI bridge address offset */
  345. if (mmap_state == pci_mmap_mem) {
  346. res_bit = IORESOURCE_MEM;
  347. } else {
  348. io_offset = (unsigned long)pci_ctrl->io_space.base;
  349. offset += io_offset;
  350. res_bit = IORESOURCE_IO;
  351. }
  352. /*
  353. * Check that the offset requested corresponds to one of the
  354. * resources of the device.
  355. */
  356. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  357. struct resource *rp = &dev->resource[i];
  358. int flags = rp->flags;
  359. /* treat ROM as memory (should be already) */
  360. if (i == PCI_ROM_RESOURCE)
  361. flags |= IORESOURCE_MEM;
  362. /* Active and same type? */
  363. if ((flags & res_bit) == 0)
  364. continue;
  365. /* In the range of this resource? */
  366. if (offset < (rp->start & PAGE_MASK) || offset > rp->end)
  367. continue;
  368. /* found it! construct the final physical address */
  369. if (mmap_state == pci_mmap_io)
  370. offset += pci_ctrl->io_space.start - io_offset;
  371. vma->vm_pgoff = offset >> PAGE_SHIFT;
  372. return 0;
  373. }
  374. return -EINVAL;
  375. }
  376. /*
  377. * Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  378. * mapping.
  379. */
  380. static __inline__ void
  381. __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  382. enum pci_mmap_state mmap_state)
  383. {
  384. vma->vm_flags |= VM_SHM | VM_LOCKED | VM_IO;
  385. }
  386. /*
  387. * Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  388. * device mapping.
  389. */
  390. static __inline__ void
  391. __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  392. enum pci_mmap_state mmap_state, int write_combine)
  393. {
  394. int prot = pgprot_val(vma->vm_page_prot);
  395. /* Set to write-through */
  396. prot &= ~_PAGE_NO_CACHE;
  397. #if 0
  398. if (!write_combine)
  399. prot |= _PAGE_WRITETHRU;
  400. #endif
  401. vma->vm_page_prot = __pgprot(prot);
  402. }
  403. /*
  404. * Perform the actual remap of the pages for a PCI device mapping, as
  405. * appropriate for this architecture. The region in the process to map
  406. * is described by vm_start and vm_end members of VMA, the base physical
  407. * address is found in vm_pgoff.
  408. * The pci device structure is provided so that architectures may make mapping
  409. * decisions on a per-device or per-bus basis.
  410. *
  411. * Returns a negative error code on failure, zero on success.
  412. */
  413. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  414. enum pci_mmap_state mmap_state,
  415. int write_combine)
  416. {
  417. int ret;
  418. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  419. if (ret < 0)
  420. return ret;
  421. __pci_mmap_set_flags(dev, vma, mmap_state);
  422. __pci_mmap_set_pgprot(dev, vma, mmap_state, write_combine);
  423. ret = io_remap_page_range(vma, vma->vm_start, vma->vm_pgoff<<PAGE_SHIFT,
  424. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  425. return ret;
  426. }
  427. /*
  428. * This probably belongs here rather than ioport.c because
  429. * we do not want this crud linked into SBus kernels.
  430. * Also, think for a moment about likes of floppy.c that
  431. * include architecture specific parts. They may want to redefine ins/outs.
  432. *
  433. * We do not use horroble macroses here because we want to
  434. * advance pointer by sizeof(size).
  435. */
  436. void outsb(unsigned long addr, const void *src, unsigned long count) {
  437. while (count) {
  438. count -= 1;
  439. writeb(*(const char *)src, addr);
  440. src += 1;
  441. addr += 1;
  442. }
  443. }
  444. void outsw(unsigned long addr, const void *src, unsigned long count) {
  445. while (count) {
  446. count -= 2;
  447. writew(*(const short *)src, addr);
  448. src += 2;
  449. addr += 2;
  450. }
  451. }
  452. void outsl(unsigned long addr, const void *src, unsigned long count) {
  453. while (count) {
  454. count -= 4;
  455. writel(*(const long *)src, addr);
  456. src += 4;
  457. addr += 4;
  458. }
  459. }
  460. void insb(unsigned long addr, void *dst, unsigned long count) {
  461. while (count) {
  462. count -= 1;
  463. *(unsigned char *)dst = readb(addr);
  464. dst += 1;
  465. addr += 1;
  466. }
  467. }
  468. void insw(unsigned long addr, void *dst, unsigned long count) {
  469. while (count) {
  470. count -= 2;
  471. *(unsigned short *)dst = readw(addr);
  472. dst += 2;
  473. addr += 2;
  474. }
  475. }
  476. void insl(unsigned long addr, void *dst, unsigned long count) {
  477. while (count) {
  478. count -= 4;
  479. /*
  480. * XXX I am sure we are in for an unaligned trap here.
  481. */
  482. *(unsigned long *)dst = readl(addr);
  483. dst += 4;
  484. addr += 4;
  485. }
  486. }