2 * vrc4173.c, NEC VRC4173 base driver for NEC VR4122/VR4131.
4 * Copyright (C) 2001-2003 MontaVista Software Inc.
5 * Author: Yoichi Yuasa <yyuasa@mvista.com, or source@mvista.com>
6 * Copyright (C) 2004 Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/config.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/interrupt.h>
26 #include <linux/irq.h>
27 #include <linux/pci.h>
28 #include <linux/spinlock.h>
29 #include <linux/types.h>
31 #include <asm/vr41xx/vr41xx.h>
32 #include <asm/vr41xx/vrc4173.h>
34 MODULE_DESCRIPTION("NEC VRC4173 base driver for NEC VR4122/4131");
35 MODULE_AUTHOR("Yoichi Yuasa <yyuasa@mvista.com>");
36 MODULE_LICENSE("GPL");
38 #define VRC4173_CMUCLKMSK 0x040
42 #define MSKPS2CH1 0x0008
43 #define MSKPS2CH2 0x0010
45 #define MSKCARD1 0x0040
46 #define MSKCARD2 0x0080
47 #define MSKAC97 0x0100
48 #define MSK48MUSB 0x0400
49 #define MSK48MPIN 0x0800
50 #define MSK48MOSC 0x1000
51 #define VRC4173_CMUSRST 0x042
53 #define CARD1RST 0x0002
54 #define CARD2RST 0x0004
55 #define AC97RST 0x0008
57 #define VRC4173_SYSINT1REG 0x060
58 #define VRC4173_MSYSINT1REG 0x06c
60 #define VRC4173_SELECTREG 0x09e
66 static struct pci_device_id vrc4173_id_table[] __devinitdata = {
67 { .vendor = PCI_VENDOR_ID_NEC,
68 .device = PCI_DEVICE_ID_NEC_VRC4173,
69 .subvendor = PCI_ANY_ID,
70 .subdevice = PCI_ANY_ID, },
74 unsigned long vrc4173_io_offset = 0;
76 EXPORT_SYMBOL(vrc4173_io_offset);
78 static int vrc4173_initialized;
79 static uint16_t vrc4173_cmuclkmsk;
80 static uint16_t vrc4173_selectreg;
81 static spinlock_t vrc4173_cmu_lock;
82 static spinlock_t vrc4173_giu_lock;
84 static inline void set_cmusrst(uint16_t val)
88 cmusrst = vrc4173_inw(VRC4173_CMUSRST);
90 vrc4173_outw(cmusrst, VRC4173_CMUSRST);
93 static inline void clear_cmusrst(uint16_t val)
97 cmusrst = vrc4173_inw(VRC4173_CMUSRST);
99 vrc4173_outw(cmusrst, VRC4173_CMUSRST);
102 void vrc4173_supply_clock(vrc4173_clock_t clock)
104 if (vrc4173_initialized) {
105 spin_lock_irq(&vrc4173_cmu_lock);
108 case VRC4173_PIU_CLOCK:
109 vrc4173_cmuclkmsk |= MSKPIU;
111 case VRC4173_KIU_CLOCK:
112 vrc4173_cmuclkmsk |= MSKKIU;
114 case VRC4173_AIU_CLOCK:
115 vrc4173_cmuclkmsk |= MSKAIU;
117 case VRC4173_PS2_CH1_CLOCK:
118 vrc4173_cmuclkmsk |= MSKPS2CH1;
120 case VRC4173_PS2_CH2_CLOCK:
121 vrc4173_cmuclkmsk |= MSKPS2CH2;
123 case VRC4173_USBU_PCI_CLOCK:
125 vrc4173_cmuclkmsk |= MSKUSB;
127 case VRC4173_CARDU1_PCI_CLOCK:
128 set_cmusrst(CARD1RST);
129 vrc4173_cmuclkmsk |= MSKCARD1;
131 case VRC4173_CARDU2_PCI_CLOCK:
132 set_cmusrst(CARD2RST);
133 vrc4173_cmuclkmsk |= MSKCARD2;
135 case VRC4173_AC97U_PCI_CLOCK:
136 set_cmusrst(AC97RST);
137 vrc4173_cmuclkmsk |= MSKAC97;
139 case VRC4173_USBU_48MHz_CLOCK:
141 vrc4173_cmuclkmsk |= MSK48MUSB;
143 case VRC4173_EXT_48MHz_CLOCK:
144 if (vrc4173_cmuclkmsk & MSK48MOSC)
145 vrc4173_cmuclkmsk |= MSK48MPIN;
148 "vrc4173_supply_clock: "
149 "Please supply VRC4173_48MHz_CLOCK first "
150 "rather than VRC4173_EXT_48MHz_CLOCK.\n");
152 case VRC4173_48MHz_CLOCK:
153 vrc4173_cmuclkmsk |= MSK48MOSC;
157 "vrc4173_supply_clock: Invalid CLOCK value %u\n", clock);
161 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
164 case VRC4173_USBU_PCI_CLOCK:
165 case VRC4173_USBU_48MHz_CLOCK:
166 clear_cmusrst(USBRST);
168 case VRC4173_CARDU1_PCI_CLOCK:
169 clear_cmusrst(CARD1RST);
171 case VRC4173_CARDU2_PCI_CLOCK:
172 clear_cmusrst(CARD2RST);
174 case VRC4173_AC97U_PCI_CLOCK:
175 clear_cmusrst(AC97RST);
181 spin_unlock_irq(&vrc4173_cmu_lock);
185 EXPORT_SYMBOL(vrc4173_supply_clock);
187 void vrc4173_mask_clock(vrc4173_clock_t clock)
189 if (vrc4173_initialized) {
190 spin_lock_irq(&vrc4173_cmu_lock);
193 case VRC4173_PIU_CLOCK:
194 vrc4173_cmuclkmsk &= ~MSKPIU;
196 case VRC4173_KIU_CLOCK:
197 vrc4173_cmuclkmsk &= ~MSKKIU;
199 case VRC4173_AIU_CLOCK:
200 vrc4173_cmuclkmsk &= ~MSKAIU;
202 case VRC4173_PS2_CH1_CLOCK:
203 vrc4173_cmuclkmsk &= ~MSKPS2CH1;
205 case VRC4173_PS2_CH2_CLOCK:
206 vrc4173_cmuclkmsk &= ~MSKPS2CH2;
208 case VRC4173_USBU_PCI_CLOCK:
210 vrc4173_cmuclkmsk &= ~MSKUSB;
212 case VRC4173_CARDU1_PCI_CLOCK:
213 set_cmusrst(CARD1RST);
214 vrc4173_cmuclkmsk &= ~MSKCARD1;
216 case VRC4173_CARDU2_PCI_CLOCK:
217 set_cmusrst(CARD2RST);
218 vrc4173_cmuclkmsk &= ~MSKCARD2;
220 case VRC4173_AC97U_PCI_CLOCK:
221 set_cmusrst(AC97RST);
222 vrc4173_cmuclkmsk &= ~MSKAC97;
224 case VRC4173_USBU_48MHz_CLOCK:
226 vrc4173_cmuclkmsk &= ~MSK48MUSB;
228 case VRC4173_EXT_48MHz_CLOCK:
229 vrc4173_cmuclkmsk &= ~MSK48MPIN;
231 case VRC4173_48MHz_CLOCK:
232 vrc4173_cmuclkmsk &= ~MSK48MOSC;
235 printk(KERN_WARNING "vrc4173_mask_clock: Invalid CLOCK value %u\n", clock);
239 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
242 case VRC4173_USBU_PCI_CLOCK:
243 case VRC4173_USBU_48MHz_CLOCK:
244 clear_cmusrst(USBRST);
246 case VRC4173_CARDU1_PCI_CLOCK:
247 clear_cmusrst(CARD1RST);
249 case VRC4173_CARDU2_PCI_CLOCK:
250 clear_cmusrst(CARD2RST);
252 case VRC4173_AC97U_PCI_CLOCK:
253 clear_cmusrst(AC97RST);
259 spin_unlock_irq(&vrc4173_cmu_lock);
263 EXPORT_SYMBOL(vrc4173_mask_clock);
265 static inline void vrc4173_cmu_init(void)
267 vrc4173_cmuclkmsk = vrc4173_inw(VRC4173_CMUCLKMSK);
269 spin_lock_init(&vrc4173_cmu_lock);
272 void vrc4173_select_function(vrc4173_function_t function)
274 if (vrc4173_initialized) {
275 spin_lock_irq(&vrc4173_giu_lock);
279 vrc4173_selectreg |= SEL2;
282 vrc4173_selectreg |= SEL1;
285 vrc4173_selectreg &= SEL2 | SEL1 | SEL0;
287 case KEYBOARD_8SCANLINES:
288 vrc4173_selectreg &= SEL3 | SEL2 | SEL1;
290 case KEYBOARD_10SCANLINES:
291 vrc4173_selectreg &= SEL3 | SEL2;
293 case KEYBOARD_12SCANLINES:
294 vrc4173_selectreg &= SEL3;
297 vrc4173_selectreg |= SEL0;
300 vrc4173_selectreg |= SEL3;
304 vrc4173_outw(vrc4173_selectreg, VRC4173_SELECTREG);
306 spin_unlock_irq(&vrc4173_giu_lock);
310 EXPORT_SYMBOL(vrc4173_select_function);
312 static inline void vrc4173_giu_init(void)
314 vrc4173_selectreg = vrc4173_inw(VRC4173_SELECTREG);
316 spin_lock_init(&vrc4173_giu_lock);
319 static void enable_vrc4173_irq(unsigned int irq)
323 val = vrc4173_inw(VRC4173_MSYSINT1REG);
324 val |= (uint16_t)1 << (irq - VRC4173_IRQ_BASE);
325 vrc4173_outw(val, VRC4173_MSYSINT1REG);
328 static void disable_vrc4173_irq(unsigned int irq)
332 val = vrc4173_inw(VRC4173_MSYSINT1REG);
333 val &= ~((uint16_t)1 << (irq - VRC4173_IRQ_BASE));
334 vrc4173_outw(val, VRC4173_MSYSINT1REG);
337 static unsigned int startup_vrc4173_irq(unsigned int irq)
339 enable_vrc4173_irq(irq);
340 return 0; /* never anything pending */
343 #define shutdown_vrc4173_irq disable_vrc4173_irq
344 #define ack_vrc4173_irq disable_vrc4173_irq
346 static void end_vrc4173_irq(unsigned int irq)
348 if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
349 enable_vrc4173_irq(irq);
352 static struct hw_interrupt_type vrc4173_irq_type = {
353 .typename = "VRC4173",
354 .startup = startup_vrc4173_irq,
355 .shutdown = shutdown_vrc4173_irq,
356 .enable = enable_vrc4173_irq,
357 .disable = disable_vrc4173_irq,
358 .ack = ack_vrc4173_irq,
359 .end = end_vrc4173_irq,
362 static int vrc4173_get_irq_number(int irq)
364 uint16_t status, mask;
367 status = vrc4173_inw(VRC4173_SYSINT1REG);
368 mask = vrc4173_inw(VRC4173_MSYSINT1REG);
372 for (i = 0; i < 16; i++)
373 if (status & (0x0001 << i))
374 return VRC4173_IRQ(i);
380 static inline int vrc4173_icu_init(int cascade_irq)
384 if (cascade_irq < GIU_IRQ(0) || cascade_irq > GIU_IRQ(15))
387 vrc4173_outw(0, VRC4173_MSYSINT1REG);
389 vr41xx_set_irq_trigger(GIU_IRQ_TO_PIN(cascade_irq), TRIGGER_LEVEL, SIGNAL_THROUGH);
390 vr41xx_set_irq_level(GIU_IRQ_TO_PIN(cascade_irq), LEVEL_LOW);
392 for (i = VRC4173_IRQ_BASE; i <= VRC4173_IRQ_LAST; i++)
393 irq_desc[i].handler = &vrc4173_irq_type;
398 static int __devinit vrc4173_probe(struct pci_dev *dev,
399 const struct pci_device_id *id)
401 unsigned long start, flags;
404 err = pci_enable_device(dev);
406 printk(KERN_ERR "vrc4173: Failed to enable PCI device, aborting\n");
412 start = pci_resource_start(dev, 0);
414 printk(KERN_ERR "vrc4173:No such PCI I/O resource, aborting\n");
418 flags = pci_resource_flags(dev, 0);
419 if ((flags & IORESOURCE_IO) == 0) {
420 printk(KERN_ERR "vrc4173: No such PCI I/O resource, aborting\n");
424 err = pci_request_regions(dev, "NEC VRC4173");
426 printk(KERN_ERR "vrc4173: PCI resources are busy, aborting\n");
430 set_vrc4173_io_offset(start);
435 err = vrc4173_icu_init(dev->irq);
437 printk(KERN_ERR "vrc4173: Invalid IRQ %d, aborting\n", dev->irq);
441 err = vr41xx_cascade_irq(dev->irq, vrc4173_get_irq_number);
443 printk(KERN_ERR "vrc4173: IRQ resource %d is busy, aborting\n", dev->irq);
448 "NEC VRC4173 at 0x%#08lx, IRQ is cascaded to %d\n", start, dev->irq);
453 static void vrc4173_remove(struct pci_dev *dev)
455 free_irq(dev->irq, NULL);
457 pci_release_regions(dev);
460 static struct pci_driver vrc4173_driver = {
461 .name = "NEC VRC4173",
462 .probe = vrc4173_probe,
463 .remove = vrc4173_remove,
464 .id_table = vrc4173_id_table,
467 static int __devinit vrc4173_init(void)
471 err = pci_module_init(&vrc4173_driver);
475 vrc4173_initialized = 1;
480 static void __devexit vrc4173_exit(void)
482 vrc4173_initialized = 0;
484 pci_unregister_driver(&vrc4173_driver);
487 module_init(vrc4173_init);
488 module_exit(vrc4173_exit);