ARM: tegra: power: Powergate PCIE and SATA partitions on tegra 3
[linux-3.10.git] / arch / arm / mach-tegra / common.c
1 /*
2  * arch/arm/mach-tegra/common.c
3  *
4  * Copyright (C) 2010 Google, Inc.
5  *
6  * Author:
7  *      Colin Cross <ccross@android.com>
8  *
9  * This software is licensed under the terms of the GNU General Public
10  * License version 2, as published by the Free Software Foundation, and
11  * may be copied, distributed, and modified under those terms.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19
20 #include <linux/platform_device.h>
21 #include <linux/init.h>
22 #include <linux/io.h>
23 #include <linux/clk.h>
24 #include <linux/delay.h>
25 #include <linux/irqchip.h>
26 #include <linux/clk/tegra.h>
27 #include <linux/highmem.h>
28 #include <linux/memblock.h>
29 #include <linux/bitops.h>
30
31 #include <asm/hardware/cache-l2x0.h>
32 #include <asm/system_info.h>
33
34 #include <mach/powergate.h>
35
36 #include "board.h"
37 #include "common.h"
38 #include "dvfs.h"
39 #include "fuse.h"
40 #include "iomap.h"
41 #include "pm.h"
42 #include "pmc.h"
43 #include "apbio.h"
44 #include "sleep.h"
45 #include "reset.h"
46
47 #define MC_SECURITY_CFG2        0x7c
48
49 #define AHB_ARBITRATION_PRIORITY_CTRL           0x4
50 #define   AHB_PRIORITY_WEIGHT(x)        (((x) & 0x7) << 29)
51 #define   PRIORITY_SELECT_USB   BIT(6)
52 #define   PRIORITY_SELECT_USB2  BIT(18)
53 #define   PRIORITY_SELECT_USB3  BIT(17)
54
55 #define AHB_GIZMO_AHB_MEM               0xc
56 #define   ENB_FAST_REARBITRATE  BIT(2)
57 #define   DONT_SPLIT_AHB_WR     BIT(7)
58
59 #define AHB_GIZMO_USB           0x1c
60 #define AHB_GIZMO_USB2          0x78
61 #define AHB_GIZMO_USB3          0x7c
62 #define   IMMEDIATE     BIT(18)
63
64 #define AHB_MEM_PREFETCH_CFG3   0xe0
65 #define AHB_MEM_PREFETCH_CFG4   0xe4
66 #define AHB_MEM_PREFETCH_CFG1   0xec
67 #define AHB_MEM_PREFETCH_CFG2   0xf0
68 #define   PREFETCH_ENB  BIT(31)
69 #define   MST_ID(x)     (((x) & 0x1f) << 26)
70 #define   AHBDMA_MST_ID MST_ID(5)
71 #define   USB_MST_ID    MST_ID(6)
72 #define   USB2_MST_ID   MST_ID(18)
73 #define   USB3_MST_ID   MST_ID(17)
74 #define   ADDR_BNDRY(x) (((x) & 0xf) << 21)
75 #define   INACTIVITY_TIMEOUT(x) (((x) & 0xffff) << 0)
76
77 unsigned long tegra_bootloader_fb_start;
78 unsigned long tegra_bootloader_fb_size;
79 unsigned long tegra_fb_start;
80 unsigned long tegra_fb_size;
81 unsigned long tegra_fb2_start;
82 unsigned long tegra_fb2_size;
83 unsigned long tegra_carveout_start;
84 unsigned long tegra_carveout_size;
85 unsigned long tegra_lp0_vec_start;
86 unsigned long tegra_lp0_vec_size;
87 unsigned long tegra_grhost_aperture = ~0ul;
88 static   bool is_tegra_debug_uart_hsport;
89 static struct board_info pmu_board_info;
90
91 static int pmu_core_edp = 1200; /* default 1.2V EDP limit */
92 static int board_panel_type;
93
94 /*
95  * Storage for debug-macro.S's state.
96  *
97  * This must be in .data not .bss so that it gets initialized each time the
98  * kernel is loaded. The data is declared here rather than debug-macro.S so
99  * that multiple inclusions of debug-macro.S point at the same data.
100  */
101 u32 tegra_uart_config[4] = {
102         /* Debug UART initialization required */
103         1,
104         /* Debug UART physical address */
105         0,
106         /* Debug UART virtual address */
107         0,
108         /* Scratch space for debug macro */
109         0,
110 };
111
112 #ifdef CONFIG_OF
113 void __init tegra_dt_init_irq(void)
114 {
115         tegra_clocks_init();
116         tegra_pmc_init();
117         tegra_init_irq();
118         irqchip_init();
119 }
120 #endif
121
122 void tegra_assert_system_reset(char mode, const char *cmd)
123 {
124         void __iomem *reset = IO_ADDRESS(TEGRA_PMC_BASE + 0);
125         u32 reg;
126
127         reg = readl_relaxed(reset);
128         reg |= 0x10;
129         writel_relaxed(reg, reset);
130 }
131
132 void tegra_init_cache(u32 tag_latency, u32 data_latency)
133 {
134 #ifdef CONFIG_CACHE_L2X0
135         void __iomem *p = IO_ADDRESS(TEGRA_ARM_PERIF_BASE) + 0x3000;
136         u32 aux_ctrl, cache_type;
137         u32 tag_latency, data_latency;
138
139 #if defined(CONFIG_ARCH_TEGRA_2x_SOC)
140         tag_latency = 0x331;
141         data_latency = 0x441;
142 #elif defined(CONFIG_ARCH_TEGRA_3x_SOC)
143 #ifdef CONFIG_TEGRA_SILICON_PLATFORM
144         if (is_lp_cluster()) {
145                 tag_latency = 0x221;
146                 data_latency = 0x221;
147         } else {
148                 tag_latency = 0x331;
149                 data_latency = 0x441;
150         }
151 #else
152         tag_latency = 0x770;
153         data_latency = 0x770;
154 #endif
155 #endif
156         writel_relaxed(tag_latency, p + L2X0_TAG_LATENCY_CTRL);
157         writel_relaxed(data_latency, p + L2X0_DATA_LATENCY_CTRL);
158
159 #if defined(CONFIG_ARCH_TEGRA_3x_SOC)
160 #ifndef CONFIG_TEGRA_FPGA_PLATFORM
161         writel(7, p + L2X0_PREFETCH_CTRL);
162         writel(2, p + L2X0_POWER_CTRL);
163 #endif  
164
165         /* Enable PL310 double line fill feature. */
166         writel(((1<<30) | 0), p + L2X0_PREFETCH_CTRL);
167 #endif
168
169         cache_type = readl(p + L2X0_CACHE_TYPE);
170         aux_ctrl = (cache_type & 0x700) << (17-8);
171         aux_ctrl |= 0x7C400001;
172
173         l2x0_init(p, aux_ctrl, 0x8200c3fe);
174 #endif
175 }
176
177 static void __init tegra_init_power(void)
178 {
179 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
180         tegra_powergate_partition_with_clk_off(TEGRA_POWERGATE_SATA);
181 #endif
182         tegra_powergate_partition_with_clk_off(TEGRA_POWERGATE_PCIE);
183 }
184
185 static inline unsigned long gizmo_readl(unsigned long offset)
186 {
187         return readl(IO_TO_VIRT(TEGRA_AHB_GIZMO_BASE + offset));
188 }
189
190 static inline void gizmo_writel(unsigned long value, unsigned long offset)
191 {
192         writel(value, IO_TO_VIRT(TEGRA_AHB_GIZMO_BASE + offset));
193 }
194
195 static void __init tegra_init_ahb_gizmo_settings(void)
196 {
197         unsigned long val;
198
199         val = gizmo_readl(AHB_GIZMO_AHB_MEM);
200         val |= ENB_FAST_REARBITRATE | IMMEDIATE | DONT_SPLIT_AHB_WR;
201         gizmo_writel(val, AHB_GIZMO_AHB_MEM);
202
203         val = gizmo_readl(AHB_GIZMO_USB);
204         val |= IMMEDIATE;
205         gizmo_writel(val, AHB_GIZMO_USB);
206
207         val = gizmo_readl(AHB_GIZMO_USB2);
208         val |= IMMEDIATE;
209         gizmo_writel(val, AHB_GIZMO_USB2);
210
211         val = gizmo_readl(AHB_GIZMO_USB3);
212         val |= IMMEDIATE;
213         gizmo_writel(val, AHB_GIZMO_USB3);
214
215         val = gizmo_readl(AHB_ARBITRATION_PRIORITY_CTRL);
216         val |= PRIORITY_SELECT_USB | PRIORITY_SELECT_USB2 | PRIORITY_SELECT_USB3
217                                 | AHB_PRIORITY_WEIGHT(7);
218         gizmo_writel(val, AHB_ARBITRATION_PRIORITY_CTRL);
219
220         val = gizmo_readl(AHB_MEM_PREFETCH_CFG1);
221         val &= ~MST_ID(~0);
222         val |= PREFETCH_ENB | AHBDMA_MST_ID | ADDR_BNDRY(0xc) | INACTIVITY_TIMEOUT(0x1000);
223         gizmo_writel(val, AHB_MEM_PREFETCH_CFG1);
224
225         val = gizmo_readl(AHB_MEM_PREFETCH_CFG2);
226         val &= ~MST_ID(~0);
227         val |= PREFETCH_ENB | USB_MST_ID | ADDR_BNDRY(0xc) | INACTIVITY_TIMEOUT(0x1000);
228         gizmo_writel(val, AHB_MEM_PREFETCH_CFG2);
229
230         val = gizmo_readl(AHB_MEM_PREFETCH_CFG3);
231         val &= ~MST_ID(~0);
232         val |= PREFETCH_ENB | USB3_MST_ID | ADDR_BNDRY(0xc) | INACTIVITY_TIMEOUT(0x1000);
233         gizmo_writel(val, AHB_MEM_PREFETCH_CFG3);
234
235         val = gizmo_readl(AHB_MEM_PREFETCH_CFG4);
236         val &= ~MST_ID(~0);
237         val |= PREFETCH_ENB | USB2_MST_ID | ADDR_BNDRY(0xc) | INACTIVITY_TIMEOUT(0x1000);
238         gizmo_writel(val, AHB_MEM_PREFETCH_CFG4);
239 }
240
241 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
242 void __init tegra20_init_early(void)
243 {
244 #ifndef CONFIG_SMP
245         /* For SMP system, initializing the reset handler here is too
246            late. For non-SMP systems, the function that calls the reset
247            handler initializer is not called, so do it here for non-SMP. */
248         tegra_cpu_reset_handler_init();
249 #endif
250         tegra_apb_io_init();
251         tegra_init_fuse();
252         tegra_init_cache();
253         tegra_powergate_init();
254         tegra20_hotplug_init();
255         tegra_init_power();
256         tegra_init_ahb_gizmo_settings();
257 }
258 #endif
259 #ifdef CONFIG_ARCH_TEGRA_3x_SOC
260 void __init tegra30_init_early(void)
261 {
262 #ifndef CONFIG_SMP
263         /* For SMP system, initializing the reset handler here is too
264            late. For non-SMP systems, the function that calls the reset
265            handler initializer is not called, so do it here for non-SMP. */
266         tegra_cpu_reset_handler_init();
267 #endif
268         tegra_apb_io_init();
269         tegra_init_fuse();
270         tegra_init_cache();
271         tegra_pmc_init();
272         tegra_powergate_init();
273         tegra30_hotplug_init();
274         tegra_init_power();
275         tegra_init_ahb_gizmo_settings();
276 }
277 #endif
278
279 static int __init tegra_lp0_vec_arg(char *options)
280 {
281         char *p = options;
282
283         tegra_lp0_vec_size = memparse(p, &p);
284         if (*p == '@')
285                 tegra_lp0_vec_start = memparse(p+1, &p);
286         if (!tegra_lp0_vec_size || !tegra_lp0_vec_start) {
287                 tegra_lp0_vec_size = 0;
288                 tegra_lp0_vec_start = 0;
289         }
290
291         return 0;
292 }
293 early_param("lp0_vec", tegra_lp0_vec_arg);
294
295 static int __init tegra_bootloader_fb_arg(char *options)
296 {
297         char *p = options;
298         unsigned long start;
299         unsigned long size;
300
301         tegra_bootloader_fb_size = memparse(p, &p);
302         if (*p == '@')
303                 tegra_bootloader_fb_start = memparse(p+1, &p);
304
305         pr_info("Found tegra_fbmem: %08lx@%08lx\n",
306                 tegra_bootloader_fb_size, tegra_bootloader_fb_start);
307
308         return 0;
309 }
310 early_param("tegra_fbmem", tegra_bootloader_fb_arg);
311
312 enum panel_type get_panel_type(void)
313 {
314         return board_panel_type;
315 }
316 static int __init tegra_board_panel_type(char *options)
317 {
318         if (!strcmp(options, "lvds"))
319                 board_panel_type = panel_type_lvds;
320         else if (!strcmp(options, "dsi"))
321                 board_panel_type = panel_type_dsi;
322         else
323                 return 0;
324         return 1;
325 }
326 __setup("panel=", tegra_board_panel_type);
327
328 int get_core_edp(void)
329 {
330         return pmu_core_edp;
331 }
332 static int __init tegra_pmu_core_edp(char *options)
333 {
334         char *p = options;
335         int core_edp = memparse(p, &p);
336         if (core_edp != 0)
337                 pmu_core_edp = core_edp;
338         return 1;
339 }
340 __setup("core_edp_mv=", tegra_pmu_core_edp);
341
342 static int __init tegra_debug_uartport(char *info)
343 {
344         if (!strcmp(info, "hsport"))
345                 is_tegra_debug_uart_hsport = true;
346         else if (!strcmp(info, "lsport"))
347                 is_tegra_debug_uart_hsport = false;
348
349         return 1;
350 }
351
352 bool is_tegra_debug_uartport_hs(void)
353 {
354         return is_tegra_debug_uart_hsport;
355 }
356
357 __setup("debug_uartport=", tegra_debug_uartport);
358
359 void tegra_get_board_info(struct board_info *bi)
360 {
361         bi->board_id = (system_serial_high >> 16) & 0xFFFF;
362         bi->sku = (system_serial_high) & 0xFFFF;
363         bi->fab = (system_serial_low >> 24) & 0xFF;
364         bi->major_revision = (system_serial_low >> 16) & 0xFF;
365         bi->minor_revision = (system_serial_low >> 8) & 0xFF;
366 }
367
368 static int __init tegra_pmu_board_info(char *info)
369 {
370         char *p = info;
371         pmu_board_info.board_id = memparse(p, &p);
372         pmu_board_info.sku = memparse(p+1, &p);
373         pmu_board_info.fab = memparse(p+1, &p);
374         pmu_board_info.major_revision = memparse(p+1, &p);
375         pmu_board_info.minor_revision = memparse(p+1, &p);
376         return 1;
377 }
378
379 void tegra_get_pmu_board_info(struct board_info *bi)
380 {
381         memcpy(bi, &pmu_board_info, sizeof(struct board_info));
382 }
383
384 __setup("pmuboard=", tegra_pmu_board_info);
385
386
387 /*
388  * Tegra has a protected aperture that prevents access by most non-CPU
389  * memory masters to addresses above the aperture value.  Enabling it
390  * secures the CPU's memory from the GPU, except through the GART.
391  */
392 void __init tegra_protected_aperture_init(unsigned long aperture)
393 {
394 #ifndef CONFIG_NVMAP_ALLOW_SYSMEM
395         void __iomem *mc_base = IO_ADDRESS(TEGRA_MC_BASE);
396         pr_info("Enabling Tegra protected aperture at 0x%08lx\n", aperture);
397         writel(aperture, mc_base + MC_SECURITY_CFG2);
398 #else
399         pr_err("Tegra protected aperture disabled because nvmap is using "
400                 "system memory\n");
401 #endif
402 }
403
404 /*
405  * Due to conflicting restrictions on the placement of the framebuffer,
406  * the bootloader is likely to leave the framebuffer pointed at a location
407  * in memory that is outside the grhost aperture.  This function will move
408  * the framebuffer contents from a physical address that is anywher (lowmem,
409  * highmem, or outside the memory map) to a physical address that is outside
410  * the memory map.
411  */
412 void tegra_move_framebuffer(unsigned long to, unsigned long from,
413         unsigned long size)
414 {
415         struct page *page;
416         void __iomem *to_io;
417         void *from_virt;
418         unsigned long i;
419
420         BUG_ON(PAGE_ALIGN((unsigned long)to) != (unsigned long)to);
421         BUG_ON(PAGE_ALIGN(from) != from);
422         BUG_ON(PAGE_ALIGN(size) != size);
423
424         to_io = ioremap(to, size);
425         if (!to_io) {
426                 pr_err("%s: Failed to map target framebuffer\n", __func__);
427                 return;
428         }
429
430         if (pfn_valid(page_to_pfn(phys_to_page(from)))) {
431                 for (i = 0 ; i < size; i += PAGE_SIZE) {
432                         page = phys_to_page(from + i);
433                         from_virt = kmap(page);
434                         memcpy_toio(to_io + i, from_virt, PAGE_SIZE);
435                         kunmap(page);
436                 }
437         } else {
438                 void __iomem *from_io = ioremap(from, size);
439                 if (!from_io) {
440                         pr_err("%s: Failed to map source framebuffer\n",
441                                 __func__);
442                         goto out;
443                 }
444
445                 for (i = 0; i < size; i += 4)
446                         writel(readl(from_io + i), to_io + i);
447
448                 iounmap(from_io);
449         }
450 out:
451         iounmap(to_io);
452 }
453
454 #ifdef CONFIG_TEGRA_IOVMM_SMMU
455 /* Support for Tegra3 A01 chip mask that needs to have SMMU IOVA reside in
456  * the upper half of 4GB IOVA space. A02 and after use the bottom 1GB and
457  * do not need to reserve memory.
458  */
459 #define SUPPORT_TEGRA_3_IOVMM_SMMU_A01
460 #endif
461
462 void __init tegra_reserve(unsigned long carveout_size, unsigned long fb_size,
463         unsigned long fb2_size)
464 {
465 #ifdef SUPPORT_TEGRA_3_IOVMM_SMMU_A01
466         extern struct platform_device tegra_smmu_device;
467         int smmu_reserved = 0;
468         struct resource *smmu_window =
469                     platform_get_resource_byname(&tegra_smmu_device,
470                                                 IORESOURCE_MEM, "smmu");
471 #endif
472         if (tegra_lp0_vec_size)
473                 if (memblock_reserve(tegra_lp0_vec_start, tegra_lp0_vec_size)) {
474                         pr_err("Failed to reserve lp0_vec %08lx@%08lx\n",
475                                 tegra_lp0_vec_size, tegra_lp0_vec_start);
476                         tegra_lp0_vec_start = 0;
477                         tegra_lp0_vec_size = 0;
478                 }
479
480         if (carveout_size) {
481                 tegra_carveout_start = memblock_end_of_DRAM() - carveout_size;
482                 if (memblock_remove(tegra_carveout_start, carveout_size)) {
483                         pr_err("Failed to remove carveout %08lx@%08lx "
484                                 "from memory map\n",
485                                 carveout_size, tegra_carveout_start);
486                         tegra_carveout_start = 0;
487                         tegra_carveout_size = 0;
488                 } else
489                         tegra_carveout_size = carveout_size;
490         }
491
492         if (fb2_size) {
493                 tegra_fb2_start = memblock_end_of_DRAM() - fb2_size;
494                 if (memblock_remove(tegra_fb2_start, fb2_size)) {
495                         pr_err("Failed to remove second framebuffer "
496                                 "%08lx@%08lx from memory map\n",
497                                 fb2_size, tegra_fb2_start);
498                         tegra_fb2_start = 0;
499                         tegra_fb2_size = 0;
500                 } else
501                         tegra_fb2_size = fb2_size;
502         }
503
504         if (fb_size) {
505                 tegra_fb_start = memblock_end_of_DRAM() - fb_size;
506                 if (memblock_remove(tegra_fb_start, fb_size)) {
507                         pr_err("Failed to remove framebuffer %08lx@%08lx "
508                                 "from memory map\n",
509                                 fb_size, tegra_fb_start);
510                         tegra_fb_start = 0;
511                         tegra_fb_size = 0;
512                 } else
513                         tegra_fb_size = fb_size;
514         }
515
516         if (tegra_fb_size)
517                 tegra_grhost_aperture = tegra_fb_start;
518
519         if (tegra_fb2_size && tegra_fb2_start < tegra_grhost_aperture)
520                 tegra_grhost_aperture = tegra_fb2_start;
521
522         if (tegra_carveout_size && tegra_carveout_start < tegra_grhost_aperture)
523                 tegra_grhost_aperture = tegra_carveout_start;
524
525 #ifdef SUPPORT_TEGRA_3_IOVMM_SMMU_A01
526         if (!smmu_window) {
527                 pr_err("No SMMU resources\n");
528         } else {
529                 size_t smmu_window_size;
530
531                 if (tegra_revision == TEGRA_REVISION_A01) {
532                         smmu_window->start = TEGRA_SMMU_BASE_A01;
533                         smmu_window->end   = TEGRA_SMMU_BASE_A01 +
534                                                 TEGRA_SMMU_SIZE_A01 - 1;
535                 }
536                 smmu_window_size = smmu_window->end + 1 - smmu_window->start;
537                 if (smmu_window->start >= 0x80000000) {
538                         if (memblock_reserve(smmu_window->start,
539                                                 smmu_window_size))
540                                 pr_err(
541                         "Failed to reserve SMMU I/O VA window %08lx@%08lx\n",
542                                 (unsigned long)smmu_window_size,
543                                 (unsigned long)smmu_window->start);
544                         else
545                                 smmu_reserved = 1;
546                 }
547         }
548 #endif
549
550         /*
551          * We copy the bootloader's framebuffer to the framebuffer allocated
552          * above, and then free this one.
553          * */
554         if (tegra_bootloader_fb_size) {
555                 tegra_bootloader_fb_size = PAGE_ALIGN(tegra_bootloader_fb_size);
556                 if (memblock_reserve(tegra_bootloader_fb_start,
557                                 tegra_bootloader_fb_size)) {
558                         pr_err("Failed to reserve bootloader frame buffer "
559                                 "%08lx@%08lx\n", tegra_bootloader_fb_size,
560                                 tegra_bootloader_fb_start);
561                         tegra_bootloader_fb_start = 0;
562                         tegra_bootloader_fb_size = 0;
563                 }
564         }
565
566         pr_info("Tegra reserved memory:\n"
567                 "LP0:                    %08lx - %08lx\n"
568                 "Bootloader framebuffer: %08lx - %08lx\n"
569                 "Framebuffer:            %08lx - %08lx\n"
570                 "2nd Framebuffer:        %08lx - %08lx\n"
571                 "Carveout:               %08lx - %08lx\n",
572                 tegra_lp0_vec_start,
573                 tegra_lp0_vec_size ?
574                         tegra_lp0_vec_start + tegra_lp0_vec_size - 1 : 0,
575                 tegra_bootloader_fb_start,
576                 tegra_bootloader_fb_size ?
577                         tegra_bootloader_fb_start + tegra_bootloader_fb_size - 1 : 0,
578                 tegra_fb_start,
579                 tegra_fb_size ?
580                         tegra_fb_start + tegra_fb_size - 1 : 0,
581                 tegra_fb2_start,
582                 tegra_fb2_size ?
583                         tegra_fb2_start + tegra_fb2_size - 1 : 0,
584                 tegra_carveout_start,
585                 tegra_carveout_size ?
586                         tegra_carveout_start + tegra_carveout_size - 1 : 0);
587
588 #ifdef SUPPORT_TEGRA_3_IOVMM_SMMU_A01
589         if (smmu_reserved)
590                 pr_info("SMMU:                   %08x - %08x\n",
591                         smmu_window->start, smmu_window->end);
592 #endif
593 }
594
595 void __init tegra_release_bootloader_fb(void)
596 {
597         /* Since bootloader fb is reserved in common.c, it is freed here. */
598         if (tegra_bootloader_fb_size)
599                 if (memblock_free(tegra_bootloader_fb_start,
600                                                 tegra_bootloader_fb_size))
601                         pr_err("Failed to free bootloader fb.\n");
602 }
603
604 void __init tegra_init_late(void)
605 {
606 #ifndef CONFIG_COMMON_CLK
607         tegra_clk_debugfs_init();
608 #endif
609         tegra_powergate_debugfs_init();
610 }