pci-mvebu.c 33 KB

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