blob: 5050a433ee96d020fc195422a482e53393fdbda2 [file]
/*
* Amlogic Meson6 and Meson8 DWMAC glue layer
*
* Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/device.h>
#include <linux/ethtool.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/stmmac.h>
#include "stmmac_platform.h"
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
#include <linux/bitops.h>
#include <linux/of_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/gpio/consumer.h>
#include "dwmac1000.h"
#include "dwmac_dma.h"
#include <linux/amlogic/pwr_ctrl.h>
#include <linux/gpio/consumer.h>
#endif
#define ETHMAC_SPEED_100 BIT(1)
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
/*if not g12a use genphy driver*/
/* if it's internal phy we will shutdown analog*/
static unsigned int is_internal_phy;
/* Ethernet register for G12A PHY */
#define REG_ETH_REG1_OFFSET 0x4
#define ETH_PLL_CTL0 0x44
#define ETH_PLL_CTL1 0x48
#define ETH_PLL_CTL2 0x4C
#define ETH_PLL_CTL3 0x50
#define ETH_PLL_CTL4 0x54
#define ETH_PLL_CTL5 0x58
#define ETH_PLL_CTL6 0x5C
#define ETH_PLL_CTL7 0x60
#define ETH_PHY_CNTL0 0x80
#define ETH_PHY_CNTL1 0x84
#define ETH_PHY_CNTL2 0x88
#define ETH_USE_EPHY BIT(5)
#define ETH_EPHY_FROM_MAC BIT(6)
/*TODO wol function for exphy*/
unsigned int support_internal_phy_wol;
unsigned int support_external_phy_wol;
static unsigned int support_mac_wol;
static unsigned int analog_ver;
#endif
struct meson_dwmac {
struct device *dev;
void __iomem *reg;
};
static void meson6_dwmac_fix_mac_speed(void *priv, unsigned int speed)
{
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
#else
struct meson_dwmac *dwmac = priv;
unsigned int val;
val = readl(dwmac->reg);
switch (speed) {
case SPEED_10:
val &= ~ETHMAC_SPEED_100;
break;
case SPEED_100:
val |= ETHMAC_SPEED_100;
break;
}
writel(val, dwmac->reg);
#endif
}
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
static int dwmac_meson_cfg_ctrl(void __iomem *base_addr)
{
void __iomem *ETH_PHY_config_addr = base_addr;
/*config phyid should between a 0~0xffffffff*/
/*please don't use 44000181, this has been used by internal phy*/
writel(0x33010180, ETH_PHY_config_addr + ETH_PHY_CNTL0);
/*use_phy_smi | use_phy_ip | co_clkin from eth_phy_top*/
writel(0x260, ETH_PHY_config_addr + ETH_PHY_CNTL2);
/*led signal is inverted*/
writel(0x41054147, ETH_PHY_config_addr + ETH_PHY_CNTL1);
writel(0x41014147, ETH_PHY_config_addr + ETH_PHY_CNTL1);
writel(0x41054147, ETH_PHY_config_addr + ETH_PHY_CNTL1);
/*wait phy to reset cause Power Up Reset need 5.2~2.6 ms*/
mdelay(10);
return 0;
}
static int dwmac_meson_cfg_pll(void __iomem *base_addr,
struct platform_device *pdev)
{
void __iomem *ETH_PHY_config_addr = base_addr;
u32 pll_val[3] = {0};
of_property_read_u32_array(pdev->dev.of_node, "pll_val",
pll_val, sizeof(pll_val) / sizeof(u32));
//printk("wzh pll %x %x %x", pll_val[0], pll_val[1], pll_val[2]);
writel(pll_val[0] | 0x30000000, ETH_PHY_config_addr + ETH_PLL_CTL0);
writel(pll_val[1], ETH_PHY_config_addr + ETH_PLL_CTL1);
writel(pll_val[2], ETH_PHY_config_addr + ETH_PLL_CTL2);
writel(0x00000000, ETH_PHY_config_addr + ETH_PLL_CTL3);
usleep_range(100, 200);
writel(pll_val[0] | 0x10000000, ETH_PHY_config_addr + ETH_PLL_CTL0);
return 0;
}
static int dwmac_meson_cfg_analog(void __iomem *base_addr,
struct platform_device *pdev)
{
void __iomem *ETH_PHY_config_addr = base_addr;
u32 analog_val[3] = {0};
of_property_read_u32_array
(pdev->dev.of_node, "analog_val",
analog_val,
sizeof(analog_val) / sizeof(u32));
//printk("wzh analog %x %x %x", analog_val[0],
//analog_val[1], analog_val[2]);
/*Analog*/
writel(analog_val[0], ETH_PHY_config_addr + ETH_PLL_CTL5);
writel(analog_val[1], ETH_PHY_config_addr + ETH_PLL_CTL6);
writel(analog_val[2], ETH_PHY_config_addr + ETH_PLL_CTL7);
return 0;
}
struct gpio_desc *gdesc_x18;
struct gpio_desc *gdesc_x19;
static struct workqueue_struct *moniter_led_wq;
static struct delayed_work moniter_led_worker;
//extern int led_link;
unsigned int led_time_s;
unsigned int led_time_c;
unsigned int led_state;
unsigned int led_base;
void turn_active_on(void)
{
if (led_base)
gpiod_direction_output(gdesc_x19, 0);
else
gpiod_direction_input(gdesc_x19);
}
void turn_active_off(void)
{
if (led_base)
gpiod_direction_input(gdesc_x19);
else
gpiod_direction_output(gdesc_x19, 0);
}
void turn_link_on(void)
{
if (led_base)
gpiod_direction_output(gdesc_x18, 0);
else
gpiod_direction_input(gdesc_x18);
}
void turn_link_off(void)
{
if (led_base)
gpiod_direction_input(gdesc_x18);
else
gpiod_direction_output(gdesc_x18, 0);
}
static void moniter_led_handler(struct work_struct *work)
{
if (led_link) {
if (led_state) {
turn_active_off();
led_state = 0;
} else {
turn_active_on();
led_state = 1;
}
turn_link_on();
} else {
turn_active_off();
turn_link_off();
}
if (led_state)
queue_delayed_work(moniter_led_wq,
&moniter_led_worker, led_time_s);
else
queue_delayed_work(moniter_led_wq,
&moniter_led_worker, led_time_c);
}
static void c1_init_led(void)
{
moniter_led_wq = create_singlethread_workqueue("eth_moniter_led_wq");
INIT_DELAYED_WORK(&moniter_led_worker, moniter_led_handler);
queue_delayed_work(moniter_led_wq, &moniter_led_worker, led_time_s);
}
void __iomem *phy_analog_config_addr;
unsigned int tx_amp_bl2;
/*for newer then g12a use this dts architecture for dts*/
static void __iomem *g12a_network_interface_setup(struct platform_device *pdev)
{
struct device_node *np = pdev->dev.of_node;
struct device *dev = &pdev->dev;
struct pinctrl *pin_ctl;
struct resource *res = NULL;
u32 mc_val;
u32 cali_val;
u32 SW_led;
void __iomem *addr = NULL;
void __iomem *REG_ETH_reg0_addr = NULL;
void __iomem *ETH_PHY_config_addr = NULL;
void __iomem *tx_amp_src = NULL;
struct gpio_desc *gdesc;
u32 internal_phy = 0;
is_internal_phy = 0;
/*map PRG_ETH_REG */
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eth_cfg");
if (!res) {
dev_err(&pdev->dev, "Unable to get resource(%d)\n", __LINE__);
return NULL;
}
addr = devm_ioremap_resource(dev, res);
if (IS_ERR(addr)) {
dev_err(&pdev->dev, "Unable to map base (%d)\n", __LINE__);
return NULL;
}
REG_ETH_reg0_addr = addr;
/*map ETH_PLL address*/
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eth_pll");
if (!res) {
dev_err(&pdev->dev, "Unable to get resource(%d)\n", __LINE__);
return NULL;
}
addr = devm_ioremap_resource(dev, res);
if (IS_ERR(addr)) {
dev_err(&pdev->dev, "Unable to map clk base (%d)\n", __LINE__);
return NULL;
}
ETH_PHY_config_addr = addr;
phy_analog_config_addr = addr;
res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "tx_amp_src");
if (!res) {
pr_info("tx_amp_src not setup\n");
} else {
tx_amp_src = devm_ioremap_resource(dev, res);
if (IS_ERR(addr)) {
dev_err(&pdev->dev,
"cat't map tx_amp (%d)\n", __LINE__);
return NULL;
}
tx_amp_bl2 = readl(tx_amp_src);
pr_info("wzh tx_Amp 0x%x\n", tx_amp_bl2);
}
/*PRG_ETH_REG0*/
if (of_property_read_u32(np, "mc_val", &mc_val))
pr_info("Miss mc_val for REG0\n");
else
writel(mc_val, REG_ETH_reg0_addr);
/*read phy option*/
if (of_property_read_u32(np, "internal_phy", &internal_phy) != 0) {
pr_info("Dts miss internal_phy item\n");
return REG_ETH_reg0_addr;
}
is_internal_phy = internal_phy;
/* Config G12A internal PHY */
if (internal_phy) {
/*mac wol*/
if (of_property_read_u32(np, "mac_wol",
&support_mac_wol))
pr_info("MAC wol not set\n");
else
pr_info("MAC wol :got wol %d .set it\n",
support_mac_wol);
/*PLL*/
dwmac_meson_cfg_pll(ETH_PHY_config_addr, pdev);
dwmac_meson_cfg_analog(ETH_PHY_config_addr, pdev);
dwmac_meson_cfg_ctrl(ETH_PHY_config_addr);
/*patch led*/
// pin_ctl = devm_pinctrl_get_select
// (&pdev->dev, "internal_eth_pins");
gdesc_x18 = gpiod_get(&pdev->dev, "GPIOX18_pin",
GPIOD_FLAGS_BIT_DIR_OUT);
gdesc_x19 = gpiod_get(&pdev->dev, "GPIOX19_pin",
GPIOD_FLAGS_BIT_DIR_OUT);
if ((IS_ERR(gdesc_x19)) || IS_ERR(gdesc_x18))
pr_info("Miss led GPIO setting\n");
if (of_property_read_u32(np, "SW_led", &SW_led))
SW_led = 0;
if (SW_led) {
pr_info("turn on software leds\n");
if (of_property_read_u32(np, "led_time_s",
&led_time_s)) {
pr_info("miss led_time_s setting\n");
led_time_s = 100;
}
if (of_property_read_u32(np, "led_time_c",
&led_time_c)) {
pr_info("miss led_time_c setting\n");
led_time_c = 100;
}
if (of_property_read_u32(np, "led_base",
&led_base)) {
pr_info("miss led_base setting\n");
led_base = 1;
}
c1_init_led();
} else {
pin_ctl = devm_pinctrl_get_select
(&pdev->dev, "internal_eth_pins");
pr_info("turn off software leds\n");
}
return REG_ETH_reg0_addr;
}
/*config extern phy*/
if (internal_phy == 0) {
/*reset phy*/
gdesc = devm_gpiod_get(&pdev->dev, "rst_pin",
GPIOD_FLAGS_BIT_DIR_OUT);
gpiod_direction_output(gdesc, 0);
mdelay(10);
gpiod_direction_output(gdesc, 1);
mdelay(40);
/* only exphy support wol since g12a*/
/*we enable/disable wol with item in dts with "wol=<1>"*/
if (of_property_read_u32(np, "wol",
&support_external_phy_wol))
pr_debug("exphy wol not set\n");
else
pr_debug("exphy Ethernet :got wol %d .set it\n",
support_external_phy_wol);
/*switch to extern phy*/
/*analog == 2 means c2, which not include config part*/
if (analog_ver != 2) {
pr_info("c2 should not in\n");
writel(0x0, ETH_PHY_config_addr + ETH_PHY_CNTL2);
}
/*set PRG_ETH_REG1 for exphy delay*/
if (of_property_read_u32(np, "cali_val", &cali_val))
pr_info("Not set cali_val for REG1\n");
else
writel(cali_val, REG_ETH_reg0_addr +
REG_ETH_REG1_OFFSET);
if (mc_val & 0x1)
pin_ctl = devm_pinctrl_get_select
(&pdev->dev, "external_eth_rgmii_pins");
else
pin_ctl = devm_pinctrl_get_select
(&pdev->dev, "external_eth_pins");
return REG_ETH_reg0_addr;
}
pr_info("should not happen\n");
return REG_ETH_reg0_addr;
}
//void __iomem *phy_analog_config_addr;
static int dwmac_meson_disable_analog(struct device *dev)
{
if (analog_ver == 0) {
writel(0x00000000, phy_analog_config_addr + 0x0);
writel(0x003e0000, phy_analog_config_addr + 0x4);
writel(0x12844008, phy_analog_config_addr + 0x8);
writel(0x0800a40c, phy_analog_config_addr + 0xc);
writel(0x00000000, phy_analog_config_addr + 0x10);
writel(0x031d161c, phy_analog_config_addr + 0x14);
writel(0x00001683, phy_analog_config_addr + 0x18);
writel(0x09c0040a, phy_analog_config_addr + 0x44);
}
if (analog_ver == 1) {
/*todo*/
/*todo*/
}
return 0;
}
static int dwmac_meson_recover_analog(struct device *dev)
{
pr_info("recover %p\n", phy_analog_config_addr);
if (analog_ver == 0) {
writel(0x19c0040a, phy_analog_config_addr + 0x44);
writel(0x0, phy_analog_config_addr + 0x4);
}
if (analog_ver == 1) {
/*todo*/
/*todo*/
}
return 0;
}
static int meson6_dwmac_suspend(struct device *dev)
{
int ret;
/*shudown internal phy analog*/
struct pinctrl *pin_ctrl;
struct pinctrl_state *turnoff_tes = NULL;
/*shudown internal phy analog*/
pr_info("suspend inter = %d\n", is_internal_phy);
if ((is_internal_phy) && support_mac_wol == 0) {
/*turn off led*/
pin_ctrl = devm_pinctrl_get(dev);
if (IS_ERR_OR_NULL(pin_ctrl)) {
/*led will not work*/
pr_info("pinctrl is null\n");
} else {
turnoff_tes = pinctrl_lookup_state
(pin_ctrl, "internal_gpio_pins");
if (IS_ERR_OR_NULL(turnoff_tes))
pr_info("Not support gpio low\n");
else
pinctrl_select_state(pin_ctrl, turnoff_tes);
devm_pinctrl_put(pin_ctrl);
pin_ctrl = NULL;
}
dwmac_meson_disable_analog(dev);
}
ret = stmmac_pltfr_suspend(dev);
return ret;
}
static int meson6_dwmac_resume(struct device *dev)
{
int ret;
struct pinctrl *pin_ctrl;
struct pinctrl_state *turnon_tes = NULL;
pr_info("resuem inter = %d\n", is_internal_phy);
if ((is_internal_phy) && support_mac_wol == 0) {
pin_ctrl = devm_pinctrl_get(dev);
if (IS_ERR_OR_NULL(pin_ctrl)) {
pr_info("pinctrl is null\n");
} else {
turnon_tes = pinctrl_lookup_state
(pin_ctrl, "internal_eth_pins");
pinctrl_select_state(pin_ctrl, turnon_tes);
devm_pinctrl_put(pin_ctrl);
pin_ctrl = NULL;
}
dwmac_meson_recover_analog(dev);
}
ret = stmmac_pltfr_resume(dev);
return ret;
}
EXPORT_SYMBOL_GPL(meson6_dwmac_resume);
void meson6_dwmac_shutdown(struct platform_device *pdev)
{
struct pinctrl *pin_ctrl;
struct pinctrl_state *turnoff_tes = NULL;
/*shudown internal phy analog*/
if (is_internal_phy) {
pin_ctrl = devm_pinctrl_get(&pdev->dev);
if (IS_ERR_OR_NULL(pin_ctrl)) {
pr_info("pinctrl is null\n");
} else {
turnoff_tes = pinctrl_lookup_state
(pin_ctrl, "internal_gpio_pins");
if (IS_ERR_OR_NULL(turnoff_tes))
pr_info("Not support gpio low\n");
else
pinctrl_select_state(pin_ctrl, turnoff_tes);
//pinctrl_select_state(pin_ctrl, turnoff_tes);
devm_pinctrl_put(pin_ctrl);
pin_ctrl = NULL;
}
dwmac_meson_disable_analog(&pdev->dev);
}
stmmac_pltfr_suspend(&pdev->dev);
}
#endif
static int meson6_dwmac_probe(struct platform_device *pdev)
{
struct plat_stmmacenet_data *plat_dat;
struct stmmac_resources stmmac_res;
struct meson_dwmac *dwmac;
int ret;
struct device_node *np = pdev->dev.of_node;
if (of_property_read_u32(np, "analog_ver", &analog_ver))
pr_info("analog_ver not set\n");
else
pr_info("analog_Ver %d\n", analog_ver);
ret = stmmac_get_platform_resources(pdev, &stmmac_res);
if (ret)
return ret;
plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac);
if (IS_ERR(plat_dat))
return PTR_ERR(plat_dat);
dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
if (!dwmac) {
ret = -ENOMEM;
goto err_remove_config_dt;
}
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
dwmac->reg = g12a_network_interface_setup(pdev);
/* Custom initialisation (if needed) */
if (plat_dat->init) {
ret = plat_dat->init(pdev, plat_dat->bsp_priv);
if (ret)
return ret;
}
#else
struct resource *res;
res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
dwmac->reg = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(dwmac->reg)) {
ret = PTR_ERR(dwmac->reg);
goto err_remove_config_dt;
}
#endif
plat_dat->bsp_priv = dwmac;
plat_dat->fix_mac_speed = meson6_dwmac_fix_mac_speed;
ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
if (ret)
goto err_remove_config_dt;
return 0;
err_remove_config_dt:
stmmac_remove_config_dt(pdev, plat_dat);
return ret;
}
static const struct of_device_id meson6_dwmac_match[] = {
{ .compatible = "amlogic,meson6-dwmac" },
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
{ .compatible = "amlogic, g12a-eth-dwmac" },
#endif
{ }
};
MODULE_DEVICE_TABLE(of, meson6_dwmac_match);
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
SIMPLE_DEV_PM_OPS(stmmac_pltfr_pm_ops, meson6_dwmac_suspend,
meson6_dwmac_resume);
#endif
static struct platform_driver meson6_dwmac_driver = {
.probe = meson6_dwmac_probe,
.remove = stmmac_pltfr_remove,
#ifdef CONFIG_AMLOGIC_ETH_PRIVE
.shutdown = meson6_dwmac_shutdown,
#endif
.driver = {
.name = "meson6-dwmac",
.pm = &stmmac_pltfr_pm_ops,
.of_match_table = meson6_dwmac_match,
},
};
module_platform_driver(meson6_dwmac_driver);
MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
MODULE_DESCRIPTION("Amlogic Meson6 and Meson8 DWMAC glue layer");
MODULE_LICENSE("GPL v2");