18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cadence USB3 and USBSSP DRD header file.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018-2020 Cadence.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Author: Pawel Laszczak <pawell@cadence.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#ifndef __LINUX_CDNS3_DRD
108c2ecf20Sopenharmony_ci#define __LINUX_CDNS3_DRD
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/usb/otg.h>
138c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
148c2ecf20Sopenharmony_ci#include "core.h"
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci/*  DRD register interface for version v1 of cdns3 driver. */
178c2ecf20Sopenharmony_cistruct cdns3_otg_regs {
188c2ecf20Sopenharmony_ci	__le32 did;
198c2ecf20Sopenharmony_ci	__le32 rid;
208c2ecf20Sopenharmony_ci	__le32 capabilities;
218c2ecf20Sopenharmony_ci	__le32 reserved1;
228c2ecf20Sopenharmony_ci	__le32 cmd;
238c2ecf20Sopenharmony_ci	__le32 sts;
248c2ecf20Sopenharmony_ci	__le32 state;
258c2ecf20Sopenharmony_ci	__le32 reserved2;
268c2ecf20Sopenharmony_ci	__le32 ien;
278c2ecf20Sopenharmony_ci	__le32 ivect;
288c2ecf20Sopenharmony_ci	__le32 refclk;
298c2ecf20Sopenharmony_ci	__le32 tmr;
308c2ecf20Sopenharmony_ci	__le32 reserved3[4];
318c2ecf20Sopenharmony_ci	__le32 simulate;
328c2ecf20Sopenharmony_ci	__le32 override;
338c2ecf20Sopenharmony_ci	__le32 susp_ctrl;
348c2ecf20Sopenharmony_ci	__le32 phyrst_cfg;
358c2ecf20Sopenharmony_ci	__le32 anasts;
368c2ecf20Sopenharmony_ci	__le32 adp_ramp_time;
378c2ecf20Sopenharmony_ci	__le32 ctrl1;
388c2ecf20Sopenharmony_ci	__le32 ctrl2;
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/*  DRD register interface for version v0 of cdns3 driver. */
428c2ecf20Sopenharmony_cistruct cdns3_otg_legacy_regs {
438c2ecf20Sopenharmony_ci	__le32 cmd;
448c2ecf20Sopenharmony_ci	__le32 sts;
458c2ecf20Sopenharmony_ci	__le32 state;
468c2ecf20Sopenharmony_ci	__le32 refclk;
478c2ecf20Sopenharmony_ci	__le32 ien;
488c2ecf20Sopenharmony_ci	__le32 ivect;
498c2ecf20Sopenharmony_ci	__le32 reserved1[3];
508c2ecf20Sopenharmony_ci	__le32 tmr;
518c2ecf20Sopenharmony_ci	__le32 reserved2[2];
528c2ecf20Sopenharmony_ci	__le32 version;
538c2ecf20Sopenharmony_ci	__le32 capabilities;
548c2ecf20Sopenharmony_ci	__le32 reserved3[2];
558c2ecf20Sopenharmony_ci	__le32 simulate;
568c2ecf20Sopenharmony_ci	__le32 reserved4[5];
578c2ecf20Sopenharmony_ci	__le32 ctrl1;
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* DRD register interface for cdnsp driver */
618c2ecf20Sopenharmony_cistruct cdnsp_otg_regs {
628c2ecf20Sopenharmony_ci	__le32 did;
638c2ecf20Sopenharmony_ci	__le32 rid;
648c2ecf20Sopenharmony_ci	__le32 cfgs1;
658c2ecf20Sopenharmony_ci	__le32 cfgs2;
668c2ecf20Sopenharmony_ci	__le32 cmd;
678c2ecf20Sopenharmony_ci	__le32 sts;
688c2ecf20Sopenharmony_ci	__le32 state;
698c2ecf20Sopenharmony_ci	__le32 ien;
708c2ecf20Sopenharmony_ci	__le32 ivect;
718c2ecf20Sopenharmony_ci	__le32 tmr;
728c2ecf20Sopenharmony_ci	__le32 simulate;
738c2ecf20Sopenharmony_ci	__le32 adpbc_sts;
748c2ecf20Sopenharmony_ci	__le32 adp_ramp_time;
758c2ecf20Sopenharmony_ci	__le32 adpbc_ctrl1;
768c2ecf20Sopenharmony_ci	__le32 adpbc_ctrl2;
778c2ecf20Sopenharmony_ci	__le32 override;
788c2ecf20Sopenharmony_ci	__le32 vbusvalid_dbnc_cfg;
798c2ecf20Sopenharmony_ci	__le32 sessvalid_dbnc_cfg;
808c2ecf20Sopenharmony_ci	__le32 susp_timing_ctrl;
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define OTG_CDNSP_DID	0x0004034E
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Common registers interface for both CDNS3 and CDNSP version of DRD.
878c2ecf20Sopenharmony_ci */
888c2ecf20Sopenharmony_cistruct cdns3_otg_common_regs {
898c2ecf20Sopenharmony_ci	__le32 cmd;
908c2ecf20Sopenharmony_ci	__le32 sts;
918c2ecf20Sopenharmony_ci	__le32 state;
928c2ecf20Sopenharmony_ci};
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/*
958c2ecf20Sopenharmony_ci * Interrupt related registers. This registers are mapped in different
968c2ecf20Sopenharmony_ci * location for CDNSP controller.
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_cistruct cdns3_otg_irq_regs {
998c2ecf20Sopenharmony_ci	__le32 ien;
1008c2ecf20Sopenharmony_ci	__le32 ivect;
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci/* CDNS_RID - bitmasks */
1048c2ecf20Sopenharmony_ci#define CDNS_RID(p)			((p) & GENMASK(15, 0))
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci/* CDNS_VID - bitmasks */
1078c2ecf20Sopenharmony_ci#define CDNS_DID(p)			((p) & GENMASK(31, 0))
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* OTGCMD - bitmasks */
1108c2ecf20Sopenharmony_ci/* "Request the bus for Device mode. */
1118c2ecf20Sopenharmony_ci#define OTGCMD_DEV_BUS_REQ		BIT(0)
1128c2ecf20Sopenharmony_ci/* Request the bus for Host mode */
1138c2ecf20Sopenharmony_ci#define OTGCMD_HOST_BUS_REQ		BIT(1)
1148c2ecf20Sopenharmony_ci/* Enable OTG mode. */
1158c2ecf20Sopenharmony_ci#define OTGCMD_OTG_EN			BIT(2)
1168c2ecf20Sopenharmony_ci/* Disable OTG mode */
1178c2ecf20Sopenharmony_ci#define OTGCMD_OTG_DIS			BIT(3)
1188c2ecf20Sopenharmony_ci/*"Configure OTG as A-Device. */
1198c2ecf20Sopenharmony_ci#define OTGCMD_A_DEV_EN			BIT(4)
1208c2ecf20Sopenharmony_ci/*"Configure OTG as A-Device. */
1218c2ecf20Sopenharmony_ci#define OTGCMD_A_DEV_DIS		BIT(5)
1228c2ecf20Sopenharmony_ci/* Drop the bus for Device mod	e. */
1238c2ecf20Sopenharmony_ci#define OTGCMD_DEV_BUS_DROP		BIT(8)
1248c2ecf20Sopenharmony_ci/* Drop the bus for Host mode*/
1258c2ecf20Sopenharmony_ci#define OTGCMD_HOST_BUS_DROP		BIT(9)
1268c2ecf20Sopenharmony_ci/* Power Down USBSS-DEV - only for CDNS3.*/
1278c2ecf20Sopenharmony_ci#define OTGCMD_DEV_POWER_OFF		BIT(11)
1288c2ecf20Sopenharmony_ci/* Power Down CDNSXHCI - only for CDNS3. */
1298c2ecf20Sopenharmony_ci#define OTGCMD_HOST_POWER_OFF		BIT(12)
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* OTGIEN - bitmasks */
1328c2ecf20Sopenharmony_ci/* ID change interrupt enable */
1338c2ecf20Sopenharmony_ci#define OTGIEN_ID_CHANGE_INT		BIT(0)
1348c2ecf20Sopenharmony_ci/* Vbusvalid fall detected interrupt enable.*/
1358c2ecf20Sopenharmony_ci#define OTGIEN_VBUSVALID_RISE_INT	BIT(4)
1368c2ecf20Sopenharmony_ci/* Vbusvalid fall detected interrupt enable */
1378c2ecf20Sopenharmony_ci#define OTGIEN_VBUSVALID_FALL_INT	BIT(5)
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/* OTGSTS - bitmasks */
1408c2ecf20Sopenharmony_ci/*
1418c2ecf20Sopenharmony_ci * Current value of the ID pin. It is only valid when idpullup in
1428c2ecf20Sopenharmony_ci *  OTGCTRL1_TYPE register is set to '1'.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_ci#define OTGSTS_ID_VALUE			BIT(0)
1458c2ecf20Sopenharmony_ci/* Current value of the vbus_valid */
1468c2ecf20Sopenharmony_ci#define OTGSTS_VBUS_VALID		BIT(1)
1478c2ecf20Sopenharmony_ci/* Current value of the b_sess_vld */
1488c2ecf20Sopenharmony_ci#define OTGSTS_SESSION_VALID		BIT(2)
1498c2ecf20Sopenharmony_ci/*Device mode is active*/
1508c2ecf20Sopenharmony_ci#define OTGSTS_DEV_ACTIVE		BIT(3)
1518c2ecf20Sopenharmony_ci/* Host mode is active. */
1528c2ecf20Sopenharmony_ci#define OTGSTS_HOST_ACTIVE		BIT(4)
1538c2ecf20Sopenharmony_ci/* OTG Controller not ready. */
1548c2ecf20Sopenharmony_ci#define OTGSTS_OTG_NRDY_MASK		BIT(11)
1558c2ecf20Sopenharmony_ci#define OTGSTS_OTG_NRDY(p)		((p) & OTGSTS_OTG_NRDY_MASK)
1568c2ecf20Sopenharmony_ci/*
1578c2ecf20Sopenharmony_ci * Value of the strap pins for:
1588c2ecf20Sopenharmony_ci * CDNS3:
1598c2ecf20Sopenharmony_ci * 000 - no default configuration
1608c2ecf20Sopenharmony_ci * 010 - Controller initiall configured as Host
1618c2ecf20Sopenharmony_ci * 100 - Controller initially configured as Device
1628c2ecf20Sopenharmony_ci * CDNSP:
1638c2ecf20Sopenharmony_ci * 000 - No default configuration.
1648c2ecf20Sopenharmony_ci * 010 - Controller initiall configured as Host.
1658c2ecf20Sopenharmony_ci * 100 - Controller initially configured as Device.
1668c2ecf20Sopenharmony_ci */
1678c2ecf20Sopenharmony_ci#define OTGSTS_STRAP(p)			(((p) & GENMASK(14, 12)) >> 12)
1688c2ecf20Sopenharmony_ci#define OTGSTS_STRAP_NO_DEFAULT_CFG	0x00
1698c2ecf20Sopenharmony_ci#define OTGSTS_STRAP_HOST_OTG		0x01
1708c2ecf20Sopenharmony_ci#define OTGSTS_STRAP_HOST		0x02
1718c2ecf20Sopenharmony_ci#define OTGSTS_STRAP_GADGET		0x04
1728c2ecf20Sopenharmony_ci#define OTGSTS_CDNSP_STRAP_HOST		0x01
1738c2ecf20Sopenharmony_ci#define OTGSTS_CDNSP_STRAP_GADGET	0x02
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* Host mode is turned on. */
1768c2ecf20Sopenharmony_ci#define OTGSTS_CDNS3_XHCI_READY		BIT(26)
1778c2ecf20Sopenharmony_ci#define OTGSTS_CDNSP_XHCI_READY		BIT(27)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* "Device mode is turned on .*/
1808c2ecf20Sopenharmony_ci#define OTGSTS_CDNS3_DEV_READY		BIT(27)
1818c2ecf20Sopenharmony_ci#define OTGSTS_CDNSP_DEV_READY		BIT(26)
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* OTGSTATE- bitmasks */
1848c2ecf20Sopenharmony_ci#define OTGSTATE_DEV_STATE_MASK		GENMASK(2, 0)
1858c2ecf20Sopenharmony_ci#define OTGSTATE_HOST_STATE_MASK	GENMASK(5, 3)
1868c2ecf20Sopenharmony_ci#define OTGSTATE_HOST_STATE_IDLE	0x0
1878c2ecf20Sopenharmony_ci#define OTGSTATE_HOST_STATE_VBUS_FALL	0x7
1888c2ecf20Sopenharmony_ci#define OTGSTATE_HOST_STATE(p)		(((p) & OTGSTATE_HOST_STATE_MASK) >> 3)
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/* OTGREFCLK - bitmasks */
1918c2ecf20Sopenharmony_ci#define OTGREFCLK_STB_CLK_SWITCH_EN	BIT(31)
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/* OVERRIDE - bitmasks */
1948c2ecf20Sopenharmony_ci#define OVERRIDE_IDPULLUP		BIT(0)
1958c2ecf20Sopenharmony_ci/* Only for CDNS3_CONTROLLER_V0 version */
1968c2ecf20Sopenharmony_ci#define OVERRIDE_IDPULLUP_V0		BIT(24)
1978c2ecf20Sopenharmony_ci/* Vbusvalid/Sesvalid override select. */
1988c2ecf20Sopenharmony_ci#define OVERRIDE_SESS_VLD_SEL		BIT(10)
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci/* PHYRST_CFG - bitmasks */
2018c2ecf20Sopenharmony_ci#define PHYRST_CFG_PHYRST_A_ENABLE     BIT(0)
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#define CDNS3_ID_PERIPHERAL		1
2048c2ecf20Sopenharmony_ci#define CDNS3_ID_HOST			0
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cibool cdns3_is_host(struct cdns3 *cdns);
2078c2ecf20Sopenharmony_cibool cdns3_is_device(struct cdns3 *cdns);
2088c2ecf20Sopenharmony_ciint cdns3_get_id(struct cdns3 *cdns);
2098c2ecf20Sopenharmony_ciint cdns3_get_vbus(struct cdns3 *cdns);
2108c2ecf20Sopenharmony_ciint cdns3_drd_init(struct cdns3 *cdns);
2118c2ecf20Sopenharmony_ciint cdns3_drd_exit(struct cdns3 *cdns);
2128c2ecf20Sopenharmony_ciint cdns3_drd_update_mode(struct cdns3 *cdns);
2138c2ecf20Sopenharmony_ciint cdns3_drd_gadget_on(struct cdns3 *cdns);
2148c2ecf20Sopenharmony_civoid cdns3_drd_gadget_off(struct cdns3 *cdns);
2158c2ecf20Sopenharmony_ciint cdns3_drd_host_on(struct cdns3 *cdns);
2168c2ecf20Sopenharmony_civoid cdns3_drd_host_off(struct cdns3 *cdns);
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#endif /* __LINUX_CDNS3_DRD */
219