summaryrefslogtreecommitdiff
path: root/sys/dev/pci/musycc_obsd.c
blob: 25a58d88602f3a215644b3117b8d3edeec7ef0ca (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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*	$OpenBSD: musycc_obsd.c,v 1.10 2011/04/03 15:36:03 jasper Exp $ */

/*
 * Copyright (c) 2004,2005  Internet Business Solutions AG, Zurich, Switzerland
 * Written by: Claudio Jeker <jeker@accoom.net>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/types.h>

#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/socket.h>

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

#include <net/if.h>
#include <net/if_media.h>
#include <net/if_sppp.h>

#include <dev/pci/musyccvar.h>
#include <dev/pci/musyccreg.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcidevs.h>

int	musycc_match(struct device *, void *, void *);
void	musycc_softc_attach(struct device *, struct device *, void *);
void	musycc_ebus_attach(struct device *, struct musycc_softc *,
	    struct pci_attach_args *);
int	musycc_ebus_print(void *, const char *);

struct cfattach musycc_ca = {
	sizeof(struct musycc_softc), musycc_match, musycc_softc_attach
};

struct cfdriver musycc_cd = {
	NULL, "musycc", DV_DULL
};

SLIST_HEAD(, musycc_softc) msc_list = SLIST_HEAD_INITIALIZER(msc_list);

const struct pci_matchid musycc_pci_devices[] = {
	{ PCI_VENDOR_CONEXANT, PCI_PRODUCT_CONEXANT_MUSYCC8478 },
	{ PCI_VENDOR_CONEXANT, PCI_PRODUCT_CONEXANT_MUSYCC8474 },
	{ PCI_VENDOR_CONEXANT, PCI_PRODUCT_CONEXANT_MUSYCC8472 },
	{ PCI_VENDOR_CONEXANT, PCI_PRODUCT_CONEXANT_MUSYCC8471 }
};

int
musycc_match(struct device *parent, void *match, void *aux)
{
	return (pci_matchbyid((struct pci_attach_args *)aux, musycc_pci_devices,
	    nitems(musycc_pci_devices)));
}

void
musycc_softc_attach(struct device *parent, struct device *self, void *aux)
{
	struct musycc_softc	*sc = (struct musycc_softc *)self;
	struct pci_attach_args	*pa = aux;
	pci_chipset_tag_t	 pc = pa->pa_pc;
	pci_intr_handle_t	 ih;
	const char		*intrstr = NULL;

	if (pci_mapreg_map(pa, MUSYCC_PCI_BAR,
	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
	    &sc->mc_st, &sc->mc_sh, NULL, &sc->mc_iosize, 0)) {
		printf(": can't map mem space\n");
		return;
	}
	sc->mc_dmat = pa->pa_dmat;

	switch (PCI_PRODUCT(pa->pa_id)) {
	case PCI_PRODUCT_CONEXANT_MUSYCC8478:
		sc->mc_ngroups = 8;
		sc->mc_nports = 8;
		break;
	case PCI_PRODUCT_CONEXANT_MUSYCC8474:
		sc->mc_ngroups = 4;
		sc->mc_nports = 4;
		break;
	case PCI_PRODUCT_CONEXANT_MUSYCC8472:
		sc->mc_ngroups = 2;
		sc->mc_nports = 2;
		break;
	case PCI_PRODUCT_CONEXANT_MUSYCC8471:
		sc->mc_ngroups = 1;
		sc->mc_nports = 1;
		break;
	}

	if (pa->pa_function == 1)
		return (musycc_ebus_attach(parent, sc, pa));

	sc->bus = parent->dv_unit;
	sc->device = pa->pa_device;
	SLIST_INSERT_HEAD(&msc_list, sc, list);

	/*
	 * Allocate our interrupt.
	 */
	if (pci_intr_map(pa, &ih)) {
		printf(": couldn't map interrupt\n");
		bus_space_unmap(sc->mc_st, sc->mc_sh, sc->mc_iosize);
		return;
	}

	intrstr = pci_intr_string(pc, ih);
	sc->mc_ih = pci_intr_establish(pc, ih, IPL_NET, musycc_intr, sc,
	    self->dv_xname);
	if (sc->mc_ih == NULL) {
		printf(": couldn't establish interrupt");
		if (intrstr != NULL)
			printf(" at %s", intrstr);
		printf("\n");
		bus_space_unmap(sc->mc_st, sc->mc_sh, sc->mc_iosize);
		return;
	}

	printf(": %s\n", intrstr);

	/* soft reset device */
	bus_space_write_4(sc->mc_st, sc->mc_sh, MUSYCC_SERREQ(0),
	    MUSYCC_SREQ_SET(1));
	bus_space_barrier(sc->mc_st, sc->mc_sh, MUSYCC_SERREQ(0),
	    sizeof(u_int32_t), BUS_SPACE_BARRIER_WRITE);

	/*
	 * preload global configuration: set EBUS to sane defaults
	 * so that the ROM access will work.
	 * intel mode, elapse = 3, blapse = 3, alapse = 3, disable INTB
	 */
	sc->mc_global_conf = MUSYCC_CONF_MPUSEL | MUSYCC_CONF_ECKEN |
	    MUSYCC_CONF_ELAPSE_SET(3) | MUSYCC_CONF_ALAPSE_SET(3) |
	    MUSYCC_CONF_BLAPSE_SET(3) | MUSYCC_CONF_INTB;

	/* Dual Address Cycle Base Pointer */
	bus_space_write_4(sc->mc_st, sc->mc_sh, MUSYCC_DACB_PTR, 0);
	/* Global Configuration Descriptor */
	bus_space_write_4(sc->mc_st, sc->mc_sh, MUSYCC_GLOBALCONF,
	    sc->mc_global_conf);

	return;
}

