suncore.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* suncore.c
  3. *
  4. * Common SUN serial routines. Based entirely
  5. * upon drivers/sbus/char/sunserial.c which is:
  6. *
  7. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  8. *
  9. * Adaptation to new UART layer is:
  10. *
  11. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/console.h>
  15. #include <linux/tty.h>
  16. #include <linux/errno.h>
  17. #include <linux/string.h>
  18. #include <linux/serial_core.h>
  19. #include <linux/sunserialcore.h>
  20. #include <linux/init.h>
  21. #include <asm/prom.h>
  22. static int sunserial_current_minor = 64;
  23. int sunserial_register_minors(struct uart_driver *drv, int count)
  24. {
  25. int err = 0;
  26. drv->minor = sunserial_current_minor;
  27. drv->nr += count;
  28. /* Register the driver on the first call */
  29. if (drv->nr == count)
  30. err = uart_register_driver(drv);
  31. if (err == 0) {
  32. sunserial_current_minor += count;
  33. drv->tty_driver->name_base = drv->minor - 64;
  34. }
  35. return err;
  36. }
  37. EXPORT_SYMBOL(sunserial_register_minors);
  38. void sunserial_unregister_minors(struct uart_driver *drv, int count)
  39. {
  40. drv->nr -= count;
  41. sunserial_current_minor -= count;
  42. if (drv->nr == 0)
  43. uart_unregister_driver(drv);
  44. }
  45. EXPORT_SYMBOL(sunserial_unregister_minors);
  46. int sunserial_console_match(struct console *con, struct device_node *dp,
  47. struct uart_driver *drv, int line, bool ignore_line)
  48. {
  49. if (!con)
  50. return 0;
  51. drv->cons = con;
  52. if (of_console_device != dp)
  53. return 0;
  54. if (!ignore_line) {
  55. int off = 0;
  56. if (of_console_options &&
  57. *of_console_options == 'b')
  58. off = 1;
  59. if ((line & 1) != off)
  60. return 0;
  61. }
  62. if (!console_set_on_cmdline) {
  63. con->index = line;
  64. add_preferred_console(con->name, line, NULL);
  65. }
  66. return 1;
  67. }
  68. EXPORT_SYMBOL(sunserial_console_match);
  69. void sunserial_console_termios(struct console *con, struct device_node *uart_dp)
  70. {
  71. const char *mode, *s;
  72. char mode_prop[] = "ttyX-mode";
  73. int baud, bits, stop, cflag;
  74. char parity;
  75. if (of_node_name_eq(uart_dp, "rsc") ||
  76. of_node_name_eq(uart_dp, "rsc-console") ||
  77. of_node_name_eq(uart_dp, "rsc-control")) {
  78. mode = of_get_property(uart_dp,
  79. "ssp-console-modes", NULL);
  80. if (!mode)
  81. mode = "115200,8,n,1,-";
  82. } else if (of_node_name_eq(uart_dp, "lom-console")) {
  83. mode = "9600,8,n,1,-";
  84. } else {
  85. struct device_node *dp;
  86. char c;
  87. c = 'a';
  88. if (of_console_options)
  89. c = *of_console_options;
  90. mode_prop[3] = c;
  91. dp = of_find_node_by_path("/options");
  92. mode = of_get_property(dp, mode_prop, NULL);
  93. if (!mode)
  94. mode = "9600,8,n,1,-";
  95. of_node_put(dp);
  96. }
  97. cflag = CREAD | HUPCL | CLOCAL;
  98. s = mode;
  99. baud = simple_strtoul(s, NULL, 0);
  100. s = strchr(s, ',');
  101. bits = simple_strtoul(++s, NULL, 0);
  102. s = strchr(s, ',');
  103. parity = *(++s);
  104. s = strchr(s, ',');
  105. stop = simple_strtoul(++s, NULL, 0);
  106. s = strchr(s, ',');
  107. /* XXX handshake is not handled here. */
  108. switch (baud) {
  109. case 150: cflag |= B150; break;
  110. case 300: cflag |= B300; break;
  111. case 600: cflag |= B600; break;
  112. case 1200: cflag |= B1200; break;
  113. case 2400: cflag |= B2400; break;
  114. case 4800: cflag |= B4800; break;
  115. case 9600: cflag |= B9600; break;
  116. case 19200: cflag |= B19200; break;
  117. case 38400: cflag |= B38400; break;
  118. case 57600: cflag |= B57600; break;
  119. case 115200: cflag |= B115200; break;
  120. case 230400: cflag |= B230400; break;
  121. case 460800: cflag |= B460800; break;
  122. default: baud = 9600; cflag |= B9600; break;
  123. }
  124. switch (bits) {
  125. case 5: cflag |= CS5; break;
  126. case 6: cflag |= CS6; break;
  127. case 7: cflag |= CS7; break;
  128. case 8: cflag |= CS8; break;
  129. default: cflag |= CS8; break;
  130. }
  131. switch (parity) {
  132. case 'o': cflag |= (PARENB | PARODD); break;
  133. case 'e': cflag |= PARENB; break;
  134. case 'n': default: break;
  135. }
  136. switch (stop) {
  137. case 2: cflag |= CSTOPB; break;
  138. case 1: default: break;
  139. }
  140. con->cflag = cflag;
  141. }
  142. /* Sun serial MOUSE auto baud rate detection. */
  143. static struct mouse_baud_cflag {
  144. int baud;
  145. unsigned int cflag;
  146. } mouse_baud_table[] = {
  147. { 1200, B1200 },
  148. { 2400, B2400 },
  149. { 4800, B4800 },
  150. { 9600, B9600 },
  151. { -1, ~0 },
  152. { -1, ~0 },
  153. };
  154. unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
  155. {
  156. int i;
  157. for (i = 0; mouse_baud_table[i].baud != -1; i++)
  158. if (mouse_baud_table[i].cflag == (cflag & CBAUD))
  159. break;
  160. i += 1;
  161. if (mouse_baud_table[i].baud == -1)
  162. i = 0;
  163. *new_baud = mouse_baud_table[i].baud;
  164. return mouse_baud_table[i].cflag;
  165. }
  166. EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
  167. /* Basically, when the baud rate is wrong the mouse spits out
  168. * breaks to us.
  169. */
  170. int suncore_mouse_baud_detection(unsigned char ch, int is_break)
  171. {
  172. static int mouse_got_break = 0;
  173. static int ctr = 0;
  174. if (is_break) {
  175. /* Let a few normal bytes go by before we jump the gun
  176. * and say we need to try another baud rate.
  177. */
  178. if (mouse_got_break && ctr < 8)
  179. return 1;
  180. /* Ok, we need to try another baud. */
  181. ctr = 0;
  182. mouse_got_break = 1;
  183. return 2;
  184. }
  185. if (mouse_got_break) {
  186. ctr++;
  187. if (ch == 0x87) {
  188. /* Correct baud rate determined. */
  189. mouse_got_break = 0;
  190. }
  191. return 1;
  192. }
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(suncore_mouse_baud_detection);
  196. static int __init suncore_init(void)
  197. {
  198. return 0;
  199. }
  200. device_initcall(suncore_init);
  201. #if 0 /* ..def MODULE ; never supported as such */
  202. MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
  203. MODULE_DESCRIPTION("Sun serial common layer");
  204. MODULE_LICENSE("GPL");
  205. #endif