d13516981629f45131b4538d18129e0d58be4cb2
[cascardo/linux.git] / drivers / soc / tegra / pmc.c
1 /*
2  * drivers/soc/tegra/pmc.c
3  *
4  * Copyright (c) 2010 Google, Inc
5  *
6  * Author:
7  *      Colin Cross <ccross@google.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 #define pr_fmt(fmt) "tegra-pmc: " fmt
21
22 #include <linux/kernel.h>
23 #include <linux/clk.h>
24 #include <linux/clk/tegra.h>
25 #include <linux/debugfs.h>
26 #include <linux/delay.h>
27 #include <linux/err.h>
28 #include <linux/export.h>
29 #include <linux/init.h>
30 #include <linux/io.h>
31 #include <linux/iopoll.h>
32 #include <linux/of.h>
33 #include <linux/of_address.h>
34 #include <linux/of_platform.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_domain.h>
37 #include <linux/reboot.h>
38 #include <linux/reset.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42
43 #include <soc/tegra/common.h>
44 #include <soc/tegra/fuse.h>
45 #include <soc/tegra/pmc.h>
46
47 #define PMC_CNTRL                       0x0
48 #define  PMC_CNTRL_SYSCLK_POLARITY      (1 << 10)  /* sys clk polarity */
49 #define  PMC_CNTRL_SYSCLK_OE            (1 << 11)  /* system clock enable */
50 #define  PMC_CNTRL_SIDE_EFFECT_LP0      (1 << 14)  /* LP0 when CPU pwr gated */
51 #define  PMC_CNTRL_CPU_PWRREQ_POLARITY  (1 << 15)  /* CPU pwr req polarity */
52 #define  PMC_CNTRL_CPU_PWRREQ_OE        (1 << 16)  /* CPU pwr req enable */
53 #define  PMC_CNTRL_INTR_POLARITY        (1 << 17)  /* inverts INTR polarity */
54 #define  PMC_CNTRL_MAIN_RST             (1 <<  4)
55
56 #define DPD_SAMPLE                      0x020
57 #define  DPD_SAMPLE_ENABLE              (1 << 0)
58 #define  DPD_SAMPLE_DISABLE             (0 << 0)
59
60 #define PWRGATE_TOGGLE                  0x30
61 #define  PWRGATE_TOGGLE_START           (1 << 8)
62
63 #define REMOVE_CLAMPING                 0x34
64
65 #define PWRGATE_STATUS                  0x38
66
67 #define PMC_SCRATCH0                    0x50
68 #define  PMC_SCRATCH0_MODE_RECOVERY     (1 << 31)
69 #define  PMC_SCRATCH0_MODE_BOOTLOADER   (1 << 30)
70 #define  PMC_SCRATCH0_MODE_RCM          (1 << 1)
71 #define  PMC_SCRATCH0_MODE_MASK         (PMC_SCRATCH0_MODE_RECOVERY | \
72                                          PMC_SCRATCH0_MODE_BOOTLOADER | \
73                                          PMC_SCRATCH0_MODE_RCM)
74
75 #define PMC_CPUPWRGOOD_TIMER            0xc8
76 #define PMC_CPUPWROFF_TIMER             0xcc
77
78 #define PMC_SCRATCH41                   0x140
79
80 #define PMC_SENSOR_CTRL                 0x1b0
81 #define PMC_SENSOR_CTRL_SCRATCH_WRITE   (1 << 2)
82 #define PMC_SENSOR_CTRL_ENABLE_RST      (1 << 1)
83
84 #define PMC_RST_STATUS                  0x1b4
85 #define  PMC_RST_STATUS_POR             0
86 #define  PMC_RST_STATUS_WATCHDOG        1
87 #define  PMC_RST_STATUS_SENSOR          2
88 #define  PMC_RST_STATUS_SW_MAIN         3
89 #define  PMC_RST_STATUS_LP0             4
90 #define  PMC_RST_STATUS_AOTAG           5
91
92 #define IO_DPD_REQ                      0x1b8
93 #define  IO_DPD_REQ_CODE_IDLE           (0 << 30)
94 #define  IO_DPD_REQ_CODE_OFF            (1 << 30)
95 #define  IO_DPD_REQ_CODE_ON             (2 << 30)
96 #define  IO_DPD_REQ_CODE_MASK           (3 << 30)
97
98 #define IO_DPD_STATUS                   0x1bc
99 #define IO_DPD2_REQ                     0x1c0
100 #define IO_DPD2_STATUS                  0x1c4
101 #define SEL_DPD_TIM                     0x1c8
102
103 #define PMC_SCRATCH54                   0x258
104 #define PMC_SCRATCH54_DATA_SHIFT        8
105 #define PMC_SCRATCH54_ADDR_SHIFT        0
106
107 #define PMC_SCRATCH55                   0x25c
108 #define PMC_SCRATCH55_RESET_TEGRA       (1 << 31)
109 #define PMC_SCRATCH55_CNTRL_ID_SHIFT    27
110 #define PMC_SCRATCH55_PINMUX_SHIFT      24
111 #define PMC_SCRATCH55_16BITOP           (1 << 15)
112 #define PMC_SCRATCH55_CHECKSUM_SHIFT    16
113 #define PMC_SCRATCH55_I2CSLV1_SHIFT     0
114
115 #define GPU_RG_CNTRL                    0x2d4
116
117 struct tegra_powergate {
118         struct generic_pm_domain genpd;
119         struct tegra_pmc *pmc;
120         unsigned int id;
121         struct clk **clks;
122         unsigned int num_clks;
123         struct reset_control **resets;
124         unsigned int num_resets;
125 };
126
127 struct tegra_pmc_soc {
128         unsigned int num_powergates;
129         const char *const *powergates;
130         unsigned int num_cpu_powergates;
131         const u8 *cpu_powergates;
132
133         bool has_tsense_reset;
134         bool has_gpu_clamps;
135 };
136
137 /**
138  * struct tegra_pmc - NVIDIA Tegra PMC
139  * @dev: pointer to PMC device structure
140  * @base: pointer to I/O remapped register region
141  * @clk: pointer to pclk clock
142  * @soc: pointer to SoC data structure
143  * @debugfs: pointer to debugfs entry
144  * @rate: currently configured rate of pclk
145  * @suspend_mode: lowest suspend mode available
146  * @cpu_good_time: CPU power good time (in microseconds)
147  * @cpu_off_time: CPU power off time (in microsecends)
148  * @core_osc_time: core power good OSC time (in microseconds)
149  * @core_pmu_time: core power good PMU time (in microseconds)
150  * @core_off_time: core power off time (in microseconds)
151  * @corereq_high: core power request is active-high
152  * @sysclkreq_high: system clock request is active-high
153  * @combined_req: combined power request for CPU & core
154  * @cpu_pwr_good_en: CPU power good signal is enabled
155  * @lp0_vec_phys: physical base address of the LP0 warm boot code
156  * @lp0_vec_size: size of the LP0 warm boot code
157  * @powergates_available: Bitmap of available power gates
158  * @powergates_lock: mutex for power gate register access
159  */
160 struct tegra_pmc {
161         struct device *dev;
162         void __iomem *base;
163         struct clk *clk;
164         struct dentry *debugfs;
165
166         const struct tegra_pmc_soc *soc;
167
168         unsigned long rate;
169
170         enum tegra_suspend_mode suspend_mode;
171         u32 cpu_good_time;
172         u32 cpu_off_time;
173         u32 core_osc_time;
174         u32 core_pmu_time;
175         u32 core_off_time;
176         bool corereq_high;
177         bool sysclkreq_high;
178         bool combined_req;
179         bool cpu_pwr_good_en;
180         u32 lp0_vec_phys;
181         u32 lp0_vec_size;
182         DECLARE_BITMAP(powergates_available, TEGRA_POWERGATE_MAX);
183
184         struct mutex powergates_lock;
185 };
186
187 static struct tegra_pmc *pmc = &(struct tegra_pmc) {
188         .base = NULL,
189         .suspend_mode = TEGRA_SUSPEND_NONE,
190 };
191
192 static inline struct tegra_powergate *
193 to_powergate(struct generic_pm_domain *domain)
194 {
195         return container_of(domain, struct tegra_powergate, genpd);
196 }
197
198 static u32 tegra_pmc_readl(unsigned long offset)
199 {
200         return readl(pmc->base + offset);
201 }
202
203 static void tegra_pmc_writel(u32 value, unsigned long offset)
204 {
205         writel(value, pmc->base + offset);
206 }
207
208 static inline bool tegra_powergate_state(int id)
209 {
210         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
211                 return (tegra_pmc_readl(GPU_RG_CNTRL) & 0x1) == 0;
212         else
213                 return (tegra_pmc_readl(PWRGATE_STATUS) & BIT(id)) != 0;
214 }
215
216 static inline bool tegra_powergate_is_valid(int id)
217 {
218         return (pmc->soc && pmc->soc->powergates[id]);
219 }
220
221 static inline bool tegra_powergate_is_available(int id)
222 {
223         return test_bit(id, pmc->powergates_available);
224 }
225
226 static int tegra_powergate_lookup(struct tegra_pmc *pmc, const char *name)
227 {
228         unsigned int i;
229
230         if (!pmc || !pmc->soc || !name)
231                 return -EINVAL;
232
233         for (i = 0; i < pmc->soc->num_powergates; i++) {
234                 if (!tegra_powergate_is_valid(i))
235                         continue;
236
237                 if (!strcmp(name, pmc->soc->powergates[i]))
238                         return i;
239         }
240
241         dev_err(pmc->dev, "powergate %s not found\n", name);
242
243         return -ENODEV;
244 }
245
246 /**
247  * tegra_powergate_set() - set the state of a partition
248  * @id: partition ID
249  * @new_state: new state of the partition
250  */
251 static int tegra_powergate_set(unsigned int id, bool new_state)
252 {
253         bool status;
254         int err;
255
256         if (id == TEGRA_POWERGATE_3D && pmc->soc->has_gpu_clamps)
257                 return -EINVAL;
258
259         mutex_lock(&pmc->powergates_lock);
260
261         if (tegra_powergate_state(id) == new_state) {
262                 mutex_unlock(&pmc->powergates_lock);
263                 return 0;
264         }
265
266         tegra_pmc_writel(PWRGATE_TOGGLE_START | id, PWRGATE_TOGGLE);
267
268         err = readx_poll_timeout(tegra_powergate_state, id, status,
269                                  status == new_state, 10, 100000);
270
271         mutex_unlock(&pmc->powergates_lock);
272
273         return err;
274 }
275
276 static int __tegra_powergate_remove_clamping(unsigned int id)
277 {
278         u32 mask;
279
280         mutex_lock(&pmc->powergates_lock);
281
282         /*
283          * On Tegra124 and later, the clamps for the GPU are controlled by a
284          * separate register (with different semantics).
285          */
286         if (id == TEGRA_POWERGATE_3D) {
287                 if (pmc->soc->has_gpu_clamps) {
288                         tegra_pmc_writel(0, GPU_RG_CNTRL);
289                         goto out;
290                 }
291         }
292
293         /*
294          * Tegra 2 has a bug where PCIE and VDE clamping masks are
295          * swapped relatively to the partition ids
296          */
297         if (id == TEGRA_POWERGATE_VDEC)
298                 mask = (1 << TEGRA_POWERGATE_PCIE);
299         else if (id == TEGRA_POWERGATE_PCIE)
300                 mask = (1 << TEGRA_POWERGATE_VDEC);
301         else
302                 mask = (1 << id);
303
304         tegra_pmc_writel(mask, REMOVE_CLAMPING);
305
306 out:
307         mutex_unlock(&pmc->powergates_lock);
308
309         return 0;
310 }
311
312 static void tegra_powergate_disable_clocks(struct tegra_powergate *pg)
313 {
314         unsigned int i;
315
316         for (i = 0; i < pg->num_clks; i++)
317                 clk_disable_unprepare(pg->clks[i]);
318 }
319
320 static int tegra_powergate_enable_clocks(struct tegra_powergate *pg)
321 {
322         unsigned int i;
323         int err;
324
325         for (i = 0; i < pg->num_clks; i++) {
326                 err = clk_prepare_enable(pg->clks[i]);
327                 if (err)
328                         goto out;
329         }
330
331         return 0;
332
333 out:
334         while (i--)
335                 clk_disable_unprepare(pg->clks[i]);
336
337         return err;
338 }
339
340 static int tegra_powergate_reset_assert(struct tegra_powergate *pg)
341 {
342         unsigned int i;
343         int err;
344
345         for (i = 0; i < pg->num_resets; i++) {
346                 err = reset_control_assert(pg->resets[i]);
347                 if (err)
348                         return err;
349         }
350
351         return 0;
352 }
353
354 static int tegra_powergate_reset_deassert(struct tegra_powergate *pg)
355 {
356         unsigned int i;
357         int err;
358
359         for (i = 0; i < pg->num_resets; i++) {
360                 err = reset_control_deassert(pg->resets[i]);
361                 if (err)
362                         return err;
363         }
364
365         return 0;
366 }
367
368 static int tegra_powergate_power_up(struct tegra_powergate *pg,
369                                     bool disable_clocks)
370 {
371         int err;
372
373         err = tegra_powergate_reset_assert(pg);
374         if (err)
375                 return err;
376
377         usleep_range(10, 20);
378
379         err = tegra_powergate_set(pg->id, true);
380         if (err < 0)
381                 return err;
382
383         usleep_range(10, 20);
384
385         err = tegra_powergate_enable_clocks(pg);
386         if (err)
387                 goto disable_clks;
388
389         usleep_range(10, 20);
390
391         err = __tegra_powergate_remove_clamping(pg->id);
392         if (err)
393                 goto disable_clks;
394
395         usleep_range(10, 20);
396
397         err = tegra_powergate_reset_deassert(pg);
398         if (err)
399                 goto powergate_off;
400
401         usleep_range(10, 20);
402
403         if (disable_clocks)
404                 tegra_powergate_disable_clocks(pg);
405
406         return 0;
407
408 disable_clks:
409         tegra_powergate_disable_clocks(pg);
410         usleep_range(10, 20);
411 powergate_off:
412         tegra_powergate_set(pg->id, false);
413
414         return err;
415 }
416
417 static int tegra_powergate_power_down(struct tegra_powergate *pg)
418 {
419         int err;
420
421         err = tegra_powergate_enable_clocks(pg);
422         if (err)
423                 return err;
424
425         usleep_range(10, 20);
426
427         err = tegra_powergate_reset_assert(pg);
428         if (err)
429                 goto disable_clks;
430
431         usleep_range(10, 20);
432
433         tegra_powergate_disable_clocks(pg);
434
435         usleep_range(10, 20);
436
437         err = tegra_powergate_set(pg->id, false);
438         if (err)
439                 goto assert_resets;
440
441         return 0;
442
443 assert_resets:
444         tegra_powergate_enable_clocks(pg);
445         usleep_range(10, 20);
446         tegra_powergate_reset_deassert(pg);
447         usleep_range(10, 20);
448 disable_clks:
449         tegra_powergate_disable_clocks(pg);
450
451         return err;
452 }
453
454 static int tegra_genpd_power_on(struct generic_pm_domain *domain)
455 {
456         struct tegra_powergate *pg = to_powergate(domain);
457         struct tegra_pmc *pmc = pg->pmc;
458         int err;
459
460         err = tegra_powergate_power_up(pg, true);
461         if (err)
462                 dev_err(pmc->dev, "failed to turn on PM domain %s: %d\n",
463                         pg->genpd.name, err);
464
465         return err;
466 }
467
468 static int tegra_genpd_power_off(struct generic_pm_domain *domain)
469 {
470         struct tegra_powergate *pg = to_powergate(domain);
471         struct tegra_pmc *pmc = pg->pmc;
472         int err;
473
474         err = tegra_powergate_power_down(pg);
475         if (err)
476                 dev_err(pmc->dev, "failed to turn off PM domain %s: %d\n",
477                         pg->genpd.name, err);
478
479         return err;
480 }
481
482 /**
483  * tegra_powergate_power_on() - power on partition
484  * @id: partition ID
485  */
486 int tegra_powergate_power_on(unsigned int id)
487 {
488         if (!tegra_powergate_is_available(id))
489                 return -EINVAL;
490
491         return tegra_powergate_set(id, true);
492 }
493
494 /**
495  * tegra_powergate_power_off() - power off partition
496  * @id: partition ID
497  */
498 int tegra_powergate_power_off(unsigned int id)
499 {
500         if (!tegra_powergate_is_available(id))
501                 return -EINVAL;
502
503         return tegra_powergate_set(id, false);
504 }
505 EXPORT_SYMBOL(tegra_powergate_power_off);
506
507 /**
508  * tegra_powergate_is_powered() - check if partition is powered
509  * @id: partition ID
510  */
511 int tegra_powergate_is_powered(unsigned int id)
512 {
513         int status;
514
515         if (!tegra_powergate_is_valid(id))
516                 return -EINVAL;
517
518         mutex_lock(&pmc->powergates_lock);
519         status = tegra_powergate_state(id);
520         mutex_unlock(&pmc->powergates_lock);
521
522         return status;
523 }
524
525 /**
526  * tegra_powergate_remove_clamping() - remove power clamps for partition
527  * @id: partition ID
528  */
529 int tegra_powergate_remove_clamping(unsigned int id)
530 {
531         if (!tegra_powergate_is_available(id))
532                 return -EINVAL;
533
534         return __tegra_powergate_remove_clamping(id);
535 }
536 EXPORT_SYMBOL(tegra_powergate_remove_clamping);
537
538 /**
539  * tegra_powergate_sequence_power_up() - power up partition
540  * @id: partition ID
541  * @clk: clock for partition
542  * @rst: reset for partition
543  *
544  * Must be called with clk disabled, and returns with clk enabled.
545  */
546 int tegra_powergate_sequence_power_up(unsigned int id, struct clk *clk,
547                                       struct reset_control *rst)
548 {
549         struct tegra_powergate pg;
550         int err;
551
552         pg.id = id;
553         pg.clks = &clk;
554         pg.num_clks = 1;
555         pg.resets = &rst;
556         pg.num_resets = 1;
557
558         err = tegra_powergate_power_up(&pg, false);
559         if (err)
560                 pr_err("failed to turn on partition %d: %d\n", id, err);
561
562         return err;
563 }
564 EXPORT_SYMBOL(tegra_powergate_sequence_power_up);
565
566 #ifdef CONFIG_SMP
567 /**
568  * tegra_get_cpu_powergate_id() - convert from CPU ID to partition ID
569  * @cpuid: CPU partition ID
570  *
571  * Returns the partition ID corresponding to the CPU partition ID or a
572  * negative error code on failure.
573  */
574 static int tegra_get_cpu_powergate_id(unsigned int cpuid)
575 {
576         if (pmc->soc && cpuid < pmc->soc->num_cpu_powergates)
577                 return pmc->soc->cpu_powergates[cpuid];
578
579         return -EINVAL;
580 }
581
582 /**
583  * tegra_pmc_cpu_is_powered() - check if CPU partition is powered
584  * @cpuid: CPU partition ID
585  */
586 bool tegra_pmc_cpu_is_powered(unsigned int cpuid)
587 {
588         int id;
589
590         id = tegra_get_cpu_powergate_id(cpuid);
591         if (id < 0)
592                 return false;
593
594         return tegra_powergate_is_powered(id);
595 }
596
597 /**
598  * tegra_pmc_cpu_power_on() - power on CPU partition
599  * @cpuid: CPU partition ID
600  */
601 int tegra_pmc_cpu_power_on(unsigned int cpuid)
602 {
603         int id;
604
605         id = tegra_get_cpu_powergate_id(cpuid);
606         if (id < 0)
607                 return id;
608
609         return tegra_powergate_set(id, true);
610 }
611
612 /**
613  * tegra_pmc_cpu_remove_clamping() - remove power clamps for CPU partition
614  * @cpuid: CPU partition ID
615  */
616 int tegra_pmc_cpu_remove_clamping(unsigned int cpuid)
617 {
618         int id;
619
620         id = tegra_get_cpu_powergate_id(cpuid);
621         if (id < 0)
622                 return id;
623
624         return tegra_powergate_remove_clamping(id);
625 }
626 #endif /* CONFIG_SMP */
627
628 static int tegra_pmc_restart_notify(struct notifier_block *this,
629                                     unsigned long action, void *data)
630 {
631         const char *cmd = data;
632         u32 value;
633
634         value = tegra_pmc_readl(PMC_SCRATCH0);
635         value &= ~PMC_SCRATCH0_MODE_MASK;
636
637         if (cmd) {
638                 if (strcmp(cmd, "recovery") == 0)
639                         value |= PMC_SCRATCH0_MODE_RECOVERY;
640
641                 if (strcmp(cmd, "bootloader") == 0)
642                         value |= PMC_SCRATCH0_MODE_BOOTLOADER;
643
644                 if (strcmp(cmd, "forced-recovery") == 0)
645                         value |= PMC_SCRATCH0_MODE_RCM;
646         }
647
648         tegra_pmc_writel(value, PMC_SCRATCH0);
649
650         /* reset everything but PMC_SCRATCH0 and PMC_RST_STATUS */
651         value = tegra_pmc_readl(PMC_CNTRL);
652         value |= PMC_CNTRL_MAIN_RST;
653         tegra_pmc_writel(value, PMC_CNTRL);
654
655         return NOTIFY_DONE;
656 }
657
658 static struct notifier_block tegra_pmc_restart_handler = {
659         .notifier_call = tegra_pmc_restart_notify,
660         .priority = 128,
661 };
662
663 static int powergate_show(struct seq_file *s, void *data)
664 {
665         unsigned int i;
666         int status;
667
668         seq_printf(s, " powergate powered\n");
669         seq_printf(s, "------------------\n");
670
671         for (i = 0; i < pmc->soc->num_powergates; i++) {
672                 status = tegra_powergate_is_powered(i);
673                 if (status < 0)
674                         continue;
675
676                 seq_printf(s, " %9s %7s\n", pmc->soc->powergates[i],
677                            status ? "yes" : "no");
678         }
679
680         return 0;
681 }
682
683 static int powergate_open(struct inode *inode, struct file *file)
684 {
685         return single_open(file, powergate_show, inode->i_private);
686 }
687
688 static const struct file_operations powergate_fops = {
689         .open = powergate_open,
690         .read = seq_read,
691         .llseek = seq_lseek,
692         .release = single_release,
693 };
694
695 static int tegra_powergate_debugfs_init(void)
696 {
697         pmc->debugfs = debugfs_create_file("powergate", S_IRUGO, NULL, NULL,
698                                            &powergate_fops);
699         if (!pmc->debugfs)
700                 return -ENOMEM;
701
702         return 0;
703 }
704
705 static int tegra_powergate_of_get_clks(struct tegra_powergate *pg,
706                                        struct device_node *np)
707 {
708         struct clk *clk;
709         unsigned int i, count;
710         int err;
711
712         count = of_count_phandle_with_args(np, "clocks", "#clock-cells");
713         if (count == 0)
714                 return -ENODEV;
715
716         pg->clks = kcalloc(count, sizeof(clk), GFP_KERNEL);
717         if (!pg->clks)
718                 return -ENOMEM;
719
720         for (i = 0; i < count; i++) {
721                 pg->clks[i] = of_clk_get(np, i);
722                 if (IS_ERR(pg->clks[i])) {
723                         err = PTR_ERR(pg->clks[i]);
724                         goto err;
725                 }
726         }
727
728         pg->num_clks = count;
729
730         return 0;
731
732 err:
733         while (i--)
734                 clk_put(pg->clks[i]);
735         kfree(pg->clks);
736
737         return err;
738 }
739
740 static int tegra_powergate_of_get_resets(struct tegra_powergate *pg,
741                                          struct device_node *np)
742 {
743         struct reset_control *rst;
744         unsigned int i, count;
745         int err;
746
747         count = of_count_phandle_with_args(np, "resets", "#reset-cells");
748         if (count == 0)
749                 return -ENODEV;
750
751         pg->resets = kcalloc(count, sizeof(rst), GFP_KERNEL);
752         if (!pg->resets)
753                 return -ENOMEM;
754
755         for (i = 0; i < count; i++) {
756                 pg->resets[i] = of_reset_control_get_by_index(np, i);
757                 if (IS_ERR(pg->resets[i])) {
758                         err = PTR_ERR(pg->resets[i]);
759                         goto error;
760                 }
761         }
762
763         pg->num_resets = count;
764
765         return 0;
766
767 error:
768         while (i--)
769                 reset_control_put(pg->resets[i]);
770         kfree(pg->resets);
771
772         return err;
773 }
774
775 static void tegra_powergate_add(struct tegra_pmc *pmc, struct device_node *np)
776 {
777         struct tegra_powergate *pg;
778         bool off;
779         int id;
780
781         pg = kzalloc(sizeof(*pg), GFP_KERNEL);
782         if (!pg)
783                 goto error;
784
785         id = tegra_powergate_lookup(pmc, np->name);
786         if (id < 0)
787                 goto free_mem;
788
789         /*
790          * Clear the bit for this powergate so it cannot be managed
791          * directly via the legacy APIs for controlling powergates.
792          */
793         clear_bit(id, pmc->powergates_available);
794
795         pg->id = id;
796         pg->genpd.name = np->name;
797         pg->genpd.power_off = tegra_genpd_power_off;
798         pg->genpd.power_on = tegra_genpd_power_on;
799         pg->pmc = pmc;
800
801         if (tegra_powergate_of_get_clks(pg, np))
802                 goto set_available;
803
804         if (tegra_powergate_of_get_resets(pg, np))
805                 goto remove_clks;
806
807         off = !tegra_powergate_is_powered(pg->id);
808
809         pm_genpd_init(&pg->genpd, NULL, off);
810
811         if (of_genpd_add_provider_simple(np, &pg->genpd))
812                 goto remove_resets;
813
814         dev_dbg(pmc->dev, "added power domain %s\n", pg->genpd.name);
815
816         return;
817
818 remove_resets:
819         while (pg->num_resets--)
820                 reset_control_put(pg->resets[pg->num_resets]);
821         kfree(pg->resets);
822
823 remove_clks:
824         while (pg->num_clks--)
825                 clk_put(pg->clks[pg->num_clks]);
826         kfree(pg->clks);
827
828 set_available:
829         set_bit(id, pmc->powergates_available);
830
831 free_mem:
832         kfree(pg);
833
834 error:
835         dev_err(pmc->dev, "failed to create power domain for %s\n", np->name);
836 }
837
838 static void tegra_powergate_init(struct tegra_pmc *pmc)
839 {
840         struct device_node *np, *child;
841
842         np = of_get_child_by_name(pmc->dev->of_node, "powergates");
843         if (!np)
844                 return;
845
846         for_each_child_of_node(np, child) {
847                 tegra_powergate_add(pmc, child);
848                 of_node_put(child);
849         }
850
851         of_node_put(np);
852 }
853
854 static int tegra_io_rail_prepare(unsigned int id, unsigned long *request,
855                                  unsigned long *status, unsigned int *bit)
856 {
857         unsigned long rate, value;
858
859         *bit = id % 32;
860
861         /*
862          * There are two sets of 30 bits to select IO rails, but bits 30 and
863          * 31 are control bits rather than IO rail selection bits.
864          */
865         if (id > 63 || *bit == 30 || *bit == 31)
866                 return -EINVAL;
867
868         if (id < 32) {
869                 *status = IO_DPD_STATUS;
870                 *request = IO_DPD_REQ;
871         } else {
872                 *status = IO_DPD2_STATUS;
873                 *request = IO_DPD2_REQ;
874         }
875
876         rate = clk_get_rate(pmc->clk);
877
878         tegra_pmc_writel(DPD_SAMPLE_ENABLE, DPD_SAMPLE);
879
880         /* must be at least 200 ns, in APB (PCLK) clock cycles */
881         value = DIV_ROUND_UP(1000000000, rate);
882         value = DIV_ROUND_UP(200, value);
883         tegra_pmc_writel(value, SEL_DPD_TIM);
884
885         return 0;
886 }
887
888 static int tegra_io_rail_poll(unsigned long offset, unsigned long mask,
889                               unsigned long val, unsigned long timeout)
890 {
891         unsigned long value;
892
893         timeout = jiffies + msecs_to_jiffies(timeout);
894
895         while (time_after(timeout, jiffies)) {
896                 value = tegra_pmc_readl(offset);
897                 if ((value & mask) == val)
898                         return 0;
899
900                 usleep_range(250, 1000);
901         }
902
903         return -ETIMEDOUT;
904 }
905
906 static void tegra_io_rail_unprepare(void)
907 {
908         tegra_pmc_writel(DPD_SAMPLE_DISABLE, DPD_SAMPLE);
909 }
910
911 int tegra_io_rail_power_on(unsigned int id)
912 {
913         unsigned long request, status, value;
914         unsigned int bit, mask;
915         int err;
916
917         mutex_lock(&pmc->powergates_lock);
918
919         err = tegra_io_rail_prepare(id, &request, &status, &bit);
920         if (err)
921                 goto error;
922
923         mask = 1 << bit;
924
925         value = tegra_pmc_readl(request);
926         value |= mask;
927         value &= ~IO_DPD_REQ_CODE_MASK;
928         value |= IO_DPD_REQ_CODE_OFF;
929         tegra_pmc_writel(value, request);
930
931         err = tegra_io_rail_poll(status, mask, 0, 250);
932         if (err) {
933                 pr_info("tegra_io_rail_poll() failed: %d\n", err);
934                 goto error;
935         }
936
937         tegra_io_rail_unprepare();
938
939 error:
940         mutex_unlock(&pmc->powergates_lock);
941
942         return err;
943 }
944 EXPORT_SYMBOL(tegra_io_rail_power_on);
945
946 int tegra_io_rail_power_off(unsigned int id)
947 {
948         unsigned long request, status, value;
949         unsigned int bit, mask;
950         int err;
951
952         mutex_lock(&pmc->powergates_lock);
953
954         err = tegra_io_rail_prepare(id, &request, &status, &bit);
955         if (err) {
956                 pr_info("tegra_io_rail_prepare() failed: %d\n", err);
957                 goto error;
958         }
959
960         mask = 1 << bit;
961
962         value = tegra_pmc_readl(request);
963         value |= mask;
964         value &= ~IO_DPD_REQ_CODE_MASK;
965         value |= IO_DPD_REQ_CODE_ON;
966         tegra_pmc_writel(value, request);
967
968         err = tegra_io_rail_poll(status, mask, mask, 250);
969         if (err)
970                 goto error;
971
972         tegra_io_rail_unprepare();
973
974 error:
975         mutex_unlock(&pmc->powergates_lock);
976
977         return err;
978 }
979 EXPORT_SYMBOL(tegra_io_rail_power_off);
980
981 #ifdef CONFIG_PM_SLEEP
982 enum tegra_suspend_mode tegra_pmc_get_suspend_mode(void)
983 {
984         return pmc->suspend_mode;
985 }
986
987 void tegra_pmc_set_suspend_mode(enum tegra_suspend_mode mode)
988 {
989         if (mode < TEGRA_SUSPEND_NONE || mode >= TEGRA_MAX_SUSPEND_MODE)
990                 return;
991
992         pmc->suspend_mode = mode;
993 }
994
995 void tegra_pmc_enter_suspend_mode(enum tegra_suspend_mode mode)
996 {
997         unsigned long long rate = 0;
998         u32 value;
999
1000         switch (mode) {
1001         case TEGRA_SUSPEND_LP1:
1002                 rate = 32768;
1003                 break;
1004
1005         case TEGRA_SUSPEND_LP2:
1006                 rate = clk_get_rate(pmc->clk);
1007                 break;
1008
1009         default:
1010                 break;
1011         }
1012
1013         if (WARN_ON_ONCE(rate == 0))
1014                 rate = 100000000;
1015
1016         if (rate != pmc->rate) {
1017                 u64 ticks;
1018
1019                 ticks = pmc->cpu_good_time * rate + USEC_PER_SEC - 1;
1020                 do_div(ticks, USEC_PER_SEC);
1021                 tegra_pmc_writel(ticks, PMC_CPUPWRGOOD_TIMER);
1022
1023                 ticks = pmc->cpu_off_time * rate + USEC_PER_SEC - 1;
1024                 do_div(ticks, USEC_PER_SEC);
1025                 tegra_pmc_writel(ticks, PMC_CPUPWROFF_TIMER);
1026
1027                 wmb();
1028
1029                 pmc->rate = rate;
1030         }
1031
1032         value = tegra_pmc_readl(PMC_CNTRL);
1033         value &= ~PMC_CNTRL_SIDE_EFFECT_LP0;
1034         value |= PMC_CNTRL_CPU_PWRREQ_OE;
1035         tegra_pmc_writel(value, PMC_CNTRL);
1036 }
1037 #endif
1038
1039 static int tegra_pmc_parse_dt(struct tegra_pmc *pmc, struct device_node *np)
1040 {
1041         u32 value, values[2];
1042
1043         if (of_property_read_u32(np, "nvidia,suspend-mode", &value)) {
1044         } else {
1045                 switch (value) {
1046                 case 0:
1047                         pmc->suspend_mode = TEGRA_SUSPEND_LP0;
1048                         break;
1049
1050                 case 1:
1051                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1052                         break;
1053
1054                 case 2:
1055                         pmc->suspend_mode = TEGRA_SUSPEND_LP2;
1056                         break;
1057
1058                 default:
1059                         pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1060                         break;
1061                 }
1062         }
1063
1064         pmc->suspend_mode = tegra_pm_validate_suspend_mode(pmc->suspend_mode);
1065
1066         if (of_property_read_u32(np, "nvidia,cpu-pwr-good-time", &value))
1067                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1068
1069         pmc->cpu_good_time = value;
1070
1071         if (of_property_read_u32(np, "nvidia,cpu-pwr-off-time", &value))
1072                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1073
1074         pmc->cpu_off_time = value;
1075
1076         if (of_property_read_u32_array(np, "nvidia,core-pwr-good-time",
1077                                        values, ARRAY_SIZE(values)))
1078                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1079
1080         pmc->core_osc_time = values[0];
1081         pmc->core_pmu_time = values[1];
1082
1083         if (of_property_read_u32(np, "nvidia,core-pwr-off-time", &value))
1084                 pmc->suspend_mode = TEGRA_SUSPEND_NONE;
1085
1086         pmc->core_off_time = value;
1087
1088         pmc->corereq_high = of_property_read_bool(np,
1089                                 "nvidia,core-power-req-active-high");
1090
1091         pmc->sysclkreq_high = of_property_read_bool(np,
1092                                 "nvidia,sys-clock-req-active-high");
1093
1094         pmc->combined_req = of_property_read_bool(np,
1095                                 "nvidia,combined-power-req");
1096
1097         pmc->cpu_pwr_good_en = of_property_read_bool(np,
1098                                 "nvidia,cpu-pwr-good-en");
1099
1100         if (of_property_read_u32_array(np, "nvidia,lp0-vec", values,
1101                                        ARRAY_SIZE(values)))
1102                 if (pmc->suspend_mode == TEGRA_SUSPEND_LP0)
1103                         pmc->suspend_mode = TEGRA_SUSPEND_LP1;
1104
1105         pmc->lp0_vec_phys = values[0];
1106         pmc->lp0_vec_size = values[1];
1107
1108         return 0;
1109 }
1110
1111 static void tegra_pmc_init(struct tegra_pmc *pmc)
1112 {
1113         u32 value;
1114
1115         /* Always enable CPU power request */
1116         value = tegra_pmc_readl(PMC_CNTRL);
1117         value |= PMC_CNTRL_CPU_PWRREQ_OE;
1118         tegra_pmc_writel(value, PMC_CNTRL);
1119
1120         value = tegra_pmc_readl(PMC_CNTRL);
1121
1122         if (pmc->sysclkreq_high)
1123                 value &= ~PMC_CNTRL_SYSCLK_POLARITY;
1124         else
1125                 value |= PMC_CNTRL_SYSCLK_POLARITY;
1126
1127         /* configure the output polarity while the request is tristated */
1128         tegra_pmc_writel(value, PMC_CNTRL);
1129
1130         /* now enable the request */
1131         value = tegra_pmc_readl(PMC_CNTRL);
1132         value |= PMC_CNTRL_SYSCLK_OE;
1133         tegra_pmc_writel(value, PMC_CNTRL);
1134 }
1135
1136 static void tegra_pmc_init_tsense_reset(struct tegra_pmc *pmc)
1137 {
1138         static const char disabled[] = "emergency thermal reset disabled";
1139         u32 pmu_addr, ctrl_id, reg_addr, reg_data, pinmux;
1140         struct device *dev = pmc->dev;
1141         struct device_node *np;
1142         u32 value, checksum;
1143
1144         if (!pmc->soc->has_tsense_reset)
1145                 return;
1146
1147         np = of_find_node_by_name(pmc->dev->of_node, "i2c-thermtrip");
1148         if (!np) {
1149                 dev_warn(dev, "i2c-thermtrip node not found, %s.\n", disabled);
1150                 return;
1151         }
1152
1153         if (of_property_read_u32(np, "nvidia,i2c-controller-id", &ctrl_id)) {
1154                 dev_err(dev, "I2C controller ID missing, %s.\n", disabled);
1155                 goto out;
1156         }
1157
1158         if (of_property_read_u32(np, "nvidia,bus-addr", &pmu_addr)) {
1159                 dev_err(dev, "nvidia,bus-addr missing, %s.\n", disabled);
1160                 goto out;
1161         }
1162
1163         if (of_property_read_u32(np, "nvidia,reg-addr", &reg_addr)) {
1164                 dev_err(dev, "nvidia,reg-addr missing, %s.\n", disabled);
1165                 goto out;
1166         }
1167
1168         if (of_property_read_u32(np, "nvidia,reg-data", &reg_data)) {
1169                 dev_err(dev, "nvidia,reg-data missing, %s.\n", disabled);
1170                 goto out;
1171         }
1172
1173         if (of_property_read_u32(np, "nvidia,pinmux-id", &pinmux))
1174                 pinmux = 0;
1175
1176         value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1177         value |= PMC_SENSOR_CTRL_SCRATCH_WRITE;
1178         tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1179
1180         value = (reg_data << PMC_SCRATCH54_DATA_SHIFT) |
1181                 (reg_addr << PMC_SCRATCH54_ADDR_SHIFT);
1182         tegra_pmc_writel(value, PMC_SCRATCH54);
1183
1184         value = PMC_SCRATCH55_RESET_TEGRA;
1185         value |= ctrl_id << PMC_SCRATCH55_CNTRL_ID_SHIFT;
1186         value |= pinmux << PMC_SCRATCH55_PINMUX_SHIFT;
1187         value |= pmu_addr << PMC_SCRATCH55_I2CSLV1_SHIFT;
1188
1189         /*
1190          * Calculate checksum of SCRATCH54, SCRATCH55 fields. Bits 23:16 will
1191          * contain the checksum and are currently zero, so they are not added.
1192          */
1193         checksum = reg_addr + reg_data + (value & 0xff) + ((value >> 8) & 0xff)
1194                 + ((value >> 24) & 0xff);
1195         checksum &= 0xff;
1196         checksum = 0x100 - checksum;
1197
1198         value |= checksum << PMC_SCRATCH55_CHECKSUM_SHIFT;
1199
1200         tegra_pmc_writel(value, PMC_SCRATCH55);
1201
1202         value = tegra_pmc_readl(PMC_SENSOR_CTRL);
1203         value |= PMC_SENSOR_CTRL_ENABLE_RST;
1204         tegra_pmc_writel(value, PMC_SENSOR_CTRL);
1205
1206         dev_info(pmc->dev, "emergency thermal reset enabled\n");
1207
1208 out:
1209         of_node_put(np);
1210 }
1211
1212 static int tegra_pmc_probe(struct platform_device *pdev)
1213 {
1214         void __iomem *base;
1215         struct resource *res;
1216         int err;
1217
1218         err = tegra_pmc_parse_dt(pmc, pdev->dev.of_node);
1219         if (err < 0)
1220                 return err;
1221
1222         /* take over the memory region from the early initialization */
1223         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1224         base = devm_ioremap_resource(&pdev->dev, res);
1225         if (IS_ERR(base))
1226                 return PTR_ERR(base);
1227
1228         pmc->clk = devm_clk_get(&pdev->dev, "pclk");
1229         if (IS_ERR(pmc->clk)) {
1230                 err = PTR_ERR(pmc->clk);
1231                 dev_err(&pdev->dev, "failed to get pclk: %d\n", err);
1232                 return err;
1233         }
1234
1235         pmc->dev = &pdev->dev;
1236
1237         tegra_pmc_init(pmc);
1238
1239         tegra_pmc_init_tsense_reset(pmc);
1240
1241         if (IS_ENABLED(CONFIG_DEBUG_FS)) {
1242                 err = tegra_powergate_debugfs_init();
1243                 if (err < 0)
1244                         return err;
1245         }
1246
1247         err = register_restart_handler(&tegra_pmc_restart_handler);
1248         if (err) {
1249                 debugfs_remove(pmc->debugfs);
1250                 dev_err(&pdev->dev, "unable to register restart handler, %d\n",
1251                         err);
1252                 return err;
1253         }
1254
1255         tegra_powergate_init(pmc);
1256
1257         mutex_lock(&pmc->powergates_lock);
1258         iounmap(pmc->base);
1259         pmc->base = base;
1260         mutex_unlock(&pmc->powergates_lock);
1261
1262         return 0;
1263 }
1264
1265 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
1266 static int tegra_pmc_suspend(struct device *dev)
1267 {
1268         tegra_pmc_writel(virt_to_phys(tegra_resume), PMC_SCRATCH41);
1269
1270         return 0;
1271 }
1272
1273 static int tegra_pmc_resume(struct device *dev)
1274 {
1275         tegra_pmc_writel(0x0, PMC_SCRATCH41);
1276
1277         return 0;
1278 }
1279
1280 static SIMPLE_DEV_PM_OPS(tegra_pmc_pm_ops, tegra_pmc_suspend, tegra_pmc_resume);
1281
1282 #endif
1283
1284 static const char * const tegra20_powergates[] = {
1285         [TEGRA_POWERGATE_CPU] = "cpu",
1286         [TEGRA_POWERGATE_3D] = "3d",
1287         [TEGRA_POWERGATE_VENC] = "venc",
1288         [TEGRA_POWERGATE_VDEC] = "vdec",
1289         [TEGRA_POWERGATE_PCIE] = "pcie",
1290         [TEGRA_POWERGATE_L2] = "l2",
1291         [TEGRA_POWERGATE_MPE] = "mpe",
1292 };
1293
1294 static const struct tegra_pmc_soc tegra20_pmc_soc = {
1295         .num_powergates = ARRAY_SIZE(tegra20_powergates),
1296         .powergates = tegra20_powergates,
1297         .num_cpu_powergates = 0,
1298         .cpu_powergates = NULL,
1299         .has_tsense_reset = false,
1300         .has_gpu_clamps = false,
1301 };
1302
1303 static const char * const tegra30_powergates[] = {
1304         [TEGRA_POWERGATE_CPU] = "cpu0",
1305         [TEGRA_POWERGATE_3D] = "3d0",
1306         [TEGRA_POWERGATE_VENC] = "venc",
1307         [TEGRA_POWERGATE_VDEC] = "vdec",
1308         [TEGRA_POWERGATE_PCIE] = "pcie",
1309         [TEGRA_POWERGATE_L2] = "l2",
1310         [TEGRA_POWERGATE_MPE] = "mpe",
1311         [TEGRA_POWERGATE_HEG] = "heg",
1312         [TEGRA_POWERGATE_SATA] = "sata",
1313         [TEGRA_POWERGATE_CPU1] = "cpu1",
1314         [TEGRA_POWERGATE_CPU2] = "cpu2",
1315         [TEGRA_POWERGATE_CPU3] = "cpu3",
1316         [TEGRA_POWERGATE_CELP] = "celp",
1317         [TEGRA_POWERGATE_3D1] = "3d1",
1318 };
1319
1320 static const u8 tegra30_cpu_powergates[] = {
1321         TEGRA_POWERGATE_CPU,
1322         TEGRA_POWERGATE_CPU1,
1323         TEGRA_POWERGATE_CPU2,
1324         TEGRA_POWERGATE_CPU3,
1325 };
1326
1327 static const struct tegra_pmc_soc tegra30_pmc_soc = {
1328         .num_powergates = ARRAY_SIZE(tegra30_powergates),
1329         .powergates = tegra30_powergates,
1330         .num_cpu_powergates = ARRAY_SIZE(tegra30_cpu_powergates),
1331         .cpu_powergates = tegra30_cpu_powergates,
1332         .has_tsense_reset = true,
1333         .has_gpu_clamps = false,
1334 };
1335
1336 static const char * const tegra114_powergates[] = {
1337         [TEGRA_POWERGATE_CPU] = "crail",
1338         [TEGRA_POWERGATE_3D] = "3d",
1339         [TEGRA_POWERGATE_VENC] = "venc",
1340         [TEGRA_POWERGATE_VDEC] = "vdec",
1341         [TEGRA_POWERGATE_MPE] = "mpe",
1342         [TEGRA_POWERGATE_HEG] = "heg",
1343         [TEGRA_POWERGATE_CPU1] = "cpu1",
1344         [TEGRA_POWERGATE_CPU2] = "cpu2",
1345         [TEGRA_POWERGATE_CPU3] = "cpu3",
1346         [TEGRA_POWERGATE_CELP] = "celp",
1347         [TEGRA_POWERGATE_CPU0] = "cpu0",
1348         [TEGRA_POWERGATE_C0NC] = "c0nc",
1349         [TEGRA_POWERGATE_C1NC] = "c1nc",
1350         [TEGRA_POWERGATE_DIS] = "dis",
1351         [TEGRA_POWERGATE_DISB] = "disb",
1352         [TEGRA_POWERGATE_XUSBA] = "xusba",
1353         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1354         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1355 };
1356
1357 static const u8 tegra114_cpu_powergates[] = {
1358         TEGRA_POWERGATE_CPU0,
1359         TEGRA_POWERGATE_CPU1,
1360         TEGRA_POWERGATE_CPU2,
1361         TEGRA_POWERGATE_CPU3,
1362 };
1363
1364 static const struct tegra_pmc_soc tegra114_pmc_soc = {
1365         .num_powergates = ARRAY_SIZE(tegra114_powergates),
1366         .powergates = tegra114_powergates,
1367         .num_cpu_powergates = ARRAY_SIZE(tegra114_cpu_powergates),
1368         .cpu_powergates = tegra114_cpu_powergates,
1369         .has_tsense_reset = true,
1370         .has_gpu_clamps = false,
1371 };
1372
1373 static const char * const tegra124_powergates[] = {
1374         [TEGRA_POWERGATE_CPU] = "crail",
1375         [TEGRA_POWERGATE_3D] = "3d",
1376         [TEGRA_POWERGATE_VENC] = "venc",
1377         [TEGRA_POWERGATE_PCIE] = "pcie",
1378         [TEGRA_POWERGATE_VDEC] = "vdec",
1379         [TEGRA_POWERGATE_MPE] = "mpe",
1380         [TEGRA_POWERGATE_HEG] = "heg",
1381         [TEGRA_POWERGATE_SATA] = "sata",
1382         [TEGRA_POWERGATE_CPU1] = "cpu1",
1383         [TEGRA_POWERGATE_CPU2] = "cpu2",
1384         [TEGRA_POWERGATE_CPU3] = "cpu3",
1385         [TEGRA_POWERGATE_CELP] = "celp",
1386         [TEGRA_POWERGATE_CPU0] = "cpu0",
1387         [TEGRA_POWERGATE_C0NC] = "c0nc",
1388         [TEGRA_POWERGATE_C1NC] = "c1nc",
1389         [TEGRA_POWERGATE_SOR] = "sor",
1390         [TEGRA_POWERGATE_DIS] = "dis",
1391         [TEGRA_POWERGATE_DISB] = "disb",
1392         [TEGRA_POWERGATE_XUSBA] = "xusba",
1393         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1394         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1395         [TEGRA_POWERGATE_VIC] = "vic",
1396         [TEGRA_POWERGATE_IRAM] = "iram",
1397 };
1398
1399 static const u8 tegra124_cpu_powergates[] = {
1400         TEGRA_POWERGATE_CPU0,
1401         TEGRA_POWERGATE_CPU1,
1402         TEGRA_POWERGATE_CPU2,
1403         TEGRA_POWERGATE_CPU3,
1404 };
1405
1406 static const struct tegra_pmc_soc tegra124_pmc_soc = {
1407         .num_powergates = ARRAY_SIZE(tegra124_powergates),
1408         .powergates = tegra124_powergates,
1409         .num_cpu_powergates = ARRAY_SIZE(tegra124_cpu_powergates),
1410         .cpu_powergates = tegra124_cpu_powergates,
1411         .has_tsense_reset = true,
1412         .has_gpu_clamps = true,
1413 };
1414
1415 static const char * const tegra210_powergates[] = {
1416         [TEGRA_POWERGATE_CPU] = "crail",
1417         [TEGRA_POWERGATE_3D] = "3d",
1418         [TEGRA_POWERGATE_VENC] = "venc",
1419         [TEGRA_POWERGATE_PCIE] = "pcie",
1420         [TEGRA_POWERGATE_MPE] = "mpe",
1421         [TEGRA_POWERGATE_SATA] = "sata",
1422         [TEGRA_POWERGATE_CPU1] = "cpu1",
1423         [TEGRA_POWERGATE_CPU2] = "cpu2",
1424         [TEGRA_POWERGATE_CPU3] = "cpu3",
1425         [TEGRA_POWERGATE_CPU0] = "cpu0",
1426         [TEGRA_POWERGATE_C0NC] = "c0nc",
1427         [TEGRA_POWERGATE_SOR] = "sor",
1428         [TEGRA_POWERGATE_DIS] = "dis",
1429         [TEGRA_POWERGATE_DISB] = "disb",
1430         [TEGRA_POWERGATE_XUSBA] = "xusba",
1431         [TEGRA_POWERGATE_XUSBB] = "xusbb",
1432         [TEGRA_POWERGATE_XUSBC] = "xusbc",
1433         [TEGRA_POWERGATE_VIC] = "vic",
1434         [TEGRA_POWERGATE_IRAM] = "iram",
1435         [TEGRA_POWERGATE_NVDEC] = "nvdec",
1436         [TEGRA_POWERGATE_NVJPG] = "nvjpg",
1437         [TEGRA_POWERGATE_AUD] = "aud",
1438         [TEGRA_POWERGATE_DFD] = "dfd",
1439         [TEGRA_POWERGATE_VE2] = "ve2",
1440 };
1441
1442 static const u8 tegra210_cpu_powergates[] = {
1443         TEGRA_POWERGATE_CPU0,
1444         TEGRA_POWERGATE_CPU1,
1445         TEGRA_POWERGATE_CPU2,
1446         TEGRA_POWERGATE_CPU3,
1447 };
1448
1449 static const struct tegra_pmc_soc tegra210_pmc_soc = {
1450         .num_powergates = ARRAY_SIZE(tegra210_powergates),
1451         .powergates = tegra210_powergates,
1452         .num_cpu_powergates = ARRAY_SIZE(tegra210_cpu_powergates),
1453         .cpu_powergates = tegra210_cpu_powergates,
1454         .has_tsense_reset = true,
1455         .has_gpu_clamps = true,
1456 };
1457
1458 static const struct of_device_id tegra_pmc_match[] = {
1459         { .compatible = "nvidia,tegra210-pmc", .data = &tegra210_pmc_soc },
1460         { .compatible = "nvidia,tegra132-pmc", .data = &tegra124_pmc_soc },
1461         { .compatible = "nvidia,tegra124-pmc", .data = &tegra124_pmc_soc },
1462         { .compatible = "nvidia,tegra114-pmc", .data = &tegra114_pmc_soc },
1463         { .compatible = "nvidia,tegra30-pmc", .data = &tegra30_pmc_soc },
1464         { .compatible = "nvidia,tegra20-pmc", .data = &tegra20_pmc_soc },
1465         { }
1466 };
1467
1468 static struct platform_driver tegra_pmc_driver = {
1469         .driver = {
1470                 .name = "tegra-pmc",
1471                 .suppress_bind_attrs = true,
1472                 .of_match_table = tegra_pmc_match,
1473 #if defined(CONFIG_PM_SLEEP) && defined(CONFIG_ARM)
1474                 .pm = &tegra_pmc_pm_ops,
1475 #endif
1476         },
1477         .probe = tegra_pmc_probe,
1478 };
1479 builtin_platform_driver(tegra_pmc_driver);
1480
1481 /*
1482  * Early initialization to allow access to registers in the very early boot
1483  * process.
1484  */
1485 static int __init tegra_pmc_early_init(void)
1486 {
1487         const struct of_device_id *match;
1488         struct device_node *np;
1489         struct resource regs;
1490         unsigned int i;
1491         bool invert;
1492         u32 value;
1493
1494         np = of_find_matching_node_and_match(NULL, tegra_pmc_match, &match);
1495         if (!np) {
1496                 /*
1497                  * Fall back to legacy initialization for 32-bit ARM only. All
1498                  * 64-bit ARM device tree files for Tegra are required to have
1499                  * a PMC node.
1500                  *
1501                  * This is for backwards-compatibility with old device trees
1502                  * that didn't contain a PMC node. Note that in this case the
1503                  * SoC data can't be matched and therefore powergating is
1504                  * disabled.
1505                  */
1506                 if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
1507                         pr_warn("DT node not found, powergating disabled\n");
1508
1509                         regs.start = 0x7000e400;
1510                         regs.end = 0x7000e7ff;
1511                         regs.flags = IORESOURCE_MEM;
1512
1513                         pr_warn("Using memory region %pR\n", &regs);
1514                 } else {
1515                         /*
1516                          * At this point we're not running on Tegra, so play
1517                          * nice with multi-platform kernels.
1518                          */
1519                         return 0;
1520                 }
1521         } else {
1522                 /*
1523                  * Extract information from the device tree if we've found a
1524                  * matching node.
1525                  */
1526                 if (of_address_to_resource(np, 0, &regs) < 0) {
1527                         pr_err("failed to get PMC registers\n");
1528                         return -ENXIO;
1529                 }
1530
1531                 pmc->soc = match->data;
1532         }
1533
1534         pmc->base = ioremap_nocache(regs.start, resource_size(&regs));
1535         if (!pmc->base) {
1536                 pr_err("failed to map PMC registers\n");
1537                 return -ENXIO;
1538         }
1539
1540         /* Create a bit-map of the available and valid partitions */
1541         for (i = 0; i < pmc->soc->num_powergates; i++)
1542                 if (pmc->soc->powergates[i])
1543                         set_bit(i, pmc->powergates_available);
1544
1545         mutex_init(&pmc->powergates_lock);
1546
1547         /*
1548          * Invert the interrupt polarity if a PMC device tree node exists and
1549          * contains the nvidia,invert-interrupt property.
1550          */
1551         invert = of_property_read_bool(np, "nvidia,invert-interrupt");
1552
1553         value = tegra_pmc_readl(PMC_CNTRL);
1554
1555         if (invert)
1556                 value |= PMC_CNTRL_INTR_POLARITY;
1557         else
1558                 value &= ~PMC_CNTRL_INTR_POLARITY;
1559
1560         tegra_pmc_writel(value, PMC_CNTRL);
1561
1562         return 0;
1563 }
1564 early_initcall(tegra_pmc_early_init);