Initial commit: Linux kernel 4.19 for Unplugged OS

This commit is contained in:
ziv committed 2025-10-06 13:52:42 +00:00
commit d30235b04c
80519 files changed
+35800681

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
config USB_MTK_HDRC
tristate "MediaTek MUSB support"
select USB_GADGET_SELECTED
select USB_GADGET_DUALSPEED
help
Say Y here if your system has a high speed USB
controller based on the MTK MUSB IP. Then
configure options to match your silicon and the board
it's being used with, including the USB peripheral role,
or the USB host role, or both.
config MTK_MUSB_QMU_SUPPORT
bool "QMU Transfer mode of MediaTek MUSB"
depends on USB_MTK_HDRC
help
Say Y here if your system has a QMU capability for USB
controller based on the MTK MUSB IP.
QMU is some kind of enhanced DMA mode by HW queeu rather
than SW queue to reduce SW effort.
config MTK_MUSB_QMU_PURE_ZLP_SUPPORT
bool "Transfer ZLP by GDP of MediaTek MUSB"
depends on MTK_MUSB_QMU_SUPPORT
help
Say Y here if QMU IP supports sending zero
length packet(ZLP) by setting ZLP flag in GDP
and the driver can enable by internal register.
After MT6763 should support this feature.
config MTK_MUSB_DRV_36BIT
tristate "MediaTek MUSB 36bits addressing support"
default n
help
This selects the MediaTek MUSB support 36-bits
address accessing.
Say Y here if you want to enable 36-bits addressing support.
If unsure, say N.
config USB_MTK_OTG
bool "MediaTek MUSB OTG support"
help
Enable Mediatek USB Host OTG detection mechanism
and USB-IF Compliance Test Program.
1. IDDIG - OTG cable attachment detection.
2. DRVVBUS - provides the VBUS for the attached devices
config USB_MTK_CHARGER_DETECT
bool "MediaTek MUSB charger detect"
depends on MTK_USBFSH
help
If make port1 enable in mt8163, please open the config.
When port0 is using, plug in device to port1,
port0 will get the disconnect. fix the issue, need open
the config.
config MTK_UART_USB_SWITCH
bool "MediaTek UART could use USB DATA pins"
help
This is a feature for debugging purpose.
It uses the USB TX/RX as UART TX/RX.
A USB to UART cable is needed to use this feature.
Using the UI in the EngineerMode to switch between USB and UART mode.
After switching to UART mode and connect to the PC with a USB to UART cable,
you can connect to a Virtual COM port on the PC and get the log output to the UART console.
config MTK_MUSB_CARPLAY_SUPPORT
bool "MediaTek MUSB Carplay support"
help
Say Y here if your want to use carplay.
config MTK_MUSB_SW_WITCH_MODE
bool "MediaTek MUSB provide user space interface to switch usb role without id pin"
help
Usb host/device/idle mode switch through command, not only depend on ID pin.
Say Y here if your want to use software to switch usb mode.
config MTK_MUSB_PORT0_LOWPOWER_MODE
bool "MediaTek MUSB PORT0 low power mode support"
help
Dynamic control the usb port0 clock when PORT1 not exist.
Say Y here if your want to enable port0 low power mode for saving power.
config MTK_MUSB_DUAL_ROLE
bool "Mediatek MUSB Dual Role mode support"
select USB_ROLE_SWITCH
help
This is the default mode of working of MTK MUSB controller
where both host and gadget
features are enabled.
It is better to be be used with Mediatek extcon and tphy support.
Say Y here if your want to use usb role switch.
config MTK_MUSB_PHY
tristate "MediaTek USB20 PHY support"
help
MediaTek USB20 PHY support.
+76
View File
@@ -0,0 +1,76 @@
#
# Copyright (C) 2017 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
#
# for USB OTG silicon based on Mentor Graphics INVENTRA designs
#
ifeq ($(MTK_ALPS_BOX_SUPPORT),yes)
ccflags-y += -DMTK_ALPS_BOX_SUPPORT
endif
usb20plat := $(subst ",,$(CONFIG_MTK_PLATFORM))
ifeq ($(CONFIG_MACH_MT6781),y)
usb20plat := mt6781
endif
ifeq ($(CONFIG_MACH_MT6761),y)
usb20plat := mt6765
endif
ifeq ($(CONFIG_MACH_MT6833),y)
usb20plat := mt6833
endif
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
# for SPM control usage
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include
# for phy and vbus settings
ccflags-y += -I$(srctree)/drivers/misc/mediatek/usb20/$(usb20plat)
ifeq ($(CONFIG_TCPC_CLASS),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/tcpc/inc
endif
#for 6739
ccflags-y += -I$(srctree)/drivers/usb/core/
# Driver
obj-$(CONFIG_USB_MTK_HDRC) := musb_hdrc.o
musb_hdrc-$(CONFIG_USB_MTK_HDRC) += musb_core.o
musb_hdrc-$(CONFIG_USB_MTK_HDRC) += musb_virthub.o musb_host.o
musb_hdrc-$(CONFIG_USB_MTK_HDRC) += musb_gadget_ep0.o musb_gadget.o
musb_hdrc-$(CONFIG_PROC_FS) += musb_debugfs.o
musb_hdrc-$(CONFIG_MTK_MUSB_DUAL_ROLE) += musb_dr.o
# obj-$(CONFIG_MTK_MUSB_PLAT) += mtk_musb.o
obj-$(CONFIG_USB_MTK_HDRC) += musbhsdma.o
#Platform
obj-y += $(usb20plat)/
obj-$(CONFIG_USB_MTK_HDRC) += $(usb20plat)/usb20.o
obj-$(CONFIG_USB_MTK_HDRC) += $(usb20plat)/usb20_host.o
obj-$(CONFIG_USBIF_COMPLIANCE) += $(usb20plat)/usb20_otg_if.o
# QMU Realted
musb_hdrc-$(CONFIG_MTK_MUSB_QMU_SUPPORT)+= mtk_qmu.o musb_qmu.o
ifeq ($(CONFIG_MTK_MUSB_QMU_SUPPORT),y)
subdir-ccflags-$(CONFIG_MACH_MT8163) += -DMUSB_QMU_LIMIT_SUPPORT -DMUSB_QMU_LIMIT_RXQ_NUM=4 -DMUSB_QMU_LIMIT_TXQ_NUM=4
endif
ifeq ($(CONFIG_MTK_MUSB_QMU_SUPPORT),y)
subdir-ccflags-$(CONFIG_MACH_MT8167) += -DMUSB_QMU_LIMIT_SUPPORT -DMUSB_QMU_LIMIT_RXQ_NUM=4 -DMUSB_QMU_LIMIT_TXQ_NUM=4
endif
subdir-ccflags-$(CONFIG_MACH_MT6781) += -DUSB2_PHY_V2
subdir-ccflags-$(CONFIG_MACH_MT6833) += -DUSB2_PHY_V2
@@ -0,0 +1,37 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
#
# for USB OTG silicon based on Mentor Graphics INVENTRA designs
#
ccflags-$(CONFIG_USB_MTK_HDRC) += -I$(srctree)/drivers/misc/mediatek/usb20
# for battery related
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
# for SPM control usage
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
# for TYPEC connection management
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/inc
ifeq ($(CONFIG_TCPC_CLASS),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/tcpc/inc
endif
# for ep0 test
ccflags-y += -I$(srctree)/drivers/usb/core/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/usb20
# Phy
obj-$(CONFIG_MTK_MUSB_PHY) += mtk_usb20_phy.o
mtk_usb20_phy-y += usb20_phy.o
mtk_usb20_phy-$(CONFIG_DEBUG_FS) += usb20_phy_debugfs.o
File diff suppressed because it is too large. Load diff
+122
View File
@@ -0,0 +1,122 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __USB20_H__
#define __USB20_H__
#include <linux/interrupt.h>
#include <musb.h>
struct mt_usb_work {
struct delayed_work dwork;
int ops;
};
/* ToDo: should be moved to glue */
extern struct musb *mtk_musb;
extern struct musb *musb;
struct mt_usb_glue {
struct device *dev;
struct platform_device *musb_pdev;
struct musb *mtk_musb;
/* common power & clock */
struct clk *musb_clk;
struct clk *musb_clk_top_sel;
struct clk *musb_clk_univpll3_d4;
#ifdef CONFIG_PHY_MTK_TPHY
struct platform_device *usb_phy;
struct phy *phy;
struct usb_phy *xceiv;
enum phy_mode phy_mode;
#endif
#ifdef CONFIG_MTK_MUSB_DUAL_ROLE
struct otg_switch_mtk otg_sx;
#endif
};
extern struct mt_usb_glue *glue;
#define glue_to_musb(g) platform_get_drvdata(g->musb)
extern int kernel_init_done;
extern void BATTERY_SetUSBState(int usb_state);
/* specific USB fuctnion */
enum CABLE_MODE {
CABLE_MODE_CHRG_ONLY = 0,
CABLE_MODE_NORMAL,
CABLE_MODE_HOST_ONLY,
CABLE_MODE_MAX
};
enum USB_CLK_STATE {
NO_CHANGE = 0,
ON_TO_OFF,
OFF_TO_ON,
};
/* specific USB operation */
enum CONNECTION_OPS {
CONNECTION_OPS_DISC = 0,
CONNECTION_OPS_CHECK,
CONNECTION_OPS_CONN
};
enum VBUS_OPS {
VBUS_OPS_OFF = 0,
VBUS_OPS_ON
};
#ifdef CONFIG_MTK_UART_USB_SWITCH
enum PORT_MODE {
PORT_MODE_USB = 0,
PORT_MODE_UART,
PORT_MODE_MAX
};
extern bool usb_phy_check_in_uart_mode(void);
extern void usb_phy_switch_to_usb(void);
extern void usb_phy_switch_to_uart(void);
#endif
#ifdef CONFIG_FPGA_EARLY_PORTING
extern void USB_PHY_Write_Register8(u8 var, u8 addr);
extern u8 USB_PHY_Read_Register8(u8 addr);
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
#define RG_GPIO_SELECT (0x600)
#define GPIO_SEL_OFFSET (4)
#define GPIO_SEL_MASK (0x3 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART0 (0x1 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART1 (0x2 << GPIO_SEL_OFFSET)
#define GET_GPIO_SEL_VAL(x) ((x & GPIO_SEL_MASK) >> GPIO_SEL_OFFSET)
extern void __iomem *ap_gpio_base;
extern bool in_uart_mode;
#endif
extern int usb20_phy_init_debugfs(void);
#ifdef CONFIG_USB_MTK_OTG
extern void mt_usb_otg_init(struct musb *musb);
extern void mt_usb_otg_exit(struct musb *musb);
extern int mt_usb_get_vbus_status(struct musb *musb);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
#endif
extern void musb_platform_reset(struct musb *musb);
extern bool usb_enable_clock(bool enable);
extern bool usb_prepare_clock(bool enable);
extern void usb_prepare_enable_clock(bool enable);
extern void mt_usb_dev_disconnect(void);
/* usb host mode wakeup */
#define USB_WAKEUP_DEC_CON1 0x404
#define USB1_CDEN BIT(0)
#define USB1_CDDEBOUNCE(x) (((x) & 0xf) << 1)
#endif
@@ -0,0 +1,844 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <usb20.h>
#include <musb_io.h>
#include <mtk_musb_reg.h>
#include <musb_core.h>
#ifdef CONFIG_USB_MTK_OTG
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <mtk_musb.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#ifdef CONFIG_MTK_USB_TYPEC
#ifdef CONFIG_TCPC_CLASS
#include <tcpm.h>
#endif
#endif
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/phy/phy.h>
#ifdef CONFIG_MTK_MUSB_PHY
#include <usb20_phy.h>
#endif
MODULE_LICENSE("GPL v2");
#if CONFIG_MTK_GAUGE_VERSION == 30
#include <mt-plat/v1/charger_class.h>
static struct charger_device *primary_charger;
#endif
#include <mt-plat/mtk_boot_common.h>
struct device_node *usb_node;
static int iddig_eint_num;
static ktime_t ktime_start, ktime_end;
static struct musb_fifo_cfg fifo_cfg_host[] = {
{ .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 64, .mode = BUF_SINGLE},
};
int delay_time = 15;
module_param(delay_time, int, 0400);
int delay_time1 = 55;
module_param(delay_time1, int, 0400);
int iddig_cnt;
module_param(iddig_cnt, int, 0400);
static bool vbus_on;
module_param(vbus_on, bool, 0400);
static int vbus_control;
module_param(vbus_control, int, 0400);
#ifdef CONFIG_MTK_MUSB_PHY
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
#endif
static void _set_vbus(int is_on)
{
#if CONFIG_MTK_GAUGE_VERSION == 30
if (!primary_charger) {
DBG(0, "vbus_init<%d>\n", vbus_on);
primary_charger = get_charger_by_name("primary_chg");
if (!primary_charger) {
DBG(0, "get primary charger device failed\n");
return;
}
}
#endif
DBG(0, "op<%d>, status<%d>\n", is_on, vbus_on);
if (is_on && !vbus_on) {
/* update flag 1st then enable VBUS to make host mode correct used by PMIC */
vbus_on = true;
#if CONFIG_MTK_GAUGE_VERSION == 30
charger_dev_enable_otg(primary_charger, true);
charger_dev_set_boost_current_limit(primary_charger, 1500000);
#else
set_chr_enable_otg(0x1);
set_chr_boost_current_limit(1500);
#endif
} else if (!is_on && vbus_on) {
#if CONFIG_MTK_GAUGE_VERSION == 30
charger_dev_enable_otg(primary_charger, false);
#else
set_chr_enable_otg(0x0);
#endif
/* disable VBUS 1st then update flag to make host mode correct used by PMIC */
vbus_on = false;
}
}
void mt_set_vbus(int is_on)
{
#ifndef CONFIG_FPGA_EARLY_PORTING
DBG(0, "is_on<%d>, control<%d>\n", is_on, vbus_control);
if (!vbus_control)
return;
if (is_on)
_set_vbus(1);
else
_set_vbus(0);
#endif
}
int mt_usb_get_vbus_status(struct musb *musb)
{
#if 1
return true;
#else
int ret = 0;
if ((musb_readb(musb->mregs, MUSB_DEVCTL) & MUSB_DEVCTL_VBUS) != MUSB_DEVCTL_VBUS)
ret = 1;
else
DBG(0, "VBUS error, devctl=%x, power=%d\n", musb_readb(musb->mregs, MUSB_DEVCTL), musb->power);
pr_debug("vbus ready = %d\n", ret);
return ret;
#endif
}
#if defined(CONFIG_USBIF_COMPLIANCE)
int sw_deboun_time = 1;
#else
int sw_deboun_time = 400;
#endif
module_param(sw_deboun_time, int, 0400);
int typec_control;
module_param(typec_control, int, 0400);
static bool typec_req_host;
static bool iddig_req_host;
static void do_host_work(struct work_struct *data);
static void issue_host_work(int ops, int delay, bool on_st)
{
struct mt_usb_work *work;
if (!mtk_musb) {
DBG(0, "mtk_musb = NULL\n");
return;
}
/* create and prepare worker */
work = kzalloc(sizeof(struct mt_usb_work), GFP_ATOMIC);
if (!work) {
DBG(0, "work is NULL, directly return\n");
return;
}
work->ops = ops;
INIT_DELAYED_WORK(&work->dwork, do_host_work);
/* issue connection work */
DBG(0, "issue work, ops<%d>, delay<%d>, on_st<%d>\n",
ops, delay, on_st);
if (on_st)
queue_delayed_work(mtk_musb->st_wq,
&work->dwork, msecs_to_jiffies(delay));
else
schedule_delayed_work(&work->dwork,
msecs_to_jiffies(delay));
}
void mt_usb_host_connect(int delay)
{
typec_req_host = true;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_CONN, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_connect);
void mt_usb_host_disconnect(int delay)
{
typec_req_host = false;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_DISC, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_disconnect);
static bool musb_is_host(void)
{
bool host_mode = 0;
if (typec_control)
host_mode = typec_req_host;
else
host_mode = iddig_req_host;
return host_mode;
}
void musb_session_restart(struct musb *musb)
{
void __iomem *mbase = musb->mregs;
musb_writeb(mbase, MUSB_DEVCTL, (musb_readb(mbase, MUSB_DEVCTL) & (~MUSB_DEVCTL_SESSION)));
#ifdef CONFIG_MTK_MUSB_PHY
DBG(0, "[MUSB] stopped session for VBUSERROR interrupt\n");
USBPHY_SET32(0x6c, (0x3c<<8));
USBPHY_SET32(0x6c, (0x10<<0));
USBPHY_CLR32(0x6c, (0x2c<<0));
DBG(0, "[MUSB] force PHY to idle, 0x6c=%x\n", USBPHY_READ32(0x6c));
mdelay(5);
USBPHY_CLR32(0x6c, (0x3c<<8));
USBPHY_CLR32(0x6c, (0x3c<<0));
DBG(0, "[MUSB] let PHY resample VBUS, 0x6c=%x\n", USBPHY_READ32(0x6c));
#endif
musb_writeb(mbase, MUSB_DEVCTL, (musb_readb(mbase, MUSB_DEVCTL) | MUSB_DEVCTL_SESSION));
DBG(0, "[MUSB] restart session\n");
}
static struct delayed_work host_plug_test_work;
int host_plug_test_enable; /* default disable */
module_param(host_plug_test_enable, int, 0400);
int host_plug_in_test_period_ms = 5000;
module_param(host_plug_in_test_period_ms, int, 0400);
int host_plug_out_test_period_ms = 5000;
module_param(host_plug_out_test_period_ms, int, 0400);
int host_test_vbus_off_time_us = 3000;
module_param(host_test_vbus_off_time_us, int, 0400);
int host_test_vbus_only = 1;
module_param(host_test_vbus_only, int, 0400);
static int host_plug_test_triggered;
void switch_int_to_device(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_HIGH);
enable_irq(iddig_eint_num);
DBG(0, "switch_int_to_device is done\n");
}
void switch_int_to_host(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_LOW);
enable_irq(iddig_eint_num);
DBG(0, "switch_int_to_host is done\n");
}
static void do_host_plug_test_work(struct work_struct *data)
{
static ktime_t ktime_begin, ktime_end;
static s64 diff_time;
static int host_on;
static struct wakeup_source *host_test_wakelock;
static int wake_lock_inited;
if (!wake_lock_inited) {
DBG(0, "%s wake_lock_init\n", __func__);
host_test_wakelock = wakeup_source_register(NULL,
"host.test.lock");
wake_lock_inited = 1;
}
host_plug_test_triggered = 1;
/* sync global status */
mb();
__pm_stay_awake(host_test_wakelock);
DBG(0, "BEGIN");
ktime_begin = ktime_get();
host_on = 1;
while (1) {
if (!musb_is_host() && host_on) {
DBG(0, "about to exit");
break;
}
msleep(50);
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_begin));
if (host_on && diff_time >= host_plug_in_test_period_ms) {
host_on = 0;
DBG(0, "OFF\n");
ktime_begin = ktime_get();
/* simulate plug out */
_set_vbus(0);
udelay(host_test_vbus_off_time_us);
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_DISC, 0, false);
} else if (!host_on && diff_time >=
host_plug_out_test_period_ms) {
host_on = 1;
DBG(0, "ON\n");
ktime_begin = ktime_get();
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_CONN, 0, false);
_set_vbus(1);
msleep(100);
}
}
/* wait host_work done */
msleep(1000);
host_plug_test_triggered = 0;
__pm_relax(host_test_wakelock);
DBG(0, "END\n");
}
#define ID_PIN_WORK_RECHECK_TIME 30 /* 30 ms */
#define ID_PIN_WORK_BLOCK_TIMEOUT 30000 /* 30000 ms */
struct usb_role_switch {
struct device dev;
struct mutex lock; /* device lock*/
enum usb_role role;
/* From descriptor */
struct device *usb2_port;
struct device *usb3_port;
struct device *udc;
usb_role_switch_set_t set;
usb_role_switch_get_t get;
bool allow_userspace_control;
};
static void do_host_work(struct work_struct *data)
{
u8 devctl = 0;
unsigned long flags;
static int inited, timeout; /* default to 0 */
static s64 diff_time;
bool host_on;
int usb_clk_state = NO_CHANGE;
struct mt_usb_work *work =
container_of(data, struct mt_usb_work, dwork.work);
/*struct mt_usb_glue *glue = mtk_musb->glue;*/
/*
* kernel_init_done should be set in
* early-init stage through init.$platform.usb.rc
*/
while (!inited && !kernel_init_done &&
!mtk_musb->is_ready && !timeout) {
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_start));
DBG_LIMIT(3, "init_done:%d, is_ready:%d, inited:%d, TO:%d, diff:%lld",
kernel_init_done, mtk_musb->is_ready, inited, timeout,
diff_time);
if (diff_time > ID_PIN_WORK_BLOCK_TIMEOUT) {
DBG(0, "diff_time:%lld\n", diff_time);
timeout = 1;
}
msleep(ID_PIN_WORK_RECHECK_TIME);
}
if (!inited) {
DBG(0, "PASS,init_done:%d,is_ready:%d,inited:%d, TO:%d\n",
kernel_init_done, mtk_musb->is_ready,
inited, timeout);
inited = 1;
}
/* always prepare clock and check if need to unprepater later */
/* clk_prepare_cnt +1 here */
usb_prepare_clock(true);
spin_lock_irqsave(&mtk_musb->lock, flags);
musb_generic_disable(mtk_musb);
spin_unlock_irqrestore(&mtk_musb->lock, flags);
down(&mtk_musb->musb_lock);
DBG(0, "work start, is_host=%d\n", mtk_musb->is_host);
if (mtk_musb->in_ipo_off) {
DBG(0, "do nothing due to in_ipo_off\n");
goto out;
}
host_on = (work->ops ==
CONNECTION_OPS_CONN ? true : false);
DBG(0, "work start, is_host=%d, host_on=%d\n",
mtk_musb->is_host, host_on);
if (host_on && !mtk_musb->is_host) {
/* switch to HOST state before turn on VBUS */
MUSB_HST_MODE(mtk_musb);
/* to make sure all event clear */
msleep(32);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (!usb_on_sram) {
int ret;
ret = gpd_switch_to_sram(mtk_musb->controller);
DBG(0, "gpd_switch_to_sram, ret<%d>\n", ret);
if (ret == 0)
usb_on_sram = 1;
}
#endif
/* setup fifo for host mode */
ep_config_from_table_for_host(mtk_musb);
if (!mtk_musb->host_suspend)
__pm_stay_awake(mtk_musb->usb_lock);
/* this make PHY operation workable */
musb_platform_enable(mtk_musb);
/* for no VBUS sensing IP*/
/* wait VBUS ready */
msleep(100);
/* clear session*/
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, (devctl&(~MUSB_DEVCTL_SESSION)));
phy_set_mode(glue->phy, PHY_MODE_INVALID);
/* wait */
mdelay(5);
/* restart session */
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, (devctl | MUSB_DEVCTL_SESSION));
phy_set_mode(glue->phy, PHY_MODE_USB_HOST);
musb_start(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_device(mtk_musb);
/* for some signal issue */
mdelay(5);
mt_set_vbus(1);
//if (check_vbus() < 0)
// DBG(0, "check vbus fail\n");
if (host_plug_test_enable && !host_plug_test_triggered)
queue_delayed_work(mtk_musb->st_wq, &host_plug_test_work, 0);
usb_clk_state = OFF_TO_ON;
} else if (!host_on && mtk_musb->is_host) {
/* for device no disconnect interrupt */
spin_lock_irqsave(&mtk_musb->lock, flags);
if (mtk_musb->is_active) {
DBG(0, "for not receiving disconnect interrupt\n");
usb_hcd_resume_root_hub(musb_to_hcd(mtk_musb));
musb_root_disconnect(mtk_musb);
}
spin_unlock_irqrestore(&mtk_musb->lock, flags);
DBG(1, "devctl 1 is %x\n", musb_readb(mtk_musb->mregs, MUSB_DEVCTL));
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, 0);
DBG(1, "devctl 1-2 is %x\n", musb_readb(mtk_musb->mregs, MUSB_DEVCTL));
if (mtk_musb->usb_lock->active)
__pm_relax(mtk_musb->usb_lock);
mt_set_vbus(0);
/* for no VBUS sensing IP */
phy_set_mode(glue->phy, PHY_MODE_INVALID);
musb_stop(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_host(mtk_musb);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (usb_on_sram) {
gpd_switch_to_dram(mtk_musb->controller);
usb_on_sram = 0;
}
#endif
/* to make sure all event clear */
msleep(32);
mtk_musb->xceiv->otg->state = OTG_STATE_B_IDLE;
/* switch to DEV state after turn off VBUS */
MUSB_DEV_MODE(mtk_musb);
usb_clk_state = ON_TO_OFF;
}
out:
DBG(0, "work end, is_host=%d\n", mtk_musb->is_host);
up(&mtk_musb->musb_lock);
kobject_uevent(&glue->otg_sx.role_sw->dev.kobj, KOBJ_CHANGE);
if (usb_clk_state == ON_TO_OFF) {
/* clock on -> of: clk_prepare_cnt -2 */
usb_prepare_clock(false);
usb_prepare_clock(false);
} else if (usb_clk_state == NO_CHANGE) {
/* clock no change : clk_prepare_cnt -1 */
usb_prepare_clock(false);
}
/* free mt_usb_work */
kfree(work);
}
#include <linux/usb/role.h>
static irqreturn_t mt_usb_ext_iddig_int(int irq, void *dev_id)
{
iddig_cnt++;
iddig_req_host = !iddig_req_host;
DBG(0, "id pin assert, %s\n", iddig_req_host ? "connect" : "disconnect");
if (iddig_req_host)
mt_usb_host_connect(sw_deboun_time);
else
mt_usb_host_disconnect(sw_deboun_time);
disable_irq_nosync(iddig_eint_num);
return IRQ_HANDLED;
}
static const struct of_device_id otg_iddig_of_match[] = {
{.compatible = "mediatek,usb_iddig_bi_eint"},
{},
};
static int otg_iddig_probe(struct platform_device *pdev)
{
int ret;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
iddig_eint_num = irq_of_parse_and_map(node, 0);
DBG(0, "iddig_eint_num<%d>\n", iddig_eint_num);
if (iddig_eint_num < 0)
return -ENODEV;
ret = request_irq(iddig_eint_num, mt_usb_ext_iddig_int, IRQF_TRIGGER_LOW, "USB_IDDIG", NULL);
if (ret) {
DBG(0, "request EINT <%d> fail, ret<%d>\n", iddig_eint_num, ret);
return ret;
}
return 0;
}
static struct platform_driver otg_iddig_driver = {
.probe = otg_iddig_probe,
/* .remove = otg_iddig_remove, */
/* .shutdown = otg_iddig_shutdown, */
.driver = {
.name = "otg_iddig",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(otg_iddig_of_match),
},
};
static int iddig_int_init(void)
{
int ret = 0;
ret = platform_driver_register(&otg_iddig_driver);
if (ret)
DBG(0, "ret:%d\n", ret);
return 0;
}
void mt_usb_otg_init(struct musb *musb)
{
/* BYPASS OTG function in special mode */
INIT_DELAYED_WORK(&host_plug_test_work, do_host_plug_test_work);
ktime_start = ktime_get();
/* CONNECTION MANAGEMENT*/
#ifdef CONFIG_MTK_USB_TYPEC
DBG(0, "host controlled by TYPEC\n");
typec_control = 1;
#else
DBG(0, "host controlled by IDDIG\n");
iddig_int_init();
vbus_control = 1;
#endif
/* EP table */
musb->fifo_cfg_host = fifo_cfg_host;
musb->fifo_cfg_host_size = ARRAY_SIZE(fifo_cfg_host);
/*
* otg_state.name = "otg_state";
* otg_state.index = 0;
* otg_state.state = 0;
*
* if (switch_dev_register(&otg_state))
* pr_notice("switch_dev_register fail\n");
* else
* pr_debug("switch_dev register success\n");
*/
}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb)
{
DBG(0, "OTG disable vbus\n");
mt_set_vbus(0);
}
#ifdef CONFIG_MTK_MUSB_PHY
#include "hub.h"
static struct delayed_work ep0_stress_work;
struct usb_device *udev;
static void do_ep0_stress_work(struct work_struct *data)
{
int ret;
u16 status;
ret = usb_get_status(udev, USB_RECIP_DEVICE, USB_STATUS_TYPE_STANDARD, 0, &status);
DBG_LIMIT(30, "usb_get_status, ret<%d>", ret);
if (ret == 0)
schedule_delayed_work(&ep0_stress_work, 0);
else {
DBG(0, "usb_get_status, ret<%d>\n", ret);
usb_autosuspend_device(udev);
}
}
static void trigger_ep0_stress_work(void)
{
int ret;
struct usb_hcd *hcd = musb_to_hcd(mtk_musb);
struct usb_device *rhdev = hcd->self.root_hub;
struct usb_hub *hub = usb_hub_to_struct_hub(rhdev);
struct usb_port *port_dev = hub->ports[0];
udev = port_dev->child;
ret = usb_autoresume_device(udev);
DBG(0, "usb_autoresume_device, ret<%d>\n", ret);
if (ret)
return;
INIT_DELAYED_WORK(&ep0_stress_work, do_ep0_stress_work);
schedule_delayed_work(&ep0_stress_work, 0);
}
#endif
enum {
DO_IT = 0,
REVERT,
};
#ifdef CONFIG_MTK_MUSB_PHY
static void bypass_disc_circuit(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 13-12 RG_USB20_HSRX_MMODE_SELE, dft:00 */
if (act == DO_IT) {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_SET32(0x18, (0x20<<8));
} else {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_CLR32(0x18, (0x20<<8));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
static void disc_threshold_to_max(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 7-4 RG_USB20_DISCTH, dft:1000 */
if (act == DO_IT) {
USBPHY_SET32(0x18, (0xf0<<0));
} else {
USBPHY_CLR32(0x18, (0x70<<0));
USBPHY_SET32(0x18, (0x80<<0));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
#endif
static int option;
static int set_option(const char *val, const struct kernel_param *kp)
{
int local_option;
int rv;
/* update module parameter */
rv = param_set_int(val, kp);
if (rv)
return rv;
/* update local_option */
rv = kstrtoint(val, 10, &local_option);
if (rv != 0)
return rv;
DBG(0, "option:%d, local_option:%d\n", option, local_option);
switch (local_option) {
case 0:
DBG(0, "case %d\n", local_option);
iddig_int_init();
break;
case 1:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(0);
break;
case 2:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(0);
break;
case 3:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(3000);
break;
case 4:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(3000);
break;
#ifdef CONFIG_MTK_MUSB_PHY
case 5:
DBG(0, "case %d\n", local_option);
trigger_ep0_stress_work();
break;
case 6:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(DO_IT);
break;
case 7:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(REVERT);
break;
case 8:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(DO_IT);
break;
case 9:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(REVERT);
break;
#endif
case 10:
DBG(0, "case %d\n", local_option);
_set_vbus(1);
break;
case 11:
DBG(0, "case %d\n", local_option);
_set_vbus(0);
break;
case 12:
//DBG(0, "vbus=%d\n", pmic_get_vbus());
break;
default:
break;
}
return 0;
}
static struct kernel_param_ops option_param_ops = {
.set = set_option,
.get = param_get_int,
};
module_param_cb(option, &option_param_ops, &option, 0400);
#else
#include "musb_core.h"
/* for not define CONFIG_USB_MTK_OTG */
void mt_usb_otg_init(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_exit);
void mt_set_vbus(int is_on) {}
int mt_usb_get_vbus_status(struct musb *musb) {return 1; }
void switch_int_to_device(struct musb *musb) {}
void switch_int_to_host(struct musb *musb) {}
void musb_disable_host(struct musb *musb) {}
void musb_enable_host(struct musb *musb) {}
void musb_session_restart(struct musb *musb) {}
EXPORT_SYMBOL(musb_session_restart);
#endif
@@ -0,0 +1,736 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifdef CONFIG_MTK_CLKMGR
#include <mach/mt_clkmgr.h>
#else
#include <linux/clk.h>
#endif
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <mtk_musb.h>
#include <musb_core.h>
#include "usb20.h"
#include "mtk_devinfo.h"
#ifdef CONFIG_OF
#include <linux/of_address.h>
#endif
#include <mt-plat/aee.h>
#include <mt-plat/mtk_boot_common.h>
#define FRA (48)
#define PARA (28)
#ifdef CONFIG_FPGA_EARLY_PORTING
bool usb_enable_clock(bool enable)
{
return true;
}
bool usb_prepare_clock(bool enable)
{
return true;
}
void usb_prepare_enable_clock(bool enable)
{
}
void usb_phy_poweron(void)
{
}
void usb_phy_savecurrent(void)
{
}
void usb_phy_recover(void)
{
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
}
void Charger_Detect_Release(void)
{
}
void usb_phy_context_save(void)
{
}
void usb_phy_context_restore(void)
{
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
return false;
}
void usb_phy_switch_to_uart(void)
{
}
void usb_phy_switch_to_usb(void)
{
}
#endif
#else
#ifdef CONFIG_MTK_USB2JTAG_SUPPORT
int usb2jtag_usb_init(void)
{
struct device_node *node = NULL;
void __iomem *usb_phy_base;
u32 temp;
node = of_find_compatible_node(NULL, NULL, "mediatek,mt6739-usb20");
if (!node) {
pr_notice("[USB2JTAG] map node @ mediatek,USB0 failed\n");
return -1;
}
usb_phy_base = of_iomap(node, 1);
if (!usb_phy_base) {
pr_notice("[USB2JTAG] iomap usb_phy_base failed\n");
return -1;
}
/* rg_usb20_gpio_ctl: bit[9] = 1 */
temp = readl(usb_phy_base + 0x820);
writel(temp | (1 << 9), usb_phy_base + 0x820);
/* RG_USB20_BGR_EN: bit[0] = 1 */
temp = readl(usb_phy_base + 0x800);
writel(temp | (1 << 0), usb_phy_base + 0x800);
/* RG_USB20_BC11_SW_EN: bit[23] = 0 */
temp = readl(usb_phy_base + 0x818);
writel(temp & ~(1 << 23), usb_phy_base + 0x818);
/* wait stable */
mdelay(1);
iounmap(usb_phy_base);
return 0;
}
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool in_uart_mode;
#endif
void usb_prepare_enable_clock(bool enable)
{
if (enable) {
usb_prepare_clock(true);
usb_enable_clock(true);
} else {
usb_enable_clock(false);
usb_prepare_clock(false);
}
}
DEFINE_MUTEX(prepare_lock);
static atomic_t clk_prepare_cnt = ATOMIC_INIT(0);
bool usb_prepare_clock(bool enable)
{
int before_cnt = atomic_read(&clk_prepare_cnt);
mutex_lock(&prepare_lock);
if (IS_ERR_OR_NULL(musb_clk) ||
IS_ERR_OR_NULL(musb_clk_top_sel) ||
IS_ERR_OR_NULL(musb_clk_univpll3_d4)) {
DBG(0, "clk not ready\n");
mutex_unlock(&prepare_lock);
return 0;
}
if (enable) {
if (clk_prepare(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel prepare fail\n");
} else {
if (clk_set_parent(musb_clk_top_sel, musb_clk_univpll3_d4))
DBG(0, "musb_clk_top_sel set_parent fail\n");
}
if (clk_prepare(musb_clk))
DBG(0, "musb_clk prepare fail\n");
atomic_inc(&clk_prepare_cnt);
} else {
atomic_dec(&clk_prepare_cnt);
clk_unprepare(musb_clk_top_sel);
clk_unprepare(musb_clk);
}
mutex_unlock(&prepare_lock);
DBG(1, "enable(%d), usb prepare_cnt, before(%d), after(%d)\n",
enable, before_cnt, atomic_read(&clk_prepare_cnt));
if (atomic_read(&clk_prepare_cnt) < 0)
aee_kernel_warning("usb20", "usb clock prepare_cnt error\n");
return 1;
}
static DEFINE_SPINLOCK(musb_reg_clock_lock);
bool usb_enable_clock(bool enable)
{
static int count;
static int real_enable = 0, real_disable;
static int virt_enable = 0, virt_disable;
unsigned long flags;
DBG(1, "enable(%d),count(%d),<%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable, real_enable, real_disable);
spin_lock_irqsave(&musb_reg_clock_lock, flags);
if (unlikely(atomic_read(&clk_prepare_cnt) <= 0)) {
DBG_LIMIT(1, "clock not prepare");
goto exit;
}
if (enable && count == 0) {
if (clk_enable(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel enable fail\n");
goto exit;
}
if (clk_enable(musb_clk)) {
DBG(0, "musb_clk enable fail\n");
clk_disable(musb_clk_top_sel);
goto exit;
}
usb_hal_dpidle_request(USB_DPIDLE_FORBIDDEN);
real_enable++;
} else if (!enable && count == 1) {
clk_disable(musb_clk);
clk_disable(musb_clk_top_sel);
usb_hal_dpidle_request(USB_DPIDLE_ALLOWED);
real_disable++;
}
if (enable)
count++;
else
count = (count == 0) ? 0 : (count - 1);
exit:
if (enable)
virt_enable++;
else
virt_disable++;
spin_unlock_irqrestore(&musb_reg_clock_lock, flags);
DBG(1, "enable(%d),count(%d), <%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable, real_enable, real_disable);
return 1;
}
static void hs_slew_rate_cal(void)
{
unsigned long data;
unsigned long x;
unsigned char value;
unsigned long start_time, timeout;
unsigned int timeout_flag = 0;
/* enable usb ring oscillator. */
USBPHY_SET32(0x14, (0x1 << 15));
/* wait 1us. */
udelay(1);
/* enable free run clock */
USBPHY_SET32(0xF10 - 0x800, (0x01 << 8));
/* setting cyclecnt. */
USBPHY_SET32(0xF00 - 0x800, (0x04 << 8));
/* enable frequency meter */
USBPHY_SET32(0xF00 - 0x800, (0x01 << 24));
/* wait for frequency valid. */
start_time = jiffies;
timeout = jiffies + 3 * HZ;
while (!((USBPHY_READ32(0xF10 - 0x800) & 0xFF) == 0x1)) {
if (time_after(jiffies, timeout)) {
timeout_flag = 1;
break;
}
}
/* read result. */
if (timeout_flag) {
DBG(0, "[USBPHY] Slew Rate Calibration: Timeout\n");
value = 0x4;
} else {
data = USBPHY_READ32(0xF0C - 0x800);
x = ((1024 * FRA * PARA) / data);
value = (unsigned char)(x / 1000);
if ((x - value * 1000) / 100 >= 5)
value += 1;
DBG(0, "[USBPHY]slew calibration:FM_OUT =%lu,x=%lu,value=%d\n", data, x, value);
}
/* disable Frequency and disable free run clock. */
USBPHY_CLR32(0xF00 - 0x800, (0x01 << 24));
USBPHY_CLR32(0xF10 - 0x800, (0x01 << 8));
#define MSK_RG_USB20_HSTX_SRCTRL 0x7
/* all clr first then set */
USBPHY_CLR32(0x14, (MSK_RG_USB20_HSTX_SRCTRL << 12));
USBPHY_SET32(0x14, ((value & MSK_RG_USB20_HSTX_SRCTRL) << 12));
/* disable usb ring oscillator. */
USBPHY_CLR32(0x14, (0x1 << 15));
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
u32 usb_port_mode;
usb_port_mode = USBPHY_READ32(0x68);
if (((usb_port_mode >> 30) & 0x3) == 1) {
DBG(0, "%s:%d - IN UART MODE : 0x%x\n", __func__, __LINE__, usb_port_mode);
in_uart_mode = true;
} else {
DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n", __func__, __LINE__, usb_port_mode);
in_uart_mode = false;
}
return in_uart_mode;
}
void usb_phy_switch_to_uart(void)
{
unsigned int val = 0;
if (usb_phy_check_in_uart_mode()) {
DBG(0, "Already in UART mode.\n");
return;
}
udelay(50);
/* RG_USB20_BC11_SW_EN 0x11F4_0818[23] = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* Set RG_SUSPENDM 0x11F4_0868[3] to 1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force suspendm 0x11F4_0868[18] = 1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b01 */
USBPHY_CLR32(0x68, (0x3 << 30));
USBPHY_SET32(0x68, (0x1 << 30));
/* force_uart_i 0x11F4_0868[29] = 0*/
USBPHY_CLR32(0x68, (0x1 << 29));
/* force_uart_bias_en 0x11F4_0868[28] = 1 */
USBPHY_SET32(0x68, (0x1 << 28));
/* force_uart_tx_oe 0x11F4_0868[27] = 1 */
USBPHY_SET32(0x68, (0x1 << 27));
/* force_uart_en 0x11F4_0868[26] = 1 */
USBPHY_SET32(0x68, (0x1 << 26));
/* RG_UART_BIAS_EN 0x11F4_086c[18] = 1 */
USBPHY_SET32(0x6C, (0x1 << 18));
/* RG_UART_TX_OE 0x11F4_086c[17] = 1 */
USBPHY_SET32(0x6C, (0x1 << 17));
/* Set RG_UART_EN to 1 */
USBPHY_SET32(0x6C, (0x1 << 16));
/* Set RG_USB20_DM_100K_EN to 1 */
USBPHY_SET32(0x20, (0x1 << 17));
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
val = readl(ap_gpio_base);
writel(val | (GPIO_SEL_UART0), ap_gpio_base);
in_uart_mode = true;
}
void usb_phy_switch_to_usb(void)
{
unsigned int val = 0;
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
/* clear force_uart_en */
USBPHY_CLR32(0x68, (0x1 << 26));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b00 */
USBPHY_CLR32(0x68, (0x3 << 30));
in_uart_mode = false;
usb_phy_poweron();
}
#endif
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
void usb_rev6_setting(int value)
{
static int direct_return;
if (direct_return)
return;
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
if (value)
USBPHY_SET32(0x18, (value << 24));
else
direct_return = 1;
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_poweron(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No usb_phy_poweron\n");
return;
}
#endif
/* wait 50 usec for PHY3.3v/1.8v stable. */
udelay(50);
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* rg_usb20_dp_100k_mode 1'b1 0x20 18
* USB20_DP_100K_EN 1'b0 0x20 16
* RG_USB20_DM_100K_EN 1'b0 0x20 17
* RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
* force_suspendm 1'b0 0x68 18
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 9) | (0x1 << 8)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* rg_usb20_dp_100k_mode, 1'b1 */
USBPHY_SET32(0x20, (0x1 << 18));
/* USB20_DP_100K_EN 1'b0, RG_USB20_DM_100K_EN, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 16) | (0x1 << 17)));
/* For saving 1V8 & 3V3 power*/
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
/* USBPHY_SET32(0x18, (0x1 << 20)); */
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* wait for 800 usec. */
udelay(800);
DBG(0, "usb power on success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
static void usb_phy_savecurrent_internal(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No usb_phy_savecurrent_internal\n");
return;
}
#endif
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* RG_USB20_OTG_VBUSCMP_EN 1'b0 0x18 20
* RG_SUSPENDM 1'b1 0x68 03
* force_suspendm 1'b1 0x68 18
* RG_DPPULLDOWN 1'b1 0x68 06
* RG_DMPULLDOWN 1'b1 0x68 07
* RG_XCVRSEL[1:0] 2'b01 0x68 [04-05]
* RG_TERMSEL 1'b1 0x68 02
* RG_DATAIN[3:0] 4'b0000 0x68 [10-13]
* force_dp_pulldown 1'b1 0x68 20
* force_dm_pulldown 1'b1 0x68 21
* force_xcversel 1'b1 0x68 19
* force_termsel 1'b1 0x68 17
* force_datain 1'b1 0x68 23
* RG_SUSPENDM 1'b0 0x68 03
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 20));
/* RG_SUSPENDM, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force_suspendm, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b01. */
USBPHY_CLR32(0x68, (0x3 << 4));
USBPHY_SET32(0x68, (0x1 << 4));
/* RG_TERMSEL, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b1, force_dm_pulldown, 1'b1,
* force_xcversel, 1'b1, force_termsel, 1'b1, force_datain, 1'b1
*/
USBPHY_SET32(0x68, ((0x1 << 20) | (0x1 << 21) | (0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
/* wait 800 usec. */
udelay(800);
/* RG_SUSPENDM, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 3));
udelay(1);
set_usb_phy_mode(PHY_MODE_INVALID);
}
void usb_phy_savecurrent(void)
{
usb_phy_savecurrent_internal();
DBG(0, "usb save current success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_recover(void)
{
unsigned int efuse_val = 0;
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No usb_phy_recover\n");
return;
}
#endif
/* wait 50 usec. */
udelay(50);
/*
* 04.force_uart_en 1'b0 0x68 26
* 04.RG_UART_EN 1'b0 0x6C 16
* 04.rg_usb20_gpio_ctl 1'b0 0x20 09
* 04.usb20_gpio_mode 1'b0 0x20 08
* 05.force_suspendm 1'b0 0x68 18
* 06.RG_DPPULLDOWN 1'b0 0x68 06
* 07.RG_DMPULLDOWN 1'b0 0x68 07
* 08.RG_XCVRSEL[1:0] 2'b00 0x68 [04:05]
* 09.RG_TERMSEL 1'b0 0x68 02
* 10.RG_DATAIN[3:0] 4'b0000 0x68 [10:13]
* 11.force_dp_pulldown 1'b0 0x68 20
* 12.force_dm_pulldown 1'b0 0x68 21
* 13.force_xcversel 1'b0 0x68 19
* 14.force_termsel 1'b0 0x68 17
* 15.force_datain 1'b0 0x68 23
* 16.RG_USB20_BC11_SW_EN 1'b0 0x18 23
* 17.RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
*/
/* clean PUPD_BIST_EN */
/* PUPD_BIST_EN = 1'b0 */
/* PMIC will use it to detect charger type */
/* NEED?? USBPHY_CLR8(0x1d, 0x10);*/
USBPHY_CLR32(0x1c, (0x1 << 12));
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6C, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b00. */
USBPHY_CLR32(0x68, (0x3 << 4));
/* RG_TERMSEL, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b0, force_dm_pulldown, 1'b0,
* force_xcversel, 1'b0, force_termsel, 1'b0, force_datain, 1'b0
*/
USBPHY_CLR32(0x68, ((0x1 << 20) | (0x1 << 21) | (0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* For saving 1V8 & 3V3 power*/
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
/* USBPHY_SET32(0x18, (0x1 << 20)); */
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
usb_rev6_setting(0x40);
/* wait 800 usec. */
udelay(800);
/* force enter device mode */
set_usb_phy_mode(PHY_MODE_USB_DEVICE);
hs_slew_rate_cal();
/* M_ANALOG8[4:0] => RG_USB20_INTR_CAL[4:0] */
efuse_val = (get_devinfo_with_index(108) & (0x1f<<0)) >> 0;
if (efuse_val) {
DBG(0, "skip efuse setting temporary, RG_USB20_INTR_CAL=0x%x\n", efuse_val);
/* DBG(0, "apply efuse setting, RG_USB20_INTR_CAL=0x%x\n", efuse_val); */
/* USBPHY_CLR32(0x04, (0x1F<<19)); */
/* USBPHY_SET32(0x04, (efuse_val<<19)); */
}
/* disc threshold to max, RG_USB20_DISCTH[7:4], dft:1000, MAX:1111 */
USBPHY_SET32(0x18, (0xf0<<0));
DBG(0, "usb recovery success\n");
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
/* RG_USB20_BC11_SW_EN = 1'b1 */
USBPHY_SET32(0x18, (0x1 << 23));
usb_prepare_enable_clock(false);
DBG(0, "Charger_Detect_Init\n");
}
EXPORT_SYMBOL(Charger_Detect_Init);
void Charger_Detect_Release(void)
{
usb_prepare_enable_clock(true);
/* RG_USB20_BC11_SW_EN = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
udelay(1);
usb_prepare_enable_clock(false);
DBG(0, "Charger_Detect_Release\n");
}
EXPORT_SYMBOL(Charger_Detect_Release);
void usb_phy_context_save(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
in_uart_mode = usb_phy_check_in_uart_mode();
#endif
}
void usb_phy_context_restore(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode)
usb_phy_switch_to_uart();
#endif
}
#endif
@@ -0,0 +1,642 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <musb_core.h>
#include <musb_debug.h>
#include <mtk_musb.h>
#define MYDBG(fmt, args...) pr_notice("MTK_ICUSB [DBG], <%s(), %d> " fmt, __func__, __LINE__, ## args)
/* general */
#define BIT_WIDTH_1 1
#define MSK_WIDTH_1 0x1
#define VAL_MAX_WDITH_1 0x1
#define VAL_0_WIDTH_1 0x0
#define VAL_1_WIDTH_1 0x1
#define STRNG_0_WIDTH_1 "0"
#define STRNG_1_WIDTH_1 "1"
#define BIT_WIDTH_2 2
#define MSK_WIDTH_2 0x3
#define VAL_MAX_WDITH_2 0x3
#define VAL_0_WIDTH_2 0x0
#define VAL_1_WIDTH_2 0x1
#define VAL_2_WIDTH_2 0x2
#define VAL_3_WIDTH_2 0x3
#define STRNG_0_WIDTH_2 "00"
#define STRNG_1_WIDTH_2 "01"
#define STRNG_2_WIDTH_2 "10"
#define STRNG_3_WIDTH_2 "11"
#define BIT_WIDTH_3 3
#define MSK_WIDTH_3 0x7
#define VAL_MAX_WDITH_3 0x7
#define VAL_0_WIDTH_3 0x0
#define VAL_1_WIDTH_3 0x1
#define VAL_2_WIDTH_3 0x2
#define VAL_3_WIDTH_3 0x3
#define VAL_4_WIDTH_3 0x4
#define VAL_5_WIDTH_3 0x5
#define VAL_6_WIDTH_3 0x6
#define VAL_7_WIDTH_3 0x7
#define STRNG_0_WIDTH_3 "000"
#define STRNG_1_WIDTH_3 "001"
#define STRNG_2_WIDTH_3 "010"
#define STRNG_3_WIDTH_3 "011"
#define STRNG_4_WIDTH_3 "100"
#define STRNG_5_WIDTH_3 "101"
#define STRNG_6_WIDTH_3 "110"
#define STRNG_7_WIDTH_3 "111"
/* specific */
#define FILE_USB_DRIVING_CAPABILITY "USB_DRIVING_CAPABILITY"
#define FILE_RG_USB20_TERM_VREF_SEL "RG_USB20_TERM_VREF_SEL"
#define MSK_RG_USB20_TERM_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define FILE_RG_USB20_HSTX_SRCTRL "RG_USB20_HSTX_SRCTRL"
#define MSK_RG_USB20_HSTX_SRCTRL MSK_WIDTH_3
#define SHFT_RG_USB20_HSTX_SRCTRL 12
#define OFFSET_RG_USB20_HSTX_SRCTRL 0x14
#define FILE_RG_USB20_VRT_VREF_SEL "RG_USB20_VRT_VREF_SEL"
#define MSK_RG_USB20_VRT_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define FILE_RG_USB20_INTR_EN "RG_USB20_INTR_EN"
#define MSK_RG_USB20_INTR_EN MSK_WIDTH_1
#define SHFT_RG_USB20_INTR_EN 5
#define OFFSET_RG_USB20_INTR_EN 0x0
#define FILE_RG_USB20_PHY_REV6 "RG_USB20_PHY_REV6"
#define MSK_RG_USB20_PHY_REV6 MSK_WIDTH_2
#define SHFT_RG_USB20_PHY_REV6 30
#define OFFSET_RG_USB20_PHY_REV6 0x18
static struct proc_dir_entry *usb20_phy_procfs_root;
void usb20_phy_debugfs_write_width1(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
MYDBG("s(%s)\n", buf);
if (!strncmp(buf, STRNG_0_WIDTH_1, BIT_WIDTH_1)) {
MYDBG("%s case\n", STRNG_0_WIDTH_1);
clr_val = VAL_1_WIDTH_1;
}
if (!strncmp(buf, STRNG_1_WIDTH_1, BIT_WIDTH_1)) {
MYDBG("%s case\n", STRNG_1_WIDTH_1);
set_val = VAL_1_WIDTH_1;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_1 - set_val;
MYDBG("offset:%x, clr_val:%x, set_val:%x, before shft\n", offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
MYDBG("offset:%x, clr_val:%x, set_val:%x, after shft\n", offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
MYDBG("do nothing\n");
}
}
void usb20_phy_debugfs_rev6_write(u8 offset, u8 shift, char *buf)
{
u8 set_val = 0xFF;
MYDBG("s(%s)\n", buf);
if (!strncmp(buf, STRNG_0_WIDTH_2, BIT_WIDTH_2)) {
MYDBG("%s case\n", STRNG_0_WIDTH_2);
set_val = VAL_0_WIDTH_2;
}
if (!strncmp(buf, STRNG_1_WIDTH_2, BIT_WIDTH_2)) {
MYDBG("%s case\n", STRNG_1_WIDTH_2);
set_val = VAL_1_WIDTH_2;
}
if (!strncmp(buf, STRNG_2_WIDTH_2, BIT_WIDTH_2)) {
MYDBG("%s case\n", STRNG_2_WIDTH_2);
set_val = VAL_2_WIDTH_2;
}
if (!strncmp(buf, STRNG_3_WIDTH_2, BIT_WIDTH_2)) {
MYDBG("%s case\n", STRNG_3_WIDTH_2);
set_val = VAL_3_WIDTH_2;
}
if (set_val <= VAL_MAX_WDITH_2) {
MYDBG("offset:%x, set_val:%x, before shft\n", offset, set_val);
USBPHY_CLR32(offset, (VAL_MAX_WDITH_2<<shift));
USBPHY_SET32(offset, (set_val<<shift));
} else {
MYDBG("do nothing\n");
}
}
void usb20_phy_debugfs_write_width3(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
MYDBG("s(%s)\n", buf);
if (!strncmp(buf, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_0_WIDTH_3);
clr_val = VAL_7_WIDTH_3;
}
if (!strncmp(buf, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_1_WIDTH_3);
set_val = VAL_1_WIDTH_3;
}
if (!strncmp(buf, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_2_WIDTH_3);
set_val = VAL_2_WIDTH_3;
}
if (!strncmp(buf, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_3_WIDTH_3);
set_val = VAL_3_WIDTH_3;
}
if (!strncmp(buf, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_4_WIDTH_3);
set_val = VAL_4_WIDTH_3;
}
if (!strncmp(buf, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_5_WIDTH_3);
set_val = VAL_5_WIDTH_3;
}
if (!strncmp(buf, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_6_WIDTH_3);
set_val = VAL_6_WIDTH_3;
}
if (!strncmp(buf, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_7_WIDTH_3);
set_val = VAL_7_WIDTH_3;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_3 - set_val;
MYDBG("offset:%x, clr_val:%x, set_val:%x, before shft\n", offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
MYDBG("offset:%x, clr_val:%x, set_val:%x, after shft\n", offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
MYDBG("do nothing\n");
}
}
u8 usb20_phy_debugfs_read_val(u8 offset, u8 shft, u8 msk, u8 width, char *str)
{
u32 val;
int i, temp;
val = USBPHY_READ32(offset);
MYDBG("offset:%x, val:%x, shft:%x, msk:%x\n", offset, val, shft, msk);
val = val >> shft;
MYDBG("offset:%x, val:%x, shft:%x, msk:%x\n", offset, val, shft, msk);
val = val & msk;
MYDBG("offset:%x, val:%x, shft:%x, msk:%x\n", offset, val, shft, msk);
temp = val;
str[width] = '\0';
for (i = (width - 1); i >= 0; i--) {
if (val % 2)
str[i] = '1';
else
str[i] = '0';
MYDBG("str[%d]:%c\n", i, str[i]);
val /= 2;
}
MYDBG("str(%s)\n", str);
return val;
}
static int usb_driving_capability_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
u8 combined_val, tmp_val = 0xff;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL, SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL, BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
combined_val = tmp_val;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL, SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL, BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
MYDBG("%s case\n", STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
MYDBG("combined_val(%d), tmp_val(%d)\n", combined_val, tmp_val);
if ((tmp_val == (combined_val - 1)) || (tmp_val == combined_val))
combined_val += tmp_val;
else
combined_val = tmp_val * (VAL_MAX_WDITH_3 + 1) + combined_val;
MYDBG("combined_val(%d), tmp_val(%d)\n", combined_val, tmp_val);
seq_printf(s, "\n%s = %d\n", FILE_USB_DRIVING_CAPABILITY, combined_val);
return 0;
}
static int rg_usb20_term_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL, SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_TERM_VREF_SEL, str);
return 0;
}
static int rg_usb20_hstx_srctrl_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_HSTX_SRCTRL, SHFT_RG_USB20_HSTX_SRCTRL,
MSK_RG_USB20_HSTX_SRCTRL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_HSTX_SRCTRL, str);
return 0;
}
static int rg_usb20_vrt_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL, SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_VRT_VREF_SEL, str);
return 0;
}
static int rg_usb20_intr_en_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_INTR_EN, SHFT_RG_USB20_INTR_EN,
MSK_RG_USB20_INTR_EN, BIT_WIDTH_1, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_INTR_EN, str);
return 0;
}
static int rg_usb20_rev6_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_PHY_REV6, SHFT_RG_USB20_PHY_REV6,
MSK_RG_USB20_PHY_REV6, BIT_WIDTH_2, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_PHY_REV6, str);
return 0;
}
static int usb_driving_capability_open(struct inode *inode, struct file *file)
{
return single_open(file, usb_driving_capability_show, PDE_DATA(inode));
}
static int rg_usb20_term_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_term_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_hstx_srctrl_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_hstx_srctrl_show, PDE_DATA(inode));
}
static int rg_usb20_vrt_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_vrt_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_intr_en_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_intr_en_show, PDE_DATA(inode));
}
static int rg_usb20_rev6_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_rev6_show, PDE_DATA(inode));
}
void val_to_bstring_width3(u8 val, char *str)
{
if (val == VAL_0_WIDTH_3)
memcpy(str, STRNG_0_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_1_WIDTH_3)
memcpy(str, STRNG_1_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_2_WIDTH_3)
memcpy(str, STRNG_2_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_3_WIDTH_3)
memcpy(str, STRNG_3_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_4_WIDTH_3)
memcpy(str, STRNG_4_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_5_WIDTH_3)
memcpy(str, STRNG_5_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_6_WIDTH_3)
memcpy(str, STRNG_6_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_7_WIDTH_3)
memcpy(str, STRNG_7_WIDTH_3, BIT_WIDTH_3 + 1);
MYDBG("val(%d), str(%s)\n", val, str);
}
static ssize_t usb_driving_capability_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[18];
u8 val, tmp_val;
char str_rg_usb20_term_vref_sel[18], str_rg_usb20_vrt_vref_sel[18];
memset(buf, 0x00, sizeof(buf));
MYDBG("\n");
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtol(buf, 10, (long *)&val) != 0) {
MYDBG("kstrtol, err(%d)\n", kstrtol(buf, 10, (long *)&val));
return count;
}
MYDBG("kstrtol, val(%d)\n", val);
if (val > VAL_7_WIDTH_3 * 2) {
MYDBG("wrong val set(%d), direct return\n", val);
return count;
}
tmp_val = val;
val /= 2;
MYDBG("val(%d), tmp_val(%d)\n", val, tmp_val);
val_to_bstring_width3(tmp_val - val, str_rg_usb20_term_vref_sel);
val_to_bstring_width3(val, str_rg_usb20_vrt_vref_sel);
MYDBG("Config TERM_VREF_SEL %s\n", str_rg_usb20_term_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL, SHFT_RG_USB20_TERM_VREF_SEL,
str_rg_usb20_term_vref_sel);
MYDBG("Config VRT_VREF_SEL %s\n", str_rg_usb20_vrt_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL, SHFT_RG_USB20_VRT_VREF_SEL,
str_rg_usb20_vrt_vref_sel);
return count;
}
static ssize_t rg_usb20_term_vref_sel_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL, SHFT_RG_USB20_TERM_VREF_SEL,
buf);
return count;
}
static ssize_t rg_usb20_hstx_srctrl_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_HSTX_SRCTRL, SHFT_RG_USB20_HSTX_SRCTRL, buf);
return count;
}
static ssize_t rg_usb20_vrt_vref_sel_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL, SHFT_RG_USB20_VRT_VREF_SEL,
buf);
return count;
}
static ssize_t rg_usb20_intr_en_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width1(OFFSET_RG_USB20_INTR_EN, SHFT_RG_USB20_INTR_EN, buf);
return count;
}
static ssize_t rg_usb20_rev6_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_rev6_write(OFFSET_RG_USB20_PHY_REV6, SHFT_RG_USB20_PHY_REV6, buf);
return count;
}
static const struct file_operations usb_driving_capability_fops = {
.open = usb_driving_capability_open,
.write = usb_driving_capability_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_term_vref_sel_fops = {
.open = rg_usb20_term_vref_sel_open,
.write = rg_usb20_term_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_hstx_srctrl_fops = {
.open = rg_usb20_hstx_srctrl_open,
.write = rg_usb20_hstx_srctrl_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_vrt_vref_sel_fops = {
.open = rg_usb20_vrt_vref_sel_open,
.write = rg_usb20_vrt_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_intr_en_fops = {
.open = rg_usb20_intr_en_open,
.write = rg_usb20_intr_en_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_rev6_fops = {
.open = rg_usb20_rev6_open,
.write = rg_usb20_rev6_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
int usb20_phy_init_debugfs(void)
{
struct proc_dir_entry *root;
struct proc_dir_entry *file;
int ret;
proc_mkdir("mtk_usb", NULL);
root = proc_mkdir("mtk_usb/usb20_phy", NULL);
if (!root) {
ret = -ENOMEM;
goto err0;
}
file = proc_create(FILE_USB_DRIVING_CAPABILITY, 0644,
root, &usb_driving_capability_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_TERM_VREF_SEL, 0644,
root, &rg_usb20_term_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_HSTX_SRCTRL, 0644,
root, &rg_usb20_hstx_srctrl_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_VRT_VREF_SEL, 0644,
root, &rg_usb20_vrt_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_INTR_EN, 0644,
root, &rg_usb20_intr_en_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_PHY_REV6, 0644,
root, &rg_usb20_rev6_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
usb20_phy_procfs_root = root;
return 0;
err1:
proc_remove(root);
err0:
return ret;
}
void /* __init_or_exit */ usb20_phy_exit_debugfs(struct musb *musb)
{
proc_remove(usb20_phy_procfs_root);
}
@@ -0,0 +1,37 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
#
# for USB OTG silicon based on Mentor Graphics INVENTRA designs
#
ccflags-y += -I$(srctree)/drivers/misc/mediatek/usb20
# for battery related
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
# for SPM control usage
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
# for TYPEC connection management
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/inc
ifeq ($(CONFIG_TCPC_CLASS),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/tcpc/inc
endif
# for ep0 test
ccflags-y += -I$(srctree)/drivers/usb/core/
ccflags-y += -I$(srctree)/drivers/misc/mediatek/usb20
# Phy
obj-$(CONFIG_MTK_MUSB_PHY) += mtk_usb20_phy.o
mtk_usb20_phy-y += usb20_phy.o
mtk_usb20_phy-$(CONFIG_DEBUG_FS) += usb20_phy_debugfs.o
File diff suppressed because it is too large. Load diff
+137
View File
@@ -0,0 +1,137 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/scatterlist.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include "musb_core.h"
#ifdef CONFIG_MTK_MUSB_CARPLAY_SUPPORT
struct carplay_dev {
struct usb_interface *intf;
struct usb_device *dev;
};
struct carplay_dev *apple_dev;
bool apple;
int send_switch_cmd(void)
{
int retval;
if (apple_dev == NULL) {
DBG(0, "no apple device attach.\n");
return -1;
}
DBG(0, "before usb_control_msg\n");
retval = usb_control_msg(apple_dev->dev,
usb_rcvctrlpipe(apple_dev->dev, 0),
0x51, 0x40, 1, 0, NULL, 0,
USB_CTRL_GET_TIMEOUT);
DBG(0, "after usb_control_msg retval = %d\n", retval);
if (retval != 0) {
DBG(0, "%s fail retval = %d\n", __func__, retval);
return -1;
}
return 0;
}
static int carplay_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *udev;
struct carplay_dev *car_dev;
DBG(0, "++ carplay probe ++\n");
udev = interface_to_usbdev(intf);
car_dev = kzalloc(sizeof(*car_dev), GFP_KERNEL);
if (!car_dev)
return -ENOMEM;
usb_set_intfdata(intf, car_dev);
car_dev->dev = udev;
car_dev->intf = intf;
apple_dev = car_dev;
apple = true;
if (car_dev->dev == NULL)
DBG(0, "car_dev->dev error\n");
return 0;
}
static void carplay_disconnect(struct usb_interface *intf)
{
struct carplay_dev *car_dev = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
dev_dbg(&intf->dev, "disconnect\n");
car_dev->dev = NULL;
car_dev->intf = NULL;
kfree(car_dev);
apple_dev = NULL;
apple = false;
DBG(0, "%s.\n", __func__);
}
static const struct usb_device_id id_table[] = {
/*-------------------------------------------------------------*/
/* EZ-USB devices which download firmware to replace (or in our
* case augment) the default device implementation.
*/
/* generic EZ-USB FX2 controller (or development board) */
{USB_DEVICE(0x05ac, 0x12a8),
},
/*-------------------------------------------------------------*/
{}
};
/* MODULE_DEVICE_TABLE(usb, id_table); */
static struct usb_driver carplay_driver = {
.name = "carplay",
.id_table = id_table,
.probe = carplay_probe,
.disconnect = carplay_disconnect,
};
/*-------------------------------------------------------------------------*/
static int __init carplay_init(void)
{
DBG(0, "%s register carplay_driver\n", __func__);
return usb_register(&carplay_driver);
}
module_init(carplay_init);
static void __exit carplay_exit(void)
{
usb_deregister(&carplay_driver);
}
module_exit(carplay_exit);
MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
MODULE_LICENSE("GPL");
#endif
File diff suppressed because it is too large. Load diff
+143
View File
@@ -0,0 +1,143 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __USB20_H__
#define __USB20_H__
#ifdef CONFIG_FPGA_EARLY_PORTING
#define FPGA_PLATFORM
#endif
#include <linux/interrupt.h>
#include <musb.h>
#ifdef USB2_PHY_V2
#define USB_PHY_OFFSET 0x300
#else
#define USB_PHY_OFFSET 0x800
#endif
#define USBPHY_READ32(offset) \
readl((void __iomem *)(((unsigned long)\
mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE32(offset, value) \
writel(value, (void __iomem *)\
(((unsigned long)mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_SET32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) | (mask))
#define USBPHY_CLR32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) & (~(mask)))
struct mt_usb_work {
struct delayed_work dwork;
int ops;
};
/* ToDo: should be moved to glue */
extern struct musb *mtk_musb;
extern struct musb *musb;
struct mt_usb_glue {
struct device *dev;
struct platform_device *musb_pdev;
struct musb *mtk_musb;
/* common power & clock */
struct clk *musb_clk;
struct clk *musb_clk_top_sel;
struct clk *musb_clk_univpll3_d4;
#ifdef CONFIG_PHY_MTK_TPHY
struct platform_device *usb_phy;
struct phy *phy;
struct usb_phy *xceiv;
enum phy_mode phy_mode;
#endif
#ifdef CONFIG_MTK_MUSB_DUAL_ROLE
struct otg_switch_mtk otg_sx;
#endif
};
extern struct mt_usb_glue *glue;
#define glue_to_musb(g) platform_get_drvdata(g->musb)
extern int kernel_init_done;
/* specific USB fuctnion */
enum CABLE_MODE {
CABLE_MODE_CHRG_ONLY = 0,
CABLE_MODE_NORMAL,
CABLE_MODE_HOST_ONLY,
CABLE_MODE_MAX
};
enum USB_CLK_STATE {
NO_CHANGE = 0,
ON_TO_OFF,
OFF_TO_ON,
};
/* specific USB operation */
enum CONNECTION_OPS {
CONNECTION_OPS_DISC = 0,
CONNECTION_OPS_CHECK,
CONNECTION_OPS_CONN
};
enum VBUS_OPS {
VBUS_OPS_OFF = 0,
VBUS_OPS_ON
};
#ifdef CONFIG_MTK_UART_USB_SWITCH
enum PORT_MODE {
PORT_MODE_USB = 0,
PORT_MODE_UART,
PORT_MODE_MAX
};
extern bool usb_phy_check_in_uart_mode(void);
extern void usb_phy_switch_to_usb(void);
extern void usb_phy_switch_to_uart(void);
#endif
#ifdef FPGA_PLATFORM
extern void USB_PHY_Write_Register8(u8 var, u8 addr);
extern u8 USB_PHY_Read_Register8(u8 addr);
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
#define RG_GPIO_SELECT (0x600)
#define GPIO_SEL_OFFSET (4)
#define GPIO_SEL_MASK (0x7 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART0 (0x1 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART1 (0x2 << GPIO_SEL_OFFSET)
#define GET_GPIO_SEL_VAL(x) ((x & GPIO_SEL_MASK) >> GPIO_SEL_OFFSET)
extern void __iomem *ap_gpio_base;
extern bool in_uart_mode;
#endif
extern int usb20_phy_init_debugfs(void);
#ifdef CONFIG_USB_MTK_OTG
extern void mt_usb_otg_init(struct musb *musb);
extern void mt_usb_otg_exit(struct musb *musb);
extern int mt_usb_get_vbus_status(struct musb *musb);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
#endif
extern void musb_platform_reset(struct musb *musb);
extern bool usb_enable_clock(bool enable);
extern bool usb_prepare_clock(bool enable);
extern void usb_prepare_enable_clock(bool enable);
extern void mt_usb_dev_disconnect(void);
extern void set_usb_phy_clear(void);
/* usb host mode wakeup */
#define USB_WAKEUP_DEC_CON1 0x404
#define USB1_CDEN BIT(0)
#define USB1_CDDEBOUNCE(x) (((x) & 0xf) << 1)
#endif
@@ -0,0 +1,778 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <usb20.h>
#include <musb_io.h>
#include <mtk_musb_reg.h>
#include <musb_core.h>
#ifdef CONFIG_USB_MTK_OTG
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <mtk_musb.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#ifdef CONFIG_MTK_USB_TYPEC
#ifdef CONFIG_TCPC_CLASS
#include <tcpm.h>
#endif
#endif
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/phy/phy.h>
#ifdef CONFIG_MTK_MUSB_PHY
#include <usb20_phy.h>
#endif
MODULE_LICENSE("GPL v2");
#include <mt-plat/mtk_boot_common.h>
struct device_node *usb_node;
static int iddig_eint_num;
static ktime_t ktime_start, ktime_end;
static struct regulator *reg_vbus;
static struct musb_fifo_cfg fifo_cfg_host[] = {
{ .hw_ep_num = 1, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 1, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_RX,
.maxpacket = 64, .mode = BUF_SINGLE},
};
u32 delay_time = 15;
module_param(delay_time, int, 0644);
u32 delay_time1 = 55;
module_param(delay_time1, int, 0644);
u32 iddig_cnt;
module_param(iddig_cnt, int, 0644);
static bool vbus_on;
module_param(vbus_on, bool, 0644);
static int vbus_control;
module_param(vbus_control, int, 0644);
#ifdef CONFIG_MTK_MUSB_PHY
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
#endif
static void _set_vbus(int is_on)
{
if (!reg_vbus) {
DBG(0, "vbus_init\n");
reg_vbus = regulator_get(mtk_musb->controller, "usb-otg-vbus");
if (IS_ERR_OR_NULL(reg_vbus)) {
DBG(0, "failed to get vbus\n");
return;
}
}
DBG(0, "op<%d>, status<%d>\n", is_on, vbus_on);
if (is_on && !vbus_on) {
/* update flag 1st then enable VBUS to make
* host mode correct used by PMIC
*/
vbus_on = true;
if (regulator_set_voltage(reg_vbus, 5000000, 5000000))
DBG(0, "vbus regulator set voltage failed\n");
if (regulator_set_current_limit(reg_vbus, 1500000, 1800000))
DBG(0, "vbus regulator set current limit failed\n");
if (regulator_enable(reg_vbus))
DBG(0, "vbus regulator enable failed\n");
} else if (!is_on && vbus_on) {
/* disable VBUS 1st then update flag
* to make host mode correct used by PMIC
*/
vbus_on = false;
regulator_disable(reg_vbus);
}
}
int mt_usb_get_vbus_status(struct musb *musb)
{
return true;
#ifdef NEVER
int ret = 0;
if ((musb_readb(musb->mregs, MUSB_DEVCTL) &
MUSB_DEVCTL_VBUS) != MUSB_DEVCTL_VBUS)
ret = 1;
else
DBG(0, "VBUS error, devctl=%x, power=%d\n",
musb_readb(musb->mregs, MUSB_DEVCTL),
musb->power);
pr_debug("vbus ready = %d\n", ret);
return ret;
#endif
}
#if defined(CONFIG_USBIF_COMPLIANCE)
u32 sw_deboun_time = 1;
#else
u32 sw_deboun_time = 400;
#endif
module_param(sw_deboun_time, int, 0644);
u32 typec_control;
module_param(typec_control, int, 0644);
static bool typec_req_host;
static bool iddig_req_host;
static void do_host_work(struct work_struct *data);
static void issue_host_work(int ops, int delay, bool on_st)
{
struct mt_usb_work *work;
if (!mtk_musb) {
DBG(0, "mtk_musb = NULL\n");
return;
}
/* create and prepare worker */
work = kzalloc(sizeof(struct mt_usb_work), GFP_ATOMIC);
if (!work) {
DBG(0, "work is NULL, directly return\n");
return;
}
work->ops = ops;
INIT_DELAYED_WORK(&work->dwork, do_host_work);
/* issue connection work */
DBG(0, "issue work, ops<%d>, delay<%d>, on_st<%d>\n",
ops, delay, on_st);
if (on_st)
queue_delayed_work(mtk_musb->st_wq,
&work->dwork, msecs_to_jiffies(delay));
else
schedule_delayed_work(&work->dwork,
msecs_to_jiffies(delay));
}
void mt_usb_host_connect(int delay)
{
typec_req_host = true;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_CONN, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_connect);
void set_usb_phy_clear(void)
{
/* Clear USB phy U2PHYDTM1 */
USBPHY_CLR32(0x6c, (0xFFFF));
DBG(0, "Clear PHY setting, 0x6c=%x\n", USBPHY_READ32(0x6c));
}
void mt_usb_host_disconnect(int delay)
{
typec_req_host = false;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_DISC, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_disconnect);
static bool musb_is_host(void)
{
bool host_mode = 0;
if (typec_control)
host_mode = typec_req_host;
else
host_mode = iddig_req_host;
return host_mode;
}
void musb_session_restart(struct musb *musb)
{
void __iomem *mbase = musb->mregs;
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) & (~MUSB_DEVCTL_SESSION)));
#ifdef CONFIG_MTK_MUSB_PHY
DBG(0, "[MUSB] stopped session for VBUSERROR interrupt\n");
USBPHY_SET32(0x6c, (0x3c<<8));
USBPHY_SET32(0x6c, (0x10<<0));
USBPHY_CLR32(0x6c, (0x2c<<0));
DBG(0, "[MUSB] force PHY to idle, 0x6c=%x\n", USBPHY_READ32(0x6c));
mdelay(5);
USBPHY_CLR32(0x6c, (0x3c<<8));
USBPHY_CLR32(0x6c, (0x3c<<0));
DBG(0, "[MUSB] let PHY resample VBUS, 0x6c=%x\n"
, USBPHY_READ32(0x6c));
#endif
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) | MUSB_DEVCTL_SESSION));
DBG(0, "[MUSB] restart session\n");
}
EXPORT_SYMBOL(musb_session_restart);
static struct delayed_work host_plug_test_work;
int host_plug_test_enable; /* default disable */
module_param(host_plug_test_enable, int, 0644);
int host_plug_in_test_period_ms = 5000;
module_param(host_plug_in_test_period_ms, int, 0644);
int host_plug_out_test_period_ms = 5000;
module_param(host_plug_out_test_period_ms, int, 0644);
int host_test_vbus_off_time_us = 3000;
module_param(host_test_vbus_off_time_us, int, 0644);
int host_test_vbus_only = 1;
module_param(host_test_vbus_only, int, 0644);
static int host_plug_test_triggered;
void switch_int_to_device(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_HIGH);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
void switch_int_to_host(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_LOW);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
static void do_host_plug_test_work(struct work_struct *data)
{
static ktime_t ktime_begin, ktime_end;
static s64 diff_time;
static int host_on;
static struct wakeup_source *host_test_wakelock;
static int wake_lock_inited;
if (!wake_lock_inited) {
DBG(0, "wake_lock_init\n");
host_test_wakelock = wakeup_source_register(NULL,
"host.test.lock");
wake_lock_inited = 1;
}
host_plug_test_triggered = 1;
/* sync global status */
mb();
__pm_stay_awake(host_test_wakelock);
DBG(0, "BEGIN");
ktime_begin = ktime_get();
host_on = 1;
while (1) {
if (!musb_is_host() && host_on) {
DBG(0, "about to exit");
break;
}
msleep(50);
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_begin));
if (host_on && diff_time >= host_plug_in_test_period_ms) {
host_on = 0;
DBG(0, "OFF\n");
ktime_begin = ktime_get();
/* simulate plug out */
_set_vbus(0);
udelay(host_test_vbus_off_time_us);
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_DISC, 0, false);
} else if (!host_on && diff_time >=
host_plug_out_test_period_ms) {
host_on = 1;
DBG(0, "ON\n");
ktime_begin = ktime_get();
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_CONN, 0, false);
_set_vbus(1);
msleep(100);
}
}
/* wait host_work done */
msleep(1000);
host_plug_test_triggered = 0;
__pm_relax(host_test_wakelock);
DBG(0, "END\n");
}
#define ID_PIN_WORK_RECHECK_TIME 30 /* 30 ms */
#define ID_PIN_WORK_BLOCK_TIMEOUT 30000 /* 30000 ms */
static void do_host_work(struct work_struct *data)
{
u8 devctl = 0;
unsigned long flags;
static int inited, timeout; /* default to 0 */
static s64 diff_time;
bool host_on;
int usb_clk_state = NO_CHANGE;
struct mt_usb_work *work =
container_of(data, struct mt_usb_work, dwork.work);
struct mt_usb_glue *glue = mtk_musb->glue;
/*
* kernel_init_done should be set in
* early-init stage through init.$platform.usb.rc
*/
while (!inited && !kernel_init_done &&
!mtk_musb->is_ready && !timeout) {
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_start));
DBG_LIMIT(3,
"init_done:%d, is_ready:%d, inited:%d, TO:%d, diff:%lld",
kernel_init_done,
mtk_musb->is_ready,
inited,
timeout,
diff_time);
if (diff_time > ID_PIN_WORK_BLOCK_TIMEOUT) {
DBG(0, "diff_time:%lld\n", diff_time);
timeout = 1;
}
msleep(ID_PIN_WORK_RECHECK_TIME);
}
if (!inited) {
DBG(0, "PASS,init_done:%d,is_ready:%d,inited:%d, TO:%d\n",
kernel_init_done, mtk_musb->is_ready,
inited, timeout);
inited = 1;
}
/* always prepare clock and check if need to unprepater later */
/* clk_prepare_cnt +1 here */
usb_prepare_clock(true);
down(&mtk_musb->musb_lock);
host_on = (work->ops ==
CONNECTION_OPS_CONN ? true : false);
DBG(0, "work start, is_host=%d, host_on=%d\n",
mtk_musb->is_host, host_on);
if (host_on && !mtk_musb->is_host) {
/* switch to HOST state before turn on VBUS */
MUSB_HST_MODE(mtk_musb);
/* to make sure all event clear */
msleep(32);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (!usb_on_sram) {
int ret;
ret = gpd_switch_to_sram(mtk_musb->controller);
DBG(0, "gpd_switch_to_sram, ret<%d>\n", ret);
if (ret == 0)
usb_on_sram = 1;
}
#endif
/* setup fifo for host mode */
ep_config_from_table_for_host(mtk_musb);
if (!mtk_musb->host_suspend)
__pm_stay_awake(mtk_musb->usb_lock);
/* this make PHY operation workable */
musb_platform_enable(mtk_musb);
/* for no VBUS sensing IP*/
/* wait VBUS ready */
msleep(100);
/* clear session*/
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl&(~MUSB_DEVCTL_SESSION)));
phy_set_mode(glue->phy, PHY_MODE_INVALID);
/* wait */
mdelay(5);
/* restart session */
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl | MUSB_DEVCTL_SESSION));
phy_set_mode(glue->phy, PHY_MODE_USB_HOST);
musb_start(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_device(mtk_musb);
if (host_plug_test_enable && !host_plug_test_triggered)
queue_delayed_work(mtk_musb->st_wq,
&host_plug_test_work, 0);
usb_clk_state = OFF_TO_ON;
} else if (!host_on && mtk_musb->is_host) {
/* switch from host -> device */
/* for device no disconnect interrupt */
spin_lock_irqsave(&mtk_musb->lock, flags);
if (mtk_musb->is_active) {
DBG(0, "for not receiving disconnect interrupt\n");
usb_hcd_resume_root_hub(musb_to_hcd(mtk_musb));
musb_root_disconnect(mtk_musb);
}
spin_unlock_irqrestore(&mtk_musb->lock, flags);
DBG(1, "devctl is %x\n",
musb_readb(mtk_musb->mregs, MUSB_DEVCTL));
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, 0);
if (mtk_musb->usb_lock->active)
__pm_relax(mtk_musb->usb_lock);
/* for no VBUS sensing IP */
phy_set_mode(glue->phy, PHY_MODE_INVALID);
musb_stop(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_host(mtk_musb);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (usb_on_sram) {
gpd_switch_to_dram(mtk_musb->controller);
usb_on_sram = 0;
}
#endif
/* to make sure all event clear */
msleep(32);
mtk_musb->xceiv->otg->state = OTG_STATE_B_IDLE;
/* switch to DEV state after turn off VBUS */
MUSB_DEV_MODE(mtk_musb);
usb_clk_state = ON_TO_OFF;
}
DBG(0, "work end, is_host=%d\n", mtk_musb->is_host);
up(&mtk_musb->musb_lock);
if (usb_clk_state == ON_TO_OFF) {
/* clock on -> of: clk_prepare_cnt -2 */
usb_prepare_clock(false);
usb_prepare_clock(false);
} else if (usb_clk_state == NO_CHANGE) {
/* clock no change : clk_prepare_cnt -1 */
usb_prepare_clock(false);
}
/* free mt_usb_work */
kfree(work);
}
static irqreturn_t mt_usb_ext_iddig_int(int irq, void *dev_id)
{
iddig_cnt++;
iddig_req_host = !iddig_req_host;
DBG(0, "id pin assert, %s\n", iddig_req_host ?
"connect" : "disconnect");
if (iddig_req_host)
mt_usb_host_connect(0);
else
mt_usb_host_disconnect(0);
disable_irq_nosync(iddig_eint_num);
return IRQ_HANDLED;
}
static const struct of_device_id otg_iddig_of_match[] = {
{.compatible = "mediatek,usb_iddig_bi_eint"},
{},
};
static int otg_iddig_probe(struct platform_device *pdev)
{
int ret;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
iddig_eint_num = irq_of_parse_and_map(node, 0);
DBG(0, "iddig_eint_num<%d>\n", iddig_eint_num);
if (iddig_eint_num < 0)
return -ENODEV;
ret = request_irq(iddig_eint_num, mt_usb_ext_iddig_int,
IRQF_TRIGGER_LOW, "USB_IDDIG", NULL);
if (ret) {
DBG(0,
"request EINT <%d> fail, ret<%d>\n",
iddig_eint_num, ret);
return ret;
}
return 0;
}
static struct platform_driver otg_iddig_driver = {
.probe = otg_iddig_probe,
/* .remove = otg_iddig_remove, */
/* .shutdown = otg_iddig_shutdown, */
.driver = {
.name = "otg_iddig",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(otg_iddig_of_match),
},
};
static int iddig_int_init(void)
{
int ret = 0;
ret = platform_driver_register(&otg_iddig_driver);
if (ret)
DBG(0, "ret:%d\n", ret);
return 0;
}
void mt_usb_otg_init(struct musb *musb)
{
/* test */
INIT_DELAYED_WORK(&host_plug_test_work, do_host_plug_test_work);
ktime_start = ktime_get();
/* CONNECTION MANAGEMENT*/
#ifdef CONFIG_MTK_USB_TYPEC
DBG(0, "host controlled by TYPEC\n");
typec_control = 1;
#ifdef CONFIG_TCPC_CLASS
DBG(0, "host controlled by IDDIG\n");
iddig_int_init();
vbus_control = 1;
#endif /* CONFIG_TCPC_CLASS */
#endif /* CONFIG_MTK_USB_TYPEC */
/* EP table */
musb->fifo_cfg_host = fifo_cfg_host;
musb->fifo_cfg_host_size = ARRAY_SIZE(fifo_cfg_host);
}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb)
{
DBG(0, "OTG disable vbus\n");
}
EXPORT_SYMBOL(mt_usb_otg_exit);
enum {
DO_IT = 0,
REVERT,
};
#ifdef CONFIG_MTK_MUSB_PHY
static void bypass_disc_circuit(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 13-12 RG_USB20_HSRX_MMODE_SELE, dft:00 */
if (act == DO_IT) {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_SET32(0x18, (0x20<<8));
} else {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_CLR32(0x18, (0x20<<8));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
static void disc_threshold_to_max(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 7-4 RG_USB20_DISCTH, dft:1000 */
if (act == DO_IT) {
USBPHY_SET32(0x18, (0xf0<<0));
} else {
USBPHY_CLR32(0x18, (0x70<<0));
USBPHY_SET32(0x18, (0x80<<0));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
#endif
static int option;
static int set_option(const char *val, const struct kernel_param *kp)
{
int local_option;
int rv;
/* update module parameter */
rv = param_set_int(val, kp);
if (rv)
return rv;
/* update local_option */
rv = kstrtoint(val, 10, &local_option);
if (rv != 0)
return rv;
DBG(0, "option:%d, local_option:%d\n", option, local_option);
switch (local_option) {
case 0:
DBG(0, "case %d\n", local_option);
iddig_int_init();
break;
case 1:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(0);
break;
case 2:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(0);
break;
case 3:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(3000);
break;
case 4:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(3000);
break;
#ifdef CONFIG_MTK_MUSB_PHY
case 5:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(DO_IT);
break;
case 6:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(REVERT);
break;
case 7:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(DO_IT);
break;
case 8:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(REVERT);
break;
#endif
case 9:
DBG(0, "case %d\n", local_option);
_set_vbus(1);
break;
case 10:
DBG(0, "case %d\n", local_option);
_set_vbus(0);
break;
default:
break;
}
return 0;
}
static struct kernel_param_ops option_param_ops = {
.set = set_option,
.get = param_get_int,
};
module_param_cb(option, &option_param_ops, &option, 0644);
#else
#include "musb_core.h"
/* for not define CONFIG_USB_MTK_OTG */
void mt_usb_otg_init(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_exit);
void mt_usb_set_vbus(struct musb *musb, int is_on) {}
int mt_usb_get_vbus_status(struct musb *musb) {return 1; }
void switch_int_to_device(struct musb *musb) {}
void switch_int_to_host(struct musb *musb) {}
void musb_session_restart(struct musb *musb) {}
EXPORT_SYMBOL(musb_session_restart);
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,847 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#ifdef CONFIG_MTK_CLKMGR
#include <mach/mt_clkmgr.h>
#else
#include <linux/clk.h>
#endif
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <mtk_musb.h>
#include <musb_core.h>
#include "usb20.h"
#include <linux/nvmem-consumer.h>
#include <linux/phy/phy.h>
#ifdef CONFIG_OF
#include <linux/of_address.h>
#endif
#ifdef CONFIG_MTK_AEE_FEATURE
#include <mt-plat/aee.h>
#endif
#include <mt-plat/mtk_boot_common.h>
#define FRA (48)
#define PARA (28)
#ifdef FPGA_PLATFORM
bool usb_enable_clock(bool enable)
{
return true;
}
bool usb_prepare_clock(bool enable)
{
return true;
}
void usb_prepare_enable_clock(bool enable)
{
}
void usb_phy_poweron(void)
{
}
void usb_phy_savecurrent(void)
{
}
void usb_phy_recover(struct musb *musb)
{
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
}
void Charger_Detect_Release(void)
{
}
void usb_phy_context_save(void)
{
}
void usb_phy_context_restore(void)
{
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
return false;
}
void usb_phy_switch_to_uart(void)
{
}
void usb_phy_switch_to_usb(void)
{
}
#endif
#else
#include <linux/of_irq.h>
#include <linux/of_address.h>
#define VAL_MAX_WIDTH_2 0x3
#define VAL_MAX_WIDTH_3 0x7
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_PHY_REV6 0x18
#define SHFT_RG_USB20_PHY_REV6 30
void usb_phy_tuning(void)
{
static bool inited;
static s32 u2_vrt_ref, u2_term_ref, u2_enhance;
static struct device_node *of_node;
if (!inited) {
u2_vrt_ref = u2_term_ref = u2_enhance = -1;
of_node = of_find_compatible_node(NULL,
NULL, "mediatek,phy_tuning");
if (of_node) {
/* value won't be updated if property not being found */
of_property_read_u32(of_node,
"u2_vrt_ref", (u32 *) &u2_vrt_ref);
of_property_read_u32(of_node,
"u2_term_ref", (u32 *) &u2_term_ref);
of_property_read_u32(of_node,
"u2_enhance", (u32 *) &u2_enhance);
}
inited = true;
} else if (!of_node)
return;
if (u2_vrt_ref != -1) {
if (u2_vrt_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_VRT_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_VRT_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_VRT_VREF_SEL,
u2_vrt_ref << SHFT_RG_USB20_VRT_VREF_SEL);
}
}
if (u2_term_ref != -1) {
if (u2_term_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_TERM_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_TERM_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_TERM_VREF_SEL,
u2_term_ref << SHFT_RG_USB20_TERM_VREF_SEL);
}
}
if (u2_enhance != -1) {
if (u2_enhance <= VAL_MAX_WIDTH_2) {
USBPHY_CLR32(OFFSET_RG_USB20_PHY_REV6,
VAL_MAX_WIDTH_2 << SHFT_RG_USB20_PHY_REV6);
USBPHY_SET32(OFFSET_RG_USB20_PHY_REV6,
u2_enhance<<SHFT_RG_USB20_PHY_REV6);
}
}
}
#ifdef CONFIG_MTK_USB2JTAG_SUPPORT
int usb2jtag_usb_init(void)
{
struct device_node *node = NULL;
void __iomem *usb_phy_base;
u32 temp;
#if defined(CONFIG_MACH_MT6765)
node = of_find_compatible_node(NULL, NULL, "mediatek,mt6765-usb20");
#elif defined(CONFIG_MACH_MT6761)
node = of_find_compatible_node(NULL, NULL, "mediatek,mt6761-usb20");
#endif
if (!node) {
pr_notice("[USB2JTAG] map node @ mediatek,USB0 failed\n");
return -1;
}
usb_phy_base = of_iomap(node, 1);
if (!usb_phy_base) {
pr_notice("[USB2JTAG] iomap usb_phy_base failed\n");
return -1;
}
/* rg_usb20_gpio_ctl: bit[9] = 1 */
temp = readl(usb_phy_base + 0x820);
writel(temp | (1 << 9), usb_phy_base + 0x820);
/* RG_USB20_BGR_EN: bit[0] = 1 */
temp = readl(usb_phy_base + 0x800);
writel(temp | (1 << 0), usb_phy_base + 0x800);
/* RG_USB20_BC11_SW_EN: bit[23] = 0 */
temp = readl(usb_phy_base + 0x818);
writel(temp & ~(1 << 23), usb_phy_base + 0x818);
/* wait stable */
mdelay(1);
iounmap(usb_phy_base);
return 0;
}
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool in_uart_mode;
#endif
void usb_prepare_enable_clock(bool enable)
{
if (enable) {
usb_prepare_clock(true);
usb_enable_clock(true);
} else {
usb_enable_clock(false);
usb_prepare_clock(false);
}
}
DEFINE_MUTEX(prepare_lock);
static atomic_t clk_prepare_cnt = ATOMIC_INIT(0);
bool usb_prepare_clock(bool enable)
{
int before_cnt = atomic_read(&clk_prepare_cnt);
mutex_lock(&prepare_lock);
if (IS_ERR_OR_NULL(musb_clk) ||
IS_ERR_OR_NULL(musb_clk_top_sel) ||
IS_ERR_OR_NULL(musb_clk_univpll3_d4)) {
DBG(0, "clk not ready\n");
mutex_unlock(&prepare_lock);
return 0;
}
if (enable) {
if (clk_prepare(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel prepare fail\n");
} else {
if (clk_set_parent(musb_clk_top_sel,
musb_clk_univpll3_d4))
DBG(0, "musb_clk_top_sel set_parent fail\n");
}
if (clk_prepare(musb_clk))
DBG(0, "musb_clk prepare fail\n");
atomic_inc(&clk_prepare_cnt);
} else {
clk_unprepare(musb_clk_top_sel);
clk_unprepare(musb_clk);
atomic_dec(&clk_prepare_cnt);
}
mutex_unlock(&prepare_lock);
DBG(1, "enable(%d), usb prepare_cnt, before(%d), after(%d)\n",
enable, before_cnt, atomic_read(&clk_prepare_cnt));
#ifdef CONFIG_MTK_AEE_FEATURE
if (atomic_read(&clk_prepare_cnt) < 0)
aee_kernel_warning("usb20", "usb clock prepare_cnt error\n");
#endif
return 1;
}
static DEFINE_SPINLOCK(musb_reg_clock_lock);
bool usb_enable_clock(bool enable)
{
static int count;
static int real_enable = 0, real_disable;
static int virt_enable = 0, virt_disable;
unsigned long flags;
DBG(1, "enable(%d),count(%d),<%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
spin_lock_irqsave(&musb_reg_clock_lock, flags);
if (unlikely(atomic_read(&clk_prepare_cnt) <= 0)) {
DBG_LIMIT(1, "clock not prepare");
goto exit;
}
if (enable && count == 0) {
if (clk_enable(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel enable fail\n");
goto exit;
}
if (clk_enable(musb_clk)) {
DBG(0, "musb_clk enable fail\n");
clk_disable(musb_clk_top_sel);
goto exit;
}
usb_hal_dpidle_request(USB_DPIDLE_FORBIDDEN);
real_enable++;
} else if (!enable && count == 1) {
clk_disable(musb_clk);
clk_disable(musb_clk_top_sel);
usb_hal_dpidle_request(USB_DPIDLE_ALLOWED);
real_disable++;
}
if (enable)
count++;
else
count = (count == 0) ? 0 : (count - 1);
exit:
if (enable)
virt_enable++;
else
virt_disable++;
spin_unlock_irqrestore(&musb_reg_clock_lock, flags);
DBG(1, "enable(%d),count(%d), <%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
return 1;
}
static void hs_slew_rate_cal(void)
{
unsigned long data;
unsigned long x;
unsigned char value;
unsigned long start_time, timeout;
unsigned int timeout_flag = 0;
/* enable usb ring oscillator. */
USBPHY_SET32(0x14, (0x1 << 15));
/* wait 1us. */
udelay(1);
/* enable free run clock */
USBPHY_SET32(0xF10 - 0x800, (0x01 << 8));
/* setting cyclecnt. */
USBPHY_SET32(0xF00 - 0x800, (0x04 << 8));
/* enable frequency meter */
USBPHY_SET32(0xF00 - 0x800, (0x01 << 24));
/* wait for frequency valid. */
start_time = jiffies;
timeout = jiffies + 3 * HZ;
while (!((USBPHY_READ32(0xF10 - 0x800) & 0xFF) == 0x1)) {
if (time_after(jiffies, timeout)) {
timeout_flag = 1;
break;
}
}
/* read result. */
if (timeout_flag) {
DBG(0, "[USBPHY] Slew Rate Calibration: Timeout\n");
value = 0x4;
} else {
data = USBPHY_READ32(0xF0C - 0x800);
x = ((1024 * FRA * PARA) / data);
value = (unsigned char)(x / 1000);
if ((x - value * 1000) / 100 >= 5)
value += 1;
DBG(1, "[USBPHY]slew calibration:FM_OUT =%lu,x=%lu,value=%d\n",
data, x, value);
}
/* disable Frequency and disable free run clock. */
USBPHY_CLR32(0xF00 - 0x800, (0x01 << 24));
USBPHY_CLR32(0xF10 - 0x800, (0x01 << 8));
#define MSK_RG_USB20_HSTX_SRCTRL 0x7
/* all clr first then set */
USBPHY_CLR32(0x14, (MSK_RG_USB20_HSTX_SRCTRL << 12));
USBPHY_SET32(0x14, ((value & MSK_RG_USB20_HSTX_SRCTRL) << 12));
/* disable usb ring oscillator. */
USBPHY_CLR32(0x14, (0x1 << 15));
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
u32 usb_port_mode;
usb_enable_clock(true);
udelay(50);
usb_port_mode = USBPHY_READ32(0x68);
usb_enable_clock(false);
if (((usb_port_mode >> 30) & 0x3) == 1) {
DBG(0, "%s:%d - IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = true;
} else {
DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = false;
}
return in_uart_mode;
}
void usb_phy_switch_to_uart(void)
{
unsigned int val = 0;
if (usb_phy_check_in_uart_mode()) {
DBG(0, "Already in UART mode.\n");
return;
}
udelay(50);
/* RG_USB20_BC11_SW_EN 0x11F4_0818[23] = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* Set RG_SUSPENDM 0x11F4_0868[3] to 1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force suspendm 0x11F4_0868[18] = 1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b01 */
USBPHY_CLR32(0x68, (0x3 << 30));
USBPHY_SET32(0x68, (0x1 << 30));
/* force_uart_i 0x11F4_0868[29] = 0*/
USBPHY_CLR32(0x68, (0x1 << 29));
/* force_uart_bias_en 0x11F4_0868[28] = 1 */
USBPHY_SET32(0x68, (0x1 << 28));
/* force_uart_tx_oe 0x11F4_0868[27] = 1 */
USBPHY_SET32(0x68, (0x1 << 27));
/* force_uart_en 0x11F4_0868[26] = 1 */
USBPHY_SET32(0x68, (0x1 << 26));
/* RG_UART_BIAS_EN 0x11F4_086c[18] = 1 */
USBPHY_SET32(0x6C, (0x1 << 18));
/* RG_UART_TX_OE 0x11F4_086c[17] = 1 */
USBPHY_SET32(0x6C, (0x1 << 17));
/* Set RG_UART_EN to 1 */
USBPHY_SET32(0x6C, (0x1 << 16));
/* Set RG_USB20_DM_100K_EN to 1 */
USBPHY_SET32(0x20, (0x1 << 17));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
val = readl(ap_gpio_base);
writel(val | (GPIO_SEL_UART0), ap_gpio_base);
in_uart_mode = true;
}
void usb_phy_switch_to_usb(void)
{
unsigned int val = 0;
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
/* clear force_uart_en */
USBPHY_CLR32(0x68, (0x1 << 26));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b00 */
USBPHY_CLR32(0x68, (0x3 << 30));
in_uart_mode = false;
usb_phy_poweron();
}
#endif
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
void usb_rev6_setting(int value)
{
static int direct_return;
if (direct_return)
return;
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
if (value)
USBPHY_SET32(0x18, (value << 24));
else
direct_return = 1;
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_poweron(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec for PHY3.3v/1.8v stable. */
udelay(50);
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* rg_usb20_dp_100k_mode 1'b1 0x20 18
* USB20_DP_100K_EN 1'b0 0x20 16
* RG_USB20_DM_100K_EN 1'b0 0x20 17
* RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
* force_suspendm 1'b0 0x68 18
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 9) | (0x1 << 8)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* rg_usb20_dp_100k_mode, 1'b1 */
USBPHY_SET32(0x20, (0x1 << 18));
/* USB20_DP_100K_EN 1'b0, RG_USB20_DM_100K_EN, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 16) | (0x1 << 17)));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
USBPHY_SET32(0x18, (0x40 << 24));
/* wait for 800 usec. */
udelay(800);
DBG(0, "usb power on success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
static void usb_phy_savecurrent_internal(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* RG_USB20_OTG_VBUSCMP_EN 1'b0 0x18 20
* RG_SUSPENDM 1'b1 0x68 03
* force_suspendm 1'b1 0x68 18
* RG_DPPULLDOWN 1'b1 0x68 06
* RG_DMPULLDOWN 1'b1 0x68 07
* RG_XCVRSEL[1:0] 2'b01 0x68 [04-05]
* RG_TERMSEL 1'b1 0x68 02
* RG_DATAIN[3:0] 4'b0000 0x68 [10-13]
* force_dp_pulldown 1'b1 0x68 20
* force_dm_pulldown 1'b1 0x68 21
* force_xcversel 1'b1 0x68 19
* force_termsel 1'b1 0x68 17
* force_datain 1'b1 0x68 23
* RG_SUSPENDM 1'b0 0x68 03
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 20));
/* RG_SUSPENDM, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force_suspendm, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b01. */
USBPHY_CLR32(0x68, (0x3 << 4));
USBPHY_SET32(0x68, (0x1 << 4));
/* RG_TERMSEL, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b1, force_dm_pulldown, 1'b1,
* force_xcversel, 1'b1, force_termsel, 1'b1, force_datain, 1'b1
*/
USBPHY_SET32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
udelay(800);
/* RG_SUSPENDM, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 3));
udelay(1);
set_usb_phy_mode(PHY_MODE_INVALID);
}
void usb_phy_savecurrent(void)
{
usb_phy_savecurrent_internal();
DBG(0, "usb save current success\n");
}
unsigned int usb_phy_get_efuse_val(struct device *dev)
{
unsigned int efuse_val = 0;
struct nvmem_cell *cell;
u32 *buf;
size_t len;
/* M_ANALOG8[4:0] => RG_USB20_INTR_CAL[4:0] */
cell = nvmem_cell_get(dev, "efuse_idx107");
if (cell == NULL) {
DBG(0, "nvmem_cell_get return NULL");
return 0;
}
if (IS_ERR(cell)) {
DBG(0, "can not get efuse_cell");
return 0;
}
buf = (u32 *)nvmem_cell_read(cell, &len);
nvmem_cell_put(cell);
if (IS_ERR(buf)) {
DBG(0, "can not get efuse_buf");
return 0;
}
efuse_val = (*buf & (0x1f<<0)) >> 0;
kfree(buf);
return efuse_val;
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_recover(struct musb *musb)
{
unsigned int efuse_val = 0;
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec. */
udelay(50);
/*
* 04.force_uart_en 1'b0 0x68 26
* 04.RG_UART_EN 1'b0 0x6C 16
* 04.rg_usb20_gpio_ctl 1'b0 0x20 09
* 04.usb20_gpio_mode 1'b0 0x20 08
* 05.force_suspendm 1'b0 0x68 18
* 06.RG_DPPULLDOWN 1'b0 0x68 06
* 07.RG_DMPULLDOWN 1'b0 0x68 07
* 08.RG_XCVRSEL[1:0] 2'b00 0x68 [04:05]
* 09.RG_TERMSEL 1'b0 0x68 02
* 10.RG_DATAIN[3:0] 4'b0000 0x68 [10:13]
* 11.force_dp_pulldown 1'b0 0x68 20
* 12.force_dm_pulldown 1'b0 0x68 21
* 13.force_xcversel 1'b0 0x68 19
* 14.force_termsel 1'b0 0x68 17
* 15.force_datain 1'b0 0x68 23
* 16.RG_USB20_BC11_SW_EN 1'b0 0x18 23
* 17.RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
*/
/* clean PUPD_BIST_EN */
/* PUPD_BIST_EN = 1'b0 */
/* PMIC will use it to detect charger type */
/* NEED?? USBPHY_CLR8(0x1d, 0x10);*/
USBPHY_CLR32(0x1c, (0x1 << 12));
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6C, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b00. */
USBPHY_CLR32(0x68, (0x3 << 4));
/* RG_TERMSEL, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b0, force_dm_pulldown, 1'b0,
* force_xcversel, 1'b0, force_termsel, 1'b0, force_datain, 1'b0
*/
USBPHY_CLR32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
usb_rev6_setting(0x40);
/* wait 800 usec. */
udelay(800);
/* force enter device mode */
set_usb_phy_mode(PHY_MODE_USB_DEVICE);
hs_slew_rate_cal();
efuse_val = musb->efuse_val;
if (efuse_val) {
DBG(0, "apply efuse setting, RG_USB20_INTR_CAL=0x%x\n",
efuse_val);
USBPHY_CLR32(0x04, (0x1F<<19));
/* Add pass margin */
USBPHY_SET32(0x04, ((efuse_val+2)<<19));
}
/* disc threshold to max, RG_USB20_DISCTH[7:4], dft:1000, MAX:1111 */
USBPHY_SET32(0x18, (0xf0<<0));
usb_phy_tuning();
DBG(0, "usb recovery success\n");
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
/* RG_USB20_BC11_SW_EN = 1'b1 */
USBPHY_SET32(0x18, (0x1 << 23));
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Init);
void Charger_Detect_Release(void)
{
usb_prepare_enable_clock(true);
/* RG_USB20_BC11_SW_EN = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
udelay(1);
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Release);
void usb_phy_context_save(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
in_uart_mode = usb_phy_check_in_uart_mode();
#endif
}
void usb_phy_context_restore(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode)
usb_phy_switch_to_uart();
#endif
}
#endif
@@ -0,0 +1,723 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <musb_core.h>
#include <musb_debug.h>
#include <mtk_musb.h>
/* general */
#define BIT_WIDTH_1 1
#define MSK_WIDTH_1 0x1
#define VAL_MAX_WDITH_1 0x1
#define VAL_0_WIDTH_1 0x0
#define VAL_1_WIDTH_1 0x1
#define STRNG_0_WIDTH_1 "0"
#define STRNG_1_WIDTH_1 "1"
#define BIT_WIDTH_2 2
#define MSK_WIDTH_2 0x3
#define VAL_MAX_WDITH_2 0x3
#define VAL_0_WIDTH_2 0x0
#define VAL_1_WIDTH_2 0x1
#define VAL_2_WIDTH_2 0x2
#define VAL_3_WIDTH_2 0x3
#define STRNG_0_WIDTH_2 "00"
#define STRNG_1_WIDTH_2 "01"
#define STRNG_2_WIDTH_2 "10"
#define STRNG_3_WIDTH_2 "11"
#define BIT_WIDTH_3 3
#define MSK_WIDTH_3 0x7
#define VAL_MAX_WDITH_3 0x7
#define VAL_0_WIDTH_3 0x0
#define VAL_1_WIDTH_3 0x1
#define VAL_2_WIDTH_3 0x2
#define VAL_3_WIDTH_3 0x3
#define VAL_4_WIDTH_3 0x4
#define VAL_5_WIDTH_3 0x5
#define VAL_6_WIDTH_3 0x6
#define VAL_7_WIDTH_3 0x7
#define STRNG_0_WIDTH_3 "000"
#define STRNG_1_WIDTH_3 "001"
#define STRNG_2_WIDTH_3 "010"
#define STRNG_3_WIDTH_3 "011"
#define STRNG_4_WIDTH_3 "100"
#define STRNG_5_WIDTH_3 "101"
#define STRNG_6_WIDTH_3 "110"
#define STRNG_7_WIDTH_3 "111"
/* specific */
#define FILE_USB_DRIVING_CAPABILITY "USB_DRIVING_CAPABILITY"
#define FILE_RG_USB20_TERM_VREF_SEL "RG_USB20_TERM_VREF_SEL"
#define MSK_RG_USB20_TERM_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define FILE_RG_USB20_HSTX_SRCTRL "RG_USB20_HSTX_SRCTRL"
#define MSK_RG_USB20_HSTX_SRCTRL MSK_WIDTH_3
#define SHFT_RG_USB20_HSTX_SRCTRL 12
#define OFFSET_RG_USB20_HSTX_SRCTRL 0x14
#define FILE_RG_USB20_VRT_VREF_SEL "RG_USB20_VRT_VREF_SEL"
#define MSK_RG_USB20_VRT_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define FILE_RG_USB20_INTR_EN "RG_USB20_INTR_EN"
#define MSK_RG_USB20_INTR_EN MSK_WIDTH_1
#define SHFT_RG_USB20_INTR_EN 5
#define OFFSET_RG_USB20_INTR_EN 0x0
#define FILE_RG_USB20_PHY_REV6 "RG_USB20_PHY_REV6"
#define MSK_RG_USB20_PHY_REV6 MSK_WIDTH_2
#define SHFT_RG_USB20_PHY_REV6 30
#define OFFSET_RG_USB20_PHY_REV6 0x18
static struct dentry *usb20_phy_debugfs_root;
void usb20_phy_debugfs_write_width1(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_1);
clr_val = VAL_1_WIDTH_1;
}
if (!strncmp(buf, STRNG_1_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_1);
set_val = VAL_1_WIDTH_1;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_1 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n"
, __func__, __LINE__);
}
}
void usb20_phy_debugfs_rev6_write(u8 offset, u8 shift, char *buf)
{
u8 set_val = 0xFF;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_2);
set_val = VAL_0_WIDTH_2;
}
if (!strncmp(buf, STRNG_1_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_2);
set_val = VAL_1_WIDTH_2;
}
if (!strncmp(buf, STRNG_2_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_2);
set_val = VAL_2_WIDTH_2;
}
if (!strncmp(buf, STRNG_3_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_2);
set_val = VAL_3_WIDTH_2;
}
if (set_val <= VAL_MAX_WDITH_2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> case offset:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, set_val);
USBPHY_CLR32(offset, (VAL_MAX_WDITH_2<<shift));
USBPHY_SET32(offset, (set_val<<shift));
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
void usb20_phy_debugfs_write_width3(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
clr_val = VAL_7_WIDTH_3;
}
if (!strncmp(buf, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
set_val = VAL_1_WIDTH_3;
}
if (!strncmp(buf, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
set_val = VAL_2_WIDTH_3;
}
if (!strncmp(buf, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
set_val = VAL_3_WIDTH_3;
}
if (!strncmp(buf, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
set_val = VAL_4_WIDTH_3;
}
if (!strncmp(buf, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
set_val = VAL_5_WIDTH_3;
}
if (!strncmp(buf, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
set_val = VAL_6_WIDTH_3;
}
if (!strncmp(buf, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
set_val = VAL_7_WIDTH_3;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_3 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__, offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
u8 usb20_phy_debugfs_read_val(u8 offset, u8 shft, u8 msk, u8 width, char *str)
{
u32 val;
int i, temp;
val = USBPHY_READ32(offset);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val >> shft;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val & msk;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
temp = val;
str[width] = '\0';
for (i = (width - 1); i >= 0; i--) {
if (val % 2)
str[i] = '1';
else
str[i] = '0';
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str[%d]:%c\n\n",
__func__, __LINE__, i, str[i]);
val /= 2;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str(%s)\n",
__func__, __LINE__, str);
return val;
}
static int usb_driving_capability_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
u8 combined_val, tmp_val = 0xff;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL, BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
combined_val = tmp_val;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL,
BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
if ((tmp_val == (combined_val - 1)) || (tmp_val == combined_val))
combined_val += tmp_val;
else
combined_val = tmp_val * (VAL_MAX_WDITH_3 + 1) + combined_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
seq_printf(s, "%d", combined_val);
return 0;
}
static int rg_usb20_term_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL,
BIT_WIDTH_3, str);
seq_printf(s, "%s", str);
return 0;
}
static int rg_usb20_hstx_srctrl_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL,
MSK_RG_USB20_HSTX_SRCTRL, BIT_WIDTH_3, str);
seq_printf(s, "%s", str);
return 0;
}
static int rg_usb20_vrt_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL, BIT_WIDTH_3, str);
seq_printf(s, "%s", str);
return 0;
}
static int rg_usb20_intr_en_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN,
MSK_RG_USB20_INTR_EN, BIT_WIDTH_1, str);
seq_printf(s, "%s", str);
return 0;
}
static int rg_usb20_rev6_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6,
MSK_RG_USB20_PHY_REV6, BIT_WIDTH_2, str);
seq_printf(s, "%s", str);
return 0;
}
static int usb_driving_capability_open(struct inode *inode, struct file *file)
{
return single_open(file, usb_driving_capability_show, inode->i_private);
}
static int rg_usb20_term_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_term_vref_sel_show, inode->i_private);
}
static int rg_usb20_hstx_srctrl_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_hstx_srctrl_show, inode->i_private);
}
static int rg_usb20_vrt_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_vrt_vref_sel_show, inode->i_private);
}
static int rg_usb20_intr_en_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_intr_en_show, inode->i_private);
}
static int rg_usb20_rev6_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_rev6_show, inode->i_private);
}
void val_to_bstring_width3(u8 val, char *str)
{
if (val == VAL_0_WIDTH_3)
memcpy(str, STRNG_0_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_1_WIDTH_3)
memcpy(str, STRNG_1_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_2_WIDTH_3)
memcpy(str, STRNG_2_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_3_WIDTH_3)
memcpy(str, STRNG_3_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_4_WIDTH_3)
memcpy(str, STRNG_4_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_5_WIDTH_3)
memcpy(str, STRNG_5_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_6_WIDTH_3)
memcpy(str, STRNG_6_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_7_WIDTH_3)
memcpy(str, STRNG_7_WIDTH_3, BIT_WIDTH_3 + 1);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), str(%s)\n",
__func__, __LINE__, val, str);
}
static ssize_t usb_driving_capability_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
u8 val, tmp_val;
char str_rg_usb20_term_vref_sel[18], str_rg_usb20_vrt_vref_sel[18];
memset(buf, 0x00, sizeof(buf));
pr_notice("\n");
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtol(buf, 10, (long *)&val) != 0) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtol, err(%d)\n)\n",
__func__, __LINE__, kstrtol(buf, 10, (long *)&val));
return count;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtol, val(%d)\n)\n",
__func__, __LINE__, val);
if (val > VAL_7_WIDTH_3 * 2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> wrong val set(%d), direct return\n",
__func__, __LINE__, val);
return count;
}
tmp_val = val;
val /= 2;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), tmp_val(%d)\n",
__func__, __LINE__, val, tmp_val);
val_to_bstring_width3(tmp_val - val, str_rg_usb20_term_vref_sel);
val_to_bstring_width3(val, str_rg_usb20_vrt_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config TERM_VREF_SEL %s\n",
__func__, __LINE__, str_rg_usb20_term_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
str_rg_usb20_term_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config VRT_VREF_SEL %s\n\n",
__func__, __LINE__, str_rg_usb20_vrt_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
str_rg_usb20_vrt_vref_sel);
return count;
}
static ssize_t rg_usb20_term_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_hstx_srctrl_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL, buf);
return count;
}
static ssize_t rg_usb20_vrt_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_intr_en_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width1(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN, buf);
return count;
}
static ssize_t rg_usb20_rev6_write(struct file *file,
const char __user *ubuf, size_t count,
loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_rev6_write(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6, buf);
return count;
}
static const struct file_operations usb_driving_capability_fops = {
.open = usb_driving_capability_open,
.write = usb_driving_capability_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_term_vref_sel_fops = {
.open = rg_usb20_term_vref_sel_open,
.write = rg_usb20_term_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_hstx_srctrl_fops = {
.open = rg_usb20_hstx_srctrl_open,
.write = rg_usb20_hstx_srctrl_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_vrt_vref_sel_fops = {
.open = rg_usb20_vrt_vref_sel_open,
.write = rg_usb20_vrt_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_intr_en_fops = {
.open = rg_usb20_intr_en_open,
.write = rg_usb20_intr_en_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_rev6_fops = {
.open = rg_usb20_rev6_open,
.write = rg_usb20_rev6_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
int usb20_phy_init_debugfs(void)
{
struct dentry *root;
struct dentry *file;
int ret;
root = debugfs_create_dir("usb20_phy", NULL);
if (!root) {
ret = -ENOMEM;
goto err0;
}
file = debugfs_create_file(FILE_USB_DRIVING_CAPABILITY, 0644,
root, NULL, &usb_driving_capability_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = debugfs_create_file(FILE_RG_USB20_TERM_VREF_SEL, 0644,
root, NULL, &rg_usb20_term_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = debugfs_create_file(FILE_RG_USB20_HSTX_SRCTRL, 0644,
root, NULL, &rg_usb20_hstx_srctrl_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = debugfs_create_file(FILE_RG_USB20_VRT_VREF_SEL, 0644,
root, NULL, &rg_usb20_vrt_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = debugfs_create_file(FILE_RG_USB20_INTR_EN, 0644,
root, NULL, &rg_usb20_intr_en_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = debugfs_create_file(FILE_RG_USB20_PHY_REV6, 0644,
root, NULL, &rg_usb20_rev6_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
usb20_phy_debugfs_root = root;
return 0;
err1:
debugfs_remove_recursive(root);
err0:
return ret;
}
void /* __init_or_exit */ usb20_phy_exit_debugfs(struct musb *musb)
{
debugfs_remove_recursive(usb20_phy_debugfs_root);
}
@@ -0,0 +1,36 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
#
# for USB OTG silicon based on Mentor Graphics INVENTRA designs
#
ccflags-$(CONFIG_USB_MTK_HDRC) += -I$(srctree)/drivers/misc/mediatek/usb20
# for battery related
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
# for SPM control usage
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
# for TYPEC connection management
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/inc
ifeq ($(CONFIG_TCPC_CLASS),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/tcpc/inc
endif
# for ep0 test
ccflags-y += -I$(srctree)/drivers/usb/core/
# Phy
obj-$(CONFIG_MTK_MUSB_PHY) += mtk_usb20_phy.o
mtk_usb20_phy-y += usb20_phy.o
mtk_usb20_phy-$(CONFIG_DEBUG_FS) += usb20_phy_debugfs.o
File diff suppressed because it is too large. Load diff
+136
View File
@@ -0,0 +1,136 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/scatterlist.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include "musb_core.h"
#ifdef CONFIG_MTK_MUSB_CARPLAY_SUPPORT
struct carplay_dev {
struct usb_interface *intf;
struct usb_device *dev;
};
struct carplay_dev *apple_dev;
bool apple;
int send_switch_cmd(void)
{
int retval;
if (apple_dev == NULL) {
DBG(0, "no apple device attach.\n");
return -1;
}
DBG(0, "before usb_control_msg\n");
retval = usb_control_msg(apple_dev->dev,
usb_rcvctrlpipe(apple_dev->dev, 0),
0x51, 0x40, 1, 0, NULL, 0,
USB_CTRL_GET_TIMEOUT);
DBG(0, "after usb_control_msg retval = %d\n", retval);
if (retval != 0) {
DBG(0, "%s fail retval = %d\n", __func__, retval);
return -1;
}
return 0;
}
static int carplay_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *udev;
struct carplay_dev *car_dev;
DBG(0, "++ carplay probe ++\n");
udev = interface_to_usbdev(intf);
car_dev = kzalloc(sizeof(*car_dev), GFP_KERNEL);
if (!car_dev)
return -ENOMEM;
usb_set_intfdata(intf, car_dev);
car_dev->dev = udev;
car_dev->intf = intf;
apple_dev = car_dev;
apple = true;
if (car_dev->dev == NULL)
DBG(0, "car_dev->dev error\n");
return 0;
}
static void carplay_disconnect(struct usb_interface *intf)
{
struct carplay_dev *car_dev = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
dev_dbg(&intf->dev, "disconnect\n");
car_dev->dev = NULL;
car_dev->intf = NULL;
kfree(car_dev);
apple_dev = NULL;
apple = false;
DBG(0, "%s.\n", __func__);
}
static const struct usb_device_id id_table[] = {
/*-------------------------------------------------------------*/
/* EZ-USB devices which download firmware to replace (or in our
* case augment) the default device implementation.
*/
/* generic EZ-USB FX2 controller (or development board) */
{USB_DEVICE(0x05ac, 0x12a8),
},
/*-------------------------------------------------------------*/
{}
};
/* MODULE_DEVICE_TABLE(usb, id_table); */
static struct usb_driver carplay_driver = {
.name = "carplay",
.id_table = id_table,
.probe = carplay_probe,
.disconnect = carplay_disconnect,
};
/*-------------------------------------------------------------------------*/
static int __init carplay_init(void)
{
DBG(0, "%s register carplay_driver\n", __func__);
return usb_register(&carplay_driver);
}
module_init(carplay_init);
static void __exit carplay_exit(void)
{
usb_deregister(&carplay_driver);
}
module_exit(carplay_exit);
MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
MODULE_LICENSE("GPL");
#endif
File diff suppressed because it is too large. Load diff
+139
View File
@@ -0,0 +1,139 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __USB20_H__
#define __USB20_H__
#ifdef CONFIG_FPGA_EARLY_PORTING
#define FPGA_PLATFORM
#endif
#include <linux/interrupt.h>
#include <musb.h>
struct mt_usb_work {
struct delayed_work dwork;
int ops;
};
/* ToDo: should be moved to glue */
extern struct musb *mtk_musb;
extern struct musb *musb;
struct mt_usb_glue {
struct device *dev;
struct platform_device *musb_pdev;
struct musb *mtk_musb;
/* common power & clock */
struct clk *musb_clk;
struct clk *musb_clk_top_sel;
struct clk *musb_clk_univpll3_d4;
#ifdef CONFIG_PHY_MTK_TPHY
struct platform_device *usb_phy;
struct phy *phy;
struct usb_phy *xceiv;
enum phy_mode phy_mode;
#endif
#ifdef CONFIG_MTK_MUSB_DUAL_ROLE
struct otg_switch_mtk otg_sx;
#endif
};
extern struct mt_usb_glue *glue;
#define glue_to_musb(g) platform_get_drvdata(g->musb)
extern int kernel_init_done;
extern unsigned int upmu_get_rgs_chrdet(void);
extern bool upmu_is_chr_det(void);
extern enum charger_type mt_charger_type_detection(void);
extern void BATTERY_SetUSBState(int usb_state);
extern void upmu_interrupt_chrdet_int_en(unsigned int val);
/* specific USB fuctnion */
enum CABLE_MODE {
CABLE_MODE_CHRG_ONLY = 0,
CABLE_MODE_NORMAL,
CABLE_MODE_HOST_ONLY,
CABLE_MODE_MAX
};
enum USB_CLK_STATE {
NO_CHANGE = 0,
ON_TO_OFF,
OFF_TO_ON,
};
/* specific USB operation */
enum CONNECTION_OPS {
CONNECTION_OPS_DISC = 0,
CONNECTION_OPS_CHECK,
CONNECTION_OPS_CONN
};
enum VBUS_OPS {
VBUS_OPS_OFF = 0,
VBUS_OPS_ON
};
#ifdef CONFIG_MTK_UART_USB_SWITCH
enum PORT_MODE {
PORT_MODE_USB = 0,
PORT_MODE_UART,
PORT_MODE_MAX
};
extern bool usb_phy_check_in_uart_mode(void);
extern void usb_phy_switch_to_usb(void);
extern void usb_phy_switch_to_uart(void);
#endif
#ifdef FPGA_PLATFORM
extern void USB_PHY_Write_Register8(u8 var, u8 addr);
extern u8 USB_PHY_Read_Register8(u8 addr);
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
#define RG_GPIO_SELECT (0x600)
#define GPIO_SEL_OFFSET (4)
#define GPIO_SEL_MASK (0x7 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART0 (0x1 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART1 (0x2 << GPIO_SEL_OFFSET)
#define GET_GPIO_SEL_VAL(x) ((x & GPIO_SEL_MASK) >> GPIO_SEL_OFFSET)
extern void __iomem *ap_gpio_base;
extern bool in_uart_mode;
#endif
extern int usb20_phy_init_debugfs(void);
extern enum charger_type mt_get_charger_type(void);
#ifndef CONFIG_FPGA_EARLY_PORTING
#include <upmu_common.h>
#endif
#define PHY_IDLE_MODE 0
#define PHY_DEV_ACTIVE 1
#define PHY_HOST_ACTIVE 2
void set_usb_phy_mode(int mode);
#ifdef CONFIG_USB_MTK_OTG
extern void mt_usb_otg_init(struct musb *musb);
extern void mt_usb_otg_exit(struct musb *musb);
extern int mt_usb_get_vbus_status(struct musb *musb);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
#endif
extern void musb_platform_reset(struct musb *musb);
extern bool usb_enable_clock(bool enable);
extern bool usb_prepare_clock(bool enable);
extern void usb_prepare_enable_clock(bool enable);
/* usb host mode wakeup */
#define USB_WAKEUP_DEC_CON1 0x404
#define USB1_CDEN BIT(0)
#define USB1_CDDEBOUNCE(x) (((x) & 0xf) << 1)
#endif
@@ -0,0 +1,762 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <usb20.h>
#include <musb_io.h>
#include <mtk_musb_reg.h>
#include <musb_core.h>
#ifdef CONFIG_USB_MTK_OTG
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <mtk_musb.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#ifdef CONFIG_MTK_USB_TYPEC
#ifdef CONFIG_TCPC_CLASS
#include <tcpm.h>
#endif
#endif
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/phy/phy.h>
#ifdef CONFIG_MTK_MUSB_PHY
#include <usb20_phy.h>
#endif
MODULE_LICENSE("GPL v2");
#include <mt-plat/mtk_boot_common.h>
struct device_node *usb_node;
static int iddig_eint_num;
static ktime_t ktime_start, ktime_end;
static struct regulator *reg_vbus;
static struct musb_fifo_cfg fifo_cfg_host[] = {
{ .hw_ep_num = 1, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 1, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_RX,
.maxpacket = 64, .mode = BUF_SINGLE},
};
u32 delay_time = 15;
module_param(delay_time, int, 0644);
u32 delay_time1 = 55;
module_param(delay_time1, int, 0644);
u32 iddig_cnt;
module_param(iddig_cnt, int, 0644);
static bool vbus_on;
module_param(vbus_on, bool, 0644);
static int vbus_control;
module_param(vbus_control, int, 0644);
#ifdef CONFIG_MTK_MUSB_PHY
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
#endif
static void _set_vbus(int is_on)
{
if (!reg_vbus) {
DBG(0, "vbus_init\n");
reg_vbus = regulator_get(mtk_musb->controller, "usb-otg-vbus");
if (IS_ERR_OR_NULL(reg_vbus)) {
DBG(0, "failed to get vbus\n");
return;
}
}
DBG(0, "op<%d>, status<%d>\n", is_on, vbus_on);
if (is_on && !vbus_on) {
/* update flag 1st then enable VBUS to make
* host mode correct used by PMIC
*/
vbus_on = true;
if (regulator_set_voltage(reg_vbus, 5000000, 5000000))
DBG(0, "vbus regulator set voltage failed\n");
if (regulator_set_current_limit(reg_vbus, 1500000, 1800000))
DBG(0, "vbus regulator set current limit failed\n");
if (regulator_enable(reg_vbus))
DBG(0, "vbus regulator enable failed\n");
} else if (!is_on && vbus_on) {
/* disable VBUS 1st then update flag
* to make host mode correct used by PMIC
*/
vbus_on = false;
regulator_disable(reg_vbus);
}
}
int mt_usb_get_vbus_status(struct musb *musb)
{
#if 1
return true;
#else
int ret = 0;
if ((musb_readb(musb->mregs, MUSB_DEVCTL) &
MUSB_DEVCTL_VBUS) != MUSB_DEVCTL_VBUS)
ret = 1;
else
DBG(0, "VBUS error, devctl=%x, power=%d\n",
musb_readb(musb->mregs, MUSB_DEVCTL),
musb->power);
pr_debug("vbus ready = %d\n", ret);
return ret;
#endif
}
#if defined(CONFIG_USBIF_COMPLIANCE)
u32 sw_deboun_time = 1;
#else
u32 sw_deboun_time = 400;
#endif
module_param(sw_deboun_time, int, 0644);
u32 typec_control;
module_param(typec_control, int, 0644);
static bool typec_req_host;
static bool iddig_req_host;
static void do_host_work(struct work_struct *data);
static void issue_host_work(int ops, int delay, bool on_st)
{
struct mt_usb_work *work;
if (!mtk_musb) {
DBG(0, "mtk_musb = NULL\n");
return;
}
/* create and prepare worker */
work = kzalloc(sizeof(struct mt_usb_work), GFP_ATOMIC);
if (!work) {
DBG(0, "work is NULL, directly return\n");
return;
}
work->ops = ops;
INIT_DELAYED_WORK(&work->dwork, do_host_work);
/* issue connection work */
DBG(0, "issue work, ops<%d>, delay<%d>, on_st<%d>\n",
ops, delay, on_st);
if (on_st)
queue_delayed_work(mtk_musb->st_wq,
&work->dwork, msecs_to_jiffies(delay));
else
schedule_delayed_work(&work->dwork,
msecs_to_jiffies(delay));
}
void mt_usb_host_connect(int delay)
{
typec_req_host = true;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_CONN, delay, true);
}
void mt_usb_host_disconnect(int delay)
{
typec_req_host = false;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_DISC, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_disconnect);
static bool musb_is_host(void)
{
bool host_mode = 0;
if (typec_control)
host_mode = typec_req_host;
else
host_mode = iddig_req_host;
return host_mode;
}
void musb_session_restart(struct musb *musb)
{
void __iomem *mbase = musb->mregs;
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) & (~MUSB_DEVCTL_SESSION)));
#ifdef CONFIG_MTK_MUSB_PHY
DBG(0, "[MUSB] stopped session for VBUSERROR interrupt\n");
USBPHY_SET32(0x6c, (0x3c<<8));
USBPHY_SET32(0x6c, (0x10<<0));
USBPHY_CLR32(0x6c, (0x2c<<0));
DBG(0, "[MUSB] force PHY to idle, 0x6c=%x\n", USBPHY_READ32(0x6c));
mdelay(5);
USBPHY_CLR32(0x6c, (0x3c<<8));
USBPHY_CLR32(0x6c, (0x3c<<0));
DBG(0, "[MUSB] let PHY resample VBUS, 0x6c=%x\n"
, USBPHY_READ32(0x6c));
#endif
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) | MUSB_DEVCTL_SESSION));
DBG(0, "[MUSB] restart session\n");
}
EXPORT_SYMBOL(musb_session_restart);
static struct delayed_work host_plug_test_work;
int host_plug_test_enable; /* default disable */
module_param(host_plug_test_enable, int, 0644);
int host_plug_in_test_period_ms = 5000;
module_param(host_plug_in_test_period_ms, int, 0644);
int host_plug_out_test_period_ms = 5000;
module_param(host_plug_out_test_period_ms, int, 0644);
int host_test_vbus_off_time_us = 3000;
module_param(host_test_vbus_off_time_us, int, 0644);
int host_test_vbus_only = 1;
module_param(host_test_vbus_only, int, 0644);
static int host_plug_test_triggered;
void switch_int_to_device(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_HIGH);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
void switch_int_to_host(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_LOW);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
static void do_host_plug_test_work(struct work_struct *data)
{
static ktime_t ktime_begin, ktime_end;
static s64 diff_time;
static int host_on;
static struct wakeup_source *host_test_wakelock;
static int wake_lock_inited;
if (!wake_lock_inited) {
DBG(0, "wake_lock_init\n");
host_test_wakelock = wakeup_source_register(NULL,
"host.test.lock");
wake_lock_inited = 1;
}
host_plug_test_triggered = 1;
/* sync global status */
mb();
__pm_stay_awake(host_test_wakelock);
DBG(0, "BEGIN");
ktime_begin = ktime_get();
host_on = 1;
while (1) {
if (!musb_is_host() && host_on) {
DBG(0, "about to exit");
break;
}
msleep(50);
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_begin));
if (host_on && diff_time >= host_plug_in_test_period_ms) {
host_on = 0;
DBG(0, "OFF\n");
ktime_begin = ktime_get();
/* simulate plug out */
_set_vbus(0);
udelay(host_test_vbus_off_time_us);
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_DISC, 0, false);
} else if (!host_on && diff_time >=
host_plug_out_test_period_ms) {
host_on = 1;
DBG(0, "ON\n");
ktime_begin = ktime_get();
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_CONN, 0, false);
_set_vbus(1);
msleep(100);
}
}
/* wait host_work done */
msleep(1000);
host_plug_test_triggered = 0;
__pm_relax(host_test_wakelock);
DBG(0, "END\n");
}
#define ID_PIN_WORK_RECHECK_TIME 30 /* 30 ms */
#define ID_PIN_WORK_BLOCK_TIMEOUT 30000 /* 30000 ms */
static void do_host_work(struct work_struct *data)
{
u8 devctl = 0;
unsigned long flags;
static int inited, timeout; /* default to 0 */
static s64 diff_time;
bool host_on;
int usb_clk_state = NO_CHANGE;
struct mt_usb_work *work =
container_of(data, struct mt_usb_work, dwork.work);
struct mt_usb_glue *glue = mtk_musb->glue;
/*
* kernel_init_done should be set in
* early-init stage through init.$platform.usb.rc
*/
while (!inited && !kernel_init_done &&
!mtk_musb->is_ready && !timeout) {
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_start));
DBG_LIMIT(3,
"init_done:%d, is_ready:%d, inited:%d, TO:%d, diff:%lld",
kernel_init_done,
mtk_musb->is_ready,
inited,
timeout,
diff_time);
if (diff_time > ID_PIN_WORK_BLOCK_TIMEOUT) {
DBG(0, "diff_time:%lld\n", diff_time);
timeout = 1;
}
msleep(ID_PIN_WORK_RECHECK_TIME);
}
if (!inited) {
DBG(0, "PASS,init_done:%d,is_ready:%d,inited:%d, TO:%d\n",
kernel_init_done, mtk_musb->is_ready,
inited, timeout);
inited = 1;
}
/* always prepare clock and check if need to unprepater later */
/* clk_prepare_cnt +1 here */
usb_prepare_clock(true);
down(&mtk_musb->musb_lock);
host_on = (work->ops ==
CONNECTION_OPS_CONN ? true : false);
DBG(0, "work start, is_host=%d, host_on=%d\n",
mtk_musb->is_host, host_on);
if (host_on && !mtk_musb->is_host) {
/* switch to HOST state before turn on VBUS */
MUSB_HST_MODE(mtk_musb);
/* to make sure all event clear */
msleep(32);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (!usb_on_sram) {
int ret;
ret = gpd_switch_to_sram(mtk_musb->controller);
DBG(0, "gpd_switch_to_sram, ret<%d>\n", ret);
if (ret == 0)
usb_on_sram = 1;
}
#endif
/* setup fifo for host mode */
ep_config_from_table_for_host(mtk_musb);
if (!mtk_musb->host_suspend)
__pm_stay_awake(mtk_musb->usb_lock);
/* this make PHY operation workable */
musb_platform_enable(mtk_musb);
/* for no VBUS sensing IP*/
/* wait VBUS ready */
msleep(100);
/* clear session*/
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl&(~MUSB_DEVCTL_SESSION)));
phy_set_mode(glue->phy, PHY_MODE_INVALID);
/* wait */
mdelay(5);
/* restart session */
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl | MUSB_DEVCTL_SESSION));
phy_set_mode(glue->phy, PHY_MODE_USB_HOST);
musb_start(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_device(mtk_musb);
if (host_plug_test_enable && !host_plug_test_triggered)
queue_delayed_work(mtk_musb->st_wq,
&host_plug_test_work, 0);
usb_clk_state = OFF_TO_ON;
} else if (!host_on && mtk_musb->is_host) {
/* switch from host -> device */
/* for device no disconnect interrupt */
spin_lock_irqsave(&mtk_musb->lock, flags);
if (mtk_musb->is_active) {
DBG(0, "for not receiving disconnect interrupt\n");
usb_hcd_resume_root_hub(musb_to_hcd(mtk_musb));
musb_root_disconnect(mtk_musb);
}
spin_unlock_irqrestore(&mtk_musb->lock, flags);
DBG(1, "devctl is %x\n",
musb_readb(mtk_musb->mregs, MUSB_DEVCTL));
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, 0);
if (mtk_musb->usb_lock->active)
__pm_relax(mtk_musb->usb_lock);
/* for no VBUS sensing IP */
phy_set_mode(glue->phy, PHY_MODE_INVALID);
musb_stop(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_host(mtk_musb);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (usb_on_sram) {
gpd_switch_to_dram(mtk_musb->controller);
usb_on_sram = 0;
}
#endif
/* to make sure all event clear */
msleep(32);
mtk_musb->xceiv->otg->state = OTG_STATE_B_IDLE;
/* switch to DEV state after turn off VBUS */
MUSB_DEV_MODE(mtk_musb);
usb_clk_state = ON_TO_OFF;
}
DBG(0, "work end, is_host=%d\n", mtk_musb->is_host);
up(&mtk_musb->musb_lock);
if (usb_clk_state == ON_TO_OFF) {
/* clock on -> of: clk_prepare_cnt -2 */
usb_prepare_clock(false);
usb_prepare_clock(false);
} else if (usb_clk_state == NO_CHANGE) {
/* clock no change : clk_prepare_cnt -1 */
usb_prepare_clock(false);
}
/* free mt_usb_work */
kfree(work);
}
static irqreturn_t mt_usb_ext_iddig_int(int irq, void *dev_id)
{
iddig_cnt++;
iddig_req_host = !iddig_req_host;
DBG(0, "id pin assert, %s\n", iddig_req_host ?
"connect" : "disconnect");
if (iddig_req_host)
mt_usb_host_connect(0);
else
mt_usb_host_disconnect(0);
disable_irq_nosync(iddig_eint_num);
return IRQ_HANDLED;
}
static const struct of_device_id otg_iddig_of_match[] = {
{.compatible = "mediatek,usb_iddig_bi_eint"},
{},
};
static int otg_iddig_probe(struct platform_device *pdev)
{
int ret;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
iddig_eint_num = irq_of_parse_and_map(node, 0);
DBG(0, "iddig_eint_num<%d>\n", iddig_eint_num);
if (iddig_eint_num < 0)
return -ENODEV;
ret = request_irq(iddig_eint_num, mt_usb_ext_iddig_int,
IRQF_TRIGGER_LOW, "USB_IDDIG", NULL);
if (ret) {
DBG(0,
"request EINT <%d> fail, ret<%d>\n",
iddig_eint_num, ret);
return ret;
}
return 0;
}
static struct platform_driver otg_iddig_driver = {
.probe = otg_iddig_probe,
/* .remove = otg_iddig_remove, */
/* .shutdown = otg_iddig_shutdown, */
.driver = {
.name = "otg_iddig",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(otg_iddig_of_match),
},
};
static int iddig_int_init(void)
{
int ret = 0;
ret = platform_driver_register(&otg_iddig_driver);
if (ret)
DBG(0, "ret:%d\n", ret);
return 0;
}
void mt_usb_otg_init(struct musb *musb)
{
/* test */
INIT_DELAYED_WORK(&host_plug_test_work, do_host_plug_test_work);
ktime_start = ktime_get();
/* CONNECTION MANAGEMENT*/
#ifdef CONFIG_MTK_USB_TYPEC
DBG(0, "host controlled by TYPEC\n");
typec_control = 1;
#ifdef CONFIG_TCPC_CLASS
DBG(0, "host controlled by IDDIG\n");
iddig_int_init();
vbus_control = 1;
#endif /* CONFIG_TCPC_CLASS */
#endif /* CONFIG_MTK_USB_TYPEC */
/* EP table */
musb->fifo_cfg_host = fifo_cfg_host;
musb->fifo_cfg_host_size = ARRAY_SIZE(fifo_cfg_host);
}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb)
{
DBG(0, "OTG disable vbus\n");
}
EXPORT_SYMBOL(mt_usb_otg_exit);
enum {
DO_IT = 0,
REVERT,
};
#ifdef CONFIG_MTK_MUSB_PHY
static void bypass_disc_circuit(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 13-12 RG_USB20_HSRX_MMODE_SELE, dft:00 */
if (act == DO_IT) {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_SET32(0x18, (0x20<<8));
} else {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_CLR32(0x18, (0x20<<8));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
static void disc_threshold_to_max(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 7-4 RG_USB20_DISCTH, dft:1000 */
if (act == DO_IT) {
USBPHY_SET32(0x18, (0xf0<<0));
} else {
USBPHY_CLR32(0x18, (0x70<<0));
USBPHY_SET32(0x18, (0x80<<0));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
#endif
static int option;
static int set_option(const char *val, const struct kernel_param *kp)
{
int local_option;
int rv;
/* update module parameter */
rv = param_set_int(val, kp);
if (rv)
return rv;
/* update local_option */
rv = kstrtoint(val, 10, &local_option);
if (rv != 0)
return rv;
DBG(0, "option:%d, local_option:%d\n", option, local_option);
switch (local_option) {
case 0:
DBG(0, "case %d\n", local_option);
iddig_int_init();
break;
case 1:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(0);
break;
case 2:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(0);
break;
case 3:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(3000);
break;
case 4:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(3000);
break;
#ifdef CONFIG_MTK_MUSB_PHY
case 5:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(DO_IT);
break;
case 6:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(REVERT);
break;
case 7:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(DO_IT);
break;
case 8:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(REVERT);
break;
#endif
case 9:
DBG(0, "case %d\n", local_option);
_set_vbus(1);
break;
case 10:
DBG(0, "case %d\n", local_option);
_set_vbus(0);
break;
default:
break;
}
return 0;
}
static struct kernel_param_ops option_param_ops = {
.set = set_option,
.get = param_get_int,
};
module_param_cb(option, &option_param_ops, &option, 0644);
#else
#include "musb_core.h"
/* for not define CONFIG_USB_MTK_OTG */
void mt_usb_otg_init(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_exit);
void mt_usb_set_vbus(struct musb *musb, int is_on) {}
int mt_usb_get_vbus_status(struct musb *musb) {return 1; }
void switch_int_to_device(struct musb *musb) {}
void switch_int_to_host(struct musb *musb) {}
void musb_session_restart(struct musb *musb) {}
EXPORT_SYMBOL(musb_session_restart);
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,848 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifdef CONFIG_MTK_CLKMGR
#include <mach/mt_clkmgr.h>
#else
#include <linux/clk.h>
#endif
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <mtk_musb.h>
#include <musb_core.h>
#include "usb20.h"
#include "mtk_devinfo.h"
#include <linux/phy/phy.h>
#ifdef CONFIG_OF
#include <linux/of_address.h>
#endif
#ifdef CONFIG_MTK_AEE_FEATURE
#include <mt-plat/aee.h>
#endif
#include <mt-plat/mtk_boot_common.h>
#define FRA (48)
#define PARA (28)
#ifdef FPGA_PLATFORM
bool usb_enable_clock(bool enable)
{
return true;
}
bool usb_prepare_clock(bool enable)
{
return true;
}
void usb_prepare_enable_clock(bool enable)
{
}
void usb_phy_poweron(void)
{
}
void usb_phy_savecurrent(void)
{
}
void usb_phy_recover(void)
{
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
}
void Charger_Detect_Release(void)
{
}
void usb_phy_context_save(void)
{
}
void usb_phy_context_restore(void)
{
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
return false;
}
void usb_phy_switch_to_uart(void)
{
}
void usb_phy_switch_to_usb(void)
{
}
#endif
#else
#include <linux/of_irq.h>
#include <linux/of_address.h>
#define VAL_MAX_WIDTH_2 0x3
#define VAL_MAX_WIDTH_3 0x7
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_PHY_REV6 0x18
#define SHFT_RG_USB20_PHY_REV6 30
void usb_phy_tuning(void)
{
static bool inited;
static s32 u2_vrt_ref, u2_term_ref, u2_enhance;
static struct device_node *of_node;
if (!inited) {
u2_vrt_ref = u2_term_ref = u2_enhance = -1;
of_node = of_find_compatible_node(NULL,
NULL, "mediatek,phy_tuning");
if (of_node) {
/* value won't be updated if property not being found */
of_property_read_u32(of_node,
"u2_vrt_ref", (u32 *) &u2_vrt_ref);
of_property_read_u32(of_node,
"u2_term_ref", (u32 *) &u2_term_ref);
of_property_read_u32(of_node,
"u2_enhance", (u32 *) &u2_enhance);
}
inited = true;
} else if (!of_node)
return;
if (u2_vrt_ref != -1) {
if (u2_vrt_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_VRT_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_VRT_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_VRT_VREF_SEL,
u2_vrt_ref << SHFT_RG_USB20_VRT_VREF_SEL);
}
}
if (u2_term_ref != -1) {
if (u2_term_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_TERM_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_TERM_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_TERM_VREF_SEL,
u2_term_ref << SHFT_RG_USB20_TERM_VREF_SEL);
}
}
if (u2_enhance != -1) {
if (u2_enhance <= VAL_MAX_WIDTH_2) {
USBPHY_CLR32(OFFSET_RG_USB20_PHY_REV6,
VAL_MAX_WIDTH_2 << SHFT_RG_USB20_PHY_REV6);
USBPHY_SET32(OFFSET_RG_USB20_PHY_REV6,
u2_enhance<<SHFT_RG_USB20_PHY_REV6);
}
}
}
#ifdef CONFIG_MTK_USB2JTAG_SUPPORT
int usb2jtag_usb_init(void)
{
struct device_node *node = NULL;
void __iomem *usb_phy_base;
u32 temp;
node = of_find_compatible_node(NULL, NULL, "mediatek,mt6768-usb20");
if (!node) {
pr_err("[USB2JTAG] map node @ mediatek,USB0 failed\n");
return -1;
}
usb_phy_base = of_iomap(node, 1);
if (!usb_phy_base) {
pr_err("[USB2JTAG] iomap usb_phy_base failed\n");
return -1;
}
/* rg_usb20_gpio_ctl: bit[9] = 1 */
temp = readl(usb_phy_base + 0x820);
writel(temp | (1 << 9), usb_phy_base + 0x820);
/* RG_USB20_BGR_EN: bit[0] = 1 */
temp = readl(usb_phy_base + 0x800);
writel(temp | (1 << 0), usb_phy_base + 0x800);
/* RG_USB20_BC11_SW_EN: bit[23] = 0 */
temp = readl(usb_phy_base + 0x818);
writel(temp & ~(1 << 23), usb_phy_base + 0x818);
/* wait stable */
mdelay(1);
iounmap(usb_phy_base);
return 0;
}
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool in_uart_mode;
#endif
void usb_prepare_enable_clock(bool enable)
{
if (enable) {
usb_prepare_clock(true);
usb_enable_clock(true);
} else {
usb_enable_clock(false);
usb_prepare_clock(false);
}
}
DEFINE_MUTEX(prepare_lock);
static atomic_t clk_prepare_cnt = ATOMIC_INIT(0);
bool usb_prepare_clock(bool enable)
{
int before_cnt = atomic_read(&clk_prepare_cnt);
mutex_lock(&prepare_lock);
if (IS_ERR_OR_NULL(musb_clk) ||
IS_ERR_OR_NULL(musb_clk_top_sel) ||
IS_ERR_OR_NULL(musb_clk_univpll3_d4)) {
DBG(0, "clk not ready\n");
mutex_unlock(&prepare_lock);
return 0;
}
if (enable) {
if (clk_prepare(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel prepare fail\n");
} else {
if (clk_set_parent(musb_clk_top_sel,
musb_clk_univpll3_d4))
DBG(0, "musb_clk_top_sel set_parent fail\n");
}
if (clk_prepare(musb_clk))
DBG(0, "musb_clk prepare fail\n");
atomic_inc(&clk_prepare_cnt);
} else {
clk_unprepare(musb_clk_top_sel);
clk_unprepare(musb_clk);
atomic_dec(&clk_prepare_cnt);
}
mutex_unlock(&prepare_lock);
DBG(1, "enable(%d), usb prepare_cnt, before(%d), after(%d)\n",
enable, before_cnt, atomic_read(&clk_prepare_cnt));
#ifdef CONFIG_MTK_AEE_FEATURE
if (atomic_read(&clk_prepare_cnt) < 0)
aee_kernel_warning("usb20", "usb clock prepare_cnt error\n");
#endif
return 1;
}
static DEFINE_SPINLOCK(musb_reg_clock_lock);
bool usb_enable_clock(bool enable)
{
static int count;
static int real_enable = 0, real_disable;
static int virt_enable = 0, virt_disable;
unsigned long flags;
DBG(1, "enable(%d),count(%d),<%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
spin_lock_irqsave(&musb_reg_clock_lock, flags);
if (unlikely(atomic_read(&clk_prepare_cnt) <= 0)) {
DBG_LIMIT(1, "clock not prepare");
goto exit;
}
if (enable && count == 0) {
if (clk_enable(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel enable fail\n");
goto exit;
}
if (clk_enable(musb_clk)) {
DBG(0, "musb_clk enable fail\n");
clk_disable(musb_clk_top_sel);
goto exit;
}
usb_hal_dpidle_request(USB_DPIDLE_FORBIDDEN);
real_enable++;
} else if (!enable && count == 1) {
clk_disable(musb_clk);
clk_disable(musb_clk_top_sel);
usb_hal_dpidle_request(USB_DPIDLE_ALLOWED);
real_disable++;
}
if (enable)
count++;
else
count = (count == 0) ? 0 : (count - 1);
exit:
if (enable)
virt_enable++;
else
virt_disable++;
spin_unlock_irqrestore(&musb_reg_clock_lock, flags);
DBG(1, "enable(%d),count(%d), <%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
return 1;
}
static void hs_slew_rate_cal(void)
{
unsigned long data;
unsigned long x;
unsigned char value;
unsigned long start_time, timeout;
unsigned int timeout_flag = 0;
/* enable usb ring oscillator. */
USBPHY_SET32(0x14, (0x1 << 15));
/* wait 1us. */
udelay(1);
/* enable free run clock */
USBPHY_SET32(0xF10 - 0x800, (0x01 << 8));
/* setting cyclecnt. */
USBPHY_SET32(0xF00 - 0x800, (0x04 << 8));
/* enable frequency meter */
USBPHY_SET32(0xF00 - 0x800, (0x01 << 24));
/* wait for frequency valid. */
start_time = jiffies;
timeout = jiffies + 3 * HZ;
while (!((USBPHY_READ32(0xF10 - 0x800) & 0xFF) == 0x1)) {
if (time_after(jiffies, timeout)) {
timeout_flag = 1;
break;
}
}
/* read result. */
if (timeout_flag) {
DBG(0, "[USBPHY] Slew Rate Calibration: Timeout\n");
value = 0x4;
} else {
data = USBPHY_READ32(0xF0C - 0x800);
x = ((1024 * FRA * PARA) / data);
value = (unsigned char)(x / 1000);
if ((x - value * 1000) / 100 >= 5)
value += 1;
DBG(1, "[USBPHY]slew calibration:FM_OUT =%lu,x=%lu,value=%d\n",
data, x, value);
}
/* disable Frequency and disable free run clock. */
USBPHY_CLR32(0xF00 - 0x800, (0x01 << 24));
USBPHY_CLR32(0xF10 - 0x800, (0x01 << 8));
#define MSK_RG_USB20_HSTX_SRCTRL 0x7
/* all clr first then set */
USBPHY_CLR32(0x14, (MSK_RG_USB20_HSTX_SRCTRL << 12));
USBPHY_SET32(0x14, ((value & MSK_RG_USB20_HSTX_SRCTRL) << 12));
/* disable usb ring oscillator. */
USBPHY_CLR32(0x14, (0x1 << 15));
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
u32 usb_port_mode;
usb_enable_clock(true);
udelay(50);
usb_port_mode = USBPHY_READ32(0x68);
usb_enable_clock(false);
if (((usb_port_mode >> 30) & 0x3) == 1) {
DBG(0, "%s:%d - IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = true;
} else {
DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = false;
}
return in_uart_mode;
}
void usb_phy_switch_to_uart(void)
{
unsigned int val = 0;
if (usb_phy_check_in_uart_mode()) {
DBG(0, "Already in UART mode.\n");
return;
}
udelay(50);
/* RG_USB20_BC11_SW_EN 0x11F4_0818[23] = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* Set RG_SUSPENDM 0x11F4_0868[3] to 1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force suspendm 0x11F4_0868[18] = 1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b01 */
USBPHY_CLR32(0x68, (0x3 << 30));
USBPHY_SET32(0x68, (0x1 << 30));
/* force_uart_i 0x11F4_0868[29] = 0*/
USBPHY_CLR32(0x68, (0x1 << 29));
/* force_uart_bias_en 0x11F4_0868[28] = 1 */
USBPHY_SET32(0x68, (0x1 << 28));
/* force_uart_tx_oe 0x11F4_0868[27] = 1 */
USBPHY_SET32(0x68, (0x1 << 27));
/* force_uart_en 0x11F4_0868[26] = 1 */
USBPHY_SET32(0x68, (0x1 << 26));
/* RG_UART_BIAS_EN 0x11F4_086c[18] = 1 */
USBPHY_SET32(0x6C, (0x1 << 18));
/* RG_UART_TX_OE 0x11F4_086c[17] = 1 */
USBPHY_SET32(0x6C, (0x1 << 17));
/* Set RG_UART_EN to 1 */
USBPHY_SET32(0x6C, (0x1 << 16));
/* Set RG_USB20_DM_100K_EN to 1 */
USBPHY_SET32(0x20, (0x1 << 17));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
val = readl(ap_gpio_base);
writel(val | (GPIO_SEL_UART0), ap_gpio_base);
in_uart_mode = true;
}
void usb_phy_switch_to_usb(void)
{
unsigned int val = 0;
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
/* clear force_uart_en */
USBPHY_CLR32(0x68, (0x1 << 26));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b00 */
USBPHY_CLR32(0x68, (0x3 << 30));
in_uart_mode = false;
usb_phy_poweron();
}
#endif
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
void usb_rev6_setting(int value)
{
static int direct_return;
if (direct_return)
return;
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
if (value)
USBPHY_SET32(0x18, (value << 24));
else
direct_return = 1;
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_poweron(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec for PHY3.3v/1.8v stable. */
udelay(50);
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* rg_usb20_dp_100k_mode 1'b1 0x20 18
* USB20_DP_100K_EN 1'b0 0x20 16
* RG_USB20_DM_100K_EN 1'b0 0x20 17
* RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
* force_suspendm 1'b0 0x68 18
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 9) | (0x1 << 8)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* rg_usb20_dp_100k_mode, 1'b1 */
USBPHY_SET32(0x20, (0x1 << 18));
/* USB20_DP_100K_EN 1'b0, RG_USB20_DM_100K_EN, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 16) | (0x1 << 17)));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
USBPHY_SET32(0x18, (0x40 << 24));
/* wait for 800 usec. */
udelay(800);
DBG(0, "usb power on success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
static void usb_phy_savecurrent_internal(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* RG_USB20_OTG_VBUSCMP_EN 1'b0 0x18 20
* RG_SUSPENDM 1'b1 0x68 03
* force_suspendm 1'b1 0x68 18
* RG_DPPULLDOWN 1'b1 0x68 06
* RG_DMPULLDOWN 1'b1 0x68 07
* RG_XCVRSEL[1:0] 2'b01 0x68 [04-05]
* RG_TERMSEL 1'b1 0x68 02
* RG_DATAIN[3:0] 4'b0000 0x68 [10-13]
* force_dp_pulldown 1'b1 0x68 20
* force_dm_pulldown 1'b1 0x68 21
* force_xcversel 1'b1 0x68 19
* force_termsel 1'b1 0x68 17
* force_datain 1'b1 0x68 23
* RG_SUSPENDM 1'b0 0x68 03
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 20));
/* RG_SUSPENDM, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force_suspendm, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b01. */
USBPHY_CLR32(0x68, (0x3 << 4));
USBPHY_SET32(0x68, (0x1 << 4));
/* RG_TERMSEL, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b1, force_dm_pulldown, 1'b1,
* force_xcversel, 1'b1, force_termsel, 1'b1, force_datain, 1'b1
*/
USBPHY_SET32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
udelay(800);
/* RG_SUSPENDM, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 3));
udelay(1);
set_usb_phy_mode(PHY_MODE_INVALID);
}
void usb_phy_savecurrent(void)
{
usb_phy_savecurrent_internal();
DBG(0, "usb save current success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_recover(void)
{
unsigned int efuse_val = 0;
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec. */
udelay(50);
/*
* 04.force_uart_en 1'b0 0x68 26
* 04.RG_UART_EN 1'b0 0x6C 16
* 04.rg_usb20_gpio_ctl 1'b0 0x20 09
* 04.usb20_gpio_mode 1'b0 0x20 08
* 05.force_suspendm 1'b0 0x68 18
* 06.RG_DPPULLDOWN 1'b0 0x68 06
* 07.RG_DMPULLDOWN 1'b0 0x68 07
* 08.RG_XCVRSEL[1:0] 2'b00 0x68 [04:05]
* 09.RG_TERMSEL 1'b0 0x68 02
* 10.RG_DATAIN[3:0] 4'b0000 0x68 [10:13]
* 11.force_dp_pulldown 1'b0 0x68 20
* 12.force_dm_pulldown 1'b0 0x68 21
* 13.force_xcversel 1'b0 0x68 19
* 14.force_termsel 1'b0 0x68 17
* 15.force_datain 1'b0 0x68 23
* 16.RG_USB20_BC11_SW_EN 1'b0 0x18 23
* 17.RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
*/
/* clean PUPD_BIST_EN */
/* PUPD_BIST_EN = 1'b0 */
/* PMIC will use it to detect charger type */
/* NEED?? USBPHY_CLR8(0x1d, 0x10);*/
USBPHY_CLR32(0x1c, (0x1 << 12));
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6C, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b00. */
USBPHY_CLR32(0x68, (0x3 << 4));
/* RG_TERMSEL, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b0, force_dm_pulldown, 1'b0,
* force_xcversel, 1'b0, force_termsel, 1'b0, force_datain, 1'b0
*/
USBPHY_CLR32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
usb_rev6_setting(0x40);
/* wait 800 usec. */
udelay(800);
/* force enter device mode */
set_usb_phy_mode(PHY_DEV_ACTIVE);
hs_slew_rate_cal();
/* M_ANALOG8[4:0] => RG_USB20_INTR_CAL[4:0] */
efuse_val = (get_devinfo_with_index(107) & (0x1f<<0)) >> 0;
if (efuse_val) {
DBG(0, "apply efuse setting, RG_USB20_INTR_CAL=0x%x\n",
efuse_val);
USBPHY_CLR32(0x04, (0x1F<<19));
USBPHY_SET32(0x04, (efuse_val<<19));
}
/* RG_USB20_DISCTH[7:4], 4'b0111 for 700 mV */
USBPHY_CLR32(0x18, (0xf0<<0));
USBPHY_SET32(0x18, (0x70<<0));
/* HQA special request */
{
USBPHY_CLR32(OFFSET_RG_USB20_VRT_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_VRT_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_VRT_VREF_SEL,
5 << SHFT_RG_USB20_VRT_VREF_SEL);
USBPHY_CLR32(OFFSET_RG_USB20_TERM_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_TERM_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_TERM_VREF_SEL,
5 << SHFT_RG_USB20_TERM_VREF_SEL);
/* discth = 7, u2_enhance = 1 already in */
}
usb_phy_tuning();
DBG(0, "usb recovery success\n");
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
if ((get_boot_mode() == META_BOOT) ||
(get_boot_mode() == ADVMETA_BOOT) ||
!mtk_musb) {
DBG(0, "%s Skip, musb<%p>\n",
__func__, mtk_musb);
return;
}
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
/* RG_USB20_BC11_SW_EN = 1'b1 */
USBPHY_SET32(0x18, (0x1 << 23));
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Init);
void Charger_Detect_Release(void)
{
if ((get_boot_mode() == META_BOOT) ||
(get_boot_mode() == ADVMETA_BOOT) ||
!mtk_musb) {
DBG(0, "%s Skip, musb<%p>\n",
__func__, mtk_musb);
return;
}
usb_prepare_enable_clock(true);
/* RG_USB20_BC11_SW_EN = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
udelay(1);
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Release);
void usb_phy_context_save(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
in_uart_mode = usb_phy_check_in_uart_mode();
#endif
}
void usb_phy_context_restore(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode)
usb_phy_switch_to_uart();
#endif
}
#endif
@@ -0,0 +1,725 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <musb_core.h>
#include <musb_debug.h>
#include <mtk_musb.h>
/* general */
#define BIT_WIDTH_1 1
#define MSK_WIDTH_1 0x1
#define VAL_MAX_WDITH_1 0x1
#define VAL_0_WIDTH_1 0x0
#define VAL_1_WIDTH_1 0x1
#define STRNG_0_WIDTH_1 "0"
#define STRNG_1_WIDTH_1 "1"
#define BIT_WIDTH_2 2
#define MSK_WIDTH_2 0x3
#define VAL_MAX_WDITH_2 0x3
#define VAL_0_WIDTH_2 0x0
#define VAL_1_WIDTH_2 0x1
#define VAL_2_WIDTH_2 0x2
#define VAL_3_WIDTH_2 0x3
#define STRNG_0_WIDTH_2 "00"
#define STRNG_1_WIDTH_2 "01"
#define STRNG_2_WIDTH_2 "10"
#define STRNG_3_WIDTH_2 "11"
#define BIT_WIDTH_3 3
#define MSK_WIDTH_3 0x7
#define VAL_MAX_WDITH_3 0x7
#define VAL_0_WIDTH_3 0x0
#define VAL_1_WIDTH_3 0x1
#define VAL_2_WIDTH_3 0x2
#define VAL_3_WIDTH_3 0x3
#define VAL_4_WIDTH_3 0x4
#define VAL_5_WIDTH_3 0x5
#define VAL_6_WIDTH_3 0x6
#define VAL_7_WIDTH_3 0x7
#define STRNG_0_WIDTH_3 "000"
#define STRNG_1_WIDTH_3 "001"
#define STRNG_2_WIDTH_3 "010"
#define STRNG_3_WIDTH_3 "011"
#define STRNG_4_WIDTH_3 "100"
#define STRNG_5_WIDTH_3 "101"
#define STRNG_6_WIDTH_3 "110"
#define STRNG_7_WIDTH_3 "111"
/* specific */
#define FILE_USB_DRIVING_CAPABILITY "USB_DRIVING_CAPABILITY"
#define FILE_RG_USB20_TERM_VREF_SEL "RG_USB20_TERM_VREF_SEL"
#define MSK_RG_USB20_TERM_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define FILE_RG_USB20_HSTX_SRCTRL "RG_USB20_HSTX_SRCTRL"
#define MSK_RG_USB20_HSTX_SRCTRL MSK_WIDTH_3
#define SHFT_RG_USB20_HSTX_SRCTRL 12
#define OFFSET_RG_USB20_HSTX_SRCTRL 0x14
#define FILE_RG_USB20_VRT_VREF_SEL "RG_USB20_VRT_VREF_SEL"
#define MSK_RG_USB20_VRT_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define FILE_RG_USB20_INTR_EN "RG_USB20_INTR_EN"
#define MSK_RG_USB20_INTR_EN MSK_WIDTH_1
#define SHFT_RG_USB20_INTR_EN 5
#define OFFSET_RG_USB20_INTR_EN 0x0
#define FILE_RG_USB20_PHY_REV6 "RG_USB20_PHY_REV6"
#define MSK_RG_USB20_PHY_REV6 MSK_WIDTH_2
#define SHFT_RG_USB20_PHY_REV6 30
#define OFFSET_RG_USB20_PHY_REV6 0x18
static struct proc_dir_entry *usb20_phy_procfs_root;
void usb20_phy_debugfs_write_width1(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_1);
clr_val = VAL_1_WIDTH_1;
}
if (!strncmp(buf, STRNG_1_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_1);
set_val = VAL_1_WIDTH_1;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_1 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n"
, __func__, __LINE__);
}
}
void usb20_phy_debugfs_rev6_write(u8 offset, u8 shift, char *buf)
{
u8 set_val = 0xFF;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_2);
set_val = VAL_0_WIDTH_2;
}
if (!strncmp(buf, STRNG_1_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_2);
set_val = VAL_1_WIDTH_2;
}
if (!strncmp(buf, STRNG_2_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_2);
set_val = VAL_2_WIDTH_2;
}
if (!strncmp(buf, STRNG_3_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_2);
set_val = VAL_3_WIDTH_2;
}
if (set_val <= VAL_MAX_WDITH_2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> case offset:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, set_val);
USBPHY_CLR32(offset, (VAL_MAX_WDITH_2<<shift));
USBPHY_SET32(offset, (set_val<<shift));
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
void usb20_phy_debugfs_write_width3(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
clr_val = VAL_7_WIDTH_3;
}
if (!strncmp(buf, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
set_val = VAL_1_WIDTH_3;
}
if (!strncmp(buf, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
set_val = VAL_2_WIDTH_3;
}
if (!strncmp(buf, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
set_val = VAL_3_WIDTH_3;
}
if (!strncmp(buf, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
set_val = VAL_4_WIDTH_3;
}
if (!strncmp(buf, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
set_val = VAL_5_WIDTH_3;
}
if (!strncmp(buf, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
set_val = VAL_6_WIDTH_3;
}
if (!strncmp(buf, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
set_val = VAL_7_WIDTH_3;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_3 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__, offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
u8 usb20_phy_debugfs_read_val(u8 offset, u8 shft, u8 msk, u8 width, char *str)
{
u32 val;
int i, temp;
val = USBPHY_READ32(offset);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val >> shft;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val & msk;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
temp = val;
str[width] = '\0';
for (i = (width - 1); i >= 0; i--) {
if (val % 2)
str[i] = '1';
else
str[i] = '0';
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str[%d]:%c\n\n",
__func__, __LINE__, i, str[i]);
val /= 2;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str(%s)\n",
__func__, __LINE__, str);
return val;
}
static int usb_driving_capability_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
u8 combined_val, tmp_val = 0xff;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL, BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
combined_val = tmp_val;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL,
BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
if ((tmp_val == (combined_val - 1)) || (tmp_val == combined_val))
combined_val += tmp_val;
else
combined_val = tmp_val * (VAL_MAX_WDITH_3 + 1) + combined_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
seq_printf(s, "\n%s = %d\n", FILE_USB_DRIVING_CAPABILITY, combined_val);
return 0;
}
static int rg_usb20_term_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL,
BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_TERM_VREF_SEL, str);
return 0;
}
static int rg_usb20_hstx_srctrl_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL,
MSK_RG_USB20_HSTX_SRCTRL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_HSTX_SRCTRL, str);
return 0;
}
static int rg_usb20_vrt_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_VRT_VREF_SEL, str);
return 0;
}
static int rg_usb20_intr_en_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN,
MSK_RG_USB20_INTR_EN, BIT_WIDTH_1, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_INTR_EN, str);
return 0;
}
static int rg_usb20_rev6_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6,
MSK_RG_USB20_PHY_REV6, BIT_WIDTH_2, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_PHY_REV6, str);
return 0;
}
static int usb_driving_capability_open(struct inode *inode, struct file *file)
{
return single_open(file, usb_driving_capability_show, PDE_DATA(inode));
}
static int rg_usb20_term_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_term_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_hstx_srctrl_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_hstx_srctrl_show, PDE_DATA(inode));
}
static int rg_usb20_vrt_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_vrt_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_intr_en_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_intr_en_show, PDE_DATA(inode));
}
static int rg_usb20_rev6_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_rev6_show, PDE_DATA(inode));
}
void val_to_bstring_width3(u8 val, char *str)
{
if (val == VAL_0_WIDTH_3)
memcpy(str, STRNG_0_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_1_WIDTH_3)
memcpy(str, STRNG_1_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_2_WIDTH_3)
memcpy(str, STRNG_2_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_3_WIDTH_3)
memcpy(str, STRNG_3_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_4_WIDTH_3)
memcpy(str, STRNG_4_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_5_WIDTH_3)
memcpy(str, STRNG_5_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_6_WIDTH_3)
memcpy(str, STRNG_6_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_7_WIDTH_3)
memcpy(str, STRNG_7_WIDTH_3, BIT_WIDTH_3 + 1);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), str(%s)\n",
__func__, __LINE__, val, str);
}
static ssize_t usb_driving_capability_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
u8 val, tmp_val;
char str_rg_usb20_term_vref_sel[18], str_rg_usb20_vrt_vref_sel[18];
memset(buf, 0x00, sizeof(buf));
pr_notice("\n");
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtol(buf, 10, (long *)&val) != 0) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtol, err(%d)\n)\n",
__func__, __LINE__, kstrtol(buf, 10, (long *)&val));
return count;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtol, val(%d)\n)\n",
__func__, __LINE__, val);
if (val > VAL_7_WIDTH_3 * 2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> wrong val set(%d), direct return\n",
__func__, __LINE__, val);
return count;
}
tmp_val = val;
val /= 2;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), tmp_val(%d)\n",
__func__, __LINE__, val, tmp_val);
val_to_bstring_width3(tmp_val - val, str_rg_usb20_term_vref_sel);
val_to_bstring_width3(val, str_rg_usb20_vrt_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config TERM_VREF_SEL %s\n",
__func__, __LINE__, str_rg_usb20_term_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
str_rg_usb20_term_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config VRT_VREF_SEL %s\n\n",
__func__, __LINE__, str_rg_usb20_vrt_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
str_rg_usb20_vrt_vref_sel);
return count;
}
static ssize_t rg_usb20_term_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_hstx_srctrl_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL, buf);
return count;
}
static ssize_t rg_usb20_vrt_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_intr_en_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width1(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN, buf);
return count;
}
static ssize_t rg_usb20_rev6_write(struct file *file,
const char __user *ubuf, size_t count,
loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_rev6_write(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6, buf);
return count;
}
static const struct file_operations usb_driving_capability_fops = {
.open = usb_driving_capability_open,
.write = usb_driving_capability_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_term_vref_sel_fops = {
.open = rg_usb20_term_vref_sel_open,
.write = rg_usb20_term_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_hstx_srctrl_fops = {
.open = rg_usb20_hstx_srctrl_open,
.write = rg_usb20_hstx_srctrl_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_vrt_vref_sel_fops = {
.open = rg_usb20_vrt_vref_sel_open,
.write = rg_usb20_vrt_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_intr_en_fops = {
.open = rg_usb20_intr_en_open,
.write = rg_usb20_intr_en_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_rev6_fops = {
.open = rg_usb20_rev6_open,
.write = rg_usb20_rev6_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
int usb20_phy_init_debugfs(void)
{
struct proc_dir_entry *root;
struct proc_dir_entry *file;
int ret;
proc_mkdir("mtk_usb", NULL);
root = proc_mkdir("mtk_usb/usb20_phy", NULL);
if (!root) {
ret = -ENOMEM;
goto err0;
}
file = proc_create(FILE_USB_DRIVING_CAPABILITY, 0644,
root, &usb_driving_capability_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_TERM_VREF_SEL, 0644,
root, &rg_usb20_term_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_HSTX_SRCTRL, 0644,
root, &rg_usb20_hstx_srctrl_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_VRT_VREF_SEL, 0644,
root, &rg_usb20_vrt_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_INTR_EN, 0644,
root, &rg_usb20_intr_en_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_PHY_REV6, 0644,
root, &rg_usb20_rev6_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
usb20_phy_procfs_root = root;
return 0;
err1:
proc_remove(root);
err0:
return ret;
}
void /* __init_or_exit */ usb20_phy_exit_debugfs(struct musb *musb)
{
proc_remove(usb20_phy_procfs_root);
}
@@ -0,0 +1,36 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
#
# for USB OTG silicon based on Mentor Graphics INVENTRA designs
#
ccflags-$(CONFIG_USB_MTK_HDRC) += -I$(srctree)/drivers/misc/mediatek/usb20
# for battery related
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
# for SPM control usage
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
# for TYPEC connection management
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/inc
ifeq ($(CONFIG_TCPC_CLASS),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/tcpc/inc
endif
# for ep0 test
ccflags-y += -I$(srctree)/drivers/usb/core/
# Phy
obj-$(CONFIG_MTK_MUSB_PHY) += mtk_usb20_phy.o
mtk_usb20_phy-y += usb20_phy.o
mtk_usb20_phy-$(CONFIG_DEBUG_FS) += usb20_phy_debugfs.o
File diff suppressed because it is too large. Load diff
+137
View File
@@ -0,0 +1,137 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/scatterlist.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include "musb_core.h"
#ifdef CONFIG_MTK_MUSB_CARPLAY_SUPPORT
struct carplay_dev {
struct usb_interface *intf;
struct usb_device *dev;
};
struct carplay_dev *apple_dev;
bool apple;
int send_switch_cmd(void)
{
int retval;
if (apple_dev == NULL) {
DBG(0, "no apple device attach.\n");
return -1;
}
DBG(0, "before usb_control_msg\n");
retval = usb_control_msg(apple_dev->dev,
usb_rcvctrlpipe(apple_dev->dev, 0),
0x51, 0x40, 1, 0, NULL, 0,
USB_CTRL_GET_TIMEOUT);
DBG(0, "after usb_control_msg retval = %d\n", retval);
if (retval != 0) {
DBG(0, "%s fail retval = %d\n", __func__, retval);
return -1;
}
return 0;
}
static int carplay_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *udev;
struct carplay_dev *car_dev;
DBG(0, "++ carplay probe ++\n");
udev = interface_to_usbdev(intf);
car_dev = kzalloc(sizeof(*car_dev), GFP_KERNEL);
if (!car_dev)
return -ENOMEM;
usb_set_intfdata(intf, car_dev);
car_dev->dev = udev;
car_dev->intf = intf;
apple_dev = car_dev;
apple = true;
if (car_dev->dev == NULL)
DBG(0, "car_dev->dev error\n");
return 0;
}
static void carplay_disconnect(struct usb_interface *intf)
{
struct carplay_dev *car_dev = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
dev_dbg(&intf->dev, "disconnect\n");
car_dev->dev = NULL;
car_dev->intf = NULL;
kfree(car_dev);
apple_dev = NULL;
apple = false;
DBG(0, "%s.\n", __func__);
}
static const struct usb_device_id id_table[] = {
/*-------------------------------------------------------------*/
/* EZ-USB devices which download firmware to replace (or in our
* case augment) the default device implementation.
*/
/* generic EZ-USB FX2 controller (or development board) */
{USB_DEVICE(0x05ac, 0x12a8),
},
/*-------------------------------------------------------------*/
{}
};
/* MODULE_DEVICE_TABLE(usb, id_table); */
static struct usb_driver carplay_driver = {
.name = "carplay",
.id_table = id_table,
.probe = carplay_probe,
.disconnect = carplay_disconnect,
};
/*-------------------------------------------------------------------------*/
static int __init carplay_init(void)
{
DBG(0, "%s register carplay_driver\n", __func__);
return usb_register(&carplay_driver);
}
module_init(carplay_init);
static void __exit carplay_exit(void)
{
usb_deregister(&carplay_driver);
}
module_exit(carplay_exit);
MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
MODULE_LICENSE("GPL");
#endif
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+146
View File
@@ -0,0 +1,146 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifndef __USB20_H__
#define __USB20_H__
#ifdef CONFIG_FPGA_EARLY_PORTING
#define FPGA_PLATFORM
#endif
#include <linux/interrupt.h>
#include <musb.h>
struct mt_usb_work {
struct delayed_work dwork;
int ops;
};
/* ToDo: should be moved to glue */
extern struct musb *mtk_musb;
extern struct musb *musb;
struct mt_usb_glue {
struct device *dev;
struct platform_device *musb_pdev;
struct musb *mtk_musb;
/* common power & clock */
struct clk *musb_clk;
struct clk *musb_clk_top_sel;
struct clk *musb_clk_univpll5_d2;
#ifdef CONFIG_PHY_MTK_TPHY
struct platform_device *usb_phy;
struct phy *phy;
struct usb_phy *xceiv;
enum phy_mode phy_mode;
#endif
#ifdef CONFIG_MTK_MUSB_DUAL_ROLE
struct otg_switch_mtk otg_sx;
#endif
};
extern struct mt_usb_glue *glue;
#define glue_to_musb(g) platform_get_drvdata(g->musb)
extern int kernel_init_done;
extern unsigned int upmu_get_rgs_chrdet(void);
extern bool upmu_is_chr_det(void);
extern enum charger_type mt_charger_type_detection(void);
extern void BATTERY_SetUSBState(int usb_state);
extern void upmu_interrupt_chrdet_int_en(unsigned int val);
/* specific USB fuctnion */
enum CABLE_MODE {
CABLE_MODE_CHRG_ONLY = 0,
CABLE_MODE_NORMAL,
CABLE_MODE_HOST_ONLY,
CABLE_MODE_MAX
};
enum USB_CLK_STATE {
NO_CHANGE = 0,
ON_TO_OFF,
OFF_TO_ON,
};
/* specific USB operation */
enum CONNECTION_OPS {
CONNECTION_OPS_DISC = 0,
CONNECTION_OPS_CHECK,
CONNECTION_OPS_CONN
};
enum VBUS_OPS {
VBUS_OPS_OFF = 0,
VBUS_OPS_ON
};
enum MTK_USB_SMC_CALL {
MTK_USB_SMC_INFRA_REQUEST = 0,
MTK_USB_SMC_INFRA_RELEASE,
MTK_USB_SMC_NUM
};
#ifdef CONFIG_MTK_UART_USB_SWITCH
enum PORT_MODE {
PORT_MODE_USB = 0,
PORT_MODE_UART,
PORT_MODE_MAX
};
extern bool usb_phy_check_in_uart_mode(void);
extern void usb_phy_switch_to_usb(void);
extern void usb_phy_switch_to_uart(void);
#endif
#ifdef FPGA_PLATFORM
extern void USB_PHY_Write_Register8(u8 var, u8 addr);
extern u8 USB_PHY_Read_Register8(u8 addr);
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
#define RG_GPIO_SELECT (0x600)
#define GPIO_SEL_OFFSET (4)
#define GPIO_SEL_MASK (0x7 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART0 (0x1 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART1 (0x2 << GPIO_SEL_OFFSET)
#define GET_GPIO_SEL_VAL(x) ((x & GPIO_SEL_MASK) >> GPIO_SEL_OFFSET)
extern void __iomem *ap_gpio_base;
extern bool in_uart_mode;
#endif
extern int usb20_phy_init_debugfs(void);
extern enum charger_type mt_get_charger_type(void);
#ifndef CONFIG_FPGA_EARLY_PORTING
#include <upmu_common.h>
#endif
#define PHY_IDLE_MODE 0
#define PHY_DEV_ACTIVE 1
#define PHY_HOST_ACTIVE 2
void set_usb_phy_mode(int mode);
#ifdef CONFIG_USB_MTK_OTG
extern void mt_usb_otg_init(struct musb *musb);
extern void mt_usb_otg_exit(struct musb *musb);
extern int mt_usb_get_vbus_status(struct musb *musb);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
#endif
extern void musb_platform_reset(struct musb *musb);
extern bool usb_enable_clock(bool enable);
extern bool usb_prepare_clock(bool enable);
extern void usb_prepare_enable_clock(bool enable);
/* usb host mode wakeup */
#define USB_WAKEUP_DEC_CON1 0x404
#define USB1_CDEN BIT(0)
#define USB1_CDDEBOUNCE(x) (((x) & 0xf) << 1)
#endif
@@ -0,0 +1,777 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <usb20.h>
#include <musb_io.h>
#include <mtk_musb_reg.h>
#include <musb_core.h>
#ifdef CONFIG_USB_MTK_OTG
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <mtk_musb.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#ifdef CONFIG_MTK_USB_TYPEC
#ifdef CONFIG_TCPC_CLASS
#include <tcpm.h>
#endif
#endif
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/phy/phy.h>
#ifdef CONFIG_MTK_MUSB_PHY
#include <usb20_phy.h>
#endif
MODULE_LICENSE("GPL v2");
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
#include <mt-plat/v1/charger_class.h>
static struct charger_device *primary_charger;
#endif
#endif
#include <mt-plat/mtk_boot_common.h>
struct device_node *usb_node;
static int iddig_eint_num;
static ktime_t ktime_start, ktime_end;
static struct musb_fifo_cfg fifo_cfg_host[] = {
{ .hw_ep_num = 1, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 1, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_RX,
.maxpacket = 64, .mode = BUF_SINGLE},
};
u32 delay_time = 15;
module_param(delay_time, int, 0644);
u32 delay_time1 = 55;
module_param(delay_time1, int, 0644);
u32 iddig_cnt;
module_param(iddig_cnt, int, 0644);
static bool vbus_on;
module_param(vbus_on, bool, 0644);
static int vbus_control;
module_param(vbus_control, int, 0644);
#ifdef CONFIG_MTK_MUSB_PHY
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
#endif
static void _set_vbus(int is_on)
{
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
if (!primary_charger) {
DBG(0, "vbus_init<%d>\n", vbus_on);
primary_charger = get_charger_by_name("primary_chg");
if (!primary_charger) {
DBG(0, "get primary charger device failed\n");
return;
}
}
#endif
#endif
DBG(0, "op<%d>, status<%d>\n", is_on, vbus_on);
if (is_on && !vbus_on) {
/* update flag 1st then enable VBUS to make
* host mode correct used by PMIC
*/
vbus_on = true;
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
charger_dev_enable_otg(primary_charger, true);
charger_dev_set_boost_current_limit(primary_charger, 1500000);
#else
set_chr_enable_otg(0x1);
set_chr_boost_current_limit(1500);
#endif
#endif
} else if (!is_on && vbus_on) {
/* disable VBUS 1st then update flag
* to make host mode correct used by PMIC
*/
vbus_on = false;
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
charger_dev_enable_otg(primary_charger, false);
#else
set_chr_enable_otg(0x0);
#endif
#endif
}
}
int mt_usb_get_vbus_status(struct musb *musb)
{
#if 1
return true;
#else
int ret = 0;
if ((musb_readb(musb->mregs, MUSB_DEVCTL) &
MUSB_DEVCTL_VBUS) != MUSB_DEVCTL_VBUS)
ret = 1;
else
DBG(0, "VBUS error, devctl=%x, power=%d\n",
musb_readb(musb->mregs, MUSB_DEVCTL),
musb->power);
pr_debug("vbus ready = %d\n", ret);
return ret;
#endif
}
#if defined(CONFIG_USBIF_COMPLIANCE)
u32 sw_deboun_time = 1;
#else
u32 sw_deboun_time = 400;
#endif
module_param(sw_deboun_time, int, 0644);
u32 typec_control;
module_param(typec_control, int, 0644);
static bool typec_req_host;
static bool iddig_req_host;
static void do_host_work(struct work_struct *data);
static void issue_host_work(int ops, int delay, bool on_st)
{
struct mt_usb_work *work;
if (!mtk_musb) {
DBG(0, "mtk_musb = NULL\n");
return;
}
/* create and prepare worker */
work = kzalloc(sizeof(struct mt_usb_work), GFP_ATOMIC);
if (!work) {
DBG(0, "work is NULL, directly return\n");
return;
}
work->ops = ops;
INIT_DELAYED_WORK(&work->dwork, do_host_work);
/* issue connection work */
DBG(0, "issue work, ops<%d>, delay<%d>, on_st<%d>\n",
ops, delay, on_st);
if (on_st)
queue_delayed_work(mtk_musb->st_wq,
&work->dwork, msecs_to_jiffies(delay));
else
schedule_delayed_work(&work->dwork,
msecs_to_jiffies(delay));
}
void mt_usb_host_connect(int delay)
{
typec_req_host = true;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_CONN, delay, true);
}
void mt_usb_host_disconnect(int delay)
{
typec_req_host = false;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_DISC, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_disconnect);
static bool musb_is_host(void)
{
bool host_mode = 0;
if (typec_control)
host_mode = typec_req_host;
else
host_mode = iddig_req_host;
return host_mode;
}
void musb_session_restart(struct musb *musb)
{
void __iomem *mbase = musb->mregs;
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) & (~MUSB_DEVCTL_SESSION)));
#ifdef CONFIG_MTK_MUSB_PHY
DBG(0, "[MUSB] stopped session for VBUSERROR interrupt\n");
USBPHY_SET32(0x6c, (0x3c<<8));
USBPHY_SET32(0x6c, (0x10<<0));
USBPHY_CLR32(0x6c, (0x2c<<0));
DBG(0, "[MUSB] force PHY to idle, 0x6c=%x\n", USBPHY_READ32(0x6c));
mdelay(5);
USBPHY_CLR32(0x6c, (0x3c<<8));
USBPHY_CLR32(0x6c, (0x3c<<0));
DBG(0, "[MUSB] let PHY resample VBUS, 0x6c=%x\n"
, USBPHY_READ32(0x6c));
#endif
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) | MUSB_DEVCTL_SESSION));
DBG(0, "[MUSB] restart session\n");
}
EXPORT_SYMBOL(musb_session_restart);
static struct delayed_work host_plug_test_work;
int host_plug_test_enable; /* default disable */
module_param(host_plug_test_enable, int, 0644);
int host_plug_in_test_period_ms = 5000;
module_param(host_plug_in_test_period_ms, int, 0644);
int host_plug_out_test_period_ms = 5000;
module_param(host_plug_out_test_period_ms, int, 0644);
int host_test_vbus_off_time_us = 3000;
module_param(host_test_vbus_off_time_us, int, 0644);
int host_test_vbus_only = 1;
module_param(host_test_vbus_only, int, 0644);
static int host_plug_test_triggered;
void switch_int_to_device(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_HIGH);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
void switch_int_to_host(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_LOW);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
static void do_host_plug_test_work(struct work_struct *data)
{
static ktime_t ktime_begin, ktime_end;
static s64 diff_time;
static int host_on;
static struct wakeup_source *host_test_wakelock;
static int wake_lock_inited;
if (!wake_lock_inited) {
DBG(0, "wake_lock_init\n");
host_test_wakelock = wakeup_source_register(NULL,
"host.test.lock");
wake_lock_inited = 1;
}
host_plug_test_triggered = 1;
/* sync global status */
mb();
__pm_stay_awake(host_test_wakelock);
DBG(0, "BEGIN");
ktime_begin = ktime_get();
host_on = 1;
while (1) {
if (!musb_is_host() && host_on) {
DBG(0, "about to exit");
break;
}
msleep(50);
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_begin));
if (host_on && diff_time >= host_plug_in_test_period_ms) {
host_on = 0;
DBG(0, "OFF\n");
ktime_begin = ktime_get();
/* simulate plug out */
_set_vbus(0);
udelay(host_test_vbus_off_time_us);
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_DISC, 0, false);
} else if (!host_on && diff_time >=
host_plug_out_test_period_ms) {
host_on = 1;
DBG(0, "ON\n");
ktime_begin = ktime_get();
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_CONN, 0, false);
_set_vbus(1);
msleep(100);
}
}
/* wait host_work done */
msleep(1000);
host_plug_test_triggered = 0;
__pm_relax(host_test_wakelock);
DBG(0, "END\n");
}
#define ID_PIN_WORK_RECHECK_TIME 30 /* 30 ms */
#define ID_PIN_WORK_BLOCK_TIMEOUT 30000 /* 30000 ms */
static void do_host_work(struct work_struct *data)
{
u8 devctl = 0;
unsigned long flags;
static int inited, timeout; /* default to 0 */
static s64 diff_time;
bool host_on;
int usb_clk_state = NO_CHANGE;
struct mt_usb_work *work =
container_of(data, struct mt_usb_work, dwork.work);
struct mt_usb_glue *glue = mtk_musb->glue;
/*
* kernel_init_done should be set in
* early-init stage through init.$platform.usb.rc
*/
while (!inited && !kernel_init_done &&
!mtk_musb->is_ready && !timeout) {
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_start));
DBG_LIMIT(3,
"init_done:%d, is_ready:%d, inited:%d, TO:%d, diff:%lld",
kernel_init_done,
mtk_musb->is_ready,
inited,
timeout,
diff_time);
if (diff_time > ID_PIN_WORK_BLOCK_TIMEOUT) {
DBG(0, "diff_time:%lld\n", diff_time);
timeout = 1;
}
msleep(ID_PIN_WORK_RECHECK_TIME);
}
if (!inited) {
DBG(0, "PASS,init_done:%d,is_ready:%d,inited:%d, TO:%d\n",
kernel_init_done, mtk_musb->is_ready,
inited, timeout);
inited = 1;
}
/* always prepare clock and check if need to unprepater later */
/* clk_prepare_cnt +1 here */
usb_prepare_clock(true);
down(&mtk_musb->musb_lock);
host_on = (work->ops ==
CONNECTION_OPS_CONN ? true : false);
DBG(0, "work start, is_host=%d, host_on=%d\n",
mtk_musb->is_host, host_on);
if (host_on && !mtk_musb->is_host) {
/* switch to HOST state before turn on VBUS */
MUSB_HST_MODE(mtk_musb);
/* to make sure all event clear */
msleep(32);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (!usb_on_sram) {
int ret;
ret = gpd_switch_to_sram(mtk_musb->controller);
DBG(0, "gpd_switch_to_sram, ret<%d>\n", ret);
if (ret == 0)
usb_on_sram = 1;
}
#endif
/* setup fifo for host mode */
ep_config_from_table_for_host(mtk_musb);
if (!mtk_musb->host_suspend)
__pm_stay_awake(mtk_musb->usb_lock);
/* this make PHY operation workable */
musb_platform_enable(mtk_musb);
/* for no VBUS sensing IP*/
/* wait VBUS ready */
msleep(100);
/* clear session*/
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl&(~MUSB_DEVCTL_SESSION)));
phy_set_mode(glue->phy, PHY_MODE_INVALID);
/* wait */
mdelay(5);
/* restart session */
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl | MUSB_DEVCTL_SESSION));
phy_set_mode(glue->phy, PHY_MODE_USB_HOST);
musb_start(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_device(mtk_musb);
if (host_plug_test_enable && !host_plug_test_triggered)
queue_delayed_work(mtk_musb->st_wq,
&host_plug_test_work, 0);
usb_clk_state = OFF_TO_ON;
} else if (!host_on && mtk_musb->is_host) {
/* switch from host -> device */
/* for device no disconnect interrupt */
spin_lock_irqsave(&mtk_musb->lock, flags);
if (mtk_musb->is_active) {
DBG(0, "for not receiving disconnect interrupt\n");
usb_hcd_resume_root_hub(musb_to_hcd(mtk_musb));
musb_root_disconnect(mtk_musb);
}
spin_unlock_irqrestore(&mtk_musb->lock, flags);
DBG(1, "devctl is %x\n",
musb_readb(mtk_musb->mregs, MUSB_DEVCTL));
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, 0);
if (mtk_musb->usb_lock->active)
__pm_relax(mtk_musb->usb_lock);
/* for no VBUS sensing IP */
phy_set_mode(glue->phy, PHY_MODE_INVALID);
musb_stop(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_host(mtk_musb);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (usb_on_sram) {
gpd_switch_to_dram(mtk_musb->controller);
usb_on_sram = 0;
}
#endif
/* to make sure all event clear */
msleep(32);
mtk_musb->xceiv->otg->state = OTG_STATE_B_IDLE;
/* switch to DEV state after turn off VBUS */
MUSB_DEV_MODE(mtk_musb);
usb_clk_state = ON_TO_OFF;
}
DBG(0, "work end, is_host=%d\n", mtk_musb->is_host);
up(&mtk_musb->musb_lock);
if (usb_clk_state == ON_TO_OFF) {
/* clock on -> of: clk_prepare_cnt -2 */
usb_prepare_clock(false);
usb_prepare_clock(false);
} else if (usb_clk_state == NO_CHANGE) {
/* clock no change : clk_prepare_cnt -1 */
usb_prepare_clock(false);
}
/* free mt_usb_work */
kfree(work);
}
static irqreturn_t mt_usb_ext_iddig_int(int irq, void *dev_id)
{
iddig_cnt++;
iddig_req_host = !iddig_req_host;
DBG(0, "id pin assert, %s\n", iddig_req_host ?
"connect" : "disconnect");
if (iddig_req_host)
mt_usb_host_connect(0);
else
mt_usb_host_disconnect(0);
disable_irq_nosync(iddig_eint_num);
return IRQ_HANDLED;
}
static const struct of_device_id otg_iddig_of_match[] = {
{.compatible = "mediatek,usb_iddig_bi_eint"},
{},
};
static int otg_iddig_probe(struct platform_device *pdev)
{
int ret;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
iddig_eint_num = irq_of_parse_and_map(node, 0);
DBG(0, "iddig_eint_num<%d>\n", iddig_eint_num);
if (iddig_eint_num < 0)
return -ENODEV;
ret = request_irq(iddig_eint_num, mt_usb_ext_iddig_int,
IRQF_TRIGGER_LOW, "USB_IDDIG", NULL);
if (ret) {
DBG(0,
"request EINT <%d> fail, ret<%d>\n",
iddig_eint_num, ret);
return ret;
}
return 0;
}
static struct platform_driver otg_iddig_driver = {
.probe = otg_iddig_probe,
/* .remove = otg_iddig_remove, */
/* .shutdown = otg_iddig_shutdown, */
.driver = {
.name = "otg_iddig",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(otg_iddig_of_match),
},
};
static int iddig_int_init(void)
{
int ret = 0;
ret = platform_driver_register(&otg_iddig_driver);
if (ret)
DBG(0, "ret:%d\n", ret);
return 0;
}
void mt_usb_otg_init(struct musb *musb)
{
/* test */
INIT_DELAYED_WORK(&host_plug_test_work, do_host_plug_test_work);
ktime_start = ktime_get();
/* CONNECTION MANAGEMENT*/
#ifdef CONFIG_MTK_USB_TYPEC
DBG(0, "host controlled by TYPEC\n");
typec_control = 1;
#ifdef CONFIG_TCPC_CLASS
DBG(0, "host controlled by IDDIG\n");
iddig_int_init();
vbus_control = 1;
#endif /* CONFIG_TCPC_CLASS */
#endif /* CONFIG_MTK_USB_TYPEC */
/* EP table */
musb->fifo_cfg_host = fifo_cfg_host;
musb->fifo_cfg_host_size = ARRAY_SIZE(fifo_cfg_host);
}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb)
{
DBG(0, "OTG disable vbus\n");
}
EXPORT_SYMBOL(mt_usb_otg_exit);
enum {
DO_IT = 0,
REVERT,
};
#ifdef CONFIG_MTK_MUSB_PHY
static void bypass_disc_circuit(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 13-12 RG_USB20_HSRX_MMODE_SELE, dft:00 */
if (act == DO_IT) {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_SET32(0x18, (0x20<<8));
} else {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_CLR32(0x18, (0x20<<8));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
static void disc_threshold_to_max(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 7-4 RG_USB20_DISCTH, dft:1000 */
if (act == DO_IT) {
USBPHY_SET32(0x18, (0xf0<<0));
} else {
USBPHY_CLR32(0x18, (0x70<<0));
USBPHY_SET32(0x18, (0x80<<0));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
#endif
static int option;
static int set_option(const char *val, const struct kernel_param *kp)
{
int local_option;
int rv;
/* update module parameter */
rv = param_set_int(val, kp);
if (rv)
return rv;
/* update local_option */
rv = kstrtoint(val, 10, &local_option);
if (rv != 0)
return rv;
DBG(0, "option:%d, local_option:%d\n", option, local_option);
switch (local_option) {
case 0:
DBG(0, "case %d\n", local_option);
iddig_int_init();
break;
case 1:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(0);
break;
case 2:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(0);
break;
case 3:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(3000);
break;
case 4:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(3000);
break;
#ifdef CONFIG_MTK_MUSB_PHY
case 5:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(DO_IT);
break;
case 6:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(REVERT);
break;
case 7:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(DO_IT);
break;
case 8:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(REVERT);
break;
#endif
case 9:
DBG(0, "case %d\n", local_option);
_set_vbus(1);
break;
case 10:
DBG(0, "case %d\n", local_option);
_set_vbus(0);
break;
default:
break;
}
return 0;
}
static struct kernel_param_ops option_param_ops = {
.set = set_option,
.get = param_get_int,
};
module_param_cb(option, &option_param_ops, &option, 0644);
#else
#include "musb_core.h"
/* for not define CONFIG_USB_MTK_OTG */
void mt_usb_otg_init(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_exit);
void mt_usb_set_vbus(struct musb *musb, int is_on) {}
int mt_usb_get_vbus_status(struct musb *musb) {return 1; }
void switch_int_to_device(struct musb *musb) {}
void switch_int_to_host(struct musb *musb) {}
void musb_session_restart(struct musb *musb) {}
EXPORT_SYMBOL(musb_session_restart);
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,824 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#ifdef CONFIG_MTK_CLKMGR
#include <mach/mt_clkmgr.h>
#else
#include <linux/clk.h>
#endif
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <mtk_musb.h>
#include <musb_core.h>
#include "usb20.h"
#include "mtk_devinfo.h"
#include <linux/phy/phy.h>
#ifdef CONFIG_OF
#include <linux/of_address.h>
#endif
#ifdef CONFIG_MTK_AEE_FEATURE
#include <mt-plat/aee.h>
#endif
#include <mt-plat/mtk_boot_common.h>
#define FRA (48)
#define PARA (28)
#ifdef FPGA_PLATFORM
bool usb_enable_clock(bool enable)
{
return true;
}
bool usb_prepare_clock(bool enable)
{
return true;
}
void usb_prepare_enable_clock(bool enable)
{
}
void usb_phy_poweron(void)
{
}
void usb_phy_savecurrent(void)
{
}
void usb_phy_recover(void)
{
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
}
void Charger_Detect_Release(void)
{
}
void usb_phy_context_save(void)
{
}
void usb_phy_context_restore(void)
{
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
return false;
}
void usb_phy_switch_to_uart(void)
{
}
void usb_phy_switch_to_usb(void)
{
}
#endif
#else
#include <linux/of_irq.h>
#include <linux/of_address.h>
#define VAL_MAX_WIDTH_2 0x3
#define VAL_MAX_WIDTH_3 0x7
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_PHY_REV6 0x18
#define SHFT_RG_USB20_PHY_REV6 30
void usb_phy_tuning(void)
{
static bool inited;
static s32 u2_vrt_ref, u2_term_ref, u2_enhance;
struct device_node *of_node;
if (!inited) {
/* apply default value */
u2_vrt_ref = -1;
u2_term_ref = -1;
u2_enhance = -1;
of_node = of_find_compatible_node(NULL,
NULL, "mediatek,phy_tuning");
if (of_node) {
/* value won't be updated if property not being found */
of_property_read_u32(of_node,
"u2_vrt_ref", (u32 *) &u2_vrt_ref);
of_property_read_u32(of_node,
"u2_term_ref", (u32 *) &u2_term_ref);
of_property_read_u32(of_node,
"u2_enhance", (u32 *) &u2_enhance);
}
inited = true;
}
if (u2_vrt_ref != -1) {
if (u2_vrt_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_VRT_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_VRT_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_VRT_VREF_SEL,
u2_vrt_ref << SHFT_RG_USB20_VRT_VREF_SEL);
}
}
if (u2_term_ref != -1) {
if (u2_term_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_TERM_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_TERM_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_TERM_VREF_SEL,
u2_term_ref << SHFT_RG_USB20_TERM_VREF_SEL);
}
}
if (u2_enhance != -1) {
if (u2_enhance <= VAL_MAX_WIDTH_2) {
USBPHY_CLR32(OFFSET_RG_USB20_PHY_REV6,
VAL_MAX_WIDTH_2 << SHFT_RG_USB20_PHY_REV6);
USBPHY_SET32(OFFSET_RG_USB20_PHY_REV6,
u2_enhance<<SHFT_RG_USB20_PHY_REV6);
}
}
}
#ifdef CONFIG_MTK_USB2JTAG_SUPPORT
int usb2jtag_usb_init(void)
{
struct device_node *node = NULL;
void __iomem *usb_phy_base, usb_sif_base;
u32 temp;
node = of_find_compatible_node(NULL, NULL, "mediatek,mt6781-usb20");
if (!node) {
pr_err("[USB2JTAG] map node @ mediatek,USB0 failed\n");
return -1;
}
usb_phy_base = of_iomap(node, 1);
if (!usb_phy_base) {
pr_err("[USB2JTAG] iomap usb_phy_base failed\n");
return -1;
}
usb_sif_base = usb_phy_base + USB_PHY_OFFSET;
/* rg_usb20_gpio_ctl: bit[9] = 1 */
temp = readl(usb_sif_base + 0x20);
writel(temp | (1 << 9), usb_sif_base + 0x20);
/* RG_USB20_BGR_EN: bit[0] = 1 */
temp = readl(usb_sif_base);
writel(temp | (1 << 0), usb_sif_base);
/* RG_USB20_BC11_SW_EN: bit[23] = 0 */
temp = readl(usb_sif_base + 0x18);
writel(temp & ~(1 << 23), usb_sif_base + 0x18);
/* wait stable */
mdelay(1);
iounmap(usb_phy_base);
return 0;
}
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool in_uart_mode;
#endif
void usb_prepare_enable_clock(bool enable)
{
if (enable) {
usb_prepare_clock(true);
usb_enable_clock(true);
} else {
usb_enable_clock(false);
usb_prepare_clock(false);
}
}
DEFINE_MUTEX(prepare_lock);
static atomic_t clk_prepare_cnt = ATOMIC_INIT(0);
bool usb_prepare_clock(bool enable)
{
int before_cnt = atomic_read(&clk_prepare_cnt);
mutex_lock(&prepare_lock);
if (IS_ERR_OR_NULL(musb_clk) ||
IS_ERR_OR_NULL(musb_clk_top_sel) ||
IS_ERR_OR_NULL(musb_clk_univpll5_d2)) {
DBG(0, "clk not ready\n");
mutex_unlock(&prepare_lock);
return 0;
}
if (enable) {
if (clk_prepare(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel prepare fail\n");
} else {
if (clk_set_parent(musb_clk_top_sel,
musb_clk_univpll5_d2))
DBG(0, "musb_clk_top_sel set_parent fail\n");
}
if (clk_prepare(musb_clk))
DBG(0, "musb_clk prepare fail\n");
atomic_inc(&clk_prepare_cnt);
} else {
clk_unprepare(musb_clk_top_sel);
clk_unprepare(musb_clk);
atomic_dec(&clk_prepare_cnt);
}
mutex_unlock(&prepare_lock);
DBG(1, "enable(%d), usb prepare_cnt, before(%d), after(%d)\n",
enable, before_cnt, atomic_read(&clk_prepare_cnt));
#ifdef CONFIG_MTK_AEE_FEATURE
if (atomic_read(&clk_prepare_cnt) < 0)
aee_kernel_warning("usb20", "usb clock prepare_cnt error\n");
#endif
return 1;
}
static DEFINE_SPINLOCK(musb_reg_clock_lock);
bool usb_enable_clock(bool enable)
{
static int count;
static int real_enable = 0, real_disable;
static int virt_enable = 0, virt_disable;
unsigned long flags;
DBG(1, "enable(%d),count(%d),<%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
spin_lock_irqsave(&musb_reg_clock_lock, flags);
if (unlikely(atomic_read(&clk_prepare_cnt) <= 0)) {
DBG_LIMIT(1, "clock not prepare");
goto exit;
}
if (enable && count == 0) {
if (clk_enable(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel enable fail\n");
goto exit;
}
if (clk_enable(musb_clk)) {
DBG(0, "musb_clk enable fail\n");
clk_disable(musb_clk_top_sel);
goto exit;
}
usb_hal_dpidle_request(USB_DPIDLE_FORBIDDEN);
real_enable++;
} else if (!enable && count == 1) {
clk_disable(musb_clk);
clk_disable(musb_clk_top_sel);
usb_hal_dpidle_request(USB_DPIDLE_ALLOWED);
real_disable++;
}
if (enable)
count++;
else
count = (count == 0) ? 0 : (count - 1);
exit:
if (enable)
virt_enable++;
else
virt_disable++;
spin_unlock_irqrestore(&musb_reg_clock_lock, flags);
DBG(1, "enable(%d),count(%d), <%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
return 1;
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
u32 usb_port_mode;
usb_enable_clock(true);
udelay(50);
usb_port_mode = USBPHY_READ32(0x68);
usb_enable_clock(false);
if (((usb_port_mode >> 30) & 0x3) == 1) {
DBG(0, "%s:%d - IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = true;
} else {
DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = false;
}
return in_uart_mode;
}
void usb_phy_switch_to_uart(void)
{
unsigned int val = 0;
if (usb_phy_check_in_uart_mode()) {
DBG(0, "Already in UART mode.\n");
return;
}
udelay(50);
/* RG_USB20_BC11_SW_EN 0x11F4_0818[23] = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* Set RG_SUSPENDM 0x11F4_0868[3] to 1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force suspendm 0x11F4_0868[18] = 1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b01 */
USBPHY_CLR32(0x68, (0x3 << 30));
USBPHY_SET32(0x68, (0x1 << 30));
/* force_uart_i 0x11F4_0868[29] = 0*/
USBPHY_CLR32(0x68, (0x1 << 29));
/* force_uart_bias_en 0x11F4_0868[28] = 1 */
USBPHY_SET32(0x68, (0x1 << 28));
/* force_uart_tx_oe 0x11F4_0868[27] = 1 */
USBPHY_SET32(0x68, (0x1 << 27));
/* force_uart_en 0x11F4_0868[26] = 1 */
USBPHY_SET32(0x68, (0x1 << 26));
/* RG_UART_BIAS_EN 0x11F4_086c[18] = 1 */
USBPHY_SET32(0x6C, (0x1 << 18));
/* RG_UART_TX_OE 0x11F4_086c[17] = 1 */
USBPHY_SET32(0x6C, (0x1 << 17));
/* Set RG_UART_EN to 1 */
USBPHY_SET32(0x6C, (0x1 << 16));
/* Set RG_USB20_DM_100K_EN to 1 */
USBPHY_SET32(0x20, (0x1 << 17));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
val = readl(ap_gpio_base);
writel(val | (GPIO_SEL_UART0), ap_gpio_base);
in_uart_mode = true;
}
void usb_phy_switch_to_usb(void)
{
unsigned int val = 0;
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
/* clear force_uart_en */
USBPHY_CLR32(0x68, (0x1 << 26));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b00 */
USBPHY_CLR32(0x68, (0x3 << 30));
in_uart_mode = false;
usb_phy_poweron();
}
#endif
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
void usb_rev6_setting(int value)
{
static int direct_return;
if (direct_return)
return;
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
if (value)
USBPHY_SET32(0x18, (value << 24));
else
direct_return = 1;
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_poweron(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec for PHY3.3v/1.8v stable. */
udelay(50);
USBPHY_SET32(0x68, (0x1 << 18));
USBPHY_CLR32(0x68, (0x1 << 3));
USBPHY_SET32(0x68, (0x1 << 3));
udelay(30);
USBPHY_CLR32(0x68, (0x1 << 18));
USBPHY_CLR32(0x68, (0x1 << 3));
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* rg_usb20_dp_100k_mode 1'b1 0x20 18
* USB20_DP_100K_EN 1'b0 0x20 16
* RG_USB20_DM_100K_EN 1'b0 0x20 17
* RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
* force_suspendm 1'b0 0x68 18
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 9) | (0x1 << 8)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* rg_usb20_dp_100k_mode, 1'b1 */
USBPHY_SET32(0x20, (0x1 << 18));
/* USB20_DP_100K_EN 1'b0, RG_USB20_DM_100K_EN, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 16) | (0x1 << 17)));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
USBPHY_SET32(0x18, (0x40 << 24));
/* wait for 800 usec. */
udelay(800);
DBG(0, "usb power on success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
static void usb_phy_savecurrent_internal(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* RG_USB20_OTG_VBUSCMP_EN 1'b0 0x18 20
* RG_SUSPENDM 1'b1 0x68 03
* force_suspendm 1'b1 0x68 18
* RG_DPPULLDOWN 1'b1 0x68 06
* RG_DMPULLDOWN 1'b1 0x68 07
* RG_XCVRSEL[1:0] 2'b01 0x68 [04-05]
* RG_TERMSEL 1'b1 0x68 02
* RG_DATAIN[3:0] 4'b0000 0x68 [10-13]
* force_dp_pulldown 1'b1 0x68 20
* force_dm_pulldown 1'b1 0x68 21
* force_xcversel 1'b1 0x68 19
* force_termsel 1'b1 0x68 17
* force_datain 1'b1 0x68 23
* RG_SUSPENDM 1'b0 0x68 03
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 20));
/* RG_SUSPENDM, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force_suspendm, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b01. */
USBPHY_CLR32(0x68, (0x3 << 4));
USBPHY_SET32(0x68, (0x1 << 4));
/* RG_TERMSEL, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b1, force_dm_pulldown, 1'b1,
* force_xcversel, 1'b1, force_termsel, 1'b1, force_datain, 1'b1
*/
USBPHY_SET32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
udelay(800);
/* RG_SUSPENDM, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 3));
udelay(1);
set_usb_phy_mode(PHY_MODE_INVALID);
}
void usb_phy_savecurrent(void)
{
usb_phy_savecurrent_internal();
DBG(0, "usb save current success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_recover(void)
{
unsigned int efuse_val = 0;
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec. */
udelay(50);
USBPHY_SET32(0x68, (0x1 << 18));
USBPHY_CLR32(0x68, (0x1 << 3));
USBPHY_SET32(0x68, (0x1 << 3));
udelay(30);
USBPHY_CLR32(0x68, (0x1 << 18));
USBPHY_CLR32(0x68, (0x1 << 3));
/*
* 04.force_uart_en 1'b0 0x68 26
* 04.RG_UART_EN 1'b0 0x6C 16
* 04.rg_usb20_gpio_ctl 1'b0 0x20 09
* 04.usb20_gpio_mode 1'b0 0x20 08
* 05.force_suspendm 1'b0 0x68 18
* 06.RG_DPPULLDOWN 1'b0 0x68 06
* 07.RG_DMPULLDOWN 1'b0 0x68 07
* 08.RG_XCVRSEL[1:0] 2'b00 0x68 [04:05]
* 09.RG_TERMSEL 1'b0 0x68 02
* 10.RG_DATAIN[3:0] 4'b0000 0x68 [10:13]
* 11.force_dp_pulldown 1'b0 0x68 20
* 12.force_dm_pulldown 1'b0 0x68 21
* 13.force_xcversel 1'b0 0x68 19
* 14.force_termsel 1'b0 0x68 17
* 15.force_datain 1'b0 0x68 23
* 16.RG_USB20_BC11_SW_EN 1'b0 0x18 23
* 17.RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
*/
/* clean PUPD_BIST_EN */
/* PUPD_BIST_EN = 1'b0 */
/* PMIC will use it to detect charger type */
/* NEED?? USBPHY_CLR8(0x1d, 0x10);*/
USBPHY_CLR32(0x1c, (0x1 << 12));
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6C, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b00. */
USBPHY_CLR32(0x68, (0x3 << 4));
/* RG_TERMSEL, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b0, force_dm_pulldown, 1'b0,
* force_xcversel, 1'b0, force_termsel, 1'b0, force_datain, 1'b0
*/
USBPHY_CLR32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
usb_rev6_setting(0x40);
/* wait 800 usec. */
udelay(800);
/* force enter device mode */
set_usb_phy_mode(PHY_DEV_ACTIVE);
/* M_ANALOG8[4:0] => RG_USB20_INTR_CAL[4:0] */
efuse_val = (get_devinfo_with_index(108) & (0x1f<<0)) >> 0;
if (efuse_val) {
DBG(0, "apply efuse setting, RG_USB20_INTR_CAL=0x%x\n",
efuse_val);
USBPHY_CLR32(0x04, (0x1F<<19));
USBPHY_SET32(0x04, (efuse_val<<19));
}
/* RG_USB20_DISCTH[7:4], 4'b0111 for 700 mV */
USBPHY_CLR32(0x18, (0xf0<<0));
USBPHY_SET32(0x18, (0x70<<0));
/* HQA Request comes from SA */
USBPHY_CLR32(0x18, (0x1<<28));
USBPHY_CLR32(0x18, (0xf<<0));
USBPHY_SET32(0x18, (0x2<<0));
USBPHY_SET32(0x8, (0x1<<3));
usb_phy_tuning();
DBG(0, "usb recovery success\n");
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
if ((get_boot_mode() == META_BOOT) ||
(get_boot_mode() == ADVMETA_BOOT) ||
!mtk_musb) {
DBG(0, "%s Skip, musb<%p>\n",
__func__, mtk_musb);
return;
}
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
/* RG_USB20_BC11_SW_EN = 1'b1 */
USBPHY_SET32(0x18, (0x1 << 23));
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Init);
void Charger_Detect_Release(void)
{
if ((get_boot_mode() == META_BOOT) ||
(get_boot_mode() == ADVMETA_BOOT) ||
!mtk_musb) {
DBG(0, "%s Skip, musb<%p>\n",
__func__, mtk_musb);
return;
}
usb_prepare_enable_clock(true);
/* RG_USB20_BC11_SW_EN = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
udelay(1);
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Release);
void usb_phy_context_save(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
in_uart_mode = usb_phy_check_in_uart_mode();
#endif
}
void usb_phy_context_restore(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode)
usb_phy_switch_to_uart();
#endif
}
void usb_dpdm_pulldown(bool enable)
{
DBG(0, "%s: enable=%d\n", __func__, enable);
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
if (enable) {
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 6) | (0x1 << 7));
/* RG_USB20_PHY_REV */
USBPHY_CLR32(0x18, (0x2 << 24));
} else {
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 6) | (0x1 << 7));
/* RG_USB20_PHY_REV */
USBPHY_SET32(0x18, (0x2 << 24));
}
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
#endif
@@ -0,0 +1,918 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2016 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <musb_core.h>
#include <musb_debug.h>
#include <mtk_musb.h>
/* general */
#define BIT_WIDTH_1 1
#define MSK_WIDTH_1 0x1
#define VAL_MAX_WDITH_1 0x1
#define VAL_0_WIDTH_1 0x0
#define VAL_1_WIDTH_1 0x1
#define STRNG_0_WIDTH_1 "0"
#define STRNG_1_WIDTH_1 "1"
#define BIT_WIDTH_2 2
#define MSK_WIDTH_2 0x3
#define VAL_MAX_WDITH_2 0x3
#define VAL_0_WIDTH_2 0x0
#define VAL_1_WIDTH_2 0x1
#define VAL_2_WIDTH_2 0x2
#define VAL_3_WIDTH_2 0x3
#define STRNG_0_WIDTH_2 "00"
#define STRNG_1_WIDTH_2 "01"
#define STRNG_2_WIDTH_2 "10"
#define STRNG_3_WIDTH_2 "11"
#define BIT_WIDTH_3 3
#define MSK_WIDTH_3 0x7
#define VAL_MAX_WDITH_3 0x7
#define VAL_0_WIDTH_3 0x0
#define VAL_1_WIDTH_3 0x1
#define VAL_2_WIDTH_3 0x2
#define VAL_3_WIDTH_3 0x3
#define VAL_4_WIDTH_3 0x4
#define VAL_5_WIDTH_3 0x5
#define VAL_6_WIDTH_3 0x6
#define VAL_7_WIDTH_3 0x7
#define STRNG_0_WIDTH_3 "000"
#define STRNG_1_WIDTH_3 "001"
#define STRNG_2_WIDTH_3 "010"
#define STRNG_3_WIDTH_3 "011"
#define STRNG_4_WIDTH_3 "100"
#define STRNG_5_WIDTH_3 "101"
#define STRNG_6_WIDTH_3 "110"
#define STRNG_7_WIDTH_3 "111"
#define BIT_WIDTH_4 4
#define MSK_WIDTH_4 0xf
#define VAL_MAX_WDITH_4 0xf
#define VAL_0_WIDTH_4 0x0
#define VAL_1_WIDTH_4 0x1
#define VAL_2_WIDTH_4 0x2
#define VAL_3_WIDTH_4 0x3
#define VAL_4_WIDTH_4 0x4
#define VAL_5_WIDTH_4 0x5
#define VAL_6_WIDTH_4 0x6
#define VAL_7_WIDTH_4 0x7
#define VAL_8_WIDTH_4 0x8
#define VAL_9_WIDTH_4 0x9
#define VAL_10_WIDTH_4 0xa
#define VAL_11_WIDTH_4 0xb
#define VAL_12_WIDTH_4 0xc
#define VAL_13_WIDTH_4 0xd
#define VAL_14_WIDTH_4 0xe
#define VAL_15_WIDTH_4 0xf
#define STRNG_0_WIDTH_4 "0000"
#define STRNG_1_WIDTH_4 "0001"
#define STRNG_2_WIDTH_4 "0010"
#define STRNG_3_WIDTH_4 "0011"
#define STRNG_4_WIDTH_4 "0100"
#define STRNG_5_WIDTH_4 "0101"
#define STRNG_6_WIDTH_4 "0110"
#define STRNG_7_WIDTH_4 "0111"
#define STRNG_8_WIDTH_4 "1000"
#define STRNG_9_WIDTH_4 "1001"
#define STRNG_10_WIDTH_4 "1010"
#define STRNG_11_WIDTH_4 "1011"
#define STRNG_12_WIDTH_4 "1100"
#define STRNG_13_WIDTH_4 "1101"
#define STRNG_14_WIDTH_4 "1110"
#define STRNG_15_WIDTH_4 "1111"
/* specific */
#define FILE_USB_DRIVING_CAPABILITY "USB_DRIVING_CAPABILITY"
#define FILE_RG_USB20_TERM_VREF_SEL "RG_USB20_TERM_VREF_SEL"
#define MSK_RG_USB20_TERM_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define FILE_RG_USB20_HSTX_SRCTRL "RG_USB20_HSTX_SRCTRL"
#define MSK_RG_USB20_HSTX_SRCTRL MSK_WIDTH_3
#define SHFT_RG_USB20_HSTX_SRCTRL 12
#define OFFSET_RG_USB20_HSTX_SRCTRL 0x14
#define FILE_RG_USB20_VRT_VREF_SEL "RG_USB20_VRT_VREF_SEL"
#define MSK_RG_USB20_VRT_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define FILE_RG_USB20_INTR_EN "RG_USB20_INTR_EN"
#define MSK_RG_USB20_INTR_EN MSK_WIDTH_1
#define SHFT_RG_USB20_INTR_EN 5
#define OFFSET_RG_USB20_INTR_EN 0x0
#define FILE_RG_USB20_PHY_REV6 "RG_USB20_PHY_REV6"
#define MSK_RG_USB20_PHY_REV6 MSK_WIDTH_2
#define SHFT_RG_USB20_PHY_REV6 30
#define OFFSET_RG_USB20_PHY_REV6 0x18
#define FILE_RG_USB20_DISCTH "RG_USB20_DISCTH"
#define MSK_RG_USB20_DISCTH MSK_WIDTH_4
#define SHFT_RG_USB20_DISCTH 4
#define OFFSET_RG_USB20_DISCTH 0x18
static struct proc_dir_entry *usb20_phy_procfs_root;
void usb20_phy_debugfs_write_width1(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_1);
clr_val = VAL_1_WIDTH_1;
}
if (!strncmp(buf, STRNG_1_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_1);
set_val = VAL_1_WIDTH_1;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_1 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n"
, __func__, __LINE__);
}
}
void usb20_phy_debugfs_rev6_write(u8 offset, u8 shift, char *buf)
{
u8 set_val = 0xFF;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_2);
set_val = VAL_0_WIDTH_2;
}
if (!strncmp(buf, STRNG_1_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_2);
set_val = VAL_1_WIDTH_2;
}
if (!strncmp(buf, STRNG_2_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_2);
set_val = VAL_2_WIDTH_2;
}
if (!strncmp(buf, STRNG_3_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_2);
set_val = VAL_3_WIDTH_2;
}
if (set_val <= VAL_MAX_WDITH_2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> case offset:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, set_val);
USBPHY_CLR32(offset, (VAL_MAX_WDITH_2<<shift));
USBPHY_SET32(offset, (set_val<<shift));
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
void usb20_phy_debugfs_write_width3(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
clr_val = VAL_7_WIDTH_3;
}
if (!strncmp(buf, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
set_val = VAL_1_WIDTH_3;
}
if (!strncmp(buf, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
set_val = VAL_2_WIDTH_3;
}
if (!strncmp(buf, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
set_val = VAL_3_WIDTH_3;
}
if (!strncmp(buf, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
set_val = VAL_4_WIDTH_3;
}
if (!strncmp(buf, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
set_val = VAL_5_WIDTH_3;
}
if (!strncmp(buf, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
set_val = VAL_6_WIDTH_3;
}
if (!strncmp(buf, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
set_val = VAL_7_WIDTH_3;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_3 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__, offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
void usb20_phy_debugfs_write_width4(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_4);
clr_val = VAL_15_WIDTH_4;
}
if (!strncmp(buf, STRNG_1_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_4);
set_val = VAL_1_WIDTH_4;
}
if (!strncmp(buf, STRNG_2_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_4);
set_val = VAL_2_WIDTH_4;
}
if (!strncmp(buf, STRNG_3_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_4);
set_val = VAL_3_WIDTH_4;
}
if (!strncmp(buf, STRNG_4_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_4);
set_val = VAL_4_WIDTH_4;
}
if (!strncmp(buf, STRNG_5_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_4);
set_val = VAL_5_WIDTH_4;
}
if (!strncmp(buf, STRNG_6_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_4);
set_val = VAL_6_WIDTH_4;
}
if (!strncmp(buf, STRNG_7_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_4);
set_val = VAL_7_WIDTH_4;
}
if (!strncmp(buf, STRNG_8_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_8_WIDTH_4);
set_val = VAL_8_WIDTH_4;
}
if (!strncmp(buf, STRNG_9_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_9_WIDTH_4);
set_val = VAL_9_WIDTH_4;
}
if (!strncmp(buf, STRNG_10_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_10_WIDTH_4);
set_val = VAL_10_WIDTH_4;
}
if (!strncmp(buf, STRNG_11_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_11_WIDTH_4);
set_val = VAL_11_WIDTH_4;
}
if (!strncmp(buf, STRNG_12_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_12_WIDTH_4);
set_val = VAL_12_WIDTH_4;
}
if (!strncmp(buf, STRNG_13_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_13_WIDTH_4);
set_val = VAL_13_WIDTH_4;
}
if (!strncmp(buf, STRNG_14_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_14_WIDTH_4);
set_val = VAL_14_WIDTH_4;
}
if (!strncmp(buf, STRNG_15_WIDTH_4, BIT_WIDTH_4)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_15_WIDTH_4);
set_val = VAL_15_WIDTH_4;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_4 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__, offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
u8 usb20_phy_debugfs_read_val(u8 offset, u8 shft, u8 msk, u8 width, char *str)
{
u32 val;
int i, temp;
val = USBPHY_READ32(offset);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val >> shft;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val & msk;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
temp = val;
str[width] = '\0';
for (i = (width - 1); i >= 0; i--) {
if (val % 2)
str[i] = '1';
else
str[i] = '0';
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str[%d]:%c\n\n",
__func__, __LINE__, i, str[i]);
val /= 2;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str(%s)\n",
__func__, __LINE__, str);
return val;
}
static int usb_driving_capability_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
u8 combined_val, tmp_val = 0xff;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL, BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
combined_val = tmp_val;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL,
BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
if ((tmp_val == (combined_val - 1)) || (tmp_val == combined_val))
combined_val += tmp_val;
else
combined_val = tmp_val * (VAL_MAX_WDITH_3 + 1) + combined_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
seq_printf(s, "\n%s = %d\n", FILE_USB_DRIVING_CAPABILITY, combined_val);
return 0;
}
static int rg_usb20_term_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL,
BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_TERM_VREF_SEL, str);
return 0;
}
static int rg_usb20_hstx_srctrl_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL,
MSK_RG_USB20_HSTX_SRCTRL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_HSTX_SRCTRL, str);
return 0;
}
static int rg_usb20_vrt_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_VRT_VREF_SEL, str);
return 0;
}
static int rg_usb20_intr_en_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN,
MSK_RG_USB20_INTR_EN, BIT_WIDTH_1, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_INTR_EN, str);
return 0;
}
static int rg_usb20_rev6_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6,
MSK_RG_USB20_PHY_REV6, BIT_WIDTH_2, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_PHY_REV6, str);
return 0;
}
static int rg_usb20_discth_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_DISCTH,
SHFT_RG_USB20_DISCTH,
MSK_RG_USB20_DISCTH, BIT_WIDTH_4, str);
seq_printf(s, "%s", str);
return 0;
}
static int usb_driving_capability_open(struct inode *inode, struct file *file)
{
return single_open(file, usb_driving_capability_show, PDE_DATA(inode));
}
static int rg_usb20_term_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_term_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_hstx_srctrl_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_hstx_srctrl_show, PDE_DATA(inode));
}
static int rg_usb20_vrt_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_vrt_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_intr_en_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_intr_en_show, PDE_DATA(inode));
}
static int rg_usb20_rev6_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_rev6_show, PDE_DATA(inode));
}
static int rg_usb20_discth_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_discth_show, PDE_DATA(inode));
}
void val_to_bstring_width3(u8 val, char *str)
{
if (val == VAL_0_WIDTH_3)
memcpy(str, STRNG_0_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_1_WIDTH_3)
memcpy(str, STRNG_1_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_2_WIDTH_3)
memcpy(str, STRNG_2_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_3_WIDTH_3)
memcpy(str, STRNG_3_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_4_WIDTH_3)
memcpy(str, STRNG_4_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_5_WIDTH_3)
memcpy(str, STRNG_5_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_6_WIDTH_3)
memcpy(str, STRNG_6_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_7_WIDTH_3)
memcpy(str, STRNG_7_WIDTH_3, BIT_WIDTH_3 + 1);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), str(%s)\n",
__func__, __LINE__, val, str);
}
static ssize_t usb_driving_capability_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
u8 val, tmp_val;
char str_rg_usb20_term_vref_sel[18], str_rg_usb20_vrt_vref_sel[18];
memset(buf, 0x00, sizeof(buf));
pr_notice("\n");
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtou8(buf, 10, &val) != 0) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtou8, err(%d)\n)\n",
__func__, __LINE__, kstrtou8(buf, 10, &val));
return count;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtou8, val(%d)\n)\n",
__func__, __LINE__, val);
if (val > VAL_7_WIDTH_3 * 2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> wrong val set(%d), direct return\n",
__func__, __LINE__, val);
return count;
}
tmp_val = val;
val /= 2;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), tmp_val(%d)\n",
__func__, __LINE__, val, tmp_val);
val_to_bstring_width3(tmp_val - val, str_rg_usb20_term_vref_sel);
val_to_bstring_width3(val, str_rg_usb20_vrt_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config TERM_VREF_SEL %s\n",
__func__, __LINE__, str_rg_usb20_term_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
str_rg_usb20_term_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config VRT_VREF_SEL %s\n\n",
__func__, __LINE__, str_rg_usb20_vrt_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
str_rg_usb20_vrt_vref_sel);
return count;
}
static ssize_t rg_usb20_term_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_hstx_srctrl_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL, buf);
return count;
}
static ssize_t rg_usb20_vrt_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_intr_en_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width1(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN, buf);
return count;
}
static ssize_t rg_usb20_rev6_write(struct file *file,
const char __user *ubuf, size_t count,
loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_rev6_write(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6, buf);
return count;
}
static ssize_t rg_usb20_discth_write(struct file *file,
const char __user *ubuf, size_t count,
loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width4(OFFSET_RG_USB20_DISCTH,
SHFT_RG_USB20_DISCTH, buf);
return count;
}
static const struct file_operations usb_driving_capability_fops = {
.open = usb_driving_capability_open,
.write = usb_driving_capability_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_term_vref_sel_fops = {
.open = rg_usb20_term_vref_sel_open,
.write = rg_usb20_term_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_hstx_srctrl_fops = {
.open = rg_usb20_hstx_srctrl_open,
.write = rg_usb20_hstx_srctrl_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_vrt_vref_sel_fops = {
.open = rg_usb20_vrt_vref_sel_open,
.write = rg_usb20_vrt_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_intr_en_fops = {
.open = rg_usb20_intr_en_open,
.write = rg_usb20_intr_en_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_rev6_fops = {
.open = rg_usb20_rev6_open,
.write = rg_usb20_rev6_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_discth_fops = {
.open = rg_usb20_discth_open,
.write = rg_usb20_discth_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
int usb20_phy_init_debugfs(void)
{
struct proc_dir_entry *root;
struct proc_dir_entry *file;
int ret;
proc_mkdir("mtk_usb", NULL);
root = proc_mkdir("mtk_usb/usb20_phy", NULL);
if (!root) {
ret = -ENOMEM;
goto err0;
}
file = proc_create(FILE_USB_DRIVING_CAPABILITY, 0644,
root, &usb_driving_capability_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_TERM_VREF_SEL, 0644,
root, &rg_usb20_term_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_HSTX_SRCTRL, 0644,
root, &rg_usb20_hstx_srctrl_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_VRT_VREF_SEL, 0644,
root, &rg_usb20_vrt_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_INTR_EN, 0644,
root, &rg_usb20_intr_en_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_PHY_REV6, 0644,
root, &rg_usb20_rev6_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_DISCTH, 0644,
root, &rg_usb20_discth_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
usb20_phy_procfs_root = root;
return 0;
err1:
proc_remove(root);
err0:
return ret;
}
void /* __init_or_exit */ usb20_phy_exit_debugfs(struct musb *musb)
{
proc_remove(usb20_phy_procfs_root);
}
@@ -0,0 +1,36 @@
#
# Copyright (C) 2015 MediaTek Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
#
# for USB OTG silicon based on Mentor Graphics INVENTRA designs
#
ccflags-$(CONFIG_USB_MTK_HDRC) += -I$(srctree)/drivers/misc/mediatek/usb20
# for battery related
ccflags-y += -I$(srctree)/drivers/misc/mediatek/include/mt-plat
# for SPM control usage
ccflags-y += -I$(srctree)/drivers/misc/mediatek/base/power/include/
# for TYPEC connection management
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/inc
ifeq ($(CONFIG_TCPC_CLASS),y)
ccflags-y += -I$(srctree)/drivers/misc/mediatek/typec/tcpc/inc
endif
# for ep0 test
ccflags-y += -I$(srctree)/drivers/usb/core/
# Phy
obj-$(CONFIG_MTK_MUSB_PHY) += mtk_usb20_phy.o
mtk_usb20_phy-y += usb20_phy.o
mtk_usb20_phy-$(CONFIG_DEBUG_FS) += usb20_phy_debugfs.o
File diff suppressed because it is too large. Load diff
+136
View File
@@ -0,0 +1,136 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/scatterlist.h>
#include <linux/mutex.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include "musb_core.h"
#ifdef CONFIG_MTK_MUSB_CARPLAY_SUPPORT
struct carplay_dev {
struct usb_interface *intf;
struct usb_device *dev;
};
struct carplay_dev *apple_dev;
bool apple;
int send_switch_cmd(void)
{
int retval;
if (apple_dev == NULL) {
DBG(0, "no apple device attach.\n");
return -1;
}
DBG(0, "before usb_control_msg\n");
retval = usb_control_msg(apple_dev->dev,
usb_rcvctrlpipe(apple_dev->dev, 0),
0x51, 0x40, 1, 0, NULL, 0,
USB_CTRL_GET_TIMEOUT);
DBG(0, "after usb_control_msg retval = %d\n", retval);
if (retval != 0) {
DBG(0, "%s fail retval = %d\n", __func__, retval);
return -1;
}
return 0;
}
static int carplay_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
struct usb_device *udev;
struct carplay_dev *car_dev;
DBG(0, "++ carplay probe ++\n");
udev = interface_to_usbdev(intf);
car_dev = kzalloc(sizeof(*car_dev), GFP_KERNEL);
if (!car_dev)
return -ENOMEM;
usb_set_intfdata(intf, car_dev);
car_dev->dev = udev;
car_dev->intf = intf;
apple_dev = car_dev;
apple = true;
if (car_dev->dev == NULL)
DBG(0, "car_dev->dev error\n");
return 0;
}
static void carplay_disconnect(struct usb_interface *intf)
{
struct carplay_dev *car_dev = usb_get_intfdata(intf);
usb_set_intfdata(intf, NULL);
dev_dbg(&intf->dev, "disconnect\n");
car_dev->dev = NULL;
car_dev->intf = NULL;
kfree(car_dev);
apple_dev = NULL;
apple = false;
DBG(0, "%s.\n", __func__);
}
static const struct usb_device_id id_table[] = {
/*-------------------------------------------------------------*/
/* EZ-USB devices which download firmware to replace (or in our
* case augment) the default device implementation.
*/
/* generic EZ-USB FX2 controller (or development board) */
{USB_DEVICE(0x05ac, 0x12a8),
},
/*-------------------------------------------------------------*/
{}
};
/* MODULE_DEVICE_TABLE(usb, id_table); */
static struct usb_driver carplay_driver = {
.name = "carplay",
.id_table = id_table,
.probe = carplay_probe,
.disconnect = carplay_disconnect,
};
/*-------------------------------------------------------------------------*/
static int __init carplay_init(void)
{
DBG(0, "%s register carplay_driver\n", __func__);
return usb_register(&carplay_driver);
}
module_init(carplay_init);
static void __exit carplay_exit(void)
{
usb_deregister(&carplay_driver);
}
module_exit(carplay_exit);
MODULE_DESCRIPTION("USB Core/HCD Testing Driver");
MODULE_LICENSE("GPL");
#endif
File diff suppressed because it is too large. Load diff
+152
View File
@@ -0,0 +1,152 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifndef __USB20_H__
#define __USB20_H__
#ifdef CONFIG_FPGA_EARLY_PORTING
#define FPGA_PLATFORM
#endif
#include <linux/interrupt.h>
#include <musb.h>
struct mt_usb_work {
struct delayed_work dwork;
int ops;
};
/* ToDo: should be moved to glue */
extern struct musb *mtk_musb;
extern struct musb *musb;
struct mt_usb_glue {
struct device *dev;
struct platform_device *musb_pdev;
struct musb *mtk_musb;
/* common power & clock */
struct clk *musb_clk;
struct clk *musb_ref_clk;
struct clk *musb_clk_top_sel;
struct clk *musb_clk_univpll5_d4;
#ifdef CONFIG_PHY_MTK_TPHY
struct platform_device *usb_phy;
struct phy *phy;
struct usb_phy *xceiv;
enum phy_mode phy_mode;
#endif
#ifdef CONFIG_MTK_MUSB_DUAL_ROLE
struct otg_switch_mtk otg_sx;
#endif
};
extern struct mt_usb_glue *glue;
#define glue_to_musb(g) platform_get_drvdata(g->musb)
extern int kernel_init_done;
extern unsigned int upmu_get_rgs_chrdet(void);
extern bool upmu_is_chr_det(void);
extern enum charger_type mt_charger_type_detection(void);
extern void BATTERY_SetUSBState(int usb_state);
extern void upmu_interrupt_chrdet_int_en(unsigned int val);
/* specific USB fuctnion */
enum CABLE_MODE {
CABLE_MODE_CHRG_ONLY = 0,
CABLE_MODE_NORMAL,
CABLE_MODE_HOST_ONLY,
CABLE_MODE_MAX
};
enum USB_CLK_STATE {
NO_CHANGE = 0,
ON_TO_OFF,
OFF_TO_ON,
};
/* specific USB operation */
enum CONNECTION_OPS {
CONNECTION_OPS_DISC = 0,
CONNECTION_OPS_CHECK,
CONNECTION_OPS_CONN
};
enum VBUS_OPS {
VBUS_OPS_OFF = 0,
VBUS_OPS_ON
};
enum MTK_USB_SMC_CALL {
MTK_USB_SMC_INFRA_REQUEST = 0,
MTK_USB_SMC_INFRA_RELEASE,
MTK_USB_SMC_NUM
};
#ifdef CONFIG_MTK_UART_USB_SWITCH
enum PORT_MODE {
PORT_MODE_USB = 0,
PORT_MODE_UART,
PORT_MODE_MAX
};
extern bool usb_phy_check_in_uart_mode(void);
extern void usb_phy_switch_to_usb(void);
extern void usb_phy_switch_to_uart(void);
#endif
#ifdef FPGA_PLATFORM
extern void USB_PHY_Write_Register8(u8 var, u8 addr);
extern u8 USB_PHY_Read_Register8(u8 addr);
#endif
extern void usb_phy_savecurrent(void);
extern void usb_phy_recover(void);
extern bool usb20_check_vbus_on(void);
#ifdef CONFIG_MTK_UART_USB_SWITCH
#define RG_GPIO_SELECT (0x600)
#define GPIO_SEL_OFFSET (4)
#define GPIO_SEL_MASK (0x7 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART0 (0x1 << GPIO_SEL_OFFSET)
#define GPIO_SEL_UART1 (0x2 << GPIO_SEL_OFFSET)
#define GET_GPIO_SEL_VAL(x) ((x & GPIO_SEL_MASK) >> GPIO_SEL_OFFSET)
extern void __iomem *ap_gpio_base;
extern bool in_uart_mode;
#endif
extern int usb20_phy_init_debugfs(void);
extern enum charger_type mt_get_charger_type(void);
#ifndef CONFIG_FPGA_EARLY_PORTING
#include <upmu_common.h>
#endif
#define PHY_IDLE_MODE 0
#define PHY_DEV_ACTIVE 1
#define PHY_HOST_ACTIVE 2
void set_usb_phy_mode(int mode);
#ifdef CONFIG_USB_MTK_OTG
extern void mt_usb_otg_init(struct musb *musb);
extern void mt_usb_otg_exit(struct musb *musb);
extern int mt_usb_get_vbus_status(struct musb *musb);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
extern void mt_usb_host_connect(int delay);
extern void mt_usb_host_disconnect(int delay);
#endif
extern void musb_platform_reset(struct musb *musb);
extern bool usb_enable_clock(bool enable);
extern bool usb_prepare_clock(bool enable);
extern void usb_prepare_enable_clock(bool enable);
/* usb host mode wakeup */
#define USB_WK_CTRL 0x420
#define USB_CDEN BIT(6)
#define USB_IP_SLEEP BIT(12)
#define USB_CDDEBOUNCE(x) (((x) & 0xf) << 28)
#define MISC_CONFIG 0xf08
#define USB_CD_CLR BIT(7)
#endif
@@ -0,0 +1,779 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <usb20.h>
#include <musb_io.h>
#include <mtk_musb_reg.h>
#include <musb_core.h>
#ifdef CONFIG_USB_MTK_OTG
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <mtk_musb.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#ifdef CONFIG_MTK_USB_TYPEC
#ifdef CONFIG_TCPC_CLASS
#include <tcpm.h>
#endif
#endif
#include <linux/workqueue.h>
#include <linux/mutex.h>
#include <linux/phy/phy.h>
#ifdef CONFIG_MTK_MUSB_PHY
#include <usb20_phy.h>
#endif
MODULE_LICENSE("GPL v2");
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
#include <mt-plat/v1/charger_class.h>
static struct charger_device *primary_charger;
#endif
#endif
#include <mt-plat/mtk_boot_common.h>
struct device_node *usb_node;
static int iddig_eint_num;
static ktime_t ktime_start, ktime_end;
static struct musb_fifo_cfg fifo_cfg_host[] = {
{ .hw_ep_num = 1, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 1, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 2, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 3, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 4, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 5, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 6, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 7, .style = FIFO_RX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_TX,
.maxpacket = 512, .mode = BUF_SINGLE},
{ .hw_ep_num = 8, .style = FIFO_RX,
.maxpacket = 64, .mode = BUF_SINGLE},
};
u32 delay_time = 15;
module_param(delay_time, int, 0644);
u32 delay_time1 = 55;
module_param(delay_time1, int, 0644);
u32 iddig_cnt;
module_param(iddig_cnt, int, 0644);
static bool vbus_on;
module_param(vbus_on, bool, 0644);
static int vbus_control;
module_param(vbus_control, int, 0644);
#ifdef CONFIG_MTK_MUSB_PHY
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
#endif
static void _set_vbus(int is_on)
{
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
if (!primary_charger) {
DBG(0, "vbus_init<%d>\n", vbus_on);
primary_charger = get_charger_by_name("primary_chg");
if (!primary_charger) {
DBG(0, "get primary charger device failed\n");
return;
}
}
#endif
#endif
DBG(0, "op<%d>, status<%d>\n", is_on, vbus_on);
if (is_on && !vbus_on) {
/* update flag 1st then enable VBUS to make
* host mode correct used by PMIC
*/
vbus_on = true;
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
charger_dev_enable_otg(primary_charger, true);
charger_dev_set_boost_current_limit(primary_charger, 1500000);
#else
set_chr_enable_otg(0x1);
set_chr_boost_current_limit(1500);
#endif
#endif
} else if (!is_on && vbus_on) {
/* disable VBUS 1st then update flag
* to make host mode correct used by PMIC
*/
vbus_on = false;
#ifdef CONFIG_MTK_CHARGER
#if CONFIG_MTK_GAUGE_VERSION == 30
charger_dev_enable_otg(primary_charger, false);
#else
set_chr_enable_otg(0x0);
#endif
#endif
}
}
int mt_usb_get_vbus_status(struct musb *musb)
{
#if 1
return true;
#else
int ret = 0;
if ((musb_readb(musb->mregs, MUSB_DEVCTL) &
MUSB_DEVCTL_VBUS) != MUSB_DEVCTL_VBUS)
ret = 1;
else
DBG(0, "VBUS error, devctl=%x, power=%d\n",
musb_readb(musb->mregs, MUSB_DEVCTL),
musb->power);
pr_debug("vbus ready = %d\n", ret);
return ret;
#endif
}
#if defined(CONFIG_USBIF_COMPLIANCE)
u32 sw_deboun_time = 1;
#else
u32 sw_deboun_time = 400;
#endif
module_param(sw_deboun_time, int, 0644);
u32 typec_control;
module_param(typec_control, int, 0644);
static bool typec_req_host;
static bool iddig_req_host;
static void do_host_work(struct work_struct *data);
static void issue_host_work(int ops, int delay, bool on_st)
{
struct mt_usb_work *work;
if (!mtk_musb) {
DBG(0, "mtk_musb = NULL\n");
return;
}
/* create and prepare worker */
work = kzalloc(sizeof(struct mt_usb_work), GFP_ATOMIC);
if (!work) {
DBG(0, "work is NULL, directly return\n");
return;
}
work->ops = ops;
INIT_DELAYED_WORK(&work->dwork, do_host_work);
/* issue connection work */
DBG(0, "issue work, ops<%d>, delay<%d>, on_st<%d>\n",
ops, delay, on_st);
if (on_st)
queue_delayed_work(mtk_musb->st_wq,
&work->dwork, msecs_to_jiffies(delay));
else
schedule_delayed_work(&work->dwork,
msecs_to_jiffies(delay));
}
void mt_usb_host_connect(int delay)
{
typec_req_host = true;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_CONN, delay, true);
}
void mt_usb_host_disconnect(int delay)
{
typec_req_host = false;
DBG(0, "%s\n", typec_req_host ? "connect" : "disconnect");
issue_host_work(CONNECTION_OPS_DISC, delay, true);
}
EXPORT_SYMBOL(mt_usb_host_disconnect);
static bool musb_is_host(void)
{
bool host_mode = 0;
if (typec_control)
host_mode = typec_req_host;
else
host_mode = iddig_req_host;
return host_mode;
}
void musb_session_restart(struct musb *musb)
{
void __iomem *mbase = musb->mregs;
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) & (~MUSB_DEVCTL_SESSION)));
#ifdef CONFIG_MTK_MUSB_PHY
DBG(0, "[MUSB] stopped session for VBUSERROR interrupt\n");
USBPHY_SET32(0x6c, (0x3c<<8));
USBPHY_SET32(0x6c, (0x10<<0));
USBPHY_CLR32(0x6c, (0x2c<<0));
DBG(0, "[MUSB] force PHY to idle, 0x6c=%x\n", USBPHY_READ32(0x6c));
mdelay(5);
USBPHY_CLR32(0x6c, (0x3c<<8));
USBPHY_CLR32(0x6c, (0x3c<<0));
DBG(0, "[MUSB] let PHY resample VBUS, 0x6c=%x\n"
, USBPHY_READ32(0x6c));
#endif
musb_writeb(mbase, MUSB_DEVCTL,
(musb_readb(mbase,
MUSB_DEVCTL) | MUSB_DEVCTL_SESSION));
DBG(0, "[MUSB] restart session\n");
}
EXPORT_SYMBOL(musb_session_restart);
static struct delayed_work host_plug_test_work;
int host_plug_test_enable; /* default disable */
module_param(host_plug_test_enable, int, 0644);
int host_plug_in_test_period_ms = 5000;
module_param(host_plug_in_test_period_ms, int, 0644);
int host_plug_out_test_period_ms = 5000;
module_param(host_plug_out_test_period_ms, int, 0644);
int host_test_vbus_off_time_us = 3000;
module_param(host_test_vbus_off_time_us, int, 0644);
int host_test_vbus_only = 1;
module_param(host_test_vbus_only, int, 0644);
static int host_plug_test_triggered;
void switch_int_to_device(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_HIGH);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
void switch_int_to_host(struct musb *musb)
{
irq_set_irq_type(iddig_eint_num, IRQF_TRIGGER_LOW);
enable_irq(iddig_eint_num);
DBG(0, "%s is done\n", __func__);
}
static void do_host_plug_test_work(struct work_struct *data)
{
static ktime_t ktime_begin, ktime_end;
static s64 diff_time;
static int host_on;
static struct wakeup_source *host_test_wakelock;
static int wake_lock_inited;
if (!wake_lock_inited) {
DBG(0, "wake_lock_init\n");
host_test_wakelock = wakeup_source_register(NULL,
"host.test.lock");
wake_lock_inited = 1;
}
host_plug_test_triggered = 1;
/* sync global status */
mb();
__pm_stay_awake(host_test_wakelock);
DBG(0, "BEGIN");
ktime_begin = ktime_get();
host_on = 1;
while (1) {
if (!musb_is_host() && host_on) {
DBG(0, "about to exit");
break;
}
msleep(50);
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_begin));
if (host_on && diff_time >= host_plug_in_test_period_ms) {
host_on = 0;
DBG(0, "OFF\n");
ktime_begin = ktime_get();
/* simulate plug out */
_set_vbus(0);
udelay(host_test_vbus_off_time_us);
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_DISC, 0, false);
} else if (!host_on && diff_time >=
host_plug_out_test_period_ms) {
host_on = 1;
DBG(0, "ON\n");
ktime_begin = ktime_get();
if (!host_test_vbus_only)
issue_host_work(CONNECTION_OPS_CONN, 0, false);
_set_vbus(1);
msleep(100);
}
}
/* wait host_work done */
msleep(1000);
host_plug_test_triggered = 0;
__pm_relax(host_test_wakelock);
DBG(0, "END\n");
}
#define ID_PIN_WORK_RECHECK_TIME 30 /* 30 ms */
#define ID_PIN_WORK_BLOCK_TIMEOUT 30000 /* 30000 ms */
static void do_host_work(struct work_struct *data)
{
u8 devctl = 0;
unsigned long flags;
static int inited, timeout; /* default to 0 */
static s64 diff_time;
bool host_on;
int usb_clk_state = NO_CHANGE;
struct mt_usb_work *work =
container_of(data, struct mt_usb_work, dwork.work);
struct mt_usb_glue *glue = mtk_musb->glue;
/*
* kernel_init_done should be set in
* early-init stage through init.$platform.usb.rc
*/
while (!inited && !kernel_init_done &&
!mtk_musb->is_ready && !timeout) {
ktime_end = ktime_get();
diff_time = ktime_to_ms(ktime_sub(ktime_end, ktime_start));
DBG_LIMIT(3,
"init_done:%d, is_ready:%d, inited:%d, TO:%d, diff:%lld",
kernel_init_done,
mtk_musb->is_ready,
inited,
timeout,
diff_time);
if (diff_time > ID_PIN_WORK_BLOCK_TIMEOUT) {
DBG(0, "diff_time:%lld\n", diff_time);
timeout = 1;
}
msleep(ID_PIN_WORK_RECHECK_TIME);
}
if (!inited) {
DBG(0, "PASS,init_done:%d,is_ready:%d,inited:%d, TO:%d\n",
kernel_init_done, mtk_musb->is_ready,
inited, timeout);
inited = 1;
}
/* always prepare clock and check if need to unprepater later */
/* clk_prepare_cnt +1 here */
usb_prepare_clock(true);
down(&mtk_musb->musb_lock);
host_on = (work->ops ==
CONNECTION_OPS_CONN ? true : false);
DBG(0, "work start, is_host=%d, host_on=%d\n",
mtk_musb->is_host, host_on);
if (host_on && !mtk_musb->is_host) {
/* switch to HOST state before turn on VBUS */
MUSB_HST_MODE(mtk_musb);
/* to make sure all event clear */
msleep(32);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (!usb_on_sram) {
int ret;
ret = gpd_switch_to_sram(mtk_musb->controller);
DBG(0, "gpd_switch_to_sram, ret<%d>\n", ret);
if (ret == 0)
usb_on_sram = 1;
}
#endif
/* setup fifo for host mode */
ep_config_from_table_for_host(mtk_musb);
if (!mtk_musb->host_suspend)
__pm_stay_awake(mtk_musb->usb_lock);
/* this make PHY operation workable */
musb_platform_enable(mtk_musb);
/* for no VBUS sensing IP*/
/* wait VBUS ready */
msleep(100);
/* clear session*/
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl&(~MUSB_DEVCTL_SESSION)));
phy_set_mode(glue->phy, PHY_MODE_INVALID);
/* wait */
mdelay(5);
/* restart session */
devctl = musb_readb(mtk_musb->mregs, MUSB_DEVCTL);
musb_writeb(mtk_musb->mregs,
MUSB_DEVCTL, (devctl | MUSB_DEVCTL_SESSION));
phy_set_mode(glue->phy, PHY_MODE_USB_HOST);
musb_start(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_device(mtk_musb);
if (host_plug_test_enable && !host_plug_test_triggered)
queue_delayed_work(mtk_musb->st_wq,
&host_plug_test_work, 0);
usb_clk_state = OFF_TO_ON;
} else if (!host_on && mtk_musb->is_host) {
/* switch from host -> device */
/* for device no disconnect interrupt */
spin_lock_irqsave(&mtk_musb->lock, flags);
if (mtk_musb->is_active) {
DBG(0, "for not receiving disconnect interrupt\n");
usb_hcd_resume_root_hub(musb_to_hcd(mtk_musb));
musb_root_disconnect(mtk_musb);
}
spin_unlock_irqrestore(&mtk_musb->lock, flags);
DBG(1, "devctl is %x\n",
musb_readb(mtk_musb->mregs, MUSB_DEVCTL));
musb_writeb(mtk_musb->mregs, MUSB_DEVCTL, 0);
if (mtk_musb->usb_lock->active)
__pm_relax(mtk_musb->usb_lock);
/* for no VBUS sensing IP */
phy_set_mode(glue->phy, PHY_MODE_INVALID);
musb_stop(mtk_musb);
if (!typec_control && !host_plug_test_triggered)
switch_int_to_host(mtk_musb);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
if (usb_on_sram) {
gpd_switch_to_dram(mtk_musb->controller);
usb_on_sram = 0;
}
#endif
/* to make sure all event clear */
msleep(32);
mtk_musb->xceiv->otg->state = OTG_STATE_B_IDLE;
/* switch to DEV state after turn off VBUS */
MUSB_DEV_MODE(mtk_musb);
usb_clk_state = ON_TO_OFF;
}
DBG(0, "work end, is_host=%d\n", mtk_musb->is_host);
up(&mtk_musb->musb_lock);
if (usb_clk_state == ON_TO_OFF) {
/* clock on -> of: clk_prepare_cnt -2 */
usb_prepare_clock(false);
usb_prepare_clock(false);
} else if (usb_clk_state == NO_CHANGE) {
/* clock no change : clk_prepare_cnt -1 */
usb_prepare_clock(false);
}
/* free mt_usb_work */
kfree(work);
}
static irqreturn_t mt_usb_ext_iddig_int(int irq, void *dev_id)
{
iddig_cnt++;
iddig_req_host = !iddig_req_host;
DBG(0, "id pin assert, %s\n", iddig_req_host ?
"connect" : "disconnect");
if (iddig_req_host)
mt_usb_host_connect(0);
else
mt_usb_host_disconnect(0);
disable_irq_nosync(iddig_eint_num);
return IRQ_HANDLED;
}
static const struct of_device_id otg_iddig_of_match[] = {
{.compatible = "mediatek,usb_iddig_bi_eint"},
{},
};
static int otg_iddig_probe(struct platform_device *pdev)
{
int ret;
struct device *dev = &pdev->dev;
struct device_node *node = dev->of_node;
iddig_eint_num = irq_of_parse_and_map(node, 0);
DBG(0, "iddig_eint_num<%d>\n", iddig_eint_num);
if (iddig_eint_num < 0)
return -ENODEV;
ret = request_irq(iddig_eint_num, mt_usb_ext_iddig_int,
IRQF_TRIGGER_LOW, "USB_IDDIG", NULL);
if (ret) {
DBG(0,
"request EINT <%d> fail, ret<%d>\n",
iddig_eint_num, ret);
return ret;
}
return 0;
}
static struct platform_driver otg_iddig_driver = {
.probe = otg_iddig_probe,
/* .remove = otg_iddig_remove, */
/* .shutdown = otg_iddig_shutdown, */
.driver = {
.name = "otg_iddig",
.owner = THIS_MODULE,
.of_match_table = of_match_ptr(otg_iddig_of_match),
},
};
static int iddig_int_init(void)
{
int ret = 0;
ret = platform_driver_register(&otg_iddig_driver);
if (ret)
DBG(0, "ret:%d\n", ret);
return 0;
}
void mt_usb_otg_init(struct musb *musb)
{
/* test */
INIT_DELAYED_WORK(&host_plug_test_work, do_host_plug_test_work);
ktime_start = ktime_get();
/* CONNECTION MANAGEMENT*/
#ifdef CONFIG_MTK_USB_TYPEC
DBG(0, "host controlled by TYPEC\n");
typec_control = 1;
#ifdef CONFIG_TCPC_CLASS
DBG(0, "host controlled by IDDIG\n");
iddig_int_init();
vbus_control = 1;
#endif /* CONFIG_TCPC_CLASS */
#endif /* CONFIG_MTK_USB_TYPEC */
/* EP table */
musb->fifo_cfg_host = fifo_cfg_host;
musb->fifo_cfg_host_size = ARRAY_SIZE(fifo_cfg_host);
}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb)
{
DBG(0, "OTG disable vbus\n");
}
EXPORT_SYMBOL(mt_usb_otg_exit);
enum {
DO_IT = 0,
REVERT,
};
#ifdef CONFIG_MTK_MUSB_PHY
static void bypass_disc_circuit(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 13-12 RG_USB20_HSRX_MMODE_SELE, dft:00 */
if (act == DO_IT) {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_SET32(0x18, (0x20<<8));
} else {
USBPHY_CLR32(0x18, (0x10<<8));
USBPHY_CLR32(0x18, (0x20<<8));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
static void disc_threshold_to_max(int act)
{
u32 val;
usb_prepare_enable_clock(true);
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
/* 0x18, 7-4 RG_USB20_DISCTH, dft:1000 */
if (act == DO_IT) {
USBPHY_SET32(0x18, (0xf0<<0));
} else {
USBPHY_CLR32(0x18, (0x70<<0));
USBPHY_SET32(0x18, (0x80<<0));
}
val = USBPHY_READ32(0x18);
DBG(0, "val<0x%x>\n", val);
usb_prepare_enable_clock(false);
}
#endif
static int option;
static int set_option(const char *val, const struct kernel_param *kp)
{
int local_option;
int rv;
/* update module parameter */
rv = param_set_int(val, kp);
if (rv)
return rv;
/* update local_option */
rv = kstrtoint(val, 10, &local_option);
if (rv != 0)
return rv;
DBG(0, "option:%d, local_option:%d\n", option, local_option);
switch (local_option) {
case 0:
DBG(0, "case %d\n", local_option);
iddig_int_init();
break;
case 1:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(0);
break;
case 2:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(0);
break;
case 3:
DBG(0, "case %d\n", local_option);
mt_usb_host_connect(3000);
break;
case 4:
DBG(0, "case %d\n", local_option);
mt_usb_host_disconnect(3000);
break;
#ifdef CONFIG_MTK_MUSB_PHY
case 5:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(DO_IT);
break;
case 6:
DBG(0, "case %d\n", local_option);
disc_threshold_to_max(REVERT);
break;
case 7:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(DO_IT);
break;
case 8:
DBG(0, "case %d\n", local_option);
bypass_disc_circuit(REVERT);
break;
#endif
case 9:
DBG(0, "case %d\n", local_option);
_set_vbus(1);
break;
case 10:
DBG(0, "case %d\n", local_option);
_set_vbus(0);
break;
default:
break;
}
return 0;
}
static struct kernel_param_ops option_param_ops = {
.set = set_option,
.get = param_get_int,
};
module_param_cb(option, &option_param_ops, &option, 0644);
#else
#include "musb_core.h"
/* for not define CONFIG_USB_MTK_OTG */
void mt_usb_otg_init(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_init);
void mt_usb_otg_exit(struct musb *musb) {}
EXPORT_SYMBOL(mt_usb_otg_exit);
void mt_usb_set_vbus(struct musb *musb, int is_on) {}
int mt_usb_get_vbus_status(struct musb *musb) {return 1; }
void switch_int_to_device(struct musb *musb) {}
void switch_int_to_host(struct musb *musb) {}
void musb_session_restart(struct musb *musb) {}
EXPORT_SYMBOL(musb_session_restart);
#endif
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,905 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#ifdef CONFIG_MTK_CLKMGR
#include <mach/mt_clkmgr.h>
#else
#include <linux/clk.h>
#endif
#include <linux/jiffies.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/spinlock.h>
#include <mtk_musb.h>
#include <musb_core.h>
#include "usb20.h"
#include "mtk_devinfo.h"
#include <linux/phy/phy.h>
#ifdef CONFIG_OF
#include <linux/of_address.h>
#endif
#ifdef CONFIG_MTK_AEE_FEATURE
#include <mt-plat/aee.h>
#endif
#include <mt-plat/mtk_boot_common.h>
#define FRA (48)
#define PARA (28)
#ifdef CONFIG_OF
extern struct musb *mtk_musb;
#ifdef USB2_PHY_V2
#define USB_PHY_OFFSET 0x300
#else
#define USB_PHY_OFFSET 0x800
#endif
#define USBPHY_READ8(offset) \
readb((void __iomem *)\
(((unsigned long)\
mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE8(offset, value) writeb(value, (void __iomem *)\
(((unsigned long)mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_SET8(offset, mask) \
USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) | (mask))
#define USBPHY_CLR8(offset, mask) \
USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) & (~(mask)))
#define USBPHY_READ32(offset) \
readl((void __iomem *)(((unsigned long)\
mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE32(offset, value) \
writel(value, (void __iomem *)\
(((unsigned long)mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_SET32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) | (mask))
#define USBPHY_CLR32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) & (~(mask)))
#ifdef MTK_UART_USB_SWITCH
#define UART2_BASE 0x11003000
#endif
#else
#include <mach/mt_reg_base.h>
#define USBPHY_READ8(offset) \
readb((void __iomem *)(USB_SIF_BASE+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE8(offset, value) \
writeb(value, (void __iomem *)(USB_SIF_BASE+USB_PHY_OFFSET+offset))
#define USBPHY_SET8(offset, mask) \
USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) | (mask))
#define USBPHY_CLR8(offset, mask) \
USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) & (~mask))
#define USBPHY_READ32(offset) \
readl((void __iomem *)(USB_SIF_BASE+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE32(offset, value) \
writel(value, (void __iomem *)(USB_SIF_BASE+USB_PHY_OFFSET+offset))
#define USBPHY_SET32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) | (mask))
#define USBPHY_CLR32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) & (~mask))
#endif
#ifdef FPGA_PLATFORM
bool usb_enable_clock(bool enable)
{
return true;
}
bool usb_prepare_clock(bool enable)
{
return true;
}
void usb_prepare_enable_clock(bool enable)
{
}
void usb_phy_poweron(void)
{
}
void usb_phy_savecurrent(void)
{
}
void usb_phy_recover(void)
{
}
/* BC1.2 */
void Charger_Detect_Init(void)
{
}
void Charger_Detect_Release(void)
{
}
void usb_phy_context_save(void)
{
}
void usb_phy_context_restore(void)
{
}
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
return false;
}
void usb_phy_switch_to_uart(void)
{
}
void usb_phy_switch_to_usb(void)
{
}
#endif
#else
#include <linux/of_irq.h>
#include <linux/of_address.h>
#define VAL_MAX_WIDTH_2 0x3
#define VAL_MAX_WIDTH_3 0x7
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_PHY_REV6 0x18
#define SHFT_RG_USB20_PHY_REV6 30
void usb_phy_tuning(void)
{
static bool inited;
static s32 u2_vrt_ref, u2_term_ref, u2_enhance;
struct device_node *of_node;
if (!inited) {
/* apply default value */
u2_vrt_ref = 5;
u2_term_ref = 5;
u2_enhance = 1;
of_node = of_find_compatible_node(NULL,
NULL, "mediatek,phy_tuning");
if (of_node) {
/* value won't be updated if property not being found */
of_property_read_u32(of_node,
"u2_vrt_ref", (u32 *) &u2_vrt_ref);
of_property_read_u32(of_node,
"u2_term_ref", (u32 *) &u2_term_ref);
of_property_read_u32(of_node,
"u2_enhance", (u32 *) &u2_enhance);
}
inited = true;
}
if (u2_vrt_ref != -1) {
if (u2_vrt_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_VRT_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_VRT_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_VRT_VREF_SEL,
u2_vrt_ref << SHFT_RG_USB20_VRT_VREF_SEL);
}
}
if (u2_term_ref != -1) {
if (u2_term_ref <= VAL_MAX_WIDTH_3) {
USBPHY_CLR32(OFFSET_RG_USB20_TERM_VREF_SEL,
VAL_MAX_WIDTH_3 << SHFT_RG_USB20_TERM_VREF_SEL);
USBPHY_SET32(OFFSET_RG_USB20_TERM_VREF_SEL,
u2_term_ref << SHFT_RG_USB20_TERM_VREF_SEL);
}
}
if (u2_enhance != -1) {
if (u2_enhance <= VAL_MAX_WIDTH_2) {
USBPHY_CLR32(OFFSET_RG_USB20_PHY_REV6,
VAL_MAX_WIDTH_2 << SHFT_RG_USB20_PHY_REV6);
USBPHY_SET32(OFFSET_RG_USB20_PHY_REV6,
u2_enhance<<SHFT_RG_USB20_PHY_REV6);
}
}
}
#ifdef CONFIG_MTK_USB2JTAG_SUPPORT
int usb2jtag_usb_init(void)
{
struct device_node *node = NULL;
void __iomem *usb_phy_base, usb_sif_base;
u32 temp;
node = of_find_compatible_node(NULL, NULL, "mediatek,mt6833-usb20");
if (!node) {
pr_err("[USB2JTAG] map node @ mediatek,USB0 failed\n");
return -1;
}
usb_phy_base = of_iomap(node, 1);
if (!usb_phy_base) {
pr_err("[USB2JTAG] iomap usb_phy_base failed\n");
return -1;
}
usb_sif_base = usb_phy_base + USB_PHY_OFFSET;
/* rg_usb20_gpio_ctl: bit[9] = 1 */
temp = readl(usb_sif_base + 0x20);
writel(temp | (1 << 9), usb_sif_base + 0x20);
/* RG_USB20_BGR_EN: bit[0] = 1 */
temp = readl(usb_sif_base);
writel(temp | (1 << 0), usb_sif_base);
/* RG_USB20_BC11_SW_EN: bit[23] = 0 */
temp = readl(usb_sif_base + 0x18);
writel(temp & ~(1 << 23), usb_sif_base + 0x18);
/* wait stable */
mdelay(1);
iounmap(usb_phy_base);
return 0;
}
#endif
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool in_uart_mode;
#endif
void usb_prepare_enable_clock(bool enable)
{
if (enable) {
usb_prepare_clock(true);
usb_enable_clock(true);
} else {
usb_enable_clock(false);
usb_prepare_clock(false);
}
}
DEFINE_MUTEX(prepare_lock);
static atomic_t clk_prepare_cnt = ATOMIC_INIT(0);
bool usb_prepare_clock(bool enable)
{
int before_cnt = atomic_read(&clk_prepare_cnt);
mutex_lock(&prepare_lock);
if (IS_ERR_OR_NULL(musb_clk) ||
IS_ERR_OR_NULL(musb_ref_clk) ||
IS_ERR_OR_NULL(musb_clk_top_sel) ||
IS_ERR_OR_NULL(musb_clk_univpll5_d4)) {
DBG(0, "clk not ready\n");
mutex_unlock(&prepare_lock);
return 0;
}
if (enable) {
if (clk_prepare(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel prepare fail\n");
} else {
if (clk_set_parent(musb_clk_top_sel,
musb_clk_univpll5_d4))
DBG(0, "musb_clk_top_sel set_parent fail\n");
}
if (clk_prepare(musb_clk))
DBG(0, "musb_clk prepare fail\n");
if (clk_prepare(musb_ref_clk))
DBG(0, "musb_ref_clk prepare fail\n");
atomic_inc(&clk_prepare_cnt);
} else {
clk_unprepare(musb_clk_top_sel);
clk_unprepare(musb_ref_clk);
clk_unprepare(musb_clk);
atomic_dec(&clk_prepare_cnt);
}
mutex_unlock(&prepare_lock);
DBG(1, "enable(%d), usb prepare_cnt, before(%d), after(%d)\n",
enable, before_cnt, atomic_read(&clk_prepare_cnt));
#ifdef CONFIG_MTK_AEE_FEATURE
if (atomic_read(&clk_prepare_cnt) < 0)
aee_kernel_warning("usb20", "usb clock prepare_cnt error\n");
#endif
return 1;
}
EXPORT_SYMBOL(usb_prepare_clock);
static DEFINE_SPINLOCK(musb_reg_clock_lock);
bool usb_enable_clock(bool enable)
{
static int count;
static int real_enable = 0, real_disable;
static int virt_enable = 0, virt_disable;
unsigned long flags;
DBG(1, "enable(%d),count(%d),<%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
spin_lock_irqsave(&musb_reg_clock_lock, flags);
if (unlikely(atomic_read(&clk_prepare_cnt) <= 0)) {
DBG_LIMIT(1, "clock not prepare");
goto exit;
}
if (enable && count == 0) {
if (clk_enable(musb_clk_top_sel)) {
DBG(0, "musb_clk_top_sel enable fail\n");
goto exit;
}
if (clk_enable(musb_clk)) {
DBG(0, "musb_clk enable fail\n");
clk_disable(musb_clk_top_sel);
goto exit;
}
if (clk_enable(musb_ref_clk)) {
DBG(0, "musb_ref_clk enable fail\n");
clk_disable(musb_clk);
clk_disable(musb_clk_top_sel);
goto exit;
}
usb_hal_dpidle_request(USB_DPIDLE_FORBIDDEN);
real_enable++;
} else if (!enable && count == 1) {
clk_disable(musb_clk);
clk_disable(musb_ref_clk);
clk_disable(musb_clk_top_sel);
usb_hal_dpidle_request(USB_DPIDLE_ALLOWED);
real_disable++;
}
if (enable)
count++;
else
count = (count == 0) ? 0 : (count - 1);
exit:
if (enable)
virt_enable++;
else
virt_disable++;
spin_unlock_irqrestore(&musb_reg_clock_lock, flags);
DBG(1, "enable(%d),count(%d), <%d,%d,%d,%d>\n",
enable, count, virt_enable, virt_disable,
real_enable, real_disable);
return 1;
}
EXPORT_SYMBOL(usb_enable_clock);
#ifdef CONFIG_MTK_UART_USB_SWITCH
bool usb_phy_check_in_uart_mode(void)
{
u32 usb_port_mode;
usb_enable_clock(true);
udelay(50);
usb_port_mode = USBPHY_READ32(0x68);
usb_enable_clock(false);
if (((usb_port_mode >> 30) & 0x3) == 1) {
DBG(0, "%s:%d - IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = true;
} else {
DBG(0, "%s:%d - NOT IN UART MODE : 0x%x\n",
__func__, __LINE__, usb_port_mode);
in_uart_mode = false;
}
return in_uart_mode;
}
void usb_phy_switch_to_uart(void)
{
unsigned int val = 0;
if (usb_phy_check_in_uart_mode()) {
DBG(0, "Already in UART mode.\n");
return;
}
udelay(50);
/* RG_USB20_BC11_SW_EN 0x11F4_0818[23] = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* Set RG_SUSPENDM 0x11F4_0868[3] to 1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force suspendm 0x11F4_0868[18] = 1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b01 */
USBPHY_CLR32(0x68, (0x3 << 30));
USBPHY_SET32(0x68, (0x1 << 30));
/* force_uart_i 0x11F4_0868[29] = 0*/
USBPHY_CLR32(0x68, (0x1 << 29));
/* force_uart_bias_en 0x11F4_0868[28] = 1 */
USBPHY_SET32(0x68, (0x1 << 28));
/* force_uart_tx_oe 0x11F4_0868[27] = 1 */
USBPHY_SET32(0x68, (0x1 << 27));
/* force_uart_en 0x11F4_0868[26] = 1 */
USBPHY_SET32(0x68, (0x1 << 26));
/* RG_UART_BIAS_EN 0x11F4_086c[18] = 1 */
USBPHY_SET32(0x6C, (0x1 << 18));
/* RG_UART_TX_OE 0x11F4_086c[17] = 1 */
USBPHY_SET32(0x6C, (0x1 << 17));
/* Set RG_UART_EN to 1 */
USBPHY_SET32(0x6C, (0x1 << 16));
/* Set RG_USB20_DM_100K_EN to 1 */
USBPHY_SET32(0x20, (0x1 << 17));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
val = readl(ap_gpio_base);
writel(val | (GPIO_SEL_UART0), ap_gpio_base);
in_uart_mode = true;
}
void usb_phy_switch_to_usb(void)
{
unsigned int val = 0;
/* GPIO Selection */
val = readl(ap_gpio_base);
writel(val & (~(GPIO_SEL_MASK)), ap_gpio_base);
/* clear force_uart_en */
USBPHY_CLR32(0x68, (0x1 << 26));
/* Set rg_uart_mode 0x11F4_0868[31:30] to 2'b00 */
USBPHY_CLR32(0x68, (0x3 << 30));
in_uart_mode = false;
usb_phy_poweron();
}
#endif
void set_usb_phy_mode(int mode)
{
switch (mode) {
case PHY_MODE_USB_DEVICE:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=1, IDPULLUP=1 */
USBPHY_CLR32(0x6C, (0x10<<0));
USBPHY_SET32(0x6C, (0x2F<<0));
USBPHY_SET32(0x6C, (0x3F<<8));
break;
case PHY_MODE_USB_HOST:
/* VBUSVALID=1, AVALID=1, BVALID=1, SESSEND=0, IDDIG=0, IDPULLUP=1 */
USBPHY_CLR32(0x6c, (0x12<<0));
USBPHY_SET32(0x6c, (0x2d<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
case PHY_MODE_INVALID:
/* VBUSVALID=0, AVALID=0, BVALID=0, SESSEND=1, IDDIG=0, IDPULLUP=1 */
USBPHY_SET32(0x6c, (0x11<<0));
USBPHY_CLR32(0x6c, (0x2e<<0));
USBPHY_SET32(0x6c, (0x3f<<8));
break;
default:
DBG(0, "mode error %d\n", mode);
}
DBG(0, "force PHY to mode %d, 0x6c=%x\n", mode, USBPHY_READ32(0x6c));
}
void usb_rev6_setting(int value)
{
static int direct_return;
if (direct_return)
return;
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
if (value)
USBPHY_SET32(0x18, (value << 24));
else
direct_return = 1;
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_poweron(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec for PHY3.3v/1.8v stable. */
udelay(50);
/* RG_USB20_PLL_PREDIV[1:0]=2'b00 */
USBPHY_CLR32(0x00, (0x3 << 6));
/* RG_USB20_PLL_FBDIV[21:0]=22'd1814843 */
USBPHY_CLR32(0x30, (0x3fffff << 0));
USBPHY_SET32(0x30, (0x1bb13b << 0));
/* RG_USB20_PLL_FRA_EN=1'b1 */
USBPHY_SET32(0x44, (0x1 << 3));
/* RG_USB20_PLL_ REFCLK_SEL=1'b1 */
USBPHY_SET32(0x44, (0x1 << 5));
/* RG_USB20_PLL_BW[2:0]=3'b011 */
USBPHY_CLR32(0x08, (0x7 << 19));
USBPHY_SET32(0x08, (0x3 << 19));
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* rg_usb20_dp_100k_mode 1'b1 0x20 18
* USB20_DP_100K_EN 1'b0 0x20 16
* RG_USB20_DM_100K_EN 1'b0 0x20 17
* RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
* force_suspendm 1'b0 0x68 18
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 9) | (0x1 << 8)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* rg_usb20_dp_100k_mode, 1'b1 */
USBPHY_SET32(0x20, (0x1 << 18));
/* USB20_DP_100K_EN 1'b0, RG_USB20_DM_100K_EN, 1'b0 */
USBPHY_CLR32(0x20, ((0x1 << 16) | (0x1 << 17)));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
USBPHY_CLR32(0x18, (0xFF << 24));
USBPHY_SET32(0x18, (0x40 << 24));
/* wait for 800 usec. */
udelay(800);
DBG(0, "usb power on success\n");
}
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_savecurrent_internal(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/*
* force_uart_en 1'b0 0x68 26
* RG_UART_EN 1'b0 0x6c 16
* rg_usb20_gpio_ctl 1'b0 0x20 09
* usb20_gpio_mode 1'b0 0x20 08
* RG_USB20_BC11_SW_EN 1'b0 0x18 23
* RG_USB20_OTG_VBUSCMP_EN 1'b0 0x18 20
* RG_SUSPENDM 1'b1 0x68 03
* force_suspendm 1'b1 0x68 18
* RG_DPPULLDOWN 1'b1 0x68 06
* RG_DMPULLDOWN 1'b1 0x68 07
* RG_XCVRSEL[1:0] 2'b01 0x68 [04-05]
* RG_TERMSEL 1'b1 0x68 02
* RG_DATAIN[3:0] 4'b0000 0x68 [10-13]
* force_dp_pulldown 1'b1 0x68 20
* force_dm_pulldown 1'b1 0x68 21
* force_xcversel 1'b1 0x68 19
* force_termsel 1'b1 0x68 17
* force_datain 1'b1 0x68 23
* RG_SUSPENDM 1'b0 0x68 03
*/
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6c, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 20));
/* RG_SUSPENDM, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 3));
/* force_suspendm, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b01. */
USBPHY_CLR32(0x68, (0x3 << 4));
USBPHY_SET32(0x68, (0x1 << 4));
/* RG_TERMSEL, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b1, force_dm_pulldown, 1'b1,
* force_xcversel, 1'b1, force_termsel, 1'b1, force_datain, 1'b1
*/
USBPHY_SET32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
udelay(800);
/* RG_SUSPENDM, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 3));
udelay(1);
set_usb_phy_mode(PHY_MODE_INVALID);
}
void usb_phy_savecurrent(void)
{
usb_phy_savecurrent_internal();
DBG(0, "usb save current success\n");
}
EXPORT_SYMBOL(usb_phy_savecurrent);
/* M17_USB_PWR Sequence 20160603.xls */
void usb_phy_recover(void)
{
unsigned int efuse_val = 0;
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode) {
DBG(0, "At UART mode. No %s\n", __func__);
return;
}
#endif
/* wait 50 usec. */
udelay(50);
/* RG_USB20_PLL_PREDIV[1:0]=2'b00 */
USBPHY_CLR32(0x00, (0x3 << 6));
/* RG_USB20_PLL_FBDIV[21:0]=22'd1814843 */
USBPHY_CLR32(0x30, (0x3fffff << 0));
USBPHY_SET32(0x30, (0x1bb13b << 0));
/* RG_USB20_PLL_FRA_EN=1'b1 */
USBPHY_SET32(0x44, (0x1 << 3));
/* RG_USB20_PLL_ REFCLK_SEL=1'b1 */
USBPHY_SET32(0x44, (0x1 << 5));
/* RG_USB20_PLL_BW[2:0]=3'b011 */
USBPHY_CLR32(0x08, (0x7 << 19));
USBPHY_SET32(0x08, (0x3 << 19));
/*
* 04.force_uart_en 1'b0 0x68 26
* 04.RG_UART_EN 1'b0 0x6C 16
* 04.rg_usb20_gpio_ctl 1'b0 0x20 09
* 04.usb20_gpio_mode 1'b0 0x20 08
* 05.force_suspendm 1'b0 0x68 18
* 06.RG_DPPULLDOWN 1'b0 0x68 06
* 07.RG_DMPULLDOWN 1'b0 0x68 07
* 08.RG_XCVRSEL[1:0] 2'b00 0x68 [04:05]
* 09.RG_TERMSEL 1'b0 0x68 02
* 10.RG_DATAIN[3:0] 4'b0000 0x68 [10:13]
* 11.force_dp_pulldown 1'b0 0x68 20
* 12.force_dm_pulldown 1'b0 0x68 21
* 13.force_xcversel 1'b0 0x68 19
* 14.force_termsel 1'b0 0x68 17
* 15.force_datain 1'b0 0x68 23
* 16.RG_USB20_BC11_SW_EN 1'b0 0x18 23
* 17.RG_USB20_OTG_VBUSCMP_EN 1'b1 0x18 20
*/
/* clean PUPD_BIST_EN */
/* PUPD_BIST_EN = 1'b0 */
/* PMIC will use it to detect charger type */
/* NEED?? USBPHY_CLR8(0x1d, 0x10);*/
USBPHY_CLR32(0x1c, (0x1 << 12));
/* force_uart_en, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 26));
/* RG_UART_EN, 1'b0 */
USBPHY_CLR32(0x6C, (0x1 << 16));
/* rg_usb20_gpio_ctl, 1'b0, usb20_gpio_mode, 1'b0 */
USBPHY_CLR32(0x20, (0x1 << 9));
USBPHY_CLR32(0x20, (0x1 << 8));
/* force_suspendm, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 18));
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, ((0x1 << 6) | (0x1 << 7)));
/* RG_XCVRSEL[1:0], 2'b00. */
USBPHY_CLR32(0x68, (0x3 << 4));
/* RG_TERMSEL, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 2));
/* RG_DATAIN[3:0], 4'b0000 */
USBPHY_CLR32(0x68, (0xF << 10));
/* force_dp_pulldown, 1'b0, force_dm_pulldown, 1'b0,
* force_xcversel, 1'b0, force_termsel, 1'b0, force_datain, 1'b0
*/
USBPHY_CLR32(0x68, ((0x1 << 20) | (0x1 << 21) |
(0x1 << 19) | (0x1 << 17) | (0x1 << 23)));
/* RG_USB20_BC11_SW_EN, 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
/* RG_USB20_OTG_VBUSCMP_EN, 1'b1 */
USBPHY_SET32(0x18, (0x1 << 20));
/* RG_USB20_PHY_REV[7:0] = 8'b01000000 */
usb_rev6_setting(0x40);
/* wait 800 usec. */
udelay(800);
/* force enter device mode */
set_usb_phy_mode(PHY_DEV_ACTIVE);
/* M_ANALOG8[4:0] => RG_USB20_INTR_CAL[4:0] */
efuse_val = (get_devinfo_with_index(107) & (0x1f<<0)) >> 0;
if (efuse_val) {
DBG(0, "apply efuse setting, RG_USB20_INTR_CAL=0x%x\n",
efuse_val);
USBPHY_CLR32(0x04, (0x1F<<19));
USBPHY_SET32(0x04, (efuse_val<<19));
}
/* RG_USB20_DISCTH[7:4], 4'b0111 for 700 mV */
USBPHY_CLR32(0x18, (0xf0<<0));
USBPHY_SET32(0x18, (0x70<<0));
USBPHY_SET32(0x18, (0x1<<28));
USBPHY_CLR32(0x18, (0xf<<0));
USBPHY_SET32(0x18, (0x5<<0));
usb_phy_tuning();
DBG(0, "usb recovery success\n");
}
EXPORT_SYMBOL(usb_phy_recover);
/* BC1.2 */
void Charger_Detect_Init(void)
{
#if 0
if ((get_boot_mode() == META_BOOT) ||
(get_boot_mode() == ADVMETA_BOOT) ||
!mtk_musb) {
DBG(0, "%s Skip, musb<%p>\n",
__func__, mtk_musb);
return;
}
#endif
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
/* RG_USB20_BC11_SW_EN = 1'b1 */
USBPHY_SET32(0x18, (0x1 << 23));
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Init);
void Charger_Detect_Release(void)
{
#if 0
if ((get_boot_mode() == META_BOOT) ||
(get_boot_mode() == ADVMETA_BOOT) ||
!mtk_musb) {
DBG(0, "%s Skip, musb<%p>\n",
__func__, mtk_musb);
return;
}
#endif
usb_prepare_enable_clock(true);
/* RG_USB20_BC11_SW_EN = 1'b0 */
USBPHY_CLR32(0x18, (0x1 << 23));
udelay(1);
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
EXPORT_SYMBOL(Charger_Detect_Release);
void usb_phy_context_save(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
in_uart_mode = usb_phy_check_in_uart_mode();
#endif
}
void usb_phy_context_restore(void)
{
#ifdef CONFIG_MTK_UART_USB_SWITCH
if (in_uart_mode)
usb_phy_switch_to_uart();
#endif
}
void usb_dpdm_pulldown(bool enable)
{
DBG(0, "%s: enable=%d\n", __func__, enable);
usb_prepare_enable_clock(true);
/* wait 50 usec. */
udelay(50);
if (enable) {
/* RG_DPPULLDOWN, 1'b1, RG_DMPULLDOWN, 1'b1 */
USBPHY_SET32(0x68, (0x1 << 6) | (0x1 << 7));
/* RG_USB20_PHY_REV */
USBPHY_CLR32(0x18, (0x2 << 24));
} else {
/* RG_DPPULLDOWN, 1'b0, RG_DMPULLDOWN, 1'b0 */
USBPHY_CLR32(0x68, (0x1 << 6) | (0x1 << 7));
/* RG_USB20_PHY_REV */
USBPHY_SET32(0x18, (0x2 << 24));
}
usb_prepare_enable_clock(false);
DBG(0, "%s\n", __func__);
}
#endif
@@ -0,0 +1,725 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (c) 2019 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/uaccess.h>
#include <musb_core.h>
#include <musb_debug.h>
#include <mtk_musb.h>
/* general */
#define BIT_WIDTH_1 1
#define MSK_WIDTH_1 0x1
#define VAL_MAX_WDITH_1 0x1
#define VAL_0_WIDTH_1 0x0
#define VAL_1_WIDTH_1 0x1
#define STRNG_0_WIDTH_1 "0"
#define STRNG_1_WIDTH_1 "1"
#define BIT_WIDTH_2 2
#define MSK_WIDTH_2 0x3
#define VAL_MAX_WDITH_2 0x3
#define VAL_0_WIDTH_2 0x0
#define VAL_1_WIDTH_2 0x1
#define VAL_2_WIDTH_2 0x2
#define VAL_3_WIDTH_2 0x3
#define STRNG_0_WIDTH_2 "00"
#define STRNG_1_WIDTH_2 "01"
#define STRNG_2_WIDTH_2 "10"
#define STRNG_3_WIDTH_2 "11"
#define BIT_WIDTH_3 3
#define MSK_WIDTH_3 0x7
#define VAL_MAX_WDITH_3 0x7
#define VAL_0_WIDTH_3 0x0
#define VAL_1_WIDTH_3 0x1
#define VAL_2_WIDTH_3 0x2
#define VAL_3_WIDTH_3 0x3
#define VAL_4_WIDTH_3 0x4
#define VAL_5_WIDTH_3 0x5
#define VAL_6_WIDTH_3 0x6
#define VAL_7_WIDTH_3 0x7
#define STRNG_0_WIDTH_3 "000"
#define STRNG_1_WIDTH_3 "001"
#define STRNG_2_WIDTH_3 "010"
#define STRNG_3_WIDTH_3 "011"
#define STRNG_4_WIDTH_3 "100"
#define STRNG_5_WIDTH_3 "101"
#define STRNG_6_WIDTH_3 "110"
#define STRNG_7_WIDTH_3 "111"
/* specific */
#define FILE_USB_DRIVING_CAPABILITY "USB_DRIVING_CAPABILITY"
#define FILE_RG_USB20_TERM_VREF_SEL "RG_USB20_TERM_VREF_SEL"
#define MSK_RG_USB20_TERM_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_TERM_VREF_SEL 8
#define OFFSET_RG_USB20_TERM_VREF_SEL 0x4
#define FILE_RG_USB20_HSTX_SRCTRL "RG_USB20_HSTX_SRCTRL"
#define MSK_RG_USB20_HSTX_SRCTRL MSK_WIDTH_3
#define SHFT_RG_USB20_HSTX_SRCTRL 12
#define OFFSET_RG_USB20_HSTX_SRCTRL 0x14
#define FILE_RG_USB20_VRT_VREF_SEL "RG_USB20_VRT_VREF_SEL"
#define MSK_RG_USB20_VRT_VREF_SEL MSK_WIDTH_3
#define SHFT_RG_USB20_VRT_VREF_SEL 12
#define OFFSET_RG_USB20_VRT_VREF_SEL 0x4
#define FILE_RG_USB20_INTR_EN "RG_USB20_INTR_EN"
#define MSK_RG_USB20_INTR_EN MSK_WIDTH_1
#define SHFT_RG_USB20_INTR_EN 5
#define OFFSET_RG_USB20_INTR_EN 0x0
#define FILE_RG_USB20_PHY_REV6 "RG_USB20_PHY_REV6"
#define MSK_RG_USB20_PHY_REV6 MSK_WIDTH_2
#define SHFT_RG_USB20_PHY_REV6 30
#define OFFSET_RG_USB20_PHY_REV6 0x18
static struct proc_dir_entry *usb20_phy_procfs_root;
void usb20_phy_debugfs_write_width1(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_1);
clr_val = VAL_1_WIDTH_1;
}
if (!strncmp(buf, STRNG_1_WIDTH_1, BIT_WIDTH_1)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_1);
set_val = VAL_1_WIDTH_1;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_1 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__,
offset, clr_val,
set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n"
, __func__, __LINE__);
}
}
void usb20_phy_debugfs_rev6_write(u8 offset, u8 shift, char *buf)
{
u8 set_val = 0xFF;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_2);
set_val = VAL_0_WIDTH_2;
}
if (!strncmp(buf, STRNG_1_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_2);
set_val = VAL_1_WIDTH_2;
}
if (!strncmp(buf, STRNG_2_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_2);
set_val = VAL_2_WIDTH_2;
}
if (!strncmp(buf, STRNG_3_WIDTH_2, BIT_WIDTH_2)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_2);
set_val = VAL_3_WIDTH_2;
}
if (set_val <= VAL_MAX_WDITH_2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> case offset:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, set_val);
USBPHY_CLR32(offset, (VAL_MAX_WDITH_2<<shift));
USBPHY_SET32(offset, (set_val<<shift));
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
void usb20_phy_debugfs_write_width3(u8 offset, u8 shift, char *buf)
{
u32 clr_val = 0, set_val = 0;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> s(%s)\n",
__func__, __LINE__, buf);
if (!strncmp(buf, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
clr_val = VAL_7_WIDTH_3;
}
if (!strncmp(buf, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
set_val = VAL_1_WIDTH_3;
}
if (!strncmp(buf, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
set_val = VAL_2_WIDTH_3;
}
if (!strncmp(buf, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
set_val = VAL_3_WIDTH_3;
}
if (!strncmp(buf, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
set_val = VAL_4_WIDTH_3;
}
if (!strncmp(buf, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
set_val = VAL_5_WIDTH_3;
}
if (!strncmp(buf, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
set_val = VAL_6_WIDTH_3;
}
if (!strncmp(buf, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
set_val = VAL_7_WIDTH_3;
}
if (clr_val || set_val) {
clr_val = VAL_MAX_WDITH_3 - set_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, before shft\n",
__func__, __LINE__, offset, clr_val, set_val);
clr_val <<= shift;
set_val <<= shift;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, clr_val:%x, set_val:%x, after shft\n",
__func__, __LINE__, offset, clr_val, set_val);
USBPHY_CLR32(offset, clr_val);
USBPHY_SET32(offset, set_val);
} else {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> do nothing\n",
__func__, __LINE__);
}
}
u8 usb20_phy_debugfs_read_val(u8 offset, u8 shft, u8 msk, u8 width, char *str)
{
u32 val;
int i, temp;
val = USBPHY_READ32(offset);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val >> shft;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
val = val & msk;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> offset:%x, val:%x, shft:%x, msk:%x\n",
__func__, __LINE__, offset, val, shft, msk);
temp = val;
str[width] = '\0';
for (i = (width - 1); i >= 0; i--) {
if (val % 2)
str[i] = '1';
else
str[i] = '0';
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str[%d]:%c\n\n",
__func__, __LINE__, i, str[i]);
val /= 2;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> str(%s)\n",
__func__, __LINE__, str);
return val;
}
static int usb_driving_capability_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
u8 combined_val, tmp_val = 0xff;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL, BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
combined_val = tmp_val;
val = usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL,
BIT_WIDTH_3, str);
if (!strncmp(str, STRNG_0_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_0_WIDTH_3);
tmp_val = VAL_0_WIDTH_3;
}
if (!strncmp(str, STRNG_1_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_1_WIDTH_3);
tmp_val = VAL_1_WIDTH_3;
}
if (!strncmp(str, STRNG_2_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_2_WIDTH_3);
tmp_val = VAL_2_WIDTH_3;
}
if (!strncmp(str, STRNG_3_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_3_WIDTH_3);
tmp_val = VAL_3_WIDTH_3;
}
if (!strncmp(str, STRNG_4_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_4_WIDTH_3);
tmp_val = VAL_4_WIDTH_3;
}
if (!strncmp(str, STRNG_5_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_5_WIDTH_3);
tmp_val = VAL_5_WIDTH_3;
}
if (!strncmp(str, STRNG_6_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_6_WIDTH_3);
tmp_val = VAL_6_WIDTH_3;
}
if (!strncmp(str, STRNG_7_WIDTH_3, BIT_WIDTH_3)) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> %s case\n",
__func__, __LINE__, STRNG_7_WIDTH_3);
tmp_val = VAL_7_WIDTH_3;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
if ((tmp_val == (combined_val - 1)) || (tmp_val == combined_val))
combined_val += tmp_val;
else
combined_val = tmp_val * (VAL_MAX_WDITH_3 + 1) + combined_val;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> combined_val(%d), tmp_val(%d)\n",
__func__, __LINE__, combined_val, tmp_val);
seq_printf(s, "\n%s = %d\n", FILE_USB_DRIVING_CAPABILITY, combined_val);
return 0;
}
static int rg_usb20_term_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
MSK_RG_USB20_TERM_VREF_SEL,
BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_TERM_VREF_SEL, str);
return 0;
}
static int rg_usb20_hstx_srctrl_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL,
MSK_RG_USB20_HSTX_SRCTRL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_HSTX_SRCTRL, str);
return 0;
}
static int rg_usb20_vrt_vref_sel_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
MSK_RG_USB20_VRT_VREF_SEL, BIT_WIDTH_3, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_VRT_VREF_SEL, str);
return 0;
}
static int rg_usb20_intr_en_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN,
MSK_RG_USB20_INTR_EN, BIT_WIDTH_1, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_INTR_EN, str);
return 0;
}
static int rg_usb20_rev6_show(struct seq_file *s, void *unused)
{
u8 val;
char str[16];
val =
usb20_phy_debugfs_read_val(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6,
MSK_RG_USB20_PHY_REV6, BIT_WIDTH_2, str);
seq_printf(s, "\n%s = %s\n", FILE_RG_USB20_PHY_REV6, str);
return 0;
}
static int usb_driving_capability_open(struct inode *inode, struct file *file)
{
return single_open(file, usb_driving_capability_show, PDE_DATA(inode));
}
static int rg_usb20_term_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_term_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_hstx_srctrl_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_hstx_srctrl_show, PDE_DATA(inode));
}
static int rg_usb20_vrt_vref_sel_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_vrt_vref_sel_show, PDE_DATA(inode));
}
static int rg_usb20_intr_en_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_intr_en_show, PDE_DATA(inode));
}
static int rg_usb20_rev6_open(struct inode *inode, struct file *file)
{
return single_open(file, rg_usb20_rev6_show, PDE_DATA(inode));
}
void val_to_bstring_width3(u8 val, char *str)
{
if (val == VAL_0_WIDTH_3)
memcpy(str, STRNG_0_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_1_WIDTH_3)
memcpy(str, STRNG_1_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_2_WIDTH_3)
memcpy(str, STRNG_2_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_3_WIDTH_3)
memcpy(str, STRNG_3_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_4_WIDTH_3)
memcpy(str, STRNG_4_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_5_WIDTH_3)
memcpy(str, STRNG_5_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_6_WIDTH_3)
memcpy(str, STRNG_6_WIDTH_3, BIT_WIDTH_3 + 1);
if (val == VAL_7_WIDTH_3)
memcpy(str, STRNG_7_WIDTH_3, BIT_WIDTH_3 + 1);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), str(%s)\n",
__func__, __LINE__, val, str);
}
static ssize_t usb_driving_capability_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
u8 val, tmp_val;
char str_rg_usb20_term_vref_sel[18], str_rg_usb20_vrt_vref_sel[18];
memset(buf, 0x00, sizeof(buf));
pr_notice("\n");
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtol(buf, 10, (long *)&val) != 0) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtol, err(%d)\n)\n",
__func__, __LINE__, kstrtol(buf, 10, (long *)&val));
return count;
}
pr_notice("MTK_ICUSB [DBG], <%s(), %d> kstrtol, val(%d)\n)\n",
__func__, __LINE__, val);
if (val > VAL_7_WIDTH_3 * 2) {
pr_notice("MTK_ICUSB [DBG], <%s(), %d> wrong val set(%d), direct return\n",
__func__, __LINE__, val);
return count;
}
tmp_val = val;
val /= 2;
pr_notice("MTK_ICUSB [DBG], <%s(), %d> val(%d), tmp_val(%d)\n",
__func__, __LINE__, val, tmp_val);
val_to_bstring_width3(tmp_val - val, str_rg_usb20_term_vref_sel);
val_to_bstring_width3(val, str_rg_usb20_vrt_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config TERM_VREF_SEL %s\n",
__func__, __LINE__, str_rg_usb20_term_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL,
str_rg_usb20_term_vref_sel);
pr_notice("MTK_ICUSB [DBG], <%s(), %d> Config VRT_VREF_SEL %s\n\n",
__func__, __LINE__, str_rg_usb20_vrt_vref_sel);
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL,
str_rg_usb20_vrt_vref_sel);
return count;
}
static ssize_t rg_usb20_term_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_TERM_VREF_SEL,
SHFT_RG_USB20_TERM_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_hstx_srctrl_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_HSTX_SRCTRL,
SHFT_RG_USB20_HSTX_SRCTRL, buf);
return count;
}
static ssize_t rg_usb20_vrt_vref_sel_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width3(OFFSET_RG_USB20_VRT_VREF_SEL,
SHFT_RG_USB20_VRT_VREF_SEL, buf);
return count;
}
static ssize_t rg_usb20_intr_en_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_write_width1(OFFSET_RG_USB20_INTR_EN,
SHFT_RG_USB20_INTR_EN, buf);
return count;
}
static ssize_t rg_usb20_rev6_write(struct file *file,
const char __user *ubuf, size_t count,
loff_t *ppos)
{
char buf[18];
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
usb20_phy_debugfs_rev6_write(OFFSET_RG_USB20_PHY_REV6,
SHFT_RG_USB20_PHY_REV6, buf);
return count;
}
static const struct file_operations usb_driving_capability_fops = {
.open = usb_driving_capability_open,
.write = usb_driving_capability_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_term_vref_sel_fops = {
.open = rg_usb20_term_vref_sel_open,
.write = rg_usb20_term_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_hstx_srctrl_fops = {
.open = rg_usb20_hstx_srctrl_open,
.write = rg_usb20_hstx_srctrl_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_vrt_vref_sel_fops = {
.open = rg_usb20_vrt_vref_sel_open,
.write = rg_usb20_vrt_vref_sel_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_intr_en_fops = {
.open = rg_usb20_intr_en_open,
.write = rg_usb20_intr_en_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static const struct file_operations rg_usb20_rev6_fops = {
.open = rg_usb20_rev6_open,
.write = rg_usb20_rev6_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
int usb20_phy_init_debugfs(void)
{
struct proc_dir_entry *root;
struct proc_dir_entry *file;
int ret;
proc_mkdir("mtk_usb", NULL);
root = proc_mkdir("mtk_usb/usb20_phy", NULL);
if (!root) {
ret = -ENOMEM;
goto err0;
}
file = proc_create(FILE_USB_DRIVING_CAPABILITY, 0644,
root, &usb_driving_capability_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_TERM_VREF_SEL, 0644,
root, &rg_usb20_term_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_HSTX_SRCTRL, 0644,
root, &rg_usb20_hstx_srctrl_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_VRT_VREF_SEL, 0644,
root, &rg_usb20_vrt_vref_sel_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_INTR_EN, 0644,
root, &rg_usb20_intr_en_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
file = proc_create(FILE_RG_USB20_PHY_REV6, 0644,
root, &rg_usb20_rev6_fops);
if (!file) {
ret = -ENOMEM;
goto err1;
}
usb20_phy_procfs_root = root;
return 0;
err1:
proc_remove(root);
err0:
return ret;
}
void /* __init_or_exit */ usb20_phy_exit_debugfs(struct musb *musb)
{
proc_remove(usb20_phy_procfs_root);
}
+116
View File
@@ -0,0 +1,116 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/usb/class-dual-role.h>
#include "musb_core.h"
#include "mtk_musb.h"
#ifdef CONFIG_DUAL_ROLE_USB_INTF
static enum dual_role_property mt_dual_role_props[] = {
DUAL_ROLE_PROP_MODE,
DUAL_ROLE_PROP_PR,
DUAL_ROLE_PROP_DR,
/* DUAL_ROLE_PROP_VCONN_SUPPLY, */
};
static int mt_dual_role_get_prop(struct dual_role_phy_instance *dual_role,
enum dual_role_property prop, unsigned int *val)
{
struct musb *musb = dev_get_drvdata(dual_role->dev.parent);
int ret = 0;
int mode, pr, dr;
dev_info(musb->controller, "prop=%d, power=%d, is_host=%d",
prop, musb->power, is_host_active(musb));
if (musb->power) {
if (is_host_active(musb)) {
mode = DUAL_ROLE_PROP_MODE_DFP;
pr = DUAL_ROLE_PROP_PR_SRC;
dr = DUAL_ROLE_PROP_DR_HOST;
} else {
mode = DUAL_ROLE_PROP_MODE_UFP;
pr = DUAL_ROLE_PROP_PR_SNK;
dr = DUAL_ROLE_PROP_DR_DEVICE;
}
} else {
mode = DUAL_ROLE_PROP_MODE_NONE;
pr = DUAL_ROLE_PROP_PR_NONE;
dr = DUAL_ROLE_PROP_DR_NONE;
}
switch (prop) {
case DUAL_ROLE_PROP_MODE:
*val = mode;
break;
case DUAL_ROLE_PROP_PR:
*val = pr;
break;
case DUAL_ROLE_PROP_DR:
*val = dr;
break;
case DUAL_ROLE_PROP_VCONN_SUPPLY:
*val = DUAL_ROLE_PROP_VCONN_SUPPLY_NO;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static int mt_dual_role_prop_is_writeable(
struct dual_role_phy_instance *dual_role, enum dual_role_property prop)
{
/* not support writeable */
return 0;
}
static int mt_dual_role_set_prop(struct dual_role_phy_instance *dual_role,
enum dual_role_property prop, const unsigned int *val)
{
/* do nothing */
return 0;
}
int mt_usb_dual_role_changed(struct musb *musb)
{
if (musb->dr_usb)
dual_role_instance_changed(musb->dr_usb);
return 0;
}
int mt_usb_dual_role_init(struct musb *musb)
{
struct dual_role_phy_desc *dual_desc;
dual_desc = devm_kzalloc(musb->controller, sizeof(*dual_desc),
GFP_KERNEL);
if (!dual_desc)
return -ENOMEM;
dual_desc->name = "dual-role-usb20";
dual_desc->supported_modes = DUAL_ROLE_SUPPORTED_MODES_DFP_AND_UFP;
dual_desc->properties = mt_dual_role_props;
dual_desc->num_properties = ARRAY_SIZE(mt_dual_role_props);
dual_desc->get_property = mt_dual_role_get_prop;
dual_desc->set_property = mt_dual_role_set_prop;
dual_desc->property_is_writeable = mt_dual_role_prop_is_writeable;
musb->dr_usb = devm_dual_role_instance_register(musb->controller,
dual_desc);
if (IS_ERR(musb->dr_usb)) {
dev_info(musb->controller, "fail to register dual role usb\n");
return -EINVAL;
}
return 0;
}
#endif /* CONFIG_DUAL_ROLE_USB_INTF */
+83
View File
@@ -0,0 +1,83 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MUSB_MTK_MUSB_H__
#define __MUSB_MTK_MUSB_H__
#ifdef CONFIG_MTK_MUSB_PHY
#ifdef CONFIG_OF
extern struct musb *mtk_musb;
#ifdef USB2_PHY_V2
#define USB_PHY_OFFSET 0x300
#else
#define USB_PHY_OFFSET 0x800
#endif
#define USBPHY_READ8(offset) \
readb((void __iomem *)\
(((unsigned long)\
mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE8(offset, value) writeb(value, (void __iomem *)\
(((unsigned long)mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_SET8(offset, mask) \
USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) | (mask))
#define USBPHY_CLR8(offset, mask) \
USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) & (~(mask)))
#define USBPHY_READ32(offset) \
readl((void __iomem *)(((unsigned long)\
mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_WRITE32(offset, value) \
writel(value, (void __iomem *)\
(((unsigned long)mtk_musb->xceiv->io_priv)+USB_PHY_OFFSET+offset))
#define USBPHY_SET32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) | (mask))
#define USBPHY_CLR32(offset, mask) \
USBPHY_WRITE32(offset, (USBPHY_READ32(offset)) & (~(mask)))
#endif /* End of CONFIG_OF define */
#endif /* End of CONFIG_MTK_MUSB_PHY */
struct musb;
enum usb_state_enum {
USB_SUSPEND = 0,
USB_UNCONFIGURED,
USB_CONFIGURED
};
/* USB phy and clock */
extern bool usb_pre_clock(bool enable);
#ifdef CONFIG_MTK_UART_USB_SWITCH
extern void usb_phy_context_restore(void);
extern void usb_phy_context_save(void);
#endif
/* general USB */
extern bool mt_usb_is_device(void);
extern void mt_usb_connect(void);
extern void mt_usb_disconnect(void);
extern void mt_usb_reconnect(void);
extern bool usb_cable_connected(struct musb *musb);
extern void musb_sync_with_bat(struct musb *musb, int usb_state);
bool is_saving_mode(void);
/* host and otg */
extern void mt_usb_init_drvvbus(void);
extern void mt_usb_iddig_int(struct musb *musb);
extern void switch_int_to_device(struct musb *musb);
extern void switch_int_to_host(struct musb *musb);
extern void switch_int_to_host_and_mask(struct musb *musb);
extern void musb_session_restart(struct musb *musb);
#ifdef CONFIG_DUAL_ROLE_USB_INTF
extern int mt_usb_dual_role_init(struct musb *musb);
extern int mt_usb_dual_role_changed(struct musb *musb);
#endif /* CONFIG_DUAL_ROLE_USB_INTF */
extern bool is_usb_rdy(void);
extern void Charger_Detect_Init(void);
extern void Charger_Detect_Release(void);
#endif
+545
View File
@@ -0,0 +1,545 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MT_MUSB_REG_H__
#define __MT_MUSB_REG_H__
#include "musb_debug.h"
#define MUSB_EP0_FIFOSIZE 64 /* This is non-configurable */
/*
* MUSB Register bits
*/
/* POWER */
#define MUSB_POWER_ISOUPDATE 0x80
#define MUSB_POWER_SOFTCONN 0x40
#define MUSB_POWER_HSENAB 0x20
#define MUSB_POWER_HSMODE 0x10
#define MUSB_POWER_RESET 0x08
#define MUSB_POWER_RESUME 0x04
#define MUSB_POWER_SUSPENDM 0x02
#define MUSB_POWER_ENSUSPEND 0x01
/* INTRUSB */
#define MUSB_INTR_SUSPEND 0x01
#define MUSB_INTR_RESUME 0x02
#define MUSB_INTR_RESET 0x04
#define MUSB_INTR_BABBLE 0x04
#define MUSB_INTR_SOF 0x08
#define MUSB_INTR_CONNECT 0x10
#define MUSB_INTR_DISCONNECT 0x20
#define MUSB_INTR_SESSREQ 0x40
#define MUSB_INTR_VBUSERROR 0x80 /* For SESSION end */
/* DEVCTL */
#define MUSB_DEVCTL_BDEVICE 0x80
#define MUSB_DEVCTL_FSDEV 0x40
#define MUSB_DEVCTL_LSDEV 0x20
#define MUSB_DEVCTL_VBUS 0x18
#define MUSB_DEVCTL_VBUS_SHIFT 3
#define MUSB_DEVCTL_HM 0x04
#define MUSB_DEVCTL_HR 0x02
#define MUSB_DEVCTL_SESSION 0x01
/* MUSB ULPI VBUSCONTROL */
#define MUSB_ULPI_USE_EXTVBUS 0x01
#define MUSB_ULPI_USE_EXTVBUSIND 0x02
/* ULPI_REG_CONTROL */
#define MUSB_ULPI_REG_REQ (1 << 0)
#define MUSB_ULPI_REG_CMPLT (1 << 1)
#define MUSB_ULPI_RDN_WR (1 << 2)
/* TESTMODE */
#define MUSB_TEST_FORCE_HOST 0x80
#define MUSB_TEST_FIFO_ACCESS 0x40
#define MUSB_TEST_FORCE_FS 0x20
#define MUSB_TEST_FORCE_HS 0x10
#define MUSB_TEST_PACKET 0x08
#define MUSB_TEST_K 0x04
#define MUSB_TEST_J 0x02
#define MUSB_TEST_SE0_NAK 0x01
/* TXTYPE */
#define MUSB_TXTYPE_TXSPEED 0xC0
#define MUSB_TXTYPE_TXSPEED_FULL 0x80
/* Allocate for double-packet buffering (effectively doubles assigned _SIZE) */
#define MUSB_FIFOSZ_DPB 0x10
/* Allocation size (8, 16, 32, ... 4096) */
#define MUSB_FIFOSZ_SIZE 0x0f
/* CSR0 */
#define MUSB_CSR0_FLUSHFIFO 0x0100
#define MUSB_CSR0_TXPKTRDY 0x0002
#define MUSB_CSR0_RXPKTRDY 0x0001
/* CSR0 in Peripheral mode */
#define MUSB_CSR0_P_SVDSETUPEND 0x0080
#define MUSB_CSR0_P_SVDRXPKTRDY 0x0040
#define MUSB_CSR0_P_SENDSTALL 0x0020
#define MUSB_CSR0_P_SETUPEND 0x0010
#define MUSB_CSR0_P_DATAEND 0x0008
#define MUSB_CSR0_P_SENTSTALL 0x0004
/* CSR0 in Host mode */
#define MUSB_CSR0_H_DIS_PING 0x0800
#define MUSB_CSR0_H_WR_DATATOGGLE 0x0400 /* Set to allow setting: */
#define MUSB_CSR0_H_DATATOGGLE 0x0200 /* Data toggle control */
#define MUSB_CSR0_H_NAKTIMEOUT 0x0080
#define MUSB_CSR0_H_STATUSPKT 0x0040
#define MUSB_CSR0_H_REQPKT 0x0020
#define MUSB_CSR0_H_ERROR 0x0010
#define MUSB_CSR0_H_SETUPPKT 0x0008
#define MUSB_CSR0_H_RXSTALL 0x0004
/* CSR0 bits to avoid zeroing (write zero clears, write 1 ignored) */
#define MUSB_CSR0_P_WZC_BITS \
(MUSB_CSR0_P_SENTSTALL)
#define MUSB_CSR0_H_WZC_BITS \
(MUSB_CSR0_H_NAKTIMEOUT | MUSB_CSR0_H_RXSTALL \
| MUSB_CSR0_RXPKTRDY)
/* TxType/RxType */
#define MUSB_TYPE_SPEED 0xc0
#define MUSB_TYPE_SPEED_SHIFT 6
#define MUSB_TYPE_PROTO 0x30 /* Implicitly zero for ep0 */
#define MUSB_TYPE_PROTO_SHIFT 4
#define MUSB_TYPE_REMOTE_END 0xf /* Implicitly zero for ep0 */
/* CONFIGDATA */
#define MUSB_CONFIGDATA_MPRXE 0x80 /* Auto bulk pkt combining */
#define MUSB_CONFIGDATA_MPTXE 0x40 /* Auto bulk pkt splitting */
#define MUSB_CONFIGDATA_BIGENDIAN 0x20
#define MUSB_CONFIGDATA_HBRXE 0x10 /* HB-ISO for RX */
#define MUSB_CONFIGDATA_HBTXE 0x08 /* HB-ISO for TX */
#define MUSB_CONFIGDATA_DYNFIFO 0x04 /* Dynamic FIFO sizing */
#define MUSB_CONFIGDATA_SOFTCONE 0x02 /* SoftConnect */
#define MUSB_CONFIGDATA_UTMIDW 0x01 /* Data width 0/1 => 8/16bits */
/* TXCSR in Peripheral and Host mode */
#define MUSB_TXCSR_AUTOSET 0x8000
#define MUSB_TXCSR_DMAENAB 0x1000
#define MUSB_TXCSR_FRCDATATOG 0x0800
#define MUSB_TXCSR_DMAMODE 0x0400
#define MUSB_TXCSR_CLRDATATOG 0x0040
#define MUSB_TXCSR_FLUSHFIFO 0x0008
#define MUSB_TXCSR_FIFONOTEMPTY 0x0002
#define MUSB_TXCSR_TXPKTRDY 0x0001
/* TXCSR in Peripheral mode */
#define MUSB_TXCSR_P_ISO 0x4000
#define MUSB_TXCSR_P_INCOMPTX 0x0080
#define MUSB_TXCSR_P_SENTSTALL 0x0020
#define MUSB_TXCSR_P_SENDSTALL 0x0010
#define MUSB_TXCSR_P_UNDERRUN 0x0004
/* TXCSR in Host mode */
#define MUSB_TXCSR_H_WR_DATATOGGLE 0x0200
#define MUSB_TXCSR_H_DATATOGGLE 0x0100
#define MUSB_TXCSR_H_NAKTIMEOUT 0x0080
#define MUSB_TXCSR_H_RXSTALL 0x0020
#define MUSB_TXCSR_H_ERROR 0x0004
/* TXCSR bits to avoid zeroing (write zero clears, write 1 ignored) */
#define MUSB_TXCSR_P_WZC_BITS \
(MUSB_TXCSR_P_INCOMPTX | MUSB_TXCSR_P_SENTSTALL \
| MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_FIFONOTEMPTY)
#define MUSB_TXCSR_H_WZC_BITS \
(MUSB_TXCSR_H_NAKTIMEOUT | MUSB_TXCSR_H_RXSTALL \
| MUSB_TXCSR_H_ERROR | MUSB_TXCSR_FIFONOTEMPTY)
/* RXCSR in Peripheral and Host mode */
#define MUSB_RXCSR_AUTOCLEAR 0x8000
#define MUSB_RXCSR_DMAENAB 0x2000
#define MUSB_RXCSR_DISNYET 0x1000
#define MUSB_RXCSR_PID_ERR 0x1000
#define MUSB_RXCSR_DMAMODE 0x0800
#define MUSB_RXCSR_INCOMPRX 0x0100
#define MUSB_RXCSR_CLRDATATOG 0x0080
#define MUSB_RXCSR_FLUSHFIFO 0x0010
#define MUSB_RXCSR_DATAERROR 0x0008
#define MUSB_RXCSR_FIFOFULL 0x0002
#define MUSB_RXCSR_RXPKTRDY 0x0001
/* ALPS00798316, Enable DMA RxMode1 */
#define MUSB_EP_RXPKTCOUNT 0x0300
/* ALPS00798316, Enable DMA RxMode1 */
/* RXCSR in Peripheral mode */
#define MUSB_RXCSR_P_ISO 0x4000
#define MUSB_RXCSR_P_SENTSTALL 0x0040
#define MUSB_RXCSR_P_SENDSTALL 0x0020
#define MUSB_RXCSR_P_OVERRUN 0x0004
/* RXCSR in Host mode */
#define MUSB_RXCSR_H_AUTOREQ 0x4000
#define MUSB_RXCSR_H_WR_DATATOGGLE 0x0400
#define MUSB_RXCSR_H_DATATOGGLE 0x0200
#define MUSB_RXCSR_H_RXSTALL 0x0040
#define MUSB_RXCSR_H_REQPKT 0x0020
#define MUSB_RXCSR_H_ERROR 0x0004
/* RXCSR bits to avoid zeroing (write zero clears, write 1 ignored) */
#define MUSB_RXCSR_P_WZC_BITS \
(MUSB_RXCSR_P_SENTSTALL | MUSB_RXCSR_P_OVERRUN \
| MUSB_RXCSR_RXPKTRDY)
#define MUSB_RXCSR_H_WZC_BITS \
(MUSB_RXCSR_H_RXSTALL | MUSB_RXCSR_H_ERROR \
| MUSB_RXCSR_DATAERROR | MUSB_RXCSR_RXPKTRDY)
/* HUBADDR */
#define MUSB_HUBADDR_MULTI_TT 0x80
/*
* Common USB registers
*/
#define MUSB_FADDR 0x00 /* 8-bit */
#define MUSB_POWER 0x01 /* 8-bit */
#define MUSB_INTRTX 0x02 /* 16-bit */
#define MUSB_INTRRX 0x04
#define MUSB_INTRTXE 0x06
#define MUSB_INTRRXE 0x08
#define MUSB_INTRUSB 0x0A /* 8 bit */
#define MUSB_INTRUSBE 0x0B /* 8 bit */
#define MUSB_FRAME 0x0C
#define MUSB_INDEX 0x0E /* 8 bit */
#define MUSB_TESTMODE 0x0F /* 8 bit */
#define MUSB_TXTYPE_EP0 0x1A /* 8 bit */
#define MUSB_FIFO_OFFSET(epnum) (0x20 + ((epnum) * 4))
/*
* Additional Control Registers
*/
#define MUSB_DEVCTL 0x60 /* 8 bit */
#define MUSB_OPSTATE 0x620
#define MUSB_OPSTATE_HOST_WAIT_DEV 0x21
#define OTG_IDLE 0
/*
* MD Direct Tethering related Registers
*/
#define MUSB_USB_MDL1INTM 0x744
#define MUSB_QIMCR 0xc08
#define MUSB_QIMSR 0xc0c
#define MUSB_USBGCSR 0xb00
/* These are always controlled through the INDEX register */
#define MUSB_TXFIFOSZ 0x62 /* 8-bit (see masks) */
#define MUSB_RXFIFOSZ 0x63 /* 8-bit (see masks) */
#define MUSB_TXFIFOADD 0x64 /* 16-bit offset shifted right 3 */
#define MUSB_RXFIFOADD 0x66 /* 16-bit offset shifted right 3 */
/* REVISIT: vctrl/vstatus: optional vendor utmi+phy register at 0x68 */
#define MUSB_HWVERS 0x6C /* 8 bit */
#define MUSB_ULPI_BUSCONTROL 0x70 /* 8 bit */
#define MUSB_ULPI_INT_MASK 0x72 /* 8 bit */
#define MUSB_ULPI_INT_SRC 0x73 /* 8 bit */
#define MUSB_ULPI_REG_DATA 0x74 /* 8 bit */
#define MUSB_ULPI_REG_ADDR 0x75 /* 8 bit */
#define MUSB_ULPI_REG_CONTROL 0x76 /* 8 bit */
#define MUSB_ULPI_RAW_DATA 0x77 /* 8 bit */
#define MUSB_EPINFO 0x78 /* 8 bit */
#define MUSB_RAMINFO 0x79 /* 8 bit */
#define MUSB_LINKINFO 0x7a /* 8 bit */
#define MUSB_VPLEN 0x7b /* 8 bit */
#define MUSB_HS_EOF1 0x7c /* 8 bit */
#define MUSB_FS_EOF1 0x7d /* 8 bit */
#define MUSB_LS_EOF1 0x7e /* 8 bit */
/* Offsets to endpoint registers */
#define MUSB_TXMAXP 0x00
#define MUSB_TXCSR 0x02
#define MUSB_CSR0 MUSB_TXCSR /* Re-used for EP0 */
#define MUSB_RXMAXP 0x04
#define MUSB_RXCSR 0x06
#define MUSB_RXCOUNT 0x08
#define MUSB_COUNT0 MUSB_RXCOUNT /* Re-used for EP0 */
#define MUSB_TXTYPE 0x0A
#define MUSB_TYPE0 MUSB_TXTYPE /* Re-used for EP0 */
#define MUSB_TXINTERVAL 0x0B
#define MUSB_NAKLIMIT0 MUSB_TXINTERVAL /* Re-used for EP0 */
#define MUSB_RXTYPE 0x0C
#define MUSB_RXINTERVAL 0x0D
#define MUSB_FIFOSIZE 0x0F
#define MUSB_CONFIGDATA MUSB_FIFOSIZE /* Re-used for EP0 */
/* Offsets to endpoint registers in indexed model (using INDEX register) */
#define MUSB_INDEXED_OFFSET(_epnum, _offset) \
(0x10 + (_offset))
#define MUSB_TXCSR_MODE 0x2000
/* "bus control"/target registers, for host side multipoint (external hubs) */
#define MUSB_TXFUNCADDR 0x0480
#define MUSB_TXHUBADDR 0x0482
#define MUSB_RXFUNCADDR 0x0484
#define MUSB_RXHUBADDR 0x0486
/* Toggle registers */
#define MUSB_RXTOG 0x0080
#define MUSB_RXTOGEN 0x0082
#define MUSB_TXTOG 0x0084
#define MUSB_TXTOGEN 0x0086
#define MUSB_BUSCTL_OFFSET(_epnum, _offset) \
(0x80 + (8*(_epnum)) + (_offset))
/* MTK Software reset reg */
#define MUSB_SWRST 0x74
#define MUSB_SWRST_PHY_RST (1<<7)
#define MUSB_SWRST_PHYSIG_GATE_HS (1<<6)
#define MUSB_SWRST_PHYSIG_GATE_EN (1<<5)
#define MUSB_SWRST_REDUCE_DLY (1<<4)
#define MUSB_SWRST_UNDO_SRPFIX (1<<3)
#define MUSB_SWRST_FRC_VBUSVALID (1<<2)
#define MUSB_SWRST_SWRST (1<<1)
#define MUSB_SWRST_DISUSBRESET (1<<0)
#define USB_L1INTS (0x00a0) /* USB level 1 interrupt status register */
#define USB_L1INTM (0x00a4) /* USB level 1 interrupt mask register */
#define USB_L1INTP (0x00a8) /* USB level 1 interrupt polarity register */
/* #define DMA_INTR (USB_BASE + 0x0200) */
#define DMA_INTR_UNMASK_CLR_OFFSET (16)
#define DMA_INTR_UNMASK_SET_OFFSET (24)
#define USB_DMA_REALCOUNT(chan) (0x0280+0x10*(chan))
/* ====================== */
/* USB interrupt register */
/* ====================== */
/* word access */
#define TX_INT_STATUS (1<<0)
#define RX_INT_STATUS (1<<1)
#define USBCOM_INT_STATUS (1<<2)
#define DMA_INT_STATUS (1<<3)
#define PSR_INT_STATUS (1<<4)
#define QINT_STATUS (1<<5)
#define QHIF_INT_STATUS (1<<6)
#define DPDM_INT_STATUS (1<<7)
#define VBUSVALID_INT_STATUS (1<<8)
#define IDDIG_INT_STATUS (1<<9)
#define DRVVBUS_INT_STATUS (1<<10)
#define VBUSVALID_INT_POL (1<<8)
#define IDDIG_INT_POL (1<<9)
#define DRVVBUS_INT_POL (1<<10)
#define RESREG 0x700 /* Reserved Register */
#define HSTPWRDWN_OPT (1<<0) /* connection detection option */
/*
* OTG 2.0 Registers
*/
#define OTG20_CSRL 0x730 /* OTG20 Related Control Register L */
#define OTG20_CSRH 0x731 /* OTG20 Related Control Register H */
/* OTG20 Related Control Register L */
/* Disable Host mode entering
* C_OPM_HSUS state before entering suspend
*/
#define DIS_HSUS (1<<7)
/* EN: FS idle of A device will
* transfer to HFS_HSUS state first
*/
#define A_HFS_WHNP (1<<6)
/* Disables B device entering
* C_OPM_B_WTDIS states before
* switching to host mode
*/
#define DIS_B_WTDIS (1<<5)
/* EN: host-hs-suspend entering OPM_FS_WTCON state first
* while receiving disconnect signal
*/
#define HHS_SUSP_DIS (1<<4)
/* EN: Disables B device
* charging VBUS function
* for OTG2.0 feature
*/
#define DIS_CHARGE_VBUS (1<<3)
/* EN: hsus mode of host initializing resuming interrupt
* while receiving resume K as waiting for HNP
*/
#define HSUS_RESUME_INT (1<<2)
/* EN: hnpsus-mode of host entering host-normal mode as
* receiving resume K while waiting for HNP
*/
#define HSUS_RESUME (1<<1)
#define OTG20_EN (1<<0) /* Enables OTG 2.0 feature */
/* OTG20 Related Control Register H */
/* Informs whether HW sends bus reset automatically
* while B-device changes to host with HNP
*/
#define DIS_AUTORST (1<<1)
/* EN: to decrease A
* device connection
* denounce waiting timing
*/
#define CON_DEB_SHORT (1<<0)
/* QMU Registers */
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
#define MUSB_QMUBASE (0x800)
#define MUSB_QISAR (0xc00)
#define MUSB_QIMR (0xc04)
#define MUSB_GPZCR (0xc34)
#endif
static inline void musb_write_txfifosz(void __iomem *mbase, u8 c_size)
{
musb_writeb(mbase, MUSB_TXFIFOSZ, c_size);
}
static inline void musb_write_txfifoadd(void __iomem *mbase, u16 c_off)
{
musb_writew(mbase, MUSB_TXFIFOADD, c_off);
}
static inline void musb_write_rxfifosz(void __iomem *mbase, u8 c_size)
{
musb_writeb(mbase, MUSB_RXFIFOSZ, c_size);
}
static inline void musb_write_rxfifoadd(void __iomem *mbase, u16 c_off)
{
musb_writew(mbase, MUSB_RXFIFOADD, c_off);
}
static inline void musb_write_ulpi_buscontrol(void __iomem *mbase, u8 val)
{
musb_writeb(mbase, MUSB_ULPI_BUSCONTROL, val);
}
static inline u8 musb_read_txfifosz(void __iomem *mbase)
{
return musb_readb(mbase, MUSB_TXFIFOSZ);
}
static inline u16 musb_read_txfifoadd(void __iomem *mbase)
{
return musb_readw(mbase, MUSB_TXFIFOADD);
}
static inline u8 musb_read_rxfifosz(void __iomem *mbase)
{
return musb_readb(mbase, MUSB_RXFIFOSZ);
}
static inline u16 musb_read_rxfifoadd(void __iomem *mbase)
{
return musb_readw(mbase, MUSB_RXFIFOADD);
}
static inline u8 musb_read_ulpi_buscontrol(void __iomem *mbase)
{
return musb_readb(mbase, MUSB_ULPI_BUSCONTROL);
}
static inline u8 musb_read_configdata(void __iomem *mbase)
{
musb_writeb(mbase, MUSB_INDEX, 0);
return musb_readb(mbase, 0x10 + MUSB_CONFIGDATA);
}
static inline u16 musb_read_hwvers(void __iomem *mbase)
{
return musb_readw(mbase, MUSB_HWVERS);
}
static inline void musb_write_rxfunaddr(void __iomem *mbase,
u8 epnum, u8 qh_addr_reg)
{
musb_writew(mbase, MUSB_RXFUNCADDR + 8 * epnum, qh_addr_reg);
}
static inline void musb_write_rxhubaddr(void __iomem *mbase,
u8 epnum, u8 qh_h_addr_reg)
{
u16 rx_hub_port_addr = musb_readw(mbase, 0x0486 + 8 * epnum);
rx_hub_port_addr &= 0xff00;
rx_hub_port_addr |= qh_h_addr_reg;
musb_writew(mbase, MUSB_RXHUBADDR + 8 * epnum, rx_hub_port_addr);
}
static inline void musb_write_rxhubport(void __iomem *mbase,
u8 epnum, u8 qh_h_port_reg)
{
u16 rx_hub_port_addr = musb_readw(mbase, 0x0486 + 8 * epnum);
u16 rx_port_addr = (u16) qh_h_port_reg;
rx_hub_port_addr &= 0x00ff;
rx_hub_port_addr |= (rx_port_addr << 8);
musb_writew(mbase, MUSB_RXHUBADDR + 8 * epnum, rx_hub_port_addr);
}
static inline void musb_write_txfunaddr(void __iomem *mbase,
u8 epnum, u8 qh_addr_reg)
{
u16 new_qh_addr_reg;
unsigned char power, txtype;
power = musb_readb(mbase, MUSB_POWER);
txtype = musb_readb(mbase, MUSB_TXTYPE_EP0);
DBG(4, "%s - ep%d, power: 0x%X, txtype: 0x%X\n", __func__, epnum,
power, txtype);
if ((power & MUSB_POWER_HSMODE) &&
((txtype & MUSB_TXTYPE_TXSPEED) == MUSB_TXTYPE_TXSPEED_FULL)) {
new_qh_addr_reg = (qh_addr_reg | 0x100);
DBG(4, "%s SPLIT TRANS ep%d 0x%X 0x%X\n", __func__, epnum,
qh_addr_reg,
new_qh_addr_reg);
musb_writew(mbase, MUSB_TXFUNCADDR + 8 * epnum,
new_qh_addr_reg);
} else {
musb_writew(mbase, MUSB_TXFUNCADDR + 8 * epnum, qh_addr_reg);
}
}
static inline void musb_write_txhubaddr(void __iomem *mbase,
u8 epnum, u8 qh_h_addr_reg)
{
u16 tx_hub_port_addr = musb_readw(mbase, 0x0482 + 8 * epnum);
tx_hub_port_addr &= 0xff00;
tx_hub_port_addr |= qh_h_addr_reg;
musb_writew(mbase, MUSB_TXHUBADDR + 8 * epnum, tx_hub_port_addr);
}
static inline void musb_write_txhubport(void __iomem *mbase,
u8 epnum, u8 qh_h_port_reg)
{
u16 tx_hub_port_addr = musb_readw(mbase, 0x0482 + 8 * epnum);
u16 tx_port_addr = (u16) qh_h_port_reg;
tx_hub_port_addr &= 0x00ff;
tx_hub_port_addr |= (tx_port_addr << 8);
musb_writew(mbase, MUSB_TXHUBADDR + 8 * epnum, tx_hub_port_addr);
}
#endif /* __MUSB_REGS_H__ */
File diff suppressed because it is too large. Load diff
+514
View File
@@ -0,0 +1,514 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef _MTK_QMU_H_
#define _MTK_QMU_H_
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
/* for musb_read/write api */
#include "mtk_musb.h"
#include "musb_debug.h"
#include "musb_io.h"
#include <linux/dmapool.h>
/* CUSTOM SETTING */
#define GPD_LEN_ALIGNED (64) /* > gpd len (16) and cache line size aligned */
/* GPD_LEN_ALIGNED -
* 16(should be sizeof(TGPD)
*/
#define GPD_EXT_LEN (48)
#define GPD_SZ (16)
#define DFT_MAX_GPD_NUM 144
#ifndef MUSB_QMU_LIMIT_SUPPORT
#define RXQ_NUM 8
#define TXQ_NUM 8
#else
#define RXQ_NUM MUSB_QMU_LIMIT_RXQ_NUM
#define TXQ_NUM MUSB_QMU_LIMIT_TXQ_NUM
#endif
#define MAX_QMU_EP RXQ_NUM
#define TXQ 0
#define RXQ 1
#define ISOC_EP_START_IDX 1
/* QMU SETTING */
#define NO_ZLP 0
#define HW_MODE 1
#define GPD_MODE 2
#define TXZLP GPD_MODE
/* #define TXZLP HW_MODE */
/*#define TXZLP NO_ZLP */
/* #define CFG_RX_ZLP_EN */
#define CFG_RX_COZ_EN
#define CFG_CS_CHECK
/* #define CFG_EMPTY_CHECK */
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
struct tx_haddr {
u8 hiaddr;
u8 reserved;
};
struct rx_haddr {
u8 hiaddr;
};
union gpd_b14 {
u8 ExtLength; /*Tx ExtLength for TXGPD*/
struct rx_haddr rx_haddr; /*Rx hi address for RXGPD*/
};
union gpd_w1 {
u16 DataBufferLen; /*Rx Allow Length for RXGPD*/
struct tx_haddr tx_haddr; /*Tx hi address for TXGPD */
};
#endif
/* TGPD */
struct TGPD {
u8 flag;
u8 chksum;
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
union gpd_w1 gpd_w1;
#else
u16 DataBufferLen; /*Rx Allow Length */
#endif
/* address field, 32-bit long */
u32 pNext;
u32 pBuf;
u16 bufLen;
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
union gpd_b14 gpd_b14;
#else
u8 ExtLength;
#endif
u8 ZTepFlag;
};
struct _GPD_RANGE {
struct TGPD *pNext;
struct TGPD *pStart;
struct TGPD *pEnd;
};
extern int mtk_host_qmu_concurrent;
extern int mtk_host_qmu_pipe_msk;
extern int mtk_host_qmu_force_isoc_restart;
extern int mtk_host_active_dev_cnt;
#ifdef CONFIG_MTK_UAC_POWER_SAVING
extern unsigned int low_power_timer_total_trigger_cnt;
extern unsigned int low_power_timer_total_wake_cnt;
extern int low_power_timer_mode2_option;
extern int low_power_timer_mode;
extern int usb_on_sram;
extern int audio_on_sram;
extern int use_mtk_audio;
extern int mtk_audio_request_sram
(dma_addr_t *phys_addr, unsigned char **virt_addr,
unsigned int length, void *user);
extern void mtk_audio_free_sram(void *user);
extern int gpd_switch_to_sram(struct device *dev);
extern void gpd_switch_to_dram(struct device *dev);
#endif
extern int mtk_qmu_dbg_level; /* refer to musb_core.c */
extern int mtk_qmu_max_gpd_num;
extern int isoc_ep_end_idx;
extern int isoc_ep_gpd_count;
static inline int mtk_dbg_level(unsigned int level)
{
return mtk_qmu_dbg_level >= level;
}
#define LOG_EMERG 0
#define LOG_ALERT 1
#define LOG_CRIT 2
#define LOG_ERR 3
#define LOG_WARN 4
#define LOG_NOTICE 5
#define LOG_INFO 6
#define LOG_DBG 7
#define QMU_DBG_ON
#ifdef QMU_DBG_ON
#define QMU_ERR(format, args...) do {if (mtk_dbg_level(LOG_ERR)) \
pr_notice("QMU_ERR,<%s %d>, " format, __func__, __LINE__, ## args); } \
while (0)
#define QMU_WARN(format, args...) do {if (mtk_dbg_level(LOG_WARN)) \
pr_notice("QMU_WARN,<%s %d>, " format, __func__, __LINE__, ## args); } \
while (0)
#define QMU_INFO(format, args...) do {if (mtk_dbg_level(LOG_INFO)) \
pr_notice("QMU_INFO,<%s %d>, " format, __func__, __LINE__, ## args); } \
while (0)
#define QMU_DBG(format, args...) do {if (mtk_dbg_level(LOG_DBG)) \
pr_notice("QMU_DBG,<%s %d>, " format, __func__, __LINE__, ## args); } \
while (0)
#else
#define QMU_ERR(format, args...) do {} while (0)
#define QMU_WARN(format, args...) do {} while (0)
#define QMU_INFO(format, args...) do {} while (0)
#define QMU_DBG(format, args...) do {} while (0)
#endif
/* QMU macros */
#define USB_HW_QMU_OFF 0x0000
#define USB_HW_QUCS_OFF 0x0300
#define USB_HW_QIRQ_OFF 0x0400
#define USB_HW_QDBG_OFF 0x04F0
#define MGC_O_QMU_QCR0 0x0000
#define MGC_O_QMU_QCR2 0x0008
#define MGC_O_QMU_QCR3 0x000C
#define MGC_O_QMU_RQCSR0 0x0010
#define MGC_O_QMU_RQSAR0 0x0014
#define MGC_O_QMU_RQCPR0 0x0018
#define MGC_O_QMU_RQCSR(n) (MGC_O_QMU_RQCSR0+0x0010*((n)-1))
#define MGC_O_QMU_RQSAR(n) (MGC_O_QMU_RQSAR0+0x0010*((n)-1))
#define MGC_O_QMU_RQCPR(n) (MGC_O_QMU_RQCPR0+0x0010*((n)-1))
#define MGC_O_QMU_RQTR_BASE 0x0090
#define MGC_O_QMU_RQTR(n) (MGC_O_QMU_RQTR_BASE+0x4*((n)-1))
#define MGC_O_QMU_RQLDPR0 0x0100
#define MGC_O_QMU_RQLDPR(n) (MGC_O_QMU_RQLDPR0+0x4*((n)-1))
#define MGC_O_QMU_TQCSR0 0x0200
#define MGC_O_QMU_TQSAR0 0x0204
#define MGC_O_QMU_TQCPR0 0x0208
#define MGC_O_QMU_TQCSR(n) (MGC_O_QMU_TQCSR0+0x0010*((n)-1))
#define MGC_O_QMU_TQSAR(n) (MGC_O_QMU_TQSAR0+0x0010*((n)-1))
#define MGC_O_QMU_TQCPR(n) (MGC_O_QMU_TQCPR0+0x0010*((n)-1))
#define MGC_O_QMU_QAR 0x0300
#define MGC_O_QUCS_USBGCSR 0x0000
#define MGC_O_QIRQ_QISAR 0x0000
#define MGC_O_QIRQ_QIMR 0x0004
#define MGC_O_QIRQ_QIMCR 0x0008
#define MGC_O_QIRQ_QIMSR 0x000C
#define MGC_O_QIRQ_IOCDISR 0x0030
#define MGC_O_QIRQ_TEPEMPR 0x0060
#define MGC_O_QIRQ_TEPEMPMR 0x0064
#define MGC_O_QIRQ_TEPEMPMCR 0x0068
#define MGC_O_QIRQ_TEPEMPMSR 0x006C
#define MGC_O_QIRQ_REPEMPR 0x0070
#define MGC_O_QIRQ_REPEMPMR 0x0074
#define MGC_O_QIRQ_REPEMPMCR 0x0078
#define MGC_O_QIRQ_REPEMPMSR 0x007C
#define MGC_O_QIRQ_RQEIR 0x0090
#define MGC_O_QIRQ_RQEIMR 0x0094
#define MGC_O_QIRQ_RQEIMCR 0x0098
#define MGC_O_QIRQ_RQEIMSR 0x009C
#define MGC_O_QIRQ_REPEIR 0x00A0
#define MGC_O_QIRQ_REPEIMR 0x00A4
#define MGC_O_QIRQ_REPEIMCR 0x00A8
#define MGC_O_QIRQ_REPEIMSR 0x00AC
#define MGC_O_QIRQ_TQEIR 0x00B0
#define MGC_O_QIRQ_TQEIMR 0x00B4
#define MGC_O_QIRQ_TQEIMCR 0x00B8
#define MGC_O_QIRQ_TQEIMSR 0x00BC
#define MGC_O_QIRQ_TEPEIR 0x00C0
#define MGC_O_QIRQ_TEPEIMR 0x00C4
#define MGC_O_QIRQ_TEPEIMCR 0x00C8
#define MGC_O_QIRQ_TEPEIMSR 0x00CC
#define MGC_O_QDBG_DFCR 0x0000
#define MGC_O_QDBG_DFMR 0x0004
/* brief Queue Control value Definition */
#define DQMU_QUE_START 0x00000001
#define DQMU_QUE_RESUME 0x00000002
#define DQMU_QUE_STOP 0x00000004
#define DQMU_QUE_ACTIVE 0x00008000
/*brief USB QMU Special Control USBGCSR value Definition*/
#define USB_QMU_Tx0_EN 0x00000001
#define USB_QMU_Tx_EN(n) (USB_QMU_Tx0_EN<<((n)-1))
#define USB_QMU_Rx0_EN 0x00010000
#define USB_QMU_Rx_EN(n) (USB_QMU_Rx0_EN<<((n)-1))
#define USB_QMU_HIFEVT_EN 0x00000100
#define USB_QMU_HIFCMD_EN 0x01000000
#define DQMU_SW_RESET 0x00010000
#define DQMU_CS16B_EN 0x80000000
#define DQMU_TQ0CS_EN 0x00010000
#define DQMU_TQCS_EN(n) (DQMU_TQ0CS_EN<<((n)-1))
#define DQMU_RQ0CS_EN 0x00000001
#define DQMU_RQCS_EN(n) (DQMU_RQ0CS_EN<<((n)-1))
#define DQMU_TX0_ZLP 0x01000000
#define DQMU_TX_ZLP(n) (DQMU_TX0_ZLP<<((n)-1))
#define DQMU_TX0_MULTIPLE 0x00010000
#define DQMU_TX_MULTIPLE(n) (DQMU_TX0_MULTIPLE<<((n)-1))
#define DQMU_T0Q_GDP_ZLP 0x00000100
#define DQMU_TQ_GDP_ZLP(n) (DQMU_T0Q_GDP_ZLP<<((n)-1))
#define DQMU_RX0_MULTIPLE 0x00010000
#define DQMU_RX_MULTIPLE(n) (DQMU_RX0_MULTIPLE<<((n)-1))
#define DQMU_RX0_ZLP 0x01000000
#define DQMU_RX_ZLP(n) (DQMU_RX0_ZLP<<((n)-1))
#define DQMU_RX0_COZ 0x00000100
#define DQMU_RX_COZ(n) (DQMU_RX0_COZ<<((n)-1))
#define DQMU_M_TXEP_ERR 0x10000000
#define DQMU_M_TXQ_ERR 0x08000000
#define DQMU_M_RXEP_ERR 0x04000000
#define DQMU_M_RXQ_ERR 0x02000000
#define DQMU_M_RQ_EMPTY 0x00020000
#define DQMU_M_TQ_EMPTY 0x00010000
#define DQMU_M_RX0_EMPTY 0x00000001
#define DQMU_M_RX_EMPTY(n) (DQMU_M_RX0_EMPTY<<((n)-1))
#define DQMU_M_TX0_EMPTY 0x00000001
#define DQMU_M_TX_EMPTY(n) (DQMU_M_TX0_EMPTY<<((n)-1))
#define DQMU_M_RX0_DONE 0x00000100
#define DQMU_M_RX_DONE(n) (DQMU_M_RX0_DONE<<((n)-1))
#define DQMU_M_TX0_DONE 0x00000001
#define DQMU_M_TX_DONE(n) (DQMU_M_TX0_DONE<<((n)-1))
#define DQMU_M_RX0_ZLP_ERR 0x01000000
#define DQMU_M_RX_ZLP_ERR(n) (DQMU_M_RX0_ZLP_ERR<<((n)-1))
#define DQMU_M_RX0_LEN_ERR 0x00000100
#define DQMU_M_RX_LEN_ERR(n) (DQMU_M_RX0_LEN_ERR<<((n)-1))
#define DQMU_M_RX0_GPDCS_ERR 0x00000001
#define DQMU_M_RX_GPDCS_ERR(n) (DQMU_M_RX0_GPDCS_ERR<<((n)-1))
#define DQMU_M_TX0_LEN_ERR 0x00010000
#define DQMU_M_TX_LEN_ERR(n) (DQMU_M_TX0_LEN_ERR<<((n)-1))
#define DQMU_M_TX0_GPDCS_ERR 0x00000100
#define DQMU_M_TX_GPDCS_ERR(n) (DQMU_M_TX0_GPDCS_ERR<<((n)-1))
#define DQMU_M_TX0_BDCS_ERR 0x00000001
#define DQMU_M_TX_BDCS_ERR(n) (DQMU_M_TX0_BDCS_ERR<<((n)-1))
#define DQMU_M_TX0_EP_ERR 0x00000001
#define DQMU_M_TX_EP_ERR(n) (DQMU_M_TX0_EP_ERR<<((n)-1))
#define DQMU_M_RX0_EP_ERR 0x00000001
#define DQMU_M_RX_EP_ERR(n) (DQMU_M_RX0_EP_ERR<<((n)-1))
#define DQMU_M_RQ_DIS_IOC(n) (0x100<<((n)-1))
#define MGC_ReadQMU8(base, _offset) \
musb_readb(base, (USB_HW_QMU_OFF + _offset))
#define MGC_ReadQUCS8(base, _offset) \
musb_readb(base, (USB_HW_QUCS_OFF + _offset))
#define MGC_ReadQIRQ8(base, _offset) \
musb_readb(base, (USB_HW_QIRQ_OFF + _offset))
#define MGC_ReadQMU16(base, _offset) \
musb_readw(base, (USB_HW_QMU_OFF + _offset))
#define MGC_ReadQUCS16(base, _offset) \
musb_readw(base, (USB_HW_QUCS_OFF + _offset))
#define MGC_ReadQIRQ16(base, _offset) \
musb_readw(base, (USB_HW_QIRQ_OFF + _offset))
#define MGC_ReadQMU32(base, _offset) \
musb_readl(base, (USB_HW_QMU_OFF + _offset))
#define MGC_ReadQUCS32(base, _offset) \
musb_readl(base, (USB_HW_QUCS_OFF + _offset))
#define MGC_ReadQIRQ32(base, _offset) \
musb_readl(base, (USB_HW_QIRQ_OFF + _offset))
#define MGC_WriteQMU32(base, _offset, _data) \
musb_writel(base, (USB_HW_QMU_OFF + _offset), _data)
#define MGC_WriteQUCS32(base, _offset, _data) \
musb_writel(base, (USB_HW_QUCS_OFF + _offset), _data)
#define MGC_WriteQIRQ32(base, _offset, _data) \
musb_writel(base, (USB_HW_QIRQ_OFF + _offset), _data)
u8 PDU_calcCksum(u8 *data, int len);
/* brief Define DMAQ GPD format */
#define TGPD_FLAGS_HWO 0x01
#define TGPD_IS_FLAGS_HWO(_pd) \
(((struct TGPD *)_pd)->flag & TGPD_FLAGS_HWO)
#define TGPD_SET_FLAGS_HWO(_pd) \
(((struct TGPD *)_pd)->flag |= TGPD_FLAGS_HWO)
#define TGPD_CLR_FLAGS_HWO(_pd) \
(((struct TGPD *)_pd)->flag &= (~TGPD_FLAGS_HWO))
#define TGPD_FORMAT_BDP 0x02
#define TGPD_IS_FORMAT_BDP(_pd) \
(((struct TGPD *)_pd)->flag & TGPD_FORMAT_BDP)
#define TGPD_SET_FORMAT_BDP(_pd) \
(((struct TGPD *)_pd)->flag |= TGPD_FORMAT_BDP)
#define TGPD_CLR_FORMAT_BDP(_pd) \
(((struct TGPD *)_pd)->flag &= (~TGPD_FORMAT_BDP))
#define TGPD_SET_FLAG(_pd, _flag) \
(((struct TGPD *)_pd)->flag = \
(((struct TGPD *)_pd)->flag&(~TGPD_FLAGS_HWO))|(_flag))
#define TGPD_GET_FLAG(_pd) \
(((struct TGPD *)_pd)->flag & TGPD_FLAGS_HWO)
#define TGPD_SET_CHKSUM(_pd, _n) \
(((struct TGPD *)_pd)->chksum = PDU_calcCksum((u8 *)_pd, _n))
#define TGPD_SET_CHKSUM_HWO(_pd, _n) \
(((struct TGPD *)_pd)->chksum = PDU_calcCksum((u8 *)_pd, _n)-1)
#define TGPD_GET_CHKSUM(_pd) (((struct TGPD *)_pd)->chksum)
#define TGPD_SET_FORMAT(_pd, _fmt) \
(((struct TGPD *)_pd)->flag =\
(((struct TGPD *)_pd)->flag&(~TGPD_FORMAT_BDP))|(_fmt))
#define TGPD_GET_FORMAT(_pd) \
(((((struct TGPD *)_pd)->flag & TGPD_FORMAT_BDP)>>1))
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
#define TGPD_SET_DataBUF_LEN(_pd, _len) \
(((struct TGPD *)_pd)->gpd_w1.DataBufferLen = _len)
#define TGPD_ADD_DataBUF_LEN(_pd, _len) \
(((struct TGPD *)_pd)->gpd_w1.DataBufferLen += _len)
#define TGPD_GET_DataBUF_LEN(_pd) \
(((struct TGPD *)_pd)->gpd_w1.DataBufferLen)
#else
#define TGPD_SET_DataBUF_LEN(_pd, _len) \
(((struct TGPD *)_pd)->DataBufferLen = _len)
#define TGPD_ADD_DataBUF_LEN(_pd, _len) \
(((struct TGPD *)_pd)->DataBufferLen += _len)
#define TGPD_GET_DataBUF_LEN(_pd) (((struct TGPD *)_pd)->DataBufferLen)
#endif
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
#define TGPD_SET_NEXT(_pd, _next) \
(((struct TGPD *)_pd)->pNext = (u32)_next)
#define TGPD_SET_NEXT_TXHI(_pd, _next) \
do { \
((struct TGPD *) _pd)->gpd_w1.tx_haddr.hiaddr &= 0x0F; \
((struct TGPD *) _pd)->gpd_w1.tx_haddr.hiaddr |=\
((u8)_next << 4); \
} while (0)
#define TGPD_SET_NEXT_RXHI(_pd, _next) \
do { \
((struct TGPD *) _pd)->gpd_b14.rx_haddr.hiaddr &= 0x0F; \
((struct TGPD *) _pd)->gpd_b14.rx_haddr.hiaddr |=\
((u8)_next << 4); \
} while (0)
#define TGPD_GET_NEXT(_pd) \
((uintptr_t)((struct TGPD *)_pd)->pNext)
#define TGPD_GET_NEXT_TXHI(_pd) \
((uintptr_t)((struct TGPD *)_pd)->gpd_w1.tx_haddr.hiaddr >> 4)
#define TGPD_GET_NEXT_RXHI(_pd) \
((uintptr_t)((struct TGPD *)_pd)->gpd_b14.rx_haddr.hiaddr >> 4)
#define TGPD_GET_NEXT_TX(_pd) \
((struct TGPD *)(TGPD_GET_NEXT(_pd) | (TGPD_GET_NEXT_TXHI(_pd) << 32)))
#define TGPD_GET_NEXT_RX(_pd) \
((struct TGPD *)(TGPD_GET_NEXT(_pd) | (TGPD_GET_NEXT_RXHI(_pd) << 32)))
#define TGPD_SET_DATA(_pd, _data) \
(((struct TGPD *)_pd)->pBuf = (u32)_data)
#define TGPD_SET_DATA_TXHI(_pd, _next) \
do { \
((struct TGPD *)_pd)->gpd_w1.tx_haddr.hiaddr &= 0xF0; \
((struct TGPD *)_pd)->gpd_w1.tx_haddr.hiaddr |=\
((u8)_next & 0x0F); \
} while (0)
#define TGPD_SET_DATA_RXHI(_pd, _next) \
do { \
((struct TGPD *)_pd)->gpd_b14.rx_haddr.hiaddr &= 0xF0; \
((struct TGPD *)_pd)->gpd_b14.rx_haddr.hiaddr |=\
((u8)_next & 0x0F); \
} while (0)
#define TGPD_GET_DATA(_pd) ((uintptr_t)((struct TGPD *)_pd)->pBuf)
#define TGPD_GET_DATA_TXHI(_pd) \
((uintptr_t)((struct TGPD *)_pd)->gpd_w1.tx_haddr.hiaddr & 0x0F)
#define TGPD_GET_DATA_RXHI(_pd) \
((uintptr_t)((struct TGPD *)_pd)->gpd_b14.rx_haddr.hiaddr & 0x0F)
#define TGPD_GET_DATA_TX(_pd) \
((struct TGPD *)(TGPD_GET_DATA(_pd) | (TGPD_GET_DATA_TXHI(_pd) << 32)))
#define TGPD_GET_DATA_RX(_pd) \
((struct TGPD *)(TGPD_GET_DATA(_pd) | (TGPD_GET_DATA_RXHI(_pd) << 32)))
#else
#define TGPD_GET_NEXT_TX(_pd) TGPD_GET_NEXT(_pd)
#define TGPD_GET_NEXT_RX(_pd) TGPD_GET_NEXT(_pd)
#define TGPD_SET_NEXT(_pd, _next) \
(((struct TGPD *)_pd)->pNext = (u32)(uintptr_t)((struct TGPD *)_next))
#define TGPD_GET_NEXT(_pd) \
((struct TGPD *)(uintptr_t)((struct TGPD *)_pd)->pNext)
#define TGPD_GET_DATA_TX(_pd) TGPD_GET_DATA(_pd)
#define TGPD_GET_DATA_RX(_pd) TGPD_GET_DATA(_pd)
#define TGPD_SET_DATA(_pd, _data) \
(((struct TGPD *)_pd)->pBuf = (u32)(uintptr_t)_data)
#define TGPD_GET_DATA(_pd) \
((u8 *)(uintptr_t)((struct TGPD *)_pd)->pBuf)
#endif
#define TGPD_SET_BUF_LEN(_pd, _len) (((struct TGPD *)_pd)->bufLen = _len)
#define TGPD_ADD_BUF_LEN(_pd, _len) (((struct TGPD *)_pd)->bufLen += _len)
#define TGPD_GET_BUF_LEN(_pd) (((struct TGPD *)_pd)->bufLen)
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
#define TGPD_SET_EXT_LEN(_pd, _len) \
(((struct TGPD *)_pd)->gpd_b14.ExtLength = _len)
#define TGPD_GET_EXT_LEN(_pd) \
(((struct TGPD *)_pd)->gpd_b14.ExtLength)
#else
#define TGPD_SET_EXT_LEN(_pd, _len) (((struct TGPD *)_pd)->ExtLength = _len)
#define TGPD_GET_EXT_LEN(_pd) (((struct TGPD *)_pd)->ExtLength)
#endif
#define TGPD_SET_EPaddr(_pd, _EP) (((struct TGPD *)_pd)->ZTepFlag = \
(((struct TGPD *)_pd)->ZTepFlag&0xF0)|(_EP))
#define TGPD_GET_EPaddr(_pd) (((struct TGPD *)_pd)->ZTepFlag & 0x0F)
#define TGPD_FORMAT_TGL 0x10
#define TGPD_IS_FORMAT_TGL(_pd) \
((((struct TGPD *)_pd)->ZTepFlag & TGPD_FORMAT_TGL))
#define TGPD_SET_FORMAT_TGL(_pd) \
((((struct TGPD *)_pd)->ZTepFlag |= TGPD_FORMAT_TGL))
#define TGPD_CLR_FORMAT_TGL(_pd) \
((((struct TGPD *)_pd)->ZTepFlag &= (~TGPD_FORMAT_TGL)))
#define TGPD_FORMAT_ZLP 0x20
#define TGPD_IS_FORMAT_ZLP(_pd) \
((((struct TGPD *)_pd)->ZTepFlag & TGPD_FORMAT_ZLP))
#define TGPD_SET_FORMAT_ZLP(_pd) \
((((struct TGPD *)_pd)->ZTepFlag |= TGPD_FORMAT_ZLP))
#define TGPD_CLR_FORMAT_ZLP(_pd) \
((((struct TGPD *)_pd)->ZTepFlag &= (~TGPD_FORMAT_ZLP)))
#define TGPD_SET_TGL(_pd, _TGL) \
(((struct TGPD *)_pd)->ZTepFlag |= ((_TGL) ? 0x10 : 0x00))
#define TGPD_GET_TGL(_pd) (((struct TGPD *)_pd)->ZTepFlag & 0x10 ? 1:0)
#define TGPD_SET_ZLP(_pd, _ZLP) \
(((struct TGPD *)_pd)->ZTepFlag |= ((_ZLP) ? 0x20 : 0x00))
#define TGPD_GET_ZLP(_pd) (((struct TGPD *)_pd)->ZTepFlag & 0x20 ? 1:0)
#define TGPD_FLAG_IOC 0x80
#define TGPD_SET_IOC(_pd) (((struct TGPD *)_pd)->flag |= TGPD_FLAG_IOC)
#define TGPD_CLR_IOC(_pd) (((struct TGPD *)_pd)->flag &= (~TGPD_FLAG_IOC))
extern void qmu_destroy_gpd_pool(struct device *dev);
extern int qmu_init_gpd_pool(struct device *dev);
extern void qmu_reset_gpd_pool(u32 ep_num, u8 isRx);
extern bool mtk_is_qmu_enabled(u8 EP_Num, u8 isRx);
extern void mtk_qmu_enable(struct musb *musb, u8 EP_Num, u8 isRx);
extern void mtk_qmu_insert_task(u8 EP_Num, u8 isRx,
dma_addr_t buf, u32 length, u8 zlp, u8 isioc);
extern void mtk_qmu_resume(u8 EP_Num, u8 isRx);
extern void qmu_done_rx(struct musb *musb, u8 ep_num);
extern void qmu_done_tx(struct musb *musb, u8 ep_num);
extern void mtk_disable_q(struct musb *musb, u8 ep_num, u8 isRx);
extern void mtk_qmu_irq_err(struct musb *musb, u32 qisar);
extern void flush_ep_csr(struct musb *musb, u8 ep_num, u8 isRx);
extern void mtk_qmu_stop(u8 ep_num, u8 isRx);
#define QMU_RX_SPLIT_BLOCK_SIZE (32*1024)
#define QMU_RX_SPLIT_THRE (64*1024)
extern u32 qmu_used_gpd_count(u8 isRx, u32 num);
extern u32 qmu_free_gpd_count(u8 isRx, u32 num);
extern void h_qmu_done_rx(struct musb *musb, u8 ep_num);
extern void h_qmu_done_tx(struct musb *musb, u8 ep_num);
#endif
#endif
+192
View File
@@ -0,0 +1,192 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __LINUX_USB_MUSB_H
#define __LINUX_USB_MUSB_H
#include <linux/extcon.h>
#include <linux/usb/otg.h>
#include <linux/usb/role.h>
/* The USB role is defined by the connector used on the board, so long as
* standards are being followed. (Developer boards sometimes won't.)
*/
enum musb_mode {
MUSB_UNDEFINED = 0,
MUSB_HOST, /* A or Mini-A connector */
MUSB_PERIPHERAL, /* B or Mini-B connector */
MUSB_OTG /* Mini-AB connector */
};
enum musb_fifo_style {
FIFO_RXTX,
FIFO_TX,
FIFO_RX
} __attribute__ ((packed));
enum musb_buf_mode {
BUF_SINGLE,
BUF_DOUBLE
} __attribute__ ((packed));
enum musb_ep_mode {
EP_CONT,
EP_INT,
EP_BULK,
EP_ISO
} __packed;
struct musb_fifo_cfg {
u8 hw_ep_num;
enum musb_fifo_style style;
enum musb_buf_mode mode;
u16 maxpacket;
enum musb_ep_mode ep_mode;
};
#define MUSB_EP_FIFO(ep, st, m, pkt) \
{ \
.hw_ep_num = ep, \
.style = st, \
.mode = m, \
.maxpacket = pkt, \
}
#define MUSB_EP_FIFO_SINGLE(ep, st, pkt) \
MUSB_EP_FIFO(ep, st, BUF_SINGLE, pkt)
#define MUSB_EP_FIFO_DOUBLE(ep, st, pkt) \
MUSB_EP_FIFO(ep, st, BUF_DOUBLE, pkt)
struct musb_hdrc_eps_bits {
const char name[16];
u8 bits;
};
struct musb_hdrc_config {
struct musb_fifo_cfg *fifo_cfg; /* board fifo configuration */
unsigned int fifo_cfg_size;/* size of the fifo configuration */
/* MUSB configuration-specific details */
unsigned multipoint:1; /* multipoint device */
unsigned dyn_fifo:1 __deprecated;/* supports dynamic fifo sizing */
unsigned soft_con:1 __deprecated;/* soft connect required */
unsigned utm_16:1 __deprecated; /* utm data witdh is 16 bits */
unsigned big_endian:1; /* true if CPU uses big-endian */
unsigned mult_bulk_tx:1; /* Tx ep required for multbulk pkts */
unsigned mult_bulk_rx:1; /* Rx ep required for multbulk pkts */
unsigned high_iso_tx:1; /* Tx ep required for HB iso */
unsigned high_iso_rx:1; /* Rx ep required for HD iso */
unsigned dma:1 __deprecated; /* supports DMA */
unsigned vendor_req:1 __deprecated; /* vendor registers required */
u8 num_eps; /* number of endpoints _with_ ep0 */
u8 dma_channels __deprecated; /* number of dma channels */
u8 dyn_fifo_size; /* dynamic size in bytes */
u8 vendor_ctrl __deprecated; /* vendor control reg width */
u8 vendor_stat __deprecated; /* vendor status reg witdh */
u8 dma_req_chan __deprecated; /* bitmask for required dma channels */
u8 ram_bits; /* ram address size */
struct musb_hdrc_eps_bits *eps_bits __deprecated;
};
struct musb_hdrc_platform_data {
/* MUSB_HOST, MUSB_PERIPHERAL, or MUSB_OTG */
u8 dr_mode;
/* for clk_get() */
const char *clock;
/* (HOST or OTG) switch VBUS on/off */
int (*set_vbus)(struct device *dev, int is_on);
/* (HOST or OTG) mA/2 power supplied on (default = 8mA) */
u8 power;
/* (PERIPHERAL) mA/2 max power consumed (default = 100mA) */
u8 min_power;
/* (HOST or OTG) msec/2 after VBUS on till power good */
u8 potpgt;
/* (HOST or OTG) program PHY for external Vbus */
unsigned extvbus:1;
/* Power the device on or off */
int (*set_power)(int state);
/* MUSB configuration-specific details */
struct musb_hdrc_config *config;
/* Architecture specific board data */
void *board_data;
/* Platform specific struct musb_ops pointer */
const void *platform_ops;
};
/**
* MUSB_DR_FORCE_NONE: automatically switch host and periperal mode
* by IDPIN signal.
* MUSB_DR_FORCE_HOST: force to enter host mode and override OTG
* IDPIN signal.
* MUSB_DR_FORCE_DEVICE: force to enter peripheral mode.
*/
enum mt_usb_dr_force_mode {
MUSB_DR_FORCE_NONE = 0,
MUSB_DR_FORCE_HOST,
MUSB_DR_FORCE_DEVICE,
};
/**
* MUSB_DR_OPERATION_NONE: force to tun off usb
* MUSB_DR_OPERATION_NORMAL: automatically switch host and
* periperal mode by usb role switch.
* MUSB_DR_OPERATION_HOST: force to enter host mode.
* MUSB_DR_OPERATION_DEVICE: force to enter peripheral mode.
*/
enum mt_usb_dr_operation_mode {
MUSB_DR_OPERATION_NONE = 0,
MUSB_DR_OPERATION_NORMAL,
MUSB_DR_OPERATION_HOST,
MUSB_DR_OPERATION_DEVICE,
};
/**
* @vbus: vbus 5V used by host mode
* @edev: external connector used to detect vbus and iddig changes
* @vbus_nb: notifier for vbus detection
* @vbus_work : work of vbus detection notifier, used to avoid sleep in
* notifier callback which is atomic context
* @vbus_event : event of vbus detecion notifier
* @id_nb : notifier for iddig(idpin) detection
* @id_work : work of iddig detection notifier
* @id_event : event of iddig detecion notifier
* @role_sw : use USB Role Switch to support dual-role switch, can't use
* extcon at the same time, and extcon is deprecated.
* @role_sw_used : true when the USB Role Switch is used.
* @manual_drd_enabled: it's true when supports dual-role device by debugfs
* to switch host/device modes depending on user input.
*/
struct otg_switch_mtk {
struct regulator *vbus;
struct extcon_dev *edev;
struct notifier_block vbus_nb;
struct work_struct vbus_work;
unsigned long vbus_event;
struct notifier_block id_nb;
struct work_struct id_work;
unsigned long id_event;
struct usb_role_switch *role_sw;
bool role_sw_used;
bool manual_drd_enabled;
u32 sw_state;
enum usb_role latest_role;
enum mt_usb_dr_operation_mode op_mode;
};
#endif /* __LINUX_USB_MUSB_H */
File diff suppressed because it is too large. Load diff
+678
View File
@@ -0,0 +1,678 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MUSB_CORE_H__
#define __MUSB_CORE_H__
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/device.h>
#include <linux/usb/ch9.h>
#include <linux/usb/gadget.h>
#include <linux/usb.h>
#include <linux/usb/otg.h>
#include <linux/usb/role.h>
#include <musb.h>
#include <musb_dr.h>
#include <linux/pm_wakeup.h>
#include <linux/version.h>
#include <linux/clk.h>
#if defined(CONFIG_MTK_CHARGER)
extern enum charger_type mt_get_charger_type(void);
#endif
/* to prevent 32 bit project misuse */
#if defined(CONFIG_MTK_MUSB_DRV_36BIT) && !defined(CONFIG_64BIT)
#error
#endif
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
#include "mtk_qmu.h"
#endif
#define SHARE_IRQ -1
struct musb;
struct musb_hw_ep;
struct musb_ep;
extern int musb_fake_CDP;
extern int musb_force_on;
extern int musb_host_dynamic_fifo;
extern int musb_host_dynamic_fifo_usage_msk;
extern unsigned int musb_uart_debug;
extern bool musb_host_db_enable;
extern bool musb_host_db_workaround1;
extern bool musb_host_db_workaround2;
extern long musb_host_db_delay_ns;
extern long musb_host_db_workaround_cnt;
extern int mtk_host_audio_free_ep_udelay;
extern bool mtk_usb_power;
extern ktime_t ktime_ready;
extern int ep_config_from_table_for_host(struct musb *musb);
extern int polling_vbus_value(void *data);
#if defined(CONFIG_USBIF_COMPLIANCE)
extern bool polling_vbus;
extern struct task_struct *vbus_polling_tsk;
extern void musb_set_host_request_flag(struct musb *musb, unsigned int value);
extern void pmic_bvalid_det_int_en(int i);
#define CONFIG_USBIF_COMPLIANCE_PMIC
#if defined(CONFIG_USBIF_COMPLIANCE_PMIC)
extern int PMIC_IMM_GetOneChannelValue(int dwChannel, int deCount, int trimd);
#define MT6328_AUX_VCDT_AP 2 /* include/mach/upmu_sw.h */
#define AUX_VCDT_AP MT6328_AUX_VCDT_AP /* include/mach/upmu_sw.h */
#define R_CHARGER_1 330
#define R_CHARGER_2 39
#else
extern signed_int battery_meter_get_charger_voltage(void);
#endif
extern void send_otg_event(enum usb_otg_event event);
#endif
extern void musb_bug(void);
/* Helper defines for struct musb->hwvers */
#define MUSB_HWVERS_MAJOR(x) ((x >> 10) & 0x1f)
#define MUSB_HWVERS_MINOR(x) (x & 0x3ff)
#define MUSB_HWVERS_RC 0x8000
#define MUSB_HWVERS_1300 0x52C
#define MUSB_HWVERS_1400 0x590
#define MUSB_HWVERS_1800 0x720
#define MUSB_HWVERS_1900 0x784
#define MUSB_HWVERS_2000 0x800
#define MUSB_HUB_SUPPORT 0x4000
#include "mtk_musb.h"
#include "musb_debug.h"
#include "musb_dma.h"
#include "musb_io.h"
#include "mtk_musb_reg.h"
#include "musb_gadget.h"
#include <linux/usb/hcd.h>
#include "musb_host.h"
/* NOTE: otg and peripheral-only state machines start at B_IDLE.
* OTG or host-only go to A_IDLE when ID is sensed.
*/
#define is_peripheral_active(m) (!(m)->is_host)
#define is_host_active(m) ((m)->is_host)
#ifdef CONFIG_PROC_FS
#include <linux/fs.h>
#define MUSB_CONFIG_PROC_FS
#endif
/****************************** PERIPHERAL ROLE *****************************/
extern irqreturn_t musb_g_ep0_irq(struct musb *musb);
extern void musb_g_tx(struct musb *musb, u8 epnum);
extern void musb_g_rx(struct musb *musb, u8 epnum);
extern void musb_g_reset(struct musb *musb);
extern void musb_g_suspend(struct musb *musb);
extern void musb_g_resume(struct musb *musb);
extern void musb_g_wakeup(struct musb *musb);
extern void musb_g_disconnect(struct musb *musb);
/****************************** HOST ROLE ***********************************/
extern irqreturn_t musb_h_ep0_irq(struct musb *musb);
extern void musb_host_tx(struct musb *musb, u8 epnum);
extern void musb_host_rx(struct musb *musb, u8 epnum);
/****************************** CONSTANTS ********************************/
#ifdef CONFIG_MTK_MUSB_CARPLAY_SUPPORT
extern bool apple;
extern void musb_id_pin_work_device(void);
extern void musb_id_pin_work_host(struct work_struct *data);
extern int send_switch_cmd(void);
#endif
#ifndef MUSB_C_NUM_EPS
#define MUSB_C_NUM_EPS ((u8)16)
#endif
#ifndef MUSB_MAX_END0_PACKET
#define MUSB_MAX_END0_PACKET ((u16)MUSB_EP0_FIFOSIZE)
#endif
/* host side ep0 states */
enum musb_h_ep0_state {
MUSB_EP0_IDLE,
MUSB_EP0_START, /* expect ack of setup */
MUSB_EP0_IN, /* expect IN DATA */
MUSB_EP0_OUT, /* expect ack of OUT DATA */
MUSB_EP0_STATUS, /* expect ack of STATUS */
} __packed;
/* peripheral side ep0 states */
enum musb_g_ep0_state {
MUSB_EP0_STAGE_IDLE, /* idle, waiting for SETUP */
MUSB_EP0_STAGE_SETUP, /* received SETUP */
MUSB_EP0_STAGE_TX, /* IN data */
MUSB_EP0_STAGE_RX, /* OUT data */
MUSB_EP0_STAGE_STATUSIN, /* (after OUT data) */
MUSB_EP0_STAGE_STATUSOUT, /* (after IN data) */
MUSB_EP0_STAGE_ACKWAIT, /* after zlp, before statusin */
} __packed;
/*
* OTG protocol constants. See USB OTG 1.3 spec,
* sections 5.5 "Device Timings" and 6.6.5 "Timers".
*/
#define OTG_TIME_A_WAIT_VRISE 100 /* msec (max) */
/* when switch host to device within min 1 second, the otg state can't*/
/* switch to b-idle successfully, then connect to host and can't run */
/* gadget rest in BUS_RESET, so need to decrease min 1 second to 600ms*/
#define OTG_TIME_A_WAIT_BCON 600 /* min 1 second */
#define OTG_TIME_A_AIDL_BDIS 200 /* min 200 msec */
#if defined(CONFIG_USBIF_COMPLIANCE)
#define OTG_TIME_B_ASE0_BRST 155 /* min 3.125 ms */
#else
#define OTG_TIME_B_ASE0_BRST 100 /* min 3.125 ms */
#endif
/*************************** REGISTER ACCESS ********************************/
#define musb_ep_select(_mbase, _epnum) \
musb_writeb((_mbase), MUSB_INDEX, (_epnum))
#define MUSB_EP_OFFSET MUSB_INDEXED_OFFSET
/****************************** FUNCTIONS ********************************/
#define MUSB_HST_MODE(_musb)\
{ (_musb)->is_host = true; }
#define MUSB_DEV_MODE(_musb) \
{ (_musb)->is_host = false; }
#define test_devctl_hst_mode(_x) \
(musb_readb((_x)->mregs, MUSB_DEVCTL)&MUSB_DEVCTL_HM)
#define MUSB_MODE(musb) ((musb)->is_host ? "Host" : "Peripheral")
enum writeFunc_enum {
funcWriteb = 0,
funcWritew,
funcWritel,
funcInterrupt
};
void dumpTime(enum writeFunc_enum func, int epnum);
/******************************** TYPES *************************************/
/**
* struct musb_platform_ops - Operations passed to musb_core by HW glue layer
* @init: turns on clocks, sets up platform-specific registers, etc
* @exit: undoes @init
* @set_mode: forcefully changes operating mode
* @try_ilde: tries to idle the IP
* @vbus_status: returns vbus status if possible
* @set_vbus: forces vbus status
* @adjust_channel_params: pre check for standard dma channel_program func
*/
struct musb_platform_ops {
int (*init)(struct musb *musb);
int (*exit)(struct musb *musb);
void (*enable)(struct musb *musb);
void (*disable)(struct musb *musb);
int (*set_mode)(struct musb *musb, u8 mode);
void (*try_idle)(struct musb *musb, unsigned long timeout);
int (*vbus_status)(struct musb *musb);
void (*set_vbus)(struct musb *musb, int on);
int (*adjust_channel_params)(struct dma_channel *channel,
u16 packet_sz, u8 *mode, dma_addr_t *dma_addr, u32 *len);
void (*enable_clk)(struct musb *musb);
void (*disable_clk)(struct musb *musb);
void (*prepare_clk)(struct musb *musb);
void (*unprepare_clk)(struct musb *musb);
void (*enable_wakeup)(struct musb *musb, bool enable);
};
/*
* struct musb_hw_ep - endpoint hardware (bidirectional)
*
* Ordered slightly for better cacheline locality.
*/
struct musb_hw_ep {
struct musb *musb;
void __iomem *fifo;
void __iomem *regs;
/* index in musb->endpoints[] */
u8 epnum;
enum musb_ep_mode ep_mode;
/* hardware configuration, possibly dynamic */
bool is_shared_fifo;
bool tx_double_buffered;
bool rx_double_buffered;
u16 max_packet_sz_tx;
u16 max_packet_sz_rx;
struct dma_channel *tx_channel;
struct dma_channel *rx_channel;
void __iomem *target_regs;
/* currently scheduled peripheral endpoint */
struct musb_qh *in_qh;
struct musb_qh *out_qh;
u8 rx_reinit;
u8 tx_reinit;
/* peripheral side */
struct musb_ep ep_in; /* TX */
struct musb_ep ep_out; /* RX */
};
static inline struct musb_request *next_in_request(struct musb_hw_ep *hw_ep)
{
return next_request(&hw_ep->ep_in);
}
static inline struct musb_request *next_out_request(struct musb_hw_ep *hw_ep)
{
return next_request(&hw_ep->ep_out);
}
struct musb_csr_regs {
/* FIFO registers */
u16 txmaxp, txcsr, rxmaxp, rxcsr;
u16 rxfifoadd, txfifoadd;
u8 txtype, txinterval, rxtype, rxinterval;
u8 rxfifosz, txfifosz;
u8 txfunaddr, txhubaddr, txhubport;
u8 rxfunaddr, rxhubaddr, rxhubport;
};
struct musb_context_registers {
u8 power;
u8 intrusbe;
u16 frame;
u8 index, testmode;
u8 devctl, busctl, misc;
u32 otg_interfsel;
u32 l1_int;
struct musb_csr_regs index_regs[MUSB_C_NUM_EPS];
};
/*
* struct musb - Driver instance data.
*/
struct musb {
struct semaphore musb_lock;
/* device lock */
spinlock_t lock;
const struct musb_platform_ops *ops;
struct musb_context_registers context;
irqreturn_t (*isr)(int irq, void *private_data);
struct work_struct irq_work;
struct work_struct otg_notifier_work;
u16 hwvers;
struct delayed_work id_pin_work;
#ifdef CONFIG_MTK_MUSB_CARPLAY_SUPPORT
struct delayed_work carplay_work;
#endif
struct musb_fifo_cfg *fifo_cfg;
unsigned int fifo_cfg_size;
struct musb_fifo_cfg *fifo_cfg_host;
unsigned int fifo_cfg_host_size;
u32 fifo_size;
u16 intrrxe;
u16 intrtxe;
/* this hub status bit is reserved by USB 2.0 and not seen by usbcore */
#define MUSB_PORT_STAT_RESUME (1 << 31)
u32 port1_status;
unsigned long rh_timer;
enum musb_h_ep0_state ep0_stage;
/* bulk traffic normally dedicates endpoint hardware, and each
* direction has its own ring of host side endpoints.
* we try to progress the transfer at the head of each endpoint's
* queue until it completes or NAKs too much; then we try the next
* endpoint.
*/
struct musb_hw_ep *bulk_ep;
struct list_head control; /* of musb_qh */
struct list_head in_bulk; /* of musb_qh */
struct list_head out_bulk; /* of musb_qh */
struct timer_list otg_timer;
struct timer_list idle_timer;
#if defined(CONFIG_USBIF_COMPLIANCE)
struct timer_list otg_vbus_polling_timer;
#endif
struct notifier_block nb;
struct dma_controller *dma_controller;
struct device *controller;
void __iomem *ctrl_base;
void __iomem *mregs;
/* passed down from chip/board specific irq handlers */
u8 int_usb;
u16 int_rx;
u16 int_tx;
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
u32 int_queue;
#endif
struct usb_phy *xceiv;
struct phy *phy;
u8 xceiv_event;
int nIrq;
int dma_irq;
unsigned irq_wake:1;
struct musb_hw_ep endpoints[MUSB_C_NUM_EPS];
#define control_ep endpoints
#define VBUSERR_RETRY_COUNT 3
u16 vbuserr_retry;
u16 epmask;
u8 nr_endpoints;
int (*board_set_power)(int state);
u8 min_power; /* vbus for periph, in mA/2 */
bool is_host;
bool in_ipo_off;
int a_wait_bcon; /* VBUS timeout in msecs */
unsigned long idle_timeout; /* Next timeout in jiffies */
/* active means connected and not suspended */
unsigned is_active:1;
unsigned is_multipoint:1;
unsigned ignore_disconnect:1; /* during bus resets */
unsigned hb_iso_rx:1; /* high bandwidth iso rx? */
unsigned hb_iso_tx:1; /* high bandwidth iso tx? */
unsigned dyn_fifo:1; /* dynamic FIFO supported? */
unsigned bulk_split:1;
#define can_bulk_split(musb, type) \
(((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_split)
unsigned bulk_combine:1;
#define can_bulk_combine(musb, type) \
(((type) == USB_ENDPOINT_XFER_BULK) && (musb)->bulk_combine)
/* is_suspended means USB B_PERIPHERAL suspend */
unsigned is_suspended:1;
/* may_wakeup means remote wakeup is enabled */
unsigned may_wakeup:1;
/* is_self_powered is reported in device status and the
* config descriptor. is_bus_powered means B_PERIPHERAL
* draws some VBUS current; both can be true.
*/
unsigned is_self_powered:1;
unsigned is_bus_powered:1;
unsigned set_address:1;
unsigned test_mode:1;
unsigned softconnect:1;
u8 address;
u8 test_mode_nr;
u16 ackpend; /* ep0 */
enum musb_g_ep0_state ep0_state;
struct usb_gadget g; /* the gadget */
struct usb_gadget_driver *gadget_driver; /* its driver */
struct wakeup_source *usb_lock;
/*
* FIXME: Remove this flag.
*
* This is only added to allow Blackfin to work
* with current driver. For some unknown reason
* Blackfin doesn't work with double buffering
* and that's enabled by default.
*
* We added this flag to forcefully disable double
* buffering until we get it working.
*/
unsigned double_buffer_not_ok:1;
struct musb_hdrc_config *config;
#ifdef MUSB_CONFIG_PROC_FS
struct proc_dir_entry *proc_entry;
#endif
int xceiv_old_state;
#ifdef CONFIG_DEBUG_FS
struct dentry *debugfs_root;
#endif
bool power;
unsigned is_ready:1;
bool usb_if;
u16 fifo_addr;
#if defined(CONFIG_USBIF_COMPLIANCE)
bool srp_drvvbus;
enum usb_otg_event otg_event;
#endif
struct workqueue_struct *st_wq;
struct power_supply *usb_psy;
struct notifier_block psy_nb;
#if defined(CONFIG_USB_ROLE_SWITCH)
struct otg_switch_mtk *otg_sx;
#endif
struct mt_usb_glue *glue;
/* host suspend */
bool host_suspend;
bool usb_connected;
};
static inline struct musb *gadget_to_musb(struct usb_gadget *g)
{
return container_of(g, struct musb, g);
}
static inline int musb_read_fifosize
(struct musb *musb, struct musb_hw_ep *hw_ep, u8 epnum)
{
void __iomem *mbase = musb->mregs;
u8 reg = 0;
/* read from core using indexed model */
reg = musb_readb(mbase, MUSB_EP_OFFSET(epnum, MUSB_FIFOSIZE));
/* 0's returned when no more endpoints */
if (!reg)
return -ENODEV;
musb->nr_endpoints++;
musb->epmask |= (1 << epnum);
hw_ep->max_packet_sz_tx = 1 << (reg & 0x0f);
/* shared TX/RX FIFO? */
if ((reg & 0xf0) == 0xf0) {
hw_ep->max_packet_sz_rx = hw_ep->max_packet_sz_tx;
hw_ep->is_shared_fifo = true;
return 0;
}
hw_ep->max_packet_sz_rx = 1 << ((reg & 0xf0) >> 4);
hw_ep->is_shared_fifo = false;
return 0;
}
static inline void musb_configure_ep0(struct musb *musb)
{
musb->endpoints[0].max_packet_sz_tx = MUSB_EP0_FIFOSIZE;
musb->endpoints[0].max_packet_sz_rx = MUSB_EP0_FIFOSIZE;
musb->endpoints[0].is_shared_fifo = true;
}
/***************************** Glue it together *****************************/
extern const char musb_driver_name[];
extern void musb_start(struct musb *musb);
extern void musb_stop(struct musb *musb);
extern int musb_get_id(struct device *dev, gfp_t gfp_mask);
extern void musb_put_id(struct device *dev, int id);
extern void musb_write_fifo(struct musb_hw_ep *ep, u16 len, const u8 *src);
extern void musb_read_fifo(struct musb_hw_ep *ep, u16 len, u8 *dst);
extern void musb_load_testpacket(struct musb *musb);
extern void musb_generic_disable(struct musb *musb);
extern irqreturn_t musb_interrupt(struct musb *musb);
extern irqreturn_t dma_controller_irq(int irq, void *private_data);
extern void musb_hnp_stop(struct musb *musb);
static inline void musb_platform_set_vbus(struct musb *musb, int is_on)
{
if (musb->ops->set_vbus)
musb->ops->set_vbus(musb, is_on);
}
static inline void musb_platform_enable(struct musb *musb)
{
if (musb->ops->enable)
musb->ops->enable(musb);
}
static inline void musb_platform_disable(struct musb *musb)
{
if (musb->ops->disable)
musb->ops->disable(musb);
}
static inline int musb_platform_set_mode(struct musb *musb, u8 mode)
{
if (!musb->ops->set_mode)
return 0;
return musb->ops->set_mode(musb, mode);
}
static inline void
musb_platform_try_idle(struct musb *musb, unsigned long timeout)
{
if (musb->ops->try_idle)
musb->ops->try_idle(musb, timeout);
}
static inline int musb_platform_get_vbus_status(struct musb *musb)
{
if (!musb->ops->vbus_status)
return 0;
return musb->ops->vbus_status(musb);
}
static inline int musb_platform_init(struct musb *musb)
{
if (!musb->ops->init)
return -EINVAL;
return musb->ops->init(musb);
}
static inline int musb_platform_exit(struct musb *musb)
{
if (!musb->ops->exit)
return -EINVAL;
return musb->ops->exit(musb);
}
static inline void musb_platform_enable_clk(struct musb *musb)
{
if (musb->ops->enable_clk)
musb->ops->enable_clk(musb);
}
static inline void musb_platform_disable_clk(struct musb *musb)
{
if (musb->ops->disable_clk)
musb->ops->disable_clk(musb);
}
static inline void musb_platform_prepare_clk(struct musb *musb)
{
if (musb->ops->prepare_clk)
musb->ops->prepare_clk(musb);
}
static inline void musb_platform_unprepare_clk(struct musb *musb)
{
if (musb->ops->unprepare_clk)
musb->ops->unprepare_clk(musb);
}
static inline void musb_platform_enable_wakeup(struct musb *musb)
{
if (musb->ops->enable_wakeup)
musb->ops->enable_wakeup(musb, true);
}
static inline void musb_platform_disable_wakeup(struct musb *musb)
{
if (musb->ops->enable_wakeup)
musb->ops->enable_wakeup(musb, false);
}
/* #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0) */
static inline const char *otg_state_string(enum usb_otg_state state)
{
return usb_otg_state_string(state);
}
enum {
USB_DPIDLE_ALLOWED = 0,
USB_DPIDLE_FORBIDDEN,
USB_DPIDLE_SRAM,
USB_DPIDLE_TIMER,
USB_DPIDLE_SUSPEND,
USB_DPIDLE_RESUME
};
extern void usb_hal_dpidle_request(int mode);
extern void register_usb_hal_dpidle_request(void (*function)(int));
extern void usb_hal_disconnect_check(void);
extern void register_usb_hal_disconnect_check(void (*function)(void));
extern void wake_up_bat(void);
extern void wait_tx_done(u8 epnum, unsigned int timeout_ns);
extern int host_tx_refcnt_inc(int epnum);
extern int host_tx_refcnt_dec(int epnum);
extern void host_tx_refcnt_reset(int epnum);
extern void dump_tx_ops(u8 ep_num);
#endif /* __MUSB_CORE_H__ */
+78
View File
@@ -0,0 +1,78 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MUSB_LINUX_DEBUG_H__
#define __MUSB_LINUX_DEBUG_H__
extern struct musb *musb;
#define yprintk(facility, format, args...) \
pr_notice("[MUSB]%s %d: " format, \
__func__, __LINE__, ## args)
/* workaroud for redefine warning in usb_dump.c */
#ifdef WARNING
#undef WARNING
#endif
#ifdef INFO
#undef INFO
#endif
#ifdef ERR
#undef ERR
#endif
#define WARNING(fmt, args...) yprintk(KERN_WARNING, fmt, ## args)
#define INFO(fmt, args...) yprintk(KERN_INFO, fmt, ## args)
#define ERR(fmt, args...) yprintk(KERN_ERR, fmt, ## args)
#define xprintk(level, format, args...) do { \
if (_dbg_level(level)) { \
if (musb_uart_debug) {\
pr_notice("[MUSB]%s %d: " format, \
__func__, __LINE__, ## args); \
} \
else {\
pr_debug("[MUSB]%s %d: " format, \
__func__, __LINE__, ## args); \
} \
} } while (0)
extern unsigned int musb_debug;
extern unsigned int musb_debug_limit;
extern unsigned int musb_uart_debug;
extern unsigned int musb_speed;
static inline int _dbg_level(unsigned int level)
{
return level <= musb_debug;
}
#ifdef DBG
#undef DBG
#endif
#define DBG(level, fmt, args...) xprintk(level, fmt, ## args)
#define DBG_LIMIT(FREQ, fmt, args...) do {\
static DEFINE_RATELIMIT_STATE(ratelimit, HZ, FREQ);\
static int skip_cnt;\
\
if (unlikely(!musb_debug_limit))\
DBG(0, fmt "<unlimit>\n", ## args);\
else { \
if (__ratelimit(&ratelimit)) {\
DBG(0, fmt ", skip_cnt<%d>\n", ## args, skip_cnt);\
skip_cnt = 0;\
} else\
skip_cnt++;\
} \
} while (0)\
/* extern const char *otg_state_string(struct musb *); */
extern int musb_init_debugfs(struct musb *musb) __attribute__((weak));
extern void musb_exit_debugfs(struct musb *musb) __attribute__((weak));
extern void musb_dr_debugfs_init(struct musb *musb);
#endif /* __MUSB_LINUX_DEBUG_H__ */
+776
View File
@@ -0,0 +1,776 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#ifdef CONFIG_MTK_MUSB_PHY
#include <usb20_phy.h>
#endif
#define MUSB_OTG_CSR0 0x102
#include <musb.h>
#include <musb_core.h>
#include <musb_debug.h>
#include <musb_dr.h>
#define PROC_DIR_MTK_USB "mtk_usb"
#define PROC_FILE_REGDUMP "mtk_usb/regdump"
#define PROC_FILE_TESTMODE "mtk_usb/testmode"
#define PROC_FILE_REGW "mtk_usb/regw"
#define PROC_FILE_REGR "mtk_usb/regr"
#define PROC_FILE_SPEED "mtk_usb/speed"
#define PROC_FILE_NUM 5
static struct proc_dir_entry *proc_files[PROC_FILE_NUM] = {
NULL, NULL, NULL, NULL, NULL};
#define PROC_FILE_MODE "mtk_usb/mode"
#define PROC_FILE_VBUS "mtk_usb/vbus"
#define PROC_FILE_DR_NUM 2
static struct proc_dir_entry *proc_dr_files[PROC_FILE_DR_NUM] = {
NULL, NULL};
struct musb_register_map {
char *name;
unsigned int offset;
unsigned int size;
};
static const struct musb_register_map musb_regmap[] = {
{"FAddr", 0x00, 8},
{"Power", 0x01, 8},
{"Frame", 0x0c, 16},
{"Index", 0x0e, 8},
{"Testmode", 0x0f, 8},
{"TxMaxPp", 0x10, 16},
{"TxCSRp", 0x12, 16},
{"RxMaxPp", 0x14, 16},
{"RxCSR", 0x16, 16},
{"RxCount", 0x18, 16},
{"ConfigData", 0x1f, 8},
{"DevCtl", 0x60, 8},
{"MISC", 0x61, 8},
{"TxFIFOsz", 0x62, 8},
{"RxFIFOsz", 0x63, 8},
{"TxFIFOadd", 0x64, 16},
{"RxFIFOadd", 0x66, 16},
{"VControl", 0x68, 32},
{"HWVers", 0x6C, 16},
{"EPInfo", 0x78, 8},
{"RAMInfo", 0x79, 8},
{"LinkInfo", 0x7A, 8},
{"VPLen", 0x7B, 8},
{"HS_EOF1", 0x7C, 8},
{"FS_EOF1", 0x7D, 8},
{"LS_EOF1", 0x7E, 8},
{"SOFT_RST", 0x7F, 8},
{"DMA_CNTLch0", 0x204, 16},
{"DMA_ADDRch0", 0x208, 32},
{"DMA_COUNTch0", 0x20C, 32},
{"DMA_CNTLch1", 0x214, 16},
{"DMA_ADDRch1", 0x218, 32},
{"DMA_COUNTch1", 0x21C, 32},
{"DMA_CNTLch2", 0x224, 16},
{"DMA_ADDRch2", 0x228, 32},
{"DMA_COUNTch2", 0x22C, 32},
{"DMA_CNTLch3", 0x234, 16},
{"DMA_ADDRch3", 0x238, 32},
{"DMA_COUNTch3", 0x23C, 32},
{"DMA_CNTLch4", 0x244, 16},
{"DMA_ADDRch4", 0x248, 32},
{"DMA_COUNTch4", 0x24C, 32},
{"DMA_CNTLch5", 0x254, 16},
{"DMA_ADDRch5", 0x258, 32},
{"DMA_COUNTch5", 0x25C, 32},
{"DMA_CNTLch6", 0x264, 16},
{"DMA_ADDRch6", 0x268, 32},
{"DMA_COUNTch6", 0x26C, 32},
{"DMA_CNTLch7", 0x274, 16},
{"DMA_ADDRch7", 0x278, 32},
{"DMA_COUNTch7", 0x27C, 32},
{} /* Terminating Entry */
};
static int musb_regdump_show(struct seq_file *s, void *unused)
{
struct musb *musb = s->private;
unsigned int i;
seq_puts(s, "MUSB (M)HDRC Register Dump\n");
for (i = 0; i < ARRAY_SIZE(musb_regmap); i++) {
switch (musb_regmap[i].size) {
case 8:
seq_printf(s, "%-12s: %02x\n", musb_regmap[i].name,
musb_readb(musb->mregs, musb_regmap[i].offset));
break;
case 16:
seq_printf(s, "%-12s: %04x\n", musb_regmap[i].name,
musb_readw(musb->mregs, musb_regmap[i].offset));
break;
case 32:
seq_printf(s, "%-12s: %08x\n", musb_regmap[i].name,
musb_readl(musb->mregs, musb_regmap[i].offset));
break;
}
}
return 0;
}
static int musb_regdump_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_regdump_show, PDE_DATA(inode));
}
static int musb_test_mode_show(struct seq_file *s, void *unused)
{
struct musb *musb = s->private;
unsigned int test;
test = musb_readb(musb->mregs, MUSB_TESTMODE);
if (test & MUSB_TEST_FORCE_HOST)
seq_puts(s, "force host\n");
if (test & MUSB_TEST_FIFO_ACCESS)
seq_puts(s, "fifo access\n");
if (test & MUSB_TEST_FORCE_FS)
seq_puts(s, "force full-speed\n");
if (test & MUSB_TEST_FORCE_HS)
seq_puts(s, "force high-speed\n");
if (test & MUSB_TEST_PACKET)
seq_puts(s, "test packet\n");
if (test & MUSB_TEST_K)
seq_puts(s, "test K\n");
if (test & MUSB_TEST_J)
seq_puts(s, "test J\n");
if (test & MUSB_TEST_SE0_NAK)
seq_puts(s, "test SE0 NAK\n");
return 0;
}
static const struct file_operations musb_regdump_fops = {
.open = musb_regdump_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int musb_test_mode_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_test_mode_show, PDE_DATA(inode));
}
void musbdebugfs_otg_write_fifo(u16 len, u8 *buf, struct musb *mtk_musb)
{
int i;
DBG(0, "musb_otg_write_fifo, len=%d\n", len);
for (i = 0; i < len; i++)
musb_writeb(mtk_musb->mregs, 0x20, *(buf + i));
}
void musbdebugfs_h_setup(struct usb_ctrlrequest *setup, struct musb *mtk_musb)
{
unsigned short csr0;
DBG(0, "musb_h_setup++\n");
musbdebugfs_otg_write_fifo
(sizeof(struct usb_ctrlrequest), (u8 *)setup, mtk_musb);
csr0 = musb_readw(mtk_musb->mregs, MUSB_OTG_CSR0);
DBG(0, "musb_h_setup,csr0=0x%x\n", csr0);
csr0 |= MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
musb_writew(mtk_musb->mregs, MUSB_OTG_CSR0, csr0);
DBG(0, "musb_h_setup--\n");
}
static ssize_t musb_test_mode_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
struct seq_file *s = file->private_data;
struct musb *musb = s->private;
u8 test = 0;
char buf[20];
unsigned char power;
struct usb_ctrlrequest setup_packet;
setup_packet.bRequestType =
USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
setup_packet.bRequest = USB_REQ_GET_DESCRIPTOR;
setup_packet.wIndex = 0;
setup_packet.wValue = 0x0100;
setup_packet.wLength = 0x40;
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (!strncmp(buf, "force host", 9))
test = MUSB_TEST_FORCE_HOST;
if (!strncmp(buf, "fifo access", 11))
test = MUSB_TEST_FIFO_ACCESS;
if (!strncmp(buf, "force full-speed", 15))
test = MUSB_TEST_FORCE_FS;
if (!strncmp(buf, "force high-speed", 15))
test = MUSB_TEST_FORCE_HS;
if (!strncmp(buf, "test packet", 10)) {
test = MUSB_TEST_PACKET;
musb_load_testpacket(musb);
}
if (!strncmp(buf, "test suspend_resume", 18)) {
DBG(0, "HS_HOST_PORT_SUSPEND_RESUME\n");
msleep(5000); /* the host must continue sending SOFs for 15s */
DBG(0, "please begin to trigger suspend!\n");
msleep(10000);
power = musb_readb(musb->mregs, MUSB_POWER);
power |= MUSB_POWER_SUSPENDM | MUSB_POWER_ENSUSPEND;
musb_writeb(musb->mregs, MUSB_POWER, power);
msleep(5000);
DBG(0, "please begin to trigger resume!\n");
msleep(10000);
power &= ~MUSB_POWER_SUSPENDM;
power |= MUSB_POWER_RESUME;
musb_writeb(musb->mregs, MUSB_POWER, power);
mdelay(25);
power &= ~MUSB_POWER_RESUME;
musb_writeb(musb->mregs, MUSB_POWER, power);
/* SOF continue */
musbdebugfs_h_setup(&setup_packet, musb);
return count;
}
if (!strncmp(buf, "test get_descripter", 18)) {
DBG(0, "SINGLE_STEP_GET_DEVICE_DESCRIPTOR\n");
/* the host issues SOFs for 15s allowing the test engineer
* to raise the scope trigger just above the SOF voltage level.
*/
msleep(15000);
musbdebugfs_h_setup(&setup_packet, musb);
return count;
}
if (!strncmp(buf, "test K", 6))
test = MUSB_TEST_K;
if (!strncmp(buf, "test J", 6))
test = MUSB_TEST_J;
if (!strncmp(buf, "test SE0 NAK", 12))
test = MUSB_TEST_SE0_NAK;
musb_writeb(musb->mregs, MUSB_TESTMODE, test);
return count;
}
static const struct file_operations musb_test_mode_fops = {
.open = musb_test_mode_open,
.write = musb_test_mode_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static inline int my_isspace(char c)
{
return (c == ' ' || c == '\t' || c == '\n' || c == '\12');
}
static inline int my_isupper(char c)
{
return (c >= 'A' && c <= 'Z');
}
static inline int my_isalpha(char c)
{
return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
}
static inline int my_isdigit(char c)
{
return (c >= '0' && c <= '9');
}
static unsigned my_strtoul
(const char *nptr, char **endptr, unsigned int base)
{
const char *s = nptr;
unsigned long acc;
int c;
unsigned long cutoff;
int neg = 0, any, cutlim;
do {
c = *s++;
} while (my_isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else if (c == '+')
c = *s++;
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
} else if ((base == 0 || base == 2) &&
c == '0' && (*s == 'b' || *s == 'B')) {
c = s[1];
s += 2;
base = 2;
}
if (base == 0)
base = c == '0' ? 8 : 10;
cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0;; c = *s++) {
if (my_isdigit(c))
c -= '0';
else if (my_isalpha(c))
c -= my_isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
if ((any < 0 || acc > cutoff || acc == cutoff) && c > cutlim)
any = -1;
else {
any = 1;
acc *= base;
acc += c;
}
}
if (any < 0)
acc = ULONG_MAX;
else if (neg)
acc = -acc;
if (endptr != 0)
*endptr = (char *)(any ? s - 1 : nptr);
return acc;
}
static int musb_regw_show(struct seq_file *s, void *unused)
{
DBG(0, "%s -> Called\n", __func__);
pr_notice("Uage:\n");
pr_notice("Mac Write: echo mac:addr:data > regw\n");
pr_notice("Phy Write: echo phy:addr:data > regw\n");
return 0;
}
static int musb_regw_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_regw_show, PDE_DATA(inode));
}
static ssize_t musb_regw_mode_write
(struct file *file, const char __user *ubuf
, size_t count, loff_t *ppos)
{
struct seq_file *s = file->private_data;
struct musb *musb = s->private;
char buf[20];
u8 is_mac = 0;
char *tmp1 = NULL;
char *tmp2 = NULL;
unsigned int offset = 0;
u8 data = 0;
memset(buf, 0x00, sizeof(buf));
pr_notice("%s -> Called\n", __func__);
if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
buf[19] = '\0';
if ((!strncmp(buf, "MAC", 3)) || (!strncmp(buf, "mac", 3)))
is_mac = 1;
else if ((!strncmp(buf, "PHY", 3)) || (!strncmp(buf, "phy", 3)))
is_mac = 0;
else
return -EFAULT;
tmp1 = strchr(buf, ':');
if (tmp1 == NULL)
return -EFAULT;
tmp1++;
if (strlen(tmp1) == 0)
return -EFAULT;
tmp2 = strrchr(buf, ':');
if (tmp2 == NULL)
return -EFAULT;
tmp2++;
if (strlen(tmp2) == 0)
return -EFAULT;
offset = my_strtoul(tmp1, NULL, 0);
data = my_strtoul(tmp2, NULL, 0);
if (is_mac == 1) {
pr_notice("Mac base adddr 0x%lx, Write 0x%x[0x%x]\n",
(unsigned long)musb->mregs, offset, data);
musb_writeb(musb->mregs, offset, data);
} else {
if ((offset % 4) != 0) {
pr_notice("Must use 32bits alignment address\n");
return count;
}
#ifdef CONFIG_MTK_MUSB_PHY
pr_notice("Phy base adddr 0x%lx, Write 0x%x[0x%x]\n",
(unsigned long)((void __iomem *)
(((unsigned long)musb->xceiv->io_priv)
+ 0x800)), offset, data);
USBPHY_WRITE32(offset, data);
#endif
}
return count;
}
static const struct file_operations musb_regw_fops = {
.open = musb_regw_open,
.write = musb_regw_mode_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int musb_regr_show(struct seq_file *s, void *unused)
{
DBG(0, "%s -> Called\n"
"Uage:\n"
"Mac Read: echo mac:addr > regr\n"
"Phy Read: echo phy:addr > regr\n", __func__);
return 0;
}
static int musb_regr_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_regr_show, PDE_DATA(inode));
}
static ssize_t musb_regr_mode_write(struct file *file,
const char __user *ubuf,
size_t count, loff_t *ppos)
{
struct seq_file *s = file->private_data;
struct musb *musb = s->private;
char buf[20];
u8 is_mac = 0;
char *tmp = NULL;
unsigned int offset = 0;
memset(buf, 0x00, sizeof(buf));
pr_notice("%s -> Called\n", __func__);
if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
buf[19] = '\0';
if ((!strncmp(buf, "MAC", 3)) || (!strncmp(buf, "mac", 3)))
is_mac = 1;
else if ((!strncmp(buf, "PHY", 3)) || (!strncmp(buf, "phy", 3)))
is_mac = 0;
else
return -EFAULT;
tmp = strrchr(buf, ':');
if (tmp == NULL)
return -EFAULT;
tmp++;
if (strlen(tmp) == 0)
return -EFAULT;
offset = my_strtoul(tmp, NULL, 0);
if (is_mac == 1)
pr_notice("Read Mac base adddr 0x%lx, Read 0x%x[0x%x]\n",
(unsigned long)musb->mregs, offset
, musb_readb(musb->mregs, offset));
else {
if ((offset % 4) != 0) {
pr_notice("Must use 32bits alignment address\n");
return count;
}
#ifdef CONFIG_MTK_MUSB_PHY
pr_notice("Read Phy base adddr 0x%lx, Read 0x%x[0x%x]\n",
(unsigned long)((void __iomem *)
(((unsigned long)musb->xceiv->io_priv)
+ 0x800)), offset,
USBPHY_READ32(offset));
#endif
}
return count;
}
static const struct file_operations musb_regr_fops = {
.open = musb_regr_open,
.write = musb_regr_mode_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int musb_speed_show(struct seq_file *s, void *unused)
{
seq_printf(s, "musb_speed = %d\n", musb_speed);
return 0;
}
static int musb_speed_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_speed_show, PDE_DATA(inode));
}
static ssize_t musb_speed_write(struct file *file,
const char __user *ubuf, size_t count, loff_t *ppos)
{
char buf[20];
unsigned int val;
memset(buf, 0x00, sizeof(buf));
if (copy_from_user(buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtouint(buf, 10, &val) == 0 && val >= 0 && val <= 1)
musb_speed = val;
else
return -EINVAL;
return count;
}
static const struct file_operations musb_speed_fops = {
.open = musb_speed_open,
.write = musb_speed_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
int musb_init_debugfs(struct musb *musb)
{
int ret, idx = 0;
proc_mkdir(PROC_DIR_MTK_USB, NULL);
proc_files[idx] = proc_create_data(PROC_FILE_REGDUMP, 0444,
NULL, &musb_regdump_fops, musb);
if (!proc_files[idx]) {
ret = -ENOMEM;
goto err1;
}
idx++;
proc_files[idx] = proc_create_data(PROC_FILE_TESTMODE, 0644,
NULL, &musb_test_mode_fops, musb);
if (!proc_files[idx]) {
ret = -ENOMEM;
goto err1;
}
idx++;
proc_files[idx] = proc_create_data(PROC_FILE_REGW, 0644,
NULL, &musb_regw_fops, musb);
if (!proc_files[idx]) {
ret = -ENOMEM;
goto err1;
}
idx++;
proc_files[idx] = proc_create_data(PROC_FILE_REGR, 0644,
NULL, &musb_regr_fops, musb);
if (!proc_files[idx]) {
ret = -ENOMEM;
goto err1;
}
idx++;
proc_files[idx] = proc_create_data(PROC_FILE_SPEED, 0644,
NULL, &musb_speed_fops, musb);
if (!proc_files[idx]) {
ret = -ENOMEM;
goto err1;
}
return 0;
err1:
for (; idx >= 0; idx--) {
if (proc_files[idx]) {
proc_remove(proc_files[idx]);
proc_files[idx] = NULL;
}
}
return ret;
}
void /* __init_or_exit */ musb_exit_debugfs(struct musb *musb)
{
int idx = 0;
for (; idx < PROC_FILE_NUM; idx++) {
if (proc_files[idx]) {
proc_remove(proc_files[idx]);
proc_files[idx] = NULL;
}
}
}
static int musb_mode_show(struct seq_file *sf, void *unused)
{
struct musb *musb = sf->private;
struct mt_usb_glue *glue =
container_of(&musb, struct mt_usb_glue, mtk_musb);
seq_printf(sf, "current mode: %s(%s drd)\n(echo device/host)\n",
musb->is_host ? "host" : "device",
glue->otg_sx.manual_drd_enabled ? "manual" : "auto");
return 0;
}
static int musb_mode_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_mode_show, PDE_DATA(inode));
}
static ssize_t musb_mode_write(struct file *file, const char __user *ubuf,
size_t count, loff_t *ppos)
{
struct seq_file *sf = file->private_data;
struct musb *musb = sf->private;
char buf[16];
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (!strncmp(buf, "host", 4) && !musb->is_host) {
mt_usb_mode_switch(musb, 1);
} else if (!strncmp(buf, "device", 6) && musb->is_host) {
mt_usb_mode_switch(musb, 0);
} else {
dev_err(musb->controller, "wrong or duplicated setting\n");
return -EINVAL;
}
return count;
}
static const struct file_operations musb_mode_fops = {
.open = musb_mode_open,
.write = musb_mode_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int musb_vbus_show(struct seq_file *sf, void *unused)
{
struct musb *musb = sf->private;
struct mt_usb_glue *glue =
container_of(&musb, struct mt_usb_glue, mtk_musb);
struct otg_switch_mtk *otg_sx = &glue->otg_sx;
seq_printf(sf, "vbus state: %s\n(echo on/off)\n",
regulator_is_enabled(otg_sx->vbus) ? "on" : "off");
return 0;
}
static int musb_vbus_open(struct inode *inode, struct file *file)
{
return single_open(file, musb_vbus_show, PDE_DATA(inode));
}
static ssize_t musb_vbus_write(struct file *file, const char __user *ubuf,
size_t count, loff_t *ppos)
{
struct seq_file *sf = file->private_data;
struct musb *musb = sf->private;
struct mt_usb_glue *glue =
container_of(&musb, struct mt_usb_glue, mtk_musb);
struct otg_switch_mtk *otg_sx = &glue->otg_sx;
char buf[16];
bool enable;
if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
return -EFAULT;
if (kstrtobool(buf, &enable)) {
dev_err(musb->controller, "wrong setting\n");
return -EINVAL;
}
mt_usb_set_vbus(otg_sx, enable);
return count;
}
static const struct file_operations musb_vbus_fops = {
.open = musb_vbus_open,
.write = musb_vbus_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
void musb_dr_debugfs_init(struct musb *musb)
{
int idx = 0;
proc_mkdir(PROC_DIR_MTK_USB, NULL);
proc_dr_files[idx++] = proc_create_data(PROC_FILE_MODE, 0644,
NULL, &musb_mode_fops, musb);
proc_dr_files[idx++] = proc_create_data(PROC_FILE_VBUS, 0644,
NULL, &musb_vbus_fops, musb);
}
+146
View File
@@ -0,0 +1,146 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MUSB_DMA_H__
#define __MUSB_DMA_H__
struct musb_hw_ep;
/*
* DMA Controller Abstraction
*
* DMA Controllers are abstracted to allow use of a variety of different
* implementations of DMA, as allowed by the Inventra USB cores. On the
* host side, usbcore sets up the DMA mappings and flushes caches; on the
* peripheral side, the gadget controller driver does. Responsibilities
* of a DMA controller driver include:
*
* - Handling the details of moving multiple USB packets
* in cooperation with the Inventra USB core, including especially
* the correct RX side treatment of short packets and buffer-full
* states (both of which terminate transfers).
*
* - Knowing the correlation between dma channels and the
* Inventra core's local endpoint resources and data direction.
*
* - Maintaining a list of allocated/available channels.
*
* - Updating channel status on interrupts,
* whether shared with the Inventra core or separate.
*/
#define DMA_ADDR_INVALID (~(dma_addr_t)0)
#ifndef CONFIG_MUSB_PIO_ONLY
#define is_dma_capable() (1)
#else
#define is_dma_capable() (0)
#endif
/* Anomaly 05000456 - USB Receive Interrupt Is Not Generated in DMA Mode 1
* Only allow DMA mode 1 to be used when the USB will actually generate the
* interrupts we expect.
*/
/*
* DMA channel status ... updated by the dma controller driver whenever that
* status changes, and protected by the overall controller spinlock.
*/
enum dma_channel_status {
/* unallocated */
MUSB_DMA_STATUS_UNKNOWN,
/* allocated ... but not busy, no errors */
MUSB_DMA_STATUS_FREE,
/* busy ... transactions are active */
MUSB_DMA_STATUS_BUSY,
/* transaction(s) aborted due to ... dma or memory bus error */
MUSB_DMA_STATUS_BUS_ABORT,
/* transaction(s) aborted due to ... core error or USB fault */
MUSB_DMA_STATUS_CORE_ABORT
};
struct dma_controller;
/**
* struct dma_channel - A DMA channel.
* @private_data: channel-private data
* @max_len: the maximum number of bytes the channel can move in one
* transaction (typically representing many USB maximum-sized packets)
* @actual_len: how many bytes have been transferred
* @prog_len: how many bytes have been programmed for transfer
* @status: current channel status (updated e.g. on interrupt)
* @desired_mode: true if mode 1 is desired; false if mode 0 is desired
*
* channels are associated with an endpoint for the duration of at least
* one usb transfer.
*/
struct dma_channel {
void *private_data;
/* FIXME not void* private_data, but a dma_controller * */
size_t max_len;
size_t actual_len;
size_t prog_len;
enum dma_channel_status status;
bool desired_mode;
};
/*
* dma_channel_status - return status of dma channel
* @c: the channel
*
* Returns the software's view of the channel status. If that status is BUSY
* then it's possible that the hardware has completed (or aborted) a transfer,
* so the driver needs to update that status.
*/
static inline enum dma_channel_status dma_channel_status(struct dma_channel *c)
{
return (is_dma_capable() && c) ? c->status : MUSB_DMA_STATUS_UNKNOWN;
}
/**
* struct dma_controller - A DMA Controller.
* @start: call this to start a DMA controller;
* return 0 on success, else negative errno
* @stop: call this to stop a DMA controller
* return 0 on success, else negative errno
* @channel_alloc: call this to allocate a DMA channel
* @channel_release: call this to release a DMA channel
* @channel_abort: call this to abort a pending DMA transaction,
* returning it to FREE (but allocated) state
* @channel_pause: This function pauses the ongoing DMA transfer
* @channel_resume: This function resumes the ongoing DMA transfer
* @tx_status: Gets the residue of an ongoing DMA transfer
* @check_resiudue: checks if the residue of an ongoing DMA
* transfer is valid
* Controllers manage dma channels.
*/
struct dma_controller {
int (*start)(struct dma_controller *c);
int (*stop)(struct dma_controller *c);
struct dma_channel *(*channel_alloc)(struct dma_controller *c,
struct musb_hw_ep *hw_ep,
u8 is_tx);
void (*channel_release)(struct dma_channel *channel);
int (*channel_program)(struct dma_channel *channel,
u16 maxpacket, u8 mode,
dma_addr_t dma_addr, u32 length);
int (*channel_abort)(struct dma_channel *channel);
int (*channel_pause)(struct dma_channel *channel);
int (*channel_resume)(struct dma_channel *channel);
int (*tx_status)(struct dma_channel *channel);
int (*check_residue)(struct dma_channel *channel, u32 residue);
int (*is_compatible)(struct dma_channel *channel,
u16 maxpacket, void *buf, u32 length);
};
/* called after channel_program(), may indicate a fault */
extern void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit);
extern struct dma_controller
*dma_controller_create(struct musb *musb, void __iomem *base);
extern void dma_controller_destroy(struct dma_controller *c);
#endif /* __MUSB_DMA_H__ */
+488
View File
@@ -0,0 +1,488 @@
// SPDX-License-Identifier: GPL-2.0
/*
* musb_dr.c - dual role switch and host glue layer
*
* Copyright (C) 2021 MediaTek Inc.
*
* Author: Macpaul Lin <macpaul.lin@mediatek.com>
*/
#include <linux/of_platform.h>
#include <usb20.h>
#include <musb_dr.h>
#include <musb_host.h>
#include <musb_gadget.h>
#ifdef CONFIG_DEBUG_FS
#include <musb_debug.h>
#endif
#if IS_ENABLED(CONFIG_MTK_BASE_POWER)
#include "mtk_spm_resource_req.h"
#endif
#define USB2_PORT 2
enum mt_usb_vbus_id_state {
MUSB_ID_FLOAT = 1,
MUSB_ID_GROUND,
MUSB_VBUS_OFF,
MUSB_VBUS_VALID,
};
static char *mailbox_state_string(enum mt_usb_vbus_id_state state)
{
switch (state) {
case MUSB_ID_FLOAT:
return "ID_FLOAT";
case MUSB_ID_GROUND:
return "ID_GROUND";
case MUSB_VBUS_OFF:
return "VBUS_OFF";
case MUSB_VBUS_VALID:
return "VBUS_VALID";
default:
return "UNKNOWN";
}
}
int mt_usb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
{
struct mt_usb_glue *glue =
container_of(otg_sx, struct mt_usb_glue, otg_sx);
struct musb *musb = glue->mtk_musb;
struct regulator *vbus = otg_sx->vbus;
int ret;
/* vbus is optional */
if (!vbus)
return 0;
dev_dbg(musb->controller, "%s: turn %s\n", __func__, is_on ? "on" : "off");
if (is_on) {
ret = regulator_enable(vbus);
if (ret) {
dev_err(musb->controller, "vbus regulator enable failed\n");
return ret;
}
} else {
regulator_disable(vbus);
}
return 0;
}
EXPORT_SYMBOL(mt_usb_set_vbus);
static void mt_usb_gadget_disconnect(struct musb *musb)
{
/* notify gadget driver */
if (musb->g.speed == USB_SPEED_UNKNOWN)
return;
if (musb->gadget_driver && musb->gadget_driver->disconnect) {
musb->gadget_driver->disconnect(&musb->g);
musb->g.speed = USB_SPEED_UNKNOWN;
}
usb_gadget_set_state(&musb->g, USB_STATE_NOTATTACHED);
}
/*
* switch to host: -> MUSB_VBUS_OFF --> MUSB_ID_GROUND
* switch to device: -> MUSB_ID_FLOAT --> MUSB_VBUS_VALID
*/
static void mt_usb_set_mailbox(struct otg_switch_mtk *otg_sx,
enum mt_usb_vbus_id_state status)
{
struct mt_usb_glue *glue =
container_of(otg_sx, struct mt_usb_glue, otg_sx);
struct musb *musb = glue->mtk_musb;
int i;
dev_info(musb->controller, "mailbox %s\n", mailbox_state_string(status));
switch (status) {
case MUSB_ID_GROUND:
mt_usb_set_vbus(otg_sx, 1);
musb->is_ready = true;
otg_sx->sw_state |= MUSB_ID_GROUND;
mt_usb_host_connect(0);
break;
case MUSB_ID_FLOAT:
mt_usb_host_disconnect(0);
musb->is_ready = false;
/* turn off VBUS until do_host_work switch to DEV mode */
for (i = 0; i < 6; i++) {
if (!musb->is_host)
break;
mdelay(50);
}
mt_usb_set_vbus(otg_sx, 0);
otg_sx->sw_state &= ~MUSB_ID_GROUND;
break;
case MUSB_VBUS_OFF:
/* ToDo or fix: killing any outstanding requests */
mt_usb_set_vbus(otg_sx, false);
musb->usb_connected = 0;
musb->is_host = false;
mt_usb_disconnect(); /* sync to UI */
mt_usb_gadget_disconnect(musb); /* sync to UI */
otg_sx->sw_state &= ~MUSB_VBUS_VALID;
break;
case MUSB_VBUS_VALID:
mt_usb_set_vbus(otg_sx, true);
/* avoid suspend when works as device */
otg_sx->sw_state |= MUSB_VBUS_VALID;
musb->usb_connected = 1;
mt_usb_connect();
break;
default:
dev_err(musb->controller, "invalid state\n");
}
}
static void mt_usb_id_work(struct work_struct *work)
{
struct otg_switch_mtk *otg_sx =
container_of(work, struct otg_switch_mtk, id_work);
if (otg_sx->id_event)
mt_usb_set_mailbox(otg_sx, MUSB_ID_GROUND);
else
mt_usb_set_mailbox(otg_sx, MUSB_ID_FLOAT);
}
static void mt_usb_vbus_work(struct work_struct *work)
{
struct otg_switch_mtk *otg_sx =
container_of(work, struct otg_switch_mtk, vbus_work);
if (otg_sx->vbus_event)
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_VALID);
else
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_OFF);
}
/*
* @mt_usb_id_notifier is called in atomic context, but @mt_usb_set_mailbox
* may sleep, so use work queue here
*/
static int mt_usb_id_notifier(struct notifier_block *nb,
unsigned long event, void *ptr)
{
struct otg_switch_mtk *otg_sx =
container_of(nb, struct otg_switch_mtk, id_nb);
otg_sx->id_event = event;
schedule_work(&otg_sx->id_work);
return NOTIFY_DONE;
}
static int mt_usb_vbus_notifier(struct notifier_block *nb,
unsigned long event, void *ptr)
{
struct otg_switch_mtk *otg_sx =
container_of(nb, struct otg_switch_mtk, vbus_nb);
otg_sx->vbus_event = event;
schedule_work(&otg_sx->vbus_work);
return NOTIFY_DONE;
}
static int mt_usb_extcon_register(struct otg_switch_mtk *otg_sx)
{
struct mt_usb_glue *glue =
container_of(otg_sx, struct mt_usb_glue, otg_sx);
struct musb *musb = glue->mtk_musb;
struct extcon_dev *edev = otg_sx->edev;
int ret;
/* extcon is optional */
if (!edev)
return 0;
otg_sx->vbus_nb.notifier_call = mt_usb_vbus_notifier;
ret = devm_extcon_register_notifier(musb->controller, edev, EXTCON_USB,
&otg_sx->vbus_nb);
if (ret < 0) {
dev_err(musb->controller, "failed to register notifier for USB\n");
return ret;
}
otg_sx->id_nb.notifier_call = mt_usb_id_notifier;
ret = devm_extcon_register_notifier(musb->controller, edev, EXTCON_USB_HOST,
&otg_sx->id_nb);
if (ret < 0) {
dev_err(musb->controller, "failed to register notifier for USB-HOST\n");
return ret;
}
dev_dbg(musb->controller, "EXTCON_USB: %d, EXTCON_USB_HOST: %d\n",
extcon_get_state(edev, EXTCON_USB),
extcon_get_state(edev, EXTCON_USB_HOST));
/* default as host, switch to device mode if needed */
if (extcon_get_state(edev, EXTCON_USB_HOST) == false)
mt_usb_set_mailbox(otg_sx, MUSB_ID_FLOAT);
if (extcon_get_state(edev, EXTCON_USB) == true)
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_VALID);
return 0;
}
/*
* We provide an interface via debugfs to switch between host and device modes
* depending on user input.
* This is useful in special cases, such as uses TYPE-A receptacle but also
* wants to support dual-role mode.
*/
void mt_usb_mode_switch(struct musb *musb, int to_host)
{
struct mt_usb_glue *glue =
container_of(&musb, struct mt_usb_glue, mtk_musb);
struct otg_switch_mtk *otg_sx = &glue->otg_sx;
if (to_host) {
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_OFF);
mt_usb_set_mailbox(otg_sx, MUSB_ID_GROUND);
} else {
mt_usb_set_mailbox(otg_sx, MUSB_ID_FLOAT);
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_VALID);
}
}
EXPORT_SYMBOL(mt_usb_mode_switch);
static int mt_usb_role_sx_set(struct device *dev, enum usb_role role)
{
struct mt_usb_glue *glue = dev_get_drvdata(dev);
struct otg_switch_mtk *otg_sx = &glue->otg_sx;
bool id_event, vbus_event;
static bool first_init = true;
dev_info(dev, "role_sx_set role %d, latest_role: %d\n",
role, otg_sx->latest_role);
/* Avoid transit from HOST -> DEV with NONE state */
if ((role == USB_ROLE_DEVICE && otg_sx->latest_role == USB_ROLE_HOST) ||
(role == USB_ROLE_HOST && otg_sx->latest_role == USB_ROLE_DEVICE)) {
DBG(0, "force USB_ROLE_NONE transit state.\n");
mt_usb_role_sx_set(dev, USB_ROLE_NONE);
}
otg_sx->latest_role = role;
if (otg_sx->op_mode != MUSB_DR_OPERATION_NORMAL) {
dev_info(dev, "op_mode %d, skip set role\n", otg_sx->op_mode);
return 0;
}
id_event = (role == USB_ROLE_HOST);
vbus_event = (role == USB_ROLE_DEVICE);
#ifdef CONFIG_MTK_UART_USB_SWITCH
in_uart_mode = usb_phy_check_in_uart_mode();
if (in_uart_mode) {
DBG(0, "At UART mode. Switch to USB is not support\n");
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_OFF);
phy_set_mode(glue->phy, PHY_MODE_INVALID);
return 0;
}
#endif
if (!!(otg_sx->sw_state & MUSB_VBUS_VALID) ^ vbus_event) {
if (vbus_event) {
dev_info(dev, "%s: if vbus_event true\n", __func__);
#ifdef CONFIG_MACH_MT6761
// phy_set_mode(glue->phy, PHY_MODE_USB_DEVICE);
set_usb_phy_clear();
#else
phy_set_mode(glue->phy, PHY_MODE_USB_DEVICE);
#endif
phy_power_on(glue->phy);
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_VALID);
} else {
mt_usb_set_mailbox(otg_sx, MUSB_VBUS_OFF);
dev_info(dev, "%s: if vbus_event false\n", __func__);
phy_power_off(glue->phy);
}
}
if (!!(otg_sx->sw_state & MUSB_ID_GROUND) ^ id_event) {
if (id_event) {
dev_info(dev, "%s: if id_event true\n", __func__);
phy_power_on(glue->phy);
/* PHY mode will be set in host_connect work */
mt_usb_set_mailbox(otg_sx, MUSB_ID_GROUND);
} else {
/*
* add this for reduce boot 200ms
* and add delay 200ms for plugout
*/
if (!first_init)
mdelay(200);
else
first_init = false;
/* PHY mode will be set in host_disconnect work */
mt_usb_set_mailbox(otg_sx, MUSB_ID_FLOAT);
phy_power_off(glue->phy);
}
}
return 0;
}
static enum usb_role mt_usb_role_sx_get(struct device *dev)
{
struct mt_usb_glue *glue = dev_get_drvdata(dev);
struct musb *musb = glue->mtk_musb;
enum usb_role role;
role = musb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE;
return role;
}
static int mt_usb_role_sw_register(struct otg_switch_mtk *otg_sx)
{
struct usb_role_switch_desc role_sx_desc = { 0 };
struct mt_usb_glue *glue =
container_of(otg_sx, struct mt_usb_glue, otg_sx);
struct musb *musb = glue->mtk_musb;
if (!otg_sx->role_sw_used)
return 0;
role_sx_desc.set = mt_usb_role_sx_set;
role_sx_desc.get = mt_usb_role_sx_get;
role_sx_desc.allow_userspace_control = true;
otg_sx->role_sw = usb_role_switch_register(glue->dev, &role_sx_desc);
if (IS_ERR(otg_sx->role_sw))
return PTR_ERR(otg_sx->role_sw);
mt_usb_role_sx_set(glue->dev, USB_ROLE_NONE);
musb->usb_connected = 0;
return 0;
}
static ssize_t cmode_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct musb *mtk_musb = dev_get_drvdata(dev);
struct otg_switch_mtk *otg_sx = mtk_musb->otg_sx;
enum usb_role role = otg_sx->latest_role;
/* note: can't use container_of() by mtk_musb glue, use otg_sx here */
struct mt_usb_glue *glue =
container_of(otg_sx, struct mt_usb_glue, otg_sx);
int mode;
if (kstrtoint(buf, 10, &mode))
return -EINVAL;
dev_info(dev, "store cmode %d op_mode %d\n", mode, otg_sx->op_mode);
if (otg_sx->op_mode != mode) {
/* set switch role */
switch (mode) {
case MUSB_DR_OPERATION_NONE:
otg_sx->latest_role = USB_ROLE_NONE;
break;
case MUSB_DR_OPERATION_NORMAL:
/* switch usb role to latest role */
break;
case MUSB_DR_OPERATION_HOST:
otg_sx->latest_role = USB_ROLE_HOST;
break;
case MUSB_DR_OPERATION_DEVICE:
otg_sx->latest_role = USB_ROLE_DEVICE;
break;
default:
return -EINVAL;
}
/* switch operation mode to normal temporarily */
otg_sx->op_mode = MUSB_DR_OPERATION_NORMAL;
/* switch usb role */
mt_usb_role_sx_set(glue->dev, otg_sx->latest_role);
/* update operation mode */
otg_sx->op_mode = mode;
/* restore role */
otg_sx->latest_role = role;
}
return count;
}
static ssize_t cmode_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct musb *mtk_musb = dev_get_drvdata(dev);
struct otg_switch_mtk *otg_sx = mtk_musb->otg_sx;
return sprintf(buf, "%d\n", otg_sx->op_mode);
}
static DEVICE_ATTR_RW(cmode);
static struct attribute *mt_usb_dr_attrs[] = {
&dev_attr_cmode.attr,
NULL
};
static const struct attribute_group mt_usb_dr_group = {
.attrs = mt_usb_dr_attrs,
};
int mt_usb_otg_switch_init(struct mt_usb_glue *glue)
{
struct otg_switch_mtk *otg_sx = &glue->otg_sx;
struct musb *mtk_musb = glue->mtk_musb;
int ret = 0;
/* we need to keep otg_sx here for cmode operations */
mtk_musb->otg_sx = otg_sx;
INIT_WORK(&otg_sx->id_work, mt_usb_id_work);
INIT_WORK(&otg_sx->vbus_work, mt_usb_vbus_work);
/* default as host, update state */
otg_sx->sw_state = mtk_musb->is_host ?
MUSB_ID_GROUND : MUSB_VBUS_VALID;
/* initial operation mode */
otg_sx->op_mode = MUSB_DR_OPERATION_NORMAL;
ret = sysfs_create_group(&mtk_musb->controller->kobj, &mt_usb_dr_group);
if (ret)
dev_info(mtk_musb->controller, "error creating sysfs attributes\n");
#ifdef CONFIG_DEBUG_FS
if (otg_sx->manual_drd_enabled)
musb_dr_debugfs_init(mtk_musb);
#endif
else if (otg_sx->role_sw_used)
ret = mt_usb_role_sw_register(otg_sx);
else
ret = mt_usb_extcon_register(otg_sx);
return ret;
}
EXPORT_SYMBOL(mt_usb_otg_switch_init);
void mt_usb_otg_switch_exit(struct mt_usb_glue *glue)
{
struct otg_switch_mtk *otg_sx = &glue->otg_sx;
struct musb *mtk_musb = glue->mtk_musb;
cancel_work_sync(&otg_sx->id_work);
cancel_work_sync(&otg_sx->vbus_work);
usb_role_switch_unregister(otg_sx->role_sw);
sysfs_remove_group(&mtk_musb->controller->kobj, &mt_usb_dr_group);
}
+101
View File
@@ -0,0 +1,101 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* musb_dr.h - dual role switch and host glue layer header
*
* Copyright (C) 2021 MediaTek Inc.
*
* Author: Macpaul Lin <macpaul.lin@mediatek.com>
*/
#ifndef _MUSB_DR_H_
#define _MUSB_DR_H_
#include <musb.h>
#include <musb_core.h>
#include <mtk_musb.h>
#include <usb20.h>
#if IS_ENABLED(CONFIG_USB_MTK_OTG) || IS_ENABLED(CONFIG_MTK_MUSB_DUAL_ROLE)
int musb_host_init(struct musb *musb, struct device_node *parent_dn);
void musb_host_exit(struct musb *musb);
int musb_wakeup_of_property_parse(struct musb *musb,
struct device_node *dn);
int musb_host_enable(struct musb *musb);
int musb_host_disable(struct musb *musb, bool suspend);
void musb_wakeup_set(struct musb *musb, bool enable);
#else
static inline int musb_host_init(struct musb *musb,
struct device_node *parent_dn)
{
return 0;
}
static inline void musb_host_exit(struct musb *musb)
{}
static inline int musb_wakeup_of_property_parse(
struct musb *musb, struct device_node *dn)
{
return 0;
}
static inline int musb_host_enable(struct musb *musb)
{
return 0;
}
static inline int musb_host_disable(struct musb *musb, bool suspend)
{
return 0;
}
static inline void musb_wakeup_set(struct musb *musb, bool enable)
{}
#endif
#if IS_ENABLED(CONFIG_USB_MTK_HDRC) || IS_ENABLED(CONFIG_MTK_MUSB_DUAL_ROLE)
int musb_gadget_init(struct musb *musb);
void musb_gadget_exit(struct musb *musb);
#else
static inline int musb_gadget_init(struct musb *musb)
{
return 0;
}
static inline void musb_gadget_exit(struct musb *musb)
{}
#endif
#if IS_ENABLED(CONFIG_MTK_MUSB_DUAL_ROLE)
int mt_usb_otg_switch_init(struct mt_usb_glue *glue);
void mt_usb_otg_switch_exit(struct mt_usb_glue *glue);
extern void mt_usb_mode_switch(struct musb *musb, int to_host);
extern int mt_usb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on);
void mt_usb_set_force_mode(struct musb *musb,
enum mt_usb_dr_force_mode mode);
#else
static inline int mt_usb_otg_switch_init(struct mt_usb_glue *glue)
{
return 0;
}
static inline void mt_usb_otg_switch_exit(struct mt_usb_glue *glue)
{}
static inline void mt_usb_mode_switch(struct musb *musb, int to_host)
{}
static inline int mt_usb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on)
{
return 0;
}
static inline void
mt_usb_set_force_mode(struct musb *musb, enum mt_usb_dr_force_mode mode)
{}
#endif
#endif /* _MUSB_DR_H_ */
File diff suppressed because it is too large. Load diff
+101
View File
@@ -0,0 +1,101 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MUSB_GADGET_H
#define __MUSB_GADGET_H
#include <linux/list.h>
enum buffer_map_state {
UN_MAPPED = 0,
PRE_MAPPED,
MUSB_MAPPED
};
struct musb_request {
struct usb_request request;
struct list_head list;
struct musb_ep *ep;
struct musb *musb;
u8 tx; /* endpoint direction */
u8 epnum;
enum buffer_map_state map_state;
};
static inline struct musb_request *to_musb_request(struct usb_request *req)
{
return req ? container_of(req, struct musb_request, request) : NULL;
}
extern struct usb_request
*musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags);
extern void musb_free_request(struct usb_ep *ep, struct usb_request *req);
/*
* struct musb_ep - peripheral side view of endpoint rx or tx side
*/
struct musb_ep {
/* stuff towards the head is basically write-once. */
struct usb_ep end_point;
char name[12];
struct musb_hw_ep *hw_ep;
struct musb *musb;
u16 fifo_size;
enum musb_buf_mode fifo_mode;
u8 current_epnum;
/* ... when enabled/disabled ... */
u8 type;
u8 is_in;
u16 packet_sz;
const struct usb_endpoint_descriptor *desc;
struct dma_channel *dma;
/* later things are modified based on usage */
struct list_head req_list;
u8 wedged;
/* true if lock must be dropped but req_list may not be advanced */
u8 busy;
u8 hb_mult;
/* true if packet is received in fifo and req_list is empty */
u8 rx_pending;
};
static inline struct musb_ep *to_musb_ep(struct usb_ep *ep)
{
return ep ? container_of(ep, struct musb_ep, end_point) : NULL;
}
static inline struct musb_request *next_request(struct musb_ep *ep)
{
struct list_head *queue = &ep->req_list;
if (list_empty(queue))
return NULL;
return container_of(queue->next, struct musb_request, list);
}
extern void musb_g_tx(struct musb *musb, u8 epnum);
extern void musb_g_rx(struct musb *musb, u8 epnum);
extern const struct usb_ep_ops musb_g_ep0_ops;
extern int musb_gadget_setup(struct musb *musb);
extern void musb_gadget_cleanup(struct musb *musb);
extern void
musb_g_giveback(struct musb_ep *ep,
struct usb_request *request,
int status) __releases(ep->musb->lock) __acquires(ep->musb->lock);
extern void
musb_ep_restart(struct musb *musb, struct musb_request *req);
#endif /* __MUSB_GADGET_H */
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+100
View File
@@ -0,0 +1,100 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef _MUSB_HOST_H
#define _MUSB_HOST_H
#include <linux/scatterlist.h>
static inline struct usb_hcd *musb_to_hcd(struct musb *musb)
{
return container_of((void *)musb, struct usb_hcd, hcd_priv);
}
static inline struct musb *hcd_to_musb(struct usb_hcd *hcd)
{
return (struct musb *)(hcd->hcd_priv);
}
/* stored in "usb_host_endpoint.hcpriv" for scheduled endpoints */
struct musb_qh {
struct usb_host_endpoint *hep; /* usbcore info */
struct usb_device *dev;
struct musb_hw_ep *hw_ep; /* current binding */
struct list_head ring; /* of musb_qh */
/* struct musb_qh *next; *//* for periodic tree */
u8 mux; /* qh multiplexed to hw_ep */
unsigned int offset; /* in urb->transfer_buffer */
unsigned int segsize; /* current xfer fragment */
u8 type_reg; /* {rx,tx} type register */
u8 intv_reg; /* {rx,tx} interval register */
u8 addr_reg; /* device address register */
u8 h_addr_reg; /* hub address register */
u8 h_port_reg; /* hub port register */
u8 is_ready; /* safe to modify hw_ep */
u8 type; /* XFERTYPE_* */
u8 epnum;
u8 hb_mult; /* high bandwidth pkts per uf */
u16 maxpacket;
u16 frame; /* for periodic schedule */
unsigned int iso_idx; /* in urb->iso_frame_desc[] */
struct sg_mapping_iter sg_miter; /* for highmem in PIO mode */
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
u8 is_use_qmu;
#endif
bool db_used;
};
/* map from control or bulk queue head to the first qh on that ring */
static inline struct musb_qh *first_qh(struct list_head *q)
{
if (list_empty(q))
return NULL;
return list_entry(q->next, struct musb_qh, ring);
}
void musb_h_pre_disable(struct musb *musb);
extern void musb_root_disconnect(struct musb *musb);
extern int musb_port_suspend(struct musb *musb, bool do_suspend);
struct usb_hcd;
extern int musb_hub_status_data(struct usb_hcd *hcd, char *buf);
extern int musb_hub_control(struct usb_hcd *hcd,
u16 typeReq, u16 wValue,
u16 wIndex, char *buf,
u16 wLength);
extern const struct hc_driver musb_hc_driver;
static inline struct urb *next_urb(struct musb_qh *qh)
{
struct list_head *queue;
if (!qh)
return NULL;
queue = &qh->hep->urb_list;
if (list_empty(queue))
return NULL;
return list_entry(queue->next, struct urb, urb_list);
}
extern u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr);
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
extern void musb_ep_set_qh(struct musb_hw_ep *ep, int isRx, struct musb_qh *qh);
extern struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int isRx);
extern void musb_advance_schedule(struct musb *musb, struct urb *urb,
struct musb_hw_ep *hw_ep, int is_in);
#endif
enum {
QH_FREE_RESCUE_INTERRUPT,
QH_FREE_RESCUE_EP_DISABLE,
};
#endif /* _MUSB_HOST_H */
+173
View File
@@ -0,0 +1,173 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef __MUSB_LINUX_PLATFORM_ARCH_H__
#define __MUSB_LINUX_PLATFORM_ARCH_H__
#include <linux/io.h>
#include <linux/spinlock.h>
#include <usb20.h>
#include <musb_debug.h>
extern bool mtk_usb_power;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
extern void mt_usb_clock_prepare(void);
extern void mt_usb_clock_unprepare(void);
#endif
extern spinlock_t usb_io_lock;
static inline u16 musb_readw(const void __iomem *addr, unsigned int offset)
{
u16 rc = 0;
if (likely(mtk_usb_power)) {
rc = readw(addr + offset);
} else {
unsigned long flags = 0;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_prepare();
#endif
spin_lock_irqsave(&usb_io_lock, flags);
usb_enable_clock(true);
DBG(1, "[MUSB]:access %s function when usb clock is off 0x%X\n",
__func__, offset);
rc = readw(addr + offset);
usb_enable_clock(false);
spin_unlock_irqrestore(&usb_io_lock, flags);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_unprepare();
#endif
}
return rc;
}
static inline u32
musb_readl(const void __iomem *addr, unsigned int offset)
{
u32 rc = 0;
if (likely(mtk_usb_power)) {
rc = readl(addr + offset);
} else {
unsigned long flags = 0;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_prepare();
#endif
spin_lock_irqsave(&usb_io_lock, flags);
usb_enable_clock(true);
DBG(1, "[MUSB]:access %s function when usb clock is off 0x%X\n",
__func__, offset);
rc = readl(addr + offset);
usb_enable_clock(false);
spin_unlock_irqrestore(&usb_io_lock, flags);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_unprepare();
#endif
}
return rc;
}
static inline void
musb_writew(void __iomem *addr, unsigned int offset, u16 data)
{
if (likely(mtk_usb_power)) {
writew(data, addr + offset);
} else {
unsigned long flags = 0;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_prepare();
#endif
spin_lock_irqsave(&usb_io_lock, flags);
usb_enable_clock(true);
DBG(1, "[MUSB]:access %s function when usb clock is off 0x%X\n",
__func__, offset);
writew(data, addr + offset);
usb_enable_clock(false);
spin_unlock_irqrestore(&usb_io_lock, flags);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_unprepare();
#endif
}
}
static inline void
musb_writel(void __iomem *addr, unsigned int offset, u32 data)
{
if (likely(mtk_usb_power)) {
writel(data, addr + offset);
} else {
unsigned long flags = 0;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_prepare();
#endif
spin_lock_irqsave(&usb_io_lock, flags);
usb_enable_clock(true);
DBG(1, "[MUSB]:access %s function when usb clock is off 0x%X\n",
__func__, offset);
writel(data, addr + offset);
usb_enable_clock(false);
spin_unlock_irqrestore(&usb_io_lock, flags);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_unprepare();
#endif
}
}
static inline u8 musb_readb(const void __iomem *addr, unsigned int offset)
{
u8 rc = 0;
if (likely(mtk_usb_power)) {
rc = readb(addr + offset);
} else {
unsigned long flags = 0;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_prepare();
#endif
spin_lock_irqsave(&usb_io_lock, flags);
usb_enable_clock(true);
DBG(1, "[MUSB]:access %s function when usb clock is off 0x%X\n",
__func__, offset);
rc = readb(addr + offset);
usb_enable_clock(false);
spin_unlock_irqrestore(&usb_io_lock, flags);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_unprepare();
#endif
}
return rc;
}
static inline void musb_writeb
(void __iomem *addr, unsigned int offset, u8 data)
{
if (likely(mtk_usb_power)) {
writeb(data, addr + offset);
} else {
unsigned long flags = 0;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_prepare();
#endif
spin_lock_irqsave(&usb_io_lock, flags);
usb_enable_clock(true);
DBG(1, "[MUSB]:access %s function when usb clock is off 0x%X\n",
__func__, offset);
writeb(data, addr + offset);
usb_enable_clock(false);
spin_unlock_irqrestore(&usb_io_lock, flags);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
mt_usb_clock_unprepare();
#endif
}
}
#endif
+753
View File
@@ -0,0 +1,753 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/stat.h>
#include <linux/usb/composite.h>
#include "musb_core.h"
#include "musb_host.h"
#include "musbhsdma.h"
#include "mtk_musb.h"
#include "musb_qmu.h"
static unsigned int host_qmu_tx_max_active_isoc_gpd[MAX_QMU_EP + 1];
static unsigned int host_qmu_tx_max_number_of_pkts[MAX_QMU_EP + 1];
static unsigned int host_qmu_rx_max_active_isoc_gpd[MAX_QMU_EP + 1];
static unsigned int host_qmu_rx_max_number_of_pkts[MAX_QMU_EP + 1];
#define MTK_HOST_ACTIVE_DEV_TABLE_SZ 128
static u8 mtk_host_active_dev_table[MTK_HOST_ACTIVE_DEV_TABLE_SZ];
#ifdef CONFIG_MTK_UAC_POWER_SAVING
static struct workqueue_struct *low_power_timer_test_wq;
static struct work_struct low_power_timer_test_work;
static int low_power_timer_activate;
static unsigned long low_power_timer_total_sleep;
static unsigned long low_power_timer_total_wake;
static int low_power_timer_request_time;
static unsigned int low_power_timer_trigger_cnt;
static unsigned int low_power_timer_wake_cnt;
static ktime_t ktime_wake, ktime_sleep, ktime_begin, ktime_end;
static int low_power_timer_irq_ctrl;
#define LOW_POWER_TIMER_MIN_PERIOD 10 /* ms */
#define LOW_POWER_TIMER_MAX_PERIOD 50 /* ms */
static struct delayed_work low_power_timer_montior_work;
enum {
IDLE_STAGE,
RUNNING_STAGE
};
#define MONITOR_FREQ 6000 /* ms */
static void do_low_power_timer_monitor_work(struct work_struct *work)
{
static int state = IDLE_STAGE;
static unsigned int last_trigger_cnt;
DBG(1, "state:%s, last:%d, balanced<%d,%d>\n",
state?"RUNNING_STAGE":"IDLE_STAGE",
last_trigger_cnt,
low_power_timer_total_trigger_cnt,
low_power_timer_total_wake_cnt);
if (state == IDLE_STAGE) {
if (last_trigger_cnt != low_power_timer_total_trigger_cnt)
state = RUNNING_STAGE;
} else if (state == RUNNING_STAGE) {
if (last_trigger_cnt == low_power_timer_total_trigger_cnt
&& low_power_timer_total_trigger_cnt ==
low_power_timer_total_wake_cnt)
state = IDLE_STAGE;
else if (last_trigger_cnt ==
low_power_timer_total_trigger_cnt
&& low_power_timer_total_trigger_cnt !=
low_power_timer_total_wake_cnt)
musb_bug();
}
last_trigger_cnt = low_power_timer_total_trigger_cnt;
schedule_delayed_work(&low_power_timer_montior_work,
msecs_to_jiffies(MONITOR_FREQ));
}
static void low_power_timer_wakeup_func(unsigned long data);
static DEFINE_TIMER(low_power_timer, low_power_timer_wakeup_func, 0, 0);
static void low_power_timer_resource_reset(void)
{
low_power_timer_total_sleep = low_power_timer_total_wake = 0;
low_power_timer_trigger_cnt = low_power_timer_wake_cnt = 0;
}
static void low_power_timer_wakeup_func(struct timer_list *timer)
{
#ifdef TIMER_LOCK
unsigned long flags;
spin_lock_irqsave(&mtk_musb->lock, flags);
#endif
if (unlikely(low_power_timer_irq_ctrl))
enable_irq(mtk_musb->nIrq);
low_power_timer_wake_cnt++;
low_power_timer_total_wake_cnt++;
ktime_wake = ktime_get();
low_power_timer_total_sleep +=
ktime_to_us(ktime_sub(ktime_wake, ktime_sleep));
/* deep idle forbidd here */
if (low_power_timer_mode == 1)
usb_hal_dpidle_request(USB_DPIDLE_FORBIDDEN);
/* make sure all global status sync to memory */
mb();
if (low_power_timer_trigger_cnt >= 2) {
static DEFINE_RATELIMIT_STATE(ratelimit, HZ, 1);
if (__ratelimit(&ratelimit))
pr_debug("<ratelimit> avg sleep(%lu/%d, %lu)us,
avg wake(%lu/%d, %lu)us, mode<%d,%d>,
local balanced<%d,%d>\n"
, low_power_timer_total_sleep,
low_power_timer_trigger_cnt,
low_power_timer_total_sleep/
low_power_timer_trigger_cnt,
low_power_timer_total_wake,
(low_power_timer_trigger_cnt - 1),
low_power_timer_total_wake/
(low_power_timer_trigger_cnt - 1),
low_power_timer_mode,
low_power_timer_mode2_option,
low_power_timer_trigger_cnt,
low_power_timer_wake_cnt);
}
low_power_timer_activate = 0;
DBG(1, "sleep forbidden <%d,%d>\n",
low_power_timer_total_trigger_cnt,
low_power_timer_total_wake_cnt);
#ifdef TIMER_FUNC_LOCK
spin_unlock_irqrestore(&mtk_musb->lock, flags);
#endif
}
static void try_trigger_low_power_timer(signed int sleep_ms)
{
DBG(1, "sleep_ms:%d\n", sleep_ms);
if (sleep_ms <= LOW_POWER_TIMER_MIN_PERIOD ||
sleep_ms >= LOW_POWER_TIMER_MAX_PERIOD) {
low_power_timer_activate = 0;
return;
}
/* dynamic allocation for timer , result in memory leak */
#ifdef TIMER_DYNAMIC
{
struct timer_list *timer;
timer = kzalloc(sizeof(struct timer_list), GFP_ATOMIC);
if (!timer) {
low_power_timer_activate = 0;
DBG(0, "alloc timer fail\n");
return;
}
timer_setup(timer, low_power_timer_wakeup_func, 0);
timer->expires = jiffies + msecs_to_jiffies(sleep_ms);
add_timer(timer);
}
#else
mod_timer(&low_power_timer, jiffies + msecs_to_jiffies(sleep_ms));
#endif
DBG(1, "sleep allowed <%d,%d>, %d ms\n",
low_power_timer_total_trigger_cnt,
low_power_timer_total_wake_cnt, sleep_ms);
ktime_sleep = ktime_get();
low_power_timer_trigger_cnt++;
low_power_timer_total_trigger_cnt++;
if (unlikely(low_power_timer_irq_ctrl))
disable_irq_nosync(mtk_musb->nIrq);
if (low_power_timer_trigger_cnt >= 2)
low_power_timer_total_wake +=
ktime_to_us(ktime_sub(ktime_sleep, ktime_wake));
/* deep idle allow here */
if (low_power_timer_mode == 1)
usb_hal_dpidle_request(USB_DPIDLE_SRAM);
}
static void do_low_power_timer_test_work(struct work_struct *work)
{
unsigned long flags;
signed int set_time;
unsigned long diff_time_ns;
int gpd_free_count_last, gpd_free_count;
int done = 0;
spin_lock_irqsave(&mtk_musb->lock, flags);
gpd_free_count = qmu_free_gpd_count(0, ISOC_EP_START_IDX);
gpd_free_count_last = gpd_free_count;
spin_unlock_irqrestore(&mtk_musb->lock, flags);
while (!done) {
udelay(300);
spin_lock_irqsave(&mtk_musb->lock, flags);
gpd_free_count = qmu_free_gpd_count(0, ISOC_EP_START_IDX);
if (gpd_free_count == gpd_free_count_last) {
ktime_end = ktime_get();
diff_time_ns =
ktime_to_us(ktime_sub(ktime_end, ktime_begin));
set_time = (low_power_timer_request_time -
(diff_time_ns/1000));
try_trigger_low_power_timer(set_time);
done = 1;
}
gpd_free_count_last = gpd_free_count;
spin_unlock_irqrestore(&mtk_musb->lock, flags);
}
}
static void lower_power_timer_test_init(void)
{
INIT_WORK(&low_power_timer_test_work, do_low_power_timer_test_work);
low_power_timer_test_wq =
create_singlethread_workqueue("usb20_low_power_timer_test_wq");
/* TIMER_DEFERRABLE for not interfering with deep idle */
INIT_DEFERRABLE_WORK(&low_power_timer_montior_work,
do_low_power_timer_monitor_work);
schedule_delayed_work(&low_power_timer_montior_work, 0);
}
/* mode 0 : no timer mechanism
* mode 1 : real case for idle task, no-usb-irq control
* mode 2 + option 0: simulate SCREEN ON mode 1 case
* mode 2 + option 1: simulate SCREEN OFF mode 1 perfect case
* mode 2 + option 2: simulate SCREEN OFF mode 1 real case
*/
static void low_power_timer_sleep(unsigned int sleep_ms)
{
DBG(1, "sleep(%d) ms\n", sleep_ms);
low_power_timer_activate = 1;
if (low_power_timer_mode == 2 && low_power_timer_mode2_option != 0)
low_power_timer_irq_ctrl = 1;
else
low_power_timer_irq_ctrl = 0;
if ((low_power_timer_mode == 2) &&
(low_power_timer_mode2_option == 2)) {
low_power_timer_request_time = sleep_ms;
ktime_begin = ktime_get();
queue_work(low_power_timer_test_wq, &low_power_timer_test_work);
} else
try_trigger_low_power_timer(sleep_ms);
}
#endif
static void mtk_host_active_dev_resource_reset(void)
{
memset(mtk_host_active_dev_table, 0, sizeof(mtk_host_active_dev_table));
mtk_host_active_dev_cnt = 0;
}
void musb_host_active_dev_add(unsigned int addr)
{
DBG(1, "devnum:%d\n", addr);
if (addr < MTK_HOST_ACTIVE_DEV_TABLE_SZ &&
!mtk_host_active_dev_table[addr]) {
mtk_host_active_dev_table[addr] = 1;
mtk_host_active_dev_cnt++;
}
}
EXPORT_SYMBOL(musb_host_active_dev_add);
void __iomem *qmu_base;
/* debug variable to check qmu_base issue */
void __iomem *qmu_base_2;
int musb_qmu_init(struct musb *musb)
{
/* set DMA channel 0 burst mode to boost QMU speed */
musb_writel(musb->mregs, 0x204, musb_readl(musb->mregs, 0x204) | 0x600);
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
/* eanble DMA channel 0 36-BIT support */
musb_writel(musb->mregs, 0x204,
musb_readl(musb->mregs, 0x204) | 0x4000);
#endif
/* make IOC field in GPD workable */
musb_writel((musb->mregs + MUSB_QISAR), 0x30, 0);
qmu_base = (void __iomem *)(mtk_musb->mregs + MUSB_QMUBASE);
/* debug variable to check qmu_base issue */
qmu_base_2 = (void __iomem *)(mtk_musb->mregs + MUSB_QMUBASE);
/* finish all hw op before init qmu */
mb();
if (qmu_init_gpd_pool(musb->controller)) {
QMU_ERR("[QMU]qmu_init_gpd_pool fail\n");
return -1;
}
#ifdef CONFIG_MTK_UAC_POWER_SAVING
lower_power_timer_test_init();
#endif
return 0;
}
EXPORT_SYMBOL(musb_qmu_init);
void musb_qmu_exit(struct musb *musb)
{
qmu_destroy_gpd_pool(musb->controller);
}
EXPORT_SYMBOL(musb_qmu_exit);
void musb_disable_q_all(struct musb *musb)
{
u32 ep_num;
QMU_WARN("disable_q_all\n");
mtk_host_active_dev_resource_reset();
#ifdef CONFIG_MTK_UAC_POWER_SAVING
low_power_timer_resource_reset();
#endif
for (ep_num = 1; ep_num <= RXQ_NUM; ep_num++) {
if (mtk_is_qmu_enabled(ep_num, RXQ))
mtk_disable_q(musb, ep_num, 1);
/* reset statistics */
host_qmu_rx_max_active_isoc_gpd[ep_num] = 0;
host_qmu_rx_max_number_of_pkts[ep_num] = 0;
}
for (ep_num = 1; ep_num <= TXQ_NUM; ep_num++) {
if (mtk_is_qmu_enabled(ep_num, TXQ))
mtk_disable_q(musb, ep_num, 0);
/* reset statistics */
host_qmu_tx_max_active_isoc_gpd[ep_num] = 0;
host_qmu_tx_max_number_of_pkts[ep_num] = 0;
}
}
EXPORT_SYMBOL(musb_disable_q_all);
void musb_kick_D_CmdQ(struct musb *musb, struct musb_request *request)
{
int isRx;
isRx = request->tx ? 0 : 1;
/* enable qmu at musb_gadget_eanble */
#ifdef NEVER
if (!mtk_is_qmu_enabled(request->epnum, isRx)) {
/* enable qmu */
mtk_qmu_enable(musb, request->epnum, isRx);
}
#endif
/* note tx needed additional zlp field */
mtk_qmu_insert_task(request->epnum,
isRx,
request->request.dma,
request->request.length,
((request->request.zero == 1) ? 1 : 0), 1);
mtk_qmu_resume(request->epnum, isRx);
}
EXPORT_SYMBOL(musb_kick_D_CmdQ);
irqreturn_t musb_q_irq(struct musb *musb)
{
irqreturn_t retval = IRQ_NONE;
u32 wQmuVal = musb->int_queue;
int i;
QMU_INFO("wQmuVal:%d\n", wQmuVal);
for (i = 1; i <= MAX_QMU_EP; i++) {
if (wQmuVal & DQMU_M_RX_DONE(i)) {
if (!musb->is_host)
qmu_done_rx(musb, i);
else
h_qmu_done_rx(musb, i);
}
if (wQmuVal & DQMU_M_TX_DONE(i)) {
if (!musb->is_host)
qmu_done_tx(musb, i);
else
h_qmu_done_tx(musb, i);
}
}
mtk_qmu_irq_err(musb, wQmuVal);
return retval;
}
EXPORT_SYMBOL(musb_q_irq);
void musb_flush_qmu(u32 ep_num, u8 isRx)
{
QMU_DBG("flush %s(%d)\n", isRx ? "RQ" : "TQ", ep_num);
mtk_qmu_stop(ep_num, isRx);
qmu_reset_gpd_pool(ep_num, isRx);
}
EXPORT_SYMBOL(musb_flush_qmu);
void musb_restart_qmu(struct musb *musb, u32 ep_num, u8 isRx)
{
QMU_DBG("restart %s(%d)\n", isRx ? "RQ" : "TQ", ep_num);
flush_ep_csr(musb, ep_num, isRx);
mtk_qmu_enable(musb, ep_num, isRx);
}
EXPORT_SYMBOL(musb_restart_qmu);
bool musb_is_qmu_stop(u32 ep_num, u8 isRx)
{
void __iomem *base = qmu_base;
/* debug variable to check qmu_base issue */
if (qmu_base != qmu_base_2)
QMU_WARN("qmu_base != qmu_base_2, qmu_base = %p, qmu_base_2=%p"
, qmu_base, qmu_base_2);
if (!isRx) {
if (MGC_ReadQMU16(base, MGC_O_QMU_TQCSR(ep_num))
& DQMU_QUE_ACTIVE)
return false;
else
return true;
} else {
if (MGC_ReadQMU16(base, MGC_O_QMU_RQCSR(ep_num))
& DQMU_QUE_ACTIVE)
return false;
else
return true;
}
}
#ifndef CONFIG_MTK_MUSB_QMU_PURE_ZLP_SUPPORT
void musb_tx_zlp_qmu(struct musb *musb, u32 ep_num)
{
/* sent ZLP through PIO */
void __iomem *epio = musb->endpoints[ep_num].regs;
void __iomem *mbase = musb->mregs;
int cnt = 50; /* 50*200us, total 10 ms */
int is_timeout = 1;
u16 csr;
QMU_WARN("TX ZLP direct sent\n");
musb_ep_select(mbase, ep_num);
/* disable dma for pio */
csr = musb_readw(epio, MUSB_TXCSR);
csr &= ~MUSB_TXCSR_DMAENAB;
musb_writew(epio, MUSB_TXCSR, csr);
/* TXPKTRDY */
csr = musb_readw(epio, MUSB_TXCSR);
csr |= MUSB_TXCSR_TXPKTRDY;
musb_writew(epio, MUSB_TXCSR, csr);
/* wait ZLP sent */
while (cnt--) {
csr = musb_readw(epio, MUSB_TXCSR);
if (!(csr & MUSB_TXCSR_TXPKTRDY)) {
is_timeout = 0;
break;
}
udelay(200);
}
/* re-enable dma for qmu */
csr = musb_readw(epio, MUSB_TXCSR);
csr |= MUSB_TXCSR_DMAENAB;
musb_writew(epio, MUSB_TXCSR, csr);
if (is_timeout)
QMU_ERR("TX ZLP sent fail???\n");
QMU_WARN("TX ZLP sent done\n");
}
#endif
int mtk_kick_CmdQ(struct musb *musb,
int isRx, struct musb_qh *qh, struct urb *urb)
{
void __iomem *mbase = musb->mregs;
u16 intr_e = 0;
struct musb_hw_ep *hw_ep = qh->hw_ep;
void __iomem *epio = hw_ep->regs;
unsigned int offset = 0;
u8 bIsIoc;
dma_addr_t pBuffer;
u32 dwLength;
u16 i;
u32 gpd_free_count = 0;
if (!urb) {
QMU_WARN("!urb\n");
return -1; /*KOBE : should we return a value */
}
if (!mtk_is_qmu_enabled(hw_ep->epnum, isRx)) {
DBG(1, "! mtk_is_qmu_enabled<%d,%s>\n",
hw_ep->epnum, isRx?"RXQ":"TXQ");
musb_ep_select(mbase, hw_ep->epnum);
flush_ep_csr(musb, hw_ep->epnum, isRx);
if (isRx) {
DBG(4, "isRX = 1\n");
if (qh->type == USB_ENDPOINT_XFER_ISOC) {
DBG(4, "USB_ENDPOINT_XFER_ISOC\n");
if (qh->hb_mult == 3)
musb_writew(epio, MUSB_RXMAXP,
qh->maxpacket|0x1000);
else if (qh->hb_mult == 2)
musb_writew(epio, MUSB_RXMAXP,
qh->maxpacket|0x800);
else
musb_writew(epio, MUSB_RXMAXP,
qh->maxpacket);
} else {
DBG(4, "!! USB_ENDPOINT_XFER_ISOC\n");
musb_writew(epio, MUSB_RXMAXP, qh->maxpacket);
}
musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_DMAENAB);
/*CC: speed */
musb_writeb(epio, MUSB_RXTYPE,
(qh->type_reg | usb_pipeendpoint(urb->pipe)));
musb_writeb(epio, MUSB_RXINTERVAL, qh->intv_reg);
#ifdef CONFIG_USB_MTK_HDRC
if (musb->is_multipoint) {
DBG(4, "is_multipoint\n");
musb_write_rxfunaddr(musb->mregs, hw_ep->epnum,
qh->addr_reg);
musb_write_rxhubaddr(musb->mregs, hw_ep->epnum,
qh->h_addr_reg);
musb_write_rxhubport(musb->mregs, hw_ep->epnum,
qh->h_port_reg);
} else {
DBG(4, "!! is_multipoint\n");
musb_writeb(musb->mregs, MUSB_FADDR,
qh->addr_reg);
}
#endif
/*turn off intrRx*/
intr_e = musb_readw(musb->mregs, MUSB_INTRRXE);
intr_e = intr_e & (~(1<<(hw_ep->epnum)));
musb_writew(musb->mregs, MUSB_INTRRXE, intr_e);
} else {
musb_writew(epio, MUSB_TXMAXP, qh->maxpacket);
musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_DMAENAB);
/*CC: speed?*/
musb_writeb(epio, MUSB_TXTYPE,
(qh->type_reg | usb_pipeendpoint(urb->pipe)));
musb_writeb(epio, MUSB_TXINTERVAL,
qh->intv_reg);
#ifdef CONFIG_USB_MTK_HDRC
if (musb->is_multipoint) {
DBG(4, "is_multipoint\n");
musb_write_txfunaddr(mbase, hw_ep->epnum,
qh->addr_reg);
musb_write_txhubaddr(mbase, hw_ep->epnum,
qh->h_addr_reg);
musb_write_txhubport(mbase, hw_ep->epnum,
qh->h_port_reg);
/* FIXME if !epnum, do the same for RX ... */
} else {
DBG(4, "!! is_multipoint\n");
musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
}
#endif
/*
* Turn off intrTx ,
* but this will be revert
* by musb_ep_program
*/
intr_e = musb_readw(musb->mregs, MUSB_INTRTXE);
intr_e = intr_e & (~(1<<hw_ep->epnum));
musb_writew(musb->mregs, MUSB_INTRTXE, intr_e);
}
mtk_qmu_enable(musb, hw_ep->epnum, isRx);
}
gpd_free_count = qmu_free_gpd_count(isRx, hw_ep->epnum);
if (qh->type == USB_ENDPOINT_XFER_ISOC) {
u32 gpd_used_count;
DBG(4, "USB_ENDPOINT_XFER_ISOC\n");
pBuffer = urb->transfer_dma;
if (gpd_free_count < urb->number_of_packets) {
DBG(0, "gpd_free_count:%d, number_of_packets:%d\n",
gpd_free_count, urb->number_of_packets);
DBG(0, "%s:%d Error Here\n", __func__, __LINE__);
return -ENOSPC;
}
for (i = 0; i < urb->number_of_packets; i++) {
urb->iso_frame_desc[i].status = 0;
offset = urb->iso_frame_desc[i].offset;
dwLength = urb->iso_frame_desc[i].length;
/* If interrupt on complete ? */
bIsIoc = (i == (urb->number_of_packets-1)) ? 1 : 0;
DBG(4, "mtk_qmu_insert_task\n");
mtk_qmu_insert_task(hw_ep->epnum, isRx,
pBuffer + offset, dwLength, 0, bIsIoc);
mtk_qmu_resume(hw_ep->epnum, isRx);
}
gpd_used_count = qmu_used_gpd_count(isRx, hw_ep->epnum);
if (!isRx) {
if (host_qmu_tx_max_active_isoc_gpd[hw_ep->epnum]
< gpd_used_count)
host_qmu_tx_max_active_isoc_gpd[hw_ep->epnum]
= gpd_used_count;
if (host_qmu_tx_max_number_of_pkts[hw_ep->epnum]
< urb->number_of_packets)
host_qmu_tx_max_number_of_pkts[hw_ep->epnum]
= urb->number_of_packets;
DBG_LIMIT(1,
"TXQ[%d], max_isoc gpd:%d, max_pkts:%d, active_dev:%d\n",
hw_ep->epnum,
host_qmu_tx_max_active_isoc_gpd
[hw_ep->epnum],
host_qmu_tx_max_number_of_pkts
[hw_ep->epnum],
mtk_host_active_dev_cnt);
#ifdef CONFIG_MTK_UAC_POWER_SAVING
DBG(1,
"mode:%d, activate:%d, ep:%d-%s, mtk_host_active_dev_cnt:%d\n",
low_power_timer_mode,
low_power_timer_activate,
hw_ep->epnum, isRx?"in":"out",
mtk_host_active_dev_cnt);
if (low_power_timer_mode
&& !low_power_timer_activate
&& mtk_host_active_dev_cnt == 1
&& hw_ep->epnum == ISOC_EP_START_IDX
&& usb_on_sram
&& audio_on_sram
&& (host_qmu_rx_max_active_isoc_gpd
[ISOC_EP_START_IDX] == 0)) {
if (urb->dev->speed == USB_SPEED_FULL)
low_power_timer_sleep
(gpd_used_count * 3 / 4);
else
low_power_timer_sleep
(gpd_used_count * 2 / 3 / 8);
}
#endif
} else {
if (host_qmu_rx_max_active_isoc_gpd[hw_ep->epnum]
< gpd_used_count)
host_qmu_rx_max_active_isoc_gpd[hw_ep->epnum]
= gpd_used_count;
if (host_qmu_rx_max_number_of_pkts[hw_ep->epnum]
< urb->number_of_packets)
host_qmu_rx_max_number_of_pkts[hw_ep->epnum]
= urb->number_of_packets;
DBG_LIMIT(1,
"RXQ[%d], max_isoc gpd:%d, max_pkts:%d, active_dev:%d\n",
hw_ep->epnum,
host_qmu_rx_max_active_isoc_gpd
[hw_ep->epnum],
host_qmu_rx_max_number_of_pkts
[hw_ep->epnum],
mtk_host_active_dev_cnt);
}
} else {
/* Must be the bulk transfer type */
QMU_WARN("non isoc\n");
pBuffer = urb->transfer_dma;
if (urb->transfer_buffer_length < QMU_RX_SPLIT_THRE) {
if (gpd_free_count < 1) {
DBG(0,
"gpd_free_count:%d, number_of_packets:%d\n",
gpd_free_count, urb->number_of_packets);
DBG(0, "%s:%d Error Here\n",
__func__, __LINE__);
return -ENOSPC;
}
DBG(4, "urb->transfer_buffer_length : %d\n",
urb->transfer_buffer_length);
dwLength = urb->transfer_buffer_length;
bIsIoc = 1;
mtk_qmu_insert_task(hw_ep->epnum, isRx,
pBuffer + offset, dwLength, 0, bIsIoc);
mtk_qmu_resume(hw_ep->epnum, isRx);
} else {
/*reuse isoc urb->unmber_of_packets*/
urb->number_of_packets =
((urb->transfer_buffer_length)
+ QMU_RX_SPLIT_BLOCK_SIZE-1)
/(QMU_RX_SPLIT_BLOCK_SIZE);
if (gpd_free_count < urb->number_of_packets) {
DBG(0,
"gpd_free_count:%d, number_of_packets:%d\n",
gpd_free_count, urb->number_of_packets);
DBG(0, "%s:%d Error Here\n"
, __func__, __LINE__);
return -ENOSPC;
}
for (i = 0; i < urb->number_of_packets; i++) {
offset = QMU_RX_SPLIT_BLOCK_SIZE*i;
dwLength = QMU_RX_SPLIT_BLOCK_SIZE;
/* If interrupt on complete ? */
bIsIoc = (i == (urb->number_of_packets-1)) ?
1 : 0;
dwLength = (i == (urb->number_of_packets-1)) ?
((urb->transfer_buffer_length)
% QMU_RX_SPLIT_BLOCK_SIZE) : dwLength;
if (dwLength == 0)
dwLength = QMU_RX_SPLIT_BLOCK_SIZE;
mtk_qmu_insert_task(hw_ep->epnum, isRx,
pBuffer + offset, dwLength, 0, bIsIoc);
mtk_qmu_resume(hw_ep->epnum, isRx);
}
}
}
/*sync register*/
mb();
DBG(4, "\n");
return 0;
}
EXPORT_SYMBOL(mtk_kick_CmdQ);
+32
View File
@@ -0,0 +1,32 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#ifndef _MUSB_QMU_H_
#define _MUSB_QMU_H_
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
#include "musb_core.h" /* for struct musb */
extern int musb_qmu_init(struct musb *musb);
extern void musb_qmu_exit(struct musb *musb);
extern void musb_kick_D_CmdQ(struct musb *musb, struct musb_request *request);
extern void musb_disable_q_all(struct musb *musb);
extern irqreturn_t musb_q_irq(struct musb *musb);
extern void musb_flush_qmu(u32 ep_num, u8 isRx);
extern void musb_restart_qmu(struct musb *musb, u32 ep_num, u8 isRx);
extern bool musb_is_qmu_stop(u32 ep_num, u8 isRx);
#ifndef CONFIG_MTK_MUSB_QMU_PURE_ZLP_SUPPORT
extern void musb_tx_zlp_qmu(struct musb *musb, u32 ep_num);
#endif
/*FIXME, not good layer present */
extern void mtk_qmu_enable(struct musb *musb, u8 EP_Num, u8 isRx);
extern void __iomem *qmu_base;
extern int mtk_kick_CmdQ(struct musb *musb,
int isRx, struct musb_qh *qh, struct urb *urb);
extern void musb_host_active_dev_add(unsigned int addr);
#endif
#endif
+464
View File
@@ -0,0 +1,464 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/time.h>
#include <linux/timer.h>
#include <asm/unaligned.h>
#include "musb_core.h"
static int h_pre_disable = 1;
module_param(h_pre_disable, int, 0644);
static void musb_host_check_disconnect(struct musb *musb)
{
u8 opstate = musb_readb(musb->mregs, MUSB_OPSTATE);
bool is_con = musb->port1_status & USB_PORT_STAT_CONNECTION;
if (opstate == MUSB_OPSTATE_HOST_WAIT_DEV && is_con) {
DBG(0, "disconnect when suspend");
musb->int_usb |= MUSB_INTR_DISCONNECT;
musb->xceiv->otg->state = OTG_STATE_A_HOST;
musb_interrupt(musb);
}
}
int musb_port_suspend(struct musb *musb, bool do_suspend)
{
struct usb_otg *otg = musb->xceiv->otg;
u8 power;
void __iomem *mbase = musb->mregs;
if (!is_host_active(musb))
return 0;
DBG(0, "%s\n", do_suspend ? "suspend" : "resume");
/* NOTE: this doesn't necessarily put PHY into low power mode,
* turning off its clock; that's a function of PHY integration and
* MUSB_POWER_ENSUSPEND. PHY may need a clock (sigh) to detect
* SE0 changing to connect (J) or wakeup (K) states.
*/
power = musb_readb(mbase, MUSB_POWER);
if (do_suspend) {
int retries = 10000;
if (power & MUSB_POWER_RESUME)
return -EBUSY;
if (!(power & MUSB_POWER_SUSPENDM)) {
power |= MUSB_POWER_SUSPENDM;
musb_writeb(mbase, MUSB_POWER, power);
/* Needed for OPT A tests */
power = musb_readb(mbase, MUSB_POWER);
while (power & MUSB_POWER_SUSPENDM) {
power = musb_readb(mbase, MUSB_POWER);
if (retries-- < 1)
break;
}
}
DBG(3, "Root port suspended, power %02x\n", power);
musb->port1_status |= USB_PORT_STAT_SUSPEND;
switch (musb->xceiv->otg->state) {
case OTG_STATE_A_HOST:
musb->xceiv->otg->state = OTG_STATE_A_SUSPEND;
musb->is_active = otg->host->b_hnp_enable;
if (musb->is_active)
mod_timer(&musb->otg_timer, jiffies
+ msecs_to_jiffies(OTG_TIME_A_AIDL_BDIS));
musb_platform_try_idle(musb, 0);
break;
case OTG_STATE_B_HOST:
musb->xceiv->otg->state = OTG_STATE_B_WAIT_ACON;
musb->is_active = otg->host->b_hnp_enable;
musb_platform_try_idle(musb, 0);
break;
default:
DBG(0, "bogus rh suspend? %s\n",
otg_state_string(musb->xceiv->otg->state));
}
} else if (power & MUSB_POWER_SUSPENDM) {
power &= ~MUSB_POWER_SUSPENDM;
power |= MUSB_POWER_RESUME;
musb_writeb(mbase, MUSB_POWER, power);
DBG(3, "Root port resuming, power %02x\n", power);
/* later, GetPortStatus will stop RESUME signaling */
musb->port1_status |= MUSB_PORT_STAT_RESUME;
musb->rh_timer = jiffies + msecs_to_jiffies(20);
}
return 0;
}
static void musb_port_reset(struct musb *musb, bool do_reset)
{
u8 power;
void __iomem *mbase = musb->mregs;
if (musb->xceiv->otg->state == OTG_STATE_B_IDLE) {
DBG(2, "HNP: Returning from HNP; no hub reset from b_idle\n");
musb->port1_status &= ~USB_PORT_STAT_RESET;
return;
}
if (!is_host_active(musb))
return;
/* NOTE: caller guarantees it will turn off the reset when
* the appropriate amount of time has passed
*/
power = musb_readb(mbase, MUSB_POWER);
if (do_reset) {
DBG(0, "force musb_platform_reset\n");
musb_platform_reset(musb);
mdelay(3);
/*
* If RESUME is set, we must make sure it stays minimum 20 ms.
* Then we must clear RESUME and wait a bit to let musb start
* generating SOFs. If we don't do this, OPT HS A 6.8 tests
* fail with "Error! Did not receive an SOF before suspend
* detected".
*/
if (power & MUSB_POWER_RESUME) {
while (time_before(jiffies, musb->rh_timer))
mdelay(1);
musb_writeb(mbase,
MUSB_POWER, power & ~MUSB_POWER_RESUME);
mdelay(1);
}
musb->ignore_disconnect = true;
power &= 0xf0;
musb_writeb(mbase, MUSB_POWER, power | MUSB_POWER_RESET);
musb->port1_status |= USB_PORT_STAT_RESET;
musb->port1_status &= ~USB_PORT_STAT_ENABLE;
musb->rh_timer = jiffies + msecs_to_jiffies(50);
} else {
DBG(4, "root port reset stopped\n");
musb_writeb(mbase, MUSB_POWER, power & ~MUSB_POWER_RESET);
musb->ignore_disconnect = false;
power = musb_readb(mbase, MUSB_POWER);
if (power & MUSB_POWER_HSMODE) {
DBG(4, "high-speed device connected\n");
musb->port1_status |= USB_PORT_STAT_HIGH_SPEED;
}
musb->port1_status &= ~USB_PORT_STAT_RESET;
musb->port1_status |= USB_PORT_STAT_ENABLE
| (USB_PORT_STAT_C_RESET << 16)
| (USB_PORT_STAT_C_ENABLE << 16);
usb_hcd_poll_rh_status(musb_to_hcd(musb));
musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
}
}
void musb_root_disconnect(struct musb *musb)
{
struct usb_otg *otg = musb->xceiv->otg;
musb->port1_status = USB_PORT_STAT_POWER
| (USB_PORT_STAT_C_CONNECTION << 16);
usb_hcd_poll_rh_status(musb_to_hcd(musb));
musb->is_active = 0;
if (h_pre_disable) {
/* when UMS device is detached,
* khubd need to wait for usb-storage
* thread to stop, then it will disable all endpoints,
* and clean up pending
* URBs. But if usb-storage is waiting for some URBs,
* it will never stop.
* So there is a dead lock:
* khubd need to end usb-storage then flush URB,
* but usb-storage need that URB to end itself.
* So we flush URB here first,
* this will cause usb-storage quit waiting and end
* itself when khubd asks.
*/
spin_unlock(&musb->lock);
musb_h_pre_disable(musb);
spin_lock(&musb->lock);
} else
DBG(0, "SKIP musb_h_pre_disable\n");
DBG(0, "host disconnect (%s)\n",
otg_state_string(musb->xceiv->otg->state));
switch (musb->xceiv->otg->state) {
case OTG_STATE_A_SUSPEND:
if (otg->host->b_hnp_enable) {
musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
musb->g.is_a_peripheral = 1;
break;
}
/* FALLTHROUGH */
case OTG_STATE_A_HOST:
musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
musb->is_active = 0;
break;
case OTG_STATE_A_WAIT_VFALL:
musb->xceiv->otg->state = OTG_STATE_B_IDLE;
break;
default:
DBG(0, "host disconnect (%s)\n",
otg_state_string(musb->xceiv->otg->state));
}
}
EXPORT_SYMBOL(musb_root_disconnect);
/*---------------------------------------------------------------------*/
/* Caller may or may not hold musb->lock */
int musb_hub_status_data(struct usb_hcd *hcd, char *buf)
{
struct musb *musb = hcd_to_musb(hcd);
int retval = 0;
/* called in_irq() via usb_hcd_poll_rh_status() */
if (musb->port1_status & 0xffff0000) {
*buf = 0x02;
retval = 1;
}
return retval;
}
int musb_hub_control(struct usb_hcd *hcd,
u16 typeReq, u16 wValue, u16 wIndex, char *buf, u16 wLength)
{
struct musb *musb = hcd_to_musb(hcd);
u32 temp;
int retval = 0;
unsigned long flags;
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
bool usb_active = true;
if (!mtk_usb_power) {
musb_platform_enable(musb);
usb_active = false;
DBG(1, "musb was in-active!!!\n");
}
#endif
musb_platform_prepare_clk(musb);
spin_lock_irqsave(&musb->lock, flags);
if (unlikely(!HCD_HW_ACCESSIBLE(hcd))) {
spin_unlock_irqrestore(&musb->lock, flags);
return -ESHUTDOWN;
}
/* hub features: always zero, setting is a NOP
* port features: reported, sometimes updated when host is active
* no indicators
*/
switch (typeReq) {
case ClearHubFeature:
case SetHubFeature:
switch (wValue) {
case C_HUB_OVER_CURRENT:
case C_HUB_LOCAL_POWER:
break;
default:
goto error;
}
break;
case ClearPortFeature:
if ((wIndex & 0xff) != 1)
goto error;
switch (wValue) {
case USB_PORT_FEAT_ENABLE:
break;
case USB_PORT_FEAT_SUSPEND:
musb_port_suspend(musb, false);
break;
case USB_PORT_FEAT_POWER:
if (!hcd->self.is_b_host)
musb_platform_set_vbus(musb, 0);
break;
case USB_PORT_FEAT_C_CONNECTION:
case USB_PORT_FEAT_C_ENABLE:
case USB_PORT_FEAT_C_OVER_CURRENT:
case USB_PORT_FEAT_C_RESET:
case USB_PORT_FEAT_C_SUSPEND:
break;
default:
goto error;
}
DBG(5, "clear feature %d\n", wValue);
musb->port1_status &= ~(1 << wValue);
break;
case GetHubDescriptor:
{
struct usb_hub_descriptor *desc = (void *)buf;
desc->bDescLength = 9;
desc->bDescriptorType = 0x29;
desc->bNbrPorts = 1;
desc->wHubCharacteristics = cpu_to_le16(0x0001
/* per-port power switching */
| 0x0010
/* no overcurrent reporting */
);
desc->bPwrOn2PwrGood = 5; /* msec/2 */
desc->bHubContrCurrent = 0;
/* workaround bogus struct definition */
desc->u.hs.DeviceRemovable[0] = 0x02; /* port 1 */
desc->u.hs.DeviceRemovable[1] = 0xff;
}
break;
case GetHubStatus:
temp = 0;
*(__le32 *) buf = cpu_to_le32(temp);
break;
case GetPortStatus:
if (wIndex != 1)
goto error;
/* finish RESET signaling? */
if ((musb->port1_status & USB_PORT_STAT_RESET)
&& time_after_eq(jiffies, musb->rh_timer))
musb_port_reset(musb, false);
/* finish RESUME signaling? */
if ((musb->port1_status & MUSB_PORT_STAT_RESUME)
&& time_after_eq(jiffies, musb->rh_timer)) {
u8 power;
power = musb_readb(musb->mregs, MUSB_POWER);
power &= ~MUSB_POWER_RESUME;
DBG(4, "root port resume stopped, power %02x\n"
, power);
musb_writeb(musb->mregs, MUSB_POWER, power);
/* ISSUE: DaVinci (RTL 1.300) disconnects after
* resume of high speed peripherals (but not full
* speed ones).
*/
musb->is_active = 1;
musb->port1_status &= ~(USB_PORT_STAT_SUSPEND
| MUSB_PORT_STAT_RESUME);
musb->port1_status |= USB_PORT_STAT_C_SUSPEND << 16;
usb_hcd_poll_rh_status(musb_to_hcd(musb));
/* NOTE: it might really be A_WAIT_BCON ... */
musb->xceiv->otg->state = OTG_STATE_A_HOST;
}
musb_host_check_disconnect(musb);
put_unaligned(cpu_to_le32(musb->port1_status
& ~MUSB_PORT_STAT_RESUME),
(__le32 *) buf);
/* port change status is more interesting */
DBG(0, "port status %08x,devctl=0x%x\n", musb->port1_status,
musb_readb(musb->mregs, MUSB_DEVCTL));
break;
case SetPortFeature:
if ((wIndex & 0xff) != 1)
goto error;
switch (wValue) {
case USB_PORT_FEAT_POWER:
/* NOTE: this controller has a strange state machine
* that involves "requesting sessions" according to
* magic side effects from incompletely-described
* rules about startup...
*
* This call is what really starts the host mode; be
* very careful about side effects if you reorder any
* initialization logic, e.g. for OTG, or change any
* logic relating to VBUS power-up.
*/
DBG(0, "try to call musb_start in virthub\n");
/* if (!hcd->self.is_b_host) */
/* musb_start(musb); */
break;
case USB_PORT_FEAT_RESET:
musb_port_reset(musb, true);
break;
case USB_PORT_FEAT_SUSPEND:
musb_port_suspend(musb, true);
break;
case USB_PORT_FEAT_TEST:
break; /* wz */
/* if (unlikely(is_host_active(musb))) */
/* goto error; */
wIndex >>= 8;
switch (wIndex) {
case 1:
pr_debug("TEST_J\n");
temp = MUSB_TEST_J;
break;
case 2:
pr_debug("TEST_K\n");
temp = MUSB_TEST_K;
break;
case 3:
pr_debug("TEST_SE0_NAK\n");
temp = MUSB_TEST_SE0_NAK;
break;
case 4:
pr_debug("TEST_PACKET\n");
temp = MUSB_TEST_PACKET;
musb_load_testpacket(musb);
break;
case 5:
pr_debug("TEST_FORCE_ENABLE\n");
temp = MUSB_TEST_FORCE_HOST
| MUSB_TEST_FORCE_HS;
musb_writeb(musb->mregs,
MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
break;
case 6:
pr_debug("TEST_FIFO_ACCESS\n");
temp = MUSB_TEST_FIFO_ACCESS;
break;
default:
goto error;
}
musb_writeb(musb->mregs, MUSB_TESTMODE, temp);
break;
default:
goto error;
}
DBG(5, "set feature %d\n", wValue);
musb->port1_status |= 1 << wValue;
break;
default:
error:
/* "protocol stall" on error */
retval = -EPIPE;
}
spin_unlock_irqrestore(&musb->lock, flags);
musb_platform_unprepare_clk(musb);
#ifdef CONFIG_MTK_MUSB_PORT0_LOWPOWER_MODE
if (!usb_active)
musb_platform_disable(musb);
#endif
return retval;
}
+657
View File
@@ -0,0 +1,657 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2018 MediaTek Inc.
*/
#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/export.h>
/* #include <asm/system.h> */
#include "musb_core.h"
#include "musbhsdma.h"
#ifdef CONFIG_OF
/* extern void __iomem *USB_BASE; */
#endif
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
#define USB_DMACNTL_ADDR36_EN (1 << 14)
#define USB_DMACNT_COUNT_MASK (0xffffff)
#define USB_DMACNT_HADDR_OFFSET (24)
#define USB_DMACNT_HADDR_MASK (0xf)
static u32 dma_extract_count(u32 count)
{
return (count & USB_DMACNT_COUNT_MASK);
}
static dma_addr_t dma_append_high_addr(
dma_addr_t addr,
void __iomem *mbase,
u8 bchannel)
{
u64 hbit;
hbit = musb_read_hsdma_count(mbase, bchannel);
hbit = hbit >> USB_DMACNT_HADDR_OFFSET;
addr = addr | (hbit << 32);
return addr;
}
#endif
static int dma_controller_start(struct dma_controller *c)
{
/* nothing to do */
return 0;
}
static void dma_channel_release(struct dma_channel *channel);
static int dma_controller_stop(struct dma_controller *c)
{
struct musb_dma_controller *controller = container_of(c,
struct musb_dma_controller, controller);
struct musb *musb = controller->private_data;
struct dma_channel *channel;
u8 bit;
if (controller->used_channels != 0) {
dev_notice(musb->controller, "Stopping DMA controller while channel active\n");
for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
if (controller->used_channels & (1 << bit)) {
channel = &controller->channel[bit].channel;
dma_channel_release(channel);
if (!controller->used_channels)
break;
}
}
}
return 0;
}
static struct dma_channel *dma_channel_allocate(struct dma_controller *c,
struct musb_hw_ep *hw_ep, u8 transmit)
{
struct musb_dma_controller *controller = container_of(c,
struct musb_dma_controller, controller);
struct musb *musb = controller->private_data;
struct musb_dma_channel *musb_channel = NULL;
struct dma_channel *channel = NULL;
u8 bit;
#ifdef CONFIG_MTK_MUSB_QMU_SUPPORT
/* reserve dma channel 0 for QMU */
for (bit = 1; bit < MUSB_HSDMA_CHANNELS; bit++) {
#else
for (bit = 0; bit < MUSB_HSDMA_CHANNELS; bit++) {
#endif
if (!(controller->used_channels & (1 << bit))) {
controller->used_channels |= (1 << bit);
musb_channel = &(controller->channel[bit]);
musb_channel->controller = controller;
musb_channel->idx = bit;
if (musb->is_host) {
musb_channel->epnum = hw_ep->epnum;
} else {
if (transmit) {
/* dma irq will use
* this member to get the hw ep.
*/
musb_channel->epnum =
hw_ep->ep_in.current_epnum;
} else
/* after mapping,
* hw ep num eques to the current num
*/
musb_channel->epnum =
hw_ep->ep_out.current_epnum;
}
musb_channel->transmit = transmit;
channel = &(musb_channel->channel);
channel->private_data = musb_channel;
channel->status = MUSB_DMA_STATUS_FREE;
channel->max_len = 0x100000;
/* Tx => mode 1; Rx => mode 0 */
channel->desired_mode = transmit;
channel->actual_len = 0;
break;
}
}
return channel;
}
static void dma_channel_release(struct dma_channel *channel)
{
struct musb_dma_channel *musb_channel = channel->private_data;
u8 bchannel = musb_channel->idx;
void __iomem *mbase = musb_channel->controller->base;
musb_writew(mbase, MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
MUSB_HSDMA_CONTROL), 0);
channel->actual_len = 0;
musb_channel->start_addr = 0;
musb_channel->len = 0;
musb_channel->controller->used_channels &= ~(1 << musb_channel->idx);
channel->status = MUSB_DMA_STATUS_UNKNOWN;
}
static void configure_channel(struct dma_channel *channel,
u16 packet_sz, u8 mode, dma_addr_t dma_addr, u32 len)
{
struct musb_dma_channel *musb_channel = channel->private_data;
struct musb_dma_controller *controller = musb_channel->controller;
void __iomem *mbase = controller->base;
u8 bchannel = musb_channel->idx;
u16 csr = 0;
DBG(4, "%p, pkt_sz %d, addr 0x%x, len %d, mode %d\n",
channel, packet_sz, (unsigned int)dma_addr, len, mode);
if (mode) {
csr |= 1 << MUSB_HSDMA_MODE1_SHIFT;
if (len < packet_sz) {
DBG(0, "%s:%d Error Here\n", __func__, __LINE__);
return;
}
}
csr |= MUSB_HSDMA_BURSTMODE_INCR16 << MUSB_HSDMA_BURSTMODE_SHIFT;
csr |= (musb_channel->epnum << MUSB_HSDMA_ENDPOINT_SHIFT)
| (1 << MUSB_HSDMA_ENABLE_SHIFT)
| (1 << MUSB_HSDMA_IRQENABLE_SHIFT)
| (musb_channel->transmit ? (1 << MUSB_HSDMA_TRANSMIT_SHIFT)
: 0);
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
{
u32 val;
/* enable 36-bit support */
csr |= USB_DMACNTL_ADDR36_EN;
/* low address */
musb_write_hsdma_addr(mbase, bchannel, dma_addr & 0xFFFFFFFF);
/* count */
val = len & USB_DMACNT_COUNT_MASK;
/* high address */
val |= (((dma_addr >> 32) & USB_DMACNT_HADDR_MASK)
<< USB_DMACNT_HADDR_OFFSET);
musb_write_hsdma_count(mbase, bchannel, val);
}
#else
/* address/count */
musb_write_hsdma_addr(mbase, bchannel, dma_addr);
musb_write_hsdma_count(mbase, bchannel, len);
#endif
/* control (this should start things) */
musb_writew(mbase,
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL), csr);
DBG(5, "MUSB:DMA channel %d control reg is %x\n",
bchannel, musb_readw(mbase,
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL)));
}
static int dma_channel_program(struct dma_channel *channel,
u16 packet_sz, u8 mode, dma_addr_t dma_addr, u32 len)
{
struct musb_dma_channel *musb_channel = channel->private_data;
struct musb_dma_controller *controller = musb_channel->controller;
struct musb *musb = controller->private_data;
DBG(2, "ep%d-%s pkt_sz %d, dma_addr 0x%x length %d, mode %d\n",
musb_channel->epnum,
musb_channel->transmit ?
"Tx" : "Rx", packet_sz, (unsigned int)dma_addr, len, mode);
if (channel->status == MUSB_DMA_STATUS_UNKNOWN ||
channel->status == MUSB_DMA_STATUS_BUSY) {
DBG(0, "%s:%d Error Here\n", __func__, __LINE__);
return -1;
}
/* Let targets check/tweak the arguments */
if (musb->ops->adjust_channel_params) {
int ret = musb->ops->adjust_channel_params(channel,
packet_sz, &mode, &dma_addr, &len);
if (ret)
return ret;
}
#ifdef NEVER
/*
* The DMA engine in RTL1.8 and above cannot handle
* DMA addresses that are not aligned to a 4 byte boundary.
* It ends up masking the last two bits of the address
* programmed in DMA_ADDR.
*
* Fail such DMA transfers, so that the backup PIO mode
* can carry out the transfer
*/
if ((musb->hwvers >= MUSB_HWVERS_1800) && (dma_addr % 4))
return false;
#endif
channel->actual_len = 0;
musb_channel->start_addr = dma_addr;
musb_channel->len = len;
musb_channel->max_packet_sz = packet_sz;
channel->status = MUSB_DMA_STATUS_BUSY;
configure_channel(channel, packet_sz, mode, dma_addr, len);
return true;
}
static int dma_channel_abort(struct dma_channel *channel)
{
struct musb_dma_channel *musb_channel = channel->private_data;
void __iomem *mbase = musb_channel->controller->base;
u8 bchannel = musb_channel->idx;
int offset;
u16 csr;
if (channel->status == MUSB_DMA_STATUS_BUSY) {
if (musb_channel->transmit) {
offset =
MUSB_EP_OFFSET(musb_channel->epnum, MUSB_TXCSR);
/*
* The programming guide says that we must clear
* the DMAENAB bit before the DMAMODE bit...
*/
csr = musb_readw(mbase, offset);
csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
musb_writew(mbase, offset, csr);
csr &= ~MUSB_TXCSR_DMAMODE;
musb_writew(mbase, offset, csr);
} else {
offset = MUSB_EP_OFFSET
(musb_channel->epnum, MUSB_RXCSR);
csr = musb_readw(mbase, offset);
csr &= ~(MUSB_RXCSR_AUTOCLEAR |
MUSB_RXCSR_DMAENAB |
MUSB_RXCSR_DMAMODE);
musb_writew(mbase, offset, csr);
}
musb_writew(mbase,
MUSB_HSDMA_CHANNEL_OFFSET(bchannel,
MUSB_HSDMA_CONTROL), 0);
musb_write_hsdma_addr(mbase, bchannel, 0);
musb_write_hsdma_count(mbase, bchannel, 0);
channel->status = MUSB_DMA_STATUS_FREE;
}
return 0;
}
static int dma_channel_pause(struct dma_channel *channel)
{
/*
* Probably nothing to be done here. This is needed
* only for certain DMA controllers which require
* the DMA channel to be paused to get correct DMA
* transfer residue
*/
return 0;
}
static int dma_channel_resume(struct dma_channel *channel)
{
/* Probably nothing to be done here */
return 0;
}
static int dma_channel_tx_status(struct dma_channel *channel)
{
struct musb_dma_channel *musb_channel = channel->private_data;
void __iomem *mbase = musb_channel->controller->base;
u8 bchannel = musb_channel->idx;
u32 count, residue;
dma_addr_t addr;
/*
* Get the number of bytes left to be transferred over
* DMA
* The MUSB spec mentions "The DMA controller ADDR register
* will have been incremented as packets were unloaded from
* the fifo, the processor can determine the size of the
* transfer by comparing the current value of ADDR against
* the start address of the memory buffer
*/
/* residue = musb_read_hsdma_count(mbase, bchannel); */
addr = musb_read_hsdma_addr(mbase, bchannel);
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
addr = dma_append_high_addr(addr, mbase, bchannel);
#endif
count = addr - musb_channel->start_addr;
residue = channel->prog_len - count;
return residue;
}
static int dma_channel_check_residue(struct dma_channel *channel, u32 residue)
{
int status;
/* In cases where we know the transfer length and were expecting
* a DMA completion we could get into the DMA busy condition
* here if the next packet is short and the EP interrupt occurs
* before we receive dma_completion interrupt for current transfer
* Wait for dma_completion. MUSB will interrupt us again for this
* short packet when we clear the DMA bits
*/
if (!residue) {
/* Wait for DMA completion */
status = -EINPROGRESS;
} else if (residue == channel->prog_len) {
/* Nothing transferred over DMA? */
/* WARN_ON(1); */
status = -EINVAL;
} else {
/* residue looks OK */
status = 0;
}
return status;
}
irqreturn_t dma_controller_irq(int irq, void *private_data)
{
struct musb_dma_controller *controller = private_data;
struct musb *musb = controller->private_data;
struct musb_dma_channel *musb_channel;
struct dma_channel *channel;
void __iomem *mbase = controller->base;
irqreturn_t retval = IRQ_NONE;
unsigned long flags;
u8 bchannel;
u8 int_hsdma;
u32 count;
u16 csr;
dma_addr_t addr;
spin_lock_irqsave(&musb->lock, flags);
/* musb_read_clear_dma_interrupt */
int_hsdma = musb_readb(musb->mregs, MUSB_HSDMA_INTR);
/* make sure int_hsdma up to date before W1C */
mb();
musb_writeb(musb->mregs, MUSB_HSDMA_INTR, int_hsdma);
/* musb_read_clear_dma_interrupt */
if (!int_hsdma) {
DBG(2, "spurious DMA irq\n");
for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
musb_channel = (struct musb_dma_channel *)
&(controller->channel[bchannel]);
channel = &musb_channel->channel;
if (channel->status == MUSB_DMA_STATUS_BUSY) {
count = musb_read_hsdma_count(mbase, bchannel);
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
count = dma_extract_count(count);
#endif
if (count == 0)
int_hsdma |= (1 << bchannel);
}
}
DBG(2, "int_hsdma = 0x%x\n", int_hsdma);
if (!int_hsdma)
goto done;
}
for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
if (int_hsdma & (1 << bchannel)) {
musb_channel = (struct musb_dma_channel *)
&(controller->channel[bchannel]);
channel = &musb_channel->channel;
DBG(1, "MUSB:DMA channel %d interrupt\n", bchannel);
csr = musb_readw(mbase,
MUSB_HSDMA_CHANNEL_OFFSET
(bchannel, MUSB_HSDMA_CONTROL));
if (csr & (1 << MUSB_HSDMA_BUSERROR_SHIFT)) {
musb_channel->channel.status
= MUSB_DMA_STATUS_BUS_ABORT;
} else {
u8 devctl;
addr = musb_read_hsdma_addr(mbase, bchannel);
#ifdef CONFIG_MTK_MUSB_DRV_36BIT
addr =
dma_append_high_addr
(addr, mbase, bchannel);
#endif
channel->actual_len = addr
- musb_channel->start_addr;
#ifdef NEVER
channel->actual_len =
musb_readl(mbase, USB_DMA_REALCOUNT(bchannel));
#endif
DBG(2,
"[MUSB] channel %d ch %p, 0x%p -> 0x%p (%zu / %d) %s\n",
bchannel,
channel,
(void *)(uintptr_t)
musb_channel->start_addr,
(void *)(uintptr_t)
addr, channel->actual_len,
musb_channel->len,
(channel->actual_len
< musb_channel->len)
? "=> reconfig 0" : "=> complete");
devctl = musb_readb(mbase, MUSB_DEVCTL);
channel->status = MUSB_DMA_STATUS_FREE;
/* completed */
if ((devctl & MUSB_DEVCTL_HM)
&& (musb_channel->transmit)
&& ((channel->desired_mode == 0)
|| (channel->actual_len &
(musb_channel->max_packet_sz - 1)))
) {
u8 epnum = musb_channel->epnum;
int offset = MUSB_EP_OFFSET(epnum,
MUSB_TXCSR);
u16 txcsr;
host_tx_refcnt_inc(epnum);
/*
* The programming guide says that we
* must clear DMAENAB before DMAMODE.
*/
musb_ep_select(mbase, epnum);
txcsr = musb_readw(mbase, offset);
txcsr &=
~(MUSB_TXCSR_DMAENAB
| MUSB_TXCSR_AUTOSET);
musb_writew(mbase, offset, txcsr);
/* Send out the packet */
txcsr &= ~MUSB_TXCSR_DMAMODE;
txcsr |= MUSB_TXCSR_TXPKTRDY;
musb_writew(mbase, offset, txcsr);
if (musb_host_db_workaround2
&& musb->is_host)
wait_tx_done(epnum, 2000000000);
} else
musb_dma_completion(musb,
musb_channel->epnum,
musb_channel->transmit);
}
}
}
DBG(4, "MUSB: DMA interrupt completino on ep\n");
retval = IRQ_HANDLED;
done:
spin_unlock_irqrestore(&musb->lock, flags);
return retval;
}
void dma_controller_destroy(struct dma_controller *c)
{
struct musb_dma_controller *controller = container_of(c,
struct musb_dma_controller, controller);
if (!controller)
return;
if (controller->irq)
free_irq(controller->irq, c);
kfree(controller);
}
struct dma_controller *dma_controller_create
(struct musb *musb, void __iomem *base)
{
struct musb_dma_controller *controller;
int irq = musb->dma_irq;
if ((irq <= 0) && (irq != SHARE_IRQ)) {
DBG(0, "[MUSB] No DMA interrupt line!\n");
return NULL;
}
controller = kzalloc(sizeof(*controller), GFP_KERNEL);
if (!controller)
return NULL;
controller->channel_count = MUSB_HSDMA_CHANNELS;
controller->private_data = musb;
controller->base = base;
controller->controller.start = dma_controller_start;
controller->controller.stop = dma_controller_stop;
controller->controller.channel_alloc = dma_channel_allocate;
controller->controller.channel_release = dma_channel_release;
controller->controller.channel_program = dma_channel_program;
controller->controller.channel_abort = dma_channel_abort;
controller->controller.channel_pause = dma_channel_pause;
controller->controller.channel_resume = dma_channel_resume;
controller->controller.tx_status = dma_channel_tx_status;
controller->controller.check_residue = dma_channel_check_residue;
if (irq != SHARE_IRQ) {
if (request_irq(irq, dma_controller_irq, 0,
dev_name(musb->controller), &controller->controller)) {
DBG(0, "request_irq %d failed!\n", irq);
dma_controller_destroy(&controller->controller);
return NULL;
}
}
controller->irq = irq;
return &controller->controller;
}
#define MUSB_HSDMA_REAL_COUNT 0x80
#define USB_HSDMA_CHANNEL_OFFSET(_bchannel, _offset) \
(MUSB_HSDMA_BASE + (_bchannel << 4) + _offset)
#define usb_read_hsdma_addr(mbase, bchannel) \
musb_readl(mbase, \
USB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS))
#define usb_read_hsdma_ctrl(mbase, bchannel) \
musb_readb(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_CONTROL))
#define usb_read_hsdma_count(mbase, bchannel) \
musb_readl(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT))
#define usb_read_hsdma_real_count(mbase, bchannel) \
musb_readl(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_REAL_COUNT))
u8 USB_DMA_status(u8 *pbDMAen, u8 *pbDMAdir)
{
u8 bchannel;
u8 bDMAen = 0;
u8 bDMAdir = 0;
#ifdef CONFIG_OF
for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
bDMAen |=
(usb_read_hsdma_ctrl(mtk_musb->mregs, bchannel) & 0x01)
<< bchannel;
bDMAdir |=
((usb_read_hsdma_ctrl(mtk_musb->mregs, bchannel) & 0x02) >> 1)
<< bchannel;
}
#else
void __iomem *base = USB_BASE;
for (bchannel = 0; bchannel < MUSB_HSDMA_CHANNELS; bchannel++) {
bDMAen |= (usb_read_hsdma_ctrl(base, bchannel) & 0x01)
<< bchannel;
bDMAdir |= ((usb_read_hsdma_ctrl(base, bchannel) & 0x02) >> 1)
<< bchannel;
}
#endif
if (pbDMAen)
*pbDMAen = bDMAen;
if (pbDMAdir)
*pbDMAdir = bDMAdir;
if (bDMAen > 0)
return 1;
else
return 0;
}
EXPORT_SYMBOL(USB_DMA_status);
u32 USB_DMA_address(u32 *len, u8 bchannel)
{
#ifdef CONFIG_OF
/* void __iomem *base = USB_BASE; */
if (len) {
*len =
usb_read_hsdma_count(mtk_musb->mregs,
bchannel) + usb_read_hsdma_real_count
(mtk_musb->mregs, bchannel);
}
return (usb_read_hsdma_addr(mtk_musb->mregs, bchannel) -
usb_read_hsdma_real_count(mtk_musb->mregs, bchannel));
#else
void __iomem *base = (void *)USB_BASE;
if (len) {
*len =
usb_read_hsdma_count(base, bchannel) +
usb_read_hsdma_real_count(base, bchannel);
}
return usb_read_hsdma_addr(base, bchannel) -
usb_read_hsdma_real_count(base, bchannel);
#endif
}
EXPORT_SYMBOL(USB_DMA_address);
+69
View File
@@ -0,0 +1,69 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* Copyright (C) 2017 MediaTek Inc.
*/
#define MUSB_HSDMA_BASE 0x200
#define MUSB_HSDMA_INTR (MUSB_HSDMA_BASE + 0)
#define MUSB_HSDMA_CONTROL 0x4
#define MUSB_HSDMA_ADDRESS 0x8
#define MUSB_HSDMA_COUNT 0xc
#define MUSB_HSDMA_CHANNEL_OFFSET(_bchannel, _offset) \
(MUSB_HSDMA_BASE + (_bchannel << 4) + _offset)
#define musb_read_hsdma_addr(mbase, bchannel) \
musb_readl(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS))
#define musb_write_hsdma_addr(mbase, bchannel, addr) \
musb_writel(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_ADDRESS), \
addr)
#define musb_read_hsdma_count(mbase, bchannel) \
musb_readl(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT))
#define musb_write_hsdma_count(mbase, bchannel, len) \
musb_writel(mbase, \
MUSB_HSDMA_CHANNEL_OFFSET(bchannel, MUSB_HSDMA_COUNT), \
len)
/* control register (16-bit): */
#define MUSB_HSDMA_ENABLE_SHIFT 0
#define MUSB_HSDMA_TRANSMIT_SHIFT 1
#define MUSB_HSDMA_MODE1_SHIFT 2
#define MUSB_HSDMA_IRQENABLE_SHIFT 3
#define MUSB_HSDMA_ENDPOINT_SHIFT 4
#define MUSB_HSDMA_BUSERROR_SHIFT 8
#define MUSB_HSDMA_BURSTMODE_SHIFT 9
#define MUSB_HSDMA_BURSTMODE (3 << MUSB_HSDMA_BURSTMODE_SHIFT)
#define MUSB_HSDMA_BURSTMODE_UNSPEC 0
#define MUSB_HSDMA_BURSTMODE_INCR4 1
#define MUSB_HSDMA_BURSTMODE_INCR8 2
#define MUSB_HSDMA_BURSTMODE_INCR16 3
#define MUSB_HSDMA_CHANNELS 8
struct musb_dma_controller;
struct musb_dma_channel {
struct dma_channel channel;
struct musb_dma_controller *controller;
dma_addr_t start_addr;
u32 len;
u16 max_packet_sz;
u8 idx;
u8 epnum;
u8 transmit;
};
struct musb_dma_controller {
struct dma_controller controller;
struct musb_dma_channel channel[MUSB_HSDMA_CHANNELS];
void *private_data;
void __iomem *base;
u8 channel_count;
u8 used_channels;
u8 irq;
};