2 * Copyright (c) 2015 M. Warner Losh <imp@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice unmodified, this list of conditions, and the following
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
34 #include <sys/errno.h>
35 #include <sys/libkern.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/sysctl.h>
42 #include <dev/ow/ow.h>
43 #include <dev/ow/owll.h>
44 #include <dev/ow/own.h>
47 * lldev - link level device
48 * ndev - network / transport device (this module)
49 * pdev - presentation device (children of this module)
52 typedef int ow_enum_fn(device_t, device_t);
53 typedef int ow_found_fn(device_t, romid_t);
57 device_t dev; /* Newbus driver back pointer */
58 struct mtx mtx; /* bus mutex */
59 device_t owner; /* bus owner, if != NULL */
67 static int ow_acquire_bus(device_t ndev, device_t pdev, int how);
68 static void ow_release_bus(device_t ndev, device_t pdev);
70 #define OW_LOCK(_sc) mtx_lock(&(_sc)->mtx)
71 #define OW_UNLOCK(_sc) mtx_unlock(&(_sc)->mtx)
72 #define OW_LOCK_DESTROY(_sc) mtx_destroy(&_sc->mtx)
73 #define OW_ASSERT_LOCKED(_sc) mtx_assert(&_sc->mtx, MA_OWNED)
74 #define OW_ASSERT_UNLOCKED(_sc) mtx_assert(&_sc->mtx, MA_NOTOWNED)
76 static MALLOC_DEFINE(M_OW, "ow", "House keeping data for 1wire bus");
78 static const struct ow_timing timing_regular_min = {
84 .t_rdv = 15, /* fixed */
92 static const struct ow_timing timing_regular_max = {
97 .t_rec = 960, /* infinity */
98 .t_rdv = 15, /* fixed */
99 .t_rstl = 960, /* infinity */
100 .t_rsth = 960, /* infinity */
101 .t_pdl = 240, /* 60us to 240us */
102 .t_pdh = 60, /* 15us to 60us */
103 .t_lowr = 15, /* 1us */
106 static struct ow_timing timing_regular = {
107 .t_slot = 60, /* 60 <= t < 120 */
108 .t_low0 = 60, /* 60 <= t < t_slot < 120 */
109 .t_low1 = 1, /* 1 <= t < 15 */
110 .t_release = 45, /* 0 <= t < 45 */
111 .t_rec = 15, /* 1 <= t < inf */
112 .t_rdv = 15, /* t == 15 */
113 .t_rstl = 480, /* 480 <= t < inf */
114 .t_rsth = 480, /* 480 <= t < inf */
115 .t_pdl = 60, /* 60 <= t < 240 */
116 .t_pdh = 60, /* 15 <= t < 60 */
117 .t_lowr = 1, /* 1 <= t < 15 */
121 static const struct ow_timing timing_overdrive_min = {
127 .t_rdv = 2, /* fixed */
135 static const struct ow_timing timing_overdrive_max = {
140 .t_rec = 960, /* infinity */
141 .t_rdv = 2, /* fixed */
143 .t_rsth = 960, /* infinity */
149 static struct ow_timing timing_overdrive = {
150 .t_slot = 11, /* 6 <= t < 16 */
151 .t_low0 = 6, /* 6 <= t < t_slot < 16 */
152 .t_low1 = 1, /* 1 <= t < 2 */
153 .t_release = 4, /* 0 <= t < 4 */
154 .t_rec = 1, /* 1 <= t < inf */
155 .t_rdv = 2, /* t == 2 */
156 .t_rstl = 48, /* 48 <= t < 80 */
157 .t_rsth = 48, /* 48 <= t < inf */
158 .t_pdl = 8, /* 8 <= t < 24 */
159 .t_pdh = 2, /* 2 <= t < 6 */
160 .t_lowr = 1, /* 1 <= t < 2 */
163 SYSCTL_NODE(_hw, OID_AUTO, ow, CTLFLAG_RD, 0, "1-Wire protocol");
164 SYSCTL_NODE(_hw_ow, OID_AUTO, regular, CTLFLAG_RD, 0,
165 "Regular mode timings");
166 SYSCTL_NODE(_hw_ow, OID_AUTO, overdrive, CTLFLAG_RD, 0,
167 "Overdrive mode timings");
169 #define _OW_TIMING_SYSCTL(mode, param) \
171 sysctl_ow_timing_ ## mode ## _ ## param(SYSCTL_HANDLER_ARGS) \
173 int val = timing_ ## mode.param; \
175 err = sysctl_handle_int(oidp, &val, 0, req); \
176 if (err != 0 || req->newptr == NULL) \
178 if (val < timing_ ## mode ## _min.param) \
180 else if (val >= timing_ ## mode ## _max.param) \
182 timing_ ## mode.param = val; \
185 SYSCTL_PROC(_hw_ow_ ## mode, OID_AUTO, param, \
186 CTLTYPE_INT | CTLFLAG_RWTUN, 0, sizeof(int), \
187 sysctl_ow_timing_ ## mode ## _ ## param, "I", \
188 "1-Wire timing parameter in microseconds (-1 resets to default)")
190 #define OW_TIMING_SYSCTL(param) \
191 _OW_TIMING_SYSCTL(regular, param); \
192 _OW_TIMING_SYSCTL(overdrive, param)
194 OW_TIMING_SYSCTL(t_slot);
195 OW_TIMING_SYSCTL(t_low0);
196 OW_TIMING_SYSCTL(t_low1);
197 OW_TIMING_SYSCTL(t_release);
198 OW_TIMING_SYSCTL(t_rec);
199 OW_TIMING_SYSCTL(t_rdv);
200 OW_TIMING_SYSCTL(t_rstl);
201 OW_TIMING_SYSCTL(t_rsth);
202 OW_TIMING_SYSCTL(t_pdl);
203 OW_TIMING_SYSCTL(t_pdh);
204 OW_TIMING_SYSCTL(t_lowr);
206 #undef _OW_TIMING_SYSCTL
207 #undef OW_TIMING_SYSCTL
210 ow_send_byte(device_t lldev, struct ow_timing *t, uint8_t byte)
214 for (i = 0; i < 8; i++)
216 OWLL_WRITE_ONE(lldev, t);
218 OWLL_WRITE_ZERO(lldev, t);
222 ow_read_byte(device_t lldev, struct ow_timing *t, uint8_t *bytep)
228 for (i = 0; i < 8; i++) {
229 OWLL_READ_DATA(lldev, t, &bit);
236 ow_send_command(device_t ndev, device_t pdev, struct ow_cmd *cmd)
238 int present, i, bit, tries;
242 lldev = device_get_parent(ndev);
245 * Retry the reset a couple of times before giving up.
249 OWLL_RESET_AND_PRESENCE(lldev, &timing_regular, &present);
251 device_printf(ndev, "Reset said no device on bus?.\n");
252 } while (present == 1 && tries-- > 0);
254 device_printf(ndev, "Reset said the device wasn't there.\n");
255 return ENOENT; /* No devices acked the RESET */
258 device_printf(ndev, "Reset discovered bus wired wrong.\n");
262 for (i = 0; i < cmd->rom_len; i++)
263 ow_send_byte(lldev, &timing_regular, cmd->rom_cmd[i]);
264 for (i = 0; i < cmd->rom_read_len; i++)
265 ow_read_byte(lldev, &timing_regular, cmd->rom_read + i);
268 * Per AN937, the reset pulse and ROM level are always
269 * done with the regular timings. Certain ROM commands
270 * put the device into overdrive mode for the remainder
271 * of the data transfer, which is why we have to pass the
272 * timings here. Commands that need to be handled like this
273 * are expected to be flagged by the client.
275 t = (cmd->flags & OW_FLAG_OVERDRIVE) ?
276 &timing_overdrive : &timing_regular;
277 for (i = 0; i < cmd->xpt_len; i++)
278 ow_send_byte(lldev, t, cmd->xpt_cmd[i]);
279 if (cmd->flags & OW_FLAG_READ_BIT) {
280 memset(cmd->xpt_read, 0, (cmd->xpt_read_len + 7) / 8);
281 for (i = 0; i < cmd->xpt_read_len; i++) {
282 OWLL_READ_DATA(lldev, t, &bit);
283 cmd->xpt_read[i / 8] |= bit << (i % 8);
286 for (i = 0; i < cmd->xpt_read_len; i++)
287 ow_read_byte(lldev, t, cmd->xpt_read + i);
294 ow_search_rom(device_t lldev, device_t dev)
298 memset(&cmd, 0, sizeof(cmd));
299 cmd.rom_cmd[0] = SEARCH_ROM;
301 return ow_send_command(lldev, dev, &cmd);
306 ow_alarm_search(device_t lldev, device_t dev)
310 memset(&cmd, 0, sizeof(cmd));
311 cmd.rom_cmd[0] = ALARM_SEARCH;
313 return ow_send_command(lldev, dev, &cmd);
318 ow_add_child(device_t dev, romid_t romid)
320 struct ow_devinfo *di;
323 di = malloc(sizeof(*di), M_OW, M_WAITOK);
325 child = device_add_child(dev, NULL, -1);
330 device_set_ivars(child, di);
335 ow_child_by_romid(device_t dev, romid_t romid)
337 device_t *children, retval, child;
339 struct ow_devinfo *di;
341 if (device_get_children(dev, &children, &nkid) != 0)
344 for (i = 0; i < nkid; i++) {
346 di = device_get_ivars(child);
347 if (di->romid == romid) {
352 free(children, M_TEMP);
358 * CRC generator table -- taken from AN937 DOW CRC LOOKUP FUNCTION Table 2
360 const uint8_t ow_crc_table[] = {
361 0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
362 157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
363 35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
364 190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
365 70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
366 219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
367 101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
368 248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
369 140,210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113,147, 205,
370 17, 79, 173, 243, 112, 46, 204, 146, 211,141, 111, 49, 178, 236, 14, 80,
371 175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82,176, 238,
372 50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
373 202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
374 87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
375 233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
376 116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53
380 * Converted from DO_CRC page 131 ANN937
383 ow_crc(device_t ndev, device_t pdev, uint8_t *buffer, size_t len)
388 for (i = 0; i < len; i++)
389 crc = ow_crc_table[crc ^ buffer[i]];
394 ow_check_crc(romid_t romid)
396 return ow_crc(NULL, NULL, (uint8_t *)&romid, sizeof(romid)) == 0;
400 ow_device_found(device_t dev, romid_t romid)
403 /* XXX Move this up into enumerate? */
405 * All valid ROM IDs have a valid CRC. Check that first.
407 if (!ow_check_crc(romid)) {
408 device_printf(dev, "Device romid %8D failed CRC.\n",
414 * If we've seen this child before, don't add a new one for it.
416 if (ow_child_by_romid(dev, romid) != NULL)
419 return ow_add_child(dev, romid);
423 ow_enumerate(device_t dev, ow_enum_fn *enumfp, ow_found_fn *foundfp)
425 device_t lldev = device_get_parent(dev);
426 int first, second, i, dir, prior, last, err, retries;
427 uint64_t probed, last_mask;
434 err = ow_acquire_bus(dev, dev, OWN_DONTWAIT);
439 printf("Reached the sanity limit\n");
447 * See AN397 section 5.II.C.3 for the algorithm (though a bit
448 * poorly stated). The search command forces each device to
449 * send ROM ID bits one at a time (first the bit, then the
450 * complement) the master (us) sends back a bit. If the
451 * device's bit doesn't match what we send back, that device
452 * stops sending bits back. So each time through we remember
453 * where we made the last decision (always 0). If there's a
454 * conflict there this time (and there will be in the absence
455 * of a hardware failure) we go with 1. This way, we prune the
456 * devices on the bus and wind up with a unique ROM. We know
457 * we're done when we detect no new conflicts. The same
458 * algorithm is used for devices in alarm state as well.
460 * In addition, experience has shown that sometimes devices
461 * stop responding in the middle of enumeration, so try this
462 * step again a few times when that happens. It is unclear if
463 * this is due to a nosiy electrical environment or some odd
468 * The enumeration command should be successfully sent, if not,
469 * we have big issues on the bus so punt. Lower layers report
470 * any unusual errors, so we don't need to here.
472 err = enumfp(dev, dev);
476 for (i = 0; i < 64; i++) {
477 OWLL_READ_DATA(lldev, &timing_regular, &first);
478 OWLL_READ_DATA(lldev, &timing_regular, &second);
479 switch (first | second << 1) {
480 case 0: /* Conflict */
482 dir = (last_mask >> i) & 1;
489 case 1: /* 1 then 0 -> 1 for all */
492 case 2: /* 0 then 1 -> 0 for all */
497 * No device responded. This is unexpected, but
498 * experience has shown that on some platforms
499 * we miss a timing window, or otherwise have
500 * an issue. Start this step over. Since we've
501 * not updated prior yet, we can just jump to
502 * the top of the loop for a re-do of this step.
504 printf("oops, starting over\n");
508 default: /* NOTREACHED */
512 OWLL_WRITE_ONE(lldev, &timing_regular);
515 OWLL_WRITE_ZERO(lldev, &timing_regular);
519 foundfp(dev, probed);
523 ow_release_bus(dev, dev);
529 ow_probe(device_t dev)
532 device_set_desc(dev, "1 Wire Bus");
533 return (BUS_PROBE_GENERIC);
537 ow_attach(device_t ndev)
542 * Find all the devices on the bus. We don't probe / attach them in the
543 * enumeration phase. We do this because we want to allow the probe /
544 * attach routines of the child drivers to have as full an access to the
545 * bus as possible. While we reset things before the next step of the
546 * search (so it would likely be OK to allow access by the clients to
547 * the bus), it is more conservative to find them all, then to do the
548 * attach of the devices. This also allows the child devices to have
549 * more knowledge of the bus. We also ignore errors from the enumeration
550 * because they might happen after we've found a few devices.
552 sc = device_get_softc(ndev);
554 mtx_init(&sc->mtx, device_get_nameunit(sc->dev), "ow", MTX_DEF);
555 ow_enumerate(ndev, ow_search_rom, ow_device_found);
556 return bus_generic_attach(ndev);
560 ow_detach(device_t ndev)
562 device_t *children, child;
564 struct ow_devinfo *di;
567 sc = device_get_softc(ndev);
569 * detach all the children first. This is blocking until any threads
572 bus_generic_detach(ndev);
575 * We delete all the children, and free up the ivars
577 if (device_get_children(ndev, &children, &nkid) != 0)
579 for (i = 0; i < nkid; i++) {
581 di = device_get_ivars(child);
583 device_delete_child(ndev, child);
585 free(children, M_TEMP);
592 * Not sure this is really needed. I'm having trouble figuring out what
593 * location means in the context of the one wire bus.
596 ow_child_location_str(device_t dev, device_t child, char *buf,
605 ow_child_pnpinfo_str(device_t dev, device_t child, char *buf,
608 struct ow_devinfo *di;
610 di = device_get_ivars(child);
611 snprintf(buf, buflen, "romid=%8D", &di->romid, ":");
616 ow_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
618 struct ow_devinfo *di;
621 di = device_get_ivars(child);
624 *result = di->romid & 0xff;
627 ptr = (romid_t **)result;
638 ow_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
645 ow_print_child(device_t ndev, device_t pdev)
648 struct ow_devinfo *di;
650 di = device_get_ivars(pdev);
652 retval += bus_print_child_header(ndev, pdev);
653 retval += printf(" romid %8D", &di->romid, ":");
654 retval += bus_print_child_footer(ndev, pdev);
660 ow_probe_nomatch(device_t ndev, device_t pdev)
662 struct ow_devinfo *di;
664 di = device_get_ivars(pdev);
665 device_printf(ndev, "romid %8D: no driver\n", &di->romid, ":");
669 ow_acquire_bus(device_t ndev, device_t pdev, int how)
673 sc = device_get_softc(ndev);
674 OW_ASSERT_UNLOCKED(sc);
676 if (sc->owner != NULL) {
677 if (sc->owner == pdev)
678 panic("%s: %s recursively acquiring the bus.\n",
679 device_get_nameunit(ndev),
680 device_get_nameunit(pdev));
681 if (how == OWN_DONTWAIT) {
685 while (sc->owner != NULL)
686 mtx_sleep(sc, &sc->mtx, 0, "owbuswait", 0);
695 ow_release_bus(device_t ndev, device_t pdev)
699 sc = device_get_softc(ndev);
700 OW_ASSERT_UNLOCKED(sc);
702 if (sc->owner == NULL)
703 panic("%s: %s releasing unowned bus.", device_get_nameunit(ndev),
704 device_get_nameunit(pdev));
705 if (sc->owner != pdev)
706 panic("%s: %s don't own the bus. %s does. game over.",
707 device_get_nameunit(ndev), device_get_nameunit(pdev),
708 device_get_nameunit(sc->owner));
714 devclass_t ow_devclass;
716 static device_method_t ow_methods[] = {
717 /* Device interface */
718 DEVMETHOD(device_probe, ow_probe),
719 DEVMETHOD(device_attach, ow_attach),
720 DEVMETHOD(device_detach, ow_detach),
723 DEVMETHOD(bus_child_pnpinfo_str, ow_child_pnpinfo_str),
724 DEVMETHOD(bus_child_location_str, ow_child_location_str),
725 DEVMETHOD(bus_read_ivar, ow_read_ivar),
726 DEVMETHOD(bus_write_ivar, ow_write_ivar),
727 DEVMETHOD(bus_print_child, ow_print_child),
728 DEVMETHOD(bus_probe_nomatch, ow_probe_nomatch),
730 /* One Wire Network/Transport layer interface */
731 DEVMETHOD(own_send_command, ow_send_command),
732 DEVMETHOD(own_acquire_bus, ow_acquire_bus),
733 DEVMETHOD(own_release_bus, ow_release_bus),
734 DEVMETHOD(own_crc, ow_crc),
738 static driver_t ow_driver = {
741 sizeof(struct ow_softc),
744 DRIVER_MODULE(ow, owc, ow_driver, ow_devclass, 0, 0);
745 MODULE_VERSION(ow, 1);