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