diff --git a/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py b/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py index c859532e5..f6450792a 100644 --- a/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py +++ b/packages/base/all/vendor-config-onl/src/python/onl/platform/base.py @@ -664,3 +664,7 @@ class OnlPlatformPortConfig_48x1_6x10(object): class OnlPlatformPortConfig_4x100_20x25(object): PORT_COUNT=24 PORT_CONFIG="4x100 + 20x25" + +class OnlPlatformPortConfig_32x400_1x10_1x1(object): + PORT_COUNT=34 + PORT_CONFIG="32x400 + 1x10 + 1x1" diff --git a/packages/platforms/accton/x86-64/as9737-32db/.gitignore b/packages/platforms/accton/x86-64/as9737-32db/.gitignore new file mode 100644 index 000000000..8af48e604 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/.gitignore @@ -0,0 +1,2 @@ +*x86*64*accton*as9737*32db*.mk +onlpdump.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/Makefile b/packages/platforms/accton/x86-64/as9737-32db/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/Makefile b/packages/platforms/accton/x86-64/as9737-32db/modules/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/PKG.yml b/packages/platforms/accton/x86-64/as9737-32db/modules/PKG.yml new file mode 100644 index 000000000..528123376 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/PKG.yml @@ -0,0 +1 @@ +!include $ONL_TEMPLATES/platform-modules.yml VENDOR=accton BASENAME=x86-64-accton-as9737-32db ARCH=amd64 KERNELS="onl-kernel-6.1-lts-x86-64-all:amd64" diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/.gitignore b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/.gitignore new file mode 100644 index 000000000..a65b41774 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/.gitignore @@ -0,0 +1 @@ +lib diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/Makefile b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/Makefile new file mode 100644 index 000000000..eb85ba871 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/Makefile @@ -0,0 +1,6 @@ +KERNELS := onl-kernel-6.1-lts-x86-64-all:amd64 +KMODULES := src +VENDOR := accton +BASENAME := x86-64-accton-as9737-32db +ARCH := x86_64 +include $(ONL)/make/kmodule.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/Makefile b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/Makefile new file mode 100644 index 000000000..1ce36b1eb --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/Makefile @@ -0,0 +1,10 @@ +obj-m += accton_ipmi_intf.o +obj-m += x86-64-accton-as9737-32db-cpld.o +obj-m += x86-64-accton-as9737-32db-fan.o +obj-m += x86-64-accton-as9737-32db-fpga.o +obj-m += x86-64-accton-as9737-32db-i2c-ocores.o +obj-m += x86-64-accton-as9737-32db-leds.o +obj-m += x86-64-accton-as9737-32db-mux.o +obj-m += x86-64-accton-as9737-32db-psu.o +obj-m += x86-64-accton-as9737-32db-sys.o +obj-m += x86-64-accton-as9737-32db-thermal.o diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/accton_ipmi_intf.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/accton_ipmi_intf.c new file mode 100644 index 000000000..d66fbc2b3 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/accton_ipmi_intf.c @@ -0,0 +1,232 @@ +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 +/* + * Copyright 2024 Accton Technology Corporation. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * + * Description: + * IPMI driver related interface implementation + */ +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define ACCTON_IPMI_NETFN 0x34 +#define IPMI_TIMEOUT (5 * HZ) +#define IPMI_ERR_RETRY_TIMES 0 +#define RAW_CMD_BUF_SIZE 40 + +static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data); + +/* Functions to talk to the IPMI layer */ + +/* Initialize IPMI data structure and create a user interface for communication */ +int init_ipmi_data(struct ipmi_data *ipmi, int iface, struct device *dev) +{ + int err; + + if (!ipmi || !dev) + return -EINVAL; + + init_completion(&ipmi->read_complete); + + // Initialize IPMI address + ipmi->address.addr_type = IPMI_SYSTEM_INTERFACE_ADDR_TYPE; + ipmi->address.channel = IPMI_BMC_CHANNEL; + ipmi->address.data[0] = 0; + ipmi->interface = iface; + ipmi->dev = dev; // Storing the device for future reference + + // Initialize message buffers + ipmi->tx_msgid = 0; + ipmi->tx_message.netfn = ACCTON_IPMI_NETFN; + + // Assign the message handler + ipmi->ipmi_hndlrs.ipmi_recv_hndl = ipmi_msg_handler; + + // Create IPMI messaging interface user + err = ipmi_create_user(ipmi->interface, &ipmi->ipmi_hndlrs, + ipmi, &ipmi->user); + if (err < 0) { + dev_err(dev, + "Unable to register user with IPMI interface %d, err: %d\n", + ipmi->interface, err); + return err; + } + + return 0; +} +EXPORT_SYMBOL(init_ipmi_data); + +/* Handler function for receiving IPMI messages */ +static void ipmi_msg_handler(struct ipmi_recv_msg *msg, void *user_msg_data) +{ + unsigned short rx_len; + struct ipmi_data *ipmi = user_msg_data; + + // Check for message ID mismatch + if (msg->msgid != ipmi->tx_msgid) { + dev_err(ipmi->dev, "Mismatch between received msgid " + "(%02x) and transmitted msgid (%02x)!\n", + (int)msg->msgid, (int)ipmi->tx_msgid); + ipmi_free_recv_msg(msg); + return; + } + + // Handle received message type + ipmi->rx_recv_type = msg->recv_type; + + // Parse message data + if (msg->msg.data_len > 0) + ipmi->rx_result = msg->msg.data[0]; + else + ipmi->rx_result = IPMI_UNKNOWN_ERR_COMPLETION_CODE; + + // Copy remaining message data if available + if (msg->msg.data_len > 1) { + rx_len = msg->msg.data_len - 1; + if (ipmi->rx_msg_len < rx_len) + rx_len = ipmi->rx_msg_len; + + ipmi->rx_msg_len = rx_len; + memcpy(ipmi->rx_msg_data, msg->msg.data + 1, ipmi->rx_msg_len); + } else { + ipmi->rx_msg_len = 0; + } + + // Free the received message and signal completion + ipmi_free_recv_msg(msg); + complete(&ipmi->read_complete); +} + +static void _ipmi_log_error(struct ipmi_data *ipmi, unsigned char cmd, + unsigned char *tx_data, unsigned short tx_len, + int status, int retry) +{ + int i, pos; + char *cmdline = NULL; + char raw_cmd[RAW_CMD_BUF_SIZE] = { 0 }; + + // Format the command and data into a raw command string + pos = snprintf(raw_cmd, sizeof(raw_cmd), "0x%02x", cmd); + for (i = 0; i < tx_len && pos < sizeof(raw_cmd); i++) { + pos += snprintf(raw_cmd + pos, sizeof(raw_cmd) - pos, + " 0x%02x", tx_data[i]); + } + + // Log the error message + cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL); + dev_err(ipmi->dev, + "ipmi_send_message: retry(%d), error(%d), cmd(%s) raw_cmd=[%s]\r\n", + retry, status, cmdline ? cmdline : "", raw_cmd); + + if (cmdline) { + kfree(cmdline); + } +} + +/* Send an IPMI command */ +static int _ipmi_send_message(struct ipmi_data *ipmi, unsigned char cmd, + unsigned char *tx_data, unsigned short tx_len, + unsigned char *rx_data, unsigned short rx_len) +{ + int err; + + // Validate the input parameters + if ((tx_len && !tx_data) || (rx_len && !rx_data)) { + return -EINVAL; + } + + // Initialize IPMI message + ipmi->tx_message.cmd = cmd; + ipmi->tx_message.data = tx_len ? tx_data : NULL; + ipmi->tx_message.data_len = tx_len; + ipmi->rx_msg_data = rx_len ? rx_data : NULL; + ipmi->rx_msg_len = rx_len; + + // Validate the IPMI address + err = ipmi_validate_addr(&ipmi->address, sizeof(ipmi->address)); + if (err) { + dev_err(ipmi->dev, "Invalid IPMI address: %x\n", err); + return err; + } + + // Increment message ID and send the request + ipmi->tx_msgid++; + err = ipmi_request_settime(ipmi->user, &ipmi->address, ipmi->tx_msgid, + &ipmi->tx_message, ipmi, 0, 0, 0); + if (err) { + dev_err(ipmi->dev, "IPMI request_settime failed: %x\n", err); + return err; + } + + // Wait for the message to complete + err = wait_for_completion_timeout(&ipmi->read_complete, IPMI_TIMEOUT); + if (!err) { + dev_err(ipmi->dev, "IPMI command timeout\n"); + return -ETIMEDOUT; + } + + return 0; +} + +/* Send an IPMI command to the IPMI device and receive the response */ +int ipmi_send_message(struct ipmi_data *ipmi, unsigned char cmd, + unsigned char *tx_data, unsigned short tx_len, + unsigned char *rx_data, unsigned short rx_len) +{ + int status = 0, retry = 0; + + // Validate the input parameters + if ((tx_len && !tx_data) || (rx_len && !rx_data)) { + return -EINVAL; + } + + for (retry = 0; retry <= IPMI_ERR_RETRY_TIMES; retry++) { + status = _ipmi_send_message(ipmi, cmd, tx_data, tx_len, rx_data, rx_len); + if (unlikely(status != 0)) { + _ipmi_log_error(ipmi, cmd, tx_data, tx_len, status, retry); + continue; + } + + if (unlikely(ipmi->rx_result != 0)) { + _ipmi_log_error(ipmi, cmd, tx_data, tx_len, status, retry); + continue; + } + + // Success, exit the retry loop + break; + } + + return status; +} + +EXPORT_SYMBOL(ipmi_send_message); + +static int __init ipmi_module_init(void) +{ + printk(KERN_INFO "Accton IPMI Module loaded\n"); + return 0; +} + +static void __exit ipmi_module_exit(void) +{ + printk(KERN_INFO "Accton IPMI Module unloaded\n"); +} + +module_init(ipmi_module_init); +module_exit(ipmi_module_exit); + +MODULE_AUTHOR("Brandon Chuang "); +MODULE_DESCRIPTION("Accton IPMI messaging module"); +MODULE_LICENSE("GPL"); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/accton_ipmi_intf.h b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/accton_ipmi_intf.h new file mode 100644 index 000000000..4b2fe59f9 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/accton_ipmi_intf.h @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 +/* + * Copyright 2024 Accton Technology Corporation. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * + * Description: + * IPMI driver related interface declarations + */ + +#ifndef ACCTON_IPMI_INTF_H +#define ACCTON_IPMI_INTF_H + +#include +#include + +/* Structure to hold IPMI (Intelligent Platform Management Interface) data */ +struct ipmi_data { + struct completion read_complete; // Synchronization primitive for signaling message read completion + struct ipmi_addr address; // Structure to store the IPMI system interface address + struct ipmi_user *user; // Pointer to IPMI user created by the kernel + int interface; // Interface identifier for the IPMI system + + struct kernel_ipmi_msg tx_message; // Message structure for sending IPMI commands + long tx_msgid; // Message ID for tracking IPMI message transactions + + void *rx_msg_data; // Pointer to buffer for storing received IPMI message data + unsigned short rx_msg_len; // Length of the received IPMI message + unsigned char rx_result; // Result code from the received IPMI message + int rx_recv_type; // Type of the received message (e.g., system interface, LAN, etc.) + + struct ipmi_user_hndl ipmi_hndlrs; // IPMI handler structure for handling incoming IPMI messages + struct device *dev; // Device structure for logging errors +}; + +/* Function declarations */ + +/* + * Initialize IPMI data structure and create a user interface for communication. + * + * @param ipmi: Pointer to ipmi_data structure to be initialized. + * @param iface: IPMI interface identifier. + * @param dev: Device structure for logging errors. + * @return 0 on success, or an error code on failure. + */ +extern int init_ipmi_data(struct ipmi_data *ipmi, int iface, struct device *dev); + +/* + * Send an IPMI command to the IPMI device and receive the response. + * + * @param ipmi: Pointer to ipmi_data structure containing IPMI communication information. + * @param cmd: IPMI command byte. + * @param tx_data: Pointer to data buffer for the command payload. + * @param tx_len: Length of the command payload data. + * @param rx_data: Pointer to buffer for storing the response data. + * @param rx_len: Length of the response data buffer. + * @return 0 on success, or an error code on failure. + */ +extern int ipmi_send_message(struct ipmi_data *ipmi, unsigned char cmd, + unsigned char *tx_data, unsigned short tx_len, + unsigned char *rx_data, unsigned short rx_len); + +#endif /* ACCTON_IPMI_INTF_H */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-cpld.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-cpld.c new file mode 100644 index 000000000..bb60b9e26 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-cpld.c @@ -0,0 +1,809 @@ +/* + * Copyright (C) Roger Ho + * + * This module provides support for accessing the Accton CPLD and + * retrieving the status of QSFP-DD/SFP port. + * This includes the: + * Accton as9737_32db FPGA/CPLD2/CPLD3 + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define I2C_RW_RETRY_COUNT (10) +#define I2C_RW_RETRY_INTERVAL (60) /* ms */ + +#define PORT_NUM (32 + 2) /* 32 QSFPDDs + 2 SFPs */ + +/* FPGA */ +#define FPGA_BOARD_INFO_REG (0x00) +#define FPGA_VERSION_REG (0x01) + +/* CPLD 2 */ +#define CPLD2_VERSION_REG (0x01) +#define XCVR_P8_P1_PRESENT_REG (0x12) +#define XCVR_P16_P9_PRESENT_REG (0x13) +#define XCVR_P8_P1_RESET_REG (0x14) +#define XCVR_P16_P9_RESET_REG (0x15) +#define XCVR_P8_P1_LPMODE_REG (0x60) +#define XCVR_P16_P9_LPMODE_REG (0x61) +#define XCVR_P8_P1_PWR_EN_REG (0X81) +#define XCVR_P16_P9_PWR_EN_REG (0X82) +#define XCVR_P8_P1_PWR_GOOD_REG (0X83) +#define XCVR_P16_P9_PWR_GOOD_REG (0X84) + +/* CPLD 3 */ +#define CPLD3_VERSION_REG (0x01) +#define XCVR_P24_P17_PRESENT_REG (0x12) +#define XCVR_P32_P25_PRESENT_REG (0x13) +#define XCVR_P24_P17_RESET_REG (0x14) +#define XCVR_P32_P25_RESET_REG (0x15) +#define SFP_PRESENT_REG (0x20) +#define SFP_TXDIS_REG (0x21) +#define SFP_RATE_SEL (0x20) +#define SFP_RXLOSS_REG (0x26) +#define SFP_TXFAULT_REG (0x27) +#define XCVR_P24_P17_LPMODE_REG (0x60) +#define XCVR_P32_P25_LPMODE_REG (0x61) +#define XCVR_P24_P17_PWR_EN_REG (0X81) +#define XCVR_P32_P25_PWR_EN_REG (0X82) +#define XCVR_P24_P17_PWR_GOOD_REG (0X83) +#define XCVR_P32_P25_PWR_GOOD_REG (0X84) + + +static LIST_HEAD(cpld_client_list); +static struct mutex list_lock; + +struct cpld_client_node { + struct i2c_client *client; + struct list_head list; +}; + +enum cpld_type { + as9737_32db_fpga, + as9737_32db_cpld2, + as9737_32db_cpld3 +}; + +struct as9737_32db_cpld_data { + enum cpld_type type; + u8 reg; + struct mutex update_lock; +}; + +static const struct i2c_device_id as9737_32db_cpld_id[] = { + { "as9737_32db_fpga", as9737_32db_fpga}, + { "as9737_32db_cpld2", as9737_32db_cpld2 }, + { "as9737_32db_cpld3", as9737_32db_cpld3 }, + { } +}; +MODULE_DEVICE_TABLE(i2c, as9737_32db_cpld_id); + +#define QDD_TRANSCEIVER_ATTR_ID(index) \ + MODULE_PRESENT_##index = (index - 1), \ + MODULE_LPMODE_##index = (index - 1) + (PORT_NUM * 1), \ + MODULE_RESET_##index = (index - 1) + (PORT_NUM * 2), \ + MODULE_PWR_GOOD_##index = (index - 1) + (PORT_NUM * 3), \ + MODULE_PWR_ENABLE_##index = (index - 1) + (PORT_NUM * 4) + +#define SFP_TRANSCEIVER_ATTR_ID(index) \ + MODULE_PRESENT_##index = (index - 1), \ + MODULE_TX_DISABLE_##index = (index - 1) + (PORT_NUM * 5), \ + MODULE_RX_LOS_##index = (index - 1) + (PORT_NUM * 6), \ + MODULE_TX_FAULT_##index = (index - 1) + (PORT_NUM * 7) + +/* + * MODULE_PRESENT_1 ... MODULE_PRESENT_34 => 0 ... 33 + * MODULE_LPMODE_1 ... MODULE_LPMODE_32 => 34 ... 65 + * MODULE_RESET_1 ... MODULE_RESET_32 => 68 ... 99 + * MODULE_PWR_GOOD_1 ... MODULE_PWR_GOOD_32 => 102 ... 133 + * MODULE_PWR_ENABLE_1 ... MODULE_PWR_ENABLE_32 => 136 ... 167 + * MODULE_TX_DISABLE_33 ... MODULE_TX_DISABLE_34 => 202 ... 203 + * MODULE_RX_LOS_33 ... MODULE_RX_LOS_34 => 236 ... 237 + * MODULE_TX_FAULT_33 ... MODULE_TX_FAULT_34 => 270 ... 271 + */ + +enum as9737_32db_cpld_sysfs_attributes { + /* transceiver attributes */ + QDD_TRANSCEIVER_ATTR_ID(1), + QDD_TRANSCEIVER_ATTR_ID(2), + QDD_TRANSCEIVER_ATTR_ID(3), + QDD_TRANSCEIVER_ATTR_ID(4), + QDD_TRANSCEIVER_ATTR_ID(5), + QDD_TRANSCEIVER_ATTR_ID(6), + QDD_TRANSCEIVER_ATTR_ID(7), + QDD_TRANSCEIVER_ATTR_ID(8), + QDD_TRANSCEIVER_ATTR_ID(9), + QDD_TRANSCEIVER_ATTR_ID(10), + QDD_TRANSCEIVER_ATTR_ID(11), + QDD_TRANSCEIVER_ATTR_ID(12), + QDD_TRANSCEIVER_ATTR_ID(13), + QDD_TRANSCEIVER_ATTR_ID(14), + QDD_TRANSCEIVER_ATTR_ID(15), + QDD_TRANSCEIVER_ATTR_ID(16), + QDD_TRANSCEIVER_ATTR_ID(17), + QDD_TRANSCEIVER_ATTR_ID(18), + QDD_TRANSCEIVER_ATTR_ID(19), + QDD_TRANSCEIVER_ATTR_ID(20), + QDD_TRANSCEIVER_ATTR_ID(21), + QDD_TRANSCEIVER_ATTR_ID(22), + QDD_TRANSCEIVER_ATTR_ID(23), + QDD_TRANSCEIVER_ATTR_ID(24), + QDD_TRANSCEIVER_ATTR_ID(25), + QDD_TRANSCEIVER_ATTR_ID(26), + QDD_TRANSCEIVER_ATTR_ID(27), + QDD_TRANSCEIVER_ATTR_ID(28), + QDD_TRANSCEIVER_ATTR_ID(29), + QDD_TRANSCEIVER_ATTR_ID(30), + QDD_TRANSCEIVER_ATTR_ID(31), + QDD_TRANSCEIVER_ATTR_ID(32), + SFP_TRANSCEIVER_ATTR_ID(33), + SFP_TRANSCEIVER_ATTR_ID(34), + BOARD_INFO, + VERSION, + ACCESS, +}; + +/* sysfs attributes for hwmon */ +static ssize_t show(struct device *dev, struct device_attribute *da, char *buf); +static ssize_t show_status(struct device *dev, struct device_attribute *da, + char *buf); +static ssize_t set_status(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t reg_read(struct device *dev, struct device_attribute *da, + char *buf); +static ssize_t reg_write(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static int as9737_32db_cpld_read_internal(struct i2c_client *client, u8 reg); +static int as9737_32db_cpld_write_internal(struct i2c_client *client, u8 reg, + u8 value); + +/* transceiver attributes */ +#define DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(module_present_##index, S_IRUGO, \ + show_status, NULL, MODULE_PRESENT_##index); \ + static SENSOR_DEVICE_ATTR(module_reset_##index, S_IRUGO | S_IWUSR, \ + show_status, set_status, MODULE_RESET_##index); \ + static SENSOR_DEVICE_ATTR(module_lpmode_##index, S_IRUGO | S_IWUSR, \ + show_status, set_status, MODULE_LPMODE_##index); \ + static SENSOR_DEVICE_ATTR(module_pwr_good_##index, S_IRUGO, \ + show_status, NULL, MODULE_PWR_GOOD_##index); \ + static SENSOR_DEVICE_ATTR(module_pwr_enable_##index, \ + S_IRUGO | S_IWUSR, show_status, set_status, \ + MODULE_PWR_ENABLE_##index) + +#define DECLARE_SFP_TRANSCEIVER_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(module_present_##index, S_IRUGO, \ + show_status, NULL, MODULE_PRESENT_##index); \ + static SENSOR_DEVICE_ATTR(module_tx_disable_##index, \ + S_IRUGO | S_IWUSR, show_status, set_status, \ + MODULE_TX_DISABLE_##index); \ + static SENSOR_DEVICE_ATTR(module_rx_los_##index, S_IRUGO, \ + show_status, NULL, MODULE_RX_LOS_##index); \ + static SENSOR_DEVICE_ATTR(module_tx_fault_##index, S_IRUGO, \ + show_status, NULL, MODULE_TX_FAULT_##index) + +/* declare transceiver attributes callback function */ +static SENSOR_DEVICE_ATTR(board_info, S_IRUGO, show, NULL, BOARD_INFO); +static SENSOR_DEVICE_ATTR(version, S_IRUGO, show, NULL, VERSION); +static SENSOR_DEVICE_ATTR(access, S_IRUGO | S_IWUSR, reg_read, reg_write, + ACCESS); + +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(1); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(2); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(3); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(4); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(5); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(6); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(7); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(8); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(9); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(10); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(11); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(12); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(13); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(14); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(15); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(16); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(17); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(18); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(19); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(20); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(21); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(22); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(23); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(24); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(25); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(26); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(27); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(28); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(29); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(30); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(31); +DECLARE_QDD_TRANSCEIVER_SENSOR_DEVICE_ATTR(32); +DECLARE_SFP_TRANSCEIVER_SENSOR_DEVICE_ATTR(33); +DECLARE_SFP_TRANSCEIVER_SENSOR_DEVICE_ATTR(34); + +#define DECLARE_QDD_TRANSCEIVER_ATTR(index) \ + &sensor_dev_attr_module_present_##index.dev_attr.attr, \ + &sensor_dev_attr_module_reset_##index.dev_attr.attr, \ + &sensor_dev_attr_module_lpmode_##index.dev_attr.attr, \ + &sensor_dev_attr_module_pwr_good_##index.dev_attr.attr, \ + &sensor_dev_attr_module_pwr_enable_##index.dev_attr.attr + +#define DECLARE_SFP_TRANSCEIVER_ATTR(index) \ + &sensor_dev_attr_module_present_##index.dev_attr.attr, \ + &sensor_dev_attr_module_tx_disable_##index.dev_attr.attr, \ + &sensor_dev_attr_module_rx_los_##index.dev_attr.attr, \ + &sensor_dev_attr_module_tx_fault_##index.dev_attr.attr + +static struct attribute *as9737_32db_cpld2_attributes[] = { + &sensor_dev_attr_version.dev_attr.attr, + &sensor_dev_attr_access.dev_attr.attr, + DECLARE_QDD_TRANSCEIVER_ATTR(1), + DECLARE_QDD_TRANSCEIVER_ATTR(2), + DECLARE_QDD_TRANSCEIVER_ATTR(3), + DECLARE_QDD_TRANSCEIVER_ATTR(4), + DECLARE_QDD_TRANSCEIVER_ATTR(5), + DECLARE_QDD_TRANSCEIVER_ATTR(6), + DECLARE_QDD_TRANSCEIVER_ATTR(7), + DECLARE_QDD_TRANSCEIVER_ATTR(8), + DECLARE_QDD_TRANSCEIVER_ATTR(9), + DECLARE_QDD_TRANSCEIVER_ATTR(10), + DECLARE_QDD_TRANSCEIVER_ATTR(11), + DECLARE_QDD_TRANSCEIVER_ATTR(12), + DECLARE_QDD_TRANSCEIVER_ATTR(13), + DECLARE_QDD_TRANSCEIVER_ATTR(14), + DECLARE_QDD_TRANSCEIVER_ATTR(15), + DECLARE_QDD_TRANSCEIVER_ATTR(16), + NULL +}; + +static const struct attribute_group as9737_32db_cpld2_group = { + .attrs = as9737_32db_cpld2_attributes, +}; + +static struct attribute *as9737_32db_cpld3_attributes[] = { + &sensor_dev_attr_version.dev_attr.attr, + &sensor_dev_attr_access.dev_attr.attr, + DECLARE_QDD_TRANSCEIVER_ATTR(17), + DECLARE_QDD_TRANSCEIVER_ATTR(18), + DECLARE_QDD_TRANSCEIVER_ATTR(19), + DECLARE_QDD_TRANSCEIVER_ATTR(20), + DECLARE_QDD_TRANSCEIVER_ATTR(21), + DECLARE_QDD_TRANSCEIVER_ATTR(22), + DECLARE_QDD_TRANSCEIVER_ATTR(23), + DECLARE_QDD_TRANSCEIVER_ATTR(24), + DECLARE_QDD_TRANSCEIVER_ATTR(25), + DECLARE_QDD_TRANSCEIVER_ATTR(26), + DECLARE_QDD_TRANSCEIVER_ATTR(27), + DECLARE_QDD_TRANSCEIVER_ATTR(28), + DECLARE_QDD_TRANSCEIVER_ATTR(29), + DECLARE_QDD_TRANSCEIVER_ATTR(30), + DECLARE_QDD_TRANSCEIVER_ATTR(31), + DECLARE_QDD_TRANSCEIVER_ATTR(32), + DECLARE_SFP_TRANSCEIVER_ATTR(33), + DECLARE_SFP_TRANSCEIVER_ATTR(34), + NULL +}; + +static const struct attribute_group as9737_32db_cpld3_group = { + .attrs = as9737_32db_cpld3_attributes, +}; + +static struct attribute *as9737_32db_fpga_attributes[] = { + &sensor_dev_attr_board_info.dev_attr.attr, + &sensor_dev_attr_version.dev_attr.attr, + &sensor_dev_attr_access.dev_attr.attr, + NULL +}; + +static const struct attribute_group as9737_32db_fpga_group = { + .attrs = as9737_32db_fpga_attributes, +}; + +struct attribute_mapping { + u16 attr_base; + u8 reg; + u8 revert; +}; + +static const struct attribute_mapping attribute_mappings[] = { + [MODULE_PRESENT_1 ... MODULE_PRESENT_8] = {MODULE_PRESENT_1, XCVR_P8_P1_PRESENT_REG, 1}, + [MODULE_PRESENT_9 ... MODULE_PRESENT_16] = {MODULE_PRESENT_9, XCVR_P16_P9_PRESENT_REG, 1}, + [MODULE_PRESENT_17 ... MODULE_PRESENT_24] = {MODULE_PRESENT_17, XCVR_P24_P17_PRESENT_REG, 1}, + [MODULE_PRESENT_25 ... MODULE_PRESENT_32] = {MODULE_PRESENT_25, XCVR_P32_P25_PRESENT_REG, 1}, + /* Bit 1:SFP P2, Bit 0: SFP P1 */ + [MODULE_PRESENT_33 ... MODULE_PRESENT_34] = {MODULE_PRESENT_33, SFP_PRESENT_REG, 1}, + + [MODULE_LPMODE_1 ... MODULE_LPMODE_8] = {MODULE_LPMODE_1, XCVR_P8_P1_LPMODE_REG, 0}, + [MODULE_LPMODE_9 ... MODULE_LPMODE_16] = {MODULE_LPMODE_9, XCVR_P16_P9_LPMODE_REG, 0}, + [MODULE_LPMODE_17 ... MODULE_LPMODE_24] = {MODULE_LPMODE_17, XCVR_P24_P17_LPMODE_REG, 0}, + [MODULE_LPMODE_25 ... MODULE_LPMODE_32] = {MODULE_LPMODE_25, XCVR_P32_P25_LPMODE_REG, 0}, + + [MODULE_RESET_1 ... MODULE_RESET_8] = {MODULE_RESET_1, XCVR_P8_P1_RESET_REG, 1}, + [MODULE_RESET_9 ... MODULE_RESET_16] = {MODULE_RESET_9, XCVR_P16_P9_RESET_REG, 1}, + [MODULE_RESET_17 ... MODULE_RESET_24] = {MODULE_RESET_17, XCVR_P24_P17_RESET_REG, 1}, + [MODULE_RESET_25 ... MODULE_RESET_32] = {MODULE_RESET_25, XCVR_P32_P25_RESET_REG, 1}, + + [MODULE_PWR_GOOD_1 ... MODULE_PWR_GOOD_8] = {MODULE_PWR_GOOD_1, XCVR_P8_P1_PWR_GOOD_REG, 0}, + [MODULE_PWR_GOOD_9 ... MODULE_PWR_GOOD_16] = {MODULE_PWR_GOOD_1, XCVR_P16_P9_PWR_GOOD_REG, 0}, + [MODULE_PWR_GOOD_17 ... MODULE_PWR_GOOD_24] = {MODULE_PWR_GOOD_1, XCVR_P24_P17_PWR_GOOD_REG, 0}, + [MODULE_PWR_GOOD_25 ... MODULE_PWR_GOOD_32] = {MODULE_PWR_GOOD_1, XCVR_P32_P25_PWR_GOOD_REG, 0}, + + [MODULE_PWR_ENABLE_1 ... MODULE_PWR_ENABLE_8] = {MODULE_PWR_ENABLE_1, XCVR_P8_P1_PWR_EN_REG, 0}, + [MODULE_PWR_ENABLE_9 ... MODULE_PWR_ENABLE_16] = {MODULE_PWR_ENABLE_1, XCVR_P16_P9_PWR_EN_REG, 0}, + [MODULE_PWR_ENABLE_17 ... MODULE_PWR_ENABLE_24] = {MODULE_PWR_ENABLE_1, XCVR_P24_P17_PWR_EN_REG, 0}, + [MODULE_PWR_ENABLE_25 ... MODULE_PWR_ENABLE_32] = {MODULE_PWR_ENABLE_1, XCVR_P32_P25_PWR_EN_REG, 0}, + + /* Bit 1:SFP P2, Bit 0: SFP P1 */ + [MODULE_TX_DISABLE_33 ... MODULE_TX_DISABLE_34] ={MODULE_TX_DISABLE_33, SFP_TXDIS_REG, 0}, + [MODULE_RX_LOS_33 ... MODULE_RX_LOS_34] = {MODULE_RX_LOS_33, SFP_RXLOSS_REG, 0}, + [MODULE_TX_FAULT_33 ... MODULE_TX_FAULT_34] = {MODULE_TX_FAULT_33, SFP_TXFAULT_REG, 0}, +}; + +static ssize_t show(struct device *dev, struct device_attribute *da, char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct as9737_32db_cpld_data *data = i2c_get_clientdata(client); + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + u8 reg = 0; + int val = 0; + + switch(attr->index) { + case VERSION: + switch (data->type) { + case as9737_32db_fpga: + reg = FPGA_VERSION_REG; + break; + case as9737_32db_cpld2: + reg = CPLD2_VERSION_REG; + break; + case as9737_32db_cpld3: + reg = CPLD3_VERSION_REG; + break; + default: + break; + } + break; + case BOARD_INFO: + reg = FPGA_BOARD_INFO_REG; + break; + default: + break; + } + + val = i2c_smbus_read_byte_data(client, reg); + + if (val < 0) + dev_dbg(&client->dev, "cpld(0x%02x) reg(0x%02x) err %d\n", + client->addr, reg, val); + + return sprintf(buf, "%02X\n", val); +} + +static ssize_t show_status(struct device *dev, struct device_attribute *da, char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct as9737_32db_cpld_data *data = i2c_get_clientdata(client); + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int status = 0; + u8 reg = 0, reg_val = 0, bits_shift = 0; + + switch(attr->index) { + case MODULE_PRESENT_1 ... MODULE_TX_FAULT_34: + reg = attribute_mappings[attr->index].reg; + mutex_lock(&data->update_lock); + status = as9737_32db_cpld_read_internal(client, reg); + if (unlikely(status < 0)) + goto exit; + + mutex_unlock(&data->update_lock); + reg_val = status; + + bits_shift = attr->index - + attribute_mappings[attr->index].attr_base; + + reg_val = (reg_val >> bits_shift) & 0x01; + if (attribute_mappings[attr->index].revert) + reg_val = !reg_val; + + status = sprintf(buf, "%d\n", reg_val); + break; + default: + break; +} + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static ssize_t set_status(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct as9737_32db_cpld_data *data = i2c_get_clientdata(client); + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int status = 0; + u8 input; + u8 reg = 0, mask = 0, should_set_bit = 0; + + status = kstrtou8(buf, 10, &input); + if (status) + return status; + + reg = attribute_mappings[attr->index].reg; + + mask = 0x01 << (attr->index - + attribute_mappings[attr->index].attr_base); + should_set_bit = attribute_mappings[attr->index].revert ? + !input : input; + + mutex_lock(&data->update_lock); + status = as9737_32db_cpld_read_internal(client, reg); + + if (unlikely(status < 0)) + goto exit; + + if (should_set_bit) + status |= mask; + else + status &= ~mask; + + status = as9737_32db_cpld_write_internal(client, reg, status); + + if (unlikely(status < 0)) + goto exit; + + mutex_unlock(&data->update_lock); + return count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static ssize_t reg_read(struct device *dev, struct device_attribute *da, char *buf) +{ + struct i2c_client *client = to_i2c_client(dev); + struct as9737_32db_cpld_data *data = i2c_get_clientdata(client); + int reg_val, status = 0; + + mutex_lock(&data->update_lock); + reg_val = as9737_32db_cpld_read_internal(client, data->reg); + + if (unlikely(reg_val < 0)) + goto exit; + + mutex_unlock(&data->update_lock); + + status = sprintf(buf, "0x%02x\n", reg_val); + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static ssize_t reg_write(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + struct i2c_client *client = to_i2c_client(dev); + struct as9737_32db_cpld_data *data = i2c_get_clientdata(client); + int args, status; + char *opt, tmp[32] = {0}; + char *tmp_p; + size_t copy_size; + u8 input[2] = {0}; + + copy_size = (count < sizeof(tmp)) ? count : sizeof(tmp) - 1; + #ifdef __STDC_LIB_EXT1__ + memcpy_s(tmp, copy_size, buf, copy_size); + #else + memcpy(tmp, buf, copy_size); + #endif + tmp[copy_size] = '\0'; + + args = 0; + tmp_p = tmp; + + while (args < 2 && (opt = strsep(&tmp_p, " ")) != NULL) { + if (kstrtou8(opt, 16, &input[args]) == 0) + args++; + } + + switch(args) { + case 2: + /* Write value to register */ + mutex_lock(&data->update_lock); + status = as9737_32db_cpld_write_internal(client, input[0], + input[1]); + if (unlikely(status < 0)) + goto exit; + + mutex_unlock(&data->update_lock); + break; + case 1: + /* Read value from register */ + data->reg = input[0]; + break; + default: + return -EINVAL; + } + + return count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static void as9737_32db_cpld_add_client(struct i2c_client *client) +{ + struct cpld_client_node *node = kzalloc(sizeof(struct cpld_client_node) + , GFP_KERNEL); + + if (!node) { + dev_dbg(&client->dev, "Can't allocate cpld_client_node (0x%x)\n", + client->addr); + return; + } + + node->client = client; + + mutex_lock(&list_lock); + list_add(&node->list, &cpld_client_list); + mutex_unlock(&list_lock); +} + +static void as9737_32db_cpld_remove_client(struct i2c_client *client) +{ + struct list_head *list_node = NULL; + struct cpld_client_node *cpld_node = NULL; + int found = 0; + + mutex_lock(&list_lock); + + list_for_each(list_node, &cpld_client_list) + { + cpld_node = list_entry(list_node, struct cpld_client_node, + list); + + if (cpld_node->client == client) { + found = 1; + break; + } + } + + if (found) { + list_del(list_node); + kfree(cpld_node); + } + + mutex_unlock(&list_lock); +} + +/* + * I2C init/probing/exit functions + */ +static int as9737_32db_cpld_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); + struct as9737_32db_cpld_data *data; + int ret = -ENODEV; + const struct attribute_group *group = NULL; + + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) + goto exit; + + data = kzalloc(sizeof(struct as9737_32db_cpld_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto exit; + } + + i2c_set_clientdata(client, data); + mutex_init(&data->update_lock); + data->type = id->driver_data; + + /* Register sysfs hooks */ + switch (data->type) { + case as9737_32db_fpga: + data->reg = FPGA_VERSION_REG; + group = &as9737_32db_fpga_group; + break; + case as9737_32db_cpld2: + data->reg = CPLD2_VERSION_REG; + group = &as9737_32db_cpld2_group; + break; + case as9737_32db_cpld3: + data->reg = CPLD3_VERSION_REG; + group = &as9737_32db_cpld3_group; + break; + default: + break; + } + + if (group) { + ret = sysfs_create_group(&client->dev.kobj, group); + if (ret) + goto exit_free; + } + + as9737_32db_cpld_add_client(client); + return 0; + +exit_free: + kfree(data); +exit: + return ret; +} + +static void as9737_32db_cpld_remove(struct i2c_client *client) +{ + struct as9737_32db_cpld_data *data = i2c_get_clientdata(client); + const struct attribute_group *group = NULL; + + as9737_32db_cpld_remove_client(client); + + /* Remove sysfs hooks */ + switch (data->type) { + case as9737_32db_fpga: + group = &as9737_32db_fpga_group; + break; + case as9737_32db_cpld2: + group = &as9737_32db_cpld2_group; + break; + case as9737_32db_cpld3: + group = &as9737_32db_cpld3_group; + break; + default: + break; + } + + if (group) + sysfs_remove_group(&client->dev.kobj, group); + + kfree(data); +} + +static int as9737_32db_cpld_read_internal(struct i2c_client *client, u8 reg) +{ + int status = 0, retry = I2C_RW_RETRY_COUNT; + + while (retry) { + status = i2c_smbus_read_byte_data(client, reg); + if (unlikely(status < 0)) { + msleep(I2C_RW_RETRY_INTERVAL); + retry--; + continue; + } + + break; + } + + return status; +} + +static int as9737_32db_cpld_write_internal(struct i2c_client *client, u8 reg, u8 value) +{ + int status = 0, retry = I2C_RW_RETRY_COUNT; + + while (retry) { + status = i2c_smbus_write_byte_data(client, reg, value); + if (unlikely(status < 0)) { + msleep(I2C_RW_RETRY_INTERVAL); + retry--; + continue; + } + + break; + } + + return status; +} + +int as9737_32db_cpld_read(unsigned short cpld_addr, u8 reg) +{ + struct list_head *list_node = NULL; + struct cpld_client_node *cpld_node = NULL; + int ret = -EPERM; + + mutex_lock(&list_lock); + + list_for_each(list_node, &cpld_client_list) + { + cpld_node = list_entry(list_node, struct cpld_client_node, + list); + + if (cpld_node->client->addr == cpld_addr) { + ret = as9737_32db_cpld_read_internal( + cpld_node->client, reg); + break; + } + } + + mutex_unlock(&list_lock); + + return ret; +} +EXPORT_SYMBOL(as9737_32db_cpld_read); + +int as9737_32db_cpld_write(unsigned short cpld_addr, u8 reg, u8 value) +{ + struct list_head *list_node = NULL; + struct cpld_client_node *cpld_node = NULL; + int ret = -EIO; + + mutex_lock(&list_lock); + + list_for_each(list_node, &cpld_client_list) + { + cpld_node = list_entry(list_node, struct cpld_client_node, + list); + + if (cpld_node->client->addr == cpld_addr) { + ret = as9737_32db_cpld_write_internal( + cpld_node->client, reg, value); + break; + } + } + + mutex_unlock(&list_lock); + + return ret; +} +EXPORT_SYMBOL(as9737_32db_cpld_write); + +static struct i2c_driver as9737_32db_cpld_driver = { + .driver = { + .name = "as9737_32db_cpld", + .owner = THIS_MODULE, + }, + .probe = as9737_32db_cpld_probe, + .remove = as9737_32db_cpld_remove, + .id_table = as9737_32db_cpld_id, +}; + +static int __init as9737_32db_cpld_init(void) +{ + mutex_init(&list_lock); + return i2c_add_driver(&as9737_32db_cpld_driver); +} + +static void __exit as9737_32db_cpld_exit(void) +{ + i2c_del_driver(&as9737_32db_cpld_driver); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("AS9373-32DB CPLD driver"); +MODULE_LICENSE("GPL"); + +module_init(as9737_32db_cpld_init); +module_exit(as9737_32db_cpld_exit); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-fan.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-fan.c new file mode 100644 index 000000000..704e625e9 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-fan.c @@ -0,0 +1,657 @@ +/* + * Copyright (C) Roger Ho + * + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9737_32db_fan" +#define IPMI_FAN_READ_CMD 0x14 +#define IPMI_FAN_WRITE_CMD 0x15 +#define IPMI_FAN_READ_RPM_CMD 0x20 +#define IPMI_FAN_REG_READ_CMD 0x20 +#define IPMI_FAN_REG_WRITE_CMD 0x21 +#define MAX_FAN_SPEED_RPM 33000 + +static ssize_t set_fan(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t show_fan(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_version(struct device *dev, struct device_attribute *da, + char *buf); +static ssize_t show_dir(struct device *dev, struct device_attribute *da, + char *buf); +static ssize_t show_threshold(struct device *dev, struct device_attribute *da, + char *buf); +static int as9737_32db_fan_probe(struct platform_device *pdev); +static int as9737_32db_fan_remove(struct platform_device *pdev); + +enum fan_id { + FAN_1, + FAN_2, + FAN_3, + FAN_4, + FAN_5, + FAN_6, + FAN_7, + FAN_8, + FAN_9, + FAN_10, + FAN_11, + FAN_12, + NUM_OF_FAN, + NUM_OF_FAN_MODULE = NUM_OF_FAN +}; + +enum fan_data_index { + FAN_PRESENT, + FAN_PWM, + FAN_SPEED0, + FAN_SPEED1, + FAN_DATA_COUNT, + + FAN_TARGET_SPEED0 = 0, + FAN_TARGET_SPEED1, + FAN_SPEED_TOLERANCE, + FAN_SPEED_DATA_COUNT +}; + +struct as9737_32db_fan_data { + struct platform_device *pdev; + struct device *hwmon_dev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + /* 4 bytes for each fan, the last 2 bytes is fan dir */ + unsigned char ipmi_resp[NUM_OF_FAN * FAN_DATA_COUNT + 2]; + unsigned char ipmi_resp_cpld[2]; + unsigned char ipmi_resp_speed[NUM_OF_FAN * FAN_SPEED_DATA_COUNT]; + struct ipmi_data ipmi; + unsigned char ipmi_tx_data[3]; /* 0: FAN id, 1: 0x02, 2: PWM */ +}; + +struct as9737_32db_fan_data *data = NULL; + +static struct platform_driver as9737_32db_fan_driver = { + .probe = as9737_32db_fan_probe, + .remove = as9737_32db_fan_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +#define FAN_PRESENT_ATTR_ID(index) FAN##index##_PRESENT +#define FAN_PWM_ATTR_ID(index) FAN##index##_PWM +#define FAN_RPM_ATTR_ID(index) FAN##index##_INPUT +#define FAN_DIR_ATTR_ID(index) FAN##index##_DIR +#define FAN_FAULT_ATTR_ID(index) FAN##index##_FAULT +#define FAN_RPM_TARGET_ATTR_ID(index) FAN##index##_TARGET +#define FAN_RPM_TOLERANCE_ATTR_ID(index) FAN##index##_TOLERANCE + +#define FAN_ATTR(fan_id) \ + FAN_PRESENT_ATTR_ID(fan_id), \ + FAN_PWM_ATTR_ID(fan_id), \ + FAN_RPM_ATTR_ID(fan_id), \ + FAN_DIR_ATTR_ID(fan_id), \ + FAN_FAULT_ATTR_ID(fan_id) + +#define FAN_RPM_THRESHOLD_ATTR(fan_id) \ + FAN_RPM_TARGET_ATTR_ID(fan_id), \ + FAN_RPM_TOLERANCE_ATTR_ID(fan_id) + +enum as9737_32db_fan_sysfs_attrs { + FAN_ATTR(1), + FAN_ATTR(2), + FAN_ATTR(3), + FAN_ATTR(4), + FAN_ATTR(5), + FAN_ATTR(6), + FAN_ATTR(7), + FAN_ATTR(8), + FAN_ATTR(9), + FAN_ATTR(10), + FAN_ATTR(11), + FAN_ATTR(12), + NUM_OF_FAN_ATTR, + FAN_VERSION, + FAN_MAX_RPM, + NUM_OF_PER_FAN_ATTR = (NUM_OF_FAN_ATTR/NUM_OF_FAN), + FAN_RPM_THRESHOLD_ATTR(1), + FAN_RPM_THRESHOLD_ATTR(2), + FAN_RPM_THRESHOLD_ATTR(3), + FAN_RPM_THRESHOLD_ATTR(4), + FAN_RPM_THRESHOLD_ATTR(5), + FAN_RPM_THRESHOLD_ATTR(6), + FAN_RPM_THRESHOLD_ATTR(7), + FAN_RPM_THRESHOLD_ATTR(8), + FAN_RPM_THRESHOLD_ATTR(9), + FAN_RPM_THRESHOLD_ATTR(10), + FAN_RPM_THRESHOLD_ATTR(11), + FAN_RPM_THRESHOLD_ATTR(12), + NUM_OF_FAN_RPM_THRESHOLD_ATTR, + NUM_OF_PER_FAN_RPM_THRESHOLD_ATTR = ((NUM_OF_FAN_RPM_THRESHOLD_ATTR-NUM_OF_PER_FAN_ATTR)/NUM_OF_FAN), +}; + +/* fan attributes */ +#define DECLARE_FAN_VER_SENSOR_DEVICE_ATTR() \ + static SENSOR_DEVICE_ATTR(version, S_IRUGO, show_version, NULL, FAN_VERSION) +#define DECLARE_FAN_VER_ATTR() \ + &sensor_dev_attr_version.dev_attr.attr + +#define DECLARE_FAN_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(fan##index##_present, S_IRUGO, show_fan, NULL, \ + FAN##index##_PRESENT); \ + static SENSOR_DEVICE_ATTR(fan##index##_pwm, S_IWUSR | S_IRUGO, show_fan, \ + set_fan, FAN##index##_PWM); \ + static SENSOR_DEVICE_ATTR(fan##index##_input, S_IRUGO, show_fan, NULL, \ + FAN##index##_INPUT); \ + static SENSOR_DEVICE_ATTR(fan##index##_dir, S_IRUGO, show_dir, NULL, \ + FAN##index##_DIR); \ + static SENSOR_DEVICE_ATTR(fan##index##_fault, S_IRUGO, show_fan, NULL, \ + FAN##index##_FAULT); \ + static SENSOR_DEVICE_ATTR(fan##index##_target, S_IRUGO, show_threshold, \ + NULL, FAN##index##_TARGET); \ + static SENSOR_DEVICE_ATTR(fan##index##_tolerance, S_IRUGO, show_threshold,\ + NULL, FAN##index##_TOLERANCE) + +static SENSOR_DEVICE_ATTR(fan_max_speed_rpm, S_IRUGO, show_fan, NULL, \ + FAN_MAX_RPM); +#define DECLARE_FAN_MAX_RPM_ATTR(index) \ + &sensor_dev_attr_fan_max_speed_rpm.dev_attr.attr + +#define DECLARE_FAN_ATTR(index) \ + &sensor_dev_attr_fan##index##_present.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_pwm.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_input.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_dir.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_fault.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_target.dev_attr.attr, \ + &sensor_dev_attr_fan##index##_tolerance.dev_attr.attr + +DECLARE_FAN_SENSOR_DEVICE_ATTR(1); +DECLARE_FAN_SENSOR_DEVICE_ATTR(2); +DECLARE_FAN_SENSOR_DEVICE_ATTR(3); +DECLARE_FAN_SENSOR_DEVICE_ATTR(4); +DECLARE_FAN_SENSOR_DEVICE_ATTR(5); +DECLARE_FAN_SENSOR_DEVICE_ATTR(6); +DECLARE_FAN_SENSOR_DEVICE_ATTR(7); +DECLARE_FAN_SENSOR_DEVICE_ATTR(8); +DECLARE_FAN_SENSOR_DEVICE_ATTR(9); +DECLARE_FAN_SENSOR_DEVICE_ATTR(10); +DECLARE_FAN_SENSOR_DEVICE_ATTR(11); +DECLARE_FAN_SENSOR_DEVICE_ATTR(12); +DECLARE_FAN_VER_SENSOR_DEVICE_ATTR(); + +static struct attribute *as9737_32db_fan_attrs[] = { + /* fan attributes */ + DECLARE_FAN_ATTR(1), + DECLARE_FAN_ATTR(2), + DECLARE_FAN_ATTR(3), + DECLARE_FAN_ATTR(4), + DECLARE_FAN_ATTR(5), + DECLARE_FAN_ATTR(6), + DECLARE_FAN_ATTR(7), + DECLARE_FAN_ATTR(8), + DECLARE_FAN_ATTR(9), + DECLARE_FAN_ATTR(10), + DECLARE_FAN_ATTR(11), + DECLARE_FAN_ATTR(12), + DECLARE_FAN_VER_ATTR(), + DECLARE_FAN_MAX_RPM_ATTR(), + NULL +}; +ATTRIBUTE_GROUPS(as9737_32db_fan); + +static struct as9737_32db_fan_data *as9737_32db_fan_update_device(void) +{ + int status = 0; + + if (time_before(jiffies, data->last_updated + HZ * 5) && data->valid) + return data; + + data->valid = 0; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_READ_CMD, NULL, 0, + data->ipmi_resp, sizeof(data->ipmi_resp)); + + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + + data->ipmi_tx_data[0] = IPMI_FAN_READ_RPM_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp_speed, + sizeof(data->ipmi_resp_speed)); + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show_fan(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = attr->index / NUM_OF_PER_FAN_ATTR; + int value = 0; + int index = 0; + int present = 0; + int error = 0; + + if (attr->index == FAN_MAX_RPM) + return sprintf(buf, "%d\n", MAX_FAN_SPEED_RPM); + + mutex_lock(&data->update_lock); + + data = as9737_32db_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + index = fid * FAN_DATA_COUNT; /* base index */ + present = !!data->ipmi_resp[index + FAN_PRESENT]; + + switch (attr->index) { + case FAN1_PRESENT: + case FAN2_PRESENT: + case FAN3_PRESENT: + case FAN4_PRESENT: + case FAN5_PRESENT: + case FAN6_PRESENT: + case FAN7_PRESENT: + case FAN8_PRESENT: + case FAN9_PRESENT: + case FAN10_PRESENT: + case FAN11_PRESENT: + case FAN12_PRESENT: + value = present; + break; + case FAN1_PWM: + case FAN2_PWM: + case FAN3_PWM: + case FAN4_PWM: + case FAN5_PWM: + case FAN6_PWM: + case FAN7_PWM: + case FAN8_PWM: + case FAN9_PWM: + case FAN10_PWM: + case FAN11_PWM: + case FAN12_PWM: + index = (fid % NUM_OF_FAN_MODULE) * FAN_DATA_COUNT; + value = DIV_ROUND_CLOSEST(data->ipmi_resp[index + FAN_PWM] * 100, 255); + break; + case FAN1_INPUT: + case FAN2_INPUT: + case FAN3_INPUT: + case FAN4_INPUT: + case FAN5_INPUT: + case FAN6_INPUT: + case FAN7_INPUT: + case FAN8_INPUT: + case FAN9_INPUT: + case FAN10_INPUT: + case FAN11_INPUT: + case FAN12_INPUT: + value = (int)data->ipmi_resp[index + FAN_SPEED0] | + (int)data->ipmi_resp[index + FAN_SPEED1] << 8; + break; + case FAN1_FAULT: + case FAN2_FAULT: + case FAN3_FAULT: + case FAN4_FAULT: + case FAN5_FAULT: + case FAN6_FAULT: + case FAN7_FAULT: + case FAN8_FAULT: + case FAN9_FAULT: + case FAN10_FAULT: + case FAN11_FAULT: + case FAN12_FAULT: + value = (int)data->ipmi_resp[index + FAN_SPEED0] | + (int)data->ipmi_resp[index + FAN_SPEED1] << 8; + value = !value; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t set_fan(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long pwm; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = attr->index / NUM_OF_PER_FAN_ATTR; + + status = kstrtol(buf, 10, &pwm); + if (status) + return status; + + pwm = DIV_ROUND_CLOSEST(pwm * 255, 100); /* Convert pwm to register value */ + + mutex_lock(&data->update_lock); + + /* + * Send IPMI write command : + * BMC supports a design with four fan modules, each containing two fans. + * Since NUM_OF_FAN_MODULE is 12 in AS9737-32DB, it needs to be divided by 2. + * The result : + * fan1_pwm(Front) : ipmi_tx_data[0] = 1, fan2_pwm(Front) : ipmi_tx_data[0] = 2 + * fan3_pwm(Front) : ipmi_tx_data[0] = 3, fan4_pwm(Front) : ipmi_tx_data[0] = 4 + * fan5_pwm(Rear) : ipmi_tx_data[0] = 1, fan6_pwm(Rear) : ipmi_tx_data[0] = 2 + * fan7_pwm(Rear) : ipmi_tx_data[0] = 3, fan8_pwm(Rear) : ipmi_tx_data[0] = 4 + * fan9_pwm(Rear) : ipmi_tx_data[0] = 1, fan10_pwm(Rear) : ipmi_tx_data[0] = 2 + * fan11_pwm(Rear) : ipmi_tx_data[0] = 3, fan12_pwm(Rear) : ipmi_tx_data[0] = 4 + * + */ + data->ipmi_tx_data[0] = (fid % (NUM_OF_FAN_MODULE / 2)) + 1; + data->ipmi_tx_data[1] = 0x02; + data->ipmi_tx_data[2] = pwm; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_WRITE_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), + NULL, 0); + + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Update pwm to ipmi_resp buffer to prevent from the impact of lazy update */ + data->ipmi_resp[fid * FAN_DATA_COUNT + FAN_PWM] = pwm; + status = count; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static struct as9737_32db_fan_data *as9737_32db_fan_update_cpld_ver(void) +{ + int status = 0; + + data->valid = 0; + data->ipmi_tx_data[0] = 0x66; + status = ipmi_send_message(&data->ipmi, IPMI_FAN_REG_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp_cpld, + sizeof(data->ipmi_resp_cpld)); + + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show_version(struct device *dev, struct device_attribute *da, + char *buf) +{ + unsigned char major; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9737_32db_fan_update_cpld_ver(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + major = data->ipmi_resp_cpld[0]; + mutex_unlock(&data->update_lock); + return sprintf(buf, "%02X\n", major); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_dir(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid = (attr->index / NUM_OF_PER_FAN_ATTR); + int value = 0; + int index = 0; + int present = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9737_32db_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + index = fid * FAN_DATA_COUNT; /* base index */ + present = !!data->ipmi_resp[index + FAN_PRESENT]; + + value = data->ipmi_resp[NUM_OF_FAN * FAN_DATA_COUNT] | + (data->ipmi_resp[NUM_OF_FAN * FAN_DATA_COUNT + 1] << 8); + mutex_unlock(&data->update_lock); + + if (!present) + return sprintf(buf, "0\n"); + else + return sprintf(buf, "%s\n", + (value & BIT(fid % NUM_OF_FAN_MODULE)) ? "B2F" : "F2B"); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_threshold(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char fid; + int index = 0; + int value = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9737_32db_fan_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + switch (attr->index) { + case FAN1_TARGET: + case FAN2_TARGET: + case FAN3_TARGET: + case FAN4_TARGET: + case FAN5_TARGET: + case FAN6_TARGET: + case FAN7_TARGET: + case FAN8_TARGET: + case FAN9_TARGET: + case FAN10_TARGET: + case FAN11_TARGET: + case FAN12_TARGET: + fid = (attr->index - FAN1_TARGET) / NUM_OF_PER_FAN_RPM_THRESHOLD_ATTR; + index = fid * FAN_SPEED_DATA_COUNT; + value = (int)data->ipmi_resp_speed[index + FAN_TARGET_SPEED0] | + (int)data->ipmi_resp_speed[index + FAN_TARGET_SPEED1] << 8; + break; + case FAN1_TOLERANCE: + case FAN2_TOLERANCE: + case FAN3_TOLERANCE: + case FAN4_TOLERANCE: + case FAN5_TOLERANCE: + case FAN6_TOLERANCE: + case FAN7_TOLERANCE: + case FAN8_TOLERANCE: + case FAN9_TOLERANCE: + case FAN10_TOLERANCE: + case FAN11_TOLERANCE: + case FAN12_TOLERANCE: + fid = (attr->index - FAN1_TOLERANCE) / NUM_OF_PER_FAN_RPM_THRESHOLD_ATTR; + index = fid * FAN_SPEED_DATA_COUNT; + value = (int)data->ipmi_resp_speed[index + FAN_SPEED_TOLERANCE]; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static int as9737_32db_fan_probe(struct platform_device *pdev) +{ + int status = 0; + struct device *hwmon_dev; + + hwmon_dev = hwmon_device_register_with_groups(&pdev->dev, DRVNAME, + NULL, as9737_32db_fan_groups); + if (IS_ERR(data->hwmon_dev)) { + status = PTR_ERR(data->hwmon_dev); + return status; + } + + mutex_lock(&data->update_lock); + data->hwmon_dev = hwmon_dev; + mutex_unlock(&data->update_lock); + + dev_info(&pdev->dev, "Device Created\n"); + + return status; +} + +static int as9737_32db_fan_remove(struct platform_device *pdev) +{ + mutex_lock(&data->update_lock); + if (data->hwmon_dev) { + hwmon_device_unregister(data->hwmon_dev); + data->hwmon_dev = NULL; + } + mutex_unlock(&data->update_lock); + + return 0; +} + +static int __init as9737_32db_fan_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9737_32db_fan_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9737_32db_fan_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) { + goto ipmi_err; + } + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9737_32db_fan_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9737_32db_fan_exit(void) +{ + if (data) { + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + kfree(data); + } + platform_driver_unregister(&as9737_32db_fan_driver); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9737_32db_fan driver"); +MODULE_LICENSE("GPL"); + +module_init(as9737_32db_fan_init); +module_exit(as9737_32db_fan_exit); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-fpga.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-fpga.c new file mode 100644 index 000000000..277a96cb4 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-fpga.c @@ -0,0 +1,367 @@ +/* + * Copyright (C) Roger Ho + * + * This module provides support for accessing + * QSFP-DD/SFP eeprom by I2C. + * + * This includes the: + * Accton as9737_32db FPGA I2C + * + * Copyright (C) 2017 Finisar Corp. + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include + +/*********************************************** + * variable define + * *********************************************/ +#define DRVNAME "as9737_32db_fpga_i2c" +#define OCORES_I2C_DRVNAME "ocores-as9737" + +#define PORT_NUM (32 + 2) /* 32 QSFPDDs + 1 SFP+ + 1 SFP */ + +/* + * PCIE BAR0 address + */ +#define BAR0_NUM 0 +#define FPGA_PCI_VENDOR_ID 0x1172 +#define FPGA_PCI_DEVICE_ID 0x0004 + +#define CPLD2 0x01 +#define CPLD3 0x02 + +/*********************************************** + * macro define + * *********************************************/ +#define pcie_err(fmt, args...) \ + printk(KERN_ERR "["DRVNAME"]: " fmt " ", ##args) + +#define pcie_info(fmt, args...) \ + printk(KERN_ERR "["DRVNAME"]: " fmt " ", ##args) + +#define DEFINE_RES_REG_NAMED(_start, _size, _name) \ + DEFINE_RES_NAMED((_start), (_size), (_name), IORESOURCE_REG) + +/*********************************************** + * structure & variable declare + * *********************************************/ +typedef struct pci_fpga_device_s { + struct pci_dev *pci_dev; + struct platform_device *fpga_i2c[PORT_NUM]; +} pci_fpga_device_t; + +static struct platform_device *pdev = NULL; + +/*********************************************** + * enum define + * *********************************************/ + + +/*********************************************** + * function declare + * *********************************************/ + +#if 0 +static inline unsigned int fpga_read(const void __iomem *addr) +{ + return ioread8(addr); +} + +static inline void fpga_write(void __iomem *addr, u8 val) +{ + iowrite8(val, addr); +} +#endif + +struct port_data { + u16 offset; + u16 channel; + u16 cpld; /* 0x01 --> CPLD2, 0x02 --> CPLD3 */ +}; + +/* ============PCIe Bar Offset to I2C Master Mapping============== */ +#ifdef HAS_CHANNEL_REG +static const struct port_data port[PORT_NUM]= { + {0x00, 0x00, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port1 */ + {0x00, 0x01, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port2 */ + {0x00, 0x02, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port3 */ + {0x00, 0x03, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port4 */ + {0x00, 0x04, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port5 */ + {0x00, 0x05, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port6 */ + {0x00, 0x06, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port7 */ + {0x00, 0x07, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port8 */ + {0x00, 0x08, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port9 */ + {0x00, 0x09, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port10 */ + {0x00, 0x0a, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port11 */ + {0x00, 0x0b, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port12 */ + {0x00, 0x0c, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port13 */ + {0x00, 0x0d, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port14 */ + {0x00, 0x0e, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port15 */ + {0x00, 0x0f, CPLD2},/* 0x00 - 0x05 CPLD2 I2C Master Port16 */ + {0x00, 0x10, CPLD3},/* 0x00 - 0x05 CPLD2 I2C Master Port17 */ + {0x00, 0x11, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port18 */ + {0x00, 0x12, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port19 */ + {0x00, 0x13, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port20 */ + {0x00, 0x14, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port21 */ + {0x00, 0x15, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port22 */ + {0x00, 0x16, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port23 */ + {0x00, 0x17, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port24 */ + {0x00, 0x18, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port25 */ + {0x00, 0x19, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port26 */ + {0x00, 0x1a, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port27 */ + {0x00, 0x1b, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port28 */ + {0x00, 0x1c, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port29 */ + {0x00, 0x1d, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port30 */ + {0x00, 0x1e, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port31 */ + {0x00, 0x1f, CPLD3},/* 0x00 - 0x05 CPLD3 I2C Master Port32 */ + {0x00, 0x20, CPLD2},/* 0x00 - 0x05 CPLD3 I2C Master Port33 */ + {0x00, 0x21, CPLD2},/* 0x00 - 0x05 CPLD3 I2C Master Port34 */ +}; +#else +static const struct port_data port[PORT_NUM]= { + {0x0000, 0x00, CPLD2},/* 0x0000 - 0x0010 CPLD2 I2C Master Port1 */ + {0x0014, 0x00, CPLD2},/* 0x0014 - 0x0024 CPLD2 I2C Master Port2 */ + {0x0028, 0x00, CPLD2},/* 0x0028 - 0x0038 CPLD2 I2C Master Port3 */ + {0x003C, 0x00, CPLD2},/* 0x003c - 0x004c CPLD2 I2C Master Port4 */ + {0x0050, 0x00, CPLD2},/* 0x0050 - 0x0060 CPLD2 I2C Master Port5 */ + {0x0064, 0x00, CPLD2},/* 0x0064 - 0x0074 CPLD2 I2C Master Port6 */ + {0x0078, 0x00, CPLD2},/* 0x0078 - 0x0088 CPLD2 I2C Master Port7 */ + {0x008C, 0x00, CPLD2},/* 0x008c - 0x009c CPLD2 I2C Master Port8 */ + {0x00A0, 0x00, CPLD2},/* 0x00a0 - 0x00b0 CPLD2 I2C Master Port9 */ + {0x00B4, 0x00, CPLD2},/* 0x00b4 - 0x00c4 CPLD2 I2C Master Port10*/ + {0x00C8, 0x00, CPLD2},/* 0x00c8 - 0x00d8 CPLD2 I2C Master Port11 */ + {0x00DC, 0x00, CPLD2},/* 0x00dc - 0x00ec CPLD2 I2C Master Port12 */ + {0x00F0, 0x00, CPLD2},/* 0x00f0 - 0x0100 CPLD2 I2C Master Port13 */ + {0x0104, 0x00, CPLD2},/* 0x0104 - 0x0114 CPLD2 I2C Master Port14 */ + {0x0118, 0x00, CPLD2},/* 0x0118 - 0x0128 CPLD2 I2C Master Port15 */ + {0x012C, 0x00, CPLD2},/* 0x012c - 0x013c CPLD2 I2C Master Port16 */ + {0x0140, 0x00, CPLD3},/* 0x0140 - 0x0150 CPLD2 I2C Master Port17 */ + {0x0154, 0x00, CPLD3},/* 0x0154 - 0x0164 CPLD3 I2C Master Port18 */ + {0x0168, 0x00, CPLD3},/* 0x0168 - 0x0178 CPLD3 I2C Master Port19 */ + {0x017C, 0x00, CPLD3},/* 0x017c - 0x018c CPLD3 I2C Master Port20 */ + {0x0190, 0x00, CPLD3},/* 0x0190 - 0x01a0 CPLD3 I2C Master Port21 */ + {0x01A4, 0x00, CPLD3},/* 0x01a4 - 0x01b4 CPLD3 I2C Master Port22 */ + {0x01B8, 0x00, CPLD3},/* 0x01b8 - 0x01c8 CPLD3 I2C Master Port23 */ + {0x01CC, 0x00, CPLD3},/* 0x01cc - 0x01dc CPLD3 I2C Master Port24 */ + {0x01E0, 0x00, CPLD3},/* 0x01e0 - 0x01f0 CPLD3 I2C Master Port25 */ + {0x01F4, 0x00, CPLD3},/* 0x01f4 - 0x0204 CPLD3 I2C Master Port26 */ + {0x0208, 0x00, CPLD3},/* 0x0208 - 0x0218 CPLD3 I2C Master Port27 */ + {0x021C, 0x00, CPLD3},/* 0x021c - 0x022c CPLD3 I2C Master Port28 */ + {0x0230, 0x00, CPLD3},/* 0x0230 - 0x0240 CPLD3 I2C Master Port29 */ + {0x0244, 0x00, CPLD3},/* 0x0244 - 0x0254 CPLD3 I2C Master Port30 */ + {0x0258, 0x00, CPLD3},/* 0x0258 - 0x0268 CPLD3 I2C Master Port31 */ + {0x026C, 0x00, CPLD3},/* 0x026c - 0x027c CPLD3 I2C Master Port32 */ + {0x0280, 0x00, CPLD2},/* 0x0280 - 0x0290 CPLD3 I2C Master Port33 */ + {0x0294, 0x00, CPLD2},/* 0x0294 - 0x02a4 CPLD3 I2C Master Port34 */ +}; +#endif + +static struct ocores_i2c_platform_data as9737_32db_platform_data = { + .reg_io_width = 1, + .reg_shift = 2, + /* + * PRER_L and PRER_H are calculated based on clock_khz and bus_khz + * in i2c-ocores.c:ocores_init. + */ +#if 1 + /* SCL 100KHZ in FPGA spec. => PRER_L = 0x6D, PRER_H = 0x00 */ + .clock_khz = 55000, + .bus_khz = 100, +#else + /* SCL 400KHZ in FPGA spec. => PRER_L = 0x6D, PRER_H = 0x00 */ + .clock_khz = 220000, + .bus_khz = 400, +#endif +}; + +struct platform_device *ocore_i2c_device_add(unsigned int id, unsigned long bar_base, + const struct port_data port) +{ + struct platform_device *pdev; + int err; + struct resource res[] = { + DEFINE_RES_MEM_NAMED(bar_base + port.offset, 0x14, "mem_map"), +#ifdef HAS_CHANNEL_REG + DEFINE_RES_REG_NAMED(port.channel, 0x01, "channel"), +#endif + }; + + pdev = platform_device_alloc(OCORES_I2C_DRVNAME, id); + if (!pdev) { + err = -ENOMEM; + pcie_err("Port%u device allocation failed (%d)\n", + (id & 0xFF) + 1, err); + goto exit; + } + + err = platform_device_add_resources(pdev, res, ARRAY_SIZE(res)); + if (err) { + pcie_err("Port%u device resource addition failed (%d)\n", + (id & 0xFF) + 1, err); + goto exit_device_put; + } + + err = platform_device_add_data(pdev, &as9737_32db_platform_data, + sizeof(struct ocores_i2c_platform_data)); + if (err) { + pcie_err("Port%u platform data allocation failed (%d)\n", + (id & 0xFF) + 1, err); + goto exit_device_put; + } + + err = platform_device_add(pdev); + if (err) { + pcie_err("Port%u device addition failed (%d)\n", + (id & 0xFF) + 1, err); + goto exit_device_put; + } + + return pdev; + +exit_device_put: + platform_device_put(pdev); +exit: + return NULL; +} + +static int as9737_32db_pcie_fpga_i2c_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + pci_fpga_device_t *fpga_dev; + struct pci_dev *pcidev; + int i; + int status = 0, err = 0; + unsigned long bar_base; + + fpga_dev = devm_kzalloc(dev, sizeof(pci_fpga_device_t), GFP_KERNEL); + if (!fpga_dev) { + return -ENOMEM; + } + platform_set_drvdata(pdev, fpga_dev); + + pcidev = pci_get_device(FPGA_PCI_VENDOR_ID, FPGA_PCI_DEVICE_ID, NULL); + if (!pcidev) { + dev_err(dev, "Cannot found PCI device(%x:%x)\n", + FPGA_PCI_VENDOR_ID, FPGA_PCI_DEVICE_ID); + return -ENODEV; + } + fpga_dev->pci_dev = pcidev; + + err = pci_enable_device(pcidev); + if (err != 0) { + dev_err(dev, "Cannot enable PCI device(%x:%x)\n", + FPGA_PCI_VENDOR_ID, FPGA_PCI_DEVICE_ID); + status = -ENODEV; + goto exit_pci_disable; + } + /* enable PCI bus-mastering */ + pci_set_master(pcidev); + + /* Create I2C ocore devices first, then create the FPGA sysfs. + * To prevent the application from accessing an ocore device + * that has not been fully created due to the port status + * being present. + */ + + /* + * Create ocore_i2c device for QSFP-DD EEPROM + */ + bar_base = pci_resource_start(pcidev, BAR0_NUM); + for (i = 0; i < PORT_NUM; i++) { + fpga_dev->fpga_i2c[i] = + ocore_i2c_device_add(i + 1, bar_base, port[i]); + if (IS_ERR(fpga_dev->fpga_i2c[i])) { + status = PTR_ERR(fpga_dev->fpga_i2c[i]); + dev_err(dev, "rc:%d, unload Port%u[0x%ux] device\n", + status, i + 1, port[i].offset); + goto exit_ocores_device; + } + } + + return 0; + +exit_ocores_device: + while (i > 0) { + i--; + platform_device_unregister(fpga_dev->fpga_i2c[i]); + } + +exit_pci_disable: + pci_disable_device(fpga_dev->pci_dev); + + return status; +} + +static int as9737_32db_pcie_fpga_i2c_remove(struct platform_device *pdev) +{ + pci_fpga_device_t *fpga_dev = platform_get_drvdata(pdev); + + if (pci_is_enabled(fpga_dev->pci_dev)) { + int i; + + /* Unregister ocore_i2c device */ + for (i = 0; i < PORT_NUM; i++) + platform_device_unregister(fpga_dev->fpga_i2c[i]); + + pci_disable_device(fpga_dev->pci_dev); + } + + return 0; +} + +static struct platform_driver pcie_fpga_i2c_driver = { + .probe = as9737_32db_pcie_fpga_i2c_probe, + .remove = as9737_32db_pcie_fpga_i2c_remove, + .driver = { + .owner = THIS_MODULE, + .name = DRVNAME, + }, +}; + +static int __init as9737_32db_pcie_fpga_i2c_init(void) +{ + int status = 0; + + /* + * Create FPGA I2C platform driver and device + */ + status = platform_driver_register(&pcie_fpga_i2c_driver); + if (status < 0) + return status; + + pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(pdev)) { + status = PTR_ERR(pdev); + goto exit_pci; + } + + return status; + +exit_pci: + platform_driver_unregister(&pcie_fpga_i2c_driver); + + return status; +} + +static void __exit as9737_32db_pcie_fpga_i2c_exit(void) +{ + platform_device_unregister(pdev); + platform_driver_unregister(&pcie_fpga_i2c_driver); +} + + +module_init(as9737_32db_pcie_fpga_i2c_init); +module_exit(as9737_32db_pcie_fpga_i2c_exit); + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("AS9737-32DB FPGA I2C driver"); +MODULE_LICENSE("GPL"); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-i2c-ocores.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-i2c-ocores.c new file mode 100644 index 000000000..3147c4197 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-i2c-ocores.c @@ -0,0 +1,987 @@ + +// SPDX-License-Identifier: GPL-2.0 +/* + * i2c-ocores.c: I2C bus driver for OpenCores I2C controller + * (https://opencores.org/project/i2c/overview) + * + * Peter Korsgaard + * + * Support for the GRLIB port of the controller by + * Andreas Larsson + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * 'process_lock' exists because ocores_process() and ocores_process_timeout() + * can't run in parallel. + */ +struct ocores_i2c { + void __iomem *base; + int iobase; + u32 reg_shift; + u32 reg_io_width; + unsigned long flags; + wait_queue_head_t wait; + struct i2c_adapter adap; + struct i2c_msg *msg; + int pos; + int nmsgs; + int state; /* see STATE_ */ + spinlock_t process_lock; + struct clk *clk; + int ip_clock_khz; + int bus_clock_khz; +#ifdef HAS_CHANNEL_REG + u8 channel; +#endif + void (*setreg)(struct ocores_i2c *i2c, int reg, u8 value); + u8 (*getreg)(struct ocores_i2c *i2c, int reg); +}; + +/* registers */ +#define OCI2C_PRELOW 0 +#define OCI2C_PREHIGH 1 +#define OCI2C_CONTROL 2 +#define OCI2C_DATA 3 +#define OCI2C_CMD 4 /* write only */ +#define OCI2C_STATUS 4 /* read only, same address as OCI2C_CMD */ +#ifdef HAS_CHANNEL_REG +#define OCI2C_CHANNEL 5 /* Assign port num; Only for AS9737-32DB FPGA */ +#endif + +#define OCI2C_CTRL_IEN 0x40 +#define OCI2C_CTRL_EN 0x80 + +#define OCI2C_CMD_START 0x91 +#define OCI2C_CMD_STOP 0x41 +#define OCI2C_CMD_READ 0x21 +#define OCI2C_CMD_WRITE 0x11 +#define OCI2C_CMD_READ_ACK 0x21 +#define OCI2C_CMD_READ_NACK 0x29 +#define OCI2C_CMD_IACK 0x01 + +#define OCI2C_STAT_IF 0x01 +#define OCI2C_STAT_TIP 0x02 +#define OCI2C_STAT_ARBLOST 0x20 +#define OCI2C_STAT_BUSY 0x40 +#define OCI2C_STAT_NACK 0x80 + +#define STATE_DONE 0 +#define STATE_START 1 +#define STATE_WRITE 2 +#define STATE_READ 3 +#define STATE_ERROR 4 + +#define TYPE_OCORES 0 +#define TYPE_GRLIB 1 +#define TYPE_SIFIVE_REV0 2 + +#define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */ + +static unsigned int timeout = 1; +module_param(timeout , uint, S_IRUGO|S_IWUSR); +MODULE_PARM_DESC(timeout, "Timeout for ocores_poll_wait, measured in milliseconds."); + +static unsigned int debug = 1; +module_param(debug , uint, S_IRUGO|S_IWUSR); +MODULE_PARM_DESC(debug, "Enable or disable debug message. 1 -> enable, 0 -> disable"); + +#ifdef HAS_CHANNEL_REG +spinlock_t cpld_access_lock; + +#define LOCK(lock) \ +do { \ + spin_lock(lock); \ +} while (0) + +#define UNLOCK(lock) \ +do { \ + spin_unlock(lock); \ +} while (0) +#endif + +static void oc_setreg_8(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite8(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_16(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite16(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_32(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite32(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_16be(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite16be(value, i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_32be(struct ocores_i2c *i2c, int reg, u8 value) +{ + iowrite32be(value, i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_8(struct ocores_i2c *i2c, int reg) +{ + return ioread8(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_16(struct ocores_i2c *i2c, int reg) +{ + return ioread16(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_32(struct ocores_i2c *i2c, int reg) +{ + return ioread32(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_16be(struct ocores_i2c *i2c, int reg) +{ + return ioread16be(i2c->base + (reg << i2c->reg_shift)); +} + +static inline u8 oc_getreg_32be(struct ocores_i2c *i2c, int reg) +{ + return ioread32be(i2c->base + (reg << i2c->reg_shift)); +} + +static void oc_setreg_io_8(struct ocores_i2c *i2c, int reg, u8 value) +{ + outb(value, i2c->iobase + reg); +} + +static inline u8 oc_getreg_io_8(struct ocores_i2c *i2c, int reg) +{ + return inb(i2c->iobase + reg); +} + +static inline void oc_setreg(struct ocores_i2c *i2c, int reg, u8 value) +{ + i2c->setreg(i2c, reg, value); +} + +static inline u8 oc_getreg(struct ocores_i2c *i2c, int reg) +{ + return i2c->getreg(i2c, reg); +} + +static void ocores_process(struct ocores_i2c *i2c, u8 stat) +{ + struct i2c_msg *msg = i2c->msg; + unsigned long flags; + struct device *dev = i2c->adap.dev.parent; + char *cmdline = NULL; + + if (debug) { + cmdline = kstrdup_quotable_cmdline(current, GFP_KERNEL); + } + /* + * If we spin here is because we are in timeout, so we are going + * to be in STATE_ERROR. See ocores_process_timeout() + */ + spin_lock_irqsave(&i2c->process_lock, flags); + if ((i2c->state == STATE_DONE) || (i2c->state == STATE_ERROR)) { + /* stop has been sent */ + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); + wake_up(&i2c->wait); + goto out; + } + + /* error? */ + if (stat & OCI2C_STAT_ARBLOST) { + i2c->state = STATE_ERROR; + if (debug) + dev_warn(dev, "I2C %s arbitration lost", i2c->adap.name); + + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + goto out; + } + + if ((i2c->state == STATE_START) || (i2c->state == STATE_WRITE)) { + i2c->state = (msg->flags & I2C_M_RD) ? STATE_READ : STATE_WRITE; + + if (stat & OCI2C_STAT_NACK) { + i2c->state = STATE_ERROR; + if (debug) + dev_warn(dev, + "I2C %s, no ACK from slave 0x%02x (%s)", + i2c->adap.name, + msg->addr, cmdline ? cmdline : ""); + + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + goto out; + } + } else { + msg->buf[i2c->pos++] = oc_getreg(i2c, OCI2C_DATA); + } + + /* end of msg? */ + if (i2c->pos == msg->len) { + i2c->nmsgs--; + i2c->msg++; + i2c->pos = 0; + msg = i2c->msg; + + if (i2c->nmsgs) { /* end? */ + /* send start? */ + if (!(msg->flags & I2C_M_NOSTART)) { + u8 addr = i2c_8bit_addr_from_msg(msg); + + i2c->state = STATE_START; + + oc_setreg(i2c, OCI2C_DATA, addr); + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); + goto out; + } + i2c->state = (msg->flags & I2C_M_RD) + ? STATE_READ : STATE_WRITE; + } else { + i2c->state = STATE_DONE; + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + goto out; + } + } + + if (i2c->state == STATE_READ) { + oc_setreg(i2c, OCI2C_CMD, i2c->pos == (msg->len-1) ? + OCI2C_CMD_READ_NACK : OCI2C_CMD_READ_ACK); + } else { + oc_setreg(i2c, OCI2C_DATA, msg->buf[i2c->pos++]); + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_WRITE); + } + +out: + spin_unlock_irqrestore(&i2c->process_lock, flags); + if (cmdline) kfree(cmdline); + +} + +static irqreturn_t ocores_isr(int irq, void *dev_id) +{ + struct ocores_i2c *i2c = dev_id; + u8 stat = oc_getreg(i2c, OCI2C_STATUS); + + if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) { + if ((stat & OCI2C_STAT_IF) && !(stat & OCI2C_STAT_BUSY)) + return IRQ_NONE; + } else if (!(stat & OCI2C_STAT_IF)) { + return IRQ_NONE; + } + ocores_process(i2c, stat); + + return IRQ_HANDLED; +} + +/** + * Process timeout event + * @i2c: ocores I2C device instance + */ +static void ocores_process_timeout(struct ocores_i2c *i2c) +{ + unsigned long flags; + + spin_lock_irqsave(&i2c->process_lock, flags); + i2c->state = STATE_ERROR; + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_STOP); + spin_unlock_irqrestore(&i2c->process_lock, flags); +} + +/** + * Wait until something change in a given register + * @i2c: ocores I2C device instance + * @reg: register to query + * @mask: bitmask to apply on register value + * @val: expected result + * @timeout: timeout in jiffies + * + * Timeout is necessary to avoid to stay here forever when the chip + * does not answer correctly. + * + * Return: 0 on success, -ETIMEDOUT on timeout + */ +static int ocores_wait(struct ocores_i2c *i2c, + int reg, u8 mask, u8 val, + const unsigned long timeout) +{ + unsigned long j; + + j = jiffies + timeout; + while (1) { + u8 status = oc_getreg(i2c, reg); + + if ((status & mask) == val) + break; + + if (time_after(jiffies, j)) + return -ETIMEDOUT; + } + return 0; +} + +/** + * Wait until is possible to process some data + * @i2c: ocores I2C device instance + * + * Used when the device is in polling mode (interrupts disabled). + * + * Return: 0 on success, -ETIMEDOUT on timeout + */ +static int ocores_poll_wait(struct ocores_i2c *i2c) +{ + u8 mask; + int err; + + if (i2c->state == STATE_DONE || i2c->state == STATE_ERROR) { + /* transfer is over */ + mask = OCI2C_STAT_BUSY; + } else { + /* on going transfer */ + mask = OCI2C_STAT_TIP; + /* + * We wait for the data to be transferred (8bit), + * then we start polling on the ACK/NACK bit + */ + udelay((8 * 1000) / i2c->bus_clock_khz); + } + + /* + * once we are here we expect to get the expected result immediately + * so if after 1ms we timeout then something is broken. + */ + err = ocores_wait(i2c, OCI2C_STATUS, mask, 0, + msecs_to_jiffies(timeout)); + if (err) { + if (debug) { + dev_warn(i2c->adap.dev.parent, + "%s: STATUS timeout, bit 0x%x did not clear in %ums(msecs_to_jiffies(%u)=%lu)\n", + __func__, mask, timeout, timeout, + msecs_to_jiffies(timeout)); + } + } + return err; +} + +/** + * It handles an IRQ-less transfer + * @i2c: ocores I2C device instance + * + * Even if IRQ are disabled, the I2C OpenCore IP behavior is exactly the same + * (only that IRQ are not produced). This means that we can re-use entirely + * ocores_isr(), we just add our polling code around it. + * + * It can run in atomic context + * + * Return: 0 on success, -ETIMEDOUT on timeout + */ +static int ocores_process_polling(struct ocores_i2c *i2c) +{ + irqreturn_t ret; + int err; + + while (1) { + err = ocores_poll_wait(i2c); + if (err) + break; /* timeout */ + + ret = ocores_isr(-1, i2c); + if (ret == IRQ_NONE){ + break; /* all messages have been transferred */ + } else { + if (i2c->flags & OCORES_FLAG_BROKEN_IRQ) + if (i2c->state == STATE_DONE) + break; + } + } + + return err; +} + +static int ocores_xfer_core(struct ocores_i2c *i2c, + struct i2c_msg *msgs, int num, + bool polling) +{ + int ret = 0; + u8 ctrl; + +#ifdef HAS_CHANNEL_REG + LOCK(&cpld_access_lock); + + /* Only for AS9737-32DB FPGA */ + oc_setreg(i2c, OCI2C_CHANNEL, i2c->channel); +#endif + + ctrl = oc_getreg(i2c, OCI2C_CONTROL); + if (polling) + oc_setreg(i2c, OCI2C_CONTROL, ctrl & ~OCI2C_CTRL_IEN); + else + oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_IEN); + + i2c->msg = msgs; + i2c->pos = 0; + i2c->nmsgs = num; + i2c->state = STATE_START; + + oc_setreg(i2c, OCI2C_DATA, i2c_8bit_addr_from_msg(i2c->msg)); + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_START); + + if (polling) { + ret = ocores_process_polling(i2c); + } else { + if (wait_event_timeout(i2c->wait, + (i2c->state == STATE_ERROR) || + (i2c->state == STATE_DONE), HZ) == 0) + ret = -ETIMEDOUT; + } + if (ret) { + ocores_process_timeout(i2c); +#ifdef HAS_CHANNEL_REG + UNLOCK(&cpld_access_lock); +#endif + return ret; + } + +#ifdef HAS_CHANNEL_REG + UNLOCK(&cpld_access_lock); +#endif + return (i2c->state == STATE_DONE) ? num : -EIO; +} + +static int ocores_xfer_polling(struct i2c_adapter *adap, + struct i2c_msg *msgs, int num) +{ + return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, true); +} + +static int ocores_xfer(struct i2c_adapter *adap, + struct i2c_msg *msgs, int num) +{ + return ocores_xfer_core(i2c_get_adapdata(adap), msgs, num, false); +} + +static int ocores_init(struct device *dev, struct ocores_i2c *i2c) +{ + int prescale; + int diff; + u8 ctrl; + +#ifdef HAS_CHANNEL_REG + oc_setreg(i2c, OCI2C_CHANNEL, i2c->channel); +#endif + + ctrl = oc_getreg(i2c, OCI2C_CONTROL); + + /* make sure the device is disabled */ + ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); + oc_setreg(i2c, OCI2C_CONTROL, ctrl); + + prescale = (i2c->ip_clock_khz / (5 * i2c->bus_clock_khz)) - 1; + prescale = clamp(prescale, 0, 0xffff); + + diff = i2c->ip_clock_khz / (5 * (prescale + 1)) - i2c->bus_clock_khz; + if (abs(diff) > i2c->bus_clock_khz / 10) { + dev_err(dev, + "Unsupported clock settings: core: %d KHz, bus: %d KHz\n", + i2c->ip_clock_khz, i2c->bus_clock_khz); + return -EINVAL; + } + +#ifdef HAS_CHANNEL_REG + dev_info(dev, "OCI2C_CHANNEL=0x%02x OCI2C_PRELOW=0x%02x OCI2C_PREHIGH=0x%02x\n", + i2c->channel, prescale & 0xff, prescale >> 8); +#else + dev_info(dev, "OCI2C_PRELOW=0x%02x OCI2C_PREHIGH=0x%02x\n", + prescale & 0xff, prescale >> 8); +#endif + oc_setreg(i2c, OCI2C_PRELOW, prescale & 0xff); + oc_setreg(i2c, OCI2C_PREHIGH, prescale >> 8); + + /* Init the device */ + oc_setreg(i2c, OCI2C_CMD, OCI2C_CMD_IACK); + oc_setreg(i2c, OCI2C_CONTROL, ctrl | OCI2C_CTRL_EN); + + return 0; +} + + +static u32 ocores_func(struct i2c_adapter *adap) +{ + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; +} + +static struct i2c_algorithm ocores_algorithm = { + .master_xfer = ocores_xfer, + .master_xfer_atomic = ocores_xfer_polling, + .functionality = ocores_func, +}; + +static const struct i2c_adapter ocores_adapter = { + .owner = THIS_MODULE, + .name = "i2c-ocores", + .class = I2C_CLASS_DEPRECATED, + .algo = &ocores_algorithm, +}; + +static const struct of_device_id ocores_i2c_match[] = { + { + .compatible = "opencores,i2c-ocores", + .data = (void *)TYPE_OCORES, + }, + { + .compatible = "aeroflexgaisler,i2cmst", + .data = (void *)TYPE_GRLIB, + }, + { + .compatible = "sifive,fu540-c000-i2c", + .data = (void *)TYPE_SIFIVE_REV0, + }, + { + .compatible = "sifive,i2c0", + .data = (void *)TYPE_SIFIVE_REV0, + }, + {}, +}; +MODULE_DEVICE_TABLE(of, ocores_i2c_match); + +#ifdef CONFIG_OF +/* + * Read and write functions for the GRLIB port of the controller. Registers are + * 32-bit big endian and the PRELOW and PREHIGH registers are merged into one + * register. The subsequent registers have their offsets decreased accordingly. + */ +static u8 oc_getreg_grlib(struct ocores_i2c *i2c, int reg) +{ + u32 rd; + int rreg = reg; + + if (reg != OCI2C_PRELOW) + rreg--; + rd = ioread32be(i2c->base + (rreg << i2c->reg_shift)); + if (reg == OCI2C_PREHIGH) + return (u8)(rd >> 8); + else + return (u8)rd; +} + +static void oc_setreg_grlib(struct ocores_i2c *i2c, int reg, u8 value) +{ + u32 curr, wr; + int rreg = reg; + + if (reg != OCI2C_PRELOW) + rreg--; + if (reg == OCI2C_PRELOW || reg == OCI2C_PREHIGH) { + curr = ioread32be(i2c->base + (rreg << i2c->reg_shift)); + if (reg == OCI2C_PRELOW) + wr = (curr & 0xff00) | value; + else + wr = (((u32)value) << 8) | (curr & 0xff); + } else { + wr = value; + } + iowrite32be(wr, i2c->base + (rreg << i2c->reg_shift)); +} + +static int ocores_i2c_of_probe(struct platform_device *pdev, + struct ocores_i2c *i2c) +{ + struct device_node *np = pdev->dev.of_node; + const struct of_device_id *match; + u32 val; + u32 clock_frequency; + bool clock_frequency_present; + + if (of_property_read_u32(np, "reg-shift", &i2c->reg_shift)) { + /* no 'reg-shift', check for deprecated 'regstep' */ + if (!of_property_read_u32(np, "regstep", &val)) { + if (!is_power_of_2(val)) { + dev_err(&pdev->dev, "invalid regstep %d\n", + val); + return -EINVAL; + } + i2c->reg_shift = ilog2(val); + dev_warn(&pdev->dev, + "regstep property deprecated, use reg-shift\n"); + } + } + + clock_frequency_present = !of_property_read_u32(np, "clock-frequency", + &clock_frequency); + + i2c->bus_clock_khz = 100; + + i2c->clk = devm_clk_get(&pdev->dev, NULL); + + if (!IS_ERR(i2c->clk)) { + int ret = clk_prepare_enable(i2c->clk); + + if (ret) { + dev_err(&pdev->dev, + "clk_prepare_enable failed: %d\n", ret); + return ret; + } + i2c->ip_clock_khz = clk_get_rate(i2c->clk) / 1000; + if (clock_frequency_present) + i2c->bus_clock_khz = clock_frequency / 1000; + } + + if (i2c->ip_clock_khz == 0) { + if (of_property_read_u32(np, "opencores,ip-clock-frequency", + &val)) { + if (!clock_frequency_present) { + dev_err(&pdev->dev, + "Missing required parameter 'opencores,ip-clock-frequency'\n"); + clk_disable_unprepare(i2c->clk); + return -ENODEV; + } + i2c->ip_clock_khz = clock_frequency / 1000; + dev_warn(&pdev->dev, + "Deprecated usage of the 'clock-frequency' property, please update to 'opencores,ip-clock-frequency'\n"); + } else { + i2c->ip_clock_khz = val / 1000; + if (clock_frequency_present) + i2c->bus_clock_khz = clock_frequency / 1000; + } + } + + of_property_read_u32(pdev->dev.of_node, "reg-io-width", + &i2c->reg_io_width); + + match = of_match_node(ocores_i2c_match, pdev->dev.of_node); + if (match && (long)match->data == TYPE_GRLIB) { + dev_dbg(&pdev->dev, "GRLIB variant of i2c-ocores\n"); + i2c->setreg = oc_setreg_grlib; + i2c->getreg = oc_getreg_grlib; + } + + return 0; +} +#else +#define ocores_i2c_of_probe(pdev, i2c) -ENODEV +#endif + +#ifdef HAS_CHANNEL_REG +static void __iomem *devm_ioremap_shared_resource(struct device *dev, + const struct resource *res) +{ + resource_size_t size; + void __iomem *dest_ptr; + char *pretty_name; + + BUG_ON(!dev); + + if (!res || resource_type(res) != IORESOURCE_MEM) { + dev_err(dev, "invalid resource\n"); + return IOMEM_ERR_PTR(-EINVAL); + } + + size = resource_size(res); + + if (res->name) + pretty_name = devm_kasprintf(dev, GFP_KERNEL, "%s %s", dev_name(dev), res->name); + else + pretty_name = devm_kstrdup(dev, dev_name(dev), GFP_KERNEL); + if (!pretty_name) + return IOMEM_ERR_PTR(-ENOMEM); + + /*********************************************** + * Cannot use devm_request_mem_region in here. * + ***********************************************/ + + dest_ptr = devm_ioremap(dev, res->start, size); + if (!dest_ptr) { + dev_err(dev, "ioremap failed for resource %pR\n", res); + dest_ptr = IOMEM_ERR_PTR(-ENOMEM); + } + + return dest_ptr; +} +#endif + +static int ocores_i2c_probe(struct platform_device *pdev) +{ + struct ocores_i2c *i2c; + struct ocores_i2c_platform_data *pdata; + const struct of_device_id *match; + struct resource *res; + int irq; + int ret; + int i; + + i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL); + if (!i2c) + return -ENOMEM; + + spin_lock_init(&i2c->process_lock); + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + if (res) { +#ifdef HAS_CHANNEL_REG + i2c->base = devm_ioremap_shared_resource(&pdev->dev, res); +#else + i2c->base = devm_ioremap_resource(&pdev->dev, res); +#endif + dev_info(&pdev->dev, + "Resouce Memory: start:0x%llx, end:0x%llx", + res->start, res->end); + + if (IS_ERR(i2c->base)) + return PTR_ERR(i2c->base); + } else { + res = platform_get_resource(pdev, IORESOURCE_IO, 0); + if (!res) + return -EINVAL; + + i2c->iobase = res->start; + if (!devm_request_region(&pdev->dev, res->start, + resource_size(res), + pdev->name)) { + dev_err(&pdev->dev, "Can't get I/O resource.\n"); + return -EBUSY; + } + i2c->setreg = oc_setreg_io_8; + i2c->getreg = oc_getreg_io_8; + } + +#ifdef HAS_CHANNEL_REG + res = platform_get_resource_byname(pdev, IORESOURCE_REG, "channel"); + if (res) { + i2c->channel = res->start; + dev_info(&pdev->dev, "Resouce Channel: 0x%llx", res->start); + } else { + dev_err(&pdev->dev, "No Channel resource for port number identification.\n"); + return -EINVAL; + } +#endif + + pdata = dev_get_platdata(&pdev->dev); + if (pdata) { + i2c->reg_shift = pdata->reg_shift; + i2c->reg_io_width = pdata->reg_io_width; + i2c->ip_clock_khz = pdata->clock_khz; + dev_info(&pdev->dev, + "Write %d KHz, ioWidth:%d, shift:%d", + i2c->ip_clock_khz, + pdata->reg_io_width ,pdata->reg_shift); + if (pdata->bus_khz) + i2c->bus_clock_khz = pdata->bus_khz; + else + i2c->bus_clock_khz = 100; + } else { + ret = ocores_i2c_of_probe(pdev, i2c); + if (ret) + return ret; + } + + if (i2c->reg_io_width == 0) + i2c->reg_io_width = 1; /* Set to default value */ + + if (!i2c->setreg || !i2c->getreg) { + bool be = pdata ? pdata->big_endian : + of_device_is_big_endian(pdev->dev.of_node); + + switch (i2c->reg_io_width) { + case 1: + i2c->setreg = oc_setreg_8; + i2c->getreg = oc_getreg_8; + break; + + case 2: + i2c->setreg = be ? oc_setreg_16be : oc_setreg_16; + i2c->getreg = be ? oc_getreg_16be : oc_getreg_16; + break; + + case 4: + i2c->setreg = be ? oc_setreg_32be : oc_setreg_32; + i2c->getreg = be ? oc_getreg_32be : oc_getreg_32; + break; + + default: + dev_err(&pdev->dev, "Unsupported I/O width (%d)\n", + i2c->reg_io_width); + ret = -EINVAL; + goto err_clk; + } + } + + init_waitqueue_head(&i2c->wait); + + irq = platform_get_irq_optional(pdev, 0); + if (irq == -ENXIO) { + ocores_algorithm.master_xfer = ocores_xfer_polling; + + /* + * Set in OCORES_FLAG_BROKEN_IRQ to enable workaround for + * FU540-C000 SoC in polling mode. + */ + match = of_match_node(ocores_i2c_match, pdev->dev.of_node); + if (match && (long)match->data == TYPE_SIFIVE_REV0) + i2c->flags |= OCORES_FLAG_BROKEN_IRQ; + } else { + if (irq < 0) + return irq; + } + + if (ocores_algorithm.master_xfer != ocores_xfer_polling) { + ret = devm_request_any_context_irq(&pdev->dev, irq, + ocores_isr, 0, + pdev->name, i2c); + if (ret) { + dev_err(&pdev->dev, "Cannot claim IRQ\n"); + goto err_clk; + } + } + ret = ocores_init(&pdev->dev, i2c); + + if (ret) + goto err_clk; + + /* hook up driver to tree */ + platform_set_drvdata(pdev, i2c); + i2c->adap = ocores_adapter; + i2c_set_adapdata(&i2c->adap, i2c); + i2c->adap.dev.parent = &pdev->dev; + i2c->adap.dev.of_node = pdev->dev.of_node; + + /* add i2c adapter to i2c tree */ + ret = i2c_add_adapter(&i2c->adap); + + if (ret) + goto err_clk; + + /* add in known devices to the bus */ + if (pdata) { + for (i = 0; i < pdata->num_devices; i++) + i2c_new_client_device(&i2c->adap, pdata->devices + i); + } + + return 0; + +err_clk: + clk_disable_unprepare(i2c->clk); + return ret; +} + +static int ocores_i2c_remove(struct platform_device *pdev) +{ + struct ocores_i2c *i2c = platform_get_drvdata(pdev); + u8 ctrl; + + ctrl = oc_getreg(i2c, OCI2C_CONTROL); + + /* disable i2c logic */ + ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); + oc_setreg(i2c, OCI2C_CONTROL, ctrl); + + /* remove adapter & data */ + i2c_del_adapter(&i2c->adap); + + if (!IS_ERR(i2c->clk)) + clk_disable_unprepare(i2c->clk); + + return 0; +} + +#ifdef CONFIG_PM_SLEEP +static int ocores_i2c_suspend(struct device *dev) +{ + struct ocores_i2c *i2c = dev_get_drvdata(dev); + u8 ctrl = oc_getreg(i2c, OCI2C_CONTROL); + + /* make sure the device is disabled */ + ctrl &= ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); + oc_setreg(i2c, OCI2C_CONTROL, ctrl); + + if (!IS_ERR(i2c->clk)) + clk_disable_unprepare(i2c->clk); + return 0; +} + +static int ocores_i2c_resume(struct device *dev) +{ + struct ocores_i2c *i2c = dev_get_drvdata(dev); + + if (!IS_ERR(i2c->clk)) { + unsigned long rate; + int ret = clk_prepare_enable(i2c->clk); + + if (ret) { + dev_err(dev, + "clk_prepare_enable failed: %d\n", ret); + return ret; + } + rate = clk_get_rate(i2c->clk) / 1000; + if (rate) + i2c->ip_clock_khz = rate; + } + return ocores_init(dev, i2c); +} + +static SIMPLE_DEV_PM_OPS(ocores_i2c_pm, ocores_i2c_suspend, ocores_i2c_resume); +#define OCORES_I2C_PM (&ocores_i2c_pm) +#else +#define OCORES_I2C_PM NULL +#endif + +static struct platform_driver ocores_i2c_driver = { + .probe = ocores_i2c_probe, + .remove = ocores_i2c_remove, + .driver = { + .name = "ocores-as9737", + .of_match_table = ocores_i2c_match, + .pm = OCORES_I2C_PM, + }, +}; + +#ifdef HAS_CHANNEL_REG +static int __init ocores_i2c_as9817_64_init(void) +{ + int err; + + err = platform_driver_register(&ocores_i2c_driver); + if (err < 0) { + pr_err("Failed to register ocores_i2c_driver"); + return err; + } + + spin_lock_init(&cpld_access_lock); + + return 0; +} +static void __exit ocores_i2c_as9817_64_exit(void) +{ + platform_driver_unregister(&ocores_i2c_driver); +} + +module_init(ocores_i2c_as9817_64_init); +module_exit(ocores_i2c_as9817_64_exit); +#else +module_platform_driver(ocores_i2c_driver); +#endif + +MODULE_AUTHOR("Peter Korsgaard "); +MODULE_DESCRIPTION("OpenCores I2C bus driver"); +MODULE_LICENSE("GPL"); +MODULE_ALIAS("platform:ocores-as9737"); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-leds.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-leds.c new file mode 100644 index 000000000..387139e7e --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-leds.c @@ -0,0 +1,391 @@ +/* + * Copyright (C) Roger Ho + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9737_32db_led" +#define IPMI_LED_READ_CMD 0x1A +#define IPMI_LED_WRITE_CMD 0x1B + +static ssize_t set_led(struct device *dev, struct device_attribute *da, + const char *buf, size_t count); +static ssize_t show_led(struct device *dev, struct device_attribute *attr, + char *buf); +static int as9737_32db_led_probe(struct platform_device *pdev); +static int as9737_32db_led_remove(struct platform_device *pdev); + +struct as9737_32db_led_data { + struct platform_device *pdev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + unsigned char ipmi_resp[6]; /* 0:Loc 1:Diag 2:Gnss 3:Fan 4:Psu1 5:Psu2 */ + struct ipmi_data ipmi; +}; + +struct as9737_32db_led_data *data = NULL; + +static struct platform_driver as9737_32db_led_driver = { + .probe = as9737_32db_led_probe, + .remove = as9737_32db_led_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +enum ipmi_led_light_mode { + IPMI_LED_MODE_OFF, + IPMI_LED_MODE_RED = 2, + IPMI_LED_MODE_RED_BLINKING = 3, + IPMI_LED_MODE_GREEN = 4, + IPMI_LED_MODE_GREEN_BLINKING = 5, + IPMI_LED_MODE_YELLOW = 6, + IPMI_LED_MODE_YELLOW_BLINKING = 7, + IPMI_LED_MODE_BLUE = 8, + IPMI_LED_MODE_BLUE_BLINKING = 9, + IPMI_LED_MODE_CYAN = 0xC, + IPMI_LED_MODE_WHITE = 0xE, + IPMI_LED_MODE_AMBER = 0x10, + IPMI_LED_MODE_ORANGE = 0x20, +}; + +enum led_light_mode { + LED_MODE_OFF, + LED_MODE_RED = 10, + LED_MODE_RED_BLINKING = 11, + LED_MODE_ORANGE = 12, + LED_MODE_ORANGE_BLINKING = 13, + LED_MODE_YELLOW = 14, + LED_MODE_YELLOW_BLINKING = 15, + LED_MODE_GREEN = 16, + LED_MODE_GREEN_BLINKING = 17, + LED_MODE_BLUE = 18, + LED_MODE_BLUE_BLINKING = 19, + LED_MODE_PURPLE = 20, + LED_MODE_PURPLE_BLINKING = 21, + LED_MODE_AUTO = 22, + LED_MODE_AUTO_BLINKING = 23, + LED_MODE_WHITE = 24, + LED_MODE_WHITE_BLINKING = 25, + LED_MODE_CYAN = 26, + LED_MODE_CYAN_BLINKING = 27, + LED_MODE_UNKNOWN = 99 +}; + +enum as9737_32db_led_sysfs_attrs { + LED_LOC, + LED_DIAG, + LED_FAN, + LED_PSU1, + LED_PSU2 +}; + +static SENSOR_DEVICE_ATTR(led_loc, S_IWUSR | S_IRUGO, show_led, set_led, + LED_LOC); +static SENSOR_DEVICE_ATTR(led_diag, S_IWUSR | S_IRUGO, show_led, set_led, + LED_DIAG); +static SENSOR_DEVICE_ATTR(led_fan, S_IWUSR | S_IRUGO, show_led, set_led, + LED_FAN); +static SENSOR_DEVICE_ATTR(led_psu1, S_IWUSR | S_IRUGO, show_led, set_led, + LED_PSU1); +static SENSOR_DEVICE_ATTR(led_psu2, S_IWUSR | S_IRUGO, show_led, set_led, + LED_PSU2); + +static struct attribute *as9737_32db_led_attributes[] = { + &sensor_dev_attr_led_loc.dev_attr.attr, + &sensor_dev_attr_led_diag.dev_attr.attr, + &sensor_dev_attr_led_fan.dev_attr.attr, + &sensor_dev_attr_led_psu1.dev_attr.attr, + &sensor_dev_attr_led_psu2.dev_attr.attr, + NULL +}; + +static const struct attribute_group as9737_32db_led_group = { + .attrs = as9737_32db_led_attributes, +}; + +static struct as9737_32db_led_data *as9737_32db_led_update_device(void) +{ + int status = 0; + + if (time_before(jiffies, data->last_updated + HZ * 5) && data->valid) + return data; + + data->valid = 0; + status = ipmi_send_message(&data->ipmi, IPMI_LED_READ_CMD, NULL, 0, + data->ipmi_resp, sizeof(data->ipmi_resp)); + + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show_led(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int value = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9737_32db_led_update_device(); + if (!data->valid) { + error = -EIO; + goto exit; + } + + switch (data->ipmi_resp[attr->index]) { + case IPMI_LED_MODE_OFF: + value = LED_MODE_OFF; + break; + case IPMI_LED_MODE_RED: + value = LED_MODE_RED; + break; + case IPMI_LED_MODE_RED_BLINKING: + value = LED_MODE_RED_BLINKING; + break; + case IPMI_LED_MODE_GREEN: + value = LED_MODE_GREEN; + break; + case IPMI_LED_MODE_GREEN_BLINKING: + value = LED_MODE_GREEN_BLINKING; + break; + case IPMI_LED_MODE_BLUE: + value = LED_MODE_BLUE; + break; + case IPMI_LED_MODE_BLUE_BLINKING: + value = LED_MODE_BLUE_BLINKING; + break; + case IPMI_LED_MODE_CYAN: + value = LED_MODE_CYAN; + break; + case IPMI_LED_MODE_WHITE: + value = LED_MODE_WHITE; + break; + case IPMI_LED_MODE_AMBER: + value = IPMI_LED_MODE_AMBER; + break; + case IPMI_LED_MODE_ORANGE: + value = LED_MODE_ORANGE; + break; + case IPMI_LED_MODE_YELLOW: + value = LED_MODE_YELLOW; + break; + case IPMI_LED_MODE_YELLOW_BLINKING: + value = LED_MODE_YELLOW_BLINKING; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", value); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t set_led(struct device *dev, struct device_attribute *da, + const char *buf, size_t count) +{ + long mode; + int status; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + + status = kstrtol(buf, 10, &mode); + if (status) + return status; + + mutex_lock(&data->update_lock); + + data = as9737_32db_led_update_device(); + if (!data->valid) { + status = -EIO; + goto exit; + } + + data->ipmi_resp[0] = attr->index + 1; + + switch (mode) { + case LED_MODE_OFF: + data->ipmi_resp[1] = IPMI_LED_MODE_OFF; + break; + case LED_MODE_RED: + data->ipmi_resp[1] = IPMI_LED_MODE_RED; + break; + case LED_MODE_RED_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_RED_BLINKING; + break; + case LED_MODE_GREEN: + data->ipmi_resp[1] = IPMI_LED_MODE_GREEN; + break; + case LED_MODE_GREEN_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_GREEN_BLINKING; + break; + case LED_MODE_BLUE: + data->ipmi_resp[1] = IPMI_LED_MODE_BLUE; + break; + case LED_MODE_BLUE_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_BLUE_BLINKING; + break; + case LED_MODE_CYAN: + data->ipmi_resp[1] = IPMI_LED_MODE_CYAN; + break; + case LED_MODE_WHITE: + data->ipmi_resp[1] = IPMI_LED_MODE_WHITE; + break; + case LED_MODE_YELLOW: + data->ipmi_resp[1] = IPMI_LED_MODE_YELLOW; + break; + case LED_MODE_YELLOW_BLINKING: + data->ipmi_resp[1] = IPMI_LED_MODE_YELLOW_BLINKING; + break; + case LED_MODE_ORANGE: + data->ipmi_resp[1] = IPMI_LED_MODE_ORANGE; + break; + default: + status = -EINVAL; + goto exit; + } + + /* Send IPMI write command */ + status = ipmi_send_message(&data->ipmi, IPMI_LED_WRITE_CMD, + data->ipmi_resp, 2, NULL, 0); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + status = count; + data->valid = 0; + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static int as9737_32db_led_probe(struct platform_device *pdev) +{ + int status = -1; + + /* Register sysfs hooks */ + status = sysfs_create_group(&pdev->dev.kobj, &as9737_32db_led_group); + if (status) + goto exit; + + dev_info(&pdev->dev, "device created\n"); + + return 0; + +exit: + return status; +} + +static int as9737_32db_led_remove(struct platform_device *pdev) +{ + sysfs_remove_group(&pdev->dev.kobj, &as9737_32db_led_group); + + return 0; +} + +static int __init as9737_32db_led_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9737_32db_led_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + data->valid = 0; + + ret = platform_driver_register(&as9737_32db_led_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9737_32db_led_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9737_32db_led_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9737_32db_led_driver); + kfree(data); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9737_32db_led driver"); +MODULE_LICENSE("GPL"); + +module_init(as9737_32db_led_init); +module_exit(as9737_32db_led_exit); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-mux.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-mux.c new file mode 100644 index 000000000..45771aca5 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-mux.c @@ -0,0 +1,199 @@ +/* + * Copyright (C) Brandon Chuang + * + * This module supports the accton cpld that hold the channel select + * mechanism for other i2c slave devices, such as SFP. + * This includes the: + * Accton as456x CPLD1/CPLD2/CPLD3 + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DRVNAME "as9737_32db_mux" + +#define I2C_RW_RETRY_COUNT 10 +#define I2C_RW_RETRY_INTERVAL 60 /* ms */ + +#define AS9737_MUX_NCHANS 7 +#define AS9737_MUX_SELECT_REG 0x0 +#define AS9737_MUX_DESELECT_VAL 0x3 + +enum mux_type { + as9737_32db_mux +}; + +struct chip_desc { + u8 nchans; + u8 select_reg; + u8 deselect_val; +}; + +struct as9737_32db_mux_data { + enum mux_type type; + struct mutex update_lock; + struct i2c_client *client; +}; + +/* Provide specs for the as456x CPLD types we know about */ +static const struct chip_desc chips[] = { + [as9737_32db_mux] = { + .nchans = AS9737_MUX_NCHANS, + .select_reg = AS9737_MUX_SELECT_REG, + .deselect_val = AS9737_MUX_DESELECT_VAL + } +}; + +static const struct i2c_device_id as9737_32db_mux_id[] = { + { "as9737_32db_mux", as9737_32db_mux }, + { } +}; +MODULE_DEVICE_TABLE(i2c, as9737_32db_mux_id); + +static const struct of_device_id as9737_32db_mux_of_match[] = { + { .compatible = "edgecore,as9737_32db_mux", .data = &chips[as9737_32db_mux] }, + {} +}; +MODULE_DEVICE_TABLE(of, as9737_32db_mux_of_match); + +/* Write to mux register. Don't use i2c_transfer()/i2c_smbus_xfer() + for this as they will try to lock adapter a second time */ +static int as9737_32db_mux_write(struct i2c_adapter *adap, + struct i2c_client *client, u8 reg, u8 val) +{ + union i2c_smbus_data data; + + data.byte = val; + return __i2c_smbus_xfer(adap, client->addr, client->flags, + I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, + &data); +} + +static int as9737_32db_mux_select_chan(struct i2c_mux_core *muxc, u32 chan) +{ + struct as9737_32db_mux_data *data = i2c_mux_priv(muxc); + struct i2c_client *client = data->client; + int ret = 0; + + mutex_lock(&data->update_lock); + switch (data->type) { + case as9737_32db_mux: + ret = as9737_32db_mux_write(muxc->parent, client, + chips[data->type].select_reg, chan & 0x7); + break; + default: + break; + } + + mutex_unlock(&data->update_lock); + return ret; +} + +static int as9737_32db_mux_deselect_mux(struct i2c_mux_core *muxc, u32 chan) +{ + struct as9737_32db_mux_data *data = i2c_mux_priv(muxc); + struct i2c_client *client = data->client; + int ret = 0; + + mutex_lock(&data->update_lock); + ret = as9737_32db_mux_write(muxc->parent, client, + chips[data->type].select_reg, + chips[data->type].deselect_val); + mutex_unlock(&data->update_lock); + return ret; +} + +static void as9737_32db_mux_cleanup(struct i2c_mux_core *muxc) +{ + i2c_mux_del_adapters(muxc); +} + +/* + * I2C init/probing/exit functions + */ +static int as9737_32db_mux_probe(struct i2c_client *client, + const struct i2c_device_id *id) +{ + struct i2c_adapter *adap = to_i2c_adapter(client->dev.parent); + struct device *dev = &client->dev; + struct as9737_32db_mux_data *data; + struct i2c_mux_core *muxc; + int ret = -ENODEV; + int i = 0; + + if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_BYTE)) + return -ENODEV; + + muxc = i2c_mux_alloc(adap, dev, AS9737_MUX_NCHANS, sizeof(*data), 0, + as9737_32db_mux_select_chan, + as9737_32db_mux_deselect_mux); + if (!muxc) + return -ENOMEM; + + data = i2c_mux_priv(muxc); + mutex_init(&data->update_lock); + data->type = id->driver_data; + data->client = client; + i2c_set_clientdata(client, muxc); + + /* Now create an adapter for each channel */ + for (i = 0; i < chips[data->type].nchans; i++) { + ret = i2c_mux_add_adapter(muxc, 0, i, 0); + if (ret) + goto exit_mux; + } + + return 0; + +exit_mux: + as9737_32db_mux_cleanup(muxc); + return ret; +} + +static void as9737_32db_mux_remove(struct i2c_client *client) +{ + struct i2c_mux_core *muxc = i2c_get_clientdata(client); + as9737_32db_mux_cleanup(muxc); +} + +static struct i2c_driver as9737_32db_mux_driver = { + .driver = { + .name = "as9737_32db_mux", + .owner = THIS_MODULE, + }, + .probe = as9737_32db_mux_probe, + .remove = as9737_32db_mux_remove, + .id_table = as9737_32db_mux_id, +}; + +module_i2c_driver(as9737_32db_mux_driver); + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9737_32db_mux driver"); +MODULE_LICENSE("GPL"); + diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-psu.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-psu.c new file mode 100644 index 000000000..85d4ab361 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-psu.c @@ -0,0 +1,926 @@ +/* + * Copyright (C) Roger Ho + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9737_32db_psu" +#define IPMI_PSU_READ_CMD 0x16 +#define IPMI_PSU_MODEL_NAME_CMD 0x10 +#define IPMI_PSU_SERIAL_NUM_CMD 0x11 +#define IPMI_PSU_FAN_DIR_CMD 0x13 +#define IPMI_PSU_INFO_CMD 0x20 +#define IPMI_MODEL_SERIAL_LEN 32 +#define IPMI_FAN_DIR_LEN 3 + +static ssize_t show_psu(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_psu_info(struct device *dev, struct device_attribute *attr, + char *buf); +static ssize_t show_string(struct device *dev, struct device_attribute *attr, + char *buf); +static int as9737_32db_psu_probe(struct platform_device *pdev); +static int as9737_32db_psu_remove(struct platform_device *pdev); + +enum psu_id { + PSU_1, + PSU_2, + NUM_OF_PSU +}; + +enum psu_data_index { + PSU_PRESENT = 0, + PSU_TEMP_FAULT, + PSU_POWER_GOOD_CPLD, + PSU_POWER_GOOD_PMBUS, + PSU_OVER_VOLTAGE, + PSU_OVER_CURRENT, + PSU_POWER_ON, + PSU_VIN0, + PSU_VIN1, + PSU_VIN2, + PSU_VOUT0, + PSU_VOUT1, + PSU_VOUT2, + PSU_IIN0, + PSU_IIN1, + PSU_IIN2, + PSU_IOUT0, + PSU_IOUT1, + PSU_IOUT2, + PSU_PIN0, + PSU_PIN1, + PSU_PIN2, + PSU_PIN3, + PSU_POUT0, + PSU_POUT1, + PSU_POUT2, + PSU_POUT3, + PSU_TEMP1_0, + PSU_TEMP1_1, + PSU_TEMP2_0, + PSU_TEMP2_1, + PSU_TEMP3_0, + PSU_TEMP3_1, + PSU_FAN0, + PSU_FAN1, + PSU_VOUT_MODE, + PSU_STATUS_COUNT, + PSU_MODEL = 0, + PSU_SERIAL = 0, + PSU_TEMP1_MAX0 = 2, + PSU_TEMP1_MAX1, + PSU_TEMP1_MIN0, + PSU_TEMP1_MIN1, + PSU_TEMP2_MAX0, + PSU_TEMP2_MAX1, + PSU_TEMP2_MIN0, + PSU_TEMP2_MIN1, + PSU_TEMP3_MAX0, + PSU_TEMP3_MAX1, + PSU_TEMP3_MIN0, + PSU_TEMP3_MIN1, + PSU_VIN_UPPER_CRIT0, + PSU_VIN_UPPER_CRIT1, + PSU_VIN_UPPER_CRIT2, + PSU_VIN_MAX0, + PSU_VIN_MAX1, + PSU_VIN_MAX2, + PSU_VIN_MIN0, + PSU_VIN_MIN1, + PSU_VIN_MIN2, + PSU_VIN_LOWER_CRIT0, + PSU_VIN_LOWER_CRIT1, + PSU_VIN_LOWER_CRIT2, + PSU_VOUT_MAX0, + PSU_VOUT_MAX1, + PSU_VOUT_MAX2, + PSU_VOUT_MIN0, + PSU_VOUT_MIN1, + PSU_VOUT_MIN2, + PSU_IIN_MAX0, + PSU_IIN_MAX1, + PSU_IIN_MAX2, + PSU_IOUT_MAX0, + PSU_IOUT_MAX1, + PSU_IOUT_MAX2, + PSU_PIN_MAX0, + PSU_PIN_MAX1, + PSU_PIN_MAX2, + PSU_PIN_MAX3, + PSU_POUT_MAX0, + PSU_POUT_MAX1, + PSU_POUT_MAX2, + PSU_POUT_MAX3, + PSU_INFO_COUNT +}; + +struct ipmi_psu_resp_data { + unsigned char status[PSU_STATUS_COUNT]; + unsigned char info[PSU_INFO_COUNT]; + char serial[IPMI_MODEL_SERIAL_LEN+1]; + char model[IPMI_MODEL_SERIAL_LEN+1]; + char fandir[IPMI_FAN_DIR_LEN+1]; +}; + +struct as9737_32db_psu_data { + struct platform_device *pdev; + struct device *hwmon_dev[2]; + struct mutex update_lock; + char valid[2]; /* != 0 if registers are valid, 0: PSU1, 1: PSU2 */ + unsigned long last_updated[2]; /* In jiffies, 0: PSU1, 1: PSU2 */ + struct ipmi_data ipmi; + struct ipmi_psu_resp_data ipmi_resp[2]; /* 0: PSU1, 1: PSU2 */ + unsigned char ipmi_tx_data[2]; +}; + +struct as9737_32db_psu_data *data = NULL; + +static struct platform_driver as9737_32db_psu_driver = { + .probe = as9737_32db_psu_probe, + .remove = as9737_32db_psu_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +#define PSU_PRESENT_ATTR_ID(index) PSU##index##_PRESENT +#define PSU_POWERGOOD_ATTR_ID(index) PSU##index##_POWER_GOOD +#define PSU_VIN_ATTR_ID(index) PSU##index##_VIN +#define PSU_VOUT_ATTR_ID(index) PSU##index##_VOUT +#define PSU_IIN_ATTR_ID(index) PSU##index##_IIN +#define PSU_IOUT_ATTR_ID(index) PSU##index##_IOUT +#define PSU_PIN_ATTR_ID(index) PSU##index##_PIN +#define PSU_POUT_ATTR_ID(index) PSU##index##_POUT +#define PSU_MODEL_ATTR_ID(index) PSU##index##_MODEL +#define PSU_SERIAL_ATTR_ID(index) PSU##index##_SERIAL +#define PSU_TEMP1_INPUT_ATTR_ID(index) PSU##index##_TEMP1_INPUT +#define PSU_TEMP2_INPUT_ATTR_ID(index) PSU##index##_TEMP2_INPUT +#define PSU_TEMP3_INPUT_ATTR_ID(index) PSU##index##_TEMP3_INPUT +#define PSU_FAN_INPUT_ATTR_ID(index) PSU##index##_FAN_INPUT +#define PSU_FAN_DIR_ATTR_ID(index) PSU##index##_FAN_DIR + +#define PSU_TEMP1_INPUT_MAX_ATTR_ID(index) PSU##index##_TEMP1_INPUT_MAX +#define PSU_TEMP1_INPUT_MIN_ATTR_ID(index) PSU##index##_TEMP1_INPUT_MIN +#define PSU_TEMP2_INPUT_MAX_ATTR_ID(index) PSU##index##_TEMP2_INPUT_MAX +#define PSU_TEMP2_INPUT_MIN_ATTR_ID(index) PSU##index##_TEMP2_INPUT_MIN +#define PSU_TEMP3_INPUT_MAX_ATTR_ID(index) PSU##index##_TEMP3_INPUT_MAX +#define PSU_TEMP3_INPUT_MIN_ATTR_ID(index) PSU##index##_TEMP3_INPUT_MIN +#define PSU_VIN_MAX_ATTR_ID(index) PSU##index##_VIN_MAX +#define PSU_VIN_MIN_ATTR_ID(index) PSU##index##_VIN_MIN +#define PSU_VIN_UPPER_CRIT_ATTR_ID(index) PSU##index##_VIN_UPPER_CRIT +#define PSU_VIN_LOWER_CRIT_ATTR_ID(index) PSU##index##_VIN_LOWER_CRIT +#define PSU_VOUT_MAX_ATTR_ID(index) PSU##index##_VOUT_MAX +#define PSU_VOUT_MIN_ATTR_ID(index) PSU##index##_VOUT_MIN +#define PSU_IIN_MAX_ATTR_ID(index) PSU##index##_IIN_MAX +#define PSU_IOUT_MAX_ATTR_ID(index) PSU##index##_IOUT_MAX +#define PSU_PIN_MAX_ATTR_ID(index) PSU##index##_PIN_MAX +#define PSU_POUT_MAX_ATTR_ID(index) PSU##index##_POUT_MAX + +#define PSU_ATTR(psu_id) \ + PSU_PRESENT_ATTR_ID(psu_id), \ + PSU_POWERGOOD_ATTR_ID(psu_id), \ + PSU_VIN_ATTR_ID(psu_id), \ + PSU_VOUT_ATTR_ID(psu_id), \ + PSU_IIN_ATTR_ID(psu_id), \ + PSU_IOUT_ATTR_ID(psu_id), \ + PSU_PIN_ATTR_ID(psu_id), \ + PSU_POUT_ATTR_ID(psu_id), \ + PSU_MODEL_ATTR_ID(psu_id), \ + PSU_SERIAL_ATTR_ID(psu_id), \ + PSU_TEMP1_INPUT_ATTR_ID(psu_id), \ + PSU_TEMP2_INPUT_ATTR_ID(psu_id), \ + PSU_TEMP3_INPUT_ATTR_ID(psu_id), \ + PSU_FAN_INPUT_ATTR_ID(psu_id), \ + PSU_FAN_DIR_ATTR_ID(psu_id), \ + PSU_TEMP1_INPUT_MAX_ATTR_ID(psu_id), \ + PSU_TEMP1_INPUT_MIN_ATTR_ID(psu_id), \ + PSU_TEMP2_INPUT_MAX_ATTR_ID(psu_id), \ + PSU_TEMP2_INPUT_MIN_ATTR_ID(psu_id), \ + PSU_TEMP3_INPUT_MAX_ATTR_ID(psu_id), \ + PSU_TEMP3_INPUT_MIN_ATTR_ID(psu_id), \ + PSU_VIN_MAX_ATTR_ID(psu_id), \ + PSU_VIN_MIN_ATTR_ID(psu_id), \ + PSU_VIN_UPPER_CRIT_ATTR_ID(psu_id), \ + PSU_VIN_LOWER_CRIT_ATTR_ID(psu_id), \ + PSU_VOUT_MAX_ATTR_ID(psu_id), \ + PSU_VOUT_MIN_ATTR_ID(psu_id), \ + PSU_IIN_MAX_ATTR_ID(psu_id), \ + PSU_IOUT_MAX_ATTR_ID(psu_id), \ + PSU_PIN_MAX_ATTR_ID(psu_id), \ + PSU_POUT_MAX_ATTR_ID(psu_id) + +enum as9737_32db_psu_sysfs_attrs { + /* psu attributes */ + PSU_ATTR(1), + PSU_ATTR(2), + NUM_OF_PSU_ATTR, + NUM_OF_PER_PSU_ATTR = (NUM_OF_PSU_ATTR/NUM_OF_PSU) +}; + +/* psu attributes */ +#define DECLARE_PSU_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(psu##index##_present, S_IRUGO, show_psu, \ + NULL, PSU##index##_PRESENT); \ + static SENSOR_DEVICE_ATTR(psu##index##_power_good, S_IRUGO, show_psu, \ + NULL, PSU##index##_POWER_GOOD); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin, S_IRUGO, show_psu, NULL, \ + PSU##index##_VIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_vout, S_IRUGO, show_psu, NULL, \ + PSU##index##_VOUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_iin, S_IRUGO, show_psu, NULL, \ + PSU##index##_IIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_iout, S_IRUGO, show_psu, NULL, \ + PSU##index##_IOUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_pin, S_IRUGO, show_psu, NULL, \ + PSU##index##_PIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_pout, S_IRUGO, show_psu, NULL, \ + PSU##index##_POUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_model, S_IRUGO, show_string, \ + NULL, PSU##index##_MODEL); \ + static SENSOR_DEVICE_ATTR(psu##index##_serial, S_IRUGO, show_string, \ + NULL, PSU##index##_SERIAL); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp1_input, S_IRUGO, show_psu, \ + NULL, PSU##index##_TEMP1_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp2_input, S_IRUGO, show_psu, \ + NULL, PSU##index##_TEMP2_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp3_input, S_IRUGO, show_psu, \ + NULL, PSU##index##_TEMP3_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_fan1_input, S_IRUGO, show_psu, \ + NULL, PSU##index##_FAN_INPUT); \ + static SENSOR_DEVICE_ATTR(psu##index##_fan_dir, S_IRUGO, show_string, \ + NULL, PSU##index##_FAN_DIR); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp1_input_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP1_INPUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp1_input_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP1_INPUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp2_input_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP2_INPUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp2_input_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP2_INPUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp3_input_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP3_INPUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_temp3_input_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_TEMP3_INPUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_upper_crit, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_UPPER_CRIT); \ + static SENSOR_DEVICE_ATTR(psu##index##_vin_lower_crit, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VIN_LOWER_CRIT); \ + static SENSOR_DEVICE_ATTR(psu##index##_vout_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VOUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_vout_min, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_VOUT_MIN); \ + static SENSOR_DEVICE_ATTR(psu##index##_iin_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_IIN_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_iout_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_IOUT_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_pin_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_PIN_MAX); \ + static SENSOR_DEVICE_ATTR(psu##index##_pout_max, S_IRUGO, \ + show_psu_info, NULL, PSU##index##_POUT_MAX) + +#define DECLARE_PSU_ATTR(index) \ + &sensor_dev_attr_psu##index##_present.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_power_good.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vout.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iin.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iout.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pin.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pout.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_model.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_serial.dev_attr.attr,\ + &sensor_dev_attr_psu##index##_temp1_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp2_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp3_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_fan1_input.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_fan_dir.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp1_input_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp1_input_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp2_input_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp2_input_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp3_input_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_temp3_input_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_upper_crit.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vin_lower_crit.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_vout_max.dev_attr.attr,\ + &sensor_dev_attr_psu##index##_vout_min.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iin_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_iout_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pin_max.dev_attr.attr, \ + &sensor_dev_attr_psu##index##_pout_max.dev_attr.attr + +DECLARE_PSU_SENSOR_DEVICE_ATTR(1); +/*Duplicate nodes for lm-sensors.*/ +static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, show_psu, NULL, PSU1_VOUT); +static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, show_psu, NULL, PSU1_IOUT); +static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, show_psu, NULL, PSU1_POUT); +static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_psu, NULL, PSU1_TEMP1_INPUT); +static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_psu, NULL, PSU1_FAN_INPUT); + +static struct attribute *as9737_32db_psu1_attrs[] = { + /* psu attributes */ + DECLARE_PSU_ATTR(1), + &sensor_dev_attr_curr1_input.dev_attr.attr, + &sensor_dev_attr_in0_input.dev_attr.attr, + &sensor_dev_attr_power1_input.dev_attr.attr, + &sensor_dev_attr_temp1_input.dev_attr.attr, + &sensor_dev_attr_fan1_input.dev_attr.attr, + NULL +}; +static struct attribute_group as9737_32db_psu1_group = { + .attrs = as9737_32db_psu1_attrs, +}; +/* ATTRIBUTE_GROUPS(as9737_32db_psu1); */ + +DECLARE_PSU_SENSOR_DEVICE_ATTR(2); +/*Duplicate nodes for lm-sensors.*/ +static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, show_psu, NULL, PSU2_VOUT); +static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, show_psu, NULL, PSU2_IOUT); +static SENSOR_DEVICE_ATTR(power2_input, S_IRUGO, show_psu, NULL, PSU2_POUT); +static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_psu, NULL, PSU2_TEMP1_INPUT); +static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_psu, NULL, PSU2_FAN_INPUT); +static struct attribute *as9737_32db_psu2_attrs[] = { + /* psu attributes */ + DECLARE_PSU_ATTR(2), + &sensor_dev_attr_curr2_input.dev_attr.attr, + &sensor_dev_attr_in1_input.dev_attr.attr, + &sensor_dev_attr_power2_input.dev_attr.attr, + &sensor_dev_attr_temp2_input.dev_attr.attr, + &sensor_dev_attr_fan2_input.dev_attr.attr, + NULL +}; +static struct attribute_group as9737_32db_psu2_group = { + .attrs = as9737_32db_psu2_attrs, +}; +/* ATTRIBUTE_GROUPS(as9737_32db_psu2); */ + +const struct attribute_group *as9737_32db_psu_groups[][2] = { + {&as9737_32db_psu1_group, NULL}, + {&as9737_32db_psu2_group, NULL} +}; + +static struct as9737_32db_psu_data *as9737_32db_psu_update_device( + struct device_attribute *da) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + int status = 0; + + if (time_before(jiffies, data->last_updated[pid] + HZ * 5) && + data->valid[pid]) + return data; + + data->valid[pid] = 0; + /* To be compatible for older BMC firmware */ + data->ipmi_resp[pid].status[PSU_VOUT_MODE] = 0xff; + + /* Get status from ipmi */ + data->ipmi_tx_data[0] = pid + 1; /* PSU ID base id for ipmi start from 1 */ + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 1, + data->ipmi_resp[pid].status, + sizeof(data->ipmi_resp[pid].status)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get model name from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_MODEL_NAME_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].model, + sizeof(data->ipmi_resp[pid].model) - 1); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get serial number from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_SERIAL_NUM_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].serial, + sizeof(data->ipmi_resp[pid].serial) - 1); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get fan direction from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_FAN_DIR_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].fandir, + sizeof(data->ipmi_resp[pid].fandir) - 1); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + /* Get capability from ipmi */ + data->ipmi_tx_data[1] = IPMI_PSU_INFO_CMD; + status = ipmi_send_message(&data->ipmi, IPMI_PSU_READ_CMD, + data->ipmi_tx_data, 2, + data->ipmi_resp[pid].info, + sizeof(data->ipmi_resp[pid].info)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated[pid] = jiffies; + data->valid[pid] = 1; + +exit: + return data; +} + +#define VALIDATE_PRESENT_RETURN(id) \ +do { \ + if (data->ipmi_resp[id].status[PSU_PRESENT] == 0) { \ + mutex_unlock(&data->update_lock); \ + return -ENXIO; \ + } \ +} while (0) + +static ssize_t show_psu(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + u32 value = 0; + int present = 0; + int error = 0; + int multiplier = 1000; + + mutex_lock(&data->update_lock); + + data = as9737_32db_psu_update_device(da); + if (!data->valid[pid]) { + error = -EIO; + goto exit; + } + + present = !!(data->ipmi_resp[pid].status[PSU_PRESENT]); + + switch (attr->index) { + case PSU1_PRESENT: + case PSU2_PRESENT: + value = present; + break; + case PSU1_POWER_GOOD: + case PSU2_POWER_GOOD: + VALIDATE_PRESENT_RETURN(pid); + value = data->ipmi_resp[pid].status[PSU_POWER_GOOD_PMBUS]; + break; + case PSU1_IIN: + case PSU2_IIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_IIN0] | + (u32)data->ipmi_resp[pid].status[PSU_IIN1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_IIN2] << 16); + break; + case PSU1_IOUT: + case PSU2_IOUT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_IOUT0] | + (u32)data->ipmi_resp[pid].status[PSU_IOUT1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_IOUT2] << 16); + break; + case PSU1_VIN: + case PSU2_VIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_VIN0] | + (u32)data->ipmi_resp[pid].status[PSU_VIN1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_VIN2] << 16); + break; + case PSU1_VOUT: + case PSU2_VOUT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_VOUT0] | + (u32)data->ipmi_resp[pid].status[PSU_VOUT1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_VOUT2] << 16); + break; + case PSU1_PIN: + case PSU2_PIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_PIN0] | + (u32)data->ipmi_resp[pid].status[PSU_PIN1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_PIN2] << 16 | + (u32)data->ipmi_resp[pid].status[PSU_PIN3] << 24); + value /= 1000; // Convert to milliwatt + break; + case PSU1_POUT: + case PSU2_POUT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].status[PSU_POUT0] | + (u32)data->ipmi_resp[pid].status[PSU_POUT1] << 8 | + (u32)data->ipmi_resp[pid].status[PSU_POUT2] << 16 | + (u32)data->ipmi_resp[pid].status[PSU_POUT3] << 24); + value /= 1000; // Convert to milliwatt + break; + case PSU1_TEMP1_INPUT: + case PSU2_TEMP1_INPUT: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].status[PSU_TEMP1_0] | + (u16)data->ipmi_resp[pid].status[PSU_TEMP1_1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP2_INPUT: + case PSU2_TEMP2_INPUT: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].status[PSU_TEMP2_0] | + (u16)data->ipmi_resp[pid].status[PSU_TEMP2_1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP3_INPUT: + case PSU2_TEMP3_INPUT: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].status[PSU_TEMP3_0] | + (u16)data->ipmi_resp[pid].status[PSU_TEMP3_1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_FAN_INPUT: + case PSU2_FAN_INPUT: + VALIDATE_PRESENT_RETURN(pid); + multiplier = 1; + value = ((u32)data->ipmi_resp[pid].status[PSU_FAN0] | + (u32)data->ipmi_resp[pid].status[PSU_FAN1] << 8); + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + + return sprintf(buf, "%d\n", present ? value : 0); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_psu_info(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + s32 value = 0; + int present = 0; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9737_32db_psu_update_device(da); + if (!data->valid[pid]) { + error = -EIO; + goto exit; + } + + present = !!(data->ipmi_resp[pid].status[PSU_PRESENT]); + + switch (attr->index) { + case PSU1_TEMP1_INPUT_MAX: + case PSU2_TEMP1_INPUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP1_MAX0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP1_MAX1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP1_INPUT_MIN: + case PSU2_TEMP1_INPUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP1_MIN0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP1_MIN1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP2_INPUT_MAX: + case PSU2_TEMP2_INPUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP2_MAX0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP2_MAX1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP2_INPUT_MIN: + case PSU2_TEMP2_INPUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP2_MIN0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP2_MIN1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP3_INPUT_MAX: + case PSU2_TEMP3_INPUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP3_MAX0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP3_MAX1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_TEMP3_INPUT_MIN: + case PSU2_TEMP3_INPUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = (s16)((u16)data->ipmi_resp[pid].info[PSU_TEMP3_MIN0] | + (u16)data->ipmi_resp[pid].info[PSU_TEMP3_MIN1] << 8); + value *= 1000; // Convert to millidegree Celsius + break; + case PSU1_VIN_UPPER_CRIT: + case PSU2_VIN_UPPER_CRIT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_UPPER_CRIT0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_UPPER_CRIT1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_UPPER_CRIT2] << 16); + break; + case PSU1_VIN_LOWER_CRIT: + case PSU2_VIN_LOWER_CRIT: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_LOWER_CRIT0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_LOWER_CRIT1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_LOWER_CRIT2] << 16); + break; + case PSU1_VIN_MAX: + case PSU2_VIN_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MAX2] << 16); + break; + case PSU1_VIN_MIN: + case PSU2_VIN_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VIN_MIN0] | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MIN1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VIN_MIN2] << 16); + break; + case PSU1_VOUT_MAX: + case PSU2_VOUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VOUT_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MAX2] << 16); + break; + case PSU1_VOUT_MIN: + case PSU2_VOUT_MIN: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_VOUT_MIN0] | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MIN1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_VOUT_MIN2] << 16); + break; + case PSU1_IIN_MAX: + case PSU2_IIN_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_IIN_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_IIN_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_IIN_MAX2] << 16); + break; + case PSU1_IOUT_MAX: + case PSU2_IOUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_IOUT_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_IOUT_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_IOUT_MAX2] << 16); + break; + case PSU1_PIN_MAX: + case PSU2_PIN_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_PIN_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_PIN_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_PIN_MAX2] << 16 | + (u32)data->ipmi_resp[pid].info[PSU_PIN_MAX3] << 24); + value /= 1000; // Convert to milliwatt + break; + case PSU1_POUT_MAX: + case PSU2_POUT_MAX: + VALIDATE_PRESENT_RETURN(pid); + value = ((u32)data->ipmi_resp[pid].info[PSU_POUT_MAX0] | + (u32)data->ipmi_resp[pid].info[PSU_POUT_MAX1] << 8 | + (u32)data->ipmi_resp[pid].info[PSU_POUT_MAX2] << 16 | + (u32)data->ipmi_resp[pid].info[PSU_POUT_MAX3] << 24); + value /= 1000; // Convert to milliwatt + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + + return sprintf(buf, "%d\n", present ? value : 0); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static ssize_t show_string(struct device *dev, struct device_attribute *da, + char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + unsigned char pid = attr->index / NUM_OF_PER_PSU_ATTR; + char *str = NULL; + int error = 0; + + mutex_lock(&data->update_lock); + + data = as9737_32db_psu_update_device(da); + if (!data->valid[pid]) { + error = -EIO; + goto exit; + } + + switch (attr->index) { + case PSU1_MODEL: + case PSU2_MODEL: + VALIDATE_PRESENT_RETURN(pid); + str = data->ipmi_resp[pid].model; + break; + case PSU1_SERIAL: + case PSU2_SERIAL: + VALIDATE_PRESENT_RETURN(pid); + str = data->ipmi_resp[pid].serial; + break; + case PSU1_FAN_DIR: + case PSU2_FAN_DIR: + VALIDATE_PRESENT_RETURN(pid); + str = data->ipmi_resp[pid].fandir; + break; + default: + error = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%s\n", str); + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static umode_t as9737_32db_psu_is_visible(const void *drvdata, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + return 0; +} + +static const struct hwmon_channel_info *as9737_32db_psu_info[] = { + HWMON_CHANNEL_INFO(power, HWMON_P_ENABLE), + NULL, +}; + +static const struct hwmon_ops as9737_32db_psu_hwmon_ops = { + .is_visible = as9737_32db_psu_is_visible, +}; + +static const struct hwmon_chip_info as9737_32db_psu_chip_info = { + .ops = &as9737_32db_psu_hwmon_ops, + .info = as9737_32db_psu_info, +}; + +static int as9737_32db_psu_probe(struct platform_device *pdev) +{ + int status = 0; + struct device *hwmon_dev = NULL; + int i = 0; + + for(i = 0; i < 2; i++) { + hwmon_dev = hwmon_device_register_with_info(&pdev->dev, DRVNAME, + NULL, &as9737_32db_psu_chip_info, as9737_32db_psu_groups[i]); + + if (IS_ERR(hwmon_dev)) { + status = PTR_ERR(hwmon_dev); + return status; + } + + mutex_lock(&data->update_lock); + data->hwmon_dev[i] = hwmon_dev; + mutex_unlock(&data->update_lock); + + dev_info(&pdev->dev, "PSU%d device created\n", i + 1); + } + + return 0; +} + +static int as9737_32db_psu_remove(struct platform_device *pdev) +{ + int i = 0; + + for(i = 0; i < 2 ; i++) { + mutex_lock(&data->update_lock); + if (data->hwmon_dev[i]) { + hwmon_device_unregister(data->hwmon_dev[i]); + data->hwmon_dev[i] = NULL; + } + mutex_unlock(&data->update_lock); + } + + return 0; +} + +static int __init as9737_32db_psu_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9737_32db_psu_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9737_32db_psu_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9737_32db_psu_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9737_32db_psu_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9737_32db_psu_driver); + kfree(data); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9737_32db_psu driver"); +MODULE_LICENSE("GPL"); + +module_init(as9737_32db_psu_init); +module_exit(as9737_32db_psu_exit); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-sys.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-sys.c new file mode 100644 index 000000000..57f5910d2 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-sys.c @@ -0,0 +1,360 @@ +/* + * Copyright (C) Roger Ho + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9737_32db_sys" + +#define IPMI_READ_MAX_LEN 128 + +#define EEPROM_NAME "eeprom" +#define EEPROM_SIZE 256 /* 256 byte eeprom */ + +#define IPMI_SYSEEPROM_READ_CMD 0x18 +#define IPMI_CPLD_VER_READ_CMD 0x20 +#define IPMI_CPLD_REG_READ_CMD 0x22 + +static int as9737_32db_sys_probe(struct platform_device *pdev); +static int as9737_32db_sys_remove(struct platform_device *pdev); +static ssize_t show(struct device *dev, + struct device_attribute *da, char *buf); + +struct as9737_32db_sys_data { + struct platform_device *pdev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + struct ipmi_data ipmi; + unsigned char ipmi_resp_eeprom[EEPROM_SIZE]; + unsigned char ipmi_resp_cpld[2]; + unsigned char ipmi_tx_data[2]; + struct bin_attribute eeprom; /* eeprom data */ +}; + +struct as9737_32db_sys_data *data = NULL; + +static struct platform_driver as9737_32db_sys_driver = { + .probe = as9737_32db_sys_probe, + .remove = as9737_32db_sys_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +enum as9737_32db_sys_sysfs_attrs { + CPU_EC_VER, + FPGA_VER, + BIOS_FLASH_ID, +}; + +static SENSOR_DEVICE_ATTR(cpu_ec_version, S_IRUGO, show, NULL, CPU_EC_VER); +static SENSOR_DEVICE_ATTR(fpga_version, S_IRUGO, show, NULL, FPGA_VER); +static SENSOR_DEVICE_ATTR(bios_flash_id, S_IRUGO, show, NULL, BIOS_FLASH_ID); + +static struct attribute *as9737_32db_sys_attributes[] = { + &sensor_dev_attr_cpu_ec_version.dev_attr.attr, + &sensor_dev_attr_fpga_version.dev_attr.attr, + &sensor_dev_attr_bios_flash_id.dev_attr.attr, + NULL +}; + +static const struct attribute_group as9737_32db_sys_group = { + .attrs = as9737_32db_sys_attributes, +}; + +static ssize_t sys_eeprom_read(loff_t off, char *buf, size_t count) +{ + int status = 0; + unsigned char length = 0; + + if ((off + count) > EEPROM_SIZE) + return -EINVAL; + + length = (count >= IPMI_READ_MAX_LEN) ? IPMI_READ_MAX_LEN : count; + data->ipmi_tx_data[0] = (off & 0xff); + data->ipmi_tx_data[1] = length; + status = ipmi_send_message(&data->ipmi, IPMI_SYSEEPROM_READ_CMD, + data->ipmi_tx_data, sizeof(data->ipmi_tx_data), + data->ipmi_resp_eeprom + off, length); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + status = length; /* Read length */ + memcpy(buf, data->ipmi_resp_eeprom + off, length); + +exit: + return status; +} + +static ssize_t sysfs_bin_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *attr, + char *buf, loff_t off, size_t count) +{ + ssize_t retval = 0; + + if (unlikely(!count)) + return count; + + /* + * Read data from chip, protecting against concurrent updates + * from this host + */ + mutex_lock(&data->update_lock); + + while (count) { + ssize_t status; + + status = sys_eeprom_read(off, buf, count); + if (status <= 0) { + if (retval == 0) + retval = status; + break; + } + + buf += status; + off += status; + count -= status; + retval += status; + } + + mutex_unlock(&data->update_lock); + return retval; +} + +static int sysfs_eeprom_init(struct kobject *kobj, + struct bin_attribute *eeprom) +{ + sysfs_bin_attr_init(eeprom); + eeprom->attr.name = EEPROM_NAME; + eeprom->attr.mode = S_IRUGO; + eeprom->read = sysfs_bin_read; + eeprom->size = EEPROM_SIZE; + eeprom->write = NULL; + + /* Create eeprom file */ + return sysfs_create_bin_file(kobj, eeprom); +} + +static int sysfs_eeprom_cleanup(struct kobject *kobj, + struct bin_attribute *eeprom) +{ + sysfs_remove_bin_file(kobj, eeprom); + return 0; +} + +static struct as9737_32db_sys_data *as9737_32db_sys_update_reg( + struct device_attribute *da) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int status = 0; + unsigned char ipmi_cmd = IPMI_CPLD_VER_READ_CMD; + unsigned char tx_data_len = 1; + + data->valid = 0; + + switch (attr->index) { + case CPU_EC_VER: + ipmi_cmd = IPMI_CPLD_VER_READ_CMD; + tx_data_len = 1; + data->ipmi_tx_data[0] = 0x21; + break; + + case FPGA_VER: + ipmi_cmd = IPMI_CPLD_VER_READ_CMD; + tx_data_len = 1; + data->ipmi_tx_data[0] = 0x60; + break; + + case BIOS_FLASH_ID: + ipmi_cmd = IPMI_CPLD_REG_READ_CMD; + tx_data_len = 2; + data->ipmi_tx_data[0] = 0x60; + data->ipmi_tx_data[1] = 0xFA; + break; + default: + goto exit; + } + + status = ipmi_send_message(&data->ipmi, ipmi_cmd, + data->ipmi_tx_data, tx_data_len, + data->ipmi_resp_cpld, + sizeof(data->ipmi_resp_cpld)); + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + +exit: + return data; +} + +static ssize_t show(struct device *dev, + struct device_attribute *da, char *buf) +{ + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + int count = 0; + int error = 0; + unsigned char bios_flash_id; + + mutex_lock(&data->update_lock); + + data = as9737_32db_sys_update_reg(da); + if (!data->valid) { + error = -EIO; + goto exit; + } + + switch (attr->index) { + case CPU_EC_VER: + count = sprintf(buf, "%02X.%02X\n", data->ipmi_resp_cpld[0], + data->ipmi_resp_cpld[1]); + break; + + case FPGA_VER: + count = sprintf(buf, "%02X\n", data->ipmi_resp_cpld[0]); + break; + + case BIOS_FLASH_ID: + bios_flash_id = data->ipmi_resp_cpld[0] & 0x3; + count = sprintf(buf, "%d\n", (bios_flash_id == 0 || + bios_flash_id == 2) ? 1 : 2); /*1: master, 2: slave*/ + break; + + default: + error = -EIO; + goto exit; + } + + mutex_unlock(&data->update_lock); + return count; + +exit: + mutex_unlock(&data->update_lock); + return error; +} + +static int as9737_32db_sys_probe(struct platform_device *pdev) +{ + int status = -1; + + /* Register sysfs hooks */ + status = sysfs_eeprom_init(&pdev->dev.kobj, &data->eeprom); + if (status) + goto exit; + + /* Register sysfs hooks */ + status = sysfs_create_group(&pdev->dev.kobj, &as9737_32db_sys_group); + if (status) + goto exit; + + dev_info(&pdev->dev, "device created\n"); + + return 0; + +exit: + return status; +} + +static int as9737_32db_sys_remove(struct platform_device *pdev) +{ + sysfs_eeprom_cleanup(&pdev->dev.kobj, &data->eeprom); + sysfs_remove_group(&pdev->dev.kobj, &as9737_32db_sys_group); + + return 0; +} + +static int __init as9737_32db_sys_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9737_32db_sys_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9737_32db_sys_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9737_32db_sys_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9737_32db_sys_exit(void) +{ + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9737_32db_sys_driver); + kfree(data); +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9737_32db_sys driver"); +MODULE_LICENSE("GPL"); + +module_init(as9737_32db_sys_init); +module_exit(as9737_32db_sys_exit); diff --git a/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-thermal.c b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-thermal.c new file mode 100644 index 000000000..65295e4be --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/modules/builds/src/x86-64-accton-as9737-32db-thermal.c @@ -0,0 +1,351 @@ +/* + * Copyright (C) Roger Ho + * + * Based on: + * pca954x.c from Kumar Gala + * Copyright (C) 2006 + * + * Based on: + * pca954x.c from Ken Harrenstien + * Copyright (C) 2004 Google, Inc. (Ken Harrenstien) + * + * Based on: + * i2c-virtual_cb.c from Brian Kuschak + * and + * pca9540.c from Jean Delvare . + * + * This file is licensed under the terms of the GNU General Public + * License version 2. This program is licensed "as is" without any + * warranty of any kind, whether express or implied. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "accton_ipmi_intf.h" + +#define DRVNAME "as9737_32db_thermal" +#define IPMI_THERMAL_READ_CMD 0x12 +#define THERMAL_COUNT 5 +#define THERMAL_DATA_LEN 3 +#define THERMAL_DATA_COUNT (THERMAL_COUNT * THERMAL_DATA_LEN) + +static ssize_t show_temp(struct device *dev, struct device_attribute *attr, + char *buf); +#ifdef ENABLE_THRESHOLD +static ssize_t show_threshold(struct device *dev, struct device_attribute *da, + char *buf); +#endif +static int as9737_32db_thermal_probe(struct platform_device *pdev); +static int as9737_32db_thermal_remove(struct platform_device *pdev); + +enum temp_data_index { + TEMP_ADDR, + TEMP_FAULT, + TEMP_INPUT, + TEMP_DATA_COUNT +}; + +struct as9737_32db_thermal_data { + struct platform_device *pdev; + struct device *hwmon_dev; + struct mutex update_lock; + char valid; /* != 0 if registers are valid */ + unsigned long last_updated; /* In jiffies */ + char ipmi_resp[THERMAL_DATA_COUNT]; /* 3 bytes for each thermal */ + struct ipmi_data ipmi; + unsigned char ipmi_tx_data[2]; /* 0: thermal id, 1: temp */ +}; + +#ifdef ENABLE_THRESHOLD +static s8 temp_max_alarm[THERMAL_COUNT] = { 85, 85, 85, 79, 92}; +static s8 temp_max[THERMAL_COUNT] = { 80, 80, 80, 74, 87}; +static s8 temp_min[THERMAL_COUNT] = { -45, -45, -45, -45, -45}; +static s8 temp_min_alarm[THERMAL_COUNT] = { -50, -50, -50, -50, -50}; +#endif + +struct as9737_32db_thermal_data *data = NULL; + +static struct platform_driver as9737_32db_thermal_driver = { + .probe = as9737_32db_thermal_probe, + .remove = as9737_32db_thermal_remove, + .driver = { + .name = DRVNAME, + .owner = THIS_MODULE, + }, +}; + +enum as9737_32db_thermal_sysfs_attrs { + TEMP1_INPUT, // 0x48 + TEMP2_INPUT, // 0x49 + TEMP3_INPUT, // 0x4A + TEMP4_INPUT, // 0x4C + TEMP5_INPUT, // 0x4F + TEMP1_MAX_ALARM, + TEMP2_MAX_ALARM, + TEMP3_MAX_ALARM, + TEMP4_MAX_ALARM, + TEMP5_MAX_ALARM, + TEMP1_MAX, + TEMP2_MAX, + TEMP3_MAX, + TEMP4_MAX, + TEMP5_MAX, + TEMP1_MIN, + TEMP2_MIN, + TEMP3_MIN, + TEMP4_MIN, + TEMP5_MIN, + TEMP1_MIN_ALARM, + TEMP2_MIN_ALARM, + TEMP3_MIN_ALARM, + TEMP4_MIN_ALARM, + TEMP5_MIN_ALARM, +}; + +#ifdef ENABLE_THRESHOLD +// Read only temp_input +#define DECLARE_THERMAL_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, show_temp, \ + NULL, TEMP##index##_INPUT); \ + static SENSOR_DEVICE_ATTR(temp##index##_crit, S_IRUGO, show_threshold,\ + NULL, TEMP##index##_MAX_ALARM); \ + static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO, show_threshold,\ + NULL, TEMP##index##_MAX); \ + static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO, show_threshold,\ + NULL, TEMP##index##_MIN); \ + static SENSOR_DEVICE_ATTR(temp##index##_lcrit, S_IRUGO, show_threshold,\ + NULL, TEMP##index##_MIN_ALARM) + +#define DECLARE_THERMAL_ATTR(index) \ + &sensor_dev_attr_temp##index##_input.dev_attr.attr, \ + &sensor_dev_attr_temp##index##_crit.dev_attr.attr, \ + &sensor_dev_attr_temp##index##_max.dev_attr.attr, \ + &sensor_dev_attr_temp##index##_min.dev_attr.attr, \ + &sensor_dev_attr_temp##index##_lcrit.dev_attr.attr +#else +#define DECLARE_THERMAL_SENSOR_DEVICE_ATTR(index) \ + static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, show_temp, \ + NULL, TEMP##index##_INPUT); + +#define DECLARE_THERMAL_ATTR(index) \ + &sensor_dev_attr_temp##index##_input.dev_attr.attr +#endif + +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(1); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(2); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(3); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(4); +DECLARE_THERMAL_SENSOR_DEVICE_ATTR(5); + +static struct attribute *as9737_32db_thermal_attrs[] = { + DECLARE_THERMAL_ATTR(1), + DECLARE_THERMAL_ATTR(2), + DECLARE_THERMAL_ATTR(3), + DECLARE_THERMAL_ATTR(4), + DECLARE_THERMAL_ATTR(5), + NULL +}; +ATTRIBUTE_GROUPS(as9737_32db_thermal); + +#ifdef ENABLE_THRESHOLD +static ssize_t show_threshold(struct device *dev, struct device_attribute *da, + char *buf) +{ + int status = 0; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + + mutex_lock(&data->update_lock); + + switch (attr->index) { + case TEMP1_MAX_ALARM ... TEMP5_MAX_ALARM: + status = (int)temp_max_alarm[attr->index - TEMP1_MAX_ALARM]; + break; + case TEMP1_MAX ... TEMP5_MAX: + status = (int)temp_max[attr->index - TEMP1_MAX]; + break; + case TEMP1_MIN ... TEMP5_MIN: + status = (int)temp_min[attr->index - TEMP1_MIN]; + break; + case TEMP1_MIN_ALARM ... TEMP5_MIN_ALARM: + status = (int)temp_min_alarm[attr->index - TEMP1_MIN_ALARM]; + break; + default: + status = -EINVAL; + goto exit; + } + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", status * 1000); + +exit: + mutex_unlock(&data->update_lock); + return status; +} +#endif + +static ssize_t show_temp(struct device *dev, struct device_attribute *da, + char *buf) +{ + int status = 0; + int index = 0; + struct sensor_device_attribute *attr = to_sensor_dev_attr(da); + + mutex_lock(&data->update_lock); + + if (time_after(jiffies, data->last_updated + HZ * 5) || !data->valid) { + data->valid = 0; + + status = ipmi_send_message(&data->ipmi, IPMI_THERMAL_READ_CMD, + NULL, 0, data->ipmi_resp, sizeof(data->ipmi_resp)); + + if (unlikely(status != 0)) + goto exit; + + if (unlikely(data->ipmi.rx_result != 0)) { + status = -EIO; + goto exit; + } + + data->last_updated = jiffies; + data->valid = 1; + } + + /* Get temp fault status */ + index = attr->index * TEMP_DATA_COUNT + TEMP_FAULT; + if (unlikely(data->ipmi_resp[index] == 0)) { + status = -EIO; + goto exit; + } + + /* Get temperature in degree celsius */ + index = attr->index * TEMP_DATA_COUNT + TEMP_INPUT; + status = ((s8)data->ipmi_resp[index]) * 1000; + + mutex_unlock(&data->update_lock); + return sprintf(buf, "%d\n", status); + +exit: + mutex_unlock(&data->update_lock); + return status; +} + +static umode_t as9737_32db_thermal_is_visible(const void *drvdata, + enum hwmon_sensor_types type, + u32 attr, int channel) +{ + return 0; +} + +static const struct hwmon_channel_info *as9737_32db_thermal_info[] = { + HWMON_CHANNEL_INFO(temp, HWMON_T_ENABLE), + NULL, +}; + +static const struct hwmon_ops as9737_32db_thermal_hwmon_ops = { + .is_visible = as9737_32db_thermal_is_visible, +}; + +static const struct hwmon_chip_info as9737_32db_thermal_chip_info = { + .ops = &as9737_32db_thermal_hwmon_ops, + .info = as9737_32db_thermal_info, +}; + +static int as9737_32db_thermal_probe(struct platform_device *pdev) +{ + int status = 0; + struct device *hwmon_dev; + + hwmon_dev = hwmon_device_register_with_info(&pdev->dev, DRVNAME, + NULL, &as9737_32db_thermal_chip_info, as9737_32db_thermal_groups); + if (IS_ERR(data->hwmon_dev)) { + status = PTR_ERR(data->hwmon_dev); + return status; + } + + mutex_lock(&data->update_lock); + data->hwmon_dev = hwmon_dev; + mutex_unlock(&data->update_lock); + + dev_info(&pdev->dev, "Device Created\n"); + + return status; +} + +static int as9737_32db_thermal_remove(struct platform_device *pdev) +{ + mutex_lock(&data->update_lock); + if (data->hwmon_dev) { + hwmon_device_unregister(data->hwmon_dev); + data->hwmon_dev = NULL; + } + mutex_unlock(&data->update_lock); + + return 0; +} + +static int __init as9737_32db_thermal_init(void) +{ + int ret; + + data = kzalloc(sizeof(struct as9737_32db_thermal_data), GFP_KERNEL); + if (!data) { + ret = -ENOMEM; + goto alloc_err; + } + + mutex_init(&data->update_lock); + + ret = platform_driver_register(&as9737_32db_thermal_driver); + if (ret < 0) + goto dri_reg_err; + + data->pdev = platform_device_register_simple(DRVNAME, -1, NULL, 0); + if (IS_ERR(data->pdev)) { + ret = PTR_ERR(data->pdev); + goto dev_reg_err; + } + + /* Set up IPMI interface */ + ret = init_ipmi_data(&data->ipmi, 0, &data->pdev->dev); + if (ret) + goto ipmi_err; + + return 0; + +ipmi_err: + platform_device_unregister(data->pdev); +dev_reg_err: + platform_driver_unregister(&as9737_32db_thermal_driver); +dri_reg_err: + kfree(data); +alloc_err: + return ret; +} + +static void __exit as9737_32db_thermal_exit(void) +{ + if (data) { + ipmi_destroy_user(data->ipmi.user); + platform_device_unregister(data->pdev); + platform_driver_unregister(&as9737_32db_thermal_driver); + kfree(data); + } +} + +MODULE_AUTHOR("Roger Ho "); +MODULE_DESCRIPTION("as9737_32db_thermal driver"); +MODULE_LICENSE("GPL"); + +module_init(as9737_32db_thermal_init); +module_exit(as9737_32db_thermal_exit); diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/Makefile b/packages/platforms/accton/x86-64/as9737-32db/onlp/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/PKG.yml b/packages/platforms/accton/x86-64/as9737-32db/onlp/PKG.yml new file mode 100644 index 000000000..a1b332ab3 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/PKG.yml @@ -0,0 +1 @@ +!include $ONL_TEMPLATES/onlp-platform-revision.yml PLATFORM=x86-64-accton-as9737-32db ARCH=amd64 TOOLCHAIN=x86_64-linux-gnu REVISION=r0 diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/Makefile b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/Makefile new file mode 100644 index 000000000..e7437cb23 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/Makefile @@ -0,0 +1,2 @@ +FILTER=src +include $(ONL)/make/subdirs.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/lib/Makefile b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/lib/Makefile new file mode 100644 index 000000000..f7a788f9d --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/lib/Makefile @@ -0,0 +1,2 @@ +PLATFORM := x86-64-accton-as9737-32db +include $(ONL)/packages/base/any/onlp/builds/platform/libonlp-platform.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/onlpdump/Makefile b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/onlpdump/Makefile new file mode 100644 index 000000000..dcd69b795 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/onlpdump/Makefile @@ -0,0 +1,2 @@ +PLATFORM := x86-64-accton-as9737-32db +include $(ONL)/packages/base/any/onlp/builds/platform/onlps.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/.gitignore b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/.gitignore new file mode 100644 index 000000000..e69de29bb diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/.module b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/.module new file mode 100644 index 000000000..9df783dbf --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/.module @@ -0,0 +1 @@ +name: x86_64_accton_as9737_32db diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/Makefile b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/Makefile new file mode 100644 index 000000000..e07f6c511 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/Makefile @@ -0,0 +1,9 @@ +############################################################################### +# +# +# +############################################################################### +include $(ONL)/make/config.mk +MODULE := x86_64_accton_as9737_32db +AUTOMODULE := x86_64_accton_as9737_32db +include $(BUILDER)/definemodule.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/README b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/README new file mode 100644 index 000000000..6f560aad0 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/README @@ -0,0 +1,6 @@ +############################################################################### +# +# x86_64_accton_as9737_32db README +# +############################################################################### + diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/auto/make.mk b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/auto/make.mk new file mode 100644 index 000000000..a0ce705bd --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/auto/make.mk @@ -0,0 +1,9 @@ +############################################################################### +# +# x86_64_accton_as9737_32db Autogeneration +# +############################################################################### +x86_64_accton_as9737_32db_AUTO_DEFS := module/auto/x86_64_accton_as9737_32db.yml +x86_64_accton_as9737_32db_AUTO_DIRS := module/inc/x86_64_accton_as9737_32db module/src +include $(BUILDER)/auto.mk + diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/auto/x86_64_accton_as9737_32db.yml b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/auto/x86_64_accton_as9737_32db.yml new file mode 100644 index 000000000..c10407b52 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/auto/x86_64_accton_as9737_32db.yml @@ -0,0 +1,50 @@ +############################################################################### +# +# x86_64_accton_as9737_32db Autogeneration Definitions. +# +############################################################################### + +cdefs: &cdefs +- X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING: + doc: "Include or exclude logging." + default: 1 +- X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT: + doc: "Default enabled log options." + default: AIM_LOG_OPTIONS_DEFAULT +- X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT: + doc: "Default enabled log bits." + default: AIM_LOG_BITS_DEFAULT +- X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT: + doc: "Default enabled custom log bits." + default: 0 +- X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB: + doc: "Default all porting macros to use the C standard libraries." + default: 1 +- X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS: + doc: "Include standard library headers for stdlib porting macros." + default: X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB +- X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI: + doc: "Include generic uCli support." + default: 0 +- X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION: + doc: "Assume chassis fan direction is the same as the PSU fan direction." + default: 0 + + +definitions: + cdefs: + X86_64_ACCTON_AS9737_32DB_CONFIG_HEADER: + defs: *cdefs + basename: x86_64_accton_as9737_32db_config + + portingmacro: + x86_64_accton_as9737_32db: + macros: + - malloc + - free + - memset + - memcpy + + - vsnprintf + - snprintf + - strlen diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db.x b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db.x new file mode 100644 index 000000000..1e91d99e8 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db.x @@ -0,0 +1,14 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +/* <--auto.start.xmacro(ALL).define> */ +/* */ + +/* <--auto.start.xenum(ALL).define> */ +/* */ + + diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_config.h b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_config.h new file mode 100644 index 000000000..5abd23ffa --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_config.h @@ -0,0 +1,137 @@ +/**************************************************************************//** + * + * @file + * @brief x86_64_accton_as9737_32db Configuration Header + * + * @addtogroup x86_64_accton_as9737_32db-config + * @{ + * + *****************************************************************************/ +#ifndef __X86_64_ACCTON_AS9737_32DB_CONFIG_H__ +#define __X86_64_ACCTON_AS9737_32DB_CONFIG_H__ + +#ifdef GLOBAL_INCLUDE_CUSTOM_CONFIG +#include +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_INCLUDE_CUSTOM_CONFIG +#include +#endif + +/* */ +#include +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING + * + * Include or exclude logging. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING +#define X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING 1 +#endif + +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT + * + * Default enabled log options. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT +#define X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT AIM_LOG_OPTIONS_DEFAULT +#endif + +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT + * + * Default enabled log bits. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT +#define X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT AIM_LOG_BITS_DEFAULT +#endif + +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT + * + * Default enabled custom log bits. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT +#define X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT 0 +#endif + +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB + * + * Default all porting macros to use the C standard libraries. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB +#define X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB 1 +#endif + +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS + * + * Include standard library headers for stdlib porting macros. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS +#define X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB +#endif + +/** + * X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI + * + * Include generic uCli support. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI +#define X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI 0 +#endif + +/** + * X86_64_ACCTON_as9737_32db_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION + * + * Assume chassis fan direction is the same as the PSU fan direction. */ + + +#ifndef X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION +#define X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION 0 +#endif + + + +/** + * All compile time options can be queried or displayed + */ + +/** Configuration settings structure. */ +typedef struct x86_64_accton_as9737_32db_config_settings_s { + /** name */ + const char* name; + /** value */ + const char* value; +} x86_64_accton_as9737_32db_config_settings_t; + +/** Configuration settings table. */ +/** x86_64_accton_as9737_32db_config_settings table. */ +extern x86_64_accton_as9737_32db_config_settings_t x86_64_accton_as9737_32db_config_settings[]; + +/** + * @brief Lookup a configuration setting. + * @param setting The name of the configuration option to lookup. + */ +const char* x86_64_accton_as9737_32db_config_lookup(const char* setting); + +/** + * @brief Show the compile-time configuration. + * @param pvs The output stream. + */ +int x86_64_accton_as9737_32db_config_show(struct aim_pvs_s* pvs); + +/* */ + +#include "x86_64_accton_as9737_32db_porting.h" + +#endif /* __X86_64_ACCTON_AS9737_32DB_CONFIG_H__ */ +/* @} */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_dox.h b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_dox.h new file mode 100644 index 000000000..2547a8098 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_dox.h @@ -0,0 +1,26 @@ +/**************************************************************************//** + * + * x86_64_accton_as9737_32db Doxygen Header + * + *****************************************************************************/ +#ifndef __X86_64_ACCTON_AS9737_32DB_DOX_H__ +#define __X86_64_ACCTON_AS9737_32DB_DOX_H__ + +/** + * @defgroup x86_64_accton_as9737_32db x86_64_accton_as9737_32db - x86_64_accton_as9737_32db Description + * + +The documentation overview for this module should go here. + + * + * @{ + * + * @defgroup x86_64_accton_as9737_32db-x86_64_accton_as9737_32db Public Interface + * @defgroup x86_64_accton_as9737_32db-config Compile Time Configuration + * @defgroup x86_64_accton_as9737_32db-porting Porting Macros + * + * @} + * + */ + +#endif /* __X86_64_ACCTON_AS9737_32DB_DOX_H__ */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_porting.h b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_porting.h new file mode 100644 index 000000000..e99d9a0c5 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/inc/x86_64_accton_as9737_32db/x86_64_accton_as9737_32db_porting.h @@ -0,0 +1,97 @@ +/**************************************************************************//** + * + * @file + * @brief x86_64_accton_as9737_32db Porting Macros. + * + * @addtogroup x86_64_accton_as9737_32db-porting + * @{ + * + *****************************************************************************/ +#ifndef __X86_64_ACCTON_AS9737_32DB_PORTING_H__ +#define __X86_64_ACCTON_AS9737_32DB_PORTING_H__ + + +/* */ +#if X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS == 1 +#include +#include +#include +#include +#include +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_MALLOC + #if defined(GLOBAL_MALLOC) + #define X86_64_ACCTON_AS9737_32DB_MALLOC GLOBAL_MALLOC + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_MALLOC malloc + #else + #error The macro X86_64_ACCTON_AS9737_32DB_MALLOC is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_FREE + #if defined(GLOBAL_FREE) + #define X86_64_ACCTON_AS9737_32DB_FREE GLOBAL_FREE + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_FREE free + #else + #error The macro X86_64_ACCTON_AS9737_32DB_FREE is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_MEMSET + #if defined(GLOBAL_MEMSET) + #define X86_64_ACCTON_AS9737_32DB_MEMSET GLOBAL_MEMSET + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_MEMSET memset + #else + #error The macro X86_64_ACCTON_as9737_32db_MEMSET is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_MEMCPY + #if defined(GLOBAL_MEMCPY) + #define X86_64_ACCTON_AS9737_32DB_MEMCPY GLOBAL_MEMCPY + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_MEMCPY memcpy + #else + #error The macro X86_64_ACCTON_AS9737_32DB_MEMCPY is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_VSNPRINTF + #if defined(GLOBAL_VSNPRINTF) + #define X86_64_ACCTON_AS9737_32DB_VSNPRINTF GLOBAL_VSNPRINTF + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_VSNPRINTF vsnprintf + #else + #error The macro X86_64_ACCTON_AS9737_32DB_VSNPRINTF is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_SNPRINTF + #if defined(GLOBAL_SNPRINTF) + #define X86_64_ACCTON_AS9737_32DB_SNPRINTF GLOBAL_SNPRINTF + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_SNPRINTF snprintf + #else + #error The macro X86_64_ACCTON_AS9737_32DB_SNPRINTF is required but cannot be defined. + #endif +#endif + +#ifndef X86_64_ACCTON_AS9737_32DB_STRLEN + #if defined(GLOBAL_STRLEN) + #define X86_64_ACCTON_AS9737_32DB_STRLEN GLOBAL_STRLEN + #elif X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB == 1 + #define X86_64_ACCTON_AS9737_32DB_STRLEN strlen + #else + #error The macro X86_64_ACCTON_AS9737_32DB_STRLEN is required but cannot be defined. + #endif +#endif + +/* */ + + +#endif /* __X86_64_ACCTON_AS9737_32DB_PORTING_H__ */ +/* @} */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/make.mk b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/make.mk new file mode 100644 index 000000000..1a8eba730 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/make.mk @@ -0,0 +1,10 @@ +############################################################################### +# +# +# +############################################################################### +THIS_DIR := $(dir $(lastword $(MAKEFILE_LIST))) +x86_64_accton_as9737_32db_INCLUDES := -I $(THIS_DIR)inc +x86_64_accton_as9737_32db_INTERNAL_INCLUDES := -I $(THIS_DIR)src +x86_64_accton_as9737_32db_DEPENDMODULE_ENTRIES := init:x86_64_accton_as9737_32db ucli:x86_64_accton_as9737_32db + diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/Makefile b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/Makefile new file mode 100644 index 000000000..7e9dfbe05 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/Makefile @@ -0,0 +1,8 @@ +############################################################################### +# +# Local source generation targets. +# +############################################################################### + +ucli: + @../../../../tools/uclihandlers.py x86_64_accton_as9737_32db_ucli.c diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/debug.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/debug.c new file mode 100644 index 000000000..c4a50e35f --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/debug.c @@ -0,0 +1,72 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include "x86_64_accton_as9737_32db_int.h" + +#if x86_64_accton_as9737_32db_CONFIG_INCLUDE_DEBUG == 1 + +#include + +static char help__[] = + "Usage: debug [options]\n" + " -c CPLD Versions\n" + " -h Help\n" + ; + +int +x86_64_accton_as9737_32db_debug_main(int argc, char* argv[]) +{ + int c = 0; + int help = 0; + int rv = 0; + + while( (c = getopt(argc, argv, "ch")) != -1) { + switch(c) { + case 'c': + c = 1; + break; + case 'h': + help = 1; + rv = 0; + break; + default: + help = 1; + rv = 1; + break; + } + } + + if(help || argc == 1) { + printf("%s", help__); + return rv; + } + + if(c) + printf("Not implemented.\n"); + + return 0; +} + +#endif diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/fani.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/fani.c new file mode 100644 index 000000000..391685264 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/fani.c @@ -0,0 +1,258 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * Fan Platform Implementation Defaults. + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +enum fan_id { + FAN_1_ON_FAN_BOARD = 1, + FAN_2_ON_FAN_BOARD, + FAN_3_ON_FAN_BOARD, + FAN_4_ON_FAN_BOARD, + FAN_5_ON_FAN_BOARD, + FAN_6_ON_FAN_BOARD, + FAN_1_ON_PSU_1, + FAN_1_ON_PSU_2 +}; + +#define MAX_FAN_SPEED 33000 +#define MAX_PSU_FAN_SPEED 24000 + +#define CHASSIS_FAN_INFO(fid) \ +{ \ + { ONLP_FAN_ID_CREATE(FAN_##fid##_ON_FAN_BOARD), "Chassis Fan - "#fid, 0, {0} },\ + 0x0,\ + ONLP_FAN_CAPS_SET_PERCENTAGE | ONLP_FAN_CAPS_GET_RPM | ONLP_FAN_CAPS_GET_PERCENTAGE,\ + 0,\ + 0,\ + ONLP_FAN_MODE_INVALID,\ +} + +#define PSU_FAN_INFO(pid, fid) \ +{ \ + { ONLP_FAN_ID_CREATE(FAN_##fid##_ON_PSU_##pid), "PSU "#pid" - Fan "#fid, 0, {0} },\ + 0x0,\ + ONLP_FAN_CAPS_GET_RPM | ONLP_FAN_CAPS_GET_PERCENTAGE,\ + 0,\ + 0,\ + ONLP_FAN_MODE_INVALID,\ +} + +/* Static fan information */ +onlp_fan_info_t finfo[] = { + { }, /* Not used */ + CHASSIS_FAN_INFO(1), + CHASSIS_FAN_INFO(2), + CHASSIS_FAN_INFO(3), + CHASSIS_FAN_INFO(4), + CHASSIS_FAN_INFO(5), + CHASSIS_FAN_INFO(6), + PSU_FAN_INFO(1,1), + PSU_FAN_INFO(2,1) +}; + +#define VALIDATE(_id) \ +do { \ + if(!ONLP_OID_IS_FAN(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ +} while(0) + +static int +_onlp_fani_set_fan_dir_info(int fid, onlp_fan_info_t* info) +{ + enum onlp_fan_dir dir = onlp_get_fan_dir(fid); + + if (FAN_DIR_F2B == dir) { + info->caps |= ONLP_FAN_CAPS_F2B; + info->status |= ONLP_FAN_STATUS_F2B; + } else { + info->caps |= ONLP_FAN_CAPS_B2F; + info->status |= ONLP_FAN_STATUS_B2F; + } + + return ONLP_STATUS_OK; +} + +static int +_onlp_fani_info_get_fan(int fid, onlp_fan_info_t* info) +{ + int value, ret, pwm; + + /* get fan present status + */ + ret = onlp_file_read_int(&value, FAN_SYSFS_FORMAT"fan%d_present", fid); + + if (ret < 0) + return ONLP_STATUS_E_INTERNAL; + + if (value == 0) + return ONLP_STATUS_OK; + + info->status |= ONLP_FAN_STATUS_PRESENT; + + /* get fan fault status (turn on when any one fails) + */ + ret = onlp_file_read_int(&value, "%s""fan%d_fault", FAN_SYSFS_FORMAT, fid); + + if (ret < 0) + return ONLP_STATUS_E_INTERNAL; + + if (value > 0) { + info->status |= ONLP_FAN_STATUS_FAILED; + return ONLP_STATUS_OK; + } + + /* get fan speed + */ + ret = onlp_file_read_int(&value, "%s""fan%d_input", FAN_SYSFS_FORMAT, fid); + + if (ret < 0) + return ONLP_STATUS_E_INTERNAL; + + info->rpm = value; + + /* get speed percentage from rpm + */ + pwm = 0; + ret = onlp_file_read_int(&pwm, "%s""fan%d_pwm", FAN_SYSFS_FORMAT, fid); + + if (ret < 0) + return ONLP_STATUS_E_INTERNAL; + + value = 0; + ret = onlp_file_read_int(&value, "%s""fan%d_target", FAN_SYSFS_FORMAT, fid); + + if (ret < 0 || value == 0) + return ONLP_STATUS_E_INTERNAL; + + info->percentage = (info->rpm * pwm) / value; + + if (info->percentage > 100) + info->percentage = 100; + + _onlp_fani_set_fan_dir_info(fid, info); + + return ONLP_STATUS_OK; +} + +static int +_onlp_fani_info_get_fan_on_psu(int pid, onlp_fan_info_t* info) +{ + int val = 0; + + info->status |= ONLP_FAN_STATUS_PRESENT; + + /* Get power good status */ + if (psu_status_info_get(pid, "power_good", &val) == ONLP_STATUS_OK) { + info->status |= (val != PSU_STATUS_POWER_GOOD) ? ONLP_FAN_STATUS_FAILED : 0; + } + + /* get fan direction */ + if (psu_status_info_get(pid, "fan_dir", &val) == ONLP_STATUS_OK) { + if (val == FAN_DIR_B2F) + info->status |= ONLP_FAN_STATUS_B2F; + else if (val == FAN_DIR_F2B) + info->status |= ONLP_FAN_STATUS_F2B; + } + + /* get fan speed */ + if (psu_status_info_get(pid, "fan1_input", &val) == ONLP_STATUS_OK) { + /* get speed percentage from rpm */ + info->rpm = val; + info->percentage = (info->rpm * 100)/MAX_PSU_FAN_SPEED; + } + + return ONLP_STATUS_OK; +} + +/* + * This function will be called prior to all of onlp_fani_* functions. + */ +int +onlp_fani_init(void) +{ + return ONLP_STATUS_OK; +} + +int +onlp_fani_info_get(onlp_oid_t id, onlp_fan_info_t* info) +{ + int rc = 0; + int fid; + VALIDATE(id); + + fid = ONLP_OID_ID_GET(id); + *info = finfo[fid]; + + switch (fid) { + case FAN_1_ON_FAN_BOARD: + case FAN_2_ON_FAN_BOARD: + case FAN_3_ON_FAN_BOARD: + case FAN_4_ON_FAN_BOARD: + case FAN_5_ON_FAN_BOARD: + case FAN_6_ON_FAN_BOARD: + rc = _onlp_fani_info_get_fan(fid, info); + break; + case FAN_1_ON_PSU_1: + case FAN_1_ON_PSU_2: + rc = _onlp_fani_info_get_fan_on_psu(fid-FAN_6_ON_FAN_BOARD, info); + break; + default: + rc = ONLP_STATUS_E_INVALID; + break; + } + + return rc; +} + +/* + * This function sets the fan speed of the given OID as a percentage. + * + * This will only be called if the OID has the PERCENTAGE_SET + * capability. + * + * It is optional if you have no fans at all with this feature. + */ +int +onlp_fani_percentage_set(onlp_oid_t id, int p) +{ + int fid; + + VALIDATE(id); + + fid = ONLP_OID_ID_GET(id); + + if (fid < FAN_1_ON_FAN_BOARD || fid > FAN_6_ON_FAN_BOARD) + return ONLP_STATUS_E_UNSUPPORTED; + + if (onlp_file_write_int(p, "%s""fan%d_pwm", FAN_SYSFS_FORMAT, fid) != 0) { + AIM_LOG_ERROR("Unable to change duty cycle of fan (%d)\r\n", fid); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/ledi.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/ledi.c new file mode 100644 index 000000000..713902add --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/ledi.c @@ -0,0 +1,224 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2013 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +#define VALIDATE(_id) \ +do { \ + if(!ONLP_OID_IS_LED(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ +} while(0) + +#define LED_FORMAT "/sys/devices/platform/as9737_32db_led/%s" + +enum led_light_mode { /*must be the same with the definition @ kernel driver */ + LED_MODE_OFF, + LED_MODE_RED = 10, + LED_MODE_RED_BLINKING = 11, + LED_MODE_ORANGE = 12, + LED_MODE_ORANGE_BLINKING = 13, + LED_MODE_YELLOW = 14, + LED_MODE_YELLOW_BLINKING = 15, + LED_MODE_GREEN = 16, + LED_MODE_GREEN_BLINKING = 17, + LED_MODE_BLUE = 18, + LED_MODE_BLUE_BLINKING = 19, + LED_MODE_PURPLE = 20, + LED_MODE_PURPLE_BLINKING = 21, + LED_MODE_AUTO = 22, + LED_MODE_AUTO_BLINKING = 23, + LED_MODE_WHITE = 24, + LED_MODE_WHITE_BLINKING = 25, + LED_MODE_CYAN = 26, + LED_MODE_CYAN_BLINKING = 27, + LED_MODE_UNKNOWN = 99 +}; + +typedef struct led_light_mode_map { + enum onlp_led_id id; + enum led_light_mode driver_led_mode; + enum onlp_led_mode_e onlp_led_mode; +} led_light_mode_map_t; + +led_light_mode_map_t led_map[] = { + { LED_LOC, LED_MODE_OFF, ONLP_LED_MODE_OFF}, + { LED_LOC, LED_MODE_BLUE_BLINKING, ONLP_LED_MODE_BLUE_BLINKING}, + { LED_DIAG, LED_MODE_OFF, ONLP_LED_MODE_OFF}, + { LED_DIAG, LED_MODE_GREEN, ONLP_LED_MODE_GREEN}, + { LED_DIAG, LED_MODE_RED, ONLP_LED_MODE_RED}, + { LED_FAN, LED_MODE_GREEN, ONLP_LED_MODE_GREEN}, + { LED_FAN, LED_MODE_RED, ONLP_LED_MODE_RED}, + { LED_FAN, LED_MODE_OFF, ONLP_LED_MODE_OFF}, + { LED_PSU1, LED_MODE_GREEN, ONLP_LED_MODE_GREEN}, + { LED_PSU1, LED_MODE_RED, ONLP_LED_MODE_RED}, + { LED_PSU1, LED_MODE_OFF, ONLP_LED_MODE_OFF}, + { LED_PSU2, LED_MODE_GREEN, ONLP_LED_MODE_GREEN}, + { LED_PSU2, LED_MODE_RED, ONLP_LED_MODE_RED}, + { LED_PSU2, LED_MODE_OFF, ONLP_LED_MODE_OFF} +}; + +static char *leds[] = { /* must map with onlp_led_id */ + NULL, + "led_loc", + "led_diag", + "led_psu1", + "led_psu2", + "led_fan", +}; + +/* + * Get the information for the given LED OID. + */ +static onlp_led_info_t linfo[] = +{ + { }, /* Not used */ + { + { ONLP_LED_ID_CREATE(LED_LOC), "Chassis LED 1 (LOC LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_BLUE_BLINKING, + }, + { + { ONLP_LED_ID_CREATE(LED_DIAG), "Chassis LED 2 (DIAG LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_GREEN | ONLP_LED_CAPS_RED, + }, + { + { ONLP_LED_ID_CREATE(LED_PSU1), "Chassis LED 3 (PSU1 LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_AUTO, + }, + { + { ONLP_LED_ID_CREATE(LED_PSU2), "Chassis LED 4 (PSU2 LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_AUTO, + }, + { + { ONLP_LED_ID_CREATE(LED_FAN), "Chassis LED 5 (FAN LED)", 0, {0} }, + ONLP_LED_STATUS_PRESENT, + ONLP_LED_CAPS_ON_OFF | ONLP_LED_CAPS_AUTO, + }, +}; + +static int driver_to_onlp_led_mode(enum onlp_led_id id, enum led_light_mode driver_led_mode) +{ + int i, nsize = sizeof(led_map)/sizeof(led_map[0]); + + for (i = 0; i < nsize; i++) { + if (id == led_map[i].id && + driver_led_mode == led_map[i].driver_led_mode) + return led_map[i].onlp_led_mode; + } + + return 0; +} + +static int onlp_to_driver_led_mode(enum onlp_led_id id, onlp_led_mode_t onlp_led_mode) +{ + int i, nsize = sizeof(led_map) / sizeof(led_map[0]); + + for (i = 0; i < nsize; i++) { + if (id == led_map[i].id && onlp_led_mode == led_map[i].onlp_led_mode) + return led_map[i].driver_led_mode; + } + + return 0; +} + +/* + * This function will be called prior to any other onlp_ledi_* functions. + */ +int +onlp_ledi_init(void) +{ + return ONLP_STATUS_OK; +} + +int +onlp_ledi_info_get(onlp_oid_t id, onlp_led_info_t* info) +{ + int lid, value; + VALIDATE(id); + + lid = ONLP_OID_ID_GET(id); + + /* Set the onlp_oid_hdr_t and capabilities */ + *info = linfo[ONLP_OID_ID_GET(id)]; + + /* Get LED mode */ + if (onlp_file_read_int(&value, LED_FORMAT, leds[lid]) < 0) + return ONLP_STATUS_E_INTERNAL; + + info->mode = driver_to_onlp_led_mode(lid, value); + + /* Set the on/off status */ + if (info->mode != ONLP_LED_MODE_OFF) + info->status |= ONLP_LED_STATUS_ON; + + return ONLP_STATUS_OK; +} + +/* + * Turn an LED on or off. + * + * This function will only be called if the LED OID supports the ONOFF + * capability. + * + * What 'on' means in terms of colors or modes for multimode LEDs is + * up to the platform to decide. This is intended as baseline toggle mechanism. + */ +int +onlp_ledi_set(onlp_oid_t id, int on_or_off) +{ + VALIDATE(id); + + if (!on_or_off) + return onlp_ledi_mode_set(id, ONLP_LED_MODE_OFF); + + return ONLP_STATUS_E_UNSUPPORTED; +} + +/* + * This function puts the LED into the given mode. It is a more functional + * interface for multimode LEDs. + * + * Only modes reported in the LED's capabilities will be attempted. + */ +int +onlp_ledi_mode_set(onlp_oid_t id, onlp_led_mode_t mode) +{ + int lid; + VALIDATE(id); + + lid = ONLP_OID_ID_GET(id); + + if (onlp_file_write_int(onlp_to_driver_led_mode(lid , mode), + LED_FORMAT, leds[lid]) != 0) + return ONLP_STATUS_E_INTERNAL; + + return ONLP_STATUS_OK; +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/make.mk b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/make.mk new file mode 100644 index 000000000..5858530b3 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/make.mk @@ -0,0 +1,9 @@ +############################################################################### +# +# +# +############################################################################### + +LIBRARY := x86_64_accton_as9737_32db +$(LIBRARY)_SUBDIR := $(dir $(lastword $(MAKEFILE_LIST))) +include $(BUILDER)/lib.mk diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/platform_lib.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/platform_lib.c new file mode 100644 index 000000000..decca74d9 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/platform_lib.c @@ -0,0 +1,193 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +enum onlp_fan_dir onlp_get_fan_dir(int fid) +{ + int len = 0; + int i = 0; + int hwmon_idx; + char *str = NULL; + char *dirs[FAN_DIR_COUNT] = { "F2B", "B2F" }; + char file[32]; + enum onlp_fan_dir dir = FAN_DIR_F2B; + + hwmon_idx = onlp_get_fan_hwmon_idx(); + if (hwmon_idx >= 0) { + /* Read attribute */ + snprintf(file, sizeof(file), "fan%d_dir", fid); + len = onlp_file_read_str(&str, FAN_SYSFS_FORMAT_1, hwmon_idx, + file); + + /* Verify Fan dir string length */ + if (!str || len < 3) { + AIM_FREE_IF_PTR(str); + return dir; + } + + for (i = 0; i < AIM_ARRAYSIZE(dirs); i++) { + if (strncmp(str, dirs[i], strlen(dirs[i])) == 0) { + dir = (enum onlp_fan_dir)i; + break; + } + } + + AIM_FREE_IF_PTR(str); + } + + return dir; +} + +int onlp_get_psu_hwmon_idx(int pid) +{ + /* find hwmon index */ + char* file = NULL; + char path[64]; + int ret, hwmon_idx, max_hwmon_idx = 20; + + for (hwmon_idx = 0; hwmon_idx <= max_hwmon_idx; hwmon_idx++) { + snprintf(path, sizeof(path), "/sys/devices/platform/as9737_32db_psu/hwmon/hwmon%d/", + hwmon_idx); + + if (pid == 1) + ret = onlp_file_find(path, "psu1_present", &file); + else if (pid == 2) + ret = onlp_file_find(path, "psu2_present", &file); + else + return -1; + + AIM_FREE_IF_PTR(file); + + if (ONLP_STATUS_OK == ret) + return hwmon_idx; + } + + return -1; +} + +int onlp_get_fan_hwmon_idx(void) +{ + /* find hwmon index */ + char* file = NULL; + char path[64]; + int ret, hwmon_idx, max_hwmon_idx = 20; + + for (hwmon_idx = 0; hwmon_idx <= max_hwmon_idx; hwmon_idx++) { + snprintf(path, + sizeof(path), + "/sys/devices/platform/as9737_32db_fan/hwmon/hwmon%d/", + hwmon_idx); + + ret = onlp_file_find(path, "name", &file); + AIM_FREE_IF_PTR(file); + + if (ONLP_STATUS_OK == ret) + return hwmon_idx; + } + + return -1; +} + +psu_type_t get_psu_type(int id, char* modelname, int modelname_len) +{ + int ret = 0; + int hwmon_idx; + psu_type_t ptype = PSU_TYPE_UNKNOWN; + + hwmon_idx = onlp_get_psu_hwmon_idx(id); + if(hwmon_idx >= 0){ + char file[32]; + char *mn = NULL; + + snprintf(file, sizeof(file), "psu%d_model", id); + ret = onlp_file_read_str(&mn, + PSU_SYSFS_FORMAT_1, hwmon_idx, file); + + if (ret <= 0 || ret > PSU_MODEL_NAME_LEN+1 || mn == NULL) { + AIM_FREE_IF_PTR(mn); + return PSU_TYPE_UNKNOWN; + } + + if (modelname) + strncpy(modelname, mn, PSU_MODEL_NAME_LEN + 1); + + if (strncmp(mn, "FSJ001-610G", PSU_MODEL_NAME_LEN) == 0) + ptype = PSU_TYPE_AC_FSJ001_610G_F2B; + else if (strncmp(mn, "FSJ001-612G", PSU_MODEL_NAME_LEN) == 0) + ptype = PSU_TYPE_AC_FSJ001_612G_F2B; + else if (strncmp(mn, "FSJ004-612G", PSU_MODEL_NAME_LEN) == 0) + ptype = PSU_TYPE_AC_FSJ004_612G_B2F; + else if (strncmp(mn, "FSJ035-610G", PSU_MODEL_NAME_LEN) == 0) + ptype = PSU_TYPE_DC48_FSJ035_610G_F2B; + else if (strncmp(mn, "FSJ036-610G", PSU_MODEL_NAME_LEN) == 0) + ptype = PSU_TYPE_DC48_FSJ036_610G_B2F; + + AIM_FREE_IF_PTR(mn); + return ptype; + } + + return ptype; +} + +int psu_status_info_get(int id, char *node, int *value) +{ + int ret = 0; + int len = 0; + int hwmon_idx = 0; + char path[PSU_NODE_MAX_PATH_LEN] = {0}; + char *fandir = NULL; + + *value = 0; + + if (strncmp(node, "fan_dir", strlen("fan_dir")) == 0) { + hwmon_idx = onlp_get_psu_hwmon_idx(id); + if (hwmon_idx >= 0) { + snprintf(path, sizeof(path), PSU_SYSFS_FORMAT_1"%d_fan_dir", hwmon_idx, "psu", id); + len = onlp_file_read_str(&fandir, "%s", path); + + if (fandir && len) { + if (strncmp(fandir, "B2F", strlen("B2F")) == 0) + *value = FAN_DIR_B2F; + else if (strncmp(fandir, "F2B", strlen("F2B")) == 0) + *value = FAN_DIR_F2B; + } else { + AIM_LOG_ERROR("Unable to read status from file(%s)\r\n", path); + return ONLP_STATUS_E_INTERNAL; + } + AIM_FREE_IF_PTR(fandir); + } + } else { + sprintf(path, PSU_SYSFS_FORMAT, id, node); + if (onlp_file_read_int(value, path) < 0) { + AIM_LOG_ERROR("Unable to read status from file(%s)\r\n", path); + return ONLP_STATUS_E_INTERNAL; + } + } + + return ret; +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/platform_lib.h b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/platform_lib.h new file mode 100644 index 000000000..7f3e0b8b5 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/platform_lib.h @@ -0,0 +1,112 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#ifndef __PLATFORM_LIB_H__ +#define __PLATFORM_LIB_H__ + +#include "x86_64_accton_as9737_32db_log.h" + +#define CHASSIS_FAN_COUNT 6 +#define CHASSIS_THERMAL_COUNT 6 +#define CHASSIS_LED_COUNT 5 +#define CHASSIS_PSU_COUNT 2 + +#define PSU_MODEL_NAME_LEN 11 +#define NUM_OF_THERMAL_PER_PSU 3 +#define PSU_NODE_MAX_PATH_LEN 64 + +#define PSU1_ID 1 +#define PSU2_ID 2 + +#define PSU_STATUS_PRESENT 1 +#define PSU_STATUS_POWER_GOOD 1 + +#define PSU_SYSFS_FORMAT "/sys/devices/platform/as9737_32db_psu*psu%d_%s" +#define PSU_SYSFS_FORMAT_1 "/sys/devices/platform/as9737_32db_psu/hwmon/hwmon%d/%s" +#define FAN_SYSFS_FORMAT "/sys/devices/platform/as9737_32db_fan*" +#define FAN_SYSFS_FORMAT_1 "/sys/devices/platform/as9737_32db_fan/hwmon/hwmon%d/%s" +#define SYS_LED_PATH "/sys/devices/platform/as9737_32db_led/" +#define IDPROM_PATH "/sys/devices/platform/as9737_32db_sys/eeprom" +#define BIOS_VER_PATH "/sys/devices/virtual/dmi/id/bios_version" +#define BMC_VER1_PATH "/sys/bus/platform/drivers/ipmi_si/IPI0001:00/bmc/firmware_revision" +#define BMC_VER2_PATH "/sys/bus/platform/drivers/ipmi_si/IPI0001:00/bmc/aux_firmware_revision" + +enum onlp_thermal_id { + THERMAL_RESERVED = 0, + THERMAL_CPU_CORE, + THERMAL_1_ON_MAIN_BROAD, + THERMAL_2_ON_MAIN_BROAD, + THERMAL_3_ON_MAIN_BROAD, + THERMAL_4_ON_MAIN_BROAD, + THERMAL_5_ON_MAIN_BROAD, + THERMAL_1_ON_PSU1, + THERMAL_2_ON_PSU1, + THERMAL_3_ON_PSU1, + THERMAL_1_ON_PSU2, + THERMAL_2_ON_PSU2, + THERMAL_3_ON_PSU2, + THERMAL_COUNT +}; + +typedef enum psu_type { + PSU_TYPE_AC_FSJ001_610G_F2B, + PSU_TYPE_AC_FSJ001_612G_F2B, + PSU_TYPE_AC_FSJ004_612G_B2F, + PSU_TYPE_DC48_FSJ035_610G_F2B, + PSU_TYPE_DC48_FSJ036_610G_B2F, + NUM_OF_PSU_TYPE, + PSU_TYPE_UNKNOWN, +} psu_type_t; + +enum onlp_led_id { + LED_LOC = 1, + LED_DIAG, + LED_PSU1, + LED_PSU2, + LED_FAN +}; + +enum onlp_fan_dir { + FAN_DIR_F2B, + FAN_DIR_B2F, + FAN_DIR_COUNT +}; + +enum onlp_fan_dir onlp_get_fan_dir(int fid); +int onlp_get_psu_hwmon_idx(int pid); +int onlp_get_fan_hwmon_idx(void); +psu_type_t get_psu_type(int id, char* modelname, int modelname_len); +int psu_status_info_get(int id, char *node, int *value); + +#define AIM_FREE_IF_PTR(p) \ + do \ + { \ + if (p) { \ + aim_free(p); \ + p = NULL; \ + } \ + } while (0) + +#endif /* __PLATFORM_LIB_H__ */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/psui.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/psui.c new file mode 100644 index 000000000..7c95edd03 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/psui.c @@ -0,0 +1,187 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +#define VALIDATE(_id) \ + do { \ + if(!ONLP_OID_IS_PSU(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ + } while(0) + +int +onlp_psui_init(void) +{ + return ONLP_STATUS_OK; +} + +/* + * Get all information about the given PSU oid. + */ +static onlp_psu_info_t pinfo[] = { + { }, /* Not used */ + { + { ONLP_PSU_ID_CREATE(PSU1_ID), "PSU-1", 0, {0} }, + }, + { + { ONLP_PSU_ID_CREATE(PSU2_ID), "PSU-2", 0, {0} }, + } +}; + +int +onlp_psui_info_get(onlp_oid_t id, onlp_psu_info_t* info) +{ + int val = 0; + int ret = ONLP_STATUS_OK; + int pid = ONLP_OID_ID_GET(id); + int thermal_count = 0; + int hwmon_idx; + VALIDATE(id); + + memset(info, 0, sizeof(onlp_psu_info_t)); + *info = pinfo[pid]; /* Set the onlp_oid_hdr_t */ + + /* Get the present state */ + ret = onlp_file_read_int(&val, + PSU_SYSFS_FORMAT, pid, "present"); + + if (ret < 0) { + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_E_INTERNAL; + } + + if (val != PSU_STATUS_PRESENT) { + info->status &= ~ONLP_PSU_STATUS_PRESENT; + return ONLP_STATUS_OK; + } + info->status |= ONLP_PSU_STATUS_PRESENT; + + /* Set capability + */ + info->caps = ONLP_PSU_CAPS_AC; + + /* Set the associated oid_table */ + thermal_count = CHASSIS_THERMAL_COUNT; + + info->hdr.coids[0] = ONLP_THERMAL_ID_CREATE(thermal_count + + (pid-1)*NUM_OF_THERMAL_PER_PSU + 1); + info->hdr.coids[1] = ONLP_THERMAL_ID_CREATE(thermal_count + + (pid-1)*NUM_OF_THERMAL_PER_PSU + 2); + info->hdr.coids[2] = ONLP_THERMAL_ID_CREATE(thermal_count + + (pid-1)*NUM_OF_THERMAL_PER_PSU + 3); + info->hdr.coids[3] = ONLP_FAN_ID_CREATE(pid + CHASSIS_FAN_COUNT); + + /* Read voltage, current and power */ + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "vin"); + if (ret == ONLP_STATUS_OK && val) { + info->mvin = val; + info->caps |= ONLP_PSU_CAPS_VIN; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "iin"); + if (ret == ONLP_STATUS_OK && val) { + info->miin = val; + info->caps |= ONLP_PSU_CAPS_IIN; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "pin"); + if (ret == ONLP_STATUS_OK && val) { + info->mpin = val; + info->caps |= ONLP_PSU_CAPS_PIN; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "vout"); + if (ret == ONLP_STATUS_OK && val) { + info->mvout = val; + info->caps |= ONLP_PSU_CAPS_VOUT; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "iout"); + if (ret == ONLP_STATUS_OK && val) { + info->miout = val; + info->caps |= ONLP_PSU_CAPS_IOUT; + } + + val = 0; + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, pid, "pout"); + if (ret == ONLP_STATUS_OK && val) { + info->mpout = val; + info->caps |= ONLP_PSU_CAPS_POUT; + } + + hwmon_idx = onlp_get_psu_hwmon_idx(pid); + if (hwmon_idx >= 0) { + char *str = NULL; + int len; + char file[32]; + + /* Read model */ + snprintf(file, sizeof(file), "psu%d_model", pid); + len = onlp_file_read_str(&str, + PSU_SYSFS_FORMAT_1, hwmon_idx, file); + + if (str && len) { + memcpy(info->model, str, len); + info->model[len] = '\0'; + } + AIM_FREE_IF_PTR(str); + + /* Read serial */ + snprintf(file, sizeof(file), "psu%d_serial", pid); + len = onlp_file_read_str(&str, + PSU_SYSFS_FORMAT_1, hwmon_idx, file); + + if (str && len) { + memcpy(info->serial, str, len); + info->serial[len] = '\0'; + } + AIM_FREE_IF_PTR(str); + } + + /* Get power good status */ + ret = onlp_file_read_int(&val, + PSU_SYSFS_FORMAT, pid, "power_good"); + + if (ret < 0) { + info->status |= ONLP_PSU_STATUS_FAILED; + return ONLP_STATUS_E_INTERNAL; + } + + if (val != PSU_STATUS_POWER_GOOD) { + info->status |= ONLP_PSU_STATUS_UNPLUGGED; + info->caps = 0; + return ONLP_STATUS_OK; + } + + return ret; +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/sfpi.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/sfpi.c new file mode 100644 index 000000000..9f38cc975 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/sfpi.c @@ -0,0 +1,344 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2013 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include +#include +#include "x86_64_accton_as9737_32db_int.h" +#include "x86_64_accton_as9737_32db_log.h" + +#define VALIDATE_SFP(_port) \ + do { \ + if (_port < 33 || _port > 34) \ + return ONLP_STATUS_E_UNSUPPORTED; \ + } while(0) + +#define VALIDATE_QSFP(_port) \ + do { \ + if (_port < 1 || _port > 32 ) \ + return ONLP_STATUS_E_UNSUPPORTED; \ + } while(0) + +#define MODULE_EEPROM_FORMAT "/sys/bus/i2c/devices/%d-0050/eeprom" +#define MODULE_PRESENT_CPLD2_FORMAT "/sys/bus/i2c/devices/36-0061/module_present_%d" +#define MODULE_PRESENT_CPLD3_FORMAT "/sys/bus/i2c/devices/37-0062/module_present_%d" +#define MODULE_RXLOS_FORMAT "/sys/bus/i2c/devices/37-0062/module_rx_los_%d" +#define MODULE_TXFAULT_FORMAT "/sys/bus/i2c/devices/37-0062/module_tx_fault_%d" +#define MODULE_TXDISABLE_FORMAT "/sys/bus/i2c/devices/37-0062/module_tx_disable_%d" +#define MODULE_RESET_CPLD2_FORMAT "/sys/bus/i2c/devices/36-0061/module_reset_%d" +#define MODULE_RESET_CPLD3_FORMAT "/sys/bus/i2c/devices/37-0062/module_reset_%d" +#define MODULE_LPMODE_CPLD2_FORMAT "/sys/bus/i2c/devices/36-0061/module_lpmode_%d" +#define MODULE_LPMODE_CPLD3_FORMAT "/sys/bus/i2c/devices/37-0062/module_lpmode_%d" + +#define NUM_OF_SFP_PORT 34 +static const int port_bus_index[NUM_OF_SFP_PORT] = { + 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, + 34, 35 +}; + +#define PORT_BUS_INDEX(port) (port_bus_index[port-1]) + +/************************************************************ + * + * SFPI Entry Points + * + ***********************************************************/ +int +onlp_sfpi_init(void) +{ + /* Called at initialization time */ + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_bitmap_get(onlp_sfp_bitmap_t* bmap) +{ + /* + * Ports {1, 34} + */ + int p; + + for(p = 1; p <= NUM_OF_SFP_PORT; p++) + AIM_BITMAP_SET(bmap, p); + + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_is_present(int port) +{ + /* + * Return 1 if present. + * Return 0 if not present. + * Return < 0 if error. + */ + + int present; + char *path = NULL; + + switch (port) { + case 1 ... 16: + path = MODULE_PRESENT_CPLD2_FORMAT; + break; + case 17 ... 34: + path = MODULE_PRESENT_CPLD3_FORMAT; + break; + default: + return ONLP_STATUS_E_INVALID; + } + + if (onlp_file_read_int(&present, path, port) < 0) { + AIM_LOG_ERROR("Unable to read present status from port(%d)\r\n", + port); + + return ONLP_STATUS_E_INTERNAL; + } + + return present; +} + +int +onlp_sfpi_eeprom_read(int port, uint8_t data[256]) +{ + /* + * Read the SFP eeprom into data[] + * + * Return MISSING if SFP is missing. + * Return OK if eeprom is read + */ + int size = 0; + memset(data, 0, 256); + + if(onlp_file_read(data, 256, &size, MODULE_EEPROM_FORMAT, PORT_BUS_INDEX(port)) + != ONLP_STATUS_OK) { + AIM_LOG_ERROR("Unable to read eeprom from port(%d)\r\n", port); + return ONLP_STATUS_E_INTERNAL; + } + + if (size != 256) { + AIM_LOG_ERROR("Unable to read eeprom from port(%d), size is different!\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_dom_read(int port, uint8_t data[256]) +{ + FILE* fp; + char file[64] = {0}; + + sprintf(file, MODULE_EEPROM_FORMAT, PORT_BUS_INDEX(port)); + fp = fopen(file, "r"); + if(fp == NULL) { + AIM_LOG_ERROR("Unable to open the eeprom device file of port(%d)", + port); + return ONLP_STATUS_E_INTERNAL; + } + + if (fseek(fp, 256, SEEK_CUR) != 0) { + fclose(fp); + AIM_LOG_ERROR("Unable to set the file position indicator of port(%d)", + port); + return ONLP_STATUS_E_INTERNAL; + } + + int ret = fread(data, 1, 256, fp); + fclose(fp); + if (ret != 256) { + AIM_LOG_ERROR("Unable to read the module_eeprom device file of port(%d)", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; +} + +int +onlp_sfpi_dev_readb(int port, uint8_t devaddr, uint8_t addr) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_readb(bus, devaddr, addr, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_dev_writeb(int port, uint8_t devaddr, uint8_t addr, uint8_t value) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_writeb(bus, devaddr, addr, value, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_dev_readw(int port, uint8_t devaddr, uint8_t addr) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_readw(bus, devaddr, addr, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_dev_writew(int port, uint8_t devaddr, uint8_t addr, uint16_t value) +{ + int bus = PORT_BUS_INDEX(port); + return onlp_i2c_writew(bus, devaddr, addr, value, ONLP_I2C_F_FORCE); +} + +int +onlp_sfpi_control_set(int port, onlp_sfp_control_t control, int value) +{ + char *path = NULL; + + switch(control) { + case ONLP_SFP_CONTROL_TX_DISABLE: { + VALIDATE_SFP(port); + + if (onlp_file_write_int(value, MODULE_TXDISABLE_FORMAT, port) + < 0) { + AIM_LOG_ERROR("Unable to set tx_disable status to port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_RESET_STATE: { + VALIDATE_QSFP(port); + + path = (port < 17) ? MODULE_RESET_CPLD2_FORMAT + : MODULE_RESET_CPLD3_FORMAT; + + if (onlp_file_write_int(value, path, port) < 0) { + AIM_LOG_ERROR("Unable to write reset status to port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_LP_MODE: { + VALIDATE_QSFP(port); + + path = (port < 17) ? MODULE_LPMODE_CPLD2_FORMAT + : MODULE_LPMODE_CPLD3_FORMAT; + + if (onlp_file_write_int(value, path, port) < 0) { + AIM_LOG_ERROR("Unable to write reset status to port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + default: + break; + } + + return ONLP_STATUS_E_UNSUPPORTED; +} + +int +onlp_sfpi_control_get(int port, onlp_sfp_control_t control, int* value) +{ + char *path = NULL; + + switch(control) { + case ONLP_SFP_CONTROL_RX_LOS: { + *value = 0; + if (port >= 33 && port <= 34) { + if (onlp_file_read_int(value, MODULE_RXLOS_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read rx_loss status from port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + } + + return ONLP_STATUS_OK; + } + + case ONLP_SFP_CONTROL_TX_FAULT: { + VALIDATE_SFP(port); + + if (onlp_file_read_int(value, MODULE_TXFAULT_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read tx_fault status from port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + + case ONLP_SFP_CONTROL_TX_DISABLE: { + VALIDATE_SFP(port); + + if (onlp_file_read_int(value, MODULE_TXDISABLE_FORMAT, port) < 0) { + AIM_LOG_ERROR("Unable to read tx_disabled status from port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_RESET_STATE: { + VALIDATE_QSFP(port); + + path = (port < 17) ? MODULE_RESET_CPLD2_FORMAT + : MODULE_RESET_CPLD3_FORMAT; + + if (onlp_file_read_int(value, path, port) < 0) { + AIM_LOG_ERROR("Unable to read reset status from port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + case ONLP_SFP_CONTROL_LP_MODE: { + VALIDATE_QSFP(port); + + path = (port < 17) ? MODULE_LPMODE_CPLD2_FORMAT + : MODULE_LPMODE_CPLD3_FORMAT; + + if (onlp_file_read_int(value, path, port) < 0) { + AIM_LOG_ERROR("Unable to read reset status from port(%d)\r\n", + port); + return ONLP_STATUS_E_INTERNAL; + } + + return ONLP_STATUS_OK; + } + default: + break; + } + + return ONLP_STATUS_E_UNSUPPORTED; +} + +int +onlp_sfpi_denit(void) +{ + return ONLP_STATUS_OK; +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/sysi.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/sysi.c new file mode 100644 index 000000000..9df5d53cf --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/sysi.c @@ -0,0 +1,183 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2017 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * + * + ***********************************************************/ +#include +#include + +#include +#include +#include +#include +#include +#include +#include "platform_lib.h" + +#include "x86_64_accton_as9737_32db_int.h" +#include "x86_64_accton_as9737_32db_log.h" + +#define NUM_OF_CPLD_VER 5 + +const char* onlp_sysi_platform_get(void) +{ + return "x86-64-accton-as9737-32db-r0"; +} + +int onlp_sysi_onie_data_get(uint8_t** data, int* size) +{ + uint8_t* rdata = aim_zmalloc(256); + if(onlp_file_read(rdata, 256, size, IDPROM_PATH) == ONLP_STATUS_OK) { + if(*size == 256) { + *data = rdata; + return ONLP_STATUS_OK; + } + } + + aim_free(rdata); + *size = 0; + return ONLP_STATUS_E_INTERNAL; +} + +int onlp_sysi_oids_get(onlp_oid_t* table, int max) +{ + int i; + onlp_oid_t* e = table; + memset(table, 0, max*sizeof(onlp_oid_t)); + + /* 5 Thermal sensors on the chassis */ + for (i = 1; i <= CHASSIS_THERMAL_COUNT; i++) + *e++ = ONLP_THERMAL_ID_CREATE(i); + + /* 5 LEDs on the chassis */ + for (i = 1; i <= CHASSIS_LED_COUNT; i++) + *e++ = ONLP_LED_ID_CREATE(i); + + /* 2 PSUs on the chassis */ + for (i = 1; i <= CHASSIS_PSU_COUNT; i++) + *e++ = ONLP_PSU_ID_CREATE(i); + + /* 6 Fans on the chassis */ + for (i = 1; i <= CHASSIS_FAN_COUNT; i++) + *e++ = ONLP_FAN_ID_CREATE(i); + + return 0; +} + +static char* cpld_ver_path[NUM_OF_CPLD_VER] = { + "/sys/devices/platform/as9737_32db_sys/cpu_ec_version", + "/sys/devices/platform/as9737_32db_sys/fpga_version", + "/sys/bus/i2c/devices/35-0061/version", /* CPLD-2 */ + "/sys/bus/i2c/devices/36-0062/version", /* CPLD-3 */ + "/sys/devices/platform/as9737_32db_fan/hwmon/hwmon*/version" /* Fan CPLD */ +}; + +int onlp_sysi_platform_info_get(onlp_platform_info_t* pi) +{ + int i, len, ret = ONLP_STATUS_OK; + char *v[NUM_OF_CPLD_VER] = {NULL}; + onlp_onie_info_t onie; + char *bios_ver = NULL; + char *bmc_buf = NULL; + char *aux_buf = NULL; + int bmc_major = 0, bmc_minor = 0; + unsigned int bmc_aux[4] = {0}; + char bmc_ver[16] = ""; + const char *bios = ""; + const char *onie_ver = ""; + + for (i = 0; i < AIM_ARRAYSIZE(cpld_ver_path); i++) { + if (i == 4) { + int hwmon_idx = onlp_get_fan_hwmon_idx(); + + if (hwmon_idx < 0) { + ret = ONLP_STATUS_E_INTERNAL; + break; + } + + len = onlp_file_read_str(&v[i], FAN_SYSFS_FORMAT_1, hwmon_idx, "version"); + } else { + len = onlp_file_read_str(&v[i], cpld_ver_path[i]); + } + + if (v[i] == NULL || len <= 0) { + ret = ONLP_STATUS_E_INTERNAL; + break; + } + } + + if (ret == ONLP_STATUS_OK) { + pi->cpld_versions = aim_fstrdup("\r\n\t CPU EC: %s" + "\r\n\t FPGA(0x60): %s" + "\r\n\t SMB CPLD(0x61): %s" + "\r\n\t SMB CPLD(0x62): %s" + "\r\n\t Fan CPLD(0x66): %s\r\n", + v[0], v[1], v[2], v[3], v[4]); + } + + for (i = 0; i < AIM_ARRAYSIZE(v); i++) + AIM_FREE_IF_PTR(v[i]); + + if ((onlp_file_read_str(&bmc_buf, BMC_VER1_PATH) >= 0) && + (onlp_file_read_str(&aux_buf, BMC_VER2_PATH) >= 0)) + { + bmc_buf[strcspn(bmc_buf, "\n")] = '\0'; + aux_buf[strcspn(aux_buf, "\n")] = '\0'; + + /* + * NOTE: The value in /sys/devices/platform/ipmi_bmc.0/firmware_revision is formatted + * using "%u.%x" in the kernel driver (see ipmi_msghandler.c::firmware_revision_show). + * The second field (after the dot) is output in hexadecimal format and must be parsed + * using "%x" from user-space. + */ + if (sscanf(bmc_buf, "%u.%x", &bmc_major, &bmc_minor) == 2 && + sscanf(aux_buf, "0x%x 0x%x 0x%x 0x%x", &bmc_aux[0], &bmc_aux[1], &bmc_aux[2], &bmc_aux[3]) == 4) + { + snprintf(bmc_ver, sizeof(bmc_ver), "%02X.%02X.%02X", + bmc_major, bmc_minor, bmc_aux[3]); + } + } + + if (onlp_file_read_str(&bios_ver, BIOS_VER_PATH) > 0) { + bios = bios_ver; + } + + if (onlp_onie_decode_file(&onie, IDPROM_PATH) >= 0) { + onie_ver = onie.onie_version; + } + + pi->other_versions = aim_fstrdup("\r\n\t BIOS: %s\r\n\t ONIE: %s\r\n\t BMC: %s", + bios, onie_ver, bmc_ver); + + AIM_FREE_IF_PTR(bmc_buf); + AIM_FREE_IF_PTR(aux_buf); + AIM_FREE_IF_PTR(bios_ver); + onlp_onie_info_free(&onie); + + return ret; +} + +void onlp_sysi_platform_info_free(onlp_platform_info_t* pi) +{ + aim_free(pi->cpld_versions); +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/thermali.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/thermali.c new file mode 100644 index 000000000..25d236268 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/thermali.c @@ -0,0 +1,323 @@ +/************************************************************ + * + * + * Copyright 2014 Big Switch Networks, Inc. + * Copyright 2014 Accton Technology Corporation. + * + * Licensed under the Eclipse Public License, Version 1.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.eclipse.org/legal/epl-v10.html + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the + * License. + * + * + ************************************************************ + * + * Thermal Sensor Platform Implementation. + * + ***********************************************************/ +#include +#include +#include "platform_lib.h" + +#define VALIDATE(_id) \ + do { \ + if(!ONLP_OID_IS_THERMAL(_id)) { \ + return ONLP_STATUS_E_INVALID; \ + } \ + } while(0) + +static char* devfiles__[] = { /* must map with onlp_thermal_id */ + NULL, + NULL, /* CPU_CORE files */ + "/sys/devices/platform/as9737_32db_thermal*temp1_input", + "/sys/devices/platform/as9737_32db_thermal*temp2_input", + "/sys/devices/platform/as9737_32db_thermal*temp3_input", + "/sys/devices/platform/as9737_32db_thermal*temp4_input", + "/sys/devices/platform/as9737_32db_thermal*temp5_input", + "/sys/devices/platform/as9737_32db_psu*psu1_temp1_input", + "/sys/devices/platform/as9737_32db_psu*psu1_temp2_input", + "/sys/devices/platform/as9737_32db_psu*psu1_temp3_input", + "/sys/devices/platform/as9737_32db_psu*psu2_temp1_input", + "/sys/devices/platform/as9737_32db_psu*psu2_temp2_input", + "/sys/devices/platform/as9737_32db_psu*psu2_temp3_input" +}; + +static char* cpu_coretemp_files[] = { + "/sys/devices/platform/coretemp.0*temp2_input", + "/sys/devices/platform/coretemp.0*temp3_input", + "/sys/devices/platform/coretemp.0*temp4_input", + "/sys/devices/platform/coretemp.0*temp5_input", + NULL, +}; + +#define AS9737_THERMAL_CAPS (ONLP_THERMAL_CAPS_GET_TEMPERATURE | \ + ONLP_THERMAL_CAPS_GET_WARNING_THRESHOLD | \ + ONLP_THERMAL_CAPS_GET_ERROR_THRESHOLD | \ + ONLP_THERMAL_CAPS_GET_SHUTDOWN_THRESHOLD) + +/* Static values */ +static onlp_thermal_info_t tinfo[] = { + { }, /* Not used */ + { { ONLP_THERMAL_ID_CREATE(THERMAL_CPU_CORE), "CPU Core", 0}, + ONLP_THERMAL_STATUS_PRESENT, + AS9737_THERMAL_CAPS, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_MAIN_BROAD), "MB_FrontCenter_temp(0x48)", 0}, + ONLP_THERMAL_STATUS_PRESENT, + AS9737_THERMAL_CAPS, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_MAIN_BROAD), "MB_FrontRight_temp(0x49)", 0}, + ONLP_THERMAL_STATUS_PRESENT, + AS9737_THERMAL_CAPS, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_3_ON_MAIN_BROAD), "MB_RearCenter_temp(0x4A)", 0}, + ONLP_THERMAL_STATUS_PRESENT, + AS9737_THERMAL_CAPS, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_4_ON_MAIN_BROAD), "MB_RearLeft_temp(0x4C)", 0}, + ONLP_THERMAL_STATUS_PRESENT, + AS9737_THERMAL_CAPS, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_5_ON_MAIN_BROAD), "MB_RearCenter_temp(0x4F)", 0}, + ONLP_THERMAL_STATUS_PRESENT, + AS9737_THERMAL_CAPS, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_PSU1), "PSU-1 Thermal Sensor 1", + ONLP_PSU_ID_CREATE(PSU1_ID)}, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_PSU1), "PSU-1 Thermal Sensor 2", + ONLP_PSU_ID_CREATE(PSU1_ID)}, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_3_ON_PSU1), "PSU-1 Thermal Sensor 3", + ONLP_PSU_ID_CREATE(PSU1_ID)}, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_1_ON_PSU2), "PSU-2 Thermal Sensor 1", + ONLP_PSU_ID_CREATE(PSU2_ID)}, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_2_ON_PSU2), "PSU-2 Thermal Sensor 2", + ONLP_PSU_ID_CREATE(PSU2_ID)}, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + }, + { { ONLP_THERMAL_ID_CREATE(THERMAL_3_ON_PSU2), "PSU-2 Thermal Sensor 3", + ONLP_PSU_ID_CREATE(PSU2_ID)}, + ONLP_THERMAL_STATUS_PRESENT, + ONLP_THERMAL_CAPS_ALL, 0, ONLP_THERMAL_THRESHOLD_INIT_DEFAULTS + } +}; + +typedef struct threshold_t { + int warning; + int error; + int shutdown; +} threshold_t; + +threshold_t threshold[FAN_DIR_COUNT][THERMAL_COUNT-(CHASSIS_PSU_COUNT*NUM_OF_THERMAL_PER_PSU)] = { + [FAN_DIR_F2B][THERMAL_CPU_CORE].warning = 85000, + [FAN_DIR_F2B][THERMAL_CPU_CORE].error = 95000, + [FAN_DIR_F2B][THERMAL_CPU_CORE].shutdown = 100000, + [FAN_DIR_F2B][THERMAL_1_ON_MAIN_BROAD].warning = 65000, + [FAN_DIR_F2B][THERMAL_1_ON_MAIN_BROAD].error = 70000, + [FAN_DIR_F2B][THERMAL_1_ON_MAIN_BROAD].shutdown = 85000, + [FAN_DIR_F2B][THERMAL_2_ON_MAIN_BROAD].warning = 65000, + [FAN_DIR_F2B][THERMAL_2_ON_MAIN_BROAD].error = 70000, + [FAN_DIR_F2B][THERMAL_2_ON_MAIN_BROAD].shutdown = 85000, + [FAN_DIR_F2B][THERMAL_3_ON_MAIN_BROAD].warning = 65000, + [FAN_DIR_F2B][THERMAL_3_ON_MAIN_BROAD].error = 70000, + [FAN_DIR_F2B][THERMAL_3_ON_MAIN_BROAD].shutdown = 85000, + [FAN_DIR_F2B][THERMAL_4_ON_MAIN_BROAD].warning = 65000, + [FAN_DIR_F2B][THERMAL_4_ON_MAIN_BROAD].error = 70000, + [FAN_DIR_F2B][THERMAL_4_ON_MAIN_BROAD].shutdown = 85000, + [FAN_DIR_F2B][THERMAL_5_ON_MAIN_BROAD].warning = 65000, + [FAN_DIR_F2B][THERMAL_5_ON_MAIN_BROAD].error = 70000, + [FAN_DIR_F2B][THERMAL_5_ON_MAIN_BROAD].shutdown = 85000, + + [FAN_DIR_B2F][THERMAL_CPU_CORE].warning = 85000, + [FAN_DIR_B2F][THERMAL_CPU_CORE].error = 95000, + [FAN_DIR_B2F][THERMAL_CPU_CORE].shutdown = 100000, + [FAN_DIR_B2F][THERMAL_1_ON_MAIN_BROAD].warning = 55000, + [FAN_DIR_B2F][THERMAL_1_ON_MAIN_BROAD].error = 60000, + [FAN_DIR_B2F][THERMAL_1_ON_MAIN_BROAD].shutdown = 75000, + [FAN_DIR_B2F][THERMAL_2_ON_MAIN_BROAD].warning = 55000, + [FAN_DIR_B2F][THERMAL_2_ON_MAIN_BROAD].error = 60000, + [FAN_DIR_B2F][THERMAL_2_ON_MAIN_BROAD].shutdown = 75000, + [FAN_DIR_B2F][THERMAL_3_ON_MAIN_BROAD].warning = 55000, + [FAN_DIR_B2F][THERMAL_3_ON_MAIN_BROAD].error = 60000, + [FAN_DIR_B2F][THERMAL_3_ON_MAIN_BROAD].shutdown = 75000, + [FAN_DIR_B2F][THERMAL_4_ON_MAIN_BROAD].warning = 55000, + [FAN_DIR_B2F][THERMAL_4_ON_MAIN_BROAD].error = 60000, + [FAN_DIR_B2F][THERMAL_4_ON_MAIN_BROAD].shutdown = 75000, + [FAN_DIR_B2F][THERMAL_5_ON_MAIN_BROAD].warning = 55000, + [FAN_DIR_B2F][THERMAL_5_ON_MAIN_BROAD].error = 60000, + [FAN_DIR_B2F][THERMAL_5_ON_MAIN_BROAD].shutdown = 75000, +}; + +threshold_t psu_threshold[NUM_OF_PSU_TYPE][NUM_OF_THERMAL_PER_PSU] = { + [PSU_TYPE_AC_FSJ001_610G_F2B][0].warning = 72000, + [PSU_TYPE_AC_FSJ001_610G_F2B][0].error = 77000, + [PSU_TYPE_AC_FSJ001_610G_F2B][0].shutdown = 80000, + [PSU_TYPE_AC_FSJ001_610G_F2B][1].warning = 105000, + [PSU_TYPE_AC_FSJ001_610G_F2B][1].error = 110000, + [PSU_TYPE_AC_FSJ001_610G_F2B][1].shutdown = 113000, + [PSU_TYPE_AC_FSJ001_610G_F2B][2].warning = 109000, + [PSU_TYPE_AC_FSJ001_610G_F2B][2].error = 114000, + [PSU_TYPE_AC_FSJ001_610G_F2B][2].shutdown = 117000, + [PSU_TYPE_AC_FSJ001_612G_F2B][0].warning = 72000, + [PSU_TYPE_AC_FSJ001_612G_F2B][0].error = 77000, + [PSU_TYPE_AC_FSJ001_612G_F2B][0].shutdown = 80000, + [PSU_TYPE_AC_FSJ001_612G_F2B][1].warning = 105000, + [PSU_TYPE_AC_FSJ001_612G_F2B][1].error = 110000, + [PSU_TYPE_AC_FSJ001_612G_F2B][1].shutdown = 113000, + [PSU_TYPE_AC_FSJ001_612G_F2B][2].warning = 109000, + [PSU_TYPE_AC_FSJ001_612G_F2B][2].error = 114000, + [PSU_TYPE_AC_FSJ001_612G_F2B][2].shutdown = 117000, + [PSU_TYPE_AC_FSJ004_612G_B2F][0].warning = 72000, + [PSU_TYPE_AC_FSJ004_612G_B2F][0].error = 77000, + [PSU_TYPE_AC_FSJ004_612G_B2F][0].shutdown = 80000, + [PSU_TYPE_AC_FSJ004_612G_B2F][1].warning = 105000, + [PSU_TYPE_AC_FSJ004_612G_B2F][1].error = 110000, + [PSU_TYPE_AC_FSJ004_612G_B2F][1].shutdown = 113000, + [PSU_TYPE_AC_FSJ004_612G_B2F][2].warning = 109000, + [PSU_TYPE_AC_FSJ004_612G_B2F][2].error = 114000, + [PSU_TYPE_AC_FSJ004_612G_B2F][2].shutdown = 117000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][0].warning = 65000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][0].error = 70000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][0].shutdown = 75000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][1].warning = 112000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][1].error = 117000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][1].shutdown = 120000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][2].warning = 95000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][2].error = 100000, + [PSU_TYPE_DC48_FSJ035_610G_F2B][2].shutdown = 110000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][0].warning = 65000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][0].error = 70000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][0].shutdown = 75000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][1].warning = 112000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][1].error = 117000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][1].shutdown = 120000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][2].warning = 95000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][2].error = 100000, + [PSU_TYPE_DC48_FSJ036_610G_B2F][2].shutdown = 110000, +}; + +static int +onlp_thermali_psu_threshold_get(onlp_oid_t id, onlp_thermal_info_t* info) +{ + int ret = ONLP_STATUS_OK; + int thermal_id; + int psu_id; + psu_type_t psu_type; + VALIDATE(id); + + thermal_id = ONLP_OID_ID_GET(id); + psu_id = ((thermal_id - THERMAL_1_ON_PSU1) / NUM_OF_THERMAL_PER_PSU) + 1; + + /* Get PSU type + */ + psu_type = get_psu_type(psu_id, NULL, 0); + + switch (psu_type) { + case PSU_TYPE_AC_FSJ001_610G_F2B: + case PSU_TYPE_AC_FSJ001_612G_F2B: + case PSU_TYPE_AC_FSJ004_612G_B2F: + case PSU_TYPE_DC48_FSJ035_610G_F2B: + case PSU_TYPE_DC48_FSJ036_610G_B2F: + info->thresholds.warning = psu_threshold[psu_type][(thermal_id - THERMAL_1_ON_PSU1) % NUM_OF_THERMAL_PER_PSU].warning; + info->thresholds.error = psu_threshold[psu_type][(thermal_id - THERMAL_1_ON_PSU1) % NUM_OF_THERMAL_PER_PSU].error; + info->thresholds.shutdown = psu_threshold[psu_type][(thermal_id - THERMAL_1_ON_PSU1) % NUM_OF_THERMAL_PER_PSU].shutdown; + break; + default: + ret = ONLP_STATUS_E_UNSUPPORTED; + break; + } + + return ret; +} + +/* + * This will be called to intiialize the thermali subsystem. + */ +int +onlp_thermali_init(void) +{ + return ONLP_STATUS_OK; +} + +/* + * Retrieve the information structure for the given thermal OID. + * + * If the OID is invalid, return ONLP_E_STATUS_INVALID. + * If an unexpected error occurs, return ONLP_E_STATUS_INTERNAL. + * Otherwise, return ONLP_STATUS_OK with the OID's information. + * + * Note -- it is expected that you fill out the information + * structure even if the sensor described by the OID is not present. + */ +int +onlp_thermali_info_get(onlp_oid_t id, onlp_thermal_info_t* info) +{ + int tid; + int psu_id, psu_tid_start = 0; + int val = 0; + int ret = 0; + int i; + enum onlp_fan_dir dir = FAN_DIR_F2B;; + + VALIDATE(id); + tid = ONLP_OID_ID_GET(id); + + for (i = 1; i < CHASSIS_FAN_COUNT+1; i++) { + dir = onlp_get_fan_dir(i); + + if (dir == FAN_DIR_B2F) + break; + } + + if( (tid >= THERMAL_CPU_CORE) && (tid <= THERMAL_5_ON_MAIN_BROAD)) { + /* Set the onlp_oid_hdr_t and capabilities */ + *info = tinfo[tid]; + info->thresholds.warning = threshold[dir][tid].warning; + info->thresholds.error = threshold[dir][tid].error; + info->thresholds.shutdown = threshold[dir][tid].shutdown; + } else if ((tid >= THERMAL_1_ON_PSU1) && (tid <= THERMAL_3_ON_PSU2)) { + *info = tinfo[tid]; + onlp_thermali_psu_threshold_get(id, info); + } + + if (tid == THERMAL_CPU_CORE) + return onlp_file_read_int_max(&info->mcelsius, cpu_coretemp_files); + + psu_tid_start = CHASSIS_THERMAL_COUNT + 1; + + if (tid >= psu_tid_start) { + psu_id = ( tid <= THERMAL_3_ON_PSU1 ) ? PSU1_ID : PSU2_ID; + /* Get power good status */ + ret = onlp_file_read_int(&val, PSU_SYSFS_FORMAT, psu_id, "power_good"); + if (ret < 0) { + AIM_LOG_ERROR("Unable to read status from (PSU_SYSFS_FORMAT)\r\n", psu_id, "power_good"); + return ONLP_STATUS_E_INTERNAL; + } + + if(val != PSU_STATUS_POWER_GOOD) { + info->status |= ONLP_THERMAL_STATUS_FAILED; + } + } + + return onlp_file_read_int(&info->mcelsius, devfiles__[tid]); +} diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_config.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_config.c new file mode 100644 index 000000000..fca52785a --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_config.c @@ -0,0 +1,80 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +/* */ +#define __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(_x) #_x +#define __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(_x) __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(_x) +x86_64_accton_as9737_32db_config_settings_t x86_64_accton_as9737_32db_config_settings[] = +{ +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_LOGGING(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_STDLIB(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_PORTING_INCLUDE_STDLIB_HEADERS(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_UCLI(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif +#ifdef X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION + { __x86_64_accton_as9737_32db_config_STRINGIFY_NAME(X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION), __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE(X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION) }, +#else +{ X86_64_ACCTON_AS9737_32DB_CONFIG_INCLUDE_DEFAULT_FAN_DIRECTION(__x86_64_accton_as9737_32db_config_STRINGIFY_NAME), "__undefined__" }, +#endif + { NULL, NULL } +}; +#undef __x86_64_accton_as9737_32db_config_STRINGIFY_VALUE +#undef __x86_64_accton_as9737_32db_config_STRINGIFY_NAME + +const char* +x86_64_accton_as9737_32db_config_lookup(const char* setting) +{ + int i; + for(i = 0; x86_64_accton_as9737_32db_config_settings[i].name; i++) { + if(!strcmp(x86_64_accton_as9737_32db_config_settings[i].name, setting)) { + return x86_64_accton_as9737_32db_config_settings[i].value; + } + } + return NULL; +} + +int +x86_64_accton_as9737_32db_config_show(struct aim_pvs_s* pvs) +{ + int i; + for(i = 0; x86_64_accton_as9737_32db_config_settings[i].name; i++) { + aim_printf(pvs, "%s = %s\n", x86_64_accton_as9737_32db_config_settings[i].name, x86_64_accton_as9737_32db_config_settings[i].value); + } + return i; +} + +/* */ \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_enums.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_enums.c new file mode 100644 index 000000000..6bf1b64f6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_enums.c @@ -0,0 +1,9 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +/* <--auto.start.enum(ALL).source> */ +/* */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_int.h b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_int.h new file mode 100644 index 000000000..b516852df --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_int.h @@ -0,0 +1,11 @@ +/**************************************************************************//** + * + * x86_64_accton_as9737_32db Internal Header + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9737_32db_INT_H__ +#define __x86_64_accton_as9737_32db_INT_H__ + +#include + +#endif /* __x86_64_accton_as9737_32db_INT_H__ */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_log.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_log.c new file mode 100644 index 000000000..df0e8d2b0 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_log.c @@ -0,0 +1,17 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +#include "x86_64_accton_as9737_32db_log.h" +/* + * x86_64_accton_as9737_32db log struct. + */ +AIM_LOG_STRUCT_DEFINE( + X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_OPTIONS_DEFAULT, + X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_BITS_DEFAULT, + NULL, /* Custom log map */ + X86_64_ACCTON_AS9737_32DB_CONFIG_LOG_CUSTOM_BITS_DEFAULT + ); diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_log.h b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_log.h new file mode 100644 index 000000000..429708a42 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_log.h @@ -0,0 +1,12 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#ifndef __x86_64_accton_as9737_32db_LOG_H__ +#define __x86_64_accton_as9737_32db_LOG_H__ + +#define AIM_LOG_MODULE_NAME x86_64_accton_as9737_32db +#include + +#endif /* __x86_64_accton_as9737_32db_LOG_H__ */ diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_module.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_module.c new file mode 100644 index 000000000..ab4d98ec1 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_module.c @@ -0,0 +1,24 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +#include "x86_64_accton_as9737_32db_log.h" + +static int +datatypes_init__(void) +{ +#define x86_64_accton_as9737_32db_ENUMERATION_ENTRY(_enum_name, _desc) AIM_DATATYPE_MAP_REGISTER(_enum_name, _enum_name##_map, _desc, AIM_LOG_INTERNAL); +#include + return 0; +} + +void __x86_64_accton_as9737_32db_module_init__(void) +{ + AIM_LOG_STRUCT_REGISTER(); + datatypes_init__(); +} + +int __onlp_platform_version__ = 1; diff --git a/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_ucli.c b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_ucli.c new file mode 100644 index 000000000..6670c0002 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/onlp/builds/x86_64_accton_as9737_32db/module/src/x86_64_accton_as9737_32db_ucli.c @@ -0,0 +1,49 @@ +/**************************************************************************//** + * + * + * + *****************************************************************************/ +#include + +#if x86_64_accton_as9737_32db_CONFIG_INCLUDE_UCLI == 1 + +#include +#include +#include + +static ucli_status_t +x86_64_accton_as9737_32db_ucli_ucli__config__(ucli_context_t* uc) +{ + UCLI_HANDLER_MACRO_MODULE_CONFIG(x86_64_accton_as9737_32db) +} + +/* */ +/* */ + +static ucli_module_t +x86_64_accton_as9737_32db_ucli_module__ = + { + "x86_64_accton_as9737_32db_ucli", + NULL, + x86_64_accton_as9737_32db_ucli_ucli_handlers__, + NULL, + NULL, + }; + +ucli_node_t* +x86_64_accton_as9737_32db_ucli_node_create(void) +{ + ucli_node_t* n; + ucli_module_init(&x86_64_accton_as9737_32db_ucli_module__); + n = ucli_node_create("x86_64_accton_as9737_32db", NULL, &x86_64_accton_as9737_32db_ucli_module__); + ucli_node_subnode_add(n, ucli_module_log_node_create("x86_64_accton_as9737_32db")); + return n; +} + +#else +void* +x86_64_accton_as9737_32db_ucli_node_create(void) +{ + return NULL; +} +#endif diff --git a/packages/platforms/accton/x86-64/as9737-32db/platform-config/Makefile b/packages/platforms/accton/x86-64/as9737-32db/platform-config/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/platform-config/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/Makefile b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/Makefile new file mode 100644 index 000000000..003238cf6 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/Makefile @@ -0,0 +1 @@ +include $(ONL)/make/pkg.mk \ No newline at end of file diff --git a/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/PKG.yml b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/PKG.yml new file mode 100644 index 000000000..68e127fd7 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/PKG.yml @@ -0,0 +1 @@ +!include $ONL_TEMPLATES/platform-config-platform.yml ARCH=amd64 VENDOR=accton BASENAME=x86-64-accton-as9737-32db REVISION=r0 diff --git a/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/src/lib/x86-64-accton-as9737-32db-r0.yml b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/src/lib/x86-64-accton-as9737-32db-r0.yml new file mode 100644 index 000000000..2d30c68d7 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/src/lib/x86-64-accton-as9737-32db-r0.yml @@ -0,0 +1,32 @@ +--- + +###################################################################### +# +# platform-config for AS9737 +# +###################################################################### + +x86-64-accton-as9737-32db-r0: + + grub: + + serial: >- + --port=0x3f8 + --speed=115200 + --word=8 + --parity=no + --stop=1 + + kernel: + <<: *kernel-6-1 + + args: >- + console=ttyS0,115200n8 + intel_iommu=pt + pcie_aspm=off + + ##network: + ## interfaces: + ## ma1: + ## name: ~ + ## syspath: pci0000:00/0000:00:1c.0/0000:0a:00.0 diff --git a/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/src/python/x86_64_accton_as9737_32db_r0/__init__.py b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/src/python/x86_64_accton_as9737_32db_r0/__init__.py new file mode 100644 index 000000000..1dc2ea195 --- /dev/null +++ b/packages/platforms/accton/x86-64/as9737-32db/platform-config/r0/src/python/x86_64_accton_as9737_32db_r0/__init__.py @@ -0,0 +1,134 @@ +import commands +import time +from itertools import chain +from onl.platform.base import * +from onl.platform.accton import * + +init_ipmi_dev = [ + 'echo "remove,kcs,i/o,0xca2" > /sys/module/ipmi_si/parameters/hotmod', + 'echo "add,kcs,i/o,0xca2" > /sys/module/ipmi_si/parameters/hotmod' +] + +# ONL may boot more faster than BMC, so retries are needed. +# But if the waiting time runs out, there may be something wrong with BMC, +# then ONL gives up waiting. +ATTEMPTS = 5 +INTERVAL = 3 + +def init_ipmi_dev_intf(): + attempts = ATTEMPTS + interval = INTERVAL + + while attempts: + if os.path.exists('/dev/ipmi0') or os.path.exists('/dev/ipmidev/0'): + return (True, (ATTEMPTS - attempts) * interval) + + for i in range(0, len(init_ipmi_dev)): + commands.getstatusoutput(init_ipmi_dev[i]) + + attempts -= 1 + time.sleep(interval) + + return (False, ATTEMPTS * interval) + +def init_ipmi_oem_cmd(): + attempts = ATTEMPTS + interval = INTERVAL + + while attempts: + # to see if BMC is scanning + status, output = commands.getstatusoutput('ipmitool raw 0x34 0x95') + if status: + attempts -= 1 + time.sleep(interval) + continue + + return (True, (ATTEMPTS - attempts) * interval) + + return (False, ATTEMPTS * interval) + +def init_ipmi(): + attempts = ATTEMPTS + interval = 60 + + while attempts: + attempts -= 1 + + (status, elapsed_dev) = init_ipmi_dev_intf() + if status is not True: + time.sleep(interval - elapsed_dev) + continue + + (status, elapsed_oem) = init_ipmi_oem_cmd() + if status is not True: + time.sleep(interval - elapsed_dev - elapsed_oem) + continue + + print('IPMI dev interface is ready.') + return True + + print('Failed to initialize IPMI dev interface') + return False + +def get_i2c_bus_num_offset(): + cmd = 'cat /sys/bus/i2c/devices/i2c-0/name' + process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + return 1 if b'iSMT' in stdout else 0 + +class OnlPlatform_x86_64_accton_as9737_32db_r0(OnlPlatformAccton, + OnlPlatformPortConfig_32x400_1x10_1x1): + PLATFORM='x86-64-accton-as9737-32db-r0' + MODEL="AS9737-32DB" + SYS_OBJECT_ID=".9737.32" + + def modprobe(self, module, required=True, params={}): + cmd = "modprobe %s" % module + subprocess.check_call(cmd, shell=True) + + def baseconfig(self): + if init_ipmi() is not True: + return False + + self.modprobe('optoe') + self.modprobe('at24') + self.insmod('accton_ipmi_intf') + + for m in [ 'i2c-ocores', 'fpga', 'mux', 'cpld', 'fan', 'psu', 'thermal', 'sys', 'leds' ]: + self.insmod("x86-64-accton-as9737-32db-%s.ko" % m) + + bus_offset = get_i2c_bus_num_offset() + + ########### initialize I2C bus 0 ########### + self.new_i2c_devices( + [ + # initialize multiplexer (PCA9548) + ('as9737_32db_mux', 0x77, 0+bus_offset), + + # initialize CPLDs + ('as9737_32db_cpld2', 0x61, 36), + ('as9737_32db_cpld3', 0x62, 37), + + # EEPROM + #('24c02', 0x56, 0), + ] + ) + + # initialize SFP devices + for port in range(1, 17): + subprocess.call('echo 0 > /sys/bus/i2c/devices/36-0061/module_reset_%d' % (port), shell=True) + + for port in range(17, 33): + subprocess.call('echo 0 > /sys/bus/i2c/devices/37-0062/module_reset_%d' % (port), shell=True) + + sfp_bus = [ + 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, + 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, + 34, 35 + ] + + for port in range(1, len(sfp_bus)+1): + self.new_i2c_device('optoe3' if (port <= 32) else 'optoe2', 0x50, sfp_bus[port-1]) + subprocess.call('echo port%d > /sys/bus/i2c/devices/%d-0050/port_name' % (port, sfp_bus[port-1]), shell=True) + + return True