blob: 13626c113ab2130c708db49f4d34840d17ffa0dd [file] [log] [blame]
developerba28e032021-12-07 10:40:00 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * xHCI host controller toolkit driver for chgdt-en
4 *
5 * Copyright (C) 2021 MediaTek Inc.
6 *
7 * Author: Zhanyong Wang <zhanyong.wang@mediatek.com>
8 */
9
10#include <linux/platform_device.h>
11#include <linux/module.h>
12#include <linux/slab.h>
13#include <linux/usb.h>
14#include "xhci-mtk.h"
15#include "xhci-mtk-test.h"
16#include "xhci-mtk-unusual.h"
17
18static ssize_t RG_CHGDT_EN_show(struct device *dev,
19 struct device_attribute *attr, char *buf)
20{
21 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
22 struct usb_hcd *hcd = mtk->hcd;
23 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
24 ssize_t cnt = 0;
25 void __iomem *addr;
26 u32 val;
27 u32 i;
28 int ports;
29 char str[32];
30 int index = 0;
31 u32 io, length;
32 int ret;
33
34 ports = mtk->num_u3_ports + mtk->num_u2_ports;
35 cnt += sprintf(buf + cnt, " RG_CHGDT_EN usage:\n");
36 cnt += sprintf(buf + cnt,
37 " echo u2p index 1b0 > RG_CHGDT_EN\n");
38 if (mtk->num_u3_ports + 1 != ports)
39 cnt += sprintf(buf + cnt, " parameter: u2p: %i ~ %i\n",
40 mtk->num_u3_ports + 1, ports);
41 else
42 cnt += sprintf(buf + cnt, " parameter: u2p: %i\n",
43 mtk->num_u3_ports + 1);
44 if (mtk->num_u2_ports > 1)
45 cnt += sprintf(buf + cnt, " parameter: index: 0 ~ %i\n",
46 mtk->num_u2_ports);
47 else
48 cnt += sprintf(buf + cnt, " parameter: index: 0\n");
49
50 cnt += sprintf(buf + cnt, " e.g.: echo 2 0 1b1 > RG_CHGDT_EN\n");
51 cnt += sprintf(buf + cnt,
52 " port2 binding phy 0, enable 1b'1 as CHGDT_EN\n");
53
54 cnt += sprintf(buf + cnt,
55 "\n=========current HQA setting check=========\n");
56 for (i = 1; i <= ports; i++) {
57 addr = &xhci->op_regs->port_status_base +
58 NUM_PORT_REGS * ((i - 1) & 0xff);
59 val = readl(addr);
60 if (i <= mtk->num_u3_ports) {
61 cnt += sprintf(buf + cnt,
62 "USB30 Port%i: 0x%08X\n", i, val);
63 } else {
64 cnt += sprintf(buf + cnt,
65 "USB20 Port%i: 0x%08X\n", i, val);
66
67 ret = query_phy_addr(dev->of_node,
68 &index, &io, &length);
69 if (ret && ret != -EACCES) {
70 if (ret == -EPERM)
71 cnt += sprintf(buf + cnt,
72 "USB20 Port%i (Phy%i: absent)\n",
73 i, index);
74 else
75 cnt += sprintf(buf + cnt,
76 "USB20 Port%i (Phy%i) failure %i\n",
77 i, index, ret);
78 continue;
79 }
80
81 cnt += sprintf(buf + cnt,
82 "USB20 Port%i (Phy%i:%sable): 0x%08X 0x%08X\n",
83 i, index, ret ? " dis" : " en", io, length);
84
85 addr = ioremap_nocache(io, length);
86 addr += (length != 0x100) ? 0x300 : 0;
87
88 HQA_INFORMACTION_COLLECTS();
89
90 iounmap(addr);
91 index ++;
92 }
93 }
94
95 if (mtk->hqa_pos) {
96 cnt += sprintf(buf + cnt, "%s", mtk->hqa_buf);
97 mtk->hqa_pos = 0;
98 }
99
100 return cnt;
101}
102
103static ssize_t RG_CHGDT_EN_store(struct device *dev,
104 struct device_attribute *attr,
105 const char *buf, size_t n)
106{
107 u32 val;
108 u32 io;
109 u32 length;
110 int ports;
111 int words;
112 int port;
113 int index;
114 int ret;
115 char *str = NULL;
116 void __iomem *addr;
117 struct xhci_hcd_mtk *mtk = dev_get_drvdata(dev);
118 struct device_node *node = dev->of_node;
119
120 ports = mtk->num_u3_ports + mtk->num_u2_ports;
121 mtk->hqa_pos = 0;
122
123 memset(mtk->hqa_buf, 0, mtk->hqa_size);
124
125 str = kzalloc(n, GFP_ATOMIC);
126
127 hqa_info(mtk, "RG_CHGDT_EN(%lu): %s\n", n, buf);
128
129 words = sscanf(buf, "%i %i 1b%1[0,1]", &port, &index, str);
130 if ((words != 3) ||
131 (port < mtk->num_u3_ports || port > ports)) {
132 hqa_info(mtk, "Check params(%i):\" %i %i %s\", Please!\n",
133 words, port, index, str);
134
135 ret = -EINVAL;
136 goto error;
137 }
138
139 hqa_info(mtk, " params: %i %i %s\n",
140 port, index, str);
141
142 ret = query_phy_addr(node, &index, &io, &length);
143 if (ret && ret != -EACCES)
144 goto error;
145
146 io += (length != 0x100) ? 0x300 : 0;
147 io += USB20_PHY_U2PHYBC12C;
148
149 addr = ioremap_nocache(io, 4);
150 val = binary_write_width1(addr, SHFT_RG_CHGDT_EN, str);
151 hqa_info(mtk, "Port%i(Phy%i)[0x%08X]: 0x%08X but 0x%08X\n",
152 port, index, io, val, readl(addr));
153
154 iounmap(addr);
155 ret = n;
156
157error:
158 kfree(str);
159 return ret;
160}
161DEVICE_ATTR_RW(RG_CHGDT_EN);