cae95785161233cd97f6ca03472cc436d25f9ff4
[oweals/u-boot.git] / arch / x86 / cpu / ivybridge / early_me.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * From Coreboot src/southbridge/intel/bd82x6x/early_me.c
4  *
5  * Copyright (C) 2011 The Chromium OS Authors. All rights reserved.
6  */
7
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <log.h>
12 #include <sysreset.h>
13 #include <asm/pci.h>
14 #include <asm/cpu.h>
15 #include <asm/processor.h>
16 #include <asm/arch/me.h>
17 #include <asm/arch/pch.h>
18 #include <asm/io.h>
19
20 static const char *const me_ack_values[] = {
21         [ME_HFS_ACK_NO_DID]     = "No DID Ack received",
22         [ME_HFS_ACK_RESET]      = "Non-power cycle reset",
23         [ME_HFS_ACK_PWR_CYCLE]  = "Power cycle reset",
24         [ME_HFS_ACK_S3]         = "Go to S3",
25         [ME_HFS_ACK_S4]         = "Go to S4",
26         [ME_HFS_ACK_S5]         = "Go to S5",
27         [ME_HFS_ACK_GBL_RESET]  = "Global Reset",
28         [ME_HFS_ACK_CONTINUE]   = "Continue to boot"
29 };
30
31 int intel_early_me_init(struct udevice *me_dev)
32 {
33         int count;
34         struct me_uma uma;
35         struct me_hfs hfs;
36
37         debug("Intel ME early init\n");
38
39         /* Wait for ME UMA SIZE VALID bit to be set */
40         for (count = ME_RETRY; count > 0; --count) {
41                 pci_read_dword_ptr(me_dev, &uma, PCI_ME_UMA);
42                 if (uma.valid)
43                         break;
44                 udelay(ME_DELAY);
45         }
46         if (!count) {
47                 printf("ERROR: ME is not ready!\n");
48                 return -EBUSY;
49         }
50
51         /* Check for valid firmware */
52         pci_read_dword_ptr(me_dev, &hfs, PCI_ME_HFS);
53         if (hfs.fpt_bad) {
54                 printf("WARNING: ME has bad firmware\n");
55                 return -EBADF;
56         }
57
58         debug("Intel ME firmware is ready\n");
59
60         return 0;
61 }
62
63 int intel_early_me_uma_size(struct udevice *me_dev)
64 {
65         struct me_uma uma;
66
67         pci_read_dword_ptr(me_dev, &uma, PCI_ME_UMA);
68         if (uma.valid) {
69                 debug("ME: Requested %uMB UMA\n", uma.size);
70                 return uma.size;
71         }
72
73         debug("ME: Invalid UMA size\n");
74         return -EINVAL;
75 }
76
77 static inline void set_global_reset(struct udevice *dev, int enable)
78 {
79         u32 etr3;
80
81         dm_pci_read_config32(dev, ETR3, &etr3);
82
83         /* Clear CF9 Without Resume Well Reset Enable */
84         etr3 &= ~ETR3_CWORWRE;
85
86         /* CF9GR indicates a Global Reset */
87         if (enable)
88                 etr3 |= ETR3_CF9GR;
89         else
90                 etr3 &= ~ETR3_CF9GR;
91
92         dm_pci_write_config32(dev, ETR3, etr3);
93 }
94
95 int intel_early_me_init_done(struct udevice *dev, struct udevice *me_dev,
96                              uint status)
97 {
98         int count;
99         u32 mebase_l, mebase_h;
100         struct me_hfs hfs;
101         struct me_did did = {
102                 .init_done = ME_INIT_DONE,
103                 .status = status
104         };
105
106         /* MEBASE from MESEG_BASE[35:20] */
107         dm_pci_read_config32(PCH_DEV, PCI_CPU_MEBASE_L, &mebase_l);
108         dm_pci_read_config32(PCH_DEV, PCI_CPU_MEBASE_H, &mebase_h);
109         mebase_h &= 0xf;
110         did.uma_base = (mebase_l >> 20) | (mebase_h << 12);
111
112         /* Send message to ME */
113         debug("ME: Sending Init Done with status: %d, UMA base: 0x%04x\n",
114               status, did.uma_base);
115
116         pci_write_dword_ptr(me_dev, &did, PCI_ME_H_GS);
117
118         /* Must wait for ME acknowledgement */
119         for (count = ME_RETRY; count > 0; --count) {
120                 pci_read_dword_ptr(me_dev, &hfs, PCI_ME_HFS);
121                 if (hfs.bios_msg_ack)
122                         break;
123                 udelay(ME_DELAY);
124         }
125         if (!count) {
126                 printf("ERROR: ME failed to respond\n");
127                 return -ETIMEDOUT;
128         }
129
130         /* Return the requested BIOS action */
131         debug("ME: Requested BIOS Action: %s\n", me_ack_values[hfs.ack_data]);
132
133         /* Check status after acknowledgement */
134         intel_me_status(me_dev);
135
136         switch (hfs.ack_data) {
137         case ME_HFS_ACK_CONTINUE:
138                 /* Continue to boot */
139                 return 0;
140         case ME_HFS_ACK_RESET:
141                 /* Non-power cycle reset */
142                 set_global_reset(dev, 0);
143                 sysreset_walk_halt(SYSRESET_COLD);
144                 break;
145         case ME_HFS_ACK_PWR_CYCLE:
146                 /* Power cycle reset */
147                 set_global_reset(dev, 0);
148                 sysreset_walk_halt(SYSRESET_COLD);
149                 break;
150         case ME_HFS_ACK_GBL_RESET:
151                 /* Global reset */
152                 set_global_reset(dev, 1);
153                 sysreset_walk_halt(SYSRESET_COLD);
154                 break;
155         case ME_HFS_ACK_S3:
156         case ME_HFS_ACK_S4:
157         case ME_HFS_ACK_S5:
158                 break;
159         }
160
161         return -EINVAL;
162 }
163
164 static const struct udevice_id ivybridge_syscon_ids[] = {
165         { .compatible = "intel,me", .data = X86_SYSCON_ME },
166         { }
167 };
168
169 U_BOOT_DRIVER(syscon_intel_me) = {
170         .name = "intel_me_syscon",
171         .id = UCLASS_SYSCON,
172         .of_match = ivybridge_syscon_ids,
173 };