olpc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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/setup.h>
  21. #include <asm/olpc.h>
  22. #ifdef CONFIG_OPEN_FIRMWARE
  23. #include <asm/ofw.h>
  24. #endif
  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. spin_lock_irqsave(&ec_lock, flags);
  101. /* Clear OBF */
  102. for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
  103. inb(0x68);
  104. if (i == 10) {
  105. printk(KERN_ERR "olpc-ec: timeout while attempting to "
  106. "clear OBF flag!\n");
  107. goto err;
  108. }
  109. if (wait_on_ibf(0x6c, 0)) {
  110. printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
  111. "quiesce!\n");
  112. goto err;
  113. }
  114. restart:
  115. /*
  116. * Note that if we time out during any IBF checks, that's a failure;
  117. * we have to return. There's no way for the kernel to clear that.
  118. *
  119. * If we time out during an OBF check, we can restart the command;
  120. * reissuing it will clear the OBF flag, and we should be alright.
  121. * The OBF flag will sometimes misbehave due to what we believe
  122. * is a hardware quirk..
  123. */
  124. printk(KERN_DEBUG "olpc-ec: running cmd 0x%x\n", cmd);
  125. outb(cmd, 0x6c);
  126. if (wait_on_ibf(0x6c, 0)) {
  127. printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
  128. "command!\n");
  129. goto err;
  130. }
  131. if (inbuf && inlen) {
  132. /* write data to EC */
  133. for (i = 0; i < inlen; i++) {
  134. if (wait_on_ibf(0x6c, 0)) {
  135. printk(KERN_ERR "olpc-ec: timeout waiting for"
  136. " EC accept data!\n");
  137. goto err;
  138. }
  139. printk(KERN_DEBUG "olpc-ec: sending cmd arg 0x%x\n",
  140. 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. goto restart;
  151. }
  152. outbuf[i] = inb(0x68);
  153. printk(KERN_DEBUG "olpc-ec: received 0x%x\n",
  154. outbuf[i]);
  155. }
  156. }
  157. ret = 0;
  158. err:
  159. spin_unlock_irqrestore(&ec_lock, flags);
  160. return ret;
  161. }
  162. EXPORT_SYMBOL_GPL(olpc_ec_cmd);
  163. #ifdef CONFIG_OPEN_FIRMWARE
  164. static void __init platform_detect(void)
  165. {
  166. size_t propsize;
  167. __be32 rev;
  168. if (ofw("getprop", 4, 1, NULL, "board-revision-int", &rev, 4,
  169. &propsize) || propsize != 4) {
  170. printk(KERN_ERR "ofw: getprop call failed!\n");
  171. rev = cpu_to_be32(0);
  172. }
  173. olpc_platform_info.boardrev = be32_to_cpu(rev);
  174. }
  175. #else
  176. static void __init platform_detect(void)
  177. {
  178. /* stopgap until OFW support is added to the kernel */
  179. olpc_platform_info.boardrev = olpc_board(0xc2);
  180. }
  181. #endif
  182. static int __init olpc_init(void)
  183. {
  184. unsigned char *romsig;
  185. /* The ioremap check is dangerous; limit what we run it on */
  186. if (!is_geode() || cs5535_has_vsa2())
  187. return 0;
  188. spin_lock_init(&ec_lock);
  189. romsig = ioremap(0xffffffc0, 16);
  190. if (!romsig)
  191. return 0;
  192. if (strncmp(romsig, "CL1 Q", 7))
  193. goto unmap;
  194. if (strncmp(romsig+6, romsig+13, 3)) {
  195. printk(KERN_INFO "OLPC BIOS signature looks invalid. "
  196. "Assuming not OLPC\n");
  197. goto unmap;
  198. }
  199. printk(KERN_INFO "OLPC board with OpenFirmware %.16s\n", romsig);
  200. olpc_platform_info.flags |= OLPC_F_PRESENT;
  201. /* get the platform revision */
  202. platform_detect();
  203. /* assume B1 and above models always have a DCON */
  204. if (olpc_board_at_least(olpc_board(0xb1)))
  205. olpc_platform_info.flags |= OLPC_F_DCON;
  206. /* get the EC revision */
  207. olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
  208. (unsigned char *) &olpc_platform_info.ecver, 1);
  209. #ifdef CONFIG_PCI_OLPC
  210. /* If the VSA exists let it emulate PCI, if not emulate in kernel */
  211. if (!cs5535_has_vsa2())
  212. x86_init.pci.arch_init = pci_olpc_init;
  213. #endif
  214. printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n",
  215. ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
  216. olpc_platform_info.boardrev >> 4,
  217. olpc_platform_info.ecver);
  218. unmap:
  219. iounmap(romsig);
  220. return 0;
  221. }
  222. postcore_initcall(olpc_init);