GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / net / wimax / i2400m / sdio.c
blobd8281cbae4c04f5599178d4ef60cafee24f68e5d
1 /*
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
23 * 02110-1301, USA.
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).
32 * ROADMAP
34 * i2400m_probe()
35 * alloc_netdev()
36 * i2400ms_netdev_setup()
37 * i2400ms_init()
38 * i2400m_netdev_setup()
39 * i2400ms_enable_function()
40 * i2400m_setup()
42 * i2400m_remove()
43 * i2400m_release()
44 * free_netdev(net_dev)
46 * i2400ms_bus_reset() Called by i2400m_reset
47 * __i2400ms_reset()
48 * __i2400ms_send_barker()
51 #include <linux/slab.h>
52 #include <linux/debugfs.h>
53 #include <linux/mmc/sdio_ids.h>
54 #include <linux/mmc/sdio.h>
55 #include <linux/mmc/sdio_func.h>
56 #include "i2400m-sdio.h"
57 #include <linux/wimax/i2400m.h>
59 #define D_SUBMODULE main
60 #include "sdio-debug-levels.h"
62 /* IOE WiMAX function timeout in seconds */
63 static int ioe_timeout = 2;
64 module_param(ioe_timeout, int, 0);
66 static char i2400ms_debug_params[128];
67 module_param_string(debug, i2400ms_debug_params, sizeof(i2400ms_debug_params),
68 0644);
69 MODULE_PARM_DESC(debug,
70 "String of space-separated NAME:VALUE pairs, where NAMEs "
71 "are the different debug submodules and VALUE are the "
72 "initial debug value to set.");
74 /* Our firmware file name list */
75 static const char *i2400ms_bus_fw_names[] = {
76 #define I2400MS_FW_FILE_NAME "i2400m-fw-sdio-1.3.sbcf"
77 I2400MS_FW_FILE_NAME,
78 NULL
82 static const struct i2400m_poke_table i2400ms_pokes[] = {
83 I2400M_FW_POKE(0x6BE260, 0x00000088),
84 I2400M_FW_POKE(0x080550, 0x00000005),
85 I2400M_FW_POKE(0xAE0000, 0x00000000),
86 I2400M_FW_POKE(0x000000, 0x00000000), /* MUST be 0 terminated or bad
87 * things will happen */
91 * Enable the SDIO function
93 * Tries to enable the SDIO function; might fail if it is still not
94 * ready (in some hardware, the SDIO WiMAX function is only enabled
95 * when we ask it to explicitly doing). Tries until a timeout is
96 * reached.
98 * The @maxtries argument indicates how many times (at most) it should
99 * be tried to enable the function. 0 means forever. This acts along
100 * with the timeout (ie: it'll stop trying as soon as the maximum
101 * number of tries is reached _or_ as soon as the timeout is reached).
103 * The reverse of this is...sdio_disable_function()
105 * Returns: 0 if the SDIO function was enabled, < 0 errno code on
106 * error (-ENODEV when it was unable to enable the function).
108 static
109 int i2400ms_enable_function(struct i2400ms *i2400ms, unsigned maxtries)
111 struct sdio_func *func = i2400ms->func;
112 u64 timeout;
113 int err;
114 struct device *dev = &func->dev;
115 unsigned tries = 0;
117 d_fnstart(3, dev, "(func %p)\n", func);
118 timeout = get_jiffies_64() + ioe_timeout * HZ;
119 err = -ENODEV;
120 while (err != 0 && time_before64(get_jiffies_64(), timeout)) {
121 sdio_claim_host(func);
122 if (i2400ms->iwmc3200)
123 func->enable_timeout = IWMC3200_IOR_TIMEOUT;
124 err = sdio_enable_func(func);
125 if (0 == err) {
126 sdio_release_host(func);
127 d_printf(2, dev, "SDIO function enabled\n");
128 goto function_enabled;
130 d_printf(2, dev, "SDIO function failed to enable: %d\n", err);
131 sdio_release_host(func);
132 if (maxtries > 0 && ++tries >= maxtries) {
133 err = -ETIME;
134 break;
136 msleep(I2400MS_INIT_SLEEP_INTERVAL);
138 /* If timed out, device is not there yet -- get -ENODEV so
139 * the device driver core will retry later on. */
140 if (err == -ETIME) {
141 dev_err(dev, "Can't enable WiMAX function; "
142 " has the function been enabled?\n");
143 err = -ENODEV;
145 function_enabled:
146 d_fnend(3, dev, "(func %p) = %d\n", func, err);
147 return err;
152 * Setup minimal device communication infrastructure needed to at
153 * least be able to update the firmware.
155 * Note the ugly trick: if we are in the probe path
156 * (i2400ms->debugfs_dentry == NULL), we only retry function
157 * enablement one, to avoid racing with the iwmc3200 top controller.
159 static
160 int i2400ms_bus_setup(struct i2400m *i2400m)
162 int result;
163 struct i2400ms *i2400ms =
164 container_of(i2400m, struct i2400ms, i2400m);
165 struct device *dev = i2400m_dev(i2400m);
166 struct sdio_func *func = i2400ms->func;
167 int retries;
169 sdio_claim_host(func);
170 result = sdio_set_block_size(func, I2400MS_BLK_SIZE);
171 sdio_release_host(func);
172 if (result < 0) {
173 dev_err(dev, "Failed to set block size: %d\n", result);
174 goto error_set_blk_size;
177 if (i2400ms->iwmc3200 && i2400ms->debugfs_dentry == NULL)
178 retries = 1;
179 else
180 retries = 0;
181 result = i2400ms_enable_function(i2400ms, retries);
182 if (result < 0) {
183 dev_err(dev, "Cannot enable SDIO function: %d\n", result);
184 goto error_func_enable;
187 result = i2400ms_tx_setup(i2400ms);
188 if (result < 0)
189 goto error_tx_setup;
190 result = i2400ms_rx_setup(i2400ms);
191 if (result < 0)
192 goto error_rx_setup;
193 return 0;
195 error_rx_setup:
196 i2400ms_tx_release(i2400ms);
197 error_tx_setup:
198 sdio_claim_host(func);
199 sdio_disable_func(func);
200 sdio_release_host(func);
201 error_func_enable:
202 error_set_blk_size:
203 return result;
208 * Tear down minimal device communication infrastructure needed to at
209 * least be able to update the firmware.
211 static
212 void i2400ms_bus_release(struct i2400m *i2400m)
214 struct i2400ms *i2400ms =
215 container_of(i2400m, struct i2400ms, i2400m);
216 struct sdio_func *func = i2400ms->func;
218 i2400ms_rx_release(i2400ms);
219 i2400ms_tx_release(i2400ms);
220 sdio_claim_host(func);
221 sdio_disable_func(func);
222 sdio_release_host(func);
227 * Setup driver resources needed to communicate with the device
229 * The fw needs some time to settle, and it was just uploaded,
230 * so give it a break first. I'd prefer to just wait for the device to
231 * send something, but seems the poking we do to enable SDIO stuff
232 * interferes with it, so just give it a break before starting...
234 static
235 int i2400ms_bus_dev_start(struct i2400m *i2400m)
237 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
238 struct sdio_func *func = i2400ms->func;
239 struct device *dev = &func->dev;
241 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
242 msleep(200);
243 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, 0);
244 return 0;
249 * Sends a barker buffer to the device
251 * This helper will allocate a kmalloced buffer and use it to transmit
252 * (then free it). Reason for this is that the SDIO host controller
253 * expects alignment (unknown exactly which) which the stack won't
254 * really provide and certain arches/host-controller combinations
255 * cannot use stack/vmalloc/text areas for DMA transfers.
257 static
258 int __i2400ms_send_barker(struct i2400ms *i2400ms,
259 const __le32 *barker, size_t barker_size)
261 int ret;
262 struct sdio_func *func = i2400ms->func;
263 struct device *dev = &func->dev;
264 void *buffer;
266 ret = -ENOMEM;
267 buffer = kmalloc(I2400MS_BLK_SIZE, GFP_KERNEL);
268 if (buffer == NULL)
269 goto error_kzalloc;
271 memcpy(buffer, barker, barker_size);
272 sdio_claim_host(func);
273 ret = sdio_memcpy_toio(func, 0, buffer, I2400MS_BLK_SIZE);
274 sdio_release_host(func);
276 if (ret < 0)
277 d_printf(0, dev, "E: barker error: %d\n", ret);
279 kfree(buffer);
280 error_kzalloc:
281 return ret;
285 static
286 int i2400ms_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
288 int result = 0;
289 struct i2400ms *i2400ms =
290 container_of(i2400m, struct i2400ms, i2400m);
291 struct device *dev = i2400m_dev(i2400m);
292 static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
293 cpu_to_le32(I2400M_WARM_RESET_BARKER),
294 cpu_to_le32(I2400M_WARM_RESET_BARKER),
295 cpu_to_le32(I2400M_WARM_RESET_BARKER),
296 cpu_to_le32(I2400M_WARM_RESET_BARKER),
298 static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
299 cpu_to_le32(I2400M_COLD_RESET_BARKER),
300 cpu_to_le32(I2400M_COLD_RESET_BARKER),
301 cpu_to_le32(I2400M_COLD_RESET_BARKER),
302 cpu_to_le32(I2400M_COLD_RESET_BARKER),
305 if (rt == I2400M_RT_WARM)
306 result = __i2400ms_send_barker(i2400ms, i2400m_WARM_BOOT_BARKER,
307 sizeof(i2400m_WARM_BOOT_BARKER));
308 else if (rt == I2400M_RT_COLD)
309 result = __i2400ms_send_barker(i2400ms, i2400m_COLD_BOOT_BARKER,
310 sizeof(i2400m_COLD_BOOT_BARKER));
311 else if (rt == I2400M_RT_BUS) {
312 do_bus_reset:
314 i2400ms_bus_release(i2400m);
316 /* Wait for the device to settle */
317 msleep(40);
319 result = i2400ms_bus_setup(i2400m);
320 } else
321 BUG();
322 if (result < 0 && rt != I2400M_RT_BUS) {
323 dev_err(dev, "%s reset failed (%d); trying SDIO reset\n",
324 rt == I2400M_RT_WARM ? "warm" : "cold", result);
325 rt = I2400M_RT_BUS;
326 goto do_bus_reset;
328 return result;
332 static
333 void i2400ms_netdev_setup(struct net_device *net_dev)
335 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
336 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
337 i2400ms_init(i2400ms);
338 i2400m_netdev_setup(net_dev);
343 * Debug levels control; see debug.h
345 struct d_level D_LEVEL[] = {
346 D_SUBMODULE_DEFINE(main),
347 D_SUBMODULE_DEFINE(tx),
348 D_SUBMODULE_DEFINE(rx),
349 D_SUBMODULE_DEFINE(fw),
351 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
354 #define __debugfs_register(prefix, name, parent) \
355 do { \
356 result = d_level_register_debugfs(prefix, name, parent); \
357 if (result < 0) \
358 goto error; \
359 } while (0)
362 static
363 int i2400ms_debugfs_add(struct i2400ms *i2400ms)
365 int result;
366 struct dentry *dentry = i2400ms->i2400m.wimax_dev.debugfs_dentry;
368 dentry = debugfs_create_dir("i2400m-sdio", dentry);
369 result = PTR_ERR(dentry);
370 if (IS_ERR(dentry)) {
371 if (result == -ENODEV)
372 result = 0; /* No debugfs support */
373 goto error;
375 i2400ms->debugfs_dentry = dentry;
376 __debugfs_register("dl_", main, dentry);
377 __debugfs_register("dl_", tx, dentry);
378 __debugfs_register("dl_", rx, dentry);
379 __debugfs_register("dl_", fw, dentry);
381 return 0;
383 error:
384 debugfs_remove_recursive(i2400ms->debugfs_dentry);
385 i2400ms->debugfs_dentry = NULL;
386 return result;
390 static struct device_type i2400ms_type = {
391 .name = "wimax",
395 * Probe a i2400m interface and register it
397 * @func: SDIO function
398 * @id: SDIO device ID
399 * @returns: 0 if ok, < 0 errno code on error.
401 * Alloc a net device, initialize the bus-specific details and then
402 * calls the bus-generic initialization routine. That will register
403 * the wimax and netdev devices, upload the firmware [using
404 * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
405 * communication with the device and then will start to talk to it to
406 * finnish setting it up.
408 * Initialization is tricky; some instances of the hw are packed with
409 * others in a way that requires a third driver that enables the WiMAX
410 * function. In those cases, we can't enable the SDIO function and
411 * we'll return with -ENODEV. When the driver that enables the WiMAX
412 * function does its thing, it has to do a bus_rescan_devices() on the
413 * SDIO bus so this driver is called again to enumerate the WiMAX
414 * function.
416 static
417 int i2400ms_probe(struct sdio_func *func,
418 const struct sdio_device_id *id)
420 int result;
421 struct net_device *net_dev;
422 struct device *dev = &func->dev;
423 struct i2400m *i2400m;
424 struct i2400ms *i2400ms;
426 /* Allocate instance [calls i2400m_netdev_setup() on it]. */
427 result = -ENOMEM;
428 net_dev = alloc_netdev(sizeof(*i2400ms), "wmx%d",
429 i2400ms_netdev_setup);
430 if (net_dev == NULL) {
431 dev_err(dev, "no memory for network device instance\n");
432 goto error_alloc_netdev;
434 SET_NETDEV_DEV(net_dev, dev);
435 SET_NETDEV_DEVTYPE(net_dev, &i2400ms_type);
436 i2400m = net_dev_to_i2400m(net_dev);
437 i2400ms = container_of(i2400m, struct i2400ms, i2400m);
438 i2400m->wimax_dev.net_dev = net_dev;
439 i2400ms->func = func;
440 sdio_set_drvdata(func, i2400ms);
442 i2400m->bus_tx_block_size = I2400MS_BLK_SIZE;
444 * Room required in the TX queue for SDIO message to accommodate
445 * a smallest payload while allocating header space is 224 bytes,
446 * which is the smallest message size(the block size 256 bytes)
447 * minus the smallest message header size(32 bytes).
449 i2400m->bus_tx_room_min = I2400MS_BLK_SIZE - I2400M_PL_ALIGN * 2;
450 i2400m->bus_pl_size_max = I2400MS_PL_SIZE_MAX;
451 i2400m->bus_setup = i2400ms_bus_setup;
452 i2400m->bus_dev_start = i2400ms_bus_dev_start;
453 i2400m->bus_dev_stop = NULL;
454 i2400m->bus_release = i2400ms_bus_release;
455 i2400m->bus_tx_kick = i2400ms_bus_tx_kick;
456 i2400m->bus_reset = i2400ms_bus_reset;
457 /* The iwmc3200-wimax sometimes requires the driver to try
458 * hard when we paint it into a corner. */
459 i2400m->bus_bm_retries = I2400M_SDIO_BOOT_RETRIES;
460 i2400m->bus_bm_cmd_send = i2400ms_bus_bm_cmd_send;
461 i2400m->bus_bm_wait_for_ack = i2400ms_bus_bm_wait_for_ack;
462 i2400m->bus_fw_names = i2400ms_bus_fw_names;
463 i2400m->bus_bm_mac_addr_impaired = 1;
464 i2400m->bus_bm_pokes_table = &i2400ms_pokes[0];
466 switch (func->device) {
467 case SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX:
468 case SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX_2G5:
469 i2400ms->iwmc3200 = 1;
470 break;
471 default:
472 i2400ms->iwmc3200 = 0;
475 result = i2400m_setup(i2400m, I2400M_BRI_NO_REBOOT);
476 if (result < 0) {
477 dev_err(dev, "cannot setup device: %d\n", result);
478 goto error_setup;
481 result = i2400ms_debugfs_add(i2400ms);
482 if (result < 0) {
483 dev_err(dev, "cannot create SDIO debugfs: %d\n",
484 result);
485 goto error_debugfs_add;
487 return 0;
489 error_debugfs_add:
490 i2400m_release(i2400m);
491 error_setup:
492 sdio_set_drvdata(func, NULL);
493 free_netdev(net_dev);
494 error_alloc_netdev:
495 return result;
499 static
500 void i2400ms_remove(struct sdio_func *func)
502 struct device *dev = &func->dev;
503 struct i2400ms *i2400ms = sdio_get_drvdata(func);
504 struct i2400m *i2400m = &i2400ms->i2400m;
505 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
507 d_fnstart(3, dev, "SDIO func %p\n", func);
508 debugfs_remove_recursive(i2400ms->debugfs_dentry);
509 i2400ms->debugfs_dentry = NULL;
510 i2400m_release(i2400m);
511 sdio_set_drvdata(func, NULL);
512 free_netdev(net_dev);
513 d_fnend(3, dev, "SDIO func %p\n", func);
516 static
517 const struct sdio_device_id i2400ms_sdio_ids[] = {
518 /* Intel: i2400m WiMAX (iwmc3200) over SDIO */
519 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL,
520 SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX) },
521 { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL,
522 SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX_2G5) },
523 { /* end: all zeroes */ },
525 MODULE_DEVICE_TABLE(sdio, i2400ms_sdio_ids);
528 static
529 struct sdio_driver i2400m_sdio_driver = {
530 .name = KBUILD_MODNAME,
531 .probe = i2400ms_probe,
532 .remove = i2400ms_remove,
533 .id_table = i2400ms_sdio_ids,
537 static
538 int __init i2400ms_driver_init(void)
540 d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400ms_debug_params,
541 "i2400m_sdio.debug");
542 return sdio_register_driver(&i2400m_sdio_driver);
544 module_init(i2400ms_driver_init);
547 static
548 void __exit i2400ms_driver_exit(void)
550 flush_scheduled_work(); /* for the stuff we schedule */
551 sdio_unregister_driver(&i2400m_sdio_driver);
553 module_exit(i2400ms_driver_exit);
556 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
557 MODULE_DESCRIPTION("Intel 2400M WiMAX networking for SDIO");
558 MODULE_LICENSE("GPL");
559 MODULE_FIRMWARE(I2400MS_FW_FILE_NAME);