escc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2012 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/serial.h>
  19. #include <grub/types.h>
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/time.h>
  24. #include <grub/i18n.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. struct grub_escc_descriptor
  27. {
  28. volatile grub_uint8_t *escc_ctrl;
  29. volatile grub_uint8_t *escc_data;
  30. };
  31. static void
  32. do_real_config (struct grub_serial_port *port)
  33. {
  34. grub_uint8_t bitsspec;
  35. grub_uint8_t parity_stop_spec;
  36. if (port->configured)
  37. return;
  38. /* Make sure the port is waiting for address now. */
  39. (void) *port->escc_desc->escc_ctrl;
  40. switch (port->config.speed)
  41. {
  42. case 57600:
  43. *port->escc_desc->escc_ctrl = 13;
  44. *port->escc_desc->escc_ctrl = 0;
  45. *port->escc_desc->escc_ctrl = 12;
  46. *port->escc_desc->escc_ctrl = 0;
  47. *port->escc_desc->escc_ctrl = 14;
  48. *port->escc_desc->escc_ctrl = 1;
  49. *port->escc_desc->escc_ctrl = 11;
  50. *port->escc_desc->escc_ctrl = 0x50;
  51. break;
  52. case 38400:
  53. *port->escc_desc->escc_ctrl = 13;
  54. *port->escc_desc->escc_ctrl = 0;
  55. *port->escc_desc->escc_ctrl = 12;
  56. *port->escc_desc->escc_ctrl = 1;
  57. *port->escc_desc->escc_ctrl = 14;
  58. *port->escc_desc->escc_ctrl = 1;
  59. *port->escc_desc->escc_ctrl = 11;
  60. *port->escc_desc->escc_ctrl = 0x50;
  61. break;
  62. }
  63. parity_stop_spec = 0;
  64. switch (port->config.parity)
  65. {
  66. case GRUB_SERIAL_PARITY_NONE:
  67. parity_stop_spec |= 0;
  68. break;
  69. case GRUB_SERIAL_PARITY_ODD:
  70. parity_stop_spec |= 1;
  71. break;
  72. case GRUB_SERIAL_PARITY_EVEN:
  73. parity_stop_spec |= 3;
  74. break;
  75. }
  76. switch (port->config.stop_bits)
  77. {
  78. case GRUB_SERIAL_STOP_BITS_1:
  79. parity_stop_spec |= 0x4;
  80. break;
  81. case GRUB_SERIAL_STOP_BITS_1_5:
  82. parity_stop_spec |= 0x8;
  83. break;
  84. case GRUB_SERIAL_STOP_BITS_2:
  85. parity_stop_spec |= 0xc;
  86. break;
  87. }
  88. *port->escc_desc->escc_ctrl = 4;
  89. *port->escc_desc->escc_ctrl = 0x40 | parity_stop_spec;
  90. bitsspec = port->config.word_len - 5;
  91. bitsspec = ((bitsspec >> 1) | (bitsspec << 1)) & 3;
  92. *port->escc_desc->escc_ctrl = 3;
  93. *port->escc_desc->escc_ctrl = (bitsspec << 6) | 0x1;
  94. port->configured = 1;
  95. return;
  96. }
  97. /* Fetch a key. */
  98. static int
  99. serial_hw_fetch (struct grub_serial_port *port)
  100. {
  101. do_real_config (port);
  102. *port->escc_desc->escc_ctrl = 0;
  103. if (*port->escc_desc->escc_ctrl & 1)
  104. return *port->escc_desc->escc_data;
  105. return -1;
  106. }
  107. /* Put a character. */
  108. static void
  109. serial_hw_put (struct grub_serial_port *port, const int c)
  110. {
  111. grub_uint64_t endtime;
  112. do_real_config (port);
  113. if (port->broken > 5)
  114. endtime = grub_get_time_ms ();
  115. else if (port->broken > 1)
  116. endtime = grub_get_time_ms () + 50;
  117. else
  118. endtime = grub_get_time_ms () + 200;
  119. /* Wait until the transmitter holding register is empty. */
  120. while (1)
  121. {
  122. *port->escc_desc->escc_ctrl = 0;
  123. if (*port->escc_desc->escc_ctrl & 4)
  124. break;
  125. if (grub_get_time_ms () > endtime)
  126. {
  127. port->broken++;
  128. /* There is something wrong. But what can I do? */
  129. return;
  130. }
  131. }
  132. if (port->broken)
  133. port->broken--;
  134. *port->escc_desc->escc_data = c;
  135. }
  136. /* Initialize a serial device. PORT is the port number for a serial device.
  137. SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
  138. 19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
  139. for the device. Likewise, PARITY is the type of the parity and
  140. STOP_BIT_LEN is the length of the stop bit. The possible values for
  141. WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
  142. macros. */
  143. static grub_err_t
  144. serial_hw_configure (struct grub_serial_port *port __attribute__ ((unused)),
  145. struct grub_serial_config *config __attribute__ ((unused)))
  146. {
  147. if (config->speed != 38400 && config->speed != 57600)
  148. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  149. N_("unsupported serial port speed"));
  150. if (config->parity != GRUB_SERIAL_PARITY_NONE
  151. && config->parity != GRUB_SERIAL_PARITY_ODD
  152. && config->parity != GRUB_SERIAL_PARITY_EVEN)
  153. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  154. N_("unsupported serial port parity"));
  155. if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
  156. && config->stop_bits != GRUB_SERIAL_STOP_BITS_1_5
  157. && config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
  158. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  159. N_("unsupported serial port stop bits number"));
  160. if (config->word_len < 5 || config->word_len > 8)
  161. return grub_error (GRUB_ERR_BAD_ARGUMENT,
  162. N_("unsupported serial port word length"));
  163. port->config = *config;
  164. port->configured = 0;
  165. /* FIXME: should check if the serial terminal was found. */
  166. return GRUB_ERR_NONE;
  167. }
  168. struct grub_serial_driver grub_escc_driver =
  169. {
  170. .configure = serial_hw_configure,
  171. .fetch = serial_hw_fetch,
  172. .put = serial_hw_put
  173. };
  174. static struct grub_escc_descriptor escc_descs[2];
  175. static char *macio = 0;
  176. static void
  177. add_device (grub_addr_t addr, int channel)
  178. {
  179. struct grub_serial_port *port;
  180. grub_err_t err;
  181. struct grub_serial_config config =
  182. {
  183. .speed = 38400,
  184. .word_len = 8,
  185. .parity = GRUB_SERIAL_PARITY_NONE,
  186. .stop_bits = GRUB_SERIAL_STOP_BITS_1
  187. };
  188. escc_descs[channel].escc_ctrl
  189. = (volatile grub_uint8_t *) (grub_addr_t) addr;
  190. escc_descs[channel].escc_data = escc_descs[channel].escc_ctrl + 16;
  191. port = grub_zalloc (sizeof (*port));
  192. if (!port)
  193. {
  194. grub_errno = 0;
  195. return;
  196. }
  197. port->name = grub_xasprintf ("escc-ch-%c", channel + 'a');
  198. if (!port->name)
  199. {
  200. grub_errno = 0;
  201. return;
  202. }
  203. port->escc_desc = &escc_descs[channel];
  204. port->driver = &grub_escc_driver;
  205. err = port->driver->configure (port, &config);
  206. if (err)
  207. grub_print_error ();
  208. grub_serial_register (port);
  209. }
  210. static int
  211. find_macio (struct grub_ieee1275_devalias *alias)
  212. {
  213. if (grub_strcmp (alias->type, "mac-io") != 0)
  214. return 0;
  215. macio = grub_strdup (alias->path);
  216. return 1;
  217. }
  218. GRUB_MOD_INIT (escc)
  219. {
  220. grub_uint32_t macio_addr[4];
  221. grub_uint32_t escc_addr[2];
  222. grub_ieee1275_phandle_t dev;
  223. struct grub_ieee1275_devalias alias;
  224. char *escc = 0;
  225. grub_ieee1275_devices_iterate (find_macio);
  226. if (!macio)
  227. return;
  228. FOR_IEEE1275_DEVCHILDREN(macio, alias)
  229. if (grub_strcmp (alias.type, "escc") == 0)
  230. {
  231. escc = grub_strdup (alias.path);
  232. break;
  233. }
  234. grub_ieee1275_devalias_free (&alias);
  235. if (!escc)
  236. {
  237. grub_free (macio);
  238. return;
  239. }
  240. if (grub_ieee1275_finddevice (macio, &dev))
  241. {
  242. grub_free (macio);
  243. grub_free (escc);
  244. return;
  245. }
  246. if (grub_ieee1275_get_integer_property (dev, "assigned-addresses",
  247. macio_addr, sizeof (macio_addr), 0))
  248. {
  249. grub_free (macio);
  250. grub_free (escc);
  251. return;
  252. }
  253. if (grub_ieee1275_finddevice (escc, &dev))
  254. {
  255. grub_free (macio);
  256. grub_free (escc);
  257. return;
  258. }
  259. if (grub_ieee1275_get_integer_property (dev, "reg",
  260. escc_addr, sizeof (escc_addr), 0))
  261. {
  262. grub_free (macio);
  263. grub_free (escc);
  264. return;
  265. }
  266. add_device (macio_addr[2] + escc_addr[0] + 32, 0);
  267. add_device (macio_addr[2] + escc_addr[0], 1);
  268. grub_free (macio);
  269. grub_free (escc);
  270. }
  271. GRUB_MOD_FINI (escc)
  272. {
  273. }