Home | History | Annotate | Line # | Download | only in isa
dsrtc.c revision 1.13
      1  1.13     skrll /*	$NetBSD: dsrtc.c,v 1.13 2021/08/13 11:40:43 skrll Exp $	*/
      2   1.1     chris 
      3   1.1     chris /*
      4   1.1     chris  * Copyright (c) 1998 Mark Brinicombe.
      5   1.1     chris  * Copyright (c) 1998 Causality Limited.
      6   1.1     chris  * All rights reserved.
      7   1.1     chris  *
      8   1.1     chris  * Written by Mark Brinicombe, Causality Limited
      9   1.1     chris  *
     10   1.1     chris  * Redistribution and use in source and binary forms, with or without
     11   1.1     chris  * modification, are permitted provided that the following conditions
     12   1.1     chris  * are met:
     13   1.1     chris  * 1. Redistributions of source code must retain the above copyright
     14   1.1     chris  *    notice, this list of conditions and the following disclaimer.
     15   1.1     chris  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1     chris  *    notice, this list of conditions and the following disclaimer in the
     17   1.1     chris  *    documentation and/or other materials provided with the distribution.
     18   1.1     chris  * 3. All advertising materials mentioning features or use of this software
     19   1.1     chris  *    must display the following acknowledgement:
     20   1.1     chris  *	This product includes software developed by Mark Brinicombe
     21   1.1     chris  *	for the NetBSD Project.
     22   1.1     chris  * 4. The name of the company nor the name of the author may be used to
     23   1.1     chris  *    endorse or promote products derived from this software without specific
     24   1.1     chris  *    prior written permission.
     25   1.1     chris  *
     26   1.1     chris  * THIS SOFTWARE IS PROVIDED BY CAUASLITY LIMITED ``AS IS'' AND ANY EXPRESS
     27   1.1     chris  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     28   1.1     chris  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     29   1.1     chris  * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED OR CONTRIBUTORS BE LIABLE
     30   1.1     chris  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1     chris  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     32   1.1     chris  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1     chris  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1     chris  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1     chris  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1     chris  * SUCH DAMAGE.
     37   1.1     chris  */
     38   1.5     chris 
     39   1.5     chris #include <sys/cdefs.h>
     40  1.13     skrll __KERNEL_RCSID(0, "$NetBSD: dsrtc.c,v 1.13 2021/08/13 11:40:43 skrll Exp $");
     41   1.1     chris 
     42   1.1     chris #include <sys/param.h>
     43   1.1     chris #include <sys/systm.h>
     44   1.1     chris #include <sys/kernel.h>
     45   1.1     chris #include <sys/conf.h>
     46   1.1     chris #include <sys/device.h>
     47   1.1     chris 
     48  1.10  christos #include <dev/clock_subr.h>
     49   1.1     chris #include <arm/footbridge/isa/ds1687reg.h>
     50   1.1     chris 
     51   1.1     chris #include <dev/isa/isavar.h>
     52   1.1     chris 
     53   1.1     chris #define NRTC_PORTS	2
     54   1.1     chris 
     55   1.1     chris struct dsrtc_softc {
     56   1.1     chris 	bus_space_tag_t	sc_iot;
     57   1.1     chris 	bus_space_handle_t sc_ioh;
     58  1.10  christos 	struct todr_chip_handle sc_todr;
     59   1.1     chris };
     60   1.1     chris 
     61  1.12     skrll void dsrtcattach(device_t parent, device_t self, void *aux);
     62  1.12     skrll int dsrtcmatch(device_t parent, cfdata_t cf, void *aux);
     63   1.9   gdamore int ds1687_read(struct dsrtc_softc *sc, int addr);
     64   1.9   gdamore void ds1687_write(struct dsrtc_softc *sc, int addr, int data);
     65   1.9   gdamore #if 0
     66   1.9   gdamore int ds1687_ram_read(struct dsrtc_softc *sc, int addr);
     67   1.9   gdamore void ds1687_ram_write(struct dsrtc_softc *sc, int addr, int data);
     68   1.9   gdamore #endif
     69   1.9   gdamore static void ds1687_bank_select(struct dsrtc_softc *, int);
     70  1.10  christos static int dsrtc_write(todr_chip_handle_t, struct clock_ymdhms *);
     71  1.10  christos static int dsrtc_read(todr_chip_handle_t, struct clock_ymdhms *);
     72   1.1     chris 
     73   1.1     chris int
     74   1.9   gdamore ds1687_read(struct dsrtc_softc *sc, int addr)
     75   1.1     chris {
     76   1.1     chris 
     77   1.1     chris 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
     78   1.1     chris 	return(bus_space_read_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG));
     79   1.1     chris }
     80   1.1     chris 
     81   1.1     chris void
     82   1.9   gdamore ds1687_write(struct dsrtc_softc *sc, int addr, int data)
     83   1.1     chris {
     84   1.1     chris 
     85   1.1     chris 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_ADDR_REG, addr);
     86   1.1     chris 	bus_space_write_1(sc->sc_iot, sc->sc_ioh, RTC_DATA_REG, data);
     87   1.1     chris }
     88   1.1     chris 
     89   1.1     chris static void
     90   1.9   gdamore ds1687_bank_select(struct dsrtc_softc *sc, int bank)
     91   1.1     chris {
     92   1.1     chris 	int data;
     93   1.1     chris 
     94   1.1     chris 	data = ds1687_read(sc, RTC_REG_A);
     95   1.1     chris 	data &= ~RTC_REG_A_BANK_MASK;
     96   1.1     chris 	if (bank)
     97   1.1     chris 		data |= RTC_REG_A_BANK1;
     98   1.1     chris 	ds1687_write(sc, RTC_REG_A, data);
     99   1.1     chris }
    100   1.1     chris 
    101   1.1     chris #if 0
    102   1.1     chris /* Nothing uses these yet */
    103   1.1     chris int
    104   1.9   gdamore ds1687_ram_read(struct dsrtc_softc *sc, int addr)
    105   1.1     chris {
    106   1.1     chris 	if (addr < RTC_PC_RAM_SIZE)
    107   1.1     chris 		return(ds1687_read(sc, RTC_PC_RAM_START + addr));
    108   1.1     chris 
    109   1.1     chris 	addr -= RTC_PC_RAM_SIZE;
    110   1.1     chris 	if (addr < RTC_BANK0_RAM_SIZE)
    111  1.13     skrll 		return(ds1687_read(sc, RTC_BANK0_RAM_START + addr));
    112   1.1     chris 
    113   1.1     chris 	addr -= RTC_BANK0_RAM_SIZE;
    114   1.1     chris 	if (addr < RTC_EXT_RAM_SIZE) {
    115   1.1     chris 		int data;
    116   1.1     chris 
    117   1.1     chris 		ds1687_bank_select(sc, 1);
    118   1.1     chris 		ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
    119   1.1     chris 		data = ds1687_read(sc, RTC_EXT_RAM_DATA);
    120   1.1     chris 		ds1687_bank_select(sc, 0);
    121   1.1     chris 		return(data);
    122   1.1     chris 	}
    123   1.1     chris 	return(-1);
    124   1.1     chris }
    125   1.1     chris 
    126   1.1     chris void
    127   1.9   gdamore ds1687_ram_write(struct dsrtc_softc *sc, int addr, int val)
    128   1.1     chris {
    129   1.1     chris 	if (addr < RTC_PC_RAM_SIZE)
    130   1.1     chris 		return(ds1687_write(sc, RTC_PC_RAM_START + addr, val));
    131   1.1     chris 
    132   1.1     chris 	addr -= RTC_PC_RAM_SIZE;
    133   1.1     chris 	if (addr < RTC_BANK0_RAM_SIZE)
    134   1.1     chris 		return(ds1687_write(sc, RTC_BANK0_RAM_START + addr, val));
    135   1.1     chris 
    136   1.1     chris 	addr -= RTC_BANK0_RAM_SIZE;
    137   1.1     chris 	if (addr < RTC_EXT_RAM_SIZE) {
    138   1.1     chris 		ds1687_bank_select(sc, 1);
    139   1.1     chris 		ds1687_write(sc, RTC_EXT_RAM_ADDRESS, addr);
    140   1.1     chris 		ds1687_write(sc, RTC_EXT_RAM_DATA, val);
    141   1.1     chris 		ds1687_bank_select(sc, 0);
    142   1.1     chris 	}
    143   1.1     chris }
    144   1.1     chris #endif
    145   1.1     chris 
    146   1.1     chris static int
    147  1.10  christos dsrtc_write(todr_chip_handle_t tc, struct clock_ymdhms *dt)
    148   1.1     chris {
    149  1.10  christos 	struct dsrtc_softc *sc = tc->cookie;
    150   1.1     chris 
    151  1.10  christos 	ds1687_write(sc, RTC_SECONDS, dt->dt_sec);
    152  1.10  christos 	ds1687_write(sc, RTC_MINUTES, dt->dt_min);
    153  1.10  christos 	ds1687_write(sc, RTC_HOURS, dt->dt_hour);
    154  1.10  christos 	ds1687_write(sc, RTC_DAYOFMONTH, dt->dt_day);
    155  1.10  christos 	ds1687_write(sc, RTC_MONTH, dt->dt_mon);
    156  1.10  christos 	ds1687_write(sc, RTC_YEAR, dt->dt_year % 100);
    157   1.1     chris 	ds1687_bank_select(sc, 1);
    158  1.10  christos 	ds1687_write(sc, RTC_CENTURY, dt->dt_year / 100);
    159   1.1     chris 	ds1687_bank_select(sc, 0);
    160  1.10  christos 	return(0);
    161   1.1     chris }
    162   1.1     chris 
    163   1.1     chris static int
    164  1.10  christos dsrtc_read(todr_chip_handle_t tc, struct clock_ymdhms *dt)
    165   1.1     chris {
    166  1.10  christos 	struct dsrtc_softc *sc = tc->cookie;
    167   1.1     chris 
    168  1.10  christos 	dt->dt_sec   = ds1687_read(sc, RTC_SECONDS);
    169  1.10  christos 	dt->dt_min   = ds1687_read(sc, RTC_MINUTES);
    170  1.10  christos 	dt->dt_hour  = ds1687_read(sc, RTC_HOURS);
    171  1.10  christos 	dt->dt_day   = ds1687_read(sc, RTC_DAYOFMONTH);
    172  1.10  christos 	dt->dt_mon   = ds1687_read(sc, RTC_MONTH);
    173  1.10  christos 	dt->dt_year  = ds1687_read(sc, RTC_YEAR);
    174   1.1     chris 	ds1687_bank_select(sc, 1);
    175  1.10  christos 	dt->dt_year  += ds1687_read(sc, RTC_CENTURY) * 100;
    176   1.1     chris 	ds1687_bank_select(sc, 0);
    177   1.1     chris 
    178  1.10  christos 	return(0);
    179   1.1     chris }
    180   1.1     chris 
    181   1.1     chris /* device and attach structures */
    182  1.11     skrll CFATTACH_DECL_NEW(ds1687rtc, sizeof(struct dsrtc_softc),
    183   1.4   thorpej     dsrtcmatch, dsrtcattach, NULL, NULL);
    184   1.1     chris 
    185   1.1     chris /*
    186   1.1     chris  * dsrtcmatch()
    187   1.1     chris  *
    188   1.1     chris  * Validate the IIC address to make sure its an RTC we understand
    189   1.1     chris  */
    190   1.1     chris 
    191   1.1     chris int
    192  1.11     skrll dsrtcmatch(device_t parent, cfdata_t cf, void *aux)
    193   1.1     chris {
    194   1.1     chris 	struct isa_attach_args *ia = aux;
    195   1.1     chris 
    196   1.1     chris 	if (ia->ia_nio < 1 ||
    197   1.7  drochner 	    ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
    198   1.1     chris 		return (0);
    199   1.1     chris 
    200   1.1     chris 	ia->ia_nio = 1;
    201   1.1     chris 	ia->ia_io[0].ir_size = NRTC_PORTS;
    202   1.1     chris 
    203   1.1     chris 	ia->ia_niomem = 0;
    204   1.1     chris 	ia->ia_nirq = 0;
    205   1.1     chris 	ia->ia_ndrq = 0;
    206   1.1     chris 
    207   1.1     chris 	return(1);
    208   1.1     chris }
    209   1.1     chris 
    210   1.1     chris /*
    211   1.1     chris  * dsrtcattach()
    212   1.1     chris  *
    213   1.1     chris  * Attach the rtc device
    214   1.1     chris  */
    215   1.1     chris 
    216   1.1     chris void
    217  1.11     skrll dsrtcattach(device_t parent, device_t self, void *aux)
    218   1.1     chris {
    219  1.11     skrll 	struct dsrtc_softc *sc = device_private(self);
    220   1.1     chris 	struct isa_attach_args *ia = aux;
    221  1.13     skrll 
    222   1.1     chris 	sc->sc_iot = ia->ia_iot;
    223   1.1     chris 	if (bus_space_map(sc->sc_iot, ia->ia_io[0].ir_addr,
    224   1.1     chris 	    ia->ia_io[0].ir_size, 0, &sc->sc_ioh)) {
    225  1.11     skrll 		aprint_error(": cannot map I/O space\n");
    226   1.1     chris 		return;
    227   1.1     chris 	}
    228   1.1     chris 
    229   1.1     chris 	ds1687_write(sc, RTC_REG_A, RTC_REG_A_DV1);
    230   1.1     chris 	ds1687_write(sc, RTC_REG_B, RTC_REG_B_BINARY | RTC_REG_B_24_HOUR);
    231   1.1     chris 
    232   1.1     chris 	if (!(ds1687_read(sc, RTC_REG_D) & RTC_REG_D_VRT))
    233  1.11     skrll 		aprint_error(": lithium cell is dead, RTC unreliable");
    234  1.11     skrll 	aprint_normal("\n");
    235   1.1     chris 
    236  1.10  christos 	sc->sc_todr.todr_gettime_ymdhms = dsrtc_read;
    237  1.10  christos 	sc->sc_todr.todr_settime_ymdhms = dsrtc_write;
    238  1.10  christos 	sc->sc_todr.cookie = sc;
    239  1.10  christos 	todr_attach(&sc->sc_todr);
    240   1.1     chris }
    241   1.1     chris 
    242   1.1     chris /* End of dsrtc.c */
    243