blob: 93caa821a1d2823fc6b1e1d942c7d0a9bc5d4b87 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080010#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060011#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080013
14#include "../gadget/dwc2_udc_otg_priv.h"
15
16DECLARE_GLOBAL_DATA_PTR;
17
18#define BIT_WRITEABLE_SHIFT 16
19
20struct usb2phy_reg {
21 unsigned int offset;
22 unsigned int bitend;
23 unsigned int bitstart;
24 unsigned int disable;
25 unsigned int enable;
26};
27
28/**
29 * struct rockchip_usb2_phy_cfg: usb-phy port configuration
30 * @port_reset: usb otg per-port reset register
31 * @soft_con: software control usb otg register
32 * @suspend: phy suspend register
33 */
34struct rockchip_usb2_phy_cfg {
35 struct usb2phy_reg port_reset;
36 struct usb2phy_reg soft_con;
37 struct usb2phy_reg suspend;
38};
39
40struct rockchip_usb2_phy_dt_id {
41 char compatible[128];
42 const void *data;
43};
44
45static const struct rockchip_usb2_phy_cfg rk3288_pdata = {
46 .port_reset = {0x00, 12, 12, 0, 1},
47 .soft_con = {0x08, 2, 2, 0, 1},
48 .suspend = {0x0c, 5, 0, 0x01, 0x2A},
49};
50
51static struct rockchip_usb2_phy_dt_id rockchip_usb2_phy_dt_ids[] = {
52 { .compatible = "rockchip,rk3288-usb-phy", .data = &rk3288_pdata },
53 {}
54};
55
56static void property_enable(struct dwc2_plat_otg_data *pdata,
57 const struct usb2phy_reg *reg, bool en)
58{
59 unsigned int val, mask, tmp;
60
61 tmp = en ? reg->enable : reg->disable;
62 mask = GENMASK(reg->bitend, reg->bitstart);
63 val = (tmp << reg->bitstart) | (mask << BIT_WRITEABLE_SHIFT);
64
65 writel(val, pdata->regs_phy + reg->offset);
66}
67
68
69void otg_phy_init(struct dwc2_udc *dev)
70{
71 struct dwc2_plat_otg_data *pdata = dev->pdata;
72 struct rockchip_usb2_phy_cfg *phy_cfg = NULL;
73 struct rockchip_usb2_phy_dt_id *of_id;
74 int i;
75
76 for (i = 0; i < ARRAY_SIZE(rockchip_usb2_phy_dt_ids); i++) {
77 of_id = &rockchip_usb2_phy_dt_ids[i];
Kever Yang45bda032019-10-16 17:13:31 +080078 if (ofnode_device_is_compatible(pdata->phy_of_node,
79 of_id->compatible)){
Xu Ziyuan1ecf3e42016-07-14 14:52:32 +080080 phy_cfg = (struct rockchip_usb2_phy_cfg *)of_id->data;
81 break;
82 }
83 }
84 if (!phy_cfg) {
85 debug("Can't find device platform data\n");
86
87 hang();
88 return;
89 }
90 pdata->priv = phy_cfg;
91 /* disable software control */
92 property_enable(pdata, &phy_cfg->soft_con, false);
93
94 /* reset otg port */
95 property_enable(pdata, &phy_cfg->port_reset, true);
96 mdelay(1);
97 property_enable(pdata, &phy_cfg->port_reset, false);
98 udelay(1);
99}
100
101void otg_phy_off(struct dwc2_udc *dev)
102{
103 struct dwc2_plat_otg_data *pdata = dev->pdata;
104 struct rockchip_usb2_phy_cfg *phy_cfg = pdata->priv;
105
106 /* enable software control */
107 property_enable(pdata, &phy_cfg->soft_con, true);
108 /* enter suspend */
109 property_enable(pdata, &phy_cfg->suspend, true);
110}