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