summaryrefslogtreecommitdiffstats
path: root/src/usr/gpio/gpiodd.C
blob: 559dab79e62484881e5136e29cb3d5588c0f3e47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/gpio/gpiodd.C $                                       */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2014,2018                        */
/* [+] Google Inc.                                                        */
/* [+] International Business Machines Corp.                              */
/*                                                                        */
/*                                                                        */
/* Licensed under the Apache License, Version 2.0 (the "License");        */
/* you may not use this file except in compliance with the License.       */
/* You may obtain a copy of the License at                                */
/*                                                                        */
/*     http://www.apache.org/licenses/LICENSE-2.0                         */
/*                                                                        */
/* Unless required by applicable law or agreed to in writing, software    */
/* distributed under the License is distributed on an "AS IS" BASIS,      */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or        */
/* implied. See the License for the specific language governing           */
/* permissions and limitations under the License.                         */
/*                                                                        */
/* IBM_PROLOG_END_TAG                                                     */

#include <trace/interface.H>
#include <errl/errlentry.H>
#include <errl/errlmanager.H>
#include <errl/errludtarget.H>
#include <errl/errludstring.H>
#include <targeting/common/targetservice.H>
#include <targeting/common/commontargeting.H>
#include <devicefw/driverif.H>
#include "gpiodd.H"
#include <gpio/gpioddreasoncodes.H>

trace_desc_t * g_trac_gpio = NULL;
TRAC_INIT( & g_trac_gpio, GPIO_COMP_NAME, KILOBYTE );

using namespace DeviceFW;

namespace GPIO
{

enum
{
    // Asserting that a GPIO Port Extender will never have more than an 8-bit
    // address as these devices never have more than a handful of control
    // registers.
    GPIO_ADDR_SIZE = 1,
};

// Link to device driver interface
DEVICE_REGISTER_ROUTE( DeviceFW::WILDCARD,
                       DeviceFW::GPIO,
                       TARGETING::TYPE_MEMBUF,
                       gpioPerformOp);

errlHndl_t gpioPerformOp(DeviceFW::OperationType i_opType,
                         TARGETING::Target * i_target,
                         void * io_buffer,
                         size_t & i_buflen,
                         int64_t i_accessType,
                         va_list i_args)
{
    errlHndl_t err = NULL;
    gpioAddr_t gpioInfo;

    gpioInfo.deviceType = va_arg( i_args, uint64_t );
    gpioInfo.portAddr   = va_arg( i_args, uint64_t );

    TRACDCOMP(g_trac_gpio, ENTER_MRK"gpioPerformOp(): "
              "optype %d deviceType %d portAddr %d",
              i_opType, gpioInfo.deviceType, gpioInfo.portAddr);

    do
    {
        err = gpioReadAttributes (i_target, gpioInfo);
        if( err )
        {
            break;
        }

        TARGETING::TargetService& ts = TARGETING::targetService();
        TARGETING::Target * i2c_master = ts.toTarget(gpioInfo.i2cMasterPath);

        if( i2c_master == NULL )
        {
            TRACFCOMP( g_trac_gpio,ERR_MRK"gpioPerformOp() - "
                       "I2C Target not found. Device type %d.",
                       gpioInfo.deviceType );
            /*@
             * @errortype
             * @reasoncode       GPIO_I2C_TARGET_NOT_FOUND
             * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid         GPIO_PERFORM_OP
             * @userdata1        Device type
             * @userdata2        HUID of target
             * @devdesc          Invalid GPIO device type
             * @custdesc         A problem occurred during the IPL
             *                   of the system.
             */
            err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          GPIO::GPIO_PERFORM_OP,
                                          GPIO::GPIO_I2C_TARGET_NOT_FOUND,
                                          gpioInfo.deviceType,
                                          TARGETING::get_huid(i_target),
                                          true /*Add HB SW Callout*/ );

            err->collectTrace( GPIO_COMP_NAME );
            break;
        }

        if( i_opType == DeviceFW::READ )
        {
            err = gpioRead(i2c_master,
                           io_buffer,
                           i_buflen,
                           gpioInfo);

            if( err )
            {
                break;
            }
        }
        else if (i_opType == DeviceFW::WRITE )
        {
            err = gpioWrite(i2c_master,
                            io_buffer,
                            i_buflen,
                            gpioInfo);

            if( err )
            {
                break;
            }
        }
        else
        {
            TRACFCOMP( g_trac_gpio,ERR_MRK"gpioPerformOp() - "
                       "Invalid OP type %d.",
                       i_opType );
            /*@
             * @errortype
             * @reasoncode       GPIO_INVALID_OP
             * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid         GPIO_PERFORM_OP
             * @userdata1        OP type
             * @userdata2        HUID of target
             * @devdesc          Invalid GPIO device type
             * @custdesc         A problem occurred during the IPL
             *                   of the system.
             */
            err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          GPIO::GPIO_PERFORM_OP,
                                          GPIO::GPIO_INVALID_OP,
                                          i_opType,
                                          TARGETING::get_huid(i_target),
                                          true /*Add HB SW Callout*/ );

            err->collectTrace( GPIO_COMP_NAME );
            break;
        }

    } while (0);

    return err;
}

