blob: e2d772c5ec78afead09a0d0f9191f71f00c7a974 [file] [log] [blame]
Marek Vasutdd96fba2022-04-13 00:42:54 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2022 Marek Vasut <marex@denx.de>
4 */
5
6#include <common.h>
7#include <asm/io.h>
8#include <clk.h>
9#include <dm.h>
10#include <dm/device.h>
11#include <dm/device_compat.h>
12#include <power-domain-uclass.h>
13
14#include <dt-bindings/power/imx8mp-power.h>
15
16#define GPR_REG0 0x0
17#define PCIE_CLOCK_MODULE_EN BIT(0)
18#define USB_CLOCK_MODULE_EN BIT(1)
19
20struct imx8mp_hsiomix_priv {
21 void __iomem *base;
22 struct clk clk_usb;
23 struct power_domain pd_bus;
24 struct power_domain pd_usb;
25 struct power_domain pd_usb_phy1;
26 struct power_domain pd_usb_phy2;
27};
28
29static int imx8mp_hsiomix_on(struct power_domain *power_domain)
30{
31 struct udevice *dev = power_domain->dev;
32 struct imx8mp_hsiomix_priv *priv = dev_get_priv(dev);
33 struct power_domain *domain;
34 int ret;
35
36 ret = power_domain_on(&priv->pd_bus);
37 if (ret)
38 return ret;
39
40 if (power_domain->id == IMX8MP_HSIOBLK_PD_USB) {
41 domain = &priv->pd_usb;
42 } else if (power_domain->id == IMX8MP_HSIOBLK_PD_USB_PHY1) {
43 domain = &priv->pd_usb_phy1;
44 } else if (power_domain->id == IMX8MP_HSIOBLK_PD_USB_PHY2) {
45 domain = &priv->pd_usb_phy2;
46 } else {
47 ret = -EINVAL;
48 goto err_pd;
49 }
50
51 ret = power_domain_on(domain);
52 if (ret)
53 goto err_pd;
54
55 ret = clk_enable(&priv->clk_usb);
56 if (ret)
57 goto err_clk;
58
59 if (power_domain->id == IMX8MP_HSIOBLK_PD_USB)
60 setbits_le32(priv->base + GPR_REG0, USB_CLOCK_MODULE_EN);
61
62 return 0;
63
64err_clk:
65 power_domain_off(domain);
66err_pd:
67 power_domain_off(&priv->pd_bus);
68 return ret;
69}
70
71static int imx8mp_hsiomix_off(struct power_domain *power_domain)
72{
73 struct udevice *dev = power_domain->dev;
74 struct imx8mp_hsiomix_priv *priv = dev_get_priv(dev);
75
76 if (power_domain->id == IMX8MP_HSIOBLK_PD_USB)
77 clrbits_le32(priv->base + GPR_REG0, USB_CLOCK_MODULE_EN);
78
79 clk_disable(&priv->clk_usb);
80
81 if (power_domain->id == IMX8MP_HSIOBLK_PD_USB)
82 power_domain_off(&priv->pd_usb);
83 else if (power_domain->id == IMX8MP_HSIOBLK_PD_USB_PHY1)
84 power_domain_off(&priv->pd_usb_phy1);
85 else if (power_domain->id == IMX8MP_HSIOBLK_PD_USB_PHY2)
86 power_domain_off(&priv->pd_usb_phy2);
87
88 power_domain_off(&priv->pd_bus);
89
90 return 0;
91}
92
93static int imx8mp_hsiomix_of_xlate(struct power_domain *power_domain,
94 struct ofnode_phandle_args *args)
95{
96 power_domain->id = args->args[0];
97
98 return 0;
99}
100
101static int imx8mp_hsiomix_probe(struct udevice *dev)
102{
103 struct imx8mp_hsiomix_priv *priv = dev_get_priv(dev);
104 int ret;
105
106 priv->base = dev_read_addr_ptr(dev);
107
108 ret = clk_get_by_name(dev, "usb", &priv->clk_usb);
109 if (ret < 0)
110 return ret;
111
112 ret = power_domain_get_by_name(dev, &priv->pd_bus, "bus");
113 if (ret < 0)
Sean Andersond318eb32023-12-16 14:38:42 -0500114 return ret;
Marek Vasutdd96fba2022-04-13 00:42:54 +0200115
116 ret = power_domain_get_by_name(dev, &priv->pd_usb, "usb");
117 if (ret < 0)
118 goto err_pd_usb;
119
120 ret = power_domain_get_by_name(dev, &priv->pd_usb_phy1, "usb-phy1");
121 if (ret < 0)
122 goto err_pd_usb_phy1;
123
124 ret = power_domain_get_by_name(dev, &priv->pd_usb_phy2, "usb-phy2");
125 if (ret < 0)
126 goto err_pd_usb_phy2;
127
128 return 0;
129
130err_pd_usb_phy2:
131 power_domain_free(&priv->pd_usb_phy1);
132err_pd_usb_phy1:
133 power_domain_free(&priv->pd_usb);
134err_pd_usb:
135 power_domain_free(&priv->pd_bus);
Marek Vasutdd96fba2022-04-13 00:42:54 +0200136 return ret;
137}
138
139static const struct udevice_id imx8mp_hsiomix_ids[] = {
140 { .compatible = "fsl,imx8mp-hsio-blk-ctrl" },
141 { }
142};
143
144struct power_domain_ops imx8mp_hsiomix_ops = {
145 .on = imx8mp_hsiomix_on,
146 .off = imx8mp_hsiomix_off,
147 .of_xlate = imx8mp_hsiomix_of_xlate,
148};
149
150U_BOOT_DRIVER(imx8mp_hsiomix) = {
151 .name = "imx8mp_hsiomix",
152 .id = UCLASS_POWER_DOMAIN,
153 .of_match = imx8mp_hsiomix_ids,
154 .probe = imx8mp_hsiomix_probe,
155 .priv_auto = sizeof(struct imx8mp_hsiomix_priv),
156 .ops = &imx8mp_hsiomix_ops,
157};