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