C3cpu_wtd_sysfs

Posted 码农编程录

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C3cpu_wtd_sysfs相关的知识,希望对你有一定的参考价值。

文章目录


2.cpu_wtd_sysfs:

switchboard.c (fpga下i2c访问Switch CPLD1,Switch CPLD2 :CPLD, FPGA , QSFP)

// cpld_h.c : lpc访问COMe/Baseboard CPLD : CPLD , LED , WatchDog
#include "common_hq.h"
#include <linux/kobject.h>
#define CPLD_DRIVER_NAME 	"sys_cpld"
#define CPLD_DRIVER_VER		"0.0.1"
/**
 * CPLD register address for read and write.
 */
/*
 * Base CPLD:0xA100 ~ 0xA1DF
 * COMe CPLD:0xA1E0 ~ 0xA1FF
 * */
#define BASE_CPLD_ADDR 		0xA100
#define COME_CPLD_ADDR		0xA1E0
#define VERSION_ADDR 		0xA100

#define BASE_PCB_VER_ADDR	0xA100
#define BASE_H_VER_ADDR		0xA101
#define BASE_M_VER_ADDR		0xA102
#define BASE_L_VER_ADDR		0xA103
#define BASE_SCRATCH_ADDR	0xA104

#define COME_PCB_VER_ADDR	0xA1E0
#define COME_H_VER_ADDR		0xA1E1
#define COME_M_VER_ADDR		0xA1E2
#define COME_L_VER_ADDR		0xA1E3
#define COME_SCRATCH_ADDR	0xA1E4

#define CPLD_REGISTER_SIZE 	0xFF
#define CPLD_TOTAL_NUMBER   4

/*watchdog*/
#define BASE_REG_ADDR_WD_EN         0xA190
#define BASE_REG_ADDR_WD_LTIME      0xA191
#define BASE_REG_ADDR_WD_HTIME      0xA192
#define BASE_REG_ADDR_WD_KICK       0xA193
#define BASE_REG_ADDR_WD_FOUN       0xA194
#define BASE_REG_ADDR_WD_STAE       0xA195  //status
#define BASE_REG_ADDR_WD_CLEAR      0xA196
#define BASE_REG_ADDR_WD_LTIMELEFT  0xA197
#define BASE_REG_ADDR_WD_HTIMELEFT  0xA198

/*sysled*/
#define PSU_LED_ADDR     0xA140
#define SYS_LED_ADDR     0xA141
#define Alarm_LED_ADDR   0xA142
#define Fan_LED_ADDR     0xA143
#define BMC_LED_ADDR     0xA144

enum

    DARK,
    GREEN,
    YELLOW,
    RED,
    BLUE,
    GREEN_LIGHT_FLASHING,
    YELLOW_LIGHT_FLASHING,
    RED_LIGHT_FLASHING,
    BLUE_LIGHT_FLASHING
;

int set_bit_value(int value,int bit,int status);
int get_bit_value(int value,int bit);

/******************************************************watchdog*******************************************************************/
ssize_t get_main_watchdog_identify(char *buf, size_t count)

    return sprintf(buf,"CPU_wdt\\n");

EXPORT_SYMBOL(get_main_watchdog_identify);

ssize_t get_main_watchdog_state(char *buf, size_t count)

    int data=inb(BASE_REG_ADDR_WD_EN);
    // printk(KERN_INFO "Baseboard CPLD addr:%.2x ,value: %.2x \\n",BASE_REG_ADDR_WD_EN,inb(BASE_REG_ADDR_WD_EN));
    data = get_bit_value(data,0);
    data=sprintf(buf,"%x\\n",data);  //转为16进制
    return data;

EXPORT_SYMBOL(get_main_watchdog_state);

ssize_t get_main_watchdog_timeleft(char *buf, size_t count)

    int data_ligh = inb(BASE_REG_ADDR_WD_HTIMELEFT);
    // printk(KERN_INFO "Baseboard CPLD addr BASE_REG_ADDR_WD_HTIME :%.2x,value: %.2x \\n",BASE_REG_ADDR_WD_HTIMELEFT,data_ligh);
    int data_low = inb(BASE_REG_ADDR_WD_LTIMELEFT);
    // printk(KERN_INFO "Baseboard CPLD addr BASE_REG_ADDR_WD_LTIME:%.2x,value: %.2x \\n",BASE_REG_ADDR_WD_LTIMELEFT,data_low);
    int data=(data_ligh << 8 )+data_low;
    // printk(KERN_INFO "data :%.2x \\n",data);
    data=data/1000;
    data=sprintf(buf,"%d\\n",data);
    return data;

EXPORT_SYMBOL(get_main_watchdog_timeleft);

