]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/mx6/soc.c
imx: mx6: add get_cpu_speed_grade_hz func to return MHz speed grade from OTP
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / mx6 / soc.c
1 /*
2  * (C) Copyright 2007
3  * Sascha Hauer, Pengutronix
4  *
5  * (C) Copyright 2009 Freescale Semiconductor, Inc.
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  */
9
10 #include <common.h>
11 #include <stdbool.h>
12 #include <dm.h>
13 #include <div64.h>
14 #include <ipu.h>
15 #include <imx_thermal.h>
16 #include <asm/armv7.h>
17 #include <asm/bootm.h>
18 #include <asm/pl310.h>
19 #include <asm/errno.h>
20 #include <asm/io.h>
21 #include <asm/arch/imx-regs.h>
22 #include <asm/arch/crm_regs.h>
23 #include <asm/arch/regs-ocotp.h>
24 #include <asm/arch/clock.h>
25 #include <asm/arch/mxc_hdmi.h>
26 #include <asm/arch/sys_proto.h>
27 #include <asm/imx-common/boot_mode.h>
28 #include <asm/imx-common/dma.h>
29
30 DECLARE_GLOBAL_DATA_PTR;
31
32 #define __data __attribute__((section(".data")))
33
34 #ifdef CONFIG_MX6_TEMPERATURE_MIN
35 #define TEMPERATURE_MIN                 CONFIG_MX6_TEMPERATURE_MIN
36 #else
37 #define TEMPERATURE_MIN                 (-40)
38 #endif
39 #ifdef CONFIG_MX6_TEMPERATURE_HOT
40 #define TEMPERATURE_HOT                 CONFIG_MX6_TEMPERATURE_HOT
41 #else
42 #define TEMPERATURE_HOT                 80
43 #endif
44 #ifdef CONFIG_MX6_TEMPERATURE_MAX
45 #define TEMPERATURE_MAX                 CONFIG_MX6_TEMPERATURE_MAX
46 #else
47 #define TEMPERATURE_MAX                 125
48 #endif
49 #define TEMP_AVG_COUNT                  5
50 #define TEMP_WARN_THRESHOLD             5
51
52 enum ldo_reg {
53         LDO_ARM,
54         LDO_SOC,
55         LDO_PU,
56 };
57
58 struct scu_regs {
59         u32     ctrl;
60         u32     config;
61         u32     status;
62         u32     invalidate;
63         u32     fpga_rev;
64 };
65
66 #if defined(CONFIG_IMX6_THERMAL)
67 static const struct imx_thermal_plat imx6_thermal_plat = {
68         .regs = (void *)ANATOP_BASE_ADDR,
69         .fuse_bank = 1,
70         .fuse_word = 6,
71 };
72
73 U_BOOT_DEVICE(imx6_thermal) = {
74         .name = "imx_thermal",
75         .platdata = &imx6_thermal_plat,
76 };
77 #endif
78
79 u32 get_nr_cpus(void)
80 {
81         struct scu_regs *scu = (struct scu_regs *)SCU_BASE_ADDR;
82         return readl(&scu->config) & 3;
83 }
84
85 u32 get_cpu_rev(void)
86 {
87         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
88         u32 reg = readl(&anatop->digprog_sololite);
89         u32 type = ((reg >> 16) & 0xff);
90
91         if (type != MXC_CPU_MX6SL) {
92                 reg = readl(&anatop->digprog);
93                 struct scu_regs *scu = (struct scu_regs *)SCU_BASE_ADDR;
94                 u32 cfg = readl(&scu->config) & 3;
95                 type = ((reg >> 16) & 0xff);
96                 if (type == MXC_CPU_MX6DL) {
97                         if (!cfg)
98                                 type = MXC_CPU_MX6SOLO;
99                 }
100
101                 if (type == MXC_CPU_MX6Q) {
102                         if (cfg == 1)
103                                 type = MXC_CPU_MX6D;
104                 }
105
106         }
107         reg &= 0xff;            /* mx6 silicon revision */
108         return (type << 12) | (reg + 0x10);
109 }
110
111 /*
112  * OCOTP_CFG3[17:16] (see Fusemap Description Table offset 0x440)
113  * defines a 2-bit SPEED_GRADING
114  */
115 #define OCOTP_CFG3_SPEED_SHIFT  16
116 #define OCOTP_CFG3_SPEED_800MHZ 0
117 #define OCOTP_CFG3_SPEED_850MHZ 1
118 #define OCOTP_CFG3_SPEED_1GHZ   2
119 #define OCOTP_CFG3_SPEED_1P2GHZ 3
120
121 u32 get_cpu_speed_grade_hz(void)
122 {
123         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
124         struct fuse_bank *bank = &ocotp->bank[0];
125         struct fuse_bank0_regs *fuse =
126                 (struct fuse_bank0_regs *)bank->fuse_regs;
127         uint32_t val;
128
129         val = readl(&fuse->cfg3);
130         val >>= OCOTP_CFG3_SPEED_SHIFT;
131         val &= 0x3;
132
133         switch (val) {
134         /* Valid for IMX6DQ */
135         case OCOTP_CFG3_SPEED_1P2GHZ:
136                 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D))
137                         return 1200000000;
138         /* Valid for IMX6SX/IMX6SDL/IMX6DQ */
139         case OCOTP_CFG3_SPEED_1GHZ:
140                 return 996000000;
141         /* Valid for IMX6DQ */
142         case OCOTP_CFG3_SPEED_850MHZ:
143                 if (is_cpu_type(MXC_CPU_MX6Q) || is_cpu_type(MXC_CPU_MX6D))
144                         return 852000000;
145         /* Valid for IMX6SX/IMX6SDL/IMX6DQ */
146         case OCOTP_CFG3_SPEED_800MHZ:
147                 return 792000000;
148         }
149         return 0;
150 }
151
152 #ifdef CONFIG_REVISION_TAG
153 u32 __weak get_board_rev(void)
154 {
155         u32 cpurev = get_cpu_rev();
156         u32 type = ((cpurev >> 12) & 0xff);
157         if (type == MXC_CPU_MX6SOLO)
158                 cpurev = (MXC_CPU_MX6DL) << 12 | (cpurev & 0xFFF);
159
160         if (type == MXC_CPU_MX6D)
161                 cpurev = (MXC_CPU_MX6Q) << 12 | (cpurev & 0xFFF);
162
163         return cpurev;
164 }
165 #endif
166
167 void init_aips(void)
168 {
169         struct aipstz_regs *aips1, *aips2;
170 #ifdef CONFIG_SOC_MX6SX
171         struct aipstz_regs *aips3;
172 #endif
173
174         aips1 = (struct aipstz_regs *)AIPS1_ARB_BASE_ADDR;
175         aips2 = (struct aipstz_regs *)AIPS2_ARB_BASE_ADDR;
176 #ifdef CONFIG_SOC_MX6SX
177         aips3 = (struct aipstz_regs *)AIPS3_ARB_BASE_ADDR;
178 #endif
179
180         /*
181          * Set all MPROTx to be non-bufferable, trusted for R/W,
182          * not forced to user-mode.
183          */
184         writel(0x77777777, &aips1->mprot0);
185         writel(0x77777777, &aips1->mprot1);
186         writel(0x77777777, &aips2->mprot0);
187         writel(0x77777777, &aips2->mprot1);
188
189         /*
190          * Set all OPACRx to be non-bufferable, not require
191          * supervisor privilege level for access,allow for
192          * write access and untrusted master access.
193          */
194         writel(0x00000000, &aips1->opacr0);
195         writel(0x00000000, &aips1->opacr1);
196         writel(0x00000000, &aips1->opacr2);
197         writel(0x00000000, &aips1->opacr3);
198         writel(0x00000000, &aips1->opacr4);
199         writel(0x00000000, &aips2->opacr0);
200         writel(0x00000000, &aips2->opacr1);
201         writel(0x00000000, &aips2->opacr2);
202         writel(0x00000000, &aips2->opacr3);
203         writel(0x00000000, &aips2->opacr4);
204
205 #ifdef CONFIG_SOC_MX6SX
206         /*
207          * Set all MPROTx to be non-bufferable, trusted for R/W,
208          * not forced to user-mode.
209          */
210         writel(0x77777777, &aips3->mprot0);
211         writel(0x77777777, &aips3->mprot1);
212
213         /*
214          * Set all OPACRx to be non-bufferable, not require
215          * supervisor privilege level for access,allow for
216          * write access and untrusted master access.
217          */
218         writel(0x00000000, &aips3->opacr0);
219         writel(0x00000000, &aips3->opacr1);
220         writel(0x00000000, &aips3->opacr2);
221         writel(0x00000000, &aips3->opacr3);
222         writel(0x00000000, &aips3->opacr4);
223 #endif
224 }
225
226 static void clear_ldo_ramp(void)
227 {
228         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
229         int reg;
230
231         /* ROM may modify LDO ramp up time according to fuse setting, so in
232          * order to be in the safe side we neeed to reset these settings to
233          * match the reset value: 0'b00
234          */
235         reg = readl(&anatop->ana_misc2);
236         reg &= ~(0x3f << 24);
237         writel(reg, &anatop->ana_misc2);
238 }
239
240 /*
241  * Set the PMU_REG_CORE register
242  *
243  * Set LDO_SOC/PU/ARM regulators to the specified millivolt level.
244  * Possible values are from 0.725V to 1.450V in steps of
245  * 0.025V (25mV).
246  */
247 static int set_ldo_voltage(enum ldo_reg ldo, u32 mv)
248 {
249         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
250         u32 val, step, old, reg = readl(&anatop->reg_core);
251         u8 shift;
252
253         if (mv < 725)
254                 val = 0x00;     /* Power gated off */
255         else if (mv > 1450)
256                 val = 0x1F;     /* Power FET switched full on. No regulation */
257         else
258                 val = (mv - 700) / 25;
259
260         clear_ldo_ramp();
261
262         switch (ldo) {
263         case LDO_SOC:
264                 shift = 18;
265                 break;
266         case LDO_PU:
267                 shift = 9;
268                 break;
269         case LDO_ARM:
270                 shift = 0;
271                 break;
272         default:
273                 return -EINVAL;
274         }
275
276         old = (reg & (0x1F << shift)) >> shift;
277         step = abs(val - old);
278         if (step == 0)
279                 return 0;
280
281         reg = (reg & ~(0x1F << shift)) | (val << shift);
282         writel(reg, &anatop->reg_core);
283
284         /*
285          * The LDO ramp-up is based on 64 clock cycles of 24 MHz = 2.6 us per
286          * step
287          */
288         udelay(3 * step);
289
290         return 0;
291 }
292
293 static u32 __data thermal_calib;
294
295 #define FACTOR0                         10000000
296 #define FACTOR1                         15976
297 #define FACTOR2                         4297157
298
299 int raw_to_celsius(unsigned int raw, unsigned int raw_25c, unsigned int raw_hot,
300                 unsigned int hot_temp)
301 {
302         int temperature;
303
304         if (raw_hot != 0 && hot_temp != 0) {
305                 unsigned int raw_n40c, ratio;
306
307                 ratio = ((raw_25c - raw_hot) * 100) / (hot_temp - 25);
308                 raw_n40c = raw_25c + (13 * ratio) / 20;
309                 if (raw <= raw_n40c)
310                         temperature = (raw_n40c - raw) * 100 / ratio - 40;
311                 else
312                         temperature = TEMPERATURE_MIN;
313         } else {
314                 u64 temp64 = FACTOR0;
315                 unsigned int c1, c2;
316                 /*
317                  * Derived from linear interpolation:
318                  * slope = 0.4297157 - (0.0015976 * 25C fuse)
319                  * slope = (FACTOR2 - FACTOR1 * n1) / FACTOR0
320                  * (Nmeas - n1) / (Tmeas - t1) = slope
321                  * We want to reduce this down to the minimum computation necessary
322                  * for each temperature read.  Also, we want Tmeas in millicelsius
323                  * and we don't want to lose precision from integer division. So...
324                  * Tmeas = (Nmeas - n1) / slope + t1
325                  * milli_Tmeas = 1000 * (Nmeas - n1) / slope + 1000 * t1
326                  * milli_Tmeas = -1000 * (n1 - Nmeas) / slope + 1000 * t1
327                  * Let constant c1 = (-1000 / slope)
328                  * milli_Tmeas = (n1 - Nmeas) * c1 + 1000 * t1
329                  * Let constant c2 = n1 *c1 + 1000 * t1
330                  * milli_Tmeas = c2 - Nmeas * c1
331                  */
332                 temp64 *= 1000;
333                 do_div(temp64, FACTOR1 * raw_25c - FACTOR2);
334                 c1 = temp64;
335                 c2 = raw_25c * c1 + 1000 * 25;
336                 temperature = (c2 - raw * c1) / 1000;
337         }
338         return temperature;
339 }
340
341 int read_cpu_temperature(void)
342 {
343         unsigned int reg, tmp, i;
344         unsigned int raw_25c, raw_hot, hot_temp;
345         int temperature;
346         struct anatop_regs *const anatop = (void *)ANATOP_BASE_ADDR;
347         struct mx6_ocotp_regs *const ocotp_regs = (void *)OCOTP_BASE_ADDR;
348
349         if (!thermal_calib) {
350                 ocotp_clk_enable();
351                 writel(1, &ocotp_regs->hw_ocotp_read_ctrl);
352                 thermal_calib = readl(&ocotp_regs->hw_ocotp_ana1);
353                 writel(0, &ocotp_regs->hw_ocotp_read_ctrl);
354                 ocotp_clk_disable();
355         }
356
357         if (thermal_calib == 0 || thermal_calib == 0xffffffff)
358                 return TEMPERATURE_MIN;
359
360         /* Fuse data layout:
361          * [31:20] sensor value @ 25C
362          * [19:8] sensor value of hot
363          * [7:0] hot temperature value */
364         raw_25c = thermal_calib >> 20;
365         raw_hot = (thermal_calib & 0xfff00) >> 8;
366         hot_temp = thermal_calib & 0xff;
367
368         /* now we only using single measure, every time we measure
369          * the temperature, we will power on/off the anadig module
370          */
371         writel(BM_ANADIG_TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
372         writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
373
374         /* write measure freq */
375         writel(327, &anatop->tempsense1);
376         writel(BM_ANADIG_TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
377         writel(BM_ANADIG_TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
378         writel(BM_ANADIG_TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
379
380         /* average the temperature value over multiple readings */
381         for (i = 0; i < TEMP_AVG_COUNT; i++) {
382                 static int failed;
383                 int limit = 100;
384
385                 while ((readl(&anatop->tempsense0) &
386                                 BM_ANADIG_TEMPSENSE0_FINISHED) == 0) {
387                         udelay(10000);
388                         if (--limit < 0)
389                                 break;
390                 }
391                 if ((readl(&anatop->tempsense0) &
392                                 BM_ANADIG_TEMPSENSE0_FINISHED) == 0) {
393                         if (!failed) {
394                                 printf("Failed to read temp sensor\n");
395                                 failed = 1;
396                         }
397                         return 0;
398                 }
399                 failed = 0;
400                 reg = (readl(&anatop->tempsense0) &
401                         BM_ANADIG_TEMPSENSE0_TEMP_VALUE) >>
402                         BP_ANADIG_TEMPSENSE0_TEMP_VALUE;
403                 if (i == 0)
404                         tmp = reg;
405                 else
406                         tmp = (tmp * i + reg) / (i + 1);
407                 writel(BM_ANADIG_TEMPSENSE0_FINISHED,
408                         &anatop->tempsense0_clr);
409         }
410
411         temperature = raw_to_celsius(tmp, raw_25c, raw_hot, hot_temp);
412
413         /* power down anatop thermal sensor */
414         writel(BM_ANADIG_TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
415         writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
416
417         return temperature;
418 }
419
420 int check_cpu_temperature(int boot)
421 {
422         static int __data max_temp;
423         int boot_limit = getenv_ulong("max_boot_temp", 10, TEMPERATURE_HOT);
424         int tmp = read_cpu_temperature();
425         bool first = true;
426
427         if (tmp < TEMPERATURE_MIN || tmp > TEMPERATURE_MAX) {
428                 printf("Temperature:   can't get valid data!\n");
429                 return tmp;
430         }
431
432         if (!boot) {
433                 if (tmp > boot_limit) {
434                         printf("CPU is %d C, too hot, resetting...\n", tmp);
435                         udelay(100000);
436                         reset_cpu(0);
437                 }
438                 if (tmp > max_temp) {
439                         if (tmp > boot_limit - TEMP_WARN_THRESHOLD)
440                                 printf("WARNING: CPU temperature %d C\n", tmp);
441                         max_temp = tmp;
442                 }
443         } else {
444                 printf("Temperature:   %d C, calibration data 0x%x\n",
445                         tmp, thermal_calib);
446                 while (tmp >= boot_limit) {
447                         if (first) {
448                                 printf("CPU is %d C, too hot to boot, waiting...\n",
449                                         tmp);
450                                 first = false;
451                         }
452                         if (ctrlc())
453                                 break;
454                         udelay(50000);
455                         tmp = read_cpu_temperature();
456                         if (tmp > boot_limit - TEMP_WARN_THRESHOLD && tmp != max_temp)
457                                 printf("WARNING: CPU temperature %d C\n", tmp);
458                         max_temp = tmp;
459                 }
460         }
461         return tmp;
462 }
463
464 static void imx_set_wdog_powerdown(bool enable)
465 {
466         struct wdog_regs *wdog1 = (struct wdog_regs *)WDOG1_BASE_ADDR;
467         struct wdog_regs *wdog2 = (struct wdog_regs *)WDOG2_BASE_ADDR;
468
469 #ifdef CONFIG_MX6SX
470         struct wdog_regs *wdog3 = (struct wdog_regs *)WDOG3_BASE_ADDR;
471         writew(enable, &wdog3->wmcr);
472 #endif
473
474         /* Write to the PDE (Power Down Enable) bit */
475         writew(enable, &wdog1->wmcr);
476         writew(enable, &wdog2->wmcr);
477 }
478
479 static void set_ahb_rate(u32 val)
480 {
481         struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
482         u32 reg, div;
483
484         div = get_periph_clk() / val - 1;
485         reg = readl(&mxc_ccm->cbcdr);
486
487         writel((reg & (~MXC_CCM_CBCDR_AHB_PODF_MASK)) |
488                 (div << MXC_CCM_CBCDR_AHB_PODF_OFFSET), &mxc_ccm->cbcdr);
489 }
490
491 static void clear_mmdc_ch_mask(void)
492 {
493         struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
494
495         /* Clear MMDC channel mask */
496         writel(0, &mxc_ccm->ccdr);
497 }
498
499 static void init_bandgap(void)
500 {
501         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
502         /*
503          * Ensure the bandgap has stabilized.
504          */
505         while (!(readl(&anatop->ana_misc0) & 0x80))
506                 ;
507         /*
508          * For best noise performance of the analog blocks using the
509          * outputs of the bandgap, the reftop_selfbiasoff bit should
510          * be set.
511          */
512         writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
513 }
514
515 #ifdef CONFIG_SOC_MX6SL
516 static void set_preclk_from_osc(void)
517 {
518         struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
519         u32 reg;
520
521         reg = readl(&mxc_ccm->cscmr1);
522         reg |= MXC_CCM_CSCMR1_PER_CLK_SEL_MASK;
523         writel(reg, &mxc_ccm->cscmr1);
524 }
525 #endif
526
527 #define SRC_SCR_WARM_RESET_ENABLE       0
528
529 static void init_src(void)
530 {
531         struct src *src_regs = (struct src *)SRC_BASE_ADDR;
532         u32 val;
533
534         /*
535          * force warm reset sources to generate cold reset
536          * for a more reliable restart
537          */
538         val = readl(&src_regs->scr);
539         val &= ~(1 << SRC_SCR_WARM_RESET_ENABLE);
540         writel(val, &src_regs->scr);
541 }
542
543 int arch_cpu_init(void)
544 {
545         init_aips();
546
547         /* Need to clear MMDC_CHx_MASK to make warm reset work. */
548         clear_mmdc_ch_mask();
549
550         /*
551          * Disable self-bias circuit in the analog bandap.
552          * The self-bias circuit is used by the bandgap during startup.
553          * This bit should be set after the bandgap has initialized.
554          */
555         init_bandgap();
556
557         /*
558          * When low freq boot is enabled, ROM will not set AHB
559          * freq, so we need to ensure AHB freq is 132MHz in such
560          * scenario.
561          */
562         if (mxc_get_clock(MXC_ARM_CLK) == 396000000)
563                 set_ahb_rate(132000000);
564
565                 /* Set perclk to source from OSC 24MHz */
566 #if defined(CONFIG_SOC_MX6SL)
567         set_preclk_from_osc();
568 #endif
569
570         imx_set_wdog_powerdown(false); /* Disable PDE bit of WMCR register */
571
572 #ifdef CONFIG_VIDEO_IPUV3
573         gd->arch.ipu_hw_rev = IPUV3_HW_REV_IPUV3H;
574 #endif
575 #ifdef  CONFIG_APBH_DMA
576         /* Timer is required for Initializing APBH DMA */
577         timer_init();
578         mxs_dma_init();
579 #endif
580
581         init_src();
582
583         return 0;
584 }
585
586 int board_postclk_init(void)
587 {
588         set_ldo_voltage(LDO_SOC, 1175); /* Set VDDSOC to 1.175V */
589
590         return 0;
591 }
592
593 #ifndef CONFIG_SYS_DCACHE_OFF
594 void enable_caches(void)
595 {
596 #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
597         enum dcache_option option = DCACHE_WRITETHROUGH;
598 #else
599         enum dcache_option option = DCACHE_WRITEBACK;
600 #endif
601
602         /* Avoid random hang when download by usb */
603         invalidate_dcache_all();
604
605         /* Enable D-cache. I-cache is already enabled in start.S */
606         dcache_enable();
607
608         /* Enable caching on OCRAM and ROM */
609         mmu_set_region_dcache_behaviour(ROMCP_ARB_BASE_ADDR,
610                                         ROMCP_ARB_END_ADDR,
611                                         option);
612         mmu_set_region_dcache_behaviour(IRAM_BASE_ADDR,
613                                         IRAM_SIZE,
614                                         option);
615 }
616 #endif
617
618 #if defined(CONFIG_FEC_MXC)
619 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
620 {
621         struct ocotp_regs *ocotp = (struct ocotp_regs *)OCOTP_BASE_ADDR;
622         struct fuse_bank *bank = &ocotp->bank[4];
623         struct fuse_bank4_regs *fuse =
624                         (struct fuse_bank4_regs *)bank->fuse_regs;
625
626         u32 value = readl(&fuse->mac_addr_high);
627         mac[0] = (value >> 8);
628         mac[1] = value;
629
630         value = readl(&fuse->mac_addr_low);
631         mac[2] = value >> 24;
632         mac[3] = value >> 16;
633         mac[4] = value >> 8;
634         mac[5] = value;
635 }
636 #endif
637
638 void boot_mode_apply(unsigned cfg_val)
639 {
640         unsigned reg;
641         struct src *psrc = (struct src *)SRC_BASE_ADDR;
642         writel(cfg_val, &psrc->gpr9);
643         reg = readl(&psrc->gpr10);
644         if (cfg_val)
645                 reg |= 1 << 28;
646         else
647                 reg &= ~(1 << 28);
648         writel(reg, &psrc->gpr10);
649 }
650 /*
651  * cfg_val will be used for
652  * Boot_cfg4[7:0]:Boot_cfg3[7:0]:Boot_cfg2[7:0]:Boot_cfg1[7:0]
653  * After reset, if GPR10[28] is 1, ROM will use GPR9[25:0]
654  * instead of SBMR1 to determine the boot device.
655  */
656 const struct boot_mode soc_boot_modes[] = {
657         {"normal",      MAKE_CFGVAL(0x00, 0x00, 0x00, 0x00)},
658         /* reserved value should start rom usb */
659         {"usb",         MAKE_CFGVAL(0x01, 0x00, 0x00, 0x00)},
660         {"sata",        MAKE_CFGVAL(0x20, 0x00, 0x00, 0x00)},
661         {"ecspi1:0",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x08)},
662         {"ecspi1:1",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x18)},
663         {"ecspi1:2",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x28)},
664         {"ecspi1:3",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x38)},
665         /* 4 bit bus width */
666         {"esdhc1",      MAKE_CFGVAL(0x40, 0x20, 0x00, 0x00)},
667         {"esdhc2",      MAKE_CFGVAL(0x40, 0x28, 0x00, 0x00)},
668         {"esdhc3",      MAKE_CFGVAL(0x40, 0x30, 0x00, 0x00)},
669         {"esdhc4",      MAKE_CFGVAL(0x40, 0x38, 0x00, 0x00)},
670         {NULL,          0},
671 };
672
673 void s_init(void)
674 {
675         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
676         struct mxc_ccm_reg *ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
677         u32 mask480;
678         u32 mask528;
679         u32 reg, periph1, periph2;
680
681         if (is_cpu_type(MXC_CPU_MX6SX))
682                 return;
683
684         /* Due to hardware limitation, on MX6Q we need to gate/ungate all PFDs
685          * to make sure PFD is working right, otherwise, PFDs may
686          * not output clock after reset, MX6DL and MX6SL have added 396M pfd
687          * workaround in ROM code, as bus clock need it
688          */
689
690         mask480 = ANATOP_PFD_CLKGATE_MASK(0) |
691                 ANATOP_PFD_CLKGATE_MASK(1) |
692                 ANATOP_PFD_CLKGATE_MASK(2) |
693                 ANATOP_PFD_CLKGATE_MASK(3);
694         mask528 = ANATOP_PFD_CLKGATE_MASK(1) |
695                 ANATOP_PFD_CLKGATE_MASK(3);
696
697         reg = readl(&ccm->cbcmr);
698         periph2 = ((reg & MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_MASK)
699                 >> MXC_CCM_CBCMR_PRE_PERIPH2_CLK_SEL_OFFSET);
700         periph1 = ((reg & MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_MASK)
701                 >> MXC_CCM_CBCMR_PRE_PERIPH_CLK_SEL_OFFSET);
702
703         /* Checking if PLL2 PFD0 or PLL2 PFD2 is using for periph clock */
704         if ((periph2 != 0x2) && (periph1 != 0x2))
705                 mask528 |= ANATOP_PFD_CLKGATE_MASK(0);
706
707         if ((periph2 != 0x1) && (periph1 != 0x1) &&
708                 (periph2 != 0x3) && (periph1 != 0x3))
709                 mask528 |= ANATOP_PFD_CLKGATE_MASK(2);
710
711         writel(mask480, &anatop->pfd_480_set);
712         writel(mask528, &anatop->pfd_528_set);
713         writel(mask480, &anatop->pfd_480_clr);
714         writel(mask528, &anatop->pfd_528_clr);
715 }
716
717 #ifdef CONFIG_IMX_HDMI
718 void imx_enable_hdmi_phy(void)
719 {
720         struct hdmi_regs *hdmi = (struct hdmi_regs *)HDMI_ARB_BASE_ADDR;
721         u8 reg;
722         reg = readb(&hdmi->phy_conf0);
723         reg |= HDMI_PHY_CONF0_PDZ_MASK;
724         writeb(reg, &hdmi->phy_conf0);
725         udelay(3000);
726         reg |= HDMI_PHY_CONF0_ENTMDS_MASK;
727         writeb(reg, &hdmi->phy_conf0);
728         udelay(3000);
729         reg |= HDMI_PHY_CONF0_GEN2_TXPWRON_MASK;
730         writeb(reg, &hdmi->phy_conf0);
731         writeb(HDMI_MC_PHYRSTZ_ASSERT, &hdmi->mc_phyrstz);
732 }
733
734 void imx_setup_hdmi(void)
735 {
736         struct mxc_ccm_reg *mxc_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
737         struct hdmi_regs *hdmi  = (struct hdmi_regs *)HDMI_ARB_BASE_ADDR;
738         int reg;
739
740         /* Turn on HDMI PHY clock */
741         reg = readl(&mxc_ccm->CCGR2);
742         reg |=  MXC_CCM_CCGR2_HDMI_TX_IAHBCLK_MASK|
743                  MXC_CCM_CCGR2_HDMI_TX_ISFRCLK_MASK;
744         writel(reg, &mxc_ccm->CCGR2);
745         writeb(HDMI_MC_PHYRSTZ_DEASSERT, &hdmi->mc_phyrstz);
746         reg = readl(&mxc_ccm->chsccdr);
747         reg &= ~(MXC_CCM_CHSCCDR_IPU1_DI0_PRE_CLK_SEL_MASK|
748                  MXC_CCM_CHSCCDR_IPU1_DI0_PODF_MASK|
749                  MXC_CCM_CHSCCDR_IPU1_DI0_CLK_SEL_MASK);
750         reg |= (CHSCCDR_PODF_DIVIDE_BY_3
751                  << MXC_CCM_CHSCCDR_IPU1_DI0_PODF_OFFSET)
752                  |(CHSCCDR_IPU_PRE_CLK_540M_PFD
753                  << MXC_CCM_CHSCCDR_IPU1_DI0_PRE_CLK_SEL_OFFSET);
754         writel(reg, &mxc_ccm->chsccdr);
755 }
756 #endif
757
758 #ifndef CONFIG_SYS_L2CACHE_OFF
759 #define IOMUXC_GPR11_L2CACHE_AS_OCRAM 0x00000002
760 void v7_outer_cache_enable(void)
761 {
762         struct pl310_regs *const pl310 = (struct pl310_regs *)L2_PL310_BASE;
763         unsigned int val;
764
765
766         /*
767          * Set bit 22 in the auxiliary control register. If this bit
768          * is cleared, PL310 treats Normal Shared Non-cacheable
769          * accesses as Cacheable no-allocate.
770          */
771         setbits_le32(&pl310->pl310_aux_ctrl, L310_SHARED_ATT_OVERRIDE_ENABLE);
772
773 #if defined CONFIG_SOC_MX6SL
774         struct iomuxc *iomux = (struct iomuxc *)IOMUXC_BASE_ADDR;
775         val = readl(&iomux->gpr[11]);
776         if (val & IOMUXC_GPR11_L2CACHE_AS_OCRAM) {
777                 /* L2 cache configured as OCRAM, reset it */
778                 val &= ~IOMUXC_GPR11_L2CACHE_AS_OCRAM;
779                 writel(val, &iomux->gpr[11]);
780         }
781 #endif
782
783         /* Must disable the L2 before changing the latency parameters */
784         clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
785
786         writel(0x132, &pl310->pl310_tag_latency_ctrl);
787         writel(0x132, &pl310->pl310_data_latency_ctrl);
788
789         val = readl(&pl310->pl310_prefetch_ctrl);
790
791         /* Turn on the L2 I/D prefetch */
792         val |= 0x30000000;
793
794         /*
795          * The L2 cache controller(PL310) version on the i.MX6D/Q is r3p1-50rel0
796          * The L2 cache controller(PL310) version on the i.MX6DL/SOLO/SL is r3p2
797          * But according to ARM PL310 errata: 752271
798          * ID: 752271: Double linefill feature can cause data corruption
799          * Fault Status: Present in: r3p0, r3p1, r3p1-50rel0. Fixed in r3p2
800          * Workaround: The only workaround to this erratum is to disable the
801          * double linefill feature. This is the default behavior.
802          */
803
804 #ifndef CONFIG_SOC_MX6Q
805         val |= 0x40800000;
806 #endif
807         writel(val, &pl310->pl310_prefetch_ctrl);
808
809         val = readl(&pl310->pl310_power_ctrl);
810         val |= L2X0_DYNAMIC_CLK_GATING_EN;
811         val |= L2X0_STNDBY_MODE_EN;
812         writel(val, &pl310->pl310_power_ctrl);
813
814         setbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
815 }
816
817 void v7_outer_cache_disable(void)
818 {
819         struct pl310_regs *const pl310 = (struct pl310_regs *)L2_PL310_BASE;
820
821         clrbits_le32(&pl310->pl310_ctrl, L2X0_CTRL_EN);
822 }
823 #endif /* !CONFIG_SYS_L2CACHE_OFF */