kernel: bump 4.19 to 4.19.88
[oweals/openwrt.git] / target / linux / mediatek / patches-4.19 / 0101-pci-mediatek-backport-fix-pcie.patch
1 --- a/drivers/clk/clk-devres.c
2 +++ b/drivers/clk/clk-devres.c
3 @@ -34,6 +34,17 @@ struct clk *devm_clk_get(struct device *
4  }
5  EXPORT_SYMBOL(devm_clk_get);
6  
7 +struct clk *devm_clk_get_optional(struct device *dev, const char *id)
8 +{
9 +        struct clk *clk = devm_clk_get(dev, id);
10 +
11 +        if (clk == ERR_PTR(-ENOENT))
12 +                return NULL;
13 +
14 +        return clk;
15 +}
16 +EXPORT_SYMBOL(devm_clk_get_optional);
17 +
18  struct clk_bulk_devres {
19         struct clk_bulk_data *clks;
20         int num_clks;
21 --- a/drivers/pci/controller/pcie-mediatek.c
22 +++ b/drivers/pci/controller/pcie-mediatek.c
23 @@ -15,6 +15,7 @@
24  #include <linux/irqdomain.h>
25  #include <linux/kernel.h>
26  #include <linux/msi.h>
27 +#include <linux/module.h>
28  #include <linux/of_address.h>
29  #include <linux/of_pci.h>
30  #include <linux/of_platform.h>
31 @@ -162,6 +163,7 @@ struct mtk_pcie_soc {
32   * @phy: pointer to PHY control block
33   * @lane: lane count
34   * @slot: port slot
35 + * @irq: GIC irq
36   * @irq_domain: legacy INTx IRQ domain
37   * @inner_domain: inner IRQ domain
38   * @msi_domain: MSI IRQ domain
39 @@ -182,6 +184,7 @@ struct mtk_pcie_port {
40         struct phy *phy;
41         u32 lane;
42         u32 slot;
43 +       int irq;
44         struct irq_domain *irq_domain;
45         struct irq_domain *inner_domain;
46         struct irq_domain *msi_domain;
47 @@ -225,10 +228,8 @@ static void mtk_pcie_subsys_powerdown(st
48  
49         clk_disable_unprepare(pcie->free_ck);
50  
51 -       if (dev->pm_domain) {
52 -               pm_runtime_put_sync(dev);
53 -               pm_runtime_disable(dev);
54 -       }
55 +       pm_runtime_put_sync(dev);
56 +       pm_runtime_disable(dev);
57  }
58  
59  static void mtk_pcie_port_free(struct mtk_pcie_port *port)
60 @@ -532,6 +533,27 @@ static void mtk_pcie_enable_msi(struct m
61         writel(val, port->base + PCIE_INT_MASK);
62  }
63  
64 +static void mtk_pcie_irq_teardown(struct mtk_pcie *pcie)
65 +{
66 +       struct mtk_pcie_port *port, *tmp;
67 +
68 +       list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
69 +               irq_set_chained_handler_and_data(port->irq, NULL, NULL);
70 +
71 +               if (port->irq_domain)
72 +                       irq_domain_remove(port->irq_domain);
73 +
74 +               if (IS_ENABLED(CONFIG_PCI_MSI)) {
75 +                       if (port->msi_domain)
76 +                               irq_domain_remove(port->msi_domain);
77 +                       if (port->inner_domain)
78 +                               irq_domain_remove(port->inner_domain);
79 +               }
80 +
81 +               irq_dispose_mapping(port->irq);
82 +       }
83 +}
84 +
85  static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
86                              irq_hw_number_t hwirq)
87  {
88 @@ -561,6 +583,7 @@ static int mtk_pcie_init_irq_domain(stru
89  
90         port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
91                                                  &intx_domain_ops, port);
92 +       of_node_put(pcie_intc_node);
93         if (!port->irq_domain) {
94                 dev_err(dev, "failed to get INTx IRQ domain\n");
95                 return -ENODEV;
96 @@ -622,7 +645,7 @@ static int mtk_pcie_setup_irq(struct mtk
97         struct mtk_pcie *pcie = port->pcie;
98         struct device *dev = pcie->dev;
99         struct platform_device *pdev = to_platform_device(dev);
100 -       int err, irq;
101 +       int err;
102  
103         err = mtk_pcie_init_irq_domain(port, node);
104         if (err) {
105 @@ -630,8 +653,9 @@ static int mtk_pcie_setup_irq(struct mtk
106                 return err;
107         }
108  
109 -       irq = platform_get_irq(pdev, port->slot);
110 -       irq_set_chained_handler_and_data(irq, mtk_pcie_intr_handler, port);
111 +       port->irq = platform_get_irq(pdev, port->slot);
112 +       irq_set_chained_handler_and_data(port->irq,
113 +                                        mtk_pcie_intr_handler, port);
114  
115         return 0;
116  }
117 @@ -904,49 +928,29 @@ static int mtk_pcie_parse_port(struct mt
118  
119         /* sys_ck might be divided into the following parts in some chips */
120         snprintf(name, sizeof(name), "ahb_ck%d", slot);
121 -       port->ahb_ck = devm_clk_get(dev, name);
122 -       if (IS_ERR(port->ahb_ck)) {
123 -               if (PTR_ERR(port->ahb_ck) == -EPROBE_DEFER)
124 -                       return -EPROBE_DEFER;
125 -
126 -               port->ahb_ck = NULL;
127 -       }
128 +       port->ahb_ck = devm_clk_get_optional(dev, name);
129 +       if (IS_ERR(port->ahb_ck))
130 +               return PTR_ERR(port->ahb_ck);
131  
132         snprintf(name, sizeof(name), "axi_ck%d", slot);
133 -       port->axi_ck = devm_clk_get(dev, name);
134 -       if (IS_ERR(port->axi_ck)) {
135 -               if (PTR_ERR(port->axi_ck) == -EPROBE_DEFER)
136 -                       return -EPROBE_DEFER;
137 -
138 -               port->axi_ck = NULL;
139 -       }
140 +       port->axi_ck = devm_clk_get_optional(dev, name);
141 +       if (IS_ERR(port->axi_ck))
142 +               return PTR_ERR(port->axi_ck);
143  
144         snprintf(name, sizeof(name), "aux_ck%d", slot);
145 -       port->aux_ck = devm_clk_get(dev, name);
146 -       if (IS_ERR(port->aux_ck)) {
147 -               if (PTR_ERR(port->aux_ck) == -EPROBE_DEFER)
148 -                       return -EPROBE_DEFER;
149 -
150 -               port->aux_ck = NULL;
151 -       }
152 +       port->aux_ck = devm_clk_get_optional(dev, name);
153 +       if (IS_ERR(port->aux_ck))
154 +               return PTR_ERR(port->aux_ck);
155  
156         snprintf(name, sizeof(name), "obff_ck%d", slot);
157 -       port->obff_ck = devm_clk_get(dev, name);
158 -       if (IS_ERR(port->obff_ck)) {
159 -               if (PTR_ERR(port->obff_ck) == -EPROBE_DEFER)
160 -                       return -EPROBE_DEFER;
161 -
162 -               port->obff_ck = NULL;
163 -       }
164 +       port->obff_ck = devm_clk_get_optional(dev, name);
165 +       if (IS_ERR(port->obff_ck))
166 +               return PTR_ERR(port->obff_ck);
167  
168         snprintf(name, sizeof(name), "pipe_ck%d", slot);
169 -       port->pipe_ck = devm_clk_get(dev, name);
170 -       if (IS_ERR(port->pipe_ck)) {
171 -               if (PTR_ERR(port->pipe_ck) == -EPROBE_DEFER)
172 -                       return -EPROBE_DEFER;
173 -
174 -               port->pipe_ck = NULL;
175 -       }
176 +       port->pipe_ck = devm_clk_get_optional(dev, name);
177 +       if (IS_ERR(port->pipe_ck))
178 +               return PTR_ERR(port->pipe_ck);
179  
180         snprintf(name, sizeof(name), "pcie-rst%d", slot);
181         port->reset = devm_reset_control_get_optional_exclusive(dev, name);
182 @@ -999,10 +1003,8 @@ static int mtk_pcie_subsys_powerup(struc
183                 pcie->free_ck = NULL;
184         }
185  
186 -       if (dev->pm_domain) {
187 -               pm_runtime_enable(dev);
188 -               pm_runtime_get_sync(dev);
189 -       }
190 +       pm_runtime_enable(dev);
191 +       pm_runtime_get_sync(dev);
192  
193         /* enable top level clock */
194         err = clk_prepare_enable(pcie->free_ck);
195 @@ -1014,10 +1016,8 @@ static int mtk_pcie_subsys_powerup(struc
196         return 0;
197  
198  err_free_ck:
199 -       if (dev->pm_domain) {
200 -               pm_runtime_put_sync(dev);
201 -               pm_runtime_disable(dev);
202 -       }
203 +       pm_runtime_put_sync(dev);
204 +       pm_runtime_disable(dev);
205  
206         return err;
207  }
208 @@ -1122,36 +1122,6 @@ static int mtk_pcie_request_resources(st
209                 return err;
210  
211         err = devm_pci_remap_iospace(dev, &pcie->pio, pcie->io.start);
212 -       if (err)
213 -               return err;
214 -
215 -       return 0;
216 -}
217 -
218 -static int mtk_pcie_register_host(struct pci_host_bridge *host)
219 -{
220 -       struct mtk_pcie *pcie = pci_host_bridge_priv(host);
221 -       struct pci_bus *child;
222 -       int err;
223 -
224 -       host->busnr = pcie->busn.start;
225 -       host->dev.parent = pcie->dev;
226 -       host->ops = pcie->soc->ops;
227 -       host->map_irq = of_irq_parse_and_map_pci;
228 -       host->swizzle_irq = pci_common_swizzle;
229 -       host->sysdata = pcie;
230 -
231 -       err = pci_scan_root_bus_bridge(host);
232 -       if (err < 0)
233 -               return err;
234 -
235 -       pci_bus_size_bridges(host->bus);
236 -       pci_bus_assign_resources(host->bus);
237 -
238 -       list_for_each_entry(child, &host->bus->children, node)
239 -               pcie_bus_configure_settings(child);
240 -
241 -       pci_bus_add_devices(host->bus);
242  
243         return 0;
244  }
245 @@ -1182,7 +1152,14 @@ static int mtk_pcie_probe(struct platfor
246         if (err)
247                 goto put_resources;
248  
249 -       err = mtk_pcie_register_host(host);
250 +       host->busnr = pcie->busn.start;
251 +       host->dev.parent = pcie->dev;
252 +       host->ops = pcie->soc->ops;
253 +       host->map_irq = of_irq_parse_and_map_pci;
254 +       host->swizzle_irq = pci_common_swizzle;
255 +       host->sysdata = pcie;
256 +
257 +       err = pci_host_probe(host);
258         if (err)
259                 goto put_resources;
260  
261 @@ -1195,6 +1172,80 @@ put_resources:
262         return err;
263  }
264  
265 +
266 +static void mtk_pcie_free_resources(struct mtk_pcie *pcie)
267 +{
268 +       struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
269 +       struct list_head *windows = &host->windows;
270 +
271 +       pci_free_resource_list(windows);
272 +}
273 +
274 +static int mtk_pcie_remove(struct platform_device *pdev)
275 +{
276 +       struct mtk_pcie *pcie = platform_get_drvdata(pdev);
277 +       struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
278 +
279 +       pci_stop_root_bus(host->bus);
280 +       pci_remove_root_bus(host->bus);
281 +       mtk_pcie_free_resources(pcie);
282 +
283 +       mtk_pcie_irq_teardown(pcie);
284 +
285 +       mtk_pcie_put_resources(pcie);
286 +
287 +       return 0;
288 +}
289 +
290 +static int __maybe_unused mtk_pcie_suspend_noirq(struct device *dev)
291 +{
292 +       struct mtk_pcie *pcie = dev_get_drvdata(dev);
293 +       struct mtk_pcie_port *port;
294 +
295 +       if (list_empty(&pcie->ports))
296 +               return 0;
297 +
298 +       list_for_each_entry(port, &pcie->ports, list) {
299 +               clk_disable_unprepare(port->pipe_ck);
300 +               clk_disable_unprepare(port->obff_ck);
301 +               clk_disable_unprepare(port->axi_ck);
302 +               clk_disable_unprepare(port->aux_ck);
303 +               clk_disable_unprepare(port->ahb_ck);
304 +               clk_disable_unprepare(port->sys_ck);
305 +               phy_power_off(port->phy);
306 +               phy_exit(port->phy);
307 +       }
308 +
309 +       clk_disable_unprepare(pcie->free_ck);
310 +
311 +       return 0;
312 +}
313 +
314 +static int __maybe_unused mtk_pcie_resume_noirq(struct device *dev)
315 +{
316 +       struct mtk_pcie *pcie = dev_get_drvdata(dev);
317 +       struct mtk_pcie_port *port, *tmp;
318 +
319 +       if (list_empty(&pcie->ports))
320 +               return 0;
321 +
322 +       clk_prepare_enable(pcie->free_ck);
323 +
324 +       list_for_each_entry_safe(port, tmp, &pcie->ports, list)
325 +               mtk_pcie_enable_port(port);
326 +
327 +       /* In case of EP was removed while system suspend. */
328 +       if (list_empty(&pcie->ports))
329 +               clk_disable_unprepare(pcie->free_ck);
330 +
331 +       return 0;
332 +}
333 +
334 +static const struct dev_pm_ops mtk_pcie_pm_ops = {
335 +       SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq,
336 +                                     mtk_pcie_resume_noirq)
337 +};
338 +
339  static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
340         .ops = &mtk_pcie_ops,
341         .startup = mtk_pcie_startup_port,
342 @@ -1223,10 +1274,13 @@ static const struct of_device_id mtk_pci
343  
344  static struct platform_driver mtk_pcie_driver = {
345         .probe = mtk_pcie_probe,
346 +       .remove = mtk_pcie_remove,
347         .driver = {
348                 .name = "mtk-pcie",
349                 .of_match_table = mtk_pcie_ids,
350                 .suppress_bind_attrs = true,
351 +               .pm = &mtk_pcie_pm_ops,
352         },
353  };
354 -builtin_platform_driver(mtk_pcie_driver);
355 +module_platform_driver(mtk_pcie_driver);
356 +MODULE_LICENSE("GPL v2");
357 --- a/include/linux/clk.h
358 +++ b/include/linux/clk.h
359 @@ -349,6 +349,17 @@ int __must_check devm_clk_bulk_get(struc
360  struct clk *devm_clk_get(struct device *dev, const char *id);
361  
362  /**
363 + * devm_clk_get_optional - lookup and obtain a managed reference to an optional
364 + *                         clock producer.
365 + * @dev: device for clock "consumer"
366 + * @id: clock consumer ID
367 + *
368 + * Behaves the same as devm_clk_get() except where there is no clock producer.
369 + * In this case, instead of returning -ENOENT, the function returns NULL.
370 + */
371 +struct clk *devm_clk_get_optional(struct device *dev, const char *id);
372 +
373 +/**
374   * devm_get_clk_from_child - lookup and obtain a managed reference to a
375   *                          clock producer from child node.
376   * @dev: device for clock "consumer"