~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/skiboot/hw/p8-i2c.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2013-2014 IBM Corp.
 
2
 *
 
3
 * Licensed under the Apache License, Version 2.0 (the "License");
 
4
 * you may not use this file except in compliance with the License.
 
5
 * You may obtain a copy of the License at
 
6
 *
 
7
 * http://www.apache.org/licenses/LICENSE-2.0
 
8
 *
 
9
 * Unless required by applicable law or agreed to in writing, software
 
10
 * distributed under the License is distributed on an "AS IS" BASIS,
 
11
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
12
 * implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
#undef DEBUG
 
18
 
 
19
#include <opal.h>
 
20
#include <skiboot.h>
 
21
#include <mem_region-malloc.h>
 
22
#include <lock.h>
 
23
#include <chip.h>
 
24
#include <i2c.h>
 
25
#include <xscom.h>
 
26
#include <timebase.h>
 
27
#include <timer.h>
 
28
#include <opal-msg.h>
 
29
#include <errorlog.h>
 
30
#include <centaur.h>
 
31
 
 
32
DEFINE_LOG_ENTRY(OPAL_RC_I2C_INIT, OPAL_PLATFORM_ERR_EVT, OPAL_I2C,
 
33
                 OPAL_IO_SUBSYSTEM, OPAL_PREDICTIVE_ERR_DEGRADED_PERF,
 
34
                 OPAL_NA);
 
35
DEFINE_LOG_ENTRY(OPAL_RC_I2C_START_REQ, OPAL_INPUT_OUTPUT_ERR_EVT, OPAL_I2C,
 
36
                 OPAL_IO_SUBSYSTEM, OPAL_INFO, OPAL_NA);
 
37
DEFINE_LOG_ENTRY(OPAL_RC_I2C_TIMEOUT, OPAL_INPUT_OUTPUT_ERR_EVT, OPAL_I2C,
 
38
                 OPAL_IO_SUBSYSTEM, OPAL_INFO, OPAL_NA);
 
39
DEFINE_LOG_ENTRY(OPAL_RC_I2C_TRANSFER, OPAL_INPUT_OUTPUT_ERR_EVT, OPAL_I2C,
 
40
                 OPAL_IO_SUBSYSTEM, OPAL_INFO, OPAL_NA);
 
41
DEFINE_LOG_ENTRY(OPAL_RC_I2C_RESET, OPAL_INPUT_OUTPUT_ERR_EVT, OPAL_I2C,
 
42
                 OPAL_IO_SUBSYSTEM, OPAL_INFO, OPAL_NA);
 
43
 
 
44
#ifdef DEBUG
 
45
#define DBG(fmt...) prlog(PR_ERR, "I2C: " fmt)
 
46
#define I2C_TIMEOUT_IRQ_MS              100     /* 100ms/byte timeout */
 
47
#define I2C_TIMEOUT_POLL_MS             4000    /* 4s/byte timeout */
 
48
#else
 
49
#define DBG(fmt...) prlog(PR_TRACE, "I2C: " fmt)
 
50
#define I2C_TIMEOUT_IRQ_MS              1       /* 1ms/byte timeout */
 
51
#define I2C_TIMEOUT_POLL_MS             4000    /* 4s/byte timeout */
 
52
#endif
 
53
 
 
54
/* How long to keep the sensor cache disabled after an access
 
55
 * in milliseconds
 
56
 */
 
57
#define SENSOR_CACHE_EN_DELAY 10
 
58
 
 
59
#define USEC_PER_SEC            1000000
 
60
#define USEC_PER_MSEC           1000
 
61
#define I2C_RESET_DELAY_MS      5 /* 5 msecs */
 
62
#define I2C_FIFO_HI_LVL         4
 
63
#define I2C_FIFO_LO_LVL         4
 
64
 
 
65
/*
 
66
 * I2C registers set.
 
67
 * Below is the offset of registers from base which is stored in the
 
68
 * 'struct p8_i2c_master'
 
69
 */
 
70
 
 
71
/* I2C FIFO register */
 
72
#define I2C_FIFO_REG                    0x4
 
73
#define I2C_FIFO                        PPC_BITMASK(0, 7)
 
74
 
 
75
/* I2C command register */
 
76
#define I2C_CMD_REG                     0x5
 
77
#define I2C_CMD_WITH_START              PPC_BIT(0)
 
78
#define I2C_CMD_WITH_ADDR               PPC_BIT(1)
 
79
#define I2C_CMD_READ_CONT               PPC_BIT(2)
 
80
#define I2C_CMD_WITH_STOP               PPC_BIT(3)
 
81
#define I2C_CMD_DEV_ADDR                PPC_BITMASK(8, 14)
 
82
#define I2C_CMD_READ_NOT_WRITE          PPC_BIT(15)
 
83
#define I2C_CMD_LEN_BYTES               PPC_BITMASK(16, 31)
 
84
#define I2C_MAX_TFR_LEN                 0xfff0ull
 
85
 
 
86
/* I2C mode register */
 
87
#define I2C_MODE_REG                    0x6
 
88
#define I2C_MODE_BIT_RATE_DIV           PPC_BITMASK(0, 15)
 
89
#define I2C_MODE_PORT_NUM               PPC_BITMASK(16, 21)
 
90
#define I2C_MODE_ENHANCED               PPC_BIT(28)
 
91
#define I2C_MODE_DIAGNOSTIC             PPC_BIT(29)
 
92
#define I2C_MODE_PACING_ALLOW           PPC_BIT(30)
 
93
#define I2C_MODE_WRAP                   PPC_BIT(31)
 
94
 
 
95
/* I2C watermark register */
 
96
#define I2C_WATERMARK_REG               0x7
 
97
#define I2C_WATERMARK_HIGH              PPC_BITMASK(16, 19)
 
98
#define I2C_WATERMARK_LOW               PPC_BITMASK(24, 27)
 
99
 
 
100
/* I2C interrupt mask, condition and interrupt registers */
 
101
#define I2C_INTR_MASK_REG               0x8
 
102
#define I2C_INTR_COND_REG               0x9
 
103
#define I2C_INTR_REG                    0xa
 
104
#define I2C_INTR_ALL                    PPC_BITMASK(16, 31)
 
105
#define I2C_INTR_INVALID_CMD            PPC_BIT(16)
 
106
#define I2C_INTR_LBUS_PARITY_ERR        PPC_BIT(17)
 
107
#define I2C_INTR_BKEND_OVERRUN_ERR      PPC_BIT(18)
 
108
#define I2C_INTR_BKEND_ACCESS_ERR       PPC_BIT(19)
 
109
#define I2C_INTR_ARBT_LOST_ERR          PPC_BIT(20)
 
110
#define I2C_INTR_NACK_RCVD_ERR          PPC_BIT(21)
 
111
#define I2C_INTR_DATA_REQ               PPC_BIT(22)
 
112
#define I2C_INTR_CMD_COMP               PPC_BIT(23)
 
113
#define I2C_INTR_STOP_ERR               PPC_BIT(24)
 
114
#define I2C_INTR_I2C_BUSY               PPC_BIT(25)
 
115
#define I2C_INTR_NOT_I2C_BUSY           PPC_BIT(26)
 
116
#define I2C_INTR_SCL_EQ_1               PPC_BIT(28)
 
117
#define I2C_INTR_SCL_EQ_0               PPC_BIT(29)
 
118
#define I2C_INTR_SDA_EQ_1               PPC_BIT(30)
 
119
#define I2C_INTR_SDA_EQ_0               PPC_BIT(31)
 
120
 
 
121
/* I2C status register */
 
122
#define I2C_RESET_I2C_REG               0xb
 
123
#define I2C_RESET_ERRORS                0xc
 
124
#define I2C_STAT_REG                    0xb
 
125
#define I2C_STAT_INVALID_CMD            PPC_BIT(0)
 
126
#define I2C_STAT_LBUS_PARITY_ERR        PPC_BIT(1)
 
127
#define I2C_STAT_BKEND_OVERRUN_ERR      PPC_BIT(2)
 
128
#define I2C_STAT_BKEND_ACCESS_ERR       PPC_BIT(3)
 
129
#define I2C_STAT_ARBT_LOST_ERR          PPC_BIT(4)
 
130
#define I2C_STAT_NACK_RCVD_ERR          PPC_BIT(5)
 
