pci-mvebu.c 28 KB

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