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