pdc_cons.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * PDC Console support - ie use firmware to dump text via boot console
  3. *
  4. * Copyright (C) 1999-2003 Matthew Wilcox <willy at parisc-linux.org>
  5. * Copyright (C) 2000 Martin K Petersen <mkp at mkp.net>
  6. * Copyright (C) 2000 John Marvin <jsm at parisc-linux.org>
  7. * Copyright (C) 2000-2003 Paul Bame <bame at parisc-linux.org>
  8. * Copyright (C) 2000 Philipp Rumpf <prumpf with tux.org>
  9. * Copyright (C) 2000 Michael Ang <mang with subcarrier.org>
  10. * Copyright (C) 2000 Grant Grundler <grundler with parisc-linux.org>
  11. * Copyright (C) 2001-2002 Ryan Bradetich <rbrad at parisc-linux.org>
  12. * Copyright (C) 2001 Helge Deller <deller at parisc-linux.org>
  13. * Copyright (C) 2001 Thomas Bogendoerfer <tsbogend at parisc-linux.org>
  14. * Copyright (C) 2002 Randolph Chung <tausq with parisc-linux.org>
  15. * Copyright (C) 2010 Guy Martin <gmsoft at tuxicoman.be>
  16. *
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  31. */
  32. /*
  33. * The PDC console is a simple console, which can be used for debugging
  34. * boot related problems on HP PA-RISC machines. It is also useful when no
  35. * other console works.
  36. *
  37. * This code uses the ROM (=PDC) based functions to read and write characters
  38. * from and to PDC's boot path.
  39. */
  40. /* Define EARLY_BOOTUP_DEBUG to debug kernel related boot problems.
  41. * On production kernels EARLY_BOOTUP_DEBUG should be undefined. */
  42. #define EARLY_BOOTUP_DEBUG
  43. #include <linux/kernel.h>
  44. #include <linux/console.h>
  45. #include <linux/string.h>
  46. #include <linux/init.h>
  47. #include <linux/major.h>
  48. #include <linux/tty.h>
  49. #include <asm/page.h> /* for PAGE0 */
  50. #include <asm/pdc.h> /* for iodc_call() proto and friends */
  51. static DEFINE_SPINLOCK(pdc_console_lock);
  52. static struct console pdc_cons;
  53. static void pdc_console_write(struct console *co, const char *s, unsigned count)
  54. {
  55. int i = 0;
  56. unsigned long flags;
  57. spin_lock_irqsave(&pdc_console_lock, flags);
  58. do {
  59. i += pdc_iodc_print(s + i, count - i);
  60. } while (i < count);
  61. spin_unlock_irqrestore(&pdc_console_lock, flags);
  62. }
  63. int pdc_console_poll_key(struct console *co)
  64. {
  65. int c;
  66. unsigned long flags;
  67. spin_lock_irqsave(&pdc_console_lock, flags);
  68. c = pdc_iodc_getc();
  69. spin_unlock_irqrestore(&pdc_console_lock, flags);
  70. return c;
  71. }
  72. static int pdc_console_setup(struct console *co, char *options)
  73. {
  74. return 0;
  75. }
  76. #if defined(CONFIG_PDC_CONSOLE)
  77. #include <linux/vt_kern.h>
  78. #include <linux/tty_flip.h>
  79. #define PDC_CONS_POLL_DELAY (30 * HZ / 1000)
  80. static void pdc_console_poll(unsigned long unused);
  81. static DEFINE_TIMER(pdc_console_timer, pdc_console_poll, 0, 0);
  82. static struct tty_port tty_port;
  83. static int pdc_console_tty_open(struct tty_struct *tty, struct file *filp)
  84. {
  85. tty_port_tty_set(&tty_port, tty);
  86. mod_timer(&pdc_console_timer, jiffies + PDC_CONS_POLL_DELAY);
  87. return 0;
  88. }
  89. static void pdc_console_tty_close(struct tty_struct *tty, struct file *filp)
  90. {
  91. if (tty->count == 1) {
  92. del_timer_sync(&pdc_console_timer);
  93. tty_port_tty_set(&tty_port, NULL);
  94. }
  95. }
  96. static int pdc_console_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
  97. {
  98. pdc_console_write(NULL, buf, count);
  99. return count;
  100. }
  101. static int pdc_console_tty_write_room(struct tty_struct *tty)
  102. {
  103. return 32768; /* no limit, no buffer used */
  104. }
  105. static int pdc_console_tty_chars_in_buffer(struct tty_struct *tty)
  106. {
  107. return 0; /* no buffer */
  108. }
  109. static const struct tty_operations pdc_console_tty_ops = {
  110. .open = pdc_console_tty_open,
  111. .close = pdc_console_tty_close,
  112. .write = pdc_console_tty_write,
  113. .write_room = pdc_console_tty_write_room,
  114. .chars_in_buffer = pdc_console_tty_chars_in_buffer,
  115. };
  116. static void pdc_console_poll(unsigned long unused)
  117. {
  118. int data, count = 0;
  119. while (1) {
  120. data = pdc_console_poll_key(NULL);
  121. if (data == -1)
  122. break;
  123. tty_insert_flip_char(&tty_port, data & 0xFF, TTY_NORMAL);
  124. count ++;
  125. }
  126. if (count)
  127. tty_flip_buffer_push(&tty_port);
  128. if (pdc_cons.flags & CON_ENABLED)
  129. mod_timer(&pdc_console_timer, jiffies + PDC_CONS_POLL_DELAY);
  130. }
  131. static struct tty_driver *pdc_console_tty_driver;
  132. static int __init pdc_console_tty_driver_init(void)
  133. {
  134. int err;
  135. /* Check if the console driver is still registered.
  136. * It is unregistered if the pdc console was not selected as the
  137. * primary console. */
  138. struct console *tmp;
  139. console_lock();
  140. for_each_console(tmp)
  141. if (tmp == &pdc_cons)
  142. break;
  143. console_unlock();
  144. if (!tmp) {
  145. printk(KERN_INFO "PDC console driver not registered anymore, not creating %s\n", pdc_cons.name);
  146. return -ENODEV;
  147. }
  148. printk(KERN_INFO "The PDC console driver is still registered, removing CON_BOOT flag\n");
  149. pdc_cons.flags &= ~CON_BOOT;
  150. pdc_console_tty_driver = alloc_tty_driver(1);
  151. if (!pdc_console_tty_driver)
  152. return -ENOMEM;
  153. tty_port_init(&tty_port);
  154. pdc_console_tty_driver->driver_name = "pdc_cons";
  155. pdc_console_tty_driver->name = "ttyB";
  156. pdc_console_tty_driver->major = MUX_MAJOR;
  157. pdc_console_tty_driver->minor_start = 0;
  158. pdc_console_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  159. pdc_console_tty_driver->init_termios = tty_std_termios;
  160. pdc_console_tty_driver->flags = TTY_DRIVER_REAL_RAW |
  161. TTY_DRIVER_RESET_TERMIOS;
  162. tty_set_operations(pdc_console_tty_driver, &pdc_console_tty_ops);
  163. tty_port_link_device(&tty_port, pdc_console_tty_driver, 0);
  164. err = tty_register_driver(pdc_console_tty_driver);
  165. if (err) {
  166. printk(KERN_ERR "Unable to register the PDC console TTY driver\n");
  167. tty_port_destroy(&tty_port);
  168. return err;
  169. }
  170. return 0;
  171. }
  172. device_initcall(pdc_console_tty_driver_init);
  173. static struct tty_driver * pdc_console_device (struct console *c, int *index)
  174. {
  175. *index = c->index;
  176. return pdc_console_tty_driver;
  177. }
  178. #else
  179. #define pdc_console_device NULL
  180. #endif
  181. static struct console pdc_cons = {
  182. .name = "ttyB",
  183. .write = pdc_console_write,
  184. .device = pdc_console_device,
  185. .setup = pdc_console_setup,
  186. .flags = CON_BOOT | CON_PRINTBUFFER,
  187. .index = -1,
  188. };
  189. static int pdc_console_initialized;
  190. static void pdc_console_init_force(void)
  191. {
  192. if (pdc_console_initialized)
  193. return;
  194. ++pdc_console_initialized;
  195. /* If the console is duplex then copy the COUT parameters to CIN. */
  196. if (PAGE0->mem_cons.cl_class == CL_DUPLEX)
  197. memcpy(&PAGE0->mem_kbd, &PAGE0->mem_cons, sizeof(PAGE0->mem_cons));
  198. /* register the pdc console */
  199. register_console(&pdc_cons);
  200. }
  201. void __init pdc_console_init(void)
  202. {
  203. #if defined(EARLY_BOOTUP_DEBUG) || defined(CONFIG_PDC_CONSOLE)
  204. pdc_console_init_force();
  205. #endif
  206. #ifdef EARLY_BOOTUP_DEBUG
  207. printk(KERN_INFO "Initialized PDC Console for debugging.\n");
  208. #endif
  209. }
  210. /*
  211. * Used for emergencies. Currently only used if an HPMC occurs. If an
  212. * HPMC occurs, it is possible that the current console may not be
  213. * properly initialised after the PDC IO reset. This routine unregisters
  214. * all of the current consoles, reinitializes the pdc console and
  215. * registers it.
  216. */
  217. void pdc_console_restart(void)
  218. {
  219. struct console *console;
  220. if (pdc_console_initialized)
  221. return;
  222. /* If we've already seen the output, don't bother to print it again */
  223. if (console_drivers != NULL)
  224. pdc_cons.flags &= ~CON_PRINTBUFFER;
  225. while ((console = console_drivers) != NULL)
  226. unregister_console(console_drivers);
  227. /* force registering the pdc console */
  228. pdc_console_init_force();
  229. }