console.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * arch/xtensa/platforms/iss/console.c
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2001-2005 Tensilica Inc.
  9. * Authors Christian Zankel, Joe Taylor
  10. */
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/console.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/major.h>
  18. #include <linux/param.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/serial.h>
  21. #include <linux/serialP.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/irq.h>
  24. #include <platform/simcall.h>
  25. #include <linux/tty.h>
  26. #include <linux/tty_flip.h>
  27. #ifdef SERIAL_INLINE
  28. #define _INLINE_ inline
  29. #endif
  30. #define SERIAL_MAX_NUM_LINES 1
  31. #define SERIAL_TIMER_VALUE (20 * HZ)
  32. static struct tty_driver *serial_driver;
  33. static struct tty_port serial_port;
  34. static struct timer_list serial_timer;
  35. static DEFINE_SPINLOCK(timer_lock);
  36. int errno;
  37. static int __simc (int a, int b, int c, int d, int e, int f) __attribute__((__noinline__));
  38. static int __simc (int a, int b, int c, int d, int e, int f)
  39. {
  40. int ret;
  41. __asm__ __volatile__ ("simcall\n"
  42. "mov %0, a2\n"
  43. "mov %1, a3\n" : "=a" (ret), "=a" (errno)
  44. : : "a2", "a3");
  45. return ret;
  46. }
  47. static char *serial_version = "0.1";
  48. static char *serial_name = "ISS serial driver";
  49. /*
  50. * This routine is called whenever a serial port is opened. It
  51. * enables interrupts for a serial port, linking in its async structure into
  52. * the IRQ chain. It also performs the serial-specific
  53. * initialization for the tty structure.
  54. */
  55. static void rs_poll(unsigned long);
  56. static int rs_open(struct tty_struct *tty, struct file * filp)
  57. {
  58. tty->port = &serial_port;
  59. spin_lock(&timer_lock);
  60. if (tty->count == 1) {
  61. setup_timer(&serial_timer, rs_poll, (unsigned long)tty);
  62. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  63. }
  64. spin_unlock(&timer_lock);
  65. return 0;
  66. }
  67. /*
  68. * ------------------------------------------------------------
  69. * iss_serial_close()
  70. *
  71. * This routine is called when the serial port gets closed. First, we
  72. * wait for the last remaining data to be sent. Then, we unlink its
  73. * async structure from the interrupt chain if necessary, and we free
  74. * that IRQ if nothing is left in the chain.
  75. * ------------------------------------------------------------
  76. */
  77. static void rs_close(struct tty_struct *tty, struct file * filp)
  78. {
  79. spin_lock_bh(&timer_lock);
  80. if (tty->count == 1)
  81. del_timer_sync(&serial_timer);
  82. spin_unlock_bh(&timer_lock);
  83. }
  84. static int rs_write(struct tty_struct * tty,
  85. const unsigned char *buf, int count)
  86. {
  87. /* see drivers/char/serialX.c to reference original version */
  88. __simc (SYS_write, 1, (unsigned long)buf, count, 0, 0);
  89. return count;
  90. }
  91. static void rs_poll(unsigned long priv)
  92. {
  93. struct tty_struct* tty = (struct tty_struct*) priv;
  94. struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
  95. int i = 0;
  96. unsigned char c;
  97. spin_lock(&timer_lock);
  98. while (__simc(SYS_select_one, 0, XTISS_SELECT_ONE_READ, (int)&tv,0,0)){
  99. __simc (SYS_read, 0, (unsigned long)&c, 1, 0, 0);
  100. tty_insert_flip_char(tty, c, TTY_NORMAL);
  101. i++;
  102. }
  103. if (i)
  104. tty_flip_buffer_push(tty);
  105. mod_timer(&serial_timer, jiffies + SERIAL_TIMER_VALUE);
  106. spin_unlock(&timer_lock);
  107. }
  108. static int rs_put_char(struct tty_struct *tty, unsigned char ch)
  109. {
  110. char buf[2];
  111. buf[0] = ch;
  112. buf[1] = '\0'; /* Is this NULL necessary? */
  113. __simc (SYS_write, 1, (unsigned long) buf, 1, 0, 0);
  114. return 1;
  115. }
  116. static void rs_flush_chars(struct tty_struct *tty)
  117. {
  118. }
  119. static int rs_write_room(struct tty_struct *tty)
  120. {
  121. /* Let's say iss can always accept 2K characters.. */
  122. return 2 * 1024;
  123. }
  124. static int rs_chars_in_buffer(struct tty_struct *tty)
  125. {
  126. /* the iss doesn't buffer characters */
  127. return 0;
  128. }
  129. static void rs_hangup(struct tty_struct *tty)
  130. {
  131. /* Stub, once again.. */
  132. }
  133. static void rs_wait_until_sent(struct tty_struct *tty, int timeout)
  134. {
  135. /* Stub, once again.. */
  136. }
  137. static int rs_proc_show(struct seq_file *m, void *v)
  138. {
  139. seq_printf(m, "serinfo:1.0 driver:%s\n", serial_version);
  140. return 0;
  141. }
  142. static int rs_proc_open(struct inode *inode, struct file *file)
  143. {
  144. return single_open(file, rs_proc_show, NULL);
  145. }
  146. static const struct file_operations rs_proc_fops = {
  147. .owner = THIS_MODULE,
  148. .open = rs_proc_open,
  149. .read = seq_read,
  150. .llseek = seq_lseek,
  151. .release = single_release,
  152. };
  153. static const struct tty_operations serial_ops = {
  154. .open = rs_open,
  155. .close = rs_close,
  156. .write = rs_write,
  157. .put_char = rs_put_char,
  158. .flush_chars = rs_flush_chars,
  159. .write_room = rs_write_room,
  160. .chars_in_buffer = rs_chars_in_buffer,
  161. .hangup = rs_hangup,
  162. .wait_until_sent = rs_wait_until_sent,
  163. .proc_fops = &rs_proc_fops,
  164. };
  165. int __init rs_init(void)
  166. {
  167. tty_port_init(&serial_port);
  168. serial_driver = alloc_tty_driver(SERIAL_MAX_NUM_LINES);
  169. printk ("%s %s\n", serial_name, serial_version);
  170. /* Initialize the tty_driver structure */
  171. serial_driver->driver_name = "iss_serial";
  172. serial_driver->name = "ttyS";
  173. serial_driver->major = TTY_MAJOR;
  174. serial_driver->minor_start = 64;
  175. serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
  176. serial_driver->subtype = SERIAL_TYPE_NORMAL;
  177. serial_driver->init_termios = tty_std_termios;
  178. serial_driver->init_termios.c_cflag =
  179. B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  180. serial_driver->flags = TTY_DRIVER_REAL_RAW;
  181. tty_set_operations(serial_driver, &serial_ops);
  182. if (tty_register_driver(serial_driver))
  183. panic("Couldn't register serial driver\n");
  184. return 0;
  185. }
  186. static __exit void rs_exit(void)
  187. {
  188. int error;
  189. if ((error = tty_unregister_driver(serial_driver)))
  190. printk("ISS_SERIAL: failed to unregister serial driver (%d)\n",
  191. error);
  192. put_tty_driver(serial_driver);
  193. }
  194. /* We use `late_initcall' instead of just `__initcall' as a workaround for
  195. * the fact that (1) simcons_tty_init can't be called before tty_init,
  196. * (2) tty_init is called via `module_init', (3) if statically linked,
  197. * module_init == device_init, and (4) there's no ordering of init lists.
  198. * We can do this easily because simcons is always statically linked, but
  199. * other tty drivers that depend on tty_init and which must use
  200. * `module_init' to declare their init routines are likely to be broken.
  201. */
  202. late_initcall(rs_init);
  203. #ifdef CONFIG_SERIAL_CONSOLE
  204. static void iss_console_write(struct console *co, const char *s, unsigned count)
  205. {
  206. int len = strlen(s);
  207. if (s != 0 && *s != 0)
  208. __simc (SYS_write, 1, (unsigned long)s,
  209. count < len ? count : len,0,0);
  210. }
  211. static struct tty_driver* iss_console_device(struct console *c, int *index)
  212. {
  213. *index = c->index;
  214. return serial_driver;
  215. }
  216. static struct console sercons = {
  217. .name = "ttyS",
  218. .write = iss_console_write,
  219. .device = iss_console_device,
  220. .flags = CON_PRINTBUFFER,
  221. .index = -1
  222. };
  223. static int __init iss_console_init(void)
  224. {
  225. register_console(&sercons);
  226. return 0;
  227. }
  228. console_initcall(iss_console_init);
  229. #endif /* CONFIG_SERIAL_CONSOLE */