blob: 74de174a4c443d6daa172915c43f0cb1aa10293b [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
Dirk Brandewie1c0b2dd2009-05-21 11:56:35 -070081static 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
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -080089/*
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 reverse of this is...sdio_disable_function()
98 *
99 * Returns: 0 if the SDIO function was enabled, < 0 errno code on
100 * error (-ENODEV when it was unable to enable the function).
101 */
102static
103int i2400ms_enable_function(struct sdio_func *func)
104{
105 u64 timeout;
106 int err;
107 struct device *dev = &func->dev;
108
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
112 * it is ready */
113 timeout = get_jiffies_64() + ioe_timeout * HZ;
114 err = -ENODEV;
115 while (err != 0 && time_before64(get_jiffies_64(), timeout)) {
116 sdio_claim_host(func);
117 err = sdio_enable_func(func);
118 if (0 == err) {
119 sdio_release_host(func);
120 d_printf(2, dev, "SDIO function enabled\n");
121 goto function_enabled;
122 }
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);
127 }
128 /* If timed out, device is not there yet -- get -ENODEV so
129 * the device driver core will retry later on. */
130 if (err == -ETIME) {
131 dev_err(dev, "Can't enable WiMAX function; "
132 " has the function been enabled?\n");
133 err = -ENODEV;
134 }
135function_enabled:
136 d_fnend(3, dev, "(func %p) = %d\n", func, err);
137 return err;
138}
139
140
141/*
142 * Setup driver resources needed to communicate with the device
143 *
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...
148 */
149static
150int i2400ms_bus_dev_start(struct i2400m *i2400m)
151{
152 int result;
153 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
154 struct sdio_func *func = i2400ms->func;
155 struct device *dev = &func->dev;
156
157 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
158 msleep(200);
159 result = i2400ms_rx_setup(i2400ms);
160 if (result < 0)
161 goto error_rx_setup;
162 result = i2400ms_tx_setup(i2400ms);
163 if (result < 0)
164 goto error_tx_setup;
165 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
166 return result;
167
168 i2400ms_tx_release(i2400ms);
169error_tx_setup:
170 i2400ms_rx_release(i2400ms);
171error_rx_setup:
172 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
173 return result;
174}
175
176
177static
178void i2400ms_bus_dev_stop(struct i2400m *i2400m)
179{
180 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
181 struct sdio_func *func = i2400ms->func;
182 struct device *dev = &func->dev;
183
184 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
185 i2400ms_rx_release(i2400ms);
186 i2400ms_tx_release(i2400ms);
187 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
188}
189
190
191/*
192 * Sends a barker buffer to the device
193 *
194 * This helper will allocate a kmalloced buffer and use it to transmit
195 * (then free it). Reason for this is that the SDIO host controller
196 * expects alignment (unknown exactly which) which the stack won't
197 * really provide and certain arches/host-controller combinations
198 * cannot use stack/vmalloc/text areas for DMA transfers.
199 */
200static
201int __i2400ms_send_barker(struct i2400ms *i2400ms,
202 const __le32 *barker, size_t barker_size)
203{
204 int ret;
205 struct sdio_func *func = i2400ms->func;
206 struct device *dev = &func->dev;
207 void *buffer;
208
209 ret = -ENOMEM;
210 buffer = kmalloc(I2400MS_BLK_SIZE, GFP_KERNEL);
211 if (buffer == NULL)
212 goto error_kzalloc;
213
214 memcpy(buffer, barker, barker_size);
215 sdio_claim_host(func);
216 ret = sdio_memcpy_toio(func, 0, buffer, I2400MS_BLK_SIZE);
217 sdio_release_host(func);
218
219 if (ret < 0)
220 d_printf(0, dev, "E: barker error: %d\n", ret);
221
222 kfree(buffer);
223error_kzalloc:
224 return ret;
225}
226
227
228/*
229 * Reset a device at different levels (warm, cold or bus)
230 *
231 * @i2400ms: device descriptor
232 * @reset_type: soft, warm or bus reset (I2400M_RT_WARM/SOFT/BUS)
233 *
234 * FIXME: not tested -- need to confirm expected effects
235 *
236 * Warm and cold resets get an SDIO reset if they fail (unimplemented)
237 *
238 * Warm reset:
239 *
240 * The device will be fully reset internally, but won't be
241 * disconnected from the USB bus (so no reenumeration will
242 * happen). Firmware upload will be neccessary.
243 *
244 * The device will send a reboot barker in the notification endpoint
245 * that will trigger the driver to reinitialize the state
246 * automatically from notif.c:i2400m_notification_grok() into
247 * i2400m_dev_bootstrap_delayed().
248 *
249 * Cold and bus (USB) reset:
250 *
251 * The device will be fully reset internally, disconnected from the
252 * USB bus an a reenumeration will happen. Firmware upload will be
253 * neccessary. Thus, we don't do any locking or struct
254 * reinitialization, as we are going to be fully disconnected and
255 * reenumerated.
256 *
257 * Note we need to return -ENODEV if a warm reset was requested and we
258 * had to resort to a bus reset. See i2400m_op_reset(), wimax_reset()
259 * and wimax_dev->op_reset.
260 *
261 * WARNING: no driver state saved/fixed
262 */
263static
264int i2400ms_bus_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
265{
Dirk Brandewie10b1de62009-05-12 07:54:00 -0700266 int result = 0;
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800267 struct i2400ms *i2400ms =
268 container_of(i2400m, struct i2400ms, i2400m);
269 struct device *dev = i2400m_dev(i2400m);
270 static const __le32 i2400m_WARM_BOOT_BARKER[4] = {
Harvey Harrisonee437772009-02-01 00:43:54 -0800271 cpu_to_le32(I2400M_WARM_RESET_BARKER),
272 cpu_to_le32(I2400M_WARM_RESET_BARKER),
273 cpu_to_le32(I2400M_WARM_RESET_BARKER),
274 cpu_to_le32(I2400M_WARM_RESET_BARKER),
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800275 };
276 static const __le32 i2400m_COLD_BOOT_BARKER[4] = {
Harvey Harrisonee437772009-02-01 00:43:54 -0800277 cpu_to_le32(I2400M_COLD_RESET_BARKER),
278 cpu_to_le32(I2400M_COLD_RESET_BARKER),
279 cpu_to_le32(I2400M_COLD_RESET_BARKER),
280 cpu_to_le32(I2400M_COLD_RESET_BARKER),
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800281 };
282
283 if (rt == I2400M_RT_WARM)
284 result = __i2400ms_send_barker(i2400ms, i2400m_WARM_BOOT_BARKER,
285 sizeof(i2400m_WARM_BOOT_BARKER));
286 else if (rt == I2400M_RT_COLD)
287 result = __i2400ms_send_barker(i2400ms, i2400m_COLD_BOOT_BARKER,
288 sizeof(i2400m_COLD_BOOT_BARKER));
289 else if (rt == I2400M_RT_BUS) {
290do_bus_reset:
Dirk Brandewie10b1de62009-05-12 07:54:00 -0700291 /* call netif_tx_disable() before sending IOE disable,
292 * so that all the tx from network layer are stopped
293 * while IOE is being reset. Make sure it is called
294 * only after register_netdev() was issued.
295 */
296 if (i2400m->wimax_dev.net_dev->reg_state == NETREG_REGISTERED)
297 netif_tx_disable(i2400m->wimax_dev.net_dev);
298
299 sdio_claim_host(i2400ms->func);
300 sdio_disable_func(i2400ms->func);
301 sdio_release_host(i2400ms->func);
302
303 /* Wait for the device to settle */
304 msleep(40);
305
306 result = i2400ms_enable_function(i2400ms->func);
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800307 } else
308 BUG();
309 if (result < 0 && rt != I2400M_RT_BUS) {
310 dev_err(dev, "%s reset failed (%d); trying SDIO reset\n",
311 rt == I2400M_RT_WARM ? "warm" : "cold", result);
312 rt = I2400M_RT_BUS;
313 goto do_bus_reset;
314 }
315 return result;
316}
317
318
319static
320void i2400ms_netdev_setup(struct net_device *net_dev)
321{
322 struct i2400m *i2400m = net_dev_to_i2400m(net_dev);
323 struct i2400ms *i2400ms = container_of(i2400m, struct i2400ms, i2400m);
324 i2400ms_init(i2400ms);
325 i2400m_netdev_setup(net_dev);
326}
327
328
329/*
330 * Debug levels control; see debug.h
331 */
332struct d_level D_LEVEL[] = {
333 D_SUBMODULE_DEFINE(main),
334 D_SUBMODULE_DEFINE(tx),
335 D_SUBMODULE_DEFINE(rx),
336 D_SUBMODULE_DEFINE(fw),
337};
338size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
339
340
341#define __debugfs_register(prefix, name, parent) \
342do { \
343 result = d_level_register_debugfs(prefix, name, parent); \
344 if (result < 0) \
345 goto error; \
346} while (0)
347
348
349static
350int i2400ms_debugfs_add(struct i2400ms *i2400ms)
351{
352 int result;
353 struct dentry *dentry = i2400ms->i2400m.wimax_dev.debugfs_dentry;
354
355 dentry = debugfs_create_dir("i2400m-usb", dentry);
356 result = PTR_ERR(dentry);
357 if (IS_ERR(dentry)) {
358 if (result == -ENODEV)
359 result = 0; /* No debugfs support */
360 goto error;
361 }
362 i2400ms->debugfs_dentry = dentry;
363 __debugfs_register("dl_", main, dentry);
364 __debugfs_register("dl_", tx, dentry);
365 __debugfs_register("dl_", rx, dentry);
366 __debugfs_register("dl_", fw, dentry);
367
368 return 0;
369
370error:
371 debugfs_remove_recursive(i2400ms->debugfs_dentry);
372 return result;
373}
374
375
376/*
377 * Probe a i2400m interface and register it
378 *
379 * @func: SDIO function
380 * @id: SDIO device ID
381 * @returns: 0 if ok, < 0 errno code on error.
382 *
383 * Alloc a net device, initialize the bus-specific details and then
384 * calls the bus-generic initialization routine. That will register
385 * the wimax and netdev devices, upload the firmware [using
386 * _bus_bm_*()], call _bus_dev_start() to finalize the setup of the
387 * communication with the device and then will start to talk to it to
388 * finnish setting it up.
389 *
390 * Initialization is tricky; some instances of the hw are packed with
391 * others in a way that requires a third driver that enables the WiMAX
392 * function. In those cases, we can't enable the SDIO function and
393 * we'll return with -ENODEV. When the driver that enables the WiMAX
394 * function does its thing, it has to do a bus_rescan_devices() on the
395 * SDIO bus so this driver is called again to enumerate the WiMAX
396 * function.
397 */
398static
399int i2400ms_probe(struct sdio_func *func,
400 const struct sdio_device_id *id)
401{
402 int result;
403 struct net_device *net_dev;
404 struct device *dev = &func->dev;
405 struct i2400m *i2400m;
406 struct i2400ms *i2400ms;
407
408 /* Allocate instance [calls i2400m_netdev_setup() on it]. */
409 result = -ENOMEM;
410 net_dev = alloc_netdev(sizeof(*i2400ms), "wmx%d",
411 i2400ms_netdev_setup);
412 if (net_dev == NULL) {
413 dev_err(dev, "no memory for network device instance\n");
414 goto error_alloc_netdev;
415 }
416 SET_NETDEV_DEV(net_dev, dev);
417 i2400m = net_dev_to_i2400m(net_dev);
418 i2400ms = container_of(i2400m, struct i2400ms, i2400m);
419 i2400m->wimax_dev.net_dev = net_dev;
420 i2400ms->func = func;
421 sdio_set_drvdata(func, i2400ms);
422
423 i2400m->bus_tx_block_size = I2400MS_BLK_SIZE;
424 i2400m->bus_pl_size_max = I2400MS_PL_SIZE_MAX;
425 i2400m->bus_dev_start = i2400ms_bus_dev_start;
426 i2400m->bus_dev_stop = i2400ms_bus_dev_stop;
427 i2400m->bus_tx_kick = i2400ms_bus_tx_kick;
428 i2400m->bus_reset = i2400ms_bus_reset;
Inaky Perez-Gonzalezecddfd52009-06-03 16:13:14 +0800429 /* The iwmc3200-wimax sometimes requires the driver to try
430 * hard when we paint it into a corner. */
431 i2400m->bus_bm_retries = I3200_BOOT_RETRIES;
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800432 i2400m->bus_bm_cmd_send = i2400ms_bus_bm_cmd_send;
433 i2400m->bus_bm_wait_for_ack = i2400ms_bus_bm_wait_for_ack;
Inaky Perez-Gonzalez1039abb2009-02-28 23:42:47 +0000434 i2400m->bus_fw_names = i2400ms_bus_fw_names;
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800435 i2400m->bus_bm_mac_addr_impaired = 1;
Dirk Brandewie1c0b2dd2009-05-21 11:56:35 -0700436 i2400m->bus_bm_pokes_table = &i2400ms_pokes[0];
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800437
Inaky Perez-Gonzaleza0a4c4c2009-04-30 14:39:21 -0700438 sdio_claim_host(func);
439 result = sdio_set_block_size(func, I2400MS_BLK_SIZE);
440 sdio_release_host(func);
441 if (result < 0) {
442 dev_err(dev, "Failed to set block size: %d\n", result);
443 goto error_set_blk_size;
444 }
445
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800446 result = i2400ms_enable_function(i2400ms->func);
447 if (result < 0) {
448 dev_err(dev, "Cannot enable SDIO function: %d\n", result);
449 goto error_func_enable;
450 }
451
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800452 result = i2400m_setup(i2400m, I2400M_BRI_NO_REBOOT);
453 if (result < 0) {
454 dev_err(dev, "cannot setup device: %d\n", result);
455 goto error_setup;
456 }
457
458 result = i2400ms_debugfs_add(i2400ms);
459 if (result < 0) {
460 dev_err(dev, "cannot create SDIO debugfs: %d\n",
461 result);
462 goto error_debugfs_add;
463 }
464 return 0;
465
466error_debugfs_add:
467 i2400m_release(i2400m);
468error_setup:
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800469 sdio_claim_host(func);
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800470 sdio_disable_func(func);
471 sdio_release_host(func);
472error_func_enable:
Inaky Perez-Gonzaleza0a4c4c2009-04-30 14:39:21 -0700473error_set_blk_size:
474 sdio_set_drvdata(func, NULL);
Inaky Perez-Gonzaleza0848822008-12-20 16:57:55 -0800475 free_netdev(net_dev);
476error_alloc_netdev:
477 return result;
478}
479
480
481static
482void i2400ms_remove(struct sdio_func *func)
483{
484 struct device *dev = &func->dev;
485 struct i2400ms *i2400ms = sdio_get_drvdata(func);
486 struct i2400m *i2400m = &i2400ms->i2400m;
487 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
488
489 d_fnstart(3, dev, "SDIO func %p\n", func);
490 debugfs_remove_recursive(i2400ms->debugfs_dentry);
491 i2400m_release(i2400m);
492 sdio_set_drvdata(func, NULL);
493 sdio_claim_host(func);
494 sdio_disable_func(func);
495 sdio_release_host(func);
496 free_netdev(net_dev);
497 d_fnend(3, dev, "SDIO func %p\n", func);
498}
499
500enum {
501 I2400MS_INTEL_VID = 0x89,
502};
503
504static
505const struct sdio_device_id i2400ms_sdio_ids[] = {
506 /* Intel: i2400m WiMAX over SDIO */
507 { SDIO_DEVICE(I2400MS_INTEL_VID, 0x1402) },
508 { }, /* end: all zeroes */
509};
510MODULE_DEVICE_TABLE(sdio, i2400ms_sdio_ids);
511
512
513static
514struct sdio_driver i2400m_sdio_driver = {
515 .name = KBUILD_MODNAME,
516 .probe = i2400ms_probe,
517 .remove = i2400ms_remove,
518 .id_table = i2400ms_sdio_ids,
519};
520
521
522static
523int __init i2400ms_driver_init(void)
524{
525 return sdio_register_driver(&i2400m_sdio_driver);
526}
527module_init(i2400ms_driver_init);
528
529
530static
531void __exit i2400ms_driver_exit(void)
532{
533 flush_scheduled_work(); /* for the stuff we schedule */
534 sdio_unregister_driver(&i2400m_sdio_driver);
535}
536module_exit(i2400ms_driver_exit);
537
538
539MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
540MODULE_DESCRIPTION("Intel 2400M WiMAX networking for SDIO");
541MODULE_LICENSE("GPL");
542MODULE_FIRMWARE(I2400MS_FW_FILE_NAME);