olpc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <asm/geode.h>
  20. #include <asm/olpc.h>
  21. #ifdef CONFIG_OPEN_FIRMWARE
  22. #include <asm/ofw.h>
  23. #endif
  24. struct olpc_platform_t olpc_platform_info;
  25. EXPORT_SYMBOL_GPL(olpc_platform_info);
  26. static DEFINE_SPINLOCK(ec_lock);
  27. /* what the timeout *should* be (in ms) */
  28. #define EC_BASE_TIMEOUT 20
  29. /* the timeout that bugs in the EC might force us to actually use */
  30. static int ec_timeout = EC_BASE_TIMEOUT;
  31. static int __init olpc_ec_timeout_set(char *str)
  32. {
  33. if (get_option(&str, &ec_timeout) != 1) {
  34. ec_timeout = EC_BASE_TIMEOUT;
  35. printk(KERN_ERR "olpc-ec: invalid argument to "
  36. "'olpc_ec_timeout=', ignoring!\n");
  37. }
  38. printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
  39. ec_timeout);
  40. return 1;
  41. }
  42. __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
  43. /*
  44. * These {i,o}bf_status functions return whether the buffers are full or not.
  45. */
  46. static inline unsigned int ibf_status(unsigned int port)
  47. {
  48. return !!(inb(port) & 0x02);
  49. }
  50. static inline unsigned int obf_status(unsigned int port)
  51. {
  52. return inb(port) & 0x01;
  53. }
  54. #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
  55. static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
  56. {
  57. unsigned int timeo;
  58. int state = ibf_status(port);
  59. for (timeo = ec_timeout; state != desired && timeo; timeo--) {
  60. mdelay(1);
  61. state = ibf_status(port);
  62. }
  63. if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
  64. timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
  65. printk(KERN_WARNING "olpc-ec: %d: waited %u ms for IBF!\n",
  66. line, ec_timeout - timeo);
  67. }
  68. return !(state == desired);
  69. }
  70. #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
  71. static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
  72. {
  73. unsigned int timeo;
  74. int state = obf_status(port);
  75. for (timeo = ec_timeout; state != desired && timeo; timeo--) {
  76. mdelay(1);
  77. state = obf_status(port);
  78. }
  79. if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
  80. timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
  81. printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n",
  82. line, ec_timeout - timeo);
  83. }
  84. return !(state == desired);
  85. }
  86. /*
  87. * This allows the kernel to run Embedded Controller commands. The EC is
  88. * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
  89. * available EC commands are here:
  90. * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
  91. * OpenFirmware's source is available, the EC's is not.
  92. */
  93. int olpc_ec_cmd(unsigned char cmd, unsigned char *inbuf, size_t inlen,
  94. unsigned char *outbuf, size_t outlen)
  95. {
  96. unsigned long flags;
  97. int ret = -EIO;
  98. int i;
  99. spin_lock_irqsave(&ec_lock, flags);
  100. /* Clear OBF */
  101. for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
  102. inb(0x68);
  103. if (i == 10) {
  104. printk(KERN_ERR "olpc-ec: timeout while attempting to "
  105. "clear OBF flag!\n");
  106. goto err;
  107. }
  108. if (wait_on_ibf(0x6c, 0)) {
  109. printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
  110. "quiesce!\n");
  111. goto err;
  112. }
  113. restart:
  114. /*
  115. * Note that if we time out during any IBF checks, that's a failure;
  116. * we have to return. There's no way for the kernel to clear that.
  117. *
  118. * If we time out during an OBF check, we can restart the command;
  119. * reissuing it will clear the OBF flag, and we should be alright.
  120. * The OBF flag will sometimes misbehave due to what we believe
  121. * is a hardware quirk..
  122. */
  123. printk(KERN_DEBUG "olpc-ec: running cmd 0x%x\n", cmd);
  124. outb(cmd, 0x6c);
  125. if (wait_on_ibf(0x6c, 0)) {
  126. printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
  127. "command!\n");
  128. goto err;
  129. }
  130. if (inbuf && inlen) {
  131. /* write data to EC */
  132. for (i = 0; i < inlen; i++) {
  133. if (wait_on_ibf(0x6c, 0)) {
  134. printk(KERN_ERR "olpc-ec: timeout waiting for"
  135. " EC accept data!\n");
  136. goto err;
  137. }
  138. printk(KERN_DEBUG "olpc-ec: sending cmd arg 0x%x\n",
  139. inbuf[i]);
  140. outb(inbuf[i], 0x68);
  141. }
  142. }
  143. if (outbuf && outlen) {
  144. /* read data from EC */
  145. for (i = 0; i < outlen; i++) {
  146. if (wait_on_obf(0x6c, 1)) {
  147. printk(KERN_ERR "olpc-ec: timeout waiting for"
  148. " EC to provide data!\n");
  149. goto restart;
  150. }
  151. outbuf[i] = inb(0x68);
  152. printk(KERN_DEBUG "olpc-ec: received 0x%x\n",
  153. outbuf[i]);
  154. }
  155. }
  156. ret = 0;
  157. err:
  158. spin_unlock_irqrestore(&ec_lock, flags);
  159. return ret;
  160. }
  161. EXPORT_SYMBOL_GPL(olpc_ec_cmd);
  162. #ifdef CONFIG_OPEN_FIRMWARE
  163. static void __init platform_detect(void)
  164. {
  165. size_t propsize;
  166. __be32 rev;
  167. if (ofw("getprop", 4, 1, NULL, "board-revision-int", &rev, 4,
  168. &propsize) || propsize != 4) {
  169. printk(KERN_ERR "ofw: getprop call failed!\n");
  170. rev = cpu_to_be32(0);
  171. }
  172. olpc_platform_info.boardrev = be32_to_cpu(rev);
  173. }
  174. #else
  175. static void __init platform_detect(void)
  176. {
  177. /* stopgap until OFW support is added to the kernel */
  178. olpc_platform_info.boardrev = olpc_board(0xc2);
  179. }
  180. #endif
  181. static int __init olpc_init(void)
  182. {
  183. unsigned char *romsig;
  184. /* The ioremap check is dangerous; limit what we run it on */
  185. if (!is_geode() || geode_has_vsa2())
  186. return 0;
  187. spin_lock_init(&ec_lock);
  188. romsig = ioremap(0xffffffc0, 16);
  189. if (!romsig)
  190. return 0;
  191. if (strncmp(romsig, "CL1 Q", 7))
  192. goto unmap;
  193. if (strncmp(romsig+6, romsig+13, 3)) {
  194. printk(KERN_INFO "OLPC BIOS signature looks invalid. "
  195. "Assuming not OLPC\n");
  196. goto unmap;
  197. }
  198. printk(KERN_INFO "OLPC board with OpenFirmware %.16s\n", romsig);
  199. olpc_platform_info.flags |= OLPC_F_PRESENT;
  200. /* get the platform revision */
  201. platform_detect();
  202. /* assume B1 and above models always have a DCON */
  203. if (olpc_board_at_least(olpc_board(0xb1)))
  204. olpc_platform_info.flags |= OLPC_F_DCON;
  205. /* get the EC revision */
  206. olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
  207. (unsigned char *) &olpc_platform_info.ecver, 1);
  208. /* check to see if the VSA exists */
  209. if (geode_has_vsa2())
  210. olpc_platform_info.flags |= OLPC_F_VSA;
  211. printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n",
  212. ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
  213. olpc_platform_info.boardrev >> 4,
  214. olpc_platform_info.ecver);
  215. unmap:
  216. iounmap(romsig);
  217. return 0;
  218. }
  219. postcore_initcall(olpc_init);