olpc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Support for the OLPC DCON and OLPC EC access
  4. *
  5. * Copyright © 2006 Advanced Micro Devices, Inc.
  6. * Copyright © 2007-2008 Andres Salomon <[email protected]>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/export.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/string.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/syscore_ops.h>
  17. #include <linux/mutex.h>
  18. #include <linux/olpc-ec.h>
  19. #include <asm/geode.h>
  20. #include <asm/setup.h>
  21. #include <asm/olpc.h>
  22. #include <asm/olpc_ofw.h>
  23. struct olpc_platform_t olpc_platform_info;
  24. EXPORT_SYMBOL_GPL(olpc_platform_info);
  25. /* what the timeout *should* be (in ms) */
  26. #define EC_BASE_TIMEOUT 20
  27. /* the timeout that bugs in the EC might force us to actually use */
  28. static int ec_timeout = EC_BASE_TIMEOUT;
  29. static int __init olpc_ec_timeout_set(char *str)
  30. {
  31. if (get_option(&str, &ec_timeout) != 1) {
  32. ec_timeout = EC_BASE_TIMEOUT;
  33. printk(KERN_ERR "olpc-ec: invalid argument to "
  34. "'olpc_ec_timeout=', ignoring!\n");
  35. }
  36. printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
  37. ec_timeout);
  38. return 1;
  39. }
  40. __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
  41. /*
  42. * These {i,o}bf_status functions return whether the buffers are full or not.
  43. */
  44. static inline unsigned int ibf_status(unsigned int port)
  45. {
  46. return !!(inb(port) & 0x02);
  47. }
  48. static inline unsigned int obf_status(unsigned int port)
  49. {
  50. return inb(port) & 0x01;
  51. }
  52. #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
  53. static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
  54. {
  55. unsigned int timeo;
  56. int state = ibf_status(port);
  57. for (timeo = ec_timeout; state != desired && timeo; timeo--) {
  58. mdelay(1);
  59. state = ibf_status(port);
  60. }
  61. if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
  62. timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
  63. printk(KERN_WARNING "olpc-ec: %d: waited %u ms for IBF!\n",
  64. line, ec_timeout - timeo);
  65. }
  66. return !(state == desired);
  67. }
  68. #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
  69. static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
  70. {
  71. unsigned int timeo;
  72. int state = obf_status(port);
  73. for (timeo = ec_timeout; state != desired && timeo; timeo--) {
  74. mdelay(1);
  75. state = obf_status(port);
  76. }
  77. if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
  78. timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
  79. printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n",
  80. line, ec_timeout - timeo);
  81. }
  82. return !(state == desired);
  83. }
  84. /*
  85. * This allows the kernel to run Embedded Controller commands. The EC is
  86. * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
  87. * available EC commands are here:
  88. * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
  89. * OpenFirmware's source is available, the EC's is not.
  90. */
  91. static int olpc_xo1_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf,
  92. size_t outlen, void *arg)
  93. {
  94. int ret = -EIO;
  95. int i;
  96. int restarts = 0;
  97. /* Clear OBF */
  98. for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
  99. inb(0x68);
  100. if (i == 10) {
  101. printk(KERN_ERR "olpc-ec: timeout while attempting to "
  102. "clear OBF flag!\n");
  103. goto err;
  104. }
  105. if (wait_on_ibf(0x6c, 0)) {
  106. printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
  107. "quiesce!\n");
  108. goto err;
  109. }
  110. restart:
  111. /*
  112. * Note that if we time out during any IBF checks, that's a failure;
  113. * we have to return. There's no way for the kernel to clear that.
  114. *
  115. * If we time out during an OBF check, we can restart the command;
  116. * reissuing it will clear the OBF flag, and we should be alright.
  117. * The OBF flag will sometimes misbehave due to what we believe
  118. * is a hardware quirk..
  119. */
  120. pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
  121. outb(cmd, 0x6c);
  122. if (wait_on_ibf(0x6c, 0)) {
  123. printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
  124. "command!\n");
  125. goto err;
  126. }
  127. if (inbuf && inlen) {
  128. /* write data to EC */
  129. for (i = 0; i < inlen; i++) {
  130. pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
  131. outb(inbuf[i], 0x68);
  132. if (wait_on_ibf(0x6c, 0)) {
  133. printk(KERN_ERR "olpc-ec: timeout waiting for"
  134. " EC accept data!\n");
  135. goto err;
  136. }
  137. }
  138. }
  139. if (outbuf && outlen) {
  140. /* read data from EC */
  141. for (i = 0; i < outlen; i++) {
  142. if (wait_on_obf(0x6c, 1)) {
  143. printk(KERN_ERR "olpc-ec: timeout waiting for"
  144. " EC to provide data!\n");
  145. if (restarts++ < 10)
  146. goto restart;
  147. goto err;
  148. }
  149. outbuf[i] = inb(0x68);
  150. pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
  151. }
  152. }
  153. ret = 0;
  154. err:
  155. return ret;
  156. }
  157. static bool __init check_ofw_architecture(struct device_node *root)
  158. {
  159. const char *olpc_arch;
  160. int propsize;
  161. olpc_arch = of_get_property(root, "architecture", &propsize);
  162. return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
  163. }
  164. static u32 __init get_board_revision(struct device_node *root)
  165. {
  166. int propsize;
  167. const __be32 *rev;
  168. rev = of_get_property(root, "board-revision-int", &propsize);
  169. if (propsize != 4)
  170. return 0;
  171. return be32_to_cpu(*rev);
  172. }
  173. static bool __init platform_detect(void)
  174. {
  175. struct device_node *root = of_find_node_by_path("/");
  176. bool success;
  177. if (!root)
  178. return false;
  179. success = check_ofw_architecture(root);
  180. if (success) {
  181. olpc_platform_info.boardrev = get_board_revision(root);
  182. olpc_platform_info.flags |= OLPC_F_PRESENT;
  183. pr_info("OLPC board revision %s%X\n",
  184. ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
  185. olpc_platform_info.boardrev >> 4);
  186. }
  187. of_node_put(root);
  188. return success;
  189. }
  190. static int __init add_xo1_platform_devices(void)
  191. {
  192. struct platform_device *pdev;
  193. pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
  194. if (IS_ERR(pdev))
  195. return PTR_ERR(pdev);
  196. pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
  197. return PTR_ERR_OR_ZERO(pdev);
  198. }
  199. static int olpc_xo1_ec_suspend(struct platform_device *pdev)
  200. {
  201. /*
  202. * Squelch SCIs while suspended. This is a fix for
  203. * <http://dev.laptop.org/ticket/1835>.
  204. */
  205. return olpc_ec_cmd(EC_SET_SCI_INHIBIT, NULL, 0, NULL, 0);
  206. }
  207. static int olpc_xo1_ec_resume(struct platform_device *pdev)
  208. {
  209. /* Tell the EC to stop inhibiting SCIs */
  210. olpc_ec_cmd(EC_SET_SCI_INHIBIT_RELEASE, NULL, 0, NULL, 0);
  211. /*
  212. * Tell the wireless module to restart USB communication.
  213. * Must be done twice.
  214. */
  215. olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
  216. olpc_ec_cmd(EC_WAKE_UP_WLAN, NULL, 0, NULL, 0);
  217. return 0;
  218. }
  219. static struct olpc_ec_driver ec_xo1_driver = {
  220. .suspend = olpc_xo1_ec_suspend,
  221. .resume = olpc_xo1_ec_resume,
  222. .ec_cmd = olpc_xo1_ec_cmd,
  223. #ifdef CONFIG_OLPC_XO1_SCI
  224. /*
  225. * XO-1 EC wakeups are available when olpc-xo1-sci driver is
  226. * compiled in
  227. */
  228. .wakeup_available = true,
  229. #endif
  230. };
  231. static struct olpc_ec_driver ec_xo1_5_driver = {
  232. .ec_cmd = olpc_xo1_ec_cmd,
  233. #ifdef CONFIG_OLPC_XO15_SCI
  234. /*
  235. * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
  236. * compiled in
  237. */
  238. .wakeup_available = true,
  239. #endif
  240. };
  241. static int __init olpc_init(void)
  242. {
  243. int r = 0;
  244. if (!olpc_ofw_present() || !platform_detect())
  245. return 0;
  246. /* register the XO-1 and 1.5-specific EC handler */
  247. if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
  248. olpc_ec_driver_register(&ec_xo1_driver, NULL);
  249. else
  250. olpc_ec_driver_register(&ec_xo1_5_driver, NULL);
  251. platform_device_register_simple("olpc-ec", -1, NULL, 0);
  252. /* assume B1 and above models always have a DCON */
  253. if (olpc_board_at_least(olpc_board(0xb1)))
  254. olpc_platform_info.flags |= OLPC_F_DCON;
  255. #ifdef CONFIG_PCI_OLPC
  256. /* If the VSA exists let it emulate PCI, if not emulate in kernel.
  257. * XO-1 only. */
  258. if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
  259. !cs5535_has_vsa2())
  260. x86_init.pci.arch_init = pci_olpc_init;
  261. #endif
  262. if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
  263. r = add_xo1_platform_devices();
  264. if (r)
  265. return r;
  266. }
  267. return 0;
  268. }
  269. postcore_initcall(olpc_init);