035da9d76ade8fc53d9430ced97d8145448ff382
[openwrt.git] / target / linux / brcm47xx / patches-3.0 / 0004-bcma-add-SOC-bus.patch
1 From 22573303ad477fa07c948a9944e9c18fea9af724 Mon Sep 17 00:00:00 2001
2 From: Hauke Mehrtens <hauke@hauke-m.de>
3 Date: Sat, 18 Jun 2011 14:31:53 +0200
4 Subject: [PATCH 04/22] bcma: add SOC bus
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This patch adds support for using bcma on a Broadcom SoC as the system
10 bus. An SoC like the bcm4716 could register this bus and use it to
11 searches for the bcma cores and register the devices on this bus.
12
13 BCMA_HOSTTYPE_NONE was intended for SoCs at first but BCMA_HOSTTYPE_SOC
14 is a better name.
15
16 Acked-by: Rafał Miłecki <zajec5@gmail.com>
17 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
18 ---
19  drivers/bcma/Kconfig          |    4 +
20  drivers/bcma/Makefile         |    1 +
21  drivers/bcma/host_soc.c       |  183 +++++++++++++++++++++++++++++++++++++++++
22  drivers/bcma/main.c           |    6 +-
23  drivers/bcma/scan.c           |   42 ++++++++-
24  include/linux/bcma/bcma.h     |    5 +-
25  include/linux/bcma/bcma_soc.h |   16 ++++
26  7 files changed, 250 insertions(+), 7 deletions(-)
27  create mode 100644 drivers/bcma/host_soc.c
28  create mode 100644 include/linux/bcma/bcma_soc.h
29
30 --- a/drivers/bcma/Kconfig
31 +++ b/drivers/bcma/Kconfig
32 @@ -34,6 +34,10 @@ config BCMA_DRIVER_PCI_HOSTMODE
33         help
34           PCI core hostmode operation (external PCI bus).
35  
36 +config BCMA_HOST_SOC
37 +       bool
38 +       depends on BCMA && MIPS
39 +
40  config BCMA_DEBUG
41         bool "BCMA debugging"
42         depends on BCMA
43 --- a/drivers/bcma/Makefile
44 +++ b/drivers/bcma/Makefile
45 @@ -3,6 +3,7 @@ bcma-y                                  += driver_chipcommon.o driver
46  bcma-y                                 += driver_pci.o
47  bcma-$(CONFIG_BCMA_DRIVER_PCI_HOSTMODE)        += driver_pci_host.o
48  bcma-$(CONFIG_BCMA_HOST_PCI)           += host_pci.o
49 +bcma-$(CONFIG_BCMA_HOST_SOC)           += host_soc.o
50  obj-$(CONFIG_BCMA)                     += bcma.o
51  
52  ccflags-$(CONFIG_BCMA_DEBUG)           := -DDEBUG
53 --- /dev/null
54 +++ b/drivers/bcma/host_soc.c
55 @@ -0,0 +1,183 @@
56 +/*
57 + * Broadcom specific AMBA
58 + * System on Chip (SoC) Host
59 + *
60 + * Licensed under the GNU/GPL. See COPYING for details.
61 + */
62 +
63 +#include "bcma_private.h"
64 +#include "scan.h"
65 +#include <linux/bcma/bcma.h>
66 +#include <linux/bcma/bcma_soc.h>
67 +
68 +static u8 bcma_host_soc_read8(struct bcma_device *core, u16 offset)
69 +{
70 +       return readb(core->io_addr + offset);
71 +}
72 +
73 +static u16 bcma_host_soc_read16(struct bcma_device *core, u16 offset)
74 +{
75 +       return readw(core->io_addr + offset);
76 +}
77 +
78 +static u32 bcma_host_soc_read32(struct bcma_device *core, u16 offset)
79 +{
80 +       return readl(core->io_addr + offset);
81 +}
82 +
83 +static void bcma_host_soc_write8(struct bcma_device *core, u16 offset,
84 +                                u8 value)
85 +{
86 +       writeb(value, core->io_addr + offset);
87 +}
88 +
89 +static void bcma_host_soc_write16(struct bcma_device *core, u16 offset,
90 +                                u16 value)
91 +{
92 +       writew(value, core->io_addr + offset);
93 +}
94 +
95 +static void bcma_host_soc_write32(struct bcma_device *core, u16 offset,
96 +                                u32 value)
97 +{
98 +       writel(value, core->io_addr + offset);
99 +}
100 +
101 +#ifdef CONFIG_BCMA_BLOCKIO
102 +static void bcma_host_soc_block_read(struct bcma_device *core, void *buffer,
103 +                                    size_t count, u16 offset, u8 reg_width)
104 +{
105 +       void __iomem *addr = core->io_addr + offset;
106 +
107 +       switch (reg_width) {
108 +       case sizeof(u8): {
109 +               u8 *buf = buffer;
110 +
111 +               while (count) {
112 +                       *buf = __raw_readb(addr);
113 +                       buf++;
114 +                       count--;
115 +               }
116 +               break;
117 +       }
118 +       case sizeof(u16): {
119 +               __le16 *buf = buffer;
120 +
121 +               WARN_ON(count & 1);
122 +               while (count) {
123 +                       *buf = (__force __le16)__raw_readw(addr);
124 +                       buf++;
125 +                       count -= 2;
126 +               }
127 +               break;
128 +       }
129 +       case sizeof(u32): {
130 +               __le32 *buf = buffer;
131 +
132 +               WARN_ON(count & 3);
133 +               while (count) {
134 +                       *buf = (__force __le32)__raw_readl(addr);
135 +                       buf++;
136 +                       count -= 4;
137 +               }
138 +               break;
139 +       }
140 +       default:
141 +               WARN_ON(1);
142 +       }
143 +}
144 +
145 +static void bcma_host_soc_block_write(struct bcma_device *core,
146 +                                     const void *buffer,
147 +                                     size_t count, u16 offset, u8 reg_width)
148 +{
149 +       void __iomem *addr = core->io_addr + offset;
150 +
151 +       switch (reg_width) {
152 +       case sizeof(u8): {
153 +               const u8 *buf = buffer;
154 +
155 +               while (count) {
156 +                       __raw_writeb(*buf, addr);
157 +                       buf++;
158 +                       count--;
159 +               }
160 +               break;
161 +       }
162 +       case sizeof(u16): {
163 +               const __le16 *buf = buffer;
164 +
165 +               WARN_ON(count & 1);
166 +               while (count) {
167 +                       __raw_writew((__force u16)(*buf), addr);
168 +                       buf++;
169 +                       count -= 2;
170 +               }
171 +               break;
172 +       }
173 +       case sizeof(u32): {
174 +               const __le32 *buf = buffer;
175 +
176 +               WARN_ON(count & 3);
177 +               while (count) {
178 +                       __raw_writel((__force u32)(*buf), addr);
179 +                       buf++;
180 +                       count -= 4;
181 +               }
182 +               break;
183 +       }
184 +       default:
185 +               WARN_ON(1);
186 +       }
187 +}
188 +#endif /* CONFIG_BCMA_BLOCKIO */
189 +
190 +static u32 bcma_host_soc_aread32(struct bcma_device *core, u16 offset)
191 +{
192 +       return readl(core->io_wrap + offset);
193 +}
194 +
195 +static void bcma_host_soc_awrite32(struct bcma_device *core, u16 offset,
196 +                                 u32 value)
197 +{
198 +       writel(value, core->io_wrap + offset);
199 +}
200 +
201 +const struct bcma_host_ops bcma_host_soc_ops = {
202 +       .read8          = bcma_host_soc_read8,
203 +       .read16         = bcma_host_soc_read16,
204 +       .read32         = bcma_host_soc_read32,
205 +       .write8         = bcma_host_soc_write8,
206 +       .write16        = bcma_host_soc_write16,
207 +       .write32        = bcma_host_soc_write32,
208 +#ifdef CONFIG_BCMA_BLOCKIO
209 +       .block_read     = bcma_host_soc_block_read,
210 +       .block_write    = bcma_host_soc_block_write,
211 +#endif
212 +       .aread32        = bcma_host_soc_aread32,
213 +       .awrite32       = bcma_host_soc_awrite32,
214 +};
215 +
216 +int __init bcma_host_soc_register(struct bcma_soc *soc)
217 +{
218 +       struct bcma_bus *bus = &soc->bus;
219 +       int err;
220 +
221 +       /* iomap only first core. We have to read some register on this core
222 +        * to scan the bus.
223 +        */
224 +       bus->mmio = ioremap_nocache(BCMA_ADDR_BASE, BCMA_CORE_SIZE * 1);
225 +       if (!bus->mmio)
226 +               return -ENOMEM;
227 +
228 +       /* Host specific */
229 +       bus->hosttype = BCMA_HOSTTYPE_SOC;
230 +       bus->ops = &bcma_host_soc_ops;
231 +
232 +       /* Register */
233 +       err = bcma_bus_early_register(bus, &soc->core_cc, &soc->core_mips);
234 +       if (err)
235 +               iounmap(bus->mmio);
236 +
237 +       return err;
238 +}
239 --- a/drivers/bcma/main.c
240 +++ b/drivers/bcma/main.c
241 @@ -66,6 +66,10 @@ static struct bcma_device *bcma_find_cor
242  static void bcma_release_core_dev(struct device *dev)
243  {
244         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
245 +       if (core->io_addr)
246 +               iounmap(core->io_addr);
247 +       if (core->io_wrap)
248 +               iounmap(core->io_wrap);
249         kfree(core);
250  }
251  
252 @@ -93,8 +97,8 @@ static int bcma_register_cores(struct bc
253                         core->dma_dev = &bus->host_pci->dev;
254                         core->irq = bus->host_pci->irq;
255                         break;
256 -               case BCMA_HOSTTYPE_NONE:
257                 case BCMA_HOSTTYPE_SDIO:
258 +               case BCMA_HOSTTYPE_SOC:
259                         break;
260                 }
261  
262 --- a/drivers/bcma/scan.c
263 +++ b/drivers/bcma/scan.c
264 @@ -337,6 +337,16 @@ static int bcma_get_next_core(struct bcm
265                         }
266                 }
267         }
268 +       if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
269 +               core->io_addr = ioremap_nocache(core->addr, BCMA_CORE_SIZE);
270 +               if (!core->io_addr)
271 +                       return -ENOMEM;
272 +               core->io_wrap = ioremap_nocache(core->wrap, BCMA_CORE_SIZE);
273 +               if (!core->io_wrap) {
274 +                       iounmap(core->io_addr);
275 +                       return -ENOMEM;
276 +               }
277 +       }
278         return 0;
279  }
280  
281 @@ -369,7 +379,14 @@ int bcma_bus_scan(struct bcma_bus *bus)
282         bcma_init_bus(bus);
283  
284         erombase = bcma_scan_read32(bus, 0, BCMA_CC_EROM);
285 -       eromptr = bus->mmio;
286 +       if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
287 +               eromptr = ioremap_nocache(erombase, BCMA_CORE_SIZE);
288 +               if (!eromptr)
289 +                       return -ENOMEM;
290 +       } else {
291 +               eromptr = bus->mmio;
292 +       }
293 +
294         eromend = eromptr + BCMA_CORE_SIZE / sizeof(u32);
295  
296         bcma_scan_switch_core(bus, erombase);
297 @@ -404,6 +421,9 @@ int bcma_bus_scan(struct bcma_bus *bus)
298                 list_add(&core->list, &bus->cores);
299         }
300  
301 +       if (bus->hosttype == BCMA_HOSTTYPE_SOC)
302 +               iounmap(eromptr);
303 +
304         return 0;
305  }
306  
307 @@ -414,10 +434,18 @@ int __init bcma_bus_scan_early(struct bc
308         u32 erombase;
309         u32 __iomem *eromptr, *eromend;
310  
311 -       int err, core_num = 0;
312 +       int err = -ENODEV;
313 +       int core_num = 0;
314  
315         erombase = bcma_scan_read32(bus, 0, BCMA_CC_EROM);
316 -       eromptr = bus->mmio;
317 +       if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
318 +               eromptr = ioremap_nocache(erombase, BCMA_CORE_SIZE);
319 +               if (!eromptr)
320 +                       return -ENOMEM;
321 +       } else {
322 +               eromptr = bus->mmio;
323 +       }
324 +
325         eromend = eromptr + BCMA_CORE_SIZE / sizeof(u32);
326  
327         bcma_scan_switch_core(bus, erombase);
328 @@ -447,8 +475,12 @@ int __init bcma_bus_scan_early(struct bc
329                         core->id.class);
330  
331                 list_add(&core->list, &bus->cores);
332 -               return 0;
333 +               err = 0;
334 +               break;
335         }
336  
337 -       return -ENODEV;
338 +       if (bus->hosttype == BCMA_HOSTTYPE_SOC)
339 +               iounmap(eromptr);
340 +
341 +       return err;
342  }
343 --- a/include/linux/bcma/bcma.h
344 +++ b/include/linux/bcma/bcma.h
345 @@ -14,9 +14,9 @@ struct bcma_device;
346  struct bcma_bus;
347  
348  enum bcma_hosttype {
349 -       BCMA_HOSTTYPE_NONE,
350         BCMA_HOSTTYPE_PCI,
351         BCMA_HOSTTYPE_SDIO,
352 +       BCMA_HOSTTYPE_SOC,
353  };
354  
355  struct bcma_chipinfo {
356 @@ -138,6 +138,9 @@ struct bcma_device {
357         u32 addr;
358         u32 wrap;
359  
360 +       void __iomem *io_addr;
361 +       void __iomem *io_wrap;
362 +
363         void *drvdata;
364         struct list_head list;
365  };
366 --- /dev/null
367 +++ b/include/linux/bcma/bcma_soc.h
368 @@ -0,0 +1,16 @@
369 +#ifndef LINUX_BCMA_SOC_H_
370 +#define LINUX_BCMA_SOC_H_
371 +
372 +#include <linux/bcma/bcma.h>
373 +
374 +struct bcma_soc {
375 +       struct bcma_bus bus;
376 +       struct bcma_device core_cc;
377 +       struct bcma_device core_mips;
378 +};
379 +
380 +int __init bcma_host_soc_register(struct bcma_soc *soc);
381 +
382 +int bcma_bus_register(struct bcma_bus *bus);
383 +
384 +#endif /* LINUX_BCMA_SOC_H_ */