sdio.c 16 KB

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