blob: 6183103b5bd1f7ba5ea39bf4cb4d78ccf152450f [file] [log] [blame]
Vikas Manocha07e9e412017-02-12 10:25:49 -08001#include <common.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -08002#include <dm.h>
3#include <dm/pinctrl.h>
Vikas Manochaec8630a2017-04-10 15:02:57 -07004#include <asm/arch/gpio.h>
5#include <asm/gpio.h>
6#include <asm/io.h>
Vikas Manocha07e9e412017-02-12 10:25:49 -08007
8DECLARE_GLOBAL_DATA_PTR;
9
Vikas Manocha40ddb3a2017-04-10 15:03:04 -070010#define MAX_PINS_ONE_IP 70
Vikas Manochaec8630a2017-04-10 15:02:57 -070011#define MODE_BITS_MASK 3
12#define OSPEED_MASK 3
13#define PUPD_MASK 3
14#define OTYPE_MSK 1
15#define AFR_MASK 0xF
16
17static int stm32_gpio_config(struct gpio_desc *desc,
18 const struct stm32_gpio_ctl *ctl)
19{
20 struct stm32_gpio_priv *priv = dev_get_priv(desc->dev);
21 struct stm32_gpio_regs *regs = priv->regs;
22 u32 index;
23
24 if (!ctl || ctl->af > 15 || ctl->mode > 3 || ctl->otype > 1 ||
25 ctl->pupd > 2 || ctl->speed > 3)
26 return -EINVAL;
27
28 index = (desc->offset & 0x07) * 4;
29 clrsetbits_le32(&regs->afr[desc->offset >> 3], AFR_MASK << index,
30 ctl->af << index);
31
32 index = desc->offset * 2;
33 clrsetbits_le32(&regs->moder, MODE_BITS_MASK << index,
34 ctl->mode << index);
35 clrsetbits_le32(&regs->ospeedr, OSPEED_MASK << index,
36 ctl->speed << index);
37 clrsetbits_le32(&regs->pupdr, PUPD_MASK << index, ctl->pupd << index);
38
39 index = desc->offset;
40 clrsetbits_le32(&regs->otyper, OTYPE_MSK << index, ctl->otype << index);
41
42 return 0;
43}
Vikas Manocha07e9e412017-02-12 10:25:49 -080044static int prep_gpio_dsc(struct stm32_gpio_dsc *gpio_dsc, u32 port_pin)
45{
46 gpio_dsc->port = (port_pin & 0xF000) >> 12;
47 gpio_dsc->pin = (port_pin & 0x0F00) >> 8;
48 debug("%s: GPIO:port= %d, pin= %d\n", __func__, gpio_dsc->port,
49 gpio_dsc->pin);
50
51 return 0;
52}
53
54static int prep_gpio_ctl(struct stm32_gpio_ctl *gpio_ctl, u32 gpio_fn, int node)
55{
56 gpio_fn &= 0x00FF;
Vikas Manochaec8630a2017-04-10 15:02:57 -070057 gpio_ctl->af = 0;
Vikas Manocha07e9e412017-02-12 10:25:49 -080058
59 switch (gpio_fn) {
60 case 0:
61 gpio_ctl->mode = STM32_GPIO_MODE_IN;
62 break;
63 case 1 ... 16:
64 gpio_ctl->mode = STM32_GPIO_MODE_AF;
65 gpio_ctl->af = gpio_fn - 1;
66 break;
67 case 17:
68 gpio_ctl->mode = STM32_GPIO_MODE_AN;
69 break;
70 default:
71 gpio_ctl->mode = STM32_GPIO_MODE_OUT;
72 break;
73 }
74
75 gpio_ctl->speed = fdtdec_get_int(gd->fdt_blob, node, "slew-rate", 0);
76
77 if (fdtdec_get_bool(gd->fdt_blob, node, "drive-open-drain"))
78 gpio_ctl->otype = STM32_GPIO_OTYPE_OD;
79 else
80 gpio_ctl->otype = STM32_GPIO_OTYPE_PP;
81
82 if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-up"))
83 gpio_ctl->pupd = STM32_GPIO_PUPD_UP;
84 else if (fdtdec_get_bool(gd->fdt_blob, node, "bias-pull-down"))
85 gpio_ctl->pupd = STM32_GPIO_PUPD_DOWN;
86 else
87 gpio_ctl->pupd = STM32_GPIO_PUPD_NO;
88
89 debug("%s: gpio fn= %d, slew-rate= %x, op type= %x, pull-upd is = %x\n",
90 __func__, gpio_fn, gpio_ctl->speed, gpio_ctl->otype,
91 gpio_ctl->pupd);
92
93 return 0;
94}
95
Christophe Kerelloa466d212017-06-20 17:04:18 +020096static int stm32_pinctrl_config(int offset)
Vikas Manocha07e9e412017-02-12 10:25:49 -080097{
Vikas Manocha40ddb3a2017-04-10 15:03:04 -070098 u32 pin_mux[MAX_PINS_ONE_IP];
Vikas Manocha07e9e412017-02-12 10:25:49 -080099 int rv, len;
100
Vikas Manocha07e9e412017-02-12 10:25:49 -0800101 /*
102 * check for "pinmux" property in each subnode (e.g. pins1 and pins2 for
103 * usart1) of pin controller phandle "pinctrl-0"
104 * */
Christophe Kerelloa466d212017-06-20 17:04:18 +0200105 fdt_for_each_subnode(offset, gd->fdt_blob, offset) {
Vikas Manocha07e9e412017-02-12 10:25:49 -0800106 struct stm32_gpio_dsc gpio_dsc;
107 struct stm32_gpio_ctl gpio_ctl;
108 int i;
109
Christophe Kerelloa466d212017-06-20 17:04:18 +0200110 len = fdtdec_get_int_array_count(gd->fdt_blob, offset,
Vikas Manocha07e9e412017-02-12 10:25:49 -0800111 "pinmux", pin_mux,
112 ARRAY_SIZE(pin_mux));
Christophe Kerelloa466d212017-06-20 17:04:18 +0200113 debug("%s: no of pinmux entries= %d\n", __func__, len);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800114 if (len < 0)
115 return -EINVAL;
116 for (i = 0; i < len; i++) {
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700117 struct gpio_desc desc;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800118 debug("%s: pinmux = %x\n", __func__, *(pin_mux + i));
119 prep_gpio_dsc(&gpio_dsc, *(pin_mux + i));
Christophe Kerelloa466d212017-06-20 17:04:18 +0200120 prep_gpio_ctl(&gpio_ctl, *(pin_mux + i), offset);
Vikas Manocha1a8fde72017-04-10 15:02:59 -0700121 rv = uclass_get_device_by_seq(UCLASS_GPIO,
122 gpio_dsc.port, &desc.dev);
123 if (rv)
124 return rv;
125 desc.offset = gpio_dsc.pin;
126 rv = stm32_gpio_config(&desc, &gpio_ctl);
Vikas Manocha07e9e412017-02-12 10:25:49 -0800127 debug("%s: rv = %d\n\n", __func__, rv);
128 if (rv)
129 return rv;
130 }
Christophe Kerelloa466d212017-06-20 17:04:18 +0200131 }
132
133 return 0;
134}
135
136static int stm32_pinctrl_set_state_simple(struct udevice *dev,
137 struct udevice *periph)
138{
139 const void *fdt = gd->fdt_blob;
140 const fdt32_t *list;
141 uint32_t phandle;
142 int config_node;
143 int size, i, ret;
144
145 list = fdt_getprop(fdt, dev_of_offset(periph), "pinctrl-0", &size);
146 if (!list)
147 return -EINVAL;
148
149 debug("%s: periph->name = %s\n", __func__, periph->name);
150
151 size /= sizeof(*list);
152 for (i = 0; i < size; i++) {
153 phandle = fdt32_to_cpu(*list++);
154
155 config_node = fdt_node_offset_by_phandle(fdt, phandle);
156 if (config_node < 0) {
157 error("prop pinctrl-0 index %d invalid phandle\n", i);
158 return -EINVAL;
159 }
160
161 ret = stm32_pinctrl_config(config_node);
162 if (ret)
163 return ret;
Vikas Manocha07e9e412017-02-12 10:25:49 -0800164 }
165
166 return 0;
167}
168
169static struct pinctrl_ops stm32_pinctrl_ops = {
170 .set_state_simple = stm32_pinctrl_set_state_simple,
171};
172
173static const struct udevice_id stm32_pinctrl_ids[] = {
174 { .compatible = "st,stm32f746-pinctrl" },
175 { }
176};
177
178U_BOOT_DRIVER(pinctrl_stm32) = {
179 .name = "pinctrl_stm32",
180 .id = UCLASS_PINCTRL,
181 .of_match = stm32_pinctrl_ids,
182 .ops = &stm32_pinctrl_ops,
183 .bind = dm_scan_fdt_dev,
184};