errlHndl_t gpioRead( TARGETING::Target * i_target,
                     void * o_buffer,
                     size_t & io_buflen,
                     gpioAddr_t & i_gpioInfo)
{
    errlHndl_t err = NULL;

    // This i2c interface writes the gpio portAddr to the device
    // then reads the value of the port w/o a stop bit in between ops
    err = deviceOp( DeviceFW::READ,
                    i_target,
                    o_buffer,
                    io_buflen,
                    DEVICE_I2C_ADDRESS_OFFSET
                    ( i_gpioInfo.i2cPort,
                      i_gpioInfo.engine,
                      i_gpioInfo.i2cDeviceAddr,
                      GPIO_ADDR_SIZE,
                      reinterpret_cast<uint8_t*>(&(i_gpioInfo.portAddr)),
                      i_gpioInfo.i2cMuxBusSelector,
                      &(i_gpioInfo.i2cMuxPath)
                    )
                  );
    if(err)
    {
        err->collectTrace( GPIO_COMP_NAME );
    }

    return err;
}

errlHndl_t gpioWrite ( TARGETING::Target * i_target,
                     void * i_buffer,
                     size_t i_buflen,
                     gpioAddr_t & i_gpioInfo)
{
    errlHndl_t err = NULL;

    size_t cmdlen = GPIO_ADDR_SIZE + i_buflen;
    uint8_t cmd[cmdlen];
    cmd[0] = i_gpioInfo.portAddr;
    memcpy(&(cmd[GPIO_ADDR_SIZE]), i_buffer, i_buflen);

    err = deviceOp( DeviceFW::WRITE,
                    i_target,
                    &cmd,
                    cmdlen,
                    DEVICE_I2C_ADDRESS
                    ( i_gpioInfo.i2cPort,
                      i_gpioInfo.engine,
                      i_gpioInfo.i2cDeviceAddr,
                      i_gpioInfo.i2cMuxBusSelector,
                      &(i_gpioInfo.i2cMuxPath)
                    )
                  );
    if(err)
    {
        err->collectTrace( GPIO_COMP_NAME );
    }

    return err;
}


errlHndl_t gpioReadAttributes ( TARGETING::Target * i_target,
                                gpioAddr_t & io_gpioInfo)
{
    errlHndl_t err = NULL;

    TARGETING::GpioInfo gpioData;

    bool attrReadErr = false;


    switch(io_gpioInfo.deviceType)
    {
        case PCA95X_GPIO:
            if( !( i_target->
                   tryGetAttr<TARGETING::ATTR_GPIO_INFO>( gpioData ) ) )
            {
                attrReadErr = true;
            }
            break;
        default:

            TRACFCOMP( g_trac_gpio,ERR_MRK"gpioReadAttributes() - "
                       "Invalid device type (%d) to read attributes from!",
                       io_gpioInfo.deviceType );
            /*@
             * @errortype
             * @reasoncode       GPIO_INVALID_DEVICE_TYPE
             * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
             * @moduleid         GPIO_READATTRIBUTES
             * @userdata1        Device type
             * @userdata2        HUID of target
             * @devdesc          Invalid GPIO device type
             * @custdesc         A problem occurred during the IPL
             *                   of the system.
             */
            err = new ERRORLOG::ErrlEntry(ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                          GPIO::GPIO_READATTRIBUTES,
                                          GPIO::GPIO_INVALID_DEVICE_TYPE,
                                          io_gpioInfo.deviceType,
                                          TARGETING::get_huid(i_target),
                                          true /*Add HB SW Callout*/ );

            err->collectTrace( GPIO_COMP_NAME );

            break;
    }

    if(attrReadErr)
    {
        TRACFCOMP( g_trac_gpio,
                   ERR_MRK"gpioReadAttributes() - ERROR reading "
                   "attributes for device type %d!",
                   io_gpioInfo.deviceType );

        /*@
         * @errortype
         * @reasoncode       GPIO_ATTR_INFO_NOT_FOUND
         * @severity         ERRORLOG::ERRL_SEV_UNRECOVERABLE
         * @moduleid         GPIO_READATTRIBUTES
         * @userdata1        HUID of target
         * @userdata2        GPIO device type
         * @devdesc          GPIO device attribute was not found
         * @custdesc         A problem occurred during the IPL
         *                   of the system.
         */
        err = new ERRORLOG::ErrlEntry(
                                      ERRORLOG::ERRL_SEV_UNRECOVERABLE,
                                      GPIO::GPIO_READATTRIBUTES,
                                      GPIO::GPIO_ATTR_INFO_NOT_FOUND,
                                      TARGETING::get_huid(i_target),
                                      io_gpioInfo.deviceType,
                                      true /*Add HB SW Callout*/);


        err->collectTrace( GPIO_COMP_NAME );
    }

    if( !err )
    {
        io_gpioInfo.i2cMasterPath = gpioData.i2cMasterPath;
        io_gpioInfo.engine        = gpioData.engine;
        io_gpioInfo.i2cPort       = gpioData.port;
        io_gpioInfo.i2cDeviceAddr = gpioData.devAddr;
        io_gpioInfo.i2cMuxBusSelector = gpioData.i2cMuxBusSelector;
        io_gpioInfo.i2cMuxPath    = gpioData.i2cMuxPath;
    }

    return err;
}

}; // end namespace GPIO

OpenPOWER on IntegriCloud