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