1*cbab9cadSchs /* $NetBSD: rambo.c,v 1.13 2012/10/27 17:18:03 chs Exp $ */
23f55a7b9Swdk
33f55a7b9Swdk /*
43f55a7b9Swdk * Copyright (c) 2000 The NetBSD Foundation, Inc.
53f55a7b9Swdk * All rights reserved.
63f55a7b9Swdk *
73f55a7b9Swdk * This code is derived from software contributed to The NetBSD Foundation
83f55a7b9Swdk * by Wayne Knowles
93f55a7b9Swdk *
103f55a7b9Swdk * Redistribution and use in source and binary forms, with or without
113f55a7b9Swdk * modification, are permitted provided that the following conditions
123f55a7b9Swdk * are met:
133f55a7b9Swdk * 1. Redistributions of source code must retain the above copyright
143f55a7b9Swdk * notice, this list of conditions and the following disclaimer.
153f55a7b9Swdk * 2. Redistributions in binary form must reproduce the above copyright
163f55a7b9Swdk * notice, this list of conditions and the following disclaimer in the
173f55a7b9Swdk * documentation and/or other materials provided with the distribution.
183f55a7b9Swdk *
193f55a7b9Swdk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203f55a7b9Swdk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213f55a7b9Swdk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223f55a7b9Swdk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233f55a7b9Swdk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243f55a7b9Swdk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253f55a7b9Swdk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263f55a7b9Swdk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273f55a7b9Swdk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283f55a7b9Swdk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293f55a7b9Swdk * POSSIBILITY OF SUCH DAMAGE.
303f55a7b9Swdk */
313f55a7b9Swdk
324b2744bfSlukem #include <sys/cdefs.h>
33*cbab9cadSchs __KERNEL_RCSID(0, "$NetBSD: rambo.c,v 1.13 2012/10/27 17:18:03 chs Exp $");
344b2744bfSlukem
353f55a7b9Swdk #include <sys/param.h>
363f55a7b9Swdk #include <sys/kernel.h>
373f55a7b9Swdk #include <sys/device.h>
383f55a7b9Swdk #include <sys/systm.h>
3912fed6d0Sgdamore #include <sys/timetc.h>
403f55a7b9Swdk
413f55a7b9Swdk #include <machine/cpu.h>
423f55a7b9Swdk #include <machine/mainboard.h>
433f55a7b9Swdk #include <machine/autoconf.h>
443f55a7b9Swdk #include <machine/sysconf.h>
453f55a7b9Swdk #include <machine/bus.h>
463f55a7b9Swdk
473f55a7b9Swdk #include <mipsco/obio/rambo.h>
483f55a7b9Swdk
493f55a7b9Swdk /*
503f55a7b9Swdk * Timer & Interrupt manipulation routines for the Rambo Custom ASIC
513f55a7b9Swdk */
523f55a7b9Swdk
53*cbab9cadSchs static int rambo_match(device_t, cfdata_t, void *);
54*cbab9cadSchs static void rambo_attach(device_t, device_t, void *);
5512fed6d0Sgdamore static unsigned rambo_get_timecount(struct timecounter *);
5602cdf4d2Sdsl void rambo_clkintr(struct clockframe *);
5712fed6d0Sgdamore static void rambo_tc_init(void);
583f55a7b9Swdk
593f55a7b9Swdk struct rambo_softc {
603f55a7b9Swdk struct evcnt sc_intrcnt;
613f55a7b9Swdk bus_space_tag_t sc_bst;
623f55a7b9Swdk bus_space_handle_t sc_bsh;
633f55a7b9Swdk u_int32_t sc_tclast;
643f55a7b9Swdk u_int32_t sc_hzticks;
653f55a7b9Swdk };
663f55a7b9Swdk
673f55a7b9Swdk static struct rambo_softc *rambo;
683f55a7b9Swdk
69*cbab9cadSchs CFATTACH_DECL_NEW(rambo, sizeof(struct rambo_softc),
70c5e91d44Sthorpej rambo_match, rambo_attach, NULL, NULL);
713f55a7b9Swdk
723f55a7b9Swdk static int
rambo_match(device_t parent,cfdata_t cf,void * aux)73*cbab9cadSchs rambo_match(device_t parent, cfdata_t cf, void *aux)
743f55a7b9Swdk {
753f55a7b9Swdk return 1;
763f55a7b9Swdk }
773f55a7b9Swdk
783f55a7b9Swdk static void
rambo_attach(device_t parent,device_t self,void * aux)79*cbab9cadSchs rambo_attach(device_t parent, device_t self, void *aux)
803f55a7b9Swdk {
813f55a7b9Swdk struct confargs *ca = aux;
82*cbab9cadSchs struct rambo_softc *sc = device_private(self);
833f55a7b9Swdk
843f55a7b9Swdk sc->sc_bst = ca->ca_bustag;
853f55a7b9Swdk
863f55a7b9Swdk if (bus_space_map(ca->ca_bustag, ca->ca_addr, 256,
873f55a7b9Swdk BUS_SPACE_MAP_LINEAR,
883f55a7b9Swdk &sc->sc_bsh) != 0) {
893f55a7b9Swdk printf(": cannot map registers\n");
903f55a7b9Swdk return;
913f55a7b9Swdk }
923f55a7b9Swdk evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
933f55a7b9Swdk "timer", "intr");
943f55a7b9Swdk
953f55a7b9Swdk /* Setup RAMBO Timer to generate timer interrupts */
963f55a7b9Swdk sc->sc_hzticks = HZ_TO_TICKS(hz);
973f55a7b9Swdk
98b3809689Swdk sc->sc_tclast = 0;
993f55a7b9Swdk bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TCOUNT, 0);
1003f55a7b9Swdk bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_TBREAK, sc->sc_hzticks);
1013f55a7b9Swdk
1023f55a7b9Swdk bus_space_write_4(sc->sc_bst, sc->sc_bsh, RB_CTLREG,
1033f55a7b9Swdk RB_PARITY_EN | RB_BUZZOFF | RB_CLR_IOERR);
1043f55a7b9Swdk
1053f55a7b9Swdk printf(": parity enabled\n");
1063f55a7b9Swdk rambo = sc;
10712fed6d0Sgdamore platform.clkinit = rambo_tc_init;
1083f55a7b9Swdk }
1093f55a7b9Swdk
1103f55a7b9Swdk void
rambo_clkintr(struct clockframe * cf)111454af1c0Sdsl rambo_clkintr(struct clockframe *cf)
1123f55a7b9Swdk {
113b3809689Swdk register u_int32_t tbreak, tcount;
114b3809689Swdk register int delta;
1153f55a7b9Swdk
1163f55a7b9Swdk rambo->sc_intrcnt.ev_count++;
1173f55a7b9Swdk tbreak = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK);
118b3809689Swdk tcount = bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT);
119b3809689Swdk delta = tcount - tbreak;
120b3809689Swdk
121b3809689Swdk if (delta > (rambo->sc_hzticks>>1)) {
122b3809689Swdk /*
123b3809689Swdk * Either tcount may overtake the updated tbreak value
124b3809689Swdk * or we have missed several interrupt's
125b3809689Swdk */
126b3809689Swdk int cycles = 10 * hz;
127b3809689Swdk while (cycles && tbreak < tcount) {
1283f55a7b9Swdk hardclock(cf);
129b3809689Swdk rambo->sc_tclast = tbreak;
1303f55a7b9Swdk tbreak += rambo->sc_hzticks;
131b3809689Swdk cycles--;
132b3809689Swdk }
133b3809689Swdk if (cycles == 0) { /* catchup failed - assume we are in sync */
134b3809689Swdk tcount = bus_space_read_4(rambo->sc_bst,
135b3809689Swdk rambo->sc_bsh, RB_TCOUNT);
136b3809689Swdk rambo->sc_tclast = tbreak = tcount;
137b3809689Swdk }
138b3809689Swdk } else {
139b3809689Swdk hardclock(cf);
140b3809689Swdk rambo->sc_tclast = tbreak;
1413f55a7b9Swdk }
1423f55a7b9Swdk
143b3809689Swdk tbreak += rambo->sc_hzticks;
144b3809689Swdk
1453f55a7b9Swdk bus_space_write_4(rambo->sc_bst, rambo->sc_bsh, RB_TBREAK, tbreak);
1463f55a7b9Swdk }
1473f55a7b9Swdk
1483f55a7b9Swdk /*
1493f55a7b9Swdk * Calculate the number of microseconds since the last clock tick
1503f55a7b9Swdk */
151b3809689Swdk static unsigned
rambo_get_timecount(struct timecounter * tc)15212fed6d0Sgdamore rambo_get_timecount(struct timecounter *tc)
1533f55a7b9Swdk {
1543f55a7b9Swdk
15512fed6d0Sgdamore return (bus_space_read_4(rambo->sc_bst, rambo->sc_bsh, RB_TCOUNT));
15612fed6d0Sgdamore }
15712fed6d0Sgdamore
15812fed6d0Sgdamore static void
rambo_tc_init(void)15912fed6d0Sgdamore rambo_tc_init(void)
16012fed6d0Sgdamore {
16112fed6d0Sgdamore static struct timecounter tc = {
16212fed6d0Sgdamore .tc_get_timecount = rambo_get_timecount,
16312fed6d0Sgdamore .tc_frequency = RB_FREQUENCY,
16412fed6d0Sgdamore .tc_quality = 100,
16512fed6d0Sgdamore .tc_name = "rambo_tcount",
16612fed6d0Sgdamore .tc_counter_mask = ~0
16712fed6d0Sgdamore };
16812fed6d0Sgdamore
16912fed6d0Sgdamore tc_init(&tc);
1703f55a7b9Swdk }
171