blob: f1cf9160d5657545dc4375f0cdfd47b5b68a9a3e [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
6#include <common.h>
Simon Glassf11478f2019-12-28 10:45:07 -07007#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.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
44static const struct rockchip_usb2_phy_cfg rk3288_pdata = {
45 .port_reset = {0x00, 12, 12, 0, 1},
46 .soft_con = {0x08, 2, 2, 0, 1},
47 .suspend = {0x0c, 5, 0, 0x01, 0x2A},
48};
49
50static struct rockchip_usb2_phy_dt_id rockchip_usb2_phy_dt_ids[] = {
51 { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
52 {}
53};
54
55static void property_enable(struct dwc2_plat_otg_data *pdata,
56 const struct usb2phy_reg *reg, bool en)
57{
58 unsigned int val, mask, tmp;
59
60 tmp = en ? reg->enable : reg->disable;
61 mask = GENMASK(reg->bitend, reg->bitstart);
62 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
63
64 writel(val, pdata->regs_phy + reg->offset);
65}
66
67
68void otg_phy_init(struct dwc2_udc *dev)
69{
70 struct dwc2_plat_otg_data *pdata = dev->pdata;
71 struct rockchip_usb2_phy_cfg *phy_cfg = NULL;
72 struct rockchip_usb2_phy_dt_id *of_id;
73 int i;
74
75 for (i = 0; i < ARRAY_SIZE(rockchip_usb2_phy_dt_ids); i++) {
76 of_id = &rockchip_usb2_phy_dt_ids[i];
Kever Yang45bda032019-10-16 17:13:31 +080077 if (ofnode_device_is_compatible(pdata->phy_of_node,
78 of_id->compatible)){
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080079 phy_cfg = (struct rockchip_usb2_phy_cfg *)of_id->data;
80 break;
81 }
82 }
83 if (!phy_cfg) {
84 debug("Can't find device platform data\n");
85
86 hang();
87 return;
88 }
89 pdata->priv = phy_cfg;
90 /* disable software control */
91 property_enable(pdata, &phy_cfg->soft_con, false);
92
93 /* reset otg port */
94 property_enable(pdata, &phy_cfg->port_reset, true);
95 mdelay(1);
96 property_enable(pdata, &phy_cfg->port_reset, false);
97 udelay(1);
98}
99
100void otg_phy_off(struct dwc2_udc *dev)
101{
102 struct dwc2_plat_otg_data *pdata = dev->pdata;
103 struct rockchip_usb2_phy_cfg *phy_cfg = pdata->priv;
104
105 /* enable software control */
106 property_enable(pdata, &phy_cfg->soft_con, true);
107 /* enter suspend */
108 property_enable(pdata, &phy_cfg->suspend, true);
109}