blob: 8bc6cb13f49c3a82a1c65afc88e997f5b61b0ffc [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.
7 *
8 * Maxime Ripard <maxime.ripard@free-electrons.com>
9 * Eugen Hristev <eugen.hristev@microchip.com>
10 *
11 */
12
13#include <common.h>
14#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Maxime Ripardbdbdca32018-09-18 10:35:24 +030016#include <w1.h>
Eugen Hristev6a546122018-09-18 10:35:28 +030017#include <w1-eeprom.h>
Maxime Ripardbdbdca32018-09-18 10:35:24 +030018
19#include <dm/device-internal.h>
20
21#define W1_MATCH_ROM 0x55
22#define W1_SKIP_ROM 0xcc
23#define W1_SEARCH 0xf0
24
25struct w1_bus {
26 u64 search_id;
27};
28
29static int w1_enumerate(struct udevice *bus)
30{
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;
36 int last_zero = -1;
37 u8 triplet_ret = 0;
38 int i;
39
40 if (!ops->reset || !ops->write_byte || !ops->triplet)
41 return -ENOSYS;
42
43 while (!last_device) {
44 last_rn = rn;
45 rn = 0;
46
47 /*
48 * Reset bus and all 1-wire device state machines
49 * so they can respond to our requests.
50 *
51 * Return 0 - device(s) present, 1 - no devices present.
52 */
53 if (ops->reset(bus)) {
54 debug("%s: No devices present on the wire.\n",
55 __func__);
56 break;
57 }
58
59 /* Start the search */
60 ops->write_byte(bus, W1_SEARCH);
61 for (i = 0; i < 64; ++i) {
62 /* Determine the direction/search bit */
63 if (i == desc_bit)
64 /* took the 0 path last time, so take the 1 path */
65 search_bit = 1;
66 else if (i > desc_bit)
67 /* take the 0 path on the next branch */
68 search_bit = 0;
69 else
70 search_bit = ((last_rn >> i) & 0x1);
71
72 /* Read two bits and write one bit */
73 triplet_ret = ops->triplet(bus, search_bit);
74
75 /* quit if no device responded */
76 if ((triplet_ret & 0x03) == 0x03)
77 break;
78
79 /* If both directions were valid, and we took the 0 path... */
80 if (triplet_ret == 0)
81 last_zero = i;
82
83 /* extract the direction taken & update the device number */
84 tmp64 = (triplet_ret >> 2);
85 rn |= (tmp64 << i);
86 }
87
Maxime Ripardbdbdca32018-09-18 10:35:24 +030088 if ((triplet_ret & 0x03) != 0x03) {
89 if (desc_bit == last_zero || last_zero < 0) {
90 last_device = 1;
91 w1->search_id = 0;
92 } else {
93 w1->search_id = rn;
94 }
95 desc_bit = last_zero;
96
97 debug("%s: Detected new device 0x%llx (family 0x%x)\n",
98 bus->name, rn, (u8)(rn & 0xff));
Eugen Hristev6a546122018-09-18 10:35:28 +030099
100 /* attempt to register as w1-eeprom device */
101 w1_eeprom_register_new_device(rn);
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300102 }
103 }
104
105 return 0;
106}
107
108int w1_get_bus(int busnum, struct udevice **busp)
109{
110 int ret, i = 0;
111
112 struct udevice *dev;
113
114 for (ret = uclass_first_device(UCLASS_W1, &dev);
Martin Fuzzey775e7ad2018-10-22 18:31:08 +0200115 dev && !ret;
116 ret = uclass_next_device(&dev), i++) {
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300117 if (i == busnum) {
118 *busp = dev;
119 return 0;
120 }
121 }
Martin Fuzzey775e7ad2018-10-22 18:31:08 +0200122
123 if (!ret) {
124 debug("Cannot find w1 bus %d\n", busnum);
125 ret = -ENODEV;
126 }
127
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300128 return ret;
129}
130
131u8 w1_get_device_family(struct udevice *dev)
132{
Simon Glass71fa5b42020-12-03 16:55:18 -0700133 struct w1_device *w1 = dev_get_parent_plat(dev);
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300134
135 return w1->id & 0xff;
136}
137
138int w1_reset_select(struct udevice *dev)
139{
Simon Glass71fa5b42020-12-03 16:55:18 -0700140 struct w1_device *w1 = dev_get_parent_plat(dev);
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300141 struct udevice *bus = dev_get_parent(dev);
142 const struct w1_ops *ops = device_get_ops(bus);
143 int i;
144
145 if (!ops->reset || !ops->write_byte)
146 return -ENOSYS;
147
148 ops->reset(bus);
149
150 ops->write_byte(bus, W1_MATCH_ROM);
151
152 for (i = 0; i < sizeof(w1->id); i++)
153 ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff);
154
155 return 0;
156}
157
158int w1_read_byte(struct udevice *dev)
159{
160 struct udevice *bus = dev_get_parent(dev);
161 const struct w1_ops *ops = device_get_ops(bus);
162
163 if (!ops->read_byte)
164 return -ENOSYS;
165
166 return ops->read_byte(bus);
167}
168
169int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count)
170{
171 int i, ret;
172
173 for (i = 0; i < count; i++) {
174 ret = w1_read_byte(dev);
175 if (ret < 0)
176 return ret;
177
178 buf[i] = ret & 0xff;
179 }
180
181 return 0;
182}
183
184int w1_write_byte(struct udevice *dev, u8 byte)
185{
186 struct udevice *bus = dev_get_parent(dev);
187 const struct w1_ops *ops = device_get_ops(bus);
188
189 if (!ops->write_byte)
190 return -ENOSYS;
191
192 ops->write_byte(bus, byte);
193
194 return 0;
195}
196
197static int w1_post_probe(struct udevice *bus)
198{
199 w1_enumerate(bus);
200
201 return 0;
202}
203
204int w1_init(void)
205{
206 struct udevice *bus;
207 struct uclass *uc;
208 int ret;
209
210 ret = uclass_get(UCLASS_W1, &uc);
211 if (ret)
212 return ret;
213
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");
218 continue;
219 }
220
221 if (ret) { /* Other error. */
222 printf("W1 controller probe failed.\n");
223 continue;
224 }
225 }
226 return 0;
227}
228
229UCLASS_DRIVER(w1) = {
230 .name = "w1",
231 .id = UCLASS_W1,
232 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700233 .per_device_auto = sizeof(struct w1_bus),
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300234 .post_probe = w1_post_probe,
235#if CONFIG_IS_ENABLED(OF_CONTROL)
236 .post_bind = dm_scan_fdt_dev,
237#endif
Simon Glass71fa5b42020-12-03 16:55:18 -0700238 .per_child_plat_auto = sizeof(struct w1_device),
Maxime Ripardbdbdca32018-09-18 10:35:24 +0300239};