ssize_t get_main_watchdog_timeout(char *buf, size_t count)

    int data_ligh = inb(BASE_REG_ADDR_WD_HTIME);
    // printk(KERN_INFO "Baseboard CPLD addr BASE_REG_ADDR_WD_HTIME :%.2x,value: %.2x \\n",BASE_REG_ADDR_WD_HTIME,data_ligh);
    int data_low = inb(BASE_REG_ADDR_WD_LTIME);
    // printk(KERN_INFO "Baseboard CPLD addr BASE_REG_ADDR_WD_LTIME:%.2x,value: %.2x \\n",BASE_REG_ADDR_WD_LTIME,data_low);
    int data=(data_ligh << 8 )+data_low;    //拼接
    // printk(KERN_INFO "data :%.2x \\n",data);
    data=data/1000;   //毫秒变成秒
    data=sprintf(buf,"%d\\n",data);
    return data;

EXPORT_SYMBOL(get_main_watchdog_timeout);

int set_main_watchdog_timeout(int value)

    int value_ms = value * 1000 ;  //秒变成毫秒
    outb(value_ms&0xff ,BASE_REG_ADDR_WD_LTIME);  //outb写,取低八位
    outb((value_ms>>8)&0xff ,BASE_REG_ADDR_WD_HTIME);
    return;

EXPORT_SYMBOL(set_main_watchdog_timeout);

static void feed_watchdog(void)

        int addr,value;
        addr = BASE_REG_ADDR_WD_KICK;
        value = inb(addr);  // 读
        value = set_bit_value(value,0,0);  // 设 0x7c
        value = set_bit_value(value,1,0);
        value = set_bit_value(value,2,1);
        value = set_bit_value(value,3,1);
        value = set_bit_value(value,4,1);
        value = set_bit_value(value,5,1);
        value = set_bit_value(value,6,1);
        value = set_bit_value(value,7,0);
        outb(value,addr);  // 写


ssize_t get_main_watchdog_enable_status(char *buf, size_t count)

    int addr,value;
    addr = BASE_REG_ADDR_WD_EN;
    value=inb(addr);
    value = get_bit_value(value,0);   //看最后一位
    return sprintf(buf,"%02X\\n", value);

EXPORT_SYMBOL(get_main_watchdog_enable_status);

int set_main_watchdog_enable_status(int status)

    int addr,value;
    addr = BASE_REG_ADDR_WD_EN;
    value = inb(addr);
    switch(status)
    
        case 0:
            value = set_bit_value(value,0,0);
            outb(value,addr);
            break;
        case 1:
            value = set_bit_value(value,0,1);
            outb(value,addr);
            feed_watchdog();
            break;
        default:
            return -ENOSYS;
    
    return status;

EXPORT_SYMBOL(set_main_watchdog_enable_status);


/******************************************************************************new enable node***********************************/
ssize_t get_watchdog_enable_status(struct device *dev, struct device_attribute *attr, char *buf)

    size_t count=0;
    return get_main_watchdog_enable_status(buf, count);

static ssize_t set_watchdog_enable_status(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)

    int value,rc = 0;
    rc = kstrtoint(buf,10,&value);
    if (rc != 0) 
        return size;
    
    set_main_watchdog_enable_status(value);
    return size;

static struct device_attribute watchdog_enable_attr = __ATTR(enable, S_IRUGO | S_IWUSR,get_watchdog_enable_status,set_watchdog_enable_status);


/****************************************************************************new timeout node*******************************/
ssize_t get_watchdog_timeout(struct device *dev, struct device_attribute *attr, char *buf)

    return get_main_watchdog_timeout(buf,NULL);

static ssize_t set_watchdog_timeout(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)

    int value,rc = 0;
    rc = kstrtoint(buf,10,&value);
    if (rc != 0) 
        return size;
    
    set_main_watchdog_timeout(value);
    return size;

static struct device_attribute watchdog_timeout_attr = __ATTR(timeout, S_IRUGO | S_IWUSR,get_watchdog_timeout,set_watchdog_timeout);


/******************************************************************************new feed node*******************************/
static ssize_t hq_set_watchdog_feed(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)

    int data,rc = 0;
    rc = kstrtoint(buf,16,&data);
    if (rc != 0) 
        return size;
    
    // printk(KERN_INFO "Baseboard CPLD addr BASE_REG_ADDR_WD_KICK :%.2x,value: %.2x \\n",BASE_REG_ADDR_WD_KICK,data);
    if(data==0x7c)
    
        outb(data,BASE_REG_ADDR_WD_KICK);
        // printk(KERN_INFO "Baseboard CPLD feed dog getreg value : %.2x \\n",inb(BASE_REG_ADDR_WD_KICK));
    
    else
    
        return -EINVAL;
    
    return size;

