]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/arm/at91/at91_pit.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / arm / at91 / at91_pit.c
1 /*-
2  * Copyright (c) 2009 Gallon Sylvestre.  All rights reserved.
3  * Copyright (c) 2010 Greg Ansley.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/resource.h>
35 #include <sys/systm.h>
36 #include <sys/rman.h>
37 #include <sys/time.h>
38 #include <sys/timetc.h>
39 #include <sys/watchdog.h>
40
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/cpufunc.h>
44 #include <machine/frame.h>
45 #include <machine/intr.h>
46 #include <machine/resource.h>
47
48 #include <arm/at91/at91var.h>
49 #include <arm/at91/at91_pitreg.h>
50
51 #ifndef PIT_PRESCALE
52 #define PIT_PRESCALE (16)
53 #endif
54
55 static struct pit_softc {
56         struct resource *mem_res;       /* Memory resource */
57         void            *intrhand;      /* Interrupt handle */
58         device_t        sc_dev;
59 } *sc;
60
61 static uint32_t timecount = 0;
62 static unsigned at91_pit_get_timecount(struct timecounter *tc);
63 static int pit_intr(void *arg);
64
65 static inline uint32_t
66 RD4(struct pit_softc *sc, bus_size_t off)
67 {
68
69         return (bus_read_4(sc->mem_res, off));
70 }
71
72 static inline void
73 WR4(struct pit_softc *sc, bus_size_t off, uint32_t val)
74 {
75
76         bus_write_4(sc->mem_res, off, val);
77 }
78
79 void
80 at91_pit_delay(int us)
81 {
82         int32_t cnt, last, piv;
83         uint64_t pit_freq;
84         const uint64_t mhz  = 1E6;
85
86         last = PIT_PIV(RD4(sc, PIT_PIIR));
87
88         /* Max delay ~= 260s. @ 133Mhz */
89         pit_freq = at91_master_clock / PIT_PRESCALE;
90         cnt  = ((pit_freq * us) + (mhz -1)) / mhz;
91         cnt  = (cnt <= 0) ? 1 : cnt;
92
93         while (cnt > 0) {
94                 piv = PIT_PIV(RD4(sc, PIT_PIIR));
95                         cnt  -= piv - last ;
96                 if (piv < last)
97                         cnt -= PIT_PIV(~0u) - last;
98                 last = piv;
99         }
100 }
101
102 static struct timecounter at91_pit_timecounter = {
103         at91_pit_get_timecount, /* get_timecount */
104         NULL, /* no poll_pps */
105         0xffffffff, /* counter mask */
106         0 / PIT_PRESCALE, /* frequency */
107         "AT91SAM9 timer", /* name */
108         1000 /* quality */
109 };
110
111 static int
112 at91_pit_probe(device_t dev)
113 {
114
115         device_set_desc(dev, "AT91SAM9 PIT");
116         return (0);
117 }
118
119 static int
120 at91_pit_attach(device_t dev)
121 {
122         void *ih;
123         int rid, err = 0;
124         struct at91_softc *at91_sc;
125         struct resource *irq;
126
127         at91_sc = device_get_softc(device_get_parent(dev));
128         sc = device_get_softc(dev);
129         sc->sc_dev = dev;
130
131         rid = 0;
132         sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
133             RF_ACTIVE);
134
135         if (sc->mem_res == NULL)
136                 panic("couldn't allocate register resources");
137
138         rid = 0;
139         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1,
140             RF_ACTIVE | RF_SHAREABLE);
141         if (!irq) {
142                 device_printf(dev, "could not allocate interrupt resources.\n");
143                 err = ENOMEM;
144                 goto out;
145         }
146
147         /* Activate the interrupt. */
148         err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, pit_intr, NULL, NULL,
149             &ih);
150
151         at91_pit_timecounter.tc_frequency =  at91_master_clock / PIT_PRESCALE;
152         tc_init(&at91_pit_timecounter);
153
154         /* Enable the PIT here. */
155         WR4(sc, PIT_MR, PIT_PIV(at91_master_clock / PIT_PRESCALE / hz) |
156             PIT_EN | PIT_IEN);
157 out:
158         return (err);
159 }
160
161 static device_method_t at91_pit_methods[] = {
162         DEVMETHOD(device_probe, at91_pit_probe),
163         DEVMETHOD(device_attach, at91_pit_attach),
164         DEVMETHOD_END
165 };
166
167 static driver_t at91_pit_driver = {
168         "at91_pit",
169         at91_pit_methods,
170         sizeof(struct pit_softc),
171 };
172
173 static devclass_t at91_pit_devclass;
174
175 DRIVER_MODULE(at91_pit, atmelarm, at91_pit_driver, at91_pit_devclass, NULL,
176     NULL);
177
178 static int
179 pit_intr(void *arg)
180 {
181         struct trapframe *fp = arg;
182         uint32_t icnt;
183
184         if (RD4(sc, PIT_SR) & PIT_PITS_DONE) {
185                 icnt = RD4(sc, PIT_PIVR) >> 20;
186
187                 /* Just add in the overflows we just read */
188                 timecount +=  PIT_PIV(RD4(sc, PIT_MR)) * icnt;
189
190                 hardclock(TRAPF_USERMODE(fp), TRAPF_PC(fp));
191                 return (FILTER_HANDLED);
192         }
193         return (FILTER_STRAY);
194 }
195
196 static unsigned
197 at91_pit_get_timecount(struct timecounter *tc)
198 {
199         uint32_t piir, icnt;
200
201         piir = RD4(sc, PIT_PIIR); /* Current  count | over flows */
202         icnt = piir >> 20;      /* Overflows */
203         return (timecount + PIT_PIV(piir) + PIT_PIV(RD4(sc, PIT_MR)) * icnt);
204 }