| 1 |
|
| 2 |
|
| 3 |
#define __SIMPLE_DEVICE__ |
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <commonlib/bsd/helpers.h> |
| 7 |
#include <console/console.h> |
| 8 |
#include <device/mmio.h> |
| 9 |
#include <device/pci_def.h> |
| 10 |
#include <device/pci_ops.h> |
| 11 |
#include <device/pci_type.h> |
| 12 |
#include <intelblocks/pcr.h> |
| 13 |
#include <soc/pci_devs.h> |
| 14 |
#include <timer.h> |
| 15 |
#include <types.h> |
| 16 |
|
| 17 |
#if (CONFIG_PCR_BASE_ADDRESS == 0) |
| 18 |
#error "PCR_BASE_ADDRESS need to be non-zero!" |
| 19 |
#endif |
| 20 |
|
| 21 |
#if !CONFIG(PCR_COMMON_IOSF_1_0) |
| 22 |
|
| 23 |
#define PCR_SBI_CMD_TIMEOUT 10 |
| 24 |
|
| 25 |
|
| 26 |
#define P2SB_CR_SBI_ADDR 0xd0 |
| 27 |
#define P2SB_CR_SBI_DESTID 24 |
| 28 |
#define P2SB_CR_SBI_DATA 0xd4 |
| 29 |
#define P2SB_CR_SBI_STATUS 0xd8 |
| 30 |
|
| 31 |
#define P2SB_CR_SBI_OPCODE 8 |
| 32 |
#define P2SB_CR_SBI_OPCODE_MASK 0xFF00 |
| 33 |
|
| 34 |
#define P2SB_CR_SBI_POSTED 7 |
| 35 |
#define P2SB_CR_SBI_POSTED_MASK 0x0080 |
| 36 |
|
| 37 |
#define P2SB_CR_SBI_STATUS_MASK 0x0006 |
| 38 |
#define P2SB_CR_SBI_STATUS_SUCCESS 0 |
| 39 |
#define P2SB_CR_SBI_STATUS_NOT_SUPPORTED 1 |
| 40 |
#define P2SB_CR_SBI_STATUS_POWERED_DOWN 2 |
| 41 |
#define P2SB_CR_SBI_STATUS_MULTI_CAST_MIXED 3 |
| 42 |
|
| 43 |
#define P2SB_CR_SBI_STATUS_READY 0 |
| 44 |
#define P2SB_CR_SBI_STATUS_BUSY 1 |
| 45 |
#define P2SB_CR_SBI_ROUTE_IDEN 0xda |
| 46 |
|
| 47 |
#define P2SB_CR_SBI_FBE 12 |
| 48 |
#define P2SB_CR_SBI_FBE_MASK 0xF |
| 49 |
|
| 50 |
#define P2SB_CR_SBI_BAR 8 |
| 51 |
#define P2SB_CR_SBI_MASK 0x7 |
| 52 |
|
| 53 |
#define P2SB_CR_SBI_FID 0 |
| 54 |
#define P2SB_CR_SBI_FID_MASK 0xFF |
| 55 |
#define P2SB_CR_SBI_EXT_ADDR 0xdc |
| 56 |
#endif |
| 57 |
|
| 58 |
static void *__pcr_reg_address(uint8_t pid, uint16_t offset) |
| 59 |
{ |
| 60 |
uintptr_t reg_addr; |
| 61 |
|
| 62 |
|
| 63 |
reg_addr = CONFIG_PCR_BASE_ADDRESS; |
| 64 |
reg_addr += ((uintptr_t)pid) << PCR_PORTID_SHIFT; |
| 65 |
reg_addr += (uintptr_t)offset; |
| 66 |
|
| 67 |
return (void *)reg_addr; |
| 68 |
} |
| 69 |
|
| 70 |
void *pcr_reg_address(uint8_t pid, uint16_t offset) |
| 71 |
{ |
| 72 |
if (CONFIG(PCR_COMMON_IOSF_1_0)) |
| 73 |
assert(IS_ALIGNED(offset, sizeof(uint32_t))); |
| 74 |
|
| 75 |
return __pcr_reg_address(pid, offset); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static inline void check_pcr_offset_align(uint16_t offset, size_t size) |
| 86 |
{ |
| 87 |
const size_t align = CONFIG(PCR_COMMON_IOSF_1_0) ? |
| 88 |
sizeof(uint32_t) : size; |
| 89 |
|
| 90 |
assert(IS_ALIGNED(offset, align)); |
| 91 |
} |
| 92 |
|
| 93 |
uint32_t pcr_read32(uint8_t pid, uint16_t offset) |
| 94 |
{ |
| 95 |
|
| 96 |
assert(IS_ALIGNED(offset, sizeof(uint32_t))); |
| 97 |
|
| 98 |
return read32(__pcr_reg_address(pid, offset)); |
| 99 |
} |
| 100 |
|
| 101 |
uint16_t pcr_read16(uint8_t pid, uint16_t offset) |
| 102 |
{ |
| 103 |
|
| 104 |
check_pcr_offset_align(offset, sizeof(uint16_t)); |
| 105 |
|
| 106 |
return read16(__pcr_reg_address(pid, offset)); |
| 107 |
} |
| 108 |
|
| 109 |
uint8_t pcr_read8(uint8_t pid, uint16_t offset) |
| 110 |
{ |
| 111 |
|
| 112 |
check_pcr_offset_align(offset, sizeof(uint8_t)); |
| 113 |
|
| 114 |
return read8(__pcr_reg_address(pid, offset)); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
static inline void write_completion(uint8_t pid, uint16_t offset) |
| 124 |
{ |
| 125 |
read32(__pcr_reg_address(pid, ALIGN_DOWN(offset, sizeof(uint32_t)))); |
| 126 |
} |
| 127 |
|
| 128 |
void pcr_write32(uint8_t pid, uint16_t offset, uint32_t indata) |
| 129 |
{ |
| 130 |
|
| 131 |
assert(IS_ALIGNED(offset, sizeof(indata))); |
| 132 |
|
| 133 |
write32(__pcr_reg_address(pid, offset), indata); |
| 134 |
|
| 135 |
write_completion(pid, offset); |
| 136 |
} |
| 137 |
|
| 138 |
void pcr_write16(uint8_t pid, uint16_t offset, uint16_t indata) |
| 139 |
{ |
| 140 |
|
| 141 |
check_pcr_offset_align(offset, sizeof(uint16_t)); |
| 142 |
|
| 143 |
write16(__pcr_reg_address(pid, offset), indata); |
| 144 |
|
| 145 |
write_completion(pid, offset); |
| 146 |
} |
| 147 |
|
| 148 |
void pcr_write8(uint8_t pid, uint16_t offset, uint8_t indata) |
| 149 |
{ |
| 150 |
|
| 151 |
check_pcr_offset_align(offset, sizeof(uint8_t)); |
| 152 |
|
| 153 |
write8(__pcr_reg_address(pid, offset), indata); |
| 154 |
|
| 155 |
write_completion(pid, offset); |
| 156 |
} |
| 157 |
|
| 158 |
void pcr_rmw32(uint8_t pid, uint16_t offset, uint32_t anddata, uint32_t ordata) |
| 159 |
{ |
| 160 |
uint32_t data32; |
| 161 |
|
| 162 |
data32 = pcr_read32(pid, offset); |
| 163 |
data32 &= anddata; |
| 164 |
data32 |= ordata; |
| 165 |
pcr_write32(pid, offset, data32); |
| 166 |
} |
| 167 |
|
| 168 |
void pcr_rmw16(uint8_t pid, uint16_t offset, uint16_t anddata, uint16_t ordata) |
| 169 |
{ |
| 170 |
uint16_t data16; |
| 171 |
|
| 172 |
data16 = pcr_read16(pid, offset); |
| 173 |
data16 &= anddata; |
| 174 |
data16 |= ordata; |
| 175 |
pcr_write16(pid, offset, data16); |
| 176 |
} |
| 177 |
|
| 178 |
void pcr_rmw8(uint8_t pid, uint16_t offset, uint8_t anddata, uint8_t ordata) |
| 179 |
{ |
| 180 |
uint8_t data8; |
| 181 |
|
| 182 |
data8 = pcr_read8(pid, offset); |
| 183 |
data8 &= anddata; |
| 184 |
data8 |= ordata; |
| 185 |
pcr_write8(pid, offset, data8); |
| 186 |
} |
| 187 |
|
| 188 |
void pcr_or32(uint8_t pid, uint16_t offset, uint32_t ordata) |
| 189 |
{ |
| 190 |
uint32_t data32; |
| 191 |
|
| 192 |
data32 = pcr_read32(pid, offset); |
| 193 |
data32 |= ordata; |
| 194 |
pcr_write32(pid, offset, data32); |
| 195 |
} |
| 196 |
|
| 197 |
void pcr_or16(uint8_t pid, uint16_t offset, uint16_t ordata) |
| 198 |
{ |
| 199 |
uint16_t data16; |
| 200 |
|
| 201 |
data16 = pcr_read16(pid, offset); |
| 202 |
data16 |= ordata; |
| 203 |
pcr_write16(pid, offset, data16); |
| 204 |
} |
| 205 |
|
| 206 |
void pcr_or8(uint8_t pid, uint16_t offset, uint8_t ordata) |
| 207 |
{ |
| 208 |
uint8_t data8; |
| 209 |
|
| 210 |
data8 = pcr_read8(pid, offset); |
| 211 |
data8 |= ordata; |
| 212 |
pcr_write8(pid, offset, data8); |
| 213 |
} |
| 214 |
|
| 215 |
#if !CONFIG(PCR_COMMON_IOSF_1_0) |
| 216 |
|
| 217 |
static int pcr_wait_for_completion(const pci_devfn_t dev) |
| 218 |
{ |
| 219 |
struct stopwatch sw; |
| 220 |
|
| 221 |
stopwatch_init_msecs_expire(&sw, PCR_SBI_CMD_TIMEOUT); |
| 222 |
do { |
| 223 |
if ((pci_read_config16(dev, P2SB_CR_SBI_STATUS) & |
| 224 |
P2SB_CR_SBI_STATUS_BUSY) == 0) |
| 225 |
return 0; |
| 226 |
} while (!stopwatch_expired(&sw)); |
| 227 |
|
| 228 |
return -1; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
int pcr_execute_sideband_msg(pci_devfn_t dev, struct pcr_sbi_msg *msg, uint32_t *data, |
| 248 |
uint8_t *response) |
| 249 |
{ |
| 250 |
uint32_t sbi_data; |
| 251 |
uint16_t sbi_status; |
| 252 |
uint16_t sbi_rid; |
| 253 |
|
| 254 |
if (!msg || !data || !response) { |
| 255 |
printk(BIOS_ERR, "Pointer checked for NULL Fail! " |
| 256 |
"msg = %p \t data = %p \t response = %p\n", |
| 257 |
msg, data, response); |
| 258 |
return -1; |
| 259 |
} |
| 260 |
|
| 261 |
switch (msg->opcode) { |
| 262 |
case MEM_READ: |
| 263 |
case MEM_WRITE: |
| 264 |
case PCI_CONFIG_READ: |
| 265 |
case PCI_CONFIG_WRITE: |
| 266 |
case PCR_READ: |
| 267 |
case PCR_WRITE: |
| 268 |
case GPIO_LOCK_UNLOCK: |
| 269 |
break; |
| 270 |
default: |
| 271 |
printk(BIOS_ERR, "SBI Failure: Wrong Input = %x!\n", |
| 272 |
msg->opcode); |
| 273 |
return -1; |
| 274 |
} |
| 275 |
|
| 276 |
if (pci_read_config16(dev, PCI_VENDOR_ID) == 0xffff) { |
| 277 |
printk(BIOS_ERR, "SBI Failure: P2SB device Hidden!\n"); |
| 278 |
return -1; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
if (pcr_wait_for_completion(dev)) { |
| 287 |
printk(BIOS_ERR, "SBI Failure: Time Out!\n"); |
| 288 |
return -1; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
*response = P2SB_CR_SBI_STATUS_NOT_SUPPORTED; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
pci_write_config32(dev, P2SB_CR_SBI_ADDR, |
| 299 |
(msg->pid << P2SB_CR_SBI_DESTID) | msg->offset); |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
pci_write_config32(dev, P2SB_CR_SBI_EXT_ADDR, msg->offset >> 16); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
sbi_status = pci_read_config16(dev, P2SB_CR_SBI_STATUS); |
| 315 |
sbi_status &= ~(P2SB_CR_SBI_OPCODE_MASK | P2SB_CR_SBI_POSTED_MASK); |
| 316 |
sbi_status |= (msg->opcode << P2SB_CR_SBI_OPCODE) | |
| 317 |
(msg->is_posted << P2SB_CR_SBI_POSTED); |
| 318 |
pci_write_config16(dev, P2SB_CR_SBI_STATUS, sbi_status); |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
sbi_rid = ((msg->fast_byte_enable & P2SB_CR_SBI_FBE_MASK) |
| 326 |
<< P2SB_CR_SBI_FBE) | |
| 327 |
((msg->bar & P2SB_CR_SBI_MASK) << P2SB_CR_SBI_BAR) | |
| 328 |
(msg->fid & P2SB_CR_SBI_FID_MASK); |
| 329 |
pci_write_config16(dev, P2SB_CR_SBI_ROUTE_IDEN, sbi_rid); |
| 330 |
|
| 331 |
switch (msg->opcode) { |
| 332 |
case MEM_WRITE: |
| 333 |
case PCI_CONFIG_WRITE: |
| 334 |
case PCR_WRITE: |
| 335 |
case GPIO_LOCK_UNLOCK: |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
sbi_data = *data; |
| 341 |
pci_write_config32(dev, P2SB_CR_SBI_DATA, sbi_data); |
| 342 |
break; |
| 343 |
default: |
| 344 |
|
| 345 |
pci_write_config32(dev, P2SB_CR_SBI_DATA, 0); |
| 346 |
break; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
sbi_status = pci_read_config16(dev, P2SB_CR_SBI_STATUS); |
| 355 |
sbi_status |= P2SB_CR_SBI_STATUS_BUSY; |
| 356 |
pci_write_config16(dev, P2SB_CR_SBI_STATUS, sbi_status); |
| 357 |
|
| 358 |
|
| 359 |
if (pcr_wait_for_completion(dev)) { |
| 360 |
printk(BIOS_ERR, "SBI Failure: Time Out!\n"); |
| 361 |
return -1; |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
*response = (sbi_status & P2SB_CR_SBI_STATUS_MASK) >> 1; |
| 369 |
if (*response == P2SB_CR_SBI_STATUS_SUCCESS) { |
| 370 |
switch (msg->opcode) { |
| 371 |
case MEM_READ: |
| 372 |
case PCI_CONFIG_READ: |
| 373 |
case PCR_READ: |
| 374 |
sbi_data = pci_read_config32(dev, P2SB_CR_SBI_DATA); |
| 375 |
*data = sbi_data; |
| 376 |
break; |
| 377 |
default: |
| 378 |
break; |
| 379 |
} |
| 380 |
return 0; |
| 381 |
} |
| 382 |
printk(BIOS_ERR, "SBI Failure: Transaction Status = %x\n", |
| 383 |
*response); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
#endif |