pd: refactor tcpm and move alert function to tcpm driver

Refactor the tcpm/tcpc split such that the tcpm driver implements
the alert functionality since it may be unique for different tcpc
chips.

BUG=chrome-os-partner:41842
BRANCH=none
TEST=make -j buildall. run on samus and glados.

Change-Id: I23f2d7f8627d5337b8d001a09bf27622be24fe33
Signed-off-by: Alec Berg <alecaberg@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/281631
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
This commit is contained in:
Alec Berg
2015-06-23 17:52:04 -07:00
committed by ChromeOS Commit Bot
parent 59576398db
commit 82ec2510a3
8 changed files with 265 additions and 192 deletions

View File

@@ -5,14 +5,14 @@
/* TCPM for MCU also running TCPC */
#include "task.h"
#include "tcpci.h"
#include "usb_pd.h"
#include "usb_pd_tcpm.h"
#include "console.h"
extern int tcpc_alert_status(int port, uint16_t *alert);
extern int tcpc_alert_status(int port, int *alert);
extern int tcpc_alert_status_clear(int port, uint16_t mask);
extern int tcpc_alert_mask_update(int port, uint16_t mask);
extern int tcpc_alert_mask_set(int port, uint16_t mask);
extern int tcpc_get_cc(int port, int *cc1, int *cc2);
extern int tcpc_set_cc(int port, int pull);
extern int tcpc_set_polarity(int port, int polarity);
@@ -24,10 +24,28 @@ extern int tcpc_get_message(int port, uint32_t *payload, int *head);
extern int tcpc_transmit(int port, enum tcpm_transmit_type type,
uint16_t header, const uint32_t *data);
static int init_alert_mask(int port)
{
uint16_t mask;
int rv;
/*
* Create mask of alert events that will cause the TCPC to
* signal the TCPM via the Alert# gpio line.
*/
mask = TCPC_REG_ALERT_TX_SUCCESS | TCPC_REG_ALERT_TX_FAILED |
TCPC_REG_ALERT_TX_DISCARDED | TCPC_REG_ALERT_RX_STATUS |
TCPC_REG_ALERT_RX_HARD_RST | TCPC_REG_ALERT_CC_STATUS;
/* Set the alert mask in TCPC */
rv = tcpm_alert_mask_set(port, mask);
return rv;
}
int tcpm_init(int port)
{
tcpc_init(port);
return EC_SUCCESS;
return init_alert_mask(port);
}
int tcpm_get_cc(int port, int *cc1, int *cc2)
@@ -55,7 +73,7 @@ int tcpm_set_msg_header(int port, int power_role, int data_role)
return tcpc_set_msg_header(port, power_role, data_role);
}
int tcpm_alert_status(int port, int alert_reg, uint16_t *alert)
int tcpm_alert_status(int port, int *alert)
{
int rv;
@@ -71,9 +89,9 @@ int tcpm_set_rx_enable(int port, int enable)
return tcpc_set_rx_enable(port, enable);
}
int tcpm_alert_mask_set(int port, int reg, uint16_t mask)
int tcpm_alert_mask_set(int port, uint16_t mask)
{
return tcpc_alert_mask_update(port, mask);
return tcpc_alert_mask_set(port, mask);
}
int tcpm_get_message(int port, uint32_t *payload, int *head)
@@ -86,3 +104,33 @@ int tcpm_transmit(int port, enum tcpm_transmit_type type, uint16_t header,
{
return tcpc_transmit(port, type, header, data);
}
void tcpc_alert(int port)
{
int status;
/* Read the Alert register from the TCPC */
tcpm_alert_status(port, &status);
if (status & TCPC_REG_ALERT_CC_STATUS) {
/* CC status changed, wake task */
task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_CC, 0);
}
if (status & TCPC_REG_ALERT_RX_STATUS) {
/*
* message received. since TCPC is compiled in, we
* already received PD_EVENT_RX from phy layer in
* pd_rx_event(), so we don't need to set another
* event.
*/
}
if (status & TCPC_REG_ALERT_RX_HARD_RST) {
/* hard reset received */
pd_execute_hard_reset(port);
task_wake(PD_PORT_TO_TASK_ID(port));
}
if (status & TCPC_REG_ALERT_TX_COMPLETE) {
/* transmit complete */
pd_transmit_complete(port, status & TCPC_REG_ALERT_TX_COMPLETE);
}
}

View File

