lcdpanel.c revision 1.1 1 1.1 christos /* $NetBSD: lcdpanel.c,v 1.1 2018/04/09 20:16:16 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2002 Dennis I. Chernoivanov
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. The name of the author may not be used to endorse or promote products
16 1.1 christos * derived from this software without specific prior written permission
17 1.1 christos *
18 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 christos */
29 1.1 christos
30 1.1 christos #include <sys/cdefs.h>
31 1.1 christos __KERNEL_RCSID(0, "$NetBSD: lcdpanel.c,v 1.1 2018/04/09 20:16:16 christos Exp $");
32 1.1 christos
33 1.1 christos #include <sys/param.h>
34 1.1 christos #include <sys/systm.h>
35 1.1 christos #include <sys/proc.h>
36 1.1 christos #include <sys/poll.h>
37 1.1 christos #include <sys/conf.h>
38 1.1 christos #include <sys/uio.h>
39 1.1 christos #include <sys/types.h>
40 1.1 christos #include <sys/kernel.h>
41 1.1 christos #include <sys/device.h>
42 1.1 christos #include <sys/callout.h>
43 1.1 christos #include <sys/select.h>
44 1.1 christos #include <sys/reboot.h>
45 1.1 christos
46 1.1 christos #include <sys/bus.h>
47 1.1 christos #include <machine/autoconf.h>
48 1.1 christos
49 1.1 christos #include <dev/ic/hd44780reg.h>
50 1.1 christos #include <dev/ic/hd44780var.h>
51 1.1 christos #include <dev/ic/lcdkp_subr.h>
52 1.1 christos
53 1.1 christos #include "ioconf.h"
54 1.1 christos
55 1.1 christos #define LCDPANEL_POLLRATE (hz / 10)
56 1.1 christos #define LCDPANEL_REGION 0x20
57 1.1 christos #define DATA_OFFSET 0x10
58 1.1 christos #define LCDPANEL_COLS 16
59 1.1 christos #define LCDPANEL_VCOLS 40
60 1.1 christos #define LCDPANEL_ROWS 2
61 1.1 christos
62 1.1 christos struct lcdpanel_softc {
63 1.1 christos device_t sc_dev;
64 1.1 christos
65 1.1 christos struct hd44780_chip sc_lcd;
66 1.1 christos struct lcdkp_chip sc_kp;
67 1.1 christos
68 1.1 christos struct selinfo sc_selq;
69 1.1 christos struct callout sc_callout;
70 1.1 christos };
71 1.1 christos
72 1.1 christos struct lcd_message {
73 1.1 christos const char firstcol[LCDPANEL_VCOLS];
74 1.1 christos const char secondcol[LCDPANEL_VCOLS];
75 1.1 christos };
76 1.1 christos static const struct lcd_message startup_message = {
77 1.1 christos "NetBSD/cobalt ",
78 1.1 christos "Starting up... "
79 1.1 christos };
80 1.1 christos static const struct lcd_message halt_message = {
81 1.1 christos "NetBSD/cobalt ",
82 1.1 christos "Halting... "
83 1.1 christos };
84 1.1 christos static const struct lcd_message reboot_message = {
85 1.1 christos "NetBSD/cobalt ",
86 1.1 christos "Rebooting... "
87 1.1 christos };
88 1.1 christos
89 1.1 christos static int lcdpanel_match(device_t, cfdata_t, void *);
90 1.1 christos static void lcdpanel_attach(device_t, device_t, void *);
91 1.1 christos static bool lcdpanel_shutdown(device_t, int);
92 1.1 christos
93 1.1 christos static void lcdpanel_soft(void *);
94 1.1 christos
95 1.1 christos static uint8_t lcdpanel_cbt_kprread(bus_space_tag_t, bus_space_handle_t);
96 1.1 christos static uint8_t lcdpanel_cbt_hdreadreg(struct hd44780_chip *, uint32_t, uint32_t);
97 1.1 christos static void lcdpanel_cbt_hdwritereg(struct hd44780_chip *, uint32_t, uint32_t,
98 1.1 christos uint8_t);
99 1.1 christos
100 1.1 christos dev_type_open(lcdpanelopen);
101 1.1 christos dev_type_close(lcdpanelclose);
102 1.1 christos dev_type_read(lcdpanelread);
103 1.1 christos dev_type_write(lcdpanelwrite);
104 1.1 christos dev_type_ioctl(lcdpanelioctl);
105 1.1 christos dev_type_poll(lcdpanelpoll);
106 1.1 christos
107 1.1 christos const struct cdevsw lcdpanel_cdevsw = {
108 1.1 christos .d_open = lcdpanelopen,
109 1.1 christos .d_close = lcdpanelclose,
110 1.1 christos .d_read = lcdpanelread,
111 1.1 christos .d_write = lcdpanelwrite,
112 1.1 christos .d_ioctl = lcdpanelioctl,
113 1.1 christos .d_stop = nostop,
114 1.1 christos .d_tty = notty,
115 1.1 christos .d_poll = lcdpanelpoll,
116 1.1 christos .d_mmap = nommap,
117 1.1 christos .d_kqfilter = nokqfilter,
118 1.1 christos .d_discard = nodiscard,
119 1.1 christos .d_flag = 0
120 1.1 christos };
121 1.1 christos
122 1.1 christos CFATTACH_DECL_NEW(lcdpanel, sizeof(struct lcdpanel_softc),
123 1.1 christos lcdpanel_match, lcdpanel_attach, NULL, NULL);
124 1.1 christos
125 1.1 christos static int
126 1.1 christos lcdpanel_match(device_t parent, cfdata_t cf, void *aux)
127 1.1 christos {
128 1.1 christos
129 1.1 christos return 1;
130 1.1 christos }
131 1.1 christos
132 1.1 christos static void
133 1.1 christos lcdpanel_attach(device_t parent, device_t self, void *aux)
134 1.1 christos {
135 1.1 christos struct lcdpanel_softc *sc = device_private(self);
136 1.1 christos struct mainbus_attach_args *maa = aux;
137 1.1 christos struct hd44780_io io;
138 1.1 christos static struct lcdkp_xlate keys[] = {
139 1.1 christos { 0xfa, 'h' },
140 1.1 christos { 0xf6, 'k' },
141 1.1 christos { 0xde, 'l' },
142 1.1 christos { 0xee, 'j' },
143 1.1 christos { 0x7e, 's' },
144 1.1 christos { 0xbe, 'e' }
145 1.1 christos };
146 1.1 christos
147 1.1 christos sc->sc_lcd.sc_dev = self;
148 1.1 christos sc->sc_lcd.sc_iot = maa->ma_iot;
149 1.1 christos if (bus_space_map(sc->sc_lcd.sc_iot, maa->ma_addr, LCDPANEL_REGION,
150 1.1 christos 0, &sc->sc_lcd.sc_ioir)) {
151 1.1 christos aprint_error(": unable to map registers\n");
152 1.1 christos return;
153 1.1 christos }
154 1.1 christos bus_space_subregion(sc->sc_lcd.sc_iot, sc->sc_lcd.sc_ioir, DATA_OFFSET,
155 1.1 christos 1, &sc->sc_lcd.sc_iodr);
156 1.1 christos
157 1.1 christos printf("\n");
158 1.1 christos
159 1.1 christos sc->sc_lcd.sc_dev_ok = 1;
160 1.1 christos sc->sc_lcd.sc_cols = LCDPANEL_COLS;
161 1.1 christos sc->sc_lcd.sc_vcols = LCDPANEL_VCOLS;
162 1.1 christos sc->sc_lcd.sc_flags = HD_8BIT | HD_MULTILINE | HD_KEYPAD;
163 1.1 christos
164 1.1 christos sc->sc_lcd.sc_writereg = lcdpanel_cbt_hdwritereg;
165 1.1 christos sc->sc_lcd.sc_readreg = lcdpanel_cbt_hdreadreg;
166 1.1 christos
167 1.1 christos hd44780_attach_subr(&sc->sc_lcd);
168 1.1 christos
169 1.1 christos /* Hello World */
170 1.1 christos io.dat = 0;
171 1.1 christos io.len = LCDPANEL_VCOLS * LCDPANEL_ROWS;
172 1.1 christos memcpy(io.buf, &startup_message, io.len);
173 1.1 christos hd44780_ddram_io(&sc->sc_lcd, sc->sc_lcd.sc_curchip, &io,
174 1.1 christos HD_DDRAM_WRITE);
175 1.1 christos
176 1.1 christos pmf_device_register1(self, NULL, NULL, lcdpanel_shutdown);
177 1.1 christos
178 1.1 christos sc->sc_kp.sc_iot = maa->ma_iot;
179 1.1 christos sc->sc_kp.sc_ioh = MIPS_PHYS_TO_KSEG1(LCDPANEL_BASE); /* XXX */
180 1.1 christos
181 1.1 christos sc->sc_kp.sc_knum = sizeof(keys) / sizeof(struct lcdkp_xlate);
182 1.1 christos sc->sc_kp.sc_kpad = keys;
183 1.1 christos sc->sc_kp.sc_rread = lcdpanel_cbt_kprread;
184 1.1 christos
185 1.1 christos lcdkp_attach_subr(&sc->sc_kp);
186 1.1 christos
187 1.1 christos callout_init(&sc->sc_callout, 0);
188 1.1 christos selinit(&sc->sc_selq);
189 1.1 christos }
190 1.1 christos
191 1.1 christos static bool
192 1.1 christos lcdpanel_shutdown(device_t self, int howto)
193 1.1 christos {
194 1.1 christos struct lcdpanel_softc *sc = device_private(self);
195 1.1 christos struct hd44780_io io;
196 1.1 christos
197 1.1 christos /* Goodbye World */
198 1.1 christos io.dat = 0;
199 1.1 christos io.len = LCDPANEL_VCOLS * LCDPANEL_ROWS;
200 1.1 christos if (howto & RB_HALT)
201 1.1 christos memcpy(io.buf, &halt_message, io.len);
202 1.1 christos else
203 1.1 christos memcpy(io.buf, &reboot_message, io.len);
204 1.1 christos hd44780_ddram_io(&sc->sc_lcd, sc->sc_lcd.sc_curchip, &io,
205 1.1 christos HD_DDRAM_WRITE);
206 1.1 christos
207 1.1 christos return true;
208 1.1 christos }
209 1.1 christos
210 1.1 christos static uint8_t
211 1.1 christos lcdpanel_cbt_kprread(bus_space_tag_t iot, bus_space_handle_t ioh)
212 1.1 christos {
213 1.1 christos
214 1.1 christos delay(HD_TIMEOUT_NORMAL);
215 1.1 christos return (bus_space_read_4(iot, ioh, 0x00) >> 24) & 0xff;
216 1.1 christos }
217 1.1 christos
218 1.1 christos
219 1.1 christos static void
220 1.1 christos lcdpanel_cbt_hdwritereg(struct hd44780_chip *hd, uint32_t en, uint32_t rs,
221 1.1 christos uint8_t dat)
222 1.1 christos {
223 1.1 christos
224 1.1 christos if (rs)
225 1.1 christos bus_space_write_4(hd->sc_iot, hd->sc_iodr, 0x00, dat << 24);
226 1.1 christos else
227 1.1 christos bus_space_write_4(hd->sc_iot, hd->sc_ioir, 0x00, dat << 24);
228 1.1 christos delay(HD_TIMEOUT_NORMAL);
229 1.1 christos }
230 1.1 christos
231 1.1 christos static uint8_t
232 1.1 christos lcdpanel_cbt_hdreadreg(struct hd44780_chip *hd, uint32_t en, uint32_t rs)
233 1.1 christos {
234 1.1 christos
235 1.1 christos delay(HD_TIMEOUT_NORMAL);
236 1.1 christos if (rs)
237 1.1 christos return (bus_space_read_4(hd->sc_iot, hd->sc_iodr, 0x00) >> 24)
238 1.1 christos & 0xff;
239 1.1 christos else
240 1.1 christos return (bus_space_read_4(hd->sc_iot, hd->sc_ioir, 0x00) >> 24)
241 1.1 christos & 0xff;
242 1.1 christos }
243 1.1 christos
244 1.1 christos int
245 1.1 christos lcdpanelopen(dev_t dev, int flag, int mode, struct lwp *l)
246 1.1 christos {
247 1.1 christos struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
248 1.1 christos
249 1.1 christos return (sc->sc_lcd.sc_dev_ok == 0) ? ENXIO : 0;
250 1.1 christos }
251 1.1 christos
252 1.1 christos int
253 1.1 christos lcdpanelclose(dev_t dev, int flag, int mode, struct lwp *l)
254 1.1 christos {
255 1.1 christos struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
256 1.1 christos
257 1.1 christos selnotify(&sc->sc_selq, 0, 0);
258 1.1 christos return 0;
259 1.1 christos }
260 1.1 christos
261 1.1 christos int
262 1.1 christos lcdpanelread(dev_t dev, struct uio *uio, int flag)
263 1.1 christos {
264 1.1 christos int error;
265 1.1 christos uint8_t b;
266 1.1 christos struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
267 1.1 christos
268 1.1 christos if (uio->uio_resid < sizeof(b))
269 1.1 christos return EIO;
270 1.1 christos
271 1.1 christos if ((error = lcdkp_readkey(&sc->sc_kp, &b)) != 0)
272 1.1 christos return error;
273 1.1 christos
274 1.1 christos return uiomove((void*)&b, sizeof(b), uio);
275 1.1 christos }
276 1.1 christos
277 1.1 christos int
278 1.1 christos lcdpanelwrite(dev_t dev, struct uio *uio, int flag)
279 1.1 christos {
280 1.1 christos int error;
281 1.1 christos struct hd44780_io io;
282 1.1 christos struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
283 1.1 christos
284 1.1 christos io.dat = 0;
285 1.1 christos io.len = uio->uio_resid;
286 1.1 christos if (io.len > HD_MAX_CHARS)
287 1.1 christos io.len = HD_MAX_CHARS;
288 1.1 christos
289 1.1 christos if ((error = uiomove((void*)io.buf, io.len, uio)) != 0)
290 1.1 christos return error;
291 1.1 christos
292 1.1 christos hd44780_ddram_redraw(&sc->sc_lcd, 0, &io);
293 1.1 christos return 0;
294 1.1 christos }
295 1.1 christos
296 1.1 christos int
297 1.1 christos lcdpanelioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
298 1.1 christos {
299 1.1 christos struct lcdpanel_softc *sc = device_lookup_private(&lcdpanel_cd, minor(dev));
300 1.1 christos
301 1.1 christos return hd44780_ioctl_subr(&sc->sc_lcd, cmd, data);
302 1.1 christos }
303 1.1 christos
304 1.1 christos int
305 1.1 christos lcdpanelpoll(dev_t dev, int events, struct lwp *l)
306 1.1 christos {
307 1.1 christos int revents = 0;
308 1.1 christos
309 1.1 christos if ((events & (POLLIN | POLLRDNORM)) != 0) {
310 1.1 christos struct lcdpanel_softc *sc;
311 1.1 christos
312 1.1 christos sc = device_lookup_private(&lcdpanel_cd, minor(dev));
313 1.1 christos if (lcdkp_scankey(&sc->sc_kp) != 0) {
314 1.1 christos revents = events & (POLLIN | POLLRDNORM);
315 1.1 christos } else {
316 1.1 christos selrecord(l, &sc->sc_selq);
317 1.1 christos callout_reset(&sc->sc_callout, LCDPANEL_POLLRATE,
318 1.1 christos lcdpanel_soft, sc);
319 1.1 christos }
320 1.1 christos }
321 1.1 christos
322 1.1 christos return revents;
323 1.1 christos }
324 1.1 christos
325 1.1 christos static void
326 1.1 christos lcdpanel_soft(void *arg)
327 1.1 christos {
328 1.1 christos struct lcdpanel_softc *sc = arg;
329 1.1 christos
330 1.1 christos if (lcdkp_scankey(&sc->sc_kp) != 0)
331 1.1 christos selnotify(&sc->sc_selq, 0, 0);
332 1.1 christos else
333 1.1 christos callout_reset(&sc->sc_callout, LCDPANEL_POLLRATE, lcdpanel_soft, sc);
334 1.1 christos }
335