summaryrefslogtreecommitdiff
path: root/sys/arch/evbarm/tsarm/tsrtc.c
blob: 1892be6e80a072e4b40e2055b70f0e06b8a9d12f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*	$NetBSD: tsrtc.c,v 1.8 2014/11/20 16:34:25 christos Exp $	*/

/*
 * Copyright (c) 1995, 1996 Carnegie-Mellon University.
 * All rights reserved.
 *
 * Author: Chris G. Demetriou
 *
 * Permission to use, copy, modify and distribute this software and
 * its documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie the
 * rights to redistribute these changes.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.8 2014/11/20 16:34:25 christos Exp $");

#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */

#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/device.h>

#include <sys/bus.h>

#include <dev/clock_subr.h>
#include <dev/ic/mc146818reg.h>
#include <dev/ic/mc146818var.h>

#include <evbarm/tsarm/tspldvar.h>
#include <evbarm/tsarm/tsarmreg.h>

int	tsrtc_match(device_t, cfdata_t, void *);
void	tsrtc_attach(device_t, device_t, void *);

struct tsrtc_softc {
	struct mc146818_softc	sc_mc;
	bus_space_tag_t		sc_iot;
	bus_space_handle_t	sc_dath;
	bus_space_handle_t	sc_idxh;
};

CFATTACH_DECL_NEW(tsrtc, sizeof (struct tsrtc_softc),
    tsrtc_match, tsrtc_attach, NULL, NULL);

void	tsrtc_write(struct mc146818_softc *, u_int, u_int);
u_int	tsrtc_read(struct mc146818_softc *, u_int);


int
tsrtc_match(device_t parent, cfdata_t cf, void *aux)
{
	struct tspld_attach_args *aa = aux;
	struct tsrtc_softc tsrtc, *tsc;
	struct mc146818_softc *sc;
	unsigned int t1, t2;
	static int found = -1;

	if (found != -1)
		return found;

	tsc = &tsrtc;
	sc = &tsc->sc_mc;
	tsc->sc_iot = aa->ta_iot;
	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0,
		 &tsc->sc_idxh))
		return (0);
	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0,
		 &tsc->sc_dath))
		return (0);

	/* Read from the seconds counter. */
	t1 = bcdtobin(tsrtc_read(sc, MC_SEC));
	if (t1 > 59)
		goto unmap;

	/* Wait, then look again. */
	DELAY(1100000);
	t2 = bcdtobin(tsrtc_read(sc, MC_SEC));
	if (t2 > 59)
		goto unmap;

        /* If [1,2) seconds have passed since, call it a clock. */
	if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
		found = 1;

 unmap:
	bus_space_unmap(tsc->sc_iot, tsc->sc_idxh, 1);
	bus_space_unmap(tsc->sc_iot, tsc->sc_dath, 1);

	return (found);
}

void
tsrtc_attach(device_t parent, device_t self, void *aux)
{
	struct tsrtc_softc *tsc = device_private(self);
	struct mc146818_softc *sc = &tsc->sc_mc;
	struct tspld_attach_args *aa = aux;

	sc->sc_dev = self;
	tsc->sc_iot = aa->ta_iot;
	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX,
	    1, 0, &tsc->sc_idxh))
		panic("tsrtc_attach: couldn't map clock I/O space");
	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT,
	    1, 0, &tsc->sc_dath))
		panic("tsrtc_attach: couldn't map clock I/O space");

	sc->sc_year0 = 2000;
	sc->sc_mcread = tsrtc_read;
	sc->sc_mcwrite = tsrtc_write;
	sc->sc_flag = MC146818_BCD;
	mc146818_attach(sc);

	aprint_normal("\n");

	(*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
}

void
tsrtc_write(struct mc146818_softc *mc_sc, u_int reg, u_int datum)
{
	struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;

	bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
	bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum);
}

u_int
tsrtc_read(struct mc146818_softc *mc_sc, u_int reg)
{
	struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
	u_int datum;

	bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
	datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0);

	return (datum);
}