bus.hh revision 10800
112863Sgabeblack@google.com/*
212863Sgabeblack@google.com * Copyright (c) 2012 ARM Limited
312863Sgabeblack@google.com * All rights reserved
412863Sgabeblack@google.com *
512863Sgabeblack@google.com * The license below extends only to copyright in the software and shall
612863Sgabeblack@google.com * not be construed as granting a license to any other intellectual
712863Sgabeblack@google.com * property including but not limited to intellectual property relating
812863Sgabeblack@google.com * to a hardware implementation of the functionality of the software
912863Sgabeblack@google.com * licensed hereunder.  You may use the software subject to the license
1012863Sgabeblack@google.com * terms below provided that you ensure that this notice is replicated
1112863Sgabeblack@google.com * unmodified and in its entirety in all distributions of the software,
1212863Sgabeblack@google.com * modified or unmodified, in source code or in binary form.
1312863Sgabeblack@google.com *
1412863Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
1512863Sgabeblack@google.com * modification, are permitted provided that the following conditions are
1612863Sgabeblack@google.com * met: redistributions of source code must retain the above copyright
1712863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer;
1812863Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright
1912863Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the
2012863Sgabeblack@google.com * documentation and/or other materials provided with the distribution;
2112863Sgabeblack@google.com * neither the name of the copyright holders nor the names of its
2212863Sgabeblack@google.com * contributors may be used to endorse or promote products derived from
2312863Sgabeblack@google.com * this software without specific prior written permission.
2412863Sgabeblack@google.com *
2512863Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2612863Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2712863Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2812863Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2912863Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
3012863Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
3112863Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
3212863Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
3312950Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3413046Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3513035Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3613035Sgabeblack@google.com *
3713035Sgabeblack@google.com * Authors: Peter Enns
3813053Sgabeblack@google.com */
3912950Sgabeblack@google.com
4012950Sgabeblack@google.com
4112950Sgabeblack@google.com/** @file
4212950Sgabeblack@google.com * Implementiation of an i2c bus
4313053Sgabeblack@google.com */
4413053Sgabeblack@google.com
4513053Sgabeblack@google.com#ifndef __DEV_I2CBUS__
4613053Sgabeblack@google.com#define __DEV_I2CBUS__
4713059Sgabeblack@google.com
4813053Sgabeblack@google.com#include <map>
4913053Sgabeblack@google.com
5013053Sgabeblack@google.com#include "dev/i2cdev.hh"
5112950Sgabeblack@google.com#include "dev/io_device.hh"
5212863Sgabeblack@google.com#include "params/I2CBus.hh"
5312863Sgabeblack@google.com
5413035Sgabeblack@google.comclass I2CBus : public BasicPioDevice
5513035Sgabeblack@google.com{
5613035Sgabeblack@google.com  protected:
5713035Sgabeblack@google.com
5813035Sgabeblack@google.com    enum I2CState {
5913035Sgabeblack@google.com        IDLE,
6013035Sgabeblack@google.com        RECEIVING_ADDR,
6113035Sgabeblack@google.com        RECEIVING_DATA,
6213035Sgabeblack@google.com        SENDING_DATA,
6313035Sgabeblack@google.com    };
6413035Sgabeblack@google.com
6513035Sgabeblack@google.com    /**
6613035Sgabeblack@google.com     * Read [and Set] serial control bits:
6713035Sgabeblack@google.com     * Bit [0] is SCL
6813035Sgabeblack@google.com     * Bit [1] is SDA
6913035Sgabeblack@google.com     *
7013035Sgabeblack@google.com     * http://infocenter.arm.com/help/topic/com.arm.doc.dui0440b/Bbajdjeg.html
7112863Sgabeblack@google.com     */
7212863Sgabeblack@google.com    static const int SB_CONTROLS = 0x0;
7312863Sgabeblack@google.com    /** Clear control bits. Analogous to SB_CONTROLS */
7412863Sgabeblack@google.com    static const int SB_CONTROLC = 0x4;
7512950Sgabeblack@google.com
7612950Sgabeblack@google.com    /** I2C clock wire (0, 1). */
7712863Sgabeblack@google.com    uint8_t scl;
7813035Sgabeblack@google.com    /** I2C data wire (0, 1) */
7913035Sgabeblack@google.com    uint8_t sda;
8012863Sgabeblack@google.com
8112863Sgabeblack@google.com    /**
8212950Sgabeblack@google.com     * State used by I2CBus::write to determine what stage of an i2c
8312982Sgabeblack@google.com     * transmission it is currently in.
8412982Sgabeblack@google.com     */
8512950Sgabeblack@google.com    enum I2CState state;
8612863Sgabeblack@google.com
8712950Sgabeblack@google.com    /**
8812950Sgabeblack@google.com     * Order of the bit of the current message that is being sent or
8912950Sgabeblack@google.com     * received (0 - 7).
9012950Sgabeblack@google.com     */
9112950Sgabeblack@google.com    int currBit;
9212950Sgabeblack@google.com
9312950Sgabeblack@google.com    /**
9412950Sgabeblack@google.com     * Key used to access a device in the slave devices map. This
9512950Sgabeblack@google.com     * is the same address that is specified in kernel board
9612950Sgabeblack@google.com     * initialization code (e.g., arch/arm/mach-realview/core.c).
9712950Sgabeblack@google.com     */
9812950Sgabeblack@google.com    uint8_t i2cAddr;
9912950Sgabeblack@google.com
10012950Sgabeblack@google.com    /** 8-bit buffer used to send and receive messages bit by bit. */
10112950Sgabeblack@google.com    uint8_t message;
10212950Sgabeblack@google.com
10312950Sgabeblack@google.com    /**
10412950Sgabeblack@google.com     * All the slave i2c devices that are connected to this
10512950Sgabeblack@google.com     * bus. Each device has an address that points to the actual
10612950Sgabeblack@google.com     * device.
10712950Sgabeblack@google.com     */
10812950Sgabeblack@google.com    std::map<uint8_t, I2CDevice*> devices;
10912950Sgabeblack@google.com
11012863Sgabeblack@google.com    /**
11113035Sgabeblack@google.com     * Update data (sda) and clock (scl) to match any transitions
11213035Sgabeblack@google.com     * specified by pkt.
11313053Sgabeblack@google.com     *
11413053Sgabeblack@google.com     * @param pkt memory request packet
11513059Sgabeblack@google.com     */
11612863Sgabeblack@google.com    void updateSignals(PacketPtr pkt);
11712863Sgabeblack@google.com
11812950Sgabeblack@google.com    /**
11913079Sgabeblack@google.com     * Clock set check
12012950Sgabeblack@google.com     *
12112863Sgabeblack@google.com     * @param pkt memory request packet
12213045Sgabeblack@google.com     * @return true if pkt indicates that scl transition from 0 to 1
12313045Sgabeblack@google.com     */
12413045Sgabeblack@google.com    bool isClockSet(PacketPtr pkt) const;
12513046Sgabeblack@google.com
12612982Sgabeblack@google.com    /**
12712863Sgabeblack@google.com     * i2c start signal check
12812863Sgabeblack@google.com     *
12912863Sgabeblack@google.com     * @param pkt memory request packet
130     * @return true if pkt indicates a new transmission
131     */
132    bool isStart(PacketPtr pkt) const;
133
134    /**
135     * i2c end signal check
136     *
137     * @param pkt memory request packet
138     * @return true if pkt indicates stopping the current transmission
139     */
140    bool isEnd(PacketPtr pkt) const;
141
142  public:
143
144    I2CBus(const I2CBusParams* p);
145
146    virtual Tick read(PacketPtr pkt);
147    virtual Tick write(PacketPtr pkt);
148
149    virtual void serialize(std::ostream& os);
150    virtual void unserialize(Checkpoint* cp, const std::string& section);
151};
152
153#endif //__DEV_I2CBUS
154