| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
| 3 | #ifndef _SOC_INTELBLOCKS_GPIO_H_ |
| 4 | #define _SOC_INTELBLOCKS_GPIO_H_ |
| 5 | |
| 6 | #include <soc/gpio.h> |
| 7 | #include "gpio_defs.h" |
| 8 | |
| 9 | /* GPIO community IOSF sideband VNNREQ/ACK handshake */ |
| 10 | #define MISCCFG_GPVNNREQEN (1 << 7) |
| 11 | /* GPIO community PGCB clock gating */ |
| 12 | #define MISCCFG_GPPGCBDPCGEN (1 << 6) |
| 13 | /* GPIO community IOSF sideband clock gating */ |
| 14 | #define MISCCFG_GPSIDEDPCGEN (1 << 5) |
| 15 | /* GPIO community RCOMP clock gating */ |
| 16 | #define MISCCFG_GPRCOMPCDLCGEN (1 << 4) |
| 17 | /* GPIO community RTC clock gating */ |
| 18 | #define MISCCFG_GPRTCDLCGEN (1 << 3) |
| 19 | /* GFX controller clock gating */ |
| 20 | #define MISCCFG_GSXSLCGEN (1 << 2) |
| 21 | /* GPIO community partition clock gating */ |
| 22 | #define MISCCFG_GPDPCGEN (1 << 1) |
| 23 | /* GPIO community local clock gating */ |
| 24 | #define MISCCFG_GPDLCGEN (1 << 0) |
| 25 | |
| 26 | #ifndef __ACPI__ |
| 27 | #include <types.h> |
| 28 | #include <device/device.h> |
| 29 | |
| 30 | /* |
| 31 | * GPIO numbers may not be contiguous and instead will have a different |
| 32 | * starting pin number for each pad group. |
| 33 | */ |
| 34 | #define INTEL_GPP_BASE(first_of_community, start_of_group, end_of_group,\ |
| 35 | group_pad_base) \ |
| 36 | { \ |
| 37 | .first_pad = (start_of_group) - (first_of_community), \ |
| 38 | .size = (end_of_group) - (start_of_group) + 1, \ |
| 39 | .acpi_pad_base = (group_pad_base), \ |
| 40 | } |
| 41 | |
| 42 | /* |
| 43 | * A pad base of -1 indicates that this group uses contiguous numbering |
| 44 | * and a pad base should not be used for this group. |
| 45 | */ |
| 46 | #define PAD_BASE_NONE -1 |
| 47 | |
| 48 | /* The common/default group numbering is contiguous */ |
| 49 | #define INTEL_GPP(first_of_community, start_of_group, end_of_group) \ |
| 50 | INTEL_GPP_BASE(first_of_community, start_of_group, end_of_group,\ |
| 51 | PAD_BASE_NONE) |
| 52 | |
| 53 | /* |
| 54 | * Following should be defined in soc/gpio.h |
| 55 | * GPIO_MISCCFG - offset to GPIO MISCCFG Register |
| 56 | * |
| 57 | * GPIO_NUM_PAD_CFG_REGS - number of PAD config registers in the SOC |
| 58 | * For SOCs that have DW0 and DW1, it should be 2 |
| 59 | * NUM_GPI_STATUS_REGS - total number of GPI status registers across all |
| 60 | * GPIO communities in the SOC |
| 61 | * |
| 62 | * The register offsets specific to the soc communities should be provided in |
| 63 | * struct pad_community table returned from soc_gpio_get_community |
| 64 | */ |
| 65 | |
| 66 | typedef uint32_t gpio_t; |
| 67 | |
| 68 | enum gpio_lock_action { |
| 69 | GPIO_UNLOCK = 0x0, |
| 70 | GPIO_LOCK_CONFIG = 0x1, |
| 71 | GPIO_LOCK_TX = 0x2, |
| 72 | GPIO_LOCK_FULL = GPIO_LOCK_CONFIG | GPIO_LOCK_TX, |
| 73 | }; |
| 74 | |
| 75 | struct pad_config { |
| 76 | gpio_t pad;/* offset of pad within community */ |
| 77 | uint32_t pad_config[GPIO_NUM_PAD_CFG_REGS];/* |
| 78 | Pad config data corresponding to DW0, DW1,.... */ |
| 79 | enum gpio_lock_action lock_action; /* Pad lock configuration */ |
| 80 | }; |
| 81 | |
| 82 | /* |
| 83 | * Structure provides the logical to actual value for PADRSTCFG in DW0. Note |
| 84 | * that the values are expected to be within the field placement of the register |
| 85 | * itself. i.e. if the reset field is at 31:30 then the values within logical |
| 86 | * and chipset should occupy 31:30. |
| 87 | */ |
| 88 | struct reset_mapping { |
| 89 | uint32_t logical; |
| 90 | uint32_t chipset; |
| 91 | }; |
| 92 | |
| 93 | /* Structure describes the groups within each community */ |
| 94 | struct pad_group { |
| 95 | int first_pad; /* offset of first pad of the group relative |
| 96 | to the community */ |
| 97 | unsigned int size; /* Size of the group */ |
| 98 | /* |
| 99 | * This is the starting pin number for the pads in this group when |
| 100 | * they are used in ACPI. This is only needed if the pins are not |
| 101 | * contiguous across groups, most groups will have this set to |
| 102 | * PAD_BASE_NONE and use contiguous numbering for ACPI. |
| 103 | */ |
| 104 | int acpi_pad_base; |
| 105 | }; |
| 106 | |
| 107 | /* A range of consecutive virtual-wire entries in a community */ |
| 108 | struct vw_entries { |
| 109 | gpio_t first_pad; |
| 110 | gpio_t last_pad; |
| 111 | }; |
| 112 | |
| 113 | /* virtual-wire mapping base and the starting bit position for a group */ |
| 114 | struct vw_map { |
| 115 | uint8_t base; |
| 116 | uint8_t start_pos; |
| 117 | }; |
| 118 | |
| 119 | /* This structure will be used to describe a community or each group within a |
| 120 | * community when multiple groups exist inside a community |
| 121 | */ |
| 122 | struct pad_community { |
| 123 | const char *name; |
| 124 | const char *acpi_path; |
| 125 | size_t num_gpi_regs;/* number of gpi registers in community */ |
| 126 | size_t max_pads_per_group; /* number of pads in each group; |
| 127 | Number of pads bit mapped in each GPI status/en and Host Own Reg */ |
| 128 | gpio_t first_pad; /* first pad in community */ |
| 129 | gpio_t last_pad; /* last pad in community */ |
| 130 | uint16_t pad_own_reg_0; /* offset to Pad Ownership (host or CSME) Reg 0 */ |
| 131 | uint16_t host_own_reg_0; /* offset to Host Ownership (ACPI or driver) Reg 0 */ |
| 132 | uint16_t gpi_int_sts_reg_0; /* offset to GPI Int STS Reg 0 */ |
| 133 | uint16_t gpi_int_en_reg_0; /* offset to GPI Int Enable Reg 0 */ |
| 134 | uint16_t gpi_smi_sts_reg_0; /* offset to GPI SMI STS Reg 0 */ |
| 135 | uint16_t gpi_smi_en_reg_0; /* offset to GPI SMI EN Reg 0 */ |
| 136 | uint16_t gpi_gpe_sts_reg_0; /* offset to GPI GPE STS Reg 0 */ |
| 137 | uint16_t gpi_gpe_en_reg_0; /* offset to GPI GPE EN Reg 0 */ |
| 138 | uint16_t gpi_nmi_sts_reg_0; /* offset to GPI NMI STS Reg 0 */ |
| 139 | uint16_t gpi_nmi_en_reg_0; /* offset to GPI NMI EN Reg 0 */ |
| 140 | uint16_t pad_cfg_base; /* offset to first PAD_GFG_DW0 Reg */ |
| 141 | uint16_t pad_cfg_lock_offset; /* offset to first PADCFGLOCK Reg */ |
| 142 | uint8_t gpi_status_offset; /* specifies offset in struct |
| 143 | gpi_status */ |
| 144 | uint8_t port; /* PCR Port ID */ |
| 145 | #if CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_16BIT_CPU_PORTID) |
| 146 | uint16_t cpu_port; /* Use 16-bit CPU Port ID */ |
| 147 | #else |
| 148 | uint8_t cpu_port; /* Use 8-bit CPU Port ID */ |
| 149 | #endif |
| 150 | const struct reset_mapping *reset_map; /* PADRSTCFG logical to |
| 151 | chipset mapping */ |
| 152 | size_t num_reset_vals; |
| 153 | const struct pad_group *groups; |
| 154 | size_t num_groups; |
| 155 | unsigned int vw_base; |
| 156 | /* |
| 157 | * Note: The entries must be in the same order here as the order in |
| 158 | * which they map to VW indexes (beginning with VW base) |
| 159 | */ |
| 160 | const struct vw_entries *vw_entries; |
| 161 | const struct vw_map *vw_map; |
| 162 | size_t num_vw_entries; |
| 163 | }; |
| 164 | |
| 165 | /* |
| 166 | * Provides storage for all GPI status registers from all communities |
| 167 | */ |
| 168 | struct gpi_status { |
| 169 | uint32_t grp[NUM_GPI_STATUS_REGS]; |
| 170 | }; |
| 171 | |
| 172 | /* |
| 173 | * Structure provides the pmc to gpio group mapping |
| 174 | */ |
| 175 | struct pmc_to_gpio_route { |
| 176 | int pmc; |
| 177 | int gpio; |
| 178 | }; |
| 179 | |
| 180 | /* |
| 181 | * Returns the first community in the list. This will help to iterate |
| 182 | * through the list. It also returns total number of gpio communities. |
| 183 | * The soc layer provides a table describing available gpio communities. |
| 184 | */ |
| 185 | const struct pad_community *soc_gpio_get_community(size_t *num_communities); |
| 186 | |
| 187 | /* |
| 188 | * Clear GPI SMI status and fill in the structure representing enabled |
| 189 | * and set status. |
| 190 | */ |
| 191 | void gpi_clear_get_smi_status(struct gpi_status *sts); |
| 192 | |
| 193 | /* Return 1 if gpio is set in the sts. Otherwise 0. */ |
| 194 | int gpi_status_get(const struct gpi_status *sts, gpio_t gpi); |
| 195 | |
| 196 | /* |
| 197 | * Configuration for raw pads. Some pads are designated as only special function |
| 198 | * pins, and don't have an associated GPIO number, so we need to expose the raw |
| 199 | * pad configuration functionality. |
| 200 | */ |
| 201 | void gpio_configure_pads(const struct pad_config *cfg, size_t num_pads); |
| 202 | |
| 203 | /* |
| 204 | * gpio_configure_pads_with_override accepts as input two GPIO tables: |
| 205 | * 1. Base config |
| 206 | * 2. Override config |
| 207 | * |
| 208 | * This function configures raw pads in base config and applies override in |
| 209 | * override config if any. Thus, for every GPIO_x in base config, this function |
| 210 | * looks up the GPIO in override config and if it is present there, then applies |
| 211 | * the configuration from override config. GPIOs that are only specified in the |
| 212 | * override, but not in the base configuration, will be ignored. |
| 213 | */ |
| 214 | void gpio_configure_pads_with_override(const struct pad_config *base_cfg, |
| 215 | size_t base_num_pads, |
| 216 | const struct pad_config *override_cfg, |
| 217 | size_t override_num_pads); |
| 218 | |
| 219 | /* |
| 220 | * Calculate Address of DW0 register for given GPIO |
| 221 | */ |
| 222 | void *gpio_dwx_address(const gpio_t pad); |
| 223 | |
| 224 | struct gpio_lock_config { |
| 225 | gpio_t pad; |
| 226 | enum gpio_lock_action lock_action; |
| 227 | }; |
| 228 | |
| 229 | /* |
| 230 | * Lock a GPIO's configuration. |
| 231 | * |
| 232 | * The caller may specify if they wish to only lock the pad configuration, only |
| 233 | * the TX state, or both. When the configuration is locked, the following |
| 234 | * registers become Read-Only and software writes to these registers have no |
| 235 | * effect. |
| 236 | * |
| 237 | * Pad Configuration registers, |
| 238 | * GPI_NMI_EN, |
| 239 | * GPI_SMI_EN, |
| 240 | * GPI_GPE_EN |
| 241 | * |
| 242 | * Note that this is only effective if the pad is owned by the host and this |
| 243 | * function may only be called in SMM. |
| 244 | * |
| 245 | * @param pad: GPIO pad number |
| 246 | * @param lock_action: Which register to lock. |
| 247 | * @return 0 if successful, |
| 248 | * 1 - unsuccessful |
| 249 | * 2 - powered down |
| 250 | * 3 - multi-cast mixed |
| 251 | * -1 - sideband message failed or other error |
| 252 | */ |
| 253 | int gpio_lock_pad(const gpio_t pad, enum gpio_lock_action lock_action); |
| 254 | |
| 255 | /* |
| 256 | * gpio_lock_pads() can be used to lock an array of gpio pads, avoiding |
| 257 | * the p2sb_unhide() and p2sb_hide() calls between each gpio lock that would |
| 258 | * occur if gpio_lock_pad() were used to lock each pad in the list. |
| 259 | * |
| 260 | * @param pad_list: array of gpio_lock_config structures, one for each gpio to lock |
| 261 | * @param count: number of gpio_lock_config structs in the pad_list array |
| 262 | * @return 0 if successful, |
| 263 | * 1 - unsuccessful |
| 264 | * 2 - powered down |
| 265 | * 3 - multi-cast mixed |
| 266 | * -1 - sideband message failed or other error |
| 267 | */ |
| 268 | int gpio_lock_pads(const struct gpio_lock_config *pad_list, const size_t count); |
| 269 | |
| 270 | /* |
| 271 | * Returns an array of gpio_lock_config entries that the SoC |
| 272 | * deems security risks that should be locked down. |
| 273 | */ |
| 274 | const struct gpio_lock_config *soc_gpio_lock_config(size_t *num); |
| 275 | |
| 276 | /* |
| 277 | * Returns the pmc_gpe to gpio_gpe mapping table |
| 278 | * |
| 279 | */ |
| 280 | const struct pmc_to_gpio_route *soc_pmc_gpio_routes(size_t *num); |
| 281 | |
| 282 | /* |
| 283 | * Set the GPIO groups for the GPE blocks. The values from PMC register GPE_CFG |
| 284 | * are passed which is then mapped to proper groups for MISCCFG. This basically |
| 285 | * sets the MISCCFG register bits: |
| 286 | * dw0 = gpe0_route[11:8]. This is ACPI GPE0b. |
| 287 | * dw1 = gpe0_route[15:12]. This is ACPI GPE0c. |
| 288 | * dw2 = gpe0_route[19:16]. This is ACPI GPE0d. |
| 289 | */ |
| 290 | void gpio_route_gpe(uint8_t gpe0b, uint8_t gpe0c, uint8_t gpe0d); |
| 291 | |
| 292 | /* |
| 293 | * Function returns PCR port ID for this pad |
| 294 | */ |
| 295 | uint8_t gpio_get_pad_portid(const gpio_t pad); |
| 296 | |
| 297 | /* |
| 298 | * Function to patch GPIO settings for SoC specifically |
| 299 | * cfg = pad config contains pad number and reg value. |
| 300 | * dw_reg = pad config dword number. |
| 301 | * reg_val = the reg value need to be patched. |
| 302 | * Returns gpio setting patched for SoC specifically |
| 303 | */ |
| 304 | uint32_t soc_gpio_pad_config_fixup(const struct pad_config *cfg, |
| 305 | int dw_reg, uint32_t reg_val); |
| 306 | |
| 307 | /* |
| 308 | * Function to reset/clear the GPI Interrupt Enable & Status registers for |
| 309 | * all GPIO pad communities. |
| 310 | */ |
| 311 | void gpi_clear_int_cfg(void); |
| 312 | |
| 313 | /* The function performs GPIO Power Management programming. */ |
| 314 | void gpio_pm_configure(const uint8_t *misccfg_pm_values, size_t num); |
| 315 | |
| 316 | /* |
| 317 | * Set gpio ops of the device to gpio block ops. |
| 318 | * Shall be called by all SoCs that use intelblocks/gpio. |
| 319 | */ |
| 320 | void block_gpio_enable(struct device *dev); |
| 321 | |
| 322 | /* |
| 323 | * Returns true if any GPIO that uses the specified IRQ is also programmed to |
| 324 | * route IRQs to IOAPIC. |
| 325 | */ |
| 326 | bool gpio_routes_ioapic_irq(unsigned int irq); |
| 327 | |
| 328 | size_t gpio_get_index_in_group(gpio_t pad); |
| 329 | |
| 330 | /* |
| 331 | * Returns true and stuffs out params for virtual-wire index and bit position |
| 332 | * for the given GPIO, otherwise false if there is no VW index for the pad. |
| 333 | */ |
| 334 | bool gpio_get_vw_info(gpio_t pad, unsigned int *vw_index, unsigned int *vw_bit); |
| 335 | |
| 336 | /* Returns PCR port ID for this pad for the CPU; will be 0 if not available */ |
| 337 | unsigned int gpio_get_pad_cpu_portid(gpio_t pad); |
| 338 | |
| 339 | /* Return the gpio pad number based table */ |
| 340 | struct pad_config *new_padbased_table(void); |
| 341 | |
| 342 | /* Must pass the table with pad number based */ |
| 343 | void gpio_padbased_override(struct pad_config *padbased_table, |
| 344 | const struct pad_config *override_cfg, |
| 345 | size_t override_num_pads); |
| 346 | |
| 347 | /* |
| 348 | * Must pass the table with pad number based, will skip configures the unmapped |
| 349 | * pins by check pad and DW0 are 0. |
| 350 | */ |
| 351 | void gpio_configure_pads_with_padbased(struct pad_config *padbased_table); |
| 352 | |
| 353 | #endif |
| 354 | #endif /* _SOC_INTELBLOCKS_GPIO_H_ */ |