Home | History | Annotate | Line # | Download | only in dev
ds1743.c revision 1.1
      1  1.1  scw /*	$NetBSD: ds1743.c,v 1.1 2002/12/09 12:16:23 scw 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.1  scw 
     40  1.1  scw #include <sys/param.h>
     41  1.1  scw #include <sys/systm.h>
     42  1.1  scw #include <sys/device.h>
     43  1.1  scw 
     44  1.1  scw #include <machine/rtc.h>
     45  1.1  scw #include <machine/bus.h>
     46  1.1  scw 
     47  1.1  scw #include <evbppc/walnut/dev/ds1743reg.h>
     48  1.1  scw #include <evbppc/walnut/dev/todclockvar.h>
     49  1.1  scw #include <evbppc/walnut/dev/pbusvar.h>
     50  1.1  scw 
     51  1.1  scw struct dsrtc_softc {
     52  1.1  scw 	struct device	sc_dev;
     53  1.1  scw 	bus_space_tag_t	sc_iot;
     54  1.1  scw 	bus_space_handle_t sc_ioh;
     55  1.1  scw };
     56  1.1  scw 
     57  1.1  scw static void dsrtcattach(struct device *, struct device *, void *);
     58  1.1  scw static int dsrtcmatch(struct device *, struct cfdata *, void *);
     59  1.1  scw #if 0	/* Nothing uses these yet */
     60  1.1  scw static int ds1743_ram_read(struct dsrtc_softc *, int);
     61  1.1  scw static void ds1743_ram_write(struct dsrtc_softc *, int, int);
     62  1.1  scw #endif
     63  1.1  scw 
     64  1.1  scw static int dsrtc_read(void *, rtc_t *);
     65  1.1  scw static int dsrtc_write(void *, rtc_t *);
     66  1.1  scw static inline u_char ds1743_read(struct dsrtc_softc *, int);
     67  1.1  scw static inline void ds1743_write(struct dsrtc_softc *, int, u_char);
     68  1.1  scw static u_char ds1743_lock(struct dsrtc_softc *, u_char);
     69  1.1  scw static void ds1743_unlock(struct dsrtc_softc *, u_char);
     70  1.1  scw 
     71  1.1  scw /* device and attach structures */
     72  1.1  scw CFATTACH_DECL(dsrtc, sizeof(struct dsrtc_softc),
     73  1.1  scw     dsrtcmatch, dsrtcattach, NULL, NULL);
     74  1.1  scw 
     75  1.1  scw /*
     76  1.1  scw  * dsrtcmatch()
     77  1.1  scw  *
     78  1.1  scw  * Validate the IIC address to make sure its an RTC we understand
     79  1.1  scw  */
     80  1.1  scw int ds1743found = 0;
     81  1.1  scw 
     82  1.1  scw #define DS_SCRATCH_ADDR 0x1FF7
     83  1.1  scw 
     84  1.1  scw static int
     85  1.1  scw dsrtcmatch(struct device *parent, struct cfdata *cf, void *aux)
     86  1.1  scw {
     87  1.1  scw 	struct pbus_attach_args *paa = aux;
     88  1.1  scw 	int retval = !ds1743found;
     89  1.1  scw 	bus_space_handle_t h;
     90  1.1  scw 	u_int8_t x;
     91  1.1  scw 
     92  1.1  scw 	/* match only RTC devices */
     93  1.1  scw 	if (strcmp(paa->pb_name, cf->cf_name) != 0)
     94  1.1  scw 		return 0;
     95  1.1  scw 
     96  1.1  scw 	if (bus_space_map(0, paa->pb_addr, DS_SIZE, 0, &h)) {
     97  1.1  scw 		printf("%s: can't map i/o space\n", paa->pb_name);
     98  1.1  scw 		return 0;
     99  1.1  scw 	}
    100  1.1  scw 
    101  1.1  scw 	/* Read one byte of what's supposed to be NVRAM */
    102  1.1  scw 	x = bus_space_read_1(0, h, DS_SCRATCH_ADDR);
    103  1.1  scw 	bus_space_write_1(0, h, DS_SCRATCH_ADDR, 0xAA);
    104  1.1  scw 	if (bus_space_read_1(0, h, DS_SCRATCH_ADDR) != 0xAA) {
    105  1.1  scw 		retval = 0;
    106  1.1  scw 		goto done;
    107  1.1  scw 	}
    108  1.1  scw 
    109  1.1  scw 	bus_space_write_1(0, h, DS_SCRATCH_ADDR, 0x55);
    110  1.1  scw 	if (bus_space_read_1(0, h, DS_SCRATCH_ADDR) != 0x55) {
    111  1.1  scw 		retval = 0;
    112  1.1  scw 		goto done;
    113  1.1  scw 	}
    114  1.1  scw 
    115  1.1  scw 	/* Restore scratch byte value */
    116  1.1  scw 	bus_space_write_1(0, h, DS_SCRATCH_ADDR, x);
    117  1.1  scw   done:
    118  1.1  scw 	bus_space_unmap(0, h, DS_SIZE);
    119  1.1  scw 
    120  1.1  scw 	return retval;
    121  1.1  scw }
    122  1.1  scw 
    123  1.1  scw /*
    124  1.1  scw  * dsrtcattach()
    125  1.1  scw  *
    126  1.1  scw  * Attach the rtc device
    127  1.1  scw  */
    128  1.1  scw 
    129  1.1  scw static void
    130  1.1  scw dsrtcattach(struct device *parent, struct device *self, void *aux)
    131  1.1  scw {
    132  1.1  scw 	struct dsrtc_softc *sc = (struct dsrtc_softc *)self;
    133  1.1  scw 	struct pbus_attach_args *paa = aux;
    134  1.1  scw 	struct todclock_attach_args ta;
    135  1.1  scw 
    136  1.1  scw 	ds1743found = 1;
    137  1.1  scw 
    138  1.1  scw 	sc->sc_iot = 0;
    139  1.1  scw 	sc->sc_ioh = paa->pb_addr;
    140  1.1  scw 	if (bus_space_map(sc->sc_iot, paa->pb_addr, DS_SIZE, 0, &sc->sc_ioh)) {
    141  1.1  scw 		printf(": can't map i/o space\n");
    142  1.1  scw 		return;
    143  1.1  scw 	}
    144  1.1  scw 
    145  1.1  scw 	ds1743_unlock(sc,0);	/* Make sure the clock is running */
    146  1.1  scw 	if ((ds1743_read(sc, DS_DAY) & DS_CTL_BF) == 0)
    147  1.1  scw 		printf(": lithium cell is dead, RTC unreliable");
    148  1.1  scw 	printf("\n");
    149  1.1  scw 
    150  1.1  scw #ifdef DEBUG
    151  1.1  scw 	{
    152  1.1  scw 		rtc_t rtc;
    153  1.1  scw 		dsrtc_read(sc,&rtc);
    154  1.1  scw 		printf("RTC: %d/%d/%02d%02d %d:%02d:%02d\n",
    155  1.1  scw 			rtc.rtc_mon, rtc.rtc_day, rtc.rtc_cen, rtc.rtc_year,
    156  1.1  scw 			rtc.rtc_hour, rtc.rtc_min, rtc.rtc_sec);
    157  1.1  scw 	}
    158  1.1  scw #endif
    159  1.1  scw 
    160  1.1  scw 
    161  1.1  scw 	ta.ta_name = "todclock";
    162  1.1  scw 	ta.ta_rtc_arg = sc;
    163  1.1  scw 	ta.ta_rtc_write = dsrtc_write;
    164  1.1  scw 	ta.ta_rtc_read = dsrtc_read;
    165  1.1  scw 	ta.ta_flags = 0;
    166  1.1  scw 	config_found(self, &ta, NULL);
    167  1.1  scw }
    168  1.1  scw 
    169  1.1  scw static inline u_char
    170  1.1  scw ds1743_read(struct dsrtc_softc *sc, int addr)
    171  1.1  scw {
    172  1.1  scw 
    173  1.1  scw 	return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, addr));
    174  1.1  scw }
    175  1.1  scw 
    176  1.1  scw static inline void
    177  1.1  scw ds1743_write(struct dsrtc_softc *sc, int addr, u_char data)
    178  1.1  scw {
    179  1.1  scw 
    180  1.1  scw 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, addr, data);
    181  1.1  scw }
    182  1.1  scw 
    183  1.1  scw 
    184  1.1  scw #if 0	/* Nothing uses these yet */
    185  1.1  scw static u_char
    186  1.1  scw ds1743_ram_read(struct dsrtc_softc *sc, int addr)
    187  1.1  scw {
    188  1.1  scw 
    189  1.1  scw 	if (addr >= DS_RAM_SIZE)
    190  1.1  scw 		return(-1);
    191  1.1  scw 	return(ds1743_read(sc, addr));
    192  1.1  scw }
    193  1.1  scw 
    194  1.1  scw static void
    195  1.1  scw ds1743_ram_write(struct dsrtc_softc *sc, int addr, u_char val)
    196  1.1  scw {
    197  1.1  scw 
    198  1.1  scw 	if (addr >= DS_RAM_SIZE)
    199  1.1  scw 		return (-1);
    200  1.1  scw 	ds1743_write(sc,addr,val);
    201  1.1  scw }
    202  1.1  scw #endif
    203  1.1  scw 
    204  1.1  scw #define BCD(x)	((((x)/10)<<4)|(x%10))
    205  1.1  scw #define unBCD(v,x)	v=x; v = ((v>>4) & 0xf)*10+(v & 0xf)
    206  1.1  scw 
    207  1.1  scw static u_char
    208  1.1  scw ds1743_lock(struct dsrtc_softc *sc, u_char mode)
    209  1.1  scw {
    210  1.1  scw 	u_char octl,ctl;
    211  1.1  scw 
    212  1.1  scw 	octl = ds1743_read(sc,DS_CENTURY);
    213  1.1  scw 	ctl = octl | (mode & DS_CTL_RW);
    214  1.1  scw 	ds1743_write(sc,DS_CENTURY, ctl);	/* Lock RTC for both reading and writing */
    215  1.1  scw 	return octl;
    216  1.1  scw }
    217  1.1  scw 
    218  1.1  scw static void
    219  1.1  scw ds1743_unlock(struct dsrtc_softc *sc, u_char key)
    220  1.1  scw {
    221  1.1  scw 	int ctl;
    222  1.1  scw 
    223  1.1  scw 	ctl = ds1743_read(sc,DS_CENTURY);
    224  1.1  scw 	ctl = (ctl & 0x3f) | (key & DS_CTL_RW);
    225  1.1  scw 	ds1743_write(sc,DS_CENTURY, ctl);	/* Enable updates */
    226  1.1  scw }
    227  1.1  scw 
    228  1.1  scw static int
    229  1.1  scw dsrtc_write(void * arg, rtc_t * rtc)
    230  1.1  scw {
    231  1.1  scw 	struct dsrtc_softc *sc = arg;
    232  1.1  scw 	u_char key;
    233  1.1  scw 
    234  1.1  scw 	key = ds1743_lock(sc,DS_CTL_W);
    235  1.1  scw 
    236  1.1  scw 	ds1743_write(sc, DS_SECONDS, BCD(rtc->rtc_sec) & 0x7f);
    237  1.1  scw 	ds1743_write(sc, DS_MINUTES, BCD(rtc->rtc_min) & 0x7f);
    238  1.1  scw 	ds1743_write(sc, DS_HOURS, BCD(rtc->rtc_hour)  & 0x3f);
    239  1.1  scw 	ds1743_write(sc, DS_DATE, BCD(rtc->rtc_day)    & 0x3f);
    240  1.1  scw 	ds1743_write(sc, DS_MONTH, BCD(rtc->rtc_mon)   & 0x1f);
    241  1.1  scw 	ds1743_write(sc, DS_YEAR, BCD(rtc->rtc_year));
    242  1.1  scw 	ds1743_write(sc, DS_CENTURY, ((ds1743_read(sc,DS_CENTURY) & DS_CTL_RW)
    243  1.1  scw 				      | BCD(rtc->rtc_cen)));
    244  1.1  scw 
    245  1.1  scw 	ds1743_unlock(sc,key);
    246  1.1  scw 	dsrtc_read(arg,rtc);
    247  1.1  scw 	return(1);
    248  1.1  scw }
    249  1.1  scw 
    250  1.1  scw static int
    251  1.1  scw dsrtc_read(void *arg, rtc_t *rtc)
    252  1.1  scw {
    253  1.1  scw 	struct dsrtc_softc *sc = arg;
    254  1.1  scw 	u_char key;
    255  1.1  scw 
    256  1.1  scw 	key = ds1743_lock(sc,DS_CTL_R);
    257  1.1  scw 	rtc->rtc_micro = 0;
    258  1.1  scw 	rtc->rtc_centi = 0;
    259  1.1  scw 	unBCD(rtc->rtc_sec,ds1743_read(sc, DS_SECONDS) & 0x7f);
    260  1.1  scw 	unBCD(rtc->rtc_min,ds1743_read(sc, DS_MINUTES) & 0x7f);
    261  1.1  scw 	unBCD(rtc->rtc_hour, ds1743_read(sc, DS_HOURS) & 0x3f);
    262  1.1  scw 	unBCD(rtc->rtc_day, ds1743_read(sc, DS_DATE)   & 0x3f);
    263  1.1  scw 	unBCD(rtc->rtc_mon,ds1743_read(sc, DS_MONTH)   & 0x1f);
    264  1.1  scw 	unBCD(rtc->rtc_year, ds1743_read(sc, DS_YEAR));
    265  1.1  scw 	unBCD(rtc->rtc_cen, ds1743_read(sc, DS_CENTURY) & ~DS_CTL_RW);
    266  1.1  scw 
    267  1.1  scw 	ds1743_unlock(sc,key);
    268  1.1  scw 	return(1);
    269  1.1  scw }
    270