18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Allwinner sun4i MUSB Glue Layer
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Based on code from
88c2ecf20Sopenharmony_ci * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/clk.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/extcon.h>
148c2ecf20Sopenharmony_ci#include <linux/io.h>
158c2ecf20Sopenharmony_ci#include <linux/kernel.h>
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/of.h>
188c2ecf20Sopenharmony_ci#include <linux/phy/phy-sun4i-usb.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/reset.h>
218c2ecf20Sopenharmony_ci#include <linux/soc/sunxi/sunxi_sram.h>
228c2ecf20Sopenharmony_ci#include <linux/usb/musb.h>
238c2ecf20Sopenharmony_ci#include <linux/usb/of.h>
248c2ecf20Sopenharmony_ci#include <linux/usb/usb_phy_generic.h>
258c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
268c2ecf20Sopenharmony_ci#include "musb_core.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/*
298c2ecf20Sopenharmony_ci * Register offsets, note sunxi musb has a different layout then most
308c2ecf20Sopenharmony_ci * musb implementations, we translate the layout in musb_readb & friends.
318c2ecf20Sopenharmony_ci */
328c2ecf20Sopenharmony_ci#define SUNXI_MUSB_POWER			0x0040
338c2ecf20Sopenharmony_ci#define SUNXI_MUSB_DEVCTL			0x0041
348c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INDEX			0x0042
358c2ecf20Sopenharmony_ci#define SUNXI_MUSB_VEND0			0x0043
368c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INTRTX			0x0044
378c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INTRRX			0x0046
388c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INTRTXE			0x0048
398c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INTRRXE			0x004a
408c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INTRUSB			0x004c
418c2ecf20Sopenharmony_ci#define SUNXI_MUSB_INTRUSBE			0x0050
428c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FRAME			0x0054
438c2ecf20Sopenharmony_ci#define SUNXI_MUSB_TXFIFOSZ			0x0090
448c2ecf20Sopenharmony_ci#define SUNXI_MUSB_TXFIFOADD			0x0092
458c2ecf20Sopenharmony_ci#define SUNXI_MUSB_RXFIFOSZ			0x0094
468c2ecf20Sopenharmony_ci#define SUNXI_MUSB_RXFIFOADD			0x0096
478c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FADDR			0x0098
488c2ecf20Sopenharmony_ci#define SUNXI_MUSB_TXFUNCADDR			0x0098
498c2ecf20Sopenharmony_ci#define SUNXI_MUSB_TXHUBADDR			0x009a
508c2ecf20Sopenharmony_ci#define SUNXI_MUSB_TXHUBPORT			0x009b
518c2ecf20Sopenharmony_ci#define SUNXI_MUSB_RXFUNCADDR			0x009c
528c2ecf20Sopenharmony_ci#define SUNXI_MUSB_RXHUBADDR			0x009e
538c2ecf20Sopenharmony_ci#define SUNXI_MUSB_RXHUBPORT			0x009f
548c2ecf20Sopenharmony_ci#define SUNXI_MUSB_CONFIGDATA			0x00c0
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* VEND0 bits */
578c2ecf20Sopenharmony_ci#define SUNXI_MUSB_VEND0_PIO_MODE		0
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/* flags */
608c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_ENABLED			0
618c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_HOSTMODE			1
628c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_HOSTMODE_PEND		2
638c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_VBUS_ON			3
648c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_PHY_ON			4
658c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_HAS_SRAM			5
668c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_HAS_RESET			6
678c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_NO_CONFIGDATA		7
688c2ecf20Sopenharmony_ci#define SUNXI_MUSB_FL_PHY_MODE_PEND		8
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/* Our read/write methods need access and do not get passed in a musb ref :| */
718c2ecf20Sopenharmony_cistatic struct musb *sunxi_musb;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistruct sunxi_glue {
748c2ecf20Sopenharmony_ci	struct device		*dev;
758c2ecf20Sopenharmony_ci	struct musb		*musb;
768c2ecf20Sopenharmony_ci	struct platform_device	*musb_pdev;
778c2ecf20Sopenharmony_ci	struct clk		*clk;
788c2ecf20Sopenharmony_ci	struct reset_control	*rst;
798c2ecf20Sopenharmony_ci	struct phy		*phy;
808c2ecf20Sopenharmony_ci	struct platform_device	*usb_phy;
818c2ecf20Sopenharmony_ci	struct usb_phy		*xceiv;
828c2ecf20Sopenharmony_ci	enum phy_mode		phy_mode;
838c2ecf20Sopenharmony_ci	unsigned long		flags;
848c2ecf20Sopenharmony_ci	struct work_struct	work;
858c2ecf20Sopenharmony_ci	struct extcon_dev	*extcon;
868c2ecf20Sopenharmony_ci	struct notifier_block	host_nb;
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/* phy_power_on / off may sleep, so we use a workqueue  */
908c2ecf20Sopenharmony_cistatic void sunxi_musb_work(struct work_struct *work)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
938c2ecf20Sopenharmony_ci	bool vbus_on, phy_on;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
968c2ecf20Sopenharmony_ci		return;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
998c2ecf20Sopenharmony_ci		struct musb *musb = glue->musb;
1008c2ecf20Sopenharmony_ci		unsigned long flags;
1018c2ecf20Sopenharmony_ci		u8 devctl;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		spin_lock_irqsave(&musb->lock, flags);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci		devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
1068c2ecf20Sopenharmony_ci		if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
1078c2ecf20Sopenharmony_ci			set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
1088c2ecf20Sopenharmony_ci			musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
1098c2ecf20Sopenharmony_ci			MUSB_HST_MODE(musb);
1108c2ecf20Sopenharmony_ci			devctl |= MUSB_DEVCTL_SESSION;
1118c2ecf20Sopenharmony_ci		} else {
1128c2ecf20Sopenharmony_ci			clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
1138c2ecf20Sopenharmony_ci			musb->xceiv->otg->state = OTG_STATE_B_IDLE;
1148c2ecf20Sopenharmony_ci			MUSB_DEV_MODE(musb);
1158c2ecf20Sopenharmony_ci			devctl &= ~MUSB_DEVCTL_SESSION;
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci		writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&musb->lock, flags);
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
1238c2ecf20Sopenharmony_ci	phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	if (phy_on != vbus_on) {
1268c2ecf20Sopenharmony_ci		if (vbus_on) {
1278c2ecf20Sopenharmony_ci			phy_power_on(glue->phy);
1288c2ecf20Sopenharmony_ci			set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
1298c2ecf20Sopenharmony_ci		} else {
1308c2ecf20Sopenharmony_ci			phy_power_off(glue->phy);
1318c2ecf20Sopenharmony_ci			clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	if (test_and_clear_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags))
1368c2ecf20Sopenharmony_ci		phy_set_mode(glue->phy, glue->phy_mode);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic void sunxi_musb_set_vbus(struct musb *musb, int is_on)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (is_on) {
1448c2ecf20Sopenharmony_ci		set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
1458c2ecf20Sopenharmony_ci		musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
1468c2ecf20Sopenharmony_ci	} else {
1478c2ecf20Sopenharmony_ci		clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
1488c2ecf20Sopenharmony_ci	}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	schedule_work(&glue->work);
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistatic void sunxi_musb_pre_root_reset_end(struct musb *musb)
1548c2ecf20Sopenharmony_ci{
1558c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	sun4i_usb_phy_set_squelch_detect(glue->phy, false);
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic void sunxi_musb_post_root_reset_end(struct musb *musb)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	sun4i_usb_phy_set_squelch_detect(glue->phy, true);
1658c2ecf20Sopenharmony_ci}
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	struct musb *musb = __hci;
1708c2ecf20Sopenharmony_ci	unsigned long flags;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	spin_lock_irqsave(&musb->lock, flags);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
1758c2ecf20Sopenharmony_ci	if (musb->int_usb)
1768c2ecf20Sopenharmony_ci		writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
1798c2ecf20Sopenharmony_ci		/* ep0 FADDR must be 0 when (re)entering peripheral mode */
1808c2ecf20Sopenharmony_ci		musb_ep_select(musb->mregs, 0);
1818c2ecf20Sopenharmony_ci		musb_writeb(musb->mregs, MUSB_FADDR, 0);
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
1858c2ecf20Sopenharmony_ci	if (musb->int_tx)
1868c2ecf20Sopenharmony_ci		writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
1898c2ecf20Sopenharmony_ci	if (musb->int_rx)
1908c2ecf20Sopenharmony_ci		writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	musb_interrupt(musb);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&musb->lock, flags);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int sunxi_musb_host_notifier(struct notifier_block *nb,
2008c2ecf20Sopenharmony_ci				    unsigned long event, void *ptr)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (event)
2058c2ecf20Sopenharmony_ci		set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
2068c2ecf20Sopenharmony_ci	else
2078c2ecf20Sopenharmony_ci		clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
2108c2ecf20Sopenharmony_ci	schedule_work(&glue->work);
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	return NOTIFY_DONE;
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int sunxi_musb_init(struct musb *musb)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
2188c2ecf20Sopenharmony_ci	int ret;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	sunxi_musb = musb;
2218c2ecf20Sopenharmony_ci	musb->phy = glue->phy;
2228c2ecf20Sopenharmony_ci	musb->xceiv = glue->xceiv;
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags)) {
2258c2ecf20Sopenharmony_ci		ret = sunxi_sram_claim(musb->controller->parent);
2268c2ecf20Sopenharmony_ci		if (ret)
2278c2ecf20Sopenharmony_ci			return ret;
2288c2ecf20Sopenharmony_ci	}
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(glue->clk);
2318c2ecf20Sopenharmony_ci	if (ret)
2328c2ecf20Sopenharmony_ci		goto error_sram_release;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
2358c2ecf20Sopenharmony_ci		ret = reset_control_deassert(glue->rst);
2368c2ecf20Sopenharmony_ci		if (ret)
2378c2ecf20Sopenharmony_ci			goto error_clk_disable;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* Register notifier before calling phy_init() */
2438c2ecf20Sopenharmony_ci	ret = devm_extcon_register_notifier(glue->dev, glue->extcon,
2448c2ecf20Sopenharmony_ci					EXTCON_USB_HOST, &glue->host_nb);
2458c2ecf20Sopenharmony_ci	if (ret)
2468c2ecf20Sopenharmony_ci		goto error_reset_assert;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	ret = phy_init(glue->phy);
2498c2ecf20Sopenharmony_ci	if (ret)
2508c2ecf20Sopenharmony_ci		goto error_reset_assert;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	musb->isr = sunxi_musb_interrupt;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	/* Stop the musb-core from doing runtime pm (not supported on sunxi) */
2558c2ecf20Sopenharmony_ci	pm_runtime_get(musb->controller);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	return 0;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_cierror_reset_assert:
2608c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
2618c2ecf20Sopenharmony_ci		reset_control_assert(glue->rst);
2628c2ecf20Sopenharmony_cierror_clk_disable:
2638c2ecf20Sopenharmony_ci	clk_disable_unprepare(glue->clk);
2648c2ecf20Sopenharmony_cierror_sram_release:
2658c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
2668c2ecf20Sopenharmony_ci		sunxi_sram_release(musb->controller->parent);
2678c2ecf20Sopenharmony_ci	return ret;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic int sunxi_musb_exit(struct musb *musb)
2718c2ecf20Sopenharmony_ci{
2728c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	pm_runtime_put(musb->controller);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	cancel_work_sync(&glue->work);
2778c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
2788c2ecf20Sopenharmony_ci		phy_power_off(glue->phy);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	phy_exit(glue->phy);
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
2838c2ecf20Sopenharmony_ci		reset_control_assert(glue->rst);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	clk_disable_unprepare(glue->clk);
2868c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
2878c2ecf20Sopenharmony_ci		sunxi_sram_release(musb->controller->parent);
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	devm_usb_put_phy(glue->dev, glue->xceiv);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	return 0;
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic void sunxi_musb_enable(struct musb *musb)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	glue->musb = musb;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/* musb_core does not call us in a balanced manner */
3018c2ecf20Sopenharmony_ci	if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
3028c2ecf20Sopenharmony_ci		return;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	schedule_work(&glue->work);
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic void sunxi_musb_disable(struct musb *musb)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic struct dma_controller *
3158c2ecf20Sopenharmony_cisunxi_musb_dma_controller_create(struct musb *musb, void __iomem *base)
3168c2ecf20Sopenharmony_ci{
3178c2ecf20Sopenharmony_ci	return NULL;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic void sunxi_musb_dma_controller_destroy(struct dma_controller *c)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int sunxi_musb_set_mode(struct musb *musb, u8 mode)
3258c2ecf20Sopenharmony_ci{
3268c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
3278c2ecf20Sopenharmony_ci	enum phy_mode new_mode;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	switch (mode) {
3308c2ecf20Sopenharmony_ci	case MUSB_HOST:
3318c2ecf20Sopenharmony_ci		new_mode = PHY_MODE_USB_HOST;
3328c2ecf20Sopenharmony_ci		break;
3338c2ecf20Sopenharmony_ci	case MUSB_PERIPHERAL:
3348c2ecf20Sopenharmony_ci		new_mode = PHY_MODE_USB_DEVICE;
3358c2ecf20Sopenharmony_ci		break;
3368c2ecf20Sopenharmony_ci	case MUSB_OTG:
3378c2ecf20Sopenharmony_ci		new_mode = PHY_MODE_USB_OTG;
3388c2ecf20Sopenharmony_ci		break;
3398c2ecf20Sopenharmony_ci	default:
3408c2ecf20Sopenharmony_ci		dev_err(musb->controller->parent,
3418c2ecf20Sopenharmony_ci			"Error requested mode not supported by this kernel\n");
3428c2ecf20Sopenharmony_ci		return -EINVAL;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (glue->phy_mode == new_mode)
3468c2ecf20Sopenharmony_ci		return 0;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	if (musb->port_mode != MUSB_OTG) {
3498c2ecf20Sopenharmony_ci		dev_err(musb->controller->parent,
3508c2ecf20Sopenharmony_ci			"Error changing modes is only supported in dual role mode\n");
3518c2ecf20Sopenharmony_ci		return -EINVAL;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	if (musb->port1_status & USB_PORT_STAT_ENABLE)
3558c2ecf20Sopenharmony_ci		musb_root_disconnect(musb);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/*
3588c2ecf20Sopenharmony_ci	 * phy_set_mode may sleep, and we're called with a spinlock held,
3598c2ecf20Sopenharmony_ci	 * so let sunxi_musb_work deal with it.
3608c2ecf20Sopenharmony_ci	 */
3618c2ecf20Sopenharmony_ci	glue->phy_mode = new_mode;
3628c2ecf20Sopenharmony_ci	set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
3638c2ecf20Sopenharmony_ci	schedule_work(&glue->work);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	return 0;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic int sunxi_musb_recover(struct musb *musb)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_ci	/*
3738c2ecf20Sopenharmony_ci	 * Schedule a phy_set_mode with the current glue->phy_mode value,
3748c2ecf20Sopenharmony_ci	 * this will force end the current session.
3758c2ecf20Sopenharmony_ci	 */
3768c2ecf20Sopenharmony_ci	set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
3778c2ecf20Sopenharmony_ci	schedule_work(&glue->work);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return 0;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/*
3838c2ecf20Sopenharmony_ci * sunxi musb register layout
3848c2ecf20Sopenharmony_ci * 0x00 - 0x17	fifo regs, 1 long per fifo
3858c2ecf20Sopenharmony_ci * 0x40 - 0x57	generic control regs (power - frame)
3868c2ecf20Sopenharmony_ci * 0x80 - 0x8f	ep control regs (addressed through hw_ep->regs, indexed)
3878c2ecf20Sopenharmony_ci * 0x90 - 0x97	fifo control regs (indexed)
3888c2ecf20Sopenharmony_ci * 0x98 - 0x9f	multipoint / busctl regs (indexed)
3898c2ecf20Sopenharmony_ci * 0xc0		configdata reg
3908c2ecf20Sopenharmony_ci */
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic u32 sunxi_musb_fifo_offset(u8 epnum)
3938c2ecf20Sopenharmony_ci{
3948c2ecf20Sopenharmony_ci	return (epnum * 4);
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
3988c2ecf20Sopenharmony_ci{
3998c2ecf20Sopenharmony_ci	WARN_ONCE(offset != 0,
4008c2ecf20Sopenharmony_ci		  "sunxi_musb_ep_offset called with non 0 offset\n");
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	return 0x80; /* indexed, so ignore epnum */
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_cistatic u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	return SUNXI_MUSB_TXFUNCADDR + offset;
4088c2ecf20Sopenharmony_ci}
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cistatic u8 sunxi_musb_readb(void __iomem *addr, u32 offset)
4118c2ecf20Sopenharmony_ci{
4128c2ecf20Sopenharmony_ci	struct sunxi_glue *glue;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	if (addr == sunxi_musb->mregs) {
4158c2ecf20Sopenharmony_ci		/* generic control or fifo control reg access */
4168c2ecf20Sopenharmony_ci		switch (offset) {
4178c2ecf20Sopenharmony_ci		case MUSB_FADDR:
4188c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_FADDR);
4198c2ecf20Sopenharmony_ci		case MUSB_POWER:
4208c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_POWER);
4218c2ecf20Sopenharmony_ci		case MUSB_INTRUSB:
4228c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_INTRUSB);
4238c2ecf20Sopenharmony_ci		case MUSB_INTRUSBE:
4248c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_INTRUSBE);
4258c2ecf20Sopenharmony_ci		case MUSB_INDEX:
4268c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_INDEX);
4278c2ecf20Sopenharmony_ci		case MUSB_TESTMODE:
4288c2ecf20Sopenharmony_ci			return 0; /* No testmode on sunxi */
4298c2ecf20Sopenharmony_ci		case MUSB_DEVCTL:
4308c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_DEVCTL);
4318c2ecf20Sopenharmony_ci		case MUSB_TXFIFOSZ:
4328c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_TXFIFOSZ);
4338c2ecf20Sopenharmony_ci		case MUSB_RXFIFOSZ:
4348c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_RXFIFOSZ);
4358c2ecf20Sopenharmony_ci		case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
4368c2ecf20Sopenharmony_ci			glue = dev_get_drvdata(sunxi_musb->controller->parent);
4378c2ecf20Sopenharmony_ci			/* A33 saves a reg, and we get to hardcode this */
4388c2ecf20Sopenharmony_ci			if (test_bit(SUNXI_MUSB_FL_NO_CONFIGDATA,
4398c2ecf20Sopenharmony_ci				     &glue->flags))
4408c2ecf20Sopenharmony_ci				return 0xde;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci			return readb(addr + SUNXI_MUSB_CONFIGDATA);
4438c2ecf20Sopenharmony_ci		/* Offset for these is fixed by sunxi_musb_busctl_offset() */
4448c2ecf20Sopenharmony_ci		case SUNXI_MUSB_TXFUNCADDR:
4458c2ecf20Sopenharmony_ci		case SUNXI_MUSB_TXHUBADDR:
4468c2ecf20Sopenharmony_ci		case SUNXI_MUSB_TXHUBPORT:
4478c2ecf20Sopenharmony_ci		case SUNXI_MUSB_RXFUNCADDR:
4488c2ecf20Sopenharmony_ci		case SUNXI_MUSB_RXHUBADDR:
4498c2ecf20Sopenharmony_ci		case SUNXI_MUSB_RXHUBPORT:
4508c2ecf20Sopenharmony_ci			/* multipoint / busctl reg access */
4518c2ecf20Sopenharmony_ci			return readb(addr + offset);
4528c2ecf20Sopenharmony_ci		default:
4538c2ecf20Sopenharmony_ci			dev_err(sunxi_musb->controller->parent,
4548c2ecf20Sopenharmony_ci				"Error unknown readb offset %u\n", offset);
4558c2ecf20Sopenharmony_ci			return 0;
4568c2ecf20Sopenharmony_ci		}
4578c2ecf20Sopenharmony_ci	} else if (addr == (sunxi_musb->mregs + 0x80)) {
4588c2ecf20Sopenharmony_ci		/* ep control reg access */
4598c2ecf20Sopenharmony_ci		/* sunxi has a 2 byte hole before the txtype register */
4608c2ecf20Sopenharmony_ci		if (offset >= MUSB_TXTYPE)
4618c2ecf20Sopenharmony_ci			offset += 2;
4628c2ecf20Sopenharmony_ci		return readb(addr + offset);
4638c2ecf20Sopenharmony_ci	}
4648c2ecf20Sopenharmony_ci
4658c2ecf20Sopenharmony_ci	dev_err(sunxi_musb->controller->parent,
4668c2ecf20Sopenharmony_ci		"Error unknown readb at 0x%x bytes offset\n",
4678c2ecf20Sopenharmony_ci		(int)(addr - sunxi_musb->mregs));
4688c2ecf20Sopenharmony_ci	return 0;
4698c2ecf20Sopenharmony_ci}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_cistatic void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
4728c2ecf20Sopenharmony_ci{
4738c2ecf20Sopenharmony_ci	if (addr == sunxi_musb->mregs) {
4748c2ecf20Sopenharmony_ci		/* generic control or fifo control reg access */
4758c2ecf20Sopenharmony_ci		switch (offset) {
4768c2ecf20Sopenharmony_ci		case MUSB_FADDR:
4778c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_FADDR);
4788c2ecf20Sopenharmony_ci		case MUSB_POWER:
4798c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_POWER);
4808c2ecf20Sopenharmony_ci		case MUSB_INTRUSB:
4818c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_INTRUSB);
4828c2ecf20Sopenharmony_ci		case MUSB_INTRUSBE:
4838c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
4848c2ecf20Sopenharmony_ci		case MUSB_INDEX:
4858c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_INDEX);
4868c2ecf20Sopenharmony_ci		case MUSB_TESTMODE:
4878c2ecf20Sopenharmony_ci			if (data)
4888c2ecf20Sopenharmony_ci				dev_warn(sunxi_musb->controller->parent,
4898c2ecf20Sopenharmony_ci					"sunxi-musb does not have testmode\n");
4908c2ecf20Sopenharmony_ci			return;
4918c2ecf20Sopenharmony_ci		case MUSB_DEVCTL:
4928c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_DEVCTL);
4938c2ecf20Sopenharmony_ci		case MUSB_TXFIFOSZ:
4948c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
4958c2ecf20Sopenharmony_ci		case MUSB_RXFIFOSZ:
4968c2ecf20Sopenharmony_ci			return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
4978c2ecf20Sopenharmony_ci		/* Offset for these is fixed by sunxi_musb_busctl_offset() */
4988c2ecf20Sopenharmony_ci		case SUNXI_MUSB_TXFUNCADDR:
4998c2ecf20Sopenharmony_ci		case SUNXI_MUSB_TXHUBADDR:
5008c2ecf20Sopenharmony_ci		case SUNXI_MUSB_TXHUBPORT:
5018c2ecf20Sopenharmony_ci		case SUNXI_MUSB_RXFUNCADDR:
5028c2ecf20Sopenharmony_ci		case SUNXI_MUSB_RXHUBADDR:
5038c2ecf20Sopenharmony_ci		case SUNXI_MUSB_RXHUBPORT:
5048c2ecf20Sopenharmony_ci			/* multipoint / busctl reg access */
5058c2ecf20Sopenharmony_ci			return writeb(data, addr + offset);
5068c2ecf20Sopenharmony_ci		default:
5078c2ecf20Sopenharmony_ci			dev_err(sunxi_musb->controller->parent,
5088c2ecf20Sopenharmony_ci				"Error unknown writeb offset %u\n", offset);
5098c2ecf20Sopenharmony_ci			return;
5108c2ecf20Sopenharmony_ci		}
5118c2ecf20Sopenharmony_ci	} else if (addr == (sunxi_musb->mregs + 0x80)) {
5128c2ecf20Sopenharmony_ci		/* ep control reg access */
5138c2ecf20Sopenharmony_ci		if (offset >= MUSB_TXTYPE)
5148c2ecf20Sopenharmony_ci			offset += 2;
5158c2ecf20Sopenharmony_ci		return writeb(data, addr + offset);
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	dev_err(sunxi_musb->controller->parent,
5198c2ecf20Sopenharmony_ci		"Error unknown writeb at 0x%x bytes offset\n",
5208c2ecf20Sopenharmony_ci		(int)(addr - sunxi_musb->mregs));
5218c2ecf20Sopenharmony_ci}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_cistatic u16 sunxi_musb_readw(void __iomem *addr, u32 offset)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	if (addr == sunxi_musb->mregs) {
5268c2ecf20Sopenharmony_ci		/* generic control or fifo control reg access */
5278c2ecf20Sopenharmony_ci		switch (offset) {
5288c2ecf20Sopenharmony_ci		case MUSB_INTRTX:
5298c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_INTRTX);
5308c2ecf20Sopenharmony_ci		case MUSB_INTRRX:
5318c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_INTRRX);
5328c2ecf20Sopenharmony_ci		case MUSB_INTRTXE:
5338c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_INTRTXE);
5348c2ecf20Sopenharmony_ci		case MUSB_INTRRXE:
5358c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_INTRRXE);
5368c2ecf20Sopenharmony_ci		case MUSB_FRAME:
5378c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_FRAME);
5388c2ecf20Sopenharmony_ci		case MUSB_TXFIFOADD:
5398c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_TXFIFOADD);
5408c2ecf20Sopenharmony_ci		case MUSB_RXFIFOADD:
5418c2ecf20Sopenharmony_ci			return readw(addr + SUNXI_MUSB_RXFIFOADD);
5428c2ecf20Sopenharmony_ci		case MUSB_HWVERS:
5438c2ecf20Sopenharmony_ci			return 0; /* sunxi musb version is not known */
5448c2ecf20Sopenharmony_ci		default:
5458c2ecf20Sopenharmony_ci			dev_err(sunxi_musb->controller->parent,
5468c2ecf20Sopenharmony_ci				"Error unknown readw offset %u\n", offset);
5478c2ecf20Sopenharmony_ci			return 0;
5488c2ecf20Sopenharmony_ci		}
5498c2ecf20Sopenharmony_ci	} else if (addr == (sunxi_musb->mregs + 0x80)) {
5508c2ecf20Sopenharmony_ci		/* ep control reg access */
5518c2ecf20Sopenharmony_ci		return readw(addr + offset);
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	dev_err(sunxi_musb->controller->parent,
5558c2ecf20Sopenharmony_ci		"Error unknown readw at 0x%x bytes offset\n",
5568c2ecf20Sopenharmony_ci		(int)(addr - sunxi_musb->mregs));
5578c2ecf20Sopenharmony_ci	return 0;
5588c2ecf20Sopenharmony_ci}
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_cistatic void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	if (addr == sunxi_musb->mregs) {
5638c2ecf20Sopenharmony_ci		/* generic control or fifo control reg access */
5648c2ecf20Sopenharmony_ci		switch (offset) {
5658c2ecf20Sopenharmony_ci		case MUSB_INTRTX:
5668c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_INTRTX);
5678c2ecf20Sopenharmony_ci		case MUSB_INTRRX:
5688c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_INTRRX);
5698c2ecf20Sopenharmony_ci		case MUSB_INTRTXE:
5708c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_INTRTXE);
5718c2ecf20Sopenharmony_ci		case MUSB_INTRRXE:
5728c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_INTRRXE);
5738c2ecf20Sopenharmony_ci		case MUSB_FRAME:
5748c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_FRAME);
5758c2ecf20Sopenharmony_ci		case MUSB_TXFIFOADD:
5768c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
5778c2ecf20Sopenharmony_ci		case MUSB_RXFIFOADD:
5788c2ecf20Sopenharmony_ci			return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
5798c2ecf20Sopenharmony_ci		default:
5808c2ecf20Sopenharmony_ci			dev_err(sunxi_musb->controller->parent,
5818c2ecf20Sopenharmony_ci				"Error unknown writew offset %u\n", offset);
5828c2ecf20Sopenharmony_ci			return;
5838c2ecf20Sopenharmony_ci		}
5848c2ecf20Sopenharmony_ci	} else if (addr == (sunxi_musb->mregs + 0x80)) {
5858c2ecf20Sopenharmony_ci		/* ep control reg access */
5868c2ecf20Sopenharmony_ci		return writew(data, addr + offset);
5878c2ecf20Sopenharmony_ci	}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	dev_err(sunxi_musb->controller->parent,
5908c2ecf20Sopenharmony_ci		"Error unknown writew at 0x%x bytes offset\n",
5918c2ecf20Sopenharmony_ci		(int)(addr - sunxi_musb->mregs));
5928c2ecf20Sopenharmony_ci}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_cistatic const struct musb_platform_ops sunxi_musb_ops = {
5958c2ecf20Sopenharmony_ci	.quirks		= MUSB_INDEXED_EP,
5968c2ecf20Sopenharmony_ci	.init		= sunxi_musb_init,
5978c2ecf20Sopenharmony_ci	.exit		= sunxi_musb_exit,
5988c2ecf20Sopenharmony_ci	.enable		= sunxi_musb_enable,
5998c2ecf20Sopenharmony_ci	.disable	= sunxi_musb_disable,
6008c2ecf20Sopenharmony_ci	.fifo_offset	= sunxi_musb_fifo_offset,
6018c2ecf20Sopenharmony_ci	.ep_offset	= sunxi_musb_ep_offset,
6028c2ecf20Sopenharmony_ci	.busctl_offset	= sunxi_musb_busctl_offset,
6038c2ecf20Sopenharmony_ci	.readb		= sunxi_musb_readb,
6048c2ecf20Sopenharmony_ci	.writeb		= sunxi_musb_writeb,
6058c2ecf20Sopenharmony_ci	.readw		= sunxi_musb_readw,
6068c2ecf20Sopenharmony_ci	.writew		= sunxi_musb_writew,
6078c2ecf20Sopenharmony_ci	.dma_init	= sunxi_musb_dma_controller_create,
6088c2ecf20Sopenharmony_ci	.dma_exit	= sunxi_musb_dma_controller_destroy,
6098c2ecf20Sopenharmony_ci	.set_mode	= sunxi_musb_set_mode,
6108c2ecf20Sopenharmony_ci	.recover	= sunxi_musb_recover,
6118c2ecf20Sopenharmony_ci	.set_vbus	= sunxi_musb_set_vbus,
6128c2ecf20Sopenharmony_ci	.pre_root_reset_end = sunxi_musb_pre_root_reset_end,
6138c2ecf20Sopenharmony_ci	.post_root_reset_end = sunxi_musb_post_root_reset_end,
6148c2ecf20Sopenharmony_ci};
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci/* Allwinner OTG supports up to 5 endpoints */
6178c2ecf20Sopenharmony_ci#define SUNXI_MUSB_MAX_EP_NUM	6
6188c2ecf20Sopenharmony_ci#define SUNXI_MUSB_RAM_BITS	11
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
6218c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
6228c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
6238c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
6248c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
6258c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
6268c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
6278c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
6288c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
6298c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
6308c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
6318c2ecf20Sopenharmony_ci};
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci/* H3/V3s OTG supports only 4 endpoints */
6348c2ecf20Sopenharmony_ci#define SUNXI_MUSB_MAX_EP_NUM_H3	5
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_cistatic struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
6378c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
6388c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
6398c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
6408c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
6418c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
6428c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
6438c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
6448c2ecf20Sopenharmony_ci	MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
6458c2ecf20Sopenharmony_ci};
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_cistatic const struct musb_hdrc_config sunxi_musb_hdrc_config = {
6488c2ecf20Sopenharmony_ci	.fifo_cfg       = sunxi_musb_mode_cfg,
6498c2ecf20Sopenharmony_ci	.fifo_cfg_size  = ARRAY_SIZE(sunxi_musb_mode_cfg),
6508c2ecf20Sopenharmony_ci	.multipoint	= true,
6518c2ecf20Sopenharmony_ci	.dyn_fifo	= true,
6528c2ecf20Sopenharmony_ci	.num_eps	= SUNXI_MUSB_MAX_EP_NUM,
6538c2ecf20Sopenharmony_ci	.ram_bits	= SUNXI_MUSB_RAM_BITS,
6548c2ecf20Sopenharmony_ci};
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_cistatic struct musb_hdrc_config sunxi_musb_hdrc_config_h3 = {
6578c2ecf20Sopenharmony_ci	.fifo_cfg       = sunxi_musb_mode_cfg_h3,
6588c2ecf20Sopenharmony_ci	.fifo_cfg_size  = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
6598c2ecf20Sopenharmony_ci	.multipoint	= true,
6608c2ecf20Sopenharmony_ci	.dyn_fifo	= true,
6618c2ecf20Sopenharmony_ci	.num_eps	= SUNXI_MUSB_MAX_EP_NUM_H3,
6628c2ecf20Sopenharmony_ci	.ram_bits	= SUNXI_MUSB_RAM_BITS,
6638c2ecf20Sopenharmony_ci};
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic int sunxi_musb_probe(struct platform_device *pdev)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	struct musb_hdrc_platform_data	pdata;
6698c2ecf20Sopenharmony_ci	struct platform_device_info	pinfo;
6708c2ecf20Sopenharmony_ci	struct sunxi_glue		*glue;
6718c2ecf20Sopenharmony_ci	struct device_node		*np = pdev->dev.of_node;
6728c2ecf20Sopenharmony_ci	int ret;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	if (!np) {
6758c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error no device tree node found\n");
6768c2ecf20Sopenharmony_ci		return -EINVAL;
6778c2ecf20Sopenharmony_ci	}
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
6808c2ecf20Sopenharmony_ci	if (!glue)
6818c2ecf20Sopenharmony_ci		return -ENOMEM;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	memset(&pdata, 0, sizeof(pdata));
6848c2ecf20Sopenharmony_ci	switch (usb_get_dr_mode(&pdev->dev)) {
6858c2ecf20Sopenharmony_ci#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
6868c2ecf20Sopenharmony_ci	case USB_DR_MODE_HOST:
6878c2ecf20Sopenharmony_ci		pdata.mode = MUSB_HOST;
6888c2ecf20Sopenharmony_ci		glue->phy_mode = PHY_MODE_USB_HOST;
6898c2ecf20Sopenharmony_ci		break;
6908c2ecf20Sopenharmony_ci#endif
6918c2ecf20Sopenharmony_ci#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
6928c2ecf20Sopenharmony_ci	case USB_DR_MODE_PERIPHERAL:
6938c2ecf20Sopenharmony_ci		pdata.mode = MUSB_PERIPHERAL;
6948c2ecf20Sopenharmony_ci		glue->phy_mode = PHY_MODE_USB_DEVICE;
6958c2ecf20Sopenharmony_ci		break;
6968c2ecf20Sopenharmony_ci#endif
6978c2ecf20Sopenharmony_ci#ifdef CONFIG_USB_MUSB_DUAL_ROLE
6988c2ecf20Sopenharmony_ci	case USB_DR_MODE_OTG:
6998c2ecf20Sopenharmony_ci		pdata.mode = MUSB_OTG;
7008c2ecf20Sopenharmony_ci		glue->phy_mode = PHY_MODE_USB_OTG;
7018c2ecf20Sopenharmony_ci		break;
7028c2ecf20Sopenharmony_ci#endif
7038c2ecf20Sopenharmony_ci	default:
7048c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
7058c2ecf20Sopenharmony_ci		return -EINVAL;
7068c2ecf20Sopenharmony_ci	}
7078c2ecf20Sopenharmony_ci	pdata.platform_ops	= &sunxi_musb_ops;
7088c2ecf20Sopenharmony_ci	if (!of_device_is_compatible(np, "allwinner,sun8i-h3-musb"))
7098c2ecf20Sopenharmony_ci		pdata.config = &sunxi_musb_hdrc_config;
7108c2ecf20Sopenharmony_ci	else
7118c2ecf20Sopenharmony_ci		pdata.config = &sunxi_musb_hdrc_config_h3;
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	glue->dev = &pdev->dev;
7148c2ecf20Sopenharmony_ci	INIT_WORK(&glue->work, sunxi_musb_work);
7158c2ecf20Sopenharmony_ci	glue->host_nb.notifier_call = sunxi_musb_host_notifier;
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun4i-a10-musb"))
7188c2ecf20Sopenharmony_ci		set_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun6i-a31-musb"))
7218c2ecf20Sopenharmony_ci		set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (of_device_is_compatible(np, "allwinner,sun8i-a33-musb") ||
7248c2ecf20Sopenharmony_ci	    of_device_is_compatible(np, "allwinner,sun8i-h3-musb")) {
7258c2ecf20Sopenharmony_ci		set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
7268c2ecf20Sopenharmony_ci		set_bit(SUNXI_MUSB_FL_NO_CONFIGDATA, &glue->flags);
7278c2ecf20Sopenharmony_ci	}
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	glue->clk = devm_clk_get(&pdev->dev, NULL);
7308c2ecf20Sopenharmony_ci	if (IS_ERR(glue->clk)) {
7318c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error getting clock: %ld\n",
7328c2ecf20Sopenharmony_ci			PTR_ERR(glue->clk));
7338c2ecf20Sopenharmony_ci		return PTR_ERR(glue->clk);
7348c2ecf20Sopenharmony_ci	}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
7378c2ecf20Sopenharmony_ci		glue->rst = devm_reset_control_get(&pdev->dev, NULL);
7388c2ecf20Sopenharmony_ci		if (IS_ERR(glue->rst)) {
7398c2ecf20Sopenharmony_ci			if (PTR_ERR(glue->rst) == -EPROBE_DEFER)
7408c2ecf20Sopenharmony_ci				return -EPROBE_DEFER;
7418c2ecf20Sopenharmony_ci			dev_err(&pdev->dev, "Error getting reset %ld\n",
7428c2ecf20Sopenharmony_ci				PTR_ERR(glue->rst));
7438c2ecf20Sopenharmony_ci			return PTR_ERR(glue->rst);
7448c2ecf20Sopenharmony_ci		}
7458c2ecf20Sopenharmony_ci	}
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
7488c2ecf20Sopenharmony_ci	if (IS_ERR(glue->extcon)) {
7498c2ecf20Sopenharmony_ci		if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
7508c2ecf20Sopenharmony_ci			return -EPROBE_DEFER;
7518c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Invalid or missing extcon\n");
7528c2ecf20Sopenharmony_ci		return PTR_ERR(glue->extcon);
7538c2ecf20Sopenharmony_ci	}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	glue->phy = devm_phy_get(&pdev->dev, "usb");
7568c2ecf20Sopenharmony_ci	if (IS_ERR(glue->phy)) {
7578c2ecf20Sopenharmony_ci		if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
7588c2ecf20Sopenharmony_ci			return -EPROBE_DEFER;
7598c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error getting phy %ld\n",
7608c2ecf20Sopenharmony_ci			PTR_ERR(glue->phy));
7618c2ecf20Sopenharmony_ci		return PTR_ERR(glue->phy);
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	glue->usb_phy = usb_phy_generic_register();
7658c2ecf20Sopenharmony_ci	if (IS_ERR(glue->usb_phy)) {
7668c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
7678c2ecf20Sopenharmony_ci			PTR_ERR(glue->usb_phy));
7688c2ecf20Sopenharmony_ci		return PTR_ERR(glue->usb_phy);
7698c2ecf20Sopenharmony_ci	}
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
7728c2ecf20Sopenharmony_ci	if (IS_ERR(glue->xceiv)) {
7738c2ecf20Sopenharmony_ci		ret = PTR_ERR(glue->xceiv);
7748c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
7758c2ecf20Sopenharmony_ci		goto err_unregister_usb_phy;
7768c2ecf20Sopenharmony_ci	}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, glue);
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	memset(&pinfo, 0, sizeof(pinfo));
7818c2ecf20Sopenharmony_ci	pinfo.name	 = "musb-hdrc";
7828c2ecf20Sopenharmony_ci	pinfo.id	= PLATFORM_DEVID_AUTO;
7838c2ecf20Sopenharmony_ci	pinfo.parent	= &pdev->dev;
7848c2ecf20Sopenharmony_ci	pinfo.fwnode	= of_fwnode_handle(pdev->dev.of_node);
7858c2ecf20Sopenharmony_ci	pinfo.of_node_reused = true;
7868c2ecf20Sopenharmony_ci	pinfo.res	= pdev->resource;
7878c2ecf20Sopenharmony_ci	pinfo.num_res	= pdev->num_resources;
7888c2ecf20Sopenharmony_ci	pinfo.data	= &pdata;
7898c2ecf20Sopenharmony_ci	pinfo.size_data = sizeof(pdata);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	glue->musb_pdev = platform_device_register_full(&pinfo);
7928c2ecf20Sopenharmony_ci	if (IS_ERR(glue->musb_pdev)) {
7938c2ecf20Sopenharmony_ci		ret = PTR_ERR(glue->musb_pdev);
7948c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
7958c2ecf20Sopenharmony_ci		goto err_unregister_usb_phy;
7968c2ecf20Sopenharmony_ci	}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	return 0;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_cierr_unregister_usb_phy:
8018c2ecf20Sopenharmony_ci	usb_phy_generic_unregister(glue->usb_phy);
8028c2ecf20Sopenharmony_ci	return ret;
8038c2ecf20Sopenharmony_ci}
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_cistatic int sunxi_musb_remove(struct platform_device *pdev)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	struct sunxi_glue *glue = platform_get_drvdata(pdev);
8088c2ecf20Sopenharmony_ci	struct platform_device *usb_phy = glue->usb_phy;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	platform_device_unregister(glue->musb_pdev);
8118c2ecf20Sopenharmony_ci	usb_phy_generic_unregister(usb_phy);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	return 0;
8148c2ecf20Sopenharmony_ci}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_cistatic const struct of_device_id sunxi_musb_match[] = {
8178c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun4i-a10-musb", },
8188c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun6i-a31-musb", },
8198c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun8i-a33-musb", },
8208c2ecf20Sopenharmony_ci	{ .compatible = "allwinner,sun8i-h3-musb", },
8218c2ecf20Sopenharmony_ci	{}
8228c2ecf20Sopenharmony_ci};
8238c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, sunxi_musb_match);
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_cistatic struct platform_driver sunxi_musb_driver = {
8268c2ecf20Sopenharmony_ci	.probe = sunxi_musb_probe,
8278c2ecf20Sopenharmony_ci	.remove = sunxi_musb_remove,
8288c2ecf20Sopenharmony_ci	.driver = {
8298c2ecf20Sopenharmony_ci		.name = "musb-sunxi",
8308c2ecf20Sopenharmony_ci		.of_match_table = sunxi_musb_match,
8318c2ecf20Sopenharmony_ci	},
8328c2ecf20Sopenharmony_ci};
8338c2ecf20Sopenharmony_cimodule_platform_driver(sunxi_musb_driver);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
8368c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
8378c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
838