1 /*- 2 * SPDX-License-Identifier: BSD-4-Clause 3 * 4 * Copyright (C) 2003 5 * Hidetoshi Shimokawa. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * 18 * This product includes software developed by Hidetoshi Shimokawa. 19 * 20 * 4. Neither the name of the author nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * $Id: dcons_crom.c,v 1.8 2003/10/23 15:47:21 simokawa Exp $ 37 * $FreeBSD$ 38 */ 39 40 #include <sys/param.h> 41 #include <sys/kernel.h> 42 #include <sys/module.h> 43 #include <sys/systm.h> 44 #include <sys/types.h> 45 #include <sys/conf.h> 46 #include <sys/malloc.h> 47 48 #include <sys/bus.h> 49 #include <machine/bus.h> 50 51 #include <dev/firewire/firewire.h> 52 #include <dev/firewire/firewirereg.h> 53 #include <dev/firewire/iec13213.h> 54 #include <dev/dcons/dcons.h> 55 #include <dev/dcons/dcons_os.h> 56 57 #include <sys/cons.h> 58 59 #if (defined(__i386__) || defined(__amd64__)) 60 #include <vm/vm.h> 61 #include <vm/vm_param.h> 62 #include <vm/pmap.h> 63 #include <machine/segments.h> /* for idt */ 64 #endif 65 66 static bus_addr_t dcons_paddr; 67 68 static int force_console = 0; 69 TUNABLE_INT("hw.firewire.dcons_crom.force_console", &force_console); 70 71 #define ADDR_HI(x) (((x) >> 24) & 0xffffff) 72 #define ADDR_LO(x) ((x) & 0xffffff) 73 74 struct dcons_crom_softc { 75 struct firewire_dev_comm fd; 76 struct crom_chunk unit; 77 struct crom_chunk spec; 78 struct crom_chunk ver; 79 bus_dma_tag_t dma_tag; 80 bus_dmamap_t dma_map; 81 bus_addr_t bus_addr; 82 eventhandler_tag ehand; 83 }; 84 85 static void 86 dcons_crom_identify(driver_t *driver, device_t parent) 87 { 88 BUS_ADD_CHILD(parent, 0, "dcons_crom", device_get_unit(parent)); 89 } 90 91 static int 92 dcons_crom_probe(device_t dev) 93 { 94 device_t pa; 95 96 pa = device_get_parent(dev); 97 if(device_get_unit(dev) != device_get_unit(pa)){ 98 return(ENXIO); 99 } 100 101 device_set_desc(dev, "dcons configuration ROM"); 102 return (0); 103 } 104 105 #if (defined(__i386__) || defined(__amd64__)) 106 static void 107 dcons_crom_expose_idt(struct dcons_crom_softc *sc) 108 { 109 static off_t idt_paddr; 110 111 /* XXX */ 112 idt_paddr = (char *)idt - (char *)KERNBASE; 113 114 crom_add_entry(&sc->unit, DCONS_CSR_KEY_RESET_HI, ADDR_HI(idt_paddr)); 115 crom_add_entry(&sc->unit, DCONS_CSR_KEY_RESET_LO, ADDR_LO(idt_paddr)); 116 } 117 #endif 118 119 static void 120 dcons_crom_post_busreset(void *arg) 121 { 122 struct dcons_crom_softc *sc; 123 struct crom_src *src; 124 struct crom_chunk *root; 125 126 sc = (struct dcons_crom_softc *) arg; 127 src = sc->fd.fc->crom_src; 128 root = sc->fd.fc->crom_root; 129 130 bzero(&sc->unit, sizeof(struct crom_chunk)); 131 132 crom_add_chunk(src, root, &sc->unit, CROM_UDIR); 133 crom_add_entry(&sc->unit, CSRKEY_SPEC, CSRVAL_VENDOR_PRIVATE); 134 crom_add_simple_text(src, &sc->unit, &sc->spec, "FreeBSD"); 135 crom_add_entry(&sc->unit, CSRKEY_VER, DCONS_CSR_VAL_VER); 136 crom_add_simple_text(src, &sc->unit, &sc->ver, "dcons"); 137 crom_add_entry(&sc->unit, DCONS_CSR_KEY_HI, ADDR_HI(dcons_paddr)); 138 crom_add_entry(&sc->unit, DCONS_CSR_KEY_LO, ADDR_LO(dcons_paddr)); 139 #if (defined(__i386__) || defined(__amd64__)) 140 dcons_crom_expose_idt(sc); 141 #endif 142 } 143 144 static void 145 dmamap_cb(void *arg, bus_dma_segment_t *segments, int seg, int error) 146 { 147 struct dcons_crom_softc *sc; 148 149 if (error) 150 printf("dcons_dmamap_cb: error=%d\n", error); 151 152 sc = (struct dcons_crom_softc *)arg; 153 sc->bus_addr = segments[0].ds_addr; 154 155 bus_dmamap_sync(sc->dma_tag, sc->dma_map, BUS_DMASYNC_PREWRITE); 156 device_printf(sc->fd.dev, 157 "bus_addr 0x%jx\n", (uintmax_t)sc->bus_addr); 158 if (dcons_paddr != 0) { 159 /* XXX */ 160 device_printf(sc->fd.dev, "dcons_paddr is already set\n"); 161 return; 162 } 163 dcons_conf->dma_tag = sc->dma_tag; 164 dcons_conf->dma_map = sc->dma_map; 165 dcons_paddr = sc->bus_addr; 166 167 /* Force to be the high-level console */ 168 if (force_console) 169 cnselect(dcons_conf->cdev); 170 } 171 172 static void 173 dcons_crom_poll(void *p, int arg) 174 { 175 struct dcons_crom_softc *sc = (struct dcons_crom_softc *) p; 176 177 sc->fd.fc->poll(sc->fd.fc, -1, -1); 178 } 179 180 static int 181 dcons_crom_attach(device_t dev) 182 { 183 struct dcons_crom_softc *sc; 184 int error; 185 186 if (dcons_conf->buf == NULL) 187 return (ENXIO); 188 sc = (struct dcons_crom_softc *) device_get_softc(dev); 189 sc->fd.fc = device_get_ivars(dev); 190 sc->fd.dev = dev; 191 sc->fd.post_explore = NULL; 192 sc->fd.post_busreset = (void *) dcons_crom_post_busreset; 193 194 /* map dcons buffer */ 195 error = bus_dma_tag_create( 196 /*parent*/ sc->fd.fc->dmat, 197 /*alignment*/ sizeof(u_int32_t), 198 /*boundary*/ 0, 199 /*lowaddr*/ BUS_SPACE_MAXADDR, 200 /*highaddr*/ BUS_SPACE_MAXADDR, 201 /*filter*/NULL, /*filterarg*/NULL, 202 /*maxsize*/ dcons_conf->size, 203 /*nsegments*/ 1, 204 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT, 205 /*flags*/ BUS_DMA_ALLOCNOW, 206 /*lockfunc*/busdma_lock_mutex, 207 /*lockarg*/&Giant, 208 &sc->dma_tag); 209 if (error != 0) 210 return (error); 211 error = bus_dmamap_create(sc->dma_tag, BUS_DMA_COHERENT, &sc->dma_map); 212 if (error != 0) 213 return (error); 214 error = bus_dmamap_load(sc->dma_tag, sc->dma_map, 215 (void *)dcons_conf->buf, dcons_conf->size, 216 dmamap_cb, sc, 0); 217 if (error != 0) 218 return (error); 219 sc->ehand = EVENTHANDLER_REGISTER(dcons_poll, dcons_crom_poll, 220 (void *)sc, 0); 221 return (0); 222 } 223 224 static int 225 dcons_crom_detach(device_t dev) 226 { 227 struct dcons_crom_softc *sc; 228 229 sc = (struct dcons_crom_softc *) device_get_softc(dev); 230 sc->fd.post_busreset = NULL; 231 232 if (sc->ehand) 233 EVENTHANDLER_DEREGISTER(dcons_poll, sc->ehand); 234 235 /* XXX */ 236 if (dcons_conf->dma_tag == sc->dma_tag) 237 dcons_conf->dma_tag = NULL; 238 239 bus_dmamap_unload(sc->dma_tag, sc->dma_map); 240 bus_dmamap_destroy(sc->dma_tag, sc->dma_map); 241 bus_dma_tag_destroy(sc->dma_tag); 242 243 return 0; 244 } 245 246 static devclass_t dcons_crom_devclass; 247 248 static device_method_t dcons_crom_methods[] = { 249 /* device interface */ 250 DEVMETHOD(device_identify, dcons_crom_identify), 251 DEVMETHOD(device_probe, dcons_crom_probe), 252 DEVMETHOD(device_attach, dcons_crom_attach), 253 DEVMETHOD(device_detach, dcons_crom_detach), 254 { 0, 0 } 255 }; 256 257 static driver_t dcons_crom_driver = { 258 "dcons_crom", 259 dcons_crom_methods, 260 sizeof(struct dcons_crom_softc), 261 }; 262 263 DRIVER_MODULE(dcons_crom, firewire, dcons_crom_driver, 264 dcons_crom_devclass, 0, 0); 265 MODULE_VERSION(dcons_crom, 1); 266 MODULE_DEPEND(dcons_crom, dcons, 267 DCONS_VERSION, DCONS_VERSION, DCONS_VERSION); 268 MODULE_DEPEND(dcons_crom, firewire, 1, 1, 1); 269