pci-mvebu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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/module.h>
  12. #include <linux/mbus.h>
  13. #include <linux/slab.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/of_platform.h>
  19. /*
  20. * PCIe unit register offsets.
  21. */
  22. #define PCIE_DEV_ID_OFF 0x0000
  23. #define PCIE_CMD_OFF 0x0004
  24. #define PCIE_DEV_REV_OFF 0x0008
  25. #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3))
  26. #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3))
  27. #define PCIE_HEADER_LOG_4_OFF 0x0128
  28. #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4))
  29. #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4))
  30. #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4))
  31. #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4))
  32. #define PCIE_WIN5_CTRL_OFF 0x1880
  33. #define PCIE_WIN5_BASE_OFF 0x1884
  34. #define PCIE_WIN5_REMAP_OFF 0x188c
  35. #define PCIE_CONF_ADDR_OFF 0x18f8
  36. #define PCIE_CONF_ADDR_EN 0x80000000
  37. #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc))
  38. #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16)
  39. #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11)
  40. #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8)
  41. #define PCIE_CONF_ADDR(bus, devfn, where) \
  42. (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \
  43. PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \
  44. PCIE_CONF_ADDR_EN)
  45. #define PCIE_CONF_DATA_OFF 0x18fc
  46. #define PCIE_MASK_OFF 0x1910
  47. #define PCIE_MASK_ENABLE_INTS 0x0f000000
  48. #define PCIE_CTRL_OFF 0x1a00
  49. #define PCIE_CTRL_X1_MODE 0x0001
  50. #define PCIE_STAT_OFF 0x1a04
  51. #define PCIE_STAT_BUS 0xff00
  52. #define PCIE_STAT_DEV 0x1f0000
  53. #define PCIE_STAT_LINK_DOWN BIT(0)
  54. #define PCIE_DEBUG_CTRL 0x1a60
  55. #define PCIE_DEBUG_SOFT_RESET BIT(20)
  56. /*
  57. * This product ID is registered by Marvell, and used when the Marvell
  58. * SoC is not the root complex, but an endpoint on the PCIe bus. It is
  59. * therefore safe to re-use this PCI ID for our emulated PCI-to-PCI
  60. * bridge.
  61. */
  62. #define MARVELL_EMULATED_PCI_PCI_BRIDGE_ID 0x7846
  63. /* PCI configuration space of a PCI-to-PCI bridge */
  64. struct mvebu_sw_pci_bridge {
  65. u16 vendor;
  66. u16 device;
  67. u16 command;
  68. u16 status;
  69. u16 class;
  70. u8 interface;
  71. u8 revision;
  72. u8 bist;
  73. u8 header_type;
  74. u8 latency_timer;
  75. u8 cache_line_size;
  76. u32 bar[2];
  77. u8 primary_bus;
  78. u8 secondary_bus;
  79. u8 subordinate_bus;
  80. u8 secondary_latency_timer;
  81. u8 iobase;
  82. u8 iolimit;
  83. u16 secondary_status;
  84. u16 membase;
  85. u16 memlimit;
  86. u16 prefmembase;
  87. u16 prefmemlimit;
  88. u32 prefbaseupper;
  89. u32 preflimitupper;
  90. u16 iobaseupper;
  91. u16 iolimitupper;
  92. u8 cappointer;
  93. u8 reserved1;
  94. u16 reserved2;
  95. u32 romaddr;
  96. u8 intline;
  97. u8 intpin;
  98. u16 bridgectrl;
  99. };
  100. struct mvebu_pcie_port;
  101. /* Structure representing all PCIe interfaces */
  102. struct mvebu_pcie {
  103. struct platform_device *pdev;
  104. struct mvebu_pcie_port *ports;
  105. struct resource io;
  106. struct resource realio;
  107. struct resource mem;
  108. struct resource busn;
  109. int nports;
  110. };
  111. /* Structure representing one PCIe interface */
  112. struct mvebu_pcie_port {
  113. char *name;
  114. void __iomem *base;
  115. spinlock_t conf_lock;
  116. int haslink;
  117. u32 port;
  118. u32 lane;
  119. int devfn;
  120. struct clk *clk;
  121. struct mvebu_sw_pci_bridge bridge;
  122. struct device_node *dn;
  123. struct mvebu_pcie *pcie;
  124. phys_addr_t memwin_base;
  125. size_t memwin_size;
  126. phys_addr_t iowin_base;
  127. size_t iowin_size;
  128. };
  129. static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
  130. {
  131. return !(readl(port->base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
  132. }
  133. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
  134. {
  135. u32 stat;
  136. stat = readl(port->base + PCIE_STAT_OFF);
  137. stat &= ~PCIE_STAT_BUS;
  138. stat |= nr << 8;
  139. writel(stat, port->base + PCIE_STAT_OFF);
  140. }
  141. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
  142. {
  143. u32 stat;
  144. stat = readl(port->base + PCIE_STAT_OFF);
  145. stat &= ~PCIE_STAT_DEV;
  146. stat |= nr << 16;
  147. writel(stat, port->base + PCIE_STAT_OFF);
  148. }
  149. /*
  150. * Setup PCIE BARs and Address Decode Wins:
  151. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  152. * WIN[0-3] -> DRAM bank[0-3]
  153. */
  154. static void __init mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
  155. {
  156. const struct mbus_dram_target_info *dram;
  157. u32 size;
  158. int i;
  159. dram = mv_mbus_dram_info();
  160. /* First, disable and clear BARs and windows. */
  161. for (i = 1; i < 3; i++) {
  162. writel(0, port->base + PCIE_BAR_CTRL_OFF(i));
  163. writel(0, port->base + PCIE_BAR_LO_OFF(i));
  164. writel(0, port->base + PCIE_BAR_HI_OFF(i));
  165. }
  166. for (i = 0; i < 5; i++) {
  167. writel(0, port->base + PCIE_WIN04_CTRL_OFF(i));
  168. writel(0, port->base + PCIE_WIN04_BASE_OFF(i));
  169. writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
  170. }
  171. writel(0, port->base + PCIE_WIN5_CTRL_OFF);
  172. writel(0, port->base + PCIE_WIN5_BASE_OFF);
  173. writel(0, port->base + PCIE_WIN5_REMAP_OFF);
  174. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  175. size = 0;
  176. for (i = 0; i < dram->num_cs; i++) {
  177. const struct mbus_dram_window *cs = dram->cs + i;
  178. writel(cs->base & 0xffff0000,
  179. port->base + PCIE_WIN04_BASE_OFF(i));
  180. writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
  181. writel(((cs->size - 1) & 0xffff0000) |
  182. (cs->mbus_attr << 8) |
  183. (dram->mbus_dram_target_id << 4) | 1,
  184. port->base + PCIE_WIN04_CTRL_OFF(i));
  185. size += cs->size;
  186. }
  187. /* Round up 'size' to the nearest power of two. */
  188. if ((size & (size - 1)) != 0)
  189. size = 1 << fls(size);
  190. /* Setup BAR[1] to all DRAM banks. */
  191. writel(dram->cs[0].base, port->base + PCIE_BAR_LO_OFF(1));
  192. writel(0, port->base + PCIE_BAR_HI_OFF(1));
  193. writel(((size - 1) & 0xffff0000) | 1,
  194. port->base + PCIE_BAR_CTRL_OFF(1));
  195. }
  196. static void __init mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
  197. {
  198. u16 cmd;
  199. u32 mask;
  200. /* Point PCIe unit MBUS decode windows to DRAM space. */
  201. mvebu_pcie_setup_wins(port);
  202. /* Master + slave enable. */
  203. cmd = readw(port->base + PCIE_CMD_OFF);
  204. cmd |= PCI_COMMAND_IO;
  205. cmd |= PCI_COMMAND_MEMORY;
  206. cmd |= PCI_COMMAND_MASTER;
  207. writew(cmd, port->base + PCIE_CMD_OFF);
  208. /* Enable interrupt lines A-D. */
  209. mask = readl(port->base + PCIE_MASK_OFF);
  210. mask |= PCIE_MASK_ENABLE_INTS;
  211. writel(mask, port->base + PCIE_MASK_OFF);
  212. }
  213. static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
  214. struct pci_bus *bus,
  215. u32 devfn, int where, int size, u32 *val)
  216. {
  217. writel(PCIE_CONF_ADDR(bus->number, devfn, where),
  218. port->base + PCIE_CONF_ADDR_OFF);
  219. *val = readl(port->base + PCIE_CONF_DATA_OFF);
  220. if (size == 1)
  221. *val = (*val >> (8 * (where & 3))) & 0xff;
  222. else if (size == 2)
  223. *val = (*val >> (8 * (where & 3))) & 0xffff;
  224. return PCIBIOS_SUCCESSFUL;
  225. }
  226. static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
  227. struct pci_bus *bus,
  228. u32 devfn, int where, int size, u32 val)
  229. {
  230. int ret = PCIBIOS_SUCCESSFUL;
  231. writel(PCIE_CONF_ADDR(bus->number, devfn, where),
  232. port->base + PCIE_CONF_ADDR_OFF);
  233. if (size == 4)
  234. writel(val, port->base + PCIE_CONF_DATA_OFF);
  235. else if (size == 2)
  236. writew(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
  237. else if (size == 1)
  238. writeb(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
  239. else
  240. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  241. return ret;
  242. }
  243. static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
  244. {
  245. phys_addr_t iobase;
  246. /* Are the new iobase/iolimit values invalid? */
  247. if (port->bridge.iolimit < port->bridge.iobase ||
  248. port->bridge.iolimitupper < port->bridge.iobaseupper) {
  249. /* If a window was configured, remove it */
  250. if (port->iowin_base) {
  251. mvebu_mbus_del_window(port->iowin_base,
  252. port->iowin_size);
  253. port->iowin_base = 0;
  254. port->iowin_size = 0;
  255. }
  256. return;
  257. }
  258. /*
  259. * We read the PCI-to-PCI bridge emulated registers, and
  260. * calculate the base address and size of the address decoding
  261. * window to setup, according to the PCI-to-PCI bridge
  262. * specifications. iobase is the bus address, port->iowin_base
  263. * is the CPU address.
  264. */
  265. iobase = ((port->bridge.iobase & 0xF0) << 8) |
  266. (port->bridge.iobaseupper << 16);
  267. port->iowin_base = port->pcie->io.start + iobase;
  268. port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
  269. (port->bridge.iolimitupper << 16)) -
  270. iobase);
  271. mvebu_mbus_add_window_remap_flags(port->name, port->iowin_base,
  272. port->iowin_size,
  273. iobase,
  274. MVEBU_MBUS_PCI_IO);
  275. pci_ioremap_io(iobase, port->iowin_base);
  276. }
  277. static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
  278. {
  279. /* Are the new membase/memlimit values invalid? */
  280. if (port->bridge.memlimit < port->bridge.membase) {
  281. /* If a window was configured, remove it */
  282. if (port->memwin_base) {
  283. mvebu_mbus_del_window(port->memwin_base,
  284. port->memwin_size);
  285. port->memwin_base = 0;
  286. port->memwin_size = 0;
  287. }
  288. return;
  289. }
  290. /*
  291. * We read the PCI-to-PCI bridge emulated registers, and
  292. * calculate the base address and size of the address decoding
  293. * window to setup, according to the PCI-to-PCI bridge
  294. * specifications.
  295. */
  296. port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16);
  297. port->memwin_size =
  298. (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
  299. port->memwin_base;
  300. mvebu_mbus_add_window_remap_flags(port->name, port->memwin_base,
  301. port->memwin_size,
  302. MVEBU_MBUS_NO_REMAP,
  303. MVEBU_MBUS_PCI_MEM);
  304. }
  305. /*
  306. * Initialize the configuration space of the PCI-to-PCI bridge
  307. * associated with the given PCIe interface.
  308. */
  309. static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
  310. {
  311. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  312. memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
  313. bridge->status = PCI_STATUS_CAP_LIST;
  314. bridge->class = PCI_CLASS_BRIDGE_PCI;
  315. bridge->vendor = PCI_VENDOR_ID_MARVELL;
  316. bridge->device = MARVELL_EMULATED_PCI_PCI_BRIDGE_ID;
  317. bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
  318. bridge->cache_line_size = 0x10;
  319. /* We support 32 bits I/O addressing */
  320. bridge->iobase = PCI_IO_RANGE_TYPE_32;
  321. bridge->iolimit = PCI_IO_RANGE_TYPE_32;
  322. }
  323. /*
  324. * Read the configuration space of the PCI-to-PCI bridge associated to
  325. * the given PCIe interface.
  326. */
  327. static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
  328. unsigned int where, int size, u32 *value)
  329. {
  330. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  331. switch (where & ~3) {
  332. case PCI_VENDOR_ID:
  333. *value = bridge->device << 16 | bridge->vendor;
  334. break;
  335. case PCI_COMMAND:
  336. *value = bridge->status << 16 | bridge->command;
  337. break;
  338. case PCI_CLASS_REVISION:
  339. *value = bridge->class << 16 | bridge->interface << 8 |
  340. bridge->revision;
  341. break;
  342. case PCI_CACHE_LINE_SIZE:
  343. *value = bridge->bist << 24 | bridge->header_type << 16 |
  344. bridge->latency_timer << 8 | bridge->cache_line_size;
  345. break;
  346. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  347. *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
  348. break;
  349. case PCI_PRIMARY_BUS:
  350. *value = (bridge->secondary_latency_timer << 24 |
  351. bridge->subordinate_bus << 16 |
  352. bridge->secondary_bus << 8 |
  353. bridge->primary_bus);
  354. break;
  355. case PCI_IO_BASE:
  356. *value = (bridge->secondary_status << 16 |
  357. bridge->iolimit << 8 |
  358. bridge->iobase);
  359. break;
  360. case PCI_MEMORY_BASE:
  361. *value = (bridge->memlimit << 16 | bridge->membase);
  362. break;
  363. case PCI_PREF_MEMORY_BASE:
  364. *value = (bridge->prefmemlimit << 16 | bridge->prefmembase);
  365. break;
  366. case PCI_PREF_BASE_UPPER32:
  367. *value = bridge->prefbaseupper;
  368. break;
  369. case PCI_PREF_LIMIT_UPPER32:
  370. *value = bridge->preflimitupper;
  371. break;
  372. case PCI_IO_BASE_UPPER16:
  373. *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
  374. break;
  375. case PCI_ROM_ADDRESS1:
  376. *value = 0;
  377. break;
  378. default:
  379. *value = 0xffffffff;
  380. return PCIBIOS_BAD_REGISTER_NUMBER;
  381. }
  382. if (size == 2)
  383. *value = (*value >> (8 * (where & 3))) & 0xffff;
  384. else if (size == 1)
  385. *value = (*value >> (8 * (where & 3))) & 0xff;
  386. return PCIBIOS_SUCCESSFUL;
  387. }
  388. /* Write to the PCI-to-PCI bridge configuration space */
  389. static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
  390. unsigned int where, int size, u32 value)
  391. {
  392. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  393. u32 mask, reg;
  394. int err;
  395. if (size == 4)
  396. mask = 0x0;
  397. else if (size == 2)
  398. mask = ~(0xffff << ((where & 3) * 8));
  399. else if (size == 1)
  400. mask = ~(0xff << ((where & 3) * 8));
  401. else
  402. return PCIBIOS_BAD_REGISTER_NUMBER;
  403. err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
  404. if (err)
  405. return err;
  406. value = (reg & mask) | value << ((where & 3) * 8);
  407. switch (where & ~3) {
  408. case PCI_COMMAND:
  409. bridge->command = value & 0xffff;
  410. bridge->status = value >> 16;
  411. break;
  412. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  413. bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
  414. break;
  415. case PCI_IO_BASE:
  416. /*
  417. * We also keep bit 1 set, it is a read-only bit that
  418. * indicates we support 32 bits addressing for the
  419. * I/O
  420. */
  421. bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
  422. bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
  423. bridge->secondary_status = value >> 16;
  424. mvebu_pcie_handle_iobase_change(port);
  425. break;
  426. case PCI_MEMORY_BASE:
  427. bridge->membase = value & 0xffff;
  428. bridge->memlimit = value >> 16;
  429. mvebu_pcie_handle_membase_change(port);
  430. break;
  431. case PCI_PREF_MEMORY_BASE:
  432. bridge->prefmembase = value & 0xffff;
  433. bridge->prefmemlimit = value >> 16;
  434. break;
  435. case PCI_PREF_BASE_UPPER32:
  436. bridge->prefbaseupper = value;
  437. break;
  438. case PCI_PREF_LIMIT_UPPER32:
  439. bridge->preflimitupper = value;
  440. break;
  441. case PCI_IO_BASE_UPPER16:
  442. bridge->iobaseupper = value & 0xffff;
  443. bridge->iolimitupper = value >> 16;
  444. mvebu_pcie_handle_iobase_change(port);
  445. break;
  446. case PCI_PRIMARY_BUS:
  447. bridge->primary_bus = value & 0xff;
  448. bridge->secondary_bus = (value >> 8) & 0xff;
  449. bridge->subordinate_bus = (value >> 16) & 0xff;
  450. bridge->secondary_latency_timer = (value >> 24) & 0xff;
  451. mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
  452. break;
  453. default:
  454. break;
  455. }
  456. return PCIBIOS_SUCCESSFUL;
  457. }
  458. static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
  459. {
  460. return sys->private_data;
  461. }
  462. static struct mvebu_pcie_port *
  463. mvebu_pcie_find_port(struct mvebu_pcie *pcie, struct pci_bus *bus,
  464. int devfn)
  465. {
  466. int i;
  467. for (i = 0; i < pcie->nports; i++) {
  468. struct mvebu_pcie_port *port = &pcie->ports[i];
  469. if (bus->number == 0 && port->devfn == devfn)
  470. return port;
  471. if (bus->number != 0 &&
  472. port->bridge.secondary_bus == bus->number)
  473. return port;
  474. }
  475. return NULL;
  476. }
  477. /* PCI configuration space write function */
  478. static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  479. int where, int size, u32 val)
  480. {
  481. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  482. struct mvebu_pcie_port *port;
  483. unsigned long flags;
  484. int ret;
  485. port = mvebu_pcie_find_port(pcie, bus, devfn);
  486. if (!port)
  487. return PCIBIOS_DEVICE_NOT_FOUND;
  488. /* Access the emulated PCI-to-PCI bridge */
  489. if (bus->number == 0)
  490. return mvebu_sw_pci_bridge_write(port, where, size, val);
  491. if (!port->haslink || PCI_SLOT(devfn) != 0)
  492. return PCIBIOS_DEVICE_NOT_FOUND;
  493. /* Access the real PCIe interface */
  494. spin_lock_irqsave(&port->conf_lock, flags);
  495. ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
  496. where, size, val);
  497. spin_unlock_irqrestore(&port->conf_lock, flags);
  498. return ret;
  499. }
  500. /* PCI configuration space read function */
  501. static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  502. int size, u32 *val)
  503. {
  504. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  505. struct mvebu_pcie_port *port;
  506. unsigned long flags;
  507. int ret;
  508. port = mvebu_pcie_find_port(pcie, bus, devfn);
  509. if (!port) {
  510. *val = 0xffffffff;
  511. return PCIBIOS_DEVICE_NOT_FOUND;
  512. }
  513. /* Access the emulated PCI-to-PCI bridge */
  514. if (bus->number == 0)
  515. return mvebu_sw_pci_bridge_read(port, where, size, val);
  516. if (!port->haslink || PCI_SLOT(devfn) != 0) {
  517. *val = 0xffffffff;
  518. return PCIBIOS_DEVICE_NOT_FOUND;
  519. }
  520. /* Access the real PCIe interface */
  521. spin_lock_irqsave(&port->conf_lock, flags);
  522. ret = mvebu_pcie_hw_rd_conf(port, bus, devfn,
  523. where, size, val);
  524. spin_unlock_irqrestore(&port->conf_lock, flags);
  525. return ret;
  526. }
  527. static struct pci_ops mvebu_pcie_ops = {
  528. .read = mvebu_pcie_rd_conf,
  529. .write = mvebu_pcie_wr_conf,
  530. };
  531. static int __init mvebu_pcie_setup(int nr, struct pci_sys_data *sys)
  532. {
  533. struct mvebu_pcie *pcie = sys_to_pcie(sys);
  534. int i;
  535. pci_add_resource_offset(&sys->resources, &pcie->realio, sys->io_offset);
  536. pci_add_resource_offset(&sys->resources, &pcie->mem, sys->mem_offset);
  537. pci_add_resource(&sys->resources, &pcie->busn);
  538. for (i = 0; i < pcie->nports; i++) {
  539. struct mvebu_pcie_port *port = &pcie->ports[i];
  540. mvebu_pcie_setup_hw(port);
  541. }
  542. return 1;
  543. }
  544. static int __init mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin)
  545. {
  546. struct of_irq oirq;
  547. int ret;
  548. ret = of_irq_map_pci(dev, &oirq);
  549. if (ret)
  550. return ret;
  551. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  552. oirq.size);
  553. }
  554. static struct pci_bus *mvebu_pcie_scan_bus(int nr, struct pci_sys_data *sys)
  555. {
  556. struct mvebu_pcie *pcie = sys_to_pcie(sys);
  557. struct pci_bus *bus;
  558. bus = pci_create_root_bus(&pcie->pdev->dev, sys->busnr,
  559. &mvebu_pcie_ops, sys, &sys->resources);
  560. if (!bus)
  561. return NULL;
  562. pci_scan_child_bus(bus);
  563. return bus;
  564. }
  565. resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
  566. const struct resource *res,
  567. resource_size_t start,
  568. resource_size_t size,
  569. resource_size_t align)
  570. {
  571. if (dev->bus->number != 0)
  572. return start;
  573. /*
  574. * On the PCI-to-PCI bridge side, the I/O windows must have at
  575. * least a 64 KB size and be aligned on their size, and the
  576. * memory windows must have at least a 1 MB size and be
  577. * aligned on their size
  578. */
  579. if (res->flags & IORESOURCE_IO)
  580. return round_up(start, max((resource_size_t)SZ_64K, size));
  581. else if (res->flags & IORESOURCE_MEM)
  582. return round_up(start, max((resource_size_t)SZ_1M, size));
  583. else
  584. return start;
  585. }
  586. static void __init mvebu_pcie_enable(struct mvebu_pcie *pcie)
  587. {
  588. struct hw_pci hw;
  589. memset(&hw, 0, sizeof(hw));
  590. hw.nr_controllers = 1;
  591. hw.private_data = (void **)&pcie;
  592. hw.setup = mvebu_pcie_setup;
  593. hw.scan = mvebu_pcie_scan_bus;
  594. hw.map_irq = mvebu_pcie_map_irq;
  595. hw.ops = &mvebu_pcie_ops;
  596. hw.align_resource = mvebu_pcie_align_resource;
  597. pci_common_init(&hw);
  598. }
  599. /*
  600. * Looks up the list of register addresses encoded into the reg =
  601. * <...> property for one that matches the given port/lane. Once
  602. * found, maps it.
  603. */
  604. static void __iomem * __init
  605. mvebu_pcie_map_registers(struct platform_device *pdev,
  606. struct device_node *np,
  607. struct mvebu_pcie_port *port)
  608. {
  609. struct resource regs;
  610. int ret = 0;
  611. ret = of_address_to_resource(np, 0, &regs);
  612. if (ret)
  613. return NULL;
  614. return devm_request_and_ioremap(&pdev->dev, &regs);
  615. }
  616. static int __init mvebu_pcie_probe(struct platform_device *pdev)
  617. {
  618. struct mvebu_pcie *pcie;
  619. struct device_node *np = pdev->dev.of_node;
  620. struct of_pci_range range;
  621. struct of_pci_range_parser parser;
  622. struct device_node *child;
  623. int i, ret;
  624. pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie),
  625. GFP_KERNEL);
  626. if (!pcie)
  627. return -ENOMEM;
  628. pcie->pdev = pdev;
  629. if (of_pci_range_parser_init(&parser, np))
  630. return -EINVAL;
  631. /* Get the I/O and memory ranges from DT */
  632. for_each_of_pci_range(&parser, &range) {
  633. unsigned long restype = range.flags & IORESOURCE_TYPE_BITS;
  634. if (restype == IORESOURCE_IO) {
  635. of_pci_range_to_resource(&range, np, &pcie->io);
  636. of_pci_range_to_resource(&range, np, &pcie->realio);
  637. pcie->io.name = "I/O";
  638. pcie->realio.start = max_t(resource_size_t,
  639. PCIBIOS_MIN_IO,
  640. range.pci_addr);
  641. pcie->realio.end = min_t(resource_size_t,
  642. IO_SPACE_LIMIT,
  643. range.pci_addr + range.size);
  644. }
  645. if (restype == IORESOURCE_MEM) {
  646. of_pci_range_to_resource(&range, np, &pcie->mem);
  647. pcie->mem.name = "MEM";
  648. }
  649. }
  650. /* Get the bus range */
  651. ret = of_pci_parse_bus_range(np, &pcie->busn);
  652. if (ret) {
  653. dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
  654. ret);
  655. return ret;
  656. }
  657. for_each_child_of_node(pdev->dev.of_node, child) {
  658. if (!of_device_is_available(child))
  659. continue;
  660. pcie->nports++;
  661. }
  662. pcie->ports = devm_kzalloc(&pdev->dev, pcie->nports *
  663. sizeof(struct mvebu_pcie_port),
  664. GFP_KERNEL);
  665. if (!pcie->ports)
  666. return -ENOMEM;
  667. i = 0;
  668. for_each_child_of_node(pdev->dev.of_node, child) {
  669. struct mvebu_pcie_port *port = &pcie->ports[i];
  670. if (!of_device_is_available(child))
  671. continue;
  672. port->pcie = pcie;
  673. if (of_property_read_u32(child, "marvell,pcie-port",
  674. &port->port)) {
  675. dev_warn(&pdev->dev,
  676. "ignoring PCIe DT node, missing pcie-port property\n");
  677. continue;
  678. }
  679. if (of_property_read_u32(child, "marvell,pcie-lane",
  680. &port->lane))
  681. port->lane = 0;
  682. port->name = kasprintf(GFP_KERNEL, "pcie%d.%d",
  683. port->port, port->lane);
  684. port->devfn = of_pci_get_devfn(child);
  685. if (port->devfn < 0)
  686. continue;
  687. port->base = mvebu_pcie_map_registers(pdev, child, port);
  688. if (!port->base) {
  689. dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n",
  690. port->port, port->lane);
  691. continue;
  692. }
  693. mvebu_pcie_set_local_dev_nr(port, 1);
  694. if (mvebu_pcie_link_up(port)) {
  695. port->haslink = 1;
  696. dev_info(&pdev->dev, "PCIe%d.%d: link up\n",
  697. port->port, port->lane);
  698. } else {
  699. port->haslink = 0;
  700. dev_info(&pdev->dev, "PCIe%d.%d: link down\n",
  701. port->port, port->lane);
  702. }
  703. port->clk = of_clk_get_by_name(child, NULL);
  704. if (IS_ERR(port->clk)) {
  705. dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n",
  706. port->port, port->lane);
  707. iounmap(port->base);
  708. port->haslink = 0;
  709. continue;
  710. }
  711. port->dn = child;
  712. clk_prepare_enable(port->clk);
  713. spin_lock_init(&port->conf_lock);
  714. mvebu_sw_pci_bridge_init(port);
  715. i++;
  716. }
  717. mvebu_pcie_enable(pcie);
  718. return 0;
  719. }
  720. static const struct of_device_id mvebu_pcie_of_match_table[] = {
  721. { .compatible = "marvell,armada-xp-pcie", },
  722. { .compatible = "marvell,armada-370-pcie", },
  723. {},
  724. };
  725. MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
  726. static struct platform_driver mvebu_pcie_driver = {
  727. .driver = {
  728. .owner = THIS_MODULE,
  729. .name = "mvebu-pcie",
  730. .of_match_table =
  731. of_match_ptr(mvebu_pcie_of_match_table),
  732. },
  733. };
  734. static int __init mvebu_pcie_init(void)
  735. {
  736. return platform_driver_probe(&mvebu_pcie_driver,
  737. mvebu_pcie_probe);
  738. }
  739. subsys_initcall(mvebu_pcie_init);
  740. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  741. MODULE_DESCRIPTION("Marvell EBU PCIe driver");
  742. MODULE_LICENSE("GPLv2");