Lines Matching +full:npcm750 +full:- +full:i2c

1 // SPDX-License-Identifier: GPL-2.0
3 * Nuvoton NPCM7xx I2C Controller driver
11 #include <linux/i2c.h>
29 * External I2C Interface driver xfer indication values, which indicate status
58 /* I2C Bank (module had 2 banks of registers) */
64 /* Internal I2C states values (for the I2C module state machine). */
125 #define NPCM_I2CTXF_CTL 0x12 /* Tx-FIFO Control */
128 #define NPCM_I2CTXF_STS 0x1A /* Tx-FIFO Status */
129 #define NPCM_I2CRXF_STS 0x1C /* Rx-FIFO Status */
130 #define NPCM_I2CRXF_CTL 0x1E /* Rx-FIFO Control */
549 /* Status of one I2C module */
603 u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
609 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
614 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_init_params()
615 bus->rd_size = 0; in npcm_i2c_init_params()
616 bus->wr_size = 0; in npcm_i2c_init_params()
617 bus->rd_ind = 0; in npcm_i2c_init_params()
618 bus->wr_ind = 0; in npcm_i2c_init_params()
619 bus->read_block_use = false; in npcm_i2c_init_params()
620 bus->int_time_stamp = 0; in npcm_i2c_init_params()
621 bus->PEC_use = false; in npcm_i2c_init_params()
622 bus->PEC_mask = 0; in npcm_i2c_init_params()
624 if (bus->slave) in npcm_i2c_init_params()
625 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_init_params()
631 iowrite8(data, bus->reg + NPCM_I2CSDA); in npcm_i2c_wr_byte()
636 return ioread8(bus->reg + NPCM_I2CSDA); in npcm_i2c_rd_byte()
643 return !!(I2CCTL3_SCL_LVL & ioread8(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SCL()
650 return !!(I2CCTL3_SDA_LVL & ioread8(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SDA()
655 if (bus->operation == I2C_READ_OPER) in npcm_i2c_get_index()
656 return bus->rd_ind; in npcm_i2c_get_index()
657 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_get_index()
658 return bus->wr_ind; in npcm_i2c_get_index()
665 return bus->wr_size == 0 && bus->rd_size == 0; in npcm_i2c_is_quick()
677 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_disable()
681 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
683 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
685 bus->state = I2C_DISABLE; in npcm_i2c_disable()
690 u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
693 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
694 bus->state = I2C_IDLE; in npcm_i2c_enable()
703 val = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
705 iowrite8(val, bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
707 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
713 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
720 tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_tx_fifo_empty()
722 if ((tx_fifo_sts & bus->data->txf_sts_tx_bytes) == 0) in npcm_i2c_tx_fifo_empty()
733 rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_rx_fifo_full()
735 if ((rx_fifo_sts & bus->data->rxf_sts_rx_bytes) == 0) in npcm_i2c_rx_fifo_full()
746 val = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
748 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
755 val = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
757 iowrite8(val, bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
764 val = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
766 iowrite8(val, bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
773 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
779 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
786 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
789 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
797 * override HW issue: I2C may fail to supply stop condition in Master in npcm_i2c_master_stop()
802 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
805 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
807 if (!bus->fifo_use) in npcm_i2c_master_stop()
812 if (bus->operation == I2C_READ_OPER) in npcm_i2c_master_stop()
817 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_master_stop()
824 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
830 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
837 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
840 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
849 iowrite8(val, bus->reg + NPCM_I2CST); in npcm_i2c_clear_master_status()
858 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
864 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
876 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
881 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
884 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
889 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
893 dev_err(bus->dev, "try to enable more than 2 SA not supported\n"); in npcm_i2c_slave_enable()
896 return -EFAULT; in npcm_i2c_slave_enable()
899 iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]); in npcm_i2c_slave_enable()
917 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
924 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
927 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_reset()
928 iowrite8(0xFF, bus->reg + NPCM_I2CST); in npcm_i2c_reset()
934 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_reset()
937 if (bus->slave) { in npcm_i2c_reset()
938 addr = bus->slave->addr; in npcm_i2c_reset()
946 bus->state = I2C_IDLE; in npcm_i2c_reset()
951 return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST)); in npcm_i2c_is_master()
961 msgs = bus->msgs; in npcm_i2c_callback()
962 msgs_num = bus->msgs_num; in npcm_i2c_callback()
964 * check that transaction was not timed-out, and msgs still in npcm_i2c_callback()
970 if (completion_done(&bus->cmd_complete)) in npcm_i2c_callback()
975 bus->cmd_err = bus->msgs_num; in npcm_i2c_callback()
976 if (bus->tx_complete_cnt < ULLONG_MAX) in npcm_i2c_callback()
977 bus->tx_complete_cnt++; in npcm_i2c_callback()
981 if (bus->msgs) { in npcm_i2c_callback()
992 bus->cmd_err = -ENXIO; in npcm_i2c_callback()
997 bus->cmd_err = -EAGAIN; in npcm_i2c_callback()
1001 /* I2C wake up */ in npcm_i2c_callback()
1007 bus->operation = I2C_NO_OPER; in npcm_i2c_callback()
1009 if (bus->slave) in npcm_i2c_callback()
1010 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_callback()
1013 complete(&bus->cmd_complete); in npcm_i2c_callback()
1018 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_fifo_usage()
1019 return (bus->data->txf_sts_tx_bytes & in npcm_i2c_fifo_usage()
1020 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_fifo_usage()
1021 if (bus->operation == I2C_READ_OPER) in npcm_i2c_fifo_usage()
1022 return (bus->data->rxf_sts_rx_bytes & in npcm_i2c_fifo_usage()
1023 ioread8(bus->reg + NPCM_I2CRXF_STS)); in npcm_i2c_fifo_usage()
1035 size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
1036 while (max_bytes-- && size_free_fifo) { in npcm_i2c_write_to_fifo_master()
1037 if (bus->wr_ind < bus->wr_size) in npcm_i2c_write_to_fifo_master()
1038 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_write_to_fifo_master()
1041 size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
1047 * configure the FIFO before using it. If nread is -1 RX FIFO will not be
1054 if (!bus->fifo_use) in npcm_i2c_set_fifo()
1062 rxf_ctl = min_t(int, nread, bus->data->fifo_size); in npcm_i2c_set_fifo()
1065 if (nread <= bus->data->fifo_size) in npcm_i2c_set_fifo()
1066 rxf_ctl |= bus->data->rxf_ctl_last_pec; in npcm_i2c_set_fifo()
1073 if (bus->rd_ind == 0 && bus->read_block_use) { in npcm_i2c_set_fifo()
1079 iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_set_fifo()
1084 if (nwrite > bus->data->fifo_size) in npcm_i2c_set_fifo()
1086 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
1088 iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
1098 while (bytes_in_fifo--) { in npcm_i2c_read_fifo()
1100 if (bus->rd_ind < bus->rd_size) in npcm_i2c_read_fifo()
1101 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_read_fifo()
1122 dev_err(bus->dev, "get slave: try to use more than 2 SA not supported\n"); in npcm_i2c_get_slave_addr()
1124 slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]); in npcm_i2c_get_slave_addr()
1137 if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add) in npcm_i2c_remove_slave_addr()
1138 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_remove_slave_addr()
1152 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_write_fifo_slave()
1153 while (max_bytes-- && bus->data->fifo_size != npcm_i2c_fifo_usage(bus)) { in npcm_i2c_write_fifo_slave()
1154 if (bus->slv_wr_size <= 0) in npcm_i2c_write_fifo_slave()
1156 bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1); in npcm_i2c_write_fifo_slave()
1157 npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]); in npcm_i2c_write_fifo_slave()
1158 bus->slv_wr_ind++; in npcm_i2c_write_fifo_slave()
1159 bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1); in npcm_i2c_write_fifo_slave()
1160 bus->slv_wr_size--; in npcm_i2c_write_fifo_slave()
1168 if (!bus->slave) in npcm_i2c_read_fifo_slave()
1171 while (bytes_in_fifo--) { in npcm_i2c_read_fifo_slave()
1174 bus->slv_rd_ind = bus->slv_rd_ind & (bus->data->fifo_size - 1); in npcm_i2c_read_fifo_slave()
1175 bus->slv_rd_buf[bus->slv_rd_ind] = data; in npcm_i2c_read_fifo_slave()
1176 bus->slv_rd_ind++; in npcm_i2c_read_fifo_slave()
1179 if (bus->slv_rd_ind == 1 && bus->read_block_use) in npcm_i2c_read_fifo_slave()
1180 bus->slv_rd_size = data + bus->PEC_use + 1; in npcm_i2c_read_fifo_slave()
1189 int ret = bus->slv_wr_ind; in npcm_i2c_slave_get_wr_buf()
1192 for (i = 0; i < bus->data->fifo_size; i++) { in npcm_i2c_slave_get_wr_buf()
1193 if (bus->slv_wr_size >= bus->data->fifo_size) in npcm_i2c_slave_get_wr_buf()
1195 if (bus->state == I2C_SLAVE_MATCH) { in npcm_i2c_slave_get_wr_buf()
1196 i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value); in npcm_i2c_slave_get_wr_buf()
1197 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_get_wr_buf()
1199 i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value); in npcm_i2c_slave_get_wr_buf()
1201 ind = (bus->slv_wr_ind + bus->slv_wr_size) & (bus->data->fifo_size - 1); in npcm_i2c_slave_get_wr_buf()
1202 bus->slv_wr_buf[ind] = value; in npcm_i2c_slave_get_wr_buf()
1203 bus->slv_wr_size++; in npcm_i2c_slave_get_wr_buf()
1205 return bus->data->fifo_size - ret; in npcm_i2c_slave_get_wr_buf()
1212 for (i = 0; i < bus->slv_rd_ind; i++) in npcm_i2c_slave_send_rd_buf()
1213 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED, in npcm_i2c_slave_send_rd_buf()
1214 &bus->slv_rd_buf[i]); in npcm_i2c_slave_send_rd_buf()
1219 if (bus->slv_rd_ind) { in npcm_i2c_slave_send_rd_buf()
1220 bus->slv_wr_size = 0; in npcm_i2c_slave_send_rd_buf()
1221 bus->slv_wr_ind = 0; in npcm_i2c_slave_send_rd_buf()
1224 bus->slv_rd_ind = 0; in npcm_i2c_slave_send_rd_buf()
1225 bus->slv_rd_size = bus->adap.quirks->max_read_len; in npcm_i2c_slave_send_rd_buf()
1234 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_receive()
1235 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_receive()
1236 bus->slv_rd_size = nread; in npcm_i2c_slave_receive()
1237 bus->slv_rd_ind = 0; in npcm_i2c_slave_receive()
1239 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_slave_receive()
1240 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_slave_receive()
1251 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_xmit()
1264 * NACK on read will be once reached to bus->adap->quirks->max_read_len.
1273 left_in_fifo = bus->data->txf_sts_tx_bytes & in npcm_i2c_slave_wr_buf_sync()
1274 ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_slave_wr_buf_sync()
1277 if (left_in_fifo >= bus->data->fifo_size || in npcm_i2c_slave_wr_buf_sync()
1278 bus->slv_wr_size >= bus->data->fifo_size) in npcm_i2c_slave_wr_buf_sync()
1282 bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1283 bus->slv_wr_size = bus->slv_wr_size + left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1285 if (bus->slv_wr_ind < 0) in npcm_i2c_slave_wr_buf_sync()
1286 bus->slv_wr_ind += bus->data->fifo_size; in npcm_i2c_slave_wr_buf_sync()
1291 if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) { in npcm_i2c_slave_rd_wr()
1296 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_rd_wr()
1297 npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len, in npcm_i2c_slave_rd_wr()
1298 bus->slv_wr_buf); in npcm_i2c_slave_rd_wr()
1306 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_rd_wr()
1308 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_slave_rd_wr()
1310 npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len, in npcm_i2c_slave_rd_wr()
1311 bus->slv_rd_buf); in npcm_i2c_slave_rd_wr()
1319 u8 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1323 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_int_slave_handler()
1325 if (bus->fifo_use) in npcm_i2c_int_slave_handler()
1328 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1331 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1332 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1333 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1339 iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1350 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_int_slave_handler()
1353 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1355 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1361 if (completion_done(&bus->cmd_complete) == false) { in npcm_i2c_int_slave_handler()
1362 bus->cmd_err = -EIO; in npcm_i2c_int_slave_handler()
1363 complete(&bus->cmd_complete); in npcm_i2c_int_slave_handler()
1365 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1366 iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1374 bus->stop_ind = I2C_SLAVE_DONE_IND; in npcm_i2c_int_slave_handler()
1376 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1383 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1390 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1391 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1392 i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0); in npcm_i2c_int_slave_handler()
1393 iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1394 if (bus->fifo_use) { in npcm_i2c_int_slave_handler()
1400 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1402 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1406 /* restart condition occurred and Rx-FIFO was not empty */ in npcm_i2c_int_slave_handler()
1407 if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR, in npcm_i2c_int_slave_handler()
1408 ioread8(bus->reg + NPCM_I2CFIF_CTS))) { in npcm_i2c_int_slave_handler()
1409 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1410 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1411 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1413 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1414 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1417 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1427 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1431 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_int_slave_handler()
1432 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1434 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1436 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED, in npcm_i2c_int_slave_handler()
1438 bus->operation = I2C_READ_OPER; in npcm_i2c_int_slave_handler()
1440 if (bus->own_slave_addr == 0xFF) { in npcm_i2c_int_slave_handler()
1442 val = ioread8(bus->reg + NPCM_I2CCST); in npcm_i2c_int_slave_handler()
1449 i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_int_slave_handler()
1450 i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2); in npcm_i2c_int_slave_handler()
1453 * the i2c module can response to 10 own SA. in npcm_i2c_int_slave_handler()
1463 bus->own_slave_addr = addr; in npcm_i2c_int_slave_handler()
1464 if (bus->PEC_mask & BIT(info)) in npcm_i2c_int_slave_handler()
1465 bus->PEC_use = true; in npcm_i2c_int_slave_handler()
1467 bus->PEC_use = false; in npcm_i2c_int_slave_handler()
1470 bus->own_slave_addr = 0; in npcm_i2c_int_slave_handler()
1472 bus->own_slave_addr = 0x61; in npcm_i2c_int_slave_handler()
1481 * (regular write-read mode) in npcm_i2c_int_slave_handler()
1483 if ((bus->state == I2C_OPER_STARTED && in npcm_i2c_int_slave_handler()
1484 bus->operation == I2C_READ_OPER && in npcm_i2c_int_slave_handler()
1485 bus->stop_ind == I2C_SLAVE_XMIT_IND) || in npcm_i2c_int_slave_handler()
1486 bus->stop_ind == I2C_SLAVE_RCV_IND) { in npcm_i2c_int_slave_handler()
1488 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1493 bus->stop_ind = I2C_SLAVE_XMIT_IND; in npcm_i2c_int_slave_handler()
1495 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_int_slave_handler()
1496 bus->state = I2C_SLAVE_MATCH; in npcm_i2c_int_slave_handler()
1498 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1502 /* Slave SDA status is set - tx or rx */ in npcm_i2c_int_slave_handler()
1504 (bus->fifo_use && in npcm_i2c_int_slave_handler()
1507 iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1526 struct npcm_i2c *bus = i2c_get_adapdata(client->adapter); in npcm_i2c_reg_slave()
1528 bus->slave = client; in npcm_i2c_reg_slave()
1530 if (client->flags & I2C_CLIENT_TEN) in npcm_i2c_reg_slave()
1531 return -EAFNOSUPPORT; in npcm_i2c_reg_slave()
1533 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1536 bus->slv_rd_size = 0; in npcm_i2c_reg_slave()
1537 bus->slv_wr_size = 0; in npcm_i2c_reg_slave()
1538 bus->slv_rd_ind = 0; in npcm_i2c_reg_slave()
1539 bus->slv_wr_ind = 0; in npcm_i2c_reg_slave()
1540 if (client->flags & I2C_CLIENT_PEC) in npcm_i2c_reg_slave()
1541 bus->PEC_use = true; in npcm_i2c_reg_slave()
1543 dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num, in npcm_i2c_reg_slave()
1544 client->addr, bus->PEC_use); in npcm_i2c_reg_slave()
1546 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true); in npcm_i2c_reg_slave()
1552 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1558 struct npcm_i2c *bus = client->adapter->algo_data; in npcm_i2c_unreg_slave()
1561 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1562 if (!bus->slave) { in npcm_i2c_unreg_slave()
1563 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1564 return -EINVAL; in npcm_i2c_unreg_slave()
1567 npcm_i2c_remove_slave_addr(bus, client->addr); in npcm_i2c_unreg_slave()
1568 bus->slave = NULL; in npcm_i2c_unreg_slave()
1569 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1581 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1590 if (rcount < (2 * bus->data->fifo_size) && rcount > bus->data->fifo_size) in npcm_i2c_master_fifo_read()
1591 fifo_bytes = rcount - bus->data->fifo_size; in npcm_i2c_master_fifo_read()
1594 /* last bytes are about to be read - end of tx */ in npcm_i2c_master_fifo_read()
1595 bus->state = I2C_STOP_PENDING; in npcm_i2c_master_fifo_read()
1596 bus->stop_ind = ind; in npcm_i2c_master_fifo_read()
1603 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1604 npcm_i2c_set_fifo(bus, rcount, -1); in npcm_i2c_master_fifo_read()
1612 if (bus->fifo_use) in npcm_i2c_irq_master_handler_write()
1615 /* Master write operation - last byte handling */ in npcm_i2c_irq_master_handler_write()
1616 if (bus->wr_ind == bus->wr_size) { in npcm_i2c_irq_master_handler_write()
1617 if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0) in npcm_i2c_irq_master_handler_write()
1627 if (bus->rd_size == 0) { in npcm_i2c_irq_master_handler_write()
1630 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_write()
1631 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_master_handler_write()
1637 /* last write-byte written on previous int - restart */ in npcm_i2c_irq_master_handler_write()
1638 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_master_handler_write()
1643 * Receiving one byte only - stall after successful in npcm_i2c_irq_master_handler_write()
1646 * unintentionally NACK the next multi-byte read. in npcm_i2c_irq_master_handler_write()
1648 if (bus->rd_size == 1) in npcm_i2c_irq_master_handler_write()
1652 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_master_handler_write()
1654 npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1); in npcm_i2c_irq_master_handler_write()
1658 if (!bus->fifo_use || bus->wr_size == 1) { in npcm_i2c_irq_master_handler_write()
1659 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_irq_master_handler_write()
1661 wcount = bus->wr_size - bus->wr_ind; in npcm_i2c_irq_master_handler_write()
1662 npcm_i2c_set_fifo(bus, -1, wcount); in npcm_i2c_irq_master_handler_write()
1675 block_extra_bytes_size = bus->read_block_use + bus->PEC_use; in npcm_i2c_irq_master_handler_read()
1681 if (bus->rd_ind == 0) { /* first byte handling: */ in npcm_i2c_irq_master_handler_read()
1682 if (bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1686 bus->rd_size = data + block_extra_bytes_size; in npcm_i2c_irq_master_handler_read()
1687 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_irq_master_handler_read()
1690 if (bus->fifo_use) { in npcm_i2c_irq_master_handler_read()
1691 data = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1693 iowrite8(data, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1696 npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1); in npcm_i2c_irq_master_handler_read()
1703 if (bus->rd_size == block_extra_bytes_size && in npcm_i2c_irq_master_handler_read()
1704 bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1705 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_read()
1706 bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND; in npcm_i2c_irq_master_handler_read()
1707 bus->cmd_err = -EIO; in npcm_i2c_irq_master_handler_read()
1719 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_nmatch()
1721 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_nmatch()
1722 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_nmatch()
1730 if (bus->nack_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_nack()
1731 bus->nack_cnt++; in npcm_i2c_irq_handle_nack()
1733 if (bus->fifo_use) { in npcm_i2c_irq_handle_nack()
1738 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_irq_handle_nack()
1739 bus->wr_ind -= npcm_i2c_fifo_usage(bus); in npcm_i2c_irq_handle_nack()
1742 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_nack()
1746 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_irq_handle_nack()
1761 readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val, in npcm_i2c_irq_handle_nack()
1766 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_nack()
1773 npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind); in npcm_i2c_irq_handle_nack()
1779 if (bus->ber_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_ber()
1780 bus->ber_cnt++; in npcm_i2c_irq_handle_ber()
1781 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_ber()
1785 bus->ber_state = true; in npcm_i2c_irq_handle_ber()
1789 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_irq_handle_ber()
1791 bus->cmd_err = -EAGAIN; in npcm_i2c_irq_handle_ber()
1792 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_ber()
1794 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_ber()
1801 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_eob()
1802 npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind); in npcm_i2c_irq_handle_eob()
1809 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_handle_stall_after_start()
1810 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_handle_stall_after_start()
1813 } else if ((bus->rd_size == 1) && !bus->read_block_use) { in npcm_i2c_irq_handle_stall_after_start()
1815 * Receiving one byte only - set NACK after ensuring in npcm_i2c_irq_handle_stall_after_start()
1821 /* Reset stall-after-address-byte */ in npcm_i2c_irq_handle_stall_after_start()
1825 iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_stall_after_start()
1828 /* SDA status is set - TX or RX, master */
1836 if (bus->state == I2C_IDLE) { in npcm_i2c_irq_handle_sda()
1837 bus->stop_ind = I2C_WAKE_UP_IND; in npcm_i2c_irq_handle_sda()
1839 if (npcm_i2c_is_quick(bus) || bus->read_block_use) in npcm_i2c_irq_handle_sda()
1849 * Receiving one byte only - stall after successful completion in npcm_i2c_irq_handle_sda()
1852 * multi-byte read in npcm_i2c_irq_handle_sda()
1854 if (bus->wr_size == 0 && bus->rd_size == 1) in npcm_i2c_irq_handle_sda()
1857 /* Initiate I2C master tx */ in npcm_i2c_irq_handle_sda()
1862 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1867 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1869 /* re-enable */ in npcm_i2c_irq_handle_sda()
1871 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1880 if (bus->wr_size) in npcm_i2c_irq_handle_sda()
1881 npcm_i2c_set_fifo(bus, -1, bus->wr_size); in npcm_i2c_irq_handle_sda()
1883 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_handle_sda()
1885 bus->state = I2C_OPER_STARTED; in npcm_i2c_irq_handle_sda()
1887 if (npcm_i2c_is_quick(bus) || bus->wr_size) in npcm_i2c_irq_handle_sda()
1888 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_irq_handle_sda()
1890 npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0)); in npcm_i2c_irq_handle_sda()
1893 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_irq_handle_sda()
1895 else if (bus->operation == I2C_READ_OPER) in npcm_i2c_irq_handle_sda()
1903 int ret = -EIO; in npcm_i2c_int_master_handler()
1905 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_master_handler()
1925 ioread8(bus->reg + NPCM_I2CCTL1)) == 1) && in npcm_i2c_int_master_handler()
1927 ioread8(bus->reg + NPCM_I2CCST3)))) { in npcm_i2c_int_master_handler()
1931 if (bus->slave) in npcm_i2c_int_master_handler()
1932 iowrite8(bus->slave->addr | NPCM_I2CADDR_SAEN, in npcm_i2c_int_master_handler()
1933 bus->reg + NPCM_I2CADDR1); in npcm_i2c_int_master_handler()
1944 /* SDA status is set - TX or RX, master */ in npcm_i2c_int_master_handler()
1946 (bus->fifo_use && in npcm_i2c_int_master_handler()
1961 int status = -ENOTRECOVERABLE; in npcm_i2c_recovery_tgclk()
1967 dev_dbg(bus->dev, "bus%d-0x%x recovery skipped, bus not stuck", in npcm_i2c_recovery_tgclk()
1968 bus->num, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1970 bus->ber_state = false; in npcm_i2c_recovery_tgclk()
1977 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1980 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_recovery_tgclk()
1981 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_recovery_tgclk()
1988 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1991 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1992 npcm_i2c_set_fifo(bus, -1, 0); in npcm_i2c_recovery_tgclk()
1997 iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
2004 } while (!done && iter--); in npcm_i2c_recovery_tgclk()
2006 /* If SDA line is released: send start-addr-stop, to re-sync. */ in npcm_i2c_recovery_tgclk()
2009 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_recovery_tgclk()
2027 status = -ENOTRECOVERABLE; in npcm_i2c_recovery_tgclk()
2029 if (bus->rec_fail_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
2030 bus->rec_fail_cnt++; in npcm_i2c_recovery_tgclk()
2032 if (bus->rec_succ_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
2033 bus->rec_succ_cnt++; in npcm_i2c_recovery_tgclk()
2035 bus->ber_state = false; in npcm_i2c_recovery_tgclk()
2043 struct i2c_bus_recovery_info *rinfo = &bus->rinfo; in npcm_i2c_recovery_init()
2045 rinfo->recover_bus = npcm_i2c_recovery_tgclk; in npcm_i2c_recovery_init()
2048 * npcm i2c HW allows direct reading of SCL and SDA. in npcm_i2c_recovery_init()
2053 rinfo->get_scl = npcm_i2c_get_SCL; in npcm_i2c_recovery_init()
2054 rinfo->get_sda = npcm_i2c_get_SDA; in npcm_i2c_recovery_init()
2055 _adap->bus_recovery_info = rinfo; in npcm_i2c_recovery_init()
2065 * NPCM7XX i2c module timing parameters are dependent on module core clk (APB)
2078 bus->bus_freq = bus_freq_hz; in npcm_i2c_init_clk()
2096 return -EINVAL; in npcm_i2c_init_clk()
2100 if (bus->apb_clk >= smb_timing[scl_table_cnt].core_clk) in npcm_i2c_init_clk()
2104 return -EINVAL; in npcm_i2c_init_clk()
2108 bus->reg + NPCM_I2CCTL2); in npcm_i2c_init_clk()
2113 bus->reg + NPCM_I2CCTL3); in npcm_i2c_init_clk()
2121 * k1 = 2 * SCLLT7-0 -> Low Time = k1 / 2 in npcm_i2c_init_clk()
2122 * k2 = 2 * SCLLT7-0 -> High Time = k2 / 2 in npcm_i2c_init_clk()
2124 iowrite8(smb_timing[scl_table_cnt].scllt, bus->reg + NPCM_I2CSCLLT); in npcm_i2c_init_clk()
2125 iowrite8(smb_timing[scl_table_cnt].sclht, bus->reg + NPCM_I2CSCLHT); in npcm_i2c_init_clk()
2127 iowrite8(smb_timing[scl_table_cnt].dbcnt, bus->reg + NPCM_I2CCTL5); in npcm_i2c_init_clk()
2130 iowrite8(smb_timing[scl_table_cnt].hldt, bus->reg + NPCM_I2CCTL4); in npcm_i2c_init_clk()
2145 if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) || in npcm_i2c_init_module()
2147 return -EINVAL; in npcm_i2c_init_module()
2153 if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) { in npcm_i2c_init_module()
2154 bus->fifo_use = true; in npcm_i2c_init_module()
2156 val = ioread8(bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
2158 iowrite8(val, bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
2161 bus->fifo_use = false; in npcm_i2c_init_module()
2164 /* Configure I2C module clock frequency */ in npcm_i2c_init_module()
2167 dev_err(bus->dev, "npcm_i2c_init_clk failed\n"); in npcm_i2c_init_module()
2173 bus->state = I2C_IDLE; in npcm_i2c_init_module()
2174 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
2176 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
2181 if ((npcm_i2c_get_SDA(&bus->adap) == 0) || (npcm_i2c_get_SCL(&bus->adap) == 0)) { in npcm_i2c_init_module()
2182 dev_err(bus->dev, "I2C%d init fail: lines are low\n", bus->num); in npcm_i2c_init_module()
2183 dev_err(bus->dev, "SDA=%d SCL=%d\n", npcm_i2c_get_SDA(&bus->adap), in npcm_i2c_init_module()
2184 npcm_i2c_get_SCL(&bus->adap)); in npcm_i2c_init_module()
2185 return -ENXIO; in npcm_i2c_init_module()
2198 bus->state = I2C_DISABLE; in __npcm_i2c_init()
2199 bus->master_or_slave = I2C_SLAVE; in __npcm_i2c_init()
2200 bus->int_time_stamp = 0; in __npcm_i2c_init()
2202 bus->slave = NULL; in __npcm_i2c_init()
2205 ret = device_property_read_u32(&pdev->dev, "clock-frequency", in __npcm_i2c_init()
2208 dev_info(&pdev->dev, "Could not read clock-frequency property"); in __npcm_i2c_init()
2214 dev_err(&pdev->dev, "npcm_i2c_init_module failed\n"); in __npcm_i2c_init()
2226 bus->master_or_slave = I2C_MASTER; in npcm_i2c_bus_irq()
2228 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_bus_irq()
2229 bus->int_time_stamp = jiffies; in npcm_i2c_bus_irq()
2234 if (bus->slave) { in npcm_i2c_bus_irq()
2235 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_bus_irq()
2251 if (bus->state != I2C_IDLE) { in npcm_i2c_master_start_xmit()
2252 bus->cmd_err = -EBUSY; in npcm_i2c_master_start_xmit()
2255 bus->wr_buf = write_data; in npcm_i2c_master_start_xmit()
2256 bus->wr_size = nwrite; in npcm_i2c_master_start_xmit()
2257 bus->wr_ind = 0; in npcm_i2c_master_start_xmit()
2258 bus->rd_buf = read_data; in npcm_i2c_master_start_xmit()
2259 bus->rd_size = nread; in npcm_i2c_master_start_xmit()
2260 bus->rd_ind = 0; in npcm_i2c_master_start_xmit()
2261 bus->PEC_use = 0; in npcm_i2c_master_start_xmit()
2263 /* for tx PEC is appended to buffer from i2c IF. PEC flag is ignored */ in npcm_i2c_master_start_xmit()
2265 bus->PEC_use = use_PEC; in npcm_i2c_master_start_xmit()
2267 bus->read_block_use = use_read_block; in npcm_i2c_master_start_xmit()
2269 bus->operation = I2C_READ_OPER; in npcm_i2c_master_start_xmit()
2271 bus->operation = I2C_WRITE_OPER; in npcm_i2c_master_start_xmit()
2272 if (bus->fifo_use) { in npcm_i2c_master_start_xmit()
2277 i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2280 iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2283 bus->state = I2C_IDLE; in npcm_i2c_master_start_xmit()
2303 if (bus->state == I2C_DISABLE) { in npcm_i2c_master_xfer()
2304 dev_err(bus->dev, "I2C%d module is disabled", bus->num); in npcm_i2c_master_xfer()
2305 return -EINVAL; in npcm_i2c_master_xfer()
2309 if (msg0->flags & I2C_M_RD) { /* read */ in npcm_i2c_master_xfer()
2312 read_data = msg0->buf; in npcm_i2c_master_xfer()
2313 if (msg0->flags & I2C_M_RECV_LEN) { in npcm_i2c_master_xfer()
2316 if (msg0->flags & I2C_CLIENT_PEC) in npcm_i2c_master_xfer()
2319 nread = msg0->len; in npcm_i2c_master_xfer()
2322 nwrite = msg0->len; in npcm_i2c_master_xfer()
2323 write_data = msg0->buf; in npcm_i2c_master_xfer()
2328 read_data = msg1->buf; in npcm_i2c_master_xfer()
2329 if (msg1->flags & I2C_M_RECV_LEN) { in npcm_i2c_master_xfer()
2332 if (msg1->flags & I2C_CLIENT_PEC) in npcm_i2c_master_xfer()
2335 nread = msg1->len; in npcm_i2c_master_xfer()
2342 dev_err(bus->dev, "i2c%d buffer too big\n", bus->num); in npcm_i2c_master_xfer()
2343 return -EINVAL; in npcm_i2c_master_xfer()
2346 time_left = jiffies + bus->adap.timeout / bus->adap.retries + 1; in npcm_i2c_master_xfer()
2353 spin_lock_irqsave(&bus->lock, flags); in npcm_i2c_master_xfer()
2354 bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB; in npcm_i2c_master_xfer()
2356 if (!bus_busy && bus->slave) in npcm_i2c_master_xfer()
2357 iowrite8((bus->slave->addr & 0x7F), in npcm_i2c_master_xfer()
2358 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2360 spin_unlock_irqrestore(&bus->lock, flags); in npcm_i2c_master_xfer()
2369 * from the bus->dest_addr for the i2c_recover_bus() call later. in npcm_i2c_master_xfer()
2372 * the i2c bus if some error condition occurs. in npcm_i2c_master_xfer()
2377 bus->dest_addr = i2c_8bit_addr_from_msg(msg0) & ~I2C_M_RD; in npcm_i2c_master_xfer()
2388 if (bus_busy || bus->ber_state) { in npcm_i2c_master_xfer()
2389 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_master_xfer()
2392 return -EAGAIN; in npcm_i2c_master_xfer()
2396 bus->msgs = msgs; in npcm_i2c_master_xfer()
2397 bus->msgs_num = num; in npcm_i2c_master_xfer()
2398 bus->cmd_err = 0; in npcm_i2c_master_xfer()
2399 bus->read_block_use = read_block; in npcm_i2c_master_xfer()
2401 reinit_completion(&bus->cmd_complete); in npcm_i2c_master_xfer()
2413 timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); in npcm_i2c_master_xfer()
2414 timeout = max_t(unsigned long, bus->adap.timeout / bus->adap.retries, in npcm_i2c_master_xfer()
2416 time_left = wait_for_completion_timeout(&bus->cmd_complete, in npcm_i2c_master_xfer()
2420 if (bus->timeout_cnt < ULLONG_MAX) in npcm_i2c_master_xfer()
2421 bus->timeout_cnt++; in npcm_i2c_master_xfer()
2422 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_master_xfer()
2424 bus->cmd_err = -EIO; in npcm_i2c_master_xfer()
2425 bus->state = I2C_IDLE; in npcm_i2c_master_xfer()
2431 if (bus->cmd_err == -EAGAIN) in npcm_i2c_master_xfer()
2432 bus->cmd_err = i2c_recover_bus(adap); in npcm_i2c_master_xfer()
2439 else if (bus->cmd_err && in npcm_i2c_master_xfer()
2440 (bus->data->rxf_ctl_last_pec & ioread8(bus->reg + NPCM_I2CRXF_CTL))) in npcm_i2c_master_xfer()
2449 if (bus->slave) in npcm_i2c_master_xfer()
2450 iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN, in npcm_i2c_master_xfer()
2451 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2455 return bus->cmd_err; in npcm_i2c_master_xfer()
2482 /* i2c debugfs directory: used to keep health monitor of i2c devices */
2492 d = debugfs_create_dir(dev_name(&pdev->dev), npcm_i2c_debugfs_dir); in npcm_i2c_init_debugfs()
2495 debugfs_create_u64("ber_cnt", 0444, d, &bus->ber_cnt); in npcm_i2c_init_debugfs()
2496 debugfs_create_u64("nack_cnt", 0444, d, &bus->nack_cnt); in npcm_i2c_init_debugfs()
2497 debugfs_create_u64("rec_succ_cnt", 0444, d, &bus->rec_succ_cnt); in npcm_i2c_init_debugfs()
2498 debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt); in npcm_i2c_init_debugfs()
2499 debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt); in npcm_i2c_init_debugfs()
2500 debugfs_create_u64("tx_complete_cnt", 0444, d, &bus->tx_complete_cnt); in npcm_i2c_init_debugfs()
2502 bus->debugfs = d; in npcm_i2c_init_debugfs()
2507 struct device_node *np = pdev->dev.of_node; in npcm_i2c_probe_bus()
2509 struct device *dev = &pdev->dev; in npcm_i2c_probe_bus()
2516 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); in npcm_i2c_probe_bus()
2518 return -ENOMEM; in npcm_i2c_probe_bus()
2520 bus->dev = &pdev->dev; in npcm_i2c_probe_bus()
2522 bus->data = of_device_get_match_data(dev); in npcm_i2c_probe_bus()
2523 if (!bus->data) { in npcm_i2c_probe_bus()
2525 return -EINVAL; in npcm_i2c_probe_bus()
2528 bus->num = of_alias_get_id(pdev->dev.of_node, "i2c"); in npcm_i2c_probe_bus()
2530 i2c_clk = devm_clk_get(&pdev->dev, NULL); in npcm_i2c_probe_bus()
2533 bus->apb_clk = clk_get_rate(i2c_clk); in npcm_i2c_probe_bus()
2535 gcr_regmap = syscon_regmap_lookup_by_phandle(np, "nuvoton,sys-mgr"); in npcm_i2c_probe_bus()
2537 gcr_regmap = syscon_regmap_lookup_by_compatible("nuvoton,npcm750-gcr"); in npcm_i2c_probe_bus()
2541 regmap_write(gcr_regmap, NPCM_I2CSEGCTL, bus->data->segctl_init_val); in npcm_i2c_probe_bus()
2543 bus->reg = devm_platform_ioremap_resource(pdev, 0); in npcm_i2c_probe_bus()
2544 if (IS_ERR(bus->reg)) in npcm_i2c_probe_bus()
2545 return PTR_ERR(bus->reg); in npcm_i2c_probe_bus()
2547 spin_lock_init(&bus->lock); in npcm_i2c_probe_bus()
2548 init_completion(&bus->cmd_complete); in npcm_i2c_probe_bus()
2550 adap = &bus->adap; in npcm_i2c_probe_bus()
2551 adap->owner = THIS_MODULE; in npcm_i2c_probe_bus()
2552 adap->retries = 3; in npcm_i2c_probe_bus()
2558 adap->timeout = 2 * HZ; in npcm_i2c_probe_bus()
2559 adap->algo = &npcm_i2c_algo; in npcm_i2c_probe_bus()
2560 adap->quirks = &npcm_i2c_quirks; in npcm_i2c_probe_bus()
2561 adap->algo_data = bus; in npcm_i2c_probe_bus()
2562 adap->dev.parent = &pdev->dev; in npcm_i2c_probe_bus()
2563 adap->dev.of_node = pdev->dev.of_node; in npcm_i2c_probe_bus()
2564 adap->nr = pdev->id; in npcm_i2c_probe_bus()
2573 * might do a warm reset during the last smbus/i2c transfer session. in npcm_i2c_probe_bus()
2577 ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0, in npcm_i2c_probe_bus()
2578 dev_name(bus->dev), bus); in npcm_i2c_probe_bus()
2590 snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d", in npcm_i2c_probe_bus()
2591 bus->num); in npcm_i2c_probe_bus()
2592 ret = i2c_add_numbered_adapter(&bus->adap); in npcm_i2c_probe_bus()
2606 debugfs_remove_recursive(bus->debugfs); in npcm_i2c_remove_bus()
2607 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2609 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2610 i2c_del_adapter(&bus->adap); in npcm_i2c_remove_bus()
2614 { .compatible = "nuvoton,npcm750-i2c", .data = &npxm7xx_i2c_data },
2615 { .compatible = "nuvoton,npcm845-i2c", .data = &npxm8xx_i2c_data },
2624 .name = "nuvoton-i2c",
2655 MODULE_DESCRIPTION("Nuvoton I2C Bus Driver");