cgthree.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* $OpenBSD: cgthree.c,v 1.45 2013/10/20 20:07:30 miod Exp $ */
  2. /*
  3. * Copyright (c) 2001 Jason L. Wright (jason@thought.net)
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. * Effort sponsored in part by the Defense Advanced Research Projects
  28. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  29. * Materiel Command, USAF, under agreement number F30602-01-2-0537.
  30. *
  31. */
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/kernel.h>
  35. #include <sys/errno.h>
  36. #include <sys/device.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/malloc.h>
  39. #include <machine/bus.h>
  40. #include <machine/intr.h>
  41. #include <machine/autoconf.h>
  42. #include <machine/openfirm.h>
  43. #include <dev/sbus/sbusvar.h>
  44. #include <dev/wscons/wsconsio.h>
  45. #include <dev/wscons/wsdisplayvar.h>
  46. #include <dev/rasops/rasops.h>
  47. #include <machine/fbvar.h>
  48. #include <dev/ic/bt458reg.h>
  49. #define CGTHREE_CTRL_OFFSET 0x400000
  50. #define CGTHREE_CTRL_SIZE (sizeof(u_int32_t) * 8)
  51. #define CGTHREE_VID_OFFSET 0x800000
  52. #define CGTHREE_VID_SIZE (1024 * 1024)
  53. union bt_cmap {
  54. u_int8_t cm_map[256][3]; /* 256 r/b/g entries */
  55. u_int32_t cm_chip[256 * 3 / 4]; /* the way the chip is loaded */
  56. };
  57. #define BT_ADDR 0x00 /* map address register */
  58. #define BT_CMAP 0x04 /* colormap data register */
  59. #define BT_CTRL 0x08 /* control register */
  60. #define BT_OMAP 0x0c /* overlay (cursor) map register */
  61. #define CG3_FBC_CTRL 0x10 /* control */
  62. #define CG3_FBC_STAT 0x11 /* status */
  63. #define CG3_FBC_START 0x12 /* cursor start */
  64. #define CG3_FBC_END 0x13 /* cursor end */
  65. #define CG3_FBC_VCTRL 0x14 /* 12 bytes of timing goo */
  66. #define BT_WRITE(sc, reg, val) \
  67. bus_space_write_4((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg), (val))
  68. #define BT_READ(sc, reg) \
  69. bus_space_read_4((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg))
  70. #define BT_BARRIER(sc,reg,flags) \
  71. bus_space_barrier((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg), \
  72. sizeof(u_int32_t), (flags))
  73. #define BT_D4M3(x) ((((x) >> 2) << 1) + ((x) >> 2)) /* (x / 4) * 3 */
  74. #define BT_D4M4(x) ((x) & ~3) /* (x / 4) * 4 */
  75. #define FBC_CTRL_IENAB 0x80 /* interrupt enable */
  76. #define FBC_CTRL_VENAB 0x40 /* video enable */
  77. #define FBC_CTRL_TIME 0x20 /* timing enable */
  78. #define FBC_CTRL_CURS 0x10 /* cursor compare enable */
  79. #define FBC_CTRL_XTAL 0x0c /* xtal select (0,1,2,test): */
  80. #define FBC_CTRL_XTAL_0 0x00 /* 0 */
  81. #define FBC_CTRL_XTAL_1 0x04 /* 0 */
  82. #define FBC_CTRL_XTAL_2 0x08 /* 0 */
  83. #define FBC_CTRL_XTAL_TEST 0x0c /* 0 */
  84. #define FBC_CTRL_DIV 0x03 /* divisor (1,2,3,4): */
  85. #define FBC_CTRL_DIV_1 0x00 /* / 1 */
  86. #define FBC_CTRL_DIV_2 0x01 /* / 2 */
  87. #define FBC_CTRL_DIV_3 0x02 /* / 3 */
  88. #define FBC_CTRL_DIV_4 0x03 /* / 4 */
  89. #define FBC_STAT_INTR 0x80 /* interrupt pending */
  90. #define FBC_STAT_RES 0x70 /* monitor sense: */
  91. #define FBC_STAT_RES_1024 0x10 /* 1024x768 */
  92. #define FBC_STAT_RES_1280 0x40 /* 1280x1024 */
  93. #define FBC_STAT_RES_1152 0x30 /* 1152x900 */
  94. #define FBC_STAT_RES_1152A 0x40 /* 1152x900x76, A */
  95. #define FBC_STAT_RES_1600 0x50 /* 1600x1200 */
  96. #define FBC_STAT_RES_1152B 0x60 /* 1152x900x86, B */
  97. #define FBC_STAT_ID 0x0f /* id mask: */
  98. #define FBC_STAT_ID_COLOR 0x01 /* color */
  99. #define FBC_STAT_ID_MONO 0x02 /* monochrome */
  100. #define FBC_STAT_ID_MONOECL 0x03 /* monochrome, ecl */
  101. #define FBC_READ(sc, reg) \
  102. bus_space_read_1((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg))
  103. #define FBC_WRITE(sc, reg, val) \
  104. bus_space_write_1((sc)->sc_bustag, (sc)->sc_ctrl_regs, (reg), (val))
  105. struct cgthree_softc {
  106. struct sunfb sc_sunfb;
  107. bus_space_tag_t sc_bustag;
  108. bus_addr_t sc_paddr;
  109. bus_space_handle_t sc_ctrl_regs;
  110. bus_space_handle_t sc_vid_regs;
  111. int sc_nscreens;
  112. union bt_cmap sc_cmap;
  113. u_int sc_mode;
  114. };
  115. int cgthree_ioctl(void *, u_long, caddr_t, int, struct proc *);
  116. paddr_t cgthree_mmap(void *, off_t, int);
  117. int cgthree_is_console(int);
  118. void cgthree_loadcmap(struct cgthree_softc *, u_int, u_int);
  119. int cg3_bt_putcmap(union bt_cmap *, struct wsdisplay_cmap *);
  120. int cg3_bt_getcmap(union bt_cmap *, struct wsdisplay_cmap *);
  121. void cgthree_setcolor(void *, u_int, u_int8_t, u_int8_t, u_int8_t);
  122. void cgthree_burner(void *, u_int, u_int);
  123. void cgthree_reset(struct cgthree_softc *);
  124. struct wsdisplay_accessops cgthree_accessops = {
  125. .ioctl = cgthree_ioctl,
  126. .mmap = cgthree_mmap,
  127. .burn_screen = cgthree_burner
  128. };
  129. int cgthreematch(struct device *, void *, void *);
  130. void cgthreeattach(struct device *, struct device *, void *);
  131. struct cfattach cgthree_ca = {
  132. sizeof (struct cgthree_softc), cgthreematch, cgthreeattach
  133. };
  134. struct cfdriver cgthree_cd = {
  135. NULL, "cgthree", DV_DULL
  136. };
  137. #define CG3_TYPE_DEFAULT 0
  138. #define CG3_TYPE_76HZ 1
  139. #define CG3_TYPE_SMALL 2
  140. struct cg3_videoctrl {
  141. u_int8_t sense;
  142. u_int8_t vctrl[12];
  143. u_int8_t ctrl;
  144. } cg3_videoctrl[] = {
  145. { /* cpd-1790 */
  146. FBC_STAT_RES_1152 | FBC_STAT_ID_COLOR,
  147. { 0xbb, 0x2b, 0x04, 0x14, 0xae, 0x03,
  148. 0xa8, 0x24, 0x01, 0x05, 0xff, 0x01 },
  149. FBC_CTRL_XTAL_0 | FBC_CTRL_DIV_1
  150. },
  151. { /* gdm-20e20 */
  152. FBC_STAT_RES_1152A | FBC_STAT_ID_COLOR,
  153. { 0xb7, 0x27, 0x03, 0x0f, 0xae, 0x03,
  154. 0xae, 0x2a, 0x01, 0x09, 0xff, 0x01 },
  155. FBC_CTRL_XTAL_1 | FBC_CTRL_DIV_1
  156. },
  157. { /* defaults, should be last */
  158. 0xff,
  159. { 0xbb, 0x2b, 0x03, 0x0b, 0xb3, 0x03,
  160. 0xaf, 0x2b, 0x02, 0x0a, 0xff, 0x01 },
  161. 0,
  162. },
  163. };
  164. int
  165. cgthreematch(struct device *parent, void *vcf, void *aux)
  166. {
  167. struct cfdata *cf = vcf;
  168. struct sbus_attach_args *sa = aux;
  169. return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
  170. }
  171. void
  172. cgthreeattach(struct device *parent, struct device *self, void *aux)
  173. {
  174. struct cgthree_softc *sc = (struct cgthree_softc *)self;
  175. struct sbus_attach_args *sa = aux;
  176. int node, console;
  177. const char *nam;
  178. node = sa->sa_node;
  179. sc->sc_bustag = sa->sa_bustag;
  180. sc->sc_paddr = sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset);
  181. fb_setsize(&sc->sc_sunfb, 8, 1152, 900, node, 0);
  182. if (sa->sa_nreg != 1) {
  183. printf(": expected %d registers, got %d\n", 1, sa->sa_nreg);
  184. goto fail;
  185. }
  186. /*
  187. * Map just CTRL and video RAM.
  188. */
  189. if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
  190. sa->sa_reg[0].sbr_offset + CGTHREE_CTRL_OFFSET,
  191. CGTHREE_CTRL_SIZE, 0, 0, &sc->sc_ctrl_regs) != 0) {
  192. printf(": cannot map ctrl registers\n");
  193. goto fail_ctrl;
  194. }
  195. if (sbus_bus_map(sa->sa_bustag, sa->sa_reg[0].sbr_slot,
  196. sa->sa_reg[0].sbr_offset + CGTHREE_VID_OFFSET,
  197. sc->sc_sunfb.sf_fbsize, BUS_SPACE_MAP_LINEAR,
  198. 0, &sc->sc_vid_regs) != 0) {
  199. printf(": cannot map vid registers\n");
  200. goto fail_vid;
  201. }
  202. nam = getpropstring(node, "model");
  203. if (*nam == '\0')
  204. nam = sa->sa_name;
  205. printf(": %s", nam);
  206. console = cgthree_is_console(node);
  207. cgthree_reset(sc);
  208. cgthree_burner(sc, 1, 0);
  209. sc->sc_sunfb.sf_ro.ri_bits = (void *)bus_space_vaddr(sc->sc_bustag,
  210. sc->sc_vid_regs);
  211. sc->sc_sunfb.sf_ro.ri_hw = sc;
  212. printf(", %dx%d\n", sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_height);
  213. fbwscons_init(&sc->sc_sunfb, 0, console);
  214. fbwscons_setcolormap(&sc->sc_sunfb, cgthree_setcolor);
  215. if (console)
  216. fbwscons_console_init(&sc->sc_sunfb, -1);
  217. fbwscons_attach(&sc->sc_sunfb, &cgthree_accessops, console);
  218. return;
  219. fail_vid:
  220. bus_space_unmap(sa->sa_bustag, sc->sc_ctrl_regs, CGTHREE_CTRL_SIZE);
  221. fail_ctrl:
  222. fail:
  223. ;
  224. }
  225. int
  226. cgthree_ioctl(void *v, u_long cmd, caddr_t data, int flags, struct proc *p)
  227. {
  228. struct cgthree_softc *sc = v;
  229. struct wsdisplay_fbinfo *wdf;
  230. struct wsdisplay_cmap *cm;
  231. int error;
  232. switch (cmd) {
  233. case WSDISPLAYIO_GTYPE:
  234. *(u_int *)data = WSDISPLAY_TYPE_SUNCG3;
  235. break;
  236. case WSDISPLAYIO_SMODE:
  237. sc->sc_mode = *(u_int *)data;
  238. break;
  239. case WSDISPLAYIO_GINFO:
  240. wdf = (void *)data;
  241. wdf->height = sc->sc_sunfb.sf_height;
  242. wdf->width = sc->sc_sunfb.sf_width;
  243. wdf->depth = sc->sc_sunfb.sf_depth;
  244. wdf->cmsize = 256;
  245. break;
  246. case WSDISPLAYIO_LINEBYTES:
  247. *(u_int *)data = sc->sc_sunfb.sf_linebytes;
  248. break;
  249. case WSDISPLAYIO_GETCMAP:
  250. cm = (struct wsdisplay_cmap *)data;
  251. error = cg3_bt_getcmap(&sc->sc_cmap, cm);
  252. if (error)
  253. return (error);
  254. break;
  255. case WSDISPLAYIO_PUTCMAP:
  256. cm = (struct wsdisplay_cmap *)data;
  257. error = cg3_bt_putcmap(&sc->sc_cmap, cm);
  258. if (error)
  259. return (error);
  260. cgthree_loadcmap(sc, cm->index, cm->count);
  261. break;
  262. case WSDISPLAYIO_SVIDEO:
  263. case WSDISPLAYIO_GVIDEO:
  264. break;
  265. case WSDISPLAYIO_GCURPOS:
  266. case WSDISPLAYIO_SCURPOS:
  267. case WSDISPLAYIO_GCURMAX:
  268. case WSDISPLAYIO_GCURSOR:
  269. case WSDISPLAYIO_SCURSOR:
  270. default:
  271. return -1; /* not supported yet */
  272. }
  273. return (0);
  274. }
  275. #define START (128 * 1024 + 128 * 1024)
  276. #define NOOVERLAY (0x04000000)
  277. paddr_t
  278. cgthree_mmap(void *v, off_t offset, int prot)
  279. {
  280. struct cgthree_softc *sc = v;
  281. if (offset & PGOFSET || offset < 0)
  282. return (-1);
  283. switch (sc->sc_mode) {
  284. case WSDISPLAYIO_MODE_MAPPED:
  285. if (offset >= NOOVERLAY)
  286. offset -= NOOVERLAY;
  287. else if (offset >= START)
  288. offset -= START;
  289. else
  290. offset = 0;
  291. if (offset >= sc->sc_sunfb.sf_fbsize)
  292. return (-1);
  293. return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
  294. CGTHREE_VID_OFFSET + offset, prot, BUS_SPACE_MAP_LINEAR));
  295. case WSDISPLAYIO_MODE_DUMBFB:
  296. if (offset < sc->sc_sunfb.sf_fbsize)
  297. return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
  298. CGTHREE_VID_OFFSET + offset, prot,
  299. BUS_SPACE_MAP_LINEAR));
  300. break;
  301. }
  302. return (-1);
  303. }
  304. int
  305. cgthree_is_console(int node)
  306. {
  307. extern int fbnode;
  308. return (fbnode == node);
  309. }
  310. void
  311. cgthree_setcolor(void *v, u_int index, u_int8_t r, u_int8_t g, u_int8_t b)
  312. {
  313. struct cgthree_softc *sc = v;
  314. union bt_cmap *bcm = &sc->sc_cmap;
  315. bcm->cm_map[index][0] = r;
  316. bcm->cm_map[index][1] = g;
  317. bcm->cm_map[index][2] = b;
  318. cgthree_loadcmap(sc, index, 1);
  319. }
  320. void
  321. cgthree_loadcmap(struct cgthree_softc *sc, u_int start, u_int ncolors)
  322. {
  323. u_int cstart;
  324. int count;
  325. cstart = BT_D4M3(start);
  326. count = BT_D4M3(start + ncolors - 1) - BT_D4M3(start) + 3;
  327. BT_WRITE(sc, BT_ADDR, BT_D4M4(start));
  328. while (--count >= 0) {
  329. BT_WRITE(sc, BT_CMAP, sc->sc_cmap.cm_chip[cstart]);
  330. cstart++;
  331. }
  332. }
  333. int
  334. cg3_bt_getcmap(union bt_cmap *bcm, struct wsdisplay_cmap *rcm)
  335. {
  336. u_int index = rcm->index, count = rcm->count, i;
  337. int error;
  338. if (index >= 256 || count > 256 - index)
  339. return (EINVAL);
  340. for (i = 0; i < count; i++) {
  341. if ((error = copyout(&bcm->cm_map[index + i][0],
  342. &rcm->red[i], 1)) != 0)
  343. return (error);
  344. if ((error = copyout(&bcm->cm_map[index + i][1],
  345. &rcm->green[i], 1)) != 0)
  346. return (error);
  347. if ((error = copyout(&bcm->cm_map[index + i][2],
  348. &rcm->blue[i], 1)) != 0)
  349. return (error);
  350. }
  351. return (0);
  352. }
  353. int
  354. cg3_bt_putcmap(union bt_cmap *bcm, struct wsdisplay_cmap *rcm)
  355. {
  356. u_int index = rcm->index, count = rcm->count, i;
  357. int error;
  358. if (index >= 256 || count > 256 - index)
  359. return (EINVAL);
  360. for (i = 0; i < count; i++) {
  361. if ((error = copyin(&rcm->red[i],
  362. &bcm->cm_map[index + i][0], 1)) != 0)
  363. return (error);
  364. if ((error = copyin(&rcm->green[i],
  365. &bcm->cm_map[index + i][1], 1)) != 0)
  366. return (error);
  367. if ((error = copyin(&rcm->blue[i],
  368. &bcm->cm_map[index + i][2], 1)) != 0)
  369. return (error);
  370. }
  371. return (0);
  372. }
  373. void
  374. cgthree_reset(struct cgthree_softc *sc)
  375. {
  376. int i, j;
  377. u_int8_t sts, ctrl;
  378. sts = FBC_READ(sc, CG3_FBC_STAT);
  379. ctrl = FBC_READ(sc, CG3_FBC_CTRL);
  380. if (ctrl & FBC_CTRL_TIME) {
  381. /* already initialized */
  382. return;
  383. }
  384. for (i = 0; i < nitems(cg3_videoctrl); i++) {
  385. if (cg3_videoctrl[i].sense == 0xff ||
  386. (cg3_videoctrl[i].sense ==
  387. (sts & (FBC_STAT_RES | FBC_STAT_ID)))) {
  388. for (j = 0; j < 12; j++)
  389. FBC_WRITE(sc, CG3_FBC_VCTRL + j,
  390. cg3_videoctrl[i].vctrl[j]);
  391. ctrl &= ~(FBC_CTRL_XTAL | FBC_CTRL_DIV);
  392. ctrl |= cg3_videoctrl[i].ctrl |
  393. FBC_CTRL_TIME;
  394. FBC_WRITE(sc, CG3_FBC_CTRL, ctrl);
  395. break;
  396. }
  397. }
  398. /* enable all the bit planes */
  399. BT_WRITE(sc, BT_ADDR, BT_RMR);
  400. BT_BARRIER(sc, BT_ADDR, BUS_SPACE_BARRIER_WRITE);
  401. BT_WRITE(sc, BT_CTRL, 0xff);
  402. BT_BARRIER(sc, BT_CTRL, BUS_SPACE_BARRIER_WRITE);
  403. /* no plane should blink */
  404. BT_WRITE(sc, BT_ADDR, BT_BMR);
  405. BT_BARRIER(sc, BT_ADDR, BUS_SPACE_BARRIER_WRITE);
  406. BT_WRITE(sc, BT_CTRL, 0x00);
  407. BT_BARRIER(sc, BT_CTRL, BUS_SPACE_BARRIER_WRITE);
  408. /*
  409. * enable the RAMDAC, disable blink, disable overlay 0 and 1,
  410. * use 4:1 multiplexor.
  411. */
  412. BT_WRITE(sc, BT_ADDR, BT_CR);
  413. BT_BARRIER(sc, BT_ADDR, BUS_SPACE_BARRIER_WRITE);
  414. BT_WRITE(sc, BT_CTRL,
  415. (BTCR_MPLX_4 | BTCR_RAMENA | BTCR_BLINK_6464));
  416. BT_BARRIER(sc, BT_CTRL, BUS_SPACE_BARRIER_WRITE);
  417. /* disable the D/A read pins */
  418. BT_WRITE(sc, BT_ADDR, BT_CTR);
  419. BT_BARRIER(sc, BT_ADDR, BUS_SPACE_BARRIER_WRITE);
  420. BT_WRITE(sc, BT_CTRL, 0x00);
  421. BT_BARRIER(sc, BT_CTRL, BUS_SPACE_BARRIER_WRITE);
  422. }
  423. void
  424. cgthree_burner(void *vsc, u_int on, u_int flags)
  425. {
  426. struct cgthree_softc *sc = vsc;
  427. int s;
  428. u_int8_t fbc;
  429. s = splhigh();
  430. fbc = FBC_READ(sc, CG3_FBC_CTRL);
  431. if (on)
  432. fbc |= FBC_CTRL_VENAB | FBC_CTRL_TIME;
  433. else {
  434. fbc &= ~FBC_CTRL_VENAB;
  435. if (flags & WSDISPLAY_BURN_VBLANK)
  436. fbc &= ~FBC_CTRL_TIME;
  437. }
  438. FBC_WRITE(sc, CG3_FBC_CTRL, fbc);
  439. splx(s);
  440. }