xref: /netbsd-src/sys/arch/hpcarm/dev/j720ssp.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: j720ssp.c,v 1.30 2006/06/27 14:36:50 peter Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001, 2006 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by IWAMOTO Toshihiro.
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 /* Jornada 720 SSP port. */
40 
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: j720ssp.c,v 1.30 2006/06/27 14:36:50 peter Exp $");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 
48 #include <arm/sa11x0/sa11x0_var.h>
49 #include <arm/sa11x0/sa11x0_gpioreg.h>
50 #include <arm/sa11x0/sa11x0_ppcreg.h>
51 #include <arm/sa11x0/sa11x0_sspreg.h>
52 
53 #include <hpcarm/dev/j720sspvar.h>
54 
55 #ifdef DEBUG
56 #define DPRINTF(arg)	printf arg
57 #else
58 #define DPRINTF(arg)	/* nothing */
59 #endif
60 
61 #define BIT_INVERT(x)							\
62 	do {								\
63 		(x) = ((((x) & 0xf0) >> 4) | (((x) & 0x0f) << 4));	\
64 		(x) = ((((x) & 0xcc) >> 2) | (((x) & 0x33) << 2));	\
65 		(x) = ((((x) & 0xaa) >> 1) | (((x) & 0x55) << 1));	\
66 	} while (0)
67 
68 static int	j720ssp_match(struct device *, struct cfdata *, void *);
69 static void	j720ssp_attach(struct device *, struct device *, void *);
70 static int	j720ssp_search(struct device *, struct cfdata *,
71 		    const int *, void *);
72 static int	j720ssp_print(void *, const char *);
73 
74 CFATTACH_DECL(j720ssp, sizeof(struct j720ssp_softc),
75     j720ssp_match, j720ssp_attach, NULL, NULL);
76 
77 
78 static int
79 j720ssp_match(struct device *parent, struct cfdata *cf, void *aux)
80 {
81 
82 	if (strcmp(cf->cf_name, "j720ssp") != 0)
83 		return 0;
84 
85 	return 1;
86 }
87 
88 static void
89 j720ssp_attach(struct device *parent, struct device *self, void *aux)
90 {
91 	struct j720ssp_softc *sc = (void *)self;
92 	struct sa11x0_softc *psc = (void *)parent;
93 	struct sa11x0_attach_args *sa = aux;
94 
95 	sc->sc_iot = psc->sc_iot;
96 	sc->sc_gpioh = psc->sc_gpioh;
97 	sc->sc_parent = psc;
98 
99 	if (bus_space_map(sc->sc_iot, sa->sa_addr, sa->sa_size, 0,
100 	    &sc->sc_ssph)) {
101 		printf(": unable to map SSP registers\n");
102 		return;
103 	}
104 
105 	printf("\n");
106 
107 	config_search_ia(j720ssp_search, self, "j720ssp", NULL);
108 }
109 
110 static int
111 j720ssp_search(struct device *parent, struct cfdata *cf,
112     const int *ldesc, void *aux)
113 {
114 
115 	if (config_match(parent, cf, NULL) > 0)
116 		config_attach(parent, cf, NULL, j720ssp_print);
117 
118 	return 0;
119 }
120 
121 static int
122 j720ssp_print(void *aux, const char *pnp)
123 {
124 
125 	return pnp ? QUIET : UNCONF;
126 }
127 
128 int
129 j720ssp_readwrite(struct j720ssp_softc *sc, int drainfifo, int in,
130     int *out, int wait)
131 {
132 	int timeout;
133 
134 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_TNF))
135 		continue;
136 
137 	timeout = 400000;
138 	while (bus_space_read_4(sc->sc_iot, sc->sc_gpioh, SAGPIO_PLR) & 0x400)
139 		if (--timeout == 0) {
140 			DPRINTF(("j720ssp_readwrite: timeout 0\n"));
141 			return -1;
142 		}
143 	if (drainfifo) {
144 		while (bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) &
145 		      SR_RNE)
146 			bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
147 		delay(wait);
148 	}
149 
150 	BIT_INVERT(in);
151 	bus_space_write_4(sc->sc_iot, sc->sc_ssph, SASSP_DR, in << 8);
152 
153 	delay(wait);
154 	timeout = 100000;
155 	while (!(bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_SR) & SR_RNE))
156 		if (--timeout == 0) {
157 			DPRINTF(("j720ssp_readwrite: timeout 1\n"));
158 			return -1;
159 		}
160 
161 	*out = bus_space_read_4(sc->sc_iot, sc->sc_ssph, SASSP_DR);
162 	BIT_INVERT(*out);
163 
164 	return 0;
165 }
166