summaryrefslogtreecommitdiffstats
path: root/drivers/gpio/mpc83xx_gpio.c
blob: 92b6e0cb76da3942199ff2a15a58068c4dba7f10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Freescale MPC83xx GPIO handling.
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <mpc83xx.h>
#include <asm/gpio.h>
#include <asm/io.h>

#ifndef CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION
#define CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION 0
#endif
#ifndef CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION
#define CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION 0
#endif
#ifndef CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN
#define CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN 0
#endif
#ifndef CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN
#define CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN 0
#endif
#ifndef CONFIG_MPC83XX_GPIO_0_INIT_VALUE
#define CONFIG_MPC83XX_GPIO_0_INIT_VALUE 0
#endif
#ifndef CONFIG_MPC83XX_GPIO_1_INIT_VALUE
#define CONFIG_MPC83XX_GPIO_1_INIT_VALUE 0
#endif

static unsigned int gpio_output_value[MPC83XX_GPIO_CTRLRS];

/*
 * Generic_GPIO primitives.
 */

int gpio_request(unsigned gpio, const char *label)
{
	if (gpio >= MAX_NUM_GPIOS)
		return -1;

	return 0;
}

int gpio_free(unsigned gpio)
{
	/* Do not set to input */
	return 0;
}

/* set GPIO pin 'gpio' as an input */
int gpio_direction_input(unsigned gpio)
{
	immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
	unsigned int ctrlr;
	unsigned int line;
	unsigned int line_mask;

	/* 32-bits per controller */
	ctrlr = gpio >> 5;
	line = gpio & (0x1F);

	/* Big endian */
	line_mask = 1 << (31 - line);

	clrbits_be32(&im->gpio[ctrlr].dir, line_mask);

	return 0;
}

/* set GPIO pin 'gpio' as an output, with polarity 'value' */
int gpio_direction_output(unsigned gpio, int value)
{
	immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
	unsigned int ctrlr;
	unsigned int line;
	unsigned int line_mask;

	if (value != 0 && value != 1) {
		printf("Error: Value parameter must be 0 or 1.\n");
		return -1;
	}

	gpio_set_value(gpio, value);

	/* 32-bits per controller */
	ctrlr = gpio >> 5;
	line = gpio & (0x1F);

	/* Big endian */
	line_mask = 1 << (31 - line);

	/* Make the line output */
	setbits_be32(&im->gpio[ctrlr].dir, line_mask);

	return 0;
}

/* read GPIO IN value of pin 'gpio' */
int gpio_get_value(unsigned gpio)
{
	immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
	unsigned int ctrlr;
	unsigned int line;
	unsigned int line_mask;

	/* 32-bits per controller */
	ctrlr = gpio >> 5;
	line = gpio & (0x1F);

	/* Big endian */
	line_mask = 1 << (31 - line);

	/* Read the value and mask off the bit */
	return (in_be32(&im->gpio[ctrlr].dat) & line_mask) != 0;
}

/* write GPIO OUT value to pin 'gpio' */
int gpio_set_value(unsigned gpio, int value)
{
	immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
	unsigned int ctrlr;
	unsigned int line;
	unsigned int line_mask;

	if (value != 0 && value != 1) {
		printf("Error: Value parameter must be 0 or 1.\n");
		return -1;
	}

	/* 32-bits per controller */
	ctrlr = gpio >> 5;
	line = gpio & (0x1F);

	/* Big endian */
	line_mask = 1 << (31 - line);

	/* Update the local output buffer soft copy */
	gpio_output_value[ctrlr] =
		(gpio_output_value[ctrlr] & ~line_mask) | \
			(value ? line_mask : 0);

	/* Write the output */
	out_be32(&im->gpio[ctrlr].dat, gpio_output_value[ctrlr]);

	return 0;
}

/* Configure GPIO registers early */
void mpc83xx_gpio_init_f(void)
{
	immap_t *im = (immap_t *)CONFIG_SYS_IMMR;

#if MPC83XX_GPIO_CTRLRS >= 1
	out_be32(&im->gpio[0].dir, CONFIG_MPC83XX_GPIO_0_INIT_DIRECTION);
	out_be32(&im->gpio[0].odr, CONFIG_MPC83XX_GPIO_0_INIT_OPEN_DRAIN);
	out_be32(&im->gpio[0].dat, CONFIG_MPC83XX_GPIO_0_INIT_VALUE);
	out_be32(&im->gpio[0].ier, 0xFFFFFFFF); /* Clear all events */
	out_be32(&im->gpio[0].imr, 0);
	out_be32(&im->gpio[0].icr, 0);
#endif

#if MPC83XX_GPIO_CTRLRS >= 2
	out_be32(&im->gpio[1].dir, CONFIG_MPC83XX_GPIO_1_INIT_DIRECTION);
	out_be32(&im->gpio[1].odr, CONFIG_MPC83XX_GPIO_1_INIT_OPEN_DRAIN);
	out_be32(&im->gpio[1].dat, CONFIG_MPC83XX_GPIO_1_INIT_VALUE);
	out_be32(&im->gpio[1].ier, 0xFFFFFFFF); /* Clear all events */
	out_be32(&im->gpio[1].imr, 0);
	out_be32(&im->gpio[1].icr, 0);
#endif
}

/* Initialize GPIO soft-copies */
void mpc83xx_gpio_init_r(void)
{
#if MPC83XX_GPIO_CTRLRS >= 1
	gpio_output_value[0] = CONFIG_MPC83XX_GPIO_0_INIT_VALUE;
#endif

#if MPC83XX_GPIO_CTRLRS >= 2
	gpio_output_value[1] = CONFIG_MPC83XX_GPIO_1_INIT_VALUE;
#endif
}