131
#define I2C_STAT_DATA_REQ               PPC_BIT(6)
 
132
#define I2C_STAT_CMD_COMP               PPC_BIT(7)
 
133
#define I2C_STAT_STOP_ERR               PPC_BIT(8)
 
134
#define I2C_STAT_UPPER_THRS             PPC_BITMASK(9, 15)
 
135
#define I2C_STAT_ANY_I2C_INTR           PPC_BIT(16)
 
136
#define I2C_STAT_PORT_HISTORY_BUSY      PPC_BIT(19)
 
137
#define I2C_STAT_SCL_INPUT_LEVEL        PPC_BIT(20)
 
138
#define I2C_STAT_SDA_INPUT_LEVEL        PPC_BIT(21)
 
139
#define I2C_STAT_PORT_BUSY              PPC_BIT(22)
 
140
#define I2C_STAT_INTERFACE_BUSY         PPC_BIT(23)
 
141
#define I2C_STAT_FIFO_ENTRY_COUNT       PPC_BITMASK(24, 31)
 
142
 
 
143
#define I2C_STAT_ANY_ERR (I2C_STAT_INVALID_CMD | I2C_STAT_LBUS_PARITY_ERR | \
 
144
                          I2C_STAT_BKEND_OVERRUN_ERR | \
 
145
                          I2C_STAT_BKEND_ACCESS_ERR | I2C_STAT_ARBT_LOST_ERR | \
 
146
                          I2C_STAT_NACK_RCVD_ERR | I2C_STAT_STOP_ERR)
 
147
 
 
148
/* Pseudo-status used for timeouts */
 
149
#define I2C_STAT_PSEUDO_TIMEOUT         PPC_BIT(63)
 
150
 
 
151
 
 
152
/* I2C extended status register */
 
153
#define I2C_EXTD_STAT_REG               0xc
 
154
#define I2C_EXTD_STAT_FIFO_SIZE         PPC_BITMASK(0, 7)
 
155
#define I2C_EXTD_STAT_MSM_CURSTATE      PPC_BITMASK(11, 15)
 
156
#define I2C_EXTD_STAT_SCL_IN_SYNC       PPC_BIT(16)
 
157
#define I2C_EXTD_STAT_SDA_IN_SYNC       PPC_BIT(17)
 
158
#define I2C_EXTD_STAT_S_SCL             PPC_BIT(18)
 
159
#define I2C_EXTD_STAT_S_SDA             PPC_BIT(19)
 
160
#define I2C_EXTD_STAT_M_SCL             PPC_BIT(20)
 
161
#define I2C_EXTD_STAT_M_SDA             PPC_BIT(21)
 
162
#define I2C_EXTD_STAT_HIGH_WATER        PPC_BIT(22)
 
163
#define I2C_EXTD_STAT_LOW_WATER         PPC_BIT(23)
 
164
#define I2C_EXTD_STAT_I2C_BUSY          PPC_BIT(24)
 
165
#define I2C_EXTD_STAT_SELF_BUSY         PPC_BIT(25)
 
166
#define I2C_EXTD_STAT_I2C_VERSION       PPC_BITMASK(27, 31)
 
167
 
 
168
/* I2C residual front end/back end length */
 
169
#define I2C_RESIDUAL_LEN_REG            0xd
 
170
#define I2C_RESIDUAL_FRONT_END          PPC_BITMASK(0, 15)
 
171
#define I2C_RESIDUAL_BACK_END           PPC_BITMASK(16, 31)
 
172
 
 
173
/* Port busy register */
 
174
#define I2C_PORT_BUYS_REG               0xe
 
175
 
 
176
enum p8_i2c_master_type {
 
177
        I2C_POWER8,
 
178
        I2C_CENTAUR,
 
179
        MAX_I2C_TYPE,
 
180
};
 
181
 
 
182
struct p8_i2c_master {
 
183
        struct lock             lock;           /* Lock to guard the members */
 
184
        enum p8_i2c_master_type type;           /* P8 vs. Centaur */
 
185
        uint64_t                poll_interval;  /* Polling interval  */
 
186
        uint64_t                byte_timeout;   /* Timeout per byte */
 
187
        uint64_t                xscom_base;     /* xscom base of i2cm */
 
188
        uint32_t                fifo_size;      /* Maximum size of FIFO  */
 
189
        uint32_t                chip_id;        /* Chip the i2cm sits on */
 
190
        uint32_t                engine_id;      /* Engine# on chip */
 
191
        uint8_t                 obuf[4];        /* Offset buffer */
 
192
        uint32_t                bytes_sent;
 
193
        bool                    irq_ok;         /* Interrupt working ? */
 
194
        bool                    occ_cache_dis;  /* I have disabled the cache */
 
195
        enum request_state {
 
196
                state_idle,
 
197
                state_occache_dis,
 
198
                state_offset,
 
199
                state_data,
 
200
                state_error,
 
201
                state_recovery,
 
202
        }                       state;
 
203
        struct list_head        req_list;       /* Request queue head */
 
204
        struct timer            poller;
 
205
        struct timer            timeout;
 
206
        struct timer            recovery;
 
207
        struct timer            sensor_cache;
 
208
        uint8_t                 recovery_pass;
 
209
        struct list_node        link;
 
210
};
 
211
 
 
212
struct p8_i2c_master_port {
 
213
        struct i2c_bus          bus; /* Abstract bus struct for the client */
 
214
        struct p8_i2c_master    *master;
 
215
        uint32_t                port_num;
 
216
        uint32_t                bit_rate_div;   /* Divisor to set bus speed*/
 
217
};
 
218
 
 
219
struct p8_i2c_request {
 
220
        struct i2c_request      req;
 
221
        uint32_t                port_num;
 
222
        uint64_t                timeout;
 
223
};
 
224
 
 
225
static void p8_i2c_print_debug_info(struct p8_i2c_master_port *port,
 
226
                                    struct i2c_request *req)
 
227
{
 
228
        struct p8_i2c_master *master = port->master;
 
229
        uint64_t cmd, mode, stat, estat, intr;
 
230
        int rc;
 
231
 
 
232
        /* Print master and request structure bits */
 
233
        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
 
234
                         "I2C: Chip %08x Eng. %d Port %d--\n"
 
235
                         " xscom_base=0x%016llx\tstate=%d\tbytes_sent=%d\n",
 
236
                         master->chip_id, master->engine_id, port->port_num,
 
237
                         master->xscom_base, master->state, master->bytes_sent);
 
238
 
 
239
        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Request info--\n"
 
240
                         " addr=0x%04x\toffset_bytes=%d\toffset=%d\tlen=%d\n",
 
241
                         req->dev_addr, req->offset_bytes, req->offset,
 
242
                         req->rw_len);
 
243
 
 
244
        /* Dump the current state of i2c registers */
 
245
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_CMD_REG,
 
246
                        &cmd);
 
247
        if (rc) {
 
248
                prlog(PR_DEBUG, "I2C: Failed to read CMD_REG\n");
 
249
                cmd = 0ull;
 
250
        }
 
251
 
 
252
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_MODE_REG,
 
253
                        &mode);
 
254
        if (rc) {
 
255
                prlog(PR_DEBUG, "I2C: Failed to read MODE_REG\n");
 
256
                mode = 0ull;
 
257
        }
 
258
 
 
259
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_STAT_REG,
 
260
                        &stat);
 
261
        if (rc) {
 
262
                prlog(PR_DEBUG, "I2C: Failed to read STAT_REG\n");
 
263
                stat = 0ull;
 
264
        }
 
265
 
 
266
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_EXTD_STAT_REG,
 
267
                        &estat);
 
268
        if (rc) {
 
269
                prlog(PR_DEBUG, "I2C: Failed to read EXTD_STAT_REG\n");
 
270
                estat = 0ull;
 
271
        }
 
272
 
 
273
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_INTR_MASK_REG,
 
274
                        &intr);
 
275
        if (rc) {
 
276
                prlog(PR_DEBUG, "I2C: Failed to read INTR_MASK_REG\n");
 
277
                intr = 0ull;
 
278
        }
 
279
 
 
280
        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Register dump--\n"
 
281
                         "cmd:0x%016llx\tmode:0x%016llx\tstat:0x%016llx\n"
 
282
                         "estat:0x%016llx\tintr:0x%016llx\n", cmd, mode, stat,
 
