hvc_opal.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * opal driver interface to hvc_console.c
  4. *
  5. * Copyright 2011 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  6. */
  7. #undef DEBUG
  8. #include <linux/types.h>
  9. #include <linux/init.h>
  10. #include <linux/delay.h>
  11. #include <linux/slab.h>
  12. #include <linux/console.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/export.h>
  16. #include <linux/interrupt.h>
  17. #include <asm/hvconsole.h>
  18. #include <asm/prom.h>
  19. #include <asm/firmware.h>
  20. #include <asm/hvsi.h>
  21. #include <asm/udbg.h>
  22. #include <asm/opal.h>
  23. #include "hvc_console.h"
  24. static const char hvc_opal_name[] = "hvc_opal";
  25. static const struct of_device_id hvc_opal_match[] = {
  26. { .name = "serial", .compatible = "ibm,opal-console-raw" },
  27. { .name = "serial", .compatible = "ibm,opal-console-hvsi" },
  28. { },
  29. };
  30. typedef enum hv_protocol {
  31. HV_PROTOCOL_RAW,
  32. HV_PROTOCOL_HVSI
  33. } hv_protocol_t;
  34. struct hvc_opal_priv {
  35. hv_protocol_t proto; /* Raw data or HVSI packets */
  36. struct hvsi_priv hvsi; /* HVSI specific data */
  37. };
  38. static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
  39. /* For early boot console */
  40. static struct hvc_opal_priv hvc_opal_boot_priv;
  41. static u32 hvc_opal_boot_termno;
  42. static const struct hv_ops hvc_opal_raw_ops = {
  43. .get_chars = opal_get_chars,
  44. .put_chars = opal_put_chars,
  45. .notifier_add = notifier_add_irq,
  46. .notifier_del = notifier_del_irq,
  47. .notifier_hangup = notifier_hangup_irq,
  48. };
  49. static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
  50. {
  51. struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
  52. if (WARN_ON(!pv))
  53. return -ENODEV;
  54. return hvsilib_get_chars(&pv->hvsi, buf, count);
  55. }
  56. static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  57. {
  58. struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
  59. if (WARN_ON(!pv))
  60. return -ENODEV;
  61. return hvsilib_put_chars(&pv->hvsi, buf, count);
  62. }
  63. static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
  64. {
  65. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  66. int rc;
  67. pr_devel("HVSI@%x: do open !\n", hp->vtermno);
  68. rc = notifier_add_irq(hp, data);
  69. if (rc)
  70. return rc;
  71. return hvsilib_open(&pv->hvsi, hp);
  72. }
  73. static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
  74. {
  75. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  76. pr_devel("HVSI@%x: do close !\n", hp->vtermno);
  77. hvsilib_close(&pv->hvsi, hp);
  78. notifier_del_irq(hp, data);
  79. }
  80. void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
  81. {
  82. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  83. pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
  84. hvsilib_close(&pv->hvsi, hp);
  85. notifier_hangup_irq(hp, data);
  86. }
  87. static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
  88. {
  89. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  90. if (!pv)
  91. return -EINVAL;
  92. return pv->hvsi.mctrl;
  93. }
  94. static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  95. unsigned int clear)
  96. {
  97. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  98. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  99. hp->vtermno, set, clear);
  100. if (set & TIOCM_DTR)
  101. hvsilib_write_mctrl(&pv->hvsi, 1);
  102. else if (clear & TIOCM_DTR)
  103. hvsilib_write_mctrl(&pv->hvsi, 0);
  104. return 0;
  105. }
  106. static const struct hv_ops hvc_opal_hvsi_ops = {
  107. .get_chars = hvc_opal_hvsi_get_chars,
  108. .put_chars = hvc_opal_hvsi_put_chars,
  109. .notifier_add = hvc_opal_hvsi_open,
  110. .notifier_del = hvc_opal_hvsi_close,
  111. .notifier_hangup = hvc_opal_hvsi_hangup,
  112. .tiocmget = hvc_opal_hvsi_tiocmget,
  113. .tiocmset = hvc_opal_hvsi_tiocmset,
  114. };
  115. static int hvc_opal_probe(struct platform_device *dev)
  116. {
  117. const struct hv_ops *ops;
  118. struct hvc_struct *hp;
  119. struct hvc_opal_priv *pv;
  120. hv_protocol_t proto;
  121. unsigned int termno, irq, boot = 0;
  122. const __be32 *reg;
  123. if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
  124. proto = HV_PROTOCOL_RAW;
  125. ops = &hvc_opal_raw_ops;
  126. } else if (of_device_is_compatible(dev->dev.of_node,
  127. "ibm,opal-console-hvsi")) {
  128. proto = HV_PROTOCOL_HVSI;
  129. ops = &hvc_opal_hvsi_ops;
  130. } else {
  131. pr_err("hvc_opal: Unknown protocol for %pOF\n",
  132. dev->dev.of_node);
  133. return -ENXIO;
  134. }
  135. reg = of_get_property(dev->dev.of_node, "reg", NULL);
  136. termno = reg ? be32_to_cpup(reg) : 0;
  137. /* Is it our boot one ? */
  138. if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
  139. pv = hvc_opal_privs[termno];
  140. boot = 1;
  141. } else if (hvc_opal_privs[termno] == NULL) {
  142. pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
  143. if (!pv)
  144. return -ENOMEM;
  145. pv->proto = proto;
  146. hvc_opal_privs[termno] = pv;
  147. if (proto == HV_PROTOCOL_HVSI)
  148. hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars,
  149. termno, 0);
  150. /* Instanciate now to establish a mapping index==vtermno */
  151. hvc_instantiate(termno, termno, ops);
  152. } else {
  153. pr_err("hvc_opal: Device %pOF has duplicate terminal number #%d\n",
  154. dev->dev.of_node, termno);
  155. return -ENXIO;
  156. }
  157. pr_info("hvc%d: %s protocol on %pOF%s\n", termno,
  158. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
  159. dev->dev.of_node,
  160. boot ? " (boot console)" : "");
  161. irq = irq_of_parse_and_map(dev->dev.of_node, 0);
  162. if (!irq) {
  163. pr_info("hvc%d: No interrupts property, using OPAL event\n",
  164. termno);
  165. irq = opal_event_request(ilog2(OPAL_EVENT_CONSOLE_INPUT));
  166. }
  167. if (!irq) {
  168. pr_err("hvc_opal: Unable to map interrupt for device %pOF\n",
  169. dev->dev.of_node);
  170. return irq;
  171. }
  172. hp = hvc_alloc(termno, irq, ops, MAX_VIO_PUT_CHARS);
  173. if (IS_ERR(hp))
  174. return PTR_ERR(hp);
  175. /* hvc consoles on powernv may need to share a single irq */
  176. hp->flags = IRQF_SHARED;
  177. dev_set_drvdata(&dev->dev, hp);
  178. return 0;
  179. }
  180. static int hvc_opal_remove(struct platform_device *dev)
  181. {
  182. struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
  183. int rc, termno;
  184. termno = hp->vtermno;
  185. rc = hvc_remove(hp);
  186. if (rc == 0) {
  187. if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
  188. kfree(hvc_opal_privs[termno]);
  189. hvc_opal_privs[termno] = NULL;
  190. }
  191. return rc;
  192. }
  193. static struct platform_driver hvc_opal_driver = {
  194. .probe = hvc_opal_probe,
  195. .remove = hvc_opal_remove,
  196. .driver = {
  197. .name = hvc_opal_name,
  198. .of_match_table = hvc_opal_match,
  199. }
  200. };
  201. static int __init hvc_opal_init(void)
  202. {
  203. if (!firmware_has_feature(FW_FEATURE_OPAL))
  204. return -ENODEV;
  205. /* Register as a vio device to receive callbacks */
  206. return platform_driver_register(&hvc_opal_driver);
  207. }
  208. device_initcall(hvc_opal_init);
  209. static void udbg_opal_putc(char c)
  210. {
  211. unsigned int termno = hvc_opal_boot_termno;
  212. int count = -1;
  213. if (c == '\n')
  214. udbg_opal_putc('\r');
  215. do {
  216. switch(hvc_opal_boot_priv.proto) {
  217. case HV_PROTOCOL_RAW:
  218. count = opal_put_chars(termno, &c, 1);
  219. break;
  220. case HV_PROTOCOL_HVSI:
  221. count = hvc_opal_hvsi_put_chars(termno, &c, 1);
  222. break;
  223. }
  224. } while(count == 0 || count == -EAGAIN);
  225. }
  226. static int udbg_opal_getc_poll(void)
  227. {
  228. unsigned int termno = hvc_opal_boot_termno;
  229. int rc = 0;
  230. char c;
  231. switch(hvc_opal_boot_priv.proto) {
  232. case HV_PROTOCOL_RAW:
  233. rc = opal_get_chars(termno, &c, 1);
  234. break;
  235. case HV_PROTOCOL_HVSI:
  236. rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
  237. break;
  238. }
  239. if (!rc)
  240. return -1;
  241. return c;
  242. }
  243. static int udbg_opal_getc(void)
  244. {
  245. int ch;
  246. for (;;) {
  247. ch = udbg_opal_getc_poll();
  248. if (ch == -1) {
  249. /* This shouldn't be needed...but... */
  250. volatile unsigned long delay;
  251. for (delay=0; delay < 2000000; delay++)
  252. ;
  253. } else {
  254. return ch;
  255. }
  256. }
  257. }
  258. static void udbg_init_opal_common(void)
  259. {
  260. udbg_putc = udbg_opal_putc;
  261. udbg_getc = udbg_opal_getc;
  262. udbg_getc_poll = udbg_opal_getc_poll;
  263. tb_ticks_per_usec = 0x200; /* Make udelay not suck */
  264. }
  265. void __init hvc_opal_init_early(void)
  266. {
  267. struct device_node *stdout_node = of_node_get(of_stdout);
  268. const __be32 *termno;
  269. const struct hv_ops *ops;
  270. u32 index;
  271. /* If the console wasn't in /chosen, try /ibm,opal */
  272. if (!stdout_node) {
  273. struct device_node *opal, *np;
  274. /* Current OPAL takeover doesn't provide the stdout
  275. * path, so we hard wire it
  276. */
  277. opal = of_find_node_by_path("/ibm,opal/consoles");
  278. if (opal)
  279. pr_devel("hvc_opal: Found consoles in new location\n");
  280. if (!opal) {
  281. opal = of_find_node_by_path("/ibm,opal");
  282. if (opal)
  283. pr_devel("hvc_opal: "
  284. "Found consoles in old location\n");
  285. }
  286. if (!opal)
  287. return;
  288. for_each_child_of_node(opal, np) {
  289. if (!strcmp(np->name, "serial")) {
  290. stdout_node = np;
  291. break;
  292. }
  293. }
  294. of_node_put(opal);
  295. }
  296. if (!stdout_node)
  297. return;
  298. termno = of_get_property(stdout_node, "reg", NULL);
  299. index = termno ? be32_to_cpup(termno) : 0;
  300. if (index >= MAX_NR_HVC_CONSOLES)
  301. return;
  302. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  303. /* Check the protocol */
  304. if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
  305. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  306. ops = &hvc_opal_raw_ops;
  307. pr_devel("hvc_opal: Found RAW console\n");
  308. }
  309. else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
  310. hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
  311. ops = &hvc_opal_hvsi_ops;
  312. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
  313. opal_put_chars, index, 1);
  314. /* HVSI, perform the handshake now */
  315. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  316. pr_devel("hvc_opal: Found HVSI console\n");
  317. } else
  318. goto out;
  319. hvc_opal_boot_termno = index;
  320. udbg_init_opal_common();
  321. add_preferred_console("hvc", index, NULL);
  322. hvc_instantiate(index, index, ops);
  323. out:
  324. of_node_put(stdout_node);
  325. }
  326. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
  327. void __init udbg_init_debug_opal_raw(void)
  328. {
  329. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  330. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  331. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  332. hvc_opal_boot_termno = index;
  333. udbg_init_opal_common();
  334. }
  335. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
  336. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
  337. void __init udbg_init_debug_opal_hvsi(void)
  338. {
  339. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  340. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  341. hvc_opal_boot_termno = index;
  342. udbg_init_opal_common();
  343. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars,
  344. index, 1);
  345. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  346. }
  347. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */