Maxime Ripard | d3e19cf | 2018-09-18 10:35:24 +0300 | [diff] [blame^] | 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 | * |
| 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> |
| 15 | #include <w1.h> |
| 16 | |
| 17 | #include <dm/device-internal.h> |
| 18 | |
| 19 | #define W1_MATCH_ROM 0x55 |
| 20 | #define W1_SKIP_ROM 0xcc |
| 21 | #define W1_SEARCH 0xf0 |
| 22 | |
| 23 | struct w1_bus { |
| 24 | u64 search_id; |
| 25 | }; |
| 26 | |
| 27 | static int w1_enumerate(struct udevice *bus) |
| 28 | { |
| 29 | const struct w1_ops *ops = device_get_ops(bus); |
| 30 | struct w1_bus *w1 = dev_get_uclass_priv(bus); |
| 31 | u64 last_rn, rn = w1->search_id, tmp64; |
| 32 | bool last_device = false; |
| 33 | int search_bit, desc_bit = 64; |
| 34 | int last_zero = -1; |
| 35 | u8 triplet_ret = 0; |
| 36 | int i; |
| 37 | |
| 38 | if (!ops->reset || !ops->write_byte || !ops->triplet) |
| 39 | return -ENOSYS; |
| 40 | |
| 41 | while (!last_device) { |
| 42 | last_rn = rn; |
| 43 | rn = 0; |
| 44 | |
| 45 | /* |
| 46 | * Reset bus and all 1-wire device state machines |
| 47 | * so they can respond to our requests. |
| 48 | * |
| 49 | * Return 0 - device(s) present, 1 - no devices present. |
| 50 | */ |
| 51 | if (ops->reset(bus)) { |
| 52 | debug("%s: No devices present on the wire.\n", |
| 53 | __func__); |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | /* Start the search */ |
| 58 | ops->write_byte(bus, W1_SEARCH); |
| 59 | for (i = 0; i < 64; ++i) { |
| 60 | /* Determine the direction/search bit */ |
| 61 | if (i == desc_bit) |
| 62 | /* took the 0 path last time, so take the 1 path */ |
| 63 | search_bit = 1; |
| 64 | else if (i > desc_bit) |
| 65 | /* take the 0 path on the next branch */ |
| 66 | search_bit = 0; |
| 67 | else |
| 68 | search_bit = ((last_rn >> i) & 0x1); |
| 69 | |
| 70 | /* Read two bits and write one bit */ |
| 71 | triplet_ret = ops->triplet(bus, search_bit); |
| 72 | |
| 73 | /* quit if no device responded */ |
| 74 | if ((triplet_ret & 0x03) == 0x03) |
| 75 | break; |
| 76 | |
| 77 | /* If both directions were valid, and we took the 0 path... */ |
| 78 | if (triplet_ret == 0) |
| 79 | last_zero = i; |
| 80 | |
| 81 | /* extract the direction taken & update the device number */ |
| 82 | tmp64 = (triplet_ret >> 2); |
| 83 | rn |= (tmp64 << i); |
| 84 | } |
| 85 | |
| 86 | /* last device or error, aborting here */ |
| 87 | if ((triplet_ret & 0x03) == 0x03) |
| 88 | last_device = true; |
| 89 | |
| 90 | if ((triplet_ret & 0x03) != 0x03) { |
| 91 | if (desc_bit == last_zero || last_zero < 0) { |
| 92 | last_device = 1; |
| 93 | w1->search_id = 0; |
| 94 | } else { |
| 95 | w1->search_id = rn; |
| 96 | } |
| 97 | desc_bit = last_zero; |
| 98 | |
| 99 | debug("%s: Detected new device 0x%llx (family 0x%x)\n", |
| 100 | bus->name, rn, (u8)(rn & 0xff)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | int w1_get_bus(int busnum, struct udevice **busp) |
| 108 | { |
| 109 | int ret, i = 0; |
| 110 | |
| 111 | struct udevice *dev; |
| 112 | |
| 113 | for (ret = uclass_first_device(UCLASS_W1, &dev); |
| 114 | !ret; |
| 115 | uclass_next_device(&dev), i++) { |
| 116 | if (ret) { |
| 117 | debug("Cannot find w1 bus %d\n", busnum); |
| 118 | return ret; |
| 119 | } |
| 120 | if (i == busnum) { |
| 121 | *busp = dev; |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | return ret; |
| 126 | } |
| 127 | |
| 128 | u8 w1_get_device_family(struct udevice *dev) |
| 129 | { |
| 130 | struct w1_device *w1 = dev_get_parent_platdata(dev); |
| 131 | |
| 132 | return w1->id & 0xff; |
| 133 | } |
| 134 | |
| 135 | int w1_reset_select(struct udevice *dev) |
| 136 | { |
| 137 | struct w1_device *w1 = dev_get_parent_platdata(dev); |
| 138 | struct udevice *bus = dev_get_parent(dev); |
| 139 | const struct w1_ops *ops = device_get_ops(bus); |
| 140 | int i; |
| 141 | |
| 142 | if (!ops->reset || !ops->write_byte) |
| 143 | return -ENOSYS; |
| 144 | |
| 145 | ops->reset(bus); |
| 146 | |
| 147 | ops->write_byte(bus, W1_MATCH_ROM); |
| 148 | |
| 149 | for (i = 0; i < sizeof(w1->id); i++) |
| 150 | ops->write_byte(bus, (w1->id >> (i * 8)) & 0xff); |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | int w1_read_byte(struct udevice *dev) |
| 156 | { |
| 157 | struct udevice *bus = dev_get_parent(dev); |
| 158 | const struct w1_ops *ops = device_get_ops(bus); |
| 159 | |
| 160 | if (!ops->read_byte) |
| 161 | return -ENOSYS; |
| 162 | |
| 163 | return ops->read_byte(bus); |
| 164 | } |
| 165 | |
| 166 | int w1_read_buf(struct udevice *dev, u8 *buf, unsigned int count) |
| 167 | { |
| 168 | int i, ret; |
| 169 | |
| 170 | for (i = 0; i < count; i++) { |
| 171 | ret = w1_read_byte(dev); |
| 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | |
| 175 | buf[i] = ret & 0xff; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int w1_write_byte(struct udevice *dev, u8 byte) |
| 182 | { |
| 183 | struct udevice *bus = dev_get_parent(dev); |
| 184 | const struct w1_ops *ops = device_get_ops(bus); |
| 185 | |
| 186 | if (!ops->write_byte) |
| 187 | return -ENOSYS; |
| 188 | |
| 189 | ops->write_byte(bus, byte); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static int w1_post_probe(struct udevice *bus) |
| 195 | { |
| 196 | w1_enumerate(bus); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int w1_init(void) |
| 202 | { |
| 203 | struct udevice *bus; |
| 204 | struct uclass *uc; |
| 205 | int ret; |
| 206 | |
| 207 | ret = uclass_get(UCLASS_W1, &uc); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
| 211 | uclass_foreach_dev(bus, uc) { |
| 212 | ret = device_probe(bus); |
| 213 | if (ret == -ENODEV) { /* No such device. */ |
| 214 | printf("W1 controller not available.\n"); |
| 215 | continue; |
| 216 | } |
| 217 | |
| 218 | if (ret) { /* Other error. */ |
| 219 | printf("W1 controller probe failed.\n"); |
| 220 | continue; |
| 221 | } |
| 222 | } |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | UCLASS_DRIVER(w1) = { |
| 227 | .name = "w1", |
| 228 | .id = UCLASS_W1, |
| 229 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 230 | .per_device_auto_alloc_size = sizeof(struct w1_bus), |
| 231 | .post_probe = w1_post_probe, |
| 232 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
| 233 | .post_bind = dm_scan_fdt_dev, |
| 234 | #endif |
| 235 | .per_child_platdata_auto_alloc_size = sizeof(struct w1_device), |
| 236 | }; |