xref: /netbsd-src/sys/dev/fdt/fdtvar.h (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* $NetBSD: fdtvar.h,v 1.29 2018/04/07 18:05:08 bouyer 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/termios.h>
35 
36 #include <dev/i2c/i2cvar.h>
37 #include <dev/clk/clk.h>
38 
39 #include <dev/clock_subr.h>
40 
41 #include <dev/ofw/openfirm.h>
42 
43 struct fdt_attach_args {
44 	const char *faa_name;
45 	bus_space_tag_t faa_bst;
46 	bus_space_tag_t faa_a4x_bst;
47 	bus_dma_tag_t faa_dmat;
48 	int faa_phandle;
49 	int faa_quiet;
50 };
51 
52 /* flags for fdtbus_intr_establish */
53 #define FDT_INTR_MPSAFE	__BIT(0)
54 
55 struct fdtbus_interrupt_controller_func {
56 	void *	(*establish)(device_t, u_int *, int, int,
57 			     int (*)(void *), void *);
58 	void	(*disestablish)(device_t, void *);
59 	bool	(*intrstr)(device_t, u_int *, char *, size_t);
60 };
61 
62 struct fdtbus_i2c_controller_func {
63 	i2c_tag_t (*get_tag)(device_t);
64 };
65 
66 struct fdtbus_gpio_controller;
67 
68 struct fdtbus_gpio_pin {
69 	struct fdtbus_gpio_controller *gp_gc;
70 	void *gp_priv;
71 };
72 
73 struct fdtbus_gpio_controller_func {
74 	void *	(*acquire)(device_t, const void *, size_t, int);
75 	void	(*release)(device_t, void *);
76 	int	(*read)(device_t, void *, bool);
77 	void	(*write)(device_t, void *, int, bool);
78 };
79 
80 struct fdtbus_pinctrl_controller;
81 
82 struct fdtbus_pinctrl_pin {
83 	struct fdtbus_pinctrl_controller *pp_pc;
84 	void *pp_priv;
85 };
86 
87 struct fdtbus_pinctrl_controller_func {
88 	int (*set_config)(device_t, const void *, size_t);
89 };
90 
91 struct fdtbus_regulator_controller;
92 
93 struct fdtbus_regulator {
94 	struct fdtbus_regulator_controller *reg_rc;
95 };
96 
97 struct fdtbus_regulator_controller_func {
98 	int	(*acquire)(device_t);
99 	void	(*release)(device_t);
100 	int	(*enable)(device_t, bool);
101 	int	(*set_voltage)(device_t, u_int, u_int);
102 	int	(*get_voltage)(device_t, u_int *);
103 };
104 
105 struct fdtbus_clock_controller_func {
106 	struct clk *	(*decode)(device_t, const void *, size_t);
107 };
108 
109 struct fdtbus_reset_controller;
110 
111 struct fdtbus_reset {
112 	struct fdtbus_reset_controller *rst_rc;
113 	void *rst_priv;
114 };
115 
116 struct fdtbus_reset_controller_func {
117 	void *	(*acquire)(device_t, const void *, size_t);
118 	void	(*release)(device_t, void *);
119 	int	(*reset_assert)(device_t, void *);
120 	int	(*reset_deassert)(device_t, void *);
121 };
122 
123 struct fdtbus_dma_controller;
124 
125 struct fdtbus_dma {
126 	struct fdtbus_dma_controller *dma_dc;
127 	void *dma_priv;
128 };
129 
130 enum fdtbus_dma_dir {
131 	FDT_DMA_READ,		/* device -> memory */
132 	FDT_DMA_WRITE		/* memory -> device */
133 };
134 
135 struct fdtbus_dma_opt {
136 	int opt_bus_width;		/* Bus width */
137 	int opt_burst_len;		/* Burst length */
138 	int opt_swap;			/* Enable data swapping */
139 	int opt_dblbuf;			/* Enable double buffering */
140 	int opt_wrap_len;		/* Address wrap-around window */
141 };
142 
143 struct fdtbus_dma_req {
144 	bus_dma_segment_t *dreq_segs;	/* Memory */
145 	int dreq_nsegs;
146 
147 	bus_addr_t dreq_dev_phys;	/* Device */
148 	int dreq_sel;			/* Device selector */
149 
150 	enum fdtbus_dma_dir dreq_dir;	/* Transfer direction */
151 
152 	int dreq_block_irq;		/* Enable IRQ at end of block */
153 	int dreq_block_multi;		/* Enable multiple block transfers */
154 	int dreq_flow;			/* Enable flow control */
155 
156 	struct fdtbus_dma_opt dreq_mem_opt;	/* Memory options */
157 	struct fdtbus_dma_opt dreq_dev_opt;	/* Device options */
158 };
159 
160 struct fdtbus_dma_controller_func {
161 	void *	(*acquire)(device_t, const void *, size_t,
162 			   void (*)(void *), void *);
163 	void	(*release)(device_t, void *);
164 	int	(*transfer)(device_t, void *, struct fdtbus_dma_req *);
165 	void	(*halt)(device_t, void *);
166 };
167 
168 struct fdtbus_power_controller;
169 
170 struct fdtbus_power_controller_func {
171 	void 	(*reset)(device_t);
172 	void	(*poweroff)(device_t);
173 };
174 
175 struct fdtbus_phy_controller;
176 
177 struct fdtbus_phy {
178 	struct fdtbus_phy_controller *phy_pc;
179 	void *phy_priv;
180 };
181 
182 struct fdtbus_phy_controller_func {
183 	void *	(*acquire)(device_t, const void *, size_t);
184 	void	(*release)(device_t, void *);
185 	int	(*enable)(device_t, void *, bool);
186 };
187 
188 struct fdtbus_mmc_pwrseq;
189 
190 struct fdtbus_mmc_pwrseq_func {
191 	void	(*pre_power_on)(device_t);
192 	void	(*post_power_on)(device_t);
193 	void	(*power_off)(device_t);
194 	void	(*reset)(device_t);
195 };
196 
197 struct fdt_console {
198 	int	(*match)(int);
199 	void	(*consinit)(struct fdt_attach_args *, u_int);
200 };
201 
202 struct fdt_console_info {
203 	const struct fdt_console *ops;
204 };
205 
206 #define	_FDT_CONSOLE_REGISTER(name)	\
207 	__link_set_add_rodata(fdt_consoles, __CONCAT(name,_consinfo));
208 
209 #define	FDT_CONSOLE(_name, _ops)					\
210 static const struct fdt_console_info __CONCAT(_name,_consinfo) = {	\
211 	.ops = (_ops)							\
212 };									\
213 _FDT_CONSOLE_REGISTER(_name)
214 
215 TAILQ_HEAD(fdt_conslist, fdt_console_info);
216 
217 int		fdtbus_register_interrupt_controller(device_t, int,
218 		    const struct fdtbus_interrupt_controller_func *);
219 int		fdtbus_register_i2c_controller(device_t, int,
220 		    const struct fdtbus_i2c_controller_func *);
221 int		fdtbus_register_gpio_controller(device_t, int,
222 		    const struct fdtbus_gpio_controller_func *);
223 int		fdtbus_register_pinctrl_config(device_t, int,
224 		    const struct fdtbus_pinctrl_controller_func *);
225 int		fdtbus_register_regulator_controller(device_t, int,
226 		    const struct fdtbus_regulator_controller_func *);
227 int		fdtbus_register_clock_controller(device_t, int,
228 		    const struct fdtbus_clock_controller_func *);
229 int		fdtbus_register_reset_controller(device_t, int,
230 		    const struct fdtbus_reset_controller_func *);
231 int		fdtbus_register_dma_controller(device_t, int,
232 		    const struct fdtbus_dma_controller_func *);
233 int		fdtbus_register_power_controller(device_t, int,
234 		    const struct fdtbus_power_controller_func *);
235 int		fdtbus_register_phy_controller(device_t, int,
236 		    const struct fdtbus_phy_controller_func *);
237 int		fdtbus_register_mmc_pwrseq(device_t, int,
238 		    const struct fdtbus_mmc_pwrseq_func *);
239 
240 void		fdtbus_set_decoderegprop(bool);
241 
242 int		fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
243 int		fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
244 		    bus_size_t *);
245 int		fdtbus_get_reg64(int, u_int, uint64_t *, uint64_t *);
246 int		fdtbus_get_phandle(int, const char *);
247 int		fdtbus_get_phandle_from_native(int);
248 i2c_tag_t	fdtbus_get_i2c_tag(int);
249 void *		fdtbus_intr_establish(int, u_int, int, int,
250 		    int (*func)(void *), void *arg);
251 void		fdtbus_intr_disestablish(int, void *);
252 bool		fdtbus_intr_str(int, u_int, char *, size_t);
253 struct fdtbus_gpio_pin *fdtbus_gpio_acquire(int, const char *, int);
254 struct fdtbus_gpio_pin *fdtbus_gpio_acquire_index(int, const char *, int, int);
255 void		fdtbus_gpio_release(struct fdtbus_gpio_pin *);
256 int		fdtbus_gpio_read(struct fdtbus_gpio_pin *);
257 void		fdtbus_gpio_write(struct fdtbus_gpio_pin *, int);
258 int		fdtbus_gpio_read_raw(struct fdtbus_gpio_pin *);
259 void		fdtbus_gpio_write_raw(struct fdtbus_gpio_pin *, int);
260 void		fdtbus_pinctrl_configure(void);
261 int		fdtbus_pinctrl_set_config_index(int, u_int);
262 int		fdtbus_pinctrl_set_config(int, const char *);
263 struct fdtbus_regulator *fdtbus_regulator_acquire(int, const char *);
264 void		fdtbus_regulator_release(struct fdtbus_regulator *);
265 int		fdtbus_regulator_enable(struct fdtbus_regulator *);
266 int		fdtbus_regulator_disable(struct fdtbus_regulator *);
267 int		fdtbus_regulator_set_voltage(struct fdtbus_regulator *,
268 		    u_int, u_int);
269 int		fdtbus_regulator_get_voltage(struct fdtbus_regulator *,
270 		    u_int *);
271 
272 struct fdtbus_dma *fdtbus_dma_get(int, const char *, void (*)(void *), void *);
273 struct fdtbus_dma *fdtbus_dma_get_index(int, u_int, void (*)(void *),
274 		    void *);
275 void		fdtbus_dma_put(struct fdtbus_dma *);
276 int		fdtbus_dma_transfer(struct fdtbus_dma *,
277 		    struct fdtbus_dma_req *);
278 void		fdtbus_dma_halt(struct fdtbus_dma *);
279 
280 struct clk *	fdtbus_clock_get(int, const char *);
281 struct clk *	fdtbus_clock_get_index(int, u_int);
282 
283 struct fdtbus_reset *fdtbus_reset_get(int, const char *);
284 struct fdtbus_reset *fdtbus_reset_get_index(int, u_int);
285 void		fdtbus_reset_put(struct fdtbus_reset *);
286 int		fdtbus_reset_assert(struct fdtbus_reset *);
287 int		fdtbus_reset_deassert(struct fdtbus_reset *);
288 
289 struct fdtbus_phy *fdtbus_phy_get(int, const char *);
290 struct fdtbus_phy *fdtbus_phy_get_index(int, u_int);
291 void		fdtbus_phy_put(struct fdtbus_phy *);
292 int		fdtbus_phy_enable(struct fdtbus_phy *, bool);
293 
294 struct fdtbus_mmc_pwrseq *fdtbus_mmc_pwrseq_get(int);
295 void		fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
296 void		fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
297 void		fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
298 void		fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
299 
300 int		fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
301 
302 void		fdtbus_power_reset(void);
303 void		fdtbus_power_poweroff(void);
304 
305 bool		fdtbus_set_data(const void *);
306 const void *	fdtbus_get_data(void);
307 int		fdtbus_phandle2offset(int);
308 int		fdtbus_offset2phandle(int);
309 bool		fdtbus_get_path(int, char *, size_t);
310 
311 const struct fdt_console *fdtbus_get_console(void);
312 
313 const char *	fdtbus_get_stdout_path(void);
314 int		fdtbus_get_stdout_phandle(void);
315 int		fdtbus_get_stdout_speed(void);
316 tcflag_t	fdtbus_get_stdout_flags(void);
317 
318 bool		fdtbus_status_okay(int);
319 
320 const void *	fdtbus_get_prop(int, const char *, int *);
321 const char *	fdtbus_get_string(int, const char *);
322 const char *	fdtbus_get_string_index(int, const char *, u_int);
323 
324 void		fdt_remove_byhandle(int);
325 void		fdt_remove_bycompat(const char *[]);
326 int		fdtbus_print(void *, const char *);
327 
328 #endif /* _DEV_FDT_FDTVAR_H */
329