Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Sean Anderson | 02a8f6d | 2020-10-04 21:39:43 -0400 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SYSCON |
| 8 | |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 11 | #include <syscon.h> |
| 12 | #include <dm.h> |
| 13 | #include <errno.h> |
| 14 | #include <regmap.h> |
| 15 | #include <dm/device-internal.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 17 | #include <dm/lists.h> |
| 18 | #include <dm/root.h> |
| 19 | #include <linux/err.h> |
| 20 | |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 21 | /* |
| 22 | * Caution: |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 23 | * This API requires the given device has already been bound to the syscon |
| 24 | * driver. For example, |
| 25 | * |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 26 | * compatible = "syscon", "simple-mfd"; |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 27 | * |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 28 | * works, but |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 29 | * |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 30 | * compatible = "simple-mfd", "syscon"; |
Heinrich Schuchardt | 43b05c0 | 2020-08-22 07:16:26 +0200 | [diff] [blame] | 31 | * |
| 32 | * does not. The behavior is different from Linux. |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 33 | */ |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 34 | struct regmap *syscon_get_regmap(struct udevice *dev) |
| 35 | { |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 36 | struct syscon_uc_info *priv; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 37 | |
Simon Glass | ead6497 | 2015-07-06 12:54:38 -0600 | [diff] [blame] | 38 | if (device_get_uclass_id(dev) != UCLASS_SYSCON) |
| 39 | return ERR_PTR(-ENOEXEC); |
| 40 | priv = dev_get_uclass_priv(dev); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 41 | return priv->regmap; |
| 42 | } |
| 43 | |
| 44 | static int syscon_pre_probe(struct udevice *dev) |
| 45 | { |
| 46 | struct syscon_uc_info *priv = dev_get_uclass_priv(dev); |
| 47 | |
Simon Glass | 7e75aba | 2019-02-16 20:24:38 -0700 | [diff] [blame] | 48 | /* Special case for PCI devices, which don't have a regmap */ |
| 49 | if (device_get_uclass_id(dev->parent) == UCLASS_PCI) |
| 50 | return 0; |
| 51 | |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 52 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 53 | /* |
| 54 | * With OF_PLATDATA we really have no way of knowing the format of |
| 55 | * the device-specific platform data. So we assume that it starts with |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 56 | * a 'reg' member that holds a single address and size. Drivers |
| 57 | * using OF_PLATDATA will need to ensure that this is true. In case of |
| 58 | * odd reg structures other then the syscon_base_plat structure |
| 59 | * below the regmap must be defined in the individual syscon driver. |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 60 | */ |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 61 | struct syscon_base_plat { |
| 62 | phys_addr_t reg[2]; |
| 63 | }; |
| 64 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 65 | struct syscon_base_plat *plat = dev_get_plat(dev); |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 66 | |
Johan Jonker | 2e304a2 | 2023-03-13 01:30:46 +0100 | [diff] [blame] | 67 | /* |
| 68 | * Return if the regmap is already defined in the individual |
| 69 | * syscon driver. |
| 70 | */ |
| 71 | if (priv->regmap) |
| 72 | return 0; |
| 73 | |
| 74 | return regmap_init_mem_plat(dev, plat->reg, sizeof(plat->reg[0]), |
| 75 | ARRAY_SIZE(plat->reg) / 2, &priv->regmap); |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 76 | #else |
Masahiro Yamada | e4873e3 | 2018-04-19 12:14:03 +0900 | [diff] [blame] | 77 | return regmap_init_mem(dev_ofnode(dev), &priv->regmap); |
Simon Glass | 73dd069 | 2016-07-04 11:58:00 -0600 | [diff] [blame] | 78 | #endif |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 79 | } |
| 80 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 81 | static int syscon_probe_by_ofnode(ofnode node, struct udevice **devp) |
| 82 | { |
| 83 | struct udevice *dev, *parent; |
| 84 | int ret; |
| 85 | |
| 86 | /* found node with "syscon" compatible, not bounded to SYSCON UCLASS */ |
| 87 | if (!ofnode_device_is_compatible(node, "syscon")) { |
Sean Anderson | 8d06ce4 | 2020-09-15 10:44:37 -0400 | [diff] [blame] | 88 | log_debug("invalid compatible for syscon device\n"); |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 89 | return -EINVAL; |
| 90 | } |
| 91 | |
| 92 | /* bound to driver with same ofnode or to root if not found */ |
| 93 | if (device_find_global_by_ofnode(node, &parent)) |
| 94 | parent = dm_root(); |
| 95 | |
| 96 | /* force bound to syscon class */ |
| 97 | ret = device_bind_driver_to_node(parent, "syscon", |
| 98 | ofnode_get_name(node), |
| 99 | node, &dev); |
| 100 | if (ret) { |
| 101 | dev_dbg(dev, "unable to bound syscon device\n"); |
| 102 | return ret; |
| 103 | } |
| 104 | ret = device_probe(dev); |
| 105 | if (ret) { |
| 106 | dev_dbg(dev, "unable to probe syscon device\n"); |
| 107 | return ret; |
| 108 | } |
| 109 | |
| 110 | *devp = dev; |
| 111 | return 0; |
| 112 | } |
| 113 | |
Jean-Jacques Hiblot | dc44ea4 | 2018-11-29 10:57:37 +0100 | [diff] [blame] | 114 | struct regmap *syscon_regmap_lookup_by_phandle(struct udevice *dev, |
| 115 | const char *name) |
| 116 | { |
| 117 | struct udevice *syscon; |
| 118 | struct regmap *r; |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 119 | u32 phandle; |
| 120 | ofnode node; |
Jean-Jacques Hiblot | dc44ea4 | 2018-11-29 10:57:37 +0100 | [diff] [blame] | 121 | int err; |
| 122 | |
| 123 | err = uclass_get_device_by_phandle(UCLASS_SYSCON, dev, |
| 124 | name, &syscon); |
| 125 | if (err) { |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 126 | /* found node with "syscon" compatible, not bounded to SYSCON */ |
| 127 | err = ofnode_read_u32(dev_ofnode(dev), name, &phandle); |
| 128 | if (err) |
| 129 | return ERR_PTR(err); |
| 130 | |
| 131 | node = ofnode_get_by_phandle(phandle); |
| 132 | if (!ofnode_valid(node)) { |
| 133 | dev_dbg(dev, "unable to find syscon device\n"); |
| 134 | return ERR_PTR(-EINVAL); |
| 135 | } |
| 136 | err = syscon_probe_by_ofnode(node, &syscon); |
| 137 | if (err) |
| 138 | return ERR_PTR(-ENODEV); |
Jean-Jacques Hiblot | dc44ea4 | 2018-11-29 10:57:37 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | r = syscon_get_regmap(syscon); |
| 142 | if (!r) { |
| 143 | dev_dbg(dev, "unable to find regmap\n"); |
| 144 | return ERR_PTR(-ENODEV); |
| 145 | } |
| 146 | |
| 147 | return r; |
| 148 | } |
| 149 | |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 150 | int syscon_get_by_driver_data(ulong driver_data, struct udevice **devp) |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 151 | { |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 152 | int ret; |
| 153 | |
Simon Glass | 36ab8e3 | 2016-03-11 22:06:49 -0700 | [diff] [blame] | 154 | *devp = NULL; |
Simon Glass | e5f50c9 | 2020-02-06 09:54:51 -0700 | [diff] [blame] | 155 | |
| 156 | ret = uclass_first_device_drvdata(UCLASS_SYSCON, driver_data, devp); |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 157 | if (ret) |
Simon Glass | 72f0d99 | 2020-09-27 18:46:17 -0600 | [diff] [blame] | 158 | return ret; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 159 | |
Simon Glass | e5f50c9 | 2020-02-06 09:54:51 -0700 | [diff] [blame] | 160 | return 0; |
Simon Glass | 6d5579c | 2016-01-17 16:11:08 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | struct regmap *syscon_get_regmap_by_driver_data(ulong driver_data) |
| 164 | { |
| 165 | struct syscon_uc_info *priv; |
| 166 | struct udevice *dev; |
| 167 | int ret; |
| 168 | |
| 169 | ret = syscon_get_by_driver_data(driver_data, &dev); |
| 170 | if (ret) |
| 171 | return ERR_PTR(ret); |
| 172 | priv = dev_get_uclass_priv(dev); |
| 173 | |
| 174 | return priv->regmap; |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void *syscon_get_first_range(ulong driver_data) |
| 178 | { |
| 179 | struct regmap *map; |
| 180 | |
| 181 | map = syscon_get_regmap_by_driver_data(driver_data); |
| 182 | if (IS_ERR(map)) |
| 183 | return map; |
| 184 | return regmap_get_range(map, 0); |
| 185 | } |
| 186 | |
| 187 | UCLASS_DRIVER(syscon) = { |
| 188 | .id = UCLASS_SYSCON, |
| 189 | .name = "syscon", |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 190 | .per_device_auto = sizeof(struct syscon_uc_info), |
Simon Glass | 6a84aaf | 2015-06-23 15:38:43 -0600 | [diff] [blame] | 191 | .pre_probe = syscon_pre_probe, |
| 192 | }; |
Paul Burton | 8c946a7 | 2016-09-08 07:47:37 +0100 | [diff] [blame] | 193 | |
| 194 | static const struct udevice_id generic_syscon_ids[] = { |
| 195 | { .compatible = "syscon" }, |
| 196 | { } |
| 197 | }; |
| 198 | |
| 199 | U_BOOT_DRIVER(generic_syscon) = { |
| 200 | .name = "syscon", |
| 201 | .id = UCLASS_SYSCON, |
Simon Glass | 9288265 | 2021-08-07 07:24:04 -0600 | [diff] [blame] | 202 | #if CONFIG_IS_ENABLED(OF_REAL) |
Simon Glass | 8061bc6 | 2017-07-29 11:34:52 -0600 | [diff] [blame] | 203 | .bind = dm_scan_fdt_dev, |
| 204 | #endif |
Paul Burton | 8c946a7 | 2016-09-08 07:47:37 +0100 | [diff] [blame] | 205 | .of_match = generic_syscon_ids, |
| 206 | }; |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | * Linux-compatible syscon-to-regmap |
| 210 | * The syscon node can be bound to another driver, but still works |
| 211 | * as a syscon provider. |
| 212 | */ |
Patrick Delaunay | 8a5a8a7 | 2018-10-31 16:49:03 +0100 | [diff] [blame] | 213 | struct regmap *syscon_node_to_regmap(ofnode node) |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 214 | { |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 215 | struct udevice *dev; |
| 216 | struct regmap *r; |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 217 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 218 | if (uclass_get_device_by_ofnode(UCLASS_SYSCON, node, &dev)) |
| 219 | if (syscon_probe_by_ofnode(node, &dev)) |
| 220 | return ERR_PTR(-ENODEV); |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 221 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 222 | r = syscon_get_regmap(dev); |
| 223 | if (!r) { |
| 224 | dev_dbg(dev, "unable to find regmap\n"); |
| 225 | return ERR_PTR(-ENODEV); |
| 226 | } |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 227 | |
Patrick Delaunay | ee01043 | 2019-03-07 09:57:13 +0100 | [diff] [blame] | 228 | return r; |
Masahiro Yamada | 60f9a1e | 2018-04-19 12:14:04 +0900 | [diff] [blame] | 229 | } |