pcie-designware.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. /*
  2. * Synopsys Designware PCIe host controller driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Author: Jingoo Han <jg1.han@samsung.com>
  8. *
  9. * This program 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/irq.h>
  14. #include <linux/irqdomain.h>
  15. #include <linux/kernel.h>
  16. #include <linux/msi.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_pci.h>
  19. #include <linux/pci.h>
  20. #include <linux/pci_regs.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/types.h>
  23. #include <linux/delay.h>
  24. #include "pcie-designware.h"
  25. /* Parameters for the waiting for link up routine */
  26. #define LINK_WAIT_MAX_RETRIES 10
  27. #define LINK_WAIT_USLEEP_MIN 90000
  28. #define LINK_WAIT_USLEEP_MAX 100000
  29. /* Parameters for the waiting for iATU enabled routine */
  30. #define LINK_WAIT_MAX_IATU_RETRIES 5
  31. #define LINK_WAIT_IATU_MIN 9000
  32. #define LINK_WAIT_IATU_MAX 10000
  33. /* Synopsys-specific PCIe configuration registers */
  34. #define PCIE_PORT_LINK_CONTROL 0x710
  35. #define PORT_LINK_MODE_MASK (0x3f << 16)
  36. #define PORT_LINK_MODE_1_LANES (0x1 << 16)
  37. #define PORT_LINK_MODE_2_LANES (0x3 << 16)
  38. #define PORT_LINK_MODE_4_LANES (0x7 << 16)
  39. #define PORT_LINK_MODE_8_LANES (0xf << 16)
  40. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  41. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  42. #define PORT_LOGIC_LINK_WIDTH_MASK (0x1f << 8)
  43. #define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
  44. #define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
  45. #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
  46. #define PORT_LOGIC_LINK_WIDTH_8_LANES (0x8 << 8)
  47. #define PCIE_MSI_ADDR_LO 0x820
  48. #define PCIE_MSI_ADDR_HI 0x824
  49. #define PCIE_MSI_INTR0_ENABLE 0x828
  50. #define PCIE_MSI_INTR0_MASK 0x82C
  51. #define PCIE_MSI_INTR0_STATUS 0x830
  52. #define PCIE_ATU_VIEWPORT 0x900
  53. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  54. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  55. #define PCIE_ATU_REGION_INDEX2 (0x2 << 0)
  56. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  57. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  58. #define PCIE_ATU_CR1 0x904
  59. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  60. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  61. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  62. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  63. #define PCIE_ATU_CR2 0x908
  64. #define PCIE_ATU_ENABLE (0x1 << 31)
  65. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  66. #define PCIE_ATU_LOWER_BASE 0x90C
  67. #define PCIE_ATU_UPPER_BASE 0x910
  68. #define PCIE_ATU_LIMIT 0x914
  69. #define PCIE_ATU_LOWER_TARGET 0x918
  70. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  71. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  72. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  73. #define PCIE_ATU_UPPER_TARGET 0x91C
  74. /*
  75. * iATU Unroll-specific register definitions
  76. * From 4.80 core version the address translation will be made by unroll
  77. */
  78. #define PCIE_ATU_UNR_REGION_CTRL1 0x00
  79. #define PCIE_ATU_UNR_REGION_CTRL2 0x04
  80. #define PCIE_ATU_UNR_LOWER_BASE 0x08
  81. #define PCIE_ATU_UNR_UPPER_BASE 0x0C
  82. #define PCIE_ATU_UNR_LIMIT 0x10
  83. #define PCIE_ATU_UNR_LOWER_TARGET 0x14
  84. #define PCIE_ATU_UNR_UPPER_TARGET 0x18
  85. /* Register address builder */
  86. #define PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(region) ((0x3 << 20) | (region << 9))
  87. /* PCIe Port Logic registers */
  88. #define PLR_OFFSET 0x700
  89. #define PCIE_PHY_DEBUG_R1 (PLR_OFFSET + 0x2c)
  90. #define PCIE_PHY_DEBUG_R1_LINK_UP (0x1 << 4)
  91. #define PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING (0x1 << 29)
  92. static struct pci_ops dw_pcie_ops;
  93. int dw_pcie_cfg_read(void __iomem *addr, int size, u32 *val)
  94. {
  95. if ((uintptr_t)addr & (size - 1)) {
  96. *val = 0;
  97. return PCIBIOS_BAD_REGISTER_NUMBER;
  98. }
  99. if (size == 4)
  100. *val = readl(addr);
  101. else if (size == 2)
  102. *val = readw(addr);
  103. else if (size == 1)
  104. *val = readb(addr);
  105. else {
  106. *val = 0;
  107. return PCIBIOS_BAD_REGISTER_NUMBER;
  108. }
  109. return PCIBIOS_SUCCESSFUL;
  110. }
  111. int dw_pcie_cfg_write(void __iomem *addr, int size, u32 val)
  112. {
  113. if ((uintptr_t)addr & (size - 1))
  114. return PCIBIOS_BAD_REGISTER_NUMBER;
  115. if (size == 4)
  116. writel(val, addr);
  117. else if (size == 2)
  118. writew(val, addr);
  119. else if (size == 1)
  120. writeb(val, addr);
  121. else
  122. return PCIBIOS_BAD_REGISTER_NUMBER;
  123. return PCIBIOS_SUCCESSFUL;
  124. }
  125. u32 dw_pcie_readl_rc(struct pcie_port *pp, u32 reg)
  126. {
  127. if (pp->ops->readl_rc)
  128. return pp->ops->readl_rc(pp, reg);
  129. return readl(pp->dbi_base + reg);
  130. }
  131. void dw_pcie_writel_rc(struct pcie_port *pp, u32 reg, u32 val)
  132. {
  133. if (pp->ops->writel_rc)
  134. pp->ops->writel_rc(pp, reg, val);
  135. else
  136. writel(val, pp->dbi_base + reg);
  137. }
  138. static u32 dw_pcie_readl_unroll(struct pcie_port *pp, u32 index, u32 reg)
  139. {
  140. u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
  141. return dw_pcie_readl_rc(pp, offset + reg);
  142. }
  143. static void dw_pcie_writel_unroll(struct pcie_port *pp, u32 index, u32 reg,
  144. u32 val)
  145. {
  146. u32 offset = PCIE_GET_ATU_OUTB_UNR_REG_OFFSET(index);
  147. dw_pcie_writel_rc(pp, offset + reg, val);
  148. }
  149. static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  150. u32 *val)
  151. {
  152. if (pp->ops->rd_own_conf)
  153. return pp->ops->rd_own_conf(pp, where, size, val);
  154. return dw_pcie_cfg_read(pp->dbi_base + where, size, val);
  155. }
  156. static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  157. u32 val)
  158. {
  159. if (pp->ops->wr_own_conf)
  160. return pp->ops->wr_own_conf(pp, where, size, val);
  161. return dw_pcie_cfg_write(pp->dbi_base + where, size, val);
  162. }
  163. static void dw_pcie_prog_outbound_atu(struct pcie_port *pp, int index,
  164. int type, u64 cpu_addr, u64 pci_addr, u32 size)
  165. {
  166. u32 retries, val;
  167. if (pp->iatu_unroll_enabled) {
  168. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_LOWER_BASE,
  169. lower_32_bits(cpu_addr));
  170. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_UPPER_BASE,
  171. upper_32_bits(cpu_addr));
  172. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_LIMIT,
  173. lower_32_bits(cpu_addr + size - 1));
  174. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_LOWER_TARGET,
  175. lower_32_bits(pci_addr));
  176. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_UPPER_TARGET,
  177. upper_32_bits(pci_addr));
  178. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_REGION_CTRL1,
  179. type);
  180. dw_pcie_writel_unroll(pp, index, PCIE_ATU_UNR_REGION_CTRL2,
  181. PCIE_ATU_ENABLE);
  182. } else {
  183. dw_pcie_writel_rc(pp, PCIE_ATU_VIEWPORT,
  184. PCIE_ATU_REGION_OUTBOUND | index);
  185. dw_pcie_writel_rc(pp, PCIE_ATU_LOWER_BASE,
  186. lower_32_bits(cpu_addr));
  187. dw_pcie_writel_rc(pp, PCIE_ATU_UPPER_BASE,
  188. upper_32_bits(cpu_addr));
  189. dw_pcie_writel_rc(pp, PCIE_ATU_LIMIT,
  190. lower_32_bits(cpu_addr + size - 1));
  191. dw_pcie_writel_rc(pp, PCIE_ATU_LOWER_TARGET,
  192. lower_32_bits(pci_addr));
  193. dw_pcie_writel_rc(pp, PCIE_ATU_UPPER_TARGET,
  194. upper_32_bits(pci_addr));
  195. dw_pcie_writel_rc(pp, PCIE_ATU_CR1, type);
  196. dw_pcie_writel_rc(pp, PCIE_ATU_CR2, PCIE_ATU_ENABLE);
  197. }
  198. /*
  199. * Make sure ATU enable takes effect before any subsequent config
  200. * and I/O accesses.
  201. */
  202. for (retries = 0; retries < LINK_WAIT_MAX_IATU_RETRIES; retries++) {
  203. if (pp->iatu_unroll_enabled)
  204. val = dw_pcie_readl_unroll(pp, index,
  205. PCIE_ATU_UNR_REGION_CTRL2);
  206. else
  207. val = dw_pcie_readl_rc(pp, PCIE_ATU_CR2);
  208. if (val == PCIE_ATU_ENABLE)
  209. return;
  210. usleep_range(LINK_WAIT_IATU_MIN, LINK_WAIT_IATU_MAX);
  211. }
  212. dev_err(pp->dev, "iATU is not being enabled\n");
  213. }
  214. static struct irq_chip dw_msi_irq_chip = {
  215. .name = "PCI-MSI",
  216. .irq_enable = pci_msi_unmask_irq,
  217. .irq_disable = pci_msi_mask_irq,
  218. .irq_mask = pci_msi_mask_irq,
  219. .irq_unmask = pci_msi_unmask_irq,
  220. };
  221. /* MSI int handler */
  222. irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
  223. {
  224. unsigned long val;
  225. int i, pos, irq;
  226. irqreturn_t ret = IRQ_NONE;
  227. for (i = 0; i < MAX_MSI_CTRLS; i++) {
  228. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
  229. (u32 *)&val);
  230. if (val) {
  231. ret = IRQ_HANDLED;
  232. pos = 0;
  233. while ((pos = find_next_bit(&val, 32, pos)) != 32) {
  234. irq = irq_find_mapping(pp->irq_domain,
  235. i * 32 + pos);
  236. dw_pcie_wr_own_conf(pp,
  237. PCIE_MSI_INTR0_STATUS + i * 12,
  238. 4, 1 << pos);
  239. generic_handle_irq(irq);
  240. pos++;
  241. }
  242. }
  243. }
  244. return ret;
  245. }
  246. void dw_pcie_msi_init(struct pcie_port *pp)
  247. {
  248. u64 msi_target;
  249. pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
  250. msi_target = virt_to_phys((void *)pp->msi_data);
  251. /* program the msi_data */
  252. dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
  253. (u32)(msi_target & 0xffffffff));
  254. dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4,
  255. (u32)(msi_target >> 32 & 0xffffffff));
  256. }
  257. static void dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
  258. {
  259. unsigned int res, bit, val;
  260. res = (irq / 32) * 12;
  261. bit = irq % 32;
  262. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
  263. val &= ~(1 << bit);
  264. dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
  265. }
  266. static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base,
  267. unsigned int nvec, unsigned int pos)
  268. {
  269. unsigned int i;
  270. for (i = 0; i < nvec; i++) {
  271. irq_set_msi_desc_off(irq_base, i, NULL);
  272. /* Disable corresponding interrupt on MSI controller */
  273. if (pp->ops->msi_clear_irq)
  274. pp->ops->msi_clear_irq(pp, pos + i);
  275. else
  276. dw_pcie_msi_clear_irq(pp, pos + i);
  277. }
  278. bitmap_release_region(pp->msi_irq_in_use, pos, order_base_2(nvec));
  279. }
  280. static void dw_pcie_msi_set_irq(struct pcie_port *pp, int irq)
  281. {
  282. unsigned int res, bit, val;
  283. res = (irq / 32) * 12;
  284. bit = irq % 32;
  285. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
  286. val |= 1 << bit;
  287. dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
  288. }
  289. static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
  290. {
  291. int irq, pos0, i;
  292. struct pcie_port *pp = (struct pcie_port *) msi_desc_to_pci_sysdata(desc);
  293. pos0 = bitmap_find_free_region(pp->msi_irq_in_use, MAX_MSI_IRQS,
  294. order_base_2(no_irqs));
  295. if (pos0 < 0)
  296. goto no_valid_irq;
  297. irq = irq_find_mapping(pp->irq_domain, pos0);
  298. if (!irq)
  299. goto no_valid_irq;
  300. /*
  301. * irq_create_mapping (called from dw_pcie_host_init) pre-allocates
  302. * descs so there is no need to allocate descs here. We can therefore
  303. * assume that if irq_find_mapping above returns non-zero, then the
  304. * descs are also successfully allocated.
  305. */
  306. for (i = 0; i < no_irqs; i++) {
  307. if (irq_set_msi_desc_off(irq, i, desc) != 0) {
  308. clear_irq_range(pp, irq, i, pos0);
  309. goto no_valid_irq;
  310. }
  311. /*Enable corresponding interrupt in MSI interrupt controller */
  312. if (pp->ops->msi_set_irq)
  313. pp->ops->msi_set_irq(pp, pos0 + i);
  314. else
  315. dw_pcie_msi_set_irq(pp, pos0 + i);
  316. }
  317. *pos = pos0;
  318. desc->nvec_used = no_irqs;
  319. desc->msi_attrib.multiple = order_base_2(no_irqs);
  320. return irq;
  321. no_valid_irq:
  322. *pos = pos0;
  323. return -ENOSPC;
  324. }
  325. static void dw_msi_setup_msg(struct pcie_port *pp, unsigned int irq, u32 pos)
  326. {
  327. struct msi_msg msg;
  328. u64 msi_target;
  329. if (pp->ops->get_msi_addr)
  330. msi_target = pp->ops->get_msi_addr(pp);
  331. else
  332. msi_target = virt_to_phys((void *)pp->msi_data);
  333. msg.address_lo = (u32)(msi_target & 0xffffffff);
  334. msg.address_hi = (u32)(msi_target >> 32 & 0xffffffff);
  335. if (pp->ops->get_msi_data)
  336. msg.data = pp->ops->get_msi_data(pp, pos);
  337. else
  338. msg.data = pos;
  339. pci_write_msi_msg(irq, &msg);
  340. }
  341. static int dw_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
  342. struct msi_desc *desc)
  343. {
  344. int irq, pos;
  345. struct pcie_port *pp = pdev->bus->sysdata;
  346. if (desc->msi_attrib.is_msix)
  347. return -EINVAL;
  348. irq = assign_irq(1, desc, &pos);
  349. if (irq < 0)
  350. return irq;
  351. dw_msi_setup_msg(pp, irq, pos);
  352. return 0;
  353. }
  354. static int dw_msi_setup_irqs(struct msi_controller *chip, struct pci_dev *pdev,
  355. int nvec, int type)
  356. {
  357. #ifdef CONFIG_PCI_MSI
  358. int irq, pos;
  359. struct msi_desc *desc;
  360. struct pcie_port *pp = pdev->bus->sysdata;
  361. /* MSI-X interrupts are not supported */
  362. if (type == PCI_CAP_ID_MSIX)
  363. return -EINVAL;
  364. WARN_ON(!list_is_singular(&pdev->dev.msi_list));
  365. desc = list_entry(pdev->dev.msi_list.next, struct msi_desc, list);
  366. irq = assign_irq(nvec, desc, &pos);
  367. if (irq < 0)
  368. return irq;
  369. dw_msi_setup_msg(pp, irq, pos);
  370. return 0;
  371. #else
  372. return -EINVAL;
  373. #endif
  374. }
  375. static void dw_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
  376. {
  377. struct irq_data *data = irq_get_irq_data(irq);
  378. struct msi_desc *msi = irq_data_get_msi_desc(data);
  379. struct pcie_port *pp = (struct pcie_port *) msi_desc_to_pci_sysdata(msi);
  380. clear_irq_range(pp, irq, 1, data->hwirq);
  381. }
  382. static struct msi_controller dw_pcie_msi_chip = {
  383. .setup_irq = dw_msi_setup_irq,
  384. .setup_irqs = dw_msi_setup_irqs,
  385. .teardown_irq = dw_msi_teardown_irq,
  386. };
  387. int dw_pcie_wait_for_link(struct pcie_port *pp)
  388. {
  389. int retries;
  390. /* check if the link is up or not */
  391. for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
  392. if (dw_pcie_link_up(pp)) {
  393. dev_info(pp->dev, "link up\n");
  394. return 0;
  395. }
  396. usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
  397. }
  398. dev_err(pp->dev, "phy link never came up\n");
  399. return -ETIMEDOUT;
  400. }
  401. int dw_pcie_link_up(struct pcie_port *pp)
  402. {
  403. u32 val;
  404. if (pp->ops->link_up)
  405. return pp->ops->link_up(pp);
  406. val = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
  407. return ((val & PCIE_PHY_DEBUG_R1_LINK_UP) &&
  408. (!(val & PCIE_PHY_DEBUG_R1_LINK_IN_TRAINING)));
  409. }
  410. static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  411. irq_hw_number_t hwirq)
  412. {
  413. irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
  414. irq_set_chip_data(irq, domain->host_data);
  415. return 0;
  416. }
  417. static const struct irq_domain_ops msi_domain_ops = {
  418. .map = dw_pcie_msi_map,
  419. };
  420. static u8 dw_pcie_iatu_unroll_enabled(struct pcie_port *pp)
  421. {
  422. u32 val;
  423. val = dw_pcie_readl_rc(pp, PCIE_ATU_VIEWPORT);
  424. if (val == 0xffffffff)
  425. return 1;
  426. return 0;
  427. }
  428. int dw_pcie_host_init(struct pcie_port *pp)
  429. {
  430. struct device_node *np = pp->dev->of_node;
  431. struct platform_device *pdev = to_platform_device(pp->dev);
  432. struct pci_bus *bus, *child;
  433. struct resource *cfg_res;
  434. int i, ret;
  435. LIST_HEAD(res);
  436. struct resource_entry *win, *tmp;
  437. cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
  438. if (cfg_res) {
  439. pp->cfg0_size = resource_size(cfg_res)/2;
  440. pp->cfg1_size = resource_size(cfg_res)/2;
  441. pp->cfg0_base = cfg_res->start;
  442. pp->cfg1_base = cfg_res->start + pp->cfg0_size;
  443. } else if (!pp->va_cfg0_base) {
  444. dev_err(pp->dev, "missing *config* reg space\n");
  445. }
  446. ret = of_pci_get_host_bridge_resources(np, 0, 0xff, &res, &pp->io_base);
  447. if (ret)
  448. return ret;
  449. ret = devm_request_pci_bus_resources(&pdev->dev, &res);
  450. if (ret)
  451. goto error;
  452. /* Get the I/O and memory ranges from DT */
  453. resource_list_for_each_entry_safe(win, tmp, &res) {
  454. switch (resource_type(win->res)) {
  455. case IORESOURCE_IO:
  456. ret = pci_remap_iospace(win->res, pp->io_base);
  457. if (ret) {
  458. dev_warn(pp->dev, "error %d: failed to map resource %pR\n",
  459. ret, win->res);
  460. resource_list_destroy_entry(win);
  461. } else {
  462. pp->io = win->res;
  463. pp->io->name = "I/O";
  464. pp->io_size = resource_size(pp->io);
  465. pp->io_bus_addr = pp->io->start - win->offset;
  466. }
  467. break;
  468. case IORESOURCE_MEM:
  469. pp->mem = win->res;
  470. pp->mem->name = "MEM";
  471. pp->mem_size = resource_size(pp->mem);
  472. pp->mem_bus_addr = pp->mem->start - win->offset;
  473. break;
  474. case 0:
  475. pp->cfg = win->res;
  476. pp->cfg0_size = resource_size(pp->cfg)/2;
  477. pp->cfg1_size = resource_size(pp->cfg)/2;
  478. pp->cfg0_base = pp->cfg->start;
  479. pp->cfg1_base = pp->cfg->start + pp->cfg0_size;
  480. break;
  481. case IORESOURCE_BUS:
  482. pp->busn = win->res;
  483. break;
  484. }
  485. }
  486. if (!pp->dbi_base) {
  487. pp->dbi_base = devm_ioremap(pp->dev, pp->cfg->start,
  488. resource_size(pp->cfg));
  489. if (!pp->dbi_base) {
  490. dev_err(pp->dev, "error with ioremap\n");
  491. ret = -ENOMEM;
  492. goto error;
  493. }
  494. }
  495. pp->mem_base = pp->mem->start;
  496. if (!pp->va_cfg0_base) {
  497. pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
  498. pp->cfg0_size);
  499. if (!pp->va_cfg0_base) {
  500. dev_err(pp->dev, "error with ioremap in function\n");
  501. ret = -ENOMEM;
  502. goto error;
  503. }
  504. }
  505. if (!pp->va_cfg1_base) {
  506. pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
  507. pp->cfg1_size);
  508. if (!pp->va_cfg1_base) {
  509. dev_err(pp->dev, "error with ioremap\n");
  510. ret = -ENOMEM;
  511. goto error;
  512. }
  513. }
  514. ret = of_property_read_u32(np, "num-lanes", &pp->lanes);
  515. if (ret)
  516. pp->lanes = 0;
  517. ret = of_property_read_u32(np, "num-viewport", &pp->num_viewport);
  518. if (ret)
  519. pp->num_viewport = 2;
  520. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  521. if (!pp->ops->msi_host_init) {
  522. pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
  523. MAX_MSI_IRQS, &msi_domain_ops,
  524. &dw_pcie_msi_chip);
  525. if (!pp->irq_domain) {
  526. dev_err(pp->dev, "irq domain init failed\n");
  527. ret = -ENXIO;
  528. goto error;
  529. }
  530. for (i = 0; i < MAX_MSI_IRQS; i++)
  531. irq_create_mapping(pp->irq_domain, i);
  532. } else {
  533. ret = pp->ops->msi_host_init(pp, &dw_pcie_msi_chip);
  534. if (ret < 0)
  535. goto error;
  536. }
  537. }
  538. if (pp->ops->host_init)
  539. pp->ops->host_init(pp);
  540. pp->root_bus_nr = pp->busn->start;
  541. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  542. bus = pci_scan_root_bus_msi(pp->dev, pp->root_bus_nr,
  543. &dw_pcie_ops, pp, &res,
  544. &dw_pcie_msi_chip);
  545. dw_pcie_msi_chip.dev = pp->dev;
  546. } else
  547. bus = pci_scan_root_bus(pp->dev, pp->root_bus_nr, &dw_pcie_ops,
  548. pp, &res);
  549. if (!bus) {
  550. ret = -ENOMEM;
  551. goto error;
  552. }
  553. if (pp->ops->scan_bus)
  554. pp->ops->scan_bus(pp);
  555. #ifdef CONFIG_ARM
  556. /* support old dtbs that incorrectly describe IRQs */
  557. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  558. #endif
  559. pci_bus_size_bridges(bus);
  560. pci_bus_assign_resources(bus);
  561. list_for_each_entry(child, &bus->children, node)
  562. pcie_bus_configure_settings(child);
  563. pci_bus_add_devices(bus);
  564. return 0;
  565. error:
  566. pci_free_resource_list(&res);
  567. return ret;
  568. }
  569. static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  570. u32 devfn, int where, int size, u32 *val)
  571. {
  572. int ret, type;
  573. u32 busdev, cfg_size;
  574. u64 cpu_addr;
  575. void __iomem *va_cfg_base;
  576. if (pp->ops->rd_other_conf)
  577. return pp->ops->rd_other_conf(pp, bus, devfn, where, size, val);
  578. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  579. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  580. if (bus->parent->number == pp->root_bus_nr) {
  581. type = PCIE_ATU_TYPE_CFG0;
  582. cpu_addr = pp->cfg0_base;
  583. cfg_size = pp->cfg0_size;
  584. va_cfg_base = pp->va_cfg0_base;
  585. } else {
  586. type = PCIE_ATU_TYPE_CFG1;
  587. cpu_addr = pp->cfg1_base;
  588. cfg_size = pp->cfg1_size;
  589. va_cfg_base = pp->va_cfg1_base;
  590. }
  591. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
  592. type, cpu_addr,
  593. busdev, cfg_size);
  594. ret = dw_pcie_cfg_read(va_cfg_base + where, size, val);
  595. if (pp->num_viewport <= 2)
  596. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
  597. PCIE_ATU_TYPE_IO, pp->io_base,
  598. pp->io_bus_addr, pp->io_size);
  599. return ret;
  600. }
  601. static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  602. u32 devfn, int where, int size, u32 val)
  603. {
  604. int ret, type;
  605. u32 busdev, cfg_size;
  606. u64 cpu_addr;
  607. void __iomem *va_cfg_base;
  608. if (pp->ops->wr_other_conf)
  609. return pp->ops->wr_other_conf(pp, bus, devfn, where, size, val);
  610. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  611. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  612. if (bus->parent->number == pp->root_bus_nr) {
  613. type = PCIE_ATU_TYPE_CFG0;
  614. cpu_addr = pp->cfg0_base;
  615. cfg_size = pp->cfg0_size;
  616. va_cfg_base = pp->va_cfg0_base;
  617. } else {
  618. type = PCIE_ATU_TYPE_CFG1;
  619. cpu_addr = pp->cfg1_base;
  620. cfg_size = pp->cfg1_size;
  621. va_cfg_base = pp->va_cfg1_base;
  622. }
  623. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
  624. type, cpu_addr,
  625. busdev, cfg_size);
  626. ret = dw_pcie_cfg_write(va_cfg_base + where, size, val);
  627. if (pp->num_viewport <= 2)
  628. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
  629. PCIE_ATU_TYPE_IO, pp->io_base,
  630. pp->io_bus_addr, pp->io_size);
  631. return ret;
  632. }
  633. static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
  634. int dev)
  635. {
  636. /* If there is no link, then there is no device */
  637. if (bus->number != pp->root_bus_nr) {
  638. if (!dw_pcie_link_up(pp))
  639. return 0;
  640. }
  641. /* access only one slot on each root port */
  642. if (bus->number == pp->root_bus_nr && dev > 0)
  643. return 0;
  644. return 1;
  645. }
  646. static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  647. int size, u32 *val)
  648. {
  649. struct pcie_port *pp = bus->sysdata;
  650. if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
  651. *val = 0xffffffff;
  652. return PCIBIOS_DEVICE_NOT_FOUND;
  653. }
  654. if (bus->number == pp->root_bus_nr)
  655. return dw_pcie_rd_own_conf(pp, where, size, val);
  656. return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val);
  657. }
  658. static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  659. int where, int size, u32 val)
  660. {
  661. struct pcie_port *pp = bus->sysdata;
  662. if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
  663. return PCIBIOS_DEVICE_NOT_FOUND;
  664. if (bus->number == pp->root_bus_nr)
  665. return dw_pcie_wr_own_conf(pp, where, size, val);
  666. return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val);
  667. }
  668. static struct pci_ops dw_pcie_ops = {
  669. .read = dw_pcie_rd_conf,
  670. .write = dw_pcie_wr_conf,
  671. };
  672. void dw_pcie_setup_rc(struct pcie_port *pp)
  673. {
  674. u32 val;
  675. /* set the number of lanes */
  676. val = dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL);
  677. val &= ~PORT_LINK_MODE_MASK;
  678. switch (pp->lanes) {
  679. case 1:
  680. val |= PORT_LINK_MODE_1_LANES;
  681. break;
  682. case 2:
  683. val |= PORT_LINK_MODE_2_LANES;
  684. break;
  685. case 4:
  686. val |= PORT_LINK_MODE_4_LANES;
  687. break;
  688. case 8:
  689. val |= PORT_LINK_MODE_8_LANES;
  690. break;
  691. default:
  692. dev_err(pp->dev, "num-lanes %u: invalid value\n", pp->lanes);
  693. return;
  694. }
  695. dw_pcie_writel_rc(pp, PCIE_PORT_LINK_CONTROL, val);
  696. /* set link width speed control register */
  697. val = dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL);
  698. val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
  699. switch (pp->lanes) {
  700. case 1:
  701. val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
  702. break;
  703. case 2:
  704. val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
  705. break;
  706. case 4:
  707. val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
  708. break;
  709. case 8:
  710. val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
  711. break;
  712. }
  713. dw_pcie_writel_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
  714. /* setup RC BARs */
  715. dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_0, 0x00000004);
  716. dw_pcie_writel_rc(pp, PCI_BASE_ADDRESS_1, 0x00000000);
  717. /* setup interrupt pins */
  718. val = dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE);
  719. val &= 0xffff00ff;
  720. val |= 0x00000100;
  721. dw_pcie_writel_rc(pp, PCI_INTERRUPT_LINE, val);
  722. /* setup bus numbers */
  723. val = dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS);
  724. val &= 0xff000000;
  725. val |= 0x00010100;
  726. dw_pcie_writel_rc(pp, PCI_PRIMARY_BUS, val);
  727. /* setup command register */
  728. val = dw_pcie_readl_rc(pp, PCI_COMMAND);
  729. val &= 0xffff0000;
  730. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  731. PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
  732. dw_pcie_writel_rc(pp, PCI_COMMAND, val);
  733. /*
  734. * If the platform provides ->rd_other_conf, it means the platform
  735. * uses its own address translation component rather than ATU, so
  736. * we should not program the ATU here.
  737. */
  738. if (!pp->ops->rd_other_conf) {
  739. /* get iATU unroll support */
  740. pp->iatu_unroll_enabled = dw_pcie_iatu_unroll_enabled(pp);
  741. dev_dbg(pp->dev, "iATU unroll: %s\n",
  742. pp->iatu_unroll_enabled ? "enabled" : "disabled");
  743. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
  744. PCIE_ATU_TYPE_MEM, pp->mem_base,
  745. pp->mem_bus_addr, pp->mem_size);
  746. if (pp->num_viewport > 2)
  747. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX2,
  748. PCIE_ATU_TYPE_IO, pp->io_base,
  749. pp->io_bus_addr, pp->io_size);
  750. }
  751. dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
  752. /* program correct class for RC */
  753. dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
  754. dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
  755. val |= PORT_LOGIC_SPEED_CHANGE;
  756. dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
  757. }