@@ -6,20 +6,37 @@
/* Type-C port manager */
#include "i2c.h"
#include "task.h"
#include "tcpci.h"
#include "timer.h"
#include "usb_pd.h"
#include "usb_pd_tcpc.h"
#include "usb_pd_tcpm.h"
#include "util.h"
#include "console.h"
/* Convert port number to tcpc i2c address */
#define I2C_ADDR_TCPC(p) (CONFIG_TCPC_I2C_BASE_ADDR + 2*(p))
static int tcpc_polarity, tcpc_vconn;
static int init_alert_mask(int port)
{
uint16_t mask;
int rv;
/*
* Create mask of alert events that will cause the TCPC to
* signal the TCPM via the Alert# gpio line.
*/
mask = TCPC_REG_ALERT_TX_SUCCESS | TCPC_REG_ALERT_TX_FAILED |
TCPC_REG_ALERT_TX_DISCARDED | TCPC_REG_ALERT_RX_STATUS |
TCPC_REG_ALERT_RX_HARD_RST | TCPC_REG_ALERT_CC_STATUS;
/* Set the alert mask in TCPC */
rv = tcpm_alert_mask_set(port, mask);
return rv;
}
int tcpm_init(int port)
{
int rv, vid = 0;
@@ -32,7 +49,7 @@ int tcpm_init(int port)
* is complete
*/
if (rv == EC_SUCCESS && vid)
return rv;
return init_alert_mask(port);
msleep(10);
}
}
@@ -92,19 +109,19 @@ int tcpm_set_msg_header(int port, int power_role, int data_role)
TCPC_REG_MSG_HDR_INFO_SET(data_role, power_role));
}
int tcpm_alert_status(int port, int alert_reg, uint16_t *alert)
int tcpm_alert_status(int port, int *alert)
{
int rv;
/* Read TCPC Alert register */
rv = i2c_read16(I2C_PORT_TCPC, I2C_ADDR_TCPC(port),
alert_reg, (int *)alert);
TCPC_REG_ALERT, alert);
/*
* The PD protocol layer will process all alert bits
* returned by this function. Therefore, these bits
* can now be cleared from the TCPC register.
*/
i2c_write16(I2C_PORT_TCPC, I2C_ADDR_TCPC(port),
alert_reg, *alert);
TCPC_REG_ALERT, *alert);
return rv;
}
@@ -116,12 +133,12 @@ int tcpm_set_rx_enable(int port, int enable)
enable ? TCPC_REG_RX_DETECT_SOP_HRST_MASK : 0);
}
int tcpm_alert_mask_set(int port, int reg, uint16_t mask)
int tcpm_alert_mask_set(int port, uint16_t mask)
{
int rv;
/* write to the Alert Mask register */
rv = i2c_write16(I2C_PORT_TCPC, I2C_ADDR_TCPC(port),
reg, mask);
TCPC_REG_ALERT_MASK, mask);
if (rv)
return rv;
@@ -192,3 +209,29 @@ int tcpm_transmit(int port, enum tcpm_transmit_type type, uint16_t header,
return rv;
}
void tcpc_alert(int port)
{
int status;
/* Read the Alert register from the TCPC */
tcpm_alert_status(port, &status);
if (status & TCPC_REG_ALERT_CC_STATUS) {
/* CC status changed, wake task */
task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_CC, 0);
}
if (status & TCPC_REG_ALERT_RX_STATUS) {
/* message received */
task_set_event(PD_PORT_TO_TASK_ID(port), PD_EVENT_RX, 0);
}
if (status & TCPC_REG_ALERT_RX_HARD_RST) {
/* hard reset received */
pd_execute_hard_reset(port);
task_wake(PD_PORT_TO_TASK_ID(port));
}
if (status & TCPC_REG_ALERT_TX_COMPLETE) {
/* transmit complete */
pd_transmit_complete(port, status & TCPC_REG_ALERT_TX_COMPLETE);
}
}

85
driver/tcpm/tcpci.h Normal file
View File

