Merge branch 'master' of /home/src/linux-2.6/
[linux-2.6] / 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 <yoichi_yuasa@tripeaks.co.jp>
7  *  Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
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 #define VRC4173_MPIUINTREG      0x06e
60 #define VRC4173_MAIUINTREG      0x070
61 #define VRC4173_MKIUINTREG      0x072
62
63 #define VRC4173_SELECTREG       0x09e
64  #define SEL3                   0x0008
65  #define SEL2                   0x0004
66  #define SEL1                   0x0002
67  #define SEL0                   0x0001
68
69 static struct pci_device_id vrc4173_id_table[] __devinitdata = {
70         {       .vendor         = PCI_VENDOR_ID_NEC,
71                 .device         = PCI_DEVICE_ID_NEC_VRC4173,
72                 .subvendor      = PCI_ANY_ID,
73                 .subdevice      = PCI_ANY_ID,                   },
74         {       .vendor         = 0,                            },
75 };
76
77 unsigned long vrc4173_io_offset = 0;
78
79 EXPORT_SYMBOL(vrc4173_io_offset);
80
81 static int vrc4173_initialized;
82 static uint16_t vrc4173_cmuclkmsk;
83 static uint16_t vrc4173_selectreg;
84 static DEFINE_SPINLOCK(vrc4173_cmu_lock);
85 static DEFINE_SPINLOCK(vrc4173_giu_lock);
86
87 static inline void set_cmusrst(uint16_t val)
88 {
89         uint16_t cmusrst;
90
91         cmusrst = vrc4173_inw(VRC4173_CMUSRST);
92         cmusrst |= val;
93         vrc4173_outw(cmusrst, VRC4173_CMUSRST);
94 }
95
96 static inline void clear_cmusrst(uint16_t val)
97 {
98         uint16_t cmusrst;
99
100         cmusrst = vrc4173_inw(VRC4173_CMUSRST);
101         cmusrst &= ~val;
102         vrc4173_outw(cmusrst, VRC4173_CMUSRST);
103 }
104
105 void vrc4173_supply_clock(vrc4173_clock_t clock)
106 {
107         if (vrc4173_initialized) {
108                 spin_lock_irq(&vrc4173_cmu_lock);
109
110                 switch (clock) {
111                 case VRC4173_PIU_CLOCK:
112                         vrc4173_cmuclkmsk |= MSKPIU;
113                         break;
114                 case VRC4173_KIU_CLOCK:
115                         vrc4173_cmuclkmsk |= MSKKIU;
116                         break;
117                 case VRC4173_AIU_CLOCK:
118                         vrc4173_cmuclkmsk |= MSKAIU;
119                         break;
120                 case VRC4173_PS2_CH1_CLOCK:
121                         vrc4173_cmuclkmsk |= MSKPS2CH1;
122                         break;
123                 case VRC4173_PS2_CH2_CLOCK:
124                         vrc4173_cmuclkmsk |= MSKPS2CH2;
125                         break;
126                 case VRC4173_USBU_PCI_CLOCK:
127                         set_cmusrst(USBRST);
128                         vrc4173_cmuclkmsk |= MSKUSB;
129                         break;
130                 case VRC4173_CARDU1_PCI_CLOCK:
131                         set_cmusrst(CARD1RST);
132                         vrc4173_cmuclkmsk |= MSKCARD1;
133                         break;
134                 case VRC4173_CARDU2_PCI_CLOCK:
135                         set_cmusrst(CARD2RST);
136                         vrc4173_cmuclkmsk |= MSKCARD2;
137                         break;
138                 case VRC4173_AC97U_PCI_CLOCK:
139                         set_cmusrst(AC97RST);
140                         vrc4173_cmuclkmsk |= MSKAC97;
141                         break;
142                 case VRC4173_USBU_48MHz_CLOCK:
143                         set_cmusrst(USBRST);
144                         vrc4173_cmuclkmsk |= MSK48MUSB;
145                         break;
146                 case VRC4173_EXT_48MHz_CLOCK:
147                         if (vrc4173_cmuclkmsk & MSK48MOSC)
148                                 vrc4173_cmuclkmsk |= MSK48MPIN;
149                         else
150                                 printk(KERN_WARNING
151                                        "vrc4173_supply_clock: "
152                                        "Please supply VRC4173_48MHz_CLOCK first "
153                                        "rather than VRC4173_EXT_48MHz_CLOCK.\n");
154                         break;
155                 case VRC4173_48MHz_CLOCK:
156                         vrc4173_cmuclkmsk |= MSK48MOSC;
157                         break;
158                 default:
159                         printk(KERN_WARNING
160                                "vrc4173_supply_clock: Invalid CLOCK value %u\n", clock);
161                         break;
162                 }
163
164                 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
165
166                 switch (clock) {
167                 case VRC4173_USBU_PCI_CLOCK:
168                 case VRC4173_USBU_48MHz_CLOCK:
169                         clear_cmusrst(USBRST);
170                         break;
171                 case VRC4173_CARDU1_PCI_CLOCK:
172                         clear_cmusrst(CARD1RST);
173                         break;
174                 case VRC4173_CARDU2_PCI_CLOCK:
175                         clear_cmusrst(CARD2RST);
176                         break;
177                 case VRC4173_AC97U_PCI_CLOCK:
178                         clear_cmusrst(AC97RST);
179                         break;
180                 default:
181                         break;
182                 }
183
184                 spin_unlock_irq(&vrc4173_cmu_lock);
185         }
186 }
187
188 EXPORT_SYMBOL(vrc4173_supply_clock);
189
190 void vrc4173_mask_clock(vrc4173_clock_t clock)
191 {
192         if (vrc4173_initialized) {
193                 spin_lock_irq(&vrc4173_cmu_lock);
194
195                 switch (clock) {
196                 case VRC4173_PIU_CLOCK:
197                         vrc4173_cmuclkmsk &= ~MSKPIU;
198                         break;
199                 case VRC4173_KIU_CLOCK:
200                         vrc4173_cmuclkmsk &= ~MSKKIU;
201                         break;
202                 case VRC4173_AIU_CLOCK:
203                         vrc4173_cmuclkmsk &= ~MSKAIU;
204                         break;
205                 case VRC4173_PS2_CH1_CLOCK:
206                         vrc4173_cmuclkmsk &= ~MSKPS2CH1;
207                         break;
208                 case VRC4173_PS2_CH2_CLOCK:
209                         vrc4173_cmuclkmsk &= ~MSKPS2CH2;
210                         break;
211                 case VRC4173_USBU_PCI_CLOCK:
212                         set_cmusrst(USBRST);
213                         vrc4173_cmuclkmsk &= ~MSKUSB;
214                         break;
215                 case VRC4173_CARDU1_PCI_CLOCK:
216                         set_cmusrst(CARD1RST);
217                         vrc4173_cmuclkmsk &= ~MSKCARD1;
218                         break;
219                 case VRC4173_CARDU2_PCI_CLOCK:
220                         set_cmusrst(CARD2RST);
221                         vrc4173_cmuclkmsk &= ~MSKCARD2;
222                         break;
223                 case VRC4173_AC97U_PCI_CLOCK:
224                         set_cmusrst(AC97RST);
225                         vrc4173_cmuclkmsk &= ~MSKAC97;
226                         break;
227                 case VRC4173_USBU_48MHz_CLOCK:
228                         set_cmusrst(USBRST);
229                         vrc4173_cmuclkmsk &= ~MSK48MUSB;
230                         break;
231                 case VRC4173_EXT_48MHz_CLOCK:
232                         vrc4173_cmuclkmsk &= ~MSK48MPIN;
233                         break;
234                 case VRC4173_48MHz_CLOCK:
235                         vrc4173_cmuclkmsk &= ~MSK48MOSC;
236                         break;
237                 default:
238                         printk(KERN_WARNING "vrc4173_mask_clock: Invalid CLOCK value %u\n", clock);
239                         break;
240                 }
241
242                 vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
243
244                 switch (clock) {
245                 case VRC4173_USBU_PCI_CLOCK:
246                 case VRC4173_USBU_48MHz_CLOCK:
247                         clear_cmusrst(USBRST);
248                         break;
249                 case VRC4173_CARDU1_PCI_CLOCK:
250                         clear_cmusrst(CARD1RST);
251                         break;
252                 case VRC4173_CARDU2_PCI_CLOCK:
253                         clear_cmusrst(CARD2RST);
254                         break;
255                 case VRC4173_AC97U_PCI_CLOCK:
256                         clear_cmusrst(AC97RST);
257                         break;
258                 default:
259                         break;
260                 }
261
262                 spin_unlock_irq(&vrc4173_cmu_lock);
263         }
264 }
265
266 EXPORT_SYMBOL(vrc4173_mask_clock);
267
268 static inline void vrc4173_cmu_init(void)
269 {
270         vrc4173_cmuclkmsk = vrc4173_inw(VRC4173_CMUCLKMSK);
271
272         spin_lock_init(&vrc4173_cmu_lock);
273 }
274
275 void vrc4173_select_function(vrc4173_function_t function)
276 {
277         if (vrc4173_initialized) {
278                 spin_lock_irq(&vrc4173_giu_lock);
279
280                 switch(function) {
281                 case PS2_CHANNEL1:
282                         vrc4173_selectreg |= SEL2;
283                         break;
284                 case PS2_CHANNEL2:
285                         vrc4173_selectreg |= SEL1;
286                         break;
287                 case TOUCHPANEL:
288                         vrc4173_selectreg &= SEL2 | SEL1 | SEL0;
289                         break;
290                 case KEYBOARD_8SCANLINES:
291                         vrc4173_selectreg &= SEL3 | SEL2 | SEL1;
292                         break;
293                 case KEYBOARD_10SCANLINES:
294                         vrc4173_selectreg &= SEL3 | SEL2;
295                         break;
296                 case KEYBOARD_12SCANLINES:
297                         vrc4173_selectreg &= SEL3;
298                         break;
299                 case GPIO_0_15PINS:
300                         vrc4173_selectreg |= SEL0;
301                         break;
302                 case GPIO_16_20PINS:
303                         vrc4173_selectreg |= SEL3;
304                         break;
305                 }
306
307                 vrc4173_outw(vrc4173_selectreg, VRC4173_SELECTREG);
308
309                 spin_unlock_irq(&vrc4173_giu_lock);
310         }
311 }
312
313 EXPORT_SYMBOL(vrc4173_select_function);
314
315 static inline void vrc4173_giu_init(void)
316 {
317         vrc4173_selectreg = vrc4173_inw(VRC4173_SELECTREG);
318
319         spin_lock_init(&vrc4173_giu_lock);
320 }
321
322 void vrc4173_enable_piuint(uint16_t mask)
323 {
324         irq_desc_t *desc = irq_desc + VRC4173_PIU_IRQ;
325         unsigned long flags;
326         uint16_t val;
327
328         spin_lock_irqsave(&desc->lock, flags);
329         val = vrc4173_inw(VRC4173_MPIUINTREG);
330         val |= mask;
331         vrc4173_outw(val, VRC4173_MPIUINTREG);
332         spin_unlock_irqrestore(&desc->lock, flags);
333 }
334
335 EXPORT_SYMBOL(vrc4173_enable_piuint);
336
337 void vrc4173_disable_piuint(uint16_t mask)
338 {
339         irq_desc_t *desc = irq_desc + VRC4173_PIU_IRQ;
340         unsigned long flags;
341         uint16_t val;
342
343         spin_lock_irqsave(&desc->lock, flags);
344         val = vrc4173_inw(VRC4173_MPIUINTREG);
345         val &= ~mask;
346         vrc4173_outw(val, VRC4173_MPIUINTREG);
347         spin_unlock_irqrestore(&desc->lock, flags);
348 }
349
350 EXPORT_SYMBOL(vrc4173_disable_piuint);
351
352 void vrc4173_enable_aiuint(uint16_t mask)
353 {
354         irq_desc_t *desc = irq_desc + VRC4173_AIU_IRQ;
355         unsigned long flags;
356         uint16_t val;
357
358         spin_lock_irqsave(&desc->lock, flags);
359         val = vrc4173_inw(VRC4173_MAIUINTREG);
360         val |= mask;
361         vrc4173_outw(val, VRC4173_MAIUINTREG);
362         spin_unlock_irqrestore(&desc->lock, flags);
363 }
364
365 EXPORT_SYMBOL(vrc4173_enable_aiuint);
366
367 void vrc4173_disable_aiuint(uint16_t mask)
368 {
369         irq_desc_t *desc = irq_desc + VRC4173_AIU_IRQ;
370         unsigned long flags;
371         uint16_t val;
372
373         spin_lock_irqsave(&desc->lock, flags);
374         val = vrc4173_inw(VRC4173_MAIUINTREG);
375         val &= ~mask;
376         vrc4173_outw(val, VRC4173_MAIUINTREG);
377         spin_unlock_irqrestore(&desc->lock, flags);
378 }
379
380 EXPORT_SYMBOL(vrc4173_disable_aiuint);
381
382 void vrc4173_enable_kiuint(uint16_t mask)
383 {
384         irq_desc_t *desc = irq_desc + VRC4173_KIU_IRQ;
385         unsigned long flags;
386         uint16_t val;
387
388         spin_lock_irqsave(&desc->lock, flags);
389         val = vrc4173_inw(VRC4173_MKIUINTREG);
390         val |= mask;
391         vrc4173_outw(val, VRC4173_MKIUINTREG);
392         spin_unlock_irqrestore(&desc->lock, flags);
393 }
394
395 EXPORT_SYMBOL(vrc4173_enable_kiuint);
396
397 void vrc4173_disable_kiuint(uint16_t mask)
398 {
399         irq_desc_t *desc = irq_desc + VRC4173_KIU_IRQ;
400         unsigned long flags;
401         uint16_t val;
402
403         spin_lock_irqsave(&desc->lock, flags);
404         val = vrc4173_inw(VRC4173_MKIUINTREG);
405         val &= ~mask;
406         vrc4173_outw(val, VRC4173_MKIUINTREG);
407         spin_unlock_irqrestore(&desc->lock, flags);
408 }
409
410 EXPORT_SYMBOL(vrc4173_disable_kiuint);
411
412 static void enable_vrc4173_irq(unsigned int irq)
413 {
414         uint16_t val;
415
416         val = vrc4173_inw(VRC4173_MSYSINT1REG);
417         val |= (uint16_t)1 << (irq - VRC4173_IRQ_BASE);
418         vrc4173_outw(val, VRC4173_MSYSINT1REG);
419 }
420
421 static void disable_vrc4173_irq(unsigned int irq)
422 {
423         uint16_t val;
424
425         val = vrc4173_inw(VRC4173_MSYSINT1REG);
426         val &= ~((uint16_t)1 << (irq - VRC4173_IRQ_BASE));
427         vrc4173_outw(val, VRC4173_MSYSINT1REG);
428 }
429
430 static unsigned int startup_vrc4173_irq(unsigned int irq)
431 {
432         enable_vrc4173_irq(irq);
433         return 0; /* never anything pending */
434 }
435
436 #define shutdown_vrc4173_irq    disable_vrc4173_irq
437 #define ack_vrc4173_irq         disable_vrc4173_irq
438
439 static void end_vrc4173_irq(unsigned int irq)
440 {
441         if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
442                 enable_vrc4173_irq(irq);
443 }
444
445 static struct hw_interrupt_type vrc4173_irq_type = {
446         .typename       = "VRC4173",
447         .startup        = startup_vrc4173_irq,
448         .shutdown       = shutdown_vrc4173_irq,
449         .enable         = enable_vrc4173_irq,
450         .disable        = disable_vrc4173_irq,
451         .ack            = ack_vrc4173_irq,
452         .end            = end_vrc4173_irq,
453 };
454
455 static int vrc4173_get_irq_number(int irq)
456 {
457         uint16_t status, mask;
458         int i;
459
460         status = vrc4173_inw(VRC4173_SYSINT1REG);
461         mask = vrc4173_inw(VRC4173_MSYSINT1REG);
462
463         status &= mask;
464         if (status) {
465                 for (i = 0; i < 16; i++)
466                         if (status & (0x0001 << i))
467                                 return VRC4173_IRQ(i);
468         }
469
470         return -EINVAL;
471 }
472
473 static inline int vrc4173_icu_init(int cascade_irq)
474 {
475         int i;
476
477         if (cascade_irq < GIU_IRQ(0) || cascade_irq > GIU_IRQ(15))
478                 return -EINVAL;
479
480         vrc4173_outw(0, VRC4173_MSYSINT1REG);
481
482         vr41xx_set_irq_trigger(GIU_IRQ_TO_PIN(cascade_irq), TRIGGER_LEVEL, SIGNAL_THROUGH);
483         vr41xx_set_irq_level(GIU_IRQ_TO_PIN(cascade_irq), LEVEL_LOW);
484
485         for (i = VRC4173_IRQ_BASE; i <= VRC4173_IRQ_LAST; i++)
486                 irq_desc[i].handler = &vrc4173_irq_type;
487
488         return 0;
489 }
490
491 static int __devinit vrc4173_probe(struct pci_dev *dev,
492                                    const struct pci_device_id *id)
493 {
494         unsigned long start, flags;
495         int err;
496
497         err = pci_enable_device(dev);
498         if (err < 0) {
499                 printk(KERN_ERR "vrc4173: Failed to enable PCI device, aborting\n");
500                 return err;
501         }
502
503         pci_set_master(dev);
504
505         start = pci_resource_start(dev, 0);
506         if (start == 0) {
507                 printk(KERN_ERR "vrc4173:No such PCI I/O resource, aborting\n");
508                 return -ENXIO;
509         }
510
511         flags = pci_resource_flags(dev, 0);
512         if ((flags & IORESOURCE_IO) == 0) {
513                 printk(KERN_ERR "vrc4173: No such PCI I/O resource, aborting\n");
514                 return -ENXIO;
515         }
516
517         err = pci_request_regions(dev, "NEC VRC4173");
518         if (err < 0) {
519                 printk(KERN_ERR "vrc4173: PCI resources are busy, aborting\n");
520                 return err;
521         }
522
523         set_vrc4173_io_offset(start);
524
525         vrc4173_cmu_init();
526         vrc4173_giu_init();
527
528         err = vrc4173_icu_init(dev->irq);
529         if (err < 0) {
530                 printk(KERN_ERR "vrc4173: Invalid IRQ %d, aborting\n", dev->irq);
531                 return err;
532         }
533
534         err = vr41xx_cascade_irq(dev->irq, vrc4173_get_irq_number);
535         if (err < 0) {
536                 printk(KERN_ERR "vrc4173: IRQ resource %d is busy, aborting\n", dev->irq);
537                 return err;
538         }
539
540         printk(KERN_INFO
541                "NEC VRC4173 at 0x%#08lx, IRQ is cascaded to %d\n", start, dev->irq);
542
543         return 0;
544 }
545
546 static void vrc4173_remove(struct pci_dev *dev)
547 {
548         free_irq(dev->irq, NULL);
549
550         pci_release_regions(dev);
551 }
552
553 static struct pci_driver vrc4173_driver = {
554         .name           = "NEC VRC4173",
555         .probe          = vrc4173_probe,
556         .remove         = vrc4173_remove,
557         .id_table       = vrc4173_id_table,
558 };
559
560 static int __devinit vrc4173_init(void)
561 {
562         int err;
563
564         err = pci_register_driver(&vrc4173_driver);
565         if (err < 0)
566                 return err;
567
568         vrc4173_initialized = 1;
569
570         return 0;
571 }
572
573 static void __devexit vrc4173_exit(void)
574 {
575         vrc4173_initialized = 0;
576
577         pci_unregister_driver(&vrc4173_driver);
578 }
579
580 module_init(vrc4173_init);
581 module_exit(vrc4173_exit);