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