gt.c revision 1.10 1 /* $NetBSD: gt.c,v 1.10 2004/08/28 12:32:48 tsutsui Exp $ */
2
3 /*
4 * Copyright (c) 2000 Soren S. Jorvang. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: gt.c,v 1.10 2004/08/28 12:32:48 tsutsui Exp $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/ioctl.h>
34 #include <sys/select.h>
35 #include <sys/tty.h>
36 #include <sys/proc.h>
37 #include <sys/user.h>
38 #include <sys/conf.h>
39 #include <sys/file.h>
40 #include <sys/uio.h>
41 #include <sys/kernel.h>
42 #include <sys/syslog.h>
43 #include <sys/types.h>
44 #include <sys/device.h>
45
46 #include <machine/autoconf.h>
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49
50 #include <dev/pci/pcivar.h>
51
52 #include <cobalt/cobalt/clockvar.h>
53 #include <cobalt/dev/gtreg.h>
54
55 #include "pci.h"
56
57 struct gt_softc {
58 struct device sc_dev;
59
60 bus_space_tag_t sc_bst;
61 bus_space_handle_t sc_bsh;
62 };
63
64 static int gt_match(struct device *, struct cfdata *, void *);
65 static void gt_attach(struct device *, struct device *, void *);
66 static int gt_print(void *aux, const char *pnp);
67
68 static void gt_timer_init(struct gt_softc *sc);
69 static void gt_timer0_init(void *);
70 static long gt_timer0_read(void *);
71
72 CFATTACH_DECL(gt, sizeof(struct gt_softc),
73 gt_match, gt_attach, NULL, NULL);
74
75 static int
76 gt_match(parent, match, aux)
77 struct device *parent;
78 struct cfdata *match;
79 void *aux;
80 {
81 return 1;
82 }
83
84 #define GT_REG_REGION 0x1000
85
86 static void
87 gt_attach(parent, self, aux)
88 struct device *parent;
89 struct device *self;
90 void *aux;
91 {
92 struct mainbus_attach_args *ma = aux;
93 struct gt_softc *sc = (void *)self;
94 #if NPCI > 0
95 struct pcibus_attach_args pba;
96 #endif
97
98 sc->sc_bst = ma->ma_iot;
99 if (bus_space_map(sc->sc_bst, ma->ma_addr, GT_REG_REGION,
100 0, &sc->sc_bsh)) {
101 printf(": unable to map GT64111 registers\n");
102 return;
103 }
104
105 printf("\n");
106
107 gt_timer_init(sc);
108
109 bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_PCI_COMMAND,
110 (bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_PCI_COMMAND) &
111 ~PCI_SYNCMODE) | PCI_PCLK_HIGH);
112
113 #if NPCI > 0
114 pba.pba_busname = "pci";
115 pba.pba_dmat = &pci_bus_dma_tag;
116 pba.pba_dmat64 = NULL;
117 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
118 pba.pba_bus = 0;
119 pba.pba_bridgetag = NULL;
120 pba.pba_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED |
121 PCI_FLAGS_MRL_OKAY | /*PCI_FLAGS_MRM_OKAY|*/ PCI_FLAGS_MWI_OKAY;
122 config_found(self, &pba, gt_print);
123 #endif
124 }
125
126 static int
127 gt_print(aux, pnp)
128 void *aux;
129 const char *pnp;
130 {
131 /* XXX */
132 return 0;
133 }
134
135 static void
136 gt_timer_init(struct gt_softc *sc)
137 {
138
139 /* stop timer0 */
140 bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL,
141 bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL) & ~ENTC0);
142
143 timer_start = gt_timer0_init;
144 timer_read = gt_timer0_read;
145 timer_cookie = sc;
146 }
147
148 #define TIMER0_INIT_VALUE 500000
149
150 static void
151 gt_timer0_init(void *cookie)
152 {
153 struct gt_softc *sc = cookie;
154
155 bus_space_write_4(sc->sc_bst, sc->sc_bsh,
156 GT_TIMER_COUNTER0, TIMER0_INIT_VALUE);
157 /* start timer0 */
158 bus_space_write_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL,
159 bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_CTRL) | ENTC0);
160 }
161
162 static long
163 gt_timer0_read(void *cookie)
164 {
165 struct gt_softc *sc = cookie;
166 uint32_t counter0;
167
168 counter0 = bus_space_read_4(sc->sc_bst, sc->sc_bsh, GT_TIMER_COUNTER0);
169 counter0 = TIMER0_INIT_VALUE - counter0;
170 #if 0
171 counter /= 50;
172 #else
173 /*
174 * From pmax/pmax/dec_3min.c:
175 * 1/64 + 1/256 + 1/2048 = 41/2048 = 1/49.9512...
176 */
177 counter0 = (counter0 >> 6) + (counter0 >> 8) + (counter0 >> 11);
178 #endif
179 return counter0;
180 }
181