2 * Intel Wireless WiMAX Connection 2400m
3 * Linux driver model glue for the SDIO device, reset & fw upload
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>
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.
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.
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
26 * See i2400m-sdio.h for a general description of this driver.
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).
36 * i2400ms_netdev_setup()
38 * i2400m_netdev_setup()
39 * i2400ms_enable_function()
44 * free_netdev(net_dev)
46 * i2400ms_bus_reset() Called by i2400m->bus_reset
48 * __i2400ms_send_barker()
50 * i2400ms_bus_dev_start() Called by i2400m_dev_start() [who is
51 * i2400ms_tx_setup() called by i2400m_setup()]
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()
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>
66 #define D_SUBMODULE main
67 #include "sdio-debug-levels.h"
69 /* IOE WiMAX function timeout in seconds */
70 static int ioe_timeout = 2;
71 module_param(ioe_timeout, int, 0);
73 /* Our firmware file name list */
74 static const char *i2400ms_bus_fw_names[] = {
75 #define I2400MS_FW_FILE_NAME "i2400m-fw-sdio-1.3.sbcf"
81 static const struct i2400m_poke_table i2400ms_pokes[] = {
82 I2400M_FW_POKE(0x6BE260, 0x00000088),
83 I2400M_FW_POKE(0x080550, 0x00000005),
84 I2400M_FW_POKE(0xAE0000, 0x00000000),
85 I2400M_FW_POKE(0x000000, 0x00000000), /* MUST be 0 terminated or bad
86 * things will happen */
90 * Enable the SDIO function
92 * Tries to enable the SDIO function; might fail if it is still not
93 * ready (in some hardware, the SDIO WiMAX function is only enabled
94 * when we ask it to explicitly doing). Tries until a timeout is
97 * The reverse of this is...sdio_disable_function()
99 * Returns: 0 if the SDIO function was enabled, < 0 errno code on
100 * error (-ENODEV when it was unable to enable the function).
103 int i2400ms_enable_function(struct sdio_func *func)
107 struct device *dev = &func->dev;
109 d_fnstart(3, dev, "(func %p)\n", func);
110 /* Setup timeout (FIXME: This needs to read the CIS table to
111 * get a real timeout) and then wait for the device to signal
113 timeout = get_jiffies_64() + ioe_timeout * HZ;
115 while (err != 0 && time_before64(get_jiffies_64(), timeout)) {
116 sdio_claim_host(func);
117 err = sdio_enable_func(func);
119 sdio_release_host(func);
120 d_printf(2, dev, "SDIO function enabled\n");
121 goto function_enabled;
123 d_printf(2, dev, "SDIO function failed to enable: %d\n", err);
124 sdio_disable_func(func);
125 sdio_release_host(func);
126 msleep(I2400MS_INIT_SLEEP_INTERVAL);
128 /* If timed out, device is not there yet -- get -ENODEV so
129 * the device driver core will retry later on. */
131 dev_err(dev, "Can't enable WiMAX function; "
132 " has the function been enabled?\n");
136 d_fnend(3, dev, "(func %p) = %d\n", func, err);
142 * Setup driver resources needed to communicate with the device
144 * The fw needs some time to settle, and it was just uploaded,
145 * so give it a break first. I'd prefer to just wait for the device to
146 * send something, but seems the poking we do to enable SDIO stuff
147 * interferes with it, so just give it a break before starting...
150 int i2400ms_bus_dev_start(struct i2400m *i2400m)
153 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
154 struct sdio_func *func = i2400ms->func;
155 struct device *dev = &func->dev;
157 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
159 result = i2400ms_tx_setup(i2400ms);
162 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
166 i2400ms_tx_release(i2400ms);
167 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
173 void i2400ms_bus_dev_stop(struct i2400m *i2400m)
175 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
176 struct sdio_func *func = i2400ms->func;
177 struct device *dev = &func->dev;
179 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
180 i2400ms_tx_release(i2400ms);
181 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
186 * Sends a barker buffer to the device
188 * This helper will allocate a kmalloced buffer and use it to transmit
189 * (then free it). Reason for this is that the SDIO host controller
190 * expects alignment (unknown exactly which) which the stack won't
191 * really provide and certain arches/host-controller combinations
192 * cannot use stack/vmalloc/text areas for DMA transfers.
195 int __i2400ms_send_barker(struct i2400ms *i2400ms,
196 const __le32 *barker, size_t barker_size)
199 struct sdio_func *func = i2400ms->func;
200 struct device *dev = &func->dev;
204 buffer = kmalloc(I2400MS_BLK_SIZE, GFP_KERNEL);
208 memcpy(buffer, barker, barker_size);
209 sdio_claim_host(func);
210 ret = sdio_memcpy_toio(func, 0, buffer, I2400MS_BLK_SIZE);
211 sdio_release_host(func);
214 d_printf(0, dev, "E: barker error: %d\n", ret);
223 * Reset a device at different levels (warm, cold or bus)
225 * @i2400ms: device descriptor
226 * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
228 * FIXME: not tested -- need to confirm expected effects
230 * Warm and cold resets get an SDIO reset if they fail (unimplemented)
234 * The device will be fully reset internally, but won't be
235 * disconnected from the USB bus (so no reenumeration will
236 * happen). Firmware upload will be neccessary.
238 * The device will send a reboot barker in the notification endpoint
239 * that will trigger the driver to reinitialize the state
240 * automatically from notif.c:i2400m_notification_grok() into
241 * i2400m_dev_bootstrap_delayed().
243 * Cold and bus (USB) reset:
245 * The device will be fully reset internally, disconnected from the
246 * USB bus an a reenumeration will happen. Firmware upload will be
247 * neccessary. Thus, we don't do any locking or struct
248 * reinitialization, as we are going to be fully disconnected and
251 * Note we need to return -ENODEV if a warm reset was requested and we
252 * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
253 * and wimax_dev->op_reset.
255 * WARNING: no driver state saved/fixed
258 int i2400ms_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
261 struct i2400ms *i2400ms =
262 container_of(i2400m, struct i2400ms, i2400m);
263 struct device *dev = i2400m_dev(i2400m);
264 static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
265 cpu_to_le32(I2400M_WARM_RESET_BARKER),
266 cpu_to_le32(I2400M_WARM_RESET_BARKER),
267 cpu_to_le32(I2400M_WARM_RESET_BARKER),
268 cpu_to_le32(I2400M_WARM_RESET_BARKER),
270 static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
271 cpu_to_le32(I2400M_COLD_RESET_BARKER),
272 cpu_to_le32(I2400M_COLD_RESET_BARKER),
273 cpu_to_le32(I2400M_COLD_RESET_BARKER),
274 cpu_to_le32(I2400M_COLD_RESET_BARKER),
277 if (rt == I2400M_RT_WARM)
278 result = __i2400ms_send_barker(i2400ms, i2400m_WARM_BOOT_BARKER,
279 sizeof(i2400m_WARM_BOOT_BARKER));
280 else if (rt == I2400M_RT_COLD)
281 result = __i2400ms_send_barker(i2400ms, i2400m_COLD_BOOT_BARKER,
282 sizeof(i2400m_COLD_BOOT_BARKER));
283 else if (rt == I2400M_RT_BUS) {
285 /* call netif_tx_disable() before sending IOE disable,
286 * so that all the tx from network layer are stopped
287 * while IOE is being reset. Make sure it is called
288 * only after register_netdev() was issued.
290 if (i2400m->wimax_dev.net_dev->reg_state == NETREG_REGISTERED)
291 netif_tx_disable(i2400m->wimax_dev.net_dev);
293 i2400ms_rx_release(i2400ms);
294 sdio_claim_host(i2400ms->func);
295 sdio_disable_func(i2400ms->func);
296 sdio_release_host(i2400ms->func);
298 /* Wait for the device to settle */
301 result = i2400ms_enable_function(i2400ms->func);
303 i2400ms_rx_setup(i2400ms);
306 if (result < 0 && rt != I2400M_RT_BUS) {
307 dev_err(dev, "%s reset failed (%d); trying SDIO reset\n",
308 rt == I2400M_RT_WARM ? "warm" : "cold", result);
317 void i2400ms_netdev_setup(struct net_device *net_dev)
319 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
320 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
321 i2400ms_init(i2400ms);
322 i2400m_netdev_setup(net_dev);
327 * Debug levels control; see debug.h
329 struct d_level D_LEVEL[] = {
330 D_SUBMODULE_DEFINE(main),
331 D_SUBMODULE_DEFINE(tx),
332 D_SUBMODULE_DEFINE(rx),
333 D_SUBMODULE_DEFINE(fw),
335 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
338 #define __debugfs_register(prefix, name, parent) \
340 result = d_level_register_debugfs(prefix, name, parent); \
347 int i2400ms_debugfs_add(struct i2400ms *i2400ms)
350 struct dentry *dentry = i2400ms->i2400m.wimax_dev.debugfs_dentry;
352 dentry = debugfs_create_dir("i2400m-usb", dentry);
353 result = PTR_ERR(dentry);
354 if (IS_ERR(dentry)) {
355 if (result == -ENODEV)
356 result = 0; /* No debugfs support */
359 i2400ms->debugfs_dentry = dentry;
360 __debugfs_register("dl_", main, dentry);
361 __debugfs_register("dl_", tx, dentry);
362 __debugfs_register("dl_", rx, dentry);
363 __debugfs_register("dl_", fw, dentry);
368 debugfs_remove_recursive(i2400ms->debugfs_dentry);
374 * Probe a i2400m interface and register it
376 * @func: SDIO function
377 * @id: SDIO device ID
378 * @returns: 0 if ok, < 0 errno code on error.
380 * Alloc a net device, initialize the bus-specific details and then
381 * calls the bus-generic initialization routine. That will register
382 * the wimax and netdev devices, upload the firmware [using
383 * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
384 * communication with the device and then will start to talk to it to
385 * finnish setting it up.
387 * Initialization is tricky; some instances of the hw are packed with
388 * others in a way that requires a third driver that enables the WiMAX
389 * function. In those cases, we can't enable the SDIO function and
390 * we'll return with -ENODEV. When the driver that enables the WiMAX
391 * function does its thing, it has to do a bus_rescan_devices() on the
392 * SDIO bus so this driver is called again to enumerate the WiMAX
396 int i2400ms_probe(struct sdio_func *func,
397 const struct sdio_device_id *id)
400 struct net_device *net_dev;
401 struct device *dev = &func->dev;
402 struct i2400m *i2400m;
403 struct i2400ms *i2400ms;
405 /* Allocate instance [calls i2400m_netdev_setup() on it]. */
407 net_dev = alloc_netdev(sizeof(*i2400ms), "wmx%d",
408 i2400ms_netdev_setup);
409 if (net_dev == NULL) {
410 dev_err(dev, "no memory for network device instance\n");
411 goto error_alloc_netdev;
413 SET_NETDEV_DEV(net_dev, dev);
414 i2400m = net_dev_to_i2400m(net_dev);
415 i2400ms = container_of(i2400m, struct i2400ms, i2400m);
416 i2400m->wimax_dev.net_dev = net_dev;
417 i2400ms->func = func;
418 sdio_set_drvdata(func, i2400ms);
420 i2400m->bus_tx_block_size = I2400MS_BLK_SIZE;
421 i2400m->bus_pl_size_max = I2400MS_PL_SIZE_MAX;
422 i2400m->bus_dev_start = i2400ms_bus_dev_start;
423 i2400m->bus_dev_stop = i2400ms_bus_dev_stop;
424 i2400m->bus_tx_kick = i2400ms_bus_tx_kick;
425 i2400m->bus_reset = i2400ms_bus_reset;
426 /* The iwmc3200-wimax sometimes requires the driver to try
427 * hard when we paint it into a corner. */
428 i2400m->bus_bm_retries = I3200_BOOT_RETRIES;
429 i2400m->bus_bm_cmd_send = i2400ms_bus_bm_cmd_send;
430 i2400m->bus_bm_wait_for_ack = i2400ms_bus_bm_wait_for_ack;
431 i2400m->bus_fw_names = i2400ms_bus_fw_names;
432 i2400m->bus_bm_mac_addr_impaired = 1;
433 i2400m->bus_bm_pokes_table = &i2400ms_pokes[0];
435 sdio_claim_host(func);
436 result = sdio_set_block_size(func, I2400MS_BLK_SIZE);
437 sdio_release_host(func);
439 dev_err(dev, "Failed to set block size: %d\n", result);
440 goto error_set_blk_size;
443 result = i2400ms_enable_function(i2400ms->func);
445 dev_err(dev, "Cannot enable SDIO function: %d\n", result);
446 goto error_func_enable;
449 result = i2400ms_rx_setup(i2400ms);
453 result = i2400m_setup(i2400m, I2400M_BRI_NO_REBOOT);
455 dev_err(dev, "cannot setup device: %d\n", result);
459 result = i2400ms_debugfs_add(i2400ms);
461 dev_err(dev, "cannot create SDIO debugfs: %d\n",
463 goto error_debugfs_add;
468 i2400m_release(i2400m);
470 i2400ms_rx_release(i2400ms);
472 sdio_claim_host(func);
473 sdio_disable_func(func);
474 sdio_release_host(func);
477 sdio_set_drvdata(func, NULL);
478 free_netdev(net_dev);
485 void i2400ms_remove(struct sdio_func *func)
487 struct device *dev = &func->dev;
488 struct i2400ms *i2400ms = sdio_get_drvdata(func);
489 struct i2400m *i2400m = &i2400ms->i2400m;
490 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
492 d_fnstart(3, dev, "SDIO func %p\n", func);
493 debugfs_remove_recursive(i2400ms->debugfs_dentry);
494 i2400ms_rx_release(i2400ms);
495 i2400m_release(i2400m);
496 sdio_set_drvdata(func, NULL);
497 sdio_claim_host(func);
498 sdio_disable_func(func);
499 sdio_release_host(func);
500 free_netdev(net_dev);
501 d_fnend(3, dev, "SDIO func %p\n", func);
505 I2400MS_INTEL_VID = 0x89,
509 const struct sdio_device_id i2400ms_sdio_ids[] = {
510 /* Intel: i2400m WiMAX over SDIO */
511 { SDIO_DEVICE(I2400MS_INTEL_VID, 0x1402) },
512 { }, /* end: all zeroes */
514 MODULE_DEVICE_TABLE(sdio, i2400ms_sdio_ids);
518 struct sdio_driver i2400m_sdio_driver = {
519 .name = KBUILD_MODNAME,
520 .probe = i2400ms_probe,
521 .remove = i2400ms_remove,
522 .id_table = i2400ms_sdio_ids,
527 int __init i2400ms_driver_init(void)
529 return sdio_register_driver(&i2400m_sdio_driver);
531 module_init(i2400ms_driver_init);
535 void __exit i2400ms_driver_exit(void)
537 flush_scheduled_work(); /* for the stuff we schedule */
538 sdio_unregister_driver(&i2400m_sdio_driver);
540 module_exit(i2400ms_driver_exit);
543 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
544 MODULE_DESCRIPTION("Intel 2400M WiMAX networking for SDIO");
545 MODULE_LICENSE("GPL");
546 MODULE_FIRMWARE(I2400MS_FW_FILE_NAME);