Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries |
| 4 | * |
| 5 | * Author: Claudiu Beznea <claudiu.beznea@microchip.com> |
| 6 | */ |
| 7 | |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 8 | #include <cpu.h> |
| 9 | #include <dm.h> |
| 10 | #include <div64.h> |
| 11 | #include <linux/clk-provider.h> |
| 12 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 13 | struct at91_cpu_plat { |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 14 | const char *name; |
| 15 | ulong cpufreq_mhz; |
| 16 | ulong mckfreq_mhz; |
| 17 | ulong xtalfreq_mhz; |
| 18 | }; |
| 19 | |
| 20 | extern char *get_cpu_name(void); |
| 21 | |
| 22 | const char *at91_cpu_get_name(void) |
| 23 | { |
| 24 | return get_cpu_name(); |
| 25 | } |
| 26 | |
| 27 | int at91_cpu_get_desc(const struct udevice *dev, char *buf, int size) |
| 28 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 29 | struct at91_cpu_plat *plat = dev_get_plat(dev); |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 30 | |
| 31 | snprintf(buf, size, "%s\n" |
| 32 | "Crystal frequency: %8lu MHz\n" |
| 33 | "CPU clock : %8lu MHz\n" |
| 34 | "Master clock : %8lu MHz\n", |
| 35 | plat->name, plat->xtalfreq_mhz, plat->cpufreq_mhz, |
| 36 | plat->mckfreq_mhz); |
| 37 | |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static int at91_cpu_get_info(const struct udevice *dev, struct cpu_info *info) |
| 42 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 43 | struct at91_cpu_plat *plat = dev_get_plat(dev); |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 44 | |
| 45 | info->cpu_freq = plat->cpufreq_mhz * 1000000; |
| 46 | info->features = BIT(CPU_FEAT_L1_CACHE); |
| 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static int at91_cpu_get_count(const struct udevice *dev) |
| 52 | { |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | static int at91_cpu_get_vendor(const struct udevice *dev, char *buf, int size) |
| 57 | { |
| 58 | snprintf(buf, size, "Microchip Technology Inc."); |
| 59 | |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | static const struct cpu_ops at91_cpu_ops = { |
| 64 | .get_desc = at91_cpu_get_desc, |
| 65 | .get_info = at91_cpu_get_info, |
| 66 | .get_count = at91_cpu_get_count, |
| 67 | .get_vendor = at91_cpu_get_vendor, |
| 68 | }; |
| 69 | |
| 70 | static const struct udevice_id at91_cpu_ids[] = { |
| 71 | { .compatible = "arm,cortex-a7" }, |
Claudiu Beznea | 3f0a8a7 | 2021-07-16 08:43:49 +0300 | [diff] [blame] | 72 | { .compatible = "arm,arm926ej-s" }, |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 73 | { /* Sentinel. */ } |
| 74 | }; |
| 75 | |
| 76 | static int at91_cpu_probe(struct udevice *dev) |
| 77 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 78 | struct at91_cpu_plat *plat = dev_get_plat(dev); |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 79 | struct clk clk; |
| 80 | ulong rate; |
| 81 | int ret; |
| 82 | |
| 83 | ret = clk_get_by_index(dev, 0, &clk); |
| 84 | if (ret) |
| 85 | return ret; |
| 86 | |
| 87 | rate = clk_get_rate(&clk); |
| 88 | if (!rate) |
| 89 | return -ENOTSUPP; |
| 90 | plat->cpufreq_mhz = DIV_ROUND_CLOSEST_ULL(rate, 1000000); |
| 91 | |
| 92 | ret = clk_get_by_index(dev, 1, &clk); |
| 93 | if (ret) |
| 94 | return ret; |
| 95 | |
| 96 | rate = clk_get_rate(&clk); |
| 97 | if (!rate) |
| 98 | return -ENOTSUPP; |
| 99 | plat->mckfreq_mhz = DIV_ROUND_CLOSEST_ULL(rate, 1000000); |
| 100 | |
| 101 | ret = clk_get_by_index(dev, 2, &clk); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | rate = clk_get_rate(&clk); |
| 106 | if (!rate) |
| 107 | return -ENOTSUPP; |
| 108 | plat->xtalfreq_mhz = DIV_ROUND_CLOSEST_ULL(rate, 1000000); |
| 109 | |
| 110 | plat->name = get_cpu_name(); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | U_BOOT_DRIVER(cpu_at91_drv) = { |
| 116 | .name = "at91-cpu", |
| 117 | .id = UCLASS_CPU, |
| 118 | .of_match = at91_cpu_ids, |
| 119 | .ops = &at91_cpu_ops, |
| 120 | .probe = at91_cpu_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 121 | .plat_auto = sizeof(struct at91_cpu_plat), |
Claudiu Beznea | 929f5cc | 2020-10-01 13:27:25 +0300 | [diff] [blame] | 122 | .flags = DM_FLAG_PRE_RELOC, |
| 123 | }; |