1 // SPDX-License-Identifier: MIT
3 * Copyright © 2024 Intel Corporation
10 #include "intel_uncore.h"
18 struct intel_uncore *uncore;
23 u32 (*read32)(struct intel_rom *rom, loff_t offset);
24 u16 (*read16)(struct intel_rom *rom, loff_t offset);
25 void (*read_block)(struct intel_rom *rom, void *data, loff_t offset, size_t size);
26 void (*free)(struct intel_rom *rom);
29 static u32 spi_read32(struct intel_rom *rom, loff_t offset)
31 intel_uncore_write(rom->uncore, PRIMARY_SPI_ADDRESS,
32 rom->offset + offset);
34 return intel_uncore_read(rom->uncore, PRIMARY_SPI_TRIGGER);
37 static u16 spi_read16(struct intel_rom *rom, loff_t offset)
39 return spi_read32(rom, offset) & 0xffff;
42 struct intel_rom *intel_rom_spi(struct drm_i915_private *i915)
44 struct intel_rom *rom;
47 rom = kzalloc(sizeof(*rom), GFP_KERNEL);
51 rom->uncore = &i915->uncore;
53 static_region = intel_uncore_read(rom->uncore, SPI_STATIC_REGIONS);
54 static_region &= OPTIONROM_SPI_REGIONID_MASK;
55 intel_uncore_write(rom->uncore, PRIMARY_SPI_REGIONID, static_region);
57 rom->offset = intel_uncore_read(rom->uncore, OROM_OFFSET) & OROM_OFFSET_MASK;
61 rom->read32 = spi_read32;
62 rom->read16 = spi_read16;
67 static u32 pci_read32(struct intel_rom *rom, loff_t offset)
69 return ioread32(rom->oprom + offset);
72 static u16 pci_read16(struct intel_rom *rom, loff_t offset)
74 return ioread16(rom->oprom + offset);
77 static void pci_read_block(struct intel_rom *rom, void *data,
78 loff_t offset, size_t size)
80 memcpy_fromio(data, rom->oprom + offset, size);
83 static void pci_free(struct intel_rom *rom)
85 pci_unmap_rom(rom->pdev, rom->oprom);
88 struct intel_rom *intel_rom_pci(struct drm_i915_private *i915)
90 struct intel_rom *rom;
92 rom = kzalloc(sizeof(*rom), GFP_KERNEL);
96 rom->pdev = to_pci_dev(i915->drm.dev);
98 rom->oprom = pci_map_rom(rom->pdev, &rom->size);
104 rom->read32 = pci_read32;
105 rom->read16 = pci_read16;
106 rom->read_block = pci_read_block;
107 rom->free = pci_free;
112 u32 intel_rom_read32(struct intel_rom *rom, loff_t offset)
114 return rom->read32(rom, offset);
117 u16 intel_rom_read16(struct intel_rom *rom, loff_t offset)
119 return rom->read16(rom, offset);
122 void intel_rom_read_block(struct intel_rom *rom, void *data,
123 loff_t offset, size_t size)
128 if (rom->read_block) {
129 rom->read_block(rom, data, offset, size);
133 for (index = 0; index < size; index += 4)
134 *ptr++ = rom->read32(rom, offset + index);
137 loff_t intel_rom_find(struct intel_rom *rom, u32 needle)
141 for (offset = 0; offset < rom->size; offset += 4) {
142 if (rom->read32(rom, offset) == needle)
149 size_t intel_rom_size(struct intel_rom *rom)
154 void intel_rom_free(struct intel_rom *rom)
156 if (rom && rom->free)