1
/* Copyright 2013-2014 IBM Corp.
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
7
* http://www.apache.org/licenses/LICENSE-2.0
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
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
21
#include <mem_region-malloc.h>
32
DEFINE_LOG_ENTRY(OPAL_RC_I2C_INIT, OPAL_PLATFORM_ERR_EVT, OPAL_I2C,
33
OPAL_IO_SUBSYSTEM, OPAL_PREDICTIVE_ERR_DEGRADED_PERF,
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);
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 */
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 */
54
/* How long to keep the sensor cache disabled after an access
57
#define SENSOR_CACHE_EN_DELAY 10
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
67
* Below is the offset of registers from base which is stored in the
68
* 'struct p8_i2c_master'
71
/* I2C FIFO register */
72
#define I2C_FIFO_REG 0x4
73
#define I2C_FIFO PPC_BITMASK(0, 7)
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
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)
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)
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)
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)
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)
148
/* Pseudo-status used for timeouts */
149
#define I2C_STAT_PSEUDO_TIMEOUT PPC_BIT(63)
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)
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)
173
/* Port busy register */
174
#define I2C_PORT_BUYS_REG 0xe
176
enum p8_i2c_master_type {
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 */
193
bool irq_ok; /* Interrupt working ? */
194
bool occ_cache_dis; /* I have disabled the cache */
203
struct list_head req_list; /* Request queue head */
205
struct timer timeout;
206
struct timer recovery;
207
struct timer sensor_cache;
208
uint8_t recovery_pass;
209
struct list_node link;
212
struct p8_i2c_master_port {
213
struct i2c_bus bus; /* Abstract bus struct for the client */
214
struct p8_i2c_master *master;
216
uint32_t bit_rate_div; /* Divisor to set bus speed*/
219
struct p8_i2c_request {
220
struct i2c_request req;
225
static void p8_i2c_print_debug_info(struct p8_i2c_master_port *port,
226
struct i2c_request *req)
228
struct p8_i2c_master *master = port->master;
229
uint64_t cmd, mode, stat, estat, intr;
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);
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,
244
/* Dump the current state of i2c registers */
245
rc = xscom_read(master->chip_id, master->xscom_base + I2C_CMD_REG,
248
prlog(PR_DEBUG, "I2C: Failed to read CMD_REG\n");
252
rc = xscom_read(master->chip_id, master->xscom_base + I2C_MODE_REG,
255
prlog(PR_DEBUG, "I2C: Failed to read MODE_REG\n");
259
rc = xscom_read(master->chip_id, master->xscom_base + I2C_STAT_REG,
262
prlog(PR_DEBUG, "I2C: Failed to read STAT_REG\n");
266
rc = xscom_read(master->chip_id, master->xscom_base + I2C_EXTD_STAT_REG,
269
prlog(PR_DEBUG, "I2C: Failed to read EXTD_STAT_REG\n");
273
rc = xscom_read(master->chip_id, master->xscom_base + I2C_INTR_MASK_REG,
276
prlog(PR_DEBUG, "I2C: Failed to read INTR_MASK_REG\n");
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,
286
static bool p8_i2c_has_irqs(struct p8_i2c_master *master)
288
struct proc_chip *chip;
290
/* Centaur I2C doesn't have interrupts */
291
if (master->type == I2C_CENTAUR)
294
chip = get_chip(master->chip_id);
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 !
301
* Also we don't have interrupts for the Centaur i2c.
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:
315
static int p8_i2c_enable_irqs(struct p8_i2c_master *master)
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);
324
prlog(PR_ERR, "I2C: Failed to enable the interrupts\n");
329
static int p8_i2c_prog_watermark(struct p8_i2c_master *master)
334
rc = xscom_read(master->chip_id, master->xscom_base + I2C_WATERMARK_REG,
337
prlog(PR_ERR, "I2C: Failed to read the WATERMARK_REG\n");
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);
347
prlog(PR_ERR, "I2C: Failed to set high/low watermark level\n");
352
static int p8_i2c_prog_mode(struct p8_i2c_master_port *port, bool enhanced_mode)
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;
362
rc = xscom_read(master->chip_id, master->xscom_base +
363
I2C_MODE_REG, &mode);
365
prlog(PR_ERR, "I2C: Failed to read the MODE_REG\n");
369
mode = SETFIELD(I2C_MODE_PORT_NUM, mode, request->port_num);
370
mode = SETFIELD(I2C_MODE_BIT_RATE_DIV, mode, port->bit_rate_div);
372
mode |= I2C_MODE_ENHANCED;
374
mode &= ~I2C_MODE_ENHANCED;
378
rc = xscom_write(master->chip_id, master->xscom_base + I2C_MODE_REG,
381
prlog(PR_ERR, "I2C: Failed to write the MODE_REG\n");
386
static void p8_i2c_complete_request(struct p8_i2c_master *master,
387
struct i2c_request *req, int ret)
389
struct p8_i2c_request *request =
390
container_of(req, struct p8_i2c_request, req);
392
/* We only complete the current top level request */
393
assert(req == list_top(&master->req_list, struct i2c_request, link));
395
cancel_timer_async(&master->timeout);
396
request->timeout = 0ul;
398
list_del(&req->link);
399
master->state = state_idle;
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));
407
unlock(&master->lock);
409
req->completion(ret, req);
410
/* req might have been freed at this point */
415
static int p8_i2c_engine_reset(struct p8_i2c_master_port *port)
417
struct p8_i2c_master *master = port->master;
420
/* Reset the i2c engine */
421
rc = xscom_write(master->chip_id, master->xscom_base +
422
I2C_RESET_I2C_REG, 0);
424
log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: Failed "
425
"to reset the i2c engine\n");
429
/* Reprogram the watermark and mode */
430
rc = p8_i2c_prog_watermark(port->master);
432
log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: Failed to"
433
"program the WATERMARK_REG\n");
437
rc = p8_i2c_prog_mode(port, false);
439
log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: Failed to"
440
"program the MODE_REG\n");
445
static void p8_i2c_translate_error(struct i2c_request *req, uint64_t status)
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;
466
static void p8_i2c_status_error(struct p8_i2c_master_port *port,
467
struct i2c_request *req,
470
struct p8_i2c_master *master = port->master;
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.
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);
483
p8_i2c_translate_error(req, status);
485
rc = p8_i2c_engine_reset(port);
489
if (status & (I2C_STAT_LBUS_PARITY_ERR | I2C_STAT_ARBT_LOST_ERR |
490
I2C_STAT_STOP_ERR)) {
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
496
p8_i2c_complete_request(master, req, req->result);
499
* Reset the bus by issuing a STOP command to slave.
501
* Reprogram the mode register with 'enhanced bit' set
503
rc = p8_i2c_prog_mode(port, true);
505
log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: "
506
"Failed to program the MODE_REG\n");
510
/* Enable the interrupt */
511
p8_i2c_enable_irqs(master);
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);
518
log_simple_error(&e_info(OPAL_RC_I2C_RESET), "I2C: "
519
"Failed to issue immediate STOP\n");
526
p8_i2c_complete_request(master, req, req->result);
529
static int p8_i2c_fifo_read(struct p8_i2c_master *master,
530
uint8_t *buf, uint32_t count)
536
for (i = 0; i < count; i++, buf++) {
537
rc = xscom_read(master->chip_id, master->xscom_base +
538
I2C_FIFO_REG, &fifo);
540
log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
541
"I2C: Failed to read the fifo\n");
545
*buf = GETFIELD(I2C_FIFO, fifo);
550
static int p8_i2c_fifo_write(struct p8_i2c_master *master,
551
uint8_t *buf, uint32_t count)
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 +
562
log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
563
"I2C: Failed to write the fifo\n");
570
static void p8_i2c_status_data_request(struct p8_i2c_master *master,
571
struct i2c_request *req,
574
uint32_t fifo_count, fifo_free, count;
578
fifo_count = GETFIELD(I2C_STAT_FIFO_ENTRY_COUNT, status);
579
fifo_free = master->fifo_size - fifo_count;
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);
584
switch(master->state) {
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");
592
rc = p8_i2c_fifo_write(master, master->obuf,
596
/* For read, wait address phase to complete */
597
if (rc || req->op != SMBUS_WRITE)
600
/* For writes, transition to data phase now */
601
master->state = state_data;
602
fifo_free -= req->offset_bytes;
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,
616
buf = req->rw_buf + master->bytes_sent;
617
count = req->rw_len - master->bytes_sent;
619
/* Check direction */
620
if (req->op == I2C_READ || req->op == SMBUS_READ) {
621
if (count > fifo_count)
623
rc = p8_i2c_fifo_read(master, buf, count);
625
if (count > fifo_free)
627
rc = p8_i2c_fifo_write(master, buf, count);
630
master->bytes_sent += count;
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;
638
p8_i2c_complete_request(master, req, rc);
640
p8_i2c_enable_irqs(master);
643
static void p8_i2c_complete_offset(struct p8_i2c_master *master,
644
struct i2c_request *req)
649
DBG("Completing offset phase\n");
651
/* If it's a write, we should only get here for empty
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");
661
/* Switch to data phase */
662
master->state = state_data;
664
/* If it's not a read command, or there are no data to read,
665
* then we complete the command
667
if (req->op != SMBUS_READ || req->rw_len == 0)
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);
676
DBG("Command: %016llx, state: %d\n", cmd, master->state);
679
rc = xscom_write(master->chip_id, master->xscom_base + I2C_CMD_REG,
682
log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Failed "
683
"to write the CMD_REG\n");
687
/* Enable the interrupts */
688
p8_i2c_enable_irqs(master);
692
p8_i2c_complete_request(master, req, rc);
695
static void p8_i2c_status_cmd_completion(struct p8_i2c_master *master,
696
struct i2c_request *req)
700
DBG("Command completion, state=%d bytes_sent=%d\n",
701
master->state, master->bytes_sent);
703
/* If we complete an offset, we probably need to transition
704
* do a data read, check if that all makes sense
706
if (master->state == state_offset) {
707
p8_i2c_complete_offset(master, req);
711
/* If we are not already in error state, check if we have
712
* completed our data transfer properly
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 ? */
720
rc = master->state == state_error ? req->result : OPAL_SUCCESS;
721
p8_i2c_complete_request(master, req, rc);
724
static void p8_i2c_check_status(struct p8_i2c_master *master)
726
struct p8_i2c_master_port *port;
727
struct i2c_request *req;
731
/* If we are idle, just return, we'll catch error conditions
732
* when we next try to enqueue a request
734
if (master->state == state_idle)
737
/* Read status register */
738
rc = xscom_read(master->chip_id, master->xscom_base + I2C_STAT_REG,
741
log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Failed "
742
"to read the STAT_REG\n");
746
/* Nothing happened ? Go back */
747
if (!(status & (I2C_STAT_ANY_ERR | I2C_STAT_DATA_REQ |
751
DBG("Non-0 status: %016llx\n", status);
753
/* Mask the interrupts for this engine */
754
rc = xscom_write(master->chip_id, master->xscom_base + I2C_INTR_REG,
757
log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER), "I2C: Failed "
758
"to disable the interrupts\n");
762
/* No request ? That's not normal ! Bail out without re-enabling
765
req = list_top(&master->req_list, struct i2c_request, link);
767
log_simple_error(&e_info(OPAL_RC_I2C_TRANSFER),
768
"I2C: Interrupt with no request"
769
", status=0x%016llx\n", status);
773
/* Get port for current request */
774
port = container_of(req->bus, struct p8_i2c_master_port, bus);
776
/* Handle the status in that order: errors, data requests and
777
* command completion.
779
if (status & I2C_STAT_ANY_ERR) {
780
/* Mask status to avoid some unrelated bit overwriting
781
* our pseudo-status "timeout" bit 63
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);
790
static int p8_i2c_check_initial_status(struct p8_i2c_master_port *port)
792
struct p8_i2c_master *master = port->master;
793
uint64_t status, estat;
796
master->recovery_pass++;
798
/* Read status register */
799
rc = xscom_read(master->chip_id, master->xscom_base + I2C_STAT_REG,
802
log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
803
"to read the STAT_REG\n");
807
rc = xscom_read(master->chip_id,
808
master->xscom_base + I2C_EXTD_STAT_REG,
811
log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
812
"to read the EXTD_STAT_REG\n");
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 */
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);
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;
831
/* Mark state as "recovery" to block any other activity */
832
master->state = state_recovery;
834
/* Reset the engine */
835
p8_i2c_engine_reset(port);
837
/* Delay 5ms for bus to settle */
838
schedule_timer(&master->recovery, msecs_to_tb(5));
839
unlock(&master->lock);
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");
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;
855
master->state = state_recovery;
857
/* Delay 5ms for bus to settle */
858
schedule_timer(&master->recovery, msecs_to_tb(5));
859
unlock(&master->lock);
863
master->recovery_pass = 0;
867
static int p8_i2c_start_request(struct p8_i2c_master *master,
868
struct i2c_request *req)
870
struct p8_i2c_master_port *port;
871
struct p8_i2c_request *request =
872
container_of(req, struct p8_i2c_request, req);
876
DBG("Starting req %d len=%d addr=%02x (offset=%x)\n",
877
req->op, req->rw_len, req->dev_addr, req->offset);
880
port = container_of(req->bus, struct p8_i2c_master_port, bus);
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);
887
log_simple_error(&e_info(OPAL_RC_I2C_START_REQ),
889
"to disable the sensor cache\n");
892
master->occ_cache_dis = true;
894
/* Do we need to wait ? */
896
DBG("Waiting %lld\n", rc);
897
master->state = state_occache_dis;
898
schedule_timer(&master->recovery, rc);
903
/* Convert the offset if needed */
904
if (req->offset_bytes) {
907
for (i = 0; i < req->offset_bytes; i++) {
910
b = req->offset >> (8 * (req->offset_bytes - i - 1));
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]);
918
/* Program mode register */
919
rc = p8_i2c_prog_mode(port, false);
921
log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
922
"to program the MODE_REG\n");
927
rc = p8_i2c_check_initial_status(port);
929
master->recovery_pass = 0;
933
/* Initialize bytes_sent */
934
master->bytes_sent = 0;
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);
941
cmd |= I2C_CMD_READ_NOT_WRITE;
944
cmd |= I2C_CMD_WITH_STOP;
945
cmd = SETFIELD(I2C_CMD_LEN_BYTES, cmd, req->rw_len);
946
master->state = state_data;
949
cmd = SETFIELD(I2C_CMD_LEN_BYTES, cmd, req->offset_bytes);
950
master->state = state_offset;
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;
959
return OPAL_PARAMETER;
961
DBG("Command: %016llx, state: %d\n", cmd, master->state);
964
rc = xscom_write(master->chip_id, master->xscom_base + I2C_CMD_REG,
967
log_simple_error(&e_info(OPAL_RC_I2C_START_REQ), "I2C: Failed "
968
"to write the CMD_REG\n");
972
/* Enable the interrupts */
973
p8_i2c_enable_irqs(master);
975
/* Run a poll timer for boot cases or non-working interrupts
978
now = schedule_timer(&master->poller, master->poll_interval);
980
/* Calculate and start timeout */
981
if (request->timeout) {
982
request->timeout += now;
984
tbytes = req->rw_len + req->offset_bytes + 2;
985
request->timeout = now + tbytes * master->byte_timeout;
988
/* Start the timeout */
989
schedule_timer_at(&master->timeout, request->timeout);
994
static void p8_i2c_check_work(struct p8_i2c_master *master)
996
struct i2c_request *req;
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);
1007
static int p8_i2c_queue_request(struct i2c_request *req)
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;
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;
1021
if (req->offset_bytes > 4) {
1022
prlog(PR_ERR, "I2C: Invalid offset size %d\n", req->offset_bytes);
1023
return OPAL_PARAMETER;
1025
lock(&master->lock);
1026
list_add_tail(&master->req_list, &req->link);
1027
p8_i2c_check_work(master);
1028
unlock(&master->lock);
1033
static struct i2c_request *p8_i2c_alloc_request(struct i2c_bus *bus)
1035
struct p8_i2c_master_port *port =
1036
container_of(bus, struct p8_i2c_master_port, bus);
1037
struct p8_i2c_request *request;
1039
request = zalloc(sizeof(*request));
1041
prlog(PR_ERR, "I2C: Failed to allocate i2c request\n");
1045
request->port_num = port->port_num;
1046
request->req.bus = bus;
1048
return &request->req;
1051
static void p8_i2c_free_request(struct i2c_request *req)
1053
struct p8_i2c_request *request =
1054
container_of(req, struct p8_i2c_request, req);
1058
static void p8_i2c_set_request_timeout(struct i2c_request *req,
1061
struct p8_i2c_request *request =
1062
container_of(req, struct p8_i2c_request, req);
1064
request->timeout = msecs_to_tb(duration);
1067
static inline uint32_t p8_i2c_get_bit_rate_divisor(uint32_t lb_freq,
1070
assert(bus_speed > 0);
1071
return (((lb_freq / bus_speed) - 1) / 4);
1074
static inline uint64_t p8_i2c_get_poll_interval(uint32_t bus_speed)
1078
assert(bus_speed > 0);
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);
1085
static void p8_i2c_timeout(struct timer *t __unused, void *data, uint64_t now)
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;
1092
lock(&master->lock);
1094
/* This could be spurrious ... */
1095
if (master->state == state_idle) {
1096
DBG("I2C: Timeout in idle state\n");
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
1103
req = list_top(&master->req_list, struct i2c_request, link);
1105
DBG("I2C: Timeout with no"
1106
" pending request state=%d\n", master->state);
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");
1115
request->timeout = 0ul;
1116
port = container_of(req->bus, struct p8_i2c_master_port, bus);
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);
1123
/* Use the standard error path */
1124
p8_i2c_status_error(port, req, I2C_STAT_PSEUDO_TIMEOUT);
1126
unlock(&master->lock);
1129
static void p8_i2c_recover(struct timer *t __unused, void *data,
1130
uint64_t now __unused)
1132
struct p8_i2c_master *master = data;
1134
lock(&master->lock);
1135
assert(master->state == state_recovery ||
1136
master->state == state_occache_dis);
1137
master->state = state_idle;
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).
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;
1149
/* Re-check for new work */
1150
p8_i2c_check_work(master);
1151
unlock(&master->lock);
1154
static void p8_i2c_enable_scache(struct timer *t __unused, void *data,
1155
uint64_t now __unused)
1157
struct p8_i2c_master *master = data;
1159
lock(&master->lock);
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;
1167
unlock(&master->lock);
1170
static void p8_i2c_poll(struct timer *t __unused, void *data, uint64_t now)
1172
struct p8_i2c_master *master = data;
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.
1180
/* Lockless fast bailout */
1181
if (master->state == state_idle)
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);
1192
void p8_i2c_interrupt(uint32_t chip_id)
1194
struct proc_chip *chip = get_chip(chip_id);
1195
struct p8_i2c_master *master = NULL;
1198
list_for_each(&chip->i2cms, master, link) {
1200
/* Lockless fast bailout (shared interrupt) */
1201
if (master->state == state_idle)
1204
lock(&master->lock);
1206
/* Run the state machine */
1207
p8_i2c_check_status(master);
1209
/* Check for new work */
1210
p8_i2c_check_work(master);
1212
unlock(&master->lock);
1216
static const char *compat[] = {
1221
static void p8_i2c_add_bus_prop(struct p8_i2c_master_port *port)
1223
const struct dt_property *c, *p;
1224
struct dt_node *np = port->bus.dt_node;
1227
c = dt_find_property(np, "compatible");
1228
p = dt_find_property(np, "ibm,port-name");
1231
if (port->master->type == I2C_POWER8)
1232
dt_add_property_strings(np, "compatible",
1233
"ibm,power8-i2c-port",
1235
else if (port->master->type == I2C_CENTAUR)
1236
dt_add_property_strings(np, "compatible",
1237
"ibm,centaur-i2c-port",
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,
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,
1251
dt_add_property_string(np, "ibm,port-name", name);
1255
static void p8_i2c_init_one(struct dt_node *i2cm, enum p8_i2c_master_type type)
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;
1263
static bool irq_printed;
1266
master = zalloc(sizeof(*master));
1268
log_simple_error(&e_info(OPAL_RC_I2C_INIT),
1269
"I2C: Failed to allocate master "
1273
master->type = type;
1275
/* Local bus speed in Hz */
1276
lb_freq = dt_prop_get_u32(i2cm, "clock-frequency");
1278
/* XXX HACK for bad HB value */
1279
if (lb_freq == 600000000) {
1280
prlog(PR_ERR, "I2C: Fixing up HB bad clock freq !\n");
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);
1292
chip_list = ¢aur->i2cms;
1294
/* Detect bad device-tree from HostBoot giving us bogus
1297
if (master->engine_id > 0) {
1298
prlog(PR_ERR, "I2C: Skipping Centaur Master #1\n");
1303
struct proc_chip *chip = get_chip(master->chip_id);
1305
chip_list = &chip->i2cms;
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);
1312
prlog(PR_INFO, "I2C: Chip %08x Eng. %d\n",
1313
master->chip_id, master->engine_id);
1315
/* Disable OCC cache during inits */
1316
if (master->type == I2C_CENTAUR) {
1317
rc = centaur_disable_sensor_cache(master->chip_id);
1319
log_simple_error(&e_info(OPAL_RC_I2C_INIT), "I2C: "
1320
"Error %lld disabling sensor cache\n",
1322
/* Ignore error and move on ... */
1326
rc = xscom_read(master->chip_id, master->xscom_base +
1327
I2C_EXTD_STAT_REG, &ex_stat);
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);
1337
master->fifo_size = GETFIELD(I2C_EXTD_STAT_FIFO_SIZE, ex_stat);
1338
list_head_init(&master->req_list);
1340
/* Check if interrupt is usable */
1341
master->irq_ok = p8_i2c_has_irqs(master);
1344
prlog(PR_INFO, "I2C: Interrupts %sfunctional\n",
1345
master->irq_ok ? "" : "non-");
1348
/* Program the watermark register */
1349
rc = p8_i2c_prog_watermark(master);
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);
1355
/* Handle errors from p8_i2c_prog_watermark */
1357
log_simple_error(&e_info(OPAL_RC_I2C_INIT),
1358
"I2C: Failed to program the "
1364
/* Allocate ports driven by this master */
1366
dt_for_each_child(i2cm, i2cm_port)
1369
port = zalloc(sizeof(*port) * count);
1371
log_simple_error(&e_info(OPAL_RC_I2C_INIT),
1372
"I2C: Insufficient memory\n");
1377
/* Add master to chip's list */
1378
list_add_tail(chip_list, &master->link);
1381
dt_for_each_child(i2cm, i2cm_port) {
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);
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",
1402
(char *)dt_prop_get(i2cm_port,
1403
"ibm,port-name"), speed/1000);
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...
1413
master->poll_interval = TIMER_POLL;
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);
1421
void p8_i2c_init(void)
1423
struct dt_node *i2cm;
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);