xref: /netbsd/sys/dev/pci/cy82c693.c (revision bf9ec67e)
1 /* $NetBSD: cy82c693.c,v 1.3 2001/11/13 07:35:02 lukem Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Common routines to read/write control registers on the Cypress 82c693
41  * hyperCache(tm) Stand-Alone PCI Peripheral Controller with USB.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: cy82c693.c,v 1.3 2001/11/13 07:35:02 lukem Exp $");
46 
47 #include "opt_multiprocessor.h"
48 #include "opt_lockdebug.h"
49 
50 #include <sys/param.h>
51 #include <sys/device.h>
52 #include <sys/systm.h>
53 #include <sys/lock.h>
54 
55 #include <machine/bus.h>
56 
57 #include <dev/pci/pcireg.h>
58 #include <dev/pci/pcivar.h>
59 
60 #include <dev/pci/cy82c693reg.h>
61 #include <dev/pci/cy82c693var.h>
62 
63 static struct cy82c693_handle cyhc_handle;
64 static int cyhc_initialized;
65 
66 static struct simplelock cyhc_slock = SIMPLELOCK_INITIALIZER;
67 
68 #define	CYHC_LOCK(s)							\
69 do {									\
70 	s = splhigh();							\
71 	simple_lock(&cyhc_slock);					\
72 } while (0)
73 
74 #define	CYHC_UNLOCK(s)							\
75 do {									\
76 	simple_unlock(&cyhc_slock);					\
77 	splx(s);							\
78 } while (0)
79 
80 const struct cy82c693_handle *
81 cy82c693_init(bus_space_tag_t iot)
82 {
83 	bus_space_handle_t ioh;
84 	int s;
85 
86 	CYHC_LOCK(s);
87 
88 	if (cyhc_initialized) {
89 		CYHC_UNLOCK(s);
90 		if (iot != cyhc_handle.cyhc_iot)
91 			panic("cy82c693_init");
92 		return (&cyhc_handle);
93 	}
94 
95 	if (bus_space_map(iot, CYHC_CONFIG_ADDR, 2, 0, &ioh) != 0) {
96 		CYHC_UNLOCK(s);
97 		return (NULL);
98 	}
99 
100 	cyhc_handle.cyhc_iot = iot;
101 	cyhc_handle.cyhc_ioh = ioh;
102 
103 	cyhc_initialized = 1;
104 
105 	CYHC_UNLOCK(s);
106 
107 	return (&cyhc_handle);
108 }
109 
110 u_int8_t
111 cy82c693_read(const struct cy82c693_handle *cyhc, int reg)
112 {
113 	int s;
114 	u_int8_t rv;
115 
116 	CYHC_LOCK(s);
117 
118 	if (cyhc_initialized == 0) {
119 		CYHC_UNLOCK(s);
120 		panic("cy82c693_read");
121 	}
122 
123 	bus_space_write_1(cyhc->cyhc_iot, cyhc->cyhc_ioh, 0, reg);
124 	rv = bus_space_read_1(cyhc->cyhc_iot, cyhc->cyhc_ioh, 1);
125 
126 	CYHC_UNLOCK(s);
127 
128 	return (rv);
129 }
130 
131 void
132 cy82c693_write(const struct cy82c693_handle *cyhc, int reg, u_int8_t val)
133 {
134 	int s;
135 
136 	CYHC_LOCK(s);
137 
138 	if (cyhc_initialized == 0) {
139 		CYHC_UNLOCK(s);
140 		panic("cy82c693_write");
141 	}
142 
143 	bus_space_write_1(cyhc->cyhc_iot, cyhc->cyhc_ioh, 0, reg);
144 	bus_space_write_1(cyhc->cyhc_iot, cyhc->cyhc_ioh, 1, val);
145 
146 	CYHC_UNLOCK(s);
147 }
148