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. irq = assign_irq(1, desc, &pos);
  242. if (irq < 0)
  243. return irq;
  244. if (pp->ops->get_msi_addr)
  245. msg.address_lo = pp->ops->get_msi_addr(pp);
  246. else
  247. msg.address_lo = virt_to_phys((void *)pp->msi_data);
  248. msg.address_hi = 0x0;
  249. if (pp->ops->get_msi_data)
  250. msg.data = pp->ops->get_msi_data(pp, pos);
  251. else
  252. msg.data = pos;
  253. pci_write_msi_msg(irq, &msg);
  254. return 0;
  255. }
  256. static void dw_msi_teardown_irq(struct msi_controller *chip, unsigned int irq)
  257. {
  258. struct irq_data *data = irq_get_irq_data(irq);
  259. struct msi_desc *msi = irq_data_get_msi(data);
  260. struct pcie_port *pp = sys_to_pcie(msi->dev->bus->sysdata);
  261. clear_irq_range(pp, irq, 1, data->hwirq);
  262. }
  263. static struct msi_controller dw_pcie_msi_chip = {
  264. .setup_irq = dw_msi_setup_irq,
  265. .teardown_irq = dw_msi_teardown_irq,
  266. };
  267. int dw_pcie_link_up(struct pcie_port *pp)
  268. {
  269. if (pp->ops->link_up)
  270. return pp->ops->link_up(pp);
  271. else
  272. return 0;
  273. }
  274. static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  275. irq_hw_number_t hwirq)
  276. {
  277. irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
  278. irq_set_chip_data(irq, domain->host_data);
  279. set_irq_flags(irq, IRQF_VALID);
  280. return 0;
  281. }
  282. static const struct irq_domain_ops msi_domain_ops = {
  283. .map = dw_pcie_msi_map,
  284. };
  285. int __init dw_pcie_host_init(struct pcie_port *pp)
  286. {
  287. struct device_node *np = pp->dev->of_node;
  288. struct platform_device *pdev = to_platform_device(pp->dev);
  289. struct of_pci_range range;
  290. struct of_pci_range_parser parser;
  291. struct resource *cfg_res;
  292. u32 val, na, ns;
  293. const __be32 *addrp;
  294. int i, index, ret;
  295. /* Find the address cell size and the number of cells in order to get
  296. * the untranslated address.
  297. */
  298. of_property_read_u32(np, "#address-cells", &na);
  299. ns = of_n_size_cells(np);
  300. cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
  301. if (cfg_res) {
  302. pp->cfg0_size = resource_size(cfg_res)/2;
  303. pp->cfg1_size = resource_size(cfg_res)/2;
  304. pp->cfg0_base = cfg_res->start;
  305. pp->cfg1_base = cfg_res->start + pp->cfg0_size;
  306. /* Find the untranslated configuration space address */
  307. index = of_property_match_string(np, "reg-names", "config");
  308. addrp = of_get_address(np, index, NULL, NULL);
  309. pp->cfg0_mod_base = of_read_number(addrp, ns);
  310. pp->cfg1_mod_base = pp->cfg0_mod_base + pp->cfg0_size;
  311. } else {
  312. dev_err(pp->dev, "missing *config* reg space\n");
  313. }
  314. if (of_pci_range_parser_init(&parser, np)) {
  315. dev_err(pp->dev, "missing ranges property\n");
  316. return -EINVAL;
  317. }
  318. /* Get the I/O and memory ranges from DT */
  319. for_each_of_pci_range(&parser, &range) {
  320. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  321. if (restype == IORESOURCE_IO) {
  322. of_pci_range_to_resource(&range, np, &pp->io);
  323. pp->io.name = "I/O";
  324. pp->io.start = max_t(resource_size_t,
  325. PCIBIOS_MIN_IO,
  326. range.pci_addr + global_io_offset);
  327. pp->io.end = min_t(resource_size_t,
  328. IO_SPACE_LIMIT,
  329. range.pci_addr + range.size
  330. + global_io_offset - 1);
  331. pp->io_size = resource_size(&pp->io);
  332. pp->io_bus_addr = range.pci_addr;
  333. pp->io_base = range.cpu_addr;
  334. /* Find the untranslated IO space address */
  335. pp->io_mod_base = of_read_number(parser.range -
  336. parser.np + na, ns);
  337. }
  338. if (restype == IORESOURCE_MEM) {
  339. of_pci_range_to_resource(&range, np, &pp->mem);
  340. pp->mem.name = "MEM";
  341. pp->mem_size = resource_size(&pp->mem);
  342. pp->mem_bus_addr = range.pci_addr;
  343. /* Find the untranslated MEM space address */
  344. pp->mem_mod_base = of_read_number(parser.range -
  345. parser.np + na, ns);
  346. }
  347. if (restype == 0) {
  348. of_pci_range_to_resource(&range, np, &pp->cfg);
  349. pp->cfg0_size = resource_size(&pp->cfg)/2;
  350. pp->cfg1_size = resource_size(&pp->cfg)/2;
  351. pp->cfg0_base = pp->cfg.start;
  352. pp->cfg1_base = pp->cfg.start + pp->cfg0_size;
  353. /* Find the untranslated configuration space address */
  354. pp->cfg0_mod_base = of_read_number(parser.range -
  355. parser.np + na, ns);
  356. pp->cfg1_mod_base = pp->cfg0_mod_base +
  357. pp->cfg0_size;
  358. }
  359. }
  360. ret = of_pci_parse_bus_range(np, &pp->busn);
  361. if (ret < 0) {
  362. pp->busn.name = np->name;
  363. pp->busn.start = 0;
  364. pp->busn.end = 0xff;
  365. pp->busn.flags = IORESOURCE_BUS;
  366. dev_dbg(pp->dev, "failed to parse bus-range property: %d, using default %pR\n",
  367. ret, &pp->busn);
  368. }
  369. if (!pp->dbi_base) {
  370. pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
  371. resource_size(&pp->cfg));
  372. if (!pp->dbi_base) {
  373. dev_err(pp->dev, "error with ioremap\n");
  374. return -ENOMEM;
  375. }
  376. }
  377. pp->mem_base = pp->mem.start;
  378. if (!pp->va_cfg0_base) {
  379. pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
  380. pp->cfg0_size);
  381. if (!pp->va_cfg0_base) {
  382. dev_err(pp->dev, "error with ioremap in function\n");
  383. return -ENOMEM;
  384. }
  385. }
  386. if (!pp->va_cfg1_base) {
  387. pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
  388. pp->cfg1_size);
  389. if (!pp->va_cfg1_base) {
  390. dev_err(pp->dev, "error with ioremap\n");
  391. return -ENOMEM;
  392. }
  393. }
  394. if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
  395. dev_err(pp->dev, "Failed to parse the number of lanes\n");
  396. return -EINVAL;
  397. }
  398. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  399. if (!pp->ops->msi_host_init) {
  400. pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
  401. MAX_MSI_IRQS, &msi_domain_ops,
  402. &dw_pcie_msi_chip);
  403. if (!pp->irq_domain) {
  404. dev_err(pp->dev, "irq domain init failed\n");
  405. return -ENXIO;
  406. }
  407. for (i = 0; i < MAX_MSI_IRQS; i++)
  408. irq_create_mapping(pp->irq_domain, i);
  409. } else {
  410. ret = pp->ops->msi_host_init(pp, &dw_pcie_msi_chip);
  411. if (ret < 0)
  412. return ret;
  413. }
  414. }
  415. if (pp->ops->host_init)
  416. pp->ops->host_init(pp);
  417. dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
  418. /* program correct class for RC */
  419. dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
  420. dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
  421. val |= PORT_LOGIC_SPEED_CHANGE;
  422. dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
  423. #ifdef CONFIG_PCI_MSI
  424. dw_pcie_msi_chip.dev = pp->dev;
  425. dw_pci.msi_ctrl = &dw_pcie_msi_chip;
  426. #endif
  427. dw_pci.nr_controllers = 1;
  428. dw_pci.private_data = (void **)&pp;
  429. pci_common_init_dev(pp->dev, &dw_pci);
  430. #ifdef CONFIG_PCI_DOMAINS
  431. dw_pci.domain++;
  432. #endif
  433. return 0;
  434. }
  435. static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
  436. {
  437. /* Program viewport 0 : OUTBOUND : CFG0 */
  438. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  439. PCIE_ATU_VIEWPORT);
  440. dw_pcie_writel_rc(pp, pp->cfg0_mod_base, PCIE_ATU_LOWER_BASE);
  441. dw_pcie_writel_rc(pp, (pp->cfg0_mod_base >> 32), PCIE_ATU_UPPER_BASE);
  442. dw_pcie_writel_rc(pp, pp->cfg0_mod_base + pp->cfg0_size - 1,
  443. PCIE_ATU_LIMIT);
  444. dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
  445. dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
  446. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1);
  447. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  448. }
  449. static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
  450. {
  451. /* Program viewport 1 : OUTBOUND : CFG1 */
  452. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  453. PCIE_ATU_VIEWPORT);
  454. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1);
  455. dw_pcie_writel_rc(pp, pp->cfg1_mod_base, PCIE_ATU_LOWER_BASE);
  456. dw_pcie_writel_rc(pp, (pp->cfg1_mod_base >> 32), PCIE_ATU_UPPER_BASE);
  457. dw_pcie_writel_rc(pp, pp->cfg1_mod_base + pp->cfg1_size - 1,
  458. PCIE_ATU_LIMIT);
  459. dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
  460. dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
  461. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  462. }
  463. static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
  464. {
  465. /* Program viewport 0 : OUTBOUND : MEM */
  466. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  467. PCIE_ATU_VIEWPORT);
  468. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
  469. dw_pcie_writel_rc(pp, pp->mem_mod_base, PCIE_ATU_LOWER_BASE);
  470. dw_pcie_writel_rc(pp, (pp->mem_mod_base >> 32), PCIE_ATU_UPPER_BASE);
  471. dw_pcie_writel_rc(pp, pp->mem_mod_base + pp->mem_size - 1,
  472. PCIE_ATU_LIMIT);
  473. dw_pcie_writel_rc(pp, pp->mem_bus_addr, PCIE_ATU_LOWER_TARGET);
  474. dw_pcie_writel_rc(pp, upper_32_bits(pp->mem_bus_addr),
  475. PCIE_ATU_UPPER_TARGET);
  476. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  477. }
  478. static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
  479. {
  480. /* Program viewport 1 : OUTBOUND : IO */
  481. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  482. PCIE_ATU_VIEWPORT);
  483. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1);
  484. dw_pcie_writel_rc(pp, pp->io_mod_base, PCIE_ATU_LOWER_BASE);
  485. dw_pcie_writel_rc(pp, (pp->io_mod_base >> 32), PCIE_ATU_UPPER_BASE);
  486. dw_pcie_writel_rc(pp, pp->io_mod_base + pp->io_size - 1,
  487. PCIE_ATU_LIMIT);
  488. dw_pcie_writel_rc(pp, pp->io_bus_addr, PCIE_ATU_LOWER_TARGET);
  489. dw_pcie_writel_rc(pp, upper_32_bits(pp->io_bus_addr),
  490. PCIE_ATU_UPPER_TARGET);
  491. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  492. }
  493. static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  494. u32 devfn, int where, int size, u32 *val)
  495. {
  496. int ret = PCIBIOS_SUCCESSFUL;
  497. u32 address, busdev;
  498. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  499. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  500. address = where & ~0x3;
  501. if (bus->parent->number == pp->root_bus_nr) {
  502. dw_pcie_prog_viewport_cfg0(pp, busdev);
  503. ret = dw_pcie_cfg_read(pp->va_cfg0_base + address, where, size,
  504. val);
  505. dw_pcie_prog_viewport_mem_outbound(pp);
  506. } else {
  507. dw_pcie_prog_viewport_cfg1(pp, busdev);
  508. ret = dw_pcie_cfg_read(pp->va_cfg1_base + address, where, size,
  509. val);
  510. dw_pcie_prog_viewport_io_outbound(pp);
  511. }
  512. return ret;
  513. }
  514. static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  515. u32 devfn, int where, int size, u32 val)
  516. {
  517. int ret = PCIBIOS_SUCCESSFUL;
  518. u32 address, busdev;
  519. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  520. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  521. address = where & ~0x3;
  522. if (bus->parent->number == pp->root_bus_nr) {
  523. dw_pcie_prog_viewport_cfg0(pp, busdev);
  524. ret = dw_pcie_cfg_write(pp->va_cfg0_base + address, where, size,
  525. val);
  526. dw_pcie_prog_viewport_mem_outbound(pp);
  527. } else {
  528. dw_pcie_prog_viewport_cfg1(pp, busdev);
  529. ret = dw_pcie_cfg_write(pp->va_cfg1_base + address, where, size,
  530. val);
  531. dw_pcie_prog_viewport_io_outbound(pp);
  532. }
  533. return ret;
  534. }
  535. static int dw_pcie_valid_config(struct pcie_port *pp,
  536. struct pci_bus *bus, int dev)
  537. {
  538. /* If there is no link, then there is no device */
  539. if (bus->number != pp->root_bus_nr) {
  540. if (!dw_pcie_link_up(pp))
  541. return 0;
  542. }
  543. /* access only one slot on each root port */
  544. if (bus->number == pp->root_bus_nr && dev > 0)
  545. return 0;
  546. /*
  547. * do not read more than one device on the bus directly attached
  548. * to RC's (Virtual Bridge's) DS side.
  549. */
  550. if (bus->primary == pp->root_bus_nr && dev > 0)
  551. return 0;
  552. return 1;
  553. }
  554. static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  555. int size, u32 *val)
  556. {
  557. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  558. int ret;
  559. if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
  560. *val = 0xffffffff;
  561. return PCIBIOS_DEVICE_NOT_FOUND;
  562. }
  563. if (bus->number != pp->root_bus_nr)
  564. if (pp->ops->rd_other_conf)
  565. ret = pp->ops->rd_other_conf(pp, bus, devfn,
  566. where, size, val);
  567. else
  568. ret = dw_pcie_rd_other_conf(pp, bus, devfn,
  569. where, size, val);
  570. else
  571. ret = dw_pcie_rd_own_conf(pp, where, size, val);
  572. return ret;
  573. }
  574. static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  575. int where, int size, u32 val)
  576. {
  577. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  578. int ret;
  579. if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
  580. return PCIBIOS_DEVICE_NOT_FOUND;
  581. if (bus->number != pp->root_bus_nr)
  582. if (pp->ops->wr_other_conf)
  583. ret = pp->ops->wr_other_conf(pp, bus, devfn,
  584. where, size, val);
  585. else
  586. ret = dw_pcie_wr_other_conf(pp, bus, devfn,
  587. where, size, val);
  588. else
  589. ret = dw_pcie_wr_own_conf(pp, where, size, val);
  590. return ret;
  591. }
  592. static struct pci_ops dw_pcie_ops = {
  593. .read = dw_pcie_rd_conf,
  594. .write = dw_pcie_wr_conf,
  595. };
  596. static int dw_pcie_setup(int nr, struct pci_sys_data *sys)
  597. {
  598. struct pcie_port *pp;
  599. pp = sys_to_pcie(sys);
  600. if (global_io_offset < SZ_1M && pp->io_size > 0) {
  601. sys->io_offset = global_io_offset - pp->io_bus_addr;
  602. pci_ioremap_io(global_io_offset, pp->io_base);
  603. global_io_offset += SZ_64K;
  604. pci_add_resource_offset(&sys->resources, &pp->io,
  605. sys->io_offset);
  606. }
  607. sys->mem_offset = pp->mem.start - pp->mem_bus_addr;
  608. pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
  609. pci_add_resource(&sys->resources, &pp->busn);
  610. return 1;
  611. }
  612. static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  613. {
  614. struct pci_bus *bus;
  615. struct pcie_port *pp = sys_to_pcie(sys);
  616. pp->root_bus_nr = sys->busnr;
  617. bus = pci_create_root_bus(pp->dev, sys->busnr,
  618. &dw_pcie_ops, sys, &sys->resources);
  619. if (!bus)
  620. return NULL;
  621. pci_scan_child_bus(bus);
  622. if (bus && pp->ops->scan_bus)
  623. pp->ops->scan_bus(pp);
  624. return bus;
  625. }
  626. static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  627. {
  628. struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
  629. int irq;
  630. irq = of_irq_parse_and_map_pci(dev, slot, pin);
  631. if (!irq)
  632. irq = pp->irq;
  633. return irq;
  634. }
  635. static struct hw_pci dw_pci = {
  636. .setup = dw_pcie_setup,
  637. .scan = dw_pcie_scan_bus,
  638. .map_irq = dw_pcie_map_irq,
  639. };
  640. void dw_pcie_setup_rc(struct pcie_port *pp)
  641. {
  642. u32 val;
  643. u32 membase;
  644. u32 memlimit;
  645. /* set the number of lanes */
  646. dw_pcie_readl_rc(pp, PCIE_PORT_LINK_CONTROL, &val);
  647. val &= ~PORT_LINK_MODE_MASK;
  648. switch (pp->lanes) {
  649. case 1:
  650. val |= PORT_LINK_MODE_1_LANES;
  651. break;
  652. case 2:
  653. val |= PORT_LINK_MODE_2_LANES;
  654. break;
  655. case 4:
  656. val |= PORT_LINK_MODE_4_LANES;
  657. break;
  658. }
  659. dw_pcie_writel_rc(pp, val, PCIE_PORT_LINK_CONTROL);
  660. /* set link width speed control register */
  661. dw_pcie_readl_rc(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
  662. val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
  663. switch (pp->lanes) {
  664. case 1:
  665. val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
  666. break;
  667. case 2:
  668. val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
  669. break;
  670. case 4:
  671. val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
  672. break;
  673. }
  674. dw_pcie_writel_rc(pp, val, PCIE_LINK_WIDTH_SPEED_CONTROL);
  675. /* setup RC BARs */
  676. dw_pcie_writel_rc(pp, 0x00000004, PCI_BASE_ADDRESS_0);
  677. dw_pcie_writel_rc(pp, 0x00000000, PCI_BASE_ADDRESS_1);
  678. /* setup interrupt pins */
  679. dw_pcie_readl_rc(pp, PCI_INTERRUPT_LINE, &val);
  680. val &= 0xffff00ff;
  681. val |= 0x00000100;
  682. dw_pcie_writel_rc(pp, val, PCI_INTERRUPT_LINE);
  683. /* setup bus numbers */
  684. dw_pcie_readl_rc(pp, PCI_PRIMARY_BUS, &val);
  685. val &= 0xff000000;
  686. val |= 0x00010100;
  687. dw_pcie_writel_rc(pp, val, PCI_PRIMARY_BUS);
  688. /* setup memory base, memory limit */
  689. membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
  690. memlimit = (pp->mem_size + (u32)pp->mem_base) & 0xfff00000;
  691. val = memlimit | membase;
  692. dw_pcie_writel_rc(pp, val, PCI_MEMORY_BASE);
  693. /* setup command register */
  694. dw_pcie_readl_rc(pp, PCI_COMMAND, &val);
  695. val &= 0xffff0000;
  696. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  697. PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
  698. dw_pcie_writel_rc(pp, val, PCI_COMMAND);
  699. }
  700. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  701. MODULE_DESCRIPTION("Designware PCIe host controller driver");
  702. MODULE_LICENSE("GPL v2");