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