aboutsummaryrefslogtreecommitdiffstats
path: root/arch/mips/vr41xx/common/vrc4173.c
blob: ba58764ef8ead5b0613fa13424caa50c336446c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/*
 *  vrc4173.c, NEC VRC4173 base driver for NEC VR4122/VR4131.
 *
 *  Copyright (C) 2001-2003  MontaVista Software Inc.
 *    Author: Yoichi Yuasa <yyuasa@mvista.com, or source@mvista.com>
 *  Copyright (C) 2004  Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
 *  Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/pci.h>
#include <linux/spinlock.h>
#include <linux/types.h>

#include <asm/vr41xx/vr41xx.h>
#include <asm/vr41xx/vrc4173.h>

MODULE_DESCRIPTION("NEC VRC4173 base driver for NEC VR4122/4131");
MODULE_AUTHOR("Yoichi Yuasa <yyuasa@mvista.com>");
MODULE_LICENSE("GPL");

#define VRC4173_CMUCLKMSK	0x040
 #define MSKPIU			0x0001
 #define MSKKIU			0x0002
 #define MSKAIU			0x0004
 #define MSKPS2CH1		0x0008
 #define MSKPS2CH2		0x0010
 #define MSKUSB			0x0020
 #define MSKCARD1		0x0040
 #define MSKCARD2		0x0080
 #define MSKAC97		0x0100
 #define MSK48MUSB		0x0400
 #define MSK48MPIN		0x0800
 #define MSK48MOSC		0x1000
#define VRC4173_CMUSRST		0x042
 #define USBRST			0x0001
 #define CARD1RST		0x0002
 #define CARD2RST		0x0004
 #define AC97RST		0x0008

#define VRC4173_SYSINT1REG	0x060
#define VRC4173_MSYSINT1REG	0x06c
#define VRC4173_MPIUINTREG	0x06e
#define VRC4173_MAIUINTREG	0x070
#define VRC4173_MKIUINTREG	0x072

#define VRC4173_SELECTREG	0x09e
 #define SEL3			0x0008
 #define SEL2			0x0004
 #define SEL1			0x0002
 #define SEL0			0x0001

static struct pci_device_id vrc4173_id_table[] __devinitdata = {
	{	.vendor		= PCI_VENDOR_ID_NEC,
		.device		= PCI_DEVICE_ID_NEC_VRC4173,
		.subvendor	= PCI_ANY_ID,
		.subdevice	= PCI_ANY_ID,			},
	{	.vendor		= 0,				},
};

unsigned long vrc4173_io_offset = 0;

EXPORT_SYMBOL(vrc4173_io_offset);

static int vrc4173_initialized;
static uint16_t vrc4173_cmuclkmsk;
static uint16_t vrc4173_selectreg;
static spinlock_t vrc4173_cmu_lock;
static spinlock_t vrc4173_giu_lock;

static inline void set_cmusrst(uint16_t val)
{
	uint16_t cmusrst;

	cmusrst = vrc4173_inw(VRC4173_CMUSRST);
	cmusrst |= val;
	vrc4173_outw(cmusrst, VRC4173_CMUSRST);
}

static inline void clear_cmusrst(uint16_t val)
{
	uint16_t cmusrst;

	cmusrst = vrc4173_inw(VRC4173_CMUSRST);
	cmusrst &= ~val;
	vrc4173_outw(cmusrst, VRC4173_CMUSRST);
}

void vrc4173_supply_clock(vrc4173_clock_t clock)
{
	if (vrc4173_initialized) {
		spin_lock_irq(&vrc4173_cmu_lock);

		switch (clock) {
		case VRC4173_PIU_CLOCK:
			vrc4173_cmuclkmsk |= MSKPIU;
			break;
		case VRC4173_KIU_CLOCK:
			vrc4173_cmuclkmsk |= MSKKIU;
			break;
		case VRC4173_AIU_CLOCK:
			vrc4173_cmuclkmsk |= MSKAIU;
			break;
		case VRC4173_PS2_CH1_CLOCK:
			vrc4173_cmuclkmsk |= MSKPS2CH1;
			break;
		case VRC4173_PS2_CH2_CLOCK:
			vrc4173_cmuclkmsk |= MSKPS2CH2;
			break;
		case VRC4173_USBU_PCI_CLOCK:
			set_cmusrst(USBRST);
			vrc4173_cmuclkmsk |= MSKUSB;
			break;
		case VRC4173_CARDU1_PCI_CLOCK:
			set_cmusrst(CARD1RST);
			vrc4173_cmuclkmsk |= MSKCARD1;
			break;
		case VRC4173_CARDU2_PCI_CLOCK:
			set_cmusrst(CARD2RST);
			vrc4173_cmuclkmsk |= MSKCARD2;
			break;
		case VRC4173_AC97U_PCI_CLOCK:
			set_cmusrst(AC97RST);
			vrc4173_cmuclkmsk |= MSKAC97;
			break;
		case VRC4173_USBU_48MHz_CLOCK:
			set_cmusrst(USBRST);
			vrc4173_cmuclkmsk |= MSK48MUSB;
			break;
		case VRC4173_EXT_48MHz_CLOCK:
			if (vrc4173_cmuclkmsk & MSK48MOSC)
				vrc4173_cmuclkmsk |= MSK48MPIN;
			else
				printk(KERN_WARNING
				       "vrc4173_supply_clock: "
				       "Please supply VRC4173_48MHz_CLOCK first "
				       "rather than VRC4173_EXT_48MHz_CLOCK.\n");
			break;
		case VRC4173_48MHz_CLOCK:
			vrc4173_cmuclkmsk |= MSK48MOSC;
			break;
		default:
			printk(KERN_WARNING
			       "vrc4173_supply_clock: Invalid CLOCK value %u\n", clock);
			break;
		}

		vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);

		switch (clock) {
		case VRC4173_USBU_PCI_CLOCK:
		case VRC4173_USBU_48MHz_CLOCK:
			clear_cmusrst(USBRST);
			break;
		case VRC4173_CARDU1_PCI_CLOCK:
			clear_cmusrst(CARD1RST);
			break;
		case VRC4173_CARDU2_PCI_CLOCK:
			clear_cmusrst(CARD2RST);
			break;
		case VRC4173_AC97U_PCI_CLOCK:
			clear_cmusrst(AC97RST);
			break;
		default:
			break;
		}

		spin_unlock_irq(&vrc4173_cmu_lock);
	}
}

EXPORT_SYMBOL(vrc4173_supply_clock);

void vrc4173_mask_clock(vrc4173_clock_t clock)
{
	if (vrc4173_initialized) {
		spin_lock_irq(&vrc4173_cmu_lock);

		switch (clock) {
		case VRC4173_PIU_CLOCK:
			vrc4173_cmuclkmsk &= ~MSKPIU;
			break;
		case VRC4173_KIU_CLOCK:
			vrc4173_cmuclkmsk &= ~MSKKIU;
			break;
		case VRC4173_AIU_CLOCK:
			vrc4173_cmuclkmsk &= ~MSKAIU;
			break;
		case VRC4173_PS2_CH1_CLOCK:
			vrc4173_cmuclkmsk &= ~MSKPS2CH1;
			break;
		case VRC4173_PS2_CH2_CLOCK:
			vrc4173_cmuclkmsk &= ~MSKPS2CH2;
			break;
		case VRC4173_USBU_PCI_CLOCK:
			set_cmusrst(USBRST);
			vrc4173_cmuclkmsk &= ~MSKUSB;
			break;
		case VRC4173_CARDU1_PCI_CLOCK:
			set_cmusrst(CARD1RST);
			vrc4173_cmuclkmsk &= ~MSKCARD1;
			break;
		case VRC4173_CARDU2_PCI_CLOCK:
			set_cmusrst(CARD2RST);
			vrc4173_cmuclkmsk &= ~MSKCARD2;
			break;
		case VRC4173_AC97U_PCI_CLOCK:
			set_cmusrst(AC97RST);
			vrc4173_cmuclkmsk &= ~MSKAC97;
			break;
		case VRC4173_USBU_48MHz_CLOCK:
			set_cmusrst(USBRST);
			vrc4173_cmuclkmsk &= ~MSK48MUSB;
			break;
		case VRC4173_EXT_48MHz_CLOCK:
			vrc4173_cmuclkmsk &= ~MSK48MPIN;
			break;
		case VRC4173_48MHz_CLOCK:
			vrc4173_cmuclkmsk &= ~MSK48MOSC;
			break;
		default:
			printk(KERN_WARNING "vrc4173_mask_clock: Invalid CLOCK value %u\n", clock);
			break;
		}

		vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);

		switch (clock) {
		case VRC4173_USBU_PCI_CLOCK:
		case VRC4173_USBU_48MHz_CLOCK:
			clear_cmusrst(USBRST);
			break;
		case VRC4173_CARDU1_PCI_CLOCK:
			clear_cmusrst(CARD1RST);
			break;
		case VRC4173_CARDU2_PCI_CLOCK:
			clear_cmusrst(CARD2RST);
			break;
		case VRC4173_AC97U_PCI_CLOCK:
			clear_cmusrst(AC97RST);
			break;
		default:
			break;
		}

		spin_unlock_irq(&vrc4173_cmu_lock);
	}
}

EXPORT_SYMBOL(vrc4173_mask_clock);

static inline void vrc4173_cmu_init(void)
{
	vrc4173_cmuclkmsk = vrc4173_inw(VRC4173_CMUCLKMSK);

	spin_lock_init(&vrc4173_cmu_lock);
}

void vrc4173_select_function(vrc4173_function_t function)
{
	if (vrc4173_initialized) {
		spin_lock_irq(&vrc4173_giu_lock);

		switch(function) {
		case PS2_CHANNEL1:
			vrc4173_selectreg |= SEL2;
			break;
		case PS2_CHANNEL2:
			vrc4173_selectreg |= SEL1;
			break;
		case TOUCHPANEL:
			vrc4173_selectreg &= SEL2 | SEL1 | SEL0;
			break;
		case KEYBOARD_8SCANLINES:
			vrc4173_selectreg &= SEL3 | SEL2 | SEL1;
			break;
		case KEYBOARD_10SCANLINES:
			vrc4173_selectreg &= SEL3 | SEL2;
			break;
		case KEYBOARD_12SCANLINES:
			vrc4173_selectreg &= SEL3;
			break;
		case GPIO_0_15PINS:
			vrc4173_selectreg |= SEL0;
			break;
		case GPIO_16_20PINS:
			vrc4173_selectreg |= SEL3;
			break;
		}

		vrc4173_outw(vrc4173_selectreg, VRC4173_SELECTREG);

		spin_unlock_irq(&vrc4173_giu_lock);
	}
}

EXPORT_SYMBOL(vrc4173_select_function);

static inline void vrc4173_giu_init(void)
{
	vrc4173_selectreg = vrc4173_inw(VRC4173_SELECTREG);

	spin_lock_init(&vrc4173_giu_lock);
}

void vrc4173_enable_piuint(uint16_t mask)
{
	irq_desc_t *desc = irq_desc + VRC4173_PIU_IRQ;
	unsigned long flags;
	uint16_t val;

	spin_lock_irqsave(&desc->lock, flags);
	val = vrc4173_inw(VRC4173_MPIUINTREG);
	val |= mask;
	vrc4173_outw(val, VRC4173_MPIUINTREG);
	spin_unlock_irqrestore(&desc->lock, flags);
}

EXPORT_SYMBOL(vrc4173_enable_piuint);

void vrc4173_disable_piuint(uint16_t mask)
{
	irq_desc_t *desc = irq_desc + VRC4173_PIU_IRQ;
	unsigned long flags;
	uint16_t val;

	spin_lock_irqsave(&desc->lock, flags);
	val = vrc4173_inw(VRC4173_MPIUINTREG);
	val &= ~mask;
	vrc4173_outw(val, VRC4173_MPIUINTREG);
	spin_unlock_irqrestore(&desc->lock, flags);
}

EXPORT_SYMBOL(vrc4173_disable_piuint);

void vrc4173_enable_aiuint(uint16_t mask)
{
	irq_desc_t *desc = irq_desc + VRC4173_AIU_IRQ;
	unsigned long flags;
	uint16_t val;

	spin_lock_irqsave(&desc->lock, flags);
	val = vrc4173_inw(VRC4173_MAIUINTREG);
	val |= mask;
	vrc4173_outw(val, VRC4173_MAIUINTREG);
	spin_unlock_irqrestore(&desc->lock, flags);
}

EXPORT_SYMBOL(vrc4173_enable_aiuint);

void vrc4173_disable_aiuint(uint16_t mask)
{
	irq_desc_t *desc = irq_desc + VRC4173_AIU_IRQ;
	unsigned long flags;
	uint16_t val;

	spin_lock_irqsave(&desc->lock, flags);
	val = vrc4173_inw(VRC4173_MAIUINTREG);
	val &= ~mask;
	vrc4173_outw(val, VRC4173_MAIUINTREG);
	spin_unlock_irqrestore(&desc->lock, flags);
}

EXPORT_SYMBOL(vrc4173_disable_aiuint);

void vrc4173_enable_kiuint(uint16_t mask)
{
	irq_desc_t *desc = irq_desc + VRC4173_KIU_IRQ;
	unsigned long flags;
	uint16_t val;

	spin_lock_irqsave(&desc->lock, flags);
	val = vrc4173_inw(VRC4173_MKIUINTREG);
	val |= mask;
	vrc4173_outw(val, VRC4173_MKIUINTREG);
	spin_unlock_irqrestore(&desc->lock, flags);
}

EXPORT_SYMBOL(vrc4173_enable_kiuint);

void vrc4173_disable_kiuint(uint16_t mask)
{
	irq_desc_t *desc = irq_desc + VRC4173_KIU_IRQ;
	unsigned long flags;
	uint16_t val;

	spin_lock_irqsave(&desc->lock, flags);
	val = vrc4173_inw(VRC4173_MKIUINTREG);
	val &= ~mask;
	vrc4173_outw(val, VRC4173_MKIUINTREG);
	spin_unlock_irqrestore(&desc->lock, flags);
}

EXPORT_SYMBOL(vrc4173_disable_kiuint);

static void enable_vrc4173_irq(unsigned int irq)
{
	uint16_t val;

	val = vrc4173_inw(VRC4173_MSYSINT1REG);
	val |= (uint16_t)1 << (irq - VRC4173_IRQ_BASE);
	vrc4173_outw(val, VRC4173_MSYSINT1REG);
}

static void disable_vrc4173_irq(unsigned int irq)
{
	uint16_t val;

	val = vrc4173_inw(VRC4173_MSYSINT1REG);
	val &= ~((uint16_t)1 << (irq - VRC4173_IRQ_BASE));
	vrc4173_outw(val, VRC4173_MSYSINT1REG);
}

static unsigned int startup_vrc4173_irq(unsigned int irq)
{
	enable_vrc4173_irq(irq);
	return 0; /* never anything pending */
}

