Home | History | Annotate | Line # | Download | only in fdt
fdtvar.h revision 1.70.12.2
      1 /* $NetBSD: fdtvar.h,v 1.70.12.2 2022/01/18 00:14:20 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) 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 /* flags for fdtbus_intr_establish */
     63 #define FDT_INTR_MPSAFE	__BIT(0)
     64 
     65 /* Interrupt trigger types defined by the FDT "interrupts" bindings. */
     66 #define	FDT_INTR_TYPE_POS_EDGE		__BIT(0)
     67 #define	FDT_INTR_TYPE_NEG_EDGE		__BIT(1)
     68 #define	FDT_INTR_TYPE_DOUBLE_EDGE	(FDT_INTR_TYPE_POS_EDGE | \
     69 					 FDT_INTR_TYPE_NEG_EDGE)
     70 #define	FDT_INTR_TYPE_HIGH_LEVEL	__BIT(2)
     71 #define	FDT_INTR_TYPE_LOW_LEVEL		__BIT(3)
     72 
     73 struct fdtbus_interrupt_controller_func {
     74 	void *	(*establish)(device_t, u_int *, int, int,
     75 			     int (*)(void *), void *, const char *);
     76 	void	(*disestablish)(device_t, void *);
     77 	bool	(*intrstr)(device_t, u_int *, char *, size_t);
     78 	void	(*mask)(device_t, void *);
     79 	void	(*unmask)(device_t, void *);
     80 };
     81 
     82 struct fdtbus_gpio_controller;
     83 
     84 struct fdtbus_gpio_pin {
     85 	struct fdtbus_gpio_controller *gp_gc;
     86 	void *gp_priv;
     87 };
     88 
     89 struct fdtbus_gpio_controller_func {
     90 	void *	(*acquire)(device_t, const void *, size_t, int);
     91 	void	(*release)(device_t, void *);
     92 	int	(*read)(device_t, void *, bool);
     93 	void	(*write)(device_t, void *, int, bool);
     94 };
     95 
     96 struct fdtbus_pinctrl_controller;
     97 
     98 struct fdtbus_pinctrl_pin {
     99 	struct fdtbus_pinctrl_controller *pp_pc;
    100 	void *pp_priv;
    101 };
    102 
    103 struct fdtbus_pinctrl_controller_func {
    104 	int (*set_config)(device_t, const void *, size_t);
    105 };
    106 
    107 struct fdtbus_regulator_controller;
    108 
    109 struct fdtbus_regulator {
    110 	struct fdtbus_regulator_controller *reg_rc;
    111 };
    112 
    113 struct fdtbus_regulator_controller_func {
    114 	int	(*acquire)(device_t);
    115 	void	(*release)(device_t);
    116 	int	(*enable)(device_t, bool);
    117 	int	(*set_voltage)(device_t, u_int, u_int);
    118 	int	(*get_voltage)(device_t, u_int *);
    119 };
    120 
    121 struct fdtbus_clock_controller_func {
    122 	struct clk *	(*decode)(device_t, int, const void *, size_t);
    123 };
    124 
    125 struct fdtbus_reset_controller;
    126 
    127 struct fdtbus_reset {
    128 	struct fdtbus_reset_controller *rst_rc;
    129 	void *rst_priv;
    130 };
    131 
    132 struct fdtbus_reset_controller_func {
    133 	void *	(*acquire)(device_t, const void *, size_t);
    134 	void	(*release)(device_t, void *);
    135 	int	(*reset_assert)(device_t, void *);
    136 	int	(*reset_deassert)(device_t, void *);
    137 };
    138 
    139 struct fdtbus_dai_controller_func {
    140 	audio_dai_tag_t (*get_tag)(device_t, const void *, size_t);
    141 };
    142 
    143 struct fdtbus_dma_controller;
    144 
    145 struct fdtbus_dma {
    146 	struct fdtbus_dma_controller *dma_dc;
    147 	void *dma_priv;
    148 };
    149 
    150 enum fdtbus_dma_dir {
    151 	FDT_DMA_READ,		/* device -> memory */
    152 	FDT_DMA_WRITE		/* memory -> device */
    153 };
    154 
    155 struct fdtbus_dma_opt {
    156 	int opt_bus_width;		/* Bus width */
    157 	int opt_burst_len;		/* Burst length */
    158 	int opt_swap;			/* Enable data swapping */
    159 	int opt_dblbuf;			/* Enable double buffering */
    160 	int opt_wrap_len;		/* Address wrap-around window */
    161 };
    162 
    163 struct fdtbus_dma_req {
    164 	bus_dma_segment_t *dreq_segs;	/* Memory */
    165 	int dreq_nsegs;
    166 
    167 	bus_addr_t dreq_dev_phys;	/* Device */
    168 	int dreq_sel;			/* Device selector */
    169 
    170 	enum fdtbus_dma_dir dreq_dir;	/* Transfer direction */
    171 
    172 	int dreq_block_irq;		/* Enable IRQ at end of block */
    173 	int dreq_block_multi;		/* Enable multiple block transfers */
    174 	int dreq_flow;			/* Enable flow control */
    175 
    176 	struct fdtbus_dma_opt dreq_mem_opt;	/* Memory options */
    177 	struct fdtbus_dma_opt dreq_dev_opt;	/* Device options */
    178 };
    179 
    180 struct fdtbus_dma_controller_func {
    181 	void *	(*acquire)(device_t, const void *, size_t,
    182 			   void (*)(void *), void *);
    183 	void	(*release)(device_t, void *);
    184 	int	(*transfer)(device_t, void *, struct fdtbus_dma_req *);
    185 	void	(*halt)(device_t, void *);
    186 };
    187 
    188 struct fdtbus_power_controller;
    189 
    190 struct fdtbus_power_controller_func {
    191 	void 	(*reset)(device_t);
    192 	void	(*poweroff)(device_t);
    193 };
    194 
    195 struct fdtbus_phy_controller;
    196 
    197 struct fdtbus_phy {
    198 	struct fdtbus_phy_controller *phy_pc;
    199 	void *phy_priv;
    200 };
    201 
    202 struct fdtbus_phy_controller_func {
    203 	void *	(*acquire)(device_t, const void *, size_t);
    204 	void	(*release)(device_t, void *);
    205 	int	(*enable)(device_t, void *, bool);
    206 };
    207 
    208 struct fdtbus_pwm_controller_func {
    209 	pwm_tag_t (*get_tag)(device_t, const void *, size_t);
    210 };
    211 
    212 struct fdtbus_mmc_pwrseq;
    213 
    214 struct fdtbus_mmc_pwrseq_func {
    215 	void	(*pre_power_on)(device_t);
    216 	void	(*post_power_on)(device_t);
    217 	void	(*power_off)(device_t);
    218 	void	(*reset)(device_t);
    219 };
    220 
    221 struct syscon;
    222 
    223 struct fdt_console {
    224 	int	(*match)(int);
    225 	void	(*consinit)(struct fdt_attach_args *, u_int);
    226 };
    227 
    228 struct fdt_console_info {
    229 	const struct fdt_console *ops;
    230 };
    231 
    232 struct fdt_phandle_data {
    233 	int phandle;
    234 	int count;
    235 	const u_int *values;
    236 };
    237 
    238 #define	_FDT_CONSOLE_REGISTER(name)	\
    239 	__link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo));
    240 
    241 #define	FDT_CONSOLE(_name, _ops)					\
    242 static const struct fdt_console_info __CONCAT(_name,_consinfo) = {	\
    243 	.ops = (_ops)							\
    244 };									\
    245 _FDT_CONSOLE_REGISTER(_name)
    246 
    247 struct fdt_opp_info {
    248 	const char *	opp_compat;
    249 	bool		(*opp_supported)(const int, const int);
    250 };
    251 
    252 #define	_FDT_OPP_REGISTER(name)		\
    253 	__link_set_add_rodata(fdt_opps, __CONCAT(name,_oppinfo));
    254 
    255 #define	FDT_OPP(_name, _compat, _suppfn)				\
    256 static const struct fdt_opp_info __CONCAT(_name,_oppinfo) = {		\
    257 	.opp_compat = (_compat),					\
    258 	.opp_supported = (_suppfn)					\
    259 };									\
    260 _FDT_OPP_REGISTER(_name)
    261 
    262 TAILQ_HEAD(fdt_conslist, fdt_console_info);
    263 
    264 struct fdt_dma_range {
    265 	paddr_t		dr_sysbase;
    266 	bus_addr_t	dr_busbase;
    267 	bus_size_t	dr_len;
    268 };
    269 
    270 int		fdtbus_register_interrupt_controller(device_t, int,
    271 		    const struct fdtbus_interrupt_controller_func *);
    272 int		fdtbus_register_i2c_controller(i2c_tag_t, int);
    273 int		fdtbus_register_gpio_controller(device_t, int,
    274 		    const struct fdtbus_gpio_controller_func *);
    275 int		fdtbus_register_pinctrl_config(device_t, int,
    276 		    const struct fdtbus_pinctrl_controller_func *);
    277 int		fdtbus_register_regulator_controller(device_t, int,
    278 		    const struct fdtbus_regulator_controller_func *);
    279 int		fdtbus_register_clock_controller(device_t, int,
    280 		    const struct fdtbus_clock_controller_func *);
    281 int		fdtbus_register_reset_controller(device_t, int,
    282 		    const struct fdtbus_reset_controller_func *);
    283 int		fdtbus_register_dai_controller(device_t, int,
    284 		    const struct fdtbus_dai_controller_func *);
    285 int		fdtbus_register_dma_controller(device_t, int,
    286 		    const struct fdtbus_dma_controller_func *);
    287 int		fdtbus_register_power_controller(device_t, int,
    288 		    const struct fdtbus_power_controller_func *);
    289 int		fdtbus_register_phy_controller(device_t, int,
    290 		    const struct fdtbus_phy_controller_func *);
    291 int		fdtbus_register_pwm_controller(device_t, int,
    292 		    const struct fdtbus_pwm_controller_func *);
    293 int		fdtbus_register_mmc_pwrseq(device_t, int,
    294 		    const struct fdtbus_mmc_pwrseq_func *);
    295 int		fdtbus_register_syscon(device_t, int, struct syscon *);
    296 
    297 void		fdtbus_set_decoderegprop(bool);
    298 
    299 int		fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
    300 int		fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
    301 		    bus_size_t *);
    302 int		fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *);
    303 int		fdtbus_get_addr_cells(int);
    304 int		fdtbus_get_size_cells(int);
    305 uint64_t	fdtbus_get_cells(const uint8_t *, int);
    306 int		fdtbus_get_phandle(int, const char *);
    307 int		fdtbus_get_phandle_with_data(int, const char *, const char *,
    308 		    int, struct fdt_phandle_data *);
    309 int		fdtbus_get_phandle_from_native(int);
    310 i2c_tag_t	fdtbus_get_i2c_tag(int);
    311 i2c_tag_t	fdtbus_i2c_acquire(int, const char *);
    312 void *		fdtbus_intr_establish(int, u_int, int, int,
    313 		    int (*func)(void *), void *arg);
    314 void *		fdtbus_intr_establish_xname(int, u_int, int, int,
    315 		    int (*func)(void *), void *arg, const char *);
    316 void *		fdtbus_intr_establish_byname(int, const char *, int, int,
    317 		    int (*func)(void *), void *arg, const char *);
    318 void *		fdtbus_intr_establish_raw(int, const u_int *, int, int,
    319 		    int (*func)(void *), void *arg, const char *);
    320 void		fdtbus_intr_mask(int, void *);
    321 void		fdtbus_intr_unmask(int, void *);
    322 void		fdtbus_intr_disestablish(int, void *);
    323 bool		fdtbus_intr_str(int, u_int, char *, size_t);
    324 bool		fdtbus_intr_str_raw(int, const u_int *, char *, size_t);
    325 int		fdtbus_gpio_count(int, const char *);
    326 struct fdtbus_gpio_pin *fdtbus_gpio_acquire(int, const char *, int);
    327 struct fdtbus_gpio_pin *fdtbus_gpio_acquire_index(int, const char *, int, int);
    328 void		fdtbus_gpio_release(struct fdtbus_gpio_pin *);
    329 int		fdtbus_gpio_read(struct fdtbus_gpio_pin *);
    330 void		fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
    331 int		fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
    332 void		fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
    333 audio_dai_tag_t	fdtbus_dai_acquire(int, const char *);
    334 audio_dai_tag_t	fdtbus_dai_acquire_index(int, const char *, int);
    335 pwm_tag_t	fdtbus_pwm_acquire(int, const char *);
    336 pwm_tag_t	fdtbus_pwm_acquire_index(int, const char *, int);
    337 int		fdtbus_pinctrl_set_config_index(int, u_int);
    338 int		fdtbus_pinctrl_set_config(int, const char *);
    339 bool		fdtbus_pinctrl_has_config(int, const char *);
    340 const char *	fdtbus_pinctrl_parse_function(int);
    341 const void *	fdtbus_pinctrl_parse_pins(int, int *);
    342 const char *	fdtbus_pinctrl_parse_groups(int, int *);
    343 const u_int *	fdtbus_pinctrl_parse_pinmux(int, int *);
    344 int		fdtbus_pinctrl_parse_bias(int, int *);
    345 int		fdtbus_pinctrl_parse_drive(int);
    346 int		fdtbus_pinctrl_parse_drive_strength(int);
    347 int		fdtbus_pinctrl_parse_input_output(int, int *);
    348 struct fdtbus_regulator *fdtbus_regulator_acquire(int, const char *);
    349 void		fdtbus_regulator_release(struct fdtbus_regulator *);
    350 int		fdtbus_regulator_enable(struct fdtbus_regulator *);
    351 int		fdtbus_regulator_disable(struct fdtbus_regulator *);
    352 int		fdtbus_regulator_set_voltage(struct fdtbus_regulator *,
    353 		    u_int, u_int);
    354 int		fdtbus_regulator_get_voltage(struct fdtbus_regulator *,
    355 		    u_int *);
    356 int		fdtbus_regulator_supports_voltage(struct fdtbus_regulator *,
    357 		    u_int, u_int);
    358 struct syscon *	fdtbus_syscon_acquire(int, const char *);
    359 struct syscon *	fdtbus_syscon_lookup(int);
    360 
    361 struct fdtbus_dma *fdtbus_dma_get(int, const char *, void (*)(void *), void *);
    362 struct fdtbus_dma *fdtbus_dma_get_index(int, u_int, void (*)(void *),
    363 		    void *);
    364 void		fdtbus_dma_put(struct fdtbus_dma *);
    365 int		fdtbus_dma_transfer(struct fdtbus_dma *,
    366 		    struct fdtbus_dma_req *);
    367 void		fdtbus_dma_halt(struct fdtbus_dma *);
    368 
    369 struct clk *	fdtbus_clock_get(int, const char *);
    370 struct clk *	fdtbus_clock_get_index(int, u_int);
    371 struct clk *	fdtbus_clock_byname(const char *);
    372 void		fdtbus_clock_assign(int);
    373 u_int		fdtbus_clock_count(int, const char *);
    374 int		fdtbus_clock_enable(int, const char *, bool);
    375 int		fdtbus_clock_enable_index(int, u_int, bool);
    376 
    377 struct fdtbus_reset *fdtbus_reset_get(int, const char *);
    378 struct fdtbus_reset *fdtbus_reset_get_index(int, u_int);
    379 void		fdtbus_reset_put(struct fdtbus_reset *);
    380 int		fdtbus_reset_assert(struct fdtbus_reset *);
    381 int		fdtbus_reset_deassert(struct fdtbus_reset *);
    382 
    383 struct fdtbus_phy *fdtbus_phy_get(int, const char *);
    384 struct fdtbus_phy *fdtbus_phy_get_index(int, u_int);
    385 void		fdtbus_phy_put(struct fdtbus_phy *);
    386 device_t	fdtbus_phy_device(struct fdtbus_phy *);
    387 int		fdtbus_phy_enable(struct fdtbus_phy *, bool);
    388 
    389 struct fdtbus_mmc_pwrseq *fdtbus_mmc_pwrseq_get(int);
    390 void		fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
    391 void		fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
    392 void		fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
    393 void		fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
    394 
    395 int		fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
    396 
    397 void		fdtbus_power_reset(void);
    398 void		fdtbus_power_poweroff(void);
    399 
    400 bool		fdtbus_init(const void *);
    401 const void *	fdtbus_get_data(void);
    402 int		fdtbus_phandle2offset(int);
    403 int		fdtbus_offset2phandle(int);
    404 bool		fdtbus_get_path(int, char *, size_t);
    405 
    406 const struct fdt_console *fdtbus_get_console(void);
    407 
    408 const char *	fdtbus_get_stdout_path(void);
    409 int		fdtbus_get_stdout_phandle(void);
    410 int		fdtbus_get_stdout_speed(void);
    411 tcflag_t	fdtbus_get_stdout_flags(void);
    412 
    413 bool		fdtbus_status_okay(int);
    414 
    415 const void *	fdtbus_get_prop(int, const char *, int *);
    416 const char *	fdtbus_get_string(int, const char *);
    417 const char *	fdtbus_get_string_index(int, const char *, u_int);
    418 int		fdtbus_get_index(int, const char *, const char *, u_int *);
    419 
    420 void		fdt_add_bus(device_t, int, struct fdt_attach_args *);
    421 void		fdt_add_bus_match(device_t, int, struct fdt_attach_args *,
    422 				  bool (*)(void *, int), void *);
    423 void		fdt_add_child(device_t, int, struct fdt_attach_args *, u_int);
    424 
    425 void		fdt_remove_byhandle(int);
    426 void		fdt_remove_bycompat(const char *[]);
    427 int		fdt_find_with_property(const char *, int *);
    428 
    429 int		fdtbus_print(void *, const char *);
    430 
    431 bus_dma_tag_t	fdtbus_dma_tag_create(int, const struct fdt_dma_range *,
    432 		    u_int);
    433 
    434 #endif /* _DEV_FDT_FDTVAR_H_ */
    435