Marek Vasut | dd96fba | 2022-04-13 00:42:54 +0200 | [diff] [blame] | 1 | // 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 | |
| 20 | struct 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 | |
| 29 | static 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 | |
| 64 | err_clk: |
| 65 | power_domain_off(domain); |
| 66 | err_pd: |
| 67 | power_domain_off(&priv->pd_bus); |
| 68 | return ret; |
| 69 | } |
| 70 | |
| 71 | static 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 | |
| 93 | static 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 | |
| 101 | static 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) |
| 114 | goto err_pd_bus; |
| 115 | |
| 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 | |
| 130 | err_pd_usb_phy2: |
| 131 | power_domain_free(&priv->pd_usb_phy1); |
| 132 | err_pd_usb_phy1: |
| 133 | power_domain_free(&priv->pd_usb); |
| 134 | err_pd_usb: |
| 135 | power_domain_free(&priv->pd_bus); |
| 136 | err_pd_bus: |
| 137 | clk_free(&priv->clk_usb); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static const struct udevice_id imx8mp_hsiomix_ids[] = { |
| 142 | { .compatible = "fsl,imx8mp-hsio-blk-ctrl" }, |
| 143 | { } |
| 144 | }; |
| 145 | |
| 146 | struct power_domain_ops imx8mp_hsiomix_ops = { |
| 147 | .on = imx8mp_hsiomix_on, |
| 148 | .off = imx8mp_hsiomix_off, |
| 149 | .of_xlate = imx8mp_hsiomix_of_xlate, |
| 150 | }; |
| 151 | |
| 152 | U_BOOT_DRIVER(imx8mp_hsiomix) = { |
| 153 | .name = "imx8mp_hsiomix", |
| 154 | .id = UCLASS_POWER_DOMAIN, |
| 155 | .of_match = imx8mp_hsiomix_ids, |
| 156 | .probe = imx8mp_hsiomix_probe, |
| 157 | .priv_auto = sizeof(struct imx8mp_hsiomix_priv), |
| 158 | .ops = &imx8mp_hsiomix_ops, |
| 159 | }; |