gbe.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /* $OpenBSD: gbe.c,v 1.21 2013/10/21 10:36:16 miod Exp $ */
  2. /*
  3. * Copyright (c) 2007, 2008, 2009 Joel Sing <jsing@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Graphics Back End (GBE) Framebuffer for SGI O2.
  19. */
  20. #include <sys/param.h>
  21. #include <sys/systm.h>
  22. #include <sys/device.h>
  23. #include <sys/malloc.h>
  24. #include <uvm/uvm_extern.h>
  25. #include <machine/autoconf.h>
  26. #include <machine/bus.h>
  27. #include <mips64/arcbios.h>
  28. #include <sgi/dev/gl.h>
  29. #include <sgi/localbus/crimebus.h>
  30. #include <sgi/localbus/macebusvar.h>
  31. #include <dev/wscons/wsconsio.h>
  32. #include <dev/wscons/wsdisplayvar.h>
  33. #include <dev/rasops/rasops.h>
  34. #include "gbereg.h"
  35. /* Amount of memory to allocate for framebuffer. */
  36. #define GBE_FB_SIZE 8 * 1 << 20
  37. /*
  38. * Colourmap data.
  39. */
  40. struct gbe_cmap {
  41. u_int8_t cm_red[256];
  42. u_int8_t cm_green[256];
  43. u_int8_t cm_blue[256];
  44. };
  45. /*
  46. * Screen data.
  47. */
  48. struct gbe_screen {
  49. struct device *sc; /* Back pointer. */
  50. struct rasops_info ri; /* Screen raster display info. */
  51. struct rasops_info ri_tile; /* Raster info for rasops tile. */
  52. struct gbe_cmap cmap; /* Display colour map. */
  53. int fb_size; /* Size of framebuffer memory. */
  54. int tm_size; /* Size of tilemap memory. */
  55. caddr_t tm; /* Address of tilemap memory. */
  56. paddr_t tm_phys; /* Physical address of tilemap. */
  57. caddr_t fb; /* Address of framebuffer memory. */
  58. paddr_t fb_phys; /* Physical address of framebuffer. */
  59. caddr_t ro; /* Address of rasops tile. */
  60. paddr_t ro_phys; /* Physical address of rasops tile. */
  61. int width; /* Width in pixels. */
  62. int height; /* Height in pixels. */
  63. int depth; /* Colour depth in bits. */
  64. int mode; /* Display mode. */
  65. int bufmode; /* Rendering engine buffer mode. */
  66. int linebytes; /* Bytes per line. */
  67. int ro_curpos; /* Current position in rasops tile. */
  68. };
  69. /*
  70. * GBE device data.
  71. */
  72. struct gbe_softc {
  73. struct device sc_dev;
  74. bus_space_tag_t iot;
  75. bus_space_handle_t ioh; /* GBE registers. */
  76. bus_space_handle_t re_ioh; /* Rendering engine registers. */
  77. bus_dma_tag_t dmat;
  78. int rev; /* Hardware revision. */
  79. int console; /* Is this the console? */
  80. int screens; /* No of screens allocated. */
  81. struct gbe_screen *curscr; /* Current screen. */
  82. };
  83. /*
  84. * Hardware and device related functions.
  85. */
  86. void gbe_init_screen(struct gbe_screen *);
  87. void gbe_enable(struct gbe_softc *);
  88. void gbe_disable(struct gbe_softc *);
  89. void gbe_setup(struct gbe_softc *);
  90. void gbe_setcolour(struct gbe_softc *, u_int, u_int8_t, u_int8_t, u_int8_t);
  91. void gbe_wait_re_idle(struct gbe_softc *);
  92. /*
  93. * Colour map handling for indexed modes.
  94. */
  95. int gbe_getcmap(struct gbe_cmap *, struct wsdisplay_cmap *);
  96. int gbe_putcmap(struct gbe_cmap *, struct wsdisplay_cmap *);
  97. void gbe_loadcmap(struct gbe_screen *, u_int, u_int);
  98. /*
  99. * Interfaces for wscons.
  100. */
  101. int gbe_ioctl(void *, u_long, caddr_t, int, struct proc *);
  102. paddr_t gbe_mmap(void *, off_t, int);
  103. int gbe_alloc_screen(void *, const struct wsscreen_descr *, void **,
  104. int *, int *, long *);
  105. void gbe_free_screen(void *, void *);
  106. int gbe_show_screen(void *, void *, int, void (*)(void *, int, int),
  107. void *);
  108. int gbe_load_font(void *, void *, struct wsdisplay_font *);
  109. int gbe_list_font(void *, struct wsdisplay_font *);
  110. /*
  111. * Hardware acceleration for rasops.
  112. */
  113. void gbe_rop(struct gbe_softc *, int, int, int, int, int);
  114. void gbe_copyrect(struct gbe_softc *, int, int, int, int, int, int, int);
  115. void gbe_fillrect(struct gbe_softc *, int, int, int, int, int);
  116. int gbe_do_cursor(struct rasops_info *);
  117. int gbe_putchar(void *, int, int, u_int, long);
  118. int gbe_copycols(void *, int, int, int, int);
  119. int gbe_erasecols(void *, int, int, int, long);
  120. int gbe_copyrows(void *, int, int, int);
  121. int gbe_eraserows(void *, int, int, long);
  122. static struct gbe_screen gbe_consdata;
  123. static int gbe_console;
  124. struct wsscreen_descr gbe_stdscreen = {
  125. "std", /* Screen name. */
  126. };
  127. struct wsdisplay_accessops gbe_accessops = {
  128. .ioctl = gbe_ioctl,
  129. .mmap = gbe_mmap,
  130. .alloc_screen = gbe_alloc_screen,
  131. .free_screen = gbe_free_screen,
  132. .show_screen = gbe_show_screen,
  133. .load_font = gbe_load_font,
  134. .list_font = gbe_list_font
  135. };
  136. const struct wsscreen_descr *gbe_scrlist[] = {
  137. &gbe_stdscreen
  138. };
  139. struct wsscreen_list gbe_screenlist = {
  140. sizeof(gbe_scrlist) / sizeof(struct wsscreen_descr *), gbe_scrlist
  141. };
  142. int gbe_match(struct device *, void *, void *);
  143. void gbe_attach(struct device *, struct device *, void *);
  144. int gbe_activate(struct device *, int);
  145. struct cfattach gbe_ca = {
  146. sizeof (struct gbe_softc), gbe_match, gbe_attach,
  147. NULL, gbe_activate
  148. };
  149. struct cfdriver gbe_cd = {
  150. NULL, "gbe", DV_DULL
  151. };
  152. int
  153. gbe_match(struct device *parent, void *cf, void *aux)
  154. {
  155. struct mainbus_attach_args *maa = aux;
  156. if (strcmp(maa->maa_name, gbe_cd.cd_name) != 0)
  157. return 0;
  158. return 1;
  159. }
  160. void
  161. gbe_attach(struct device *parent, struct device *self, void *aux)
  162. {
  163. struct gbe_softc *gsc = (void*)self;
  164. struct gbe_screen *screen;
  165. struct wsemuldisplaydev_attach_args waa;
  166. bus_dma_segment_t tm_segs[1];
  167. bus_dma_segment_t fb_segs[1];
  168. bus_dma_segment_t ro_segs[1];
  169. bus_dmamap_t tm_dmamap;
  170. bus_dmamap_t fb_dmamap;
  171. bus_dmamap_t ro_dmamap;
  172. int tm_nsegs;
  173. int fb_nsegs;
  174. int ro_nsegs;
  175. uint32_t val;
  176. long attr;
  177. printf(": ");
  178. /* GBE isn't strictly on the crimebus, but use this for now... */
  179. gsc->iot = &crimebus_tag;
  180. gsc->dmat = &mace_bus_dma_tag;
  181. gsc->console = gbe_console;
  182. gsc->screens = 0;
  183. if (gsc->console == 1) {
  184. /*
  185. * We've already been setup via gbe_cnattach().
  186. */
  187. gsc->ioh = PHYS_TO_XKPHYS(GBE_BASE, CCA_NC);
  188. gsc->re_ioh = PHYS_TO_XKPHYS(RE_BASE, CCA_NC);
  189. gsc->rev = bus_space_read_4(gsc->iot, gsc->ioh, GBE_CTRL_STAT)
  190. & 0xf;
  191. gsc->curscr = &gbe_consdata;
  192. gbe_consdata.sc = (void*)self;
  193. printf("rev %u, %iMB, %dx%d at %d bits\n", gsc->rev,
  194. gbe_consdata.fb_size >> 20, gbe_consdata.width,
  195. gbe_consdata.height, gbe_consdata.depth);
  196. waa.console = gsc->console;
  197. waa.scrdata = &gbe_screenlist;
  198. waa.accessops = &gbe_accessops;
  199. waa.accesscookie = &gbe_consdata;
  200. waa.defaultscreens = 0;
  201. config_found(self, &waa, wsemuldisplaydevprint);
  202. return;
  203. }
  204. /*
  205. * Setup screen data.
  206. */
  207. gsc->curscr = malloc(sizeof(struct gbe_screen), M_DEVBUF, M_NOWAIT);
  208. if (gsc->curscr == NULL) {
  209. printf("failed to allocate screen memory!\n");
  210. return;
  211. }
  212. gsc->curscr->sc = (void *)gsc;
  213. screen = gsc->curscr;
  214. /*
  215. * Setup bus space mappings.
  216. */
  217. if (bus_space_map(gsc->iot, GBE_BASE - CRIMEBUS_BASE, GBE_REG_SIZE,
  218. BUS_SPACE_MAP_LINEAR, &gsc->ioh)) {
  219. printf("failed to map framebuffer bus space!\n");
  220. return;
  221. }
  222. if (bus_space_map(gsc->iot, RE_BASE - CRIMEBUS_BASE, RE_REG_SIZE,
  223. BUS_SPACE_MAP_LINEAR, &gsc->re_ioh)) {
  224. printf("failed to map rendering engine bus space!\n");
  225. goto fail0;
  226. }
  227. /* Determine GBE revision. */
  228. gsc->rev = bus_space_read_4(gsc->iot, gsc->ioh, GBE_CTRL_STAT) & 0xf;
  229. /* Determine resolution configured by firmware. */
  230. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_HCMAP);
  231. screen->width = (val >> GBE_VT_HCMAP_ON_SHIFT) & 0xfff;
  232. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_VCMAP);
  233. screen->height = (val >> GBE_VT_VCMAP_ON_SHIFT) & 0xfff;
  234. if (screen->width == 0 || screen->height == 0) {
  235. printf("device has not been setup by firmware!\n");
  236. goto fail1;
  237. }
  238. /* Setup screen defaults. */
  239. screen->fb_size = GBE_FB_SIZE;
  240. screen->tm_size = GBE_TLB_SIZE * sizeof(uint16_t);
  241. screen->depth = 8;
  242. screen->linebytes = screen->width * screen->depth / 8;
  243. /*
  244. * Setup DMA for tilemap.
  245. */
  246. if (bus_dmamap_create(gsc->dmat, screen->tm_size, 1, screen->tm_size,
  247. 0, BUS_DMA_NOWAIT, &tm_dmamap)) {
  248. printf("failed to create DMA map for tilemap!\n");
  249. goto fail1;
  250. }
  251. if (bus_dmamem_alloc(gsc->dmat, screen->tm_size, 65536, 0, tm_segs, 1,
  252. &tm_nsegs, BUS_DMA_NOWAIT)) {
  253. printf("failed to allocate DMA memory for tilemap!\n");
  254. goto fail2;
  255. }
  256. if (bus_dmamem_map(gsc->dmat, tm_segs, tm_nsegs, screen->tm_size,
  257. &screen->tm, BUS_DMA_COHERENT)) {
  258. printf("failed to map DMA memory for tilemap!\n");
  259. goto fail3;
  260. }
  261. if (bus_dmamap_load(gsc->dmat, tm_dmamap, screen->tm, screen->tm_size,
  262. NULL, BUS_DMA_NOWAIT)){
  263. printf("failed to load DMA map for tilemap\n");
  264. goto fail4;
  265. }
  266. /*
  267. * Setup DMA for framebuffer.
  268. */
  269. if (bus_dmamap_create(gsc->dmat, screen->fb_size, 1, screen->fb_size,
  270. 0, BUS_DMA_NOWAIT, &fb_dmamap)) {
  271. printf("failed to create DMA map for framebuffer!\n");
  272. goto fail5;
  273. }
  274. if (bus_dmamem_alloc(gsc->dmat, screen->fb_size, 65536, 0, fb_segs,
  275. 1, &fb_nsegs, BUS_DMA_NOWAIT)) {
  276. printf("failed to allocate DMA memory for framebuffer!\n");
  277. goto fail6;
  278. }
  279. if (bus_dmamem_map(gsc->dmat, fb_segs, fb_nsegs, screen->fb_size,
  280. &screen->fb, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) {
  281. printf("failed to map DMA memory for framebuffer!\n");
  282. goto fail7;
  283. }
  284. if (bus_dmamap_load(gsc->dmat, fb_dmamap, screen->fb, screen->fb_size,
  285. NULL, BUS_DMA_NOWAIT)) {
  286. printf("failed to load DMA map for framebuffer\n");
  287. goto fail8;
  288. }
  289. /*
  290. * Setup DMA for rasops tile.
  291. */
  292. if (bus_dmamap_create(gsc->dmat, GBE_TILE_SIZE, 1, GBE_TILE_SIZE,
  293. 0, BUS_DMA_NOWAIT, &ro_dmamap)) {
  294. printf("failed to create DMA map for rasops tile!\n");
  295. goto fail9;
  296. }
  297. if (bus_dmamem_alloc(gsc->dmat, GBE_TILE_SIZE, 65536, 0, ro_segs,
  298. 1, &ro_nsegs, BUS_DMA_NOWAIT)) {
  299. printf("failed to allocate DMA memory for rasops tile!\n");
  300. goto fail10;
  301. }
  302. if (bus_dmamem_map(gsc->dmat, ro_segs, ro_nsegs, GBE_TILE_SIZE,
  303. &screen->ro, BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) {
  304. printf("failed to map DMA memory for rasops tile!\n");
  305. goto fail11;
  306. }
  307. if (bus_dmamap_load(gsc->dmat, ro_dmamap, screen->ro, GBE_TILE_SIZE,
  308. NULL, BUS_DMA_NOWAIT)) {
  309. printf("failed to load DMA map for rasops tile\n");
  310. goto fail12;
  311. }
  312. screen->tm_phys = tm_dmamap->dm_segs[0].ds_addr;
  313. screen->fb_phys = fb_dmamap->dm_segs[0].ds_addr;
  314. screen->ro_phys = ro_dmamap->dm_segs[0].ds_addr;
  315. gbe_init_screen(screen);
  316. gbe_disable(gsc);
  317. gbe_setup(gsc);
  318. gbe_enable(gsc);
  319. /* Load colourmap if required. */
  320. if (screen->depth == 8)
  321. gbe_loadcmap(screen, 0, 255);
  322. /* Clear framebuffer. */
  323. gbe_fillrect(gsc, 0, 0, screen->width, screen->height, 0);
  324. printf("rev %u, %iMB, %dx%d at %d bits\n", gsc->rev,
  325. screen->fb_size >> 20, screen->width, screen->height,
  326. screen->depth);
  327. /*
  328. * Attach wsdisplay.
  329. */
  330. /* Attach as console if necessary. */
  331. if (strncmp(bios_console, "video", 5) == 0) {
  332. screen->ri.ri_ops.alloc_attr(&screen->ri, 0, 0, 0, &attr);
  333. wsdisplay_cnattach(&gbe_stdscreen, &screen->ri, 0, 0, attr);
  334. gsc->console = 1;
  335. }
  336. waa.console = gsc->console;
  337. waa.scrdata = &gbe_screenlist;
  338. waa.accessops = &gbe_accessops;
  339. waa.accesscookie = screen;
  340. waa.defaultscreens = 0;
  341. config_found(self, &waa, wsemuldisplaydevprint);
  342. return;
  343. fail12:
  344. bus_dmamem_unmap(gsc->dmat, screen->ro, GBE_TILE_SIZE);
  345. fail11:
  346. bus_dmamem_free(gsc->dmat, ro_segs, ro_nsegs);
  347. fail10:
  348. bus_dmamap_destroy(gsc->dmat, ro_dmamap);
  349. fail9:
  350. bus_dmamap_unload(gsc->dmat, fb_dmamap);
  351. fail8:
  352. bus_dmamem_unmap(gsc->dmat, screen->fb, screen->fb_size);
  353. fail7:
  354. bus_dmamem_free(gsc->dmat, fb_segs, fb_nsegs);
  355. fail6:
  356. bus_dmamap_destroy(gsc->dmat, fb_dmamap);
  357. fail5:
  358. bus_dmamap_unload(gsc->dmat, tm_dmamap);
  359. fail4:
  360. bus_dmamem_unmap(gsc->dmat, screen->tm, screen->tm_size);
  361. fail3:
  362. bus_dmamem_free(gsc->dmat, tm_segs, tm_nsegs);
  363. fail2:
  364. bus_dmamap_destroy(gsc->dmat, tm_dmamap);
  365. fail1:
  366. bus_space_unmap(gsc->iot, gsc->re_ioh, RE_REG_SIZE);
  367. fail0:
  368. bus_space_unmap(gsc->iot, gsc->ioh, GBE_REG_SIZE);
  369. }
  370. int
  371. gbe_activate(struct device *self, int act)
  372. {
  373. struct gbe_softc *gsc = (struct gbe_softc *)self;
  374. int ret = 0;
  375. switch (act) {
  376. case DVACT_POWERDOWN:
  377. gbe_disable(gsc);
  378. break;
  379. }
  380. return (ret);
  381. }
  382. /*
  383. * GBE hardware specific functions.
  384. */
  385. void
  386. gbe_init_screen(struct gbe_screen *screen)
  387. {
  388. uint16_t *tm;
  389. int i;
  390. /*
  391. * Initialise screen.
  392. */
  393. screen->mode = WSDISPLAYIO_MODE_EMUL;
  394. /* Initialise rasops. */
  395. memset(&screen->ri, 0, sizeof(struct rasops_info));
  396. screen->ri.ri_flg = RI_CENTER;
  397. screen->ri.ri_depth = screen->depth;
  398. screen->ri.ri_width = screen->width;
  399. screen->ri.ri_height = screen->height;
  400. screen->ri.ri_bits = (void *)screen->fb;
  401. screen->ri.ri_stride = screen->linebytes;
  402. if (screen->depth == 32) {
  403. screen->ri.ri_rpos = 24;
  404. screen->ri.ri_rnum = 8;
  405. screen->ri.ri_gpos = 16;
  406. screen->ri.ri_gnum = 8;
  407. screen->ri.ri_bpos = 8;
  408. screen->ri.ri_bnum = 8;
  409. } else if (screen->depth == 16) {
  410. screen->ri.ri_rpos = 10;
  411. screen->ri.ri_rnum = 5;
  412. screen->ri.ri_gpos = 5;
  413. screen->ri.ri_gnum = 5;
  414. screen->ri.ri_bpos = 0;
  415. screen->ri.ri_bnum = 5;
  416. }
  417. rasops_init(&screen->ri, screen->height / 8, screen->width / 8);
  418. /* Create a rasops instance that can draw into a single tile. */
  419. memcpy(&screen->ri_tile, &screen->ri, sizeof(struct rasops_info));
  420. screen->ri_tile.ri_flg = 0;
  421. screen->ri_tile.ri_width = GBE_TILE_WIDTH >> (screen->depth >> 4);
  422. screen->ri_tile.ri_height = GBE_TILE_HEIGHT;
  423. screen->ri_tile.ri_stride = screen->ri_tile.ri_width *
  424. screen->depth / 8;
  425. screen->ri_tile.ri_xorigin = 0;
  426. screen->ri_tile.ri_yorigin = 0;
  427. screen->ri_tile.ri_bits = screen->ro;
  428. screen->ri_tile.ri_origbits = screen->ro;
  429. screen->ro_curpos = 0;
  430. screen->ri.ri_hw = screen->sc;
  431. screen->ri.ri_do_cursor = gbe_do_cursor;
  432. screen->ri.ri_ops.putchar = gbe_putchar;
  433. screen->ri.ri_ops.copyrows = gbe_copyrows;
  434. screen->ri.ri_ops.copycols = gbe_copycols;
  435. screen->ri.ri_ops.eraserows = gbe_eraserows;
  436. screen->ri.ri_ops.erasecols = gbe_erasecols;
  437. gbe_stdscreen.ncols = screen->ri.ri_cols;
  438. gbe_stdscreen.nrows = screen->ri.ri_rows;
  439. gbe_stdscreen.textops = &screen->ri.ri_ops;
  440. gbe_stdscreen.fontwidth = screen->ri.ri_font->fontwidth;
  441. gbe_stdscreen.fontheight = screen->ri.ri_font->fontheight;
  442. gbe_stdscreen.capabilities = screen->ri.ri_caps;
  443. /*
  444. * Map framebuffer into tilemap. Each entry in the tilemap is 16 bits
  445. * wide. Each tile is 64KB or 2^16 bits, hence the last 16 bits of the
  446. * address will range from 0x0000 to 0xffff. As a result we simply
  447. * discard the lower 16 bits and store bits 17 through 32 as an entry
  448. * in the tilemap.
  449. */
  450. tm = (void *)screen->tm;
  451. for (i = 0; i < (screen->fb_size >> GBE_TILE_SHIFT) &&
  452. i < GBE_TLB_SIZE; i++)
  453. tm[i] = (screen->fb_phys >> GBE_TILE_SHIFT) + i;
  454. }
  455. void
  456. gbe_enable(struct gbe_softc *gsc)
  457. {
  458. struct gbe_screen *screen = gsc->curscr;
  459. uint32_t val;
  460. int i;
  461. /* Enable dot clock. */
  462. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK);
  463. bus_space_write_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK,
  464. val | GBE_DOTCLOCK_RUN);
  465. for (i = 0; i < 10000; i++) {
  466. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK);
  467. if ((val & GBE_DOTCLOCK_RUN) == GBE_DOTCLOCK_RUN)
  468. break;
  469. delay(10);
  470. }
  471. if (i == 10000)
  472. printf("timeout enabling dot clock!\n");
  473. /* Unfreeze pixel counter. */
  474. bus_space_write_4(gsc->iot, gsc->ioh, GBE_VT_XY, 0);
  475. for (i = 0; i < 10000; i++) {
  476. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_XY);
  477. if ((val & GBE_VT_XY_FREEZE) == 0)
  478. break;
  479. delay(10);
  480. }
  481. if (i == 10000)
  482. printf("timeout unfreezing pixel counter!\n");
  483. /* Disable sync-on-green. */
  484. if (strcmp(osloadoptions, "nosog") == 0)
  485. bus_space_write_4(gsc->iot, gsc->ioh, GBE_VT_FLAGS,
  486. GBE_VT_SYNC_LOW);
  487. /* Provide GBE with address of tilemap and enable DMA. */
  488. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_CTRL,
  489. ((screen->tm_phys >> 9) <<
  490. GBE_FB_CTRL_TILE_PTR_SHIFT) | GBE_FB_CTRL_DMA_ENABLE);
  491. }
  492. void
  493. gbe_disable(struct gbe_softc *gsc)
  494. {
  495. uint32_t val;
  496. int i;
  497. /* Nothing to do if the pixel counter is frozen! */
  498. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_XY);
  499. if ((val & GBE_VT_XY_FREEZE) == GBE_VT_XY_FREEZE)
  500. return;
  501. /* Enable sync-on-green. */
  502. bus_space_write_4(gsc->iot, gsc->ioh, GBE_VT_FLAGS, 0);
  503. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK);
  504. if ((val & GBE_DOTCLOCK_RUN) == 0)
  505. return;
  506. /* Disable overlay and turn off hardware cursor. */
  507. bus_space_write_4(gsc->iot, gsc->ioh, GBE_OVERLAY_TILE, 0);
  508. bus_space_write_4(gsc->iot, gsc->ioh, GBE_CURSOR_CTRL, 0);
  509. bus_space_write_4(gsc->iot, gsc->ioh, GBE_DID_CTRL, 0);
  510. /* Disable DMA. */
  511. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_OVERLAY_CTRL);
  512. bus_space_write_4(gsc->iot, gsc->ioh, GBE_OVERLAY_CTRL,
  513. val & ~GBE_OVERLAY_CTRL_DMA_ENABLE);
  514. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_FB_CTRL);
  515. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_CTRL,
  516. val & ~GBE_FB_CTRL_DMA_ENABLE);
  517. bus_space_write_4(gsc->iot, gsc->ioh, GBE_DID_CTRL, 0);
  518. for (i = 0; i < 100000; i++) {
  519. if ((bus_space_read_4(gsc->iot, gsc->ioh, GBE_OVERLAY_HW_CTRL)
  520. & GBE_OVERLAY_CTRL_DMA_ENABLE) == 0 &&
  521. (bus_space_read_4(gsc->iot, gsc->ioh, GBE_FB_HW_CTRL)
  522. & GBE_FB_CTRL_DMA_ENABLE) == 0 &&
  523. bus_space_read_4(gsc->iot, gsc->ioh, GBE_DID_HW_CTRL) == 0)
  524. break;
  525. delay(10);
  526. }
  527. if (i == 100000)
  528. printf("timeout disabling DMA!\n");
  529. /* Wait for the end of pixel refresh. */
  530. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_VPIX)
  531. & GBE_VT_VPIX_OFF_MASK;
  532. for (i = 0; i < 100000; i++) {
  533. if (((bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_XY)
  534. & GBE_VT_XY_Y_MASK) >> GBE_VT_XY_Y_SHIFT) < val)
  535. break;
  536. delay(1);
  537. }
  538. if (i == 100000)
  539. printf("timeout waiting for pixel refresh!\n");
  540. for (i = 0; i < 100000; i++) {
  541. if (((bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_XY)
  542. & GBE_VT_XY_Y_MASK) >> GBE_VT_XY_Y_SHIFT) > val)
  543. break;
  544. delay(1);
  545. }
  546. if (i == 100000)
  547. printf("timeout waiting for pixel refresh!\n");
  548. /* Freeze pixel counter. */
  549. bus_space_write_4(gsc->iot, gsc->ioh, GBE_VT_XY, GBE_VT_XY_FREEZE);
  550. for (i = 0; i < 100000; i++) {
  551. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_XY);
  552. if ((val & GBE_VT_XY_FREEZE) == GBE_VT_XY_FREEZE)
  553. break;
  554. delay(10);
  555. }
  556. if (i == 100000)
  557. printf("timeout freezing pixel counter!\n");
  558. /* Disable dot clock. */
  559. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK);
  560. bus_space_write_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK,
  561. val & ~GBE_DOTCLOCK_RUN);
  562. for (i = 0; i < 100000; i++) {
  563. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_DOTCLOCK);
  564. if ((val & GBE_DOTCLOCK_RUN) == 0)
  565. break;
  566. delay(10);
  567. }
  568. if (i == 100000)
  569. printf("timeout disabling dot clock!\n");
  570. /* Reset DMA fifo. */
  571. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_TILE);
  572. val &= ~(1 << GBE_FB_SIZE_TILE_FIFO_RESET_SHIFT);
  573. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_TILE,
  574. val | (1 << GBE_FB_SIZE_TILE_FIFO_RESET_SHIFT));
  575. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_TILE, val);
  576. }
  577. void
  578. gbe_setup(struct gbe_softc *gsc)
  579. {
  580. struct gbe_screen *screen = gsc->curscr;
  581. int i, t, cmode, tile_width, tiles_x, tiles_y;
  582. u_char *colour;
  583. uint16_t *tm;
  584. uint32_t val;
  585. uint64_t reg;
  586. /*
  587. * Setup framebuffer.
  588. */
  589. switch (screen->depth) {
  590. case 32:
  591. cmode = GBE_CMODE_RGB8;
  592. screen->bufmode = COLOUR_DEPTH_32 << BUFMODE_BUFDEPTH_SHIFT |
  593. PIXEL_TYPE_RGB << BUFMODE_PIXTYPE_SHIFT |
  594. COLOUR_DEPTH_32 << BUFMODE_PIXDEPTH_SHIFT;
  595. break;
  596. case 16:
  597. cmode = GBE_CMODE_ARGB5;
  598. screen->bufmode = COLOUR_DEPTH_16 << BUFMODE_BUFDEPTH_SHIFT |
  599. PIXEL_TYPE_RGBA << BUFMODE_PIXTYPE_SHIFT |
  600. COLOUR_DEPTH_16 << BUFMODE_PIXDEPTH_SHIFT;
  601. break;
  602. case 8:
  603. default:
  604. cmode = GBE_CMODE_I8;
  605. screen->bufmode = COLOUR_DEPTH_8 << BUFMODE_BUFDEPTH_SHIFT |
  606. PIXEL_TYPE_CI << BUFMODE_PIXTYPE_SHIFT |
  607. COLOUR_DEPTH_8 << BUFMODE_PIXDEPTH_SHIFT;
  608. break;
  609. }
  610. /* Calculate tile width in bytes and screen size in tiles. */
  611. tile_width = GBE_TILE_WIDTH >> (screen->depth >> 4);
  612. tiles_x = (screen->width + tile_width - 1) >>
  613. (GBE_TILE_WIDTH_SHIFT - (screen->depth >> 4));
  614. tiles_y = (screen->height + GBE_TILE_HEIGHT - 1) >>
  615. GBE_TILE_HEIGHT_SHIFT;
  616. if (screen->mode != WSDISPLAYIO_MODE_EMUL) {
  617. /*
  618. * Setup the framebuffer in "linear" mode. We trick the
  619. * framebuffer into linear mode by telling it that it is one
  620. * tile wide and specifying an adjusted framebuffer height.
  621. */
  622. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_TILE,
  623. ((screen->depth >> 4) << GBE_FB_SIZE_TILE_DEPTH_SHIFT) |
  624. (1 << GBE_FB_SIZE_TILE_WIDTH_SHIFT));
  625. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_PIXEL,
  626. (screen->width * screen->height / tile_width) <<
  627. GBE_FB_SIZE_PIXEL_HEIGHT_SHIFT);
  628. } else {
  629. /*
  630. * Setup the framebuffer in tiled mode. Provide the tile
  631. * colour depth, screen width in whole and partial tiles,
  632. * and the framebuffer height in pixels.
  633. */
  634. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_TILE,
  635. ((screen->depth >> 4) << GBE_FB_SIZE_TILE_DEPTH_SHIFT) |
  636. ((screen->width / tile_width) <<
  637. GBE_FB_SIZE_TILE_WIDTH_SHIFT) |
  638. (((screen->width % tile_width) >> (screen->depth >> 4)) /
  639. 32));
  640. bus_space_write_4(gsc->iot, gsc->ioh, GBE_FB_SIZE_PIXEL,
  641. screen->height << GBE_FB_SIZE_PIXEL_HEIGHT_SHIFT);
  642. }
  643. /* Set colour mode registers. */
  644. val = (cmode << GBE_WID_MODE_SHIFT) | GBE_BMODE_BOTH;
  645. for (i = 0; i < (32 * 4); i += 4)
  646. bus_space_write_4(gsc->iot, gsc->ioh, GBE_MODE + i, val);
  647. /*
  648. * Initialise colourmap if required.
  649. */
  650. if (screen->depth == 8) {
  651. for (i = 0; i < 16; i++) {
  652. colour = (u_char *)&rasops_cmap[i * 3];
  653. screen->cmap.cm_red[i] = colour[0];
  654. screen->cmap.cm_green[i] = colour[1];
  655. screen->cmap.cm_blue[i] = colour[2];
  656. }
  657. for (i = 240; i < 256; i++) {
  658. colour = (u_char *)&rasops_cmap[i * 3];
  659. screen->cmap.cm_red[i] = colour[0];
  660. screen->cmap.cm_green[i] = colour[1];
  661. screen->cmap.cm_blue[i] = colour[2];
  662. }
  663. }
  664. /*
  665. * Setup an alpha ramp.
  666. */
  667. for (i = 0; i < GBE_GMAP_ENTRIES; i++)
  668. bus_space_write_4(gsc->iot, gsc->ioh,
  669. GBE_GMAP + i * sizeof(u_int32_t),
  670. (i << 24) | (i << 16) | (i << 8));
  671. /*
  672. * Initialise the rendering engine.
  673. */
  674. val = screen->mode | BUF_TYPE_TLB_A << BUFMODE_BUFTYPE_SHIFT;
  675. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_BUFMODE_SRC, val);
  676. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_BUFMODE_DST, val);
  677. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_CLIPMODE, 0);
  678. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_COLOUR_MASK, 0xffffffff);
  679. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PIXEL_XFER_X_STEP, 1);
  680. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PIXEL_XFER_Y_STEP, 1);
  681. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_WINOFFSET_DST, 0);
  682. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_WINOFFSET_SRC, 0);
  683. /*
  684. * Load framebuffer tiles into TLB A. Each TLB consists of a 16x16
  685. * tile array representing 2048x2048 pixels. Each entry in the TLB
  686. * consists of four 16-bit entries which represent bits 17:32 of the
  687. * 64KB tile address. As a result, we can make use of the tilemap
  688. * which already stores tile entries in the same format.
  689. */
  690. tm = (void *)screen->tm;
  691. for (i = 0, t = 0; i < GBE_TLB_SIZE; i++) {
  692. reg <<= 16;
  693. if (i % 16 < tiles_x)
  694. reg |= (tm[t++] | 0x8000);
  695. if (i % 4 == 3)
  696. bus_space_write_8(gsc->iot, gsc->re_ioh,
  697. RE_TLB_A + (i >> 2) * 8, reg);
  698. }
  699. /* Load single tile into TLB B for rasops. */
  700. bus_space_write_8(gsc->iot, gsc->re_ioh,
  701. RE_TLB_B, (screen->ro_phys >> 16 | 0x8000) << 48);
  702. }
  703. void
  704. gbe_wait_re_idle(struct gbe_softc *gsc)
  705. {
  706. int i;
  707. /* Wait until rendering engine is idle. */
  708. for (i = 0; i < 100000; i++) {
  709. if (bus_space_read_4(gsc->iot, gsc->re_ioh, RE_PP_STATUS) &
  710. RE_PP_STATUS_IDLE)
  711. break;
  712. delay(1);
  713. }
  714. if (i == 100000)
  715. printf("%s: rendering engine did not become idle!\n",
  716. gsc->sc_dev.dv_xname);
  717. }
  718. /*
  719. * Interfaces for wscons.
  720. */
  721. int
  722. gbe_ioctl(void *v, u_long cmd, caddr_t data, int flags, struct proc *p)
  723. {
  724. struct gbe_screen *screen = (struct gbe_screen *)v;
  725. int rc, mode;
  726. switch (cmd) {
  727. case WSDISPLAYIO_GTYPE:
  728. *(u_int *)data = WSDISPLAY_TYPE_GBE;
  729. break;
  730. case WSDISPLAYIO_GINFO:
  731. {
  732. struct wsdisplay_fbinfo *fb = (struct wsdisplay_fbinfo *)data;
  733. fb->height = screen->height;
  734. fb->width = screen->width;
  735. fb->depth = screen->depth;
  736. fb->cmsize = screen->depth == 8 ? 256 : 0;
  737. }
  738. break;
  739. case WSDISPLAYIO_LINEBYTES:
  740. *(u_int *)data = screen->linebytes;
  741. break;
  742. case WSDISPLAYIO_GETCMAP:
  743. if (screen->depth == 8) {
  744. struct wsdisplay_cmap *cm =
  745. (struct wsdisplay_cmap *)data;
  746. rc = gbe_getcmap(&screen->cmap, cm);
  747. if (rc != 0)
  748. return (rc);
  749. }
  750. break;
  751. case WSDISPLAYIO_PUTCMAP:
  752. if (screen->depth == 8) {
  753. struct wsdisplay_cmap *cm =
  754. (struct wsdisplay_cmap *)data;
  755. rc = gbe_putcmap(&screen->cmap, cm);
  756. if (rc != 0)
  757. return (rc);
  758. gbe_loadcmap(screen, cm->index, cm->index + cm->count);
  759. }
  760. break;
  761. case WSDISPLAYIO_GMODE:
  762. *(u_int *)data = screen->mode;
  763. break;
  764. case WSDISPLAYIO_SMODE:
  765. mode = *(u_int *)data;
  766. if (mode == WSDISPLAYIO_MODE_EMUL ||
  767. mode == WSDISPLAYIO_MODE_MAPPED ||
  768. mode == WSDISPLAYIO_MODE_DUMBFB) {
  769. screen->mode = mode;
  770. gbe_disable((struct gbe_softc *)screen->sc);
  771. gbe_setup((struct gbe_softc *)screen->sc);
  772. gbe_enable((struct gbe_softc *)screen->sc);
  773. /* Clear framebuffer if entering emulated mode. */
  774. if (screen->mode == WSDISPLAYIO_MODE_EMUL)
  775. gbe_fillrect((struct gbe_softc *)screen->sc,
  776. 0, 0, screen->width, screen->height, 0);
  777. }
  778. break;
  779. case WSDISPLAYIO_GVIDEO:
  780. case WSDISPLAYIO_SVIDEO:
  781. /* Handled by the upper layer. */
  782. break;
  783. default:
  784. return (-1);
  785. }
  786. return (0);
  787. }
  788. paddr_t
  789. gbe_mmap(void *v, off_t offset, int protection)
  790. {
  791. struct gbe_screen *screen = (void *)v;
  792. paddr_t pa;
  793. if (offset >= 0 && offset < screen->fb_size)
  794. pa = screen->fb_phys + offset;
  795. else
  796. pa = -1;
  797. return (pa);
  798. }
  799. int
  800. gbe_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep,
  801. int *curxp, int *curyp, long *attrp)
  802. {
  803. struct gbe_screen *screen = (struct gbe_screen *)v;
  804. struct gbe_softc *gsc = (struct gbe_softc *)screen->sc;
  805. /* We do not allow multiple consoles at the moment. */
  806. if (gsc->screens > 0)
  807. return (ENOMEM);
  808. gsc->screens++;
  809. /* Return rasops_info via cookie. */
  810. *cookiep = &screen->ri;
  811. /* Move cursor to top left of screen. */
  812. *curxp = 0;
  813. *curyp = 0;
  814. /* Correct screen attributes. */
  815. screen->ri.ri_ops.alloc_attr(&screen->ri, 0, 0, 0, attrp);
  816. return (0);
  817. }
  818. void
  819. gbe_free_screen(void *v, void *cookie)
  820. {
  821. /* We do not allow multiple consoles at the moment. */
  822. }
  823. int
  824. gbe_show_screen(void *v, void *cookie, int waitok, void (*cb)(void *, int, int),
  825. void *cbarg)
  826. {
  827. /* We do not allow multiple consoles at the moment. */
  828. return (0);
  829. }
  830. int
  831. gbe_load_font(void *v, void *emulcookie, struct wsdisplay_font *font)
  832. {
  833. struct gbe_screen *screen = (struct gbe_screen *)v;
  834. return rasops_load_font(&screen->ri, emulcookie, font);
  835. }
  836. int
  837. gbe_list_font(void *v, struct wsdisplay_font *font)
  838. {
  839. struct gbe_screen *screen = (struct gbe_screen *)v;
  840. return rasops_list_font(&screen->ri, font);
  841. }
  842. /*
  843. * Colour map handling for indexed modes.
  844. */
  845. void
  846. gbe_setcolour(struct gbe_softc *gsc, u_int index, u_int8_t r, u_int8_t g,
  847. u_int8_t b)
  848. {
  849. int i;
  850. /* Wait until the colourmap FIFO has free space. */
  851. for (i = 0; i < 10000; i++) {
  852. if (bus_space_read_4(gsc->iot, gsc->ioh, GBE_CMAP_FIFO)
  853. < GBE_CMAP_FIFO_ENTRIES)
  854. break;
  855. delay(10);
  856. }
  857. if (i == 10000)
  858. printf("colourmap FIFO has no free space!\n");
  859. bus_space_write_4(gsc->iot, gsc->ioh,
  860. GBE_CMAP + index * sizeof(u_int32_t),
  861. ((u_int)r << 24) | ((u_int)g << 16) | ((u_int)b << 8));
  862. }
  863. int
  864. gbe_getcmap(struct gbe_cmap *cm, struct wsdisplay_cmap *rcm)
  865. {
  866. u_int index = rcm->index, count = rcm->count;
  867. int rc;
  868. if (index >= 256 || count > 256 - index)
  869. return (EINVAL);
  870. if ((rc = copyout(&cm->cm_red[index], rcm->red, count)) != 0)
  871. return (rc);
  872. if ((rc = copyout(&cm->cm_green[index], rcm->green, count)) != 0)
  873. return (rc);
  874. if ((rc = copyout(&cm->cm_blue[index], rcm->blue, count)) != 0)
  875. return (rc);
  876. return (0);
  877. }
  878. int
  879. gbe_putcmap(struct gbe_cmap *cm, struct wsdisplay_cmap *rcm)
  880. {
  881. u_int index = rcm->index, count = rcm->count;
  882. int rc;
  883. if (index >= 256 || count > 256 - index)
  884. return (EINVAL);
  885. if ((rc = copyin(rcm->red, &cm->cm_red[index], count)) != 0)
  886. return (rc);
  887. if ((rc = copyin(rcm->green, &cm->cm_green[index], count)) != 0)
  888. return (rc);
  889. if ((rc = copyin(rcm->blue, &cm->cm_blue[index], count)) != 0)
  890. return (rc);
  891. return (0);
  892. }
  893. void
  894. gbe_loadcmap(struct gbe_screen *screen, u_int start, u_int end)
  895. {
  896. struct gbe_softc *gsc = (void *)screen->sc;
  897. struct gbe_cmap *cm = &screen->cmap;
  898. for (; start <= end; start++)
  899. gbe_setcolour(gsc, start,
  900. cm->cm_red[start], cm->cm_green[start], cm->cm_blue[start]);
  901. }
  902. /*
  903. * Hardware accelerated functions for rasops.
  904. */
  905. void
  906. gbe_rop(struct gbe_softc *gsc, int x, int y, int w, int h, int op)
  907. {
  908. gbe_wait_re_idle(gsc);
  909. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PRIMITIVE,
  910. PRIMITIVE_RECTANGLE | PRIMITIVE_LRTB);
  911. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_DRAWMODE,
  912. DRAWMODE_BITMASK | DRAWMODE_BYTEMASK | DRAWMODE_PIXEL_XFER |
  913. DRAWMODE_LOGIC_OP);
  914. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_LOGIC_OP, op);
  915. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PIXEL_XFER_SRC,
  916. (x << 16) | (y & 0xffff));
  917. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_VERTEX_X_0,
  918. (x << 16) | (y & 0xffff));
  919. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_VERTEX_X_1 | RE_START,
  920. ((x + w - 1) << 16) | ((y + h - 1) & 0xffff));
  921. }
  922. void
  923. gbe_copyrect(struct gbe_softc *gsc, int src, int sx, int sy, int dx, int dy,
  924. int w, int h)
  925. {
  926. int direction, x0, y0, x1, y1;
  927. if (sx >= dx && sy >= dy) {
  928. direction = PRIMITIVE_LRTB;
  929. x0 = dx;
  930. y0 = dy;
  931. x1 = dx + w - 1;
  932. y1 = dy + h - 1;
  933. } else if (sx >= dx && sy < dy) {
  934. direction = PRIMITIVE_LRBT;
  935. sy = sy + h - 1;
  936. x0 = dx;
  937. y0 = dy + h - 1;
  938. x1 = dx + w - 1;
  939. y1 = dy;
  940. } else if (sx < dx && sy >= dy) {
  941. direction = PRIMITIVE_RLTB;
  942. sx = sx + w - 1;
  943. x0 = dx + w - 1;
  944. y0 = dy;
  945. x1 = dx;
  946. y1 = dy + h - 1;
  947. } else if (sx < dx && sy < dy) {
  948. direction = PRIMITIVE_RLBT;
  949. sy = sy + h - 1;
  950. sx = sx + w - 1;
  951. x0 = dx + w - 1;
  952. y0 = dy + h - 1;
  953. x1 = dx;
  954. y1 = dy;
  955. }
  956. gbe_wait_re_idle(gsc);
  957. if (src != BUF_TYPE_TLB_A)
  958. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_BUFMODE_SRC,
  959. gsc->curscr->bufmode | (src << BUFMODE_BUFTYPE_SHIFT));
  960. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PRIMITIVE,
  961. PRIMITIVE_RECTANGLE | direction);
  962. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_DRAWMODE,
  963. DRAWMODE_BITMASK | DRAWMODE_BYTEMASK | DRAWMODE_PIXEL_XFER);
  964. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PIXEL_XFER_SRC,
  965. (sx << 16) | (sy & 0xffff));
  966. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_VERTEX_X_0,
  967. (x0 << 16) | (y0 & 0xffff));
  968. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_VERTEX_X_1 | RE_START,
  969. (x1 << 16) | (y1 & 0xffff));
  970. if (src != BUF_TYPE_TLB_A) {
  971. gbe_wait_re_idle(gsc);
  972. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_BUFMODE_SRC,
  973. gsc->curscr->bufmode |
  974. (BUF_TYPE_TLB_A << BUFMODE_BUFTYPE_SHIFT));
  975. }
  976. }
  977. void
  978. gbe_fillrect(struct gbe_softc *gsc, int x, int y, int w, int h, int bg)
  979. {
  980. gbe_wait_re_idle(gsc);
  981. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_PRIMITIVE,
  982. PRIMITIVE_RECTANGLE | PRIMITIVE_LRTB);
  983. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_DRAWMODE,
  984. DRAWMODE_BITMASK | DRAWMODE_BYTEMASK);
  985. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_SHADE_FG_COLOUR, bg);
  986. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_VERTEX_X_0,
  987. (x << 16) | (y & 0xffff));
  988. bus_space_write_4(gsc->iot, gsc->re_ioh, RE_PP_VERTEX_X_1 | RE_START,
  989. ((x + w - 1) << 16) | ((y + h - 1) & 0xffff));
  990. }
  991. int
  992. gbe_do_cursor(struct rasops_info *ri)
  993. {
  994. struct gbe_softc *sc = ri->ri_hw;
  995. int y, x, w, h;
  996. w = ri->ri_font->fontwidth;
  997. h = ri->ri_font->fontheight;
  998. x = ri->ri_xorigin + ri->ri_ccol * w;
  999. y = ri->ri_yorigin + ri->ri_crow * h;
  1000. gbe_rop(sc, x, y, w, h, OPENGL_LOGIC_OP_COPY_INVERTED);
  1001. return 0;
  1002. }
  1003. int
  1004. gbe_putchar(void *cookie, int row, int col, u_int uc, long attr)
  1005. {
  1006. struct rasops_info *ri = cookie;
  1007. struct gbe_softc *gsc = ri->ri_hw;
  1008. struct gbe_screen *screen = gsc->curscr;
  1009. struct rasops_info *ri_tile = &screen->ri_tile;
  1010. int x, y, w, h;
  1011. w = ri->ri_font->fontwidth;
  1012. h = ri->ri_font->fontheight;
  1013. x = ri->ri_xorigin + col * w;
  1014. y = ri->ri_yorigin + row * h;
  1015. ri_tile->ri_ops.putchar(ri_tile, 0, screen->ro_curpos, uc, attr);
  1016. gbe_copyrect(gsc, BUF_TYPE_TLB_B, screen->ro_curpos * w, 0, x, y, w, h);
  1017. screen->ro_curpos++;
  1018. if ((screen->ro_curpos + 1) * w > screen->ri_tile.ri_width)
  1019. screen->ro_curpos = 0;
  1020. return 0;
  1021. }
  1022. int
  1023. gbe_copycols(void *cookie, int row, int src, int dst, int num)
  1024. {
  1025. struct rasops_info *ri = cookie;
  1026. struct gbe_softc *sc = ri->ri_hw;
  1027. num *= ri->ri_font->fontwidth;
  1028. src *= ri->ri_font->fontwidth;
  1029. dst *= ri->ri_font->fontwidth;
  1030. row *= ri->ri_font->fontheight;
  1031. gbe_copyrect(sc, BUF_TYPE_TLB_A, ri->ri_xorigin + src,
  1032. ri->ri_yorigin + row, ri->ri_xorigin + dst, ri->ri_yorigin + row,
  1033. num, ri->ri_font->fontheight);
  1034. return 0;
  1035. }
  1036. int
  1037. gbe_erasecols(void *cookie, int row, int col, int num, long attr)
  1038. {
  1039. struct rasops_info *ri = cookie;
  1040. struct gbe_softc *sc = ri->ri_hw;
  1041. int bg, fg;
  1042. ri->ri_ops.unpack_attr(cookie, attr, &fg, &bg, NULL);
  1043. row *= ri->ri_font->fontheight;
  1044. col *= ri->ri_font->fontwidth;
  1045. num *= ri->ri_font->fontwidth;
  1046. gbe_fillrect(sc, ri->ri_xorigin + col, ri->ri_yorigin + row,
  1047. num, ri->ri_font->fontheight, ri->ri_devcmap[bg]);
  1048. return 0;
  1049. }
  1050. int
  1051. gbe_copyrows(void *cookie, int src, int dst, int num)
  1052. {
  1053. struct rasops_info *ri = cookie;
  1054. struct gbe_softc *sc = ri->ri_hw;
  1055. num *= ri->ri_font->fontheight;
  1056. src *= ri->ri_font->fontheight;
  1057. dst *= ri->ri_font->fontheight;
  1058. gbe_copyrect(sc, BUF_TYPE_TLB_A, ri->ri_xorigin, ri->ri_yorigin + src,
  1059. ri->ri_xorigin, ri->ri_yorigin + dst, ri->ri_emuwidth, num);
  1060. return 0;
  1061. }
  1062. int
  1063. gbe_eraserows(void *cookie, int row, int num, long attr)
  1064. {
  1065. struct rasops_info *ri = cookie;
  1066. struct gbe_softc *sc = ri->ri_hw;
  1067. int x, y, w, bg, fg;
  1068. ri->ri_ops.unpack_attr(cookie, attr, &fg, &bg, NULL);
  1069. if ((num == ri->ri_rows) && ISSET(ri->ri_flg, RI_FULLCLEAR)) {
  1070. num = ri->ri_height;
  1071. x = y = 0;
  1072. w = ri->ri_width;
  1073. } else {
  1074. num *= ri->ri_font->fontheight;
  1075. x = ri->ri_xorigin;
  1076. y = ri->ri_yorigin + row * ri->ri_font->fontheight;
  1077. w = ri->ri_emuwidth;
  1078. }
  1079. gbe_fillrect(sc, x, y, w, num, ri->ri_devcmap[bg]);
  1080. return 0;
  1081. }
  1082. /*
  1083. * Console functions for early display.
  1084. */
  1085. int
  1086. gbe_cnprobe(bus_space_tag_t iot, bus_addr_t addr)
  1087. {
  1088. bus_space_handle_t ioh;
  1089. int val, width, height;
  1090. /* Setup bus space mapping. */
  1091. ioh = PHYS_TO_XKPHYS(addr, CCA_NC);
  1092. /* Determine resolution configured by firmware. */
  1093. val = bus_space_read_4(iot, ioh, GBE_VT_HCMAP);
  1094. width = (val >> GBE_VT_HCMAP_ON_SHIFT) & 0xfff;
  1095. val = bus_space_read_4(iot, ioh, GBE_VT_VCMAP);
  1096. height = (val >> GBE_VT_VCMAP_ON_SHIFT) & 0xfff;
  1097. /* Ensure that the firmware has setup the device. */
  1098. if (width != 0 && height != 0)
  1099. return (1);
  1100. else
  1101. return (0);
  1102. }
  1103. int
  1104. gbe_cnattach(bus_space_tag_t iot, bus_addr_t addr)
  1105. {
  1106. struct gbe_softc *gsc;
  1107. uint32_t val;
  1108. paddr_t pa;
  1109. vaddr_t va;
  1110. long attr;
  1111. /*
  1112. * Setup GBE for use as early console.
  1113. */
  1114. va = pmap_steal_memory(sizeof(struct gbe_softc), NULL, NULL);
  1115. gsc = (struct gbe_softc *)va;
  1116. gsc->curscr = &gbe_consdata;
  1117. gbe_consdata.sc = (struct device *)gsc;
  1118. /* Setup bus space mapping. */
  1119. gsc->iot = iot;
  1120. gsc->ioh = PHYS_TO_XKPHYS(addr, CCA_NC);
  1121. gsc->re_ioh = PHYS_TO_XKPHYS(RE_BASE, CCA_NC);
  1122. /* Determine GBE revision. */
  1123. gsc->rev = bus_space_read_4(gsc->iot, gsc->ioh, GBE_CTRL_STAT) & 0xf;
  1124. /* Determine resolution configured by firmware. */
  1125. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_HCMAP);
  1126. gbe_consdata.width = (val >> GBE_VT_HCMAP_ON_SHIFT) & 0xfff;
  1127. val = bus_space_read_4(gsc->iot, gsc->ioh, GBE_VT_VCMAP);
  1128. gbe_consdata.height = (val >> GBE_VT_VCMAP_ON_SHIFT) & 0xfff;
  1129. /* Ensure that the firmware has setup the device. */
  1130. if (gbe_consdata.width == 0 || gbe_consdata.height == 0)
  1131. return (ENXIO);
  1132. /* Setup screen defaults. */
  1133. gbe_consdata.fb_size = GBE_FB_SIZE;
  1134. gbe_consdata.tm_size = GBE_TLB_SIZE * sizeof(uint16_t);
  1135. gbe_consdata.depth = 8;
  1136. gbe_consdata.linebytes = gbe_consdata.width * gbe_consdata.depth / 8;
  1137. /*
  1138. * Steal memory for tilemap - 64KB aligned and coherent.
  1139. */
  1140. va = pmap_steal_memory(gbe_consdata.tm_size + 65536, NULL, NULL);
  1141. pmap_extract(pmap_kernel(), va, &pa);
  1142. gbe_consdata.tm_phys = ((pa >> 16) + 1) << 16;
  1143. gbe_consdata.tm = (caddr_t)PHYS_TO_XKPHYS(gbe_consdata.tm_phys, CCA_NC);
  1144. /*
  1145. * Steal memory for framebuffer - 64KB aligned and coherent.
  1146. */
  1147. va = pmap_steal_memory(gbe_consdata.fb_size + 65536, NULL, NULL);
  1148. pmap_extract(pmap_kernel(), va, &pa);
  1149. gbe_consdata.fb_phys = ((pa >> 16) + 1) << 16;
  1150. gbe_consdata.fb = (caddr_t)PHYS_TO_XKPHYS(gbe_consdata.fb_phys, CCA_NC);
  1151. /*
  1152. * Steal memory for rasops tile - 64KB aligned and coherent.
  1153. */
  1154. va = pmap_steal_memory(GBE_TILE_SIZE + 65536, NULL, NULL);
  1155. pmap_extract(pmap_kernel(), va, &pa);
  1156. gbe_consdata.ro_phys = ((pa >> 16) + 1) << 16;
  1157. gbe_consdata.ro = (caddr_t)PHYS_TO_XKPHYS(gbe_consdata.ro_phys, CCA_NC);
  1158. /*
  1159. * Setup GBE hardware.
  1160. */
  1161. gbe_init_screen(&gbe_consdata);
  1162. gbe_disable(gsc);
  1163. gbe_setup(gsc);
  1164. gbe_enable(gsc);
  1165. /* Load colourmap if required. */
  1166. if (gbe_consdata.depth == 8)
  1167. gbe_loadcmap(&gbe_consdata, 0, 255);
  1168. /* Clear framebuffer. */
  1169. gbe_fillrect(gsc, 0, 0, gbe_consdata.width, gbe_consdata.height, 0);
  1170. /*
  1171. * Attach wsdisplay.
  1172. */
  1173. gbe_consdata.ri.ri_ops.alloc_attr(&gbe_consdata.ri, 0, 0, 0, &attr);
  1174. wsdisplay_cnattach(&gbe_stdscreen, &gbe_consdata.ri, 0, 0, attr);
  1175. gbe_console = 1;
  1176. return (0);
  1177. }