2018-02-23 07:38:19 -05:00
|
|
|
from migen import *
|
|
|
|
from migen.genlib.record import *
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2016-04-29 01:21:42 -04:00
|
|
|
from litex.soc.interconnect import wishbone
|
2015-11-07 06:26:46 -05:00
|
|
|
from litex.soc.interconnect.csr import AutoCSR
|
|
|
|
from litex.soc.integration.soc_core import *
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2016-04-29 11:40:55 -04:00
|
|
|
from litedram.frontend import crossbar
|
2018-08-21 08:52:28 -04:00
|
|
|
from litedram.frontend.wishbone import LiteDRAMWishbone2Native
|
2016-04-29 11:40:55 -04:00
|
|
|
from litedram import dfii, core
|
2016-04-29 01:21:42 -04:00
|
|
|
|
2015-04-13 10:47:22 -04:00
|
|
|
|
2015-09-29 06:14:54 -04:00
|
|
|
__all__ = ["SoCSDRAM", "soc_sdram_args", "soc_sdram_argdict"]
|
|
|
|
|
|
|
|
|
2015-10-01 23:17:47 -04:00
|
|
|
class ControllerInjector(Module, AutoCSR):
|
2016-05-03 18:59:02 -04:00
|
|
|
def __init__(self, phy, geom_settings, timing_settings, **kwargs):
|
2015-09-24 03:59:55 -04:00
|
|
|
self.submodules.dfii = dfii.DFIInjector(geom_settings.addressbits, geom_settings.bankbits,
|
|
|
|
phy.settings.dfi_databits, phy.settings.nphases)
|
2015-12-27 06:01:29 -05:00
|
|
|
self.comb += self.dfii.master.connect(phy.dfi)
|
2015-09-24 03:59:55 -04:00
|
|
|
|
2018-08-22 07:40:22 -04:00
|
|
|
self.submodules.controller = controller = core.LiteDRAMController(
|
|
|
|
phy.settings, geom_settings, timing_settings, **kwargs)
|
2016-04-29 10:24:24 -04:00
|
|
|
self.comb += controller.dfi.connect(self.dfii.slave)
|
2015-09-24 03:59:55 -04:00
|
|
|
|
2016-05-03 13:44:33 -04:00
|
|
|
self.submodules.crossbar = crossbar.LiteDRAMCrossbar(controller.interface, controller.nrowbits)
|
2015-09-24 03:59:55 -04:00
|
|
|
|
|
|
|
|
2015-09-23 12:18:27 -04:00
|
|
|
class SoCSDRAM(SoCCore):
|
2015-04-13 10:19:55 -04:00
|
|
|
csr_map = {
|
2015-04-13 11:56:51 -04:00
|
|
|
"sdram": 8,
|
2015-10-01 23:17:47 -04:00
|
|
|
"l2_cache": 9
|
2015-04-13 10:19:55 -04:00
|
|
|
}
|
2015-09-25 06:43:20 -04:00
|
|
|
csr_map.update(SoCCore.csr_map)
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2015-10-01 23:17:47 -04:00
|
|
|
def __init__(self, platform, clk_freq, l2_size=8192, **kwargs):
|
2015-09-25 06:43:20 -04:00
|
|
|
SoCCore.__init__(self, platform, clk_freq, **kwargs)
|
2018-07-19 06:51:16 -04:00
|
|
|
if self.cpu_type is not None and self.csr_data_width != 8:
|
|
|
|
raise NotImplementedError("BIOS supports SDRAM initialization only for csr_data_width=8")
|
2015-10-01 23:17:47 -04:00
|
|
|
self.l2_size = l2_size
|
2016-04-12 14:16:47 -04:00
|
|
|
|
2015-10-01 23:17:47 -04:00
|
|
|
self._sdram_phy = []
|
2015-06-17 08:52:30 -04:00
|
|
|
self._wb_sdram_ifs = []
|
|
|
|
self._wb_sdram = wishbone.Interface()
|
|
|
|
|
|
|
|
def add_wb_sdram_if(self, interface):
|
|
|
|
if self.finalized:
|
|
|
|
raise FinalizeError
|
|
|
|
self._wb_sdram_ifs.append(interface)
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2016-05-03 18:59:02 -04:00
|
|
|
def register_sdram(self, phy, geom_settings, timing_settings, **kwargs):
|
2015-10-01 23:17:47 -04:00
|
|
|
assert not self._sdram_phy
|
|
|
|
self._sdram_phy.append(phy) # encapsulate in list to prevent CSR scanning
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2018-08-22 07:40:22 -04:00
|
|
|
self.submodules.sdram = ControllerInjector(
|
|
|
|
phy, geom_settings, timing_settings, **kwargs)
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2015-04-13 10:19:55 -04:00
|
|
|
dfi_databits_divisor = 1 if phy.settings.memtype == "SDR" else 2
|
|
|
|
sdram_width = phy.settings.dfi_databits//dfi_databits_divisor
|
2015-10-01 23:17:47 -04:00
|
|
|
main_ram_size = 2**(geom_settings.bankbits +
|
|
|
|
geom_settings.rowbits +
|
|
|
|
geom_settings.colbits)*sdram_width//8
|
2018-08-22 07:40:22 -04:00
|
|
|
|
2017-01-12 19:33:48 -05:00
|
|
|
# TODO: modify mem_map to allow larger memories.
|
2015-04-13 10:19:55 -04:00
|
|
|
main_ram_size = min(main_ram_size, 256*1024*1024)
|
2016-04-27 06:34:18 -04:00
|
|
|
self.add_constant("L2_SIZE", self.l2_size)
|
2015-03-28 18:10:33 -04:00
|
|
|
|
2015-06-17 10:32:17 -04:00
|
|
|
# add a Wishbone interface to the DRAM
|
2015-06-17 08:52:30 -04:00
|
|
|
wb_sdram = wishbone.Interface()
|
|
|
|
self.add_wb_sdram_if(wb_sdram)
|
|
|
|
self.register_mem("main_ram", self.mem_map["main_ram"], wb_sdram, main_ram_size)
|
|
|
|
|
2016-04-29 10:24:24 -04:00
|
|
|
if self.l2_size:
|
2016-04-29 11:40:55 -04:00
|
|
|
port = self.sdram.crossbar.get_port()
|
2018-08-22 07:28:23 -04:00
|
|
|
self.submodules.l2_cache = wishbone.Cache(
|
|
|
|
self.l2_size//4, self._wb_sdram, wishbone.Interface(port.data_width))
|
2018-08-21 08:52:28 -04:00
|
|
|
self.submodules.wishbone_bridge = LiteDRAMWishbone2Native(self.l2_cache.slave, port)
|
2015-02-28 05:44:14 -05:00
|
|
|
|
2015-04-13 10:19:55 -04:00
|
|
|
def do_finalize(self):
|
|
|
|
if not self.integrated_main_ram_size:
|
2015-10-01 23:17:47 -04:00
|
|
|
if not self._sdram_phy:
|
|
|
|
raise FinalizeError("Need to call SDRAMSoC.register_sdram()")
|
2015-06-17 08:52:30 -04:00
|
|
|
|
|
|
|
# arbitrate wishbone interfaces to the DRAM
|
2018-08-22 07:40:22 -04:00
|
|
|
self.submodules.wb_sdram_con = wishbone.Arbiter(
|
|
|
|
self._wb_sdram_ifs, self._wb_sdram)
|
2015-09-25 06:43:20 -04:00
|
|
|
SoCCore.do_finalize(self)
|
2015-09-29 06:14:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
soc_sdram_args = soc_core_args
|
|
|
|
soc_sdram_argdict = soc_core_argdict
|