ofw_syscons.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2003 Peter Grehan
  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. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/param.h>
  29. #include <sys/systm.h>
  30. #include <sys/module.h>
  31. #include <sys/bus.h>
  32. #include <sys/kernel.h>
  33. #include <sys/sysctl.h>
  34. #include <sys/limits.h>
  35. #include <sys/conf.h>
  36. #include <sys/cons.h>
  37. #include <sys/proc.h>
  38. #include <sys/fcntl.h>
  39. #include <sys/malloc.h>
  40. #include <sys/fbio.h>
  41. #include <sys/consio.h>
  42. #include <machine/bus.h>
  43. #include <machine/sc_machdep.h>
  44. #include <machine/vm.h>
  45. #include <sys/rman.h>
  46. #include <dev/fb/fbreg.h>
  47. #include <dev/syscons/syscons.h>
  48. #include <dev/ofw/openfirm.h>
  49. #include <dev/ofw/ofw_bus.h>
  50. #include <dev/ofw/ofw_pci.h>
  51. #include <powerpc/ofw/ofw_syscons.h>
  52. static int ofwfb_ignore_mmap_checks = 1;
  53. static int ofwfb_reset_on_switch = 1;
  54. static SYSCTL_NODE(_hw, OID_AUTO, ofwfb, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  55. "ofwfb");
  56. SYSCTL_INT(_hw_ofwfb, OID_AUTO, relax_mmap, CTLFLAG_RW,
  57. &ofwfb_ignore_mmap_checks, 0, "relaxed mmap bounds checking");
  58. SYSCTL_INT(_hw_ofwfb, OID_AUTO, reset_on_mode_switch, CTLFLAG_RW,
  59. &ofwfb_reset_on_switch, 0, "reset the framebuffer driver on mode switch");
  60. extern u_char dflt_font_16[];
  61. extern u_char dflt_font_14[];
  62. extern u_char dflt_font_8[];
  63. static int ofwfb_configure(int flags);
  64. static vi_probe_t ofwfb_probe;
  65. static vi_init_t ofwfb_init;
  66. static vi_get_info_t ofwfb_get_info;
  67. static vi_query_mode_t ofwfb_query_mode;
  68. static vi_set_mode_t ofwfb_set_mode;
  69. static vi_save_font_t ofwfb_save_font;
  70. static vi_load_font_t ofwfb_load_font;
  71. static vi_show_font_t ofwfb_show_font;
  72. static vi_save_palette_t ofwfb_save_palette;
  73. static vi_load_palette_t ofwfb_load_palette;
  74. static vi_set_border_t ofwfb_set_border;
  75. static vi_save_state_t ofwfb_save_state;
  76. static vi_load_state_t ofwfb_load_state;
  77. static vi_set_win_org_t ofwfb_set_win_org;
  78. static vi_read_hw_cursor_t ofwfb_read_hw_cursor;
  79. static vi_set_hw_cursor_t ofwfb_set_hw_cursor;
  80. static vi_set_hw_cursor_shape_t ofwfb_set_hw_cursor_shape;
  81. static vi_blank_display_t ofwfb_blank_display;
  82. static vi_mmap_t ofwfb_mmap;
  83. static vi_ioctl_t ofwfb_ioctl;
  84. static vi_clear_t ofwfb_clear;
  85. static vi_fill_rect_t ofwfb_fill_rect;
  86. static vi_bitblt_t ofwfb_bitblt;
  87. static vi_diag_t ofwfb_diag;
  88. static vi_save_cursor_palette_t ofwfb_save_cursor_palette;
  89. static vi_load_cursor_palette_t ofwfb_load_cursor_palette;
  90. static vi_copy_t ofwfb_copy;
  91. static vi_putp_t ofwfb_putp;
  92. static vi_putc_t ofwfb_putc;
  93. static vi_puts_t ofwfb_puts;
  94. static vi_putm_t ofwfb_putm;
  95. static video_switch_t ofwfbvidsw = {
  96. .probe = ofwfb_probe,
  97. .init = ofwfb_init,
  98. .get_info = ofwfb_get_info,
  99. .query_mode = ofwfb_query_mode,
  100. .set_mode = ofwfb_set_mode,
  101. .save_font = ofwfb_save_font,
  102. .load_font = ofwfb_load_font,
  103. .show_font = ofwfb_show_font,
  104. .save_palette = ofwfb_save_palette,
  105. .load_palette = ofwfb_load_palette,
  106. .set_border = ofwfb_set_border,
  107. .save_state = ofwfb_save_state,
  108. .load_state = ofwfb_load_state,
  109. .set_win_org = ofwfb_set_win_org,
  110. .read_hw_cursor = ofwfb_read_hw_cursor,
  111. .set_hw_cursor = ofwfb_set_hw_cursor,
  112. .set_hw_cursor_shape = ofwfb_set_hw_cursor_shape,
  113. .blank_display = ofwfb_blank_display,
  114. .mmap = ofwfb_mmap,
  115. .ioctl = ofwfb_ioctl,
  116. .clear = ofwfb_clear,
  117. .fill_rect = ofwfb_fill_rect,
  118. .bitblt = ofwfb_bitblt,
  119. .diag = ofwfb_diag,
  120. .save_cursor_palette = ofwfb_save_cursor_palette,
  121. .load_cursor_palette = ofwfb_load_cursor_palette,
  122. .copy = ofwfb_copy,
  123. .putp = ofwfb_putp,
  124. .putc = ofwfb_putc,
  125. .puts = ofwfb_puts,
  126. .putm = ofwfb_putm,
  127. };
  128. /*
  129. * bitmap depth-specific routines
  130. */
  131. static vi_blank_display_t ofwfb_blank_display8;
  132. static vi_putc_t ofwfb_putc8;
  133. static vi_putm_t ofwfb_putm8;
  134. static vi_set_border_t ofwfb_set_border8;
  135. static vi_blank_display_t ofwfb_blank_display32;
  136. static vi_putc_t ofwfb_putc32;
  137. static vi_putm_t ofwfb_putm32;
  138. static vi_set_border_t ofwfb_set_border32;
  139. VIDEO_DRIVER(ofwfb, ofwfbvidsw, ofwfb_configure);
  140. extern sc_rndr_sw_t txtrndrsw;
  141. RENDERER(ofwfb, 0, txtrndrsw, gfb_set);
  142. RENDERER_MODULE(ofwfb, gfb_set);
  143. /*
  144. * Define the iso6429-1983 colormap
  145. */
  146. static struct {
  147. uint8_t red;
  148. uint8_t green;
  149. uint8_t blue;
  150. } ofwfb_cmap[16] = { /* # R G B Color */
  151. /* - - - - ----- */
  152. { 0x00, 0x00, 0x00 }, /* 0 0 0 0 Black */
  153. { 0x00, 0x00, 0xaa }, /* 1 0 0 2/3 Blue */
  154. { 0x00, 0xaa, 0x00 }, /* 2 0 2/3 0 Green */
  155. { 0x00, 0xaa, 0xaa }, /* 3 0 2/3 2/3 Cyan */
  156. { 0xaa, 0x00, 0x00 }, /* 4 2/3 0 0 Red */
  157. { 0xaa, 0x00, 0xaa }, /* 5 2/3 0 2/3 Magenta */
  158. { 0xaa, 0x55, 0x00 }, /* 6 2/3 1/3 0 Brown */
  159. { 0xaa, 0xaa, 0xaa }, /* 7 2/3 2/3 2/3 White */
  160. { 0x55, 0x55, 0x55 }, /* 8 1/3 1/3 1/3 Gray */
  161. { 0x55, 0x55, 0xff }, /* 9 1/3 1/3 1 Bright Blue */
  162. { 0x55, 0xff, 0x55 }, /* 10 1/3 1 1/3 Bright Green */
  163. { 0x55, 0xff, 0xff }, /* 11 1/3 1 1 Bright Cyan */
  164. { 0xff, 0x55, 0x55 }, /* 12 1 1/3 1/3 Bright Red */
  165. { 0xff, 0x55, 0xff }, /* 13 1 1/3 1 Bright Magenta */
  166. { 0xff, 0xff, 0x80 }, /* 14 1 1 1/3 Bright Yellow */
  167. { 0xff, 0xff, 0xff } /* 15 1 1 1 Bright White */
  168. };
  169. #define TODO printf("%s: unimplemented\n", __func__)
  170. static u_int16_t ofwfb_static_window[ROW*COL];
  171. static struct ofwfb_softc ofwfb_softc;
  172. static __inline int
  173. ofwfb_background(uint8_t attr)
  174. {
  175. return (attr >> 4);
  176. }
  177. static __inline int
  178. ofwfb_foreground(uint8_t attr)
  179. {
  180. return (attr & 0x0f);
  181. }
  182. static u_int
  183. ofwfb_pix32(struct ofwfb_softc *sc, int attr)
  184. {
  185. u_int retval;
  186. if (sc->sc_tag == &bs_le_tag)
  187. retval = (ofwfb_cmap[attr].red << 16) |
  188. (ofwfb_cmap[attr].green << 8) |
  189. ofwfb_cmap[attr].blue;
  190. else
  191. retval = (ofwfb_cmap[attr].blue << 16) |
  192. (ofwfb_cmap[attr].green << 8) |
  193. ofwfb_cmap[attr].red;
  194. return (retval);
  195. }
  196. static int
  197. ofwfb_configure(int flags)
  198. {
  199. struct ofwfb_softc *sc;
  200. phandle_t chosen;
  201. ihandle_t stdout;
  202. phandle_t node;
  203. uint32_t fb_phys;
  204. int depth;
  205. int disable;
  206. int len;
  207. int i;
  208. char type[16];
  209. static int done = 0;
  210. disable = 0;
  211. TUNABLE_INT_FETCH("hw.syscons.disable", &disable);
  212. if (disable != 0)
  213. return (0);
  214. if (done != 0)
  215. return (0);
  216. done = 1;
  217. sc = &ofwfb_softc;
  218. chosen = OF_finddevice("/chosen");
  219. OF_getprop(chosen, "stdout", &stdout, sizeof(stdout));
  220. node = OF_instance_to_package(stdout);
  221. if (node == -1) {
  222. /*
  223. * The "/chosen/stdout" does not exist try
  224. * using "screen" directly.
  225. */
  226. node = OF_finddevice("screen");
  227. }
  228. OF_getprop(node, "device_type", type, sizeof(type));
  229. if (strcmp(type, "display") != 0)
  230. return (0);
  231. /* Only support 8 and 32-bit framebuffers */
  232. OF_getprop(node, "depth", &depth, sizeof(depth));
  233. if (depth == 8) {
  234. sc->sc_blank = ofwfb_blank_display8;
  235. sc->sc_putc = ofwfb_putc8;
  236. sc->sc_putm = ofwfb_putm8;
  237. sc->sc_set_border = ofwfb_set_border8;
  238. } else if (depth == 32) {
  239. sc->sc_blank = ofwfb_blank_display32;
  240. sc->sc_putc = ofwfb_putc32;
  241. sc->sc_putm = ofwfb_putm32;
  242. sc->sc_set_border = ofwfb_set_border32;
  243. } else
  244. return (0);
  245. if (OF_getproplen(node, "height") != sizeof(sc->sc_height) ||
  246. OF_getproplen(node, "width") != sizeof(sc->sc_width) ||
  247. OF_getproplen(node, "linebytes") != sizeof(sc->sc_stride))
  248. return (0);
  249. sc->sc_depth = depth;
  250. sc->sc_node = node;
  251. sc->sc_console = 1;
  252. OF_getprop(node, "height", &sc->sc_height, sizeof(sc->sc_height));
  253. OF_getprop(node, "width", &sc->sc_width, sizeof(sc->sc_width));
  254. OF_getprop(node, "linebytes", &sc->sc_stride, sizeof(sc->sc_stride));
  255. /*
  256. * Get the PCI addresses of the adapter. The node may be the
  257. * child of the PCI device: in that case, try the parent for
  258. * the assigned-addresses property.
  259. */
  260. len = OF_getprop(node, "assigned-addresses", sc->sc_pciaddrs,
  261. sizeof(sc->sc_pciaddrs));
  262. if (len == -1) {
  263. len = OF_getprop(OF_parent(node), "assigned-addresses",
  264. sc->sc_pciaddrs, sizeof(sc->sc_pciaddrs));
  265. }
  266. if (len == -1)
  267. len = 0;
  268. sc->sc_num_pciaddrs = len / sizeof(struct ofw_pci_register);
  269. /*
  270. * Grab the physical address of the framebuffer, and then map it
  271. * into our memory space. If the MMU is not yet up, it will be
  272. * remapped for us when relocation turns on.
  273. *
  274. * XXX We assume #address-cells is 1 at this point.
  275. */
  276. if (OF_getproplen(node, "address") == sizeof(fb_phys)) {
  277. OF_getprop(node, "address", &fb_phys, sizeof(fb_phys));
  278. sc->sc_tag = &bs_be_tag;
  279. bus_space_map(sc->sc_tag, fb_phys, sc->sc_height *
  280. sc->sc_stride, BUS_SPACE_MAP_PREFETCHABLE, &sc->sc_addr);
  281. } else {
  282. /*
  283. * Some IBM systems don't have an address property. Try to
  284. * guess the framebuffer region from the assigned addresses.
  285. * This is ugly, but there doesn't seem to be an alternative.
  286. * Linux does the same thing.
  287. */
  288. fb_phys = sc->sc_num_pciaddrs;
  289. for (i = 0; i < sc->sc_num_pciaddrs; i++) {
  290. /* If it is too small, not the framebuffer */
  291. if (sc->sc_pciaddrs[i].size_lo <
  292. sc->sc_stride*sc->sc_height)
  293. continue;
  294. /* If it is not memory, it isn't either */
  295. if (!(sc->sc_pciaddrs[i].phys_hi &
  296. OFW_PCI_PHYS_HI_SPACE_MEM32))
  297. continue;
  298. /* This could be the framebuffer */
  299. fb_phys = i;
  300. /* If it is prefetchable, it certainly is */
  301. if (sc->sc_pciaddrs[i].phys_hi &
  302. OFW_PCI_PHYS_HI_PREFETCHABLE)
  303. break;
  304. }
  305. if (fb_phys == sc->sc_num_pciaddrs)
  306. return (0);
  307. OF_decode_addr(node, fb_phys, &sc->sc_tag, &sc->sc_addr, NULL);
  308. }
  309. ofwfb_init(0, &sc->sc_va, 0);
  310. return (0);
  311. }
  312. static int
  313. ofwfb_probe(int unit, video_adapter_t **adp, void *arg, int flags)
  314. {
  315. TODO;
  316. return (0);
  317. }
  318. static int
  319. ofwfb_init(int unit, video_adapter_t *adp, int flags)
  320. {
  321. struct ofwfb_softc *sc;
  322. video_info_t *vi;
  323. int cborder;
  324. int font_height;
  325. sc = (struct ofwfb_softc *)adp;
  326. vi = &adp->va_info;
  327. vid_init_struct(adp, "ofwfb", -1, unit);
  328. /* The default font size can be overridden by loader */
  329. font_height = 16;
  330. TUNABLE_INT_FETCH("hw.syscons.fsize", &font_height);
  331. if (font_height == 8) {
  332. sc->sc_font = dflt_font_8;
  333. sc->sc_font_height = 8;
  334. } else if (font_height == 14) {
  335. sc->sc_font = dflt_font_14;
  336. sc->sc_font_height = 14;
  337. } else {
  338. /* default is 8x16 */
  339. sc->sc_font = dflt_font_16;
  340. sc->sc_font_height = 16;
  341. }
  342. /* The user can set a border in chars - default is 1 char width */
  343. cborder = 1;
  344. TUNABLE_INT_FETCH("hw.syscons.border", &cborder);
  345. vi->vi_cheight = sc->sc_font_height;
  346. vi->vi_width = sc->sc_width/8 - 2*cborder;
  347. vi->vi_height = sc->sc_height/sc->sc_font_height - 2*cborder;
  348. vi->vi_cwidth = 8;
  349. /*
  350. * Clamp width/height to syscons maximums
  351. */
  352. if (vi->vi_width > COL)
  353. vi->vi_width = COL;
  354. if (vi->vi_height > ROW)
  355. vi->vi_height = ROW;
  356. sc->sc_xmargin = (sc->sc_width - (vi->vi_width * vi->vi_cwidth)) / 2;
  357. sc->sc_ymargin = (sc->sc_height - (vi->vi_height * vi->vi_cheight))/2;
  358. /*
  359. * Avoid huge amounts of conditional code in syscons by
  360. * defining a dummy h/w text display buffer.
  361. */
  362. adp->va_window = (vm_offset_t) ofwfb_static_window;
  363. /*
  364. * Enable future font-loading and flag color support, as well as
  365. * adding V_ADP_MODECHANGE so that we ofwfb_set_mode() gets called
  366. * when the X server shuts down. This enables us to get the console
  367. * back when X disappears.
  368. */
  369. adp->va_flags |= V_ADP_FONT | V_ADP_COLOR | V_ADP_MODECHANGE;
  370. ofwfb_set_mode(&sc->sc_va, 0);
  371. vid_register(&sc->sc_va);
  372. return (0);
  373. }
  374. static int
  375. ofwfb_get_info(video_adapter_t *adp, int mode, video_info_t *info)
  376. {
  377. bcopy(&adp->va_info, info, sizeof(*info));
  378. return (0);
  379. }
  380. static int
  381. ofwfb_query_mode(video_adapter_t *adp, video_info_t *info)
  382. {
  383. TODO;
  384. return (0);
  385. }
  386. static int
  387. ofwfb_set_mode(video_adapter_t *adp, int mode)
  388. {
  389. struct ofwfb_softc *sc;
  390. char name[64];
  391. ihandle_t ih;
  392. int i, retval;
  393. sc = (struct ofwfb_softc *)adp;
  394. if (ofwfb_reset_on_switch) {
  395. /*
  396. * Open the display device, which will initialize it.
  397. */
  398. memset(name, 0, sizeof(name));
  399. OF_package_to_path(sc->sc_node, name, sizeof(name));
  400. ih = OF_open(name);
  401. if (sc->sc_depth == 8) {
  402. /*
  403. * Install the ISO6429 colormap - older OFW systems
  404. * don't do this by default
  405. */
  406. for (i = 0; i < 16; i++) {
  407. OF_call_method("color!", ih, 4, 1,
  408. ofwfb_cmap[i].red,
  409. ofwfb_cmap[i].green,
  410. ofwfb_cmap[i].blue,
  411. i,
  412. &retval);
  413. }
  414. }
  415. }
  416. ofwfb_blank_display(&sc->sc_va, V_DISPLAY_ON);
  417. return (0);
  418. }
  419. static int
  420. ofwfb_save_font(video_adapter_t *adp, int page, int size, int width,
  421. u_char *data, int c, int count)
  422. {
  423. TODO;
  424. return (0);
  425. }
  426. static int
  427. ofwfb_load_font(video_adapter_t *adp, int page, int size, int width,
  428. u_char *data, int c, int count)
  429. {
  430. struct ofwfb_softc *sc;
  431. sc = (struct ofwfb_softc *)adp;
  432. /*
  433. * syscons code has already determined that current width/height
  434. * are unchanged for this new font
  435. */
  436. sc->sc_font = data;
  437. return (0);
  438. }
  439. static int
  440. ofwfb_show_font(video_adapter_t *adp, int page)
  441. {
  442. return (0);
  443. }
  444. static int
  445. ofwfb_save_palette(video_adapter_t *adp, u_char *palette)
  446. {
  447. /* TODO; */
  448. return (0);
  449. }
  450. static int
  451. ofwfb_load_palette(video_adapter_t *adp, u_char *palette)
  452. {
  453. /* TODO; */
  454. return (0);
  455. }
  456. static int
  457. ofwfb_set_border8(video_adapter_t *adp, int border)
  458. {
  459. struct ofwfb_softc *sc;
  460. int i, j;
  461. uint8_t *addr;
  462. uint8_t bground;
  463. sc = (struct ofwfb_softc *)adp;
  464. bground = ofwfb_background(border);
  465. /* Set top margin */
  466. addr = (uint8_t *) sc->sc_addr;
  467. for (i = 0; i < sc->sc_ymargin; i++) {
  468. for (j = 0; j < sc->sc_width; j++) {
  469. *(addr + j) = bground;
  470. }
  471. addr += sc->sc_stride;
  472. }
  473. /* bottom margin */
  474. addr = (uint8_t *) sc->sc_addr + (sc->sc_height - sc->sc_ymargin)*sc->sc_stride;
  475. for (i = 0; i < sc->sc_ymargin; i++) {
  476. for (j = 0; j < sc->sc_width; j++) {
  477. *(addr + j) = bground;
  478. }
  479. addr += sc->sc_stride;
  480. }
  481. /* remaining left and right borders */
  482. addr = (uint8_t *) sc->sc_addr + sc->sc_ymargin*sc->sc_stride;
  483. for (i = 0; i < sc->sc_height - 2*sc->sc_xmargin; i++) {
  484. for (j = 0; j < sc->sc_xmargin; j++) {
  485. *(addr + j) = bground;
  486. *(addr + j + sc->sc_width - sc->sc_xmargin) = bground;
  487. }
  488. addr += sc->sc_stride;
  489. }
  490. return (0);
  491. }
  492. static int
  493. ofwfb_set_border32(video_adapter_t *adp, int border)
  494. {
  495. /* XXX Be lazy for now and blank entire screen */
  496. return (ofwfb_blank_display32(adp, border));
  497. }
  498. static int
  499. ofwfb_set_border(video_adapter_t *adp, int border)
  500. {
  501. struct ofwfb_softc *sc;
  502. sc = (struct ofwfb_softc *)adp;
  503. return ((*sc->sc_set_border)(adp, border));
  504. }
  505. static int
  506. ofwfb_save_state(video_adapter_t *adp, void *p, size_t size)
  507. {
  508. TODO;
  509. return (0);
  510. }
  511. static int
  512. ofwfb_load_state(video_adapter_t *adp, void *p)
  513. {
  514. TODO;
  515. return (0);
  516. }
  517. static int
  518. ofwfb_set_win_org(video_adapter_t *adp, off_t offset)
  519. {
  520. TODO;
  521. return (0);
  522. }
  523. static int
  524. ofwfb_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
  525. {
  526. *col = 0;
  527. *row = 0;
  528. return (0);
  529. }
  530. static int
  531. ofwfb_set_hw_cursor(video_adapter_t *adp, int col, int row)
  532. {
  533. return (0);
  534. }
  535. static int
  536. ofwfb_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
  537. int celsize, int blink)
  538. {
  539. return (0);
  540. }
  541. static int
  542. ofwfb_blank_display8(video_adapter_t *adp, int mode)
  543. {
  544. struct ofwfb_softc *sc;
  545. int i;
  546. uint32_t *addr;
  547. uint32_t color;
  548. uint32_t end;
  549. sc = (struct ofwfb_softc *)adp;
  550. addr = (uint32_t *) sc->sc_addr;
  551. end = (sc->sc_stride/4) * sc->sc_height;
  552. /* Splat 4 pixels at once. */
  553. color = (ofwfb_background(SC_NORM_ATTR) << 24) |
  554. (ofwfb_background(SC_NORM_ATTR) << 16) |
  555. (ofwfb_background(SC_NORM_ATTR) << 8) |
  556. (ofwfb_background(SC_NORM_ATTR));
  557. for (i = 0; i < end; i++)
  558. *(addr + i) = color;
  559. return (0);
  560. }
  561. static int
  562. ofwfb_blank_display32(video_adapter_t *adp, int mode)
  563. {
  564. struct ofwfb_softc *sc;
  565. int i;
  566. uint32_t *addr, blank;
  567. sc = (struct ofwfb_softc *)adp;
  568. addr = (uint32_t *) sc->sc_addr;
  569. blank = ofwfb_pix32(sc, ofwfb_background(SC_NORM_ATTR));
  570. for (i = 0; i < (sc->sc_stride/4)*sc->sc_height; i++)
  571. *(addr + i) = blank;
  572. return (0);
  573. }
  574. static int
  575. ofwfb_blank_display(video_adapter_t *adp, int mode)
  576. {
  577. struct ofwfb_softc *sc;
  578. sc = (struct ofwfb_softc *)adp;
  579. return ((*sc->sc_blank)(adp, mode));
  580. }
  581. static int
  582. ofwfb_mmap(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
  583. int prot, vm_memattr_t *memattr)
  584. {
  585. struct ofwfb_softc *sc;
  586. int i;
  587. sc = (struct ofwfb_softc *)adp;
  588. /*
  589. * Make sure the requested address lies within the PCI device's
  590. * assigned addrs
  591. */
  592. for (i = 0; i < sc->sc_num_pciaddrs; i++)
  593. if (offset >= sc->sc_pciaddrs[i].phys_lo &&
  594. offset < (sc->sc_pciaddrs[i].phys_lo + sc->sc_pciaddrs[i].size_lo))
  595. {
  596. /*
  597. * If this is a prefetchable BAR, we can (and should)
  598. * enable write-combining.
  599. */
  600. if (sc->sc_pciaddrs[i].phys_hi &
  601. OFW_PCI_PHYS_HI_PREFETCHABLE)
  602. *memattr = VM_MEMATTR_WRITE_COMBINING;
  603. *paddr = offset;
  604. return (0);
  605. }
  606. /*
  607. * Hack for Radeon...
  608. */
  609. if (ofwfb_ignore_mmap_checks) {
  610. *paddr = offset;
  611. return (0);
  612. }
  613. /*
  614. * This might be a legacy VGA mem request: if so, just point it at the
  615. * framebuffer, since it shouldn't be touched
  616. */
  617. if (offset < sc->sc_stride*sc->sc_height) {
  618. *paddr = sc->sc_addr + offset;
  619. return (0);
  620. }
  621. /*
  622. * Error if we didn't have a better idea.
  623. */
  624. if (sc->sc_num_pciaddrs == 0)
  625. return (ENOMEM);
  626. return (EINVAL);
  627. }
  628. static int
  629. ofwfb_ioctl(video_adapter_t *adp, u_long cmd, caddr_t data)
  630. {
  631. return (0);
  632. }
  633. static int
  634. ofwfb_clear(video_adapter_t *adp)
  635. {
  636. TODO;
  637. return (0);
  638. }
  639. static int
  640. ofwfb_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
  641. {
  642. TODO;
  643. return (0);
  644. }
  645. static int
  646. ofwfb_bitblt(video_adapter_t *adp, ...)
  647. {
  648. TODO;
  649. return (0);
  650. }
  651. static int
  652. ofwfb_diag(video_adapter_t *adp, int level)
  653. {
  654. TODO;
  655. return (0);
  656. }
  657. static int
  658. ofwfb_save_cursor_palette(video_adapter_t *adp, u_char *palette)
  659. {
  660. TODO;
  661. return (0);
  662. }
  663. static int
  664. ofwfb_load_cursor_palette(video_adapter_t *adp, u_char *palette)
  665. {
  666. TODO;
  667. return (0);
  668. }
  669. static int
  670. ofwfb_copy(video_adapter_t *adp, vm_offset_t src, vm_offset_t dst, int n)
  671. {
  672. TODO;
  673. return (0);
  674. }
  675. static int
  676. ofwfb_putp(video_adapter_t *adp, vm_offset_t off, uint32_t p, uint32_t a,
  677. int size, int bpp, int bit_ltor, int byte_ltor)
  678. {
  679. TODO;
  680. return (0);
  681. }
  682. static int
  683. ofwfb_putc8(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
  684. {
  685. struct ofwfb_softc *sc;
  686. int row;
  687. int col;
  688. int i;
  689. uint32_t *addr;
  690. u_char *p, fg, bg;
  691. union {
  692. uint32_t l;
  693. uint8_t c[4];
  694. } ch1, ch2;
  695. sc = (struct ofwfb_softc *)adp;
  696. row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
  697. col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
  698. p = sc->sc_font + c*sc->sc_font_height;
  699. addr = (u_int32_t *)((uintptr_t)sc->sc_addr
  700. + (row + sc->sc_ymargin)*sc->sc_stride
  701. + col + sc->sc_xmargin);
  702. fg = ofwfb_foreground(a);
  703. bg = ofwfb_background(a);
  704. for (i = 0; i < sc->sc_font_height; i++) {
  705. u_char fline = p[i];
  706. /*
  707. * Assume that there is more background than foreground
  708. * in characters and init accordingly
  709. */
  710. ch1.l = ch2.l = (bg << 24) | (bg << 16) | (bg << 8) | bg;
  711. /*
  712. * Calculate 2 x 4-chars at a time, and then
  713. * write these out.
  714. */
  715. if (fline & 0x80) ch1.c[0] = fg;
  716. if (fline & 0x40) ch1.c[1] = fg;
  717. if (fline & 0x20) ch1.c[2] = fg;
  718. if (fline & 0x10) ch1.c[3] = fg;
  719. if (fline & 0x08) ch2.c[0] = fg;
  720. if (fline & 0x04) ch2.c[1] = fg;
  721. if (fline & 0x02) ch2.c[2] = fg;
  722. if (fline & 0x01) ch2.c[3] = fg;
  723. addr[0] = ch1.l;
  724. addr[1] = ch2.l;
  725. addr += (sc->sc_stride / sizeof(u_int32_t));
  726. }
  727. return (0);
  728. }
  729. static int
  730. ofwfb_putc32(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
  731. {
  732. struct ofwfb_softc *sc;
  733. int row;
  734. int col;
  735. int i, j, k;
  736. uint32_t *addr, fg, bg;
  737. u_char *p;
  738. sc = (struct ofwfb_softc *)adp;
  739. row = (off / adp->va_info.vi_width) * adp->va_info.vi_cheight;
  740. col = (off % adp->va_info.vi_width) * adp->va_info.vi_cwidth;
  741. p = sc->sc_font + c*sc->sc_font_height;
  742. addr = (uint32_t *)sc->sc_addr
  743. + (row + sc->sc_ymargin)*(sc->sc_stride/4)
  744. + col + sc->sc_xmargin;
  745. fg = ofwfb_pix32(sc, ofwfb_foreground(a));
  746. bg = ofwfb_pix32(sc, ofwfb_background(a));
  747. for (i = 0; i < sc->sc_font_height; i++) {
  748. for (j = 0, k = 7; j < 8; j++, k--) {
  749. if ((p[i] & (1 << k)) == 0)
  750. *(addr + j) = bg;
  751. else
  752. *(addr + j) = fg;
  753. }
  754. addr += (sc->sc_stride/4);
  755. }
  756. return (0);
  757. }
  758. static int
  759. ofwfb_putc(video_adapter_t *adp, vm_offset_t off, uint8_t c, uint8_t a)
  760. {
  761. struct ofwfb_softc *sc;
  762. sc = (struct ofwfb_softc *)adp;
  763. return ((*sc->sc_putc)(adp, off, c, a));
  764. }
  765. static int
  766. ofwfb_puts(video_adapter_t *adp, vm_offset_t off, u_int16_t *s, int len)
  767. {
  768. int i;
  769. for (i = 0; i < len; i++) {
  770. ofwfb_putc(adp, off + i, s[i] & 0xff, (s[i] & 0xff00) >> 8);
  771. }
  772. return (0);
  773. }
  774. static int
  775. ofwfb_putm(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
  776. uint32_t pixel_mask, int size, int width)
  777. {
  778. struct ofwfb_softc *sc;
  779. sc = (struct ofwfb_softc *)adp;
  780. return ((*sc->sc_putm)(adp, x, y, pixel_image, pixel_mask, size,
  781. width));
  782. }
  783. static int
  784. ofwfb_putm8(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
  785. uint32_t pixel_mask, int size, int width)
  786. {
  787. struct ofwfb_softc *sc;
  788. int i, j, k;
  789. uint8_t *addr;
  790. u_char fg, bg;
  791. sc = (struct ofwfb_softc *)adp;
  792. addr = (u_int8_t *)((uintptr_t)sc->sc_addr
  793. + (y + sc->sc_ymargin)*sc->sc_stride
  794. + x + sc->sc_xmargin);
  795. fg = ofwfb_foreground(SC_NORM_ATTR);
  796. bg = ofwfb_background(SC_NORM_ATTR);
  797. for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
  798. /*
  799. * Calculate 2 x 4-chars at a time, and then
  800. * write these out.
  801. */
  802. for (j = 0, k = width; j < 8; j++, k--) {
  803. if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
  804. continue;
  805. if (pixel_image[i] & (1 << k))
  806. addr[j] = (addr[j] == fg) ? bg : fg;
  807. }
  808. addr += (sc->sc_stride / sizeof(u_int8_t));
  809. }
  810. return (0);
  811. }
  812. static int
  813. ofwfb_putm32(video_adapter_t *adp, int x, int y, uint8_t *pixel_image,
  814. uint32_t pixel_mask, int size, int width)
  815. {
  816. struct ofwfb_softc *sc;
  817. int i, j, k;
  818. uint32_t fg, bg;
  819. uint32_t *addr;
  820. sc = (struct ofwfb_softc *)adp;
  821. addr = (uint32_t *)sc->sc_addr
  822. + (y + sc->sc_ymargin)*(sc->sc_stride/4)
  823. + x + sc->sc_xmargin;
  824. fg = ofwfb_pix32(sc, ofwfb_foreground(SC_NORM_ATTR));
  825. bg = ofwfb_pix32(sc, ofwfb_background(SC_NORM_ATTR));
  826. for (i = 0; i < size && i+y < sc->sc_height - 2*sc->sc_ymargin; i++) {
  827. for (j = 0, k = width; j < 8; j++, k--) {
  828. if (x + j >= sc->sc_width - 2*sc->sc_xmargin)
  829. continue;
  830. if (pixel_image[i] & (1 << k))
  831. *(addr + j) = (*(addr + j) == fg) ? bg : fg;
  832. }
  833. addr += (sc->sc_stride/4);
  834. }
  835. return (0);
  836. }
  837. /*
  838. * Define the syscons nexus device attachment
  839. */
  840. static void
  841. ofwfb_scidentify(driver_t *driver, device_t parent)
  842. {
  843. device_t child;
  844. /*
  845. * Add with a priority guaranteed to make it last on
  846. * the device list
  847. */
  848. child = BUS_ADD_CHILD(parent, INT_MAX, SC_DRIVER_NAME, 0);
  849. }
  850. static int
  851. ofwfb_scprobe(device_t dev)
  852. {
  853. int error;
  854. device_set_desc(dev, "System console");
  855. error = sc_probe_unit(device_get_unit(dev),
  856. device_get_flags(dev) | SC_AUTODETECT_KBD);
  857. if (error != 0)
  858. return (error);
  859. /* This is a fake device, so make sure we added it ourselves */
  860. return (BUS_PROBE_NOWILDCARD);
  861. }
  862. static int
  863. ofwfb_scattach(device_t dev)
  864. {
  865. return (sc_attach_unit(device_get_unit(dev),
  866. device_get_flags(dev) | SC_AUTODETECT_KBD));
  867. }
  868. static device_method_t ofwfb_sc_methods[] = {
  869. DEVMETHOD(device_identify, ofwfb_scidentify),
  870. DEVMETHOD(device_probe, ofwfb_scprobe),
  871. DEVMETHOD(device_attach, ofwfb_scattach),
  872. { 0, 0 }
  873. };
  874. static driver_t ofwfb_sc_driver = {
  875. SC_DRIVER_NAME,
  876. ofwfb_sc_methods,
  877. sizeof(sc_softc_t),
  878. };
  879. DRIVER_MODULE(ofwfb, nexus, ofwfb_sc_driver, 0, 0);
  880. /*
  881. * Utility routines from <dev/fb/fbreg.h>
  882. */
  883. void
  884. ofwfb_bcopy(const void *s, void *d, size_t c)
  885. {
  886. bcopy(s, d, c);
  887. }
  888. void
  889. ofwfb_bzero(void *d, size_t c)
  890. {
  891. bzero(d, c);
  892. }
  893. void
  894. ofwfb_fillw(int pat, void *base, size_t cnt)
  895. {
  896. u_int16_t *bptr = base;
  897. while (cnt--)
  898. *bptr++ = pat;
  899. }
  900. u_int16_t
  901. ofwfb_readw(u_int16_t *addr)
  902. {
  903. return (*addr);
  904. }
  905. void
  906. ofwfb_writew(u_int16_t *addr, u_int16_t val)
  907. {
  908. *addr = val;
  909. }