VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / arch / mips / vr41xx / common / vrc4173.c
1 /*
2  *  vrc4173.c, NEC VRC4173 base driver for NEC VR4122/VR4131.
3  *
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>
7  *
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.
12  *
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.
17  *
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
21  */
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>
30
31 #include <asm/vr41xx/vr41xx.h>
32 #include <asm/vr41xx/vrc4173.h>
33
34 MODULE_DESCRIPTION("NEC VRC4173 base driver for NEC VR4122/4131");
35 MODULE_AUTHOR("Yoichi Yuasa <yyuasa@mvista.com>");
36 MODULE_LICENSE("GPL");
37
38 #define VRC4173_CMUCLKMSK       0x040
39  #define MSKPIU                 0x0001
40  #define MSKKIU                 0x0002
41  #define MSKAIU                 0x0004
42  #define MSKPS2CH1              0x0008
43  #define MSKPS2CH2              0x0010
44  #define MSKUSB                 0x0020
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
52  #define USBRST                 0x0001
53  #define CARD1RST               0x0002
54  #define CARD2RST               0x0004
55  #define AC97RST                0x0008
56
57 #define VRC4173_SYSINT1REG      0x060
58 #define VRC4173_MSYSINT1REG     0x06c
59
60 #define VRC4173_SELECTREG       0x09e
61  #define SEL3                   0x0008
62  #define SEL2                   0x0004
63  #define SEL1                   0x0002
64  #define SEL0                   0x0001
65
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,                   },
71         {       .vendor         = 0,                            },
72 };
73
74 unsigned long vrc4173_io_offset = 0;
75
76 EXPORT_SYMBOL(vrc4173_io_offset);
77
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;
83
84 static inline void set_cmusrst(uint16_t val)
85 {
86         uint16_t cmusrst;
87
88         cmusrst = vrc4173_inw(VRC4173_CMUSRST);
89         cmusrst |= val;
90         vrc4173_outw(cmusrst, VRC4173_CMUSRST);
91 }
92
93 static inline void clear_cmusrst(uint16_t val)
94 {
95         uint16_t cmusrst;
96
97         cmusrst = vrc4173_inw(VRC4173_CMUSRST);
98         cmusrst &= ~val;
99         vrc4173_outw(cmusrst, VRC4173_CMUSRST);
100 }
101
102 void vrc4173_supply_clock(vrc4173_clock_t clock)
103 {
104         if (vrc4173_initialized) {
105                 spin_lock_irq(&vrc4173_cmu_lock);
106
107                 switch (clock) {
108                 case VRC4173_PIU_CLOCK:
109                         vrc4173_cmuclkmsk |= MSKPIU;
110                         break;
111                 case VRC4173_KIU_CLOCK:
112                         vrc4173_cmuclkmsk |= MSKKIU;
113                         break;
114                 case VRC4173_AIU_CLOCK:
115                         vrc4173_cmuclkmsk |= MSKAIU;
116                         break;
117                 case VRC4173_PS2_CH1_CLOCK:
118                         vrc4173_cmuclkmsk |= MSKPS2CH1;
119                         break;
120                 case VRC4173_PS2_CH2_CLOCK:
121                         vrc4173_cmuclkmsk |= MSKPS2CH2;
122                         break;
123                 case VRC4173_USBU_PCI_CLOCK:
124                         set_cmusrst(USBRST);
125                         vrc4173_cmuclkmsk |= MSKUSB;
126                         break;
127                 case VRC4173_CARDU1_PCI_CLOCK:
128                         set_cmusrst(CARD1RST);
129                         vrc4173_cmuclkmsk |= MSKCARD1;
130                         break;
131                 case VRC4173_CARDU2_PCI_CLOCK:
132                         set_cmusrst(CARD2RST);
133                         vrc4173_cmuclkmsk |= MSKCARD2;
134                         break;
135                 case VRC4173_AC97U_PCI_CLOCK:
136                         set_cmusrst(AC97RST);
137                         vrc4173_cmuclkmsk |= MSKAC97;
138                         break;
139                 case VRC4173_USBU_48MHz_CLOCK:
140                         set_cmusrst(USBRST);
141                         vrc4173_cmuclkmsk |= MSK48MUSB;
142                         break;
143                 case VRC4173_EXT_48MHz_CLOCK:
144                         if (vrc4173_cmuclkmsk & MSK48MOSC)
145                                 vrc4173_cmuclkmsk |= MSK48MPIN;
146                         else
147                                 printk(KERN_WARNING
148                                        "vrc4173_supply_clock: "
149                                        "Please supply VRC4173_48MHz_CLOCK first "
150                                        "rather than VRC4173_EXT_48MHz_CLOCK.\n");
151                         break;
152                 case VRC4173_48MHz_CLOCK:
153                         vrc4173_cmuclkmsk |= MSK48MOSC;
154                         break;
155                 default:
156                         printk(KERN_WARNING
157                                "vrc4173_supply_clock: Invalid CLOCK value %u\n", clock);
158                         break;
159                 }
160
161                 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
162
163                 switch (clock) {
164                 case VRC4173_USBU_PCI_CLOCK:
165                 case VRC4173_USBU_48MHz_CLOCK:
166                         clear_cmusrst(USBRST);
167                         break;
168                 case VRC4173_CARDU1_PCI_CLOCK:
169                         clear_cmusrst(CARD1RST);
170                         break;
171                 case VRC4173_CARDU2_PCI_CLOCK:
172                         clear_cmusrst(CARD2RST);
173                         break;
174                 case VRC4173_AC97U_PCI_CLOCK:
175                         clear_cmusrst(AC97RST);
176                         break;
177                 default:
178                         break;
179                 }
180
181                 spin_unlock_irq(&vrc4173_cmu_lock);
182         }
183 }
184
185 EXPORT_SYMBOL(vrc4173_supply_clock);
186
187 void vrc4173_mask_clock(vrc4173_clock_t clock)
188 {
189         if (vrc4173_initialized) {
190                 spin_lock_irq(&vrc4173_cmu_lock);
191
192                 switch (clock) {
193                 case VRC4173_PIU_CLOCK:
194                         vrc4173_cmuclkmsk &= ~MSKPIU;
195                         break;
196                 case VRC4173_KIU_CLOCK:
197                         vrc4173_cmuclkmsk &= ~MSKKIU;
198                         break;
199                 case VRC4173_AIU_CLOCK:
200                         vrc4173_cmuclkmsk &= ~MSKAIU;
201                         break;
202                 case VRC4173_PS2_CH1_CLOCK:
203                         vrc4173_cmuclkmsk &= ~MSKPS2CH1;
204                         break;
205                 case VRC4173_PS2_CH2_CLOCK:
206                         vrc4173_cmuclkmsk &= ~MSKPS2CH2;
207                         break;
208                 case VRC4173_USBU_PCI_CLOCK:
209                         set_cmusrst(USBRST);
210                         vrc4173_cmuclkmsk &= ~MSKUSB;
211                         break;
212                 case VRC4173_CARDU1_PCI_CLOCK:
213                         set_cmusrst(CARD1RST);
214                         vrc4173_cmuclkmsk &= ~MSKCARD1;
215                         break;
216                 case VRC4173_CARDU2_PCI_CLOCK:
217                         set_cmusrst(CARD2RST);
218                         vrc4173_cmuclkmsk &= ~MSKCARD2;
219                         break;
220                 case VRC4173_AC97U_PCI_CLOCK:
221                         set_cmusrst(AC97RST);
222                         vrc4173_cmuclkmsk &= ~MSKAC97;
223                         break;
224                 case VRC4173_USBU_48MHz_CLOCK:
225                         set_cmusrst(USBRST);
226                         vrc4173_cmuclkmsk &= ~MSK48MUSB;
227                         break;
228                 case VRC4173_EXT_48MHz_CLOCK:
229                         vrc4173_cmuclkmsk &= ~MSK48MPIN;
230                         break;
231                 case VRC4173_48MHz_CLOCK:
232                         vrc4173_cmuclkmsk &= ~MSK48MOSC;
233                         break;
234                 default:
235                         printk(KERN_WARNING "vrc4173_mask_clock: Invalid CLOCK value %u\n", clock);
236                         break;
237                 }
238
239                 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
240
241                 switch (clock) {
242                 case VRC4173_USBU_PCI_CLOCK:
243                 case VRC4173_USBU_48MHz_CLOCK:
244                         clear_cmusrst(USBRST);
245                         break;
246                 case VRC4173_CARDU1_PCI_CLOCK:
247                         clear_cmusrst(CARD1RST);
248                         break;
249                 case VRC4173_CARDU2_PCI_CLOCK:
250                         clear_cmusrst(CARD2RST);
251                         break;
252                 case VRC4173_AC97U_PCI_CLOCK:
253                         clear_cmusrst(AC97RST);
254                         break;
255                 default:
256                         break;
257                 }
258
259                 spin_unlock_irq(&vrc4173_cmu_lock);
260         }
261 }
262
263 EXPORT_SYMBOL(vrc4173_mask_clock);
264
265 static inline void vrc4173_cmu_init(void)
266 {
267         vrc4173_cmuclkmsk = vrc4173_inw(VRC4173_CMUCLKMSK);
268
269         spin_lock_init(&vrc4173_cmu_lock);
270 }
271
272 void vrc4173_select_function(vrc4173_function_t function)
273 {
274         if (vrc4173_initialized) {
275                 spin_lock_irq(&vrc4173_giu_lock);
276
277                 switch(function) {
278                 case PS2_CHANNEL1:
279                         vrc4173_selectreg |= SEL2;
280                         break;
281                 case PS2_CHANNEL2:
282                         vrc4173_selectreg |= SEL1;
283                         break;
284                 case TOUCHPANEL:
285                         vrc4173_selectreg &= SEL2 | SEL1 | SEL0;
286                         break;
287                 case KEYBOARD_8SCANLINES:
288                         vrc4173_selectreg &= SEL3 | SEL2 | SEL1;
289                         break;
290                 case KEYBOARD_10SCANLINES:
291                         vrc4173_selectreg &= SEL3 | SEL2;
292                         break;
293                 case KEYBOARD_12SCANLINES:
294                         vrc4173_selectreg &= SEL3;
295                         break;
296                 case GPIO_0_15PINS:
297                         vrc4173_selectreg |= SEL0;
298                         break;
299                 case GPIO_16_20PINS:
300                         vrc4173_selectreg |= SEL3;
301                         break;
302                 }
303
304                 vrc4173_outw(vrc4173_selectreg, VRC4173_SELECTREG);
305
306                 spin_unlock_irq(&vrc4173_giu_lock);
307         }
308 }
309
310 EXPORT_SYMBOL(vrc4173_select_function);
311
312 static inline void vrc4173_giu_init(void)
313 {
314         vrc4173_selectreg = vrc4173_inw(VRC4173_SELECTREG);
315
316         spin_lock_init(&vrc4173_giu_lock);
317 }
318
319 static void enable_vrc4173_irq(unsigned int irq)
320 {
321         uint16_t val;
322
323         val = vrc4173_inw(VRC4173_MSYSINT1REG);
324         val |= (uint16_t)1 << (irq - VRC4173_IRQ_BASE);
325         vrc4173_outw(val, VRC4173_MSYSINT1REG);
326 }
327
328 static void disable_vrc4173_irq(unsigned int irq)
329 {
330         uint16_t val;
331
332         val = vrc4173_inw(VRC4173_MSYSINT1REG);
333         val &= ~((uint16_t)1 << (irq - VRC4173_IRQ_BASE));
334         vrc4173_outw(val, VRC4173_MSYSINT1REG);
335 }
336
337 static unsigned int startup_vrc4173_irq(unsigned int irq)
338 {
339         enable_vrc4173_irq(irq);
340         return 0; /* never anything pending */
341 }
342
343 #define shutdown_vrc4173_irq    disable_vrc4173_irq
344 #define ack_vrc4173_irq         disable_vrc4173_irq
345
346 static void end_vrc4173_irq(unsigned int irq)
347 {
348         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
349                 enable_vrc4173_irq(irq);
350 }
351
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,
360 };
361
362 static int vrc4173_get_irq_number(int irq)
363 {
364         uint16_t status, mask;
365         int i;
366
367         status = vrc4173_inw(VRC4173_SYSINT1REG);
368         mask = vrc4173_inw(VRC4173_MSYSINT1REG);
369
370         status &= mask;
371         if (status) {
372                 for (i = 0; i < 16; i++)
373                         if (status & (0x0001 << i))
374                                 return VRC4173_IRQ(i);
375         }
376
377         return -EINVAL;
378 }
379
380 static inline int vrc4173_icu_init(int cascade_irq)
381 {
382         int i;
383
384         if (cascade_irq < GIU_IRQ(0) || cascade_irq > GIU_IRQ(15))
385                 return -EINVAL;
386         
387         vrc4173_outw(0, VRC4173_MSYSINT1REG);
388
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);
391
392         for (i = VRC4173_IRQ_BASE; i <= VRC4173_IRQ_LAST; i++)
393                 irq_desc[i].handler = &vrc4173_irq_type;
394
395         return 0;
396 }
397
398 static int __devinit vrc4173_probe(struct pci_dev *dev,
399                                    const struct pci_device_id *id)
400 {
401         unsigned long start, flags;
402         int err;
403
404         err = pci_enable_device(dev);
405         if (err < 0) {
406                 printk(KERN_ERR "vrc4173: Failed to enable PCI device, aborting\n");
407                 return err;
408         }
409
410         pci_set_master(dev);
411
412         start = pci_resource_start(dev, 0);
413         if (start == 0) {
414                 printk(KERN_ERR "vrc4173:No such PCI I/O resource, aborting\n");
415                 return -ENXIO;
416         }
417
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");
421                 return -ENXIO;
422         }
423
424         err = pci_request_regions(dev, "NEC VRC4173");
425         if (err < 0) {
426                 printk(KERN_ERR "vrc4173: PCI resources are busy, aborting\n");
427                 return err;
428         }
429
430         set_vrc4173_io_offset(start);
431
432         vrc4173_cmu_init();
433         vrc4173_giu_init();
434
435         err = vrc4173_icu_init(dev->irq);
436         if (err < 0) {
437                 printk(KERN_ERR "vrc4173: Invalid IRQ %d, aborting\n", dev->irq);
438                 return err;
439         }
440
441         err = vr41xx_cascade_irq(dev->irq, vrc4173_get_irq_number);
442         if (err < 0) {
443                 printk(KERN_ERR "vrc4173: IRQ resource %d is busy, aborting\n", dev->irq);
444                 return err;
445         }
446
447         printk(KERN_INFO
448                "NEC VRC4173 at 0x%#08lx, IRQ is cascaded to %d\n", start, dev->irq);
449
450         return 0;
451 }
452
453 static void vrc4173_remove(struct pci_dev *dev)
454 {
455         free_irq(dev->irq, NULL);
456
457         pci_release_regions(dev);
458 }
459
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,
465 };
466
467 static int __devinit vrc4173_init(void)
468 {
469         int err;
470
471         err = pci_module_init(&vrc4173_driver);
472         if (err < 0)
473                 return err;
474
475         vrc4173_initialized = 1;
476
477         return 0;
478 }
479
480 static void __devexit vrc4173_exit(void)
481 {
482         vrc4173_initialized = 0;
483
484         pci_unregister_driver(&vrc4173_driver);
485 }
486
487 module_init(vrc4173_init);
488 module_exit(vrc4173_exit);