pcie-designware.c 21 KB

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