Home | History | Annotate | Line # | Download | only in ti
ti_omaptimer.c revision 1.5
      1 /*	$NetBSD: ti_omaptimer.c,v 1.5 2021/01/15 23:19:33 jmcneill Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 __KERNEL_RCSID(0, "$NetBSD: ti_omaptimer.c,v 1.5 2021/01/15 23:19:33 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_xname(sc->sc_phandle, 0, IPL_CLOCK,
    110 	    FDT_INTR_MPSAFE, omaptimer_intr, NULL, device_xname(sc->sc_dev));
    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 void
    129 omaptimer_enable(struct omaptimer_softc *sc, uint32_t value)
    130 {
    131 	/* Configure the timer */
    132 	WR4(sc, TIMER_TLDR, value);
    133 	WR4(sc, TIMER_TCRR, value);
    134 	WR4(sc, TIMER_TIER, 0);
    135 	WR4(sc, TIMER_TCLR, TCLR_ST | TCLR_AR);
    136 }
    137 
    138 static int
    139 omaptimer_match(device_t parent, cfdata_t match, void *aux)
    140 {
    141 	struct fdt_attach_args * const faa = aux;
    142 
    143 	return of_match_compat_data(faa->faa_phandle, compat_data);
    144 }
    145 
    146 static void
    147 omaptimer_attach(device_t parent, device_t self, void *aux)
    148 {
    149 	struct omaptimer_softc * const sc = device_private(self);
    150 	struct fdt_attach_args * const faa = aux;
    151 	const int phandle = faa->faa_phandle;
    152 	struct timecounter *tc = &sc->sc_tc;
    153 	const char *modname;
    154 	struct clk *hwmod;
    155 	bus_addr_t addr;
    156 	bus_size_t size;
    157 	u_int rate;
    158 
    159 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    160 		aprint_error(": couldn't get registers\n");
    161 		return;
    162 	}
    163 
    164 	sc->sc_dev = self;
    165 	sc->sc_phandle = phandle;
    166 	sc->sc_bst = faa->faa_bst;
    167 	sc->sc_type = of_search_compatible(phandle, compat_data)->data;
    168 
    169 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    170 		device_printf(self, "unable to map bus space");
    171 		return;
    172 	}
    173 
    174 	hwmod = ti_prcm_get_hwmod(phandle, 0);
    175 	if (hwmod == NULL || clk_enable(hwmod) != 0) {
    176 		aprint_error(": couldn't enable module\n");
    177 		return;
    178 	}
    179 
    180 	modname = fdtbus_get_string(phandle, "ti,hwmods");
    181 	if (modname == NULL)
    182 		modname = fdtbus_get_string(OF_parent(phandle), "ti,hwmods");
    183 
    184 	aprint_naive("\n");
    185 	aprint_normal(": Timer (%s)\n", modname);
    186 
    187 	rate = clk_get_rate(hwmod);
    188 
    189 	if (strcmp(modname, "timer2") == 0) {
    190 		omaptimer_enable(sc, 0);
    191 
    192 		/* Install timecounter */
    193 		tc->tc_get_timecount = omaptimer_get_timecount;
    194 		tc->tc_counter_mask = ~0u;
    195 		tc->tc_frequency = rate;
    196 		tc->tc_name = modname;
    197 		tc->tc_quality = 200;
    198 		tc->tc_priv = sc;
    199 		tc_init(tc);
    200 
    201 	} else if (strcmp(modname, "timer3") == 0) {
    202 		const uint32_t value = (0xffffffff - ((rate / hz) - 1));
    203 		omaptimer_enable(sc, value);
    204 
    205 		/* Use this as the OS timer in UP configurations */
    206 		if (!arm_has_mpext_p) {
    207 			timer_softc = sc;
    208 			arm_fdt_timer_register(omaptimer_cpu_initclocks);
    209 		}
    210 	}
    211 }
    212 
    213 CFATTACH_DECL_NEW(omaptimer, sizeof(struct omaptimer_softc),
    214     omaptimer_match, omaptimer_attach, NULL, NULL);
    215 
    216