hvc_vio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * vio driver interface to hvc_console.c
  3. *
  4. * This code was moved here to allow the remaining code to be reused as a
  5. * generic polling mode with semi-reliable transport driver core to the
  6. * console and tty subsystems.
  7. *
  8. *
  9. * Copyright (C) 2001 Anton Blanchard <anton@au.ibm.com>, IBM
  10. * Copyright (C) 2001 Paul Mackerras <paulus@au.ibm.com>, IBM
  11. * Copyright (C) 2004 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  12. * Copyright (C) 2004 IBM Corporation
  13. *
  14. * Additional Author(s):
  15. * Ryan S. Arnold <rsa@us.ibm.com>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. *
  31. * TODO:
  32. *
  33. * - handle error in sending hvsi protocol packets
  34. * - retry nego on subsequent sends ?
  35. */
  36. #undef DEBUG
  37. #include <linux/types.h>
  38. #include <linux/init.h>
  39. #include <linux/delay.h>
  40. #include <linux/slab.h>
  41. #include <linux/console.h>
  42. #include <asm/hvconsole.h>
  43. #include <asm/vio.h>
  44. #include <asm/prom.h>
  45. #include <asm/firmware.h>
  46. #include <asm/hvsi.h>
  47. #include <asm/udbg.h>
  48. #include "hvc_console.h"
  49. static const char hvc_driver_name[] = "hvc_console";
  50. static struct vio_device_id hvc_driver_table[] __devinitdata = {
  51. {"serial", "hvterm1"},
  52. #ifndef HVC_OLD_HVSI
  53. {"serial", "hvterm-protocol"},
  54. #endif
  55. { "", "" }
  56. };
  57. MODULE_DEVICE_TABLE(vio, hvc_driver_table);
  58. typedef enum hv_protocol {
  59. HV_PROTOCOL_RAW,
  60. HV_PROTOCOL_HVSI
  61. } hv_protocol_t;
  62. struct hvterm_priv {
  63. u32 termno; /* HV term number */
  64. hv_protocol_t proto; /* Raw data or HVSI packets */
  65. struct hvsi_priv hvsi; /* HVSI specific data */
  66. };
  67. static struct hvterm_priv *hvterm_privs[MAX_NR_HVC_CONSOLES];
  68. /* For early boot console */
  69. static struct hvterm_priv hvterm_priv0;
  70. static int hvterm_raw_get_chars(uint32_t vtermno, char *buf, int count)
  71. {
  72. struct hvterm_priv *pv = hvterm_privs[vtermno];
  73. unsigned long got, i;
  74. if (WARN_ON(!pv))
  75. return 0;
  76. /*
  77. * Vio firmware will read up to SIZE_VIO_GET_CHARS at its own discretion
  78. * so we play safe and avoid the situation where got > count which could
  79. * overload the flip buffer.
  80. */
  81. if (count < SIZE_VIO_GET_CHARS)
  82. return -EAGAIN;
  83. got = hvc_get_chars(pv->termno, buf, count);
  84. /*
  85. * Work around a HV bug where it gives us a null
  86. * after every \r. -- paulus
  87. */
  88. for (i = 1; i < got; ++i) {
  89. if (buf[i] == 0 && buf[i-1] == '\r') {
  90. --got;
  91. if (i < got)
  92. memmove(&buf[i], &buf[i+1], got - i);
  93. }
  94. }
  95. return got;
  96. }
  97. static int hvterm_raw_put_chars(uint32_t vtermno, const char *buf, int count)
  98. {
  99. struct hvterm_priv *pv = hvterm_privs[vtermno];
  100. if (WARN_ON(!pv))
  101. return 0;
  102. return hvc_put_chars(pv->termno, buf, count);
  103. }
  104. static const struct hv_ops hvterm_raw_ops = {
  105. .get_chars = hvterm_raw_get_chars,
  106. .put_chars = hvterm_raw_put_chars,
  107. .notifier_add = notifier_add_irq,
  108. .notifier_del = notifier_del_irq,
  109. .notifier_hangup = notifier_hangup_irq,
  110. };
  111. static int hvterm_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
  112. {
  113. struct hvterm_priv *pv = hvterm_privs[vtermno];
  114. if (WARN_ON(!pv))
  115. return 0;
  116. return hvsi_get_chars(&pv->hvsi, buf, count);
  117. }
  118. static int hvterm_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  119. {
  120. struct hvterm_priv *pv = hvterm_privs[vtermno];
  121. if (WARN_ON(!pv))
  122. return 0;
  123. return hvsi_put_chars(&pv->hvsi, buf, count);
  124. }
  125. static int hvterm_hvsi_open(struct hvc_struct *hp, int data)
  126. {
  127. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  128. int rc;
  129. pr_devel("HVSI@%x: open !\n", pv->termno);
  130. rc = notifier_add_irq(hp, data);
  131. if (rc)
  132. return rc;
  133. return hvsi_open(&pv->hvsi, hp);
  134. }
  135. static void hvterm_hvsi_close(struct hvc_struct *hp, int data)
  136. {
  137. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  138. pr_devel("HVSI@%x: do close !\n", pv->termno);
  139. hvsi_close(&pv->hvsi, hp);
  140. notifier_del_irq(hp, data);
  141. }
  142. void hvterm_hvsi_hangup(struct hvc_struct *hp, int data)
  143. {
  144. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  145. pr_devel("HVSI@%x: do hangup !\n", pv->termno);
  146. hvsi_close(&pv->hvsi, hp);
  147. notifier_hangup_irq(hp, data);
  148. }
  149. static int hvterm_hvsi_tiocmget(struct hvc_struct *hp)
  150. {
  151. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  152. if (!pv)
  153. return -EINVAL;
  154. return pv->hvsi.mctrl;
  155. }
  156. static int hvterm_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  157. unsigned int clear)
  158. {
  159. struct hvterm_priv *pv = hvterm_privs[hp->vtermno];
  160. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  161. pv->termno, set, clear);
  162. if (set & TIOCM_DTR)
  163. hvsi_write_mctrl(&pv->hvsi, 1);
  164. else if (clear & TIOCM_DTR)
  165. hvsi_write_mctrl(&pv->hvsi, 0);
  166. return 0;
  167. }
  168. static const struct hv_ops hvterm_hvsi_ops = {
  169. .get_chars = hvterm_hvsi_get_chars,
  170. .put_chars = hvterm_hvsi_put_chars,
  171. .notifier_add = hvterm_hvsi_open,
  172. .notifier_del = hvterm_hvsi_close,
  173. .notifier_hangup = hvterm_hvsi_hangup,
  174. .tiocmget = hvterm_hvsi_tiocmget,
  175. .tiocmset = hvterm_hvsi_tiocmset,
  176. };
  177. static int __devinit hvc_vio_probe(struct vio_dev *vdev,
  178. const struct vio_device_id *id)
  179. {
  180. const struct hv_ops *ops;
  181. struct hvc_struct *hp;
  182. struct hvterm_priv *pv;
  183. hv_protocol_t proto;
  184. int i, termno = -1;
  185. /* probed with invalid parameters. */
  186. if (!vdev || !id)
  187. return -EPERM;
  188. if (of_device_is_compatible(vdev->dev.of_node, "hvterm1")) {
  189. proto = HV_PROTOCOL_RAW;
  190. ops = &hvterm_raw_ops;
  191. } else if (of_device_is_compatible(vdev->dev.of_node, "hvterm-protocol")) {
  192. proto = HV_PROTOCOL_HVSI;
  193. ops = &hvterm_hvsi_ops;
  194. } else {
  195. pr_err("hvc_vio: Unkown protocol for %s\n", vdev->dev.of_node->full_name);
  196. return -ENXIO;
  197. }
  198. pr_devel("hvc_vio_probe() device %s, using %s protocol\n",
  199. vdev->dev.of_node->full_name,
  200. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi");
  201. /* Is it our boot one ? */
  202. if (hvterm_privs[0] == &hvterm_priv0 &&
  203. vdev->unit_address == hvterm_priv0.termno) {
  204. pv = hvterm_privs[0];
  205. termno = 0;
  206. pr_devel("->boot console, using termno 0\n");
  207. }
  208. /* nope, allocate a new one */
  209. else {
  210. for (i = 0; i < MAX_NR_HVC_CONSOLES && termno < 0; i++)
  211. if (!hvterm_privs[i])
  212. termno = i;
  213. pr_devel("->non-boot console, using termno %d\n", termno);
  214. if (termno < 0)
  215. return -ENODEV;
  216. pv = kzalloc(sizeof(struct hvterm_priv), GFP_KERNEL);
  217. if (!pv)
  218. return -ENOMEM;
  219. pv->termno = vdev->unit_address;
  220. pv->proto = proto;
  221. hvterm_privs[termno] = pv;
  222. hvsi_init(&pv->hvsi, hvc_get_chars, hvc_put_chars,
  223. pv->termno, 0);
  224. }
  225. hp = hvc_alloc(termno, vdev->irq, ops, MAX_VIO_PUT_CHARS);
  226. if (IS_ERR(hp))
  227. return PTR_ERR(hp);
  228. dev_set_drvdata(&vdev->dev, hp);
  229. return 0;
  230. }
  231. static int __devexit hvc_vio_remove(struct vio_dev *vdev)
  232. {
  233. struct hvc_struct *hp = dev_get_drvdata(&vdev->dev);
  234. int rc, termno;
  235. termno = hp->vtermno;
  236. rc = hvc_remove(hp);
  237. if (rc == 0) {
  238. if (hvterm_privs[termno] != &hvterm_priv0)
  239. kfree(hvterm_privs[termno]);
  240. hvterm_privs[termno] = NULL;
  241. }
  242. return rc;
  243. }
  244. static struct vio_driver hvc_vio_driver = {
  245. .id_table = hvc_driver_table,
  246. .probe = hvc_vio_probe,
  247. .remove = __devexit_p(hvc_vio_remove),
  248. .driver = {
  249. .name = hvc_driver_name,
  250. .owner = THIS_MODULE,
  251. }
  252. };
  253. static int __init hvc_vio_init(void)
  254. {
  255. int rc;
  256. if (firmware_has_feature(FW_FEATURE_ISERIES))
  257. return -EIO;
  258. /* Register as a vio device to receive callbacks */
  259. rc = vio_register_driver(&hvc_vio_driver);
  260. return rc;
  261. }
  262. module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
  263. static void __exit hvc_vio_exit(void)
  264. {
  265. vio_unregister_driver(&hvc_vio_driver);
  266. }
  267. module_exit(hvc_vio_exit);
  268. static void udbg_hvc_putc(char c)
  269. {
  270. int count = -1;
  271. if (c == '\n')
  272. udbg_hvc_putc('\r');
  273. do {
  274. switch(hvterm_priv0.proto) {
  275. case HV_PROTOCOL_RAW:
  276. count = hvterm_raw_put_chars(0, &c, 1);
  277. break;
  278. case HV_PROTOCOL_HVSI:
  279. count = hvterm_hvsi_put_chars(0, &c, 1);
  280. break;
  281. }
  282. } while(count == 0);
  283. }
  284. static int udbg_hvc_getc_poll(void)
  285. {
  286. int rc = 0;
  287. char c;
  288. switch(hvterm_priv0.proto) {
  289. case HV_PROTOCOL_RAW:
  290. rc = hvterm_raw_get_chars(0, &c, 1);
  291. break;
  292. case HV_PROTOCOL_HVSI:
  293. rc = hvterm_hvsi_get_chars(0, &c, 1);
  294. break;
  295. }
  296. if (!rc)
  297. return -1;
  298. return c;
  299. }
  300. static int udbg_hvc_getc(void)
  301. {
  302. int ch;
  303. for (;;) {
  304. ch = udbg_hvc_getc_poll();
  305. if (ch == -1) {
  306. /* This shouldn't be needed...but... */
  307. volatile unsigned long delay;
  308. for (delay=0; delay < 2000000; delay++)
  309. ;
  310. } else {
  311. return ch;
  312. }
  313. }
  314. }
  315. void __init hvc_vio_init_early(void)
  316. {
  317. struct device_node *stdout_node;
  318. const u32 *termno;
  319. const char *name;
  320. const struct hv_ops *ops;
  321. /* find the boot console from /chosen/stdout */
  322. if (!of_chosen)
  323. return;
  324. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  325. if (name == NULL)
  326. return;
  327. stdout_node = of_find_node_by_path(name);
  328. if (!stdout_node)
  329. return;
  330. name = of_get_property(stdout_node, "name", NULL);
  331. if (!name) {
  332. printk(KERN_WARNING "stdout node missing 'name' property!\n");
  333. goto out;
  334. }
  335. /* Check if it's a virtual terminal */
  336. if (strncmp(name, "vty", 3) != 0)
  337. goto out;
  338. termno = of_get_property(stdout_node, "reg", NULL);
  339. if (termno == NULL)
  340. goto out;
  341. hvterm_priv0.termno = *termno;
  342. hvterm_privs[0] = &hvterm_priv0;
  343. /* Check the protocol */
  344. if (of_device_is_compatible(stdout_node, "hvterm1")) {
  345. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  346. ops = &hvterm_raw_ops;
  347. }
  348. else if (of_device_is_compatible(stdout_node, "hvterm-protocol")) {
  349. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  350. ops = &hvterm_hvsi_ops;
  351. hvsi_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  352. hvterm_priv0.termno, 1);
  353. /* HVSI, perform the handshake now */
  354. hvsi_establish(&hvterm_priv0.hvsi);
  355. } else
  356. goto out;
  357. udbg_putc = udbg_hvc_putc;
  358. udbg_getc = udbg_hvc_getc;
  359. udbg_getc_poll = udbg_hvc_getc_poll;
  360. #ifdef HVC_OLD_HVSI
  361. /* When using the old HVSI driver don't register the HVC
  362. * backend for HVSI, only do udbg
  363. */
  364. if (hvterm_priv0.proto == HV_PROTOCOL_HVSI)
  365. goto out;
  366. #endif
  367. add_preferred_console("hvc", 0, NULL);
  368. hvc_instantiate(0, 0, ops);
  369. out:
  370. of_node_put(stdout_node);
  371. }
  372. /* call this from early_init() for a working debug console on
  373. * vterm capable LPAR machines
  374. */
  375. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR
  376. void __init udbg_init_debug_lpar(void)
  377. {
  378. hvterm_privs[0] = &hvterm_priv0;
  379. hvterm_priv0.termno = 0;
  380. hvterm_priv0.proto = HV_PROTOCOL_RAW;
  381. udbg_putc = udbg_hvc_putc;
  382. udbg_getc = udbg_hvc_getc;
  383. udbg_getc_poll = udbg_hvc_getc_poll;
  384. }
  385. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR */
  386. #ifdef CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI
  387. void __init udbg_init_debug_lpar_hvsi(void)
  388. {
  389. hvterm_privs[0] = &hvterm_priv0;
  390. hvterm_priv0.termno = CONFIG_PPC_EARLY_DEBUG_HVSI_VTERMNO;
  391. hvterm_priv0.proto = HV_PROTOCOL_HVSI;
  392. udbg_putc = udbg_hvc_putc;
  393. udbg_getc = udbg_hvc_getc;
  394. udbg_getc_poll = udbg_hvc_getc_poll;
  395. hvsi_init(&hvterm_priv0.hvsi, hvc_get_chars, hvc_put_chars,
  396. hvterm_priv0.termno, 1);
  397. hvsi_establish(&hvterm_priv0.hvsi);
  398. }
  399. #endif /* CONFIG_PPC_EARLY_DEBUG_LPAR_HVSI */