283
                         estat, intr);
 
284
}
 
285
 
 
286
static bool p8_i2c_has_irqs(struct p8_i2c_master *master)
 
287
{
 
288
        struct proc_chip *chip;
 
289
 
 
290
        /* Centaur I2C doesn't have interrupts */
 
291
        if (master->type == I2C_CENTAUR)
 
292
                return false;
 
293
 
 
294
        chip = get_chip(master->chip_id);
 
295
 
 
296
        /* The i2c interrurpt was only added to Murano DD2.1 and Venice
 
297
         * DD2.0. When operating without interrupts, we need to bump the
 
298
         * timeouts as we rely solely on the polls from Linux which can
 
299
         * be up to 2s apart !
 
300
         *
 
301
         * Also we don't have interrupts for the Centaur i2c.
 
302
         */
 
303
        switch (chip->type) {
 
304
        case PROC_CHIP_P8_MURANO:
 
305
                return chip->ec_level >= 0x21;
 
306
        case PROC_CHIP_P8_VENICE:
 
307
                return chip->ec_level >= 0x20;
 
308
        case PROC_CHIP_P8_NAPLES:
 
309
                return true;
 
310
        default:
 
311
                return false;
 
312
        }
 
313
}
 
314
 
 
315
static int p8_i2c_enable_irqs(struct p8_i2c_master *master)
 
316
{
 
317
        int rc;
 
318
 
 
319
        /* Enable the interrupts */
 
320
        rc = xscom_write(master->chip_id, master->xscom_base +
 
321
                         I2C_INTR_COND_REG, I2C_STAT_ANY_ERR >> 16 |
 
322
                         I2C_INTR_CMD_COMP | I2C_INTR_DATA_REQ);
 
323
        if (rc)
 
324
                prlog(PR_ERR, "I2C: Failed to enable the interrupts\n");
 
325
 
 
326
        return rc;
 
327
}
 
328
 
 
329
static int p8_i2c_prog_watermark(struct p8_i2c_master *master)
 
330
{
 
331
        uint64_t watermark;
 
332
        int rc;
 
333
 
 
334
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_WATERMARK_REG,
 
335
                        &watermark);
 
336
        if (rc) {
 
337
                prlog(PR_ERR, "I2C: Failed to read the WATERMARK_REG\n");
 
338
                return rc;
 
339
        }
 
340
 
 
341
        /* Set the high/low watermark */
 
342
        watermark = SETFIELD(I2C_WATERMARK_HIGH, watermark, I2C_FIFO_HI_LVL);
 
343
        watermark = SETFIELD(I2C_WATERMARK_LOW, watermark, I2C_FIFO_LO_LVL);
 
344
        rc = xscom_write(master->chip_id, master->xscom_base +
 
345
                         I2C_WATERMARK_REG, watermark);
 
346
        if (rc)
 
347
                prlog(PR_ERR, "I2C: Failed to set high/low watermark level\n");
 
348
 
 
349
        return rc;
 
350
}
 
351
 
 
352
static int p8_i2c_prog_mode(struct p8_i2c_master_port *port, bool enhanced_mode)
 
353
{
 
354
        struct p8_i2c_master *master = port->master;
 
355
        struct i2c_request *req = list_top(&master->req_list,
 
356
                                           struct i2c_request, link);
 
357
        struct p8_i2c_request *request =
 
358
                container_of(req, struct p8_i2c_request, req);
 
359
        uint64_t mode, omode;
 
360
        int rc;
 
361
 
 
362
        rc = xscom_read(master->chip_id, master->xscom_base +
 
363
                        I2C_MODE_REG, &mode);
 
364
        if (rc) {
 
365
                prlog(PR_ERR, "I2C: Failed to read the MODE_REG\n");
 
366
                return rc;
 
367
        }
 
368
        omode = mode;
 
369
        mode = SETFIELD(I2C_MODE_PORT_NUM, mode, request->port_num);
 
370
        mode = SETFIELD(I2C_MODE_BIT_RATE_DIV, mode, port->bit_rate_div);
 
371
        if (enhanced_mode)
 
372
                mode |= I2C_MODE_ENHANCED;
 
373
        else
 
374
                mode &= ~I2C_MODE_ENHANCED;
 
375
        if (mode == omode)
 
376
                return 0;
 
377
 
 
378
        rc = xscom_write(master->chip_id, master->xscom_base + I2C_MODE_REG,
 
379
                         mode);
 
380
        if (rc)
 
381
                prlog(PR_ERR, "I2C: Failed to write the MODE_REG\n");
 
382
 
 
383
        return rc;
 
384
}
 
385
 
 
386
static void p8_i2c_complete_request(struct p8_i2c_master *master,
 
387
                                    struct i2c_request *req, int ret)
 
388
{
 
389
        struct p8_i2c_request *request =
 
390
                container_of(req, struct p8_i2c_request, req);
 
391
 
 
392
        /* We only complete the current top level request */
 
393
        assert(req == list_top(&master->req_list, struct i2c_request, link));
 
394
 
 
395
        cancel_timer_async(&master->timeout);
 
396
        request->timeout = 0ul;
 
397
 
 
398
        list_del(&req->link);
 
399
        master->state = state_idle;
 
400
        req->result = ret;
 
401
 
 
402
        /* Schedule re-enabling of sensor cache */
 
403
        if (master->occ_cache_dis)
 
404
                schedule_timer(&master->sensor_cache,
 
405
                               msecs_to_tb(SENSOR_CACHE_EN_DELAY));
 
406
 
 
407
        unlock(&master->lock);
 
408
        if (req->completion)
 
409
                req->completion(ret, req);
 
410
        /* req might have been freed at this point */
 
411
        lock(&master->lock);
 
412
}
 
413
 
 
414
 
 
415
static int p8_i2c_engine_reset(struct p8_i2c_master_port *port)
 
416
{
 
417
        struct p8_i2c_master *master = port->master;
 
418
        int rc;
 
419
 
 
420
        /* Reset the i2c engine */
 
421
        rc = xscom_write(master->chip_id, master->xscom_base +
 
422
                         I2C_RESET_I2C_REG, 0);
 
423
        if (rc) {
 
424
                log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: Failed "
 
425
                                 "to reset the i2c engine\n");
 
426
                return rc;
 
427
        }
 
428
 
 
429
        /* Reprogram the watermark and mode */
 
430
        rc = p8_i2c_prog_watermark(port->master);
 
431
        if (rc) {
 
432
                log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: Failed to"
 
433
                                 "program the WATERMARK_REG\n");
 
434
                return rc;
 
435
        }
 
436
 
 
437
        rc = p8_i2c_prog_mode(port, false);
 
438
        if (rc)
 
439
                log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: Failed to"
 
440
                                 "program the MODE_REG\n");
 
441
 
 
442
        return rc;
 
443
}
 
444
 
 
445
static void p8_i2c_translate_error(struct i2c_request *req, uint64_t status)
 
446
{
 
447
        /* Assuming there are not more than one type of error simultaneously */
 
448
        if (status & I2C_STAT_NACK_RCVD_ERR)
 
449
                req->result = OPAL_I2C_NACK_RCVD;
 
450
        else if (status & I2C_STAT_INVALID_CMD)
 
451
                req->result = OPAL_I2C_INVALID_CMD;
 
452
        else if (status & I2C_STAT_LBUS_PARITY_ERR)
 
453
                req->result = OPAL_I2C_LBUS_PARITY;
 
454
        else if (status & I2C_STAT_BKEND_OVERRUN_ERR)
 
455
                req->result = OPAL_I2C_BKEND_OVERRUN;
 
456
        else if (status & I2C_STAT_BKEND_ACCESS_ERR)
 
457
                req->result = OPAL_I2C_BKEND_ACCESS;
 
458
        else if (status & I2C_STAT_ARBT_LOST_ERR)
 
459
                req->result = OPAL_I2C_ARBT_LOST;
 
460
        else if (status & I2C_STAT_STOP_ERR)
 
461
                req->result = OPAL_I2C_STOP_ERR;
 
462
        else if (status & I2C_STAT_PSEUDO_TIMEOUT)
 
463
                req->result = OPAL_I2C_TIMEOUT;
 
464
}
 
465
 
 
466
static void p8_i2c_status_error(struct p8_i2c_master_port *port,
 
467
                                struct i2c_request *req,
 
468
                                uint64_t status)
 
