pcie-designware.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/pci.h>
  17. #include <linux/pci_regs.h>
  18. #include <linux/types.h>
  19. #include "pcie-designware.h"
  20. /* Synopsis specific PCIE configuration registers */
  21. #define PCIE_PORT_LINK_CONTROL 0x710
  22. #define PORT_LINK_MODE_MASK (0x3f << 16)
  23. #define PORT_LINK_MODE_1_LANES (0x1 << 16)
  24. #define PORT_LINK_MODE_2_LANES (0x3 << 16)
  25. #define PORT_LINK_MODE_4_LANES (0x7 << 16)
  26. #define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
  27. #define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
  28. #define PORT_LOGIC_LINK_WIDTH_MASK (0x1ff << 8)
  29. #define PORT_LOGIC_LINK_WIDTH_1_LANES (0x1 << 8)
  30. #define PORT_LOGIC_LINK_WIDTH_2_LANES (0x2 << 8)
  31. #define PORT_LOGIC_LINK_WIDTH_4_LANES (0x4 << 8)
  32. #define PCIE_MSI_ADDR_LO 0x820
  33. #define PCIE_MSI_ADDR_HI 0x824
  34. #define PCIE_MSI_INTR0_ENABLE 0x828
  35. #define PCIE_MSI_INTR0_MASK 0x82C
  36. #define PCIE_MSI_INTR0_STATUS 0x830
  37. #define PCIE_ATU_VIEWPORT 0x900
  38. #define PCIE_ATU_REGION_INBOUND (0x1 << 31)
  39. #define PCIE_ATU_REGION_OUTBOUND (0x0 << 31)
  40. #define PCIE_ATU_REGION_INDEX1 (0x1 << 0)
  41. #define PCIE_ATU_REGION_INDEX0 (0x0 << 0)
  42. #define PCIE_ATU_CR1 0x904
  43. #define PCIE_ATU_TYPE_MEM (0x0 << 0)
  44. #define PCIE_ATU_TYPE_IO (0x2 << 0)
  45. #define PCIE_ATU_TYPE_CFG0 (0x4 << 0)
  46. #define PCIE_ATU_TYPE_CFG1 (0x5 << 0)
  47. #define PCIE_ATU_CR2 0x908
  48. #define PCIE_ATU_ENABLE (0x1 << 31)
  49. #define PCIE_ATU_BAR_MODE_ENABLE (0x1 << 30)
  50. #define PCIE_ATU_LOWER_BASE 0x90C
  51. #define PCIE_ATU_UPPER_BASE 0x910
  52. #define PCIE_ATU_LIMIT 0x914
  53. #define PCIE_ATU_LOWER_TARGET 0x918
  54. #define PCIE_ATU_BUS(x) (((x) & 0xff) << 24)
  55. #define PCIE_ATU_DEV(x) (((x) & 0x1f) << 19)
  56. #define PCIE_ATU_FUNC(x) (((x) & 0x7) << 16)
  57. #define PCIE_ATU_UPPER_TARGET 0x91C
  58. static struct hw_pci dw_pci;
  59. unsigned long global_io_offset;
  60. static inline struct pcie_port *sys_to_pcie(struct pci_sys_data *sys)
  61. {
  62. return sys->private_data;
  63. }
  64. int cfg_read(void __iomem *addr, int where, int size, u32 *val)
  65. {
  66. *val = readl(addr);
  67. if (size == 1)
  68. *val = (*val >> (8 * (where & 3))) & 0xff;
  69. else if (size == 2)
  70. *val = (*val >> (8 * (where & 3))) & 0xffff;
  71. else if (size != 4)
  72. return PCIBIOS_BAD_REGISTER_NUMBER;
  73. return PCIBIOS_SUCCESSFUL;
  74. }
  75. int cfg_write(void __iomem *addr, int where, int size, u32 val)
  76. {
  77. if (size == 4)
  78. writel(val, addr);
  79. else if (size == 2)
  80. writew(val, addr + (where & 2));
  81. else if (size == 1)
  82. writeb(val, addr + (where & 3));
  83. else
  84. return PCIBIOS_BAD_REGISTER_NUMBER;
  85. return PCIBIOS_SUCCESSFUL;
  86. }
  87. static inline void dw_pcie_readl_rc(struct pcie_port *pp,
  88. void __iomem *dbi_addr, u32 *val)
  89. {
  90. if (pp->ops->readl_rc)
  91. pp->ops->readl_rc(pp, dbi_addr, val);
  92. else
  93. *val = readl(dbi_addr);
  94. }
  95. static inline void dw_pcie_writel_rc(struct pcie_port *pp,
  96. u32 val, void __iomem *dbi_addr)
  97. {
  98. if (pp->ops->writel_rc)
  99. pp->ops->writel_rc(pp, val, dbi_addr);
  100. else
  101. writel(val, dbi_addr);
  102. }
  103. int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
  104. u32 *val)
  105. {
  106. int ret;
  107. if (pp->ops->rd_own_conf)
  108. ret = pp->ops->rd_own_conf(pp, where, size, val);
  109. else
  110. ret = cfg_read(pp->dbi_base + (where & ~0x3), where, size, val);
  111. return ret;
  112. }
  113. int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
  114. u32 val)
  115. {
  116. int ret;
  117. if (pp->ops->wr_own_conf)
  118. ret = pp->ops->wr_own_conf(pp, where, size, val);
  119. else
  120. ret = cfg_write(pp->dbi_base + (where & ~0x3), where, size,
  121. val);
  122. return ret;
  123. }
  124. int dw_pcie_link_up(struct pcie_port *pp)
  125. {
  126. if (pp->ops->link_up)
  127. return pp->ops->link_up(pp);
  128. else
  129. return 0;
  130. }
  131. int __init dw_pcie_host_init(struct pcie_port *pp)
  132. {
  133. struct device_node *np = pp->dev->of_node;
  134. struct of_pci_range range;
  135. struct of_pci_range_parser parser;
  136. u32 val;
  137. if (of_pci_range_parser_init(&parser, np)) {
  138. dev_err(pp->dev, "missing ranges property\n");
  139. return -EINVAL;
  140. }
  141. /* Get the I/O and memory ranges from DT */
  142. for_each_of_pci_range(&parser, &range) {
  143. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  144. if (restype == IORESOURCE_IO) {
  145. of_pci_range_to_resource(&range, np, &pp->io);
  146. pp->io.name = "I/O";
  147. pp->io.start = max_t(resource_size_t,
  148. PCIBIOS_MIN_IO,
  149. range.pci_addr + global_io_offset);
  150. pp->io.end = min_t(resource_size_t,
  151. IO_SPACE_LIMIT,
  152. range.pci_addr + range.size
  153. + global_io_offset);
  154. pp->config.io_size = resource_size(&pp->io);
  155. pp->config.io_bus_addr = range.pci_addr;
  156. }
  157. if (restype == IORESOURCE_MEM) {
  158. of_pci_range_to_resource(&range, np, &pp->mem);
  159. pp->mem.name = "MEM";
  160. pp->config.mem_size = resource_size(&pp->mem);
  161. pp->config.mem_bus_addr = range.pci_addr;
  162. }
  163. if (restype == 0) {
  164. of_pci_range_to_resource(&range, np, &pp->cfg);
  165. pp->config.cfg0_size = resource_size(&pp->cfg)/2;
  166. pp->config.cfg1_size = resource_size(&pp->cfg)/2;
  167. }
  168. }
  169. if (!pp->dbi_base) {
  170. pp->dbi_base = devm_ioremap(pp->dev, pp->cfg.start,
  171. resource_size(&pp->cfg));
  172. if (!pp->dbi_base) {
  173. dev_err(pp->dev, "error with ioremap\n");
  174. return -ENOMEM;
  175. }
  176. }
  177. pp->cfg0_base = pp->cfg.start;
  178. pp->cfg1_base = pp->cfg.start + pp->config.cfg0_size;
  179. pp->io_base = pp->io.start;
  180. pp->mem_base = pp->mem.start;
  181. pp->va_cfg0_base = devm_ioremap(pp->dev, pp->cfg0_base,
  182. pp->config.cfg0_size);
  183. if (!pp->va_cfg0_base) {
  184. dev_err(pp->dev, "error with ioremap in function\n");
  185. return -ENOMEM;
  186. }
  187. pp->va_cfg1_base = devm_ioremap(pp->dev, pp->cfg1_base,
  188. pp->config.cfg1_size);
  189. if (!pp->va_cfg1_base) {
  190. dev_err(pp->dev, "error with ioremap\n");
  191. return -ENOMEM;
  192. }
  193. if (of_property_read_u32(np, "num-lanes", &pp->lanes)) {
  194. dev_err(pp->dev, "Failed to parse the number of lanes\n");
  195. return -EINVAL;
  196. }
  197. if (pp->ops->host_init)
  198. pp->ops->host_init(pp);
  199. dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
  200. /* program correct class for RC */
  201. dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
  202. dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
  203. val |= PORT_LOGIC_SPEED_CHANGE;
  204. dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
  205. dw_pci.nr_controllers = 1;
  206. dw_pci.private_data = (void **)&pp;
  207. pci_common_init(&dw_pci);
  208. pci_assign_unassigned_resources();
  209. #ifdef CONFIG_PCI_DOMAINS
  210. dw_pci.domain++;
  211. #endif
  212. return 0;
  213. }
  214. static void dw_pcie_prog_viewport_cfg0(struct pcie_port *pp, u32 busdev)
  215. {
  216. u32 val;
  217. void __iomem *dbi_base = pp->dbi_base;
  218. /* Program viewport 0 : OUTBOUND : CFG0 */
  219. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0;
  220. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  221. dw_pcie_writel_rc(pp, pp->cfg0_base, dbi_base + PCIE_ATU_LOWER_BASE);
  222. dw_pcie_writel_rc(pp, (pp->cfg0_base >> 32),
  223. dbi_base + PCIE_ATU_UPPER_BASE);
  224. dw_pcie_writel_rc(pp, pp->cfg0_base + pp->config.cfg0_size - 1,
  225. dbi_base + PCIE_ATU_LIMIT);
  226. dw_pcie_writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET);
  227. dw_pcie_writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
  228. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG0, dbi_base + PCIE_ATU_CR1);
  229. val = PCIE_ATU_ENABLE;
  230. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  231. }
  232. static void dw_pcie_prog_viewport_cfg1(struct pcie_port *pp, u32 busdev)
  233. {
  234. u32 val;
  235. void __iomem *dbi_base = pp->dbi_base;
  236. /* Program viewport 1 : OUTBOUND : CFG1 */
  237. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1;
  238. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  239. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_CFG1, dbi_base + PCIE_ATU_CR1);
  240. val = PCIE_ATU_ENABLE;
  241. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  242. dw_pcie_writel_rc(pp, pp->cfg1_base, dbi_base + PCIE_ATU_LOWER_BASE);
  243. dw_pcie_writel_rc(pp, (pp->cfg1_base >> 32),
  244. dbi_base + PCIE_ATU_UPPER_BASE);
  245. dw_pcie_writel_rc(pp, pp->cfg1_base + pp->config.cfg1_size - 1,
  246. dbi_base + PCIE_ATU_LIMIT);
  247. dw_pcie_writel_rc(pp, busdev, dbi_base + PCIE_ATU_LOWER_TARGET);
  248. dw_pcie_writel_rc(pp, 0, dbi_base + PCIE_ATU_UPPER_TARGET);
  249. }
  250. static void dw_pcie_prog_viewport_mem_outbound(struct pcie_port *pp)
  251. {
  252. u32 val;
  253. void __iomem *dbi_base = pp->dbi_base;
  254. /* Program viewport 0 : OUTBOUND : MEM */
  255. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX0;
  256. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  257. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_MEM, dbi_base + PCIE_ATU_CR1);
  258. val = PCIE_ATU_ENABLE;
  259. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  260. dw_pcie_writel_rc(pp, pp->mem_base, dbi_base + PCIE_ATU_LOWER_BASE);
  261. dw_pcie_writel_rc(pp, (pp->mem_base >> 32),
  262. dbi_base + PCIE_ATU_UPPER_BASE);
  263. dw_pcie_writel_rc(pp, pp->mem_base + pp->config.mem_size - 1,
  264. dbi_base + PCIE_ATU_LIMIT);
  265. dw_pcie_writel_rc(pp, pp->config.mem_bus_addr,
  266. dbi_base + PCIE_ATU_LOWER_TARGET);
  267. dw_pcie_writel_rc(pp, upper_32_bits(pp->config.mem_bus_addr),
  268. dbi_base + PCIE_ATU_UPPER_TARGET);
  269. }
  270. static void dw_pcie_prog_viewport_io_outbound(struct pcie_port *pp)
  271. {
  272. u32 val;
  273. void __iomem *dbi_base = pp->dbi_base;
  274. /* Program viewport 1 : OUTBOUND : IO */
  275. val = PCIE_ATU_REGION_OUTBOUND | PCIE_ATU_REGION_INDEX1;
  276. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_VIEWPORT);
  277. dw_pcie_writel_rc(pp, PCIE_ATU_TYPE_IO, dbi_base + PCIE_ATU_CR1);
  278. val = PCIE_ATU_ENABLE;
  279. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_ATU_CR2);
  280. dw_pcie_writel_rc(pp, pp->io_base, dbi_base + PCIE_ATU_LOWER_BASE);
  281. dw_pcie_writel_rc(pp, (pp->io_base >> 32),
  282. dbi_base + PCIE_ATU_UPPER_BASE);
  283. dw_pcie_writel_rc(pp, pp->io_base + pp->config.io_size - 1,
  284. dbi_base + PCIE_ATU_LIMIT);
  285. dw_pcie_writel_rc(pp, pp->config.io_bus_addr,
  286. dbi_base + PCIE_ATU_LOWER_TARGET);
  287. dw_pcie_writel_rc(pp, upper_32_bits(pp->config.io_bus_addr),
  288. dbi_base + PCIE_ATU_UPPER_TARGET);
  289. }
  290. static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  291. u32 devfn, int where, int size, u32 *val)
  292. {
  293. int ret = PCIBIOS_SUCCESSFUL;
  294. u32 address, busdev;
  295. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  296. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  297. address = where & ~0x3;
  298. if (bus->parent->number == pp->root_bus_nr) {
  299. dw_pcie_prog_viewport_cfg0(pp, busdev);
  300. ret = cfg_read(pp->va_cfg0_base + address, where, size, val);
  301. dw_pcie_prog_viewport_mem_outbound(pp);
  302. } else {
  303. dw_pcie_prog_viewport_cfg1(pp, busdev);
  304. ret = cfg_read(pp->va_cfg1_base + address, where, size, val);
  305. dw_pcie_prog_viewport_io_outbound(pp);
  306. }
  307. return ret;
  308. }
  309. static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
  310. u32 devfn, int where, int size, u32 val)
  311. {
  312. int ret = PCIBIOS_SUCCESSFUL;
  313. u32 address, busdev;
  314. busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
  315. PCIE_ATU_FUNC(PCI_FUNC(devfn));
  316. address = where & ~0x3;
  317. if (bus->parent->number == pp->root_bus_nr) {
  318. dw_pcie_prog_viewport_cfg0(pp, busdev);
  319. ret = cfg_write(pp->va_cfg0_base + address, where, size, val);
  320. dw_pcie_prog_viewport_mem_outbound(pp);
  321. } else {
  322. dw_pcie_prog_viewport_cfg1(pp, busdev);
  323. ret = cfg_write(pp->va_cfg1_base + address, where, size, val);
  324. dw_pcie_prog_viewport_io_outbound(pp);
  325. }
  326. return ret;
  327. }
  328. static int dw_pcie_valid_config(struct pcie_port *pp,
  329. struct pci_bus *bus, int dev)
  330. {
  331. /* If there is no link, then there is no device */
  332. if (bus->number != pp->root_bus_nr) {
  333. if (!dw_pcie_link_up(pp))
  334. return 0;
  335. }
  336. /* access only one slot on each root port */
  337. if (bus->number == pp->root_bus_nr && dev > 0)
  338. return 0;
  339. /*
  340. * do not read more than one device on the bus directly attached
  341. * to RC's (Virtual Bridge's) DS side.
  342. */
  343. if (bus->primary == pp->root_bus_nr && dev > 0)
  344. return 0;
  345. return 1;
  346. }
  347. static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  348. int size, u32 *val)
  349. {
  350. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  351. unsigned long flags;
  352. int ret;
  353. if (!pp) {
  354. BUG();
  355. return -EINVAL;
  356. }
  357. if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0) {
  358. *val = 0xffffffff;
  359. return PCIBIOS_DEVICE_NOT_FOUND;
  360. }
  361. spin_lock_irqsave(&pp->conf_lock, flags);
  362. if (bus->number != pp->root_bus_nr)
  363. ret = dw_pcie_rd_other_conf(pp, bus, devfn,
  364. where, size, val);
  365. else
  366. ret = dw_pcie_rd_own_conf(pp, where, size, val);
  367. spin_unlock_irqrestore(&pp->conf_lock, flags);
  368. return ret;
  369. }
  370. static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  371. int where, int size, u32 val)
  372. {
  373. struct pcie_port *pp = sys_to_pcie(bus->sysdata);
  374. unsigned long flags;
  375. int ret;
  376. if (!pp) {
  377. BUG();
  378. return -EINVAL;
  379. }
  380. if (dw_pcie_valid_config(pp, bus, PCI_SLOT(devfn)) == 0)
  381. return PCIBIOS_DEVICE_NOT_FOUND;
  382. spin_lock_irqsave(&pp->conf_lock, flags);
  383. if (bus->number != pp->root_bus_nr)
  384. ret = dw_pcie_wr_other_conf(pp, bus, devfn,
  385. where, size, val);
  386. else
  387. ret = dw_pcie_wr_own_conf(pp, where, size, val);
  388. spin_unlock_irqrestore(&pp->conf_lock, flags);
  389. return ret;
  390. }
  391. static struct pci_ops dw_pcie_ops = {
  392. .read = dw_pcie_rd_conf,
  393. .write = dw_pcie_wr_conf,
  394. };
  395. int dw_pcie_setup(int nr, struct pci_sys_data *sys)
  396. {
  397. struct pcie_port *pp;
  398. pp = sys_to_pcie(sys);
  399. if (!pp)
  400. return 0;
  401. if (global_io_offset < SZ_1M && pp->config.io_size > 0) {
  402. sys->io_offset = global_io_offset - pp->config.io_bus_addr;
  403. pci_ioremap_io(sys->io_offset, pp->io.start);
  404. global_io_offset += SZ_64K;
  405. pci_add_resource_offset(&sys->resources, &pp->io,
  406. sys->io_offset);
  407. }
  408. sys->mem_offset = pp->mem.start - pp->config.mem_bus_addr;
  409. pci_add_resource_offset(&sys->resources, &pp->mem, sys->mem_offset);
  410. return 1;
  411. }
  412. struct pci_bus *dw_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  413. {
  414. struct pci_bus *bus;
  415. struct pcie_port *pp = sys_to_pcie(sys);
  416. if (pp) {
  417. pp->root_bus_nr = sys->busnr;
  418. bus = pci_scan_root_bus(NULL, sys->busnr, &dw_pcie_ops,
  419. sys, &sys->resources);
  420. } else {
  421. bus = NULL;
  422. BUG();
  423. }
  424. return bus;
  425. }
  426. int dw_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  427. {
  428. struct pcie_port *pp = sys_to_pcie(dev->bus->sysdata);
  429. return pp->irq;
  430. }
  431. static struct hw_pci dw_pci = {
  432. .setup = dw_pcie_setup,
  433. .scan = dw_pcie_scan_bus,
  434. .map_irq = dw_pcie_map_irq,
  435. };
  436. void dw_pcie_setup_rc(struct pcie_port *pp)
  437. {
  438. struct pcie_port_info *config = &pp->config;
  439. void __iomem *dbi_base = pp->dbi_base;
  440. u32 val;
  441. u32 membase;
  442. u32 memlimit;
  443. /* set the number of lines as 4 */
  444. dw_pcie_readl_rc(pp, dbi_base + PCIE_PORT_LINK_CONTROL, &val);
  445. val &= ~PORT_LINK_MODE_MASK;
  446. switch (pp->lanes) {
  447. case 1:
  448. val |= PORT_LINK_MODE_1_LANES;
  449. break;
  450. case 2:
  451. val |= PORT_LINK_MODE_2_LANES;
  452. break;
  453. case 4:
  454. val |= PORT_LINK_MODE_4_LANES;
  455. break;
  456. }
  457. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_PORT_LINK_CONTROL);
  458. /* set link width speed control register */
  459. dw_pcie_readl_rc(pp, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL, &val);
  460. val &= ~PORT_LOGIC_LINK_WIDTH_MASK;
  461. switch (pp->lanes) {
  462. case 1:
  463. val |= PORT_LOGIC_LINK_WIDTH_1_LANES;
  464. break;
  465. case 2:
  466. val |= PORT_LOGIC_LINK_WIDTH_2_LANES;
  467. break;
  468. case 4:
  469. val |= PORT_LOGIC_LINK_WIDTH_4_LANES;
  470. break;
  471. }
  472. dw_pcie_writel_rc(pp, val, dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
  473. /* setup RC BARs */
  474. dw_pcie_writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_0);
  475. dw_pcie_writel_rc(pp, 0x00000004, dbi_base + PCI_BASE_ADDRESS_1);
  476. /* setup interrupt pins */
  477. dw_pcie_readl_rc(pp, dbi_base + PCI_INTERRUPT_LINE, &val);
  478. val &= 0xffff00ff;
  479. val |= 0x00000100;
  480. dw_pcie_writel_rc(pp, val, dbi_base + PCI_INTERRUPT_LINE);
  481. /* setup bus numbers */
  482. dw_pcie_readl_rc(pp, dbi_base + PCI_PRIMARY_BUS, &val);
  483. val &= 0xff000000;
  484. val |= 0x00010100;
  485. dw_pcie_writel_rc(pp, val, dbi_base + PCI_PRIMARY_BUS);
  486. /* setup memory base, memory limit */
  487. membase = ((u32)pp->mem_base & 0xfff00000) >> 16;
  488. memlimit = (config->mem_size + (u32)pp->mem_base) & 0xfff00000;
  489. val = memlimit | membase;
  490. dw_pcie_writel_rc(pp, val, dbi_base + PCI_MEMORY_BASE);
  491. /* setup command register */
  492. dw_pcie_readl_rc(pp, dbi_base + PCI_COMMAND, &val);
  493. val &= 0xffff0000;
  494. val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  495. PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
  496. dw_pcie_writel_rc(pp, val, dbi_base + PCI_COMMAND);
  497. }
  498. MODULE_AUTHOR("Jingoo Han <jg1.han@samsung.com>");
  499. MODULE_DESCRIPTION("Designware PCIe host controller driver");
  500. MODULE_LICENSE("GPL v2");