pci-mvebu.c 29 KB

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