#define shutdown_vrc4173_irq	disable_vrc4173_irq
#define ack_vrc4173_irq		disable_vrc4173_irq

static void end_vrc4173_irq(unsigned int irq)
{
	if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
		enable_vrc4173_irq(irq);
}

static struct hw_interrupt_type vrc4173_irq_type = {
	.typename	= "VRC4173",
	.startup	= startup_vrc4173_irq,
	.shutdown	= shutdown_vrc4173_irq,
	.enable		= enable_vrc4173_irq,
	.disable	= disable_vrc4173_irq,
	.ack		= ack_vrc4173_irq,
	.end		= end_vrc4173_irq,
};

static int vrc4173_get_irq_number(int irq)
{
	uint16_t status, mask;
	int i;

        status = vrc4173_inw(VRC4173_SYSINT1REG);
        mask = vrc4173_inw(VRC4173_MSYSINT1REG);

	status &= mask;
	if (status) {
		for (i = 0; i < 16; i++)
			if (status & (0x0001 << i))
				return VRC4173_IRQ(i);
	}

	return -EINVAL;
}

static inline int vrc4173_icu_init(int cascade_irq)
{
	int i;

	if (cascade_irq < GIU_IRQ(0) || cascade_irq > GIU_IRQ(15))
		return -EINVAL;

	vrc4173_outw(0, VRC4173_MSYSINT1REG);

	vr41xx_set_irq_trigger(GIU_IRQ_TO_PIN(cascade_irq), TRIGGER_LEVEL, SIGNAL_THROUGH);
	vr41xx_set_irq_level(GIU_IRQ_TO_PIN(cascade_irq), LEVEL_LOW);

	for (i = VRC4173_IRQ_BASE; i <= VRC4173_IRQ_LAST; i++)
                irq_desc[i].handler = &vrc4173_irq_type;

	return 0;
}

