1 // See LICENSE for license details.
2 package sifive.blocks.devices.uart
5 import freechips.rocketchip.chip.RTCPeriod
6 import freechips.rocketchip.config.Parameters
7 import freechips.rocketchip.diplomacy.DTSTimebase
8 import freechips.rocketchip.regmapper._
9 import freechips.rocketchip.tilelink._
10 import freechips.rocketchip.util._
12 import sifive.blocks.util.{NonBlockingEnqueue, NonBlockingDequeue}
14 case class UARTParams(
18 divisorBits: Int = 16,
24 trait HasUARTParameters {
26 def uartDataBits = c.dataBits
27 def uartStopBits = c.stopBits
28 def uartDivisorBits = c.divisorBits
30 def uartOversample = c.oversample
31 def uartOversampleFactor = 1 << uartOversample
32 def uartNSamples = c.nSamples
34 def uartNTxEntries = c.nTxEntries
35 def uartNRxEntries = c.nRxEntries
37 require(uartDivisorBits > uartOversample)
38 require(uartOversampleFactor > uartNSamples)
41 abstract class UARTModule(val c: UARTParams)(implicit val p: Parameters)
42 extends Module with HasUARTParameters
44 class UARTPortIO extends Bundle {
45 val txd = Bool(OUTPUT)
49 trait HasUARTTopBundleContents extends Bundle {
50 val port = new UARTPortIO
53 class UARTTx(c: UARTParams)(implicit p: Parameters) extends UARTModule(c)(p) {
56 val in = Decoupled(Bits(width = uartDataBits)).flip
57 val out = Bits(OUTPUT, 1)
58 val div = UInt(INPUT, uartDivisorBits)
59 val nstop = UInt(INPUT, log2Up(uartStopBits))
62 val prescaler = Reg(init = UInt(0, uartDivisorBits))
63 val pulse = (prescaler === UInt(0))
65 private val n = uartDataBits + 1
66 val counter = Reg(init = UInt(0, log2Floor(n + uartStopBits) + 1))
67 val shifter = Reg(Bits(width = n))
68 val out = Reg(init = Bits(1, 1))
71 val busy = (counter =/= UInt(0))
72 io.in.ready := io.en && !busy
74 printf("%c", io.in.bits)
75 shifter := Cat(io.in.bits, Bits(0, 1))
76 counter := Mux1H((0 until uartStopBits).map(i =>
77 (io.nstop === UInt(i)) -> UInt(n + i + 1)))
80 prescaler := Mux(pulse, io.div, prescaler - UInt(1))
82 when (pulse && busy) {
83 counter := counter - UInt(1)
84 shifter := Cat(Bits(1, 1), shifter >> 1)
89 class UARTRx(c: UARTParams)(implicit p: Parameters) extends UARTModule(c)(p) {
92 val in = Bits(INPUT, 1)
93 val out = Valid(Bits(width = uartDataBits))
94 val div = UInt(INPUT, uartDivisorBits)
97 val debounce = Reg(init = UInt(0, 2))
98 val debounce_max = (debounce === UInt(3))
99 val debounce_min = (debounce === UInt(0))
101 val prescaler = Reg(init = UInt(0, uartDivisorBits - uartOversample))
102 val start = Wire(init = Bool(false))
103 val busy = Wire(init = Bool(false))
104 val pulse = (prescaler === UInt(0)) && busy
107 prescaler := prescaler - UInt(1)
109 when (start || pulse) {
110 prescaler := io.div >> uartOversample
113 val sample = Reg(Bits(width = uartNSamples))
114 val voter = Majority(sample.toBools.toSet)
116 sample := Cat(sample, io.in)
119 private val delay0 = (uartOversampleFactor + uartNSamples) >> 1
120 private val delay1 = uartOversampleFactor
122 val timer = Reg(UInt(width = uartOversample + 1))
123 val counter = Reg(UInt(width = log2Floor(uartDataBits) + 1))
124 val shifter = Reg(Bits(width = uartDataBits))
125 val expire = (timer === UInt(0)) && pulse
127 val sched = Wire(init = Bool(false))
129 timer := timer - UInt(1)
132 timer := UInt(delay1-1)
135 val valid = Reg(init = Bool(false))
137 io.out.valid := valid
138 io.out.bits := shifter
140 val (s_idle :: s_start :: s_data :: Nil) = Enum(UInt(), 3)
141 val state = Reg(init = s_idle)
145 when (!(!io.in) && !debounce_min) {
146 debounce := debounce - UInt(1)
149 debounce := debounce + UInt(1)
150 when (debounce_max) {
153 timer := UInt(delay0-1)
166 counter := UInt(uartDataBits)
174 counter := counter - UInt(1)
175 when (counter === UInt(0)) {
179 shifter := Cat(voter, shifter >> 1)
191 class UARTInterrupts extends Bundle {
196 trait HasUARTTopModuleContents extends Module with HasUARTParameters with HasRegMap {
197 val io: HasUARTTopBundleContents
198 implicit val p: Parameters
199 def params: UARTParams
202 val txm = Module(new UARTTx(params))
203 val txq = Module(new Queue(txm.io.in.bits, uartNTxEntries))
205 val rxm = Module(new UARTRx(params))
206 val rxq = Module(new Queue(rxm.io.out.bits, uartNRxEntries))
208 val divinit = p(DTSTimebase) * p(RTCPeriod) / 115200
209 val div = Reg(init = UInt(divinit, uartDivisorBits))
211 private val stopCountBits = log2Up(uartStopBits)
212 private val txCountBits = log2Floor(uartNTxEntries) + 1
213 private val rxCountBits = log2Floor(uartNRxEntries) + 1
215 val txen = Reg(init = Bool(false))
216 val rxen = Reg(init = Bool(false))
217 val txwm = Reg(init = UInt(0, txCountBits))
218 val rxwm = Reg(init = UInt(0, rxCountBits))
219 val nstop = Reg(init = UInt(0, stopCountBits))
222 txm.io.in <> txq.io.deq
224 txm.io.nstop := nstop
225 io.port.txd := txm.io.out
228 rxm.io.in := io.port.rxd
229 rxq.io.enq <> rxm.io.out
232 val ie = Reg(init = new UARTInterrupts().fromBits(Bits(0)))
233 val ip = Wire(new UARTInterrupts)
235 ip.txwm := (txq.io.count < txwm)
236 ip.rxwm := (rxq.io.count > rxwm)
237 interrupts(0) := (ip.txwm && ie.txwm) || (ip.rxwm && ie.rxwm)
240 UARTCtrlRegs.txfifo -> NonBlockingEnqueue(txq.io.enq),
241 UARTCtrlRegs.rxfifo -> NonBlockingDequeue(rxq.io.deq),
243 UARTCtrlRegs.txctrl -> Seq(
245 RegField(stopCountBits, nstop)),
246 UARTCtrlRegs.rxctrl -> Seq(RegField(1, rxen)),
247 UARTCtrlRegs.txmark -> Seq(RegField(txCountBits, txwm)),
248 UARTCtrlRegs.rxmark -> Seq(RegField(rxCountBits, rxwm)),
250 UARTCtrlRegs.ie -> Seq(
251 RegField(1, ie.txwm),
252 RegField(1, ie.rxwm)),
254 UARTCtrlRegs.ip -> Seq(
255 RegField.r(1, ip.txwm),
256 RegField.r(1, ip.rxwm)),
258 UARTCtrlRegs.div -> Seq(
259 RegField(uartDivisorBits, div))
263 // Magic TL2 Incantation to create a TL2 UART
264 class TLUART(w: Int, c: UARTParams)(implicit p: Parameters)
265 extends TLRegisterRouter(c.address, "serial", Seq("sifive,uart0"), interrupts = 1, beatBytes = w)(
266 new TLRegBundle(c, _) with HasUARTTopBundleContents)(
267 new TLRegModule(c, _, _) with HasUARTTopModuleContents)