Lines Matching full:bus
30 * of the bus.
143 #define NPCM_I2CT_OUT 0x14 /* Bus T.O. */
319 static inline void npcm_i2c_select_bank(struct npcm_i2c *bus, in npcm_i2c_select_bank() argument
322 u8 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
328 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_select_bank()
331 static void npcm_i2c_init_params(struct npcm_i2c *bus) in npcm_i2c_init_params() argument
333 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_init_params()
334 bus->rd_size = 0; in npcm_i2c_init_params()
335 bus->wr_size = 0; in npcm_i2c_init_params()
336 bus->rd_ind = 0; in npcm_i2c_init_params()
337 bus->wr_ind = 0; in npcm_i2c_init_params()
338 bus->read_block_use = false; in npcm_i2c_init_params()
339 bus->int_time_stamp = 0; in npcm_i2c_init_params()
340 bus->PEC_use = false; in npcm_i2c_init_params()
341 bus->PEC_mask = 0; in npcm_i2c_init_params()
343 if (bus->slave) in npcm_i2c_init_params()
344 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_init_params()
348 static inline void npcm_i2c_wr_byte(struct npcm_i2c *bus, u8 data) in npcm_i2c_wr_byte() argument
350 iowrite8(data, bus->reg + NPCM_I2CSDA); in npcm_i2c_wr_byte()
353 static inline u8 npcm_i2c_rd_byte(struct npcm_i2c *bus) in npcm_i2c_rd_byte() argument
355 return ioread8(bus->reg + NPCM_I2CSDA); in npcm_i2c_rd_byte()
360 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_get_SCL() local
362 return !!(I2CCTL3_SCL_LVL & ioread32(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SCL()
367 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_get_SDA() local
369 return !!(I2CCTL3_SDA_LVL & ioread32(bus->reg + NPCM_I2CCTL3)); in npcm_i2c_get_SDA()
372 static inline u16 npcm_i2c_get_index(struct npcm_i2c *bus) in npcm_i2c_get_index() argument
374 if (bus->operation == I2C_READ_OPER) in npcm_i2c_get_index()
375 return bus->rd_ind; in npcm_i2c_get_index()
376 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_get_index()
377 return bus->wr_ind; in npcm_i2c_get_index()
382 static inline bool npcm_i2c_is_quick(struct npcm_i2c *bus) in npcm_i2c_is_quick() argument
384 return bus->wr_size == 0 && bus->rd_size == 0; in npcm_i2c_is_quick()
387 static void npcm_i2c_disable(struct npcm_i2c *bus) in npcm_i2c_disable() argument
395 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_disable()
399 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_disable()
401 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_disable()
404 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
406 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_disable()
408 bus->state = I2C_DISABLE; in npcm_i2c_disable()
411 static void npcm_i2c_enable(struct npcm_i2c *bus) in npcm_i2c_enable() argument
413 u8 i2cctl2 = ioread8(bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
416 iowrite8(i2cctl2, bus->reg + NPCM_I2CCTL2); in npcm_i2c_enable()
417 bus->state = I2C_IDLE; in npcm_i2c_enable()
421 static inline void npcm_i2c_eob_int(struct npcm_i2c *bus, bool enable) in npcm_i2c_eob_int() argument
426 val = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
428 iowrite8(val, bus->reg + NPCM_I2CCST3); in npcm_i2c_eob_int()
430 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
436 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_eob_int()
439 static inline bool npcm_i2c_tx_fifo_empty(struct npcm_i2c *bus) in npcm_i2c_tx_fifo_empty() argument
443 tx_fifo_sts = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_tx_fifo_empty()
452 static inline bool npcm_i2c_rx_fifo_full(struct npcm_i2c *bus) in npcm_i2c_rx_fifo_full() argument
456 rx_fifo_sts = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_rx_fifo_full()
465 static inline void npcm_i2c_clear_fifo_int(struct npcm_i2c *bus) in npcm_i2c_clear_fifo_int() argument
469 val = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
471 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_clear_fifo_int()
474 static inline void npcm_i2c_clear_tx_fifo(struct npcm_i2c *bus) in npcm_i2c_clear_tx_fifo() argument
478 val = ioread8(bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
480 iowrite8(val, bus->reg + NPCM_I2CTXF_STS); in npcm_i2c_clear_tx_fifo()
483 static inline void npcm_i2c_clear_rx_fifo(struct npcm_i2c *bus) in npcm_i2c_clear_rx_fifo() argument
487 val = ioread8(bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
489 iowrite8(val, bus->reg + NPCM_I2CRXF_STS); in npcm_i2c_clear_rx_fifo()
492 static void npcm_i2c_int_enable(struct npcm_i2c *bus, bool enable) in npcm_i2c_int_enable() argument
496 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
502 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_int_enable()
505 static inline void npcm_i2c_master_start(struct npcm_i2c *bus) in npcm_i2c_master_start() argument
509 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
512 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_start()
515 static inline void npcm_i2c_master_stop(struct npcm_i2c *bus) in npcm_i2c_master_stop() argument
525 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
528 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_master_stop()
530 if (!bus->fifo_use) in npcm_i2c_master_stop()
533 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_master_stop()
535 if (bus->operation == I2C_READ_OPER) in npcm_i2c_master_stop()
536 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_master_stop()
538 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_master_stop()
539 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_master_stop()
540 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_master_stop()
543 static inline void npcm_i2c_stall_after_start(struct npcm_i2c *bus, bool stall) in npcm_i2c_stall_after_start() argument
547 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
553 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_stall_after_start()
556 static inline void npcm_i2c_nack(struct npcm_i2c *bus) in npcm_i2c_nack() argument
560 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
563 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_nack()
567 static void npcm_i2c_slave_int_enable(struct npcm_i2c *bus, bool enable) in npcm_i2c_slave_int_enable() argument
572 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
578 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_int_enable()
581 static int npcm_i2c_slave_enable(struct npcm_i2c *bus, enum i2c_addr addr_type, in npcm_i2c_slave_enable() argument
590 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
595 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_slave_enable()
599 i2cctl3 = ioread8(bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
604 iowrite8(i2cctl3, bus->reg + NPCM_I2CCTL3); in npcm_i2c_slave_enable()
611 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_slave_enable()
613 iowrite8(sa_reg, bus->reg + npcm_i2caddr[addr_type]); in npcm_i2c_slave_enable()
614 npcm_i2c_slave_int_enable(bus, enable); in npcm_i2c_slave_enable()
616 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_slave_enable()
621 static void npcm_i2c_reset(struct npcm_i2c *bus) in npcm_i2c_reset() argument
632 i2cctl1 = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
634 npcm_i2c_disable(bus); in npcm_i2c_reset()
635 npcm_i2c_enable(bus); in npcm_i2c_reset()
639 iowrite8(i2cctl1, bus->reg + NPCM_I2CCTL1); in npcm_i2c_reset()
641 /* Clear BB (BUS BUSY) bit */ in npcm_i2c_reset()
642 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_reset()
643 iowrite8(0xFF, bus->reg + NPCM_I2CST); in npcm_i2c_reset()
646 iowrite8(NPCM_I2CCST3_EO_BUSY, bus->reg + NPCM_I2CCST3); in npcm_i2c_reset()
649 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_reset()
652 if (bus->slave) { in npcm_i2c_reset()
653 addr = bus->slave->addr; in npcm_i2c_reset()
654 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, addr, true); in npcm_i2c_reset()
658 bus->state = I2C_IDLE; in npcm_i2c_reset()
661 static inline bool npcm_i2c_is_master(struct npcm_i2c *bus) in npcm_i2c_is_master() argument
663 return !!FIELD_GET(NPCM_I2CST_MASTER, ioread8(bus->reg + NPCM_I2CST)); in npcm_i2c_is_master()
666 static void npcm_i2c_callback(struct npcm_i2c *bus, in npcm_i2c_callback() argument
672 msgs = bus->msgs; in npcm_i2c_callback()
673 msgs_num = bus->msgs_num; in npcm_i2c_callback()
681 if (completion_done(&bus->cmd_complete)) in npcm_i2c_callback()
686 bus->cmd_err = bus->msgs_num; in npcm_i2c_callback()
690 if (bus->msgs) { in npcm_i2c_callback()
697 if (completion_done(&bus->cmd_complete) == false) in npcm_i2c_callback()
698 complete(&bus->cmd_complete); in npcm_i2c_callback()
703 bus->cmd_err = -ENXIO; in npcm_i2c_callback()
704 if (bus->master_or_slave == I2C_MASTER) in npcm_i2c_callback()
705 complete(&bus->cmd_complete); in npcm_i2c_callback()
709 /* Bus error */ in npcm_i2c_callback()
710 bus->cmd_err = -EAGAIN; in npcm_i2c_callback()
711 if (bus->master_or_slave == I2C_MASTER) in npcm_i2c_callback()
712 complete(&bus->cmd_complete); in npcm_i2c_callback()
722 bus->operation = I2C_NO_OPER; in npcm_i2c_callback()
724 if (bus->slave) in npcm_i2c_callback()
725 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_callback()
729 static u8 npcm_i2c_fifo_usage(struct npcm_i2c *bus) in npcm_i2c_fifo_usage() argument
731 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_fifo_usage()
733 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_fifo_usage()
734 if (bus->operation == I2C_READ_OPER) in npcm_i2c_fifo_usage()
736 ioread8(bus->reg + NPCM_I2CRXF_STS)); in npcm_i2c_fifo_usage()
740 static void npcm_i2c_write_to_fifo_master(struct npcm_i2c *bus, u16 max_bytes) in npcm_i2c_write_to_fifo_master() argument
748 size_free_fifo = I2C_HW_FIFO_SIZE - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
750 if (bus->wr_ind < bus->wr_size) in npcm_i2c_write_to_fifo_master()
751 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_write_to_fifo_master()
753 npcm_i2c_wr_byte(bus, 0xFF); in npcm_i2c_write_to_fifo_master()
754 size_free_fifo = I2C_HW_FIFO_SIZE - npcm_i2c_fifo_usage(bus); in npcm_i2c_write_to_fifo_master()
763 static void npcm_i2c_set_fifo(struct npcm_i2c *bus, int nread, int nwrite) in npcm_i2c_set_fifo() argument
767 if (!bus->fifo_use) in npcm_i2c_set_fifo()
769 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_set_fifo()
770 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_set_fifo()
771 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_set_fifo()
786 if (bus->rd_ind == 0 && bus->read_block_use) { in npcm_i2c_set_fifo()
792 iowrite8(rxf_ctl, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_set_fifo()
799 iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
801 iowrite8(nwrite, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_set_fifo()
803 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_set_fifo()
807 static void npcm_i2c_read_fifo(struct npcm_i2c *bus, u8 bytes_in_fifo) in npcm_i2c_read_fifo() argument
812 data = npcm_i2c_rd_byte(bus); in npcm_i2c_read_fifo()
813 if (bus->rd_ind < bus->rd_size) in npcm_i2c_read_fifo()
814 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_read_fifo()
818 static inline void npcm_i2c_clear_master_status(struct npcm_i2c *bus) in npcm_i2c_clear_master_status() argument
824 iowrite8(val, bus->reg + NPCM_I2CST); in npcm_i2c_clear_master_status()
827 static void npcm_i2c_master_abort(struct npcm_i2c *bus) in npcm_i2c_master_abort() argument
830 if (!npcm_i2c_is_master(bus)) in npcm_i2c_master_abort()
833 npcm_i2c_eob_int(bus, true); in npcm_i2c_master_abort()
834 npcm_i2c_master_stop(bus); in npcm_i2c_master_abort()
835 npcm_i2c_clear_master_status(bus); in npcm_i2c_master_abort()
839 static u8 npcm_i2c_get_slave_addr(struct npcm_i2c *bus, enum i2c_addr addr_type) in npcm_i2c_get_slave_addr() argument
845 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_get_slave_addr()
847 slave_add = ioread8(bus->reg + npcm_i2caddr[(int)addr_type]); in npcm_i2c_get_slave_addr()
850 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_get_slave_addr()
855 static int npcm_i2c_remove_slave_addr(struct npcm_i2c *bus, u8 slave_add) in npcm_i2c_remove_slave_addr() argument
861 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_remove_slave_addr()
863 if (ioread8(bus->reg + npcm_i2caddr[i]) == slave_add) in npcm_i2c_remove_slave_addr()
864 iowrite8(0, bus->reg + npcm_i2caddr[i]); in npcm_i2c_remove_slave_addr()
866 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_remove_slave_addr()
870 static void npcm_i2c_write_fifo_slave(struct npcm_i2c *bus, u16 max_bytes) in npcm_i2c_write_fifo_slave() argument
876 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_write_fifo_slave()
877 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_write_fifo_slave()
878 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_write_fifo_slave()
879 while (max_bytes-- && I2C_HW_FIFO_SIZE != npcm_i2c_fifo_usage(bus)) { in npcm_i2c_write_fifo_slave()
880 if (bus->slv_wr_size <= 0) in npcm_i2c_write_fifo_slave()
882 bus->slv_wr_ind = bus->slv_wr_ind % I2C_HW_FIFO_SIZE; in npcm_i2c_write_fifo_slave()
883 npcm_i2c_wr_byte(bus, bus->slv_wr_buf[bus->slv_wr_ind]); in npcm_i2c_write_fifo_slave()
884 bus->slv_wr_ind++; in npcm_i2c_write_fifo_slave()
885 bus->slv_wr_ind = bus->slv_wr_ind % I2C_HW_FIFO_SIZE; in npcm_i2c_write_fifo_slave()
886 bus->slv_wr_size--; in npcm_i2c_write_fifo_slave()
890 static void npcm_i2c_read_fifo_slave(struct npcm_i2c *bus, u8 bytes_in_fifo) in npcm_i2c_read_fifo_slave() argument
894 if (!bus->slave) in npcm_i2c_read_fifo_slave()
898 data = npcm_i2c_rd_byte(bus); in npcm_i2c_read_fifo_slave()
900 bus->slv_rd_ind = bus->slv_rd_ind % I2C_HW_FIFO_SIZE; in npcm_i2c_read_fifo_slave()
901 bus->slv_rd_buf[bus->slv_rd_ind] = data; in npcm_i2c_read_fifo_slave()
902 bus->slv_rd_ind++; in npcm_i2c_read_fifo_slave()
905 if (bus->slv_rd_ind == 1 && bus->read_block_use) in npcm_i2c_read_fifo_slave()
906 bus->slv_rd_size = data + bus->PEC_use + 1; in npcm_i2c_read_fifo_slave()
910 static int npcm_i2c_slave_get_wr_buf(struct npcm_i2c *bus) in npcm_i2c_slave_get_wr_buf() argument
915 int ret = bus->slv_wr_ind; in npcm_i2c_slave_get_wr_buf()
919 if (bus->slv_wr_size >= I2C_HW_FIFO_SIZE) in npcm_i2c_slave_get_wr_buf()
921 i2c_slave_event(bus->slave, I2C_SLAVE_READ_REQUESTED, &value); in npcm_i2c_slave_get_wr_buf()
922 ind = (bus->slv_wr_ind + bus->slv_wr_size) % I2C_HW_FIFO_SIZE; in npcm_i2c_slave_get_wr_buf()
923 bus->slv_wr_buf[ind] = value; in npcm_i2c_slave_get_wr_buf()
924 bus->slv_wr_size++; in npcm_i2c_slave_get_wr_buf()
925 i2c_slave_event(bus->slave, I2C_SLAVE_READ_PROCESSED, &value); in npcm_i2c_slave_get_wr_buf()
930 static void npcm_i2c_slave_send_rd_buf(struct npcm_i2c *bus) in npcm_i2c_slave_send_rd_buf() argument
934 for (i = 0; i < bus->slv_rd_ind; i++) in npcm_i2c_slave_send_rd_buf()
935 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_RECEIVED, in npcm_i2c_slave_send_rd_buf()
936 &bus->slv_rd_buf[i]); in npcm_i2c_slave_send_rd_buf()
941 if (bus->slv_rd_ind) { in npcm_i2c_slave_send_rd_buf()
942 bus->slv_wr_size = 0; in npcm_i2c_slave_send_rd_buf()
943 bus->slv_wr_ind = 0; in npcm_i2c_slave_send_rd_buf()
946 bus->slv_rd_ind = 0; in npcm_i2c_slave_send_rd_buf()
947 bus->slv_rd_size = bus->adap.quirks->max_read_len; in npcm_i2c_slave_send_rd_buf()
949 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_slave_send_rd_buf()
950 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_slave_send_rd_buf()
953 static void npcm_i2c_slave_receive(struct npcm_i2c *bus, u16 nread, in npcm_i2c_slave_receive() argument
956 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_receive()
957 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_receive()
958 bus->slv_rd_size = nread; in npcm_i2c_slave_receive()
959 bus->slv_rd_ind = 0; in npcm_i2c_slave_receive()
961 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_slave_receive()
962 iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_slave_receive()
963 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_slave_receive()
964 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_slave_receive()
967 static void npcm_i2c_slave_xmit(struct npcm_i2c *bus, u16 nwrite, in npcm_i2c_slave_xmit() argument
973 bus->state = I2C_OPER_STARTED; in npcm_i2c_slave_xmit()
974 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_xmit()
977 npcm_i2c_slave_get_wr_buf(bus); in npcm_i2c_slave_xmit()
978 npcm_i2c_write_fifo_slave(bus, nwrite); in npcm_i2c_slave_xmit()
986 * to the FIFO and onward to the bus.
987 * NACK on read will be once reached to bus->adap->quirks->max_read_len.
992 static void npcm_i2c_slave_wr_buf_sync(struct npcm_i2c *bus) in npcm_i2c_slave_wr_buf_sync() argument
997 ioread8(bus->reg + NPCM_I2CTXF_STS)); in npcm_i2c_slave_wr_buf_sync()
1001 bus->slv_wr_size >= I2C_HW_FIFO_SIZE) in npcm_i2c_slave_wr_buf_sync()
1005 bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1006 bus->slv_wr_size = bus->slv_wr_size + left_in_fifo; in npcm_i2c_slave_wr_buf_sync()
1008 if (bus->slv_wr_ind < 0) in npcm_i2c_slave_wr_buf_sync()
1009 bus->slv_wr_ind += I2C_HW_FIFO_SIZE; in npcm_i2c_slave_wr_buf_sync()
1012 static void npcm_i2c_slave_rd_wr(struct npcm_i2c *bus) in npcm_i2c_slave_rd_wr() argument
1014 if (NPCM_I2CST_XMIT & ioread8(bus->reg + NPCM_I2CST)) { in npcm_i2c_slave_rd_wr()
1019 bus->operation = I2C_WRITE_OPER; in npcm_i2c_slave_rd_wr()
1020 npcm_i2c_slave_xmit(bus, bus->adap.quirks->max_write_len, in npcm_i2c_slave_rd_wr()
1021 bus->slv_wr_buf); in npcm_i2c_slave_rd_wr()
1029 bus->operation = I2C_READ_OPER; in npcm_i2c_slave_rd_wr()
1030 npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus)); in npcm_i2c_slave_rd_wr()
1031 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_slave_rd_wr()
1032 npcm_i2c_slave_send_rd_buf(bus); in npcm_i2c_slave_rd_wr()
1033 npcm_i2c_slave_receive(bus, bus->adap.quirks->max_read_len, in npcm_i2c_slave_rd_wr()
1034 bus->slv_rd_buf); in npcm_i2c_slave_rd_wr()
1038 static irqreturn_t npcm_i2c_int_slave_handler(struct npcm_i2c *bus) in npcm_i2c_int_slave_handler() argument
1042 u8 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1046 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_int_slave_handler()
1047 npcm_i2c_slave_wr_buf_sync(bus); in npcm_i2c_int_slave_handler()
1048 if (bus->fifo_use) in npcm_i2c_int_slave_handler()
1051 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1054 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1055 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1056 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1062 iowrite8(NPCM_I2CST_NEGACK, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1067 /* Slave mode: a Bus Error (BER) has been identified */ in npcm_i2c_int_slave_handler()
1070 * Check whether bus arbitration or Start or Stop during data in npcm_i2c_int_slave_handler()
1071 * xfer bus arbitration problem should not result in recovery in npcm_i2c_int_slave_handler()
1073 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_int_slave_handler()
1075 /* wait for bus busy before clear fifo */ in npcm_i2c_int_slave_handler()
1076 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1078 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1084 if (completion_done(&bus->cmd_complete) == false) { in npcm_i2c_int_slave_handler()
1085 bus->cmd_err = -EIO; in npcm_i2c_int_slave_handler()
1086 complete(&bus->cmd_complete); in npcm_i2c_int_slave_handler()
1088 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1089 iowrite8(NPCM_I2CST_BER, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1095 u8 bytes_in_fifo = npcm_i2c_fifo_usage(bus); in npcm_i2c_int_slave_handler()
1097 bus->stop_ind = I2C_SLAVE_DONE_IND; in npcm_i2c_int_slave_handler()
1099 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1100 npcm_i2c_read_fifo_slave(bus, bytes_in_fifo); in npcm_i2c_int_slave_handler()
1103 npcm_i2c_slave_send_rd_buf(bus); in npcm_i2c_int_slave_handler()
1106 bus->stop_ind = I2C_NO_STATUS_IND; in npcm_i2c_int_slave_handler()
1113 bus->operation = I2C_NO_OPER; in npcm_i2c_int_slave_handler()
1114 bus->own_slave_addr = 0xFF; in npcm_i2c_int_slave_handler()
1115 i2c_slave_event(bus->slave, I2C_SLAVE_STOP, 0); in npcm_i2c_int_slave_handler()
1116 iowrite8(NPCM_I2CST_SLVSTP, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1117 if (bus->fifo_use) { in npcm_i2c_int_slave_handler()
1118 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_int_slave_handler()
1119 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_int_slave_handler()
1120 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_int_slave_handler()
1123 bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1125 bus->state = I2C_IDLE; in npcm_i2c_int_slave_handler()
1130 if (bus->fifo_use && FIELD_GET(NPCM_I2CFIF_CTS_SLVRSTR, in npcm_i2c_int_slave_handler()
1131 ioread8(bus->reg + NPCM_I2CFIF_CTS))) { in npcm_i2c_int_slave_handler()
1132 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1133 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1134 if (bus->operation == I2C_READ_OPER) in npcm_i2c_int_slave_handler()
1135 npcm_i2c_read_fifo_slave(bus, npcm_i2c_fifo_usage(bus)); in npcm_i2c_int_slave_handler()
1136 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1137 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1140 iowrite8(val, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_int_slave_handler()
1141 npcm_i2c_slave_rd_wr(bus); in npcm_i2c_int_slave_handler()
1150 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_int_slave_handler()
1151 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_int_slave_handler()
1152 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_int_slave_handler()
1153 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_int_slave_handler()
1154 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_int_slave_handler()
1155 iowrite8(I2C_HW_FIFO_SIZE, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_int_slave_handler()
1157 bus->operation = I2C_WRITE_OPER; in npcm_i2c_int_slave_handler()
1159 i2c_slave_event(bus->slave, I2C_SLAVE_WRITE_REQUESTED, in npcm_i2c_int_slave_handler()
1161 bus->operation = I2C_READ_OPER; in npcm_i2c_int_slave_handler()
1163 if (bus->own_slave_addr == 0xFF) { in npcm_i2c_int_slave_handler()
1165 val = ioread8(bus->reg + NPCM_I2CCST); in npcm_i2c_int_slave_handler()
1172 i2ccst3 = ioread8(bus->reg + NPCM_I2CCST3); in npcm_i2c_int_slave_handler()
1173 i2ccst2 = ioread8(bus->reg + NPCM_I2CCST2); in npcm_i2c_int_slave_handler()
1184 addr = npcm_i2c_get_slave_addr(bus, eaddr); in npcm_i2c_int_slave_handler()
1186 bus->own_slave_addr = addr; in npcm_i2c_int_slave_handler()
1187 if (bus->PEC_mask & BIT(info)) in npcm_i2c_int_slave_handler()
1188 bus->PEC_use = true; in npcm_i2c_int_slave_handler()
1190 bus->PEC_use = false; in npcm_i2c_int_slave_handler()
1193 bus->own_slave_addr = 0; in npcm_i2c_int_slave_handler()
1195 bus->own_slave_addr = 0x61; in npcm_i2c_int_slave_handler()
1206 if ((bus->state == I2C_OPER_STARTED && in npcm_i2c_int_slave_handler()
1207 bus->operation == I2C_READ_OPER && in npcm_i2c_int_slave_handler()
1208 bus->stop_ind == I2C_SLAVE_XMIT_IND) || in npcm_i2c_int_slave_handler()
1209 bus->stop_ind == I2C_SLAVE_RCV_IND) { in npcm_i2c_int_slave_handler()
1211 bus->stop_ind = I2C_SLAVE_RESTART_IND; in npcm_i2c_int_slave_handler()
1216 bus->stop_ind = I2C_SLAVE_XMIT_IND; in npcm_i2c_int_slave_handler()
1218 bus->stop_ind = I2C_SLAVE_RCV_IND; in npcm_i2c_int_slave_handler()
1219 bus->state = I2C_SLAVE_MATCH; in npcm_i2c_int_slave_handler()
1220 npcm_i2c_slave_rd_wr(bus); in npcm_i2c_int_slave_handler()
1221 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1227 (bus->fifo_use && in npcm_i2c_int_slave_handler()
1228 (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) { in npcm_i2c_int_slave_handler()
1229 npcm_i2c_slave_rd_wr(bus); in npcm_i2c_int_slave_handler()
1230 iowrite8(NPCM_I2CST_SDAST, bus->reg + NPCM_I2CST); in npcm_i2c_int_slave_handler()
1240 struct npcm_i2c *bus = i2c_get_adapdata(client->adapter); in npcm_i2c_reg_slave() local
1242 bus->slave = client; in npcm_i2c_reg_slave()
1244 if (!bus->slave) in npcm_i2c_reg_slave()
1250 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1252 npcm_i2c_init_params(bus); in npcm_i2c_reg_slave()
1253 bus->slv_rd_size = 0; in npcm_i2c_reg_slave()
1254 bus->slv_wr_size = 0; in npcm_i2c_reg_slave()
1255 bus->slv_rd_ind = 0; in npcm_i2c_reg_slave()
1256 bus->slv_wr_ind = 0; in npcm_i2c_reg_slave()
1258 bus->PEC_use = true; in npcm_i2c_reg_slave()
1260 dev_info(bus->dev, "i2c%d register slave SA=0x%x, PEC=%d\n", bus->num, in npcm_i2c_reg_slave()
1261 client->addr, bus->PEC_use); in npcm_i2c_reg_slave()
1263 npcm_i2c_slave_enable(bus, I2C_SLAVE_ADDR1, client->addr, true); in npcm_i2c_reg_slave()
1264 npcm_i2c_clear_fifo_int(bus); in npcm_i2c_reg_slave()
1265 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_reg_slave()
1266 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_reg_slave()
1267 npcm_i2c_slave_int_enable(bus, true); in npcm_i2c_reg_slave()
1269 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_reg_slave()
1275 struct npcm_i2c *bus = client->adapter->algo_data; in npcm_i2c_unreg_slave() local
1278 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1279 if (!bus->slave) { in npcm_i2c_unreg_slave()
1280 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1283 npcm_i2c_slave_int_enable(bus, false); in npcm_i2c_unreg_slave()
1284 npcm_i2c_remove_slave_addr(bus, client->addr); in npcm_i2c_unreg_slave()
1285 bus->slave = NULL; in npcm_i2c_unreg_slave()
1286 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_unreg_slave()
1291 static void npcm_i2c_master_fifo_read(struct npcm_i2c *bus) in npcm_i2c_master_fifo_read() argument
1297 fifo_bytes = npcm_i2c_fifo_usage(bus); in npcm_i2c_master_fifo_read()
1298 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1312 bus->state = I2C_STOP_PENDING; in npcm_i2c_master_fifo_read()
1313 bus->stop_ind = ind; in npcm_i2c_master_fifo_read()
1314 npcm_i2c_eob_int(bus, true); in npcm_i2c_master_fifo_read()
1316 npcm_i2c_master_stop(bus); in npcm_i2c_master_fifo_read()
1317 npcm_i2c_read_fifo(bus, fifo_bytes); in npcm_i2c_master_fifo_read()
1319 npcm_i2c_read_fifo(bus, fifo_bytes); in npcm_i2c_master_fifo_read()
1320 rcount = bus->rd_size - bus->rd_ind; in npcm_i2c_master_fifo_read()
1321 npcm_i2c_set_fifo(bus, rcount, -1); in npcm_i2c_master_fifo_read()
1325 static void npcm_i2c_irq_master_handler_write(struct npcm_i2c *bus) in npcm_i2c_irq_master_handler_write() argument
1329 if (bus->fifo_use) in npcm_i2c_irq_master_handler_write()
1330 npcm_i2c_clear_tx_fifo(bus); /* clear the TX fifo status bit */ in npcm_i2c_irq_master_handler_write()
1333 if (bus->wr_ind == bus->wr_size) { in npcm_i2c_irq_master_handler_write()
1334 if (bus->fifo_use && npcm_i2c_fifo_usage(bus) > 0) in npcm_i2c_irq_master_handler_write()
1344 if (bus->rd_size == 0) { in npcm_i2c_irq_master_handler_write()
1346 npcm_i2c_eob_int(bus, true); in npcm_i2c_irq_master_handler_write()
1347 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_write()
1348 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_master_handler_write()
1349 npcm_i2c_master_stop(bus); in npcm_i2c_irq_master_handler_write()
1351 npcm_i2c_wr_byte(bus, 0xFF); in npcm_i2c_irq_master_handler_write()
1355 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_master_handler_write()
1357 npcm_i2c_master_start(bus); in npcm_i2c_irq_master_handler_write()
1365 if (bus->rd_size == 1) in npcm_i2c_irq_master_handler_write()
1366 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_irq_master_handler_write()
1369 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_master_handler_write()
1371 npcm_i2c_wr_byte(bus, bus->dest_addr | 0x1); in npcm_i2c_irq_master_handler_write()
1375 if (!bus->fifo_use || bus->wr_size == 1) { in npcm_i2c_irq_master_handler_write()
1376 npcm_i2c_wr_byte(bus, bus->wr_buf[bus->wr_ind++]); in npcm_i2c_irq_master_handler_write()
1378 wcount = bus->wr_size - bus->wr_ind; in npcm_i2c_irq_master_handler_write()
1379 npcm_i2c_set_fifo(bus, -1, wcount); in npcm_i2c_irq_master_handler_write()
1381 npcm_i2c_write_to_fifo_master(bus, wcount); in npcm_i2c_irq_master_handler_write()
1386 static void npcm_i2c_irq_master_handler_read(struct npcm_i2c *bus) in npcm_i2c_irq_master_handler_read() argument
1392 block_extra_bytes_size = bus->read_block_use + bus->PEC_use; in npcm_i2c_irq_master_handler_read()
1398 if (bus->rd_ind == 0) { /* first byte handling: */ in npcm_i2c_irq_master_handler_read()
1399 if (bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1401 data = npcm_i2c_rd_byte(bus); in npcm_i2c_irq_master_handler_read()
1403 bus->rd_size = data + block_extra_bytes_size; in npcm_i2c_irq_master_handler_read()
1404 bus->rd_buf[bus->rd_ind++] = data; in npcm_i2c_irq_master_handler_read()
1407 if (bus->fifo_use) { in npcm_i2c_irq_master_handler_read()
1408 data = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1410 iowrite8(data, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_master_handler_read()
1413 npcm_i2c_set_fifo(bus, bus->rd_size - 1, -1); in npcm_i2c_irq_master_handler_read()
1414 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_irq_master_handler_read()
1416 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_irq_master_handler_read()
1417 npcm_i2c_master_fifo_read(bus); in npcm_i2c_irq_master_handler_read()
1420 if (bus->rd_size == block_extra_bytes_size && in npcm_i2c_irq_master_handler_read()
1421 bus->read_block_use) { in npcm_i2c_irq_master_handler_read()
1422 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_master_handler_read()
1423 bus->stop_ind = I2C_BLOCK_BYTES_ERR_IND; in npcm_i2c_irq_master_handler_read()
1424 bus->cmd_err = -EIO; in npcm_i2c_irq_master_handler_read()
1425 npcm_i2c_eob_int(bus, true); in npcm_i2c_irq_master_handler_read()
1426 npcm_i2c_master_stop(bus); in npcm_i2c_irq_master_handler_read()
1427 npcm_i2c_read_fifo(bus, npcm_i2c_fifo_usage(bus)); in npcm_i2c_irq_master_handler_read()
1429 npcm_i2c_master_fifo_read(bus); in npcm_i2c_irq_master_handler_read()
1434 static void npcm_i2c_irq_handle_nmatch(struct npcm_i2c *bus) in npcm_i2c_irq_handle_nmatch() argument
1436 iowrite8(NPCM_I2CST_NMATCH, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_nmatch()
1437 npcm_i2c_nack(bus); in npcm_i2c_irq_handle_nmatch()
1438 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_nmatch()
1439 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_nmatch()
1443 static void npcm_i2c_irq_handle_nack(struct npcm_i2c *bus) in npcm_i2c_irq_handle_nack() argument
1447 if (bus->nack_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_nack()
1448 bus->nack_cnt++; in npcm_i2c_irq_handle_nack()
1450 if (bus->fifo_use) { in npcm_i2c_irq_handle_nack()
1455 if (bus->operation == I2C_WRITE_OPER) in npcm_i2c_irq_handle_nack()
1456 bus->wr_ind -= npcm_i2c_fifo_usage(bus); in npcm_i2c_irq_handle_nack()
1459 iowrite8(NPCM_I2CFIF_CTS_CLR_FIFO, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_nack()
1463 bus->stop_ind = I2C_NACK_IND; in npcm_i2c_irq_handle_nack()
1465 if (npcm_i2c_is_master(bus)) { in npcm_i2c_irq_handle_nack()
1467 npcm_i2c_eob_int(bus, false); in npcm_i2c_irq_handle_nack()
1468 npcm_i2c_master_stop(bus); in npcm_i2c_irq_handle_nack()
1471 * The bus is released from stall only after the SW clears in npcm_i2c_irq_handle_nack()
1474 npcm_i2c_clear_master_status(bus); in npcm_i2c_irq_handle_nack()
1475 readx_poll_timeout_atomic(ioread8, bus->reg + NPCM_I2CCST, val, in npcm_i2c_irq_handle_nack()
1478 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_nack()
1482 * In such case, the bus is released from stall only after the in npcm_i2c_irq_handle_nack()
1485 npcm_i2c_callback(bus, bus->stop_ind, bus->wr_ind); in npcm_i2c_irq_handle_nack()
1488 /* Master mode: a Bus Error has been identified */
1489 static void npcm_i2c_irq_handle_ber(struct npcm_i2c *bus) in npcm_i2c_irq_handle_ber() argument
1491 if (bus->ber_cnt < ULLONG_MAX) in npcm_i2c_irq_handle_ber()
1492 bus->ber_cnt++; in npcm_i2c_irq_handle_ber()
1493 bus->stop_ind = I2C_BUS_ERR_IND; in npcm_i2c_irq_handle_ber()
1494 if (npcm_i2c_is_master(bus)) { in npcm_i2c_irq_handle_ber()
1495 npcm_i2c_master_abort(bus); in npcm_i2c_irq_handle_ber()
1497 npcm_i2c_clear_master_status(bus); in npcm_i2c_irq_handle_ber()
1499 /* Clear BB (BUS BUSY) bit */ in npcm_i2c_irq_handle_ber()
1500 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_irq_handle_ber()
1502 bus->cmd_err = -EAGAIN; in npcm_i2c_irq_handle_ber()
1503 npcm_i2c_callback(bus, bus->stop_ind, npcm_i2c_get_index(bus)); in npcm_i2c_irq_handle_ber()
1505 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_ber()
1509 static void npcm_i2c_irq_handle_eob(struct npcm_i2c *bus) in npcm_i2c_irq_handle_eob() argument
1511 npcm_i2c_eob_int(bus, false); in npcm_i2c_irq_handle_eob()
1512 bus->state = I2C_IDLE; in npcm_i2c_irq_handle_eob()
1513 npcm_i2c_callback(bus, bus->stop_ind, bus->rd_ind); in npcm_i2c_irq_handle_eob()
1517 static void npcm_i2c_irq_handle_stall_after_start(struct npcm_i2c *bus) in npcm_i2c_irq_handle_stall_after_start() argument
1519 if (npcm_i2c_is_quick(bus)) { in npcm_i2c_irq_handle_stall_after_start()
1520 bus->state = I2C_STOP_PENDING; in npcm_i2c_irq_handle_stall_after_start()
1521 bus->stop_ind = I2C_MASTER_DONE_IND; in npcm_i2c_irq_handle_stall_after_start()
1522 npcm_i2c_eob_int(bus, true); in npcm_i2c_irq_handle_stall_after_start()
1523 npcm_i2c_master_stop(bus); in npcm_i2c_irq_handle_stall_after_start()
1524 } else if ((bus->rd_size == 1) && !bus->read_block_use) { in npcm_i2c_irq_handle_stall_after_start()
1529 npcm_i2c_nack(bus); in npcm_i2c_irq_handle_stall_after_start()
1533 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_irq_handle_stall_after_start()
1536 iowrite8(NPCM_I2CST_STASTR, bus->reg + NPCM_I2CST); in npcm_i2c_irq_handle_stall_after_start()
1540 static void npcm_i2c_irq_handle_sda(struct npcm_i2c *bus, u8 i2cst) in npcm_i2c_irq_handle_sda() argument
1544 if (!npcm_i2c_is_master(bus)) in npcm_i2c_irq_handle_sda()
1547 if (bus->state == I2C_IDLE) { in npcm_i2c_irq_handle_sda()
1548 bus->stop_ind = I2C_WAKE_UP_IND; in npcm_i2c_irq_handle_sda()
1550 if (npcm_i2c_is_quick(bus) || bus->read_block_use) in npcm_i2c_irq_handle_sda()
1555 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_irq_handle_sda()
1557 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_irq_handle_sda()
1565 if (bus->wr_size == 0 && bus->rd_size == 1) in npcm_i2c_irq_handle_sda()
1566 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_irq_handle_sda()
1571 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_irq_handle_sda()
1573 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1578 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1582 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_irq_handle_sda()
1591 if (bus->wr_size) in npcm_i2c_irq_handle_sda()
1592 npcm_i2c_set_fifo(bus, -1, bus->wr_size); in npcm_i2c_irq_handle_sda()
1594 npcm_i2c_set_fifo(bus, bus->rd_size, -1); in npcm_i2c_irq_handle_sda()
1596 bus->state = I2C_OPER_STARTED; in npcm_i2c_irq_handle_sda()
1598 if (npcm_i2c_is_quick(bus) || bus->wr_size) in npcm_i2c_irq_handle_sda()
1599 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_irq_handle_sda()
1601 npcm_i2c_wr_byte(bus, bus->dest_addr | BIT(0)); in npcm_i2c_irq_handle_sda()
1605 bus->operation = I2C_WRITE_OPER; in npcm_i2c_irq_handle_sda()
1606 npcm_i2c_irq_master_handler_write(bus); in npcm_i2c_irq_handle_sda()
1608 bus->operation = I2C_READ_OPER; in npcm_i2c_irq_handle_sda()
1609 npcm_i2c_irq_master_handler_read(bus); in npcm_i2c_irq_handle_sda()
1614 static int npcm_i2c_int_master_handler(struct npcm_i2c *bus) in npcm_i2c_int_master_handler() argument
1619 i2cst = ioread8(bus->reg + NPCM_I2CST); in npcm_i2c_int_master_handler()
1622 npcm_i2c_irq_handle_nmatch(bus); in npcm_i2c_int_master_handler()
1627 npcm_i2c_irq_handle_nack(bus); in npcm_i2c_int_master_handler()
1631 /* Master mode: a Bus Error has been identified */ in npcm_i2c_int_master_handler()
1633 npcm_i2c_irq_handle_ber(bus); in npcm_i2c_int_master_handler()
1639 ioread8(bus->reg + NPCM_I2CCTL1)) == 1) && in npcm_i2c_int_master_handler()
1641 ioread8(bus->reg + NPCM_I2CCST3)))) { in npcm_i2c_int_master_handler()
1642 npcm_i2c_irq_handle_eob(bus); in npcm_i2c_int_master_handler()
1648 npcm_i2c_irq_handle_stall_after_start(bus); in npcm_i2c_int_master_handler()
1654 (bus->fifo_use && in npcm_i2c_int_master_handler()
1655 (npcm_i2c_tx_fifo_empty(bus) || npcm_i2c_rx_fifo_full(bus)))) { in npcm_i2c_int_master_handler()
1656 npcm_i2c_irq_handle_sda(bus, i2cst); in npcm_i2c_int_master_handler()
1670 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_recovery_tgclk() local
1675 dev_dbg(bus->dev, "bus%d recovery skipped, bus not stuck", in npcm_i2c_recovery_tgclk()
1676 bus->num); in npcm_i2c_recovery_tgclk()
1677 npcm_i2c_reset(bus); in npcm_i2c_recovery_tgclk()
1681 npcm_i2c_int_enable(bus, false); in npcm_i2c_recovery_tgclk()
1682 npcm_i2c_disable(bus); in npcm_i2c_recovery_tgclk()
1683 npcm_i2c_enable(bus); in npcm_i2c_recovery_tgclk()
1684 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1685 npcm_i2c_clear_tx_fifo(bus); in npcm_i2c_recovery_tgclk()
1686 npcm_i2c_clear_rx_fifo(bus); in npcm_i2c_recovery_tgclk()
1687 iowrite8(0, bus->reg + NPCM_I2CRXF_CTL); in npcm_i2c_recovery_tgclk()
1688 iowrite8(0, bus->reg + NPCM_I2CTXF_CTL); in npcm_i2c_recovery_tgclk()
1689 npcm_i2c_stall_after_start(bus, false); in npcm_i2c_recovery_tgclk()
1692 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_recovery_tgclk()
1695 fif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1698 iowrite8(fif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_recovery_tgclk()
1699 npcm_i2c_set_fifo(bus, -1, 0); in npcm_i2c_recovery_tgclk()
1704 iowrite8(NPCM_I2CCST_TGSCL, bus->reg + NPCM_I2CCST); in npcm_i2c_recovery_tgclk()
1716 npcm_i2c_wr_byte(bus, bus->dest_addr); in npcm_i2c_recovery_tgclk()
1717 npcm_i2c_master_start(bus); in npcm_i2c_recovery_tgclk()
1722 if (npcm_i2c_is_master(bus) > 0) { in npcm_i2c_recovery_tgclk()
1724 npcm_i2c_master_stop(bus); in npcm_i2c_recovery_tgclk()
1728 npcm_i2c_reset(bus); in npcm_i2c_recovery_tgclk()
1729 npcm_i2c_int_enable(bus, true); in npcm_i2c_recovery_tgclk()
1736 if (bus->rec_fail_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1737 bus->rec_fail_cnt++; in npcm_i2c_recovery_tgclk()
1739 if (bus->rec_succ_cnt < ULLONG_MAX) in npcm_i2c_recovery_tgclk()
1740 bus->rec_succ_cnt++; in npcm_i2c_recovery_tgclk()
1748 struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); in npcm_i2c_recovery_init() local
1749 struct i2c_bus_recovery_info *rinfo = &bus->rinfo; in npcm_i2c_recovery_init()
1772 * and bus frequency.
1773 * 100kHz bus requires tSCL = 4 * SCLFRQ * tCLK. LT and HT are simetric.
1774 * 400kHz bus requires assymetric HT and LT. A different equation is recomended
1778 static int npcm_i2c_init_clk(struct npcm_i2c *bus, u32 bus_freq_hz) in npcm_i2c_init_clk() argument
1789 src_clk_khz = bus->apb_clk / 1000; in npcm_i2c_init_clk()
1791 bus->bus_freq = bus_freq_hz; in npcm_i2c_init_clk()
1872 bus->reg + NPCM_I2CCTL2); in npcm_i2c_init_clk()
1876 bus->reg + NPCM_I2CCTL3); in npcm_i2c_init_clk()
1879 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_init_clk()
1887 iowrite8(k1 / 2, bus->reg + NPCM_I2CSCLLT); in npcm_i2c_init_clk()
1888 iowrite8(k2 / 2, bus->reg + NPCM_I2CSCLHT); in npcm_i2c_init_clk()
1890 iowrite8(dbnct, bus->reg + NPCM_I2CCTL5); in npcm_i2c_init_clk()
1893 iowrite8(hldt, bus->reg + NPCM_I2CCTL4); in npcm_i2c_init_clk()
1896 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_init_clk()
1901 static int npcm_i2c_init_module(struct npcm_i2c *bus, enum i2c_mode mode, in npcm_i2c_init_module() argument
1908 if ((bus->state != I2C_DISABLE && bus->state != I2C_IDLE) || in npcm_i2c_init_module()
1912 npcm_i2c_disable(bus); in npcm_i2c_init_module()
1915 if (FIELD_GET(I2C_VER_FIFO_EN, ioread8(bus->reg + I2C_VER))) { in npcm_i2c_init_module()
1916 bus->fifo_use = true; in npcm_i2c_init_module()
1917 npcm_i2c_select_bank(bus, I2C_BANK_0); in npcm_i2c_init_module()
1918 val = ioread8(bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1920 iowrite8(val, bus->reg + NPCM_I2CFIF_CTL); in npcm_i2c_init_module()
1921 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_init_module()
1923 bus->fifo_use = false; in npcm_i2c_init_module()
1927 ret = npcm_i2c_init_clk(bus, bus_freq_hz); in npcm_i2c_init_module()
1929 dev_err(bus->dev, "npcm_i2c_init_clk failed\n"); in npcm_i2c_init_module()
1934 npcm_i2c_enable(bus); in npcm_i2c_init_module()
1935 bus->state = I2C_IDLE; in npcm_i2c_init_module()
1936 val = ioread8(bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1938 iowrite8(val, bus->reg + NPCM_I2CCTL1); in npcm_i2c_init_module()
1940 npcm_i2c_int_enable(bus, true); in npcm_i2c_init_module()
1942 npcm_i2c_reset(bus); in npcm_i2c_init_module()
1947 static int __npcm_i2c_init(struct npcm_i2c *bus, struct platform_device *pdev) in __npcm_i2c_init() argument
1953 bus->state = I2C_DISABLE; in __npcm_i2c_init()
1954 bus->master_or_slave = I2C_SLAVE; in __npcm_i2c_init()
1955 bus->int_time_stamp = 0; in __npcm_i2c_init()
1957 bus->slave = NULL; in __npcm_i2c_init()
1967 ret = npcm_i2c_init_module(bus, I2C_MASTER, clk_freq_hz); in __npcm_i2c_init()
1978 struct npcm_i2c *bus = dev_id; in npcm_i2c_bus_irq() local
1980 if (npcm_i2c_is_master(bus)) in npcm_i2c_bus_irq()
1981 bus->master_or_slave = I2C_MASTER; in npcm_i2c_bus_irq()
1983 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_bus_irq()
1984 bus->int_time_stamp = jiffies; in npcm_i2c_bus_irq()
1985 if (!npcm_i2c_int_master_handler(bus)) in npcm_i2c_bus_irq()
1989 if (bus->slave) { in npcm_i2c_bus_irq()
1990 bus->master_or_slave = I2C_SLAVE; in npcm_i2c_bus_irq()
1991 return npcm_i2c_int_slave_handler(bus); in npcm_i2c_bus_irq()
1997 static bool npcm_i2c_master_start_xmit(struct npcm_i2c *bus, in npcm_i2c_master_start_xmit() argument
2002 if (bus->state != I2C_IDLE) { in npcm_i2c_master_start_xmit()
2003 bus->cmd_err = -EBUSY; in npcm_i2c_master_start_xmit()
2006 bus->dest_addr = slave_addr << 1; in npcm_i2c_master_start_xmit()
2007 bus->wr_buf = write_data; in npcm_i2c_master_start_xmit()
2008 bus->wr_size = nwrite; in npcm_i2c_master_start_xmit()
2009 bus->wr_ind = 0; in npcm_i2c_master_start_xmit()
2010 bus->rd_buf = read_data; in npcm_i2c_master_start_xmit()
2011 bus->rd_size = nread; in npcm_i2c_master_start_xmit()
2012 bus->rd_ind = 0; in npcm_i2c_master_start_xmit()
2013 bus->PEC_use = 0; in npcm_i2c_master_start_xmit()
2017 bus->PEC_use = use_PEC; in npcm_i2c_master_start_xmit()
2019 bus->read_block_use = use_read_block; in npcm_i2c_master_start_xmit()
2021 bus->operation = I2C_READ_OPER; in npcm_i2c_master_start_xmit()
2023 bus->operation = I2C_WRITE_OPER; in npcm_i2c_master_start_xmit()
2024 if (bus->fifo_use) { in npcm_i2c_master_start_xmit()
2027 npcm_i2c_select_bank(bus, I2C_BANK_1); in npcm_i2c_master_start_xmit()
2029 i2cfif_cts = ioread8(bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2032 iowrite8(i2cfif_cts, bus->reg + NPCM_I2CFIF_CTS); in npcm_i2c_master_start_xmit()
2035 bus->state = I2C_IDLE; in npcm_i2c_master_start_xmit()
2036 npcm_i2c_stall_after_start(bus, true); in npcm_i2c_master_start_xmit()
2037 npcm_i2c_master_start(bus); in npcm_i2c_master_start_xmit()
2044 struct npcm_i2c *bus = container_of(adap, struct npcm_i2c, adap); in npcm_i2c_master_xfer() local
2057 if (bus->state == I2C_DISABLE) { in npcm_i2c_master_xfer()
2058 dev_err(bus->dev, "I2C%d module is disabled", bus->num); in npcm_i2c_master_xfer()
2101 timeout_usec = (2 * 9 * USEC_PER_SEC / bus->bus_freq) * (2 + nread + nwrite); in npcm_i2c_master_xfer()
2104 dev_err(bus->dev, "i2c%d buffer too big\n", bus->num); in npcm_i2c_master_xfer()
2111 * we must clear slave address immediately when the bus is not in npcm_i2c_master_xfer()
2115 spin_lock_irqsave(&bus->lock, flags); in npcm_i2c_master_xfer()
2116 bus_busy = ioread8(bus->reg + NPCM_I2CCST) & NPCM_I2CCST_BB; in npcm_i2c_master_xfer()
2118 if (!bus_busy && bus->slave) in npcm_i2c_master_xfer()
2119 iowrite8((bus->slave->addr & 0x7F), in npcm_i2c_master_xfer()
2120 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2122 spin_unlock_irqrestore(&bus->lock, flags); in npcm_i2c_master_xfer()
2127 iowrite8(NPCM_I2CCST_BB, bus->reg + NPCM_I2CCST); in npcm_i2c_master_xfer()
2128 npcm_i2c_reset(bus); in npcm_i2c_master_xfer()
2133 npcm_i2c_init_params(bus); in npcm_i2c_master_xfer()
2134 bus->dest_addr = slave_addr; in npcm_i2c_master_xfer()
2135 bus->msgs = msgs; in npcm_i2c_master_xfer()
2136 bus->msgs_num = num; in npcm_i2c_master_xfer()
2137 bus->cmd_err = 0; in npcm_i2c_master_xfer()
2138 bus->read_block_use = read_block; in npcm_i2c_master_xfer()
2140 reinit_completion(&bus->cmd_complete); in npcm_i2c_master_xfer()
2141 if (!npcm_i2c_master_start_xmit(bus, slave_addr, nwrite, nread, in npcm_i2c_master_xfer()
2147 time_left = wait_for_completion_timeout(&bus->cmd_complete, in npcm_i2c_master_xfer()
2151 if (bus->timeout_cnt < ULLONG_MAX) in npcm_i2c_master_xfer()
2152 bus->timeout_cnt++; in npcm_i2c_master_xfer()
2153 if (bus->master_or_slave == I2C_MASTER) { in npcm_i2c_master_xfer()
2155 bus->cmd_err = -EIO; in npcm_i2c_master_xfer()
2156 bus->state = I2C_IDLE; in npcm_i2c_master_xfer()
2160 ret = bus->cmd_err; in npcm_i2c_master_xfer()
2162 /* if there was BER, check if need to recover the bus: */ in npcm_i2c_master_xfer()
2163 if (bus->cmd_err == -EAGAIN) in npcm_i2c_master_xfer()
2171 if (bus->cmd_err && in npcm_i2c_master_xfer()
2172 (NPCM_I2CRXF_CTL_LAST_PEC & ioread8(bus->reg + NPCM_I2CRXF_CTL))) in npcm_i2c_master_xfer()
2173 npcm_i2c_reset(bus); in npcm_i2c_master_xfer()
2177 if (bus->slave) in npcm_i2c_master_xfer()
2178 iowrite8((bus->slave->addr & 0x7F) | NPCM_I2CADDR_SAEN, in npcm_i2c_master_xfer()
2179 bus->reg + NPCM_I2CADDR1); in npcm_i2c_master_xfer()
2181 return bus->cmd_err; in npcm_i2c_master_xfer()
2212 struct npcm_i2c *bus) in npcm_i2c_init_debugfs() argument
2221 debugfs_create_u64("ber_cnt", 0444, d, &bus->ber_cnt); in npcm_i2c_init_debugfs()
2222 debugfs_create_u64("nack_cnt", 0444, d, &bus->nack_cnt); in npcm_i2c_init_debugfs()
2223 debugfs_create_u64("rec_succ_cnt", 0444, d, &bus->rec_succ_cnt); in npcm_i2c_init_debugfs()
2224 debugfs_create_u64("rec_fail_cnt", 0444, d, &bus->rec_fail_cnt); in npcm_i2c_init_debugfs()
2225 debugfs_create_u64("timeout_cnt", 0444, d, &bus->timeout_cnt); in npcm_i2c_init_debugfs()
2227 bus->debugfs = d; in npcm_i2c_init_debugfs()
2232 struct npcm_i2c *bus; in npcm_i2c_probe_bus() local
2240 bus = devm_kzalloc(&pdev->dev, sizeof(*bus), GFP_KERNEL); in npcm_i2c_probe_bus()
2241 if (!bus) in npcm_i2c_probe_bus()
2244 bus->dev = &pdev->dev; in npcm_i2c_probe_bus()
2246 bus->num = of_alias_get_id(pdev->dev.of_node, "i2c"); in npcm_i2c_probe_bus()
2251 bus->apb_clk = clk_get_rate(i2c_clk); in npcm_i2c_probe_bus()
2262 bus->reg = devm_platform_ioremap_resource(pdev, 0); in npcm_i2c_probe_bus()
2263 if (IS_ERR(bus->reg)) in npcm_i2c_probe_bus()
2264 return PTR_ERR(bus->reg); in npcm_i2c_probe_bus()
2266 spin_lock_init(&bus->lock); in npcm_i2c_probe_bus()
2267 init_completion(&bus->cmd_complete); in npcm_i2c_probe_bus()
2269 adap = &bus->adap; in npcm_i2c_probe_bus()
2275 adap->algo_data = bus; in npcm_i2c_probe_bus()
2284 ret = devm_request_irq(bus->dev, irq, npcm_i2c_bus_irq, 0, in npcm_i2c_probe_bus()
2285 dev_name(bus->dev), bus); in npcm_i2c_probe_bus()
2289 ret = __npcm_i2c_init(bus, pdev); in npcm_i2c_probe_bus()
2295 i2c_set_adapdata(adap, bus); in npcm_i2c_probe_bus()
2297 snprintf(bus->adap.name, sizeof(bus->adap.name), "npcm_i2c_%d", in npcm_i2c_probe_bus()
2298 bus->num); in npcm_i2c_probe_bus()
2299 ret = i2c_add_numbered_adapter(&bus->adap); in npcm_i2c_probe_bus()
2303 platform_set_drvdata(pdev, bus); in npcm_i2c_probe_bus()
2304 npcm_i2c_init_debugfs(pdev, bus); in npcm_i2c_probe_bus()
2311 struct npcm_i2c *bus = platform_get_drvdata(pdev); in npcm_i2c_remove_bus() local
2313 debugfs_remove_recursive(bus->debugfs); in npcm_i2c_remove_bus()
2314 spin_lock_irqsave(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2315 npcm_i2c_disable(bus); in npcm_i2c_remove_bus()
2316 spin_unlock_irqrestore(&bus->lock, lock_flags); in npcm_i2c_remove_bus()
2317 i2c_del_adapter(&bus->adap); in npcm_i2c_remove_bus()
2354 MODULE_DESCRIPTION("Nuvoton I2C Bus Driver");