blob: 4430c90d4d760a6d7b6f5b66601d1c65b08cfc23 [file] [log] [blame]
Mike Frysingerb52c2d12010-06-02 04:34:49 -04001/*
2 * Control GPIO pins on the fly
3 *
4 * Copyright (c) 2008-2010 Analog Devices Inc.
5 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <common.h>
10#include <command.h>
11
12#include <asm/blackfin.h>
13#include <asm/gpio.h>
14
15enum {
16 GPIO_INPUT,
17 GPIO_SET,
18 GPIO_CLEAR,
19 GPIO_TOGGLE,
20};
21
22int do_gpio(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
23{
24 if (argc == 2 && !strcmp(argv[1], "status")) {
25 bfin_gpio_labels();
26 return 0;
27 }
28
Mike Frysinger12c2a492010-07-23 04:24:46 -040029 if (argc != 3)
Mike Frysingerb52c2d12010-06-02 04:34:49 -040030 show_usage:
Mike Frysinger12c2a492010-07-23 04:24:46 -040031 return cmd_usage(cmdtp);
Mike Frysingerb52c2d12010-06-02 04:34:49 -040032
33 /* parse the behavior */
34 ulong sub_cmd;
35 switch (argv[1][0]) {
36 case 'i': sub_cmd = GPIO_INPUT; break;
37 case 's': sub_cmd = GPIO_SET; break;
38 case 'c': sub_cmd = GPIO_CLEAR; break;
39 case 't': sub_cmd = GPIO_TOGGLE; break;
40 default: goto show_usage;
41 }
42
43 /* parse the pin with format: [p][port]<#> */
44 const char *str_pin = argv[2];
45
46 /* grab the [p]<port> portion */
47 ulong port_base;
48 if (*str_pin == 'p') ++str_pin;
49 switch (*str_pin) {
50#ifdef GPIO_PA0
51 case 'a': port_base = GPIO_PA0; break;
52#endif
53#ifdef GPIO_PB0
54 case 'b': port_base = GPIO_PB0; break;
55#endif
56#ifdef GPIO_PC0
57 case 'c': port_base = GPIO_PC0; break;
58#endif
59#ifdef GPIO_PD0
60 case 'd': port_base = GPIO_PD0; break;
61#endif
62#ifdef GPIO_PE0
63 case 'e': port_base = GPIO_PE0; break;
64#endif
65#ifdef GPIO_PF0
66 case 'f': port_base = GPIO_PF0; break;
67#endif
68#ifdef GPIO_PG0
69 case 'g': port_base = GPIO_PG0; break;
70#endif
71#ifdef GPIO_PH0
72 case 'h': port_base = GPIO_PH0; break;
73#endif
74#ifdef GPIO_PI0
75 case 'i': port_base = GPIO_PI0; break;
76#endif
77#ifdef GPIO_PJ
78 case 'j': port_base = GPIO_PJ0; break;
79#endif
80 default: goto show_usage;
81 }
82
83 /* grab the <#> portion */
84 ulong pin = simple_strtoul(str_pin + 1, NULL, 10);
85 if (pin > 15)
86 goto show_usage;
87
88 /* grab the pin before we tweak it */
89 ulong gpio = port_base + pin;
90 gpio_request(gpio, "cmd_gpio");
91
92 /* finally, let's do it: set direction and exec command */
93 if (sub_cmd == GPIO_INPUT) {
94 gpio_direction_input(gpio);
95 printf("gpio: pin %lu on port %c set to input\n", pin, *str_pin);
96 return 0;
97 }
98
99 ulong value;
100 switch (sub_cmd) {
101 case GPIO_SET: value = 1; break;
102 case GPIO_CLEAR: value = 0; break;
103 case GPIO_TOGGLE: value = !gpio_get_value(gpio); break;
104 default: goto show_usage;
105 }
106 gpio_direction_output(gpio, value);
107 printf("gpio: pin %lu on port %c (gpio %lu) value is %lu\n",
108 pin, *str_pin, gpio, value);
109
110 gpio_free(gpio);
111
112 return 0;
113}
114
115U_BOOT_CMD(gpio, 3, 0, do_gpio,
116 "set/clear/toggle gpio output pins",
117 "<set|clear|toggle> <port><pin>\n"
118 " - set/clear/toggle the specified pin (e.g. PF10)");