2019-09-01 22:35:37 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2020-08-23 09:00:17 -04:00
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019 Sean Cross <sean@xobs.io>
|
|
|
|
# Copyright (c) 2018 David Shah <dave@ds0.me>
|
2020-10-06 05:39:10 -04:00
|
|
|
# Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
|
2020-08-23 09:00:17 -04:00
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-05-05 09:11:38 -04:00
|
|
|
import os
|
2020-10-06 05:39:10 -04:00
|
|
|
import sys
|
2019-09-01 22:35:37 -04:00
|
|
|
|
|
|
|
from migen import *
|
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
from litex_boards.platforms import fomu_pvt
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2021-03-24 10:03:48 -04:00
|
|
|
from litex.soc.cores.ram import Up5kSPRAM
|
2020-10-06 05:39:10 -04:00
|
|
|
from litex.soc.cores.clock import iCE40PLL
|
|
|
|
from litex.soc.integration.soc_core import *
|
|
|
|
from litex.soc.integration.soc import SoCRegion
|
|
|
|
from litex.soc.integration.builder import *
|
|
|
|
from litex.soc.cores.led import LedChaser
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
kB = 1024
|
|
|
|
mB = 1024*kB
|
2019-09-01 22:35:37 -04:00
|
|
|
|
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
class _CRG(Module):
|
|
|
|
def __init__(self, platform, sys_clk_freq):
|
|
|
|
assert sys_clk_freq == 12e6
|
2020-11-04 05:09:30 -05:00
|
|
|
self.rst = Signal()
|
2020-10-06 05:39:10 -04:00
|
|
|
self.clock_domains.cd_sys = ClockDomain()
|
2022-04-01 05:30:38 -04:00
|
|
|
self.clock_domains.cd_por = ClockDomain()
|
2019-09-01 22:35:37 -04:00
|
|
|
self.clock_domains.cd_usb_12 = ClockDomain()
|
|
|
|
self.clock_domains.cd_usb_48 = ClockDomain()
|
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# # #
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# Clk/Rst
|
|
|
|
clk48 = platform.request("clk48")
|
|
|
|
platform.add_period_constraint(clk48, 1e9/48e6)
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# Power On Reset
|
|
|
|
por_count = Signal(16, reset=2**16-1)
|
|
|
|
por_done = Signal()
|
|
|
|
self.comb += self.cd_por.clk.eq(ClockSignal())
|
|
|
|
self.comb += por_done.eq(por_count == 0)
|
|
|
|
self.sync.por += If(~por_done, por_count.eq(por_count - 1))
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# USB PLL
|
|
|
|
self.submodules.pll = pll = iCE40PLL()
|
2021-05-20 03:14:54 -04:00
|
|
|
#self.comb += pll.reset.eq(self.rst) # FIXME: Add proper iCE40PLL reset support and add back | self.rst.
|
2020-10-06 05:39:10 -04:00
|
|
|
pll.clko_freq_range = ( 12e6, 275e9) # FIXME: improve iCE40PLL to avoid lowering clko_freq_min.
|
|
|
|
pll.register_clkin(clk48, 48e6)
|
|
|
|
pll.create_clkout(self.cd_usb_12, 12e6, with_reset=False)
|
|
|
|
self.comb += self.cd_usb_48.clk.eq(clk48)
|
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_usb_12, ~por_done | ~pll.locked)
|
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_usb_48, ~por_done | ~pll.locked)
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# Sys Clk
|
|
|
|
self.comb += self.cd_sys.clk.eq(self.cd_usb_12.clk)
|
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_sys, ~por_done | ~pll.locked)
|
2019-09-01 22:35:37 -04:00
|
|
|
|
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2021-07-28 12:20:42 -04:00
|
|
|
def __init__(self, bios_flash_offset, spi_flash_module="AT25SF161", sys_clk_freq=int(12e6),
|
2021-07-28 11:34:02 -04:00
|
|
|
with_led_chaser=True, **kwargs):
|
2020-10-06 05:39:10 -04:00
|
|
|
kwargs["uart_name"] = "usb_acm" # Enforce UART to USB-ACM
|
|
|
|
platform = fomu_pvt.Platform()
|
2020-02-02 19:57:56 -05:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# Disable Integrated ROM/SRAM since too large for iCE40 and UP5K has specific SPRAM.
|
2020-01-13 11:00:01 -05:00
|
|
|
kwargs["integrated_sram_size"] = 0
|
2020-10-06 05:39:10 -04:00
|
|
|
kwargs["integrated_rom_size"] = 0
|
|
|
|
|
|
|
|
# Serial -----------------------------------------------------------------------------------
|
|
|
|
# FIXME: do proper install of ValentyUSB.
|
2020-11-27 12:53:45 -05:00
|
|
|
os.system("git clone https://github.com/litex-hub/valentyusb -b hw_cdc_eptri")
|
2020-10-06 05:39:10 -04:00
|
|
|
sys.path.append("valentyusb")
|
|
|
|
|
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq,
|
2022-01-18 11:13:02 -05:00
|
|
|
ident = "LiteX SoC on Fomu",
|
2020-10-06 05:39:10 -04:00
|
|
|
**kwargs)
|
|
|
|
|
|
|
|
# CRG --------------------------------------------------------------------------------------
|
|
|
|
self.submodules.crg = _CRG(platform, sys_clk_freq)
|
|
|
|
|
2021-09-29 13:33:22 -04:00
|
|
|
# 128KB SPRAM (used as 64kB SRAM / 64kB RAM) -----------------------------------------------
|
2020-10-06 05:39:10 -04:00
|
|
|
self.submodules.spram = Up5kSPRAM(size=128*kB)
|
2021-09-30 03:32:26 -04:00
|
|
|
self.bus.add_slave("psram", self.spram.bus, SoCRegion(size=128*kB))
|
|
|
|
self.bus.add_region("sram", SoCRegion(
|
|
|
|
origin = self.bus.regions["psram"].origin + 0*kB,
|
|
|
|
size = 64*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2021-09-29 13:33:22 -04:00
|
|
|
if not self.integrated_main_ram_size:
|
|
|
|
self.bus.add_region("main_ram", SoCRegion(
|
2021-09-30 03:32:26 -04:00
|
|
|
origin = self.bus.regions["psram"].origin + 64*kB,
|
2021-09-29 13:33:22 -04:00
|
|
|
size = 64*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2020-10-06 05:39:10 -04:00
|
|
|
|
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
2021-07-28 11:34:02 -04:00
|
|
|
from litespi.modules import AT25SF161, GD25Q16C, MX25R1635F, W25Q128JV
|
2021-07-27 13:55:04 -04:00
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
2021-07-28 11:34:02 -04:00
|
|
|
|
|
|
|
# lambdas for lazy module instantiation.
|
2021-07-28 12:20:42 -04:00
|
|
|
spi_flash_modules = {
|
|
|
|
"AT25SF161": lambda: AT25SF161( Codes.READ_1_1_4),
|
|
|
|
"GD25Q16C": lambda: GD25Q16C( Codes.READ_1_1_1),
|
|
|
|
"MX25R1635F": lambda: MX25R1635F(Codes.READ_1_1_4),
|
|
|
|
"W25Q128JV": lambda: W25Q128JV( Codes.READ_1_1_4),
|
2021-07-28 11:34:02 -04:00
|
|
|
}
|
2021-07-28 12:20:42 -04:00
|
|
|
self.add_spi_flash(mode="4x", module=spi_flash_modules[spi_flash_module](), with_master=False)
|
2020-10-06 05:39:10 -04:00
|
|
|
|
|
|
|
# Add ROM linker region --------------------------------------------------------------------
|
|
|
|
self.bus.add_region("rom", SoCRegion(
|
2022-01-07 04:34:47 -05:00
|
|
|
origin = self.bus.regions["spiflash"].origin + bios_flash_offset,
|
2020-10-06 05:39:10 -04:00
|
|
|
size = 32*kB,
|
|
|
|
linker = True)
|
|
|
|
)
|
2022-01-07 09:19:23 -05:00
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
2019-09-17 05:08:05 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
# Leds -------------------------------------------------------------------------------------
|
2021-07-06 17:39:37 -04:00
|
|
|
if with_led_chaser:
|
|
|
|
self.submodules.leds = LedChaser(
|
|
|
|
pads = platform.request_all("user_led_n"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
2020-10-06 05:39:10 -04:00
|
|
|
|
|
|
|
# Flash --------------------------------------------------------------------------------------------
|
|
|
|
|
2021-05-20 03:14:54 -04:00
|
|
|
def flash(build_dir, build_name, bios_flash_offset):
|
2020-10-06 05:39:10 -04:00
|
|
|
from litex.build.dfu import DFUProg
|
|
|
|
prog = DFUProg(vid="1209", pid="5bf0")
|
2021-07-21 05:41:35 -04:00
|
|
|
bitstream = open(f"{build_dir}/gateware/{build_name}.bin", "rb")
|
|
|
|
bios = open(f"{build_dir}/software/bios/bios.bin", "rb")
|
|
|
|
image = open(f"{build_dir}/image.bin", "wb")
|
|
|
|
# Copy bitstream at 0.
|
|
|
|
assert bios_flash_offset >= 128*kB
|
|
|
|
for i in range(0, bios_flash_offset):
|
2020-10-06 05:39:10 -04:00
|
|
|
b = bitstream.read(1)
|
|
|
|
if not b:
|
|
|
|
image.write(0xff.to_bytes(1, "big"))
|
|
|
|
else:
|
|
|
|
image.write(b)
|
2021-07-21 05:41:35 -04:00
|
|
|
# Copy bios at bios_flash_offset.
|
|
|
|
for i in range(0, 32*kB):
|
2020-10-06 05:39:10 -04:00
|
|
|
b = bios.read(1)
|
|
|
|
if not b:
|
|
|
|
image.write(0xff.to_bytes(1, "big"))
|
|
|
|
else:
|
|
|
|
image.write(b)
|
|
|
|
bitstream.close()
|
|
|
|
bios.close()
|
|
|
|
image.close()
|
2021-05-20 03:14:54 -04:00
|
|
|
prog.load_bitstream(f"{build_dir}/image.bin")
|
2019-09-01 22:35:37 -04:00
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
2022-03-21 11:59:40 -04:00
|
|
|
from litex.soc.integration.soc import LiteXSoCArgumentParser
|
|
|
|
parser = LiteXSoCArgumentParser(description="LiteX SoC on Fomu")
|
2022-03-21 13:30:10 -04:00
|
|
|
target_group = parser.add_argument_group(title="Target options")
|
|
|
|
target_group.add_argument("--build", action="store_true", help="Build bitstream.")
|
|
|
|
target_group.add_argument("--sys-clk-freq", default=12e6, help="System clock frequency.")
|
|
|
|
target_group.add_argument("--bios-flash-offset", default="0x20000", help="BIOS offset in SPI Flash.")
|
|
|
|
target_group.add_argument("--flash", action="store_true", help="Flash Bitstream.")
|
2019-09-01 22:35:37 -04:00
|
|
|
builder_args(parser)
|
|
|
|
soc_core_args(parser)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-07-21 05:41:35 -04:00
|
|
|
dfu_flash_offset = 0x40000
|
|
|
|
|
2020-11-12 12:07:28 -05:00
|
|
|
soc = BaseSoC(
|
2021-12-20 15:41:12 -05:00
|
|
|
bios_flash_offset = dfu_flash_offset + int(args.bios_flash_offset, 0),
|
2020-11-12 12:07:28 -05:00
|
|
|
sys_clk_freq = int(float(args.sys_clk_freq)),
|
|
|
|
**soc_core_argdict(args)
|
|
|
|
)
|
2019-09-01 22:35:37 -04:00
|
|
|
builder = Builder(soc, **builder_argdict(args))
|
2020-05-05 09:11:38 -04:00
|
|
|
builder.build(run=args.build)
|
2019-09-01 22:35:37 -04:00
|
|
|
|
2020-10-06 05:39:10 -04:00
|
|
|
if args.flash:
|
2021-12-20 15:41:12 -05:00
|
|
|
flash(builder.output_dir, soc.build_name, int(args.bios_flash_offset, 0))
|
2020-10-06 05:39:10 -04:00
|
|
|
|
2019-09-01 22:35:37 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|