469
{
 
470
        struct p8_i2c_master *master = port->master;
 
471
        int rc;
 
472
 
 
473
        /* Display any error other than I2C_INTR_NACK_RCVD_ERR or
 
474
         * timeout since getting NACK's is normal if Linux is probing
 
475
         * the bus and timeouts will have already logged something.
 
476
         */
 
477
        if (!(status & (I2C_STAT_NACK_RCVD_ERR | I2C_STAT_PSEUDO_TIMEOUT))) {
 
478
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
 
479
                                 "I2C: Transfer error occurred\n");
 
480
                p8_i2c_print_debug_info(port, req);
 
481
        }
 
482
 
 
483
        p8_i2c_translate_error(req, status);
 
484
 
 
485
        rc = p8_i2c_engine_reset(port);
 
486
        if (rc)
 
487
                goto exit;
 
488
 
 
489
        if (status & (I2C_STAT_LBUS_PARITY_ERR | I2C_STAT_ARBT_LOST_ERR |
 
490
                      I2C_STAT_STOP_ERR)) {
 
491
                /*
 
492
                 * Don't bother issuing a STOP command for those errors
 
493
                 * just get rid of the current request and start off with
 
494
                 * the fresh one in the list
 
495
                 */
 
496
                p8_i2c_complete_request(master, req, req->result);
 
497
        } else {
 
498
                /*
 
499
                 * Reset the bus by issuing a STOP command to slave.
 
500
                 *
 
501
                 * Reprogram the mode register with 'enhanced bit' set
 
502
                 */
 
503
                rc = p8_i2c_prog_mode(port, true);
 
504
                if (rc) {
 
505
                        log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: "
 
506
                                         "Failed to program the MODE_REG\n");
 
507
                        goto exit;
 
508
                }
 
509
 
 
510
                /* Enable the interrupt */
 
511
                p8_i2c_enable_irqs(master);
 
512
 
 
513
                /* Send an immediate stop */
 
514
                master->state = state_error;
 
515
                rc = xscom_write(master->chip_id, master->xscom_base +
 
516
                                 I2C_CMD_REG, I2C_CMD_WITH_STOP);
 
517
                if (rc) {
 
518
                        log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: "
 
519
                                         "Failed to issue immediate STOP\n");
 
520
                        goto exit;
 
521
                }
 
522
        }
 
523
        return;
 
524
 
 
525
exit:
 
526
        p8_i2c_complete_request(master, req, req->result);
 
527
}
 
528
 
 
529
static int p8_i2c_fifo_read(struct p8_i2c_master *master,
 
530
                            uint8_t *buf, uint32_t count)
 
531
{
 
532
        uint64_t fifo;
 
533
        uint32_t i;
 
534
        int rc = 0;
 
535
 
 
536
        for (i = 0; i < count; i++, buf++) {
 
537
                rc = xscom_read(master->chip_id, master->xscom_base +
 
538
                                I2C_FIFO_REG, &fifo);
 
539
                if (rc) {
 
540
                        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
 
541
                                         "I2C: Failed to read the fifo\n");
 
542
                        break;
 
543
                }
 
544
 
 
545
                *buf = GETFIELD(I2C_FIFO, fifo);
 
546
        }
 
547
        return rc;
 
548
}
 
549
 
 
550
static int p8_i2c_fifo_write(struct p8_i2c_master *master,
 
551
                             uint8_t *buf, uint32_t count)
 
552
{
 
553
        uint64_t fifo;
 
554
        uint32_t i;
 
555
        int rc = 0;
 
556
 
 
557
        for (i = 0; i < count; i++, buf++) {
 
558
                fifo = SETFIELD(I2C_FIFO, 0ull, *buf);
 
559
                rc = xscom_write(master->chip_id, master->xscom_base +
 
560
                                 I2C_FIFO_REG, fifo);
 
561
                if (rc) {
 
562
                        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
 
563
                                         "I2C: Failed to write the fifo\n");
 
564
                        break;
 
565
                }
 
566
        }
 
567
        return rc;
 
568
}
 
569
 
 
570
static void p8_i2c_status_data_request(struct p8_i2c_master *master,
 
571
                                       struct i2c_request *req,
 
572
                                       uint64_t status)
 
573
{
 
574
        uint32_t fifo_count, fifo_free, count;
 
575
        uint8_t *buf;
 
576
        int rc = 0;
 
577
 
 
578
        fifo_count = GETFIELD(I2C_STAT_FIFO_ENTRY_COUNT, status);
 
579
        fifo_free = master->fifo_size - fifo_count;
 
580
 
 
581
        DBG("Data request, state=%d fifo_count=%d/%d bytes_sent=%d\n",
 
582
            master->state, fifo_count, master->fifo_size, master->bytes_sent);
 
583
 
 
584
        switch(master->state) {
 
585
        case state_offset:
 
586
                /* We assume the offset can always be written in one go */
 
587
                if (fifo_free < req->offset_bytes) {
 
588
                        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
 
589
                                         "I2C: Fifo too small for offset !\n");
 
590
                        rc = OPAL_HARDWARE;
 
591
                } else {
 
592
                        rc = p8_i2c_fifo_write(master, master->obuf,
 
593
                                               req->offset_bytes);
 
594
                }
 
595
 
 
596
                /* For read, wait address phase to complete */
 
597
                if (rc || req->op != SMBUS_WRITE)
 
598
                        break;
 
599
 
 
600
                /* For writes, transition to data phase now */
 
601
                master->state = state_data;
 
602
                fifo_free -= req->offset_bytes;
 
603
                /* Fall through */
 
604
        case state_data:
 
605
                /* Sanity check */
 
606
                if (master->bytes_sent >= req->rw_len) {
 
607
                        log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: "
 
608
                                         "Data req with no data to send sent=%d "
 
609
                                         "req=%d\n", master->bytes_sent,
 
610
                                         req->rw_len);
 
611
                        rc = OPAL_HARDWARE;
 
612
                        break;
 
613
                }
 
614
 
 
615
                /* Get next chunk */
 
616
                buf = req->rw_buf + master->bytes_sent;
 
617
                count = req->rw_len - master->bytes_sent;
 
618
 
 
619
                /* Check direction */
 
620
                if (req->op == I2C_READ || req->op == SMBUS_READ) {
 
621
                        if (count > fifo_count)
 
622
                                count = fifo_count;
 
623
                        rc = p8_i2c_fifo_read(master, buf, count);
 
624
                } else {
 
625
                        if (count > fifo_free)
 
626
                                count = fifo_free;
 
627
                        rc = p8_i2c_fifo_write(master, buf, count);
 
628
                }
 
629
                if (rc == 0)
 
630
                        master->bytes_sent += count;
 
631
                break;
 
632
        default:
 
633
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Invalid "
 
634
                                 "state %d in data req !\n", master->state);
 
635
                rc = OPAL_WRONG_STATE;
 
636
        }
 
637
        if (rc)
 
638
                p8_i2c_complete_request(master, req, rc);
 
639
        else
 
640
                p8_i2c_enable_irqs(master);
 
641
}
 
642
 
 
643
static void p8_i2c_complete_offset(struct p8_i2c_master *master,
 
644
                                   struct i2c_request *req)
 
645
{
 
646
        uint64_t cmd;
 
647
        int rc = 0;
 
648
 
 
649
        DBG("Completing offset phase\n");
 
650
 
 
651
        /* If it's a write, we should only get here for empty
 
652
         * write commands
 
653
         */
 
654
        if (req->op == SMBUS_WRITE && req->rw_len != 0) {
 
655
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Write "
 
656
                                 "completion in offset state !\n");
 
657
                rc = OPAL_HARDWARE;
 
658
                goto complete;
 
659
        }
 
660
 
 
661
        /* Switch to data phase */
 
662
        master->state = state_data;
 
663
 
 
664
        /* If it's not a read command, or there are no data to read,
 
665
         * then we complete the command
 
666
         */
 
667
        if (req->op != SMBUS_READ || req->rw_len == 0)
 
668
                goto complete;
 
669
 
 
670
        /* Otherwise, let's start the data phase */
 
671
        cmd = I2C_CMD_WITH_START | I2C_CMD_WITH_ADDR |
 
672
                I2C_CMD_WITH_STOP | I2C_CMD_READ_NOT_WRITE;
 
