Home | History | Annotate | Line # | Download | only in ti
ti_omaptimer.c revision 1.6
      1 /*	$NetBSD: ti_omaptimer.c,v 1.6 2021/01/18 02:35:49 thorpej Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 __KERNEL_RCSID(0, "$NetBSD: ti_omaptimer.c,v 1.6 2021/01/18 02:35:49 thorpej 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 device_compatible_entry compat_data[] = {
     65 	{ .compat = "ti,am335x-timer-1ms",	.value = DM_TIMER_AM335X },
     66 	{ .compat = "ti,am335x-timer",		.value = DM_TIMER_AM335X },
     67 	{ .compat = "ti,omap3430-timer",	.value = DM_TIMER_OMAP3430 },
     68 
     69 	{ 0 }
     70 };
     71 
     72 struct omaptimer_softc {
     73 	device_t sc_dev;
     74 	bus_space_tag_t sc_bst;
     75 	bus_space_handle_t sc_bsh;
     76 	int sc_phandle;
     77 	enum omaptimer_type sc_type;
     78 	struct timecounter sc_tc;
     79 };
     80 
     81 #define	RD4(sc, reg)			\
     82 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, omaptimer_regmap[(sc)->sc_type][(reg)])
     83 #define	WR4(sc, reg, val)		\
     84 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, omaptimer_regmap[(sc)->sc_type][(reg)], val)
     85 
     86 static struct omaptimer_softc *timer_softc;
     87 
     88 static int
     89 omaptimer_intr(void *arg)
     90 {
     91 	struct omaptimer_softc * const sc = timer_softc;
     92 	struct clockframe * const frame = arg;
     93 
     94 	WR4(sc, TIMER_TISR, OVF_IT_FLAG);
     95 	hardclock(frame);
     96 
     97 	return 1;
     98 }
     99 
    100 static void
    101 omaptimer_cpu_initclocks(void)
    102 {
    103 	struct omaptimer_softc * const sc = timer_softc;
    104 	char intrstr[128];
    105 	void *ih;
    106 
    107 	KASSERT(sc != NULL);
    108 	if (!fdtbus_intr_str(sc->sc_phandle, 0, intrstr, sizeof(intrstr)))
    109 		panic("%s: failed to decode interrupt", __func__);
    110 	ih = fdtbus_intr_establish_xname(sc->sc_phandle, 0, IPL_CLOCK,
    111 	    FDT_INTR_MPSAFE, omaptimer_intr, NULL, device_xname(sc->sc_dev));
    112 	if (ih == NULL)
    113 		panic("%s: failed to establish timer interrupt", __func__);
    114 
    115 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
    116 
    117 	/* Enable interrupts */
    118 	WR4(sc, TIMER_TIER, OVF_EN_FLAG);
    119 }
    120 
    121 static u_int
    122 omaptimer_get_timecount(struct timecounter *tc)
    123 {
    124 	struct omaptimer_softc * const sc = tc->tc_priv;
    125 
    126 	return RD4(sc, TIMER_TCRR);
    127 }
    128 
    129 static void
    130 omaptimer_enable(struct omaptimer_softc *sc, uint32_t value)
    131 {
    132 	/* Configure the timer */
    133 	WR4(sc, TIMER_TLDR, value);
    134 	WR4(sc, TIMER_TCRR, value);
    135 	WR4(sc, TIMER_TIER, 0);
    136 	WR4(sc, TIMER_TCLR, TCLR_ST | TCLR_AR);
    137 }
    138 
    139 static int
    140 omaptimer_match(device_t parent, cfdata_t match, void *aux)
    141 {
    142 	struct fdt_attach_args * const faa = aux;
    143 
    144 	return of_match_compat_data(faa->faa_phandle, compat_data);
    145 }
    146 
    147 static void
    148 omaptimer_attach(device_t parent, device_t self, void *aux)
    149 {
    150 	struct omaptimer_softc * const sc = device_private(self);
    151 	struct fdt_attach_args * const faa = aux;
    152 	const int phandle = faa->faa_phandle;
    153 	struct timecounter *tc = &sc->sc_tc;
    154 	const char *modname;
    155 	struct clk *hwmod;
    156 	bus_addr_t addr;
    157 	bus_size_t size;
    158 	u_int rate;
    159 
    160 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    161 		aprint_error(": couldn't get registers\n");
    162 		return;
    163 	}
    164 
    165 	sc->sc_dev = self;
    166 	sc->sc_phandle = phandle;
    167 	sc->sc_bst = faa->faa_bst;
    168 	sc->sc_type = of_search_compatible(phandle, compat_data)->value;
    169 
    170 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    171 		device_printf(self, "unable to map bus space");
    172 		return;
    173 	}
    174 
    175 	hwmod = ti_prcm_get_hwmod(phandle, 0);
    176 	if (hwmod == NULL || clk_enable(hwmod) != 0) {
    177 		aprint_error(": couldn't enable module\n");
    178 		return;
    179 	}
    180 
    181 	modname = fdtbus_get_string(phandle, "ti,hwmods");
    182 	if (modname == NULL)
    183 		modname = fdtbus_get_string(OF_parent(phandle), "ti,hwmods");
    184 
    185 	aprint_naive("\n");
    186 	aprint_normal(": Timer (%s)\n", modname);
    187 
    188 	rate = clk_get_rate(hwmod);
    189 
    190 	if (strcmp(modname, "timer2") == 0) {
    191 		omaptimer_enable(sc, 0);
    192 
    193 		/* Install timecounter */
    194 		tc->tc_get_timecount = omaptimer_get_timecount;
    195 		tc->tc_counter_mask = ~0u;
    196 		tc->tc_frequency = rate;
    197 		tc->tc_name = modname;
    198 		tc->tc_quality = 200;
    199 		tc->tc_priv = sc;
    200 		tc_init(tc);
    201 
    202 	} else if (strcmp(modname, "timer3") == 0) {
    203 		const uint32_t value = (0xffffffff - ((rate / hz) - 1));
    204 		omaptimer_enable(sc, value);
    205 
    206 		/* Use this as the OS timer in UP configurations */
    207 		if (!arm_has_mpext_p) {
    208 			timer_softc = sc;
    209 			arm_fdt_timer_register(omaptimer_cpu_initclocks);
    210 		}
    211 	}
    212 }
    213 
    214 CFATTACH_DECL_NEW(omaptimer, sizeof(struct omaptimer_softc),
    215     omaptimer_match, omaptimer_attach, NULL, NULL);
    216 
    217