76a50ac02ebb2c2ffa9ff294890a4c9480f81dfc
[pandora-kernel.git] / drivers / net / wimax / i2400m / sdio.c
1 /*
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_reset
47  *   __i2400ms_reset()
48  *     __i2400ms_send_barker()
49  */
50
51 #include <linux/debugfs.h>
52 #include <linux/mmc/sdio_ids.h>
53 #include <linux/mmc/sdio.h>
54 #include <linux/mmc/sdio_func.h>
55 #include "i2400m-sdio.h"
56 #include <linux/wimax/i2400m.h>
57
58 #define D_SUBMODULE main
59 #include "sdio-debug-levels.h"
60
61 /* IOE WiMAX function timeout in seconds */
62 static int ioe_timeout = 2;
63 module_param(ioe_timeout, int, 0);
64
65 static char i2400ms_debug_params[128];
66 module_param_string(debug, i2400ms_debug_params, sizeof(i2400ms_debug_params),
67                     0644);
68 MODULE_PARM_DESC(debug,
69                  "String of space-separated NAME:VALUE pairs, where NAMEs "
70                  "are the different debug submodules and VALUE are the "
71                  "initial debug value to set.");
72
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"
76         I2400MS_FW_FILE_NAME,
77         NULL
78 };
79
80
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 */
87 };
88
89 /*
90  * Enable the SDIO function
91  *
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
95  * reached.
96  *
97  * The @maxtries argument indicates how many times (at most) it should
98  * be tried to enable the function. 0 means forever. This acts along
99  * with the timeout (ie: it'll stop trying as soon as the maximum
100  * number of tries is reached _or_ as soon as the timeout is reached).
101  *
102  * The reverse of this is...sdio_disable_function()
103  *
104  * Returns: 0 if the SDIO function was enabled, < 0 errno code on
105  *     error (-ENODEV when it was unable to enable the function).
106  */
107 static
108 int i2400ms_enable_function(struct i2400ms *i2400ms, unsigned maxtries)
109 {
110         struct sdio_func *func = i2400ms->func;
111         u64 timeout;
112         int err;
113         struct device *dev = &func->dev;
114         unsigned tries = 0;
115
116         d_fnstart(3, dev, "(func %p)\n", func);
117         /* Setup timeout (FIXME: This needs to read the CIS table to
118          * get a real timeout) and then wait for the device to signal
119          * it is ready */
120         timeout = get_jiffies_64() + ioe_timeout * HZ;
121         err = -ENODEV;
122         while (err != 0 && time_before64(get_jiffies_64(), timeout)) {
123                 sdio_claim_host(func);
124                 /*
125                  * There is a sillicon bug on the IWMC3200, where the
126                  * IOE timeout will cause problems on Moorestown
127                  * platforms (system hang). We explicitly overwrite
128                  * func->enable_timeout here to work around the issue.
129                  */
130                 if (i2400ms->iwmc3200)
131                         func->enable_timeout = IWMC3200_IOR_TIMEOUT;
132                 err = sdio_enable_func(func);
133                 if (0 == err) {
134                         sdio_release_host(func);
135                         d_printf(2, dev, "SDIO function enabled\n");
136                         goto function_enabled;
137                 }
138                 d_printf(2, dev, "SDIO function failed to enable: %d\n", err);
139                 sdio_release_host(func);
140                 if (maxtries > 0 && ++tries >= maxtries) {
141                         err = -ETIME;
142                         break;
143                 }
144                 msleep(I2400MS_INIT_SLEEP_INTERVAL);
145         }
146         /* If timed out, device is not there yet -- get -ENODEV so
147          * the device driver core will retry later on. */
148         if (err == -ETIME) {
149                 dev_err(dev, "Can't enable WiMAX function; "
150                         " has the function been enabled?\n");
151                 err = -ENODEV;
152         }
153 function_enabled:
154         d_fnend(3, dev, "(func %p) = %d\n", func, err);
155         return err;
156 }
157
158
159 /*
160  * Setup minimal device communication infrastructure needed to at
161  * least be able to update the firmware.
162  *
163  * Note the ugly trick: if we are in the probe path
164  * (i2400ms->debugfs_dentry == NULL), we only retry function
165  * enablement one, to avoid racing with the iwmc3200 top controller.
166  */
167 static
168 int i2400ms_bus_setup(struct i2400m *i2400m)
169 {
170         int result;
171         struct i2400ms *i2400ms =
172                 container_of(i2400m, struct i2400ms, i2400m);
173         struct device *dev = i2400m_dev(i2400m);
174         struct sdio_func *func = i2400ms->func;
175         int retries;
176
177         sdio_claim_host(func);
178         result = sdio_set_block_size(func, I2400MS_BLK_SIZE);
179         sdio_release_host(func);
180         if (result < 0) {
181                 dev_err(dev, "Failed to set block size: %d\n", result);
182                 goto error_set_blk_size;
183         }
184
185         if (i2400ms->iwmc3200 && i2400ms->debugfs_dentry == NULL)
186                 retries = 1;
187         else
188                 retries = 0;
189         result = i2400ms_enable_function(i2400ms, retries);
190         if (result < 0) {
191                 dev_err(dev, "Cannot enable SDIO function: %d\n", result);
192                 goto error_func_enable;
193         }
194
195         result = i2400ms_tx_setup(i2400ms);
196         if (result < 0)
197                 goto error_tx_setup;
198         result = i2400ms_rx_setup(i2400ms);
199         if (result < 0)
200                 goto error_rx_setup;
201         return 0;
202
203 error_rx_setup:
204         i2400ms_tx_release(i2400ms);
205 error_tx_setup:
206         sdio_claim_host(func);
207         sdio_disable_func(func);
208         sdio_release_host(func);
209 error_func_enable:
210 error_set_blk_size:
211         return result;
212 }
213
214
215 /*
216  * Tear down minimal device communication infrastructure needed to at
217  * least be able to update the firmware.
218  */
219 static
220 void i2400ms_bus_release(struct i2400m *i2400m)
221 {
222         struct i2400ms *i2400ms =
223                 container_of(i2400m, struct i2400ms, i2400m);
224         struct sdio_func *func = i2400ms->func;
225
226         i2400ms_rx_release(i2400ms);
227         i2400ms_tx_release(i2400ms);
228         sdio_claim_host(func);
229         sdio_disable_func(func);
230         sdio_release_host(func);
231 }
232
233
234 /*
235  * Setup driver resources needed to communicate with the device
236  *
237  * The fw needs some time to settle, and it was just uploaded,
238  * so give it a break first. I'd prefer to just wait for the device to
239  * send something, but seems the poking we do to enable SDIO stuff
240  * interferes with it, so just give it a break before starting...
241  */
242 static
243 int i2400ms_bus_dev_start(struct i2400m *i2400m)
244 {
245         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
246         struct sdio_func *func = i2400ms->func;
247         struct device *dev = &func->dev;
248
249         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
250         msleep(200);
251         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, 0);
252         return 0;
253 }
254
255
256 /*
257  * Sends a barker buffer to the device
258  *
259  * This helper will allocate a kmalloced buffer and use it to transmit
260  * (then free it). Reason for this is that the SDIO host controller
261  * expects alignment (unknown exactly which) which the stack won't
262  * really provide and certain arches/host-controller combinations
263  * cannot use stack/vmalloc/text areas for DMA transfers.
264  */
265 static
266 int __i2400ms_send_barker(struct i2400ms *i2400ms,
267                           const __le32 *barker, size_t barker_size)
268 {
269         int  ret;
270         struct sdio_func *func = i2400ms->func;
271         struct device *dev = &func->dev;
272         void *buffer;
273
274         ret = -ENOMEM;
275         buffer = kmalloc(I2400MS_BLK_SIZE, GFP_KERNEL);
276         if (buffer == NULL)
277                 goto error_kzalloc;
278
279         memcpy(buffer, barker, barker_size);
280         sdio_claim_host(func);
281         ret = sdio_memcpy_toio(func, 0, buffer, I2400MS_BLK_SIZE);
282         sdio_release_host(func);
283
284         if (ret < 0)
285                 d_printf(0, dev, "E: barker error: %d\n", ret);
286
287         kfree(buffer);
288 error_kzalloc:
289         return ret;
290 }
291
292
293 /*
294  * Reset a device at different levels (warm, cold or bus)
295  *
296  * @i2400ms: device descriptor
297  * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
298  *
299  * FIXME: not tested -- need to confirm expected effects
300  *
301  * Warm and cold resets get an SDIO reset if they fail (unimplemented)
302  *
303  * Warm reset:
304  *
305  * The device will be fully reset internally, but won't be
306  * disconnected from the bus (so no reenumeration will
307  * happen). Firmware upload will be neccessary.
308  *
309  * The device will send a reboot barker that will trigger the driver
310  * to reinitialize the state via __i2400m_dev_reset_handle.
311  *
312  *
313  * Cold and bus reset:
314  *
315  * The device will be fully reset internally, disconnected from the
316  * bus an a reenumeration will happen. Firmware upload will be
317  * neccessary. Thus, we don't do any locking or struct
318  * reinitialization, as we are going to be fully disconnected and
319  * reenumerated.
320  *
321  * Note we need to return -ENODEV if a warm reset was requested and we
322  * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
323  * and wimax_dev->op_reset.
324  *
325  * WARNING: no driver state saved/fixed
326  */
327 static
328 int i2400ms_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
329 {
330         int result = 0;
331         struct i2400ms *i2400ms =
332                 container_of(i2400m, struct i2400ms, i2400m);
333         struct device *dev = i2400m_dev(i2400m);
334         static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
335                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
336                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
337                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
338                 cpu_to_le32(I2400M_WARM_RESET_BARKER),
339         };
340         static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
341                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
342                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
343                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
344                 cpu_to_le32(I2400M_COLD_RESET_BARKER),
345         };
346
347         if (rt == I2400M_RT_WARM)
348                 result = __i2400ms_send_barker(i2400ms, i2400m_WARM_BOOT_BARKER,
349                                                sizeof(i2400m_WARM_BOOT_BARKER));
350         else if (rt == I2400M_RT_COLD)
351                 result = __i2400ms_send_barker(i2400ms, i2400m_COLD_BOOT_BARKER,
352                                                sizeof(i2400m_COLD_BOOT_BARKER));
353         else if (rt == I2400M_RT_BUS) {
354 do_bus_reset:
355
356                 i2400ms_bus_release(i2400m);
357
358                 /* Wait for the device to settle */
359                 msleep(40);
360
361                 result =  i2400ms_bus_setup(i2400m);
362         } else
363                 BUG();
364         if (result < 0 && rt != I2400M_RT_BUS) {
365                 dev_err(dev, "%s reset failed (%d); trying SDIO reset\n",
366                         rt == I2400M_RT_WARM ? "warm" : "cold", result);
367                 rt = I2400M_RT_BUS;
368                 goto do_bus_reset;
369         }
370         return result;
371 }
372
373
374 static
375 void i2400ms_netdev_setup(struct net_device *net_dev)
376 {
377         struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
378         struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
379         i2400ms_init(i2400ms);
380         i2400m_netdev_setup(net_dev);
381 }
382
383
384 /*
385  * Debug levels control; see debug.h
386  */
387 struct d_level D_LEVEL[] = {
388         D_SUBMODULE_DEFINE(main),
389         D_SUBMODULE_DEFINE(tx),
390         D_SUBMODULE_DEFINE(rx),
391         D_SUBMODULE_DEFINE(fw),
392 };
393 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
394
395
396 #define __debugfs_register(prefix, name, parent)                        \
397 do {                                                                    \
398         result = d_level_register_debugfs(prefix, name, parent);        \
399         if (result < 0)                                                 \
400                 goto error;                                             \
401 } while (0)
402
403
404 static
405 int i2400ms_debugfs_add(struct i2400ms *i2400ms)
406 {
407         int result;
408         struct dentry *dentry = i2400ms->i2400m.wimax_dev.debugfs_dentry;
409
410         dentry = debugfs_create_dir("i2400m-sdio", dentry);
411         result = PTR_ERR(dentry);
412         if (IS_ERR(dentry)) {
413                 if (result == -ENODEV)
414                         result = 0;     /* No debugfs support */
415                 goto error;
416         }
417         i2400ms->debugfs_dentry = dentry;
418         __debugfs_register("dl_", main, dentry);
419         __debugfs_register("dl_", tx, dentry);
420         __debugfs_register("dl_", rx, dentry);
421         __debugfs_register("dl_", fw, dentry);
422
423         return 0;
424
425 error:
426         debugfs_remove_recursive(i2400ms->debugfs_dentry);
427         i2400ms->debugfs_dentry = NULL;
428         return result;
429 }
430
431
432 static struct device_type i2400ms_type = {
433         .name   = "wimax",
434 };
435
436 /*
437  * Probe a i2400m interface and register it
438  *
439  * @func:    SDIO function
440  * @id:      SDIO device ID
441  * @returns: 0 if ok, < 0 errno code on error.
442  *
443  * Alloc a net device, initialize the bus-specific details and then
444  * calls the bus-generic initialization routine. That will register
445  * the wimax and netdev devices, upload the firmware [using
446  * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
447  * communication with the device and then will start to talk to it to
448  * finnish setting it up.
449  *
450  * Initialization is tricky; some instances of the hw are packed with
451  * others in a way that requires a third driver that enables the WiMAX
452  * function. In those cases, we can't enable the SDIO function and
453  * we'll return with -ENODEV. When the driver that enables the WiMAX
454  * function does its thing, it has to do a bus_rescan_devices() on the
455  * SDIO bus so this driver is called again to enumerate the WiMAX
456  * function.
457  */
458 static
459 int i2400ms_probe(struct sdio_func *func,
460                   const struct sdio_device_id *id)
461 {
462         int result;
463         struct net_device *net_dev;
464         struct device *dev = &func->dev;
465         struct i2400m *i2400m;
466         struct i2400ms *i2400ms;
467
468         /* Allocate instance [calls i2400m_netdev_setup() on it]. */
469         result = -ENOMEM;
470         net_dev = alloc_netdev(sizeof(*i2400ms), "wmx%d",
471                                i2400ms_netdev_setup);
472         if (net_dev == NULL) {
473                 dev_err(dev, "no memory for network device instance\n");
474                 goto error_alloc_netdev;
475         }
476         SET_NETDEV_DEV(net_dev, dev);
477         SET_NETDEV_DEVTYPE(net_dev, &i2400ms_type);
478         i2400m = net_dev_to_i2400m(net_dev);
479         i2400ms = container_of(i2400m, struct i2400ms, i2400m);
480         i2400m->wimax_dev.net_dev = net_dev;
481         i2400ms->func = func;
482         sdio_set_drvdata(func, i2400ms);
483
484         i2400m->bus_tx_block_size = I2400MS_BLK_SIZE;
485         i2400m->bus_pl_size_max = I2400MS_PL_SIZE_MAX;
486         i2400m->bus_setup = i2400ms_bus_setup;
487         i2400m->bus_dev_start = i2400ms_bus_dev_start;
488         i2400m->bus_dev_stop = NULL;
489         i2400m->bus_release = i2400ms_bus_release;
490         i2400m->bus_tx_kick = i2400ms_bus_tx_kick;
491         i2400m->bus_reset = i2400ms_bus_reset;
492         /* The iwmc3200-wimax sometimes requires the driver to try
493          * hard when we paint it into a corner. */
494         i2400m->bus_bm_retries = I2400M_SDIO_BOOT_RETRIES;
495         i2400m->bus_bm_cmd_send = i2400ms_bus_bm_cmd_send;
496         i2400m->bus_bm_wait_for_ack = i2400ms_bus_bm_wait_for_ack;
497         i2400m->bus_fw_names = i2400ms_bus_fw_names;
498         i2400m->bus_bm_mac_addr_impaired = 1;
499         i2400m->bus_bm_pokes_table = &i2400ms_pokes[0];
500
501         switch (func->device) {
502         case SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX:
503         case SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX_2G5:
504                 i2400ms->iwmc3200 = 1;
505                 break;
506         default:
507                 i2400ms->iwmc3200 = 0;
508         }
509
510         result = i2400m_setup(i2400m, I2400M_BRI_NO_REBOOT);
511         if (result < 0) {
512                 dev_err(dev, "cannot setup device: %d\n", result);
513                 goto error_setup;
514         }
515
516         result = i2400ms_debugfs_add(i2400ms);
517         if (result < 0) {
518                 dev_err(dev, "cannot create SDIO debugfs: %d\n",
519                         result);
520                 goto error_debugfs_add;
521         }
522         return 0;
523
524 error_debugfs_add:
525         i2400m_release(i2400m);
526 error_setup:
527         sdio_set_drvdata(func, NULL);
528         free_netdev(net_dev);
529 error_alloc_netdev:
530         return result;
531 }
532
533
534 static
535 void i2400ms_remove(struct sdio_func *func)
536 {
537         struct device *dev = &func->dev;
538         struct i2400ms *i2400ms = sdio_get_drvdata(func);
539         struct i2400m *i2400m = &i2400ms->i2400m;
540         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
541
542         d_fnstart(3, dev, "SDIO func %p\n", func);
543         debugfs_remove_recursive(i2400ms->debugfs_dentry);
544         i2400ms->debugfs_dentry = NULL;
545         i2400m_release(i2400m);
546         sdio_set_drvdata(func, NULL);
547         free_netdev(net_dev);
548         d_fnend(3, dev, "SDIO func %p\n", func);
549 }
550
551 static
552 const struct sdio_device_id i2400ms_sdio_ids[] = {
553         /* Intel: i2400m WiMAX (iwmc3200) over SDIO */
554         { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL,
555                       SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX) },
556         { SDIO_DEVICE(SDIO_VENDOR_ID_INTEL,
557                       SDIO_DEVICE_ID_INTEL_IWMC3200WIMAX_2G5) },
558         { /* end: all zeroes */ },
559 };
560 MODULE_DEVICE_TABLE(sdio, i2400ms_sdio_ids);
561
562
563 static
564 struct sdio_driver i2400m_sdio_driver = {
565         .name           = KBUILD_MODNAME,
566         .probe          = i2400ms_probe,
567         .remove         = i2400ms_remove,
568         .id_table       = i2400ms_sdio_ids,
569 };
570
571
572 static
573 int __init i2400ms_driver_init(void)
574 {
575         d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400ms_debug_params,
576                        "i2400m_sdio.debug");
577         return sdio_register_driver(&i2400m_sdio_driver);
578 }
579 module_init(i2400ms_driver_init);
580
581
582 static
583 void __exit i2400ms_driver_exit(void)
584 {
585         flush_scheduled_work(); /* for the stuff we schedule */
586         sdio_unregister_driver(&i2400m_sdio_driver);
587 }
588 module_exit(i2400ms_driver_exit);
589
590
591 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
592 MODULE_DESCRIPTION("Intel 2400M WiMAX networking for SDIO");
593 MODULE_LICENSE("GPL");
594 MODULE_FIRMWARE(I2400MS_FW_FILE_NAME);