sdio.c 16 KB

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