1*1dc652efSthorpej /* $NetBSD: mkclock.c,v 1.13 2023/12/20 05:33:58 thorpej Exp $ */
22ae6e40cStsutsui
32ae6e40cStsutsui /*
42ae6e40cStsutsui * Copyright (c) 1992, 1993
52ae6e40cStsutsui * The Regents of the University of California. All rights reserved.
62ae6e40cStsutsui * Copyright (c) 1994 Gordon W. Ross
72ae6e40cStsutsui * Copyright (c) 1993 Adam Glass
82ae6e40cStsutsui * Copyright (c) 1996 Paul Kranenburg
92ae6e40cStsutsui * Copyright (c) 1996
102ae6e40cStsutsui * The President and Fellows of Harvard College. All rights reserved.
112ae6e40cStsutsui *
122ae6e40cStsutsui * This software was developed by the Computer Systems Engineering group
132ae6e40cStsutsui * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
142ae6e40cStsutsui * contributed to Berkeley.
152ae6e40cStsutsui *
162ae6e40cStsutsui * All advertising materials mentioning features or use of this software
172ae6e40cStsutsui * must display the following acknowledgement:
182ae6e40cStsutsui * This product includes software developed by Harvard University.
192ae6e40cStsutsui * This product includes software developed by the University of
202ae6e40cStsutsui * California, Lawrence Berkeley Laboratory.
212ae6e40cStsutsui *
222ae6e40cStsutsui * Redistribution and use in source and binary forms, with or without
232ae6e40cStsutsui * modification, are permitted provided that the following conditions
242ae6e40cStsutsui * are met:
252ae6e40cStsutsui *
262ae6e40cStsutsui * 1. Redistributions of source code must retain the above copyright
272ae6e40cStsutsui * notice, this list of conditions and the following disclaimer.
282ae6e40cStsutsui * 2. Redistributions in binary form must reproduce the above copyright
292ae6e40cStsutsui * notice, this list of conditions and the following disclaimer in the
302ae6e40cStsutsui * documentation and/or other materials provided with the distribution.
312ae6e40cStsutsui * 3. All advertising materials mentioning features or use of this software
322ae6e40cStsutsui * must display the following acknowledgement:
332ae6e40cStsutsui * This product includes software developed by the University of
342ae6e40cStsutsui * California, Berkeley and its contributors.
352ae6e40cStsutsui * This product includes software developed by Paul Kranenburg.
362ae6e40cStsutsui * This product includes software developed by Harvard University.
372ae6e40cStsutsui * 4. Neither the name of the University nor the names of its contributors
382ae6e40cStsutsui * may be used to endorse or promote products derived from this software
392ae6e40cStsutsui * without specific prior written permission.
402ae6e40cStsutsui *
412ae6e40cStsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
422ae6e40cStsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
432ae6e40cStsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
442ae6e40cStsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
452ae6e40cStsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
462ae6e40cStsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
472ae6e40cStsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
482ae6e40cStsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
492ae6e40cStsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
502ae6e40cStsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
512ae6e40cStsutsui * SUCH DAMAGE.
522ae6e40cStsutsui *
532ae6e40cStsutsui * @(#)clock.c 8.1 (Berkeley) 6/11/93
542ae6e40cStsutsui *
552ae6e40cStsutsui */
562ae6e40cStsutsui
572ae6e40cStsutsui #include <sys/cdefs.h>
58*1dc652efSthorpej __KERNEL_RCSID(0, "$NetBSD: mkclock.c,v 1.13 2023/12/20 05:33:58 thorpej Exp $");
592ae6e40cStsutsui
602ae6e40cStsutsui /*
612ae6e40cStsutsui * Clock driver for 'mkclock' - Mostek MK48Txx TOD clock.
622ae6e40cStsutsui */
632ae6e40cStsutsui
642ae6e40cStsutsui #include <sys/param.h>
652ae6e40cStsutsui #include <sys/kernel.h>
662ae6e40cStsutsui #include <sys/device.h>
672ae6e40cStsutsui #include <sys/proc.h>
682ae6e40cStsutsui #include <sys/resourcevar.h>
692ae6e40cStsutsui #include <sys/systm.h>
702ae6e40cStsutsui
712ae6e40cStsutsui #include <uvm/uvm_extern.h>
722ae6e40cStsutsui
73b6584574Sdyoung #include <sys/bus.h>
742ae6e40cStsutsui #include <machine/autoconf.h>
752ae6e40cStsutsui #include <machine/eeprom.h>
762ae6e40cStsutsui #include <machine/cpu.h>
772ae6e40cStsutsui
782ae6e40cStsutsui #include <dev/clock_subr.h>
792ae6e40cStsutsui #include <dev/ic/mk48txxreg.h>
802ae6e40cStsutsui #include <dev/ic/mk48txxvar.h>
812ae6e40cStsutsui
822ae6e40cStsutsui #include <dev/sbus/sbusvar.h>
832ae6e40cStsutsui #include <dev/ebus/ebusreg.h>
842ae6e40cStsutsui #include <dev/ebus/ebusvar.h>
852ae6e40cStsutsui
8639a57b1cSmrg #include <sparc64/dev/fhcvar.h>
8739a57b1cSmrg
882ae6e40cStsutsui /*
892ae6e40cStsutsui * clock (eeprom) attaches at the sbus or the ebus (PCI)
902ae6e40cStsutsui */
9103bb6cc8Stsutsui static int mkclock_sbus_match(device_t, cfdata_t, void *);
9203bb6cc8Stsutsui static void mkclock_sbus_attach(device_t, device_t, void *);
932ae6e40cStsutsui
9403bb6cc8Stsutsui static int mkclock_ebus_match(device_t, cfdata_t, void *);
9503bb6cc8Stsutsui static void mkclock_ebus_attach(device_t, device_t, void *);
962ae6e40cStsutsui
9739a57b1cSmrg static int mkclock_fhc_match(device_t, cfdata_t, void *);
9839a57b1cSmrg static void mkclock_fhc_attach(device_t, device_t, void *);
9939a57b1cSmrg
1002ae6e40cStsutsui static void mkclock_attach(struct mk48txx_softc *, int);
1012ae6e40cStsutsui
1022ae6e40cStsutsui static int mkclock_wenable(struct todr_chip_handle *, int);
1032ae6e40cStsutsui
1042ae6e40cStsutsui
10503bb6cc8Stsutsui CFATTACH_DECL_NEW(mkclock_sbus, sizeof(struct mk48txx_softc),
1062ae6e40cStsutsui mkclock_sbus_match, mkclock_sbus_attach, NULL, NULL);
1072ae6e40cStsutsui
10803bb6cc8Stsutsui CFATTACH_DECL_NEW(mkclock_ebus, sizeof(struct mk48txx_softc),
1092ae6e40cStsutsui mkclock_ebus_match, mkclock_ebus_attach, NULL, NULL);
1102ae6e40cStsutsui
11139a57b1cSmrg CFATTACH_DECL_NEW(mkclock_fhc, sizeof(struct mk48txx_softc),
11239a57b1cSmrg mkclock_fhc_match, mkclock_fhc_attach, NULL, NULL);
11339a57b1cSmrg
1142ae6e40cStsutsui /*
1152ae6e40cStsutsui * The OPENPROM calls the clock the "eeprom", so we have to have our
1162ae6e40cStsutsui * own special match function to call it the "clock".
1172ae6e40cStsutsui */
1182ae6e40cStsutsui static int
mkclock_sbus_match(device_t parent,cfdata_t cf,void * aux)11903bb6cc8Stsutsui mkclock_sbus_match(device_t parent, cfdata_t cf, void *aux)
1202ae6e40cStsutsui {
1212ae6e40cStsutsui struct sbus_attach_args *sa = aux;
1222ae6e40cStsutsui
1232ae6e40cStsutsui return (strcmp("eeprom", sa->sa_name) == 0);
1242ae6e40cStsutsui }
1252ae6e40cStsutsui
1262ae6e40cStsutsui static int
mkclock_ebus_match(device_t parent,cfdata_t cf,void * aux)12703bb6cc8Stsutsui mkclock_ebus_match(device_t parent, cfdata_t cf, void *aux)
1282ae6e40cStsutsui {
1292ae6e40cStsutsui struct ebus_attach_args *ea = aux;
1302ae6e40cStsutsui
1312ae6e40cStsutsui return (strcmp("eeprom", ea->ea_name) == 0);
1322ae6e40cStsutsui }
1332ae6e40cStsutsui
13439a57b1cSmrg static int
mkclock_fhc_match(device_t parent,cfdata_t cf,void * aux)13539a57b1cSmrg mkclock_fhc_match(device_t parent, cfdata_t cf, void *aux)
13639a57b1cSmrg {
13739a57b1cSmrg struct fhc_attach_args *fa = aux;
13839a57b1cSmrg
13939a57b1cSmrg return (strcmp("eeprom", fa->fa_name) == 0);
14039a57b1cSmrg }
14139a57b1cSmrg
1422ae6e40cStsutsui /*
1432ae6e40cStsutsui * Attach a clock (really `eeprom') to the sbus or ebus.
1442ae6e40cStsutsui *
1452ae6e40cStsutsui * We ignore any existing virtual address as we need to map
1462ae6e40cStsutsui * this read-only and make it read-write only temporarily,
1472ae6e40cStsutsui * whenever we read or write the clock chip. The clock also
1482ae6e40cStsutsui * contains the ID ``PROM'', and I have already had the pleasure
1492ae6e40cStsutsui * of reloading the CPU type, Ethernet address, etc, by hand from
1502ae6e40cStsutsui * the console FORTH interpreter. I intend not to enjoy it again.
1512ae6e40cStsutsui *
1522ae6e40cStsutsui * the MK48T02 is 2K. the MK48T08 is 8K, and the MK48T59 is
1532ae6e40cStsutsui * supposed to be identical to it.
1542ae6e40cStsutsui *
1552ae6e40cStsutsui * This is *UGLY*! We probably have multiple mappings. But I do
1562ae6e40cStsutsui * know that this all fits inside an 8K page, so I'll just map in
1572ae6e40cStsutsui * once.
1582ae6e40cStsutsui *
1592ae6e40cStsutsui * What we really need is some way to record the bus attach args
1602ae6e40cStsutsui * so we can call *_bus_map() later with BUS_SPACE_MAP_READONLY
1612ae6e40cStsutsui * or not to write enable/disable the device registers. This is
1622ae6e40cStsutsui * a non-trivial operation.
1632ae6e40cStsutsui */
1642ae6e40cStsutsui
1652ae6e40cStsutsui /* ARGSUSED */
1662ae6e40cStsutsui static void
mkclock_sbus_attach(device_t parent,device_t self,void * aux)16703bb6cc8Stsutsui mkclock_sbus_attach(device_t parent, device_t self, void *aux)
1682ae6e40cStsutsui {
16903bb6cc8Stsutsui struct mk48txx_softc *sc = device_private(self);
1702ae6e40cStsutsui struct sbus_attach_args *sa = aux;
1712ae6e40cStsutsui int sz;
1722ae6e40cStsutsui
1734090172bStsutsui sc->sc_dev = self;
1742ae6e40cStsutsui sc->sc_bst = sa->sa_bustag;
1752ae6e40cStsutsui
1762ae6e40cStsutsui /* use sa->sa_regs[0].size? */
1772ae6e40cStsutsui sz = 8192;
1782ae6e40cStsutsui
1792ae6e40cStsutsui if (sbus_bus_map(sc->sc_bst,
1802ae6e40cStsutsui sa->sa_slot,
181e3b6fc3bSnakayama trunc_page(sa->sa_offset),
1822ae6e40cStsutsui sz,
1832ae6e40cStsutsui BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_READONLY,
1842ae6e40cStsutsui &sc->sc_bsh) != 0) {
18503bb6cc8Stsutsui aprint_error(": can't map register\n");
1862ae6e40cStsutsui return;
1872ae6e40cStsutsui }
1882ae6e40cStsutsui mkclock_attach(sc, sa->sa_node);
1892ae6e40cStsutsui }
1902ae6e40cStsutsui
1912ae6e40cStsutsui
1922ae6e40cStsutsui /* ARGSUSED */
1932ae6e40cStsutsui static void
mkclock_ebus_attach(device_t parent,device_t self,void * aux)194cbab9cadSchs mkclock_ebus_attach(device_t parent, device_t self, void *aux)
1952ae6e40cStsutsui {
19650cafe93Stsutsui struct mk48txx_softc *sc = device_private(self);
1972ae6e40cStsutsui struct ebus_attach_args *ea = aux;
1982ae6e40cStsutsui int sz;
1992ae6e40cStsutsui
2004090172bStsutsui sc->sc_dev = self;
2012ae6e40cStsutsui sc->sc_bst = ea->ea_bustag;
2022ae6e40cStsutsui
2032ae6e40cStsutsui /* hard code to 8K? */
2042ae6e40cStsutsui sz = ea->ea_reg[0].size;
2052ae6e40cStsutsui
206f5f7a747Sjdc /* Use the PROM address if there. */
207f5f7a747Sjdc if (ea->ea_nvaddr)
208f5f7a747Sjdc sparc_promaddr_to_handle(sc->sc_bst, ea->ea_vaddr[0],
209f5f7a747Sjdc &sc->sc_bsh);
210f5f7a747Sjdc else if (bus_space_map(sc->sc_bst,
2112ae6e40cStsutsui EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
2122ae6e40cStsutsui sz,
2132ae6e40cStsutsui BUS_SPACE_MAP_LINEAR,
2142ae6e40cStsutsui &sc->sc_bsh) != 0) {
21503bb6cc8Stsutsui aprint_error(": can't map register\n");
2162ae6e40cStsutsui return;
2172ae6e40cStsutsui }
2182ae6e40cStsutsui mkclock_attach(sc, ea->ea_node);
2192ae6e40cStsutsui }
2202ae6e40cStsutsui
22139a57b1cSmrg /* ARGSUSED */
22239a57b1cSmrg static void
mkclock_fhc_attach(device_t parent,device_t self,void * aux)223cbab9cadSchs mkclock_fhc_attach(device_t parent, device_t self, void *aux)
22439a57b1cSmrg {
22539a57b1cSmrg struct mk48txx_softc *sc = device_private(self);
22639a57b1cSmrg struct fhc_attach_args *fa = aux;
22739a57b1cSmrg
22839a57b1cSmrg sc->sc_dev = self;
22939a57b1cSmrg sc->sc_bst = fa->fa_bustag;
23039a57b1cSmrg
23139a57b1cSmrg if (fhc_bus_map(sc->sc_bst,
23239a57b1cSmrg fa->fa_reg[0].fbr_slot,
23339a57b1cSmrg (fa->fa_reg[0].fbr_offset & ~NBPG),
23439a57b1cSmrg fa->fa_reg[0].fbr_size,
23539a57b1cSmrg BUS_SPACE_MAP_LINEAR,
23639a57b1cSmrg &sc->sc_bsh) != 0) {
23739a57b1cSmrg aprint_error(": can't map register\n");
23839a57b1cSmrg return;
23939a57b1cSmrg }
24039a57b1cSmrg mkclock_attach(sc, fa->fa_node);
24139a57b1cSmrg }
24239a57b1cSmrg
2432ae6e40cStsutsui
2442ae6e40cStsutsui static void
mkclock_attach(struct mk48txx_softc * sc,int node)2452ae6e40cStsutsui mkclock_attach(struct mk48txx_softc *sc, int node)
2462ae6e40cStsutsui {
2472ae6e40cStsutsui
2482ae6e40cStsutsui sc->sc_model = prom_getpropstring(node, "model");
2492ae6e40cStsutsui
2502ae6e40cStsutsui #ifdef DIAGNOSTIC
2512ae6e40cStsutsui if (sc->sc_model == NULL)
2522ae6e40cStsutsui panic("clockattach: no model property");
2532ae6e40cStsutsui #endif
2542ae6e40cStsutsui
2552ae6e40cStsutsui /* Our TOD clock year 0 is 1968 */
2562ae6e40cStsutsui sc->sc_year0 = 1968;
2572ae6e40cStsutsui
2582ae6e40cStsutsui /* Save info for the clock wenable call. */
2592ae6e40cStsutsui sc->sc_handle.todr_setwen = mkclock_wenable;
2602ae6e40cStsutsui
26105517e38Stsutsui mk48txx_attach(sc);
26205517e38Stsutsui
26303bb6cc8Stsutsui aprint_normal("\n");
2642ae6e40cStsutsui }
2652ae6e40cStsutsui
2662ae6e40cStsutsui /*
2672ae6e40cStsutsui * Write en/dis-able clock registers. We coordinate so that several
2682ae6e40cStsutsui * writers can run simultaneously.
2692ae6e40cStsutsui */
2702ae6e40cStsutsui static int
mkclock_wenable(struct todr_chip_handle * handle,int onoff)2712ae6e40cStsutsui mkclock_wenable(struct todr_chip_handle *handle, int onoff)
2722ae6e40cStsutsui {
2732ae6e40cStsutsui struct mk48txx_softc *sc;
2742ae6e40cStsutsui vm_prot_t prot;
2752ae6e40cStsutsui vaddr_t va;
2762ae6e40cStsutsui int s, err = 0;
2772ae6e40cStsutsui static int writers;
2782ae6e40cStsutsui
2792171cc30Smrg /* XXXSMP */
2802ae6e40cStsutsui s = splhigh();
2812ae6e40cStsutsui if (onoff)
2822ae6e40cStsutsui prot = writers++ == 0 ? VM_PROT_READ|VM_PROT_WRITE : 0;
2832ae6e40cStsutsui else
2842ae6e40cStsutsui prot = --writers == 0 ? VM_PROT_READ : 0;
2852ae6e40cStsutsui splx(s);
2862ae6e40cStsutsui if (prot == VM_PROT_NONE) {
2872ae6e40cStsutsui return 0;
2882ae6e40cStsutsui }
2892ae6e40cStsutsui sc = handle->cookie;
2902ae6e40cStsutsui va = (vaddr_t)bus_space_vaddr(sc->sc_bst, sc->sc_bsh);
2912ae6e40cStsutsui if (va == 0UL) {
2922ae6e40cStsutsui printf("clock_wenable: WARNING -- cannot get va\n");
2932ae6e40cStsutsui return EIO;
2942ae6e40cStsutsui }
2952ae6e40cStsutsui pmap_kprotect(va, prot);
2962ae6e40cStsutsui return (err);
2972ae6e40cStsutsui }
298