xref: /netbsd-src/sys/arch/arm/amlogic/meson6_timer.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1 /*	$NetBSD: meson6_timer.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2019 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nick Hudson
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: meson6_timer.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 #include <sys/timetc.h>
40 
41 #include <dev/fdt/fdtvar.h>
42 
43 static int	meson6_timer_match(device_t, cfdata_t, void *);
44 static void	meson6_timer_attach(device_t, device_t, void *);
45 
46 struct meson6_timer_softc {
47 	device_t		sc_dev;
48 	bus_space_tag_t		sc_bst;
49 	bus_space_handle_t	sc_bsh;
50 
51 	struct timecounter sc_tc;
52 };
53 
54 CFATTACH_DECL_NEW(meson6_timer, sizeof(struct meson6_timer_softc),
55     meson6_timer_match, meson6_timer_attach, NULL, NULL);
56 
57 #define TIMER_READ(sc, reg)			\
58     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
59 #define TIMER_WRITE(sc, reg, val)		\
60     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
61 
62 #define MESON_TIMER_MUX					0x00
63 #define  MESON_TIMER_MUX_TIMERD_EN			__BIT(19)
64 #define  MESON_TIMER_MUX_TIMERC_EN			__BIT(18)
65 #define  MESON_TIMER_MUX_TIMERB_EN			__BIT(17)
66 #define  MESON_TIMER_MUX_TIMERA_EN			__BIT(16)
67 #define  MESON_TIMER_MUX_TIMERD_MODE			__BIT(15)
68 #define  MESON_TIMER_MUX_TIMERC_MODE			__BIT(14)
69 #define  MESON_TIMER_MUX_TIMERB_MODE			__BIT(13)
70 #define  MESON_TIMER_MUX_TIMERA_MODE			__BIT(12)
71 #define  MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK	__BITS(10, 8)
72 #define   MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_SYSTEM_CLOCK	0x0
73 #define   MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_1US		0x1
74 #define   MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_10US		0x2
75 #define   MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_100US		0x3
76 #define   MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_1MS		0x4
77 #define  MESON_TIMER_MUX_TIMERD_INPUT_CLOCK_MASK	__BITS(7, 6)
78 #define  MESON_TIMER_MUX_TIMERC_INPUT_CLOCK_MASK	__BITS(5, 4)
79 #define  MESON_TIMER_MUX_TIMERB_INPUT_CLOCK_MASK	__BITS(3, 2)
80 #define  MESON_TIMER_MUX_TIMERA_INPUT_CLOCK_MASK	__BITS(1, 0)
81 #define   MESON_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1US		0x0
82 #define   MESON_TIMER_MUX_TIMERABCD_INPUT_CLOCK_10US		0x1
83 #define   MESON_TIMER_MUX_TIMERABCD_INPUT_CLOCK_100US		0x2
84 #define   MESON_TIMER_MUX_TIMERABCD_INPUT_CLOCK_1MS		0x3
85 
86 #define MESON_TIMERA					0x04
87 #define MESON_TIMERB					0x08
88 #define MESON_TIMERC					0x0c
89 #define MESON_TIMERD					0x10
90 #define MESON_TIMERE					0x14
91 
92 static u_int
meson6_timer_get_timecount(struct timecounter * tc)93 meson6_timer_get_timecount(struct timecounter *tc)
94 {
95 	struct meson6_timer_softc * const sc = tc->tc_priv;
96 
97 	return TIMER_READ(sc, MESON_TIMERE);
98 }
99 
100 static const struct device_compatible_entry compat_data[] = {
101 	{ .compat = "amlogic,meson6-timer" },
102 	DEVICE_COMPAT_EOL
103 };
104 
105 static int
meson6_timer_match(device_t parent,cfdata_t cf,void * aux)106 meson6_timer_match(device_t parent, cfdata_t cf, void *aux)
107 {
108 	struct fdt_attach_args * const faa = aux;
109 
110 	return of_compatible_match(faa->faa_phandle, compat_data);
111 }
112 
113 static void
meson6_timer_attach(device_t parent,device_t self,void * aux)114 meson6_timer_attach(device_t parent, device_t self, void *aux)
115 {
116 	struct meson6_timer_softc * const sc = device_private(self);
117 	struct fdt_attach_args * const faa = aux;
118 	const int phandle = faa->faa_phandle;
119 	bus_addr_t addr;
120 	bus_size_t size;
121 
122 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
123 		aprint_error(": couldn't get registers\n");
124 		return;
125 	}
126 
127 	sc->sc_dev = self;
128 	sc->sc_bst = faa->faa_bst;
129 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
130 		aprint_error(": couldn't map registers\n");
131 		return;
132 	}
133 
134 	/* Enable clocks */
135 	struct clk *clk;
136 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
137 		if (clk_enable(clk) != 0) {
138 			aprint_error(": failed to enable clock #%d\n", i);
139 			return;
140 		}
141 	}
142 
143 	aprint_naive("\n");
144 	aprint_normal(": Timers\n");
145 
146 	uint32_t mask = TIMER_READ(sc, MESON_TIMER_MUX);
147 	mask &= ~MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK;
148 	mask |= __SHIFTIN(MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_1US, MESON_TIMER_MUX_TIMERE_INPUT_CLOCK_MASK);
149 
150 	TIMER_WRITE(sc, MESON_TIMER_MUX, mask);
151 
152 	/* Timecounter setup */
153 	struct timecounter *tc = &sc->sc_tc;
154 	tc->tc_get_timecount = meson6_timer_get_timecount;
155 	tc->tc_counter_mask = ~0u;
156 	tc->tc_frequency = 1000000;
157 	tc->tc_name = "Timer E";
158 	tc->tc_quality = 500;
159 	tc->tc_priv = sc;
160 	tc_init(tc);
161 }
162 
163