1 /* $NetBSD: com_hpcio.c,v 1.7 2003/07/15 02:29:28 lukem Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 TAKEMRUA Shin. 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 * 3. Neither the name of the project nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: com_hpcio.c,v 1.7 2003/07/15 02:29:28 lukem Exp $"); 34 35 #include "opt_kgdb.h" 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/device.h> 40 #include <sys/reboot.h> 41 #include <sys/termios.h> 42 43 #include <machine/intr.h> 44 #include <machine/bus.h> 45 #include <machine/platid.h> 46 #include <machine/platid_mask.h> 47 48 #include <hpcmips/dev/com_hpciovar.h> 49 50 #include <dev/hpc/hpciovar.h> 51 #include <dev/ic/comvar.h> 52 #include <dev/ic/comreg.h> 53 54 #include "locators.h" 55 56 #define COM_HPCIODEBUG 57 #ifdef COM_HPCIODEBUG 58 int com_hpcio_debug = 0; 59 #define DPRINTF(arg...) do { if (com_hpcio_debug) printf(arg); } while (0) 60 #else 61 #define DPRINTF(arg...) do {} while (0) 62 #endif 63 64 #define COM_HPCIO_BYTE_ALIGNMENT 0 65 #define COM_HPCIO_HALFWORD_ALIGNMENT 1 66 67 struct com_hpcio_softc { 68 struct com_softc hsc_com; 69 struct bus_space_tag hsc_iot; 70 struct hpcio_chip *hsc_hc; 71 }; 72 static struct bus_space_tag com_hpcio_cniotx; 73 static bus_space_tag_t com_hpcio_cniot = &com_hpcio_cniotx; 74 static int com_hpcio_cniobase; 75 76 static int com_hpcio_probe(struct device *, struct cfdata *, void *); 77 static void com_hpcio_attach(struct device *, struct device *, void *); 78 static int com_hpcio_common_probe(bus_space_tag_t, int, int *); 79 void com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base); 80 bus_space_protos(bs_notimpl); 81 bus_space_protos(bs_through); 82 bus_space_protos(com_hpcio); 83 84 CFATTACH_DECL(com_hpcio, sizeof(struct com_hpcio_softc), 85 com_hpcio_probe, com_hpcio_attach, NULL, NULL); 86 87 struct bus_space_ops com_hpcio_bs_ops = { 88 /* mapping/unmapping */ 89 bs_through_bs_map, 90 bs_through_bs_unmap, 91 bs_through_bs_subregion, 92 93 /* allocation/deallocation */ 94 bs_through_bs_alloc, 95 bs_through_bs_free, 96 97 /* get kernel virtual address */ 98 bs_through_bs_vaddr, /* there is no linear mapping */ 99 100 /* Mmap bus space for user */ 101 bs_through_bs_mmap, 102 103 /* barrier */ 104 bs_through_bs_barrier, 105 106 /* probe */ 107 bs_through_bs_peek, 108 bs_through_bs_poke, 109 110 /* read (single) */ 111 com_hpcio_bs_r_1, 112 bs_notimpl_bs_r_2, 113 bs_notimpl_bs_r_4, 114 bs_notimpl_bs_r_8, 115 116 /* read multiple */ 117 bs_notimpl_bs_rm_1, 118 bs_notimpl_bs_rm_2, 119 bs_notimpl_bs_rm_4, 120 bs_notimpl_bs_rm_8, 121 122 /* read region */ 123 bs_notimpl_bs_rr_1, 124 bs_notimpl_bs_rr_2, 125 bs_notimpl_bs_rr_4, 126 bs_notimpl_bs_rr_8, 127 128 /* write (single) */ 129 com_hpcio_bs_w_1, 130 bs_notimpl_bs_w_2, 131 bs_notimpl_bs_w_4, 132 bs_notimpl_bs_w_8, 133 134 /* write multiple */ 135 com_hpcio_bs_wm_1, 136 bs_notimpl_bs_wm_2, 137 bs_notimpl_bs_wm_4, 138 bs_notimpl_bs_wm_8, 139 140 /* write region */ 141 bs_notimpl_bs_wr_1, 142 bs_notimpl_bs_wr_2, 143 bs_notimpl_bs_wr_4, 144 bs_notimpl_bs_wr_8, 145 146 #ifdef BUS_SPACE_HAS_REAL_STREAM_METHODS 147 /* read stream (single) */ 148 bs_notimpl_bs_rs_1, 149 bs_notimpl_bs_rs_2, 150 bs_notimpl_bs_rs_4, 151 bs_notimpl_bs_rs_8, 152 153 /* read multiple stream */ 154 bs_notimpl_bs_rms_1, 155 bs_notimpl_bs_rms_2, 156 bs_notimpl_bs_rms_4, 157 bs_notimpl_bs_rms_8, 158 159 /* read region stream */ 160 bs_notimpl_bs_rrs_1, 161 bs_notimpl_bs_rrs_2, 162 bs_notimpl_bs_rrs_4, 163 bs_notimpl_bs_rrs_8, 164 165 /* write stream (single) */ 166 bs_notimpl_bs_ws_1, 167 bs_notimpl_bs_ws_2, 168 bs_notimpl_bs_ws_4, 169 bs_notimpl_bs_ws_8, 170 171 /* write multiple stream */ 172 bs_notimpl_bs_wms_1, 173 bs_notimpl_bs_wms_2, 174 bs_notimpl_bs_wms_4, 175 bs_notimpl_bs_wms_8, 176 177 /* write region stream */ 178 bs_notimpl_bs_wrs_1, 179 bs_notimpl_bs_wrs_2, 180 bs_notimpl_bs_wrs_4, 181 bs_notimpl_bs_wrs_8, 182 #endif /* BUS_SPACE_HAS_REAL_STREAM_METHODS */ 183 184 /* set multi */ 185 bs_notimpl_bs_sm_1, 186 bs_notimpl_bs_sm_2, 187 bs_notimpl_bs_sm_4, 188 bs_notimpl_bs_sm_8, 189 190 /* set region */ 191 bs_notimpl_bs_sr_1, 192 bs_notimpl_bs_sr_2, 193 bs_notimpl_bs_sr_4, 194 bs_notimpl_bs_sr_8, 195 196 /* copy */ 197 bs_notimpl_bs_c_1, 198 bs_notimpl_bs_c_2, 199 bs_notimpl_bs_c_4, 200 bs_notimpl_bs_c_8, 201 }; 202 203 int 204 com_hpcio_cndb_attach(bus_space_tag_t iot, int iobase, int rate, 205 int frequency, tcflag_t cflag, int kgdb) 206 { 207 int alignment; 208 209 DPRINTF("com_hpcio_cndb_attach()\n"); 210 if (!com_hpcio_common_probe(iot, iobase, &alignment)) { 211 DPRINTF("com_hpcio_cndb_attach(): probe failed\n"); 212 return (ENOTTY); 213 } 214 if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) { 215 DPRINTF("com_hpcio_cndb_attach(): half word aligned\n"); 216 com_hpcio_iot_init(&com_hpcio_cniotx, iot); 217 com_hpcio_cniot = &com_hpcio_cniotx; 218 } else { 219 com_hpcio_cniot = iot; 220 } 221 com_hpcio_cniobase = iobase; 222 DPRINTF("com_hpcio_cndb_attach(): probe succeeded\n"); 223 #ifdef KGDB 224 if (kgdb) 225 return (com_kgdb_attach(com_hpcio_cniot, iobase, rate, 226 frequency, COM_TYPE_NORMAL, cflag)); 227 else 228 #endif 229 return (comcnattach(com_hpcio_cniot, iobase, rate, 230 frequency, COM_TYPE_NORMAL, cflag)); 231 } 232 233 static int 234 com_hpcio_common_probe(bus_space_tag_t iot, int iobase, int *alignment) 235 { 236 bus_space_handle_t ioh; 237 static struct bus_space_tag tmpiot; 238 int rv; 239 240 /* 241 * try byte aligned register 242 */ 243 *alignment = COM_HPCIO_BYTE_ALIGNMENT; 244 if (bus_space_map(iot, iobase, 1, 0, &ioh)) 245 return 0; 246 rv = comprobe1(iot, ioh); 247 bus_space_unmap(iot, ioh, 1); 248 249 if (rv != 0) 250 return (rv); 251 252 /* 253 * try half word aligned register 254 */ 255 *alignment = COM_HPCIO_HALFWORD_ALIGNMENT; 256 com_hpcio_iot_init(&tmpiot, iot); 257 if (bus_space_map(&tmpiot, iobase, 1, 0, &ioh)) 258 return 0; 259 rv = comprobe1(&tmpiot, ioh); 260 bus_space_unmap(&tmpiot, ioh, 1); 261 262 return (rv); 263 } 264 265 static int 266 com_hpcio_probe(struct device *parent, struct cfdata *cf, void *aux) 267 { 268 struct hpcio_attach_args *haa = aux; 269 bus_space_tag_t iot = haa->haa_iot; 270 int addr, alignment; 271 272 if (cf->cf_loc[HPCIOIFCF_PLATFORM] != HPCIOIFCF_PLATFORM_DEFAULT) { 273 platid_mask_t mask; 274 275 mask = PLATID_DEREF(cf->cf_loc[HPCIOIFCF_PLATFORM]); 276 if (!platid_match(&platid, &mask)) 277 return (0); /* platform id didn't match */ 278 } 279 280 if ((addr = cf->cf_loc[HPCIOIFCF_ADDR]) == HPCIOIFCF_ADDR_DEFAULT) 281 return (0); /* address wasn't specified */ 282 283 return com_hpcio_common_probe(iot, addr, &alignment); 284 } 285 286 287 static void 288 com_hpcio_attach(struct device *parent, struct device *self, void *aux) 289 { 290 struct com_hpcio_softc *hsc = (void *)self; 291 struct com_softc *sc = &hsc->hsc_com; 292 struct hpcio_attach_args *haa = aux; 293 bus_space_tag_t iot; 294 bus_space_handle_t ioh; 295 int addr, port, mode, alignment, *loc; 296 297 loc = sc->sc_dev.dv_cfdata->cf_loc; 298 addr = loc[HPCIOIFCF_ADDR]; 299 printf(" addr %x", addr); 300 if ((com_hpcio_cniot == haa->haa_iot || 301 com_hpcio_cniot->bs_base == haa->haa_iot) && 302 com_hpcio_cniobase == addr && 303 com_is_console(com_hpcio_cniot, addr, 0)) { 304 iot = com_hpcio_cniot; 305 if (com_hpcio_cniot->bs_base == haa->haa_iot) 306 printf(", half word aligned"); 307 } else { 308 com_hpcio_common_probe(haa->haa_iot, addr, &alignment); 309 if (alignment == COM_HPCIO_HALFWORD_ALIGNMENT) { 310 printf(", half word aligned"); 311 iot = &hsc->hsc_iot; 312 com_hpcio_iot_init(iot, haa->haa_iot); 313 } else { 314 iot = haa->haa_iot; 315 } 316 } 317 if (bus_space_map(iot, addr, 1, 0, &ioh)) { 318 printf(": can't map bus space\n"); 319 return; 320 } 321 sc->sc_iobase = addr; 322 sc->sc_iot = iot; 323 sc->sc_ioh = ioh; 324 325 sc->enable = NULL; 326 sc->disable = NULL; 327 328 sc->sc_frequency = COM_FREQ; 329 com_attach_subr(sc); 330 331 hsc->hsc_hc = (*haa->haa_getchip)(haa->haa_sc, loc[HPCIOIFCF_IOCHIP]); 332 port = loc[HPCIOIFCF_PORT]; 333 mode = HPCIO_INTR_LEVEL | HPCIO_INTR_HIGH; 334 hpcio_intr_establish(hsc->hsc_hc, port, mode, comintr, sc); 335 } 336 337 /* 338 * bus stuff (registershalf word aligned) 339 */ 340 void 341 com_hpcio_iot_init(bus_space_tag_t iot, bus_space_tag_t base) 342 { 343 344 iot->bs_base = base; 345 iot->bs_ops = com_hpcio_bs_ops; /* structure assignment */ 346 iot->bs_ops.bs_r_1 = com_hpcio_bs_r_1; 347 iot->bs_ops.bs_w_1 = com_hpcio_bs_w_1; 348 iot->bs_ops.bs_wm_1 = com_hpcio_bs_wm_1; 349 } 350 351 u_int8_t 352 com_hpcio_bs_r_1(bus_space_tag_t t, bus_space_handle_t bsh, 353 bus_size_t offset) 354 { 355 return bus_space_read_1(t->bs_base, bsh, offset * 2); 356 } 357 358 void 359 com_hpcio_bs_w_1(bus_space_tag_t t, bus_space_handle_t bsh, 360 bus_size_t offset, u_int8_t value) 361 { 362 bus_space_write_1(t->bs_base, bsh, offset * 2, value); 363 } 364 365 void 366 com_hpcio_bs_wm_1(bus_space_tag_t t, bus_space_handle_t bsh, 367 bus_size_t offset, const u_int8_t *addr, bus_size_t count) 368 { 369 bus_space_write_multi_1(t->bs_base, bsh, offset * 2, addr, count); 370 } 371