@@ -0,0 +1,85 @@
/* Copyright 2015 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* USB Power delivery port management */
#ifndef __CROS_EC_USB_PD_TCPM_TCPCI_H
#define __CROS_EC_USB_PD_TCPM_TCPCI_H
#define TCPC_REG_VENDOR_ID 0x0
#define TCPC_REG_PRODUCT_ID 0x2
#define TCPC_REG_BCD_DEV 0x4
#define TCPC_REG_TC_REV 0x6
#define TCPC_REG_PD_REV 0x8
#define TCPC_REG_PD_INT_REV 0xa
#define TCPC_REG_DEV_CAP_1 0xc
#define TCPC_REG_DEV_CAP_2 0xd
#define TCPC_REG_DEV_CAP_3 0xe
#define TCPC_REG_DEV_CAP_4 0xf
#define TCPC_REG_ALERT 0x10
#define TCPC_REG_ALERT_GPIO_CHANGE (1<<10)
#define TCPC_REG_ALERT_V_ALARM_LO (1<<9)
#define TCPC_REG_ALERT_V_ALARM_HI (1<<8)
#define TCPC_REG_ALERT_SLEEP_EXITED (1<<7)
#define TCPC_REG_ALERT_POWER_STATUS (1<<6)
#define TCPC_REG_ALERT_CC_STATUS (1<<5)
#define TCPC_REG_ALERT_RX_STATUS (1<<4)
#define TCPC_REG_ALERT_RX_HARD_RST (1<<3)
#define TCPC_REG_ALERT_TX_SUCCESS (1<<2)
#define TCPC_REG_ALERT_TX_DISCARDED (1<<1)
#define TCPC_REG_ALERT_TX_FAILED (1<<0)
#define TCPC_REG_ALERT_TX_COMPLETE (TCPC_REG_ALERT_TX_SUCCESS | \
TCPC_REG_ALERT_TX_DISCARDED | \
TCPC_REG_ALERT_TX_FAILED)
#define TCPC_REG_ALERT_MASK 0x12
#define TCPC_REG_POWER_STATUS_MASK 0x14
#define TCPC_REG_CC1_STATUS 0x16
#define TCPC_REG_CC2_STATUS 0x17
#define TCPC_REG_CC_STATUS_SET(term, volt) \
((term) << 3 | volt)
#define TCPC_REG_CC_STATUS_TERM(reg) (((reg) & 0x38) >> 3)
#define TCPC_REG_CC_STATUS_VOLT(reg) ((reg) & 0x7)
#define TCPC_REG_POWER_STATUS 0x1a
#define TCPC_REG_ROLE_CTRL 0x1b
#define TCPC_REG_ROLE_CTRL_SET(drp, rp, cc2, cc1) \
((drp) << 6 | (rp) << 4 | (cc2) << 2 | (cc1))
#define TCPC_REG_ROLE_CTRL_CC2(reg) (((reg) & 0xc) >> 2)
#define TCPC_REG_ROLE_CTRL_CC1(reg) ((reg) & 0x3)
#define TCPC_REG_POWER_PATH_CTRL 0x1c
#define TCPC_REG_POWER_CTRL 0x1d
#define TCPC_REG_POWER_CTRL_SET(polarity, vconn) \
((polarity) << 4 | (vconn))
#define TCPC_REG_POWER_CTRL_POLARITY(reg) (((reg) & 0x10) >> 4)
#define TCPC_REG_POWER_CTRL_VCONN(reg) ((reg) & 0x1)
#define TCPC_REG_COMMAND 0x23
#define TCPC_REG_MSG_HDR_INFO 0x2e
#define TCPC_REG_MSG_HDR_INFO_SET(drole, prole) \
((drole) << 3 | (PD_REV20 << 1) | (prole))
#define TCPC_REG_MSG_HDR_INFO_DROLE(reg) (((reg) & 0x8) >> 3)
#define TCPC_REG_MSG_HDR_INFO_PROLE(reg) ((reg) & 0x1)
#define TCPC_REG_RX_BYTE_CNT 0x2f
#define TCPC_REG_RX_STATUS 0x30
#define TCPC_REG_RX_DETECT 0x31
#define TCPC_REG_RX_DETECT_SOP_HRST_MASK 0x21
#define TCPC_REG_RX_HDR 0x32
#define TCPC_REG_RX_DATA 0x34 /* through 0x4f */
#define TCPC_REG_TRANSMIT 0x50
#define TCPC_REG_TRANSMIT_SET(type) \
(PD_RETRY_COUNT << 4 | (type))
#define TCPC_REG_TRANSMIT_RETRY(reg) (((reg) & 0x30) >> 4)
#define TCPC_REG_TRANSMIT_TYPE(reg) ((reg) & 0x7)
#define TCPC_REG_TX_BYTE_CNT 0x51
#define TCPC_REG_TX_HDR 0x52
#define TCPC_REG_TX_DATA 0x54 /* through 0x6f */
#endif /* __CROS_EC_USB_PD_TCPM_TCPCI_H */