tslcd.c revision 1.7 1 /* $NetBSD: tslcd.c,v 1.7 2006/08/31 17:53:19 matt 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.7 2006/08/31 17:53:19 matt 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 <arm/ep93xx/epgpioreg.h>
63 #include <dev/ic/hd44780reg.h>
64 #include <dev/ic/hd44780var.h>
65 #include <evbarm/tsarm/tspldvar.h>
66 #include <evbarm/tsarm/tsarmreg.h>
67
68 struct tslcd_softc {
69 struct device sc_dev;
70 struct hd44780_chip sc_hlcd;
71 bus_space_tag_t sc_iot;
72 bus_space_handle_t sc_gpioh;
73 };
74
75 static int tslcd_match(struct device *, struct cfdata *, void *);
76 static void tslcd_attach(struct device *, struct device *, void *);
77
78 static void tslcd_writereg(struct hd44780_chip *, u_int32_t, u_int32_t, u_int8_t);
79 static u_int8_t tslcd_readreg(struct hd44780_chip *, u_int32_t, u_int32_t);
80
81 dev_type_open(tslcdopen);
82 dev_type_close(tslcdclose);
83 dev_type_read(tslcdread);
84 dev_type_write(tslcdwrite);
85 dev_type_ioctl(tslcdioctl);
86 dev_type_poll(tslcdpoll);
87
88 const struct cdevsw tslcd_cdevsw = {
89 tslcdopen, tslcdclose, tslcdread, tslcdwrite, tslcdioctl,
90 nostop, notty, tslcdpoll, nommap,
91 };
92
93 extern const struct wsdisplay_emulops hlcd_emulops;
94 extern const struct wsdisplay_accessops hlcd_accessops;
95 extern struct cfdriver tslcd_cd;
96
97 CFATTACH_DECL(tslcd, sizeof(struct tslcd_softc),
98 tslcd_match, tslcd_attach, NULL, NULL);
99
100 static const struct wsscreen_descr tslcd_stdscreen = {
101 "std_tslcd", 24, 2,
102 &hlcd_emulops,
103 5, 7,
104 0,
105 };
106
107 static const struct wsscreen_descr *_tslcd_scrlist[] = {
108 &tslcd_stdscreen,
109 };
110
111 static const struct wsscreen_list tslcd_screenlist = {
112 sizeof(_tslcd_scrlist) / sizeof(struct wsscreen_descr *),
113 _tslcd_scrlist,
114 };
115
116 static int
117 tslcd_match(parent, match, aux)
118 struct device *parent;
119 struct cfdata *match;
120 void *aux;
121 {
122 return 1;
123 }
124
125 #define GPIO_GET(x) bus_space_read_1(sc->sc_iot, sc->sc_gpioh, \
126 (EP93XX_GPIO_ ## x))
127
128 #define GPIO_SET(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
129 (EP93XX_GPIO_ ## x), (y))
130
131 #define GPIO_SETBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
132 (EP93XX_GPIO_ ## x), GPIO_GET(x) | (y))
133
134 #define GPIO_CLEARBITS(x, y) bus_space_write_1(sc->sc_iot, sc->sc_gpioh, \
135 (EP93XX_GPIO_ ## x), GPIO_GET(x) & (~(y)))
136
137 static void
138 tslcd_attach(parent, self, aux)
139 struct device *parent;
140 struct device *self;
141 void *aux;
142 {
143 struct tslcd_softc *sc = (void *)self;
144 struct tspld_attach_args *taa = aux;
145 struct wsemuldisplaydev_attach_args waa;
146
147 sc->sc_iot = taa->ta_iot;
148 if (bus_space_map(sc->sc_iot, EP93XX_APB_HWBASE + EP93XX_APB_GPIO,
149 EP93XX_APB_GPIO_SIZE, 0, &sc->sc_gpioh))
150 panic("tslcd_attach: couldn't map GPIO registers");
151
152 sc->sc_hlcd.sc_dev_ok = 1;
153 sc->sc_hlcd.sc_cols = 24;
154 sc->sc_hlcd.sc_vcols = 40;
155 sc->sc_hlcd.sc_flags = HD_8BIT | HD_MULTILINE;
156 sc->sc_hlcd.sc_dev = self;
157
158 sc->sc_hlcd.sc_writereg = tslcd_writereg;
159 sc->sc_hlcd.sc_readreg = tslcd_readreg;
160
161 GPIO_SET(PADDR, 0); /* Port A to inputs */
162 GPIO_SETBITS(PHDDR, 0x38); /* Bits 3:5 of Port H to outputs */
163 GPIO_CLEARBITS(PHDR, 0x18); /* De-assert EN, De-assert RS */
164
165 printf("\n");
166
167 hd44780_attach_subr(&sc->sc_hlcd);
168
169 waa.console = 0;
170 waa.scrdata = &tslcd_screenlist;
171 waa.accessops = &hlcd_accessops;
172 waa.accesscookie = &sc->sc_hlcd.sc_screen;
173 config_found(self, &waa, wsemuldisplaydevprint);
174 }
175
176 static void
177 tslcd_writereg(hd, en, rs, cmd)
178 struct hd44780_chip *hd;
179 u_int32_t en, rs;
180 u_int8_t cmd;
181 {
182 struct tslcd_softc *sc = (struct tslcd_softc *)hd->sc_dev;
183 u_int8_t ctrl;
184
185 if (hd->sc_dev_ok == 0)
186 return;
187
188 /* Step 1: Apply RS & WR, Send data */
189 ctrl = GPIO_GET(PHDR);
190 GPIO_SET(PADDR, 0xff); /* set port A to outputs */
191 GPIO_SET(PADR, cmd);
192 if (rs) {
193 ctrl |= 0x10; /* assert RS */
194 ctrl &= ~0x20; /* assert WR */
195 } else {
196 ctrl &= ~0x30; /* de-assert WR, de-assert RS */
197 }
198 GPIO_SET(PHDR, ctrl);
199
200 /* Step 2: setup time delay */
201 delay(1);
202
203 /* Step 3: assert EN */
204 ctrl |= 0x8;
205 GPIO_SET(PHDR, ctrl);
206
207 /* Step 4: pulse time delay */
208 delay(1);
209
210 /* Step 5: de-assert EN */
211 ctrl &= ~0x8;
212 GPIO_SET(PHDR, ctrl);
213
214 /* Step 6: hold time delay */
215 delay(1);
216
217 /* Step 7: de-assert WR */
218 ctrl |= 0x2;
219 GPIO_SET(PHDR, ctrl);
220
221 /* Step 8: minimum delay till next bus-cycle */
222 delay(1000);
223 }
224
225 static u_int8_t
226 tslcd_readreg(hd, en, rs)
227 struct hd44780_chip *hd;
228 u_int32_t en, rs;
229 {
230 struct tslcd_softc *sc = (struct tslcd_softc *)hd->sc_dev;
231 u_int8_t ret, ctrl;
232
233 if (hd->sc_dev_ok == 0)
234 return 0;
235
236 /* Step 1: Apply RS & WR, Send data */
237 ctrl = GPIO_GET(PHDR);
238 GPIO_SET(PADDR, 0x0); /* set port A to inputs */
239 if (rs) {
240 ctrl |= 0x30; /* de-assert WR, assert RS */
241 } else {
242 ctrl |= 0x20; /* de-assert WR */
243 ctrl &= ~0x10; /* de-assert RS */
244 }
245 GPIO_SET(PHDR, ctrl);
246
247 /* Step 2: setup time delay */
248 delay(1);
249
250 /* Step 3: assert EN */
251 ctrl |= 0x8;
252 GPIO_SET(PHDR, ctrl);
253
254 /* Step 4: pulse time delay */
255 delay(1);
256
257 /* Step 5: de-assert EN */
258 ret = GPIO_GET(PADR) & 0xff;
259 ctrl &= ~0x8;
260 GPIO_SET(PHDR, ctrl);
261
262 /* Step 6: hold time delay + min bus cycle interval*/
263 delay(1000);
264 return ret;
265 }
266
267 int
268 tslcdopen(dev, flag, mode, l)
269 dev_t dev;
270 int flag, mode;
271 struct lwp *l;
272 {
273 struct tslcd_softc *sc = device_lookup(&tslcd_cd, minor(dev));
274
275 if (sc->sc_hlcd.sc_dev_ok == 0)
276 return hd44780_init(&sc->sc_hlcd);
277 else
278 return 0;
279 }
280
281 int
282 tslcdclose(dev, flag, mode, l)
283 dev_t dev;
284 int flag, mode;
285 struct lwp *l;
286 {
287 return 0;
288 }
289
290 int
291 tslcdread(dev, uio, flag)
292 dev_t dev;
293 struct uio *uio;
294 int flag;
295 {
296 return EIO;
297 }
298
299 int
300 tslcdwrite(dev, uio, flag)
301 dev_t dev;
302 struct uio *uio;
303 int flag;
304 {
305 int error;
306 struct hd44780_io io;
307 struct tslcd_softc *sc = device_lookup(&tslcd_cd, minor(dev));
308
309 if (sc->sc_hlcd.sc_dev_ok == 0)
310 return EIO;
311
312 io.dat = 0;
313 io.len = uio->uio_resid;
314 if (io.len > HD_MAX_CHARS)
315 io.len = HD_MAX_CHARS;
316
317 if ((error = uiomove((void*)io.buf, io.len, uio)) != 0)
318 return error;
319
320 hd44780_ddram_redraw(&sc->sc_hlcd, sc->sc_hlcd.sc_curchip, &io);
321 return 0;
322 }
323
324 int
325 tslcdioctl(dev, cmd, data, flag, l)
326 dev_t dev;
327 u_long cmd;
328 caddr_t data;
329 int flag;
330 struct lwp *l;
331 {
332 struct tslcd_softc *sc = device_lookup(&tslcd_cd, minor(dev));
333 return hd44780_ioctl_subr(&sc->sc_hlcd, cmd, data);
334 }
335
336 int
337 tslcdpoll(dev, events, l)
338 dev_t dev;
339 int events;
340 struct lwp *l;
341 {
342 return 0;
343 }
344