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