blob: a4247ecd62330e01ee4bc5d0db68ca21831fc968 [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>
Michal Suchanek92446452022-10-12 21:57:57 +020019#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060020#include <log.h>
Maxime Ripardd3e19cf2018-09-18 10:35:24 +030021#include <w1.h>
Eugen Hristev543b98c2018-09-18 10:35:28 +030022#include <w1-eeprom.h>
Maxime Ripardd3e19cf2018-09-18 10:35:24 +030023
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
30struct w1_bus {
31 u64 search_id;
32};
33
Kory Maincentc9dffc92021-05-04 19:31:26 +020034int w1_bus_find_dev(const struct udevice *bus, u64 id, struct udevice
35**devp)
36{
37 struct udevice *dev;
38 u8 family = id & 0xff;
Kory Maincentc9dffc92021-05-04 19:31:26 +020039
Michal Suchanek49549372022-10-12 21:58:08 +020040 for (uclass_first_device(UCLASS_W1_EEPROM, &dev);
41 dev;
Kory Maincentc9dffc92021-05-04 19:31:26 +020042 uclass_next_device(&dev)) {
Kory Maincentc9dffc92021-05-04 19:31:26 +020043
44 if (dev_get_driver_data(dev) == family) {
45 *devp = dev;
46 return 0;
47 }
48 }
49
50 return -ENODEV;
51}
52
53int 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
Maxime Ripardd3e19cf2018-09-18 10:35:24 +030099static 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
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300158 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));
Eugen Hristev543b98c2018-09-18 10:35:28 +0300169
Kory Maincentc9dffc92021-05-04 19:31:26 +0200170 /* attempt to register as w1 device */
171 w1_register_new_device(rn, bus);
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300172 }
173 }
174
175 return 0;
176}
177
178int w1_get_bus(int busnum, struct udevice **busp)
179{
180 int ret, i = 0;
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300181 struct udevice *dev;
182
Michal Suchanek92446452022-10-12 21:57:57 +0200183 for (ret = uclass_first_device_check(UCLASS_W1, &dev);
184 dev;
185 ret = uclass_next_device_check(&dev), i++) {
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300186 if (i == busnum) {
Michal Suchanek92446452022-10-12 21:57:57 +0200187 if (ret) {
188 debug("Cannot probe w1 bus %d: %d (%s)\n",
189 busnum, ret, errno_str(ret));
190 return ret;
191 }
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300192 *busp = dev;
193 return 0;
194 }
195 }
Martin Fuzzey65b60892018-10-22 18:31:08 +0200196
Michal Suchanek92446452022-10-12 21:57:57 +0200197 debug("Cannot find w1 bus %d\n", busnum);
Martin Fuzzey65b60892018-10-22 18:31:08 +0200198
Michal Suchanek92446452022-10-12 21:57:57 +0200199 return -ENODEV;
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300200}
201
202u8 w1_get_device_family(struct udevice *dev)
203{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700204 struct w1_device *w1 = dev_get_parent_plat(dev);
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300205
206 return w1->id & 0xff;
207}
208
209int w1_reset_select(struct udevice *dev)
210{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700211 struct w1_device *w1 = dev_get_parent_plat(dev);
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300212 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
229int 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
240int 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
255int 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
268static int w1_post_probe(struct udevice *bus)
269{
270 w1_enumerate(bus);
271
272 return 0;
273}
274
275int 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
300UCLASS_DRIVER(w1) = {
301 .name = "w1",
302 .id = UCLASS_W1,
303 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass41575d82020-12-03 16:55:17 -0700304 .per_device_auto = sizeof(struct w1_bus),
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300305 .post_probe = w1_post_probe,
306#if CONFIG_IS_ENABLED(OF_CONTROL)
307 .post_bind = dm_scan_fdt_dev,
308#endif
Simon Glasscaa4daa2020-12-03 16:55:18 -0700309 .per_child_plat_auto = sizeof(struct w1_device),
Maxime Ripardd3e19cf2018-09-18 10:35:24 +0300310};