/drivers/staging/brcm80211/brcmfmac/dhd_sdio.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 6772 lines · 5051 code · 1015 blank · 706 comment · 946 complexity · 02856cd1f5dfd7e3cc5f432120f1cff0 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * Copyright (c) 2010 Broadcom Corporation
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/kthread.h>
  19. #include <linux/printk.h>
  20. #include <linux/pci_ids.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/sched.h>
  24. #include <linux/mmc/sdio.h>
  25. #include <linux/mmc/sdio_func.h>
  26. #include <linux/semaphore.h>
  27. #include <linux/firmware.h>
  28. #include <asm/unaligned.h>
  29. #include <defs.h>
  30. #include <brcmu_wifi.h>
  31. #include <brcmu_utils.h>
  32. #include <brcm_hw_ids.h>
  33. #include <soc.h>
  34. #include "sdio_host.h"
  35. /* register access macros */
  36. #ifndef __BIG_ENDIAN
  37. #ifndef __mips__
  38. #define R_REG(r, typ) \
  39. brcmf_sdcard_reg_read(NULL, (r), sizeof(typ))
  40. #else /* __mips__ */
  41. #define R_REG(r, typ) \
  42. ({ \
  43. __typeof(*(r)) __osl_v; \
  44. __asm__ __volatile__("sync"); \
  45. __osl_v = brcmf_sdcard_reg_read(NULL, (r),\
  46. sizeof(typ)); \
  47. __asm__ __volatile__("sync"); \
  48. __osl_v; \
  49. })
  50. #endif /* __mips__ */
  51. #else /* __BIG_ENDIAN */
  52. #define R_REG(r, typ) \
  53. brcmf_sdcard_reg_read(NULL, (r), sizeof(typ))
  54. #endif /* __BIG_ENDIAN */
  55. #define OR_REG(r, v, typ) \
  56. brcmf_sdcard_reg_write(NULL, (r), sizeof(typ), R_REG(r, typ) | (v))
  57. #ifdef BCMDBG
  58. /* ARM trap handling */
  59. /* Trap types defined by ARM (see arminc.h) */
  60. #if defined(__ARM_ARCH_4T__)
  61. #define MAX_TRAP_TYPE (TR_FIQ + 1)
  62. #elif defined(__ARM_ARCH_7M__)
  63. #define MAX_TRAP_TYPE (TR_ISR + ARMCM3_NUMINTS)
  64. #endif /* __ARM_ARCH_7M__ */
  65. /* The trap structure is defined here as offsets for assembly */
  66. #define TR_TYPE 0x00
  67. #define TR_EPC 0x04
  68. #define TR_CPSR 0x08
  69. #define TR_SPSR 0x0c
  70. #define TR_REGS 0x10
  71. #define TR_REG(n) (TR_REGS + (n) * 4)
  72. #define TR_SP TR_REG(13)
  73. #define TR_LR TR_REG(14)
  74. #define TR_PC TR_REG(15)
  75. #define TRAP_T_SIZE 80
  76. struct brcmf_trap {
  77. u32 type;
  78. u32 epc;
  79. u32 cpsr;
  80. u32 spsr;
  81. u32 r0;
  82. u32 r1;
  83. u32 r2;
  84. u32 r3;
  85. u32 r4;
  86. u32 r5;
  87. u32 r6;
  88. u32 r7;
  89. u32 r8;
  90. u32 r9;
  91. u32 r10;
  92. u32 r11;
  93. u32 r12;
  94. u32 r13;
  95. u32 r14;
  96. u32 pc;
  97. };
  98. #define CBUF_LEN (128)
  99. struct rte_log {
  100. u32 buf; /* Can't be pointer on (64-bit) hosts */
  101. uint buf_size;
  102. uint idx;
  103. char *_buf_compat; /* Redundant pointer for backward compat. */
  104. };
  105. struct rte_console {
  106. /* Virtual UART
  107. * When there is no UART (e.g. Quickturn),
  108. * the host should write a complete
  109. * input line directly into cbuf and then write
  110. * the length into vcons_in.
  111. * This may also be used when there is a real UART
  112. * (at risk of conflicting with
  113. * the real UART). vcons_out is currently unused.
  114. */
  115. volatile uint vcons_in;
  116. volatile uint vcons_out;
  117. /* Output (logging) buffer
  118. * Console output is written to a ring buffer log_buf at index log_idx.
  119. * The host may read the output when it sees log_idx advance.
  120. * Output will be lost if the output wraps around faster than the host
  121. * polls.
  122. */
  123. struct rte_log log;
  124. /* Console input line buffer
  125. * Characters are read one at a time into cbuf
  126. * until <CR> is received, then
  127. * the buffer is processed as a command line.
  128. * Also used for virtual UART.
  129. */
  130. uint cbuf_idx;
  131. char cbuf[CBUF_LEN];
  132. };
  133. #endif /* BCMDBG */
  134. #include <chipcommon.h>
  135. #include "dhd.h"
  136. #include "dhd_bus.h"
  137. #include "dhd_proto.h"
  138. #include "dhd_dbg.h"
  139. #include <bcmchip.h>
  140. #define TXQLEN 2048 /* bulk tx queue length */
  141. #define TXHI (TXQLEN - 256) /* turn on flow control above TXHI */
  142. #define TXLOW (TXHI - 256) /* turn off flow control below TXLOW */
  143. #define PRIOMASK 7
  144. #define TXRETRIES 2 /* # of retries for tx frames */
  145. #define BRCMF_RXBOUND 50 /* Default for max rx frames in
  146. one scheduling */
  147. #define BRCMF_TXBOUND 20 /* Default for max tx frames in
  148. one scheduling */
  149. #define BRCMF_TXMINMAX 1 /* Max tx frames if rx still pending */
  150. #define MEMBLOCK 2048 /* Block size used for downloading
  151. of dongle image */
  152. #define MAX_DATA_BUF (32 * 1024) /* Must be large enough to hold
  153. biggest possible glom */
  154. #ifndef BRCMF_FIRSTREAD
  155. #define BRCMF_FIRSTREAD 32
  156. #endif
  157. #if !ISPOWEROF2(BRCMF_FIRSTREAD)
  158. #error BRCMF_FIRSTREAD is not a power of 2!
  159. #endif
  160. /* SBSDIO_DEVICE_CTL */
  161. #define SBSDIO_DEVCTL_SETBUSY 0x01 /* 1: device will assert busy signal when
  162. * receiving CMD53
  163. */
  164. #define SBSDIO_DEVCTL_SPI_INTR_SYNC 0x02 /* 1: assertion of sdio interrupt is
  165. * synchronous to the sdio clock
  166. */
  167. #define SBSDIO_DEVCTL_CA_INT_ONLY 0x04 /* 1: mask all interrupts to host
  168. * except the chipActive (rev 8)
  169. */
  170. #define SBSDIO_DEVCTL_PADS_ISO 0x08 /* 1: isolate internal sdio signals, put
  171. * external pads in tri-state; requires
  172. * sdio bus power cycle to clear (rev 9)
  173. */
  174. #define SBSDIO_DEVCTL_SB_RST_CTL 0x30 /* Force SD->SB reset mapping (rev 11) */
  175. #define SBSDIO_DEVCTL_RST_CORECTL 0x00 /* Determined by CoreControl bit */
  176. #define SBSDIO_DEVCTL_RST_BPRESET 0x10 /* Force backplane reset */
  177. #define SBSDIO_DEVCTL_RST_NOBPRESET 0x20 /* Force no backplane reset */
  178. /* SBSDIO_FUNC1_CHIPCLKCSR */
  179. #define SBSDIO_FORCE_ALP 0x01 /* Force ALP request to backplane */
  180. #define SBSDIO_FORCE_HT 0x02 /* Force HT request to backplane */
  181. #define SBSDIO_FORCE_ILP 0x04 /* Force ILP request to backplane */
  182. #define SBSDIO_ALP_AVAIL_REQ 0x08 /* Make ALP ready (power up xtal) */
  183. #define SBSDIO_HT_AVAIL_REQ 0x10 /* Make HT ready (power up PLL) */
  184. #define SBSDIO_FORCE_HW_CLKREQ_OFF 0x20 /* Squelch clock requests from HW */
  185. #define SBSDIO_ALP_AVAIL 0x40 /* Status: ALP is ready */
  186. #define SBSDIO_HT_AVAIL 0x80 /* Status: HT is ready */
  187. #define SBSDIO_AVBITS (SBSDIO_HT_AVAIL | SBSDIO_ALP_AVAIL)
  188. #define SBSDIO_ALPAV(regval) ((regval) & SBSDIO_AVBITS)
  189. #define SBSDIO_HTAV(regval) (((regval) & SBSDIO_AVBITS) == SBSDIO_AVBITS)
  190. #define SBSDIO_ALPONLY(regval) (SBSDIO_ALPAV(regval) && !SBSDIO_HTAV(regval))
  191. #define SBSDIO_CLKAV(regval, alponly) (SBSDIO_ALPAV(regval) && \
  192. (alponly ? 1 : SBSDIO_HTAV(regval)))
  193. /* direct(mapped) cis space */
  194. #define SBSDIO_CIS_BASE_COMMON 0x1000 /* MAPPED common CIS address */
  195. #define SBSDIO_CIS_SIZE_LIMIT 0x200 /* maximum bytes in one CIS */
  196. #define SBSDIO_CIS_OFT_ADDR_MASK 0x1FFFF /* cis offset addr is < 17 bits */
  197. #define SBSDIO_CIS_MANFID_TUPLE_LEN 6 /* manfid tuple length, include tuple,
  198. * link bytes
  199. */
  200. /* intstatus */
  201. #define I_SMB_SW0 (1 << 0) /* To SB Mail S/W interrupt 0 */
  202. #define I_SMB_SW1 (1 << 1) /* To SB Mail S/W interrupt 1 */
  203. #define I_SMB_SW2 (1 << 2) /* To SB Mail S/W interrupt 2 */
  204. #define I_SMB_SW3 (1 << 3) /* To SB Mail S/W interrupt 3 */
  205. #define I_SMB_SW_MASK 0x0000000f /* To SB Mail S/W interrupts mask */
  206. #define I_SMB_SW_SHIFT 0 /* To SB Mail S/W interrupts shift */
  207. #define I_HMB_SW0 (1 << 4) /* To Host Mail S/W interrupt 0 */
  208. #define I_HMB_SW1 (1 << 5) /* To Host Mail S/W interrupt 1 */
  209. #define I_HMB_SW2 (1 << 6) /* To Host Mail S/W interrupt 2 */
  210. #define I_HMB_SW3 (1 << 7) /* To Host Mail S/W interrupt 3 */
  211. #define I_HMB_SW_MASK 0x000000f0 /* To Host Mail S/W interrupts mask */
  212. #define I_HMB_SW_SHIFT 4 /* To Host Mail S/W interrupts shift */
  213. #define I_WR_OOSYNC (1 << 8) /* Write Frame Out Of Sync */
  214. #define I_RD_OOSYNC (1 << 9) /* Read Frame Out Of Sync */
  215. #define I_PC (1 << 10) /* descriptor error */
  216. #define I_PD (1 << 11) /* data error */
  217. #define I_DE (1 << 12) /* Descriptor protocol Error */
  218. #define I_RU (1 << 13) /* Receive descriptor Underflow */
  219. #define I_RO (1 << 14) /* Receive fifo Overflow */
  220. #define I_XU (1 << 15) /* Transmit fifo Underflow */
  221. #define I_RI (1 << 16) /* Receive Interrupt */
  222. #define I_BUSPWR (1 << 17) /* SDIO Bus Power Change (rev 9) */
  223. #define I_XMTDATA_AVAIL (1 << 23) /* bits in fifo */
  224. #define I_XI (1 << 24) /* Transmit Interrupt */
  225. #define I_RF_TERM (1 << 25) /* Read Frame Terminate */
  226. #define I_WF_TERM (1 << 26) /* Write Frame Terminate */
  227. #define I_PCMCIA_XU (1 << 27) /* PCMCIA Transmit FIFO Underflow */
  228. #define I_SBINT (1 << 28) /* sbintstatus Interrupt */
  229. #define I_CHIPACTIVE (1 << 29) /* chip from doze to active state */
  230. #define I_SRESET (1 << 30) /* CCCR RES interrupt */
  231. #define I_IOE2 (1U << 31) /* CCCR IOE2 Bit Changed */
  232. #define I_ERRORS (I_PC | I_PD | I_DE | I_RU | I_RO | I_XU)
  233. #define I_DMA (I_RI | I_XI | I_ERRORS)
  234. /* corecontrol */
  235. #define CC_CISRDY (1 << 0) /* CIS Ready */
  236. #define CC_BPRESEN (1 << 1) /* CCCR RES signal */
  237. #define CC_F2RDY (1 << 2) /* set CCCR IOR2 bit */
  238. #define CC_CLRPADSISO (1 << 3) /* clear SDIO pads isolation */
  239. #define CC_XMTDATAAVAIL_MODE (1 << 4)
  240. #define CC_XMTDATAAVAIL_CTRL (1 << 5)
  241. /* SDA_FRAMECTRL */
  242. #define SFC_RF_TERM (1 << 0) /* Read Frame Terminate */
  243. #define SFC_WF_TERM (1 << 1) /* Write Frame Terminate */
  244. #define SFC_CRC4WOOS (1 << 2) /* CRC error for write out of sync */
  245. #define SFC_ABORTALL (1 << 3) /* Abort all in-progress frames */
  246. /* HW frame tag */
  247. #define SDPCM_FRAMETAG_LEN 4 /* 2 bytes len, 2 bytes check val */
  248. /* Total length of frame header for dongle protocol */
  249. #define SDPCM_HDRLEN (SDPCM_FRAMETAG_LEN + SDPCM_SWHEADER_LEN)
  250. #ifdef SDTEST
  251. #define SDPCM_RESERVE (SDPCM_HDRLEN + SDPCM_TEST_HDRLEN + BRCMF_SDALIGN)
  252. #else
  253. #define SDPCM_RESERVE (SDPCM_HDRLEN + BRCMF_SDALIGN)
  254. #endif
  255. /*
  256. * Software allocation of To SB Mailbox resources
  257. */
  258. /* tosbmailbox bits corresponding to intstatus bits */
  259. #define SMB_NAK (1 << 0) /* Frame NAK */
  260. #define SMB_INT_ACK (1 << 1) /* Host Interrupt ACK */
  261. #define SMB_USE_OOB (1 << 2) /* Use OOB Wakeup */
  262. #define SMB_DEV_INT (1 << 3) /* Miscellaneous Interrupt */
  263. /* tosbmailboxdata */
  264. #define SMB_DATA_VERSION_SHIFT 16 /* host protocol version */
  265. /*
  266. * Software allocation of To Host Mailbox resources
  267. */
  268. /* intstatus bits */
  269. #define I_HMB_FC_STATE I_HMB_SW0 /* Flow Control State */
  270. #define I_HMB_FC_CHANGE I_HMB_SW1 /* Flow Control State Changed */
  271. #define I_HMB_FRAME_IND I_HMB_SW2 /* Frame Indication */
  272. #define I_HMB_HOST_INT I_HMB_SW3 /* Miscellaneous Interrupt */
  273. /* tohostmailboxdata */
  274. #define HMB_DATA_NAKHANDLED 1 /* retransmit NAK'd frame */
  275. #define HMB_DATA_DEVREADY 2 /* talk to host after enable */
  276. #define HMB_DATA_FC 4 /* per prio flowcontrol update flag */
  277. #define HMB_DATA_FWREADY 8 /* fw ready for protocol activity */
  278. #define HMB_DATA_FCDATA_MASK 0xff000000
  279. #define HMB_DATA_FCDATA_SHIFT 24
  280. #define HMB_DATA_VERSION_MASK 0x00ff0000
  281. #define HMB_DATA_VERSION_SHIFT 16
  282. /*
  283. * Software-defined protocol header
  284. */
  285. /* Current protocol version */
  286. #define SDPCM_PROT_VERSION 4
  287. /* SW frame header */
  288. #define SDPCM_PACKET_SEQUENCE(p) (((u8 *)p)[0] & 0xff)
  289. #define SDPCM_CHANNEL_MASK 0x00000f00
  290. #define SDPCM_CHANNEL_SHIFT 8
  291. #define SDPCM_PACKET_CHANNEL(p) (((u8 *)p)[1] & 0x0f)
  292. #define SDPCM_NEXTLEN_OFFSET 2
  293. /* Data Offset from SOF (HW Tag, SW Tag, Pad) */
  294. #define SDPCM_DOFFSET_OFFSET 3 /* Data Offset */
  295. #define SDPCM_DOFFSET_VALUE(p) (((u8 *)p)[SDPCM_DOFFSET_OFFSET] & 0xff)
  296. #define SDPCM_DOFFSET_MASK 0xff000000
  297. #define SDPCM_DOFFSET_SHIFT 24
  298. #define SDPCM_FCMASK_OFFSET 4 /* Flow control */
  299. #define SDPCM_FCMASK_VALUE(p) (((u8 *)p)[SDPCM_FCMASK_OFFSET] & 0xff)
  300. #define SDPCM_WINDOW_OFFSET 5 /* Credit based fc */
  301. #define SDPCM_WINDOW_VALUE(p) (((u8 *)p)[SDPCM_WINDOW_OFFSET] & 0xff)
  302. #define SDPCM_SWHEADER_LEN 8 /* SW header is 64 bits */
  303. /* logical channel numbers */
  304. #define SDPCM_CONTROL_CHANNEL 0 /* Control channel Id */
  305. #define SDPCM_EVENT_CHANNEL 1 /* Asyc Event Indication Channel Id */
  306. #define SDPCM_DATA_CHANNEL 2 /* Data Xmit/Recv Channel Id */
  307. #define SDPCM_GLOM_CHANNEL 3 /* For coalesced packets */
  308. #define SDPCM_TEST_CHANNEL 15 /* Reserved for test/debug packets */
  309. #define SDPCM_SEQUENCE_WRAP 256 /* wrap-around val for 8bit frame seq */
  310. #define SDPCM_GLOMDESC(p) (((u8 *)p)[1] & 0x80)
  311. /* For TEST_CHANNEL packets, define another 4-byte header */
  312. #define SDPCM_TEST_HDRLEN 4 /*
  313. * Generally: Cmd(1), Ext(1), Len(2);
  314. * Semantics of Ext byte depend on
  315. * command. Len is current or requested
  316. * frame length, not including test
  317. * header; sent little-endian.
  318. */
  319. #define SDPCM_TEST_DISCARD 0x01 /* Receiver discards. Ext:pattern id. */
  320. #define SDPCM_TEST_ECHOREQ 0x02 /* Echo request. Ext:pattern id. */
  321. #define SDPCM_TEST_ECHORSP 0x03 /* Echo response. Ext:pattern id. */
  322. #define SDPCM_TEST_BURST 0x04 /*
  323. * Receiver to send a burst.
  324. * Ext is a frame count
  325. */
  326. #define SDPCM_TEST_SEND 0x05 /*
  327. * Receiver sets send mode.
  328. * Ext is boolean on/off
  329. */
  330. /* Handy macro for filling in datagen packets with a pattern */
  331. #define SDPCM_TEST_FILL(byteno, id) ((u8)(id + byteno))
  332. /*
  333. * Shared structure between dongle and the host.
  334. * The structure contains pointers to trap or assert information.
  335. */
  336. #define SDPCM_SHARED_VERSION 0x0002
  337. #define SDPCM_SHARED_VERSION_MASK 0x00FF
  338. #define SDPCM_SHARED_ASSERT_BUILT 0x0100
  339. #define SDPCM_SHARED_ASSERT 0x0200
  340. #define SDPCM_SHARED_TRAP 0x0400
  341. /* Space for header read, limit for data packets */
  342. #ifndef MAX_HDR_READ
  343. #define MAX_HDR_READ 32
  344. #endif
  345. #if !ISPOWEROF2(MAX_HDR_READ)
  346. #error MAX_HDR_READ is not a power of 2!
  347. #endif
  348. #define MAX_RX_DATASZ 2048
  349. /* Maximum milliseconds to wait for F2 to come up */
  350. #define BRCMF_WAIT_F2RDY 3000
  351. /* Bump up limit on waiting for HT to account for first startup;
  352. * if the image is doing a CRC calculation before programming the PMU
  353. * for HT availability, it could take a couple hundred ms more, so
  354. * max out at a 1 second (1000000us).
  355. */
  356. #if (PMU_MAX_TRANSITION_DLY <= 1000000)
  357. #undef PMU_MAX_TRANSITION_DLY
  358. #define PMU_MAX_TRANSITION_DLY 1000000
  359. #endif
  360. /* Value for ChipClockCSR during initial setup */
  361. #define BRCMF_INIT_CLKCTL1 (SBSDIO_FORCE_HW_CLKREQ_OFF | \
  362. SBSDIO_ALP_AVAIL_REQ)
  363. /* Flags for SDH calls */
  364. #define F2SYNC (SDIO_REQ_4BYTE | SDIO_REQ_FIXED)
  365. /* sbimstate */
  366. #define SBIM_IBE 0x20000 /* inbanderror */
  367. #define SBIM_TO 0x40000 /* timeout */
  368. #define SBIM_BY 0x01800000 /* busy (sonics >= 2.3) */
  369. #define SBIM_RJ 0x02000000 /* reject (sonics >= 2.3) */
  370. /* sbtmstatelow */
  371. #define SBTML_RESET 0x0001 /* reset */
  372. #define SBTML_REJ_MASK 0x0006 /* reject field */
  373. #define SBTML_REJ 0x0002 /* reject */
  374. #define SBTML_TMPREJ 0x0004 /* temporary reject, for error recovery */
  375. #define SBTML_SICF_SHIFT 16 /* Shift to locate the SI control flags in sbtml */
  376. /* sbtmstatehigh */
  377. #define SBTMH_SERR 0x0001 /* serror */
  378. #define SBTMH_INT 0x0002 /* interrupt */
  379. #define SBTMH_BUSY 0x0004 /* busy */
  380. #define SBTMH_TO 0x0020 /* timeout (sonics >= 2.3) */
  381. #define SBTMH_SISF_SHIFT 16 /* Shift to locate the SI status flags in sbtmh */
  382. /* sbidlow */
  383. #define SBIDL_INIT 0x80 /* initiator */
  384. /* sbidhigh */
  385. #define SBIDH_RC_MASK 0x000f /* revision code */
  386. #define SBIDH_RCE_MASK 0x7000 /* revision code extension field */
  387. #define SBIDH_RCE_SHIFT 8
  388. #define SBCOREREV(sbidh) \
  389. ((((sbidh) & SBIDH_RCE_MASK) >> SBIDH_RCE_SHIFT) | ((sbidh) & SBIDH_RC_MASK))
  390. #define SBIDH_CC_MASK 0x8ff0 /* core code */
  391. #define SBIDH_CC_SHIFT 4
  392. #define SBIDH_VC_MASK 0xffff0000 /* vendor code */
  393. #define SBIDH_VC_SHIFT 16
  394. /*
  395. * Conversion of 802.1D priority to precedence level
  396. */
  397. #define PRIO2PREC(prio) \
  398. (((prio) == PRIO_8021D_NONE || (prio) == PRIO_8021D_BE) ? \
  399. ((prio^2)) : (prio))
  400. BRCMF_SPINWAIT_SLEEP_INIT(sdioh_spinwait_sleep);
  401. /*
  402. * Core reg address translation.
  403. * Both macro's returns a 32 bits byte address on the backplane bus.
  404. */
  405. #define CORE_CC_REG(base, field) (base + offsetof(chipcregs_t, field))
  406. #define CORE_BUS_REG(base, field) \
  407. (base + offsetof(struct sdpcmd_regs, field))
  408. #define CORE_SB(base, field) \
  409. (base + SBCONFIGOFF + offsetof(struct sbconfig, field))
  410. /* core registers */
  411. struct sdpcmd_regs {
  412. u32 corecontrol; /* 0x00, rev8 */
  413. u32 corestatus; /* rev8 */
  414. u32 PAD[1];
  415. u32 biststatus; /* rev8 */
  416. /* PCMCIA access */
  417. u16 pcmciamesportaladdr; /* 0x010, rev8 */
  418. u16 PAD[1];
  419. u16 pcmciamesportalmask; /* rev8 */
  420. u16 PAD[1];
  421. u16 pcmciawrframebc; /* rev8 */
  422. u16 PAD[1];
  423. u16 pcmciaunderflowtimer; /* rev8 */
  424. u16 PAD[1];
  425. /* interrupt */
  426. u32 intstatus; /* 0x020, rev8 */
  427. u32 hostintmask; /* rev8 */
  428. u32 intmask; /* rev8 */
  429. u32 sbintstatus; /* rev8 */
  430. u32 sbintmask; /* rev8 */
  431. u32 funcintmask; /* rev4 */
  432. u32 PAD[2];
  433. u32 tosbmailbox; /* 0x040, rev8 */
  434. u32 tohostmailbox; /* rev8 */
  435. u32 tosbmailboxdata; /* rev8 */
  436. u32 tohostmailboxdata; /* rev8 */
  437. /* synchronized access to registers in SDIO clock domain */
  438. u32 sdioaccess; /* 0x050, rev8 */
  439. u32 PAD[3];
  440. /* PCMCIA frame control */
  441. u8 pcmciaframectrl; /* 0x060, rev8 */
  442. u8 PAD[3];
  443. u8 pcmciawatermark; /* rev8 */
  444. u8 PAD[155];
  445. /* interrupt batching control */
  446. u32 intrcvlazy; /* 0x100, rev8 */
  447. u32 PAD[3];
  448. /* counters */
  449. u32 cmd52rd; /* 0x110, rev8 */
  450. u32 cmd52wr; /* rev8 */
  451. u32 cmd53rd; /* rev8 */
  452. u32 cmd53wr; /* rev8 */
  453. u32 abort; /* rev8 */
  454. u32 datacrcerror; /* rev8 */
  455. u32 rdoutofsync; /* rev8 */
  456. u32 wroutofsync; /* rev8 */
  457. u32 writebusy; /* rev8 */
  458. u32 readwait; /* rev8 */
  459. u32 readterm; /* rev8 */
  460. u32 writeterm; /* rev8 */
  461. u32 PAD[40];
  462. u32 clockctlstatus; /* rev8 */
  463. u32 PAD[7];
  464. u32 PAD[128]; /* DMA engines */
  465. /* SDIO/PCMCIA CIS region */
  466. char cis[512]; /* 0x400-0x5ff, rev6 */
  467. /* PCMCIA function control registers */
  468. char pcmciafcr[256]; /* 0x600-6ff, rev6 */
  469. u16 PAD[55];
  470. /* PCMCIA backplane access */
  471. u16 backplanecsr; /* 0x76E, rev6 */
  472. u16 backplaneaddr0; /* rev6 */
  473. u16 backplaneaddr1; /* rev6 */
  474. u16 backplaneaddr2; /* rev6 */
  475. u16 backplaneaddr3; /* rev6 */
  476. u16 backplanedata0; /* rev6 */
  477. u16 backplanedata1; /* rev6 */
  478. u16 backplanedata2; /* rev6 */
  479. u16 backplanedata3; /* rev6 */
  480. u16 PAD[31];
  481. /* sprom "size" & "blank" info */
  482. u16 spromstatus; /* 0x7BE, rev2 */
  483. u32 PAD[464];
  484. u16 PAD[0x80];
  485. };
  486. #ifdef BCMDBG
  487. /* Device console log buffer state */
  488. struct brcmf_console {
  489. uint count; /* Poll interval msec counter */
  490. uint log_addr; /* Log struct address (fixed) */
  491. struct rte_log log; /* Log struct (host copy) */
  492. uint bufsize; /* Size of log buffer */
  493. u8 *buf; /* Log buffer (host copy) */
  494. uint last; /* Last buffer read index */
  495. };
  496. #endif /* BCMDBG */
  497. struct sdpcm_shared {
  498. u32 flags;
  499. u32 trap_addr;
  500. u32 assert_exp_addr;
  501. u32 assert_file_addr;
  502. u32 assert_line;
  503. u32 console_addr; /* Address of struct rte_console */
  504. u32 msgtrace_addr;
  505. u8 tag[32];
  506. };
  507. /* misc chip info needed by some of the routines */
  508. struct chip_info {
  509. u32 chip;
  510. u32 chiprev;
  511. u32 cccorebase;
  512. u32 ccrev;
  513. u32 cccaps;
  514. u32 buscorebase; /* 32 bits backplane bus address */
  515. u32 buscorerev;
  516. u32 buscoretype;
  517. u32 ramcorebase;
  518. u32 armcorebase;
  519. u32 pmurev;
  520. u32 ramsize;
  521. };
  522. /* Private data for SDIO bus interaction */
  523. struct brcmf_bus {
  524. struct brcmf_pub *drvr;
  525. struct brcmf_sdio_card *card; /* Handle for sdio card calls */
  526. struct chip_info *ci; /* Chip info struct */
  527. char *vars; /* Variables (from CIS and/or other) */
  528. uint varsz; /* Size of variables buffer */
  529. u32 ramsize; /* Size of RAM in SOCRAM (bytes) */
  530. u32 orig_ramsize; /* Size of RAM in SOCRAM (bytes) */
  531. u32 bus; /* gSPI or SDIO bus */
  532. u32 hostintmask; /* Copy of Host Interrupt Mask */
  533. u32 intstatus; /* Intstatus bits (events) pending */
  534. bool dpc_sched; /* Indicates DPC schedule (intrpt rcvd) */
  535. bool fcstate; /* State of dongle flow-control */
  536. u16 cl_devid; /* cached devid for brcmf_sdio_probe_attach() */
  537. uint blocksize; /* Block size of SDIO transfers */
  538. uint roundup; /* Max roundup limit */
  539. struct pktq txq; /* Queue length used for flow-control */
  540. u8 flowcontrol; /* per prio flow control bitmask */
  541. u8 tx_seq; /* Transmit sequence number (next) */
  542. u8 tx_max; /* Maximum transmit sequence allowed */
  543. u8 hdrbuf[MAX_HDR_READ + BRCMF_SDALIGN];
  544. u8 *rxhdr; /* Header of current rx frame (in hdrbuf) */
  545. u16 nextlen; /* Next Read Len from last header */
  546. u8 rx_seq; /* Receive sequence number (expected) */
  547. bool rxskip; /* Skip receive (awaiting NAK ACK) */
  548. struct sk_buff *glomd; /* Packet containing glomming descriptor */
  549. struct sk_buff *glom; /* Packet chain for glommed superframe */
  550. uint glomerr; /* Glom packet read errors */
  551. u8 *rxbuf; /* Buffer for receiving control packets */
  552. uint rxblen; /* Allocated length of rxbuf */
  553. u8 *rxctl; /* Aligned pointer into rxbuf */
  554. u8 *databuf; /* Buffer for receiving big glom packet */
  555. u8 *dataptr; /* Aligned pointer into databuf */
  556. uint rxlen; /* Length of valid data in buffer */
  557. u8 sdpcm_ver; /* Bus protocol reported by dongle */
  558. bool intr; /* Use interrupts */
  559. bool poll; /* Use polling */
  560. bool ipend; /* Device interrupt is pending */
  561. bool intdis; /* Interrupts disabled by isr */
  562. uint intrcount; /* Count of device interrupt callbacks */
  563. uint lastintrs; /* Count as of last watchdog timer */
  564. uint spurious; /* Count of spurious interrupts */
  565. uint pollrate; /* Ticks between device polls */
  566. uint polltick; /* Tick counter */
  567. uint pollcnt; /* Count of active polls */
  568. #ifdef BCMDBG
  569. struct brcmf_console console; /* Console output polling support */
  570. uint console_addr; /* Console address from shared struct */
  571. #endif /* BCMDBG */
  572. uint regfails; /* Count of R_REG failures */
  573. uint clkstate; /* State of sd and backplane clock(s) */
  574. bool activity; /* Activity flag for clock down */
  575. s32 idletime; /* Control for activity timeout */
  576. s32 idlecount; /* Activity timeout counter */
  577. s32 idleclock; /* How to set bus driver when idle */
  578. s32 sd_rxchain;
  579. bool use_rxchain; /* If brcmf should use PKT chains */
  580. bool sleeping; /* Is SDIO bus sleeping? */
  581. bool rxflow_mode; /* Rx flow control mode */
  582. bool rxflow; /* Is rx flow control on */
  583. bool alp_only; /* Don't use HT clock (ALP only) */
  584. /* Field to decide if rx of control frames happen in rxbuf or lb-pool */
  585. bool usebufpool;
  586. #ifdef SDTEST
  587. /* external loopback */
  588. bool ext_loop;
  589. u8 loopid;
  590. /* pktgen configuration */
  591. uint pktgen_freq; /* Ticks between bursts */
  592. uint pktgen_count; /* Packets to send each burst */
  593. uint pktgen_print; /* Bursts between count displays */
  594. uint pktgen_total; /* Stop after this many */
  595. uint pktgen_minlen; /* Minimum packet data len */
  596. uint pktgen_maxlen; /* Maximum packet data len */
  597. uint pktgen_mode; /* Configured mode: tx, rx, or echo */
  598. uint pktgen_stop; /* Number of tx failures causing stop */
  599. /* active pktgen fields */
  600. uint pktgen_tick; /* Tick counter for bursts */
  601. uint pktgen_ptick; /* Burst counter for printing */
  602. uint pktgen_sent; /* Number of test packets generated */
  603. uint pktgen_rcvd; /* Number of test packets received */
  604. uint pktgen_fail; /* Number of failed send attempts */
  605. u16 pktgen_len; /* Length of next packet to send */
  606. #endif /* SDTEST */
  607. /* Some additional counters */
  608. uint tx_sderrs; /* Count of tx attempts with sd errors */
  609. uint fcqueued; /* Tx packets that got queued */
  610. uint rxrtx; /* Count of rtx requests (NAK to dongle) */
  611. uint rx_toolong; /* Receive frames too long to receive */
  612. uint rxc_errors; /* SDIO errors when reading control frames */
  613. uint rx_hdrfail; /* SDIO errors on header reads */
  614. uint rx_badhdr; /* Bad received headers (roosync?) */
  615. uint rx_badseq; /* Mismatched rx sequence number */
  616. uint fc_rcvd; /* Number of flow-control events received */
  617. uint fc_xoff; /* Number which turned on flow-control */
  618. uint fc_xon; /* Number which turned off flow-control */
  619. uint rxglomfail; /* Failed deglom attempts */
  620. uint rxglomframes; /* Number of glom frames (superframes) */
  621. uint rxglompkts; /* Number of packets from glom frames */
  622. uint f2rxhdrs; /* Number of header reads */
  623. uint f2rxdata; /* Number of frame data reads */
  624. uint f2txdata; /* Number of f2 frame writes */
  625. uint f1regdata; /* Number of f1 register accesses */
  626. u8 *ctrl_frame_buf;
  627. u32 ctrl_frame_len;
  628. bool ctrl_frame_stat;
  629. spinlock_t txqlock;
  630. wait_queue_head_t ctrl_wait;
  631. struct timer_list timer;
  632. struct completion watchdog_wait;
  633. struct task_struct *watchdog_tsk;
  634. bool wd_timer_valid;
  635. struct tasklet_struct tasklet;
  636. struct task_struct *dpc_tsk;
  637. struct completion dpc_wait;
  638. bool threads_only;
  639. struct semaphore sdsem;
  640. spinlock_t sdlock;
  641. const char *fw_name;
  642. const struct firmware *firmware;
  643. const char *nv_name;
  644. u32 fw_ptr;
  645. };
  646. struct sbconfig {
  647. u32 PAD[2];
  648. u32 sbipsflag; /* initiator port ocp slave flag */
  649. u32 PAD[3];
  650. u32 sbtpsflag; /* target port ocp slave flag */
  651. u32 PAD[11];
  652. u32 sbtmerrloga; /* (sonics >= 2.3) */
  653. u32 PAD;
  654. u32 sbtmerrlog; /* (sonics >= 2.3) */
  655. u32 PAD[3];
  656. u32 sbadmatch3; /* address match3 */
  657. u32 PAD;
  658. u32 sbadmatch2; /* address match2 */
  659. u32 PAD;
  660. u32 sbadmatch1; /* address match1 */
  661. u32 PAD[7];
  662. u32 sbimstate; /* initiator agent state */
  663. u32 sbintvec; /* interrupt mask */
  664. u32 sbtmstatelow; /* target state */
  665. u32 sbtmstatehigh; /* target state */
  666. u32 sbbwa0; /* bandwidth allocation table0 */
  667. u32 PAD;
  668. u32 sbimconfiglow; /* initiator configuration */
  669. u32 sbimconfighigh; /* initiator configuration */
  670. u32 sbadmatch0; /* address match0 */
  671. u32 PAD;
  672. u32 sbtmconfiglow; /* target configuration */
  673. u32 sbtmconfighigh; /* target configuration */
  674. u32 sbbconfig; /* broadcast configuration */
  675. u32 PAD;
  676. u32 sbbstate; /* broadcast state */
  677. u32 PAD[3];
  678. u32 sbactcnfg; /* activate configuration */
  679. u32 PAD[3];
  680. u32 sbflagst; /* current sbflags */
  681. u32 PAD[3];
  682. u32 sbidlow; /* identification */
  683. u32 sbidhigh; /* identification */
  684. };
  685. /* clkstate */
  686. #define CLK_NONE 0
  687. #define CLK_SDONLY 1
  688. #define CLK_PENDING 2 /* Not used yet */
  689. #define CLK_AVAIL 3
  690. #define BRCMF_NOPMU(brcmf) (false)
  691. #ifdef BCMDBG
  692. static int qcount[NUMPRIO];
  693. static int tx_packets[NUMPRIO];
  694. #endif /* BCMDBG */
  695. /* Deferred transmit */
  696. uint brcmf_deferred_tx = 1;
  697. module_param(brcmf_deferred_tx, uint, 0);
  698. /* Watchdog thread priority, -1 to use kernel timer */
  699. int brcmf_watchdog_prio = 97;
  700. module_param(brcmf_watchdog_prio, int, 0);
  701. /* Watchdog interval */
  702. uint brcmf_watchdog_ms = 10;
  703. module_param(brcmf_watchdog_ms, uint, 0);
  704. /* DPC thread priority, -1 to use tasklet */
  705. int brcmf_dpc_prio = 98;
  706. module_param(brcmf_dpc_prio, int, 0);
  707. #ifdef BCMDBG
  708. /* Console poll interval */
  709. uint brcmf_console_ms;
  710. module_param(brcmf_console_ms, uint, 0);
  711. #endif /* BCMDBG */
  712. /* Tx/Rx bounds */
  713. uint brcmf_txbound;
  714. uint brcmf_rxbound;
  715. uint brcmf_txminmax;
  716. /* override the RAM size if possible */
  717. #define DONGLE_MIN_MEMSIZE (128 * 1024)
  718. int brcmf_dongle_memsize;
  719. static bool brcmf_alignctl;
  720. static bool sd1idle;
  721. static bool retrydata;
  722. #define RETRYCHAN(chan) (((chan) == SDPCM_EVENT_CHANNEL) || retrydata)
  723. static const uint watermark = 8;
  724. static const uint firstread = BRCMF_FIRSTREAD;
  725. /* Retry count for register access failures */
  726. static const uint retry_limit = 2;
  727. /* Force even SD lengths (some host controllers mess up on odd bytes) */
  728. static bool forcealign;
  729. #define ALIGNMENT 4
  730. #define PKTALIGN(_p, _len, _align) \
  731. do { \
  732. uint datalign; \
  733. datalign = (unsigned long)((_p)->data); \
  734. datalign = roundup(datalign, (_align)) - datalign; \
  735. if (datalign) \
  736. skb_pull((_p), datalign); \
  737. __skb_trim((_p), (_len)); \
  738. } while (0)
  739. /* Limit on rounding up frames */
  740. static const uint max_roundup = 512;
  741. /* Try doing readahead */
  742. static bool brcmf_readahead;
  743. /* To check if there's window offered */
  744. #define DATAOK(bus) \
  745. (((u8)(bus->tx_max - bus->tx_seq) != 0) && \
  746. (((u8)(bus->tx_max - bus->tx_seq) & 0x80) == 0))
  747. /*
  748. * Reads a register in the SDIO hardware block. This block occupies a series of
  749. * adresses on the 32 bit backplane bus.
  750. */
  751. static void
  752. r_sdreg32(struct brcmf_bus *bus, u32 *regvar, u32 reg_offset, u32 *retryvar)
  753. {
  754. *retryvar = 0;
  755. do {
  756. *regvar = R_REG(bus->ci->buscorebase + reg_offset, u32);
  757. } while (brcmf_sdcard_regfail(bus->card) &&
  758. (++(*retryvar) <= retry_limit));
  759. if (*retryvar) {
  760. bus->regfails += (*retryvar-1);
  761. if (*retryvar > retry_limit) {
  762. BRCMF_ERROR(("FAILED READ %Xh\n", reg_offset));
  763. *regvar = 0;
  764. }
  765. }
  766. }
  767. static void
  768. w_sdreg32(struct brcmf_bus *bus, u32 regval, u32 reg_offset, u32 *retryvar)
  769. {
  770. *retryvar = 0;
  771. do {
  772. brcmf_sdcard_reg_write(NULL, bus->ci->buscorebase + reg_offset,
  773. sizeof(u32), regval);
  774. } while (brcmf_sdcard_regfail(bus->card) &&
  775. (++(*retryvar) <= retry_limit));
  776. if (*retryvar) {
  777. bus->regfails += (*retryvar-1);
  778. if (*retryvar > retry_limit)
  779. BRCMF_ERROR(("FAILED REGISTER WRITE"
  780. " %Xh\n", reg_offset));
  781. }
  782. }
  783. #define BRCMF_BUS SDIO_BUS
  784. #define PKT_AVAILABLE() (intstatus & I_HMB_FRAME_IND)
  785. #define HOSTINTMASK (I_HMB_SW_MASK | I_CHIPACTIVE)
  786. #ifdef SDTEST
  787. static void brcmf_sdbrcm_checkdied(struct brcmf_bus *bus, void *pkt, uint seq);
  788. static void brcmf_sdbrcm_sdtest_set(struct brcmf_bus *bus, bool start);
  789. #endif
  790. #ifdef BCMDBG
  791. static int brcmf_sdbrcm_bus_console_in(struct brcmf_pub *drvr,
  792. unsigned char *msg, uint msglen);
  793. static int brcmf_sdbrcm_checkdied(struct brcmf_bus *bus, u8 *data, uint size);
  794. static int brcmf_sdbrcm_mem_dump(struct brcmf_bus *bus);
  795. #endif /* BCMDBG */
  796. static int brcmf_sdbrcm_download_state(struct brcmf_bus *bus, bool enter);
  797. static void brcmf_sdbrcm_release(struct brcmf_bus *bus);
  798. static void brcmf_sdbrcm_release_malloc(struct brcmf_bus *bus);
  799. static void brcmf_sdbrcm_disconnect(void *ptr);
  800. static bool brcmf_sdbrcm_chipmatch(u16 chipid);
  801. static bool brcmf_sdbrcm_probe_attach(struct brcmf_bus *bus, void *card,
  802. u32 regsva, u16 devid);
  803. static bool brcmf_sdbrcm_probe_malloc(struct brcmf_bus *bus, void *card);
  804. static bool brcmf_sdbrcm_probe_init(struct brcmf_bus *bus, void *card);
  805. static void brcmf_sdbrcm_release_dongle(struct brcmf_bus *bus);
  806. static uint brcmf_process_nvram_vars(char *varbuf, uint len);
  807. static void brcmf_sdbrcm_setmemsize(struct brcmf_bus *bus, int mem_size);
  808. static int brcmf_sdbrcm_send_buf(struct brcmf_bus *bus, u32 addr, uint fn,
  809. uint flags, u8 *buf, uint nbytes,
  810. struct sk_buff *pkt,
  811. void (*complete)(void *handle, int status,
  812. bool sync_waiting),
  813. void *handle);
  814. static bool brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus, void *card);
  815. static int _brcmf_sdbrcm_download_firmware(struct brcmf_bus *bus);
  816. static int brcmf_sdbrcm_download_code_file(struct brcmf_bus *bus);
  817. static int brcmf_sdbrcm_download_nvram(struct brcmf_bus *bus);
  818. static void
  819. brcmf_sdbrcm_chip_disablecore(struct brcmf_sdio_card *card, u32 corebase);
  820. static int brcmf_sdbrcm_chip_attach(struct brcmf_bus *bus, u32 regs);
  821. static void
  822. brcmf_sdbrcm_chip_resetcore(struct brcmf_sdio_card *card, u32 corebase);
  823. static void brcmf_sdbrcm_sdiod_drive_strength_init(struct brcmf_bus *bus,
  824. u32 drivestrength);
  825. static void brcmf_sdbrcm_chip_detach(struct brcmf_bus *bus);
  826. static void brcmf_sdbrcm_wait_for_event(struct brcmf_bus *bus, bool *lockvar);
  827. static void brcmf_sdbrcm_wait_event_wakeup(struct brcmf_bus *bus);
  828. static void brcmf_sdbrcm_watchdog(unsigned long data);
  829. static int brcmf_sdbrcm_watchdog_thread(void *data);
  830. static int brcmf_sdbrcm_dpc_thread(void *data);
  831. static void brcmf_sdbrcm_dpc_tasklet(unsigned long data);
  832. static void brcmf_sdbrcm_sched_dpc(struct brcmf_bus *bus);
  833. static void brcmf_sdbrcm_sdlock(struct brcmf_bus *bus);
  834. static void brcmf_sdbrcm_sdunlock(struct brcmf_bus *bus);
  835. static int brcmf_sdbrcm_get_image(char *buf, int len, struct brcmf_bus *bus);
  836. /* Packet free applicable unconditionally for sdio and sdspi.
  837. * Conditional if bufpool was present for gspi bus.
  838. */
  839. static void brcmf_sdbrcm_pktfree2(struct brcmf_bus *bus, struct sk_buff *pkt)
  840. {
  841. if ((bus->bus != SPI_BUS) || bus->usebufpool)
  842. brcmu_pkt_buf_free_skb(pkt);
  843. }
  844. static void brcmf_sdbrcm_setmemsize(struct brcmf_bus *bus, int mem_size)
  845. {
  846. s32 min_size = DONGLE_MIN_MEMSIZE;
  847. /* Restrict the memsize to user specified limit */
  848. BRCMF_ERROR(("user: Restrict the dongle ram size to %d, min %d\n",
  849. brcmf_dongle_memsize, min_size));
  850. if ((brcmf_dongle_memsize > min_size) &&
  851. (brcmf_dongle_memsize < (s32) bus->orig_ramsize))
  852. bus->ramsize = brcmf_dongle_memsize;
  853. }
  854. static int brcmf_sdbrcm_set_siaddr_window(struct brcmf_bus *bus, u32 address)
  855. {
  856. int err = 0;
  857. brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1, SBSDIO_FUNC1_SBADDRLOW,
  858. (address >> 8) & SBSDIO_SBADDRLOW_MASK, &err);
  859. if (!err)
  860. brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
  861. SBSDIO_FUNC1_SBADDRMID,
  862. (address >> 16) & SBSDIO_SBADDRMID_MASK, &err);
  863. if (!err)
  864. brcmf_sdcard_cfg_write(bus->card, SDIO_FUNC_1,
  865. SBSDIO_FUNC1_SBADDRHIGH,
  866. (address >> 24) & SBSDIO_SBADDRHIGH_MASK,
  867. &err);
  868. return err;
  869. }
  870. /* Turn backplane clock on or off */
  871. static int brcmf_sdbrcm_htclk(struct brcmf_bus *bus, bool on, bool pendok)
  872. {
  873. int err;
  874. u8 clkctl, clkreq, devctl;
  875. struct brcmf_sdio_card *card;
  876. BRCMF_TRACE(("%s: Enter\n", __func__));
  877. clkctl = 0;
  878. card = bus->card;
  879. if (on) {
  880. /* Request HT Avail */
  881. clkreq =
  882. bus->alp_only ? SBSDIO_ALP_AVAIL_REQ : SBSDIO_HT_AVAIL_REQ;
  883. if ((bus->ci->chip == BCM4329_CHIP_ID)
  884. && (bus->ci->chiprev == 0))
  885. clkreq |= SBSDIO_FORCE_ALP;
  886. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  887. SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
  888. if (err) {
  889. BRCMF_ERROR(("%s: HT Avail request error: %d\n",
  890. __func__, err));
  891. return -EBADE;
  892. }
  893. if (pendok && ((bus->ci->buscoretype == PCMCIA_CORE_ID)
  894. && (bus->ci->buscorerev == 9))) {
  895. u32 dummy, retries;
  896. r_sdreg32(bus, &dummy,
  897. offsetof(struct sdpcmd_regs, clockctlstatus),
  898. &retries);
  899. }
  900. /* Check current status */
  901. clkctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  902. SBSDIO_FUNC1_CHIPCLKCSR, &err);
  903. if (err) {
  904. BRCMF_ERROR(("%s: HT Avail read error: %d\n",
  905. __func__, err));
  906. return -EBADE;
  907. }
  908. /* Go to pending and await interrupt if appropriate */
  909. if (!SBSDIO_CLKAV(clkctl, bus->alp_only) && pendok) {
  910. /* Allow only clock-available interrupt */
  911. devctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  912. SBSDIO_DEVICE_CTL, &err);
  913. if (err) {
  914. BRCMF_ERROR(("%s: Devctl error setting CA:"
  915. " %d\n", __func__, err));
  916. return -EBADE;
  917. }
  918. devctl |= SBSDIO_DEVCTL_CA_INT_ONLY;
  919. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  920. SBSDIO_DEVICE_CTL, devctl, &err);
  921. BRCMF_INFO(("CLKCTL: set PENDING\n"));
  922. bus->clkstate = CLK_PENDING;
  923. return 0;
  924. } else if (bus->clkstate == CLK_PENDING) {
  925. /* Cancel CA-only interrupt filter */
  926. devctl =
  927. brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  928. SBSDIO_DEVICE_CTL, &err);
  929. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  930. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  931. SBSDIO_DEVICE_CTL, devctl, &err);
  932. }
  933. /* Otherwise, wait here (polling) for HT Avail */
  934. if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  935. BRCMF_SPINWAIT_SLEEP(sdioh_spinwait_sleep,
  936. ((clkctl =
  937. brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  938. SBSDIO_FUNC1_CHIPCLKCSR,
  939. &err)),
  940. !SBSDIO_CLKAV(clkctl, bus->alp_only)),
  941. PMU_MAX_TRANSITION_DLY);
  942. }
  943. if (err) {
  944. BRCMF_ERROR(("%s: HT Avail request error: %d\n",
  945. __func__, err));
  946. return -EBADE;
  947. }
  948. if (!SBSDIO_CLKAV(clkctl, bus->alp_only)) {
  949. BRCMF_ERROR(("%s: HT Avail timeout (%d): "
  950. "clkctl 0x%02x\n", __func__,
  951. PMU_MAX_TRANSITION_DLY, clkctl));
  952. return -EBADE;
  953. }
  954. /* Mark clock available */
  955. bus->clkstate = CLK_AVAIL;
  956. BRCMF_INFO(("CLKCTL: turned ON\n"));
  957. #if defined(BCMDBG)
  958. if (bus->alp_only != true) {
  959. if (SBSDIO_ALPONLY(clkctl)) {
  960. BRCMF_ERROR(("%s: HT Clock should be on.\n",
  961. __func__));
  962. }
  963. }
  964. #endif /* defined (BCMDBG) */
  965. bus->activity = true;
  966. } else {
  967. clkreq = 0;
  968. if (bus->clkstate == CLK_PENDING) {
  969. /* Cancel CA-only interrupt filter */
  970. devctl = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  971. SBSDIO_DEVICE_CTL, &err);
  972. devctl &= ~SBSDIO_DEVCTL_CA_INT_ONLY;
  973. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  974. SBSDIO_DEVICE_CTL, devctl, &err);
  975. }
  976. bus->clkstate = CLK_SDONLY;
  977. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  978. SBSDIO_FUNC1_CHIPCLKCSR, clkreq, &err);
  979. BRCMF_INFO(("CLKCTL: turned OFF\n"));
  980. if (err) {
  981. BRCMF_ERROR(("%s: Failed access turning clock off:"
  982. " %d\n", __func__, err));
  983. return -EBADE;
  984. }
  985. }
  986. return 0;
  987. }
  988. /* Change idle/active SD state */
  989. static int brcmf_sdbrcm_sdclk(struct brcmf_bus *bus, bool on)
  990. {
  991. BRCMF_TRACE(("%s: Enter\n", __func__));
  992. if (on)
  993. bus->clkstate = CLK_SDONLY;
  994. else
  995. bus->clkstate = CLK_NONE;
  996. return 0;
  997. }
  998. /* Transition SD and backplane clock readiness */
  999. static int brcmf_sdbrcm_clkctl(struct brcmf_bus *bus, uint target, bool pendok)
  1000. {
  1001. #ifdef BCMDBG
  1002. uint oldstate = bus->clkstate;
  1003. #endif /* BCMDBG */
  1004. BRCMF_TRACE(("%s: Enter\n", __func__));
  1005. /* Early exit if we're already there */
  1006. if (bus->clkstate == target) {
  1007. if (target == CLK_AVAIL) {
  1008. brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
  1009. bus->activity = true;
  1010. }
  1011. return 0;
  1012. }
  1013. switch (target) {
  1014. case CLK_AVAIL:
  1015. /* Make sure SD clock is available */
  1016. if (bus->clkstate == CLK_NONE)
  1017. brcmf_sdbrcm_sdclk(bus, true);
  1018. /* Now request HT Avail on the backplane */
  1019. brcmf_sdbrcm_htclk(bus, true, pendok);
  1020. brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
  1021. bus->activity = true;
  1022. break;
  1023. case CLK_SDONLY:
  1024. /* Remove HT request, or bring up SD clock */
  1025. if (bus->clkstate == CLK_NONE)
  1026. brcmf_sdbrcm_sdclk(bus, true);
  1027. else if (bus->clkstate == CLK_AVAIL)
  1028. brcmf_sdbrcm_htclk(bus, false, false);
  1029. else
  1030. BRCMF_ERROR(("brcmf_sdbrcm_clkctl: request for %d -> %d"
  1031. "\n", bus->clkstate, target));
  1032. brcmf_sdbrcm_wd_timer(bus, brcmf_watchdog_ms);
  1033. break;
  1034. case CLK_NONE:
  1035. /* Make sure to remove HT request */
  1036. if (bus->clkstate == CLK_AVAIL)
  1037. brcmf_sdbrcm_htclk(bus, false, false);
  1038. /* Now remove the SD clock */
  1039. brcmf_sdbrcm_sdclk(bus, false);
  1040. brcmf_sdbrcm_wd_timer(bus, 0);
  1041. break;
  1042. }
  1043. #ifdef BCMDBG
  1044. BRCMF_INFO(("brcmf_sdbrcm_clkctl: %d -> %d\n",
  1045. oldstate, bus->clkstate));
  1046. #endif /* BCMDBG */
  1047. return 0;
  1048. }
  1049. int brcmf_sdbrcm_bussleep(struct brcmf_bus *bus, bool sleep)
  1050. {
  1051. struct brcmf_sdio_card *card = bus->card;
  1052. uint retries = 0;
  1053. BRCMF_INFO(("brcmf_sdbrcm_bussleep: request %s (currently %s)\n",
  1054. (sleep ? "SLEEP" : "WAKE"),
  1055. (bus->sleeping ? "SLEEP" : "WAKE")));
  1056. /* Done if we're already in the requested state */
  1057. if (sleep == bus->sleeping)
  1058. return 0;
  1059. /* Going to sleep: set the alarm and turn off the lights... */
  1060. if (sleep) {
  1061. /* Don't sleep if something is pending */
  1062. if (bus->dpc_sched || bus->rxskip || pktq_len(&bus->txq))
  1063. return -EBUSY;
  1064. /* Disable SDIO interrupts (no longer interested) */
  1065. brcmf_sdcard_intr_disable(bus->card);
  1066. /* Make sure the controller has the bus up */
  1067. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  1068. /* Tell device to start using OOB wakeup */
  1069. w_sdreg32(bus, SMB_USE_OOB,
  1070. offsetof(struct sdpcmd_regs, tosbmailbox), &retries);
  1071. if (retries > retry_limit)
  1072. BRCMF_ERROR(("CANNOT SIGNAL CHIP, "
  1073. "WILL NOT WAKE UP!!\n"));
  1074. /* Turn off our contribution to the HT clock request */
  1075. brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
  1076. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  1077. SBSDIO_FUNC1_CHIPCLKCSR,
  1078. SBSDIO_FORCE_HW_CLKREQ_OFF, NULL);
  1079. /* Isolate the bus */
  1080. if (bus->ci->chip != BCM4329_CHIP_ID
  1081. && bus->ci->chip != BCM4319_CHIP_ID) {
  1082. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  1083. SBSDIO_DEVICE_CTL,
  1084. SBSDIO_DEVCTL_PADS_ISO, NULL);
  1085. }
  1086. /* Change state */
  1087. bus->sleeping = true;
  1088. } else {
  1089. /* Waking up: bus power up is ok, set local state */
  1090. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  1091. SBSDIO_FUNC1_CHIPCLKCSR, 0, NULL);
  1092. /* Force pad isolation off if possible
  1093. (in case power never toggled) */
  1094. if ((bus->ci->buscoretype == PCMCIA_CORE_ID)
  1095. && (bus->ci->buscorerev >= 10))
  1096. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  1097. SBSDIO_DEVICE_CTL, 0, NULL);
  1098. /* Make sure the controller has the bus up */
  1099. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, false);
  1100. /* Send misc interrupt to indicate OOB not needed */
  1101. w_sdreg32(bus, 0, offsetof(struct sdpcmd_regs, tosbmailboxdata),
  1102. &retries);
  1103. if (retries <= retry_limit)
  1104. w_sdreg32(bus, SMB_DEV_INT,
  1105. offsetof(struct sdpcmd_regs, tosbmailbox),
  1106. &retries);
  1107. if (retries > retry_limit)
  1108. BRCMF_ERROR(("CANNOT SIGNAL CHIP TO CLEAR OOB!!\n"));
  1109. /* Make sure we have SD bus access */
  1110. brcmf_sdbrcm_clkctl(bus, CLK_SDONLY, false);
  1111. /* Change state */
  1112. bus->sleeping = false;
  1113. /* Enable interrupts again */
  1114. if (bus->intr && (bus->drvr->busstate == BRCMF_BUS_DATA)) {
  1115. bus->intdis = false;
  1116. brcmf_sdcard_intr_enable(bus->card);
  1117. }
  1118. }
  1119. return 0;
  1120. }
  1121. #define BUS_WAKE(bus) \
  1122. do { \
  1123. if ((bus)->sleeping) \
  1124. brcmf_sdbrcm_bussleep((bus), false); \
  1125. } while (0);
  1126. /* Writes a HW/SW header into the packet and sends it. */
  1127. /* Assumes: (a) header space already there, (b) caller holds lock */
  1128. static int brcmf_sdbrcm_txpkt(struct brcmf_bus *bus, struct sk_buff *pkt, uint chan,
  1129. bool free_pkt)
  1130. {
  1131. int ret;
  1132. u8 *frame;
  1133. u16 len, pad = 0;
  1134. u32 swheader;
  1135. uint retries = 0;
  1136. struct brcmf_sdio_card *card;
  1137. struct sk_buff *new;
  1138. int i;
  1139. BRCMF_TRACE(("%s: Enter\n", __func__));
  1140. card = bus->card;
  1141. if (bus->drvr->dongle_reset) {
  1142. ret = -EPERM;
  1143. goto done;
  1144. }
  1145. frame = (u8 *) (pkt->data);
  1146. /* Add alignment padding, allocate new packet if needed */
  1147. pad = ((unsigned long)frame % BRCMF_SDALIGN);
  1148. if (pad) {
  1149. if (skb_headroom(pkt) < pad) {
  1150. BRCMF_INFO(("%s: insufficient headroom %d for %d pad\n",
  1151. __func__, skb_headroom(pkt), pad));
  1152. bus->drvr->tx_realloc++;
  1153. new = brcmu_pkt_buf_get_skb(pkt->len + BRCMF_SDALIGN);
  1154. if (!new) {
  1155. BRCMF_ERROR(("%s: couldn't allocate new "
  1156. "%d-byte packet\n", __func__,
  1157. pkt->len + BRCMF_SDALIGN));
  1158. ret = -ENOMEM;
  1159. goto done;
  1160. }
  1161. PKTALIGN(new, pkt->len, BRCMF_SDALIGN);
  1162. memcpy(new->data, pkt->data, pkt->len);
  1163. if (free_pkt)
  1164. brcmu_pkt_buf_free_skb(pkt);
  1165. /* free the pkt if canned one is not used */
  1166. free_pkt = true;
  1167. pkt = new;
  1168. frame = (u8 *) (pkt->data);
  1169. /* precondition: (frame % BRCMF_SDALIGN) == 0) */
  1170. pad = 0;
  1171. } else {
  1172. skb_push(pkt, pad);
  1173. frame = (u8 *) (pkt->data);
  1174. /* precondition: pad + SDPCM_HDRLEN <= pkt->len */
  1175. memset(frame, 0, pad + SDPCM_HDRLEN);
  1176. }
  1177. }
  1178. /* precondition: pad < BRCMF_SDALIGN */
  1179. /* Hardware tag: 2 byte len followed by 2 byte ~len check (all LE) */
  1180. len = (u16) (pkt->len);
  1181. *(u16 *) frame = cpu_to_le16(len);
  1182. *(((u16 *) frame) + 1) = cpu_to_le16(~len);
  1183. /* Software tag: channel, sequence number, data offset */
  1184. swheader =
  1185. ((chan << SDPCM_CHANNEL_SHIFT) & SDPCM_CHANNEL_MASK) | bus->tx_seq |
  1186. (((pad +
  1187. SDPCM_HDRLEN) << SDPCM_DOFFSET_SHIFT) & SDPCM_DOFFSET_MASK);
  1188. put_unaligned_le32(swheader, frame + SDPCM_FRAMETAG_LEN);
  1189. put_unaligned_le32(0, frame + SDPCM_FRAMETAG_LEN + sizeof(swheader));
  1190. #ifdef BCMDBG
  1191. tx_packets[pkt->priority]++;
  1192. if (BRCMF_BYTES_ON() &&
  1193. (((BRCMF_CTL_ON() && (chan == SDPCM_CONTROL_CHANNEL)) ||
  1194. (BRCMF_DATA_ON() && (chan != SDPCM_CONTROL_CHANNEL))))) {
  1195. printk(KERN_DEBUG "Tx Frame:\n");
  1196. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, frame, len);
  1197. } else if (BRCMF_HDRS_ON()) {
  1198. printk(KERN_DEBUG "TxHdr:\n");
  1199. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET,
  1200. frame, min_t(u16, len, 16));
  1201. }
  1202. #endif
  1203. /* Raise len to next SDIO block to eliminate tail command */
  1204. if (bus->roundup && bus->blocksize && (len > bus->blocksize)) {
  1205. u16 pad = bus->blocksize - (len % bus->blocksize);
  1206. if ((pad <= bus->roundup) && (pad < bus->blocksize))
  1207. len += pad;
  1208. } else if (len % BRCMF_SDALIGN) {
  1209. len += BRCMF_SDALIGN - (len % BRCMF_SDALIGN);
  1210. }
  1211. /* Some controllers have trouble with odd bytes -- round to even */
  1212. if (forcealign && (len & (ALIGNMENT - 1))) {
  1213. len = roundup(len, ALIGNMENT);
  1214. }
  1215. do {
  1216. ret = brcmf_sdbrcm_send_buf(bus, brcmf_sdcard_cur_sbwad(card),
  1217. SDIO_FUNC_2, F2SYNC, frame, len, pkt, NULL, NULL);
  1218. bus->f2txdata++;
  1219. if (ret < 0) {
  1220. /* On failure, abort the command
  1221. and terminate the frame */
  1222. BRCMF_INFO(("%s: sdio error %d, abort command and "
  1223. "terminate frame.\n", __func__, ret));
  1224. bus->tx_sderrs++;
  1225. brcmf_sdcard_abort(card, SDIO_FUNC_2);
  1226. brcmf_sdcard_cfg_write(card, SDIO_FUNC_1,
  1227. SBSDIO_FUNC1_FRAMECTRL, SFC_WF_TERM,
  1228. NULL);
  1229. bus->f1regdata++;
  1230. for (i = 0; i < 3; i++) {
  1231. u8 hi, lo;
  1232. hi = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  1233. SBSDIO_FUNC1_WFRAMEBCHI,
  1234. NULL);
  1235. lo = brcmf_sdcard_cfg_read(card, SDIO_FUNC_1,
  1236. SBSDIO_FUNC1_WFRAMEBCLO,
  1237. NULL);
  1238. bus->f1regdata += 2;
  1239. if ((hi == 0) && (lo == 0))
  1240. break;
  1241. }
  1242. }
  1243. if (ret == 0)
  1244. bus->tx_seq = (bus->tx_seq + 1) % SDPCM_SEQUENCE_WRAP;
  1245. } while ((ret < 0) && retrydata && retries++ < TXRETRIES);
  1246. done:
  1247. /* restore pkt buffer pointer before calling tx complete routine */
  1248. skb_pull(pkt, SDPCM_HDRLEN + pad);
  1249. brcmf_sdbrcm_sdunlock(bus);
  1250. brcmf_txcomplete(bus->drvr, pkt, ret != 0);
  1251. brcmf_sdbrcm_sdlock(bus);
  1252. if (free_pkt)
  1253. brcmu_pkt_buf_free_skb(pkt);
  1254. return ret;
  1255. }
  1256. int brcmf_sdbrcm_bus_txdata(struct brcmf_bus *bus, struct sk_buff *pkt)
  1257. {
  1258. int ret = -EBADE;
  1259. uint datalen, prec;
  1260. BRCMF_TRACE(("%s: Enter\n", __func__));
  1261. datalen = pkt->len;
  1262. #ifdef SDTEST
  1263. /* Push the test header if doing loopback */
  1264. if (bus->ext_loop) {
  1265. u8 *data;
  1266. skb_push(pkt, SDPCM_TEST_HDRLEN);
  1267. data = pkt->data;
  1268. *data++ = SDPCM_TEST_ECHOREQ;
  1269. *data++ = (u8) bus->loopid++;
  1270. *data++ = (datalen >> 0);
  1271. *data++ = (datalen >> 8);
  1272. datalen += SDPCM_TEST_HDRLEN;
  1273. }
  1274. #endif /* SDTEST */
  1275. /* Add space for the header */
  1276. skb_push(pkt, SDPCM_HDRLEN);
  1277. /* precondition: IS_ALIGNED((unsigned long)(pkt->data), 2) */
  1278. prec = PRIO2PREC((pkt->priority & PRIOMASK));
  1279. /* Check for existing queue, current flow-control,
  1280. pending event, or pending clock */
  1281. if (brcmf_deferred_tx || bus->fcstate || pktq_len(&bus->txq)
  1282. || bus->dpc_sched || (!DATAOK(bus))
  1283. || (bus->flowcontrol & NBITVAL(prec))
  1284. || (bus->clkstate != CLK_AVAIL)) {
  1285. BRCMF_TRACE(("%s: deferring pktq len %d\n", __func__,
  1286. pktq_len(&bus->txq)));
  1287. bus->fcqueued++;
  1288. /* Priority based enq */
  1289. spin_lock_bh(&bus->txqlock);
  1290. if (brcmf_c_prec_enq(bus->drvr, &bus->txq, pkt, prec) == false) {
  1291. skb_pull(pkt, SDPCM_HDRLEN);
  1292. brcmf_txcomplete(bus->drvr, pkt, false);
  1293. brcmu_pkt_buf_free_skb(pkt);
  1294. BRCMF_ERROR(("%s: out of bus->txq !!!\n", __func__));
  1295. ret = -ENOSR;
  1296. } else {
  1297. ret = 0;
  1298. }
  1299. spin_unlock_bh(&bus->txqlock);
  1300. if (pktq_len(&bus->txq) >= TXHI)
  1301. brcmf_txflowcontrol(bus->drvr, 0, ON);
  1302. #ifdef BCMDBG
  1303. if (pktq_plen(&bus->txq, prec) > qcount[prec])
  1304. qcount[prec] = pktq_plen(&bus->txq, prec);
  1305. #endif
  1306. /* Schedule DPC if needed to send queued packet(s) */
  1307. if (brcmf_deferred_tx && !bus->dpc_sched) {
  1308. bus->dpc_sched = true;
  1309. brcmf_sdbrcm_sched_dpc(bus);
  1310. }
  1311. } else {
  1312. /* Lock: we're about to use shared data/code (and SDIO) */
  1313. brcmf_sdbrcm_sdlock(bus);
  1314. /* Otherwise, send it now */
  1315. BUS_WAKE(bus);
  1316. /* Make sure back plane ht clk is on, no pending allowed */
  1317. brcmf_sdbrcm_clkctl(bus, CLK_AVAIL, true);
  1318. #ifndef SDTEST
  1319. BRCMF_TRACE(("%s: calling txpkt\n", __func__));
  1320. ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
  1321. #else
  1322. ret = brcmf_sdbrcm_txpkt(bus, pkt,
  1323. (bus->ext_loop ? SDPCM_TEST_CHANNEL :
  1324. SDPCM_DATA_CHANNEL), true);
  1325. #endif
  1326. if (ret)
  1327. bus->drvr->tx_errors++;
  1328. else
  1329. bus->drvr->dstats.tx_bytes += datalen;
  1330. if (bus->idletime == BRCMF_IDLE_IMMEDIATE &&
  1331. !bus->dpc_sched) {
  1332. bus->activity = false;
  1333. brcmf_sdbrcm_clkctl(bus, CLK_NONE, true);
  1334. }
  1335. brcmf_sdbrcm_sdunlock(bus);
  1336. }
  1337. return ret;
  1338. }
  1339. static uint brcmf_sdbrcm_sendfromq(struct brcmf_bus *bus, uint maxframes)
  1340. {
  1341. struct sk_buff *pkt;
  1342. u32 intstatus = 0;
  1343. uint retries = 0;
  1344. int ret = 0, prec_out;
  1345. uint cnt = 0;
  1346. uint datalen;
  1347. u8 tx_prec_map;
  1348. struct brcmf_pub *drvr = bus->drvr;
  1349. BRCMF_TRACE(("%s: Enter\n", __func__));
  1350. tx_prec_map = ~bus->flowcontrol;
  1351. /* Send frames until the limit or some other event */
  1352. for (cnt = 0; (cnt < maxframes) && DATAOK(bus); cnt++) {
  1353. spin_lock_bh(&bus->txqlock);
  1354. pkt = brcmu_pktq_mdeq(&bus->txq, tx_prec_map, &prec_out);
  1355. if (pkt == NULL) {
  1356. spin_unlock_bh(&bus->txqlock);
  1357. break;
  1358. }
  1359. spin_unlock_bh(&bus->txqlock);
  1360. datalen = pkt->len - SDPCM_HDRLEN;
  1361. #ifndef SDTEST
  1362. ret = brcmf_sdbrcm_txpkt(bus, pkt, SDPCM_DATA_CHANNEL, true);
  1363. #else
  1364. ret = brcmf_sdbrcm_txpkt(bus, pkt,
  1365. (bus->ext_loop ? SDPCM_TEST_CHANNEL :
  1366. SDPCM_DATA_CHANNEL), true);
  1367. #endif
  1368. if (ret)
  1369. bus->drvr->tx_errors++;
  1370. else
  1371. bus->drvr->dstats.tx_bytes += datalen;
  1372. /* In poll mode, need to check for other events */
  1373. if (!bus->intr && cnt) {
  1374. /* Check device status, signal pending interrupt */
  1375. r_sdreg32(bus, &intstatus,
  1376. offsetof(struct sdpcmd_regs, intstatus),
  1377. &retries);
  1378. bus->f2txdata++;
  1379. if (brcmf_sdcard_regfail(bus->card))
  1380. break;
  1381. if (intstatus & bus->hostintmask)
  1382. bus->ipend = true;
  1383. }
  1384. }
  1385. /* Deflow-control stack if needed */
  1386. if (drvr->up && (drvr->busstate == BRCMF_BUS_DATA) &&
  1387. drvr->txoff && (pktq_len(&bus->txq) < TXLOW))
  1388. brcmf_txflowcontrol(drvr, 0, OFF);
  1389. return cnt;
  1390. }
  1391. int
  1392. brcmf_sdbrcm_bus_txctl(struct brcmf_bus *bus, unsigned char *msg, uint msglen)
  1393. {
  1394. u8 *frame;
  1395. u16 len;
  1396. u32 swheader;
  1397. uint retries = 0;
  1398. struct brcmf_sdio_card *card = bus->card;
  1399. u8 doff = 0;
  1400. int ret = -1;
  1401. int i;
  1402. BRCMF_TRACE(("%s: Enter\n", __func__));
  1403. if (bus->drvr->dongle_reset)
  1404. return -EIO;
  1405. /* Back the pointer to make a room for bus header */
  1406. frame = msg - SDPCM_HDRLEN;
  1407. len = (msglen += SDPCM_HDRLEN);
  1408. /* Add alignme…