sdio.c 15 KB

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