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