olpc.c 6.9 KB

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