1 /* SPDX-License-Identifier: GPL-2.0+
4 * Microchip Technology, Inc.
5 * Eugen Hristev <eugen.hristev@microchip.com>
10 #include <w1-eeprom.h>
11 #include <dm/device-internal.h>
13 static int w1_bus(void)
15 struct udevice *bus, *dev;
18 ret = w1_get_bus(0, &bus);
20 printf("one wire interface not found\n");
21 return CMD_RET_FAILURE;
23 printf("Bus %d:\t%s", bus->seq, bus->name);
24 if (device_active(bus))
28 for (device_find_first_child(bus, &dev);
30 device_find_next_child(&dev)) {
31 ret = device_probe(dev);
33 printf("\t%s (%d) uclass %s : ", dev->name, dev->seq,
34 dev->uclass->uc_drv->name);
37 printf("device error\n");
39 printf("family 0x%x\n", w1_get_device_family(dev));
41 return CMD_RET_SUCCESS;
44 static int w1_read(int argc, char *const argv[])
46 int bus_n = 0, dev_n = 0, offset = 0, len = 512;
48 struct udevice *bus, *dev;
53 bus_n = simple_strtoul(argv[2], NULL, 10);
56 dev_n = simple_strtoul(argv[3], NULL, 10);
59 offset = simple_strtoul(argv[4], NULL, 10);
62 len = simple_strtoul(argv[5], NULL, 10);
65 printf("len needs to be <= 512\n");
66 return CMD_RET_FAILURE;
69 ret = w1_get_bus(bus_n, &bus);
71 printf("one wire interface not found\n");
72 return CMD_RET_FAILURE;
75 for (device_find_first_child(bus, &dev), i = 0;
77 device_find_next_child(&dev), i++) {
78 ret = device_probe(dev);
79 if (!ret && i == dev_n)
83 if (i != dev_n || ret || !dev) {
84 printf("invalid dev\n");
85 return CMD_RET_FAILURE;
88 if (strcmp(dev->uclass->uc_drv->name, "w1_eeprom")) {
89 printf("the device present on the interface is of unknown device class\n");
90 return CMD_RET_FAILURE;
93 ret = w1_eeprom_read_buf(dev, offset, (u8 *)buf, len);
95 printf("error reading device %s\n", dev->name);
96 return CMD_RET_FAILURE;
99 for (i = 0; i < len; i++)
100 printf("%x", buf[i]);
103 return CMD_RET_SUCCESS;
106 int do_w1(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
109 return CMD_RET_USAGE;
111 if (!strcmp(argv[1], "bus"))
114 if (!strcmp(argv[1], "read"))
115 return w1_read(argc, argv);
117 return CMD_RET_SUCCESS;
120 U_BOOT_CMD(w1, 6, 0, do_w1,
121 "onewire interface utility commands",
122 "bus - show onewire bus info (all)\n"
123 "w1 read [<bus> [<dev> [offset [length]]]]"
124 " - read from onewire device 'dev' on onewire bus 'bus'"
125 " starting from offset 'offset' and length 'length'\n"
126 " defaults: bus 0, dev 0, offset 0, length 512 bytes.");