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