coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
gpio.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <console/console.h>
4 #include <arch/io.h>
5 #include <device/mmio.h>
6 #include <device/pci.h>
7 #include <soc/gpio.h>
8 #include <soc/pm.h>
9 #include <soc/smm.h>
10 
11 #define GPIO_DEBUG
12 
13 /* gpio map to pad number LUTs */
14 
16  0, 1, 2, 3, 4, 5, 6, 7, 8, 15,
17  16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
18  26, 27, 30, 31, 32, 33, 34, 35, 36, 37,
19  38, 39, 40, 41, 45, 46, 47, 48, 49, 50,
20  51, 52, 53, 54, 55, 56, 60, 61, 62, 63,
21  64, 65, 66, 67, 68, 69, 70, 71, 72 };
22 
24  0, 1, 2, 3, 4, 5, 6, 7, 15, 16,
25  17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
26  30, 31, 32, 33, 34, 35, 45, 46, 47, 48,
27  49, 50, 51, 52, 60, 61, 62, 63, 64, 65,
28  66, 67, 68, 69, 75, 76, 77, 78, 79, 80,
29  81, 82, 83, 84, 85 };
30 
32  0, 1, 2, 3, 4, 5, 6, 7, 15, 16,
33  17, 18, 19, 20, 21, 22, 30, 31, 32, 33,
34  34, 35, 36, 37, 45, 46, 47, 48, 49, 50,
35  51, 52, 60, 61, 62, 63, 64, 65, 66, 67,
36  75, 76, 77, 78, 79, 80, 81, 82, 90, 91,
37  92, 93, 94, 95, 96, 97 };
38 
40  0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
41  10, 11, 15, 16, 17, 18, 19, 20, 21, 22,
42  23, 24, 25, 26 };
43 
44 /* GPIO Community descriptions */
45 static const struct gpio_bank gpnorth_community = {
47  .gpio_to_pad = gpncommunity_gpio_to_pad,
48  .pad_base = COMMUNITY_GPNORTH_BASE,
49  .has_gpe_en = GPE_CAPABLE,
50  .has_wake_en = 1,
51 };
52 
53 static const struct gpio_bank gpsoutheast_community = {
55  .gpio_to_pad = gpsecommunity_gpio_to_pad,
56  .pad_base = COMMUNITY_GPSOUTHEAST_BASE,
57  .has_gpe_en = GPE_CAPABLE_NONE,
58  .has_wake_en = 1,
59 };
60 
61 static const struct gpio_bank gpsouthwest_community = {
63  .gpio_to_pad = gpswcommunity_gpio_to_pad,
64  .pad_base = COMMUNITY_GPSOUTHWEST_BASE,
65  .has_gpe_en = GPE_CAPABLE,
66  .has_wake_en = 1,
67 };
68 
69 static const struct gpio_bank gpeast_community = {
71  .gpio_to_pad = gpecommunity_gpio_to_pad,
72  .pad_base = COMMUNITY_GPEAST_BASE,
73  .has_gpe_en = GPE_CAPABLE_NONE,
74  .has_wake_en = 1,
75 };
76 
77 static void setup_gpio_route(const struct soc_gpio_map *sw_gpios,
78  const struct soc_gpio_map *n_gpios)
79 {
80  const struct soc_gpio_map *n_config;
81  const struct soc_gpio_map *sw_config;
82  uint32_t route_reg = 0;
83  uint32_t int_selection = 0;
84  uint32_t alt_gpio_smi = 0;
85  uint32_t gpe0a_en = 0;
86  int gpio = 0;
87  int north_done = 0;
88  int south_done = 0;
89 
90  for (sw_config = sw_gpios, n_config = n_gpios;
91  (!north_done || !south_done); sw_config++, n_config++, gpio++) {
92 
93  /* When north config is done */
94  if ((gpio > GP_NORTH_COUNT) || (n_config->pad_conf0 == GPIO_LIST_END))
95  north_done = 1;
96 
97  /* When southwest config is done */
98  if ((gpio > GP_SOUTHWEST_COUNT) || (sw_config->pad_conf0 == GPIO_LIST_END))
99  south_done = 1;
100 
101  /* Route north gpios */
102  if (!north_done) {
103  /* Int select from 8 to 15 */
104  int_selection = ((n_config->pad_conf0 >> 28) & 0xf);
105 
106  if (n_config->gpe == SMI) {
107  /* Set the corresponding bits (01) as per the interrupt line */
108  route_reg |= (1 << ((int_selection - 8) * 2));
109 
110  /* Reset the higher bit */
111  route_reg &= ~(1 << ((int_selection - 8) * 2 + 1));
112  alt_gpio_smi |= (1 << (int_selection + 8));
113 
114  } else if (n_config->gpe == SCI) {
115  /* Set the corresponding bits as per the interrupt line */
116  route_reg |= (1 << (((int_selection - 8) * 2) + 1));
117 
118  /* Reset the bit */
119  route_reg &= ~(1 << ((int_selection - 8) * 2));
120  gpe0a_en |= (1 << (int_selection + 8));
121  }
122  }
123 
124  /* Route southwest gpios */
125  if (!south_done) {
126  /* Int select from 8 to 15 */
127  int_selection = ((sw_config->pad_conf0 >> 28) & 0xf);
128 
129  if (sw_config->gpe == SMI) {
130  /* Set the corresponding bits (10) as per the interrupt line */
131  route_reg |= (1 << (int_selection * 2));
132  route_reg &= ~(1 << (int_selection * 2 + 1));
133  alt_gpio_smi |= (1 << (int_selection + 16));
134 
135  } else if (sw_config->gpe == SCI) {
136  /* Set the corresponding bits as per the interrupt line */
137  route_reg |= (1 << ((int_selection * 2) + 1));
138 
139  /* Reset the bit */
140  route_reg &= ~(1 << (int_selection * 2));
141  gpe0a_en |= (1 << (int_selection + 16));
142  }
143  }
144  }
145 
146  /* Enable gpe bits in GPE0A_EN_REG */
147  outl(gpe0a_en, ACPI_BASE_ADDRESS + GPE0A_EN_REG);
148 
149 #ifdef GPIO_DEBUG
150  printk(BIOS_DEBUG, "gpio_rout = %x alt_gpio_smi = %x gpe0a_en = %x\n",
151  route_reg, alt_gpio_smi, gpe0a_en);
152 #endif
153  /* Save as an SMM param */
155 }
156 
157 static void setup_gpios(const struct soc_gpio_map *gpios, const struct gpio_bank *community)
158 {
159  const struct soc_gpio_map *config;
160  int gpio = 0;
161  u32 reg, family, internal_pad_num;
162  u32 mmio_addr, int_selection;
163  u32 gpio_wake0 = 0;
164  u32 gpio_wake1 = 0;
165  u32 gpio_int_mask = 0;
166 
167  if (!gpios)
168  return;
169 
170  for (config = gpios; config->pad_conf0 != GPIO_LIST_END; config++, gpio++) {
171  if (gpio > community->gpio_count)
172  break;
173 
174  /* Pad configuration registers */
175  family = community->gpio_to_pad[gpio] / MAX_FAMILY_PAD_GPIO_NO;
176  internal_pad_num = community->gpio_to_pad[gpio] % MAX_FAMILY_PAD_GPIO_NO;
177 
178  /*
179  * Calculate the MMIO Address for GPIO pin control register pointed by index.
180  * REG = IOBASE + COMMUNITY_BASE + 0x4400 + (0x400 * FAMILY_NUM) + (8 * PAD_NUM)
181  */
182  mmio_addr = FAMILY_PAD_REGS_OFF + (FAMILY_PAD_REGS_SIZE * family) +
183  (GPIO_REGS_SIZE * internal_pad_num);
184 
185  reg = community->pad_base + mmio_addr;
186 
187  /* Get int selection value */
188  int_selection = ((config->pad_conf0 >> 28) & 0xf);
189 
190  /* Get int mask register value */
191  gpio_int_mask |= (config->int_mask << int_selection);
192 
193  /* Wake capable programming, some communities have 2 wake regs */
194  if (gpio > 31)
195  gpio_wake1 |= config->wake_mask << (gpio % 32);
196  else
197  gpio_wake0 |= config->wake_mask << gpio;
198 
199  if (!config->skip_config) {
200 #ifdef GPIO_DEBUG
202  "Write Pad: Base(%x) - conf0 = %x conf1= %x gpio #- %d pad # = %d\n",
203  reg, config->pad_conf0, config->pad_conf1,
204  community->gpio_to_pad[gpio], gpio);
205 #endif
206  /* Write pad configurations to conf0 and conf1 register */
207  write32((void *)(reg + PAD_CONF0_REG), config->pad_conf0);
208  write32((void *)(reg + PAD_CONF1_REG), config->pad_conf1);
209  }
210  }
211 
212 #ifdef GPIO_DEBUG
213  printk(BIOS_DEBUG, "gpio_wake_mask0 = %x gpio_wake_mask1 = %x gpio_int_mask = %x\n",
214  gpio_wake0, gpio_wake1, gpio_int_mask);
215 #endif
216 
217  /* Wake */
218  write32((void *)(community->pad_base + GPIO_WAKE_MASK_REG0), gpio_wake0);
219 
220  /* Wake mask config for communities with 2 regs */
221  if (community->gpio_count > 32)
222  write32((void *)(community->pad_base + GPIO_WAKE_MASK_REG1), gpio_wake1);
223 
224  /* Interrupt */
225  write32((void *)(community->pad_base + GPIO_INTERRUPT_MASK), gpio_int_mask);
226 }
227 
228 void setup_soc_gpios(struct soc_gpio_config *config, u8 enable_xdp_tap)
229 {
230  if (config) {
231 
232  /*
233  * Write the default value 0xffffff to the SW write_access_policy_interrupt_reg
234  * to allow the SW interrupt mask register to be set
235  */
236  write32((void *)(COMMUNITY_GPSOUTHWEST_BASE + 0x108), 0xffffffff);
237 
238  printk(BIOS_DEBUG, "north\n");
240 
241  printk(BIOS_DEBUG, "southwest\n");
243 
244  printk(BIOS_DEBUG, "southeast\n");
246 
247  printk(BIOS_DEBUG, "east\n");
249 
250  printk(BIOS_DEBUG, "Routing SW and N gpios\n");
251  setup_gpio_route(config->southwest, config->north);
252  }
253 
254  /*
255  * Set on die termination feature with pull up value
256  * and drive the pad high for TAP_TDO and TAP_TMS
257  */
258  if (!enable_xdp_tap)
259  printk(BIOS_DEBUG, "Tri-state TDO and TMS\n");
260 }
261 
263 {
264  printk(BIOS_DEBUG, "Default/empty GPIO config\n");
265  return NULL;
266 }
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
#define printk(level,...)
Definition: stdlib.h:16
void outl(u32 val, u16 port)
#define ACPI_BASE_ADDRESS
Definition: iomap.h:99
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
struct soc_gpio_config * mainboard_get_gpios(void)
Definition: gpio.c:207
enum board_config config
Definition: memory.c:448
const struct smm_save_state_ops *legacy_ops __weak
Definition: save_state.c:8
void setup_soc_gpios(struct soc_gpio_config *config, u8 enable_xdp_tap)
Definition: gpio.c:202
#define GPIO_LIST_END
Definition: gpio.h:300
#define PAD_CONF0_REG
Definition: gpio.h:23
#define PAD_CONF1_REG
Definition: gpio.h:24
void smm_southcluster_save_param(int param, uint32_t data)
Definition: smm.c:17
@ SMM_SAVE_PARAM_GPIO_ROUTE
Definition: smm.h:9
static const struct gpio_bank gpeast_community
Definition: gpio.c:69
static const u8 gpecommunity_gpio_to_pad[GP_EAST_COUNT]
Definition: gpio.c:39
static void setup_gpio_route(const struct soc_gpio_map *sw_gpios, const struct soc_gpio_map *n_gpios)
Definition: gpio.c:77
static const u8 gpswcommunity_gpio_to_pad[GP_SOUTHWEST_COUNT]
Definition: gpio.c:31
static const u8 gpncommunity_gpio_to_pad[GP_NORTH_COUNT]
Definition: gpio.c:15
static void setup_gpios(const struct soc_gpio_map *gpios, const struct gpio_bank *community)
Definition: gpio.c:157
static const struct gpio_bank gpsoutheast_community
Definition: gpio.c:53
static const u8 gpsecommunity_gpio_to_pad[GP_SOUTHEAST_COUNT]
Definition: gpio.c:23
static const struct gpio_bank gpnorth_community
Definition: gpio.c:45
static const struct gpio_bank gpsouthwest_community
Definition: gpio.c:61
#define GP_SOUTHEAST_COUNT
Definition: gpio.h:121
#define FAMILY_PAD_REGS_OFF
Definition: gpio.h:134
#define COMMUNITY_GPSOUTHEAST_BASE
Definition: gpio.h:23
#define GPIO_WAKE_MASK_REG1
Definition: gpio.h:105
#define COMMUNITY_GPNORTH_BASE
Definition: gpio.h:17
#define GPE0A_EN_REG
Definition: gpio.h:109
#define GPIO_REGS_SIZE
Definition: gpio.h:126
#define GPIO_WAKE_MASK_REG0
Definition: gpio.h:104
#define GPE_CAPABLE_NONE
Definition: gpio.h:131
#define GP_EAST_COUNT
Definition: gpio.h:120
#define MAX_FAMILY_PAD_GPIO_NO
Definition: gpio.h:133
@ SCI
Definition: gpio.h:441
@ SMI
Definition: gpio.h:440
#define GP_NORTH_COUNT
Definition: gpio.h:119
#define FAMILY_PAD_REGS_SIZE
Definition: gpio.h:135
#define GPE_CAPABLE
Definition: gpio.h:130
#define COMMUNITY_GPEAST_BASE
Definition: gpio.h:20
#define GPIO_INTERRUPT_MASK
Definition: gpio.h:107
#define COMMUNITY_GPSOUTHWEST_BASE
Definition: gpio.h:14
#define GP_SOUTHWEST_COUNT
Definition: gpio.h:118
#define NULL
Definition: stddef.h:19
unsigned int uint32_t
Definition: stdint.h:14
uint32_t u32
Definition: stdint.h:51
uint8_t u8
Definition: stdint.h:45
const u8 * gpio_to_pad
Definition: gpio.h:355
const unsigned long pad_base
Definition: gpio.h:357
const int gpio_count
Definition: gpio.h:354
Definition: pinmux.c:36
u32 pad_conf0
Definition: gpio.h:330
u32 gpe
Definition: gpio.h:348