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