1 /*
2  * arch/mips/vr41xx/common/vrc4173.c
3  *
4  * NEC VRC4173 driver for NEC VR4122/VR4131.
5  *
6  * Author: Yoichi Yuasa <yyuasa@mvista.com, or source@mvista.com>
7  *
8  * 2001-2003 (c) MontaVista, Software, Inc. This file is licensed under
9  * the terms of the GNU General Public License version 2. This program
10  * is licensed "as is" without any warranty of any kind, whether express
11  * or implied.
12  */
13 #include <linux/config.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/pci.h>
19 #include <linux/spinlock.h>
20 #include <linux/types.h>
21 
22 #include <asm/vr41xx/vr41xx.h>
23 #include <asm/vr41xx/vrc4173.h>
24 
25 MODULE_DESCRIPTION("NEC VRC4173 driver for NEC VR4122/4131");
26 MODULE_AUTHOR("Yoichi Yuasa <yyuasa@mvista.com>");
27 MODULE_LICENSE("GPL");
28 
29 EXPORT_SYMBOL(vrc4173_io_offset);
30 EXPORT_SYMBOL(vrc4173_supply_clock);
31 EXPORT_SYMBOL(vrc4173_mask_clock);
32 EXPORT_SYMBOL(vrc4173_select_function);
33 
34 #define VRC4173_CMUCLKMSK	0x040
35  #define MSKPIU			0x0001
36  #define MSKKIU			0x0002
37  #define MSKAIU			0x0004
38  #define MSKPS2CH1		0x0008
39  #define MSKPS2CH2		0x0010
40  #define MSKUSB			0x0020
41  #define MSKCARD1		0x0040
42  #define MSKCARD2		0x0080
43  #define MSKAC97		0x0100
44  #define MSK48MUSB		0x0400
45  #define MSK48MPIN		0x0800
46  #define MSK48MOSC		0x1000
47 #define VRC4173_CMUSRST		0x042
48  #define USBRST			0x0001
49  #define CARD1RST		0x0002
50  #define CARD2RST		0x0004
51  #define AC97RST		0x0008
52 
53 #define VRC4173_SYSINT1REG	0x060
54 #define VRC4173_MSYSINT1REG	0x06c
55 
56 #define VRC4173_SELECTREG	0x09e
57 
58 static struct pci_device_id vrc4173_table[] __devinitdata = {
59 	{	.vendor		= PCI_VENDOR_ID_NEC,
60 		.device		= PCI_DEVICE_ID_NEC_VRC4173,
61 		.subvendor	= PCI_ANY_ID,
62 		.subdevice	= PCI_ANY_ID,			},
63 	{	.vendor		= 0,				},
64 };
65 
66 unsigned long vrc4173_io_offset = 0;
67 
68 static int vrc4173_initialized;
69 static uint16_t vrc4173_cmuclkmsk;
70 static uint16_t vrc4173_selectreg;
71 static spinlock_t vrc4173_cmu_lock;
72 static spinlock_t vrc4173_giu_lock;
73 
set_cmusrst(uint16_t val)74 static inline void set_cmusrst(uint16_t val)
75 {
76 	uint16_t cmusrst;
77 
78 	cmusrst = vrc4173_inw(VRC4173_CMUSRST);
79 	cmusrst |= val;
80 	vrc4173_outw(cmusrst, VRC4173_CMUSRST);
81 }
82 
clear_cmusrst(uint16_t val)83 static inline void clear_cmusrst(uint16_t val)
84 {
85 	uint16_t cmusrst;
86 
87 	cmusrst = vrc4173_inw(VRC4173_CMUSRST);
88 	cmusrst &= ~val;
89 	vrc4173_outw(cmusrst, VRC4173_CMUSRST);
90 }
91 
vrc4173_supply_clock(unsigned int clock)92 void vrc4173_supply_clock(unsigned int clock)
93 {
94 	if (vrc4173_initialized) {
95 		spin_lock_irq(&vrc4173_cmu_lock);
96 
97 		switch (clock) {
98 		case VRC4173_PIU_CLOCK:
99 			vrc4173_cmuclkmsk |= MSKPIU;
100 			break;
101 		case VRC4173_KIU_CLOCK:
102 			vrc4173_cmuclkmsk |= MSKKIU;
103 			break;
104 		case VRC4173_AIU_CLOCK:
105 			vrc4173_cmuclkmsk |= MSKAIU;
106 			break;
107 		case VRC4173_PS2_CH1_CLOCK:
108 			vrc4173_cmuclkmsk |= MSKPS2CH1;
109 			break;
110 		case VRC4173_PS2_CH2_CLOCK:
111 			vrc4173_cmuclkmsk |= MSKPS2CH2;
112 			break;
113 		case VRC4173_USBU_PCI_CLOCK:
114 			set_cmusrst(USBRST);
115 			vrc4173_cmuclkmsk |= MSKUSB;
116 			break;
117 		case VRC4173_CARDU1_PCI_CLOCK:
118 			set_cmusrst(CARD1RST);
119 			vrc4173_cmuclkmsk |= MSKCARD1;
120 			break;
121 		case VRC4173_CARDU2_PCI_CLOCK:
122 			set_cmusrst(CARD2RST);
123 			vrc4173_cmuclkmsk |= MSKCARD2;
124 			break;
125 		case VRC4173_AC97U_PCI_CLOCK:
126 			set_cmusrst(AC97RST);
127 			vrc4173_cmuclkmsk |= MSKAC97;
128 			break;
129 		case VRC4173_USBU_48MHz_CLOCK:
130 			set_cmusrst(USBRST);
131 			vrc4173_cmuclkmsk |= MSK48MUSB;
132 			break;
133 		case VRC4173_EXT_48MHz_CLOCK:
134 			if (vrc4173_cmuclkmsk & MSK48MOSC)
135 				vrc4173_cmuclkmsk |= MSK48MPIN;
136 			else
137 				printk(KERN_WARNING
138 				       "vrc4173_supply_clock: "
139 				       "Please supply VRC4173_48MHz_CLOCK first "
140 				       "rather than VRC4173_EXT_48MHz_CLOCK.\n");
141 			break;
142 		case VRC4173_48MHz_CLOCK:
143 			vrc4173_cmuclkmsk |= MSK48MOSC;
144 			break;
145 		default:
146 			printk(KERN_WARNING
147 			       "vrc4173_supply_clock: Invalid CLOCK value %u\n", clock);
148 			break;
149 		}
150 
151 		vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
152 
153 		switch (clock) {
154 		case VRC4173_USBU_PCI_CLOCK:
155 		case VRC4173_USBU_48MHz_CLOCK:
156 			clear_cmusrst(USBRST);
157 			break;
158 		case VRC4173_CARDU1_PCI_CLOCK:
159 			clear_cmusrst(CARD1RST);
160 			break;
161 		case VRC4173_CARDU2_PCI_CLOCK:
162 			clear_cmusrst(CARD2RST);
163 			break;
164 		case VRC4173_AC97U_PCI_CLOCK:
165 			clear_cmusrst(AC97RST);
166 			break;
167 		default:
168 			break;
169 		}
170 
171 		spin_unlock_irq(&vrc4173_cmu_lock);
172 	}
173 }
174 
vrc4173_mask_clock(unsigned int clock)175 void vrc4173_mask_clock(unsigned int clock)
176 {
177 	if (vrc4173_initialized) {
178 		spin_lock_irq(&vrc4173_cmu_lock);
179 
180 		switch (clock) {
181 		case VRC4173_PIU_CLOCK:
182 			vrc4173_cmuclkmsk &= ~MSKPIU;
183 			break;
184 		case VRC4173_KIU_CLOCK:
185 			vrc4173_cmuclkmsk &= ~MSKKIU;
186 			break;
187 		case VRC4173_AIU_CLOCK:
188 			vrc4173_cmuclkmsk &= ~MSKAIU;
189 			break;
190 		case VRC4173_PS2_CH1_CLOCK:
191 			vrc4173_cmuclkmsk &= ~MSKPS2CH1;
192 			break;
193 		case VRC4173_PS2_CH2_CLOCK:
194 			vrc4173_cmuclkmsk &= ~MSKPS2CH2;
195 			break;
196 		case VRC4173_USBU_PCI_CLOCK:
197 			set_cmusrst(USBRST);
198 			vrc4173_cmuclkmsk &= ~MSKUSB;
199 			break;
200 		case VRC4173_CARDU1_PCI_CLOCK:
201 			set_cmusrst(CARD1RST);
202 			vrc4173_cmuclkmsk &= ~MSKCARD1;
203 			break;
204 		case VRC4173_CARDU2_PCI_CLOCK:
205 			set_cmusrst(CARD2RST);
206 			vrc4173_cmuclkmsk &= ~MSKCARD2;
207 			break;
208 		case VRC4173_AC97U_PCI_CLOCK:
209 			set_cmusrst(AC97RST);
210 			vrc4173_cmuclkmsk &= ~MSKAC97;
211 			break;
212 		case VRC4173_USBU_48MHz_CLOCK:
213 			set_cmusrst(USBRST);
214 			vrc4173_cmuclkmsk &= ~MSK48MUSB;
215 			break;
216 		case VRC4173_EXT_48MHz_CLOCK:
217 			vrc4173_cmuclkmsk &= ~MSK48MPIN;
218 			break;
219 		case VRC4173_48MHz_CLOCK:
220 			vrc4173_cmuclkmsk &= ~MSK48MOSC;
221 			break;
222 		default:
223 			printk(KERN_WARNING "vrc4173_mask_clock: Invalid CLOCK value %u\n", clock);
224 			break;
225 		}
226 
227 		vrc4173_outw(vrc4173_cmuclkmsk, VRC4173_CMUCLKMSK);
228 
229 		switch (clock) {
230 		case VRC4173_USBU_PCI_CLOCK:
231 		case VRC4173_USBU_48MHz_CLOCK:
232 			clear_cmusrst(USBRST);
233 			break;
234 		case VRC4173_CARDU1_PCI_CLOCK:
235 			clear_cmusrst(CARD1RST);
236 			break;
237 		case VRC4173_CARDU2_PCI_CLOCK:
238 			clear_cmusrst(CARD2RST);
239 			break;
240 		case VRC4173_AC97U_PCI_CLOCK:
241 			clear_cmusrst(AC97RST);
242 			break;
243 		default:
244 			break;
245 		}
246 
247 		spin_unlock_irq(&vrc4173_cmu_lock);
248 	}
249 }
250 
vrc4173_cmu_init(void)251 static inline void vrc4173_cmu_init(void)
252 {
253 	vrc4173_cmuclkmsk = vrc4173_inw(VRC4173_CMUCLKMSK);
254 
255 	spin_lock_init(&vrc4173_cmu_lock);
256 }
257 
vrc4173_select_function(int func)258 void vrc4173_select_function(int func)
259 {
260 	if (vrc4173_initialized) {
261 		spin_lock_irq(&vrc4173_giu_lock);
262 
263 		switch(func) {
264 		case PS2CH1_SELECT:
265 			vrc4173_selectreg |= 0x0004;
266 			break;
267 		case PS2CH2_SELECT:
268 			vrc4173_selectreg |= 0x0002;
269 			break;
270 		case TOUCHPANEL_SELECT:
271 			vrc4173_selectreg &= 0x0007;
272 			break;
273 		case KIU8_SELECT:
274 			vrc4173_selectreg &= 0x000e;
275 			break;
276 		case KIU10_SELECT:
277 			vrc4173_selectreg &= 0x000c;
278 			break;
279 		case KIU12_SELECT:
280 			vrc4173_selectreg &= 0x0008;
281 			break;
282 		case GPIO_SELECT:
283 			vrc4173_selectreg |= 0x0008;
284 			break;
285 		}
286 
287 		vrc4173_outw(vrc4173_selectreg, VRC4173_SELECTREG);
288 
289 		spin_unlock_irq(&vrc4173_giu_lock);
290 	}
291 }
292 
vrc4173_giu_init(void)293 static inline void vrc4173_giu_init(void)
294 {
295 	vrc4173_selectreg = vrc4173_inw(VRC4173_SELECTREG);
296 
297 	spin_lock_init(&vrc4173_giu_lock);
298 }
299 
enable_vrc4173_irq(unsigned int irq)300 static void enable_vrc4173_irq(unsigned int irq)
301 {
302 	uint16_t val;
303 
304 	val = vrc4173_inw(VRC4173_MSYSINT1REG);
305 	val |= (uint16_t)1 << (irq - VRC4173_IRQ_BASE);
306 	vrc4173_outw(val, VRC4173_MSYSINT1REG);
307 }
308 
disable_vrc4173_irq(unsigned int irq)309 static void disable_vrc4173_irq(unsigned int irq)
310 {
311 	uint16_t val;
312 
313 	val = vrc4173_inw(VRC4173_MSYSINT1REG);
314 	val &= ~((uint16_t)1 << (irq - VRC4173_IRQ_BASE));
315 	vrc4173_outw(val, VRC4173_MSYSINT1REG);
316 }
317 
startup_vrc4173_irq(unsigned int irq)318 static unsigned int startup_vrc4173_irq(unsigned int irq)
319 {
320 	enable_vrc4173_irq(irq);
321 	return 0; /* never anything pending */
322 }
323 
324 #define shutdown_vrc4173_irq	disable_vrc4173_irq
325 #define ack_vrc4173_irq		disable_vrc4173_irq
326 
end_vrc4173_irq(unsigned int irq)327 static void end_vrc4173_irq(unsigned int irq)
328 {
329 	if (!(irq_desc[irq].status & (IRQ_DISABLED | IRQ_INPROGRESS)))
330 		enable_vrc4173_irq(irq);
331 }
332 
333 static struct hw_interrupt_type vrc4173_irq_type = {
334 	.typename	= "VRC4173",
335 	.startup	= startup_vrc4173_irq,
336 	.shutdown	= shutdown_vrc4173_irq,
337 	.enable		= enable_vrc4173_irq,
338 	.disable	= disable_vrc4173_irq,
339 	.ack		= ack_vrc4173_irq,
340 	.end		= end_vrc4173_irq,
341 };
342 
vrc4173_get_irq_number(int irq)343 static int vrc4173_get_irq_number(int irq)
344 {
345 	uint16_t status, mask;
346 	int i;
347 
348         status = vrc4173_inw(VRC4173_SYSINT1REG);
349         mask = vrc4173_inw(VRC4173_MSYSINT1REG);
350 
351 	status &= mask;
352 	if (status) {
353 		for (i = 0; i < 16; i++)
354 			if (status & (0x0001 << i))
355 				return VRC4173_IRQ(i);
356 	}
357 
358 	return -EINVAL;
359 }
360 
vrc4173_icu_init(int cascade_irq)361 static inline int vrc4173_icu_init(int cascade_irq)
362 {
363 	int i;
364 
365 	if (cascade_irq < GIU_IRQ(0) || cascade_irq > GIU_IRQ(15))
366 		return -EINVAL;
367 
368 	vrc4173_outw(0, VRC4173_MSYSINT1REG);
369 
370 	vr41xx_set_irq_trigger(GIU_IRQ_TO_PIN(cascade_irq), TRIGGER_LEVEL, SIGNAL_THROUGH);
371 	vr41xx_set_irq_level(GIU_IRQ_TO_PIN(cascade_irq), LEVEL_LOW);
372 
373 	for (i = VRC4173_IRQ_BASE; i <= VRC4173_IRQ_LAST; i++)
374                 irq_desc[i].handler = &vrc4173_irq_type;
375 
376 	return 0;
377 }
378 
vrc4173_probe(struct pci_dev * dev,const struct pci_device_id * id)379 static int __devinit vrc4173_probe(struct pci_dev *dev,
380                                    const struct pci_device_id *id)
381 {
382 	unsigned long start, flags;
383 	int err;
384 
385 	err = pci_enable_device(dev);
386 	if (err < 0) {
387 		printk(KERN_ERR "vrc4173: Failed to enable PCI device, aborting\n");
388 		return err;
389 	}
390 
391 	pci_set_master(dev);
392 
393 	start = pci_resource_start(dev, 0);
394 	if (start == 0) {
395 		printk(KERN_ERR "vrc4173:No such PCI I/O resource, aborting\n");
396 		return -ENXIO;
397 	}
398 
399 	flags = pci_resource_flags(dev, 0);
400 	if ((flags & IORESOURCE_IO) == 0) {
401 		printk(KERN_ERR "vrc4173: No such PCI I/O resource, aborting\n");
402 		return -ENXIO;
403 	}
404 
405 	err = pci_request_regions(dev, "NEC VRC4173");
406 	if (err < 0) {
407 		printk(KERN_ERR "vrc4173: PCI resources are busy, aborting\n");
408 		return err;
409 	}
410 
411 	set_vrc4173_io_offset(start);
412 
413 	vrc4173_cmu_init();
414 	vrc4173_giu_init();
415 
416 	err = vrc4173_icu_init(dev->irq);
417 	if (err < 0) {
418 		printk(KERN_ERR "vrc4173: Invalid IRQ %d, aborting\n", dev->irq);
419 		return err;
420 	}
421 
422 	err = vr41xx_cascade_irq(dev->irq, vrc4173_get_irq_number);
423 	if (err < 0) {
424 		printk(KERN_ERR "vrc4173: IRQ resource %d is busy, aborting\n", dev->irq);
425 		return err;
426 	}
427 
428 	printk(KERN_INFO
429 	       "NEC VRC4173 at 0x%#08lx, IRQ is cascaded to %d\n", start, dev->irq);
430 
431 	return 0;
432 }
433 
vrc4173_remove(struct pci_dev * dev)434 static void vrc4173_remove(struct pci_dev *dev)
435 {
436 	free_irq(dev->irq, NULL);
437 
438 	pci_release_regions(dev);
439 }
440 
441 static struct pci_driver vrc4173_driver = {
442 	.name		= "NEC VRC4173",
443 	.probe		= vrc4173_probe,
444 	.remove		= vrc4173_remove,
445 	.id_table	= vrc4173_table,
446 };
447 
vrc4173_init(void)448 static int __devinit vrc4173_init(void)
449 {
450 	int err;
451 
452 	err = pci_module_init(&vrc4173_driver);
453 	if (err < 0)
454 		return err;
455 
456 	vrc4173_initialized = 1;
457 
458 	return 0;
459 }
460 
vrc4173_exit(void)461 static void __devexit vrc4173_exit(void)
462 {
463 	vrc4173_initialized = 0;
464 
465 	pci_unregister_driver(&vrc4173_driver);
466 }
467 
468 module_init(vrc4173_init);
469 module_exit(vrc4173_exit);
470