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 | */ |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 7 | #include <clk.h> |
| 8 | #include <dm.h> |
Simon Glass | fc55736 | 2022-03-04 08:43:05 -0700 | [diff] [blame] | 9 | #include <event.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; |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 33 | |
| 34 | ret = uclass_get_device(UCLASS_CLK, 0, &dev); |
| 35 | if (ret) { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 36 | printf("clk-uclass not found\n"); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 40 | clk.id = id; |
| 41 | ret = clk_request(dev, &clk); |
| 42 | if (ret < 0) |
| 43 | return ret; |
| 44 | |
Sean Anderson | d318eb3 | 2023-12-16 14:38:42 -0500 | [diff] [blame] | 45 | return clk_get_rate(&clk); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static ulong clk_get_cpu_rate(void) |
| 49 | { |
| 50 | return rate(PB7CLK); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /* initialize prefetch module related to cpu_clk */ |
Simon Glass | 1cedca1 | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 54 | static int prefetch_init(void) |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 55 | { |
| 56 | struct pic32_reg_atomic *regs; |
| 57 | const void __iomem *base; |
| 58 | int v, nr_waits; |
| 59 | ulong rate; |
| 60 | |
| 61 | /* cpu frequency in MHZ */ |
| 62 | rate = clk_get_cpu_rate() / 1000000; |
| 63 | |
| 64 | /* get flash ECC type */ |
| 65 | base = pic32_get_syscfg_base(); |
| 66 | v = (readl(base + CFGCON) >> ECC_SHIFT) & ECC_MASK; |
| 67 | |
| 68 | if (v < 2) { |
| 69 | if (rate < 66) |
| 70 | nr_waits = 0; |
| 71 | else if (rate < 133) |
| 72 | nr_waits = 1; |
| 73 | else |
| 74 | nr_waits = 2; |
| 75 | } else { |
| 76 | if (rate <= 83) |
| 77 | nr_waits = 0; |
| 78 | else if (rate <= 166) |
| 79 | nr_waits = 1; |
| 80 | else |
| 81 | nr_waits = 2; |
| 82 | } |
| 83 | |
| 84 | regs = ioremap(PREFETCH_BASE + PRECON, sizeof(*regs)); |
| 85 | writel(nr_waits, ®s->raw); |
| 86 | |
| 87 | /* Enable prefetch for all */ |
| 88 | writel(0x30, ®s->set); |
| 89 | iounmap(regs); |
Simon Glass | 1cedca1 | 2023-08-21 21:17:01 -0600 | [diff] [blame] | 90 | |
| 91 | return 0; |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 92 | } |
| 93 | |
Simon Glass | b8357c1 | 2023-08-21 21:16:56 -0600 | [diff] [blame] | 94 | /* arch-specific CPU init after DM: flash prefetch */ |
| 95 | EVENT_SPY_SIMPLE(EVT_DM_POST_INIT_F, prefetch_init); |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 96 | |
| 97 | /* Un-gate DDR2 modules (gated by default) */ |
| 98 | static void ddr2_pmd_ungate(void) |
| 99 | { |
| 100 | void __iomem *regs; |
| 101 | |
| 102 | regs = pic32_get_syscfg_base(); |
| 103 | writel(0, regs + PMD7); |
| 104 | } |
| 105 | |
| 106 | /* initialize the DDR2 Controller and DDR2 PHY */ |
Simon Glass | d35f338 | 2017-04-06 12:47:05 -0600 | [diff] [blame] | 107 | int dram_init(void) |
Purna Chandra Mandal | 825b321 | 2016-01-28 15:30:10 +0530 | [diff] [blame] | 108 | { |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 109 | ddr2_pmd_ungate(); |
| 110 | ddr2_phy_init(); |
| 111 | ddr2_ctrl_init(); |
Simon Glass | 39f90ba | 2017-03-31 08:40:25 -0600 | [diff] [blame] | 112 | gd->ram_size = ddr2_calculate_size(); |
| 113 | |
| 114 | return 0; |
Purna Chandra Mandal | 5c2dcd2 | 2016-01-28 15:30:16 +0530 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | int misc_init_r(void) |
| 118 | { |
| 119 | set_io_port_base(0); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | #ifdef CONFIG_DISPLAY_BOARDINFO |
| 124 | const char *get_core_name(void) |
| 125 | { |
| 126 | u32 proc_id; |
| 127 | const char *str; |
| 128 | |
| 129 | proc_id = read_c0_prid(); |
| 130 | switch (proc_id) { |
| 131 | case 0x19e28: |
| 132 | str = "PIC32MZ[DA]"; |
| 133 | break; |
| 134 | default: |
| 135 | str = "UNKNOWN"; |
| 136 | } |
| 137 | |
| 138 | return str; |
| 139 | } |
| 140 | #endif |