Skip to contentSkip to conversation

i.MX6Q: mx6qsabrelite: Add keypress support to alter boot flow

PATCH Superseded Eric Nelson 2 participants 3 messages Apr 16, 2012 1 message not in the archive

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): http://lists.denx.de/pipermail/u-boot/2012-March/#119232

#1 lore raw

PATCH v2 1/1 i.MX6Q: mx6qsabrelite: Add keypress support to alter boot flow

Uses the 'magic_keys' idiom as described in doc/README.kbd:

	http://lists.denx.de/pipermail/u-boot/2012-April/122502.html

---
V2 based on suggestion by Wolfgang to follow prior implementations.

 board/freescale/mx6qsabrelite/mx6qsabrelite.c |  121 ++++++++++++++++++++++++-
 include/configs/mx6qsabrelite.h               |    3 +
 2 files changed, 122 insertions(+), 2 deletions(-)
board/freescale/mx6qsabrelite/mx6qsabrelite.c +119 -2
index 1d09a72..9ca46e7 100644
@@ -50,6 +50,10 @@ DECLARE_GLOBAL_DATA_PTR;
 	PAD_CTL_PUS_100K_DOWN | PAD_CTL_SPEED_MED |		\
 	PAD_CTL_DSE_40ohm     | PAD_CTL_SRE_FAST)
 
+#define BUTTON_PAD_CTRL (PAD_CTL_PKE | PAD_CTL_PUE |		\
+	PAD_CTL_PUS_100K_UP | PAD_CTL_SPEED_MED   |		\
+	PAD_CTL_DSE_40ohm   | PAD_CTL_HYS)
+
 int dram_init(void)
 {
        gd->ram_size = get_ram_size((void *)PHYS_SDRAM, PHYS_SDRAM_SIZE);
@@ -122,6 +126,22 @@ iomux_v3_cfg_t enet_pads2[] = {
 	MX6Q_PAD_RGMII_RX_CTL__RGMII_RX_CTL	| MUX_PAD_CTRL(ENET_PAD_CTRL),
 };
 
+/* Button assignments for J14 */
+static iomux_v3_cfg_t button_pads[] = {
+	/* Menu */
+	MX6Q_PAD_NANDF_D1__GPIO_2_1	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Back */
+	MX6Q_PAD_NANDF_D2__GPIO_2_2	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Labelled Search (mapped to Power under Android) */
+	MX6Q_PAD_NANDF_D3__GPIO_2_3	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Home */
+	MX6Q_PAD_NANDF_D4__GPIO_2_4	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Volume Down */
+	MX6Q_PAD_GPIO_19__GPIO_4_5	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Volume Up */
+	MX6Q_PAD_GPIO_18__GPIO_7_13	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+};
+
 static void setup_iomux_enet(void)
 {
 	gpio_direction_output(87, 0);  /* GPIO 3-23 */
@@ -267,11 +287,18 @@ int board_eth_init(bd_t *bis)
 	return 0;
 }
 
+static void setup_buttons(void)
+{
+	imx_iomux_v3_setup_multiple_pads(button_pads,
+					 ARRAY_SIZE(button_pads));
+}
+
 int board_early_init_f(void)
 {
-       setup_iomux_uart();
+	setup_iomux_uart();
+	setup_buttons();
 
-       return 0;
+	return 0;
 }
 
 int board_init(void)
@@ -292,3 +319,93 @@ int checkboard(void)
 
        return 0;
 }
