pci-mvebu.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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_HEADER_LOG_4_OFF 0x0128
  32. #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
  33. #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
  34. #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
  35. #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
  36. #define PCIE_WIN5_CTRL_OFF 0x1880
  37. #define PCIE_WIN5_BASE_OFF 0x1884
  38. #define PCIE_WIN5_REMAP_OFF 0x188c
  39. #define PCIE_CONF_ADDR_OFF 0x18f8
  40. #define PCIE_CONF_ADDR_EN 0x80000000
  41. #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
  42. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
  43. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
  44. #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
  45. #define PCIE_CONF_ADDR(bus, devfn, where) \
  46. (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
  47. PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
  48. PCIE_CONF_ADDR_EN)
  49. #define PCIE_CONF_DATA_OFF 0x18fc
  50. #define PCIE_MASK_OFF 0x1910
  51. #define PCIE_MASK_ENABLE_INTS 0x0f000000
  52. #define PCIE_CTRL_OFF 0x1a00
  53. #define PCIE_CTRL_X1_MODE 0x0001
  54. #define PCIE_STAT_OFF 0x1a04
  55. #define PCIE_STAT_BUS 0xff00
  56. #define PCIE_STAT_DEV 0x1f0000
  57. #define PCIE_STAT_LINK_DOWN BIT(0)
  58. #define PCIE_DEBUG_CTRL 0x1a60
  59. #define PCIE_DEBUG_SOFT_RESET BIT(20)
  60. /* PCI configuration space of a PCI-to-PCI bridge */
  61. struct mvebu_sw_pci_bridge {
  62. u16 vendor;
  63. u16 device;
  64. u16 command;
  65. u16 class;
  66. u8 interface;
  67. u8 revision;
  68. u8 bist;
  69. u8 header_type;
  70. u8 latency_timer;
  71. u8 cache_line_size;
  72. u32 bar[2];
  73. u8 primary_bus;
  74. u8 secondary_bus;
  75. u8 subordinate_bus;
  76. u8 secondary_latency_timer;
  77. u8 iobase;
  78. u8 iolimit;
  79. u16 secondary_status;
  80. u16 membase;
  81. u16 memlimit;
  82. u16 iobaseupper;
  83. u16 iolimitupper;
  84. u8 cappointer;
  85. u8 reserved1;
  86. u16 reserved2;
  87. u32 romaddr;
  88. u8 intline;
  89. u8 intpin;
  90. u16 bridgectrl;
  91. };
  92. struct mvebu_pcie_port;
  93. /* Structure representing all PCIe interfaces */
  94. struct mvebu_pcie {
  95. struct platform_device *pdev;
  96. struct mvebu_pcie_port *ports;
  97. struct msi_controller *msi;
  98. struct resource io;
  99. struct resource realio;
  100. struct resource mem;
  101. struct resource busn;
  102. int nports;
  103. };
  104. /* Structure representing one PCIe interface */
  105. struct mvebu_pcie_port {
  106. char *name;
  107. void __iomem *base;
  108. u32 port;
  109. u32 lane;
  110. int devfn;
  111. unsigned int mem_target;
  112. unsigned int mem_attr;
  113. unsigned int io_target;
  114. unsigned int io_attr;
  115. struct clk *clk;
  116. int reset_gpio;
  117. int reset_active_low;
  118. char *reset_name;
  119. struct mvebu_sw_pci_bridge bridge;
  120. struct device_node *dn;
  121. struct mvebu_pcie *pcie;
  122. phys_addr_t memwin_base;
  123. size_t memwin_size;
  124. phys_addr_t iowin_base;
  125. size_t iowin_size;
  126. u32 saved_pcie_stat;
  127. };
  128. static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg)
  129. {
  130. writel(val, port->base + reg);
  131. }
  132. static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg)
  133. {
  134. return readl(port->base + reg);
  135. }
  136. static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port)
  137. {
  138. return port->io_target != -1 && port->io_attr != -1;
  139. }
  140. static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
  141. {
  142. return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
  143. }
  144. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
  145. {
  146. u32 stat;
  147. stat = mvebu_readl(port, PCIE_STAT_OFF);
  148. stat &= ~PCIE_STAT_BUS;
  149. stat |= nr << 8;
  150. mvebu_writel(port, stat, PCIE_STAT_OFF);
  151. }
  152. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
  153. {
  154. u32 stat;
  155. stat = mvebu_readl(port, PCIE_STAT_OFF);
  156. stat &= ~PCIE_STAT_DEV;
  157. stat |= nr << 16;
  158. mvebu_writel(port, stat, PCIE_STAT_OFF);
  159. }
  160. /*
  161. * Setup PCIE BARs and Address Decode Wins:
  162. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  163. * WIN[0-3] -> DRAM bank[0-3]
  164. */
  165. static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
  166. {
  167. const struct mbus_dram_target_info *dram;
  168. u32 size;
  169. int i;
  170. dram = mv_mbus_dram_info();
  171. /* First, disable and clear BARs and windows. */
  172. for (i = 1; i < 3; i++) {
  173. mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i));
  174. mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i));
  175. mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i));
  176. }
  177. for (i = 0; i < 5; i++) {
  178. mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i));
  179. mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i));
  180. mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
  181. }
  182. mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF);
  183. mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF);
  184. mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF);
  185. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  186. size = 0;
  187. for (i = 0; i < dram->num_cs; i++) {
  188. const struct mbus_dram_window *cs = dram->cs + i;
  189. mvebu_writel(port, cs->base & 0xffff0000,
  190. PCIE_WIN04_BASE_OFF(i));
  191. mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i));
  192. mvebu_writel(port,
  193. ((cs->size - 1) & 0xffff0000) |
  194. (cs->mbus_attr << 8) |
  195. (dram->mbus_dram_target_id << 4) | 1,
  196. PCIE_WIN04_CTRL_OFF(i));
  197. size += cs->size;
  198. }
  199. /* Round up 'size' to the nearest power of two. */
  200. if ((size & (size - 1)) != 0)
  201. size = 1 << fls(size);
  202. /* Setup BAR[1] to all DRAM banks. */
  203. mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1));
  204. mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1));
  205. mvebu_writel(port, ((size - 1) & 0xffff0000) | 1,
  206. PCIE_BAR_CTRL_OFF(1));
  207. }
  208. static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
  209. {
  210. u32 cmd, mask;
  211. /* Point PCIe unit MBUS decode windows to DRAM space. */
  212. mvebu_pcie_setup_wins(port);
  213. /* Master + slave enable. */
  214. cmd = mvebu_readl(port, PCIE_CMD_OFF);
  215. cmd |= PCI_COMMAND_IO;
  216. cmd |= PCI_COMMAND_MEMORY;
  217. cmd |= PCI_COMMAND_MASTER;
  218. mvebu_writel(port, cmd, PCIE_CMD_OFF);
  219. /* Enable interrupt lines A-D. */
  220. mask = mvebu_readl(port, PCIE_MASK_OFF);
  221. mask |= PCIE_MASK_ENABLE_INTS;
  222. mvebu_writel(port, mask, PCIE_MASK_OFF);
  223. }
  224. static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
  225. struct pci_bus *bus,
  226. u32 devfn, int where, int size, u32 *val)
  227. {
  228. mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
  229. PCIE_CONF_ADDR_OFF);
  230. *val = mvebu_readl(port, PCIE_CONF_DATA_OFF);
  231. if (size == 1)
  232. *val = (*val >> (8 * (where & 3))) & 0xff;
  233. else if (size == 2)
  234. *val = (*val >> (8 * (where & 3))) & 0xffff;
  235. return PCIBIOS_SUCCESSFUL;
  236. }
  237. static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
  238. struct pci_bus *bus,
  239. u32 devfn, int where, int size, u32 val)
  240. {
  241. u32 _val, shift = 8 * (where & 3);
  242. mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where),
  243. PCIE_CONF_ADDR_OFF);
  244. _val = mvebu_readl(port, PCIE_CONF_DATA_OFF);
  245. if (size == 4)
  246. _val = val;
  247. else if (size == 2)
  248. _val = (_val & ~(0xffff << shift)) | ((val & 0xffff) << shift);
  249. else if (size == 1)
  250. _val = (_val & ~(0xff << shift)) | ((val & 0xff) << shift);
  251. else
  252. return PCIBIOS_BAD_REGISTER_NUMBER;
  253. mvebu_writel(port, _val, PCIE_CONF_DATA_OFF);
  254. return PCIBIOS_SUCCESSFUL;
  255. }
  256. /*
  257. * Remove windows, starting from the largest ones to the smallest
  258. * ones.
  259. */
  260. static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port,
  261. phys_addr_t base, size_t size)
  262. {
  263. while (size) {
  264. size_t sz = 1 << (fls(size) - 1);
  265. mvebu_mbus_del_window(base, sz);
  266. base += sz;
  267. size -= sz;
  268. }
  269. }
  270. /*
  271. * MBus windows can only have a power of two size, but PCI BARs do not
  272. * have this constraint. Therefore, we have to split the PCI BAR into
  273. * areas each having a power of two size. We start from the largest
  274. * one (i.e highest order bit set in the size).
  275. */
  276. static void mvebu_pcie_add_windows(struct mvebu_pcie_port *port,
  277. unsigned int target, unsigned int attribute,
  278. phys_addr_t base, size_t size,
  279. phys_addr_t remap)
  280. {
  281. size_t size_mapped = 0;
  282. while (size) {
  283. size_t sz = 1 << (fls(size) - 1);
  284. int ret;
  285. ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base,
  286. sz, remap);
  287. if (ret) {
  288. phys_addr_t end = base + sz - 1;
  289. dev_err(&port->pcie->pdev->dev,
  290. "Could not create MBus window at [mem %pa-%pa]: %d\n",
  291. &base, &end, ret);
  292. mvebu_pcie_del_windows(port, base - size_mapped,
  293. size_mapped);
  294. return;
  295. }
  296. size -= sz;
  297. size_mapped += sz;
  298. base += sz;
  299. if (remap != MVEBU_MBUS_NO_REMAP)
  300. remap += sz;
  301. }
  302. }
  303. static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
  304. {
  305. phys_addr_t iobase;
  306. /* Are the new iobase/iolimit values invalid? */
  307. if (port->bridge.iolimit < port->bridge.iobase ||
  308. port->bridge.iolimitupper < port->bridge.iobaseupper ||
  309. !(port->bridge.command & PCI_COMMAND_IO)) {
  310. /* If a window was configured, remove it */
  311. if (port->iowin_base) {
  312. mvebu_pcie_del_windows(port, port->iowin_base,
  313. port->iowin_size);
  314. port->iowin_base = 0;
  315. port->iowin_size = 0;
  316. }
  317. return;
  318. }
  319. if (!mvebu_has_ioport(port)) {
  320. dev_WARN(&port->pcie->pdev->dev,
  321. "Attempt to set IO when IO is disabled\n");
  322. return;
  323. }
  324. /*
  325. * We read the PCI-to-PCI bridge emulated registers, and
  326. * calculate the base address and size of the address decoding
  327. * window to setup, according to the PCI-to-PCI bridge
  328. * specifications. iobase is the bus address, port->iowin_base
  329. * is the CPU address.
  330. */
  331. iobase = ((port->bridge.iobase & 0xF0) << 8) |
  332. (port->bridge.iobaseupper << 16);
  333. port->iowin_base = port->pcie->io.start + iobase;
  334. port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
  335. (port->bridge.iolimitupper << 16)) -
  336. iobase) + 1;
  337. mvebu_pcie_add_windows(port, port->io_target, port->io_attr,
  338. port->iowin_base, port->iowin_size,
  339. iobase);
  340. }
  341. static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
  342. {
  343. /* Are the new membase/memlimit values invalid? */
  344. if (port->bridge.memlimit < port->bridge.membase ||
  345. !(port->bridge.command & PCI_COMMAND_MEMORY)) {
  346. /* If a window was configured, remove it */
  347. if (port->memwin_base) {
  348. mvebu_pcie_del_windows(port, port->memwin_base,
  349. port->memwin_size);
  350. port->memwin_base = 0;
  351. port->memwin_size = 0;
  352. }
  353. return;
  354. }
  355. /*
  356. * We read the PCI-to-PCI bridge emulated registers, and
  357. * calculate the base address and size of the address decoding
  358. * window to setup, according to the PCI-to-PCI bridge
  359. * specifications.
  360. */
  361. port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16);
  362. port->memwin_size =
  363. (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
  364. port->memwin_base + 1;
  365. mvebu_pcie_add_windows(port, port->mem_target, port->mem_attr,
  366. port->memwin_base, port->memwin_size,
  367. MVEBU_MBUS_NO_REMAP);
  368. }
  369. /*
  370. * Initialize the configuration space of the PCI-to-PCI bridge
  371. * associated with the given PCIe interface.
  372. */
  373. static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
  374. {
  375. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  376. memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
  377. bridge->class = PCI_CLASS_BRIDGE_PCI;
  378. bridge->vendor = PCI_VENDOR_ID_MARVELL;
  379. bridge->device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16;
  380. bridge->revision = mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff;
  381. bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
  382. bridge->cache_line_size = 0x10;
  383. /* We support 32 bits I/O addressing */
  384. bridge->iobase = PCI_IO_RANGE_TYPE_32;
  385. bridge->iolimit = PCI_IO_RANGE_TYPE_32;
  386. }
  387. /*
  388. * Read the configuration space of the PCI-to-PCI bridge associated to
  389. * the given PCIe interface.
  390. */
  391. static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
  392. unsigned int where, int size, u32 *value)
  393. {
  394. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  395. switch (where & ~3) {
  396. case PCI_VENDOR_ID:
  397. *value = bridge->device << 16 | bridge->vendor;
  398. break;
  399. case PCI_COMMAND:
  400. *value = bridge->command;
  401. break;
  402. case PCI_CLASS_REVISION:
  403. *value = bridge->class << 16 | bridge->interface << 8 |
  404. bridge->revision;
  405. break;
  406. case PCI_CACHE_LINE_SIZE:
  407. *value = bridge->bist << 24 | bridge->header_type << 16 |
  408. bridge->latency_timer << 8 | bridge->cache_line_size;
  409. break;
  410. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  411. *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
  412. break;
  413. case PCI_PRIMARY_BUS:
  414. *value = (bridge->secondary_latency_timer << 24 |
  415. bridge->subordinate_bus << 16 |
  416. bridge->secondary_bus << 8 |
  417. bridge->primary_bus);
  418. break;
  419. case PCI_IO_BASE:
  420. if (!mvebu_has_ioport(port))
  421. *value = bridge->secondary_status << 16;
  422. else
  423. *value = (bridge->secondary_status << 16 |
  424. bridge->iolimit << 8 |
  425. bridge->iobase);
  426. break;
  427. case PCI_MEMORY_BASE:
  428. *value = (bridge->memlimit << 16 | bridge->membase);
  429. break;
  430. case PCI_PREF_MEMORY_BASE:
  431. *value = 0;
  432. break;
  433. case PCI_IO_BASE_UPPER16:
  434. *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
  435. break;
  436. case PCI_ROM_ADDRESS1:
  437. *value = 0;
  438. break;
  439. case PCI_INTERRUPT_LINE:
  440. /* LINE PIN MIN_GNT MAX_LAT */
  441. *value = 0;
  442. break;
  443. default:
  444. *value = 0xffffffff;
  445. return PCIBIOS_BAD_REGISTER_NUMBER;
  446. }
  447. if (size == 2)
  448. *value = (*value >> (8 * (where & 3))) & 0xffff;
  449. else if (size == 1)
  450. *value = (*value >> (8 * (where & 3))) & 0xff;
  451. return PCIBIOS_SUCCESSFUL;
  452. }
  453. /* Write to the PCI-to-PCI bridge configuration space */
  454. static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
  455. unsigned int where, int size, u32 value)
  456. {
  457. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  458. u32 mask, reg;
  459. int err;
  460. if (size == 4)
  461. mask = 0x0;
  462. else if (size == 2)
  463. mask = ~(0xffff << ((where & 3) * 8));
  464. else if (size == 1)
  465. mask = ~(0xff << ((where & 3) * 8));
  466. else
  467. return PCIBIOS_BAD_REGISTER_NUMBER;
  468. err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
  469. if (err)
  470. return err;
  471. value = (reg & mask) | value << ((where & 3) * 8);
  472. switch (where & ~3) {
  473. case PCI_COMMAND:
  474. {
  475. u32 old = bridge->command;
  476. if (!mvebu_has_ioport(port))
  477. value &= ~PCI_COMMAND_IO;
  478. bridge->command = value & 0xffff;
  479. if ((old ^ bridge->command) & PCI_COMMAND_IO)
  480. mvebu_pcie_handle_iobase_change(port);
  481. if ((old ^ bridge->command) & PCI_COMMAND_MEMORY)
  482. mvebu_pcie_handle_membase_change(port);
  483. break;
  484. }
  485. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  486. bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
  487. break;
  488. case PCI_IO_BASE:
  489. /*
  490. * We also keep bit 1 set, it is a read-only bit that
  491. * indicates we support 32 bits addressing for the
  492. * I/O
  493. */
  494. bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
  495. bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
  496. mvebu_pcie_handle_iobase_change(port);
  497. break;
  498. case PCI_MEMORY_BASE:
  499. bridge->membase = value & 0xffff;
  500. bridge->memlimit = value >> 16;
  501. mvebu_pcie_handle_membase_change(port);
  502. break;
  503. case PCI_IO_BASE_UPPER16:
  504. bridge->iobaseupper = value & 0xffff;
  505. bridge->iolimitupper = value >> 16;
  506. mvebu_pcie_handle_iobase_change(port);
  507. break;
  508. case PCI_PRIMARY_BUS:
  509. bridge->primary_bus = value & 0xff;
  510. bridge->secondary_bus = (value >> 8) & 0xff;
  511. bridge->subordinate_bus = (value >> 16) & 0xff;
  512. bridge->secondary_latency_timer = (value >> 24) & 0xff;
  513. mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
  514. break;
  515. default:
  516. break;
  517. }
  518. return PCIBIOS_SUCCESSFUL;
  519. }
  520. static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
  521. {
  522. return sys->private_data;
  523. }
  524. static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie,
  525. struct pci_bus *bus,
  526. int devfn)
  527. {
  528. int i;
  529. for (i = 0; i < pcie->nports; i++) {
  530. struct mvebu_pcie_port *port = &pcie->ports[i];
  531. if (bus->number == 0 && port->devfn == devfn)
  532. return port;
  533. if (bus->number != 0 &&
  534. bus->number >= port->bridge.secondary_bus &&
  535. bus->number <= port->bridge.subordinate_bus)
  536. return port;
  537. }
  538. return NULL;
  539. }
  540. /* PCI configuration space write function */
  541. static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  542. int where, int size, u32 val)
  543. {
  544. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  545. struct mvebu_pcie_port *port;
  546. int ret;
  547. port = mvebu_pcie_find_port(pcie, bus, devfn);
  548. if (!port)
  549. return PCIBIOS_DEVICE_NOT_FOUND;
  550. /* Access the emulated PCI-to-PCI bridge */
  551. if (bus->number == 0)
  552. return mvebu_sw_pci_bridge_write(port, where, size, val);
  553. if (!mvebu_pcie_link_up(port))
  554. return PCIBIOS_DEVICE_NOT_FOUND;
  555. /*
  556. * On the secondary bus, we don't want to expose any other
  557. * device than the device physically connected in the PCIe
  558. * slot, visible in slot 0. In slot 1, there's a special
  559. * Marvell device that only makes sense when the Armada is
  560. * used as a PCIe endpoint.
  561. */
  562. if (bus->number == port->bridge.secondary_bus &&
  563. PCI_SLOT(devfn) != 0)
  564. return PCIBIOS_DEVICE_NOT_FOUND;
  565. /* Access the real PCIe interface */
  566. ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
  567. where, size, val);
  568. return ret;
  569. }
  570. /* PCI configuration space read function */
  571. static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  572. int size, u32 *val)
  573. {
  574. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  575. struct mvebu_pcie_port *port;
  576. int ret;
  577. port = mvebu_pcie_find_port(pcie, bus, devfn);
  578. if (!port) {
  579. *val = 0xffffffff;
  580. return PCIBIOS_DEVICE_NOT_FOUND;
  581. }
  582. /* Access the emulated PCI-to-PCI bridge */
  583. if (bus->number == 0)
  584. return mvebu_sw_pci_bridge_read(port, where, size, val);
  585. if (!mvebu_pcie_link_up(port)) {
  586. *val = 0xffffffff;
  587. return PCIBIOS_DEVICE_NOT_FOUND;
  588. }
  589. /*
  590. * On the secondary bus, we don't want to expose any other
  591. * device than the device physically connected in the PCIe
  592. * slot, visible in slot 0. In slot 1, there's a special
  593. * Marvell device that only makes sense when the Armada is
  594. * used as a PCIe endpoint.
  595. */
  596. if (bus->number == port->bridge.secondary_bus &&
  597. PCI_SLOT(devfn) != 0) {
  598. *val = 0xffffffff;
  599. return PCIBIOS_DEVICE_NOT_FOUND;
  600. }
  601. /* Access the real PCIe interface */
  602. ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
  603. where, size, val);
  604. return ret;
  605. }
  606. static struct pci_ops mvebu_pcie_ops = {
  607. .read = mvebu_pcie_rd_conf,
  608. .write = mvebu_pcie_wr_conf,
  609. };
  610. static int mvebu_pcie_setup(int nr, struct pci_sys_data *sys)
  611. {
  612. struct mvebu_pcie *pcie = sys_to_pcie(sys);
  613. int i;
  614. pcie->mem.name = "PCI MEM";
  615. pcie->realio.name = "PCI I/O";
  616. if (request_resource(&iomem_resource, &pcie->mem))
  617. return 0;
  618. if (resource_size(&pcie->realio) != 0) {
  619. if (request_resource(&ioport_resource, &pcie->realio)) {
  620. release_resource(&pcie->mem);
  621. return 0;
  622. }
  623. pci_add_resource_offset(&sys->resources, &pcie->realio,
  624. sys->io_offset);
  625. }
  626. pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
  627. pci_add_resource(&sys->resources, &pcie->busn);
  628. for (i = 0; i < pcie->nports; i++) {
  629. struct mvebu_pcie_port *port = &pcie->ports[i];
  630. if (!port->base)
  631. continue;
  632. mvebu_pcie_setup_hw(port);
  633. }
  634. return 1;
  635. }
  636. static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
  637. const struct resource *res,
  638. resource_size_t start,
  639. resource_size_t size,
  640. resource_size_t align)
  641. {
  642. if (dev->bus->number != 0)
  643. return start;
  644. /*
  645. * On the PCI-to-PCI bridge side, the I/O windows must have at
  646. * least a 64 KB size and the memory windows must have at
  647. * least a 1 MB size. Moreover, MBus windows need to have a
  648. * base address aligned on their size, and their size must be
  649. * a power of two. This means that if the BAR doesn't have a
  650. * power of two size, several MBus windows will actually be
  651. * created. We need to ensure that the biggest MBus window
  652. * (which will be the first one) is aligned on its size, which
  653. * explains the rounddown_pow_of_two() being done here.
  654. */
  655. if (res->flags & IORESOURCE_IO)
  656. return round_up(start, max_t(resource_size_t, SZ_64K,
  657. rounddown_pow_of_two(size)));
  658. else if (res->flags & IORESOURCE_MEM)
  659. return round_up(start, max_t(resource_size_t, SZ_1M,
  660. rounddown_pow_of_two(size)));
  661. else
  662. return start;
  663. }
  664. static void mvebu_pcie_enable(struct mvebu_pcie *pcie)
  665. {
  666. struct hw_pci hw;
  667. memset(&hw, 0, sizeof(hw));
  668. #ifdef CONFIG_PCI_MSI
  669. hw.msi_ctrl = pcie->msi;
  670. #endif
  671. hw.nr_controllers = 1;
  672. hw.private_data = (void **)&pcie;
  673. hw.setup = mvebu_pcie_setup;
  674. hw.map_irq = of_irq_parse_and_map_pci;
  675. hw.ops = &mvebu_pcie_ops;
  676. hw.align_resource = mvebu_pcie_align_resource;
  677. pci_common_init_dev(&pcie->pdev->dev, &hw);
  678. }
  679. /*
  680. * Looks up the list of register addresses encoded into the reg =
  681. * <...> property for one that matches the given port/lane. Once
  682. * found, maps it.
  683. */
  684. static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
  685. struct device_node *np,
  686. struct mvebu_pcie_port *port)
  687. {
  688. struct resource regs;
  689. int ret = 0;
  690. ret = of_address_to_resource(np, 0, &regs);
  691. if (ret)
  692. return ERR_PTR(ret);
  693. return devm_ioremap_resource(&pdev->dev, &regs);
  694. }
  695. #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
  696. #define DT_TYPE_IO 0x1
  697. #define DT_TYPE_MEM32 0x2
  698. #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
  699. #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
  700. static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
  701. unsigned long type,
  702. unsigned int *tgt,
  703. unsigned int *attr)
  704. {
  705. const int na = 3, ns = 2;
  706. const __be32 *range;
  707. int rlen, nranges, rangesz, pna, i;
  708. *tgt = -1;
  709. *attr = -1;
  710. range = of_get_property(np, "ranges", &rlen);
  711. if (!range)
  712. return -EINVAL;
  713. pna = of_n_addr_cells(np);
  714. rangesz = pna + na + ns;
  715. nranges = rlen / sizeof(__be32) / rangesz;
  716. for (i = 0; i < nranges; i++, range += rangesz) {
  717. u32 flags = of_read_number(range, 1);
  718. u32 slot = of_read_number(range + 1, 1);
  719. u64 cpuaddr = of_read_number(range + na, pna);
  720. unsigned long rtype;
  721. if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
  722. rtype = IORESOURCE_IO;
  723. else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
  724. rtype = IORESOURCE_MEM;
  725. else
  726. continue;
  727. if (slot == PCI_SLOT(devfn) && type == rtype) {
  728. *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
  729. *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
  730. return 0;
  731. }
  732. }
  733. return -ENOENT;
  734. }
  735. static void mvebu_pcie_msi_enable(struct mvebu_pcie *pcie)
  736. {
  737. struct device_node *msi_node;
  738. msi_node = of_parse_phandle(pcie->pdev->dev.of_node,
  739. "msi-parent", 0);
  740. if (!msi_node)
  741. return;
  742. pcie->msi = of_pci_find_msi_chip_by_node(msi_node);
  743. of_node_put(msi_node);
  744. if (pcie->msi)
  745. pcie->msi->dev = &pcie->pdev->dev;
  746. }
  747. static int mvebu_pcie_suspend(struct device *dev)
  748. {
  749. struct mvebu_pcie *pcie;
  750. int i;
  751. pcie = dev_get_drvdata(dev);
  752. for (i = 0; i < pcie->nports; i++) {
  753. struct mvebu_pcie_port *port = pcie->ports + i;
  754. port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF);
  755. }
  756. return 0;
  757. }
  758. static int mvebu_pcie_resume(struct device *dev)
  759. {
  760. struct mvebu_pcie *pcie;
  761. int i;
  762. pcie = dev_get_drvdata(dev);
  763. for (i = 0; i < pcie->nports; i++) {
  764. struct mvebu_pcie_port *port = pcie->ports + i;
  765. mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF);
  766. mvebu_pcie_setup_hw(port);
  767. }
  768. return 0;
  769. }
  770. static int mvebu_pcie_probe(struct platform_device *pdev)
  771. {
  772. struct mvebu_pcie *pcie;
  773. struct device_node *np = pdev->dev.of_node;
  774. struct device_node *child;
  775. int i, ret;
  776. pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie),
  777. GFP_KERNEL);
  778. if (!pcie)
  779. return -ENOMEM;
  780. pcie->pdev = pdev;
  781. platform_set_drvdata(pdev, pcie);
  782. /* Get the PCIe memory and I/O aperture */
  783. mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
  784. if (resource_size(&pcie->mem) == 0) {
  785. dev_err(&pdev->dev, "invalid memory aperture size\n");
  786. return -EINVAL;
  787. }
  788. mvebu_mbus_get_pcie_io_aperture(&pcie->io);
  789. if (resource_size(&pcie->io) != 0) {
  790. pcie->realio.flags = pcie->io.flags;
  791. pcie->realio.start = PCIBIOS_MIN_IO;
  792. pcie->realio.end = min_t(resource_size_t,
  793. IO_SPACE_LIMIT,
  794. resource_size(&pcie->io));
  795. } else
  796. pcie->realio = pcie->io;
  797. /* Get the bus range */
  798. ret = of_pci_parse_bus_range(np, &pcie->busn);
  799. if (ret) {
  800. dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
  801. ret);
  802. return ret;
  803. }
  804. i = 0;
  805. for_each_child_of_node(pdev->dev.of_node, child) {
  806. if (!of_device_is_available(child))
  807. continue;
  808. i++;
  809. }
  810. pcie->ports = devm_kzalloc(&pdev->dev, i *
  811. sizeof(struct mvebu_pcie_port),
  812. GFP_KERNEL);
  813. if (!pcie->ports)
  814. return -ENOMEM;
  815. i = 0;
  816. for_each_child_of_node(pdev->dev.of_node, child) {
  817. struct mvebu_pcie_port *port = &pcie->ports[i];
  818. enum of_gpio_flags flags;
  819. if (!of_device_is_available(child))
  820. continue;
  821. port->pcie = pcie;
  822. if (of_property_read_u32(child, "marvell,pcie-port",
  823. &port->port)) {
  824. dev_warn(&pdev->dev,
  825. "ignoring PCIe DT node, missing pcie-port property\n");
  826. continue;
  827. }
  828. if (of_property_read_u32(child, "marvell,pcie-lane",
  829. &port->lane))
  830. port->lane = 0;
  831. port->name = kasprintf(GFP_KERNEL, "pcie%d.%d",
  832. port->port, port->lane);
  833. port->devfn = of_pci_get_devfn(child);
  834. if (port->devfn < 0)
  835. continue;
  836. ret = mvebu_get_tgt_attr(np, port->devfn, IORESOURCE_MEM,
  837. &port->mem_target, &port->mem_attr);
  838. if (ret < 0) {
  839. dev_err(&pdev->dev, "PCIe%d.%d: cannot get tgt/attr for mem window\n",
  840. port->port, port->lane);
  841. continue;
  842. }
  843. if (resource_size(&pcie->io) != 0)
  844. mvebu_get_tgt_attr(np, port->devfn, IORESOURCE_IO,
  845. &port->io_target, &port->io_attr);
  846. else {
  847. port->io_target = -1;
  848. port->io_attr = -1;
  849. }
  850. port->reset_gpio = of_get_named_gpio_flags(child,
  851. "reset-gpios", 0, &flags);
  852. if (gpio_is_valid(port->reset_gpio)) {
  853. u32 reset_udelay = 20000;
  854. port->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
  855. port->reset_name = kasprintf(GFP_KERNEL,
  856. "pcie%d.%d-reset", port->port, port->lane);
  857. of_property_read_u32(child, "reset-delay-us",
  858. &reset_udelay);
  859. ret = devm_gpio_request_one(&pdev->dev,
  860. port->reset_gpio, GPIOF_DIR_OUT, port->reset_name);
  861. if (ret) {
  862. if (ret == -EPROBE_DEFER)
  863. return ret;
  864. continue;
  865. }
  866. gpio_set_value(port->reset_gpio,
  867. (port->reset_active_low) ? 1 : 0);
  868. msleep(reset_udelay/1000);
  869. }
  870. port->clk = of_clk_get_by_name(child, NULL);
  871. if (IS_ERR(port->clk)) {
  872. dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n",
  873. port->port, port->lane);
  874. continue;
  875. }
  876. ret = clk_prepare_enable(port->clk);
  877. if (ret)
  878. continue;
  879. port->base = mvebu_pcie_map_registers(pdev, child, port);
  880. if (IS_ERR(port->base)) {
  881. dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n",
  882. port->port, port->lane);
  883. port->base = NULL;
  884. clk_disable_unprepare(port->clk);
  885. continue;
  886. }
  887. mvebu_pcie_set_local_dev_nr(port, 1);
  888. port->dn = child;
  889. mvebu_sw_pci_bridge_init(port);
  890. i++;
  891. }
  892. pcie->nports = i;
  893. for (i = 0; i < (IO_SPACE_LIMIT - SZ_64K); i += SZ_64K)
  894. pci_ioremap_io(i, pcie->io.start + i);
  895. mvebu_pcie_msi_enable(pcie);
  896. mvebu_pcie_enable(pcie);
  897. platform_set_drvdata(pdev, pcie);
  898. return 0;
  899. }
  900. static const struct of_device_id mvebu_pcie_of_match_table[] = {
  901. { .compatible = "marvell,armada-xp-pcie", },
  902. { .compatible = "marvell,armada-370-pcie", },
  903. { .compatible = "marvell,dove-pcie", },
  904. { .compatible = "marvell,kirkwood-pcie", },
  905. {},
  906. };
  907. MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
  908. static struct dev_pm_ops mvebu_pcie_pm_ops = {
  909. .suspend_noirq = mvebu_pcie_suspend,
  910. .resume_noirq = mvebu_pcie_resume,
  911. };
  912. static struct platform_driver mvebu_pcie_driver = {
  913. .driver = {
  914. .name = "mvebu-pcie",
  915. .of_match_table = mvebu_pcie_of_match_table,
  916. /* driver unloading/unbinding currently not supported */
  917. .suppress_bind_attrs = true,
  918. .pm = &mvebu_pcie_pm_ops,
  919. },
  920. .probe = mvebu_pcie_probe,
  921. };
  922. module_platform_driver(mvebu_pcie_driver);
  923. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  924. MODULE_DESCRIPTION("Marvell EBU PCIe driver");
  925. MODULE_LICENSE("GPL v2");