1 /////////////////////////////////////////////////////////////////////
3 //// WISHBONE revB.2 compliant I2C Master controller Top-level ////
6 //// Author: Richard Herveille ////
7 //// richard@asics.ws ////
10 //// Downloaded from: http://www.opencores.org/projects/i2c/ ////
12 /////////////////////////////////////////////////////////////////////
14 //// Copyright (C) 2001 Richard Herveille ////
15 //// richard@asics.ws ////
17 //// This source file may be used and distributed without ////
18 //// restriction provided that this copyright statement is not ////
19 //// removed from the file and that any derivative work contains ////
20 //// the original copyright notice and the associated disclaimer.////
22 //// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY ////
23 //// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED ////
24 //// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS ////
25 //// FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL THE AUTHOR ////
26 //// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, ////
27 //// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ////
28 //// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE ////
29 //// GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR ////
30 //// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF ////
31 //// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT ////
32 //// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT ////
33 //// OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE ////
34 //// POSSIBILITY OF SUCH DAMAGE. ////
36 /////////////////////////////////////////////////////////////////////
38 // This code was re-written in Chisel by SiFive, Inc.
39 // See LICENSE for license details.
41 package sifive.blocks.devices.i2c
47 import uncore.tilelink2._
48 import rocketchip.PeripheryBusConfig
49 import util.AsyncResetRegVec
50 import sifive.blocks.devices.gpio.{GPIOPinCtrl}
52 case class I2CConfig(address: BigInt)
54 trait HasI2CParameters {
55 implicit val p: Parameters
60 class I2CPin extends Bundle {
62 val out = Bool(OUTPUT)
66 class I2CPort extends Bundle {
71 trait I2CBundle extends Bundle with HasI2CParameters {
72 val port = new I2CPort
75 trait I2CModule extends Module with HasI2CParameters with HasRegMap {
78 val I2C_CMD_NOP = UInt(0x00)
79 val I2C_CMD_START = UInt(0x01)
80 val I2C_CMD_STOP = UInt(0x02)
81 val I2C_CMD_WRITE = UInt(0x04)
82 val I2C_CMD_READ = UInt(0x08)
84 class PrescalerBundle extends Bundle{
89 class ControlBundle extends Bundle{
92 val reserved = UInt(6.W)
95 class CommandBundle extends Bundle{
101 val reserved = UInt(2.W)
105 class StatusBundle extends Bundle{
106 val receivedAck = Bool() // received aknowledge from slave
109 val reserved = UInt(3.W)
110 val transferInProgress = Bool()
114 // control state visible to SW/driver
115 val prescaler = Reg(init = (new PrescalerBundle).fromBits(0xFFFF.U))
116 val control = Reg(init = (new ControlBundle).fromBits(0.U))
117 val transmitData = Reg(init = UInt(0, 8.W))
118 val receivedData = Reg(init = UInt(0, 8.W))
119 val cmd = Reg(init = (new CommandBundle).fromBits(0.U))
120 val status = Reg(init = (new StatusBundle).fromBits(0.U))
123 //////// Bit level ////////
125 io.port.scl.out := false.B // i2c clock line output
126 io.port.sda.out := false.B // i2c data line output
128 // filter SCL and SDA signals; (attempt to) remove glitches
129 val filterCnt = Reg(init = UInt(0, 14.W))
130 when ( !control.coreEn ) {
132 } .elsewhen (!(filterCnt.orR)) {
133 filterCnt := Cat(prescaler.hi, prescaler.lo) >> 2 //16x I2C bus frequency
135 filterCnt := filterCnt - 1.U
138 val fSCL = Reg(init = UInt(0x7, 3.W))
139 val fSDA = Reg(init = UInt(0x7, 3.W))
140 when (!(filterCnt.orR)) {
141 fSCL := Cat(fSCL, io.port.scl.in)
142 fSDA := Cat(fSDA, io.port.sda.in)
145 val sSCL = Reg(init = true.B, next = (new Majority(fSCL.toBools.toSet)).out)
146 val sSDA = Reg(init = true.B, next = (new Majority(fSDA.toBools.toSet)).out)
148 val dSCL = Reg(init = true.B, next = sSCL)
149 val dSDA = Reg(init = true.B, next = sSDA)
151 val dSCLOen = Reg(next = io.port.scl.oe) // delayed scl_oen
153 // detect start condition => detect falling edge on SDA while SCL is high
154 // detect stop condition => detect rising edge on SDA while SCL is high
155 val startCond = Reg(init = false.B, next = !sSDA && dSDA && sSCL)
156 val stopCond = Reg(init = false.B, next = sSDA && !dSDA && sSCL)
158 // master drives SCL high, but another master pulls it low
159 // master start counting down its low cycle now (clock synchronization)
160 val sclSync = dSCL && !sSCL && io.port.scl.oe
162 // slave_wait is asserted when master wants to drive SCL high, but the slave pulls it low
163 // slave_wait remains asserted until the slave releases SCL
164 val slaveWait = Reg(init = false.B)
165 slaveWait := (io.port.scl.oe && !dSCLOen && !sSCL) || (slaveWait && !sSCL)
167 val clkEn = Reg(init = true.B) // clock generation signals
168 val cnt = Reg(init = UInt(0, 16.W)) // clock divider counter (synthesis)
170 // generate clk enable signal
171 when (!(cnt.orR) || !control.coreEn || sclSync ) {
172 cnt := Cat(prescaler.hi, prescaler.lo)
175 .elsewhen (slaveWait) {
183 val sclOen = Reg(init = true.B)
184 io.port.scl.oe := sclOen
186 val sdaOen = Reg(init = true.B)
187 io.port.sda.oe := sdaOen
189 val sdaChk = Reg(init = false.B) // check SDA output (Multi-master arbitration)
191 val transmitBit = Reg(init = false.B)
192 val receivedBit = Reg(Bool())
193 when (sSCL && !dSCL) {
197 val bitCmd = Reg(init = UInt(0, 4.W)) // command (from byte controller)
198 val bitCmdStop = Reg(init = false.B)
200 bitCmdStop := bitCmd === I2C_CMD_STOP
202 val bitCmdAck = Reg(init = false.B)
205 s_bit_start_a :: s_bit_start_b :: s_bit_start_c :: s_bit_start_d :: s_bit_start_e ::
206 s_bit_stop_a :: s_bit_stop_b :: s_bit_stop_c :: s_bit_stop_d ::
207 s_bit_rd_a :: s_bit_rd_b :: s_bit_rd_c :: s_bit_rd_d ::
208 s_bit_wr_a :: s_bit_wr_b :: s_bit_wr_c :: s_bit_wr_d :: Nil) = Enum(UInt(), 18)
209 val bitState = Reg(init = s_bit_idle)
211 val arbLost = Reg(init = false.B, next = (sdaChk && !sSDA && sdaOen) | ((bitState === s_bit_idle) && stopCond && !bitCmdStop))
215 bitState := s_bit_idle
228 is (I2C_CMD_START) { bitState := s_bit_start_a }
229 is (I2C_CMD_STOP) { bitState := s_bit_stop_a }
230 is (I2C_CMD_WRITE) { bitState := s_bit_wr_a }
231 is (I2C_CMD_READ) { bitState := s_bit_rd_a }
237 bitState := s_bit_start_b
243 bitState := s_bit_start_c
249 bitState := s_bit_start_d
255 bitState := s_bit_start_e
261 bitState := s_bit_idle
269 bitState := s_bit_stop_b
275 bitState := s_bit_stop_c
281 bitState := s_bit_stop_d
287 bitState := s_bit_idle
295 bitState := s_bit_rd_b
301 bitState := s_bit_rd_c
307 bitState := s_bit_rd_d
313 bitState := s_bit_idle
321 bitState := s_bit_wr_b
323 sdaOen := transmitBit
327 bitState := s_bit_wr_c
329 sdaOen := transmitBit
333 bitState := s_bit_wr_d
335 sdaOen := transmitBit
339 bitState := s_bit_idle
342 sdaOen := transmitBit
350 //////// Byte level ///////
351 val load = Reg(init = false.B) // load shift register
352 val shift = Reg(init = false.B) // shift shift register
353 val cmdAck = Reg(init = false.B) // also done
354 val receivedAck = Reg(init = false.B) // from I2C slave
355 val go = (cmd.read | cmd.write | cmd.stop) & !cmdAck
357 val bitCnt = Reg(init = UInt(0, 3.W))
362 bitCnt := bitCnt - 1.U
364 val bitCntDone = !(bitCnt.orR)
366 // receivedData is used as shift register directly
368 receivedData := transmitData
371 receivedData := Cat(receivedData, receivedBit)
374 val (s_byte_idle :: s_byte_start :: s_byte_read :: s_byte_write :: s_byte_ack :: s_byte_stop :: Nil) = Enum(UInt(), 6)
375 val byteState = Reg(init = s_byte_idle)
378 bitCmd := I2C_CMD_NOP
379 transmitBit := false.B
383 byteState := s_byte_idle
384 receivedAck := false.B
387 transmitBit := receivedData(7)
396 byteState := s_byte_start
397 bitCmd := I2C_CMD_START
399 .elsewhen (cmd.read) {
400 byteState := s_byte_read
401 bitCmd := I2C_CMD_READ
403 .elsewhen (cmd.write) {
404 byteState := s_byte_write
405 bitCmd := I2C_CMD_WRITE
408 byteState := s_byte_stop
409 bitCmd := I2C_CMD_STOP
418 byteState := s_byte_read
419 bitCmd := I2C_CMD_READ
422 byteState := s_byte_write
423 bitCmd := I2C_CMD_WRITE
432 byteState := s_byte_ack
433 bitCmd := I2C_CMD_READ
436 byteState := s_byte_write
437 bitCmd := I2C_CMD_WRITE
445 byteState := s_byte_ack
446 bitCmd := I2C_CMD_WRITE
449 byteState := s_byte_read
450 bitCmd := I2C_CMD_READ
454 transmitBit := cmd.ack
460 byteState := s_byte_stop
461 bitCmd := I2C_CMD_STOP
464 byteState := s_byte_idle
465 bitCmd := I2C_CMD_NOP
467 // generate command acknowledge signal
471 // assign ack_out output to bit_controller_rxd (contains last received bit)
472 receivedAck := receivedBit
474 transmitBit := true.B
477 transmitBit := cmd.ack
482 byteState := s_byte_idle
483 bitCmd := I2C_CMD_NOP
485 // assign ack_out output to bit_controller_rxd (contains last received bit)
493 //////// Top level ////////
495 // hack: b/c the same register offset is used to write cmd and read status
496 val nextCmd = Wire(UInt(8.W))
497 nextCmd := cmd.asUInt
498 cmd := (new CommandBundle).fromBits(nextCmd)
500 when (cmdAck || arbLost) {
501 cmd.start := false.B // clear command bits when done
502 cmd.stop := false.B // or when aribitration lost
506 cmd.irqAck := false.B // clear IRQ_ACK bit (essentially 1 cycle pulse b/c it is overwritten by regmap below)
508 status.receivedAck := receivedAck
510 status.busy := false.B
512 .elsewhen (startCond) {
513 status.busy := true.B
517 status.arbLost := true.B
519 .elsewhen (cmd.start) {
520 status.arbLost := false.B
522 status.transferInProgress := cmd.read || cmd.write
523 status.irqFlag := (cmdAck || arbLost || status.irqFlag) && !cmd.irqAck
527 I2CCtrlRegs.prescaler_lo -> Seq(RegField(8, prescaler.lo)),
528 I2CCtrlRegs.prescaler_hi -> Seq(RegField(8, prescaler.hi)),
529 I2CCtrlRegs.control -> control.elements.map{ case(name, e) => RegField(e.getWidth, e.asInstanceOf[UInt]) }.toSeq,
530 I2CCtrlRegs.data -> Seq(RegField(8, r = RegReadFn(receivedData), w = RegWriteFn(transmitData))),
531 I2CCtrlRegs.cmd_status -> Seq(RegField(8, r = RegReadFn(status.asUInt), w = RegWriteFn(nextCmd)))
534 // tie off unused bits
535 control.reserved := 0.U
537 status.reserved := 0.U
539 interrupts(0) := status.irqFlag & control.intEn
542 // Copied from UART.scala
543 class Majority(in: Set[Bool]) {
544 private val n = (in.size >> 1) + 1
545 private val clauses = in.subsets(n).map(_.reduce(_ && _))
546 val out = clauses.reduce(_ || _)
550 // Magic TL2 Incantation to create a TL2 Slave
551 class TLI2C(c: I2CConfig)(implicit p: Parameters)
552 extends TLRegisterRouter(c.address, interrupts = 1, beatBytes = p(PeripheryBusConfig).beatBytes)(
553 new TLRegBundle(c, _) with I2CBundle)(
554 new TLRegModule(c, _, _) with I2CModule)