de4392c8860ed9d2e07f40dea243fbb91ccb9a83
[sifive-blocks.git] / src / main / scala / devices / uart / UARTPeriphery.scala
1 // See LICENSE for license details.
2 package sifive.blocks.devices.uart
3
4 import Chisel._
5 import chisel3.experimental.{withClockAndReset}
6 import freechips.rocketchip.config.Field
7 import freechips.rocketchip.util.SyncResetSynchronizerShiftReg
8 import freechips.rocketchip.coreplex.{HasPeripheryBus, PeripheryBusKey, HasInterruptBus}
9 import freechips.rocketchip.diplomacy.{LazyModule, LazyMultiIOModuleImp}
10 import sifive.blocks.devices.pinctrl.{Pin}
11
12 case object PeripheryUARTKey extends Field[Seq[UARTParams]]
13
14 trait HasPeripheryUART extends HasPeripheryBus with HasInterruptBus {
15 private val divinit = (p(PeripheryBusKey).frequency / 115200).toInt
16 val uartParams = p(PeripheryUARTKey).map(_.copy(divisorInit = divinit))
17 val uarts = uartParams map { params =>
18 val uart = LazyModule(new TLUART(pbus.beatBytes, params))
19 uart.node := pbus.toVariableWidthSlaves
20 ibus.fromSync := uart.intnode
21 uart
22 }
23 }
24
25 trait HasPeripheryUARTBundle {
26 val uart: Vec[UARTPortIO]
27
28 def tieoffUARTs(dummy: Int = 1) {
29 uart.foreach { _.rxd := UInt(1) }
30 }
31
32 }
33
34 trait HasPeripheryUARTModuleImp extends LazyMultiIOModuleImp with HasPeripheryUARTBundle {
35 val outer: HasPeripheryUART
36 val uart = IO(Vec(outer.uartParams.size, new UARTPortIO))
37
38 (uart zip outer.uarts).foreach { case (io, device) =>
39 io <> device.module.io.port
40 }
41 }
42
43 class UARTPins[T <: Pin] (pingen: () => T) extends Bundle {
44 val rxd = pingen()
45 val txd = pingen()
46
47 override def cloneType: this.type =
48 this.getClass.getConstructors.head.newInstance(pingen).asInstanceOf[this.type]
49
50 def fromPort(uart: UARTPortIO, clock: Clock, reset: Bool, syncStages: Int = 0) {
51 withClockAndReset(clock, reset) {
52 txd.outputPin(uart.txd)
53 val rxd_t = rxd.inputPin()
54 uart.rxd := SyncResetSynchronizerShiftReg(rxd_t, syncStages, init = Bool(true), name = Some("uart_rxd_sync"))
55 }
56 }
57 }
58