+
+struct button_key {
+	char const	*name;
+	unsigned	gpnum;
+	char		ident;
+};
+
+static struct button_key const buttons[] = {
+	{"back",	GPIO_NUMBER(2, 2),	'B'},
+	{"home",	GPIO_NUMBER(2, 4),	'H'},
+	{"menu",	GPIO_NUMBER(2, 1),	'M'},
+	{"search",	GPIO_NUMBER(2, 3),	'S'},
+	{"volup",	GPIO_NUMBER(7, 13),	'V'},
+	{"voldown",	GPIO_NUMBER(4, 5),	'v'},
+};
+
+/*
+ * generate a null-terminated string containing the buttons pressed
+ * returns number of keys pressed
+ */
+static int read_keys(char *buf)
+{
+	int i, numpressed = 0;
+	for (i = 0; i < ARRAY_SIZE(buttons); i++) {
+		if (0 == gpio_get_value(buttons[i].gpnum))
+			buf[numpressed++] = buttons[i].ident;
+	}
+	buf[numpressed] = '\0' ;
+	return numpressed ;
+}
+
+static int do_kbd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	char envvalue[ARRAY_SIZE(buttons)+1];
+	int numpressed = read_keys(envvalue);
+	setenv("keybd", envvalue);
+	return 0 == numpressed ;
+}
+
+U_BOOT_CMD(
+	kbd, 1, 1, do_kbd,
+	"Tests for keypresses, sets 'keybd' environment variable",
+	"Returns 0 (true) to shell if key is pressed."
+);
+
+#ifdef CONFIG_PREBOOT
+static char const kbd_magic_prefix[] = "key_magic";
+static char const kbd_command_prefix[] = "key_cmd";
+
+static void preboot_keys(void)
+{
+	int i, numpressed ;
+	char keypress[ARRAY_SIZE(buttons)+1];
+	numpressed = read_keys(keypress);
+	if (0 != numpressed) {
+		char *kbd_magic_keys = getenv("magic_keys");
+		char *suffix;
+		/* loop over all magic keys;
+		 * use '\0' suffix in case of empty string
+		 */
+		for (suffix = kbd_magic_keys; *suffix ; ++suffix) {
+			char *keys ;
+			char magic[sizeof(kbd_magic_prefix) + 1];
+			sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
+			keys = getenv(magic);
+			if (0 != keys) {
+				if (0 == strcmp(keys, keypress))
+					break;
+			}
+		}
+		if (*suffix) {
+			char cmd_name[sizeof(kbd_command_prefix) + 1];
+			char *cmd;
+			sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
+			cmd = getenv(cmd_name);
+			if (cmd) {
+				setenv("preboot", cmd);
+				return ;
+			}
+		}
+	}
+}
+#endif
+
+int misc_init_r(void)
+{
+#ifdef CONFIG_PREBOOT
+	preboot_keys();
+#endif
+}
include/configs/mx6qsabrelite.h +3 -0
index 8bc8a83..7bf8479 100644
@@ -42,6 +42,7 @@
 
 #define CONFIG_ARCH_CPU_INIT
 #define CONFIG_BOARD_EARLY_INIT_F
+#define CONFIG_MISC_INIT_R
 #define CONFIG_MXC_GPIO
 
 #define CONFIG_MXC_UART
@@ -110,6 +111,8 @@
 
 #define CONFIG_BOOTDELAY               3
 
+#define CONFIG_PREBOOT                 ""
+
 #define CONFIG_LOADADDR                        0x10800000
 #define CONFIG_SYS_TEXT_BASE           0x17800000
 
Signature · 1 line
1.7.9
#2 lore raw

PATCH v2 1/1 i.MX6Q: mx6qsabrelite: Add keypress support to alter boot flow

Dear Eric Nelson,

Quoted 95 lines

Uses the 'magic_keys' idiom as described in doc/README.kbd:

	http://lists.denx.de/pipermail/u-boot/2012-April/122502.html

---
V2 based on suggestion by Wolfgang to follow prior implementations.

 board/freescale/mx6qsabrelite/mx6qsabrelite.c |  121
++++++++++++++++++++++++- include/configs/mx6qsabrelite.h               | 
  3 +
 2 files changed, 122 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/board/freescale/mx6qsabrelite/mx6qsabrelite.c index 1d09a72..9ca46e7 100644

board/freescale/mx6qsabrelite/mx6qsabrelite.c +48 -2
@@ -50,6 +50,10 @@ DECLARE_GLOBAL_DATA_PTR;
 	PAD_CTL_PUS_100K_DOWN | PAD_CTL_SPEED_MED |		\
 	PAD_CTL_DSE_40ohm     | PAD_CTL_SRE_FAST)
 
