pcie-designware.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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/module.h>
  17. #include <linux/msi.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/pci.h>
  21. #include <linux/pci_regs.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/types.h>
  24. #include "pcie-designware.h"
  25. /* Synopsis specific PCIE configuration registers */
  26. #define PCIE_PORT_LINK_CONTROL 0x710
  27. #define PORT_LINK_MODE_MASK (0x3f << 16)
  28. #define PORT_LINK_MODE_1_LANES (0x1 << 16)
  29. #define PORT_LINK_MODE_2_LANES (0x3 << 16)
  30. #define PORT_LINK_MODE_4_LANES (0x7 << 16)
  31. #define PORT_LINK_MODE_8_LANES (0xf << 16)
  32. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  33. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  34. #define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
  35. #define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
  36. #define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
  37. #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
  38. #define PORT_LOGIC_LINK_WIDTH_8_LANES (0x8 << 8)
  39. #define PCIE_MSI_ADDR_LO 0x820
  40. #define PCIE_MSI_ADDR_HI 0x824
  41. #define PCIE_MSI_INTR0_ENABLE 0x828
  42. #define PCIE_MSI_INTR0_MASK 0x82C
  43. #define PCIE_MSI_INTR0_STATUS 0x830
  44. #define PCIE_ATU_VIEWPORT 0x900
  45. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  46. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  47. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  48. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  49. #define PCIE_ATU_CR1 0x904
  50. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  51. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  52. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  53. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  54. #define PCIE_ATU_CR2 0x908
  55. #define PCIE_ATU_ENABLE (0x1 << 31)
  56. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  57. #define PCIE_ATU_LOWER_BASE 0x90C
  58. #define PCIE_ATU_UPPER_BASE 0x910
  59. #define PCIE_ATU_LIMIT 0x914
  60. #define PCIE_ATU_LOWER_TARGET 0x918
  61. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  62. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  63. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  64. #define PCIE_ATU_UPPER_TARGET 0x91C
  65. static struct hw_pci dw_pci;
  66. static unsigned long global_io_offset;
  67. static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
  68. {
  69. BUG_ON(!sys->private_data);
  70. return sys->private_data;
  71. }
  72. int dw_pcie_cfg_read(void __iomem *addr, int where, int size, u32 *val)
  73. {
  74. *val = readl(addr);
  75. if (size == 1)
  76. *val = (*val >> (8 * (where & 3))) & 0xff;
  77. else if (size == 2)
  78. *val = (*val >> (8 * (where & 3))) & 0xffff;
  79. else if (size != 4)
  80. return PCIBIOS_BAD_REGISTER_NUMBER;
  81. return PCIBIOS_SUCCESSFUL;
  82. }
  83. int dw_pcie_cfg_write(void __iomem *addr, int where, int size, u32 val)
  84. {
  85. if (size == 4)
  86. writel(val, addr);
  87. else if (size == 2)
  88. writew(val, addr + (where & 2));
  89. else if (size == 1)
  90. writeb(val, addr + (where & 3));
  91. else
  92. return PCIBIOS_BAD_REGISTER_NUMBER;
  93. return PCIBIOS_SUCCESSFUL;
  94. }
  95. static inline void dw_pcie_readl_rc(struct pcie_port *pp, u32 reg, u32 *val)
  96. {
  97. if (pp->ops->readl_rc)
  98. pp->ops->readl_rc(pp, pp->dbi_base + reg, val);
  99. else
  100. *val = readl(pp->dbi_base + reg);
  101. }
  102. static inline void dw_pcie_writel_rc(struct pcie_port *pp, u32 val, u32 reg)
  103. {
  104. if (pp->ops->writel_rc)
  105. pp->ops->writel_rc(pp, val, pp->dbi_base + reg);
  106. else
  107. writel(val, pp->dbi_base + reg);
  108. }
  109. static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  110. u32 *val)
  111. {
  112. int ret;
  113. if (pp->ops->rd_own_conf)
  114. ret = pp->ops->rd_own_conf(pp, where, size, val);
  115. else
  116. ret = dw_pcie_cfg_read(pp->dbi_base + (where & ~0x3), where,
  117. size, val);
  118. return ret;
  119. }
  120. static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  121. u32 val)
  122. {
  123. int ret;
  124. if (pp->ops->wr_own_conf)
  125. ret = pp->ops->wr_own_conf(pp, where, size, val);
  126. else
  127. ret = dw_pcie_cfg_write(pp->dbi_base + (where & ~0x3), where,
  128. size, val);
  129. return ret;
  130. }
  131. static void dw_pcie_prog_outbound_atu(struct pcie_port *pp, int index,
  132. int type, u64 cpu_addr, u64 pci_addr, u32 size)
  133. {
  134. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | index,
  135. PCIE_ATU_VIEWPORT);
  136. dw_pcie_writel_rc(pp, lower_32_bits(cpu_addr), PCIE_ATU_LOWER_BASE);
  137. dw_pcie_writel_rc(pp, upper_32_bits(cpu_addr), PCIE_ATU_UPPER_BASE);
  138. dw_pcie_writel_rc(pp, lower_32_bits(cpu_addr + size - 1),
  139. PCIE_ATU_LIMIT);
  140. dw_pcie_writel_rc(pp, lower_32_bits(pci_addr), PCIE_ATU_LOWER_TARGET);
  141. dw_pcie_writel_rc(pp, upper_32_bits(pci_addr), PCIE_ATU_UPPER_TARGET);
  142. dw_pcie_writel_rc(pp, type, PCIE_ATU_CR1);
  143. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  144. }
  145. static struct irq_chip dw_msi_irq_chip = {
  146. .name = "PCI-MSI",
  147. .irq_enable = pci_msi_unmask_irq,
  148. .irq_disable = pci_msi_mask_irq,
  149. .irq_mask = pci_msi_mask_irq,
  150. .irq_unmask = pci_msi_unmask_irq,
  151. };
  152. /* MSI int handler */
  153. irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
  154. {
  155. unsigned long val;
  156. int i, pos, irq;
  157. irqreturn_t ret = IRQ_NONE;
  158. for (i = 0; i < MAX_MSI_CTRLS; i++) {
  159. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
  160. (u32 *)&val);
  161. if (val) {
  162. ret = IRQ_HANDLED;
  163. pos = 0;
  164. while ((pos = find_next_bit(&val, 32, pos)) != 32) {
  165. irq = irq_find_mapping(pp->irq_domain,
  166. i * 32 + pos);
  167. dw_pcie_wr_own_conf(pp,
  168. PCIE_MSI_INTR0_STATUS + i * 12,
  169. 4, 1 << pos);
  170. generic_handle_irq(irq);
  171. pos++;
  172. }
  173. }
  174. }
  175. return ret;
  176. }
  177. void dw_pcie_msi_init(struct pcie_port *pp)
  178. {
  179. pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
  180. /* program the msi_data */
  181. dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
  182. virt_to_phys((void *)pp->msi_data));
  183. dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, 0);
  184. }
  185. static void dw_pcie_msi_clear_irq(struct pcie_port *pp, int irq)
  186. {
  187. unsigned int res, bit, val;
  188. res = (irq / 32) * 12;
  189. bit = irq % 32;
  190. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
  191. val &= ~(1 << bit);
  192. dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
  193. }
  194. static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base,
  195. unsigned int nvec, unsigned int pos)
  196. {
  197. unsigned int i;
  198. for (i = 0; i < nvec; i++) {
  199. irq_set_msi_desc_off(irq_base, i, NULL);
  200. /* Disable corresponding interrupt on MSI controller */
  201. if (pp->ops->msi_clear_irq)
  202. pp->ops->msi_clear_irq(pp, pos + i);
  203. else
  204. dw_pcie_msi_clear_irq(pp, pos + i);
  205. }
  206. bitmap_release_region(pp->msi_irq_in_use, pos, order_base_2(nvec));
  207. }
  208. static void dw_pcie_msi_set_irq(struct pcie_port *pp, int irq)
  209. {
  210. unsigned int res, bit, val;
  211. res = (irq / 32) * 12;
  212. bit = irq % 32;
  213. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
  214. val |= 1 << bit;
  215. dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
  216. }
  217. static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
  218. {
  219. int irq, pos0, i;
  220. struct pcie_port *pp = sys_to_pcie(msi_desc_to_pci_sysdata(desc));
  221. pos0 = bitmap_find_free_region(pp->msi_irq_in_use, MAX_MSI_IRQS,
  222. order_base_2(no_irqs));
  223. if (pos0 < 0)
  224. goto no_valid_irq;
  225. irq = irq_find_mapping(pp->irq_domain, pos0);
  226. if (!irq)
  227. goto no_valid_irq;
  228. /*
  229. * irq_create_mapping (called from dw_pcie_host_init) pre-allocates
  230. * descs so there is no need to allocate descs here. We can therefore
  231. * assume that if irq_find_mapping above returns non-zero, then the
  232. * descs are also successfully allocated.
  233. */
  234. for (i = 0; i < no_irqs; i++) {
  235. if (irq_set_msi_desc_off(irq, i, desc) != 0) {
  236. clear_irq_range(pp, irq, i, pos0);
  237. goto no_valid_irq;
  238. }
  239. /*Enable corresponding interrupt in MSI interrupt controller */
  240. if (pp->ops->msi_set_irq)
  241. pp->ops->msi_set_irq(pp, pos0 + i);
  242. else
  243. dw_pcie_msi_set_irq(pp, pos0 + i);
  244. }
  245. *pos = pos0;
  246. return irq;
  247. no_valid_irq:
  248. *pos = pos0;
  249. return -ENOSPC;
  250. }
  251. static int dw_msi_setup_irq(struct msi_controller *chip, struct pci_dev *pdev,
  252. struct msi_desc *desc)
  253. {
  254. int irq, pos;
  255. struct msi_msg msg;
  256. struct pcie_port *pp = sys_to_pcie(pdev->bus->sysdata);
  257. if (desc->msi_attrib.is_msix)
  258. return -EINVAL;
  259. irq = assign_irq(1, desc, &pos);
  260. if (irq < 0)
  261. return irq;
  262. if (pp->ops->get_msi_addr)
  263. msg.address_lo = pp->ops->get_msi_addr(pp);
  264. else
  265. msg.address_lo = virt_to_phys((void *)pp->msi_data);
  266. msg.address_hi = 0x0;
  267. if (pp->ops->get_msi_data)
  268. msg.data = pp->ops->get_msi_data(pp, pos);
  269. else
  270. msg.data = pos;
  271. pci_write_msi_msg(irq, &msg);
  272. return 0;
  273. }
  274. static void dw_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
  275. {
  276. struct irq_data *data = irq_get_irq_data(irq);
  277. struct msi_desc *msi = irq_data_get_msi_desc(data);
  278. struct pcie_port *pp = sys_to_pcie(msi_desc_to_pci_sysdata(msi));
  279. clear_irq_range(pp, irq, 1, data->hwirq);
  280. }
  281. static struct msi_controller dw_pcie_msi_chip = {
  282. .setup_irq = dw_msi_setup_irq,
  283. .teardown_irq = dw_msi_teardown_irq,
  284. };
  285. int dw_pcie_link_up(struct pcie_port *pp)
  286. {
  287. if (pp->ops->link_up)
  288. return pp->ops->link_up(pp);
  289. else
  290. return 0;
  291. }
  292. static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  293. irq_hw_number_t hwirq)
  294. {
  295. irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
  296. irq_set_chip_data(irq, domain->host_data);
  297. return 0;
  298. }
  299. static const struct irq_domain_ops msi_domain_ops = {
  300. .map = dw_pcie_msi_map,
  301. };
  302. int dw_pcie_host_init(struct pcie_port *pp)
  303. {
  304. struct device_node *np = pp->dev->of_node;
  305. struct platform_device *pdev = to_platform_device(pp->dev);
  306. struct of_pci_range range;
  307. struct of_pci_range_parser parser;
  308. struct resource *cfg_res;
  309. u32 val, na, ns;
  310. const __be32 *addrp;
  311. int i, index, ret;
  312. /* Find the address cell size and the number of cells in order to get
  313. * the untranslated address.
  314. */
  315. of_property_read_u32(np, "#address-cells", &na);
  316. ns = of_n_size_cells(np);
  317. cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
  318. if (cfg_res) {
  319. pp->cfg0_size = resource_size(cfg_res)/2;
  320. pp->cfg1_size = resource_size(cfg_res)/2;
  321. pp->cfg0_base = cfg_res->start;
  322. pp->cfg1_base = cfg_res->start + pp->cfg0_size;
  323. /* Find the untranslated configuration space address */
  324. index = of_property_match_string(np, "reg-names", "config");
  325. addrp = of_get_address(np, index, NULL, NULL);
  326. pp->cfg0_mod_base = of_read_number(addrp, ns);
  327. pp->cfg1_mod_base = pp->cfg0_mod_base + pp->cfg0_size;
  328. } else if (!pp->va_cfg0_base) {
  329. dev_err(pp->dev, "missing *config* reg space\n");
  330. }
  331. if (of_pci_range_parser_init(&parser, np)) {
  332. dev_err(pp->dev, "missing ranges property\n");
  333. return -EINVAL;
  334. }
  335. /* Get the I/O and memory ranges from DT */
  336. for_each_of_pci_range(&parser, &range) {
  337. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  338. if (restype == IORESOURCE_IO) {
  339. of_pci_range_to_resource(&range, np, &pp->io);
  340. pp->io.name = "I/O";
  341. pp->io.start = max_t(resource_size_t,
  342. PCIBIOS_MIN_IO,
  343. range.pci_addr + global_io_offset);
  344. pp->io.end = min_t(resource_size_t,
  345. IO_SPACE_LIMIT,
  346. range.pci_addr + range.size
  347. + global_io_offset - 1);
  348. pp->io_size = resource_size(&pp->io);
  349. pp->io_bus_addr = range.pci_addr;
  350. pp->io_base = range.cpu_addr;
  351. /* Find the untranslated IO space address */
  352. pp->io_mod_base = of_read_number(parser.range -
  353. parser.np + na, ns);
  354. }
  355. if (restype == IORESOURCE_MEM) {
  356. of_pci_range_to_resource(&range, np, &pp->mem);
  357. pp->mem.name = "MEM";
  358. pp->mem_size = resource_size(&pp->mem);
  359. pp->mem_bus_addr = range.pci_addr;
  360. /* Find the untranslated MEM space address */
  361. pp->mem_mod_base = of_read_number(parser.range -
  362. parser.np + na, ns);
  363. }
  364. if (restype == 0) {
  365. of_pci_range_to_resource(&range, np, &pp->cfg);
  366. pp->cfg0_size = resource_size(&pp->cfg)/2;
  367. pp->cfg1_size = resource_size(&pp->cfg)/2;
  368. pp->cfg0_base = pp->cfg.start;
  369. pp->cfg1_base = pp->cfg.start + pp->cfg0_size;
  370. /* Find the untranslated configuration space address */
  371. pp->cfg0_mod_base = of_read_number(parser.range -
  372. parser.np + na, ns);
  373. pp->cfg1_mod_base = pp->cfg0_mod_base +
  374. pp->cfg0_size;
  375. }
  376. }
  377. ret = of_pci_parse_bus_range(np, &pp->busn);
  378. if (ret < 0) {
  379. pp->busn.name = np->name;
  380. pp->busn.start = 0;
  381. pp->busn.end = 0xff;
  382. pp->busn.flags = IORESOURCE_BUS;
  383. dev_dbg(pp->dev, "failed to parse bus-range property: %d, using default %pR\n",
  384. ret, &pp->busn);
  385. }
  386. if (!pp->dbi_base) {
  387. pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
  388. resource_size(&pp->cfg));
  389. if (!pp->dbi_base) {
  390. dev_err(pp->dev, "error with ioremap\n");
  391. return -ENOMEM;
  392. }
  393. }
  394. pp->mem_base = pp->mem.start;
  395. if (!pp->va_cfg0_base) {
  396. pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
  397. pp->cfg0_size);
  398. if (!pp->va_cfg0_base) {
  399. dev_err(pp->dev, "error with ioremap in function\n");
  400. return -ENOMEM;
  401. }
  402. }
  403. if (!pp->va_cfg1_base) {
  404. pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
  405. pp->cfg1_size);
  406. if (!pp->va_cfg1_base) {
  407. dev_err(pp->dev, "error with ioremap\n");
  408. return -ENOMEM;
  409. }
  410. }
  411. if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
  412. dev_err(pp->dev, "Failed to parse the number of lanes\n");
  413. return -EINVAL;
  414. }
  415. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  416. if (!pp->ops->msi_host_init) {
  417. pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
  418. MAX_MSI_IRQS, &msi_domain_ops,
  419. &dw_pcie_msi_chip);
  420. if (!pp->irq_domain) {
  421. dev_err(pp->dev, "irq domain init failed\n");
  422. return -ENXIO;
  423. }
  424. for (i = 0; i < MAX_MSI_IRQS; i++)
  425. irq_create_mapping(pp->irq_domain, i);
  426. } else {
  427. ret = pp->ops->msi_host_init(pp, &dw_pcie_msi_chip);
  428. if (ret < 0)
  429. return ret;
  430. }
  431. }
  432. if (pp->ops->host_init)
  433. pp->ops->host_init(pp);
  434. if (!pp->ops->rd_other_conf)
  435. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX1,
  436. PCIE_ATU_TYPE_MEM, pp->mem_mod_base,
  437. pp->mem_bus_addr, pp->mem_size);
  438. dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
  439. /* program correct class for RC */
  440. dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
  441. dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
  442. val |= PORT_LOGIC_SPEED_CHANGE;
  443. dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
  444. #ifdef CONFIG_PCI_MSI
  445. dw_pcie_msi_chip.dev = pp->dev;
  446. #endif
  447. dw_pci.nr_controllers = 1;
  448. dw_pci.private_data = (void **)&pp;
  449. pci_common_init_dev(pp->dev, &dw_pci);
  450. return 0;
  451. }
  452. static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  453. u32 devfn, int where, int size, u32 *val)
  454. {
  455. int ret, type;
  456. u32 address, busdev, cfg_size;
  457. u64 cpu_addr;
  458. void __iomem *va_cfg_base;
  459. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  460. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  461. address = where & ~0x3;
  462. if (bus->parent->number == pp->root_bus_nr) {
  463. type = PCIE_ATU_TYPE_CFG0;
  464. cpu_addr = pp->cfg0_mod_base;
  465. cfg_size = pp->cfg0_size;
  466. va_cfg_base = pp->va_cfg0_base;
  467. } else {
  468. type = PCIE_ATU_TYPE_CFG1;
  469. cpu_addr = pp->cfg1_mod_base;
  470. cfg_size = pp->cfg1_size;
  471. va_cfg_base = pp->va_cfg1_base;
  472. }
  473. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
  474. type, cpu_addr,
  475. busdev, cfg_size);
  476. ret = dw_pcie_cfg_read(va_cfg_base + address, where, size, val);
  477. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
  478. PCIE_ATU_TYPE_IO, pp->io_mod_base,
  479. pp->io_bus_addr, pp->io_size);
  480. return ret;
  481. }
  482. static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  483. u32 devfn, int where, int size, u32 val)
  484. {
  485. int ret, type;
  486. u32 address, busdev, cfg_size;
  487. u64 cpu_addr;
  488. void __iomem *va_cfg_base;
  489. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  490. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  491. address = where & ~0x3;
  492. if (bus->parent->number == pp->root_bus_nr) {
  493. type = PCIE_ATU_TYPE_CFG0;
  494. cpu_addr = pp->cfg0_mod_base;
  495. cfg_size = pp->cfg0_size;
  496. va_cfg_base = pp->va_cfg0_base;
  497. } else {
  498. type = PCIE_ATU_TYPE_CFG1;
  499. cpu_addr = pp->cfg1_mod_base;
  500. cfg_size = pp->cfg1_size;
  501. va_cfg_base = pp->va_cfg1_base;
  502. }
  503. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
  504. type, cpu_addr,
  505. busdev, cfg_size);
  506. ret = dw_pcie_cfg_write(va_cfg_base + address, where, size, val);
  507. dw_pcie_prog_outbound_atu(pp, PCIE_ATU_REGION_INDEX0,
  508. PCIE_ATU_TYPE_IO, pp->io_mod_base,
  509. pp->io_bus_addr, pp->io_size);
  510. return ret;
  511. }
  512. static int dw_pcie_valid_config(struct pcie_port *pp,
  513. struct pci_bus *bus, int dev)
  514. {
  515. /* If there is no link, then there is no device */
  516. if (bus->number != pp->root_bus_nr) {
  517. if (!dw_pcie_link_up(pp))
  518. return 0;
  519. }
  520. /* access only one slot on each root port */
  521. if (bus->number == pp->root_bus_nr && dev > 0)
  522. return 0;
  523. /*
  524. * do not read more than one device on the bus directly attached
  525. * to RC's (Virtual Bridge's) DS side.
  526. */
  527. if (bus->primary == pp->root_bus_nr && dev > 0)
  528. return 0;
  529. return 1;
  530. }
  531. static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  532. int size, u32 *val)
  533. {
  534. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  535. int ret;
  536. if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
  537. *val = 0xffffffff;
  538. return PCIBIOS_DEVICE_NOT_FOUND;
  539. }
  540. if (bus->number != pp->root_bus_nr)
  541. if (pp->ops->rd_other_conf)
  542. ret = pp->ops->rd_other_conf(pp, bus, devfn,
  543. where, size, val);
  544. else
  545. ret = dw_pcie_rd_other_conf(pp, bus, devfn,
  546. where, size, val);
  547. else
  548. ret = dw_pcie_rd_own_conf(pp, where, size, val);
  549. return ret;
  550. }
  551. static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  552. int where, int size, u32 val)
  553. {
  554. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  555. int ret;
  556. if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
  557. return PCIBIOS_DEVICE_NOT_FOUND;
  558. if (bus->number != pp->root_bus_nr)
  559. if (pp->ops->wr_other_conf)
  560. ret = pp->ops->wr_other_conf(pp, bus, devfn,
  561. where, size, val);
  562. else
  563. ret = dw_pcie_wr_other_conf(pp, bus, devfn,
  564. where, size, val);
  565. else
  566. ret = dw_pcie_wr_own_conf(pp, where, size, val);
  567. return ret;
  568. }
  569. static struct pci_ops dw_pcie_ops = {
  570. .read = dw_pcie_rd_conf,
  571. .write = dw_pcie_wr_conf,
  572. };
  573. static int dw_pcie_setup(int nr, struct pci_sys_data *sys)
  574. {
  575. struct pcie_port *pp;
  576. pp = sys_to_pcie(sys);
  577. if (global_io_offset < SZ_1M && pp->io_size > 0) {
  578. sys->io_offset = global_io_offset - pp->io_bus_addr;
  579. pci_ioremap_io(global_io_offset, pp->io_base);
  580. global_io_offset += SZ_64K;
  581. pci_add_resource_offset(&sys->resources, &pp->io,
  582. sys->io_offset);
  583. }
  584. sys->mem_offset = pp->mem.start - pp->mem_bus_addr;
  585. pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
  586. pci_add_resource(&sys->resources, &pp->busn);
  587. return 1;
  588. }
  589. static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  590. {
  591. struct pci_bus *bus;
  592. struct pcie_port *pp = sys_to_pcie(sys);
  593. pp->root_bus_nr = sys->busnr;
  594. if (IS_ENABLED(CONFIG_PCI_MSI))
  595. bus = pci_scan_root_bus_msi(pp->dev, sys->busnr, &dw_pcie_ops,
  596. sys, &sys->resources,
  597. &dw_pcie_msi_chip);
  598. else
  599. bus = pci_scan_root_bus(pp->dev, sys->busnr, &dw_pcie_ops,
  600. sys, &sys->resources);
  601. if (!bus)
  602. return NULL;
  603. if (bus && pp->ops->scan_bus)
  604. pp->ops->scan_bus(pp);
  605. return bus;
  606. }
  607. static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  608. {
  609. struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
  610. int irq;
  611. irq = of_irq_parse_and_map_pci(dev, slot, pin);
  612. if (!irq)
  613. irq = pp->irq;
  614. return irq;
  615. }
  616. static struct hw_pci dw_pci = {
  617. .setup = dw_pcie_setup,
  618. .scan = dw_pcie_scan_bus,
  619. .map_irq = dw_pcie_map_irq,
  620. };
  621. void dw_pcie_setup_rc(struct pcie_port *pp)
  622. {
  623. u32 val;
  624. u32 membase;
  625. u32 memlimit;
  626. /* set the number of lanes */
  627. dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
  628. val &= ~PORT_LINK_MODE_MASK;
  629. switch (pp->lanes) {
  630. case 1:
  631. val |= PORT_LINK_MODE_1_LANES;
  632. break;
  633. case 2:
  634. val |= PORT_LINK_MODE_2_LANES;
  635. break;
  636. case 4:
  637. val |= PORT_LINK_MODE_4_LANES;
  638. break;
  639. case 8:
  640. val |= PORT_LINK_MODE_8_LANES;
  641. break;
  642. }
  643. dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
  644. /* set link width speed control register */
  645. dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
  646. val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
  647. switch (pp->lanes) {
  648. case 1:
  649. val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
  650. break;
  651. case 2:
  652. val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
  653. break;
  654. case 4:
  655. val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
  656. break;
  657. case 8:
  658. val |= PORT_LOGIC_LINK_WIDTH_8_LANES;
  659. break;
  660. }
  661. dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
  662. /* setup RC BARs */
  663. dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
  664. dw_pcie_writel_rc(pp, 0x00000000, PCI_BASE_ADDRESS_1);
  665. /* setup interrupt pins */
  666. dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
  667. val &= 0xffff00ff;
  668. val |= 0x00000100;
  669. dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
  670. /* setup bus numbers */
  671. dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
  672. val &= 0xff000000;
  673. val |= 0x00010100;
  674. dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
  675. /* setup memory base, memory limit */
  676. membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
  677. memlimit = (pp->mem_size + (u32)pp->mem_base) & 0xfff00000;
  678. val = memlimit | membase;
  679. dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE);
  680. /* setup command register */
  681. dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
  682. val &= 0xffff0000;
  683. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  684. PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
  685. dw_pcie_writel_rc(pp, val, PCI_COMMAND);
  686. }
  687. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  688. MODULE_DESCRIPTION("Designware PCIe host controller driver");
  689. MODULE_LICENSE("GPL v2");