aboutsummaryrefslogtreecommitdiffstats
path: root/package/broadcom-wl/src/glue/wl_glue.c
blob: 18e2b3a583ccfc359129be67da347676d86969a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * wl_glue.c: Broadcom WL support module providing a unified SSB/BCMA handling.
 * Copyright (C) 2011 Jo-Philipp Wich <jow@openwrt.org>
 */

#include "wl_glue.h"

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>

#ifdef CONFIG_BCM47XX
#include <bcm47xx.h>
#endif

#ifdef CONFIG_SSB
#include <linux/ssb/ssb.h>
#endif

#ifdef CONFIG_BCMA
#include <linux/bcma/bcma.h>
#endif

MODULE_AUTHOR("Jo-Philipp Wich (jow@openwrt.org)");
MODULE_DESCRIPTION("Broadcom WL SSB/BCMA compatibility layer");
MODULE_LICENSE("GPL");

static wl_glue_attach_cb_t attach_cb = NULL;
static wl_glue_remove_cb_t remove_cb = NULL;
static enum wl_glue_bus_type active_bus_type = WL_GLUE_BUS_TYPE_UNSPEC;
static int wl_glue_attached = 0;


#ifdef CONFIG_SSB
static int wl_glue_ssb_probe(struct ssb_device *dev, const struct ssb_device_id *id)
{
	void *mmio;
	void *wldev;

	if (!attach_cb)
	{
		pr_err("No attach callback registered\n");
		return -ENOSYS;
	}

	if (dev->bus->bustype != SSB_BUSTYPE_SSB)
	{
		pr_err("Attaching to SSB behind PCI is not supported. Please remove the b43 ssb bridge\n");
		return -EINVAL;
	}

	mmio = (void *) 0x18000000 + dev->core_index * 0x1000;
	wldev = attach_cb(id->vendor, id->coreid, (ulong)mmio, dev, dev->irq);

	if (!wldev)
	{
		pr_err("The attach callback failed, SSB probe aborted\n");
		return -ENODEV;
	}

	ssb_set_drvdata(dev, wldev);
	return 0;
}

static void wl_glue_ssb_remove(struct ssb_device *dev)
{
	void *wldev = ssb_get_drvdata(dev);

	if (remove_cb)
		remove_cb(wldev);

	ssb_set_drvdata(dev, NULL);
}

static const struct ssb_device_id wl_glue_ssb_tbl[] = {
	SSB_DEVICE(SSB_VENDOR_BROADCOM, SSB_DEV_80211, SSB_ANY_REV),
	SSB_DEVTABLE_END
};

static struct ssb_driver wl_glue_ssb_driver = {
	.name     = KBUILD_MODNAME,
	.id_table = wl_glue_ssb_tbl,
	.probe    = wl_glue_ssb_probe,
	.remove   = wl_glue_ssb_remove,
};
#endif /* CONFIG_SSB */

#ifdef CONFIG_BCMA
static int wl_glue_bcma_probe(struct bcma_device *dev)
{
	void *mmio;
	void *wldev;

	if (!attach_cb)
	{
		pr_err("No attach callback registered\n");
		return -ENOSYS;
	}

	if (dev->bus->hosttype != BCMA_HOSTTYPE_SOC)
	{
		pr_err("Unsupported BCMA bus type %d\n", dev->bus->hosttype);
		return -EINVAL;
	}

	/*
	 * NB:
	 * 0x18000000 = BCMA_ADDR_BASE
	 * 0x1000     = BCMA_CORE_SIZE
	 */

	mmio = (void *) 0x18000000 + dev->core_index * 0x1000;
	wldev = attach_cb(dev->id.manuf, dev->id.id, (ulong)mmio, dev, dev->irq);

	if (!wldev)
	{
		pr_err("The attach callback failed, BCMA probe aborted\n");
		return -ENODEV;
	}

	bcma_set_drvdata(dev, wldev);
	return 0;
}

static void wl_glue_bcma_remove(struct bcma_device *dev)
{
	void *wldev = bcma_get_drvdata(dev);

	if (remove_cb)
		remove_cb(wldev);

	bcma_set_drvdata(dev, NULL);
}

static const struct bcma_device_id wl_glue_bcma_tbl[] = {
	BCMA_CORE(BCMA_MANUF_BCM, BCMA_CORE_80211, BCMA_ANY_REV, BCMA_ANY_CLASS),
	BCMA_CORETABLE_END
};

static struct bcma_driver wl_glue_bcma_driver = {
	.name     = KBUILD_MODNAME,
	.id_table = wl_glue_bcma_tbl,
	.probe    = wl_glue_bcma_probe,
	.remove   = wl_glue_bcma_remove,
};
#endif /* CONFIG_BCMA */


