Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Drivers for the Total Impact PPC based computer "BRIQ" |
| 3 | * by Dr. Karsten Jeppesen |
| 4 | * |
| 5 | */ |
| 6 | |
| 7 | #include <linux/module.h> |
| 8 | |
| 9 | #include <linux/types.h> |
| 10 | #include <linux/errno.h> |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 11 | #include <linux/tty.h> |
| 12 | #include <linux/timer.h> |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/wait.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/ioport.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/miscdevice.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/mm.h> |
| 22 | #include <linux/init.h> |
| 23 | |
| 24 | #include <asm/uaccess.h> |
| 25 | #include <asm/io.h> |
| 26 | #include <asm/prom.h> |
| 27 | |
| 28 | #define BRIQ_PANEL_MINOR 156 |
| 29 | #define BRIQ_PANEL_VFD_IOPORT 0x0390 |
| 30 | #define BRIQ_PANEL_LED_IOPORT 0x0398 |
| 31 | #define BRIQ_PANEL_VER "1.1 (04/20/2002)" |
| 32 | #define BRIQ_PANEL_MSG0 "Loading Linux" |
| 33 | |
| 34 | static int vfd_is_open; |
| 35 | static unsigned char vfd[40]; |
| 36 | static int vfd_cursor; |
| 37 | static unsigned char ledpb, led; |
| 38 | |
| 39 | static void update_vfd(void) |
| 40 | { |
| 41 | int i; |
| 42 | |
| 43 | /* cursor home */ |
| 44 | outb(0x02, BRIQ_PANEL_VFD_IOPORT); |
| 45 | for (i=0; i<20; i++) |
| 46 | outb(vfd[i], BRIQ_PANEL_VFD_IOPORT + 1); |
| 47 | |
| 48 | /* cursor to next line */ |
| 49 | outb(0xc0, BRIQ_PANEL_VFD_IOPORT); |
| 50 | for (i=20; i<40; i++) |
| 51 | outb(vfd[i], BRIQ_PANEL_VFD_IOPORT + 1); |
| 52 | |
| 53 | } |
| 54 | |
| 55 | static void set_led(char state) |
| 56 | { |
| 57 | if (state == 'R') |
| 58 | led = 0x01; |
| 59 | else if (state == 'G') |
| 60 | led = 0x02; |
| 61 | else if (state == 'Y') |
| 62 | led = 0x03; |
| 63 | else if (state == 'X') |
| 64 | led = 0x00; |
| 65 | outb(led, BRIQ_PANEL_LED_IOPORT); |
| 66 | } |
| 67 | |
| 68 | static int briq_panel_open(struct inode *ino, struct file *filep) |
| 69 | { |
| 70 | /* enforce single access */ |
| 71 | if (vfd_is_open) |
| 72 | return -EBUSY; |
| 73 | vfd_is_open = 1; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int briq_panel_release(struct inode *ino, struct file *filep) |
| 79 | { |
| 80 | if (!vfd_is_open) |
| 81 | return -ENODEV; |
| 82 | |
| 83 | vfd_is_open = 0; |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 88 | static ssize_t briq_panel_read(struct file *file, char __user *buf, size_t count, |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 89 | loff_t *ppos) |
| 90 | { |
| 91 | unsigned short c; |
| 92 | unsigned char cp; |
| 93 | |
| 94 | #if 0 /* Can't seek (pread) on this device */ |
| 95 | if (ppos != &file->f_pos) |
| 96 | return -ESPIPE; |
| 97 | #endif |
| 98 | |
| 99 | if (!vfd_is_open) |
| 100 | return -ENODEV; |
| 101 | |
| 102 | c = (inb(BRIQ_PANEL_LED_IOPORT) & 0x000c) | (ledpb & 0x0003); |
| 103 | set_led(' '); |
| 104 | /* upper button released */ |
| 105 | if ((!(ledpb & 0x0004)) && (c & 0x0004)) { |
| 106 | cp = ' '; |
| 107 | ledpb = c; |
| 108 | if (copy_to_user(buf, &cp, 1)) |
| 109 | return -EFAULT; |
| 110 | return 1; |
| 111 | } |
| 112 | /* lower button released */ |
| 113 | else if ((!(ledpb & 0x0008)) && (c & 0x0008)) { |
| 114 | cp = '\r'; |
| 115 | ledpb = c; |
| 116 | if (copy_to_user(buf, &cp, 1)) |
| 117 | return -EFAULT; |
| 118 | return 1; |
| 119 | } else { |
| 120 | ledpb = c; |
| 121 | return 0; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void scroll_vfd( void ) |
| 126 | { |
| 127 | int i; |
| 128 | |
| 129 | for (i=0; i<20; i++) { |
| 130 | vfd[i] = vfd[i+20]; |
| 131 | vfd[i+20] = ' '; |
| 132 | } |
| 133 | vfd_cursor = 20; |
| 134 | } |
| 135 | |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 136 | static ssize_t briq_panel_write(struct file *file, const char __user *buf, size_t len, |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 137 | loff_t *ppos) |
| 138 | { |
| 139 | size_t indx = len; |
| 140 | int i, esc = 0; |
| 141 | |
| 142 | #if 0 /* Can't seek (pwrite) on this device */ |
| 143 | if (ppos != &file->f_pos) |
| 144 | return -ESPIPE; |
| 145 | #endif |
| 146 | |
| 147 | if (!vfd_is_open) |
| 148 | return -EBUSY; |
| 149 | |
| 150 | for (;;) { |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 151 | char c; |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 152 | if (!indx) |
| 153 | break; |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 154 | if (get_user(c, buf)) |
| 155 | return -EFAULT; |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 156 | if (esc) { |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 157 | set_led(c); |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 158 | esc = 0; |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 159 | } else if (c == 27) { |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 160 | esc = 1; |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 161 | } else if (c == 12) { |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 162 | /* do a form feed */ |
| 163 | for (i=0; i<40; i++) |
| 164 | vfd[i] = ' '; |
| 165 | vfd_cursor = 0; |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 166 | } else if (c == 10) { |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 167 | if (vfd_cursor < 20) |
| 168 | vfd_cursor = 20; |
| 169 | else if (vfd_cursor < 40) |
| 170 | vfd_cursor = 40; |
| 171 | else if (vfd_cursor < 60) |
| 172 | vfd_cursor = 60; |
| 173 | if (vfd_cursor > 59) |
| 174 | scroll_vfd(); |
| 175 | } else { |
| 176 | /* just a character */ |
| 177 | if (vfd_cursor > 39) |
| 178 | scroll_vfd(); |
Al Viro | 4ac493b | 2006-09-23 18:20:56 +0100 | [diff] [blame] | 179 | vfd[vfd_cursor++] = c; |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 180 | } |
| 181 | indx--; |
| 182 | buf++; |
| 183 | } |
| 184 | update_vfd(); |
| 185 | |
| 186 | return len; |
| 187 | } |
| 188 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 189 | static const struct file_operations briq_panel_fops = { |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 190 | .owner = THIS_MODULE, |
| 191 | .read = briq_panel_read, |
| 192 | .write = briq_panel_write, |
| 193 | .open = briq_panel_open, |
| 194 | .release = briq_panel_release, |
| 195 | }; |
| 196 | |
| 197 | static struct miscdevice briq_panel_miscdev = { |
| 198 | BRIQ_PANEL_MINOR, |
| 199 | "briq_panel", |
| 200 | &briq_panel_fops |
| 201 | }; |
| 202 | |
| 203 | static int __init briq_panel_init(void) |
| 204 | { |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 205 | struct device_node *root = of_find_node_by_path("/"); |
Al Viro | 13b5aec | 2006-09-23 16:44:58 +0100 | [diff] [blame] | 206 | const char *machine; |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 207 | int i; |
| 208 | |
Stephen Rothwell | 40cd3a4 | 2007-05-01 13:54:02 +1000 | [diff] [blame] | 209 | machine = of_get_property(root, "model", NULL); |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 210 | if (!machine || strncmp(machine, "TotalImpact,BRIQ-1", 18) != 0) { |
| 211 | of_node_put(root); |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 212 | return -ENODEV; |
Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 213 | } |
| 214 | of_node_put(root); |
Benjamin Herrenschmidt | a45b839 | 2006-07-04 15:06:20 +1000 | [diff] [blame] | 215 | |
| 216 | printk(KERN_INFO |
| 217 | "briq_panel: v%s Dr. Karsten Jeppesen (kj@totalimpact.com)\n", |
| 218 | BRIQ_PANEL_VER); |
| 219 | |
| 220 | if (!request_region(BRIQ_PANEL_VFD_IOPORT, 4, "BRIQ Front Panel")) |
| 221 | return -EBUSY; |
| 222 | |
| 223 | if (!request_region(BRIQ_PANEL_LED_IOPORT, 2, "BRIQ Front Panel")) { |
| 224 | release_region(BRIQ_PANEL_VFD_IOPORT, 4); |
| 225 | return -EBUSY; |
| 226 | } |
| 227 | ledpb = inb(BRIQ_PANEL_LED_IOPORT) & 0x000c; |
| 228 | |
| 229 | if (misc_register(&briq_panel_miscdev) < 0) { |
| 230 | release_region(BRIQ_PANEL_VFD_IOPORT, 4); |
| 231 | release_region(BRIQ_PANEL_LED_IOPORT, 2); |
| 232 | return -EBUSY; |
| 233 | } |
| 234 | |
| 235 | outb(0x38, BRIQ_PANEL_VFD_IOPORT); /* Function set */ |
| 236 | outb(0x01, BRIQ_PANEL_VFD_IOPORT); /* Clear display */ |
| 237 | outb(0x0c, BRIQ_PANEL_VFD_IOPORT); /* Display on */ |
| 238 | outb(0x06, BRIQ_PANEL_VFD_IOPORT); /* Entry normal */ |
| 239 | for (i=0; i<40; i++) |
| 240 | vfd[i]=' '; |
| 241 | #ifndef MODULE |
| 242 | vfd[0] = 'L'; |
| 243 | vfd[1] = 'o'; |
| 244 | vfd[2] = 'a'; |
| 245 | vfd[3] = 'd'; |
| 246 | vfd[4] = 'i'; |
| 247 | vfd[5] = 'n'; |
| 248 | vfd[6] = 'g'; |
| 249 | vfd[7] = ' '; |
| 250 | vfd[8] = '.'; |
| 251 | vfd[9] = '.'; |
| 252 | vfd[10] = '.'; |
| 253 | #endif /* !MODULE */ |
| 254 | |
| 255 | update_vfd(); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static void __exit briq_panel_exit(void) |
| 261 | { |
| 262 | misc_deregister(&briq_panel_miscdev); |
| 263 | release_region(BRIQ_PANEL_VFD_IOPORT, 4); |
| 264 | release_region(BRIQ_PANEL_LED_IOPORT, 2); |
| 265 | } |
| 266 | |
| 267 | module_init(briq_panel_init); |
| 268 | module_exit(briq_panel_exit); |
| 269 | |
| 270 | MODULE_LICENSE("GPL"); |
| 271 | MODULE_AUTHOR("Karsten Jeppesen <karsten@jeppesens.com>"); |
| 272 | MODULE_DESCRIPTION("Driver for the Total Impact briQ front panel"); |