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