Merge git://git.denx.de/u-boot-usb
[platform/kernel/u-boot.git] / arch / arm / imx-common / cpu.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 <bootm.h>
11 #include <common.h>
12 #include <netdev.h>
13 #include <asm/errno.h>
14 #include <asm/io.h>
15 #include <asm/arch/imx-regs.h>
16 #include <asm/arch/clock.h>
17 #include <asm/arch/sys_proto.h>
18 #include <asm/arch/crm_regs.h>
19 #include <imx_thermal.h>
20 #include <ipu_pixfmt.h>
21 #include <thermal.h>
22 #include <sata.h>
23
24 #ifdef CONFIG_FSL_ESDHC
25 #include <fsl_esdhc.h>
26 #endif
27
28 #if defined(CONFIG_DISPLAY_CPUINFO)
29 static u32 reset_cause = -1;
30
31 static char *get_reset_cause(void)
32 {
33         u32 cause;
34         struct src *src_regs = (struct src *)SRC_BASE_ADDR;
35
36         cause = readl(&src_regs->srsr);
37         writel(cause, &src_regs->srsr);
38         reset_cause = cause;
39
40         switch (cause) {
41         case 0x00001:
42         case 0x00011:
43                 return "POR";
44         case 0x00004:
45                 return "CSU";
46         case 0x00008:
47                 return "IPP USER";
48         case 0x00010:
49                 return "WDOG";
50         case 0x00020:
51                 return "JTAG HIGH-Z";
52         case 0x00040:
53                 return "JTAG SW";
54         case 0x10000:
55                 return "WARM BOOT";
56         default:
57                 return "unknown reset";
58         }
59 }
60
61 u32 get_imx_reset_cause(void)
62 {
63         return reset_cause;
64 }
65 #endif
66
67 #if defined(CONFIG_MX53) || defined(CONFIG_MX6)
68 #if defined(CONFIG_MX53)
69 #define MEMCTL_BASE     ESDCTL_BASE_ADDR
70 #else
71 #define MEMCTL_BASE     MMDC_P0_BASE_ADDR
72 #endif
73 static const unsigned char col_lookup[] = {9, 10, 11, 8, 12, 9, 9, 9};
74 static const unsigned char bank_lookup[] = {3, 2};
75
76 /* these MMDC registers are common to the IMX53 and IMX6 */
77 struct esd_mmdc_regs {
78         uint32_t        ctl;
79         uint32_t        pdc;
80         uint32_t        otc;
81         uint32_t        cfg0;
82         uint32_t        cfg1;
83         uint32_t        cfg2;
84         uint32_t        misc;
85 };
86
87 #define ESD_MMDC_CTL_GET_ROW(mdctl)     ((ctl >> 24) & 7)
88 #define ESD_MMDC_CTL_GET_COLUMN(mdctl)  ((ctl >> 20) & 7)
89 #define ESD_MMDC_CTL_GET_WIDTH(mdctl)   ((ctl >> 16) & 3)
90 #define ESD_MMDC_CTL_GET_CS1(mdctl)     ((ctl >> 30) & 1)
91 #define ESD_MMDC_MISC_GET_BANK(mdmisc)  ((misc >> 5) & 1)
92
93 /*
94  * imx_ddr_size - return size in bytes of DRAM according MMDC config
95  * The MMDC MDCTL register holds the number of bits for row, col, and data
96  * width and the MMDC MDMISC register holds the number of banks. Combine
97  * all these bits to determine the meme size the MMDC has been configured for
98  */
99 unsigned imx_ddr_size(void)
100 {
101         struct esd_mmdc_regs *mem = (struct esd_mmdc_regs *)MEMCTL_BASE;
102         unsigned ctl = readl(&mem->ctl);
103         unsigned misc = readl(&mem->misc);
104         int bits = 11 + 0 + 0 + 1;      /* row + col + bank + width */
105
106         bits += ESD_MMDC_CTL_GET_ROW(ctl);
107         bits += col_lookup[ESD_MMDC_CTL_GET_COLUMN(ctl)];
108         bits += bank_lookup[ESD_MMDC_MISC_GET_BANK(misc)];
109         bits += ESD_MMDC_CTL_GET_WIDTH(ctl);
110         bits += ESD_MMDC_CTL_GET_CS1(ctl);
111
112         /* The MX6 can do only 3840 MiB of DRAM */
113         if (bits == 32)
114                 return 0xf0000000;
115
116         return 1 << bits;
117 }
118 #endif
119
120 #if defined(CONFIG_DISPLAY_CPUINFO)
121
122 const char *get_imx_type(u32 imxtype)
123 {
124         switch (imxtype) {
125         case MXC_CPU_MX6Q:
126                 return "6Q";    /* Quad-core version of the mx6 */
127         case MXC_CPU_MX6D:
128                 return "6D";    /* Dual-core version of the mx6 */
129         case MXC_CPU_MX6DL:
130                 return "6DL";   /* Dual Lite version of the mx6 */
131         case MXC_CPU_MX6SOLO:
132                 return "6SOLO"; /* Solo version of the mx6 */
133         case MXC_CPU_MX6SL:
134                 return "6SL";   /* Solo-Lite version of the mx6 */
135         case MXC_CPU_MX6SX:
136                 return "6SX";   /* SoloX version of the mx6 */
137         case MXC_CPU_MX51:
138                 return "51";
139         case MXC_CPU_MX53:
140                 return "53";
141         default:
142                 return "??";
143         }
144 }
145
146 int print_cpuinfo(void)
147 {
148         u32 cpurev;
149         __maybe_unused u32 max_freq;
150
151 #if defined(CONFIG_MX6) && defined(CONFIG_IMX6_THERMAL)
152         struct udevice *thermal_dev;
153         int cpu_tmp, minc, maxc, ret;
154 #endif
155
156         cpurev = get_cpu_rev();
157
158 #if defined(CONFIG_MX6)
159         printf("CPU:   Freescale i.MX%s rev%d.%d",
160                get_imx_type((cpurev & 0xFF000) >> 12),
161                (cpurev & 0x000F0) >> 4,
162                (cpurev & 0x0000F) >> 0);
163         max_freq = get_cpu_speed_grade_hz();
164         if (!max_freq || max_freq == mxc_get_clock(MXC_ARM_CLK)) {
165                 printf(" at %dMHz\n", mxc_get_clock(MXC_ARM_CLK) / 1000000);
166         } else {
167                 printf(" %d MHz (running at %d MHz)\n", max_freq / 1000000,
168                        mxc_get_clock(MXC_ARM_CLK) / 1000000);
169         }
170 #else
171         printf("CPU:   Freescale i.MX%s rev%d.%d at %d MHz\n",
172                 get_imx_type((cpurev & 0xFF000) >> 12),
173                 (cpurev & 0x000F0) >> 4,
174                 (cpurev & 0x0000F) >> 0,
175                 mxc_get_clock(MXC_ARM_CLK) / 1000000);
176 #endif
177
178 #if defined(CONFIG_MX6) && defined(CONFIG_IMX6_THERMAL)
179         puts("CPU:   ");
180         switch (get_cpu_temp_grade(&minc, &maxc)) {
181         case TEMP_AUTOMOTIVE:
182                 puts("Automotive temperature grade ");
183                 break;
184         case TEMP_INDUSTRIAL:
185                 puts("Industrial temperature grade ");
186                 break;
187         case TEMP_EXTCOMMERCIAL:
188                 puts("Extended Commercial temperature grade ");
189                 break;
190         default:
191                 puts("Commercial temperature grade ");
192                 break;
193         }
194         printf("(%dC to %dC)", minc, maxc);
195         ret = uclass_get_device(UCLASS_THERMAL, 0, &thermal_dev);
196         if (!ret) {
197                 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
198
199                 if (!ret)
200                         printf(" at %dC\n", cpu_tmp);
201                 else
202                         puts(" - invalid sensor data\n");
203         } else {
204                 puts(" - invalid sensor device\n");
205         }
206 #endif
207
208         printf("Reset cause: %s\n", get_reset_cause());
209         return 0;
210 }
211 #endif
212
213 int cpu_eth_init(bd_t *bis)
214 {
215         int rc = -ENODEV;
216
217 #if defined(CONFIG_FEC_MXC)
218         rc = fecmxc_initialize(bis);
219 #endif
220
221         return rc;
222 }
223
224 #ifdef CONFIG_FSL_ESDHC
225 /*
226  * Initializes on-chip MMC controllers.
227  * to override, implement board_mmc_init()
228  */
229 int cpu_mmc_init(bd_t *bis)
230 {
231         return fsl_esdhc_mmc_init(bis);
232 }
233 #endif
234
235 u32 get_ahb_clk(void)
236 {
237         struct mxc_ccm_reg *imx_ccm = (struct mxc_ccm_reg *)CCM_BASE_ADDR;
238         u32 reg, ahb_podf;
239
240         reg = __raw_readl(&imx_ccm->cbcdr);
241         reg &= MXC_CCM_CBCDR_AHB_PODF_MASK;
242         ahb_podf = reg >> MXC_CCM_CBCDR_AHB_PODF_OFFSET;
243
244         return get_periph_clk() / (ahb_podf + 1);
245 }
246
247 void arch_preboot_os(void)
248 {
249 #if defined(CONFIG_CMD_SATA)
250         sata_stop();
251 #if defined(CONFIG_MX6)
252         disable_sata_clock();
253 #endif
254 #endif
255 #if defined(CONFIG_VIDEO_IPUV3)
256         /* disable video before launching O/S */
257         ipuv3_fb_shutdown();
258 #endif
259 }
260
261 void set_chipselect_size(int const cs_size)
262 {
263         unsigned int reg;
264         struct iomuxc *iomuxc_regs = (struct iomuxc *)IOMUXC_BASE_ADDR;
265         reg = readl(&iomuxc_regs->gpr[1]);
266
267         switch (cs_size) {
268         case CS0_128:
269                 reg &= ~0x7;    /* CS0=128MB, CS1=0, CS2=0, CS3=0 */
270                 reg |= 0x5;
271                 break;
272         case CS0_64M_CS1_64M:
273                 reg &= ~0x3F;   /* CS0=64MB, CS1=64MB, CS2=0, CS3=0 */
274                 reg |= 0x1B;
275                 break;
276         case CS0_64M_CS1_32M_CS2_32M:
277                 reg &= ~0x1FF;  /* CS0=64MB, CS1=32MB, CS2=32MB, CS3=0 */
278                 reg |= 0x4B;
279                 break;
280         case CS0_32M_CS1_32M_CS2_32M_CS3_32M:
281                 reg &= ~0xFFF;  /* CS0=32MB, CS1=32MB, CS2=32MB, CS3=32MB */
282                 reg |= 0x249;
283                 break;
284         default:
285                 printf("Unknown chip select size: %d\n", cs_size);
286                 break;
287         }
288
289         writel(reg, &iomuxc_regs->gpr[1]);
290 }