1 /* $NetBSD: mcclock_frodo.c,v 1.2 2023/01/15 06:19:45 tsutsui Exp $ */
2 /*-
3 * Copyright (c) 2014 Izumi Tsutsui. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: mcclock_frodo.c,v 1.2 2023/01/15 06:19:45 tsutsui Exp $");
28
29 #include <sys/param.h>
30 #include <sys/device.h>
31 #include <sys/bus.h>
32
33 #include <machine/cpu.h>
34
35 #include <dev/clock_subr.h>
36 #include <dev/ic/mc146818reg.h>
37 #include <dev/ic/mc146818var.h>
38
39 #include <hp300/dev/frodoreg.h>
40 #include <hp300/dev/frodovar.h>
41
42 #include "ioconf.h"
43
44 static int mcclock_frodo_probe(device_t, cfdata_t, void *);
45 static void mcclock_frodo_attach(device_t, device_t, void *);
46
47 static u_int mcclock_frodo_read(struct mc146818_softc *, u_int);
48 static void mcclock_frodo_write(struct mc146818_softc *, u_int, u_int);
49
50 CFATTACH_DECL_NEW(mcclock_frodo, sizeof(struct mc146818_softc),
51 mcclock_frodo_probe, mcclock_frodo_attach, NULL, NULL);
52
53 #define MCCLOCK_FRODO_NPORTS 2
54
55 static int
mcclock_frodo_probe(device_t parent,cfdata_t cf,void * aux)56 mcclock_frodo_probe(device_t parent, cfdata_t cf, void *aux)
57 {
58 struct frodo_attach_args *fa = aux;
59
60 /* only 425e has a valid mcclock */
61 if (machineid != HP_425 || mmuid != MMUID_425_E)
62 return 0;
63
64 if (strcmp(fa->fa_name, mcclock_cd.cd_name) != 0)
65 return 0;
66
67 if (fa->fa_offset != FRODO_CALENDAR)
68 return 0;
69
70 return 1;
71 }
72
73 static void
mcclock_frodo_attach(device_t parent,device_t self,void * aux)74 mcclock_frodo_attach(device_t parent, device_t self, void *aux)
75 {
76 struct mc146818_softc *sc = device_private(self);
77 struct frodo_attach_args *fa = aux;
78
79 sc->sc_dev = self;
80 sc->sc_bst = fa->fa_bst;
81
82 if (bus_space_map(sc->sc_bst, fa->fa_base + fa->fa_offset,
83 MCCLOCK_FRODO_NPORTS << 2, 0, &sc->sc_bsh) != 0) {
84 aprint_error(": can't map register\n");
85 return;
86 }
87
88 sc->sc_year0 = 1900;
89 sc->sc_mcread = mcclock_frodo_read;
90 sc->sc_mcwrite = mcclock_frodo_write;
91 sc->sc_flag = 0;
92 mc146818_attach(sc);
93
94 aprint_normal("\n");
95
96 /* XXX: not sure which mode (BINARY or BCD) is used on HP-UX */
97 mcclock_frodo_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
98
99 /* make sure to start the 32.768kHz OSC */
100 mcclock_frodo_write(sc, MC_REGA,
101 (mcclock_frodo_read(sc, MC_REGA) & ~MC_REGA_DVMASK) |
102 MC_BASE_32_KHz);
103 }
104
105 /*
106 * mc146818 calendar chip in the frodo utility chip is can be accessed
107 * via register port at offset 0x80 and data port at offset 0x84.
108 *
109 * Note 4 byte offset stride is handled by frodo bus_space(9) layer,
110 * so bus_space_read(9) and bus_space_write(9) takes 0 or 1 as offset
111 * to access these registers.
112 */
113
114 static void
mcclock_frodo_write(struct mc146818_softc * sc,u_int reg,u_int datum)115 mcclock_frodo_write(struct mc146818_softc *sc, u_int reg, u_int datum)
116 {
117 bus_space_tag_t iot;
118 bus_space_handle_t ioh;
119
120 iot = sc->sc_bst;
121 ioh = sc->sc_bsh;
122
123 bus_space_write_1(iot, ioh, 0, reg);
124 bus_space_write_1(iot, ioh, 1, datum);
125 }
126
127 static u_int
mcclock_frodo_read(struct mc146818_softc * sc,u_int reg)128 mcclock_frodo_read(struct mc146818_softc *sc, u_int reg)
129 {
130 bus_space_tag_t iot;
131 bus_space_handle_t ioh;
132 u_int datum;
133
134 iot = sc->sc_bst;
135 ioh = sc->sc_bsh;
136
137 bus_space_write_1(iot, ioh, 0, reg);
138 datum = bus_space_read_1(iot, ioh, 1);
139
140 return datum;
141 }
142