1/*	$NetBSD: mcclock_ofisa.c,v 1.1 2021/04/27 21:39:39 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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/*
33 * OFW ISA attachment for the mc146818 real time clock.
34 */
35
36#include <sys/cdefs.h>
37__KERNEL_RCSID(0, "$NetBSD: mcclock_ofisa.c,v 1.1 2021/04/27 21:39:39 thorpej Exp $");
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/device.h>
43
44#include <sys/bus.h>
45
46#include <dev/clock_subr.h>
47#include <dev/ic/mc146818var.h>
48
49#include <dev/ofw/openfirm.h>
50#include <dev/isa/isavar.h>
51#include <dev/ofisa/ofisavar.h>
52
53struct mcclock_ofisa_platform_data {
54	int			year0;
55	int			flags;
56};
57
58static const struct device_compatible_entry compat_data[] = {
59	{ .compat = "pnpPNP,b00" },
60	DEVICE_COMPAT_EOL
61};
62
63static const struct mcclock_ofisa_platform_data dec_dnard_data = {
64	.year0 = 1900,
65	.flags = MC146818_BCD,
66};
67
68static const struct device_compatible_entry platform_data[] = {
69	{ .compat = "DEC,DNARD",	.data = &dec_dnard_data, },
70	DEVICE_COMPAT_EOL
71};
72
73static const struct mcclock_ofisa_platform_data *
74lookup_platform_data(char *model, size_t const size)
75{
76	const struct device_compatible_entry *dce;
77
78	if (OF_getprop(OF_finddevice("/"), "model", model, size) <= 0) {
79		return NULL;
80	}
81
82	/* should be plenty big enough, but never hurts... */
83	model[size - 1] = '\0';
84	const char *cmodel = model;
85
86	dce = device_compatible_lookup(&cmodel, 1, platform_data);
87	if (dce != NULL) {
88		return dce->data;
89	}
90	return NULL;
91}
92
93static u_int
94mcclock_ofisa_read(struct mc146818_softc *sc, u_int reg)
95{
96	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
97	return bus_space_read_1(sc->sc_bst, sc->sc_bsh, 1);
98}
99
100static void
101mcclock_ofisa_write(struct mc146818_softc *sc, u_int reg, u_int datum)
102{
103	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 0, reg);
104	bus_space_write_1(sc->sc_bst, sc->sc_bsh, 1, (uint8_t)datum);
105}
106
107static int
108mcclock_ofisa_match(device_t parent, cfdata_t cf, void *aux)
109{
110	struct ofisa_attach_args *aa = aux;
111	int rv;
112
113	rv = of_compatible_match(aa->oba.oba_phandle, compat_data) ? 5 : 0;
114#ifdef _MCCLOCK_OFISA_MD_MATCH
115	if (!rv)
116		rv = mcclock_ofisa_md_match(parent, cf, aux);
117#endif
118	return rv;
119}
120
121static void
122mcclock_ofisa_attach(device_t parent, device_t self, void *aux)
123{
124	struct mc146818_softc *sc = device_private(self);
125	struct ofisa_attach_args *aa = aux;
126	const struct mcclock_ofisa_platform_data *pd;
127	struct ofisa_reg_desc reg;
128	char model[64];
129	int n;
130
131	sc->sc_dev = self;
132
133	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
134#ifdef _MCCLOCK_OFISA_MD_REG_FIXUP
135	n = mcclock_ofisa_md_reg_fixup(parent, self, aux, &reg, 1, n);
136#endif
137	if (n != 1) {
138		aprint_error(": error getting register data\n");
139		return;
140	}
141	if (reg.len != 2) {
142		aprint_error(": weird register size (%lu, expected 2)\n",
143		    (unsigned long)reg.len);
144		return;
145	}
146
147	/*
148	 * We don't configure interrupts here; we're only using this
149	 * for the TODR.
150	 */
151
152	pd = lookup_platform_data(model, sizeof(model));
153
154	sc->sc_bst = (reg.type == OFISA_REG_TYPE_IO) ? aa->iot : aa->memt;
155	if (bus_space_map(sc->sc_bst, reg.addr, reg.len, 0, &sc->sc_bsh) != 0) {
156		aprint_error(": can't map register space\n");
157		return;
158	}
159
160	/*
161	 * These default to 0.  We'll warn after attaching if we didn't
162	 * find any platform data.
163	 */
164	if (pd != NULL) {
165		sc->sc_year0 = pd->year0;
166		sc->sc_flag = pd->flags;
167	}
168	sc->sc_mcread = mcclock_ofisa_read;
169	sc->sc_mcwrite = mcclock_ofisa_write;
170
171	mc146818_attach(sc);
172
173	aprint_normal("\n");
174
175	if (pd == NULL) {
176		/*
177		 * We could dynamically compute at least BCD-ness and
178		 * base year by comparing against the OFW get-time
179		 * call, but it hardly seems worth it right now.
180		 */
181		aprint_error_dev(self,
182		    "WARNING: no platform data for '%s', using defaults.\n",
183		    model);
184	}
185}
186
187CFATTACH_DECL_NEW(mcclock_ofisa, sizeof(struct mc146818_softc),
188    mcclock_ofisa_match, mcclock_ofisa_attach, NULL, NULL);
189