blob: 367b852039da52ccd23b9f99162cc53984dcc516 [file] [log] [blame]
Marek Vasut53fdab22011-11-08 23:18:13 +00001/*
2 * Freescale i.MX28 GPIO control code
3 *
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5 * on behalf of DENX Software Engineering GmbH
6 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Marek Vasut53fdab22011-11-08 23:18:13 +00008 */
9
10#include <common.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Marek Vasut53fdab22011-11-08 23:18:13 +000012#include <asm/io.h>
13#include <asm/arch/iomux.h>
14#include <asm/arch/imx-regs.h>
15
16#if defined(CONFIG_MX23)
17#define PINCTRL_BANKS 3
18#define PINCTRL_DOUT(n) (0x0500 + ((n) * 0x10))
19#define PINCTRL_DIN(n) (0x0600 + ((n) * 0x10))
20#define PINCTRL_DOE(n) (0x0700 + ((n) * 0x10))
21#define PINCTRL_PIN2IRQ(n) (0x0800 + ((n) * 0x10))
22#define PINCTRL_IRQEN(n) (0x0900 + ((n) * 0x10))
23#define PINCTRL_IRQSTAT(n) (0x0c00 + ((n) * 0x10))
24#elif defined(CONFIG_MX28)
25#define PINCTRL_BANKS 5
26#define PINCTRL_DOUT(n) (0x0700 + ((n) * 0x10))
27#define PINCTRL_DIN(n) (0x0900 + ((n) * 0x10))
28#define PINCTRL_DOE(n) (0x0b00 + ((n) * 0x10))
29#define PINCTRL_PIN2IRQ(n) (0x1000 + ((n) * 0x10))
30#define PINCTRL_IRQEN(n) (0x1100 + ((n) * 0x10))
31#define PINCTRL_IRQSTAT(n) (0x1400 + ((n) * 0x10))
32#else
33#error "Please select CONFIG_MX23 or CONFIG_MX28"
34#endif
35
36#define GPIO_INT_FALL_EDGE 0x0
37#define GPIO_INT_LOW_LEV 0x1
38#define GPIO_INT_RISE_EDGE 0x2
39#define GPIO_INT_HIGH_LEV 0x3
40#define GPIO_INT_LEV_MASK (1 << 0)
41#define GPIO_INT_POL_MASK (1 << 1)
42
43void mxs_gpio_init(void)
44{
45 int i;
46
47 for (i = 0; i < PINCTRL_BANKS; i++) {
48 writel(0, MXS_PINCTRL_BASE + PINCTRL_PIN2IRQ(i));
49 writel(0, MXS_PINCTRL_BASE + PINCTRL_IRQEN(i));
50 /* Use SCT address here to clear the IRQSTAT bits */
51 writel(0xffffffff, MXS_PINCTRL_BASE + PINCTRL_IRQSTAT(i) + 8);
52 }
53}
54
Joe Hershbergerf8928f12011-11-11 15:55:36 -060055int gpio_get_value(unsigned gpio)
Marek Vasut53fdab22011-11-08 23:18:13 +000056{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060057 uint32_t bank = PAD_BANK(gpio);
Marek Vasut53fdab22011-11-08 23:18:13 +000058 uint32_t offset = PINCTRL_DIN(bank);
Otavio Salvador5309b002012-08-05 09:05:30 +000059 struct mxs_register_32 *reg =
60 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
Marek Vasut53fdab22011-11-08 23:18:13 +000061
Joe Hershbergerf8928f12011-11-11 15:55:36 -060062 return (readl(&reg->reg) >> PAD_PIN(gpio)) & 1;
Marek Vasut53fdab22011-11-08 23:18:13 +000063}
64
Joe Hershbergerf8928f12011-11-11 15:55:36 -060065void gpio_set_value(unsigned gpio, int value)
Marek Vasut53fdab22011-11-08 23:18:13 +000066{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060067 uint32_t bank = PAD_BANK(gpio);
Marek Vasut53fdab22011-11-08 23:18:13 +000068 uint32_t offset = PINCTRL_DOUT(bank);
Otavio Salvador5309b002012-08-05 09:05:30 +000069 struct mxs_register_32 *reg =
70 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
Marek Vasut53fdab22011-11-08 23:18:13 +000071
72 if (value)
Joe Hershbergerf8928f12011-11-11 15:55:36 -060073 writel(1 << PAD_PIN(gpio), &reg->reg_set);
Marek Vasut53fdab22011-11-08 23:18:13 +000074 else
Joe Hershbergerf8928f12011-11-11 15:55:36 -060075 writel(1 << PAD_PIN(gpio), &reg->reg_clr);
Marek Vasut53fdab22011-11-08 23:18:13 +000076}
77
Joe Hershbergerf8928f12011-11-11 15:55:36 -060078int gpio_direction_input(unsigned gpio)
Marek Vasut53fdab22011-11-08 23:18:13 +000079{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060080 uint32_t bank = PAD_BANK(gpio);
Marek Vasut53fdab22011-11-08 23:18:13 +000081 uint32_t offset = PINCTRL_DOE(bank);
Otavio Salvador5309b002012-08-05 09:05:30 +000082 struct mxs_register_32 *reg =
83 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
Marek Vasut53fdab22011-11-08 23:18:13 +000084
Joe Hershbergerf8928f12011-11-11 15:55:36 -060085 writel(1 << PAD_PIN(gpio), &reg->reg_clr);
Marek Vasut53fdab22011-11-08 23:18:13 +000086
87 return 0;
88}
89
Joe Hershbergerf8928f12011-11-11 15:55:36 -060090int gpio_direction_output(unsigned gpio, int value)
Marek Vasut53fdab22011-11-08 23:18:13 +000091{
Joe Hershbergerf8928f12011-11-11 15:55:36 -060092 uint32_t bank = PAD_BANK(gpio);
Marek Vasut53fdab22011-11-08 23:18:13 +000093 uint32_t offset = PINCTRL_DOE(bank);
Otavio Salvador5309b002012-08-05 09:05:30 +000094 struct mxs_register_32 *reg =
95 (struct mxs_register_32 *)(MXS_PINCTRL_BASE + offset);
Marek Vasut53fdab22011-11-08 23:18:13 +000096
Joe Hershbergerf8928f12011-11-11 15:55:36 -060097 gpio_set_value(gpio, value);
Marek Vasut53fdab22011-11-08 23:18:13 +000098
Michael Heimpold041487c2013-11-03 22:59:26 +010099 writel(1 << PAD_PIN(gpio), &reg->reg_set);
100
Marek Vasut53fdab22011-11-08 23:18:13 +0000101 return 0;
102}
103
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600104int gpio_request(unsigned gpio, const char *label)
Marek Vasut53fdab22011-11-08 23:18:13 +0000105{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600106 if (PAD_BANK(gpio) >= PINCTRL_BANKS)
107 return -1;
Marek Vasut53fdab22011-11-08 23:18:13 +0000108
109 return 0;
110}
111
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600112int gpio_free(unsigned gpio)
Marek Vasut53fdab22011-11-08 23:18:13 +0000113{
Joe Hershbergerf8928f12011-11-11 15:55:36 -0600114 return 0;
Marek Vasut53fdab22011-11-08 23:18:13 +0000115}
Måns Rullgårda7ec6862015-12-15 22:27:57 +0000116
117int name_to_gpio(const char *name)
118{
119 unsigned bank, pin;
120 char *end;
121
122 bank = simple_strtoul(name, &end, 10);
123
124 if (!*end || *end != ':')
125 return bank;
126
127 pin = simple_strtoul(end + 1, NULL, 10);
128
129 return (bank << MXS_PAD_BANK_SHIFT) | (pin << MXS_PAD_PIN_SHIFT);
130}