blob: b00b8ac511358fa4821e793a32a3035af681947d [file] [log] [blame]
Raul E Rangel3ba21802021-06-24 17:03:35 -06001/* SPDX-License-Identifier: GPL-2.0-only */
2
Raul E Rangeld373d5d2021-06-25 11:07:23 -06003#include <amdblocks/lpc.h>
4#include <amdblocks/spi.h>
5#include <assert.h>
Raul E Rangel3ba21802021-06-24 17:03:35 -06006#include <boot_device.h>
Raul E Rangeld373d5d2021-06-25 11:07:23 -06007#include <commonlib/bsd/cb_err.h>
8#include <commonlib/bsd/helpers.h>
Raul E Rangel3ba21802021-06-24 17:03:35 -06009#include <commonlib/region.h>
Raul E Rangeld373d5d2021-06-25 11:07:23 -060010#include <console/console.h>
11#include <delay.h>
12#include <device/pci_ops.h>
13#include <soc/pci_devs.h>
Raul E Rangel3ba21802021-06-24 17:03:35 -060014#include <spi_flash.h>
15#include <string.h>
16#include <types.h>
17
18/* The ROM is memory mapped just below 4GiB. Form a pointer for the base. */
19#define rom_base ((void *)(uintptr_t)(0x100000000ULL - CONFIG_ROM_SIZE))
20
Raul E Rangeld373d5d2021-06-25 11:07:23 -060021struct spi_dma_transaction {
22 uint8_t *destination;
23 size_t source;
24 size_t size;
25 size_t transfer_size;
26 size_t remaining;
27};
28
Raul E Rangel3ba21802021-06-24 17:03:35 -060029static void *spi_dma_mmap(const struct region_device *rd, size_t offset, size_t size __unused)
30{
31 const struct mem_region_device *mdev;
32
33 mdev = container_of(rd, __typeof__(*mdev), rdev);
34
35 return &mdev->base[offset];
36}
37
38static int spi_dma_munmap(const struct region_device *rd __unused, void *mapping __unused)
39{
40 return 0;
41}
42
43static ssize_t spi_dma_readat_mmap(const struct region_device *rd, void *b, size_t offset,
44 size_t size)
45{
46 const struct mem_region_device *mdev;
47
48 mdev = container_of(rd, __typeof__(*mdev), rdev);
49
50 memcpy(b, &mdev->base[offset], size);
51
52 return size;
53}
54
Raul E Rangeld373d5d2021-06-25 11:07:23 -060055static bool spi_dma_is_busy(void)
56{
57 return pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL)
58 & LPC_ROM_DMA_CTRL_START;
59}
60
61static bool spi_dma_has_error(void)
62{
63 return pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL)
64 & LPC_ROM_DMA_CTRL_ERROR;
65}
66
67static bool can_use_dma(void *destination, size_t source, size_t size)
68{
69 /*
70 * Print a notice if reading more than 1024 bytes using mmap. This makes
71 * it easier to debug why the SPI DMA wasn't used.
72 */
73 const size_t warning_size = 1024;
74
75 if (size < LPC_ROM_DMA_MIN_ALIGNMENT)
76 return false;
77
78 if (!IS_ALIGNED((uintptr_t)destination, LPC_ROM_DMA_MIN_ALIGNMENT)) {
79 if (size > warning_size)
80 printk(BIOS_DEBUG, "Target %p is unaligned\n", destination);
81 return false;
82 }
83
84 if (!IS_ALIGNED(source, LPC_ROM_DMA_MIN_ALIGNMENT)) {
85 if (size > warning_size)
86 printk(BIOS_DEBUG, "Source %#zx is unaligned\n", source);
87 return false;
88 }
89
90 return true;
91}
92
93static void start_spi_dma_transaction(struct spi_dma_transaction *transaction)
94{
95 uint32_t ctrl;
96
97 printk(BIOS_SPEW, "%s: dest: %p, source: %#zx, remaining: %zu\n", __func__,
98 transaction->destination, transaction->source, transaction->remaining);
99
100 /*
101 * We should have complete control over the DMA controller, so there shouldn't
102 * be any outstanding transactions.
103 */
104 assert(!spi_dma_is_busy());
105 assert(IS_ALIGNED((uintptr_t)transaction->destination, LPC_ROM_DMA_MIN_ALIGNMENT));
106 assert(IS_ALIGNED(transaction->source, LPC_ROM_DMA_MIN_ALIGNMENT));
107 assert(transaction->remaining >= LPC_ROM_DMA_MIN_ALIGNMENT);
108
109 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_SRC_ADDR, transaction->source);
110 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_DST_ADDR,
111 (uintptr_t)transaction->destination);
112
113 ctrl = pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL);
114 ctrl &= ~LPC_ROM_DMA_CTRL_DW_COUNT_MASK;
115
116 transaction->transfer_size =
117 MIN(LPC_ROM_DMA_CTRL_MAX_BYTES,
118 ALIGN_DOWN(transaction->remaining, LPC_ROM_DMA_MIN_ALIGNMENT));
119
120 ctrl |= LPC_ROM_DMA_CTRL_DW_COUNT(transaction->transfer_size);
121 ctrl |= LPC_ROM_DMA_CTRL_ERROR; /* Clear error */
122 ctrl |= LPC_ROM_DMA_CTRL_START;
123
124 pci_write_config32(SOC_LPC_DEV, LPC_ROM_DMA_EC_HOST_CONTROL, ctrl);
125}
126
127/* Returns true if transaction is still in progress. */
128static bool continue_spi_dma_transaction(const struct region_device *rd,
129 struct spi_dma_transaction *transaction)
130{
131 /* Verify we are looking at the correct transaction */
132 assert(pci_read_config32(SOC_LPC_DEV, LPC_ROM_DMA_SRC_ADDR) == transaction->source);
133
134 if (spi_dma_is_busy())
135 return true;
136
137 if (spi_dma_has_error()) {
138 printk(BIOS_ERR,
139 "ERROR: SPI DMA failure: dest: %p, source: %#zx, size: %zu\n",
140 transaction->destination, transaction->source,
141 transaction->transfer_size);
142 return false;
143 }
144
145 transaction->destination += transaction->transfer_size;
146 transaction->source += transaction->transfer_size;
147 transaction->remaining -= transaction->transfer_size;
148
149 if (transaction->remaining >= LPC_ROM_DMA_MIN_ALIGNMENT) {
150 start_spi_dma_transaction(transaction);
151 return true;
152 }
153
154 if (transaction->remaining > 0) {
155 /* Use mmap to finish off the transfer */
156 spi_dma_readat_mmap(rd, transaction->destination, transaction->source,
157 transaction->remaining);
158
159 transaction->destination += transaction->remaining;
160 transaction->source += transaction->remaining;
161 transaction->remaining -= transaction->remaining;
162 }
163
164 return false;
165}
166
167static ssize_t spi_dma_readat_dma(const struct region_device *rd, void *destination,
168 size_t source, size_t size)
169{
170 struct spi_dma_transaction transaction = {
171 .destination = destination,
172 .source = source,
173 .size = size,
174 .remaining = size,
175 };
176
177 printk(BIOS_SPEW, "%s: start: dest: %p, source: %#zx, size: %zu\n", __func__,
178 destination, source, size);
179
180 start_spi_dma_transaction(&transaction);
181
182 do {
183 udelay(2);
184 } while (continue_spi_dma_transaction(rd, &transaction));
185
186 printk(BIOS_SPEW, "%s: end: dest: %p, source: %#zx, remaining: %zu\n",
187 __func__, destination, source, transaction.remaining);
188
189 if (transaction.remaining)
190 return -1;
191
192 return transaction.size;
193}
194
195static ssize_t spi_dma_readat(const struct region_device *rd, void *b, size_t offset,
196 size_t size)
197{
198 if (can_use_dma(b, offset, size))
199 return spi_dma_readat_dma(rd, b, offset, size);
200 else
201 return spi_dma_readat_mmap(rd, b, offset, size);
202}
203
Raul E Rangel3ba21802021-06-24 17:03:35 -0600204const struct region_device_ops spi_dma_rdev_ro_ops = {
205 .mmap = spi_dma_mmap,
206 .munmap = spi_dma_munmap,
Raul E Rangeld373d5d2021-06-25 11:07:23 -0600207 .readat = spi_dma_readat,
Raul E Rangel3ba21802021-06-24 17:03:35 -0600208};
209
210static const struct mem_region_device boot_dev = {
211 .base = rom_base,
212 .rdev = REGION_DEV_INIT(&spi_dma_rdev_ro_ops, 0, CONFIG_ROM_SIZE),
213};
214
215const struct region_device *boot_device_ro(void)
216{
217 return &boot_dev.rdev;
218}
219
220uint32_t spi_flash_get_mmap_windows(struct flash_mmap_window *table)
221{
222 table->flash_base = 0;
223 table->host_base = (uint32_t)(uintptr_t)rom_base;
224 table->size = CONFIG_ROM_SIZE;
225
226 return 1;
227}
Raul E Rangeld373d5d2021-06-25 11:07:23 -0600228
229/*
230 * Without this magic bit, the SPI DMA controller will write 0s into the destination if an MMAP
231 * read happens while a DMA transaction is in progress. i.e., PSP is reading from SPI. The bit
232 * that fixes this was added to Cezanne, Renoir and later SoCs. So the SPI DMA controller is not
233 * reliable on any prior generations.
234 */
235static void spi_dma_fix(void)
236{
237 /* Internal only registers */
238 uint8_t val = spi_read8(0xfc);
239 val |= BIT(6);
240 spi_write8(0xfc, val);
241}
242
243void boot_device_init(void)
244{
245 spi_dma_fix();
246}