pcie-designware.c 22 KB

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