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