Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2018 Synopsys, Inc. All rights reserved. |
| 4 | */ |
| 5 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 6 | #include <command.h> |
Simon Glass | 9a3b4ce | 2019-12-28 10:45:01 -0700 | [diff] [blame] | 7 | #include <cpu_func.h> |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 8 | #include <dwmmc.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 9 | #include <init.h> |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 12 | #include <linux/bitops.h> |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 13 | |
Alexey Brodkin | 4e86c7e | 2018-11-27 09:46:59 +0300 | [diff] [blame] | 14 | #include <asm/arcregs.h> |
| 15 | |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
Alexey Brodkin | 4e86c7e | 2018-11-27 09:46:59 +0300 | [diff] [blame] | 18 | #define ARC_PERIPHERAL_BASE 0xF0000000 |
| 19 | |
| 20 | #define CGU_ARC_FMEAS_ARC (void *)(ARC_PERIPHERAL_BASE + 0x84) |
| 21 | #define CGU_ARC_FMEAS_ARC_START BIT(31) |
| 22 | #define CGU_ARC_FMEAS_ARC_DONE BIT(30) |
| 23 | #define CGU_ARC_FMEAS_ARC_CNT_MASK GENMASK(14, 0) |
| 24 | #define CGU_ARC_FMEAS_ARC_RCNT_OFFSET 0 |
| 25 | #define CGU_ARC_FMEAS_ARC_FCNT_OFFSET 15 |
| 26 | |
| 27 | #define SDIO_BASE (void *)(ARC_PERIPHERAL_BASE + 0x10000) |
| 28 | |
| 29 | int mach_cpu_init(void) |
| 30 | { |
| 31 | int rcnt, fcnt; |
| 32 | u32 data; |
| 33 | |
| 34 | /* Start frequency measurement */ |
| 35 | writel(CGU_ARC_FMEAS_ARC_START, CGU_ARC_FMEAS_ARC); |
| 36 | |
| 37 | /* Poll DONE bit */ |
| 38 | do { |
| 39 | data = readl(CGU_ARC_FMEAS_ARC); |
| 40 | } while (!(data & CGU_ARC_FMEAS_ARC_DONE)); |
| 41 | |
| 42 | /* Amount of reference 100 MHz clocks */ |
| 43 | rcnt = ((data >> CGU_ARC_FMEAS_ARC_RCNT_OFFSET) & |
| 44 | CGU_ARC_FMEAS_ARC_CNT_MASK); |
| 45 | |
| 46 | /* Amount of CPU clocks */ |
| 47 | fcnt = ((data >> CGU_ARC_FMEAS_ARC_FCNT_OFFSET) & |
| 48 | CGU_ARC_FMEAS_ARC_CNT_MASK); |
| 49 | |
| 50 | gd->cpu_clk = ((100 * fcnt) / rcnt) * 1000000; |
| 51 | |
| 52 | return 0; |
| 53 | } |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 54 | |
Alexey Brodkin | 9ddaf1d | 2019-07-18 15:51:25 +0300 | [diff] [blame] | 55 | int board_early_init_r(void) |
| 56 | { |
| 57 | #define EMSDP_PSRAM_BASE 0xf2001000 |
| 58 | #define PSRAM_FLASH_CONFIG_REG_0 (void *)(EMSDP_PSRAM_BASE + 0x10) |
| 59 | #define PSRAM_FLASH_CONFIG_REG_1 (void *)(EMSDP_PSRAM_BASE + 0x14) |
| 60 | #define CRE_ENABLE BIT(31) |
| 61 | #define CRE_DRIVE_CMD BIT(6) |
| 62 | |
| 63 | #define PSRAM_RCR_DPD BIT(1) |
| 64 | #define PSRAM_RCR_PAGE_MODE BIT(7) |
| 65 | |
| 66 | /* |
| 67 | * PSRAM_FLASH_CONFIG_REG_x[30:15] to the address lines[16:1] of flash, |
| 68 | * thus "<< 1". |
| 69 | */ |
| 70 | #define PSRAM_RCR_SETUP ((PSRAM_RCR_DPD | PSRAM_RCR_PAGE_MODE) << 1) |
| 71 | |
| 72 | // Switch PSRAM controller to command mode |
| 73 | writel(CRE_ENABLE | CRE_DRIVE_CMD, PSRAM_FLASH_CONFIG_REG_0); |
| 74 | // Program Refresh Configuration Register (RCR) for BANK0 |
| 75 | writew(0, (void *)(0x10000000 + PSRAM_RCR_SETUP)); |
| 76 | // Switch PSRAM controller back to memory mode |
| 77 | writel(0, PSRAM_FLASH_CONFIG_REG_0); |
| 78 | |
| 79 | |
| 80 | // Switch PSRAM controller to command mode |
| 81 | writel(CRE_ENABLE | CRE_DRIVE_CMD, PSRAM_FLASH_CONFIG_REG_1); |
| 82 | // Program Refresh Configuration Register (RCR) for BANK1 |
| 83 | writew(0, (void *)(0x10800000 + PSRAM_RCR_SETUP)); |
| 84 | // Switch PSRAM controller back to memory mode |
| 85 | writel(0, PSRAM_FLASH_CONFIG_REG_1); |
| 86 | |
| 87 | printf("PSRAM initialized.\n"); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 92 | #define CREG_BASE 0xF0001000 |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 93 | #define CREG_BOOT (void *)(CREG_BASE + 0x0FF0) |
| 94 | #define CREG_IP_SW_RESET (void *)(CREG_BASE + 0x0FF0) |
Alexey Brodkin | 6ef705b | 2018-11-27 09:47:01 +0300 | [diff] [blame] | 95 | #define CREG_IP_VERSION (void *)(CREG_BASE + 0x0FF8) |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 96 | |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 97 | /* Bits in CREG_BOOT register */ |
| 98 | #define CREG_BOOT_WP_BIT BIT(8) |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 99 | |
Harald Seiler | 35b65dd | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 100 | void reset_cpu(void) |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 101 | { |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 102 | writel(1, CREG_IP_SW_RESET); |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 103 | while (1) |
| 104 | ; /* loop forever till reset */ |
| 105 | } |
| 106 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 107 | static int do_emsdp_rom(struct cmd_tbl *cmdtp, int flag, int argc, |
| 108 | char *const argv[]) |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 109 | { |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 110 | u32 creg_boot = readl(CREG_BOOT); |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 111 | |
| 112 | if (!strcmp(argv[1], "unlock")) |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 113 | creg_boot &= ~CREG_BOOT_WP_BIT; |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 114 | else if (!strcmp(argv[1], "lock")) |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 115 | creg_boot |= CREG_BOOT_WP_BIT; |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 116 | else |
| 117 | return CMD_RET_USAGE; |
| 118 | |
Alexey Brodkin | fb9a46a | 2018-11-27 09:47:00 +0300 | [diff] [blame] | 119 | writel(creg_boot, CREG_BOOT); |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 120 | |
| 121 | return CMD_RET_SUCCESS; |
| 122 | } |
| 123 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 124 | struct cmd_tbl cmd_emsdp[] = { |
Alexey Brodkin | adc9b09 | 2018-10-18 09:54:58 +0300 | [diff] [blame] | 125 | U_BOOT_CMD_MKENT(rom, 2, 0, do_emsdp_rom, "", ""), |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 126 | }; |
| 127 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 128 | static int do_emsdp(struct cmd_tbl *cmdtp, int flag, int argc, |
| 129 | char *const argv[]) |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 130 | { |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 131 | struct cmd_tbl *c; |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 132 | |
Alexey Brodkin | adc9b09 | 2018-10-18 09:54:58 +0300 | [diff] [blame] | 133 | c = find_cmd_tbl(argv[1], cmd_emsdp, ARRAY_SIZE(cmd_emsdp)); |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 134 | |
Alexey Brodkin | adc9b09 | 2018-10-18 09:54:58 +0300 | [diff] [blame] | 135 | /* Strip off leading 'emsdp' command */ |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 136 | argc--; |
| 137 | argv++; |
| 138 | |
| 139 | if (c == NULL || argc > c->maxargs) |
| 140 | return CMD_RET_USAGE; |
| 141 | |
| 142 | return c->cmd(cmdtp, flag, argc, argv); |
| 143 | } |
| 144 | |
| 145 | U_BOOT_CMD( |
Alexey Brodkin | adc9b09 | 2018-10-18 09:54:58 +0300 | [diff] [blame] | 146 | emsdp, CONFIG_SYS_MAXARGS, 0, do_emsdp, |
| 147 | "Synopsys EMSDP specific commands", |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 148 | "rom unlock - Unlock non-volatile memory for writing\n" |
Alexey Brodkin | adc9b09 | 2018-10-18 09:54:58 +0300 | [diff] [blame] | 149 | "emsdp rom lock - Lock non-volatile memory to prevent writing\n" |
Alexey Brodkin | 2c3f926 | 2018-05-28 15:27:43 +0300 | [diff] [blame] | 150 | ); |
Alexey Brodkin | 6ef705b | 2018-11-27 09:47:01 +0300 | [diff] [blame] | 151 | |
| 152 | int checkboard(void) |
| 153 | { |
| 154 | int version = readl(CREG_IP_VERSION); |
| 155 | |
| 156 | printf("Board: ARC EM Software Development Platform v%d.%d\n", |
| 157 | (version >> 16) & 0xff, version & 0xff); |
| 158 | return 0; |
| 159 | }; |