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