summaryrefslogtreecommitdiff
path: root/sys/arch/mips/alchemy/dev/aupsc.c
blob: 68ccfefbf27199a0f4d492820147c42e4d5848b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* $NetBSD: aupsc.c,v 1.9 2021/08/07 16:18:58 thorpej Exp $ */

/*-
 * Copyright (c) 2006 Shigeyuki Fukushima.
 * All rights reserved.
 *
 * Written by Shigeyuki Fukushima.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 * 3. The name of the author may not be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: aupsc.c,v 1.9 2021/08/07 16:18:58 thorpej Exp $");

#include "locators.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/errno.h>

#include <sys/bus.h>
#include <machine/cpu.h>

#include <mips/alchemy/include/aubusvar.h>
#include <mips/alchemy/include/aureg.h>
#include <mips/alchemy/dev/aupscreg.h>
#include <mips/alchemy/dev/aupscvar.h>
#include <mips/alchemy/dev/ausmbus_pscreg.h>

struct aupsc_softc {
	device_t		sc_dev;
	bus_space_tag_t		sc_bust;
	bus_space_handle_t	sc_bush;
	int			sc_pscsel;
};

const struct aupsc_proto {
	const char *name;
	int protocol;
} aupsc_protos [] = {
	{ "ausmbus", AUPSC_SEL_SMBUS },
	{ "auspi", AUPSC_SEL_SPI },
#if 0
	{ "auaudio" },
	{ "aui2s" },
#endif
	{ NULL, AUPSC_SEL_DISABLE }
};

static int	aupsc_match(device_t, struct cfdata *, void *);
static void	aupsc_attach(device_t, device_t, void *);
static int	aupsc_submatch(device_t, struct cfdata *, const int *, void *);
static int	aupsc_print(void *, const char *);

static void	aupsc_enable(void *, int);
static void	aupsc_disable(void *);
static void	aupsc_suspend(void *);


CFATTACH_DECL_NEW(aupsc, sizeof(struct aupsc_softc),
	aupsc_match, aupsc_attach, NULL, NULL);

static int
aupsc_match(device_t parent, struct cfdata *cf, void *aux)
{
	struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;

	if (strcmp(aa->aa_name, cf->cf_name) != 0)
		return 0;

	return 1;
}

static void
aupsc_attach(device_t parent, device_t self, void *aux)
{
	int i;
	uint32_t rv;
	struct aupsc_softc *sc = device_private(self);
	struct aubus_attach_args *aa = (struct aubus_attach_args *)aux;
	struct aupsc_attach_args pa;
	struct aupsc_controller ctrl;

	sc->sc_dev = self;
	sc->sc_bust = aa->aa_st;
	if (bus_space_map(sc->sc_bust, aa->aa_addr,
			AUPSC_SIZE, 0, &sc->sc_bush) != 0) {
		aprint_error(": unable to map device registers\n");
		return;
	}

	/* Initialize PSC_SEL register */
	sc->sc_pscsel = AUPSC_SEL_DISABLE;
	rv = bus_space_read_4(sc->sc_bust, sc->sc_bush, AUPSC_SEL);
	bus_space_write_4(sc->sc_bust, sc->sc_bush,
		AUPSC_SEL, (rv & AUPSC_SEL_PS(AUPSC_SEL_DISABLE)));
	bus_space_write_4(sc->sc_bust, sc->sc_bush,
		AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_DISABLE));

	aprint_normal(": Alchemy PSC\n");
	aprint_naive("\n");

	ctrl.psc_bust = sc->sc_bust;
	ctrl.psc_bush = sc->sc_bush;
	ctrl.psc_sel = &(sc->sc_pscsel);
	ctrl.psc_enable = aupsc_enable;
	ctrl.psc_disable = aupsc_disable;
	ctrl.psc_suspend = aupsc_suspend;
	pa.aupsc_ctrl = ctrl;
	pa.aupsc_addr = aa->aa_addr;
	pa.aupsc_irq = aa->aa_irq[0];

	for (i = 0 ; aupsc_protos[i].name != NULL ; i++) {
		struct aupsc_protocol_device p;
		uint32_t s;

		pa.aupsc_name = aupsc_protos[i].name;

		p.sc_dev = sc->sc_dev;
		p.sc_ctrl = ctrl;

		aupsc_enable(&p, aupsc_protos[i].protocol);
		s = bus_space_read_4(sc->sc_bust, sc->sc_bush, AUPSC_STAT);
		aupsc_disable(&p);

		if (s & AUPSC_STAT_SR) {
			config_found(self, &pa, aupsc_print,
			    CFARGS(.submatch = aupsc_submatch));
		}
        }
}

static int
aupsc_submatch(device_t parent, struct cfdata *cf, const int *ldesc, void *aux)
{

	return config_match(parent, cf, aux);
}

static int
aupsc_print(void *aux, const char *pnp)
{
	/*
	 * By default we don't want to print anything, because
	 * otherwise we see complaints about protocols that aren't
	 * configured on every port.  (E.g. each PSC can support 4
	 * protocols, but on a typical design, only one protocol can
	 * be configured per board.)
	 *
	 * Basically, this whole thing should be replaced with an
	 * indirect configuration mechanism.  Direct configuration
	 * doesn't make sense when we absolutely require kernel
	 * configuration to operate.
	 *
	 * Alternatively, a board-specific configuration mechanism
	 * could determine this, and provide direct configuration as
	 * we do for PCMCIA.
	 */

	return QUIET;
}

static void
aupsc_enable(void *arg, int proto)
{
	struct aupsc_protocol_device *sc = arg;
	int i;

	/* XXX: (TODO) setting clock AUPSC_SEL_CLK */
	switch (proto) {
	case AUPSC_SEL_SPI:
	case AUPSC_SEL_I2S:
	case AUPSC_SEL_AC97:
	case AUPSC_SEL_SMBUS:
		break;
	case AUPSC_SEL_DISABLE:
		aupsc_disable(arg);
		break;
	default:
		printf("%s: aupsc_enable: unsupported protocol.\n",
			device_xname(sc->sc_dev));
		return;
	}

	if (*(sc->sc_ctrl.psc_sel) != AUPSC_SEL_DISABLE) {
		printf("%s: aupsc_enable: please disable first.\n",
			device_xname(sc->sc_dev));
		return;
	}

	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
			AUPSC_SEL, AUPSC_SEL_PS(proto));
	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
			AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_ENABLE));

	/* wait up to a whole second, but test every 10us */
	for (i = 1000000; i; i -= 10) {
		if (bus_space_read_4(sc->sc_ctrl.psc_bust,
			sc->sc_ctrl.psc_bush, AUPSC_STAT) & AUPSC_STAT_SR)
			return;
		delay(10);
	}
}

static void
aupsc_disable(void *arg)
{
	struct aupsc_protocol_device *sc = arg;

	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
			AUPSC_SEL, AUPSC_SEL_PS(AUPSC_SEL_DISABLE));
	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
			AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_DISABLE));
	delay(1);
}

static void
aupsc_suspend(void *arg)
{
	struct aupsc_protocol_device *sc = arg;

	bus_space_write_4(sc->sc_ctrl.psc_bust, sc->sc_ctrl.psc_bush,
			AUPSC_CTRL, AUPSC_CTRL_ENA(AUPSC_CTRL_SUSPEND));
	delay(1);
}