Commit 93f9dcf9e8b8182e97aeb7965c687176cbd0b933

Authored by Anton Vorontsov
Committed by Wolfgang Denk
1 parent cd21a458da

Add simple hwconfig infrastructure

This patch implements simple hwconfig infrastructure: an
interface for software knobs to control a hardware.

This is very simple implementation, i.e. it is implemented
via `hwconfig' environment variable. Later we could write
some "hwconfig <enable|disable|list>" commands, ncurses
interface for Award BIOS-like interface, and frame-buffer
interface for AMI GUI[1] BIOS-like interface with mouse
support[2].

Current implementation details/limitations:

1. Doesn't support options dependencies and mutual exclusion.
   We can implement this by integrating apt-get[3] into the
   u-boot. But I didn't bother yet.

2. Since we don't implement hwconfig command, i.e. we're working
   with the environement directly, there is no way to tell that
   toggling a particular option will need a reboot to take
   an effect. So, for now it's advised to always reboot the
   target after modifying hwconfig variable.

3. We support hwconfig options with arguments. For example,

   set hwconfig dr_usb:mode=peripheral,phy_type=ulpi

   That means:
   - dr_usb - enable Dual-Role USB controller;
   - dr_usb:mode=peripheral - USB in Function mode;
   - dr_usb:phy_type=ulpi - USB should work with ULPI PHYs;

The purpose of this simple implementation is to define some
internal API and then we can continue improving user experience
by adding more mature interface, like hwconfig command with
bells and whistles. Or not adding, if we feel that current
interface fits its needs.

[1] http://en.wikipedia.org/wiki/American_Megatrends
[2] Regarding ncurses and GUI with mouse support -- I'm just
    kidding.
[3] The comment regarding apt-get is also a joke, meaning that
    dependency tracking could be non-trivial. For example, for
    enabling HW feature X we may need to disable Y, and turn Z
    into reduced mode (like RMII-only interface for ethernet,
    no MII).

    It's quite trivial to implement simple cases though.

Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com>
Acked-by: Kim Phillips <kim.phillips@freescale.com>

Showing 3 changed files with 280 additions and 0 deletions Side-by-side Diff

... ... @@ -151,6 +151,7 @@
151 151 # others
152 152 COBJS-$(CONFIG_DDR_SPD) += ddr_spd.o
153 153 COBJS-$(CONFIG_CMD_DOC) += docecc.o
  154 +COBJS-$(CONFIG_HWCONFIG) += hwconfig.o
154 155 COBJS-$(CONFIG_CONSOLE_MUX) += iomux.o
155 156 COBJS-y += flash.o
156 157 COBJS-$(CONFIG_CMD_KGDB) += kgdb.o
  1 +/*
  2 + * An inteface for configuring a hardware via u-boot environment.
  3 + *
  4 + * Copyright (c) 2009 MontaVista Software, Inc.
  5 + *
  6 + * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7 + *
  8 + * This program is free software; you can redistribute it and/or
  9 + * modify it under the terms of the GNU General Public License as
  10 + * published by the Free Software Foundation; either version 2 of
  11 + * the License, or (at your option) any later version.
  12 + */
  13 +
  14 +#include <config.h>
  15 +#include <common.h>
  16 +#include <exports.h>
  17 +#include <hwconfig.h>
  18 +#include <linux/types.h>
  19 +#include <linux/string.h>
  20 +
  21 +static const char *hwconfig_parse(const char *opts, size_t maxlen,
  22 + const char *opt, char stopch, char eqch,
  23 + size_t *arglen)
  24 +{
  25 + size_t optlen = strlen(opt);
  26 + char *str;
  27 + const char *start = opts;
  28 + const char *end;
  29 +
  30 +next:
  31 + str = strstr(opts, opt);
  32 + end = str + optlen;
  33 + if (end - start > maxlen)
  34 + return NULL;
  35 +
  36 + if (str && (str == opts || str[-1] == stopch) &&
  37 + (*end == stopch || *end == eqch || *end == '\0')) {
  38 + const char *arg_end;
  39 +
  40 + if (!arglen)
  41 + return str;
  42 +
  43 + if (*end != eqch)
  44 + return NULL;
  45 +
  46 + arg_end = strchr(str, stopch);
  47 + if (!arg_end)
  48 + *arglen = min(maxlen, strlen(str)) - optlen - 1;
  49 + else
  50 + *arglen = arg_end - end - 1;
  51 +
  52 + return end + 1;
  53 + } else if (str) {
  54 + opts = end;
  55 + goto next;
  56 + }
  57 + return NULL;
  58 +}
  59 +
  60 +const char *cpu_hwconfig __attribute__((weak));
  61 +const char *board_hwconfig __attribute__((weak));
  62 +
  63 +static const char *__hwconfig(const char *opt, size_t *arglen)
  64 +{
  65 + const char *env_hwconfig = getenv("hwconfig");
  66 +
  67 + if (env_hwconfig)
  68 + return hwconfig_parse(env_hwconfig, strlen(env_hwconfig),
  69 + opt, ';', ':', arglen);
  70 +
  71 + if (board_hwconfig)
  72 + return hwconfig_parse(board_hwconfig, strlen(board_hwconfig),
  73 + opt, ';', ':', arglen);
  74 +
  75 + if (cpu_hwconfig)
  76 + return hwconfig_parse(cpu_hwconfig, strlen(cpu_hwconfig),
  77 + opt, ';', ':', arglen);
  78 +
  79 + return NULL;
  80 +}
  81 +
  82 +/*
  83 + * hwconfig - query if a particular hwconfig option is specified
  84 + * @opt: a string representing an option
  85 + *
  86 + * This call can be used to find out whether U-Boot should configure
  87 + * a particular hardware option.
  88 + *
  89 + * Returns non-zero value if the hardware option can be used and thus
  90 + * should be configured, 0 otherwise.
  91 + *
  92 + * This function also returns non-zero value if CONFIG_HWCONFIG is
  93 + * undefined.
  94 + *
  95 + * Returning non-zero value without CONFIG_HWCONFIG has its crucial
  96 + * purpose: the hwconfig() call should be a "transparent" interface,
  97 + * e.g. if a board doesn't need hwconfig facility, then we assume
  98 + * that the board file only calls things that are actually used, so
  99 + * hwconfig() will always return true result.
  100 + */
  101 +int hwconfig(const char *opt)
  102 +{
  103 + return !!__hwconfig(opt, NULL);
  104 +}
  105 +
  106 +/*
  107 + * hwconfig_arg - get hwconfig option's argument
  108 + * @opt: a string representing an option
  109 + * @arglen: a pointer to an allocated size_t variable
  110 + *
  111 + * Unlike hwconfig() function, this function returns a pointer to the
  112 + * start of the hwconfig arguments, if option is not found or it has
  113 + * no specified arguments, the function returns NULL pointer.
  114 + *
  115 + * If CONFIG_HWCONFIG is undefined, the function returns "", and
  116 + * arglen is set to 0.
  117 + */
  118 +const char *hwconfig_arg(const char *opt, size_t *arglen)
  119 +{
  120 + return __hwconfig(opt, arglen);
  121 +}
  122 +
  123 +/*
  124 + * hwconfig_arg_cmp - compare hwconfig option's argument
  125 + * @opt: a string representing an option
  126 + * @arg: a string for comparing an option's argument
  127 + *
  128 + * This call is similar to hwconfig_arg, but instead of returning
  129 + * hwconfig argument and its length, it is comparing it to @arg.
  130 + *
  131 + * Returns non-zero value if @arg matches, 0 otherwise.
  132 + *
  133 + * If CONFIG_HWCONFIG is undefined, the function returns a non-zero
  134 + * value, i.e. the argument matches.
  135 + */
  136 +int hwconfig_arg_cmp(const char *opt, const char *arg)
  137 +{
  138 + const char *argstr;
  139 + size_t arglen;
  140 +
  141 + argstr = hwconfig_arg(opt, &arglen);
  142 + if (!argstr || arglen != strlen(arg))
  143 + return 0;
  144 +
  145 + return !strncmp(argstr, arg, arglen);
  146 +}
  147 +
  148 +/*
  149 + * hwconfig_sub - query if a particular hwconfig sub-option is specified
  150 + * @opt: a string representing an option
  151 + * @subopt: a string representing a sub-option
  152 + *
  153 + * This call is similar to hwconfig(), except that it takes additional
  154 + * argument @subopt. In this example:
  155 + * "dr_usb:mode=peripheral"
  156 + * "dr_usb" is an option, "mode" is a sub-option, and "peripheral" is its
  157 + * argument.
  158 + */
  159 +int hwconfig_sub(const char *opt, const char *subopt)
  160 +{
  161 + size_t arglen;
  162 + const char *arg;
  163 +
  164 + arg = __hwconfig(opt, &arglen);
  165 + if (!arg)
  166 + return 0;
  167 + return !!hwconfig_parse(arg, arglen, subopt, ',', '=', NULL);
  168 +}
  169 +
  170 +/*
  171 + * hwconfig_subarg - get hwconfig sub-option's argument
  172 + * @opt: a string representing an option
  173 + * @subopt: a string representing a sub-option
  174 + * @subarglen: a pointer to an allocated size_t variable
  175 + *
  176 + * This call is similar to hwconfig_arg(), except that it takes an additional
  177 + * argument @subopt, and so works with sub-options.
  178 + */
  179 +const char *hwconfig_subarg(const char *opt, const char *subopt,
  180 + size_t *subarglen)
  181 +{
  182 + size_t arglen;
  183 + const char *arg;
  184 +
  185 + arg = __hwconfig(opt, &arglen);
  186 + if (!arg)
  187 + return NULL;
  188 + return hwconfig_parse(arg, arglen, subopt, ',', '=', subarglen);
  189 +}
  190 +
  191 +/*
  192 + * hwconfig_arg_cmp - compare hwconfig sub-option's argument
  193 + * @opt: a string representing an option
  194 + * @subopt: a string representing a sub-option
  195 + * @subarg: a string for comparing an sub-option's argument
  196 + *
  197 + * This call is similar to hwconfig_arg_cmp, except that it takes an additional
  198 + * argument @subopt, and so works with sub-options.
  199 + */
  200 +int hwconfig_subarg_cmp(const char *opt, const char *subopt, const char *subarg)
  201 +{
  202 + const char *argstr;
  203 + size_t arglen;
  204 +
  205 + argstr = hwconfig_subarg(opt, subopt, &arglen);
  206 + if (!argstr || arglen != strlen(subarg))
  207 + return 0;
  208 +
  209 + return !strncmp(argstr, subarg, arglen);
  210 +}
  1 +/*
  2 + * An inteface for configuring a hardware via u-boot environment.
  3 + *
  4 + * Copyright (c) 2009 MontaVista Software, Inc.
  5 + *
  6 + * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7 + *
  8 + * This program is free software; you can redistribute it and/or
  9 + * modify it under the terms of the GNU General Public License as
  10 + * published by the Free Software Foundation; either version 2 of
  11 + * the License, or (at your option) any later version.
  12 + */
  13 +
  14 +#ifndef _HWCONFIG_H
  15 +#define _HWCONFIG_H
  16 +
  17 +#include <linux/types.h>
  18 +#include <asm/errno.h>
  19 +
  20 +#ifdef CONFIG_HWCONFIG
  21 +
  22 +extern int hwconfig(const char *opt);
  23 +extern const char *hwconfig_arg(const char *opt, size_t *arglen);
  24 +extern int hwconfig_arg_cmp(const char *opt, const char *arg);
  25 +extern int hwconfig_sub(const char *opt, const char *subopt);
  26 +extern const char *hwconfig_subarg(const char *opt, const char *subopt,
  27 + size_t *subarglen);
  28 +extern int hwconfig_subarg_cmp(const char *opt, const char *subopt,
  29 + const char *subarg);
  30 +
  31 +#else
  32 +
  33 +static inline int hwconfig(const char *opt)
  34 +{
  35 + return -ENOSYS;
  36 +}
  37 +
  38 +static inline const char *hwconfig_arg(const char *opt, size_t *arglen)
  39 +{
  40 + *arglen = 0;
  41 + return "";
  42 +}
  43 +
  44 +static inline int hwconfig_arg_cmp(const char *opt, const char *arg)
  45 +{
  46 + return -ENOSYS;
  47 +}
  48 +
  49 +static inline int hwconfig_sub(const char *opt, const char *subopt)
  50 +{
  51 + return -ENOSYS;
  52 +}
  53 +
  54 +static inline const char *hwconfig_subarg(const char *opt, const char *subopt,
  55 + size_t *subarglen)
  56 +{
  57 + *subarglen = 0;
  58 + return "";
  59 +}
  60 +
  61 +static inline int hwconfig_subarg_cmp(const char *opt, const char *subopt,
  62 + const char *subarg)
  63 +{
  64 + return -ENOSYS;
  65 +}
  66 +
  67 +#endif /* CONFIG_HWCONFIG */
  68 +
  69 +#endif /* _HWCONFIG_H */