static struct device_attribute watchdog_feed_attt = __ATTR(feed, S_IRUGO | S_IWUSR, NULL, hq_set_watchdog_feed);


/************************************************************************new strategy node*******************************/
ssize_t hq_get_watchdog_strategy(struct device *dev, struct device_attribute *attr, char *buf)

    return sprintf(buf,"%0.2x\\n",inb(BASE_REG_ADDR_WD_FOUN));

static ssize_t hq_set_watchdog_strategy(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)

    int data,rc = 0;
    rc = kstrtoint(buf,16,&data);
    if (rc != 0) 
        return size;
    
    if((data==0x01) || (data ==0x02))
    
        outb(data,BASE_REG_ADDR_WD_FOUN);
        // printk(KERN_INFO " hq_set_watchdog_strategy addr value : %.2x \\n",inb(BASE_REG_ADDR_WD_FOUN));
    
    else
    
        return -EINVAL;
    
    return size;

static struct device_attribute watchdog_strategy_attr = __ATTR(strategy, S_IRUGO | S_IWUSR,hq_get_watchdog_strategy ,hq_set_watchdog_strategy);


/********************************************************************new timeout_counts node*******************************/
ssize_t hq_get_watchdog_timeout_times(struct device *dev, struct device_attribute *attr, char *buf)

    return sprintf(buf,"%d\\n",inb(BASE_REG_ADDR_WD_STAE));

static struct device_attribute watchdog_timeout_counts_attr = __ATTR(timeout_counts, S_IWUSR|S_IRUGO,hq_get_watchdog_timeout_times, NULL);


/********************************************************************new timeout_reset node*******************************/
static ssize_t hq_set_watchdog_timeout_reset(struct device *dev, struct device_attribute *attr, const char *buf, size_t size)

    int data,rc = 0;
    rc = kstrtoint(buf,16,&data);
    if (rc != 0) 
        return size;
    
    if(data==1)
    
        outb(data,BASE_REG_ADDR_WD_KICK);
    
    else
    
        return -EINVAL;
    
    return size;

static struct device_attribute watchdog_timeout_reset_attr = __ATTR(timeout_reset, S_IRUGO | S_IWUSR,NULL ,hq_set_watchdog_timeout_reset);

static struct attribute *watchdog_dir_attrs[] = 
    &watchdog_enable_attr.attr,
    &watchdog_timeout_attr.attr,
    &watchdog_feed_attt.attr,
    &watchdog_strategy_attr.attr,
    &watchdog_timeout_counts_attr.attr,
    &watchdog_timeout_reset_attr.attr,
    NULL
;

static struct attribute_group watchdog_attr_group =    // 在下面cpld_hq_drv_probe函数里: kobject_create_and_add, sysfs_create_group
    .attrs = watchdog_dir_attrs,
;
/******************************************************watchdog end*********************************************************************************/

/******************************************************sysled begain*******************************************************************************/
int set_sysled_state(int status,char *names)

    int addr,value;
    if(strcmp(names,"PSU")==0)
    
        addr=PSU_LED_ADDR;
    
    else if(strcmp(names,"SYS")==0)
    
        addr=SYS_LED_ADDR;
    
    else if(strcmp(names,"Alarm")==0)
    
       addr=Alarm_LED_ADDR;
    
    else if(strcmp(names,"Fan")==0)
    
        addr=Fan_LED_ADDR;
    
    else if(strcmp(names,"BMC")==0)
    
        addr=BMC_LED_ADDR;
    
    else
    
        return -ENOSYS;
    
    value=inb(addr);
    switch(status)
    
        case DARK:
                value=set_bit_value(value,6,1);
                value=set_bit_value(value,0,1);
                value=set_bit_value(value,1,0);
                outb(value,addr);
                // printk(KERN_INFO "Baseboard CPLD addr:%.2x ,value: %.2x \\n",addr,inb(addr));
                break;
        case GREEN:
                value=set_bit_value(value,6,1);
                value=set_bit_value(value,4,0);
                value=set_bit_value(value,5,0);
                value=set_bit_value(value,1,0);
                value=set_bit_value(value,0,0);
                outb(value,addr);
                // printk(KERN_INFO "Baseboard CPLD addr:%.2x ,value: %.2x \\n",addr,inb(addr));
                break;
        case YELLOW:
                value=set_bit_value(value,6,1);
                value=set_bit_value(value,4,1);
                value=set_bit_value(value,5,0)以上是关于C3cpu_wtd_sysfs的主要内容,如果未能解决你的问题,请参考以下文章

设置树莓派屏幕常亮,禁止树莓派屏幕休眠

iOS 设置屏幕常亮,延长休眠时间

Android保持屏幕常亮

Xamarin Essentials教程屏幕常亮ScreenLock

ttl转485模块rxd常亮

java 屏幕常亮