Home | History | Annotate | Line # | Download | only in ic
hd44780_subr.c revision 1.13
      1  1.13   tsutsui /* $NetBSD: hd44780_subr.c,v 1.13 2007/11/01 13:05:32 tsutsui Exp $ */
      2   1.1     soren 
      3   1.1     soren /*
      4   1.1     soren  * Copyright (c) 2002 Dennis I. Chernoivanov
      5   1.1     soren  * All rights reserved.
      6   1.1     soren  *
      7   1.1     soren  * Redistribution and use in source and binary forms, with or without
      8   1.1     soren  * modification, are permitted provided that the following conditions
      9   1.1     soren  * are met:
     10   1.1     soren  * 1. Redistributions of source code must retain the above copyright
     11   1.1     soren  *    notice, this list of conditions and the following disclaimer.
     12   1.1     soren  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1     soren  *    notice, this list of conditions and the following disclaimer in the
     14   1.1     soren  *    documentation and/or other materials provided with the distribution.
     15   1.1     soren  * 3. The name of the author may not be used to endorse or promote products
     16   1.1     soren  *    derived from this software without specific prior written permission
     17   1.1     soren  *
     18   1.1     soren  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1     soren  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1     soren  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1     soren  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1     soren  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1     soren  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1     soren  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1     soren  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1     soren  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1     soren  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1     soren  */
     29   1.1     soren 
     30   1.1     soren /*
     31   1.1     soren  * Subroutines for Hitachi HD44870 style displays
     32   1.1     soren  */
     33   1.1     soren 
     34   1.1     soren #include <sys/cdefs.h>
     35  1.13   tsutsui __KERNEL_RCSID(0, "$NetBSD: hd44780_subr.c,v 1.13 2007/11/01 13:05:32 tsutsui Exp $");
     36   1.1     soren 
     37   1.1     soren #include <sys/param.h>
     38   1.1     soren #include <sys/systm.h>
     39   1.1     soren #include <sys/conf.h>
     40   1.1     soren #include <sys/kernel.h>
     41   1.5      joff #include <sys/malloc.h>
     42   1.1     soren #include <sys/types.h>
     43   1.1     soren #include <sys/ioccom.h>
     44   1.1     soren 
     45   1.1     soren #include <machine/autoconf.h>
     46  1.12        ad #include <sys/intr.h>
     47  1.12        ad #include <sys/bus.h>
     48   1.1     soren 
     49   1.5      joff #include <uvm/uvm_extern.h>
     50   1.5      joff 
     51   1.5      joff #include <dev/wscons/wsdisplayvar.h>
     52   1.5      joff #include <dev/wscons/wsconsio.h>
     53   1.5      joff #include <dev/wscons/wscons_callbacks.h>
     54   1.5      joff 
     55   1.1     soren #include <dev/ic/hd44780reg.h>
     56   1.4      joff #include <dev/ic/hd44780var.h>
     57   1.1     soren 
     58   1.5      joff #define COORD_TO_IDX(x, y)	((y) * sc->sc_cols + (x))
     59   1.5      joff #define COORD_TO_DADDR(x, y)	((y) * HD_ROW2_ADDR + (x))
     60   1.7      joff #define IDX_TO_ROW(idx)		((idx) / sc->sc_cols)
     61   1.7      joff #define IDX_TO_COL(idx)		((idx) % sc->sc_cols)
     62   1.7      joff #define IDX_TO_DADDR(idx)	(IDX_TO_ROW((idx)) * HD_ROW2_ADDR + \
     63   1.7      joff 				IDX_TO_COL((idx)))
     64   1.7      joff #define DADDR_TO_ROW(daddr)	((daddr) / HD_ROW2_ADDR)
     65   1.7      joff #define DADDR_TO_COL(daddr)	((daddr) % HD_ROW2_ADDR)
     66   1.7      joff #define DADDR_TO_CHIPDADDR(daddr)	((daddr) % (HD_ROW2_ADDR * 2))
     67   1.7      joff #define DADDR_TO_CHIPNO(daddr)	((daddr) / (HD_ROW2_ADDR * 2))
     68   1.5      joff 
     69   1.5      joff static void	hlcd_cursor(void *, int, int, int);
     70   1.5      joff static int	hlcd_mapchar(void *, int, unsigned int *);
     71   1.5      joff static void	hlcd_putchar(void *, int, int, u_int, long);
     72   1.5      joff static void	hlcd_copycols(void *, int, int, int,int);
     73   1.5      joff static void	hlcd_erasecols(void *, int, int, int, long);
     74   1.5      joff static void	hlcd_copyrows(void *, int, int, int);
     75   1.5      joff static void	hlcd_eraserows(void *, int, int, long);
     76   1.5      joff static int	hlcd_allocattr(void *, int, int, int, long *);
     77   1.5      joff static void	hlcd_updatechar(struct hd44780_chip *, int, int);
     78   1.5      joff static void	hlcd_redraw(void *);
     79   1.5      joff 
     80   1.5      joff const struct wsdisplay_emulops hlcd_emulops = {
     81   1.5      joff 	hlcd_cursor,
     82   1.5      joff 	hlcd_mapchar,
     83   1.5      joff 	hlcd_putchar,
     84   1.5      joff 	hlcd_copycols,
     85   1.5      joff 	hlcd_erasecols,
     86   1.5      joff 	hlcd_copyrows,
     87   1.5      joff 	hlcd_eraserows,
     88   1.5      joff 	hlcd_allocattr
     89   1.5      joff };
     90   1.5      joff 
     91  1.10  christos static int	hlcd_ioctl(void *, void *, u_long, void *, int, struct lwp *);
     92   1.9      jmmv static paddr_t	hlcd_mmap(void *, void *, off_t, int);
     93   1.5      joff static int	hlcd_alloc_screen(void *, const struct wsscreen_descr *,
     94   1.5      joff 		    void **, int *, int *, long *);
     95   1.5      joff static void	hlcd_free_screen(void *, void *);
     96   1.5      joff static int	hlcd_show_screen(void *, void *, int,
     97   1.5      joff 		    void (*) (void *, int, int), void *);
     98   1.5      joff 
     99   1.5      joff const struct wsdisplay_accessops hlcd_accessops = {
    100   1.5      joff 	hlcd_ioctl,
    101   1.5      joff 	hlcd_mmap,
    102   1.5      joff 	hlcd_alloc_screen,
    103   1.5      joff 	hlcd_free_screen,
    104   1.5      joff 	hlcd_show_screen,
    105   1.5      joff 	0 /* load_font */
    106   1.5      joff };
    107   1.5      joff 
    108   1.5      joff static void
    109   1.5      joff hlcd_cursor(id, on, row, col)
    110   1.5      joff 	void *id;
    111   1.5      joff 	int on, row, col;
    112   1.5      joff {
    113   1.5      joff 	struct hlcd_screen *hdscr = id;
    114   1.5      joff 
    115   1.5      joff 	hdscr->hlcd_curon = on;
    116   1.5      joff 	hdscr->hlcd_curx = col;
    117   1.5      joff 	hdscr->hlcd_cury = row;
    118   1.5      joff }
    119   1.5      joff 
    120   1.5      joff static int
    121   1.5      joff hlcd_mapchar(id, uni, index)
    122   1.5      joff 	void *id;
    123   1.5      joff 	int uni;
    124   1.5      joff 	unsigned int *index;
    125   1.5      joff {
    126   1.5      joff 	if (uni < 256) {
    127   1.5      joff 		*index = uni;
    128   1.5      joff 		return (5);
    129   1.5      joff 	}
    130   1.5      joff 	*index = ' ';
    131   1.5      joff 	return (0);
    132   1.5      joff }
    133   1.5      joff 
    134   1.5      joff static void
    135   1.5      joff hlcd_putchar(id, row, col, c, attr)
    136   1.5      joff 	void *id;
    137   1.5      joff 	int row, col;
    138   1.5      joff 	u_int c;
    139   1.5      joff 	long attr;
    140   1.5      joff {
    141   1.5      joff 	struct hlcd_screen *hdscr = id;
    142   1.6     perry 
    143   1.5      joff 	c &= 0xff;
    144   1.7      joff 	if (row > 0 && (hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    145   1.5      joff 		hdscr->image[hdscr->hlcd_sc->sc_cols * row + col] = c;
    146   1.5      joff 	else
    147   1.5      joff 		hdscr->image[col] = c;
    148   1.5      joff }
    149   1.5      joff 
    150   1.5      joff /*
    151   1.5      joff  * copies columns inside a row.
    152   1.5      joff  */
    153   1.5      joff static void
    154   1.5      joff hlcd_copycols(id, row, srccol, dstcol, ncols)
    155   1.5      joff 	void *id;
    156   1.5      joff 	int row, srccol, dstcol, ncols;
    157   1.5      joff {
    158   1.5      joff 	struct hlcd_screen *hdscr = id;
    159   1.5      joff 
    160   1.5      joff 	if ((dstcol + ncols - 1) > hdscr->hlcd_sc->sc_cols)
    161   1.5      joff 		ncols = hdscr->hlcd_sc->sc_cols - srccol;
    162   1.5      joff 
    163   1.7      joff 	if (row > 0 && (hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    164  1.13   tsutsui 		memmove(&hdscr->image[hdscr->hlcd_sc->sc_cols * row + dstcol],
    165  1.13   tsutsui 		    &hdscr->image[hdscr->hlcd_sc->sc_cols * row + srccol],
    166  1.13   tsutsui 		    ncols);
    167   1.5      joff 	else
    168  1.13   tsutsui 		memmove(&hdscr->image[dstcol], &hdscr->image[srccol], ncols);
    169   1.5      joff }
    170   1.5      joff 
    171   1.5      joff 
    172   1.5      joff /*
    173   1.5      joff  * Erases a bunch of chars inside one row.
    174   1.5      joff  */
    175   1.5      joff static void
    176   1.5      joff hlcd_erasecols(id, row, startcol, ncols, fillattr)
    177   1.5      joff 	void *id;
    178   1.5      joff 	int row, startcol, ncols;
    179   1.5      joff 	long fillattr;
    180   1.5      joff {
    181   1.5      joff 	struct hlcd_screen *hdscr = id;
    182   1.5      joff 
    183   1.5      joff 	if ((startcol + ncols) > hdscr->hlcd_sc->sc_cols)
    184   1.5      joff 		ncols = hdscr->hlcd_sc->sc_cols - startcol;
    185   1.5      joff 
    186   1.7      joff 	if (row > 0 && (hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    187   1.6     perry 		memset(&hdscr->image[hdscr->hlcd_sc->sc_cols * row + startcol],
    188   1.5      joff 		    ' ', ncols);
    189   1.5      joff 	else
    190   1.5      joff 		memset(&hdscr->image[startcol], ' ', ncols);
    191   1.5      joff }
    192   1.5      joff 
    193   1.5      joff 
    194   1.5      joff static void
    195   1.5      joff hlcd_copyrows(id, srcrow, dstrow, nrows)
    196   1.5      joff 	void *id;
    197   1.5      joff 	int srcrow, dstrow, nrows;
    198   1.5      joff {
    199   1.5      joff 	struct hlcd_screen *hdscr = id;
    200   1.5      joff 	int ncols = hdscr->hlcd_sc->sc_cols;
    201   1.6     perry 
    202   1.7      joff 	if (!(hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    203   1.5      joff 		return;
    204  1.13   tsutsui 	memmove(&hdscr->image[dstrow * ncols], &hdscr->image[srcrow * ncols],
    205   1.5      joff 	    nrows * ncols);
    206   1.5      joff }
    207   1.5      joff 
    208   1.5      joff static void
    209   1.5      joff hlcd_eraserows(id, startrow, nrows, fillattr)
    210   1.5      joff 	void *id;
    211   1.5      joff 	int startrow, nrows;
    212   1.5      joff 	long fillattr;
    213   1.5      joff {
    214   1.5      joff 	struct hlcd_screen *hdscr = id;
    215   1.5      joff 	int ncols = hdscr->hlcd_sc->sc_cols;
    216   1.5      joff 
    217   1.5      joff 	memset(&hdscr->image[startrow * ncols], ' ', ncols * nrows);
    218   1.5      joff }
    219   1.5      joff 
    220   1.5      joff 
    221   1.5      joff static int
    222   1.5      joff hlcd_allocattr(id, fg, bg, flags, attrp)
    223   1.5      joff 	void *id;
    224   1.5      joff 	int fg, bg, flags;
    225   1.5      joff 	long *attrp;
    226   1.5      joff {
    227   1.5      joff         *attrp = flags;
    228   1.5      joff         return 0;
    229   1.5      joff }
    230   1.5      joff 
    231   1.5      joff static int
    232   1.9      jmmv hlcd_ioctl(v, vs, cmd, data, flag, l)
    233   1.5      joff 	void *v;
    234   1.9      jmmv 	void *vs;
    235   1.5      joff 	u_long cmd;
    236  1.10  christos 	void *data;
    237   1.5      joff 	int flag;
    238   1.8  christos 	struct lwp *l;
    239   1.5      joff {
    240   1.5      joff 
    241   1.5      joff 	switch (cmd) {
    242   1.5      joff 	case WSDISPLAYIO_GTYPE:
    243   1.5      joff 		*(u_int *)data = WSDISPLAY_TYPE_HDLCD;
    244   1.5      joff 		break;
    245   1.5      joff 
    246   1.5      joff 	case WSDISPLAYIO_SVIDEO:
    247   1.5      joff 		break;
    248   1.5      joff 
    249   1.5      joff 	case WSDISPLAYIO_GVIDEO:
    250   1.5      joff 		*(u_int *)data = WSDISPLAYIO_VIDEO_ON;
    251   1.5      joff 		break;
    252   1.5      joff 
    253   1.5      joff 	default:
    254   1.5      joff 		return EPASSTHROUGH;
    255   1.5      joff 	}
    256   1.5      joff 	return 0;
    257   1.5      joff }
    258   1.5      joff 
    259   1.5      joff static paddr_t
    260   1.9      jmmv hlcd_mmap(v, vs, offset, prot)
    261   1.5      joff 	void *v;
    262   1.9      jmmv 	void *vs;
    263   1.5      joff 	off_t offset;
    264   1.5      joff 	int prot;
    265   1.5      joff {
    266   1.5      joff 	return -1;
    267   1.5      joff }
    268   1.5      joff 
    269   1.5      joff static int
    270   1.5      joff hlcd_alloc_screen(v, type, cookiep, curxp, curyp, defattrp)
    271   1.5      joff 	void *v;
    272   1.5      joff 	const struct wsscreen_descr *type;
    273   1.5      joff 	void **cookiep;
    274   1.5      joff 	int *curxp, *curyp;
    275   1.5      joff 	long *defattrp;
    276   1.5      joff {
    277   1.5      joff 	struct hlcd_screen *hdscr = v, *new;
    278   1.5      joff 
    279   1.5      joff 	new = *cookiep = malloc(sizeof(struct hlcd_screen), M_DEVBUF, M_WAITOK);
    280   1.5      joff 	bzero(*cookiep, sizeof(struct hlcd_screen));
    281   1.5      joff 	new->hlcd_sc = hdscr->hlcd_sc;
    282   1.5      joff 	new->image = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
    283   1.5      joff 	memset(new->image, ' ', PAGE_SIZE);
    284   1.5      joff 	*curxp = *curyp = *defattrp = 0;
    285   1.5      joff 	return 0;
    286   1.5      joff }
    287   1.5      joff 
    288   1.5      joff static void
    289   1.5      joff hlcd_free_screen(v, cookie)
    290   1.5      joff 	void *v, *cookie;
    291   1.5      joff {
    292   1.5      joff }
    293   1.5      joff 
    294   1.5      joff static int
    295   1.5      joff hlcd_show_screen(v, cookie, waitok, cb, cbarg)
    296   1.5      joff 	void *v, *cookie, *cbarg;
    297   1.5      joff 	int waitok;
    298   1.5      joff 	void (*cb)(void *, int, int);
    299   1.5      joff {
    300   1.5      joff 	struct hlcd_screen *hdscr = v;
    301   1.5      joff 
    302   1.5      joff 	hdscr->hlcd_sc->sc_curscr = cookie;
    303   1.5      joff 	callout_schedule(&hdscr->hlcd_sc->redraw, 1);
    304   1.5      joff 	return (0);
    305   1.5      joff }
    306   1.5      joff 
    307   1.5      joff static void
    308   1.5      joff hlcd_updatechar(sc, daddr, c)
    309   1.5      joff 	struct hd44780_chip *sc;
    310   1.5      joff 	int daddr, c;
    311   1.5      joff {
    312   1.7      joff 	int curdaddr, en, chipdaddr;
    313   1.5      joff 
    314   1.5      joff 	curdaddr = COORD_TO_DADDR(sc->sc_screen.hlcd_curx,
    315   1.5      joff 	    sc->sc_screen.hlcd_cury);
    316   1.7      joff 	en = DADDR_TO_CHIPNO(daddr);
    317   1.7      joff 	chipdaddr = DADDR_TO_CHIPDADDR(daddr);
    318   1.5      joff 	if (daddr != curdaddr)
    319   1.7      joff 		hd44780_ir_write(sc, en, cmd_ddramset(chipdaddr));
    320   1.5      joff 
    321   1.7      joff 	hd44780_dr_write(sc, en, c);
    322   1.5      joff 
    323   1.5      joff 	daddr++;
    324   1.5      joff 	sc->sc_screen.hlcd_curx = DADDR_TO_COL(daddr);
    325   1.5      joff 	sc->sc_screen.hlcd_cury = DADDR_TO_ROW(daddr);
    326   1.5      joff }
    327   1.5      joff 
    328   1.5      joff static void
    329   1.5      joff hlcd_redraw(arg)
    330   1.5      joff 	void *arg;
    331   1.5      joff {
    332   1.5      joff 	struct hd44780_chip *sc = arg;
    333   1.5      joff 	int len, crsridx, startidx, x, y;
    334   1.7      joff 	int old_en, new_en;
    335   1.5      joff 	u_char *img, *curimg;
    336   1.6     perry 
    337   1.5      joff 	if (sc->sc_curscr == NULL)
    338   1.5      joff 		return;
    339   1.5      joff 
    340   1.5      joff 	if (sc->sc_flags & HD_MULTILINE)
    341   1.5      joff 		len = 2 * sc->sc_cols;
    342   1.5      joff 	else
    343   1.5      joff 		len = sc->sc_cols;
    344   1.5      joff 
    345   1.7      joff 	if (sc->sc_flags & HD_MULTICHIP)
    346   1.7      joff 		len = len * 2;
    347   1.7      joff 
    348   1.7      joff 	x = sc->sc_screen.hlcd_curx;
    349   1.7      joff 	y = sc->sc_screen.hlcd_cury;
    350   1.7      joff 	old_en = DADDR_TO_CHIPNO(COORD_TO_DADDR(x, y));
    351   1.7      joff 
    352   1.5      joff 	img = sc->sc_screen.image;
    353   1.5      joff 	curimg = sc->sc_curscr->image;
    354   1.5      joff 	startidx = crsridx =
    355   1.5      joff 	    COORD_TO_IDX(sc->sc_screen.hlcd_curx, sc->sc_screen.hlcd_cury);
    356   1.5      joff 	do {
    357   1.5      joff 		if (img[crsridx] != curimg[crsridx]) {
    358   1.7      joff 			hlcd_updatechar(sc, IDX_TO_DADDR(crsridx),
    359   1.5      joff 			    curimg[crsridx]);
    360   1.5      joff 			img[crsridx] = curimg[crsridx];
    361   1.5      joff 		}
    362   1.5      joff 		crsridx++;
    363   1.5      joff 		if (crsridx == len)
    364   1.5      joff 			crsridx = 0;
    365   1.5      joff 	} while (crsridx != startidx);
    366   1.6     perry 
    367   1.7      joff 	x = sc->sc_curscr->hlcd_curx;
    368   1.7      joff 	y = sc->sc_curscr->hlcd_cury;
    369   1.7      joff 	new_en = DADDR_TO_CHIPNO(COORD_TO_DADDR(x, y));
    370   1.7      joff 
    371   1.5      joff 	if (sc->sc_screen.hlcd_curx != sc->sc_curscr->hlcd_curx ||
    372   1.5      joff 	    sc->sc_screen.hlcd_cury != sc->sc_curscr->hlcd_cury) {
    373   1.7      joff 
    374   1.5      joff 		x = sc->sc_screen.hlcd_curx = sc->sc_curscr->hlcd_curx;
    375   1.5      joff 		y = sc->sc_screen.hlcd_cury = sc->sc_curscr->hlcd_cury;
    376   1.7      joff 
    377   1.7      joff 		hd44780_ir_write(sc, new_en, cmd_ddramset(
    378   1.7      joff 		    DADDR_TO_CHIPDADDR(COORD_TO_DADDR(x, y))));
    379   1.7      joff 
    380   1.7      joff 	}
    381   1.7      joff 
    382   1.7      joff 	/* visible cursor switched to other chip */
    383   1.7      joff 	if (old_en != new_en && sc->sc_screen.hlcd_curon) {
    384   1.7      joff 		hd44780_ir_write(sc, old_en, cmd_dispctl(1, 0, 0));
    385   1.7      joff 		hd44780_ir_write(sc, new_en, cmd_dispctl(1, 1, 1));
    386   1.5      joff 	}
    387   1.5      joff 
    388   1.5      joff 	if (sc->sc_screen.hlcd_curon != sc->sc_curscr->hlcd_curon) {
    389   1.5      joff 		sc->sc_screen.hlcd_curon = sc->sc_curscr->hlcd_curon;
    390   1.5      joff 		if (sc->sc_screen.hlcd_curon)
    391   1.7      joff 			hd44780_ir_write(sc, new_en, cmd_dispctl(1, 1, 1));
    392   1.5      joff 		else
    393   1.7      joff 			hd44780_ir_write(sc, new_en, cmd_dispctl(1, 0, 0));
    394   1.5      joff 	}
    395   1.5      joff 
    396   1.5      joff 	callout_schedule(&sc->redraw, 1);
    397   1.5      joff }
    398   1.5      joff 
    399   1.5      joff 
    400   1.1     soren /*
    401   1.1     soren  * Finish device attach. sc_writereg, sc_readreg and sc_flags must be properly
    402   1.1     soren  * initialized prior to this call.
    403   1.1     soren  */
    404   1.1     soren void
    405   1.1     soren hd44780_attach_subr(sc)
    406   1.1     soren 	struct hd44780_chip *sc;
    407   1.1     soren {
    408   1.3      joff 	int err = 0;
    409   1.1     soren 	/* Putc/getc are supposed to be set by platform-dependent code. */
    410   1.2      joff 	if ((sc->sc_writereg == NULL) || (sc->sc_readreg == NULL))
    411   1.1     soren 		sc->sc_dev_ok = 0;
    412   1.1     soren 
    413   1.1     soren 	/* Make sure that HD_MAX_CHARS is enough. */
    414   1.5      joff 	if ((sc->sc_flags & HD_MULTILINE) && (2 * sc->sc_cols > HD_MAX_CHARS))
    415   1.1     soren 		sc->sc_dev_ok = 0;
    416   1.5      joff 	else if (sc->sc_cols > HD_MAX_CHARS)
    417   1.1     soren 		sc->sc_dev_ok = 0;
    418   1.1     soren 
    419   1.1     soren 	if (sc->sc_dev_ok) {
    420   1.6     perry 		if ((sc->sc_flags & HD_UP) == 0)
    421   1.3      joff 			err = hd44780_init(sc);
    422   1.3      joff 		if (err != 0)
    423   1.4      joff 			printf("%s: LCD not responding or unconnected\n", sc->sc_dev->dv_xname);
    424   1.1     soren 
    425   1.1     soren 	}
    426   1.5      joff 
    427   1.5      joff 	sc->sc_screen.hlcd_sc = sc;
    428   1.6     perry 
    429   1.5      joff 	sc->sc_screen.image = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
    430   1.5      joff 	memset(sc->sc_screen.image, ' ', PAGE_SIZE);
    431   1.5      joff 	sc->sc_curscr = NULL;
    432   1.7      joff 	sc->sc_curchip = 0;
    433  1.11        ad 	callout_init(&sc->redraw, 0);
    434   1.5      joff 	callout_setfunc(&sc->redraw, hlcd_redraw, sc);
    435   1.1     soren }
    436   1.1     soren 
    437   1.7      joff int hd44780_init(sc)
    438   1.7      joff 	struct hd44780_chip *sc;
    439   1.7      joff {
    440   1.7      joff 	int ret;
    441   1.7      joff 
    442   1.7      joff 	ret = hd44780_chipinit(sc, 0);
    443   1.7      joff 	if (ret != 0 || !(sc->sc_flags & HD_MULTICHIP)) return ret;
    444   1.7      joff 	else return hd44780_chipinit(sc, 1);
    445   1.7      joff }
    446   1.7      joff 
    447   1.1     soren /*
    448   1.1     soren  * Initialize 4-bit or 8-bit connected device.
    449   1.1     soren  */
    450   1.3      joff int
    451   1.7      joff hd44780_chipinit(sc, en)
    452   1.1     soren 	struct hd44780_chip *sc;
    453   1.7      joff 	u_int32_t en;
    454   1.1     soren {
    455   1.3      joff 	u_int8_t cmd, dat;
    456   1.1     soren 
    457   1.3      joff 	sc->sc_flags &= ~(HD_TIMEDOUT|HD_UP);
    458   1.3      joff 	sc->sc_dev_ok = 1;
    459   1.7      joff 
    460   1.1     soren 	cmd = cmd_init(sc->sc_flags & HD_8BIT);
    461   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    462   1.2      joff 	delay(HD_TIMEOUT_LONG);
    463   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    464   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    465   1.1     soren 
    466   1.1     soren 	cmd = cmd_funcset(
    467   1.1     soren 			sc->sc_flags & HD_8BIT,
    468   1.1     soren 			sc->sc_flags & HD_MULTILINE,
    469   1.1     soren 			sc->sc_flags & HD_BIGFONT);
    470   1.1     soren 
    471   1.1     soren 	if ((sc->sc_flags & HD_8BIT) == 0)
    472   1.7      joff 		hd44780_ir_write(sc, en, cmd);
    473   1.1     soren 
    474   1.2      joff 	sc->sc_flags |= HD_UP;
    475   1.2      joff 
    476   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    477   1.7      joff 	hd44780_ir_write(sc, en, cmd_dispctl(0, 0, 0));
    478   1.7      joff 	hd44780_ir_write(sc, en, cmd_clear());
    479   1.7      joff 	hd44780_ir_write(sc, en, cmd_modset(1, 0));
    480   1.3      joff 
    481   1.3      joff 	if (sc->sc_flags & HD_TIMEDOUT) {
    482   1.3      joff 		sc->sc_flags &= ~HD_UP;
    483   1.3      joff 		return EIO;
    484   1.6     perry 	}
    485   1.3      joff 
    486   1.3      joff 	/* Turn display on and clear it. */
    487   1.7      joff 	hd44780_ir_write(sc, en, cmd_clear());
    488   1.7      joff 	hd44780_ir_write(sc, en, cmd_dispctl(1, 0, 0));
    489   1.3      joff 
    490   1.3      joff 	/* Attempt a simple probe for presence */
    491   1.7      joff 	hd44780_ir_write(sc, en, cmd_ddramset(0x5));
    492   1.7      joff 	hd44780_ir_write(sc, en, cmd_shift(0, 1));
    493   1.7      joff 	hd44780_busy_wait(sc, en);
    494   1.7      joff 	if ((dat = hd44780_ir_read(sc, en) & 0x7f) != 0x6) {
    495   1.3      joff 		sc->sc_dev_ok = 0;
    496   1.3      joff 		sc->sc_flags &= ~HD_UP;
    497   1.3      joff 		return EIO;
    498   1.3      joff 	}
    499   1.7      joff 	hd44780_ir_write(sc, en, cmd_ddramset(0));
    500   1.3      joff 
    501   1.3      joff 	return 0;
    502   1.1     soren }
    503   1.1     soren 
    504   1.1     soren /*
    505   1.1     soren  * Standard hd44780 ioctl() functions.
    506   1.1     soren  */
    507   1.1     soren int
    508   1.1     soren hd44780_ioctl_subr(sc, cmd, data)
    509   1.1     soren 	struct hd44780_chip *sc;
    510   1.1     soren 	u_long cmd;
    511  1.10  christos 	void *data;
    512   1.1     soren {
    513   1.1     soren 	u_int8_t tmp;
    514   1.1     soren 	int error = 0;
    515   1.7      joff 	u_int32_t en = sc->sc_curchip;
    516   1.1     soren 
    517   1.1     soren #define hd44780_io()	((struct hd44780_io *)data)
    518   1.1     soren #define hd44780_info()	((struct hd44780_info*)data)
    519   1.1     soren #define hd44780_ctrl()	((struct hd44780_dispctl*)data)
    520   1.1     soren 
    521   1.1     soren 	switch (cmd) {
    522   1.1     soren 		/* Clear the LCD. */
    523   1.1     soren 		case HLCD_CLEAR:
    524   1.7      joff 			hd44780_ir_write(sc, en, cmd_clear());
    525   1.1     soren 			break;
    526   1.1     soren 
    527   1.1     soren 		/* Move the cursor one position to the left. */
    528   1.1     soren 		case HLCD_CURSOR_LEFT:
    529   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(0, 0));
    530   1.1     soren 			break;
    531   1.1     soren 
    532   1.1     soren 		/* Move the cursor one position to the right. */
    533   1.1     soren 		case HLCD_CURSOR_RIGHT:
    534   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(0, 1));
    535   1.6     perry 			break;
    536   1.1     soren 
    537   1.1     soren 		/* Control the LCD. */
    538   1.1     soren 		case HLCD_DISPCTL:
    539   1.7      joff 			hd44780_ir_write(sc, en, cmd_dispctl(
    540   1.1     soren 						hd44780_ctrl()->display_on,
    541   1.1     soren 						hd44780_ctrl()->cursor_on,
    542   1.1     soren 						hd44780_ctrl()->blink_on));
    543   1.1     soren 			break;
    544   1.1     soren 
    545   1.1     soren 		/* Get LCD configuration. */
    546   1.1     soren 		case HLCD_GET_INFO:
    547   1.1     soren 			hd44780_info()->lines
    548   1.1     soren 				= (sc->sc_flags & HD_MULTILINE) ? 2 : 1;
    549   1.7      joff 			if (sc->sc_flags & HD_MULTICHIP)
    550   1.7      joff 				hd44780_info()->lines *= 2;
    551   1.5      joff 			hd44780_info()->phys_rows = sc->sc_cols;
    552   1.5      joff 			hd44780_info()->virt_rows = sc->sc_vcols;
    553   1.1     soren 			hd44780_info()->is_wide = sc->sc_flags & HD_8BIT;
    554   1.1     soren 			hd44780_info()->is_bigfont = sc->sc_flags & HD_BIGFONT;
    555   1.1     soren 			hd44780_info()->kp_present = sc->sc_flags & HD_KEYPAD;
    556   1.1     soren 			break;
    557   1.1     soren 
    558   1.1     soren 
    559   1.1     soren 		/* Reset the LCD. */
    560   1.1     soren 		case HLCD_RESET:
    561   1.3      joff 			error = hd44780_init(sc);
    562   1.1     soren 			break;
    563   1.1     soren 
    564   1.1     soren 		/* Get the current cursor position. */
    565   1.1     soren 		case HLCD_GET_CURSOR_POS:
    566   1.7      joff 			hd44780_io()->dat = (hd44780_ir_read(sc, en) & 0x7f);
    567   1.1     soren 			break;
    568   1.1     soren 
    569   1.1     soren 		/* Set the cursor position. */
    570   1.1     soren 		case HLCD_SET_CURSOR_POS:
    571   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(hd44780_io()->dat));
    572   1.1     soren 			break;
    573   1.1     soren 
    574   1.1     soren 		/* Get the value at the current cursor position. */
    575   1.1     soren 		case HLCD_GETC:
    576   1.7      joff 			tmp = (hd44780_ir_read(sc, en) & 0x7f);
    577   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(tmp));
    578   1.7      joff 			hd44780_io()->dat = hd44780_dr_read(sc, en);
    579   1.1     soren 			break;
    580   1.1     soren 
    581   1.1     soren 		/* Set the character at the cursor position + advance cursor. */
    582   1.1     soren 		case HLCD_PUTC:
    583   1.7      joff 			hd44780_dr_write(sc, en, hd44780_io()->dat);
    584   1.1     soren 			break;
    585   1.1     soren 
    586   1.1     soren 		/* Shift display left. */
    587   1.1     soren 		case HLCD_SHIFT_LEFT:
    588   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(1, 0));
    589   1.1     soren 			break;
    590   1.1     soren 
    591   1.1     soren 		/* Shift display right. */
    592   1.1     soren 		case HLCD_SHIFT_RIGHT:
    593   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(1, 1));
    594   1.1     soren 			break;
    595   1.1     soren 
    596   1.1     soren 		/* Return home. */
    597   1.1     soren 		case HLCD_HOME:
    598   1.7      joff 			hd44780_ir_write(sc, en, cmd_rethome());
    599   1.1     soren 			break;
    600   1.1     soren 
    601   1.1     soren 		/* Write a string to the LCD virtual area. */
    602   1.1     soren 		case HLCD_WRITE:
    603   1.7      joff 			error = hd44780_ddram_io(sc, en, hd44780_io(), HD_DDRAM_WRITE);
    604   1.6     perry 			break;
    605   1.1     soren 
    606   1.1     soren 		/* Read LCD virtual area. */
    607   1.1     soren 		case HLCD_READ:
    608   1.7      joff 			error = hd44780_ddram_io(sc, en, hd44780_io(), HD_DDRAM_READ);
    609   1.1     soren 			break;
    610   1.1     soren 
    611   1.1     soren 		/* Write to the LCD visible area. */
    612   1.1     soren 		case HLCD_REDRAW:
    613   1.7      joff 			hd44780_ddram_redraw(sc, en, hd44780_io());
    614   1.1     soren 			break;
    615   1.1     soren 
    616   1.1     soren 		/* Write raw instruction. */
    617   1.1     soren 		case HLCD_WRITE_INST:
    618   1.7      joff 			hd44780_ir_write(sc, en, hd44780_io()->dat);
    619   1.1     soren 			break;
    620   1.1     soren 
    621   1.1     soren 		/* Write raw data. */
    622   1.1     soren 		case HLCD_WRITE_DATA:
    623   1.7      joff 			hd44780_dr_write(sc, en, hd44780_io()->dat);
    624   1.7      joff 			break;
    625   1.7      joff 
    626   1.7      joff 		/* Get current chip 0 or 1 (top or bottom) */
    627   1.7      joff 		case HLCD_GET_CHIPNO:
    628   1.7      joff 			*(u_int8_t *)data = sc->sc_curchip;
    629   1.7      joff 			break;
    630   1.7      joff 
    631   1.7      joff 		/* Set current chip 0 or 1 (top or bottom) */
    632   1.7      joff 		case HLCD_SET_CHIPNO:
    633   1.7      joff 			sc->sc_curchip = *(u_int8_t *)data;
    634   1.1     soren 			break;
    635   1.1     soren 
    636   1.1     soren 		default:
    637   1.1     soren 			error = EINVAL;
    638   1.1     soren 	}
    639   1.1     soren 
    640   1.3      joff 	if (sc->sc_flags & HD_TIMEDOUT)
    641   1.3      joff 		error = EIO;
    642   1.3      joff 
    643   1.1     soren 	return error;
    644   1.1     soren }
    645   1.1     soren 
    646   1.1     soren /*
    647   1.1     soren  * Read/write particular area of the LCD screen.
    648   1.1     soren  */
    649   1.1     soren int
    650   1.7      joff hd44780_ddram_io(sc, en, io, dir)
    651   1.1     soren 	struct hd44780_chip *sc;
    652   1.7      joff 	u_int32_t en;
    653   1.1     soren 	struct hd44780_io *io;
    654   1.1     soren 	u_char dir;
    655   1.1     soren {
    656   1.1     soren 	u_int8_t hi;
    657   1.1     soren 	u_int8_t addr;
    658   1.1     soren 
    659   1.1     soren 	int error = 0;
    660   1.1     soren 	u_int8_t i = 0;
    661   1.1     soren 
    662   1.5      joff 	if (io->dat < sc->sc_vcols) {
    663   1.5      joff 		hi = HD_ROW1_ADDR + sc->sc_vcols;
    664   1.1     soren 		addr = HD_ROW1_ADDR + io->dat;
    665   1.1     soren 		for (; (addr < hi) && (i < io->len); addr++, i++) {
    666   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(addr));
    667   1.1     soren 			if (dir == HD_DDRAM_READ)
    668   1.7      joff 				io->buf[i] = hd44780_dr_read(sc, en);
    669   1.1     soren 			else
    670   1.7      joff 				hd44780_dr_write(sc, en, io->buf[i]);
    671   1.1     soren 		}
    672   1.1     soren 	}
    673   1.5      joff 	if (io->dat < 2 * sc->sc_vcols) {
    674   1.5      joff 		hi = HD_ROW2_ADDR + sc->sc_vcols;
    675   1.5      joff 		if (io->dat >= sc->sc_vcols)
    676   1.5      joff 			addr = HD_ROW2_ADDR + io->dat - sc->sc_vcols;
    677   1.1     soren 		else
    678   1.1     soren 			addr = HD_ROW2_ADDR;
    679   1.1     soren 		for (; (addr < hi) && (i < io->len); addr++, i++) {
    680   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(addr));
    681   1.1     soren 			if (dir == HD_DDRAM_READ)
    682   1.7      joff 				io->buf[i] = hd44780_dr_read(sc, en);
    683   1.1     soren 			else
    684   1.7      joff 				hd44780_dr_write(sc, en, io->buf[i]);
    685   1.1     soren 		}
    686   1.1     soren 		if (i < io->len)
    687   1.1     soren 			io->len = i;
    688   1.1     soren 	} else {
    689   1.1     soren 		error = EINVAL;
    690   1.1     soren 	}
    691   1.1     soren 	return error;
    692   1.1     soren }
    693   1.1     soren 
    694   1.1     soren /*
    695   1.1     soren  * Write to the visible area of the display.
    696   1.1     soren  */
    697   1.1     soren void
    698   1.7      joff hd44780_ddram_redraw(sc, en, io)
    699   1.1     soren 	struct hd44780_chip *sc;
    700   1.7      joff 	u_int32_t en;
    701   1.1     soren 	struct hd44780_io *io;
    702   1.1     soren {
    703   1.1     soren 	u_int8_t i;
    704   1.1     soren 
    705   1.7      joff 	hd44780_ir_write(sc, en, cmd_clear());
    706   1.7      joff 	hd44780_ir_write(sc, en, cmd_rethome());
    707  1.13   tsutsui 	hd44780_ir_write(sc, en, cmd_ddramset(HD_ROW1_ADDR));
    708   1.5      joff 	for (i = 0; (i < io->len) && (i < sc->sc_cols); i++) {
    709   1.7      joff 		hd44780_dr_write(sc, en, io->buf[i]);
    710   1.1     soren 	}
    711   1.7      joff 	hd44780_ir_write(sc, en, cmd_ddramset(HD_ROW2_ADDR));
    712   1.1     soren 	for (; (i < io->len); i++)
    713   1.7      joff 		hd44780_dr_write(sc, en, io->buf[i]);
    714   1.1     soren }
    715   1.1     soren 
    716   1.3      joff void
    717   1.7      joff hd44780_busy_wait(sc, en)
    718   1.3      joff 	struct hd44780_chip *sc;
    719   1.7      joff 	u_int32_t en;
    720   1.3      joff {
    721   1.3      joff 	int nloops = 100;
    722   1.3      joff 
    723   1.3      joff 	if (sc->sc_flags & HD_TIMEDOUT)
    724   1.3      joff 		return;
    725   1.3      joff 
    726   1.7      joff 	while(nloops-- && (hd44780_ir_read(sc, en) & BUSY_FLAG) == BUSY_FLAG);
    727   1.3      joff 
    728   1.3      joff 	if (nloops == 0) {
    729   1.3      joff 		sc->sc_flags |= HD_TIMEDOUT;
    730   1.3      joff 		sc->sc_dev_ok = 0;
    731   1.3      joff 	}
    732   1.3      joff }
    733   1.3      joff 
    734   1.1     soren #if defined(HD44780_STD_WIDE)
    735   1.1     soren /*
    736   1.2      joff  * Standard 8-bit version of 'sc_writereg' (8-bit port, 8-bit access)
    737   1.1     soren  */
    738   1.1     soren void
    739   1.7      joff hd44780_writereg(sc, en, reg, cmd)
    740   1.2      joff 	struct hd44780_chip *sc;
    741   1.7      joff 	u_int32_t en, reg;
    742   1.1     soren 	u_int8_t cmd;
    743   1.1     soren {
    744   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    745   1.2      joff 	bus_space_handle_t ioh;
    746   1.2      joff 
    747   1.3      joff 	if (sc->sc_dev_ok == 0)
    748   1.3      joff 		return;
    749   1.3      joff 
    750   1.2      joff 	if (reg == 0)
    751   1.6     perry 		ioh = sc->sc_ioir;
    752   1.2      joff 	else
    753   1.2      joff 		ioh = sc->sc_iodr;
    754   1.2      joff 
    755   1.1     soren 	bus_space_write_1(iot, ioh, 0x00, cmd);
    756   1.2      joff 	delay(HD_TIMEOUT_NORMAL);
    757   1.1     soren }
    758   1.1     soren 
    759   1.1     soren /*
    760   1.2      joff  * Standard 8-bit version of 'sc_readreg' (8-bit port, 8-bit access)
    761   1.1     soren  */
    762   1.1     soren u_int8_t
    763   1.7      joff hd44780_readreg(sc, en, reg)
    764   1.2      joff 	struct hd44780_chip *sc;
    765   1.7      joff 	u_int32_t en, reg;
    766   1.2      joff {
    767   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    768   1.1     soren 	bus_space_handle_t ioh;
    769   1.2      joff 
    770   1.3      joff 	if (sc->sc_dev_ok == 0)
    771   1.3      joff 		return;
    772   1.3      joff 
    773   1.2      joff 	if (reg == 0)
    774   1.6     perry 		ioh = sc->sc_ioir;
    775   1.2      joff 	else
    776   1.2      joff 		ioh = sc->sc_iodr;
    777   1.2      joff 
    778   1.2      joff 	delay(HD_TIMEOUT_NORMAL);
    779   1.1     soren 	return bus_space_read_1(iot, ioh, 0x00);
    780   1.1     soren }
    781   1.1     soren #elif defined(HD44780_STD_SHORT)
    782   1.1     soren /*
    783   1.2      joff  * Standard 4-bit version of 'sc_writereg' (4-bit port, 8-bit access)
    784   1.1     soren  */
    785   1.1     soren void
    786   1.7      joff hd44780_writereg(sc, en, reg, cmd)
    787   1.2      joff 	struct hd44780_chip *sc;
    788   1.7      joff 	u_int32_t en, reg;
    789   1.1     soren 	u_int8_t cmd;
    790   1.1     soren {
    791   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    792   1.2      joff 	bus_space_handle_t ioh;
    793   1.2      joff 
    794   1.3      joff 	if (sc->sc_dev_ok == 0)
    795   1.3      joff 		return;
    796   1.3      joff 
    797   1.2      joff 	if (reg == 0)
    798   1.6     perry 		ioh = sc->sc_ioir;
    799   1.2      joff 	else
    800   1.2      joff 		ioh = sc->sc_iodr;
    801   1.1     soren 
    802   1.1     soren 	bus_space_write_1(iot, ioh, 0x00, hi_bits(cmd));
    803   1.6     perry 	if (sc->sc_flags & HD_UP)
    804   1.2      joff 		bus_space_write_1(iot, ioh, 0x00, lo_bits(cmd));
    805   1.2      joff 	delay(HD_TIMEOUT_NORMAL);
    806   1.1     soren }
    807   1.1     soren 
    808   1.1     soren /*
    809   1.2      joff  * Standard 4-bit version of 'sc_readreg' (4-bit port, 8-bit access)
    810   1.1     soren  */
    811   1.1     soren u_int8_t
    812   1.7      joff hd44780_readreg(sc, en, reg)
    813   1.2      joff 	struct hd44780_chip *sc;
    814   1.7      joff 	u_int32_t en, reg;
    815   1.2      joff {
    816   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    817   1.1     soren 	bus_space_handle_t ioh;
    818   1.2      joff 	u_int8_t rd, dat;
    819   1.2      joff 
    820   1.3      joff 	if (sc->sc_dev_ok == 0)
    821   1.3      joff 		return;
    822   1.3      joff 
    823   1.2      joff 	if (reg == 0)
    824   1.6     perry 		ioh = sc->sc_ioir;
    825   1.2      joff 	else
    826   1.2      joff 		ioh = sc->sc_iodr;
    827   1.1     soren 
    828   1.1     soren 	rd = bus_space_read_1(iot, ioh, 0x00);
    829   1.1     soren 	dat = (rd & 0x0f) << 4;
    830   1.1     soren 	rd = bus_space_read_1(iot, ioh, 0x00);
    831   1.1     soren 	return (dat | (rd & 0x0f));
    832   1.1     soren }
    833   1.1     soren #endif
    834