• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Broadcom SPI Host Controller Driver - Linux Per-port
3  *
4  * Copyright (C) 1999-2017, Broadcom Corporation
5  *
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:
11  *
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.
19  *
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.
23  *
24  *
25  * <<Broadcom-WL-IPTag/Open:>>
26  *
27  * $Id: bcmsdspi_linux.c 514727 2014-11-12 03:02:48Z $
28  */
29 
30 #include <typedefs.h>
31 #include <bcmutils.h>
32 
33 #include <bcmsdbus.h>        /* bcmsdh to/from specific controller APIs */
34 #include <sdiovar.h>        /* to get msglevel bit values */
35 
36 #include <pcicfg.h>
37 #include <sdio.h>        /* SDIO Device and Protocol Specs */
38 #include <linux/sched.h>    /* request_irq(), free_irq() */
39 #include <bcmsdspi.h>
40 #include <bcmspi.h>
41 
42 extern uint sd_crc;
43 module_param(sd_crc, uint, 0);
44 
45 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
46 #define KERNEL26
47 #endif
48 
49 struct sdos_info {
50     sdioh_info_t *sd;
51     spinlock_t lock;
52     wait_queue_head_t intr_wait_queue;
53 };
54 
55 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
56 #define BLOCKABLE()    (!in_atomic())
57 #else
58 #define BLOCKABLE()    (!in_interrupt())
59 #endif
60 
61 /* Interrupt handler */
62 static irqreturn_t
sdspi_isr(int irq,void * dev_id,struct pt_regs * ptregs)63 sdspi_isr(int irq, void *dev_id
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
65 , struct pt_regs *ptregs
66 #endif
67 )
68 {
69     sdioh_info_t *sd;
70     struct sdos_info *sdos;
71     bool ours;
72 
73     sd = (sdioh_info_t *)dev_id;
74     sd->local_intrcount++;
75 
76     if (!sd->card_init_done) {
77         sd_err(("%s: Hey Bogus intr...not even initted: irq %d\n", __FUNCTION__, irq));
78         return IRQ_RETVAL(FALSE);
79     } else {
80         ours = spi_check_client_intr(sd, NULL);
81 
82         /* For local interrupts, wake the waiting process */
83         if (ours && sd->got_hcint) {
84             sdos = (struct sdos_info *)sd->sdos_info;
85             wake_up_interruptible(&sdos->intr_wait_queue);
86         }
87 
88         return IRQ_RETVAL(ours);
89     }
90 }
91 
92 
93 /* Register with Linux for interrupts */
94 int
spi_register_irq(sdioh_info_t * sd,uint irq)95 spi_register_irq(sdioh_info_t *sd, uint irq)
96 {
97     sd_trace(("Entering %s: irq == %d\n", __FUNCTION__, irq));
98     if (request_irq(irq, sdspi_isr, IRQF_SHARED, "bcmsdspi", sd) < 0) {
99         sd_err(("%s: request_irq() failed\n", __FUNCTION__));
100         return ERROR;
101     }
102     return SUCCESS;
103 }
104 
105 /* Free Linux irq */
106 void
spi_free_irq(uint irq,sdioh_info_t * sd)107 spi_free_irq(uint irq, sdioh_info_t *sd)
108 {
109     free_irq(irq, sd);
110 }
111 
112 /* Map Host controller registers */
113 uint32 *
spi_reg_map(osl_t * osh,uintptr addr,int size)114 spi_reg_map(osl_t *osh, uintptr addr, int size)
115 {
116     return (uint32 *)REG_MAP(addr, size);
117 }
118 
119 void
spi_reg_unmap(osl_t * osh,uintptr addr,int size)120 spi_reg_unmap(osl_t *osh, uintptr addr, int size)
121 {
122     REG_UNMAP((void*)(uintptr)addr);
123 }
124 
125 int
spi_osinit(sdioh_info_t * sd)126 spi_osinit(sdioh_info_t *sd)
127 {
128     struct sdos_info *sdos;
129 
130     sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
131     sd->sdos_info = (void*)sdos;
132     if (sdos == NULL)
133         return BCME_NOMEM;
134 
135     sdos->sd = sd;
136     spin_lock_init(&sdos->lock);
137     init_waitqueue_head(&sdos->intr_wait_queue);
138     return BCME_OK;
139 }
140 
141 void
spi_osfree(sdioh_info_t * sd)142 spi_osfree(sdioh_info_t *sd)
143 {
144     struct sdos_info *sdos;
145     ASSERT(sd && sd->sdos_info);
146 
147     sdos = (struct sdos_info *)sd->sdos_info;
148     MFREE(sd->osh, sdos, sizeof(struct sdos_info));
149 }
150 
151 /* Interrupt enable/disable */
152 SDIOH_API_RC
sdioh_interrupt_set(sdioh_info_t * sd,bool enable)153 sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
154 {
155     ulong flags;
156     struct sdos_info *sdos;
157 
158     sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
159 
160     sdos = (struct sdos_info *)sd->sdos_info;
161     ASSERT(sdos);
162 
163     if (!(sd->host_init_done && sd->card_init_done)) {
164         sd_err(("%s: Card & Host are not initted - bailing\n", __FUNCTION__));
165         return SDIOH_API_RC_FAIL;
166     }
167 
168     if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
169         sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
170         return SDIOH_API_RC_FAIL;
171     }
172 
173     /* Ensure atomicity for enable/disable calls */
174     spin_lock_irqsave(&sdos->lock, flags);
175 
176     sd->client_intr_enabled = enable;
177     if (enable && !sd->lockcount)
178         spi_devintr_on(sd);
179     else
180         spi_devintr_off(sd);
181 
182     spin_unlock_irqrestore(&sdos->lock, flags);
183 
184     return SDIOH_API_RC_SUCCESS;
185 }
186 
187 /* Protect against reentrancy (disable device interrupts while executing) */
188 void
spi_lock(sdioh_info_t * sd)189 spi_lock(sdioh_info_t *sd)
190 {
191     ulong flags;
192     struct sdos_info *sdos;
193 
194     sdos = (struct sdos_info *)sd->sdos_info;
195     ASSERT(sdos);
196 
197     sd_trace(("%s: %d\n", __FUNCTION__, sd->lockcount));
198 
199     spin_lock_irqsave(&sdos->lock, flags);
200     if (sd->lockcount) {
201         sd_err(("%s: Already locked!\n", __FUNCTION__));
202         ASSERT(sd->lockcount == 0);
203     }
204     spi_devintr_off(sd);
205     sd->lockcount++;
206     spin_unlock_irqrestore(&sdos->lock, flags);
207 }
208 
209 /* Enable client interrupt */
210 void
spi_unlock(sdioh_info_t * sd)211 spi_unlock(sdioh_info_t *sd)
212 {
213     ulong flags;
214     struct sdos_info *sdos;
215 
216     sd_trace(("%s: %d, %d\n", __FUNCTION__, sd->lockcount, sd->client_intr_enabled));
217     ASSERT(sd->lockcount > 0);
218 
219     sdos = (struct sdos_info *)sd->sdos_info;
220     ASSERT(sdos);
221 
222     spin_lock_irqsave(&sdos->lock, flags);
223     if (--sd->lockcount == 0 && sd->client_intr_enabled) {
224         spi_devintr_on(sd);
225     }
226     spin_unlock_irqrestore(&sdos->lock, flags);
227 }
228 
spi_waitbits(sdioh_info_t * sd,bool yield)229 void spi_waitbits(sdioh_info_t *sd, bool yield)
230 {
231 #ifndef BCMSDYIELD
232     ASSERT(!yield);
233 #endif
234     sd_trace(("%s: yield %d canblock %d\n",
235               __FUNCTION__, yield, BLOCKABLE()));
236 
237     /* Clear the "interrupt happened" flag and last intrstatus */
238     sd->got_hcint = FALSE;
239 
240 #ifdef BCMSDYIELD
241     if (yield && BLOCKABLE()) {
242         struct sdos_info *sdos;
243         sdos = (struct sdos_info *)sd->sdos_info;
244         /* Wait for the indication, the interrupt will be masked when the ISR fires. */
245         wait_event_interruptible(sdos->intr_wait_queue, (sd->got_hcint));
246     } else
247 #endif /* BCMSDYIELD */
248     {
249         spi_spinbits(sd);
250     }
251 }
252