pci-mvebu.c 25 KB

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