Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 The Chromium OS Authors. |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <errno.h> |
| 9 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 11 | #include <pch.h> |
| 12 | #include <pci.h> |
Simon Glass | 26f50fb | 2016-10-05 20:42:09 -0600 | [diff] [blame] | 13 | #include <syscon.h> |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 14 | #include <asm/cpu.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 16 | #include <asm/gpio.h> |
| 17 | #include <asm/io.h> |
| 18 | #include <asm/pci.h> |
| 19 | #include <asm/arch/gpio.h> |
| 20 | #include <dt-bindings/gpio/x86-gpio.h> |
| 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | /** |
| 25 | * struct broadwell_bank_priv - Private driver data |
| 26 | * |
| 27 | * @regs: Pointer to GPIO registers |
| 28 | * @bank: Bank number for this bank (0, 1 or 2) |
| 29 | * @offset: GPIO offset for this bank (0, 32 or 64) |
| 30 | */ |
| 31 | struct broadwell_bank_priv { |
| 32 | struct pch_lp_gpio_regs *regs; |
| 33 | int bank; |
| 34 | int offset; |
| 35 | }; |
| 36 | |
| 37 | static int broadwell_gpio_request(struct udevice *dev, unsigned offset, |
| 38 | const char *label) |
| 39 | { |
| 40 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
| 41 | struct pch_lp_gpio_regs *regs = priv->regs; |
| 42 | u32 val; |
| 43 | |
| 44 | /* |
| 45 | * Make sure that the GPIO pin we want isn't already in use for some |
| 46 | * built-in hardware function. We have to check this for every |
| 47 | * requested pin. |
| 48 | */ |
| 49 | debug("%s: request bank %d offset %d: ", __func__, priv->bank, offset); |
| 50 | val = inl(®s->own[priv->bank]); |
| 51 | if (!(val & (1UL << offset))) { |
| 52 | debug("gpio is reserved for internal use\n"); |
| 53 | return -EPERM; |
| 54 | } |
| 55 | debug("ok\n"); |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int broadwell_gpio_direction_input(struct udevice *dev, unsigned offset) |
| 61 | { |
| 62 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
| 63 | struct pch_lp_gpio_regs *regs = priv->regs; |
| 64 | |
| 65 | setio_32(®s->config[priv->offset + offset], CONFA_DIR_INPUT); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int broadwell_gpio_get_value(struct udevice *dev, unsigned offset) |
| 71 | { |
| 72 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
| 73 | struct pch_lp_gpio_regs *regs = priv->regs; |
| 74 | |
| 75 | return inl(®s->config[priv->offset + offset]) & CONFA_LEVEL_HIGH ? |
| 76 | 1 : 0; |
| 77 | } |
| 78 | |
| 79 | static int broadwell_gpio_set_value(struct udevice *dev, unsigned offset, |
| 80 | int value) |
| 81 | { |
| 82 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
| 83 | struct pch_lp_gpio_regs *regs = priv->regs; |
| 84 | |
| 85 | debug("%s: dev=%s, offset=%d, value=%d\n", __func__, dev->name, offset, |
| 86 | value); |
| 87 | clrsetio_32(®s->config[priv->offset + offset], CONFA_OUTPUT_HIGH, |
| 88 | value ? CONFA_OUTPUT_HIGH : 0); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static int broadwell_gpio_direction_output(struct udevice *dev, unsigned offset, |
| 94 | int value) |
| 95 | { |
| 96 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
| 97 | struct pch_lp_gpio_regs *regs = priv->regs; |
| 98 | |
| 99 | broadwell_gpio_set_value(dev, offset, value); |
| 100 | clrio_32(®s->config[priv->offset + offset], CONFA_DIR_INPUT); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static int broadwell_gpio_get_function(struct udevice *dev, unsigned offset) |
| 106 | { |
| 107 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
| 108 | struct pch_lp_gpio_regs *regs = priv->regs; |
| 109 | u32 mask = 1UL << offset; |
| 110 | |
| 111 | if (!(inl(®s->own[priv->bank]) & mask)) |
| 112 | return GPIOF_FUNC; |
| 113 | if (inl(®s->config[priv->offset + offset]) & CONFA_DIR_INPUT) |
| 114 | return GPIOF_INPUT; |
| 115 | else |
| 116 | return GPIOF_OUTPUT; |
| 117 | } |
| 118 | |
| 119 | static int broadwell_gpio_probe(struct udevice *dev) |
| 120 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 121 | struct broadwell_bank_plat *plat = dev_get_plat(dev); |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 122 | struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev); |
| 123 | struct broadwell_bank_priv *priv = dev_get_priv(dev); |
Simon Glass | 26f50fb | 2016-10-05 20:42:09 -0600 | [diff] [blame] | 124 | struct udevice *pinctrl; |
| 125 | int ret; |
| 126 | |
| 127 | /* Set up pin control if available */ |
| 128 | ret = syscon_get_by_driver_data(X86_SYSCON_PINCONF, &pinctrl); |
| 129 | debug("%s, pinctrl=%p, ret=%d\n", __func__, pinctrl, ret); |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 130 | |
| 131 | uc_priv->gpio_count = GPIO_PER_BANK; |
| 132 | uc_priv->bank_name = plat->bank_name; |
| 133 | |
| 134 | priv->regs = (struct pch_lp_gpio_regs *)(uintptr_t)plat->base_addr; |
| 135 | priv->bank = plat->bank; |
| 136 | priv->offset = priv->bank * 32; |
| 137 | debug("%s: probe done, regs %p, bank %d\n", __func__, priv->regs, |
| 138 | priv->bank); |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 143 | static int broadwell_gpio_of_to_plat(struct udevice *dev) |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 144 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 145 | struct broadwell_bank_plat *plat = dev_get_plat(dev); |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 146 | u32 gpiobase; |
| 147 | int bank; |
| 148 | int ret; |
| 149 | |
| 150 | ret = pch_get_gpio_base(dev->parent, &gpiobase); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 154 | bank = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1); |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 155 | if (bank == -1) { |
| 156 | debug("%s: Invalid bank number %d\n", __func__, bank); |
| 157 | return -EINVAL; |
| 158 | } |
| 159 | plat->bank = bank; |
| 160 | plat->base_addr = gpiobase; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 161 | plat->bank_name = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 162 | "bank-name", NULL); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 167 | static const struct dm_gpio_ops gpio_broadwell_ops = { |
| 168 | .request = broadwell_gpio_request, |
| 169 | .direction_input = broadwell_gpio_direction_input, |
| 170 | .direction_output = broadwell_gpio_direction_output, |
| 171 | .get_value = broadwell_gpio_get_value, |
| 172 | .set_value = broadwell_gpio_set_value, |
| 173 | .get_function = broadwell_gpio_get_function, |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | static const struct udevice_id intel_broadwell_gpio_ids[] = { |
| 177 | { .compatible = "intel,broadwell-gpio" }, |
| 178 | { } |
| 179 | }; |
| 180 | |
| 181 | U_BOOT_DRIVER(gpio_broadwell) = { |
| 182 | .name = "gpio_broadwell", |
| 183 | .id = UCLASS_GPIO, |
| 184 | .of_match = intel_broadwell_gpio_ids, |
| 185 | .ops = &gpio_broadwell_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 186 | .of_to_plat = broadwell_gpio_of_to_plat, |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 187 | .probe = broadwell_gpio_probe, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 188 | .priv_auto = sizeof(struct broadwell_bank_priv), |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 189 | .plat_auto = sizeof(struct broadwell_bank_plat), |
Simon Glass | 64b1797 | 2016-03-11 22:07:27 -0700 | [diff] [blame] | 190 | }; |