hvc_opal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 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. .owner = THIS_MODULE,
  216. .of_match_table = hvc_opal_match,
  217. }
  218. };
  219. static int __init hvc_opal_init(void)
  220. {
  221. if (!firmware_has_feature(FW_FEATURE_OPAL))
  222. return -ENODEV;
  223. /* Register as a vio device to receive callbacks */
  224. return platform_driver_register(&hvc_opal_driver);
  225. }
  226. device_initcall(hvc_opal_init);
  227. static void udbg_opal_putc(char c)
  228. {
  229. unsigned int termno = hvc_opal_boot_termno;
  230. int count = -1;
  231. if (c == '\n')
  232. udbg_opal_putc('\r');
  233. do {
  234. switch(hvc_opal_boot_priv.proto) {
  235. case HV_PROTOCOL_RAW:
  236. count = opal_put_chars(termno, &c, 1);
  237. break;
  238. case HV_PROTOCOL_HVSI:
  239. count = hvc_opal_hvsi_put_chars(termno, &c, 1);
  240. break;
  241. }
  242. } while(count == 0 || count == -EAGAIN);
  243. }
  244. static int udbg_opal_getc_poll(void)
  245. {
  246. unsigned int termno = hvc_opal_boot_termno;
  247. int rc = 0;
  248. char c;
  249. switch(hvc_opal_boot_priv.proto) {
  250. case HV_PROTOCOL_RAW:
  251. rc = opal_get_chars(termno, &c, 1);
  252. break;
  253. case HV_PROTOCOL_HVSI:
  254. rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
  255. break;
  256. }
  257. if (!rc)
  258. return -1;
  259. return c;
  260. }
  261. static int udbg_opal_getc(void)
  262. {
  263. int ch;
  264. for (;;) {
  265. ch = udbg_opal_getc_poll();
  266. if (ch == -1) {
  267. /* This shouldn't be needed...but... */
  268. volatile unsigned long delay;
  269. for (delay=0; delay < 2000000; delay++)
  270. ;
  271. } else {
  272. return ch;
  273. }
  274. }
  275. }
  276. static void udbg_init_opal_common(void)
  277. {
  278. udbg_putc = udbg_opal_putc;
  279. udbg_getc = udbg_opal_getc;
  280. udbg_getc_poll = udbg_opal_getc_poll;
  281. tb_ticks_per_usec = 0x200; /* Make udelay not suck */
  282. }
  283. void __init hvc_opal_init_early(void)
  284. {
  285. struct device_node *stdout_node = NULL;
  286. const __be32 *termno;
  287. const char *name = NULL;
  288. const struct hv_ops *ops;
  289. u32 index;
  290. /* find the boot console from /chosen/stdout */
  291. if (of_chosen)
  292. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  293. if (name) {
  294. stdout_node = of_find_node_by_path(name);
  295. if (!stdout_node) {
  296. pr_err("hvc_opal: Failed to locate default console!\n");
  297. return;
  298. }
  299. } else {
  300. struct device_node *opal, *np;
  301. /* Current OPAL takeover doesn't provide the stdout
  302. * path, so we hard wire it
  303. */
  304. opal = of_find_node_by_path("/ibm,opal/consoles");
  305. if (opal)
  306. pr_devel("hvc_opal: Found consoles in new location\n");
  307. if (!opal) {
  308. opal = of_find_node_by_path("/ibm,opal");
  309. if (opal)
  310. pr_devel("hvc_opal: "
  311. "Found consoles in old location\n");
  312. }
  313. if (!opal)
  314. return;
  315. for_each_child_of_node(opal, np) {
  316. if (!strcmp(np->name, "serial")) {
  317. stdout_node = np;
  318. break;
  319. }
  320. }
  321. of_node_put(opal);
  322. }
  323. if (!stdout_node)
  324. return;
  325. termno = of_get_property(stdout_node, "reg", NULL);
  326. index = termno ? be32_to_cpup(termno) : 0;
  327. if (index >= MAX_NR_HVC_CONSOLES)
  328. return;
  329. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  330. /* Check the protocol */
  331. if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
  332. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  333. ops = &hvc_opal_raw_ops;
  334. pr_devel("hvc_opal: Found RAW console\n");
  335. }
  336. else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
  337. hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
  338. ops = &hvc_opal_hvsi_ops;
  339. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
  340. opal_put_chars, index, 1);
  341. /* HVSI, perform the handshake now */
  342. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  343. pr_devel("hvc_opal: Found HVSI console\n");
  344. } else
  345. goto out;
  346. hvc_opal_boot_termno = index;
  347. udbg_init_opal_common();
  348. add_preferred_console("hvc", index, NULL);
  349. hvc_instantiate(index, index, ops);
  350. out:
  351. of_node_put(stdout_node);
  352. }
  353. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
  354. void __init udbg_init_debug_opal_raw(void)
  355. {
  356. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  357. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  358. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  359. hvc_opal_boot_termno = index;
  360. udbg_init_opal_common();
  361. }
  362. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
  363. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
  364. void __init udbg_init_debug_opal_hvsi(void)
  365. {
  366. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  367. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  368. hvc_opal_boot_termno = index;
  369. udbg_init_opal_common();
  370. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars,
  371. index, 1);
  372. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  373. }
  374. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */