bcmsdspi_linux.c 11 KB

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