xref: /openbsd/sys/arch/loongson/dev/mcclock_isa.c (revision 3bef86f7)
1 /*	$OpenBSD: mcclock_isa.c,v 1.4 2022/10/15 14:58:54 jsg Exp $	*/
2 /*	$NetBSD: mcclock_isa.c,v 1.5 1996/12/05 01:39:29 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
6  * All rights reserved.
7  *
8  * Author: Chris G. Demetriou
9  *
10  * Permission to use, copy, modify and distribute this software and
11  * its documentation is hereby granted, provided that both the copyright
12  * notice and this permission notice appear in all copies of the
13  * software, derivative works or modified versions, and any portions
14  * thereof, and that both notices appear in supporting documentation.
15  *
16  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
17  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
18  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
19  *
20  * Carnegie Mellon requests users of this software to return to
21  *
22  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
23  *  School of Computer Science
24  *  Carnegie Mellon University
25  *  Pittsburgh PA 15213-3890
26  *
27  * any improvements or extensions that they make and grant Carnegie the
28  * rights to redistribute these changes.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 
36 #include <machine/bus.h>
37 
38 #include <dev/clock_subr.h>
39 #include <dev/ic/mc146818reg.h>
40 #include <dev/isa/isavar.h>
41 
42 #include <loongson/dev/mcclockvar.h>
43 
44 struct mcclock_isa_softc {
45 	struct mcclock_softc	sc_mcclock;
46 
47 	bus_space_tag_t		sc_iot;
48 	bus_space_handle_t	sc_ioh;
49 };
50 
51 int	mcclock_isa_match(struct device *, void *, void *);
52 void	mcclock_isa_attach(struct device *, struct device *, void *);
53 
54 const struct cfattach mcclock_isa_ca = {
55 	sizeof (struct mcclock_isa_softc), mcclock_isa_match,
56 	    mcclock_isa_attach,
57 };
58 
59 void	mcclock_isa_write(struct mcclock_softc *, u_int, u_int);
60 u_int	mcclock_isa_read(struct mcclock_softc *, u_int);
61 
62 const struct mcclock_busfns mcclock_isa_busfns = {
63 	mcclock_isa_write, mcclock_isa_read,
64 };
65 
66 int
67 mcclock_isa_match(struct device *parent, void *match, void *aux)
68 {
69 	struct isa_attach_args *ia = aux;
70 	bus_space_handle_t ioh;
71 
72         if ((ia->ia_iobase != IOBASEUNK && ia->ia_iobase != 0x70) ||
73             /* (ia->ia_iosize != 0 && ia->ia_iosize != 0x2) || XXX isa.c */
74             ia->ia_maddr != MADDRUNK || ia->ia_msize != 0 ||
75             ia->ia_irq != IRQUNK || ia->ia_drq != DRQUNK)
76                 return (0);
77 
78 	if (bus_space_map(ia->ia_iot, 0x70, 0x2, 0, &ioh))
79 		return (0);
80 
81 	bus_space_unmap(ia->ia_iot, ioh, 0x2);
82 
83 	ia->ia_iobase = 0x70;
84 	ia->ia_iosize = 0x2;
85 
86 	return (1);
87 }
88 
89 void
90 mcclock_isa_attach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct isa_attach_args *ia = aux;
93 	struct mcclock_isa_softc *sc = (struct mcclock_isa_softc *)self;
94 
95 	sc->sc_iot = ia->ia_iot;
96 	if (bus_space_map(sc->sc_iot, ia->ia_iobase, ia->ia_iosize, 0,
97 	    &sc->sc_ioh))
98 		panic("mcclock_isa_attach: couldn't map clock I/O space");
99 
100 	mcclock_attach(&sc->sc_mcclock, &mcclock_isa_busfns);
101 }
102 
103 void
104 mcclock_isa_write(struct mcclock_softc *mcsc, u_int reg, u_int datum)
105 {
106 	struct mcclock_isa_softc *sc = (struct mcclock_isa_softc *)mcsc;
107 	bus_space_tag_t iot = sc->sc_iot;
108 	bus_space_handle_t ioh = sc->sc_ioh;
109 
110 	bus_space_write_1(iot, ioh, 0, reg);
111 	bus_space_write_1(iot, ioh, 1, datum);
112 }
113 
114 u_int
115 mcclock_isa_read(struct mcclock_softc *mcsc, u_int reg)
116 {
117 	struct mcclock_isa_softc *sc = (struct mcclock_isa_softc *)mcsc;
118 	bus_space_tag_t iot = sc->sc_iot;
119 	bus_space_handle_t ioh = sc->sc_ioh;
120 
121 	bus_space_write_1(iot, ioh, 0, reg);
122 	return bus_space_read_1(iot, ioh, 1);
123 }
124