2010-07-14 00:48:53 +00:00
|
|
|
/*-
|
2010-10-06 22:25:21 +00:00
|
|
|
* Copyright (c) 2009 Gallon Sylvestre. All rights reserved.
|
|
|
|
* Copyright (c) 2010 Greg Ansley. All rights reserved.
|
2010-07-14 00:48:53 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2010-10-06 22:25:21 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
2010-07-14 00:48:53 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
2010-10-06 22:25:21 +00:00
|
|
|
#include <sys/bus.h>
|
2010-07-14 00:48:53 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/module.h>
|
|
|
|
#include <sys/resource.h>
|
2010-10-06 22:25:21 +00:00
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/rman.h>
|
|
|
|
#include <sys/time.h>
|
2010-07-14 00:48:53 +00:00
|
|
|
#include <sys/timetc.h>
|
|
|
|
#include <sys/watchdog.h>
|
|
|
|
|
|
|
|
#include <machine/bus.h>
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#include <machine/cpufunc.h>
|
|
|
|
#include <machine/frame.h>
|
|
|
|
#include <machine/intr.h>
|
2010-10-06 22:25:21 +00:00
|
|
|
#include <machine/resource.h>
|
2010-07-14 00:48:53 +00:00
|
|
|
|
|
|
|
#include <arm/at91/at91var.h>
|
|
|
|
#include <arm/at91/at91_pitreg.h>
|
|
|
|
|
2012-06-15 08:37:50 +00:00
|
|
|
#ifndef PIT_PRESCALE
|
|
|
|
#define PIT_PRESCALE (16)
|
|
|
|
#endif
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
static struct pit_softc {
|
|
|
|
struct resource *mem_res; /* Memory resource */
|
|
|
|
void *intrhand; /* Interrupt handle */
|
|
|
|
device_t sc_dev;
|
|
|
|
} *sc;
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
static uint32_t timecount = 0;
|
2012-07-11 17:11:54 +00:00
|
|
|
static unsigned at91_pit_get_timecount(struct timecounter *tc);
|
2012-06-15 08:37:50 +00:00
|
|
|
static int pit_intr(void *arg);
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
static inline uint32_t
|
|
|
|
RD4(struct pit_softc *sc, bus_size_t off)
|
|
|
|
{
|
2012-04-14 11:29:32 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
return (bus_read_4(sc->mem_res, off));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
WR4(struct pit_softc *sc, bus_size_t off, uint32_t val)
|
|
|
|
{
|
2012-04-14 11:29:32 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
bus_write_4(sc->mem_res, off, val);
|
|
|
|
}
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
void
|
|
|
|
at91_pit_delay(int us)
|
2012-06-15 08:37:50 +00:00
|
|
|
{
|
|
|
|
int32_t cnt, last, piv;
|
|
|
|
uint64_t pit_freq;
|
|
|
|
const uint64_t mhz = 1E6;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2012-06-15 08:37:50 +00:00
|
|
|
last = PIT_PIV(RD4(sc, PIT_PIIR));
|
|
|
|
|
|
|
|
/* Max delay ~= 260s. @ 133Mhz */
|
|
|
|
pit_freq = at91_master_clock / PIT_PRESCALE;
|
|
|
|
cnt = ((pit_freq * us) + (mhz -1)) / mhz;
|
|
|
|
cnt = (cnt <= 0) ? 1 : cnt;
|
|
|
|
|
|
|
|
while (cnt > 0) {
|
|
|
|
piv = PIT_PIV(RD4(sc, PIT_PIIR));
|
|
|
|
cnt -= piv - last ;
|
|
|
|
if (piv < last)
|
|
|
|
cnt -= PIT_PIV(~0u) - last;
|
|
|
|
last = piv;
|
|
|
|
}
|
|
|
|
}
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
static struct timecounter at91_pit_timecounter = {
|
|
|
|
at91_pit_get_timecount, /* get_timecount */
|
2010-07-14 00:48:53 +00:00
|
|
|
NULL, /* no poll_pps */
|
2010-10-06 22:25:21 +00:00
|
|
|
0xffffffff, /* counter mask */
|
|
|
|
0 / PIT_PRESCALE, /* frequency */
|
|
|
|
"AT91SAM9 timer", /* name */
|
2010-07-14 00:48:53 +00:00
|
|
|
1000 /* quality */
|
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
2012-07-11 17:11:54 +00:00
|
|
|
at91_pit_probe(device_t dev)
|
2010-07-14 00:48:53 +00:00
|
|
|
{
|
|
|
|
|
2012-06-15 08:37:50 +00:00
|
|
|
device_set_desc(dev, "AT91SAM9 PIT");
|
|
|
|
return (0);
|
2010-07-14 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-07-11 17:11:54 +00:00
|
|
|
at91_pit_attach(device_t dev)
|
2010-07-14 00:48:53 +00:00
|
|
|
{
|
|
|
|
void *ih;
|
2010-10-06 22:25:21 +00:00
|
|
|
int rid, err = 0;
|
|
|
|
struct at91_softc *at91_sc;
|
|
|
|
struct resource *irq;
|
|
|
|
|
|
|
|
at91_sc = device_get_softc(device_get_parent(dev));
|
|
|
|
sc = device_get_softc(dev);
|
|
|
|
sc->sc_dev = dev;
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
rid = 0;
|
|
|
|
sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
|
|
|
|
RF_ACTIVE);
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
if (sc->mem_res == NULL)
|
2012-04-14 11:29:32 +00:00
|
|
|
panic("couldn't allocate register resources");
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
rid = 0;
|
2010-07-14 00:48:53 +00:00
|
|
|
irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1,
|
2012-04-14 11:29:32 +00:00
|
|
|
RF_ACTIVE | RF_SHAREABLE);
|
2010-10-06 22:25:21 +00:00
|
|
|
if (!irq) {
|
|
|
|
device_printf(dev, "could not allocate interrupt resources.\n");
|
|
|
|
err = ENOMEM;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Activate the interrupt. */
|
2012-04-14 11:29:32 +00:00
|
|
|
err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, pit_intr, NULL, NULL,
|
|
|
|
&ih);
|
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
at91_pit_timecounter.tc_frequency = at91_master_clock / PIT_PRESCALE;
|
|
|
|
tc_init(&at91_pit_timecounter);
|
2010-10-06 22:25:21 +00:00
|
|
|
|
2012-04-14 11:29:32 +00:00
|
|
|
/* Enable the PIT here. */
|
|
|
|
WR4(sc, PIT_MR, PIT_PIV(at91_master_clock / PIT_PRESCALE / hz) |
|
|
|
|
PIT_EN | PIT_IEN);
|
2010-10-06 22:25:21 +00:00
|
|
|
out:
|
|
|
|
return (err);
|
2010-07-14 00:48:53 +00:00
|
|
|
}
|
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
static device_method_t at91_pit_methods[] = {
|
|
|
|
DEVMETHOD(device_probe, at91_pit_probe),
|
|
|
|
DEVMETHOD(device_attach, at91_pit_attach),
|
2012-04-14 11:29:32 +00:00
|
|
|
DEVMETHOD_END
|
2010-07-14 00:48:53 +00:00
|
|
|
};
|
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
static driver_t at91_pit_driver = {
|
2010-07-14 00:48:53 +00:00
|
|
|
"at91_pit",
|
2012-07-11 17:11:54 +00:00
|
|
|
at91_pit_methods,
|
2010-10-06 22:25:21 +00:00
|
|
|
sizeof(struct pit_softc),
|
2010-07-14 00:48:53 +00:00
|
|
|
};
|
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
static devclass_t at91_pit_devclass;
|
2010-07-14 00:48:53 +00:00
|
|
|
|
2012-07-11 17:11:54 +00:00
|
|
|
DRIVER_MODULE(at91_pit, atmelarm, at91_pit_driver, at91_pit_devclass, NULL,
|
2012-04-14 11:29:32 +00:00
|
|
|
NULL);
|
2010-07-14 00:48:53 +00:00
|
|
|
|
|
|
|
static int
|
2010-10-06 22:25:21 +00:00
|
|
|
pit_intr(void *arg)
|
2010-07-14 00:48:53 +00:00
|
|
|
{
|
|
|
|
struct trapframe *fp = arg;
|
2010-10-06 22:25:21 +00:00
|
|
|
uint32_t icnt;
|
|
|
|
|
|
|
|
if (RD4(sc, PIT_SR) & PIT_PITS_DONE) {
|
|
|
|
icnt = RD4(sc, PIT_PIVR) >> 20;
|
|
|
|
|
|
|
|
/* Just add in the overflows we just read */
|
|
|
|
timecount += PIT_PIV(RD4(sc, PIT_MR)) * icnt;
|
2010-07-14 00:48:53 +00:00
|
|
|
|
|
|
|
hardclock(TRAPF_USERMODE(fp), TRAPF_PC(fp));
|
|
|
|
return (FILTER_HANDLED);
|
|
|
|
}
|
|
|
|
return (FILTER_STRAY);
|
|
|
|
}
|
|
|
|
|
2010-10-06 22:25:21 +00:00
|
|
|
static unsigned
|
2012-07-11 17:11:54 +00:00
|
|
|
at91_pit_get_timecount(struct timecounter *tc)
|
2010-07-14 00:48:53 +00:00
|
|
|
{
|
2012-04-14 11:29:32 +00:00
|
|
|
uint32_t piir, icnt;
|
2010-10-06 22:25:21 +00:00
|
|
|
|
|
|
|
piir = RD4(sc, PIT_PIIR); /* Current count | over flows */
|
|
|
|
icnt = piir >> 20; /* Overflows */
|
|
|
|
return (timecount + PIT_PIV(piir) + PIT_PIV(RD4(sc, PIT_MR)) * icnt);
|
2010-07-14 00:48:53 +00:00
|
|
|
}
|