pci-mvebu.c 23 KB

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