1132720Skan/* $NetBSD: mcclock_gbus.c,v 1.5 2024/03/06 07:34:11 thorpej Exp $ */
2132720Skan
3169691Skan/*
4132720Skan * Copyright (c) 1997 by Matthew Jacob
5132720Skan * NASA AMES Research Center.
6132720Skan * All rights reserved.
7132720Skan *
8132720Skan * Redistribution and use in source and binary forms, with or without
9132720Skan * modification, are permitted provided that the following conditions
10132720Skan * are met:
11132720Skan * 1. Redistributions of source code must retain the above copyright
12132720Skan *    notice immediately at the beginning of the file, without modification,
13132720Skan *    this list of conditions, and the following disclaimer.
14132720Skan * 2. Redistributions in binary form must reproduce the above copyright
15132720Skan *    notice, this list of conditions and the following disclaimer in the
16132720Skan *    documentation and/or other materials provided with the distribution.
17132720Skan * 3. The name of the author may not be used to endorse or promote products
18169691Skan *    derived from this software without specific prior written permission.
19132720Skan *
20132720Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21132720Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22132720Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23132720Skan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24132720Skan * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25132720Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26132720Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27132720Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28132720Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29132720Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30132720Skan * SUCH DAMAGE.
31132720Skan */
32132720Skan
33132720Skan#include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
34132720Skan
35132720Skan__KERNEL_RCSID(0, "$NetBSD: mcclock_gbus.c,v 1.5 2024/03/06 07:34:11 thorpej Exp $");
36132720Skan
37132720Skan#include <sys/param.h>
38132720Skan#include <sys/kernel.h>
39132720Skan#include <sys/systm.h>
40132720Skan#include <sys/device.h>
41132720Skan
42132720Skan#include <sys/bus.h>
43132720Skan
44132720Skan#include <alpha/gbus/gbusvar.h>
45132720Skan
46132720Skan#include <dev/clock_subr.h>
47132720Skan
48132720Skan#include <dev/ic/mc146818reg.h>
49132720Skan#include <dev/ic/mc146818var.h>
50132720Skan
51132720Skan#include <alpha/alpha/mcclockvar.h>
52132720Skan
53132720Skan#include "ioconf.h"
54132720Skan
55132720Skanstatic int	mcclock_gbus_match(device_t, cfdata_t, void *);
56132720Skanstatic void	mcclock_gbus_attach(device_t, device_t, void *);
57132720Skan
58169691SkanCFATTACH_DECL_NEW(mcclock_gbus, sizeof(struct mcclock_softc),
59169691Skan    mcclock_gbus_match, mcclock_gbus_attach, NULL, NULL);
60132720Skan
61132720Skanstatic void	mcclock_gbus_write(struct mc146818_softc *, u_int, u_int);
62132720Skanstatic u_int	mcclock_gbus_read(struct mc146818_softc *, u_int);
63132720Skan
64132720Skanstatic int
65132720Skanmcclock_gbus_match(device_t parent, cfdata_t cf, void *aux)
66132720Skan{
67132720Skan	struct gbus_attach_args *ga = aux;
68132720Skan
69132720Skan	if (strcmp(ga->ga_name, mcclock_cd.cd_name))
70132720Skan		return (0);
71132720Skan	return (1);
72132720Skan}
73132720Skan
74132720Skanstatic void
75132720Skanmcclock_gbus_attach(device_t parent, device_t self, void *aux)
76132720Skan{
77132720Skan	struct mcclock_softc *msc = device_private(self);
78132720Skan	struct mc146818_softc *sc = &msc->sc_mc146818;
79132720Skan	struct gbus_attach_args *ga = aux;
80132720Skan
81132720Skan	sc->sc_dev = self;
82132720Skan	sc->sc_bst = ga->ga_iot;
83132720Skan
84132720Skan	if (bus_space_map(sc->sc_bst, ga->ga_offset, MC_NREGS+MC_NVRAM_SIZE,
85132720Skan			  0, &sc->sc_bsh) != 0) {
86132720Skan		panic("mcclock_gbus_attach: couldn't map clock I/O space");
87132720Skan	}
88132720Skan
89132720Skan	/* The RTC is accessible only by a CPU on the primary CPU module. */
90132720Skan	msc->sc_primary_only = true;
91132720Skan
92132720Skan	sc->sc_mcread  = mcclock_gbus_read;
93132720Skan	sc->sc_mcwrite = mcclock_gbus_write;
94132720Skan
95132720Skan	mcclock_attach(msc);
96132720Skan}
97132720Skan
98132720Skanstatic void
99132720Skanmcclock_gbus_write(struct mc146818_softc *sc, u_int reg, u_int val)
100132720Skan{
101132720Skan	bus_space_write_1(sc->sc_bst, sc->sc_bsh, reg, (uint8_t)val);
102132720Skan}
103132720Skan
104132720Skanstatic u_int
105132720Skanmcclock_gbus_read(struct mc146818_softc *sc, u_int reg)
106132720Skan{
107132720Skan	return bus_space_read_1(sc->sc_bst, sc->sc_bsh, reg);
108132720Skan}
109132720Skan