aiutils.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. * File contents: support functions for PCI/PCIe
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/delay.h>
  20. #include <linux/pci.h>
  21. #include <defs.h>
  22. #include <chipcommon.h>
  23. #include <brcmu_utils.h>
  24. #include <brcm_hw_ids.h>
  25. #include <soc.h>
  26. #include "types.h"
  27. #include "pub.h"
  28. #include "pmu.h"
  29. #include "srom.h"
  30. #include "nicpci.h"
  31. #include "aiutils.h"
  32. /* slow_clk_ctl */
  33. /* slow clock source mask */
  34. #define SCC_SS_MASK 0x00000007
  35. /* source of slow clock is LPO */
  36. #define SCC_SS_LPO 0x00000000
  37. /* source of slow clock is crystal */
  38. #define SCC_SS_XTAL 0x00000001
  39. /* source of slow clock is PCI */
  40. #define SCC_SS_PCI 0x00000002
  41. /* LPOFreqSel, 1: 160Khz, 0: 32KHz */
  42. #define SCC_LF 0x00000200
  43. /* LPOPowerDown, 1: LPO is disabled, 0: LPO is enabled */
  44. #define SCC_LP 0x00000400
  45. /* ForceSlowClk, 1: sb/cores running on slow clock, 0: power logic control */
  46. #define SCC_FS 0x00000800
  47. /* IgnorePllOffReq, 1/0:
  48. * power logic ignores/honors PLL clock disable requests from core
  49. */
  50. #define SCC_IP 0x00001000
  51. /* XtalControlEn, 1/0:
  52. * power logic does/doesn't disable crystal when appropriate
  53. */
  54. #define SCC_XC 0x00002000
  55. /* XtalPU (RO), 1/0: crystal running/disabled */
  56. #define SCC_XP 0x00004000
  57. /* ClockDivider (SlowClk = 1/(4+divisor)) */
  58. #define SCC_CD_MASK 0xffff0000
  59. #define SCC_CD_SHIFT 16
  60. /* system_clk_ctl */
  61. /* ILPen: Enable Idle Low Power */
  62. #define SYCC_IE 0x00000001
  63. /* ALPen: Enable Active Low Power */
  64. #define SYCC_AE 0x00000002
  65. /* ForcePLLOn */
  66. #define SYCC_FP 0x00000004
  67. /* Force ALP (or HT if ALPen is not set */
  68. #define SYCC_AR 0x00000008
  69. /* Force HT */
  70. #define SYCC_HR 0x00000010
  71. /* ClkDiv (ILP = 1/(4 * (divisor + 1)) */
  72. #define SYCC_CD_MASK 0xffff0000
  73. #define SYCC_CD_SHIFT 16
  74. #define CST4329_SPROM_OTP_SEL_MASK 0x00000003
  75. /* OTP is powered up, use def. CIS, no SPROM */
  76. #define CST4329_DEFCIS_SEL 0
  77. /* OTP is powered up, SPROM is present */
  78. #define CST4329_SPROM_SEL 1
  79. /* OTP is powered up, no SPROM */
  80. #define CST4329_OTP_SEL 2
  81. /* OTP is powered down, SPROM is present */
  82. #define CST4329_OTP_PWRDN 3
  83. #define CST4329_SPI_SDIO_MODE_MASK 0x00000004
  84. #define CST4329_SPI_SDIO_MODE_SHIFT 2
  85. /* 43224 chip-specific ChipControl register bits */
  86. #define CCTRL43224_GPIO_TOGGLE 0x8000
  87. /* 12 mA drive strength */
  88. #define CCTRL_43224A0_12MA_LED_DRIVE 0x00F000F0
  89. /* 12 mA drive strength for later 43224s */
  90. #define CCTRL_43224B0_12MA_LED_DRIVE 0xF0
  91. /* 43236 Chip specific ChipStatus register bits */
  92. #define CST43236_SFLASH_MASK 0x00000040
  93. #define CST43236_OTP_MASK 0x00000080
  94. #define CST43236_HSIC_MASK 0x00000100 /* USB/HSIC */
  95. #define CST43236_BP_CLK 0x00000200 /* 120/96Mbps */
  96. #define CST43236_BOOT_MASK 0x00001800
  97. #define CST43236_BOOT_SHIFT 11
  98. #define CST43236_BOOT_FROM_SRAM 0 /* boot from SRAM, ARM in reset */
  99. #define CST43236_BOOT_FROM_ROM 1 /* boot from ROM */
  100. #define CST43236_BOOT_FROM_FLASH 2 /* boot from FLASH */
  101. #define CST43236_BOOT_FROM_INVALID 3
  102. /* 4331 chip-specific ChipControl register bits */
  103. /* 0 disable */
  104. #define CCTRL4331_BT_COEXIST (1<<0)
  105. /* 0 SECI is disabled (JTAG functional) */
  106. #define CCTRL4331_SECI (1<<1)
  107. /* 0 disable */
  108. #define CCTRL4331_EXT_LNA (1<<2)
  109. /* sprom/gpio13-15 mux */
  110. #define CCTRL4331_SPROM_GPIO13_15 (1<<3)
  111. /* 0 ext pa disable, 1 ext pa enabled */
  112. #define CCTRL4331_EXTPA_EN (1<<4)
  113. /* set drive out GPIO_CLK on sprom_cs pin */
  114. #define CCTRL4331_GPIOCLK_ON_SPROMCS (1<<5)
  115. /* use sprom_cs pin as PCIE mdio interface */
  116. #define CCTRL4331_PCIE_MDIO_ON_SPROMCS (1<<6)
  117. /* aband extpa will be at gpio2/5 and sprom_dout */
  118. #define CCTRL4331_EXTPA_ON_GPIO2_5 (1<<7)
  119. /* override core control on pipe_AuxClkEnable */
  120. #define CCTRL4331_OVR_PIPEAUXCLKEN (1<<8)
  121. /* override core control on pipe_AuxPowerDown */
  122. #define CCTRL4331_OVR_PIPEAUXPWRDOWN (1<<9)
  123. /* pcie_auxclkenable */
  124. #define CCTRL4331_PCIE_AUXCLKEN (1<<10)
  125. /* pcie_pipe_pllpowerdown */
  126. #define CCTRL4331_PCIE_PIPE_PLLDOWN (1<<11)
  127. /* enable bt_shd0 at gpio4 */
  128. #define CCTRL4331_BT_SHD0_ON_GPIO4 (1<<16)
  129. /* enable bt_shd1 at gpio5 */
  130. #define CCTRL4331_BT_SHD1_ON_GPIO5 (1<<17)
  131. /* 4331 Chip specific ChipStatus register bits */
  132. /* crystal frequency 20/40Mhz */
  133. #define CST4331_XTAL_FREQ 0x00000001
  134. #define CST4331_SPROM_PRESENT 0x00000002
  135. #define CST4331_OTP_PRESENT 0x00000004
  136. #define CST4331_LDO_RF 0x00000008
  137. #define CST4331_LDO_PAR 0x00000010
  138. /* 4319 chip-specific ChipStatus register bits */
  139. #define CST4319_SPI_CPULESSUSB 0x00000001
  140. #define CST4319_SPI_CLK_POL 0x00000002
  141. #define CST4319_SPI_CLK_PH 0x00000008
  142. /* gpio [7:6], SDIO CIS selection */
  143. #define CST4319_SPROM_OTP_SEL_MASK 0x000000c0
  144. #define CST4319_SPROM_OTP_SEL_SHIFT 6
  145. /* use default CIS, OTP is powered up */
  146. #define CST4319_DEFCIS_SEL 0x00000000
  147. /* use SPROM, OTP is powered up */
  148. #define CST4319_SPROM_SEL 0x00000040
  149. /* use OTP, OTP is powered up */
  150. #define CST4319_OTP_SEL 0x00000080
  151. /* use SPROM, OTP is powered down */
  152. #define CST4319_OTP_PWRDN 0x000000c0
  153. /* gpio [8], sdio/usb mode */
  154. #define CST4319_SDIO_USB_MODE 0x00000100
  155. #define CST4319_REMAP_SEL_MASK 0x00000600
  156. #define CST4319_ILPDIV_EN 0x00000800
  157. #define CST4319_XTAL_PD_POL 0x00001000
  158. #define CST4319_LPO_SEL 0x00002000
  159. #define CST4319_RES_INIT_MODE 0x0000c000
  160. /* PALDO is configured with external PNP */
  161. #define CST4319_PALDO_EXTPNP 0x00010000
  162. #define CST4319_CBUCK_MODE_MASK 0x00060000
  163. #define CST4319_CBUCK_MODE_BURST 0x00020000
  164. #define CST4319_CBUCK_MODE_LPBURST 0x00060000
  165. #define CST4319_RCAL_VALID 0x01000000
  166. #define CST4319_RCAL_VALUE_MASK 0x3e000000
  167. #define CST4319_RCAL_VALUE_SHIFT 25
  168. /* 4336 chip-specific ChipStatus register bits */
  169. #define CST4336_SPI_MODE_MASK 0x00000001
  170. #define CST4336_SPROM_PRESENT 0x00000002
  171. #define CST4336_OTP_PRESENT 0x00000004
  172. #define CST4336_ARMREMAP_0 0x00000008
  173. #define CST4336_ILPDIV_EN_MASK 0x00000010
  174. #define CST4336_ILPDIV_EN_SHIFT 4
  175. #define CST4336_XTAL_PD_POL_MASK 0x00000020
  176. #define CST4336_XTAL_PD_POL_SHIFT 5
  177. #define CST4336_LPO_SEL_MASK 0x00000040
  178. #define CST4336_LPO_SEL_SHIFT 6
  179. #define CST4336_RES_INIT_MODE_MASK 0x00000180
  180. #define CST4336_RES_INIT_MODE_SHIFT 7
  181. #define CST4336_CBUCK_MODE_MASK 0x00000600
  182. #define CST4336_CBUCK_MODE_SHIFT 9
  183. /* 4313 chip-specific ChipStatus register bits */
  184. #define CST4313_SPROM_PRESENT 1
  185. #define CST4313_OTP_PRESENT 2
  186. #define CST4313_SPROM_OTP_SEL_MASK 0x00000002
  187. #define CST4313_SPROM_OTP_SEL_SHIFT 0
  188. /* 4313 Chip specific ChipControl register bits */
  189. /* 12 mA drive strengh for later 4313 */
  190. #define CCTRL_4313_12MA_LED_DRIVE 0x00000007
  191. /* Manufacturer Ids */
  192. #define MFGID_ARM 0x43b
  193. #define MFGID_BRCM 0x4bf
  194. #define MFGID_MIPS 0x4a7
  195. /* Enumeration ROM registers */
  196. #define ER_EROMENTRY 0x000
  197. #define ER_REMAPCONTROL 0xe00
  198. #define ER_REMAPSELECT 0xe04
  199. #define ER_MASTERSELECT 0xe10
  200. #define ER_ITCR 0xf00
  201. #define ER_ITIP 0xf04
  202. /* Erom entries */
  203. #define ER_TAG 0xe
  204. #define ER_TAG1 0x6
  205. #define ER_VALID 1
  206. #define ER_CI 0
  207. #define ER_MP 2
  208. #define ER_ADD 4
  209. #define ER_END 0xe
  210. #define ER_BAD 0xffffffff
  211. /* EROM CompIdentA */
  212. #define CIA_MFG_MASK 0xfff00000
  213. #define CIA_MFG_SHIFT 20
  214. #define CIA_CID_MASK 0x000fff00
  215. #define CIA_CID_SHIFT 8
  216. #define CIA_CCL_MASK 0x000000f0
  217. #define CIA_CCL_SHIFT 4
  218. /* EROM CompIdentB */
  219. #define CIB_REV_MASK 0xff000000
  220. #define CIB_REV_SHIFT 24
  221. #define CIB_NSW_MASK 0x00f80000
  222. #define CIB_NSW_SHIFT 19
  223. #define CIB_NMW_MASK 0x0007c000
  224. #define CIB_NMW_SHIFT 14
  225. #define CIB_NSP_MASK 0x00003e00
  226. #define CIB_NSP_SHIFT 9
  227. #define CIB_NMP_MASK 0x000001f0
  228. #define CIB_NMP_SHIFT 4
  229. /* EROM AddrDesc */
  230. #define AD_ADDR_MASK 0xfffff000
  231. #define AD_SP_MASK 0x00000f00
  232. #define AD_SP_SHIFT 8
  233. #define AD_ST_MASK 0x000000c0
  234. #define AD_ST_SHIFT 6
  235. #define AD_ST_SLAVE 0x00000000
  236. #define AD_ST_BRIDGE 0x00000040
  237. #define AD_ST_SWRAP 0x00000080
  238. #define AD_ST_MWRAP 0x000000c0
  239. #define AD_SZ_MASK 0x00000030
  240. #define AD_SZ_SHIFT 4
  241. #define AD_SZ_4K 0x00000000
  242. #define AD_SZ_8K 0x00000010
  243. #define AD_SZ_16K 0x00000020
  244. #define AD_SZ_SZD 0x00000030
  245. #define AD_AG32 0x00000008
  246. #define AD_ADDR_ALIGN 0x00000fff
  247. #define AD_SZ_BASE 0x00001000 /* 4KB */
  248. /* EROM SizeDesc */
  249. #define SD_SZ_MASK 0xfffff000
  250. #define SD_SG32 0x00000008
  251. #define SD_SZ_ALIGN 0x00000fff
  252. /* PCI config space bit 4 for 4306c0 slow clock source */
  253. #define PCI_CFG_GPIO_SCS 0x10
  254. /* PCI config space GPIO 14 for Xtal power-up */
  255. #define PCI_CFG_GPIO_XTAL 0x40
  256. /* PCI config space GPIO 15 for PLL power-down */
  257. #define PCI_CFG_GPIO_PLL 0x80
  258. /* power control defines */
  259. #define PLL_DELAY 150 /* us pll on delay */
  260. #define FREF_DELAY 200 /* us fref change delay */
  261. #define XTAL_ON_DELAY 1000 /* us crystal power-on delay */
  262. /* resetctrl */
  263. #define AIRC_RESET 1
  264. #define NOREV -1 /* Invalid rev */
  265. /* GPIO Based LED powersave defines */
  266. #define DEFAULT_GPIO_ONTIME 10 /* Default: 10% on */
  267. #define DEFAULT_GPIO_OFFTIME 90 /* Default: 10% on */
  268. /* When Srom support present, fields in sromcontrol */
  269. #define SRC_START 0x80000000
  270. #define SRC_BUSY 0x80000000
  271. #define SRC_OPCODE 0x60000000
  272. #define SRC_OP_READ 0x00000000
  273. #define SRC_OP_WRITE 0x20000000
  274. #define SRC_OP_WRDIS 0x40000000
  275. #define SRC_OP_WREN 0x60000000
  276. #define SRC_OTPSEL 0x00000010
  277. #define SRC_LOCK 0x00000008
  278. #define SRC_SIZE_MASK 0x00000006
  279. #define SRC_SIZE_1K 0x00000000
  280. #define SRC_SIZE_4K 0x00000002
  281. #define SRC_SIZE_16K 0x00000004
  282. #define SRC_SIZE_SHIFT 1
  283. #define SRC_PRESENT 0x00000001
  284. /* External PA enable mask */
  285. #define GPIO_CTRL_EPA_EN_MASK 0x40
  286. #define DEFAULT_GPIOTIMERVAL \
  287. ((DEFAULT_GPIO_ONTIME << GPIO_ONTIME_SHIFT) | DEFAULT_GPIO_OFFTIME)
  288. #define BADIDX (SI_MAXCORES + 1)
  289. #define IS_SIM(chippkg) \
  290. ((chippkg == HDLSIM_PKG_ID) || (chippkg == HWSIM_PKG_ID))
  291. /*
  292. * Macros to disable/restore function core(D11, ENET, ILINE20, etc) interrupts
  293. * before after core switching to avoid invalid register accesss inside ISR.
  294. */
  295. #define INTR_OFF(si, intr_val) \
  296. if ((si)->intrsoff_fn && \
  297. (si)->coreid[(si)->curidx] == (si)->dev_coreid) \
  298. intr_val = (*(si)->intrsoff_fn)((si)->intr_arg)
  299. #define INTR_RESTORE(si, intr_val) \
  300. if ((si)->intrsrestore_fn && \
  301. (si)->coreid[(si)->curidx] == (si)->dev_coreid) \
  302. (*(si)->intrsrestore_fn)((si)->intr_arg, intr_val)
  303. #define PCI(sih) (ai_get_buscoretype(sih) == PCI_CORE_ID)
  304. #define PCIE(sih) (ai_get_buscoretype(sih) == PCIE_CORE_ID)
  305. #define PCI_FORCEHT(sih) (PCIE(sih) && (ai_get_chip_id(sih) == BCM4716_CHIP_ID))
  306. #ifdef BCMDBG
  307. #define SI_MSG(fmt, ...) pr_debug(fmt, ##__VA_ARGS__)
  308. #else
  309. #define SI_MSG(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  310. #endif /* BCMDBG */
  311. #define GOODCOREADDR(x, b) \
  312. (((x) >= (b)) && ((x) < ((b) + SI_MAXCORES * SI_CORE_SIZE)) && \
  313. IS_ALIGNED((x), SI_CORE_SIZE))
  314. struct aidmp {
  315. u32 oobselina30; /* 0x000 */
  316. u32 oobselina74; /* 0x004 */
  317. u32 PAD[6];
  318. u32 oobselinb30; /* 0x020 */
  319. u32 oobselinb74; /* 0x024 */
  320. u32 PAD[6];
  321. u32 oobselinc30; /* 0x040 */
  322. u32 oobselinc74; /* 0x044 */
  323. u32 PAD[6];
  324. u32 oobselind30; /* 0x060 */
  325. u32 oobselind74; /* 0x064 */
  326. u32 PAD[38];
  327. u32 oobselouta30; /* 0x100 */
  328. u32 oobselouta74; /* 0x104 */
  329. u32 PAD[6];
  330. u32 oobseloutb30; /* 0x120 */
  331. u32 oobseloutb74; /* 0x124 */
  332. u32 PAD[6];
  333. u32 oobseloutc30; /* 0x140 */
  334. u32 oobseloutc74; /* 0x144 */
  335. u32 PAD[6];
  336. u32 oobseloutd30; /* 0x160 */
  337. u32 oobseloutd74; /* 0x164 */
  338. u32 PAD[38];
  339. u32 oobsynca; /* 0x200 */
  340. u32 oobseloutaen; /* 0x204 */
  341. u32 PAD[6];
  342. u32 oobsyncb; /* 0x220 */
  343. u32 oobseloutben; /* 0x224 */
  344. u32 PAD[6];
  345. u32 oobsyncc; /* 0x240 */
  346. u32 oobseloutcen; /* 0x244 */
  347. u32 PAD[6];
  348. u32 oobsyncd; /* 0x260 */
  349. u32 oobseloutden; /* 0x264 */
  350. u32 PAD[38];
  351. u32 oobaextwidth; /* 0x300 */
  352. u32 oobainwidth; /* 0x304 */
  353. u32 oobaoutwidth; /* 0x308 */
  354. u32 PAD[5];
  355. u32 oobbextwidth; /* 0x320 */
  356. u32 oobbinwidth; /* 0x324 */
  357. u32 oobboutwidth; /* 0x328 */
  358. u32 PAD[5];
  359. u32 oobcextwidth; /* 0x340 */
  360. u32 oobcinwidth; /* 0x344 */
  361. u32 oobcoutwidth; /* 0x348 */
  362. u32 PAD[5];
  363. u32 oobdextwidth; /* 0x360 */
  364. u32 oobdinwidth; /* 0x364 */
  365. u32 oobdoutwidth; /* 0x368 */
  366. u32 PAD[37];
  367. u32 ioctrlset; /* 0x400 */
  368. u32 ioctrlclear; /* 0x404 */
  369. u32 ioctrl; /* 0x408 */
  370. u32 PAD[61];
  371. u32 iostatus; /* 0x500 */
  372. u32 PAD[127];
  373. u32 ioctrlwidth; /* 0x700 */
  374. u32 iostatuswidth; /* 0x704 */
  375. u32 PAD[62];
  376. u32 resetctrl; /* 0x800 */
  377. u32 resetstatus; /* 0x804 */
  378. u32 resetreadid; /* 0x808 */
  379. u32 resetwriteid; /* 0x80c */
  380. u32 PAD[60];
  381. u32 errlogctrl; /* 0x900 */
  382. u32 errlogdone; /* 0x904 */
  383. u32 errlogstatus; /* 0x908 */
  384. u32 errlogaddrlo; /* 0x90c */
  385. u32 errlogaddrhi; /* 0x910 */
  386. u32 errlogid; /* 0x914 */
  387. u32 errloguser; /* 0x918 */
  388. u32 errlogflags; /* 0x91c */
  389. u32 PAD[56];
  390. u32 intstatus; /* 0xa00 */
  391. u32 PAD[127];
  392. u32 config; /* 0xe00 */
  393. u32 PAD[63];
  394. u32 itcr; /* 0xf00 */
  395. u32 PAD[3];
  396. u32 itipooba; /* 0xf10 */
  397. u32 itipoobb; /* 0xf14 */
  398. u32 itipoobc; /* 0xf18 */
  399. u32 itipoobd; /* 0xf1c */
  400. u32 PAD[4];
  401. u32 itipoobaout; /* 0xf30 */
  402. u32 itipoobbout; /* 0xf34 */
  403. u32 itipoobcout; /* 0xf38 */
  404. u32 itipoobdout; /* 0xf3c */
  405. u32 PAD[4];
  406. u32 itopooba; /* 0xf50 */
  407. u32 itopoobb; /* 0xf54 */
  408. u32 itopoobc; /* 0xf58 */
  409. u32 itopoobd; /* 0xf5c */
  410. u32 PAD[4];
  411. u32 itopoobain; /* 0xf70 */
  412. u32 itopoobbin; /* 0xf74 */
  413. u32 itopoobcin; /* 0xf78 */
  414. u32 itopoobdin; /* 0xf7c */
  415. u32 PAD[4];
  416. u32 itopreset; /* 0xf90 */
  417. u32 PAD[15];
  418. u32 peripherialid4; /* 0xfd0 */
  419. u32 peripherialid5; /* 0xfd4 */
  420. u32 peripherialid6; /* 0xfd8 */
  421. u32 peripherialid7; /* 0xfdc */
  422. u32 peripherialid0; /* 0xfe0 */
  423. u32 peripherialid1; /* 0xfe4 */
  424. u32 peripherialid2; /* 0xfe8 */
  425. u32 peripherialid3; /* 0xfec */
  426. u32 componentid0; /* 0xff0 */
  427. u32 componentid1; /* 0xff4 */
  428. u32 componentid2; /* 0xff8 */
  429. u32 componentid3; /* 0xffc */
  430. };
  431. /* parse the enumeration rom to identify all cores */
  432. static void ai_scan(struct si_pub *sih, struct bcma_bus *bus)
  433. {
  434. struct si_info *sii = (struct si_info *)sih;
  435. struct bcma_device *core;
  436. uint idx;
  437. list_for_each_entry(core, &bus->cores, list) {
  438. idx = core->core_index;
  439. sii->cia[idx] = core->id.manuf << CIA_MFG_SHIFT;
  440. sii->cia[idx] |= core->id.id << CIA_CID_SHIFT;
  441. sii->cia[idx] |= core->id.class << CIA_CCL_SHIFT;
  442. sii->cib[idx] = core->id.rev << CIB_REV_SHIFT;
  443. sii->coreid[idx] = core->id.id;
  444. sii->coresba[idx] = core->addr;
  445. sii->coresba_size[idx] = 0x1000;
  446. sii->coresba2[idx] = 0;
  447. sii->coresba2_size[idx] = 0;
  448. sii->wrapba[idx] = core->wrap;
  449. sii->numcores++;
  450. }
  451. }
  452. static struct bcma_device *ai_find_bcma_core(struct si_pub *sih, uint coreidx)
  453. {
  454. struct si_info *sii = (struct si_info *)sih;
  455. struct bcma_device *core;
  456. list_for_each_entry(core, &sii->icbus->cores, list) {
  457. if (core->core_index == coreidx)
  458. return core;
  459. }
  460. return NULL;
  461. }
  462. /*
  463. * This function changes the logical "focus" to the indicated core.
  464. * Return the current core's virtual address. Since each core starts with the
  465. * same set of registers (BIST, clock control, etc), the returned address
  466. * contains the first register of this 'common' register block (not to be
  467. * confused with 'common core').
  468. */
  469. void __iomem *ai_setcoreidx(struct si_pub *sih, uint coreidx)
  470. {
  471. struct si_info *sii = (struct si_info *)sih;
  472. struct bcma_device *core;
  473. if (sii->curidx != coreidx) {
  474. core = ai_find_bcma_core(sih, coreidx);
  475. if (core == NULL)
  476. return NULL;
  477. (void)bcma_aread32(core, BCMA_IOST);
  478. sii->curidx = coreidx;
  479. }
  480. return sii->curmap;
  481. }
  482. uint ai_corerev(struct si_pub *sih)
  483. {
  484. struct si_info *sii;
  485. u32 cib;
  486. sii = (struct si_info *)sih;
  487. cib = sii->cib[sii->curidx];
  488. return (cib & CIB_REV_MASK) >> CIB_REV_SHIFT;
  489. }
  490. /* return true if PCIE capability exists in the pci config space */
  491. static bool ai_ispcie(struct si_info *sii)
  492. {
  493. u8 cap_ptr;
  494. cap_ptr =
  495. pcicore_find_pci_capability(sii->pcibus, PCI_CAP_ID_EXP, NULL,
  496. NULL);
  497. if (!cap_ptr)
  498. return false;
  499. return true;
  500. }
  501. static bool ai_buscore_prep(struct si_info *sii)
  502. {
  503. /* kludge to enable the clock on the 4306 which lacks a slowclock */
  504. if (!ai_ispcie(sii))
  505. ai_clkctl_xtal(&sii->pub, XTAL | PLL, ON);
  506. return true;
  507. }
  508. static bool
  509. ai_buscore_setup(struct si_info *sii, u32 savewin, uint *origidx)
  510. {
  511. bool pci, pcie;
  512. uint i;
  513. uint pciidx, pcieidx, pcirev, pcierev;
  514. struct chipcregs __iomem *cc;
  515. cc = ai_setcoreidx(&sii->pub, SI_CC_IDX);
  516. /* get chipcommon rev */
  517. sii->pub.ccrev = (int)ai_corerev(&sii->pub);
  518. /* get chipcommon chipstatus */
  519. if (ai_get_ccrev(&sii->pub) >= 11)
  520. sii->chipst = R_REG(&cc->chipstatus);
  521. /* get chipcommon capabilites */
  522. sii->pub.cccaps = R_REG(&cc->capabilities);
  523. /* get pmu rev and caps */
  524. if (ai_get_cccaps(&sii->pub) & CC_CAP_PMU) {
  525. sii->pub.pmucaps = R_REG(&cc->pmucapabilities);
  526. sii->pub.pmurev = sii->pub.pmucaps & PCAP_REV_MASK;
  527. }
  528. /* figure out bus/orignal core idx */
  529. sii->pub.buscoretype = NODEV_CORE_ID;
  530. sii->pub.buscorerev = NOREV;
  531. sii->buscoreidx = BADIDX;
  532. pci = pcie = false;
  533. pcirev = pcierev = NOREV;
  534. pciidx = pcieidx = BADIDX;
  535. for (i = 0; i < sii->numcores; i++) {
  536. uint cid, crev;
  537. ai_setcoreidx(&sii->pub, i);
  538. cid = ai_coreid(&sii->pub);
  539. crev = ai_corerev(&sii->pub);
  540. if (cid == PCI_CORE_ID) {
  541. pciidx = i;
  542. pcirev = crev;
  543. pci = true;
  544. } else if (cid == PCIE_CORE_ID) {
  545. pcieidx = i;
  546. pcierev = crev;
  547. pcie = true;
  548. }
  549. /* find the core idx before entering this func. */
  550. if ((savewin && (savewin == sii->coresba[i])) ||
  551. (cc == sii->regs[i]))
  552. *origidx = i;
  553. }
  554. if (pci && pcie) {
  555. if (ai_ispcie(sii))
  556. pci = false;
  557. else
  558. pcie = false;
  559. }
  560. if (pci) {
  561. sii->pub.buscoretype = PCI_CORE_ID;
  562. sii->pub.buscorerev = pcirev;
  563. sii->buscoreidx = pciidx;
  564. } else if (pcie) {
  565. sii->pub.buscoretype = PCIE_CORE_ID;
  566. sii->pub.buscorerev = pcierev;
  567. sii->buscoreidx = pcieidx;
  568. }
  569. /* fixup necessary chip/core configurations */
  570. if (!sii->pch) {
  571. sii->pch = pcicore_init(&sii->pub, sii->icbus->drv_pci.core);
  572. if (sii->pch == NULL)
  573. return false;
  574. }
  575. if (ai_pci_fixcfg(&sii->pub)) {
  576. /* si_doattach: si_pci_fixcfg failed */
  577. return false;
  578. }
  579. /* return to the original core */
  580. ai_setcoreidx(&sii->pub, *origidx);
  581. return true;
  582. }
  583. /*
  584. * get boardtype and boardrev
  585. */
  586. static __used void ai_nvram_process(struct si_info *sii)
  587. {
  588. uint w = 0;
  589. /* do a pci config read to get subsystem id and subvendor id */
  590. pci_read_config_dword(sii->pcibus, PCI_SUBSYSTEM_VENDOR_ID, &w);
  591. sii->pub.boardvendor = w & 0xffff;
  592. sii->pub.boardtype = (w >> 16) & 0xffff;
  593. }
  594. static struct si_info *ai_doattach(struct si_info *sii,
  595. struct bcma_bus *pbus)
  596. {
  597. void __iomem *regs = pbus->mmio;
  598. struct si_pub *sih = &sii->pub;
  599. u32 w, savewin;
  600. struct chipcregs __iomem *cc;
  601. uint socitype;
  602. uint origidx;
  603. memset((unsigned char *) sii, 0, sizeof(struct si_info));
  604. savewin = 0;
  605. sii->icbus = pbus;
  606. sii->buscoreidx = BADIDX;
  607. sii->pcibus = pbus->host_pci;
  608. sii->curmap = regs;
  609. sii->curwrap = sii->curmap + SI_CORE_SIZE;
  610. /* switch to Chipcommon core */
  611. bcma_read32(pbus->drv_cc.core, 0);
  612. savewin = SI_ENUM_BASE;
  613. cc = (struct chipcregs __iomem *) regs;
  614. /* bus/core/clk setup for register access */
  615. if (!ai_buscore_prep(sii))
  616. return NULL;
  617. /*
  618. * ChipID recognition.
  619. * We assume we can read chipid at offset 0 from the regs arg.
  620. * If we add other chiptypes (or if we need to support old sdio
  621. * hosts w/o chipcommon), some way of recognizing them needs to
  622. * be added here.
  623. */
  624. w = R_REG(&cc->chipid);
  625. socitype = (w & CID_TYPE_MASK) >> CID_TYPE_SHIFT;
  626. /* Might as wll fill in chip id rev & pkg */
  627. sih->chip = w & CID_ID_MASK;
  628. sih->chiprev = (w & CID_REV_MASK) >> CID_REV_SHIFT;
  629. sih->chippkg = (w & CID_PKG_MASK) >> CID_PKG_SHIFT;
  630. /* scan for cores */
  631. if (socitype == SOCI_AI) {
  632. SI_MSG("Found chip type AI (0x%08x)\n", w);
  633. /* pass chipc address instead of original core base */
  634. ai_scan(&sii->pub, pbus);
  635. } else {
  636. /* Found chip of unknown type */
  637. return NULL;
  638. }
  639. /* no cores found, bail out */
  640. if (sii->numcores == 0)
  641. return NULL;
  642. /* bus/core/clk setup */
  643. origidx = SI_CC_IDX;
  644. if (!ai_buscore_setup(sii, savewin, &origidx))
  645. goto exit;
  646. /* Init nvram from sprom/otp if they exist */
  647. if (srom_var_init(&sii->pub, cc))
  648. goto exit;
  649. ai_nvram_process(sii);
  650. /* === NVRAM, clock is ready === */
  651. cc = (struct chipcregs __iomem *) ai_setcore(sih, CC_CORE_ID, 0);
  652. W_REG(&cc->gpiopullup, 0);
  653. W_REG(&cc->gpiopulldown, 0);
  654. ai_setcoreidx(sih, origidx);
  655. /* PMU specific initializations */
  656. if (ai_get_cccaps(sih) & CC_CAP_PMU) {
  657. u32 xtalfreq;
  658. si_pmu_init(sih);
  659. si_pmu_chip_init(sih);
  660. xtalfreq = si_pmu_measure_alpclk(sih);
  661. si_pmu_pll_init(sih, xtalfreq);
  662. si_pmu_res_init(sih);
  663. si_pmu_swreg_init(sih);
  664. }
  665. /* setup the GPIO based LED powersave register */
  666. w = getintvar(sih, BRCMS_SROM_LEDDC);
  667. if (w == 0)
  668. w = DEFAULT_GPIOTIMERVAL;
  669. ai_cc_reg(sih, offsetof(struct chipcregs, gpiotimerval),
  670. ~0, w);
  671. if (PCIE(sih))
  672. pcicore_attach(sii->pch, SI_DOATTACH);
  673. if (ai_get_chip_id(sih) == BCM43224_CHIP_ID) {
  674. /*
  675. * enable 12 mA drive strenth for 43224 and
  676. * set chipControl register bit 15
  677. */
  678. if (ai_get_chiprev(sih) == 0) {
  679. SI_MSG("Applying 43224A0 WARs\n");
  680. ai_cc_reg(sih, offsetof(struct chipcregs, chipcontrol),
  681. CCTRL43224_GPIO_TOGGLE,
  682. CCTRL43224_GPIO_TOGGLE);
  683. si_pmu_chipcontrol(sih, 0, CCTRL_43224A0_12MA_LED_DRIVE,
  684. CCTRL_43224A0_12MA_LED_DRIVE);
  685. }
  686. if (ai_get_chiprev(sih) >= 1) {
  687. SI_MSG("Applying 43224B0+ WARs\n");
  688. si_pmu_chipcontrol(sih, 0, CCTRL_43224B0_12MA_LED_DRIVE,
  689. CCTRL_43224B0_12MA_LED_DRIVE);
  690. }
  691. }
  692. if (ai_get_chip_id(sih) == BCM4313_CHIP_ID) {
  693. /*
  694. * enable 12 mA drive strenth for 4313 and
  695. * set chipControl register bit 1
  696. */
  697. SI_MSG("Applying 4313 WARs\n");
  698. si_pmu_chipcontrol(sih, 0, CCTRL_4313_12MA_LED_DRIVE,
  699. CCTRL_4313_12MA_LED_DRIVE);
  700. }
  701. return sii;
  702. exit:
  703. if (sii->pch)
  704. pcicore_deinit(sii->pch);
  705. sii->pch = NULL;
  706. return NULL;
  707. }
  708. /*
  709. * Allocate a si handle and do the attach.
  710. */
  711. struct si_pub *
  712. ai_attach(struct bcma_bus *pbus)
  713. {
  714. struct si_info *sii;
  715. /* alloc struct si_info */
  716. sii = kmalloc(sizeof(struct si_info), GFP_ATOMIC);
  717. if (sii == NULL)
  718. return NULL;
  719. if (ai_doattach(sii, pbus) == NULL) {
  720. kfree(sii);
  721. return NULL;
  722. }
  723. return (struct si_pub *) sii;
  724. }
  725. /* may be called with core in reset */
  726. void ai_detach(struct si_pub *sih)
  727. {
  728. struct si_info *sii;
  729. struct si_pub *si_local = NULL;
  730. memcpy(&si_local, &sih, sizeof(struct si_pub **));
  731. sii = (struct si_info *)sih;
  732. if (sii == NULL)
  733. return;
  734. if (sii->pch)
  735. pcicore_deinit(sii->pch);
  736. sii->pch = NULL;
  737. srom_free_vars(sih);
  738. kfree(sii);
  739. }
  740. /* register driver interrupt disabling and restoring callback functions */
  741. void
  742. ai_register_intr_callback(struct si_pub *sih, void *intrsoff_fn,
  743. void *intrsrestore_fn,
  744. void *intrsenabled_fn, void *intr_arg)
  745. {
  746. struct si_info *sii;
  747. sii = (struct si_info *)sih;
  748. sii->intr_arg = intr_arg;
  749. sii->intrsoff_fn = (u32 (*)(void *)) intrsoff_fn;
  750. sii->intrsrestore_fn = (void (*) (void *, u32)) intrsrestore_fn;
  751. sii->intrsenabled_fn = (bool (*)(void *)) intrsenabled_fn;
  752. /* save current core id. when this function called, the current core
  753. * must be the core which provides driver functions(il, et, wl, etc.)
  754. */
  755. sii->dev_coreid = sii->coreid[sii->curidx];
  756. }
  757. void ai_deregister_intr_callback(struct si_pub *sih)
  758. {
  759. struct si_info *sii;
  760. sii = (struct si_info *)sih;
  761. sii->intrsoff_fn = NULL;
  762. }
  763. uint ai_coreid(struct si_pub *sih)
  764. {
  765. struct si_info *sii;
  766. sii = (struct si_info *)sih;
  767. return sii->coreid[sii->curidx];
  768. }
  769. uint ai_coreidx(struct si_pub *sih)
  770. {
  771. struct si_info *sii;
  772. sii = (struct si_info *)sih;
  773. return sii->curidx;
  774. }
  775. /* return index of coreid or BADIDX if not found */
  776. uint ai_findcoreidx(struct si_pub *sih, uint coreid, uint coreunit)
  777. {
  778. struct bcma_device *core;
  779. struct si_info *sii;
  780. uint found;
  781. sii = (struct si_info *)sih;
  782. found = 0;
  783. list_for_each_entry(core, &sii->icbus->cores, list)
  784. if (core->id.id == coreid) {
  785. if (found == coreunit)
  786. return core->core_index;
  787. found++;
  788. }
  789. return BADIDX;
  790. }
  791. /*
  792. * This function changes logical "focus" to the indicated core;
  793. * must be called with interrupts off.
  794. * Moreover, callers should keep interrupts off during switching
  795. * out of and back to d11 core.
  796. */
  797. void __iomem *ai_setcore(struct si_pub *sih, uint coreid, uint coreunit)
  798. {
  799. uint idx;
  800. idx = ai_findcoreidx(sih, coreid, coreunit);
  801. if (idx >= SI_MAXCORES)
  802. return NULL;
  803. return ai_setcoreidx(sih, idx);
  804. }
  805. /* Turn off interrupt as required by ai_setcore, before switch core */
  806. void __iomem *ai_switch_core(struct si_pub *sih, uint coreid, uint *origidx,
  807. uint *intr_val)
  808. {
  809. void __iomem *cc;
  810. struct si_info *sii;
  811. sii = (struct si_info *)sih;
  812. INTR_OFF(sii, *intr_val);
  813. *origidx = sii->curidx;
  814. cc = ai_setcore(sih, coreid, 0);
  815. return cc;
  816. }
  817. /* restore coreidx and restore interrupt */
  818. void ai_restore_core(struct si_pub *sih, uint coreid, uint intr_val)
  819. {
  820. struct si_info *sii;
  821. sii = (struct si_info *)sih;
  822. ai_setcoreidx(sih, coreid);
  823. INTR_RESTORE(sii, intr_val);
  824. }
  825. /*
  826. * Switch to 'coreidx', issue a single arbitrary 32bit register mask&set
  827. * operation, switch back to the original core, and return the new value.
  828. *
  829. * When using the silicon backplane, no fiddling with interrupts or core
  830. * switches is needed.
  831. *
  832. * Also, when using pci/pcie, we can optimize away the core switching for pci
  833. * registers and (on newer pci cores) chipcommon registers.
  834. */
  835. uint ai_cc_reg(struct si_pub *sih, uint regoff, u32 mask, u32 val)
  836. {
  837. struct bcma_device *cc;
  838. uint origidx = 0;
  839. u32 w;
  840. uint intr_val = 0;
  841. struct si_info *sii;
  842. sii = (struct si_info *)sih;
  843. cc = sii->icbus->drv_cc.core;
  844. INTR_OFF(sii, intr_val);
  845. /* save current core index */
  846. origidx = ai_coreidx(&sii->pub);
  847. /* mask and set */
  848. if (mask || val) {
  849. bcma_maskset32(cc, regoff, ~mask, val);
  850. }
  851. /* readback */
  852. w = bcma_read32(cc, regoff);
  853. /* restore core index */
  854. ai_setcoreidx(&sii->pub, origidx);
  855. INTR_RESTORE(sii, intr_val);
  856. return w;
  857. }
  858. /* return the slow clock source - LPO, XTAL, or PCI */
  859. static uint ai_slowclk_src(struct si_info *sii)
  860. {
  861. struct chipcregs __iomem *cc;
  862. u32 val;
  863. if (ai_get_ccrev(&sii->pub) < 6) {
  864. pci_read_config_dword(sii->pcibus, PCI_GPIO_OUT,
  865. &val);
  866. if (val & PCI_CFG_GPIO_SCS)
  867. return SCC_SS_PCI;
  868. return SCC_SS_XTAL;
  869. } else if (ai_get_ccrev(&sii->pub) < 10) {
  870. cc = (struct chipcregs __iomem *)
  871. ai_setcoreidx(&sii->pub, sii->curidx);
  872. return R_REG(&cc->slow_clk_ctl) & SCC_SS_MASK;
  873. } else /* Insta-clock */
  874. return SCC_SS_XTAL;
  875. }
  876. /*
  877. * return the ILP (slowclock) min or max frequency
  878. * precondition: we've established the chip has dynamic clk control
  879. */
  880. static uint ai_slowclk_freq(struct si_info *sii, bool max_freq,
  881. struct chipcregs __iomem *cc)
  882. {
  883. u32 slowclk;
  884. uint div;
  885. slowclk = ai_slowclk_src(sii);
  886. if (ai_get_ccrev(&sii->pub) < 6) {
  887. if (slowclk == SCC_SS_PCI)
  888. return max_freq ? (PCIMAXFREQ / 64)
  889. : (PCIMINFREQ / 64);
  890. else
  891. return max_freq ? (XTALMAXFREQ / 32)
  892. : (XTALMINFREQ / 32);
  893. } else if (ai_get_ccrev(&sii->pub) < 10) {
  894. div = 4 *
  895. (((R_REG(&cc->slow_clk_ctl) & SCC_CD_MASK) >>
  896. SCC_CD_SHIFT) + 1);
  897. if (slowclk == SCC_SS_LPO)
  898. return max_freq ? LPOMAXFREQ : LPOMINFREQ;
  899. else if (slowclk == SCC_SS_XTAL)
  900. return max_freq ? (XTALMAXFREQ / div)
  901. : (XTALMINFREQ / div);
  902. else if (slowclk == SCC_SS_PCI)
  903. return max_freq ? (PCIMAXFREQ / div)
  904. : (PCIMINFREQ / div);
  905. } else {
  906. /* Chipc rev 10 is InstaClock */
  907. div = R_REG(&cc->system_clk_ctl) >> SYCC_CD_SHIFT;
  908. div = 4 * (div + 1);
  909. return max_freq ? XTALMAXFREQ : (XTALMINFREQ / div);
  910. }
  911. return 0;
  912. }
  913. static void
  914. ai_clkctl_setdelay(struct si_info *sii, struct chipcregs __iomem *cc)
  915. {
  916. uint slowmaxfreq, pll_delay, slowclk;
  917. uint pll_on_delay, fref_sel_delay;
  918. pll_delay = PLL_DELAY;
  919. /*
  920. * If the slow clock is not sourced by the xtal then
  921. * add the xtal_on_delay since the xtal will also be
  922. * powered down by dynamic clk control logic.
  923. */
  924. slowclk = ai_slowclk_src(sii);
  925. if (slowclk != SCC_SS_XTAL)
  926. pll_delay += XTAL_ON_DELAY;
  927. /* Starting with 4318 it is ILP that is used for the delays */
  928. slowmaxfreq =
  929. ai_slowclk_freq(sii,
  930. (ai_get_ccrev(&sii->pub) >= 10) ? false : true, cc);
  931. pll_on_delay = ((slowmaxfreq * pll_delay) + 999999) / 1000000;
  932. fref_sel_delay = ((slowmaxfreq * FREF_DELAY) + 999999) / 1000000;
  933. W_REG(&cc->pll_on_delay, pll_on_delay);
  934. W_REG(&cc->fref_sel_delay, fref_sel_delay);
  935. }
  936. /* initialize power control delay registers */
  937. void ai_clkctl_init(struct si_pub *sih)
  938. {
  939. struct si_info *sii;
  940. uint origidx = 0;
  941. struct chipcregs __iomem *cc;
  942. if (!(ai_get_cccaps(sih) & CC_CAP_PWR_CTL))
  943. return;
  944. sii = (struct si_info *)sih;
  945. origidx = sii->curidx;
  946. cc = (struct chipcregs __iomem *)
  947. ai_setcore(sih, CC_CORE_ID, 0);
  948. if (cc == NULL)
  949. return;
  950. /* set all Instaclk chip ILP to 1 MHz */
  951. if (ai_get_ccrev(sih) >= 10)
  952. SET_REG(&cc->system_clk_ctl, SYCC_CD_MASK,
  953. (ILP_DIV_1MHZ << SYCC_CD_SHIFT));
  954. ai_clkctl_setdelay(sii, cc);
  955. ai_setcoreidx(sih, origidx);
  956. }
  957. /*
  958. * return the value suitable for writing to the
  959. * dot11 core FAST_PWRUP_DELAY register
  960. */
  961. u16 ai_clkctl_fast_pwrup_delay(struct si_pub *sih)
  962. {
  963. struct si_info *sii;
  964. uint origidx = 0;
  965. struct chipcregs __iomem *cc;
  966. uint slowminfreq;
  967. u16 fpdelay;
  968. uint intr_val = 0;
  969. sii = (struct si_info *)sih;
  970. if (ai_get_cccaps(sih) & CC_CAP_PMU) {
  971. INTR_OFF(sii, intr_val);
  972. fpdelay = si_pmu_fast_pwrup_delay(sih);
  973. INTR_RESTORE(sii, intr_val);
  974. return fpdelay;
  975. }
  976. if (!(ai_get_cccaps(sih) & CC_CAP_PWR_CTL))
  977. return 0;
  978. fpdelay = 0;
  979. origidx = sii->curidx;
  980. INTR_OFF(sii, intr_val);
  981. cc = (struct chipcregs __iomem *)
  982. ai_setcore(sih, CC_CORE_ID, 0);
  983. if (cc == NULL)
  984. goto done;
  985. slowminfreq = ai_slowclk_freq(sii, false, cc);
  986. fpdelay = (((R_REG(&cc->pll_on_delay) + 2) * 1000000) +
  987. (slowminfreq - 1)) / slowminfreq;
  988. done:
  989. ai_setcoreidx(sih, origidx);
  990. INTR_RESTORE(sii, intr_val);
  991. return fpdelay;
  992. }
  993. /* turn primary xtal and/or pll off/on */
  994. int ai_clkctl_xtal(struct si_pub *sih, uint what, bool on)
  995. {
  996. struct si_info *sii;
  997. u32 in, out, outen;
  998. sii = (struct si_info *)sih;
  999. /* pcie core doesn't have any mapping to control the xtal pu */
  1000. if (PCIE(sih))
  1001. return -1;
  1002. pci_read_config_dword(sii->pcibus, PCI_GPIO_IN, &in);
  1003. pci_read_config_dword(sii->pcibus, PCI_GPIO_OUT, &out);
  1004. pci_read_config_dword(sii->pcibus, PCI_GPIO_OUTEN, &outen);
  1005. /*
  1006. * Avoid glitching the clock if GPRS is already using it.
  1007. * We can't actually read the state of the PLLPD so we infer it
  1008. * by the value of XTAL_PU which *is* readable via gpioin.
  1009. */
  1010. if (on && (in & PCI_CFG_GPIO_XTAL))
  1011. return 0;
  1012. if (what & XTAL)
  1013. outen |= PCI_CFG_GPIO_XTAL;
  1014. if (what & PLL)
  1015. outen |= PCI_CFG_GPIO_PLL;
  1016. if (on) {
  1017. /* turn primary xtal on */
  1018. if (what & XTAL) {
  1019. out |= PCI_CFG_GPIO_XTAL;
  1020. if (what & PLL)
  1021. out |= PCI_CFG_GPIO_PLL;
  1022. pci_write_config_dword(sii->pcibus,
  1023. PCI_GPIO_OUT, out);
  1024. pci_write_config_dword(sii->pcibus,
  1025. PCI_GPIO_OUTEN, outen);
  1026. udelay(XTAL_ON_DELAY);
  1027. }
  1028. /* turn pll on */
  1029. if (what & PLL) {
  1030. out &= ~PCI_CFG_GPIO_PLL;
  1031. pci_write_config_dword(sii->pcibus,
  1032. PCI_GPIO_OUT, out);
  1033. mdelay(2);
  1034. }
  1035. } else {
  1036. if (what & XTAL)
  1037. out &= ~PCI_CFG_GPIO_XTAL;
  1038. if (what & PLL)
  1039. out |= PCI_CFG_GPIO_PLL;
  1040. pci_write_config_dword(sii->pcibus,
  1041. PCI_GPIO_OUT, out);
  1042. pci_write_config_dword(sii->pcibus,
  1043. PCI_GPIO_OUTEN, outen);
  1044. }
  1045. return 0;
  1046. }
  1047. /* clk control mechanism through chipcommon, no policy checking */
  1048. static bool _ai_clkctl_cc(struct si_info *sii, uint mode)
  1049. {
  1050. uint origidx = 0;
  1051. struct chipcregs __iomem *cc;
  1052. u32 scc;
  1053. uint intr_val = 0;
  1054. /* chipcommon cores prior to rev6 don't support dynamic clock control */
  1055. if (ai_get_ccrev(&sii->pub) < 6)
  1056. return false;
  1057. INTR_OFF(sii, intr_val);
  1058. origidx = sii->curidx;
  1059. cc = (struct chipcregs __iomem *)
  1060. ai_setcore(&sii->pub, CC_CORE_ID, 0);
  1061. if (!(ai_get_cccaps(&sii->pub) & CC_CAP_PWR_CTL) &&
  1062. (ai_get_ccrev(&sii->pub) < 20))
  1063. goto done;
  1064. switch (mode) {
  1065. case CLK_FAST: /* FORCEHT, fast (pll) clock */
  1066. if (ai_get_ccrev(&sii->pub) < 10) {
  1067. /*
  1068. * don't forget to force xtal back
  1069. * on before we clear SCC_DYN_XTAL..
  1070. */
  1071. ai_clkctl_xtal(&sii->pub, XTAL, ON);
  1072. SET_REG(&cc->slow_clk_ctl,
  1073. (SCC_XC | SCC_FS | SCC_IP), SCC_IP);
  1074. } else if (ai_get_ccrev(&sii->pub) < 20) {
  1075. OR_REG(&cc->system_clk_ctl, SYCC_HR);
  1076. } else {
  1077. OR_REG(&cc->clk_ctl_st, CCS_FORCEHT);
  1078. }
  1079. /* wait for the PLL */
  1080. if (ai_get_cccaps(&sii->pub) & CC_CAP_PMU) {
  1081. u32 htavail = CCS_HTAVAIL;
  1082. SPINWAIT(((R_REG(&cc->clk_ctl_st) & htavail)
  1083. == 0), PMU_MAX_TRANSITION_DLY);
  1084. } else {
  1085. udelay(PLL_DELAY);
  1086. }
  1087. break;
  1088. case CLK_DYNAMIC: /* enable dynamic clock control */
  1089. if (ai_get_ccrev(&sii->pub) < 10) {
  1090. scc = R_REG(&cc->slow_clk_ctl);
  1091. scc &= ~(SCC_FS | SCC_IP | SCC_XC);
  1092. if ((scc & SCC_SS_MASK) != SCC_SS_XTAL)
  1093. scc |= SCC_XC;
  1094. W_REG(&cc->slow_clk_ctl, scc);
  1095. /*
  1096. * for dynamic control, we have to
  1097. * release our xtal_pu "force on"
  1098. */
  1099. if (scc & SCC_XC)
  1100. ai_clkctl_xtal(&sii->pub, XTAL, OFF);
  1101. } else if (ai_get_ccrev(&sii->pub) < 20) {
  1102. /* Instaclock */
  1103. AND_REG(&cc->system_clk_ctl, ~SYCC_HR);
  1104. } else {
  1105. AND_REG(&cc->clk_ctl_st, ~CCS_FORCEHT);
  1106. }
  1107. break;
  1108. default:
  1109. break;
  1110. }
  1111. done:
  1112. ai_setcoreidx(&sii->pub, origidx);
  1113. INTR_RESTORE(sii, intr_val);
  1114. return mode == CLK_FAST;
  1115. }
  1116. /*
  1117. * clock control policy function throught chipcommon
  1118. *
  1119. * set dynamic clk control mode (forceslow, forcefast, dynamic)
  1120. * returns true if we are forcing fast clock
  1121. * this is a wrapper over the next internal function
  1122. * to allow flexible policy settings for outside caller
  1123. */
  1124. bool ai_clkctl_cc(struct si_pub *sih, uint mode)
  1125. {
  1126. struct si_info *sii;
  1127. sii = (struct si_info *)sih;
  1128. /* chipcommon cores prior to rev6 don't support dynamic clock control */
  1129. if (ai_get_ccrev(sih) < 6)
  1130. return false;
  1131. if (PCI_FORCEHT(sih))
  1132. return mode == CLK_FAST;
  1133. return _ai_clkctl_cc(sii, mode);
  1134. }
  1135. void ai_pci_up(struct si_pub *sih)
  1136. {
  1137. struct si_info *sii;
  1138. sii = (struct si_info *)sih;
  1139. if (PCI_FORCEHT(sih))
  1140. _ai_clkctl_cc(sii, CLK_FAST);
  1141. if (PCIE(sih))
  1142. pcicore_up(sii->pch, SI_PCIUP);
  1143. }
  1144. /* Unconfigure and/or apply various WARs when system is going to sleep mode */
  1145. void ai_pci_sleep(struct si_pub *sih)
  1146. {
  1147. struct si_info *sii;
  1148. sii = (struct si_info *)sih;
  1149. pcicore_sleep(sii->pch);
  1150. }
  1151. /* Unconfigure and/or apply various WARs when going down */
  1152. void ai_pci_down(struct si_pub *sih)
  1153. {
  1154. struct si_info *sii;
  1155. sii = (struct si_info *)sih;
  1156. /* release FORCEHT since chip is going to "down" state */
  1157. if (PCI_FORCEHT(sih))
  1158. _ai_clkctl_cc(sii, CLK_DYNAMIC);
  1159. pcicore_down(sii->pch, SI_PCIDOWN);
  1160. }
  1161. /*
  1162. * Configure the pci core for pci client (NIC) action
  1163. * coremask is the bitvec of cores by index to be enabled.
  1164. */
  1165. void ai_pci_setup(struct si_pub *sih, uint coremask)
  1166. {
  1167. struct si_info *sii;
  1168. struct sbpciregs __iomem *regs = NULL;
  1169. u32 w;
  1170. uint idx = 0;
  1171. sii = (struct si_info *)sih;
  1172. if (PCI(sih)) {
  1173. /* get current core index */
  1174. idx = sii->curidx;
  1175. /* switch over to pci core */
  1176. regs = ai_setcoreidx(sih, sii->buscoreidx);
  1177. }
  1178. /*
  1179. * Enable sb->pci interrupts. Assume
  1180. * PCI rev 2.3 support was added in pci core rev 6 and things changed..
  1181. */
  1182. if (PCIE(sih) || (PCI(sih) && (ai_get_buscorerev(sih) >= 6))) {
  1183. /* pci config write to set this core bit in PCIIntMask */
  1184. pci_read_config_dword(sii->pcibus, PCI_INT_MASK, &w);
  1185. w |= (coremask << PCI_SBIM_SHIFT);
  1186. pci_write_config_dword(sii->pcibus, PCI_INT_MASK, w);
  1187. }
  1188. if (PCI(sih)) {
  1189. pcicore_pci_setup(sii->pch);
  1190. /* switch back to previous core */
  1191. ai_setcoreidx(sih, idx);
  1192. }
  1193. }
  1194. /*
  1195. * Fixup SROMless PCI device's configuration.
  1196. * The current core may be changed upon return.
  1197. */
  1198. int ai_pci_fixcfg(struct si_pub *sih)
  1199. {
  1200. uint origidx;
  1201. void __iomem *regs = NULL;
  1202. struct si_info *sii = (struct si_info *)sih;
  1203. /* Fixup PI in SROM shadow area to enable the correct PCI core access */
  1204. /* save the current index */
  1205. origidx = ai_coreidx(&sii->pub);
  1206. /* check 'pi' is correct and fix it if not */
  1207. regs = ai_setcore(&sii->pub, ai_get_buscoretype(sih), 0);
  1208. pcicore_fixcfg(sii->pch);
  1209. /* restore the original index */
  1210. ai_setcoreidx(&sii->pub, origidx);
  1211. pcicore_hwup(sii->pch);
  1212. return 0;
  1213. }
  1214. /* mask&set gpiocontrol bits */
  1215. u32 ai_gpiocontrol(struct si_pub *sih, u32 mask, u32 val, u8 priority)
  1216. {
  1217. uint regoff;
  1218. regoff = offsetof(struct chipcregs, gpiocontrol);
  1219. return ai_cc_reg(sih, regoff, mask, val);
  1220. }
  1221. void ai_chipcontrl_epa4331(struct si_pub *sih, bool on)
  1222. {
  1223. struct si_info *sii;
  1224. struct chipcregs __iomem *cc;
  1225. uint origidx;
  1226. u32 val;
  1227. sii = (struct si_info *)sih;
  1228. origidx = ai_coreidx(sih);
  1229. cc = (struct chipcregs __iomem *) ai_setcore(sih, CC_CORE_ID, 0);
  1230. val = R_REG(&cc->chipcontrol);
  1231. if (on) {
  1232. if (ai_get_chippkg(sih) == 9 || ai_get_chippkg(sih) == 0xb)
  1233. /* Ext PA Controls for 4331 12x9 Package */
  1234. W_REG(&cc->chipcontrol, val |
  1235. CCTRL4331_EXTPA_EN |
  1236. CCTRL4331_EXTPA_ON_GPIO2_5);
  1237. else
  1238. /* Ext PA Controls for 4331 12x12 Package */
  1239. W_REG(&cc->chipcontrol,
  1240. val | CCTRL4331_EXTPA_EN);
  1241. } else {
  1242. val &= ~(CCTRL4331_EXTPA_EN | CCTRL4331_EXTPA_ON_GPIO2_5);
  1243. W_REG(&cc->chipcontrol, val);
  1244. }
  1245. ai_setcoreidx(sih, origidx);
  1246. }
  1247. /* Enable BT-COEX & Ex-PA for 4313 */
  1248. void ai_epa_4313war(struct si_pub *sih)
  1249. {
  1250. struct si_info *sii;
  1251. struct chipcregs __iomem *cc;
  1252. uint origidx;
  1253. sii = (struct si_info *)sih;
  1254. origidx = ai_coreidx(sih);
  1255. cc = ai_setcore(sih, CC_CORE_ID, 0);
  1256. /* EPA Fix */
  1257. W_REG(&cc->gpiocontrol,
  1258. R_REG(&cc->gpiocontrol) | GPIO_CTRL_EPA_EN_MASK);
  1259. ai_setcoreidx(sih, origidx);
  1260. }
  1261. /* check if the device is removed */
  1262. bool ai_deviceremoved(struct si_pub *sih)
  1263. {
  1264. u32 w;
  1265. struct si_info *sii;
  1266. sii = (struct si_info *)sih;
  1267. pci_read_config_dword(sii->pcibus, PCI_VENDOR_ID, &w);
  1268. if ((w & 0xFFFF) != PCI_VENDOR_ID_BROADCOM)
  1269. return true;
  1270. return false;
  1271. }
  1272. bool ai_is_sprom_available(struct si_pub *sih)
  1273. {
  1274. struct si_info *sii = (struct si_info *)sih;
  1275. if (ai_get_ccrev(sih) >= 31) {
  1276. uint origidx;
  1277. struct chipcregs __iomem *cc;
  1278. u32 sromctrl;
  1279. if ((ai_get_cccaps(sih) & CC_CAP_SROM) == 0)
  1280. return false;
  1281. origidx = sii->curidx;
  1282. cc = ai_setcoreidx(sih, SI_CC_IDX);
  1283. sromctrl = R_REG(&cc->sromcontrol);
  1284. ai_setcoreidx(sih, origidx);
  1285. return sromctrl & SRC_PRESENT;
  1286. }
  1287. switch (ai_get_chip_id(sih)) {
  1288. case BCM4313_CHIP_ID:
  1289. return (sii->chipst & CST4313_SPROM_PRESENT) != 0;
  1290. default:
  1291. return true;
  1292. }
  1293. }
  1294. bool ai_is_otp_disabled(struct si_pub *sih)
  1295. {
  1296. struct si_info *sii = (struct si_info *)sih;
  1297. switch (ai_get_chip_id(sih)) {
  1298. case BCM4313_CHIP_ID:
  1299. return (sii->chipst & CST4313_OTP_PRESENT) == 0;
  1300. /* These chips always have their OTP on */
  1301. case BCM43224_CHIP_ID:
  1302. case BCM43225_CHIP_ID:
  1303. default:
  1304. return false;
  1305. }
  1306. }