summaryrefslogtreecommitdiffstats
path: root/src/usr/sio/siodd.C
blob: 8bbc59f5b089a6215e6b855f2471f878d621107c (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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* IBM_PROLOG_BEGIN_TAG                                                   */
/* This is an automatically generated prolog.                             */
/*                                                                        */
/* $Source: src/usr/sio/siodd.C $                                         */
/*                                                                        */
/* OpenPOWER HostBoot Project                                             */
/*                                                                        */
/* Contributors Listed Below - COPYRIGHT 2015                             */
/* [+] 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 <sio/sio.H>
#include "siodd.H"
#include <errl/errlmanager.H>
#include <errl/errlentry.H>
#include <trace/interface.H>
#include <sys/sync.h>
#include <devicefw/userif.H>
#include <lpc/lpcif.H>
#include <kernel/console.H>
#include <devicefw/driverif.H>
#include <initservice/bootconfigif.H>
#include <sys/time.h>
#include <hbotcompid.H>
#include <stdarg.h>
#include <targeting/common/target.H>
#include <lpc/lpc_reasoncodes.H>

#include <console/consoleif.H>

// Trace definition
trace_desc_t* g_trac_sio = NULL;
TRAC_INIT(&g_trac_sio, SIO_COMP_NAME, 2*KILOBYTE, TRACE::BUFFER_SLOW); //2K

/**
 * This function performs an SIO Read operation. It follows a pre-defined
 * prototype functions in order to be registered with the device-driver
 * framework.
 *
 * @param[in]   i_opTypeOperation type, see DeviceFW::OperationType in
 * driverif.H
 * @param[in]   i_targetSIO target
 * @param[in/out] io_buffer Read: Pointer to output data storage
 *                          Write: Pointer to input data storage
 * @param[in/out] io_buflen Input: size of io_buffer (in bytes)
 *                          Output: Read: Size of output data
 *                                  Write: Size of data written
 * @param[in]   i_accessTypeDeviceFW::AccessType enum (usrif.H)
 * @param[in]   i_args  This is an argument list for DD framework.
 * @return  errlHndl_t
 */
errlHndl_t SIORead(DeviceFW::OperationType i_opType,
                   TARGETING::Target* i_target,
                   void* io_buffer,
                   size_t& io_buflen,
                   int64_t i_accessType, va_list i_args)
{
    assert(i_target == TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);
    errlHndl_t l_err = NULL;
    uint8_t l_dev = va_arg(i_args,uint64_t);
    uint8_t l_addr = va_arg(i_args,uint64_t);
    l_err = Singleton<SioDD>::instance().readSIO(i_target, l_dev, l_addr,
    static_cast<uint8_t*>(io_buffer));
    return l_err;
}

/**
 * This function performs an SIO Write operation. It follows a pre-defined
 * prototype functions in order to be registered with the device-driver
 * framework.
 *
 * param[in]   i_opType Operation type, see DeviceFW::OperationType in
 * driverif.H
 * @param[in]   i_targetSIO target
 * @param[in/out] io_buffer Read: Pointer to output data storage
 *                          Write: Pointer to input data storage
 * @param[in/out] io_buflen Input: size of io_buffer (in bytes)
 *                          Output: Read: Size of output data
 *                                  Write: Size of data written
 * @param[in]   i_accessTypeDeviceFW::AccessType enum (usrif.H)
 * @param[in]   i_args  This is an argument list for DD framework.
 * @return  errlHndl_t
 */
errlHndl_t SIOWrite(DeviceFW::OperationType i_opType,
                    TARGETING::Target* i_target,
                    void* io_buffer,
                    size_t& io_buflen,
                    int64_t i_accessType,
                    va_list i_args)
{
    assert(i_target == TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);
    errlHndl_t l_err = NULL;
    uint8_t l_dev = va_arg(i_args,uint64_t);
    uint8_t l_addr = va_arg(i_args,uint64_t);
    l_err = Singleton<SioDD>::instance().writeSIO(i_target, l_dev, l_addr,
            static_cast<uint8_t*>(io_buffer));
    return l_err;
}

/**
 * This function performs an LPC to AHB read operation using superIO accesses.
 * It follows a pre-defined prototype functions in order to be registered
 * with the device-driver framework.
 *
 * @param[in]   i_opType Operation type, see DeviceFW::OperationType in
 * driverif.H
 * @param[in]   i_targetSIO target
 * @param[in/out] io_buffer Read: Pointer to output data storage
 *                          Write: Pointer to input data storage
 * @param[in/out] io_buflen Input: size of io_buffer (in bytes)
 *                          Output: Read: Size of output data
 *                                  Write: Size of data written
 * @param[in]   i_accessTypeDeviceFW::AccessType enum (usrif.H)
 * @param[in]   i_args  This is an argument list for DD framework.
 * @return  errlHndl_t
 */
errlHndl_t ahbSioReadDD(DeviceFW::OperationType i_opType,
                          TARGETING::Target* i_target,
                          void* io_buffer,
                          size_t& io_buflen,
                          int64_t i_accessType,
                          va_list i_args)
{
    assert(i_target == TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);
    errlHndl_t l_err = NULL;
    uint32_t l_reg = va_arg(i_args,uint64_t);
    l_err = Singleton<SioDD>::instance().ahbSioRead(i_target, l_reg,
            static_cast<uint32_t*>(io_buffer));
    return l_err;
}

/**
 * This function performs an LPC to AHB read operation using superIO accesses.
 * It follows a pre-defined prototype functions in order to be registered
 * with the device-driver framework.
 *
 * @param[in]   i_opType Operation type, see DeviceFW::OperationType in
 * driverif.H
 * @param[in]   i_targetSIO target
 * @param[in/out] io_buffer Read: Pointer to output data storage
 *                          Write: Pointer to input data storage
 * @param[in/out] io_buflen Input: size of io_buffer (in bytes)
 *                          Output: Read: Size of output data
 *                                  Write: Size of data written
 * @param[in]   i_accessTypeDeviceFW::AccessType enum (usrif.H)
 * @param[in]   i_args  This is an argument list for DD framework.
 * @return  errlHndl_t
 */
errlHndl_t ahbSioWriteDD(DeviceFW::OperationType i_opType,
                         TARGETING::Target* i_target,
                         void* io_buffer,
                         size_t& io_buflen,
                         int64_t i_accessType,
                         va_list i_args)
{
    assert(i_target == TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);
    errlHndl_t l_err = NULL;
    uint32_t l_reg = va_arg(i_args,uint64_t);
    l_err = Singleton<SioDD>::instance().ahbSioWrite(i_target, l_reg,
            static_cast<uint32_t*>(io_buffer));
    return l_err;
}

// Register SIO access functions to DD framework
DEVICE_REGISTER_ROUTE( DeviceFW::READ,
                       DeviceFW::SIO,
                       TARGETING::TYPE_PROC,
                       SIORead );
DEVICE_REGISTER_ROUTE( DeviceFW::WRITE,
                       DeviceFW::SIO,
                       TARGETING::TYPE_PROC,
                       SIOWrite );

// Register AHB_SIO access functions to DD framework
DEVICE_REGISTER_ROUTE( DeviceFW::READ,
                       DeviceFW::AHB_SIO,
                       TARGETING::TYPE_PROC,
                       ahbSioReadDD );
DEVICE_REGISTER_ROUTE( DeviceFW::WRITE,
                       DeviceFW::AHB_SIO,
                       TARGETING::TYPE_PROC,
                       ahbSioWriteDD );

errlHndl_t SIO::isAvailable(bool& available)
{
    uint8_t l_byte = SIO::SIO_PASSWORD_REG;
    size_t l_len = sizeof(uint8_t);
    errlHndl_t l_err = NULL;

    l_err = deviceWrite(TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL,
                        &l_byte, l_len,
                        DEVICE_LPC_ADDRESS(LPC::TRANS_IO, SIO::SIO_ADDR_REG_2E));

    if (l_err)
    {
        /* FIXME: The implementation assumes that any error indicates that the
         * SIO device is not available. This, generally, is a terribe
         * assumption. We should instead look for the specific failure, which
         * is a LPC SYNC No Response (and this is what we see in skiboot).
         * Currently there are open questions about the hardware behaviour as
         * observed by hostboot: We see an OPBM Error Acknowledge state when we
         * try to access an absent SIO device, but no error state is present in
         * the LPCHC status register.
         *
         * We retain the interface of returning an errlHndl_t to future-proof
         * the code. The caller should commit the errl if it is valid.
         */
        TRACFCOMP(g_trac_sio,
                  "Received error during SIO availability test, assuming "
                  "absent. Reason code: 0x%x, user data: [0x%8x, 0x%8x]",
                  l_err->reasonCode(), l_err->getUserData1(),
                  l_err->getUserData2());
        available = false;
        delete l_err;
        l_err = NULL;
    }
    else
    {
        available = true;
    }

    return l_err;
}

//function to unlock superIO password register
errlHndl_t SioDD::unlock_SIO(TARGETING::Target* i_target)
{
    uint8_t l_byte = SIO::SIO_PASSWORD_REG;
    size_t l_len = sizeof(uint8_t);
    errlHndl_t l_err = NULL;
    int again = 1;

    do
    {
    // Unlock the SIO registers (write 0xA5 password to offset 0x2E two times)
    l_err = deviceWrite(i_target, &l_byte, l_len,
                        DEVICE_LPC_ADDRESS(LPC::TRANS_IO,
                                           SIO::SIO_ADDR_REG_2E));
    } while(!l_err && again--);

    return l_err;
}

//SioDD constructor
SioDD::SioDD(TARGETING::Target* i_target)
{
    assert(i_target == TARGETING::MASTER_PROCESSOR_CHIP_TARGET_SENTINEL);
    mutex_init(&iv_sio_mutex);
    iv_prev_dev = 0x00;

    errlHndl_t err = unlock_SIO(i_target);
    bool failed = (err != NULL);
    delete err;

    /* Unlocked very early, so make some noise if we fail */
    if (failed)
    {
        printk("SuperIO unlock failed! Expect future errors\n");
    }
}

//SioDD destructor
SioDD::~SioDD()
{
    mutex_destroy(&iv_sio_mutex);
}

//internal write function
errlHndl_t SioDD::_writeSIO(TARGETING::Target* i_target,
        uint8_t i_reg, uint8_t* i_data)
{
    errlHndl_t l_err = NULL;
    do
    {
        size_t l_len = sizeof(uint8_t);
        l_err = deviceWrite(i_target,
                            &i_reg,
                            l_len,
                            DEVICE_LPC_ADDRESS(LPC::TRANS_IO, SIO::SIO_ADDR_REG_2E));
        if(l_err) { break; }
        l_err = deviceWrite(i_target,
                            i_data,
                            l_len,
                            DEVICE_LPC_ADDRESS(LPC::TRANS_IO, SIO::SIO_DATA_REG_2F));
    } while(0);
    return l_err;
}

//internal read function
errlHndl_t SioDD::_readSIO(TARGETING::Target* i_target,
                        uint8_t i_reg, uint8_t*  o_byte)
{
    errlHndl_t l_err = NULL;
    size_t l_len = sizeof(uint8_t);
    do
    {
        l_err =  deviceWrite(i_target,
                             &i_reg,
                             l_len,
                             DEVICE_LPC_ADDRESS(LPC::TRANS_IO, SIO::SIO_ADDR_REG_2E));
        if(l_err) { break; }
        l_err =  deviceRead(i_target,
                            o_byte,
                            l_len,
                            DEVICE_LPC_ADDRESS(LPC::TRANS_IO, SIO::SIO_DATA_REG_2F));
    } while(0);
    return l_err;
}

//function to change logical device in SIO
errlHndl_t SioDD::changeDevice(TARGETING::Target* i_target, uint8_t i_dev)
{
    return _writeSIO(i_target, SIO::SIO_DEVICE_SELECT_REG, &i_dev);
}

//function to read from SIO register
errlHndl_t SioDD::readSIO(TARGETING::Target* i_target, uint8_t i_dev,
                             uint8_t i_reg, uint8_t* o_byte)
{
    mutex_lock(&iv_sio_mutex);
    errlHndl_t l_err = NULL;

    if(iv_prev_dev!=i_dev)
    {
        l_err = changeDevice(i_target, i_dev);
        if(!l_err)
        {
            iv_prev_dev = i_dev;
            l_err = _readSIO(i_target, i_reg, o_byte);
        }
    }
    else
    {
        l_err = _readSIO(i_target, i_reg, o_byte);
    }
    mutex_unlock(&iv_sio_mutex);
    return l_err;
}


//function to write to SIO register
errlHndl_t SioDD::writeSIO(TARGETING::Target* i_target,
                        uint8_t i_dev, uint8_t i_reg, uint8_t* i_data)
{
    mutex_lock(&iv_sio_mutex);
    errlHndl_t l_err = NULL;
    if(iv_prev_dev!=i_dev)
    {
        l_err = changeDevice(i_target, i_dev);
        if(!l_err)
        {
            iv_prev_dev = i_dev;
            l_err = _writeSIO(i_target, i_reg, i_data);
        }
    }
    else
    {
        l_err = _writeSIO(i_target, i_reg, i_data);
    }
    mutex_unlock(&iv_sio_mutex);
    return l_err;
}

//function to translate address from LPC to AHB space
errlHndl_t SioDD::_ahbSioAddrPrep(TARGETING::Target* i_target,
                        uint32_t i_addr)
{
    errlHndl_t l_err = NULL;
    uint8_t l_dev = SIO::iLPC2AHB;
    uint8_t l_data;
    do
    {
        //select device 0x0D
        l_err = changeDevice(i_target, l_dev);
        if(l_err) { break; }

        //write to f0-f3
        for (size_t i = sizeof(i_addr); i>0; i--)
        {
            l_data = i_addr>>((i-1)*8);
            l_err = _writeSIO(i_target, (0xF3-(i-1)), &l_data);
            if( l_err ) { break; }
        }

        //byte length
        l_data = SIO::SIO_iLPC2AHB_LENGTH;
        l_err = _writeSIO(i_target, 0xF8, &l_data);
        if( l_err ) { break; }
    }
    while(0);
    return l_err;
}

errlHndl_t SioDD::_ahbSioRead(TARGETING::Target* i_target,
                        uint32_t i_reg, uint32_t* o_data)
{
    errlHndl_t l_err = NULL;
    uint8_t tmp_data = 0;
    do
    {
        l_err = _ahbSioAddrPrep(i_target, i_reg);
        if( l_err ) { break; }

        //trigger operation by reading from 0xFE
        l_err = _readSIO(i_target, 0xFE, &tmp_data);
        if( l_err ) { break; }
        uint8_t* ptr8 = (uint8_t*)(o_data);
        for( size_t i=0; i<sizeof(uint32_t); i++ )
        {
            l_err = _readSIO(i_target, 0xF4+i, &ptr8[i]);
            if(l_err) { break; }
        }
    }
    while(0);
    return l_err;
}

errlHndl_t SioDD::_ahbSioWrite(TARGETING::Target* i_target,
                        uint32_t i_reg, uint32_t* i_val)
{
    errlHndl_t l_err = NULL;
    uint8_t l_data;
    do
    {
        l_err = _ahbSioAddrPrep(i_target, i_reg);
        if( l_err ) { break; }

        uint8_t* ptr8 = reinterpret_cast<uint8_t*>(i_val);
        for (size_t i = 0; i<sizeof(uint32_t); i++)
        {
            l_err = _writeSIO(i_target, (0xF4+i), &ptr8[i]);
            if( l_err ) { break; }
        }
        //trigger operation by writing to 0xFE
        l_data = 0xCF;
        l_err = _writeSIO(i_target, 0xFE, &l_data);
        if( l_err ) { break; }
    }
    while(0);
    return l_err;
}

//function to perform AHB to SIO read
errlHndl_t SioDD::ahbSioRead(TARGETING::Target* i_target,
                        uint32_t i_reg, uint32_t* o_data)
{
    mutex_lock(&iv_sio_mutex);
    errlHndl_t l_err = NULL;
    l_err = _ahbSioRead(i_target, i_reg, o_data);
    mutex_unlock(&iv_sio_mutex);
    return l_err;
}

//function to perform AHB to SIO write
errlHndl_t SioDD::ahbSioWrite(TARGETING::Target* i_target,
                        uint32_t i_reg, uint32_t* i_val)
{
    mutex_lock(&iv_sio_mutex);
    errlHndl_t l_err = NULL;
    l_err = _ahbSioWrite(i_target, i_reg, i_val);
    mutex_unlock(&iv_sio_mutex);
    return l_err;
}

OpenPOWER on IntegriCloud