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/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/platform_data/b53.h>
27 struct b53_mmap_priv
{
31 static int b53_mmap_read8(struct b53_device
*dev
, u8 page
, u8 reg
, u8
*val
)
33 u8 __iomem
*regs
= dev
->priv
;
35 *val
= readb(regs
+ (page
<< 8) + reg
);
40 static int b53_mmap_read16(struct b53_device
*dev
, u8 page
, u8 reg
, u16
*val
)
42 u8 __iomem
*regs
= dev
->priv
;
47 if (dev
->pdata
&& dev
->pdata
->big_endian
)
48 *val
= ioread16be(regs
+ (page
<< 8) + reg
);
50 *val
= readw(regs
+ (page
<< 8) + reg
);
55 static int b53_mmap_read32(struct b53_device
*dev
, u8 page
, u8 reg
, u32
*val
)
57 u8 __iomem
*regs
= dev
->priv
;
62 if (dev
->pdata
&& dev
->pdata
->big_endian
)
63 *val
= ioread32be(regs
+ (page
<< 8) + reg
);
65 *val
= readl(regs
+ (page
<< 8) + reg
);
70 static int b53_mmap_read48(struct b53_device
*dev
, u8 page
, u8 reg
, u64
*val
)
72 u8 __iomem
*regs
= dev
->priv
;
81 if (dev
->pdata
&& dev
->pdata
->big_endian
) {
82 lo
= ioread16be(regs
+ (page
<< 8) + reg
);
83 hi
= ioread32be(regs
+ (page
<< 8) + reg
+ 2);
85 lo
= readw(regs
+ (page
<< 8) + reg
);
86 hi
= readl(regs
+ (page
<< 8) + reg
+ 2);
89 *val
= ((u64
)hi
<< 16) | lo
;
94 if (dev
->pdata
&& dev
->pdata
->big_endian
) {
95 lo
= ioread32be(regs
+ (page
<< 8) + reg
);
96 hi
= ioread16be(regs
+ (page
<< 8) + reg
+ 4);
98 lo
= readl(regs
+ (page
<< 8) + reg
);
99 hi
= readw(regs
+ (page
<< 8) + reg
+ 4);
102 *val
= ((u64
)hi
<< 32) | lo
;
108 static int b53_mmap_read64(struct b53_device
*dev
, u8 page
, u8 reg
, u64
*val
)
110 u8 __iomem
*regs
= dev
->priv
;
113 if (WARN_ON(reg
% 4))
116 if (dev
->pdata
&& dev
->pdata
->big_endian
) {
117 lo
= ioread32be(regs
+ (page
<< 8) + reg
);
118 hi
= ioread32be(regs
+ (page
<< 8) + reg
+ 4);
120 lo
= readl(regs
+ (page
<< 8) + reg
);
121 hi
= readl(regs
+ (page
<< 8) + reg
+ 4);
124 *val
= ((u64
)hi
<< 32) | lo
;
129 static int b53_mmap_write8(struct b53_device
*dev
, u8 page
, u8 reg
, u8 value
)
131 u8 __iomem
*regs
= dev
->priv
;
133 writeb(value
, regs
+ (page
<< 8) + reg
);
138 static int b53_mmap_write16(struct b53_device
*dev
, u8 page
, u8 reg
,
141 u8 __iomem
*regs
= dev
->priv
;
143 if (WARN_ON(reg
% 2))
146 if (dev
->pdata
&& dev
->pdata
->big_endian
)
147 iowrite16be(value
, regs
+ (page
<< 8) + reg
);
149 writew(value
, regs
+ (page
<< 8) + reg
);
154 static int b53_mmap_write32(struct b53_device
*dev
, u8 page
, u8 reg
,
157 u8 __iomem
*regs
= dev
->priv
;
159 if (WARN_ON(reg
% 4))
162 if (dev
->pdata
&& dev
->pdata
->big_endian
)
163 iowrite32be(value
, regs
+ (page
<< 8) + reg
);
165 writel(value
, regs
+ (page
<< 8) + reg
);
170 static int b53_mmap_write48(struct b53_device
*dev
, u8 page
, u8 reg
,
173 if (WARN_ON(reg
% 2))
177 u32 hi
= (u32
)(value
>> 16);
180 b53_mmap_write16(dev
, page
, reg
, lo
);
181 b53_mmap_write32(dev
, page
, reg
+ 2, hi
);
183 u16 hi
= (u16
)(value
>> 32);
186 b53_mmap_write32(dev
, page
, reg
, lo
);
187 b53_mmap_write16(dev
, page
, reg
+ 4, hi
);
193 static int b53_mmap_write64(struct b53_device
*dev
, u8 page
, u8 reg
,
198 hi
= upper_32_bits(value
);
199 lo
= lower_32_bits(value
);
201 if (WARN_ON(reg
% 4))
204 b53_mmap_write32(dev
, page
, reg
, lo
);
205 b53_mmap_write32(dev
, page
, reg
+ 4, hi
);
210 static const struct b53_io_ops b53_mmap_ops
= {
211 .read8
= b53_mmap_read8
,
212 .read16
= b53_mmap_read16
,
213 .read32
= b53_mmap_read32
,
214 .read48
= b53_mmap_read48
,
215 .read64
= b53_mmap_read64
,
216 .write8
= b53_mmap_write8
,
217 .write16
= b53_mmap_write16
,
218 .write32
= b53_mmap_write32
,
219 .write48
= b53_mmap_write48
,
220 .write64
= b53_mmap_write64
,
223 static int b53_mmap_probe(struct platform_device
*pdev
)
225 struct b53_platform_data
*pdata
= pdev
->dev
.platform_data
;
226 struct b53_device
*dev
;
231 dev
= b53_switch_alloc(&pdev
->dev
, &b53_mmap_ops
, pdata
->regs
);
237 platform_set_drvdata(pdev
, dev
);
239 return b53_switch_register(dev
);
242 static int b53_mmap_remove(struct platform_device
*pdev
)
244 struct b53_device
*dev
= platform_get_drvdata(pdev
);
247 b53_switch_remove(dev
);
252 static const struct of_device_id b53_mmap_of_table
[] = {
253 { .compatible
= "brcm,bcm3384-switch" },
254 { .compatible
= "brcm,bcm6328-switch" },
255 { .compatible
= "brcm,bcm6368-switch" },
256 { .compatible
= "brcm,bcm63xx-switch" },
259 MODULE_DEVICE_TABLE(of
, b53_mmap_of_table
);
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");