pci-xgene.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /**
  2. * APM X-Gene PCIe Driver
  3. *
  4. * Copyright (c) 2014 Applied Micro Circuits Corporation.
  5. *
  6. * Author: Tanmay Inamdar <tinamdar@apm.com>.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/io.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/memblock.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/of_address.h>
  27. #include <linux/of_irq.h>
  28. #include <linux/of_pci.h>
  29. #include <linux/pci.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/slab.h>
  32. #define PCIECORE_CTLANDSTATUS 0x50
  33. #define PIM1_1L 0x80
  34. #define IBAR2 0x98
  35. #define IR2MSK 0x9c
  36. #define PIM2_1L 0xa0
  37. #define IBAR3L 0xb4
  38. #define IR3MSKL 0xbc
  39. #define PIM3_1L 0xc4
  40. #define OMR1BARL 0x100
  41. #define OMR2BARL 0x118
  42. #define OMR3BARL 0x130
  43. #define CFGBARL 0x154
  44. #define CFGBARH 0x158
  45. #define CFGCTL 0x15c
  46. #define RTDID 0x160
  47. #define BRIDGE_CFG_0 0x2000
  48. #define BRIDGE_CFG_4 0x2010
  49. #define BRIDGE_STATUS_0 0x2600
  50. #define LINK_UP_MASK 0x00000100
  51. #define AXI_EP_CFG_ACCESS 0x10000
  52. #define EN_COHERENCY 0xF0000000
  53. #define EN_REG 0x00000001
  54. #define OB_LO_IO 0x00000002
  55. #define XGENE_PCIE_VENDORID 0x10E8
  56. #define XGENE_PCIE_DEVICEID 0xE004
  57. #define SZ_1T (SZ_1G*1024ULL)
  58. #define PIPE_PHY_RATE_RD(src) ((0xc000 & (u32)(src)) >> 0xe)
  59. struct xgene_pcie_port {
  60. struct device_node *node;
  61. struct device *dev;
  62. struct clk *clk;
  63. void __iomem *csr_base;
  64. void __iomem *cfg_base;
  65. unsigned long cfg_addr;
  66. bool link_up;
  67. };
  68. static inline u32 pcie_bar_low_val(u32 addr, u32 flags)
  69. {
  70. return (addr & PCI_BASE_ADDRESS_MEM_MASK) | flags;
  71. }
  72. /*
  73. * When the address bit [17:16] is 2'b01, the Configuration access will be
  74. * treated as Type 1 and it will be forwarded to external PCIe device.
  75. */
  76. static void __iomem *xgene_pcie_get_cfg_base(struct pci_bus *bus)
  77. {
  78. struct xgene_pcie_port *port = bus->sysdata;
  79. if (bus->number >= (bus->primary + 1))
  80. return port->cfg_base + AXI_EP_CFG_ACCESS;
  81. return port->cfg_base;
  82. }
  83. /*
  84. * For Configuration request, RTDID register is used as Bus Number,
  85. * Device Number and Function number of the header fields.
  86. */
  87. static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
  88. {
  89. struct xgene_pcie_port *port = bus->sysdata;
  90. unsigned int b, d, f;
  91. u32 rtdid_val = 0;
  92. b = bus->number;
  93. d = PCI_SLOT(devfn);
  94. f = PCI_FUNC(devfn);
  95. if (!pci_is_root_bus(bus))
  96. rtdid_val = (b << 8) | (d << 3) | f;
  97. writel(rtdid_val, port->csr_base + RTDID);
  98. /* read the register back to ensure flush */
  99. readl(port->csr_base + RTDID);
  100. }
  101. /*
  102. * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
  103. * the translation from PCI bus to native BUS. Entire DDR region
  104. * is mapped into PCIe space using these registers, so it can be
  105. * reached by DMA from EP devices. The BAR0/1 of bridge should be
  106. * hidden during enumeration to avoid the sizing and resource allocation
  107. * by PCIe core.
  108. */
  109. static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
  110. {
  111. if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
  112. (offset == PCI_BASE_ADDRESS_1)))
  113. return true;
  114. return false;
  115. }
  116. static void __iomem *xgene_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
  117. int offset)
  118. {
  119. struct xgene_pcie_port *port = bus->sysdata;
  120. if ((pci_is_root_bus(bus) && devfn != 0) || !port->link_up ||
  121. xgene_pcie_hide_rc_bars(bus, offset))
  122. return NULL;
  123. xgene_pcie_set_rtdid_reg(bus, devfn);
  124. return xgene_pcie_get_cfg_base(bus) + offset;
  125. }
  126. static struct pci_ops xgene_pcie_ops = {
  127. .map_bus = xgene_pcie_map_bus,
  128. .read = pci_generic_config_read32,
  129. .write = pci_generic_config_write32,
  130. };
  131. static u64 xgene_pcie_set_ib_mask(void __iomem *csr_base, u32 addr,
  132. u32 flags, u64 size)
  133. {
  134. u64 mask = (~(size - 1) & PCI_BASE_ADDRESS_MEM_MASK) | flags;
  135. u32 val32 = 0;
  136. u32 val;
  137. val32 = readl(csr_base + addr);
  138. val = (val32 & 0x0000ffff) | (lower_32_bits(mask) << 16);
  139. writel(val, csr_base + addr);
  140. val32 = readl(csr_base + addr + 0x04);
  141. val = (val32 & 0xffff0000) | (lower_32_bits(mask) >> 16);
  142. writel(val, csr_base + addr + 0x04);
  143. val32 = readl(csr_base + addr + 0x04);
  144. val = (val32 & 0x0000ffff) | (upper_32_bits(mask) << 16);
  145. writel(val, csr_base + addr + 0x04);
  146. val32 = readl(csr_base + addr + 0x08);
  147. val = (val32 & 0xffff0000) | (upper_32_bits(mask) >> 16);
  148. writel(val, csr_base + addr + 0x08);
  149. return mask;
  150. }
  151. static void xgene_pcie_linkup(struct xgene_pcie_port *port,
  152. u32 *lanes, u32 *speed)
  153. {
  154. void __iomem *csr_base = port->csr_base;
  155. u32 val32;
  156. port->link_up = false;
  157. val32 = readl(csr_base + PCIECORE_CTLANDSTATUS);
  158. if (val32 & LINK_UP_MASK) {
  159. port->link_up = true;
  160. *speed = PIPE_PHY_RATE_RD(val32);
  161. val32 = readl(csr_base + BRIDGE_STATUS_0);
  162. *lanes = val32 >> 26;
  163. }
  164. }
  165. static int xgene_pcie_init_port(struct xgene_pcie_port *port)
  166. {
  167. int rc;
  168. port->clk = clk_get(port->dev, NULL);
  169. if (IS_ERR(port->clk)) {
  170. dev_err(port->dev, "clock not available\n");
  171. return -ENODEV;
  172. }
  173. rc = clk_prepare_enable(port->clk);
  174. if (rc) {
  175. dev_err(port->dev, "clock enable failed\n");
  176. return rc;
  177. }
  178. return 0;
  179. }
  180. static int xgene_pcie_map_reg(struct xgene_pcie_port *port,
  181. struct platform_device *pdev)
  182. {
  183. struct resource *res;
  184. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr");
  185. port->csr_base = devm_ioremap_resource(port->dev, res);
  186. if (IS_ERR(port->csr_base))
  187. return PTR_ERR(port->csr_base);
  188. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  189. port->cfg_base = devm_ioremap_resource(port->dev, res);
  190. if (IS_ERR(port->cfg_base))
  191. return PTR_ERR(port->cfg_base);
  192. port->cfg_addr = res->start;
  193. return 0;
  194. }
  195. static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port *port,
  196. struct resource *res, u32 offset,
  197. u64 cpu_addr, u64 pci_addr)
  198. {
  199. void __iomem *base = port->csr_base + offset;
  200. resource_size_t size = resource_size(res);
  201. u64 restype = resource_type(res);
  202. u64 mask = 0;
  203. u32 min_size;
  204. u32 flag = EN_REG;
  205. if (restype == IORESOURCE_MEM) {
  206. min_size = SZ_128M;
  207. } else {
  208. min_size = 128;
  209. flag |= OB_LO_IO;
  210. }
  211. if (size >= min_size)
  212. mask = ~(size - 1) | flag;
  213. else
  214. dev_warn(port->dev, "res size 0x%llx less than minimum 0x%x\n",
  215. (u64)size, min_size);
  216. writel(lower_32_bits(cpu_addr), base);
  217. writel(upper_32_bits(cpu_addr), base + 0x04);
  218. writel(lower_32_bits(mask), base + 0x08);
  219. writel(upper_32_bits(mask), base + 0x0c);
  220. writel(lower_32_bits(pci_addr), base + 0x10);
  221. writel(upper_32_bits(pci_addr), base + 0x14);
  222. }
  223. static void xgene_pcie_setup_cfg_reg(void __iomem *csr_base, u64 addr)
  224. {
  225. writel(lower_32_bits(addr), csr_base + CFGBARL);
  226. writel(upper_32_bits(addr), csr_base + CFGBARH);
  227. writel(EN_REG, csr_base + CFGCTL);
  228. }
  229. static int xgene_pcie_map_ranges(struct xgene_pcie_port *port,
  230. struct list_head *res,
  231. resource_size_t io_base)
  232. {
  233. struct resource_entry *window;
  234. struct device *dev = port->dev;
  235. int ret;
  236. resource_list_for_each_entry(window, res) {
  237. struct resource *res = window->res;
  238. u64 restype = resource_type(res);
  239. dev_dbg(port->dev, "%pR\n", res);
  240. switch (restype) {
  241. case IORESOURCE_IO:
  242. xgene_pcie_setup_ob_reg(port, res, OMR3BARL, io_base,
  243. res->start - window->offset);
  244. ret = pci_remap_iospace(res, io_base);
  245. if (ret < 0)
  246. return ret;
  247. break;
  248. case IORESOURCE_MEM:
  249. xgene_pcie_setup_ob_reg(port, res, OMR1BARL, res->start,
  250. res->start - window->offset);
  251. break;
  252. case IORESOURCE_BUS:
  253. break;
  254. default:
  255. dev_err(dev, "invalid resource %pR\n", res);
  256. return -EINVAL;
  257. }
  258. }
  259. xgene_pcie_setup_cfg_reg(port->csr_base, port->cfg_addr);
  260. return 0;
  261. }
  262. static void xgene_pcie_setup_pims(void *addr, u64 pim, u64 size)
  263. {
  264. writel(lower_32_bits(pim), addr);
  265. writel(upper_32_bits(pim) | EN_COHERENCY, addr + 0x04);
  266. writel(lower_32_bits(size), addr + 0x10);
  267. writel(upper_32_bits(size), addr + 0x14);
  268. }
  269. /*
  270. * X-Gene PCIe support maximum 3 inbound memory regions
  271. * This function helps to select a region based on size of region
  272. */
  273. static int xgene_pcie_select_ib_reg(u8 *ib_reg_mask, u64 size)
  274. {
  275. if ((size > 4) && (size < SZ_16M) && !(*ib_reg_mask & (1 << 1))) {
  276. *ib_reg_mask |= (1 << 1);
  277. return 1;
  278. }
  279. if ((size > SZ_1K) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 0))) {
  280. *ib_reg_mask |= (1 << 0);
  281. return 0;
  282. }
  283. if ((size > SZ_1M) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 2))) {
  284. *ib_reg_mask |= (1 << 2);
  285. return 2;
  286. }
  287. return -EINVAL;
  288. }
  289. static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port *port,
  290. struct of_pci_range *range, u8 *ib_reg_mask)
  291. {
  292. void __iomem *csr_base = port->csr_base;
  293. void __iomem *cfg_base = port->cfg_base;
  294. void *bar_addr;
  295. void *pim_addr;
  296. u64 cpu_addr = range->cpu_addr;
  297. u64 pci_addr = range->pci_addr;
  298. u64 size = range->size;
  299. u64 mask = ~(size - 1) | EN_REG;
  300. u32 flags = PCI_BASE_ADDRESS_MEM_TYPE_64;
  301. u32 bar_low;
  302. int region;
  303. region = xgene_pcie_select_ib_reg(ib_reg_mask, range->size);
  304. if (region < 0) {
  305. dev_warn(port->dev, "invalid pcie dma-range config\n");
  306. return;
  307. }
  308. if (range->flags & IORESOURCE_PREFETCH)
  309. flags |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  310. bar_low = pcie_bar_low_val((u32)cpu_addr, flags);
  311. switch (region) {
  312. case 0:
  313. xgene_pcie_set_ib_mask(csr_base, BRIDGE_CFG_4, flags, size);
  314. bar_addr = cfg_base + PCI_BASE_ADDRESS_0;
  315. writel(bar_low, bar_addr);
  316. writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
  317. pim_addr = csr_base + PIM1_1L;
  318. break;
  319. case 1:
  320. bar_addr = csr_base + IBAR2;
  321. writel(bar_low, bar_addr);
  322. writel(lower_32_bits(mask), csr_base + IR2MSK);
  323. pim_addr = csr_base + PIM2_1L;
  324. break;
  325. case 2:
  326. bar_addr = csr_base + IBAR3L;
  327. writel(bar_low, bar_addr);
  328. writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
  329. writel(lower_32_bits(mask), csr_base + IR3MSKL);
  330. writel(upper_32_bits(mask), csr_base + IR3MSKL + 0x4);
  331. pim_addr = csr_base + PIM3_1L;
  332. break;
  333. }
  334. xgene_pcie_setup_pims(pim_addr, pci_addr, ~(size - 1));
  335. }
  336. static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  337. struct device_node *node)
  338. {
  339. const int na = 3, ns = 2;
  340. int rlen;
  341. parser->node = node;
  342. parser->pna = of_n_addr_cells(node);
  343. parser->np = parser->pna + na + ns;
  344. parser->range = of_get_property(node, "dma-ranges", &rlen);
  345. if (!parser->range)
  346. return -ENOENT;
  347. parser->end = parser->range + rlen / sizeof(__be32);
  348. return 0;
  349. }
  350. static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port *port)
  351. {
  352. struct device_node *np = port->node;
  353. struct of_pci_range range;
  354. struct of_pci_range_parser parser;
  355. struct device *dev = port->dev;
  356. u8 ib_reg_mask = 0;
  357. if (pci_dma_range_parser_init(&parser, np)) {
  358. dev_err(dev, "missing dma-ranges property\n");
  359. return -EINVAL;
  360. }
  361. /* Get the dma-ranges from DT */
  362. for_each_of_pci_range(&parser, &range) {
  363. u64 end = range.cpu_addr + range.size - 1;
  364. dev_dbg(port->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
  365. range.flags, range.cpu_addr, end, range.pci_addr);
  366. xgene_pcie_setup_ib_reg(port, &range, &ib_reg_mask);
  367. }
  368. return 0;
  369. }
  370. /* clear BAR configuration which was done by firmware */
  371. static void xgene_pcie_clear_config(struct xgene_pcie_port *port)
  372. {
  373. int i;
  374. for (i = PIM1_1L; i <= CFGCTL; i += 4)
  375. writel(0x0, port->csr_base + i);
  376. }
  377. static int xgene_pcie_setup(struct xgene_pcie_port *port,
  378. struct list_head *res,
  379. resource_size_t io_base)
  380. {
  381. u32 val, lanes = 0, speed = 0;
  382. int ret;
  383. xgene_pcie_clear_config(port);
  384. /* setup the vendor and device IDs correctly */
  385. val = (XGENE_PCIE_DEVICEID << 16) | XGENE_PCIE_VENDORID;
  386. writel(val, port->csr_base + BRIDGE_CFG_0);
  387. ret = xgene_pcie_map_ranges(port, res, io_base);
  388. if (ret)
  389. return ret;
  390. ret = xgene_pcie_parse_map_dma_ranges(port);
  391. if (ret)
  392. return ret;
  393. xgene_pcie_linkup(port, &lanes, &speed);
  394. if (!port->link_up)
  395. dev_info(port->dev, "(rc) link down\n");
  396. else
  397. dev_info(port->dev, "(rc) x%d gen-%d link up\n",
  398. lanes, speed + 1);
  399. return 0;
  400. }
  401. static int xgene_pcie_probe_bridge(struct platform_device *pdev)
  402. {
  403. struct device_node *dn = pdev->dev.of_node;
  404. struct xgene_pcie_port *port;
  405. resource_size_t iobase = 0;
  406. struct pci_bus *bus;
  407. int ret;
  408. LIST_HEAD(res);
  409. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  410. if (!port)
  411. return -ENOMEM;
  412. port->node = of_node_get(pdev->dev.of_node);
  413. port->dev = &pdev->dev;
  414. ret = xgene_pcie_map_reg(port, pdev);
  415. if (ret)
  416. return ret;
  417. ret = xgene_pcie_init_port(port);
  418. if (ret)
  419. return ret;
  420. ret = of_pci_get_host_bridge_resources(dn, 0, 0xff, &res, &iobase);
  421. if (ret)
  422. return ret;
  423. ret = xgene_pcie_setup(port, &res, iobase);
  424. if (ret)
  425. return ret;
  426. bus = pci_create_root_bus(&pdev->dev, 0,
  427. &xgene_pcie_ops, port, &res);
  428. if (!bus)
  429. return -ENOMEM;
  430. pci_scan_child_bus(bus);
  431. pci_assign_unassigned_bus_resources(bus);
  432. pci_bus_add_devices(bus);
  433. platform_set_drvdata(pdev, port);
  434. return 0;
  435. }
  436. static const struct of_device_id xgene_pcie_match_table[] = {
  437. {.compatible = "apm,xgene-pcie",},
  438. {},
  439. };
  440. static struct platform_driver xgene_pcie_driver = {
  441. .driver = {
  442. .name = "xgene-pcie",
  443. .of_match_table = of_match_ptr(xgene_pcie_match_table),
  444. },
  445. .probe = xgene_pcie_probe_bridge,
  446. };
  447. module_platform_driver(xgene_pcie_driver);
  448. MODULE_AUTHOR("Tanmay Inamdar <tinamdar@apm.com>");
  449. MODULE_DESCRIPTION("APM X-Gene PCIe driver");
  450. MODULE_LICENSE("GPL v2");