blob: 790abadd816c31c84439ed65270b6a8df7867939 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * cpcihp_zt5550.c
3 *
4 * Intel/Ziatech ZT5550 CompactPCI Host Controller driver
5 *
6 * Copyright 2002 SOMA Networks, Inc.
7 * Copyright 2001 Intel San Luis Obispo
8 * Copyright 2000,2001 MontaVista Software Inc.
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 675 Mass Ave, Cambridge, MA 02139, USA.
29 *
30 * Send feedback to <scottm@somanetworks.com>
31 */
32
33#include <linux/config.h>
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/init.h>
37#include <linux/errno.h>
38#include <linux/pci.h>
39#include "cpci_hotplug.h"
40#include "cpcihp_zt5550.h"
41
42#define DRIVER_VERSION "0.2"
43#define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
44#define DRIVER_DESC "ZT5550 CompactPCI Hot Plug Driver"
45
46#define MY_NAME "cpcihp_zt5550"
47
48#define dbg(format, arg...) \
49 do { \
50 if(debug) \
51 printk (KERN_DEBUG "%s: " format "\n", \
52 MY_NAME , ## arg); \
53 } while(0)
54#define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME , ## arg)
55#define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME , ## arg)
56#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME , ## arg)
57
58/* local variables */
59static int debug;
60static int poll;
61static struct cpci_hp_controller_ops zt5550_hpc_ops;
62static struct cpci_hp_controller zt5550_hpc;
63
64/* Primary cPCI bus bridge device */
65static struct pci_dev *bus0_dev;
66static struct pci_bus *bus0;
67
68/* Host controller device */
69static struct pci_dev *hc_dev;
70
71/* Host controller register addresses */
72static void __iomem *hc_registers;
73static void __iomem *csr_hc_index;
74static void __iomem *csr_hc_data;
75static void __iomem *csr_int_status;
76static void __iomem *csr_int_mask;
77
78
79static int zt5550_hc_config(struct pci_dev *pdev)
80{
Bjorn Helgaasc8920f02005-09-28 15:15:16 -060081 int ret;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* Since we know that no boards exist with two HC chips, treat it as an error */
84 if(hc_dev) {
85 err("too many host controller devices?");
86 return -EBUSY;
87 }
Bjorn Helgaasc8920f02005-09-28 15:15:16 -060088
89 ret = pci_enable_device(pdev);
90 if(ret) {
91 err("cannot enable %s\n", pci_name(pdev));
92 return ret;
93 }
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 hc_dev = pdev;
96 dbg("hc_dev = %p", hc_dev);
97 dbg("pci resource start %lx", pci_resource_start(hc_dev, 1));
98 dbg("pci resource len %lx", pci_resource_len(hc_dev, 1));
99
100 if(!request_mem_region(pci_resource_start(hc_dev, 1),
101 pci_resource_len(hc_dev, 1), MY_NAME)) {
102 err("cannot reserve MMIO region");
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600103 ret = -ENOMEM;
104 goto exit_disable_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
107 hc_registers =
108 ioremap(pci_resource_start(hc_dev, 1), pci_resource_len(hc_dev, 1));
109 if(!hc_registers) {
110 err("cannot remap MMIO region %lx @ %lx",
111 pci_resource_len(hc_dev, 1), pci_resource_start(hc_dev, 1));
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600112 ret = -ENODEV;
113 goto exit_release_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
116 csr_hc_index = hc_registers + CSR_HCINDEX;
117 csr_hc_data = hc_registers + CSR_HCDATA;
118 csr_int_status = hc_registers + CSR_INTSTAT;
119 csr_int_mask = hc_registers + CSR_INTMASK;
120
121 /*
122 * Disable host control, fault and serial interrupts
123 */
124 dbg("disabling host control, fault and serial interrupts");
125 writeb((u8) HC_INT_MASK_REG, csr_hc_index);
126 writeb((u8) ALL_INDEXED_INTS_MASK, csr_hc_data);
127 dbg("disabled host control, fault and serial interrupts");
128
129 /*
130 * Disable timer0, timer1 and ENUM interrupts
131 */
132 dbg("disabling timer0, timer1 and ENUM interrupts");
133 writeb((u8) ALL_DIRECT_INTS_MASK, csr_int_mask);
134 dbg("disabled timer0, timer1 and ENUM interrupts");
135 return 0;
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600136
137exit_release_region:
138 release_mem_region(pci_resource_start(hc_dev, 1),
139 pci_resource_len(hc_dev, 1));
140exit_disable_device:
141 pci_disable_device(hc_dev);
142 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145static int zt5550_hc_cleanup(void)
146{
147 if(!hc_dev)
148 return -ENODEV;
149
150 iounmap(hc_registers);
151 release_mem_region(pci_resource_start(hc_dev, 1),
152 pci_resource_len(hc_dev, 1));
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600153 pci_disable_device(hc_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return 0;
155}
156
157static int zt5550_hc_query_enum(void)
158{
159 u8 value;
160
161 value = inb_p(ENUM_PORT);
162 return ((value & ENUM_MASK) == ENUM_MASK);
163}
164
165static int zt5550_hc_check_irq(void *dev_id)
166{
167 int ret;
168 u8 reg;
169
170 ret = 0;
171 if(dev_id == zt5550_hpc.dev_id) {
172 reg = readb(csr_int_status);
173 if(reg)
174 ret = 1;
175 }
176 return ret;
177}
178
179static int zt5550_hc_enable_irq(void)
180{
181 u8 reg;
182
183 if(hc_dev == NULL) {
184 return -ENODEV;
185 }
186 reg = readb(csr_int_mask);
187 reg = reg & ~ENUM_INT_MASK;
188 writeb(reg, csr_int_mask);
189 return 0;
190}
191
192static int zt5550_hc_disable_irq(void)
193{
194 u8 reg;
195
196 if(hc_dev == NULL) {
197 return -ENODEV;
198 }
199
200 reg = readb(csr_int_mask);
201 reg = reg | ENUM_INT_MASK;
202 writeb(reg, csr_int_mask);
203 return 0;
204}
205
206static int zt5550_hc_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
207{
208 int status;
209
210 status = zt5550_hc_config(pdev);
211 if(status != 0) {
212 return status;
213 }
214 dbg("returned from zt5550_hc_config");
215
216 memset(&zt5550_hpc, 0, sizeof (struct cpci_hp_controller));
217 zt5550_hpc_ops.query_enum = zt5550_hc_query_enum;
218 zt5550_hpc.ops = &zt5550_hpc_ops;
219 if(!poll) {
220 zt5550_hpc.irq = hc_dev->irq;
221 zt5550_hpc.irq_flags = SA_SHIRQ;
222 zt5550_hpc.dev_id = hc_dev;
223
224 zt5550_hpc_ops.enable_irq = zt5550_hc_enable_irq;
225 zt5550_hpc_ops.disable_irq = zt5550_hc_disable_irq;
226 zt5550_hpc_ops.check_irq = zt5550_hc_check_irq;
227 } else {
228 info("using ENUM# polling mode");
229 }
230
231 status = cpci_hp_register_controller(&zt5550_hpc);
232 if(status != 0) {
233 err("could not register cPCI hotplug controller");
234 goto init_hc_error;
235 }
236 dbg("registered controller");
237
238 /* Look for first device matching cPCI bus's bridge vendor and device IDs */
239 if(!(bus0_dev = pci_get_device(PCI_VENDOR_ID_DEC,
240 PCI_DEVICE_ID_DEC_21154, NULL))) {
241 status = -ENODEV;
242 goto init_register_error;
243 }
244 bus0 = bus0_dev->subordinate;
245 pci_dev_put(bus0_dev);
246
247 status = cpci_hp_register_bus(bus0, 0x0a, 0x0f);
248 if(status != 0) {
249 err("could not register cPCI hotplug bus");
250 goto init_register_error;
251 }
252 dbg("registered bus");
253
254 status = cpci_hp_start();
255 if(status != 0) {
256 err("could not started cPCI hotplug system");
257 cpci_hp_unregister_bus(bus0);
258 goto init_register_error;
259 }
260 dbg("started cpci hp system");
261
262 return 0;
263init_register_error:
264 cpci_hp_unregister_controller(&zt5550_hpc);
265init_hc_error:
266 err("status = %d", status);
267 zt5550_hc_cleanup();
268 return status;
269
270}
271
272static void __devexit zt5550_hc_remove_one(struct pci_dev *pdev)
273{
274 cpci_hp_stop();
275 cpci_hp_unregister_bus(bus0);
276 cpci_hp_unregister_controller(&zt5550_hpc);
277 zt5550_hc_cleanup();
278}
279
280
281static struct pci_device_id zt5550_hc_pci_tbl[] = {
282 { PCI_VENDOR_ID_ZIATECH, PCI_DEVICE_ID_ZIATECH_5550_HC, PCI_ANY_ID, PCI_ANY_ID, },
283 { 0, }
284};
285MODULE_DEVICE_TABLE(pci, zt5550_hc_pci_tbl);
286
287static struct pci_driver zt5550_hc_driver = {
288 .name = "zt5550_hc",
289 .id_table = zt5550_hc_pci_tbl,
290 .probe = zt5550_hc_init_one,
291 .remove = __devexit_p(zt5550_hc_remove_one),
292};
293
294static int __init zt5550_init(void)
295{
296 struct resource* r;
297
298 info(DRIVER_DESC " version: " DRIVER_VERSION);
299 r = request_region(ENUM_PORT, 1, "#ENUM hotswap signal register");
300 if(!r)
301 return -EBUSY;
302
303 return pci_register_driver(&zt5550_hc_driver);
304}
305
306static void __exit
307zt5550_exit(void)
308{
309 pci_unregister_driver(&zt5550_hc_driver);
310 release_region(ENUM_PORT, 1);
311}
312
313module_init(zt5550_init);
314module_exit(zt5550_exit);
315
316MODULE_AUTHOR(DRIVER_AUTHOR);
317MODULE_DESCRIPTION(DRIVER_DESC);
318MODULE_LICENSE("GPL");
319module_param(debug, bool, 0644);
320MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
321module_param(poll, bool, 0644);
322MODULE_PARM_DESC(poll, "#ENUM polling mode enabled or not");