pcie-designware.c 22 KB

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