+#define BUTTON_PAD_CTRL (PAD_CTL_PKE | PAD_CTL_PUE |		\
+	PAD_CTL_PUS_100K_UP | PAD_CTL_SPEED_MED   |		\
+	PAD_CTL_DSE_40ohm   | PAD_CTL_HYS)
+
 int dram_init(void)
 {
        gd->ram_size = get_ram_size((void *)PHYS_SDRAM, PHYS_SDRAM_SIZE);
@@ -122,6 +126,22 @@ iomux_v3_cfg_t enet_pads2[] = {
 	MX6Q_PAD_RGMII_RX_CTL__RGMII_RX_CTL	| MUX_PAD_CTRL(ENET_PAD_CTRL),
 };
 
+/* Button assignments for J14 */
+static iomux_v3_cfg_t button_pads[] = {
+	/* Menu */
+	MX6Q_PAD_NANDF_D1__GPIO_2_1	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Back */
+	MX6Q_PAD_NANDF_D2__GPIO_2_2	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Labelled Search (mapped to Power under Android) */
+	MX6Q_PAD_NANDF_D3__GPIO_2_3	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Home */
+	MX6Q_PAD_NANDF_D4__GPIO_2_4	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Volume Down */
+	MX6Q_PAD_GPIO_19__GPIO_4_5	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+	/* Volume Up */
+	MX6Q_PAD_GPIO_18__GPIO_7_13	| MUX_PAD_CTRL(BUTTON_PAD_CTRL),
+};
+
 static void setup_iomux_enet(void)
 {
 	gpio_direction_output(87, 0);  /* GPIO 3-23 */
@@ -267,11 +287,18 @@ int board_eth_init(bd_t *bis)
 	return 0;
 }
 
+static void setup_buttons(void)
+{
+	imx_iomux_v3_setup_multiple_pads(button_pads,
+					 ARRAY_SIZE(button_pads));
+}
+
 int board_early_init_f(void)
 {
-       setup_iomux_uart();
+	setup_iomux_uart();
+	setup_buttons();
 
-       return 0;
+	return 0;
 }
 
 int board_init(void)
@@ -292,3 +319,93 @@ int checkboard(void)
 
        return 0;
 }
+
+struct button_key {
+	char const	*name;
+	unsigned	gpnum;
+	char		ident;
+};
+
+static struct button_key const buttons[] = {
+	{"back",	GPIO_NUMBER(2, 2),	'B'},
+	{"home",	GPIO_NUMBER(2, 4),	'H'},
+	{"menu",	GPIO_NUMBER(2, 1),	'M'},
+	{"search",	GPIO_NUMBER(2, 3),	'S'},
+	{"volup",	GPIO_NUMBER(7, 13),	'V'},
+	{"voldown",	GPIO_NUMBER(4, 5),	'v'},
+};
+
+/*
+ * generate a null-terminated string containing the buttons pressed
+ * returns number of keys pressed

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 350

Quoted 10 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +10 -0
+ */
+static int read_keys(char *buf)
+{
+	int i, numpressed = 0;
+	for (i = 0; i < ARRAY_SIZE(buttons); i++) {
+		if (0 == gpio_get_value(buttons[i].gpnum))
+			buf[numpressed++] = buttons[i].ident;
+	}
+	buf[numpressed] = '\0' ;
+	return numpressed ;

Delete the space before semicolon please.

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 358

Quoted 8 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +3 -0
+}
+
+static int do_kbd(cmd_tbl_t *cmdtp, int flag, int argc, char * const

argv[]) +{

board/freescale/mx6qsabrelite/mx6qsabrelite.c +4 -0
+	char envvalue[ARRAY_SIZE(buttons)+1];
+	int numpressed = read_keys(envvalue);
+	setenv("keybd", envvalue);
+	return 0 == numpressed ;

Drop yoda condition

Quoted 8 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +8 -0
+}
+
+U_BOOT_CMD(
+	kbd, 1, 1, do_kbd,
+	"Tests for keypresses, sets 'keybd' environment variable",
+	"Returns 0 (true) to shell if key is pressed."
+);
+

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 376

