2020-09-30 08:01:36 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
#
|
2021-02-01 07:15:03 -05:00
|
|
|
# This file is part of LiteX-Boards.
|
2020-09-30 08:01:36 -04:00
|
|
|
#
|
2021-02-01 07:15:03 -05:00
|
|
|
# Copyright (c) 2020 Pepijn de Vos <pepijndevos@gmail.com>
|
|
|
|
# Copyright (c) 2021 Florent Kermarrec <florent@enjoy-digital.fr>
|
2020-09-30 08:01:36 -04:00
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
|
|
from migen import *
|
2021-07-14 04:43:26 -04:00
|
|
|
from migen.genlib.resetsync import AsyncResetSynchronizer
|
2020-09-30 08:01:36 -04:00
|
|
|
|
2023-02-23 03:09:33 -05:00
|
|
|
from litex.gen import *
|
2022-10-27 10:58:55 -04:00
|
|
|
|
2022-05-02 06:42:04 -04:00
|
|
|
from litex_boards.platforms import trenz_tec0117
|
|
|
|
|
2021-07-14 04:02:58 -04:00
|
|
|
from litex.build.io import DDROutput
|
2020-09-30 08:01:36 -04:00
|
|
|
|
2021-04-30 05:32:24 -04:00
|
|
|
from litex.soc.cores.clock.gowin_gw1n import GW1NPLL
|
2020-09-30 08:01:36 -04:00
|
|
|
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
|
|
|
|
|
2021-02-01 07:22:57 -05:00
|
|
|
from litedram.modules import MT48LC4M16 # FIXME: use EtronTech reference.
|
2021-01-29 16:28:31 -05:00
|
|
|
from litedram.phy import GENSDRPHY, HalfRateGENSDRPHY
|
|
|
|
|
2021-02-01 07:22:57 -05:00
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
class _CRG(LiteXModule):
|
2024-10-04 09:25:39 -04:00
|
|
|
def __init__(self, platform, sys_clk_freq, sdram_rate):
|
2022-10-27 10:58:55 -04:00
|
|
|
self.rst = Signal()
|
|
|
|
self.cd_sys = ClockDomain()
|
|
|
|
self.cd_sys2x = ClockDomain()
|
2021-02-01 07:22:57 -05:00
|
|
|
|
|
|
|
# # #
|
|
|
|
|
|
|
|
# Clk / Rst
|
|
|
|
clk100 = platform.request("clk100")
|
|
|
|
rst_n = platform.request("rst_n")
|
|
|
|
|
2021-04-30 05:32:24 -04:00
|
|
|
# PLL
|
2022-10-27 10:58:55 -04:00
|
|
|
self.pll = pll = GW1NPLL(devicename=platform.devicename, device=platform.device)
|
2021-04-30 05:32:24 -04:00
|
|
|
self.comb += pll.reset.eq(~rst_n)
|
|
|
|
pll.register_clkin(clk100, 100e6)
|
2024-10-04 09:25:39 -04:00
|
|
|
if sdram_rate == "1:2":
|
|
|
|
pll.create_clkout(self.cd_sys2x, 2*sys_clk_freq, with_reset=False)
|
|
|
|
self.specials += Instance("CLKDIV",
|
|
|
|
p_DIV_MODE= "2",
|
|
|
|
i_RESETN = rst_n,
|
|
|
|
i_HCLKIN = self.cd_sys2x.clk,
|
|
|
|
o_CLKOUT = self.cd_sys.clk
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq, with_reset=False)
|
2021-07-14 04:43:26 -04:00
|
|
|
self.specials += AsyncResetSynchronizer(self.cd_sys, ~rst_n)
|
2021-02-01 07:22:57 -05:00
|
|
|
|
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
2020-09-30 08:01:36 -04:00
|
|
|
class BaseSoC(SoCCore):
|
2022-11-08 05:54:17 -05:00
|
|
|
def __init__(self, bios_flash_offset=0x0000, sys_clk_freq=25e6, sdram_rate="1:1",
|
2024-10-04 09:25:39 -04:00
|
|
|
with_led_chaser = True, toolchain="gowin",
|
2022-11-08 06:29:11 -05:00
|
|
|
**kwargs):
|
2024-10-04 09:25:39 -04:00
|
|
|
platform = trenz_tec0117.Platform(toolchain=toolchain)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
|
|
|
# CRG --------------------------------------------------------------------------------------
|
2024-10-04 09:25:39 -04:00
|
|
|
self.crg = _CRG(platform, sys_clk_freq, sdram_rate)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
|
|
|
# Disable Integrated ROM.
|
|
|
|
kwargs["integrated_rom_size"] = 0
|
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on TEC0117", **kwargs)
|
|
|
|
|
2020-09-30 08:01:36 -04:00
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
2021-07-28 04:34:17 -04:00
|
|
|
from litespi.modules import W74M64FV
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
|
|
|
self.add_spi_flash(mode="4x", module=W74M64FV(Codes.READ_1_1_4), with_master=False)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
|
|
|
# Add ROM linker region --------------------------------------------------------------------
|
2021-07-14 06:49:03 -04:00
|
|
|
self.bus.add_region("rom", SoCRegion(
|
2022-01-07 04:34:47 -05:00
|
|
|
origin = self.bus.regions["spiflash"].origin + bios_flash_offset,
|
2024-06-13 04:04:19 -04:00
|
|
|
size = 32 * KILOBYTE,
|
2021-07-14 06:49:03 -04:00
|
|
|
linker = True)
|
|
|
|
)
|
2022-01-07 09:19:23 -05:00
|
|
|
self.cpu.set_reset_address(self.bus.regions["rom"].origin)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
2021-02-01 07:22:57 -05:00
|
|
|
# SDR SDRAM --------------------------------------------------------------------------------
|
|
|
|
if not self.integrated_main_ram_size:
|
2021-01-29 16:28:31 -05:00
|
|
|
class SDRAMPads:
|
|
|
|
def __init__(self):
|
|
|
|
self.clk = platform.request("O_sdram_clk")
|
|
|
|
self.cke = platform.request("O_sdram_cke")
|
|
|
|
self.cs_n = platform.request("O_sdram_cs_n")
|
|
|
|
self.cas_n = platform.request("O_sdram_cas_n")
|
|
|
|
self.ras_n = platform.request("O_sdram_ras_n")
|
2021-02-01 07:22:57 -05:00
|
|
|
self.we_n = platform.request("O_sdram_wen_n")
|
2021-01-29 16:28:31 -05:00
|
|
|
self.dm = platform.request("O_sdram_dqm")
|
|
|
|
self.a = platform.request("O_sdram_addr")
|
|
|
|
self.ba = platform.request("O_sdram_ba")
|
|
|
|
self.dq = platform.request("IO_sdram_dq")
|
|
|
|
sdram_pads = SDRAMPads()
|
|
|
|
|
2021-07-14 04:43:26 -04:00
|
|
|
sdram_clk = ClockSignal("sys2x" if sdram_rate == "1:2" else "sys") # FIXME: use phase shift from PLL.
|
|
|
|
self.specials += DDROutput(0, 1, sdram_pads.clk, sdram_clk)
|
2021-01-29 16:28:31 -05:00
|
|
|
|
|
|
|
sdrphy_cls = HalfRateGENSDRPHY if sdram_rate == "1:2" else GENSDRPHY
|
2022-10-27 10:58:55 -04:00
|
|
|
self.sdrphy = sdrphy_cls(sdram_pads, sys_clk_freq)
|
2021-01-29 16:28:31 -05:00
|
|
|
self.add_sdram("sdram",
|
2021-07-20 11:25:51 -04:00
|
|
|
phy = self.sdrphy,
|
|
|
|
module = MT48LC4M16(sys_clk_freq, sdram_rate), # FIXME.
|
|
|
|
l2_cache_size = 128,
|
2021-01-29 16:28:31 -05:00
|
|
|
)
|
|
|
|
|
2020-09-30 08:01:36 -04:00
|
|
|
# Leds -------------------------------------------------------------------------------------
|
2021-07-06 17:39:37 -04:00
|
|
|
if with_led_chaser:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(
|
2021-07-06 17:39:37 -04:00
|
|
|
pads = platform.request_all("user_led"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
|
|
|
# Flash --------------------------------------------------------------------------------------------
|
|
|
|
|
2024-10-04 09:25:39 -04:00
|
|
|
def flash(bios_flash_offset, toolchain="gowin"):
|
2021-01-29 15:25:10 -05:00
|
|
|
# Create FTDI <--> SPI Flash proxy bitstream and load it.
|
2021-02-01 07:17:18 -05:00
|
|
|
# -------------------------------------------------------
|
2024-10-04 09:25:39 -04:00
|
|
|
platform = trenz_tec0117.Platform(toolchain=toolchain)
|
2021-01-29 15:25:10 -05:00
|
|
|
flash = platform.request("spiflash", 0)
|
|
|
|
bus = platform.request("spiflash", 1)
|
2020-09-30 08:01:36 -04:00
|
|
|
module = Module()
|
|
|
|
module.comb += [
|
|
|
|
flash.clk.eq(bus.clk),
|
|
|
|
flash.cs_n.eq(bus.cs_n),
|
|
|
|
flash.mosi.eq(bus.mosi),
|
|
|
|
bus.miso.eq(flash.miso),
|
|
|
|
]
|
|
|
|
platform.build(module)
|
|
|
|
prog = platform.create_programmer()
|
2024-10-04 09:25:39 -04:00
|
|
|
prog.flash(0, "build/top.fs")
|
2020-09-30 08:01:36 -04:00
|
|
|
|
2024-10-04 09:25:39 -04:00
|
|
|
# Flash Image through proxy Bitstream using pyspiflash
|
2021-02-01 07:17:18 -05:00
|
|
|
# ------------------------------------
|
2021-01-29 15:25:10 -05:00
|
|
|
from spiflash.serialflash import SerialFlashManager
|
2020-09-30 08:01:36 -04:00
|
|
|
dev = SerialFlashManager.get_flash_device("ftdi://ftdi:2232/2")
|
2021-02-01 07:17:18 -05:00
|
|
|
dev.TIMINGS["chip"] = (4, 60) # Chip is too slow
|
2020-09-30 08:01:36 -04:00
|
|
|
print("Erasing flash...")
|
2020-10-01 02:41:16 -04:00
|
|
|
dev.erase(0, -1)
|
2021-07-14 06:49:03 -04:00
|
|
|
with open("build/trenz_tec0117/software/bios/bios.bin", "rb") as f:
|
|
|
|
bios = f.read()
|
2020-09-30 08:01:36 -04:00
|
|
|
print("Programming flash...")
|
2021-07-14 06:49:03 -04:00
|
|
|
dev.write(bios_flash_offset, bios)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def main():
|
2022-11-06 15:39:52 -05:00
|
|
|
from litex.build.parser import LiteXArgumentParser
|
2022-11-08 04:41:35 -05:00
|
|
|
parser = LiteXArgumentParser(platform=trenz_tec0117.Platform, description="LiteX SoC on TEC0117.")
|
|
|
|
parser.add_target_argument("--bios-flash-offset", default="0x0000", help="BIOS offset in SPI Flash.")
|
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash Bitstream and BIOS.")
|
|
|
|
parser.add_target_argument("--sys-clk-freq", default=25e6, type=float, help="System clock frequency.")
|
2022-11-05 03:07:14 -04:00
|
|
|
sdopts = parser.target_group.add_mutually_exclusive_group()
|
2022-11-08 04:41:35 -05:00
|
|
|
sdopts.add_argument("--with-spi-sdcard", action="store_true", help="Enable SPI-mode SDCard support.")
|
|
|
|
sdopts.add_argument("--with-sdcard", action="store_true", help="Enable SDCard support.")
|
2020-09-30 08:01:36 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-01-29 15:25:10 -05:00
|
|
|
soc = BaseSoC(
|
2021-12-20 15:41:12 -05:00
|
|
|
bios_flash_offset = int(args.bios_flash_offset, 0),
|
2022-11-08 04:41:35 -05:00
|
|
|
sys_clk_freq = args.sys_clk_freq,
|
2024-10-04 09:25:39 -04:00
|
|
|
toolchain = args.toolchain,
|
2022-11-07 02:43:26 -05:00
|
|
|
**parser.soc_argdict
|
2020-11-12 12:07:28 -05:00
|
|
|
)
|
2022-05-02 06:42:04 -04:00
|
|
|
soc.platform.add_extension(trenz_tec0117._sdcard_pmod_io)
|
2021-07-20 11:25:51 -04:00
|
|
|
if args.with_spi_sdcard:
|
|
|
|
soc.add_spi_sdcard()
|
|
|
|
if args.with_sdcard:
|
|
|
|
soc.add_sdcard()
|
|
|
|
|
2022-11-05 03:07:14 -04:00
|
|
|
builder = Builder(soc, **parser.builder_argdict)
|
2022-05-06 09:14:32 -04:00
|
|
|
if args.build:
|
2022-11-05 03:07:14 -04:00
|
|
|
builder.build(**parser.toolchain_argdict)
|
2020-09-30 08:01:36 -04:00
|
|
|
|
|
|
|
if args.load:
|
|
|
|
prog = soc.platform.create_programmer()
|
2024-10-04 09:25:39 -04:00
|
|
|
prog.load_bitstream(builder.get_bitstream_filename(mode="sram"))
|
2021-02-01 07:17:18 -05:00
|
|
|
|
|
|
|
if args.flash:
|
|
|
|
prog = soc.platform.create_programmer()
|
2024-10-04 09:25:39 -04:00
|
|
|
flash(int(args.bios_flash_offset, 0), toolchain=args.toolchain)
|
|
|
|
prog.flash(0, builder.get_bitstream_filename(mode="flash", ext=".fs"))
|
2020-09-30 08:01:36 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|