hndmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Utility routines for configuring different memories in Broadcom chips.
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id: $
  30. */
  31. #include <typedefs.h>
  32. #include <sbchipc.h>
  33. #include <hndsoc.h>
  34. #include <bcmdevs.h>
  35. #include <osl.h>
  36. #include <sbgci.h>
  37. #include <siutils.h>
  38. #include <bcmutils.h>
  39. #include <hndmem.h>
  40. #define IS_MEMTYPE_VALID(mem) ((mem >= MEM_SOCRAM) && (mem < MEM_MAX))
  41. #define IS_MEMCONFIG_VALID(cfg) ((cfg >= PDA_CONFIG_CLEAR) && (cfg < PDA_CONFIG_MAX))
  42. /* Returns the number of banks in a given memory */
  43. int
  44. hndmem_num_banks(si_t *sih, int mem)
  45. {
  46. uint32 savecore, mem_info;
  47. int num_banks = 0;
  48. gciregs_t *gciregs;
  49. osl_t *osh = si_osh(sih);
  50. if (!IS_MEMTYPE_VALID(mem)) {
  51. goto exit;
  52. }
  53. savecore = si_coreidx(sih);
  54. /* TODO: Check whether SOCRAM core is present or not. If not, bail out */
  55. /* In future we need to add code for TCM based chips as well */
  56. if (!si_setcore(sih, SOCRAM_CORE_ID, 0)) {
  57. goto exit;
  58. }
  59. if (sih->gcirev >= 9) {
  60. gciregs = si_setcore(sih, GCI_CORE_ID, 0);
  61. mem_info = R_REG(osh, &gciregs->wlan_mem_info);
  62. switch (mem) {
  63. case MEM_SOCRAM:
  64. num_banks = (mem_info & WLAN_MEM_INFO_REG_NUMSOCRAMBANKS_MASK) >>
  65. WLAN_MEM_INFO_REG_NUMSOCRAMBANKS_SHIFT;
  66. break;
  67. case MEM_BM:
  68. num_banks = (mem_info & WLAN_MEM_INFO_REG_NUMD11MACBM_MASK) >>
  69. WLAN_MEM_INFO_REG_NUMD11MACBM_SHIFT;
  70. break;
  71. case MEM_UCM:
  72. num_banks = (mem_info & WLAN_MEM_INFO_REG_NUMD11MACUCM_MASK) >>
  73. WLAN_MEM_INFO_REG_NUMD11MACUCM_SHIFT;
  74. break;
  75. case MEM_SHM:
  76. num_banks = (mem_info & WLAN_MEM_INFO_REG_NUMD11MACSHM_MASK) >>
  77. WLAN_MEM_INFO_REG_NUMD11MACSHM_SHIFT;
  78. break;
  79. default:
  80. ASSERT(0);
  81. break;
  82. }
  83. } else {
  84. /* TODO: Figure out bank information using SOCRAM registers */
  85. }
  86. si_setcoreidx(sih, savecore);
  87. exit:
  88. return num_banks;
  89. }
  90. /* Returns the size of a give bank in a given memory */
  91. int
  92. hndmem_bank_size(si_t *sih, hndmem_type_t mem, int bank_num)
  93. {
  94. uint32 savecore, bank_info, reg_data;
  95. int bank_sz = 0;
  96. gciregs_t *gciregs;
  97. osl_t *osh = si_osh(sih);
  98. if (!IS_MEMTYPE_VALID(mem)) {
  99. goto exit;
  100. }
  101. savecore = si_coreidx(sih);
  102. /* TODO: Check whether SOCRAM core is present or not. If not, bail out */
  103. /* In future we need to add code for TCM based chips as well */
  104. if (!si_setcore(sih, SOCRAM_CORE_ID, 0)) {
  105. goto exit;
  106. }
  107. if (sih->gcirev >= 9) {
  108. gciregs = si_setcore(sih, GCI_CORE_ID, 0);
  109. reg_data = ((mem &
  110. GCI_INDIRECT_ADDRESS_REG_GPIOINDEX_MASK) <<
  111. GCI_INDIRECT_ADDRESS_REG_GPIOINDEX_SHIFT) |
  112. ((bank_num & GCI_INDIRECT_ADDRESS_REG_REGINDEX_MASK)
  113. << GCI_INDIRECT_ADDRESS_REG_REGINDEX_SHIFT);
  114. W_REG(osh, &gciregs->gci_indirect_addr, reg_data);
  115. bank_info = R_REG(osh, &gciregs->wlan_bankxinfo);
  116. bank_sz = (bank_info & WLAN_BANKXINFO_BANK_SIZE_MASK) >>
  117. WLAN_BANKXINFO_BANK_SIZE_SHIFT;
  118. } else {
  119. /* TODO: Figure out bank size using SOCRAM registers */
  120. }
  121. si_setcoreidx(sih, savecore);
  122. exit:
  123. return bank_sz;
  124. }
  125. /* Returns the start address of given memory */
  126. uint32
  127. hndmem_mem_base(si_t *sih, hndmem_type_t mem)
  128. {
  129. uint32 savecore, base_addr = 0;
  130. /* Currently only support of SOCRAM is available in hardware */
  131. if (mem != MEM_SOCRAM) {
  132. goto exit;
  133. }
  134. savecore = si_coreidx(sih);
  135. if (si_setcore(sih, SOCRAM_CORE_ID, 0))
  136. {
  137. base_addr = si_get_slaveport_addr(sih, CORE_SLAVE_PORT_1,
  138. CORE_BASE_ADDR_0, SOCRAM_CORE_ID, 0);
  139. } else {
  140. /* TODO: Add code to get the base address of TCM */
  141. base_addr = 0;
  142. }
  143. si_setcoreidx(sih, savecore);
  144. exit:
  145. return base_addr;
  146. }
  147. #ifdef BCMDEBUG
  148. char *hndmem_type_str[] =
  149. {
  150. "SOCRAM", /* 0 */
  151. "BM", /* 1 */
  152. "UCM", /* 2 */
  153. "SHM", /* 3 */
  154. };
  155. /* Dumps the complete memory information */
  156. void
  157. hndmem_dump_meminfo_all(si_t *sih)
  158. {
  159. int mem, bank, bank_cnt, bank_sz;
  160. for (mem = MEM_SOCRAM; mem < MEM_MAX; mem++) {
  161. bank_cnt = hndmem_num_banks(sih, mem);
  162. printf("\nMemtype: %s\n", hndmem_type_str[mem]);
  163. for (bank = 0; bank < bank_cnt; bank++) {
  164. bank_sz = hndmem_bank_size(sih, mem, bank);
  165. printf("Bank-%d: %d KB\n", bank, bank_sz);
  166. }
  167. }
  168. }
  169. #endif /* BCMDEBUG */
  170. /* Configures the Sleep PDA for a particular bank for a given memory type */
  171. int
  172. hndmem_sleeppda_bank_config(si_t *sih, hndmem_type_t mem, int bank_num,
  173. hndmem_config_t config, uint32 pda)
  174. {
  175. uint32 savecore, reg_data;
  176. gciregs_t *gciregs;
  177. int err = BCME_OK;
  178. osl_t *osh = si_osh(sih);
  179. /* TODO: Check whether SOCRAM core is present or not. If not, bail out */
  180. /* In future we need to add code for TCM based chips as well */
  181. if (!si_setcore(sih, SOCRAM_CORE_ID, 0)) {
  182. err = BCME_UNSUPPORTED;
  183. goto exit;
  184. }
  185. /* Sleep PDA is supported only by GCI rev >= 9 */
  186. if (sih->gcirev < 9) {
  187. err = BCME_UNSUPPORTED;
  188. goto exit;
  189. }
  190. if (!IS_MEMTYPE_VALID(mem)) {
  191. err = BCME_BADOPTION;
  192. goto exit;
  193. }
  194. if (!IS_MEMCONFIG_VALID(config)) {
  195. err = BCME_BADOPTION;
  196. goto exit;
  197. }
  198. savecore = si_coreidx(sih);
  199. gciregs = si_setcore(sih, GCI_CORE_ID, 0);
  200. reg_data = ((mem &
  201. GCI_INDIRECT_ADDRESS_REG_GPIOINDEX_MASK) <<
  202. GCI_INDIRECT_ADDRESS_REG_GPIOINDEX_SHIFT) |
  203. ((bank_num & GCI_INDIRECT_ADDRESS_REG_REGINDEX_MASK)
  204. << GCI_INDIRECT_ADDRESS_REG_REGINDEX_SHIFT);
  205. W_REG(osh, &gciregs->gci_indirect_addr, reg_data);
  206. if (config == PDA_CONFIG_SET_PARTIAL) {
  207. W_REG(osh, &gciregs->wlan_bankxsleeppda, pda);
  208. W_REG(osh, &gciregs->wlan_bankxkill, 0);
  209. }
  210. else if (config == PDA_CONFIG_SET_FULL) {
  211. W_REG(osh, &gciregs->wlan_bankxsleeppda, WLAN_BANKX_SLEEPPDA_REG_SLEEPPDA_MASK);
  212. W_REG(osh, &gciregs->wlan_bankxkill, WLAN_BANKX_PKILL_REG_SLEEPPDA_MASK);
  213. } else {
  214. W_REG(osh, &gciregs->wlan_bankxsleeppda, 0);
  215. W_REG(osh, &gciregs->wlan_bankxkill, 0);
  216. }
  217. si_setcoreidx(sih, savecore);
  218. exit:
  219. return err;
  220. }
  221. /* Configures the Active PDA for a particular bank for a given memory type */
  222. int
  223. hndmem_activepda_bank_config(si_t *sih, hndmem_type_t mem,
  224. int bank_num, hndmem_config_t config, uint32 pda)
  225. {
  226. uint32 savecore, reg_data;
  227. gciregs_t *gciregs;
  228. int err = BCME_OK;
  229. osl_t *osh = si_osh(sih);
  230. if (!IS_MEMTYPE_VALID(mem)) {
  231. err = BCME_BADOPTION;
  232. goto exit;
  233. }
  234. if (!IS_MEMCONFIG_VALID(config)) {
  235. err = BCME_BADOPTION;
  236. goto exit;
  237. }
  238. savecore = si_coreidx(sih);
  239. /* TODO: Check whether SOCRAM core is present or not. If not, bail out */
  240. /* In future we need to add code for TCM based chips as well */
  241. if (!si_setcore(sih, SOCRAM_CORE_ID, 0)) {
  242. err = BCME_UNSUPPORTED;
  243. goto exit;
  244. }
  245. if (sih->gcirev >= 9) {
  246. gciregs = si_setcore(sih, GCI_CORE_ID, 0);
  247. reg_data = ((mem &
  248. GCI_INDIRECT_ADDRESS_REG_GPIOINDEX_MASK) <<
  249. GCI_INDIRECT_ADDRESS_REG_GPIOINDEX_SHIFT) |
  250. ((bank_num & GCI_INDIRECT_ADDRESS_REG_REGINDEX_MASK)
  251. << GCI_INDIRECT_ADDRESS_REG_REGINDEX_SHIFT);
  252. W_REG(osh, &gciregs->gci_indirect_addr, reg_data);
  253. if (config == PDA_CONFIG_SET_PARTIAL) {
  254. W_REG(osh, &gciregs->wlan_bankxactivepda, pda);
  255. }
  256. else if (config == PDA_CONFIG_SET_FULL) {
  257. W_REG(osh, &gciregs->wlan_bankxactivepda,
  258. WLAN_BANKX_SLEEPPDA_REG_SLEEPPDA_MASK);
  259. } else {
  260. W_REG(osh, &gciregs->wlan_bankxactivepda, 0);
  261. }
  262. } else {
  263. /* TODO: Configure SOCRAM PDA using SOCRAM registers */
  264. err = BCME_UNSUPPORTED;
  265. }
  266. si_setcoreidx(sih, savecore);
  267. exit:
  268. return err;
  269. }
  270. /* Configures the Sleep PDA for all the banks for a given memory type */
  271. int
  272. hndmem_sleeppda_config(si_t *sih, hndmem_type_t mem, hndmem_config_t config)
  273. {
  274. int bank;
  275. int num_banks = hndmem_num_banks(sih, mem);
  276. int err = BCME_OK;
  277. /* Sleep PDA is supported only by GCI rev >= 9 */
  278. if (sih->gcirev < 9) {
  279. err = BCME_UNSUPPORTED;
  280. goto exit;
  281. }
  282. if (!IS_MEMTYPE_VALID(mem)) {
  283. err = BCME_BADOPTION;
  284. goto exit;
  285. }
  286. if (!IS_MEMCONFIG_VALID(config)) {
  287. err = BCME_BADOPTION;
  288. goto exit;
  289. }
  290. for (bank = 0; bank < num_banks; bank++)
  291. {
  292. err = hndmem_sleeppda_bank_config(sih, mem, bank, config, 0);
  293. }
  294. exit:
  295. return err;
  296. }
  297. /* Configures the Active PDA for all the banks for a given memory type */
  298. int
  299. hndmem_activepda_config(si_t *sih, hndmem_type_t mem, hndmem_config_t config)
  300. {
  301. int bank;
  302. int num_banks = hndmem_num_banks(sih, mem);
  303. int err = BCME_OK;
  304. if (!IS_MEMTYPE_VALID(mem)) {
  305. err = BCME_BADOPTION;
  306. goto exit;
  307. }
  308. if (!IS_MEMCONFIG_VALID(config)) {
  309. err = BCME_BADOPTION;
  310. goto exit;
  311. }
  312. for (bank = 0; bank < num_banks; bank++)
  313. {
  314. err = hndmem_activepda_bank_config(sih, mem, bank, config, 0);
  315. }
  316. exit:
  317. return err;
  318. }
  319. /* Turn off/on all the possible banks in a given memory range.
  320. * Currently this works only for SOCRAM as this is restricted by HW.
  321. */
  322. int
  323. hndmem_activepda_mem_config(si_t *sih, hndmem_type_t mem, uint32 mem_start,
  324. uint32 size, hndmem_config_t config)
  325. {
  326. int bank, bank_sz, num_banks;
  327. int mem_end;
  328. int bank_start_addr, bank_end_addr;
  329. int err = BCME_OK;
  330. /* We can get bank size for only SOCRAM/TCM only. Support is not avilable
  331. * for other memories (BM, UCM and SHM)
  332. */
  333. if (mem != MEM_SOCRAM) {
  334. err = BCME_UNSUPPORTED;
  335. goto exit;
  336. }
  337. num_banks = hndmem_num_banks(sih, mem);
  338. bank_start_addr = hndmem_mem_base(sih, mem);
  339. mem_end = mem_start + size - 1;
  340. for (bank = 0; bank < num_banks; bank++)
  341. {
  342. /* Bank size is spcified in bankXinfo register in terms on KBs */
  343. bank_sz = 1024 * hndmem_bank_size(sih, mem, bank);
  344. bank_end_addr = bank_start_addr + bank_sz - 1;
  345. if (config == PDA_CONFIG_SET_FULL) {
  346. /* Check if the bank is completely overlapping with the given mem range */
  347. if ((mem_start <= bank_start_addr) && (mem_end >= bank_end_addr)) {
  348. err = hndmem_activepda_bank_config(sih, mem, bank, config, 0);
  349. }
  350. } else {
  351. /* Check if the bank is completely overlaped with the given mem range */
  352. if (((mem_start <= bank_start_addr) && (mem_end >= bank_end_addr)) ||
  353. /* Check if the bank is partially overlaped with the given range */
  354. ((mem_start <= bank_end_addr) && (mem_end >= bank_start_addr))) {
  355. err = hndmem_activepda_bank_config(sih, mem, bank, config, 0);
  356. }
  357. }
  358. bank_start_addr += bank_sz;
  359. }
  360. exit:
  361. return err;
  362. }