pci-mvebu.c 32 KB

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