rmixl_com.c revision 1.1.2.1 1 1.1.2.1 cliff /* $Id: rmixl_com.c,v 1.1.2.1 2009/09/13 03:27:38 cliff Exp $ */
2 1.1.2.1 cliff /*-
3 1.1.2.1 cliff * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
4 1.1.2.1 cliff * Copyright (c) 2006 Garrett D'Amore.
5 1.1.2.1 cliff * All rights reserved.
6 1.1.2.1 cliff *
7 1.1.2.1 cliff * Portions of this code were written by Garrett D'Amore for the
8 1.1.2.1 cliff * Champaign-Urbana Community Wireless Network Project.
9 1.1.2.1 cliff *
10 1.1.2.1 cliff * Redistribution and use in source and binary forms, with or
11 1.1.2.1 cliff * without modification, are permitted provided that the following
12 1.1.2.1 cliff * conditions are met:
13 1.1.2.1 cliff * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 cliff * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 cliff * 2. Redistributions in binary form must reproduce the above
16 1.1.2.1 cliff * copyright notice, this list of conditions and the following
17 1.1.2.1 cliff * disclaimer in the documentation and/or other materials provided
18 1.1.2.1 cliff * with the distribution.
19 1.1.2.1 cliff * 3. All advertising materials mentioning features or use of this
20 1.1.2.1 cliff * software must display the following acknowledgements:
21 1.1.2.1 cliff * This product includes software developed by the Urbana-Champaign
22 1.1.2.1 cliff * Independent Media Center.
23 1.1.2.1 cliff * This product includes software developed by Garrett D'Amore.
24 1.1.2.1 cliff * 4. Urbana-Champaign Independent Media Center's name and Garrett
25 1.1.2.1 cliff * D'Amore's name may not be used to endorse or promote products
26 1.1.2.1 cliff * derived from this software without specific prior written permission.
27 1.1.2.1 cliff *
28 1.1.2.1 cliff * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
29 1.1.2.1 cliff * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
30 1.1.2.1 cliff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 1.1.2.1 cliff * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 1.1.2.1 cliff * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
33 1.1.2.1 cliff * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
34 1.1.2.1 cliff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 1.1.2.1 cliff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
36 1.1.2.1 cliff * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 1.1.2.1 cliff * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.1.2.1 cliff * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.1.2.1 cliff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
40 1.1.2.1 cliff * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 1.1.2.1 cliff */
42 1.1.2.1 cliff
43 1.1.2.1 cliff /*-
44 1.1.2.1 cliff * Copyright (c) 1998 The NetBSD Foundation, Inc.
45 1.1.2.1 cliff * All rights reserved.
46 1.1.2.1 cliff *
47 1.1.2.1 cliff * This code is derived from software contributed to The NetBSD Foundation
48 1.1.2.1 cliff * by Charles M. Hannum.
49 1.1.2.1 cliff *
50 1.1.2.1 cliff * Redistribution and use in source and binary forms, with or without
51 1.1.2.1 cliff * modification, are permitted provided that the following conditions
52 1.1.2.1 cliff * are met:
53 1.1.2.1 cliff * 1. Redistributions of source code must retain the above copyright
54 1.1.2.1 cliff * notice, this list of conditions and the following disclaimer.
55 1.1.2.1 cliff * 2. Redistributions in binary form must reproduce the above copyright
56 1.1.2.1 cliff * notice, this list of conditions and the following disclaimer in the
57 1.1.2.1 cliff * documentation and/or other materials provided with the distribution.
58 1.1.2.1 cliff *
59 1.1.2.1 cliff * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
60 1.1.2.1 cliff * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
61 1.1.2.1 cliff * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 1.1.2.1 cliff * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
63 1.1.2.1 cliff * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 1.1.2.1 cliff * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 1.1.2.1 cliff * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 1.1.2.1 cliff * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 1.1.2.1 cliff * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 1.1.2.1 cliff * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 1.1.2.1 cliff * POSSIBILITY OF SUCH DAMAGE.
70 1.1.2.1 cliff */
71 1.1.2.1 cliff
72 1.1.2.1 cliff /*-
73 1.1.2.1 cliff * Copyright (c) 1991 The Regents of the University of California.
74 1.1.2.1 cliff * All rights reserved.
75 1.1.2.1 cliff *
76 1.1.2.1 cliff * Redistribution and use in source and binary forms, with or without
77 1.1.2.1 cliff * modification, are permitted provided that the following conditions
78 1.1.2.1 cliff * are met:
79 1.1.2.1 cliff * 1. Redistributions of source code must retain the above copyright
80 1.1.2.1 cliff * notice, this list of conditions and the following disclaimer.
81 1.1.2.1 cliff * 2. Redistributions in binary form must reproduce the above copyright
82 1.1.2.1 cliff * notice, this list of conditions and the following disclaimer in the
83 1.1.2.1 cliff * documentation and/or other materials provided with the distribution.
84 1.1.2.1 cliff * 3. Neither the name of the University nor the names of its contributors
85 1.1.2.1 cliff * may be used to endorse or promote products derived from this software
86 1.1.2.1 cliff * without specific prior written permission.
87 1.1.2.1 cliff *
88 1.1.2.1 cliff * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89 1.1.2.1 cliff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90 1.1.2.1 cliff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91 1.1.2.1 cliff * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92 1.1.2.1 cliff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93 1.1.2.1 cliff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94 1.1.2.1 cliff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95 1.1.2.1 cliff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96 1.1.2.1 cliff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97 1.1.2.1 cliff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98 1.1.2.1 cliff * SUCH DAMAGE.
99 1.1.2.1 cliff *
100 1.1.2.1 cliff * @(#)com.c 7.5 (Berkeley) 5/16/91
101 1.1.2.1 cliff */
102 1.1.2.1 cliff
103 1.1.2.1 cliff #include <sys/cdefs.h>
104 1.1.2.1 cliff __KERNEL_RCSID(0, "$NetBSD: rmixl_com.c,v 1.1.2.1 2009/09/13 03:27:38 cliff Exp $");
105 1.1.2.1 cliff
106 1.1.2.1 cliff #include <sys/param.h>
107 1.1.2.1 cliff #include <sys/systm.h>
108 1.1.2.1 cliff #include <sys/device.h>
109 1.1.2.1 cliff #include <sys/kernel.h>
110 1.1.2.1 cliff #include <sys/termios.h>
111 1.1.2.1 cliff #include <sys/ttydefaults.h>
112 1.1.2.1 cliff #include <sys/types.h>
113 1.1.2.1 cliff
114 1.1.2.1 cliff #include <machine/bus.h>
115 1.1.2.1 cliff
116 1.1.2.1 cliff #include <dev/cons.h>
117 1.1.2.1 cliff #include <dev/ic/comreg.h>
118 1.1.2.1 cliff #include <dev/ic/comvar.h>
119 1.1.2.1 cliff
120 1.1.2.1 cliff #include <mips/cpuregs.h>
121 1.1.2.1 cliff
122 1.1.2.1 cliff #include <mips/rmi/rmixl_obiovar.h>
123 1.1.2.1 cliff #include <mips/rmi/rmixl_comvar.h>
124 1.1.2.1 cliff #include <mips/rmi/rmixlvar.h>
125 1.1.2.1 cliff
126 1.1.2.1 cliff #include "opt_com.h"
127 1.1.2.1 cliff
128 1.1.2.1 cliff struct rmixl_com_softc {
129 1.1.2.1 cliff struct com_softc sc_com;
130 1.1.2.1 cliff };
131 1.1.2.1 cliff
132 1.1.2.1 cliff static void rmixl_com_initmap(struct com_regs *);
133 1.1.2.1 cliff static int rmixl_com_match(device_t, cfdata_t , void *);
134 1.1.2.1 cliff static void rmixl_com_attach(device_t, device_t, void *);
135 1.1.2.1 cliff
136 1.1.2.1 cliff CFATTACH_DECL_NEW(rmixl_com, sizeof(struct rmixl_com_softc),
137 1.1.2.1 cliff rmixl_com_match, rmixl_com_attach, NULL, NULL);
138 1.1.2.1 cliff
139 1.1.2.1 cliff #ifndef COM_REGMAP
140 1.1.2.1 cliff #error COM_REGMAP not defined!
141 1.1.2.1 cliff #endif
142 1.1.2.1 cliff
143 1.1.2.1 cliff volatile uint32_t *com0addr = (uint32_t *)
144 1.1.2.1 cliff MIPS_PHYS_TO_KSEG1(RMIXL_IO_DEV_PBASE + RMIXL_IO_DEV_UART_1);
145 1.1.2.1 cliff
146 1.1.2.1 cliff void
147 1.1.2.1 cliff rmixl_putchar_init(uint64_t io_base)
148 1.1.2.1 cliff {
149 1.1.2.1 cliff com0addr = (uint32_t *)
150 1.1.2.1 cliff MIPS_PHYS_TO_KSEG1(io_base + RMIXL_IO_DEV_UART_1);
151 1.1.2.1 cliff }
152 1.1.2.1 cliff
153 1.1.2.1 cliff void
154 1.1.2.1 cliff rmixl_putchar(char c)
155 1.1.2.1 cliff {
156 1.1.2.1 cliff int timo = 150000;
157 1.1.2.1 cliff
158 1.1.2.1 cliff while ((com0addr[5] & LSR_TXRDY) == 0)
159 1.1.2.1 cliff if (--timo == 0)
160 1.1.2.1 cliff break;
161 1.1.2.1 cliff
162 1.1.2.1 cliff com0addr[0] = c;
163 1.1.2.1 cliff
164 1.1.2.1 cliff while ((com0addr[5] & LSR_TSRE) == 0)
165 1.1.2.1 cliff if (--timo == 0)
166 1.1.2.1 cliff break;
167 1.1.2.1 cliff }
168 1.1.2.1 cliff
169 1.1.2.1 cliff void
170 1.1.2.1 cliff rmixl_puts(const char *restrict s)
171 1.1.2.1 cliff {
172 1.1.2.1 cliff char c;
173 1.1.2.1 cliff
174 1.1.2.1 cliff while ((c = *s++) != 0)
175 1.1.2.1 cliff rmixl_putchar(c);
176 1.1.2.1 cliff }
177 1.1.2.1 cliff
178 1.1.2.1 cliff void
179 1.1.2.1 cliff rmixl_puthex32(uint32_t val)
180 1.1.2.1 cliff {
181 1.1.2.1 cliff char hexc[] = "0123456789abcdef";
182 1.1.2.1 cliff
183 1.1.2.1 cliff rmixl_putchar('0');
184 1.1.2.1 cliff rmixl_putchar('x');
185 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 28) & 0xf]);
186 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 24) & 0xf]);
187 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 20) & 0xf]);
188 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 16) & 0xf]);
189 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 12) & 0xf]);
190 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 8) & 0xf]);
191 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 4) & 0xf]);
192 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 0) & 0xf]);
193 1.1.2.1 cliff }
194 1.1.2.1 cliff
195 1.1.2.1 cliff void
196 1.1.2.1 cliff rmixl_puthex64(uint64_t val)
197 1.1.2.1 cliff {
198 1.1.2.1 cliff char hexc[] = "0123456789abcdef";
199 1.1.2.1 cliff
200 1.1.2.1 cliff rmixl_putchar('0');
201 1.1.2.1 cliff rmixl_putchar('x');
202 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 60) & 0xf]);
203 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 56) & 0xf]);
204 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 52) & 0xf]);
205 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 48) & 0xf]);
206 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 44) & 0xf]);
207 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 40) & 0xf]);
208 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 36) & 0xf]);
209 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 32) & 0xf]);
210 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 28) & 0xf]);
211 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 24) & 0xf]);
212 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 20) & 0xf]);
213 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 16) & 0xf]);
214 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 12) & 0xf]);
215 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 8) & 0xf]);
216 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 4) & 0xf]);
217 1.1.2.1 cliff rmixl_putchar(hexc[(val >> 0) & 0xf]);
218 1.1.2.1 cliff }
219 1.1.2.1 cliff
220 1.1.2.1 cliff int
221 1.1.2.1 cliff rmixl_com_match(device_t parent, cfdata_t cf, void *aux)
222 1.1.2.1 cliff {
223 1.1.2.1 cliff struct obio_attach_args *obio = aux;
224 1.1.2.1 cliff struct com_regs regs;
225 1.1.2.1 cliff int rv;
226 1.1.2.1 cliff
227 1.1.2.1 cliff if (com_is_console(obio->obio_bst, obio->obio_addr, ®s.cr_ioh))
228 1.1.2.1 cliff return 1;
229 1.1.2.1 cliff
230 1.1.2.1 cliff if (bus_space_map(obio->obio_bst, obio->obio_addr, obio->obio_size,
231 1.1.2.1 cliff 0, ®s.cr_ioh))
232 1.1.2.1 cliff return 0;
233 1.1.2.1 cliff
234 1.1.2.1 cliff regs.cr_iot = obio->obio_bst;
235 1.1.2.1 cliff regs.cr_iobase = obio->obio_addr;
236 1.1.2.1 cliff regs.cr_nports = obio->obio_size;
237 1.1.2.1 cliff rmixl_com_initmap(®s);
238 1.1.2.1 cliff
239 1.1.2.1 cliff rv = com_probe_subr(®s);
240 1.1.2.1 cliff
241 1.1.2.1 cliff bus_space_unmap(obio->obio_bst, regs.cr_ioh, obio->obio_size);
242 1.1.2.1 cliff
243 1.1.2.1 cliff return rv;
244 1.1.2.1 cliff }
245 1.1.2.1 cliff
246 1.1.2.1 cliff void
247 1.1.2.1 cliff rmixl_com_attach(device_t parent, device_t self, void *aux)
248 1.1.2.1 cliff {
249 1.1.2.1 cliff struct rmixl_com_softc *rsc = device_private(self);
250 1.1.2.1 cliff struct com_softc *sc = &rsc->sc_com;
251 1.1.2.1 cliff struct obio_attach_args *obio = aux;
252 1.1.2.1 cliff #if 0
253 1.1.2.1 cliff prop_number_t prop;
254 1.1.2.1 cliff #endif
255 1.1.2.1 cliff bus_space_handle_t ioh;
256 1.1.2.1 cliff
257 1.1.2.1 cliff sc->sc_dev = self;
258 1.1.2.1 cliff
259 1.1.2.1 cliff #if 0
260 1.1.2.1 cliff prop = prop_dictionary_get(device_properties(sc->sc_dev),
261 1.1.2.1 cliff "frequency");
262 1.1.2.1 cliff if (prop == NULL) {
263 1.1.2.1 cliff aprint_error(": unable to get frequency property\n");
264 1.1.2.1 cliff return;
265 1.1.2.1 cliff }
266 1.1.2.1 cliff KASSERT(prop_object_type(prop) == PROP_TYPE_NUMBER);
267 1.1.2.1 cliff
268 1.1.2.1 cliff sc->sc_frequency = (int)prop_number_integer_value(prop);
269 1.1.2.1 cliff #else
270 1.1.2.1 cliff sc->sc_frequency = -1; /* XXX */
271 1.1.2.1 cliff #endif
272 1.1.2.1 cliff
273 1.1.2.1 cliff if (!com_is_console(obio->obio_bst, obio->obio_addr, &ioh) &&
274 1.1.2.1 cliff bus_space_map(obio->obio_bst, obio->obio_addr, obio->obio_size, 0,
275 1.1.2.1 cliff &ioh) != 0) {
276 1.1.2.1 cliff aprint_error(": can't map registers\n");
277 1.1.2.1 cliff return;
278 1.1.2.1 cliff }
279 1.1.2.1 cliff
280 1.1.2.1 cliff COM_INIT_REGS(sc->sc_regs, obio->obio_bst, ioh, obio->obio_addr);
281 1.1.2.1 cliff sc->sc_regs.cr_nports = obio->obio_size;
282 1.1.2.1 cliff rmixl_com_initmap(&sc->sc_regs);
283 1.1.2.1 cliff
284 1.1.2.1 cliff com_attach_subr(sc);
285 1.1.2.1 cliff
286 1.1.2.1 cliff #ifdef NOTYET
287 1.1.2.1 cliff rmixl_intr_establish(obio->obio_intr, comintr, sc);
288 1.1.2.1 cliff #endif
289 1.1.2.1 cliff }
290 1.1.2.1 cliff
291 1.1.2.1 cliff void
292 1.1.2.1 cliff rmixl_com_initmap(struct com_regs *regsp)
293 1.1.2.1 cliff {
294 1.1.2.1 cliff int i;
295 1.1.2.1 cliff int sz;
296 1.1.2.1 cliff
297 1.1.2.1 cliff /*
298 1.1.2.1 cliff * map the 4 byte register stride
299 1.1.2.1 cliff */
300 1.1.2.1 cliff sz = sizeof(regsp->cr_map) / sizeof(regsp->cr_map[0]);
301 1.1.2.1 cliff for (i = 0; i < 16; i++)
302 1.1.2.1 cliff regsp->cr_map[i] = com_std_map[i] * 4;
303 1.1.2.1 cliff }
304 1.1.2.1 cliff
305 1.1.2.1 cliff void
306 1.1.2.1 cliff rmixl_com_cnattach(bus_addr_t addr, int speed, int freq, int type, tcflag_t mode)
307 1.1.2.1 cliff {
308 1.1.2.1 cliff bus_space_tag_t bst;
309 1.1.2.1 cliff bus_size_t sz;
310 1.1.2.1 cliff struct com_regs regs;
311 1.1.2.1 cliff
312 1.1.2.1 cliff bst = rmixl_obio_get_bus_space_tag();
313 1.1.2.1 cliff sz = COM_NPORTS * sizeof(uint32_t); /* span of UART regs in bytes */
314 1.1.2.1 cliff
315 1.1.2.1 cliff memset(®s, 0, sizeof(regs));
316 1.1.2.1 cliff rmixl_com_initmap(®s);
317 1.1.2.1 cliff regs.cr_iot = bst;
318 1.1.2.1 cliff regs.cr_iobase = addr;
319 1.1.2.1 cliff regs.cr_nports = sz;
320 1.1.2.1 cliff
321 1.1.2.1 cliff comcnattach1(®s, speed, freq, type, mode);
322 1.1.2.1 cliff }
323