2 * Copyright (C) 2014 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
24 #define IDM_CTRL_DIRECT_OFFSET 0x00
25 #define CFG_OFFSET 0x00
26 #define CFG_RESET_SHIFT 31
27 #define CFG_EN_SHIFT 30
28 #define CFG_SLAVE_ADDR_0_SHIFT 28
29 #define CFG_M_RETRY_CNT_SHIFT 16
30 #define CFG_M_RETRY_CNT_MASK 0x0f
32 #define TIM_CFG_OFFSET 0x04
33 #define TIM_CFG_MODE_400_SHIFT 31
34 #define TIM_RAND_SLAVE_STRETCH_SHIFT 24
35 #define TIM_RAND_SLAVE_STRETCH_MASK 0x7f
36 #define TIM_PERIODIC_SLAVE_STRETCH_SHIFT 16
37 #define TIM_PERIODIC_SLAVE_STRETCH_MASK 0x7f
39 #define S_CFG_SMBUS_ADDR_OFFSET 0x08
40 #define S_CFG_EN_NIC_SMB_ADDR3_SHIFT 31
41 #define S_CFG_NIC_SMB_ADDR3_SHIFT 24
42 #define S_CFG_NIC_SMB_ADDR3_MASK 0x7f
43 #define S_CFG_EN_NIC_SMB_ADDR2_SHIFT 23
44 #define S_CFG_NIC_SMB_ADDR2_SHIFT 16
45 #define S_CFG_NIC_SMB_ADDR2_MASK 0x7f
46 #define S_CFG_EN_NIC_SMB_ADDR1_SHIFT 15
47 #define S_CFG_NIC_SMB_ADDR1_SHIFT 8
48 #define S_CFG_NIC_SMB_ADDR1_MASK 0x7f
49 #define S_CFG_EN_NIC_SMB_ADDR0_SHIFT 7
50 #define S_CFG_NIC_SMB_ADDR0_SHIFT 0
51 #define S_CFG_NIC_SMB_ADDR0_MASK 0x7f
53 #define M_FIFO_CTRL_OFFSET 0x0c
54 #define M_FIFO_RX_FLUSH_SHIFT 31
55 #define M_FIFO_TX_FLUSH_SHIFT 30
56 #define M_FIFO_RX_CNT_SHIFT 16
57 #define M_FIFO_RX_CNT_MASK 0x7f
58 #define M_FIFO_RX_THLD_SHIFT 8
59 #define M_FIFO_RX_THLD_MASK 0x3f
61 #define S_FIFO_CTRL_OFFSET 0x10
62 #define S_FIFO_RX_FLUSH_SHIFT 31
63 #define S_FIFO_TX_FLUSH_SHIFT 30
64 #define S_FIFO_RX_CNT_SHIFT 16
65 #define S_FIFO_RX_CNT_MASK 0x7f
66 #define S_FIFO_RX_THLD_SHIFT 8
67 #define S_FIFO_RX_THLD_MASK 0x3f
69 #define M_CMD_OFFSET 0x30
70 #define M_CMD_START_BUSY_SHIFT 31
71 #define M_CMD_STATUS_SHIFT 25
72 #define M_CMD_STATUS_MASK 0x07
73 #define M_CMD_STATUS_SUCCESS 0x0
74 #define M_CMD_STATUS_LOST_ARB 0x1
75 #define M_CMD_STATUS_NACK_ADDR 0x2
76 #define M_CMD_STATUS_NACK_DATA 0x3
77 #define M_CMD_STATUS_TIMEOUT 0x4
78 #define M_CMD_STATUS_FIFO_UNDERRUN 0x5
79 #define M_CMD_STATUS_RX_FIFO_FULL 0x6
80 #define M_CMD_PROTOCOL_SHIFT 9
81 #define M_CMD_PROTOCOL_MASK 0xf
82 #define M_CMD_PROTOCOL_BLK_WR 0x7
83 #define M_CMD_PROTOCOL_BLK_RD 0x8
84 #define M_CMD_PROTOCOL_PROCESS 0xa
85 #define M_CMD_PEC_SHIFT 8
86 #define M_CMD_RD_CNT_SHIFT 0
87 #define M_CMD_RD_CNT_MASK 0xff
89 #define S_CMD_OFFSET 0x34
90 #define S_CMD_START_BUSY_SHIFT 31
91 #define S_CMD_STATUS_SHIFT 23
92 #define S_CMD_STATUS_MASK 0x07
93 #define S_CMD_STATUS_SUCCESS 0x0
94 #define S_CMD_STATUS_TIMEOUT 0x5
96 #define IE_OFFSET 0x38
97 #define IE_M_RX_FIFO_FULL_SHIFT 31
98 #define IE_M_RX_THLD_SHIFT 30
99 #define IE_M_START_BUSY_SHIFT 28
100 #define IE_M_TX_UNDERRUN_SHIFT 27
101 #define IE_S_RX_FIFO_FULL_SHIFT 26
102 #define IE_S_RX_THLD_SHIFT 25
103 #define IE_S_RX_EVENT_SHIFT 24
104 #define IE_S_START_BUSY_SHIFT 23
105 #define IE_S_TX_UNDERRUN_SHIFT 22
106 #define IE_S_RD_EVENT_SHIFT 21
108 #define IS_OFFSET 0x3c
109 #define IS_M_RX_FIFO_FULL_SHIFT 31
110 #define IS_M_RX_THLD_SHIFT 30
111 #define IS_M_START_BUSY_SHIFT 28
112 #define IS_M_TX_UNDERRUN_SHIFT 27
113 #define IS_S_RX_FIFO_FULL_SHIFT 26
114 #define IS_S_RX_THLD_SHIFT 25
115 #define IS_S_RX_EVENT_SHIFT 24
116 #define IS_S_START_BUSY_SHIFT 23
117 #define IS_S_TX_UNDERRUN_SHIFT 22
118 #define IS_S_RD_EVENT_SHIFT 21
120 #define M_TX_OFFSET 0x40
121 #define M_TX_WR_STATUS_SHIFT 31
122 #define M_TX_DATA_SHIFT 0
123 #define M_TX_DATA_MASK 0xff
125 #define M_RX_OFFSET 0x44
126 #define M_RX_STATUS_SHIFT 30
127 #define M_RX_STATUS_MASK 0x03
128 #define M_RX_PEC_ERR_SHIFT 29
129 #define M_RX_DATA_SHIFT 0
130 #define M_RX_DATA_MASK 0xff
132 #define S_TX_OFFSET 0x48
133 #define S_TX_WR_STATUS_SHIFT 31
134 #define S_TX_DATA_SHIFT 0
135 #define S_TX_DATA_MASK 0xff
137 #define S_RX_OFFSET 0x4c
138 #define S_RX_STATUS_SHIFT 30
139 #define S_RX_STATUS_MASK 0x03
140 #define S_RX_PEC_ERR_SHIFT 29
141 #define S_RX_DATA_SHIFT 0
142 #define S_RX_DATA_MASK 0xff
144 #define I2C_TIMEOUT_MSEC 50000
145 #define M_TX_RX_FIFO_SIZE 64
146 #define M_RX_FIFO_MAX_THLD_VALUE (M_TX_RX_FIFO_SIZE - 1)
148 #define M_RX_MAX_READ_LEN 255
149 #define M_RX_FIFO_THLD_VALUE 50
151 #define IE_M_ALL_INTERRUPT_SHIFT 27
152 #define IE_M_ALL_INTERRUPT_MASK 0x1e
154 #define SLAVE_READ_WRITE_BIT_MASK 0x1
155 #define SLAVE_READ_WRITE_BIT_SHIFT 0x1
156 #define SLAVE_MAX_SIZE_TRANSACTION 64
157 #define SLAVE_CLOCK_STRETCH_TIME 25
159 #define IE_S_ALL_INTERRUPT_SHIFT 21
160 #define IE_S_ALL_INTERRUPT_MASK 0x3f
162 enum i2c_slave_read_status
{
163 I2C_SLAVE_RX_FIFO_EMPTY
= 0,
169 enum bus_speed_index
{
174 enum bcm_iproc_i2c_type
{
179 struct bcm_iproc_i2c_dev
{
180 struct device
*device
;
181 enum bcm_iproc_i2c_type type
;
185 void __iomem
*idm_base
;
189 /* lock for indirect access through IDM */
192 struct i2c_adapter adapter
;
193 unsigned int bus_speed
;
195 struct completion done
;
200 struct i2c_client
*slave
;
202 /* bytes that have been transferred */
203 unsigned int tx_bytes
;
204 /* bytes that have been read */
205 unsigned int rx_bytes
;
206 unsigned int thld_bytes
;
210 * Can be expanded in the future if more interrupt status bits are utilized
212 #define ISR_MASK (BIT(IS_M_START_BUSY_SHIFT) | BIT(IS_M_TX_UNDERRUN_SHIFT)\
213 | BIT(IS_M_RX_THLD_SHIFT))
215 #define ISR_MASK_SLAVE (BIT(IS_S_START_BUSY_SHIFT)\
216 | BIT(IS_S_RX_EVENT_SHIFT) | BIT(IS_S_RD_EVENT_SHIFT)\
217 | BIT(IS_S_TX_UNDERRUN_SHIFT))
219 static int bcm_iproc_i2c_reg_slave(struct i2c_client
*slave
);
220 static int bcm_iproc_i2c_unreg_slave(struct i2c_client
*slave
);
221 static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev
*iproc_i2c
,
224 static inline u32
iproc_i2c_rd_reg(struct bcm_iproc_i2c_dev
*iproc_i2c
,
229 if (iproc_i2c
->idm_base
) {
230 spin_lock(&iproc_i2c
->idm_lock
);
231 writel(iproc_i2c
->ape_addr_mask
,
232 iproc_i2c
->idm_base
+ IDM_CTRL_DIRECT_OFFSET
);
233 val
= readl(iproc_i2c
->base
+ offset
);
234 spin_unlock(&iproc_i2c
->idm_lock
);
236 val
= readl(iproc_i2c
->base
+ offset
);
242 static inline void iproc_i2c_wr_reg(struct bcm_iproc_i2c_dev
*iproc_i2c
,
245 if (iproc_i2c
->idm_base
) {
246 spin_lock(&iproc_i2c
->idm_lock
);
247 writel(iproc_i2c
->ape_addr_mask
,
248 iproc_i2c
->idm_base
+ IDM_CTRL_DIRECT_OFFSET
);
249 writel(val
, iproc_i2c
->base
+ offset
);
250 spin_unlock(&iproc_i2c
->idm_lock
);
252 writel(val
, iproc_i2c
->base
+ offset
);
256 static void bcm_iproc_i2c_slave_init(
257 struct bcm_iproc_i2c_dev
*iproc_i2c
, bool need_reset
)
262 /* put controller in reset */
263 val
= iproc_i2c_rd_reg(iproc_i2c
, CFG_OFFSET
);
264 val
|= BIT(CFG_RESET_SHIFT
);
265 iproc_i2c_wr_reg(iproc_i2c
, CFG_OFFSET
, val
);
267 /* wait 100 usec per spec */
270 /* bring controller out of reset */
271 val
&= ~(BIT(CFG_RESET_SHIFT
));
272 iproc_i2c_wr_reg(iproc_i2c
, CFG_OFFSET
, val
);
275 /* flush TX/RX FIFOs */
276 val
= (BIT(S_FIFO_RX_FLUSH_SHIFT
) | BIT(S_FIFO_TX_FLUSH_SHIFT
));
277 iproc_i2c_wr_reg(iproc_i2c
, S_FIFO_CTRL_OFFSET
, val
);
279 /* Maximum slave stretch time */
280 val
= iproc_i2c_rd_reg(iproc_i2c
, TIM_CFG_OFFSET
);
281 val
&= ~(TIM_RAND_SLAVE_STRETCH_MASK
<< TIM_RAND_SLAVE_STRETCH_SHIFT
);
282 val
|= (SLAVE_CLOCK_STRETCH_TIME
<< TIM_RAND_SLAVE_STRETCH_SHIFT
);
283 iproc_i2c_wr_reg(iproc_i2c
, TIM_CFG_OFFSET
, val
);
285 /* Configure the slave address */
286 val
= iproc_i2c_rd_reg(iproc_i2c
, S_CFG_SMBUS_ADDR_OFFSET
);
287 val
|= BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT
);
288 val
&= ~(S_CFG_NIC_SMB_ADDR3_MASK
<< S_CFG_NIC_SMB_ADDR3_SHIFT
);
289 val
|= (iproc_i2c
->slave
->addr
<< S_CFG_NIC_SMB_ADDR3_SHIFT
);
290 iproc_i2c_wr_reg(iproc_i2c
, S_CFG_SMBUS_ADDR_OFFSET
, val
);
292 /* clear all pending slave interrupts */
293 iproc_i2c_wr_reg(iproc_i2c
, IS_OFFSET
, ISR_MASK_SLAVE
);
295 /* Enable interrupt register to indicate a valid byte in receive fifo */
296 val
= BIT(IE_S_RX_EVENT_SHIFT
);
297 /* Enable interrupt register for the Slave BUSY command */
298 val
|= BIT(IE_S_START_BUSY_SHIFT
);
299 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, val
);
302 static void bcm_iproc_i2c_check_slave_status(
303 struct bcm_iproc_i2c_dev
*iproc_i2c
)
307 val
= iproc_i2c_rd_reg(iproc_i2c
, S_CMD_OFFSET
);
308 /* status is valid only when START_BUSY is cleared after it was set */
309 if (val
& BIT(S_CMD_START_BUSY_SHIFT
))
312 val
= (val
>> S_CMD_STATUS_SHIFT
) & S_CMD_STATUS_MASK
;
313 if (val
== S_CMD_STATUS_TIMEOUT
) {
314 dev_err(iproc_i2c
->device
, "slave random stretch time timeout\n");
316 /* re-initialize i2c for recovery */
317 bcm_iproc_i2c_enable_disable(iproc_i2c
, false);
318 bcm_iproc_i2c_slave_init(iproc_i2c
, true);
319 bcm_iproc_i2c_enable_disable(iproc_i2c
, true);
323 static bool bcm_iproc_i2c_slave_isr(struct bcm_iproc_i2c_dev
*iproc_i2c
,
329 /* Slave RX byte receive */
330 if (status
& BIT(IS_S_RX_EVENT_SHIFT
)) {
331 val
= iproc_i2c_rd_reg(iproc_i2c
, S_RX_OFFSET
);
332 rx_status
= (val
>> S_RX_STATUS_SHIFT
) & S_RX_STATUS_MASK
;
333 if (rx_status
== I2C_SLAVE_RX_START
) {
334 /* Start of SMBUS for Master write */
335 i2c_slave_event(iproc_i2c
->slave
,
336 I2C_SLAVE_WRITE_REQUESTED
, &value
);
338 val
= iproc_i2c_rd_reg(iproc_i2c
, S_RX_OFFSET
);
339 value
= (u8
)((val
>> S_RX_DATA_SHIFT
) & S_RX_DATA_MASK
);
340 i2c_slave_event(iproc_i2c
->slave
,
341 I2C_SLAVE_WRITE_RECEIVED
, &value
);
342 } else if (status
& BIT(IS_S_RD_EVENT_SHIFT
)) {
343 /* Start of SMBUS for Master Read */
344 i2c_slave_event(iproc_i2c
->slave
,
345 I2C_SLAVE_READ_REQUESTED
, &value
);
346 iproc_i2c_wr_reg(iproc_i2c
, S_TX_OFFSET
, value
);
348 val
= BIT(S_CMD_START_BUSY_SHIFT
);
349 iproc_i2c_wr_reg(iproc_i2c
, S_CMD_OFFSET
, val
);
352 * Enable interrupt for TX FIFO becomes empty and
353 * less than PKT_LENGTH bytes were output on the SMBUS
355 val
= iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
356 val
|= BIT(IE_S_TX_UNDERRUN_SHIFT
);
357 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, val
);
359 /* Master write other than start */
360 value
= (u8
)((val
>> S_RX_DATA_SHIFT
) & S_RX_DATA_MASK
);
361 i2c_slave_event(iproc_i2c
->slave
,
362 I2C_SLAVE_WRITE_RECEIVED
, &value
);
363 if (rx_status
== I2C_SLAVE_RX_END
)
364 i2c_slave_event(iproc_i2c
->slave
,
365 I2C_SLAVE_STOP
, &value
);
367 } else if (status
& BIT(IS_S_TX_UNDERRUN_SHIFT
)) {
368 /* Master read other than start */
369 i2c_slave_event(iproc_i2c
->slave
,
370 I2C_SLAVE_READ_PROCESSED
, &value
);
372 iproc_i2c_wr_reg(iproc_i2c
, S_TX_OFFSET
, value
);
373 val
= BIT(S_CMD_START_BUSY_SHIFT
);
374 iproc_i2c_wr_reg(iproc_i2c
, S_CMD_OFFSET
, val
);
378 if (status
& BIT(IS_S_START_BUSY_SHIFT
)) {
379 i2c_slave_event(iproc_i2c
->slave
, I2C_SLAVE_STOP
, &value
);
381 * Enable interrupt for TX FIFO becomes empty and
382 * less than PKT_LENGTH bytes were output on the SMBUS
384 val
= iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
385 val
&= ~BIT(IE_S_TX_UNDERRUN_SHIFT
);
386 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, val
);
389 /* clear interrupt status */
390 iproc_i2c_wr_reg(iproc_i2c
, IS_OFFSET
, status
);
392 bcm_iproc_i2c_check_slave_status(iproc_i2c
);
396 static void bcm_iproc_i2c_read_valid_bytes(struct bcm_iproc_i2c_dev
*iproc_i2c
)
398 struct i2c_msg
*msg
= iproc_i2c
->msg
;
401 /* Read valid data from RX FIFO */
402 while (iproc_i2c
->rx_bytes
< msg
->len
) {
403 val
= iproc_i2c_rd_reg(iproc_i2c
, M_RX_OFFSET
);
406 if (!((val
>> M_RX_STATUS_SHIFT
) & M_RX_STATUS_MASK
))
409 msg
->buf
[iproc_i2c
->rx_bytes
] =
410 (val
>> M_RX_DATA_SHIFT
) & M_RX_DATA_MASK
;
411 iproc_i2c
->rx_bytes
++;
415 static void bcm_iproc_i2c_send(struct bcm_iproc_i2c_dev
*iproc_i2c
)
417 struct i2c_msg
*msg
= iproc_i2c
->msg
;
418 unsigned int tx_bytes
= msg
->len
- iproc_i2c
->tx_bytes
;
422 /* can only fill up to the FIFO size */
423 tx_bytes
= min_t(unsigned int, tx_bytes
, M_TX_RX_FIFO_SIZE
);
424 for (i
= 0; i
< tx_bytes
; i
++) {
425 /* start from where we left over */
426 unsigned int idx
= iproc_i2c
->tx_bytes
+ i
;
430 /* mark the last byte */
431 if (idx
== msg
->len
- 1) {
432 val
|= BIT(M_TX_WR_STATUS_SHIFT
);
434 if (iproc_i2c
->irq
) {
438 * Since this is the last byte, we should now
439 * disable TX FIFO underrun interrupt
441 tmp
= iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
442 tmp
&= ~BIT(IE_M_TX_UNDERRUN_SHIFT
);
443 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
,
448 /* load data into TX FIFO */
449 iproc_i2c_wr_reg(iproc_i2c
, M_TX_OFFSET
, val
);
452 /* update number of transferred bytes */
453 iproc_i2c
->tx_bytes
+= tx_bytes
;
456 static void bcm_iproc_i2c_read(struct bcm_iproc_i2c_dev
*iproc_i2c
)
458 struct i2c_msg
*msg
= iproc_i2c
->msg
;
461 bcm_iproc_i2c_read_valid_bytes(iproc_i2c
);
462 bytes_left
= msg
->len
- iproc_i2c
->rx_bytes
;
463 if (bytes_left
== 0) {
464 if (iproc_i2c
->irq
) {
465 /* finished reading all data, disable rx thld event */
466 val
= iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
467 val
&= ~BIT(IS_M_RX_THLD_SHIFT
);
468 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, val
);
470 } else if (bytes_left
< iproc_i2c
->thld_bytes
) {
471 /* set bytes left as threshold */
472 val
= iproc_i2c_rd_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
);
473 val
&= ~(M_FIFO_RX_THLD_MASK
<< M_FIFO_RX_THLD_SHIFT
);
474 val
|= (bytes_left
<< M_FIFO_RX_THLD_SHIFT
);
475 iproc_i2c_wr_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
, val
);
476 iproc_i2c
->thld_bytes
= bytes_left
;
479 * bytes_left >= iproc_i2c->thld_bytes,
480 * hence no need to change the THRESHOLD SET.
481 * It will remain as iproc_i2c->thld_bytes itself
485 static void bcm_iproc_i2c_process_m_event(struct bcm_iproc_i2c_dev
*iproc_i2c
,
488 /* TX FIFO is empty and we have more data to send */
489 if (status
& BIT(IS_M_TX_UNDERRUN_SHIFT
))
490 bcm_iproc_i2c_send(iproc_i2c
);
492 /* RX FIFO threshold is reached and data needs to be read out */
493 if (status
& BIT(IS_M_RX_THLD_SHIFT
))
494 bcm_iproc_i2c_read(iproc_i2c
);
496 /* transfer is done */
497 if (status
& BIT(IS_M_START_BUSY_SHIFT
)) {
498 iproc_i2c
->xfer_is_done
= 1;
500 complete(&iproc_i2c
->done
);
504 static irqreturn_t
bcm_iproc_i2c_isr(int irq
, void *data
)
506 struct bcm_iproc_i2c_dev
*iproc_i2c
= data
;
507 u32 status
= iproc_i2c_rd_reg(iproc_i2c
, IS_OFFSET
);
509 u32 sl_status
= status
& ISR_MASK_SLAVE
;
512 ret
= bcm_iproc_i2c_slave_isr(iproc_i2c
, sl_status
);
523 /* process all master based events */
524 bcm_iproc_i2c_process_m_event(iproc_i2c
, status
);
525 iproc_i2c_wr_reg(iproc_i2c
, IS_OFFSET
, status
);
530 static int bcm_iproc_i2c_init(struct bcm_iproc_i2c_dev
*iproc_i2c
)
534 /* put controller in reset */
535 val
= iproc_i2c_rd_reg(iproc_i2c
, CFG_OFFSET
);
536 val
|= BIT(CFG_RESET_SHIFT
);
537 val
&= ~(BIT(CFG_EN_SHIFT
));
538 iproc_i2c_wr_reg(iproc_i2c
, CFG_OFFSET
, val
);
540 /* wait 100 usec per spec */
543 /* bring controller out of reset */
544 val
&= ~(BIT(CFG_RESET_SHIFT
));
545 iproc_i2c_wr_reg(iproc_i2c
, CFG_OFFSET
, val
);
547 /* flush TX/RX FIFOs and set RX FIFO threshold to zero */
548 val
= (BIT(M_FIFO_RX_FLUSH_SHIFT
) | BIT(M_FIFO_TX_FLUSH_SHIFT
));
549 iproc_i2c_wr_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
, val
);
550 /* disable all interrupts */
551 val
= iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
552 val
&= ~(IE_M_ALL_INTERRUPT_MASK
<<
553 IE_M_ALL_INTERRUPT_SHIFT
);
554 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, val
);
556 /* clear all pending interrupts */
557 iproc_i2c_wr_reg(iproc_i2c
, IS_OFFSET
, 0xffffffff);
562 static void bcm_iproc_i2c_enable_disable(struct bcm_iproc_i2c_dev
*iproc_i2c
,
567 val
= iproc_i2c_rd_reg(iproc_i2c
, CFG_OFFSET
);
569 val
|= BIT(CFG_EN_SHIFT
);
571 val
&= ~BIT(CFG_EN_SHIFT
);
572 iproc_i2c_wr_reg(iproc_i2c
, CFG_OFFSET
, val
);
575 static int bcm_iproc_i2c_check_status(struct bcm_iproc_i2c_dev
*iproc_i2c
,
580 val
= iproc_i2c_rd_reg(iproc_i2c
, M_CMD_OFFSET
);
581 val
= (val
>> M_CMD_STATUS_SHIFT
) & M_CMD_STATUS_MASK
;
584 case M_CMD_STATUS_SUCCESS
:
587 case M_CMD_STATUS_LOST_ARB
:
588 dev_dbg(iproc_i2c
->device
, "lost bus arbitration\n");
591 case M_CMD_STATUS_NACK_ADDR
:
592 dev_dbg(iproc_i2c
->device
, "NAK addr:0x%02x\n", msg
->addr
);
595 case M_CMD_STATUS_NACK_DATA
:
596 dev_dbg(iproc_i2c
->device
, "NAK data\n");
599 case M_CMD_STATUS_TIMEOUT
:
600 dev_dbg(iproc_i2c
->device
, "bus timeout\n");
603 case M_CMD_STATUS_FIFO_UNDERRUN
:
604 dev_dbg(iproc_i2c
->device
, "FIFO under-run\n");
607 case M_CMD_STATUS_RX_FIFO_FULL
:
608 dev_dbg(iproc_i2c
->device
, "RX FIFO full\n");
612 dev_dbg(iproc_i2c
->device
, "unknown error code=%d\n", val
);
614 /* re-initialize i2c for recovery */
615 bcm_iproc_i2c_enable_disable(iproc_i2c
, false);
616 bcm_iproc_i2c_init(iproc_i2c
);
617 bcm_iproc_i2c_enable_disable(iproc_i2c
, true);
623 static int bcm_iproc_i2c_xfer_wait(struct bcm_iproc_i2c_dev
*iproc_i2c
,
627 unsigned long time_left
= msecs_to_jiffies(I2C_TIMEOUT_MSEC
);
631 iproc_i2c_wr_reg(iproc_i2c
, M_CMD_OFFSET
, cmd
);
633 if (iproc_i2c
->irq
) {
634 time_left
= wait_for_completion_timeout(&iproc_i2c
->done
,
636 /* disable all interrupts */
637 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, 0);
638 /* read it back to flush the write */
639 iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
640 /* make sure the interrupt handler isn't running */
641 synchronize_irq(iproc_i2c
->irq
);
643 } else { /* polling mode */
644 unsigned long timeout
= jiffies
+ time_left
;
647 status
= iproc_i2c_rd_reg(iproc_i2c
,
648 IS_OFFSET
) & ISR_MASK
;
649 bcm_iproc_i2c_process_m_event(iproc_i2c
, status
);
650 iproc_i2c_wr_reg(iproc_i2c
, IS_OFFSET
, status
);
652 if (time_after(jiffies
, timeout
)) {
659 } while (!iproc_i2c
->xfer_is_done
);
662 if (!time_left
&& !iproc_i2c
->xfer_is_done
) {
663 dev_err(iproc_i2c
->device
, "transaction timed out\n");
665 /* flush both TX/RX FIFOs */
666 val
= BIT(M_FIFO_RX_FLUSH_SHIFT
) | BIT(M_FIFO_TX_FLUSH_SHIFT
);
667 iproc_i2c_wr_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
, val
);
671 ret
= bcm_iproc_i2c_check_status(iproc_i2c
, msg
);
673 /* flush both TX/RX FIFOs */
674 val
= BIT(M_FIFO_RX_FLUSH_SHIFT
) | BIT(M_FIFO_TX_FLUSH_SHIFT
);
675 iproc_i2c_wr_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
, val
);
683 * If 'process_call' is true, then this is a multi-msg transfer that requires
684 * a repeated start between the messages.
685 * More specifically, it must be a write (reg) followed by a read (data).
686 * The i2c quirks are set to enforce this rule.
688 static int bcm_iproc_i2c_xfer_internal(struct bcm_iproc_i2c_dev
*iproc_i2c
,
689 struct i2c_msg
*msgs
, bool process_call
)
693 u32 val
, tmp
, val_intr_en
;
694 unsigned int tx_bytes
;
695 struct i2c_msg
*msg
= &msgs
[0];
697 /* check if bus is busy */
698 if (!!(iproc_i2c_rd_reg(iproc_i2c
,
699 M_CMD_OFFSET
) & BIT(M_CMD_START_BUSY_SHIFT
))) {
700 dev_warn(iproc_i2c
->device
, "bus is busy\n");
704 iproc_i2c
->msg
= msg
;
706 /* format and load slave address into the TX FIFO */
707 addr
= i2c_8bit_addr_from_msg(msg
);
708 iproc_i2c_wr_reg(iproc_i2c
, M_TX_OFFSET
, addr
);
711 * For a write transaction, load data into the TX FIFO. Only allow
712 * loading up to TX FIFO size - 1 bytes of data since the first byte
713 * has been used up by the slave address
715 tx_bytes
= min_t(unsigned int, msg
->len
, M_TX_RX_FIFO_SIZE
- 1);
716 if (!(msg
->flags
& I2C_M_RD
)) {
717 for (i
= 0; i
< tx_bytes
; i
++) {
720 /* mark the last byte */
721 if (!process_call
&& (i
== msg
->len
- 1))
722 val
|= 1 << M_TX_WR_STATUS_SHIFT
;
724 iproc_i2c_wr_reg(iproc_i2c
, M_TX_OFFSET
, val
);
726 iproc_i2c
->tx_bytes
= tx_bytes
;
729 /* Process the read message if this is process call */
732 iproc_i2c
->msg
= msg
; /* point to second msg */
735 * The last byte to be sent out should be a slave
736 * address with read operation
738 addr
= i2c_8bit_addr_from_msg(msg
);
739 /* mark it the last byte out */
740 val
= addr
| (1 << M_TX_WR_STATUS_SHIFT
);
741 iproc_i2c_wr_reg(iproc_i2c
, M_TX_OFFSET
, val
);
744 /* mark as incomplete before starting the transaction */
746 reinit_completion(&iproc_i2c
->done
);
748 iproc_i2c
->xfer_is_done
= 0;
751 * Enable the "start busy" interrupt, which will be triggered after the
752 * transaction is done, i.e., the internal start_busy bit, transitions
755 val_intr_en
= BIT(IE_M_START_BUSY_SHIFT
);
758 * If TX data size is larger than the TX FIFO, need to enable TX
759 * underrun interrupt, which will be triggerred when the TX FIFO is
760 * empty. When that happens we can then pump more data into the FIFO
762 if (!process_call
&& !(msg
->flags
& I2C_M_RD
) &&
763 msg
->len
> iproc_i2c
->tx_bytes
)
764 val_intr_en
|= BIT(IE_M_TX_UNDERRUN_SHIFT
);
767 * Now we can activate the transfer. For a read operation, specify the
768 * number of bytes to read
770 val
= BIT(M_CMD_START_BUSY_SHIFT
);
771 if (msg
->flags
& I2C_M_RD
) {
774 iproc_i2c
->rx_bytes
= 0;
775 if (msg
->len
> M_RX_FIFO_MAX_THLD_VALUE
)
776 iproc_i2c
->thld_bytes
= M_RX_FIFO_THLD_VALUE
;
778 iproc_i2c
->thld_bytes
= msg
->len
;
780 /* set threshold value */
781 tmp
= iproc_i2c_rd_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
);
782 tmp
&= ~(M_FIFO_RX_THLD_MASK
<< M_FIFO_RX_THLD_SHIFT
);
783 tmp
|= iproc_i2c
->thld_bytes
<< M_FIFO_RX_THLD_SHIFT
;
784 iproc_i2c_wr_reg(iproc_i2c
, M_FIFO_CTRL_OFFSET
, tmp
);
786 /* enable the RX threshold interrupt */
787 val_intr_en
|= BIT(IE_M_RX_THLD_SHIFT
);
789 protocol
= process_call
?
790 M_CMD_PROTOCOL_PROCESS
: M_CMD_PROTOCOL_BLK_RD
;
792 val
|= (protocol
<< M_CMD_PROTOCOL_SHIFT
) |
793 (msg
->len
<< M_CMD_RD_CNT_SHIFT
);
795 val
|= (M_CMD_PROTOCOL_BLK_WR
<< M_CMD_PROTOCOL_SHIFT
);
799 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, val_intr_en
);
801 return bcm_iproc_i2c_xfer_wait(iproc_i2c
, msg
, val
);
804 static int bcm_iproc_i2c_xfer(struct i2c_adapter
*adapter
,
805 struct i2c_msg msgs
[], int num
)
807 struct bcm_iproc_i2c_dev
*iproc_i2c
= i2c_get_adapdata(adapter
);
808 bool process_call
= false;
812 /* Repeated start, use process call */
814 if (msgs
[1].flags
& I2C_M_NOSTART
) {
815 dev_err(iproc_i2c
->device
, "Invalid repeated start\n");
820 ret
= bcm_iproc_i2c_xfer_internal(iproc_i2c
, msgs
, process_call
);
822 dev_dbg(iproc_i2c
->device
, "xfer failed\n");
829 static uint32_t bcm_iproc_i2c_functionality(struct i2c_adapter
*adap
)
833 /* We do not support the SMBUS Quick command */
834 val
= I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
836 if (adap
->algo
->reg_slave
)
837 val
|= I2C_FUNC_SLAVE
;
842 static struct i2c_algorithm bcm_iproc_algo
= {
843 .master_xfer
= bcm_iproc_i2c_xfer
,
844 .functionality
= bcm_iproc_i2c_functionality
,
845 .reg_slave
= bcm_iproc_i2c_reg_slave
,
846 .unreg_slave
= bcm_iproc_i2c_unreg_slave
,
849 static const struct i2c_adapter_quirks bcm_iproc_i2c_quirks
= {
850 .flags
= I2C_AQ_COMB_WRITE_THEN_READ
,
851 .max_comb_1st_msg_len
= M_TX_RX_FIFO_SIZE
,
852 .max_read_len
= M_RX_MAX_READ_LEN
,
855 static int bcm_iproc_i2c_cfg_speed(struct bcm_iproc_i2c_dev
*iproc_i2c
)
857 unsigned int bus_speed
;
859 int ret
= of_property_read_u32(iproc_i2c
->device
->of_node
,
860 "clock-frequency", &bus_speed
);
862 dev_info(iproc_i2c
->device
,
863 "unable to interpret clock-frequency DT property\n");
864 bus_speed
= I2C_MAX_STANDARD_MODE_FREQ
;
867 if (bus_speed
< I2C_MAX_STANDARD_MODE_FREQ
) {
868 dev_err(iproc_i2c
->device
, "%d Hz bus speed not supported\n",
870 dev_err(iproc_i2c
->device
,
871 "valid speeds are 100khz and 400khz\n");
873 } else if (bus_speed
< I2C_MAX_FAST_MODE_FREQ
) {
874 bus_speed
= I2C_MAX_STANDARD_MODE_FREQ
;
876 bus_speed
= I2C_MAX_FAST_MODE_FREQ
;
879 iproc_i2c
->bus_speed
= bus_speed
;
880 val
= iproc_i2c_rd_reg(iproc_i2c
, TIM_CFG_OFFSET
);
881 val
&= ~BIT(TIM_CFG_MODE_400_SHIFT
);
882 val
|= (bus_speed
== I2C_MAX_FAST_MODE_FREQ
) << TIM_CFG_MODE_400_SHIFT
;
883 iproc_i2c_wr_reg(iproc_i2c
, TIM_CFG_OFFSET
, val
);
885 dev_info(iproc_i2c
->device
, "bus set to %u Hz\n", bus_speed
);
890 static int bcm_iproc_i2c_probe(struct platform_device
*pdev
)
893 struct bcm_iproc_i2c_dev
*iproc_i2c
;
894 struct i2c_adapter
*adap
;
895 struct resource
*res
;
897 iproc_i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*iproc_i2c
),
902 platform_set_drvdata(pdev
, iproc_i2c
);
903 iproc_i2c
->device
= &pdev
->dev
;
905 (enum bcm_iproc_i2c_type
)of_device_get_match_data(&pdev
->dev
);
906 init_completion(&iproc_i2c
->done
);
908 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
909 iproc_i2c
->base
= devm_ioremap_resource(iproc_i2c
->device
, res
);
910 if (IS_ERR(iproc_i2c
->base
))
911 return PTR_ERR(iproc_i2c
->base
);
913 if (iproc_i2c
->type
== IPROC_I2C_NIC
) {
914 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
915 iproc_i2c
->idm_base
= devm_ioremap_resource(iproc_i2c
->device
,
917 if (IS_ERR(iproc_i2c
->idm_base
))
918 return PTR_ERR(iproc_i2c
->idm_base
);
920 ret
= of_property_read_u32(iproc_i2c
->device
->of_node
,
921 "brcm,ape-hsls-addr-mask",
922 &iproc_i2c
->ape_addr_mask
);
924 dev_err(iproc_i2c
->device
,
925 "'brcm,ape-hsls-addr-mask' missing\n");
929 spin_lock_init(&iproc_i2c
->idm_lock
);
931 /* no slave support */
932 bcm_iproc_algo
.reg_slave
= NULL
;
933 bcm_iproc_algo
.unreg_slave
= NULL
;
936 ret
= bcm_iproc_i2c_init(iproc_i2c
);
940 ret
= bcm_iproc_i2c_cfg_speed(iproc_i2c
);
944 irq
= platform_get_irq(pdev
, 0);
946 ret
= devm_request_irq(iproc_i2c
->device
, irq
,
947 bcm_iproc_i2c_isr
, 0, pdev
->name
,
950 dev_err(iproc_i2c
->device
,
951 "unable to request irq %i\n", irq
);
955 iproc_i2c
->irq
= irq
;
957 dev_warn(iproc_i2c
->device
,
958 "no irq resource, falling back to poll mode\n");
961 bcm_iproc_i2c_enable_disable(iproc_i2c
, true);
963 adap
= &iproc_i2c
->adapter
;
964 i2c_set_adapdata(adap
, iproc_i2c
);
965 snprintf(adap
->name
, sizeof(adap
->name
),
966 "Broadcom iProc (%s)",
967 of_node_full_name(iproc_i2c
->device
->of_node
));
968 adap
->algo
= &bcm_iproc_algo
;
969 adap
->quirks
= &bcm_iproc_i2c_quirks
;
970 adap
->dev
.parent
= &pdev
->dev
;
971 adap
->dev
.of_node
= pdev
->dev
.of_node
;
973 return i2c_add_adapter(adap
);
976 static int bcm_iproc_i2c_remove(struct platform_device
*pdev
)
978 struct bcm_iproc_i2c_dev
*iproc_i2c
= platform_get_drvdata(pdev
);
980 if (iproc_i2c
->irq
) {
982 * Make sure there's no pending interrupt when we remove the
985 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, 0);
986 iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
987 synchronize_irq(iproc_i2c
->irq
);
990 i2c_del_adapter(&iproc_i2c
->adapter
);
991 bcm_iproc_i2c_enable_disable(iproc_i2c
, false);
996 #ifdef CONFIG_PM_SLEEP
998 static int bcm_iproc_i2c_suspend(struct device
*dev
)
1000 struct bcm_iproc_i2c_dev
*iproc_i2c
= dev_get_drvdata(dev
);
1002 if (iproc_i2c
->irq
) {
1004 * Make sure there's no pending interrupt when we go into
1007 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, 0);
1008 iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
1009 synchronize_irq(iproc_i2c
->irq
);
1012 /* now disable the controller */
1013 bcm_iproc_i2c_enable_disable(iproc_i2c
, false);
1018 static int bcm_iproc_i2c_resume(struct device
*dev
)
1020 struct bcm_iproc_i2c_dev
*iproc_i2c
= dev_get_drvdata(dev
);
1025 * Power domain could have been shut off completely in system deep
1026 * sleep, so re-initialize the block here
1028 ret
= bcm_iproc_i2c_init(iproc_i2c
);
1032 /* configure to the desired bus speed */
1033 val
= iproc_i2c_rd_reg(iproc_i2c
, TIM_CFG_OFFSET
);
1034 val
&= ~BIT(TIM_CFG_MODE_400_SHIFT
);
1035 val
|= (iproc_i2c
->bus_speed
== I2C_MAX_FAST_MODE_FREQ
) << TIM_CFG_MODE_400_SHIFT
;
1036 iproc_i2c_wr_reg(iproc_i2c
, TIM_CFG_OFFSET
, val
);
1038 bcm_iproc_i2c_enable_disable(iproc_i2c
, true);
1043 static const struct dev_pm_ops bcm_iproc_i2c_pm_ops
= {
1044 .suspend_late
= &bcm_iproc_i2c_suspend
,
1045 .resume_early
= &bcm_iproc_i2c_resume
1048 #define BCM_IPROC_I2C_PM_OPS (&bcm_iproc_i2c_pm_ops)
1050 #define BCM_IPROC_I2C_PM_OPS NULL
1051 #endif /* CONFIG_PM_SLEEP */
1054 static int bcm_iproc_i2c_reg_slave(struct i2c_client
*slave
)
1056 struct bcm_iproc_i2c_dev
*iproc_i2c
= i2c_get_adapdata(slave
->adapter
);
1058 if (iproc_i2c
->slave
)
1061 if (slave
->flags
& I2C_CLIENT_TEN
)
1062 return -EAFNOSUPPORT
;
1064 iproc_i2c
->slave
= slave
;
1065 bcm_iproc_i2c_slave_init(iproc_i2c
, false);
1069 static int bcm_iproc_i2c_unreg_slave(struct i2c_client
*slave
)
1072 struct bcm_iproc_i2c_dev
*iproc_i2c
= i2c_get_adapdata(slave
->adapter
);
1074 if (!iproc_i2c
->slave
)
1077 iproc_i2c
->slave
= NULL
;
1079 /* disable all slave interrupts */
1080 tmp
= iproc_i2c_rd_reg(iproc_i2c
, IE_OFFSET
);
1081 tmp
&= ~(IE_S_ALL_INTERRUPT_MASK
<<
1082 IE_S_ALL_INTERRUPT_SHIFT
);
1083 iproc_i2c_wr_reg(iproc_i2c
, IE_OFFSET
, tmp
);
1085 /* Erase the slave address programmed */
1086 tmp
= iproc_i2c_rd_reg(iproc_i2c
, S_CFG_SMBUS_ADDR_OFFSET
);
1087 tmp
&= ~BIT(S_CFG_EN_NIC_SMB_ADDR3_SHIFT
);
1088 iproc_i2c_wr_reg(iproc_i2c
, S_CFG_SMBUS_ADDR_OFFSET
, tmp
);
1093 static const struct of_device_id bcm_iproc_i2c_of_match
[] = {
1095 .compatible
= "brcm,iproc-i2c",
1096 .data
= (int *)IPROC_I2C
,
1098 .compatible
= "brcm,iproc-nic-i2c",
1099 .data
= (int *)IPROC_I2C_NIC
,
1103 MODULE_DEVICE_TABLE(of
, bcm_iproc_i2c_of_match
);
1105 static struct platform_driver bcm_iproc_i2c_driver
= {
1107 .name
= "bcm-iproc-i2c",
1108 .of_match_table
= bcm_iproc_i2c_of_match
,
1109 .pm
= BCM_IPROC_I2C_PM_OPS
,
1111 .probe
= bcm_iproc_i2c_probe
,
1112 .remove
= bcm_iproc_i2c_remove
,
1114 module_platform_driver(bcm_iproc_i2c_driver
);
1116 MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1117 MODULE_DESCRIPTION("Broadcom iProc I2C Driver");
1118 MODULE_LICENSE("GPL v2");