pci_mvebu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe driver for Marvell MVEBU SoCs
  4. *
  5. * Based on Barebox drivers/pci/pci-mvebu.c
  6. *
  7. * Ported to U-Boot by:
  8. * Anton Schubert <anton.schubert@gmx.de>
  9. * Stefan Roese <sr@denx.de>
  10. * Pali Rohár <pali@kernel.org>
  11. */
  12. #include <common.h>
  13. #include <dm.h>
  14. #include <log.h>
  15. #include <malloc.h>
  16. #include <dm/device-internal.h>
  17. #include <dm/lists.h>
  18. #include <dm/of_access.h>
  19. #include <pci.h>
  20. #include <reset.h>
  21. #include <asm/io.h>
  22. #include <asm/arch/cpu.h>
  23. #include <asm/arch/soc.h>
  24. #include <asm/gpio.h>
  25. #include <linux/bitops.h>
  26. #include <linux/delay.h>
  27. #include <linux/errno.h>
  28. #include <linux/ioport.h>
  29. #include <linux/mbus.h>
  30. #include <linux/sizes.h>
  31. /* PCIe unit register offsets */
  32. #define MVPCIE_ROOT_PORT_PCI_CFG_OFF 0x0000
  33. #define MVPCIE_ROOT_PORT_PCI_EXP_OFF 0x0060
  34. #define MVPCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
  35. #define MVPCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
  36. #define MVPCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
  37. #define MVPCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
  38. #define MVPCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
  39. #define MVPCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
  40. #define MVPCIE_WIN5_CTRL_OFF 0x1880
  41. #define MVPCIE_WIN5_BASE_OFF 0x1884
  42. #define MVPCIE_WIN5_REMAP_OFF 0x188c
  43. #define MVPCIE_CONF_ADDR_OFF 0x18f8
  44. #define MVPCIE_CONF_DATA_OFF 0x18fc
  45. #define MVPCIE_CTRL_OFF 0x1a00
  46. #define MVPCIE_CTRL_RC_MODE BIT(1)
  47. #define MVPCIE_STAT_OFF 0x1a04
  48. #define MVPCIE_STAT_BUS (0xff << 8)
  49. #define MVPCIE_STAT_DEV (0x1f << 16)
  50. #define MVPCIE_STAT_LINK_DOWN BIT(0)
  51. #define LINK_WAIT_RETRIES 100
  52. #define LINK_WAIT_TIMEOUT 1000
  53. struct mvebu_pcie {
  54. struct pci_controller hose;
  55. void __iomem *base;
  56. void __iomem *membase;
  57. struct resource mem;
  58. void __iomem *iobase;
  59. struct resource io;
  60. struct gpio_desc reset_gpio;
  61. u32 intregs;
  62. u32 port;
  63. u32 lane;
  64. bool is_x4;
  65. int devfn;
  66. int sec_busno;
  67. char name[16];
  68. unsigned int mem_target;
  69. unsigned int mem_attr;
  70. unsigned int io_target;
  71. unsigned int io_attr;
  72. u32 cfgcache[(0x3c - 0x10) / 4];
  73. };
  74. static inline bool mvebu_pcie_link_up(struct mvebu_pcie *pcie)
  75. {
  76. u32 val;
  77. val = readl(pcie->base + MVPCIE_STAT_OFF);
  78. return !(val & MVPCIE_STAT_LINK_DOWN);
  79. }
  80. static void mvebu_pcie_wait_for_link(struct mvebu_pcie *pcie)
  81. {
  82. int retries;
  83. /* check if the link is up or not */
  84. for (retries = 0; retries < LINK_WAIT_RETRIES; retries++) {
  85. if (mvebu_pcie_link_up(pcie)) {
  86. printf("%s: Link up\n", pcie->name);
  87. return;
  88. }
  89. udelay(LINK_WAIT_TIMEOUT);
  90. }
  91. printf("%s: Link down\n", pcie->name);
  92. }
  93. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie *pcie, int busno)
  94. {
  95. u32 stat;
  96. stat = readl(pcie->base + MVPCIE_STAT_OFF);
  97. stat &= ~MVPCIE_STAT_BUS;
  98. stat |= busno << 8;
  99. writel(stat, pcie->base + MVPCIE_STAT_OFF);
  100. }
  101. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie *pcie, int devno)
  102. {
  103. u32 stat;
  104. stat = readl(pcie->base + MVPCIE_STAT_OFF);
  105. stat &= ~MVPCIE_STAT_DEV;
  106. stat |= devno << 16;
  107. writel(stat, pcie->base + MVPCIE_STAT_OFF);
  108. }
  109. static inline struct mvebu_pcie *hose_to_pcie(struct pci_controller *hose)
  110. {
  111. return container_of(hose, struct mvebu_pcie, hose);
  112. }
  113. static bool mvebu_pcie_valid_addr(struct mvebu_pcie *pcie,
  114. int busno, int dev, int func)
  115. {
  116. /* On the root bus is only one PCI Bridge */
  117. if (busno == 0 && (dev != 0 || func != 0))
  118. return false;
  119. /* Access to other buses is possible when link is up */
  120. if (busno != 0 && !mvebu_pcie_link_up(pcie))
  121. return false;
  122. /* On secondary bus can be only one PCIe device */
  123. if (busno == pcie->sec_busno && dev != 0)
  124. return false;
  125. return true;
  126. }
  127. static int mvebu_pcie_read_config(const struct udevice *bus, pci_dev_t bdf,
  128. uint offset, ulong *valuep,
  129. enum pci_size_t size)
  130. {
  131. struct mvebu_pcie *pcie = dev_get_plat(bus);
  132. int busno = PCI_BUS(bdf) - dev_seq(bus);
  133. u32 addr, data;
  134. debug("PCIE CFG read: (b,d,f)=(%2d,%2d,%2d) ",
  135. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  136. if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
  137. debug("- out of range\n");
  138. *valuep = pci_get_ff(size);
  139. return 0;
  140. }
  141. /*
  142. * The configuration space of the PCI Bridge on the root bus (zero) is
  143. * of Type 0 but the BAR registers (including ROM BAR) don't have the
  144. * same meaning as in the PCIe specification. Therefore do not access
  145. * BAR registers and non-common registers (those which have different
  146. * meaning for Type 0 and Type 1 config space) of the PCI Bridge and
  147. * instead read their content from driver virtual cfgcache[].
  148. */
  149. if (busno == 0 && ((offset >= 0x10 && offset < 0x34) ||
  150. (offset >= 0x38 && offset < 0x3c))) {
  151. data = pcie->cfgcache[(offset - 0x10) / 4];
  152. debug("(addr,size,val)=(0x%04x, %d, 0x%08x) from cfgcache\n",
  153. offset, size, data);
  154. *valuep = pci_conv_32_to_size(data, offset, size);
  155. return 0;
  156. }
  157. /*
  158. * PCI bridge is device 0 at the root bus (zero) but mvebu has it
  159. * mapped on secondary bus with device number 1.
  160. */
  161. if (busno == 0)
  162. addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset);
  163. else
  164. addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  165. /* write address */
  166. writel(addr, pcie->base + MVPCIE_CONF_ADDR_OFF);
  167. /* read data */
  168. switch (size) {
  169. case PCI_SIZE_8:
  170. data = readb(pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 3));
  171. break;
  172. case PCI_SIZE_16:
  173. data = readw(pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 2));
  174. break;
  175. case PCI_SIZE_32:
  176. data = readl(pcie->base + MVPCIE_CONF_DATA_OFF);
  177. break;
  178. default:
  179. return -EINVAL;
  180. }
  181. if (busno == 0 && (offset & ~3) == (PCI_HEADER_TYPE & ~3)) {
  182. /*
  183. * Change Header Type of PCI Bridge device to Type 1
  184. * (0x01, used by PCI Bridges) because mvebu reports
  185. * Type 0 (0x00, used by Upstream and Endpoint devices).
  186. */
  187. data = pci_conv_size_to_32(data, 0, offset, size);
  188. data &= ~0x007f0000;
  189. data |= PCI_HEADER_TYPE_BRIDGE << 16;
  190. data = pci_conv_32_to_size(data, offset, size);
  191. }
  192. debug("(addr,size,val)=(0x%04x, %d, 0x%08x)\n", offset, size, data);
  193. *valuep = data;
  194. return 0;
  195. }
  196. static int mvebu_pcie_write_config(struct udevice *bus, pci_dev_t bdf,
  197. uint offset, ulong value,
  198. enum pci_size_t size)
  199. {
  200. struct mvebu_pcie *pcie = dev_get_plat(bus);
  201. int busno = PCI_BUS(bdf) - dev_seq(bus);
  202. u32 addr, data;
  203. debug("PCIE CFG write: (b,d,f)=(%2d,%2d,%2d) ",
  204. PCI_BUS(bdf), PCI_DEV(bdf), PCI_FUNC(bdf));
  205. debug("(addr,size,val)=(0x%04x, %d, 0x%08lx)\n", offset, size, value);
  206. if (!mvebu_pcie_valid_addr(pcie, busno, PCI_DEV(bdf), PCI_FUNC(bdf))) {
  207. debug("- out of range\n");
  208. return 0;
  209. }
  210. /*
  211. * As explained in mvebu_pcie_read_config(), PCI Bridge Type 1 specific
  212. * config registers are not available, so we write their content only
  213. * into driver virtual cfgcache[].
  214. * And as explained in mvebu_pcie_probe(), mvebu has its own specific
  215. * way for configuring secondary bus number.
  216. */
  217. if (busno == 0 && ((offset >= 0x10 && offset < 0x34) ||
  218. (offset >= 0x38 && offset < 0x3c))) {
  219. debug("Writing to cfgcache only\n");
  220. data = pcie->cfgcache[(offset - 0x10) / 4];
  221. data = pci_conv_size_to_32(data, value, offset, size);
  222. /* mvebu PCI bridge does not have configurable bars */
  223. if ((offset & ~3) == PCI_BASE_ADDRESS_0 ||
  224. (offset & ~3) == PCI_BASE_ADDRESS_1 ||
  225. (offset & ~3) == PCI_ROM_ADDRESS1)
  226. data = 0x0;
  227. pcie->cfgcache[(offset - 0x10) / 4] = data;
  228. /* mvebu has its own way how to set PCI secondary bus number */
  229. if (offset == PCI_SECONDARY_BUS ||
  230. (offset == PCI_PRIMARY_BUS && size != PCI_SIZE_8)) {
  231. pcie->sec_busno = (data >> 8) & 0xff;
  232. mvebu_pcie_set_local_bus_nr(pcie, pcie->sec_busno);
  233. debug("Secondary bus number was changed to %d\n",
  234. pcie->sec_busno);
  235. }
  236. return 0;
  237. }
  238. /*
  239. * PCI bridge is device 0 at the root bus (zero) but mvebu has it
  240. * mapped on secondary bus with device number 1.
  241. */
  242. if (busno == 0)
  243. addr = PCI_CONF1_EXT_ADDRESS(pcie->sec_busno, 1, 0, offset);
  244. else
  245. addr = PCI_CONF1_EXT_ADDRESS(busno, PCI_DEV(bdf), PCI_FUNC(bdf), offset);
  246. /* write address */
  247. writel(addr, pcie->base + MVPCIE_CONF_ADDR_OFF);
  248. /* write data */
  249. switch (size) {
  250. case PCI_SIZE_8:
  251. writeb(value, pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 3));
  252. break;
  253. case PCI_SIZE_16:
  254. writew(value, pcie->base + MVPCIE_CONF_DATA_OFF + (offset & 2));
  255. break;
  256. case PCI_SIZE_32:
  257. writel(value, pcie->base + MVPCIE_CONF_DATA_OFF);
  258. break;
  259. default:
  260. return -EINVAL;
  261. }
  262. return 0;
  263. }
  264. /*
  265. * Setup PCIE BARs and Address Decode Wins:
  266. * BAR[0] -> internal registers
  267. * BAR[1] -> covers all DRAM banks
  268. * BAR[2] -> disabled
  269. * WIN[0-3] -> DRAM bank[0-3]
  270. */
  271. static void mvebu_pcie_setup_wins(struct mvebu_pcie *pcie)
  272. {
  273. const struct mbus_dram_target_info *dram = mvebu_mbus_dram_info();
  274. u32 size;
  275. int i;
  276. /* First, disable and clear BARs and windows. */
  277. for (i = 1; i < 3; i++) {
  278. writel(0, pcie->base + MVPCIE_BAR_CTRL_OFF(i));
  279. writel(0, pcie->base + MVPCIE_BAR_LO_OFF(i));
  280. writel(0, pcie->base + MVPCIE_BAR_HI_OFF(i));
  281. }
  282. for (i = 0; i < 5; i++) {
  283. writel(0, pcie->base + MVPCIE_WIN04_CTRL_OFF(i));
  284. writel(0, pcie->base + MVPCIE_WIN04_BASE_OFF(i));
  285. writel(0, pcie->base + MVPCIE_WIN04_REMAP_OFF(i));
  286. }
  287. writel(0, pcie->base + MVPCIE_WIN5_CTRL_OFF);
  288. writel(0, pcie->base + MVPCIE_WIN5_BASE_OFF);
  289. writel(0, pcie->base + MVPCIE_WIN5_REMAP_OFF);
  290. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  291. size = 0;
  292. for (i = 0; i < dram->num_cs; i++) {
  293. const struct mbus_dram_window *cs = dram->cs + i;
  294. writel(cs->base & 0xffff0000,
  295. pcie->base + MVPCIE_WIN04_BASE_OFF(i));
  296. writel(0, pcie->base + MVPCIE_WIN04_REMAP_OFF(i));
  297. writel(((cs->size - 1) & 0xffff0000) |
  298. (cs->mbus_attr << 8) |
  299. (dram->mbus_dram_target_id << 4) | 1,
  300. pcie->base + MVPCIE_WIN04_CTRL_OFF(i));
  301. size += cs->size;
  302. }
  303. /* Round up 'size' to the nearest power of two. */
  304. if ((size & (size - 1)) != 0)
  305. size = 1 << fls(size);
  306. /* Setup BAR[1] to all DRAM banks. */
  307. writel(dram->cs[0].base | 0xc, pcie->base + MVPCIE_BAR_LO_OFF(1));
  308. writel(0, pcie->base + MVPCIE_BAR_HI_OFF(1));
  309. writel(((size - 1) & 0xffff0000) | 0x1,
  310. pcie->base + MVPCIE_BAR_CTRL_OFF(1));
  311. /* Setup BAR[0] to internal registers. */
  312. writel(pcie->intregs, pcie->base + MVPCIE_BAR_LO_OFF(0));
  313. writel(0, pcie->base + MVPCIE_BAR_HI_OFF(0));
  314. }
  315. /* Only enable PCIe link, do not setup it */
  316. static int mvebu_pcie_enable_link(struct mvebu_pcie *pcie, ofnode node)
  317. {
  318. struct reset_ctl rst;
  319. int ret;
  320. ret = reset_get_by_index_nodev(node, 0, &rst);
  321. if (ret == -ENOENT) {
  322. return 0;
  323. } else if (ret < 0) {
  324. printf("%s: cannot get reset controller: %d\n", pcie->name, ret);
  325. return ret;
  326. }
  327. ret = reset_request(&rst);
  328. if (ret) {
  329. printf("%s: cannot request reset controller: %d\n", pcie->name, ret);
  330. return ret;
  331. }
  332. ret = reset_deassert(&rst);
  333. reset_free(&rst);
  334. if (ret) {
  335. printf("%s: cannot enable PCIe port: %d\n", pcie->name, ret);
  336. return ret;
  337. }
  338. return 0;
  339. }
  340. /* Setup PCIe link but do not enable it */
  341. static void mvebu_pcie_setup_link(struct mvebu_pcie *pcie)
  342. {
  343. u32 reg;
  344. /* Setup PCIe controller to Root Complex mode */
  345. reg = readl(pcie->base + MVPCIE_CTRL_OFF);
  346. reg |= MVPCIE_CTRL_RC_MODE;
  347. writel(reg, pcie->base + MVPCIE_CTRL_OFF);
  348. /*
  349. * Set Maximum Link Width to X1 or X4 in Root Port's PCIe Link
  350. * Capability register. This register is defined by PCIe specification
  351. * as read-only but this mvebu controller has it as read-write and must
  352. * be set to number of SerDes PCIe lanes (1 or 4). If this register is
  353. * not set correctly then link with endpoint card is not established.
  354. */
  355. reg = readl(pcie->base + MVPCIE_ROOT_PORT_PCI_EXP_OFF + PCI_EXP_LNKCAP);
  356. reg &= ~PCI_EXP_LNKCAP_MLW;
  357. reg |= (pcie->is_x4 ? 4 : 1) << 4;
  358. writel(reg, pcie->base + MVPCIE_ROOT_PORT_PCI_EXP_OFF + PCI_EXP_LNKCAP);
  359. }
  360. static int mvebu_pcie_probe(struct udevice *dev)
  361. {
  362. struct mvebu_pcie *pcie = dev_get_plat(dev);
  363. struct udevice *ctlr = pci_get_controller(dev);
  364. struct pci_controller *hose = dev_get_uclass_priv(ctlr);
  365. u32 reg;
  366. int ret;
  367. /* Request for optional PERST# GPIO */
  368. ret = gpio_request_by_name(dev, "reset-gpios", 0, &pcie->reset_gpio, GPIOD_IS_OUT);
  369. if (ret && ret != -ENOENT) {
  370. printf("%s: unable to request reset-gpios: %d\n", pcie->name, ret);
  371. return ret;
  372. }
  373. /*
  374. * Change Class Code of PCI Bridge device to PCI Bridge (0x600400)
  375. * because default value is Memory controller (0x508000) which
  376. * U-Boot cannot recognize as P2P Bridge.
  377. *
  378. * Note that this mvebu PCI Bridge does not have compliant Type 1
  379. * Configuration Space. Header Type is reported as Type 0 and it
  380. * has format of Type 0 config space.
  381. *
  382. * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34)
  383. * have the same format in Marvell's specification as in PCIe
  384. * specification, but their meaning is totally different and they do
  385. * different things: they are aliased into internal mvebu registers
  386. * (e.g. MVPCIE_BAR_LO_OFF) and these should not be changed or
  387. * reconfigured by pci device drivers.
  388. *
  389. * So our driver converts Type 0 config space to Type 1 and reports
  390. * Header Type as Type 1. Access to BAR registers and to non-existent
  391. * Type 1 registers is redirected to the virtual cfgcache[] buffer,
  392. * which avoids changing unrelated registers.
  393. */
  394. reg = readl(pcie->base + MVPCIE_ROOT_PORT_PCI_CFG_OFF + PCI_CLASS_REVISION);
  395. reg &= ~0xffffff00;
  396. reg |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8;
  397. writel(reg, pcie->base + MVPCIE_ROOT_PORT_PCI_CFG_OFF + PCI_CLASS_REVISION);
  398. /*
  399. * mvebu uses local bus number and local device number to determinate
  400. * type of config request. Type 0 is used if target bus number equals
  401. * local bus number and target device number differs from local device
  402. * number. Type 1 is used if target bus number differs from local bus
  403. * number. And when target bus number equals local bus number and
  404. * target device equals local device number then request is routed to
  405. * PCI Bridge which represent local PCIe Root Port.
  406. *
  407. * It means that PCI root and secondary buses shares one bus number
  408. * which is configured via local bus number. Determination if config
  409. * request should go to root or secondary bus is done based on local
  410. * device number.
  411. *
  412. * PCIe is point-to-point bus, so at secondary bus is always exactly one
  413. * device with number 0. So set local device number to 1, it would not
  414. * conflict with any device on secondary bus number and will ensure that
  415. * accessing secondary bus and all buses behind secondary would work
  416. * automatically and correctly. Therefore this configuration of local
  417. * device number implies that setting of local bus number configures
  418. * secondary bus number. Set it to 0 as U-Boot CONFIG_PCI_PNP code will
  419. * later configure it via config write requests to the correct value.
  420. * mvebu_pcie_write_config() catches config write requests which tries
  421. * to change secondary bus number and correctly updates local bus number
  422. * based on new secondary bus number.
  423. *
  424. * With this configuration is PCI Bridge available at secondary bus as
  425. * device number 1. But it must be available at root bus (zero) as device
  426. * number 0. So in mvebu_pcie_read_config() and mvebu_pcie_write_config()
  427. * functions rewrite address to the real one when accessing the root bus.
  428. */
  429. mvebu_pcie_set_local_bus_nr(pcie, 0);
  430. mvebu_pcie_set_local_dev_nr(pcie, 1);
  431. /*
  432. * Kirkwood arch code already maps mbus windows for PCIe IO and MEM.
  433. * So skip calling mvebu_mbus_add_window_by_id() function as it would
  434. * fail on error "conflicts with another window" which means conflict
  435. * with existing PCIe window mappings.
  436. */
  437. #ifndef CONFIG_ARCH_KIRKWOOD
  438. if (resource_size(&pcie->mem) &&
  439. mvebu_mbus_add_window_by_id(pcie->mem_target, pcie->mem_attr,
  440. (phys_addr_t)pcie->mem.start,
  441. resource_size(&pcie->mem))) {
  442. printf("%s: unable to add mbus window for mem at %08x+%08x\n",
  443. pcie->name,
  444. (u32)pcie->mem.start, (unsigned)resource_size(&pcie->mem));
  445. pcie->mem.start = 0;
  446. pcie->mem.end = -1;
  447. }
  448. if (resource_size(&pcie->io) &&
  449. mvebu_mbus_add_window_by_id(pcie->io_target, pcie->io_attr,
  450. (phys_addr_t)pcie->io.start,
  451. resource_size(&pcie->io))) {
  452. printf("%s: unable to add mbus window for IO at %08x+%08x\n",
  453. pcie->name,
  454. (u32)pcie->io.start, (unsigned)resource_size(&pcie->io));
  455. pcie->io.start = 0;
  456. pcie->io.end = -1;
  457. }
  458. #endif
  459. /* Setup windows and configure host bridge */
  460. mvebu_pcie_setup_wins(pcie);
  461. /* PCI memory space */
  462. pci_set_region(hose->regions + 0, pcie->mem.start,
  463. pcie->mem.start, resource_size(&pcie->mem), PCI_REGION_MEM);
  464. hose->region_count = 1;
  465. if (resource_size(&pcie->mem)) {
  466. pci_set_region(hose->regions + hose->region_count,
  467. pcie->mem.start, pcie->mem.start,
  468. resource_size(&pcie->mem),
  469. PCI_REGION_MEM);
  470. hose->region_count++;
  471. }
  472. if (resource_size(&pcie->io)) {
  473. pci_set_region(hose->regions + hose->region_count,
  474. pcie->io.start, pcie->io.start,
  475. resource_size(&pcie->io),
  476. PCI_REGION_IO);
  477. hose->region_count++;
  478. }
  479. /* PCI Bridge support 32-bit I/O and 64-bit prefetch mem addressing */
  480. pcie->cfgcache[(PCI_IO_BASE - 0x10) / 4] =
  481. PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8);
  482. pcie->cfgcache[(PCI_PREF_MEMORY_BASE - 0x10) / 4] =
  483. PCI_PREF_RANGE_TYPE_64 | (PCI_PREF_RANGE_TYPE_64 << 16);
  484. /* Release PERST# via GPIO when it was defined */
  485. if (dm_gpio_is_valid(&pcie->reset_gpio))
  486. dm_gpio_set_value(&pcie->reset_gpio, 0);
  487. mvebu_pcie_wait_for_link(pcie);
  488. return 0;
  489. }
  490. #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
  491. #define DT_TYPE_IO 0x1
  492. #define DT_TYPE_MEM32 0x2
  493. #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
  494. #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
  495. static int mvebu_get_tgt_attr(ofnode node, int devfn,
  496. unsigned long type,
  497. unsigned int *tgt,
  498. unsigned int *attr)
  499. {
  500. const int na = 3, ns = 2;
  501. const __be32 *range;
  502. int rlen, nranges, rangesz, pna, i;
  503. *tgt = -1;
  504. *attr = -1;
  505. range = ofnode_get_property(node, "ranges", &rlen);
  506. if (!range)
  507. return -EINVAL;
  508. /*
  509. * Linux uses of_n_addr_cells() to get the number of address cells
  510. * here. Currently this function is only available in U-Boot when
  511. * CONFIG_OF_LIVE is enabled. Until this is enabled for MVEBU in
  512. * general, lets't hardcode the "pna" value in the U-Boot code.
  513. */
  514. pna = 2; /* hardcoded for now because of lack of of_n_addr_cells() */
  515. rangesz = pna + na + ns;
  516. nranges = rlen / sizeof(__be32) / rangesz;
  517. for (i = 0; i < nranges; i++, range += rangesz) {
  518. u32 flags = of_read_number(range, 1);
  519. u32 slot = of_read_number(range + 1, 1);
  520. u64 cpuaddr = of_read_number(range + na, pna);
  521. unsigned long rtype;
  522. if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
  523. rtype = IORESOURCE_IO;
  524. else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
  525. rtype = IORESOURCE_MEM;
  526. else
  527. continue;
  528. /*
  529. * The Linux code used PCI_SLOT() here, which expects devfn
  530. * in bits 7..0. PCI_DEV() in U-Boot is similar to PCI_SLOT(),
  531. * only expects devfn in 15..8, where its saved in this driver.
  532. */
  533. if (slot == PCI_DEV(devfn) && type == rtype) {
  534. *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
  535. *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
  536. return 0;
  537. }
  538. }
  539. return -ENOENT;
  540. }
  541. static int mvebu_pcie_port_parse_dt(ofnode node, ofnode parent, struct mvebu_pcie *pcie)
  542. {
  543. struct fdt_pci_addr pci_addr;
  544. const u32 *addr;
  545. u32 num_lanes;
  546. int ret = 0;
  547. int len;
  548. /* Get port number, lane number and memory target / attr */
  549. if (ofnode_read_u32(node, "marvell,pcie-port",
  550. &pcie->port)) {
  551. ret = -ENODEV;
  552. goto err;
  553. }
  554. if (ofnode_read_u32(node, "marvell,pcie-lane", &pcie->lane))
  555. pcie->lane = 0;
  556. sprintf(pcie->name, "pcie%d.%d", pcie->port, pcie->lane);
  557. if (!ofnode_read_u32(node, "num-lanes", &num_lanes) && num_lanes == 4)
  558. pcie->is_x4 = true;
  559. /* devfn is in bits [15:8], see PCI_DEV usage */
  560. ret = ofnode_read_pci_addr(node, FDT_PCI_SPACE_CONFIG, "reg", &pci_addr);
  561. if (ret < 0) {
  562. printf("%s: property \"reg\" is invalid\n", pcie->name);
  563. goto err;
  564. }
  565. pcie->devfn = pci_addr.phys_hi & 0xff00;
  566. ret = mvebu_get_tgt_attr(parent, pcie->devfn,
  567. IORESOURCE_MEM,
  568. &pcie->mem_target, &pcie->mem_attr);
  569. if (ret < 0) {
  570. printf("%s: cannot get tgt/attr for mem window\n", pcie->name);
  571. goto err;
  572. }
  573. ret = mvebu_get_tgt_attr(parent, pcie->devfn,
  574. IORESOURCE_IO,
  575. &pcie->io_target, &pcie->io_attr);
  576. if (ret < 0) {
  577. printf("%s: cannot get tgt/attr for IO window\n", pcie->name);
  578. goto err;
  579. }
  580. /* Parse PCIe controller register base from DT */
  581. addr = ofnode_get_property(node, "assigned-addresses", &len);
  582. if (!addr) {
  583. printf("%s: property \"assigned-addresses\" not found\n", pcie->name);
  584. ret = -FDT_ERR_NOTFOUND;
  585. goto err;
  586. }
  587. pcie->base = (void *)(u32)ofnode_translate_address(node, addr);
  588. pcie->intregs = (u32)pcie->base - fdt32_to_cpu(addr[2]);
  589. return 0;
  590. err:
  591. return ret;
  592. }
  593. static const struct dm_pci_ops mvebu_pcie_ops = {
  594. .read_config = mvebu_pcie_read_config,
  595. .write_config = mvebu_pcie_write_config,
  596. };
  597. static struct driver pcie_mvebu_drv = {
  598. .name = "pcie_mvebu",
  599. .id = UCLASS_PCI,
  600. .ops = &mvebu_pcie_ops,
  601. .probe = mvebu_pcie_probe,
  602. .plat_auto = sizeof(struct mvebu_pcie),
  603. };
  604. /*
  605. * Use a MISC device to bind the n instances (child nodes) of the
  606. * PCIe base controller in UCLASS_PCI.
  607. */
  608. static int mvebu_pcie_bind(struct udevice *parent)
  609. {
  610. struct mvebu_pcie **ports_pcie;
  611. struct mvebu_pcie *pcie;
  612. struct uclass_driver *drv;
  613. struct udevice *dev;
  614. struct resource mem;
  615. struct resource io;
  616. int ports_count, i;
  617. ofnode *ports_nodes;
  618. ofnode subnode;
  619. /* Lookup pci driver */
  620. drv = lists_uclass_lookup(UCLASS_PCI);
  621. if (!drv) {
  622. puts("Cannot find PCI driver\n");
  623. return -ENOENT;
  624. }
  625. ports_count = ofnode_get_child_count(dev_ofnode(parent));
  626. ports_pcie = calloc(ports_count, sizeof(*ports_pcie));
  627. ports_nodes = calloc(ports_count, sizeof(*ports_nodes));
  628. if (!ports_pcie || !ports_nodes) {
  629. free(ports_pcie);
  630. free(ports_nodes);
  631. return -ENOMEM;
  632. }
  633. ports_count = 0;
  634. #ifdef CONFIG_ARCH_KIRKWOOD
  635. mem.start = KW_DEFADR_PCI_MEM;
  636. mem.end = KW_DEFADR_PCI_MEM + KW_DEFADR_PCI_MEM_SIZE - 1;
  637. io.start = KW_DEFADR_PCI_IO;
  638. io.end = KW_DEFADR_PCI_IO + KW_DEFADR_PCI_IO_SIZE - 1;
  639. #else
  640. mem.start = MBUS_PCI_MEM_BASE;
  641. mem.end = MBUS_PCI_MEM_BASE + MBUS_PCI_MEM_SIZE - 1;
  642. io.start = MBUS_PCI_IO_BASE;
  643. io.end = MBUS_PCI_IO_BASE + MBUS_PCI_IO_SIZE - 1;
  644. #endif
  645. /* First phase: Fill mvebu_pcie struct for each port */
  646. ofnode_for_each_subnode(subnode, dev_ofnode(parent)) {
  647. if (!ofnode_is_enabled(subnode))
  648. continue;
  649. pcie = calloc(1, sizeof(*pcie));
  650. if (!pcie)
  651. continue;
  652. if (mvebu_pcie_port_parse_dt(subnode, dev_ofnode(parent), pcie) < 0) {
  653. free(pcie);
  654. continue;
  655. }
  656. /*
  657. * MVEBU PCIe controller needs MEMORY and I/O BARs to be mapped
  658. * into SoCs address space. Each controller will map 128M of MEM
  659. * and 64K of I/O space when registered.
  660. */
  661. if (resource_size(&mem) >= SZ_128M) {
  662. pcie->mem.start = mem.start;
  663. pcie->mem.end = mem.start + SZ_128M - 1;
  664. mem.start += SZ_128M;
  665. } else {
  666. printf("%s: unable to assign mbus window for mem\n", pcie->name);
  667. pcie->mem.start = 0;
  668. pcie->mem.end = -1;
  669. }
  670. if (resource_size(&io) >= SZ_64K) {
  671. pcie->io.start = io.start;
  672. pcie->io.end = io.start + SZ_64K - 1;
  673. io.start += SZ_64K;
  674. } else {
  675. printf("%s: unable to assign mbus window for io\n", pcie->name);
  676. pcie->io.start = 0;
  677. pcie->io.end = -1;
  678. }
  679. ports_pcie[ports_count] = pcie;
  680. ports_nodes[ports_count] = subnode;
  681. ports_count++;
  682. }
  683. /* Second phase: Setup all PCIe links (do not enable them yet) */
  684. for (i = 0; i < ports_count; i++)
  685. mvebu_pcie_setup_link(ports_pcie[i]);
  686. /* Third phase: Enable all PCIe links and create for each UCLASS_PCI device */
  687. for (i = 0; i < ports_count; i++) {
  688. pcie = ports_pcie[i];
  689. subnode = ports_nodes[i];
  690. /*
  691. * PCIe link can be enabled only after all PCIe links were
  692. * properly configured. This is because more PCIe links shares
  693. * one enable bit and some PCIe links cannot be enabled
  694. * individually.
  695. */
  696. if (mvebu_pcie_enable_link(pcie, subnode) < 0) {
  697. free(pcie);
  698. continue;
  699. }
  700. /* Create child device UCLASS_PCI and bind it */
  701. device_bind(parent, &pcie_mvebu_drv, pcie->name, pcie, subnode,
  702. &dev);
  703. }
  704. free(ports_pcie);
  705. free(ports_nodes);
  706. return 0;
  707. }
  708. static const struct udevice_id mvebu_pcie_ids[] = {
  709. { .compatible = "marvell,armada-xp-pcie" },
  710. { .compatible = "marvell,armada-370-pcie" },
  711. { .compatible = "marvell,kirkwood-pcie" },
  712. { }
  713. };
  714. U_BOOT_DRIVER(pcie_mvebu_base) = {
  715. .name = "pcie_mvebu_base",
  716. .id = UCLASS_MISC,
  717. .of_match = mvebu_pcie_ids,
  718. .bind = mvebu_pcie_bind,
  719. };