Home | History | Annotate | Line # | Download | only in dev
ds1743.c revision 1.2
      1  1.2  lukem /*	$NetBSD: ds1743.c,v 1.2 2003/07/15 01:37:38 lukem Exp $	*/
      2  1.1    scw 
      3  1.1    scw /*
      4  1.1    scw  * Copyright (c) 2001-2002 Wasabi Sysetms, Inc.
      5  1.1    scw  * Copyright (c) 1998 Mark Brinicombe.
      6  1.1    scw  * Copyright (c) 1998 Causality Limited.
      7  1.1    scw  * All rights reserved.
      8  1.1    scw  *
      9  1.1    scw  * Written by Mark Brinicombe, Causality Limited
     10  1.1    scw  *
     11  1.1    scw  * Redistribution and use in source and binary forms, with or without
     12  1.1    scw  * modification, are permitted provided that the following conditions
     13  1.1    scw  * are met:
     14  1.1    scw  * 1. Redistributions of source code must retain the above copyright
     15  1.1    scw  *    notice, this list of conditions and the following disclaimer.
     16  1.1    scw  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1    scw  *    notice, this list of conditions and the following disclaimer in the
     18  1.1    scw  *    documentation and/or other materials provided with the distribution.
     19  1.1    scw  * 3. All advertising materials mentioning features or use of this software
     20  1.1    scw  *    must display the following acknowledgement:
     21  1.1    scw  *	This product includes software developed by Mark Brinicombe
     22  1.1    scw  *	for the NetBSD Project.
     23  1.1    scw  * 4. The name of the company nor the name of the author may be used to
     24  1.1    scw  *    endorse or promote products derived from this software without specific
     25  1.1    scw  *    prior written permission.
     26  1.1    scw  *
     27  1.1    scw  * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
     28  1.1    scw  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     29  1.1    scw  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     30  1.1    scw  * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
     31  1.1    scw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  1.1    scw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     33  1.1    scw  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  1.1    scw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  1.1    scw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  1.1    scw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  1.1    scw  * SUCH DAMAGE.
     38  1.1    scw  */
     39  1.2  lukem 
     40  1.2  lukem #include <sys/cdefs.h>
     41  1.2  lukem __KERNEL_RCSID(0, "$NetBSD: ds1743.c,v 1.2 2003/07/15 01:37:38 lukem Exp $");
     42  1.1    scw 
     43  1.1    scw #include <sys/param.h>
     44  1.1    scw #include <sys/systm.h>
     45  1.1    scw #include <sys/device.h>
     46  1.1    scw 
     47  1.1    scw #include <machine/rtc.h>
     48  1.1    scw #include <machine/bus.h>
     49  1.1    scw 
     50  1.1    scw #include <evbppc/walnut/dev/ds1743reg.h>
     51  1.1    scw #include <evbppc/walnut/dev/todclockvar.h>
     52  1.1    scw #include <evbppc/walnut/dev/pbusvar.h>
     53  1.1    scw 
     54  1.1    scw struct dsrtc_softc {
     55  1.1    scw 	struct device	sc_dev;
     56  1.1    scw 	bus_space_tag_t	sc_iot;
     57  1.1    scw 	bus_space_handle_t sc_ioh;
     58  1.1    scw };
     59  1.1    scw 
     60  1.1    scw static void dsrtcattach(struct device *, struct device *, void *);
     61  1.1    scw static int dsrtcmatch(struct device *, struct cfdata *, void *);
     62  1.1    scw #if 0	/* Nothing uses these yet */
     63  1.1    scw static int ds1743_ram_read(struct dsrtc_softc *, int);
     64  1.1    scw static void ds1743_ram_write(struct dsrtc_softc *, int, int);
     65  1.1    scw #endif
     66  1.1    scw 
     67  1.1    scw static int dsrtc_read(void *, rtc_t *);
     68  1.1    scw static int dsrtc_write(void *, rtc_t *);
     69  1.1    scw static inline u_char ds1743_read(struct dsrtc_softc *, int);
     70  1.1    scw static inline void ds1743_write(struct dsrtc_softc *, int, u_char);
     71  1.1    scw static u_char ds1743_lock(struct dsrtc_softc *, u_char);
     72  1.1    scw static void ds1743_unlock(struct dsrtc_softc *, u_char);
     73  1.1    scw 
     74  1.1    scw /* device and attach structures */
     75  1.1    scw CFATTACH_DECL(dsrtc, sizeof(struct dsrtc_softc),
     76  1.1    scw     dsrtcmatch, dsrtcattach, NULL, NULL);
     77  1.1    scw 
     78  1.1    scw /*
     79  1.1    scw  * dsrtcmatch()
     80  1.1    scw  *
     81  1.1    scw  * Validate the IIC address to make sure its an RTC we understand
     82  1.1    scw  */
     83  1.1    scw int ds1743found = 0;
     84  1.1    scw 
     85  1.1    scw #define DS_SCRATCH_ADDR 0x1FF7
     86  1.1    scw 
     87  1.1    scw static int
     88  1.1    scw dsrtcmatch(struct device *parent, struct cfdata *cf, void *aux)
     89  1.1    scw {
     90  1.1    scw 	struct pbus_attach_args *paa = aux;
     91  1.1    scw 	int retval = !ds1743found;
     92  1.1    scw 	bus_space_handle_t h;
     93  1.1    scw 	u_int8_t x;
     94  1.1    scw 
     95  1.1    scw 	/* match only RTC devices */
     96  1.1    scw 	if (strcmp(paa->pb_name, cf->cf_name) != 0)
     97  1.1    scw 		return 0;
     98  1.1    scw 
     99  1.1    scw 	if (bus_space_map(0, paa->pb_addr, DS_SIZE, 0, &h)) {
    100  1.1    scw 		printf("%s: can't map i/o space\n", paa->pb_name);
    101  1.1    scw 		return 0;
    102  1.1    scw 	}
    103  1.1    scw 
    104  1.1    scw 	/* Read one byte of what's supposed to be NVRAM */
    105  1.1    scw 	x = bus_space_read_1(0, h, DS_SCRATCH_ADDR);
    106  1.1    scw 	bus_space_write_1(0, h, DS_SCRATCH_ADDR, 0xAA);
    107  1.1    scw 	if (bus_space_read_1(0, h, DS_SCRATCH_ADDR) != 0xAA) {
    108  1.1    scw 		retval = 0;
    109  1.1    scw 		goto done;
    110  1.1    scw 	}
    111  1.1    scw 
    112  1.1    scw 	bus_space_write_1(0, h, DS_SCRATCH_ADDR, 0x55);
    113  1.1    scw 	if (bus_space_read_1(0, h, DS_SCRATCH_ADDR) != 0x55) {
    114  1.1    scw 		retval = 0;
    115  1.1    scw 		goto done;
    116  1.1    scw 	}
    117  1.1    scw 
    118  1.1    scw 	/* Restore scratch byte value */
    119  1.1    scw 	bus_space_write_1(0, h, DS_SCRATCH_ADDR, x);
    120  1.1    scw   done:
    121  1.1    scw 	bus_space_unmap(0, h, DS_SIZE);
    122  1.1    scw 
    123  1.1    scw 	return retval;
    124  1.1    scw }
    125  1.1    scw 
    126  1.1    scw /*
    127  1.1    scw  * dsrtcattach()
    128  1.1    scw  *
    129  1.1    scw  * Attach the rtc device
    130  1.1    scw  */
    131  1.1    scw 
    132  1.1    scw static void
    133  1.1    scw dsrtcattach(struct device *parent, struct device *self, void *aux)
    134  1.1    scw {
    135  1.1    scw 	struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
    136  1.1    scw 	struct pbus_attach_args *paa = aux;
    137  1.1    scw 	struct todclock_attach_args ta;
    138  1.1    scw 
    139  1.1    scw 	ds1743found = 1;
    140  1.1    scw 
    141  1.1    scw 	sc->sc_iot = 0;
    142  1.1    scw 	sc->sc_ioh = paa->pb_addr;
    143  1.1    scw 	if (bus_space_map(sc->sc_iot, paa->pb_addr, DS_SIZE, 0, &sc->sc_ioh)) {
    144  1.1    scw 		printf(": can't map i/o space\n");
    145  1.1    scw 		return;
    146  1.1    scw 	}
    147  1.1    scw 
    148  1.1    scw 	ds1743_unlock(sc,0);	/* Make sure the clock is running */
    149  1.1    scw 	if ((ds1743_read(sc, DS_DAY) & DS_CTL_BF) == 0)
    150  1.1    scw 		printf(": lithium cell is dead, RTC unreliable");
    151  1.1    scw 	printf("\n");
    152  1.1    scw 
    153  1.1    scw #ifdef DEBUG
    154  1.1    scw 	{
    155  1.1    scw 		rtc_t rtc;
    156  1.1    scw 		dsrtc_read(sc,&rtc);
    157  1.1    scw 		printf("RTC: %d/%d/%02d%02d %d:%02d:%02d\n",
    158  1.1    scw 			rtc.rtc_mon, rtc.rtc_day, rtc.rtc_cen, rtc.rtc_year,
    159  1.1    scw 			rtc.rtc_hour, rtc.rtc_min, rtc.rtc_sec);
    160  1.1    scw 	}
    161  1.1    scw #endif
    162  1.1    scw 
    163  1.1    scw 
    164  1.1    scw 	ta.ta_name = "todclock";
    165  1.1    scw 	ta.ta_rtc_arg = sc;
    166  1.1    scw 	ta.ta_rtc_write = dsrtc_write;
    167  1.1    scw 	ta.ta_rtc_read = dsrtc_read;
    168  1.1    scw 	ta.ta_flags = 0;
    169  1.1    scw 	config_found(self, &ta, NULL);
    170  1.1    scw }
    171  1.1    scw 
    172  1.1    scw static inline u_char
    173  1.1    scw ds1743_read(struct dsrtc_softc *sc, int addr)
    174  1.1    scw {
    175  1.1    scw 
    176  1.1    scw 	return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, addr));
    177  1.1    scw }
    178  1.1    scw 
    179  1.1    scw static inline void
    180  1.1    scw ds1743_write(struct dsrtc_softc *sc, int addr, u_char data)
    181  1.1    scw {
    182  1.1    scw 
    183  1.1    scw 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, addr, data);
    184  1.1    scw }
    185  1.1    scw 
    186  1.1    scw 
    187  1.1    scw #if 0	/* Nothing uses these yet */
    188  1.1    scw static u_char
    189  1.1    scw ds1743_ram_read(struct dsrtc_softc *sc, int addr)
    190  1.1    scw {
    191  1.1    scw 
    192  1.1    scw 	if (addr >= DS_RAM_SIZE)
    193  1.1    scw 		return(-1);
    194  1.1    scw 	return(ds1743_read(sc, addr));
    195  1.1    scw }
    196  1.1    scw 
    197  1.1    scw static void
    198  1.1    scw ds1743_ram_write(struct dsrtc_softc *sc, int addr, u_char val)
    199  1.1    scw {
    200  1.1    scw 
    201  1.1    scw 	if (addr >= DS_RAM_SIZE)
    202  1.1    scw 		return (-1);
    203  1.1    scw 	ds1743_write(sc,addr,val);
    204  1.1    scw }
    205  1.1    scw #endif
    206  1.1    scw 
    207  1.1    scw #define BCD(x)	((((x)/10)<<4)|(x%10))
    208  1.1    scw #define unBCD(v,x)	v=x; v = ((v>>4) & 0xf)*10+(v & 0xf)
    209  1.1    scw 
    210  1.1    scw static u_char
    211  1.1    scw ds1743_lock(struct dsrtc_softc *sc, u_char mode)
    212  1.1    scw {
    213  1.1    scw 	u_char octl,ctl;
    214  1.1    scw 
    215  1.1    scw 	octl = ds1743_read(sc,DS_CENTURY);
    216  1.1    scw 	ctl = octl | (mode & DS_CTL_RW);
    217  1.1    scw 	ds1743_write(sc,DS_CENTURY, ctl);	/* Lock RTC for both reading and writing */
    218  1.1    scw 	return octl;
    219  1.1    scw }
    220  1.1    scw 
    221  1.1    scw static void
    222  1.1    scw ds1743_unlock(struct dsrtc_softc *sc, u_char key)
    223  1.1    scw {
    224  1.1    scw 	int ctl;
    225  1.1    scw 
    226  1.1    scw 	ctl = ds1743_read(sc,DS_CENTURY);
    227  1.1    scw 	ctl = (ctl & 0x3f) | (key & DS_CTL_RW);
    228  1.1    scw 	ds1743_write(sc,DS_CENTURY, ctl);	/* Enable updates */
    229  1.1    scw }
    230  1.1    scw 
    231  1.1    scw static int
    232  1.1    scw dsrtc_write(void * arg, rtc_t * rtc)
    233  1.1    scw {
    234  1.1    scw 	struct dsrtc_softc *sc = arg;
    235  1.1    scw 	u_char key;
    236  1.1    scw 
    237  1.1    scw 	key = ds1743_lock(sc,DS_CTL_W);
    238  1.1    scw 
    239  1.1    scw 	ds1743_write(sc, DS_SECONDS, BCD(rtc->rtc_sec) & 0x7f);
    240  1.1    scw 	ds1743_write(sc, DS_MINUTES, BCD(rtc->rtc_min) & 0x7f);
    241  1.1    scw 	ds1743_write(sc, DS_HOURS, BCD(rtc->rtc_hour)  & 0x3f);
    242  1.1    scw 	ds1743_write(sc, DS_DATE, BCD(rtc->rtc_day)    & 0x3f);
    243  1.1    scw 	ds1743_write(sc, DS_MONTH, BCD(rtc->rtc_mon)   & 0x1f);
    244  1.1    scw 	ds1743_write(sc, DS_YEAR, BCD(rtc->rtc_year));
    245  1.1    scw 	ds1743_write(sc, DS_CENTURY, ((ds1743_read(sc,DS_CENTURY) & DS_CTL_RW)
    246  1.1    scw 				      | BCD(rtc->rtc_cen)));
    247  1.1    scw 
    248  1.1    scw 	ds1743_unlock(sc,key);
    249  1.1    scw 	dsrtc_read(arg,rtc);
    250  1.1    scw 	return(1);
    251  1.1    scw }
    252  1.1    scw 
    253  1.1    scw static int
    254  1.1    scw dsrtc_read(void *arg, rtc_t *rtc)
    255  1.1    scw {
    256  1.1    scw 	struct dsrtc_softc *sc = arg;
    257  1.1    scw 	u_char key;
    258  1.1    scw 
    259  1.1    scw 	key = ds1743_lock(sc,DS_CTL_R);
    260  1.1    scw 	rtc->rtc_micro = 0;
    261  1.1    scw 	rtc->rtc_centi = 0;
    262  1.1    scw 	unBCD(rtc->rtc_sec,ds1743_read(sc, DS_SECONDS) & 0x7f);
    263  1.1    scw 	unBCD(rtc->rtc_min,ds1743_read(sc, DS_MINUTES) & 0x7f);
    264  1.1    scw 	unBCD(rtc->rtc_hour, ds1743_read(sc, DS_HOURS) & 0x3f);
    265  1.1    scw 	unBCD(rtc->rtc_day, ds1743_read(sc, DS_DATE)   & 0x3f);
    266  1.1    scw 	unBCD(rtc->rtc_mon,ds1743_read(sc, DS_MONTH)   & 0x1f);
    267  1.1    scw 	unBCD(rtc->rtc_year, ds1743_read(sc, DS_YEAR));
    268  1.1    scw 	unBCD(rtc->rtc_cen, ds1743_read(sc, DS_CENTURY) & ~DS_CTL_RW);
    269  1.1    scw 
    270  1.1    scw 	ds1743_unlock(sc,key);
    271  1.1    scw 	return(1);
    272  1.1    scw }
    273