1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright (c) 2015 Free Electrons
5 * Copyright (c) 2015 NextThing Co.
6 * Copyright (c) 2018 Microchip Technology, Inc.
7 * Copyright (c) 2021 Bootlin
15 #define LOG_CATEGORY UCLASS_W1
21 #include <w1-eeprom.h>
23 #include <dm/device-internal.h>
25 #define W1_MATCH_ROM 0x55
26 #define W1_SKIP_ROM 0xcc
27 #define W1_SEARCH 0xf0
33 int w1_bus_find_dev(const struct udevice *bus, u64 id, struct udevice
37 u8 family = id & 0xff;
39 for (uclass_first_device(UCLASS_W1_EEPROM, &dev);
41 uclass_next_device(&dev)) {
43 if (dev_get_driver_data(dev) == family) {
52 int w1_register_new_device(u64 id, struct udevice *bus)
54 u8 family = id & 0xff;
58 struct w1_driver_entry *start, *entry;
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);
63 for (entry = start; entry != start + n_ents; entry++) {
64 const u8 *match_family;
65 const struct driver *drv;
68 for (match_family = entry->family; match_family;
70 if (*match_family != family)
73 ret = w1_bus_find_dev(bus, id, &dev);
75 /* If nothing in the device tree, bind a device */
78 ret = device_bind(bus, drv, drv->name,
79 NULL, ofnode_null(), &dev);
86 w1 = dev_get_parent_plat(dev);
93 debug("%s: No matches found: error %d\n", __func__, ret);
98 static int w1_enumerate(struct udevice *bus)
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;
109 if (!ops->reset || !ops->write_byte || !ops->triplet)
112 while (!last_device) {
117 * Reset bus and all 1-wire device state machines
118 * so they can respond to our requests.
120 * Return 0 - device(s) present, 1 - no devices present.
122 if (ops->reset(bus)) {
123 debug("%s: No devices present on the wire.\n",
128 /* Start the search */
129 ops->write_byte(bus, W1_SEARCH);
130 for (i = 0; i < 64; ++i) {
131 /* Determine the direction/search bit */
133 /* took the 0 path last time, so take the 1 path */
135 else if (i > desc_bit)
136 /* take the 0 path on the next branch */
139 search_bit = ((last_rn >> i) & 0x1);
141 /* Read two bits and write one bit */
142 triplet_ret = ops->triplet(bus, search_bit);
144 /* quit if no device responded */
145 if ((triplet_ret & 0x03) == 0x03)
148 /* If both directions were valid, and we took the 0 path... */
149 if (triplet_ret == 0)
152 /* extract the direction taken & update the device number */
153 tmp64 = (triplet_ret >> 2);
157 if ((triplet_ret & 0x03) != 0x03) {
158 if (desc_bit == last_zero || last_zero < 0) {
164 desc_bit = last_zero;
166 debug("%s: Detected new device 0x%llx (family 0x%x)\n",
167 bus->name, rn, (u8)(rn & 0xff));
169 /* attempt to register as w1 device */
170 w1_register_new_device(rn, bus);
177 int w1_get_bus(int busnum, struct udevice **busp)
182 for (ret = uclass_first_device_check(UCLASS_W1, &dev);
184 ret = uclass_next_device_check(&dev), i++) {
187 debug("Cannot probe w1 bus %d: %d (%s)\n",
188 busnum, ret, errno_str(ret));
196 debug("Cannot find w1 bus %d\n", busnum);
201 u8 w1_get_device_family(struct udevice *dev)
203 struct w1_device *w1 = dev_get_parent_plat(dev);
205 return w1->id & 0xff;
208 int w1_reset_select(struct udevice *dev)
210 struct w1_device *w1 = dev_get_parent_plat(dev);
211 struct udevice *bus = dev_get_parent(dev);
212 const struct w1_ops *ops = device_get_ops(bus);
215 if (!ops->reset || !ops->write_byte)
220 ops->write_byte(bus, W1_MATCH_ROM);
222 for (i = 0; i < sizeof(w1->id); i++)
223 ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
228 int w1_read_byte(struct udevice *dev)
230 struct udevice *bus = dev_get_parent(dev);
231 const struct w1_ops *ops = device_get_ops(bus);
236 return ops->read_byte(bus);
239 int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
243 for (i = 0; i < count; i++) {
244 ret = w1_read_byte(dev);
254 int w1_write_byte(struct udevice *dev, u8 byte)
256 struct udevice *bus = dev_get_parent(dev);
257 const struct w1_ops *ops = device_get_ops(bus);
259 if (!ops->write_byte)
262 ops->write_byte(bus, byte);
267 static int w1_post_probe(struct udevice *bus)
280 ret = uclass_get(UCLASS_W1, &uc);
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");
291 if (ret) { /* Other error. */
292 printf("W1 controller probe failed.\n");
299 UCLASS_DRIVER(w1) = {
302 .flags = DM_UC_FLAG_SEQ_ALIAS,
303 .per_device_auto = sizeof(struct w1_bus),
304 .post_probe = w1_post_probe,
305 #if CONFIG_IS_ENABLED(OF_CONTROL)
306 .post_bind = dm_scan_fdt_dev,
308 .per_child_plat_auto = sizeof(struct w1_device),