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