2 * Chassis LCD/LED driver for HP-PARISC workstations
4 * (c) Copyright 2000 Red Hat Software
5 * (c) Copyright 2000 Helge Deller <hdeller@redhat.com>
6 * (c) Copyright 2001-2003 Helge Deller <deller@gmx.de>
7 * (c) Copyright 2001 Randolph Chung <tausq@debian.org>
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.
15 * - speed-up calculations with inlined assembler
16 * - interface to write to second row of LCD from /proc (if technically possible)
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/stddef.h> /* for offsetof() */
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/ioport.h>
25 #include <linux/version.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/inetdevice.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel_stat.h>
32 #include <linux/reboot.h>
33 #include <linux/proc_fs.h>
34 #include <linux/ctype.h>
35 #include <linux/blkdev.h>
37 #include <asm/processor.h>
38 #include <asm/hardware.h>
39 #include <asm/param.h> /* HZ */
42 #include <asm/uaccess.h>
44 /* The control of the LEDs and LCDs on PARISC-machines have to be done
45 completely in software. The necessary calculations are done in a tasklet
46 which is scheduled at every timer interrupt and since the calculations
47 may consume relatively much CPU-time some of the calculations can be
48 turned off with the following variables (controlled via procfs) */
50 static int led_type
= -1;
51 static int led_heartbeat
= 1;
52 static int led_diskio
= 1;
53 static int led_lanrxtx
= 1;
54 static char lcd_text
[32];
57 #define DPRINTK(x) printk x
64 unsigned char command
; /* stores the command byte */
65 unsigned char on
; /* value for turning LED on */
66 unsigned char off
; /* value for turning LED off */
69 /* Structure returned by PDC_RETURN_CHASSIS_INFO */
70 /* NOTE: we use unsigned long:16 two times, since the following member
71 lcd_cmd_reg_addr needs to be 64bit aligned on 64bit PA2.0-machines */
72 struct pdc_chassis_lcd_info_ret_block
{
73 unsigned long model
:16; /* DISPLAY_MODEL_XXXX */
74 unsigned long lcd_width
:16; /* width of the LCD in chars (DISPLAY_MODEL_LCD only) */
75 char *lcd_cmd_reg_addr
; /* ptr to LCD cmd-register & data ptr for LED */
76 char *lcd_data_reg_addr
; /* ptr to LCD data-register (LCD only) */
77 unsigned int min_cmd_delay
; /* delay in uS after cmd-write (LCD only) */
78 unsigned char reset_cmd1
; /* command #1 for writing LCD string (LCD only) */
79 unsigned char reset_cmd2
; /* command #2 for writing LCD string (LCD only) */
80 unsigned char act_enable
; /* 0 = no activity (LCD only) */
81 struct lcd_block heartbeat
;
82 struct lcd_block disk_io
;
83 struct lcd_block lan_rcv
;
84 struct lcd_block lan_tx
;
89 /* LCD_CMD and LCD_DATA for KittyHawk machines */
90 #define KITTYHAWK_LCD_CMD (0xfffffffff0190000UL) /* 64bit-ready */
91 #define KITTYHAWK_LCD_DATA (KITTYHAWK_LCD_CMD+1)
93 /* lcd_info is pre-initialized to the values needed to program KittyHawk LCD's
94 * HP seems to have used Sharp/Hitachi HD44780 LCDs most of the time. */
95 static struct pdc_chassis_lcd_info_ret_block
96 lcd_info
__attribute__((aligned(8))) =
98 .model
= DISPLAY_MODEL_LCD
,
100 .lcd_cmd_reg_addr
= (char *) KITTYHAWK_LCD_CMD
,
101 .lcd_data_reg_addr
= (char *) KITTYHAWK_LCD_DATA
,
108 /* direct access to some of the lcd_info variables */
109 #define LCD_CMD_REG lcd_info.lcd_cmd_reg_addr
110 #define LCD_DATA_REG lcd_info.lcd_data_reg_addr
111 #define LED_DATA_REG lcd_info.lcd_cmd_reg_addr /* LASI & ASP only */
114 /* ptr to LCD/LED-specific function */
115 static void (*led_func_ptr
) (unsigned char);
119 #ifdef CONFIG_PROC_FS
120 static int led_proc_read(char *page
, char **start
, off_t off
, int count
,
121 int *eof
, void *data
)
129 out
+= sprintf(out
, "Heartbeat: %d\n", led_heartbeat
);
130 out
+= sprintf(out
, "Disk IO: %d\n", led_diskio
);
131 out
+= sprintf(out
, "LAN Rx/Tx: %d\n", led_lanrxtx
);
134 out
+= sprintf(out
, "%s\n", lcd_text
);
141 len
= out
- page
- off
;
144 if (len
<= 0) return 0;
152 static int led_proc_write(struct file
*file
, const char *buf
,
153 unsigned long count
, void *data
)
155 char *cur
, lbuf
[count
];
158 if (!capable(CAP_SYS_ADMIN
))
161 memset(lbuf
, 0, count
);
163 copy_from_user(lbuf
, buf
, count
);
166 /* skip initial spaces */
167 while (*cur
&& isspace(*cur
))
176 if (d
!= 0 && d
!= 1) goto parse_error
;
179 if (*cur
++ != ' ') goto parse_error
;
182 if (d
!= 0 && d
!= 1) goto parse_error
;
185 if (*cur
++ != ' ') goto parse_error
;
188 if (d
!= 0 && d
!= 1) goto parse_error
;
195 /* reset to default */
196 lcd_print("Linux " UTS_RELEASE
);
200 /* chop off trailing \n.. if the user gives multiple
201 * \n then it's all their fault.. */
202 if (*cur
&& cur
[strlen(cur
)-1] == '\n')
203 cur
[strlen(cur
)-1] = 0;
214 if ((long)data
== LED_NOLCD
)
215 printk(KERN_CRIT
"Parse error: expect \"n n n\" (n == 0 or 1) for heartbeat,\ndisk io and lan tx/rx indicators\n");
219 static int __init
led_create_procfs(void)
221 struct proc_dir_entry
*proc_pdc_root
= NULL
;
222 struct proc_dir_entry
*ent
;
224 if (led_type
== -1) return -1;
226 proc_pdc_root
= proc_mkdir("pdc", 0);
227 if (!proc_pdc_root
) return -1;
228 proc_pdc_root
->owner
= THIS_MODULE
;
229 ent
= create_proc_entry("led", S_IFREG
|S_IRUGO
|S_IWUSR
, proc_pdc_root
);
232 ent
->data
= (void *)LED_NOLCD
; /* LED */
233 ent
->read_proc
= led_proc_read
;
234 ent
->write_proc
= led_proc_write
;
235 ent
->owner
= THIS_MODULE
;
237 if (led_type
== LED_HASLCD
)
239 ent
= create_proc_entry("lcd", S_IFREG
|S_IRUGO
|S_IWUSR
, proc_pdc_root
);
242 ent
->data
= (void *)LED_HASLCD
; /* LCD */
243 ent
->read_proc
= led_proc_read
;
244 ent
->write_proc
= led_proc_write
;
245 ent
->owner
= THIS_MODULE
;
257 #define LED_DATA 0x01 /* data to shift (0:on 1:off) */
258 #define LED_STROBE 0x02 /* strobe to clock data */
259 static void led_ASP_driver(unsigned char leds
)
264 for (i
= 0; i
< 8; i
++) {
266 value
= (leds
& 0x80) >> 7;
267 gsc_writeb( value
, LED_DATA_REG
);
268 gsc_writeb( value
| LED_STROBE
, LED_DATA_REG
);
279 static void led_LASI_driver(unsigned char leds
)
282 gsc_writeb( leds
, LED_DATA_REG
);
290 ** The logic of the LCD driver is, that we write at every scheduled call
291 ** only to one of LCD_CMD_REG _or_ LCD_DATA_REG - registers.
292 ** That way we don't need to let this tasklet busywait for min_cmd_delay
295 ** TODO: check the value of "min_cmd_delay" against the value of HZ.
298 static void led_LCD_driver(unsigned char leds
)
300 static int last_index
; /* 0:heartbeat, 1:disk, 2:lan_in, 3:lan_out */
301 static int last_was_cmd
;/* 0: CMD was written last, 1: DATA was last */
302 struct lcd_block
*block_ptr
;
305 switch (last_index
) {
306 case 0: block_ptr
= &lcd_info
.heartbeat
;
307 value
= leds
& LED_HEARTBEAT
;
309 case 1: block_ptr
= &lcd_info
.disk_io
;
310 value
= leds
& LED_DISK_IO
;
312 case 2: block_ptr
= &lcd_info
.lan_rcv
;
313 value
= leds
& LED_LAN_RCV
;
315 case 3: block_ptr
= &lcd_info
.lan_tx
;
316 value
= leds
& LED_LAN_TX
;
318 default: /* should never happen: */
323 /* write the value to the LCD data port */
324 gsc_writeb( value
? block_ptr
->on
: block_ptr
->off
, LCD_DATA_REG
);
326 /* write the command-byte to the LCD command register */
327 gsc_writeb( block_ptr
->command
, LCD_CMD_REG
);
330 /* now update the vars for the next interrupt iteration */
331 if (++last_was_cmd
== 2) { /* switch between cmd & data */
333 if (++last_index
== 4)
334 last_index
= 0; /* switch back to heartbeat index */
341 ** led_get_net_activity()
343 ** calculate if there was TX- or RX-troughput on the network interfaces
344 ** (analog to dev_get_info() from net/core/dev.c)
347 static __inline__
int led_get_net_activity(void)
352 static unsigned long rx_total_last
, tx_total_last
;
353 unsigned long rx_total
, tx_total
;
354 struct net_device
*dev
;
357 rx_total
= tx_total
= 0;
359 /* we are running as tasklet, so locking dev_base
360 * for reading should be OK */
361 read_lock(&dev_base_lock
);
362 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
363 struct net_device_stats
*stats
;
364 struct in_device
*in_dev
= __in_dev_get(dev
);
365 if (!in_dev
|| !in_dev
->ifa_list
)
367 if (LOOPBACK(in_dev
->ifa_list
->ifa_local
))
371 stats
= dev
->get_stats(dev
);
372 rx_total
+= stats
->rx_packets
;
373 tx_total
+= stats
->tx_packets
;
375 read_unlock(&dev_base_lock
);
379 if (rx_total
!= rx_total_last
) {
380 rx_total_last
= rx_total
;
381 retval
|= LED_LAN_RCV
;
384 if (tx_total
!= tx_total_last
) {
385 tx_total_last
= tx_total
;
386 retval
|= LED_LAN_TX
;
396 ** led_get_diskio_activity()
398 ** calculate if there was disk-io in the system
401 static __inline__
int led_get_diskio_activity(void)
403 static unsigned long last_pgpgin
, last_pgpgout
;
404 struct page_state pgstat
;
407 get_full_page_state(&pgstat
); /* get no of sectors in & out */
409 /* Just use a very simple calculation here. Do not care about overflow,
410 since we only want to know if there was activity or not. */
411 changed
= (pgstat
.pgpgin
!= last_pgpgin
) || (pgstat
.pgpgout
!= last_pgpgout
);
412 last_pgpgin
= pgstat
.pgpgin
;
413 last_pgpgout
= pgstat
.pgpgout
;
415 return (changed
? LED_DISK_IO
: 0);
421 ** led_tasklet_func()
423 ** is scheduled at every timer interrupt from time.c and
424 ** updates the chassis LCD/LED
427 - display load average (older machines like 715/64 have 4 "free" LED's for that)
431 #define HEARTBEAT_LEN (HZ*6/100)
432 #define HEARTBEAT_2ND_RANGE_START (HZ*22/100)
433 #define HEARTBEAT_2ND_RANGE_END (HEARTBEAT_2ND_RANGE_START + HEARTBEAT_LEN)
436 #define NORMALIZED_COUNT(count) (count)
438 #warning "Untested situation HZ != 100 !!"
439 #define NORMALIZED_COUNT(count) (count/(HZ/100))
442 static void led_tasklet_func(unsigned long unused
)
444 static unsigned char lastleds
;
445 unsigned char currentleds
; /* stores current value of the LEDs */
446 static unsigned long count
; /* static incremented value, not wrapped */
447 static unsigned long count_HZ
; /* counter in range 0..HZ */
449 /* exit if not initialized */
453 /* increment the local counters */
455 if (++count_HZ
== HZ
)
458 currentleds
= lastleds
;
462 /* flash heartbeat-LED like a real heart (2 x short then a long delay) */
463 if (count_HZ
<HEARTBEAT_LEN
||
464 (count_HZ
>=HEARTBEAT_2ND_RANGE_START
&& count_HZ
<HEARTBEAT_2ND_RANGE_END
))
465 currentleds
|= LED_HEARTBEAT
;
467 currentleds
&= ~LED_HEARTBEAT
;
470 /* look for network activity and flash LEDs respectively */
471 if (led_lanrxtx
&& ((NORMALIZED_COUNT(count
)+(8/2)) & 7) == 0)
473 currentleds
&= ~(LED_LAN_RCV
| LED_LAN_TX
);
474 currentleds
|= led_get_net_activity();
477 /* avoid to calculate diskio-stats at same irq as netio-stats */
478 if (led_diskio
&& (NORMALIZED_COUNT(count
) & 7) == 0)
480 currentleds
&= ~LED_DISK_IO
;
481 currentleds
|= led_get_diskio_activity();
484 /* blink all LEDs twice a second if we got an Oops (HPMC) */
485 if (oops_in_progress
) {
486 currentleds
= (count_HZ
<=(HZ
/2)) ? 0 : 0xff;
489 /* update the LCD/LEDs */
490 if (currentleds
!= lastleds
) {
491 led_func_ptr(currentleds
);
492 lastleds
= currentleds
;
496 /* main led tasklet struct (scheduled from time.c) */
497 DECLARE_TASKLET_DISABLED(led_tasklet
, led_tasklet_func
, 0);
503 ** called by the reboot notifier chain at shutdown and stops all
504 ** LED/LCD activities.
508 static int led_halt(struct notifier_block
*, unsigned long, void *);
510 static struct notifier_block led_notifier
= {
511 .notifier_call
= led_halt
,
514 static int led_halt(struct notifier_block
*nb
, unsigned long event
, void *buf
)
519 case SYS_RESTART
: txt
= "SYSTEM RESTART";
521 case SYS_HALT
: txt
= "SYSTEM HALT";
523 case SYS_POWER_OFF
: txt
= "SYSTEM POWER OFF";
525 default: return NOTIFY_DONE
;
528 /* completely stop the LED/LCD tasklet */
529 tasklet_disable(&led_tasklet
);
531 if (lcd_info
.model
== DISPLAY_MODEL_LCD
)
535 led_func_ptr(0xff); /* turn all LEDs ON */
537 unregister_reboot_notifier(&led_notifier
);
542 ** register_led_driver()
544 ** registers an external LED or LCD for usage by this driver.
545 ** currently only LCD-, LASI- and ASP-style LCD/LED's are supported.
549 int __init
register_led_driver(int model
, char *cmd_reg
, char *data_reg
)
551 static int initialized
;
553 if (initialized
|| !data_reg
)
556 lcd_info
.model
= model
; /* store the values */
557 LCD_CMD_REG
= (cmd_reg
== LED_CMD_REG_NONE
) ? NULL
: cmd_reg
;
559 switch (lcd_info
.model
) {
560 case DISPLAY_MODEL_LCD
:
561 LCD_DATA_REG
= data_reg
;
562 printk(KERN_INFO
"LCD display at %p,%p registered\n",
563 LCD_CMD_REG
, LCD_DATA_REG
);
564 led_func_ptr
= led_LCD_driver
;
565 lcd_print( "Linux " UTS_RELEASE
);
566 led_type
= LED_HASLCD
;
569 case DISPLAY_MODEL_LASI
:
570 LED_DATA_REG
= data_reg
;
571 led_func_ptr
= led_LASI_driver
;
572 printk(KERN_INFO
"LED display at %p registered\n", LED_DATA_REG
);
573 led_type
= LED_NOLCD
;
576 case DISPLAY_MODEL_OLD_ASP
:
577 LED_DATA_REG
= data_reg
;
578 led_func_ptr
= led_ASP_driver
;
579 printk(KERN_INFO
"LED (ASP-style) display at %p registered\n",
581 led_type
= LED_NOLCD
;
585 printk(KERN_ERR
"%s: Wrong LCD/LED model %d !\n",
586 __FUNCTION__
, lcd_info
.model
);
590 /* mark the LCD/LED driver now as initialized and
591 * register to the reboot notifier chain */
593 register_reboot_notifier(&led_notifier
);
595 /* start the led tasklet for the first time */
596 tasklet_enable(&led_tasklet
);
602 ** register_led_regions()
604 ** register_led_regions() registers the LCD/LED regions for /procfs.
605 ** At bootup - where the initialisation of the LCD/LED normally happens -
606 ** not all internal structures of request_region() are properly set up,
607 ** so that we delay the led-registration until after busdevices_init()
608 ** has been executed.
612 void __init
register_led_regions(void)
614 switch (lcd_info
.model
) {
615 case DISPLAY_MODEL_LCD
:
616 request_mem_region((unsigned long)LCD_CMD_REG
, 1, "lcd_cmd");
617 request_mem_region((unsigned long)LCD_DATA_REG
, 1, "lcd_data");
619 case DISPLAY_MODEL_LASI
:
620 case DISPLAY_MODEL_OLD_ASP
:
621 request_mem_region((unsigned long)LED_DATA_REG
, 1, "led_data");
631 ** Displays the given string on the LCD-Display of newer machines.
632 ** lcd_print() disables the timer-based led tasklet during its
633 ** execution and enables it afterwards again.
636 int lcd_print( char *str
)
640 if (!led_func_ptr
|| lcd_info
.model
!= DISPLAY_MODEL_LCD
)
643 /* temporarily disable the led tasklet */
644 tasklet_disable(&led_tasklet
);
646 /* copy display string to buffer for procfs */
647 strlcpy(lcd_text
, str
, sizeof(lcd_text
));
649 /* Set LCD Cursor to 1st character */
650 gsc_writeb(lcd_info
.reset_cmd1
, LCD_CMD_REG
);
651 udelay(lcd_info
.min_cmd_delay
);
653 /* Print the string */
654 for (i
=0; i
< lcd_info
.lcd_width
; i
++) {
656 gsc_writeb(*str
++, LCD_DATA_REG
);
658 gsc_writeb(' ', LCD_DATA_REG
);
659 udelay(lcd_info
.min_cmd_delay
);
662 /* re-enable the led tasklet */
663 tasklet_enable(&led_tasklet
);
665 return lcd_info
.lcd_width
;
671 ** led_init() is called very early in the bootup-process from setup.c
672 ** and asks the PDC for an usable chassis LCD or LED.
673 ** If the PDC doesn't return any info, then the LED
674 ** is detected by lasi.c or asp.c and registered with the
675 ** above functions lasi_led_init() or asp_led_init().
676 ** KittyHawk machines have often a buggy PDC, so that
677 ** we explicitly check for those machines here.
680 int __init
led_init(void)
682 struct pdc_chassis_info chassis_info
;
685 /* Work around the buggy PDC of KittyHawk-machines */
686 switch (CPU_HVERSION
) {
687 case 0x580: /* KittyHawk DC2-100 (K100) */
688 case 0x581: /* KittyHawk DC3-120 (K210) */
689 case 0x582: /* KittyHawk DC3 100 (K400) */
690 case 0x583: /* KittyHawk DC3 120 (K410) */
691 case 0x58B: /* KittyHawk DC2 100 (K200) */
692 printk(KERN_INFO
"%s: KittyHawk-Machine (hversion 0x%x) found, "
693 "LED detection skipped.\n", __FILE__
, CPU_HVERSION
);
694 goto found
; /* use the preinitialized values of lcd_info */
697 /* initialize the struct, so that we can check for valid return values */
698 lcd_info
.model
= DISPLAY_MODEL_NONE
;
699 chassis_info
.actcnt
= chassis_info
.maxcnt
= 0;
701 if ((ret
= pdc_chassis_info(&chassis_info
, &lcd_info
, sizeof(lcd_info
))) == PDC_OK
) {
702 DPRINTK((KERN_INFO
"%s: chassis info: model=%d (%s), "
703 "lcd_width=%d, cmd_delay=%u,\n"
704 "%s: sizecnt=%d, actcnt=%ld, maxcnt=%ld\n",
705 __FILE__
, lcd_info
.model
,
706 (lcd_info
.model
==DISPLAY_MODEL_LCD
) ? "LCD" :
707 (lcd_info
.model
==DISPLAY_MODEL_LASI
) ? "LED" : "unknown",
708 lcd_info
.lcd_width
, lcd_info
.min_cmd_delay
,
709 __FILE__
, sizeof(lcd_info
),
710 chassis_info
.actcnt
, chassis_info
.maxcnt
));
711 DPRINTK((KERN_INFO
"%s: cmd=%p, data=%p, reset1=%x, reset2=%x, act_enable=%d\n",
712 __FILE__
, lcd_info
.lcd_cmd_reg_addr
,
713 lcd_info
.lcd_data_reg_addr
, lcd_info
.reset_cmd1
,
714 lcd_info
.reset_cmd2
, lcd_info
.act_enable
));
716 /* check the results. Some machines have a buggy PDC */
717 if (chassis_info
.actcnt
<= 0 || chassis_info
.actcnt
!= chassis_info
.maxcnt
)
720 switch (lcd_info
.model
) {
721 case DISPLAY_MODEL_LCD
: /* LCD display */
722 if (chassis_info
.actcnt
<
723 offsetof(struct pdc_chassis_lcd_info_ret_block
, _pad
)-1)
725 if (!lcd_info
.act_enable
) {
726 DPRINTK((KERN_INFO
"PDC prohibited usage of the LCD.\n"));
731 case DISPLAY_MODEL_NONE
: /* no LED or LCD available */
732 printk(KERN_INFO
"PDC reported no LCD or LED.\n");
735 case DISPLAY_MODEL_LASI
: /* Lasi style 8 bit LED display */
736 if (chassis_info
.actcnt
!= 8 && chassis_info
.actcnt
!= 32)
741 printk(KERN_WARNING
"PDC reported unknown LCD/LED model %d\n",
747 /* register the LCD/LED driver */
748 register_led_driver(lcd_info
.model
, LCD_CMD_REG
, LCD_DATA_REG
);
752 DPRINTK((KERN_INFO
"pdc_chassis_info call failed with retval = %d\n", ret
));
756 lcd_info
.model
= DISPLAY_MODEL_NONE
;
760 #ifdef CONFIG_PROC_FS
761 module_init(led_create_procfs
)