673
        cmd = SETFIELD(I2C_CMD_DEV_ADDR, cmd, req->dev_addr);
 
674
        cmd = SETFIELD(I2C_CMD_LEN_BYTES, cmd, req->rw_len);
 
675
 
 
676
        DBG("Command: %016llx, state: %d\n", cmd, master->state);
 
677
 
 
678
        /* Send command */
 
679
        rc = xscom_write(master->chip_id, master->xscom_base + I2C_CMD_REG,
 
680
                         cmd);
 
681
        if (rc) {
 
682
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Failed "
 
683
                                 "to write the CMD_REG\n");
 
684
                goto complete;
 
685
        }
 
686
 
 
687
        /* Enable the interrupts */
 
688
        p8_i2c_enable_irqs(master);
 
689
        return;
 
690
 
 
691
 complete:
 
692
        p8_i2c_complete_request(master, req, rc);
 
693
}
 
694
 
 
695
static void p8_i2c_status_cmd_completion(struct p8_i2c_master *master,
 
696
                                         struct i2c_request *req)
 
697
{
 
698
        int rc;
 
699
 
 
700
        DBG("Command completion, state=%d bytes_sent=%d\n",
 
701
            master->state, master->bytes_sent);
 
702
 
 
703
        /* If we complete an offset, we probably need to transition
 
704
         * do a data read, check if that all makes sense
 
705
         */
 
706
        if (master->state == state_offset) {
 
707
                p8_i2c_complete_offset(master, req);
 
708
                return;
 
709
        }
 
710
 
 
711
        /* If we are not already in error state, check if we have
 
712
         * completed our data transfer properly
 
713
         */
 
714
        if (master->state != state_error && master->bytes_sent != req->rw_len) {
 
715
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Request "
 
716
                                 "complete with residual data req=%d done=%d\n",
 
717
                                 req->rw_len, master->bytes_sent);
 
718
                /* Should we error out here ? */
 
719
        }
 
720
        rc = master->state == state_error ? req->result : OPAL_SUCCESS;
 
721
        p8_i2c_complete_request(master, req, rc);
 
722
}
 
723
 
 
724
static void  p8_i2c_check_status(struct p8_i2c_master *master)
 
725
{
 
726
        struct p8_i2c_master_port *port;
 
727
        struct i2c_request *req;
 
728
        uint64_t status;
 
729
        int rc;
 
730
 
 
731
        /* If we are idle, just return, we'll catch error conditions
 
732
         * when we next try to enqueue a request
 
733
         */
 
734
        if (master->state == state_idle)
 
735
                return;
 
736
 
 
737
        /* Read status register */
 
738
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_STAT_REG,
 
739
                        &status);
 
740
        if (rc) {
 
741
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Failed "
 
742
                                 "to read the STAT_REG\n");
 
743
                return;
 
744
        }
 
745
 
 
746
        /* Nothing happened ? Go back */
 
747
        if (!(status & (I2C_STAT_ANY_ERR | I2C_STAT_DATA_REQ |
 
748
                        I2C_STAT_CMD_COMP)))
 
749
                return;
 
750
 
 
751
        DBG("Non-0 status: %016llx\n", status);
 
752
 
 
753
        /* Mask the interrupts for this engine */
 
754
        rc = xscom_write(master->chip_id, master->xscom_base + I2C_INTR_REG,
 
755
                         ~I2C_INTR_ALL);
 
756
        if (rc) {
 
757
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Failed "
 
758
                                 "to disable the interrupts\n");
 
759
                return;
 
760
        }
 
761
 
 
762
        /* No request ? That's not normal ! Bail out without re-enabling
 
763
         * the interrupt
 
764
         */
 
765
        req = list_top(&master->req_list, struct i2c_request, link);
 
766
        if (req == NULL) {
 
767
                log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
 
768
                                 "I2C: Interrupt with no request"
 
769
                                 ", status=0x%016llx\n", status);
 
770
                return;
 
771
        }
 
772
 
 
773
        /* Get port for current request */
 
774
        port = container_of(req->bus, struct p8_i2c_master_port, bus);
 
775
 
 
776
        /* Handle the status in that order: errors, data requests and
 
777
         * command completion.
 
778
         */
 
779
        if (status & I2C_STAT_ANY_ERR) {
 
780
                /* Mask status to avoid some unrelated bit overwriting
 
781
                 * our pseudo-status "timeout" bit 63
 
782
                 */
 
783
                p8_i2c_status_error(port, req, status & I2C_STAT_ANY_ERR);
 
784
        } else if (status & I2C_STAT_DATA_REQ)
 
785
                p8_i2c_status_data_request(master, req, status);
 
786
        else if (status & I2C_STAT_CMD_COMP)
 
787
                p8_i2c_status_cmd_completion(master, req);
 
788
}
 
789
 
 
790
static int p8_i2c_check_initial_status(struct p8_i2c_master_port *port)
 
791
{
 
792
        struct p8_i2c_master *master = port->master;
 
793
        uint64_t status, estat;
 
794
        int rc;
 
795
 
 
796
        master->recovery_pass++;
 
797
 
 
798
        /* Read status register */
 
799
        rc = xscom_read(master->chip_id, master->xscom_base + I2C_STAT_REG,
 
800
                        &status);
 
801
        if (rc) {
 
802
                log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
 
803
                                 "to read the STAT_REG\n");
 
804
                return rc;
 
805
        }
 
806
 
 
807
        rc = xscom_read(master->chip_id,
 
808
                        master->xscom_base + I2C_EXTD_STAT_REG,
 
809
                        &estat);
 
810
        if (rc) {
 
811
                log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
 
812
                                 "to read the EXTD_STAT_REG\n");
 
813
                return rc;
 
814
        }
 
815
        if (estat & (I2C_EXTD_STAT_I2C_BUSY | I2C_EXTD_STAT_SELF_BUSY)) {
 
816
                DBG("Initial estat busy ! %016llx\n", estat);
 
817
                /* Just a warning for now */
 
818
        }
 
819
 
 
820
        /* Nothing happened ? Go back */
 
821
        if (status & I2C_STAT_ANY_ERR) {
 
822
                log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: "
 
823
                                 "Initial error status 0x%016llx\n", status);
 
824
 
 
825
                if (master->recovery_pass > 1) {
 
826
                        log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: "
 
827
                                         "Error stuck, aborting !!\n");
 
828
                        return OPAL_HARDWARE;
 
829
                }
 
830
 
 
831
                /* Mark state as "recovery" to block any other activity */
 
832
                master->state = state_recovery;
 
833
 
 
834
                /* Reset the engine */
 
835
                p8_i2c_engine_reset(port);
 
836
 
 
837
                /* Delay 5ms for bus to settle */
 
838
                schedule_timer(&master->recovery, msecs_to_tb(5));
 
839
                unlock(&master->lock);
 
840
                return OPAL_BUSY;
 
841
        }
 
842
 
 
843
        /* Still busy ? */
 
844
        if (!(status & I2C_STAT_CMD_COMP)) {
 
845
                log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Initial "
 
846
                                 "command complete not set\n");
 
847
 
 
848
                if (master->recovery_pass > 5) {
 
849
                        log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: "
 
850
                                         "Command stuck, aborting !!\n");
 
851
                        return OPAL_HARDWARE;
 
852
                }
 
853
 
 
854
 
 
855
                master->state = state_recovery;
 
856
 
 
857
                /* Delay 5ms for bus to settle */
 
858
                schedule_timer(&master->recovery, msecs_to_tb(5));
 
859
                unlock(&master->lock);
 
860
                return OPAL_BUSY;
 
861
        }
 
862
 
 
863
        master->recovery_pass = 0;
 
864
        return 0;
 
865
}
 
866
 
 
867
static int p8_i2c_start_request(struct p8_i2c_master *master,
 
868
                                struct i2c_request *req)
 
