pci-mvebu.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  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. 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 void mvebu_pcie_add_bus(struct pci_bus *bus)
  656. {
  657. struct mvebu_pcie *pcie = sys_to_pcie(bus->sysdata);
  658. bus->msi = pcie->msi;
  659. }
  660. static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev,
  661. const struct resource *res,
  662. resource_size_t start,
  663. resource_size_t size,
  664. resource_size_t align)
  665. {
  666. if (dev->bus->number != 0)
  667. return start;
  668. /*
  669. * On the PCI-to-PCI bridge side, the I/O windows must have at
  670. * least a 64 KB size and the memory windows must have at
  671. * least a 1 MB size. Moreover, MBus windows need to have a
  672. * base address aligned on their size, and their size must be
  673. * a power of two. This means that if the BAR doesn't have a
  674. * power of two size, several MBus windows will actually be
  675. * created. We need to ensure that the biggest MBus window
  676. * (which will be the first one) is aligned on its size, which
  677. * explains the rounddown_pow_of_two() being done here.
  678. */
  679. if (res->flags & IORESOURCE_IO)
  680. return round_up(start, max_t(resource_size_t, SZ_64K,
  681. rounddown_pow_of_two(size)));
  682. else if (res->flags & IORESOURCE_MEM)
  683. return round_up(start, max_t(resource_size_t, SZ_1M,
  684. rounddown_pow_of_two(size)));
  685. else
  686. return start;
  687. }
  688. static void mvebu_pcie_enable(struct mvebu_pcie *pcie)
  689. {
  690. struct hw_pci hw;
  691. memset(&hw, 0, sizeof(hw));
  692. hw.nr_controllers = 1;
  693. hw.private_data = (void **)&pcie;
  694. hw.setup = mvebu_pcie_setup;
  695. hw.scan = mvebu_pcie_scan_bus;
  696. hw.map_irq = of_irq_parse_and_map_pci;
  697. hw.ops = &mvebu_pcie_ops;
  698. hw.align_resource = mvebu_pcie_align_resource;
  699. hw.add_bus = mvebu_pcie_add_bus;
  700. pci_common_init(&hw);
  701. }
  702. /*
  703. * Looks up the list of register addresses encoded into the reg =
  704. * <...> property for one that matches the given port/lane. Once
  705. * found, maps it.
  706. */
  707. static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev,
  708. struct device_node *np,
  709. struct mvebu_pcie_port *port)
  710. {
  711. struct resource regs;
  712. int ret = 0;
  713. ret = of_address_to_resource(np, 0, &regs);
  714. if (ret)
  715. return ERR_PTR(ret);
  716. return devm_ioremap_resource(&pdev->dev, &regs);
  717. }
  718. #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03)
  719. #define DT_TYPE_IO 0x1
  720. #define DT_TYPE_MEM32 0x2
  721. #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF)
  722. #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF)
  723. static int mvebu_get_tgt_attr(struct device_node *np, int devfn,
  724. unsigned long type,
  725. unsigned int *tgt,
  726. unsigned int *attr)
  727. {
  728. const int na = 3, ns = 2;
  729. const __be32 *range;
  730. int rlen, nranges, rangesz, pna, i;
  731. *tgt = -1;
  732. *attr = -1;
  733. range = of_get_property(np, "ranges", &rlen);
  734. if (!range)
  735. return -EINVAL;
  736. pna = of_n_addr_cells(np);
  737. rangesz = pna + na + ns;
  738. nranges = rlen / sizeof(__be32) / rangesz;
  739. for (i = 0; i < nranges; i++) {
  740. u32 flags = of_read_number(range, 1);
  741. u32 slot = of_read_number(range + 1, 1);
  742. u64 cpuaddr = of_read_number(range + na, pna);
  743. unsigned long rtype;
  744. if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO)
  745. rtype = IORESOURCE_IO;
  746. else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32)
  747. rtype = IORESOURCE_MEM;
  748. if (slot == PCI_SLOT(devfn) && type == rtype) {
  749. *tgt = DT_CPUADDR_TO_TARGET(cpuaddr);
  750. *attr = DT_CPUADDR_TO_ATTR(cpuaddr);
  751. return 0;
  752. }
  753. range += rangesz;
  754. }
  755. return -ENOENT;
  756. }
  757. static void mvebu_pcie_msi_enable(struct mvebu_pcie *pcie)
  758. {
  759. struct device_node *msi_node;
  760. msi_node = of_parse_phandle(pcie->pdev->dev.of_node,
  761. "msi-parent", 0);
  762. if (!msi_node)
  763. return;
  764. pcie->msi = of_pci_find_msi_chip_by_node(msi_node);
  765. if (pcie->msi)
  766. pcie->msi->dev = &pcie->pdev->dev;
  767. }
  768. static int mvebu_pcie_probe(struct platform_device *pdev)
  769. {
  770. struct mvebu_pcie *pcie;
  771. struct device_node *np = pdev->dev.of_node;
  772. struct device_node *child;
  773. int i, ret;
  774. pcie = devm_kzalloc(&pdev->dev, sizeof(struct mvebu_pcie),
  775. GFP_KERNEL);
  776. if (!pcie)
  777. return -ENOMEM;
  778. pcie->pdev = pdev;
  779. platform_set_drvdata(pdev, pcie);
  780. /* Get the PCIe memory and I/O aperture */
  781. mvebu_mbus_get_pcie_mem_aperture(&pcie->mem);
  782. if (resource_size(&pcie->mem) == 0) {
  783. dev_err(&pdev->dev, "invalid memory aperture size\n");
  784. return -EINVAL;
  785. }
  786. mvebu_mbus_get_pcie_io_aperture(&pcie->io);
  787. if (resource_size(&pcie->io) != 0) {
  788. pcie->realio.flags = pcie->io.flags;
  789. pcie->realio.start = PCIBIOS_MIN_IO;
  790. pcie->realio.end = min_t(resource_size_t,
  791. IO_SPACE_LIMIT,
  792. resource_size(&pcie->io));
  793. } else
  794. pcie->realio = pcie->io;
  795. /* Get the bus range */
  796. ret = of_pci_parse_bus_range(np, &pcie->busn);
  797. if (ret) {
  798. dev_err(&pdev->dev, "failed to parse bus-range property: %d\n",
  799. ret);
  800. return ret;
  801. }
  802. i = 0;
  803. for_each_child_of_node(pdev->dev.of_node, child) {
  804. if (!of_device_is_available(child))
  805. continue;
  806. i++;
  807. }
  808. pcie->ports = devm_kzalloc(&pdev->dev, i *
  809. sizeof(struct mvebu_pcie_port),
  810. GFP_KERNEL);
  811. if (!pcie->ports)
  812. return -ENOMEM;
  813. i = 0;
  814. for_each_child_of_node(pdev->dev.of_node, child) {
  815. struct mvebu_pcie_port *port = &pcie->ports[i];
  816. enum of_gpio_flags flags;
  817. if (!of_device_is_available(child))
  818. continue;
  819. port->pcie = pcie;
  820. if (of_property_read_u32(child, "marvell,pcie-port",
  821. &port->port)) {
  822. dev_warn(&pdev->dev,
  823. "ignoring PCIe DT node, missing pcie-port property\n");
  824. continue;
  825. }
  826. if (of_property_read_u32(child, "marvell,pcie-lane",
  827. &port->lane))
  828. port->lane = 0;
  829. port->name = kasprintf(GFP_KERNEL, "pcie%d.%d",
  830. port->port, port->lane);
  831. port->devfn = of_pci_get_devfn(child);
  832. if (port->devfn < 0)
  833. continue;
  834. ret = mvebu_get_tgt_attr(np, port->devfn, IORESOURCE_MEM,
  835. &port->mem_target, &port->mem_attr);
  836. if (ret < 0) {
  837. dev_err(&pdev->dev, "PCIe%d.%d: cannot get tgt/attr for mem window\n",
  838. port->port, port->lane);
  839. continue;
  840. }
  841. if (resource_size(&pcie->io) != 0)
  842. mvebu_get_tgt_attr(np, port->devfn, IORESOURCE_IO,
  843. &port->io_target, &port->io_attr);
  844. else {
  845. port->io_target = -1;
  846. port->io_attr = -1;
  847. }
  848. port->reset_gpio = of_get_named_gpio_flags(child,
  849. "reset-gpios", 0, &flags);
  850. if (gpio_is_valid(port->reset_gpio)) {
  851. u32 reset_udelay = 20000;
  852. port->reset_active_low = flags & OF_GPIO_ACTIVE_LOW;
  853. port->reset_name = kasprintf(GFP_KERNEL,
  854. "pcie%d.%d-reset", port->port, port->lane);
  855. of_property_read_u32(child, "reset-delay-us",
  856. &reset_udelay);
  857. ret = devm_gpio_request_one(&pdev->dev,
  858. port->reset_gpio, GPIOF_DIR_OUT, port->reset_name);
  859. if (ret) {
  860. if (ret == -EPROBE_DEFER)
  861. return ret;
  862. continue;
  863. }
  864. gpio_set_value(port->reset_gpio,
  865. (port->reset_active_low) ? 1 : 0);
  866. msleep(reset_udelay/1000);
  867. }
  868. port->clk = of_clk_get_by_name(child, NULL);
  869. if (IS_ERR(port->clk)) {
  870. dev_err(&pdev->dev, "PCIe%d.%d: cannot get clock\n",
  871. port->port, port->lane);
  872. continue;
  873. }
  874. ret = clk_prepare_enable(port->clk);
  875. if (ret)
  876. continue;
  877. port->base = mvebu_pcie_map_registers(pdev, child, port);
  878. if (IS_ERR(port->base)) {
  879. dev_err(&pdev->dev, "PCIe%d.%d: cannot map registers\n",
  880. port->port, port->lane);
  881. port->base = NULL;
  882. clk_disable_unprepare(port->clk);
  883. continue;
  884. }
  885. mvebu_pcie_set_local_dev_nr(port, 1);
  886. port->dn = child;
  887. mvebu_sw_pci_bridge_init(port);
  888. i++;
  889. }
  890. pcie->nports = i;
  891. for (i = 0; i < (IO_SPACE_LIMIT - SZ_64K); i += SZ_64K)
  892. pci_ioremap_io(i, pcie->io.start + i);
  893. mvebu_pcie_msi_enable(pcie);
  894. mvebu_pcie_enable(pcie);
  895. return 0;
  896. }
  897. static const struct of_device_id mvebu_pcie_of_match_table[] = {
  898. { .compatible = "marvell,armada-xp-pcie", },
  899. { .compatible = "marvell,armada-370-pcie", },
  900. { .compatible = "marvell,dove-pcie", },
  901. { .compatible = "marvell,kirkwood-pcie", },
  902. {},
  903. };
  904. MODULE_DEVICE_TABLE(of, mvebu_pcie_of_match_table);
  905. static struct platform_driver mvebu_pcie_driver = {
  906. .driver = {
  907. .owner = THIS_MODULE,
  908. .name = "mvebu-pcie",
  909. .of_match_table = mvebu_pcie_of_match_table,
  910. /* driver unloading/unbinding currently not supported */
  911. .suppress_bind_attrs = true,
  912. },
  913. .probe = mvebu_pcie_probe,
  914. };
  915. module_platform_driver(mvebu_pcie_driver);
  916. MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
  917. MODULE_DESCRIPTION("Marvell EBU PCIe driver");
  918. MODULE_LICENSE("GPLv2");