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