void
musycc_ebus_attach(struct device *parent, struct musycc_softc *esc,
    struct pci_attach_args *pa)
{
	struct ebus_dev			 rom;
	struct musycc_attach_args	 ma;
	struct musycc_softc		*sc;
	pci_chipset_tag_t		 pc = pa->pa_pc;
#if 0
	pci_intr_handle_t		 ih;
	const char			*intrstr = NULL;
#endif
	struct musycc_rom		 baseconf;
	struct musycc_rom_framer	 framerconf;
	bus_size_t			 offset;
	int				 i;

	/* find HDLC controller softc ... */
	SLIST_FOREACH(sc, &msc_list, list)
		if (sc->bus == parent->dv_unit && sc->device == pa->pa_device)
			break;
	if (sc == NULL) {
		printf(": corresponding hdlc controller not found\n");
		return;
	}

	/* ... and link them together */
	esc->mc_other = sc;
	sc->mc_other = esc;

#if 0
	/*
	 * Allocate our interrupt.
	 */
	if (pci_intr_map(pa, &ih)) {
		printf(": couldn't map interrupt\n");
		goto failed;
	}

	intrstr = pci_intr_string(pc, ih);
	esc->mc_ih = pci_intr_establish(pc, ih, IPL_NET, ebus_intr, esc,
	    esc->mc_dev.dv_xname);
	if (esc->mc_ih == NULL) {
		printf(": couldn't establish interrupt");
		if (intrstr != NULL)
			printf(" at %s", intrstr);
		printf("\n");
		goto failed;
	}

	/* XXX this printf should actually move to the end of the function */
	printf(": %s\n", intrstr);
#endif

	if (ebus_attach_device(&rom, sc, 0, 0x400) != 0) {
		printf(": failed to map rom @ %05p\n", 0);
		goto failed;
	}

	offset = 0;
	ebus_read_buf(&rom, offset, &baseconf, sizeof(baseconf));
	offset += sizeof(baseconf);

	if (baseconf.magic != MUSYCC_ROM_MAGIC) {
		printf(": bad rom\n");
		goto failed;
	}

	/* Do generic parts of attach. */
	if (musycc_attach_common(sc, baseconf.portmap, baseconf.portmode))
		goto failed;

	/* map and reset leds */
	/* (15 * 0x4000) << 2 */
	esc->mc_ledbase = ntohl(baseconf.ledbase) << 2;
	esc->mc_ledmask = baseconf.ledmask;
	esc->mc_ledstate = 0;
	bus_space_write_1(esc->mc_st, esc->mc_sh, esc->mc_ledbase, 0);

	printf("\n");

	for (i = 0; i < baseconf.numframer; i++) {
		if (offset >= 0x400) {
			printf("%s: bad rom\n", sc->mc_dev.dv_xname);
			goto failed;
		}
		ebus_read_buf(&rom, offset, &framerconf, sizeof(framerconf));
		offset += sizeof(framerconf);

		strlcpy(ma.ma_product, baseconf.product, sizeof(ma.ma_product));
		ma.ma_base = ntohl(framerconf.base);
		ma.ma_size = ntohl(framerconf.size);
		ma.ma_type = ntohl(framerconf.type);
		ma.ma_gnum = framerconf.gnum;
		ma.ma_port = framerconf.port;
		ma.ma_flags = framerconf.flags;
		ma.ma_slot = framerconf.slot;

		(void)config_found(&sc->mc_dev, &ma, musycc_ebus_print);
	}

	return;
failed:
	/* Failed! */
	pci_intr_disestablish(pc, sc->mc_ih);
	if (esc->mc_ih != NULL)
		pci_intr_disestablish(pc, esc->mc_ih);
	bus_space_unmap(sc->mc_st, sc->mc_sh, sc->mc_iosize);
	bus_space_unmap(esc->mc_st, esc->mc_sh, esc->mc_iosize);
	return;
}

int
musycc_ebus_print(void *aux, const char *pnp)
{
	struct musycc_attach_args *ma = aux;

	if (pnp)
		printf("framer at %s port %d slot %c",
		    pnp, ma->ma_port, ma->ma_slot);
	else
		printf(" port %d slot %c", ma->ma_port, ma->ma_slot);
	return (UNCONF);
}