cy_pci.c revision 90101
1/*
2 * Copyright (c) 1996, David Greenman
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice unmodified, this list of conditions, and the following
10 *    disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/cy/cy_pci.c 90101 2002-02-02 02:05:44Z bde $
28 */
29
30/*
31 * Cyclades Y PCI serial interface driver
32 */
33
34#include "opt_cy_pci_fastintr.h"
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/bus.h>
40
41#include <machine/bus.h>
42#include <sys/rman.h>
43#include <machine/resource.h>
44
45#include <vm/vm.h>
46#include <vm/pmap.h>
47
48#include <dev/pci/pcivar.h>
49
50#include <pci/cy_pcireg.h>
51
52extern int cyattach_common(void *, int); /* Not exactly correct */
53extern void cyintr(int);
54
55static int	cy_pci_attach __P((device_t dev));
56static int	cy_pci_probe __P((device_t dev));
57
58static device_method_t cy_pci_methods[] = {
59	/* Device interface. */
60	DEVMETHOD(device_probe,		cy_pci_probe),
61	DEVMETHOD(device_attach,	cy_pci_attach),
62
63	{ 0, 0 }
64};
65
66static driver_t cy_pci_driver = {
67	"cy",
68	cy_pci_methods,
69	0,
70};
71
72static devclass_t	cy_devclass;
73
74DRIVER_MODULE(cy, pci, cy_pci_driver, cy_devclass, 0, 0);
75
76static int
77cy_pci_probe(dev)
78	device_t dev;
79{
80	u_int32_t device_id;
81
82	device_id = pci_get_devid(dev);
83	device_id &= ~0x00060000;
84	if (device_id != 0x0100120e && device_id != 0x0101120e)
85		return (ENXIO);
86	device_set_desc(dev, "Cyclades Cyclom-Y Serial Adapter");
87	return (0);
88}
89
90static int
91cy_pci_attach(dev)
92	device_t dev;
93{
94	struct resource *ioport_res, *irq_res, *mem_res;
95	void *irq_cookie, *vaddr;
96	u_int32_t ioport;
97	int adapter, irq_setup, ioport_rid, irq_rid, mem_rid;
98	u_char plx_ver;
99
100	ioport_res = NULL;
101	irq_res = NULL;
102	mem_res = NULL;
103
104	ioport_rid = CY_PCI_BASE_ADDR1;
105	ioport_res = bus_alloc_resource(dev, SYS_RES_IOPORT, &ioport_rid,
106	    0ul, ~0ul, 0ul, RF_ACTIVE);
107	if (ioport_res == NULL) {
108		device_printf(dev, "ioport resource allocation failed\n");
109		goto fail;
110	}
111	ioport = rman_get_start(ioport_res);
112
113	mem_rid = CY_PCI_BASE_ADDR2;
114	mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &mem_rid,
115	     0ul, ~0ul, 0ul, RF_ACTIVE);
116	if (mem_res == NULL) {
117		device_printf(dev, "memory resource allocation failed\n");
118		goto fail;
119	}
120	vaddr = rman_get_virtual(mem_res);
121
122	adapter = cyattach_common(vaddr, 1);
123	if (adapter < 0) {
124		device_printf(dev, "no ports found!\n");
125		goto fail;
126	}
127
128	/*
129	 * Allocate our interrupt.
130	 * XXX	Using the ISA interrupt handler directly is a bit of a violation
131	 *	since it doesn't actually take the same argument. For PCI, the
132	 *	argument is a void * token, but for ISA it is a unit. Since
133	 *	there is no overlap in PCI/ISA unit numbers for this driver, and
134	 *	since the ISA driver must handle the interrupt anyway, we use
135	 *	the unit number as the token even for PCI.
136	 */
137	irq_rid = 0;
138	irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &irq_rid, 0ul, ~0ul, 0ul,
139	    RF_SHAREABLE | RF_ACTIVE);
140	if (irq_res == NULL) {
141		device_printf(dev, "interrupt resource allocation failed\n");
142		goto fail;
143	}
144#ifdef CY_PCI_FASTINTR
145	irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY | INTR_FAST,
146	    (driver_intr_t *)cyintr, (void *)adapter, &irq_cookie);
147#else
148	irq_setup = ENXIO;
149#endif
150	if (irq_setup != 0)
151		irq_setup = bus_setup_intr(dev, irq_res, INTR_TYPE_TTY,
152		    (driver_intr_t *)cyintr, (void *)adapter, &irq_cookie);
153	if (irq_setup != 0) {
154		device_printf(dev, "interrupt setup failed\n");
155		goto fail;
156	}
157
158	/*
159	 * Enable the "local" interrupt input to generate a
160	 * PCI interrupt.
161	 */
162	plx_ver = *((u_char *)vaddr + PLX_VER) & 0x0f;
163	switch (plx_ver) {
164	case PLX_9050:
165		outw(ioport + CY_PLX_9050_ICS,
166		    inw(ioport + CY_PLX_9050_ICS) | CY_PLX_9050_ICS_IENABLE |
167		    CY_PLX_9050_ICS_LOCAL_IENABLE);
168		break;
169	case PLX_9060:
170	case PLX_9080:
171	default:		/* Old board, use PLX_9060 values. */
172		outw(ioport + CY_PLX_9060_ICS,
173		    inw(ioport + CY_PLX_9060_ICS) | CY_PLX_9060_ICS_IENABLE |
174		    CY_PLX_9060_ICS_LOCAL_IENABLE);
175		break;
176	}
177
178	return (0);
179
180fail:
181	if (ioport_res != NULL)
182		bus_release_resource(dev, SYS_RES_IOPORT, ioport_rid,
183		    ioport_res);
184	if (irq_res != NULL)
185		bus_release_resource(dev, SYS_RES_IRQ, irq_rid, irq_res);
186	if (mem_res != NULL)
187		bus_release_resource(dev, SYS_RES_MEMORY, mem_rid, mem_res);
188	return (ENXIO);
189}
190