summaryrefslogtreecommitdiff
path: root/sys/dev/pcmcia/pcmcom.c
blob: 941bccd57b20e77a30d0d86a61e5834f5514e04a (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*	$NetBSD: pcmcom.c,v 1.14 2003/01/01 00:10:24 thorpej Exp $	*/

/*-
 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by RedBack Networks, Inc.
 *
 * 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. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the NetBSD
 *	Foundation, Inc. and its contributors.
 * 4. Neither the name of The NetBSD Foundation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``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 FOUNDATION OR CONTRIBUTORS
 * 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.
 */

/*
 * Device driver for multi-port PCMCIA serial cards, written by
 * Jason R. Thorpe for RedBack Networks, Inc.
 *
 * Most of these cards are simply multiple UARTs sharing a single interrupt
 * line, and rely on the fact that PCMCIA level-triggered interrupts can
 * be shared.  There are no special interrupt registers on them, as there
 * are on most ISA multi-port serial cards.
 *
 * If there are other cards that have interrupt registers, they should not
 * be glued into this driver.  Rather, separate drivers should be written
 * for those devices, as we have in the ISA multi-port serial card case.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: pcmcom.c,v 1.14 2003/01/01 00:10:24 thorpej Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h> 
#include <sys/termios.h>
#include <sys/malloc.h>

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

#include <dev/ic/comreg.h>
#include <dev/ic/comvar.h> 

#include <dev/pcmcia/pcmciavar.h>
#include <dev/pcmcia/pcmciareg.h>
#include <dev/pcmcia/pcmciadevs.h>

#include "com.h"
#include "pcmcom.h"

#include "locators.h"

struct pcmcom_slave_info {
	struct pcmcia_io_handle psi_pcioh;	/* PCMCIA i/o space info */
	int psi_io_window;			/* our i/o window */
	struct device *psi_child;		/* child's device */
};

struct pcmcom_softc {
	struct device sc_dev;			/* generic device glue */

	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
	void *sc_ih;				/* interrupt handle */
	int sc_enabled_count;			/* enabled count */

	struct pcmcom_slave_info *sc_slaves;	/* slave info */
	int sc_nslaves;				/* slave count */
};

struct pcmcom_attach_args {
	bus_space_tag_t pca_iot;		/* I/O tag */
	bus_space_handle_t pca_ioh;		/* I/O handle */
	int pca_slave;				/* slave # */
};

int	pcmcom_match __P((struct device *, struct cfdata *, void *));
void	pcmcom_attach __P((struct device *, struct device *, void *));
int	pcmcom_detach __P((struct device *, int));
int	pcmcom_activate __P((struct device *, enum devact));

CFATTACH_DECL(pcmcom, sizeof(struct pcmcom_softc),
    pcmcom_match, pcmcom_attach, pcmcom_detach, pcmcom_activate);

const struct pcmcom_product {
	struct pcmcia_product pp_product;
	int		pp_nslaves;		/* number of slaves */
} pcmcom_products[] = {
	{ { PCMCIA_STR_SOCKET_DUAL_RS232,	PCMCIA_VENDOR_SOCKET,
	    PCMCIA_PRODUCT_SOCKET_DUAL_RS232,	0 },
	  2 },

	{ { NULL } }
};

int	pcmcom_print __P((void *, const char *));
int	pcmcom_submatch __P((struct device *, struct cfdata *, void *));

int	pcmcom_check_cfe __P((struct pcmcom_softc *,
	    struct pcmcia_config_entry *));
void	pcmcom_attach_slave __P((struct pcmcom_softc *, int));

int	pcmcom_enable __P((struct pcmcom_softc *));
void	pcmcom_disable __P((struct pcmcom_softc *));

int	pcmcom_intr __P((void *));

int
pcmcom_match(parent, cf, aux)
	struct device *parent;
	struct cfdata *cf;
	void *aux;
{
	struct pcmcia_attach_args *pa = aux;

	if (pcmcia_product_lookup(pa,
	    (const struct pcmcia_product *)pcmcom_products,
	    sizeof pcmcom_products[0], NULL) != NULL)
		return (10);	/* beat com_pcmcia */
	return (0);
}

