ti_omaptimer.c revision 1.3 1 /* $NetBSD: ti_omaptimer.c,v 1.3 2019/10/29 22:19:13 jmcneill Exp $ */
2
3 #include <sys/cdefs.h>
4 __KERNEL_RCSID(0, "$NetBSD: ti_omaptimer.c,v 1.3 2019/10/29 22:19:13 jmcneill Exp $");
5
6 #include <sys/types.h>
7 #include <sys/param.h>
8 #include <sys/bus.h>
9 #include <sys/device.h>
10 #include <sys/timetc.h>
11 #include <sys/kernel.h>
12
13 #include <arm/locore.h>
14 #include <arm/fdt/arm_fdtvar.h>
15
16 #include <dev/fdt/fdtvar.h>
17
18 #include <arm/ti/ti_prcm.h>
19
20 enum omaptimer_type {
21 DM_TIMER_AM335X,
22 DM_TIMER_OMAP3430,
23 _DM_NTIMER
24 };
25
26 enum {
27 TIMER_TISR,
28 TIMER_TIER,
29 TIMER_TCLR,
30 TIMER_TCRR,
31 TIMER_TLDR,
32 _TIMER_NREG
33 };
34
35 /* TISR bits */
36 #define OVF_IT_FLAG __BIT(1)
37
38 /* TIER bits */
39 #define MAT_EN_FLAG __BIT(0)
40 #define OVF_EN_FLAG __BIT(1)
41 #define TCAR_EN_FLAG __BIT(2)
42
43 /* TCLR bits */
44 #define TCLR_ST __BIT(0)
45 #define TCLR_AR __BIT(1)
46
47 static uint8_t omaptimer_regmap[_DM_NTIMER][_TIMER_NREG] = {
48 [DM_TIMER_AM335X] = {
49 [TIMER_TISR] = 0x28,
50 [TIMER_TIER] = 0x2c,
51 [TIMER_TCLR] = 0x38,
52 [TIMER_TCRR] = 0x3c,
53 [TIMER_TLDR] = 0x40,
54 },
55 [DM_TIMER_OMAP3430] = {
56 [TIMER_TISR] = 0x18,
57 [TIMER_TIER] = 0x1c,
58 [TIMER_TCLR] = 0x24,
59 [TIMER_TCRR] = 0x28,
60 [TIMER_TLDR] = 0x2c,
61 },
62 };
63
64 static const struct of_compat_data compat_data[] = {
65 { "ti,am335x-timer-1ms", DM_TIMER_AM335X },
66 { "ti,am335x-timer", DM_TIMER_AM335X },
67 { "ti,omap3430-timer", DM_TIMER_OMAP3430 },
68 { NULL }
69 };
70
71 struct omaptimer_softc {
72 device_t sc_dev;
73 bus_space_tag_t sc_bst;
74 bus_space_handle_t sc_bsh;
75 int sc_phandle;
76 enum omaptimer_type sc_type;
77 struct timecounter sc_tc;
78 };
79
80 #define RD4(sc, reg) \
81 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, omaptimer_regmap[(sc)->sc_type][(reg)])
82 #define WR4(sc, reg, val) \
83 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, omaptimer_regmap[(sc)->sc_type][(reg)], val)
84
85 static struct omaptimer_softc *timer_softc;
86
87 static int
88 omaptimer_intr(void *arg)
89 {
90 struct omaptimer_softc * const sc = timer_softc;
91 struct clockframe * const frame = arg;
92
93 WR4(sc, TIMER_TISR, OVF_IT_FLAG);
94 hardclock(frame);
95
96 return 1;
97 }
98
99 static void
100 omaptimer_cpu_initclocks(void)
101 {
102 struct omaptimer_softc * const sc = timer_softc;
103 char intrstr[128];
104 void *ih;
105
106 KASSERT(sc != NULL);
107 if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr)))
108 panic("%s: failed to decode interrupt", __func__);
109 ih = fdtbus_intr_establish(sc->sc_phandle, 0, IPL_CLOCK,
110 FDT_INTR_MPSAFE, omaptimer_intr, NULL);
111 if (ih == NULL)
112 panic("%s: failed to establish timer interrupt", __func__);
113
114 aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
115
116 /* Enable interrupts */
117 WR4(sc, TIMER_TIER, OVF_EN_FLAG);
118 }
119
120 static u_int
121 omaptimer_get_timecount(struct timecounter *tc)
122 {
123 struct omaptimer_softc * const sc = tc->tc_priv;
124
125 return RD4(sc, TIMER_TCRR);
126 }
127
128 static int
129 omaptimer_match(device_t parent, cfdata_t match, void *aux)
130 {
131 struct fdt_attach_args * const faa = aux;
132
133 return of_match_compat_data(faa->faa_phandle, compat_data);
134 }
135
136 static void
137 omaptimer_attach(device_t parent, device_t self, void *aux)
138 {
139 struct omaptimer_softc * const sc = device_private(self);
140 struct fdt_attach_args * const faa = aux;
141 const int phandle = faa->faa_phandle;
142 struct timecounter *tc = &sc->sc_tc;
143 const char *modname;
144 bus_addr_t addr;
145 bus_size_t size;
146
147 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
148 aprint_error(": couldn't get registers\n");
149 return;
150 }
151
152 sc->sc_dev = self;
153 sc->sc_phandle = phandle;
154 sc->sc_bst = faa->faa_bst;
155 sc->sc_type = of_search_compatible(phandle, compat_data)->data;
156
157 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
158 device_printf(self, "unable to map bus space");
159 return;
160 }
161
162 if (ti_prcm_enable_hwmod(phandle, 0) != 0) {
163 aprint_error(": couldn't enable module\n");
164 return;
165 }
166
167 modname = fdtbus_get_string(phandle, "ti,hwmods");
168 if (modname == NULL)
169 modname = fdtbus_get_string(OF_parent(phandle), "ti,hwmods");
170
171 aprint_naive("\n");
172 aprint_normal(": Timer (%s)\n", modname);
173
174 if (strcmp(modname, "timer2") == 0) {
175 /* Install timecounter */
176 tc->tc_get_timecount = omaptimer_get_timecount;
177 tc->tc_counter_mask = ~0u;
178 tc->tc_frequency = 24000000;
179 tc->tc_name = modname;
180 tc->tc_quality = 200;
181 tc->tc_priv = sc;
182 tc_init(tc);
183 } else if (strcmp(modname, "timer3") == 0) {
184 /* Configure the timer */
185 const uint32_t value = (0xffffffff - ((24000000UL / hz) - 1));
186 WR4(sc, TIMER_TLDR, value);
187 WR4(sc, TIMER_TCRR, value);
188 WR4(sc, TIMER_TIER, 0);
189 WR4(sc, TIMER_TCLR, TCLR_ST | TCLR_AR);
190
191 /* Use this as the OS timer in UP configurations */
192 if (!arm_has_mpext_p) {
193 timer_softc = sc;
194 arm_fdt_timer_register(omaptimer_cpu_initclocks);
195 }
196 }
197 }
198
199 CFATTACH_DECL_NEW(omaptimer, sizeof(struct omaptimer_softc),
200 omaptimer_match, omaptimer_attach, NULL, NULL);
201
202