No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / isa / toasterlcd.c
blob19edbcb6d86d029273de64f9f2702c37aa39345a
1 /* $NetBSD: toasterlcd.c,v 1.9 2009/05/12 08:44:20 cegger Exp $ */
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jesse Off.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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>
36 #include <sys/proc.h>
37 #include <sys/poll.h>
38 #include <sys/conf.h>
39 #include <sys/uio.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>
46 #include <sys/bus.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 {
59 struct device sc_dev;
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,
80 &hlcd_emulops,
81 5, 7,
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 *),
91 _toasterlcd_scrlist,
94 static int
95 toasterlcd_match(device_t parent, cfdata_t match, void *aux)
97 return 1;
100 #define TSDIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
101 (TSDIO_ ## x))
103 #define TSDIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
104 (TSDIO_ ## x), (y))
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)))
112 static void
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);
141 waa.console = 0;
142 waa.scrdata = &toasterlcd_screenlist;
143 waa.accessops = &hlcd_accessops;
144 waa.accesscookie = &sc->sc_hlcd.sc_screen;
145 config_found(self, &waa, wsemuldisplaydevprint);
148 static void
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;
152 u_int8_t ctrl;
154 if (hd->sc_dev_ok == 0)
155 return;
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);
161 if (rs) {
162 ctrl |= 0x1; /* assert RS */
163 ctrl &= ~0x2; /* assert WR */
164 } else {
165 ctrl &= ~0x3; /* assert WR, de-assert RS */
167 TSDIO_SET(PBDR, ctrl);
169 /* Step 2: setup time delay */
170 delay(1);
172 /* Step 3: assert EN */
173 if (en == 1) ctrl |= 0x8;
174 else ctrl |= 0x4;
175 TSDIO_SET(PBDR, ctrl);
177 /* Step 4: pulse time delay */
178 delay(1);
180 /* Step 5: de-assert EN */
181 if (en == 1) ctrl &= ~0x8;
182 else ctrl &= ~0x4;
183 TSDIO_SET(PBDR, ctrl);
185 /* Step 6: hold time delay */
186 delay(1);
188 /* Step 7: de-assert WR */
189 ctrl |= 0x2;
190 TSDIO_SET(PBDR, ctrl);
192 /* Step 8: minimum delay till next bus-cycle */
193 delay(1000);
196 static u_int8_t
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;
200 u_int8_t ret, ctrl;
202 if (hd->sc_dev_ok == 0)
203 return 0;
205 /* Step 1: Apply RS & WR, Send data */
206 ctrl = TSDIO_GET(PBDR);
207 TSDIO_CLEARBITS(DDR, 0x1); /* set port C to inputs */
208 if (rs) {
209 ctrl |= 0x3; /* de-assert WR, assert RS */
210 } else {
211 ctrl |= 0x2; /* de-assert WR */
212 ctrl &= ~0x1; /* de-assert RS */
214 TSDIO_SET(PBDR, ctrl);
216 /* Step 2: setup time delay */
217 delay(1);
219 /* Step 3: assert EN */
220 if (en == 1) ctrl |= 0x8;
221 else ctrl |= 0x4;
222 TSDIO_SET(PBDR, ctrl);
224 /* Step 4: pulse time delay */
225 delay(1);
227 /* Step 5: de-assert EN */
228 ret = TSDIO_GET(PCDR) & 0xff;
229 if (en == 1) ctrl &= ~0x8;
230 else ctrl &= ~0x4;
231 TSDIO_SET(PBDR, ctrl);
233 /* Step 6: hold time delay + min bus cycle interval*/
234 delay(1000);
235 return ret;