pcie-designware.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
  136. {
  137. unsigned long val;
  138. int i, pos, irq;
  139. irqreturn_t ret = IRQ_NONE;
  140. for (i = 0; i < MAX_MSI_CTRLS; i++) {
  141. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS + i * 12, 4,
  142. (u32 *)&val);
  143. if (val) {
  144. ret = IRQ_HANDLED;
  145. pos = 0;
  146. while ((pos = find_next_bit(&val, 32, pos)) != 32) {
  147. irq = irq_find_mapping(pp->irq_domain,
  148. i * 32 + pos);
  149. dw_pcie_wr_own_conf(pp,
  150. PCIE_MSI_INTR0_STATUS + i * 12,
  151. 4, 1 << pos);
  152. generic_handle_irq(irq);
  153. pos++;
  154. }
  155. }
  156. }
  157. return ret;
  158. }
  159. void dw_pcie_msi_init(struct pcie_port *pp)
  160. {
  161. pp->msi_data = __get_free_pages(GFP_KERNEL, 0);
  162. /* program the msi_data */
  163. dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
  164. virt_to_phys((void *)pp->msi_data));
  165. dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4, 0);
  166. }
  167. static int find_valid_pos0(struct pcie_port *pp, int msgvec, int pos, int *pos0)
  168. {
  169. int flag = 1;
  170. do {
  171. pos = find_next_zero_bit(pp->msi_irq_in_use,
  172. MAX_MSI_IRQS, pos);
  173. /*if you have reached to the end then get out from here.*/
  174. if (pos == MAX_MSI_IRQS)
  175. return -ENOSPC;
  176. /*
  177. * Check if this position is at correct offset.nvec is always a
  178. * power of two. pos0 must be nvec bit aligned.
  179. */
  180. if (pos % msgvec)
  181. pos += msgvec - (pos % msgvec);
  182. else
  183. flag = 0;
  184. } while (flag);
  185. *pos0 = pos;
  186. return 0;
  187. }
  188. static void clear_irq_range(struct pcie_port *pp, unsigned int irq_base,
  189. unsigned int nvec, unsigned int pos)
  190. {
  191. unsigned int i, res, bit, val;
  192. for (i = 0; i < nvec; i++) {
  193. irq_set_msi_desc_off(irq_base, i, NULL);
  194. clear_bit(pos + i, pp->msi_irq_in_use);
  195. /* Disable corresponding interrupt on MSI controller */
  196. res = ((pos + i) / 32) * 12;
  197. bit = (pos + i) % 32;
  198. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
  199. val &= ~(1 << bit);
  200. dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
  201. }
  202. }
  203. static int assign_irq(int no_irqs, struct msi_desc *desc, int *pos)
  204. {
  205. int res, bit, irq, pos0, pos1, i;
  206. u32 val;
  207. struct pcie_port *pp = sys_to_pcie(desc->dev->bus->sysdata);
  208. if (!pp) {
  209. BUG();
  210. return -EINVAL;
  211. }
  212. pos0 = find_first_zero_bit(pp->msi_irq_in_use,
  213. MAX_MSI_IRQS);
  214. if (pos0 % no_irqs) {
  215. if (find_valid_pos0(pp, no_irqs, pos0, &pos0))
  216. goto no_valid_irq;
  217. }
  218. if (no_irqs > 1) {
  219. pos1 = find_next_bit(pp->msi_irq_in_use,
  220. MAX_MSI_IRQS, pos0);
  221. /* there must be nvec number of consecutive free bits */
  222. while ((pos1 - pos0) < no_irqs) {
  223. if (find_valid_pos0(pp, no_irqs, pos1, &pos0))
  224. goto no_valid_irq;
  225. pos1 = find_next_bit(pp->msi_irq_in_use,
  226. MAX_MSI_IRQS, pos0);
  227. }
  228. }
  229. irq = irq_find_mapping(pp->irq_domain, pos0);
  230. if (!irq)
  231. goto no_valid_irq;
  232. /*
  233. * irq_create_mapping (called from dw_pcie_host_init) pre-allocates
  234. * descs so there is no need to allocate descs here. We can therefore
  235. * assume that if irq_find_mapping above returns non-zero, then the
  236. * descs are also successfully allocated.
  237. */
  238. for (i = 0; i < no_irqs; i++) {
  239. if (irq_set_msi_desc_off(irq, i, desc) != 0) {
  240. clear_irq_range(pp, irq, i, pos0);
  241. goto no_valid_irq;
  242. }
  243. set_bit(pos0 + i, pp->msi_irq_in_use);
  244. /*Enable corresponding interrupt in MSI interrupt controller */
  245. res = ((pos0 + i) / 32) * 12;
  246. bit = (pos0 + i) % 32;
  247. dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, &val);
  248. val |= 1 << bit;
  249. dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE + res, 4, val);
  250. }
  251. *pos = pos0;
  252. return irq;
  253. no_valid_irq:
  254. *pos = pos0;
  255. return -ENOSPC;
  256. }
  257. static void clear_irq(unsigned int irq)
  258. {
  259. unsigned int pos, nvec;
  260. struct msi_desc *msi;
  261. struct pcie_port *pp;
  262. struct irq_data *data = irq_get_irq_data(irq);
  263. /* get the port structure */
  264. msi = irq_data_get_msi(data);
  265. pp = sys_to_pcie(msi->dev->bus->sysdata);
  266. if (!pp) {
  267. BUG();
  268. return;
  269. }
  270. /* undo what was done in assign_irq */
  271. pos = data->hwirq;
  272. nvec = 1 << msi->msi_attrib.multiple;
  273. clear_irq_range(pp, irq, nvec, pos);
  274. /* all irqs cleared; reset attributes */
  275. msi->irq = 0;
  276. msi->msi_attrib.multiple = 0;
  277. }
  278. static int dw_msi_setup_irq(struct msi_chip *chip, struct pci_dev *pdev,
  279. struct msi_desc *desc)
  280. {
  281. int irq, pos, msgvec;
  282. u16 msg_ctr;
  283. struct msi_msg msg;
  284. struct pcie_port *pp = sys_to_pcie(pdev->bus->sysdata);
  285. if (!pp) {
  286. BUG();
  287. return -EINVAL;
  288. }
  289. pci_read_config_word(pdev, desc->msi_attrib.pos+PCI_MSI_FLAGS,
  290. &msg_ctr);
  291. msgvec = (msg_ctr&PCI_MSI_FLAGS_QSIZE) >> 4;
  292. if (msgvec == 0)
  293. msgvec = (msg_ctr & PCI_MSI_FLAGS_QMASK) >> 1;
  294. if (msgvec > 5)
  295. msgvec = 0;
  296. irq = assign_irq((1 << msgvec), desc, &pos);
  297. if (irq < 0)
  298. return irq;
  299. /*
  300. * write_msi_msg() will update PCI_MSI_FLAGS so there is
  301. * no need to explicitly call pci_write_config_word().
  302. */
  303. desc->msi_attrib.multiple = msgvec;
  304. msg.address_lo = virt_to_phys((void *)pp->msi_data);
  305. msg.address_hi = 0x0;
  306. msg.data = pos;
  307. write_msi_msg(irq, &msg);
  308. return 0;
  309. }
  310. static void dw_msi_teardown_irq(struct msi_chip *chip, unsigned int irq)
  311. {
  312. clear_irq(irq);
  313. }
  314. static struct msi_chip dw_pcie_msi_chip = {
  315. .setup_irq = dw_msi_setup_irq,
  316. .teardown_irq = dw_msi_teardown_irq,
  317. };
  318. int dw_pcie_link_up(struct pcie_port *pp)
  319. {
  320. if (pp->ops->link_up)
  321. return pp->ops->link_up(pp);
  322. else
  323. return 0;
  324. }
  325. static int dw_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
  326. irq_hw_number_t hwirq)
  327. {
  328. irq_set_chip_and_handler(irq, &dw_msi_irq_chip, handle_simple_irq);
  329. irq_set_chip_data(irq, domain->host_data);
  330. set_irq_flags(irq, IRQF_VALID);
  331. return 0;
  332. }
  333. static const struct irq_domain_ops msi_domain_ops = {
  334. .map = dw_pcie_msi_map,
  335. };
  336. int __init dw_pcie_host_init(struct pcie_port *pp)
  337. {
  338. struct device_node *np = pp->dev->of_node;
  339. struct of_pci_range range;
  340. struct of_pci_range_parser parser;
  341. u32 val;
  342. int i;
  343. if (of_pci_range_parser_init(&parser, np)) {
  344. dev_err(pp->dev, "missing ranges property\n");
  345. return -EINVAL;
  346. }
  347. /* Get the I/O and memory ranges from DT */
  348. for_each_of_pci_range(&parser, &range) {
  349. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  350. if (restype == IORESOURCE_IO) {
  351. of_pci_range_to_resource(&range, np, &pp->io);
  352. pp->io.name = "I/O";
  353. pp->io.start = max_t(resource_size_t,
  354. PCIBIOS_MIN_IO,
  355. range.pci_addr + global_io_offset);
  356. pp->io.end = min_t(resource_size_t,
  357. IO_SPACE_LIMIT,
  358. range.pci_addr + range.size
  359. + global_io_offset);
  360. pp->config.io_size = resource_size(&pp->io);
  361. pp->config.io_bus_addr = range.pci_addr;
  362. pp->io_base = range.cpu_addr;
  363. }
  364. if (restype == IORESOURCE_MEM) {
  365. of_pci_range_to_resource(&range, np, &pp->mem);
  366. pp->mem.name = "MEM";
  367. pp->config.mem_size = resource_size(&pp->mem);
  368. pp->config.mem_bus_addr = range.pci_addr;
  369. }
  370. if (restype == 0) {
  371. of_pci_range_to_resource(&range, np, &pp->cfg);
  372. pp->config.cfg0_size = resource_size(&pp->cfg)/2;
  373. pp->config.cfg1_size = resource_size(&pp->cfg)/2;
  374. }
  375. }
  376. if (!pp->dbi_base) {
  377. pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
  378. resource_size(&pp->cfg));
  379. if (!pp->dbi_base) {
  380. dev_err(pp->dev, "error with ioremap\n");
  381. return -ENOMEM;
  382. }
  383. }
  384. pp->cfg0_base = pp->cfg.start;
  385. pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size;
  386. pp->mem_base = pp->mem.start;
  387. pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
  388. pp->config.cfg0_size);
  389. if (!pp->va_cfg0_base) {
  390. dev_err(pp->dev, "error with ioremap in function\n");
  391. return -ENOMEM;
  392. }
  393. pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
  394. pp->config.cfg1_size);
  395. if (!pp->va_cfg1_base) {
  396. dev_err(pp->dev, "error with ioremap\n");
  397. return -ENOMEM;
  398. }
  399. if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
  400. dev_err(pp->dev, "Failed to parse the number of lanes\n");
  401. return -EINVAL;
  402. }
  403. if (IS_ENABLED(CONFIG_PCI_MSI)) {
  404. pp->irq_domain = irq_domain_add_linear(pp->dev->of_node,
  405. MAX_MSI_IRQS, &msi_domain_ops,
  406. &dw_pcie_msi_chip);
  407. if (!pp->irq_domain) {
  408. dev_err(pp->dev, "irq domain init failed\n");
  409. return -ENXIO;
  410. }
  411. for (i = 0; i < MAX_MSI_IRQS; i++)
  412. irq_create_mapping(pp->irq_domain, i);
  413. }
  414. if (pp->ops->host_init)
  415. pp->ops->host_init(pp);
  416. dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
  417. /* program correct class for RC */
  418. dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
  419. dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
  420. val |= PORT_LOGIC_SPEED_CHANGE;
  421. dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
  422. dw_pci.nr_controllers = 1;
  423. dw_pci.private_data = (void **)&pp;
  424. pci_common_init_dev(pp->dev, &dw_pci);
  425. pci_assign_unassigned_resources();
  426. #ifdef CONFIG_PCI_DOMAINS
  427. dw_pci.domain++;
  428. #endif
  429. return 0;
  430. }
  431. static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
  432. {
  433. /* Program viewport 0 : OUTBOUND : CFG0 */
  434. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  435. PCIE_ATU_VIEWPORT);
  436. dw_pcie_writel_rc(pp, pp->cfg0_base, PCIE_ATU_LOWER_BASE);
  437. dw_pcie_writel_rc(pp, (pp->cfg0_base >> 32), PCIE_ATU_UPPER_BASE);
  438. dw_pcie_writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
  439. PCIE_ATU_LIMIT);
  440. dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
  441. dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
  442. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, PCIE_ATU_CR1);
  443. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  444. }
  445. static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
  446. {
  447. /* Program viewport 1 : OUTBOUND : CFG1 */
  448. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  449. PCIE_ATU_VIEWPORT);
  450. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, PCIE_ATU_CR1);
  451. dw_pcie_writel_rc(pp, pp->cfg1_base, PCIE_ATU_LOWER_BASE);
  452. dw_pcie_writel_rc(pp, (pp->cfg1_base >> 32), PCIE_ATU_UPPER_BASE);
  453. dw_pcie_writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
  454. PCIE_ATU_LIMIT);
  455. dw_pcie_writel_rc(pp, busdev, PCIE_ATU_LOWER_TARGET);
  456. dw_pcie_writel_rc(pp, 0, PCIE_ATU_UPPER_TARGET);
  457. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  458. }
  459. static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
  460. {
  461. /* Program viewport 0 : OUTBOUND : MEM */
  462. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0,
  463. PCIE_ATU_VIEWPORT);
  464. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, PCIE_ATU_CR1);
  465. dw_pcie_writel_rc(pp, pp->mem_base, PCIE_ATU_LOWER_BASE);
  466. dw_pcie_writel_rc(pp, (pp->mem_base >> 32), PCIE_ATU_UPPER_BASE);
  467. dw_pcie_writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
  468. PCIE_ATU_LIMIT);
  469. dw_pcie_writel_rc(pp, pp->config.mem_bus_addr, PCIE_ATU_LOWER_TARGET);
  470. dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
  471. PCIE_ATU_UPPER_TARGET);
  472. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  473. }
  474. static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
  475. {
  476. /* Program viewport 1 : OUTBOUND : IO */
  477. dw_pcie_writel_rc(pp, PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1,
  478. PCIE_ATU_VIEWPORT);
  479. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, PCIE_ATU_CR1);
  480. dw_pcie_writel_rc(pp, pp->io_base, PCIE_ATU_LOWER_BASE);
  481. dw_pcie_writel_rc(pp, (pp->io_base >> 32), PCIE_ATU_UPPER_BASE);
  482. dw_pcie_writel_rc(pp, pp->io_base + pp->config.io_size - 1,
  483. PCIE_ATU_LIMIT);
  484. dw_pcie_writel_rc(pp, pp->config.io_bus_addr, PCIE_ATU_LOWER_TARGET);
  485. dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
  486. PCIE_ATU_UPPER_TARGET);
  487. dw_pcie_writel_rc(pp, PCIE_ATU_ENABLE, PCIE_ATU_CR2);
  488. }
  489. static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  490. u32 devfn, int where, int size, u32 *val)
  491. {
  492. int ret = PCIBIOS_SUCCESSFUL;
  493. u32 address, busdev;
  494. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  495. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  496. address = where & ~0x3;
  497. if (bus->parent->number == pp->root_bus_nr) {
  498. dw_pcie_prog_viewport_cfg0(pp, busdev);
  499. ret = dw_pcie_cfg_read(pp->va_cfg0_base + address, where, size,
  500. val);
  501. dw_pcie_prog_viewport_mem_outbound(pp);
  502. } else {
  503. dw_pcie_prog_viewport_cfg1(pp, busdev);
  504. ret = dw_pcie_cfg_read(pp->va_cfg1_base + address, where, size,
  505. val);
  506. dw_pcie_prog_viewport_io_outbound(pp);
  507. }
  508. return ret;
  509. }
  510. static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  511. u32 devfn, int where, int size, u32 val)
  512. {
  513. int ret = PCIBIOS_SUCCESSFUL;
  514. u32 address, busdev;
  515. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  516. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  517. address = where & ~0x3;
  518. if (bus->parent->number == pp->root_bus_nr) {
  519. dw_pcie_prog_viewport_cfg0(pp, busdev);
  520. ret = dw_pcie_cfg_write(pp->va_cfg0_base + address, where, size,
  521. val);
  522. dw_pcie_prog_viewport_mem_outbound(pp);
  523. } else {
  524. dw_pcie_prog_viewport_cfg1(pp, busdev);
  525. ret = dw_pcie_cfg_write(pp->va_cfg1_base + address, where, size,
  526. val);
  527. dw_pcie_prog_viewport_io_outbound(pp);
  528. }
  529. return ret;
  530. }
  531. static int dw_pcie_valid_config(struct pcie_port *pp,
  532. struct pci_bus *bus, int dev)
  533. {
  534. /* If there is no link, then there is no device */
  535. if (bus->number != pp->root_bus_nr) {
  536. if (!dw_pcie_link_up(pp))
  537. return 0;
  538. }
  539. /* access only one slot on each root port */
  540. if (bus->number == pp->root_bus_nr && dev > 0)
  541. return 0;
  542. /*
  543. * do not read more than one device on the bus directly attached
  544. * to RC's (Virtual Bridge's) DS side.
  545. */
  546. if (bus->primary == pp->root_bus_nr && dev > 0)
  547. return 0;
  548. return 1;
  549. }
  550. static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  551. int size, u32 *val)
  552. {
  553. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  554. int ret;
  555. if (!pp) {
  556. BUG();
  557. return -EINVAL;
  558. }
  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. 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. 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. int ret;
  575. if (!pp) {
  576. BUG();
  577. return -EINVAL;
  578. }
  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. ret = dw_pcie_wr_other_conf(pp, bus, devfn,
  583. where, size, val);
  584. else
  585. ret = dw_pcie_wr_own_conf(pp, where, size, val);
  586. return ret;
  587. }
  588. static struct pci_ops dw_pcie_ops = {
  589. .read = dw_pcie_rd_conf,
  590. .write = dw_pcie_wr_conf,
  591. };
  592. static int dw_pcie_setup(int nr, struct pci_sys_data *sys)
  593. {
  594. struct pcie_port *pp;
  595. pp = sys_to_pcie(sys);
  596. if (!pp)
  597. return 0;
  598. if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
  599. sys->io_offset = global_io_offset - pp->config.io_bus_addr;
  600. pci_ioremap_io(global_io_offset, pp->io_base);
  601. global_io_offset += SZ_64K;
  602. pci_add_resource_offset(&sys->resources, &pp->io,
  603. sys->io_offset);
  604. }
  605. sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
  606. pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
  607. return 1;
  608. }
  609. static struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  610. {
  611. struct pci_bus *bus;
  612. struct pcie_port *pp = sys_to_pcie(sys);
  613. if (pp) {
  614. pp->root_bus_nr = sys->busnr;
  615. bus = pci_scan_root_bus(pp->dev, sys->busnr, &dw_pcie_ops,
  616. sys, &sys->resources);
  617. } else {
  618. bus = NULL;
  619. BUG();
  620. }
  621. return bus;
  622. }
  623. static int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  624. {
  625. struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
  626. int irq;
  627. irq = of_irq_parse_and_map_pci(dev, slot, pin);
  628. if (!irq)
  629. irq = pp->irq;
  630. return 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 lanes */
  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, 0x00000000, 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");