void wl_glue_set_attach_callback(wl_glue_attach_cb_t cb)
{
	attach_cb = cb;
}
EXPORT_SYMBOL(wl_glue_set_attach_callback);

void wl_glue_set_remove_callback(wl_glue_remove_cb_t cb)
{
	remove_cb = cb;
}
EXPORT_SYMBOL(wl_glue_set_remove_callback);

int wl_glue_register(void)
{
	int err;

	switch(active_bus_type)
	{
#ifdef CONFIG_SSB
	case WL_GLUE_BUS_TYPE_SSB:
		err = ssb_driver_register(&wl_glue_ssb_driver);
		break;
#endif /* CONFIG_SSB */

#ifdef CONFIG_BCMA
	case WL_GLUE_BUS_TYPE_BCMA:
		err = bcma_driver_register(&wl_glue_bcma_driver);
		break;
#endif /* CONFIG_BCMA */

	default:
		pr_err("Not attaching through glue driver due to unsupported bus\n");
		err = -ENOSYS;
		break;
	}

	if (!err)
	{
		pr_info("SSB/BCMA glue driver successfully attached\n");
		wl_glue_attached = 1;
	}

	return err;
}
EXPORT_SYMBOL(wl_glue_register);

int wl_glue_unregister(void)
{
	int err;

	if (!wl_glue_attached)
		return -ENOSYS;

	switch (active_bus_type)
	{
#ifdef CONFIG_SSB
	case WL_GLUE_BUS_TYPE_SSB:
		ssb_driver_unregister(&wl_glue_ssb_driver);
		err = 0;
		break;
#endif /* CONFIG_SSB */

#ifdef CONFIG_BCMA
	case WL_GLUE_BUS_TYPE_BCMA:
		bcma_driver_unregister(&wl_glue_bcma_driver);
		err = 0;
		break;
#endif /* CONFIG_BCMA */

	default:
		pr_err("Not removing glue driver due to unsupported bus\n");
		err = -ENOSYS;
		break;
	}

	if (!err)
	{
		pr_info("SSB/BCMA glue driver successfully detached\n");
		wl_glue_attached = 0;
	}

	return err;
}
EXPORT_SYMBOL(wl_glue_unregister);

struct device * wl_glue_get_dmadev(void *dev)
{
	struct device *dma_dev;

	if (!wl_glue_attached)
	{
		BUG();
		return NULL;
	}

	switch (active_bus_type)
	{
#ifdef CONFIG_SSB
	case WL_GLUE_BUS_TYPE_SSB:
		dma_dev = ((struct ssb_device *)dev)->dma_dev;
		break;
#endif /* CONFIG_SSB */

#ifdef CONFIG_BCMA
	case WL_GLUE_BUS_TYPE_BCMA:
		dma_dev = ((struct bcma_device *)dev)->dma_dev;
		break;
#endif /* CONFIG_BCMA */

	default:
		BUG();
		dma_dev = NULL;
		break;
	}

	return dma_dev;
}
EXPORT_SYMBOL(wl_glue_get_dmadev);


static int __init wl_glue_init(void)
{
#ifdef CONFIG_BCM47XX
	/*
	 * BCM47xx currently supports either SSB or BCMA bus,
	 * determine the used one from the info set by the
	 * platform setup code.
	 */
	switch (bcm47xx_active_bus_type)
	{
#ifdef CONFIG_SSB
	case BCM47XX_BUS_TYPE_SSB:
		active_bus_type = WL_GLUE_BUS_TYPE_SSB;
		break;
#endif /* CONFIG_SSB */

#ifdef CONFIG_BCMA
	case BCM47XX_BUS_TYPE_BCMA:
		active_bus_type = WL_GLUE_BUS_TYPE_BCMA;
		break;
#endif /* CONFIG_BCMA */
	}
#endif /* CONFIG_BCM47XX */

#ifdef CONFIG_BCM63XX
#ifdef CONFIG_SSB
	/*
	 * BCM63xx currently only uses SSB, so assume that.
	 */
	active_bus_type = WL_GLUE_BUS_TYPE_SSB;
#endif /* CONFIG_SSB */
#endif /* CONFIG_BCM63XX */

	/* do not fail here, let wl_glue_register() return -ENOSYS later */
	if (active_bus_type == WL_GLUE_BUS_TYPE_UNSPEC)
		pr_err("Unable to determine used system bus type\n");

	return 0;
}

static void __exit wl_glue_exit(void)
{
	if (wl_glue_attached)
	{
		if (wl_glue_unregister())
			pr_err("Failed to unregister glue driver\n");

		wl_glue_attached = 0;
	}

	return;
}

module_init(wl_glue_init);
module_exit(wl_glue_exit);