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