bcmsdspi_linux.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Broadcom SPI Host Controller Driver - Linux Per-port
  3. *
  4. * Copyright (C) 1999-2015, 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 406045 2013-06-05 22:09:52Z $
  25. */
  26. #include <typedefs.h>
  27. #include <bcmutils.h>
  28. #include <bcmsdbus.h> /* bcmsdh to/from specific controller APIs */
  29. #include <sdiovar.h> /* to get msglevel bit values */
  30. #ifdef BCMSPI_ANDROID
  31. #include <bcmsdh.h>
  32. #include <bcmspibrcm.h>
  33. #include <linux/spi/spi.h>
  34. #else
  35. #include <pcicfg.h>
  36. #include <sdio.h> /* SDIO Device and Protocol Specs */
  37. #include <linux/sched.h> /* request_irq(), free_irq() */
  38. #include <bcmsdspi.h>
  39. #include <bcmspi.h>
  40. #endif /* BCMSPI_ANDROID */
  41. #ifndef BCMSPI_ANDROID
  42. extern uint sd_crc;
  43. module_param(sd_crc, uint, 0);
  44. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
  45. #define KERNEL26
  46. #endif
  47. #endif /* !BCMSPI_ANDROID */
  48. struct sdos_info {
  49. sdioh_info_t *sd;
  50. spinlock_t lock;
  51. #ifndef BCMSPI_ANDROID
  52. wait_queue_head_t intr_wait_queue;
  53. #endif /* !BCMSPI_ANDROID */
  54. };
  55. #ifndef BCMSPI_ANDROID
  56. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0))
  57. #define BLOCKABLE() (!in_atomic())
  58. #else
  59. #define BLOCKABLE() (!in_interrupt())
  60. #endif
  61. /* Interrupt handler */
  62. static irqreturn_t
  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. sd = (sdioh_info_t *)dev_id;
  73. sd->local_intrcount++;
  74. if (!sd->card_init_done) {
  75. sd_err(("%s: Hey Bogus intr...not even initted: irq %d\n", __FUNCTION__, irq));
  76. return IRQ_RETVAL(FALSE);
  77. } else {
  78. ours = spi_check_client_intr(sd, NULL);
  79. /* For local interrupts, wake the waiting process */
  80. if (ours && sd->got_hcint) {
  81. sdos = (struct sdos_info *)sd->sdos_info;
  82. wake_up_interruptible(&sdos->intr_wait_queue);
  83. }
  84. return IRQ_RETVAL(ours);
  85. }
  86. }
  87. #endif /* !BCMSPI_ANDROID */
  88. #ifdef BCMSPI_ANDROID
  89. static struct spi_device *gBCMSPI = NULL;
  90. extern int bcmsdh_probe(struct device *dev);
  91. extern int bcmsdh_remove(struct device *dev);
  92. static int bcmsdh_spi_probe(struct spi_device *spi_dev)
  93. {
  94. int ret = 0;
  95. gBCMSPI = spi_dev;
  96. #ifdef SPI_PIO_32BIT_RW
  97. spi_dev->bits_per_word = 32;
  98. #else
  99. spi_dev->bits_per_word = 8;
  100. #endif /* SPI_PIO_32BIT_RW */
  101. ret = spi_setup(spi_dev);
  102. if (ret) {
  103. sd_err(("bcmsdh_spi_probe: spi_setup fail with %d\n", ret));
  104. }
  105. sd_err(("bcmsdh_spi_probe: spi_setup with %d, bits_per_word=%d\n",
  106. ret, spi_dev->bits_per_word));
  107. ret = bcmsdh_probe(&spi_dev->dev);
  108. return ret;
  109. }
  110. static int bcmsdh_spi_remove(struct spi_device *spi_dev)
  111. {
  112. int ret = 0;
  113. ret = bcmsdh_remove(&spi_dev->dev);
  114. gBCMSPI = NULL;
  115. return ret;
  116. }
  117. static struct spi_driver bcmsdh_spi_driver = {
  118. .probe = bcmsdh_spi_probe,
  119. .remove = bcmsdh_spi_remove,
  120. .driver = {
  121. .name = "wlan_spi",
  122. .bus = &spi_bus_type,
  123. .owner = THIS_MODULE,
  124. },
  125. };
  126. /*
  127. * module init
  128. */
  129. int bcmsdh_register_client_driver(void)
  130. {
  131. int error = 0;
  132. sd_trace(("bcmsdh_gspi: %s Enter\n", __FUNCTION__));
  133. error = spi_register_driver(&bcmsdh_spi_driver);
  134. return error;
  135. }
  136. /*
  137. * module cleanup
  138. */
  139. void bcmsdh_unregister_client_driver(void)
  140. {
  141. sd_trace(("%s Enter\n", __FUNCTION__));
  142. spi_unregister_driver(&bcmsdh_spi_driver);
  143. }
  144. #endif /* BCMSPI_ANDROID */
  145. /* Register with Linux for interrupts */
  146. int
  147. spi_register_irq(sdioh_info_t *sd, uint irq)
  148. {
  149. #ifndef BCMSPI_ANDROID
  150. sd_trace(("Entering %s: irq == %d\n", __FUNCTION__, irq));
  151. if (request_irq(irq, sdspi_isr, IRQF_SHARED, "bcmsdspi", sd) < 0) {
  152. sd_err(("%s: request_irq() failed\n", __FUNCTION__));
  153. return ERROR;
  154. }
  155. #endif /* !BCMSPI_ANDROID */
  156. return SUCCESS;
  157. }
  158. /* Free Linux irq */
  159. void
  160. spi_free_irq(uint irq, sdioh_info_t *sd)
  161. {
  162. #ifndef BCMSPI_ANDROID
  163. free_irq(irq, sd);
  164. #endif /* !BCMSPI_ANDROID */
  165. }
  166. /* Map Host controller registers */
  167. #ifndef BCMSPI_ANDROID
  168. uint32 *
  169. spi_reg_map(osl_t *osh, uintptr addr, int size)
  170. {
  171. return (uint32 *)REG_MAP(addr, size);
  172. }
  173. void
  174. spi_reg_unmap(osl_t *osh, uintptr addr, int size)
  175. {
  176. REG_UNMAP((void*)(uintptr)addr);
  177. }
  178. #endif /* !BCMSPI_ANDROID */
  179. int
  180. spi_osinit(sdioh_info_t *sd)
  181. {
  182. struct sdos_info *sdos;
  183. sdos = (struct sdos_info*)MALLOC(sd->osh, sizeof(struct sdos_info));
  184. sd->sdos_info = (void*)sdos;
  185. if (sdos == NULL)
  186. return BCME_NOMEM;
  187. sdos->sd = sd;
  188. spin_lock_init(&sdos->lock);
  189. #ifndef BCMSPI_ANDROID
  190. init_waitqueue_head(&sdos->intr_wait_queue);
  191. #endif /* !BCMSPI_ANDROID */
  192. return BCME_OK;
  193. }
  194. void
  195. spi_osfree(sdioh_info_t *sd)
  196. {
  197. struct sdos_info *sdos;
  198. ASSERT(sd && sd->sdos_info);
  199. sdos = (struct sdos_info *)sd->sdos_info;
  200. MFREE(sd->osh, sdos, sizeof(struct sdos_info));
  201. }
  202. /* Interrupt enable/disable */
  203. SDIOH_API_RC
  204. sdioh_interrupt_set(sdioh_info_t *sd, bool enable)
  205. {
  206. ulong flags;
  207. struct sdos_info *sdos;
  208. sd_trace(("%s: %s\n", __FUNCTION__, enable ? "Enabling" : "Disabling"));
  209. sdos = (struct sdos_info *)sd->sdos_info;
  210. ASSERT(sdos);
  211. if (!(sd->host_init_done && sd->card_init_done)) {
  212. sd_err(("%s: Card & Host are not initted - bailing\n", __FUNCTION__));
  213. return SDIOH_API_RC_FAIL;
  214. }
  215. #ifndef BCMSPI_ANDROID
  216. if (enable && !(sd->intr_handler && sd->intr_handler_arg)) {
  217. sd_err(("%s: no handler registered, will not enable\n", __FUNCTION__));
  218. return SDIOH_API_RC_FAIL;
  219. }
  220. #endif /* !BCMSPI_ANDROID */
  221. /* Ensure atomicity for enable/disable calls */
  222. spin_lock_irqsave(&sdos->lock, flags);
  223. sd->client_intr_enabled = enable;
  224. #ifndef BCMSPI_ANDROID
  225. if (enable && !sd->lockcount)
  226. spi_devintr_on(sd);
  227. else
  228. spi_devintr_off(sd);
  229. #endif /* !BCMSPI_ANDROID */
  230. spin_unlock_irqrestore(&sdos->lock, flags);
  231. return SDIOH_API_RC_SUCCESS;
  232. }
  233. /* Protect against reentrancy (disable device interrupts while executing) */
  234. void
  235. spi_lock(sdioh_info_t *sd)
  236. {
  237. ulong flags;
  238. struct sdos_info *sdos;
  239. sdos = (struct sdos_info *)sd->sdos_info;
  240. ASSERT(sdos);
  241. sd_trace(("%s: %d\n", __FUNCTION__, sd->lockcount));
  242. spin_lock_irqsave(&sdos->lock, flags);
  243. if (sd->lockcount) {
  244. sd_err(("%s: Already locked!\n", __FUNCTION__));
  245. ASSERT(sd->lockcount == 0);
  246. }
  247. #ifdef BCMSPI_ANDROID
  248. if (sd->client_intr_enabled)
  249. bcmsdh_oob_intr_set(0);
  250. #else
  251. spi_devintr_off(sd);
  252. #endif /* BCMSPI_ANDROID */
  253. sd->lockcount++;
  254. spin_unlock_irqrestore(&sdos->lock, flags);
  255. }
  256. /* Enable client interrupt */
  257. void
  258. spi_unlock(sdioh_info_t *sd)
  259. {
  260. ulong flags;
  261. struct sdos_info *sdos;
  262. sd_trace(("%s: %d, %d\n", __FUNCTION__, sd->lockcount, sd->client_intr_enabled));
  263. ASSERT(sd->lockcount > 0);
  264. sdos = (struct sdos_info *)sd->sdos_info;
  265. ASSERT(sdos);
  266. spin_lock_irqsave(&sdos->lock, flags);
  267. if (--sd->lockcount == 0 && sd->client_intr_enabled) {
  268. #ifdef BCMSPI_ANDROID
  269. bcmsdh_oob_intr_set(1);
  270. #else
  271. spi_devintr_on(sd);
  272. #endif /* BCMSPI_ANDROID */
  273. }
  274. spin_unlock_irqrestore(&sdos->lock, flags);
  275. }
  276. #ifndef BCMSPI_ANDROID
  277. void spi_waitbits(sdioh_info_t *sd, bool yield)
  278. {
  279. #ifndef BCMSDYIELD
  280. ASSERT(!yield);
  281. #endif
  282. sd_trace(("%s: yield %d canblock %d\n",
  283. __FUNCTION__, yield, BLOCKABLE()));
  284. /* Clear the "interrupt happened" flag and last intrstatus */
  285. sd->got_hcint = FALSE;
  286. #ifdef BCMSDYIELD
  287. if (yield && BLOCKABLE()) {
  288. struct sdos_info *sdos;
  289. sdos = (struct sdos_info *)sd->sdos_info;
  290. /* Wait for the indication, the interrupt will be masked when the ISR fires. */
  291. wait_event_interruptible(sdos->intr_wait_queue, (sd->got_hcint));
  292. } else
  293. #endif /* BCMSDYIELD */
  294. {
  295. spi_spinbits(sd);
  296. }
  297. }
  298. #else /* !BCMSPI_ANDROID */
  299. int bcmgspi_dump = 0; /* Set to dump complete trace of all SPI bus transactions */
  300. static void
  301. hexdump(char *pfx, unsigned char *msg, int msglen)
  302. {
  303. int i, col;
  304. char buf[80];
  305. ASSERT(strlen(pfx) + 49 <= sizeof(buf));
  306. col = 0;
  307. for (i = 0; i < msglen; i++, col++) {
  308. if (col % 16 == 0)
  309. strcpy(buf, pfx);
  310. sprintf(buf + strlen(buf), "%02x", msg[i]);
  311. if ((col + 1) % 16 == 0)
  312. printf("%s\n", buf);
  313. else
  314. sprintf(buf + strlen(buf), " ");
  315. }
  316. if (col % 16 != 0)
  317. printf("%s\n", buf);
  318. }
  319. /* Send/Receive an SPI Packet */
  320. void
  321. spi_sendrecv(sdioh_info_t *sd, uint8 *msg_out, uint8 *msg_in, int msglen)
  322. {
  323. int write = 0;
  324. int tx_len = 0;
  325. struct spi_message msg;
  326. struct spi_transfer t[2];
  327. spi_message_init(&msg);
  328. memset(t, 0, 2*sizeof(struct spi_transfer));
  329. if (sd->wordlen == 2)
  330. #if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
  331. write = msg_out[2] & 0x80;
  332. #else
  333. write = msg_out[1] & 0x80;
  334. #endif /* !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW)) */
  335. if (sd->wordlen == 4)
  336. #if !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW))
  337. write = msg_out[0] & 0x80;
  338. #else
  339. write = msg_out[3] & 0x80;
  340. #endif /* !(defined(SPI_PIO_RW_BIGENDIAN) && defined(SPI_PIO_32BIT_RW)) */
  341. if (bcmgspi_dump) {
  342. hexdump(" OUT: ", msg_out, msglen);
  343. }
  344. tx_len = write ? msglen-4 : 4;
  345. sd_trace(("spi_sendrecv: %s, wordlen %d, cmd : 0x%02x 0x%02x 0x%02x 0x%02x\n",
  346. write ? "WR" : "RD", sd->wordlen,
  347. msg_out[0], msg_out[1], msg_out[2], msg_out[3]));
  348. t[0].tx_buf = (char *)&msg_out[0];
  349. t[0].rx_buf = 0;
  350. t[0].len = tx_len;
  351. spi_message_add_tail(&t[0], &msg);
  352. t[1].rx_buf = (char *)&msg_in[tx_len];
  353. t[1].tx_buf = 0;
  354. t[1].len = msglen-tx_len;
  355. spi_message_add_tail(&t[1], &msg);
  356. spi_sync(gBCMSPI, &msg);
  357. if (bcmgspi_dump) {
  358. hexdump(" IN : ", msg_in, msglen);
  359. }
  360. }
  361. #endif /* !BCMSPI_ANDROID */