1631e83bfSPawel Dembicki // SPDX-License-Identifier: GPL-2.0
2631e83bfSPawel Dembicki /* DSA driver for:
3631e83bfSPawel Dembicki * Vitesse VSC7385 SparX-G5 5+1-port Integrated Gigabit Ethernet Switch
4631e83bfSPawel Dembicki * Vitesse VSC7388 SparX-G8 8-port Integrated Gigabit Ethernet Switch
5631e83bfSPawel Dembicki * Vitesse VSC7395 SparX-G5e 5+1-port Integrated Gigabit Ethernet Switch
6631e83bfSPawel Dembicki * Vitesse VSC7398 SparX-G8e 8-port Integrated Gigabit Ethernet Switch
7631e83bfSPawel Dembicki *
8631e83bfSPawel Dembicki * This driver takes control of the switch chip connected over CPU-attached
9631e83bfSPawel Dembicki * address bus and configures it to route packages around when connected to
10631e83bfSPawel Dembicki * a CPU port.
11631e83bfSPawel Dembicki *
12631e83bfSPawel Dembicki * Copyright (C) 2019 Pawel Dembicki <paweldembicki@gmail.com>
13631e83bfSPawel Dembicki * Based on vitesse-vsc-spi.c by:
14631e83bfSPawel Dembicki * Copyright (C) 2018 Linus Wallej <linus.walleij@linaro.org>
15631e83bfSPawel Dembicki * Includes portions of code from the firmware uploader by:
16631e83bfSPawel Dembicki * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
17631e83bfSPawel Dembicki */
18631e83bfSPawel Dembicki #include <linux/kernel.h>
19631e83bfSPawel Dembicki #include <linux/module.h>
20631e83bfSPawel Dembicki #include <linux/of.h>
21631e83bfSPawel Dembicki #include <linux/platform_device.h>
22631e83bfSPawel Dembicki
23631e83bfSPawel Dembicki #include "vitesse-vsc73xx.h"
24631e83bfSPawel Dembicki
25631e83bfSPawel Dembicki #define VSC73XX_CMD_PLATFORM_BLOCK_SHIFT 14
26631e83bfSPawel Dembicki #define VSC73XX_CMD_PLATFORM_BLOCK_MASK 0x7
27631e83bfSPawel Dembicki #define VSC73XX_CMD_PLATFORM_SUBBLOCK_SHIFT 10
28631e83bfSPawel Dembicki #define VSC73XX_CMD_PLATFORM_SUBBLOCK_MASK 0xf
29631e83bfSPawel Dembicki #define VSC73XX_CMD_PLATFORM_REGISTER_SHIFT 2
30631e83bfSPawel Dembicki
315bd6ff0cSAndrew Lunn /*
32631e83bfSPawel Dembicki * struct vsc73xx_platform - VSC73xx Platform state container
33631e83bfSPawel Dembicki */
34631e83bfSPawel Dembicki struct vsc73xx_platform {
35631e83bfSPawel Dembicki struct platform_device *pdev;
36631e83bfSPawel Dembicki void __iomem *base_addr;
37631e83bfSPawel Dembicki struct vsc73xx vsc;
38631e83bfSPawel Dembicki };
39631e83bfSPawel Dembicki
40631e83bfSPawel Dembicki static const struct vsc73xx_ops vsc73xx_platform_ops;
41631e83bfSPawel Dembicki
vsc73xx_make_addr(u8 block,u8 subblock,u8 reg)42631e83bfSPawel Dembicki static u32 vsc73xx_make_addr(u8 block, u8 subblock, u8 reg)
43631e83bfSPawel Dembicki {
44631e83bfSPawel Dembicki u32 ret;
45631e83bfSPawel Dembicki
46631e83bfSPawel Dembicki ret = (block & VSC73XX_CMD_PLATFORM_BLOCK_MASK)
47631e83bfSPawel Dembicki << VSC73XX_CMD_PLATFORM_BLOCK_SHIFT;
48631e83bfSPawel Dembicki ret |= (subblock & VSC73XX_CMD_PLATFORM_SUBBLOCK_MASK)
49631e83bfSPawel Dembicki << VSC73XX_CMD_PLATFORM_SUBBLOCK_SHIFT;
50631e83bfSPawel Dembicki ret |= reg << VSC73XX_CMD_PLATFORM_REGISTER_SHIFT;
51631e83bfSPawel Dembicki
52631e83bfSPawel Dembicki return ret;
53631e83bfSPawel Dembicki }
54631e83bfSPawel Dembicki
vsc73xx_platform_read(struct vsc73xx * vsc,u8 block,u8 subblock,u8 reg,u32 * val)55631e83bfSPawel Dembicki static int vsc73xx_platform_read(struct vsc73xx *vsc, u8 block, u8 subblock,
56631e83bfSPawel Dembicki u8 reg, u32 *val)
57631e83bfSPawel Dembicki {
58631e83bfSPawel Dembicki struct vsc73xx_platform *vsc_platform = vsc->priv;
59631e83bfSPawel Dembicki u32 offset;
60631e83bfSPawel Dembicki
61631e83bfSPawel Dembicki if (!vsc73xx_is_addr_valid(block, subblock))
62631e83bfSPawel Dembicki return -EINVAL;
63631e83bfSPawel Dembicki
64631e83bfSPawel Dembicki offset = vsc73xx_make_addr(block, subblock, reg);
65631e83bfSPawel Dembicki /* By default vsc73xx running in big-endian mode.
66631e83bfSPawel Dembicki * (See "Register Addressing" section 5.5.3 in the VSC7385 manual.)
67631e83bfSPawel Dembicki */
68631e83bfSPawel Dembicki *val = ioread32be(vsc_platform->base_addr + offset);
69631e83bfSPawel Dembicki
70631e83bfSPawel Dembicki return 0;
71631e83bfSPawel Dembicki }
72631e83bfSPawel Dembicki
vsc73xx_platform_write(struct vsc73xx * vsc,u8 block,u8 subblock,u8 reg,u32 val)73631e83bfSPawel Dembicki static int vsc73xx_platform_write(struct vsc73xx *vsc, u8 block, u8 subblock,
74631e83bfSPawel Dembicki u8 reg, u32 val)
75631e83bfSPawel Dembicki {
76631e83bfSPawel Dembicki struct vsc73xx_platform *vsc_platform = vsc->priv;
77631e83bfSPawel Dembicki u32 offset;
78631e83bfSPawel Dembicki
79631e83bfSPawel Dembicki if (!vsc73xx_is_addr_valid(block, subblock))
80631e83bfSPawel Dembicki return -EINVAL;
81631e83bfSPawel Dembicki
82631e83bfSPawel Dembicki offset = vsc73xx_make_addr(block, subblock, reg);
83631e83bfSPawel Dembicki iowrite32be(val, vsc_platform->base_addr + offset);
84631e83bfSPawel Dembicki
85631e83bfSPawel Dembicki return 0;
86631e83bfSPawel Dembicki }
87631e83bfSPawel Dembicki
vsc73xx_platform_probe(struct platform_device * pdev)88631e83bfSPawel Dembicki static int vsc73xx_platform_probe(struct platform_device *pdev)
89631e83bfSPawel Dembicki {
90631e83bfSPawel Dembicki struct device *dev = &pdev->dev;
91631e83bfSPawel Dembicki struct vsc73xx_platform *vsc_platform;
92631e83bfSPawel Dembicki int ret;
93631e83bfSPawel Dembicki
94631e83bfSPawel Dembicki vsc_platform = devm_kzalloc(dev, sizeof(*vsc_platform), GFP_KERNEL);
95631e83bfSPawel Dembicki if (!vsc_platform)
96631e83bfSPawel Dembicki return -ENOMEM;
97631e83bfSPawel Dembicki
98631e83bfSPawel Dembicki platform_set_drvdata(pdev, vsc_platform);
99631e83bfSPawel Dembicki vsc_platform->pdev = pdev;
100631e83bfSPawel Dembicki vsc_platform->vsc.dev = dev;
101631e83bfSPawel Dembicki vsc_platform->vsc.priv = vsc_platform;
102631e83bfSPawel Dembicki vsc_platform->vsc.ops = &vsc73xx_platform_ops;
103631e83bfSPawel Dembicki
104631e83bfSPawel Dembicki /* obtain I/O memory space */
105da384effSWei Yongjun vsc_platform->base_addr = devm_platform_ioremap_resource(pdev, 0);
106631e83bfSPawel Dembicki if (IS_ERR(vsc_platform->base_addr)) {
107631e83bfSPawel Dembicki dev_err(&pdev->dev, "cannot request I/O memory space\n");
108631e83bfSPawel Dembicki ret = -ENXIO;
109631e83bfSPawel Dembicki return ret;
110631e83bfSPawel Dembicki }
111631e83bfSPawel Dembicki
112631e83bfSPawel Dembicki return vsc73xx_probe(&vsc_platform->vsc);
113631e83bfSPawel Dembicki }
114631e83bfSPawel Dembicki
vsc73xx_platform_remove(struct platform_device * pdev)115*ce322d45SUwe Kleine-König static void vsc73xx_platform_remove(struct platform_device *pdev)
116631e83bfSPawel Dembicki {
117631e83bfSPawel Dembicki struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev);
118631e83bfSPawel Dembicki
1190650bf52SVladimir Oltean if (!vsc_platform)
120*ce322d45SUwe Kleine-König return;
1210650bf52SVladimir Oltean
1220650bf52SVladimir Oltean vsc73xx_remove(&vsc_platform->vsc);
1230650bf52SVladimir Oltean }
1240650bf52SVladimir Oltean
vsc73xx_platform_shutdown(struct platform_device * pdev)1250650bf52SVladimir Oltean static void vsc73xx_platform_shutdown(struct platform_device *pdev)
1260650bf52SVladimir Oltean {
1270650bf52SVladimir Oltean struct vsc73xx_platform *vsc_platform = platform_get_drvdata(pdev);
1280650bf52SVladimir Oltean
1290650bf52SVladimir Oltean if (!vsc_platform)
1300650bf52SVladimir Oltean return;
1310650bf52SVladimir Oltean
1320650bf52SVladimir Oltean vsc73xx_shutdown(&vsc_platform->vsc);
1330650bf52SVladimir Oltean
1340650bf52SVladimir Oltean platform_set_drvdata(pdev, NULL);
135631e83bfSPawel Dembicki }
136631e83bfSPawel Dembicki
137631e83bfSPawel Dembicki static const struct vsc73xx_ops vsc73xx_platform_ops = {
138631e83bfSPawel Dembicki .read = vsc73xx_platform_read,
139631e83bfSPawel Dembicki .write = vsc73xx_platform_write,
140631e83bfSPawel Dembicki };
141631e83bfSPawel Dembicki
142631e83bfSPawel Dembicki static const struct of_device_id vsc73xx_of_match[] = {
143631e83bfSPawel Dembicki {
144631e83bfSPawel Dembicki .compatible = "vitesse,vsc7385",
145631e83bfSPawel Dembicki },
146631e83bfSPawel Dembicki {
147631e83bfSPawel Dembicki .compatible = "vitesse,vsc7388",
148631e83bfSPawel Dembicki },
149631e83bfSPawel Dembicki {
150631e83bfSPawel Dembicki .compatible = "vitesse,vsc7395",
151631e83bfSPawel Dembicki },
152631e83bfSPawel Dembicki {
153631e83bfSPawel Dembicki .compatible = "vitesse,vsc7398",
154631e83bfSPawel Dembicki },
155631e83bfSPawel Dembicki { },
156631e83bfSPawel Dembicki };
157631e83bfSPawel Dembicki MODULE_DEVICE_TABLE(of, vsc73xx_of_match);
158631e83bfSPawel Dembicki
159631e83bfSPawel Dembicki static struct platform_driver vsc73xx_platform_driver = {
160631e83bfSPawel Dembicki .probe = vsc73xx_platform_probe,
161*ce322d45SUwe Kleine-König .remove_new = vsc73xx_platform_remove,
1620650bf52SVladimir Oltean .shutdown = vsc73xx_platform_shutdown,
163631e83bfSPawel Dembicki .driver = {
164631e83bfSPawel Dembicki .name = "vsc73xx-platform",
165631e83bfSPawel Dembicki .of_match_table = vsc73xx_of_match,
166631e83bfSPawel Dembicki },
167631e83bfSPawel Dembicki };
168631e83bfSPawel Dembicki module_platform_driver(vsc73xx_platform_driver);
169631e83bfSPawel Dembicki
170631e83bfSPawel Dembicki MODULE_AUTHOR("Pawel Dembicki <paweldembicki@gmail.com>");
171631e83bfSPawel Dembicki MODULE_DESCRIPTION("Vitesse VSC7385/7388/7395/7398 Platform driver");
172631e83bfSPawel Dembicki MODULE_LICENSE("GPL v2");
173