bcmsdspi_linux.c 6.1 KB

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