1 /* SPDX-License-Identifier: GPL-2.0+
4 * Microchip Technology, Inc.
5 * Eugen Hristev <eugen.hristev@microchip.com>
11 #include <w1-eeprom.h>
12 #include <dm/device-internal.h>
14 static int w1_bus(void)
16 struct udevice *bus, *dev;
19 ret = w1_get_bus(0, &bus);
21 printf("one wire interface not found\n");
22 return CMD_RET_FAILURE;
24 printf("Bus %d:\t%s", dev_seq(bus), bus->name);
25 if (device_active(bus))
29 for (device_find_first_child(bus, &dev);
31 device_find_next_child(&dev)) {
32 ret = device_probe(dev);
34 printf("\t%s (%d) uclass %s : ", dev->name, dev_seq(dev),
35 dev->uclass->uc_drv->name);
38 printf("device error\n");
40 printf("family 0x%x\n", w1_get_device_family(dev));
42 return CMD_RET_SUCCESS;
45 static int w1_read(int argc, char *const argv[])
47 int bus_n = 0, dev_n = 0, offset = 0, len = 512;
49 struct udevice *bus, *dev;
54 bus_n = simple_strtoul(argv[2], NULL, 10);
57 dev_n = simple_strtoul(argv[3], NULL, 10);
60 offset = simple_strtoul(argv[4], NULL, 10);
63 len = simple_strtoul(argv[5], NULL, 10);
66 printf("len needs to be <= 512\n");
67 return CMD_RET_FAILURE;
70 ret = w1_get_bus(bus_n, &bus);
72 printf("one wire interface not found\n");
73 return CMD_RET_FAILURE;
76 for (device_find_first_child(bus, &dev), i = 0;
78 device_find_next_child(&dev), i++) {
79 ret = device_probe(dev);
80 if (!ret && i == dev_n)
84 if (i != dev_n || ret || !dev) {
85 printf("invalid dev\n");
86 return CMD_RET_FAILURE;
89 if (strcmp(dev->uclass->uc_drv->name, "w1_eeprom")) {
90 printf("the device present on the interface is of unknown device class\n");
91 return CMD_RET_FAILURE;
94 ret = w1_eeprom_read_buf(dev, offset, (u8 *)buf, len);
96 printf("error reading device %s\n", dev->name);
97 return CMD_RET_FAILURE;
100 for (i = 0; i < len; i++)
101 printf("%x", buf[i]);
104 return CMD_RET_SUCCESS;
107 int do_w1(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
110 return CMD_RET_USAGE;
112 if (!strcmp(argv[1], "bus"))
115 if (!strcmp(argv[1], "read"))
116 return w1_read(argc, argv);
118 return CMD_RET_SUCCESS;
121 U_BOOT_CMD(w1, 6, 0, do_w1,
122 "onewire interface utility commands",
123 "bus - show onewire bus info (all)\n"
124 "w1 read [<bus> [<dev> [offset [length]]]]"
125 " - read from onewire device 'dev' on onewire bus 'bus'"
126 " starting from offset 'offset' and length 'length'\n"
127 " defaults: bus 0, dev 0, offset 0, length 512 bytes.");