pci-mvebu.c 24 KB

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