pci-xgene.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. #define ROOT_CAP_AND_CTRL 0x5C
  60. /* PCIe IP version */
  61. #define XGENE_PCIE_IP_VER_UNKN 0
  62. #define XGENE_PCIE_IP_VER_1 1
  63. struct xgene_pcie_port {
  64. struct device_node *node;
  65. struct device *dev;
  66. struct clk *clk;
  67. void __iomem *csr_base;
  68. void __iomem *cfg_base;
  69. unsigned long cfg_addr;
  70. bool link_up;
  71. u32 version;
  72. };
  73. static inline u32 pcie_bar_low_val(u32 addr, u32 flags)
  74. {
  75. return (addr & PCI_BASE_ADDRESS_MEM_MASK) | flags;
  76. }
  77. /*
  78. * When the address bit [17:16] is 2'b01, the Configuration access will be
  79. * treated as Type 1 and it will be forwarded to external PCIe device.
  80. */
  81. static void __iomem *xgene_pcie_get_cfg_base(struct pci_bus *bus)
  82. {
  83. struct xgene_pcie_port *port = bus->sysdata;
  84. if (bus->number >= (bus->primary + 1))
  85. return port->cfg_base + AXI_EP_CFG_ACCESS;
  86. return port->cfg_base;
  87. }
  88. /*
  89. * For Configuration request, RTDID register is used as Bus Number,
  90. * Device Number and Function number of the header fields.
  91. */
  92. static void xgene_pcie_set_rtdid_reg(struct pci_bus *bus, uint devfn)
  93. {
  94. struct xgene_pcie_port *port = bus->sysdata;
  95. unsigned int b, d, f;
  96. u32 rtdid_val = 0;
  97. b = bus->number;
  98. d = PCI_SLOT(devfn);
  99. f = PCI_FUNC(devfn);
  100. if (!pci_is_root_bus(bus))
  101. rtdid_val = (b << 8) | (d << 3) | f;
  102. writel(rtdid_val, port->csr_base + RTDID);
  103. /* read the register back to ensure flush */
  104. readl(port->csr_base + RTDID);
  105. }
  106. /*
  107. * X-Gene PCIe port uses BAR0-BAR1 of RC's configuration space as
  108. * the translation from PCI bus to native BUS. Entire DDR region
  109. * is mapped into PCIe space using these registers, so it can be
  110. * reached by DMA from EP devices. The BAR0/1 of bridge should be
  111. * hidden during enumeration to avoid the sizing and resource allocation
  112. * by PCIe core.
  113. */
  114. static bool xgene_pcie_hide_rc_bars(struct pci_bus *bus, int offset)
  115. {
  116. if (pci_is_root_bus(bus) && ((offset == PCI_BASE_ADDRESS_0) ||
  117. (offset == PCI_BASE_ADDRESS_1)))
  118. return true;
  119. return false;
  120. }
  121. static void __iomem *xgene_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
  122. int offset)
  123. {
  124. if ((pci_is_root_bus(bus) && devfn != 0) ||
  125. xgene_pcie_hide_rc_bars(bus, offset))
  126. return NULL;
  127. xgene_pcie_set_rtdid_reg(bus, devfn);
  128. return xgene_pcie_get_cfg_base(bus) + offset;
  129. }
  130. static int xgene_pcie_config_read32(struct pci_bus *bus, unsigned int devfn,
  131. int where, int size, u32 *val)
  132. {
  133. struct xgene_pcie_port *port = bus->sysdata;
  134. if (pci_generic_config_read32(bus, devfn, where & ~0x3, 4, val) !=
  135. PCIBIOS_SUCCESSFUL)
  136. return PCIBIOS_DEVICE_NOT_FOUND;
  137. /*
  138. * The v1 controller has a bug in its Configuration Request
  139. * Retry Status (CRS) logic: when CRS is enabled and we read the
  140. * Vendor and Device ID of a non-existent device, the controller
  141. * fabricates return data of 0xFFFF0001 ("device exists but is not
  142. * ready") instead of 0xFFFFFFFF ("device does not exist"). This
  143. * causes the PCI core to retry the read until it times out.
  144. * Avoid this by not claiming to support CRS.
  145. */
  146. if (pci_is_root_bus(bus) && (port->version == XGENE_PCIE_IP_VER_1) &&
  147. ((where & ~0x3) == ROOT_CAP_AND_CTRL))
  148. *val &= ~(PCI_EXP_RTCAP_CRSVIS << 16);
  149. if (size <= 2)
  150. *val = (*val >> (8 * (where & 3))) & ((1 << (size * 8)) - 1);
  151. return PCIBIOS_SUCCESSFUL;
  152. }
  153. static struct pci_ops xgene_pcie_ops = {
  154. .map_bus = xgene_pcie_map_bus,
  155. .read = xgene_pcie_config_read32,
  156. .write = pci_generic_config_write32,
  157. };
  158. static u64 xgene_pcie_set_ib_mask(void __iomem *csr_base, u32 addr,
  159. u32 flags, u64 size)
  160. {
  161. u64 mask = (~(size - 1) & PCI_BASE_ADDRESS_MEM_MASK) | flags;
  162. u32 val32 = 0;
  163. u32 val;
  164. val32 = readl(csr_base + addr);
  165. val = (val32 & 0x0000ffff) | (lower_32_bits(mask) << 16);
  166. writel(val, csr_base + addr);
  167. val32 = readl(csr_base + addr + 0x04);
  168. val = (val32 & 0xffff0000) | (lower_32_bits(mask) >> 16);
  169. writel(val, csr_base + addr + 0x04);
  170. val32 = readl(csr_base + addr + 0x04);
  171. val = (val32 & 0x0000ffff) | (upper_32_bits(mask) << 16);
  172. writel(val, csr_base + addr + 0x04);
  173. val32 = readl(csr_base + addr + 0x08);
  174. val = (val32 & 0xffff0000) | (upper_32_bits(mask) >> 16);
  175. writel(val, csr_base + addr + 0x08);
  176. return mask;
  177. }
  178. static void xgene_pcie_linkup(struct xgene_pcie_port *port,
  179. u32 *lanes, u32 *speed)
  180. {
  181. void __iomem *csr_base = port->csr_base;
  182. u32 val32;
  183. port->link_up = false;
  184. val32 = readl(csr_base + PCIECORE_CTLANDSTATUS);
  185. if (val32 & LINK_UP_MASK) {
  186. port->link_up = true;
  187. *speed = PIPE_PHY_RATE_RD(val32);
  188. val32 = readl(csr_base + BRIDGE_STATUS_0);
  189. *lanes = val32 >> 26;
  190. }
  191. }
  192. static int xgene_pcie_init_port(struct xgene_pcie_port *port)
  193. {
  194. int rc;
  195. port->clk = clk_get(port->dev, NULL);
  196. if (IS_ERR(port->clk)) {
  197. dev_err(port->dev, "clock not available\n");
  198. return -ENODEV;
  199. }
  200. rc = clk_prepare_enable(port->clk);
  201. if (rc) {
  202. dev_err(port->dev, "clock enable failed\n");
  203. return rc;
  204. }
  205. return 0;
  206. }
  207. static int xgene_pcie_map_reg(struct xgene_pcie_port *port,
  208. struct platform_device *pdev)
  209. {
  210. struct resource *res;
  211. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr");
  212. port->csr_base = devm_ioremap_resource(port->dev, res);
  213. if (IS_ERR(port->csr_base))
  214. return PTR_ERR(port->csr_base);
  215. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
  216. port->cfg_base = devm_ioremap_resource(port->dev, res);
  217. if (IS_ERR(port->cfg_base))
  218. return PTR_ERR(port->cfg_base);
  219. port->cfg_addr = res->start;
  220. return 0;
  221. }
  222. static void xgene_pcie_setup_ob_reg(struct xgene_pcie_port *port,
  223. struct resource *res, u32 offset,
  224. u64 cpu_addr, u64 pci_addr)
  225. {
  226. void __iomem *base = port->csr_base + offset;
  227. resource_size_t size = resource_size(res);
  228. u64 restype = resource_type(res);
  229. u64 mask = 0;
  230. u32 min_size;
  231. u32 flag = EN_REG;
  232. if (restype == IORESOURCE_MEM) {
  233. min_size = SZ_128M;
  234. } else {
  235. min_size = 128;
  236. flag |= OB_LO_IO;
  237. }
  238. if (size >= min_size)
  239. mask = ~(size - 1) | flag;
  240. else
  241. dev_warn(port->dev, "res size 0x%llx less than minimum 0x%x\n",
  242. (u64)size, min_size);
  243. writel(lower_32_bits(cpu_addr), base);
  244. writel(upper_32_bits(cpu_addr), base + 0x04);
  245. writel(lower_32_bits(mask), base + 0x08);
  246. writel(upper_32_bits(mask), base + 0x0c);
  247. writel(lower_32_bits(pci_addr), base + 0x10);
  248. writel(upper_32_bits(pci_addr), base + 0x14);
  249. }
  250. static void xgene_pcie_setup_cfg_reg(void __iomem *csr_base, u64 addr)
  251. {
  252. writel(lower_32_bits(addr), csr_base + CFGBARL);
  253. writel(upper_32_bits(addr), csr_base + CFGBARH);
  254. writel(EN_REG, csr_base + CFGCTL);
  255. }
  256. static int xgene_pcie_map_ranges(struct xgene_pcie_port *port,
  257. struct list_head *res,
  258. resource_size_t io_base)
  259. {
  260. struct resource_entry *window;
  261. struct device *dev = port->dev;
  262. int ret;
  263. resource_list_for_each_entry(window, res) {
  264. struct resource *res = window->res;
  265. u64 restype = resource_type(res);
  266. dev_dbg(port->dev, "%pR\n", res);
  267. switch (restype) {
  268. case IORESOURCE_IO:
  269. xgene_pcie_setup_ob_reg(port, res, OMR3BARL, io_base,
  270. res->start - window->offset);
  271. ret = pci_remap_iospace(res, io_base);
  272. if (ret < 0)
  273. return ret;
  274. break;
  275. case IORESOURCE_MEM:
  276. if (res->flags & IORESOURCE_PREFETCH)
  277. xgene_pcie_setup_ob_reg(port, res, OMR2BARL,
  278. res->start,
  279. res->start -
  280. window->offset);
  281. else
  282. xgene_pcie_setup_ob_reg(port, res, OMR1BARL,
  283. res->start,
  284. res->start -
  285. window->offset);
  286. break;
  287. case IORESOURCE_BUS:
  288. break;
  289. default:
  290. dev_err(dev, "invalid resource %pR\n", res);
  291. return -EINVAL;
  292. }
  293. }
  294. xgene_pcie_setup_cfg_reg(port->csr_base, port->cfg_addr);
  295. return 0;
  296. }
  297. static void xgene_pcie_setup_pims(void *addr, u64 pim, u64 size)
  298. {
  299. writel(lower_32_bits(pim), addr);
  300. writel(upper_32_bits(pim) | EN_COHERENCY, addr + 0x04);
  301. writel(lower_32_bits(size), addr + 0x10);
  302. writel(upper_32_bits(size), addr + 0x14);
  303. }
  304. /*
  305. * X-Gene PCIe support maximum 3 inbound memory regions
  306. * This function helps to select a region based on size of region
  307. */
  308. static int xgene_pcie_select_ib_reg(u8 *ib_reg_mask, u64 size)
  309. {
  310. if ((size > 4) && (size < SZ_16M) && !(*ib_reg_mask & (1 << 1))) {
  311. *ib_reg_mask |= (1 << 1);
  312. return 1;
  313. }
  314. if ((size > SZ_1K) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 0))) {
  315. *ib_reg_mask |= (1 << 0);
  316. return 0;
  317. }
  318. if ((size > SZ_1M) && (size < SZ_1T) && !(*ib_reg_mask & (1 << 2))) {
  319. *ib_reg_mask |= (1 << 2);
  320. return 2;
  321. }
  322. return -EINVAL;
  323. }
  324. static void xgene_pcie_setup_ib_reg(struct xgene_pcie_port *port,
  325. struct of_pci_range *range, u8 *ib_reg_mask)
  326. {
  327. void __iomem *csr_base = port->csr_base;
  328. void __iomem *cfg_base = port->cfg_base;
  329. void *bar_addr;
  330. void *pim_addr;
  331. u64 cpu_addr = range->cpu_addr;
  332. u64 pci_addr = range->pci_addr;
  333. u64 size = range->size;
  334. u64 mask = ~(size - 1) | EN_REG;
  335. u32 flags = PCI_BASE_ADDRESS_MEM_TYPE_64;
  336. u32 bar_low;
  337. int region;
  338. region = xgene_pcie_select_ib_reg(ib_reg_mask, range->size);
  339. if (region < 0) {
  340. dev_warn(port->dev, "invalid pcie dma-range config\n");
  341. return;
  342. }
  343. if (range->flags & IORESOURCE_PREFETCH)
  344. flags |= PCI_BASE_ADDRESS_MEM_PREFETCH;
  345. bar_low = pcie_bar_low_val((u32)cpu_addr, flags);
  346. switch (region) {
  347. case 0:
  348. xgene_pcie_set_ib_mask(csr_base, BRIDGE_CFG_4, flags, size);
  349. bar_addr = cfg_base + PCI_BASE_ADDRESS_0;
  350. writel(bar_low, bar_addr);
  351. writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
  352. pim_addr = csr_base + PIM1_1L;
  353. break;
  354. case 1:
  355. bar_addr = csr_base + IBAR2;
  356. writel(bar_low, bar_addr);
  357. writel(lower_32_bits(mask), csr_base + IR2MSK);
  358. pim_addr = csr_base + PIM2_1L;
  359. break;
  360. case 2:
  361. bar_addr = csr_base + IBAR3L;
  362. writel(bar_low, bar_addr);
  363. writel(upper_32_bits(cpu_addr), bar_addr + 0x4);
  364. writel(lower_32_bits(mask), csr_base + IR3MSKL);
  365. writel(upper_32_bits(mask), csr_base + IR3MSKL + 0x4);
  366. pim_addr = csr_base + PIM3_1L;
  367. break;
  368. }
  369. xgene_pcie_setup_pims(pim_addr, pci_addr, ~(size - 1));
  370. }
  371. static int pci_dma_range_parser_init(struct of_pci_range_parser *parser,
  372. struct device_node *node)
  373. {
  374. const int na = 3, ns = 2;
  375. int rlen;
  376. parser->node = node;
  377. parser->pna = of_n_addr_cells(node);
  378. parser->np = parser->pna + na + ns;
  379. parser->range = of_get_property(node, "dma-ranges", &rlen);
  380. if (!parser->range)
  381. return -ENOENT;
  382. parser->end = parser->range + rlen / sizeof(__be32);
  383. return 0;
  384. }
  385. static int xgene_pcie_parse_map_dma_ranges(struct xgene_pcie_port *port)
  386. {
  387. struct device_node *np = port->node;
  388. struct of_pci_range range;
  389. struct of_pci_range_parser parser;
  390. struct device *dev = port->dev;
  391. u8 ib_reg_mask = 0;
  392. if (pci_dma_range_parser_init(&parser, np)) {
  393. dev_err(dev, "missing dma-ranges property\n");
  394. return -EINVAL;
  395. }
  396. /* Get the dma-ranges from DT */
  397. for_each_of_pci_range(&parser, &range) {
  398. u64 end = range.cpu_addr + range.size - 1;
  399. dev_dbg(port->dev, "0x%08x 0x%016llx..0x%016llx -> 0x%016llx\n",
  400. range.flags, range.cpu_addr, end, range.pci_addr);
  401. xgene_pcie_setup_ib_reg(port, &range, &ib_reg_mask);
  402. }
  403. return 0;
  404. }
  405. /* clear BAR configuration which was done by firmware */
  406. static void xgene_pcie_clear_config(struct xgene_pcie_port *port)
  407. {
  408. int i;
  409. for (i = PIM1_1L; i <= CFGCTL; i += 4)
  410. writel(0x0, port->csr_base + i);
  411. }
  412. static int xgene_pcie_setup(struct xgene_pcie_port *port,
  413. struct list_head *res,
  414. resource_size_t io_base)
  415. {
  416. u32 val, lanes = 0, speed = 0;
  417. int ret;
  418. xgene_pcie_clear_config(port);
  419. /* setup the vendor and device IDs correctly */
  420. val = (XGENE_PCIE_DEVICEID << 16) | XGENE_PCIE_VENDORID;
  421. writel(val, port->csr_base + BRIDGE_CFG_0);
  422. ret = xgene_pcie_map_ranges(port, res, io_base);
  423. if (ret)
  424. return ret;
  425. ret = xgene_pcie_parse_map_dma_ranges(port);
  426. if (ret)
  427. return ret;
  428. xgene_pcie_linkup(port, &lanes, &speed);
  429. if (!port->link_up)
  430. dev_info(port->dev, "(rc) link down\n");
  431. else
  432. dev_info(port->dev, "(rc) x%d gen-%d link up\n",
  433. lanes, speed + 1);
  434. return 0;
  435. }
  436. static int xgene_pcie_probe_bridge(struct platform_device *pdev)
  437. {
  438. struct device_node *dn = pdev->dev.of_node;
  439. struct xgene_pcie_port *port;
  440. resource_size_t iobase = 0;
  441. struct pci_bus *bus;
  442. int ret;
  443. LIST_HEAD(res);
  444. port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
  445. if (!port)
  446. return -ENOMEM;
  447. port->node = of_node_get(pdev->dev.of_node);
  448. port->dev = &pdev->dev;
  449. port->version = XGENE_PCIE_IP_VER_UNKN;
  450. if (of_device_is_compatible(port->node, "apm,xgene-pcie"))
  451. port->version = XGENE_PCIE_IP_VER_1;
  452. ret = xgene_pcie_map_reg(port, pdev);
  453. if (ret)
  454. return ret;
  455. ret = xgene_pcie_init_port(port);
  456. if (ret)
  457. return ret;
  458. ret = of_pci_get_host_bridge_resources(dn, 0, 0xff, &res, &iobase);
  459. if (ret)
  460. return ret;
  461. ret = xgene_pcie_setup(port, &res, iobase);
  462. if (ret)
  463. return ret;
  464. bus = pci_create_root_bus(&pdev->dev, 0,
  465. &xgene_pcie_ops, port, &res);
  466. if (!bus)
  467. return -ENOMEM;
  468. pci_scan_child_bus(bus);
  469. pci_assign_unassigned_bus_resources(bus);
  470. pci_bus_add_devices(bus);
  471. platform_set_drvdata(pdev, port);
  472. return 0;
  473. }
  474. static const struct of_device_id xgene_pcie_match_table[] = {
  475. {.compatible = "apm,xgene-pcie",},
  476. {},
  477. };
  478. static struct platform_driver xgene_pcie_driver = {
  479. .driver = {
  480. .name = "xgene-pcie",
  481. .of_match_table = of_match_ptr(xgene_pcie_match_table),
  482. },
  483. .probe = xgene_pcie_probe_bridge,
  484. };
  485. module_platform_driver(xgene_pcie_driver);
  486. MODULE_AUTHOR("Tanmay Inamdar <tinamdar@apm.com>");
  487. MODULE_DESCRIPTION("APM X-Gene PCIe driver");
  488. MODULE_LICENSE("GPL v2");