Home | History | Annotate | Line # | Download | only in dev
rs5c313_landisk.c revision 1.1.44.1
      1  1.1.44.1  mjf /*	$NetBSD: rs5c313_landisk.c,v 1.1.44.1 2007/11/19 00:46:34 mjf Exp $	*/
      2       1.1  uwe 
      3       1.1  uwe /*-
      4       1.1  uwe  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5       1.1  uwe  * All rights reserved.
      6       1.1  uwe  *
      7       1.1  uwe  * Redistribution and use in source and binary forms, with or without
      8       1.1  uwe  * modification, are permitted provided that the following conditions
      9       1.1  uwe  * are met:
     10       1.1  uwe  * 1. Redistributions of source code must retain the above copyright
     11       1.1  uwe  *    notice, this list of conditions and the following disclaimer.
     12       1.1  uwe  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  uwe  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  uwe  *    documentation and/or other materials provided with the distribution.
     15       1.1  uwe  * 3. All advertising materials mentioning features or use of this software
     16       1.1  uwe  *    must display the following acknowledgement:
     17       1.1  uwe  *        This product includes software developed by the NetBSD
     18       1.1  uwe  *        Foundation, Inc. and its contributors.
     19       1.1  uwe  *
     20       1.1  uwe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1  uwe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1  uwe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1  uwe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1  uwe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1  uwe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1  uwe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1  uwe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1  uwe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1  uwe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1  uwe  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1  uwe  */
     32       1.1  uwe 
     33       1.1  uwe #include <sys/cdefs.h>
     34  1.1.44.1  mjf __KERNEL_RCSID(0, "$NetBSD: rs5c313_landisk.c,v 1.1.44.1 2007/11/19 00:46:34 mjf Exp $");
     35       1.1  uwe 
     36       1.1  uwe #include <sys/param.h>
     37       1.1  uwe #include <sys/systm.h>
     38       1.1  uwe #include <sys/device.h>
     39       1.1  uwe #include <sys/kernel.h>
     40       1.1  uwe 
     41       1.1  uwe #include <dev/clock_subr.h>
     42       1.1  uwe #include <dev/ic/rs5c313var.h>
     43       1.1  uwe 
     44       1.1  uwe #include <sh3/devreg.h>
     45       1.1  uwe #include <sh3/scireg.h>
     46       1.1  uwe 
     47       1.1  uwe #include <landisk/landisk/landiskreg.h>
     48       1.1  uwe 
     49       1.1  uwe 
     50       1.1  uwe /* autoconf glue */
     51  1.1.44.1  mjf static int rs5c313_landisk_match(device_t, struct cfdata *, void *);
     52  1.1.44.1  mjf static void rs5c313_landisk_attach(device_t, device_t, void *);
     53       1.1  uwe 
     54       1.1  uwe CFATTACH_DECL(rs5c313_landisk, sizeof(struct rs5c313_softc),
     55       1.1  uwe     rs5c313_landisk_match, rs5c313_landisk_attach, NULL, NULL);
     56       1.1  uwe 
     57       1.1  uwe 
     58       1.1  uwe /* chip access methods */
     59       1.1  uwe static void rtc_begin(struct rs5c313_softc *);
     60       1.1  uwe static void rtc_ce(struct rs5c313_softc *, int);
     61       1.1  uwe static void rtc_dir(struct rs5c313_softc *, int);
     62       1.1  uwe static void rtc_clk(struct rs5c313_softc *, int);
     63       1.1  uwe static int  rtc_read(struct rs5c313_softc *);
     64       1.1  uwe static void rtc_write(struct rs5c313_softc *, int);
     65       1.1  uwe 
     66  1.1.44.1  mjf static struct rs5c313_ops rs5c313_landisk_ops = {
     67       1.1  uwe 	.rs5c313_op_begin = rtc_begin,
     68       1.1  uwe 	.rs5c313_op_ce    = rtc_ce,
     69       1.1  uwe 	.rs5c313_op_clk   = rtc_clk,
     70       1.1  uwe 	.rs5c313_op_dir   = rtc_dir,
     71       1.1  uwe 	.rs5c313_op_read  = rtc_read,
     72       1.1  uwe 	.rs5c313_op_write = rtc_write,
     73       1.1  uwe };
     74       1.1  uwe 
     75       1.1  uwe #define ndelay(x) delay(x)
     76       1.1  uwe 
     77       1.1  uwe 
     78       1.1  uwe 
     79       1.1  uwe static int
     80  1.1.44.1  mjf rs5c313_landisk_match(device_t parent, struct cfdata *cf, void *aux)
     81       1.1  uwe {
     82       1.1  uwe 	static int matched = 0;
     83       1.1  uwe 
     84       1.1  uwe 	if (matched)
     85       1.1  uwe 		return 0;
     86       1.1  uwe 
     87       1.1  uwe 	matched = 1;
     88       1.1  uwe 	return 1;
     89       1.1  uwe }
     90       1.1  uwe 
     91       1.1  uwe 
     92       1.1  uwe static void
     93  1.1.44.1  mjf rs5c313_landisk_attach(device_t parent, device_t self, void *aux)
     94       1.1  uwe {
     95  1.1.44.1  mjf 	struct rs5c313_softc *sc = device_private(self);
     96       1.1  uwe 
     97       1.1  uwe 	sc->sc_ops = &rs5c313_landisk_ops;
     98       1.1  uwe 	rs5c313_attach(sc);
     99       1.1  uwe }
    100       1.1  uwe 
    101       1.1  uwe 
    102       1.1  uwe static void
    103       1.1  uwe rtc_begin(struct rs5c313_softc *sc)
    104       1.1  uwe {
    105       1.1  uwe 
    106       1.1  uwe 	SHREG_SCSPTR = SCSPTR_SPB1IO | SCSPTR_SPB1DT
    107       1.1  uwe 		     | SCSPTR_SPB0IO | SCSPTR_SPB0DT;
    108       1.1  uwe 	ndelay(100);
    109       1.1  uwe }
    110       1.1  uwe 
    111       1.1  uwe 
    112       1.1  uwe /*
    113       1.1  uwe  * CE pin
    114       1.1  uwe  */
    115       1.1  uwe static void
    116       1.1  uwe rtc_ce(struct rs5c313_softc *sc, int onoff)
    117       1.1  uwe {
    118       1.1  uwe 
    119       1.1  uwe 	if (onoff)
    120       1.1  uwe 		_reg_write_1(LANDISK_PWRMNG, PWRMNG_RTC_CE);
    121       1.1  uwe 	else
    122       1.1  uwe 		_reg_write_1(LANDISK_PWRMNG, 0);
    123       1.1  uwe 	ndelay(600);
    124       1.1  uwe }
    125       1.1  uwe 
    126       1.1  uwe 
    127       1.1  uwe /*
    128       1.1  uwe  * SCLK pin is connnected to SPB0DT.
    129       1.1  uwe  * SPB0DT is always in output mode, we set SPB0IO in rtc_begin.
    130       1.1  uwe  */
    131       1.1  uwe static void
    132       1.1  uwe rtc_clk(struct rs5c313_softc *sc, int onoff)
    133       1.1  uwe {
    134       1.1  uwe 	uint8_t r = SHREG_SCSPTR;
    135       1.1  uwe 
    136       1.1  uwe 	if (onoff)
    137       1.1  uwe 		r |= SCSPTR_SPB0DT;
    138       1.1  uwe 	else
    139       1.1  uwe 		r &= ~SCSPTR_SPB0DT;
    140       1.1  uwe 	SHREG_SCSPTR = r;
    141       1.1  uwe }
    142       1.1  uwe 
    143       1.1  uwe 
    144       1.1  uwe /*
    145       1.1  uwe  * SIO pin is connected to SPB1DT.
    146       1.1  uwe  * SPB1DT is output when SPB1IO is set.
    147       1.1  uwe  */
    148       1.1  uwe static void
    149       1.1  uwe rtc_dir(struct rs5c313_softc *sc, int output)
    150       1.1  uwe {
    151       1.1  uwe 	uint8_t r = SHREG_SCSPTR;
    152       1.1  uwe 
    153       1.1  uwe 	if (output)
    154       1.1  uwe 		r |= SCSPTR_SPB1IO;
    155       1.1  uwe 	else
    156       1.1  uwe 		r &= ~SCSPTR_SPB1IO;
    157       1.1  uwe 	SHREG_SCSPTR = r;
    158       1.1  uwe }
    159       1.1  uwe 
    160       1.1  uwe 
    161       1.1  uwe /*
    162       1.1  uwe  * Read bit from SPB1DT pin.
    163       1.1  uwe  */
    164       1.1  uwe static int
    165       1.1  uwe rtc_read(struct rs5c313_softc *sc)
    166       1.1  uwe {
    167       1.1  uwe 	int bit;
    168       1.1  uwe 
    169       1.1  uwe 	ndelay(300);
    170       1.1  uwe 
    171       1.1  uwe 	bit = (SHREG_SCSPTR & SCSPTR_SPB1DT) ? 1 : 0;
    172       1.1  uwe 
    173       1.1  uwe 	rtc_clk(sc, 0);
    174       1.1  uwe 	ndelay(300);
    175       1.1  uwe 	rtc_clk(sc, 1);
    176       1.1  uwe 
    177       1.1  uwe 	return bit;
    178       1.1  uwe }
    179       1.1  uwe 
    180       1.1  uwe 
    181       1.1  uwe /*
    182       1.1  uwe  * Write bit via SPB1DT pin.
    183       1.1  uwe  */
    184       1.1  uwe static void
    185       1.1  uwe rtc_write(struct rs5c313_softc *sc, int bit)
    186       1.1  uwe {
    187       1.1  uwe 	uint8_t r = SHREG_SCSPTR;
    188       1.1  uwe 
    189       1.1  uwe 	if (bit)
    190       1.1  uwe 		r |= SCSPTR_SPB1DT;
    191       1.1  uwe 	else
    192       1.1  uwe 		r &= ~SCSPTR_SPB1DT;
    193       1.1  uwe 	SHREG_SCSPTR = r;
    194       1.1  uwe 
    195       1.1  uwe 	ndelay(300);
    196       1.1  uwe 
    197       1.1  uwe 	rtc_clk(sc, 0);
    198       1.1  uwe 	ndelay(300);
    199       1.1  uwe 	rtc_clk(sc, 1);
    200       1.1  uwe }
    201