2 * Freescale MPC83xx GPIO handling.
4 * See file CREDITS for list of people who contributed to this
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION
29 #define CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION 0
31 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION
32 #define CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION 0
34 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN
35 #define CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN 0
37 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN
38 #define CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN 0
40 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_VALUE
41 #define CONFIG_MPC83XX_GPIO_0_INIT_VALUE 0
43 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_VALUE
44 #define CONFIG_MPC83XX_GPIO_1_INIT_VALUE 0
47 static unsigned int gpio_output_value[MPC83XX_GPIO_CTRLRS];
50 * Generic_GPIO primitives.
53 int gpio_request(unsigned gpio, const char *label)
55 if (gpio >= MAX_NUM_GPIOS)
61 int gpio_free(unsigned gpio)
63 /* Do not set to input */
67 /* set GPIO pin 'gpio' as an input */
68 int gpio_direction_input(unsigned gpio)
70 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
73 unsigned int line_mask;
75 /* 32-bits per controller */
80 line_mask = 1 << (31 - line);
82 clrbits_be32(&im->gpio[ctrlr].dir, line_mask);
87 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
88 int gpio_direction_output(unsigned gpio, int value)
90 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
93 unsigned int line_mask;
95 if (value != 0 && value != 1) {
96 printf("Error: Value parameter must be 0 or 1.\n");
100 gpio_set_value(gpio, value);
102 /* 32-bits per controller */
104 line = gpio & (0x1F);
107 line_mask = 1 << (31 - line);
109 /* Make the line output */
110 setbits_be32(&im->gpio[ctrlr].dir, line_mask);
115 /* read GPIO IN value of pin 'gpio' */
116 int gpio_get_value(unsigned gpio)
118 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
121 unsigned int line_mask;
123 /* 32-bits per controller */
125 line = gpio & (0x1F);
128 line_mask = 1 << (31 - line);
130 /* Read the value and mask off the bit */
131 return (in_be32(&im->gpio[ctrlr].dat) & line_mask) != 0;
134 /* write GPIO OUT value to pin 'gpio' */
135 int gpio_set_value(unsigned gpio, int value)
137 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
140 unsigned int line_mask;
142 if (value != 0 && value != 1) {
143 printf("Error: Value parameter must be 0 or 1.\n");
147 /* 32-bits per controller */
149 line = gpio & (0x1F);
152 line_mask = 1 << (31 - line);
154 /* Update the local output buffer soft copy */
155 gpio_output_value[ctrlr] =
156 (gpio_output_value[ctrlr] & ~line_mask) | \
157 (value ? line_mask : 0);
159 /* Write the output */
160 out_be32(&im->gpio[ctrlr].dat, gpio_output_value[ctrlr]);
165 /* Configure GPIO registers early */
166 void mpc83xx_gpio_init_f(void)
168 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
170 #if MPC83XX_GPIO_CTRLRS >= 1
171 out_be32(&im->gpio[0].dir, CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION);
172 out_be32(&im->gpio[0].odr, CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN);
173 out_be32(&im->gpio[0].dat, CONFIG_MPC83XX_GPIO_0_INIT_VALUE);
174 out_be32(&im->gpio[0].ier, 0xFFFFFFFF); /* Clear all events */
175 out_be32(&im->gpio[0].imr, 0);
176 out_be32(&im->gpio[0].icr, 0);
179 #if MPC83XX_GPIO_CTRLRS >= 2
180 out_be32(&im->gpio[1].dir, CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION);
181 out_be32(&im->gpio[1].odr, CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN);
182 out_be32(&im->gpio[1].dat, CONFIG_MPC83XX_GPIO_1_INIT_VALUE);
183 out_be32(&im->gpio[1].ier, 0xFFFFFFFF); /* Clear all events */
184 out_be32(&im->gpio[1].imr, 0);
185 out_be32(&im->gpio[1].icr, 0);
189 /* Initialize GPIO soft-copies */
190 void mpc83xx_gpio_init_r(void)
192 #if MPC83XX_GPIO_CTRLRS >= 1
193 gpio_output_value[0] = CONFIG_MPC83XX_GPIO_0_INIT_VALUE;
196 #if MPC83XX_GPIO_CTRLRS >= 2
197 gpio_output_value[1] = CONFIG_MPC83XX_GPIO_1_INIT_VALUE;