Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
| 4 | * |
| 5 | * (C) Copyright 2012 |
| 6 | * Pavel Herrmann <morpheus.ibis@gmail.com> |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Simon Glass | 82a7697 | 2020-07-19 10:15:36 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 12 | #include <dm-demo.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 14 | #include <asm/io.h> |
| 15 | |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 16 | struct udevice *demo_dev; |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 17 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 18 | static int do_demo_hello(struct cmd_tbl *cmdtp, int flag, int argc, |
| 19 | char *const argv[]) |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 20 | { |
| 21 | int ch = 0; |
| 22 | |
| 23 | if (argc) |
| 24 | ch = *argv[0]; |
| 25 | |
| 26 | return demo_hello(demo_dev, ch); |
| 27 | } |
| 28 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 29 | static int do_demo_status(struct cmd_tbl *cmdtp, int flag, int argc, |
| 30 | char *const argv[]) |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 31 | { |
| 32 | int status; |
| 33 | int ret; |
| 34 | |
| 35 | ret = demo_status(demo_dev, &status); |
| 36 | if (ret) |
| 37 | return ret; |
| 38 | |
| 39 | printf("Status: %d\n", status); |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 44 | static int do_demo_light(struct cmd_tbl *cmdtp, int flag, int argc, |
| 45 | char *const argv[]) |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 46 | { |
| 47 | int light; |
| 48 | int ret; |
| 49 | |
| 50 | if (argc) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 51 | light = hextoul(argv[0], NULL); |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 52 | ret = demo_set_light(demo_dev, light); |
| 53 | } else { |
| 54 | ret = demo_get_light(demo_dev); |
| 55 | if (ret >= 0) { |
| 56 | printf("Light: %x\n", ret); |
| 57 | ret = 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 64 | int do_demo_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 65 | { |
Heiko Schocher | 54c5d08 | 2014-05-22 12:43:05 +0200 | [diff] [blame] | 66 | struct udevice *dev; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 67 | int i, ret, err = 0; |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 68 | |
| 69 | puts("Demo uclass entries:\n"); |
| 70 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 71 | for (i = 0, ret = uclass_first_device_check(UCLASS_DEMO, &dev); |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 72 | dev; |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 73 | ret = uclass_next_device_check(&dev)) { |
| 74 | printf("entry %d - instance %08x, ops %08x, plat %08x, status %i\n", |
Mario Six | c6b89f3 | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 75 | i++, (uint)map_to_sysmem(dev), |
| 76 | (uint)map_to_sysmem(dev->driver->ops), |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 77 | (uint)map_to_sysmem(dev_get_plat(dev)), |
| 78 | ret); |
| 79 | if (ret) |
| 80 | err = ret; |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Michal Suchanek | 8676ae3 | 2022-10-12 21:57:58 +0200 | [diff] [blame] | 83 | return cmd_process_error(cmdtp, err); |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 86 | static struct cmd_tbl demo_commands[] = { |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 87 | U_BOOT_CMD_MKENT(list, 0, 1, do_demo_list, "", ""), |
| 88 | U_BOOT_CMD_MKENT(hello, 2, 1, do_demo_hello, "", ""), |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 89 | U_BOOT_CMD_MKENT(light, 2, 1, do_demo_light, "", ""), |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 90 | U_BOOT_CMD_MKENT(status, 1, 1, do_demo_status, "", ""), |
| 91 | }; |
| 92 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 93 | static int do_demo(struct cmd_tbl *cmdtp, int flag, int argc, |
| 94 | char *const argv[]) |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 95 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 96 | struct cmd_tbl *demo_cmd; |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 97 | int devnum = 0; |
| 98 | int ret; |
| 99 | |
| 100 | if (argc < 2) |
| 101 | return CMD_RET_USAGE; |
| 102 | demo_cmd = find_cmd_tbl(argv[1], demo_commands, |
| 103 | ARRAY_SIZE(demo_commands)); |
| 104 | argc -= 2; |
| 105 | argv += 2; |
Peter Tyser | b922a5f | 2015-02-03 13:18:49 -0600 | [diff] [blame] | 106 | |
| 107 | if ((!demo_cmd || argc > demo_cmd->maxargs) || |
| 108 | ((demo_cmd->name[0] != 'l') && (argc < 1))) |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 109 | return CMD_RET_USAGE; |
| 110 | |
| 111 | if (argc) { |
Simon Glass | 0b1284e | 2021-07-24 09:03:30 -0600 | [diff] [blame] | 112 | devnum = dectoul(argv[0], NULL); |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 113 | ret = uclass_get_device(UCLASS_DEMO, devnum, &demo_dev); |
| 114 | if (ret) |
| 115 | return cmd_process_error(cmdtp, ret); |
| 116 | argc--; |
| 117 | argv++; |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 118 | } else { |
| 119 | demo_dev = NULL; |
| 120 | if (demo_cmd->cmd != do_demo_list) |
| 121 | return CMD_RET_USAGE; |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | ret = demo_cmd->cmd(demo_cmd, flag, argc, argv); |
| 125 | |
| 126 | return cmd_process_error(demo_cmd, ret); |
| 127 | } |
| 128 | |
| 129 | U_BOOT_CMD( |
| 130 | demo, 4, 1, do_demo, |
| 131 | "Driver model (dm) demo operations", |
| 132 | "list List available demo devices\n" |
| 133 | "demo hello <num> [<char>] Say hello\n" |
Simon Glass | a02af4a | 2015-01-05 20:05:31 -0700 | [diff] [blame] | 134 | "demo light [<num>] Set or get the lights\n" |
| 135 | "demo status <num> Get demo device status\n" |
Simon Glass | 39f7611 | 2014-02-26 15:59:23 -0700 | [diff] [blame] | 136 | ); |