You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/csr/bus.rst
+186-10
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,187 @@
1
-
CSR bus primitives
2
-
------------------
1
+
CSR bus
2
+
-------
3
3
4
-
.. warning::
4
+
.. py:module:: amaranth_soc.csr.bus
5
5
6
-
This manual is a work in progress and is seriously incomplete!
6
+
The :mod:`amaranth_soc.csr.bus` module contains primitives to implement and access the registers of peripherals through a bus interface.
7
7
8
-
.. py:module:: amaranth_soc.csr.bus
8
+
.. testsetup::
9
+
10
+
from amaranth import *
11
+
12
+
from amaranth_soc import csr
13
+
from amaranth_soc.memory import *
14
+
15
+
.. _csr-bus-introduction:
16
+
17
+
Introduction
18
+
============
19
+
20
+
The CSR Bus API aims to provide unopinionated building blocks for accessing the *Control and Status Registers* of SoC peripherals, with an emphasis on safety and resource efficiency. It is composed of low-level :ref:`register interfaces <csr-bus-element>` suitable for a wide range of implementations, :ref:`register multiplexers <csr-bus-multiplexer>` that provide access to the registers of a peripheral, and :ref:`bus decoders <csr-bus-decoder>` that provide access to the registers of multiple peripherals.
21
+
22
+
.. _csr-bus-element:
23
+
24
+
Creating registers
25
+
++++++++++++++++++
26
+
27
+
A CSR register is a :class:`~amaranth.lib.wiring.Component` with an :class:`Element` member in its interface, oriented as input and named ``"element"``.
28
+
29
+
For example, this component is a read/write register with a configurable width:
CSR bus transactions go through the :class:`Element` port and always target the entire register. Transactions are completed in one clock cycle, regardless of the register width. A read and a write access can be part of the same transaction.
55
+
56
+
.. _csr-bus-multiplexer:
57
+
58
+
Accessing registers
59
+
+++++++++++++++++++
60
+
61
+
A :class:`Multiplexer` can provide access to a group of registers from a CSR bus. Registers must first be added to a :class:`MemoryMap`, before using it to instantiate a CSR multiplexer.
62
+
63
+
In the following example, a :class:`Multiplexer` provides access to two registers over an 8-bit bus:
The :mod:`amaranth_soc.csr.bus` module provides CSR bus primitives.
83
+
Registers mapped to a CSR bus are accessed atomically, regardless of their size. Each register is split into chunks according to the bus data width. Each chunk is assigned a consecutive address on the bus.
84
+
85
+
When the first chunk of a register is read, the values of all chunks of the register are captured, and reads from subsequent chunks return the captured values. When any chunk except the last chunk of a register is written, the written value is captured; a write to the last chunk writes all captured values to the register.
86
+
87
+
.. wavedrom:: csr/bus/example_mux
88
+
89
+
{
90
+
"signal": [
91
+
{"name": "clk",
92
+
"wave": "0P......."},
93
+
[
94
+
"csr_mux",
95
+
{"name": "bus.addr",
96
+
"wave": "x====x..|",
97
+
"data": [0,1,2,3]},
98
+
{"name": "bus.r_stb",
99
+
"wave": "01...0..|"},
100
+
{"name": "bus.r_data",
101
+
"wave": "0.3330..|",
102
+
"data": ["A", "B", "C"]},
103
+
{"name": "bus.w_stb",
104
+
"wave": "01...0..|"},
105
+
{"name": "bus.w_data",
106
+
"wave": "x4444x..|",
107
+
"data": ["E", "F", "G", "H"]}
108
+
],
109
+
{},
110
+
[
111
+
"reg_foo",
112
+
{"name": "element.r_stb",
113
+
"wave": "010.....|"},
114
+
{"name": "element.r_data",
115
+
"wave": "3.....4.|",
116
+
"data": ["CBA", "GFE"]},
117
+
{"name": "element.w_stb",
118
+
"wave": "0....10.|"},
119
+
{"name": "element.w_data",
120
+
"wave": "x....4x.|",
121
+
"data": ["GFE"]}
122
+
]
123
+
]
124
+
}
125
+
126
+
A :class:`Multiplexer` adds a latency of one clock cycle to read and write accesses.
127
+
128
+
.. important::
129
+
130
+
To safely access registers over the bus interface of a :class:`Multiplexer`:
131
+
* the bus initiator must have exclusive ownership over the address range of the multiplexer until the register transaction is either completed or aborted.
132
+
* the bus initiator must access a register in ascending order of addresses, but it may abort the transaction after any bus cycle.
133
+
134
+
.. note::
135
+
136
+
Because the CSR bus conserves logic and routing resources, it is common to e.g. bridge a CSR bus with a narrow *N*-bit datapath to a CPU with a wider *W*-bit datapath (*W>N*) in cases where CSR access latency is less important than resource usage.
137
+
138
+
In this case, two strategies are possible for connecting the CSR bus to the CPU:
139
+
140
+
* The CPU could access the CSR bus directly (with no intervening logic other than simple translation of control signals). The register alignment should be set to 1 (i.e. ``memory_map.alignment`` should be 0), and each *R*-bit register would occupy *ceil(R/N)* addresses from the CPU perspective, requiring the same amount of memory instructions to access.
141
+
* The CPU could access the CSR bus through a width down-converter, which would issue *W/N* CSR accesses for each CPU access. The register alignment should be set to *W/N*, and each *R*-bit register would occupy *ceil(R/K)* addresses from the CPU perspective, requiring the same amount of memory instructions to access.
142
+
143
+
If the register alignment is greater than 1, it affects which CSR bus write is considered a write to the last register chunk. For example, if a 24-bit register is accessed through an 8-bit CSR bus and a CPU with a 32-bit datapath, a write to this register requires 4 CSR bus writes to complete, and the last write is the one that actually writes the value to the register. This allows determining write latency solely from the amount of addresses occupied by the register in the CPU address space, and the CSR bus data width.
144
+
145
+
.. _csr-bus-decoder:
146
+
147
+
Accessing a hierarchy of registers
148
+
++++++++++++++++++++++++++++++++++
149
+
150
+
A :class:`Decoder` can provide access to group of :class:`Multiplexer`\ s and subordinate :class:`Decoder`\ s, forming a hierarchical address space of CSR registers.
151
+
152
+
In the following example, a :class:`Decoder` provides access to the registers of two peripherals:
Although there is no functional difference between adding a group of registers directly to a
179
+
:class:`Multiplexer` and adding them to multiple :class:`Multiplexer`\ s that are aggregated with a :class:`Decoder`, hierarchical CSR buses are useful for organizing a hierarchical design.
180
+
181
+
If many peripherals are directly served by a single :class:`Multiplexer`, a very large amount of ports will connect the peripheral registers to the multiplexer, and the cost of decoding logic would not be attributed to specific peripherals. With a :class:`Decoder`, only five signals per peripheral will be used, and the logic could be kept together with the peripheral.
0 commit comments