xref: /netbsd/sys/arch/evbarm/tsarm/tslcd.c (revision 95e1ffb1)
1 /* $NetBSD: tslcd.c,v 1.6 2005/12/11 12:17:11 christos 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: tslcd.c,v 1.6 2005/12/11 12:17:11 christos Exp $");
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/proc.h>
44 #include <sys/poll.h>
45 #include <sys/conf.h>
46 #include <sys/uio.h>
47 #include <sys/types.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/callout.h>
51 #include <sys/select.h>
52 #include <sys/conf.h>
53 
54 #include <machine/bus.h>
55 #include <machine/autoconf.h>
56 
57 #include <dev/wscons/wsdisplayvar.h>
58 #include <dev/wscons/wsconsio.h>
59 #include <dev/wscons/wscons_callbacks.h>
60 
61 #include <arm/ep93xx/ep93xxreg.h>
62 #include <dev/ic/hd44780reg.h>
63 #include <dev/ic/hd44780var.h>
64 #include <evbarm/tsarm/tspldvar.h>
65 #include <evbarm/tsarm/tsarmreg.h>
66 
67 struct tslcd_softc {
68 	struct device sc_dev;
69 	struct hd44780_chip sc_hlcd;
70 	bus_space_tag_t sc_iot;
71 	bus_space_handle_t sc_gpioh;
72 };
73 
74 static int	tslcd_match(struct device *, struct cfdata *, void *);
75 static void	tslcd_attach(struct device *, struct device *, void *);
76 
77 static void	tslcd_writereg(struct hd44780_chip *, u_int32_t, u_int32_t, u_int8_t);
78 static u_int8_t	tslcd_readreg(struct hd44780_chip *, u_int32_t, u_int32_t);
79 
80 dev_type_open(tslcdopen);
81 dev_type_close(tslcdclose);
82 dev_type_read(tslcdread);
83 dev_type_write(tslcdwrite);
84 dev_type_ioctl(tslcdioctl);
85 dev_type_poll(tslcdpoll);
86 
87 const struct cdevsw tslcd_cdevsw = {
88 	tslcdopen, tslcdclose, tslcdread, tslcdwrite, tslcdioctl,
89 	nostop, notty, tslcdpoll, nommap,
90 };
91 
92 extern const struct wsdisplay_emulops hlcd_emulops;
93 extern const struct wsdisplay_accessops hlcd_accessops;
94 extern struct cfdriver tslcd_cd;
95 
96 CFATTACH_DECL(tslcd, sizeof(struct tslcd_softc),
97     tslcd_match, tslcd_attach, NULL, NULL);
98 
99 static const struct wsscreen_descr tslcd_stdscreen = {
100 	"std_tslcd", 24, 2,
101 	&hlcd_emulops,
102 	5, 7,
103 	0,
104 };
105 
106 static const struct wsscreen_descr *_tslcd_scrlist[] = {
107 	&tslcd_stdscreen,
108 };
109 
110 static const struct wsscreen_list tslcd_screenlist = {
111 	sizeof(_tslcd_scrlist) / sizeof(struct wsscreen_descr *),
112 	_tslcd_scrlist,
113 };
114 
115 static int
116 tslcd_match(parent, match, aux)
117 	struct device *parent;
118 	struct cfdata *match;
119 	void *aux;
120 {
121 	return 1;
122 }
123 
124 #define GPIO_GET(x)	bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
125 	(EP93XX_GPIO_ ## x))
126 
127 #define GPIO_SET(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
128 	(EP93XX_GPIO_ ## x), (y))
129 
130 #define GPIO_SETBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
131 	(EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
132 
133 #define GPIO_CLEARBITS(x, y)	bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
134 	(EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
135 
136 static void
137 tslcd_attach(parent, self, aux)
138 	struct device *parent;
139 	struct device *self;
140 	void *aux;
141 {
142 	struct tslcd_softc *sc = (void *)self;
143 	struct tspld_attach_args *taa = aux;
144 	struct wsemuldisplaydev_attach_args waa;
145 
146 	sc->sc_iot = taa->ta_iot;
147 	if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
148 		EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
149 		panic("tslcd_attach: couldn't map GPIO registers");
150 
151 	sc->sc_hlcd.sc_dev_ok = 1;
152 	sc->sc_hlcd.sc_cols = 24;
153 	sc->sc_hlcd.sc_vcols = 40;
154 	sc->sc_hlcd.sc_flags = HD_8BIT | HD_MULTILINE;
155 	sc->sc_hlcd.sc_dev = self;
156 
157 	sc->sc_hlcd.sc_writereg = tslcd_writereg;
158 	sc->sc_hlcd.sc_readreg = tslcd_readreg;
159 
160 	GPIO_SET(PADDR, 0);		/* Port A to inputs */
161 	GPIO_SETBITS(PHDDR, 0x38);	/* Bits 3:5 of Port H to outputs */
162 	GPIO_CLEARBITS(PHDR, 0x18);	/* De-assert EN, De-assert RS */
163 
164 	printf("\n");
165 
166 	hd44780_attach_subr(&sc->sc_hlcd);
167 
168 	waa.console = 0;
169 	waa.scrdata = &tslcd_screenlist;
170 	waa.accessops = &hlcd_accessops;
171 	waa.accesscookie = &sc->sc_hlcd.sc_screen;
172 	config_found(self, &waa, wsemuldisplaydevprint);
173 }
174 
175 static void
176 tslcd_writereg(hd, en, rs, cmd)
177 	struct hd44780_chip *hd;
178 	u_int32_t en, rs;
179 	u_int8_t cmd;
180 {
181 	struct tslcd_softc *sc = (struct tslcd_softc *)hd->sc_dev;
182 	u_int8_t ctrl;
183 
184 	if (hd->sc_dev_ok == 0)
185 		return;
186 
187 	/* Step 1: Apply RS & WR, Send data */
188 	ctrl = GPIO_GET(PHDR);
189 	GPIO_SET(PADDR, 0xff); /* set port A to outputs */
190 	GPIO_SET(PADR, cmd);
191 	if (rs) {
192 		ctrl |= 0x10;	/* assert RS */
193 		ctrl &= ~0x20;	/* assert WR */
194 	} else {
195 		ctrl &= ~0x30;	/* de-assert WR, de-assert RS */
196 	}
197 	GPIO_SET(PHDR, ctrl);
198 
199 	/* Step 2: setup time delay */
200 	delay(1);
201 
202 	/* Step 3: assert EN */
203 	ctrl |= 0x8;
204 	GPIO_SET(PHDR, ctrl);
205 
206 	/* Step 4: pulse time delay */
207 	delay(1);
208 
209 	/* Step 5: de-assert EN */
210 	ctrl &= ~0x8;
211 	GPIO_SET(PHDR, ctrl);
212 
213 	/* Step 6: hold time delay */
214 	delay(1);
215 
216 	/* Step 7: de-assert WR */
217 	ctrl |= 0x2;
218 	GPIO_SET(PHDR, ctrl);
219 
220 	/* Step 8: minimum delay till next bus-cycle */
221 	delay(1000);
222 }
223 
224 static u_int8_t
225 tslcd_readreg(hd, en, rs)
226 	struct hd44780_chip *hd;
227 	u_int32_t en, rs;
228 {
229 	struct tslcd_softc *sc = (struct tslcd_softc *)hd->sc_dev;
230 	u_int8_t ret, ctrl;
231 
232 	if (hd->sc_dev_ok == 0)
233 		return 0;
234 
235 	/* Step 1: Apply RS & WR, Send data */
236 	ctrl = GPIO_GET(PHDR);
237 	GPIO_SET(PADDR, 0x0);	/* set port A to inputs */
238 	if (rs) {
239 		ctrl |= 0x30;	/* de-assert WR, assert RS */
240 	} else {
241 		ctrl |= 0x20;	/* de-assert WR */
242 		ctrl &= ~0x10;	/* de-assert RS */
243 	}
244 	GPIO_SET(PHDR, ctrl);
245 
246 	/* Step 2: setup time delay */
247 	delay(1);
248 
249 	/* Step 3: assert EN */
250 	ctrl |= 0x8;
251 	GPIO_SET(PHDR, ctrl);
252 
253 	/* Step 4: pulse time delay */
254 	delay(1);
255 
256 	/* Step 5: de-assert EN */
257 	ret = GPIO_GET(PADR) & 0xff;
258 	ctrl &= ~0x8;
259 	GPIO_SET(PHDR, ctrl);
260 
261 	/* Step 6: hold time delay + min bus cycle interval*/
262 	delay(1000);
263 	return ret;
264 }
265 
266 int
267 tslcdopen(dev, flag, mode, p)
268 	dev_t dev;
269 	int flag, mode;
270 	struct proc *p;
271 {
272 	struct tslcd_softc *sc = device_lookup(&tslcd_cd, minor(dev));
273 
274 	if (sc->sc_hlcd.sc_dev_ok == 0)
275 		return hd44780_init(&sc->sc_hlcd);
276 	else
277 		return 0;
278 }
279 
280 int
281 tslcdclose(dev, flag, mode, p)
282 	dev_t dev;
283 	int flag, mode;
284 	struct proc *p;
285 {
286 	return 0;
287 }
288 
289 int
290 tslcdread(dev, uio, flag)
291 	dev_t dev;
292 	struct uio *uio;
293 	int flag;
294 {
295 	return EIO;
296 }
297 
298 int
299 tslcdwrite(dev, uio, flag)
300 	dev_t dev;
301 	struct uio *uio;
302 	int flag;
303 {
304 	int error;
305 	struct hd44780_io io;
306 	struct tslcd_softc *sc = device_lookup(&tslcd_cd, minor(dev));
307 
308 	if (sc->sc_hlcd.sc_dev_ok == 0)
309 		return EIO;
310 
311 	io.dat = 0;
312 	io.len = uio->uio_resid;
313 	if (io.len > HD_MAX_CHARS)
314 		io.len = HD_MAX_CHARS;
315 
316 	if ((error = uiomove((void*)io.buf, io.len, uio)) != 0)
317 		return error;
318 
319 	hd44780_ddram_redraw(&sc->sc_hlcd, sc->sc_hlcd.sc_curchip, &io);
320 	return 0;
321 }
322 
323 int
324 tslcdioctl(dev, cmd, data, flag, p)
325 	dev_t dev;
326 	u_long cmd;
327 	caddr_t data;
328 	int flag;
329 	struct proc *p;
330 {
331 	struct tslcd_softc *sc = device_lookup(&tslcd_cd, minor(dev));
332 	return hd44780_ioctl_subr(&sc->sc_hlcd, cmd, data);
333 }
334 
335 int
336 tslcdpoll(dev, events, p)
337 	dev_t dev;
338 	int events;
339 	struct proc *p;
340 {
341 	return 0;
342 }
343