1/*
2 * Copyright (c) 2008 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 */
30
31#ifndef __DEV_X86_I8254_HH__
32#define __DEV_X86_I8254_HH__
33
34#include "dev/intel_8254_timer.hh"
35#include "dev/intpin.hh"
36#include "dev/io_device.hh"
37#include "params/I8254.hh"
38
39namespace X86ISA
40{
41
42class I8254 : public BasicPioDevice
43{
44  protected:
45    Tick latency;
46    class X86Intel8254Timer : public Intel8254Timer
47    {
48      protected:
49        I8254 * parent;
50
51        void
52        counterInterrupt(unsigned int num)
53        {
54            parent->counterInterrupt(num);
55        }
56
57      public:
58        X86Intel8254Timer(const std::string &name, I8254 * _parent) :
59            Intel8254Timer(_parent, name), parent(_parent)
60        {}
61    };
62
63
64    X86Intel8254Timer pit;
65
66    std::vector<IntSourcePin<I8254> *> intPin;
67
68    void counterInterrupt(unsigned int num);
69
70  public:
71    typedef I8254Params Params;
72
73    Port &
74    getPort(const std::string &if_name, PortID idx=InvalidPortID) override
75    {
76        if (if_name == "int_pin")
77            return *intPin.at(idx);
78        else
79            return BasicPioDevice::getPort(if_name, idx);
80    }
81
82    const Params *
83    params() const
84    {
85        return dynamic_cast<const Params *>(_params);
86    }
87
88    I8254(Params *p) : BasicPioDevice(p, 4), latency(p->pio_latency),
89            pit(p->name, this)
90    {
91        for (int i = 0; i < p->port_int_pin_connection_count; i++) {
92            intPin.push_back(new IntSourcePin<I8254>(csprintf(
93                            "%s.int_pin[%d]", name(), i), i, this));
94        }
95    }
96    Tick read(PacketPtr pkt) override;
97
98    Tick write(PacketPtr pkt) override;
99
100    bool
101    outputHigh(unsigned int num)
102    {
103        return pit.outputHigh(num);
104    }
105
106    uint8_t
107    readCounter(unsigned int num)
108    {
109        return pit.readCounter(num);
110    }
111
112    void
113    writeCounter(unsigned int num, const uint8_t data)
114    {
115        pit.writeCounter(num, data);
116    }
117
118    void
119    writeControl(uint8_t val)
120    {
121        pit.writeControl(val);
122    }
123
124    void serialize(CheckpointOut &cp) const override;
125    void unserialize(CheckpointIn &cp) override;
126
127    void startup() override;
128
129};
130
131} // namespace X86ISA
132
133#endif //__DEV_X86_SOUTH_BRIDGE_I8254_HH__
134