pci-mvebu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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 iobaseupper;
  86. u16 iolimitupper;
  87. u8 cappointer;
  88. u8 reserved1;
  89. u16 reserved2;
  90. u32 romaddr;
  91. u8 intline;
  92. u8 intpin;
  93. u16 bridgectrl;
  94. };
  95. struct mvebu_pcie_port;
  96. /* Structure representing all PCIe interfaces */
  97. struct mvebu_pcie {
  98. struct platform_device *pdev;
  99. struct mvebu_pcie_port *ports;
  100. struct resource io;
  101. struct resource realio;
  102. struct resource mem;
  103. struct resource busn;
  104. int nports;
  105. };
  106. /* Structure representing one PCIe interface */
  107. struct mvebu_pcie_port {
  108. char *name;
  109. void __iomem *base;
  110. spinlock_t conf_lock;
  111. int haslink;
  112. u32 port;
  113. u32 lane;
  114. int devfn;
  115. struct clk *clk;
  116. struct mvebu_sw_pci_bridge bridge;
  117. struct device_node *dn;
  118. struct mvebu_pcie *pcie;
  119. phys_addr_t memwin_base;
  120. size_t memwin_size;
  121. phys_addr_t iowin_base;
  122. size_t iowin_size;
  123. };
  124. static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port)
  125. {
  126. return !(readl(port->base + PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN);
  127. }
  128. static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr)
  129. {
  130. u32 stat;
  131. stat = readl(port->base + PCIE_STAT_OFF);
  132. stat &= ~PCIE_STAT_BUS;
  133. stat |= nr << 8;
  134. writel(stat, port->base + PCIE_STAT_OFF);
  135. }
  136. static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr)
  137. {
  138. u32 stat;
  139. stat = readl(port->base + PCIE_STAT_OFF);
  140. stat &= ~PCIE_STAT_DEV;
  141. stat |= nr << 16;
  142. writel(stat, port->base + PCIE_STAT_OFF);
  143. }
  144. /*
  145. * Setup PCIE BARs and Address Decode Wins:
  146. * BAR[0,2] -> disabled, BAR[1] -> covers all DRAM banks
  147. * WIN[0-3] -> DRAM bank[0-3]
  148. */
  149. static void __init mvebu_pcie_setup_wins(struct mvebu_pcie_port *port)
  150. {
  151. const struct mbus_dram_target_info *dram;
  152. u32 size;
  153. int i;
  154. dram = mv_mbus_dram_info();
  155. /* First, disable and clear BARs and windows. */
  156. for (i = 1; i < 3; i++) {
  157. writel(0, port->base + PCIE_BAR_CTRL_OFF(i));
  158. writel(0, port->base + PCIE_BAR_LO_OFF(i));
  159. writel(0, port->base + PCIE_BAR_HI_OFF(i));
  160. }
  161. for (i = 0; i < 5; i++) {
  162. writel(0, port->base + PCIE_WIN04_CTRL_OFF(i));
  163. writel(0, port->base + PCIE_WIN04_BASE_OFF(i));
  164. writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
  165. }
  166. writel(0, port->base + PCIE_WIN5_CTRL_OFF);
  167. writel(0, port->base + PCIE_WIN5_BASE_OFF);
  168. writel(0, port->base + PCIE_WIN5_REMAP_OFF);
  169. /* Setup windows for DDR banks. Count total DDR size on the fly. */
  170. size = 0;
  171. for (i = 0; i < dram->num_cs; i++) {
  172. const struct mbus_dram_window *cs = dram->cs + i;
  173. writel(cs->base & 0xffff0000,
  174. port->base + PCIE_WIN04_BASE_OFF(i));
  175. writel(0, port->base + PCIE_WIN04_REMAP_OFF(i));
  176. writel(((cs->size - 1) & 0xffff0000) |
  177. (cs->mbus_attr << 8) |
  178. (dram->mbus_dram_target_id << 4) | 1,
  179. port->base + PCIE_WIN04_CTRL_OFF(i));
  180. size += cs->size;
  181. }
  182. /* Round up 'size' to the nearest power of two. */
  183. if ((size & (size - 1)) != 0)
  184. size = 1 << fls(size);
  185. /* Setup BAR[1] to all DRAM banks. */
  186. writel(dram->cs[0].base, port->base + PCIE_BAR_LO_OFF(1));
  187. writel(0, port->base + PCIE_BAR_HI_OFF(1));
  188. writel(((size - 1) & 0xffff0000) | 1,
  189. port->base + PCIE_BAR_CTRL_OFF(1));
  190. }
  191. static void __init mvebu_pcie_setup_hw(struct mvebu_pcie_port *port)
  192. {
  193. u16 cmd;
  194. u32 mask;
  195. /* Point PCIe unit MBUS decode windows to DRAM space. */
  196. mvebu_pcie_setup_wins(port);
  197. /* Master + slave enable. */
  198. cmd = readw(port->base + PCIE_CMD_OFF);
  199. cmd |= PCI_COMMAND_IO;
  200. cmd |= PCI_COMMAND_MEMORY;
  201. cmd |= PCI_COMMAND_MASTER;
  202. writew(cmd, port->base + PCIE_CMD_OFF);
  203. /* Enable interrupt lines A-D. */
  204. mask = readl(port->base + PCIE_MASK_OFF);
  205. mask |= PCIE_MASK_ENABLE_INTS;
  206. writel(mask, port->base + PCIE_MASK_OFF);
  207. }
  208. static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port,
  209. struct pci_bus *bus,
  210. u32 devfn, int where, int size, u32 *val)
  211. {
  212. writel(PCIE_CONF_ADDR(bus->number, devfn, where),
  213. port->base + PCIE_CONF_ADDR_OFF);
  214. *val = readl(port->base + PCIE_CONF_DATA_OFF);
  215. if (size == 1)
  216. *val = (*val >> (8 * (where & 3))) & 0xff;
  217. else if (size == 2)
  218. *val = (*val >> (8 * (where & 3))) & 0xffff;
  219. return PCIBIOS_SUCCESSFUL;
  220. }
  221. static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port,
  222. struct pci_bus *bus,
  223. u32 devfn, int where, int size, u32 val)
  224. {
  225. int ret = PCIBIOS_SUCCESSFUL;
  226. writel(PCIE_CONF_ADDR(bus->number, devfn, where),
  227. port->base + PCIE_CONF_ADDR_OFF);
  228. if (size == 4)
  229. writel(val, port->base + PCIE_CONF_DATA_OFF);
  230. else if (size == 2)
  231. writew(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
  232. else if (size == 1)
  233. writeb(val, port->base + PCIE_CONF_DATA_OFF + (where & 3));
  234. else
  235. ret = PCIBIOS_BAD_REGISTER_NUMBER;
  236. return ret;
  237. }
  238. static void mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port)
  239. {
  240. phys_addr_t iobase;
  241. /* Are the new iobase/iolimit values invalid? */
  242. if (port->bridge.iolimit < port->bridge.iobase ||
  243. port->bridge.iolimitupper < port->bridge.iobaseupper) {
  244. /* If a window was configured, remove it */
  245. if (port->iowin_base) {
  246. mvebu_mbus_del_window(port->iowin_base,
  247. port->iowin_size);
  248. port->iowin_base = 0;
  249. port->iowin_size = 0;
  250. }
  251. return;
  252. }
  253. /*
  254. * We read the PCI-to-PCI bridge emulated registers, and
  255. * calculate the base address and size of the address decoding
  256. * window to setup, according to the PCI-to-PCI bridge
  257. * specifications. iobase is the bus address, port->iowin_base
  258. * is the CPU address.
  259. */
  260. iobase = ((port->bridge.iobase & 0xF0) << 8) |
  261. (port->bridge.iobaseupper << 16);
  262. port->iowin_base = port->pcie->io.start + iobase;
  263. port->iowin_size = ((0xFFF | ((port->bridge.iolimit & 0xF0) << 8) |
  264. (port->bridge.iolimitupper << 16)) -
  265. iobase);
  266. mvebu_mbus_add_window_remap_flags(port->name, port->iowin_base,
  267. port->iowin_size,
  268. iobase,
  269. MVEBU_MBUS_PCI_IO);
  270. pci_ioremap_io(iobase, port->iowin_base);
  271. }
  272. static void mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port)
  273. {
  274. /* Are the new membase/memlimit values invalid? */
  275. if (port->bridge.memlimit < port->bridge.membase) {
  276. /* If a window was configured, remove it */
  277. if (port->memwin_base) {
  278. mvebu_mbus_del_window(port->memwin_base,
  279. port->memwin_size);
  280. port->memwin_base = 0;
  281. port->memwin_size = 0;
  282. }
  283. return;
  284. }
  285. /*
  286. * We read the PCI-to-PCI bridge emulated registers, and
  287. * calculate the base address and size of the address decoding
  288. * window to setup, according to the PCI-to-PCI bridge
  289. * specifications.
  290. */
  291. port->memwin_base = ((port->bridge.membase & 0xFFF0) << 16);
  292. port->memwin_size =
  293. (((port->bridge.memlimit & 0xFFF0) << 16) | 0xFFFFF) -
  294. port->memwin_base;
  295. mvebu_mbus_add_window_remap_flags(port->name, port->memwin_base,
  296. port->memwin_size,
  297. MVEBU_MBUS_NO_REMAP,
  298. MVEBU_MBUS_PCI_MEM);
  299. }
  300. /*
  301. * Initialize the configuration space of the PCI-to-PCI bridge
  302. * associated with the given PCIe interface.
  303. */
  304. static void mvebu_sw_pci_bridge_init(struct mvebu_pcie_port *port)
  305. {
  306. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  307. memset(bridge, 0, sizeof(struct mvebu_sw_pci_bridge));
  308. bridge->class = PCI_CLASS_BRIDGE_PCI;
  309. bridge->vendor = PCI_VENDOR_ID_MARVELL;
  310. bridge->device = MARVELL_EMULATED_PCI_PCI_BRIDGE_ID;
  311. bridge->header_type = PCI_HEADER_TYPE_BRIDGE;
  312. bridge->cache_line_size = 0x10;
  313. /* We support 32 bits I/O addressing */
  314. bridge->iobase = PCI_IO_RANGE_TYPE_32;
  315. bridge->iolimit = PCI_IO_RANGE_TYPE_32;
  316. }
  317. /*
  318. * Read the configuration space of the PCI-to-PCI bridge associated to
  319. * the given PCIe interface.
  320. */
  321. static int mvebu_sw_pci_bridge_read(struct mvebu_pcie_port *port,
  322. unsigned int where, int size, u32 *value)
  323. {
  324. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  325. switch (where & ~3) {
  326. case PCI_VENDOR_ID:
  327. *value = bridge->device << 16 | bridge->vendor;
  328. break;
  329. case PCI_COMMAND:
  330. *value = bridge->command;
  331. break;
  332. case PCI_CLASS_REVISION:
  333. *value = bridge->class << 16 | bridge->interface << 8 |
  334. bridge->revision;
  335. break;
  336. case PCI_CACHE_LINE_SIZE:
  337. *value = bridge->bist << 24 | bridge->header_type << 16 |
  338. bridge->latency_timer << 8 | bridge->cache_line_size;
  339. break;
  340. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  341. *value = bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4];
  342. break;
  343. case PCI_PRIMARY_BUS:
  344. *value = (bridge->secondary_latency_timer << 24 |
  345. bridge->subordinate_bus << 16 |
  346. bridge->secondary_bus << 8 |
  347. bridge->primary_bus);
  348. break;
  349. case PCI_IO_BASE:
  350. *value = (bridge->secondary_status << 16 |
  351. bridge->iolimit << 8 |
  352. bridge->iobase);
  353. break;
  354. case PCI_MEMORY_BASE:
  355. *value = (bridge->memlimit << 16 | bridge->membase);
  356. break;
  357. case PCI_PREF_MEMORY_BASE:
  358. *value = 0;
  359. break;
  360. case PCI_IO_BASE_UPPER16:
  361. *value = (bridge->iolimitupper << 16 | bridge->iobaseupper);
  362. break;
  363. case PCI_ROM_ADDRESS1:
  364. *value = 0;
  365. break;
  366. default:
  367. *value = 0xffffffff;
  368. return PCIBIOS_BAD_REGISTER_NUMBER;
  369. }
  370. if (size == 2)
  371. *value = (*value >> (8 * (where & 3))) & 0xffff;
  372. else if (size == 1)
  373. *value = (*value >> (8 * (where & 3))) & 0xff;
  374. return PCIBIOS_SUCCESSFUL;
  375. }
  376. /* Write to the PCI-to-PCI bridge configuration space */
  377. static int mvebu_sw_pci_bridge_write(struct mvebu_pcie_port *port,
  378. unsigned int where, int size, u32 value)
  379. {
  380. struct mvebu_sw_pci_bridge *bridge = &port->bridge;
  381. u32 mask, reg;
  382. int err;
  383. if (size == 4)
  384. mask = 0x0;
  385. else if (size == 2)
  386. mask = ~(0xffff << ((where & 3) * 8));
  387. else if (size == 1)
  388. mask = ~(0xff << ((where & 3) * 8));
  389. else
  390. return PCIBIOS_BAD_REGISTER_NUMBER;
  391. err = mvebu_sw_pci_bridge_read(port, where & ~3, 4, &reg);
  392. if (err)
  393. return err;
  394. value = (reg & mask) | value << ((where & 3) * 8);
  395. switch (where & ~3) {
  396. case PCI_COMMAND:
  397. bridge->command = value & 0xffff;
  398. break;
  399. case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_1:
  400. bridge->bar[((where & ~3) - PCI_BASE_ADDRESS_0) / 4] = value;
  401. break;
  402. case PCI_IO_BASE:
  403. /*
  404. * We also keep bit 1 set, it is a read-only bit that
  405. * indicates we support 32 bits addressing for the
  406. * I/O
  407. */
  408. bridge->iobase = (value & 0xff) | PCI_IO_RANGE_TYPE_32;
  409. bridge->iolimit = ((value >> 8) & 0xff) | PCI_IO_RANGE_TYPE_32;
  410. bridge->secondary_status = value >> 16;
  411. mvebu_pcie_handle_iobase_change(port);
  412. break;
  413. case PCI_MEMORY_BASE:
  414. bridge->membase = value & 0xffff;
  415. bridge->memlimit = value >> 16;
  416. mvebu_pcie_handle_membase_change(port);
  417. break;
  418. case PCI_IO_BASE_UPPER16:
  419. bridge->iobaseupper = value & 0xffff;
  420. bridge->iolimitupper = value >> 16;
  421. mvebu_pcie_handle_iobase_change(port);
  422. break;
  423. case PCI_PRIMARY_BUS:
  424. bridge->primary_bus = value & 0xff;
  425. bridge->secondary_bus = (value >> 8) & 0xff;
  426. bridge->subordinate_bus = (value >> 16) & 0xff;
  427. bridge->secondary_latency_timer = (value >> 24) & 0xff;
  428. mvebu_pcie_set_local_bus_nr(port, bridge->secondary_bus);
  429. break;
  430. default:
  431. break;
  432. }
  433. return PCIBIOS_SUCCESSFUL;
  434. }
  435. static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys)
  436. {
  437. return sys->private_data;
  438. }
  439. static struct mvebu_pcie_port *
  440. mvebu_pcie_find_port(struct mvebu_pcie *pcie, struct pci_bus *bus,
  441. int devfn)
  442. {
  443. int i;
  444. for (i = 0; i < pcie->nports; i++) {
  445. struct mvebu_pcie_port *port = &pcie->ports[i];
  446. if (bus->number == 0 && port->devfn == devfn)
  447. return port;
  448. if (bus->number != 0 &&
  449. bus->number >= port->bridge.secondary_bus &&
  450. bus->number <= port->bridge.subordinate_bus)
  451. return port;
  452. }
  453. return NULL;
  454. }
  455. /* PCI configuration space write function */
  456. static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
  457. int where, int size, u32 val)
  458. {
  459. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  460. struct mvebu_pcie_port *port;
  461. unsigned long flags;
  462. int ret;
  463. port = mvebu_pcie_find_port(pcie, bus, devfn);
  464. if (!port)
  465. return PCIBIOS_DEVICE_NOT_FOUND;
  466. /* Access the emulated PCI-to-PCI bridge */
  467. if (bus->number == 0)
  468. return mvebu_sw_pci_bridge_write(port, where, size, val);
  469. if (!port->haslink)
  470. return PCIBIOS_DEVICE_NOT_FOUND;
  471. /*
  472. * On the secondary bus, we don't want to expose any other
  473. * device than the device physically connected in the PCIe
  474. * slot, visible in slot 0. In slot 1, there's a special
  475. * Marvell device that only makes sense when the Armada is
  476. * used as a PCIe endpoint.
  477. */
  478. if (bus->number == port->bridge.secondary_bus &&
  479. PCI_SLOT(devfn) != 0)
  480. return PCIBIOS_DEVICE_NOT_FOUND;
  481. /* Access the real PCIe interface */
  482. spin_lock_irqsave(&port->conf_lock, flags);
  483. ret = mvebu_pcie_hw_wr_conf(port, bus, devfn,
  484. where, size, val);
  485. spin_unlock_irqrestore(&port->conf_lock, flags);
  486. return ret;
  487. }
  488. /* PCI configuration space read function */
  489. static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
  490. int size, u32 *val)
  491. {
  492. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  493. struct mvebu_pcie_port *port;
  494. unsigned long flags;
  495. int ret;
  496. port = mvebu_pcie_find_port(pcie, bus, devfn);
  497. if (!port) {
  498. *val = 0xffffffff;
  499. return PCIBIOS_DEVICE_NOT_FOUND;
  500. }
  501. /* Access the emulated PCI-to-PCI bridge */
  502. if (bus->number == 0)
  503. return mvebu_sw_pci_bridge_read(port, where, size, val);
  504. if (!port->haslink) {
  505. *val = 0xffffffff;
  506. return PCIBIOS_DEVICE_NOT_FOUND;
  507. }
  508. /*
  509. * On the secondary bus, we don't want to expose any other
  510. * device than the device physically connected in the PCIe
  511. * slot, visible in slot 0. In slot 1, there's a special
  512. * Marvell device that only makes sense when the Armada is
  513. * used as a PCIe endpoint.
  514. */
  515. if (bus->number == port->bridge.secondary_bus &&
  516. 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. { .compatible = "marvell,kirkwood-pcie", },
  724. {},
  725. };
  726. MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
  727. static struct platform_driver mvebu_pcie_driver = {
  728. .driver = {
  729. .owner = THIS_MODULE,
  730. .name = "mvebu-pcie",
  731. .of_match_table =
  732. of_match_ptr(mvebu_pcie_of_match_table),
  733. },
  734. };
  735. static int __init mvebu_pcie_init(void)
  736. {
  737. return platform_driver_probe(&mvebu_pcie_driver,
  738. mvebu_pcie_probe);
  739. }
  740. subsys_initcall(mvebu_pcie_init);
  741. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  742. MODULE_DESCRIPTION("Marvell EBU PCIe driver");
  743. MODULE_LICENSE("GPLv2");