Quoted 10 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +10 -0
+#ifdef CONFIG_PREBOOT
+static char const kbd_magic_prefix[] = "key_magic";
+static char const kbd_command_prefix[] = "key_cmd";
+
+static void preboot_keys(void)
+{
+	int i, numpressed ;
+	char keypress[ARRAY_SIZE(buttons)+1];
+	numpressed = read_keys(keypress);
+	if (0 != numpressed) {

Yoda ...

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 379

Quoted 3 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +3 -0
+		char *kbd_magic_keys = getenv("magic_keys");
+		char *suffix;
+		/* loop over all magic keys;

Fix comment please?

Quoted 52 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +32 -0
+		 * use '\0' suffix in case of empty string
+		 */
+		for (suffix = kbd_magic_keys; *suffix ; ++suffix) {
+			char *keys ;
+			char magic[sizeof(kbd_magic_prefix) + 1];
+			sprintf(magic, "%s%c", kbd_magic_prefix, *suffix);
+			keys = getenv(magic);
+			if (0 != keys) {
+				if (0 == strcmp(keys, keypress))
+					break;
+			}
+		}
+		if (*suffix) {
+			char cmd_name[sizeof(kbd_command_prefix) + 1];
+			char *cmd;
+			sprintf(cmd_name, "%s%c", kbd_command_prefix, *suffix);
+			cmd = getenv(cmd_name);
+			if (cmd) {
+				setenv("preboot", cmd);
+				return ;
+			}
+		}
+	}
+}
+#endif
+
+int misc_init_r(void)
+{
+#ifdef CONFIG_PREBOOT
+	preboot_keys();
+#endif
+}
(quoted diff) +0 -0

b/include/configs/mx6qsabrelite.h index 8bc8a83..7bf8479 100644

include/configs/mx6qsabrelite.h +3 -0
@@ -42,6 +42,7 @@
 
 #define CONFIG_ARCH_CPU_INIT
 #define CONFIG_BOARD_EARLY_INIT_F
+#define CONFIG_MISC_INIT_R
 #define CONFIG_MXC_GPIO
 
 #define CONFIG_MXC_UART
@@ -110,6 +111,8 @@
 
 #define CONFIG_BOOTDELAY               3
 
+#define CONFIG_PREBOOT                 ""
+
 #define CONFIG_LOADADDR                        0x10800000
 #define CONFIG_SYS_TEXT_BASE           0x17800000
#3 lore raw

PATCH v2 1/1 i.MX6Q: mx6qsabrelite: Add keypress support to alter boot flow

Thanks for reviewing Marek,

Quoted 17 lines · Marek Vasut

Dear Eric Nelson,

Quoted 15 lines

Uses the 'magic_keys' idiom as described in doc/README.kbd:

	http://lists.denx.de/pipermail/u-boot/2012-April/122502.html

---
V2 based on suggestion by Wolfgang to follow prior implementations.

  board/freescale/mx6qsabrelite/mx6qsabrelite.c |  121
++++++++++++++++++++++++- include/configs/mx6qsabrelite.h               |
   3 +
  2 files changed, 122 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/board/freescale/mx6qsabrelite/mx6qsabrelite.c index 1d09a72..9ca46e7

Quoted 7 lines

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 350

Quoted 4 lines

<snip>

board/freescale/mx6qsabrelite/mx6qsabrelite.c +2 -0
+	buf[numpressed] = '\0' ;
+	return numpressed ;

Delete the space before semicolon please.

Okay, but is there a rule here somewhere? There are a bunch of other spots in this source that have space before semicolon.

Quoted 11 lines

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 358

Quoted 8 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +3 -0
+}
+
+static int do_kbd(cmd_tbl_t *cmdtp, int flag, int argc, char * const

argv[]) +{

board/freescale/mx6qsabrelite/mx6qsabrelite.c +4 -0
+	char envvalue[ARRAY_SIZE(buttons)+1];
+	int numpressed = read_keys(envvalue);
+	setenv("keybd", envvalue);
+	return 0 == numpressed ;

Drop yoda condition

Okay again, but are yoda conditionals verboten in U-Boot sources?

I find that this style catches typos (inadvertent assignments).

Quoted 5 lines
Quoted 5 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +2 -0
+}
+
 
<snip>
Quoted 9 lines

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 376

Quoted 6 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +6 -0
+static void preboot_keys(void)
+{
+	int i, numpressed ;
+	char keypress[ARRAY_SIZE(buttons)+1];
+	numpressed = read_keys(keypress);
+	if (0 != numpressed) {

Yoda ...

Got it. I'm replacing some others as well.

Quoted 6 lines

board/freescale/mx6qsabrelite/mx6qsabrelite.c int checkboard(void) · line 379

Quoted 3 lines
board/freescale/mx6qsabrelite/mx6qsabrelite.c +3 -0
+		char *kbd_magic_keys = getenv("magic_keys");
+		char *suffix;
+		/* loop over all magic keys;

Fix comment please?

V3 forthcoming.