hvc_opal.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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[] __devinitdata = {
  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 const struct hv_ops hvc_opal_raw_ops = {
  56. .get_chars = opal_get_chars,
  57. .put_chars = opal_put_chars,
  58. .notifier_add = notifier_add_irq,
  59. .notifier_del = notifier_del_irq,
  60. .notifier_hangup = notifier_hangup_irq,
  61. };
  62. static int hvc_opal_hvsi_get_chars(uint32_t vtermno, char *buf, int count)
  63. {
  64. struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
  65. if (WARN_ON(!pv))
  66. return -ENODEV;
  67. return hvsilib_get_chars(&pv->hvsi, buf, count);
  68. }
  69. static int hvc_opal_hvsi_put_chars(uint32_t vtermno, const char *buf, int count)
  70. {
  71. struct hvc_opal_priv *pv = hvc_opal_privs[vtermno];
  72. if (WARN_ON(!pv))
  73. return -ENODEV;
  74. return hvsilib_put_chars(&pv->hvsi, buf, count);
  75. }
  76. static int hvc_opal_hvsi_open(struct hvc_struct *hp, int data)
  77. {
  78. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  79. int rc;
  80. pr_devel("HVSI@%x: do open !\n", hp->vtermno);
  81. rc = notifier_add_irq(hp, data);
  82. if (rc)
  83. return rc;
  84. return hvsilib_open(&pv->hvsi, hp);
  85. }
  86. static void hvc_opal_hvsi_close(struct hvc_struct *hp, int data)
  87. {
  88. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  89. pr_devel("HVSI@%x: do close !\n", hp->vtermno);
  90. hvsilib_close(&pv->hvsi, hp);
  91. notifier_del_irq(hp, data);
  92. }
  93. void hvc_opal_hvsi_hangup(struct hvc_struct *hp, int data)
  94. {
  95. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  96. pr_devel("HVSI@%x: do hangup !\n", hp->vtermno);
  97. hvsilib_close(&pv->hvsi, hp);
  98. notifier_hangup_irq(hp, data);
  99. }
  100. static int hvc_opal_hvsi_tiocmget(struct hvc_struct *hp)
  101. {
  102. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  103. if (!pv)
  104. return -EINVAL;
  105. return pv->hvsi.mctrl;
  106. }
  107. static int hvc_opal_hvsi_tiocmset(struct hvc_struct *hp, unsigned int set,
  108. unsigned int clear)
  109. {
  110. struct hvc_opal_priv *pv = hvc_opal_privs[hp->vtermno];
  111. pr_devel("HVSI@%x: Set modem control, set=%x,clr=%x\n",
  112. hp->vtermno, set, clear);
  113. if (set & TIOCM_DTR)
  114. hvsilib_write_mctrl(&pv->hvsi, 1);
  115. else if (clear & TIOCM_DTR)
  116. hvsilib_write_mctrl(&pv->hvsi, 0);
  117. return 0;
  118. }
  119. static const struct hv_ops hvc_opal_hvsi_ops = {
  120. .get_chars = hvc_opal_hvsi_get_chars,
  121. .put_chars = hvc_opal_hvsi_put_chars,
  122. .notifier_add = hvc_opal_hvsi_open,
  123. .notifier_del = hvc_opal_hvsi_close,
  124. .notifier_hangup = hvc_opal_hvsi_hangup,
  125. .tiocmget = hvc_opal_hvsi_tiocmget,
  126. .tiocmset = hvc_opal_hvsi_tiocmset,
  127. };
  128. static int __devinit hvc_opal_probe(struct platform_device *dev)
  129. {
  130. const struct hv_ops *ops;
  131. struct hvc_struct *hp;
  132. struct hvc_opal_priv *pv;
  133. hv_protocol_t proto;
  134. unsigned int termno, boot = 0;
  135. const __be32 *reg;
  136. if (of_device_is_compatible(dev->dev.of_node, "ibm,opal-console-raw")) {
  137. proto = HV_PROTOCOL_RAW;
  138. ops = &hvc_opal_raw_ops;
  139. } else if (of_device_is_compatible(dev->dev.of_node,
  140. "ibm,opal-console-hvsi")) {
  141. proto = HV_PROTOCOL_HVSI;
  142. ops = &hvc_opal_hvsi_ops;
  143. } else {
  144. pr_err("hvc_opal: Unkown protocol for %s\n",
  145. dev->dev.of_node->full_name);
  146. return -ENXIO;
  147. }
  148. reg = of_get_property(dev->dev.of_node, "reg", NULL);
  149. termno = reg ? be32_to_cpup(reg) : 0;
  150. /* Is it our boot one ? */
  151. if (hvc_opal_privs[termno] == &hvc_opal_boot_priv) {
  152. pv = hvc_opal_privs[termno];
  153. boot = 1;
  154. } else if (hvc_opal_privs[termno] == NULL) {
  155. pv = kzalloc(sizeof(struct hvc_opal_priv), GFP_KERNEL);
  156. if (!pv)
  157. return -ENOMEM;
  158. pv->proto = proto;
  159. hvc_opal_privs[termno] = pv;
  160. if (proto == HV_PROTOCOL_HVSI)
  161. hvsilib_init(&pv->hvsi, opal_get_chars, opal_put_chars,
  162. termno, 0);
  163. /* Instanciate now to establish a mapping index==vtermno */
  164. hvc_instantiate(termno, termno, ops);
  165. } else {
  166. pr_err("hvc_opal: Device %s has duplicate terminal number #%d\n",
  167. dev->dev.of_node->full_name, termno);
  168. return -ENXIO;
  169. }
  170. pr_info("hvc%d: %s protocol on %s%s\n", termno,
  171. proto == HV_PROTOCOL_RAW ? "raw" : "hvsi",
  172. dev->dev.of_node->full_name,
  173. boot ? " (boot console)" : "");
  174. /* We don't do IRQ yet */
  175. hp = hvc_alloc(termno, 0, ops, MAX_VIO_PUT_CHARS);
  176. if (IS_ERR(hp))
  177. return PTR_ERR(hp);
  178. dev_set_drvdata(&dev->dev, hp);
  179. return 0;
  180. }
  181. static int __devexit hvc_opal_remove(struct platform_device *dev)
  182. {
  183. struct hvc_struct *hp = dev_get_drvdata(&dev->dev);
  184. int rc, termno;
  185. termno = hp->vtermno;
  186. rc = hvc_remove(hp);
  187. if (rc == 0) {
  188. if (hvc_opal_privs[termno] != &hvc_opal_boot_priv)
  189. kfree(hvc_opal_privs[termno]);
  190. hvc_opal_privs[termno] = NULL;
  191. }
  192. return rc;
  193. }
  194. static struct platform_driver hvc_opal_driver = {
  195. .probe = hvc_opal_probe,
  196. .remove = __devexit_p(hvc_opal_remove),
  197. .driver = {
  198. .name = hvc_opal_name,
  199. .owner = THIS_MODULE,
  200. .of_match_table = hvc_opal_match,
  201. }
  202. };
  203. static int __init hvc_opal_init(void)
  204. {
  205. if (!firmware_has_feature(FW_FEATURE_OPAL))
  206. return -ENODEV;
  207. /* Register as a vio device to receive callbacks */
  208. return platform_driver_register(&hvc_opal_driver);
  209. }
  210. module_init(hvc_opal_init);
  211. static void __exit hvc_opal_exit(void)
  212. {
  213. platform_driver_unregister(&hvc_opal_driver);
  214. }
  215. module_exit(hvc_opal_exit);
  216. static void udbg_opal_putc(char c)
  217. {
  218. unsigned int termno = hvc_opal_boot_termno;
  219. int count = -1;
  220. if (c == '\n')
  221. udbg_opal_putc('\r');
  222. do {
  223. switch(hvc_opal_boot_priv.proto) {
  224. case HV_PROTOCOL_RAW:
  225. count = opal_put_chars(termno, &c, 1);
  226. break;
  227. case HV_PROTOCOL_HVSI:
  228. count = hvc_opal_hvsi_put_chars(termno, &c, 1);
  229. break;
  230. }
  231. } while(count == 0 || count == -EAGAIN);
  232. }
  233. static int udbg_opal_getc_poll(void)
  234. {
  235. unsigned int termno = hvc_opal_boot_termno;
  236. int rc = 0;
  237. char c;
  238. switch(hvc_opal_boot_priv.proto) {
  239. case HV_PROTOCOL_RAW:
  240. rc = opal_get_chars(termno, &c, 1);
  241. break;
  242. case HV_PROTOCOL_HVSI:
  243. rc = hvc_opal_hvsi_get_chars(termno, &c, 1);
  244. break;
  245. }
  246. if (!rc)
  247. return -1;
  248. return c;
  249. }
  250. static int udbg_opal_getc(void)
  251. {
  252. int ch;
  253. for (;;) {
  254. ch = udbg_opal_getc_poll();
  255. if (ch == -1) {
  256. /* This shouldn't be needed...but... */
  257. volatile unsigned long delay;
  258. for (delay=0; delay < 2000000; delay++)
  259. ;
  260. } else {
  261. return ch;
  262. }
  263. }
  264. }
  265. static void udbg_init_opal_common(void)
  266. {
  267. udbg_putc = udbg_opal_putc;
  268. udbg_getc = udbg_opal_getc;
  269. udbg_getc_poll = udbg_opal_getc_poll;
  270. tb_ticks_per_usec = 0x200; /* Make udelay not suck */
  271. }
  272. void __init hvc_opal_init_early(void)
  273. {
  274. struct device_node *stdout_node = NULL;
  275. const u32 *termno;
  276. const char *name = NULL;
  277. const struct hv_ops *ops;
  278. u32 index;
  279. /* find the boot console from /chosen/stdout */
  280. if (of_chosen)
  281. name = of_get_property(of_chosen, "linux,stdout-path", NULL);
  282. if (name) {
  283. stdout_node = of_find_node_by_path(name);
  284. if (!stdout_node) {
  285. pr_err("hvc_opal: Failed to locate default console!\n");
  286. return;
  287. }
  288. } else {
  289. struct device_node *opal, *np;
  290. /* Current OPAL takeover doesn't provide the stdout
  291. * path, so we hard wire it
  292. */
  293. opal = of_find_node_by_path("/ibm,opal/consoles");
  294. if (opal)
  295. pr_devel("hvc_opal: Found consoles in new location\n");
  296. if (!opal) {
  297. opal = of_find_node_by_path("/ibm,opal");
  298. if (opal)
  299. pr_devel("hvc_opal: "
  300. "Found consoles in old location\n");
  301. }
  302. if (!opal)
  303. return;
  304. for_each_child_of_node(opal, np) {
  305. if (!strcmp(np->name, "serial")) {
  306. stdout_node = np;
  307. break;
  308. }
  309. }
  310. of_node_put(opal);
  311. }
  312. if (!stdout_node)
  313. return;
  314. termno = of_get_property(stdout_node, "reg", NULL);
  315. index = termno ? *termno : 0;
  316. if (index >= MAX_NR_HVC_CONSOLES)
  317. return;
  318. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  319. /* Check the protocol */
  320. if (of_device_is_compatible(stdout_node, "ibm,opal-console-raw")) {
  321. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  322. ops = &hvc_opal_raw_ops;
  323. pr_devel("hvc_opal: Found RAW console\n");
  324. }
  325. else if (of_device_is_compatible(stdout_node,"ibm,opal-console-hvsi")) {
  326. hvc_opal_boot_priv.proto = HV_PROTOCOL_HVSI;
  327. ops = &hvc_opal_hvsi_ops;
  328. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars,
  329. opal_put_chars, index, 1);
  330. /* HVSI, perform the handshake now */
  331. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  332. pr_devel("hvc_opal: Found HVSI console\n");
  333. } else
  334. goto out;
  335. hvc_opal_boot_termno = index;
  336. udbg_init_opal_common();
  337. add_preferred_console("hvc", index, NULL);
  338. hvc_instantiate(index, index, ops);
  339. out:
  340. of_node_put(stdout_node);
  341. }
  342. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_RAW
  343. void __init udbg_init_debug_opal(void)
  344. {
  345. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  346. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  347. hvc_opal_boot_priv.proto = HV_PROTOCOL_RAW;
  348. hvc_opal_boot_termno = index;
  349. udbg_init_opal_common();
  350. }
  351. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_RAW */
  352. #ifdef CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI
  353. void __init udbg_init_debug_opal_hvsi(void)
  354. {
  355. u32 index = CONFIG_PPC_EARLY_DEBUG_OPAL_VTERMNO;
  356. hvc_opal_privs[index] = &hvc_opal_boot_priv;
  357. hvc_opal_boot_termno = index;
  358. udbg_init_opal_common();
  359. hvsilib_init(&hvc_opal_boot_priv.hvsi, opal_get_chars, opal_put_chars,
  360. index, 1);
  361. hvsilib_establish(&hvc_opal_boot_priv.hvsi);
  362. }
  363. #endif /* CONFIG_PPC_EARLY_DEBUG_OPAL_HVSI */