pci-keystone.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * PCIe host controller driver for Texas Instruments Keystone SoCs
  3. *
  4. * Copyright (C) 2013-2014 Texas Instruments., Ltd.
  5. * http://www.ti.com
  6. *
  7. * Author: Murali Karicheri <m-karicheri2@ti.com>
  8. * Implementation based on pci-exynos.c and pcie-designware.c
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/irqchip/chained_irq.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/init.h>
  20. #include <linux/mfd/syscon.h>
  21. #include <linux/msi.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/of.h>
  24. #include <linux/of_pci.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/phy/phy.h>
  28. #include <linux/regmap.h>
  29. #include <linux/resource.h>
  30. #include <linux/signal.h>
  31. #include "pcie-designware.h"
  32. #include "pci-keystone.h"
  33. #define DRIVER_NAME "keystone-pcie"
  34. /* driver specific constants */
  35. #define MAX_MSI_HOST_IRQS 8
  36. #define MAX_LEGACY_HOST_IRQS 4
  37. /* DEV_STAT_CTRL */
  38. #define PCIE_CAP_BASE 0x70
  39. /* PCIE controller device IDs */
  40. #define PCIE_RC_K2HK 0xb008
  41. #define PCIE_RC_K2E 0xb009
  42. #define PCIE_RC_K2L 0xb00a
  43. #define PCIE_RC_K2G 0xb00b
  44. #define KS_PCIE_DEV_TYPE_MASK (0x3 << 1)
  45. #define KS_PCIE_DEV_TYPE(mode) ((mode) << 1)
  46. #define EP 0x0
  47. #define LEG_EP 0x1
  48. #define RC 0x2
  49. #define KS_PCIE_SYSCLOCKOUTEN 0x1
  50. #define to_keystone_pcie(x) dev_get_drvdata((x)->dev)
  51. struct ks_pcie_of_data {
  52. enum dw_pcie_device_mode mode;
  53. };
  54. static void quirk_limit_mrrs(struct pci_dev *dev)
  55. {
  56. struct pci_bus *bus = dev->bus;
  57. struct pci_dev *bridge = bus->self;
  58. static const struct pci_device_id rc_pci_devids[] = {
  59. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK),
  60. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  61. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E),
  62. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  63. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L),
  64. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  65. { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G),
  66. .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, },
  67. { 0, },
  68. };
  69. if (pci_is_root_bus(bus))
  70. return;
  71. /* look for the host bridge */
  72. while (!pci_is_root_bus(bus)) {
  73. bridge = bus->self;
  74. bus = bus->parent;
  75. }
  76. if (bridge) {
  77. /*
  78. * Keystone PCI controller has a h/w limitation of
  79. * 256 bytes maximum read request size. It can't handle
  80. * anything higher than this. So force this limit on
  81. * all downstream devices.
  82. */
  83. if (pci_match_id(rc_pci_devids, bridge)) {
  84. if (pcie_get_readrq(dev) > 256) {
  85. dev_info(&dev->dev, "limiting MRRS to 256\n");
  86. pcie_set_readrq(dev, 256);
  87. }
  88. }
  89. }
  90. }
  91. DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, quirk_limit_mrrs);
  92. static int ks_pcie_start_link(struct dw_pcie *pci)
  93. {
  94. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  95. ks_dw_pcie_initiate_link_train(ks_pcie);
  96. return 0;
  97. }
  98. static int ks_pcie_establish_link(struct keystone_pcie *ks_pcie)
  99. {
  100. struct dw_pcie *pci = ks_pcie->pci;
  101. struct pcie_port *pp = &pci->pp;
  102. struct device *dev = pci->dev;
  103. unsigned int retries;
  104. dw_pcie_setup_rc(pp);
  105. if (dw_pcie_link_up(pci)) {
  106. dev_err(dev, "Link already up\n");
  107. return 0;
  108. }
  109. /* check if the link is up or not */
  110. for (retries = 0; retries < 5; retries++) {
  111. ks_dw_pcie_initiate_link_train(ks_pcie);
  112. if (!dw_pcie_wait_for_link(pci))
  113. return 0;
  114. }
  115. dev_err(dev, "phy link never came up\n");
  116. return -ETIMEDOUT;
  117. }
  118. static void ks_pcie_msi_irq_handler(struct irq_desc *desc)
  119. {
  120. unsigned int irq = irq_desc_get_irq(desc);
  121. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  122. u32 offset = irq - ks_pcie->msi_host_irqs[0];
  123. struct dw_pcie *pci = ks_pcie->pci;
  124. struct device *dev = pci->dev;
  125. struct irq_chip *chip = irq_desc_get_chip(desc);
  126. dev_dbg(dev, "%s, irq %d\n", __func__, irq);
  127. /*
  128. * The chained irq handler installation would have replaced normal
  129. * interrupt driver handler so we need to take care of mask/unmask and
  130. * ack operation.
  131. */
  132. chained_irq_enter(chip, desc);
  133. ks_dw_pcie_handle_msi_irq(ks_pcie, offset);
  134. chained_irq_exit(chip, desc);
  135. }
  136. /**
  137. * ks_pcie_legacy_irq_handler() - Handle legacy interrupt
  138. * @irq: IRQ line for legacy interrupts
  139. * @desc: Pointer to irq descriptor
  140. *
  141. * Traverse through pending legacy interrupts and invoke handler for each. Also
  142. * takes care of interrupt controller level mask/ack operation.
  143. */
  144. static void ks_pcie_legacy_irq_handler(struct irq_desc *desc)
  145. {
  146. unsigned int irq = irq_desc_get_irq(desc);
  147. struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc);
  148. struct dw_pcie *pci = ks_pcie->pci;
  149. struct device *dev = pci->dev;
  150. u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0];
  151. struct irq_chip *chip = irq_desc_get_chip(desc);
  152. dev_dbg(dev, ": Handling legacy irq %d\n", irq);
  153. /*
  154. * The chained irq handler installation would have replaced normal
  155. * interrupt driver handler so we need to take care of mask/unmask and
  156. * ack operation.
  157. */
  158. chained_irq_enter(chip, desc);
  159. ks_dw_pcie_handle_legacy_irq(ks_pcie, irq_offset);
  160. chained_irq_exit(chip, desc);
  161. }
  162. static int ks_pcie_get_irq_controller_info(struct keystone_pcie *ks_pcie,
  163. char *controller, int *num_irqs)
  164. {
  165. int temp, max_host_irqs, legacy = 1, *host_irqs;
  166. struct device *dev = ks_pcie->pci->dev;
  167. struct device_node *np_pcie = dev->of_node, **np_temp;
  168. if (!strcmp(controller, "msi-interrupt-controller"))
  169. legacy = 0;
  170. if (legacy) {
  171. np_temp = &ks_pcie->legacy_intc_np;
  172. max_host_irqs = MAX_LEGACY_HOST_IRQS;
  173. host_irqs = &ks_pcie->legacy_host_irqs[0];
  174. } else {
  175. np_temp = &ks_pcie->msi_intc_np;
  176. max_host_irqs = MAX_MSI_HOST_IRQS;
  177. host_irqs = &ks_pcie->msi_host_irqs[0];
  178. }
  179. /* interrupt controller is in a child node */
  180. *np_temp = of_find_node_by_name(np_pcie, controller);
  181. if (!(*np_temp)) {
  182. dev_err(dev, "Node for %s is absent\n", controller);
  183. return -EINVAL;
  184. }
  185. temp = of_irq_count(*np_temp);
  186. if (!temp) {
  187. dev_err(dev, "No IRQ entries in %s\n", controller);
  188. return -EINVAL;
  189. }
  190. if (temp > max_host_irqs)
  191. dev_warn(dev, "Too many %s interrupts defined %u\n",
  192. (legacy ? "legacy" : "MSI"), temp);
  193. /*
  194. * support upto max_host_irqs. In dt from index 0 to 3 (legacy) or 0 to
  195. * 7 (MSI)
  196. */
  197. for (temp = 0; temp < max_host_irqs; temp++) {
  198. host_irqs[temp] = irq_of_parse_and_map(*np_temp, temp);
  199. if (!host_irqs[temp])
  200. break;
  201. }
  202. if (temp) {
  203. *num_irqs = temp;
  204. return 0;
  205. }
  206. return -EINVAL;
  207. }
  208. static void ks_pcie_setup_interrupts(struct keystone_pcie *ks_pcie)
  209. {
  210. int i;
  211. /* Legacy IRQ */
  212. for (i = 0; i < ks_pcie->num_legacy_host_irqs; i++) {
  213. irq_set_chained_handler_and_data(ks_pcie->legacy_host_irqs[i],
  214. ks_pcie_legacy_irq_handler,
  215. ks_pcie);
  216. }
  217. ks_dw_pcie_enable_legacy_irqs(ks_pcie);
  218. /* MSI IRQ */
  219. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  220. for (i = 0; i < ks_pcie->num_msi_host_irqs; i++) {
  221. irq_set_chained_handler_and_data(ks_pcie->msi_host_irqs[i],
  222. ks_pcie_msi_irq_handler,
  223. ks_pcie);
  224. }
  225. }
  226. if (ks_pcie->error_irq > 0)
  227. ks_dw_pcie_enable_error_irq(ks_pcie);
  228. }
  229. /*
  230. * When a PCI device does not exist during config cycles, keystone host gets a
  231. * bus error instead of returning 0xffffffff. This handler always returns 0
  232. * for this kind of faults.
  233. */
  234. static int keystone_pcie_fault(unsigned long addr, unsigned int fsr,
  235. struct pt_regs *regs)
  236. {
  237. unsigned long instr = *(unsigned long *) instruction_pointer(regs);
  238. if ((instr & 0x0e100090) == 0x00100090) {
  239. int reg = (instr >> 12) & 15;
  240. regs->uregs[reg] = -1;
  241. regs->ARM_pc += 4;
  242. }
  243. return 0;
  244. }
  245. static void __init ks_pcie_host_init(struct pcie_port *pp)
  246. {
  247. struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
  248. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  249. u32 val;
  250. ks_pcie_establish_link(ks_pcie);
  251. ks_dw_pcie_setup_rc_app_regs(ks_pcie);
  252. ks_pcie_setup_interrupts(ks_pcie);
  253. writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8),
  254. pci->dbi_base + PCI_IO_BASE);
  255. /* update the Vendor ID */
  256. writew(ks_pcie->device_id, pci->dbi_base + PCI_DEVICE_ID);
  257. /* update the DEV_STAT_CTRL to publish right mrrs */
  258. val = readl(pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  259. val &= ~PCI_EXP_DEVCTL_READRQ;
  260. /* set the mrrs to 256 bytes */
  261. val |= BIT(12);
  262. writel(val, pci->dbi_base + PCIE_CAP_BASE + PCI_EXP_DEVCTL);
  263. /*
  264. * PCIe access errors that result into OCP errors are caught by ARM as
  265. * "External aborts"
  266. */
  267. hook_fault_code(17, keystone_pcie_fault, SIGBUS, 0,
  268. "Asynchronous external abort");
  269. }
  270. static struct dw_pcie_host_ops keystone_pcie_host_ops = {
  271. .rd_other_conf = ks_dw_pcie_rd_other_conf,
  272. .wr_other_conf = ks_dw_pcie_wr_other_conf,
  273. .host_init = ks_pcie_host_init,
  274. .msi_set_irq = ks_dw_pcie_msi_set_irq,
  275. .msi_clear_irq = ks_dw_pcie_msi_clear_irq,
  276. .get_msi_addr = ks_dw_pcie_get_msi_addr,
  277. .msi_host_init = ks_dw_pcie_msi_host_init,
  278. .scan_bus = ks_dw_pcie_v3_65_scan_bus,
  279. };
  280. static irqreturn_t pcie_err_irq_handler(int irq, void *priv)
  281. {
  282. struct keystone_pcie *ks_pcie = priv;
  283. return ks_dw_pcie_handle_error_irq(ks_pcie);
  284. }
  285. static int __init ks_add_pcie_port(struct keystone_pcie *ks_pcie,
  286. struct platform_device *pdev)
  287. {
  288. struct dw_pcie *pci = ks_pcie->pci;
  289. struct pcie_port *pp = &pci->pp;
  290. struct device *dev = &pdev->dev;
  291. struct resource *res;
  292. void __iomem *reg_p;
  293. int ret;
  294. /* index 2 is to read PCI DEVICE_ID */
  295. res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  296. reg_p = devm_ioremap_resource(dev, res);
  297. if (IS_ERR(reg_p))
  298. return PTR_ERR(reg_p);
  299. ks_pcie->device_id = readl(reg_p) >> 16;
  300. devm_iounmap(dev, reg_p);
  301. devm_release_mem_region(dev, res->start, resource_size(res));
  302. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  303. "legacy-interrupt-controller",
  304. &ks_pcie->num_legacy_host_irqs);
  305. if (ret)
  306. return ret;
  307. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  308. ret = ks_pcie_get_irq_controller_info(ks_pcie,
  309. "msi-interrupt-controller",
  310. &ks_pcie->num_msi_host_irqs);
  311. if (ret)
  312. return ret;
  313. }
  314. /*
  315. * Index 0 is the platform interrupt for error interrupt
  316. * from RC. This is optional.
  317. */
  318. ks_pcie->error_irq = irq_of_parse_and_map(ks_pcie->np, 0);
  319. if (ks_pcie->error_irq <= 0)
  320. dev_info(dev, "no error IRQ defined\n");
  321. else {
  322. ret = request_irq(ks_pcie->error_irq, pcie_err_irq_handler,
  323. IRQF_SHARED, "pcie-error-irq", ks_pcie);
  324. if (ret < 0) {
  325. dev_err(dev, "failed to request error IRQ %d\n",
  326. ks_pcie->error_irq);
  327. return ret;
  328. }
  329. }
  330. pp->root_bus_nr = -1;
  331. pp->ops = &keystone_pcie_host_ops;
  332. ret = ks_dw_pcie_host_init(ks_pcie, ks_pcie->msi_intc_np);
  333. if (ret) {
  334. dev_err(dev, "failed to initialize host\n");
  335. return ret;
  336. }
  337. return 0;
  338. }
  339. static int ks_pcie_raise_irq(struct dw_pcie_ep *ep, enum pci_epc_irq_type type,
  340. u8 interrupt_num)
  341. {
  342. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  343. struct keystone_pcie *ks_pcie = to_keystone_pcie(pci);
  344. switch (type) {
  345. case PCI_EPC_IRQ_LEGACY:
  346. ks_dw_pcie_raise_legacy_irq(ks_pcie);
  347. break;
  348. case PCI_EPC_IRQ_MSI:
  349. dev_err(pci->dev, "Raising MSI interrupt not supported\n");
  350. return -EINVAL;
  351. default:
  352. dev_err(pci->dev, "UNKNOWN IRQ type\n");
  353. return -EINVAL;
  354. }
  355. return 0;
  356. }
  357. static struct dw_pcie_ep_ops ks_dw_pcie_ep_ops = {
  358. .ep_init = ks_dw_pcie_ep_init,
  359. .raise_irq = ks_pcie_raise_irq,
  360. };
  361. static int __init ks_add_pcie_ep(struct keystone_pcie *ks_pcie,
  362. struct platform_device *pdev)
  363. {
  364. int ret;
  365. struct dw_pcie_ep *ep;
  366. struct resource *res;
  367. struct device *dev = &pdev->dev;
  368. struct dw_pcie *pci = ks_pcie->pci;
  369. ep = &pci->ep;
  370. ep->ops = &ks_dw_pcie_ep_ops;
  371. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ep_dbics");
  372. pci->dbi_base = devm_ioremap(dev, res->start, resource_size(res));
  373. if (!pci->dbi_base)
  374. return -ENOMEM;
  375. pci->dbi_base2 = pci->dbi_base;
  376. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
  377. if (!res)
  378. return -EINVAL;
  379. ep->phys_base = res->start;
  380. ep->addr_size = resource_size(res);
  381. ret = dw_pcie_ep_init(ep);
  382. if (ret) {
  383. dev_err(dev, "failed to initialize endpoint\n");
  384. return ret;
  385. }
  386. return 0;
  387. }
  388. static const struct ks_pcie_of_data ks_pcie_rc_of_data = {
  389. .mode = DW_PCIE_RC_TYPE,
  390. };
  391. static const struct ks_pcie_of_data ks_pcie_ep_of_data = {
  392. .mode = DW_PCIE_EP_TYPE,
  393. };
  394. static const struct of_device_id ks_pcie_of_match[] = {
  395. {
  396. .type = "pci",
  397. .data = &ks_pcie_rc_of_data,
  398. .compatible = "ti,keystone-pcie",
  399. },
  400. {
  401. .data = &ks_pcie_ep_of_data,
  402. .compatible = "ti,keystone-pcie-ep",
  403. },
  404. { },
  405. };
  406. static const struct dw_pcie_ops dw_pcie_ops = {
  407. .start_link = ks_pcie_start_link,
  408. .link_up = ks_dw_pcie_link_up,
  409. .read_dbi2 = ks_dw_pcie_read_dbi2,
  410. .write_dbi2 = ks_dw_pcie_write_dbi2,
  411. .inbound_atu = ks_dw_pcie_inbound_atu,
  412. .outbound_atu = ks_dw_pcie_outbound_atu,
  413. .disable_atu = ks_dw_pcie_disable_atu,
  414. };
  415. static int __exit ks_pcie_remove(struct platform_device *pdev)
  416. {
  417. struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev);
  418. clk_disable_unprepare(ks_pcie->clk);
  419. return 0;
  420. }
  421. static int ks_pcie_set_mode(struct device *dev, enum dw_pcie_device_mode mode)
  422. {
  423. struct device_node *np = dev->of_node;
  424. struct regmap *syscon;
  425. unsigned int reg;
  426. u32 val;
  427. u32 mask;
  428. int ret = 0;
  429. syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-dev");
  430. if (IS_ERR(syscon))
  431. return 0;
  432. ret = of_property_read_u32_index(np, "ti,syscon-dev", 1,
  433. &reg);
  434. if (ret) {
  435. dev_err(dev, "can't read the data register offset!\n");
  436. return ret;
  437. }
  438. mask = KS_PCIE_DEV_TYPE_MASK | KS_PCIE_SYSCLOCKOUTEN;
  439. switch (mode) {
  440. case DW_PCIE_RC_TYPE:
  441. val = KS_PCIE_DEV_TYPE(RC) | KS_PCIE_SYSCLOCKOUTEN;
  442. break;
  443. case DW_PCIE_EP_TYPE:
  444. val = KS_PCIE_DEV_TYPE(EP);
  445. break;
  446. default:
  447. dev_err(dev, "INVALID device type %d\n", mode);
  448. return -EINVAL;
  449. }
  450. ret = regmap_update_bits(syscon, reg, mask, val);
  451. if (ret) {
  452. dev_err(dev, "failed to set pcie mode\n");
  453. return ret;
  454. }
  455. return 0;
  456. }
  457. static int __init ks_pcie_probe(struct platform_device *pdev)
  458. {
  459. struct device *dev = &pdev->dev;
  460. struct dw_pcie *pci;
  461. struct keystone_pcie *ks_pcie;
  462. struct resource *res;
  463. int ret = 0;
  464. const struct of_device_id *match;
  465. const struct ks_pcie_of_data *data;
  466. enum dw_pcie_device_mode mode = DW_PCIE_RC_TYPE;
  467. match = of_match_device(of_match_ptr(ks_pcie_of_match), dev);
  468. data = (struct ks_pcie_of_data *)match->data;
  469. if (data)
  470. mode = (enum dw_pcie_device_mode)data->mode;
  471. ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL);
  472. if (!ks_pcie)
  473. return -ENOMEM;
  474. pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  475. if (!pci)
  476. return -ENOMEM;
  477. pci->dev = dev;
  478. pci->ops = &dw_pcie_ops;
  479. /* Index 1 is the application reg. space address */
  480. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  481. ks_pcie->va_app_base = devm_ioremap_resource(dev, res);
  482. if (IS_ERR(ks_pcie->va_app_base))
  483. return PTR_ERR(ks_pcie->va_app_base);
  484. ks_pcie->app = *res;
  485. ks_pcie->pci = pci;
  486. ks_pcie->np = dev->of_node;
  487. platform_set_drvdata(pdev, ks_pcie);
  488. ks_pcie->clk = devm_clk_get(dev, "pcie");
  489. if (IS_ERR(ks_pcie->clk)) {
  490. dev_err(dev, "Failed to get pcie rc clock\n");
  491. return PTR_ERR(ks_pcie->clk);
  492. }
  493. ret = clk_prepare_enable(ks_pcie->clk);
  494. if (ret)
  495. return ret;
  496. platform_set_drvdata(pdev, ks_pcie);
  497. ret = ks_pcie_set_mode(dev, mode);
  498. if (ret < 0)
  499. goto fail_clk;
  500. switch (mode) {
  501. case DW_PCIE_RC_TYPE:
  502. ret = ks_add_pcie_port(ks_pcie, pdev);
  503. if (ret < 0)
  504. goto fail_clk;
  505. break;
  506. case DW_PCIE_EP_TYPE:
  507. ret = ks_add_pcie_ep(ks_pcie, pdev);
  508. if (ret < 0)
  509. goto fail_clk;
  510. break;
  511. default:
  512. dev_err(dev, "INVALID device type %d\n", mode);
  513. }
  514. return 0;
  515. fail_clk:
  516. clk_disable_unprepare(ks_pcie->clk);
  517. return ret;
  518. }
  519. static struct platform_driver ks_pcie_driver __refdata = {
  520. .probe = ks_pcie_probe,
  521. .remove = __exit_p(ks_pcie_remove),
  522. .driver = {
  523. .name = "keystone-pcie",
  524. .of_match_table = of_match_ptr(ks_pcie_of_match),
  525. },
  526. };
  527. builtin_platform_driver(ks_pcie_driver);