pci-mvebu.c 26 KB

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