Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * |
| 4 | * Copyright (c) 2015 Free Electrons |
| 5 | * Copyright (c) 2015 NextThing Co. |
| 6 | * Copyright (c) 2018 Microchip Technology, Inc. |
Kory Maincent | a0670e5 | 2021-05-04 19:31:26 +0200 | [diff] [blame] | 7 | * Copyright (c) 2021 Bootlin |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 8 | * |
| 9 | * Maxime Ripard <maxime.ripard@free-electrons.com> |
| 10 | * Eugen Hristev <eugen.hristev@microchip.com> |
Kory Maincent | a0670e5 | 2021-05-04 19:31:26 +0200 | [diff] [blame] | 11 | * Kory Maincent <kory.maincent@bootlin.com> |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 12 | * |
| 13 | */ |
| 14 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 15 | #define LOG_CATEGORY UCLASS_W1 |
| 16 | |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 17 | #include <common.h> |
| 18 | #include <dm.h> |
Michal Suchanek | 55b6966 | 2022-10-12 21:57:57 +0200 | [diff] [blame] | 19 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 20 | #include <log.h> |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 21 | #include <w1.h> |
Eugen Hristev | 6a54612 | 2018-09-18 10:35:28 +0300 | [diff] [blame] | 22 | #include <w1-eeprom.h> |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 23 | |
| 24 | #include <dm/device-internal.h> |
| 25 | |
| 26 | #define W1_MATCH_ROM 0x55 |
| 27 | #define W1_SKIP_ROM 0xcc |
| 28 | #define W1_SEARCH 0xf0 |
| 29 | |
| 30 | struct w1_bus { |
| 31 | u64 search_id; |
| 32 | }; |
| 33 | |
Kory Maincent | a0670e5 | 2021-05-04 19:31:26 +0200 | [diff] [blame] | 34 | int w1_bus_find_dev(const struct udevice *bus, u64 id, struct udevice |
| 35 | **devp) |
| 36 | { |
| 37 | struct udevice *dev; |
| 38 | u8 family = id & 0xff; |
| 39 | int ret; |
| 40 | |
| 41 | for (ret = uclass_first_device(UCLASS_W1_EEPROM, &dev); |
| 42 | !ret && dev; |
| 43 | uclass_next_device(&dev)) { |
| 44 | if (ret || !dev) { |
| 45 | debug("cannot find w1 eeprom dev\n"); |
| 46 | return -ENODEV; |
| 47 | } |
| 48 | |
| 49 | if (dev_get_driver_data(dev) == family) { |
| 50 | *devp = dev; |
| 51 | return 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return -ENODEV; |
| 56 | } |
| 57 | |
| 58 | int w1_register_new_device(u64 id, struct udevice *bus) |
| 59 | { |
| 60 | u8 family = id & 0xff; |
| 61 | int n_ents, ret = 0; |
| 62 | struct udevice *dev; |
| 63 | |
| 64 | struct w1_driver_entry *start, *entry; |
| 65 | |
| 66 | start = ll_entry_start(struct w1_driver_entry, w1_driver_entry); |
| 67 | n_ents = ll_entry_count(struct w1_driver_entry, w1_driver_entry); |
| 68 | |
| 69 | for (entry = start; entry != start + n_ents; entry++) { |
| 70 | const u8 *match_family; |
| 71 | const struct driver *drv; |
| 72 | struct w1_device *w1; |
| 73 | |
| 74 | for (match_family = entry->family; match_family; |
| 75 | match_family++) { |
| 76 | if (*match_family != family) |
| 77 | continue; |
| 78 | |
| 79 | ret = w1_bus_find_dev(bus, id, &dev); |
| 80 | |
| 81 | /* If nothing in the device tree, bind a device */ |
| 82 | if (ret == -ENODEV) { |
| 83 | drv = entry->driver; |
| 84 | ret = device_bind(bus, drv, drv->name, |
| 85 | NULL, ofnode_null(), &dev); |
| 86 | if (ret) |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | device_probe(dev); |
| 91 | |
| 92 | w1 = dev_get_parent_plat(dev); |
| 93 | w1->id = id; |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | debug("%s: No matches found: error %d\n", __func__, ret); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 104 | static int w1_enumerate(struct udevice *bus) |
| 105 | { |
| 106 | const struct w1_ops *ops = device_get_ops(bus); |
| 107 | struct w1_bus *w1 = dev_get_uclass_priv(bus); |
| 108 | u64 last_rn, rn = w1->search_id, tmp64; |
| 109 | bool last_device = false; |
| 110 | int search_bit, desc_bit = 64; |
| 111 | int last_zero = -1; |
| 112 | u8 triplet_ret = 0; |
| 113 | int i; |
| 114 | |
| 115 | if (!ops->reset || !ops->write_byte || !ops->triplet) |
| 116 | return -ENOSYS; |
| 117 | |
| 118 | while (!last_device) { |
| 119 | last_rn = rn; |
| 120 | rn = 0; |
| 121 | |
| 122 | /* |
| 123 | * Reset bus and all 1-wire device state machines |
| 124 | * so they can respond to our requests. |
| 125 | * |
| 126 | * Return 0 - device(s) present, 1 - no devices present. |
| 127 | */ |
| 128 | if (ops->reset(bus)) { |
| 129 | debug("%s: No devices present on the wire.\n", |
| 130 | __func__); |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | /* Start the search */ |
| 135 | ops->write_byte(bus, W1_SEARCH); |
| 136 | for (i = 0; i < 64; ++i) { |
| 137 | /* Determine the direction/search bit */ |
| 138 | if (i == desc_bit) |
| 139 | /* took the 0 path last time, so take the 1 path */ |
| 140 | search_bit = 1; |
| 141 | else if (i > desc_bit) |
| 142 | /* take the 0 path on the next branch */ |
| 143 | search_bit = 0; |
| 144 | else |
| 145 | search_bit = ((last_rn >> i) & 0x1); |
| 146 | |
| 147 | /* Read two bits and write one bit */ |
| 148 | triplet_ret = ops->triplet(bus, search_bit); |
| 149 | |
| 150 | /* quit if no device responded */ |
| 151 | if ((triplet_ret & 0x03) == 0x03) |
| 152 | break; |
| 153 | |
| 154 | /* If both directions were valid, and we took the 0 path... */ |
| 155 | if (triplet_ret == 0) |
| 156 | last_zero = i; |
| 157 | |
| 158 | /* extract the direction taken & update the device number */ |
| 159 | tmp64 = (triplet_ret >> 2); |
| 160 | rn |= (tmp64 << i); |
| 161 | } |
| 162 | |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 163 | if ((triplet_ret & 0x03) != 0x03) { |
| 164 | if (desc_bit == last_zero || last_zero < 0) { |
| 165 | last_device = 1; |
| 166 | w1->search_id = 0; |
| 167 | } else { |
| 168 | w1->search_id = rn; |
| 169 | } |
| 170 | desc_bit = last_zero; |
| 171 | |
| 172 | debug("%s: Detected new device 0x%llx (family 0x%x)\n", |
| 173 | bus->name, rn, (u8)(rn & 0xff)); |
Eugen Hristev | 6a54612 | 2018-09-18 10:35:28 +0300 | [diff] [blame] | 174 | |
Kory Maincent | a0670e5 | 2021-05-04 19:31:26 +0200 | [diff] [blame] | 175 | /* attempt to register as w1 device */ |
| 176 | w1_register_new_device(rn, bus); |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | int w1_get_bus(int busnum, struct udevice **busp) |
| 184 | { |
| 185 | int ret, i = 0; |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 186 | struct udevice *dev; |
| 187 | |
Michal Suchanek | 55b6966 | 2022-10-12 21:57:57 +0200 | [diff] [blame] | 188 | for (ret = uclass_first_device_check(UCLASS_W1, &dev); |
| 189 | dev; |
| 190 | ret = uclass_next_device_check(&dev), i++) { |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 191 | if (i == busnum) { |
Michal Suchanek | 55b6966 | 2022-10-12 21:57:57 +0200 | [diff] [blame] | 192 | if (ret) { |
| 193 | debug("Cannot probe w1 bus %d: %d (%s)\n", |
| 194 | busnum, ret, errno_str(ret)); |
| 195 | return ret; |
| 196 | } |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 197 | *busp = dev; |
| 198 | return 0; |
| 199 | } |
| 200 | } |
Martin Fuzzey | 775e7ad | 2018-10-22 18:31:08 +0200 | [diff] [blame] | 201 | |
Michal Suchanek | 55b6966 | 2022-10-12 21:57:57 +0200 | [diff] [blame] | 202 | debug("Cannot find w1 bus %d\n", busnum); |
Martin Fuzzey | 775e7ad | 2018-10-22 18:31:08 +0200 | [diff] [blame] | 203 | |
Michal Suchanek | 55b6966 | 2022-10-12 21:57:57 +0200 | [diff] [blame] | 204 | return -ENODEV; |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | u8 w1_get_device_family(struct udevice *dev) |
| 208 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 209 | struct w1_device *w1 = dev_get_parent_plat(dev); |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 210 | |
| 211 | return w1->id & 0xff; |
| 212 | } |
| 213 | |
| 214 | int w1_reset_select(struct udevice *dev) |
| 215 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 216 | struct w1_device *w1 = dev_get_parent_plat(dev); |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 217 | struct udevice *bus = dev_get_parent(dev); |
| 218 | const struct w1_ops *ops = device_get_ops(bus); |
| 219 | int i; |
| 220 | |
| 221 | if (!ops->reset || !ops->write_byte) |
| 222 | return -ENOSYS; |
| 223 | |
| 224 | ops->reset(bus); |
| 225 | |
| 226 | ops->write_byte(bus, W1_MATCH_ROM); |
| 227 | |
| 228 | for (i = 0; i < sizeof(w1->id); i++) |
| 229 | ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int w1_read_byte(struct udevice *dev) |
| 235 | { |
| 236 | struct udevice *bus = dev_get_parent(dev); |
| 237 | const struct w1_ops *ops = device_get_ops(bus); |
| 238 | |
| 239 | if (!ops->read_byte) |
| 240 | return -ENOSYS; |
| 241 | |
| 242 | return ops->read_byte(bus); |
| 243 | } |
| 244 | |
| 245 | int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count) |
| 246 | { |
| 247 | int i, ret; |
| 248 | |
| 249 | for (i = 0; i < count; i++) { |
| 250 | ret = w1_read_byte(dev); |
| 251 | if (ret < 0) |
| 252 | return ret; |
| 253 | |
| 254 | buf[i] = ret & 0xff; |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | int w1_write_byte(struct udevice *dev, u8 byte) |
| 261 | { |
| 262 | struct udevice *bus = dev_get_parent(dev); |
| 263 | const struct w1_ops *ops = device_get_ops(bus); |
| 264 | |
| 265 | if (!ops->write_byte) |
| 266 | return -ENOSYS; |
| 267 | |
| 268 | ops->write_byte(bus, byte); |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int w1_post_probe(struct udevice *bus) |
| 274 | { |
| 275 | w1_enumerate(bus); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | int w1_init(void) |
| 281 | { |
| 282 | struct udevice *bus; |
| 283 | struct uclass *uc; |
| 284 | int ret; |
| 285 | |
| 286 | ret = uclass_get(UCLASS_W1, &uc); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | |
| 290 | uclass_foreach_dev(bus, uc) { |
| 291 | ret = device_probe(bus); |
| 292 | if (ret == -ENODEV) { /* No such device. */ |
| 293 | printf("W1 controller not available.\n"); |
| 294 | continue; |
| 295 | } |
| 296 | |
| 297 | if (ret) { /* Other error. */ |
| 298 | printf("W1 controller probe failed.\n"); |
| 299 | continue; |
| 300 | } |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | UCLASS_DRIVER(w1) = { |
| 306 | .name = "w1", |
| 307 | .id = UCLASS_W1, |
| 308 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 309 | .per_device_auto = sizeof(struct w1_bus), |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 310 | .post_probe = w1_post_probe, |
| 311 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 312 | .post_bind = dm_scan_fdt_dev, |
| 313 | #endif |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 314 | .per_child_plat_auto = sizeof(struct w1_device), |
Maxime Ripard | bdbdca3 | 2018-09-18 10:35:24 +0300 | [diff] [blame] | 315 | }; |