]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/gpio/adi_gpio2.c
2 * ADI GPIO2 Abstraction Layer
3 * Support BF54x, BF60x and future processors.
5 * Copyright 2008-2013 Analog Devices Inc.
7 * Licensed under the GPL-2 or later
11 #include <asm/errno.h>
13 #include <asm/portmux.h>
15 static struct gpio_port_t
* const gpio_array
[] = {
16 (struct gpio_port_t
*)PORTA_FER
,
17 (struct gpio_port_t
*)PORTB_FER
,
18 (struct gpio_port_t
*)PORTC_FER
,
19 (struct gpio_port_t
*)PORTD_FER
,
20 (struct gpio_port_t
*)PORTE_FER
,
21 (struct gpio_port_t
*)PORTF_FER
,
22 (struct gpio_port_t
*)PORTG_FER
,
23 #if defined(CONFIG_BF54x)
24 (struct gpio_port_t
*)PORTH_FER
,
25 (struct gpio_port_t
*)PORTI_FER
,
26 (struct gpio_port_t
*)PORTJ_FER
,
30 #define RESOURCE_LABEL_SIZE 16
32 static struct str_ident
{
33 char name
[RESOURCE_LABEL_SIZE
];
34 } str_ident
[MAX_RESOURCES
];
36 static void gpio_error(unsigned gpio
)
38 printf("adi_gpio2: GPIO %d wasn't requested!\n", gpio
);
41 static void set_label(unsigned short ident
, const char *label
)
44 strncpy(str_ident
[ident
].name
, label
,
46 str_ident
[ident
].name
[RESOURCE_LABEL_SIZE
- 1] = 0;
50 static char *get_label(unsigned short ident
)
52 return *str_ident
[ident
].name
? str_ident
[ident
].name
: "UNKNOWN";
55 static int cmp_label(unsigned short ident
, const char *label
)
58 printf("adi_gpio2: please provide none-null label\n");
61 return strcmp(str_ident
[ident
].name
, label
);
66 #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
67 #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
68 #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
69 #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
70 #define DECLARE_RESERVED_MAP(m, c) unsigned short reserved_##m##_map[c]
72 static DECLARE_RESERVED_MAP(gpio
, GPIO_BANK_NUM
);
73 static DECLARE_RESERVED_MAP(peri
, gpio_bank(MAX_RESOURCES
));
75 inline int check_gpio(unsigned gpio
)
77 #if defined(CONFIG_BF54x)
78 if (gpio
== GPIO_PB15
|| gpio
== GPIO_PC14
|| gpio
== GPIO_PC15
||
79 gpio
== GPIO_PH14
|| gpio
== GPIO_PH15
||
80 gpio
== GPIO_PJ14
|| gpio
== GPIO_PJ15
)
83 if (gpio
>= MAX_GPIOS
)
88 static void port_setup(unsigned gpio
, unsigned short usage
)
90 #if defined(CONFIG_BF54x)
91 if (usage
== GPIO_USAGE
)
92 gpio_array
[gpio_bank(gpio
)]->port_fer
&= ~gpio_bit(gpio
);
94 gpio_array
[gpio_bank(gpio
)]->port_fer
|= gpio_bit(gpio
);
96 if (usage
== GPIO_USAGE
)
97 gpio_array
[gpio_bank(gpio
)]->port_fer_clear
= gpio_bit(gpio
);
99 gpio_array
[gpio_bank(gpio
)]->port_fer_set
= gpio_bit(gpio
);
104 inline void portmux_setup(unsigned short per
)
107 u16 ident
= P_IDENT(per
);
108 u16 function
= P_FUNCT2MUX(per
);
110 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
112 pmux
&= ~(0x3 << (2 * gpio_sub_n(ident
)));
113 pmux
|= (function
& 0x3) << (2 * gpio_sub_n(ident
));
115 gpio_array
[gpio_bank(ident
)]->port_mux
= pmux
;
118 inline u16
get_portmux(unsigned short per
)
121 u16 ident
= P_IDENT(per
);
123 pmux
= gpio_array
[gpio_bank(ident
)]->port_mux
;
125 return pmux
>> (2 * gpio_sub_n(ident
)) & 0x3;
128 unsigned short get_gpio_dir(unsigned gpio
)
131 (gpio_array
[gpio_bank(gpio
)]->dir_clear
>> gpio_sub_n(gpio
));
134 /***********************************************************
136 * FUNCTIONS: Peripheral Resource Allocation
140 * per Peripheral Identifier
143 * DESCRIPTION: Peripheral Resource Allocation and Setup API
144 **************************************************************/
146 int peripheral_request(unsigned short per
, const char *label
)
148 unsigned short ident
= P_IDENT(per
);
151 * Don't cares are pins with only one dedicated function
154 if (per
& P_DONTCARE
)
157 if (!(per
& P_DEFINED
))
160 BUG_ON(ident
>= MAX_RESOURCES
);
162 /* If a pin can be muxed as either GPIO or peripheral, make
163 * sure it is not already a GPIO pin when we request it.
165 if (unlikely(!check_gpio(ident
) && is_reserved(gpio
, ident
, 1))) {
166 printf("%s: Peripheral %d is already reserved as GPIO by %s!\n",
167 __func__
, ident
, get_label(ident
));
171 if (unlikely(is_reserved(peri
, ident
, 1))) {
173 * Pin functions like AMC address strobes my
174 * be requested and used by several drivers
177 if (!((per
& P_MAYSHARE
) &&
178 get_portmux(per
) == P_FUNCT2MUX(per
))) {
180 * Allow that the identical pin function can
181 * be requested from the same driver twice
184 if (cmp_label(ident
, label
) == 0)
187 printf("%s: Peripheral %d function %d is already "
188 "reserved by %s!\n", __func__
, ident
,
189 P_FUNCT2MUX(per
), get_label(ident
));
195 reserve(peri
, ident
);
198 port_setup(ident
, PERIPHERAL_USAGE
);
200 set_label(ident
, label
);
205 int peripheral_request_list(const unsigned short per
[], const char *label
)
210 for (cnt
= 0; per
[cnt
] != 0; cnt
++) {
211 ret
= peripheral_request(per
[cnt
], label
);
214 for (; cnt
> 0; cnt
--)
215 peripheral_free(per
[cnt
- 1]);
224 void peripheral_free(unsigned short per
)
226 unsigned short ident
= P_IDENT(per
);
228 if (per
& P_DONTCARE
)
231 if (!(per
& P_DEFINED
))
234 if (unlikely(!is_reserved(peri
, ident
, 0)))
237 if (!(per
& P_MAYSHARE
))
238 port_setup(ident
, GPIO_USAGE
);
240 unreserve(peri
, ident
);
242 set_label(ident
, "free");
245 void peripheral_free_list(const unsigned short per
[])
248 for (cnt
= 0; per
[cnt
] != 0; cnt
++)
249 peripheral_free(per
[cnt
]);
252 /***********************************************************
254 * FUNCTIONS: GPIO Driver
257 * gpio PIO Number between 0 and MAX_GPIOS
260 * DESCRIPTION: GPIO Driver API
261 **************************************************************/
263 int gpio_request(unsigned gpio
, const char *label
)
265 if (check_gpio(gpio
) < 0)
269 * Allow that the identical GPIO can
270 * be requested from the same driver twice
271 * Do nothing and return -
274 if (cmp_label(gpio
, label
) == 0)
277 if (unlikely(is_reserved(gpio
, gpio
, 1))) {
278 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
279 gpio
, get_label(gpio
));
282 if (unlikely(is_reserved(peri
, gpio
, 1))) {
283 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
284 "by %s!\n", gpio
, get_label(gpio
));
289 set_label(gpio
, label
);
291 port_setup(gpio
, GPIO_USAGE
);
296 int gpio_free(unsigned gpio
)
298 if (check_gpio(gpio
) < 0)
301 if (unlikely(!is_reserved(gpio
, gpio
, 0))) {
306 unreserve(gpio
, gpio
);
308 set_label(gpio
, "free");
313 #ifdef ADI_SPECIAL_GPIO_BANKS
314 static DECLARE_RESERVED_MAP(special_gpio
, gpio_bank(MAX_RESOURCES
));
316 int special_gpio_request(unsigned gpio
, const char *label
)
319 * Allow that the identical GPIO can
320 * be requested from the same driver twice
321 * Do nothing and return -
324 if (cmp_label(gpio
, label
) == 0)
327 if (unlikely(is_reserved(special_gpio
, gpio
, 1))) {
328 printf("adi_gpio2: GPIO %d is already reserved by %s!\n",
329 gpio
, get_label(gpio
));
332 if (unlikely(is_reserved(peri
, gpio
, 1))) {
333 printf("adi_gpio2: GPIO %d is already reserved as Peripheral "
334 "by %s!\n", gpio
, get_label(gpio
));
339 reserve(special_gpio
, gpio
);
342 set_label(gpio
, label
);
343 port_setup(gpio
, GPIO_USAGE
);
348 void special_gpio_free(unsigned gpio
)
350 if (unlikely(!is_reserved(special_gpio
, gpio
, 0))) {
355 unreserve(special_gpio
, gpio
);
356 unreserve(peri
, gpio
);
357 set_label(gpio
, "free");
361 static inline void __gpio_direction_input(unsigned gpio
)
363 gpio_array
[gpio_bank(gpio
)]->dir_clear
= gpio_bit(gpio
);
364 #if defined(CONFIG_BF54x)
365 gpio_array
[gpio_bank(gpio
)]->inen
|= gpio_bit(gpio
);
367 gpio_array
[gpio_bank(gpio
)]->inen_set
= gpio_bit(gpio
);
371 int gpio_direction_input(unsigned gpio
)
375 if (!is_reserved(gpio
, gpio
, 0)) {
380 local_irq_save(flags
);
381 __gpio_direction_input(gpio
);
382 local_irq_restore(flags
);
387 int gpio_set_value(unsigned gpio
, int arg
)
390 gpio_array
[gpio_bank(gpio
)]->data_set
= gpio_bit(gpio
);
392 gpio_array
[gpio_bank(gpio
)]->data_clear
= gpio_bit(gpio
);
397 int gpio_direction_output(unsigned gpio
, int value
)
401 if (!is_reserved(gpio
, gpio
, 0)) {
406 local_irq_save(flags
);
408 #if defined(CONFIG_BF54x)
409 gpio_array
[gpio_bank(gpio
)]->inen
&= ~gpio_bit(gpio
);
411 gpio_array
[gpio_bank(gpio
)]->inen_clear
= gpio_bit(gpio
);
413 gpio_set_value(gpio
, value
);
414 gpio_array
[gpio_bank(gpio
)]->dir_set
= gpio_bit(gpio
);
416 local_irq_restore(flags
);
421 int gpio_get_value(unsigned gpio
)
423 return 1 & (gpio_array
[gpio_bank(gpio
)]->data
>> gpio_sub_n(gpio
));
426 void gpio_labels(void)
430 for (c
= 0; c
< MAX_RESOURCES
; c
++) {
431 gpio
= is_reserved(gpio
, c
, 1);
432 if (!check_gpio(c
) && gpio
)
433 printf("GPIO_%d:\t%s\tGPIO %s\n", c
, get_label(c
),
434 get_gpio_dir(c
) ? "OUTPUT" : "INPUT");
435 else if (is_reserved(peri
, c
, 1))
436 printf("GPIO_%d:\t%s\tPeripheral\n", c
, get_label(c
));