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