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