1 /* $NetBSD: toasterlcd.c,v 1.11 2012/10/27 17:18:25 chs Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jesse Off.
9  *
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.
18  *
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.
30  */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: toasterlcd.c,v 1.11 2012/10/27 17:18:25 chs Exp $");
33 
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>
45 
46 #include <sys/bus.h>
47 #include <machine/autoconf.h>
48 
49 #include <dev/wscons/wsdisplayvar.h>
50 #include <dev/wscons/wsconsio.h>
51 #include <dev/wscons/wscons_callbacks.h>
52 
53 #include <dev/ic/hd44780reg.h>
54 #include <dev/ic/hd44780var.h>
55 #include <dev/isa/tsdiovar.h>
56 #include <dev/isa/tsdioreg.h>
57 
58 struct toasterlcd_softc {
59 	device_t sc_dev;
60 	struct hd44780_chip sc_hlcd;
61 	bus_space_tag_t sc_iot;
62 	bus_space_handle_t sc_gpioh;
63 };
64 
65 static int	toasterlcd_match(device_t, cfdata_t, void *);
66 static void	toasterlcd_attach(device_t, device_t, void *);
67 
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);
70 
71 extern const struct wsdisplay_emulops hlcd_emulops;
72 extern const struct wsdisplay_accessops hlcd_accessops;
73 extern struct cfdriver toasterlcd_cd;
74 
75 CFATTACH_DECL_NEW(toasterlcd, sizeof(struct toasterlcd_softc),
76     toasterlcd_match, toasterlcd_attach, NULL, NULL);
77 
78 static const struct wsscreen_descr toasterlcd_stdscreen = {
79 	"std_toasterlcd", 40, 4,
80 	&hlcd_emulops,
81 	5, 7,
82 	0,
83 };
84 
85 static const struct wsscreen_descr *_toasterlcd_scrlist[] = {
86 	&toasterlcd_stdscreen,
87 };
88 
89 static const struct wsscreen_list toasterlcd_screenlist = {
90 	sizeof(_toasterlcd_scrlist) / sizeof(struct wsscreen_descr *),
91 	_toasterlcd_scrlist,
92 };
93 
94 static int
toasterlcd_match(device_t parent,cfdata_t match,void * aux)95 toasterlcd_match(device_t parent, cfdata_t match, void *aux)
96 {
97 	return 1;
98 }
99 
100 #define TSDIO_GET(x)	bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
101 	(TSDIO_ ## x))
102 
103 #define TSDIO_SET(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
104 	(TSDIO_ ## x), (y))
105 
106 #define TSDIO_SETBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
107 	(TSDIO_ ## x), TSDIO_GET(x) | (y))
108 
109 #define TSDIO_CLEARBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
110 	(TSDIO_ ## x), TSDIO_GET(x) & (~(y)))
111 
112 static void
toasterlcd_attach(device_t parent,device_t self,void * aux)113 toasterlcd_attach(device_t parent, device_t self, void *aux)
114 {
115 	struct toasterlcd_softc *sc = device_private(self);
116 	struct tsdio_attach_args *taa = aux;
117 	struct wsemuldisplaydev_attach_args waa;
118 
119 	sc->sc_iot = taa->ta_iot;
120 	sc->sc_gpioh = taa->ta_ioh;
121 
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;
127 
128 	sc->sc_hlcd.sc_writereg = toasterlcd_writereg;
129 	sc->sc_hlcd.sc_readreg = toasterlcd_readreg;
130 
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 */
134 
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");
138 
139 	hd44780_attach_subr(&sc->sc_hlcd);
140 
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);
146 }
147 
148 static void
toasterlcd_writereg(struct hd44780_chip * hd,u_int32_t en,u_int32_t rs,u_int8_t cmd)149 toasterlcd_writereg(struct hd44780_chip *hd, u_int32_t en, u_int32_t rs, u_int8_t cmd)
150 {
151 	struct toasterlcd_softc *sc = device_private(hd->sc_dev);
152 	u_int8_t ctrl;
153 
154 	if (hd->sc_dev_ok == 0)
155 		return;
156 
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 */
166 	}
167 	TSDIO_SET(PBDR, ctrl);
168 
169 	/* Step 2: setup time delay */
170 	delay(1);
171 
172 	/* Step 3: assert EN */
173 	if (en == 1) ctrl |= 0x8;
174 	else ctrl |= 0x4;
175 	TSDIO_SET(PBDR, ctrl);
176 
177 	/* Step 4: pulse time delay */
178 	delay(1);
179 
180 	/* Step 5: de-assert EN */
181 	if (en == 1) ctrl &= ~0x8;
182 	else ctrl &= ~0x4;
183 	TSDIO_SET(PBDR, ctrl);
184 
185 	/* Step 6: hold time delay */
186 	delay(1);
187 
188 	/* Step 7: de-assert WR */
189 	ctrl |= 0x2;
190 	TSDIO_SET(PBDR, ctrl);
191 
192 	/* Step 8: minimum delay till next bus-cycle */
193 	delay(1000);
194 }
195 
196 static u_int8_t
toasterlcd_readreg(struct hd44780_chip * hd,u_int32_t en,u_int32_t rs)197 toasterlcd_readreg(struct hd44780_chip *hd, u_int32_t en, u_int32_t rs)
198 {
199 	struct toasterlcd_softc *sc = device_private(hd->sc_dev);
200 	u_int8_t ret, ctrl;
201 
202 	if (hd->sc_dev_ok == 0)
203 		return 0;
204 
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 */
213 	}
214 	TSDIO_SET(PBDR, ctrl);
215 
216 	/* Step 2: setup time delay */
217 	delay(1);
218 
219 	/* Step 3: assert EN */
220 	if (en == 1) ctrl |= 0x8;
221 	else ctrl |= 0x4;
222 	TSDIO_SET(PBDR, ctrl);
223 
224 	/* Step 4: pulse time delay */
225 	delay(1);
226 
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);
232 
233 	/* Step 6: hold time delay + min bus cycle interval*/
234 	delay(1000);
235 	return ret;
236 }
237 
238