static int __devinit vrc4173_probe(struct pci_dev *dev,
                                   const struct pci_device_id *id)
{
	unsigned long start, flags;
	int err;

	err = pci_enable_device(dev);
	if (err < 0) {
		printk(KERN_ERR "vrc4173: Failed to enable PCI device, aborting\n");
		return err;
	}

	pci_set_master(dev);

	start = pci_resource_start(dev, 0);
	if (start == 0) {
		printk(KERN_ERR "vrc4173:No such PCI I/O resource, aborting\n");
		return -ENXIO;
	}

	flags = pci_resource_flags(dev, 0);
	if ((flags & IORESOURCE_IO) == 0) {
		printk(KERN_ERR "vrc4173: No such PCI I/O resource, aborting\n");
		return -ENXIO;
	}

	err = pci_request_regions(dev, "NEC VRC4173");
	if (err < 0) {
		printk(KERN_ERR "vrc4173: PCI resources are busy, aborting\n");
		return err;
	}

	set_vrc4173_io_offset(start);

	vrc4173_cmu_init();
	vrc4173_giu_init();

	err = vrc4173_icu_init(dev->irq);
	if (err < 0) {
		printk(KERN_ERR "vrc4173: Invalid IRQ %d, aborting\n", dev->irq);
		return err;
	}

	err = vr41xx_cascade_irq(dev->irq, vrc4173_get_irq_number);
	if (err < 0) {
		printk(KERN_ERR "vrc4173: IRQ resource %d is busy, aborting\n", dev->irq);
		return err;
	}

	printk(KERN_INFO
	       "NEC VRC4173 at 0x%#08lx, IRQ is cascaded to %d\n", start, dev->irq);

	return 0;
}

static void vrc4173_remove(struct pci_dev *dev)
{
	free_irq(dev->irq, NULL);

	pci_release_regions(dev);
}

static struct pci_driver vrc4173_driver = {
	.name		= "NEC VRC4173",
	.probe		= vrc4173_probe,
	.remove		= vrc4173_remove,
	.id_table	= vrc4173_id_table,
};

static int __devinit vrc4173_init(void)
{
	int err;

	err = pci_module_init(&vrc4173_driver);
	if (err < 0)
		return err;

	vrc4173_initialized = 1;

	return 0;
}

static void __devexit vrc4173_exit(void)
{
	vrc4173_initialized = 0;

	pci_unregister_driver(&vrc4173_driver);
}

module_init(vrc4173_init);
module_exit(vrc4173_exit);