pci.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /*
  141. * If the bus contains any of these devices, then we must not turn on
  142. * parity checking of any kind.
  143. */
  144. static inline int pdev_bad_for_parity(struct pci_dev *dev)
  145. {
  146. return 0;
  147. }
  148. /*
  149. * pcibios_fixup_bus - Called after each bus is probed,
  150. * but before its children are examined.
  151. */
  152. void pcibios_fixup_bus(struct pci_bus *bus)
  153. {
  154. struct pci_dev *dev;
  155. u16 features = PCI_COMMAND_SERR
  156. | PCI_COMMAND_PARITY
  157. | PCI_COMMAND_FAST_BACK;
  158. bus->resource[0] = &ioport_resource;
  159. bus->resource[1] = &iomem_resource;
  160. /*
  161. * Walk the devices on this bus, working out what we can
  162. * and can't support.
  163. */
  164. list_for_each_entry(dev, &bus->devices, bus_list) {
  165. u16 status;
  166. pci_read_config_word(dev, PCI_STATUS, &status);
  167. /*
  168. * If any device on this bus does not support fast back
  169. * to back transfers, then the bus as a whole is not able
  170. * to support them. Having fast back to back transfers
  171. * on saves us one PCI cycle per transaction.
  172. */
  173. if (!(status & PCI_STATUS_FAST_BACK))
  174. features &= ~PCI_COMMAND_FAST_BACK;
  175. if (pdev_bad_for_parity(dev))
  176. features &= ~(PCI_COMMAND_SERR
  177. | PCI_COMMAND_PARITY);
  178. switch (dev->class >> 8) {
  179. case PCI_CLASS_BRIDGE_PCI:
  180. pci_read_config_word(dev, PCI_BRIDGE_CONTROL, &status);
  181. status |= PCI_BRIDGE_CTL_PARITY
  182. | PCI_BRIDGE_CTL_MASTER_ABORT;
  183. status &= ~(PCI_BRIDGE_CTL_BUS_RESET
  184. | PCI_BRIDGE_CTL_FAST_BACK);
  185. pci_write_config_word(dev, PCI_BRIDGE_CONTROL, status);
  186. break;
  187. case PCI_CLASS_BRIDGE_CARDBUS:
  188. pci_read_config_word(dev, PCI_CB_BRIDGE_CONTROL,
  189. &status);
  190. status |= PCI_CB_BRIDGE_CTL_PARITY
  191. | PCI_CB_BRIDGE_CTL_MASTER_ABORT;
  192. pci_write_config_word(dev, PCI_CB_BRIDGE_CONTROL,
  193. status);
  194. break;
  195. }
  196. }
  197. /*
  198. * Now walk the devices again, this time setting them up.
  199. */
  200. list_for_each_entry(dev, &bus->devices, bus_list) {
  201. u16 cmd;
  202. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  203. cmd |= features;
  204. pci_write_config_word(dev, PCI_COMMAND, cmd);
  205. pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE,
  206. L1_CACHE_BYTES >> 2);
  207. }
  208. /*
  209. * Propagate the flags to the PCI bridge.
  210. */
  211. if (bus->self && bus->self->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
  212. if (features & PCI_COMMAND_FAST_BACK)
  213. bus->bridge_ctl |= PCI_BRIDGE_CTL_FAST_BACK;
  214. if (features & PCI_COMMAND_PARITY)
  215. bus->bridge_ctl |= PCI_BRIDGE_CTL_PARITY;
  216. }
  217. /*
  218. * Report what we did for this bus
  219. */
  220. printk(KERN_INFO "PCI: bus%d: Fast back to back transfers %sabled\n",
  221. bus->number, (features & PCI_COMMAND_FAST_BACK) ? "en" : "dis");
  222. }
  223. EXPORT_SYMBOL(pcibios_fixup_bus);
  224. static int __init pci_common_init(void)
  225. {
  226. struct pci_bus *puv3_bus;
  227. pci_puv3_preinit();
  228. puv3_bus = pci_scan_bus(0, &pci_puv3_ops, NULL);
  229. if (!puv3_bus)
  230. panic("PCI: unable to scan bus!");
  231. pci_fixup_irqs(pci_common_swizzle, pci_puv3_map_irq);
  232. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  233. pci_bus_size_bridges(puv3_bus);
  234. pci_bus_assign_resources(puv3_bus);
  235. }
  236. pci_bus_add_devices(puv3_bus);
  237. return 0;
  238. }
  239. subsys_initcall(pci_common_init);
  240. char * __init pcibios_setup(char *str)
  241. {
  242. if (!strcmp(str, "debug")) {
  243. debug_pci = 1;
  244. return NULL;
  245. } else if (!strcmp(str, "firmware")) {
  246. pci_add_flags(PCI_PROBE_ONLY);
  247. return NULL;
  248. }
  249. return str;
  250. }
  251. void pcibios_set_master(struct pci_dev *dev)
  252. {
  253. /* No special bus mastering setup handling */
  254. }
  255. /*
  256. * From arch/i386/kernel/pci-i386.c:
  257. *
  258. * We need to avoid collisions with `mirrored' VGA ports
  259. * and other strange ISA hardware, so we always want the
  260. * addresses to be allocated in the 0x000-0x0ff region
  261. * modulo 0x400.
  262. *
  263. * Why? Because some silly external IO cards only decode
  264. * the low 10 bits of the IO address. The 0x00-0xff region
  265. * is reserved for motherboard devices that decode all 16
  266. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  267. * but we want to try to avoid allocating at 0x2900-0x2bff
  268. * which might be mirrored at 0x0100-0x03ff..
  269. */
  270. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  271. resource_size_t size, resource_size_t align)
  272. {
  273. resource_size_t start = res->start;
  274. if (res->flags & IORESOURCE_IO && start & 0x300)
  275. start = (start + 0x3ff) & ~0x3ff;
  276. start = (start + align - 1) & ~(align - 1);
  277. return start;
  278. }
  279. /**
  280. * pcibios_enable_device - Enable I/O and memory.
  281. * @dev: PCI device to be enabled
  282. */
  283. int pcibios_enable_device(struct pci_dev *dev, int mask)
  284. {
  285. u16 cmd, old_cmd;
  286. int idx;
  287. struct resource *r;
  288. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  289. old_cmd = cmd;
  290. for (idx = 0; idx < 6; idx++) {
  291. /* Only set up the requested stuff */
  292. if (!(mask & (1 << idx)))
  293. continue;
  294. r = dev->resource + idx;
  295. if (!r->start && r->end) {
  296. printk(KERN_ERR "PCI: Device %s not available because"
  297. " of resource collisions\n", pci_name(dev));
  298. return -EINVAL;
  299. }
  300. if (r->flags & IORESOURCE_IO)
  301. cmd |= PCI_COMMAND_IO;
  302. if (r->flags & IORESOURCE_MEM)
  303. cmd |= PCI_COMMAND_MEMORY;
  304. }
  305. /*
  306. * Bridges (eg, cardbus bridges) need to be fully enabled
  307. */
  308. if ((dev->class >> 16) == PCI_BASE_CLASS_BRIDGE)
  309. cmd |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY;
  310. if (cmd != old_cmd) {
  311. printk("PCI: enabling device %s (%04x -> %04x)\n",
  312. pci_name(dev), old_cmd, cmd);
  313. pci_write_config_word(dev, PCI_COMMAND, cmd);
  314. }
  315. return 0;
  316. }
  317. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  318. enum pci_mmap_state mmap_state, int write_combine)
  319. {
  320. unsigned long phys;
  321. if (mmap_state == pci_mmap_io)
  322. return -EINVAL;
  323. phys = vma->vm_pgoff;
  324. /*
  325. * Mark this as IO
  326. */
  327. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  328. if (remap_pfn_range(vma, vma->vm_start, phys,
  329. vma->vm_end - vma->vm_start,
  330. vma->vm_page_prot))
  331. return -EAGAIN;
  332. return 0;
  333. }