Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Purna Chandra Mandal | 825b321 | 2016-01-28 15:30:10 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 |
| 4 | * Purna Chandra Mandal <purna.mandal@microchip.com> |
| 5 | * |
Purna Chandra Mandal | 825b321 | 2016-01-28 15:30:10 +0530 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 8 | #include <clk.h> |
| 9 | #include <dm.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 10 | #include <init.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 12 | #include <asm/global_data.h> |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 13 | #include <mach/pic32.h> |
| 14 | #include <mach/ddr.h> |
| 15 | #include <dt-bindings/clock/microchip,clock.h> |
Purna Chandra Mandal | 825b321 | 2016-01-28 15:30:10 +0530 | [diff] [blame] | 16 | |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 17 | /* 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 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 28 | static ulong rate(int id) |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 29 | { |
| 30 | int ret; |
| 31 | struct udevice *dev; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 32 | struct clk clk; |
| 33 | ulong rate; |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 34 | |
| 35 | ret = uclass_get_device(UCLASS_CLK, 0, &dev); |
| 36 | if (ret) { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 37 | printf("clk-uclass not found\n"); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 38 | return 0; |
| 39 | } |
| 40 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 41 | 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 | |
| 53 | static ulong clk_get_cpu_rate(void) |
| 54 | { |
| 55 | return rate(PB7CLK); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* initialize prefetch module related to cpu_clk */ |
| 59 | static void prefetch_init(void) |
| 60 | { |
| 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, ®s->raw); |
| 91 | |
| 92 | /* Enable prefetch for all */ |
| 93 | writel(0x30, ®s->set); |
| 94 | iounmap(regs); |
| 95 | } |
| 96 | |
| 97 | /* arch specific CPU init after DM */ |
| 98 | int arch_cpu_init_dm(void) |
| 99 | { |
| 100 | /* flash prefetch */ |
| 101 | prefetch_init(); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /* Un-gate DDR2 modules (gated by default) */ |
| 106 | static void ddr2_pmd_ungate(void) |
| 107 | { |
| 108 | void __iomem *regs; |
| 109 | |
| 110 | regs = pic32_get_syscfg_base(); |
| 111 | writel(0, regs + PMD7); |
| 112 | } |
| 113 | |
| 114 | /* initialize the DDR2 Controller and DDR2 PHY */ |
Simon Glass | d35f338 | 2017-04-06 12:47:05 -0600 | [diff] [blame] | 115 | int dram_init(void) |
Purna Chandra Mandal | 825b321 | 2016-01-28 15:30:10 +0530 | [diff] [blame] | 116 | { |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 117 | ddr2_pmd_ungate(); |
| 118 | ddr2_phy_init(); |
| 119 | ddr2_ctrl_init(); |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 120 | gd->ram_size = ddr2_calculate_size(); |
| 121 | |
| 122 | return 0; |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | int misc_init_r(void) |
| 126 | { |
| 127 | set_io_port_base(0); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | #ifdef CONFIG_DISPLAY_BOARDINFO |
| 132 | const char *get_core_name(void) |
| 133 | { |
| 134 | u32 proc_id; |
| 135 | const char *str; |
| 136 | |
| 137 | proc_id = read_c0_prid(); |
| 138 | switch (proc_id) { |
| 139 | case 0x19e28: |
| 140 | str = "PIC32MZ[DA]"; |
| 141 | break; |
| 142 | default: |
| 143 | str = "UNKNOWN"; |
| 144 | } |
| 145 | |
| 146 | return str; |
| 147 | } |
| 148 | #endif |
| 149 | #ifdef CONFIG_CMD_CLK |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 150 | |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 151 | int soc_clk_dump(void) |
| 152 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 153 | int i; |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 154 | |
| 155 | printf("PLL Speed: %lu MHz\n", |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 156 | CLK_MHZ(rate(PLLCLK))); |
| 157 | |
| 158 | printf("CPU Speed: %lu MHz\n", CLK_MHZ(rate(PB7CLK))); |
| 159 | |
| 160 | printf("MPLL Speed: %lu MHz\n", CLK_MHZ(rate(MPLL))); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 161 | |
| 162 | for (i = PB1CLK; i <= PB7CLK; i++) |
| 163 | printf("PB%d Clock Speed: %lu MHz\n", i - PB1CLK + 1, |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 164 | CLK_MHZ(rate(i))); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 165 | |
| 166 | for (i = REF1CLK; i <= REF5CLK; i++) |
| 167 | printf("REFO%d Clock Speed: %lu MHz\n", i - REF1CLK + 1, |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 168 | CLK_MHZ(rate(i))); |
Purna Chandra Mandal | 825b321 | 2016-01-28 15:30:10 +0530 | [diff] [blame] | 169 | return 0; |
| 170 | } |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 171 | #endif |