com_arbus.c revision 1.1
1/* $Id: com_arbus.c,v 1.1 2006/03/21 08:15:19 gdamore 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 * 3. All advertising materials mentioning features or use of this software 59 * must display the following acknowledgement: 60 * This product includes software developed by the NetBSD 61 * Foundation, Inc. and its contributors. 62 * 4. Neither the name of The NetBSD Foundation nor the names of its 63 * contributors may be used to endorse or promote products derived 64 * from this software without specific prior written permission. 65 * 66 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 67 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 68 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 69 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 70 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 71 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 72 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 73 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 74 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 75 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 76 * POSSIBILITY OF SUCH DAMAGE. 77 */ 78 79/*- 80 * Copyright (c) 1991 The Regents of the University of California. 81 * All rights reserved. 82 * 83 * Redistribution and use in source and binary forms, with or without 84 * modification, are permitted provided that the following conditions 85 * are met: 86 * 1. Redistributions of source code must retain the above copyright 87 * notice, this list of conditions and the following disclaimer. 88 * 2. Redistributions in binary form must reproduce the above copyright 89 * notice, this list of conditions and the following disclaimer in the 90 * documentation and/or other materials provided with the distribution. 91 * 3. Neither the name of the University nor the names of its contributors 92 * may be used to endorse or promote products derived from this software 93 * without specific prior written permission. 94 * 95 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 96 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 97 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 98 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 99 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 100 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 101 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 103 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 104 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 105 * SUCH DAMAGE. 106 * 107 * @(#)com.c 7.5 (Berkeley) 5/16/91 108 */ 109 110#include <sys/cdefs.h> 111__KERNEL_RCSID(0, "$NetBSD: com_arbus.c,v 1.1 2006/03/21 08:15:19 gdamore Exp $"); 112 113#include <sys/param.h> 114#include <sys/systm.h> 115#include <sys/device.h> 116#include <sys/termios.h> 117#include <sys/ttydefaults.h> 118#include <sys/types.h> 119 120#include <machine/bus.h> 121 122#include <dev/cons.h> 123#include <dev/ic/comreg.h> 124#include <dev/ic/comvar.h> 125 126#include <mips/cpuregs.h> 127#include <mips/atheros/include/arbusvar.h> 128#include <mips/atheros/include/ar531xreg.h> 129 130struct com_arbus_softc { 131 struct com_softc sc_com; 132}; 133 134static bus_space_tag_t com_arbus_get_bus_space_tag(void); 135static int com_arbus_match(struct device *, struct cfdata *, void *); 136static void com_arbus_attach(struct device *, struct device *, void *); 137 138CFATTACH_DECL(com_arbus, sizeof(struct com_arbus_softc), 139 com_arbus_match, com_arbus_attach, NULL, NULL); 140 141#if 0 142#ifdef TTYDEF_SPEED 143#define COM_ARBUS_BAUD TTYDEF_SPEED 144#else 145#define COM_ARBUS_BAUD 115200 146#endif 147#endif 148#define COM_ARBUS_BAUD 115200 149 150int com_arbus_baud = COM_ARBUS_BAUD; 151 152#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 153 154int 155com_arbus_match(struct device *parent, struct cfdata *cf, void *aux) 156{ 157 struct arbus_attach_args *aa = aux; 158 bus_space_handle_t bsh; 159 int rv; 160 161 if (com_is_console(com_arbus_get_bus_space_tag(), aa->aa_addr, NULL)) { 162 return 1; 163 } 164 165 if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 166 0, &bsh)) 167 return 0; 168 169 rv = comprobe1(aa->aa_bst, bsh); 170 bus_space_unmap(aa->aa_bst, bsh, aa->aa_size); 171 172 return rv; 173} 174 175void 176com_arbus_attach(struct device *parent, struct device *self, void *aux) 177{ 178 struct com_arbus_softc *arsc = (void *)self; 179 struct com_softc *sc = &arsc->sc_com; 180 struct arbus_attach_args *aa = aux; 181 182 sc->sc_iot = com_arbus_get_bus_space_tag(); 183 sc->sc_iobase = aa->aa_addr; 184 sc->sc_frequency = curcpu()->ci_cpu_freq / 4; 185 186 if (!com_is_console(sc->sc_iot, sc->sc_iobase, &sc->sc_ioh) && 187 bus_space_map(sc->sc_iot, sc->sc_iobase, aa->aa_size, 0, 188 &sc->sc_ioh) != 0) { 189 printf(": can't map registers\n"); 190 return; 191 } 192 193 com_attach_subr(sc); 194 195 if (aa->aa_irq >= 0) { 196 arbus_intr_establish(aa->aa_irq, comintr, sc); 197 } 198} 199 200/* 201 * This hideousness is to cope with the fact that the 16550 registers need 202 * a stride. Yech! 203 */ 204static struct mips_bus_space com_arbus_space; 205 206static void 207com_arbus_write_1(void *v, bus_space_handle_t h, bus_size_t off, uint8_t val) 208{ 209 bus_space_write_1((bus_space_tag_t)v, h, (off * 4) + 3, val); 210} 211 212static uint8_t 213com_arbus_read_1(void *v, bus_space_handle_t h, bus_size_t off) 214{ 215 return bus_space_read_1((bus_space_tag_t)v, h, (off * 4) + 3); 216} 217 218static void 219com_arbus_read_multi_1(void *v, bus_space_handle_t h, bus_size_t off, 220 uint8_t *data, bus_size_t cnt) 221{ 222 while (cnt-- > 0) { 223 *data++ = com_arbus_read_1(v, h, off); 224 } 225} 226 227static void 228com_arbus_write_multi_1(void *v, bus_space_handle_t h, bus_size_t off, 229 const uint8_t *data, bus_size_t cnt) 230{ 231 while (cnt-- > 0) { 232 com_arbus_write_1(v, h, off, *data++); 233 } 234} 235 236static void 237com_arbus_barrier(void *v, bus_space_handle_t h, bus_size_t o, bus_size_t l, 238 int f) 239{ 240 bus_space_barrier((bus_space_tag_t)v, h, o, l, f); 241} 242 243static int 244com_arbus_map(void *v, bus_addr_t addr, bus_size_t size, int flag, 245 bus_space_handle_t *bshp, int acct) 246{ 247 return bus_space_map((bus_space_tag_t)v, addr, size, flag, bshp); 248} 249 250static void 251com_arbus_unmap(void *v, bus_space_handle_t bsh, bus_size_t size, int acct) 252{ 253 bus_space_unmap((bus_space_tag_t)v, bsh, size); 254} 255 256bus_space_tag_t 257com_arbus_get_bus_space_tag(void) 258{ 259 static int done = 0; 260 if (!done) { 261 com_arbus_space.bs_cookie = arbus_get_bus_space_tag(); 262 com_arbus_space.bs_w_1 = com_arbus_write_1; 263 com_arbus_space.bs_r_1 = com_arbus_read_1; 264 com_arbus_space.bs_wm_1 = com_arbus_write_multi_1; 265 com_arbus_space.bs_rm_1 = com_arbus_read_multi_1; 266 com_arbus_space.bs_barrier = com_arbus_barrier; 267 com_arbus_space.bs_map = com_arbus_map; 268 com_arbus_space.bs_unmap = com_arbus_unmap; 269 done++; 270 } 271 return &com_arbus_space; 272} 273 274void 275com_arbus_cnattach(bus_addr_t addr) 276{ 277 bus_space_tag_t bst; 278 bus_space_handle_t bsh; 279 uint32_t sysfreq; 280 extern void mdputs(const char *); 281 282 bst = com_arbus_get_bus_space_tag(); 283 if (bus_space_map(bst, addr, 0x1000, 0, &bsh)) 284 return; 285 286 sysfreq = curcpu()->ci_cpu_freq / 4; 287 288 comcnattach(bst, addr, com_arbus_baud, sysfreq, 289 COM_TYPE_NORMAL, CONMODE); 290} 291 292