summaryrefslogtreecommitdiffstats
path: root/arch/arm/mach-mxs/iomux-imx.c
blob: 66ba74309d5644a3ecf71734e3217767edf1aeb4 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
 * (C) Copyright 2010 Juergen Beisert - Pengutronix
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <common.h>
#include <init.h>
#include <gpio.h>
#include <errno.h>
#include <io.h>
#include <mach/imx-regs.h>

#define HW_PINCTRL_CTRL 0x000
#define HW_PINCTRL_MUXSEL0 0x100

#ifdef CONFIG_ARCH_IMX23
#define HW_PINCTRL_DRIVE0 0x200
#define HW_PINCTRL_PULL0 0x400
#define HW_PINCTRL_DOUT0 0x500
#define HW_PINCTRL_DIN0 0x600
#define HW_PINCTRL_DOE0 0x700

#define MAX_GPIO_NO 95
#endif

#ifdef CONFIG_ARCH_IMX28
#define HW_PINCTRL_DRIVE0 0x300
#define HW_PINCTRL_PULL0 0x600
#define HW_PINCTRL_DOUT0 0x700
#define HW_PINCTRL_DIN0 0x900
#define HW_PINCTRL_DOE0 0xb00

#define MAX_GPIO_NO 159
#endif

static unsigned calc_mux_reg(unsigned no)
{
	/* each register controls 16 pads */
	return ((no >> 4) << 4) + HW_PINCTRL_MUXSEL0;
}

static unsigned calc_strength_reg(unsigned no)
{
	/* each register controls 8 pads */
	return  ((no >> 3) << 4) + HW_PINCTRL_DRIVE0;
}

static unsigned calc_pullup_reg(unsigned no)
{
	/* each register controls 32 pads */
	return  ((no >> 5) << 4) + HW_PINCTRL_PULL0;
}

static unsigned calc_output_enable_reg(unsigned no)
{
	/* each register controls 32 pads */
	return  ((no >> 5) << 4) + HW_PINCTRL_DOE0;
}

static unsigned calc_output_reg(unsigned no)
{
	/* each register controls 32 pads */
	return  ((no >> 5) << 4) + HW_PINCTRL_DOUT0;
}

static unsigned calc_input_reg(unsigned no)
{
	/* each register controls 32 pads */
	return  ((no >> 5) << 4) + HW_PINCTRL_DIN0;
}

/**
 * @param[in] m One pin define per call from iomux-mx23.h/iomux-mx28.h
 */
void imx_gpio_mode(uint32_t m)
{
	uint32_t reg;
	unsigned gpio_pin, reg_offset;

	gpio_pin = GET_GPIO_NO(m);

	/* configure the pad to its function (always) */
	reg_offset = calc_mux_reg(gpio_pin);
	reg = readl(IMX_IOMUXC_BASE + reg_offset) & ~(0x3 << ((gpio_pin % 16) << 1));
	reg |= GET_FUNC(m) << ((gpio_pin % 16) << 1);
	writel(reg, IMX_IOMUXC_BASE + reg_offset);

	/* some pins are disabled when configured for GPIO */
	if ((gpio_pin > MAX_GPIO_NO) && (GET_FUNC(m) == IS_GPIO)) {
		printf("Cannot configure pad %d to GPIO\n", gpio_pin);
		return;
	}

	if (SE_PRESENT(m)) {
		reg_offset = calc_strength_reg(gpio_pin);
		reg = readl(IMX_IOMUXC_BASE + reg_offset) & ~(0x3 << ((gpio_pin % 8) << 2));
		reg |= GET_STRENGTH(m) << ((gpio_pin % 8) << 2);
		writel(reg, IMX_IOMUXC_BASE + reg_offset);
	}

	if (VE_PRESENT(m)) {
		reg_offset = calc_strength_reg(gpio_pin);
		if (GET_VOLTAGE(m) == 1)
			writel(0x1 << (((gpio_pin % 8) << 2) + 2),
				IMX_IOMUXC_BASE + reg_offset + BIT_SET);
		else
			writel(0x1 << (((gpio_pin % 8) << 2) + 2),
				IMX_IOMUXC_BASE + reg_offset + BIT_CLR);
	}

	if (PE_PRESENT(m)) {
		reg_offset = calc_pullup_reg(gpio_pin);
		writel(0x1 << (gpio_pin % 32), IMX_IOMUXC_BASE + reg_offset +
				(GET_PULLUP(m) == 1 ? BIT_SET : BIT_CLR));
	}

	if (BK_PRESENT(m)) {
		reg_offset = calc_pullup_reg(gpio_pin);
		writel(0x1 << (gpio_pin % 32), IMX_IOMUXC_BASE + reg_offset +
				(GET_BITKEEPER(m) == 1 ? BIT_CLR : BIT_SET));
	}

	if (GET_FUNC(m) == IS_GPIO) {
		if (GET_GPIODIR(m) == 1) {
			/* first set the output value */
			reg_offset = calc_output_reg(gpio_pin);
			writel(0x1 << (gpio_pin % 32), IMX_IOMUXC_BASE +
				reg_offset + (GET_GPIOVAL(m) == 1 ? BIT_SET : BIT_CLR));
			/* then the direction */
			reg_offset = calc_output_enable_reg(gpio_pin);
			writel(0x1 << (gpio_pin % 32),
				IMX_IOMUXC_BASE + reg_offset + BIT_SET);
		} else {
			/* then the direction */
			reg_offset = calc_output_enable_reg(gpio_pin);
			writel(0x1 << (gpio_pin % 32),
				IMX_IOMUXC_BASE + reg_offset + BIT_CLR);
		}
	}
}

int gpio_direction_input(unsigned gpio)
{
	unsigned reg_offset;

	if (gpio > MAX_GPIO_NO)
		return -EINVAL;

	reg_offset = calc_output_enable_reg(gpio);
	writel(0x1 << (gpio % 32), IMX_IOMUXC_BASE + reg_offset + BIT_CLR);

	return 0;
}

int gpio_direction_output(unsigned gpio, int val)
{
	unsigned reg_offset;

	if (gpio > MAX_GPIO_NO)
		return -EINVAL;

	/* first set the output value... */
	reg_offset = calc_output_reg(gpio);
	writel(0x1 << (gpio % 32), IMX_IOMUXC_BASE +
		reg_offset + (val != 0 ? BIT_SET : BIT_CLR));
	/* ...then the direction */
	reg_offset = calc_output_enable_reg(gpio);
	writel(0x1 << (gpio % 32), IMX_IOMUXC_BASE + reg_offset + BIT_SET);

	return 0;
}

void gpio_set_value(unsigned gpio, int val)
{
	unsigned reg_offset;

	reg_offset = calc_output_reg(gpio);
	writel(0x1 << (gpio % 32), IMX_IOMUXC_BASE +
				reg_offset + (val != 0 ? BIT_SET : BIT_CLR));
}

int gpio_get_value(unsigned gpio)
{
	uint32_t reg;
	unsigned reg_offset;

	reg_offset = calc_input_reg(gpio);
	reg = readl(IMX_IOMUXC_BASE + reg_offset);
	if (reg & (0x1 << (gpio % 32)))
		return 1;

	return 0;
}