pci-xgene.c 15 KB

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