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