xref: /freebsd/sys/dev/dcons/dcons_crom.c (revision cab6a39d7b343596a5823e65c0f7b426551ec22d)
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/eventhandler.h>
42 #include <sys/kernel.h>
43 #include <sys/module.h>
44 #include <sys/systm.h>
45 #include <sys/types.h>
46 #include <sys/conf.h>
47 #include <sys/malloc.h>
48 
49 #include <sys/bus.h>
50 #include <machine/bus.h>
51 
52 #include <dev/firewire/firewire.h>
53 #include <dev/firewire/firewirereg.h>
54 #include <dev/firewire/iec13213.h>
55 #include <dev/dcons/dcons.h>
56 #include <dev/dcons/dcons_os.h>
57 
58 #include <sys/cons.h>
59 
60 #if (defined(__i386__) || defined(__amd64__))
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/pmap.h>
64 #include <machine/segments.h> /* for idt */
65 #endif
66 
67 static bus_addr_t dcons_paddr;
68 
69 static int force_console = 0;
70 TUNABLE_INT("hw.firewire.dcons_crom.force_console", &force_console);
71 
72 #define ADDR_HI(x)	(((x) >> 24) & 0xffffff)
73 #define ADDR_LO(x)	((x) & 0xffffff)
74 
75 struct dcons_crom_softc {
76         struct firewire_dev_comm fd;
77 	struct crom_chunk unit;
78 	struct crom_chunk spec;
79 	struct crom_chunk ver;
80 	bus_dma_tag_t dma_tag;
81 	bus_dmamap_t dma_map;
82 	bus_addr_t bus_addr;
83 	eventhandler_tag ehand;
84 };
85 
86 static void
87 dcons_crom_identify(driver_t *driver, device_t parent)
88 {
89 	BUS_ADD_CHILD(parent, 0, "dcons_crom", device_get_unit(parent));
90 }
91 
92 static int
93 dcons_crom_probe(device_t dev)
94 {
95 	device_t pa;
96 
97 	pa = device_get_parent(dev);
98 	if(device_get_unit(dev) != device_get_unit(pa)){
99 		return(ENXIO);
100 	}
101 
102 	device_set_desc(dev, "dcons configuration ROM");
103 	return (0);
104 }
105 
106 #if (defined(__i386__) || defined(__amd64__))
107 static void
108 dcons_crom_expose_idt(struct dcons_crom_softc *sc)
109 {
110 	static off_t idt_paddr;
111 
112 	/* XXX */
113 #ifdef __amd64__
114 	idt_paddr = (char *)idt - (char *)KERNBASE;
115 #else /* __i386__ */
116 	idt_paddr = (off_t)pmap_kextract((vm_offset_t)idt);
117 #endif
118 
119 	crom_add_entry(&sc->unit, DCONS_CSR_KEY_RESET_HI, ADDR_HI(idt_paddr));
120 	crom_add_entry(&sc->unit, DCONS_CSR_KEY_RESET_LO, ADDR_LO(idt_paddr));
121 }
122 #endif
123 
124 static void
125 dcons_crom_post_busreset(void *arg)
126 {
127 	struct dcons_crom_softc *sc;
128 	struct crom_src *src;
129 	struct crom_chunk *root;
130 
131 	sc = (struct dcons_crom_softc *) arg;
132 	src = sc->fd.fc->crom_src;
133 	root = sc->fd.fc->crom_root;
134 
135 	bzero(&sc->unit, sizeof(struct crom_chunk));
136 
137 	crom_add_chunk(src, root, &sc->unit, CROM_UDIR);
138 	crom_add_entry(&sc->unit, CSRKEY_SPEC, CSRVAL_VENDOR_PRIVATE);
139 	crom_add_simple_text(src, &sc->unit, &sc->spec, "FreeBSD");
140 	crom_add_entry(&sc->unit, CSRKEY_VER, DCONS_CSR_VAL_VER);
141 	crom_add_simple_text(src, &sc->unit, &sc->ver, "dcons");
142 	crom_add_entry(&sc->unit, DCONS_CSR_KEY_HI, ADDR_HI(dcons_paddr));
143 	crom_add_entry(&sc->unit, DCONS_CSR_KEY_LO, ADDR_LO(dcons_paddr));
144 #if (defined(__i386__) || defined(__amd64__))
145 	dcons_crom_expose_idt(sc);
146 #endif
147 }
148 
149 static void
150 dmamap_cb(void *arg, bus_dma_segment_t *segments, int seg, int error)
151 {
152 	struct dcons_crom_softc *sc;
153 
154 	if (error)
155 		printf("dcons_dmamap_cb: error=%d\n", error);
156 
157 	sc = (struct dcons_crom_softc *)arg;
158 	sc->bus_addr = segments[0].ds_addr;
159 
160 	bus_dmamap_sync(sc->dma_tag, sc->dma_map, BUS_DMASYNC_PREWRITE);
161 	device_printf(sc->fd.dev,
162 	    "bus_addr 0x%jx\n", (uintmax_t)sc->bus_addr);
163 	if (dcons_paddr != 0) {
164 		/* XXX */
165 		device_printf(sc->fd.dev, "dcons_paddr is already set\n");
166 		return;
167 	}
168 	dcons_conf->dma_tag = sc->dma_tag;
169 	dcons_conf->dma_map = sc->dma_map;
170 	dcons_paddr = sc->bus_addr;
171 
172 	/* Force to be the high-level console */
173 	if (force_console)
174 		cnselect(dcons_conf->cdev);
175 }
176 
177 static void
178 dcons_crom_poll(void *p, int arg)
179 {
180 	struct dcons_crom_softc *sc = (struct dcons_crom_softc *) p;
181 
182 	sc->fd.fc->poll(sc->fd.fc, -1, -1);
183 }
184 
185 static int
186 dcons_crom_attach(device_t dev)
187 {
188 	struct dcons_crom_softc *sc;
189 	int error;
190 
191 	if (dcons_conf->buf == NULL)
192 		return (ENXIO);
193         sc = (struct dcons_crom_softc *) device_get_softc(dev);
194 	sc->fd.fc = device_get_ivars(dev);
195 	sc->fd.dev = dev;
196 	sc->fd.post_explore = NULL;
197 	sc->fd.post_busreset = (void *) dcons_crom_post_busreset;
198 
199 	/* map dcons buffer */
200 	error = bus_dma_tag_create(
201 		/*parent*/ sc->fd.fc->dmat,
202 		/*alignment*/ sizeof(u_int32_t),
203 		/*boundary*/ 0,
204 		/*lowaddr*/ BUS_SPACE_MAXADDR,
205 		/*highaddr*/ BUS_SPACE_MAXADDR,
206 		/*filter*/NULL, /*filterarg*/NULL,
207 		/*maxsize*/ dcons_conf->size,
208 		/*nsegments*/ 1,
209 		/*maxsegsz*/ BUS_SPACE_MAXSIZE_32BIT,
210 		/*flags*/ BUS_DMA_ALLOCNOW,
211 		/*lockfunc*/busdma_lock_mutex,
212 		/*lockarg*/&Giant,
213 		&sc->dma_tag);
214 	if (error != 0)
215 		return (error);
216 	error = bus_dmamap_create(sc->dma_tag, BUS_DMA_COHERENT, &sc->dma_map);
217 	if (error != 0)
218 		return (error);
219 	error = bus_dmamap_load(sc->dma_tag, sc->dma_map,
220 	    (void *)dcons_conf->buf, dcons_conf->size,
221 	    dmamap_cb, sc, 0);
222 	if (error != 0)
223 		return (error);
224 	sc->ehand = EVENTHANDLER_REGISTER(dcons_poll, dcons_crom_poll,
225 			 (void *)sc, 0);
226 	return (0);
227 }
228 
229 static int
230 dcons_crom_detach(device_t dev)
231 {
232 	struct dcons_crom_softc *sc;
233 
234         sc = (struct dcons_crom_softc *) device_get_softc(dev);
235 	sc->fd.post_busreset = NULL;
236 
237 	if (sc->ehand)
238 		EVENTHANDLER_DEREGISTER(dcons_poll, sc->ehand);
239 
240 	/* XXX */
241 	if (dcons_conf->dma_tag == sc->dma_tag)
242 		dcons_conf->dma_tag = NULL;
243 
244 	bus_dmamap_unload(sc->dma_tag, sc->dma_map);
245 	bus_dmamap_destroy(sc->dma_tag, sc->dma_map);
246 	bus_dma_tag_destroy(sc->dma_tag);
247 
248 	return 0;
249 }
250 
251 static devclass_t dcons_crom_devclass;
252 
253 static device_method_t dcons_crom_methods[] = {
254 	/* device interface */
255 	DEVMETHOD(device_identify,	dcons_crom_identify),
256 	DEVMETHOD(device_probe,		dcons_crom_probe),
257 	DEVMETHOD(device_attach,	dcons_crom_attach),
258 	DEVMETHOD(device_detach,	dcons_crom_detach),
259 	{ 0, 0 }
260 };
261 
262 static driver_t dcons_crom_driver = {
263 	"dcons_crom",
264 	dcons_crom_methods,
265 	sizeof(struct dcons_crom_softc),
266 };
267 
268 DRIVER_MODULE(dcons_crom, firewire, dcons_crom_driver,
269 					dcons_crom_devclass, 0, 0);
270 MODULE_VERSION(dcons_crom, 1);
271 MODULE_DEPEND(dcons_crom, dcons,
272 	DCONS_VERSION, DCONS_VERSION, DCONS_VERSION);
273 MODULE_DEPEND(dcons_crom, firewire, 1, 1, 1);
274