aiutils.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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, struct bcma_device *cc)
  510. {
  511. bool pci, pcie;
  512. uint i;
  513. uint pciidx, pcieidx, pcirev, pcierev;
  514. /* get chipcommon rev */
  515. sii->pub.ccrev = cc->id.rev;
  516. /* get chipcommon chipstatus */
  517. if (ai_get_ccrev(&sii->pub) >= 11)
  518. sii->chipst = bcma_read32(cc, CHIPCREGOFFS(chipstatus));
  519. /* get chipcommon capabilites */
  520. sii->pub.cccaps = bcma_read32(cc, CHIPCREGOFFS(capabilities));
  521. /* get pmu rev and caps */
  522. if (ai_get_cccaps(&sii->pub) & CC_CAP_PMU) {
  523. sii->pub.pmucaps = bcma_read32(cc,
  524. CHIPCREGOFFS(pmucapabilities));
  525. sii->pub.pmurev = sii->pub.pmucaps & PCAP_REV_MASK;
  526. }
  527. /* figure out bus/orignal core idx */
  528. sii->pub.buscoretype = NODEV_CORE_ID;
  529. sii->pub.buscorerev = NOREV;
  530. sii->buscoreidx = BADIDX;
  531. pci = pcie = false;
  532. pcirev = pcierev = NOREV;
  533. pciidx = pcieidx = BADIDX;
  534. for (i = 0; i < sii->numcores; i++) {
  535. uint cid, crev;
  536. ai_setcoreidx(&sii->pub, i);
  537. cid = ai_coreid(&sii->pub);
  538. crev = ai_corerev(&sii->pub);
  539. if (cid == PCI_CORE_ID) {
  540. pciidx = i;
  541. pcirev = crev;
  542. pci = true;
  543. } else if (cid == PCIE_CORE_ID) {
  544. pcieidx = i;
  545. pcierev = crev;
  546. pcie = true;
  547. }
  548. }
  549. if (pci && pcie) {
  550. if (ai_ispcie(sii))
  551. pci = false;
  552. else
  553. pcie = false;
  554. }
  555. if (pci) {
  556. sii->pub.buscoretype = PCI_CORE_ID;
  557. sii->pub.buscorerev = pcirev;
  558. sii->buscoreidx = pciidx;
  559. } else if (pcie) {
  560. sii->pub.buscoretype = PCIE_CORE_ID;
  561. sii->pub.buscorerev = pcierev;
  562. sii->buscoreidx = pcieidx;
  563. }
  564. /* fixup necessary chip/core configurations */
  565. if (!sii->pch) {
  566. sii->pch = pcicore_init(&sii->pub, sii->icbus->drv_pci.core);
  567. if (sii->pch == NULL)
  568. return false;
  569. }
  570. if (ai_pci_fixcfg(&sii->pub)) {
  571. /* si_doattach: si_pci_fixcfg failed */
  572. return false;
  573. }
  574. return true;
  575. }
  576. /*
  577. * get boardtype and boardrev
  578. */
  579. static __used void ai_nvram_process(struct si_info *sii)
  580. {
  581. uint w = 0;
  582. /* do a pci config read to get subsystem id and subvendor id */
  583. pci_read_config_dword(sii->pcibus, PCI_SUBSYSTEM_VENDOR_ID, &w);
  584. sii->pub.boardvendor = w & 0xffff;
  585. sii->pub.boardtype = (w >> 16) & 0xffff;
  586. }
  587. static struct si_info *ai_doattach(struct si_info *sii,
  588. struct bcma_bus *pbus)
  589. {
  590. void __iomem *regs = pbus->mmio;
  591. struct si_pub *sih = &sii->pub;
  592. u32 w, savewin;
  593. struct bcma_device *cc;
  594. uint socitype;
  595. memset((unsigned char *) sii, 0, sizeof(struct si_info));
  596. savewin = 0;
  597. sii->icbus = pbus;
  598. sii->buscoreidx = BADIDX;
  599. sii->pcibus = pbus->host_pci;
  600. sii->curmap = regs;
  601. sii->curwrap = sii->curmap + SI_CORE_SIZE;
  602. /* switch to Chipcommon core */
  603. cc = pbus->drv_cc.core;
  604. /* bus/core/clk setup for register access */
  605. if (!ai_buscore_prep(sii))
  606. return NULL;
  607. /*
  608. * ChipID recognition.
  609. * We assume we can read chipid at offset 0 from the regs arg.
  610. * If we add other chiptypes (or if we need to support old sdio
  611. * hosts w/o chipcommon), some way of recognizing them needs to
  612. * be added here.
  613. */
  614. w = bcma_read32(cc, CHIPCREGOFFS(chipid));
  615. socitype = (w & CID_TYPE_MASK) >> CID_TYPE_SHIFT;
  616. /* Might as wll fill in chip id rev & pkg */
  617. sih->chip = w & CID_ID_MASK;
  618. sih->chiprev = (w & CID_REV_MASK) >> CID_REV_SHIFT;
  619. sih->chippkg = (w & CID_PKG_MASK) >> CID_PKG_SHIFT;
  620. /* scan for cores */
  621. if (socitype == SOCI_AI) {
  622. SI_MSG("Found chip type AI (0x%08x)\n", w);
  623. /* pass chipc address instead of original core base */
  624. ai_scan(&sii->pub, pbus);
  625. } else {
  626. /* Found chip of unknown type */
  627. return NULL;
  628. }
  629. /* no cores found, bail out */
  630. if (sii->numcores == 0)
  631. return NULL;
  632. /* bus/core/clk setup */
  633. if (!ai_buscore_setup(sii, cc))
  634. goto exit;
  635. /* Init nvram from sprom/otp if they exist */
  636. if (srom_var_init(&sii->pub))
  637. goto exit;
  638. ai_nvram_process(sii);
  639. /* === NVRAM, clock is ready === */
  640. bcma_write32(cc, CHIPCREGOFFS(gpiopullup), 0);
  641. bcma_write32(cc, CHIPCREGOFFS(gpiopulldown), 0);
  642. /* PMU specific initializations */
  643. if (ai_get_cccaps(sih) & CC_CAP_PMU) {
  644. si_pmu_init(sih);
  645. (void)si_pmu_measure_alpclk(sih);
  646. si_pmu_res_init(sih);
  647. }
  648. /* setup the GPIO based LED powersave register */
  649. w = getintvar(sih, BRCMS_SROM_LEDDC);
  650. if (w == 0)
  651. w = DEFAULT_GPIOTIMERVAL;
  652. ai_cc_reg(sih, offsetof(struct chipcregs, gpiotimerval),
  653. ~0, w);
  654. if (PCIE(sih))
  655. pcicore_attach(sii->pch, SI_DOATTACH);
  656. if (ai_get_chip_id(sih) == BCM43224_CHIP_ID) {
  657. /*
  658. * enable 12 mA drive strenth for 43224 and
  659. * set chipControl register bit 15
  660. */
  661. if (ai_get_chiprev(sih) == 0) {
  662. SI_MSG("Applying 43224A0 WARs\n");
  663. ai_cc_reg(sih, offsetof(struct chipcregs, chipcontrol),
  664. CCTRL43224_GPIO_TOGGLE,
  665. CCTRL43224_GPIO_TOGGLE);
  666. si_pmu_chipcontrol(sih, 0, CCTRL_43224A0_12MA_LED_DRIVE,
  667. CCTRL_43224A0_12MA_LED_DRIVE);
  668. }
  669. if (ai_get_chiprev(sih) >= 1) {
  670. SI_MSG("Applying 43224B0+ WARs\n");
  671. si_pmu_chipcontrol(sih, 0, CCTRL_43224B0_12MA_LED_DRIVE,
  672. CCTRL_43224B0_12MA_LED_DRIVE);
  673. }
  674. }
  675. if (ai_get_chip_id(sih) == BCM4313_CHIP_ID) {
  676. /*
  677. * enable 12 mA drive strenth for 4313 and
  678. * set chipControl register bit 1
  679. */
  680. SI_MSG("Applying 4313 WARs\n");
  681. si_pmu_chipcontrol(sih, 0, CCTRL_4313_12MA_LED_DRIVE,
  682. CCTRL_4313_12MA_LED_DRIVE);
  683. }
  684. return sii;
  685. exit:
  686. if (sii->pch)
  687. pcicore_deinit(sii->pch);
  688. sii->pch = NULL;
  689. return NULL;
  690. }
  691. /*
  692. * Allocate a si handle and do the attach.
  693. */
  694. struct si_pub *
  695. ai_attach(struct bcma_bus *pbus)
  696. {
  697. struct si_info *sii;
  698. /* alloc struct si_info */
  699. sii = kmalloc(sizeof(struct si_info), GFP_ATOMIC);
  700. if (sii == NULL)
  701. return NULL;
  702. if (ai_doattach(sii, pbus) == NULL) {
  703. kfree(sii);
  704. return NULL;
  705. }
  706. return (struct si_pub *) sii;
  707. }
  708. /* may be called with core in reset */
  709. void ai_detach(struct si_pub *sih)
  710. {
  711. struct si_info *sii;
  712. struct si_pub *si_local = NULL;
  713. memcpy(&si_local, &sih, sizeof(struct si_pub **));
  714. sii = (struct si_info *)sih;
  715. if (sii == NULL)
  716. return;
  717. if (sii->pch)
  718. pcicore_deinit(sii->pch);
  719. sii->pch = NULL;
  720. srom_free_vars(sih);
  721. kfree(sii);
  722. }
  723. uint ai_coreid(struct si_pub *sih)
  724. {
  725. struct si_info *sii;
  726. sii = (struct si_info *)sih;
  727. return sii->coreid[sii->curidx];
  728. }
  729. uint ai_coreidx(struct si_pub *sih)
  730. {
  731. struct si_info *sii;
  732. sii = (struct si_info *)sih;
  733. return sii->curidx;
  734. }
  735. /* return index of coreid or BADIDX if not found */
  736. struct bcma_device *ai_findcore(struct si_pub *sih, u16 coreid, u16 coreunit)
  737. {
  738. struct bcma_device *core;
  739. struct si_info *sii;
  740. uint found;
  741. sii = (struct si_info *)sih;
  742. found = 0;
  743. list_for_each_entry(core, &sii->icbus->cores, list)
  744. if (core->id.id == coreid) {
  745. if (found == coreunit)
  746. return core;
  747. found++;
  748. }
  749. return NULL;
  750. }
  751. /*
  752. * This function changes logical "focus" to the indicated core;
  753. * must be called with interrupts off.
  754. * Moreover, callers should keep interrupts off during switching
  755. * out of and back to d11 core.
  756. */
  757. void __iomem *ai_setcore(struct si_pub *sih, uint coreid, uint coreunit)
  758. {
  759. struct bcma_device *core;
  760. core = ai_findcore(sih, coreid, coreunit);
  761. if (core == NULL)
  762. return NULL;
  763. return ai_setcoreidx(sih, core->core_index);
  764. }
  765. /* Turn off interrupt as required by ai_setcore, before switch core */
  766. void __iomem *ai_switch_core(struct si_pub *sih, uint coreid, uint *origidx,
  767. uint *intr_val)
  768. {
  769. void __iomem *cc;
  770. struct si_info *sii;
  771. sii = (struct si_info *)sih;
  772. INTR_OFF(sii, *intr_val);
  773. *origidx = sii->curidx;
  774. cc = ai_setcore(sih, coreid, 0);
  775. return cc;
  776. }
  777. /* restore coreidx and restore interrupt */
  778. void ai_restore_core(struct si_pub *sih, uint coreid, uint intr_val)
  779. {
  780. struct si_info *sii;
  781. sii = (struct si_info *)sih;
  782. ai_setcoreidx(sih, coreid);
  783. INTR_RESTORE(sii, intr_val);
  784. }
  785. /*
  786. * Switch to 'coreidx', issue a single arbitrary 32bit register mask&set
  787. * operation, switch back to the original core, and return the new value.
  788. *
  789. * When using the silicon backplane, no fiddling with interrupts or core
  790. * switches is needed.
  791. *
  792. * Also, when using pci/pcie, we can optimize away the core switching for pci
  793. * registers and (on newer pci cores) chipcommon registers.
  794. */
  795. uint ai_cc_reg(struct si_pub *sih, uint regoff, u32 mask, u32 val)
  796. {
  797. struct bcma_device *cc;
  798. uint origidx = 0;
  799. u32 w;
  800. struct si_info *sii;
  801. sii = (struct si_info *)sih;
  802. cc = sii->icbus->drv_cc.core;
  803. /* save current core index */
  804. origidx = ai_coreidx(&sii->pub);
  805. /* mask and set */
  806. if (mask || val) {
  807. bcma_maskset32(cc, regoff, ~mask, val);
  808. }
  809. /* readback */
  810. w = bcma_read32(cc, regoff);
  811. /* restore core index */
  812. ai_setcoreidx(&sii->pub, origidx);
  813. return w;
  814. }
  815. /* return the slow clock source - LPO, XTAL, or PCI */
  816. static uint ai_slowclk_src(struct si_pub *sih, struct bcma_device *cc)
  817. {
  818. struct si_info *sii;
  819. u32 val;
  820. sii = (struct si_info *)sih;
  821. if (ai_get_ccrev(&sii->pub) < 6) {
  822. pci_read_config_dword(sii->pcibus, PCI_GPIO_OUT,
  823. &val);
  824. if (val & PCI_CFG_GPIO_SCS)
  825. return SCC_SS_PCI;
  826. return SCC_SS_XTAL;
  827. } else if (ai_get_ccrev(&sii->pub) < 10) {
  828. return bcma_read32(cc, CHIPCREGOFFS(slow_clk_ctl)) &
  829. SCC_SS_MASK;
  830. } else /* Insta-clock */
  831. return SCC_SS_XTAL;
  832. }
  833. /*
  834. * return the ILP (slowclock) min or max frequency
  835. * precondition: we've established the chip has dynamic clk control
  836. */
  837. static uint ai_slowclk_freq(struct si_pub *sih, bool max_freq,
  838. struct bcma_device *cc)
  839. {
  840. u32 slowclk;
  841. uint div;
  842. slowclk = ai_slowclk_src(sih, cc);
  843. if (ai_get_ccrev(sih) < 6) {
  844. if (slowclk == SCC_SS_PCI)
  845. return max_freq ? (PCIMAXFREQ / 64)
  846. : (PCIMINFREQ / 64);
  847. else
  848. return max_freq ? (XTALMAXFREQ / 32)
  849. : (XTALMINFREQ / 32);
  850. } else if (ai_get_ccrev(sih) < 10) {
  851. div = 4 *
  852. (((bcma_read32(cc, CHIPCREGOFFS(slow_clk_ctl)) &
  853. SCC_CD_MASK) >> SCC_CD_SHIFT) + 1);
  854. if (slowclk == SCC_SS_LPO)
  855. return max_freq ? LPOMAXFREQ : LPOMINFREQ;
  856. else if (slowclk == SCC_SS_XTAL)
  857. return max_freq ? (XTALMAXFREQ / div)
  858. : (XTALMINFREQ / div);
  859. else if (slowclk == SCC_SS_PCI)
  860. return max_freq ? (PCIMAXFREQ / div)
  861. : (PCIMINFREQ / div);
  862. } else {
  863. /* Chipc rev 10 is InstaClock */
  864. div = bcma_read32(cc, CHIPCREGOFFS(system_clk_ctl));
  865. div = 4 * ((div >> SYCC_CD_SHIFT) + 1);
  866. return max_freq ? XTALMAXFREQ : (XTALMINFREQ / div);
  867. }
  868. return 0;
  869. }
  870. static void
  871. ai_clkctl_setdelay(struct si_pub *sih, struct bcma_device *cc)
  872. {
  873. uint slowmaxfreq, pll_delay, slowclk;
  874. uint pll_on_delay, fref_sel_delay;
  875. pll_delay = PLL_DELAY;
  876. /*
  877. * If the slow clock is not sourced by the xtal then
  878. * add the xtal_on_delay since the xtal will also be
  879. * powered down by dynamic clk control logic.
  880. */
  881. slowclk = ai_slowclk_src(sih, cc);
  882. if (slowclk != SCC_SS_XTAL)
  883. pll_delay += XTAL_ON_DELAY;
  884. /* Starting with 4318 it is ILP that is used for the delays */
  885. slowmaxfreq =
  886. ai_slowclk_freq(sih,
  887. (ai_get_ccrev(sih) >= 10) ? false : true, cc);
  888. pll_on_delay = ((slowmaxfreq * pll_delay) + 999999) / 1000000;
  889. fref_sel_delay = ((slowmaxfreq * FREF_DELAY) + 999999) / 1000000;
  890. bcma_write32(cc, CHIPCREGOFFS(pll_on_delay), pll_on_delay);
  891. bcma_write32(cc, CHIPCREGOFFS(fref_sel_delay), fref_sel_delay);
  892. }
  893. /* initialize power control delay registers */
  894. void ai_clkctl_init(struct si_pub *sih)
  895. {
  896. struct bcma_device *cc;
  897. if (!(ai_get_cccaps(sih) & CC_CAP_PWR_CTL))
  898. return;
  899. cc = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  900. if (cc == NULL)
  901. return;
  902. /* set all Instaclk chip ILP to 1 MHz */
  903. if (ai_get_ccrev(sih) >= 10)
  904. bcma_maskset32(cc, CHIPCREGOFFS(system_clk_ctl), SYCC_CD_MASK,
  905. (ILP_DIV_1MHZ << SYCC_CD_SHIFT));
  906. ai_clkctl_setdelay(sih, cc);
  907. }
  908. /*
  909. * return the value suitable for writing to the
  910. * dot11 core FAST_PWRUP_DELAY register
  911. */
  912. u16 ai_clkctl_fast_pwrup_delay(struct si_pub *sih)
  913. {
  914. struct si_info *sii;
  915. struct bcma_device *cc;
  916. uint slowminfreq;
  917. u16 fpdelay;
  918. sii = (struct si_info *)sih;
  919. if (ai_get_cccaps(sih) & CC_CAP_PMU) {
  920. fpdelay = si_pmu_fast_pwrup_delay(sih);
  921. return fpdelay;
  922. }
  923. if (!(ai_get_cccaps(sih) & CC_CAP_PWR_CTL))
  924. return 0;
  925. fpdelay = 0;
  926. cc = ai_findcore(sih, CC_CORE_ID, 0);
  927. if (cc) {
  928. slowminfreq = ai_slowclk_freq(sih, false, cc);
  929. fpdelay = (((bcma_read32(cc, CHIPCREGOFFS(pll_on_delay)) + 2)
  930. * 1000000) + (slowminfreq - 1)) / slowminfreq;
  931. }
  932. return fpdelay;
  933. }
  934. /* turn primary xtal and/or pll off/on */
  935. int ai_clkctl_xtal(struct si_pub *sih, uint what, bool on)
  936. {
  937. struct si_info *sii;
  938. u32 in, out, outen;
  939. sii = (struct si_info *)sih;
  940. /* pcie core doesn't have any mapping to control the xtal pu */
  941. if (PCIE(sih))
  942. return -1;
  943. pci_read_config_dword(sii->pcibus, PCI_GPIO_IN, &in);
  944. pci_read_config_dword(sii->pcibus, PCI_GPIO_OUT, &out);
  945. pci_read_config_dword(sii->pcibus, PCI_GPIO_OUTEN, &outen);
  946. /*
  947. * Avoid glitching the clock if GPRS is already using it.
  948. * We can't actually read the state of the PLLPD so we infer it
  949. * by the value of XTAL_PU which *is* readable via gpioin.
  950. */
  951. if (on && (in & PCI_CFG_GPIO_XTAL))
  952. return 0;
  953. if (what & XTAL)
  954. outen |= PCI_CFG_GPIO_XTAL;
  955. if (what & PLL)
  956. outen |= PCI_CFG_GPIO_PLL;
  957. if (on) {
  958. /* turn primary xtal on */
  959. if (what & XTAL) {
  960. out |= PCI_CFG_GPIO_XTAL;
  961. if (what & PLL)
  962. out |= PCI_CFG_GPIO_PLL;
  963. pci_write_config_dword(sii->pcibus,
  964. PCI_GPIO_OUT, out);
  965. pci_write_config_dword(sii->pcibus,
  966. PCI_GPIO_OUTEN, outen);
  967. udelay(XTAL_ON_DELAY);
  968. }
  969. /* turn pll on */
  970. if (what & PLL) {
  971. out &= ~PCI_CFG_GPIO_PLL;
  972. pci_write_config_dword(sii->pcibus,
  973. PCI_GPIO_OUT, out);
  974. mdelay(2);
  975. }
  976. } else {
  977. if (what & XTAL)
  978. out &= ~PCI_CFG_GPIO_XTAL;
  979. if (what & PLL)
  980. out |= PCI_CFG_GPIO_PLL;
  981. pci_write_config_dword(sii->pcibus,
  982. PCI_GPIO_OUT, out);
  983. pci_write_config_dword(sii->pcibus,
  984. PCI_GPIO_OUTEN, outen);
  985. }
  986. return 0;
  987. }
  988. /* clk control mechanism through chipcommon, no policy checking */
  989. static bool _ai_clkctl_cc(struct si_info *sii, uint mode)
  990. {
  991. struct bcma_device *cc;
  992. u32 scc;
  993. /* chipcommon cores prior to rev6 don't support dynamic clock control */
  994. if (ai_get_ccrev(&sii->pub) < 6)
  995. return false;
  996. cc = ai_findcore(&sii->pub, BCMA_CORE_CHIPCOMMON, 0);
  997. if (!(ai_get_cccaps(&sii->pub) & CC_CAP_PWR_CTL) &&
  998. (ai_get_ccrev(&sii->pub) < 20))
  999. return mode == CLK_FAST;
  1000. switch (mode) {
  1001. case CLK_FAST: /* FORCEHT, fast (pll) clock */
  1002. if (ai_get_ccrev(&sii->pub) < 10) {
  1003. /*
  1004. * don't forget to force xtal back
  1005. * on before we clear SCC_DYN_XTAL..
  1006. */
  1007. ai_clkctl_xtal(&sii->pub, XTAL, ON);
  1008. bcma_maskset32(cc, CHIPCREGOFFS(slow_clk_ctl),
  1009. (SCC_XC | SCC_FS | SCC_IP), SCC_IP);
  1010. } else if (ai_get_ccrev(&sii->pub) < 20) {
  1011. bcma_set32(cc, CHIPCREGOFFS(system_clk_ctl), SYCC_HR);
  1012. } else {
  1013. bcma_set32(cc, CHIPCREGOFFS(clk_ctl_st), CCS_FORCEHT);
  1014. }
  1015. /* wait for the PLL */
  1016. if (ai_get_cccaps(&sii->pub) & CC_CAP_PMU) {
  1017. u32 htavail = CCS_HTAVAIL;
  1018. SPINWAIT(((bcma_read32(cc, CHIPCREGOFFS(clk_ctl_st)) &
  1019. htavail) == 0), PMU_MAX_TRANSITION_DLY);
  1020. } else {
  1021. udelay(PLL_DELAY);
  1022. }
  1023. break;
  1024. case CLK_DYNAMIC: /* enable dynamic clock control */
  1025. if (ai_get_ccrev(&sii->pub) < 10) {
  1026. scc = bcma_read32(cc, CHIPCREGOFFS(slow_clk_ctl));
  1027. scc &= ~(SCC_FS | SCC_IP | SCC_XC);
  1028. if ((scc & SCC_SS_MASK) != SCC_SS_XTAL)
  1029. scc |= SCC_XC;
  1030. bcma_write32(cc, CHIPCREGOFFS(slow_clk_ctl), scc);
  1031. /*
  1032. * for dynamic control, we have to
  1033. * release our xtal_pu "force on"
  1034. */
  1035. if (scc & SCC_XC)
  1036. ai_clkctl_xtal(&sii->pub, XTAL, OFF);
  1037. } else if (ai_get_ccrev(&sii->pub) < 20) {
  1038. /* Instaclock */
  1039. bcma_mask32(cc, CHIPCREGOFFS(system_clk_ctl), ~SYCC_HR);
  1040. } else {
  1041. bcma_mask32(cc, CHIPCREGOFFS(clk_ctl_st), ~CCS_FORCEHT);
  1042. }
  1043. break;
  1044. default:
  1045. break;
  1046. }
  1047. return mode == CLK_FAST;
  1048. }
  1049. /*
  1050. * clock control policy function throught chipcommon
  1051. *
  1052. * set dynamic clk control mode (forceslow, forcefast, dynamic)
  1053. * returns true if we are forcing fast clock
  1054. * this is a wrapper over the next internal function
  1055. * to allow flexible policy settings for outside caller
  1056. */
  1057. bool ai_clkctl_cc(struct si_pub *sih, uint mode)
  1058. {
  1059. struct si_info *sii;
  1060. sii = (struct si_info *)sih;
  1061. /* chipcommon cores prior to rev6 don't support dynamic clock control */
  1062. if (ai_get_ccrev(sih) < 6)
  1063. return false;
  1064. if (PCI_FORCEHT(sih))
  1065. return mode == CLK_FAST;
  1066. return _ai_clkctl_cc(sii, mode);
  1067. }
  1068. void ai_pci_up(struct si_pub *sih)
  1069. {
  1070. struct si_info *sii;
  1071. sii = (struct si_info *)sih;
  1072. if (PCI_FORCEHT(sih))
  1073. _ai_clkctl_cc(sii, CLK_FAST);
  1074. if (PCIE(sih))
  1075. pcicore_up(sii->pch, SI_PCIUP);
  1076. }
  1077. /* Unconfigure and/or apply various WARs when system is going to sleep mode */
  1078. void ai_pci_sleep(struct si_pub *sih)
  1079. {
  1080. struct si_info *sii;
  1081. sii = (struct si_info *)sih;
  1082. pcicore_sleep(sii->pch);
  1083. }
  1084. /* Unconfigure and/or apply various WARs when going down */
  1085. void ai_pci_down(struct si_pub *sih)
  1086. {
  1087. struct si_info *sii;
  1088. sii = (struct si_info *)sih;
  1089. /* release FORCEHT since chip is going to "down" state */
  1090. if (PCI_FORCEHT(sih))
  1091. _ai_clkctl_cc(sii, CLK_DYNAMIC);
  1092. pcicore_down(sii->pch, SI_PCIDOWN);
  1093. }
  1094. /*
  1095. * Configure the pci core for pci client (NIC) action
  1096. * coremask is the bitvec of cores by index to be enabled.
  1097. */
  1098. void ai_pci_setup(struct si_pub *sih, uint coremask)
  1099. {
  1100. struct si_info *sii;
  1101. struct sbpciregs __iomem *regs = NULL;
  1102. u32 w;
  1103. uint idx = 0;
  1104. sii = (struct si_info *)sih;
  1105. if (PCI(sih)) {
  1106. /* get current core index */
  1107. idx = sii->curidx;
  1108. /* switch over to pci core */
  1109. regs = ai_setcoreidx(sih, sii->buscoreidx);
  1110. }
  1111. /*
  1112. * Enable sb->pci interrupts. Assume
  1113. * PCI rev 2.3 support was added in pci core rev 6 and things changed..
  1114. */
  1115. if (PCIE(sih) || (PCI(sih) && (ai_get_buscorerev(sih) >= 6))) {
  1116. /* pci config write to set this core bit in PCIIntMask */
  1117. pci_read_config_dword(sii->pcibus, PCI_INT_MASK, &w);
  1118. w |= (coremask << PCI_SBIM_SHIFT);
  1119. pci_write_config_dword(sii->pcibus, PCI_INT_MASK, w);
  1120. }
  1121. if (PCI(sih)) {
  1122. pcicore_pci_setup(sii->pch);
  1123. /* switch back to previous core */
  1124. ai_setcoreidx(sih, idx);
  1125. }
  1126. }
  1127. /*
  1128. * Fixup SROMless PCI device's configuration.
  1129. * The current core may be changed upon return.
  1130. */
  1131. int ai_pci_fixcfg(struct si_pub *sih)
  1132. {
  1133. uint origidx;
  1134. void __iomem *regs = NULL;
  1135. struct si_info *sii = (struct si_info *)sih;
  1136. /* Fixup PI in SROM shadow area to enable the correct PCI core access */
  1137. /* save the current index */
  1138. origidx = ai_coreidx(&sii->pub);
  1139. /* check 'pi' is correct and fix it if not */
  1140. regs = ai_setcore(&sii->pub, ai_get_buscoretype(sih), 0);
  1141. pcicore_fixcfg(sii->pch);
  1142. /* restore the original index */
  1143. ai_setcoreidx(&sii->pub, origidx);
  1144. pcicore_hwup(sii->pch);
  1145. return 0;
  1146. }
  1147. /* mask&set gpiocontrol bits */
  1148. u32 ai_gpiocontrol(struct si_pub *sih, u32 mask, u32 val, u8 priority)
  1149. {
  1150. uint regoff;
  1151. regoff = offsetof(struct chipcregs, gpiocontrol);
  1152. return ai_cc_reg(sih, regoff, mask, val);
  1153. }
  1154. void ai_chipcontrl_epa4331(struct si_pub *sih, bool on)
  1155. {
  1156. struct bcma_device *cc;
  1157. u32 val;
  1158. cc = ai_findcore(sih, CC_CORE_ID, 0);
  1159. if (on) {
  1160. if (ai_get_chippkg(sih) == 9 || ai_get_chippkg(sih) == 0xb)
  1161. /* Ext PA Controls for 4331 12x9 Package */
  1162. bcma_set32(cc, CHIPCREGOFFS(chipcontrol),
  1163. CCTRL4331_EXTPA_EN |
  1164. CCTRL4331_EXTPA_ON_GPIO2_5);
  1165. else
  1166. /* Ext PA Controls for 4331 12x12 Package */
  1167. bcma_set32(cc, CHIPCREGOFFS(chipcontrol),
  1168. CCTRL4331_EXTPA_EN);
  1169. } else {
  1170. val &= ~(CCTRL4331_EXTPA_EN | CCTRL4331_EXTPA_ON_GPIO2_5);
  1171. bcma_mask32(cc, CHIPCREGOFFS(chipcontrol),
  1172. ~(CCTRL4331_EXTPA_EN | CCTRL4331_EXTPA_ON_GPIO2_5));
  1173. }
  1174. }
  1175. /* Enable BT-COEX & Ex-PA for 4313 */
  1176. void ai_epa_4313war(struct si_pub *sih)
  1177. {
  1178. struct bcma_device *cc;
  1179. cc = ai_findcore(sih, CC_CORE_ID, 0);
  1180. /* EPA Fix */
  1181. bcma_set32(cc, CHIPCREGOFFS(gpiocontrol), GPIO_CTRL_EPA_EN_MASK);
  1182. }
  1183. /* check if the device is removed */
  1184. bool ai_deviceremoved(struct si_pub *sih)
  1185. {
  1186. u32 w;
  1187. struct si_info *sii;
  1188. sii = (struct si_info *)sih;
  1189. pci_read_config_dword(sii->pcibus, PCI_VENDOR_ID, &w);
  1190. if ((w & 0xFFFF) != PCI_VENDOR_ID_BROADCOM)
  1191. return true;
  1192. return false;
  1193. }
  1194. bool ai_is_sprom_available(struct si_pub *sih)
  1195. {
  1196. struct si_info *sii = (struct si_info *)sih;
  1197. if (ai_get_ccrev(sih) >= 31) {
  1198. struct bcma_device *cc;
  1199. u32 sromctrl;
  1200. if ((ai_get_cccaps(sih) & CC_CAP_SROM) == 0)
  1201. return false;
  1202. cc = ai_findcore(sih, BCMA_CORE_CHIPCOMMON, 0);
  1203. sromctrl = bcma_read32(cc, CHIPCREGOFFS(sromcontrol));
  1204. return sromctrl & SRC_PRESENT;
  1205. }
  1206. switch (ai_get_chip_id(sih)) {
  1207. case BCM4313_CHIP_ID:
  1208. return (sii->chipst & CST4313_SPROM_PRESENT) != 0;
  1209. default:
  1210. return true;
  1211. }
  1212. }
  1213. bool ai_is_otp_disabled(struct si_pub *sih)
  1214. {
  1215. struct si_info *sii = (struct si_info *)sih;
  1216. switch (ai_get_chip_id(sih)) {
  1217. case BCM4313_CHIP_ID:
  1218. return (sii->chipst & CST4313_OTP_PRESENT) == 0;
  1219. /* These chips always have their OTP on */
  1220. case BCM43224_CHIP_ID:
  1221. case BCM43225_CHIP_ID:
  1222. default:
  1223. return false;
  1224. }
  1225. }