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