pci-mvebu.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCIe driver for Marvell Armada 370 and Armada XP SoCs
  4. *
  5. * Author: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/pci.h>
  9. #include <linux/clk.h>
  10. #include <linux/delay.h>
  11. #include <linux/gpio.h>
  12. #include <linux/init.h>
  13. #include <linux/mbus.h>
  14. #include <linux/msi.h>
  15. #include <linux/slab.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_gpio.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/of_platform.h>
  22. #include "../pci.h"
  23. /*
  24. * PCIe unit register offsets.
  25. */
  26. #define PCIE_DEV_ID_OFF 0x0000
  27. #define PCIE_CMD_OFF 0x0004
  28. #define PCIE_DEV_REV_OFF 0x0008
  29. #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
  30. #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
  31. #define PCIE_CAP_PCIEXP 0x0060
  32. #define PCIE_HEADER_LOG_4_OFF 0x0128
  33. #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
  34. #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
  35. #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
  36. #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
  37. #define PCIE_WIN5_CTRL_OFF 0x1880
  38. #define PCIE_WIN5_BASE_OFF 0x1884
  39. #define PCIE_WIN5_REMAP_OFF 0x188c
  40. #define PCIE_CONF_ADDR_OFF 0x18f8
  41. #define PCIE_CONF_ADDR_EN 0x80000000
  42. #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
  43. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
  44. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
  45. #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
  46. #define PCIE_CONF_ADDR(bus, devfn, where) \
  47. (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
  48. PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
  49. PCIE_CONF_ADDR_EN)
  50. #define PCIE_CONF_DATA_OFF 0x18fc
  51. #define PCIE_MASK_OFF 0x1910
  52. #define PCIE_MASK_ENABLE_INTS 0x0f000000
  53. #define PCIE_CTRL_OFF 0x1a00
  54. #define PCIE_CTRL_X1_MODE 0x0001
  55. #define PCIE_STAT_OFF 0x1a04
  56. #define PCIE_STAT_BUS 0xff00
  57. #define PCIE_STAT_DEV 0x1f0000
  58. #define PCIE_STAT_LINK_DOWN BIT(0)
  59. #define PCIE_RC_RTSTA 0x1a14
  60. #define PCIE_DEBUG_CTRL 0x1a60
  61. #define PCIE_DEBUG_SOFT_RESET BIT(20)
  62. enum {
  63. PCISWCAP = PCI_BRIDGE_CONTROL + 2,
  64. PCISWCAP_EXP_LIST_ID = PCISWCAP + PCI_CAP_LIST_ID,
  65. PCISWCAP_EXP_DEVCAP = PCISWCAP + PCI_EXP_DEVCAP,
  66. PCISWCAP_EXP_DEVCTL = PCISWCAP + PCI_EXP_DEVCTL,
  67. PCISWCAP_EXP_LNKCAP = PCISWCAP + PCI_EXP_LNKCAP,
  68. PCISWCAP_EXP_LNKCTL = PCISWCAP + PCI_EXP_LNKCTL,
  69. PCISWCAP_EXP_SLTCAP = PCISWCAP + PCI_EXP_SLTCAP,
  70. PCISWCAP_EXP_SLTCTL = PCISWCAP + PCI_EXP_SLTCTL,
  71. PCISWCAP_EXP_RTCTL = PCISWCAP + PCI_EXP_RTCTL,
  72. PCISWCAP_EXP_RTSTA = PCISWCAP + PCI_EXP_RTSTA,
  73. PCISWCAP_EXP_DEVCAP2 = PCISWCAP + PCI_EXP_DEVCAP2,
  74. PCISWCAP_EXP_DEVCTL2 = PCISWCAP + PCI_EXP_DEVCTL2,
  75. PCISWCAP_EXP_LNKCAP2 = PCISWCAP + PCI_EXP_LNKCAP2,
  76. PCISWCAP_EXP_LNKCTL2 = PCISWCAP + PCI_EXP_LNKCTL2,
  77. PCISWCAP_EXP_SLTCAP2 = PCISWCAP + PCI_EXP_SLTCAP2,
  78. PCISWCAP_EXP_SLTCTL2 = PCISWCAP + PCI_EXP_SLTCTL2,
  79. };
  80. /* PCI configuration space of a PCI-to-PCI bridge */
  81. struct mvebu_sw_pci_bridge {
  82. u16 vendor;
  83. u16 device;
  84. u16 command;
  85. u16 status;
  86. u16 class;
  87. u8 interface;
  88. u8 revision;
  89. u8 bist;
  90. u8 header_type;
  91. u8 latency_timer;
  92. u8 cache_line_size;
  93. u32 bar[2];
  94. u8 primary_bus;
  95. u8 secondary_bus;
  96. u8 subordinate_bus;
  97. u8 secondary_latency_timer;
  98. u8 iobase;
  99. u8 iolimit;
  100. u16 secondary_status;
  101. u16 membase;
  102. u16 memlimit;
  103. u16 iobaseupper;
  104. u16 iolimitupper;
  105. u32 romaddr;
  106. u8 intline;
  107. u8 intpin;
  108. u16 bridgectrl;
  109. /* PCI express capability */
  110. u32 pcie_sltcap;
  111. u16 pcie_devctl;
  112. u16 pcie_rtctl;
  113. };
  114. struct mvebu_pcie_port;
  115. /* Structure representing all PCIe interfaces */
  116. struct mvebu_pcie {
  117. struct platform_device *pdev;
  118. struct mvebu_pcie_port *ports;
  119. struct msi_controller *msi;
  120. struct list_head resources;
  121. struct resource io;
  122. struct resource realio;
  123. struct resource mem;
  124. struct resource busn;
  125. int nports;
  126. };
  127. struct mvebu_pcie_window {
  128. phys_addr_t base;
  129. phys_addr_t remap;
  130. size_t size;
  131. };
  132. /* Structure representing one PCIe interface */
  133. struct mvebu_pcie_port {
  134. char *name;
  135. void __iomem *base;
  136. u32 port;
  137. u32 lane;
  138. int devfn;
  139. unsigned int mem_target;
  140. unsigned int mem_attr;
  141. unsigned int io_target;
  142. unsigned int io_attr;
  143. struct clk *clk;
  144. struct gpio_desc *reset_gpio;
  145. char *reset_name;
  146. struct mvebu_sw_pci_bridge bridge;
  147. struct device_node *dn;
  148. struct mvebu_pcie *pcie;
  149. struct mvebu_pcie_window memwin;
  150. struct mvebu_pcie_window iowin;
  151. u32 saved_pcie_stat;
  152. };
  153. static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg)
  154. {
  155. writel(val, port->base + reg);
  156. }
  157. static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg)
  158. {
  159. return readl(port->base + reg);
  160. }
  161. static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port)
  162. {
  163. return port->io_target != -1 && port->io_attr != -1;
  164. }
  165. static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
  166. {
  167. return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
  168. }
  169. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
  170. {
  171. u32 stat;
  172. stat = mvebu_readl(port, PCIE_STAT_OFF);
  173. stat &= ~PCIE_STAT_BUS;
  174. stat |= nr << 8;
  175. mvebu_writel(port, stat, PCIE_STAT_OFF);
  176. }
  177. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
  178. {
  179. u32 stat;
  180. stat = mvebu_readl(port, PCIE_STAT_OFF);
  181. stat &= ~PCIE_STAT_DEV;
  182. stat |= nr << 16;
  183. mvebu_writel(port, stat, PCIE_STAT_OFF);
  184. }
  185. /*
  186. * Setup PCIE BARs and Address Decode Wins:
  187. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  188. * WIN[0-3] -> DRAM bank[0-3]
  189. */
  190. static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
  191. {
  192. const struct mbus_dram_target_info *dram;
  193. u32 size;
  194. int i;
  195. dram = mv_mbus_dram_info();
  196. /* First, disable and clear BARs and windows. */
  197. for (i = 1; i < 3; i++) {
  198. mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
  199. mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
  200. mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
  201. }
  202. for (i = 0; i < 5; i++) {
  203. mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
  204. mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
  205. mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
  206. }
  207. mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
  208. mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
  209. mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
  210. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  211. size = 0;
  212. for (i = 0; i < dram->num_cs; i++) {
  213. const struct mbus_dram_window *cs = dram->cs + i;
  214. mvebu_writel(port, cs->base & 0xffff0000,
  215. PCIE_WIN04_BASE_OFF(i));
  216. mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
  217. mvebu_writel(port,
  218. ((cs->size - 1) & 0xffff0000) |
  219. (cs->mbus_attr << 8) |
  220. (dram->mbus_dram_target_id << 4) | 1,
  221. PCIE_WIN04_CTRL_OFF(i));
  222. size += cs->size;
  223. }
  224. /* Round up 'size' to the nearest power of two. */
  225. if ((size & (size - 1)) != 0)
  226. size = 1 << fls(size);
  227. /* Setup BAR[1] to all DRAM banks. */
  228. mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
  229. mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
  230. mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
  231. PCIE_BAR_CTRL_OFF(1));
  232. }
  233. static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
  234. {
  235. u32 cmd, mask;
  236. /* Point PCIe unit MBUS decode windows to DRAM space. */
  237. mvebu_pcie_setup_wins(port);
  238. /* Master + slave enable. */
  239. cmd = mvebu_readl(port, PCIE_CMD_OFF);
  240. cmd |= PCI_COMMAND_IO;
  241. cmd |= PCI_COMMAND_MEMORY;
  242. cmd |= PCI_COMMAND_MASTER;
  243. mvebu_writel(port, cmd, PCIE_CMD_OFF);
  244. /* Enable interrupt lines A-D. */
  245. mask = mvebu_readl(port, PCIE_MASK_OFF);
  246. mask |= PCIE_MASK_ENABLE_INTS;
  247. mvebu_writel(port, mask, PCIE_MASK_OFF);
  248. }
  249. static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
  250. struct pci_bus *bus,
  251. u32 devfn, int where, int size, u32 *val)
  252. {
  253. void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
  254. mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
  255. PCIE_CONF_ADDR_OFF);
  256. switch (size) {
  257. case 1:
  258. *val = readb_relaxed(conf_data + (where & 3));
  259. break;
  260. case 2:
  261. *val = readw_relaxed(conf_data + (where & 2));
  262. break;
  263. case 4:
  264. *val = readl_relaxed(conf_data);
  265. break;
  266. }
  267. return PCIBIOS_SUCCESSFUL;
  268. }
  269. static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
  270. struct pci_bus *bus,
  271. u32 devfn, int where, int size, u32 val)
  272. {
  273. void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF;
  274. mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
  275. PCIE_CONF_ADDR_OFF);
  276. switch (size) {
  277. case 1:
  278. writeb(val, conf_data + (where & 3));
  279. break;
  280. case 2:
  281. writew(val, conf_data + (where & 2));
  282. break;
  283. case 4:
  284. writel(val, conf_data);
  285. break;
  286. default:
  287. return PCIBIOS_BAD_REGISTER_NUMBER;
  288. }
  289. return PCIBIOS_SUCCESSFUL;
  290. }
  291. /*
  292. * Remove windows, starting from the largest ones to the smallest
  293. * ones.
  294. */
  295. static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
  296. phys_addr_t base, size_t size)
  297. {
  298. while (size) {
  299. size_t sz = 1 << (fls(size) - 1);
  300. mvebu_mbus_del_window(base, sz);
  301. base += sz;
  302. size -= sz;
  303. }
  304. }
  305. /*
  306. * MBus windows can only have a power of two size, but PCI BARs do not
  307. * have this constraint. Therefore, we have to split the PCI BAR into
  308. * areas each having a power of two size. We start from the largest
  309. * one (i.e highest order bit set in the size).
  310. */
  311. static void mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
  312. unsigned int target, unsigned int attribute,
  313. phys_addr_t base, size_t size,
  314. phys_addr_t remap)
  315. {
  316. size_t size_mapped = 0;
  317. while (size) {
  318. size_t sz = 1 << (fls(size) - 1);
  319. int ret;
  320. ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  321. sz, remap);
  322. if (ret) {
  323. phys_addr_t end = base + sz - 1;
  324. dev_err(&port->pcie->pdev->dev,
  325. "Could not create MBus window at [mem %pa-%pa]: %d\n",
  326. &base, &end, ret);
  327. mvebu_pcie_del_windows(port, base - size_mapped,
  328. size_mapped);
  329. return;
  330. }
  331. size -= sz;
  332. size_mapped += sz;
  333. base += sz;
  334. if (remap != MVEBU_MBUS_NO_REMAP)
  335. remap += sz;
  336. }
  337. }
  338. static void mvebu_pcie_set_window(struct mvebu_pcie_port *port,
  339. unsigned int target, unsigned int attribute,
  340. const struct mvebu_pcie_window *desired,
  341. struct mvebu_pcie_window *cur)
  342. {
  343. if (desired->base == cur->base && desired->remap == cur->remap &&
  344. desired->size == cur->size)
  345. return;
  346. if (cur->size != 0) {
  347. mvebu_pcie_del_windows(port, cur->base, cur->size);
  348. cur->size = 0;
  349. cur->base = 0;
  350. /*
  351. * If something tries to change the window while it is enabled
  352. * the change will not be done atomically. That would be
  353. * difficult to do in the general case.
  354. */
  355. }
  356. if (desired->size == 0)
  357. return;
  358. mvebu_pcie_add_windows(port, target, attribute, desired->base,
  359. desired->size, desired->remap);
  360. *cur = *desired;
  361. }
  362. static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
  363. {
  364. struct mvebu_pcie_window desired = {};
  365. /* Are the new iobase/iolimit values invalid? */
  366. if (port->bridge.iolimit < port->bridge.iobase ||
  367. port->bridge.iolimitupper < port->bridge.iobaseupper ||
  368. !(port->bridge.command & PCI_COMMAND_IO)) {
  369. mvebu_pcie_set_window(port, port->io_target, port->io_attr,
  370. &desired, &port->iowin);
  371. return;
  372. }
  373. if (!mvebu_has_ioport(port)) {
  374. dev_WARN(&port->pcie->pdev->dev,
  375. "Attempt to set IO when IO is disabled\n");
  376. return;
  377. }
  378. /*
  379. * We read the PCI-to-PCI bridge emulated registers, and
  380. * calculate the base address and size of the address decoding
  381. * window to setup, according to the PCI-to-PCI bridge
  382. * specifications. iobase is the bus address, port->iowin_base
  383. * is the CPU address.
  384. */
  385. desired.remap = ((port->bridge.iobase & 0xF0) << 8) |
  386. (port->bridge.iobaseupper << 16);
  387. desired.base = port->pcie->io.start + desired.remap;
  388. desired.size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
  389. (port->bridge.iolimitupper << 16)) -
  390. desired.remap) +
  391. 1;
  392. mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired,
  393. &port->iowin);
  394. }
  395. static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
  396. {
  397. struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP};
  398. /* Are the new membase/memlimit values invalid? */
  399. if (port->bridge.memlimit < port->bridge.membase ||
  400. !(port->bridge.command & PCI_COMMAND_MEMORY)) {
  401. mvebu_pcie_set_window(port, port->mem_target, port->mem_attr,
  402. &desired, &port->memwin);
  403. return;
  404. }
  405. /*
  406. * We read the PCI-to-PCI bridge emulated registers, and
  407. * calculate the base address and size of the address decoding
  408. * window to setup, according to the PCI-to-PCI bridge
  409. * specifications.
  410. */
  411. desired.base = ((port->bridge.membase & 0xFFF0) << 16);
  412. desired.size = (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
  413. desired.base + 1;
  414. mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired,
  415. &port->memwin);
  416. }
  417. /*
  418. * Initialize the configuration space of the PCI-to-PCI bridge
  419. * associated with the given PCIe interface.
  420. */
  421. static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
  422. {
  423. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  424. memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
  425. bridge->class = PCI_CLASS_BRIDGE_PCI;
  426. bridge->vendor = PCI_VENDOR_ID_MARVELL;
  427. bridge->device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
  428. bridge->revision = mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
  429. bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
  430. bridge->cache_line_size = 0x10;
  431. /* We support 32 bits I/O addressing */
  432. bridge->iobase = PCI_IO_RANGE_TYPE_32;
  433. bridge->iolimit = PCI_IO_RANGE_TYPE_32;
  434. /* Add capabilities */
  435. bridge->status = PCI_STATUS_CAP_LIST;
  436. }
  437. /*
  438. * Read the configuration space of the PCI-to-PCI bridge associated to
  439. * the given PCIe interface.
  440. */
  441. static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
  442. unsigned int where, int size, u32 *value)
  443. {
  444. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  445. switch (where & ~3) {
  446. case PCI_VENDOR_ID:
  447. *value = bridge->device << 16 | bridge->vendor;
  448. break;
  449. case PCI_COMMAND:
  450. *value = bridge->command | bridge->status << 16;
  451. break;
  452. case PCI_CLASS_REVISION:
  453. *value = bridge->class << 16 | bridge->interface << 8 |
  454. bridge->revision;
  455. break;
  456. case PCI_CACHE_LINE_SIZE:
  457. *value = bridge->bist << 24 | bridge->header_type << 16 |
  458. bridge->latency_timer << 8 | bridge->cache_line_size;
  459. break;
  460. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  461. *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
  462. break;
  463. case PCI_PRIMARY_BUS:
  464. *value = (bridge->secondary_latency_timer << 24 |
  465. bridge->subordinate_bus << 16 |
  466. bridge->secondary_bus << 8 |
  467. bridge->primary_bus);
  468. break;
  469. case PCI_IO_BASE:
  470. if (!mvebu_has_ioport(port))
  471. *value = bridge->secondary_status << 16;
  472. else
  473. *value = (bridge->secondary_status << 16 |
  474. bridge->iolimit << 8 |
  475. bridge->iobase);
  476. break;
  477. case PCI_MEMORY_BASE:
  478. *value = (bridge->memlimit << 16 | bridge->membase);
  479. break;
  480. case PCI_PREF_MEMORY_BASE:
  481. *value = 0;
  482. break;
  483. case PCI_IO_BASE_UPPER16:
  484. *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
  485. break;
  486. case PCI_CAPABILITY_LIST:
  487. *value = PCISWCAP;
  488. break;
  489. case PCI_ROM_ADDRESS1:
  490. *value = 0;
  491. break;
  492. case PCI_INTERRUPT_LINE:
  493. /* LINE PIN MIN_GNT MAX_LAT */
  494. *value = 0;
  495. break;
  496. case PCISWCAP_EXP_LIST_ID:
  497. /* Set PCIe v2, root port, slot support */
  498. *value = (PCI_EXP_TYPE_ROOT_PORT << 4 | 2 |
  499. PCI_EXP_FLAGS_SLOT) << 16 | PCI_CAP_ID_EXP;
  500. break;
  501. case PCISWCAP_EXP_DEVCAP:
  502. *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP);
  503. break;
  504. case PCISWCAP_EXP_DEVCTL:
  505. *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL) &
  506. ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
  507. PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
  508. *value |= bridge->pcie_devctl;
  509. break;
  510. case PCISWCAP_EXP_LNKCAP:
  511. /*
  512. * PCIe requires the clock power management capability to be
  513. * hard-wired to zero for downstream ports
  514. */
  515. *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) &
  516. ~PCI_EXP_LNKCAP_CLKPM;
  517. break;
  518. case PCISWCAP_EXP_LNKCTL:
  519. *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
  520. break;
  521. case PCISWCAP_EXP_SLTCAP:
  522. *value = bridge->pcie_sltcap;
  523. break;
  524. case PCISWCAP_EXP_SLTCTL:
  525. *value = PCI_EXP_SLTSTA_PDS << 16;
  526. break;
  527. case PCISWCAP_EXP_RTCTL:
  528. *value = bridge->pcie_rtctl;
  529. break;
  530. case PCISWCAP_EXP_RTSTA:
  531. *value = mvebu_readl(port, PCIE_RC_RTSTA);
  532. break;
  533. /* PCIe requires the v2 fields to be hard-wired to zero */
  534. case PCISWCAP_EXP_DEVCAP2:
  535. case PCISWCAP_EXP_DEVCTL2:
  536. case PCISWCAP_EXP_LNKCAP2:
  537. case PCISWCAP_EXP_LNKCTL2:
  538. case PCISWCAP_EXP_SLTCAP2:
  539. case PCISWCAP_EXP_SLTCTL2:
  540. default:
  541. /*
  542. * PCI defines configuration read accesses to reserved or
  543. * unimplemented registers to read as zero and complete
  544. * normally.
  545. */
  546. *value = 0;
  547. return PCIBIOS_SUCCESSFUL;
  548. }
  549. if (size == 2)
  550. *value = (*value >> (8 * (where & 3))) & 0xffff;
  551. else if (size == 1)
  552. *value = (*value >> (8 * (where & 3))) & 0xff;
  553. return PCIBIOS_SUCCESSFUL;
  554. }
  555. /* Write to the PCI-to-PCI bridge configuration space */
  556. static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
  557. unsigned int where, int size, u32 value)
  558. {
  559. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  560. u32 mask, reg;
  561. int err;
  562. if (size == 4)
  563. mask = 0x0;
  564. else if (size == 2)
  565. mask = ~(0xffff << ((where & 3) * 8));
  566. else if (size == 1)
  567. mask = ~(0xff << ((where & 3) * 8));
  568. else
  569. return PCIBIOS_BAD_REGISTER_NUMBER;
  570. err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
  571. if (err)
  572. return err;
  573. value = (reg & mask) | value << ((where & 3) * 8);
  574. switch (where & ~3) {
  575. case PCI_COMMAND:
  576. {
  577. u32 old = bridge->command;
  578. if (!mvebu_has_ioport(port))
  579. value &= ~PCI_COMMAND_IO;
  580. bridge->command = value & 0xffff;
  581. if ((old ^ bridge->command) & PCI_COMMAND_IO)
  582. mvebu_pcie_handle_iobase_change(port);
  583. if ((old ^ bridge->command) & PCI_COMMAND_MEMORY)
  584. mvebu_pcie_handle_membase_change(port);
  585. break;
  586. }
  587. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  588. bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
  589. break;
  590. case PCI_IO_BASE:
  591. /*
  592. * We also keep bit 1 set, it is a read-only bit that
  593. * indicates we support 32 bits addressing for the
  594. * I/O
  595. */
  596. bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
  597. bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
  598. mvebu_pcie_handle_iobase_change(port);
  599. break;
  600. case PCI_MEMORY_BASE:
  601. bridge->membase = value & 0xffff;
  602. bridge->memlimit = value >> 16;
  603. mvebu_pcie_handle_membase_change(port);
  604. break;
  605. case PCI_IO_BASE_UPPER16:
  606. bridge->iobaseupper = value & 0xffff;
  607. bridge->iolimitupper = value >> 16;
  608. mvebu_pcie_handle_iobase_change(port);
  609. break;
  610. case PCI_PRIMARY_BUS:
  611. bridge->primary_bus = value & 0xff;
  612. bridge->secondary_bus = (value >> 8) & 0xff;
  613. bridge->subordinate_bus = (value >> 16) & 0xff;
  614. bridge->secondary_latency_timer = (value >> 24) & 0xff;
  615. mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
  616. break;
  617. case PCISWCAP_EXP_DEVCTL:
  618. /*
  619. * Armada370 data says these bits must always
  620. * be zero when in root complex mode.
  621. */
  622. value &= ~(PCI_EXP_DEVCTL_URRE | PCI_EXP_DEVCTL_FERE |
  623. PCI_EXP_DEVCTL_NFERE | PCI_EXP_DEVCTL_CERE);
  624. /*
  625. * If the mask is 0xffff0000, then we only want to write
  626. * the device control register, rather than clearing the
  627. * RW1C bits in the device status register. Mask out the
  628. * status register bits.
  629. */
  630. if (mask == 0xffff0000)
  631. value &= 0xffff;
  632. mvebu_writel(port, value, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL);
  633. break;
  634. case PCISWCAP_EXP_LNKCTL:
  635. /*
  636. * If we don't support CLKREQ, we must ensure that the
  637. * CLKREQ enable bit always reads zero. Since we haven't
  638. * had this capability, and it's dependent on board wiring,
  639. * disable it for the time being.
  640. */
  641. value &= ~PCI_EXP_LNKCTL_CLKREQ_EN;
  642. /*
  643. * If the mask is 0xffff0000, then we only want to write
  644. * the link control register, rather than clearing the
  645. * RW1C bits in the link status register. Mask out the
  646. * RW1C status register bits.
  647. */
  648. if (mask == 0xffff0000)
  649. value &= ~((PCI_EXP_LNKSTA_LABS |
  650. PCI_EXP_LNKSTA_LBMS) << 16);
  651. mvebu_writel(port, value, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL);
  652. break;
  653. case PCISWCAP_EXP_RTSTA:
  654. mvebu_writel(port, value, PCIE_RC_RTSTA);
  655. break;
  656. default:
  657. break;
  658. }
  659. return PCIBIOS_SUCCESSFUL;
  660. }
  661. static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
  662. {
  663. return sys->private_data;
  664. }
  665. static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
  666. struct pci_bus *bus,
  667. int devfn)
  668. {
  669. int i;
  670. for (i = 0; i < pcie->nports; i++) {
  671. struct mvebu_pcie_port *port = &pcie->ports[i];
  672. if (bus->number == 0 && port->devfn == devfn)
  673. return port;
  674. if (bus->number != 0 &&
  675. bus->number >= port->bridge.secondary_bus &&
  676. bus->number <= port->bridge.subordinate_bus)
  677. return port;
  678. }
  679. return NULL;
  680. }
  681. /* PCI configuration space write function */
  682. static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  683. int where, int size, u32 val)
  684. {
  685. struct mvebu_pcie *pcie = bus->sysdata;
  686. struct mvebu_pcie_port *port;
  687. int ret;
  688. port = mvebu_pcie_find_port(pcie, bus, devfn);
  689. if (!port)
  690. return PCIBIOS_DEVICE_NOT_FOUND;
  691. /* Access the emulated PCI-to-PCI bridge */
  692. if (bus->number == 0)
  693. return mvebu_sw_pci_bridge_write(port, where, size, val);
  694. if (!mvebu_pcie_link_up(port))
  695. return PCIBIOS_DEVICE_NOT_FOUND;
  696. /* Access the real PCIe interface */
  697. ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
  698. where, size, val);
  699. return ret;
  700. }
  701. /* PCI configuration space read function */
  702. static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  703. int size, u32 *val)
  704. {
  705. struct mvebu_pcie *pcie = bus->sysdata;
  706. struct mvebu_pcie_port *port;
  707. int ret;
  708. port = mvebu_pcie_find_port(pcie, bus, devfn);
  709. if (!port) {
  710. *val = 0xffffffff;
  711. return PCIBIOS_DEVICE_NOT_FOUND;
  712. }
  713. /* Access the emulated PCI-to-PCI bridge */
  714. if (bus->number == 0)
  715. return mvebu_sw_pci_bridge_read(port, where, size, val);
  716. if (!mvebu_pcie_link_up(port)) {
  717. *val = 0xffffffff;
  718. return PCIBIOS_DEVICE_NOT_FOUND;
  719. }
  720. /* Access the real PCIe interface */
  721. ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
  722. where, size, val);
  723. return ret;
  724. }
  725. static struct pci_ops mvebu_pcie_ops = {
  726. .read = mvebu_pcie_rd_conf,
  727. .write = mvebu_pcie_wr_conf,
  728. };
  729. static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
  730. const struct resource *res,
  731. resource_size_t start,
  732. resource_size_t size,
  733. resource_size_t align)
  734. {
  735. if (dev->bus->number != 0)
  736. return start;
  737. /*
  738. * On the PCI-to-PCI bridge side, the I/O windows must have at
  739. * least a 64 KB size and the memory windows must have at
  740. * least a 1 MB size. Moreover, MBus windows need to have a
  741. * base address aligned on their size, and their size must be
  742. * a power of two. This means that if the BAR doesn't have a
  743. * power of two size, several MBus windows will actually be
  744. * created. We need to ensure that the biggest MBus window
  745. * (which will be the first one) is aligned on its size, which
  746. * explains the rounddown_pow_of_two() being done here.
  747. */
  748. if (res->flags & IORESOURCE_IO)
  749. return round_up(start, max_t(resource_size_t, SZ_64K,
  750. rounddown_pow_of_two(size)));
  751. else if (res->flags & IORESOURCE_MEM)
  752. return round_up(start, max_t(resource_size_t, SZ_1M,
  753. rounddown_pow_of_two(size)));
  754. else
  755. return start;
  756. }
  757. static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
  758. struct device_node *np,
  759. struct mvebu_pcie_port *port)
  760. {
  761. struct resource regs;
  762. int ret = 0;
  763. ret = of_address_to_resource(np, 0, &regs);
  764. if (ret)
  765. return ERR_PTR(ret);
  766. return devm_ioremap_resource(&pdev->dev, &regs);
  767. }
  768. #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
  769. #define DT_TYPE_IO 0x1
  770. #define DT_TYPE_MEM32 0x2
  771. #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
  772. #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
  773. static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
  774. unsigned long type,
  775. unsigned int *tgt,
  776. unsigned int *attr)
  777. {
  778. const int na = 3, ns = 2;
  779. const __be32 *range;
  780. int rlen, nranges, rangesz, pna, i;
  781. *tgt = -1;
  782. *attr = -1;
  783. range = of_get_property(np, "ranges", &rlen);
  784. if (!range)
  785. return -EINVAL;
  786. pna = of_n_addr_cells(np);
  787. rangesz = pna + na + ns;
  788. nranges = rlen / sizeof(__be32) / rangesz;
  789. for (i = 0; i < nranges; i++, range += rangesz) {
  790. u32 flags = of_read_number(range, 1);
  791. u32 slot = of_read_number(range + 1, 1);
  792. u64 cpuaddr = of_read_number(range + na, pna);
  793. unsigned long rtype;
  794. if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
  795. rtype = IORESOURCE_IO;
  796. else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
  797. rtype = IORESOURCE_MEM;
  798. else
  799. continue;
  800. if (slot == PCI_SLOT(devfn) && type == rtype) {
  801. *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
  802. *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
  803. return 0;
  804. }
  805. }
  806. return -ENOENT;
  807. }
  808. #ifdef CONFIG_PM_SLEEP
  809. static int mvebu_pcie_suspend(struct device *dev)
  810. {
  811. struct mvebu_pcie *pcie;
  812. int i;
  813. pcie = dev_get_drvdata(dev);
  814. for (i = 0; i < pcie->nports; i++) {
  815. struct mvebu_pcie_port *port = pcie->ports + i;
  816. port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
  817. }
  818. return 0;
  819. }
  820. static int mvebu_pcie_resume(struct device *dev)
  821. {
  822. struct mvebu_pcie *pcie;
  823. int i;
  824. pcie = dev_get_drvdata(dev);
  825. for (i = 0; i < pcie->nports; i++) {
  826. struct mvebu_pcie_port *port = pcie->ports + i;
  827. mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
  828. mvebu_pcie_setup_hw(port);
  829. }
  830. return 0;
  831. }
  832. #endif
  833. static void mvebu_pcie_port_clk_put(void *data)
  834. {
  835. struct mvebu_pcie_port *port = data;
  836. clk_put(port->clk);
  837. }
  838. static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie,
  839. struct mvebu_pcie_port *port, struct device_node *child)
  840. {
  841. struct device *dev = &pcie->pdev->dev;
  842. enum of_gpio_flags flags;
  843. int reset_gpio, ret;
  844. port->pcie = pcie;
  845. if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) {
  846. dev_warn(dev, "ignoring %pOF, missing pcie-port property\n",
  847. child);
  848. goto skip;
  849. }
  850. if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane))
  851. port->lane = 0;
  852. port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port,
  853. port->lane);
  854. if (!port->name) {
  855. ret = -ENOMEM;
  856. goto err;
  857. }
  858. port->devfn = of_pci_get_devfn(child);
  859. if (port->devfn < 0)
  860. goto skip;
  861. ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM,
  862. &port->mem_target, &port->mem_attr);
  863. if (ret < 0) {
  864. dev_err(dev, "%s: cannot get tgt/attr for mem window\n",
  865. port->name);
  866. goto skip;
  867. }
  868. if (resource_size(&pcie->io) != 0) {
  869. mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO,
  870. &port->io_target, &port->io_attr);
  871. } else {
  872. port->io_target = -1;
  873. port->io_attr = -1;
  874. }
  875. reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags);
  876. if (reset_gpio == -EPROBE_DEFER) {
  877. ret = reset_gpio;
  878. goto err;
  879. }
  880. if (gpio_is_valid(reset_gpio)) {
  881. unsigned long gpio_flags;
  882. port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset",
  883. port->name);
  884. if (!port->reset_name) {
  885. ret = -ENOMEM;
  886. goto err;
  887. }
  888. if (flags & OF_GPIO_ACTIVE_LOW) {
  889. dev_info(dev, "%pOF: reset gpio is active low\n",
  890. child);
  891. gpio_flags = GPIOF_ACTIVE_LOW |
  892. GPIOF_OUT_INIT_LOW;
  893. } else {
  894. gpio_flags = GPIOF_OUT_INIT_HIGH;
  895. }
  896. ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags,
  897. port->reset_name);
  898. if (ret) {
  899. if (ret == -EPROBE_DEFER)
  900. goto err;
  901. goto skip;
  902. }
  903. port->reset_gpio = gpio_to_desc(reset_gpio);
  904. }
  905. port->clk = of_clk_get_by_name(child, NULL);
  906. if (IS_ERR(port->clk)) {
  907. dev_err(dev, "%s: cannot get clock\n", port->name);
  908. goto skip;
  909. }
  910. ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port);
  911. if (ret < 0) {
  912. clk_put(port->clk);
  913. goto err;
  914. }
  915. return 1;
  916. skip:
  917. ret = 0;
  918. /* In the case of skipping, we need to free these */
  919. devm_kfree(dev, port->reset_name);
  920. port->reset_name = NULL;
  921. devm_kfree(dev, port->name);
  922. port->name = NULL;
  923. err:
  924. return ret;
  925. }
  926. /*
  927. * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs
  928. * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications
  929. * of the PCI Express Card Electromechanical Specification, 1.1.
  930. */
  931. static int mvebu_pcie_powerup(struct mvebu_pcie_port *port)
  932. {
  933. int ret;
  934. ret = clk_prepare_enable(port->clk);
  935. if (ret < 0)
  936. return ret;
  937. if (port->reset_gpio) {
  938. u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000;
  939. of_property_read_u32(port->dn, "reset-delay-us",
  940. &reset_udelay);
  941. udelay(100);
  942. gpiod_set_value_cansleep(port->reset_gpio, 0);
  943. msleep(reset_udelay / 1000);
  944. }
  945. return 0;
  946. }
  947. /*
  948. * Power down a PCIe port. Strictly, PCIe requires us to place the card
  949. * in D3hot state before asserting PERST#.
  950. */
  951. static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port)
  952. {
  953. gpiod_set_value_cansleep(port->reset_gpio, 1);
  954. clk_disable_unprepare(port->clk);
  955. }
  956. /*
  957. * We can't use devm_of_pci_get_host_bridge_resources() because we
  958. * need to parse our special DT properties encoding the MEM and IO
  959. * apertures.
  960. */
  961. static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie)
  962. {
  963. struct device *dev = &pcie->pdev->dev;
  964. struct device_node *np = dev->of_node;
  965. unsigned int i;
  966. int ret;
  967. INIT_LIST_HEAD(&pcie->resources);
  968. /* Get the bus range */
  969. ret = of_pci_parse_bus_range(np, &pcie->busn);
  970. if (ret) {
  971. dev_err(dev, "failed to parse bus-range property: %d\n", ret);
  972. return ret;
  973. }
  974. pci_add_resource(&pcie->resources, &pcie->busn);
  975. /* Get the PCIe memory aperture */
  976. mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
  977. if (resource_size(&pcie->mem) == 0) {
  978. dev_err(dev, "invalid memory aperture size\n");
  979. return -EINVAL;
  980. }
  981. pcie->mem.name = "PCI MEM";
  982. pci_add_resource(&pcie->resources, &pcie->mem);
  983. /* Get the PCIe IO aperture */
  984. mvebu_mbus_get_pcie_io_aperture(&pcie->io);
  985. if (resource_size(&pcie->io) != 0) {
  986. pcie->realio.flags = pcie->io.flags;
  987. pcie->realio.start = PCIBIOS_MIN_IO;
  988. pcie->realio.end = min_t(resource_size_t,
  989. IO_SPACE_LIMIT - SZ_64K,
  990. resource_size(&pcie->io) - 1);
  991. pcie->realio.name = "PCI I/O";
  992. for (i = 0; i < resource_size(&pcie->realio); i += SZ_64K)
  993. pci_ioremap_io(i, pcie->io.start + i);
  994. pci_add_resource(&pcie->resources, &pcie->realio);
  995. }
  996. return devm_request_pci_bus_resources(dev, &pcie->resources);
  997. }
  998. static int mvebu_pcie_probe(struct platform_device *pdev)
  999. {
  1000. struct device *dev = &pdev->dev;
  1001. struct mvebu_pcie *pcie;
  1002. struct pci_host_bridge *bridge;
  1003. struct device_node *np = dev->of_node;
  1004. struct device_node *child;
  1005. int num, i, ret;
  1006. bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie));
  1007. if (!bridge)
  1008. return -ENOMEM;
  1009. pcie = pci_host_bridge_priv(bridge);
  1010. pcie->pdev = pdev;
  1011. platform_set_drvdata(pdev, pcie);
  1012. ret = mvebu_pcie_parse_request_resources(pcie);
  1013. if (ret)
  1014. return ret;
  1015. num = of_get_available_child_count(np);
  1016. pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL);
  1017. if (!pcie->ports)
  1018. return -ENOMEM;
  1019. i = 0;
  1020. for_each_available_child_of_node(np, child) {
  1021. struct mvebu_pcie_port *port = &pcie->ports[i];
  1022. ret = mvebu_pcie_parse_port(pcie, port, child);
  1023. if (ret < 0) {
  1024. of_node_put(child);
  1025. return ret;
  1026. } else if (ret == 0) {
  1027. continue;
  1028. }
  1029. port->dn = child;
  1030. i++;
  1031. }
  1032. pcie->nports = i;
  1033. for (i = 0; i < pcie->nports; i++) {
  1034. struct mvebu_pcie_port *port = &pcie->ports[i];
  1035. child = port->dn;
  1036. if (!child)
  1037. continue;
  1038. ret = mvebu_pcie_powerup(port);
  1039. if (ret < 0)
  1040. continue;
  1041. port->base = mvebu_pcie_map_registers(pdev, child, port);
  1042. if (IS_ERR(port->base)) {
  1043. dev_err(dev, "%s: cannot map registers\n", port->name);
  1044. port->base = NULL;
  1045. mvebu_pcie_powerdown(port);
  1046. continue;
  1047. }
  1048. mvebu_pcie_setup_hw(port);
  1049. mvebu_pcie_set_local_dev_nr(port, 1);
  1050. mvebu_sw_pci_bridge_init(port);
  1051. }
  1052. pcie->nports = i;
  1053. list_splice_init(&pcie->resources, &bridge->windows);
  1054. bridge->dev.parent = dev;
  1055. bridge->sysdata = pcie;
  1056. bridge->busnr = 0;
  1057. bridge->ops = &mvebu_pcie_ops;
  1058. bridge->map_irq = of_irq_parse_and_map_pci;
  1059. bridge->swizzle_irq = pci_common_swizzle;
  1060. bridge->align_resource = mvebu_pcie_align_resource;
  1061. bridge->msi = pcie->msi;
  1062. return pci_host_probe(bridge);
  1063. }
  1064. static const struct of_device_id mvebu_pcie_of_match_table[] = {
  1065. { .compatible = "marvell,armada-xp-pcie", },
  1066. { .compatible = "marvell,armada-370-pcie", },
  1067. { .compatible = "marvell,dove-pcie", },
  1068. { .compatible = "marvell,kirkwood-pcie", },
  1069. {},
  1070. };
  1071. static const struct dev_pm_ops mvebu_pcie_pm_ops = {
  1072. SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume)
  1073. };
  1074. static struct platform_driver mvebu_pcie_driver = {
  1075. .driver = {
  1076. .name = "mvebu-pcie",
  1077. .of_match_table = mvebu_pcie_of_match_table,
  1078. /* driver unloading/unbinding currently not supported */
  1079. .suppress_bind_attrs = true,
  1080. .pm = &mvebu_pcie_pm_ops,
  1081. },
  1082. .probe = mvebu_pcie_probe,
  1083. };
  1084. builtin_platform_driver(mvebu_pcie_driver);