hvc_opal.c 10 KB

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