pci.c 10.0 KB

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