3 * Guennadi Liakhovetski, DENX Software Engineering, <lg@denx.de>
6 * Stefano Babic, DENX Software Engineering, <sbabic@denx.de>
8 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/arch/imx-regs.h>
18 enum mxc_gpio_direction {
19 MXC_GPIO_DIRECTION_IN,
20 MXC_GPIO_DIRECTION_OUT,
23 #define GPIO_PER_BANK 32
25 struct mxc_gpio_plat {
27 struct gpio_regs *regs;
30 struct mxc_bank_info {
31 struct gpio_regs *regs;
34 #ifndef CONFIG_DM_GPIO
35 #define GPIO_TO_PORT(n) ((n) / 32)
37 /* GPIO port description */
38 static unsigned long gpio_ports[] = {
39 [0] = GPIO1_BASE_ADDR,
40 [1] = GPIO2_BASE_ADDR,
41 [2] = GPIO3_BASE_ADDR,
42 #if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
43 defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
44 [3] = GPIO4_BASE_ADDR,
46 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
47 [4] = GPIO5_BASE_ADDR,
48 [5] = GPIO6_BASE_ADDR,
50 #if defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
51 [6] = GPIO7_BASE_ADDR,
55 static int mxc_gpio_direction(unsigned int gpio,
56 enum mxc_gpio_direction direction)
58 unsigned int port = GPIO_TO_PORT(gpio);
59 struct gpio_regs *regs;
62 if (port >= ARRAY_SIZE(gpio_ports)) {
63 printf("%s: Invalid GPIO %d\n", __func__, gpio);
69 regs = (struct gpio_regs *)gpio_ports[port];
71 l = readl(®s->gpio_dir);
74 case MXC_GPIO_DIRECTION_OUT:
77 case MXC_GPIO_DIRECTION_IN:
80 writel(l, ®s->gpio_dir);
85 int gpio_set_value(unsigned gpio, int value)
87 unsigned int port = GPIO_TO_PORT(gpio);
88 struct gpio_regs *regs;
91 if (port >= ARRAY_SIZE(gpio_ports)) {
92 printf("%s: Invalid GPIO %d\n", __func__, gpio);
98 regs = (struct gpio_regs *)gpio_ports[port];
100 l = readl(®s->gpio_dr);
105 writel(l, ®s->gpio_dr);
110 int gpio_get_value(unsigned gpio)
112 unsigned int port = GPIO_TO_PORT(gpio);
113 struct gpio_regs *regs;
116 if (port >= ARRAY_SIZE(gpio_ports)) {
117 printf("%s: Invalid GPIO %d\n", __func__, gpio);
123 regs = (struct gpio_regs *)gpio_ports[port];
125 if (readl(®s->gpio_dir) & (1 << gpio)) {
126 printf("WARNING: Reading status of output GPIO_%d_%d\n",
127 port - GPIO_TO_PORT(0), gpio);
128 val = (readl(®s->gpio_dr) >> gpio) & 0x01;
130 val = (readl(®s->gpio_psr) >> gpio) & 0x01;
135 int gpio_request(unsigned gpio, const char *label)
137 unsigned int port = GPIO_TO_PORT(gpio);
138 if (port >= ARRAY_SIZE(gpio_ports)) {
139 printf("%s: Invalid GPIO %d\n", __func__, gpio);
145 int gpio_free(unsigned gpio)
147 unsigned int port = GPIO_TO_PORT(gpio);
148 if (port >= ARRAY_SIZE(gpio_ports)) {
149 printf("%s: Invalid GPIO %d\n", __func__, gpio);
155 int gpio_direction_input(unsigned gpio)
157 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_IN);
160 int gpio_direction_output(unsigned gpio, int value)
162 int ret = gpio_set_value(gpio, value);
167 return mxc_gpio_direction(gpio, MXC_GPIO_DIRECTION_OUT);
171 #ifdef CONFIG_DM_GPIO
173 DECLARE_GLOBAL_DATA_PTR;
175 static int mxc_gpio_is_output(struct gpio_regs *regs, int offset)
179 val = readl(®s->gpio_dir);
181 return val & (1 << offset) ? 1 : 0;
184 static void mxc_gpio_bank_direction(struct gpio_regs *regs, int offset,
185 enum mxc_gpio_direction direction)
189 l = readl(®s->gpio_dir);
192 case MXC_GPIO_DIRECTION_OUT:
195 case MXC_GPIO_DIRECTION_IN:
198 writel(l, ®s->gpio_dir);
201 static void mxc_gpio_bank_set_value(struct gpio_regs *regs, int offset,
206 l = readl(®s->gpio_dr);
211 writel(l, ®s->gpio_dr);
214 static int mxc_gpio_bank_get_value(struct gpio_regs *regs, int offset)
216 return (readl(®s->gpio_psr) >> offset) & 0x01;
219 /* set GPIO pin 'gpio' as an input */
220 static int mxc_gpio_direction_input(struct udevice *dev, unsigned offset)
222 struct mxc_bank_info *bank = dev_get_priv(dev);
224 /* Configure GPIO direction as input. */
225 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_IN);
230 /* set GPIO pin 'gpio' as an output, with polarity 'value' */
231 static int mxc_gpio_direction_output(struct udevice *dev, unsigned offset,
234 struct mxc_bank_info *bank = dev_get_priv(dev);
236 /* Configure GPIO output value. */
237 mxc_gpio_bank_set_value(bank->regs, offset, value);
239 /* Configure GPIO direction as output. */
240 mxc_gpio_bank_direction(bank->regs, offset, MXC_GPIO_DIRECTION_OUT);
245 /* read GPIO IN value of pin 'gpio' */
246 static int mxc_gpio_get_value(struct udevice *dev, unsigned offset)
248 struct mxc_bank_info *bank = dev_get_priv(dev);
250 return mxc_gpio_bank_get_value(bank->regs, offset);
253 /* write GPIO OUT value to pin 'gpio' */
254 static int mxc_gpio_set_value(struct udevice *dev, unsigned offset,
257 struct mxc_bank_info *bank = dev_get_priv(dev);
259 mxc_gpio_bank_set_value(bank->regs, offset, value);
264 static int mxc_gpio_get_function(struct udevice *dev, unsigned offset)
266 struct mxc_bank_info *bank = dev_get_priv(dev);
268 /* GPIOF_FUNC is not implemented yet */
269 if (mxc_gpio_is_output(bank->regs, offset))
275 static const struct dm_gpio_ops gpio_mxc_ops = {
276 .direction_input = mxc_gpio_direction_input,
277 .direction_output = mxc_gpio_direction_output,
278 .get_value = mxc_gpio_get_value,
279 .set_value = mxc_gpio_set_value,
280 .get_function = mxc_gpio_get_function,
283 static int mxc_gpio_probe(struct udevice *dev)
285 struct mxc_bank_info *bank = dev_get_priv(dev);
286 struct mxc_gpio_plat *plat = dev_get_platdata(dev);
287 struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
291 banknum = plat->bank_index;
292 sprintf(name, "GPIO%d_", banknum + 1);
296 uc_priv->bank_name = str;
297 uc_priv->gpio_count = GPIO_PER_BANK;
298 bank->regs = plat->regs;
303 static int mxc_gpio_bind(struct udevice *dev)
305 struct mxc_gpio_plat *plat = dev->platdata;
309 * If platdata already exsits, directly return.
310 * Actually only when DT is not supported, platdata
311 * is statically initialized in U_BOOT_DEVICES.Here
317 addr = dev_get_addr(dev);
318 if (addr == FDT_ADDR_T_NONE)
323 * When every board is converted to driver model and DT is supported,
324 * this can be done by auto-alloc feature, but not using calloc
325 * to alloc memory for platdata.
327 plat = calloc(1, sizeof(*plat));
331 plat->regs = (struct gpio_regs *)addr;
332 plat->bank_index = dev->req_seq;
333 dev->platdata = plat;
338 static const struct udevice_id mxc_gpio_ids[] = {
339 { .compatible = "fsl,imx35-gpio" },
343 U_BOOT_DRIVER(gpio_mxc) = {
346 .ops = &gpio_mxc_ops,
347 .probe = mxc_gpio_probe,
348 .priv_auto_alloc_size = sizeof(struct mxc_bank_info),
349 .of_match = mxc_gpio_ids,
350 .bind = mxc_gpio_bind,
353 #ifndef CONFIG_OF_CONTROL
354 static const struct mxc_gpio_plat mxc_plat[] = {
355 { 0, (struct gpio_regs *)GPIO1_BASE_ADDR },
356 { 1, (struct gpio_regs *)GPIO2_BASE_ADDR },
357 { 2, (struct gpio_regs *)GPIO3_BASE_ADDR },
358 #if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
359 defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
360 { 3, (struct gpio_regs *)GPIO4_BASE_ADDR },
362 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
363 { 4, (struct gpio_regs *)GPIO5_BASE_ADDR },
364 { 5, (struct gpio_regs *)GPIO6_BASE_ADDR },
366 #if defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
367 { 6, (struct gpio_regs *)GPIO7_BASE_ADDR },
371 U_BOOT_DEVICES(mxc_gpios) = {
372 { "gpio_mxc", &mxc_plat[0] },
373 { "gpio_mxc", &mxc_plat[1] },
374 { "gpio_mxc", &mxc_plat[2] },
375 #if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX51) || \
376 defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
377 { "gpio_mxc", &mxc_plat[3] },
379 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
380 { "gpio_mxc", &mxc_plat[4] },
381 { "gpio_mxc", &mxc_plat[5] },
383 #if defined(CONFIG_SOC_MX53) || defined(CONFIG_SOC_MX6)
384 { "gpio_mxc", &mxc_plat[6] },