1 /* $NetBSD: fdtvar.h,v 1.79 2023/06/12 12:58:17 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #ifndef _DEV_FDT_FDTVAR_H_ 30 #define _DEV_FDT_FDTVAR_H_ 31 32 #include <sys/types.h> 33 #include <sys/bus.h> 34 #include <sys/gpio.h> 35 #include <sys/termios.h> 36 37 #include <dev/i2c/i2cvar.h> 38 #include <dev/pwm/pwmvar.h> 39 #include <dev/clk/clk.h> 40 41 #ifdef _KERNEL_OPT 42 #include "audio.h" 43 #endif 44 #if NAUDIO > 0 45 #include <dev/audio/audio_dai.h> 46 #else 47 typedef void *audio_dai_tag_t; 48 #endif 49 50 #include <dev/clock_subr.h> 51 52 #include <dev/ofw/openfirm.h> 53 54 struct fdt_attach_args { 55 const char *faa_name; 56 bus_space_tag_t faa_bst; 57 bus_dma_tag_t faa_dmat; 58 int faa_phandle; 59 int faa_quiet; 60 }; 61 62 63 struct fdtbus_clock_controller_func { 64 struct clk * (*decode)(device_t, int, const void *, size_t); 65 }; 66 67 68 struct fdtbus_dai_controller_func { 69 audio_dai_tag_t (*get_tag)(device_t, const void *, size_t); 70 }; 71 72 73 struct fdtbus_dma_controller; 74 75 struct fdtbus_dma { 76 struct fdtbus_dma_controller *dma_dc; 77 void *dma_priv; 78 }; 79 80 enum fdtbus_dma_dir { 81 FDT_DMA_READ, /* device -> memory */ 82 FDT_DMA_WRITE /* memory -> device */ 83 }; 84 85 struct fdtbus_dma_opt { 86 int opt_bus_width; /* Bus width */ 87 int opt_burst_len; /* Burst length */ 88 int opt_swap; /* Enable data swapping */ 89 int opt_dblbuf; /* Enable double buffering */ 90 int opt_wrap_len; /* Address wrap-around window */ 91 }; 92 93 struct fdtbus_dma_req { 94 bus_dma_segment_t *dreq_segs; /* Memory */ 95 int dreq_nsegs; 96 97 bus_addr_t dreq_dev_phys; /* Device */ 98 int dreq_sel; /* Device selector */ 99 100 enum fdtbus_dma_dir dreq_dir; /* Transfer direction */ 101 102 int dreq_block_irq; /* Enable IRQ at end of block */ 103 int dreq_block_multi; /* Enable multiple block transfers */ 104 int dreq_flow; /* Enable flow control */ 105 106 struct fdtbus_dma_opt dreq_mem_opt; /* Memory options */ 107 struct fdtbus_dma_opt dreq_dev_opt; /* Device options */ 108 }; 109 110 struct fdtbus_dma_controller_func { 111 void * (*acquire)(device_t, const void *, size_t, 112 void (*)(void *), void *); 113 void (*release)(device_t, void *); 114 int (*transfer)(device_t, void *, struct fdtbus_dma_req *); 115 void (*halt)(device_t, void *); 116 }; 117 118 119 struct fdtbus_gpio_controller; 120 121 struct fdtbus_gpio_pin { 122 struct fdtbus_gpio_controller *gp_gc; 123 void *gp_priv; 124 }; 125 126 struct fdtbus_gpio_controller_func { 127 void * (*acquire)(device_t, const void *, size_t, int); 128 void (*release)(device_t, void *); 129 int (*read)(device_t, void *, bool); 130 void (*write)(device_t, void *, int, bool); 131 }; 132 133 134 /* flags for fdtbus_intr_establish */ 135 #define FDT_INTR_MPSAFE __BIT(0) 136 137 /* Interrupt trigger types defined by the FDT "interrupts" bindings. */ 138 #define FDT_INTR_TYPE_POS_EDGE __BIT(0) 139 #define FDT_INTR_TYPE_NEG_EDGE __BIT(1) 140 #define FDT_INTR_TYPE_DOUBLE_EDGE (FDT_INTR_TYPE_POS_EDGE | \ 141 FDT_INTR_TYPE_NEG_EDGE) 142 #define FDT_INTR_TYPE_HIGH_LEVEL __BIT(2) 143 #define FDT_INTR_TYPE_LOW_LEVEL __BIT(3) 144 145 struct fdtbus_interrupt_controller_func { 146 void * (*establish)(device_t, u_int *, int, int, 147 int (*)(void *), void *, const char *); 148 void (*disestablish)(device_t, void *); 149 bool (*intrstr)(device_t, u_int *, char *, size_t); 150 void (*mask)(device_t, void *); 151 void (*unmask)(device_t, void *); 152 }; 153 154 155 struct fdtbus_iommu_func { 156 bus_dma_tag_t (*map)(device_t, const u_int *, bus_dma_tag_t); 157 }; 158 159 160 struct fdtbus_mbox_channel { 161 struct fdtbus_mbox_controller *mb_ctlr; 162 void *mb_priv; 163 }; 164 165 struct fdtbus_mbox_controller_func { 166 void * (*mc_acquire)(device_t, const void *, size_t, void (*)(void *), 167 void *); 168 void (*mc_release)(device_t, void *); 169 int (*mc_recv)(device_t, void *, void *, size_t); 170 int (*mc_send)(device_t, void *, const void *, size_t); 171 }; 172 173 174 struct fdtbus_mmc_pwrseq; 175 176 struct fdtbus_mmc_pwrseq_func { 177 void (*pre_power_on)(device_t); 178 void (*post_power_on)(device_t); 179 void (*power_off)(device_t); 180 void (*reset)(device_t); 181 }; 182 183 184 struct fdtbus_phy_controller; 185 186 struct fdtbus_phy { 187 struct fdtbus_phy_controller *phy_pc; 188 void *phy_priv; 189 }; 190 191 struct fdtbus_phy_controller_func { 192 void * (*acquire)(device_t, const void *, size_t); 193 void (*release)(device_t, void *); 194 int (*enable)(device_t, void *, bool); 195 }; 196 197 198 struct fdtbus_pinctrl_controller; 199 200 struct fdtbus_pinctrl_pin { 201 struct fdtbus_pinctrl_controller *pp_pc; 202 void *pp_priv; 203 }; 204 205 struct fdtbus_pinctrl_controller_func { 206 int (*set_config)(device_t, const void *, size_t); 207 }; 208 209 210 struct fdtbus_power_controller; 211 212 struct fdtbus_power_controller_func { 213 void (*reset)(device_t); 214 void (*poweroff)(device_t); 215 }; 216 217 218 struct fdtbus_powerdomain_controller; 219 220 struct fdtbus_powerdomain_controller_func { 221 void (*pdc_enable)(device_t, const uint32_t *, bool); 222 }; 223 224 225 struct fdtbus_pwm_controller_func { 226 pwm_tag_t (*get_tag)(device_t, const void *, size_t); 227 }; 228 229 230 struct fdtbus_regulator_controller; 231 232 struct fdtbus_regulator { 233 struct fdtbus_regulator_controller *reg_rc; 234 }; 235 236 struct fdtbus_regulator_controller_func { 237 int (*acquire)(device_t); 238 void (*release)(device_t); 239 int (*enable)(device_t, bool); 240 int (*set_voltage)(device_t, u_int, u_int); 241 int (*get_voltage)(device_t, u_int *); 242 }; 243 244 245 struct fdtbus_reset_controller; 246 247 struct fdtbus_reset { 248 struct fdtbus_reset_controller *rst_rc; 249 void *rst_priv; 250 }; 251 252 struct fdtbus_reset_controller_func { 253 void * (*acquire)(device_t, const void *, size_t); 254 void (*release)(device_t, void *); 255 int (*reset_assert)(device_t, void *); 256 int (*reset_deassert)(device_t, void *); 257 }; 258 259 260 struct fdtbus_spi_controller_func { 261 struct spi_controller * (*get_controller)(device_t); 262 }; 263 264 265 struct syscon; 266 267 268 struct fdt_console { 269 int (*match)(int); 270 void (*consinit)(struct fdt_attach_args *, u_int); 271 }; 272 273 struct fdt_console_info { 274 const struct fdt_console *ops; 275 }; 276 277 struct fdt_phandle_data { 278 int phandle; 279 int count; 280 const u_int *values; 281 }; 282 283 #define _FDT_CONSOLE_REGISTER(name) \ 284 __link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo)); 285 286 #define FDT_CONSOLE(_name, _ops) \ 287 static const struct fdt_console_info __CONCAT(_name,_consinfo) = { \ 288 .ops = (_ops) \ 289 }; \ 290 _FDT_CONSOLE_REGISTER(_name) 291 292 struct fdt_opp_info { 293 const char * opp_compat; 294 bool (*opp_supported)(const int, const int); 295 }; 296 297 #define _FDT_OPP_REGISTER(name) \ 298 __link_set_add_rodata(fdt_opps, __CONCAT(name,_oppinfo)); 299 300 #define FDT_OPP(_name, _compat, _suppfn) \ 301 static const struct fdt_opp_info __CONCAT(_name,_oppinfo) = { \ 302 .opp_compat = (_compat), \ 303 .opp_supported = (_suppfn) \ 304 }; \ 305 _FDT_OPP_REGISTER(_name) 306 307 TAILQ_HEAD(fdt_conslist, fdt_console_info); 308 309 /* 310 * Platform-specific data 311 */ 312 313 struct fdt_platform { 314 const struct pmap_devmap * 315 (*fp_devmap)(void); 316 void (*fp_bootstrap)(void); 317 int (*fp_mpstart)(void); 318 void (*fp_startup)(void); 319 void (*fp_init_attach_args)(struct fdt_attach_args *); 320 void (*fp_device_register)(device_t, void *); 321 void (*fp_reset)(void); 322 void (*fp_delay)(u_int); 323 u_int (*fp_uart_freq)(void); 324 }; 325 326 struct fdt_platform_info { 327 const char * fpi_compat; 328 const struct fdt_platform * fpi_ops; 329 }; 330 331 #define FDT_PLATFORM_DEFAULT "" 332 333 #define _FDT_PLATFORM_REGISTER(name) \ 334 __link_set_add_rodata(fdt_platforms, __CONCAT(name,_platinfo)); 335 336 #define FDT_PLATFORM(_name, _compat, _ops) \ 337 static const struct fdt_platform_info __CONCAT(_name,_platinfo) = { \ 338 .fpi_compat = (_compat), \ 339 .fpi_ops = (_ops) \ 340 }; \ 341 _FDT_PLATFORM_REGISTER(_name) 342 343 const struct fdt_platform * 344 fdt_platform_find(void); 345 346 347 struct fdt_dma_range { 348 paddr_t dr_sysbase; 349 bus_addr_t dr_busbase; 350 bus_size_t dr_len; 351 }; 352 353 #define FDT_BUS_SPACE_FLAG_NONPOSTED_MMIO __BIT(0) 354 355 int fdtbus_register_clock_controller(device_t, int, 356 const struct fdtbus_clock_controller_func *); 357 int fdtbus_register_dai_controller(device_t, int, 358 const struct fdtbus_dai_controller_func *); 359 int fdtbus_register_dma_controller(device_t, int, 360 const struct fdtbus_dma_controller_func *); 361 int fdtbus_register_gpio_controller(device_t, int, 362 const struct fdtbus_gpio_controller_func *); 363 int fdtbus_register_i2c_controller(i2c_tag_t, int); 364 int fdtbus_register_interrupt_controller(device_t, int, 365 const struct fdtbus_interrupt_controller_func *); 366 int fdtbus_register_iommu(device_t, int, 367 const struct fdtbus_iommu_func *); 368 int fdtbus_register_mbox_controller(device_t, int, 369 const struct fdtbus_mbox_controller_func *); 370 int fdtbus_register_mmc_pwrseq(device_t, int, 371 const struct fdtbus_mmc_pwrseq_func *); 372 int fdtbus_register_pinctrl_config(device_t, int, 373 const struct fdtbus_pinctrl_controller_func *); 374 int fdtbus_register_power_controller(device_t, int, 375 const struct fdtbus_power_controller_func *); 376 int fdtbus_register_powerdomain_controller(device_t, int, 377 const struct fdtbus_powerdomain_controller_func *); 378 int fdtbus_register_phy_controller(device_t, int, 379 const struct fdtbus_phy_controller_func *); 380 int fdtbus_register_pwm_controller(device_t, int, 381 const struct fdtbus_pwm_controller_func *); 382 int fdtbus_register_regulator_controller(device_t, int, 383 const struct fdtbus_regulator_controller_func *); 384 int fdtbus_register_reset_controller(device_t, int, 385 const struct fdtbus_reset_controller_func *); 386 int fdtbus_register_spi_controller(device_t, int, 387 const struct fdtbus_spi_controller_func *); 388 int fdtbus_register_syscon(device_t, int, struct syscon *); 389 390 void fdtbus_set_decoderegprop(bool); 391 392 int fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *); 393 int fdtbus_get_reg_byname(int, const char *, bus_addr_t *, 394 bus_size_t *); 395 int fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *); 396 int fdtbus_get_addr_cells(int); 397 int fdtbus_get_size_cells(int); 398 uint64_t fdtbus_get_cells(const uint8_t *, int); 399 int fdtbus_get_phandle(int, const char *); 400 int fdtbus_get_phandle_with_data(int, const char *, const char *, 401 int, struct fdt_phandle_data *); 402 int fdtbus_get_phandle_from_native(int); 403 404 405 struct clk * fdtbus_clock_get(int, const char *); 406 struct clk * fdtbus_clock_get_index(int, u_int); 407 struct clk * fdtbus_clock_byname(const char *); 408 void fdtbus_clock_assign(int); 409 u_int fdtbus_clock_count(int, const char *); 410 int fdtbus_clock_enable(int, const char *, bool); 411 int fdtbus_clock_enable_index(int, u_int, bool); 412 413 audio_dai_tag_t fdtbus_dai_acquire(int, const char *); 414 audio_dai_tag_t fdtbus_dai_acquire_index(int, const char *, int); 415 416 struct fdtbus_dma * 417 fdtbus_dma_get(int, const char *, void (*)(void *), void *); 418 struct fdtbus_dma * 419 fdtbus_dma_get_index(int, u_int, void (*)(void *), 420 void *); 421 void fdtbus_dma_put(struct fdtbus_dma *); 422 int fdtbus_dma_transfer(struct fdtbus_dma *, 423 struct fdtbus_dma_req *); 424 void fdtbus_dma_halt(struct fdtbus_dma *); 425 426 int fdtbus_gpio_count(int, const char *); 427 struct fdtbus_gpio_pin * 428 fdtbus_gpio_acquire(int, const char *, int); 429 struct fdtbus_gpio_pin * 430 fdtbus_gpio_acquire_index(int, const char *, int, int); 431 void fdtbus_gpio_release(struct fdtbus_gpio_pin *); 432 int fdtbus_gpio_read(struct fdtbus_gpio_pin *); 433 void fdtbus_gpio_write(struct fdtbus_gpio_pin *, int); 434 int fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *); 435 void fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int); 436 437 i2c_tag_t fdtbus_i2c_get_tag(int); 438 i2c_tag_t fdtbus_i2c_acquire(int, const char *); 439 440 void * fdtbus_intr_establish(int, u_int, int, int, 441 int (*func)(void *), void *arg); 442 void * fdtbus_intr_establish_xname(int, u_int, int, int, 443 int (*func)(void *), void *arg, const char *); 444 void * fdtbus_intr_establish_byname(int, const char *, int, int, 445 int (*func)(void *), void *arg, const char *); 446 void * fdtbus_intr_establish_raw(int, const u_int *, int, int, 447 int (*func)(void *), void *arg, const char *); 448 void fdtbus_intr_mask(int, void *); 449 void fdtbus_intr_unmask(int, void *); 450 void fdtbus_intr_disestablish(int, void *); 451 bool fdtbus_intr_str(int, u_int, char *, size_t); 452 bool fdtbus_intr_str_raw(int, const u_int *, char *, size_t); 453 int fdtbus_intr_parent(int); 454 455 bus_dma_tag_t fdtbus_iommu_map(int, u_int, bus_dma_tag_t); 456 bus_dma_tag_t fdtbus_iommu_map_pci(int, uint32_t, bus_dma_tag_t); 457 458 struct fdtbus_mbox_channel * 459 fdtbus_mbox_get(int, const char *, void (*)(void *), void *); 460 struct fdtbus_mbox_channel * 461 fdtbus_mbox_get_index(int, u_int, void (*)(void *), 462 void *); 463 int fdtbus_mbox_send(struct fdtbus_mbox_channel *, const void *, size_t); 464 int fdtbus_mbox_recv(struct fdtbus_mbox_channel *, void *, size_t); 465 void fdtbus_mbox_put(struct fdtbus_mbox_channel *); 466 467 struct fdtbus_mmc_pwrseq * 468 fdtbus_mmc_pwrseq_get(int); 469 void fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *); 470 void fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *); 471 void fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *); 472 void fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *); 473 474 struct fdtbus_phy * 475 fdtbus_phy_get(int, const char *); 476 struct fdtbus_phy * 477 fdtbus_phy_get_index(int, u_int); 478 void fdtbus_phy_put(struct fdtbus_phy *); 479 device_t fdtbus_phy_device(struct fdtbus_phy *); 480 int fdtbus_phy_enable(struct fdtbus_phy *, bool); 481 482 int fdtbus_pinctrl_set_config_index(int, u_int); 483 int fdtbus_pinctrl_set_config(int, const char *); 484 bool fdtbus_pinctrl_has_config(int, const char *); 485 const char * fdtbus_pinctrl_parse_function(int); 486 const void * fdtbus_pinctrl_parse_pins(int, int *); 487 const char * fdtbus_pinctrl_parse_groups(int, int *); 488 const u_int * fdtbus_pinctrl_parse_pinmux(int, int *); 489 int fdtbus_pinctrl_parse_bias(int, int *); 490 int fdtbus_pinctrl_parse_drive(int); 491 int fdtbus_pinctrl_parse_drive_strength(int); 492 int fdtbus_pinctrl_parse_input_output(int, int *); 493 494 pwm_tag_t fdtbus_pwm_acquire(int, const char *); 495 pwm_tag_t fdtbus_pwm_acquire_index(int, const char *, int); 496 497 struct fdtbus_regulator * 498 fdtbus_regulator_acquire(int, const char *); 499 void fdtbus_regulator_release(struct fdtbus_regulator *); 500 int fdtbus_regulator_enable(struct fdtbus_regulator *); 501 int fdtbus_regulator_disable(struct fdtbus_regulator *); 502 int fdtbus_regulator_set_voltage(struct fdtbus_regulator *, 503 u_int, u_int); 504 int fdtbus_regulator_get_voltage(struct fdtbus_regulator *, 505 u_int *); 506 int fdtbus_regulator_supports_voltage(struct fdtbus_regulator *, 507 u_int, u_int); 508 509 struct fdtbus_reset * 510 fdtbus_reset_get(int, const char *); 511 struct fdtbus_reset * 512 fdtbus_reset_get_index(int, u_int); 513 void fdtbus_reset_put(struct fdtbus_reset *); 514 int fdtbus_reset_assert(struct fdtbus_reset *); 515 int fdtbus_reset_deassert(struct fdtbus_reset *); 516 517 int fdtbus_todr_attach(device_t, int, todr_chip_handle_t); 518 519 void fdtbus_power_reset(void); 520 void fdtbus_power_poweroff(void); 521 522 int fdtbus_powerdomain_enable(int); 523 int fdtbus_powerdomain_enable_index(int, int); 524 int fdtbus_powerdomain_disable(int); 525 int fdtbus_powerdomain_disable_index(int, int); 526 527 struct syscon * fdtbus_syscon_acquire(int, const char *); 528 struct syscon * fdtbus_syscon_lookup(int); 529 530 531 device_t fdtbus_attach_i2cbus(device_t, int, i2c_tag_t, cfprint_t); 532 device_t fdtbus_attach_spibus(device_t, int, cfprint_t); 533 534 bool fdtbus_init(const void *); 535 const void * fdtbus_get_data(void); 536 int fdtbus_phandle2offset(int); 537 int fdtbus_offset2phandle(int); 538 bool fdtbus_get_path(int, char *, size_t); 539 540 const struct fdt_console * 541 fdtbus_get_console(void); 542 543 const char * fdtbus_get_stdout_path(void); 544 int fdtbus_get_stdout_phandle(void); 545 int fdtbus_get_stdout_speed(void); 546 tcflag_t fdtbus_get_stdout_flags(void); 547 548 bool fdtbus_status_okay(int); 549 550 const void * fdtbus_get_prop(int, const char *, int *); 551 const char * fdtbus_get_string(int, const char *); 552 const char * fdtbus_get_string_index(int, const char *, u_int); 553 int fdtbus_get_index(int, const char *, const char *, u_int *); 554 555 void fdtbus_cpus_md_attach(device_t, device_t, void *); 556 557 void fdt_add_bus(device_t, int, struct fdt_attach_args *); 558 void fdt_add_bus_match(device_t, int, struct fdt_attach_args *, 559 bool (*)(void *, int), void *); 560 void fdt_add_child(device_t, int, struct fdt_attach_args *, u_int); 561 562 void fdt_remove_byhandle(int); 563 void fdt_remove_bycompat(const char *[]); 564 int fdt_find_with_property(const char *, int *); 565 566 int fdtbus_print(void *, const char *); 567 568 bus_dma_tag_t fdtbus_dma_tag_create(int, const struct fdt_dma_range *, 569 u_int); 570 bus_space_tag_t fdtbus_bus_tag_create(int, uint32_t); 571 572 573 #endif /* _DEV_FDT_FDTVAR_H_ */ 574