2021-09-21 07:28:23 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This file is part of LiteX-Boards.
|
|
|
|
#
|
|
|
|
# Copyright (c) 2021 Antmicro <www.antmicro.com>
|
|
|
|
# SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
|
|
|
import os
|
2022-01-06 10:53:26 -05:00
|
|
|
import math
|
2022-01-19 11:01:33 -05:00
|
|
|
import json
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
from migen import *
|
|
|
|
|
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 antmicro_datacenter_ddr4_test_board
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
from litex.soc.cores.clock import *
|
|
|
|
from litex.soc.integration.soc_core import *
|
2021-11-08 10:39:49 -05:00
|
|
|
from litex.soc.integration.soc import SoCRegion
|
2021-09-21 07:28:23 -04:00
|
|
|
from litex.soc.integration.builder import *
|
|
|
|
from litex.soc.cores.led import LedChaser
|
2022-02-23 09:24:29 -05:00
|
|
|
from litex.soc.cores.bitbang import I2CMaster
|
2022-03-07 11:16:49 -05:00
|
|
|
from litex.soc.cores.video import VideoS7HDMIPHY
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
from litedram.modules import MTA18ASF2G72PZ
|
2022-01-17 06:47:39 -05:00
|
|
|
from litedram.phy.s7ddrphy import A7DDRPHY
|
2022-01-19 11:01:33 -05:00
|
|
|
from litedram.init import get_sdram_phy_py_header
|
|
|
|
from litedram.core.controller import ControllerSettings
|
|
|
|
from litedram.common import PhySettings, GeomSettings, TimingSettings
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
from liteeth.phy import LiteEthS7PHYRGMII
|
2022-03-01 03:10:19 -05:00
|
|
|
from litex.soc.cores.hyperbus import HyperRAM
|
2021-09-21 07:28:23 -04:00
|
|
|
|
2022-03-15 10:22:32 -04:00
|
|
|
from litespi.modules import S25FL128S0
|
|
|
|
from litespi.opcodes import SpiNorFlashOpCodes as Codes
|
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
# CRG ----------------------------------------------------------------------------------------------
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
class _CRG(LiteXModule):
|
2022-03-07 11:16:49 -05:00
|
|
|
def __init__(self, platform, sys_clk_freq, iodelay_clk_freq, with_video_pll=False):
|
2023-07-26 10:56:27 -04:00
|
|
|
self.rst = Signal()
|
2022-10-27 10:58:55 -04:00
|
|
|
self.cd_sys = ClockDomain()
|
|
|
|
self.cd_sys2x = ClockDomain()
|
|
|
|
self.cd_sys4x = ClockDomain()
|
|
|
|
self.cd_sys4x_dqs = ClockDomain()
|
|
|
|
self.cd_idelay = ClockDomain()
|
2021-09-21 07:28:23 -04:00
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
self.cd_hdmi = ClockDomain()
|
|
|
|
self.cd_hdmi5x = ClockDomain()
|
2022-03-07 11:16:49 -05:00
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
# # #
|
|
|
|
|
2022-03-07 11:16:49 -05:00
|
|
|
# Clk.
|
|
|
|
clk100 = platform.request("clk100")
|
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
self.pll = pll = S7PLL(speedgrade=-1)
|
2023-07-26 10:56:27 -04:00
|
|
|
self.comb += pll.reset.eq(self.rst)
|
2022-03-07 11:16:49 -05:00
|
|
|
pll.register_clkin(clk100, 100e6)
|
2022-01-17 06:47:39 -05:00
|
|
|
pll.create_clkout(self.cd_sys, sys_clk_freq)
|
|
|
|
pll.create_clkout(self.cd_sys2x, 2 * sys_clk_freq)
|
|
|
|
pll.create_clkout(self.cd_sys4x, 4 * sys_clk_freq)
|
|
|
|
pll.create_clkout(self.cd_sys4x_dqs, 4 * sys_clk_freq, phase=90)
|
|
|
|
pll.create_clkout(self.cd_idelay, iodelay_clk_freq)
|
2021-09-21 07:28:23 -04:00
|
|
|
|
2022-10-27 10:58:55 -04:00
|
|
|
self.idelayctrl = S7IDELAYCTRL(self.cd_idelay)
|
2021-09-21 07:28:23 -04:00
|
|
|
|
2022-03-07 11:16:49 -05:00
|
|
|
# Video PLL.
|
|
|
|
if with_video_pll:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.video_pll = video_pll = S7MMCM(speedgrade=-1)
|
2023-07-26 10:56:27 -04:00
|
|
|
self.comb += video_pll.reset.eq(self.rst)
|
2022-03-07 11:16:49 -05:00
|
|
|
video_pll.register_clkin(clk100, 100e6)
|
|
|
|
video_pll.create_clkout(self.cd_hdmi, 40e6)
|
|
|
|
video_pll.create_clkout(self.cd_hdmi5x, 5*40e6)
|
|
|
|
|
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
# BaseSoC ------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class BaseSoC(SoCCore):
|
2022-11-08 05:54:17 -05:00
|
|
|
def __init__(self, *, sys_clk_freq=100e6, iodelay_clk_freq=200e6,
|
2022-11-08 06:29:11 -05:00
|
|
|
with_ethernet = False,
|
|
|
|
with_etherbone = False,
|
|
|
|
eth_ip = "192.168.1.50",
|
|
|
|
eth_reset_time = "10e-3",
|
|
|
|
eth_dynamic_ip = False,
|
|
|
|
with_hyperram = False,
|
|
|
|
with_sdcard = False,
|
|
|
|
with_spi_flash = False,
|
|
|
|
with_led_chaser = True,
|
|
|
|
with_video_terminal = False,
|
|
|
|
with_video_framebuffer = False,
|
|
|
|
**kwargs):
|
2022-05-02 06:42:04 -04:00
|
|
|
platform = antmicro_datacenter_ddr4_test_board.Platform()
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
# CRG --------------------------------------------------------------------------------------
|
2022-03-07 11:16:49 -05:00
|
|
|
with_video_pll = (with_video_terminal or with_video_framebuffer)
|
2022-10-27 10:58:55 -04:00
|
|
|
self.crg = _CRG(platform, sys_clk_freq, iodelay_clk_freq=iodelay_clk_freq, with_video_pll=with_video_pll)
|
2021-09-21 07:28:23 -04:00
|
|
|
|
2022-04-21 06:17:26 -04:00
|
|
|
# SoCCore ----------------------------------------------------------------------------------
|
|
|
|
SoCCore.__init__(self, platform, sys_clk_freq, ident="LiteX SoC on data center test board", **kwargs)
|
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
# DDR4 SDRAM RDIMM -------------------------------------------------------------------------
|
|
|
|
if not self.integrated_main_ram_size:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.ddrphy = A7DDRPHY(platform.request("ddr4"),
|
2021-09-21 07:28:23 -04:00
|
|
|
memtype = "DDR4",
|
|
|
|
iodelay_clk_freq = iodelay_clk_freq,
|
|
|
|
sys_clk_freq = sys_clk_freq,
|
|
|
|
is_rdimm = True,
|
|
|
|
)
|
|
|
|
self.add_sdram("sdram",
|
|
|
|
phy = self.ddrphy,
|
|
|
|
module = MTA18ASF2G72PZ(sys_clk_freq, "1:4"),
|
|
|
|
l2_cache_size = kwargs.get("l2_size", 8192),
|
|
|
|
l2_cache_min_data_width = 256,
|
|
|
|
size = 0x40000000,
|
|
|
|
)
|
|
|
|
|
|
|
|
# HyperRAM ---------------------------------------------------------------------------------
|
|
|
|
if with_hyperram:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.hyperram = HyperRAM(platform.request("hyperram"), sys_clk_freq=sys_clk_freq)
|
2024-09-04 16:06:40 -04:00
|
|
|
self.bus.add_slave("hyperram", slave=self.hyperram.bus, region=SoCRegion(origin=0x20000000, size=8 * MEGABYTE, mode="rwx"))
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
# SD Card ----------------------------------------------------------------------------------
|
|
|
|
if with_sdcard:
|
|
|
|
self.add_sdcard()
|
|
|
|
|
|
|
|
# Ethernet / Etherbone ---------------------------------------------------------------------
|
|
|
|
if with_ethernet or with_etherbone:
|
|
|
|
# Traces between PHY and FPGA introduce ignorable delays of ~0.165ns +/- 0.015ns.
|
|
|
|
# PHY chip does not introduce delays on TX (FPGA->PHY), however it includes 1.2ns
|
|
|
|
# delay for RX CLK so we only need 0.8ns to match the desired 2ns.
|
2022-10-27 10:58:55 -04:00
|
|
|
self.ethphy = LiteEthS7PHYRGMII(
|
2021-09-21 07:28:23 -04:00
|
|
|
clock_pads = self.platform.request("eth_clocks"),
|
|
|
|
pads = self.platform.request("eth"),
|
|
|
|
rx_delay = 0.8e-9,
|
2022-01-06 10:53:26 -05:00
|
|
|
hw_reset_cycles = math.ceil(float(eth_reset_time) * self.sys_clk_freq)
|
2021-09-21 07:28:23 -04:00
|
|
|
)
|
|
|
|
if with_ethernet:
|
|
|
|
self.add_ethernet(phy=self.ethphy, dynamic_ip=eth_dynamic_ip)
|
|
|
|
if with_etherbone:
|
|
|
|
self.add_etherbone(phy=self.ethphy, ip_address=eth_ip)
|
|
|
|
|
|
|
|
# Leds -------------------------------------------------------------------------------------
|
|
|
|
if with_led_chaser:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.leds = LedChaser(
|
2021-09-21 07:28:23 -04:00
|
|
|
pads = platform.request_all("user_led"),
|
|
|
|
sys_clk_freq = sys_clk_freq)
|
|
|
|
|
2022-03-07 11:16:49 -05:00
|
|
|
# Video ------------------------------------------------------------------------------------
|
|
|
|
if with_video_terminal or with_video_framebuffer:
|
2022-10-27 10:58:55 -04:00
|
|
|
self.videophy = VideoS7HDMIPHY(platform.request("hdmi_out"), clock_domain="hdmi")
|
2022-03-07 11:16:49 -05:00
|
|
|
if with_video_terminal:
|
|
|
|
self.add_video_terminal(phy=self.videophy, timings="800x600@60Hz", clock_domain="hdmi")
|
|
|
|
if with_video_framebuffer:
|
|
|
|
self.add_video_framebuffer(phy=self.videophy, timings="800x600@60Hz", clock_domain="hdmi")
|
|
|
|
|
2022-03-15 10:22:32 -04:00
|
|
|
# SPI Flash --------------------------------------------------------------------------------
|
|
|
|
if with_spi_flash:
|
|
|
|
self.add_spi_flash(mode="4x", module=S25FL128S0(Codes.READ_1_1_4), with_master=True)
|
|
|
|
|
2022-02-23 09:24:29 -05:00
|
|
|
# System I2C (behing multiplexer) ----------------------------------------------------------
|
|
|
|
i2c_pads = platform.request('i2c')
|
2022-10-27 10:58:55 -04:00
|
|
|
self.i2c = I2CMaster(i2c_pads)
|
2022-02-23 09:24:29 -05:00
|
|
|
|
2022-01-19 11:01:33 -05:00
|
|
|
def generate_sdram_phy_py_header(self, output_file):
|
|
|
|
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
|
|
|
f = open(output_file, "w")
|
|
|
|
f.write(get_sdram_phy_py_header(
|
|
|
|
self.sdram.controller.settings.phy,
|
|
|
|
self.sdram.controller.settings.timing))
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
# Build --------------------------------------------------------------------------------------------
|
|
|
|
|
2022-01-19 11:01:33 -05:00
|
|
|
class LiteDRAMSettingsEncoder(json.JSONEncoder):
|
|
|
|
def default(self, o):
|
|
|
|
if isinstance(o, (ControllerSettings, GeomSettings, PhySettings, TimingSettings)):
|
2022-04-21 09:48:29 -04:00
|
|
|
ignored = ["self", "refresh_cls"]
|
2022-01-19 11:01:33 -05:00
|
|
|
return {k: v for k, v in vars(o).items() if k not in ignored}
|
|
|
|
elif isinstance(o, Signal) and isinstance(o.reset, Constant):
|
|
|
|
return o.reset
|
|
|
|
elif isinstance(o, Constant):
|
|
|
|
return o.value
|
|
|
|
print('o', end=' = '); __import__('pprint').pprint(o)
|
|
|
|
return super().default(o)
|
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
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=antmicro_datacenter_ddr4_test_board.Platform, description="LiteX SoC on DDR4 Datacenter Test Board.")
|
|
|
|
parser.add_target_argument("--flash", action="store_true", help="Flash bitstream.")
|
|
|
|
parser.add_target_argument("--sys-clk-freq", default=100e6, type=float, help="System clock frequency.")
|
|
|
|
parser.add_target_argument("--iodelay-clk-freq", default=200e6, type=float, help="IODELAYCTRL frequency.")
|
2022-11-05 03:07:14 -04:00
|
|
|
ethopts = parser.target_group.add_mutually_exclusive_group()
|
2022-11-08 04:41:35 -05:00
|
|
|
ethopts.add_argument("--with-ethernet", action="store_true", help="Add Ethernet.")
|
|
|
|
ethopts.add_argument("--with-etherbone", action="store_true", help="Add EtherBone.")
|
|
|
|
parser.add_target_argument("--eth-ip", default="192.168.1.50", help="Ethernet/Etherbone IP address.")
|
|
|
|
parser.add_target_argument("--eth-dynamic-ip", action="store_true", help="Enable dynamic Ethernet IP addresses setting.")
|
|
|
|
parser.add_target_argument("--eth-reset-time", default="10e-3", help="Duration of Ethernet PHY reset.")
|
|
|
|
parser.add_target_argument("--with-hyperram", action="store_true", help="Add HyperRAM.")
|
|
|
|
parser.add_target_argument("--with-sdcard", action="store_true", help="Add SDCard.")
|
|
|
|
parser.add_target_argument("--with-video-terminal", action="store_true", help="Enable Video Terminal (HDMI).")
|
|
|
|
parser.add_target_argument("--with-video-framebuffer", action="store_true", help="Enable Video Framebuffer (HDMI).")
|
2022-11-05 03:07:14 -04:00
|
|
|
parser.add_target_argument("--with-spi-flash", action="store_true", help="Enable SPI Flash (MMAPed).")
|
2021-09-21 07:28:23 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
assert not (args.with_etherbone and args.eth_dynamic_ip)
|
|
|
|
|
|
|
|
soc = BaseSoC(
|
2022-11-08 04:41:35 -05:00
|
|
|
sys_clk_freq = args.sys_clk_freq,
|
|
|
|
iodelay_clk_freq = args.iodelay_clk_freq,
|
2022-03-07 11:16:49 -05:00
|
|
|
with_ethernet = args.with_ethernet,
|
|
|
|
with_etherbone = args.with_etherbone,
|
|
|
|
eth_ip = args.eth_ip,
|
|
|
|
eth_dynamic_ip = args.eth_dynamic_ip,
|
|
|
|
with_hyperram = args.with_hyperram,
|
|
|
|
with_sdcard = args.with_sdcard,
|
2022-03-15 10:22:32 -04:00
|
|
|
with_spi_flash = args.with_spi_flash,
|
2022-03-07 11:16:49 -05:00
|
|
|
with_video_terminal = args.with_video_terminal,
|
|
|
|
with_video_framebuffer = args.with_video_framebuffer,
|
2022-11-07 02:43:26 -05:00
|
|
|
**parser.soc_argdict)
|
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)
|
2022-05-06 09:14:32 -04:00
|
|
|
builder.soc.generate_sdram_phy_py_header(os.path.join(builder.output_dir, "sdram_init.py"))
|
|
|
|
# LiteDRAM settings (controller, phy, geom, timing)
|
|
|
|
with open(os.path.join(builder.output_dir, 'litedram_settings.json'), 'w') as f:
|
|
|
|
json.dump(builder.soc.sdram.controller.settings, f, cls=LiteDRAMSettingsEncoder, indent=4)
|
2022-01-19 11:01:33 -05:00
|
|
|
|
2021-09-21 07:28:23 -04:00
|
|
|
if args.load:
|
|
|
|
prog = soc.platform.create_programmer()
|
2022-03-17 04:21:05 -04:00
|
|
|
prog.load_bitstream(builder.get_bitstream_filename(mode="sram"))
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
if args.flash:
|
|
|
|
prog = soc.platform.create_programmer()
|
2022-03-17 04:21:05 -04:00
|
|
|
prog.flash(0, builder.get_bitstream_filename(mode="flash"))
|
2021-09-21 07:28:23 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|