pcie.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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(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. struct resource **sysres = sys->resource;
  128. BUG_ON(request_resource(&iomem_resource, res_io) ||
  129. request_resource(&iomem_resource, res_mem));
  130. sysres[0] = res_io;
  131. sysres[1] = res_mem;
  132. return 1;
  133. }
  134. static struct pci_ops cns3xxx_pcie_ops = {
  135. .read = cns3xxx_pci_read_config,
  136. .write = cns3xxx_pci_write_config,
  137. };
  138. static struct pci_bus *cns3xxx_pci_scan_bus(int nr, struct pci_sys_data *sys)
  139. {
  140. return pci_scan_bus(sys->busnr, &cns3xxx_pcie_ops, sys);
  141. }
  142. static int cns3xxx_pcie_map_irq(struct pci_dev *dev, u8 slot, u8 pin)
  143. {
  144. struct cns3xxx_pcie *cnspci = pdev_to_cnspci(dev);
  145. int irq = cnspci->irqs[slot];
  146. pr_info("PCIe map irq: %04d:%02x:%02x.%02x slot %d, pin %d, irq: %d\n",
  147. pci_domain_nr(dev->bus), dev->bus->number, PCI_SLOT(dev->devfn),
  148. PCI_FUNC(dev->devfn), slot, pin, irq);
  149. return irq;
  150. }
  151. static struct cns3xxx_pcie cns3xxx_pcie[] = {
  152. [0] = {
  153. .cfg_bases = {
  154. [CNS3XXX_HOST_TYPE] = {
  155. .virtual = CNS3XXX_PCIE0_HOST_BASE_VIRT,
  156. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_HOST_BASE),
  157. .length = SZ_16M,
  158. .type = MT_DEVICE,
  159. },
  160. [CNS3XXX_CFG0_TYPE] = {
  161. .virtual = CNS3XXX_PCIE0_CFG0_BASE_VIRT,
  162. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_CFG0_BASE),
  163. .length = SZ_16M,
  164. .type = MT_DEVICE,
  165. },
  166. [CNS3XXX_CFG1_TYPE] = {
  167. .virtual = CNS3XXX_PCIE0_CFG1_BASE_VIRT,
  168. .pfn = __phys_to_pfn(CNS3XXX_PCIE0_CFG1_BASE),
  169. .length = SZ_16M,
  170. .type = MT_DEVICE,
  171. },
  172. },
  173. .res_io = {
  174. .name = "PCIe0 I/O space",
  175. .start = CNS3XXX_PCIE0_IO_BASE,
  176. .end = CNS3XXX_PCIE0_IO_BASE + SZ_16M - 1,
  177. .flags = IORESOURCE_IO,
  178. },
  179. .res_mem = {
  180. .name = "PCIe0 non-prefetchable",
  181. .start = CNS3XXX_PCIE0_MEM_BASE,
  182. .end = CNS3XXX_PCIE0_MEM_BASE + SZ_16M - 1,
  183. .flags = IORESOURCE_MEM,
  184. },
  185. .irqs = { IRQ_CNS3XXX_PCIE0_RC, IRQ_CNS3XXX_PCIE0_DEVICE, },
  186. .hw_pci = {
  187. .domain = 0,
  188. .swizzle = pci_std_swizzle,
  189. .nr_controllers = 1,
  190. .setup = cns3xxx_pci_setup,
  191. .scan = cns3xxx_pci_scan_bus,
  192. .map_irq = cns3xxx_pcie_map_irq,
  193. },
  194. },
  195. [1] = {
  196. .cfg_bases = {
  197. [CNS3XXX_HOST_TYPE] = {
  198. .virtual = CNS3XXX_PCIE1_HOST_BASE_VIRT,
  199. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_HOST_BASE),
  200. .length = SZ_16M,
  201. .type = MT_DEVICE,
  202. },
  203. [CNS3XXX_CFG0_TYPE] = {
  204. .virtual = CNS3XXX_PCIE1_CFG0_BASE_VIRT,
  205. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_CFG0_BASE),
  206. .length = SZ_16M,
  207. .type = MT_DEVICE,
  208. },
  209. [CNS3XXX_CFG1_TYPE] = {
  210. .virtual = CNS3XXX_PCIE1_CFG1_BASE_VIRT,
  211. .pfn = __phys_to_pfn(CNS3XXX_PCIE1_CFG1_BASE),
  212. .length = SZ_16M,
  213. .type = MT_DEVICE,
  214. },
  215. },
  216. .res_io = {
  217. .name = "PCIe1 I/O space",
  218. .start = CNS3XXX_PCIE1_IO_BASE,
  219. .end = CNS3XXX_PCIE1_IO_BASE + SZ_16M - 1,
  220. .flags = IORESOURCE_IO,
  221. },
  222. .res_mem = {
  223. .name = "PCIe1 non-prefetchable",
  224. .start = CNS3XXX_PCIE1_MEM_BASE,
  225. .end = CNS3XXX_PCIE1_MEM_BASE + SZ_16M - 1,
  226. .flags = IORESOURCE_MEM,
  227. },
  228. .irqs = { IRQ_CNS3XXX_PCIE1_RC, IRQ_CNS3XXX_PCIE1_DEVICE, },
  229. .hw_pci = {
  230. .domain = 1,
  231. .swizzle = pci_std_swizzle,
  232. .nr_controllers = 1,
  233. .setup = cns3xxx_pci_setup,
  234. .scan = cns3xxx_pci_scan_bus,
  235. .map_irq = cns3xxx_pcie_map_irq,
  236. },
  237. },
  238. };
  239. static void __init cns3xxx_pcie_check_link(struct cns3xxx_pcie *cnspci)
  240. {
  241. int port = cnspci->hw_pci.domain;
  242. u32 reg;
  243. unsigned long time;
  244. reg = __raw_readl(MISC_PCIE_CTRL(port));
  245. /*
  246. * Enable Application Request to 1, it will exit L1 automatically,
  247. * but when chip back, it will use another clock, still can use 0x1.
  248. */
  249. reg |= 0x3;
  250. __raw_writel(reg, MISC_PCIE_CTRL(port));
  251. pr_info("PCIe: Port[%d] Enable PCIe LTSSM\n", port);
  252. pr_info("PCIe: Port[%d] Check data link layer...", port);
  253. time = jiffies;
  254. while (1) {
  255. reg = __raw_readl(MISC_PCIE_PM_DEBUG(port));
  256. if (reg & 0x1) {
  257. pr_info("Link up.\n");
  258. cnspci->linked = 1;
  259. break;
  260. } else if (time_after(jiffies, time + 50)) {
  261. pr_info("Device not found.\n");
  262. break;
  263. }
  264. }
  265. }
  266. static void __init cns3xxx_pcie_hw_init(struct cns3xxx_pcie *cnspci)
  267. {
  268. int port = cnspci->hw_pci.domain;
  269. struct pci_sys_data sd = {
  270. .domain = port,
  271. };
  272. struct pci_bus bus = {
  273. .number = 0,
  274. .ops = &cns3xxx_pcie_ops,
  275. .sysdata = &sd,
  276. };
  277. u32 io_base = cnspci->res_io.start >> 16;
  278. u32 mem_base = cnspci->res_mem.start >> 16;
  279. u32 host_base = cnspci->cfg_bases[CNS3XXX_HOST_TYPE].pfn;
  280. u32 cfg0_base = cnspci->cfg_bases[CNS3XXX_CFG0_TYPE].pfn;
  281. u32 devfn = 0;
  282. u8 tmp8;
  283. u16 pos;
  284. u16 dc;
  285. host_base = (__pfn_to_phys(host_base) - 1) >> 16;
  286. cfg0_base = (__pfn_to_phys(cfg0_base) - 1) >> 16;
  287. pci_bus_write_config_byte(&bus, devfn, PCI_PRIMARY_BUS, 0);
  288. pci_bus_write_config_byte(&bus, devfn, PCI_SECONDARY_BUS, 1);
  289. pci_bus_write_config_byte(&bus, devfn, PCI_SUBORDINATE_BUS, 1);
  290. pci_bus_read_config_byte(&bus, devfn, PCI_PRIMARY_BUS, &tmp8);
  291. pci_bus_read_config_byte(&bus, devfn, PCI_SECONDARY_BUS, &tmp8);
  292. pci_bus_read_config_byte(&bus, devfn, PCI_SUBORDINATE_BUS, &tmp8);
  293. pci_bus_write_config_word(&bus, devfn, PCI_MEMORY_BASE, mem_base);
  294. pci_bus_write_config_word(&bus, devfn, PCI_MEMORY_LIMIT, host_base);
  295. pci_bus_write_config_word(&bus, devfn, PCI_IO_BASE_UPPER16, io_base);
  296. pci_bus_write_config_word(&bus, devfn, PCI_IO_LIMIT_UPPER16, cfg0_base);
  297. if (!cnspci->linked)
  298. return;
  299. /* Set Device Max_Read_Request_Size to 128 byte */
  300. devfn = PCI_DEVFN(1, 0);
  301. pos = pci_bus_find_capability(&bus, devfn, PCI_CAP_ID_EXP);
  302. pci_bus_read_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, &dc);
  303. dc &= ~(0x3 << 12); /* Clear Device Control Register [14:12] */
  304. pci_bus_write_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, dc);
  305. pci_bus_read_config_word(&bus, devfn, pos + PCI_EXP_DEVCTL, &dc);
  306. if (!(dc & (0x3 << 12)))
  307. pr_info("PCIe: Set Device Max_Read_Request_Size to 128 byte\n");
  308. /* Disable PCIe0 Interrupt Mask INTA to INTD */
  309. __raw_writel(~0x3FFF, MISC_PCIE_INT_MASK(port));
  310. }
  311. static int cns3xxx_pcie_abort_handler(unsigned long addr, unsigned int fsr,
  312. struct pt_regs *regs)
  313. {
  314. if (fsr & (1 << 10))
  315. regs->ARM_pc += 4;
  316. return 0;
  317. }
  318. static int __init cns3xxx_pcie_init(void)
  319. {
  320. int i;
  321. hook_fault_code(16 + 6, cns3xxx_pcie_abort_handler, SIGBUS, 0,
  322. "imprecise external abort");
  323. for (i = 0; i < ARRAY_SIZE(cns3xxx_pcie); i++) {
  324. iotable_init(cns3xxx_pcie[i].cfg_bases,
  325. ARRAY_SIZE(cns3xxx_pcie[i].cfg_bases));
  326. cns3xxx_pwr_clk_en(0x1 << PM_CLK_GATE_REG_OFFSET_PCIE(i));
  327. cns3xxx_pwr_soft_rst(0x1 << PM_SOFT_RST_REG_OFFST_PCIE(i));
  328. cns3xxx_pcie_check_link(&cns3xxx_pcie[i]);
  329. cns3xxx_pcie_hw_init(&cns3xxx_pcie[i]);
  330. pci_common_init(&cns3xxx_pcie[i].hw_pci);
  331. }
  332. pci_assign_unassigned_resources();
  333. return 0;
  334. }
  335. device_initcall(cns3xxx_pcie_init);