1 /* $NetBSD: toasterlcd.c,v 1.9 2009/05/12 08:44:20 cegger Exp $ */
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: toasterlcd.c,v 1.9 2009/05/12 08:44:20 cegger Exp $");
34 #include <sys/param.h>
35 #include <sys/systm.h>
40 #include <sys/types.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/callout.h>
44 #include <sys/select.h>
47 #include <machine/autoconf.h>
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wscons_callbacks.h>
53 #include <dev/ic/hd44780reg.h>
54 #include <dev/ic/hd44780var.h>
55 #include <dev/isa/tsdiovar.h>
56 #include <dev/isa/tsdioreg.h>
58 struct toasterlcd_softc
{
60 struct hd44780_chip sc_hlcd
;
61 bus_space_tag_t sc_iot
;
62 bus_space_handle_t sc_gpioh
;
65 static int toasterlcd_match(device_t
, cfdata_t
, void *);
66 static void toasterlcd_attach(device_t
, device_t
, void *);
68 static void toasterlcd_writereg(struct hd44780_chip
*, u_int32_t
, u_int32_t
, u_int8_t
);
69 static u_int8_t
toasterlcd_readreg(struct hd44780_chip
*, u_int32_t
, u_int32_t
);
71 extern const struct wsdisplay_emulops hlcd_emulops
;
72 extern const struct wsdisplay_accessops hlcd_accessops
;
73 extern struct cfdriver toasterlcd_cd
;
75 CFATTACH_DECL(toasterlcd
, sizeof(struct toasterlcd_softc
),
76 toasterlcd_match
, toasterlcd_attach
, NULL
, NULL
);
78 static const struct wsscreen_descr toasterlcd_stdscreen
= {
79 "std_toasterlcd", 40, 4,
85 static const struct wsscreen_descr
*_toasterlcd_scrlist
[] = {
86 &toasterlcd_stdscreen
,
89 static const struct wsscreen_list toasterlcd_screenlist
= {
90 sizeof(_toasterlcd_scrlist
) / sizeof(struct wsscreen_descr
*),
95 toasterlcd_match(device_t parent
, cfdata_t match
, void *aux
)
100 #define TSDIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
103 #define TSDIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
106 #define TSDIO_SETBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
107 (TSDIO_ ## x), TSDIO_GET(x) | (y))
109 #define TSDIO_CLEARBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
110 (TSDIO_ ## x), TSDIO_GET(x) & (~(y)))
113 toasterlcd_attach(device_t parent
, device_t self
, void *aux
)
115 struct toasterlcd_softc
*sc
= (void *)self
;
116 struct tsdio_attach_args
*taa
= aux
;
117 struct wsemuldisplaydev_attach_args waa
;
119 sc
->sc_iot
= taa
->ta_iot
;
120 sc
->sc_gpioh
= taa
->ta_ioh
;
122 sc
->sc_hlcd
.sc_dev_ok
= 1;
123 sc
->sc_hlcd
.sc_cols
= 40;
124 sc
->sc_hlcd
.sc_vcols
= 40;
125 sc
->sc_hlcd
.sc_flags
= HD_8BIT
| HD_MULTILINE
| HD_MULTICHIP
;
126 sc
->sc_hlcd
.sc_dev
= self
;
128 sc
->sc_hlcd
.sc_writereg
= toasterlcd_writereg
;
129 sc
->sc_hlcd
.sc_readreg
= toasterlcd_readreg
;
131 TSDIO_SETBITS(DDR
, 0x2); /* Port B as outputs */
132 TSDIO_CLEARBITS(DDR
, 0x1); /* Port C as inputs */
133 TSDIO_CLEARBITS(PBDR
, 0xd); /* De-assert EN, De-assert RS */
135 aprint_normal(": 4x40 text-mode hd44780 LCD\n");
136 aprint_normal_dev(&sc
->sc_dev
, "using port C, bits 0-7 as DB0-DB7\n");
137 aprint_normal_dev(&sc
->sc_dev
, "using port B, bits 0-3 as RS, WR, EN1, EN2\n");
139 hd44780_attach_subr(&sc
->sc_hlcd
);
142 waa
.scrdata
= &toasterlcd_screenlist
;
143 waa
.accessops
= &hlcd_accessops
;
144 waa
.accesscookie
= &sc
->sc_hlcd
.sc_screen
;
145 config_found(self
, &waa
, wsemuldisplaydevprint
);
149 toasterlcd_writereg(struct hd44780_chip
*hd
, u_int32_t en
, u_int32_t rs
, u_int8_t cmd
)
151 struct toasterlcd_softc
*sc
= (struct toasterlcd_softc
*)hd
->sc_dev
;
154 if (hd
->sc_dev_ok
== 0)
157 /* Step 1: Apply RS & WR, Send data */
158 ctrl
= TSDIO_GET(PBDR
);
159 TSDIO_SETBITS(DDR
, 0x1); /* set port C to outputs */
160 TSDIO_SET(PCDR
, cmd
);
162 ctrl
|= 0x1; /* assert RS */
163 ctrl
&= ~0x2; /* assert WR */
165 ctrl
&= ~0x3; /* assert WR, de-assert RS */
167 TSDIO_SET(PBDR
, ctrl
);
169 /* Step 2: setup time delay */
172 /* Step 3: assert EN */
173 if (en
== 1) ctrl
|= 0x8;
175 TSDIO_SET(PBDR
, ctrl
);
177 /* Step 4: pulse time delay */
180 /* Step 5: de-assert EN */
181 if (en
== 1) ctrl
&= ~0x8;
183 TSDIO_SET(PBDR
, ctrl
);
185 /* Step 6: hold time delay */
188 /* Step 7: de-assert WR */
190 TSDIO_SET(PBDR
, ctrl
);
192 /* Step 8: minimum delay till next bus-cycle */
197 toasterlcd_readreg(struct hd44780_chip
*hd
, u_int32_t en
, u_int32_t rs
)
199 struct toasterlcd_softc
*sc
= (struct toasterlcd_softc
*)hd
->sc_dev
;
202 if (hd
->sc_dev_ok
== 0)
205 /* Step 1: Apply RS & WR, Send data */
206 ctrl
= TSDIO_GET(PBDR
);
207 TSDIO_CLEARBITS(DDR
, 0x1); /* set port C to inputs */
209 ctrl
|= 0x3; /* de-assert WR, assert RS */
211 ctrl
|= 0x2; /* de-assert WR */
212 ctrl
&= ~0x1; /* de-assert RS */
214 TSDIO_SET(PBDR
, ctrl
);
216 /* Step 2: setup time delay */
219 /* Step 3: assert EN */
220 if (en
== 1) ctrl
|= 0x8;
222 TSDIO_SET(PBDR
, ctrl
);
224 /* Step 4: pulse time delay */
227 /* Step 5: de-assert EN */
228 ret
= TSDIO_GET(PCDR
) & 0xff;
229 if (en
== 1) ctrl
&= ~0x8;
231 TSDIO_SET(PBDR
, ctrl
);
233 /* Step 6: hold time delay + min bus cycle interval*/