869
{
 
870
        struct p8_i2c_master_port *port;
 
871
        struct p8_i2c_request *request =
 
872
                container_of(req, struct p8_i2c_request, req);
 
873
        uint64_t cmd, now;
 
874
        int64_t rc, tbytes;
 
875
 
 
876
        DBG("Starting req %d len=%d addr=%02x (offset=%x)\n",
 
877
            req->op, req->rw_len, req->dev_addr, req->offset);
 
878
 
 
879
        /* Get port */
 
880
        port = container_of(req->bus, struct p8_i2c_master_port, bus);
 
881
 
 
882
        /* Check if we need to disable the OCC cache first */
 
883
        if (master->type == I2C_CENTAUR && !master->occ_cache_dis) {
 
884
                DBG("Disabling OCC cache...\n");
 
885
                rc = centaur_disable_sensor_cache(master->chip_id);
 
886
                if (rc < 0) {
 
887
                        log_simple_error(&e_info(OPAL_RC_I2C_START_REQ),
 
888
                                         "I2C: Failed "
 
889
                                         "to disable the sensor cache\n");
 
890
                        return rc;
 
891
                }
 
892
                master->occ_cache_dis = true;
 
893
 
 
894
                /* Do we need to wait ? */
 
895
                if (rc > 0) {
 
896
                        DBG("Waiting %lld\n", rc);
 
897
                        master->state = state_occache_dis;
 
898
                        schedule_timer(&master->recovery, rc);
 
899
                        return 0;
 
900
                }
 
901
        }
 
902
 
 
903
        /* Convert the offset if needed */
 
904
        if (req->offset_bytes) {
 
905
                int i;
 
906
 
 
907
                for (i = 0; i < req->offset_bytes; i++) {
 
908
                        uint8_t b;
 
909
 
 
910
                        b = req->offset >> (8 * (req->offset_bytes - i - 1));
 
911
                        master->obuf[i] = b;
 
912
                }
 
913
                DBG("Offset %d bytes: %02x %02x %02x %02x\n",
 
914
                    req->offset_bytes, master->obuf[0], master->obuf[1],
 
915
                    master->obuf[2], master->obuf[3]);
 
916
        }
 
917
 
 
918
        /* Program mode register */
 
919
        rc = p8_i2c_prog_mode(port, false);
 
920
        if (rc) {
 
921
                log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
 
922
                                 "to program the MODE_REG\n");
 
923
                return rc;
 
924
        }
 
925
 
 
926
        /* Check status */
 
927
        rc = p8_i2c_check_initial_status(port);
 
928
        if (rc != OPAL_BUSY)
 
929
                master->recovery_pass = 0;
 
930
        if (rc)
 
931
                return rc;
 
932
 
 
933
        /* Initialize bytes_sent */
 
934
        master->bytes_sent = 0;
 
935
 
 
936
        /* Set up the command register */
 
937
        cmd = I2C_CMD_WITH_START | I2C_CMD_WITH_ADDR;
 
938
        cmd = SETFIELD(I2C_CMD_DEV_ADDR, cmd, req->dev_addr);
 
939
        switch (req->op) {
 
940
        case I2C_READ:
 
941
                cmd |= I2C_CMD_READ_NOT_WRITE;
 
942
                /* Fall through */
 
943
        case I2C_WRITE:
 
944
                cmd |= I2C_CMD_WITH_STOP;
 
945
                cmd = SETFIELD(I2C_CMD_LEN_BYTES, cmd, req->rw_len);
 
946
                master->state = state_data;
 
947
                break;
 
948
        case SMBUS_READ:
 
949
                cmd = SETFIELD(I2C_CMD_LEN_BYTES, cmd, req->offset_bytes);
 
950
                master->state = state_offset;
 
951
                break;
 
952
        case SMBUS_WRITE:
 
953
                cmd |= I2C_CMD_WITH_STOP;
 
954
                cmd = SETFIELD(I2C_CMD_LEN_BYTES, cmd,
 
955
                                req->rw_len + req->offset_bytes);
 
956
                master->state = state_offset;
 
957
                break;
 
958
        default:
 
959
                return OPAL_PARAMETER;
 
960
        }
 
961
        DBG("Command: %016llx, state: %d\n", cmd, master->state);
 
962
 
 
963
        /* Send command */
 
964
        rc = xscom_write(master->chip_id, master->xscom_base + I2C_CMD_REG,
 
965
                         cmd);
 
966
        if (rc) {
 
967
                log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
 
968
                                 "to write the CMD_REG\n");
 
969
                return rc;
 
970
        }
 
971
 
 
972
        /* Enable the interrupts */
 
973
        p8_i2c_enable_irqs(master);
 
974
 
 
975
        /* Run a poll timer for boot cases or non-working interrupts
 
976
         * cases
 
977
         */
 
978
        now = schedule_timer(&master->poller, master->poll_interval);
 
979
 
 
980
        /* Calculate and start timeout */
 
981
        if (request->timeout) {
 
982
                request->timeout += now;
 
983
        } else {
 
984
                tbytes = req->rw_len + req->offset_bytes + 2;
 
985
                request->timeout = now + tbytes * master->byte_timeout;
 
986
        }
 
987
 
 
988
        /* Start the timeout */
 
989
        schedule_timer_at(&master->timeout, request->timeout);
 
990
 
 
991
        return OPAL_SUCCESS;
 
992
}
 
993
 
 
994
static void p8_i2c_check_work(struct p8_i2c_master *master)
 
995
{
 
996
        struct i2c_request *req;
 
997
        int rc;
 
998
 
 
999
        while (master->state == state_idle && !list_empty(&master->req_list)) {
 
1000
                req = list_top(&master->req_list, struct i2c_request, link);
 
1001
                rc = p8_i2c_start_request(master, req);
 
1002
                if (rc && rc != OPAL_BUSY)
 
1003
                        p8_i2c_complete_request(master, req, rc);
 
1004
        }
 
1005
}
 
1006
 
 
1007
static int p8_i2c_queue_request(struct i2c_request *req)
 
1008
{
 
1009
        struct i2c_bus *bus = req->bus;
 
1010
        struct p8_i2c_master_port *port =
 
1011
                container_of(bus, struct p8_i2c_master_port, bus);
 
1012
        struct p8_i2c_master *master = port->master;
 
1013
        int rc = 0;
 
1014
 
 
1015
        /* Parameter check */
 
1016
        if (req->rw_len > I2C_MAX_TFR_LEN) {
 
1017
                prlog(PR_ERR, "I2C: Too large transfer %d bytes\n", req->rw_len);
 
1018
                return OPAL_PARAMETER;
 
1019
        }
 
1020
 
 
1021
        if (req->offset_bytes > 4) {
 
1022
                prlog(PR_ERR, "I2C: Invalid offset size %d\n", req->offset_bytes);
 
1023
                return OPAL_PARAMETER;
 
1024
        }
 
1025
        lock(&master->lock);
 
1026
        list_add_tail(&master->req_list, &req->link);
 
1027
        p8_i2c_check_work(master);
 
1028
        unlock(&master->lock);
 
1029
 
 
1030
        return rc;
 
1031
}
 
1032
 
 
1033
static struct i2c_request *p8_i2c_alloc_request(struct i2c_bus *bus)
 
1034
{
 
1035
        struct p8_i2c_master_port *port =
 
1036
                container_of(bus, struct p8_i2c_master_port, bus);
 
1037
        struct p8_i2c_request *request;
 
1038
 
 
1039
        request = zalloc(sizeof(*request));
 
1040
        if (!request) {
 
1041
                prlog(PR_ERR, "I2C: Failed to allocate i2c request\n");
 
1042
                return NULL;
 
1043
        }
 
1044
 
 
1045
        request->port_num = port->port_num;
 
1046
        request->req.bus = bus;
 
1047
 
 
1048
        return &request->req;
 
1049
}
 
1050
 
 
1051
static void p8_i2c_free_request(struct i2c_request *req)
 
1052
{
 
1053
        struct p8_i2c_request *request =
 
1054
                container_of(req, struct p8_i2c_request, req);
 
1055
        free(request);
 
1056
}
 
1057
 
 
1058
static void p8_i2c_set_request_timeout(struct i2c_request *req,
 
1059
                                       uint64_t duration)
 
1060
{
 
1061
        struct p8_i2c_request *request =
 
1062
                container_of(req, struct p8_i2c_request, req);
 
1063
 
 
1064
        request->timeout = msecs_to_tb(duration);
 
1065
}
 
1066
 
 
1067
static inline uint32_t p8_i2c_get_bit_rate_divisor(uint32_t lb_freq,
 
1068
                                                   uint32_t bus_speed)
 
1069
{
 
1070
        assert(bus_speed > 0);
 
1071
        return (((lb_freq / bus_speed) - 1) / 4);
 
1072
}
 
