pci-mvebu.c 22 KB

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