100-v5.16-PCI-mt7621-Add-MediaTek-MT7621-PCIe-host-controller-.patch 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  1. From: Sergio Paracuellos <[email protected]>
  2. Date: Wed, 22 Sep 2021 07:00:34 +0200
  3. Subject: [PATCH] PCI: mt7621: Add MediaTek MT7621 PCIe host controller driver
  4. Add driver for the PCIe controller of the MT7621 SoC.
  5. [bhelgaas: rename from pci-mt7621.c to pcie-mt7621.c; also rename Kconfig
  6. symbol from PCI_MT7621 to PCIE_MT7621]
  7. Link: https://lore.kernel.org/r/[email protected]
  8. Signed-off-by: Sergio Paracuellos <[email protected]>
  9. Signed-off-by: Lorenzo Pieralisi <[email protected]>
  10. Signed-off-by: Bjorn Helgaas <[email protected]>
  11. Acked-by: Greg Kroah-Hartman <[email protected]>
  12. ---
  13. rename drivers/{staging/mt7621-pci/pci-mt7621.c => pci/controller/pcie-mt7621.c} (95%)
  14. delete mode 100644 drivers/staging/mt7621-pci/Kconfig
  15. delete mode 100644 drivers/staging/mt7621-pci/Makefile
  16. delete mode 100644 drivers/staging/mt7621-pci/TODO
  17. delete mode 100644 drivers/staging/mt7621-pci/mediatek,mt7621-pci.txt
  18. --- a/arch/mips/ralink/Kconfig
  19. +++ b/arch/mips/ralink/Kconfig
  20. @@ -51,7 +51,8 @@ choice
  21. select SYS_SUPPORTS_HIGHMEM
  22. select MIPS_GIC
  23. select CLKSRC_MIPS_GIC
  24. - select HAVE_PCI if PCI_MT7621
  25. + select HAVE_PCI
  26. + select PCI_DRIVERS_GENERIC
  27. select SOC_BUS
  28. endchoice
  29. --- a/drivers/pci/controller/Kconfig
  30. +++ b/drivers/pci/controller/Kconfig
  31. @@ -312,6 +312,14 @@ config PCIE_HISI_ERR
  32. Say Y here if you want error handling support
  33. for the PCIe controller's errors on HiSilicon HIP SoCs
  34. +config PCIE_MT7621
  35. + tristate "MediaTek MT7621 PCIe Controller"
  36. + depends on (RALINK && SOC_MT7621) || (MIPS && COMPILE_TEST)
  37. + select PHY_MT7621_PCI
  38. + default SOC_MT7621
  39. + help
  40. + This selects a driver for the MediaTek MT7621 PCIe Controller.
  41. +
  42. source "drivers/pci/controller/dwc/Kconfig"
  43. source "drivers/pci/controller/mobiveil/Kconfig"
  44. source "drivers/pci/controller/cadence/Kconfig"
  45. --- a/drivers/pci/controller/Makefile
  46. +++ b/drivers/pci/controller/Makefile
  47. @@ -37,6 +37,8 @@ obj-$(CONFIG_VMD) += vmd.o
  48. obj-$(CONFIG_PCIE_BRCMSTB) += pcie-brcmstb.o
  49. obj-$(CONFIG_PCI_LOONGSON) += pci-loongson.o
  50. obj-$(CONFIG_PCIE_HISI_ERR) += pcie-hisi-error.o
  51. +obj-$(CONFIG_PCIE_MT7621) += pcie-mt7621.o
  52. +
  53. # pcie-hisi.o quirks are needed even without CONFIG_PCIE_DW
  54. obj-y += dwc/
  55. obj-y += mobiveil/
  56. --- a/drivers/staging/Kconfig
  57. +++ b/drivers/staging/Kconfig
  58. @@ -86,8 +86,6 @@ source "drivers/staging/vc04_services/Kc
  59. source "drivers/staging/pi433/Kconfig"
  60. -source "drivers/staging/mt7621-pci/Kconfig"
  61. -
  62. source "drivers/staging/mt7621-dma/Kconfig"
  63. source "drivers/staging/ralink-gdma/Kconfig"
  64. --- a/drivers/staging/Makefile
  65. +++ b/drivers/staging/Makefile
  66. @@ -33,7 +33,6 @@ obj-$(CONFIG_KS7010) += ks7010/
  67. obj-$(CONFIG_GREYBUS) += greybus/
  68. obj-$(CONFIG_BCM2835_VCHIQ) += vc04_services/
  69. obj-$(CONFIG_PI433) += pi433/
  70. -obj-$(CONFIG_PCI_MT7621) += mt7621-pci/
  71. obj-$(CONFIG_SOC_MT7621) += mt7621-dma/
  72. obj-$(CONFIG_DMA_RALINK) += ralink-gdma/
  73. obj-$(CONFIG_SOC_MT7621) += mt7621-dts/
  74. --- a/drivers/staging/mt7621-pci/Kconfig
  75. +++ /dev/null
  76. @@ -1,8 +0,0 @@
  77. -# SPDX-License-Identifier: GPL-2.0
  78. -config PCI_MT7621
  79. - tristate "MediaTek MT7621 PCI Controller"
  80. - depends on RALINK
  81. - select PCI_DRIVERS_GENERIC
  82. - help
  83. - This selects a driver for the MediaTek MT7621 PCI Controller.
  84. -
  85. --- a/drivers/staging/mt7621-pci/Makefile
  86. +++ /dev/null
  87. @@ -1,2 +0,0 @@
  88. -# SPDX-License-Identifier: GPL-2.0
  89. -obj-$(CONFIG_PCI_MT7621) += pci-mt7621.o
  90. --- a/drivers/staging/mt7621-pci/TODO
  91. +++ /dev/null
  92. @@ -1,4 +0,0 @@
  93. -
  94. -- general code review and cleanup
  95. -
  96. -Cc: NeilBrown <[email protected]>
  97. --- a/drivers/staging/mt7621-pci/mediatek,mt7621-pci.txt
  98. +++ /dev/null
  99. @@ -1,104 +0,0 @@
  100. -MediaTek MT7621 PCIe controller
  101. -
  102. -Required properties:
  103. -- compatible: "mediatek,mt7621-pci"
  104. -- device_type: Must be "pci"
  105. -- reg: Base addresses and lengths of the PCIe subsys and root ports.
  106. -- bus-range: Range of bus numbers associated with this controller.
  107. -- #address-cells: Address representation for root ports (must be 3)
  108. -- pinctrl-names : The pin control state names.
  109. -- pinctrl-0: The "default" pinctrl state.
  110. -- #size-cells: Size representation for root ports (must be 2)
  111. -- ranges: Ranges for the PCI memory and I/O regions.
  112. -- #interrupt-cells: Must be 1
  113. -- interrupt-map-mask and interrupt-map: Standard PCI IRQ mapping properties.
  114. - Please refer to the standard PCI bus binding document for a more detailed
  115. - explanation.
  116. -- status: either "disabled" or "okay".
  117. -- resets: Must contain an entry for each entry in reset-names.
  118. - See ../reset/reset.txt for details.
  119. -- reset-names: Must be "pcie0", "pcie1", "pcieN"... based on the number of
  120. - root ports.
  121. -- clocks: Must contain an entry for each entry in clock-names.
  122. - See ../clocks/clock-bindings.txt for details.
  123. -- clock-names: Must be "pcie0", "pcie1", "pcieN"... based on the number of
  124. - root ports.
  125. -- reset-gpios: GPIO specs for the reset pins.
  126. -
  127. -In addition, the device tree node must have sub-nodes describing each PCIe port
  128. -interface, having the following mandatory properties:
  129. -
  130. -Required properties:
  131. -- reg: Only the first four bytes are used to refer to the correct bus number
  132. - and device number.
  133. -- #address-cells: Must be 3
  134. -- #size-cells: Must be 2
  135. -- ranges: Sub-ranges distributed from the PCIe controller node. An empty
  136. - property is sufficient.
  137. -- bus-range: Range of bus numbers associated with this port.
  138. -
  139. -Example for MT7621:
  140. -
  141. - pcie: pcie@1e140000 {
  142. - compatible = "mediatek,mt7621-pci";
  143. - reg = <0x1e140000 0x100 /* host-pci bridge registers */
  144. - 0x1e142000 0x100 /* pcie port 0 RC control registers */
  145. - 0x1e143000 0x100 /* pcie port 1 RC control registers */
  146. - 0x1e144000 0x100>; /* pcie port 2 RC control registers */
  147. -
  148. - #address-cells = <3>;
  149. - #size-cells = <2>;
  150. -
  151. - pinctrl-names = "default";
  152. - pinctrl-0 = <&pcie_pins>;
  153. -
  154. - device_type = "pci";
  155. -
  156. - bus-range = <0 255>;
  157. - ranges = <
  158. - 0x02000000 0 0x00000000 0x60000000 0 0x10000000 /* pci memory */
  159. - 0x01000000 0 0x00000000 0x1e160000 0 0x00010000 /* io space */
  160. - >;
  161. -
  162. - #interrupt-cells = <1>;
  163. - interrupt-map-mask = <0xF0000 0 0 1>;
  164. - interrupt-map = <0x10000 0 0 1 &gic GIC_SHARED 4 IRQ_TYPE_LEVEL_HIGH>,
  165. - <0x20000 0 0 1 &gic GIC_SHARED 24 IRQ_TYPE_LEVEL_HIGH>,
  166. - <0x30000 0 0 1 &gic GIC_SHARED 25 IRQ_TYPE_LEVEL_HIGH>;
  167. -
  168. - status = "disabled";
  169. -
  170. - resets = <&rstctrl 24 &rstctrl 25 &rstctrl 26>;
  171. - reset-names = "pcie0", "pcie1", "pcie2";
  172. - clocks = <&clkctrl 24 &clkctrl 25 &clkctrl 26>;
  173. - clock-names = "pcie0", "pcie1", "pcie2";
  174. -
  175. - reset-gpios = <&gpio 19 GPIO_ACTIVE_LOW>,
  176. - <&gpio 8 GPIO_ACTIVE_LOW>,
  177. - <&gpio 7 GPIO_ACTIVE_LOW>;
  178. -
  179. - pcie@0,0 {
  180. - reg = <0x0000 0 0 0 0>;
  181. - #address-cells = <3>;
  182. - #size-cells = <2>;
  183. - ranges;
  184. - bus-range = <0x00 0xff>;
  185. - };
  186. -
  187. - pcie@1,0 {
  188. - reg = <0x0800 0 0 0 0>;
  189. - #address-cells = <3>;
  190. - #size-cells = <2>;
  191. - ranges;
  192. - bus-range = <0x00 0xff>;
  193. - };
  194. -
  195. - pcie@2,0 {
  196. - reg = <0x1000 0 0 0 0>;
  197. - #address-cells = <3>;
  198. - #size-cells = <2>;
  199. - ranges;
  200. - bus-range = <0x00 0xff>;
  201. - };
  202. - };
  203. -
  204. --- a/drivers/staging/mt7621-pci/pci-mt7621.c
  205. +++ /dev/null
  206. @@ -1,601 +0,0 @@
  207. -// SPDX-License-Identifier: GPL-2.0+
  208. -/*
  209. - * BRIEF MODULE DESCRIPTION
  210. - * PCI init for Ralink RT2880 solution
  211. - *
  212. - * Copyright 2007 Ralink Inc. ([email protected])
  213. - *
  214. - * May 2007 Bruce Chang
  215. - * Initial Release
  216. - *
  217. - * May 2009 Bruce Chang
  218. - * support RT2880/RT3883 PCIe
  219. - *
  220. - * May 2011 Bruce Chang
  221. - * support RT6855/MT7620 PCIe
  222. - */
  223. -
  224. -#include <linux/bitops.h>
  225. -#include <linux/clk.h>
  226. -#include <linux/delay.h>
  227. -#include <linux/gpio/consumer.h>
  228. -#include <linux/module.h>
  229. -#include <linux/of.h>
  230. -#include <linux/of_address.h>
  231. -#include <linux/of_pci.h>
  232. -#include <linux/of_platform.h>
  233. -#include <linux/pci.h>
  234. -#include <linux/phy/phy.h>
  235. -#include <linux/platform_device.h>
  236. -#include <linux/reset.h>
  237. -#include <linux/sys_soc.h>
  238. -
  239. -/* MediaTek specific configuration registers */
  240. -#define PCIE_FTS_NUM 0x70c
  241. -#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
  242. -#define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
  243. -
  244. -/* Host-PCI bridge registers */
  245. -#define RALINK_PCI_PCICFG_ADDR 0x0000
  246. -#define RALINK_PCI_PCIMSK_ADDR 0x000C
  247. -#define RALINK_PCI_CONFIG_ADDR 0x0020
  248. -#define RALINK_PCI_CONFIG_DATA 0x0024
  249. -#define RALINK_PCI_MEMBASE 0x0028
  250. -#define RALINK_PCI_IOBASE 0x002C
  251. -
  252. -/* PCIe RC control registers */
  253. -#define RALINK_PCI_ID 0x0030
  254. -#define RALINK_PCI_CLASS 0x0034
  255. -#define RALINK_PCI_SUBID 0x0038
  256. -#define RALINK_PCI_STATUS 0x0050
  257. -
  258. -/* Some definition values */
  259. -#define PCIE_REVISION_ID BIT(0)
  260. -#define PCIE_CLASS_CODE (0x60400 << 8)
  261. -#define PCIE_BAR_MAP_MAX GENMASK(30, 16)
  262. -#define PCIE_BAR_ENABLE BIT(0)
  263. -#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
  264. -#define PCIE_PORT_LINKUP BIT(0)
  265. -#define PCIE_PORT_CNT 3
  266. -
  267. -#define PERST_DELAY_MS 100
  268. -
  269. -/**
  270. - * struct mt7621_pcie_port - PCIe port information
  271. - * @base: I/O mapped register base
  272. - * @list: port list
  273. - * @pcie: pointer to PCIe host info
  274. - * @clk: pointer to the port clock gate
  275. - * @phy: pointer to PHY control block
  276. - * @pcie_rst: pointer to port reset control
  277. - * @gpio_rst: gpio reset
  278. - * @slot: port slot
  279. - * @enabled: indicates if port is enabled
  280. - */
  281. -struct mt7621_pcie_port {
  282. - void __iomem *base;
  283. - struct list_head list;
  284. - struct mt7621_pcie *pcie;
  285. - struct clk *clk;
  286. - struct phy *phy;
  287. - struct reset_control *pcie_rst;
  288. - struct gpio_desc *gpio_rst;
  289. - u32 slot;
  290. - bool enabled;
  291. -};
  292. -
  293. -/**
  294. - * struct mt7621_pcie - PCIe host information
  295. - * @base: IO Mapped Register Base
  296. - * @dev: Pointer to PCIe device
  297. - * @ports: pointer to PCIe port information
  298. - * @resets_inverted: depends on chip revision
  299. - * reset lines are inverted.
  300. - */
  301. -struct mt7621_pcie {
  302. - struct device *dev;
  303. - void __iomem *base;
  304. - struct list_head ports;
  305. - bool resets_inverted;
  306. -};
  307. -
  308. -static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
  309. -{
  310. - return readl_relaxed(pcie->base + reg);
  311. -}
  312. -
  313. -static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
  314. -{
  315. - writel_relaxed(val, pcie->base + reg);
  316. -}
  317. -
  318. -static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
  319. -{
  320. - u32 val = readl_relaxed(pcie->base + reg);
  321. -
  322. - val &= ~clr;
  323. - val |= set;
  324. - writel_relaxed(val, pcie->base + reg);
  325. -}
  326. -
  327. -static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
  328. -{
  329. - return readl_relaxed(port->base + reg);
  330. -}
  331. -
  332. -static inline void pcie_port_write(struct mt7621_pcie_port *port,
  333. - u32 val, u32 reg)
  334. -{
  335. - writel_relaxed(val, port->base + reg);
  336. -}
  337. -
  338. -static inline u32 mt7621_pcie_get_cfgaddr(unsigned int bus, unsigned int slot,
  339. - unsigned int func, unsigned int where)
  340. -{
  341. - return (((where & 0xF00) >> 8) << 24) | (bus << 16) | (slot << 11) |
  342. - (func << 8) | (where & 0xfc) | 0x80000000;
  343. -}
  344. -
  345. -static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
  346. - unsigned int devfn, int where)
  347. -{
  348. - struct mt7621_pcie *pcie = bus->sysdata;
  349. - u32 address = mt7621_pcie_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  350. - PCI_FUNC(devfn), where);
  351. -
  352. - writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
  353. -
  354. - return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
  355. -}
  356. -
  357. -struct pci_ops mt7621_pcie_ops = {
  358. - .map_bus = mt7621_pcie_map_bus,
  359. - .read = pci_generic_config_read,
  360. - .write = pci_generic_config_write,
  361. -};
  362. -
  363. -static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
  364. -{
  365. - u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
  366. -
  367. - pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  368. - return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
  369. -}
  370. -
  371. -static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
  372. - u32 reg, u32 val)
  373. -{
  374. - u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
  375. -
  376. - pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  377. - pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
  378. -}
  379. -
  380. -static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
  381. -{
  382. - if (port->gpio_rst)
  383. - gpiod_set_value(port->gpio_rst, 1);
  384. -}
  385. -
  386. -static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
  387. -{
  388. - if (port->gpio_rst)
  389. - gpiod_set_value(port->gpio_rst, 0);
  390. -}
  391. -
  392. -static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
  393. -{
  394. - return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
  395. -}
  396. -
  397. -static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
  398. -{
  399. - struct mt7621_pcie *pcie = port->pcie;
  400. -
  401. - if (pcie->resets_inverted)
  402. - reset_control_assert(port->pcie_rst);
  403. - else
  404. - reset_control_deassert(port->pcie_rst);
  405. -}
  406. -
  407. -static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
  408. -{
  409. - struct mt7621_pcie *pcie = port->pcie;
  410. -
  411. - if (pcie->resets_inverted)
  412. - reset_control_deassert(port->pcie_rst);
  413. - else
  414. - reset_control_assert(port->pcie_rst);
  415. -}
  416. -
  417. -static int setup_cm_memory_region(struct pci_host_bridge *host)
  418. -{
  419. - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  420. - struct device *dev = pcie->dev;
  421. - struct resource_entry *entry;
  422. - resource_size_t mask;
  423. -
  424. - entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
  425. - if (!entry) {
  426. - dev_err(dev, "Cannot get memory resource\n");
  427. - return -EINVAL;
  428. - }
  429. -
  430. - if (mips_cps_numiocu(0)) {
  431. - /*
  432. - * FIXME: hardware doesn't accept mask values with 1s after
  433. - * 0s (e.g. 0xffef), so it would be great to warn if that's
  434. - * about to happen
  435. - */
  436. - mask = ~(entry->res->end - entry->res->start);
  437. -
  438. - write_gcr_reg1_base(entry->res->start);
  439. - write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
  440. - dev_info(dev, "PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
  441. - (unsigned long long)read_gcr_reg1_base(),
  442. - (unsigned long long)read_gcr_reg1_mask());
  443. - }
  444. -
  445. - return 0;
  446. -}
  447. -
  448. -static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
  449. - struct device_node *node,
  450. - int slot)
  451. -{
  452. - struct mt7621_pcie_port *port;
  453. - struct device *dev = pcie->dev;
  454. - struct platform_device *pdev = to_platform_device(dev);
  455. - char name[10];
  456. - int err;
  457. -
  458. - port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  459. - if (!port)
  460. - return -ENOMEM;
  461. -
  462. - port->base = devm_platform_ioremap_resource(pdev, slot + 1);
  463. - if (IS_ERR(port->base))
  464. - return PTR_ERR(port->base);
  465. -
  466. - port->clk = devm_get_clk_from_child(dev, node, NULL);
  467. - if (IS_ERR(port->clk)) {
  468. - dev_err(dev, "failed to get pcie%d clock\n", slot);
  469. - return PTR_ERR(port->clk);
  470. - }
  471. -
  472. - port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
  473. - if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
  474. - dev_err(dev, "failed to get pcie%d reset control\n", slot);
  475. - return PTR_ERR(port->pcie_rst);
  476. - }
  477. -
  478. - snprintf(name, sizeof(name), "pcie-phy%d", slot);
  479. - port->phy = devm_of_phy_get(dev, node, name);
  480. - if (IS_ERR(port->phy)) {
  481. - dev_err(dev, "failed to get pcie-phy%d\n", slot);
  482. - err = PTR_ERR(port->phy);
  483. - goto remove_reset;
  484. - }
  485. -
  486. - port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
  487. - GPIOD_OUT_LOW);
  488. - if (IS_ERR(port->gpio_rst)) {
  489. - dev_err(dev, "Failed to get GPIO for PCIe%d\n", slot);
  490. - err = PTR_ERR(port->gpio_rst);
  491. - goto remove_reset;
  492. - }
  493. -
  494. - port->slot = slot;
  495. - port->pcie = pcie;
  496. -
  497. - INIT_LIST_HEAD(&port->list);
  498. - list_add_tail(&port->list, &pcie->ports);
  499. -
  500. - return 0;
  501. -
  502. -remove_reset:
  503. - reset_control_put(port->pcie_rst);
  504. - return err;
  505. -}
  506. -
  507. -static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
  508. -{
  509. - struct device *dev = pcie->dev;
  510. - struct platform_device *pdev = to_platform_device(dev);
  511. - struct device_node *node = dev->of_node, *child;
  512. - int err;
  513. -
  514. - pcie->base = devm_platform_ioremap_resource(pdev, 0);
  515. - if (IS_ERR(pcie->base))
  516. - return PTR_ERR(pcie->base);
  517. -
  518. - for_each_available_child_of_node(node, child) {
  519. - int slot;
  520. -
  521. - err = of_pci_get_devfn(child);
  522. - if (err < 0) {
  523. - of_node_put(child);
  524. - dev_err(dev, "failed to parse devfn: %d\n", err);
  525. - return err;
  526. - }
  527. -
  528. - slot = PCI_SLOT(err);
  529. -
  530. - err = mt7621_pcie_parse_port(pcie, child, slot);
  531. - if (err) {
  532. - of_node_put(child);
  533. - return err;
  534. - }
  535. - }
  536. -
  537. - return 0;
  538. -}
  539. -
  540. -static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
  541. -{
  542. - struct mt7621_pcie *pcie = port->pcie;
  543. - struct device *dev = pcie->dev;
  544. - u32 slot = port->slot;
  545. - int err;
  546. -
  547. - err = phy_init(port->phy);
  548. - if (err) {
  549. - dev_err(dev, "failed to initialize port%d phy\n", slot);
  550. - return err;
  551. - }
  552. -
  553. - err = phy_power_on(port->phy);
  554. - if (err) {
  555. - dev_err(dev, "failed to power on port%d phy\n", slot);
  556. - phy_exit(port->phy);
  557. - return err;
  558. - }
  559. -
  560. - port->enabled = true;
  561. -
  562. - return 0;
  563. -}
  564. -
  565. -static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
  566. -{
  567. - struct mt7621_pcie_port *port;
  568. -
  569. - list_for_each_entry(port, &pcie->ports, list) {
  570. - /* PCIe RC reset assert */
  571. - mt7621_control_assert(port);
  572. -
  573. - /* PCIe EP reset assert */
  574. - mt7621_rst_gpio_pcie_assert(port);
  575. - }
  576. -
  577. - msleep(PERST_DELAY_MS);
  578. -}
  579. -
  580. -static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
  581. -{
  582. - struct mt7621_pcie_port *port;
  583. -
  584. - list_for_each_entry(port, &pcie->ports, list)
  585. - mt7621_control_deassert(port);
  586. -}
  587. -
  588. -static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
  589. -{
  590. - struct mt7621_pcie_port *port;
  591. -
  592. - list_for_each_entry(port, &pcie->ports, list)
  593. - mt7621_rst_gpio_pcie_deassert(port);
  594. -
  595. - msleep(PERST_DELAY_MS);
  596. -}
  597. -
  598. -static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
  599. -{
  600. - struct device *dev = pcie->dev;
  601. - struct mt7621_pcie_port *port, *tmp;
  602. - u8 num_disabled = 0;
  603. - int err;
  604. -
  605. - mt7621_pcie_reset_assert(pcie);
  606. - mt7621_pcie_reset_rc_deassert(pcie);
  607. -
  608. - list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  609. - u32 slot = port->slot;
  610. -
  611. - if (slot == 1) {
  612. - port->enabled = true;
  613. - continue;
  614. - }
  615. -
  616. - err = mt7621_pcie_init_port(port);
  617. - if (err) {
  618. - dev_err(dev, "Initiating port %d failed\n", slot);
  619. - list_del(&port->list);
  620. - }
  621. - }
  622. -
  623. - mt7621_pcie_reset_ep_deassert(pcie);
  624. -
  625. - tmp = NULL;
  626. - list_for_each_entry(port, &pcie->ports, list) {
  627. - u32 slot = port->slot;
  628. -
  629. - if (!mt7621_pcie_port_is_linkup(port)) {
  630. - dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
  631. - slot);
  632. - mt7621_control_assert(port);
  633. - port->enabled = false;
  634. - num_disabled++;
  635. -
  636. - if (slot == 0) {
  637. - tmp = port;
  638. - continue;
  639. - }
  640. -
  641. - if (slot == 1 && tmp && !tmp->enabled)
  642. - phy_power_off(tmp->phy);
  643. - }
  644. - }
  645. -
  646. - return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
  647. -}
  648. -
  649. -static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
  650. -{
  651. - struct mt7621_pcie *pcie = port->pcie;
  652. - u32 slot = port->slot;
  653. - u32 val;
  654. -
  655. - /* enable pcie interrupt */
  656. - val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  657. - val |= PCIE_PORT_INT_EN(slot);
  658. - pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  659. -
  660. - /* map 2G DDR region */
  661. - pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
  662. - PCI_BASE_ADDRESS_0);
  663. -
  664. - /* configure class code and revision ID */
  665. - pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
  666. - RALINK_PCI_CLASS);
  667. -
  668. - /* configure RC FTS number to 250 when it leaves L0s */
  669. - val = read_config(pcie, slot, PCIE_FTS_NUM);
  670. - val &= ~PCIE_FTS_NUM_MASK;
  671. - val |= PCIE_FTS_NUM_L0(0x50);
  672. - write_config(pcie, slot, PCIE_FTS_NUM, val);
  673. -}
  674. -
  675. -static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
  676. -{
  677. - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  678. - struct device *dev = pcie->dev;
  679. - struct mt7621_pcie_port *port;
  680. - struct resource_entry *entry;
  681. - int err;
  682. -
  683. - entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
  684. - if (!entry) {
  685. - dev_err(dev, "Cannot get io resource\n");
  686. - return -EINVAL;
  687. - }
  688. -
  689. - /* Setup MEMWIN and IOWIN */
  690. - pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
  691. - pcie_write(pcie, entry->res->start, RALINK_PCI_IOBASE);
  692. -
  693. - list_for_each_entry(port, &pcie->ports, list) {
  694. - if (port->enabled) {
  695. - err = clk_prepare_enable(port->clk);
  696. - if (err) {
  697. - dev_err(dev, "enabling clk pcie%d\n",
  698. - port->slot);
  699. - return err;
  700. - }
  701. -
  702. - mt7621_pcie_enable_port(port);
  703. - dev_info(dev, "PCIE%d enabled\n", port->slot);
  704. - }
  705. - }
  706. -
  707. - return 0;
  708. -}
  709. -
  710. -static int mt7621_pcie_register_host(struct pci_host_bridge *host)
  711. -{
  712. - struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  713. -
  714. - host->ops = &mt7621_pcie_ops;
  715. - host->sysdata = pcie;
  716. - return pci_host_probe(host);
  717. -}
  718. -
  719. -static const struct soc_device_attribute mt7621_pcie_quirks_match[] = {
  720. - { .soc_id = "mt7621", .revision = "E2" },
  721. - { /* sentinel */ }
  722. -};
  723. -
  724. -static int mt7621_pcie_probe(struct platform_device *pdev)
  725. -{
  726. - struct device *dev = &pdev->dev;
  727. - const struct soc_device_attribute *attr;
  728. - struct mt7621_pcie_port *port;
  729. - struct mt7621_pcie *pcie;
  730. - struct pci_host_bridge *bridge;
  731. - int err;
  732. -
  733. - if (!dev->of_node)
  734. - return -ENODEV;
  735. -
  736. - bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  737. - if (!bridge)
  738. - return -ENOMEM;
  739. -
  740. - pcie = pci_host_bridge_priv(bridge);
  741. - pcie->dev = dev;
  742. - platform_set_drvdata(pdev, pcie);
  743. - INIT_LIST_HEAD(&pcie->ports);
  744. -
  745. - attr = soc_device_match(mt7621_pcie_quirks_match);
  746. - if (attr)
  747. - pcie->resets_inverted = true;
  748. -
  749. - err = mt7621_pcie_parse_dt(pcie);
  750. - if (err) {
  751. - dev_err(dev, "Parsing DT failed\n");
  752. - return err;
  753. - }
  754. -
  755. - err = mt7621_pcie_init_ports(pcie);
  756. - if (err) {
  757. - dev_err(dev, "Nothing connected in virtual bridges\n");
  758. - return 0;
  759. - }
  760. -
  761. - err = mt7621_pcie_enable_ports(bridge);
  762. - if (err) {
  763. - dev_err(dev, "Error enabling pcie ports\n");
  764. - goto remove_resets;
  765. - }
  766. -
  767. - err = setup_cm_memory_region(bridge);
  768. - if (err) {
  769. - dev_err(dev, "Error setting up iocu mem regions\n");
  770. - goto remove_resets;
  771. - }
  772. -
  773. - return mt7621_pcie_register_host(bridge);
  774. -
  775. -remove_resets:
  776. - list_for_each_entry(port, &pcie->ports, list)
  777. - reset_control_put(port->pcie_rst);
  778. -
  779. - return err;
  780. -}
  781. -
  782. -static int mt7621_pcie_remove(struct platform_device *pdev)
  783. -{
  784. - struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
  785. - struct mt7621_pcie_port *port;
  786. -
  787. - list_for_each_entry(port, &pcie->ports, list)
  788. - reset_control_put(port->pcie_rst);
  789. -
  790. - return 0;
  791. -}
  792. -
  793. -static const struct of_device_id mt7621_pcie_ids[] = {
  794. - { .compatible = "mediatek,mt7621-pci" },
  795. - {},
  796. -};
  797. -MODULE_DEVICE_TABLE(of, mt7621_pcie_ids);
  798. -
  799. -static struct platform_driver mt7621_pcie_driver = {
  800. - .probe = mt7621_pcie_probe,
  801. - .remove = mt7621_pcie_remove,
  802. - .driver = {
  803. - .name = "mt7621-pci",
  804. - .of_match_table = of_match_ptr(mt7621_pcie_ids),
  805. - },
  806. -};
  807. -builtin_platform_driver(mt7621_pcie_driver);
  808. --- /dev/null
  809. +++ b/drivers/pci/controller/pcie-mt7621.c
  810. @@ -0,0 +1,600 @@
  811. +// SPDX-License-Identifier: GPL-2.0+
  812. +/*
  813. + * BRIEF MODULE DESCRIPTION
  814. + * PCI init for Ralink RT2880 solution
  815. + *
  816. + * Copyright 2007 Ralink Inc. ([email protected])
  817. + *
  818. + * May 2007 Bruce Chang
  819. + * Initial Release
  820. + *
  821. + * May 2009 Bruce Chang
  822. + * support RT2880/RT3883 PCIe
  823. + *
  824. + * May 2011 Bruce Chang
  825. + * support RT6855/MT7620 PCIe
  826. + */
  827. +
  828. +#include <linux/bitops.h>
  829. +#include <linux/clk.h>
  830. +#include <linux/delay.h>
  831. +#include <linux/gpio/consumer.h>
  832. +#include <linux/module.h>
  833. +#include <linux/of.h>
  834. +#include <linux/of_address.h>
  835. +#include <linux/of_pci.h>
  836. +#include <linux/of_platform.h>
  837. +#include <linux/pci.h>
  838. +#include <linux/phy/phy.h>
  839. +#include <linux/platform_device.h>
  840. +#include <linux/reset.h>
  841. +#include <linux/sys_soc.h>
  842. +
  843. +/* MediaTek-specific configuration registers */
  844. +#define PCIE_FTS_NUM 0x70c
  845. +#define PCIE_FTS_NUM_MASK GENMASK(15, 8)
  846. +#define PCIE_FTS_NUM_L0(x) (((x) & 0xff) << 8)
  847. +
  848. +/* Host-PCI bridge registers */
  849. +#define RALINK_PCI_PCICFG_ADDR 0x0000
  850. +#define RALINK_PCI_PCIMSK_ADDR 0x000c
  851. +#define RALINK_PCI_CONFIG_ADDR 0x0020
  852. +#define RALINK_PCI_CONFIG_DATA 0x0024
  853. +#define RALINK_PCI_MEMBASE 0x0028
  854. +#define RALINK_PCI_IOBASE 0x002c
  855. +
  856. +/* PCIe RC control registers */
  857. +#define RALINK_PCI_ID 0x0030
  858. +#define RALINK_PCI_CLASS 0x0034
  859. +#define RALINK_PCI_SUBID 0x0038
  860. +#define RALINK_PCI_STATUS 0x0050
  861. +
  862. +/* Some definition values */
  863. +#define PCIE_REVISION_ID BIT(0)
  864. +#define PCIE_CLASS_CODE (0x60400 << 8)
  865. +#define PCIE_BAR_MAP_MAX GENMASK(30, 16)
  866. +#define PCIE_BAR_ENABLE BIT(0)
  867. +#define PCIE_PORT_INT_EN(x) BIT(20 + (x))
  868. +#define PCIE_PORT_LINKUP BIT(0)
  869. +#define PCIE_PORT_CNT 3
  870. +
  871. +#define PERST_DELAY_MS 100
  872. +
  873. +/**
  874. + * struct mt7621_pcie_port - PCIe port information
  875. + * @base: I/O mapped register base
  876. + * @list: port list
  877. + * @pcie: pointer to PCIe host info
  878. + * @clk: pointer to the port clock gate
  879. + * @phy: pointer to PHY control block
  880. + * @pcie_rst: pointer to port reset control
  881. + * @gpio_rst: gpio reset
  882. + * @slot: port slot
  883. + * @enabled: indicates if port is enabled
  884. + */
  885. +struct mt7621_pcie_port {
  886. + void __iomem *base;
  887. + struct list_head list;
  888. + struct mt7621_pcie *pcie;
  889. + struct clk *clk;
  890. + struct phy *phy;
  891. + struct reset_control *pcie_rst;
  892. + struct gpio_desc *gpio_rst;
  893. + u32 slot;
  894. + bool enabled;
  895. +};
  896. +
  897. +/**
  898. + * struct mt7621_pcie - PCIe host information
  899. + * @base: IO Mapped Register Base
  900. + * @dev: Pointer to PCIe device
  901. + * @ports: pointer to PCIe port information
  902. + * @resets_inverted: depends on chip revision
  903. + * reset lines are inverted.
  904. + */
  905. +struct mt7621_pcie {
  906. + void __iomem *base;
  907. + struct device *dev;
  908. + struct list_head ports;
  909. + bool resets_inverted;
  910. +};
  911. +
  912. +static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
  913. +{
  914. + return readl_relaxed(pcie->base + reg);
  915. +}
  916. +
  917. +static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
  918. +{
  919. + writel_relaxed(val, pcie->base + reg);
  920. +}
  921. +
  922. +static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
  923. +{
  924. + u32 val = readl_relaxed(pcie->base + reg);
  925. +
  926. + val &= ~clr;
  927. + val |= set;
  928. + writel_relaxed(val, pcie->base + reg);
  929. +}
  930. +
  931. +static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
  932. +{
  933. + return readl_relaxed(port->base + reg);
  934. +}
  935. +
  936. +static inline void pcie_port_write(struct mt7621_pcie_port *port,
  937. + u32 val, u32 reg)
  938. +{
  939. + writel_relaxed(val, port->base + reg);
  940. +}
  941. +
  942. +static inline u32 mt7621_pci_get_cfgaddr(unsigned int bus, unsigned int slot,
  943. + unsigned int func, unsigned int where)
  944. +{
  945. + return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) |
  946. + (func << 8) | (where & 0xfc) | 0x80000000;
  947. +}
  948. +
  949. +static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
  950. + unsigned int devfn, int where)
  951. +{
  952. + struct mt7621_pcie *pcie = bus->sysdata;
  953. + u32 address = mt7621_pci_get_cfgaddr(bus->number, PCI_SLOT(devfn),
  954. + PCI_FUNC(devfn), where);
  955. +
  956. + writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
  957. +
  958. + return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
  959. +}
  960. +
  961. +struct pci_ops mt7621_pci_ops = {
  962. + .map_bus = mt7621_pcie_map_bus,
  963. + .read = pci_generic_config_read,
  964. + .write = pci_generic_config_write,
  965. +};
  966. +
  967. +static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
  968. +{
  969. + u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
  970. +
  971. + pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  972. + return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
  973. +}
  974. +
  975. +static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
  976. + u32 reg, u32 val)
  977. +{
  978. + u32 address = mt7621_pci_get_cfgaddr(0, dev, 0, reg);
  979. +
  980. + pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
  981. + pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
  982. +}
  983. +
  984. +static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
  985. +{
  986. + if (port->gpio_rst)
  987. + gpiod_set_value(port->gpio_rst, 1);
  988. +}
  989. +
  990. +static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
  991. +{
  992. + if (port->gpio_rst)
  993. + gpiod_set_value(port->gpio_rst, 0);
  994. +}
  995. +
  996. +static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
  997. +{
  998. + return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
  999. +}
  1000. +
  1001. +static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
  1002. +{
  1003. + struct mt7621_pcie *pcie = port->pcie;
  1004. +
  1005. + if (pcie->resets_inverted)
  1006. + reset_control_assert(port->pcie_rst);
  1007. + else
  1008. + reset_control_deassert(port->pcie_rst);
  1009. +}
  1010. +
  1011. +static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
  1012. +{
  1013. + struct mt7621_pcie *pcie = port->pcie;
  1014. +
  1015. + if (pcie->resets_inverted)
  1016. + reset_control_deassert(port->pcie_rst);
  1017. + else
  1018. + reset_control_assert(port->pcie_rst);
  1019. +}
  1020. +
  1021. +static int setup_cm_memory_region(struct pci_host_bridge *host)
  1022. +{
  1023. + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  1024. + struct device *dev = pcie->dev;
  1025. + struct resource_entry *entry;
  1026. + resource_size_t mask;
  1027. +
  1028. + entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
  1029. + if (!entry) {
  1030. + dev_err(dev, "cannot get memory resource\n");
  1031. + return -EINVAL;
  1032. + }
  1033. +
  1034. + if (mips_cps_numiocu(0)) {
  1035. + /*
  1036. + * FIXME: hardware doesn't accept mask values with 1s after
  1037. + * 0s (e.g. 0xffef), so it would be great to warn if that's
  1038. + * about to happen
  1039. + */
  1040. + mask = ~(entry->res->end - entry->res->start);
  1041. +
  1042. + write_gcr_reg1_base(entry->res->start);
  1043. + write_gcr_reg1_mask(mask | CM_GCR_REGn_MASK_CMTGT_IOCU0);
  1044. + dev_info(dev, "PCI coherence region base: 0x%08llx, mask/settings: 0x%08llx\n",
  1045. + (unsigned long long)read_gcr_reg1_base(),
  1046. + (unsigned long long)read_gcr_reg1_mask());
  1047. + }
  1048. +
  1049. + return 0;
  1050. +}
  1051. +
  1052. +static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
  1053. + struct device_node *node,
  1054. + int slot)
  1055. +{
  1056. + struct mt7621_pcie_port *port;
  1057. + struct device *dev = pcie->dev;
  1058. + struct platform_device *pdev = to_platform_device(dev);
  1059. + char name[10];
  1060. + int err;
  1061. +
  1062. + port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
  1063. + if (!port)
  1064. + return -ENOMEM;
  1065. +
  1066. + port->base = devm_platform_ioremap_resource(pdev, slot + 1);
  1067. + if (IS_ERR(port->base))
  1068. + return PTR_ERR(port->base);
  1069. +
  1070. + port->clk = devm_get_clk_from_child(dev, node, NULL);
  1071. + if (IS_ERR(port->clk)) {
  1072. + dev_err(dev, "failed to get pcie%d clock\n", slot);
  1073. + return PTR_ERR(port->clk);
  1074. + }
  1075. +
  1076. + port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
  1077. + if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
  1078. + dev_err(dev, "failed to get pcie%d reset control\n", slot);
  1079. + return PTR_ERR(port->pcie_rst);
  1080. + }
  1081. +
  1082. + snprintf(name, sizeof(name), "pcie-phy%d", slot);
  1083. + port->phy = devm_of_phy_get(dev, node, name);
  1084. + if (IS_ERR(port->phy)) {
  1085. + dev_err(dev, "failed to get pcie-phy%d\n", slot);
  1086. + err = PTR_ERR(port->phy);
  1087. + goto remove_reset;
  1088. + }
  1089. +
  1090. + port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
  1091. + GPIOD_OUT_LOW);
  1092. + if (IS_ERR(port->gpio_rst)) {
  1093. + dev_err(dev, "failed to get GPIO for PCIe%d\n", slot);
  1094. + err = PTR_ERR(port->gpio_rst);
  1095. + goto remove_reset;
  1096. + }
  1097. +
  1098. + port->slot = slot;
  1099. + port->pcie = pcie;
  1100. +
  1101. + INIT_LIST_HEAD(&port->list);
  1102. + list_add_tail(&port->list, &pcie->ports);
  1103. +
  1104. + return 0;
  1105. +
  1106. +remove_reset:
  1107. + reset_control_put(port->pcie_rst);
  1108. + return err;
  1109. +}
  1110. +
  1111. +static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
  1112. +{
  1113. + struct device *dev = pcie->dev;
  1114. + struct platform_device *pdev = to_platform_device(dev);
  1115. + struct device_node *node = dev->of_node, *child;
  1116. + int err;
  1117. +
  1118. + pcie->base = devm_platform_ioremap_resource(pdev, 0);
  1119. + if (IS_ERR(pcie->base))
  1120. + return PTR_ERR(pcie->base);
  1121. +
  1122. + for_each_available_child_of_node(node, child) {
  1123. + int slot;
  1124. +
  1125. + err = of_pci_get_devfn(child);
  1126. + if (err < 0) {
  1127. + of_node_put(child);
  1128. + dev_err(dev, "failed to parse devfn: %d\n", err);
  1129. + return err;
  1130. + }
  1131. +
  1132. + slot = PCI_SLOT(err);
  1133. +
  1134. + err = mt7621_pcie_parse_port(pcie, child, slot);
  1135. + if (err) {
  1136. + of_node_put(child);
  1137. + return err;
  1138. + }
  1139. + }
  1140. +
  1141. + return 0;
  1142. +}
  1143. +
  1144. +static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
  1145. +{
  1146. + struct mt7621_pcie *pcie = port->pcie;
  1147. + struct device *dev = pcie->dev;
  1148. + u32 slot = port->slot;
  1149. + int err;
  1150. +
  1151. + err = phy_init(port->phy);
  1152. + if (err) {
  1153. + dev_err(dev, "failed to initialize port%d phy\n", slot);
  1154. + return err;
  1155. + }
  1156. +
  1157. + err = phy_power_on(port->phy);
  1158. + if (err) {
  1159. + dev_err(dev, "failed to power on port%d phy\n", slot);
  1160. + phy_exit(port->phy);
  1161. + return err;
  1162. + }
  1163. +
  1164. + port->enabled = true;
  1165. +
  1166. + return 0;
  1167. +}
  1168. +
  1169. +static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
  1170. +{
  1171. + struct mt7621_pcie_port *port;
  1172. +
  1173. + list_for_each_entry(port, &pcie->ports, list) {
  1174. + /* PCIe RC reset assert */
  1175. + mt7621_control_assert(port);
  1176. +
  1177. + /* PCIe EP reset assert */
  1178. + mt7621_rst_gpio_pcie_assert(port);
  1179. + }
  1180. +
  1181. + msleep(PERST_DELAY_MS);
  1182. +}
  1183. +
  1184. +static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
  1185. +{
  1186. + struct mt7621_pcie_port *port;
  1187. +
  1188. + list_for_each_entry(port, &pcie->ports, list)
  1189. + mt7621_control_deassert(port);
  1190. +}
  1191. +
  1192. +static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
  1193. +{
  1194. + struct mt7621_pcie_port *port;
  1195. +
  1196. + list_for_each_entry(port, &pcie->ports, list)
  1197. + mt7621_rst_gpio_pcie_deassert(port);
  1198. +
  1199. + msleep(PERST_DELAY_MS);
  1200. +}
  1201. +
  1202. +static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
  1203. +{
  1204. + struct device *dev = pcie->dev;
  1205. + struct mt7621_pcie_port *port, *tmp;
  1206. + u8 num_disabled = 0;
  1207. + int err;
  1208. +
  1209. + mt7621_pcie_reset_assert(pcie);
  1210. + mt7621_pcie_reset_rc_deassert(pcie);
  1211. +
  1212. + list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
  1213. + u32 slot = port->slot;
  1214. +
  1215. + if (slot == 1) {
  1216. + port->enabled = true;
  1217. + continue;
  1218. + }
  1219. +
  1220. + err = mt7621_pcie_init_port(port);
  1221. + if (err) {
  1222. + dev_err(dev, "initializing port %d failed\n", slot);
  1223. + list_del(&port->list);
  1224. + }
  1225. + }
  1226. +
  1227. + mt7621_pcie_reset_ep_deassert(pcie);
  1228. +
  1229. + tmp = NULL;
  1230. + list_for_each_entry(port, &pcie->ports, list) {
  1231. + u32 slot = port->slot;
  1232. +
  1233. + if (!mt7621_pcie_port_is_linkup(port)) {
  1234. + dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
  1235. + slot);
  1236. + mt7621_control_assert(port);
  1237. + port->enabled = false;
  1238. + num_disabled++;
  1239. +
  1240. + if (slot == 0) {
  1241. + tmp = port;
  1242. + continue;
  1243. + }
  1244. +
  1245. + if (slot == 1 && tmp && !tmp->enabled)
  1246. + phy_power_off(tmp->phy);
  1247. + }
  1248. + }
  1249. +
  1250. + return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
  1251. +}
  1252. +
  1253. +static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
  1254. +{
  1255. + struct mt7621_pcie *pcie = port->pcie;
  1256. + u32 slot = port->slot;
  1257. + u32 val;
  1258. +
  1259. + /* enable pcie interrupt */
  1260. + val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
  1261. + val |= PCIE_PORT_INT_EN(slot);
  1262. + pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
  1263. +
  1264. + /* map 2G DDR region */
  1265. + pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
  1266. + PCI_BASE_ADDRESS_0);
  1267. +
  1268. + /* configure class code and revision ID */
  1269. + pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
  1270. + RALINK_PCI_CLASS);
  1271. +
  1272. + /* configure RC FTS number to 250 when it leaves L0s */
  1273. + val = read_config(pcie, slot, PCIE_FTS_NUM);
  1274. + val &= ~PCIE_FTS_NUM_MASK;
  1275. + val |= PCIE_FTS_NUM_L0(0x50);
  1276. + write_config(pcie, slot, PCIE_FTS_NUM, val);
  1277. +}
  1278. +
  1279. +static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
  1280. +{
  1281. + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  1282. + struct device *dev = pcie->dev;
  1283. + struct mt7621_pcie_port *port;
  1284. + struct resource_entry *entry;
  1285. + int err;
  1286. +
  1287. + entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
  1288. + if (!entry) {
  1289. + dev_err(dev, "cannot get io resource\n");
  1290. + return -EINVAL;
  1291. + }
  1292. +
  1293. + /* Setup MEMWIN and IOWIN */
  1294. + pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
  1295. + pcie_write(pcie, entry->res->start, RALINK_PCI_IOBASE);
  1296. +
  1297. + list_for_each_entry(port, &pcie->ports, list) {
  1298. + if (port->enabled) {
  1299. + err = clk_prepare_enable(port->clk);
  1300. + if (err) {
  1301. + dev_err(dev, "enabling clk pcie%d\n",
  1302. + port->slot);
  1303. + return err;
  1304. + }
  1305. +
  1306. + mt7621_pcie_enable_port(port);
  1307. + dev_info(dev, "PCIE%d enabled\n", port->slot);
  1308. + }
  1309. + }
  1310. +
  1311. + return 0;
  1312. +}
  1313. +
  1314. +static int mt7621_pcie_register_host(struct pci_host_bridge *host)
  1315. +{
  1316. + struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
  1317. +
  1318. + host->ops = &mt7621_pci_ops;
  1319. + host->sysdata = pcie;
  1320. + return pci_host_probe(host);
  1321. +}
  1322. +
  1323. +static const struct soc_device_attribute mt7621_pci_quirks_match[] = {
  1324. + { .soc_id = "mt7621", .revision = "E2" }
  1325. +};
  1326. +
  1327. +static int mt7621_pci_probe(struct platform_device *pdev)
  1328. +{
  1329. + struct device *dev = &pdev->dev;
  1330. + const struct soc_device_attribute *attr;
  1331. + struct mt7621_pcie_port *port;
  1332. + struct mt7621_pcie *pcie;
  1333. + struct pci_host_bridge *bridge;
  1334. + int err;
  1335. +
  1336. + if (!dev->of_node)
  1337. + return -ENODEV;
  1338. +
  1339. + bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
  1340. + if (!bridge)
  1341. + return -ENOMEM;
  1342. +
  1343. + pcie = pci_host_bridge_priv(bridge);
  1344. + pcie->dev = dev;
  1345. + platform_set_drvdata(pdev, pcie);
  1346. + INIT_LIST_HEAD(&pcie->ports);
  1347. +
  1348. + attr = soc_device_match(mt7621_pci_quirks_match);
  1349. + if (attr)
  1350. + pcie->resets_inverted = true;
  1351. +
  1352. + err = mt7621_pcie_parse_dt(pcie);
  1353. + if (err) {
  1354. + dev_err(dev, "parsing DT failed\n");
  1355. + return err;
  1356. + }
  1357. +
  1358. + err = mt7621_pcie_init_ports(pcie);
  1359. + if (err) {
  1360. + dev_err(dev, "nothing connected in virtual bridges\n");
  1361. + return 0;
  1362. + }
  1363. +
  1364. + err = mt7621_pcie_enable_ports(bridge);
  1365. + if (err) {
  1366. + dev_err(dev, "error enabling pcie ports\n");
  1367. + goto remove_resets;
  1368. + }
  1369. +
  1370. + err = setup_cm_memory_region(bridge);
  1371. + if (err) {
  1372. + dev_err(dev, "error setting up iocu mem regions\n");
  1373. + goto remove_resets;
  1374. + }
  1375. +
  1376. + return mt7621_pcie_register_host(bridge);
  1377. +
  1378. +remove_resets:
  1379. + list_for_each_entry(port, &pcie->ports, list)
  1380. + reset_control_put(port->pcie_rst);
  1381. +
  1382. + return err;
  1383. +}
  1384. +
  1385. +static int mt7621_pci_remove(struct platform_device *pdev)
  1386. +{
  1387. + struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
  1388. + struct mt7621_pcie_port *port;
  1389. +
  1390. + list_for_each_entry(port, &pcie->ports, list)
  1391. + reset_control_put(port->pcie_rst);
  1392. +
  1393. + return 0;
  1394. +}
  1395. +
  1396. +static const struct of_device_id mt7621_pci_ids[] = {
  1397. + { .compatible = "mediatek,mt7621-pci" },
  1398. + {},
  1399. +};
  1400. +MODULE_DEVICE_TABLE(of, mt7621_pci_ids);
  1401. +
  1402. +static struct platform_driver mt7621_pci_driver = {
  1403. + .probe = mt7621_pci_probe,
  1404. + .remove = mt7621_pci_remove,
  1405. + .driver = {
  1406. + .name = "mt7621-pci",
  1407. + .of_match_table = of_match_ptr(mt7621_pci_ids),
  1408. + },
  1409. +};
  1410. +builtin_platform_driver(mt7621_pci_driver);