RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / arch / mips / loongson / lemote-2f / ec_kb3310b.c
blob64057244eec549b732fdc4e550e2388cf54c013a
1 /*
2 * Basic KB3310B Embedded Controller support for the YeeLoong 2F netbook
4 * Copyright (C) 2008 Lemote Inc.
5 * Author: liujl <liujl@lemote.com>, 2008-04-20
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.
13 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/delay.h>
17 #include "ec_kb3310b.h"
19 static DEFINE_SPINLOCK(index_access_lock);
20 static DEFINE_SPINLOCK(port_access_lock);
22 unsigned char ec_read(unsigned short addr)
24 unsigned char value;
25 unsigned long flags;
27 spin_lock_irqsave(&index_access_lock, flags);
28 outb((addr & 0xff00) >> 8, EC_IO_PORT_HIGH);
29 outb((addr & 0x00ff), EC_IO_PORT_LOW);
30 value = inb(EC_IO_PORT_DATA);
31 spin_unlock_irqrestore(&index_access_lock, flags);
33 return value;
35 EXPORT_SYMBOL_GPL(ec_read);
37 void ec_write(unsigned short addr, unsigned char val)
39 unsigned long flags;
41 spin_lock_irqsave(&index_access_lock, flags);
42 outb((addr & 0xff00) >> 8, EC_IO_PORT_HIGH);
43 outb((addr & 0x00ff), EC_IO_PORT_LOW);
44 outb(val, EC_IO_PORT_DATA);
45 /* flush the write action */
46 inb(EC_IO_PORT_DATA);
47 spin_unlock_irqrestore(&index_access_lock, flags);
49 return;
51 EXPORT_SYMBOL_GPL(ec_write);
54 * This function is used for EC command writes and corresponding status queries.
56 int ec_query_seq(unsigned char cmd)
58 int timeout;
59 unsigned char status;
60 unsigned long flags;
61 int ret = 0;
63 spin_lock_irqsave(&port_access_lock, flags);
65 /* make chip goto reset mode */
66 udelay(EC_REG_DELAY);
67 outb(cmd, EC_CMD_PORT);
68 udelay(EC_REG_DELAY);
70 /* check if the command is received by ec */
71 timeout = EC_CMD_TIMEOUT;
72 status = inb(EC_STS_PORT);
73 while (timeout-- && (status & (1 << 1))) {
74 status = inb(EC_STS_PORT);
75 udelay(EC_REG_DELAY);
78 spin_unlock_irqrestore(&port_access_lock, flags);
80 if (timeout <= 0) {
81 printk(KERN_ERR "%s: deadable error : timeout...\n", __func__);
82 ret = -EINVAL;
83 } else
84 printk(KERN_INFO
85 "(%x/%d)ec issued command %d status : 0x%x\n",
86 timeout, EC_CMD_TIMEOUT - timeout, cmd, status);
88 return ret;
90 EXPORT_SYMBOL_GPL(ec_query_seq);
93 * Send query command to EC to get the proper event number
95 int ec_query_event_num(void)
97 return ec_query_seq(CMD_GET_EVENT_NUM);
99 EXPORT_SYMBOL(ec_query_event_num);
102 * Get event number from EC
104 * NOTE: This routine must follow the query_event_num function in the
105 * interrupt.
107 int ec_get_event_num(void)
109 int timeout = 100;
110 unsigned char value;
111 unsigned char status;
113 udelay(EC_REG_DELAY);
114 status = inb(EC_STS_PORT);
115 udelay(EC_REG_DELAY);
116 while (timeout-- && !(status & (1 << 0))) {
117 status = inb(EC_STS_PORT);
118 udelay(EC_REG_DELAY);
120 if (timeout <= 0) {
121 pr_info("%s: get event number timeout.\n", __func__);
123 return -EINVAL;
125 value = inb(EC_DAT_PORT);
126 udelay(EC_REG_DELAY);
128 return value;
130 EXPORT_SYMBOL(ec_get_event_num);