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