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