Linux 4.19.133
[linux/fpc-iii.git] / drivers / bcma / host_soc.c
blob2dce34789329f13bf46fe3e33000aff149127205
1 /*
2 * Broadcom specific AMBA
3 * System on Chip (SoC) Host
5 * Licensed under the GNU/GPL. See COPYING for details.
6 */
8 #include "bcma_private.h"
9 #include "scan.h"
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/of_address.h>
13 #include <linux/bcma/bcma.h>
14 #include <linux/bcma/bcma_soc.h>
16 static u8 bcma_host_soc_read8(struct bcma_device *core, u16 offset)
18 return readb(core->io_addr + offset);
21 static u16 bcma_host_soc_read16(struct bcma_device *core, u16 offset)
23 return readw(core->io_addr + offset);
26 static u32 bcma_host_soc_read32(struct bcma_device *core, u16 offset)
28 return readl(core->io_addr + offset);
31 static void bcma_host_soc_write8(struct bcma_device *core, u16 offset,
32 u8 value)
34 writeb(value, core->io_addr + offset);
37 static void bcma_host_soc_write16(struct bcma_device *core, u16 offset,
38 u16 value)
40 writew(value, core->io_addr + offset);
43 static void bcma_host_soc_write32(struct bcma_device *core, u16 offset,
44 u32 value)
46 writel(value, core->io_addr + offset);
49 #ifdef CONFIG_BCMA_BLOCKIO
50 static void bcma_host_soc_block_read(struct bcma_device *core, void *buffer,
51 size_t count, u16 offset, u8 reg_width)
53 void __iomem *addr = core->io_addr + offset;
55 switch (reg_width) {
56 case sizeof(u8): {
57 u8 *buf = buffer;
59 while (count) {
60 *buf = __raw_readb(addr);
61 buf++;
62 count--;
64 break;
66 case sizeof(u16): {
67 __le16 *buf = buffer;
69 WARN_ON(count & 1);
70 while (count) {
71 *buf = (__force __le16)__raw_readw(addr);
72 buf++;
73 count -= 2;
75 break;
77 case sizeof(u32): {
78 __le32 *buf = buffer;
80 WARN_ON(count & 3);
81 while (count) {
82 *buf = (__force __le32)__raw_readl(addr);
83 buf++;
84 count -= 4;
86 break;
88 default:
89 WARN_ON(1);
93 static void bcma_host_soc_block_write(struct bcma_device *core,
94 const void *buffer,
95 size_t count, u16 offset, u8 reg_width)
97 void __iomem *addr = core->io_addr + offset;
99 switch (reg_width) {
100 case sizeof(u8): {
101 const u8 *buf = buffer;
103 while (count) {
104 __raw_writeb(*buf, addr);
105 buf++;
106 count--;
108 break;
110 case sizeof(u16): {
111 const __le16 *buf = buffer;
113 WARN_ON(count & 1);
114 while (count) {
115 __raw_writew((__force u16)(*buf), addr);
116 buf++;
117 count -= 2;
119 break;
121 case sizeof(u32): {
122 const __le32 *buf = buffer;
124 WARN_ON(count & 3);
125 while (count) {
126 __raw_writel((__force u32)(*buf), addr);
127 buf++;
128 count -= 4;
130 break;
132 default:
133 WARN_ON(1);
136 #endif /* CONFIG_BCMA_BLOCKIO */
138 static u32 bcma_host_soc_aread32(struct bcma_device *core, u16 offset)
140 if (WARN_ONCE(!core->io_wrap, "Accessed core has no wrapper/agent\n"))
141 return ~0;
142 return readl(core->io_wrap + offset);
145 static void bcma_host_soc_awrite32(struct bcma_device *core, u16 offset,
146 u32 value)
148 if (WARN_ONCE(!core->io_wrap, "Accessed core has no wrapper/agent\n"))
149 return;
150 writel(value, core->io_wrap + offset);
153 static const struct bcma_host_ops bcma_host_soc_ops = {
154 .read8 = bcma_host_soc_read8,
155 .read16 = bcma_host_soc_read16,
156 .read32 = bcma_host_soc_read32,
157 .write8 = bcma_host_soc_write8,
158 .write16 = bcma_host_soc_write16,
159 .write32 = bcma_host_soc_write32,
160 #ifdef CONFIG_BCMA_BLOCKIO
161 .block_read = bcma_host_soc_block_read,
162 .block_write = bcma_host_soc_block_write,
163 #endif
164 .aread32 = bcma_host_soc_aread32,
165 .awrite32 = bcma_host_soc_awrite32,
168 int __init bcma_host_soc_register(struct bcma_soc *soc)
170 struct bcma_bus *bus = &soc->bus;
172 /* iomap only first core. We have to read some register on this core
173 * to scan the bus.
175 bus->mmio = ioremap_nocache(BCMA_ADDR_BASE, BCMA_CORE_SIZE * 1);
176 if (!bus->mmio)
177 return -ENOMEM;
179 /* Host specific */
180 bus->hosttype = BCMA_HOSTTYPE_SOC;
181 bus->ops = &bcma_host_soc_ops;
182 bus->host_pdev = NULL;
184 /* Initialize struct, detect chip */
185 bcma_init_bus(bus);
187 return 0;
190 int __init bcma_host_soc_init(struct bcma_soc *soc)
192 struct bcma_bus *bus = &soc->bus;
193 int err;
195 /* Scan bus and initialize it */
196 err = bcma_bus_early_register(bus);
197 if (err)
198 iounmap(bus->mmio);
200 return err;
203 #ifdef CONFIG_OF
204 static int bcma_host_soc_probe(struct platform_device *pdev)
206 struct device *dev = &pdev->dev;
207 struct device_node *np = dev->of_node;
208 struct bcma_bus *bus;
209 int err;
211 /* Alloc */
212 bus = devm_kzalloc(dev, sizeof(*bus), GFP_KERNEL);
213 if (!bus)
214 return -ENOMEM;
216 /* Map MMIO */
217 bus->mmio = of_iomap(np, 0);
218 if (!bus->mmio)
219 return -ENOMEM;
221 /* Host specific */
222 bus->hosttype = BCMA_HOSTTYPE_SOC;
223 bus->ops = &bcma_host_soc_ops;
224 bus->host_pdev = pdev;
226 /* Initialize struct, detect chip */
227 bcma_init_bus(bus);
229 /* Register */
230 err = bcma_bus_register(bus);
231 if (err)
232 goto err_unmap_mmio;
234 platform_set_drvdata(pdev, bus);
236 return err;
238 err_unmap_mmio:
239 iounmap(bus->mmio);
240 return err;
243 static int bcma_host_soc_remove(struct platform_device *pdev)
245 struct bcma_bus *bus = platform_get_drvdata(pdev);
247 bcma_bus_unregister(bus);
248 iounmap(bus->mmio);
249 platform_set_drvdata(pdev, NULL);
251 return 0;
254 static const struct of_device_id bcma_host_soc_of_match[] = {
255 { .compatible = "brcm,bus-axi", },
258 MODULE_DEVICE_TABLE(of, bcma_host_soc_of_match);
260 static struct platform_driver bcma_host_soc_driver = {
261 .driver = {
262 .name = "bcma-host-soc",
263 .of_match_table = bcma_host_soc_of_match,
265 .probe = bcma_host_soc_probe,
266 .remove = bcma_host_soc_remove,
269 int __init bcma_host_soc_register_driver(void)
271 return platform_driver_register(&bcma_host_soc_driver);
274 void __exit bcma_host_soc_unregister_driver(void)
276 platform_driver_unregister(&bcma_host_soc_driver);
278 #endif /* CONFIG_OF */