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