pci-mvebu.c 33 KB

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