5313364ccda24426bb5073ef3794b0e5ef41d88f
[dragonfly.git] / sys / dev / serial / cy / cy_pci.c
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: src/sys/pci/cy_pci.c,v 1.17.2.1 2002/03/17 04:14:18 bde Exp $
28  * $DragonFly: src/sys/dev/serial/cy/cy_pci.c,v 1.10 2007/04/12 18:35:08 swildner Exp $
29  */
30
31 /*
32  * Cyclades Y PCI serial interface driver
33  */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41
42 #include <bus/pci/pcivar.h>
43
44 #include "cy_pcireg.h"
45
46 static const char *cy_probe             (pcici_t, pcidi_t);
47 static void cy_attach           (pcici_t, int);
48
49 extern int cyattach_common(void *, int); /* Not exactly correct */
50 extern void cyintr(int);
51
52 static u_long cy_count;
53
54 static struct pci_device cy_device = {
55         "cy",
56         cy_probe,
57         cy_attach,
58         &cy_count,
59         NULL
60 };
61 COMPAT_PCI_DRIVER(cy_pci, cy_device);
62
63 static const char *
64 cy_probe(pcici_t config_id, pcidi_t device_id)
65 {
66         device_id &= ~0x00060000;
67         if (device_id == 0x0100120e || device_id == 0x0101120e)
68                 return ("Cyclades Cyclom-Y Serial Adapter");
69         return (NULL);
70 }
71
72 static void
73 cy_attach(pcici_t config_id, int unit)
74 {
75         vm_offset_t paddr;
76         void *vaddr;
77         u_int32_t ioport;
78         int adapter;
79         u_char plx_ver;
80
81         ioport = (u_int32_t) pci_conf_read(config_id, CY_PCI_BASE_ADDR1) & ~0x3;
82         paddr = pci_conf_read(config_id, CY_PCI_BASE_ADDR2) & ~0xf;
83 #if 0
84         if (!pci_map_mem(config_id, CY_PCI_BASE_ADDR2, &vaddr, &paddr)) {
85                 kprintf("cy%d: couldn't map shared memory\n", unit);
86                 return;
87         };
88 #endif
89         vaddr = pmap_mapdev(paddr, 0x4000);
90
91         adapter = cyattach_common(vaddr, 1);
92         if (adapter < 0) {
93                 /*
94                  * No ports found. Release resources and punt.
95                  */
96                 kprintf("cy%d: no ports found!\n", unit);
97                 goto fail;
98         }
99
100         /*
101          * Allocate our interrupt.
102          * XXX  Using the ISA interrupt handler directly is a bit of a violation
103          *      since it doesn't actually take the same argument. For PCI, the
104          *      argument is a void * token, but for ISA it is a unit. Since
105          *      there is no overlap in PCI/ISA unit numbers for this driver, and
106          *      since the ISA driver must handle the interrupt anyway, we use
107          *      the unit number as the token even for PCI.
108          */
109         if (!pci_map_int_right(config_id, (pci_inthand_t *)cyintr,
110                                (void *)adapter, 0)) {
111                 kprintf("cy%d: couldn't map interrupt\n", unit);
112                 goto fail;
113         }
114
115         /*
116          * Enable the "local" interrupt input to generate a
117          * PCI interrupt.
118          */
119         plx_ver = *((u_char *)vaddr + PLX_VER) & 0x0f;
120         switch (plx_ver) {
121         case PLX_9050:
122                 outw(ioport + CY_PLX_9050_ICS,
123                     CY_PLX_9050_ICS_IENABLE | CY_PLX_9050_ICS_LOCAL_IENABLE |
124                     CY_PLX_9050_ICS_LOCAL_IPOLARITY);
125                 break;
126         case PLX_9060:
127         case PLX_9080:
128         default:                /* Old board, use PLX_9060 values. */
129                 outw(ioport + CY_PLX_9060_ICS,
130                     inw(ioport + CY_PLX_9060_ICS) | CY_PLX_9060_ICS_IENABLE |
131                     CY_PLX_9060_ICS_LOCAL_IENABLE);
132                 break;
133         }
134
135         return;
136
137 fail:
138         /* XXX should release any allocated virtual memory */
139         return;
140 }