at91_pit.c (213496) | at91_pit.c (234281) |
---|---|
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 --- 11 unchanged lines hidden (view full) --- 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> | 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 --- 11 unchanged lines hidden (view full) --- 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: head/sys/arm/at91/at91_pit.c 213496 2010-10-06 22:25:21Z cognet $"); | 28__FBSDID("$FreeBSD: head/sys/arm/at91/at91_pit.c 234281 2012-04-14 11:29:32Z marius $"); |
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> --- 17 unchanged lines hidden (view full) --- 54 device_t sc_dev; 55} *sc; 56 57static uint32_t timecount = 0; 58 59static inline uint32_t 60RD4(struct pit_softc *sc, bus_size_t off) 61{ | 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> --- 17 unchanged lines hidden (view full) --- 54 device_t sc_dev; 55} *sc; 56 57static uint32_t timecount = 0; 58 59static inline uint32_t 60RD4(struct pit_softc *sc, bus_size_t off) 61{ |
62 |
|
62 return (bus_read_4(sc->mem_res, off)); 63} 64 65static inline void 66WR4(struct pit_softc *sc, bus_size_t off, uint32_t val) 67{ | 63 return (bus_read_4(sc->mem_res, off)); 64} 65 66static inline void 67WR4(struct pit_softc *sc, bus_size_t off, uint32_t val) 68{ |
69 |
|
68 bus_write_4(sc->mem_res, off, val); 69} 70 71static unsigned at91pit_get_timecount(struct timecounter *tc); 72static int pit_intr(void *arg); 73 74#ifndef PIT_PRESCALE 75#define PIT_PRESCALE (16) --- 31 unchanged lines hidden (view full) --- 107 sc = device_get_softc(dev); 108 sc->sc_dev = dev; 109 110 rid = 0; 111 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 112 RF_ACTIVE); 113 114 if (sc->mem_res == NULL) | 70 bus_write_4(sc->mem_res, off, val); 71} 72 73static unsigned at91pit_get_timecount(struct timecounter *tc); 74static int pit_intr(void *arg); 75 76#ifndef PIT_PRESCALE 77#define PIT_PRESCALE (16) --- 31 unchanged lines hidden (view full) --- 109 sc = device_get_softc(dev); 110 sc->sc_dev = dev; 111 112 rid = 0; 113 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 114 RF_ACTIVE); 115 116 if (sc->mem_res == NULL) |
115 panic("couldn't allocate register resources"); | 117 panic("couldn't allocate register resources"); |
116 117 rid = 0; 118 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1, | 118 119 rid = 0; 120 irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 1, 1, 1, |
119 RF_ACTIVE | RF_SHAREABLE); | 121 RF_ACTIVE | RF_SHAREABLE); |
120 if (!irq) { 121 device_printf(dev, "could not allocate interrupt resources.\n"); 122 err = ENOMEM; 123 goto out; 124 } 125 126 /* Activate the interrupt. */ | 122 if (!irq) { 123 device_printf(dev, "could not allocate interrupt resources.\n"); 124 err = ENOMEM; 125 goto out; 126 } 127 128 /* Activate the interrupt. */ |
127 err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, pit_intr, 128 NULL, NULL, &ih); 129 | 129 err = bus_setup_intr(dev, irq, INTR_TYPE_CLK, pit_intr, NULL, NULL, 130 &ih); 131 |
130 at91pit_timecounter.tc_frequency = at91_master_clock / PIT_PRESCALE; 131 tc_init(&at91pit_timecounter); 132 | 132 at91pit_timecounter.tc_frequency = at91_master_clock / PIT_PRESCALE; 133 tc_init(&at91pit_timecounter); 134 |
133 //Enable the PIT here. 134 WR4(sc, PIT_MR, 135 PIT_PIV(at91_master_clock / PIT_PRESCALE / hz) | 136 PIT_EN | PIT_IEN); | 135 /* Enable the PIT here. */ 136 WR4(sc, PIT_MR, PIT_PIV(at91_master_clock / PIT_PRESCALE / hz) | 137 PIT_EN | PIT_IEN); |
137out: 138 return (err); 139} 140 141static device_method_t at91pit_methods[] = { 142 DEVMETHOD(device_probe, at91pit_probe), 143 DEVMETHOD(device_attach, at91pit_attach), | 138out: 139 return (err); 140} 141 142static device_method_t at91pit_methods[] = { 143 DEVMETHOD(device_probe, at91pit_probe), 144 DEVMETHOD(device_attach, at91pit_attach), |
144 {0,0}, | 145 DEVMETHOD_END |
145}; 146 147static driver_t at91pit_driver = { 148 "at91_pit", 149 at91pit_methods, 150 sizeof(struct pit_softc), 151}; 152 153static devclass_t at91pit_devclass; 154 | 146}; 147 148static driver_t at91pit_driver = { 149 "at91_pit", 150 at91pit_methods, 151 sizeof(struct pit_softc), 152}; 153 154static devclass_t at91pit_devclass; 155 |
155DRIVER_MODULE(at91_pit, atmelarm, at91pit_driver, at91pit_devclass, 0, 0); | 156DRIVER_MODULE(at91_pit, atmelarm, at91pit_driver, at91pit_devclass, NULL, 157 NULL); |
156 157static int 158pit_intr(void *arg) 159{ 160 struct trapframe *fp = arg; 161 uint32_t icnt; 162 163 if (RD4(sc, PIT_SR) & PIT_PITS_DONE) { --- 6 unchanged lines hidden (view full) --- 170 return (FILTER_HANDLED); 171 } 172 return (FILTER_STRAY); 173} 174 175static unsigned 176at91pit_get_timecount(struct timecounter *tc) 177{ | 158 159static int 160pit_intr(void *arg) 161{ 162 struct trapframe *fp = arg; 163 uint32_t icnt; 164 165 if (RD4(sc, PIT_SR) & PIT_PITS_DONE) { --- 6 unchanged lines hidden (view full) --- 172 return (FILTER_HANDLED); 173 } 174 return (FILTER_STRAY); 175} 176 177static unsigned 178at91pit_get_timecount(struct timecounter *tc) 179{ |
178 uint32_t piir, icnt; | 180 uint32_t piir, icnt; |
179 180 piir = RD4(sc, PIT_PIIR); /* Current count | over flows */ 181 icnt = piir >> 20; /* Overflows */ 182 return (timecount + PIT_PIV(piir) + PIT_PIV(RD4(sc, PIT_MR)) * icnt); 183} 184 185void 186DELAY(int us) 187{ 188 int32_t cnt, last, piv; 189 uint64_t pit_freq; 190 const uint64_t mhz = 1E6; 191 192 last = PIT_PIV(RD4(sc, PIT_PIIR)); 193 194 /* Max delay ~= 260s. @ 133Mhz */ | 181 182 piir = RD4(sc, PIT_PIIR); /* Current count | over flows */ 183 icnt = piir >> 20; /* Overflows */ 184 return (timecount + PIT_PIV(piir) + PIT_PIV(RD4(sc, PIT_MR)) * icnt); 185} 186 187void 188DELAY(int us) 189{ 190 int32_t cnt, last, piv; 191 uint64_t pit_freq; 192 const uint64_t mhz = 1E6; 193 194 last = PIT_PIV(RD4(sc, PIT_PIIR)); 195 196 /* Max delay ~= 260s. @ 133Mhz */ |
195 pit_freq = at91_master_clock / PIT_PRESCALE; | 197 pit_freq = at91_master_clock / PIT_PRESCALE; |
196 cnt = ((pit_freq * us) + (mhz -1)) / mhz; 197 cnt = (cnt <= 0) ? 1 : cnt; 198 199 while (cnt > 0) { 200 piv = PIT_PIV(RD4(sc, PIT_PIIR)); 201 cnt -= piv - last ; 202 if (piv < last) 203 cnt -= PIT_PIV(~0u) - last; 204 last = piv; 205 } 206} 207 208/* 209 * The 3 next functions must be implement with the future PLL code. 210 */ 211void 212cpu_startprofclock(void) 213{ | 198 cnt = ((pit_freq * us) + (mhz -1)) / mhz; 199 cnt = (cnt <= 0) ? 1 : cnt; 200 201 while (cnt > 0) { 202 piv = PIT_PIV(RD4(sc, PIT_PIIR)); 203 cnt -= piv - last ; 204 if (piv < last) 205 cnt -= PIT_PIV(~0u) - last; 206 last = piv; 207 } 208} 209 210/* 211 * The 3 next functions must be implement with the future PLL code. 212 */ 213void 214cpu_startprofclock(void) 215{ |
216 |
|
214} 215 216void 217cpu_stopprofclock(void) 218{ | 217} 218 219void 220cpu_stopprofclock(void) 221{ |
222 |
|
219} 220 221void 222cpu_initclocks(void) 223{ | 223} 224 225void 226cpu_initclocks(void) 227{ |
228 |
|
224} | 229} |