pci-mvebu.c 24 KB

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