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