blob: 5f49c3fd736a793c4f153f01158fec6037dcff2b [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,
Bjorn Helgaasf7625982013-11-14 11:28:18 -070016 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/module.h>
34#include <linux/moduleparam.h>
35#include <linux/init.h>
36#include <linux/errno.h>
37#include <linux/pci.h>
Thomas Gleixner6b4486e2006-07-01 19:29:41 -070038#include <linux/interrupt.h>
39#include <linux/signal.h> /* IRQF_SHARED */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "cpci_hotplug.h"
41#include "cpcihp_zt5550.h"
42
43#define DRIVER_VERSION "0.2"
44#define DRIVER_AUTHOR "Scott Murray <scottm@somanetworks.com>"
45#define DRIVER_DESC "ZT5550 CompactPCI Hot Plug Driver"
46
47#define MY_NAME "cpcihp_zt5550"
48
49#define dbg(format, arg...) \
50 do { \
Bjorn Helgaasf7625982013-11-14 11:28:18 -070051 if (debug) \
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080052 printk(KERN_DEBUG "%s: " format "\n", \
53 MY_NAME, ## arg); \
Quentin Lambert382a9c92014-09-07 20:02:04 +020054 } while (0)
Bogicevic Sasaff3ce482015-12-27 13:21:11 -080055#define err(format, arg...) printk(KERN_ERR "%s: " format "\n", MY_NAME, ## arg)
56#define info(format, arg...) printk(KERN_INFO "%s: " format "\n", MY_NAME, ## arg)
57#define warn(format, arg...) printk(KERN_WARNING "%s: " format "\n", MY_NAME, ## arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/* local variables */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103060static bool debug;
61static bool poll;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static struct cpci_hp_controller_ops zt5550_hpc_ops;
63static struct cpci_hp_controller zt5550_hpc;
64
65/* Primary cPCI bus bridge device */
66static struct pci_dev *bus0_dev;
67static struct pci_bus *bus0;
68
69/* Host controller device */
70static struct pci_dev *hc_dev;
71
72/* Host controller register addresses */
73static void __iomem *hc_registers;
74static void __iomem *csr_hc_index;
75static void __iomem *csr_hc_data;
76static void __iomem *csr_int_status;
77static void __iomem *csr_int_mask;
78
79
80static int zt5550_hc_config(struct pci_dev *pdev)
81{
Bjorn Helgaasc8920f02005-09-28 15:15:16 -060082 int ret;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 /* Since we know that no boards exist with two HC chips, treat it as an error */
Quentin Lambert382a9c92014-09-07 20:02:04 +020085 if (hc_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 err("too many host controller devices?");
87 return -EBUSY;
88 }
Bjorn Helgaasc8920f02005-09-28 15:15:16 -060089
90 ret = pci_enable_device(pdev);
Quentin Lambert382a9c92014-09-07 20:02:04 +020091 if (ret) {
Bjorn Helgaasc8920f02005-09-28 15:15:16 -060092 err("cannot enable %s\n", pci_name(pdev));
93 return ret;
94 }
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 hc_dev = pdev;
97 dbg("hc_dev = %p", hc_dev);
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -070098 dbg("pci resource start %llx", (unsigned long long)pci_resource_start(hc_dev, 1));
99 dbg("pci resource len %llx", (unsigned long long)pci_resource_len(hc_dev, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Quentin Lambert382a9c92014-09-07 20:02:04 +0200101 if (!request_mem_region(pci_resource_start(hc_dev, 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 pci_resource_len(hc_dev, 1), MY_NAME)) {
103 err("cannot reserve MMIO region");
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600104 ret = -ENOMEM;
105 goto exit_disable_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
108 hc_registers =
109 ioremap(pci_resource_start(hc_dev, 1), pci_resource_len(hc_dev, 1));
Quentin Lambert382a9c92014-09-07 20:02:04 +0200110 if (!hc_registers) {
Greg Kroah-Hartman1396a8c2006-06-12 15:14:29 -0700111 err("cannot remap MMIO region %llx @ %llx",
112 (unsigned long long)pci_resource_len(hc_dev, 1),
113 (unsigned long long)pci_resource_start(hc_dev, 1));
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600114 ret = -ENODEV;
115 goto exit_release_region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
118 csr_hc_index = hc_registers + CSR_HCINDEX;
119 csr_hc_data = hc_registers + CSR_HCDATA;
120 csr_int_status = hc_registers + CSR_INTSTAT;
121 csr_int_mask = hc_registers + CSR_INTMASK;
122
123 /*
124 * Disable host control, fault and serial interrupts
125 */
126 dbg("disabling host control, fault and serial interrupts");
127 writeb((u8) HC_INT_MASK_REG, csr_hc_index);
128 writeb((u8) ALL_INDEXED_INTS_MASK, csr_hc_data);
129 dbg("disabled host control, fault and serial interrupts");
130
131 /*
132 * Disable timer0, timer1 and ENUM interrupts
133 */
134 dbg("disabling timer0, timer1 and ENUM interrupts");
135 writeb((u8) ALL_DIRECT_INTS_MASK, csr_int_mask);
136 dbg("disabled timer0, timer1 and ENUM interrupts");
137 return 0;
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600138
139exit_release_region:
140 release_mem_region(pci_resource_start(hc_dev, 1),
141 pci_resource_len(hc_dev, 1));
142exit_disable_device:
143 pci_disable_device(hc_dev);
144 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147static int zt5550_hc_cleanup(void)
148{
Quentin Lambert382a9c92014-09-07 20:02:04 +0200149 if (!hc_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return -ENODEV;
151
152 iounmap(hc_registers);
153 release_mem_region(pci_resource_start(hc_dev, 1),
154 pci_resource_len(hc_dev, 1));
Bjorn Helgaasc8920f02005-09-28 15:15:16 -0600155 pci_disable_device(hc_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return 0;
157}
158
159static int zt5550_hc_query_enum(void)
160{
161 u8 value;
162
163 value = inb_p(ENUM_PORT);
164 return ((value & ENUM_MASK) == ENUM_MASK);
165}
166
167static int zt5550_hc_check_irq(void *dev_id)
168{
169 int ret;
170 u8 reg;
171
172 ret = 0;
Quentin Lambert382a9c92014-09-07 20:02:04 +0200173 if (dev_id == zt5550_hpc.dev_id) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 reg = readb(csr_int_status);
Quentin Lambert382a9c92014-09-07 20:02:04 +0200175 if (reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 ret = 1;
177 }
178 return ret;
179}
180
181static int zt5550_hc_enable_irq(void)
182{
183 u8 reg;
184
Quentin Lambert656f9782014-09-07 20:02:47 +0200185 if (hc_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return -ENODEV;
Quentin Lambert656f9782014-09-07 20:02:47 +0200187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 reg = readb(csr_int_mask);
189 reg = reg & ~ENUM_INT_MASK;
190 writeb(reg, csr_int_mask);
191 return 0;
192}
193
194static int zt5550_hc_disable_irq(void)
195{
196 u8 reg;
197
Quentin Lambert656f9782014-09-07 20:02:47 +0200198 if (hc_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 reg = readb(csr_int_mask);
202 reg = reg | ENUM_INT_MASK;
203 writeb(reg, csr_int_mask);
204 return 0;
205}
206
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800207static int zt5550_hc_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int status;
210
211 status = zt5550_hc_config(pdev);
Quentin Lambert656f9782014-09-07 20:02:47 +0200212 if (status != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return status;
Quentin Lambert656f9782014-09-07 20:02:47 +0200214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dbg("returned from zt5550_hc_config");
216
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800217 memset(&zt5550_hpc, 0, sizeof(struct cpci_hp_controller));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 zt5550_hpc_ops.query_enum = zt5550_hc_query_enum;
219 zt5550_hpc.ops = &zt5550_hpc_ops;
Quentin Lambert382a9c92014-09-07 20:02:04 +0200220 if (!poll) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 zt5550_hpc.irq = hc_dev->irq;
Thomas Gleixner6b4486e2006-07-01 19:29:41 -0700222 zt5550_hpc.irq_flags = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 zt5550_hpc.dev_id = hc_dev;
224
225 zt5550_hpc_ops.enable_irq = zt5550_hc_enable_irq;
226 zt5550_hpc_ops.disable_irq = zt5550_hc_disable_irq;
227 zt5550_hpc_ops.check_irq = zt5550_hc_check_irq;
228 } else {
229 info("using ENUM# polling mode");
230 }
231
232 status = cpci_hp_register_controller(&zt5550_hpc);
Quentin Lambert382a9c92014-09-07 20:02:04 +0200233 if (status != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 err("could not register cPCI hotplug controller");
235 goto init_hc_error;
236 }
237 dbg("registered controller");
238
239 /* Look for first device matching cPCI bus's bridge vendor and device IDs */
Quentin Lambert79e50e72014-09-07 20:03:32 +0200240 bus0_dev = pci_get_device(PCI_VENDOR_ID_DEC,
241 PCI_DEVICE_ID_DEC_21154, NULL);
242 if (!bus0_dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 status = -ENODEV;
244 goto init_register_error;
245 }
246 bus0 = bus0_dev->subordinate;
247 pci_dev_put(bus0_dev);
248
249 status = cpci_hp_register_bus(bus0, 0x0a, 0x0f);
Quentin Lambert382a9c92014-09-07 20:02:04 +0200250 if (status != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 err("could not register cPCI hotplug bus");
252 goto init_register_error;
253 }
254 dbg("registered bus");
255
256 status = cpci_hp_start();
Quentin Lambert382a9c92014-09-07 20:02:04 +0200257 if (status != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 err("could not started cPCI hotplug system");
259 cpci_hp_unregister_bus(bus0);
260 goto init_register_error;
261 }
262 dbg("started cpci hp system");
263
264 return 0;
265init_register_error:
266 cpci_hp_unregister_controller(&zt5550_hpc);
267init_hc_error:
268 err("status = %d", status);
269 zt5550_hc_cleanup();
270 return status;
271
272}
273
Bill Pemberton15856ad2012-11-21 15:35:00 -0500274static void zt5550_hc_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 cpci_hp_stop();
277 cpci_hp_unregister_bus(bus0);
278 cpci_hp_unregister_controller(&zt5550_hpc);
279 zt5550_hc_cleanup();
280}
281
282
283static struct pci_device_id zt5550_hc_pci_tbl[] = {
284 { PCI_VENDOR_ID_ZIATECH, PCI_DEVICE_ID_ZIATECH_5550_HC, PCI_ANY_ID, PCI_ANY_ID, },
285 { 0, }
286};
287MODULE_DEVICE_TABLE(pci, zt5550_hc_pci_tbl);
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static struct pci_driver zt5550_hc_driver = {
290 .name = "zt5550_hc",
291 .id_table = zt5550_hc_pci_tbl,
292 .probe = zt5550_hc_init_one,
Bill Pemberton15856ad2012-11-21 15:35:00 -0500293 .remove = zt5550_hc_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294};
295
296static int __init zt5550_init(void)
297{
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400298 struct resource *r;
Scott Murray03555d52007-04-13 15:34:26 -0700299 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 info(DRIVER_DESC " version: " DRIVER_VERSION);
302 r = request_region(ENUM_PORT, 1, "#ENUM hotswap signal register");
Quentin Lambert382a9c92014-09-07 20:02:04 +0200303 if (!r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return -EBUSY;
305
Scott Murray03555d52007-04-13 15:34:26 -0700306 rc = pci_register_driver(&zt5550_hc_driver);
Quentin Lambert382a9c92014-09-07 20:02:04 +0200307 if (rc < 0)
Scott Murray03555d52007-04-13 15:34:26 -0700308 release_region(ENUM_PORT, 1);
309 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static void __exit
313zt5550_exit(void)
314{
315 pci_unregister_driver(&zt5550_hc_driver);
316 release_region(ENUM_PORT, 1);
317}
318
319module_init(zt5550_init);
320module_exit(zt5550_exit);
321
322MODULE_AUTHOR(DRIVER_AUTHOR);
323MODULE_DESCRIPTION(DRIVER_DESC);
324MODULE_LICENSE("GPL");
325module_param(debug, bool, 0644);
326MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
327module_param(poll, bool, 0644);
328MODULE_PARM_DESC(poll, "#ENUM polling mode enabled or not");