Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2017 NXP |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 8 | #include <malloc.h> |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 9 | #include <power-domain-uclass.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 11 | #include <asm/io.h> |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 12 | #include <asm/mach-imx/sys_proto.h> |
| 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/device.h> |
| 15 | #include <imx_sip.h> |
Peng Fan | 4b8f22d | 2020-05-11 15:16:37 +0800 | [diff] [blame] | 16 | #include <linux/arm-smccc.h> |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Marek Vasut | 19842b6 | 2022-04-13 00:42:50 +0200 | [diff] [blame^] | 20 | struct imx8m_power_domain_plat { |
| 21 | int resource_id; |
| 22 | int has_pd; |
| 23 | struct power_domain pd; |
| 24 | }; |
| 25 | |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 26 | static int imx8m_power_domain_on(struct power_domain *power_domain) |
| 27 | { |
| 28 | struct udevice *dev = power_domain->dev; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 29 | struct imx8m_power_domain_plat *pdata; |
Peng Fan | 4b8f22d | 2020-05-11 15:16:37 +0800 | [diff] [blame] | 30 | |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 31 | pdata = dev_get_plat(dev); |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 32 | |
| 33 | if (pdata->resource_id < 0) |
| 34 | return -EINVAL; |
| 35 | |
| 36 | if (pdata->has_pd) |
| 37 | power_domain_on(&pdata->pd); |
| 38 | |
Peng Fan | 4b8f22d | 2020-05-11 15:16:37 +0800 | [diff] [blame] | 39 | arm_smccc_smc(IMX_SIP_GPC, IMX_SIP_GPC_PM_DOMAIN, |
| 40 | pdata->resource_id, 1, 0, 0, 0, 0, NULL); |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 41 | |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static int imx8m_power_domain_off(struct power_domain *power_domain) |
| 46 | { |
| 47 | struct udevice *dev = power_domain->dev; |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 48 | struct imx8m_power_domain_plat *pdata; |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 49 | pdata = dev_get_plat(dev); |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 50 | |
| 51 | if (pdata->resource_id < 0) |
| 52 | return -EINVAL; |
| 53 | |
Peng Fan | 4b8f22d | 2020-05-11 15:16:37 +0800 | [diff] [blame] | 54 | arm_smccc_smc(IMX_SIP_GPC, IMX_SIP_GPC_PM_DOMAIN, |
| 55 | pdata->resource_id, 0, 0, 0, 0, 0, NULL); |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 56 | |
| 57 | if (pdata->has_pd) |
| 58 | power_domain_off(&pdata->pd); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static int imx8m_power_domain_of_xlate(struct power_domain *power_domain, |
| 64 | struct ofnode_phandle_args *args) |
| 65 | { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | static int imx8m_power_domain_bind(struct udevice *dev) |
| 70 | { |
| 71 | int offset; |
| 72 | const char *name; |
| 73 | int ret = 0; |
| 74 | |
| 75 | offset = dev_of_offset(dev); |
| 76 | for (offset = fdt_first_subnode(gd->fdt_blob, offset); offset > 0; |
| 77 | offset = fdt_next_subnode(gd->fdt_blob, offset)) { |
| 78 | /* Bind the subnode to this driver */ |
| 79 | name = fdt_get_name(gd->fdt_blob, offset, NULL); |
| 80 | |
Marek Vasut | 8741d92 | 2022-04-13 00:42:49 +0200 | [diff] [blame] | 81 | /* Descend into 'pgc' subnode */ |
| 82 | if (!strstr(name, "power-domain")) { |
| 83 | offset = fdt_first_subnode(gd->fdt_blob, offset); |
| 84 | name = fdt_get_name(gd->fdt_blob, offset, NULL); |
| 85 | } |
| 86 | |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 87 | ret = device_bind_with_driver_data(dev, dev->driver, name, |
| 88 | dev->driver_data, |
| 89 | offset_to_ofnode(offset), |
| 90 | NULL); |
| 91 | |
| 92 | if (ret == -ENODEV) |
| 93 | printf("Driver '%s' refuses to bind\n", |
| 94 | dev->driver->name); |
| 95 | |
| 96 | if (ret) |
| 97 | printf("Error binding driver '%s': %d\n", |
| 98 | dev->driver->name, ret); |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 104 | static int imx8m_power_domain_of_to_plat(struct udevice *dev) |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 105 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 106 | struct imx8m_power_domain_plat *pdata = dev_get_plat(dev); |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 107 | |
| 108 | pdata->resource_id = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), |
| 109 | "reg", -1); |
| 110 | |
| 111 | if (!power_domain_get(dev, &pdata->pd)) |
| 112 | pdata->has_pd = 1; |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static const struct udevice_id imx8m_power_domain_ids[] = { |
| 118 | { .compatible = "fsl,imx8mq-gpc" }, |
Marek Vasut | da16034 | 2021-04-02 13:32:40 +0200 | [diff] [blame] | 119 | { .compatible = "fsl,imx8mm-gpc" }, |
Marek Vasut | f174a0d | 2021-04-10 00:19:27 +0200 | [diff] [blame] | 120 | { .compatible = "fsl,imx8mn-gpc" }, |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 121 | { } |
| 122 | }; |
| 123 | |
| 124 | struct power_domain_ops imx8m_power_domain_ops = { |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 125 | .on = imx8m_power_domain_on, |
| 126 | .off = imx8m_power_domain_off, |
| 127 | .of_xlate = imx8m_power_domain_of_xlate, |
| 128 | }; |
| 129 | |
| 130 | U_BOOT_DRIVER(imx8m_power_domain) = { |
| 131 | .name = "imx8m_power_domain", |
| 132 | .id = UCLASS_POWER_DOMAIN, |
| 133 | .of_match = imx8m_power_domain_ids, |
| 134 | .bind = imx8m_power_domain_bind, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 135 | .of_to_plat = imx8m_power_domain_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 136 | .plat_auto = sizeof(struct imx8m_power_domain_plat), |
Patrick Wildt | d08a194 | 2019-10-03 15:51:50 +0200 | [diff] [blame] | 137 | .ops = &imx8m_power_domain_ops, |
| 138 | }; |