blob: c04cef4cb98a091f74d6592c6fa4455dadc37882 [file] [log] [blame]
rev13@wp.plfec465a2015-03-01 12:44:40 +01001/*
2 * (C) Copyright 2011
3 * Yuri Tikhonov, Emcraft Systems, yur@emcraft.com
4 *
5 * (C) Copyright 2015
Kamil Lulkodecd33b2015-11-29 11:50:53 +01006 * Kamil Lulko, <kamil.lulko@gmail.com>
rev13@wp.plfec465a2015-03-01 12:44:40 +01007 *
Matt Porter359919a2015-05-05 15:00:25 -04008 * Copyright 2015 ATS Advanced Telematics Systems GmbH
9 * Copyright 2015 Konsulko Group, Matt Porter <mporter@konsulko.com>
10 *
rev13@wp.plfec465a2015-03-01 12:44:40 +010011 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include <common.h>
15#include <asm/io.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090016#include <linux/errno.h>
rev13@wp.plfec465a2015-03-01 12:44:40 +010017#include <asm/arch/stm32.h>
18#include <asm/arch/gpio.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
rev13@wp.plfec465a2015-03-01 12:44:40 +010022static const unsigned long io_base[] = {
23 STM32_GPIOA_BASE, STM32_GPIOB_BASE, STM32_GPIOC_BASE,
24 STM32_GPIOD_BASE, STM32_GPIOE_BASE, STM32_GPIOF_BASE,
25 STM32_GPIOG_BASE, STM32_GPIOH_BASE, STM32_GPIOI_BASE
26};
27
28struct stm32_gpio_regs {
29 u32 moder; /* GPIO port mode */
30 u32 otyper; /* GPIO port output type */
31 u32 ospeedr; /* GPIO port output speed */
32 u32 pupdr; /* GPIO port pull-up/pull-down */
33 u32 idr; /* GPIO port input data */
34 u32 odr; /* GPIO port output data */
35 u32 bsrr; /* GPIO port bit set/reset */
36 u32 lckr; /* GPIO port configuration lock */
37 u32 afr[2]; /* GPIO alternate function */
38};
39
40#define CHECK_DSC(x) (!x || x->port > 8 || x->pin > 15)
41#define CHECK_CTL(x) (!x || x->af > 15 || x->mode > 3 || x->otype > 1 || \
42 x->pupd > 2 || x->speed > 3)
43
44int stm32_gpio_config(const struct stm32_gpio_dsc *dsc,
45 const struct stm32_gpio_ctl *ctl)
46{
47 struct stm32_gpio_regs *gpio_regs;
48 u32 i;
49 int rv;
50
51 if (CHECK_DSC(dsc)) {
52 rv = -EINVAL;
53 goto out;
54 }
55 if (CHECK_CTL(ctl)) {
56 rv = -EINVAL;
57 goto out;
58 }
59
60 gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
61
rev13@wp.plfec465a2015-03-01 12:44:40 +010062 i = (dsc->pin & 0x07) * 4;
Axel Lin3e59f9e2015-04-26 10:32:36 +080063 clrsetbits_le32(&gpio_regs->afr[dsc->pin >> 3], 0xF << i, ctl->af << i);
rev13@wp.plfec465a2015-03-01 12:44:40 +010064
65 i = dsc->pin * 2;
66
Axel Lin3e59f9e2015-04-26 10:32:36 +080067 clrsetbits_le32(&gpio_regs->moder, 0x3 << i, ctl->mode << i);
68 clrsetbits_le32(&gpio_regs->otyper, 0x3 << i, ctl->otype << i);
69 clrsetbits_le32(&gpio_regs->ospeedr, 0x3 << i, ctl->speed << i);
70 clrsetbits_le32(&gpio_regs->pupdr, 0x3 << i, ctl->pupd << i);
rev13@wp.plfec465a2015-03-01 12:44:40 +010071
72 rv = 0;
73out:
74 return rv;
75}
76
77int stm32_gpout_set(const struct stm32_gpio_dsc *dsc, int state)
78{
79 struct stm32_gpio_regs *gpio_regs;
80 int rv;
81
82 if (CHECK_DSC(dsc)) {
83 rv = -EINVAL;
84 goto out;
85 }
86
87 gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
88
89 if (state)
90 writel(1 << dsc->pin, &gpio_regs->bsrr);
91 else
92 writel(1 << (dsc->pin + 16), &gpio_regs->bsrr);
93
94 rv = 0;
95out:
96 return rv;
97}
98
99int stm32_gpin_get(const struct stm32_gpio_dsc *dsc)
100{
101 struct stm32_gpio_regs *gpio_regs;
102 int rv;
103
104 if (CHECK_DSC(dsc)) {
105 rv = -EINVAL;
106 goto out;
107 }
108
109 gpio_regs = (struct stm32_gpio_regs *)io_base[dsc->port];
110 rv = readl(&gpio_regs->idr) & (1 << dsc->pin);
111out:
112 return rv;
113}
114
115/* Common GPIO API */
116
117int gpio_request(unsigned gpio, const char *label)
118{
119 return 0;
120}
121
122int gpio_free(unsigned gpio)
123{
124 return 0;
125}
126
127int gpio_direction_input(unsigned gpio)
128{
129 struct stm32_gpio_dsc dsc;
130 struct stm32_gpio_ctl ctl;
131
132 dsc.port = stm32_gpio_to_port(gpio);
133 dsc.pin = stm32_gpio_to_pin(gpio);
134 ctl.af = STM32_GPIO_AF0;
135 ctl.mode = STM32_GPIO_MODE_IN;
Matt Porter359919a2015-05-05 15:00:25 -0400136 ctl.otype = STM32_GPIO_OTYPE_PP;
rev13@wp.plfec465a2015-03-01 12:44:40 +0100137 ctl.pupd = STM32_GPIO_PUPD_NO;
138 ctl.speed = STM32_GPIO_SPEED_50M;
139
140 return stm32_gpio_config(&dsc, &ctl);
141}
142
143int gpio_direction_output(unsigned gpio, int value)
144{
145 struct stm32_gpio_dsc dsc;
146 struct stm32_gpio_ctl ctl;
147 int res;
148
149 dsc.port = stm32_gpio_to_port(gpio);
150 dsc.pin = stm32_gpio_to_pin(gpio);
151 ctl.af = STM32_GPIO_AF0;
152 ctl.mode = STM32_GPIO_MODE_OUT;
rev13@wp.plfec465a2015-03-01 12:44:40 +0100153 ctl.pupd = STM32_GPIO_PUPD_NO;
154 ctl.speed = STM32_GPIO_SPEED_50M;
rev13@wp.plfec465a2015-03-01 12:44:40 +0100155
156 res = stm32_gpio_config(&dsc, &ctl);
157 if (res < 0)
158 goto out;
159 res = stm32_gpout_set(&dsc, value);
160out:
161 return res;
162}
163
164int gpio_get_value(unsigned gpio)
165{
166 struct stm32_gpio_dsc dsc;
167
168 dsc.port = stm32_gpio_to_port(gpio);
169 dsc.pin = stm32_gpio_to_pin(gpio);
170
171 return stm32_gpin_get(&dsc);
172}
173
174int gpio_set_value(unsigned gpio, int value)
175{
176 struct stm32_gpio_dsc dsc;
177
178 dsc.port = stm32_gpio_to_port(gpio);
179 dsc.pin = stm32_gpio_to_pin(gpio);
180
181 return stm32_gpout_set(&dsc, value);
182}