void
pcmcom_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct pcmcom_softc *sc = (struct pcmcom_softc *)self;
	struct pcmcia_attach_args *pa = aux;
	struct pcmcia_config_entry *cfe;
	const struct pcmcom_product *pp;
	size_t size;
	int i;

	sc->sc_pf = pa->pf;

	pp = (const struct pcmcom_product *)pcmcia_product_lookup(pa,
            (const struct pcmcia_product *)pcmcom_products,
            sizeof pcmcom_products[0], NULL);
	if (pp == NULL) {
		printf("\n");
		panic("pcmcom_attach: impossible");
	}

	printf(": %s\n", pp->pp_product.pp_name);

	/* Allocate the slave info. */
	sc->sc_nslaves = pp->pp_nslaves;
	size = sizeof(struct pcmcom_slave_info) * sc->sc_nslaves;
	sc->sc_slaves = malloc(size, M_DEVBUF, M_NOWAIT|M_ZERO);
	if (sc->sc_slaves == NULL) {
		printf("%s: unable to allocate slave info\n",
		    sc->sc_dev.dv_xname);
		return;
	}

	/*
	 * The address decoders on these cards are stupid.  They decode
	 * (usually) 10 bits of address, so you need to allocate the
	 * regions they request in order for the card to differentiate
	 * between serial ports.
	 */
	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
		if (pcmcom_check_cfe(sc, cfe)) {
			/* Found one! */
			break;
		}
	}
	if (cfe == NULL) {
		printf("%s: unable to find a suitable config table entry\n",
		    sc->sc_dev.dv_xname);
		return;
	}

	/* Enable the card. */
	pcmcia_function_init(pa->pf, cfe);
	if (pcmcia_function_enable(sc->sc_pf)) {
		printf("%s: function enable failed\n", sc->sc_dev.dv_xname);
		return;
	}

	sc->sc_enabled_count = 1;

	/* Attach the children. */
	for (i = 0; i < sc->sc_nslaves; i++)
		pcmcom_attach_slave(sc, i);

	sc->sc_enabled_count = 0;
	pcmcia_function_disable(sc->sc_pf);
}

int
pcmcom_check_cfe(sc, cfe)
	struct pcmcom_softc *sc;
	struct pcmcia_config_entry *cfe;
{
	struct pcmcom_slave_info *psi;
	int slave;

	/* We need to have the same number of I/O spaces as we do slaves. */
	if (cfe->num_iospace != sc->sc_nslaves)
		return (0);

	for (slave = 0; slave < sc->sc_nslaves; slave++) {
		psi = &sc->sc_slaves[slave];
		if (pcmcia_io_alloc(sc->sc_pf,
		    cfe->iospace[slave].start,
		    cfe->iospace[slave].length,
		    cfe->iospace[slave].length,
		    &psi->psi_pcioh))
			goto release;
	}

	/* If we got here, we were able to allocate space for all slaves! */
	return (1);

 release:
	/* Release the i/o spaces we've allocated. */
	for (slave--; slave >= 0; slave--) {
		psi = &sc->sc_slaves[slave];
		pcmcia_io_free(sc->sc_pf, &psi->psi_pcioh);
	}
	return (0);
}

void
pcmcom_attach_slave(sc, slave)
	struct pcmcom_softc *sc;
	int slave;
{
	struct pcmcom_slave_info *psi = &sc->sc_slaves[slave];
	struct pcmcom_attach_args pca;

	printf("%s: slave %d", sc->sc_dev.dv_xname, slave);

	if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO8, 0, psi->psi_pcioh.size,
	    &psi->psi_pcioh, &psi->psi_io_window)) {
		printf(": can't map i/o space\n");
		return;
	}

	printf("\n");

	pca.pca_iot = psi->psi_pcioh.iot;
	pca.pca_ioh = psi->psi_pcioh.ioh;
	pca.pca_slave = slave;

	psi->psi_child = config_found_sm(&sc->sc_dev, &pca, pcmcom_print,
	    pcmcom_submatch);
}

int
pcmcom_detach(self, flags)
	struct device *self;
	int flags;
{
	struct pcmcom_softc *sc = (struct pcmcom_softc *)self;
	struct pcmcom_slave_info *psi;
	int slave, error;

	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
		psi = &sc->sc_slaves[slave];
		if (psi->psi_child == NULL)
			continue;

		/* Detach the child. */
		if ((error = config_detach(psi->psi_child, flags)) != 0)
			return (error);
		psi->psi_child = NULL;

		/* Unmap the i/o window. */
		pcmcia_io_unmap(sc->sc_pf, psi->psi_io_window);

		/* Free the i/o space. */
		pcmcia_io_free(sc->sc_pf, &psi->psi_pcioh);
	}
	return (0);
}

