xref: /netbsd-src/sys/arch/mipsco/obio/mkclock.c (revision b59f66e17c500a0d8ac8904c4c2fe7f387334983)
1 /*	$NetBSD: mkclock.c,v 1.12 2014/11/20 16:34:25 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Wayne Knowles
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.12 2014/11/20 16:34:25 christos Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/device.h>
38 #include <sys/systm.h>
39 
40 #include <dev/clock_subr.h>
41 
42 #include <machine/cpu.h>
43 #include <machine/mainboard.h>
44 #include <machine/autoconf.h>
45 #include <machine/sysconf.h>
46 #include <machine/bus.h>
47 
48 #include <mipsco/obio/clockreg.h>
49 
50 struct	mkclock_softc {
51 	bus_space_tag_t	sc_bst;
52 	bus_space_handle_t sc_bsh;
53 	struct todr_chip_handle sc_todr;
54 };
55 
56 static int mkclock_match (device_t, cfdata_t, void *);
57 static void mkclock_attach (device_t, device_t, void *);
58 
59 CFATTACH_DECL_NEW(mkclock, sizeof(struct mkclock_softc),
60     mkclock_match, mkclock_attach, NULL, NULL);
61 
62 int mkclock_read (todr_chip_handle_t, struct clock_ymdhms *);
63 int mkclock_write (todr_chip_handle_t, struct clock_ymdhms *);
64 
65 static int mk_read (struct mkclock_softc *, int);
66 static void mk_write (struct mkclock_softc *, int, int);
67 
68 int
mkclock_match(device_t parent,cfdata_t cf,void * aux)69 mkclock_match(device_t parent, cfdata_t cf, void *aux)
70 {
71 	return 1;
72 }
73 
74 void
mkclock_attach(device_t parent,device_t self,void * aux)75 mkclock_attach(device_t parent, device_t self, void *aux)
76 {
77         struct mkclock_softc *sc = device_private(self);
78 	struct confargs *ca = aux;
79 
80 	sc->sc_bst = ca->ca_bustag;
81 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
82 			  0x10000,
83 			  BUS_SPACE_MAP_LINEAR,
84 			  &sc->sc_bsh) != 0) {
85 		printf(": cannot map registers\n");
86 		return;
87 	}
88 
89 	sc->sc_todr.todr_settime_ymdhms = mkclock_write;
90 	sc->sc_todr.todr_gettime_ymdhms = mkclock_read;
91 	sc->sc_todr.cookie = sc;
92 	todr_attach(&sc->sc_todr);
93 
94 	printf("\n");
95 }
96 
97 static int
mk_read(struct mkclock_softc * sc,int reg)98 mk_read(struct mkclock_softc *sc, int reg)
99 {
100 	u_int8_t val;
101 
102 	val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
103 	return bcdtobin(val);
104 }
105 
106 static void
mk_write(struct mkclock_softc * sc,int reg,int val)107 mk_write(struct mkclock_softc *sc, int reg, int val)
108 {
109 	bus_space_write_1(sc->sc_bst, sc->sc_bsh,
110 			  DATA_PORT + reg*4, bintobcd(val));
111 }
112 
113 int
mkclock_read(todr_chip_handle_t tch,struct clock_ymdhms * dt)114 mkclock_read(todr_chip_handle_t tch, struct clock_ymdhms *dt)
115 {
116 	struct mkclock_softc *sc = tch->cookie;
117 	int s = splclock();
118 
119 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
120 	dt->dt_sec  = mk_read(sc, 0);
121 	dt->dt_min  = mk_read(sc, 1);
122 	dt->dt_hour = mk_read(sc, 2);
123 	dt->dt_wday = mk_read(sc, 3);
124 	dt->dt_day  = mk_read(sc, 4);
125 	dt->dt_mon  = mk_read(sc, 5);
126 	dt->dt_year = mk_read(sc, 6);
127 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
128 	splx(s);
129 
130 	dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
131 	return 0;
132 }
133 
134 int
mkclock_write(todr_chip_handle_t tch,struct clock_ymdhms * dt)135 mkclock_write(todr_chip_handle_t tch, struct clock_ymdhms *dt)
136 {
137 	struct mkclock_softc *sc = tch->cookie;
138 	int year, s;
139 
140 	year = dt->dt_year % 100;
141 
142 	s = splclock();
143 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
144 	mk_write(sc, 0, dt->dt_sec);
145 	mk_write(sc, 1, dt->dt_min);
146 	mk_write(sc, 2, dt->dt_hour);
147 	/* week day not set */
148 	mk_write(sc, 4, dt->dt_day);
149 	mk_write(sc, 5, dt->dt_mon);
150 	mk_write(sc, 6, year);
151 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
152 	splx(s);
153 	return 0;
154 }
155