pcie.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * PCI-E support for CNS3xxx
  3. *
  4. * Copyright 2008 Cavium Networks
  5. * Richard Liu <richard.liu@caviumnetworks.com>
  6. * Copyright 2010 MontaVista Software, LLC.
  7. * Anton Vorontsov <avorontsov@mvista.com>
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/bug.h>
  16. #include <linux/pci.h>
  17. #include <linux/io.h>
  18. #include <linux/ioport.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/ptrace.h>
  21. #include <asm/mach/map.h>
  22. #include <mach/cns3xxx.h>
  23. #include "core.h"
  24. enum cns3xxx_access_type {
  25. CNS3XXX_HOST_TYPE = 0,
  26. CNS3XXX_CFG0_TYPE,
  27. CNS3XXX_CFG1_TYPE,
  28. CNS3XXX_NUM_ACCESS_TYPES,
  29. };
  30. struct cns3xxx_pcie {
  31. struct map_desc cfg_bases[CNS3XXX_NUM_ACCESS_TYPES];
  32. unsigned int irqs[2];
  33. struct resource res_io;
  34. struct resource res_mem;
  35. struct hw_pci hw_pci;
  36. bool linked;
  37. };
  38. static struct cns3xxx_pcie cns3xxx_pcie[]; /* forward decl. */
  39. static struct cns3xxx_pcie *sysdata_to_cnspci(void *sysdata)
  40. {
  41. struct pci_sys_data *root = sysdata;
  42. return &cns3xxx_pcie[root->domain];
  43. }
  44. static struct cns3xxx_pcie *pdev_to_cnspci(const struct pci_dev *dev)
  45. {
  46. return sysdata_to_cnspci(dev->sysdata);
  47. }
  48. static struct cns3xxx_pcie *pbus_to_cnspci(struct pci_bus *bus)
  49. {
  50. return sysdata_to_cnspci(bus->sysdata);
  51. }
  52. static void __iomem *cns3xxx_pci_cfg_base(struct pci_bus *bus,
  53. unsigned int devfn, int where)
  54. {
  55. struct cns3xxx_pcie *cnspci = pbus_to_cnspci(bus);
  56. int busno = bus->number;
  57. int slot = PCI_SLOT(devfn);
  58. int offset;
  59. enum cns3xxx_access_type type;
  60. void __iomem *base;
  61. /* If there is no link, just show the CNS PCI bridge. */
  62. if (!cnspci->linked && (busno > 0 || slot > 0))
  63. return NULL;
  64. /*
  65. * The CNS PCI bridge doesn't fit into the PCI hierarchy, though
  66. * we still want to access it. For this to work, we must place
  67. * the first device on the same bus as the CNS PCI bridge.
  68. */
  69. if (busno == 0) {
  70. if (slot > 1)
  71. return NULL;
  72. type = slot;
  73. } else {
  74. type = CNS3XXX_CFG1_TYPE;
  75. }
  76. base = (void __iomem *)cnspci->cfg_bases[type].virtual;
  77. offset = ((busno & 0xf) << 20) | (devfn << 12) | (where & 0xffc);
  78. return base + offset;
  79. }
  80. static int cns3xxx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
  81. int where, int size, u32 *val)
  82. {
  83. u32 v;
  84. void __iomem *base;
  85. u32 mask = (0x1ull << (size * 8)) - 1;
  86. int shift = (where % 4) * 8;
  87. base = cns3xxx_pci_cfg_base(bus, devfn, where);
  88. if (!base) {
  89. *val = 0xffffffff;
  90. return PCIBIOS_SUCCESSFUL;
  91. }
  92. v = __raw_readl(base);
  93. if (bus->number == 0 && devfn == 0 &&
  94. (where & 0xffc) == PCI_CLASS_REVISION) {
  95. /*
  96. * RC's class is 0xb, but Linux PCI driver needs 0x604
  97. * for a PCIe bridge. So we must fixup the class code
  98. * to 0x604 here.
  99. */
  100. v &= 0xff;
  101. v |= 0x604 << 16;
  102. }
  103. *val = (v >> shift) & mask;
  104. return PCIBIOS_SUCCESSFUL;
  105. }
  106. static int cns3xxx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
  107. int where, int size, u32 val)
  108. {
  109. u32 v;
  110. void __iomem *base;
  111. u32 mask = (0x1ull << (size * 8)) - 1;
  112. int shift = (where % 4) * 8;
  113. base = cns3xxx_pci_cfg_base(bus, devfn, where);
  114. if (!base)
  115. return PCIBIOS_SUCCESSFUL;
  116. v = __raw_readl(base);
  117. v &= ~(mask << shift);
  118. v |= (val & mask) << shift;
  119. __raw_writel(v, base);
  120. return PCIBIOS_SUCCESSFUL;
  121. }
  122. static int cns3xxx_pci_setup(int nr, struct pci_sys_data *sys)
  123. {
  124. struct cns3xxx_pcie *cnspci = sysdata_to_cnspci(sys);
  125. struct resource *res_io = &cnspci->res_io;
  126. struct resource *res_mem = &cnspci->res_mem;
  127. BUG_ON(request_resource(&iomem_resource, res_io) ||
  128. request_resource(&iomem_resource, res_mem));
  129. pci_add_resource_offset(&sys->resources, res_io, sys->io_offset);
  130. pci_add_resource_offset(&sys->resources, res_mem, sys->mem_offset);
  131. return 1;
  132. }
  133. static struct pci_ops cns3xxx_pcie_ops = {
  134. .read = cns3xxx_pci_read_config,
  135. .write = cns3xxx_pci_write_config,
  136. };
  137. static int cns3xxx_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  138. {
  139. struct cns3xxx_pcie *cnspci = pdev_to_cnspci(dev);
  140. int irq = cnspci->irqs[slot];
  141. pr_info("PCIe map irq: %04d:%02x:%02x.%02x slot %d, pin %d, irq: %d\n",
  142. pci_domain_nr(dev->bus), dev->bus->number, PCI_SLOT(dev->devfn),
  143. PCI_FUNC(dev->devfn), slot, pin, irq);
  144. return irq;
  145. }
  146. static struct cns3xxx_pcie cns3xxx_pcie[] = {
  147. [0] = {
  148. .cfg_bases = {
  149. [CNS3XXX_HOST_TYPE] = {
  150. .virtual = CNS3XXX_PCIE0_HOST_BASE_VIRT,
  151. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_HOST_BASE),
  152. .length = SZ_16M,
  153. .type = MT_DEVICE,
  154. },
  155. [CNS3XXX_CFG0_TYPE] = {
  156. .virtual = CNS3XXX_PCIE0_CFG0_BASE_VIRT,
  157. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_CFG0_BASE),
  158. .length = SZ_16M,
  159. .type = MT_DEVICE,
  160. },
  161. [CNS3XXX_CFG1_TYPE] = {
  162. .virtual = CNS3XXX_PCIE0_CFG1_BASE_VIRT,
  163. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_CFG1_BASE),
  164. .length = SZ_16M,
  165. .type = MT_DEVICE,
  166. },
  167. },
  168. .res_io = {
  169. .name = "PCIe0 I/O space",
  170. .start = CNS3XXX_PCIE0_IO_BASE,
  171. .end = CNS3XXX_PCIE0_IO_BASE + SZ_16M - 1,
  172. .flags = IORESOURCE_IO,
  173. },
  174. .res_mem = {
  175. .name = "PCIe0 non-prefetchable",
  176. .start = CNS3XXX_PCIE0_MEM_BASE,
  177. .end = CNS3XXX_PCIE0_MEM_BASE + SZ_16M - 1,
  178. .flags = IORESOURCE_MEM,
  179. },
  180. .irqs = { IRQ_CNS3XXX_PCIE0_RC, IRQ_CNS3XXX_PCIE0_DEVICE, },
  181. .hw_pci = {
  182. .domain = 0,
  183. .nr_controllers = 1,
  184. .ops = &cns3xxx_pcie_ops,
  185. .setup = cns3xxx_pci_setup,
  186. .map_irq = cns3xxx_pcie_map_irq,
  187. },
  188. },
  189. [1] = {
  190. .cfg_bases = {
  191. [CNS3XXX_HOST_TYPE] = {
  192. .virtual = CNS3XXX_PCIE1_HOST_BASE_VIRT,
  193. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_HOST_BASE),
  194. .length = SZ_16M,
  195. .type = MT_DEVICE,
  196. },
  197. [CNS3XXX_CFG0_TYPE] = {
  198. .virtual = CNS3XXX_PCIE1_CFG0_BASE_VIRT,
  199. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_CFG0_BASE),
  200. .length = SZ_16M,
  201. .type = MT_DEVICE,
  202. },
  203. [CNS3XXX_CFG1_TYPE] = {
  204. .virtual = CNS3XXX_PCIE1_CFG1_BASE_VIRT,
  205. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_CFG1_BASE),
  206. .length = SZ_16M,
  207. .type = MT_DEVICE,
  208. },
  209. },
  210. .res_io = {
  211. .name = "PCIe1 I/O space",
  212. .start = CNS3XXX_PCIE1_IO_BASE,
  213. .end = CNS3XXX_PCIE1_IO_BASE + SZ_16M - 1,
  214. .flags = IORESOURCE_IO,
  215. },
  216. .res_mem = {
  217. .name = "PCIe1 non-prefetchable",
  218. .start = CNS3XXX_PCIE1_MEM_BASE,
  219. .end = CNS3XXX_PCIE1_MEM_BASE + SZ_16M - 1,
  220. .flags = IORESOURCE_MEM,
  221. },
  222. .irqs = { IRQ_CNS3XXX_PCIE1_RC, IRQ_CNS3XXX_PCIE1_DEVICE, },
  223. .hw_pci = {
  224. .domain = 1,
  225. .nr_controllers = 1,
  226. .ops = &cns3xxx_pcie_ops,
  227. .setup = cns3xxx_pci_setup,
  228. .map_irq = cns3xxx_pcie_map_irq,
  229. },
  230. },
  231. };
  232. static void __init cns3xxx_pcie_check_link(struct cns3xxx_pcie *cnspci)
  233. {
  234. int port = cnspci->hw_pci.domain;
  235. u32 reg;
  236. unsigned long time;
  237. reg = __raw_readl(MISC_PCIE_CTRL(port));
  238. /*
  239. * Enable Application Request to 1, it will exit L1 automatically,
  240. * but when chip back, it will use another clock, still can use 0x1.
  241. */
  242. reg |= 0x3;
  243. __raw_writel(reg, MISC_PCIE_CTRL(port));
  244. pr_info("PCIe: Port[%d] Enable PCIe LTSSM\n", port);
  245. pr_info("PCIe: Port[%d] Check data link layer...", port);
  246. time = jiffies;
  247. while (1) {
  248. reg = __raw_readl(MISC_PCIE_PM_DEBUG(port));
  249. if (reg & 0x1) {
  250. pr_info("Link up.\n");
  251. cnspci->linked = 1;
  252. break;
  253. } else if (time_after(jiffies, time + 50)) {
  254. pr_info("Device not found.\n");
  255. break;
  256. }
  257. }
  258. }
  259. static void __init cns3xxx_pcie_hw_init(struct cns3xxx_pcie *cnspci)
  260. {
  261. int port = cnspci->hw_pci.domain;
  262. struct pci_sys_data sd = {
  263. .domain = port,
  264. };
  265. struct pci_bus bus = {
  266. .number = 0,
  267. .ops = &cns3xxx_pcie_ops,
  268. .sysdata = &sd,
  269. };
  270. u32 io_base = cnspci->res_io.start >> 16;
  271. u32 mem_base = cnspci->res_mem.start >> 16;
  272. u32 host_base = cnspci->cfg_bases[CNS3XXX_HOST_TYPE].pfn;
  273. u32 cfg0_base = cnspci->cfg_bases[CNS3XXX_CFG0_TYPE].pfn;
  274. u32 devfn = 0;
  275. u8 tmp8;
  276. u16 pos;
  277. u16 dc;
  278. host_base = (__pfn_to_phys(host_base) - 1) >> 16;
  279. cfg0_base = (__pfn_to_phys(cfg0_base) - 1) >> 16;
  280. pci_bus_write_config_byte(&bus, devfn, PCI_PRIMARY_BUS, 0);
  281. pci_bus_write_config_byte(&bus, devfn, PCI_SECONDARY_BUS, 1);
  282. pci_bus_write_config_byte(&bus, devfn, PCI_SUBORDINATE_BUS, 1);
  283. pci_bus_read_config_byte(&bus, devfn, PCI_PRIMARY_BUS, &tmp8);
  284. pci_bus_read_config_byte(&bus, devfn, PCI_SECONDARY_BUS, &tmp8);
  285. pci_bus_read_config_byte(&bus, devfn, PCI_SUBORDINATE_BUS, &tmp8);
  286. pci_bus_write_config_word(&bus, devfn, PCI_MEMORY_BASE, mem_base);
  287. pci_bus_write_config_word(&bus, devfn, PCI_MEMORY_LIMIT, host_base);
  288. pci_bus_write_config_word(&bus, devfn, PCI_IO_BASE_UPPER16, io_base);
  289. pci_bus_write_config_word(&bus, devfn, PCI_IO_LIMIT_UPPER16, cfg0_base);
  290. if (!cnspci->linked)
  291. return;
  292. /* Set Device Max_Read_Request_Size to 128 byte */
  293. devfn = PCI_DEVFN(1, 0);
  294. pos = pci_bus_find_capability(&bus, devfn, PCI_CAP_ID_EXP);
  295. pci_bus_read_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, &dc);
  296. dc &= ~(0x3 << 12); /* Clear Device Control Register [14:12] */
  297. pci_bus_write_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, dc);
  298. pci_bus_read_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, &dc);
  299. if (!(dc & (0x3 << 12)))
  300. pr_info("PCIe: Set Device Max_Read_Request_Size to 128 byte\n");
  301. /* Disable PCIe0 Interrupt Mask INTA to INTD */
  302. __raw_writel(~0x3FFF, MISC_PCIE_INT_MASK(port));
  303. }
  304. static int cns3xxx_pcie_abort_handler(unsigned long addr, unsigned int fsr,
  305. struct pt_regs *regs)
  306. {
  307. if (fsr & (1 << 10))
  308. regs->ARM_pc += 4;
  309. return 0;
  310. }
  311. static int __init cns3xxx_pcie_init(void)
  312. {
  313. int i;
  314. pcibios_min_io = 0;
  315. pcibios_min_mem = 0;
  316. hook_fault_code(16 + 6, cns3xxx_pcie_abort_handler, SIGBUS, 0,
  317. "imprecise external abort");
  318. for (i = 0; i < ARRAY_SIZE(cns3xxx_pcie); i++) {
  319. iotable_init(cns3xxx_pcie[i].cfg_bases,
  320. ARRAY_SIZE(cns3xxx_pcie[i].cfg_bases));
  321. cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_PCIE(i));
  322. cns3xxx_pwr_soft_rst(0x1 << PM_SOFT_RST_REG_OFFST_PCIE(i));
  323. cns3xxx_pcie_check_link(&cns3xxx_pcie[i]);
  324. cns3xxx_pcie_hw_init(&cns3xxx_pcie[i]);
  325. pci_common_init(&cns3xxx_pcie[i].hw_pci);
  326. }
  327. pci_assign_unassigned_resources();
  328. return 0;
  329. }
  330. device_initcall(cns3xxx_pcie_init);