2 * B53 register access through memory mapped registers
4 * Copyright (C) 2012-2013 Jonas Gorski <jogo@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <linux/bits.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/platform_data/b53.h>
28 struct b53_mmap_priv {
32 static int b53_mmap_read8(struct b53_device *dev, u8 page, u8 reg, u8 *val)
34 struct b53_mmap_priv *priv = dev->priv;
35 void __iomem *regs = priv->regs;
37 *val = readb(regs + (page << 8) + reg);
42 static int b53_mmap_read16(struct b53_device *dev, u8 page, u8 reg, u16 *val)
44 struct b53_mmap_priv *priv = dev->priv;
45 void __iomem *regs = priv->regs;
50 if (dev->pdata && dev->pdata->big_endian)
51 *val = ioread16be(regs + (page << 8) + reg);
53 *val = readw(regs + (page << 8) + reg);
58 static int b53_mmap_read32(struct b53_device *dev, u8 page, u8 reg, u32 *val)
60 struct b53_mmap_priv *priv = dev->priv;
61 void __iomem *regs = priv->regs;
66 if (dev->pdata && dev->pdata->big_endian)
67 *val = ioread32be(regs + (page << 8) + reg);
69 *val = readl(regs + (page << 8) + reg);
74 static int b53_mmap_read48(struct b53_device *dev, u8 page, u8 reg, u64 *val)
76 struct b53_mmap_priv *priv = dev->priv;
77 void __iomem *regs = priv->regs;
86 if (dev->pdata && dev->pdata->big_endian) {
87 lo = ioread16be(regs + (page << 8) + reg);
88 hi = ioread32be(regs + (page << 8) + reg + 2);
90 lo = readw(regs + (page << 8) + reg);
91 hi = readl(regs + (page << 8) + reg + 2);
94 *val = ((u64)hi << 16) | lo;
99 if (dev->pdata && dev->pdata->big_endian) {
100 lo = ioread32be(regs + (page << 8) + reg);
101 hi = ioread16be(regs + (page << 8) + reg + 4);
103 lo = readl(regs + (page << 8) + reg);
104 hi = readw(regs + (page << 8) + reg + 4);
107 *val = ((u64)hi << 32) | lo;
113 static int b53_mmap_read64(struct b53_device *dev, u8 page, u8 reg, u64 *val)
115 struct b53_mmap_priv *priv = dev->priv;
116 void __iomem *regs = priv->regs;
119 if (WARN_ON(reg % 4))
122 if (dev->pdata && dev->pdata->big_endian) {
123 lo = ioread32be(regs + (page << 8) + reg);
124 hi = ioread32be(regs + (page << 8) + reg + 4);
126 lo = readl(regs + (page << 8) + reg);
127 hi = readl(regs + (page << 8) + reg + 4);
130 *val = ((u64)hi << 32) | lo;
135 static int b53_mmap_write8(struct b53_device *dev, u8 page, u8 reg, u8 value)
137 struct b53_mmap_priv *priv = dev->priv;
138 void __iomem *regs = priv->regs;
140 writeb(value, regs + (page << 8) + reg);
145 static int b53_mmap_write16(struct b53_device *dev, u8 page, u8 reg,
148 struct b53_mmap_priv *priv = dev->priv;
149 void __iomem *regs = priv->regs;
151 if (WARN_ON(reg % 2))
154 if (dev->pdata && dev->pdata->big_endian)
155 iowrite16be(value, regs + (page << 8) + reg);
157 writew(value, regs + (page << 8) + reg);
162 static int b53_mmap_write32(struct b53_device *dev, u8 page, u8 reg,
165 struct b53_mmap_priv *priv = dev->priv;
166 void __iomem *regs = priv->regs;
168 if (WARN_ON(reg % 4))
171 if (dev->pdata && dev->pdata->big_endian)
172 iowrite32be(value, regs + (page << 8) + reg);
174 writel(value, regs + (page << 8) + reg);
179 static int b53_mmap_write48(struct b53_device *dev, u8 page, u8 reg,
182 if (WARN_ON(reg % 2))
186 u32 hi = (u32)(value >> 16);
189 b53_mmap_write16(dev, page, reg, lo);
190 b53_mmap_write32(dev, page, reg + 2, hi);
192 u16 hi = (u16)(value >> 32);
195 b53_mmap_write32(dev, page, reg, lo);
196 b53_mmap_write16(dev, page, reg + 4, hi);
202 static int b53_mmap_write64(struct b53_device *dev, u8 page, u8 reg,
207 hi = upper_32_bits(value);
208 lo = lower_32_bits(value);
210 if (WARN_ON(reg % 4))
213 b53_mmap_write32(dev, page, reg, lo);
214 b53_mmap_write32(dev, page, reg + 4, hi);
219 static const struct b53_io_ops b53_mmap_ops = {
220 .read8 = b53_mmap_read8,
221 .read16 = b53_mmap_read16,
222 .read32 = b53_mmap_read32,
223 .read48 = b53_mmap_read48,
224 .read64 = b53_mmap_read64,
225 .write8 = b53_mmap_write8,
226 .write16 = b53_mmap_write16,
227 .write32 = b53_mmap_write32,
228 .write48 = b53_mmap_write48,
229 .write64 = b53_mmap_write64,
232 static int b53_mmap_probe_of(struct platform_device *pdev,
233 struct b53_platform_data **ppdata)
235 struct device_node *np = pdev->dev.of_node;
236 struct device_node *of_ports, *of_port;
237 struct device *dev = &pdev->dev;
238 struct b53_platform_data *pdata;
241 mem = devm_platform_ioremap_resource(pdev, 0);
245 pdata = devm_kzalloc(dev, sizeof(struct b53_platform_data),
251 pdata->chip_id = BCM63XX_DEVICE_ID;
252 pdata->big_endian = of_property_read_bool(np, "big-endian");
254 of_ports = of_get_child_by_name(np, "ports");
256 dev_err(dev, "no ports child node found\n");
260 for_each_available_child_of_node(of_ports, of_port) {
263 if (of_property_read_u32(of_port, "reg", ®))
266 if (reg < B53_CPU_PORT)
267 pdata->enabled_ports |= BIT(reg);
270 of_node_put(of_ports);
276 static int b53_mmap_probe(struct platform_device *pdev)
278 struct device_node *np = pdev->dev.of_node;
279 struct b53_platform_data *pdata = pdev->dev.platform_data;
280 struct b53_mmap_priv *priv;
281 struct b53_device *dev;
285 ret = b53_mmap_probe_of(pdev, &pdata);
287 dev_err(&pdev->dev, "OF probe error\n");
295 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
299 priv->regs = pdata->regs;
301 dev = b53_switch_alloc(&pdev->dev, &b53_mmap_ops, priv);
307 platform_set_drvdata(pdev, dev);
309 return b53_switch_register(dev);
312 static int b53_mmap_remove(struct platform_device *pdev)
314 struct b53_device *dev = platform_get_drvdata(pdev);
317 b53_switch_remove(dev);
319 platform_set_drvdata(pdev, NULL);
324 static void b53_mmap_shutdown(struct platform_device *pdev)
326 struct b53_device *dev = platform_get_drvdata(pdev);
329 b53_switch_shutdown(dev);
331 platform_set_drvdata(pdev, NULL);
334 static const struct of_device_id b53_mmap_of_table[] = {
335 { .compatible = "brcm,bcm3384-switch" },
336 { .compatible = "brcm,bcm6328-switch" },
337 { .compatible = "brcm,bcm6368-switch" },
338 { .compatible = "brcm,bcm63xx-switch" },
341 MODULE_DEVICE_TABLE(of, b53_mmap_of_table);
343 static struct platform_driver b53_mmap_driver = {
344 .probe = b53_mmap_probe,
345 .remove = b53_mmap_remove,
346 .shutdown = b53_mmap_shutdown,
348 .name = "b53-switch",
349 .of_match_table = b53_mmap_of_table,
353 module_platform_driver(b53_mmap_driver);
354 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
355 MODULE_DESCRIPTION("B53 MMAP access driver");
356 MODULE_LICENSE("Dual BSD/GPL");