Home | History | Annotate | Line # | Download | only in ic
hd44780_subr.c revision 1.18
      1  1.18       dsl /* $NetBSD: hd44780_subr.c,v 1.18 2009/03/14 21:04:20 dsl 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.18       dsl __KERNEL_RCSID(0, "$NetBSD: hd44780_subr.c,v 1.18 2009/03/14 21:04:20 dsl 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.18       dsl hlcd_cursor(void *id, int on, int row, int col)
    110   1.5      joff {
    111   1.5      joff 	struct hlcd_screen *hdscr = id;
    112   1.5      joff 
    113   1.5      joff 	hdscr->hlcd_curon = on;
    114   1.5      joff 	hdscr->hlcd_curx = col;
    115   1.5      joff 	hdscr->hlcd_cury = row;
    116   1.5      joff }
    117   1.5      joff 
    118   1.5      joff static int
    119  1.17       dsl hlcd_mapchar(void *id, int uni, unsigned int *index)
    120   1.5      joff {
    121   1.5      joff 	if (uni < 256) {
    122   1.5      joff 		*index = uni;
    123   1.5      joff 		return (5);
    124   1.5      joff 	}
    125   1.5      joff 	*index = ' ';
    126   1.5      joff 	return (0);
    127   1.5      joff }
    128   1.5      joff 
    129   1.5      joff static void
    130  1.18       dsl hlcd_putchar(void *id, int row, int col, u_int c, long attr)
    131   1.5      joff {
    132   1.5      joff 	struct hlcd_screen *hdscr = id;
    133   1.6     perry 
    134   1.5      joff 	c &= 0xff;
    135   1.7      joff 	if (row > 0 && (hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    136   1.5      joff 		hdscr->image[hdscr->hlcd_sc->sc_cols * row + col] = c;
    137   1.5      joff 	else
    138   1.5      joff 		hdscr->image[col] = c;
    139   1.5      joff }
    140   1.5      joff 
    141   1.5      joff /*
    142   1.5      joff  * copies columns inside a row.
    143   1.5      joff  */
    144   1.5      joff static void
    145  1.18       dsl hlcd_copycols(void *id, int row, int srccol, int dstcol, int ncols)
    146   1.5      joff {
    147   1.5      joff 	struct hlcd_screen *hdscr = id;
    148   1.5      joff 
    149   1.5      joff 	if ((dstcol + ncols - 1) > hdscr->hlcd_sc->sc_cols)
    150   1.5      joff 		ncols = hdscr->hlcd_sc->sc_cols - srccol;
    151   1.5      joff 
    152   1.7      joff 	if (row > 0 && (hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    153  1.13   tsutsui 		memmove(&hdscr->image[hdscr->hlcd_sc->sc_cols * row + dstcol],
    154  1.13   tsutsui 		    &hdscr->image[hdscr->hlcd_sc->sc_cols * row + srccol],
    155  1.13   tsutsui 		    ncols);
    156   1.5      joff 	else
    157  1.13   tsutsui 		memmove(&hdscr->image[dstcol], &hdscr->image[srccol], ncols);
    158   1.5      joff }
    159   1.5      joff 
    160   1.5      joff 
    161   1.5      joff /*
    162   1.5      joff  * Erases a bunch of chars inside one row.
    163   1.5      joff  */
    164   1.5      joff static void
    165  1.18       dsl hlcd_erasecols(void *id, int row, int startcol, int ncols, long fillattr)
    166   1.5      joff {
    167   1.5      joff 	struct hlcd_screen *hdscr = id;
    168   1.5      joff 
    169   1.5      joff 	if ((startcol + ncols) > hdscr->hlcd_sc->sc_cols)
    170   1.5      joff 		ncols = hdscr->hlcd_sc->sc_cols - startcol;
    171   1.5      joff 
    172   1.7      joff 	if (row > 0 && (hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    173   1.6     perry 		memset(&hdscr->image[hdscr->hlcd_sc->sc_cols * row + startcol],
    174   1.5      joff 		    ' ', ncols);
    175   1.5      joff 	else
    176   1.5      joff 		memset(&hdscr->image[startcol], ' ', ncols);
    177   1.5      joff }
    178   1.5      joff 
    179   1.5      joff 
    180   1.5      joff static void
    181  1.18       dsl hlcd_copyrows(void *id, int srcrow, int dstrow, int nrows)
    182   1.5      joff {
    183   1.5      joff 	struct hlcd_screen *hdscr = id;
    184   1.5      joff 	int ncols = hdscr->hlcd_sc->sc_cols;
    185   1.6     perry 
    186   1.7      joff 	if (!(hdscr->hlcd_sc->sc_flags & (HD_MULTILINE|HD_MULTICHIP)))
    187   1.5      joff 		return;
    188  1.13   tsutsui 	memmove(&hdscr->image[dstrow * ncols], &hdscr->image[srcrow * ncols],
    189   1.5      joff 	    nrows * ncols);
    190   1.5      joff }
    191   1.5      joff 
    192   1.5      joff static void
    193  1.18       dsl hlcd_eraserows(void *id, int startrow, int nrows, long fillattr)
    194   1.5      joff {
    195   1.5      joff 	struct hlcd_screen *hdscr = id;
    196   1.5      joff 	int ncols = hdscr->hlcd_sc->sc_cols;
    197   1.5      joff 
    198   1.5      joff 	memset(&hdscr->image[startrow * ncols], ' ', ncols * nrows);
    199   1.5      joff }
    200   1.5      joff 
    201   1.5      joff 
    202   1.5      joff static int
    203  1.18       dsl hlcd_allocattr(void *id, int fg, int bg, int flags, long *attrp)
    204   1.5      joff {
    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.5      joff 	return -1;
    235   1.5      joff }
    236   1.5      joff 
    237   1.5      joff static int
    238  1.18       dsl hlcd_alloc_screen(void *v, const struct wsscreen_descr *type, void **cookiep, int *curxp, int *curyp, long *defattrp)
    239   1.5      joff {
    240   1.5      joff 	struct hlcd_screen *hdscr = v, *new;
    241   1.5      joff 
    242  1.16    cegger 	new = *cookiep = malloc(sizeof(struct hlcd_screen),
    243  1.16    cegger 				M_DEVBUF, M_WAITOK|M_ZERO);
    244   1.5      joff 	new->hlcd_sc = hdscr->hlcd_sc;
    245   1.5      joff 	new->image = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
    246   1.5      joff 	memset(new->image, ' ', PAGE_SIZE);
    247   1.5      joff 	*curxp = *curyp = *defattrp = 0;
    248   1.5      joff 	return 0;
    249   1.5      joff }
    250   1.5      joff 
    251   1.5      joff static void
    252  1.18       dsl hlcd_free_screen(void *v, void *cookie)
    253   1.5      joff {
    254   1.5      joff }
    255   1.5      joff 
    256   1.5      joff static int
    257   1.5      joff hlcd_show_screen(v, cookie, waitok, cb, cbarg)
    258   1.5      joff 	void *v, *cookie, *cbarg;
    259   1.5      joff 	int waitok;
    260   1.5      joff 	void (*cb)(void *, int, int);
    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.5      joff 	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.5      joff 	u_char *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.1     soren 	/* Putc/getc are supposed to be set by platform-dependent code. */
    368   1.2      joff 	if ((sc->sc_writereg == NULL) || (sc->sc_readreg == NULL))
    369   1.1     soren 		sc->sc_dev_ok = 0;
    370   1.1     soren 
    371   1.1     soren 	/* Make sure that HD_MAX_CHARS is enough. */
    372   1.5      joff 	if ((sc->sc_flags & HD_MULTILINE) && (2 * sc->sc_cols > HD_MAX_CHARS))
    373   1.1     soren 		sc->sc_dev_ok = 0;
    374   1.5      joff 	else if (sc->sc_cols > HD_MAX_CHARS)
    375   1.1     soren 		sc->sc_dev_ok = 0;
    376   1.1     soren 
    377   1.1     soren 	if (sc->sc_dev_ok) {
    378   1.6     perry 		if ((sc->sc_flags & HD_UP) == 0)
    379   1.3      joff 			err = hd44780_init(sc);
    380   1.3      joff 		if (err != 0)
    381  1.15        he 			aprint_error_dev(sc->sc_dev, "LCD not responding or unconnected\n");
    382   1.1     soren 
    383   1.1     soren 	}
    384   1.5      joff 
    385   1.5      joff 	sc->sc_screen.hlcd_sc = sc;
    386   1.6     perry 
    387   1.5      joff 	sc->sc_screen.image = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK);
    388   1.5      joff 	memset(sc->sc_screen.image, ' ', PAGE_SIZE);
    389   1.5      joff 	sc->sc_curscr = NULL;
    390   1.7      joff 	sc->sc_curchip = 0;
    391  1.11        ad 	callout_init(&sc->redraw, 0);
    392   1.5      joff 	callout_setfunc(&sc->redraw, hlcd_redraw, sc);
    393   1.1     soren }
    394   1.1     soren 
    395   1.7      joff int hd44780_init(sc)
    396   1.7      joff 	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.7      joff 	if (ret != 0 || !(sc->sc_flags & HD_MULTICHIP)) return ret;
    402   1.7      joff 	else return hd44780_chipinit(sc, 1);
    403   1.7      joff }
    404   1.7      joff 
    405   1.1     soren /*
    406   1.1     soren  * Initialize 4-bit or 8-bit connected device.
    407   1.1     soren  */
    408   1.3      joff int
    409  1.17       dsl hd44780_chipinit(struct hd44780_chip *sc, u_int32_t en)
    410   1.1     soren {
    411   1.3      joff 	u_int8_t cmd, dat;
    412   1.1     soren 
    413   1.3      joff 	sc->sc_flags &= ~(HD_TIMEDOUT|HD_UP);
    414   1.3      joff 	sc->sc_dev_ok = 1;
    415   1.7      joff 
    416   1.1     soren 	cmd = cmd_init(sc->sc_flags & HD_8BIT);
    417   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    418   1.2      joff 	delay(HD_TIMEOUT_LONG);
    419   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    420   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    421   1.1     soren 
    422   1.1     soren 	cmd = cmd_funcset(
    423   1.1     soren 			sc->sc_flags & HD_8BIT,
    424   1.1     soren 			sc->sc_flags & HD_MULTILINE,
    425   1.1     soren 			sc->sc_flags & HD_BIGFONT);
    426   1.1     soren 
    427   1.1     soren 	if ((sc->sc_flags & HD_8BIT) == 0)
    428   1.7      joff 		hd44780_ir_write(sc, en, cmd);
    429   1.1     soren 
    430   1.2      joff 	sc->sc_flags |= HD_UP;
    431   1.2      joff 
    432   1.7      joff 	hd44780_ir_write(sc, en, cmd);
    433   1.7      joff 	hd44780_ir_write(sc, en, cmd_dispctl(0, 0, 0));
    434   1.7      joff 	hd44780_ir_write(sc, en, cmd_clear());
    435   1.7      joff 	hd44780_ir_write(sc, en, cmd_modset(1, 0));
    436   1.3      joff 
    437   1.3      joff 	if (sc->sc_flags & HD_TIMEDOUT) {
    438   1.3      joff 		sc->sc_flags &= ~HD_UP;
    439   1.3      joff 		return EIO;
    440   1.6     perry 	}
    441   1.3      joff 
    442   1.3      joff 	/* Turn display on and clear it. */
    443   1.7      joff 	hd44780_ir_write(sc, en, cmd_clear());
    444   1.7      joff 	hd44780_ir_write(sc, en, cmd_dispctl(1, 0, 0));
    445   1.3      joff 
    446   1.3      joff 	/* Attempt a simple probe for presence */
    447   1.7      joff 	hd44780_ir_write(sc, en, cmd_ddramset(0x5));
    448   1.7      joff 	hd44780_ir_write(sc, en, cmd_shift(0, 1));
    449   1.7      joff 	hd44780_busy_wait(sc, en);
    450   1.7      joff 	if ((dat = hd44780_ir_read(sc, en) & 0x7f) != 0x6) {
    451   1.3      joff 		sc->sc_dev_ok = 0;
    452   1.3      joff 		sc->sc_flags &= ~HD_UP;
    453   1.3      joff 		return EIO;
    454   1.3      joff 	}
    455   1.7      joff 	hd44780_ir_write(sc, en, cmd_ddramset(0));
    456   1.3      joff 
    457   1.3      joff 	return 0;
    458   1.1     soren }
    459   1.1     soren 
    460   1.1     soren /*
    461   1.1     soren  * Standard hd44780 ioctl() functions.
    462   1.1     soren  */
    463   1.1     soren int
    464  1.17       dsl hd44780_ioctl_subr(struct hd44780_chip *sc, u_long cmd, void *data)
    465   1.1     soren {
    466   1.1     soren 	u_int8_t tmp;
    467   1.1     soren 	int error = 0;
    468   1.7      joff 	u_int32_t en = sc->sc_curchip;
    469   1.1     soren 
    470   1.1     soren #define hd44780_io()	((struct hd44780_io *)data)
    471   1.1     soren #define hd44780_info()	((struct hd44780_info*)data)
    472   1.1     soren #define hd44780_ctrl()	((struct hd44780_dispctl*)data)
    473   1.1     soren 
    474   1.1     soren 	switch (cmd) {
    475   1.1     soren 		/* Clear the LCD. */
    476   1.1     soren 		case HLCD_CLEAR:
    477   1.7      joff 			hd44780_ir_write(sc, en, cmd_clear());
    478   1.1     soren 			break;
    479   1.1     soren 
    480   1.1     soren 		/* Move the cursor one position to the left. */
    481   1.1     soren 		case HLCD_CURSOR_LEFT:
    482   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(0, 0));
    483   1.1     soren 			break;
    484   1.1     soren 
    485   1.1     soren 		/* Move the cursor one position to the right. */
    486   1.1     soren 		case HLCD_CURSOR_RIGHT:
    487   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(0, 1));
    488   1.6     perry 			break;
    489   1.1     soren 
    490   1.1     soren 		/* Control the LCD. */
    491   1.1     soren 		case HLCD_DISPCTL:
    492   1.7      joff 			hd44780_ir_write(sc, en, cmd_dispctl(
    493   1.1     soren 						hd44780_ctrl()->display_on,
    494   1.1     soren 						hd44780_ctrl()->cursor_on,
    495   1.1     soren 						hd44780_ctrl()->blink_on));
    496   1.1     soren 			break;
    497   1.1     soren 
    498   1.1     soren 		/* Get LCD configuration. */
    499   1.1     soren 		case HLCD_GET_INFO:
    500   1.1     soren 			hd44780_info()->lines
    501   1.1     soren 				= (sc->sc_flags & HD_MULTILINE) ? 2 : 1;
    502   1.7      joff 			if (sc->sc_flags & HD_MULTICHIP)
    503   1.7      joff 				hd44780_info()->lines *= 2;
    504   1.5      joff 			hd44780_info()->phys_rows = sc->sc_cols;
    505   1.5      joff 			hd44780_info()->virt_rows = sc->sc_vcols;
    506   1.1     soren 			hd44780_info()->is_wide = sc->sc_flags & HD_8BIT;
    507   1.1     soren 			hd44780_info()->is_bigfont = sc->sc_flags & HD_BIGFONT;
    508   1.1     soren 			hd44780_info()->kp_present = sc->sc_flags & HD_KEYPAD;
    509   1.1     soren 			break;
    510   1.1     soren 
    511   1.1     soren 
    512   1.1     soren 		/* Reset the LCD. */
    513   1.1     soren 		case HLCD_RESET:
    514   1.3      joff 			error = hd44780_init(sc);
    515   1.1     soren 			break;
    516   1.1     soren 
    517   1.1     soren 		/* Get the current cursor position. */
    518   1.1     soren 		case HLCD_GET_CURSOR_POS:
    519   1.7      joff 			hd44780_io()->dat = (hd44780_ir_read(sc, en) & 0x7f);
    520   1.1     soren 			break;
    521   1.1     soren 
    522   1.1     soren 		/* Set the cursor position. */
    523   1.1     soren 		case HLCD_SET_CURSOR_POS:
    524   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(hd44780_io()->dat));
    525   1.1     soren 			break;
    526   1.1     soren 
    527   1.1     soren 		/* Get the value at the current cursor position. */
    528   1.1     soren 		case HLCD_GETC:
    529   1.7      joff 			tmp = (hd44780_ir_read(sc, en) & 0x7f);
    530   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(tmp));
    531   1.7      joff 			hd44780_io()->dat = hd44780_dr_read(sc, en);
    532   1.1     soren 			break;
    533   1.1     soren 
    534   1.1     soren 		/* Set the character at the cursor position + advance cursor. */
    535   1.1     soren 		case HLCD_PUTC:
    536   1.7      joff 			hd44780_dr_write(sc, en, hd44780_io()->dat);
    537   1.1     soren 			break;
    538   1.1     soren 
    539   1.1     soren 		/* Shift display left. */
    540   1.1     soren 		case HLCD_SHIFT_LEFT:
    541   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(1, 0));
    542   1.1     soren 			break;
    543   1.1     soren 
    544   1.1     soren 		/* Shift display right. */
    545   1.1     soren 		case HLCD_SHIFT_RIGHT:
    546   1.7      joff 			hd44780_ir_write(sc, en, cmd_shift(1, 1));
    547   1.1     soren 			break;
    548   1.1     soren 
    549   1.1     soren 		/* Return home. */
    550   1.1     soren 		case HLCD_HOME:
    551   1.7      joff 			hd44780_ir_write(sc, en, cmd_rethome());
    552   1.1     soren 			break;
    553   1.1     soren 
    554   1.1     soren 		/* Write a string to the LCD virtual area. */
    555   1.1     soren 		case HLCD_WRITE:
    556   1.7      joff 			error = hd44780_ddram_io(sc, en, hd44780_io(), HD_DDRAM_WRITE);
    557   1.6     perry 			break;
    558   1.1     soren 
    559   1.1     soren 		/* Read LCD virtual area. */
    560   1.1     soren 		case HLCD_READ:
    561   1.7      joff 			error = hd44780_ddram_io(sc, en, hd44780_io(), HD_DDRAM_READ);
    562   1.1     soren 			break;
    563   1.1     soren 
    564   1.1     soren 		/* Write to the LCD visible area. */
    565   1.1     soren 		case HLCD_REDRAW:
    566   1.7      joff 			hd44780_ddram_redraw(sc, en, hd44780_io());
    567   1.1     soren 			break;
    568   1.1     soren 
    569   1.1     soren 		/* Write raw instruction. */
    570   1.1     soren 		case HLCD_WRITE_INST:
    571   1.7      joff 			hd44780_ir_write(sc, en, hd44780_io()->dat);
    572   1.1     soren 			break;
    573   1.1     soren 
    574   1.1     soren 		/* Write raw data. */
    575   1.1     soren 		case HLCD_WRITE_DATA:
    576   1.7      joff 			hd44780_dr_write(sc, en, hd44780_io()->dat);
    577   1.7      joff 			break;
    578   1.7      joff 
    579   1.7      joff 		/* Get current chip 0 or 1 (top or bottom) */
    580   1.7      joff 		case HLCD_GET_CHIPNO:
    581   1.7      joff 			*(u_int8_t *)data = sc->sc_curchip;
    582   1.7      joff 			break;
    583   1.7      joff 
    584   1.7      joff 		/* Set current chip 0 or 1 (top or bottom) */
    585   1.7      joff 		case HLCD_SET_CHIPNO:
    586   1.7      joff 			sc->sc_curchip = *(u_int8_t *)data;
    587   1.1     soren 			break;
    588   1.1     soren 
    589   1.1     soren 		default:
    590   1.1     soren 			error = EINVAL;
    591   1.1     soren 	}
    592   1.1     soren 
    593   1.3      joff 	if (sc->sc_flags & HD_TIMEDOUT)
    594   1.3      joff 		error = EIO;
    595   1.3      joff 
    596   1.1     soren 	return error;
    597   1.1     soren }
    598   1.1     soren 
    599   1.1     soren /*
    600   1.1     soren  * Read/write particular area of the LCD screen.
    601   1.1     soren  */
    602   1.1     soren int
    603  1.17       dsl hd44780_ddram_io(struct hd44780_chip *sc, u_int32_t en, struct hd44780_io *io, u_char dir)
    604   1.1     soren {
    605   1.1     soren 	u_int8_t hi;
    606   1.1     soren 	u_int8_t addr;
    607   1.1     soren 
    608   1.1     soren 	int error = 0;
    609   1.1     soren 	u_int8_t i = 0;
    610   1.1     soren 
    611   1.5      joff 	if (io->dat < sc->sc_vcols) {
    612   1.5      joff 		hi = HD_ROW1_ADDR + sc->sc_vcols;
    613   1.1     soren 		addr = HD_ROW1_ADDR + io->dat;
    614   1.1     soren 		for (; (addr < hi) && (i < io->len); addr++, i++) {
    615   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(addr));
    616   1.1     soren 			if (dir == HD_DDRAM_READ)
    617   1.7      joff 				io->buf[i] = hd44780_dr_read(sc, en);
    618   1.1     soren 			else
    619   1.7      joff 				hd44780_dr_write(sc, en, io->buf[i]);
    620   1.1     soren 		}
    621   1.1     soren 	}
    622   1.5      joff 	if (io->dat < 2 * sc->sc_vcols) {
    623   1.5      joff 		hi = HD_ROW2_ADDR + sc->sc_vcols;
    624   1.5      joff 		if (io->dat >= sc->sc_vcols)
    625   1.5      joff 			addr = HD_ROW2_ADDR + io->dat - sc->sc_vcols;
    626   1.1     soren 		else
    627   1.1     soren 			addr = HD_ROW2_ADDR;
    628   1.1     soren 		for (; (addr < hi) && (i < io->len); addr++, i++) {
    629   1.7      joff 			hd44780_ir_write(sc, en, cmd_ddramset(addr));
    630   1.1     soren 			if (dir == HD_DDRAM_READ)
    631   1.7      joff 				io->buf[i] = hd44780_dr_read(sc, en);
    632   1.1     soren 			else
    633   1.7      joff 				hd44780_dr_write(sc, en, io->buf[i]);
    634   1.1     soren 		}
    635   1.1     soren 		if (i < io->len)
    636   1.1     soren 			io->len = i;
    637   1.1     soren 	} else {
    638   1.1     soren 		error = EINVAL;
    639   1.1     soren 	}
    640   1.1     soren 	return error;
    641   1.1     soren }
    642   1.1     soren 
    643   1.1     soren /*
    644   1.1     soren  * Write to the visible area of the display.
    645   1.1     soren  */
    646   1.1     soren void
    647  1.17       dsl hd44780_ddram_redraw(struct hd44780_chip *sc, u_int32_t en, struct hd44780_io *io)
    648   1.1     soren {
    649   1.1     soren 	u_int8_t i;
    650   1.1     soren 
    651   1.7      joff 	hd44780_ir_write(sc, en, cmd_clear());
    652   1.7      joff 	hd44780_ir_write(sc, en, cmd_rethome());
    653  1.13   tsutsui 	hd44780_ir_write(sc, en, cmd_ddramset(HD_ROW1_ADDR));
    654   1.5      joff 	for (i = 0; (i < io->len) && (i < sc->sc_cols); i++) {
    655   1.7      joff 		hd44780_dr_write(sc, en, io->buf[i]);
    656   1.1     soren 	}
    657   1.7      joff 	hd44780_ir_write(sc, en, cmd_ddramset(HD_ROW2_ADDR));
    658   1.1     soren 	for (; (i < io->len); i++)
    659   1.7      joff 		hd44780_dr_write(sc, en, io->buf[i]);
    660   1.1     soren }
    661   1.1     soren 
    662   1.3      joff void
    663  1.17       dsl hd44780_busy_wait(struct hd44780_chip *sc, u_int32_t en)
    664   1.3      joff {
    665   1.3      joff 	int nloops = 100;
    666   1.3      joff 
    667   1.3      joff 	if (sc->sc_flags & HD_TIMEDOUT)
    668   1.3      joff 		return;
    669   1.3      joff 
    670   1.7      joff 	while(nloops-- && (hd44780_ir_read(sc, en) & BUSY_FLAG) == BUSY_FLAG);
    671   1.3      joff 
    672   1.3      joff 	if (nloops == 0) {
    673   1.3      joff 		sc->sc_flags |= HD_TIMEDOUT;
    674   1.3      joff 		sc->sc_dev_ok = 0;
    675   1.3      joff 	}
    676   1.3      joff }
    677   1.3      joff 
    678   1.1     soren #if defined(HD44780_STD_WIDE)
    679   1.1     soren /*
    680   1.2      joff  * Standard 8-bit version of 'sc_writereg' (8-bit port, 8-bit access)
    681   1.1     soren  */
    682   1.1     soren void
    683  1.18       dsl hd44780_writereg(struct hd44780_chip *sc, u_int32_t en, u_int32_t reg, u_int8_t cmd)
    684   1.1     soren {
    685   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    686   1.2      joff 	bus_space_handle_t ioh;
    687   1.2      joff 
    688   1.3      joff 	if (sc->sc_dev_ok == 0)
    689   1.3      joff 		return;
    690   1.3      joff 
    691   1.2      joff 	if (reg == 0)
    692   1.6     perry 		ioh = sc->sc_ioir;
    693   1.2      joff 	else
    694   1.2      joff 		ioh = sc->sc_iodr;
    695   1.2      joff 
    696   1.1     soren 	bus_space_write_1(iot, ioh, 0x00, cmd);
    697   1.2      joff 	delay(HD_TIMEOUT_NORMAL);
    698   1.1     soren }
    699   1.1     soren 
    700   1.1     soren /*
    701   1.2      joff  * Standard 8-bit version of 'sc_readreg' (8-bit port, 8-bit access)
    702   1.1     soren  */
    703   1.1     soren u_int8_t
    704  1.18       dsl hd44780_readreg(struct hd44780_chip *sc, u_int32_t en, u_int32_t reg)
    705   1.2      joff {
    706   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    707   1.1     soren 	bus_space_handle_t ioh;
    708   1.2      joff 
    709   1.3      joff 	if (sc->sc_dev_ok == 0)
    710   1.3      joff 		return;
    711   1.3      joff 
    712   1.2      joff 	if (reg == 0)
    713   1.6     perry 		ioh = sc->sc_ioir;
    714   1.2      joff 	else
    715   1.2      joff 		ioh = sc->sc_iodr;
    716   1.2      joff 
    717   1.2      joff 	delay(HD_TIMEOUT_NORMAL);
    718   1.1     soren 	return bus_space_read_1(iot, ioh, 0x00);
    719   1.1     soren }
    720   1.1     soren #elif defined(HD44780_STD_SHORT)
    721   1.1     soren /*
    722   1.2      joff  * Standard 4-bit version of 'sc_writereg' (4-bit port, 8-bit access)
    723   1.1     soren  */
    724   1.1     soren void
    725  1.18       dsl hd44780_writereg(struct hd44780_chip *sc, u_int32_t en, u_int32_t reg, u_int8_t cmd)
    726   1.1     soren {
    727   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    728   1.2      joff 	bus_space_handle_t ioh;
    729   1.2      joff 
    730   1.3      joff 	if (sc->sc_dev_ok == 0)
    731   1.3      joff 		return;
    732   1.3      joff 
    733   1.2      joff 	if (reg == 0)
    734   1.6     perry 		ioh = sc->sc_ioir;
    735   1.2      joff 	else
    736   1.2      joff 		ioh = sc->sc_iodr;
    737   1.1     soren 
    738   1.1     soren 	bus_space_write_1(iot, ioh, 0x00, hi_bits(cmd));
    739   1.6     perry 	if (sc->sc_flags & HD_UP)
    740   1.2      joff 		bus_space_write_1(iot, ioh, 0x00, lo_bits(cmd));
    741   1.2      joff 	delay(HD_TIMEOUT_NORMAL);
    742   1.1     soren }
    743   1.1     soren 
    744   1.1     soren /*
    745   1.2      joff  * Standard 4-bit version of 'sc_readreg' (4-bit port, 8-bit access)
    746   1.1     soren  */
    747   1.1     soren u_int8_t
    748  1.18       dsl hd44780_readreg(struct hd44780_chip *sc, u_int32_t en, u_int32_t reg)
    749   1.2      joff {
    750   1.2      joff 	bus_space_tag_t iot = sc->sc_iot;
    751   1.1     soren 	bus_space_handle_t ioh;
    752   1.2      joff 	u_int8_t rd, dat;
    753   1.2      joff 
    754   1.3      joff 	if (sc->sc_dev_ok == 0)
    755   1.3      joff 		return;
    756   1.3      joff 
    757   1.2      joff 	if (reg == 0)
    758   1.6     perry 		ioh = sc->sc_ioir;
    759   1.2      joff 	else
    760   1.2      joff 		ioh = sc->sc_iodr;
    761   1.1     soren 
    762   1.1     soren 	rd = bus_space_read_1(iot, ioh, 0x00);
    763   1.1     soren 	dat = (rd & 0x0f) << 4;
    764   1.1     soren 	rd = bus_space_read_1(iot, ioh, 0x00);
    765   1.1     soren 	return (dat | (rd & 0x0f));
    766   1.1     soren }
    767   1.1     soren #endif
    768