2 * 'Standard' SDIO HOST CONTROLLER driver - linux portion
4 * Copyright (C) 1999-2010, Broadcom Corporation
6 * Unless you and Broadcom execute a separate written software license
7 * agreement governing use of this software, this software is licensed to you
8 * under the terms of the GNU General Public License version 2 (the "GPL"),
9 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
10 * following added to such license:
12 * As a special exception, the copyright holders of this software give you
13 * permission to link this software with independent modules, and to copy and
14 * distribute the resulting executable under terms of your choice, provided that
15 * you also meet, for each linked independent module, the terms and conditions of
16 * the license of that module. An independent module is a module which is not
17 * derived from this software. The special exception does not apply to any
18 * modifications of the software.
20 * Notwithstanding the above, under no circumstances may you combine this
21 * software in any way with any other Broadcom software provided under a license
22 * other than the GPL, without Broadcom's express prior written consent.
24 * $Id: bcmsdstd_linux.c,v 1.11.18.2.16.1 2010/08/17 17:03:13 Exp $
30 #include <sdio.h> /* SDIO Specs */
31 #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
32 #include <sdiovar.h> /* to get msglevel bit values */
34 #include <linux/sched.h> /* request_irq() */
41 wait_queue_head_t intr_wait_queue;
44 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
45 #define BLOCKABLE() (!in_atomic())
47 #define BLOCKABLE() (!in_interrupt())
50 /* Interrupt handler */
52 sdstd_isr(int irq, void *dev_id
53 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
54 , struct pt_regs *ptregs
59 struct sdos_info *sdos;
62 sd = (sdioh_info_t *)dev_id;
64 if (!sd->card_init_done) {
65 sd_err(("%s: Hey Bogus intr...not even initted: irq %d\n", __FUNCTION__, irq));
66 return IRQ_RETVAL(FALSE);
68 ours = check_client_intr(sd);
70 /* For local interrupts, wake the waiting process */
71 if (ours && sd->got_hcint) {
72 sd_trace(("INTR->WAKE\n"));
73 sdos = (struct sdos_info *)sd->sdos_info;
74 wake_up_interruptible(&sdos->intr_wait_queue);
76 return IRQ_RETVAL(ours);
80 /* Register with Linux for interrupts */
82 sdstd_register_irq(sdioh_info_t *sd, uint irq)
84 sd_trace(("Entering %s: irq == %d\n", __FUNCTION__, irq));
85 if (request_irq(irq, sdstd_isr, IRQF_SHARED, "bcmsdstd", sd) < 0) {
86 sd_err(("%s: request_irq() failed\n", __FUNCTION__));
94 sdstd_free_irq(uint irq, sdioh_info_t *sd)
99 /* Map Host controller registers */
102 sdstd_reg_map(osl_t *osh, int32 addr, int size)
104 return (uint32 *)REG_MAP(addr, size);
108 sdstd_reg_unmap(osl_t *osh, int32 addr, int size)
110 REG_UNMAP((void*)(uintptr)addr);
114 sdstd_osinit(sdioh_info_t *sd)
116 struct sdos_info *sdos;
118 sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
119 sd->sdos_info = (void*)sdos;
124 spin_lock_init(&sdos->lock);
125 init_waitqueue_head(&sdos->intr_wait_queue);
130 sdstd_osfree(sdioh_info_t *sd)
132 struct sdos_info *sdos;
133 ASSERT(sd && sd->sdos_info);
135 sdos = (struct sdos_info *)sd->sdos_info;
136 MFREE(sd->osh, sdos, sizeof(struct sdos_info));
139 /* Interrupt enable/disable */
141 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
144 struct sdos_info *sdos;
146 sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
148 sdos = (struct sdos_info *)sd->sdos_info;
151 if (!(sd->host_init_done && sd->card_init_done)) {
152 sd_err(("%s: Card & Host are not initted - bailing\n", __FUNCTION__));
153 return SDIOH_API_RC_FAIL;
156 if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
157 sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
158 return SDIOH_API_RC_FAIL;
161 /* Ensure atomicity for enable/disable calls */
162 spin_lock_irqsave(&sdos->lock, flags);
164 sd->client_intr_enabled = enable;
165 if (enable && !sd->lockcount)
166 sdstd_devintr_on(sd);
168 sdstd_devintr_off(sd);
170 spin_unlock_irqrestore(&sdos->lock, flags);
172 return SDIOH_API_RC_SUCCESS;
175 /* Protect against reentrancy (disable device interrupts while executing) */
177 sdstd_lock(sdioh_info_t *sd)
180 struct sdos_info *sdos;
182 sdos = (struct sdos_info *)sd->sdos_info;
185 sd_trace(("%s: %d\n", __FUNCTION__, sd->lockcount));
187 spin_lock_irqsave(&sdos->lock, flags);
189 sd_err(("%s: Already locked! called from %p\n",
191 __builtin_return_address(0)));
192 ASSERT(sd->lockcount == 0);
194 sdstd_devintr_off(sd);
196 spin_unlock_irqrestore(&sdos->lock, flags);
199 /* Enable client interrupt */
201 sdstd_unlock(sdioh_info_t *sd)
204 struct sdos_info *sdos;
206 sd_trace(("%s: %d, %d\n", __FUNCTION__, sd->lockcount, sd->client_intr_enabled));
207 ASSERT(sd->lockcount > 0);
209 sdos = (struct sdos_info *)sd->sdos_info;
212 spin_lock_irqsave(&sdos->lock, flags);
213 if (--sd->lockcount == 0 && sd->client_intr_enabled) {
214 sdstd_devintr_on(sd);
216 spin_unlock_irqrestore(&sdos->lock, flags);
220 sdstd_waitbits(sdioh_info_t *sd, uint16 norm, uint16 err, bool yield)
222 struct sdos_info *sdos;
224 sdos = (struct sdos_info *)sd->sdos_info;
229 sd_trace(("%s: int 0x%02x err 0x%02x yield %d canblock %d\n",
230 __FUNCTION__, norm, err, yield, BLOCKABLE()));
232 /* Clear the "interrupt happened" flag and last intrstatus */
233 sd->got_hcint = FALSE;
234 sd->last_intrstatus = 0;
237 if (yield && BLOCKABLE()) {
238 /* Enable interrupts, wait for the indication, then disable */
239 sdstd_intrs_on(sd, norm, err);
240 wait_event_interruptible(sdos->intr_wait_queue, (sd->got_hcint));
241 sdstd_intrs_off(sd, norm, err);
243 #endif /* BCMSDYIELD */
245 sdstd_spinbits(sd, norm, err);
248 sd_trace(("%s: last_intrstatus 0x%04x\n", __FUNCTION__, sd->last_intrstatus));
250 return sd->last_intrstatus;