blob: 9b77a7fb920f9d85fc3a9a931045d5bf57054b98 [file] [log] [blame]
Simon Glassc267cf72014-02-26 15:59:22 -07001/*
2 * Copyright (c) 2013 Google, Inc
3 *
4 * (C) Copyright 2012
5 * Marek Vasut <marex@denx.de>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <malloc.h>
13#include <errno.h>
14#include <asm/io.h>
15#include <dm/root.h>
16#include <dm/test.h>
17#include <dm/uclass-internal.h>
18
Simon Glassae48d202014-07-23 06:55:10 -060019/**
20 * dm_display_line() - Display information about a single device
21 *
22 * Displays a single line of information with an option prefix
23 *
24 * @dev: Device to display
25 * @buf: Prefix to display at the start of the line
26 */
27static void dm_display_line(struct udevice *dev, char *buf)
28{
29 printf("%s- %c %s @ %08lx", buf,
30 dev->flags & DM_FLAG_ACTIVATED ? '*' : ' ',
31 dev->name, (ulong)map_to_sysmem(dev));
32 puts("\n");
33}
34
Heiko Schocherb74fcb42014-05-22 12:43:05 +020035static int display_succ(struct udevice *in, char *buf)
Simon Glassc267cf72014-02-26 15:59:22 -070036{
37 int len;
38 int ip = 0;
39 char local[16];
Heiko Schocherb74fcb42014-05-22 12:43:05 +020040 struct udevice *pos, *n, *prev = NULL;
Simon Glassc267cf72014-02-26 15:59:22 -070041
Simon Glassae48d202014-07-23 06:55:10 -060042 dm_display_line(in, buf);
Simon Glassc267cf72014-02-26 15:59:22 -070043
44 if (list_empty(&in->child_head))
45 return 0;
46
47 len = strlen(buf);
48 strncpy(local, buf, sizeof(local));
49 snprintf(local + len, 2, "|");
50 if (len && local[len - 1] == '`')
51 local[len - 1] = ' ';
52
53 list_for_each_entry_safe(pos, n, &in->child_head, sibling_node) {
54 if (ip++)
55 display_succ(prev, local);
56 prev = pos;
57 }
58
59 snprintf(local + len, 2, "`");
60 display_succ(prev, local);
61
62 return 0;
63}
64
Heiko Schocherb74fcb42014-05-22 12:43:05 +020065static int dm_dump(struct udevice *dev)
Simon Glassc267cf72014-02-26 15:59:22 -070066{
67 if (!dev)
68 return -EINVAL;
69 return display_succ(dev, "");
70}
71
72static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc,
73 char * const argv[])
74{
Heiko Schocherb74fcb42014-05-22 12:43:05 +020075 struct udevice *root;
Simon Glassc267cf72014-02-26 15:59:22 -070076
77 root = dm_root();
Simon Glass39f98ba2014-06-11 23:29:51 -060078 printf("ROOT %08lx\n", (ulong)map_to_sysmem(root));
Simon Glassc267cf72014-02-26 15:59:22 -070079 return dm_dump(root);
80}
81
82static int do_dm_dump_uclass(cmd_tbl_t *cmdtp, int flag, int argc,
83 char * const argv[])
84{
85 struct uclass *uc;
86 int ret;
87 int id;
88
89 for (id = 0; id < UCLASS_COUNT; id++) {
Heiko Schocherb74fcb42014-05-22 12:43:05 +020090 struct udevice *dev;
Simon Glassc267cf72014-02-26 15:59:22 -070091
92 ret = uclass_get(id, &uc);
93 if (ret)
94 continue;
95
96 printf("uclass %d: %s\n", id, uc->uc_drv->name);
97 for (ret = uclass_first_device(id, &dev);
98 dev;
99 ret = uclass_next_device(&dev)) {
Simon Glassae48d202014-07-23 06:55:10 -0600100 dm_display_line(dev, "");
Simon Glassc267cf72014-02-26 15:59:22 -0700101 }
102 puts("\n");
103 }
104
105 return 0;
106}
107
Simon Glass17e9c782014-06-11 23:29:50 -0600108#ifdef CONFIG_DM_TEST
Simon Glassc267cf72014-02-26 15:59:22 -0700109static int do_dm_test(cmd_tbl_t *cmdtp, int flag, int argc,
110 char * const argv[])
111{
112 return dm_test_main();
113}
Simon Glass17e9c782014-06-11 23:29:50 -0600114#define TEST_HELP "\ndm test Run tests"
115#else
116#define TEST_HELP
117#endif
Simon Glassc267cf72014-02-26 15:59:22 -0700118
119static cmd_tbl_t test_commands[] = {
120 U_BOOT_CMD_MKENT(tree, 0, 1, do_dm_dump_all, "", ""),
121 U_BOOT_CMD_MKENT(uclass, 1, 1, do_dm_dump_uclass, "", ""),
Simon Glass17e9c782014-06-11 23:29:50 -0600122#ifdef CONFIG_DM_TEST
Simon Glassc267cf72014-02-26 15:59:22 -0700123 U_BOOT_CMD_MKENT(test, 1, 1, do_dm_test, "", ""),
Simon Glass17e9c782014-06-11 23:29:50 -0600124#endif
Simon Glassc267cf72014-02-26 15:59:22 -0700125};
126
127static int do_dm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
128{
129 cmd_tbl_t *test_cmd;
130 int ret;
131
132 if (argc != 2)
133 return CMD_RET_USAGE;
134 test_cmd = find_cmd_tbl(argv[1], test_commands,
135 ARRAY_SIZE(test_commands));
136 argc -= 2;
137 argv += 2;
138 if (!test_cmd || argc > test_cmd->maxargs)
139 return CMD_RET_USAGE;
140
141 ret = test_cmd->cmd(test_cmd, flag, argc, argv);
142
143 return cmd_process_error(test_cmd, ret);
144}
145
146U_BOOT_CMD(
147 dm, 2, 1, do_dm,
148 "Driver model low level access",
Simon Glassae48d202014-07-23 06:55:10 -0600149 "tree Dump driver model tree ('*' = activated)\n"
Simon Glass17e9c782014-06-11 23:29:50 -0600150 "dm uclass Dump list of instances for each uclass"
151 TEST_HELP
Simon Glassc267cf72014-02-26 15:59:22 -0700152);