pci.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * linux/arch/unicore32/kernel/pci.c
  3. *
  4. * Code specific to PKUnity SoC and UniCore ISA
  5. *
  6. * Copyright (C) 2001-2010 GUAN Xue-tao
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * PCI bios-type initialisation for PCI machines
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/io.h>
  22. static int debug_pci;
  23. static int use_firmware;
  24. #define CONFIG_CMD(bus, devfn, where) \
  25. (0x80000000 | (bus->number << 16) | (devfn << 8) | (where & ~3))
  26. static int
  27. puv3_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  28. int size, u32 *value)
  29. {
  30. writel(CONFIG_CMD(bus, devfn, where), PCICFG_ADDR);
  31. switch (size) {
  32. case 1:
  33. *value = (readl(PCICFG_DATA) >> ((where & 3) * 8)) & 0xFF;
  34. break;
  35. case 2:
  36. *value = (readl(PCICFG_DATA) >> ((where & 2) * 8)) & 0xFFFF;
  37. break;
  38. case 4:
  39. *value = readl(PCICFG_DATA);
  40. break;
  41. }
  42. return PCIBIOS_SUCCESSFUL;
  43. }
  44. static int
  45. puv3_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  46. int size, u32 value)
  47. {
  48. writel(CONFIG_CMD(bus, devfn, where), PCICFG_ADDR);
  49. switch (size) {
  50. case 1:
  51. writel((readl(PCICFG_DATA) & ~FMASK(8, (where&3)*8))
  52. | FIELD(value, 8, (where&3)*8), PCICFG_DATA);
  53. break;
  54. case 2:
  55. writel((readl(PCICFG_DATA) & ~FMASK(16, (where&2)*8))
  56. | FIELD(value, 16, (where&2)*8), PCICFG_DATA);
  57. break;
  58. case 4:
  59. writel(value, PCICFG_DATA);
  60. break;
  61. }
  62. return PCIBIOS_SUCCESSFUL;
  63. }
  64. struct pci_ops pci_puv3_ops = {
  65. .read = puv3_read_config,
  66. .write = puv3_write_config,
  67. };
  68. void pci_puv3_preinit(void)
  69. {
  70. printk(KERN_DEBUG "PCI: PKUnity PCI Controller Initializing ...\n");
  71. /* config PCI bridge base */
  72. writel(io_v2p(PKUNITY_PCIBRI_BASE), PCICFG_BRIBASE);
  73. writel(0, PCIBRI_AHBCTL0);
  74. writel(io_v2p(PKUNITY_PCIBRI_BASE) | PCIBRI_BARx_MEM, PCIBRI_AHBBAR0);
  75. writel(0xFFFF0000, PCIBRI_AHBAMR0);
  76. writel(0, PCIBRI_AHBTAR0);
  77. writel(PCIBRI_CTLx_AT, PCIBRI_AHBCTL1);
  78. writel(io_v2p(PKUNITY_PCILIO_BASE) | PCIBRI_BARx_IO, PCIBRI_AHBBAR1);
  79. writel(0xFFFF0000, PCIBRI_AHBAMR1);
  80. writel(0x00000000, PCIBRI_AHBTAR1);
  81. writel(PCIBRI_CTLx_PREF, PCIBRI_AHBCTL2);
  82. writel(io_v2p(PKUNITY_PCIMEM_BASE) | PCIBRI_BARx_MEM, PCIBRI_AHBBAR2);
  83. writel(0xF8000000, PCIBRI_AHBAMR2);
  84. writel(0, PCIBRI_AHBTAR2);
  85. writel(io_v2p(PKUNITY_PCIAHB_BASE) | PCIBRI_BARx_MEM, PCIBRI_BAR1);
  86. writel(PCIBRI_CTLx_AT | PCIBRI_CTLx_PREF, PCIBRI_PCICTL0);
  87. writel(io_v2p(PKUNITY_PCIAHB_BASE) | PCIBRI_BARx_MEM, PCIBRI_PCIBAR0);
  88. writel(0xF8000000, PCIBRI_PCIAMR0);
  89. writel(PKUNITY_SDRAM_BASE, PCIBRI_PCITAR0);
  90. writel(readl(PCIBRI_CMD) | PCIBRI_CMD_IO | PCIBRI_CMD_MEM, PCIBRI_CMD);
  91. }
  92. static int __init pci_puv3_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  93. {
  94. if (dev->bus->number == 0) {
  95. #ifdef CONFIG_ARCH_FPGA /* 4 pci slots */
  96. if (dev->devfn == 0x00)
  97. return IRQ_PCIINTA;
  98. else if (dev->devfn == 0x08)
  99. return IRQ_PCIINTB;
  100. else if (dev->devfn == 0x10)
  101. return IRQ_PCIINTC;
  102. else if (dev->devfn == 0x18)
  103. return IRQ_PCIINTD;
  104. #endif
  105. #ifdef CONFIG_PUV3_DB0913 /* 3 pci slots */
  106. if (dev->devfn == 0x30)
  107. return IRQ_PCIINTB;
  108. else if (dev->devfn == 0x60)
  109. return IRQ_PCIINTC;
  110. else if (dev->devfn == 0x58)
  111. return IRQ_PCIINTD;
  112. #endif
  113. #if defined(CONFIG_PUV3_NB0916) || defined(CONFIG_PUV3_SMW0919)
  114. /* only support 2 pci devices */
  115. if (dev->devfn == 0x00)
  116. return IRQ_PCIINTC; /* sata */
  117. #endif
  118. }
  119. return -1;
  120. }
  121. /*
  122. * Only first 128MB of memory can be accessed via PCI.
  123. * We use GFP_DMA to allocate safe buffers to do map/unmap.
  124. * This is really ugly and we need a better way of specifying
  125. * DMA-capable regions of memory.
  126. */
  127. void __init puv3_pci_adjust_zones(unsigned long *zone_size,
  128. unsigned long *zhole_size)
  129. {
  130. unsigned int sz = SZ_128M >> PAGE_SHIFT;
  131. /*
  132. * Only adjust if > 128M on current system
  133. */
  134. if (zone_size[0] <= sz)
  135. return;
  136. zone_size[1] = zone_size[0] - sz;
  137. zone_size[0] = sz;
  138. zhole_size[1] = zhole_size[0];
  139. zhole_size[0] = 0;
  140. }
  141. void __devinit pcibios_update_irq(struct pci_dev *dev, int irq)
  142. {
  143. if (debug_pci)
  144. printk(KERN_DEBUG "PCI: Assigning IRQ %02d to %s\n",
  145. irq, pci_name(dev));
  146. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  147. }
  148. /*
  149. * If the bus contains any of these devices, then we must not turn on
  150. * parity checking of any kind.
  151. */
  152. static inline int pdev_bad_for_parity(struct pci_dev *dev)
  153. {
  154. return 0;
  155. }
  156. /*
  157. * pcibios_fixup_bus - Called after each bus is probed,
  158. * but before its children are examined.
  159. */
  160. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  161. {
  162. struct pci_dev *dev;
  163. u16 features = PCI_COMMAND_SERR
  164. | PCI_COMMAND_PARITY
  165. | PCI_COMMAND_FAST_BACK;
  166. bus->resource[0] = &ioport_resource;
  167. bus->resource[1] = &iomem_resource;
  168. /*
  169. * Walk the devices on this bus, working out what we can
  170. * and can't support.
  171. */
  172. list_for_each_entry(dev, &bus->devices, bus_list) {
  173. u16 status;
  174. pci_read_config_word(dev, PCI_STATUS, &status);
  175. /*
  176. * If any device on this bus does not support fast back
  177. * to back transfers, then the bus as a whole is not able
  178. * to support them. Having fast back to back transfers
  179. * on saves us one PCI cycle per transaction.
  180. */
  181. if (!(status & PCI_STATUS_FAST_BACK))
  182. features &= ~PCI_COMMAND_FAST_BACK;
  183. if (pdev_bad_for_parity(dev))
  184. features &= ~(PCI_COMMAND_SERR
  185. | PCI_COMMAND_PARITY);
  186. switch (dev->class >> 8) {
  187. case PCI_CLASS_BRIDGE_PCI:
  188. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &status);
  189. status |= PCI_BRIDGE_CTL_PARITY
  190. | PCI_BRIDGE_CTL_MASTER_ABORT;
  191. status &= ~(PCI_BRIDGE_CTL_BUS_RESET
  192. | PCI_BRIDGE_CTL_FAST_BACK);
  193. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, status);
  194. break;
  195. case PCI_CLASS_BRIDGE_CARDBUS:
  196. pci_read_config_word(dev, PCI_CB_BRIDGE_CONTROL,
  197. &status);
  198. status |= PCI_CB_BRIDGE_CTL_PARITY
  199. | PCI_CB_BRIDGE_CTL_MASTER_ABORT;
  200. pci_write_config_word(dev, PCI_CB_BRIDGE_CONTROL,
  201. status);
  202. break;
  203. }
  204. }
  205. /*
  206. * Now walk the devices again, this time setting them up.
  207. */
  208. list_for_each_entry(dev, &bus->devices, bus_list) {
  209. u16 cmd;
  210. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  211. cmd |= features;
  212. pci_write_config_word(dev, PCI_COMMAND, cmd);
  213. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  214. L1_CACHE_BYTES >> 2);
  215. }
  216. /*
  217. * Propagate the flags to the PCI bridge.
  218. */
  219. if (bus->self && bus->self->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  220. if (features & PCI_COMMAND_FAST_BACK)
  221. bus->bridge_ctl |= PCI_BRIDGE_CTL_FAST_BACK;
  222. if (features & PCI_COMMAND_PARITY)
  223. bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY;
  224. }
  225. /*
  226. * Report what we did for this bus
  227. */
  228. printk(KERN_INFO "PCI: bus%d: Fast back to back transfers %sabled\n",
  229. bus->number, (features & PCI_COMMAND_FAST_BACK) ? "en" : "dis");
  230. }
  231. #ifdef CONFIG_HOTPLUG
  232. EXPORT_SYMBOL(pcibios_fixup_bus);
  233. #endif
  234. static int __init pci_common_init(void)
  235. {
  236. struct pci_bus *puv3_bus;
  237. pci_puv3_preinit();
  238. puv3_bus = pci_scan_bus(0, &pci_puv3_ops, NULL);
  239. if (!puv3_bus)
  240. panic("PCI: unable to scan bus!");
  241. pci_fixup_irqs(pci_common_swizzle, pci_puv3_map_irq);
  242. if (!use_firmware) {
  243. /*
  244. * Size the bridge windows.
  245. */
  246. pci_bus_size_bridges(puv3_bus);
  247. /*
  248. * Assign resources.
  249. */
  250. pci_bus_assign_resources(puv3_bus);
  251. }
  252. /*
  253. * Tell drivers about devices found.
  254. */
  255. pci_bus_add_devices(puv3_bus);
  256. return 0;
  257. }
  258. subsys_initcall(pci_common_init);
  259. char * __devinit pcibios_setup(char *str)
  260. {
  261. if (!strcmp(str, "debug")) {
  262. debug_pci = 1;
  263. return NULL;
  264. } else if (!strcmp(str, "firmware")) {
  265. use_firmware = 1;
  266. return NULL;
  267. }
  268. return str;
  269. }
  270. /*
  271. * From arch/i386/kernel/pci-i386.c:
  272. *
  273. * We need to avoid collisions with `mirrored' VGA ports
  274. * and other strange ISA hardware, so we always want the
  275. * addresses to be allocated in the 0x000-0x0ff region
  276. * modulo 0x400.
  277. *
  278. * Why? Because some silly external IO cards only decode
  279. * the low 10 bits of the IO address. The 0x00-0xff region
  280. * is reserved for motherboard devices that decode all 16
  281. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  282. * but we want to try to avoid allocating at 0x2900-0x2bff
  283. * which might be mirrored at 0x0100-0x03ff..
  284. */
  285. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  286. resource_size_t size, resource_size_t align)
  287. {
  288. resource_size_t start = res->start;
  289. if (res->flags & IORESOURCE_IO && start & 0x300)
  290. start = (start + 0x3ff) & ~0x3ff;
  291. start = (start + align - 1) & ~(align - 1);
  292. return start;
  293. }
  294. /**
  295. * pcibios_enable_device - Enable I/O and memory.
  296. * @dev: PCI device to be enabled
  297. */
  298. int pcibios_enable_device(struct pci_dev *dev, int mask)
  299. {
  300. u16 cmd, old_cmd;
  301. int idx;
  302. struct resource *r;
  303. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  304. old_cmd = cmd;
  305. for (idx = 0; idx < 6; idx++) {
  306. /* Only set up the requested stuff */
  307. if (!(mask & (1 << idx)))
  308. continue;
  309. r = dev->resource + idx;
  310. if (!r->start && r->end) {
  311. printk(KERN_ERR "PCI: Device %s not available because"
  312. " of resource collisions\n", pci_name(dev));
  313. return -EINVAL;
  314. }
  315. if (r->flags & IORESOURCE_IO)
  316. cmd |= PCI_COMMAND_IO;
  317. if (r->flags & IORESOURCE_MEM)
  318. cmd |= PCI_COMMAND_MEMORY;
  319. }
  320. /*
  321. * Bridges (eg, cardbus bridges) need to be fully enabled
  322. */
  323. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE)
  324. cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  325. if (cmd != old_cmd) {
  326. printk("PCI: enabling device %s (%04x -> %04x)\n",
  327. pci_name(dev), old_cmd, cmd);
  328. pci_write_config_word(dev, PCI_COMMAND, cmd);
  329. }
  330. return 0;
  331. }
  332. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  333. enum pci_mmap_state mmap_state, int write_combine)
  334. {
  335. unsigned long phys;
  336. if (mmap_state == pci_mmap_io)
  337. return -EINVAL;
  338. phys = vma->vm_pgoff;
  339. /*
  340. * Mark this as IO
  341. */
  342. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  343. if (remap_pfn_range(vma, vma->vm_start, phys,
  344. vma->vm_end - vma->vm_start,
  345. vma->vm_page_prot))
  346. return -EAGAIN;
  347. return 0;
  348. }