pci-mvebu.c 25 KB

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