blob: ce9a7b5b81952a942525fdd504b552cfa93de9f2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +08002/*
3 * Copyright 2016 Rockchip Electronics Co., Ltd
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +08004 */
5
Simon Glassf11478f2019-12-28 10:45:07 -07006#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <asm/global_data.h>
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +08009#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060010#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060011#include <linux/delay.h>
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080012
13#include "../gadget/dwc2_udc_otg_priv.h"
14
15DECLARE_GLOBAL_DATA_PTR;
16
17#define BIT_WRITEABLE_SHIFT 16
18
19struct usb2phy_reg {
20 unsigned int offset;
21 unsigned int bitend;
22 unsigned int bitstart;
23 unsigned int disable;
24 unsigned int enable;
25};
26
27/**
28 * struct rockchip_usb2_phy_cfg: usb-phy port configuration
29 * @port_reset: usb otg per-port reset register
30 * @soft_con: software control usb otg register
31 * @suspend: phy suspend register
32 */
33struct rockchip_usb2_phy_cfg {
34 struct usb2phy_reg port_reset;
35 struct usb2phy_reg soft_con;
36 struct usb2phy_reg suspend;
37};
38
39struct rockchip_usb2_phy_dt_id {
40 char compatible[128];
41 const void *data;
42};
43
Johan Jonkerc7a39e42022-04-29 23:40:06 +020044static const struct rockchip_usb2_phy_cfg rk3066a_pdata = {
45 .port_reset = {0x00, 12, 12, 0, 1},
46 .soft_con = {0x08, 2, 2, 0, 1},
47 .suspend = {0x08, 8, 3, (0x01 << 3), (0x2A << 3)},
48};
49
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080050static const struct rockchip_usb2_phy_cfg rk3288_pdata = {
51 .port_reset = {0x00, 12, 12, 0, 1},
52 .soft_con = {0x08, 2, 2, 0, 1},
53 .suspend = {0x0c, 5, 0, 0x01, 0x2A},
54};
55
56static struct rockchip_usb2_phy_dt_id rockchip_usb2_phy_dt_ids[] = {
Johan Jonkerc7a39e42022-04-29 23:40:06 +020057 { .compatible = "rockchip,rk3066a-usb-phy", .data = &rk3066a_pdata },
58 { .compatible = "rockchip,rk3188-usb-phy", .data = &rk3288_pdata },
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080059 { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
60 {}
61};
62
63static void property_enable(struct dwc2_plat_otg_data *pdata,
64 const struct usb2phy_reg *reg, bool en)
65{
66 unsigned int val, mask, tmp;
67
68 tmp = en ? reg->enable : reg->disable;
69 mask = GENMASK(reg->bitend, reg->bitstart);
70 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
71
72 writel(val, pdata->regs_phy + reg->offset);
73}
74
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080075void otg_phy_init(struct dwc2_udc *dev)
76{
77 struct dwc2_plat_otg_data *pdata = dev->pdata;
78 struct rockchip_usb2_phy_cfg *phy_cfg = NULL;
79 struct rockchip_usb2_phy_dt_id *of_id;
80 int i;
81
82 for (i = 0; i < ARRAY_SIZE(rockchip_usb2_phy_dt_ids); i++) {
83 of_id = &rockchip_usb2_phy_dt_ids[i];
Kever Yang45bda032019-10-16 17:13:31 +080084 if (ofnode_device_is_compatible(pdata->phy_of_node,
85 of_id->compatible)){
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080086 phy_cfg = (struct rockchip_usb2_phy_cfg *)of_id->data;
87 break;
88 }
89 }
90 if (!phy_cfg) {
91 debug("Can't find device platform data\n");
92
93 hang();
94 return;
95 }
96 pdata->priv = phy_cfg;
97 /* disable software control */
98 property_enable(pdata, &phy_cfg->soft_con, false);
99
100 /* reset otg port */
101 property_enable(pdata, &phy_cfg->port_reset, true);
102 mdelay(1);
103 property_enable(pdata, &phy_cfg->port_reset, false);
104 udelay(1);
105}
106
107void otg_phy_off(struct dwc2_udc *dev)
108{
109 struct dwc2_plat_otg_data *pdata = dev->pdata;
110 struct rockchip_usb2_phy_cfg *phy_cfg = pdata->priv;
111
112 /* enable software control */
113 property_enable(pdata, &phy_cfg->soft_con, true);
114 /* enter suspend */
115 property_enable(pdata, &phy_cfg->suspend, true);
116}