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