Path: blob/master/arch/mips/loongson/lemote-2f/ec_kb3310b.c
10818 views
/*1* Basic KB3310B Embedded Controller support for the YeeLoong 2F netbook2*3* Copyright (C) 2008 Lemote Inc.4* Author: liujl <[email protected]>, 2008-04-205*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*/1112#include <linux/module.h>13#include <linux/spinlock.h>14#include <linux/delay.h>1516#include "ec_kb3310b.h"1718static DEFINE_SPINLOCK(index_access_lock);19static DEFINE_SPINLOCK(port_access_lock);2021unsigned char ec_read(unsigned short addr)22{23unsigned char value;24unsigned long flags;2526spin_lock_irqsave(&index_access_lock, flags);27outb((addr & 0xff00) >> 8, EC_IO_PORT_HIGH);28outb((addr & 0x00ff), EC_IO_PORT_LOW);29value = inb(EC_IO_PORT_DATA);30spin_unlock_irqrestore(&index_access_lock, flags);3132return value;33}34EXPORT_SYMBOL_GPL(ec_read);3536void ec_write(unsigned short addr, unsigned char val)37{38unsigned long flags;3940spin_lock_irqsave(&index_access_lock, flags);41outb((addr & 0xff00) >> 8, EC_IO_PORT_HIGH);42outb((addr & 0x00ff), EC_IO_PORT_LOW);43outb(val, EC_IO_PORT_DATA);44/* flush the write action */45inb(EC_IO_PORT_DATA);46spin_unlock_irqrestore(&index_access_lock, flags);4748return;49}50EXPORT_SYMBOL_GPL(ec_write);5152/*53* This function is used for EC command writes and corresponding status queries.54*/55int ec_query_seq(unsigned char cmd)56{57int timeout;58unsigned char status;59unsigned long flags;60int ret = 0;6162spin_lock_irqsave(&port_access_lock, flags);6364/* make chip goto reset mode */65udelay(EC_REG_DELAY);66outb(cmd, EC_CMD_PORT);67udelay(EC_REG_DELAY);6869/* check if the command is received by ec */70timeout = EC_CMD_TIMEOUT;71status = inb(EC_STS_PORT);72while (timeout-- && (status & (1 << 1))) {73status = inb(EC_STS_PORT);74udelay(EC_REG_DELAY);75}7677spin_unlock_irqrestore(&port_access_lock, flags);7879if (timeout <= 0) {80printk(KERN_ERR "%s: deadable error : timeout...\n", __func__);81ret = -EINVAL;82} else83printk(KERN_INFO84"(%x/%d)ec issued command %d status : 0x%x\n",85timeout, EC_CMD_TIMEOUT - timeout, cmd, status);8687return ret;88}89EXPORT_SYMBOL_GPL(ec_query_seq);9091/*92* Send query command to EC to get the proper event number93*/94int ec_query_event_num(void)95{96return ec_query_seq(CMD_GET_EVENT_NUM);97}98EXPORT_SYMBOL(ec_query_event_num);99100/*101* Get event number from EC102*103* NOTE: This routine must follow the query_event_num function in the104* interrupt.105*/106int ec_get_event_num(void)107{108int timeout = 100;109unsigned char value;110unsigned char status;111112udelay(EC_REG_DELAY);113status = inb(EC_STS_PORT);114udelay(EC_REG_DELAY);115while (timeout-- && !(status & (1 << 0))) {116status = inb(EC_STS_PORT);117udelay(EC_REG_DELAY);118}119if (timeout <= 0) {120pr_info("%s: get event number timeout.\n", __func__);121122return -EINVAL;123}124value = inb(EC_DAT_PORT);125udelay(EC_REG_DELAY);126127return value;128}129EXPORT_SYMBOL(ec_get_event_num);130131132