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#include <linux/module.h>
67#include <linux/sched.h>
68#include <linux/slab.h>
69#include <linux/ioport.h>
70#include <linux/errno.h>
71#include <linux/kernel.h>
72#include <linux/fs.h>
73#include <linux/string.h>
74#include <linux/poll.h>
75#include <linux/init.h>
76#include <linux/interrupt.h>
77#include <linux/spinlock.h>
78
79#include <asm/etraxgpio.h>
80#include <asm/arch/hwregs/reg_map.h>
81#include <asm/arch/hwregs/reg_rdwr.h>
82#include <asm/arch/hwregs/gio_defs.h>
83#include <asm/arch/hwregs/intr_vect_defs.h>
84#include <asm/io.h>
85#include <asm/system.h>
86#include <asm/irq.h>
87
88
89
90
91
92
93
94
95
96
97#define GPIO_MAJOR 120
98
99#define D(x)
100
101#if 0
102static int dp_cnt;
103#define DP(x) do { dp_cnt++; if (dp_cnt % 1000 == 0) x; }while(0)
104#else
105#define DP(x)
106#endif
107
108static char gpio_name[] = "etrax gpio";
109
110#if 0
111static wait_queue_head_t *gpio_wq;
112#endif
113
114static int gpio_ioctl(struct inode *inode, struct file *file,
115 unsigned int cmd, unsigned long arg);
116static ssize_t gpio_write(struct file * file, const char * buf, size_t count,
117 loff_t *off);
118static int gpio_open(struct inode *inode, struct file *filp);
119static int gpio_release(struct inode *inode, struct file *filp);
120static unsigned int gpio_poll(struct file *filp, struct poll_table_struct *wait);
121
122
123
124struct gpio_private {
125 struct gpio_private *next;
126
127 unsigned char clk_mask;
128 unsigned char data_mask;
129 unsigned char write_msb;
130 unsigned char pad1;
131
132 unsigned long highalarm, lowalarm;
133 wait_queue_head_t alarm_wq;
134 int minor;
135};
136
137
138
139static struct gpio_private *alarmlist = 0;
140
141static int gpio_some_alarms = 0;
142static unsigned long gpio_pa_high_alarms = 0;
143static unsigned long gpio_pa_low_alarms = 0;
144
145static DEFINE_SPINLOCK(alarm_lock);
146
147#define NUM_PORTS (GPIO_MINOR_LAST+1)
148#define GIO_REG_RD_ADDR(reg) (volatile unsigned long*) (regi_gio + REG_RD_ADDR_gio_##reg )
149#define GIO_REG_WR_ADDR(reg) (volatile unsigned long*) (regi_gio + REG_RD_ADDR_gio_##reg )
150unsigned long led_dummy;
151
152static volatile unsigned long *data_out[NUM_PORTS] = {
153 GIO_REG_WR_ADDR(rw_pa_dout),
154 GIO_REG_WR_ADDR(rw_pb_dout),
155 &led_dummy,
156 GIO_REG_WR_ADDR(rw_pc_dout),
157 GIO_REG_WR_ADDR(rw_pd_dout),
158 GIO_REG_WR_ADDR(rw_pe_dout),
159};
160
161static volatile unsigned long *data_in[NUM_PORTS] = {
162 GIO_REG_RD_ADDR(r_pa_din),
163 GIO_REG_RD_ADDR(r_pb_din),
164 &led_dummy,
165 GIO_REG_RD_ADDR(r_pc_din),
166 GIO_REG_RD_ADDR(r_pd_din),
167 GIO_REG_RD_ADDR(r_pe_din),
168};
169
170static unsigned long changeable_dir[NUM_PORTS] = {
171 CONFIG_ETRAX_PA_CHANGEABLE_DIR,
172 CONFIG_ETRAX_PB_CHANGEABLE_DIR,
173 0,
174 CONFIG_ETRAX_PC_CHANGEABLE_DIR,
175 CONFIG_ETRAX_PD_CHANGEABLE_DIR,
176 CONFIG_ETRAX_PE_CHANGEABLE_DIR,
177};
178
179static unsigned long changeable_bits[NUM_PORTS] = {
180 CONFIG_ETRAX_PA_CHANGEABLE_BITS,
181 CONFIG_ETRAX_PB_CHANGEABLE_BITS,
182 0,
183 CONFIG_ETRAX_PC_CHANGEABLE_BITS,
184 CONFIG_ETRAX_PD_CHANGEABLE_BITS,
185 CONFIG_ETRAX_PE_CHANGEABLE_BITS,
186};
187
188static volatile unsigned long *dir_oe[NUM_PORTS] = {
189 GIO_REG_WR_ADDR(rw_pa_oe),
190 GIO_REG_WR_ADDR(rw_pb_oe),
191 &led_dummy,
192 GIO_REG_WR_ADDR(rw_pc_oe),
193 GIO_REG_WR_ADDR(rw_pd_oe),
194 GIO_REG_WR_ADDR(rw_pe_oe),
195};
196
197
198
199static unsigned int
200gpio_poll(struct file *file,
201 poll_table *wait)
202{
203 unsigned int mask = 0;
204 struct gpio_private *priv = (struct gpio_private *)file->private_data;
205 unsigned long data;
206 poll_wait(file, &priv->alarm_wq, wait);
207 if (priv->minor == GPIO_MINOR_A) {
208 reg_gio_rw_intr_cfg intr_cfg;
209 unsigned long tmp;
210 unsigned long flags;
211
212 local_irq_save(flags);
213 data = REG_TYPE_CONV(unsigned long, reg_gio_r_pa_din, REG_RD(gio, regi_gio, r_pa_din));
214
215
216
217 intr_cfg = REG_RD(gio, regi_gio, rw_intr_cfg);
218
219 tmp = ~data & priv->highalarm & 0xFF;
220 if (tmp & (1 << 0)) {
221 intr_cfg.pa0 = regk_gio_hi;
222 }
223 if (tmp & (1 << 1)) {
224 intr_cfg.pa1 = regk_gio_hi;
225 }
226 if (tmp & (1 << 2)) {
227 intr_cfg.pa2 = regk_gio_hi;
228 }
229 if (tmp & (1 << 3)) {
230 intr_cfg.pa3 = regk_gio_hi;
231 }
232 if (tmp & (1 << 4)) {
233 intr_cfg.pa4 = regk_gio_hi;
234 }
235 if (tmp & (1 << 5)) {
236 intr_cfg.pa5 = regk_gio_hi;
237 }
238 if (tmp & (1 << 6)) {
239 intr_cfg.pa6 = regk_gio_hi;
240 }
241 if (tmp & (1 << 7)) {
242 intr_cfg.pa7 = regk_gio_hi;
243 }
244
245
246
247 tmp = data & priv->lowalarm & 0xFF;
248 if (tmp & (1 << 0)) {
249 intr_cfg.pa0 = regk_gio_lo;
250 }
251 if (tmp & (1 << 1)) {
252 intr_cfg.pa1 = regk_gio_lo;
253 }
254 if (tmp & (1 << 2)) {
255 intr_cfg.pa2 = regk_gio_lo;
256 }
257 if (tmp & (1 << 3)) {
258 intr_cfg.pa3 = regk_gio_lo;
259 }
260 if (tmp & (1 << 4)) {
261 intr_cfg.pa4 = regk_gio_lo;
262 }
263 if (tmp & (1 << 5)) {
264 intr_cfg.pa5 = regk_gio_lo;
265 }
266 if (tmp & (1 << 6)) {
267 intr_cfg.pa6 = regk_gio_lo;
268 }
269 if (tmp & (1 << 7)) {
270 intr_cfg.pa7 = regk_gio_lo;
271 }
272
273 REG_WR(gio, regi_gio, rw_intr_cfg, intr_cfg);
274 local_irq_restore(flags);
275 } else if (priv->minor <= GPIO_MINOR_E)
276 data = *data_in[priv->minor];
277 else
278 return 0;
279
280 if ((data & priv->highalarm) ||
281 (~data & priv->lowalarm)) {
282 mask = POLLIN|POLLRDNORM;
283 }
284
285 DP(printk("gpio_poll ready: mask 0x%08X\n", mask));
286 return mask;
287}
288
289int etrax_gpio_wake_up_check(void)
290{
291 struct gpio_private *priv = alarmlist;
292 unsigned long data = 0;
293 int ret = 0;
294 while (priv) {
295 data = *data_in[priv->minor];
296 if ((data & priv->highalarm) ||
297 (~data & priv->lowalarm)) {
298 DP(printk("etrax_gpio_wake_up_check %i\n",priv->minor));
299 wake_up_interruptible(&priv->alarm_wq);
300 ret = 1;
301 }
302 priv = priv->next;
303 }
304 return ret;
305}
306
307static irqreturn_t
308gpio_poll_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
309{
310 if (gpio_some_alarms) {
311 return IRQ_RETVAL(etrax_gpio_wake_up_check());
312 }
313 return IRQ_NONE;
314}
315
316static irqreturn_t
317gpio_pa_interrupt(int irq, void *dev_id, struct pt_regs *regs)
318{
319 reg_gio_rw_intr_mask intr_mask;
320 reg_gio_r_masked_intr masked_intr;
321 reg_gio_rw_ack_intr ack_intr;
322 unsigned long tmp;
323 unsigned long tmp2;
324
325
326 masked_intr = REG_RD(gio, regi_gio, r_masked_intr);
327 tmp = REG_TYPE_CONV(unsigned long, reg_gio_r_masked_intr, masked_intr);
328
329
330 spin_lock(&alarm_lock);
331 tmp &= (gpio_pa_high_alarms | gpio_pa_low_alarms);
332 spin_unlock(&alarm_lock);
333
334
335 ack_intr = REG_TYPE_CONV(reg_gio_rw_ack_intr, unsigned long, tmp);
336 REG_WR(gio, regi_gio, rw_ack_intr, ack_intr);
337
338
339 intr_mask = REG_RD(gio, regi_gio, rw_intr_mask);
340 tmp2 = REG_TYPE_CONV(unsigned long, reg_gio_rw_intr_mask, intr_mask);
341 tmp2 &= ~tmp;
342 intr_mask = REG_TYPE_CONV(reg_gio_rw_intr_mask, unsigned long, tmp2);
343 REG_WR(gio, regi_gio, rw_intr_mask, intr_mask);
344
345 if (gpio_some_alarms) {
346 return IRQ_RETVAL(etrax_gpio_wake_up_check());
347 }
348 return IRQ_NONE;
349}
350
351
352static ssize_t gpio_write(struct file * file, const char * buf, size_t count,
353 loff_t *off)
354{
355 struct gpio_private *priv = (struct gpio_private *)file->private_data;
356 unsigned char data, clk_mask, data_mask, write_msb;
357 unsigned long flags;
358 unsigned long shadow;
359 volatile unsigned long *port;
360 ssize_t retval = count;
361
362
363 if (priv->minor == GPIO_MINOR_LEDS) {
364 return -EFAULT;
365 }
366
367 if (!access_ok(VERIFY_READ, buf, count)) {
368 return -EFAULT;
369 }
370 clk_mask = priv->clk_mask;
371 data_mask = priv->data_mask;
372
373
374 if (clk_mask == 0 || data_mask == 0) {
375 return -EPERM;
376 }
377 write_msb = priv->write_msb;
378 D(printk("gpio_write: %lu to data 0x%02X clk 0x%02X msb: %i\n",count, data_mask, clk_mask, write_msb));
379 port = data_out[priv->minor];
380
381 while (count--) {
382 int i;
383 data = *buf++;
384 if (priv->write_msb) {
385 for (i = 7; i >= 0;i--) {
386 local_irq_save(flags);
387 shadow = *port;
388 *port = shadow &= ~clk_mask;
389 if (data & 1<<i)
390 *port = shadow |= data_mask;
391 else
392 *port = shadow &= ~data_mask;
393
394 *port = shadow |= clk_mask;
395 local_irq_restore(flags);
396 }
397 } else {
398 for (i = 0; i <= 7;i++) {
399 local_irq_save(flags);
400 shadow = *port;
401 *port = shadow &= ~clk_mask;
402 if (data & 1<<i)
403 *port = shadow |= data_mask;
404 else
405 *port = shadow &= ~data_mask;
406
407 *port = shadow |= clk_mask;
408 local_irq_restore(flags);
409 }
410 }
411 }
412 return retval;
413}
414
415
416
417static int
418gpio_open(struct inode *inode, struct file *filp)
419{
420 struct gpio_private *priv;
421 int p = iminor(inode);
422
423 if (p > GPIO_MINOR_LAST)
424 return -EINVAL;
425
426 priv = kmalloc(sizeof(struct gpio_private),
427 GFP_KERNEL);
428
429 if (!priv)
430 return -ENOMEM;
431
432 priv->minor = p;
433
434
435
436 priv->next = alarmlist;
437 alarmlist = priv;
438 priv->clk_mask = 0;
439 priv->data_mask = 0;
440 priv->highalarm = 0;
441 priv->lowalarm = 0;
442 init_waitqueue_head(&priv->alarm_wq);
443
444 filp->private_data = (void *)priv;
445
446 return 0;
447}
448
449static int
450gpio_release(struct inode *inode, struct file *filp)
451{
452 struct gpio_private *p = alarmlist;
453 struct gpio_private *todel = (struct gpio_private *)filp->private_data;
454
455 unsigned long a_high, a_low;
456 unsigned long some_alarms;
457
458
459
460 if (p == todel) {
461 alarmlist = todel->next;
462 } else {
463 while (p->next != todel)
464 p = p->next;
465 p->next = todel->next;
466 }
467
468 kfree(todel);
469
470 p = alarmlist;
471 some_alarms = 0;
472 a_high = 0;
473 a_low = 0;
474 while (p) {
475 if (p->minor == GPIO_MINOR_A) {
476 a_high |= p->highalarm;
477 a_low |= p->lowalarm;
478 }
479
480 if (p->highalarm | p->lowalarm) {
481 some_alarms = 1;
482 }
483 p = p->next;
484 }
485
486 spin_lock(&alarm_lock);
487 gpio_some_alarms = some_alarms;
488 gpio_pa_high_alarms = a_high;
489 gpio_pa_low_alarms = a_low;
490 spin_unlock(&alarm_lock);
491
492 return 0;
493}
494
495
496
497
498
499unsigned long inline setget_input(struct gpio_private *priv, unsigned long arg)
500{
501
502
503
504 unsigned long flags;
505 unsigned long dir_shadow;
506
507 local_irq_save(flags);
508 dir_shadow = *dir_oe[priv->minor];
509 dir_shadow &= ~(arg & changeable_dir[priv->minor]);
510 *dir_oe[priv->minor] = dir_shadow;
511 local_irq_restore(flags);
512
513 if (priv->minor == GPIO_MINOR_A)
514 dir_shadow ^= 0xFF;
515 else
516 dir_shadow ^= 0x3FFFF;
517 return dir_shadow;
518
519}
520
521unsigned long inline setget_output(struct gpio_private *priv, unsigned long arg)
522{
523 unsigned long flags;
524 unsigned long dir_shadow;
525
526 local_irq_save(flags);
527 dir_shadow = *dir_oe[priv->minor];
528 dir_shadow |= (arg & changeable_dir[priv->minor]);
529 *dir_oe[priv->minor] = dir_shadow;
530 local_irq_restore(flags);
531 return dir_shadow;
532}
533
534static int
535gpio_leds_ioctl(unsigned int cmd, unsigned long arg);
536
537static int
538gpio_ioctl(struct inode *inode, struct file *file,
539 unsigned int cmd, unsigned long arg)
540{
541 unsigned long flags;
542 unsigned long val;
543 unsigned long shadow;
544 struct gpio_private *priv = (struct gpio_private *)file->private_data;
545 if (_IOC_TYPE(cmd) != ETRAXGPIO_IOCTYPE) {
546 return -EINVAL;
547 }
548
549 switch (_IOC_NR(cmd)) {
550 case IO_READBITS:
551
552 return *data_in[priv->minor];
553 break;
554 case IO_SETBITS:
555 local_irq_save(flags);
556 if (arg & 0x04)
557 printk("GPIO SET 2\n");
558
559 shadow = *data_out[priv->minor];
560 shadow |= (arg & changeable_bits[priv->minor]);
561 *data_out[priv->minor] = shadow;
562 local_irq_restore(flags);
563 break;
564 case IO_CLRBITS:
565 local_irq_save(flags);
566 if (arg & 0x04)
567 printk("GPIO CLR 2\n");
568
569 shadow = *data_out[priv->minor];
570 shadow &= ~(arg & changeable_bits[priv->minor]);
571 *data_out[priv->minor] = shadow;
572 local_irq_restore(flags);
573 break;
574 case IO_HIGHALARM:
575
576 priv->highalarm |= arg;
577 spin_lock(&alarm_lock);
578 gpio_some_alarms = 1;
579 if (priv->minor == GPIO_MINOR_A) {
580 gpio_pa_high_alarms |= arg;
581 }
582 spin_unlock(&alarm_lock);
583 break;
584 case IO_LOWALARM:
585
586 priv->lowalarm |= arg;
587 spin_lock(&alarm_lock);
588 gpio_some_alarms = 1;
589 if (priv->minor == GPIO_MINOR_A) {
590 gpio_pa_low_alarms |= arg;
591 }
592 spin_unlock(&alarm_lock);
593 break;
594 case IO_CLRALARM:
595
596 priv->highalarm &= ~arg;
597 priv->lowalarm &= ~arg;
598 spin_lock(&alarm_lock);
599 if (priv->minor == GPIO_MINOR_A) {
600 if (gpio_pa_high_alarms & arg ||
601 gpio_pa_low_alarms & arg) {
602
603 }
604 }
605 spin_unlock(&alarm_lock);
606 break;
607 case IO_READDIR:
608
609 return *dir_oe[priv->minor];
610 case IO_SETINPUT:
611
612
613
614 return setget_input(priv, arg);
615 break;
616 case IO_SETOUTPUT:
617
618
619
620 return setget_output(priv, arg);
621
622 case IO_CFG_WRITE_MODE:
623 {
624 unsigned long dir_shadow;
625 dir_shadow = *dir_oe[priv->minor];
626
627 priv->clk_mask = arg & 0xFF;
628 priv->data_mask = (arg >> 8) & 0xFF;
629 priv->write_msb = (arg >> 16) & 0x01;
630
631
632
633 if (!((priv->clk_mask & changeable_bits[priv->minor]) &&
634 (priv->data_mask & changeable_bits[priv->minor]) &&
635 (priv->clk_mask & dir_shadow) &&
636 (priv->data_mask & dir_shadow)))
637 {
638 priv->clk_mask = 0;
639 priv->data_mask = 0;
640 return -EPERM;
641 }
642 break;
643 }
644 case IO_READ_INBITS:
645
646 val = *data_in[priv->minor];
647 if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
648 return -EFAULT;
649 return 0;
650 break;
651 case IO_READ_OUTBITS:
652
653 val = *data_out[priv->minor];
654 if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
655 return -EFAULT;
656 break;
657 case IO_SETGET_INPUT:
658
659
660
661 if (copy_from_user(&val, (unsigned long*)arg, sizeof(val)))
662 return -EFAULT;
663 val = setget_input(priv, val);
664 if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
665 return -EFAULT;
666 break;
667 case IO_SETGET_OUTPUT:
668
669
670
671 if (copy_from_user(&val, (unsigned long*)arg, sizeof(val)))
672 return -EFAULT;
673 val = setget_output(priv, val);
674 if (copy_to_user((unsigned long*)arg, &val, sizeof(val)))
675 return -EFAULT;
676 break;
677 default:
678 if (priv->minor == GPIO_MINOR_LEDS)
679 return gpio_leds_ioctl(cmd, arg);
680 else
681 return -EINVAL;
682 }
683
684 return 0;
685}
686
687static int
688gpio_leds_ioctl(unsigned int cmd, unsigned long arg)
689{
690 unsigned char green;
691 unsigned char red;
692
693 switch (_IOC_NR(cmd)) {
694 case IO_LEDACTIVE_SET:
695 green = ((unsigned char) arg) & 1;
696 red = (((unsigned char) arg) >> 1) & 1;
697 LED_ACTIVE_SET_G(green);
698 LED_ACTIVE_SET_R(red);
699 break;
700
701 default:
702 return -EINVAL;
703 }
704
705 return 0;
706}
707
708const struct file_operations gpio_fops = {
709 .owner = THIS_MODULE,
710 .poll = gpio_poll,
711 .ioctl = gpio_ioctl,
712 .write = gpio_write,
713 .open = gpio_open,
714 .release = gpio_release,
715};
716
717
718
719
720static __init int
721gpio_init(void)
722{
723 int res;
724 reg_intr_vect_rw_mask intr_mask;
725
726
727
728 res = register_chrdev(GPIO_MAJOR, gpio_name, &gpio_fops);
729 if (res < 0) {
730 printk(KERN_ERR "gpio: couldn't get a major number.\n");
731 return res;
732 }
733
734
735 LED_NETWORK_SET(0);
736 LED_ACTIVE_SET(0);
737 LED_DISK_READ(0);
738 LED_DISK_WRITE(0);
739
740 printk("ETRAX FS GPIO driver v2.5, (c) 2003-2005 Axis Communications AB\n");
741
742
743
744
745
746 if (request_irq(TIMER_INTR_VECT, gpio_poll_timer_interrupt,
747 IRQF_SHARED | IRQF_DISABLED,"gpio poll", &alarmlist)) {
748 printk("err: timer0 irq for gpio\n");
749 }
750 if (request_irq(GEN_IO_INTR_VECT, gpio_pa_interrupt,
751 IRQF_SHARED | IRQF_DISABLED,"gpio PA", &alarmlist)) {
752 printk("err: PA irq for gpio\n");
753 }
754
755 intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
756 intr_mask.timer = 1;
757 intr_mask.gen_io = 1;
758 REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
759
760 return res;
761}
762
763
764
765module_init(gpio_init);
766