blob: 078ced6e551b19b8a5724ad0368ea05b3e033c0d [file] [log] [blame]
Angel Ponsba38f372020-04-05 15:46:45 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkki13f66502019-03-03 08:01:05 +02002
3#include <device/mmio.h>
Subrata Baniked7275f2015-08-22 10:36:41 +05304#include <gpio.h>
Lee Leahy32471722015-04-20 15:20:28 -07005#include <console/console.h>
6#include <soc/gpio.h>
7
8/*
9 * Return family number and internal pad number in that community by pad number
10 * and which community it is in.
11 */
12uint16_t gpio_family_number(uint8_t community, uint8_t pad)
13{
14 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +010015 * Refer to BSW BIOS Writers Guide, Table "Family Number". BSW has 4 GPIO communities.
16 * Each community has up to 7 families and each family contains a range of Pad numbers.
17 * The number in the array is the maximum no. of that range.
Lee Leahy32471722015-04-20 15:20:28 -070018 * For example: East community, family 0, Pad 0~11.
19 */
20 static const uint8_t community_base[GPIO_COMMUNITY_COUNT]
21 [GPIO_FAMILIES_MAX_PER_COMM + 1] = {
22 {0, 8, 16, 24, 32, 40, 48, 56}, /* Southwest */
23 {0, 9, 22, 34, 46, 59, 59, 59}, /* North */
24 {0, 12, 24, 24, 24, 24, 24, 24}, /* East */
25 {0, 8, 20, 26, 34, 44, 55, 55} /* Southeast */
26 };
27 const uint8_t *base;
28 uint8_t i;
29
30 /* Validate the pad number */
31 if (pad > community_base[community][7])
32 die("Pad number is out of range!");
33
34 /* Locate the family number for the pad */
35 base = &community_base[community][0];
36 for (i = 0; i < 7; i++) {
37 if ((pad >= base[0]) && (pad < base[1]))
38 break;
39 base++;
40 }
41
42 /* Family number in high byte and inner pad number in lowest byte */
43 return (i << 8) + pad - *base;
44}
45
46/*
Angel Ponsaee7ab22020-03-19 00:31:58 +010047 * Return pad configuration register offset by pad number and which community it is in.
Lee Leahy32471722015-04-20 15:20:28 -070048 */
49uint32_t *gpio_pad_config_reg(uint8_t community, uint8_t pad)
50{
51 uint16_t fpad;
52 uint32_t *pad_config_reg;
53
54 /* Get the GPIO family number */
55 fpad = gpio_family_number(community, pad);
56
57 /*
Angel Ponsaee7ab22020-03-19 00:31:58 +010058 * Refer to BSW BIOS Writers Guide, Table "Per Pad Memory Space Registers Addresses"
59 * for the Pad configuration register calculation.
Lee Leahy32471722015-04-20 15:20:28 -070060 */
Angel Ponsaee7ab22020-03-19 00:31:58 +010061 pad_config_reg = (uint32_t *)(COMMUNITY_BASE(community) + FAMILY_PAD_REGS_OFF +
62 (FAMILY_PAD_REGS_SIZE * (fpad >> 8)) + (GPIO_REGS_SIZE * (fpad & 0xff)));
Lee Leahy32471722015-04-20 15:20:28 -070063
64 return pad_config_reg;
65}
66
Subrata Baniked7275f2015-08-22 10:36:41 +053067static int gpio_get_community_num(gpio_t gpio_num, int *pad)
68{
69 int comm = 0;
70
71 if (gpio_num >= GP_SW_00 && gpio_num <= GP_SW_97) {
72 comm = GP_SOUTHWEST;
73 *pad = gpio_num % GP_SOUTHWEST_COUNT;
Angel Ponsaee7ab22020-03-19 00:31:58 +010074
Subrata Baniked7275f2015-08-22 10:36:41 +053075 } else if (gpio_num >= GP_NC_00 && gpio_num <= GP_NC_72) {
76 comm = GP_NORTH;
77 *pad = gpio_num % GP_SOUTHWEST_COUNT;
Angel Ponsaee7ab22020-03-19 00:31:58 +010078
Subrata Baniked7275f2015-08-22 10:36:41 +053079 } else if (gpio_num >= GP_E_00 && gpio_num <= GP_E_26) {
80 comm = GP_EAST;
Angel Ponsaee7ab22020-03-19 00:31:58 +010081 *pad = gpio_num % (GP_SOUTHWEST_COUNT + GP_NORTH_COUNT);
82
Subrata Baniked7275f2015-08-22 10:36:41 +053083 } else {
84 comm = GP_SOUTHEAST;
Angel Ponsaee7ab22020-03-19 00:31:58 +010085 *pad = gpio_num % (GP_SOUTHWEST_COUNT + GP_NORTH_COUNT + GP_EAST_COUNT);
Subrata Baniked7275f2015-08-22 10:36:41 +053086 }
87 return comm;
88}
89
90static void gpio_config_pad(gpio_t gpio_num, const struct soc_gpio_map *cfg)
91{
92 int comm = 0;
Lee Leahy6598b912017-03-16 17:30:09 -070093 int pad_num = 0;
Subrata Baniked7275f2015-08-22 10:36:41 +053094 uint32_t *pad_config0_reg;
95 uint32_t *pad_config1_reg;
Subrata Baniked7275f2015-08-22 10:36:41 +053096
Angel Ponsaee7ab22020-03-19 00:31:58 +010097 if (gpio_num > MAX_GPIO_CNT)
Subrata Baniked7275f2015-08-22 10:36:41 +053098 return;
99 /* Get GPIO Community based on GPIO_NUMBER */
100 comm = gpio_get_community_num(gpio_num, &pad_num);
101 /* CONF0 */
102 pad_config0_reg = gpio_pad_config_reg(comm, pad_num);
103 /* CONF1 */
104 pad_config1_reg = pad_config0_reg + 1;
105
106 write32(pad_config0_reg, cfg->pad_conf0);
107 write32(pad_config1_reg, cfg->pad_conf1);
108}
109
110void gpio_input_pullup(gpio_t gpio_num)
111{
112 struct soc_gpio_map cfg = GPIO_INPUT_PU_20K;
113 gpio_config_pad(gpio_num, &cfg);
114}
115
116void gpio_input_pulldown(gpio_t gpio_num)
117{
118 struct soc_gpio_map cfg = GPIO_INPUT_PD_20K;
119 gpio_config_pad(gpio_num, &cfg);
120}
121
122void gpio_input(gpio_t gpio_num)
123{
124 struct soc_gpio_map cfg = GPIO_INPUT_NO_PULL;
125 gpio_config_pad(gpio_num, &cfg);
126}
127
128int gpio_get(gpio_t gpio_num)
129{
130 int comm = 0;
Lee Leahy6598b912017-03-16 17:30:09 -0700131 int pad_num = 0;
Subrata Baniked7275f2015-08-22 10:36:41 +0530132 uint32_t *pad_config0_reg;
133 u32 pad_value;
Subrata Baniked7275f2015-08-22 10:36:41 +0530134
Angel Ponsaee7ab22020-03-19 00:31:58 +0100135 if (gpio_num > MAX_GPIO_CNT)
Subrata Baniked7275f2015-08-22 10:36:41 +0530136 return -1;
137
138 /* Get GPIO Community based on GPIO_NUMBER */
139 comm = gpio_get_community_num(gpio_num, &pad_num);
140 /* CONF0 */
141 pad_config0_reg = gpio_pad_config_reg(comm, pad_num);
142
143 pad_value = read32(pad_config0_reg);
144
145 return pad_value & PAD_RX_BIT;
146}
147
Lee Leahy32471722015-04-20 15:20:28 -0700148int get_gpio(int community_base, int pad0_offset)
149{
150 return (read32((void *)(community_base + pad0_offset))) & PAD_RX_BIT;
151}