hsdk.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Synopsys, Inc. All rights reserved.
  4. * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
  5. */
  6. #include <common.h>
  7. #include <config.h>
  8. #include <linux/printk.h>
  9. #include <linux/kernel.h>
  10. #include <linux/io.h>
  11. #include <asm/arcregs.h>
  12. #include <fdt_support.h>
  13. #include <dwmmc.h>
  14. #include <malloc.h>
  15. #include <usb.h>
  16. #include "clk-lib.h"
  17. #include "env-lib.h"
  18. DECLARE_GLOBAL_DATA_PTR;
  19. #define ALL_CPU_MASK GENMASK(NR_CPUS - 1, 0)
  20. #define MASTER_CPU_ID 0
  21. #define APERTURE_SHIFT 28
  22. #define NO_CCM 0x10
  23. #define SLAVE_CPU_READY 0x12345678
  24. #define BOOTSTAGE_1 1 /* after SP, FP setup, before HW init */
  25. #define BOOTSTAGE_2 2 /* after HW init, before self halt */
  26. #define BOOTSTAGE_3 3 /* after self halt */
  27. #define BOOTSTAGE_4 4 /* before app launch */
  28. #define BOOTSTAGE_5 5 /* after app launch, unreachable */
  29. #define RESET_VECTOR_ADDR 0x0
  30. #define CREG_BASE (ARC_PERIPHERAL_BASE + 0x1000)
  31. #define CREG_CPU_START (CREG_BASE + 0x400)
  32. #define CREG_CPU_START_MASK 0xF
  33. #define SDIO_BASE (ARC_PERIPHERAL_BASE + 0xA000)
  34. #define SDIO_UHS_REG_EXT (SDIO_BASE + 0x108)
  35. #define SDIO_UHS_REG_EXT_DIV_2 (2 << 30)
  36. /* Uncached access macros */
  37. #define arc_read_uncached_32(ptr) \
  38. ({ \
  39. unsigned int __ret; \
  40. __asm__ __volatile__( \
  41. " ld.di %0, [%1] \n" \
  42. : "=r"(__ret) \
  43. : "r"(ptr)); \
  44. __ret; \
  45. })
  46. #define arc_write_uncached_32(ptr, data)\
  47. ({ \
  48. __asm__ __volatile__( \
  49. " st.di %0, [%1] \n" \
  50. : \
  51. : "r"(data), "r"(ptr)); \
  52. })
  53. struct hsdk_env_core_ctl {
  54. u32_env entry[NR_CPUS];
  55. u32_env iccm[NR_CPUS];
  56. u32_env dccm[NR_CPUS];
  57. };
  58. struct hsdk_env_common_ctl {
  59. bool halt_on_boot;
  60. u32_env core_mask;
  61. u32_env cpu_freq;
  62. u32_env axi_freq;
  63. u32_env tun_freq;
  64. u32_env nvlim;
  65. u32_env icache;
  66. u32_env dcache;
  67. };
  68. /*
  69. * Uncached cross-cpu structure. All CPUs must access to this structure fields
  70. * only with arc_read_uncached_32() / arc_write_uncached_32() accessors (which
  71. * implement ld.di / st.di instructions). Simultaneous cached and uncached
  72. * access to this area will lead to data loss.
  73. * We flush all data caches in board_early_init_r() as we don't want to have
  74. * any dirty line in L1d$ or SL$ in this area.
  75. */
  76. struct hsdk_cross_cpu {
  77. /* slave CPU ready flag */
  78. u32 ready_flag;
  79. /* address of the area, which can be used for stack by slave CPU */
  80. u32 stack_ptr;
  81. /* slave CPU status - bootstage number */
  82. s32 status[NR_CPUS];
  83. /*
  84. * Slave CPU data - it is copy of corresponding fields in
  85. * hsdk_env_core_ctl and hsdk_env_common_ctl structures which are
  86. * required for slave CPUs initialization.
  87. * This fields can be populated by copying from hsdk_env_core_ctl
  88. * and hsdk_env_common_ctl structures with sync_cross_cpu_data()
  89. * function.
  90. */
  91. u32 entry[NR_CPUS];
  92. u32 iccm[NR_CPUS];
  93. u32 dccm[NR_CPUS];
  94. u32 core_mask;
  95. u32 icache;
  96. u32 dcache;
  97. u8 cache_padding[ARCH_DMA_MINALIGN];
  98. } __aligned(ARCH_DMA_MINALIGN);
  99. /* Place for slave CPUs temporary stack */
  100. static u32 slave_stack[256 * NR_CPUS] __aligned(ARCH_DMA_MINALIGN);
  101. static struct hsdk_env_common_ctl env_common = {};
  102. static struct hsdk_env_core_ctl env_core = {};
  103. static struct hsdk_cross_cpu cross_cpu_data;
  104. static const struct env_map_common env_map_common[] = {
  105. { "core_mask", ENV_HEX, true, 0x1, 0xF, &env_common.core_mask },
  106. { "non_volatile_limit", ENV_HEX, true, 0, 0xF, &env_common.nvlim },
  107. { "icache_ena", ENV_HEX, true, 0, 1, &env_common.icache },
  108. { "dcache_ena", ENV_HEX, true, 0, 1, &env_common.dcache },
  109. {}
  110. };
  111. static const struct env_map_common env_map_clock[] = {
  112. { "cpu_freq", ENV_DEC, false, 100, 1000, &env_common.cpu_freq },
  113. { "axi_freq", ENV_DEC, false, 200, 800, &env_common.axi_freq },
  114. { "tun_freq", ENV_DEC, false, 0, 150, &env_common.tun_freq },
  115. {}
  116. };
  117. static const struct env_map_percpu env_map_core[] = {
  118. { "core_iccm", ENV_HEX, true, {NO_CCM, 0, NO_CCM, 0}, {NO_CCM, 0xF, NO_CCM, 0xF}, &env_core.iccm },
  119. { "core_dccm", ENV_HEX, true, {NO_CCM, 0, NO_CCM, 0}, {NO_CCM, 0xF, NO_CCM, 0xF}, &env_core.dccm },
  120. {}
  121. };
  122. static const struct env_map_common env_map_mask[] = {
  123. { "core_mask", ENV_HEX, false, 0x1, 0xF, &env_common.core_mask },
  124. {}
  125. };
  126. static const struct env_map_percpu env_map_go[] = {
  127. { "core_entry", ENV_HEX, true, {0, 0, 0, 0}, {U32_MAX, U32_MAX, U32_MAX, U32_MAX}, &env_core.entry },
  128. {}
  129. };
  130. static void sync_cross_cpu_data(void)
  131. {
  132. u32 value;
  133. for (u32 i = 0; i < NR_CPUS; i++) {
  134. value = env_core.entry[i].val;
  135. arc_write_uncached_32(&cross_cpu_data.entry[i], value);
  136. }
  137. for (u32 i = 0; i < NR_CPUS; i++) {
  138. value = env_core.iccm[i].val;
  139. arc_write_uncached_32(&cross_cpu_data.iccm[i], value);
  140. }
  141. for (u32 i = 0; i < NR_CPUS; i++) {
  142. value = env_core.dccm[i].val;
  143. arc_write_uncached_32(&cross_cpu_data.dccm[i], value);
  144. }
  145. value = env_common.core_mask.val;
  146. arc_write_uncached_32(&cross_cpu_data.core_mask, value);
  147. value = env_common.icache.val;
  148. arc_write_uncached_32(&cross_cpu_data.icache, value);
  149. value = env_common.dcache.val;
  150. arc_write_uncached_32(&cross_cpu_data.dcache, value);
  151. }
  152. /* Can be used only on master CPU */
  153. static bool is_cpu_used(u32 cpu_id)
  154. {
  155. return !!(env_common.core_mask.val & BIT(cpu_id));
  156. }
  157. /* TODO: add ICCM BCR and DCCM BCR runtime check */
  158. static void init_slave_cpu_func(u32 core)
  159. {
  160. u32 val;
  161. /* Remap ICCM to another memory region if it exists */
  162. val = arc_read_uncached_32(&cross_cpu_data.iccm[core]);
  163. if (val != NO_CCM)
  164. write_aux_reg(ARC_AUX_ICCM_BASE, val << APERTURE_SHIFT);
  165. /* Remap DCCM to another memory region if it exists */
  166. val = arc_read_uncached_32(&cross_cpu_data.dccm[core]);
  167. if (val != NO_CCM)
  168. write_aux_reg(ARC_AUX_DCCM_BASE, val << APERTURE_SHIFT);
  169. if (arc_read_uncached_32(&cross_cpu_data.icache))
  170. icache_enable();
  171. else
  172. icache_disable();
  173. if (arc_read_uncached_32(&cross_cpu_data.dcache))
  174. dcache_enable();
  175. else
  176. dcache_disable();
  177. }
  178. static void init_cluster_nvlim(void)
  179. {
  180. u32 val = env_common.nvlim.val << APERTURE_SHIFT;
  181. flush_dcache_all();
  182. write_aux_reg(ARC_AUX_NON_VOLATILE_LIMIT, val);
  183. write_aux_reg(AUX_AUX_CACHE_LIMIT, val);
  184. flush_n_invalidate_dcache_all();
  185. }
  186. static void init_master_icache(void)
  187. {
  188. if (icache_status()) {
  189. /* I$ is enabled - we need to disable it */
  190. if (!env_common.icache.val)
  191. icache_disable();
  192. } else {
  193. /* I$ is disabled - we need to enable it */
  194. if (env_common.icache.val) {
  195. icache_enable();
  196. /* invalidate I$ right after enable */
  197. invalidate_icache_all();
  198. }
  199. }
  200. }
  201. static void init_master_dcache(void)
  202. {
  203. if (dcache_status()) {
  204. /* D$ is enabled - we need to disable it */
  205. if (!env_common.dcache.val)
  206. dcache_disable();
  207. } else {
  208. /* D$ is disabled - we need to enable it */
  209. if (env_common.dcache.val)
  210. dcache_enable();
  211. /* TODO: probably we need ti invalidate D$ right after enable */
  212. }
  213. }
  214. static int cleanup_before_go(void)
  215. {
  216. disable_interrupts();
  217. sync_n_cleanup_cache_all();
  218. return 0;
  219. }
  220. void slave_cpu_set_boot_addr(u32 addr)
  221. {
  222. /* All cores have reset vector pointing to 0 */
  223. writel(addr, (void __iomem *)RESET_VECTOR_ADDR);
  224. /* Make sure other cores see written value in memory */
  225. sync_n_cleanup_cache_all();
  226. }
  227. static inline void halt_this_cpu(void)
  228. {
  229. __builtin_arc_flag(1);
  230. }
  231. static void smp_kick_cpu_x(u32 cpu_id)
  232. {
  233. int cmd = readl((void __iomem *)CREG_CPU_START);
  234. if (cpu_id > NR_CPUS)
  235. return;
  236. cmd &= ~CREG_CPU_START_MASK;
  237. cmd |= (1 << cpu_id);
  238. writel(cmd, (void __iomem *)CREG_CPU_START);
  239. }
  240. static u32 prepare_cpu_ctart_reg(void)
  241. {
  242. int cmd = readl((void __iomem *)CREG_CPU_START);
  243. cmd &= ~CREG_CPU_START_MASK;
  244. return cmd | env_common.core_mask.val;
  245. }
  246. /* slave CPU entry for configuration */
  247. __attribute__((naked, noreturn, flatten)) noinline void hsdk_core_init_f(void)
  248. {
  249. __asm__ __volatile__(
  250. "ld.di r8, [%0]\n"
  251. "mov %%sp, r8\n"
  252. "mov %%fp, %%sp\n"
  253. : /* no output */
  254. : "r" (&cross_cpu_data.stack_ptr));
  255. invalidate_icache_all();
  256. arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_1);
  257. init_slave_cpu_func(CPU_ID_GET());
  258. arc_write_uncached_32(&cross_cpu_data.ready_flag, SLAVE_CPU_READY);
  259. arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_2);
  260. /* Halt the processor until the master kick us again */
  261. halt_this_cpu();
  262. /*
  263. * 3 NOPs after FLAG 1 instruction are no longer required for ARCv2
  264. * cores but we leave them for gebug purposes.
  265. */
  266. __builtin_arc_nop();
  267. __builtin_arc_nop();
  268. __builtin_arc_nop();
  269. arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_3);
  270. /* get the updated entry - invalidate i$ */
  271. invalidate_icache_all();
  272. arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_4);
  273. /* Run our program */
  274. ((void (*)(void))(arc_read_uncached_32(&cross_cpu_data.entry[CPU_ID_GET()])))();
  275. /* This bootstage is unreachable as we don't return from app we launch */
  276. arc_write_uncached_32(&cross_cpu_data.status[CPU_ID_GET()], BOOTSTAGE_5);
  277. /* Something went terribly wrong */
  278. while (true)
  279. halt_this_cpu();
  280. }
  281. static void clear_cross_cpu_data(void)
  282. {
  283. arc_write_uncached_32(&cross_cpu_data.ready_flag, 0);
  284. arc_write_uncached_32(&cross_cpu_data.stack_ptr, 0);
  285. for (u32 i = 0; i < NR_CPUS; i++)
  286. arc_write_uncached_32(&cross_cpu_data.status[i], 0);
  287. }
  288. static noinline void do_init_slave_cpu(u32 cpu_id)
  289. {
  290. /* attempts number for check clave CPU ready_flag */
  291. u32 attempts = 100;
  292. u32 stack_ptr = (u32)(slave_stack + (64 * cpu_id));
  293. if (cpu_id >= NR_CPUS)
  294. return;
  295. arc_write_uncached_32(&cross_cpu_data.ready_flag, 0);
  296. /* Use global unique place for each slave cpu stack */
  297. arc_write_uncached_32(&cross_cpu_data.stack_ptr, stack_ptr);
  298. debug("CPU %u: stack pool base: %p\n", cpu_id, slave_stack);
  299. debug("CPU %u: current slave stack base: %x\n", cpu_id, stack_ptr);
  300. slave_cpu_set_boot_addr((u32)hsdk_core_init_f);
  301. smp_kick_cpu_x(cpu_id);
  302. debug("CPU %u: cross-cpu flag: %x [before timeout]\n", cpu_id,
  303. arc_read_uncached_32(&cross_cpu_data.ready_flag));
  304. while (!arc_read_uncached_32(&cross_cpu_data.ready_flag) && attempts--)
  305. mdelay(10);
  306. /* Just to be sure that slave cpu is halted after it set ready_flag */
  307. mdelay(20);
  308. /*
  309. * Only print error here if we reach timeout as there is no option to
  310. * halt slave cpu (or check that slave cpu is halted)
  311. */
  312. if (!attempts)
  313. pr_err("CPU %u is not responding after init!\n", cpu_id);
  314. /* Check current stage of slave cpu */
  315. if (arc_read_uncached_32(&cross_cpu_data.status[cpu_id]) != BOOTSTAGE_2)
  316. pr_err("CPU %u status is unexpected: %d\n", cpu_id,
  317. arc_read_uncached_32(&cross_cpu_data.status[cpu_id]));
  318. debug("CPU %u: cross-cpu flag: %x [after timeout]\n", cpu_id,
  319. arc_read_uncached_32(&cross_cpu_data.ready_flag));
  320. debug("CPU %u: status: %d [after timeout]\n", cpu_id,
  321. arc_read_uncached_32(&cross_cpu_data.status[cpu_id]));
  322. }
  323. static void do_init_slave_cpus(void)
  324. {
  325. clear_cross_cpu_data();
  326. sync_cross_cpu_data();
  327. debug("cross_cpu_data location: %#x\n", (u32)&cross_cpu_data);
  328. for (u32 i = MASTER_CPU_ID + 1; i < NR_CPUS; i++)
  329. if (is_cpu_used(i))
  330. do_init_slave_cpu(i);
  331. }
  332. static void do_init_master_cpu(void)
  333. {
  334. /*
  335. * Setup master caches even if master isn't used as we want to use
  336. * same cache configuration on all running CPUs
  337. */
  338. init_master_icache();
  339. init_master_dcache();
  340. }
  341. enum hsdk_axi_masters {
  342. M_HS_CORE = 0,
  343. M_HS_RTT,
  344. M_AXI_TUN,
  345. M_HDMI_VIDEO,
  346. M_HDMI_AUDIO,
  347. M_USB_HOST,
  348. M_ETHERNET,
  349. M_SDIO,
  350. M_GPU,
  351. M_DMAC_0,
  352. M_DMAC_1,
  353. M_DVFS
  354. };
  355. #define UPDATE_VAL 1
  356. /*
  357. * m master AXI_M_m_SLV0 AXI_M_m_SLV1 AXI_M_m_OFFSET0 AXI_M_m_OFFSET1
  358. * 0 HS (CBU) 0x11111111 0x63111111 0xFEDCBA98 0x0E543210
  359. * 1 HS (RTT) 0x77777777 0x77777777 0xFEDCBA98 0x76543210
  360. * 2 AXI Tunnel 0x88888888 0x88888888 0xFEDCBA98 0x76543210
  361. * 3 HDMI-VIDEO 0x77777777 0x77777777 0xFEDCBA98 0x76543210
  362. * 4 HDMI-ADUIO 0x77777777 0x77777777 0xFEDCBA98 0x76543210
  363. * 5 USB-HOST 0x77777777 0x77999999 0xFEDCBA98 0x76DCBA98
  364. * 6 ETHERNET 0x77777777 0x77999999 0xFEDCBA98 0x76DCBA98
  365. * 7 SDIO 0x77777777 0x77999999 0xFEDCBA98 0x76DCBA98
  366. * 8 GPU 0x77777777 0x77777777 0xFEDCBA98 0x76543210
  367. * 9 DMAC (port #1) 0x77777777 0x77777777 0xFEDCBA98 0x76543210
  368. * 10 DMAC (port #2) 0x77777777 0x77777777 0xFEDCBA98 0x76543210
  369. * 11 DVFS 0x00000000 0x60000000 0x00000000 0x00000000
  370. *
  371. * Please read ARC HS Development IC Specification, section 17.2 for more
  372. * information about apertures configuration.
  373. * NOTE: we intentionally modify default settings in U-boot. Default settings
  374. * are specified in "Table 111 CREG Address Decoder register reset values".
  375. */
  376. #define CREG_AXI_M_SLV0(m) ((void __iomem *)(CREG_BASE + 0x020 * (m)))
  377. #define CREG_AXI_M_SLV1(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x004))
  378. #define CREG_AXI_M_OFT0(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x008))
  379. #define CREG_AXI_M_OFT1(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x00C))
  380. #define CREG_AXI_M_UPDT(m) ((void __iomem *)(CREG_BASE + 0x020 * (m) + 0x014))
  381. #define CREG_AXI_M_HS_CORE_BOOT ((void __iomem *)(CREG_BASE + 0x010))
  382. #define CREG_PAE ((void __iomem *)(CREG_BASE + 0x180))
  383. #define CREG_PAE_UPDT ((void __iomem *)(CREG_BASE + 0x194))
  384. void init_memory_bridge(void)
  385. {
  386. u32 reg;
  387. /*
  388. * M_HS_CORE has one unic register - BOOT.
  389. * We need to clean boot mirror (BOOT[1:0]) bits in them.
  390. */
  391. reg = readl(CREG_AXI_M_HS_CORE_BOOT) & (~0x3);
  392. writel(reg, CREG_AXI_M_HS_CORE_BOOT);
  393. writel(0x11111111, CREG_AXI_M_SLV0(M_HS_CORE));
  394. writel(0x63111111, CREG_AXI_M_SLV1(M_HS_CORE));
  395. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HS_CORE));
  396. writel(0x0E543210, CREG_AXI_M_OFT1(M_HS_CORE));
  397. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HS_CORE));
  398. writel(0x77777777, CREG_AXI_M_SLV0(M_HS_RTT));
  399. writel(0x77777777, CREG_AXI_M_SLV1(M_HS_RTT));
  400. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HS_RTT));
  401. writel(0x76543210, CREG_AXI_M_OFT1(M_HS_RTT));
  402. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HS_RTT));
  403. writel(0x88888888, CREG_AXI_M_SLV0(M_AXI_TUN));
  404. writel(0x88888888, CREG_AXI_M_SLV1(M_AXI_TUN));
  405. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_AXI_TUN));
  406. writel(0x76543210, CREG_AXI_M_OFT1(M_AXI_TUN));
  407. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_AXI_TUN));
  408. writel(0x77777777, CREG_AXI_M_SLV0(M_HDMI_VIDEO));
  409. writel(0x77777777, CREG_AXI_M_SLV1(M_HDMI_VIDEO));
  410. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HDMI_VIDEO));
  411. writel(0x76543210, CREG_AXI_M_OFT1(M_HDMI_VIDEO));
  412. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HDMI_VIDEO));
  413. writel(0x77777777, CREG_AXI_M_SLV0(M_HDMI_AUDIO));
  414. writel(0x77777777, CREG_AXI_M_SLV1(M_HDMI_AUDIO));
  415. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_HDMI_AUDIO));
  416. writel(0x76543210, CREG_AXI_M_OFT1(M_HDMI_AUDIO));
  417. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_HDMI_AUDIO));
  418. writel(0x77777777, CREG_AXI_M_SLV0(M_USB_HOST));
  419. writel(0x77999999, CREG_AXI_M_SLV1(M_USB_HOST));
  420. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_USB_HOST));
  421. writel(0x76DCBA98, CREG_AXI_M_OFT1(M_USB_HOST));
  422. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_USB_HOST));
  423. writel(0x77777777, CREG_AXI_M_SLV0(M_ETHERNET));
  424. writel(0x77999999, CREG_AXI_M_SLV1(M_ETHERNET));
  425. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_ETHERNET));
  426. writel(0x76DCBA98, CREG_AXI_M_OFT1(M_ETHERNET));
  427. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_ETHERNET));
  428. writel(0x77777777, CREG_AXI_M_SLV0(M_SDIO));
  429. writel(0x77999999, CREG_AXI_M_SLV1(M_SDIO));
  430. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_SDIO));
  431. writel(0x76DCBA98, CREG_AXI_M_OFT1(M_SDIO));
  432. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_SDIO));
  433. writel(0x77777777, CREG_AXI_M_SLV0(M_GPU));
  434. writel(0x77777777, CREG_AXI_M_SLV1(M_GPU));
  435. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_GPU));
  436. writel(0x76543210, CREG_AXI_M_OFT1(M_GPU));
  437. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_GPU));
  438. writel(0x77777777, CREG_AXI_M_SLV0(M_DMAC_0));
  439. writel(0x77777777, CREG_AXI_M_SLV1(M_DMAC_0));
  440. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_DMAC_0));
  441. writel(0x76543210, CREG_AXI_M_OFT1(M_DMAC_0));
  442. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_DMAC_0));
  443. writel(0x77777777, CREG_AXI_M_SLV0(M_DMAC_1));
  444. writel(0x77777777, CREG_AXI_M_SLV1(M_DMAC_1));
  445. writel(0xFEDCBA98, CREG_AXI_M_OFT0(M_DMAC_1));
  446. writel(0x76543210, CREG_AXI_M_OFT1(M_DMAC_1));
  447. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_DMAC_1));
  448. writel(0x00000000, CREG_AXI_M_SLV0(M_DVFS));
  449. writel(0x60000000, CREG_AXI_M_SLV1(M_DVFS));
  450. writel(0x00000000, CREG_AXI_M_OFT0(M_DVFS));
  451. writel(0x00000000, CREG_AXI_M_OFT1(M_DVFS));
  452. writel(UPDATE_VAL, CREG_AXI_M_UPDT(M_DVFS));
  453. writel(0x00000000, CREG_PAE);
  454. writel(UPDATE_VAL, CREG_PAE_UPDT);
  455. }
  456. static void setup_clocks(void)
  457. {
  458. ulong rate;
  459. /* Setup CPU clock */
  460. if (env_common.cpu_freq.set) {
  461. rate = env_common.cpu_freq.val;
  462. soc_clk_ctl("cpu-clk", &rate, CLK_ON | CLK_SET | CLK_MHZ);
  463. }
  464. /* Setup TUN clock */
  465. if (env_common.tun_freq.set) {
  466. rate = env_common.tun_freq.val;
  467. if (rate)
  468. soc_clk_ctl("tun-clk", &rate, CLK_ON | CLK_SET | CLK_MHZ);
  469. else
  470. soc_clk_ctl("tun-clk", NULL, CLK_OFF);
  471. }
  472. if (env_common.axi_freq.set) {
  473. rate = env_common.axi_freq.val;
  474. soc_clk_ctl("axi-clk", &rate, CLK_SET | CLK_ON | CLK_MHZ);
  475. }
  476. }
  477. static void do_init_cluster(void)
  478. {
  479. /*
  480. * A multi-core ARC HS configuration always includes only one
  481. * ARC_AUX_NON_VOLATILE_LIMIT register, which is shared by all the
  482. * cores.
  483. */
  484. init_cluster_nvlim();
  485. }
  486. static int check_master_cpu_id(void)
  487. {
  488. if (CPU_ID_GET() == MASTER_CPU_ID)
  489. return 0;
  490. pr_err("u-boot runs on non-master cpu with id: %lu\n", CPU_ID_GET());
  491. return -ENOENT;
  492. }
  493. static noinline int prepare_cpus(void)
  494. {
  495. int ret;
  496. ret = check_master_cpu_id();
  497. if (ret)
  498. return ret;
  499. ret = envs_process_and_validate(env_map_common, env_map_core, is_cpu_used);
  500. if (ret)
  501. return ret;
  502. printf("CPU start mask is %#x\n", env_common.core_mask.val);
  503. do_init_slave_cpus();
  504. do_init_master_cpu();
  505. do_init_cluster();
  506. return 0;
  507. }
  508. static int hsdk_go_run(u32 cpu_start_reg)
  509. {
  510. /* Cleanup caches, disable interrupts */
  511. cleanup_before_go();
  512. if (env_common.halt_on_boot)
  513. halt_this_cpu();
  514. /*
  515. * 3 NOPs after FLAG 1 instruction are no longer required for ARCv2
  516. * cores but we leave them for gebug purposes.
  517. */
  518. __builtin_arc_nop();
  519. __builtin_arc_nop();
  520. __builtin_arc_nop();
  521. /* Kick chosen slave CPUs */
  522. writel(cpu_start_reg, (void __iomem *)CREG_CPU_START);
  523. if (is_cpu_used(MASTER_CPU_ID))
  524. ((void (*)(void))(env_core.entry[MASTER_CPU_ID].val))();
  525. else
  526. halt_this_cpu();
  527. pr_err("u-boot still runs on cpu [%ld]\n", CPU_ID_GET());
  528. /*
  529. * We will never return after executing our program if master cpu used
  530. * otherwise halt master cpu manually.
  531. */
  532. while (true)
  533. halt_this_cpu();
  534. return 0;
  535. }
  536. int board_prep_linux(bootm_headers_t *images)
  537. {
  538. int ret, ofst;
  539. char mask[15];
  540. ret = envs_read_validate_common(env_map_mask);
  541. if (ret)
  542. return ret;
  543. /* Rollback to default values */
  544. if (!env_common.core_mask.set) {
  545. env_common.core_mask.val = ALL_CPU_MASK;
  546. env_common.core_mask.set = true;
  547. }
  548. printf("CPU start mask is %#x\n", env_common.core_mask.val);
  549. if (!is_cpu_used(MASTER_CPU_ID))
  550. pr_err("ERR: try to launch linux with CPU[0] disabled! It doesn't work for ARC.\n");
  551. /*
  552. * If we want to launch linux on all CPUs we don't need to patch
  553. * linux DTB as it is default configuration
  554. */
  555. if (env_common.core_mask.val == ALL_CPU_MASK)
  556. return 0;
  557. if (!IMAGE_ENABLE_OF_LIBFDT || !images->ft_len) {
  558. pr_err("WARN: core_mask setup will work properly only with external DTB!\n");
  559. return 0;
  560. }
  561. /* patch '/possible-cpus' property according to cpu mask */
  562. ofst = fdt_path_offset(images->ft_addr, "/");
  563. sprintf(mask, "%s%s%s%s",
  564. is_cpu_used(0) ? "0," : "",
  565. is_cpu_used(1) ? "1," : "",
  566. is_cpu_used(2) ? "2," : "",
  567. is_cpu_used(3) ? "3," : "");
  568. ret = fdt_setprop_string(images->ft_addr, ofst, "possible-cpus", mask);
  569. /*
  570. * If we failed to patch '/possible-cpus' property we don't need break
  571. * linux loading process: kernel will handle it but linux will print
  572. * warning like "Timeout: CPU1 FAILED to comeup !!!".
  573. * So warn here about error, but return 0 like no error had occurred.
  574. */
  575. if (ret)
  576. pr_err("WARN: failed to patch '/possible-cpus' property, ret=%d\n",
  577. ret);
  578. return 0;
  579. }
  580. void board_jump_and_run(ulong entry, int zero, int arch, uint params)
  581. {
  582. void (*kernel_entry)(int zero, int arch, uint params);
  583. u32 cpu_start_reg;
  584. kernel_entry = (void (*)(int, int, uint))entry;
  585. /* Prepare CREG_CPU_START for kicking chosen CPUs */
  586. cpu_start_reg = prepare_cpu_ctart_reg();
  587. /* In case of run without hsdk_init */
  588. slave_cpu_set_boot_addr(entry);
  589. /* In case of run with hsdk_init */
  590. for (u32 i = 0; i < NR_CPUS; i++) {
  591. env_core.entry[i].val = entry;
  592. env_core.entry[i].set = true;
  593. }
  594. /* sync cross_cpu struct as we updated core-entry variables */
  595. sync_cross_cpu_data();
  596. /* Kick chosen slave CPUs */
  597. writel(cpu_start_reg, (void __iomem *)CREG_CPU_START);
  598. if (is_cpu_used(0))
  599. kernel_entry(zero, arch, params);
  600. }
  601. static int hsdk_go_prepare_and_run(void)
  602. {
  603. /* Prepare CREG_CPU_START for kicking chosen CPUs */
  604. u32 reg = prepare_cpu_ctart_reg();
  605. if (env_common.halt_on_boot)
  606. printf("CPU will halt before application start, start application with debugger.\n");
  607. return hsdk_go_run(reg);
  608. }
  609. static int do_hsdk_go(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  610. {
  611. int ret;
  612. /*
  613. * Check for 'halt' parameter. 'halt' = enter halt-mode just before
  614. * starting the application; can be used for debug.
  615. */
  616. if (argc > 1) {
  617. env_common.halt_on_boot = !strcmp(argv[1], "halt");
  618. if (!env_common.halt_on_boot) {
  619. pr_err("Unrecognised parameter: \'%s\'\n", argv[1]);
  620. return CMD_RET_FAILURE;
  621. }
  622. }
  623. ret = check_master_cpu_id();
  624. if (ret)
  625. return ret;
  626. ret = envs_process_and_validate(env_map_mask, env_map_go, is_cpu_used);
  627. if (ret)
  628. return ret;
  629. /* sync cross_cpu struct as we updated core-entry variables */
  630. sync_cross_cpu_data();
  631. ret = hsdk_go_prepare_and_run();
  632. return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
  633. }
  634. U_BOOT_CMD(
  635. hsdk_go, 3, 0, do_hsdk_go,
  636. "Synopsys HSDK specific command",
  637. " - Boot stand-alone application on HSDK\n"
  638. "hsdk_go halt - Boot stand-alone application on HSDK, halt CPU just before application run\n"
  639. );
  640. static int do_hsdk_init(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  641. {
  642. static bool done = false;
  643. int ret;
  644. /* hsdk_init can be run only once */
  645. if (done) {
  646. printf("HSDK HW is already initialized! Please reset the board if you want to change the configuration.\n");
  647. return CMD_RET_FAILURE;
  648. }
  649. ret = prepare_cpus();
  650. if (!ret)
  651. done = true;
  652. return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
  653. }
  654. U_BOOT_CMD(
  655. hsdk_init, 1, 0, do_hsdk_init,
  656. "Synopsys HSDK specific command",
  657. "- Init HSDK HW\n"
  658. );
  659. static int do_hsdk_clock_set(cmd_tbl_t *cmdtp, int flag, int argc,
  660. char *const argv[])
  661. {
  662. int ret = 0;
  663. /* Strip off leading subcommand argument */
  664. argc--;
  665. argv++;
  666. envs_cleanup_common(env_map_clock);
  667. if (!argc) {
  668. printf("Set clocks to values specified in environment\n");
  669. ret = envs_read_common(env_map_clock);
  670. } else {
  671. printf("Set clocks to values specified in args\n");
  672. ret = args_envs_enumerate(env_map_clock, 2, argc, argv);
  673. }
  674. if (ret)
  675. return CMD_RET_FAILURE;
  676. ret = envs_validate_common(env_map_clock);
  677. if (ret)
  678. return CMD_RET_FAILURE;
  679. /* Setup clock tree HW */
  680. setup_clocks();
  681. return CMD_RET_SUCCESS;
  682. }
  683. static int do_hsdk_clock_get(cmd_tbl_t *cmdtp, int flag, int argc,
  684. char *const argv[])
  685. {
  686. ulong rate;
  687. if (soc_clk_ctl("cpu-clk", &rate, CLK_GET | CLK_MHZ))
  688. return CMD_RET_FAILURE;
  689. if (env_set_ulong("cpu_freq", rate))
  690. return CMD_RET_FAILURE;
  691. if (soc_clk_ctl("tun-clk", &rate, CLK_GET | CLK_MHZ))
  692. return CMD_RET_FAILURE;
  693. if (env_set_ulong("tun_freq", rate))
  694. return CMD_RET_FAILURE;
  695. if (soc_clk_ctl("axi-clk", &rate, CLK_GET | CLK_MHZ))
  696. return CMD_RET_FAILURE;
  697. if (env_set_ulong("axi_freq", rate))
  698. return CMD_RET_FAILURE;
  699. printf("Clock values are saved to environment\n");
  700. return CMD_RET_SUCCESS;
  701. }
  702. static int do_hsdk_clock_print(cmd_tbl_t *cmdtp, int flag, int argc,
  703. char *const argv[])
  704. {
  705. /* Main clocks */
  706. soc_clk_ctl("cpu-clk", NULL, CLK_PRINT | CLK_MHZ);
  707. soc_clk_ctl("tun-clk", NULL, CLK_PRINT | CLK_MHZ);
  708. soc_clk_ctl("axi-clk", NULL, CLK_PRINT | CLK_MHZ);
  709. soc_clk_ctl("ddr-clk", NULL, CLK_PRINT | CLK_MHZ);
  710. return CMD_RET_SUCCESS;
  711. }
  712. static int do_hsdk_clock_print_all(cmd_tbl_t *cmdtp, int flag, int argc,
  713. char *const argv[])
  714. {
  715. /*
  716. * NOTE: as of today we don't use some peripherals like HDMI / EBI
  717. * so we don't want to print their clocks ("hdmi-sys-clk", "hdmi-pll",
  718. * "hdmi-clk", "ebi-clk"). Nevertheless their clock subsystems is fully
  719. * functional and we can print their clocks if it is required
  720. */
  721. /* CPU clock domain */
  722. soc_clk_ctl("cpu-pll", NULL, CLK_PRINT | CLK_MHZ);
  723. soc_clk_ctl("cpu-clk", NULL, CLK_PRINT | CLK_MHZ);
  724. printf("\n");
  725. /* SYS clock domain */
  726. soc_clk_ctl("sys-pll", NULL, CLK_PRINT | CLK_MHZ);
  727. soc_clk_ctl("apb-clk", NULL, CLK_PRINT | CLK_MHZ);
  728. soc_clk_ctl("axi-clk", NULL, CLK_PRINT | CLK_MHZ);
  729. soc_clk_ctl("eth-clk", NULL, CLK_PRINT | CLK_MHZ);
  730. soc_clk_ctl("usb-clk", NULL, CLK_PRINT | CLK_MHZ);
  731. soc_clk_ctl("sdio-clk", NULL, CLK_PRINT | CLK_MHZ);
  732. /* soc_clk_ctl("hdmi-sys-clk", NULL, CLK_PRINT | CLK_MHZ); */
  733. soc_clk_ctl("gfx-core-clk", NULL, CLK_PRINT | CLK_MHZ);
  734. soc_clk_ctl("gfx-dma-clk", NULL, CLK_PRINT | CLK_MHZ);
  735. soc_clk_ctl("gfx-cfg-clk", NULL, CLK_PRINT | CLK_MHZ);
  736. soc_clk_ctl("dmac-core-clk", NULL, CLK_PRINT | CLK_MHZ);
  737. soc_clk_ctl("dmac-cfg-clk", NULL, CLK_PRINT | CLK_MHZ);
  738. soc_clk_ctl("sdio-ref-clk", NULL, CLK_PRINT | CLK_MHZ);
  739. soc_clk_ctl("spi-clk", NULL, CLK_PRINT | CLK_MHZ);
  740. soc_clk_ctl("i2c-clk", NULL, CLK_PRINT | CLK_MHZ);
  741. /* soc_clk_ctl("ebi-clk", NULL, CLK_PRINT | CLK_MHZ); */
  742. soc_clk_ctl("uart-clk", NULL, CLK_PRINT | CLK_MHZ);
  743. printf("\n");
  744. /* DDR clock domain */
  745. soc_clk_ctl("ddr-clk", NULL, CLK_PRINT | CLK_MHZ);
  746. printf("\n");
  747. /* HDMI clock domain */
  748. /* soc_clk_ctl("hdmi-pll", NULL, CLK_PRINT | CLK_MHZ); */
  749. /* soc_clk_ctl("hdmi-clk", NULL, CLK_PRINT | CLK_MHZ); */
  750. /* printf("\n"); */
  751. /* TUN clock domain */
  752. soc_clk_ctl("tun-pll", NULL, CLK_PRINT | CLK_MHZ);
  753. soc_clk_ctl("tun-clk", NULL, CLK_PRINT | CLK_MHZ);
  754. soc_clk_ctl("rom-clk", NULL, CLK_PRINT | CLK_MHZ);
  755. soc_clk_ctl("pwm-clk", NULL, CLK_PRINT | CLK_MHZ);
  756. printf("\n");
  757. return CMD_RET_SUCCESS;
  758. }
  759. cmd_tbl_t cmd_hsdk_clock[] = {
  760. U_BOOT_CMD_MKENT(set, 3, 0, do_hsdk_clock_set, "", ""),
  761. U_BOOT_CMD_MKENT(get, 3, 0, do_hsdk_clock_get, "", ""),
  762. U_BOOT_CMD_MKENT(print, 4, 0, do_hsdk_clock_print, "", ""),
  763. U_BOOT_CMD_MKENT(print_all, 4, 0, do_hsdk_clock_print_all, "", ""),
  764. };
  765. static int do_hsdk_clock(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
  766. {
  767. cmd_tbl_t *c;
  768. if (argc < 2)
  769. return CMD_RET_USAGE;
  770. /* Strip off leading 'hsdk_clock' command argument */
  771. argc--;
  772. argv++;
  773. c = find_cmd_tbl(argv[0], cmd_hsdk_clock, ARRAY_SIZE(cmd_hsdk_clock));
  774. if (!c)
  775. return CMD_RET_USAGE;
  776. return c->cmd(cmdtp, flag, argc, argv);
  777. }
  778. U_BOOT_CMD(
  779. hsdk_clock, CONFIG_SYS_MAXARGS, 0, do_hsdk_clock,
  780. "Synopsys HSDK specific clock command",
  781. "set - Set clock to values specified in environment / command line arguments\n"
  782. "hsdk_clock get - Save clock values to environment\n"
  783. "hsdk_clock print - Print main clock values to console\n"
  784. "hsdk_clock print_all - Print all clock values to console\n"
  785. );
  786. /* init calls */
  787. int board_early_init_f(void)
  788. {
  789. /*
  790. * Setup AXI apertures unconditionally as we want to have DDR
  791. * in 0x00000000 region when we are kicking slave cpus.
  792. */
  793. init_memory_bridge();
  794. return 0;
  795. }
  796. int board_early_init_r(void)
  797. {
  798. /*
  799. * TODO: Init USB here to be able read environment from USB MSD.
  800. * It can be done with usb_init() call. We can't do it right now
  801. * due to brocken USB IP SW reset and lack of USB IP HW reset in
  802. * linux kernel (if we init USB here we will break USB in linux)
  803. */
  804. /*
  805. * Flush all d$ as we want to use uncached area with st.di / ld.di
  806. * instructions and we don't want to have any dirty line in L1d$ or SL$
  807. * in this area. It is enough to flush all d$ once here as we access to
  808. * uncached area with regular st (non .di) instruction only when we copy
  809. * data during u-boot relocation.
  810. */
  811. flush_dcache_all();
  812. printf("Relocation Offset is: %08lx\n", gd->reloc_off);
  813. return 0;
  814. }
  815. int board_late_init(void)
  816. {
  817. /*
  818. * Populate environment with clock frequency values -
  819. * run hsdk_clock get callback without uboot command run.
  820. */
  821. do_hsdk_clock_get(NULL, 0, 0, NULL);
  822. return 0;
  823. }
  824. int board_mmc_init(bd_t *bis)
  825. {
  826. struct dwmci_host *host = NULL;
  827. host = malloc(sizeof(struct dwmci_host));
  828. if (!host) {
  829. printf("dwmci_host malloc fail!\n");
  830. return 1;
  831. }
  832. /*
  833. * Switch SDIO external ciu clock divider from default div-by-8 to
  834. * minimum possible div-by-2.
  835. */
  836. writel(SDIO_UHS_REG_EXT_DIV_2, (void __iomem *)SDIO_UHS_REG_EXT);
  837. memset(host, 0, sizeof(struct dwmci_host));
  838. host->name = "Synopsys Mobile storage";
  839. host->ioaddr = (void *)ARC_DWMMC_BASE;
  840. host->buswidth = 4;
  841. host->dev_index = 0;
  842. host->bus_hz = 50000000;
  843. add_dwmci(host, host->bus_hz / 2, 400000);
  844. return 0;
  845. }