pci-mvebu.c 33 KB

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