1 /* $NetBSD: com_arbus.c,v 1.13 2018/12/08 17:46:12 thorpej 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: com_arbus.c,v 1.13 2018/12/08 17:46:12 thorpej 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 <sys/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 #include <mips/atheros/include/arbusvar.h> 122 123 #include "opt_com.h" 124 125 struct com_arbus_softc { 126 struct com_softc sc_com; 127 }; 128 129 static void com_arbus_init_regs(struct com_regs *, bus_space_tag_t, 130 bus_space_handle_t, bus_addr_t, bus_size_t); 131 //static bus_space_tag_t com_arbus_get_bus_space_tag(void); 132 static int com_arbus_match(device_t, cfdata_t , void *); 133 static void com_arbus_attach(device_t, device_t, void *); 134 135 CFATTACH_DECL_NEW(com_arbus, sizeof(struct com_arbus_softc), 136 com_arbus_match, com_arbus_attach, NULL, NULL); 137 138 #if 0 139 #ifdef TTYDEF_SPEED 140 #define COM_ARBUS_BAUD TTYDEF_SPEED 141 #else 142 #define COM_ARBUS_BAUD 115200 143 #endif 144 #endif 145 146 #ifndef COM_ARBUS_BAUD 147 #define COM_ARBUS_BAUD 115200 148 #endif 149 150 int com_arbus_baud = COM_ARBUS_BAUD; 151 152 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 153 154 #ifndef COM_REGMAP 155 #error COM_REGMAP not defined! 156 #endif 157 158 int 159 com_arbus_match(device_t parent, cfdata_t cf, void *aux) 160 { 161 struct arbus_attach_args *aa = aux; 162 bus_space_handle_t bsh; 163 struct com_regs regs; 164 int rv; 165 166 if (strcmp(aa->aa_name, cf->cf_name) != 0) 167 return 0; 168 169 if (com_is_console(aa->aa_bst, aa->aa_addr, ®s.cr_ioh)) 170 return 1; 171 172 if (bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 173 0, &bsh)) 174 return 0; 175 176 com_arbus_init_regs(®s, aa->aa_bst, bsh, aa->aa_addr, aa->aa_size); 177 178 rv = com_probe_subr(®s); 179 180 bus_space_unmap(aa->aa_bst, bsh, aa->aa_size); 181 182 return rv; 183 } 184 185 void 186 com_arbus_attach(device_t parent, device_t self, void *aux) 187 { 188 struct com_arbus_softc *arsc = device_private(self); 189 struct com_softc *sc = &arsc->sc_com; 190 struct arbus_attach_args *aa = aux; 191 prop_number_t prop; 192 bus_space_handle_t ioh; 193 194 sc->sc_dev = self; 195 196 prop = prop_dictionary_get(device_properties(sc->sc_dev), 197 "frequency"); 198 if (prop == NULL) { 199 aprint_error(": unable to get frequency property\n"); 200 return; 201 } 202 KASSERT(prop_object_type(prop) == PROP_TYPE_NUMBER); 203 204 sc->sc_frequency = (int)prop_number_integer_value(prop); 205 206 if (!com_is_console(aa->aa_bst, aa->aa_addr, &ioh) 207 && bus_space_map(aa->aa_bst, aa->aa_addr, aa->aa_size, 0, 208 &ioh) != 0) { 209 aprint_error(": can't map registers\n"); 210 return; 211 } 212 213 com_arbus_init_regs(&sc->sc_regs, aa->aa_bst, ioh, aa->aa_addr, 214 aa->aa_size); 215 216 com_attach_subr(sc); 217 218 arbus_intr_establish(aa->aa_cirq, aa->aa_mirq, comintr, sc); 219 } 220 221 static void 222 com_arbus_init_regs(struct com_regs *regsp, bus_space_tag_t st, 223 bus_space_handle_t sh, bus_addr_t addr, bus_size_t size) 224 { 225 #if _BYTE_ORDER == _BIG_ENDIAN 226 int off = 3; 227 #else 228 int off = 0; 229 #endif 230 231 com_init_regs(regsp, st, sh, addr); 232 233 /* rewrite the map to shift for alignment */ 234 for (size_t i = 0; i < __arraycount(regsp->cr_map); i++) { 235 regsp->cr_map[i] = (regsp->cr_map[i] * 4) + off; 236 } 237 regsp->cr_nports = size; 238 } 239 240 void 241 com_arbus_cnattach(bus_addr_t addr, uint32_t freq) 242 { 243 bus_space_tag_t bst; 244 bus_space_handle_t bsh; 245 struct com_regs regs; 246 247 bst = arbus_get_bus_space_tag(); 248 249 if (bus_space_map(bst, addr, 0x1000, 0, &bsh)) 250 return; 251 252 com_arbus_init_regs(®s, bst, bsh, addr, 0x1000); 253 254 comcnattach1(®s, com_arbus_baud, freq, COM_TYPE_NORMAL, CONMODE); 255 } 256 257