1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale MPC83xx GPIO handling.
12 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION
13 #define CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION 0
15 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION
16 #define CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION 0
18 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN
19 #define CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN 0
21 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN
22 #define CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN 0
24 #ifndef CONFIG_MPC83XX_GPIO_0_INIT_VALUE
25 #define CONFIG_MPC83XX_GPIO_0_INIT_VALUE 0
27 #ifndef CONFIG_MPC83XX_GPIO_1_INIT_VALUE
28 #define CONFIG_MPC83XX_GPIO_1_INIT_VALUE 0
31 static unsigned int gpio_output_value
[MPC83XX_GPIO_CTRLRS
];
34 * Generic_GPIO primitives.
37 int gpio_request(unsigned gpio
, const char *label
)
39 if (gpio
>= MAX_NUM_GPIOS
)
45 int gpio_free(unsigned gpio
)
47 /* Do not set to input */
51 /* set GPIO pin 'gpio' as an input */
52 int gpio_direction_input(unsigned gpio
)
54 immap_t
*im
= (immap_t
*)CONFIG_SYS_IMMR
;
57 unsigned int line_mask
;
59 /* 32-bits per controller */
64 line_mask
= 1 << (31 - line
);
66 clrbits_be32(&im
->gpio
[ctrlr
].dir
, line_mask
);
71 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
72 int gpio_direction_output(unsigned gpio
, int value
)
74 immap_t
*im
= (immap_t
*)CONFIG_SYS_IMMR
;
77 unsigned int line_mask
;
79 if (value
!= 0 && value
!= 1) {
80 printf("Error: Value parameter must be 0 or 1.\n");
84 gpio_set_value(gpio
, value
);
86 /* 32-bits per controller */
91 line_mask
= 1 << (31 - line
);
93 /* Make the line output */
94 setbits_be32(&im
->gpio
[ctrlr
].dir
, line_mask
);
99 /* read GPIO IN value of pin 'gpio' */
100 int gpio_get_value(unsigned gpio
)
102 immap_t
*im
= (immap_t
*)CONFIG_SYS_IMMR
;
105 unsigned int line_mask
;
107 /* 32-bits per controller */
109 line
= gpio
& (0x1F);
112 line_mask
= 1 << (31 - line
);
114 /* Read the value and mask off the bit */
115 return (in_be32(&im
->gpio
[ctrlr
].dat
) & line_mask
) != 0;
118 /* write GPIO OUT value to pin 'gpio' */
119 int gpio_set_value(unsigned gpio
, int value
)
121 immap_t
*im
= (immap_t
*)CONFIG_SYS_IMMR
;
124 unsigned int line_mask
;
126 if (value
!= 0 && value
!= 1) {
127 printf("Error: Value parameter must be 0 or 1.\n");
131 /* 32-bits per controller */
133 line
= gpio
& (0x1F);
136 line_mask
= 1 << (31 - line
);
138 /* Update the local output buffer soft copy */
139 gpio_output_value
[ctrlr
] =
140 (gpio_output_value
[ctrlr
] & ~line_mask
) | \
141 (value
? line_mask
: 0);
143 /* Write the output */
144 out_be32(&im
->gpio
[ctrlr
].dat
, gpio_output_value
[ctrlr
]);
149 /* Configure GPIO registers early */
150 void mpc83xx_gpio_init_f(void)
152 immap_t
*im
= (immap_t
*)CONFIG_SYS_IMMR
;
154 #if MPC83XX_GPIO_CTRLRS >= 1
155 out_be32(&im
->gpio
[0].dir
, CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION
);
156 out_be32(&im
->gpio
[0].odr
, CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN
);
157 out_be32(&im
->gpio
[0].dat
, CONFIG_MPC83XX_GPIO_0_INIT_VALUE
);
158 out_be32(&im
->gpio
[0].ier
, 0xFFFFFFFF); /* Clear all events */
159 out_be32(&im
->gpio
[0].imr
, 0);
160 out_be32(&im
->gpio
[0].icr
, 0);
163 #if MPC83XX_GPIO_CTRLRS >= 2
164 out_be32(&im
->gpio
[1].dir
, CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION
);
165 out_be32(&im
->gpio
[1].odr
, CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN
);
166 out_be32(&im
->gpio
[1].dat
, CONFIG_MPC83XX_GPIO_1_INIT_VALUE
);
167 out_be32(&im
->gpio
[1].ier
, 0xFFFFFFFF); /* Clear all events */
168 out_be32(&im
->gpio
[1].imr
, 0);
169 out_be32(&im
->gpio
[1].icr
, 0);
173 /* Initialize GPIO soft-copies */
174 void mpc83xx_gpio_init_r(void)
176 #if MPC83XX_GPIO_CTRLRS >= 1
177 gpio_output_value
[0] = CONFIG_MPC83XX_GPIO_0_INIT_VALUE
;
180 #if MPC83XX_GPIO_CTRLRS >= 2
181 gpio_output_value
[1] = CONFIG_MPC83XX_GPIO_1_INIT_VALUE
;