Home | History | Annotate | Line # | Download | only in fdt
fdtvar.h revision 1.47
      1 /* $NetBSD: fdtvar.h,v 1.47 2019/01/26 14:38:30 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_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 #define	_FDT_CONSOLE_REGISTER(name)	\
    236 	__link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo));
    237 
    238 #define	FDT_CONSOLE(_name, _ops)					\
    239 static const struct fdt_console_info __CONCAT(_name,_consinfo) = {	\
    240 	.ops = (_ops)							\
    241 };									\
    242 _FDT_CONSOLE_REGISTER(_name)
    243 
    244 TAILQ_HEAD(fdt_conslist, fdt_console_info);
    245 
    246 int		fdtbus_register_interrupt_controller(device_t, int,
    247 		    const struct fdtbus_interrupt_controller_func *);
    248 int		fdtbus_register_i2c_controller(device_t, int,
    249 		    const struct fdtbus_i2c_controller_func *);
    250 int		fdtbus_register_gpio_controller(device_t, int,
    251 		    const struct fdtbus_gpio_controller_func *);
    252 int		fdtbus_register_pinctrl_config(device_t, int,
    253 		    const struct fdtbus_pinctrl_controller_func *);
    254 int		fdtbus_register_regulator_controller(device_t, int,
    255 		    const struct fdtbus_regulator_controller_func *);
    256 int		fdtbus_register_clock_controller(device_t, int,
    257 		    const struct fdtbus_clock_controller_func *);
    258 int		fdtbus_register_reset_controller(device_t, int,
    259 		    const struct fdtbus_reset_controller_func *);
    260 int		fdtbus_register_dai_controller(device_t, int,
    261 		    const struct fdtbus_dai_controller_func *);
    262 int		fdtbus_register_dma_controller(device_t, int,
    263 		    const struct fdtbus_dma_controller_func *);
    264 int		fdtbus_register_power_controller(device_t, int,
    265 		    const struct fdtbus_power_controller_func *);
    266 int		fdtbus_register_phy_controller(device_t, int,
    267 		    const struct fdtbus_phy_controller_func *);
    268 int		fdtbus_register_pwm_controller(device_t, int,
    269 		    const struct fdtbus_pwm_controller_func *);
    270 int		fdtbus_register_mmc_pwrseq(device_t, int,
    271 		    const struct fdtbus_mmc_pwrseq_func *);
    272 int		fdtbus_register_syscon(device_t, int, struct syscon *);
    273 
    274 void		fdtbus_set_decoderegprop(bool);
    275 
    276 int		fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
    277 int		fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
    278 		    bus_size_t *);
    279 int		fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *);
    280 int		fdtbus_get_phandle(int, const char *);
    281 int		fdtbus_get_phandle_from_native(int);
    282 i2c_tag_t	fdtbus_get_i2c_tag(int);
    283 void *		fdtbus_intr_establish(int, u_int, int, int,
    284 		    int (*func)(void *), void *arg);
    285 void *		fdtbus_intr_establish_raw(int, const u_int *, int, int,
    286 		    int (*func)(void *), void *arg);
    287 void		fdtbus_intr_disestablish(int, void *);
    288 bool		fdtbus_intr_str(int, u_int, char *, size_t);
    289 bool		fdtbus_intr_str_raw(int, const u_int *, char *, size_t);
    290 struct fdtbus_gpio_pin *fdtbus_gpio_acquire(int, const char *, int);
    291 struct fdtbus_gpio_pin *fdtbus_gpio_acquire_index(int, const char *, int, int);
    292 void		fdtbus_gpio_release(struct fdtbus_gpio_pin *);
    293 int		fdtbus_gpio_read(struct fdtbus_gpio_pin *);
    294 void		fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
    295 int		fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
    296 void		fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
    297 audio_dai_tag_t	fdtbus_dai_acquire(int, const char *);
    298 audio_dai_tag_t	fdtbus_dai_acquire_index(int, const char *, int);
    299 pwm_tag_t	fdtbus_pwm_acquire(int, const char *);
    300 pwm_tag_t	fdtbus_pwm_acquire_index(int, const char *, int);
    301 void		fdtbus_pinctrl_configure(void);
    302 int		fdtbus_pinctrl_set_config_index(int, u_int);
    303 int		fdtbus_pinctrl_set_config(int, const char *);
    304 const char *	fdtbus_pinctrl_parse_function(int);
    305 const void *	fdtbus_pinctrl_parse_pins(int, int *);
    306 const char *	fdtbus_pinctrl_parse_groups(int, int *);
    307 const u_int *	fdtbus_pinctrl_parse_pinmux(int, int *);
    308 int		fdtbus_pinctrl_parse_bias(int, int *);
    309 int		fdtbus_pinctrl_parse_drive(int);
    310 int		fdtbus_pinctrl_parse_drive_strength(int);
    311 int		fdtbus_pinctrl_parse_input_output(int, int *);
    312 struct fdtbus_regulator *fdtbus_regulator_acquire(int, const char *);
    313 void		fdtbus_regulator_release(struct fdtbus_regulator *);
    314 int		fdtbus_regulator_enable(struct fdtbus_regulator *);
    315 int		fdtbus_regulator_disable(struct fdtbus_regulator *);
    316 int		fdtbus_regulator_set_voltage(struct fdtbus_regulator *,
    317 		    u_int, u_int);
    318 int		fdtbus_regulator_get_voltage(struct fdtbus_regulator *,
    319 		    u_int *);
    320 int		fdtbus_regulator_supports_voltage(struct fdtbus_regulator *,
    321 		    u_int, u_int);
    322 struct syscon *	fdtbus_syscon_acquire(int, const char *);
    323 struct syscon *	fdtbus_syscon_lookup(int);
    324 
    325 struct fdtbus_dma *fdtbus_dma_get(int, const char *, void (*)(void *), void *);
    326 struct fdtbus_dma *fdtbus_dma_get_index(int, u_int, void (*)(void *),
    327 		    void *);
    328 void		fdtbus_dma_put(struct fdtbus_dma *);
    329 int		fdtbus_dma_transfer(struct fdtbus_dma *,
    330 		    struct fdtbus_dma_req *);
    331 void		fdtbus_dma_halt(struct fdtbus_dma *);
    332 
    333 struct clk *	fdtbus_clock_get(int, const char *);
    334 struct clk *	fdtbus_clock_get_index(int, u_int);
    335 struct clk *	fdtbus_clock_byname(const char *);
    336 void		fdtbus_clock_assign(int);
    337 
    338 struct fdtbus_reset *fdtbus_reset_get(int, const char *);
    339 struct fdtbus_reset *fdtbus_reset_get_index(int, u_int);
    340 void		fdtbus_reset_put(struct fdtbus_reset *);
    341 int		fdtbus_reset_assert(struct fdtbus_reset *);
    342 int		fdtbus_reset_deassert(struct fdtbus_reset *);
    343 
    344 struct fdtbus_phy *fdtbus_phy_get(int, const char *);
    345 struct fdtbus_phy *fdtbus_phy_get_index(int, u_int);
    346 void		fdtbus_phy_put(struct fdtbus_phy *);
    347 int		fdtbus_phy_enable(struct fdtbus_phy *, bool);
    348 
    349 struct fdtbus_mmc_pwrseq *fdtbus_mmc_pwrseq_get(int);
    350 void		fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
    351 void		fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
    352 void		fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
    353 void		fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
    354 
    355 int		fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
    356 
    357 void		fdtbus_power_reset(void);
    358 void		fdtbus_power_poweroff(void);
    359 
    360 device_t	fdtbus_attach_i2cbus(device_t, int, i2c_tag_t, cfprint_t);
    361 
    362 bool		fdtbus_set_data(const void *);
    363 const void *	fdtbus_get_data(void);
    364 int		fdtbus_phandle2offset(int);
    365 int		fdtbus_offset2phandle(int);
    366 bool		fdtbus_get_path(int, char *, size_t);
    367 
    368 const struct fdt_console *fdtbus_get_console(void);
    369 
    370 const char *	fdtbus_get_stdout_path(void);
    371 int		fdtbus_get_stdout_phandle(void);
    372 int		fdtbus_get_stdout_speed(void);
    373 tcflag_t	fdtbus_get_stdout_flags(void);
    374 
    375 bool		fdtbus_status_okay(int);
    376 
    377 const void *	fdtbus_get_prop(int, const char *, int *);
    378 const char *	fdtbus_get_string(int, const char *);
    379 const char *	fdtbus_get_string_index(int, const char *, u_int);
    380 
    381 void		fdt_add_bus(device_t, int, struct fdt_attach_args *);
    382 void		fdt_add_bus_match(device_t, int, struct fdt_attach_args *,
    383 				  bool (*)(void *, int), void *);
    384 void		fdt_add_child(device_t, int, struct fdt_attach_args *, u_int);
    385 
    386 void		fdt_remove_byhandle(int);
    387 void		fdt_remove_bycompat(const char *[]);
    388 int		fdt_find_with_property(const char *, int *);
    389 int		fdtbus_print(void *, const char *);
    390 
    391 #endif /* _DEV_FDT_FDTVAR_H */
    392