blob: d6fac5ccf9389d0db4d22fba0a435704a303072f [file] [log] [blame]
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -08001/*
2 * Intel Wireless WiMAX Connection 2400m
3 * Linux driver model glue for the SDIO device, reset & fw upload
4 *
5 *
6 * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7 * Dirk Brandewie <dirk.j.brandewie@intel.com>
8 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
9 * Yanir Lubetkin <yanirx.lubetkin@intel.com>
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License version
13 * 2 as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * 02110-1301, USA.
24 *
25 *
26 * See i2400m-sdio.h for a general description of this driver.
27 *
28 * This file implements driver model glue, and hook ups for the
29 * generic driver to implement the bus-specific functions (device
30 * communication setup/tear down, firmware upload and resetting).
31 *
32 * ROADMAP
33 *
34 * i2400m_probe()
35 * alloc_netdev()
36 * i2400ms_netdev_setup()
37 * i2400ms_init()
38 * i2400m_netdev_setup()
39 * i2400ms_enable_function()
40 * i2400m_setup()
41 *
42 * i2400m_remove()
43 * i2400m_release()
44 * free_netdev(net_dev)
45 *
46 * i2400ms_bus_reset() Called by i2400m->bus_reset
47 * __i2400ms_reset()
48 * __i2400ms_send_barker()
49 *
50 * i2400ms_bus_dev_start() Called by i2400m_dev_start() [who is
51 * i2400ms_tx_setup() called by i2400m_setup()]
52 * i2400ms_rx_setup()
53 *
54 * i2400ms_bus_dev_stop() Called by i2400m_dev_stop() [who is
55 * i2400ms_rx_release() is called by i2400m_release()]
56 * i2400ms_tx_release()
57 *
58 */
59
60#include <linux/debugfs.h>
61#include <linux/mmc/sdio.h>
62#include <linux/mmc/sdio_func.h>
63#include "i2400m-sdio.h"
64#include <linux/wimax/i2400m.h>
65
66#define D_SUBMODULE main
67#include "sdio-debug-levels.h"
68
69/* IOE WiMAX function timeout in seconds */
70static int ioe_timeout = 2;
71module_param(ioe_timeout, int, 0);
72
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +000073/* Our firmware file name list */
74static const char *i2400ms_bus_fw_names[] = {
75#define I2400MS_FW_FILE_NAME "i2400m-fw-sdio-1.3.sbcf"
76 I2400MS_FW_FILE_NAME,
77 NULL
78};
79
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -080080
81/*
82 * Enable the SDIO function
83 *
84 * Tries to enable the SDIO function; might fail if it is still not
85 * ready (in some hardware, the SDIO WiMAX function is only enabled
86 * when we ask it to explicitly doing). Tries until a timeout is
87 * reached.
88 *
89 * The reverse of this is...sdio_disable_function()
90 *
91 * Returns: 0 if the SDIO function was enabled, < 0 errno code on
92 * error (-ENODEV when it was unable to enable the function).
93 */
94static
95int i2400ms_enable_function(struct sdio_func *func)
96{
97 u64 timeout;
98 int err;
99 struct device *dev = &func->dev;
100
101 d_fnstart(3, dev, "(func %p)\n", func);
102 /* Setup timeout (FIXME: This needs to read the CIS table to
103 * get a real timeout) and then wait for the device to signal
104 * it is ready */
105 timeout = get_jiffies_64() + ioe_timeout * HZ;
106 err = -ENODEV;
107 while (err != 0 && time_before64(get_jiffies_64(), timeout)) {
108 sdio_claim_host(func);
109 err = sdio_enable_func(func);
110 if (0 == err) {
111 sdio_release_host(func);
112 d_printf(2, dev, "SDIO function enabled\n");
113 goto function_enabled;
114 }
115 d_printf(2, dev, "SDIO function failed to enable: %d\n", err);
116 sdio_disable_func(func);
117 sdio_release_host(func);
118 msleep(I2400MS_INIT_SLEEP_INTERVAL);
119 }
120 /* If timed out, device is not there yet -- get -ENODEV so
121 * the device driver core will retry later on. */
122 if (err == -ETIME) {
123 dev_err(dev, "Can't enable WiMAX function; "
124 " has the function been enabled?\n");
125 err = -ENODEV;
126 }
127function_enabled:
128 d_fnend(3, dev, "(func %p) = %d\n", func, err);
129 return err;
130}
131
132
133/*
134 * Setup driver resources needed to communicate with the device
135 *
136 * The fw needs some time to settle, and it was just uploaded,
137 * so give it a break first. I'd prefer to just wait for the device to
138 * send something, but seems the poking we do to enable SDIO stuff
139 * interferes with it, so just give it a break before starting...
140 */
141static
142int i2400ms_bus_dev_start(struct i2400m *i2400m)
143{
144 int result;
145 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
146 struct sdio_func *func = i2400ms->func;
147 struct device *dev = &func->dev;
148
149 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
150 msleep(200);
151 result = i2400ms_rx_setup(i2400ms);
152 if (result < 0)
153 goto error_rx_setup;
154 result = i2400ms_tx_setup(i2400ms);
155 if (result < 0)
156 goto error_tx_setup;
157 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
158 return result;
159
160 i2400ms_tx_release(i2400ms);
161error_tx_setup:
162 i2400ms_rx_release(i2400ms);
163error_rx_setup:
164 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
165 return result;
166}
167
168
169static
170void i2400ms_bus_dev_stop(struct i2400m *i2400m)
171{
172 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
173 struct sdio_func *func = i2400ms->func;
174 struct device *dev = &func->dev;
175
176 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
177 i2400ms_rx_release(i2400ms);
178 i2400ms_tx_release(i2400ms);
179 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
180}
181
182
183/*
184 * Sends a barker buffer to the device
185 *
186 * This helper will allocate a kmalloced buffer and use it to transmit
187 * (then free it). Reason for this is that the SDIO host controller
188 * expects alignment (unknown exactly which) which the stack won't
189 * really provide and certain arches/host-controller combinations
190 * cannot use stack/vmalloc/text areas for DMA transfers.
191 */
192static
193int __i2400ms_send_barker(struct i2400ms *i2400ms,
194 const __le32 *barker, size_t barker_size)
195{
196 int ret;
197 struct sdio_func *func = i2400ms->func;
198 struct device *dev = &func->dev;
199 void *buffer;
200
201 ret = -ENOMEM;
202 buffer = kmalloc(I2400MS_BLK_SIZE, GFP_KERNEL);
203 if (buffer == NULL)
204 goto error_kzalloc;
205
206 memcpy(buffer, barker, barker_size);
207 sdio_claim_host(func);
208 ret = sdio_memcpy_toio(func, 0, buffer, I2400MS_BLK_SIZE);
209 sdio_release_host(func);
210
211 if (ret < 0)
212 d_printf(0, dev, "E: barker error: %d\n", ret);
213
214 kfree(buffer);
215error_kzalloc:
216 return ret;
217}
218
219
220/*
221 * Reset a device at different levels (warm, cold or bus)
222 *
223 * @i2400ms: device descriptor
224 * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
225 *
226 * FIXME: not tested -- need to confirm expected effects
227 *
228 * Warm and cold resets get an SDIO reset if they fail (unimplemented)
229 *
230 * Warm reset:
231 *
232 * The device will be fully reset internally, but won't be
233 * disconnected from the USB bus (so no reenumeration will
234 * happen). Firmware upload will be neccessary.
235 *
236 * The device will send a reboot barker in the notification endpoint
237 * that will trigger the driver to reinitialize the state
238 * automatically from notif.c:i2400m_notification_grok() into
239 * i2400m_dev_bootstrap_delayed().
240 *
241 * Cold and bus (USB) reset:
242 *
243 * The device will be fully reset internally, disconnected from the
244 * USB bus an a reenumeration will happen. Firmware upload will be
245 * neccessary. Thus, we don't do any locking or struct
246 * reinitialization, as we are going to be fully disconnected and
247 * reenumerated.
248 *
249 * Note we need to return -ENODEV if a warm reset was requested and we
250 * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
251 * and wimax_dev->op_reset.
252 *
253 * WARNING: no driver state saved/fixed
254 */
255static
256int i2400ms_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
257{
Dirk Brandewie10b1de62009-05-12 07:54:00 -0700258 int result = 0;
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800259 struct i2400ms *i2400ms =
260 container_of(i2400m, struct i2400ms, i2400m);
261 struct device *dev = i2400m_dev(i2400m);
262 static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
Harvey Harrisonee437772009-02-01 00:43:54 -0800263 cpu_to_le32(I2400M_WARM_RESET_BARKER),
264 cpu_to_le32(I2400M_WARM_RESET_BARKER),
265 cpu_to_le32(I2400M_WARM_RESET_BARKER),
266 cpu_to_le32(I2400M_WARM_RESET_BARKER),
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800267 };
268 static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
Harvey Harrisonee437772009-02-01 00:43:54 -0800269 cpu_to_le32(I2400M_COLD_RESET_BARKER),
270 cpu_to_le32(I2400M_COLD_RESET_BARKER),
271 cpu_to_le32(I2400M_COLD_RESET_BARKER),
272 cpu_to_le32(I2400M_COLD_RESET_BARKER),
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800273 };
274
275 if (rt == I2400M_RT_WARM)
276 result = __i2400ms_send_barker(i2400ms, i2400m_WARM_BOOT_BARKER,
277 sizeof(i2400m_WARM_BOOT_BARKER));
278 else if (rt == I2400M_RT_COLD)
279 result = __i2400ms_send_barker(i2400ms, i2400m_COLD_BOOT_BARKER,
280 sizeof(i2400m_COLD_BOOT_BARKER));
281 else if (rt == I2400M_RT_BUS) {
282do_bus_reset:
Dirk Brandewie10b1de62009-05-12 07:54:00 -0700283 /* call netif_tx_disable() before sending IOE disable,
284 * so that all the tx from network layer are stopped
285 * while IOE is being reset. Make sure it is called
286 * only after register_netdev() was issued.
287 */
288 if (i2400m->wimax_dev.net_dev->reg_state == NETREG_REGISTERED)
289 netif_tx_disable(i2400m->wimax_dev.net_dev);
290
291 sdio_claim_host(i2400ms->func);
292 sdio_disable_func(i2400ms->func);
293 sdio_release_host(i2400ms->func);
294
295 /* Wait for the device to settle */
296 msleep(40);
297
298 result = i2400ms_enable_function(i2400ms->func);
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800299 } else
300 BUG();
301 if (result < 0 && rt != I2400M_RT_BUS) {
302 dev_err(dev, "%s reset failed (%d); trying SDIO reset\n",
303 rt == I2400M_RT_WARM ? "warm" : "cold", result);
304 rt = I2400M_RT_BUS;
305 goto do_bus_reset;
306 }
307 return result;
308}
309
310
311static
312void i2400ms_netdev_setup(struct net_device *net_dev)
313{
314 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
315 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
316 i2400ms_init(i2400ms);
317 i2400m_netdev_setup(net_dev);
318}
319
320
321/*
322 * Debug levels control; see debug.h
323 */
324struct d_level D_LEVEL[] = {
325 D_SUBMODULE_DEFINE(main),
326 D_SUBMODULE_DEFINE(tx),
327 D_SUBMODULE_DEFINE(rx),
328 D_SUBMODULE_DEFINE(fw),
329};
330size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
331
332
333#define __debugfs_register(prefix, name, parent) \
334do { \
335 result = d_level_register_debugfs(prefix, name, parent); \
336 if (result < 0) \
337 goto error; \
338} while (0)
339
340
341static
342int i2400ms_debugfs_add(struct i2400ms *i2400ms)
343{
344 int result;
345 struct dentry *dentry = i2400ms->i2400m.wimax_dev.debugfs_dentry;
346
347 dentry = debugfs_create_dir("i2400m-usb", dentry);
348 result = PTR_ERR(dentry);
349 if (IS_ERR(dentry)) {
350 if (result == -ENODEV)
351 result = 0; /* No debugfs support */
352 goto error;
353 }
354 i2400ms->debugfs_dentry = dentry;
355 __debugfs_register("dl_", main, dentry);
356 __debugfs_register("dl_", tx, dentry);
357 __debugfs_register("dl_", rx, dentry);
358 __debugfs_register("dl_", fw, dentry);
359
360 return 0;
361
362error:
363 debugfs_remove_recursive(i2400ms->debugfs_dentry);
364 return result;
365}
366
367
368/*
369 * Probe a i2400m interface and register it
370 *
371 * @func: SDIO function
372 * @id: SDIO device ID
373 * @returns: 0 if ok, < 0 errno code on error.
374 *
375 * Alloc a net device, initialize the bus-specific details and then
376 * calls the bus-generic initialization routine. That will register
377 * the wimax and netdev devices, upload the firmware [using
378 * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
379 * communication with the device and then will start to talk to it to
380 * finnish setting it up.
381 *
382 * Initialization is tricky; some instances of the hw are packed with
383 * others in a way that requires a third driver that enables the WiMAX
384 * function. In those cases, we can't enable the SDIO function and
385 * we'll return with -ENODEV. When the driver that enables the WiMAX
386 * function does its thing, it has to do a bus_rescan_devices() on the
387 * SDIO bus so this driver is called again to enumerate the WiMAX
388 * function.
389 */
390static
391int i2400ms_probe(struct sdio_func *func,
392 const struct sdio_device_id *id)
393{
394 int result;
395 struct net_device *net_dev;
396 struct device *dev = &func->dev;
397 struct i2400m *i2400m;
398 struct i2400ms *i2400ms;
399
400 /* Allocate instance [calls i2400m_netdev_setup() on it]. */
401 result = -ENOMEM;
402 net_dev = alloc_netdev(sizeof(*i2400ms), "wmx%d",
403 i2400ms_netdev_setup);
404 if (net_dev == NULL) {
405 dev_err(dev, "no memory for network device instance\n");
406 goto error_alloc_netdev;
407 }
408 SET_NETDEV_DEV(net_dev, dev);
409 i2400m = net_dev_to_i2400m(net_dev);
410 i2400ms = container_of(i2400m, struct i2400ms, i2400m);
411 i2400m->wimax_dev.net_dev = net_dev;
412 i2400ms->func = func;
413 sdio_set_drvdata(func, i2400ms);
414
415 i2400m->bus_tx_block_size = I2400MS_BLK_SIZE;
416 i2400m->bus_pl_size_max = I2400MS_PL_SIZE_MAX;
417 i2400m->bus_dev_start = i2400ms_bus_dev_start;
418 i2400m->bus_dev_stop = i2400ms_bus_dev_stop;
419 i2400m->bus_tx_kick = i2400ms_bus_tx_kick;
420 i2400m->bus_reset = i2400ms_bus_reset;
421 i2400m->bus_bm_cmd_send = i2400ms_bus_bm_cmd_send;
422 i2400m->bus_bm_wait_for_ack = i2400ms_bus_bm_wait_for_ack;
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +0000423 i2400m->bus_fw_names = i2400ms_bus_fw_names;
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800424 i2400m->bus_bm_mac_addr_impaired = 1;
425
Inaky Perez-Gonzaleza0a4c4c2009-04-30 14:39:21 -0700426 sdio_claim_host(func);
427 result = sdio_set_block_size(func, I2400MS_BLK_SIZE);
428 sdio_release_host(func);
429 if (result < 0) {
430 dev_err(dev, "Failed to set block size: %d\n", result);
431 goto error_set_blk_size;
432 }
433
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800434 result = i2400ms_enable_function(i2400ms->func);
435 if (result < 0) {
436 dev_err(dev, "Cannot enable SDIO function: %d\n", result);
437 goto error_func_enable;
438 }
439
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800440 result = i2400m_setup(i2400m, I2400M_BRI_NO_REBOOT);
441 if (result < 0) {
442 dev_err(dev, "cannot setup device: %d\n", result);
443 goto error_setup;
444 }
445
446 result = i2400ms_debugfs_add(i2400ms);
447 if (result < 0) {
448 dev_err(dev, "cannot create SDIO debugfs: %d\n",
449 result);
450 goto error_debugfs_add;
451 }
452 return 0;
453
454error_debugfs_add:
455 i2400m_release(i2400m);
456error_setup:
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800457 sdio_claim_host(func);
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800458 sdio_disable_func(func);
459 sdio_release_host(func);
460error_func_enable:
Inaky Perez-Gonzaleza0a4c4c2009-04-30 14:39:21 -0700461error_set_blk_size:
462 sdio_set_drvdata(func, NULL);
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800463 free_netdev(net_dev);
464error_alloc_netdev:
465 return result;
466}
467
468
469static
470void i2400ms_remove(struct sdio_func *func)
471{
472 struct device *dev = &func->dev;
473 struct i2400ms *i2400ms = sdio_get_drvdata(func);
474 struct i2400m *i2400m = &i2400ms->i2400m;
475 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
476
477 d_fnstart(3, dev, "SDIO func %p\n", func);
478 debugfs_remove_recursive(i2400ms->debugfs_dentry);
479 i2400m_release(i2400m);
480 sdio_set_drvdata(func, NULL);
481 sdio_claim_host(func);
482 sdio_disable_func(func);
483 sdio_release_host(func);
484 free_netdev(net_dev);
485 d_fnend(3, dev, "SDIO func %p\n", func);
486}
487
488enum {
489 I2400MS_INTEL_VID = 0x89,
490};
491
492static
493const struct sdio_device_id i2400ms_sdio_ids[] = {
494 /* Intel: i2400m WiMAX over SDIO */
495 { SDIO_DEVICE(I2400MS_INTEL_VID, 0x1402) },
496 { }, /* end: all zeroes */
497};
498MODULE_DEVICE_TABLE(sdio, i2400ms_sdio_ids);
499
500
501static
502struct sdio_driver i2400m_sdio_driver = {
503 .name = KBUILD_MODNAME,
504 .probe = i2400ms_probe,
505 .remove = i2400ms_remove,
506 .id_table = i2400ms_sdio_ids,
507};
508
509
510static
511int __init i2400ms_driver_init(void)
512{
513 return sdio_register_driver(&i2400m_sdio_driver);
514}
515module_init(i2400ms_driver_init);
516
517
518static
519void __exit i2400ms_driver_exit(void)
520{
521 flush_scheduled_work(); /* for the stuff we schedule */
522 sdio_unregister_driver(&i2400m_sdio_driver);
523}
524module_exit(i2400ms_driver_exit);
525
526
527MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
528MODULE_DESCRIPTION("Intel 2400M WiMAX networking for SDIO");
529MODULE_LICENSE("GPL");
530MODULE_FIRMWARE(I2400MS_FW_FILE_NAME);