blob: 3181a946a27ef4cd42b8b4ae003cfb44e3338a89 [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 */
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +05307#include <clk.h>
8#include <dm.h>
Simon Glassfc557362022-03-04 08:43:05 -07009#include <event.h>
Simon Glass97589732020-05-10 11:40:02 -060010#include <init.h>
Simon Glass9bc15642020-02-03 07:36:16 -070011#include <malloc.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053013#include <mach/pic32.h>
14#include <mach/ddr.h>
15#include <dt-bindings/clock/microchip,clock.h>
Purna Chandra Mandal825b3212016-01-28 15:30:10 +053016
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053017/* Flash prefetch */
18#define PRECON 0x00
19
20/* Flash ECCCON */
21#define ECC_MASK 0x03
22#define ECC_SHIFT 4
23
24#define CLK_MHZ(x) ((x) / 1000000)
25
26DECLARE_GLOBAL_DATA_PTR;
27
Stephen Warrena9622432016-06-17 09:44:00 -060028static ulong rate(int id)
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053029{
30 int ret;
31 struct udevice *dev;
Stephen Warrena9622432016-06-17 09:44:00 -060032 struct clk clk;
33 ulong rate;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053034
35 ret = uclass_get_device(UCLASS_CLK, 0, &dev);
36 if (ret) {
Stephen Warrena9622432016-06-17 09:44:00 -060037 printf("clk-uclass not found\n");
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053038 return 0;
39 }
40
Stephen Warrena9622432016-06-17 09:44:00 -060041 clk.id = id;
42 ret = clk_request(dev, &clk);
43 if (ret < 0)
44 return ret;
45
46 rate = clk_get_rate(&clk);
47
48 clk_free(&clk);
49
50 return rate;
51}
52
53static ulong clk_get_cpu_rate(void)
54{
55 return rate(PB7CLK);
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053056}
57
58/* initialize prefetch module related to cpu_clk */
Simon Glass1cedca12023-08-21 21:17:01 -060059static int prefetch_init(void)
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053060{
61 struct pic32_reg_atomic *regs;
62 const void __iomem *base;
63 int v, nr_waits;
64 ulong rate;
65
66 /* cpu frequency in MHZ */
67 rate = clk_get_cpu_rate() / 1000000;
68
69 /* get flash ECC type */
70 base = pic32_get_syscfg_base();
71 v = (readl(base + CFGCON) >> ECC_SHIFT) & ECC_MASK;
72
73 if (v < 2) {
74 if (rate < 66)
75 nr_waits = 0;
76 else if (rate < 133)
77 nr_waits = 1;
78 else
79 nr_waits = 2;
80 } else {
81 if (rate <= 83)
82 nr_waits = 0;
83 else if (rate <= 166)
84 nr_waits = 1;
85 else
86 nr_waits = 2;
87 }
88
89 regs = ioremap(PREFETCH_BASE + PRECON, sizeof(*regs));
90 writel(nr_waits, &regs->raw);
91
92 /* Enable prefetch for all */
93 writel(0x30, &regs->set);
94 iounmap(regs);
Simon Glass1cedca12023-08-21 21:17:01 -060095
96 return 0;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +053097}
98
Simon Glassb8357c12023-08-21 21:16:56 -060099/* arch-specific CPU init after DM: flash prefetch */
100EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, prefetch_init);
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530101
102/* Un-gate DDR2 modules (gated by default) */
103static void ddr2_pmd_ungate(void)
104{
105 void __iomem *regs;
106
107 regs = pic32_get_syscfg_base();
108 writel(0, regs + PMD7);
109}
110
111/* initialize the DDR2 Controller and DDR2 PHY */
Simon Glassd35f3382017-04-06 12:47:05 -0600112int dram_init(void)
Purna Chandra Mandal825b3212016-01-28 15:30:10 +0530113{
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530114 ddr2_pmd_ungate();
115 ddr2_phy_init();
116 ddr2_ctrl_init();
Simon Glass39f90ba2017-03-31 08:40:25 -0600117 gd->ram_size = ddr2_calculate_size();
118
119 return 0;
Purna Chandra Mandal5c2dcd22016-01-28 15:30:16 +0530120}
121
122int misc_init_r(void)
123{
124 set_io_port_base(0);
125 return 0;
126}
127
128#ifdef CONFIG_DISPLAY_BOARDINFO
129const char *get_core_name(void)
130{
131 u32 proc_id;
132 const char *str;
133
134 proc_id = read_c0_prid();
135 switch (proc_id) {
136 case 0x19e28:
137 str = "PIC32MZ[DA]";
138 break;
139 default:
140 str = "UNKNOWN";
141 }
142
143 return str;
144}
145#endif