Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2013 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <device/pci.h> |
| 17 | #include <console/console.h> |
Ben Gardner | fa6014a | 2015-12-08 21:20:25 -0600 | [diff] [blame] | 18 | #include <soc/gpio.h> |
| 19 | #include <soc/pmc.h> |
| 20 | #include <soc/smm.h> |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 21 | |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 22 | /* |
| 23 | * GPIO-to-Pad LUTs |
| 24 | * |
| 25 | * These tables translate the GPIO number to the pad configuration register |
| 26 | * for that GPIO in the memory-mapped pad configuration registers. |
| 27 | * See the tables: |
| 28 | * PCU iLB GPIO CFIO_SCORE Address Map |
| 29 | * PCU iLB GPIO CFIO_SSUS Address Map |
| 30 | */ |
Edward O'Callaghan | 735e10a | 2015-01-13 05:48:13 +1100 | [diff] [blame] | 31 | #ifndef __PRE_RAM__ |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 32 | static const u8 gpncore_gpio_to_pad[GPNCORE_COUNT] = |
| 33 | { 19, 18, 17, 20, 21, 22, 24, 25, /* [ 0: 7] */ |
| 34 | 23, 16, 14, 15, 12, 26, 27, 1, /* [ 8:15] */ |
| 35 | 4, 8, 11, 0, 3, 6, 10, 13, /* [16:23] */ |
| 36 | 2, 5, 9 }; /* [24:26] */ |
Edward O'Callaghan | 735e10a | 2015-01-13 05:48:13 +1100 | [diff] [blame] | 37 | #endif |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 38 | |
| 39 | static const u8 gpscore_gpio_to_pad[GPSCORE_COUNT] = |
| 40 | { 85, 89, 93, 96, 99, 102, 98, 101, /* [ 0: 7] */ |
| 41 | 34, 37, 36, 38, 39, 35, 40, 84, /* [ 8: 15] */ |
| 42 | 62, 61, 64, 59, 54, 56, 60, 55, /* [16: 23] */ |
| 43 | 63, 57, 51, 50, 53, 47, 52, 49, /* [24: 31] */ |
| 44 | 48, 43, 46, 41, 45, 42, 58, 44, /* [32: 39] */ |
| 45 | 95, 105, 70, 68, 67, 66, 69, 71, /* [40: 47] */ |
| 46 | 65, 72, 86, 90, 88, 92, 103, 77, /* [48: 55] */ |
| 47 | 79, 83, 78, 81, 80, 82, 13, 12, /* [56: 63] */ |
| 48 | 15, 14, 17, 18, 19, 16, 2, 1, /* [64: 71] */ |
| 49 | 0, 4, 6, 7, 9, 8, 33, 32, /* [72: 79] */ |
| 50 | 31, 30, 29, 27, 25, 28, 26, 23, /* [80: 87] */ |
| 51 | 21, 20, 24, 22, 5, 3, 10, 11, /* [88: 95] */ |
| 52 | 106, 87, 91, 104, 97, 100 }; /* [96:101] */ |
| 53 | |
| 54 | static const u8 gpssus_gpio_to_pad[GPSSUS_COUNT] = |
| 55 | { 29, 33, 30, 31, 32, 34, 36, 35, /* [ 0: 7] */ |
| 56 | 38, 37, 18, 7, 11, 20, 17, 1, /* [ 8:15] */ |
| 57 | 8, 10, 19, 12, 0, 2, 23, 39, /* [16:23] */ |
| 58 | 28, 27, 22, 21, 24, 25, 26, 51, /* [24:31] */ |
| 59 | 56, 54, 49, 55, 48, 57, 50, 58, /* [32:39] */ |
| 60 | 52, 53, 59, 40 }; /* [40:43] */ |
| 61 | |
Edward O'Callaghan | 735e10a | 2015-01-13 05:48:13 +1100 | [diff] [blame] | 62 | |
| 63 | #ifndef __PRE_RAM__ |
| 64 | |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 65 | /* GPIO bank descriptions */ |
| 66 | static const struct gpio_bank gpncore_bank = { |
| 67 | .gpio_count = GPNCORE_COUNT, |
| 68 | .gpio_to_pad = gpncore_gpio_to_pad, |
| 69 | .legacy_base = GP_LEGACY_BASE_NONE, |
| 70 | .pad_base = GPNCORE_PAD_BASE, |
| 71 | .has_wake_en = 0, |
| 72 | .gpio_f1_range_start = GPNCORE_GPIO_F1_RANGE_START, |
| 73 | .gpio_f1_range_end = GPNCORE_GPIO_F1_RANGE_END, |
| 74 | }; |
| 75 | |
| 76 | static const struct gpio_bank gpscore_bank = { |
| 77 | .gpio_count = GPSCORE_COUNT, |
| 78 | .gpio_to_pad = gpscore_gpio_to_pad, |
| 79 | .legacy_base = GPSCORE_LEGACY_BASE, |
| 80 | .pad_base = GPSCORE_PAD_BASE, |
| 81 | .has_wake_en = 0, |
| 82 | .gpio_f1_range_start = GPSCORE_GPIO_F1_RANGE_START, |
| 83 | .gpio_f1_range_end = GPSCORE_GPIO_F1_RANGE_END, |
| 84 | }; |
| 85 | |
| 86 | static const struct gpio_bank gpssus_bank = { |
| 87 | .gpio_count = GPSSUS_COUNT, |
| 88 | .gpio_to_pad = gpssus_gpio_to_pad, |
| 89 | .legacy_base = GPSSUS_LEGACY_BASE, |
| 90 | .pad_base = GPSSUS_PAD_BASE, |
| 91 | .has_wake_en = 1, |
| 92 | .gpio_f1_range_start = GPSSUS_GPIO_F1_RANGE_START, |
| 93 | .gpio_f1_range_end = GPSSUS_GPIO_F1_RANGE_END, |
| 94 | }; |
| 95 | |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 96 | |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 97 | static void setup_gpios(const struct soc_gpio_map *gpios, |
| 98 | const struct gpio_bank *bank) |
| 99 | { |
| 100 | const struct soc_gpio_map *config; |
| 101 | int gpio = 0; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 102 | u32 reg, pad_conf0, *regmmio; |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 103 | u8 set, bit; |
| 104 | |
| 105 | u32 use_sel[4] = {0}; |
| 106 | u32 io_sel[4] = {0}; |
| 107 | u32 gp_lvl[4] = {0}; |
| 108 | u32 tpe[4] = {0}; |
| 109 | u32 tne[4] = {0}; |
| 110 | u32 wake_en[4] = {0}; |
| 111 | |
| 112 | if (!gpios) |
| 113 | return; |
| 114 | |
| 115 | for (config = gpios; config->pad_conf0 != GPIO_LIST_END; |
| 116 | config++, gpio++) { |
| 117 | if (gpio > bank->gpio_count) |
| 118 | break; |
| 119 | |
| 120 | set = gpio >> 5; |
| 121 | bit = gpio % 32; |
| 122 | |
| 123 | if (bank->legacy_base != GP_LEGACY_BASE_NONE) { |
| 124 | /* Legacy IO configuration */ |
| 125 | use_sel[set] |= config->use_sel << bit; |
| 126 | io_sel[set] |= config->io_sel << bit; |
| 127 | gp_lvl[set] |= config->gp_lvl << bit; |
| 128 | tpe[set] |= config->tpe << bit; |
| 129 | tne[set] |= config->tne << bit; |
| 130 | |
| 131 | /* Some banks do not have wake_en ability */ |
| 132 | if (bank->has_wake_en) |
| 133 | wake_en[set] |= config->wake_en << bit; |
| 134 | } |
| 135 | |
| 136 | /* Pad configuration registers */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 137 | regmmio = (u32 *)(bank->pad_base + 16 * |
| 138 | bank->gpio_to_pad[gpio]); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 139 | |
| 140 | /* Add correct func to GPIO pad config */ |
| 141 | pad_conf0 = config->pad_conf0; |
| 142 | if (config->is_gpio) |
| 143 | { |
| 144 | if (gpio >= bank->gpio_f1_range_start && |
| 145 | gpio <= bank->gpio_f1_range_end) |
| 146 | pad_conf0 |= PAD_FUNC1; |
| 147 | else |
| 148 | pad_conf0 |= PAD_FUNC0; |
| 149 | } |
| 150 | |
| 151 | #ifdef GPIO_DEBUG |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 152 | printk(BIOS_DEBUG, "Write Pad: Base(%p) - %x %x %x\n", |
| 153 | regmmio, pad_conf0, config->pad_conf1, config->pad_val); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 154 | #endif |
| 155 | |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 156 | write32(regmmio + (PAD_CONF0_REG/sizeof(u32)), pad_conf0); |
| 157 | write32(regmmio + (PAD_CONF1_REG/sizeof(u32)), |
| 158 | config->pad_conf1); |
| 159 | write32(regmmio + (PAD_VAL_REG/sizeof(u32)), config->pad_val); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (bank->legacy_base != GP_LEGACY_BASE_NONE) |
| 163 | for (set = 0; set <= (bank->gpio_count - 1) / 32; ++set) { |
| 164 | reg = bank->legacy_base + 0x20 * set; |
| 165 | |
| 166 | #ifdef GPIO_DEBUG |
| 167 | printk(BIOS_DEBUG, |
| 168 | "Write GPIO: Reg(%x) - %x %x %x %x %x\n", |
| 169 | reg, use_sel[set], io_sel[set], gp_lvl[set], |
| 170 | tpe[set], tne[set]); |
| 171 | #endif |
| 172 | |
| 173 | outl(use_sel[set], reg + LEGACY_USE_SEL_REG); |
| 174 | outl(io_sel[set], reg + LEGACY_IO_SEL_REG); |
| 175 | outl(gp_lvl[set], reg + LEGACY_GP_LVL_REG); |
| 176 | outl(tpe[set], reg + LEGACY_TPE_REG); |
| 177 | outl(tne[set], reg + LEGACY_TNE_REG); |
| 178 | |
| 179 | /* TS registers are WOC */ |
| 180 | outl(0, reg + LEGACY_TS_REG); |
| 181 | |
| 182 | if (bank->has_wake_en) |
| 183 | outl(wake_en[set], reg + LEGACY_WAKE_EN_REG); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static void setup_gpio_route(const struct soc_gpio_map *sus, |
| 188 | const struct soc_gpio_map *core) |
| 189 | { |
| 190 | uint32_t route_reg = 0; |
| 191 | int i; |
| 192 | |
| 193 | for (i = 0; i < 8; i++) { |
| 194 | /* SMI takes precedence and wake_en implies SCI. */ |
| 195 | if (sus[i].smi) { |
| 196 | route_reg |= ROUTE_SMI << (2 * i); |
| 197 | } else if (sus[i].sci) { |
| 198 | route_reg |= ROUTE_SCI << (2 * i); |
| 199 | } |
| 200 | |
| 201 | if (core[i].smi) { |
| 202 | route_reg |= ROUTE_SMI << (2 * (i + 8)); |
| 203 | } else if (core[i].sci) { |
| 204 | route_reg |= ROUTE_SCI << (2 * (i + 8)); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | #if IS_ENABLED(CONFIG_HAVE_SMI_HANDLER) |
| 209 | southcluster_smm_save_gpio_route(route_reg); |
| 210 | #endif |
| 211 | } |
| 212 | |
| 213 | static void setup_dirqs(const u8 dirq[GPIO_MAX_DIRQS], |
| 214 | const struct gpio_bank *bank) |
| 215 | { |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 216 | u32 *reg = (u32 *)(bank->pad_base + PAD_BASE_DIRQ_OFFSET); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 217 | u32 val; |
| 218 | int i; |
| 219 | |
| 220 | /* Write all four DIRQ registers */ |
| 221 | for (i=0; i<4; ++i) { |
| 222 | val = dirq[i * 4 + 3] << 24 | dirq[i * 4 + 2] << 16 | |
| 223 | dirq[i * 4 + 1] << 8 | dirq[i * 4]; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 224 | write32(reg + i, val); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 225 | #ifdef GPIO_DEBUG |
| 226 | printk(BIOS_DEBUG, "Write DIRQ reg(%x) - %x\n", |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 227 | reg + i, val); |
Martin Roth | 433659a | 2014-05-12 21:55:00 -0600 | [diff] [blame] | 228 | #endif |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | void setup_soc_gpios(struct soc_gpio_config *config) |
| 233 | { |
| 234 | if (config) { |
| 235 | setup_gpios(config->ncore, &gpncore_bank); |
| 236 | setup_gpios(config->score, &gpscore_bank); |
| 237 | setup_gpios(config->ssus, &gpssus_bank); |
| 238 | setup_gpio_route(config->ssus, config->score); |
| 239 | |
| 240 | if (config->core_dirq) |
| 241 | setup_dirqs(*config->core_dirq, &gpscore_bank); |
| 242 | if (config->sus_dirq) |
| 243 | setup_dirqs(*config->sus_dirq, &gpssus_bank); |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | struct soc_gpio_config* __attribute__((weak)) mainboard_get_gpios(void) |
| 249 | { |
| 250 | printk(BIOS_DEBUG, "Default/empty GPIO config\n"); |
| 251 | return NULL; |
| 252 | } |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 253 | #endif /* #ifndef __PRE_RAM__ */ |
| 254 | |
| 255 | /** \brief returns the input / output value from an SCORE GPIO |
| 256 | * |
| 257 | * @param gpio_num The GPIO number being read |
| 258 | * @return The current input or output value of the GPIO |
| 259 | */ |
| 260 | uint8_t read_score_gpio(uint8_t gpio_num) |
| 261 | { |
| 262 | uint8_t retval = 0; |
| 263 | if (gpio_num < GPSCORE_COUNT) |
| 264 | retval = score_get_gpio(gpscore_gpio_to_pad[gpio_num]); |
| 265 | |
| 266 | return retval; |
| 267 | } |
| 268 | |
Werner Zeh | 1e8a301 | 2015-11-06 08:38:38 +0100 | [diff] [blame] | 269 | /** \brief sets an output SCORE GPIO to desired value |
| 270 | * |
| 271 | * @param gpio_num The GPIO number being read |
| 272 | * @param val The value this output must be set to (0 or 1) |
| 273 | * @return void |
| 274 | */ |
| 275 | void write_score_gpio(uint8_t gpio_num, uint8_t val) |
| 276 | { |
| 277 | if (gpio_num < GPSCORE_COUNT) |
| 278 | score_set_gpio(gpscore_gpio_to_pad[gpio_num], val); |
| 279 | } |
| 280 | |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 281 | /** \brief returns the input / output value from an SSUS GPIO |
| 282 | * |
| 283 | * @param gpio_num The GPIO number being read |
| 284 | * @return The current input or output value of the GPIO |
| 285 | */ |
| 286 | uint8_t read_ssus_gpio(uint8_t gpio_num) |
| 287 | { |
| 288 | uint8_t retval = 0; |
| 289 | if (gpio_num < GPSSUS_COUNT) |
| 290 | retval = ssus_get_gpio(gpssus_gpio_to_pad[gpio_num]); |
| 291 | |
| 292 | return retval; |
| 293 | } |
| 294 | |
Werner Zeh | 1e8a301 | 2015-11-06 08:38:38 +0100 | [diff] [blame] | 295 | /** \brief sets an output SSUS GPIO to desired value |
| 296 | * |
| 297 | * @param gpio_num The GPIO number being read |
| 298 | * @param val The value this output must be set to (0 or 1) |
| 299 | * @return void |
| 300 | */ |
| 301 | void write_ssus_gpio(uint8_t gpio_num, uint8_t val) |
| 302 | { |
| 303 | if (gpio_num < GPSSUS_COUNT) |
| 304 | ssus_set_gpio(gpssus_gpio_to_pad[gpio_num], val); |
| 305 | } |
| 306 | |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 307 | /** \brief Sets up the function, pulls, and Input/Output of a Baytrail |
| 308 | * SSUS (S5) or SCORE (S0) GPIO |
| 309 | * |
| 310 | * @param ssus_gpio 1 if SSUS GPIO is being configured 0 if SCORE GPIO |
| 311 | * @param gpio_num The GPIO number being configured |
| 312 | * @param pconf0 function, pull direction, and pull value |
| 313 | * function: PAD_FUNC0 - PAD_FUNC7 |
| 314 | * pull assign: PAD_PULL_DISABLE / PAD_PULL_UP / PAD_PULL_DOWN |
| 315 | * pull_value: PAD_PU_2K / PAD_PU_10K / PAD_PU_20K / PAD_PU_40K |
| 316 | * @param pad_val input / output state and pad value |
| 317 | * io state: PAD_VAL_INPUT / PAD_VAL_OUTPUT |
| 318 | * pad value: PAD_VAL_HIGH / PAD_VAL_LOW |
| 319 | */ |
| 320 | static void configure_ssus_score_gpio(uint8_t ssus_gpio, uint8_t gpio_num, |
| 321 | uint32_t pconf0, uint32_t pad_val) |
| 322 | { |
| 323 | uint32_t reg; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 324 | uint32_t *pad_addr; |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 325 | if (ssus_gpio) |
| 326 | pad_addr = ssus_pconf0(gpssus_gpio_to_pad[gpio_num]); |
| 327 | else |
| 328 | pad_addr = score_pconf0(gpscore_gpio_to_pad[gpio_num]); |
| 329 | |
| 330 | if ((ssus_gpio && gpio_num >= GPSSUS_COUNT) || |
| 331 | (gpio_num >= GPSCORE_COUNT)){ |
| 332 | printk(BIOS_WARNING,"Warning: Invalid %s GPIO specified (%d)\n", |
| 333 | ssus_gpio ? "SSUS" : "SCORE", gpio_num); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Pad Configuration 0 Register |
| 339 | * 2:0 - func_pin_mux |
| 340 | * 8:7 - Pull assignment: 00 - Non pull 01 - Pull Up 10 - Pull down |
| 341 | * 11 - reserved |
| 342 | * 10:9 - Pull strength: 00 - 2K 01 - 10K 10 - 20K 11 - 40K |
| 343 | */ |
| 344 | reg = PAD_CONFIG0_DEFAULT; |
| 345 | reg |= pconf0 & 0x787; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 346 | write32(pad_addr + (PAD_CONF0_REG/sizeof(u32)), reg); |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 347 | |
| 348 | /* |
| 349 | * Pad Value Register |
| 350 | * 0: Pad value |
| 351 | * 1: output enable (0 is enabled) |
| 352 | * 2: input enable (0 is enabled) |
| 353 | */ |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 354 | reg = read32(pad_addr + (PAD_VAL_REG/sizeof(u32))); |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 355 | reg &= ~0x7; |
| 356 | reg |= pad_val & 0x7; |
Kevin Paul Herbert | bde6d30 | 2014-12-24 18:43:20 -0800 | [diff] [blame] | 357 | write32(pad_addr + (PAD_VAL_REG/sizeof(u32)), reg); |
Martin Roth | 52669ef | 2014-12-11 20:38:00 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | /** \brief Sets up the function, pulls, and Input/Output of a Baytrail S5 GPIO |
| 361 | * |
| 362 | */ |
| 363 | void configure_ssus_gpio(uint8_t gpio_num, uint32_t pconf0, uint32_t pad_val) |
| 364 | { |
| 365 | configure_ssus_score_gpio(1, gpio_num, pconf0, pad_val); |
| 366 | } |
| 367 | |
| 368 | /** \brief Sets up the function, pulls, and Input/Output of a Baytrail S5 GPIO |
| 369 | * |
| 370 | */ |
| 371 | void configure_score_gpio(uint8_t gpio_num, uint32_t pconf0, uint32_t pad_val) |
| 372 | { |
| 373 | configure_ssus_score_gpio(0, gpio_num, pconf0, pad_val); |
| 374 | } |