]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/mx6/soc.c
TX6 Release 2013-04-22
[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  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25
26 #include <common.h>
27 #include <asm/errno.h>
28 #include <asm/io.h>
29 #include <asm/arch/imx-regs.h>
30 #include <asm/arch/crm_regs.h>
31 #include <asm/arch/regs-ocotp.h>
32 #include <asm/arch/clock.h>
33 #include <asm/arch/dma.h>
34 #include <asm/arch/sys_proto.h>
35 #include <asm/imx-common/boot_mode.h>
36 #ifdef CONFIG_VIDEO_IPUV3
37 #include <ipu.h>
38 #endif
39
40 DECLARE_GLOBAL_DATA_PTR;
41
42 #define TEMPERATURE_MIN                 -40
43 #define TEMPERATURE_HOT                 80
44 #define TEMPERATURE_MAX                 125
45 #define REG_VALUE_TO_CEL(ratio, raw) ((raw_n40c - raw) * 100 / ratio - 40)
46
47 #define __data  __attribute__((section(".data")))
48
49 struct scu_regs {
50         u32     ctrl;
51         u32     config;
52         u32     status;
53         u32     invalidate;
54         u32     fpga_rev;
55 };
56
57 #ifdef CONFIG_HW_WATCHDOG
58 #define wdog_base       ((void *)WDOG1_BASE_ADDR)
59 #define WDOG_WCR        0x00
60 #define WCR_WDE         (1 << 2)
61 #define WDOG_WSR        0x02
62
63 void hw_watchdog_reset(void)
64 {
65         if (readw(wdog_base + WDOG_WCR) & WCR_WDE) {
66                 static u16 toggle = 0xaaaa;
67                 static int first = 1;
68
69                 if (first) {
70                         printf("Watchdog active\n");
71                         first = 0;
72                 }
73                 writew(toggle, wdog_base + WDOG_WSR);
74                 toggle ^= 0xffff;
75         }
76 }
77 #endif
78
79 u32 get_cpu_rev(void)
80 {
81         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
82         u32 reg = readl(&anatop->digprog_sololite);
83         u32 type = ((reg >> 16) & 0xff);
84
85         if (type != MXC_CPU_MX6SL) {
86                 reg = readl(&anatop->digprog);
87                 type = ((reg >> 16) & 0xff);
88                 if (type == MXC_CPU_MX6DL) {
89                         struct scu_regs *scu = (struct scu_regs *)SCU_BASE_ADDR;
90                         u32 cfg = readl(&scu->config) & 3;
91
92                         if (!cfg)
93                                 type = MXC_CPU_MX6SOLO;
94                 }
95         }
96         reg &= 0xff;            /* mx6 silicon revision */
97         return (type << 12) | (reg + 0x10);
98 }
99
100 void init_aips(void)
101 {
102         struct aipstz_regs *aips1, *aips2;
103
104         aips1 = (struct aipstz_regs *)AIPS1_BASE_ADDR;
105         aips2 = (struct aipstz_regs *)AIPS2_BASE_ADDR;
106
107         /*
108          * Set all MPROTx to be non-bufferable, trusted for R/W,
109          * not forced to user-mode.
110          */
111         writel(0x77777777, &aips1->mprot0);
112         writel(0x77777777, &aips1->mprot1);
113         writel(0x77777777, &aips2->mprot0);
114         writel(0x77777777, &aips2->mprot1);
115
116         /*
117          * Set all OPACRx to be non-bufferable, not require
118          * supervisor privilege level for access,allow for
119          * write access and untrusted master access.
120          */
121         writel(0x00000000, &aips1->opacr0);
122         writel(0x00000000, &aips1->opacr1);
123         writel(0x00000000, &aips1->opacr2);
124         writel(0x00000000, &aips1->opacr3);
125         writel(0x00000000, &aips1->opacr4);
126         writel(0x00000000, &aips2->opacr0);
127         writel(0x00000000, &aips2->opacr1);
128         writel(0x00000000, &aips2->opacr2);
129         writel(0x00000000, &aips2->opacr3);
130         writel(0x00000000, &aips2->opacr4);
131 }
132
133 /*
134  * Set the VDDSOC
135  *
136  * Mask out the REG_CORE[22:18] bits (REG2_TRIG) and set
137  * them to the specified millivolt level.
138  * Possible values are from 0.725V to 1.450V in steps of
139  * 0.025V (25mV).
140  */
141 static void set_vddsoc(u32 mv)
142 {
143         struct anatop_regs *anatop = (struct anatop_regs *)ANATOP_BASE_ADDR;
144         u32 val, reg = readl(&anatop->reg_core);
145
146         if (mv < 725)
147                 val = 0x00;     /* Power gated off */
148         else if (mv > 1450)
149                 val = 0x1F;     /* Power FET switched full on. No regulation */
150         else
151                 val = (mv - 700) / 25;
152
153         /*
154          * Mask out the REG_CORE[22:18] bits (REG2_TRIG)
155          * and set them to the calculated value (0.7V + val * 0.25V)
156          */
157         reg = (reg & ~(0x1F << 18)) | (val << 18);
158         writel(reg, &anatop->reg_core);
159 }
160
161 static u32 __data thermal_calib;
162
163 int read_cpu_temperature(void)
164 {
165         unsigned int reg, tmp, i;
166         unsigned int raw_25c, raw_hot, hot_temp, raw_n40c, ratio;
167         int temperature;
168         struct anatop_regs *const anatop = (void *)ANATOP_BASE_ADDR;
169         struct mx6_ocotp_regs *const ocotp_regs = (void *)OCOTP_BASE_ADDR;
170
171         if (!thermal_calib) {
172                 ocotp_clk_enable();
173                 writel(1, &ocotp_regs->hw_ocotp_read_ctrl);
174                 thermal_calib = readl(&ocotp_regs->hw_ocotp_ana1);
175                 writel(0, &ocotp_regs->hw_ocotp_read_ctrl);
176                 ocotp_clk_disable();
177         }
178
179         if (thermal_calib == 0 || thermal_calib == 0xffffffff)
180                 return TEMPERATURE_MIN;
181
182         /* Fuse data layout:
183          * [31:20] sensor value @ 25C
184          * [19:8] sensor value of hot
185          * [7:0] hot temperature value */
186         raw_25c = thermal_calib >> 20;
187         raw_hot = (thermal_calib & 0xfff00) >> 8;
188         hot_temp = thermal_calib & 0xff;
189
190         ratio = ((raw_25c - raw_hot) * 100) / (hot_temp - 25);
191         raw_n40c = raw_25c + (13 * ratio) / 20;
192
193         /* now we only using single measure, every time we measure
194         the temperature, we will power on/down the anadig module*/
195         writel(BM_ANADIG_TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_clr);
196         writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_set);
197
198         /* write measure freq */
199         reg = readl(&anatop->tempsense1);
200         reg &= ~BM_ANADIG_TEMPSENSE1_MEASURE_FREQ;
201         reg |= 327;
202         writel(reg, &anatop->tempsense1);
203
204         writel(BM_ANADIG_TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_clr);
205         writel(BM_ANADIG_TEMPSENSE0_FINISHED, &anatop->tempsense0_clr);
206         writel(BM_ANADIG_TEMPSENSE0_MEASURE_TEMP, &anatop->tempsense0_set);
207
208         tmp = 0;
209         /* read five times of temperature values to get average*/
210         for (i = 0; i < 5; i++) {
211                 while ((readl(&anatop->tempsense0) &
212                                 BM_ANADIG_TEMPSENSE0_FINISHED) == 0)
213                         udelay(10000);
214                 reg = readl(&anatop->tempsense0);
215                 tmp += (reg & BM_ANADIG_TEMPSENSE0_TEMP_VALUE) >>
216                         BP_ANADIG_TEMPSENSE0_TEMP_VALUE;
217                 writel(BM_ANADIG_TEMPSENSE0_FINISHED,
218                         &anatop->tempsense0_clr);
219         }
220
221         tmp = tmp / 5;
222         if (tmp <= raw_n40c)
223                 temperature = REG_VALUE_TO_CEL(ratio, tmp);
224         else
225                 temperature = TEMPERATURE_MIN;
226
227         /* power down anatop thermal sensor */
228         writel(BM_ANADIG_TEMPSENSE0_POWER_DOWN, &anatop->tempsense0_set);
229         writel(BM_ANADIG_ANA_MISC0_REFTOP_SELBIASOFF, &anatop->ana_misc0_clr);
230
231         return temperature;
232 }
233
234 int check_cpu_temperature(int boot)
235 {
236         static int __data max_temp;
237         int boot_limit = TEMPERATURE_HOT;
238         int tmp = read_cpu_temperature();
239
240 debug("max_temp[%p]=%d diff=%d\n", &max_temp, max_temp, tmp - max_temp);
241
242         if (tmp < TEMPERATURE_MIN || tmp > TEMPERATURE_MAX) {
243                 printf("Temperature:   can't get valid data!\n");
244                 return tmp;
245         }
246
247         while (tmp >= boot_limit) {
248                 if (boot) {
249                         printf("CPU is %d C, too hot to boot, waiting...\n",
250                                 tmp);
251                         udelay(5000000);
252                         tmp = read_cpu_temperature();
253                         boot_limit = TEMPERATURE_HOT - 1;
254                 } else {
255                         printf("CPU is %d C, too hot, resetting...\n",
256                                 tmp);
257                         udelay(1000000);
258                         reset_cpu(0);
259                 }
260         }
261
262         if (boot) {
263                 printf("Temperature:   %d C, calibration data 0x%x\n",
264                         tmp, thermal_calib);
265         } else if (tmp > max_temp) {
266                 if (tmp > TEMPERATURE_HOT - 5)
267                         printf("WARNING: CPU temperature %d C\n", tmp);
268                 max_temp = tmp;
269         }
270         return tmp;
271 }
272
273 int arch_cpu_init(void)
274 {
275         init_aips();
276
277         set_vddsoc(1200);       /* Set VDDSOC to 1.2V */
278
279 #ifdef CONFIG_VIDEO_IPUV3
280         gd->arch.ipu_hw_rev = IPUV3_HW_REV_IPUV3H;
281 #endif
282 #ifdef  CONFIG_APBH_DMA
283         /* Timer is required for Initializing APBH DMA */
284         timer_init();
285         mxs_dma_init();
286 #endif
287         return 0;
288 }
289
290 #ifndef CONFIG_SYS_DCACHE_OFF
291 void enable_caches(void)
292 {
293         /* Enable D-cache. I-cache is already enabled in start.S */
294         dcache_enable();
295 }
296 #endif
297
298 #if defined(CONFIG_FEC_MXC)
299 void imx_get_mac_from_fuse(int dev_id, unsigned char *mac)
300 {
301         struct iim_regs *iim = (struct iim_regs *)IMX_IIM_BASE;
302         struct fuse_bank *bank = &iim->bank[4];
303         struct fuse_bank4_regs *fuse =
304                         (struct fuse_bank4_regs *)bank->fuse_regs;
305
306         u32 value = readl(&fuse->mac_addr_high);
307         mac[0] = (value >> 8);
308         mac[1] = value;
309
310         value = readl(&fuse->mac_addr_low);
311         mac[2] = value >> 24;
312         mac[3] = value >> 16;
313         mac[4] = value >> 8;
314         mac[5] = value;
315 }
316 #endif
317
318 void boot_mode_apply(unsigned cfg_val)
319 {
320         unsigned reg;
321         struct src *psrc = (struct src *)SRC_BASE_ADDR;
322         writel(cfg_val, &psrc->gpr9);
323         reg = readl(&psrc->gpr10);
324         if (cfg_val)
325                 reg |= 1 << 28;
326         else
327                 reg &= ~(1 << 28);
328         writel(reg, &psrc->gpr10);
329 }
330 /*
331  * cfg_val will be used for
332  * Boot_cfg4[7:0]:Boot_cfg3[7:0]:Boot_cfg2[7:0]:Boot_cfg1[7:0]
333  * After reset, if GPR10[28] is 1, ROM will copy GPR9[25:0]
334  * to SBMR1, which will determine the boot device.
335  */
336 const struct boot_mode soc_boot_modes[] = {
337         {"normal",      MAKE_CFGVAL(0x00, 0x00, 0x00, 0x00)},
338         /* reserved value should start rom usb */
339         {"usb",         MAKE_CFGVAL(0x01, 0x00, 0x00, 0x00)},
340         {"sata",        MAKE_CFGVAL(0x20, 0x00, 0x00, 0x00)},
341         {"escpi1:0",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x08)},
342         {"escpi1:1",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x18)},
343         {"escpi1:2",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x28)},
344         {"escpi1:3",    MAKE_CFGVAL(0x30, 0x00, 0x00, 0x38)},
345         /* 4 bit bus width */
346         {"esdhc1",      MAKE_CFGVAL(0x40, 0x20, 0x00, 0x00)},
347         {"esdhc2",      MAKE_CFGVAL(0x40, 0x28, 0x00, 0x00)},
348         {"esdhc3",      MAKE_CFGVAL(0x40, 0x30, 0x00, 0x00)},
349         {"esdhc4",      MAKE_CFGVAL(0x40, 0x38, 0x00, 0x00)},
350         {NULL,          0},
351 };
352 #define RESET_MAX_TIMEOUT               1000000
353 #define MXS_BLOCK_SFTRST                (1 << 31)
354 #define MXS_BLOCK_CLKGATE               (1 << 30)
355 #include <div64.h>
356
357 static const int scale = 1;
358
359 int mxs_wait_mask_set(struct mx6_register_32 *mx6_reg, uint32_t mask, unsigned long timeout)
360 {
361         unsigned long loops = 0;
362
363         timeout /= scale;
364         if (timeout == 0)
365                 timeout++;
366
367         /* Wait for at least one microsecond for the bit mask to be set */
368         while ((readl(&mx6_reg->reg) & mask) != mask) {
369                 if ((loops += scale) >= timeout) {
370                         printf("MASK %08x in %p not set after %lu ticks\n",
371                                 mask, &mx6_reg->reg, loops * scale);
372                         return 1;
373                 }
374                 udelay(scale);
375         }
376         if (loops == 0)
377                 udelay(1);
378
379         return 0;
380 }
381
382 int mxs_wait_mask_clr(struct mx6_register_32 *mx6_reg, uint32_t mask, unsigned long timeout)
383 {
384         unsigned long loops = 0;
385
386         timeout /= scale;
387         if (timeout == 0)
388                 timeout++;
389
390         /* Wait for at least one microsecond for the bit mask to be cleared */
391         while ((readl(&mx6_reg->reg) & mask) != 0) {
392                 if ((loops += scale) >= timeout) {
393                         printf("MASK %08x in %p not cleared after %lu ticks\n",
394                                 mask, &mx6_reg->reg, loops * scale);
395                         return 1;
396                 }
397                 udelay(scale);
398         }
399         if (loops == 0)
400                 udelay(1);
401
402         return 0;
403 }
404
405 int mxs_reset_block(struct mx6_register_32 *mx6_reg)
406 {
407         /* Clear SFTRST */
408         writel(MXS_BLOCK_SFTRST, &mx6_reg->reg_clr);
409
410         if (mxs_wait_mask_clr(mx6_reg, MXS_BLOCK_SFTRST, RESET_MAX_TIMEOUT)) {
411                 printf("TIMEOUT waiting for SFTRST[%p] to clear: %08x\n",
412                         &mx6_reg->reg, readl(&mx6_reg->reg));
413                 return 1;
414         }
415
416         /* Clear CLKGATE */
417         writel(MXS_BLOCK_CLKGATE, &mx6_reg->reg_clr);
418
419         /* Set SFTRST */
420         writel(MXS_BLOCK_SFTRST, &mx6_reg->reg_set);
421
422         /* Wait for CLKGATE being set */
423         if (mxs_wait_mask_set(mx6_reg, MXS_BLOCK_CLKGATE, RESET_MAX_TIMEOUT)) {
424                 printf("TIMEOUT waiting for CLKGATE[%p] to set: %08x\n",
425                         &mx6_reg->reg, readl(&mx6_reg->reg));
426                 return 0;
427         }
428
429         /* Clear SFTRST */
430         writel(MXS_BLOCK_SFTRST, &mx6_reg->reg_clr);
431
432         if (mxs_wait_mask_clr(mx6_reg, MXS_BLOCK_SFTRST, RESET_MAX_TIMEOUT)) {
433                 printf("TIMEOUT waiting for SFTRST[%p] to clear: %08x\n",
434                         &mx6_reg->reg, readl(&mx6_reg->reg));
435                 return 1;
436         }
437
438         /* Clear CLKGATE */
439         writel(MXS_BLOCK_CLKGATE, &mx6_reg->reg_clr);
440
441         if (mxs_wait_mask_clr(mx6_reg, MXS_BLOCK_CLKGATE, RESET_MAX_TIMEOUT)) {
442                 printf("TIMEOUT waiting for CLKGATE[%p] to clear: %08x\n",
443                         &mx6_reg->reg, readl(&mx6_reg->reg));
444                 return 1;
445         }
446
447         return 0;
448 }