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