1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale MPC83xx GPIO handling.
11 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION
12 #define CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION 0
14 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION
15 #define CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION 0
17 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN
18 #define CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN 0
20 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN
21 #define CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN 0
23 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_VALUE
24 #define CONFIG_MPC83XX_GPIO_0_INIT_VALUE 0
26 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_VALUE
27 #define CONFIG_MPC83XX_GPIO_1_INIT_VALUE 0
30 static unsigned int gpio_output_value[MPC83XX_GPIO_CTRLRS];
33 * Generic_GPIO primitives.
36 int gpio_request(unsigned gpio, const char *label)
38 if (gpio >= MAX_NUM_GPIOS)
44 int gpio_free(unsigned gpio)
46 /* Do not set to input */
50 /* set GPIO pin 'gpio' as an input */
51 int gpio_direction_input(unsigned gpio)
53 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
56 unsigned int line_mask;
58 /* 32-bits per controller */
63 line_mask = 1 << (31 - line);
65 clrbits_be32(&im->gpio[ctrlr].dir, line_mask);
70 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
71 int gpio_direction_output(unsigned gpio, int value)
73 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
76 unsigned int line_mask;
78 if (value != 0 && value != 1) {
79 printf("Error: Value parameter must be 0 or 1.\n");
83 gpio_set_value(gpio, value);
85 /* 32-bits per controller */
90 line_mask = 1 << (31 - line);
92 /* Make the line output */
93 setbits_be32(&im->gpio[ctrlr].dir, line_mask);
98 /* read GPIO IN value of pin 'gpio' */
99 int gpio_get_value(unsigned gpio)
101 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
104 unsigned int line_mask;
106 /* 32-bits per controller */
108 line = gpio & (0x1F);
111 line_mask = 1 << (31 - line);
113 /* Read the value and mask off the bit */
114 return (in_be32(&im->gpio[ctrlr].dat) & line_mask) != 0;
117 /* write GPIO OUT value to pin 'gpio' */
118 int gpio_set_value(unsigned gpio, int value)
120 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
123 unsigned int line_mask;
125 if (value != 0 && value != 1) {
126 printf("Error: Value parameter must be 0 or 1.\n");
130 /* 32-bits per controller */
132 line = gpio & (0x1F);
135 line_mask = 1 << (31 - line);
137 /* Update the local output buffer soft copy */
138 gpio_output_value[ctrlr] =
139 (gpio_output_value[ctrlr] & ~line_mask) | \
140 (value ? line_mask : 0);
142 /* Write the output */
143 out_be32(&im->gpio[ctrlr].dat, gpio_output_value[ctrlr]);
148 /* Configure GPIO registers early */
149 void mpc83xx_gpio_init_f(void)
151 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
153 #if MPC83XX_GPIO_CTRLRS >= 1
154 out_be32(&im->gpio[0].dir, CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION);
155 out_be32(&im->gpio[0].odr, CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN);
156 out_be32(&im->gpio[0].dat, CONFIG_MPC83XX_GPIO_0_INIT_VALUE);
157 out_be32(&im->gpio[0].ier, 0xFFFFFFFF); /* Clear all events */
158 out_be32(&im->gpio[0].imr, 0);
159 out_be32(&im->gpio[0].icr, 0);
162 #if MPC83XX_GPIO_CTRLRS >= 2
163 out_be32(&im->gpio[1].dir, CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION);
164 out_be32(&im->gpio[1].odr, CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN);
165 out_be32(&im->gpio[1].dat, CONFIG_MPC83XX_GPIO_1_INIT_VALUE);
166 out_be32(&im->gpio[1].ier, 0xFFFFFFFF); /* Clear all events */
167 out_be32(&im->gpio[1].imr, 0);
168 out_be32(&im->gpio[1].icr, 0);
172 /* Initialize GPIO soft-copies */
173 void mpc83xx_gpio_init_r(void)
175 #if MPC83XX_GPIO_CTRLRS >= 1
176 gpio_output_value[0] = CONFIG_MPC83XX_GPIO_0_INIT_VALUE;
179 #if MPC83XX_GPIO_CTRLRS >= 2
180 gpio_output_value[1] = CONFIG_MPC83XX_GPIO_1_INIT_VALUE;