mq200.c revision 1.1 1 /* $NetBSD: mq200.c,v 1.1 2000/07/22 08:53:36 takemura Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Takemura Shin
5 * 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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 */
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/device.h>
35 #include <sys/systm.h>
36
37 #include <uvm/uvm_extern.h>
38
39 #include <dev/wscons/wsconsio.h>
40
41 #include <machine/bootinfo.h>
42 #include <machine/bus.h>
43 #include <machine/autoconf.h>
44 #include <machine/config_hook.h>
45 #include <machine/platid.h>
46 #include <machine/platid_mask.h>
47
48 #include <hpcmips/dev/mq200reg.h>
49 #include <hpcmips/dev/mq200var.h>
50 #include <hpcmips/dev/bivideovar.h>
51
52 #define MQ200DEBUG
53 #ifdef MQ200DEBUG
54 #ifndef MQ200DEBUG_CONF
55 #define MQ200DEBUG_CONF 1
56 #endif
57 int mq200_debug = MQ200DEBUG_CONF;
58 #define DPRINTF(arg) do { if (mq200_debug) printf arg; } while(0);
59 #define DPRINTFN(n, arg) do { if (mq200_debug > (n)) printf arg; } while (0);
60 #else
61 #define DPRINTF(arg) do { } while (0);
62 #define DPRINTFN(n, arg) do { } while (0);
63 #endif
64
65 /*
66 * function prototypes
67 */
68 static void mq200_power __P((int, void *));
69 static int mq200_hardpower __P((void *, int, long, void *));
70 static int mq200_fbinit __P((struct hpcfb_fbconf *));
71 static int mq200_ioctl __P((void *, u_long, caddr_t, int, struct proc *));
72 static paddr_t mq200_mmap __P((void *, off_t offset, int));
73
74 /*
75 * static variables
76 */
77 struct hpcfb_accessops mq200_ha = {
78 mq200_ioctl, mq200_mmap
79 };
80
81 int
82 mq200_probe(iot, ioh)
83 bus_space_tag_t iot;
84 bus_space_handle_t ioh;
85 {
86 unsigned long regval;
87
88 regval = bus_space_read_4(iot, ioh, MQ200_PC00R);
89 DPRINTF(("mq200 probe: vendor id=%04lx product id=%04lx\n",
90 regval & 0xffff, (regval >> 16) & 0xffff));
91 if (regval != ((MQ200_PRODUCT_ID << 16) | MQ200_VENDOR_ID))
92 return (0);
93
94 return (1);
95 }
96
97 void
98 mq200_attach(sc)
99 struct mq200_softc *sc;
100 {
101 unsigned long regval;
102 struct hpcfb_attach_args ha;
103 int console = (bootinfo->bi_cnuse & BI_CNUSE_SERIAL) ? 0 : 1;
104
105 regval = bus_space_read_4(sc->sc_iot, sc->sc_ioh, MQ200_PC08R);
106 printf(": MQ200 Rev.%02lx video controller\n", regval & 0xff);
107
108 /* Add a power hook to power saving */
109 sc->sc_powerstate = MQ200_POWERSTATE_D0;
110 sc->sc_powerhook = powerhook_establish(mq200_power, sc);
111 if (sc->sc_powerhook == NULL)
112 printf("%s: WARNING: unable to establish power hook\n",
113 sc->sc_dev.dv_xname);
114
115 /* Add a hard power hook to power saving */
116 sc->sc_hardpowerhook = config_hook(CONFIG_HOOK_PMEVENT,
117 CONFIG_HOOK_PMEVENT_HARDPOWER,
118 CONFIG_HOOK_SHARE,
119 mq200_hardpower, sc);
120 if (sc->sc_hardpowerhook == NULL)
121 printf("%s: WARNING: unable to establish hard power hook\n",
122 sc->sc_dev.dv_xname);
123
124 mq200_fbinit(&sc->sc_fbconf);
125
126 if (console && hpcfb_cnattach(&sc->sc_fbconf) != 0) {
127 panic("mq200_attach: can't init fb console");
128 }
129
130 ha.ha_console = console;
131 ha.ha_accessops = &mq200_ha;
132 ha.ha_accessctx = sc;
133 ha.ha_curfbconf = 0;
134 ha.ha_nfbconf = 1;
135 ha.ha_fbconflist = &sc->sc_fbconf;
136 ha.ha_curdspconf = 0;
137 ha.ha_ndspconf = 1;
138 ha.ha_dspconflist = &sc->sc_dspconf;
139
140 config_found(&sc->sc_dev, &ha, hpcfbprint);
141
142 /*
143 * bivideo is no longer need
144 */
145 bivideo_dont_attach = 1;
146 }
147
148 static void
149 mq200_power(why, arg)
150 int why;
151 void *arg;
152 {
153 #if 0
154 struct mq200_softc *sc = arg;
155
156 switch (why) {
157 case PWR_SUSPEND:
158 sc->sc_powerstate = MQ200_POWERSTATE_D2;
159 break;
160 case PWR_STANDBY:
161 sc->sc_powerstate = MQ200_POWERSTATE_D3;
162 break;
163 case PWR_RESUME:
164 sc->sc_powerstate = MQ200_POWERSTATE_D0;
165 break;
166 }
167
168 printf("MQ200_PMCSR=%08x\n", sc->sc_powerstate);
169 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
170 MQ200_PMCSR, sc->sc_powerstate);
171 #endif
172 }
173
174 static int
175 mq200_hardpower(ctx, type, id, msg)
176 void *ctx;
177 int type;
178 long id;
179 void *msg;
180 {
181 struct mq200_softc *sc = ctx;
182 int why = (int)msg;
183
184 switch (why) {
185 case PWR_SUSPEND:
186 sc->sc_powerstate = MQ200_POWERSTATE_D2;
187 break;
188 case PWR_STANDBY:
189 sc->sc_powerstate = MQ200_POWERSTATE_D3;
190 break;
191 case PWR_RESUME:
192 sc->sc_powerstate = MQ200_POWERSTATE_D0;
193 break;
194 }
195
196 bus_space_write_4(sc->sc_iot, sc->sc_ioh,
197 MQ200_PMCSR, sc->sc_powerstate);
198
199 /*
200 * you should wait until the
201 * power state transit sequence will end.
202 */
203 {
204 unsigned long tmp;
205 do {
206 tmp = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
207 MQ200_PMCSR);
208 } while ((tmp & 0x3) != (sc->sc_powerstate & 0x3));
209 delay(100000); /* XXX */
210 }
211
212 return (0);
213 }
214
215
216 static int
217 mq200_fbinit(fb)
218 struct hpcfb_fbconf *fb;
219 {
220
221 /*
222 * get fb settings from bootinfo
223 */
224 if (bootinfo == NULL ||
225 bootinfo->fb_addr == 0 ||
226 bootinfo->fb_line_bytes == 0 ||
227 bootinfo->fb_width == 0 ||
228 bootinfo->fb_height == 0) {
229 printf("no frame buffer infomation.\n");
230 return (-1);
231 }
232
233 /* zero fill */
234 bzero(fb, sizeof(*fb));
235
236 fb->hf_conf_index = 0; /* configuration index */
237 fb->hf_nconfs = 1; /* how many configurations */
238 strcpy(fb->hf_name, "built-in video");
239 /* frame buffer name */
240 strcpy(fb->hf_conf_name, "default");
241 /* configuration name */
242 fb->hf_height = bootinfo->fb_height;
243 fb->hf_width = bootinfo->fb_width;
244 fb->hf_baseaddr = mips_ptob(mips_btop(bootinfo->fb_addr));
245 fb->hf_offset = (u_long)bootinfo->fb_addr - fb->hf_baseaddr;
246 /* frame buffer start offset */
247 fb->hf_bytes_per_line = bootinfo->fb_line_bytes;
248 fb->hf_nplanes = 1;
249 fb->hf_bytes_per_plane = bootinfo->fb_height *
250 bootinfo->fb_line_bytes;
251
252 fb->hf_access_flags |= HPCFB_ACCESS_BYTE;
253 fb->hf_access_flags |= HPCFB_ACCESS_WORD;
254 fb->hf_access_flags |= HPCFB_ACCESS_DWORD;
255
256 switch (bootinfo->fb_type) {
257 /*
258 * gray scale
259 */
260 case BIFB_D2_M2L_3:
261 case BIFB_D2_M2L_3x2:
262 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
263 /* fall through */
264 case BIFB_D2_M2L_0:
265 case BIFB_D2_M2L_0x2:
266 fb->hf_class = HPCFB_CLASS_GRAYSCALE;
267 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
268 fb->hf_pack_width = 8;
269 fb->hf_pixels_per_pack = 4;
270 fb->hf_pixel_width = 2;
271 fb->hf_class_data_length = sizeof(struct hf_gray_tag);
272 fb->hf_u.hf_gray.hf_flags = 0; /* reserved for future use */
273 break;
274
275 /*
276 * indexed color
277 */
278 case BIFB_D8_FF:
279 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
280 /* fall through */
281 case BIFB_D8_00:
282 fb->hf_class = HPCFB_CLASS_INDEXCOLOR;
283 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
284 fb->hf_pack_width = 8;
285 fb->hf_pixels_per_pack = 1;
286 fb->hf_pixel_width = 8;
287 fb->hf_class_data_length = sizeof(struct hf_indexed_tag);
288 fb->hf_u.hf_indexed.hf_flags = 0; /* reserved for future use */
289 break;
290
291 /*
292 * RGB color
293 */
294 case BIFB_D16_FFFF:
295 fb->hf_access_flags |= HPCFB_ACCESS_REVERSE;
296 /* fall through */
297 case BIFB_D16_0000:
298 fb->hf_class = HPCFB_CLASS_RGBCOLOR;
299 fb->hf_access_flags |= HPCFB_ACCESS_STATIC;
300 #if BYTE_ORDER == LITTLE_ENDIAN
301 fb->hf_swap_flags = HPCFB_SWAP_BYTE;
302 #endif
303 fb->hf_pack_width = 16;
304 fb->hf_pixels_per_pack = 1;
305 fb->hf_pixel_width = 16;
306
307 fb->hf_class_data_length = sizeof(struct hf_rgb_tag);
308 fb->hf_u.hf_rgb.hf_flags = 0; /* reserved for future use */
309
310 fb->hf_u.hf_rgb.hf_red_width = 5;
311 fb->hf_u.hf_rgb.hf_red_shift = 11;
312 fb->hf_u.hf_rgb.hf_green_width = 6;
313 fb->hf_u.hf_rgb.hf_green_shift = 5;
314 fb->hf_u.hf_rgb.hf_blue_width = 5;
315 fb->hf_u.hf_rgb.hf_blue_shift = 0;
316 fb->hf_u.hf_rgb.hf_alpha_width = 0;
317 fb->hf_u.hf_rgb.hf_alpha_shift = 0;
318 break;
319
320 default:
321 printf("unknown type (=%d).\n", bootinfo->fb_type);
322 return (-1);
323 break;
324 }
325
326 return (0); /* no error */
327 }
328
329 int
330 mq200_ioctl(v, cmd, data, flag, p)
331 void *v;
332 u_long cmd;
333 caddr_t data;
334 int flag;
335 struct proc *p;
336 {
337 struct mq200_softc *sc = (struct mq200_softc *)v;
338 struct hpcfb_fbconf *fbconf;
339 struct hpcfb_dspconf *dspconf;
340 struct wsdisplay_cmap *cmap;
341
342 switch (cmd) {
343 case WSDISPLAYIO_GETCMAP:
344 cmap = (struct wsdisplay_cmap*)data;
345
346 if (sc->sc_fbconf.hf_class != HPCFB_CLASS_INDEXCOLOR ||
347 sc->sc_fbconf.hf_pack_width != 8 ||
348 256 <= cmap->index ||
349 256 < (cmap->index + cmap->count))
350 return (EINVAL);
351
352 #if 0
353 if (!uvm_useracc(cmap->red, cmap->count, B_WRITE) ||
354 !uvm_useracc(cmap->green, cmap->count, B_WRITE) ||
355 !uvm_useracc(cmap->blue, cmap->count, B_WRITE))
356 return (EFAULT);
357
358 copyout(&bivideo_cmap_r[cmap->index], cmap->red, cmap->count);
359 copyout(&bivideo_cmap_g[cmap->index], cmap->green,cmap->count);
360 copyout(&bivideo_cmap_b[cmap->index], cmap->blue, cmap->count);
361 #endif
362
363 return (0);
364
365 case WSDISPLAYIO_PUTCMAP:
366 /*
367 * This driver can't set color map.
368 */
369 return (EINVAL);
370
371 case HPCFBIO_GCONF:
372 fbconf = (struct hpcfb_fbconf *)data;
373 if (fbconf->hf_conf_index != 0 &&
374 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
375 return (EINVAL);
376 }
377 *fbconf = sc->sc_fbconf; /* structure assignment */
378 return (0);
379 case HPCFBIO_SCONF:
380 fbconf = (struct hpcfb_fbconf *)data;
381 if (fbconf->hf_conf_index != 0 &&
382 fbconf->hf_conf_index != HPCFB_CURRENT_CONFIG) {
383 return (EINVAL);
384 }
385 /*
386 * nothing to do because we have only one configration
387 */
388 return (0);
389 case HPCFBIO_GDSPCONF:
390 dspconf = (struct hpcfb_dspconf *)data;
391 if ((dspconf->hd_unit_index != 0 &&
392 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
393 (dspconf->hd_conf_index != 0 &&
394 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
395 return (EINVAL);
396 }
397 *dspconf = sc->sc_dspconf; /* structure assignment */
398 return (0);
399 case HPCFBIO_SDSPCONF:
400 dspconf = (struct hpcfb_dspconf *)data;
401 if ((dspconf->hd_unit_index != 0 &&
402 dspconf->hd_unit_index != HPCFB_CURRENT_UNIT) ||
403 (dspconf->hd_conf_index != 0 &&
404 dspconf->hd_conf_index != HPCFB_CURRENT_CONFIG)) {
405 return (EINVAL);
406 }
407 /*
408 * nothing to do
409 * because we have only one unit and one configration
410 */
411 return (0);
412 case HPCFBIO_GOP:
413 case HPCFBIO_SOP:
414 /*
415 * curently not implemented...
416 */
417 return (EINVAL);
418 }
419
420 return (ENOTTY);
421 }
422
423 paddr_t
424 mq200_mmap(ctx, offset, prot)
425 void *ctx;
426 off_t offset;
427 int prot;
428 {
429 struct mq200_softc *sc = (struct mq200_softc *)ctx;
430
431 if (offset < 0 ||
432 (sc->sc_fbconf.hf_bytes_per_plane +
433 sc->sc_fbconf.hf_offset) < offset)
434 return -1;
435
436 return mips_btop(sc->sc_fbconf.hf_baseaddr + offset);
437 }
438