Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 1 | /* |
| 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 Glass | ae48d20 | 2014-07-23 06:55:10 -0600 | [diff] [blame^] | 19 | /** |
| 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 | */ |
| 27 | static 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 Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 35 | static int display_succ(struct udevice *in, char *buf) |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 36 | { |
| 37 | int len; |
| 38 | int ip = 0; |
| 39 | char local[16]; |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 40 | struct udevice *pos, *n, *prev = NULL; |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 41 | |
Simon Glass | ae48d20 | 2014-07-23 06:55:10 -0600 | [diff] [blame^] | 42 | dm_display_line(in, buf); |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 43 | |
| 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 Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 65 | static int dm_dump(struct udevice *dev) |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 66 | { |
| 67 | if (!dev) |
| 68 | return -EINVAL; |
| 69 | return display_succ(dev, ""); |
| 70 | } |
| 71 | |
| 72 | static int do_dm_dump_all(cmd_tbl_t *cmdtp, int flag, int argc, |
| 73 | char * const argv[]) |
| 74 | { |
Heiko Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 75 | struct udevice *root; |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 76 | |
| 77 | root = dm_root(); |
Simon Glass | 39f98ba | 2014-06-11 23:29:51 -0600 | [diff] [blame] | 78 | printf("ROOT %08lx\n", (ulong)map_to_sysmem(root)); |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 79 | return dm_dump(root); |
| 80 | } |
| 81 | |
| 82 | static 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 Schocher | b74fcb4 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 90 | struct udevice *dev; |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 91 | |
| 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 Glass | ae48d20 | 2014-07-23 06:55:10 -0600 | [diff] [blame^] | 100 | dm_display_line(dev, ""); |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 101 | } |
| 102 | puts("\n"); |
| 103 | } |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Simon Glass | 17e9c78 | 2014-06-11 23:29:50 -0600 | [diff] [blame] | 108 | #ifdef CONFIG_DM_TEST |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 109 | static int do_dm_test(cmd_tbl_t *cmdtp, int flag, int argc, |
| 110 | char * const argv[]) |
| 111 | { |
| 112 | return dm_test_main(); |
| 113 | } |
Simon Glass | 17e9c78 | 2014-06-11 23:29:50 -0600 | [diff] [blame] | 114 | #define TEST_HELP "\ndm test Run tests" |
| 115 | #else |
| 116 | #define TEST_HELP |
| 117 | #endif |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 118 | |
| 119 | static 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 Glass | 17e9c78 | 2014-06-11 23:29:50 -0600 | [diff] [blame] | 122 | #ifdef CONFIG_DM_TEST |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 123 | U_BOOT_CMD_MKENT(test, 1, 1, do_dm_test, "", ""), |
Simon Glass | 17e9c78 | 2014-06-11 23:29:50 -0600 | [diff] [blame] | 124 | #endif |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static 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 | |
| 146 | U_BOOT_CMD( |
| 147 | dm, 2, 1, do_dm, |
| 148 | "Driver model low level access", |
Simon Glass | ae48d20 | 2014-07-23 06:55:10 -0600 | [diff] [blame^] | 149 | "tree Dump driver model tree ('*' = activated)\n" |
Simon Glass | 17e9c78 | 2014-06-11 23:29:50 -0600 | [diff] [blame] | 150 | "dm uclass Dump list of instances for each uclass" |
| 151 | TEST_HELP |
Simon Glass | c267cf7 | 2014-02-26 15:59:22 -0700 | [diff] [blame] | 152 | ); |