bcmsdstd_linux.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /*
  2. * 'Standard' SDIO HOST CONTROLLER driver - linux portion
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: bcmsdstd_linux.c 700323 2017-05-18 16:12:11Z $
  30. */
  31. #include <linux/sched.h> /* request_irq() */
  32. #include <typedefs.h>
  33. #include <pcicfg.h>
  34. #include <bcmutils.h>
  35. #include <sdio.h> /* SDIO Device and Protocol Specs */
  36. #include <sdioh.h> /* SDIO Host Controller Spec header file */
  37. #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
  38. #include <sdiovar.h> /* to get msglevel bit values */
  39. #include <bcmsdstd.h>
  40. #include <bcmdevs.h>
  41. extern void* bcmsdh_probe(osl_t *osh, void *dev, void *sdioh, void *adapter_info, uint bus_type,
  42. uint bus_num, uint slot_num);
  43. extern int bcmsdh_remove(bcmsdh_info_t *bcmsdh);
  44. /* Extern functions for sdio power save */
  45. extern uint8 sdstd_turn_on_clock(sdioh_info_t *sd);
  46. extern uint8 sdstd_turn_off_clock(sdioh_info_t *sd);
  47. /* Extern variable for sdio power save. This is enabled or disabled using the IOCTL call */
  48. extern uint sd_3_power_save;
  49. struct sdos_info {
  50. sdioh_info_t *sd;
  51. spinlock_t lock;
  52. wait_queue_head_t intr_wait_queue;
  53. struct timer_list tuning_timer;
  54. int tuning_timer_exp;
  55. atomic_t timer_enab;
  56. struct tasklet_struct tuning_tasklet;
  57. };
  58. #define SDSTD_WAITBITS_TIMEOUT (5 * HZ) /* seconds * HZ */
  59. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
  60. #define BLOCKABLE() (!in_atomic())
  61. #else
  62. #define BLOCKABLE() (!in_interrupt())
  63. #endif // endif
  64. static void
  65. sdstd_3_ostasklet(ulong data);
  66. static void
  67. sdstd_3_tuning_timer(ulong data);
  68. /* Interrupt handler */
  69. static irqreturn_t
  70. sdstd_isr(int irq, void *dev_id
  71. #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
  72. , struct pt_regs *ptregs
  73. #endif // endif
  74. )
  75. {
  76. sdioh_info_t *sd;
  77. struct sdos_info *sdos;
  78. bool ours;
  79. unsigned long flags;
  80. sd = (sdioh_info_t *)dev_id;
  81. sdos = (struct sdos_info *)sd->sdos_info;
  82. if (!sd->card_init_done) {
  83. sd_err(("%s: Hey Bogus intr...not even initted: irq %d\n", __FUNCTION__, irq));
  84. return IRQ_RETVAL(FALSE);
  85. } else {
  86. if (sdstd_3_is_retuning_int_set(sd)) {
  87. /* for 3.0 host, retuning request might come in this path */
  88. /* * disable ISR's */
  89. local_irq_save(flags);
  90. if (sdstd_3_check_and_set_retuning(sd))
  91. tasklet_schedule(&sdos->tuning_tasklet);
  92. /* * enable back ISR's */
  93. local_irq_restore(flags);
  94. /* * disable tuning isr signaling */
  95. sdstd_3_disable_retuning_int(sd);
  96. /* * note: check_client_intr() checks for intmask also to
  97. wakeup. so be careful to use sd->intmask to disable
  98. re-tuning ISR.
  99. */
  100. }
  101. ours = check_client_intr(sd);
  102. /* For local interrupts, wake the waiting process */
  103. if (ours && sd->got_hcint) {
  104. sd_trace(("INTR->WAKE\n"));
  105. /* sdos = (struct sdos_info *)sd->sdos_info; */
  106. wake_up_interruptible(&sdos->intr_wait_queue);
  107. }
  108. return IRQ_RETVAL(ours);
  109. }
  110. }
  111. /* Register with Linux for interrupts */
  112. int
  113. sdstd_register_irq(sdioh_info_t *sd, uint irq)
  114. {
  115. sd_trace(("Entering %s: irq == %d\n", __FUNCTION__, irq));
  116. if (request_irq(irq, sdstd_isr, IRQF_SHARED, "bcmsdstd", sd) < 0) {
  117. sd_err(("%s: request_irq() failed\n", __FUNCTION__));
  118. return ERROR;
  119. }
  120. return SUCCESS;
  121. }
  122. /* Free Linux irq */
  123. void
  124. sdstd_free_irq(uint irq, sdioh_info_t *sd)
  125. {
  126. free_irq(irq, sd);
  127. }
  128. /* Map Host controller registers */
  129. uint32 *
  130. sdstd_reg_map(osl_t *osh, ulong addr, int size)
  131. {
  132. return (uint32 *)REG_MAP(addr, size);
  133. }
  134. void
  135. sdstd_reg_unmap(osl_t *osh, ulong addr, int size)
  136. {
  137. REG_UNMAP((void*)(uintptr)addr);
  138. }
  139. int
  140. sdstd_osinit(sdioh_info_t *sd)
  141. {
  142. struct sdos_info *sdos;
  143. sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
  144. sd->sdos_info = (void*)sdos;
  145. if (sdos == NULL)
  146. return BCME_NOMEM;
  147. sdos->sd = sd;
  148. spin_lock_init(&sdos->lock);
  149. atomic_set(&sdos->timer_enab, FALSE);
  150. init_waitqueue_head(&sdos->intr_wait_queue);
  151. return BCME_OK;
  152. }
  153. /* initilize tuning related OS structures */
  154. void
  155. sdstd_3_osinit_tuning(sdioh_info_t *sd)
  156. {
  157. struct sdos_info *sdos = (struct sdos_info *)sd->sdos_info;
  158. uint8 timer_count = sdstd_3_get_tuning_exp(sdos->sd);
  159. sd_trace(("%s Enter\n", __FUNCTION__));
  160. /* initialize timer and tasklet for tuning */
  161. init_timer(&sdos->tuning_timer);
  162. sdos->tuning_timer.data = (ulong)sdos;
  163. sdos->tuning_timer.function = sdstd_3_tuning_timer;
  164. if (timer_count == CAP3_RETUNING_TC_DISABLED || timer_count > CAP3_RETUNING_TC_1024S) {
  165. sdos->tuning_timer_exp = 0;
  166. } else {
  167. sdos->tuning_timer_exp = 1 << (timer_count - 1);
  168. }
  169. tasklet_init(&sdos->tuning_tasklet, sdstd_3_ostasklet, (ulong)sdos);
  170. if (sdos->tuning_timer_exp) {
  171. sdos->tuning_timer.expires = jiffies + sdos->tuning_timer_exp * HZ;
  172. add_timer(&sdos->tuning_timer);
  173. atomic_set(&sdos->timer_enab, TRUE);
  174. }
  175. }
  176. /* finalize tuning related OS structures */
  177. void
  178. sdstd_3_osclean_tuning(sdioh_info_t *sd)
  179. {
  180. struct sdos_info *sdos = (struct sdos_info *)sd->sdos_info;
  181. if (atomic_read(&sdos->timer_enab) == TRUE) {
  182. /* disable timer if it was running */
  183. del_timer_sync(&sdos->tuning_timer);
  184. atomic_set(&sdos->timer_enab, FALSE);
  185. }
  186. tasklet_kill(&sdos->tuning_tasklet);
  187. }
  188. static void
  189. sdstd_3_ostasklet(ulong data)
  190. {
  191. struct sdos_info *sdos = (struct sdos_info *)data;
  192. int tune_state = sdstd_3_get_tune_state(sdos->sd);
  193. int data_state = sdstd_3_get_data_state(sdos->sd);
  194. if ((tune_state == TUNING_START) || (tune_state == TUNING_ONGOING) ||
  195. (tune_state == TUNING_START_AFTER_DAT)) {
  196. return;
  197. }
  198. else if (data_state == DATA_TRANSFER_IDLE)
  199. sdstd_3_set_tune_state(sdos->sd, TUNING_START);
  200. else if (data_state == DATA_TRANSFER_ONGOING)
  201. sdstd_3_set_tune_state(sdos->sd, TUNING_START_AFTER_DAT);
  202. }
  203. static void
  204. sdstd_3_tuning_timer(ulong data)
  205. {
  206. struct sdos_info *sdos = (struct sdos_info *)data;
  207. /* uint8 timeout = 0; */
  208. unsigned long int_flags;
  209. sd_trace(("%s: enter\n", __FUNCTION__));
  210. /* schedule tasklet */
  211. /* * disable ISR's */
  212. local_irq_save(int_flags);
  213. if (sdstd_3_check_and_set_retuning(sdos->sd))
  214. tasklet_schedule(&sdos->tuning_tasklet);
  215. /* * enable back ISR's */
  216. local_irq_restore(int_flags);
  217. }
  218. void sdstd_3_start_tuning(sdioh_info_t *sd)
  219. {
  220. int tune_state;
  221. unsigned long int_flags = 0;
  222. unsigned int timer_enab;
  223. struct sdos_info *sdos = (struct sdos_info *)sd->sdos_info;
  224. sd_trace(("%s: enter\n", __FUNCTION__));
  225. /* * disable ISR's */
  226. local_irq_save(int_flags);
  227. timer_enab = atomic_read(&sdos->timer_enab);
  228. tune_state = sdstd_3_get_tune_state(sd);
  229. if (tune_state == TUNING_ONGOING) {
  230. /* do nothing */
  231. local_irq_restore(int_flags);
  232. goto exit;
  233. }
  234. /* change state */
  235. sdstd_3_set_tune_state(sd, TUNING_ONGOING);
  236. /* * enable ISR's */
  237. local_irq_restore(int_flags);
  238. sdstd_3_clk_tuning(sd, sdstd_3_get_uhsi_clkmode(sd));
  239. #ifdef BCMSDIOH_STD_TUNING_WAR
  240. /*
  241. * Observed intermittent SDIO command error after re-tuning done
  242. * successfully. Re-tuning twice is giving much reliable results.
  243. */
  244. sdstd_3_clk_tuning(sd, sdstd_3_get_uhsi_clkmode(sd));
  245. #endif /* BCMSDIOH_STD_TUNING_WAR */
  246. /* * disable ISR's */
  247. local_irq_save(int_flags);
  248. sdstd_3_set_tune_state(sd, TUNING_IDLE);
  249. /* * enable ISR's */
  250. local_irq_restore(int_flags);
  251. /* enable retuning intrrupt */
  252. sdstd_3_enable_retuning_int(sd);
  253. /* start retuning timer if enabled */
  254. if ((sdos->tuning_timer_exp) && (timer_enab)) {
  255. if (sd->sd3_tuning_reqd) {
  256. sdos->tuning_timer.expires = jiffies + sdos->tuning_timer_exp * HZ;
  257. mod_timer(&sdos->tuning_timer, sdos->tuning_timer.expires);
  258. }
  259. }
  260. exit:
  261. return;
  262. }
  263. void
  264. sdstd_osfree(sdioh_info_t *sd)
  265. {
  266. struct sdos_info *sdos;
  267. ASSERT(sd && sd->sdos_info);
  268. sdos = (struct sdos_info *)sd->sdos_info;
  269. MFREE(sd->osh, sdos, sizeof(struct sdos_info));
  270. }
  271. /* Interrupt enable/disable */
  272. SDIOH_API_RC
  273. sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
  274. {
  275. ulong flags;
  276. struct sdos_info *sdos;
  277. sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
  278. sdos = (struct sdos_info *)sd->sdos_info;
  279. ASSERT(sdos);
  280. if (!(sd->host_init_done && sd->card_init_done)) {
  281. sd_err(("%s: Card & Host are not initted - bailing\n", __FUNCTION__));
  282. return SDIOH_API_RC_FAIL;
  283. }
  284. if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
  285. sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
  286. return SDIOH_API_RC_FAIL;
  287. }
  288. /* Ensure atomicity for enable/disable calls */
  289. spin_lock_irqsave(&sdos->lock, flags);
  290. sd->client_intr_enabled = enable;
  291. if (enable && !sd->lockcount)
  292. sdstd_devintr_on(sd);
  293. else
  294. sdstd_devintr_off(sd);
  295. spin_unlock_irqrestore(&sdos->lock, flags);
  296. return SDIOH_API_RC_SUCCESS;
  297. }
  298. /* Protect against reentrancy (disable device interrupts while executing) */
  299. void
  300. sdstd_lock(sdioh_info_t *sd)
  301. {
  302. ulong flags;
  303. struct sdos_info *sdos;
  304. int wait_count = 0;
  305. sdos = (struct sdos_info *)sd->sdos_info;
  306. ASSERT(sdos);
  307. sd_trace(("%s: %d\n", __FUNCTION__, sd->lockcount));
  308. spin_lock_irqsave(&sdos->lock, flags);
  309. while (sd->lockcount)
  310. {
  311. spin_unlock_irqrestore(&sdos->lock, flags);
  312. yield();
  313. spin_lock_irqsave(&sdos->lock, flags);
  314. if (++wait_count == 25000) {
  315. if (!(sd->lockcount == 0)) {
  316. sd_err(("%s: ERROR: sd->lockcount == 0\n", __FUNCTION__));
  317. }
  318. }
  319. }
  320. if (wait_count)
  321. printk("sdstd_lock: wait count = %d\n", wait_count);
  322. sdstd_devintr_off(sd);
  323. sd->lockcount++;
  324. spin_unlock_irqrestore(&sdos->lock, flags);
  325. if ((sd->controller_type == SDIOH_TYPE_RICOH_R5C822) && (sd->version == HOST_CONTR_VER_3))
  326. sdstd_turn_on_clock(sd);
  327. }
  328. /* Enable client interrupt */
  329. void
  330. sdstd_unlock(sdioh_info_t *sd)
  331. {
  332. ulong flags;
  333. struct sdos_info *sdos;
  334. sd_trace(("%s: %d, %d\n", __FUNCTION__, sd->lockcount, sd->client_intr_enabled));
  335. ASSERT(sd->lockcount > 0);
  336. sdos = (struct sdos_info *)sd->sdos_info;
  337. ASSERT(sdos);
  338. spin_lock_irqsave(&sdos->lock, flags);
  339. if (--sd->lockcount == 0 && sd->client_intr_enabled) {
  340. sdstd_devintr_on(sd);
  341. }
  342. spin_unlock_irqrestore(&sdos->lock, flags);
  343. if (sd_3_power_save)
  344. {
  345. if ((sd->controller_type == SDIOH_TYPE_RICOH_R5C822) &&
  346. (sd->version == HOST_CONTR_VER_3))
  347. sdstd_turn_off_clock(sd);
  348. }
  349. }
  350. void
  351. sdstd_os_lock_irqsave(sdioh_info_t *sd, ulong* flags)
  352. {
  353. struct sdos_info *sdos = (struct sdos_info *)sd->sdos_info;
  354. spin_lock_irqsave(&sdos->lock, *flags);
  355. }
  356. void
  357. sdstd_os_unlock_irqrestore(sdioh_info_t *sd, ulong* flags)
  358. {
  359. struct sdos_info *sdos = (struct sdos_info *)sd->sdos_info;
  360. spin_unlock_irqrestore(&sdos->lock, *flags);
  361. }
  362. void
  363. sdstd_waitlockfree(sdioh_info_t *sd)
  364. {
  365. if (sd->lockcount) {
  366. printk("wait lock free\n");
  367. while (sd->lockcount)
  368. {
  369. yield();
  370. }
  371. }
  372. }
  373. /* Returns 0 for success, -1 for interrupted, -2 for timeout */
  374. int
  375. sdstd_waitbits(sdioh_info_t *sd, uint16 norm, uint16 err, bool local_yield, uint16 *bits)
  376. {
  377. struct sdos_info *sdos;
  378. int rc = 0;
  379. sdos = (struct sdos_info *)sd->sdos_info;
  380. #ifndef BCMSDYIELD
  381. ASSERT(!local_yield);
  382. #endif // endif
  383. sd_trace(("%s: int 0x%02x err 0x%02x yield %d canblock %d\n",
  384. __FUNCTION__, norm, err, local_yield, BLOCKABLE()));
  385. /* Clear the "interrupt happened" flag and last intrstatus */
  386. sd->got_hcint = FALSE;
  387. sd->last_intrstatus = 0;
  388. #ifdef BCMSDYIELD
  389. if (local_yield && BLOCKABLE()) {
  390. /* Enable interrupts, wait for the indication, then disable */
  391. sdstd_intrs_on(sd, norm, err);
  392. rc = wait_event_interruptible_timeout(sdos->intr_wait_queue,
  393. (sd->got_hcint),
  394. SDSTD_WAITBITS_TIMEOUT);
  395. if (rc < 0)
  396. rc = -1; /* interrupted */
  397. else if (rc == 0)
  398. rc = -2; /* timeout */
  399. else
  400. rc = 0; /* success */
  401. sdstd_intrs_off(sd, norm, err);
  402. } else
  403. #endif /* BCMSDYIELD */
  404. {
  405. sdstd_spinbits(sd, norm, err);
  406. }
  407. sd_trace(("%s: last_intrstatus 0x%04x\n", __FUNCTION__, sd->last_intrstatus));
  408. *bits = sd->last_intrstatus;
  409. return rc;
  410. }
  411. #ifdef DHD_DEBUG
  412. void sdstd_enable_disable_periodic_timer(sdioh_info_t *sd, uint val)
  413. {
  414. struct sdos_info *sdos = (struct sdos_info *)sd->sdos_info;
  415. if (val == SD_DHD_ENABLE_PERIODIC_TUNING) {
  416. /* start of tuning timer */
  417. sdos->tuning_timer.expires = jiffies + sdos->tuning_timer_exp * HZ;
  418. mod_timer(&sdos->tuning_timer, sdos->tuning_timer.expires);
  419. }
  420. if (val == SD_DHD_DISABLE_PERIODIC_TUNING) {
  421. /* stop periodic timer */
  422. del_timer_sync(&sdos->tuning_timer);
  423. }
  424. }
  425. #endif /* debugging purpose */
  426. /* forward declarations for PCI probe and remove functions. */
  427. static int __devinit bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
  428. static void __devexit bcmsdh_pci_remove(struct pci_dev *pdev);
  429. /**
  430. * pci id table
  431. */
  432. static struct pci_device_id bcmsdh_pci_devid[] __devinitdata = {
  433. { vendor: PCI_ANY_ID,
  434. device: PCI_ANY_ID,
  435. subvendor: PCI_ANY_ID,
  436. subdevice: PCI_ANY_ID,
  437. class: 0,
  438. class_mask: 0,
  439. driver_data: 0,
  440. },
  441. { 0, 0, 0, 0, 0, 0, 0}
  442. };
  443. MODULE_DEVICE_TABLE(pci, bcmsdh_pci_devid);
  444. /**
  445. * SDIO Host Controller pci driver info
  446. */
  447. static struct pci_driver bcmsdh_pci_driver = {
  448. node: {&(bcmsdh_pci_driver.node), &(bcmsdh_pci_driver.node)},
  449. name: "bcmsdh",
  450. id_table: bcmsdh_pci_devid,
  451. probe: bcmsdh_pci_probe,
  452. remove: bcmsdh_pci_remove,
  453. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0))
  454. save_state: NULL,
  455. #endif // endif
  456. suspend: NULL,
  457. resume: NULL,
  458. };
  459. extern uint sd_pci_slot; /* Force detection to a particular PCI */
  460. /* slot only . Allows for having multiple */
  461. /* WL devices at once in a PC */
  462. /* Only one instance of dhd will be */
  463. /* usable at a time */
  464. /* Upper word is bus number, */
  465. /* lower word is slot number */
  466. /* Default value of 0xffffffff turns this */
  467. /* off */
  468. module_param(sd_pci_slot, uint, 0);
  469. /**
  470. * Detect supported SDIO Host Controller and attach if found.
  471. *
  472. * Determine if the device described by pdev is a supported SDIO Host
  473. * Controller. If so, attach to it and attach to the target device.
  474. */
  475. static int __devinit
  476. bcmsdh_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  477. {
  478. osl_t *osh = NULL;
  479. sdioh_info_t *sdioh = NULL;
  480. int rc;
  481. if (sd_pci_slot != 0xFFFFffff) {
  482. if (pdev->bus->number != (sd_pci_slot>>16) ||
  483. PCI_SLOT(pdev->devfn) != (sd_pci_slot&0xffff)) {
  484. sd_err(("%s: %s: bus %X, slot %X, vend %X, dev %X\n",
  485. __FUNCTION__,
  486. bcmsdh_chipmatch(pdev->vendor, pdev->device)
  487. ?"Found compatible SDIOHC"
  488. :"Probing unknown device",
  489. pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor,
  490. pdev->device));
  491. return -ENODEV;
  492. }
  493. sd_err(("%s: %s: bus %X, slot %X, vendor %X, device %X (good PCI location)\n",
  494. __FUNCTION__,
  495. bcmsdh_chipmatch(pdev->vendor, pdev->device)
  496. ?"Using compatible SDIOHC"
  497. :"WARNING, forced use of unkown device",
  498. pdev->bus->number, PCI_SLOT(pdev->devfn), pdev->vendor, pdev->device));
  499. }
  500. if ((pdev->vendor == VENDOR_TI) && ((pdev->device == PCIXX21_FLASHMEDIA_ID) ||
  501. (pdev->device == PCIXX21_FLASHMEDIA0_ID))) {
  502. uint32 config_reg;
  503. sd_err(("%s: Disabling TI FlashMedia Controller.\n", __FUNCTION__));
  504. if (!(osh = osl_attach(pdev, SDIO_BUS, TRUE))) {
  505. sd_err(("%s: osl_attach failed\n", __FUNCTION__));
  506. goto err;
  507. }
  508. config_reg = OSL_PCI_READ_CONFIG(osh, 0x4c, 4);
  509. /*
  510. * Set MMC_SD_DIS bit in FlashMedia Controller.
  511. * Disbling the SD/MMC Controller in the FlashMedia Controller
  512. * allows the Standard SD Host Controller to take over control
  513. * of the SD Slot.
  514. */
  515. config_reg |= 0x02;
  516. OSL_PCI_WRITE_CONFIG(osh, 0x4c, 4, config_reg);
  517. osl_detach(osh);
  518. }
  519. /* match this pci device with what we support */
  520. /* we can't solely rely on this to believe it is our SDIO Host Controller! */
  521. if (!bcmsdh_chipmatch(pdev->vendor, pdev->device)) {
  522. if (pdev->vendor == VENDOR_BROADCOM) {
  523. sd_err(("%s: Unknown Broadcom device (vendor: %#x, device: %#x).\n",
  524. __FUNCTION__, pdev->vendor, pdev->device));
  525. }
  526. return -ENODEV;
  527. }
  528. /* this is a pci device we might support */
  529. sd_err(("%s: Found possible SDIO Host Controller: bus %d slot %d func %d irq %d\n",
  530. __FUNCTION__,
  531. pdev->bus->number, PCI_SLOT(pdev->devfn),
  532. PCI_FUNC(pdev->devfn), pdev->irq));
  533. /* use bcmsdh_query_device() to get the vendor ID of the target device so
  534. * it will eventually appear in the Broadcom string on the console
  535. */
  536. /* allocate SDIO Host Controller state info */
  537. if (!(osh = osl_attach(pdev, SDIO_BUS, TRUE))) {
  538. sd_err(("%s: osl_attach failed\n", __FUNCTION__));
  539. goto err;
  540. }
  541. /* map to address where host can access */
  542. pci_set_master(pdev);
  543. rc = pci_enable_device(pdev);
  544. if (rc) {
  545. sd_err(("%s: Cannot enable PCI device\n", __FUNCTION__));
  546. goto err;
  547. }
  548. sdioh = sdioh_attach(osh, (void *)(ulong)pci_resource_start(pdev, 0), pdev->irq);
  549. if (sdioh == NULL) {
  550. sd_err(("%s: sdioh_attach failed\n", __FUNCTION__));
  551. goto err;
  552. }
  553. sdioh->bcmsdh = bcmsdh_probe(osh, &pdev->dev, sdioh, NULL, PCI_BUS, -1, -1);
  554. if (sdioh->bcmsdh == NULL) {
  555. sd_err(("%s: bcmsdh_probe failed\n", __FUNCTION__));
  556. goto err;
  557. }
  558. pci_set_drvdata(pdev, sdioh);
  559. return 0;
  560. err:
  561. if (sdioh != NULL)
  562. sdioh_detach(osh, sdioh);
  563. if (osh != NULL)
  564. osl_detach(osh);
  565. return -ENOMEM;
  566. }
  567. /**
  568. * Detach from target devices and SDIO Host Controller
  569. */
  570. static void __devexit
  571. bcmsdh_pci_remove(struct pci_dev *pdev)
  572. {
  573. sdioh_info_t *sdioh;
  574. osl_t *osh;
  575. sdioh = pci_get_drvdata(pdev);
  576. if (sdioh == NULL) {
  577. sd_err(("%s: error, no sdioh handler found\n", __FUNCTION__));
  578. return;
  579. }
  580. osh = sdioh->osh;
  581. bcmsdh_remove(sdioh->bcmsdh);
  582. sdioh_detach(osh, sdioh);
  583. osl_detach(osh);
  584. }
  585. int bcmsdh_register_client_driver(void)
  586. {
  587. return pci_module_init(&bcmsdh_pci_driver);
  588. }
  589. void bcmsdh_unregister_client_driver(void)
  590. {
  591. pci_unregister_driver(&bcmsdh_pci_driver);
  592. }