blob: 7ed306e045ea762e44530a6042250930a54730a0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Purna Chandra Mandal825b3212016-01-28 15:30:10 +05302/*
3 * Copyright (C) 2015
4 * Purna Chandra Mandal <purna.mandal@microchip.com>
5 *
Purna Chandra Mandal825b3212016-01-28 15:30:10 +05306 */
7#include <common.h>
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +05308#include <clk.h>
9#include <dm.h>
Simon Glassfc557362022-03-04 08:43:05 -070010#include <event.h>
Simon Glass97589732020-05-10 11:40:02 -060011#include <init.h>
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053014#include <mach/pic32.h>
15#include <mach/ddr.h>
16#include <dt-bindings/clock/microchip,clock.h>
Purna Chandra Mandal825b3212016-01-28 15:30:10 +053017
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053018/* Flash prefetch */
19#define PRECON 0x00
20
21/* Flash ECCCON */
22#define ECC_MASK 0x03
23#define ECC_SHIFT 4
24
25#define CLK_MHZ(x) ((x) / 1000000)
26
27DECLARE_GLOBAL_DATA_PTR;
28
Stephen Warrena9622432016-06-17 09:44:00 -060029static ulong rate(int id)
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053030{
31 int ret;
32 struct udevice *dev;
Stephen Warrena9622432016-06-17 09:44:00 -060033 struct clk clk;
34 ulong rate;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053035
36 ret = uclass_get_device(UCLASS_CLK, 0, &dev);
37 if (ret) {
Stephen Warrena9622432016-06-17 09:44:00 -060038 printf("clk-uclass not found\n");
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053039 return 0;
40 }
41
Stephen Warrena9622432016-06-17 09:44:00 -060042 clk.id = id;
43 ret = clk_request(dev, &clk);
44 if (ret < 0)
45 return ret;
46
47 rate = clk_get_rate(&clk);
48
49 clk_free(&clk);
50
51 return rate;
52}
53
54static ulong clk_get_cpu_rate(void)
55{
56 return rate(PB7CLK);
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053057}
58
59/* initialize prefetch module related to cpu_clk */
Simon Glass1cedca12023-08-21 21:17:01 -060060static int prefetch_init(void)
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053061{
62 struct pic32_reg_atomic *regs;
63 const void __iomem *base;
64 int v, nr_waits;
65 ulong rate;
66
67 /* cpu frequency in MHZ */
68 rate = clk_get_cpu_rate() / 1000000;
69
70 /* get flash ECC type */
71 base = pic32_get_syscfg_base();
72 v = (readl(base + CFGCON) >> ECC_SHIFT) & ECC_MASK;
73
74 if (v < 2) {
75 if (rate < 66)
76 nr_waits = 0;
77 else if (rate < 133)
78 nr_waits = 1;
79 else
80 nr_waits = 2;
81 } else {
82 if (rate <= 83)
83 nr_waits = 0;
84 else if (rate <= 166)
85 nr_waits = 1;
86 else
87 nr_waits = 2;
88 }
89
90 regs = ioremap(PREFETCH_BASE + PRECON, sizeof(*regs));
91 writel(nr_waits, &regs->raw);
92
93 /* Enable prefetch for all */
94 writel(0x30, &regs->set);
95 iounmap(regs);
Simon Glass1cedca12023-08-21 21:17:01 -060096
97 return 0;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053098}
99
Simon Glassb8357c12023-08-21 21:16:56 -0600100/* arch-specific CPU init after DM: flash prefetch */
101EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, prefetch_init);
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530102
103/* Un-gate DDR2 modules (gated by default) */
104static void ddr2_pmd_ungate(void)
105{
106 void __iomem *regs;
107
108 regs = pic32_get_syscfg_base();
109 writel(0, regs + PMD7);
110}
111
112/* initialize the DDR2 Controller and DDR2 PHY */
Simon Glassd35f3382017-04-06 12:47:05 -0600113int dram_init(void)
Purna Chandra Mandal825b3212016-01-28 15:30:10 +0530114{
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530115 ddr2_pmd_ungate();
116 ddr2_phy_init();
117 ddr2_ctrl_init();
Simon Glass39f90ba2017-03-31 08:40:25 -0600118 gd->ram_size = ddr2_calculate_size();
119
120 return 0;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530121}
122
123int misc_init_r(void)
124{
125 set_io_port_base(0);
126 return 0;
127}
128
129#ifdef CONFIG_DISPLAY_BOARDINFO
130const char *get_core_name(void)
131{
132 u32 proc_id;
133 const char *str;
134
135 proc_id = read_c0_prid();
136 switch (proc_id) {
137 case 0x19e28:
138 str = "PIC32MZ[DA]";
139 break;
140 default:
141 str = "UNKNOWN";
142 }
143
144 return str;
145}
146#endif
147#ifdef CONFIG_CMD_CLK
Stephen Warrena9622432016-06-17 09:44:00 -0600148
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530149int soc_clk_dump(void)
150{
Stephen Warrena9622432016-06-17 09:44:00 -0600151 int i;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530152
153 printf("PLL Speed: %lu MHz\n",
Stephen Warrena9622432016-06-17 09:44:00 -0600154 CLK_MHZ(rate(PLLCLK)));
155
156 printf("CPU Speed: %lu MHz\n", CLK_MHZ(rate(PB7CLK)));
157
158 printf("MPLL Speed: %lu MHz\n", CLK_MHZ(rate(MPLL)));
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530159
160 for (i = PB1CLK; i <= PB7CLK; i++)
161 printf("PB%d Clock Speed: %lu MHz\n", i - PB1CLK + 1,
Stephen Warrena9622432016-06-17 09:44:00 -0600162 CLK_MHZ(rate(i)));
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530163
164 for (i = REF1CLK; i <= REF5CLK; i++)
165 printf("REFO%d Clock Speed: %lu MHz\n", i - REF1CLK + 1,
Stephen Warrena9622432016-06-17 09:44:00 -0600166 CLK_MHZ(rate(i)));
Purna Chandra Mandal825b3212016-01-28 15:30:10 +0530167 return 0;
168}
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530169#endif