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/kernel.h>
20 #include <linux/kconfig.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 u8 __iomem
*regs
= dev
->priv
;
36 *val
= readb(regs
+ (page
<< 8) + reg
);
41 static int b53_mmap_read16(struct b53_device
*dev
, u8 page
, u8 reg
, u16
*val
)
43 u8 __iomem
*regs
= dev
->priv
;
48 if (dev
->pdata
&& dev
->pdata
->big_endian
)
49 *val
= ioread16be(regs
+ (page
<< 8) + reg
);
51 *val
= readw(regs
+ (page
<< 8) + reg
);
56 static int b53_mmap_read32(struct b53_device
*dev
, u8 page
, u8 reg
, u32
*val
)
58 u8 __iomem
*regs
= dev
->priv
;
63 if (dev
->pdata
&& dev
->pdata
->big_endian
)
64 *val
= ioread32be(regs
+ (page
<< 8) + reg
);
66 *val
= readl(regs
+ (page
<< 8) + reg
);
71 static int b53_mmap_read48(struct b53_device
*dev
, u8 page
, u8 reg
, u64
*val
)
73 u8 __iomem
*regs
= dev
->priv
;
82 if (dev
->pdata
&& dev
->pdata
->big_endian
) {
83 lo
= ioread16be(regs
+ (page
<< 8) + reg
);
84 hi
= ioread32be(regs
+ (page
<< 8) + reg
+ 2);
86 lo
= readw(regs
+ (page
<< 8) + reg
);
87 hi
= readl(regs
+ (page
<< 8) + reg
+ 2);
90 *val
= ((u64
)hi
<< 16) | lo
;
95 if (dev
->pdata
&& dev
->pdata
->big_endian
) {
96 lo
= ioread32be(regs
+ (page
<< 8) + reg
);
97 hi
= ioread16be(regs
+ (page
<< 8) + reg
+ 4);
99 lo
= readl(regs
+ (page
<< 8) + reg
);
100 hi
= readw(regs
+ (page
<< 8) + reg
+ 4);
103 *val
= ((u64
)hi
<< 32) | lo
;
109 static int b53_mmap_read64(struct b53_device
*dev
, u8 page
, u8 reg
, u64
*val
)
111 u8 __iomem
*regs
= dev
->priv
;
114 if (WARN_ON(reg
% 4))
117 if (dev
->pdata
&& dev
->pdata
->big_endian
) {
118 lo
= ioread32be(regs
+ (page
<< 8) + reg
);
119 hi
= ioread32be(regs
+ (page
<< 8) + reg
+ 4);
121 lo
= readl(regs
+ (page
<< 8) + reg
);
122 hi
= readl(regs
+ (page
<< 8) + reg
+ 4);
125 *val
= ((u64
)hi
<< 32) | lo
;
130 static int b53_mmap_write8(struct b53_device
*dev
, u8 page
, u8 reg
, u8 value
)
132 u8 __iomem
*regs
= dev
->priv
;
134 writeb(value
, regs
+ (page
<< 8) + reg
);
139 static int b53_mmap_write16(struct b53_device
*dev
, u8 page
, u8 reg
,
142 u8 __iomem
*regs
= dev
->priv
;
144 if (WARN_ON(reg
% 2))
147 if (dev
->pdata
&& dev
->pdata
->big_endian
)
148 iowrite16be(value
, regs
+ (page
<< 8) + reg
);
150 writew(value
, regs
+ (page
<< 8) + reg
);
155 static int b53_mmap_write32(struct b53_device
*dev
, u8 page
, u8 reg
,
158 u8 __iomem
*regs
= dev
->priv
;
160 if (WARN_ON(reg
% 4))
163 if (dev
->pdata
&& dev
->pdata
->big_endian
)
164 iowrite32be(value
, regs
+ (page
<< 8) + reg
);
166 writel(value
, regs
+ (page
<< 8) + reg
);
171 static int b53_mmap_write48(struct b53_device
*dev
, u8 page
, u8 reg
,
174 if (WARN_ON(reg
% 2))
178 u32 hi
= (u32
)(value
>> 16);
181 b53_mmap_write16(dev
, page
, reg
, lo
);
182 b53_mmap_write32(dev
, page
, reg
+ 2, hi
);
184 u16 hi
= (u16
)(value
>> 32);
187 b53_mmap_write32(dev
, page
, reg
, lo
);
188 b53_mmap_write16(dev
, page
, reg
+ 4, hi
);
194 static int b53_mmap_write64(struct b53_device
*dev
, u8 page
, u8 reg
,
199 hi
= upper_32_bits(value
);
200 lo
= lower_32_bits(value
);
202 if (WARN_ON(reg
% 4))
205 b53_mmap_write32(dev
, page
, reg
, lo
);
206 b53_mmap_write32(dev
, page
, reg
+ 4, hi
);
211 static struct b53_io_ops b53_mmap_ops
= {
212 .read8
= b53_mmap_read8
,
213 .read16
= b53_mmap_read16
,
214 .read32
= b53_mmap_read32
,
215 .read48
= b53_mmap_read48
,
216 .read64
= b53_mmap_read64
,
217 .write8
= b53_mmap_write8
,
218 .write16
= b53_mmap_write16
,
219 .write32
= b53_mmap_write32
,
220 .write48
= b53_mmap_write48
,
221 .write64
= b53_mmap_write64
,
224 static int b53_mmap_probe(struct platform_device
*pdev
)
226 struct b53_platform_data
*pdata
= pdev
->dev
.platform_data
;
227 struct b53_device
*dev
;
232 dev
= b53_switch_alloc(&pdev
->dev
, &b53_mmap_ops
, pdata
->regs
);
238 platform_set_drvdata(pdev
, dev
);
240 return b53_switch_register(dev
);
243 static int b53_mmap_remove(struct platform_device
*pdev
)
245 struct b53_device
*dev
= platform_get_drvdata(pdev
);
248 b53_switch_remove(dev
);
253 static const struct of_device_id b53_mmap_of_table
[] = {
254 { .compatible
= "brcm,bcm3384-switch" },
255 { .compatible
= "brcm,bcm6328-switch" },
256 { .compatible
= "brcm,bcm6368-switch" },
257 { .compatible
= "brcm,bcm63xx-switch" },
261 static struct platform_driver b53_mmap_driver
= {
262 .probe
= b53_mmap_probe
,
263 .remove
= b53_mmap_remove
,
265 .name
= "b53-switch",
266 .of_match_table
= b53_mmap_of_table
,
270 module_platform_driver(b53_mmap_driver
);
271 MODULE_AUTHOR("Jonas Gorski <jogo@openwrt.org>");
272 MODULE_DESCRIPTION("B53 MMAP access driver");
273 MODULE_LICENSE("Dual BSD/GPL");