at91_pit.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2009 Gallon Sylvestre.  All rights reserved.
5 * Copyright (c) 2010 Greg Ansley.  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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, 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#include "opt_platform.h"
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: stable/11/sys/arm/at91/at91_pit.c 330897 2018-03-14 03:19:51Z eadler $");
33
34#include <sys/param.h>
35#include <sys/bus.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/resource.h>
39#include <sys/systm.h>
40#include <sys/rman.h>
41#include <sys/time.h>
42#include <sys/timetc.h>
43#include <sys/watchdog.h>
44
45#include <machine/bus.h>
46#include <machine/cpu.h>
47#include <machine/frame.h>
48#include <machine/intr.h>
49#include <machine/resource.h>
50
51#include <arm/at91/at91var.h>
52#include <arm/at91/at91_pitreg.h>
53
54#ifdef FDT
55#include <dev/fdt/fdt_common.h>
56#include <dev/ofw/ofw_bus.h>
57#include <dev/ofw/ofw_bus_subr.h>
58#endif
59
60#ifndef PIT_PRESCALE
61#define PIT_PRESCALE (16)
62#endif
63
64static struct pit_softc {
65	struct resource	*mem_res;	/* Memory resource */
66	void		*intrhand;	/* Interrupt handle */
67	device_t	sc_dev;
68} *sc;
69
70static uint32_t timecount = 0;
71static unsigned at91_pit_get_timecount(struct timecounter *tc);
72static int pit_intr(void *arg);
73
74static inline uint32_t
75RD4(struct pit_softc *sc, bus_size_t off)
76{
77
78	return (bus_read_4(sc->mem_res, off));
79}
80
81static inline void
82WR4(struct pit_softc *sc, bus_size_t off, uint32_t val)
83{
84
85	bus_write_4(sc->mem_res, off, val);
86}
87
88void
89at91_pit_delay(int us)
90{
91	int32_t cnt, last, piv;
92	uint64_t pit_freq;
93	const uint64_t mhz  = 1E6;
94
95	if (sc == NULL)
96		return;
97
98	last = PIT_PIV(RD4(sc, PIT_PIIR));
99
100	/* Max delay ~= 260s. @ 133Mhz */
101	pit_freq = at91_master_clock / PIT_PRESCALE;
102	cnt  = howmany(pit_freq * us, mhz);
103	cnt  = (cnt <= 0) ? 1 : cnt;
104
105	while (cnt > 0) {
106		piv = PIT_PIV(RD4(sc, PIT_PIIR));
107			cnt  -= piv - last ;
108		if (piv < last)
109			cnt -= PIT_PIV(~0u) - last;
110		last = piv;
111	}
112}
113
114static struct timecounter at91_pit_timecounter = {
115	at91_pit_get_timecount, /* get_timecount */
116	NULL, /* no poll_pps */
117	0xffffffff, /* counter mask */
118	0 / PIT_PRESCALE, /* frequency */
119	"AT91SAM9 timer", /* name */
120	1000 /* quality */
121};
122
123static int
124at91_pit_probe(device_t dev)
125{
126#ifdef FDT
127	if (!ofw_bus_is_compatible(dev, "atmel,at91sam9260-pit"))
128		return (ENXIO);
129#endif
130	device_set_desc(dev, "AT91SAM9 PIT");
131        return (0);
132}
133
134static int
135at91_pit_attach(device_t dev)
136{
137	void *ih;
138	int rid, err = 0;
139	struct resource *irq;
140
141	sc = device_get_softc(dev);
142	sc->sc_dev = dev;
143
144	rid = 0;
145	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
146	    RF_ACTIVE);
147
148	if (sc->mem_res == NULL)
149		panic("couldn't allocate register resources");
150
151	rid = 0;
152	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1,
153	    RF_ACTIVE | RF_SHAREABLE);
154	if (!irq) {
155		device_printf(dev, "could not allocate interrupt resources.\n");
156		err = ENOMEM;
157		goto out;
158	}
159
160	/* Activate the interrupt. */
161	err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, pit_intr, NULL, NULL,
162	    &ih);
163
164	at91_pit_timecounter.tc_frequency =  at91_master_clock / PIT_PRESCALE;
165	tc_init(&at91_pit_timecounter);
166
167	/* Enable the PIT here. */
168	WR4(sc, PIT_MR, PIT_PIV(at91_master_clock / PIT_PRESCALE / hz) |
169	    PIT_EN | PIT_IEN);
170out:
171	return (err);
172}
173
174
175static int
176pit_intr(void *arg)
177{
178	struct trapframe *fp = arg;
179	uint32_t icnt;
180
181	if (RD4(sc, PIT_SR) & PIT_PITS_DONE) {
182		icnt = RD4(sc, PIT_PIVR) >> 20;
183
184		/* Just add in the overflows we just read */
185		timecount +=  PIT_PIV(RD4(sc, PIT_MR)) * icnt;
186
187		hardclock(TRAPF_USERMODE(fp), TRAPF_PC(fp));
188		return (FILTER_HANDLED);
189	}
190	return (FILTER_STRAY);
191}
192
193static unsigned
194at91_pit_get_timecount(struct timecounter *tc)
195{
196	uint32_t piir, icnt;
197
198	piir = RD4(sc, PIT_PIIR); /* Current  count | over flows */
199	icnt = piir >> 20;	/* Overflows */
200	return (timecount + PIT_PIV(piir) + PIT_PIV(RD4(sc, PIT_MR)) * icnt);
201}
202
203static device_method_t at91_pit_methods[] = {
204	DEVMETHOD(device_probe, at91_pit_probe),
205	DEVMETHOD(device_attach, at91_pit_attach),
206	DEVMETHOD_END
207};
208
209static driver_t at91_pit_driver = {
210	"at91_pit",
211	at91_pit_methods,
212	sizeof(struct pit_softc),
213};
214
215static devclass_t at91_pit_devclass;
216
217#ifdef FDT
218EARLY_DRIVER_MODULE(at91_pit, simplebus, at91_pit_driver, at91_pit_devclass,
219    NULL, NULL, BUS_PASS_TIMER);
220#else
221EARLY_DRIVER_MODULE(at91_pit, atmelarm, at91_pit_driver, at91_pit_devclass,
222    NULL, NULL, BUS_PASS_TIMER);
223#endif
224