blob: 1e1a2f44aec6e88f58d1c75404d9aae52d71d409 [file] [log] [blame]
Angel Pons182dbde2020-04-02 23:49:05 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03002
3#ifndef _HUDSON_EARLY_SETUP_C_
4#define _HUDSON_EARLY_SETUP_C_
5
Marc Jonesf962aa52017-03-22 18:47:49 +08006#include <assert.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +03007#include <stdint.h>
Michał Żygowski287ce5f2019-12-01 17:41:23 +01008#include <amdblocks/acpimmio.h>
Kyösti Mälkki13f66502019-03-03 08:01:05 +02009#include <device/mmio.h>
Kyösti Mälkkif1b58b72019-03-01 13:43:02 +020010#include <device/pci_ops.h>
Elyes HAOUASeb789f02018-10-27 16:40:25 +020011
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030012#include "hudson.h"
Dave Frodinf364fc72015-03-13 08:22:17 -060013#include "pci_devs.h"
Piotr Króldcd2f172016-05-27 12:04:13 +020014#include <Fch/Fch.h>
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030015
16void hudson_pci_port80(void)
17{
18 u8 byte;
Antonello Dettori1ac97282016-09-03 10:45:33 +020019 pci_devfn_t dev;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030020
21 /* P2P Bridge */
22 dev = PCI_DEV(0, 0x14, 4);
23
24 /* Chip Control: Enable subtractive decoding */
25 byte = pci_read_config8(dev, 0x40);
26 byte |= 1 << 5;
27 pci_write_config8(dev, 0x40, byte);
28
29 /* Misc Control: Enable subtractive decoding if 0x40 bit 5 is set */
30 byte = pci_read_config8(dev, 0x4B);
31 byte |= 1 << 7;
32 pci_write_config8(dev, 0x4B, byte);
33
34 /* The same IO Base and IO Limit here is meaningful because we set the
35 * bridge to be subtractive. During early setup stage, we have to make
36 * sure that data can go through port 0x80.
37 */
38 /* IO Base: 0xf000 */
39 byte = pci_read_config8(dev, 0x1C);
40 byte |= 0xF << 4;
41 pci_write_config8(dev, 0x1C, byte);
42
43 /* IO Limit: 0xf000 */
44 byte = pci_read_config8(dev, 0x1D);
45 byte |= 0xF << 4;
46 pci_write_config8(dev, 0x1D, byte);
47
48 /* PCI Command: Enable IO response */
49 byte = pci_read_config8(dev, 0x04);
50 byte |= 1 << 0;
51 pci_write_config8(dev, 0x04, byte);
52
53 /* LPC controller */
54 dev = PCI_DEV(0, 0x14, 3);
55
56 byte = pci_read_config8(dev, 0x4A);
57 byte &= ~(1 << 5); /* disable lpc port 80 */
58 pci_write_config8(dev, 0x4A, byte);
59}
60
61void hudson_lpc_port80(void)
62{
63 u8 byte;
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030064
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030065 /* Enable port 80 LPC decode in pci function 3 configuration space. */
Elyes HAOUAS2f58a002020-04-22 16:07:39 +020066 const pci_devfn_t dev = PCI_DEV(0, 0x14, 3);
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +030067 byte = pci_read_config8(dev, 0x4a);
68 byte |= 1 << 5; /* enable port 80 */
69 pci_write_config8(dev, 0x4a, byte);
70}
71
Dave Frodinf364fc72015-03-13 08:22:17 -060072void hudson_lpc_decode(void)
73{
Michał Żygowski8cee45c2019-11-23 18:03:46 +010074 u32 tmp;
Dave Frodinf364fc72015-03-13 08:22:17 -060075
Kyösti Mälkkia244d5e2019-12-09 08:08:58 +020076 /* Enable LPC controller */
77 pm_write8(0xec, pm_read8(0xec) | 0x01);
78
Elyes HAOUAS2f58a002020-04-22 16:07:39 +020079 const pci_devfn_t dev = PCI_DEV(0, 0x14, 3);
Elyes Haouas4450bee2022-02-11 22:01:57 +010080 /* Serial port enumeration on Hudson:
Michał Żygowski8cee45c2019-11-23 18:03:46 +010081 * PORT0 - 0x3f8
82 * PORT1 - 0x2f8
83 * PORT5 - 0x2e8
84 * PORT7 - 0x3e8
85 */
86 tmp = DECODE_ENABLE_SERIAL_PORT0 | DECODE_ENABLE_SERIAL_PORT1
87 | DECODE_ENABLE_SERIAL_PORT5 | DECODE_ENABLE_SERIAL_PORT7;
Dave Frodinf364fc72015-03-13 08:22:17 -060088
89 pci_write_config32(dev, LPC_IO_PORT_DECODE_ENABLE, tmp);
90}
91
Marc Jonesf962aa52017-03-22 18:47:49 +080092static void enable_wideio(uint8_t port, uint16_t size)
93{
94 uint32_t wideio_enable[] = {
95 LPC_WIDEIO0_ENABLE,
96 LPC_WIDEIO1_ENABLE,
97 LPC_WIDEIO2_ENABLE
98 };
99 uint32_t alt_wideio_enable[] = {
100 LPC_ALT_WIDEIO0_ENABLE,
101 LPC_ALT_WIDEIO1_ENABLE,
102 LPC_ALT_WIDEIO2_ENABLE
103 };
Elyes HAOUAS2f58a002020-04-22 16:07:39 +0200104 const pci_devfn_t dev = PCI_DEV(0, PCU_DEV, LPC_FUNC);
Marc Jonesf962aa52017-03-22 18:47:49 +0800105 uint32_t tmp;
106
107 /* Only allow port 0-2 */
108 assert(port <= ARRAY_SIZE(wideio_enable));
109
110 if (size == 16) {
111 tmp = pci_read_config32(dev, LPC_ALT_WIDEIO_RANGE_ENABLE);
112 tmp |= alt_wideio_enable[port];
113 pci_write_config32(dev, LPC_ALT_WIDEIO_RANGE_ENABLE, tmp);
Elyes HAOUASb0f19882018-06-09 11:59:00 +0200114 } else { /* 512 */
Marc Jonesf962aa52017-03-22 18:47:49 +0800115 tmp = pci_read_config32(dev, LPC_ALT_WIDEIO_RANGE_ENABLE);
116 tmp &= ~alt_wideio_enable[port];
117 pci_write_config32(dev, LPC_ALT_WIDEIO_RANGE_ENABLE, tmp);
118 }
119
120 /* Enable the range */
121 tmp = pci_read_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE);
122 tmp |= wideio_enable[port];
123 pci_write_config32(dev, LPC_IO_OR_MEM_DECODE_ENABLE, tmp);
124}
125
126/*
127 * lpc_wideio_window() may be called any point in romstage, but take
128 * care that AGESA doesn't overwrite the range this function used.
129 * The function checks if there is an empty range and if all ranges are
130 * used the function throws an assert. The function doesn't check for a
131 * duplicate range, for ranges that can be merged into a single
132 * range, or ranges that overlap.
133 *
134 * The developer is expected to ensure that there are no conflicts.
135 */
136static void lpc_wideio_window(uint16_t base, uint16_t size)
137{
Elyes HAOUAS2f58a002020-04-22 16:07:39 +0200138 const pci_devfn_t dev = PCI_DEV(0, PCU_DEV, LPC_FUNC);
Marc Jonesf962aa52017-03-22 18:47:49 +0800139 u32 tmp;
140
141 /* Support 512 or 16 bytes per range */
142 assert(size == 512 || size == 16);
143
144 /* Find and open Base Register and program it */
145 tmp = pci_read_config32(dev, LPC_WIDEIO_GENERIC_PORT);
146
147 if ((tmp & 0xFFFF) == 0) { /* WIDEIO0 */
148 tmp |= base;
149 pci_write_config32(dev, LPC_WIDEIO_GENERIC_PORT, tmp);
150 enable_wideio(0, size);
151 } else if ((tmp & 0xFFFF0000) == 0) { /* WIDEIO1 */
152 tmp |= (base << 16);
153 pci_write_config32(dev, LPC_WIDEIO_GENERIC_PORT, tmp);
154 enable_wideio(1, size);
155 } else { /* Check WIDEIO2 register */
156 tmp = pci_read_config32(dev, LPC_WIDEIO2_GENERIC_PORT);
157 if ((tmp & 0xFFFF) == 0) { /* WIDEIO2 */
158 tmp |= base;
159 pci_write_config32(dev, LPC_WIDEIO2_GENERIC_PORT, tmp);
160 enable_wideio(2, size);
161 } else { /* All WIDEIO locations used*/
Julius Werner3e034b62020-07-29 17:39:21 -0700162 BUG();
Marc Jonesf962aa52017-03-22 18:47:49 +0800163 }
164 }
165}
166
167void lpc_wideio_512_window(uint16_t base)
168{
169 assert(IS_ALIGNED(base, 512));
170 lpc_wideio_window(base, 512);
171}
172
173void lpc_wideio_16_window(uint16_t base)
174{
175 assert(IS_ALIGNED(base, 16));
176 lpc_wideio_window(base, 16);
177}
178
Piotr Króldcd2f172016-05-27 12:04:13 +0200179void hudson_clk_output_48Mhz(void)
180{
Marshall Dawson0bf3f552017-07-13 12:06:25 -0600181 u32 ctrl;
Piotr Króldcd2f172016-05-27 12:04:13 +0200182
183 /*
184 * Enable the X14M_25M_48M_OSC pin and leaving it at it's default so
185 * 48Mhz will be on ball AP13 (FT3b package)
186 */
Michał Żygowski287ce5f2019-12-01 17:41:23 +0100187 ctrl = misc_read32(FCH_MISC_REG40);
Piotr Króldcd2f172016-05-27 12:04:13 +0200188
189 /* clear the OSCOUT1_ClkOutputEnb to enable the 48 Mhz clock */
Elyes Haouas4c152112022-07-16 09:51:03 +0200190 ctrl &= (u32)~(1 << 2);
Michał Żygowski287ce5f2019-12-01 17:41:23 +0100191 misc_write32(FCH_MISC_REG40, ctrl);
Piotr Króldcd2f172016-05-27 12:04:13 +0200192}
193
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700194static uintptr_t hudson_spibase(void)
195{
196 /* Make sure the base address is predictable */
Elyes HAOUAS2f58a002020-04-22 16:07:39 +0200197 const pci_devfn_t dev = PCI_DEV(0, 0x14, 3);
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700198
199 u32 base = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER)
200 & 0xfffffff0;
Elyes Haouas4c152112022-07-16 09:51:03 +0200201 if (!base) {
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700202 base = SPI_BASE_ADDRESS;
203 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, base
204 | SPI_ROM_ENABLE);
205 /* PCI_COMMAND_MEMORY is read-only and enabled. */
206 }
207 return (uintptr_t)base;
208}
209
210void hudson_set_spi100(u16 norm, u16 fast, u16 alt, u16 tpm)
211{
212 uintptr_t base = hudson_spibase();
Elyes Haouas067642d2022-12-03 13:38:56 +0100213 write16p(base + SPI100_SPEED_CONFIG,
214 (norm << SPI_NORM_SPEED_NEW_SH) |
215 (fast << SPI_FAST_SPEED_NEW_SH) |
216 (alt << SPI_ALT_SPEED_NEW_SH) |
217 (tpm << SPI_TPM_SPEED_NEW_SH));
218 write16p(base + SPI100_ENABLE, SPI_USE_SPI100 |
219 read16p(base + SPI100_ENABLE));
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700220}
221
222void hudson_disable_4dw_burst(void)
223{
224 uintptr_t base = hudson_spibase();
Elyes Haouas067642d2022-12-03 13:38:56 +0100225 write16p(base + SPI100_HOST_PREF_CONFIG,
226 read16p(base + SPI100_HOST_PREF_CONFIG)
227 & ~SPI_RD4DW_EN_HOST);
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700228}
229
230/* Hudson 1-3 only. For Hudson 1, call with fast=1 */
231void hudson_set_readspeed(u16 norm, u16 fast)
232{
233 uintptr_t base = hudson_spibase();
Elyes Haouas067642d2022-12-03 13:38:56 +0100234 write16p(base + SPI_CNTRL1,
235 (read16p(base + SPI_CNTRL1)
236 & ~SPI_CNTRL1_SPEED_MASK)
237 | (norm << SPI_NORM_SPEED_SH)
238 | (fast << SPI_FAST_SPEED_SH));
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700239}
240
241void hudson_read_mode(u32 mode)
242{
243 uintptr_t base = hudson_spibase();
Elyes Haouas067642d2022-12-03 13:38:56 +0100244 write32p(base + SPI_CNTRL0,
245 (read32p(base + SPI_CNTRL0)
246 & ~SPI_READ_MODE_MASK) | mode);
Marshall Dawson91dea4a2017-02-10 16:03:54 -0700247}
248
Marc Jones6fcaaef2017-04-20 16:48:42 -0600249void hudson_tpm_decode_spi(void)
250{
Elyes HAOUAS2f58a002020-04-22 16:07:39 +0200251 const pci_devfn_t dev = PCI_DEV(0, 0x14, 3); /* LPC device */
Marc Jones6fcaaef2017-04-20 16:48:42 -0600252
253 u32 spibase = pci_read_config32(dev, SPIROM_BASE_ADDRESS_REGISTER);
254 pci_write_config32(dev, SPIROM_BASE_ADDRESS_REGISTER, spibase
255 | ROUTE_TPM_2_SPI);
256}
257
Kyösti Mälkkie8b4da22014-10-21 18:22:32 +0300258#endif