162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * AMD 10Gb Ethernet driver
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This file is available to you under your choice of the following two
562306a36Sopenharmony_ci * licenses:
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * License 1: GPLv2
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (c) 2014 Advanced Micro Devices, Inc.
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * This file is free software; you may copy, redistribute and/or modify
1262306a36Sopenharmony_ci * it under the terms of the GNU General Public License as published by
1362306a36Sopenharmony_ci * the Free Software Foundation, either version 2 of the License, or (at
1462306a36Sopenharmony_ci * your option) any later version.
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * This file is distributed in the hope that it will be useful, but
1762306a36Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
1862306a36Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1962306a36Sopenharmony_ci * General Public License for more details.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * You should have received a copy of the GNU General Public License
2262306a36Sopenharmony_ci * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * This file incorporates work covered by the following copyright and
2562306a36Sopenharmony_ci * permission notice:
2662306a36Sopenharmony_ci *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
2762306a36Sopenharmony_ci *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
2862306a36Sopenharmony_ci *     Inc. unless otherwise expressly agreed to in writing between Synopsys
2962306a36Sopenharmony_ci *     and you.
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci *     The Software IS NOT an item of Licensed Software or Licensed Product
3262306a36Sopenharmony_ci *     under any End User Software License Agreement or Agreement for Licensed
3362306a36Sopenharmony_ci *     Product with Synopsys or any supplement thereto.  Permission is hereby
3462306a36Sopenharmony_ci *     granted, free of charge, to any person obtaining a copy of this software
3562306a36Sopenharmony_ci *     annotated with this license and the Software, to deal in the Software
3662306a36Sopenharmony_ci *     without restriction, including without limitation the rights to use,
3762306a36Sopenharmony_ci *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
3862306a36Sopenharmony_ci *     of the Software, and to permit persons to whom the Software is furnished
3962306a36Sopenharmony_ci *     to do so, subject to the following conditions:
4062306a36Sopenharmony_ci *
4162306a36Sopenharmony_ci *     The above copyright notice and this permission notice shall be included
4262306a36Sopenharmony_ci *     in all copies or substantial portions of the Software.
4362306a36Sopenharmony_ci *
4462306a36Sopenharmony_ci *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
4562306a36Sopenharmony_ci *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
4662306a36Sopenharmony_ci *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
4762306a36Sopenharmony_ci *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
4862306a36Sopenharmony_ci *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
4962306a36Sopenharmony_ci *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
5062306a36Sopenharmony_ci *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
5162306a36Sopenharmony_ci *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
5262306a36Sopenharmony_ci *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5362306a36Sopenharmony_ci *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
5462306a36Sopenharmony_ci *     THE POSSIBILITY OF SUCH DAMAGE.
5562306a36Sopenharmony_ci *
5662306a36Sopenharmony_ci *
5762306a36Sopenharmony_ci * License 2: Modified BSD
5862306a36Sopenharmony_ci *
5962306a36Sopenharmony_ci * Copyright (c) 2014 Advanced Micro Devices, Inc.
6062306a36Sopenharmony_ci * All rights reserved.
6162306a36Sopenharmony_ci *
6262306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
6362306a36Sopenharmony_ci * modification, are permitted provided that the following conditions are met:
6462306a36Sopenharmony_ci *     * Redistributions of source code must retain the above copyright
6562306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer.
6662306a36Sopenharmony_ci *     * Redistributions in binary form must reproduce the above copyright
6762306a36Sopenharmony_ci *       notice, this list of conditions and the following disclaimer in the
6862306a36Sopenharmony_ci *       documentation and/or other materials provided with the distribution.
6962306a36Sopenharmony_ci *     * Neither the name of Advanced Micro Devices, Inc. nor the
7062306a36Sopenharmony_ci *       names of its contributors may be used to endorse or promote products
7162306a36Sopenharmony_ci *       derived from this software without specific prior written permission.
7262306a36Sopenharmony_ci *
7362306a36Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
7462306a36Sopenharmony_ci * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
7562306a36Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
7662306a36Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
7762306a36Sopenharmony_ci * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
7862306a36Sopenharmony_ci * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
7962306a36Sopenharmony_ci * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
8062306a36Sopenharmony_ci * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8162306a36Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
8262306a36Sopenharmony_ci * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8362306a36Sopenharmony_ci *
8462306a36Sopenharmony_ci * This file incorporates work covered by the following copyright and
8562306a36Sopenharmony_ci * permission notice:
8662306a36Sopenharmony_ci *     The Synopsys DWC ETHER XGMAC Software Driver and documentation
8762306a36Sopenharmony_ci *     (hereinafter "Software") is an unsupported proprietary work of Synopsys,
8862306a36Sopenharmony_ci *     Inc. unless otherwise expressly agreed to in writing between Synopsys
8962306a36Sopenharmony_ci *     and you.
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci *     The Software IS NOT an item of Licensed Software or Licensed Product
9262306a36Sopenharmony_ci *     under any End User Software License Agreement or Agreement for Licensed
9362306a36Sopenharmony_ci *     Product with Synopsys or any supplement thereto.  Permission is hereby
9462306a36Sopenharmony_ci *     granted, free of charge, to any person obtaining a copy of this software
9562306a36Sopenharmony_ci *     annotated with this license and the Software, to deal in the Software
9662306a36Sopenharmony_ci *     without restriction, including without limitation the rights to use,
9762306a36Sopenharmony_ci *     copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9862306a36Sopenharmony_ci *     of the Software, and to permit persons to whom the Software is furnished
9962306a36Sopenharmony_ci *     to do so, subject to the following conditions:
10062306a36Sopenharmony_ci *
10162306a36Sopenharmony_ci *     The above copyright notice and this permission notice shall be included
10262306a36Sopenharmony_ci *     in all copies or substantial portions of the Software.
10362306a36Sopenharmony_ci *
10462306a36Sopenharmony_ci *     THIS SOFTWARE IS BEING DISTRIBUTED BY SYNOPSYS SOLELY ON AN "AS IS"
10562306a36Sopenharmony_ci *     BASIS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
10662306a36Sopenharmony_ci *     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
10762306a36Sopenharmony_ci *     PARTICULAR PURPOSE ARE HEREBY DISCLAIMED. IN NO EVENT SHALL SYNOPSYS
10862306a36Sopenharmony_ci *     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
10962306a36Sopenharmony_ci *     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
11062306a36Sopenharmony_ci *     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
11162306a36Sopenharmony_ci *     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
11262306a36Sopenharmony_ci *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
11362306a36Sopenharmony_ci *     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
11462306a36Sopenharmony_ci *     THE POSSIBILITY OF SUCH DAMAGE.
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci#include <linux/debugfs.h>
11862306a36Sopenharmony_ci#include <linux/module.h>
11962306a36Sopenharmony_ci#include <linux/slab.h>
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#include "xgbe.h"
12262306a36Sopenharmony_ci#include "xgbe-common.h"
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic ssize_t xgbe_common_read(char __user *buffer, size_t count,
12562306a36Sopenharmony_ci				loff_t *ppos, unsigned int value)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	char *buf;
12862306a36Sopenharmony_ci	ssize_t len;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	if (*ppos != 0)
13162306a36Sopenharmony_ci		return 0;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	buf = kasprintf(GFP_KERNEL, "0x%08x\n", value);
13462306a36Sopenharmony_ci	if (!buf)
13562306a36Sopenharmony_ci		return -ENOMEM;
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci	if (count < strlen(buf)) {
13862306a36Sopenharmony_ci		kfree(buf);
13962306a36Sopenharmony_ci		return -ENOSPC;
14062306a36Sopenharmony_ci	}
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	len = simple_read_from_buffer(buffer, count, ppos, buf, strlen(buf));
14362306a36Sopenharmony_ci	kfree(buf);
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	return len;
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic ssize_t xgbe_common_write(const char __user *buffer, size_t count,
14962306a36Sopenharmony_ci				 loff_t *ppos, unsigned int *value)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	char workarea[32];
15262306a36Sopenharmony_ci	ssize_t len;
15362306a36Sopenharmony_ci	int ret;
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	if (*ppos != 0)
15662306a36Sopenharmony_ci		return -EINVAL;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	if (count >= sizeof(workarea))
15962306a36Sopenharmony_ci		return -ENOSPC;
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	len = simple_write_to_buffer(workarea, sizeof(workarea) - 1, ppos,
16262306a36Sopenharmony_ci				     buffer, count);
16362306a36Sopenharmony_ci	if (len < 0)
16462306a36Sopenharmony_ci		return len;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci	workarea[len] = '\0';
16762306a36Sopenharmony_ci	ret = kstrtouint(workarea, 16, value);
16862306a36Sopenharmony_ci	if (ret)
16962306a36Sopenharmony_ci		return -EIO;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	return len;
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_cistatic ssize_t xgmac_reg_addr_read(struct file *filp, char __user *buffer,
17562306a36Sopenharmony_ci				   size_t count, loff_t *ppos)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xgmac_reg);
18062306a36Sopenharmony_ci}
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic ssize_t xgmac_reg_addr_write(struct file *filp,
18362306a36Sopenharmony_ci				    const char __user *buffer,
18462306a36Sopenharmony_ci				    size_t count, loff_t *ppos)
18562306a36Sopenharmony_ci{
18662306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	return xgbe_common_write(buffer, count, ppos,
18962306a36Sopenharmony_ci				 &pdata->debugfs_xgmac_reg);
19062306a36Sopenharmony_ci}
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic ssize_t xgmac_reg_value_read(struct file *filp, char __user *buffer,
19362306a36Sopenharmony_ci				    size_t count, loff_t *ppos)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
19662306a36Sopenharmony_ci	unsigned int value;
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	value = XGMAC_IOREAD(pdata, pdata->debugfs_xgmac_reg);
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, value);
20162306a36Sopenharmony_ci}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_cistatic ssize_t xgmac_reg_value_write(struct file *filp,
20462306a36Sopenharmony_ci				     const char __user *buffer,
20562306a36Sopenharmony_ci				     size_t count, loff_t *ppos)
20662306a36Sopenharmony_ci{
20762306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
20862306a36Sopenharmony_ci	unsigned int value;
20962306a36Sopenharmony_ci	ssize_t len;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	len = xgbe_common_write(buffer, count, ppos, &value);
21262306a36Sopenharmony_ci	if (len < 0)
21362306a36Sopenharmony_ci		return len;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	XGMAC_IOWRITE(pdata, pdata->debugfs_xgmac_reg, value);
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci	return len;
21862306a36Sopenharmony_ci}
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cistatic const struct file_operations xgmac_reg_addr_fops = {
22162306a36Sopenharmony_ci	.owner = THIS_MODULE,
22262306a36Sopenharmony_ci	.open = simple_open,
22362306a36Sopenharmony_ci	.read =  xgmac_reg_addr_read,
22462306a36Sopenharmony_ci	.write = xgmac_reg_addr_write,
22562306a36Sopenharmony_ci};
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_cistatic const struct file_operations xgmac_reg_value_fops = {
22862306a36Sopenharmony_ci	.owner = THIS_MODULE,
22962306a36Sopenharmony_ci	.open = simple_open,
23062306a36Sopenharmony_ci	.read =  xgmac_reg_value_read,
23162306a36Sopenharmony_ci	.write = xgmac_reg_value_write,
23262306a36Sopenharmony_ci};
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_cistatic ssize_t xpcs_mmd_read(struct file *filp, char __user *buffer,
23562306a36Sopenharmony_ci			     size_t count, loff_t *ppos)
23662306a36Sopenharmony_ci{
23762306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xpcs_mmd);
24062306a36Sopenharmony_ci}
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_cistatic ssize_t xpcs_mmd_write(struct file *filp, const char __user *buffer,
24362306a36Sopenharmony_ci			      size_t count, loff_t *ppos)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	return xgbe_common_write(buffer, count, ppos,
24862306a36Sopenharmony_ci				 &pdata->debugfs_xpcs_mmd);
24962306a36Sopenharmony_ci}
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_cistatic ssize_t xpcs_reg_addr_read(struct file *filp, char __user *buffer,
25262306a36Sopenharmony_ci				  size_t count, loff_t *ppos)
25362306a36Sopenharmony_ci{
25462306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xpcs_reg);
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic ssize_t xpcs_reg_addr_write(struct file *filp, const char __user *buffer,
26062306a36Sopenharmony_ci				   size_t count, loff_t *ppos)
26162306a36Sopenharmony_ci{
26262306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	return xgbe_common_write(buffer, count, ppos,
26562306a36Sopenharmony_ci				 &pdata->debugfs_xpcs_reg);
26662306a36Sopenharmony_ci}
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_cistatic ssize_t xpcs_reg_value_read(struct file *filp, char __user *buffer,
26962306a36Sopenharmony_ci				   size_t count, loff_t *ppos)
27062306a36Sopenharmony_ci{
27162306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
27262306a36Sopenharmony_ci	unsigned int value;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci	value = XMDIO_READ(pdata, pdata->debugfs_xpcs_mmd,
27562306a36Sopenharmony_ci			   pdata->debugfs_xpcs_reg);
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, value);
27862306a36Sopenharmony_ci}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_cistatic ssize_t xpcs_reg_value_write(struct file *filp,
28162306a36Sopenharmony_ci				    const char __user *buffer,
28262306a36Sopenharmony_ci				    size_t count, loff_t *ppos)
28362306a36Sopenharmony_ci{
28462306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
28562306a36Sopenharmony_ci	unsigned int value;
28662306a36Sopenharmony_ci	ssize_t len;
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ci	len = xgbe_common_write(buffer, count, ppos, &value);
28962306a36Sopenharmony_ci	if (len < 0)
29062306a36Sopenharmony_ci		return len;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	XMDIO_WRITE(pdata, pdata->debugfs_xpcs_mmd, pdata->debugfs_xpcs_reg,
29362306a36Sopenharmony_ci		    value);
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	return len;
29662306a36Sopenharmony_ci}
29762306a36Sopenharmony_ci
29862306a36Sopenharmony_cistatic const struct file_operations xpcs_mmd_fops = {
29962306a36Sopenharmony_ci	.owner = THIS_MODULE,
30062306a36Sopenharmony_ci	.open = simple_open,
30162306a36Sopenharmony_ci	.read =  xpcs_mmd_read,
30262306a36Sopenharmony_ci	.write = xpcs_mmd_write,
30362306a36Sopenharmony_ci};
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_cistatic const struct file_operations xpcs_reg_addr_fops = {
30662306a36Sopenharmony_ci	.owner = THIS_MODULE,
30762306a36Sopenharmony_ci	.open = simple_open,
30862306a36Sopenharmony_ci	.read =  xpcs_reg_addr_read,
30962306a36Sopenharmony_ci	.write = xpcs_reg_addr_write,
31062306a36Sopenharmony_ci};
31162306a36Sopenharmony_ci
31262306a36Sopenharmony_cistatic const struct file_operations xpcs_reg_value_fops = {
31362306a36Sopenharmony_ci	.owner = THIS_MODULE,
31462306a36Sopenharmony_ci	.open = simple_open,
31562306a36Sopenharmony_ci	.read =  xpcs_reg_value_read,
31662306a36Sopenharmony_ci	.write = xpcs_reg_value_write,
31762306a36Sopenharmony_ci};
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_cistatic ssize_t xprop_reg_addr_read(struct file *filp, char __user *buffer,
32062306a36Sopenharmony_ci				   size_t count, loff_t *ppos)
32162306a36Sopenharmony_ci{
32262306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xprop_reg);
32562306a36Sopenharmony_ci}
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_cistatic ssize_t xprop_reg_addr_write(struct file *filp,
32862306a36Sopenharmony_ci				    const char __user *buffer,
32962306a36Sopenharmony_ci				    size_t count, loff_t *ppos)
33062306a36Sopenharmony_ci{
33162306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	return xgbe_common_write(buffer, count, ppos,
33462306a36Sopenharmony_ci				 &pdata->debugfs_xprop_reg);
33562306a36Sopenharmony_ci}
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_cistatic ssize_t xprop_reg_value_read(struct file *filp, char __user *buffer,
33862306a36Sopenharmony_ci				    size_t count, loff_t *ppos)
33962306a36Sopenharmony_ci{
34062306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
34162306a36Sopenharmony_ci	unsigned int value;
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	value = XP_IOREAD(pdata, pdata->debugfs_xprop_reg);
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, value);
34662306a36Sopenharmony_ci}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_cistatic ssize_t xprop_reg_value_write(struct file *filp,
34962306a36Sopenharmony_ci				     const char __user *buffer,
35062306a36Sopenharmony_ci				     size_t count, loff_t *ppos)
35162306a36Sopenharmony_ci{
35262306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
35362306a36Sopenharmony_ci	unsigned int value;
35462306a36Sopenharmony_ci	ssize_t len;
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci	len = xgbe_common_write(buffer, count, ppos, &value);
35762306a36Sopenharmony_ci	if (len < 0)
35862306a36Sopenharmony_ci		return len;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	XP_IOWRITE(pdata, pdata->debugfs_xprop_reg, value);
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	return len;
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_cistatic const struct file_operations xprop_reg_addr_fops = {
36662306a36Sopenharmony_ci	.owner = THIS_MODULE,
36762306a36Sopenharmony_ci	.open = simple_open,
36862306a36Sopenharmony_ci	.read =  xprop_reg_addr_read,
36962306a36Sopenharmony_ci	.write = xprop_reg_addr_write,
37062306a36Sopenharmony_ci};
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_cistatic const struct file_operations xprop_reg_value_fops = {
37362306a36Sopenharmony_ci	.owner = THIS_MODULE,
37462306a36Sopenharmony_ci	.open = simple_open,
37562306a36Sopenharmony_ci	.read =  xprop_reg_value_read,
37662306a36Sopenharmony_ci	.write = xprop_reg_value_write,
37762306a36Sopenharmony_ci};
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_cistatic ssize_t xi2c_reg_addr_read(struct file *filp, char __user *buffer,
38062306a36Sopenharmony_ci				  size_t count, loff_t *ppos)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, pdata->debugfs_xi2c_reg);
38562306a36Sopenharmony_ci}
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_cistatic ssize_t xi2c_reg_addr_write(struct file *filp,
38862306a36Sopenharmony_ci				   const char __user *buffer,
38962306a36Sopenharmony_ci				   size_t count, loff_t *ppos)
39062306a36Sopenharmony_ci{
39162306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	return xgbe_common_write(buffer, count, ppos,
39462306a36Sopenharmony_ci				 &pdata->debugfs_xi2c_reg);
39562306a36Sopenharmony_ci}
39662306a36Sopenharmony_ci
39762306a36Sopenharmony_cistatic ssize_t xi2c_reg_value_read(struct file *filp, char __user *buffer,
39862306a36Sopenharmony_ci				   size_t count, loff_t *ppos)
39962306a36Sopenharmony_ci{
40062306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
40162306a36Sopenharmony_ci	unsigned int value;
40262306a36Sopenharmony_ci
40362306a36Sopenharmony_ci	value = XI2C_IOREAD(pdata, pdata->debugfs_xi2c_reg);
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	return xgbe_common_read(buffer, count, ppos, value);
40662306a36Sopenharmony_ci}
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_cistatic ssize_t xi2c_reg_value_write(struct file *filp,
40962306a36Sopenharmony_ci				    const char __user *buffer,
41062306a36Sopenharmony_ci				    size_t count, loff_t *ppos)
41162306a36Sopenharmony_ci{
41262306a36Sopenharmony_ci	struct xgbe_prv_data *pdata = filp->private_data;
41362306a36Sopenharmony_ci	unsigned int value;
41462306a36Sopenharmony_ci	ssize_t len;
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	len = xgbe_common_write(buffer, count, ppos, &value);
41762306a36Sopenharmony_ci	if (len < 0)
41862306a36Sopenharmony_ci		return len;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	XI2C_IOWRITE(pdata, pdata->debugfs_xi2c_reg, value);
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci	return len;
42362306a36Sopenharmony_ci}
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_cistatic const struct file_operations xi2c_reg_addr_fops = {
42662306a36Sopenharmony_ci	.owner = THIS_MODULE,
42762306a36Sopenharmony_ci	.open = simple_open,
42862306a36Sopenharmony_ci	.read =  xi2c_reg_addr_read,
42962306a36Sopenharmony_ci	.write = xi2c_reg_addr_write,
43062306a36Sopenharmony_ci};
43162306a36Sopenharmony_ci
43262306a36Sopenharmony_cistatic const struct file_operations xi2c_reg_value_fops = {
43362306a36Sopenharmony_ci	.owner = THIS_MODULE,
43462306a36Sopenharmony_ci	.open = simple_open,
43562306a36Sopenharmony_ci	.read =  xi2c_reg_value_read,
43662306a36Sopenharmony_ci	.write = xi2c_reg_value_write,
43762306a36Sopenharmony_ci};
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_civoid xgbe_debugfs_init(struct xgbe_prv_data *pdata)
44062306a36Sopenharmony_ci{
44162306a36Sopenharmony_ci	char *buf;
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci	/* Set defaults */
44462306a36Sopenharmony_ci	pdata->debugfs_xgmac_reg = 0;
44562306a36Sopenharmony_ci	pdata->debugfs_xpcs_mmd = 1;
44662306a36Sopenharmony_ci	pdata->debugfs_xpcs_reg = 0;
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ci	buf = kasprintf(GFP_KERNEL, "amd-xgbe-%s", pdata->netdev->name);
44962306a36Sopenharmony_ci	if (!buf)
45062306a36Sopenharmony_ci		return;
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci	pdata->xgbe_debugfs = debugfs_create_dir(buf, NULL);
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci	debugfs_create_file("xgmac_register", 0600, pdata->xgbe_debugfs, pdata,
45562306a36Sopenharmony_ci			    &xgmac_reg_addr_fops);
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	debugfs_create_file("xgmac_register_value", 0600, pdata->xgbe_debugfs,
45862306a36Sopenharmony_ci			    pdata, &xgmac_reg_value_fops);
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	debugfs_create_file("xpcs_mmd", 0600, pdata->xgbe_debugfs, pdata,
46162306a36Sopenharmony_ci			    &xpcs_mmd_fops);
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ci	debugfs_create_file("xpcs_register", 0600, pdata->xgbe_debugfs, pdata,
46462306a36Sopenharmony_ci			    &xpcs_reg_addr_fops);
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	debugfs_create_file("xpcs_register_value", 0600, pdata->xgbe_debugfs,
46762306a36Sopenharmony_ci			    pdata, &xpcs_reg_value_fops);
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	if (pdata->xprop_regs) {
47062306a36Sopenharmony_ci		debugfs_create_file("xprop_register", 0600, pdata->xgbe_debugfs,
47162306a36Sopenharmony_ci				    pdata, &xprop_reg_addr_fops);
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci		debugfs_create_file("xprop_register_value", 0600,
47462306a36Sopenharmony_ci				    pdata->xgbe_debugfs, pdata,
47562306a36Sopenharmony_ci				    &xprop_reg_value_fops);
47662306a36Sopenharmony_ci	}
47762306a36Sopenharmony_ci
47862306a36Sopenharmony_ci	if (pdata->xi2c_regs) {
47962306a36Sopenharmony_ci		debugfs_create_file("xi2c_register", 0600, pdata->xgbe_debugfs,
48062306a36Sopenharmony_ci				    pdata, &xi2c_reg_addr_fops);
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci		debugfs_create_file("xi2c_register_value", 0600,
48362306a36Sopenharmony_ci				    pdata->xgbe_debugfs, pdata,
48462306a36Sopenharmony_ci				    &xi2c_reg_value_fops);
48562306a36Sopenharmony_ci	}
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci	if (pdata->vdata->an_cdr_workaround) {
48862306a36Sopenharmony_ci		debugfs_create_bool("an_cdr_workaround", 0600,
48962306a36Sopenharmony_ci				    pdata->xgbe_debugfs,
49062306a36Sopenharmony_ci				    &pdata->debugfs_an_cdr_workaround);
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci		debugfs_create_bool("an_cdr_track_early", 0600,
49362306a36Sopenharmony_ci				    pdata->xgbe_debugfs,
49462306a36Sopenharmony_ci				    &pdata->debugfs_an_cdr_track_early);
49562306a36Sopenharmony_ci	}
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci	kfree(buf);
49862306a36Sopenharmony_ci}
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_civoid xgbe_debugfs_exit(struct xgbe_prv_data *pdata)
50162306a36Sopenharmony_ci{
50262306a36Sopenharmony_ci	debugfs_remove_recursive(pdata->xgbe_debugfs);
50362306a36Sopenharmony_ci	pdata->xgbe_debugfs = NULL;
50462306a36Sopenharmony_ci}
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_civoid xgbe_debugfs_rename(struct xgbe_prv_data *pdata)
50762306a36Sopenharmony_ci{
50862306a36Sopenharmony_ci	char *buf;
50962306a36Sopenharmony_ci
51062306a36Sopenharmony_ci	if (!pdata->xgbe_debugfs)
51162306a36Sopenharmony_ci		return;
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_ci	buf = kasprintf(GFP_KERNEL, "amd-xgbe-%s", pdata->netdev->name);
51462306a36Sopenharmony_ci	if (!buf)
51562306a36Sopenharmony_ci		return;
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci	if (!strcmp(pdata->xgbe_debugfs->d_name.name, buf))
51862306a36Sopenharmony_ci		goto out;
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	debugfs_rename(pdata->xgbe_debugfs->d_parent, pdata->xgbe_debugfs,
52162306a36Sopenharmony_ci		       pdata->xgbe_debugfs->d_parent, buf);
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ciout:
52462306a36Sopenharmony_ci	kfree(buf);
52562306a36Sopenharmony_ci}
526