agten.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* $OpenBSD: agten.c,v 1.10 2013/10/20 20:07:30 miod Exp $ */
  2. /*
  3. * Copyright (c) 2002, 2003, Miodrag Vallat.
  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. */
  28. /*
  29. * Fujitsu AG-10 framebuffer driver.
  30. *
  31. * The AG-10 is mostly made of:
  32. * - a 3DLabs 300SX Glint chip, with two 6MB independent framebuffer spaces
  33. * - a Number Nine Imagine 128 chip with its own 4MB framebuffer space
  34. * - a Weitek P9100 with its own 2MB of framebuffer memory
  35. * - an IBM PaletteDAC 561 ramdac
  36. * - an Analog Devices ADSP-21062
  37. *
  38. * All of these chips (memory, registers, etc) are mapped in the SBus
  39. * memory space associated to the board. What is unexpected, however, is
  40. * that there are also PCI registers mappings for the first three chips!
  41. *
  42. * The three graphics chips act as overlays of each other, for the final
  43. * video output.
  44. *
  45. * The PROM initialization will use the I128 framebuffer memory for output,
  46. * which is ``above'' the P9100. The P9100 seems to only be there to provide
  47. * a simple RAMDAC interface, but its frame buffer memory is accessible and
  48. * will appear as an ``underlay'' plane.
  49. */
  50. /*
  51. * TODO
  52. * - initialize the I128 in 32bit mode
  53. * - use the i128 acceleration features
  54. */
  55. #include <sys/param.h>
  56. #include <sys/systm.h>
  57. #include <sys/buf.h>
  58. #include <sys/device.h>
  59. #include <sys/ioctl.h>
  60. #include <sys/malloc.h>
  61. #include <sys/mman.h>
  62. #include <sys/tty.h>
  63. #include <sys/conf.h>
  64. #include <uvm/uvm_extern.h>
  65. #include <machine/autoconf.h>
  66. #include <machine/pmap.h>
  67. #include <machine/cpu.h>
  68. #include <machine/conf.h>
  69. #include <dev/wscons/wsconsio.h>
  70. #include <dev/wscons/wsdisplayvar.h>
  71. #include <dev/rasops/rasops.h>
  72. #include <machine/fbvar.h>
  73. #include <dev/ic/p9000.h>
  74. #include <dev/ic/ibm561reg.h>
  75. #include <dev/sbus/sbusvar.h>
  76. struct agten_cmap {
  77. u_int8_t cm_red[256];
  78. u_int8_t cm_green[256];
  79. u_int8_t cm_blue[256];
  80. };
  81. /* per-display variables */
  82. struct agten_softc {
  83. struct sunfb sc_sunfb; /* common base part */
  84. bus_space_tag_t sc_bustag;
  85. bus_addr_t sc_paddr;
  86. off_t sc_physoffset; /* offset for frame buffer */
  87. volatile u_int8_t *sc_p9100;
  88. struct agten_cmap sc_cmap; /* shadow color map */
  89. volatile u_int32_t *sc_i128_fb;
  90. int sc_nscreens;
  91. };
  92. int agten_ioctl(void *, u_long, caddr_t, int, struct proc *);
  93. paddr_t agten_mmap(void *, off_t, int);
  94. void agten_reset(struct agten_softc *);
  95. void agten_setcolor(void *, u_int, u_int8_t, u_int8_t, u_int8_t);
  96. static __inline__ void ibm561_write(struct agten_softc *, u_int32_t, u_int32_t);
  97. int agten_getcmap(struct agten_cmap *, struct wsdisplay_cmap *);
  98. int agten_putcmap(struct agten_cmap *, struct wsdisplay_cmap *);
  99. void agten_loadcmap(struct agten_softc *, u_int, u_int);
  100. struct wsdisplay_accessops agten_accessops = {
  101. .ioctl = agten_ioctl,
  102. .mmap = agten_mmap
  103. };
  104. int agtenmatch(struct device *, void *, void *);
  105. void agtenattach(struct device *, struct device *, void *);
  106. struct cfattach agten_ca = {
  107. sizeof(struct agten_softc), agtenmatch, agtenattach
  108. };
  109. struct cfdriver agten_cd = {
  110. NULL, "agten", DV_DULL
  111. };
  112. int
  113. agtenmatch(struct device *parent, void *vcf, void *aux)
  114. {
  115. struct sbus_attach_args *sa = aux;
  116. if (strcmp(sa->sa_name, "PFU,aga") != 0)
  117. return (0);
  118. return (1);
  119. }
  120. void
  121. agtenattach(struct device *parent, struct device *self, void *args)
  122. {
  123. struct agten_softc *sc = (struct agten_softc *)self;
  124. struct sbus_attach_args *sa = args;
  125. bus_space_tag_t bt;
  126. bus_space_handle_t bh;
  127. int node, isconsole;
  128. char *nam;
  129. bt = sa->sa_bustag;
  130. node = sa->sa_node;
  131. nam = getpropstring(node, "model");
  132. printf(": model %s", nam);
  133. isconsole = node == fbnode;
  134. /*
  135. * Map the various beasts of this card we are interested in.
  136. */
  137. sc->sc_bustag = bt;
  138. sc->sc_paddr = sbus_bus_addr(bt, sa->sa_slot, sa->sa_offset);
  139. sc->sc_physoffset =
  140. (off_t)getpropint(node, "i128_fb_physaddr", 0x8000000);
  141. if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset + sc->sc_physoffset,
  142. getpropint(node, "i128_fb_size", 0x400000), BUS_SPACE_MAP_LINEAR,
  143. 0, &bh) != 0) {
  144. printf("\n%s: couldn't map video memory\n", self->dv_xname);
  145. return;
  146. }
  147. sc->sc_i128_fb = bus_space_vaddr(bt, bh);
  148. if (sbus_bus_map(bt, sa->sa_slot, sa->sa_offset +
  149. getpropint(node, "p9100_reg_physaddr", 0x10a0000), 0x4000,
  150. BUS_SPACE_MAP_LINEAR, 0, &bh) != 0) {
  151. printf("\n%s: couldn't map control registers\n", self->dv_xname);
  152. return;
  153. }
  154. sc->sc_p9100 = bus_space_vaddr(bt, bh);
  155. /*
  156. * For some reason the agten does not use the canonical name for
  157. * properties, but uses an ffb_ prefix; and the linebytes property is
  158. * missing.
  159. * The following is a specific version of
  160. * fb_setsize(&sc->sc_sunfb, 8, 1152, 900, node, BUS_SBUS);
  161. * using the correct property names.
  162. */
  163. #ifdef notyet
  164. sc->sc_sunfb.sf_depth = 32;
  165. #else
  166. sc->sc_sunfb.sf_depth = getpropint(node, "ffb_depth", 8);
  167. #endif
  168. sc->sc_sunfb.sf_width = getpropint(node, "ffb_width", 1152);
  169. sc->sc_sunfb.sf_height = getpropint(node, "ffb_height", 900);
  170. sc->sc_sunfb.sf_linebytes =
  171. roundup(sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_depth) *
  172. sc->sc_sunfb.sf_depth / 8;
  173. sc->sc_sunfb.sf_fbsize =
  174. sc->sc_sunfb.sf_height * sc->sc_sunfb.sf_linebytes;
  175. printf(", %dx%d, depth %d\n",
  176. sc->sc_sunfb.sf_width, sc->sc_sunfb.sf_height,
  177. sc->sc_sunfb.sf_depth);
  178. sc->sc_sunfb.sf_ro.ri_bits = (void *)sc->sc_i128_fb;
  179. sc->sc_sunfb.sf_ro.ri_hw = sc;
  180. fbwscons_init(&sc->sc_sunfb, 0, isconsole);
  181. fbwscons_setcolormap(&sc->sc_sunfb, agten_setcolor);
  182. if (isconsole)
  183. fbwscons_console_init(&sc->sc_sunfb, -1);
  184. fbwscons_attach(&sc->sc_sunfb, &agten_accessops, isconsole);
  185. }
  186. int
  187. agten_ioctl(dev, cmd, data, flags, p)
  188. void *dev;
  189. u_long cmd;
  190. caddr_t data;
  191. int flags;
  192. struct proc *p;
  193. {
  194. struct agten_softc *sc = dev;
  195. struct wsdisplay_cmap *cm;
  196. struct wsdisplay_fbinfo *wdf;
  197. int error;
  198. switch (cmd) {
  199. case WSDISPLAYIO_GTYPE:
  200. *(u_int *)data = WSDISPLAY_TYPE_AGTEN;
  201. break;
  202. case WSDISPLAYIO_GINFO:
  203. wdf = (struct wsdisplay_fbinfo *)data;
  204. wdf->height = sc->sc_sunfb.sf_height;
  205. wdf->width = sc->sc_sunfb.sf_width;
  206. wdf->depth = sc->sc_sunfb.sf_depth;
  207. wdf->cmsize = (sc->sc_sunfb.sf_depth == 8) ? 256 : 0;
  208. break;
  209. case WSDISPLAYIO_LINEBYTES:
  210. *(u_int *)data = sc->sc_sunfb.sf_linebytes;
  211. break;
  212. case WSDISPLAYIO_GETCMAP:
  213. if (sc->sc_sunfb.sf_depth == 8) {
  214. cm = (struct wsdisplay_cmap *)data;
  215. error = agten_getcmap(&sc->sc_cmap, cm);
  216. if (error)
  217. return (error);
  218. }
  219. break;
  220. case WSDISPLAYIO_PUTCMAP:
  221. if (sc->sc_sunfb.sf_depth == 8) {
  222. cm = (struct wsdisplay_cmap *)data;
  223. error = agten_putcmap(&sc->sc_cmap, cm);
  224. if (error)
  225. return (error);
  226. agten_loadcmap(sc, 0, 256);
  227. }
  228. break;
  229. case WSDISPLAYIO_SVIDEO:
  230. case WSDISPLAYIO_GVIDEO:
  231. break;
  232. case WSDISPLAYIO_GCURPOS:
  233. case WSDISPLAYIO_SCURPOS:
  234. case WSDISPLAYIO_GCURMAX:
  235. case WSDISPLAYIO_GCURSOR:
  236. case WSDISPLAYIO_SCURSOR:
  237. default:
  238. return (-1); /* not supported yet */
  239. }
  240. return (0);
  241. }
  242. /*
  243. * Return the address that would map the given device at the given
  244. * offset, allowing for the given protection, or return -1 for error.
  245. */
  246. paddr_t
  247. agten_mmap(v, offset, prot)
  248. void *v;
  249. off_t offset;
  250. int prot;
  251. {
  252. struct agten_softc *sc = v;
  253. if (offset & PGOFSET)
  254. return (-1);
  255. /* Allow mapping as a dumb framebuffer from offset 0 */
  256. if (offset >= 0 && offset < sc->sc_sunfb.sf_fbsize) {
  257. return (bus_space_mmap(sc->sc_bustag, sc->sc_paddr,
  258. sc->sc_physoffset + offset, prot, BUS_SPACE_MAP_LINEAR));
  259. }
  260. return (-1); /* not a user-map offset */
  261. }
  262. void
  263. agten_setcolor(v, index, r, g, b)
  264. void *v;
  265. u_int index;
  266. u_int8_t r, g, b;
  267. {
  268. struct agten_softc *sc = v;
  269. sc->sc_cmap.cm_red[index] = r;
  270. sc->sc_cmap.cm_green[index] = g;
  271. sc->sc_cmap.cm_blue[index] = b;
  272. agten_loadcmap(sc, index, 1);
  273. }
  274. int
  275. agten_getcmap(struct agten_cmap *cm, struct wsdisplay_cmap *rcm)
  276. {
  277. u_int index = rcm->index, count = rcm->count;
  278. int error;
  279. if (index >= 256 || count > 256 - index)
  280. return (EINVAL);
  281. if ((error = copyout(&cm->cm_red[index], rcm->red, count)) != 0)
  282. return (error);
  283. if ((error = copyout(&cm->cm_green[index], rcm->green, count)) != 0)
  284. return (error);
  285. if ((error = copyout(&cm->cm_blue[index], rcm->blue, count)) != 0)
  286. return (error);
  287. return (0);
  288. }
  289. int
  290. agten_putcmap(struct agten_cmap *cm, struct wsdisplay_cmap *rcm)
  291. {
  292. u_int index = rcm->index, count = rcm->count;
  293. int error;
  294. if (index >= 256 || count > 256 - index)
  295. return (EINVAL);
  296. if ((error = copyin(rcm->red, &cm->cm_red[index], count)) != 0)
  297. return (error);
  298. if ((error = copyin(rcm->green, &cm->cm_green[index], count)) != 0)
  299. return (error);
  300. if ((error = copyin(rcm->blue, &cm->cm_blue[index], count)) != 0)
  301. return (error);
  302. return (0);
  303. }
  304. static __inline__ void
  305. ibm561_write(struct agten_softc *sc, u_int32_t reg, u_int32_t value)
  306. {
  307. /*
  308. * For some design reason the IBM561 PaletteDac needs to be fed
  309. * values shifted left by 16 bits. What happened to simplicity?
  310. */
  311. *(volatile u_int32_t *)(sc->sc_p9100 + P9100_RAMDAC_REGISTER(reg)) =
  312. (value) << 16;
  313. }
  314. void
  315. agten_loadcmap(struct agten_softc *sc, u_int start, u_int ncolors)
  316. {
  317. int i;
  318. u_int8_t *red, *green, *blue;
  319. ibm561_write(sc, IBM561_ADDR_LOW,
  320. (IBM561_CMAP_TABLE + start) & 0xff);
  321. ibm561_write(sc, IBM561_ADDR_HIGH,
  322. ((IBM561_CMAP_TABLE + start) >> 8) & 0xff);
  323. red = sc->sc_cmap.cm_red;
  324. green = sc->sc_cmap.cm_green;
  325. blue = sc->sc_cmap.cm_blue;
  326. for (i = start; i < start + ncolors; i++) {
  327. ibm561_write(sc, IBM561_CMD_CMAP, *red++);
  328. ibm561_write(sc, IBM561_CMD_CMAP, *green++);
  329. ibm561_write(sc, IBM561_CMD_CMAP, *blue++);
  330. }
  331. }