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