1073
 
 
1074
static inline uint64_t p8_i2c_get_poll_interval(uint32_t bus_speed)
 
1075
{
 
1076
        uint64_t usec;
 
1077
 
 
1078
        assert(bus_speed > 0);
 
1079
 
 
1080
        /* Polling Interval = 8 * (1/bus_speed) * (1/10) -> convert to uSec */
 
1081
        usec = ((8 * USEC_PER_SEC) / (10 * bus_speed));
 
1082
        return usecs_to_tb(usec);
 
1083
}
 
1084
 
 
1085
static void p8_i2c_timeout(struct timer *t __unused, void *data, uint64_t now)
 
1086
{
 
1087
        struct p8_i2c_master_port *port;
 
1088
        struct p8_i2c_master *master = data;
 
1089
        struct p8_i2c_request *request;
 
1090
        struct i2c_request *req;
 
1091
 
 
1092
        lock(&master->lock);
 
1093
 
 
1094
        /* This could be spurrious ... */
 
1095
        if (master->state == state_idle) {
 
1096
                DBG("I2C: Timeout in idle state\n");
 
1097
                goto exit;
 
1098
        }
 
1099
 
 
1100
        /* We might still be spurrious timer, we need to ensure that the
 
1101
         * head request is indeed old enough to be the one timing out
 
1102
         */
 
1103
        req = list_top(&master->req_list, struct i2c_request, link);
 
1104
        if (req == NULL) {
 
1105
                DBG("I2C: Timeout with no"
 
1106
                    " pending request state=%d\n", master->state);
 
1107
                goto exit;
 
1108
        }
 
1109
        request = container_of(req, struct p8_i2c_request, req);
 
1110
        if (tb_compare(now, request->timeout) == TB_ABEFOREB) {
 
1111
                DBG("I2C: Timeout with request not expired\n");
 
1112
                goto exit;
 
1113
        }
 
1114
 
 
1115
        request->timeout = 0ul;
 
1116
        port = container_of(req->bus, struct p8_i2c_master_port, bus);
 
1117
 
 
1118
        /* Allright, we have a request and it has timed out ... */
 
1119
        log_simple_error(&e_info(OPAL_RC_I2C_TIMEOUT),
 
1120
                         "I2C: Request timeout !\n");
 
1121
        p8_i2c_print_debug_info(port, req);
 
1122
 
 
1123
        /* Use the standard error path */
 
1124
        p8_i2c_status_error(port, req, I2C_STAT_PSEUDO_TIMEOUT);
 
1125
 exit:
 
1126
        unlock(&master->lock);
 
1127
}
 
1128
 
 
1129
static void p8_i2c_recover(struct timer *t __unused, void *data,
 
1130
                           uint64_t now __unused)
 
1131
{
 
1132
        struct p8_i2c_master *master = data;
 
1133
 
 
1134
        lock(&master->lock);
 
1135
        assert(master->state == state_recovery ||
 
1136
               master->state == state_occache_dis);
 
1137
        master->state = state_idle;
 
1138
 
 
1139
        /* We may or may not still have work pending, re-enable the sensor cache
 
1140
         * immediately if we don't (we just waited the recovery time so there is
 
1141
         * little point waiting longer).
 
1142
         */
 
1143
        if (master->occ_cache_dis && list_empty(&master->req_list)) {
 
1144
                DBG("Re-enabling OCC cache after recovery\n");
 
1145
                centaur_enable_sensor_cache(master->chip_id);
 
1146
                master->occ_cache_dis = false;
 
1147
        }
 
1148
 
 
1149
        /* Re-check for new work */
 
1150
        p8_i2c_check_work(master);
 
1151
        unlock(&master->lock);
 
1152
}
 
1153
 
 
1154
static void p8_i2c_enable_scache(struct timer *t __unused, void *data,
 
1155
                                 uint64_t now __unused)
 
1156
{
 
1157
        struct p8_i2c_master *master = data;
 
1158
 
 
1159
        lock(&master->lock);
 
1160
 
 
1161
        /* Check if we are still idle */
 
1162
        if (master->state == state_idle && master->occ_cache_dis) {
 
1163
                DBG("Re-enabling OCC cache\n");
 
1164
                centaur_enable_sensor_cache(master->chip_id);
 
1165
                master->occ_cache_dis = false;
 
1166
        }
 
1167
        unlock(&master->lock);
 
1168
}
 
1169
 
 
1170
static void p8_i2c_poll(struct timer *t __unused, void *data, uint64_t now)
 
1171
{
 
1172
        struct p8_i2c_master *master = data;
 
1173
 
 
1174
        /*
 
1175
         * This is called when the interrupt isn't functional or
 
1176
         * generally from the opal pollers, so fast while booting
 
1177
         * and slowly when Linux is up.
 
1178
         */
 
1179
 
 
1180
        /* Lockless fast bailout */
 
1181
        if (master->state == state_idle)
 
1182
                return;
 
1183
 
 
1184
        lock(&master->lock);
 
1185
        p8_i2c_check_status(master);
 
1186
        if (master->state != state_idle)
 
1187
                schedule_timer_at(&master->poller, now + master->poll_interval);
 
1188
        p8_i2c_check_work(master);
 
1189
        unlock(&master->lock);
 
1190
}
 
1191
 
 
1192
void p8_i2c_interrupt(uint32_t chip_id)
 
1193
{
 
1194
        struct proc_chip *chip = get_chip(chip_id);
 
1195
        struct p8_i2c_master *master = NULL;
 
1196
 
 
1197
        assert(chip);
 
1198
        list_for_each(&chip->i2cms, master, link) {
 
1199
 
 
1200
                /* Lockless fast bailout (shared interrupt) */
 
1201
                if (master->state == state_idle)
 
1202
                        continue;
 
1203
 
 
1204
                lock(&master->lock);
 
1205
 
 
1206
                /* Run the state machine */
 
1207
                p8_i2c_check_status(master);
 
1208
 
 
1209
                /* Check for new work */
 
1210
                p8_i2c_check_work(master);
 
1211
 
 
1212
                unlock(&master->lock);
 
1213
        }
 
1214
}
 
1215
 
 
1216
static const char *compat[] = {
 
1217
        "ibm,power8-i2cm",
 
1218
        "ibm,centaur-i2cm"
 
1219
};
 
1220
 
 
1221
static void p8_i2c_add_bus_prop(struct p8_i2c_master_port *port)
 
1222
{
 
1223
        const struct dt_property *c, *p;
 
1224
        struct dt_node *np = port->bus.dt_node;
 
1225
        char name[32];
 
1226
 
 
1227
        c = dt_find_property(np, "compatible");
 
1228
        p = dt_find_property(np, "ibm,port-name");
 
1229
 
 
1230
        if (!c) {
 
1231
                if (port->master->type == I2C_POWER8)
 
1232
                        dt_add_property_strings(np, "compatible",
 
1233
                                                "ibm,power8-i2c-port",
 
1234
                                                "ibm,opal-i2c");
 
1235
                else if (port->master->type == I2C_CENTAUR)
 
1236
                        dt_add_property_strings(np, "compatible",
 
1237
                                                "ibm,centaur-i2c-port",
 
1238
                                                "ibm,opal-i2c");
 
1239
        }
 
1240
 
 
1241
        if (!p) {
 
1242
                if (port->master->type == I2C_POWER8)
 
1243
                        snprintf(name, sizeof(name), "p8_%08x_e%dp%d",
 
1244
                                 port->master->chip_id, port->master->engine_id,
 
1245
                                 port->port_num);
 
1246
                else if (port->master->type == I2C_CENTAUR)
 
1247
                        snprintf(name, sizeof(name), "cen_%08x_e%dp%d",
 
1248
                                 port->master->chip_id, port->master->engine_id,
 
1249
                                 port->port_num);
 
1250
 
 
1251
                dt_add_property_string(np, "ibm,port-name", name);
 
1252
        }
 
1253
}
 
1254
 
 
1255
static void p8_i2c_init_one(struct dt_node *i2cm, enum p8_i2c_master_type type)
 
