mcclock.c revision 1.1.4.2 1 1.1.4.2 simonb /* $NetBSD: mcclock.c,v 1.1.4.2 2006/04/22 11:37:21 simonb Exp $ */
2 1.1.4.2 simonb
3 1.1.4.2 simonb /*
4 1.1.4.2 simonb * Copyright (c) 1995, 1996 Carnegie-Mellon University.
5 1.1.4.2 simonb * All rights reserved.
6 1.1.4.2 simonb *
7 1.1.4.2 simonb * Author: Chris G. Demetriou
8 1.1.4.2 simonb *
9 1.1.4.2 simonb * Permission to use, copy, modify and distribute this software and
10 1.1.4.2 simonb * its documentation is hereby granted, provided that both the copyright
11 1.1.4.2 simonb * notice and this permission notice appear in all copies of the
12 1.1.4.2 simonb * software, derivative works or modified versions, and any portions
13 1.1.4.2 simonb * thereof, and that both notices appear in supporting documentation.
14 1.1.4.2 simonb *
15 1.1.4.2 simonb * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 1.1.4.2 simonb * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 1.1.4.2 simonb * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 1.1.4.2 simonb *
19 1.1.4.2 simonb * Carnegie Mellon requests users of this software to return to
20 1.1.4.2 simonb *
21 1.1.4.2 simonb * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 1.1.4.2 simonb * School of Computer Science
23 1.1.4.2 simonb * Carnegie Mellon University
24 1.1.4.2 simonb * Pittsburgh PA 15213-3890
25 1.1.4.2 simonb *
26 1.1.4.2 simonb * any improvements or extensions that they make and grant Carnegie the
27 1.1.4.2 simonb * rights to redistribute these changes.
28 1.1.4.2 simonb */
29 1.1.4.2 simonb
30 1.1.4.2 simonb #include <sys/cdefs.h>
31 1.1.4.2 simonb __KERNEL_RCSID(0, "$NetBSD: mcclock.c,v 1.1.4.2 2006/04/22 11:37:21 simonb Exp $");
32 1.1.4.2 simonb
33 1.1.4.2 simonb #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
34 1.1.4.2 simonb
35 1.1.4.2 simonb #include <sys/param.h>
36 1.1.4.2 simonb #include <sys/kernel.h>
37 1.1.4.2 simonb #include <sys/systm.h>
38 1.1.4.2 simonb #include <sys/device.h>
39 1.1.4.2 simonb
40 1.1.4.2 simonb #include <machine/autoconf.h>
41 1.1.4.2 simonb #include <machine/bus.h>
42 1.1.4.2 simonb
43 1.1.4.2 simonb #include <dev/clock_subr.h>
44 1.1.4.2 simonb #include <dev/ic/mc146818reg.h>
45 1.1.4.2 simonb #include <dev/ic/mc146818var.h>
46 1.1.4.2 simonb
47 1.1.4.2 simonb #define MCCLOCK_NPORTS 2
48 1.1.4.2 simonb
49 1.1.4.2 simonb static int mcclock_match(struct device *, struct cfdata *, void *);
50 1.1.4.2 simonb static void mcclock_attach(struct device *, struct device *, void *);
51 1.1.4.2 simonb
52 1.1.4.2 simonb CFATTACH_DECL(mcclock, sizeof (struct mc146818_softc),
53 1.1.4.2 simonb mcclock_match, mcclock_attach, NULL, NULL);
54 1.1.4.2 simonb
55 1.1.4.2 simonb static void mcclock_write(struct mc146818_softc *, u_int, u_int);
56 1.1.4.2 simonb static u_int mcclock_read(struct mc146818_softc *, u_int);
57 1.1.4.2 simonb
58 1.1.4.2 simonb
59 1.1.4.2 simonb static int
60 1.1.4.2 simonb mcclock_match(struct device *parent, struct cfdata *match, void *aux)
61 1.1.4.2 simonb {
62 1.1.4.2 simonb static int mcclock_found;
63 1.1.4.2 simonb
64 1.1.4.2 simonb if (mcclock_found)
65 1.1.4.2 simonb return 0;
66 1.1.4.2 simonb
67 1.1.4.2 simonb mcclock_found = 1;
68 1.1.4.2 simonb
69 1.1.4.2 simonb return 1;
70 1.1.4.2 simonb }
71 1.1.4.2 simonb
72 1.1.4.2 simonb static void
73 1.1.4.2 simonb mcclock_attach(struct device *parent, struct device *self, void *aux)
74 1.1.4.2 simonb {
75 1.1.4.2 simonb struct mc146818_softc *sc = (void *)self;
76 1.1.4.2 simonb struct mainbus_attach_args *ma = aux;
77 1.1.4.2 simonb
78 1.1.4.2 simonb sc->sc_bst = ma->ma_iot;
79 1.1.4.2 simonb if (bus_space_map(sc->sc_bst, ma->ma_addr, MCCLOCK_NPORTS,
80 1.1.4.2 simonb 0, &sc->sc_bsh)) {
81 1.1.4.2 simonb aprint_error("mcclock_attach: unable to map registers\n");
82 1.1.4.2 simonb return;
83 1.1.4.2 simonb }
84 1.1.4.2 simonb
85 1.1.4.2 simonb sc->sc_year0 = 2000;
86 1.1.4.2 simonb sc->sc_mcread = mcclock_read;
87 1.1.4.2 simonb sc->sc_mcwrite = mcclock_write;
88 1.1.4.2 simonb sc->sc_flag = MC146818_BCD;
89 1.1.4.2 simonb mc146818_attach(sc);
90 1.1.4.2 simonb
91 1.1.4.2 simonb printf("\n");
92 1.1.4.2 simonb
93 1.1.4.2 simonb (*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
94 1.1.4.2 simonb
95 1.1.4.2 simonb todr_attach(&sc->sc_handle);
96 1.1.4.2 simonb }
97 1.1.4.2 simonb
98 1.1.4.2 simonb static void
99 1.1.4.2 simonb mcclock_write(struct mc146818_softc *sc, u_int reg, u_int datum)
100 1.1.4.2 simonb {
101 1.1.4.2 simonb bus_space_tag_t iot;
102 1.1.4.2 simonb bus_space_handle_t ioh;
103 1.1.4.2 simonb
104 1.1.4.2 simonb iot = sc->sc_bst;
105 1.1.4.2 simonb ioh = sc->sc_bsh;
106 1.1.4.2 simonb
107 1.1.4.2 simonb bus_space_write_1(iot, ioh, 0, reg);
108 1.1.4.2 simonb bus_space_write_1(iot, ioh, 1, datum);
109 1.1.4.2 simonb }
110 1.1.4.2 simonb
111 1.1.4.2 simonb static u_int
112 1.1.4.2 simonb mcclock_read(struct mc146818_softc *sc, u_int reg)
113 1.1.4.2 simonb {
114 1.1.4.2 simonb bus_space_tag_t iot;
115 1.1.4.2 simonb bus_space_handle_t ioh;
116 1.1.4.2 simonb u_int datum;
117 1.1.4.2 simonb
118 1.1.4.2 simonb iot = sc->sc_bst;
119 1.1.4.2 simonb ioh = sc->sc_bsh;
120 1.1.4.2 simonb
121 1.1.4.2 simonb bus_space_write_1(iot, ioh, 0, reg);
122 1.1.4.2 simonb datum = bus_space_read_1(iot, ioh, 1);
123 1.1.4.2 simonb
124 1.1.4.2 simonb return datum;
125 1.1.4.2 simonb }
126