1 /* This is just an experiment. Full automatic porting
2 is probably not possible but a lot can be automated. */
22 type PCIDevData
struct {
29 type PCIDevice
interface {
30 Scan(ctx Context
, addr PCIDevData
)
33 type InteltoolData
struct {
34 GPIO
map[uint16]uint32
35 RCBA
map[uint16]uint32
36 IOBP
map[uint32]uint32
38 MCHBAR
map[uint16]uint32
39 PMBASE
map[uint16]uint32
49 type AzaliaCodec
struct {
54 PinConfig
map[int]uint32
57 type DevReader
interface {
58 GetPCIList() []PCIDevData
60 GetInteltool() InteltoolData
61 GetAzaliaCodecs() []AzaliaCodec
62 GetACPI() map[string][]byte
63 GetCPUModel() []uint32
65 GetIOPorts() []IOPorts
75 type SouthBridger
interface {
76 GetGPIOHeader() string
80 NeedRouteGPIOManually()
83 var SouthBridge SouthBridger
84 var BootBlockFiles
map[string]string = map[string]string{}
85 var ROMStageFiles
map[string]string = map[string]string{}
86 var RAMStageFiles
map[string]string = map[string]string{}
87 var SMMFiles
map[string]string = map[string]string{}
88 var MainboardInit
string
89 var MainboardEnable
string
90 var MainboardIncludes
[]string
102 var KconfigBool
map[string]bool = map[string]bool{}
103 var KconfigComment
map[string]string = map[string]string{}
104 var KconfigString
map[string]string = map[string]string{}
105 var KconfigHex
map[string]uint32 = map[string]uint32{}
106 var KconfigInt
map[string]int = map[string]int{}
109 var FlashROMSupport
= ""
111 func GetLE16(inp
[]byte) uint16 {
112 return uint16(inp
[0]) |
(uint16(inp
[1]) << 8)
115 func FormatHexLE16(inp
[]byte) string {
116 return fmt
.Sprintf("0x%04x", GetLE16(inp
))
119 func FormatHex32(u
uint32) string {
120 return fmt
.Sprintf("0x%08x", u
)
123 func FormatHex8(u
uint8) string {
124 return fmt
.Sprintf("0x%02x", u
)
127 func FormatInt32(u
uint32) string {
128 return fmt
.Sprintf("%d", u
)
131 func FormatHexLE32(d
[]uint8) string {
132 u
:= uint32(d
[0]) |
(uint32(d
[1]) << 8) |
(uint32(d
[2]) << 16) |
(uint32(d
[3]) << 24)
133 return FormatHex32(u
)
136 func FormatBool(inp
bool) string {
144 func sanitize(inp
string) string {
145 result
:= strings
.ToLower(inp
)
146 result
= strings
.Replace(result
, " ", "_", -1)
147 result
= strings
.Replace(result
, ",", "_", -1)
148 result
= strings
.Replace(result
, "-", "_", -1)
149 for strings
.HasSuffix(result
, ".") {
150 result
= result
[0 : len(result
)-1]
155 func AddBootBlockFile(Name
string, Condition
string) {
156 BootBlockFiles
[Name
] = Condition
159 func AddROMStageFile(Name
string, Condition
string) {
160 ROMStageFiles
[Name
] = Condition
163 func AddRAMStageFile(Name
string, Condition
string) {
164 RAMStageFiles
[Name
] = Condition
167 func AddSMMFile(Name
string, Condition
string) {
168 SMMFiles
[Name
] = Condition
171 func IsIOPortUsedBy(ctx Context
, port
uint16, name
string) bool {
172 for _
, io
:= range ctx
.InfoSource
.GetIOPorts() {
173 if io
.Start
<= port
&& port
<= io
.End
&& io
.Usage
== name
{
180 var FlagOutDir
= flag
.String("coreboot_dir", ".", "Resulting coreboot directory")
182 func writeMF(mf
*os
.File
, files
map[string]string, category
string) {
184 for file
, _
:= range files
{
185 keys
= append(keys
, file
)
190 for _
, file
:= range keys
{
191 condition
:= files
[file
]
193 fmt
.Fprintf(mf
, "%s-y += %s\n", category
, file
)
195 fmt
.Fprintf(mf
, "%s-$(%s) += %s\n", category
, condition
, file
)
200 func Create(ctx Context
, name
string) *os
.File
{
201 li
:= strings
.LastIndex(name
, "/")
203 os
.MkdirAll(ctx
.BaseDirectory
+"/"+name
[0:li
], 0700)
205 mf
, err
:= os
.Create(ctx
.BaseDirectory
+ "/" + name
)
212 func Add_gpl(f
*os
.File
) {
213 fmt
.Fprintln(f
, "/* SPDX-License-Identifier: GPL-2.0-only */")
217 func RestorePCI16Simple(f
*os
.File
, pcidev PCIDevData
, addr
uint16) {
218 fmt
.Fprintf(f
, " pci_write_config16(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x);\n",
219 pcidev
.Bus
, pcidev
.Dev
, pcidev
.Func
, addr
,
220 pcidev
.ConfigDump
[addr
+1],
221 pcidev
.ConfigDump
[addr
])
224 func RestorePCI32Simple(f
*os
.File
, pcidev PCIDevData
, addr
uint16) {
225 fmt
.Fprintf(f
, " pci_write_config32(PCI_DEV(%d, 0x%02x, %d), 0x%02x, 0x%02x%02x%02x%02x);\n",
226 pcidev
.Bus
, pcidev
.Dev
, pcidev
.Func
, addr
,
227 pcidev
.ConfigDump
[addr
+3],
228 pcidev
.ConfigDump
[addr
+2],
229 pcidev
.ConfigDump
[addr
+1],
230 pcidev
.ConfigDump
[addr
])
233 func RestoreRCBA32(f
*os
.File
, inteltool InteltoolData
, addr
uint16) {
234 fmt
.Fprintf(f
, "\tRCBA32(0x%04x) = 0x%08x;\n", addr
, inteltool
.RCBA
[addr
])
237 type PCISlot
struct {
240 additionalComment
string
244 type DevTreeNode
struct {
249 Registers
map[string]string
250 IOs
map[uint16]uint16
251 Children
[]DevTreeNode
263 var DevTree DevTreeNode
264 var MissingChildren
map[string][]DevTreeNode
= map[string][]DevTreeNode
{}
265 var unmatchedPCIChips
map[PCIAddr
]DevTreeNode
= map[PCIAddr
]DevTreeNode
{}
266 var unmatchedPCIDevices
map[PCIAddr
]DevTreeNode
= map[PCIAddr
]DevTreeNode
{}
268 func Offset(dt
*os
.File
, offset
int) {
269 for i
:= 0; i
< offset
; i
++ {
270 fmt
.Fprintf(dt
, "\t")
274 func MatchDev(dev
*DevTreeNode
) {
275 for idx
:= range dev
.Children
{
276 MatchDev(&dev
.Children
[idx
])
279 for _
, slot
:= range dev
.PCISlots
{
280 slotDev
, ok
:= unmatchedPCIDevices
[slot
.PCIAddr
]
283 dev
.Children
= append(dev
.Children
,
285 Registers
: map[string]string{},
290 Comment
: slot
.additionalComment
,
298 if slot
.additionalComment
!= "" && slotDev
.Comment
!= "" {
299 slotDev
.Comment
= slot
.additionalComment
+ " " + slotDev
.Comment
301 slotDev
.Comment
= slot
.additionalComment
+ slotDev
.Comment
305 dev
.Children
= append(dev
.Children
, slotDev
)
306 delete(unmatchedPCIDevices
, slot
.PCIAddr
)
309 for _
, slot
:= range dev
.PCISlots
{
310 slotChip
, ok
:= unmatchedPCIChips
[slot
.PCIAddr
]
316 if slot
.additionalComment
!= "" && slotChip
.Comment
!= "" {
317 slotChip
.Comment
= slot
.additionalComment
+ " " + slotChip
.Comment
319 slotChip
.Comment
= slot
.additionalComment
+ slotChip
.Comment
322 delete(unmatchedPCIChips
, slot
.PCIAddr
)
324 dev
.Children
= append(dev
.Children
, slotChip
)
327 if dev
.PCIController
{
328 for slot
, slotDev
:= range unmatchedPCIChips
{
329 if slot
.Bus
== dev
.ChildPCIBus
{
330 delete(unmatchedPCIChips
, slot
)
332 dev
.Children
= append(dev
.Children
, slotDev
)
337 if dev
.MissingParent
!= "" {
338 for _
, child
:= range MissingChildren
[dev
.MissingParent
] {
340 dev
.Children
= append(dev
.Children
, child
)
342 delete(MissingChildren
, dev
.MissingParent
)
345 if dev
.PCIController
{
346 for slot
, slotDev
:= range unmatchedPCIDevices
{
347 if slot
.Bus
== dev
.ChildPCIBus
{
349 dev
.Children
= append(dev
.Children
, slotDev
)
350 delete(unmatchedPCIDevices
, slot
)
356 func writeOn(dt
*os
.File
, dev DevTreeNode
) {
358 fmt
.Fprintf(dt
, "off")
360 fmt
.Fprintf(dt
, "on")
364 func WriteDev(dt
*os
.File
, offset
int, alias
string, dev DevTreeNode
) {
367 case "cpu_cluster", "domain":
368 fmt
.Fprintf(dt
, "device %s 0x%x ", dev
.Chip
, dev
.Dev
)
372 fmt
.Fprintf(dt
, "device ref %s ", alias
)
374 fmt
.Fprintf(dt
, "device %s %02x.%x ", dev
.Chip
, dev
.Dev
, dev
.Func
)
378 fmt
.Fprintf(dt
, "device %s %02x ", dev
.Chip
, dev
.Dev
)
381 fmt
.Fprintf(dt
, "chip %s", dev
.Chip
)
383 if dev
.Comment
!= "" {
384 fmt
.Fprintf(dt
, " # %s", dev
.Comment
)
386 fmt
.Fprintf(dt
, "\n")
389 fmt
.Fprintf(dt
, "ops %s\n", dev
.Ops
)
391 if dev
.Chip
== "pci" && dev
.SubSystem
!= 0 && dev
.SubVendor
!= 0 {
393 fmt
.Fprintf(dt
, "subsystemid 0x%04x 0x%04x\n", dev
.SubVendor
, dev
.SubSystem
)
397 for reg
, _
:= range dev
.Registers
{
398 keys
= append(keys
, reg
)
403 for _
, reg
:= range keys
{
404 val
:= dev
.Registers
[reg
]
406 fmt
.Fprintf(dt
, "register \"%s\" = \"%s\"\n", reg
, val
)
410 for reg
, _
:= range dev
.IOs
{
411 ios
= append(ios
, int(reg
))
416 for _
, reg
:= range ios
{
417 val
:= dev
.IOs
[uint16(reg
)]
419 fmt
.Fprintf(dt
, "io 0x%x = 0x%x\n", reg
, val
)
422 for _
, child
:= range dev
.Children
{
424 for _
, slot
:= range dev
.PCISlots
{
425 if slot
.PCIAddr
.Bus
== child
.Bus
&&
426 slot
.PCIAddr
.Dev
== child
.Dev
&& slot
.PCIAddr
.Func
== child
.Func
{
430 WriteDev(dt
, offset
+1, alias
, child
)
434 fmt
.Fprintf(dt
, "end\n")
437 func PutChip(domain
string, cur DevTreeNode
) {
438 MissingChildren
[domain
] = append(MissingChildren
[domain
], cur
)
441 func PutPCIChip(addr PCIDevData
, cur DevTreeNode
) {
442 unmatchedPCIChips
[addr
.PCIAddr
] = cur
445 func PutPCIDevParent(addr PCIDevData
, comment
string, parent
string) {
447 Registers
: map[string]string{},
452 MissingParent
: parent
,
455 if addr
.ConfigDump
[0xa] == 0x04 && addr
.ConfigDump
[0xb] == 0x06 {
456 cur
.PCIController
= true
457 cur
.ChildPCIBus
= int(addr
.ConfigDump
[0x19])
460 for capPtr
:= addr
.ConfigDump
[0x34]; capPtr
!= 0; capPtr
= addr
.ConfigDump
[capPtr
+1] {
461 /* Avoid hangs. There are only 0x100 different possible values for capPtr.
462 If we iterate longer than that, we're in endless loop. */
467 if addr
.ConfigDump
[capPtr
] == 0x0d {
468 cur
.SubVendor
= GetLE16(addr
.ConfigDump
[capPtr
+4 : capPtr
+6])
469 cur
.SubSystem
= GetLE16(addr
.ConfigDump
[capPtr
+6 : capPtr
+8])
473 cur
.SubVendor
= GetLE16(addr
.ConfigDump
[0x2c:0x2e])
474 cur
.SubSystem
= GetLE16(addr
.ConfigDump
[0x2e:0x30])
476 unmatchedPCIDevices
[addr
.PCIAddr
] = cur
479 func PutPCIDev(addr PCIDevData
, comment
string) {
480 PutPCIDevParent(addr
, comment
, "")
483 type GenericPCI
struct {
489 type GenericVGA
struct {
493 type DSDTInclude
struct {
498 type DSDTDefine
struct {
504 var DSDTIncludes
[]DSDTInclude
505 var DSDTPCI0Includes
[]DSDTInclude
506 var DSDTDefines
[]DSDTDefine
508 func (g GenericPCI
) Scan(ctx Context
, addr PCIDevData
) {
509 PutPCIDevParent(addr
, g
.Comment
, g
.MissingParent
)
512 var IGDEnabled
bool = false
514 func (g GenericVGA
) Scan(ctx Context
, addr PCIDevData
) {
515 KconfigString
["VGA_BIOS_ID"] = fmt
.Sprintf("%04x,%04x",
518 PutPCIDevParent(addr
, g
.Comment
, g
.MissingParent
)
522 func makeKconfigName(ctx Context
) {
523 kn
:= Create(ctx
, "Kconfig.name")
526 fmt
.Fprintf(kn
, "config %s\n\tbool \"%s\"\n", ctx
.KconfigName
, ctx
.Model
)
529 func makeComment(name
string) string {
530 cmt
, ok
:= KconfigComment
[name
]
537 func makeKconfig(ctx Context
) {
538 kc
:= Create(ctx
, "Kconfig")
541 fmt
.Fprintf(kc
, "if %s\n\n", ctx
.KconfigName
)
543 fmt
.Fprintf(kc
, "config BOARD_SPECIFIC_OPTIONS\n\tdef_bool y\n")
545 for name
, val
:= range KconfigBool
{
547 keys
= append(keys
, name
)
553 for _
, name
:= range keys
{
554 fmt
.Fprintf(kc
, "\tselect %s%s\n", name
, makeComment(name
))
558 for name
, val
:= range KconfigBool
{
560 keys
= append(keys
, name
)
566 for _
, name
:= range keys
{
571 `, name
, makeComment(name
))
575 for name
, _
:= range KconfigString
{
576 keys
= append(keys
, name
)
581 for _
, name
:= range keys
{
586 `, name
, makeComment(name
), KconfigString
[name
])
590 for name
, _
:= range KconfigHex
{
591 keys
= append(keys
, name
)
596 for _
, name
:= range keys
{
601 `, name
, makeComment(name
), KconfigHex
[name
])
605 for name
, _
:= range KconfigInt
{
606 keys
= append(keys
, name
)
611 for _
, name
:= range keys
{
616 `, name
, makeComment(name
), KconfigInt
[name
])
619 fmt
.Fprintf(kc
, "endif\n")
622 const MoboDir
= "/src/mainboard/"
624 func makeVendor(ctx Context
) {
626 vendorSane
:= ctx
.SaneVendor
627 vendorDir
:= *FlagOutDir
+ MoboDir
+ vendorSane
628 vendorUpper
:= strings
.ToUpper(vendorSane
)
629 kconfig
:= vendorDir
+ "/Kconfig"
630 if _
, err
:= os
.Stat(kconfig
); os
.IsNotExist(err
) {
631 f
, err
:= os
.Create(kconfig
)
636 f
.WriteString(`if VENDOR_` + vendorUpper
+ `
639 prompt "Mainboard model"
641 source "src/mainboard/` + vendorSane
+ `/*/Kconfig.name"
645 source "src/mainboard/` + vendorSane
+ `/*/Kconfig"
647 config MAINBOARD_VENDOR
649 default "` + vendor
+ `"
651 endif # VENDOR_` + vendorUpper
+ "\n")
653 kconfigName
:= vendorDir
+ "/Kconfig.name"
654 if _
, err
:= os
.Stat(kconfigName
); os
.IsNotExist(err
) {
655 f
, err
:= os
.Create(kconfigName
)
660 f
.WriteString(`config VENDOR_` + vendorUpper
+ `
661 bool "` + vendor
+ `"
667 func GuessECGPE(ctx Context
) int {
668 /* FIXME:XX Use iasl -d and/or better parsing */
669 dsdt
:= ctx
.InfoSource
.GetACPI()["DSDT"]
670 idx
:= bytes
.Index(dsdt
, []byte{0x08, '_', 'G', 'P', 'E', 0x0a}) /* Name (_GPE, byte). */
672 return int(dsdt
[idx
+6])
677 func GuessSPDMap(ctx Context
) []uint8 {
678 dmi
:= ctx
.InfoSource
.GetDMI()
680 if dmi
.Vendor
== "LENOVO" {
681 return []uint8{0x50, 0x52, 0x51, 0x53}
683 return []uint8{0x50, 0x51, 0x52, 0x53}
691 ctx
.InfoSource
= MakeLogReader()
693 dmi
:= ctx
.InfoSource
.GetDMI()
695 ctx
.Vendor
= dmi
.Vendor
697 if dmi
.Vendor
== "LENOVO" {
698 ctx
.Model
= dmi
.Version
700 ctx
.Model
= dmi
.Model
704 KconfigBool
["SYSTEM_TYPE_LAPTOP"] = true
706 ctx
.SaneVendor
= sanitize(ctx
.Vendor
)
708 last
:= ctx
.SaneVendor
709 for _
, suf
:= range []string{"_inc", "_co", "_corp"} {
710 ctx
.SaneVendor
= strings
.TrimSuffix(ctx
.SaneVendor
, suf
)
712 if last
== ctx
.SaneVendor
{
716 ctx
.MoboID
= ctx
.SaneVendor
+ "/" + sanitize(ctx
.Model
)
717 ctx
.KconfigName
= "BOARD_" + strings
.ToUpper(ctx
.SaneVendor
+"_"+sanitize(ctx
.Model
))
718 ctx
.BaseDirectory
= *FlagOutDir
+ MoboDir
+ ctx
.MoboID
719 KconfigString
["MAINBOARD_DIR"] = ctx
.MoboID
720 KconfigString
["MAINBOARD_PART_NUMBER"] = ctx
.Model
722 os
.MkdirAll(ctx
.BaseDirectory
, 0700)
729 KconfigBool
["MAINBOARD_HAS_LIBGFXINIT"] = true
730 KconfigComment
["MAINBOARD_HAS_LIBGFXINIT"] = "FIXME: check this"
731 AddRAMStageFile("gma-mainboard.ads", "CONFIG_MAINBOARD_USE_LIBGFXINIT")
734 if len(BootBlockFiles
) > 0 ||
len(ROMStageFiles
) > 0 ||
len(RAMStageFiles
) > 0 ||
len(SMMFiles
) > 0 {
735 mf
:= Create(ctx
, "Makefile.mk")
737 writeMF(mf
, BootBlockFiles
, "bootblock")
738 writeMF(mf
, ROMStageFiles
, "romstage")
739 writeMF(mf
, RAMStageFiles
, "ramstage")
740 writeMF(mf
, SMMFiles
, "smm")
743 devtree
:= Create(ctx
, "devicetree.cb")
744 defer devtree
.Close()
747 WriteDev(devtree
, 0, "", DevTree
)
749 if MainboardInit
!= "" || MainboardEnable
!= "" || MainboardIncludes
!= nil {
750 mainboard
:= Create(ctx
, "mainboard.c")
751 defer mainboard
.Close()
753 mainboard
.WriteString("#include <device/device.h>\n")
754 for _
, include
:= range MainboardIncludes
{
755 mainboard
.WriteString("#include <" + include
+ ">\n")
757 mainboard
.WriteString("\n")
758 if MainboardInit
!= "" {
759 mainboard
.WriteString(`static void mainboard_init(struct device *dev)
761 ` + MainboardInit
+ "}\n\n")
763 if MainboardInit
!= "" || MainboardEnable
!= "" {
764 mainboard
.WriteString("static void mainboard_enable(struct device *dev)\n{\n")
765 if MainboardInit
!= "" {
766 mainboard
.WriteString("\tdev->ops->init = mainboard_init;\n\n")
768 mainboard
.WriteString(MainboardEnable
)
769 mainboard
.WriteString("}\n\n")
770 mainboard
.WriteString(`struct chip_operations mainboard_ops = {
771 .enable_dev = mainboard_enable,
777 bi
:= Create(ctx
, "board_info.txt")
783 bi
.WriteString("Category: laptop\n")
785 bi
.WriteString("Category: desktop\n")
786 fixme
+= "check category, "
789 missing
:= "ROM package, ROM socketed"
791 if ROMProtocol
!= "" {
792 fmt
.Fprintf(bi
, "ROM protocol: %s\n", ROMProtocol
)
794 missing
+= ", ROM protocol"
797 if FlashROMSupport
!= "" {
798 fmt
.Fprintf(bi
, "Flashrom support: %s\n", FlashROMSupport
)
800 missing
+= ", Flashrom support"
803 missing
+= ", Release year"
806 fmt
.Fprintf(bi
, "FIXME: %s, put %s\n", fixme
, missing
)
808 fmt
.Fprintf(bi
, "FIXME: put %s\n", missing
)
812 KconfigBool
["BOARD_ROMSIZE_KB_2048"] = true
813 KconfigComment
["BOARD_ROMSIZE_KB_2048"] = "FIXME: correct this"
815 KconfigBool
[fmt
.Sprintf("BOARD_ROMSIZE_KB_%d", ROMSizeKB
)] = true
821 dsdt
:= Create(ctx
, "dsdt.asl")
825 for _
, define
:= range DSDTDefines
{
826 if define
.Comment
!= "" {
827 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", define
.Comment
)
829 dsdt
.WriteString("#define " + define
.Key
+ " " + define
.Value
+ "\n")
833 `#include <acpi/acpi.h>
844 #include <acpi/dsdt_top.asl>
845 #include "acpi/platform.asl"
848 for _
, x
:= range DSDTIncludes
{
850 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", x
.Comment
)
852 fmt
.Fprintf(dsdt
, "\t#include <%s>\n", x
.File
)
859 for _
, x
:= range DSDTPCI0Includes
{
861 fmt
.Fprintf(dsdt
, "\t/* %s. */\n", x
.Comment
)
863 fmt
.Fprintf(dsdt
, "\t\t#include <%s>\n", x
.File
)
871 gma
:= Create(ctx
, "gma-mainboard.ads")
874 gma
.WriteString(`-- SPDX-License-Identifier: GPL-2.0-or-later
877 with HW.GFX.GMA.Display_Probing;
880 use HW.GFX.GMA.Display_Probing;
882 private package GMA.Mainboard is
885 ports : constant Port_List :=
899 outputPath
, _
:= filepath
.Abs(ctx
.BaseDirectory
)
900 fmt
.Printf("Done! Generated sources are in %s\n", outputPath
)