Merge tag 'rtc-5.17' of git://git.kernel.org/pub/scm/linux/kernel/git/abelloni/linux
[linux-2.6-microblaze.git] / drivers / pci / controller / pcie-mt7621.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * BRIEF MODULE DESCRIPTION
4  *     PCI init for Ralink RT2880 solution
5  *
6  * Copyright 2007 Ralink Inc. (bruce_chang@ralinktech.com.tw)
7  *
8  * May 2007 Bruce Chang
9  * Initial Release
10  *
11  * May 2009 Bruce Chang
12  * support RT2880/RT3883 PCIe
13  *
14  * May 2011 Bruce Chang
15  * support RT6855/MT7620 PCIe
16  */
17
18 #include <linux/bitops.h>
19 #include <linux/clk.h>
20 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_pci.h>
26 #include <linux/of_platform.h>
27 #include <linux/pci.h>
28 #include <linux/phy/phy.h>
29 #include <linux/platform_device.h>
30 #include <linux/reset.h>
31 #include <linux/sys_soc.h>
32
33 /* MediaTek-specific configuration registers */
34 #define PCIE_FTS_NUM                    0x70c
35 #define PCIE_FTS_NUM_MASK               GENMASK(15, 8)
36 #define PCIE_FTS_NUM_L0(x)              (((x) & 0xff) << 8)
37
38 /* Host-PCI bridge registers */
39 #define RALINK_PCI_PCICFG_ADDR          0x0000
40 #define RALINK_PCI_PCIMSK_ADDR          0x000c
41 #define RALINK_PCI_CONFIG_ADDR          0x0020
42 #define RALINK_PCI_CONFIG_DATA          0x0024
43 #define RALINK_PCI_MEMBASE              0x0028
44 #define RALINK_PCI_IOBASE               0x002c
45
46 /* PCIe RC control registers */
47 #define RALINK_PCI_ID                   0x0030
48 #define RALINK_PCI_CLASS                0x0034
49 #define RALINK_PCI_SUBID                0x0038
50 #define RALINK_PCI_STATUS               0x0050
51
52 /* Some definition values */
53 #define PCIE_REVISION_ID                BIT(0)
54 #define PCIE_CLASS_CODE                 (0x60400 << 8)
55 #define PCIE_BAR_MAP_MAX                GENMASK(30, 16)
56 #define PCIE_BAR_ENABLE                 BIT(0)
57 #define PCIE_PORT_INT_EN(x)             BIT(20 + (x))
58 #define PCIE_PORT_LINKUP                BIT(0)
59 #define PCIE_PORT_CNT                   3
60
61 #define PERST_DELAY_MS                  100
62
63 /**
64  * struct mt7621_pcie_port - PCIe port information
65  * @base: I/O mapped register base
66  * @list: port list
67  * @pcie: pointer to PCIe host info
68  * @clk: pointer to the port clock gate
69  * @phy: pointer to PHY control block
70  * @pcie_rst: pointer to port reset control
71  * @gpio_rst: gpio reset
72  * @slot: port slot
73  * @enabled: indicates if port is enabled
74  */
75 struct mt7621_pcie_port {
76         void __iomem *base;
77         struct list_head list;
78         struct mt7621_pcie *pcie;
79         struct clk *clk;
80         struct phy *phy;
81         struct reset_control *pcie_rst;
82         struct gpio_desc *gpio_rst;
83         u32 slot;
84         bool enabled;
85 };
86
87 /**
88  * struct mt7621_pcie - PCIe host information
89  * @base: IO Mapped Register Base
90  * @dev: Pointer to PCIe device
91  * @ports: pointer to PCIe port information
92  * @resets_inverted: depends on chip revision
93  * reset lines are inverted.
94  */
95 struct mt7621_pcie {
96         struct device *dev;
97         void __iomem *base;
98         struct list_head ports;
99         bool resets_inverted;
100 };
101
102 static inline u32 pcie_read(struct mt7621_pcie *pcie, u32 reg)
103 {
104         return readl_relaxed(pcie->base + reg);
105 }
106
107 static inline void pcie_write(struct mt7621_pcie *pcie, u32 val, u32 reg)
108 {
109         writel_relaxed(val, pcie->base + reg);
110 }
111
112 static inline void pcie_rmw(struct mt7621_pcie *pcie, u32 reg, u32 clr, u32 set)
113 {
114         u32 val = readl_relaxed(pcie->base + reg);
115
116         val &= ~clr;
117         val |= set;
118         writel_relaxed(val, pcie->base + reg);
119 }
120
121 static inline u32 pcie_port_read(struct mt7621_pcie_port *port, u32 reg)
122 {
123         return readl_relaxed(port->base + reg);
124 }
125
126 static inline void pcie_port_write(struct mt7621_pcie_port *port,
127                                    u32 val, u32 reg)
128 {
129         writel_relaxed(val, port->base + reg);
130 }
131
132 static inline u32 mt7621_pcie_get_cfgaddr(unsigned int bus, unsigned int slot,
133                                          unsigned int func, unsigned int where)
134 {
135         return (((where & 0xf00) >> 8) << 24) | (bus << 16) | (slot << 11) |
136                 (func << 8) | (where & 0xfc) | 0x80000000;
137 }
138
139 static void __iomem *mt7621_pcie_map_bus(struct pci_bus *bus,
140                                          unsigned int devfn, int where)
141 {
142         struct mt7621_pcie *pcie = bus->sysdata;
143         u32 address = mt7621_pcie_get_cfgaddr(bus->number, PCI_SLOT(devfn),
144                                              PCI_FUNC(devfn), where);
145
146         writel_relaxed(address, pcie->base + RALINK_PCI_CONFIG_ADDR);
147
148         return pcie->base + RALINK_PCI_CONFIG_DATA + (where & 3);
149 }
150
151 static struct pci_ops mt7621_pcie_ops = {
152         .map_bus        = mt7621_pcie_map_bus,
153         .read           = pci_generic_config_read,
154         .write          = pci_generic_config_write,
155 };
156
157 static u32 read_config(struct mt7621_pcie *pcie, unsigned int dev, u32 reg)
158 {
159         u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
160
161         pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
162         return pcie_read(pcie, RALINK_PCI_CONFIG_DATA);
163 }
164
165 static void write_config(struct mt7621_pcie *pcie, unsigned int dev,
166                          u32 reg, u32 val)
167 {
168         u32 address = mt7621_pcie_get_cfgaddr(0, dev, 0, reg);
169
170         pcie_write(pcie, address, RALINK_PCI_CONFIG_ADDR);
171         pcie_write(pcie, val, RALINK_PCI_CONFIG_DATA);
172 }
173
174 static inline void mt7621_rst_gpio_pcie_assert(struct mt7621_pcie_port *port)
175 {
176         if (port->gpio_rst)
177                 gpiod_set_value(port->gpio_rst, 1);
178 }
179
180 static inline void mt7621_rst_gpio_pcie_deassert(struct mt7621_pcie_port *port)
181 {
182         if (port->gpio_rst)
183                 gpiod_set_value(port->gpio_rst, 0);
184 }
185
186 static inline bool mt7621_pcie_port_is_linkup(struct mt7621_pcie_port *port)
187 {
188         return (pcie_port_read(port, RALINK_PCI_STATUS) & PCIE_PORT_LINKUP) != 0;
189 }
190
191 static inline void mt7621_control_assert(struct mt7621_pcie_port *port)
192 {
193         struct mt7621_pcie *pcie = port->pcie;
194
195         if (pcie->resets_inverted)
196                 reset_control_assert(port->pcie_rst);
197         else
198                 reset_control_deassert(port->pcie_rst);
199 }
200
201 static inline void mt7621_control_deassert(struct mt7621_pcie_port *port)
202 {
203         struct mt7621_pcie *pcie = port->pcie;
204
205         if (pcie->resets_inverted)
206                 reset_control_deassert(port->pcie_rst);
207         else
208                 reset_control_assert(port->pcie_rst);
209 }
210
211 static int mt7621_pcie_parse_port(struct mt7621_pcie *pcie,
212                                   struct device_node *node,
213                                   int slot)
214 {
215         struct mt7621_pcie_port *port;
216         struct device *dev = pcie->dev;
217         struct platform_device *pdev = to_platform_device(dev);
218         char name[10];
219         int err;
220
221         port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
222         if (!port)
223                 return -ENOMEM;
224
225         port->base = devm_platform_ioremap_resource(pdev, slot + 1);
226         if (IS_ERR(port->base))
227                 return PTR_ERR(port->base);
228
229         port->clk = devm_get_clk_from_child(dev, node, NULL);
230         if (IS_ERR(port->clk)) {
231                 dev_err(dev, "failed to get pcie%d clock\n", slot);
232                 return PTR_ERR(port->clk);
233         }
234
235         port->pcie_rst = of_reset_control_get_exclusive(node, NULL);
236         if (PTR_ERR(port->pcie_rst) == -EPROBE_DEFER) {
237                 dev_err(dev, "failed to get pcie%d reset control\n", slot);
238                 return PTR_ERR(port->pcie_rst);
239         }
240
241         snprintf(name, sizeof(name), "pcie-phy%d", slot);
242         port->phy = devm_of_phy_get(dev, node, name);
243         if (IS_ERR(port->phy)) {
244                 dev_err(dev, "failed to get pcie-phy%d\n", slot);
245                 err = PTR_ERR(port->phy);
246                 goto remove_reset;
247         }
248
249         port->gpio_rst = devm_gpiod_get_index_optional(dev, "reset", slot,
250                                                        GPIOD_OUT_LOW);
251         if (IS_ERR(port->gpio_rst)) {
252                 dev_err(dev, "failed to get GPIO for PCIe%d\n", slot);
253                 err = PTR_ERR(port->gpio_rst);
254                 goto remove_reset;
255         }
256
257         port->slot = slot;
258         port->pcie = pcie;
259
260         INIT_LIST_HEAD(&port->list);
261         list_add_tail(&port->list, &pcie->ports);
262
263         return 0;
264
265 remove_reset:
266         reset_control_put(port->pcie_rst);
267         return err;
268 }
269
270 static int mt7621_pcie_parse_dt(struct mt7621_pcie *pcie)
271 {
272         struct device *dev = pcie->dev;
273         struct platform_device *pdev = to_platform_device(dev);
274         struct device_node *node = dev->of_node, *child;
275         int err;
276
277         pcie->base = devm_platform_ioremap_resource(pdev, 0);
278         if (IS_ERR(pcie->base))
279                 return PTR_ERR(pcie->base);
280
281         for_each_available_child_of_node(node, child) {
282                 int slot;
283
284                 err = of_pci_get_devfn(child);
285                 if (err < 0) {
286                         of_node_put(child);
287                         dev_err(dev, "failed to parse devfn: %d\n", err);
288                         return err;
289                 }
290
291                 slot = PCI_SLOT(err);
292
293                 err = mt7621_pcie_parse_port(pcie, child, slot);
294                 if (err) {
295                         of_node_put(child);
296                         return err;
297                 }
298         }
299
300         return 0;
301 }
302
303 static int mt7621_pcie_init_port(struct mt7621_pcie_port *port)
304 {
305         struct mt7621_pcie *pcie = port->pcie;
306         struct device *dev = pcie->dev;
307         u32 slot = port->slot;
308         int err;
309
310         err = phy_init(port->phy);
311         if (err) {
312                 dev_err(dev, "failed to initialize port%d phy\n", slot);
313                 return err;
314         }
315
316         err = phy_power_on(port->phy);
317         if (err) {
318                 dev_err(dev, "failed to power on port%d phy\n", slot);
319                 phy_exit(port->phy);
320                 return err;
321         }
322
323         port->enabled = true;
324
325         return 0;
326 }
327
328 static void mt7621_pcie_reset_assert(struct mt7621_pcie *pcie)
329 {
330         struct mt7621_pcie_port *port;
331
332         list_for_each_entry(port, &pcie->ports, list) {
333                 /* PCIe RC reset assert */
334                 mt7621_control_assert(port);
335
336                 /* PCIe EP reset assert */
337                 mt7621_rst_gpio_pcie_assert(port);
338         }
339
340         msleep(PERST_DELAY_MS);
341 }
342
343 static void mt7621_pcie_reset_rc_deassert(struct mt7621_pcie *pcie)
344 {
345         struct mt7621_pcie_port *port;
346
347         list_for_each_entry(port, &pcie->ports, list)
348                 mt7621_control_deassert(port);
349 }
350
351 static void mt7621_pcie_reset_ep_deassert(struct mt7621_pcie *pcie)
352 {
353         struct mt7621_pcie_port *port;
354
355         list_for_each_entry(port, &pcie->ports, list)
356                 mt7621_rst_gpio_pcie_deassert(port);
357
358         msleep(PERST_DELAY_MS);
359 }
360
361 static int mt7621_pcie_init_ports(struct mt7621_pcie *pcie)
362 {
363         struct device *dev = pcie->dev;
364         struct mt7621_pcie_port *port, *tmp;
365         u8 num_disabled = 0;
366         int err;
367
368         mt7621_pcie_reset_assert(pcie);
369         mt7621_pcie_reset_rc_deassert(pcie);
370
371         list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
372                 u32 slot = port->slot;
373
374                 if (slot == 1) {
375                         port->enabled = true;
376                         continue;
377                 }
378
379                 err = mt7621_pcie_init_port(port);
380                 if (err) {
381                         dev_err(dev, "initializing port %d failed\n", slot);
382                         list_del(&port->list);
383                 }
384         }
385
386         mt7621_pcie_reset_ep_deassert(pcie);
387
388         tmp = NULL;
389         list_for_each_entry(port, &pcie->ports, list) {
390                 u32 slot = port->slot;
391
392                 if (!mt7621_pcie_port_is_linkup(port)) {
393                         dev_err(dev, "pcie%d no card, disable it (RST & CLK)\n",
394                                 slot);
395                         mt7621_control_assert(port);
396                         port->enabled = false;
397                         num_disabled++;
398
399                         if (slot == 0) {
400                                 tmp = port;
401                                 continue;
402                         }
403
404                         if (slot == 1 && tmp && !tmp->enabled)
405                                 phy_power_off(tmp->phy);
406                 }
407         }
408
409         return (num_disabled != PCIE_PORT_CNT) ? 0 : -ENODEV;
410 }
411
412 static void mt7621_pcie_enable_port(struct mt7621_pcie_port *port)
413 {
414         struct mt7621_pcie *pcie = port->pcie;
415         u32 slot = port->slot;
416         u32 val;
417
418         /* enable pcie interrupt */
419         val = pcie_read(pcie, RALINK_PCI_PCIMSK_ADDR);
420         val |= PCIE_PORT_INT_EN(slot);
421         pcie_write(pcie, val, RALINK_PCI_PCIMSK_ADDR);
422
423         /* map 2G DDR region */
424         pcie_port_write(port, PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
425                         PCI_BASE_ADDRESS_0);
426
427         /* configure class code and revision ID */
428         pcie_port_write(port, PCIE_CLASS_CODE | PCIE_REVISION_ID,
429                         RALINK_PCI_CLASS);
430
431         /* configure RC FTS number to 250 when it leaves L0s */
432         val = read_config(pcie, slot, PCIE_FTS_NUM);
433         val &= ~PCIE_FTS_NUM_MASK;
434         val |= PCIE_FTS_NUM_L0(0x50);
435         write_config(pcie, slot, PCIE_FTS_NUM, val);
436 }
437
438 static int mt7621_pcie_enable_ports(struct pci_host_bridge *host)
439 {
440         struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
441         struct device *dev = pcie->dev;
442         struct mt7621_pcie_port *port;
443         struct resource_entry *entry;
444         int err;
445
446         entry = resource_list_first_type(&host->windows, IORESOURCE_IO);
447         if (!entry) {
448                 dev_err(dev, "cannot get io resource\n");
449                 return -EINVAL;
450         }
451
452         /* Setup MEMWIN and IOWIN */
453         pcie_write(pcie, 0xffffffff, RALINK_PCI_MEMBASE);
454         pcie_write(pcie, entry->res->start - entry->offset, RALINK_PCI_IOBASE);
455
456         list_for_each_entry(port, &pcie->ports, list) {
457                 if (port->enabled) {
458                         err = clk_prepare_enable(port->clk);
459                         if (err) {
460                                 dev_err(dev, "enabling clk pcie%d\n",
461                                         port->slot);
462                                 return err;
463                         }
464
465                         mt7621_pcie_enable_port(port);
466                         dev_info(dev, "PCIE%d enabled\n", port->slot);
467                 }
468         }
469
470         return 0;
471 }
472
473 static int mt7621_pcie_register_host(struct pci_host_bridge *host)
474 {
475         struct mt7621_pcie *pcie = pci_host_bridge_priv(host);
476
477         host->ops = &mt7621_pcie_ops;
478         host->sysdata = pcie;
479         return pci_host_probe(host);
480 }
481
482 static const struct soc_device_attribute mt7621_pcie_quirks_match[] = {
483         { .soc_id = "mt7621", .revision = "E2" }
484 };
485
486 static int mt7621_pcie_probe(struct platform_device *pdev)
487 {
488         struct device *dev = &pdev->dev;
489         const struct soc_device_attribute *attr;
490         struct mt7621_pcie_port *port;
491         struct mt7621_pcie *pcie;
492         struct pci_host_bridge *bridge;
493         int err;
494
495         if (!dev->of_node)
496                 return -ENODEV;
497
498         bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
499         if (!bridge)
500                 return -ENOMEM;
501
502         pcie = pci_host_bridge_priv(bridge);
503         pcie->dev = dev;
504         platform_set_drvdata(pdev, pcie);
505         INIT_LIST_HEAD(&pcie->ports);
506
507         attr = soc_device_match(mt7621_pcie_quirks_match);
508         if (attr)
509                 pcie->resets_inverted = true;
510
511         err = mt7621_pcie_parse_dt(pcie);
512         if (err) {
513                 dev_err(dev, "parsing DT failed\n");
514                 return err;
515         }
516
517         err = mt7621_pcie_init_ports(pcie);
518         if (err) {
519                 dev_err(dev, "nothing connected in virtual bridges\n");
520                 return 0;
521         }
522
523         err = mt7621_pcie_enable_ports(bridge);
524         if (err) {
525                 dev_err(dev, "error enabling pcie ports\n");
526                 goto remove_resets;
527         }
528
529         return mt7621_pcie_register_host(bridge);
530
531 remove_resets:
532         list_for_each_entry(port, &pcie->ports, list)
533                 reset_control_put(port->pcie_rst);
534
535         return err;
536 }
537
538 static int mt7621_pcie_remove(struct platform_device *pdev)
539 {
540         struct mt7621_pcie *pcie = platform_get_drvdata(pdev);
541         struct mt7621_pcie_port *port;
542
543         list_for_each_entry(port, &pcie->ports, list)
544                 reset_control_put(port->pcie_rst);
545
546         return 0;
547 }
548
549 static const struct of_device_id mt7621_pcie_ids[] = {
550         { .compatible = "mediatek,mt7621-pci" },
551         {},
552 };
553 MODULE_DEVICE_TABLE(of, mt7621_pcie_ids);
554
555 static struct platform_driver mt7621_pcie_driver = {
556         .probe = mt7621_pcie_probe,
557         .remove = mt7621_pcie_remove,
558         .driver = {
559                 .name = "mt7621-pci",
560                 .of_match_table = of_match_ptr(mt7621_pcie_ids),
561         },
562 };
563 builtin_platform_driver(mt7621_pcie_driver);
564
565 MODULE_LICENSE("GPL v2");