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.
17 #include <w1-eeprom.h>
19 #include <dm/device-internal.h>
21 #define W1_MATCH_ROM 0x55
22 #define W1_SKIP_ROM 0xcc
23 #define W1_SEARCH 0xf0
29 static int w1_enumerate(struct udevice *bus)
31 const struct w1_ops *ops = device_get_ops(bus);
32 struct w1_bus *w1 = dev_get_uclass_priv(bus);
33 u64 last_rn, rn = w1->search_id, tmp64;
34 bool last_device = false;
35 int search_bit, desc_bit = 64;
40 if (!ops->reset || !ops->write_byte || !ops->triplet)
43 while (!last_device) {
48 * Reset bus and all 1-wire device state machines
49 * so they can respond to our requests.
51 * Return 0 - device(s) present, 1 - no devices present.
53 if (ops->reset(bus)) {
54 debug("%s: No devices present on the wire.\n",
59 /* Start the search */
60 ops->write_byte(bus, W1_SEARCH);
61 for (i = 0; i < 64; ++i) {
62 /* Determine the direction/search bit */
64 /* took the 0 path last time, so take the 1 path */
66 else if (i > desc_bit)
67 /* take the 0 path on the next branch */
70 search_bit = ((last_rn >> i) & 0x1);
72 /* Read two bits and write one bit */
73 triplet_ret = ops->triplet(bus, search_bit);
75 /* quit if no device responded */
76 if ((triplet_ret & 0x03) == 0x03)
79 /* If both directions were valid, and we took the 0 path... */
83 /* extract the direction taken & update the device number */
84 tmp64 = (triplet_ret >> 2);
88 if ((triplet_ret & 0x03) != 0x03) {
89 if (desc_bit == last_zero || last_zero < 0) {
97 debug("%s: Detected new device 0x%llx (family 0x%x)\n",
98 bus->name, rn, (u8)(rn & 0xff));
100 /* attempt to register as w1-eeprom device */
101 w1_eeprom_register_new_device(rn);
108 int w1_get_bus(int busnum, struct udevice **busp)
114 for (ret = uclass_first_device(UCLASS_W1, &dev);
116 ret = uclass_next_device(&dev), i++) {
124 debug("Cannot find w1 bus %d\n", busnum);
131 u8 w1_get_device_family(struct udevice *dev)
133 struct w1_device *w1 = dev_get_parent_plat(dev);
135 return w1->id & 0xff;
138 int w1_reset_select(struct udevice *dev)
140 struct w1_device *w1 = dev_get_parent_plat(dev);
141 struct udevice *bus = dev_get_parent(dev);
142 const struct w1_ops *ops = device_get_ops(bus);
145 if (!ops->reset || !ops->write_byte)
150 ops->write_byte(bus, W1_MATCH_ROM);
152 for (i = 0; i < sizeof(w1->id); i++)
153 ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
158 int w1_read_byte(struct udevice *dev)
160 struct udevice *bus = dev_get_parent(dev);
161 const struct w1_ops *ops = device_get_ops(bus);
166 return ops->read_byte(bus);
169 int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
173 for (i = 0; i < count; i++) {
174 ret = w1_read_byte(dev);
184 int w1_write_byte(struct udevice *dev, u8 byte)
186 struct udevice *bus = dev_get_parent(dev);
187 const struct w1_ops *ops = device_get_ops(bus);
189 if (!ops->write_byte)
192 ops->write_byte(bus, byte);
197 static int w1_post_probe(struct udevice *bus)
210 ret = uclass_get(UCLASS_W1, &uc);
214 uclass_foreach_dev(bus, uc) {
215 ret = device_probe(bus);
216 if (ret == -ENODEV) { /* No such device. */
217 printf("W1 controller not available.\n");
221 if (ret) { /* Other error. */
222 printf("W1 controller probe failed.\n");
229 UCLASS_DRIVER(w1) = {
232 .flags = DM_UC_FLAG_SEQ_ALIAS,
233 .per_device_auto = sizeof(struct w1_bus),
234 .post_probe = w1_post_probe,
235 #if CONFIG_IS_ENABLED(OF_CONTROL)
236 .post_bind = dm_scan_fdt_dev,
238 .per_child_plat_auto = sizeof(struct w1_device),