d276cbe13bd9ba517370ba289da4cbb73ff841ad
[oweals/u-boot.git] / arch / x86 / cpu / ivybridge / bd82x6x.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014 Google, Inc
4  */
5 #include <common.h>
6 #include <dm.h>
7 #include <errno.h>
8 #include <fdtdec.h>
9 #include <log.h>
10 #include <malloc.h>
11 #include <pch.h>
12 #include <asm/cpu.h>
13 #include <asm/intel_regs.h>
14 #include <asm/io.h>
15 #include <asm/lapic.h>
16 #include <asm/lpc_common.h>
17 #include <asm/pci.h>
18 #include <asm/arch/model_206ax.h>
19 #include <asm/arch/pch.h>
20 #include <asm/arch/sandybridge.h>
21
22 DECLARE_GLOBAL_DATA_PTR;
23
24 #define GPIO_BASE               0x48
25 #define BIOS_CTRL               0xdc
26
27 #define RCBA_AUDIO_CONFIG       0x2030
28 #define RCBA_AUDIO_CONFIG_HDA   BIT(31)
29 #define RCBA_AUDIO_CONFIG_MASK  0xfe
30
31 #ifndef CONFIG_HAVE_FSP
32 static int pch_revision_id = -1;
33 static int pch_type = -1;
34
35 /**
36  * pch_silicon_revision() - Read silicon revision ID from the PCH
37  *
38  * @dev:        PCH device
39  * @return silicon revision ID
40  */
41 static int pch_silicon_revision(struct udevice *dev)
42 {
43         u8 val;
44
45         if (pch_revision_id < 0) {
46                 dm_pci_read_config8(dev, PCI_REVISION_ID, &val);
47                 pch_revision_id = val;
48         }
49
50         return pch_revision_id;
51 }
52
53 int pch_silicon_type(struct udevice *dev)
54 {
55         u8 val;
56
57         if (pch_type < 0) {
58                 dm_pci_read_config8(dev, PCI_DEVICE_ID + 1, &val);
59                 pch_type = val;
60         }
61
62         return pch_type;
63 }
64
65 /**
66  * pch_silicon_supported() - Check if a certain revision is supported
67  *
68  * @dev:        PCH device
69  * @type:       PCH type
70  * @rev:        Minimum required resion
71  * @return 0 if not supported, 1 if supported
72  */
73 static int pch_silicon_supported(struct udevice *dev, int type, int rev)
74 {
75         int cur_type = pch_silicon_type(dev);
76         int cur_rev = pch_silicon_revision(dev);
77
78         switch (type) {
79         case PCH_TYPE_CPT:
80                 /* CougarPoint minimum revision */
81                 if (cur_type == PCH_TYPE_CPT && cur_rev >= rev)
82                         return 1;
83                 /* PantherPoint any revision */
84                 if (cur_type == PCH_TYPE_PPT)
85                         return 1;
86                 break;
87
88         case PCH_TYPE_PPT:
89                 /* PantherPoint minimum revision */
90                 if (cur_type == PCH_TYPE_PPT && cur_rev >= rev)
91                         return 1;
92                 break;
93         }
94
95         return 0;
96 }
97
98 #define IOBP_RETRY 1000
99 static inline int iobp_poll(void)
100 {
101         unsigned try = IOBP_RETRY;
102         u32 data;
103
104         while (try--) {
105                 data = readl(RCB_REG(IOBPS));
106                 if ((data & 1) == 0)
107                         return 1;
108                 udelay(10);
109         }
110
111         printf("IOBP timeout\n");
112         return 0;
113 }
114
115 void pch_iobp_update(struct udevice *dev, u32 address, u32 andvalue,
116                      u32 orvalue)
117 {
118         u32 data;
119
120         /* Set the address */
121         writel(address, RCB_REG(IOBPIRI));
122
123         /* READ OPCODE */
124         if (pch_silicon_supported(dev, PCH_TYPE_CPT, PCH_STEP_B0))
125                 writel(IOBPS_RW_BX, RCB_REG(IOBPS));
126         else
127                 writel(IOBPS_READ_AX, RCB_REG(IOBPS));
128         if (!iobp_poll())
129                 return;
130
131         /* Read IOBP data */
132         data = readl(RCB_REG(IOBPD));
133         if (!iobp_poll())
134                 return;
135
136         /* Check for successful transaction */
137         if ((readl(RCB_REG(IOBPS)) & 0x6) != 0) {
138                 printf("IOBP read 0x%08x failed\n", address);
139                 return;
140         }
141
142         /* Update the data */
143         data &= andvalue;
144         data |= orvalue;
145
146         /* WRITE OPCODE */
147         if (pch_silicon_supported(dev, PCH_TYPE_CPT, PCH_STEP_B0))
148                 writel(IOBPS_RW_BX, RCB_REG(IOBPS));
149         else
150                 writel(IOBPS_WRITE_AX, RCB_REG(IOBPS));
151         if (!iobp_poll())
152                 return;
153
154         /* Write IOBP data */
155         writel(data, RCB_REG(IOBPD));
156         if (!iobp_poll())
157                 return;
158 }
159
160 static int bd82x6x_probe(struct udevice *dev)
161 {
162         if (!(gd->flags & GD_FLG_RELOC))
163                 return 0;
164
165         /* Cause the SATA device to do its init */
166         uclass_first_device(UCLASS_AHCI, &dev);
167
168         return 0;
169 }
170 #endif /* CONFIG_HAVE_FSP */
171
172 static int bd82x6x_pch_get_spi_base(struct udevice *dev, ulong *sbasep)
173 {
174         u32 rcba;
175
176         dm_pci_read_config32(dev, PCH_RCBA, &rcba);
177         /* Bits 31-14 are the base address, 13-1 are reserved, 0 is enable */
178         rcba = rcba & 0xffffc000;
179         *sbasep = rcba + 0x3800;
180
181         return 0;
182 }
183
184 static int bd82x6x_set_spi_protect(struct udevice *dev, bool protect)
185 {
186         return lpc_set_spi_protect(dev, BIOS_CTRL, protect);
187 }
188
189 static int bd82x6x_get_gpio_base(struct udevice *dev, u32 *gbasep)
190 {
191         u32 base;
192
193         /*
194          * GPIO_BASE moved to its current offset with ICH6, but prior to
195          * that it was unused (or undocumented). Check that it looks
196          * okay: not all ones or zeros.
197          *
198          * Note we don't need check bit0 here, because the Tunnel Creek
199          * GPIO base address register bit0 is reserved (read returns 0),
200          * while on the Ivybridge the bit0 is used to indicate it is an
201          * I/O space.
202          */
203         dm_pci_read_config32(dev, GPIO_BASE, &base);
204         if (base == 0x00000000 || base == 0xffffffff) {
205                 debug("%s: unexpected BASE value\n", __func__);
206                 return -ENODEV;
207         }
208
209         /*
210          * Okay, I guess we're looking at the right device. The actual
211          * GPIO registers are in the PCI device's I/O space, starting
212          * at the offset that we just read. Bit 0 indicates that it's
213          * an I/O address, not a memory address, so mask that off.
214          */
215         *gbasep = base & 1 ? base & ~3 : base & ~15;
216
217         return 0;
218 }
219
220 static int bd82x6x_ioctl(struct udevice *dev, enum pch_req_t req, void *data,
221                          int size)
222 {
223         u32 rcba, val;
224
225         switch (req) {
226         case PCH_REQ_HDA_CONFIG:
227                 dm_pci_read_config32(dev, PCH_RCBA, &rcba);
228                 val = readl(rcba + RCBA_AUDIO_CONFIG);
229                 if (!(val & RCBA_AUDIO_CONFIG_HDA))
230                         return -ENOENT;
231
232                 return val & RCBA_AUDIO_CONFIG_MASK;
233         case PCH_REQ_PMBASE_INFO: {
234                 struct pch_pmbase_info *pm = data;
235                 int ret;
236
237                 /* Find the base address of the powermanagement registers */
238                 ret = dm_pci_read_config16(dev, 0x40, &pm->base);
239                 if (ret)
240                         return ret;
241                 pm->base &= 0xfffe;
242                 pm->gpio0_en_ofs = GPE0_EN;
243                 pm->pm1_sts_ofs = PM1_STS;
244                 pm->pm1_cnt_ofs = PM1_CNT;
245
246                 return 0;
247         }
248         default:
249                 return -ENOSYS;
250         }
251 }
252
253 static const struct pch_ops bd82x6x_pch_ops = {
254         .get_spi_base   = bd82x6x_pch_get_spi_base,
255         .set_spi_protect = bd82x6x_set_spi_protect,
256         .get_gpio_base  = bd82x6x_get_gpio_base,
257         .ioctl          = bd82x6x_ioctl,
258 };
259
260 static const struct udevice_id bd82x6x_ids[] = {
261         { .compatible = "intel,bd82x6x" },
262         { }
263 };
264
265 U_BOOT_DRIVER(bd82x6x_drv) = {
266         .name           = "bd82x6x",
267         .id             = UCLASS_PCH,
268         .of_match       = bd82x6x_ids,
269 #ifndef CONFIG_HAVE_FSP
270         .probe          = bd82x6x_probe,
271 #endif
272         .ops            = &bd82x6x_pch_ops,
273 };