olpc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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 <linux/syscore_ops.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/mutex.h>
  24. #include <linux/olpc-ec.h>
  25. #include <asm/geode.h>
  26. #include <asm/setup.h>
  27. #include <asm/olpc.h>
  28. #include <asm/olpc_ofw.h>
  29. struct olpc_platform_t olpc_platform_info;
  30. EXPORT_SYMBOL_GPL(olpc_platform_info);
  31. static DEFINE_SPINLOCK(ec_lock);
  32. /* debugfs interface to EC commands */
  33. #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
  34. #define EC_MAX_CMD_REPLY (8)
  35. static struct dentry *ec_debugfs_dir;
  36. static DEFINE_MUTEX(ec_debugfs_cmd_lock);
  37. static unsigned char ec_debugfs_resp[EC_MAX_CMD_REPLY];
  38. static unsigned int ec_debugfs_resp_bytes;
  39. /* EC event mask to be applied during suspend (defining wakeup sources). */
  40. static u16 ec_wakeup_mask;
  41. /* what the timeout *should* be (in ms) */
  42. #define EC_BASE_TIMEOUT 20
  43. /* the timeout that bugs in the EC might force us to actually use */
  44. static int ec_timeout = EC_BASE_TIMEOUT;
  45. static int __init olpc_ec_timeout_set(char *str)
  46. {
  47. if (get_option(&str, &ec_timeout) != 1) {
  48. ec_timeout = EC_BASE_TIMEOUT;
  49. printk(KERN_ERR "olpc-ec: invalid argument to "
  50. "'olpc_ec_timeout=', ignoring!\n");
  51. }
  52. printk(KERN_DEBUG "olpc-ec: using %d ms delay for EC commands.\n",
  53. ec_timeout);
  54. return 1;
  55. }
  56. __setup("olpc_ec_timeout=", olpc_ec_timeout_set);
  57. /*
  58. * These {i,o}bf_status functions return whether the buffers are full or not.
  59. */
  60. static inline unsigned int ibf_status(unsigned int port)
  61. {
  62. return !!(inb(port) & 0x02);
  63. }
  64. static inline unsigned int obf_status(unsigned int port)
  65. {
  66. return inb(port) & 0x01;
  67. }
  68. #define wait_on_ibf(p, d) __wait_on_ibf(__LINE__, (p), (d))
  69. static int __wait_on_ibf(unsigned int line, unsigned int port, int desired)
  70. {
  71. unsigned int timeo;
  72. int state = ibf_status(port);
  73. for (timeo = ec_timeout; state != desired && timeo; timeo--) {
  74. mdelay(1);
  75. state = ibf_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 IBF!\n",
  80. line, ec_timeout - timeo);
  81. }
  82. return !(state == desired);
  83. }
  84. #define wait_on_obf(p, d) __wait_on_obf(__LINE__, (p), (d))
  85. static int __wait_on_obf(unsigned int line, unsigned int port, int desired)
  86. {
  87. unsigned int timeo;
  88. int state = obf_status(port);
  89. for (timeo = ec_timeout; state != desired && timeo; timeo--) {
  90. mdelay(1);
  91. state = obf_status(port);
  92. }
  93. if ((state == desired) && (ec_timeout > EC_BASE_TIMEOUT) &&
  94. timeo < (ec_timeout - EC_BASE_TIMEOUT)) {
  95. printk(KERN_WARNING "olpc-ec: %d: waited %u ms for OBF!\n",
  96. line, ec_timeout - timeo);
  97. }
  98. return !(state == desired);
  99. }
  100. /*
  101. * This allows the kernel to run Embedded Controller commands. The EC is
  102. * documented at <http://wiki.laptop.org/go/Embedded_controller>, and the
  103. * available EC commands are here:
  104. * <http://wiki.laptop.org/go/Ec_specification>. Unfortunately, while
  105. * OpenFirmware's source is available, the EC's is not.
  106. */
  107. int olpc_ec_cmd_x86(unsigned char cmd, unsigned char *inbuf, size_t inlen,
  108. unsigned char *outbuf, size_t outlen)
  109. {
  110. unsigned long flags;
  111. int ret = -EIO;
  112. int i;
  113. int restarts = 0;
  114. spin_lock_irqsave(&ec_lock, flags);
  115. /* Clear OBF */
  116. for (i = 0; i < 10 && (obf_status(0x6c) == 1); i++)
  117. inb(0x68);
  118. if (i == 10) {
  119. printk(KERN_ERR "olpc-ec: timeout while attempting to "
  120. "clear OBF flag!\n");
  121. goto err;
  122. }
  123. if (wait_on_ibf(0x6c, 0)) {
  124. printk(KERN_ERR "olpc-ec: timeout waiting for EC to "
  125. "quiesce!\n");
  126. goto err;
  127. }
  128. restart:
  129. /*
  130. * Note that if we time out during any IBF checks, that's a failure;
  131. * we have to return. There's no way for the kernel to clear that.
  132. *
  133. * If we time out during an OBF check, we can restart the command;
  134. * reissuing it will clear the OBF flag, and we should be alright.
  135. * The OBF flag will sometimes misbehave due to what we believe
  136. * is a hardware quirk..
  137. */
  138. pr_devel("olpc-ec: running cmd 0x%x\n", cmd);
  139. outb(cmd, 0x6c);
  140. if (wait_on_ibf(0x6c, 0)) {
  141. printk(KERN_ERR "olpc-ec: timeout waiting for EC to read "
  142. "command!\n");
  143. goto err;
  144. }
  145. if (inbuf && inlen) {
  146. /* write data to EC */
  147. for (i = 0; i < inlen; i++) {
  148. pr_devel("olpc-ec: sending cmd arg 0x%x\n", inbuf[i]);
  149. outb(inbuf[i], 0x68);
  150. if (wait_on_ibf(0x6c, 0)) {
  151. printk(KERN_ERR "olpc-ec: timeout waiting for"
  152. " EC accept data!\n");
  153. goto err;
  154. }
  155. }
  156. }
  157. if (outbuf && outlen) {
  158. /* read data from EC */
  159. for (i = 0; i < outlen; i++) {
  160. if (wait_on_obf(0x6c, 1)) {
  161. printk(KERN_ERR "olpc-ec: timeout waiting for"
  162. " EC to provide data!\n");
  163. if (restarts++ < 10)
  164. goto restart;
  165. goto err;
  166. }
  167. outbuf[i] = inb(0x68);
  168. pr_devel("olpc-ec: received 0x%x\n", outbuf[i]);
  169. }
  170. }
  171. ret = 0;
  172. err:
  173. spin_unlock_irqrestore(&ec_lock, flags);
  174. return ret;
  175. }
  176. EXPORT_SYMBOL_GPL(olpc_ec_cmd_x86);
  177. void olpc_ec_wakeup_set(u16 value)
  178. {
  179. ec_wakeup_mask |= value;
  180. }
  181. EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
  182. void olpc_ec_wakeup_clear(u16 value)
  183. {
  184. ec_wakeup_mask &= ~value;
  185. }
  186. EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
  187. /*
  188. * Returns true if the compile and runtime configurations allow for EC events
  189. * to wake the system.
  190. */
  191. bool olpc_ec_wakeup_available(void)
  192. {
  193. if (!machine_is_olpc())
  194. return false;
  195. /*
  196. * XO-1 EC wakeups are available when olpc-xo1-sci driver is
  197. * compiled in
  198. */
  199. #ifdef CONFIG_OLPC_XO1_SCI
  200. if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) /* XO-1 */
  201. return true;
  202. #endif
  203. /*
  204. * XO-1.5 EC wakeups are available when olpc-xo15-sci driver is
  205. * compiled in
  206. */
  207. #ifdef CONFIG_OLPC_XO15_SCI
  208. if (olpc_platform_info.boardrev >= olpc_board_pre(0xd0)) /* XO-1.5 */
  209. return true;
  210. #endif
  211. return false;
  212. }
  213. EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
  214. int olpc_ec_mask_write(u16 bits)
  215. {
  216. if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
  217. __be16 ec_word = cpu_to_be16(bits);
  218. return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *) &ec_word, 2,
  219. NULL, 0);
  220. } else {
  221. unsigned char ec_byte = bits & 0xff;
  222. return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
  223. }
  224. }
  225. EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
  226. int olpc_ec_sci_query(u16 *sci_value)
  227. {
  228. int ret;
  229. if (olpc_platform_info.flags & OLPC_F_EC_WIDE_SCI) {
  230. __be16 ec_word;
  231. ret = olpc_ec_cmd(EC_EXT_SCI_QUERY,
  232. NULL, 0, (void *) &ec_word, 2);
  233. if (ret == 0)
  234. *sci_value = be16_to_cpu(ec_word);
  235. } else {
  236. unsigned char ec_byte;
  237. ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
  238. if (ret == 0)
  239. *sci_value = ec_byte;
  240. }
  241. return ret;
  242. }
  243. EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
  244. static ssize_t ec_debugfs_cmd_write(struct file *file, const char __user *buf,
  245. size_t size, loff_t *ppos)
  246. {
  247. int i, m;
  248. unsigned char ec_cmd[EC_MAX_CMD_ARGS];
  249. unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
  250. char cmdbuf[64];
  251. int ec_cmd_bytes;
  252. mutex_lock(&ec_debugfs_cmd_lock);
  253. size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
  254. m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
  255. &ec_debugfs_resp_bytes,
  256. &ec_cmd_int[1], &ec_cmd_int[2], &ec_cmd_int[3],
  257. &ec_cmd_int[4], &ec_cmd_int[5]);
  258. if (m < 2 || ec_debugfs_resp_bytes > EC_MAX_CMD_REPLY) {
  259. /* reset to prevent overflow on read */
  260. ec_debugfs_resp_bytes = 0;
  261. printk(KERN_DEBUG "olpc-ec: bad ec cmd: "
  262. "cmd:response-count [arg1 [arg2 ...]]\n");
  263. size = -EINVAL;
  264. goto out;
  265. }
  266. /* convert scanf'd ints to char */
  267. ec_cmd_bytes = m - 2;
  268. for (i = 0; i <= ec_cmd_bytes; i++)
  269. ec_cmd[i] = ec_cmd_int[i];
  270. printk(KERN_DEBUG "olpc-ec: debugfs cmd 0x%02x with %d args "
  271. "%02x %02x %02x %02x %02x, want %d returns\n",
  272. ec_cmd[0], ec_cmd_bytes, ec_cmd[1], ec_cmd[2], ec_cmd[3],
  273. ec_cmd[4], ec_cmd[5], ec_debugfs_resp_bytes);
  274. olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
  275. ec_cmd_bytes, ec_debugfs_resp, ec_debugfs_resp_bytes);
  276. printk(KERN_DEBUG "olpc-ec: response "
  277. "%02x %02x %02x %02x %02x %02x %02x %02x (%d bytes expected)\n",
  278. ec_debugfs_resp[0], ec_debugfs_resp[1], ec_debugfs_resp[2],
  279. ec_debugfs_resp[3], ec_debugfs_resp[4], ec_debugfs_resp[5],
  280. ec_debugfs_resp[6], ec_debugfs_resp[7], ec_debugfs_resp_bytes);
  281. out:
  282. mutex_unlock(&ec_debugfs_cmd_lock);
  283. return size;
  284. }
  285. static ssize_t ec_debugfs_cmd_read(struct file *file, char __user *buf,
  286. size_t size, loff_t *ppos)
  287. {
  288. unsigned int i, r;
  289. char *rp;
  290. char respbuf[64];
  291. mutex_lock(&ec_debugfs_cmd_lock);
  292. rp = respbuf;
  293. rp += sprintf(rp, "%02x", ec_debugfs_resp[0]);
  294. for (i = 1; i < ec_debugfs_resp_bytes; i++)
  295. rp += sprintf(rp, ", %02x", ec_debugfs_resp[i]);
  296. mutex_unlock(&ec_debugfs_cmd_lock);
  297. rp += sprintf(rp, "\n");
  298. r = rp - respbuf;
  299. return simple_read_from_buffer(buf, size, ppos, respbuf, r);
  300. }
  301. static const struct file_operations ec_debugfs_genops = {
  302. .write = ec_debugfs_cmd_write,
  303. .read = ec_debugfs_cmd_read,
  304. };
  305. static void setup_debugfs(void)
  306. {
  307. ec_debugfs_dir = debugfs_create_dir("olpc-ec", 0);
  308. if (ec_debugfs_dir == ERR_PTR(-ENODEV))
  309. return;
  310. debugfs_create_file("cmd", 0600, ec_debugfs_dir, NULL,
  311. &ec_debugfs_genops);
  312. }
  313. static int olpc_ec_suspend(void)
  314. {
  315. return olpc_ec_mask_write(ec_wakeup_mask);
  316. }
  317. static bool __init check_ofw_architecture(struct device_node *root)
  318. {
  319. const char *olpc_arch;
  320. int propsize;
  321. olpc_arch = of_get_property(root, "architecture", &propsize);
  322. return propsize == 5 && strncmp("OLPC", olpc_arch, 5) == 0;
  323. }
  324. static u32 __init get_board_revision(struct device_node *root)
  325. {
  326. int propsize;
  327. const __be32 *rev;
  328. rev = of_get_property(root, "board-revision-int", &propsize);
  329. if (propsize != 4)
  330. return 0;
  331. return be32_to_cpu(*rev);
  332. }
  333. static bool __init platform_detect(void)
  334. {
  335. struct device_node *root = of_find_node_by_path("/");
  336. bool success;
  337. if (!root)
  338. return false;
  339. success = check_ofw_architecture(root);
  340. if (success) {
  341. olpc_platform_info.boardrev = get_board_revision(root);
  342. olpc_platform_info.flags |= OLPC_F_PRESENT;
  343. }
  344. of_node_put(root);
  345. return success;
  346. }
  347. static int __init add_xo1_platform_devices(void)
  348. {
  349. struct platform_device *pdev;
  350. pdev = platform_device_register_simple("xo1-rfkill", -1, NULL, 0);
  351. if (IS_ERR(pdev))
  352. return PTR_ERR(pdev);
  353. pdev = platform_device_register_simple("olpc-xo1", -1, NULL, 0);
  354. if (IS_ERR(pdev))
  355. return PTR_ERR(pdev);
  356. return 0;
  357. }
  358. static struct syscore_ops olpc_syscore_ops = {
  359. .suspend = olpc_ec_suspend,
  360. };
  361. static int __init olpc_init(void)
  362. {
  363. int r = 0;
  364. if (!olpc_ofw_present() || !platform_detect())
  365. return 0;
  366. spin_lock_init(&ec_lock);
  367. /* assume B1 and above models always have a DCON */
  368. if (olpc_board_at_least(olpc_board(0xb1)))
  369. olpc_platform_info.flags |= OLPC_F_DCON;
  370. /* get the EC revision */
  371. olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0,
  372. (unsigned char *) &olpc_platform_info.ecver, 1);
  373. #ifdef CONFIG_PCI_OLPC
  374. /* If the VSA exists let it emulate PCI, if not emulate in kernel.
  375. * XO-1 only. */
  376. if (olpc_platform_info.boardrev < olpc_board_pre(0xd0) &&
  377. !cs5535_has_vsa2())
  378. x86_init.pci.arch_init = pci_olpc_init;
  379. #endif
  380. /* EC version 0x5f adds support for wide SCI mask */
  381. if (olpc_platform_info.ecver >= 0x5f)
  382. olpc_platform_info.flags |= OLPC_F_EC_WIDE_SCI;
  383. printk(KERN_INFO "OLPC board revision %s%X (EC=%x)\n",
  384. ((olpc_platform_info.boardrev & 0xf) < 8) ? "pre" : "",
  385. olpc_platform_info.boardrev >> 4,
  386. olpc_platform_info.ecver);
  387. if (olpc_platform_info.boardrev < olpc_board_pre(0xd0)) { /* XO-1 */
  388. r = add_xo1_platform_devices();
  389. if (r)
  390. return r;
  391. }
  392. register_syscore_ops(&olpc_syscore_ops);
  393. setup_debugfs();
  394. return 0;
  395. }
  396. postcore_initcall(olpc_init);