pci-mvebu.c 26 KB

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