1256
{
 
1257
        struct p8_i2c_master_port *port;
 
1258
        uint32_t lb_freq, count, max_bus_speed;
 
1259
        struct dt_node *i2cm_port;
 
1260
        struct p8_i2c_master *master;
 
1261
        struct list_head *chip_list;
 
1262
        uint64_t ex_stat;
 
1263
        static bool irq_printed;
 
1264
        int64_t rc;
 
1265
 
 
1266
        master = zalloc(sizeof(*master));
 
1267
        if (!master) {
 
1268
                log_simple_error(&e_info(OPAL_RC_I2C_INIT),
 
1269
                                 "I2C: Failed to allocate master "
 
1270
                                 "structure\n");
 
1271
                return;
 
1272
        }
 
1273
        master->type = type;
 
1274
 
 
1275
        /* Local bus speed in Hz */
 
1276
        lb_freq = dt_prop_get_u32(i2cm, "clock-frequency");
 
1277
 
 
1278
        /* XXX HACK for bad HB value */
 
1279
        if (lb_freq == 600000000) {
 
1280
                prlog(PR_ERR, "I2C: Fixing up HB bad clock freq !\n");
 
1281
                lb_freq = 50000000;
 
1282
        }
 
1283
 
 
1284
        /* Initialise the i2c master structure */
 
1285
        master->state = state_idle;
 
1286
        master->chip_id = dt_get_chip_id(i2cm);
 
1287
        master->engine_id = dt_prop_get_u32(i2cm, "chip-engine#");
 
1288
        master->xscom_base = dt_get_address(i2cm, 0, NULL);
 
1289
        if (master->type == I2C_CENTAUR) {
 
1290
                struct centaur_chip *centaur = get_centaur(master->chip_id);
 
1291
                assert(centaur);
 
1292
                chip_list = &centaur->i2cms;
 
1293
 
 
1294
                /* Detect bad device-tree from HostBoot giving us bogus
 
1295
                 * i2c masters
 
1296
                 */
 
1297
                if (master->engine_id > 0) {
 
1298
                        prlog(PR_ERR, "I2C: Skipping Centaur Master #1\n");
 
1299
                        free(master);
 
1300
                        return;
 
1301
                }
 
1302
        } else {
 
1303
                struct proc_chip *chip = get_chip(master->chip_id);
 
1304
                assert(chip);
 
1305
                chip_list = &chip->i2cms;
 
1306
        }
 
1307
        init_timer(&master->timeout, p8_i2c_timeout, master);
 
1308
        init_timer(&master->poller, p8_i2c_poll, master);
 
1309
        init_timer(&master->recovery, p8_i2c_recover, master);
 
1310
        init_timer(&master->sensor_cache, p8_i2c_enable_scache, master);
 
1311
 
 
1312
        prlog(PR_INFO, "I2C: Chip %08x Eng. %d\n",
 
1313
              master->chip_id, master->engine_id);
 
1314
 
 
1315
        /* Disable OCC cache during inits */
 
1316
        if (master->type == I2C_CENTAUR) {
 
1317
                rc = centaur_disable_sensor_cache(master->chip_id);
 
1318
                if (rc < 0) {
 
1319
                        log_simple_error(&e_info(OPAL_RC_I2C_INIT), "I2C: "
 
1320
                                         "Error %lld disabling sensor cache\n",
 
1321
                                         rc);
 
1322
                        /* Ignore error and move on ... */
 
1323
                } else
 
1324
                        time_wait(rc);
 
1325
        }
 
1326
        rc = xscom_read(master->chip_id, master->xscom_base +
 
1327
                        I2C_EXTD_STAT_REG, &ex_stat);
 
1328
        if (rc) {
 
1329
                log_simple_error(&e_info(OPAL_RC_I2C_INIT), "I2C: "
 
1330
                                 "Failed to read EXTD_STAT_REG\n");
 
1331
                if (master->type == I2C_CENTAUR)
 
1332
                        centaur_enable_sensor_cache(master->chip_id);
 
1333
                free(master);
 
1334
                return;
 
1335
        }
 
1336
 
 
1337
        master->fifo_size = GETFIELD(I2C_EXTD_STAT_FIFO_SIZE, ex_stat);
 
1338
        list_head_init(&master->req_list);
 
1339
 
 
1340
        /* Check if interrupt is usable */
 
1341
        master->irq_ok = p8_i2c_has_irqs(master);
 
1342
        if (!irq_printed) {
 
1343
                irq_printed = true;
 
1344
                prlog(PR_INFO, "I2C: Interrupts %sfunctional\n",
 
1345
                      master->irq_ok ? "" : "non-");
 
1346
        }
 
1347
 
 
1348
        /* Program the watermark register */
 
1349
        rc = p8_i2c_prog_watermark(master);
 
1350
 
 
1351
        /* Re-enable the sensor cache, we aren't touching HW anymore */
 
1352
        if (master->type == I2C_CENTAUR)
 
1353
                centaur_enable_sensor_cache(master->chip_id);
 
1354
 
 
1355
        /* Handle errors from p8_i2c_prog_watermark */
 
1356
        if (rc) {
 
1357
                log_simple_error(&e_info(OPAL_RC_I2C_INIT),
 
1358
                                 "I2C: Failed to program the "
 
1359
                                 "WATERMARK_REG\n");
 
1360
                free(master);
 
1361
                return;
 
1362
        }
 
1363
 
 
1364
        /* Allocate ports driven by this master */
 
1365
        count = 0;
 
1366
        dt_for_each_child(i2cm, i2cm_port)
 
1367
                count++;
 
1368
 
 
1369
        port = zalloc(sizeof(*port) * count);
 
1370
        if (!port) {
 
1371
                log_simple_error(&e_info(OPAL_RC_I2C_INIT),
 
1372
                                 "I2C: Insufficient memory\n");
 
1373
                free(master);
 
1374
                return;
 
1375
        }
 
1376
 
 
1377
        /* Add master to chip's list */
 
1378
        list_add_tail(chip_list, &master->link);
 
1379
        max_bus_speed = 0;
 
1380
 
 
1381
        dt_for_each_child(i2cm, i2cm_port) {
 
1382
                uint32_t speed;
 
1383
 
 
1384
                port->port_num = dt_prop_get_u32(i2cm_port, "reg");
 
1385
                port->master = master;
 
1386
                speed = dt_prop_get_u32(i2cm_port, "bus-frequency");
 
1387
                if (speed > max_bus_speed)
 
1388
                        max_bus_speed = speed;
 
1389
                port->bit_rate_div =
 
1390
                        p8_i2c_get_bit_rate_divisor(lb_freq, speed);
 
1391
                port->bus.dt_node = i2cm_port;
 
1392
                port->bus.queue_req = p8_i2c_queue_request;
 
1393
                port->bus.alloc_req = p8_i2c_alloc_request;
 
1394
                port->bus.free_req = p8_i2c_free_request;
 
1395
                port->bus.set_req_timeout = p8_i2c_set_request_timeout;
 
1396
                i2c_add_bus(&port->bus);
 
1397
 
 
1398
                /* Add OPAL properties to the bus node */
 
1399
                p8_i2c_add_bus_prop(port);
 
1400
                prlog(PR_INFO, " P%d: <%s> %d kHz\n",
 
1401
                      port->port_num,
 
1402
                      (char *)dt_prop_get(i2cm_port,
 
1403
                                          "ibm,port-name"), speed/1000);
 
1404
                port++;
 
1405
        }
 
1406
 
 
1407
        /* If we have no interrupt, calculate a poll interval,
 
1408
         * otherwise just use a TIMER_POLL timer which will tick
 
1409
         * on OPAL pollers only (which allows us to operate
 
1410
         * during boot before interrupts are functional etc...
 
1411
         */
 
1412
        if (master->irq_ok)
 
1413
                master->poll_interval = TIMER_POLL;
 
1414
        else
 
1415
                master->poll_interval = p8_i2c_get_poll_interval(max_bus_speed);
 
1416
        master->byte_timeout = master->irq_ok ?
 
1417
                msecs_to_tb(I2C_TIMEOUT_IRQ_MS) :
 
1418
                msecs_to_tb(I2C_TIMEOUT_POLL_MS);
 
1419
}
 
1420
 
 
1421
void p8_i2c_init(void)
 
1422
{
 
1423
        struct dt_node *i2cm;
 
1424
        int i;
 
1425
 
 
1426
        for (i = 0; i < MAX_I2C_TYPE; i++) {
 
1427
                dt_for_each_compatible(dt_root, i2cm, compat[i])
 
1428
                        p8_i2c_init_one(i2cm, i);
 
1429
        }
 
1430
}