Lines Matching full:bus

30  * of the bus.
126 #define NPCM_I2CT_OUT 0x14 /* Bus T.O. */
155 #define NPCM_I2CST_BER BIT(5) /* Bus error */
161 #define NPCM_I2CCST_BB BIT(1) /* Bus busy */
339 static inline void npcm_i2c_select_bank(struct npcm_i2c *bus, in npcm_i2c_select_bank() argument
342 u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
348 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
351 static void npcm_i2c_init_params(struct npcm_i2c *bus) in npcm_i2c_init_params() argument
353 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_init_params()
354 bus->rd_size = 0; in npcm_i2c_init_params()
355 bus->wr_size = 0; in npcm_i2c_init_params()
356 bus->rd_ind = 0; in npcm_i2c_init_params()
357 bus->wr_ind = 0; in npcm_i2c_init_params()
358 bus->read_block_use = false; in npcm_i2c_init_params()
359 bus->int_time_stamp = 0; in npcm_i2c_init_params()
360 bus->PEC_use = false; in npcm_i2c_init_params()
361 bus->PEC_mask = 0; in npcm_i2c_init_params()
363 if (bus->slave) in npcm_i2c_init_params()
364 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_init_params()
368 static inline void npcm_i2c_wr_byte(struct npcm_i2c *bus, u8 data) in npcm_i2c_wr_byte() argument
370 iowrite8(data, bus->reg + NPCM_I2CSDA); in npcm_i2c_wr_byte()
373 static inline u8 npcm_i2c_rd_byte(struct npcm_i2c *bus) in npcm_i2c_rd_byte() argument
375 return ioread8(bus->reg + NPCM_I2CSDA); in npcm_i2c_rd_byte()
380 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_get_SCL() local
382 return !!(I2CCTL3_SCL_LVL & ioread8(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SCL()
387 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_get_SDA() local
389 return !!(I2CCTL3_SDA_LVL & ioread8(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SDA()
392 static inline u16 npcm_i2c_get_index(struct npcm_i2c *bus) in npcm_i2c_get_index() argument
394 if (bus->operation == I2C_READ_OPER) in npcm_i2c_get_index()
395 return bus->rd_ind; in npcm_i2c_get_index()
396 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_get_index()
397 return bus->wr_ind; in npcm_i2c_get_index()
402 static inline bool npcm_i2c_is_quick(struct npcm_i2c *bus) in npcm_i2c_is_quick() argument
404 return bus->wr_size == 0 && bus->rd_size == 0; in npcm_i2c_is_quick()
407 static void npcm_i2c_disable(struct npcm_i2c *bus) in npcm_i2c_disable() argument
416 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_disable()
420 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
422 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
424 bus->state = I2C_DISABLE; in npcm_i2c_disable()
427 static void npcm_i2c_enable(struct npcm_i2c *bus) in npcm_i2c_enable() argument
429 u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
432 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
433 bus->state = I2C_IDLE; in npcm_i2c_enable()
437 static inline void npcm_i2c_eob_int(struct npcm_i2c *bus, bool enable) in npcm_i2c_eob_int() argument
442 val = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
444 iowrite8(val, bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
446 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
452 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
455 static inline bool npcm_i2c_tx_fifo_empty(struct npcm_i2c *bus) in npcm_i2c_tx_fifo_empty() argument
459 tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_tx_fifo_empty()
461 if ((tx_fifo_sts & bus->data->txf_sts_tx_bytes) == 0) in npcm_i2c_tx_fifo_empty()
468 static inline bool npcm_i2c_rx_fifo_full(struct npcm_i2c *bus) in npcm_i2c_rx_fifo_full() argument
472 rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_rx_fifo_full()
474 if ((rx_fifo_sts & bus->data->rxf_sts_rx_bytes) == 0) in npcm_i2c_rx_fifo_full()
481 static inline void npcm_i2c_clear_fifo_int(struct npcm_i2c *bus) in npcm_i2c_clear_fifo_int() argument
485 val = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
487 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
490 static inline void npcm_i2c_clear_tx_fifo(struct npcm_i2c *bus) in npcm_i2c_clear_tx_fifo() argument
494 val = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
496 iowrite8(val, bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
499 static inline void npcm_i2c_clear_rx_fifo(struct npcm_i2c *bus) in npcm_i2c_clear_rx_fifo() argument
503 val = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
505 iowrite8(val, bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
508 static void npcm_i2c_int_enable(struct npcm_i2c *bus, bool enable) in npcm_i2c_int_enable() argument
512 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
518 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
521 static inline void npcm_i2c_master_start(struct npcm_i2c *bus) in npcm_i2c_master_start() argument
525 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
528 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
531 static inline void npcm_i2c_master_stop(struct npcm_i2c *bus) in npcm_i2c_master_stop() argument
541 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
544 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
546 if (!bus->fifo_use) in npcm_i2c_master_stop()
549 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_master_stop()
551 if (bus->operation == I2C_READ_OPER) in npcm_i2c_master_stop()
552 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_master_stop()
554 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_master_stop()
555 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_master_stop()
556 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_master_stop()
559 static inline void npcm_i2c_stall_after_start(struct npcm_i2c *bus, bool stall) in npcm_i2c_stall_after_start() argument
563 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
569 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
572 static inline void npcm_i2c_nack(struct npcm_i2c *bus) in npcm_i2c_nack() argument
576 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
579 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
582 static inline void npcm_i2c_clear_master_status(struct npcm_i2c *bus) in npcm_i2c_clear_master_status() argument
588 iowrite8(val, bus->reg + NPCM_I2CST); in npcm_i2c_clear_master_status()
592 static void npcm_i2c_slave_int_enable(struct npcm_i2c *bus, bool enable) in npcm_i2c_slave_int_enable() argument
597 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
603 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
606 static int npcm_i2c_slave_enable(struct npcm_i2c *bus, enum i2c_addr addr_type, in npcm_i2c_slave_enable() argument
615 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
620 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
623 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
628 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
632 dev_err(bus->dev, "try to enable more than 2 SA not supported\n"); in npcm_i2c_slave_enable()
638 iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]); in npcm_i2c_slave_enable()
639 npcm_i2c_slave_int_enable(bus, enable); in npcm_i2c_slave_enable()
645 static void npcm_i2c_reset(struct npcm_i2c *bus) in npcm_i2c_reset() argument
656 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
658 npcm_i2c_disable(bus); in npcm_i2c_reset()
659 npcm_i2c_enable(bus); in npcm_i2c_reset()
663 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
665 /* Clear BB (BUS BUSY) bit */ in npcm_i2c_reset()
666 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_reset()
667 iowrite8(0xFF, bus->reg + NPCM_I2CST); in npcm_i2c_reset()
670 npcm_i2c_eob_int(bus, false); in npcm_i2c_reset()
673 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_reset()
676 if (bus->slave) { in npcm_i2c_reset()
677 addr = bus->slave->addr; in npcm_i2c_reset()
678 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, addr, true); in npcm_i2c_reset()
683 npcm_i2c_clear_master_status(bus); in npcm_i2c_reset()
685 bus->state = I2C_IDLE; in npcm_i2c_reset()
688 static inline bool npcm_i2c_is_master(struct npcm_i2c *bus) in npcm_i2c_is_master() argument
690 return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST)); in npcm_i2c_is_master()
693 static void npcm_i2c_callback(struct npcm_i2c *bus, in npcm_i2c_callback() argument
700 msgs = bus->msgs; in npcm_i2c_callback()
701 msgs_num = bus->msgs_num; in npcm_i2c_callback()
709 if (completion_done(&bus->cmd_complete)) in npcm_i2c_callback()
714 bus->cmd_err = bus->msgs_num; in npcm_i2c_callback()
715 if (bus->tx_complete_cnt < ULLONG_MAX) in npcm_i2c_callback()
716 bus->tx_complete_cnt++; in npcm_i2c_callback()
720 if (bus->msgs) { in npcm_i2c_callback()
731 bus->cmd_err = -ENXIO; in npcm_i2c_callback()
735 /* Bus error */ in npcm_i2c_callback()
736 bus->cmd_err = -EAGAIN; in npcm_i2c_callback()
746 bus->operation = I2C_NO_OPER; in npcm_i2c_callback()
748 if (bus->slave) in npcm_i2c_callback()
749 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_callback()
752 complete(&bus->cmd_complete); in npcm_i2c_callback()
755 static u8 npcm_i2c_fifo_usage(struct npcm_i2c *bus) in npcm_i2c_fifo_usage() argument
757 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_fifo_usage()
758 return (bus->data->txf_sts_tx_bytes & in npcm_i2c_fifo_usage()
759 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_fifo_usage()
760 if (bus->operation == I2C_READ_OPER) in npcm_i2c_fifo_usage()
761 return (bus->data->rxf_sts_rx_bytes & in npcm_i2c_fifo_usage()
762 ioread8(bus->reg + NPCM_I2CRXF_STS)); in npcm_i2c_fifo_usage()
766 static void npcm_i2c_write_to_fifo_master(struct npcm_i2c *bus, u16 max_bytes) in npcm_i2c_write_to_fifo_master() argument
774 size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
776 if (bus->wr_ind < bus->wr_size) in npcm_i2c_write_to_fifo_master()
777 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_write_to_fifo_master()
779 npcm_i2c_wr_byte(bus, 0xFF); in npcm_i2c_write_to_fifo_master()
780 size_free_fifo = bus->data->fifo_size - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
789 static void npcm_i2c_set_fifo(struct npcm_i2c *bus, int nread, int nwrite) in npcm_i2c_set_fifo() argument
793 if (!bus->fifo_use) in npcm_i2c_set_fifo()
795 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_set_fifo()
796 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_set_fifo()
797 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_set_fifo()
801 rxf_ctl = min_t(int, nread, bus->data->fifo_size); in npcm_i2c_set_fifo()
804 if (nread <= bus->data->fifo_size) in npcm_i2c_set_fifo()
805 rxf_ctl |= bus->data->rxf_ctl_last_pec; in npcm_i2c_set_fifo()
812 if (bus->rd_ind == 0 && bus->read_block_use) { in npcm_i2c_set_fifo()
818 iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_set_fifo()
823 if (nwrite > bus->data->fifo_size) in npcm_i2c_set_fifo()
825 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
827 iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
829 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_set_fifo()
833 static void npcm_i2c_read_fifo(struct npcm_i2c *bus, u8 bytes_in_fifo) in npcm_i2c_read_fifo() argument
838 data = npcm_i2c_rd_byte(bus); in npcm_i2c_read_fifo()
839 if (bus->rd_ind < bus->rd_size) in npcm_i2c_read_fifo()
840 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_read_fifo()
844 static void npcm_i2c_master_abort(struct npcm_i2c *bus) in npcm_i2c_master_abort() argument
847 if (!npcm_i2c_is_master(bus)) in npcm_i2c_master_abort()
850 npcm_i2c_eob_int(bus, true); in npcm_i2c_master_abort()
851 npcm_i2c_master_stop(bus); in npcm_i2c_master_abort()
852 npcm_i2c_clear_master_status(bus); in npcm_i2c_master_abort()
856 static u8 npcm_i2c_get_slave_addr(struct npcm_i2c *bus, enum i2c_addr addr_type) in npcm_i2c_get_slave_addr() argument
861 dev_err(bus->dev, "get slave: try to use more than 2 SA not supported\n"); in npcm_i2c_get_slave_addr()
863 slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]); in npcm_i2c_get_slave_addr()
868 static int npcm_i2c_remove_slave_addr(struct npcm_i2c *bus, u8 slave_add) in npcm_i2c_remove_slave_addr() argument
876 if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add) in npcm_i2c_remove_slave_addr()
877 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_remove_slave_addr()
883 static void npcm_i2c_write_fifo_slave(struct npcm_i2c *bus, u16 max_bytes) in npcm_i2c_write_fifo_slave() argument
889 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_write_fifo_slave()
890 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_write_fifo_slave()
891 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_write_fifo_slave()
892 while (max_bytes-- && bus->data->fifo_size != npcm_i2c_fifo_usage(bus)) { in npcm_i2c_write_fifo_slave()
893 if (bus->slv_wr_size <= 0) in npcm_i2c_write_fifo_slave()
895 bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1); in npcm_i2c_write_fifo_slave()
896 npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]); in npcm_i2c_write_fifo_slave()
897 bus->slv_wr_ind++; in npcm_i2c_write_fifo_slave()
898 bus->slv_wr_ind = bus->slv_wr_ind & (bus->data->fifo_size - 1); in npcm_i2c_write_fifo_slave()
899 bus->slv_wr_size--; in npcm_i2c_write_fifo_slave()
903 static void npcm_i2c_read_fifo_slave(struct npcm_i2c *bus, u8 bytes_in_fifo) in npcm_i2c_read_fifo_slave() argument
907 if (!bus->slave) in npcm_i2c_read_fifo_slave()
911 data = npcm_i2c_rd_byte(bus); in npcm_i2c_read_fifo_slave()
913 bus->slv_rd_ind = bus->slv_rd_ind & (bus->data->fifo_size - 1); in npcm_i2c_read_fifo_slave()
914 bus->slv_rd_buf[bus->slv_rd_ind] = data; in npcm_i2c_read_fifo_slave()
915 bus->slv_rd_ind++; in npcm_i2c_read_fifo_slave()
918 if (bus->slv_rd_ind == 1 && bus->read_block_use) in npcm_i2c_read_fifo_slave()
919 bus->slv_rd_size = data + bus->PEC_use + 1; in npcm_i2c_read_fifo_slave()
923 static int npcm_i2c_slave_get_wr_buf(struct npcm_i2c *bus) in npcm_i2c_slave_get_wr_buf() argument
928 int ret = bus->slv_wr_ind; in npcm_i2c_slave_get_wr_buf()
931 for (i = 0; i < bus->data->fifo_size; i++) { in npcm_i2c_slave_get_wr_buf()
932 if (bus->slv_wr_size >= bus->data->fifo_size) in npcm_i2c_slave_get_wr_buf()
934 if (bus->state == I2C_SLAVE_MATCH) { in npcm_i2c_slave_get_wr_buf()
935 i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value); in npcm_i2c_slave_get_wr_buf()
936 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_get_wr_buf()
938 i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value); in npcm_i2c_slave_get_wr_buf()
940 ind = (bus->slv_wr_ind + bus->slv_wr_size) & (bus->data->fifo_size - 1); in npcm_i2c_slave_get_wr_buf()
941 bus->slv_wr_buf[ind] = value; in npcm_i2c_slave_get_wr_buf()
942 bus->slv_wr_size++; in npcm_i2c_slave_get_wr_buf()
944 return bus->data->fifo_size - ret; in npcm_i2c_slave_get_wr_buf()
947 static void npcm_i2c_slave_send_rd_buf(struct npcm_i2c *bus) in npcm_i2c_slave_send_rd_buf() argument
951 for (i = 0; i < bus->slv_rd_ind; i++) in npcm_i2c_slave_send_rd_buf()
952 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED, in npcm_i2c_slave_send_rd_buf()
953 &bus->slv_rd_buf[i]); in npcm_i2c_slave_send_rd_buf()
958 if (bus->slv_rd_ind) { in npcm_i2c_slave_send_rd_buf()
959 bus->slv_wr_size = 0; in npcm_i2c_slave_send_rd_buf()
960 bus->slv_wr_ind = 0; in npcm_i2c_slave_send_rd_buf()
963 bus->slv_rd_ind = 0; in npcm_i2c_slave_send_rd_buf()
964 bus->slv_rd_size = bus->adap.quirks->max_read_len; in npcm_i2c_slave_send_rd_buf()
966 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_slave_send_rd_buf()
967 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_slave_send_rd_buf()
970 static void npcm_i2c_slave_receive(struct npcm_i2c *bus, u16 nread, in npcm_i2c_slave_receive() argument
973 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_receive()
974 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_receive()
975 bus->slv_rd_size = nread; in npcm_i2c_slave_receive()
976 bus->slv_rd_ind = 0; in npcm_i2c_slave_receive()
978 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_slave_receive()
979 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_slave_receive()
980 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_slave_receive()
981 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_slave_receive()
984 static void npcm_i2c_slave_xmit(struct npcm_i2c *bus, u16 nwrite, in npcm_i2c_slave_xmit() argument
990 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_xmit()
993 npcm_i2c_slave_get_wr_buf(bus); in npcm_i2c_slave_xmit()
994 npcm_i2c_write_fifo_slave(bus, nwrite); in npcm_i2c_slave_xmit()
1002 * to the FIFO and onward to the bus.
1003 * NACK on read will be once reached to bus->adap->quirks->max_read_len.
1008 static void npcm_i2c_slave_wr_buf_sync(struct npcm_i2c *bus) in npcm_i2c_slave_wr_buf_sync() argument
1012 left_in_fifo = bus->data->txf_sts_tx_bytes & in npcm_i2c_slave_wr_buf_sync()
1013 ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_slave_wr_buf_sync()
1016 if (left_in_fifo >= bus->data->fifo_size || in npcm_i2c_slave_wr_buf_sync()
1017 bus->slv_wr_size >= bus->data->fifo_size) in npcm_i2c_slave_wr_buf_sync()
1021 bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1022 bus->slv_wr_size = bus->slv_wr_size + left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1024 if (bus->slv_wr_ind < 0) in npcm_i2c_slave_wr_buf_sync()
1025 bus->slv_wr_ind += bus->data->fifo_size; in npcm_i2c_slave_wr_buf_sync()
1028 static void npcm_i2c_slave_rd_wr(struct npcm_i2c *bus) in npcm_i2c_slave_rd_wr() argument
1030 if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) { in npcm_i2c_slave_rd_wr()
1035 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_rd_wr()
1036 npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len, in npcm_i2c_slave_rd_wr()
1037 bus->slv_wr_buf); in npcm_i2c_slave_rd_wr()
1045 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_rd_wr()
1046 npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus)); in npcm_i2c_slave_rd_wr()
1047 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_slave_rd_wr()
1048 npcm_i2c_slave_send_rd_buf(bus); in npcm_i2c_slave_rd_wr()
1049 npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len, in npcm_i2c_slave_rd_wr()
1050 bus->slv_rd_buf); in npcm_i2c_slave_rd_wr()
1054 static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus) in npcm_i2c_int_slave_handler() argument
1058 u8 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1062 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_int_slave_handler()
1063 npcm_i2c_slave_wr_buf_sync(bus); in npcm_i2c_int_slave_handler()
1064 if (bus->fifo_use) in npcm_i2c_int_slave_handler()
1067 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1070 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1071 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1072 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1078 iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1083 /* Slave mode: a Bus Error (BER) has been identified */ in npcm_i2c_int_slave_handler()
1086 * Check whether bus arbitration or Start or Stop during data in npcm_i2c_int_slave_handler()
1087 * xfer bus arbitration problem should not result in recovery in npcm_i2c_int_slave_handler()
1089 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_int_slave_handler()
1091 /* wait for bus busy before clear fifo */ in npcm_i2c_int_slave_handler()
1092 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1094 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1100 if (completion_done(&bus->cmd_complete) == false) { in npcm_i2c_int_slave_handler()
1101 bus->cmd_err = -EIO; in npcm_i2c_int_slave_handler()
1102 complete(&bus->cmd_complete); in npcm_i2c_int_slave_handler()
1104 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1105 iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1111 u8 bytes_in_fifo = npcm_i2c_fifo_usage(bus); in npcm_i2c_int_slave_handler()
1113 bus->stop_ind = I2C_SLAVE_DONE_IND; in npcm_i2c_int_slave_handler()
1115 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1116 npcm_i2c_read_fifo_slave(bus, bytes_in_fifo); in npcm_i2c_int_slave_handler()
1119 npcm_i2c_slave_send_rd_buf(bus); in npcm_i2c_int_slave_handler()
1122 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1129 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1130 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1131 i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0); in npcm_i2c_int_slave_handler()
1132 iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1133 if (bus->fifo_use) { in npcm_i2c_int_slave_handler()
1134 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_int_slave_handler()
1135 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_int_slave_handler()
1136 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_int_slave_handler()
1139 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1141 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1146 if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR, in npcm_i2c_int_slave_handler()
1147 ioread8(bus->reg + NPCM_I2CFIF_CTS))) { in npcm_i2c_int_slave_handler()
1148 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1149 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1150 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1151 npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus)); in npcm_i2c_int_slave_handler()
1152 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1153 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1156 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1157 npcm_i2c_slave_rd_wr(bus); in npcm_i2c_int_slave_handler()
1166 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1167 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_int_slave_handler()
1168 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_int_slave_handler()
1169 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_int_slave_handler()
1170 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_int_slave_handler()
1171 iowrite8(bus->data->fifo_size, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1173 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1175 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED, in npcm_i2c_int_slave_handler()
1177 bus->operation = I2C_READ_OPER; in npcm_i2c_int_slave_handler()
1179 if (bus->own_slave_addr == 0xFF) { in npcm_i2c_int_slave_handler()
1181 val = ioread8(bus->reg + NPCM_I2CCST); in npcm_i2c_int_slave_handler()
1188 i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_int_slave_handler()
1189 i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2); in npcm_i2c_int_slave_handler()
1200 addr = npcm_i2c_get_slave_addr(bus, eaddr); in npcm_i2c_int_slave_handler()
1202 bus->own_slave_addr = addr; in npcm_i2c_int_slave_handler()
1203 if (bus->PEC_mask & BIT(info)) in npcm_i2c_int_slave_handler()
1204 bus->PEC_use = true; in npcm_i2c_int_slave_handler()
1206 bus->PEC_use = false; in npcm_i2c_int_slave_handler()
1209 bus->own_slave_addr = 0; in npcm_i2c_int_slave_handler()
1211 bus->own_slave_addr = 0x61; in npcm_i2c_int_slave_handler()
1222 if ((bus->state == I2C_OPER_STARTED && in npcm_i2c_int_slave_handler()
1223 bus->operation == I2C_READ_OPER && in npcm_i2c_int_slave_handler()
1224 bus->stop_ind == I2C_SLAVE_XMIT_IND) || in npcm_i2c_int_slave_handler()
1225 bus->stop_ind == I2C_SLAVE_RCV_IND) { in npcm_i2c_int_slave_handler()
1227 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1232 bus->stop_ind = I2C_SLAVE_XMIT_IND; in npcm_i2c_int_slave_handler()
1234 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_int_slave_handler()
1235 bus->state = I2C_SLAVE_MATCH; in npcm_i2c_int_slave_handler()
1236 npcm_i2c_slave_rd_wr(bus); in npcm_i2c_int_slave_handler()
1237 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1243 (bus->fifo_use && in npcm_i2c_int_slave_handler()
1244 (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) { in npcm_i2c_int_slave_handler()
1245 npcm_i2c_slave_rd_wr(bus); in npcm_i2c_int_slave_handler()
1246 iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1255 npcm_i2c_eob_int(bus, false); in npcm_i2c_int_slave_handler()
1256 npcm_i2c_clear_master_status(bus); in npcm_i2c_int_slave_handler()
1265 struct npcm_i2c *bus = i2c_get_adapdata(client->adapter); in npcm_i2c_reg_slave() local
1267 bus->slave = client; in npcm_i2c_reg_slave()
1272 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1274 npcm_i2c_init_params(bus); in npcm_i2c_reg_slave()
1275 bus->slv_rd_size = 0; in npcm_i2c_reg_slave()
1276 bus->slv_wr_size = 0; in npcm_i2c_reg_slave()
1277 bus->slv_rd_ind = 0; in npcm_i2c_reg_slave()
1278 bus->slv_wr_ind = 0; in npcm_i2c_reg_slave()
1280 bus->PEC_use = true; in npcm_i2c_reg_slave()
1282 dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num, in npcm_i2c_reg_slave()
1283 client->addr, bus->PEC_use); in npcm_i2c_reg_slave()
1285 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true); in npcm_i2c_reg_slave()
1286 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_reg_slave()
1287 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_reg_slave()
1288 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_reg_slave()
1289 npcm_i2c_slave_int_enable(bus, true); in npcm_i2c_reg_slave()
1291 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1297 struct npcm_i2c *bus = client->adapter->algo_data; in npcm_i2c_unreg_slave() local
1300 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1301 if (!bus->slave) { in npcm_i2c_unreg_slave()
1302 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1305 npcm_i2c_slave_int_enable(bus, false); in npcm_i2c_unreg_slave()
1306 npcm_i2c_remove_slave_addr(bus, client->addr); in npcm_i2c_unreg_slave()
1307 bus->slave = NULL; in npcm_i2c_unreg_slave()
1308 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1313 static void npcm_i2c_master_fifo_read(struct npcm_i2c *bus) in npcm_i2c_master_fifo_read() argument
1319 fifo_bytes = npcm_i2c_fifo_usage(bus); in npcm_i2c_master_fifo_read()
1320 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1329 if (rcount < (2 * bus->data->fifo_size) && rcount > bus->data->fifo_size) in npcm_i2c_master_fifo_read()
1330 fifo_bytes = rcount - bus->data->fifo_size; in npcm_i2c_master_fifo_read()
1334 bus->state = I2C_STOP_PENDING; in npcm_i2c_master_fifo_read()
1335 bus->stop_ind = ind; in npcm_i2c_master_fifo_read()
1336 npcm_i2c_eob_int(bus, true); in npcm_i2c_master_fifo_read()
1338 npcm_i2c_master_stop(bus); in npcm_i2c_master_fifo_read()
1339 npcm_i2c_read_fifo(bus, fifo_bytes); in npcm_i2c_master_fifo_read()
1341 npcm_i2c_read_fifo(bus, fifo_bytes); in npcm_i2c_master_fifo_read()
1342 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1343 npcm_i2c_set_fifo(bus, rcount, -1); in npcm_i2c_master_fifo_read()
1347 static void npcm_i2c_irq_master_handler_write(struct npcm_i2c *bus) in npcm_i2c_irq_master_handler_write() argument
1351 if (bus->fifo_use) in npcm_i2c_irq_master_handler_write()
1352 npcm_i2c_clear_tx_fifo(bus); /* clear the TX fifo status bit */ in npcm_i2c_irq_master_handler_write()
1355 if (bus->wr_ind == bus->wr_size) { in npcm_i2c_irq_master_handler_write()
1356 if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0) in npcm_i2c_irq_master_handler_write()
1366 if (bus->rd_size == 0) { in npcm_i2c_irq_master_handler_write()
1368 npcm_i2c_eob_int(bus, true); in npcm_i2c_irq_master_handler_write()
1369 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_write()
1370 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_master_handler_write()
1371 npcm_i2c_master_stop(bus); in npcm_i2c_irq_master_handler_write()
1373 npcm_i2c_wr_byte(bus, 0xFF); in npcm_i2c_irq_master_handler_write()
1377 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_master_handler_write()
1379 npcm_i2c_master_start(bus); in npcm_i2c_irq_master_handler_write()
1387 if (bus->rd_size == 1) in npcm_i2c_irq_master_handler_write()
1388 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_irq_master_handler_write()
1391 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_master_handler_write()
1393 npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1); in npcm_i2c_irq_master_handler_write()
1397 if (!bus->fifo_use || bus->wr_size == 1) { in npcm_i2c_irq_master_handler_write()
1398 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_irq_master_handler_write()
1400 wcount = bus->wr_size - bus->wr_ind; in npcm_i2c_irq_master_handler_write()
1401 npcm_i2c_set_fifo(bus, -1, wcount); in npcm_i2c_irq_master_handler_write()
1403 npcm_i2c_write_to_fifo_master(bus, wcount); in npcm_i2c_irq_master_handler_write()
1408 static void npcm_i2c_irq_master_handler_read(struct npcm_i2c *bus) in npcm_i2c_irq_master_handler_read() argument
1414 block_extra_bytes_size = bus->read_block_use + bus->PEC_use; in npcm_i2c_irq_master_handler_read()
1420 if (bus->rd_ind == 0) { /* first byte handling: */ in npcm_i2c_irq_master_handler_read()
1421 if (bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1423 data = npcm_i2c_rd_byte(bus); in npcm_i2c_irq_master_handler_read()
1425 bus->rd_size = data + block_extra_bytes_size; in npcm_i2c_irq_master_handler_read()
1426 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_irq_master_handler_read()
1429 if (bus->fifo_use) { in npcm_i2c_irq_master_handler_read()
1430 data = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1432 iowrite8(data, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1435 npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1); in npcm_i2c_irq_master_handler_read()
1436 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_irq_master_handler_read()
1438 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_irq_master_handler_read()
1439 npcm_i2c_master_fifo_read(bus); in npcm_i2c_irq_master_handler_read()
1442 if (bus->rd_size == block_extra_bytes_size && in npcm_i2c_irq_master_handler_read()
1443 bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1444 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_read()
1445 bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND; in npcm_i2c_irq_master_handler_read()
1446 bus->cmd_err = -EIO; in npcm_i2c_irq_master_handler_read()
1447 npcm_i2c_eob_int(bus, true); in npcm_i2c_irq_master_handler_read()
1448 npcm_i2c_master_stop(bus); in npcm_i2c_irq_master_handler_read()
1449 npcm_i2c_read_fifo(bus, npcm_i2c_fifo_usage(bus)); in npcm_i2c_irq_master_handler_read()
1451 npcm_i2c_master_fifo_read(bus); in npcm_i2c_irq_master_handler_read()
1456 static void npcm_i2c_irq_handle_nmatch(struct npcm_i2c *bus) in npcm_i2c_irq_handle_nmatch() argument
1458 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_nmatch()
1459 npcm_i2c_nack(bus); in npcm_i2c_irq_handle_nmatch()
1460 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_nmatch()
1461 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_nmatch()
1465 static void npcm_i2c_irq_handle_nack(struct npcm_i2c *bus) in npcm_i2c_irq_handle_nack() argument
1469 if (bus->nack_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_nack()
1470 bus->nack_cnt++; in npcm_i2c_irq_handle_nack()
1472 if (bus->fifo_use) { in npcm_i2c_irq_handle_nack()
1477 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_irq_handle_nack()
1478 bus->wr_ind -= npcm_i2c_fifo_usage(bus); in npcm_i2c_irq_handle_nack()
1481 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_nack()
1485 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_irq_handle_nack()
1487 if (npcm_i2c_is_master(bus)) { in npcm_i2c_irq_handle_nack()
1489 npcm_i2c_eob_int(bus, false); in npcm_i2c_irq_handle_nack()
1490 npcm_i2c_master_stop(bus); in npcm_i2c_irq_handle_nack()
1493 npcm_i2c_rd_byte(bus); in npcm_i2c_irq_handle_nack()
1496 * The bus is released from stall only after the SW clears in npcm_i2c_irq_handle_nack()
1499 npcm_i2c_clear_master_status(bus); in npcm_i2c_irq_handle_nack()
1500 readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val, in npcm_i2c_irq_handle_nack()
1502 /* Verify no status bits are still set after bus is released */ in npcm_i2c_irq_handle_nack()
1503 npcm_i2c_clear_master_status(bus); in npcm_i2c_irq_handle_nack()
1505 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_nack()
1509 * In such case, the bus is released from stall only after the in npcm_i2c_irq_handle_nack()
1512 npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind); in npcm_i2c_irq_handle_nack()
1515 /* Master mode: a Bus Error has been identified */
1516 static void npcm_i2c_irq_handle_ber(struct npcm_i2c *bus) in npcm_i2c_irq_handle_ber() argument
1518 if (bus->ber_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_ber()
1519 bus->ber_cnt++; in npcm_i2c_irq_handle_ber()
1520 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_ber()
1521 if (npcm_i2c_is_master(bus)) { in npcm_i2c_irq_handle_ber()
1522 npcm_i2c_master_abort(bus); in npcm_i2c_irq_handle_ber()
1524 npcm_i2c_clear_master_status(bus); in npcm_i2c_irq_handle_ber()
1526 /* Clear BB (BUS BUSY) bit */ in npcm_i2c_irq_handle_ber()
1527 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_irq_handle_ber()
1529 bus->cmd_err = -EAGAIN; in npcm_i2c_irq_handle_ber()
1530 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_ber()
1532 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_ber()
1536 static void npcm_i2c_irq_handle_eob(struct npcm_i2c *bus) in npcm_i2c_irq_handle_eob() argument
1538 npcm_i2c_eob_int(bus, false); in npcm_i2c_irq_handle_eob()
1539 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_eob()
1540 npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind); in npcm_i2c_irq_handle_eob()
1544 static void npcm_i2c_irq_handle_stall_after_start(struct npcm_i2c *bus) in npcm_i2c_irq_handle_stall_after_start() argument
1546 if (npcm_i2c_is_quick(bus)) { in npcm_i2c_irq_handle_stall_after_start()
1547 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_handle_stall_after_start()
1548 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_handle_stall_after_start()
1549 npcm_i2c_eob_int(bus, true); in npcm_i2c_irq_handle_stall_after_start()
1550 npcm_i2c_master_stop(bus); in npcm_i2c_irq_handle_stall_after_start()
1551 } else if ((bus->rd_size == 1) && !bus->read_block_use) { in npcm_i2c_irq_handle_stall_after_start()
1556 npcm_i2c_nack(bus); in npcm_i2c_irq_handle_stall_after_start()
1560 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_irq_handle_stall_after_start()
1563 iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_stall_after_start()
1567 static void npcm_i2c_irq_handle_sda(struct npcm_i2c *bus, u8 i2cst) in npcm_i2c_irq_handle_sda() argument
1571 if (!npcm_i2c_is_master(bus)) in npcm_i2c_irq_handle_sda()
1574 if (bus->state == I2C_IDLE) { in npcm_i2c_irq_handle_sda()
1575 bus->stop_ind = I2C_WAKE_UP_IND; in npcm_i2c_irq_handle_sda()
1577 if (npcm_i2c_is_quick(bus) || bus->read_block_use) in npcm_i2c_irq_handle_sda()
1582 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_irq_handle_sda()
1584 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_irq_handle_sda()
1592 if (bus->wr_size == 0 && bus->rd_size == 1) in npcm_i2c_irq_handle_sda()
1593 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_irq_handle_sda()
1598 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_irq_handle_sda()
1600 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1605 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1609 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1618 if (bus->wr_size) in npcm_i2c_irq_handle_sda()
1619 npcm_i2c_set_fifo(bus, -1, bus->wr_size); in npcm_i2c_irq_handle_sda()
1621 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_handle_sda()
1623 bus->state = I2C_OPER_STARTED; in npcm_i2c_irq_handle_sda()
1625 if (npcm_i2c_is_quick(bus) || bus->wr_size) in npcm_i2c_irq_handle_sda()
1626 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_irq_handle_sda()
1628 npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0)); in npcm_i2c_irq_handle_sda()
1632 bus->operation = I2C_WRITE_OPER; in npcm_i2c_irq_handle_sda()
1633 npcm_i2c_irq_master_handler_write(bus); in npcm_i2c_irq_handle_sda()
1635 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_handle_sda()
1636 npcm_i2c_irq_master_handler_read(bus); in npcm_i2c_irq_handle_sda()
1641 static int npcm_i2c_int_master_handler(struct npcm_i2c *bus) in npcm_i2c_int_master_handler() argument
1646 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_master_handler()
1649 npcm_i2c_irq_handle_nmatch(bus); in npcm_i2c_int_master_handler()
1654 npcm_i2c_irq_handle_nack(bus); in npcm_i2c_int_master_handler()
1658 /* Master mode: a Bus Error has been identified */ in npcm_i2c_int_master_handler()
1660 npcm_i2c_irq_handle_ber(bus); in npcm_i2c_int_master_handler()
1666 ioread8(bus->reg + NPCM_I2CCTL1)) == 1) && in npcm_i2c_int_master_handler()
1668 ioread8(bus->reg + NPCM_I2CCST3)))) { in npcm_i2c_int_master_handler()
1669 npcm_i2c_irq_handle_eob(bus); in npcm_i2c_int_master_handler()
1675 npcm_i2c_irq_handle_stall_after_start(bus); in npcm_i2c_int_master_handler()
1681 (bus->fifo_use && in npcm_i2c_int_master_handler()
1682 (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) { in npcm_i2c_int_master_handler()
1683 npcm_i2c_irq_handle_sda(bus, i2cst); in npcm_i2c_int_master_handler()
1697 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_recovery_tgclk() local
1702 dev_dbg(bus->dev, "bus%d-0x%x recovery skipped, bus not stuck", in npcm_i2c_recovery_tgclk()
1703 bus->num, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1704 npcm_i2c_reset(bus); in npcm_i2c_recovery_tgclk()
1708 npcm_i2c_int_enable(bus, false); in npcm_i2c_recovery_tgclk()
1709 npcm_i2c_disable(bus); in npcm_i2c_recovery_tgclk()
1710 npcm_i2c_enable(bus); in npcm_i2c_recovery_tgclk()
1711 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1712 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_recovery_tgclk()
1713 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_recovery_tgclk()
1714 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_recovery_tgclk()
1715 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_recovery_tgclk()
1716 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_recovery_tgclk()
1719 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_recovery_tgclk()
1722 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1725 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1726 npcm_i2c_set_fifo(bus, -1, 0); in npcm_i2c_recovery_tgclk()
1731 iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1743 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1744 npcm_i2c_master_start(bus); in npcm_i2c_recovery_tgclk()
1749 if (npcm_i2c_is_master(bus) > 0) { in npcm_i2c_recovery_tgclk()
1751 npcm_i2c_master_stop(bus); in npcm_i2c_recovery_tgclk()
1755 npcm_i2c_reset(bus); in npcm_i2c_recovery_tgclk()
1756 npcm_i2c_int_enable(bus, true); in npcm_i2c_recovery_tgclk()
1763 if (bus->rec_fail_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1764 bus->rec_fail_cnt++; in npcm_i2c_recovery_tgclk()
1766 if (bus->rec_succ_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1767 bus->rec_succ_cnt++; in npcm_i2c_recovery_tgclk()
1775 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_recovery_init() local
1776 struct i2c_bus_recovery_info *rinfo = &bus->rinfo; in npcm_i2c_recovery_init()
1799 * and bus frequency.
1800 * 100kHz bus requires tSCL = 4 * SCLFRQ * tCLK. LT and HT are symmetric.
1801 * 400kHz bus requires asymmetric HT and LT. A different equation is recommended
1805 static int npcm_i2c_init_clk(struct npcm_i2c *bus, u32 bus_freq_hz) in npcm_i2c_init_clk() argument
1816 src_clk_khz = bus->apb_clk / 1000; in npcm_i2c_init_clk()
1818 bus->bus_freq = bus_freq_hz; in npcm_i2c_init_clk()
1899 bus->reg + NPCM_I2CCTL2); in npcm_i2c_init_clk()
1903 bus->reg + NPCM_I2CCTL3); in npcm_i2c_init_clk()
1906 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_init_clk()
1914 iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT); in npcm_i2c_init_clk()
1915 iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT); in npcm_i2c_init_clk()
1917 iowrite8(dbnct, bus->reg + NPCM_I2CCTL5); in npcm_i2c_init_clk()
1920 iowrite8(hldt, bus->reg + NPCM_I2CCTL4); in npcm_i2c_init_clk()
1923 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_init_clk()
1928 static int npcm_i2c_init_module(struct npcm_i2c *bus, enum i2c_mode mode, in npcm_i2c_init_module() argument
1935 if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) || in npcm_i2c_init_module()
1939 npcm_i2c_int_enable(bus, false); in npcm_i2c_init_module()
1940 npcm_i2c_disable(bus); in npcm_i2c_init_module()
1943 if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) { in npcm_i2c_init_module()
1944 bus->fifo_use = true; in npcm_i2c_init_module()
1945 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_init_module()
1946 val = ioread8(bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1948 iowrite8(val, bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1949 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_init_module()
1951 bus->fifo_use = false; in npcm_i2c_init_module()
1955 ret = npcm_i2c_init_clk(bus, bus_freq_hz); in npcm_i2c_init_module()
1957 dev_err(bus->dev, "npcm_i2c_init_clk failed\n"); in npcm_i2c_init_module()
1962 npcm_i2c_enable(bus); in npcm_i2c_init_module()
1963 bus->state = I2C_IDLE; in npcm_i2c_init_module()
1964 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1966 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1968 npcm_i2c_reset(bus); in npcm_i2c_init_module()
1971 if ((npcm_i2c_get_SDA(&bus->adap) == 0) || (npcm_i2c_get_SCL(&bus->adap) == 0)) { in npcm_i2c_init_module()
1972 dev_err(bus->dev, "I2C%d init fail: lines are low\n", bus->num); in npcm_i2c_init_module()
1973 dev_err(bus->dev, "SDA=%d SCL=%d\n", npcm_i2c_get_SDA(&bus->adap), in npcm_i2c_init_module()
1974 npcm_i2c_get_SCL(&bus->adap)); in npcm_i2c_init_module()
1978 npcm_i2c_int_enable(bus, true); in npcm_i2c_init_module()
1982 static int __npcm_i2c_init(struct npcm_i2c *bus, struct platform_device *pdev) in __npcm_i2c_init() argument
1988 bus->state = I2C_DISABLE; in __npcm_i2c_init()
1989 bus->master_or_slave = I2C_SLAVE; in __npcm_i2c_init()
1990 bus->int_time_stamp = 0; in __npcm_i2c_init()
1992 bus->slave = NULL; in __npcm_i2c_init()
2002 ret = npcm_i2c_init_module(bus, I2C_MASTER, clk_freq_hz); in __npcm_i2c_init()
2013 struct npcm_i2c *bus = dev_id; in npcm_i2c_bus_irq() local
2015 if (npcm_i2c_is_master(bus)) in npcm_i2c_bus_irq()
2016 bus->master_or_slave = I2C_MASTER; in npcm_i2c_bus_irq()
2018 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_bus_irq()
2019 bus->int_time_stamp = jiffies; in npcm_i2c_bus_irq()
2020 if (!npcm_i2c_int_master_handler(bus)) in npcm_i2c_bus_irq()
2024 if (bus->slave) { in npcm_i2c_bus_irq()
2025 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_bus_irq()
2026 if (npcm_i2c_int_slave_handler(bus)) in npcm_i2c_bus_irq()
2031 npcm_i2c_clear_master_status(bus); in npcm_i2c_bus_irq()
2036 static bool npcm_i2c_master_start_xmit(struct npcm_i2c *bus, in npcm_i2c_master_start_xmit() argument
2041 if (bus->state != I2C_IDLE) { in npcm_i2c_master_start_xmit()
2042 bus->cmd_err = -EBUSY; in npcm_i2c_master_start_xmit()
2045 bus->dest_addr = slave_addr << 1; in npcm_i2c_master_start_xmit()
2046 bus->wr_buf = write_data; in npcm_i2c_master_start_xmit()
2047 bus->wr_size = nwrite; in npcm_i2c_master_start_xmit()
2048 bus->wr_ind = 0; in npcm_i2c_master_start_xmit()
2049 bus->rd_buf = read_data; in npcm_i2c_master_start_xmit()
2050 bus->rd_size = nread; in npcm_i2c_master_start_xmit()
2051 bus->rd_ind = 0; in npcm_i2c_master_start_xmit()
2052 bus->PEC_use = 0; in npcm_i2c_master_start_xmit()
2056 bus->PEC_use = use_PEC; in npcm_i2c_master_start_xmit()
2058 bus->read_block_use = use_read_block; in npcm_i2c_master_start_xmit()
2060 bus->operation = I2C_READ_OPER; in npcm_i2c_master_start_xmit()
2062 bus->operation = I2C_WRITE_OPER; in npcm_i2c_master_start_xmit()
2063 if (bus->fifo_use) { in npcm_i2c_master_start_xmit()
2066 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_master_start_xmit()
2068 i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2071 iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2074 bus->state = I2C_IDLE; in npcm_i2c_master_start_xmit()
2075 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_master_start_xmit()
2076 npcm_i2c_master_start(bus); in npcm_i2c_master_start_xmit()
2083 struct npcm_i2c *bus = container_of(adap, struct npcm_i2c, adap); in npcm_i2c_master_xfer() local
2095 if (bus->state == I2C_DISABLE) { in npcm_i2c_master_xfer()
2096 dev_err(bus->dev, "I2C%d module is disabled", bus->num); in npcm_i2c_master_xfer()
2139 timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); in npcm_i2c_master_xfer()
2140 timeout = max_t(unsigned long, bus->adap.timeout, usecs_to_jiffies(timeout_usec)); in npcm_i2c_master_xfer()
2142 dev_err(bus->dev, "i2c%d buffer too big\n", bus->num); in npcm_i2c_master_xfer()
2149 * we must clear slave address immediately when the bus is not in npcm_i2c_master_xfer()
2153 spin_lock_irqsave(&bus->lock, flags); in npcm_i2c_master_xfer()
2154 bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB; in npcm_i2c_master_xfer()
2156 if (!bus_busy && bus->slave) in npcm_i2c_master_xfer()
2157 iowrite8((bus->slave->addr & 0x7F), in npcm_i2c_master_xfer()
2158 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2160 spin_unlock_irqrestore(&bus->lock, flags); in npcm_i2c_master_xfer()
2165 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_master_xfer()
2166 npcm_i2c_reset(bus); in npcm_i2c_master_xfer()
2171 npcm_i2c_init_params(bus); in npcm_i2c_master_xfer()
2172 bus->dest_addr = slave_addr; in npcm_i2c_master_xfer()
2173 bus->msgs = msgs; in npcm_i2c_master_xfer()
2174 bus->msgs_num = num; in npcm_i2c_master_xfer()
2175 bus->cmd_err = 0; in npcm_i2c_master_xfer()
2176 bus->read_block_use = read_block; in npcm_i2c_master_xfer()
2178 reinit_completion(&bus->cmd_complete); in npcm_i2c_master_xfer()
2180 npcm_i2c_int_enable(bus, true); in npcm_i2c_master_xfer()
2182 if (npcm_i2c_master_start_xmit(bus, slave_addr, nwrite, nread, in npcm_i2c_master_xfer()
2185 time_left = wait_for_completion_timeout(&bus->cmd_complete, in npcm_i2c_master_xfer()
2189 if (bus->timeout_cnt < ULLONG_MAX) in npcm_i2c_master_xfer()
2190 bus->timeout_cnt++; in npcm_i2c_master_xfer()
2191 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_master_xfer()
2193 bus->cmd_err = -EIO; in npcm_i2c_master_xfer()
2194 bus->state = I2C_IDLE; in npcm_i2c_master_xfer()
2199 /* if there was BER, check if need to recover the bus: */ in npcm_i2c_master_xfer()
2200 if (bus->cmd_err == -EAGAIN) in npcm_i2c_master_xfer()
2201 bus->cmd_err = i2c_recover_bus(adap); in npcm_i2c_master_xfer()
2208 else if (bus->cmd_err && in npcm_i2c_master_xfer()
2209 (bus->data->rxf_ctl_last_pec & ioread8(bus->reg + NPCM_I2CRXF_CTL))) in npcm_i2c_master_xfer()
2210 npcm_i2c_reset(bus); in npcm_i2c_master_xfer()
2213 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_master_xfer()
2214 npcm_i2c_eob_int(bus, false); in npcm_i2c_master_xfer()
2218 if (bus->slave) in npcm_i2c_master_xfer()
2219 iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN, in npcm_i2c_master_xfer()
2220 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2222 npcm_i2c_int_enable(bus, false); in npcm_i2c_master_xfer()
2224 return bus->cmd_err; in npcm_i2c_master_xfer()
2252 struct npcm_i2c *bus) in npcm_i2c_init_debugfs() argument
2254 debugfs_create_u64("ber_cnt", 0444, bus->adap.debugfs, &bus->ber_cnt); in npcm_i2c_init_debugfs()
2255 debugfs_create_u64("nack_cnt", 0444, bus->adap.debugfs, &bus->nack_cnt); in npcm_i2c_init_debugfs()
2256 debugfs_create_u64("rec_succ_cnt", 0444, bus->adap.debugfs, &bus->rec_succ_cnt); in npcm_i2c_init_debugfs()
2257 debugfs_create_u64("rec_fail_cnt", 0444, bus->adap.debugfs, &bus->rec_fail_cnt); in npcm_i2c_init_debugfs()
2258 debugfs_create_u64("timeout_cnt", 0444, bus->adap.debugfs, &bus->timeout_cnt); in npcm_i2c_init_debugfs()
2259 debugfs_create_u64("tx_complete_cnt", 0444, bus->adap.debugfs, &bus->tx_complete_cnt); in npcm_i2c_init_debugfs()
2268 struct npcm_i2c *bus; in npcm_i2c_probe_bus() local
2273 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); in npcm_i2c_probe_bus()
2274 if (!bus) in npcm_i2c_probe_bus()
2277 bus->dev = &pdev->dev; in npcm_i2c_probe_bus()
2279 bus->data = of_device_get_match_data(dev); in npcm_i2c_probe_bus()
2280 if (!bus->data) { in npcm_i2c_probe_bus()
2285 bus->num = of_alias_get_id(pdev->dev.of_node, "i2c"); in npcm_i2c_probe_bus()
2290 bus->apb_clk = clk_get_rate(i2c_clk); in npcm_i2c_probe_bus()
2298 regmap_write(gcr_regmap, NPCM_I2CSEGCTL, bus->data->segctl_init_val); in npcm_i2c_probe_bus()
2300 bus->reg = devm_platform_ioremap_resource(pdev, 0); in npcm_i2c_probe_bus()
2301 if (IS_ERR(bus->reg)) in npcm_i2c_probe_bus()
2302 return PTR_ERR(bus->reg); in npcm_i2c_probe_bus()
2304 spin_lock_init(&bus->lock); in npcm_i2c_probe_bus()
2305 init_completion(&bus->cmd_complete); in npcm_i2c_probe_bus()
2307 adap = &bus->adap; in npcm_i2c_probe_bus()
2313 adap->algo_data = bus; in npcm_i2c_probe_bus()
2322 ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0, in npcm_i2c_probe_bus()
2323 dev_name(bus->dev), bus); in npcm_i2c_probe_bus()
2327 ret = __npcm_i2c_init(bus, pdev); in npcm_i2c_probe_bus()
2333 i2c_set_adapdata(adap, bus); in npcm_i2c_probe_bus()
2335 snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d", in npcm_i2c_probe_bus()
2336 bus->num); in npcm_i2c_probe_bus()
2337 ret = i2c_add_numbered_adapter(&bus->adap); in npcm_i2c_probe_bus()
2341 platform_set_drvdata(pdev, bus); in npcm_i2c_probe_bus()
2342 npcm_i2c_init_debugfs(pdev, bus); in npcm_i2c_probe_bus()
2349 struct npcm_i2c *bus = platform_get_drvdata(pdev); in npcm_i2c_remove_bus() local
2351 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2352 npcm_i2c_disable(bus); in npcm_i2c_remove_bus()
2353 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2354 i2c_del_adapter(&bus->adap); in npcm_i2c_remove_bus()
2378 MODULE_DESCRIPTION("Nuvoton I2C Bus Driver");