rockchip: Remove ARCH= references from documentation
[oweals/u-boot.git] / cmd / w1.c
1 /* SPDX-License-Identifier: GPL-2.0+
2  *
3  * (C) Copyright 2018
4  * Microchip Technology, Inc.
5  * Eugen Hristev <eugen.hristev@microchip.com>
6  */
7 #include <common.h>
8 #include <command.h>
9 #include <w1.h>
10 #include <w1-eeprom.h>
11 #include <dm/device-internal.h>
12
13 static int w1_bus(void)
14 {
15         struct udevice *bus, *dev;
16         int ret;
17
18         ret = w1_get_bus(0, &bus);
19         if (ret) {
20                 printf("one wire interface not found\n");
21                 return CMD_RET_FAILURE;
22         }
23         printf("Bus %d:\t%s", bus->seq, bus->name);
24         if (device_active(bus))
25                 printf("  (active)");
26         printf("\n");
27
28         for (device_find_first_child(bus, &dev);
29              dev;
30              device_find_next_child(&dev)) {
31                 ret = device_probe(dev);
32
33                 printf("\t%s (%d) uclass %s : ", dev->name, dev->seq,
34                        dev->uclass->uc_drv->name);
35
36                 if (ret)
37                         printf("device error\n");
38                 else
39                         printf("family 0x%x\n", w1_get_device_family(dev));
40         }
41         return CMD_RET_SUCCESS;
42 }
43
44 static int w1_read(int argc, char *const argv[])
45 {
46         int bus_n = 0, dev_n = 0, offset = 0, len = 512;
47         int i;
48         struct udevice *bus, *dev;
49         int ret;
50         u8 buf[512];
51
52         if (argc > 2)
53                 bus_n = simple_strtoul(argv[2], NULL, 10);
54
55         if (argc > 3)
56                 dev_n = simple_strtoul(argv[3], NULL, 10);
57
58         if (argc > 4)
59                 offset = simple_strtoul(argv[4], NULL, 10);
60
61         if (argc > 5)
62                 len = simple_strtoul(argv[5], NULL, 10);
63
64         if (len > 512) {
65                 printf("len needs to be <= 512\n");
66                 return CMD_RET_FAILURE;
67         }
68
69         ret = w1_get_bus(bus_n, &bus);
70         if (ret) {
71                 printf("one wire interface not found\n");
72                 return CMD_RET_FAILURE;
73         }
74
75         for (device_find_first_child(bus, &dev), i = 0;
76            dev && i <= dev_n;
77            device_find_next_child(&dev), i++) {
78                 ret = device_probe(dev);
79                 if (!ret && i == dev_n)
80                         break;
81         }
82
83         if (i != dev_n || ret || !dev) {
84                 printf("invalid dev\n");
85                 return CMD_RET_FAILURE;
86         }
87
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;
91         }
92
93         ret = w1_eeprom_read_buf(dev, offset, (u8 *)buf, len);
94         if (ret) {
95                 printf("error reading device %s\n", dev->name);
96                 return CMD_RET_FAILURE;
97         }
98
99         for (i = 0; i < len; i++)
100                 printf("%x", buf[i]);
101         printf("\n");
102
103         return CMD_RET_SUCCESS;
104 }
105
106 int do_w1(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
107 {
108         if (argc < 2)
109                 return CMD_RET_USAGE;
110
111         if (!strcmp(argv[1], "bus"))
112                 return w1_bus();
113
114         if (!strcmp(argv[1], "read"))
115                 return w1_read(argc, argv);
116
117         return CMD_RET_SUCCESS;
118 }
119
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.");