1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 36 #include <sys/gpio.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/module.h> 40 #include <sys/mutex.h> 41 #include <sys/rman.h> 42 43 #include <machine/bus.h> 44 #include <machine/resource.h> 45 #include <machine/intr.h> 46 47 #include <dev/fdt/simplebus.h> 48 49 #include <dev/ofw/ofw_bus.h> 50 #include <dev/ofw/ofw_bus_subr.h> 51 52 #include <dev/fdt/fdt_pinctrl.h> 53 54 #include <dev/extres/syscon/syscon.h> 55 56 #include "gpio_if.h" 57 #include "syscon_if.h" 58 #include "fdt_pinctrl_if.h" 59 60 struct rk_pinctrl_pin_drive { 61 uint32_t bank; 62 uint32_t subbank; 63 uint32_t offset; 64 uint32_t value; 65 uint32_t ma; 66 }; 67 68 struct rk_pinctrl_bank { 69 uint32_t bank; 70 uint32_t subbank; 71 uint32_t offset; 72 uint32_t nbits; 73 }; 74 75 struct rk_pinctrl_pin_fixup { 76 uint32_t bank; 77 uint32_t subbank; 78 uint32_t pin; 79 uint32_t reg; 80 uint32_t bit; 81 uint32_t mask; 82 }; 83 84 struct rk_pinctrl_gpio { 85 uint32_t bank; 86 char *gpio_name; 87 device_t gpio_dev; 88 }; 89 90 struct rk_pinctrl_softc; 91 92 struct rk_pinctrl_conf { 93 struct rk_pinctrl_bank *iomux_conf; 94 uint32_t iomux_nbanks; 95 struct rk_pinctrl_pin_fixup *pin_fixup; 96 uint32_t npin_fixup; 97 struct rk_pinctrl_pin_drive *pin_drive; 98 uint32_t npin_drive; 99 struct rk_pinctrl_gpio *gpio_bank; 100 uint32_t ngpio_bank; 101 uint32_t (*get_pd_offset)(struct rk_pinctrl_softc *, uint32_t); 102 struct syscon *(*get_syscon)(struct rk_pinctrl_softc *, uint32_t); 103 int (*parse_bias)(phandle_t, int); 104 int (*resolv_bias_value)(int, int); 105 int (*get_bias_value)(int, int); 106 }; 107 108 struct rk_pinctrl_softc { 109 struct simplebus_softc simplebus_sc; 110 device_t dev; 111 struct syscon *grf; 112 struct syscon *pmu; 113 struct rk_pinctrl_conf *conf; 114 struct mtx mtx; 115 }; 116 117 #define RK_PINCTRL_LOCK(_sc) mtx_lock_spin(&(_sc)->mtx) 118 #define RK_PINCTRL_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->mtx) 119 #define RK_PINCTRL_LOCK_ASSERT(_sc) mtx_assert(&(_sc)->mtx, MA_OWNED) 120 121 #define RK_IOMUX(_bank, _subbank, _offset, _nbits) \ 122 { \ 123 .bank = _bank, \ 124 .subbank = _subbank, \ 125 .offset = _offset, \ 126 .nbits = _nbits, \ 127 } 128 129 #define RK_PINFIX(_bank, _pin, _reg, _bit, _mask) \ 130 { \ 131 .bank = _bank, \ 132 .pin = _pin, \ 133 .reg = _reg, \ 134 .bit = _bit, \ 135 .mask = _mask, \ 136 } 137 138 #define RK_PINDRIVE(_bank, _subbank, _offset, _value, _ma) \ 139 { \ 140 .bank = _bank, \ 141 .subbank = _subbank, \ 142 .offset = _offset, \ 143 .value = _value, \ 144 .ma = _ma, \ 145 } 146 #define RK_GPIO(_bank, _name) \ 147 { \ 148 .bank = _bank, \ 149 .gpio_name = _name, \ 150 } 151 152 static struct rk_pinctrl_gpio rk3288_gpio_bank[] = { 153 RK_GPIO(0, "gpio0"), 154 RK_GPIO(1, "gpio1"), 155 RK_GPIO(2, "gpio2"), 156 RK_GPIO(3, "gpio3"), 157 RK_GPIO(4, "gpio4"), 158 RK_GPIO(5, "gpio5"), 159 RK_GPIO(6, "gpio6"), 160 RK_GPIO(7, "gpio7"), 161 RK_GPIO(8, "gpio8"), 162 }; 163 164 static struct rk_pinctrl_bank rk3288_iomux_bank[] = { 165 /* bank sub offs nbits */ 166 /* PMU */ 167 RK_IOMUX(0, 0, 0x0084, 2), 168 RK_IOMUX(0, 1, 0x0088, 2), 169 RK_IOMUX(0, 2, 0x008C, 2), 170 /* GFR */ 171 RK_IOMUX(1, 3, 0x000C, 2), 172 RK_IOMUX(2, 0, 0x0010, 2), 173 RK_IOMUX(2, 1, 0x0014, 2), 174 RK_IOMUX(2, 2, 0x0018, 2), 175 RK_IOMUX(2, 3, 0x001C, 2), 176 RK_IOMUX(3, 0, 0x0020, 2), 177 RK_IOMUX(3, 1, 0x0024, 2), 178 RK_IOMUX(3, 2, 0x0028, 2), 179 RK_IOMUX(3, 3, 0x002C, 4), 180 RK_IOMUX(4, 0, 0x0034, 4), 181 RK_IOMUX(4, 1, 0x003C, 4), 182 RK_IOMUX(4, 2, 0x0044, 2), 183 RK_IOMUX(4, 3, 0x0048, 2), 184 /* 5,0 - Empty */ 185 RK_IOMUX(5, 1, 0x0050, 2), 186 RK_IOMUX(5, 2, 0x0054, 2), 187 /* 5,3 - Empty */ 188 RK_IOMUX(6, 0, 0x005C, 2), 189 RK_IOMUX(6, 1, 0x0060, 2), 190 RK_IOMUX(6, 2, 0x0064, 2), 191 /* 6,3 - Empty */ 192 RK_IOMUX(7, 0, 0x006C, 2), 193 RK_IOMUX(7, 1, 0x0070, 2), 194 RK_IOMUX(7, 2, 0x0074, 4), 195 /* 7,3 - Empty */ 196 RK_IOMUX(8, 0, 0x0080, 2), 197 RK_IOMUX(8, 1, 0x0084, 2), 198 /* 8,2 - Empty */ 199 /* 8,3 - Empty */ 200 201 }; 202 203 static struct rk_pinctrl_pin_fixup rk3288_pin_fixup[] = { 204 }; 205 206 static struct rk_pinctrl_pin_drive rk3288_pin_drive[] = { 207 /* bank sub offs val ma */ 208 /* GPIO0A (PMU)*/ 209 RK_PINDRIVE(0, 0, 0x070, 0, 2), 210 RK_PINDRIVE(0, 0, 0x070, 1, 4), 211 RK_PINDRIVE(0, 0, 0x070, 2, 8), 212 RK_PINDRIVE(0, 0, 0x070, 3, 12), 213 214 /* GPIO0B (PMU)*/ 215 RK_PINDRIVE(0, 1, 0x074, 0, 2), 216 RK_PINDRIVE(0, 1, 0x074, 1, 4), 217 RK_PINDRIVE(0, 1, 0x074, 2, 8), 218 RK_PINDRIVE(0, 1, 0x074, 3, 12), 219 220 /* GPIO0C (PMU)*/ 221 RK_PINDRIVE(0, 2, 0x078, 0, 2), 222 RK_PINDRIVE(0, 2, 0x078, 1, 4), 223 RK_PINDRIVE(0, 2, 0x078, 2, 8), 224 RK_PINDRIVE(0, 2, 0x078, 3, 12), 225 226 /* GPIO1D */ 227 RK_PINDRIVE(1, 3, 0x1CC, 0, 2), 228 RK_PINDRIVE(1, 3, 0x1CC, 1, 4), 229 RK_PINDRIVE(1, 3, 0x1CC, 2, 8), 230 RK_PINDRIVE(1, 3, 0x1CC, 3, 12), 231 232 /* GPIO2A */ 233 RK_PINDRIVE(2, 0, 0x1D0, 0, 2), 234 RK_PINDRIVE(2, 0, 0x1D0, 1, 4), 235 RK_PINDRIVE(2, 0, 0x1D0, 2, 8), 236 RK_PINDRIVE(2, 0, 0x1D0, 3, 12), 237 238 /* GPIO2B */ 239 RK_PINDRIVE(2, 1, 0x1D4, 0, 2), 240 RK_PINDRIVE(2, 1, 0x1D4, 1, 4), 241 RK_PINDRIVE(2, 1, 0x1D4, 2, 8), 242 RK_PINDRIVE(2, 1, 0x1D4, 3, 12), 243 244 /* GPIO2C */ 245 RK_PINDRIVE(2, 2, 0x1D8, 0, 2), 246 RK_PINDRIVE(2, 2, 0x1D8, 1, 4), 247 RK_PINDRIVE(2, 2, 0x1D8, 2, 8), 248 RK_PINDRIVE(2, 2, 0x1D8, 3, 12), 249 250 /* GPIO2D */ 251 RK_PINDRIVE(2, 3, 0x1DC, 0, 2), 252 RK_PINDRIVE(2, 3, 0x1DC, 1, 4), 253 RK_PINDRIVE(2, 3, 0x1DC, 2, 8), 254 RK_PINDRIVE(2, 3, 0x1DC, 3, 12), 255 256 /* GPIO3A */ 257 RK_PINDRIVE(3, 0, 0x1E0, 0, 2), 258 RK_PINDRIVE(3, 0, 0x1E0, 1, 4), 259 RK_PINDRIVE(3, 0, 0x1E0, 2, 8), 260 RK_PINDRIVE(3, 0, 0x1E0, 3, 12), 261 262 /* GPIO3B */ 263 RK_PINDRIVE(3, 1, 0x1E4, 0, 2), 264 RK_PINDRIVE(3, 1, 0x1E4, 1, 4), 265 RK_PINDRIVE(3, 1, 0x1E4, 2, 8), 266 RK_PINDRIVE(3, 1, 0x1E4, 3, 12), 267 268 /* GPIO3C */ 269 RK_PINDRIVE(3, 2, 0x1E8, 0, 2), 270 RK_PINDRIVE(3, 2, 0x1E8, 1, 4), 271 RK_PINDRIVE(3, 2, 0x1E8, 2, 8), 272 RK_PINDRIVE(3, 2, 0x1E8, 3, 12), 273 274 /* GPIO3D */ 275 RK_PINDRIVE(3, 3, 0x1EC, 0, 2), 276 RK_PINDRIVE(3, 3, 0x1EC, 1, 4), 277 RK_PINDRIVE(3, 3, 0x1EC, 2, 8), 278 RK_PINDRIVE(3, 3, 0x1EC, 3, 12), 279 280 /* GPIO4A */ 281 RK_PINDRIVE(4, 0, 0x1F0, 0, 2), 282 RK_PINDRIVE(4, 0, 0x1F0, 1, 4), 283 RK_PINDRIVE(4, 0, 0x1F0, 2, 8), 284 RK_PINDRIVE(4, 0, 0x1F0, 3, 12), 285 286 /* GPIO4B */ 287 RK_PINDRIVE(4, 1, 0x1F4, 0, 2), 288 RK_PINDRIVE(4, 1, 0x1F4, 1, 4), 289 RK_PINDRIVE(4, 1, 0x1F4, 2, 8), 290 RK_PINDRIVE(4, 1, 0x1F4, 3, 12), 291 292 /* GPIO4C */ 293 RK_PINDRIVE(4, 2, 0x1F8, 0, 2), 294 RK_PINDRIVE(4, 2, 0x1F8, 1, 4), 295 RK_PINDRIVE(4, 2, 0x1F8, 2, 8), 296 RK_PINDRIVE(4, 2, 0x1F8, 3, 12), 297 298 /* GPIO4D */ 299 RK_PINDRIVE(4, 3, 0x1FC, 0, 2), 300 RK_PINDRIVE(4, 3, 0x1FC, 1, 4), 301 RK_PINDRIVE(4, 3, 0x1FC, 2, 8), 302 RK_PINDRIVE(4, 3, 0x1FC, 3, 12), 303 304 /* GPIO5B */ 305 RK_PINDRIVE(5, 1, 0x204, 0, 2), 306 RK_PINDRIVE(5, 1, 0x204, 1, 4), 307 RK_PINDRIVE(5, 1, 0x204, 2, 8), 308 RK_PINDRIVE(5, 1, 0x204, 3, 12), 309 310 /* GPIO5C */ 311 RK_PINDRIVE(5, 2, 0x208, 0, 2), 312 RK_PINDRIVE(5, 2, 0x208, 1, 4), 313 RK_PINDRIVE(5, 2, 0x208, 2, 8), 314 RK_PINDRIVE(5, 2, 0x208, 3, 12), 315 316 /* GPIO6A */ 317 RK_PINDRIVE(6, 0, 0x210, 0, 2), 318 RK_PINDRIVE(6, 0, 0x210, 1, 4), 319 RK_PINDRIVE(6, 0, 0x210, 2, 8), 320 RK_PINDRIVE(6, 0, 0x210, 3, 12), 321 322 /* GPIO6B */ 323 RK_PINDRIVE(6, 1, 0x214, 0, 2), 324 RK_PINDRIVE(6, 1, 0x214, 1, 4), 325 RK_PINDRIVE(6, 1, 0x214, 2, 8), 326 RK_PINDRIVE(6, 1, 0x214, 3, 12), 327 328 /* GPIO6C */ 329 RK_PINDRIVE(6, 2, 0x218, 0, 2), 330 RK_PINDRIVE(6, 2, 0x218, 1, 4), 331 RK_PINDRIVE(6, 2, 0x218, 2, 8), 332 RK_PINDRIVE(6, 2, 0x218, 3, 12), 333 334 /* GPIO7A */ 335 RK_PINDRIVE(7, 0, 0x220, 0, 2), 336 RK_PINDRIVE(7, 0, 0x220, 1, 4), 337 RK_PINDRIVE(7, 0, 0x220, 2, 8), 338 RK_PINDRIVE(7, 0, 0x220, 3, 12), 339 340 /* GPIO7B */ 341 RK_PINDRIVE(7, 1, 0x224, 0, 2), 342 RK_PINDRIVE(7, 1, 0x224, 1, 4), 343 RK_PINDRIVE(7, 1, 0x224, 2, 8), 344 RK_PINDRIVE(7, 1, 0x224, 3, 12), 345 346 /* GPIO7C */ 347 RK_PINDRIVE(7, 2, 0x228, 0, 2), 348 RK_PINDRIVE(7, 2, 0x228, 1, 4), 349 RK_PINDRIVE(7, 2, 0x228, 2, 8), 350 RK_PINDRIVE(7, 2, 0x228, 3, 12), 351 352 /* GPIO8A */ 353 RK_PINDRIVE(8, 0, 0x230, 0, 2), 354 RK_PINDRIVE(8, 0, 0x230, 1, 4), 355 RK_PINDRIVE(8, 0, 0x230, 2, 8), 356 RK_PINDRIVE(8, 0, 0x230, 3, 12), 357 358 /* GPIO8B */ 359 RK_PINDRIVE(8, 1, 0x234, 0, 2), 360 RK_PINDRIVE(8, 1, 0x234, 1, 4), 361 RK_PINDRIVE(8, 1, 0x234, 2, 8), 362 RK_PINDRIVE(8, 1, 0x234, 3, 12), 363 }; 364 365 static uint32_t 366 rk3288_get_pd_offset(struct rk_pinctrl_softc *sc, uint32_t bank) 367 { 368 if (bank == 0) 369 return (0x064); /* PMU */ 370 return (0x130); 371 } 372 373 static struct syscon * 374 rk3288_get_syscon(struct rk_pinctrl_softc *sc, uint32_t bank) 375 { 376 if (bank == 0) 377 return (sc->pmu); 378 return (sc->grf); 379 } 380 381 static int 382 rk3288_parse_bias(phandle_t node, int bank) 383 { 384 if (OF_hasprop(node, "bias-disable")) 385 return (0); 386 if (OF_hasprop(node, "bias-pull-up")) 387 return (1); 388 if (OF_hasprop(node, "bias-pull-down")) 389 return (2); 390 391 return (-1); 392 } 393 394 static int 395 rk3288_resolv_bias_value(int bank, int bias) 396 { 397 int rv = 0; 398 399 if (bias == 1) 400 rv = GPIO_PIN_PULLUP; 401 else if (bias == 2) 402 rv = GPIO_PIN_PULLDOWN; 403 404 return (rv); 405 } 406 407 static int 408 rk3288_get_bias_value(int bank, int bias) 409 { 410 int rv = 0; 411 412 if (bias & GPIO_PIN_PULLUP) 413 rv = 1; 414 else if (bias & GPIO_PIN_PULLDOWN) 415 rv = 2; 416 417 return (rv); 418 } 419 420 struct rk_pinctrl_conf rk3288_conf = { 421 .iomux_conf = rk3288_iomux_bank, 422 .iomux_nbanks = nitems(rk3288_iomux_bank), 423 .pin_fixup = rk3288_pin_fixup, 424 .npin_fixup = nitems(rk3288_pin_fixup), 425 .pin_drive = rk3288_pin_drive, 426 .npin_drive = nitems(rk3288_pin_drive), 427 .gpio_bank = rk3288_gpio_bank, 428 .ngpio_bank = nitems(rk3288_gpio_bank), 429 .get_pd_offset = rk3288_get_pd_offset, 430 .get_syscon = rk3288_get_syscon, 431 .parse_bias = rk3288_parse_bias, 432 .resolv_bias_value = rk3288_resolv_bias_value, 433 .get_bias_value = rk3288_get_bias_value, 434 }; 435 436 static struct rk_pinctrl_gpio rk3328_gpio_bank[] = { 437 RK_GPIO(0, "gpio0"), 438 RK_GPIO(1, "gpio1"), 439 RK_GPIO(2, "gpio2"), 440 RK_GPIO(3, "gpio3"), 441 }; 442 443 static struct rk_pinctrl_bank rk3328_iomux_bank[] = { 444 /* bank sub offs nbits */ 445 RK_IOMUX(0, 0, 0x0000, 2), 446 RK_IOMUX(0, 1, 0x0004, 2), 447 RK_IOMUX(0, 2, 0x0008, 2), 448 RK_IOMUX(0, 3, 0x000C, 2), 449 RK_IOMUX(1, 0, 0x0010, 2), 450 RK_IOMUX(1, 1, 0x0014, 2), 451 RK_IOMUX(1, 2, 0x0018, 2), 452 RK_IOMUX(1, 3, 0x001C, 2), 453 RK_IOMUX(2, 0, 0x0020, 2), 454 RK_IOMUX(2, 1, 0x0024, 3), 455 RK_IOMUX(2, 2, 0x002c, 3), 456 RK_IOMUX(2, 3, 0x0034, 2), 457 RK_IOMUX(3, 0, 0x0038, 3), 458 RK_IOMUX(3, 1, 0x0040, 3), 459 RK_IOMUX(3, 2, 0x0048, 2), 460 RK_IOMUX(3, 3, 0x004c, 2), 461 }; 462 463 static struct rk_pinctrl_pin_fixup rk3328_pin_fixup[] = { 464 /* bank pin reg bit mask */ 465 RK_PINFIX(2, 12, 0x24, 8, 0x300), 466 RK_PINFIX(2, 15, 0x28, 0, 0x7), 467 RK_PINFIX(2, 23, 0x30, 14, 0x6000), 468 }; 469 470 static struct rk_pinctrl_pin_drive rk3328_pin_drive[] = { 471 /* bank sub offs val ma */ 472 RK_PINDRIVE(0, 0, 0x200, 0, 2), 473 RK_PINDRIVE(0, 0, 0x200, 1, 4), 474 RK_PINDRIVE(0, 0, 0x200, 2, 8), 475 RK_PINDRIVE(0, 0, 0x200, 3, 12), 476 477 RK_PINDRIVE(0, 1, 0x204, 0, 2), 478 RK_PINDRIVE(0, 1, 0x204, 1, 4), 479 RK_PINDRIVE(0, 1, 0x204, 2, 8), 480 RK_PINDRIVE(0, 1, 0x204, 3, 12), 481 482 RK_PINDRIVE(0, 2, 0x208, 0, 2), 483 RK_PINDRIVE(0, 2, 0x208, 1, 4), 484 RK_PINDRIVE(0, 2, 0x208, 2, 8), 485 RK_PINDRIVE(0, 2, 0x208, 3, 12), 486 487 RK_PINDRIVE(0, 3, 0x20C, 0, 2), 488 RK_PINDRIVE(0, 3, 0x20C, 1, 4), 489 RK_PINDRIVE(0, 3, 0x20C, 2, 8), 490 RK_PINDRIVE(0, 3, 0x20C, 3, 12), 491 492 RK_PINDRIVE(1, 0, 0x210, 0, 2), 493 RK_PINDRIVE(1, 0, 0x210, 1, 4), 494 RK_PINDRIVE(1, 0, 0x210, 2, 8), 495 RK_PINDRIVE(1, 0, 0x210, 3, 12), 496 497 RK_PINDRIVE(1, 1, 0x214, 0, 2), 498 RK_PINDRIVE(1, 1, 0x214, 1, 4), 499 RK_PINDRIVE(1, 1, 0x214, 2, 8), 500 RK_PINDRIVE(1, 1, 0x214, 3, 12), 501 502 RK_PINDRIVE(1, 2, 0x218, 0, 2), 503 RK_PINDRIVE(1, 2, 0x218, 1, 4), 504 RK_PINDRIVE(1, 2, 0x218, 2, 8), 505 RK_PINDRIVE(1, 2, 0x218, 3, 12), 506 507 RK_PINDRIVE(1, 3, 0x21C, 0, 2), 508 RK_PINDRIVE(1, 3, 0x21C, 1, 4), 509 RK_PINDRIVE(1, 3, 0x21C, 2, 8), 510 RK_PINDRIVE(1, 3, 0x21C, 3, 12), 511 512 RK_PINDRIVE(2, 0, 0x220, 0, 2), 513 RK_PINDRIVE(2, 0, 0x220, 1, 4), 514 RK_PINDRIVE(2, 0, 0x220, 2, 8), 515 RK_PINDRIVE(2, 0, 0x220, 3, 12), 516 517 RK_PINDRIVE(2, 1, 0x224, 0, 2), 518 RK_PINDRIVE(2, 1, 0x224, 1, 4), 519 RK_PINDRIVE(2, 1, 0x224, 2, 8), 520 RK_PINDRIVE(2, 1, 0x224, 3, 12), 521 522 RK_PINDRIVE(2, 2, 0x228, 0, 2), 523 RK_PINDRIVE(2, 2, 0x228, 1, 4), 524 RK_PINDRIVE(2, 2, 0x228, 2, 8), 525 RK_PINDRIVE(2, 2, 0x228, 3, 12), 526 527 RK_PINDRIVE(2, 3, 0x22C, 0, 2), 528 RK_PINDRIVE(2, 3, 0x22C, 1, 4), 529 RK_PINDRIVE(2, 3, 0x22C, 2, 8), 530 RK_PINDRIVE(2, 3, 0x22C, 3, 12), 531 532 RK_PINDRIVE(3, 0, 0x230, 0, 2), 533 RK_PINDRIVE(3, 0, 0x230, 1, 4), 534 RK_PINDRIVE(3, 0, 0x230, 2, 8), 535 RK_PINDRIVE(3, 0, 0x230, 3, 12), 536 537 RK_PINDRIVE(3, 1, 0x234, 0, 2), 538 RK_PINDRIVE(3, 1, 0x234, 1, 4), 539 RK_PINDRIVE(3, 1, 0x234, 2, 8), 540 RK_PINDRIVE(3, 1, 0x234, 3, 12), 541 542 RK_PINDRIVE(3, 2, 0x238, 0, 2), 543 RK_PINDRIVE(3, 2, 0x238, 1, 4), 544 RK_PINDRIVE(3, 2, 0x238, 2, 8), 545 RK_PINDRIVE(3, 2, 0x238, 3, 12), 546 547 RK_PINDRIVE(3, 3, 0x23C, 0, 2), 548 RK_PINDRIVE(3, 3, 0x23C, 1, 4), 549 RK_PINDRIVE(3, 3, 0x23C, 2, 8), 550 RK_PINDRIVE(3, 3, 0x23C, 3, 12), 551 }; 552 553 static uint32_t 554 rk3328_get_pd_offset(struct rk_pinctrl_softc *sc, uint32_t bank) 555 { 556 return (0x100); 557 } 558 559 static struct syscon * 560 rk3328_get_syscon(struct rk_pinctrl_softc *sc, uint32_t bank) 561 { 562 return (sc->grf); 563 } 564 565 struct rk_pinctrl_conf rk3328_conf = { 566 .iomux_conf = rk3328_iomux_bank, 567 .iomux_nbanks = nitems(rk3328_iomux_bank), 568 .pin_fixup = rk3328_pin_fixup, 569 .npin_fixup = nitems(rk3328_pin_fixup), 570 .pin_drive = rk3328_pin_drive, 571 .npin_drive = nitems(rk3328_pin_drive), 572 .gpio_bank = rk3328_gpio_bank, 573 .ngpio_bank = nitems(rk3328_gpio_bank), 574 .get_pd_offset = rk3328_get_pd_offset, 575 .get_syscon = rk3328_get_syscon, 576 .parse_bias = rk3288_parse_bias, 577 .resolv_bias_value = rk3288_resolv_bias_value, 578 .get_bias_value = rk3288_get_bias_value, 579 }; 580 581 static struct rk_pinctrl_gpio rk3399_gpio_bank[] = { 582 RK_GPIO(0, "gpio0"), 583 RK_GPIO(1, "gpio1"), 584 RK_GPIO(2, "gpio2"), 585 RK_GPIO(3, "gpio3"), 586 RK_GPIO(4, "gpio4"), 587 }; 588 589 static struct rk_pinctrl_bank rk3399_iomux_bank[] = { 590 /* bank sub offs nbits */ 591 RK_IOMUX(0, 0, 0x0000, 2), 592 RK_IOMUX(0, 1, 0x0004, 2), 593 RK_IOMUX(0, 2, 0x0008, 2), 594 RK_IOMUX(0, 3, 0x000C, 2), 595 RK_IOMUX(1, 0, 0x0010, 2), 596 RK_IOMUX(1, 1, 0x0014, 2), 597 RK_IOMUX(1, 2, 0x0018, 2), 598 RK_IOMUX(1, 3, 0x001C, 2), 599 RK_IOMUX(2, 0, 0xE000, 2), 600 RK_IOMUX(2, 1, 0xE004, 2), 601 RK_IOMUX(2, 2, 0xE008, 2), 602 RK_IOMUX(2, 3, 0xE00C, 2), 603 RK_IOMUX(3, 0, 0xE010, 2), 604 RK_IOMUX(3, 1, 0xE014, 2), 605 RK_IOMUX(3, 2, 0xE018, 2), 606 RK_IOMUX(3, 3, 0xE01C, 2), 607 RK_IOMUX(4, 0, 0xE020, 2), 608 RK_IOMUX(4, 1, 0xE024, 2), 609 RK_IOMUX(4, 2, 0xE028, 2), 610 RK_IOMUX(4, 3, 0xE02C, 2), 611 }; 612 613 static struct rk_pinctrl_pin_fixup rk3399_pin_fixup[] = {}; 614 615 static struct rk_pinctrl_pin_drive rk3399_pin_drive[] = { 616 /* bank sub offs val ma */ 617 /* GPIO0A */ 618 RK_PINDRIVE(0, 0, 0x80, 0, 5), 619 RK_PINDRIVE(0, 0, 0x80, 1, 10), 620 RK_PINDRIVE(0, 0, 0x80, 2, 15), 621 RK_PINDRIVE(0, 0, 0x80, 3, 20), 622 623 /* GPIOB */ 624 RK_PINDRIVE(0, 1, 0x88, 0, 5), 625 RK_PINDRIVE(0, 1, 0x88, 1, 10), 626 RK_PINDRIVE(0, 1, 0x88, 2, 15), 627 RK_PINDRIVE(0, 1, 0x88, 3, 20), 628 629 /* GPIO1A */ 630 RK_PINDRIVE(1, 0, 0xA0, 0, 3), 631 RK_PINDRIVE(1, 0, 0xA0, 1, 6), 632 RK_PINDRIVE(1, 0, 0xA0, 2, 9), 633 RK_PINDRIVE(1, 0, 0xA0, 3, 12), 634 635 /* GPIO1B */ 636 RK_PINDRIVE(1, 1, 0xA8, 0, 3), 637 RK_PINDRIVE(1, 1, 0xA8, 1, 6), 638 RK_PINDRIVE(1, 1, 0xA8, 2, 9), 639 RK_PINDRIVE(1, 1, 0xA8, 3, 12), 640 641 /* GPIO1C */ 642 RK_PINDRIVE(1, 2, 0xB0, 0, 3), 643 RK_PINDRIVE(1, 2, 0xB0, 1, 6), 644 RK_PINDRIVE(1, 2, 0xB0, 2, 9), 645 RK_PINDRIVE(1, 2, 0xB0, 3, 12), 646 647 /* GPIO1D */ 648 RK_PINDRIVE(1, 3, 0xB8, 0, 3), 649 RK_PINDRIVE(1, 3, 0xB8, 1, 6), 650 RK_PINDRIVE(1, 3, 0xB8, 2, 9), 651 RK_PINDRIVE(1, 3, 0xB8, 3, 12), 652 }; 653 654 static uint32_t 655 rk3399_get_pd_offset(struct rk_pinctrl_softc *sc, uint32_t bank) 656 { 657 if (bank < 2) 658 return (0x40); 659 660 return (0xE040); 661 } 662 663 static struct syscon * 664 rk3399_get_syscon(struct rk_pinctrl_softc *sc, uint32_t bank) 665 { 666 if (bank < 2) 667 return (sc->pmu); 668 669 return (sc->grf); 670 } 671 672 static int 673 rk3399_parse_bias(phandle_t node, int bank) 674 { 675 int pullup, pulldown; 676 677 if (OF_hasprop(node, "bias-disable")) 678 return (0); 679 680 switch (bank) { 681 case 0: 682 case 2: 683 pullup = 3; 684 pulldown = 1; 685 break; 686 case 1: 687 case 3: 688 case 4: 689 pullup = 1; 690 pulldown = 2; 691 break; 692 } 693 694 if (OF_hasprop(node, "bias-pull-up")) 695 return (pullup); 696 if (OF_hasprop(node, "bias-pull-down")) 697 return (pulldown); 698 699 return (-1); 700 } 701 702 static int 703 rk3399_resolv_bias_value(int bank, int bias) 704 { 705 int rv = 0; 706 707 switch (bank) { 708 case 0: 709 case 2: 710 if (bias == 3) 711 rv = GPIO_PIN_PULLUP; 712 else if (bias == 1) 713 rv = GPIO_PIN_PULLDOWN; 714 break; 715 case 1: 716 case 3: 717 case 4: 718 if (bias == 1) 719 rv = GPIO_PIN_PULLUP; 720 else if (bias == 2) 721 rv = GPIO_PIN_PULLDOWN; 722 break; 723 } 724 725 return (rv); 726 } 727 728 static int 729 rk3399_get_bias_value(int bank, int bias) 730 { 731 int rv = 0; 732 733 switch (bank) { 734 case 0: 735 case 2: 736 if (bias & GPIO_PIN_PULLUP) 737 rv = 3; 738 else if (bias & GPIO_PIN_PULLDOWN) 739 rv = 1; 740 break; 741 case 1: 742 case 3: 743 case 4: 744 if (bias & GPIO_PIN_PULLUP) 745 rv = 1; 746 else if (bias & GPIO_PIN_PULLDOWN) 747 rv = 2; 748 break; 749 } 750 751 return (rv); 752 } 753 754 struct rk_pinctrl_conf rk3399_conf = { 755 .iomux_conf = rk3399_iomux_bank, 756 .iomux_nbanks = nitems(rk3399_iomux_bank), 757 .pin_fixup = rk3399_pin_fixup, 758 .npin_fixup = nitems(rk3399_pin_fixup), 759 .pin_drive = rk3399_pin_drive, 760 .npin_drive = nitems(rk3399_pin_drive), 761 .gpio_bank = rk3399_gpio_bank, 762 .ngpio_bank = nitems(rk3399_gpio_bank), 763 .get_pd_offset = rk3399_get_pd_offset, 764 .get_syscon = rk3399_get_syscon, 765 .parse_bias = rk3399_parse_bias, 766 .resolv_bias_value = rk3399_resolv_bias_value, 767 .get_bias_value = rk3399_get_bias_value, 768 }; 769 770 static struct rk_pinctrl_gpio rk3568_gpio_bank[] = { 771 RK_GPIO(0, "gpio0"), 772 RK_GPIO(1, "gpio1"), 773 RK_GPIO(2, "gpio2"), 774 RK_GPIO(3, "gpio3"), 775 RK_GPIO(4, "gpio4"), 776 }; 777 778 static struct rk_pinctrl_bank rk3568_iomux_bank[] = { 779 /* bank sub offs nbits */ 780 RK_IOMUX(0, 0, 0x0000, 4), /* PMU_GRF */ 781 RK_IOMUX(0, 1, 0x0008, 4), 782 RK_IOMUX(0, 2, 0x0010, 4), 783 RK_IOMUX(0, 3, 0x0018, 4), 784 785 RK_IOMUX(1, 0, 0x0000, 4), /* SYS_GRF */ 786 RK_IOMUX(1, 1, 0x0008, 4), 787 RK_IOMUX(1, 2, 0x0010, 4), 788 RK_IOMUX(1, 3, 0x0018, 4), 789 RK_IOMUX(2, 0, 0x0020, 4), 790 RK_IOMUX(2, 1, 0x0028, 4), 791 RK_IOMUX(2, 2, 0x0030, 4), 792 RK_IOMUX(2, 3, 0x0038, 4), 793 RK_IOMUX(3, 0, 0x0040, 4), 794 RK_IOMUX(3, 1, 0x0048, 4), 795 RK_IOMUX(3, 2, 0x0050, 4), 796 RK_IOMUX(3, 3, 0x0058, 4), 797 RK_IOMUX(4, 0, 0x0060, 4), 798 RK_IOMUX(4, 1, 0x0068, 4), 799 RK_IOMUX(4, 2, 0x0070, 4), 800 RK_IOMUX(4, 3, 0x0078, 4), 801 }; 802 803 static struct rk_pinctrl_pin_fixup rk3568_pin_fixup[] = {}; 804 805 static struct rk_pinctrl_pin_drive rk3568_pin_drive[] = { 806 /* bank sub offs val ma */ 807 /* GPIO0A */ 808 RK_PINDRIVE(0, 0, 0x0020, 0, 2), 809 RK_PINDRIVE(0, 0, 0x0020, 1, 4), 810 RK_PINDRIVE(0, 0, 0x0020, 2, 8), 811 RK_PINDRIVE(0, 0, 0x0020, 3, 12), 812 813 /* GPIO0B */ 814 RK_PINDRIVE(0, 1, 0x0024, 0, 2), 815 RK_PINDRIVE(0, 1, 0x0024, 1, 4), 816 RK_PINDRIVE(0, 1, 0x0024, 2, 8), 817 RK_PINDRIVE(0, 1, 0x0024, 3, 12), 818 819 /* GPIO0C */ 820 RK_PINDRIVE(0, 1, 0x0028, 0, 2), 821 RK_PINDRIVE(0, 1, 0x0028, 1, 4), 822 RK_PINDRIVE(0, 1, 0x0028, 2, 8), 823 RK_PINDRIVE(0, 1, 0x0028, 3, 12), 824 825 /* GPIO0D */ 826 RK_PINDRIVE(0, 1, 0x002c, 0, 2), 827 RK_PINDRIVE(0, 1, 0x002c, 1, 4), 828 RK_PINDRIVE(0, 1, 0x002c, 2, 8), 829 RK_PINDRIVE(0, 1, 0x002c, 3, 12), 830 831 /* GPIO1A */ 832 RK_PINDRIVE(1, 0, 0x0080, 0, 2), 833 RK_PINDRIVE(1, 0, 0x0080, 1, 4), 834 RK_PINDRIVE(1, 0, 0x0080, 2, 8), 835 RK_PINDRIVE(1, 0, 0x0080, 3, 12), 836 837 /* GPIO1B */ 838 RK_PINDRIVE(1, 1, 0x0084, 0, 2), 839 RK_PINDRIVE(1, 1, 0x0084, 1, 4), 840 RK_PINDRIVE(1, 1, 0x0084, 2, 8), 841 RK_PINDRIVE(1, 1, 0x0084, 3, 12), 842 843 /* GPIO1C */ 844 RK_PINDRIVE(1, 2, 0x0088, 0, 2), 845 RK_PINDRIVE(1, 2, 0x0088, 1, 4), 846 RK_PINDRIVE(1, 2, 0x0088, 2, 8), 847 RK_PINDRIVE(1, 2, 0x0088, 3, 12), 848 849 /* GPIO1D */ 850 RK_PINDRIVE(1, 3, 0x008c, 0, 2), 851 RK_PINDRIVE(1, 3, 0x008c, 1, 4), 852 RK_PINDRIVE(1, 3, 0x008c, 2, 8), 853 RK_PINDRIVE(1, 3, 0x008c, 3, 12), 854 855 /* GPIO2A */ 856 RK_PINDRIVE(2, 0, 0x0090, 0, 2), 857 RK_PINDRIVE(2, 0, 0x0090, 1, 4), 858 RK_PINDRIVE(2, 0, 0x0090, 2, 8), 859 RK_PINDRIVE(2, 0, 0x0090, 3, 12), 860 861 /* GPIO2B */ 862 RK_PINDRIVE(2, 1, 0x0094, 0, 2), 863 RK_PINDRIVE(2, 1, 0x0094, 1, 4), 864 RK_PINDRIVE(2, 1, 0x0094, 2, 8), 865 RK_PINDRIVE(2, 1, 0x0094, 3, 12), 866 867 /* GPIO2C */ 868 RK_PINDRIVE(2, 2, 0x0098, 0, 2), 869 RK_PINDRIVE(2, 2, 0x0098, 1, 4), 870 RK_PINDRIVE(2, 2, 0x0098, 2, 8), 871 RK_PINDRIVE(2, 2, 0x0098, 3, 12), 872 873 /* GPIO2D */ 874 RK_PINDRIVE(2, 3, 0x009c, 0, 2), 875 RK_PINDRIVE(2, 3, 0x009c, 1, 4), 876 RK_PINDRIVE(2, 3, 0x009c, 2, 8), 877 RK_PINDRIVE(2, 3, 0x009c, 3, 12), 878 879 /* GPIO3A */ 880 RK_PINDRIVE(3, 0, 0x00a0, 0, 2), 881 RK_PINDRIVE(3, 0, 0x00a0, 1, 4), 882 RK_PINDRIVE(3, 0, 0x00a0, 2, 8), 883 RK_PINDRIVE(3, 0, 0x00a0, 3, 12), 884 885 /* GPIO3B */ 886 RK_PINDRIVE(3, 1, 0x00a4, 0, 2), 887 RK_PINDRIVE(3, 1, 0x00a4, 1, 4), 888 RK_PINDRIVE(3, 1, 0x00a4, 2, 8), 889 RK_PINDRIVE(3, 1, 0x00a4, 3, 12), 890 891 /* GPIO3C */ 892 RK_PINDRIVE(3, 2, 0x00a8, 0, 2), 893 RK_PINDRIVE(3, 2, 0x00a8, 1, 4), 894 RK_PINDRIVE(3, 2, 0x00a8, 2, 8), 895 RK_PINDRIVE(3, 2, 0x00a8, 3, 12), 896 897 /* GPIO3D */ 898 RK_PINDRIVE(3, 3, 0x00ac, 0, 2), 899 RK_PINDRIVE(3, 3, 0x00ac, 1, 4), 900 RK_PINDRIVE(3, 3, 0x00ac, 2, 8), 901 RK_PINDRIVE(3, 3, 0x00ac, 3, 12), 902 903 /* GPIO4A */ 904 RK_PINDRIVE(4, 0, 0x00b0, 0, 2), 905 RK_PINDRIVE(4, 0, 0x00b0, 1, 4), 906 RK_PINDRIVE(4, 0, 0x00b0, 2, 8), 907 RK_PINDRIVE(4, 0, 0x00b0, 3, 12), 908 909 /* GPIO4B */ 910 RK_PINDRIVE(4, 1, 0x00b4, 0, 2), 911 RK_PINDRIVE(4, 1, 0x00b4, 1, 4), 912 RK_PINDRIVE(4, 1, 0x00b4, 2, 8), 913 RK_PINDRIVE(4, 1, 0x00b4, 3, 12), 914 915 /* GPIO4C */ 916 RK_PINDRIVE(4, 2, 0x00b8, 0, 2), 917 RK_PINDRIVE(4, 2, 0x00b8, 1, 4), 918 RK_PINDRIVE(4, 2, 0x00b8, 2, 8), 919 RK_PINDRIVE(4, 2, 0x00b8, 3, 12), 920 921 /* GPIO4D */ 922 RK_PINDRIVE(4, 3, 0x00bc, 0, 2), 923 RK_PINDRIVE(4, 3, 0x00bc, 1, 4), 924 RK_PINDRIVE(4, 3, 0x00bc, 2, 8), 925 RK_PINDRIVE(4, 3, 0x00bc, 3, 12), 926 }; 927 928 static uint32_t 929 rk3568_get_pd_offset(struct rk_pinctrl_softc *sc, uint32_t bank) 930 { 931 932 if (bank == 0) 933 return (0x20); 934 935 /* 936 * Registers start at 0x80, but bank index starts at 1. Return 0x70 937 * so later calculations get the correct offset. 938 */ 939 return (0x70); 940 } 941 942 static struct syscon * 943 rk3568_get_syscon(struct rk_pinctrl_softc *sc, uint32_t bank) 944 { 945 946 if (bank) 947 return (sc->grf); 948 else 949 return (sc->pmu); 950 } 951 952 static int 953 rk3568_parse_bias(phandle_t node, int bank) 954 { 955 956 if (OF_hasprop(node, "bias-disable")) 957 return (0); 958 if (OF_hasprop(node, "bias-pull-up")) 959 return (1); 960 if (OF_hasprop(node, "bias-pull-down")) 961 return (2); 962 return (-1); 963 } 964 965 static int 966 rk3568_resolv_bias_value(int bank, int bias) 967 { 968 969 if (bias == 1) 970 return (GPIO_PIN_PULLUP); 971 if (bias == 2) 972 return (GPIO_PIN_PULLDOWN); 973 return (0); 974 } 975 976 static int 977 rk3568_get_bias_value(int bank, int bias) 978 { 979 980 if (bias & GPIO_PIN_PULLUP) 981 return (1); 982 if (bias & GPIO_PIN_PULLDOWN) 983 return (2); 984 return (0); 985 } 986 987 struct rk_pinctrl_conf rk3568_conf = { 988 .iomux_conf = rk3568_iomux_bank, 989 .iomux_nbanks = nitems(rk3568_iomux_bank), 990 .pin_fixup = rk3568_pin_fixup, 991 .npin_fixup = nitems(rk3568_pin_fixup), 992 .pin_drive = rk3568_pin_drive, 993 .npin_drive = nitems(rk3568_pin_drive), 994 .gpio_bank = rk3568_gpio_bank, 995 .ngpio_bank = nitems(rk3568_gpio_bank), 996 .get_pd_offset = rk3568_get_pd_offset, 997 .get_syscon = rk3568_get_syscon, 998 .parse_bias = rk3568_parse_bias, 999 .resolv_bias_value = rk3568_resolv_bias_value, 1000 .get_bias_value = rk3568_get_bias_value, 1001 }; 1002 1003 static struct ofw_compat_data compat_data[] = { 1004 {"rockchip,rk3288-pinctrl", (uintptr_t)&rk3288_conf}, 1005 {"rockchip,rk3328-pinctrl", (uintptr_t)&rk3328_conf}, 1006 {"rockchip,rk3399-pinctrl", (uintptr_t)&rk3399_conf}, 1007 {"rockchip,rk3568-pinctrl", (uintptr_t)&rk3568_conf}, 1008 {NULL, 0} 1009 }; 1010 1011 static int 1012 rk_pinctrl_parse_drive(struct rk_pinctrl_softc *sc, phandle_t node, 1013 uint32_t bank, uint32_t subbank, uint32_t *drive, uint32_t *offset) 1014 { 1015 uint32_t value; 1016 int i; 1017 1018 if (OF_getencprop(node, "drive-strength", &value, 1019 sizeof(value)) != 0) 1020 return (-1); 1021 1022 /* Map to the correct drive value */ 1023 for (i = 0; i < sc->conf->npin_drive; i++) { 1024 if (sc->conf->pin_drive[i].bank != bank && 1025 sc->conf->pin_drive[i].subbank != subbank) 1026 continue; 1027 if (sc->conf->pin_drive[i].ma == value) { 1028 *drive = sc->conf->pin_drive[i].value; 1029 return (0); 1030 } 1031 } 1032 1033 return (-1); 1034 } 1035 1036 static void 1037 rk_pinctrl_get_fixup(struct rk_pinctrl_softc *sc, uint32_t bank, uint32_t pin, 1038 uint32_t *reg, uint32_t *mask, uint32_t *bit) 1039 { 1040 int i; 1041 1042 for (i = 0; i < sc->conf->npin_fixup; i++) 1043 if (sc->conf->pin_fixup[i].bank == bank && 1044 sc->conf->pin_fixup[i].pin == pin) { 1045 *reg = sc->conf->pin_fixup[i].reg; 1046 *mask = sc->conf->pin_fixup[i].mask; 1047 *bit = sc->conf->pin_fixup[i].bit; 1048 1049 return; 1050 } 1051 } 1052 1053 static int 1054 rk_pinctrl_handle_io(struct rk_pinctrl_softc *sc, phandle_t node, uint32_t bank, 1055 uint32_t pin) 1056 { 1057 bool have_cfg, have_direction, have_value; 1058 uint32_t direction_value, pin_value; 1059 struct rk_pinctrl_gpio *gpio; 1060 int i, rv; 1061 1062 have_cfg = false; 1063 have_direction = false; 1064 have_value = false; 1065 1066 /* Get (subset of) GPIO pin properties. */ 1067 if (OF_hasprop(node, "output-disable")) { 1068 have_cfg = true; 1069 have_direction = true; 1070 direction_value = GPIO_PIN_INPUT; 1071 } 1072 1073 if (OF_hasprop(node, "output-enable")) { 1074 have_cfg = true; 1075 have_direction = true; 1076 direction_value = GPIO_PIN_OUTPUT; 1077 } 1078 1079 if (OF_hasprop(node, "output-low")) { 1080 have_cfg = true; 1081 have_direction = true; 1082 direction_value = GPIO_PIN_OUTPUT; 1083 have_value = true; 1084 pin_value = 0; 1085 } 1086 1087 if (OF_hasprop(node, "output-high")) { 1088 have_cfg = true; 1089 have_direction = true; 1090 direction_value = GPIO_PIN_OUTPUT; 1091 have_value = true; 1092 pin_value = 1; 1093 } 1094 1095 if (!have_cfg) 1096 return (0); 1097 1098 /* Find gpio */ 1099 gpio = NULL; 1100 for(i = 0; i < sc->conf->ngpio_bank; i++) { 1101 if (bank == sc->conf->gpio_bank[i].bank) { 1102 gpio = sc->conf->gpio_bank + i; 1103 break; 1104 } 1105 } 1106 if (gpio == NULL) { 1107 device_printf(sc->dev, "Cannot find GPIO bank %d\n", bank); 1108 return (ENXIO); 1109 } 1110 if (gpio->gpio_dev == NULL) { 1111 device_printf(sc->dev, 1112 "No GPIO subdevice found for bank %d\n", bank); 1113 return (ENXIO); 1114 } 1115 1116 rv = 0; 1117 if (have_value) { 1118 rv = GPIO_PIN_SET(gpio->gpio_dev, pin, pin_value); 1119 if (rv != 0) { 1120 device_printf(sc->dev, "Cannot set GPIO value: %d\n", 1121 rv); 1122 return (rv); 1123 } 1124 } 1125 1126 if (have_direction) { 1127 rv = GPIO_PIN_SETFLAGS(gpio->gpio_dev, pin, direction_value); 1128 if (rv != 0) { 1129 device_printf(sc->dev, 1130 "Cannot set GPIO direction: %d\n", rv); 1131 return (rv); 1132 } 1133 } 1134 1135 return (0); 1136 } 1137 1138 static void 1139 rk_pinctrl_configure_pin(struct rk_pinctrl_softc *sc, uint32_t *pindata) 1140 { 1141 phandle_t pin_conf; 1142 struct syscon *syscon; 1143 uint32_t bank, subbank, pin, function, bias; 1144 uint32_t bit, mask, reg, drive; 1145 int i, rv; 1146 1147 bank = pindata[0]; 1148 pin = pindata[1]; 1149 function = pindata[2]; 1150 pin_conf = OF_node_from_xref(pindata[3]); 1151 subbank = pin / 8; 1152 1153 for (i = 0; i < sc->conf->iomux_nbanks; i++) 1154 if (sc->conf->iomux_conf[i].bank == bank && 1155 sc->conf->iomux_conf[i].subbank == subbank) 1156 break; 1157 1158 if (i == sc->conf->iomux_nbanks) { 1159 device_printf(sc->dev, "Unknown pin %d in bank %d\n", pin, 1160 bank); 1161 return; 1162 } 1163 1164 /* Find syscon */ 1165 syscon = sc->conf->get_syscon(sc, bank); 1166 1167 /* Setup GPIO properties first */ 1168 rv = rk_pinctrl_handle_io(sc, pin_conf, bank, pin); 1169 1170 /* Then pin pull-up/down */ 1171 bias = sc->conf->parse_bias(pin_conf, bank); 1172 if (bias >= 0) { 1173 reg = sc->conf->get_pd_offset(sc, bank); 1174 reg += bank * 0x10 + ((pin / 8) * 0x4); 1175 bit = (pin % 8) * 2; 1176 mask = (0x3 << bit); 1177 SYSCON_MODIFY_4(syscon, reg, mask, bias << bit | (mask << 16)); 1178 } 1179 1180 /* Then drive strength */ 1181 if (ofw_bus_node_is_compatible(ofw_bus_get_node(sc->dev), 1182 "rockchip,rk3568-pinctrl")) { 1183 uint32_t value; 1184 if (OF_getencprop(pin_conf, "drive-strength", &value, 1185 sizeof(value)) == 0) { 1186 if (bank) 1187 reg = 0x01c0 + (bank * 0x40) + (pin / 2 * 4); 1188 else 1189 reg = 0x0070 + (pin / 2 * 4); 1190 1191 drive = ((1 << (value + 1)) - 1) << (pin % 2); 1192 1193 mask = 0x3f << (pin % 2);; 1194 1195 SYSCON_WRITE_4(syscon, reg, drive | (mask << 16)); 1196 } 1197 } else { 1198 rv = rk_pinctrl_parse_drive(sc, pin_conf, bank, subbank, &drive, 1199 ®); 1200 if (rv == 0) { 1201 bit = (pin % 8) * 2; 1202 mask = (0x3 << bit); 1203 SYSCON_MODIFY_4(syscon, reg, mask, 1204 drive << bit | (mask << 16)); 1205 } 1206 } 1207 1208 /* Finally set the pin function */ 1209 reg = sc->conf->iomux_conf[i].offset; 1210 switch (sc->conf->iomux_conf[i].nbits) { 1211 case 4: 1212 if ((pin % 8) >= 4) 1213 reg += 0x4; 1214 bit = (pin % 4) * 4; 1215 mask = (0xF << bit); 1216 break; 1217 case 3: 1218 if ((pin % 8) >= 5) 1219 reg += 4; 1220 bit = (pin % 8 % 5) * 3; 1221 mask = (0x7 << bit); 1222 break; 1223 case 2: 1224 bit = (pin % 8) * 2; 1225 mask = (0x3 << bit); 1226 break; 1227 default: 1228 device_printf(sc->dev, 1229 "Unknown pin stride width %d in bank %d\n", 1230 sc->conf->iomux_conf[i].nbits, bank); 1231 return; 1232 } 1233 rk_pinctrl_get_fixup(sc, bank, pin, ®, &mask, &bit); 1234 1235 /* 1236 * NOTE: not all syscon registers uses hi-word write mask, thus 1237 * register modify method should be used. 1238 * XXXX We should not pass write mask to syscon register 1239 * without hi-word write mask. 1240 */ 1241 SYSCON_MODIFY_4(syscon, reg, mask, function << bit | (mask << 16)); 1242 } 1243 1244 static int 1245 rk_pinctrl_configure_pins(device_t dev, phandle_t cfgxref) 1246 { 1247 struct rk_pinctrl_softc *sc; 1248 phandle_t node; 1249 uint32_t *pins; 1250 int i, npins; 1251 1252 sc = device_get_softc(dev); 1253 node = OF_node_from_xref(cfgxref); 1254 1255 npins = OF_getencprop_alloc_multi(node, "rockchip,pins", sizeof(*pins), 1256 (void **)&pins); 1257 if (npins <= 0) 1258 return (ENOENT); 1259 1260 for (i = 0; i != npins; i += 4) 1261 rk_pinctrl_configure_pin(sc, pins + i); 1262 1263 return (0); 1264 } 1265 1266 static int 1267 rk_pinctrl_is_gpio_locked(struct rk_pinctrl_softc *sc, struct syscon *syscon, 1268 int bank, uint32_t pin, bool *is_gpio) 1269 { 1270 uint32_t subbank, bit, mask, reg; 1271 uint32_t pinfunc; 1272 int i; 1273 1274 RK_PINCTRL_LOCK_ASSERT(sc); 1275 1276 subbank = pin / 8; 1277 *is_gpio = false; 1278 1279 for (i = 0; i < sc->conf->iomux_nbanks; i++) 1280 if (sc->conf->iomux_conf[i].bank == bank && 1281 sc->conf->iomux_conf[i].subbank == subbank) 1282 break; 1283 1284 if (i == sc->conf->iomux_nbanks) { 1285 device_printf(sc->dev, "Unknown pin %d in bank %d\n", pin, 1286 bank); 1287 return (EINVAL); 1288 } 1289 1290 syscon = sc->conf->get_syscon(sc, bank); 1291 1292 /* Parse pin function */ 1293 reg = sc->conf->iomux_conf[i].offset; 1294 switch (sc->conf->iomux_conf[i].nbits) { 1295 case 4: 1296 if ((pin % 8) >= 4) 1297 reg += 0x4; 1298 bit = (pin % 4) * 4; 1299 mask = (0xF << bit); 1300 break; 1301 case 3: 1302 if ((pin % 8) >= 5) 1303 reg += 4; 1304 bit = (pin % 8 % 5) * 3; 1305 mask = (0x7 << bit); 1306 break; 1307 case 2: 1308 bit = (pin % 8) * 2; 1309 mask = (0x3 << bit); 1310 break; 1311 default: 1312 device_printf(sc->dev, 1313 "Unknown pin stride width %d in bank %d\n", 1314 sc->conf->iomux_conf[i].nbits, bank); 1315 return (EINVAL); 1316 } 1317 rk_pinctrl_get_fixup(sc, bank, pin, ®, &mask, &bit); 1318 1319 reg = SYSCON_READ_4(syscon, reg); 1320 pinfunc = (reg & mask) >> bit; 1321 1322 /* Test if the pin is in gpio mode */ 1323 if (pinfunc == 0) 1324 *is_gpio = true; 1325 1326 return (0); 1327 } 1328 1329 static int 1330 rk_pinctrl_get_bank(struct rk_pinctrl_softc *sc, device_t gpio, int *bank) 1331 { 1332 int i; 1333 1334 for (i = 0; i < sc->conf->ngpio_bank; i++) { 1335 if (sc->conf->gpio_bank[i].gpio_dev == gpio) 1336 break; 1337 } 1338 if (i == sc->conf->ngpio_bank) 1339 return (EINVAL); 1340 1341 *bank = i; 1342 return (0); 1343 } 1344 1345 static int 1346 rk_pinctrl_is_gpio(device_t pinctrl, device_t gpio, uint32_t pin, bool *is_gpio) 1347 { 1348 struct rk_pinctrl_softc *sc; 1349 struct syscon *syscon; 1350 int bank; 1351 int rv; 1352 1353 sc = device_get_softc(pinctrl); 1354 RK_PINCTRL_LOCK(sc); 1355 1356 rv = rk_pinctrl_get_bank(sc, gpio, &bank); 1357 if (rv != 0) 1358 goto done; 1359 syscon = sc->conf->get_syscon(sc, bank); 1360 rv = rk_pinctrl_is_gpio_locked(sc, syscon, bank, pin, is_gpio); 1361 1362 done: 1363 RK_PINCTRL_UNLOCK(sc); 1364 1365 return (rv); 1366 } 1367 1368 static int 1369 rk_pinctrl_get_flags(device_t pinctrl, device_t gpio, uint32_t pin, 1370 uint32_t *flags) 1371 { 1372 struct rk_pinctrl_softc *sc; 1373 struct syscon *syscon; 1374 uint32_t reg, bit; 1375 uint32_t bias; 1376 int bank; 1377 int rv = 0; 1378 bool is_gpio; 1379 1380 sc = device_get_softc(pinctrl); 1381 RK_PINCTRL_LOCK(sc); 1382 1383 rv = rk_pinctrl_get_bank(sc, gpio, &bank); 1384 if (rv != 0) 1385 goto done; 1386 syscon = sc->conf->get_syscon(sc, bank); 1387 rv = rk_pinctrl_is_gpio_locked(sc, syscon, bank, pin, &is_gpio); 1388 if (rv != 0) 1389 goto done; 1390 if (!is_gpio) { 1391 rv = EINVAL; 1392 goto done; 1393 } 1394 /* Get the pullup/pulldown configuration */ 1395 reg = sc->conf->get_pd_offset(sc, bank); 1396 reg += bank * 0x10 + ((pin / 8) * 0x4); 1397 bit = (pin % 8) * 2; 1398 reg = SYSCON_READ_4(syscon, reg); 1399 reg = (reg >> bit) & 0x3; 1400 bias = sc->conf->resolv_bias_value(bank, reg); 1401 *flags = bias; 1402 1403 done: 1404 RK_PINCTRL_UNLOCK(sc); 1405 return (rv); 1406 } 1407 1408 static int 1409 rk_pinctrl_set_flags(device_t pinctrl, device_t gpio, uint32_t pin, 1410 uint32_t flags) 1411 { 1412 struct rk_pinctrl_softc *sc; 1413 struct syscon *syscon; 1414 uint32_t bit, mask, reg; 1415 uint32_t bias; 1416 int bank; 1417 int rv = 0; 1418 bool is_gpio; 1419 1420 sc = device_get_softc(pinctrl); 1421 RK_PINCTRL_LOCK(sc); 1422 1423 rv = rk_pinctrl_get_bank(sc, gpio, &bank); 1424 if (rv != 0) 1425 goto done; 1426 syscon = sc->conf->get_syscon(sc, bank); 1427 rv = rk_pinctrl_is_gpio_locked(sc, syscon, bank, pin, &is_gpio); 1428 if (rv != 0) 1429 goto done; 1430 if (!is_gpio) { 1431 rv = EINVAL; 1432 goto done; 1433 } 1434 /* Get the pullup/pulldown configuration */ 1435 reg = sc->conf->get_pd_offset(sc, bank); 1436 reg += bank * 0x10 + ((pin / 8) * 0x4); 1437 bit = (pin % 8) * 2; 1438 mask = (0x3 << bit); 1439 bias = sc->conf->get_bias_value(bank, flags); 1440 SYSCON_MODIFY_4(syscon, reg, mask, bias << bit | (mask << 16)); 1441 1442 done: 1443 RK_PINCTRL_UNLOCK(sc); 1444 return (rv); 1445 } 1446 1447 static int 1448 rk_pinctrl_probe(device_t dev) 1449 { 1450 1451 if (!ofw_bus_status_okay(dev)) 1452 return (ENXIO); 1453 1454 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 1455 return (ENXIO); 1456 1457 device_set_desc(dev, "RockChip Pinctrl controller"); 1458 return (BUS_PROBE_DEFAULT); 1459 } 1460 1461 static int 1462 rk_pinctrl_attach(device_t dev) 1463 { 1464 struct rk_pinctrl_softc *sc; 1465 phandle_t node; 1466 device_t cdev; 1467 int rv, gpio_unit; 1468 1469 sc = device_get_softc(dev); 1470 sc->dev = dev; 1471 1472 node = ofw_bus_get_node(dev); 1473 1474 if (OF_hasprop(node, "rockchip,grf") && 1475 syscon_get_by_ofw_property(dev, node, 1476 "rockchip,grf", &sc->grf) != 0) { 1477 device_printf(dev, "cannot get grf driver handle\n"); 1478 return (ENXIO); 1479 } 1480 1481 /* RK3568,RK3399,RK3288 have banks in PMU. RK3328 doesn't have a PMU. */ 1482 if (ofw_bus_node_is_compatible(node, "rockchip,rk3568-pinctrl") || 1483 ofw_bus_node_is_compatible(node, "rockchip,rk3399-pinctrl") || 1484 ofw_bus_node_is_compatible(node, "rockchip,rk3288-pinctrl")) { 1485 if (OF_hasprop(node, "rockchip,pmu") && 1486 syscon_get_by_ofw_property(dev, node, 1487 "rockchip,pmu", &sc->pmu) != 0) { 1488 device_printf(dev, "cannot get pmu driver handle\n"); 1489 return (ENXIO); 1490 } 1491 } 1492 1493 mtx_init(&sc->mtx, "rk pinctrl", "pinctrl", MTX_SPIN); 1494 1495 sc->conf = (struct rk_pinctrl_conf *)ofw_bus_search_compatible(dev, 1496 compat_data)->ocd_data; 1497 1498 fdt_pinctrl_register(dev, "rockchip,pins"); 1499 1500 simplebus_init(dev, node); 1501 1502 bus_generic_probe(dev); 1503 1504 /* Attach child devices */ 1505 for (node = OF_child(node), gpio_unit = 0; node > 0; 1506 node = OF_peer(node)) { 1507 if (!ofw_bus_node_is_compatible(node, "rockchip,gpio-bank")) 1508 continue; 1509 cdev = simplebus_add_device(dev, node, 0, NULL, -1, NULL); 1510 if (cdev == NULL) { 1511 device_printf(dev, " Cannot add GPIO subdevice\n"); 1512 gpio_unit += 1; 1513 continue; 1514 } 1515 rv = device_probe_and_attach(cdev); 1516 if (rv != 0) { 1517 device_printf(sc->dev, 1518 "Cannot attach GPIO subdevice\n"); 1519 gpio_unit += 1; 1520 continue; 1521 } 1522 sc->conf->gpio_bank[gpio_unit].gpio_dev = cdev; 1523 gpio_unit += 1; 1524 } 1525 1526 fdt_pinctrl_configure_tree(dev); 1527 1528 return (bus_generic_attach(dev)); 1529 } 1530 1531 static int 1532 rk_pinctrl_detach(device_t dev) 1533 { 1534 1535 return (EBUSY); 1536 } 1537 1538 static device_method_t rk_pinctrl_methods[] = { 1539 /* Device interface */ 1540 DEVMETHOD(device_probe, rk_pinctrl_probe), 1541 DEVMETHOD(device_attach, rk_pinctrl_attach), 1542 DEVMETHOD(device_detach, rk_pinctrl_detach), 1543 1544 /* fdt_pinctrl interface */ 1545 DEVMETHOD(fdt_pinctrl_configure, rk_pinctrl_configure_pins), 1546 DEVMETHOD(fdt_pinctrl_is_gpio, rk_pinctrl_is_gpio), 1547 DEVMETHOD(fdt_pinctrl_get_flags, rk_pinctrl_get_flags), 1548 DEVMETHOD(fdt_pinctrl_set_flags, rk_pinctrl_set_flags), 1549 1550 DEVMETHOD_END 1551 }; 1552 1553 DEFINE_CLASS_1(rk_pinctrl, rk_pinctrl_driver, rk_pinctrl_methods, 1554 sizeof(struct rk_pinctrl_softc), simplebus_driver); 1555 1556 EARLY_DRIVER_MODULE(rk_pinctrl, simplebus, rk_pinctrl_driver, 0, 0, 1557 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE); 1558 MODULE_VERSION(rk_pinctrl, 1); 1559