int
pcmcom_activate(self, act)
	struct device *self;
	enum devact act;
{
	struct pcmcom_softc *sc = (struct pcmcom_softc *)self;
	struct pcmcom_slave_info *psi;
	int slave, error = 0, s;

	s = splserial();
	switch (act) {
	case DVACT_ACTIVATE:
		error = EOPNOTSUPP;
		break;

	case DVACT_DEACTIVATE:
		for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
			psi = &sc->sc_slaves[slave];
			if (psi->psi_child == NULL)
				continue;

			/*
			 * Deactivate the child.  Doing so will cause our
			 * own enabled count to drop to 0, once all children
			 * are deactivated.
			 */
			if ((error = config_deactivate(psi->psi_child)) != 0)
				break;
		}
		break;
	}
	splx(s);
	return (error);
}

int
pcmcom_print(aux, pnp)
	void *aux;
	const char *pnp;
{
	struct pcmcom_attach_args *pca = aux;

	/* only com's can attach to pcmcom's; easy... */
	if (pnp)
		aprint_normal("com at %s", pnp);

	aprint_normal(" slave %d", pca->pca_slave);

	return (UNCONF);
}

int
pcmcom_submatch(parent, cf, aux)
	struct device *parent;
	struct cfdata *cf;
	void *aux;
{
	struct pcmcom_attach_args *pca = aux;

	if (cf->cf_loc[PCMCOMCF_SLAVE] != pca->pca_slave &&
	    cf->cf_loc[PCMCOMCF_SLAVE] != PCMCOMCF_SLAVE_DEFAULT)
		return (0);

	return (config_match(parent, cf, aux));
}

int
pcmcom_intr(arg)
	void *arg;
{
#if NCOM > 0
	struct pcmcom_softc *sc = arg;
	int i, rval = 0;

	if (sc->sc_enabled_count == 0)
		return (0);

	for (i = 0; i < sc->sc_nslaves; i++) {
		if (sc->sc_slaves[i].psi_child != NULL)
			rval |= comintr(sc->sc_slaves[i].psi_child);
	}

	return (rval);
#else
	return (0);
#endif /* NCOM > 0 */
}

int
pcmcom_enable(sc)
	struct pcmcom_softc *sc;
{

	sc->sc_enabled_count++;
	if (sc->sc_enabled_count > 1)
		return (0);

	/* Establish the interrupt. */
	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
	    pcmcom_intr, sc);
	if (sc->sc_ih == NULL) {
		printf("%s: couldn't establish interrupt\n",
		    sc->sc_dev.dv_xname);
		return (1);
	}

	return (pcmcia_function_enable(sc->sc_pf));
}

void
pcmcom_disable(sc)
	struct pcmcom_softc *sc;
{

	sc->sc_enabled_count--;
	if (sc->sc_enabled_count != 0)
		return;

	pcmcia_function_disable(sc->sc_pf);
	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
}

/****** Here begins the com attachment code. ******/

#if NCOM_PCMCOM > 0
int	com_pcmcom_match __P((struct device *, struct cfdata *, void *));
void	com_pcmcom_attach __P((struct device *, struct device *, void *));

/* No pcmcom-specific goo in the softc; it's all in the parent. */
CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
    com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);

int	com_pcmcom_enable __P((struct com_softc *));
void	com_pcmcom_disable __P((struct com_softc *));

int
com_pcmcom_match(parent, cf, aux)
	struct device *parent;
	struct cfdata *cf;
	void *aux;
{

	/* Device is always present. */
	return (1);
}

void
com_pcmcom_attach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	struct com_softc *sc = (struct com_softc *)self;
	struct pcmcom_attach_args *pca = aux;

	sc->sc_iot = pca->pca_iot;
	sc->sc_ioh = pca->pca_ioh;

	sc->enabled = 1;

	sc->sc_iobase = -1;
	sc->sc_frequency = COM_FREQ;

	sc->enable = com_pcmcom_enable;
	sc->disable = com_pcmcom_disable;

	com_attach_subr(sc);

	sc->enabled = 0;
}

int
com_pcmcom_enable(sc)
	struct com_softc *sc;
{

	return (pcmcom_enable((struct pcmcom_softc *)sc->sc_dev.dv_parent));
}

void
com_pcmcom_disable(sc)
	struct com_softc *sc;
{

	pcmcom_disable((struct pcmcom_softc *)sc->sc_dev.dv_parent);
}
#endif /* NCOM_PCMCOM > 0 */