hvc_opal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 <asm/hvconsole.h>
  31. #include <asm/prom.h>
  32. #include <asm/firmware.h>
  33. #include <asm/hvsi.h>
  34. #include <asm/udbg.h>
  35. #include <asm/opal.h>
  36. #include "hvc_console.h"
  37. static const char hvc_opal_name[] = "hvc_opal";
  38. static const struct of_device_id hvc_opal_match[] = {
  39. { .name = "serial", .compatible = "ibm,opal-console-raw" },
  40. { .name = "serial", .compatible = "ibm,opal-console-hvsi" },
  41. { },
  42. };
  43. typedef enum hv_protocol {
  44. HV_PROTOCOL_RAW,
  45. HV_PROTOCOL_HVSI
  46. } hv_protocol_t;
  47. struct hvc_opal_priv {
  48. hv_protocol_t proto; /* Raw data or HVSI packets */
  49. struct hvsi_priv hvsi; /* HVSI specific data */
  50. };
  51. static struct hvc_opal_priv *hvc_opal_privs[MAX_NR_HVC_CONSOLES];
  52. /* For early boot console */
  53. static struct hvc_opal_priv hvc_opal_boot_priv;
  54. static u32 hvc_opal_boot_termno;
  55. static bool hvc_opal_event_registered;
  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_console_event(struct notifier_block *nb,
  130. unsigned long events, void *change)
  131. {
  132. if (events & OPAL_EVENT_CONSOLE_INPUT)
  133. hvc_kick();
  134. return 0;
  135. }
  136. static struct notifier_block hvc_opal_console_nb = {
  137. .notifier_call = hvc_opal_console_event,
  138. };
  139. static int hvc_opal_probe(struct platform_device *dev)
  140. {
  141. const struct hv_ops *ops;
  142. struct hvc_struct *hp;
  143. struct hvc_opal_priv *pv;
  144. hv_protocol_t proto;
  145. unsigned int termno, boot = 0;
  146. const __be32 *reg;
  147. if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
  148. proto = HV_PROTOCOL_RAW;
  149. ops = &hvc_opal_raw_ops;
  150. } else if (of_device_is_compatible(dev->dev.of_node,
  151. "ibm,opal-console-hvsi")) {
  152. proto = HV_PROTOCOL_HVSI;
  153. ops = &hvc_opal_hvsi_ops;
  154. } else {
  155. pr_err("hvc_opal: Unknown protocol for %s\n",
  156. dev->dev.of_node->full_name);
  157. return -ENXIO;
  158. }
  159. reg = of_get_property(dev->dev.of_node, "reg", NULL);
  160. termno = reg ? be32_to_cpup(reg) : 0;
  161. /* Is it our boot one ? */
  162. if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
  163. pv = hvc_opal_privs[termno];
  164. boot = 1;
  165. } else if (hvc_opal_privs[termno] == NULL) {
  166. pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
  167. if (!pv)
  168. return -ENOMEM;
  169. pv->proto = proto;
  170. hvc_opal_privs[termno] = pv;
  171. if (proto == HV_PROTOCOL_HVSI)
  172. hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars,
  173. termno, 0);
  174. /* Instanciate now to establish a mapping index==vtermno */
  175. hvc_instantiate(termno, termno, ops);
  176. } else {
  177. pr_err("hvc_opal: Device %s has duplicate terminal number #%d\n",
  178. dev->dev.of_node->full_name, termno);
  179. return -ENXIO;
  180. }
  181. pr_info("hvc%d: %s protocol on %s%s\n", termno,
  182. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
  183. dev->dev.of_node->full_name,
  184. boot ? " (boot console)" : "");
  185. /* We don't do IRQ ... */
  186. hp = hvc_alloc(termno, 0, ops, MAX_VIO_PUT_CHARS);
  187. if (IS_ERR(hp))
  188. return PTR_ERR(hp);
  189. dev_set_drvdata(&dev->dev, hp);
  190. /* ... but we use OPAL event to kick the console */
  191. if (!hvc_opal_event_registered) {
  192. opal_notifier_register(&hvc_opal_console_nb);
  193. hvc_opal_event_registered = true;
  194. }
  195. return 0;
  196. }
  197. static int hvc_opal_remove(struct platform_device *dev)
  198. {
  199. struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
  200. int rc, termno;
  201. termno = hp->vtermno;
  202. rc = hvc_remove(hp);
  203. if (rc == 0) {
  204. if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
  205. kfree(hvc_opal_privs[termno]);
  206. hvc_opal_privs[termno] = NULL;
  207. }
  208. return rc;
  209. }
  210. static struct platform_driver hvc_opal_driver = {
  211. .probe = hvc_opal_probe,
  212. .remove = hvc_opal_remove,
  213. .driver = {
  214. .name = hvc_opal_name,
  215. .of_match_table = hvc_opal_match,
  216. }
  217. };
  218. static int __init hvc_opal_init(void)
  219. {
  220. if (!firmware_has_feature(FW_FEATURE_OPAL))
  221. return -ENODEV;
  222. /* Register as a vio device to receive callbacks */
  223. return platform_driver_register(&hvc_opal_driver);
  224. }
  225. device_initcall(hvc_opal_init);
  226. static void udbg_opal_putc(char c)
  227. {
  228. unsigned int termno = hvc_opal_boot_termno;
  229. int count = -1;
  230. if (c == '\n')
  231. udbg_opal_putc('\r');
  232. do {
  233. switch(hvc_opal_boot_priv.proto) {
  234. case HV_PROTOCOL_RAW:
  235. count = opal_put_chars(termno, &c, 1);
  236. break;
  237. case HV_PROTOCOL_HVSI:
  238. count = hvc_opal_hvsi_put_chars(termno, &c, 1);
  239. break;
  240. }
  241. } while(count == 0 || count == -EAGAIN);
  242. }
  243. static int udbg_opal_getc_poll(void)
  244. {
  245. unsigned int termno = hvc_opal_boot_termno;
  246. int rc = 0;
  247. char c;
  248. switch(hvc_opal_boot_priv.proto) {
  249. case HV_PROTOCOL_RAW:
  250. rc = opal_get_chars(termno, &c, 1);
  251. break;
  252. case HV_PROTOCOL_HVSI:
  253. rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
  254. break;
  255. }
  256. if (!rc)
  257. return -1;
  258. return c;
  259. }
  260. static int udbg_opal_getc(void)
  261. {
  262. int ch;
  263. for (;;) {
  264. ch = udbg_opal_getc_poll();
  265. if (ch == -1) {
  266. /* This shouldn't be needed...but... */
  267. volatile unsigned long delay;
  268. for (delay=0; delay < 2000000; delay++)
  269. ;
  270. } else {
  271. return ch;
  272. }
  273. }
  274. }
  275. static void udbg_init_opal_common(void)
  276. {
  277. udbg_putc = udbg_opal_putc;
  278. udbg_getc = udbg_opal_getc;
  279. udbg_getc_poll = udbg_opal_getc_poll;
  280. tb_ticks_per_usec = 0x200; /* Make udelay not suck */
  281. }
  282. void __init hvc_opal_init_early(void)
  283. {
  284. struct device_node *stdout_node = of_node_get(of_stdout);
  285. const __be32 *termno;
  286. const struct hv_ops *ops;
  287. u32 index;
  288. /* If the console wasn't in /chosen, try /ibm,opal */
  289. if (!stdout_node) {
  290. struct device_node *opal, *np;
  291. /* Current OPAL takeover doesn't provide the stdout
  292. * path, so we hard wire it
  293. */
  294. opal = of_find_node_by_path("/ibm,opal/consoles");
  295. if (opal)
  296. pr_devel("hvc_opal: Found consoles in new location\n");
  297. if (!opal) {
  298. opal = of_find_node_by_path("/ibm,opal");
  299. if (opal)
  300. pr_devel("hvc_opal: "
  301. "Found consoles in old location\n");
  302. }
  303. if (!opal)
  304. return;
  305. for_each_child_of_node(opal, np) {
  306. if (!strcmp(np->name, "serial")) {
  307. stdout_node = np;
  308. break;
  309. }
  310. }
  311. of_node_put(opal);
  312. }
  313. if (!stdout_node)
  314. return;
  315. termno = of_get_property(stdout_node, "reg", NULL);
  316. index = termno ? be32_to_cpup(termno) : 0;
  317. if (index >= MAX_NR_HVC_CONSOLES)
  318. return;
  319. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  320. /* Check the protocol */
  321. if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
  322. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  323. ops = &hvc_opal_raw_ops;
  324. pr_devel("hvc_opal: Found RAW console\n");
  325. }
  326. else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
  327. hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
  328. ops = &hvc_opal_hvsi_ops;
  329. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
  330. opal_put_chars, index, 1);
  331. /* HVSI, perform the handshake now */
  332. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  333. pr_devel("hvc_opal: Found HVSI console\n");
  334. } else
  335. goto out;
  336. hvc_opal_boot_termno = index;
  337. udbg_init_opal_common();
  338. add_preferred_console("hvc", index, NULL);
  339. hvc_instantiate(index, index, ops);
  340. out:
  341. of_node_put(stdout_node);
  342. }
  343. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
  344. void __init udbg_init_debug_opal_raw(void)
  345. {
  346. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  347. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  348. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  349. hvc_opal_boot_termno = index;
  350. udbg_init_opal_common();
  351. }
  352. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
  353. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
  354. void __init udbg_init_debug_opal_hvsi(void)
  355. {
  356. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  357. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  358. hvc_opal_boot_termno = index;
  359. udbg_init_opal_common();
  360. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars,
  361. index, 1);
  362. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  363. }
  364. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */