blob: 3dc9aee41d913de3c59a3c04fee8dac814467ca5 [file] [log] [blame]
Dave Jones835c34a2007-10-12 21:10:53 -04001/*
2 * Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
3 *
4 * National Semiconductor SCx200 support.
5 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/errno.h>
9#include <linux/kernel.h>
10#include <linux/init.h>
Jim Cromie8bcf6132006-06-27 02:54:25 -070011#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/pci.h>
13
14#include <linux/scx200.h>
Adrian Bunkcc658cf2005-11-07 00:58:37 -080015#include <linux/scx200_gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/* Verify that the configuration block really is there */
18#define scx200_cb_probe(base) (inw((base) + SCx200_CBA) == (base))
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020MODULE_AUTHOR("Christer Weinigel <wingel@nano-system.com>");
21MODULE_DESCRIPTION("NatSemi SCx200 Driver");
22MODULE_LICENSE("GPL");
23
24unsigned scx200_gpio_base = 0;
Al Viro64b33612007-10-14 19:35:20 +010025unsigned long scx200_gpio_shadow[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27unsigned scx200_cb_base = 0;
28
29static struct pci_device_id scx200_tbl[] = {
Jim Cromie0ac25262012-02-01 10:47:01 -070030 { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SCx200_BRIDGE) },
31 { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SC1100_BRIDGE) },
32 { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SCx200_XBUS) },
33 { PCI_VDEVICE(NS, PCI_DEVICE_ID_NS_SC1100_XBUS) },
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 { },
35};
36MODULE_DEVICE_TABLE(pci,scx200_tbl);
37
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -080038static int scx200_probe(struct pci_dev *, const struct pci_device_id *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static struct pci_driver scx200_pci_driver = {
41 .name = "scx200",
42 .id_table = scx200_tbl,
43 .probe = scx200_probe,
44};
45
Jim Cromie8bcf6132006-06-27 02:54:25 -070046static DEFINE_MUTEX(scx200_gpio_config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -080048static void scx200_init_shadow(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 int bank;
Jim Cromie9b170b82006-06-27 02:54:17 -070051
52 /* read the current values driven on the GPIO signals */
53 for (bank = 0; bank < 2; ++bank)
54 scx200_gpio_shadow[bank] = inl(scx200_gpio_base + 0x10 * bank);
55}
56
Greg Kroah-Hartmana18e3692012-12-21 14:02:53 -080057static int scx200_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Jim Cromie9b170b82006-06-27 02:54:17 -070058{
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 unsigned base;
60
61 if (pdev->device == PCI_DEVICE_ID_NS_SCx200_BRIDGE ||
62 pdev->device == PCI_DEVICE_ID_NS_SC1100_BRIDGE) {
63 base = pci_resource_start(pdev, 0);
Jim Cromie8ad95f02012-02-01 10:58:49 -070064 pr_info("GPIO base 0x%x\n", base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jim Cromie8ad95f02012-02-01 10:58:49 -070066 if (!request_region(base, SCx200_GPIO_SIZE,
67 "NatSemi SCx200 GPIO")) {
68 pr_err("can't allocate I/O for GPIOs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return -EBUSY;
70 }
71
72 scx200_gpio_base = base;
Jim Cromie9b170b82006-06-27 02:54:17 -070073 scx200_init_shadow();
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 } else {
76 /* find the base of the Configuration Block */
77 if (scx200_cb_probe(SCx200_CB_BASE_FIXED)) {
78 scx200_cb_base = SCx200_CB_BASE_FIXED;
79 } else {
80 pci_read_config_dword(pdev, SCx200_CBA_SCRATCH, &base);
81 if (scx200_cb_probe(base)) {
82 scx200_cb_base = base;
83 } else {
Jim Cromie8ad95f02012-02-01 10:58:49 -070084 pr_warn("Configuration Block not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -ENODEV;
86 }
87 }
Jim Cromie8ad95f02012-02-01 10:58:49 -070088 pr_info("Configuration Block base 0x%x\n", scx200_cb_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
91 return 0;
92}
93
Jim Cromie55b8c042006-06-27 02:54:15 -070094u32 scx200_gpio_configure(unsigned index, u32 mask, u32 bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 u32 config, new_config;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jim Cromie8bcf6132006-06-27 02:54:25 -070098 mutex_lock(&scx200_gpio_config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 outl(index, scx200_gpio_base + 0x20);
101 config = inl(scx200_gpio_base + 0x24);
102
103 new_config = (config & mask) | bits;
104 outl(new_config, scx200_gpio_base + 0x24);
105
Jim Cromie8bcf6132006-06-27 02:54:25 -0700106 mutex_unlock(&scx200_gpio_config_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 return config;
109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int __init scx200_init(void)
112{
Jim Cromie8ad95f02012-02-01 10:58:49 -0700113 pr_info("NatSemi SCx200 Driver\n");
Richard Knutssond1d6da82005-11-30 00:59:14 +0100114 return pci_register_driver(&scx200_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
117static void __exit scx200_cleanup(void)
118{
119 pci_unregister_driver(&scx200_pci_driver);
120 release_region(scx200_gpio_base, SCx200_GPIO_SIZE);
121}
122
123module_init(scx200_init);
124module_exit(scx200_cleanup);
125
126EXPORT_SYMBOL(scx200_gpio_base);
127EXPORT_SYMBOL(scx200_gpio_shadow);
128EXPORT_SYMBOL(scx200_gpio_configure);
129EXPORT_SYMBOL(scx200_cb_base);