2 * SPDX-License-Identifier: BSD-4-Clause
5 * Hidetoshi Shimokawa. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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:
18 * This product includes software developed by Hidetoshi Shimokawa.
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.
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
36 * $Id: dcons_crom.c,v 1.8 2003/10/23 15:47:21 simokawa Exp $
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>
46 #include <sys/malloc.h>
49 #include <machine/bus.h>
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>
59 #if (defined(__i386__) || defined(__amd64__))
61 #include <vm/vm_param.h>
63 #include <machine/segments.h> /* for idt */
66 static bus_addr_t dcons_paddr;
68 static int force_console = 0;
69 TUNABLE_INT("hw.firewire.dcons_crom.force_console", &force_console);
71 #define ADDR_HI(x) (((x) >> 24) & 0xffffff)
72 #define ADDR_LO(x) ((x) & 0xffffff)
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;
82 eventhandler_tag ehand;
86 dcons_crom_identify(driver_t *driver, device_t parent)
88 BUS_ADD_CHILD(parent, 0, "dcons_crom", device_get_unit(parent));
92 dcons_crom_probe(device_t dev)
96 pa = device_get_parent(dev);
97 if(device_get_unit(dev) != device_get_unit(pa)){
101 device_set_desc(dev, "dcons configuration ROM");
105 #if (defined(__i386__) || defined(__amd64__))
107 dcons_crom_expose_idt(struct dcons_crom_softc *sc)
109 static off_t idt_paddr;
113 idt_paddr = (char *)idt - (char *)KERNBASE;
115 idt_paddr = (off_t)pmap_kextract((vm_offset_t)idt);
118 crom_add_entry(&sc->unit, DCONS_CSR_KEY_RESET_HI, ADDR_HI(idt_paddr));
119 crom_add_entry(&sc->unit, DCONS_CSR_KEY_RESET_LO, ADDR_LO(idt_paddr));
124 dcons_crom_post_busreset(void *arg)
126 struct dcons_crom_softc *sc;
127 struct crom_src *src;
128 struct crom_chunk *root;
130 sc = (struct dcons_crom_softc *) arg;
131 src = sc->fd.fc->crom_src;
132 root = sc->fd.fc->crom_root;
134 bzero(&sc->unit, sizeof(struct crom_chunk));
136 crom_add_chunk(src, root, &sc->unit, CROM_UDIR);
137 crom_add_entry(&sc->unit, CSRKEY_SPEC, CSRVAL_VENDOR_PRIVATE);
138 crom_add_simple_text(src, &sc->unit, &sc->spec, "FreeBSD");
139 crom_add_entry(&sc->unit, CSRKEY_VER, DCONS_CSR_VAL_VER);
140 crom_add_simple_text(src, &sc->unit, &sc->ver, "dcons");
141 crom_add_entry(&sc->unit, DCONS_CSR_KEY_HI, ADDR_HI(dcons_paddr));
142 crom_add_entry(&sc->unit, DCONS_CSR_KEY_LO, ADDR_LO(dcons_paddr));
143 #if (defined(__i386__) || defined(__amd64__))
144 dcons_crom_expose_idt(sc);
149 dmamap_cb(void *arg, bus_dma_segment_t *segments, int seg, int error)
151 struct dcons_crom_softc *sc;
154 printf("dcons_dmamap_cb: error=%d\n", error);
156 sc = (struct dcons_crom_softc *)arg;
157 sc->bus_addr = segments[0].ds_addr;
159 bus_dmamap_sync(sc->dma_tag, sc->dma_map, BUS_DMASYNC_PREWRITE);
160 device_printf(sc->fd.dev,
161 "bus_addr 0x%jx\n", (uintmax_t)sc->bus_addr);
162 if (dcons_paddr != 0) {
164 device_printf(sc->fd.dev, "dcons_paddr is already set\n");
167 dcons_conf->dma_tag = sc->dma_tag;
168 dcons_conf->dma_map = sc->dma_map;
169 dcons_paddr = sc->bus_addr;
171 /* Force to be the high-level console */
173 cnselect(dcons_conf->cdev);
177 dcons_crom_poll(void *p, int arg)
179 struct dcons_crom_softc *sc = (struct dcons_crom_softc *) p;
181 sc->fd.fc->poll(sc->fd.fc, -1, -1);
185 dcons_crom_attach(device_t dev)
187 struct dcons_crom_softc *sc;
190 if (dcons_conf->buf == NULL)
192 sc = (struct dcons_crom_softc *) device_get_softc(dev);
193 sc->fd.fc = device_get_ivars(dev);
195 sc->fd.post_explore = NULL;
196 sc->fd.post_busreset = (void *) dcons_crom_post_busreset;
198 /* map dcons buffer */
199 error = bus_dma_tag_create(
200 /*parent*/ sc->fd.fc->dmat,
201 /*alignment*/ sizeof(u_int32_t),
203 /*lowaddr*/ BUS_SPACE_MAXADDR,
204 /*highaddr*/ BUS_SPACE_MAXADDR,
205 /*filter*/NULL, /*filterarg*/NULL,
206 /*maxsize*/ dcons_conf->size,
208 /*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
209 /*flags*/ BUS_DMA_ALLOCNOW,
210 /*lockfunc*/busdma_lock_mutex,
215 error = bus_dmamap_create(sc->dma_tag, BUS_DMA_COHERENT, &sc->dma_map);
218 error = bus_dmamap_load(sc->dma_tag, sc->dma_map,
219 (void *)dcons_conf->buf, dcons_conf->size,
223 sc->ehand = EVENTHANDLER_REGISTER(dcons_poll, dcons_crom_poll,
229 dcons_crom_detach(device_t dev)
231 struct dcons_crom_softc *sc;
233 sc = (struct dcons_crom_softc *) device_get_softc(dev);
234 sc->fd.post_busreset = NULL;
237 EVENTHANDLER_DEREGISTER(dcons_poll, sc->ehand);
240 if (dcons_conf->dma_tag == sc->dma_tag)
241 dcons_conf->dma_tag = NULL;
243 bus_dmamap_unload(sc->dma_tag, sc->dma_map);
244 bus_dmamap_destroy(sc->dma_tag, sc->dma_map);
245 bus_dma_tag_destroy(sc->dma_tag);
250 static devclass_t dcons_crom_devclass;
252 static device_method_t dcons_crom_methods[] = {
253 /* device interface */
254 DEVMETHOD(device_identify, dcons_crom_identify),
255 DEVMETHOD(device_probe, dcons_crom_probe),
256 DEVMETHOD(device_attach, dcons_crom_attach),
257 DEVMETHOD(device_detach, dcons_crom_detach),
261 static driver_t dcons_crom_driver = {
264 sizeof(struct dcons_crom_softc),
267 DRIVER_MODULE(dcons_crom, firewire, dcons_crom_driver,
268 dcons_crom_devclass, 0, 0);
269 MODULE_VERSION(dcons_crom, 1);
270 MODULE_DEPEND(dcons_crom, dcons,
271 DCONS_VERSION, DCONS_VERSION, DCONS_VERSION);
272 MODULE_DEPEND(dcons_crom, firewire, 1, 1, 1);