pci-mvebu.c 26 KB

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