David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Rockchip Electronics Co., Ltd |
| 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <dm/pinctrl.h> |
| 9 | #include <regmap.h> |
| 10 | #include <syscon.h> |
| 11 | |
| 12 | #include "pinctrl-rockchip.h" |
| 13 | |
| 14 | static struct rockchip_mux_route_data rk3399_mux_route_data[] = { |
| 15 | { |
| 16 | /* uart2dbga_rx */ |
| 17 | .bank_num = 4, |
| 18 | .pin = 8, |
| 19 | .func = 2, |
| 20 | .route_offset = 0xe21c, |
| 21 | .route_val = BIT(16 + 10) | BIT(16 + 11), |
| 22 | }, { |
| 23 | /* uart2dbgb_rx */ |
| 24 | .bank_num = 4, |
| 25 | .pin = 16, |
| 26 | .func = 2, |
| 27 | .route_offset = 0xe21c, |
| 28 | .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(10), |
| 29 | }, { |
| 30 | /* uart2dbgc_rx */ |
| 31 | .bank_num = 4, |
| 32 | .pin = 19, |
| 33 | .func = 1, |
| 34 | .route_offset = 0xe21c, |
| 35 | .route_val = BIT(16 + 10) | BIT(16 + 11) | BIT(11), |
| 36 | }, { |
| 37 | /* pcie_clkreqn */ |
| 38 | .bank_num = 2, |
| 39 | .pin = 26, |
| 40 | .func = 2, |
| 41 | .route_offset = 0xe21c, |
| 42 | .route_val = BIT(16 + 14), |
| 43 | }, { |
| 44 | /* pcie_clkreqnb */ |
| 45 | .bank_num = 4, |
| 46 | .pin = 24, |
| 47 | .func = 1, |
| 48 | .route_offset = 0xe21c, |
| 49 | .route_val = BIT(16 + 14) | BIT(14), |
| 50 | }, |
| 51 | }; |
| 52 | |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame^] | 53 | static int rk3399_set_mux(struct rockchip_pin_bank *bank, int pin, int mux) |
| 54 | { |
| 55 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 56 | int iomux_num = (pin / 8); |
| 57 | struct regmap *regmap; |
| 58 | int reg, ret, mask, mux_type; |
| 59 | u8 bit; |
| 60 | u32 data, route_reg, route_val; |
| 61 | |
| 62 | regmap = (bank->iomux[iomux_num].type & IOMUX_SOURCE_PMU) |
| 63 | ? priv->regmap_pmu : priv->regmap_base; |
| 64 | |
| 65 | /* get basic quadrupel of mux registers and the correct reg inside */ |
| 66 | mux_type = bank->iomux[iomux_num].type; |
| 67 | reg = bank->iomux[iomux_num].offset; |
| 68 | reg += rockchip_get_mux_data(mux_type, pin, &bit, &mask); |
| 69 | |
| 70 | if (bank->route_mask & BIT(pin)) { |
| 71 | if (rockchip_get_mux_route(bank, pin, mux, &route_reg, |
| 72 | &route_val)) { |
| 73 | ret = regmap_write(regmap, route_reg, route_val); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | data = (mask << (bit + 16)); |
| 80 | data |= (mux & mask) << bit; |
| 81 | ret = regmap_write(regmap, reg, data); |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 86 | #define RK3399_PULL_GRF_OFFSET 0xe040 |
| 87 | #define RK3399_PULL_PMU_OFFSET 0x40 |
| 88 | |
| 89 | static void rk3399_calc_pull_reg_and_bit(struct rockchip_pin_bank *bank, |
| 90 | int pin_num, struct regmap **regmap, |
| 91 | int *reg, u8 *bit) |
| 92 | { |
| 93 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 94 | |
| 95 | /* The bank0:16 and bank1:32 pins are located in PMU */ |
| 96 | if (bank->bank_num == 0 || bank->bank_num == 1) { |
| 97 | *regmap = priv->regmap_pmu; |
| 98 | *reg = RK3399_PULL_PMU_OFFSET; |
| 99 | |
| 100 | *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE; |
| 101 | |
| 102 | *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4); |
| 103 | *bit = pin_num % ROCKCHIP_PULL_PINS_PER_REG; |
| 104 | *bit *= ROCKCHIP_PULL_BITS_PER_PIN; |
| 105 | } else { |
| 106 | *regmap = priv->regmap_base; |
| 107 | *reg = RK3399_PULL_GRF_OFFSET; |
| 108 | |
| 109 | /* correct the offset, as we're starting with the 3rd bank */ |
| 110 | *reg -= 0x20; |
| 111 | *reg += bank->bank_num * ROCKCHIP_PULL_BANK_STRIDE; |
| 112 | *reg += ((pin_num / ROCKCHIP_PULL_PINS_PER_REG) * 4); |
| 113 | |
| 114 | *bit = (pin_num % ROCKCHIP_PULL_PINS_PER_REG); |
| 115 | *bit *= ROCKCHIP_PULL_BITS_PER_PIN; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static void rk3399_calc_drv_reg_and_bit(struct rockchip_pin_bank *bank, |
| 120 | int pin_num, struct regmap **regmap, |
| 121 | int *reg, u8 *bit) |
| 122 | { |
| 123 | struct rockchip_pinctrl_priv *priv = bank->priv; |
| 124 | int drv_num = (pin_num / 8); |
| 125 | |
| 126 | /* The bank0:16 and bank1:32 pins are located in PMU */ |
| 127 | if (bank->bank_num == 0 || bank->bank_num == 1) |
| 128 | *regmap = priv->regmap_pmu; |
| 129 | else |
| 130 | *regmap = priv->regmap_base; |
| 131 | |
| 132 | *reg = bank->drv[drv_num].offset; |
| 133 | if (bank->drv[drv_num].drv_type == DRV_TYPE_IO_1V8_3V0_AUTO || |
| 134 | bank->drv[drv_num].drv_type == DRV_TYPE_IO_3V3_ONLY) |
| 135 | *bit = (pin_num % 8) * 3; |
| 136 | else |
| 137 | *bit = (pin_num % 8) * 2; |
| 138 | } |
| 139 | |
| 140 | static struct rockchip_pin_bank rk3399_pin_banks[] = { |
| 141 | PIN_BANK_IOMUX_FLAGS_DRV_FLAGS_OFFSET_PULL_FLAGS(0, 32, "gpio0", |
| 142 | IOMUX_SOURCE_PMU, |
| 143 | IOMUX_SOURCE_PMU, |
| 144 | IOMUX_SOURCE_PMU, |
| 145 | IOMUX_SOURCE_PMU, |
| 146 | DRV_TYPE_IO_1V8_ONLY, |
| 147 | DRV_TYPE_IO_1V8_ONLY, |
| 148 | DRV_TYPE_IO_DEFAULT, |
| 149 | DRV_TYPE_IO_DEFAULT, |
| 150 | 0x80, |
| 151 | 0x88, |
| 152 | -1, |
| 153 | -1, |
| 154 | PULL_TYPE_IO_1V8_ONLY, |
| 155 | PULL_TYPE_IO_1V8_ONLY, |
| 156 | PULL_TYPE_IO_DEFAULT, |
| 157 | PULL_TYPE_IO_DEFAULT |
| 158 | ), |
| 159 | PIN_BANK_IOMUX_DRV_FLAGS_OFFSET(1, 32, "gpio1", IOMUX_SOURCE_PMU, |
| 160 | IOMUX_SOURCE_PMU, |
| 161 | IOMUX_SOURCE_PMU, |
| 162 | IOMUX_SOURCE_PMU, |
| 163 | DRV_TYPE_IO_1V8_OR_3V0, |
| 164 | DRV_TYPE_IO_1V8_OR_3V0, |
| 165 | DRV_TYPE_IO_1V8_OR_3V0, |
| 166 | DRV_TYPE_IO_1V8_OR_3V0, |
| 167 | 0xa0, |
| 168 | 0xa8, |
| 169 | 0xb0, |
| 170 | 0xb8 |
| 171 | ), |
| 172 | PIN_BANK_DRV_FLAGS_PULL_FLAGS(2, 32, "gpio2", DRV_TYPE_IO_1V8_OR_3V0, |
| 173 | DRV_TYPE_IO_1V8_OR_3V0, |
| 174 | DRV_TYPE_IO_1V8_ONLY, |
| 175 | DRV_TYPE_IO_1V8_ONLY, |
| 176 | PULL_TYPE_IO_DEFAULT, |
| 177 | PULL_TYPE_IO_DEFAULT, |
| 178 | PULL_TYPE_IO_1V8_ONLY, |
| 179 | PULL_TYPE_IO_1V8_ONLY |
| 180 | ), |
| 181 | PIN_BANK_DRV_FLAGS(3, 32, "gpio3", DRV_TYPE_IO_3V3_ONLY, |
| 182 | DRV_TYPE_IO_3V3_ONLY, |
| 183 | DRV_TYPE_IO_3V3_ONLY, |
| 184 | DRV_TYPE_IO_1V8_OR_3V0 |
| 185 | ), |
| 186 | PIN_BANK_DRV_FLAGS(4, 32, "gpio4", DRV_TYPE_IO_1V8_OR_3V0, |
| 187 | DRV_TYPE_IO_1V8_3V0_AUTO, |
| 188 | DRV_TYPE_IO_1V8_OR_3V0, |
| 189 | DRV_TYPE_IO_1V8_OR_3V0 |
| 190 | ), |
| 191 | }; |
| 192 | |
| 193 | static struct rockchip_pin_ctrl rk3399_pin_ctrl = { |
David Wu | 71aede0 | 2019-04-16 21:50:54 +0800 | [diff] [blame] | 194 | .pin_banks = rk3399_pin_banks, |
| 195 | .nr_banks = ARRAY_SIZE(rk3399_pin_banks), |
| 196 | .label = "RK3399-GPIO", |
| 197 | .type = RK3399, |
| 198 | .grf_mux_offset = 0xe000, |
| 199 | .pmu_mux_offset = 0x0, |
| 200 | .grf_drv_offset = 0xe100, |
| 201 | .pmu_drv_offset = 0x80, |
| 202 | .iomux_routes = rk3399_mux_route_data, |
| 203 | .niomux_routes = ARRAY_SIZE(rk3399_mux_route_data), |
David Wu | 3dd7d6c | 2019-04-16 21:50:55 +0800 | [diff] [blame^] | 204 | .set_mux = rk3399_set_mux, |
David Wu | 71aede0 | 2019-04-16 21:50:54 +0800 | [diff] [blame] | 205 | .pull_calc_reg = rk3399_calc_pull_reg_and_bit, |
| 206 | .drv_calc_reg = rk3399_calc_drv_reg_and_bit, |
David Wu | 5f596ae | 2019-01-02 21:00:55 +0800 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | static const struct udevice_id rk3399_pinctrl_ids[] = { |
| 210 | { |
| 211 | .compatible = "rockchip,rk3399-pinctrl", |
| 212 | .data = (ulong)&rk3399_pin_ctrl |
| 213 | }, |
| 214 | { } |
| 215 | }; |
| 216 | |
| 217 | U_BOOT_DRIVER(pinctrl_rk3399) = { |
| 218 | .name = "rockchip_rk3399_pinctrl", |
| 219 | .id = UCLASS_PINCTRL, |
| 220 | .of_match = rk3399_pinctrl_ids, |
| 221 | .priv_auto_alloc_size = sizeof(struct rockchip_pinctrl_priv), |
| 222 | .ops = &rockchip_pinctrl_ops, |
| 223 | #if !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 224 | .bind = dm_scan_fdt_dev, |
| 225 | #endif |
| 226 | .probe = rockchip_pinctrl_probe, |
| 227 | }; |