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