Lines Matching +full:usb3 +full:- +full:2

1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas USB3.0 Peripheral driver (USB gadget)
5 * Copyright (C) 2015-2017 Renesas Electronics Corporation
11 #include <linux/dma-mapping.h>
13 #include <linux/extcon-provider.h>
37 #define USB3_DMA_CH0_CON(n) (0x030 + ((n) - 1) * 0x10) /* n = 1 to 4 */
38 #define USB3_DMA_CH0_PRD_ADR(n) (0x034 + ((n) - 1) * 0x10) /* n = 1 to 4 */
43 #define USB3_DRD_CON(p) ((p)->is_rzv2m ? 0x400 : 0x218)
50 #define USB3_USB_OTG_STA(p) ((p)->is_rzv2m ? 0x410 : 0x268)
51 #define USB3_USB_OTG_INT_STA(p) ((p)->is_rzv2m ? 0x414 : 0x26c)
52 #define USB3_USB_OTG_INT_ENA(p) ((p)->is_rzv2m ? 0x418 : 0x270)
77 #define AXI_INT_PRDEN_CLR_STA_SHIFT(n) (16 + (n) - 1)
78 #define AXI_INT_PRDERR_STA_SHIFT(n) (0 + (n) - 1)
133 #define USB_STA_SPEED_MASK (BIT(2) | BIT(1))
134 #define USB_STA_SPEED_HS BIT(2)
162 #define USB_OTG_IDMON(p) ((p)->is_rzv2m ? BIT(0) : BIT(4))
200 #define P0_INT_STALL BIT(2)
262 #define USB3_MAX_NUM_PIPES(p) ((p)->is_rzv2m ? 16 : 6) /* This includes PIPE 0 */
266 * To avoid double-meaning of "0" (xferred 65536 bytes or received zlp if
316 struct renesas_usb3 *usb3; member
378 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
379 #define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
382 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
386 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n)) argument
387 #define usb3_for_each_ep(usb3_ep, usb3, i) \ argument
388 for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
389 (i) < (usb3)->num_usb3_eps; \
390 (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
392 #define usb3_get_dma(usb3, i) (&(usb3)->dma[i]) argument
393 #define usb3_for_each_dma(usb3, dma, i) \ argument
394 for ((i) = 0, dma = usb3_get_dma((usb3), (i)); \
396 (i)++, dma = usb3_get_dma((usb3), (i)))
404 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs) in usb3_write() argument
406 iowrite32(data, usb3->reg + offs); in usb3_write()
409 static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs) in usb3_read() argument
411 return ioread32(usb3->reg + offs); in usb3_read()
414 static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs) in usb3_set_bit() argument
416 u32 val = usb3_read(usb3, offs); in usb3_set_bit()
419 usb3_write(usb3, val, offs); in usb3_set_bit()
422 static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs) in usb3_clear_bit() argument
424 u32 val = usb3_read(usb3, offs); in usb3_clear_bit()
427 usb3_write(usb3, val, offs); in usb3_clear_bit()
430 static void usb3_drd_write(struct renesas_usb3 *usb3, u32 data, u32 offs) in usb3_drd_write() argument
434 if (usb3->is_rzv2m) in usb3_drd_write()
435 reg = usb3->drd_reg + offs - USB3_DRD_CON(usb3); in usb3_drd_write()
437 reg = usb3->reg + offs; in usb3_drd_write()
442 static u32 usb3_drd_read(struct renesas_usb3 *usb3, u32 offs) in usb3_drd_read() argument
446 if (usb3->is_rzv2m) in usb3_drd_read()
447 reg = usb3->drd_reg + offs - USB3_DRD_CON(usb3); in usb3_drd_read()
449 reg = usb3->reg + offs; in usb3_drd_read()
454 static void usb3_drd_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs) in usb3_drd_set_bit() argument
456 u32 val = usb3_drd_read(usb3, offs); in usb3_drd_set_bit()
459 usb3_drd_write(usb3, val, offs); in usb3_drd_set_bit()
462 static void usb3_drd_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs) in usb3_drd_clear_bit() argument
464 u32 val = usb3_drd_read(usb3, offs); in usb3_drd_clear_bit()
467 usb3_drd_write(usb3, val, offs); in usb3_drd_clear_bit()
470 static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask, in usb3_wait() argument
476 if ((usb3_read(usb3, reg) & mask) == expected) in usb3_wait()
481 dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n", in usb3_wait()
484 return -EBUSY; in usb3_wait()
489 struct renesas_usb3 *usb3 = container_of(work, struct renesas_usb3, in renesas_usb3_extcon_work() local
492 extcon_set_state_sync(usb3->extcon, EXTCON_USB_HOST, usb3->extcon_host); in renesas_usb3_extcon_work()
493 extcon_set_state_sync(usb3->extcon, EXTCON_USB, usb3->extcon_usb); in renesas_usb3_extcon_work()
496 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits) in usb3_enable_irq_1() argument
498 usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1); in usb3_enable_irq_1()
501 static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits) in usb3_disable_irq_1() argument
503 usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1); in usb3_disable_irq_1()
506 static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num) in usb3_enable_pipe_irq() argument
508 usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2); in usb3_enable_pipe_irq()
511 static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num) in usb3_disable_pipe_irq() argument
513 usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2); in usb3_disable_pipe_irq()
516 static bool usb3_is_host(struct renesas_usb3 *usb3) in usb3_is_host() argument
518 return !(usb3_drd_read(usb3, USB3_DRD_CON(usb3)) & DRD_CON_PERI_CON); in usb3_is_host()
521 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3) in usb3_init_axi_bridge() argument
524 usb3_write(usb3, ~0, USB3_DMA_INT_STA); in usb3_init_axi_bridge()
525 usb3_write(usb3, 0, USB3_DMA_INT_ENA); in usb3_init_axi_bridge()
526 usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA); in usb3_init_axi_bridge()
529 static void usb3_init_epc_registers(struct renesas_usb3 *usb3) in usb3_init_epc_registers() argument
531 usb3_write(usb3, ~0, USB3_USB_INT_STA_1); in usb3_init_epc_registers()
532 if (!usb3->workaround_for_vbus) in usb3_init_epc_registers()
533 usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG); in usb3_init_epc_registers()
536 static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3) in usb3_wakeup_usb2_phy() argument
538 if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND)) in usb3_wakeup_usb2_phy()
541 usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON); in usb3_wakeup_usb2_phy()
542 usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM); in usb3_wakeup_usb2_phy()
547 static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup) in usb3_usb2_pullup() argument
551 if (usb3->softconnect && pullup) in usb3_usb2_pullup()
552 usb3_set_bit(usb3, bits, USB3_USB20_CON); in usb3_usb2_pullup()
554 usb3_clear_bit(usb3, bits, USB3_USB20_CON); in usb3_usb2_pullup()
557 static void usb3_set_test_mode(struct renesas_usb3 *usb3) in usb3_set_test_mode() argument
559 u32 val = usb3_read(usb3, USB3_USB20_CON); in usb3_set_test_mode()
562 val |= USB20_CON_B2_TSTMOD(usb3->test_mode); in usb3_set_test_mode()
563 usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON); in usb3_set_test_mode()
564 if (!usb3->test_mode) in usb3_set_test_mode()
565 usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON); in usb3_set_test_mode()
568 static void usb3_start_usb2_connection(struct renesas_usb3 *usb3) in usb3_start_usb2_connection() argument
570 usb3->disabled_count++; in usb3_start_usb2_connection()
571 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_start_usb2_connection()
572 usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON); in usb3_start_usb2_connection()
573 usb3_usb2_pullup(usb3, 1); in usb3_start_usb2_connection()
576 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3) in usb3_is_usb3_phy_in_u3() argument
578 return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3; in usb3_is_usb3_phy_in_u3()
581 static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3) in usb3_wakeup_usb3_phy() argument
583 if (!usb3_is_usb3_phy_in_u3(usb3)) in usb3_wakeup_usb3_phy()
586 usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON); in usb3_wakeup_usb3_phy()
587 usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP); in usb3_wakeup_usb3_phy()
592 static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3) in usb3_feature_get_un_enabled() argument
596 u32 val = usb3_read(usb3, USB3_SSIFCMD); in usb3_feature_get_un_enabled()
608 static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable) in usb3_feature_u2_enable() argument
614 usb3_clear_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u2_enable()
616 usb3_set_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u2_enable()
619 static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable) in usb3_feature_u1_enable() argument
625 usb3_clear_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u1_enable()
627 usb3_set_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u1_enable()
630 static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3) in usb3_start_operation_for_usb3() argument
632 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_start_operation_for_usb3()
633 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON); in usb3_start_operation_for_usb3()
634 usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON); in usb3_start_operation_for_usb3()
637 static void usb3_start_usb3_connection(struct renesas_usb3 *usb3) in usb3_start_usb3_connection() argument
639 usb3_start_operation_for_usb3(usb3); in usb3_start_usb3_connection()
640 usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON); in usb3_start_usb3_connection()
642 usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE | in usb3_start_usb3_connection()
646 static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3) in usb3_stop_usb3_connection() argument
648 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON); in usb3_stop_usb3_connection()
651 static void usb3_transition_to_default_state(struct renesas_usb3 *usb3, in usb3_transition_to_default_state() argument
654 usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2); in usb3_transition_to_default_state()
655 usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA); in usb3_transition_to_default_state()
656 usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA); in usb3_transition_to_default_state()
659 usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST | in usb3_transition_to_default_state()
662 usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND | in usb3_transition_to_default_state()
666 static void usb3_connect(struct renesas_usb3 *usb3) in usb3_connect() argument
668 if (usb3_wakeup_usb3_phy(usb3)) in usb3_connect()
669 usb3_start_usb3_connection(usb3); in usb3_connect()
672 static void usb3_reset_epc(struct renesas_usb3 *usb3) in usb3_reset_epc() argument
674 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON); in usb3_reset_epc()
675 usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_reset_epc()
676 usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON); in usb3_reset_epc()
677 usb3->test_mode = 0; in usb3_reset_epc()
678 usb3_set_test_mode(usb3); in usb3_reset_epc()
681 static void usb3_disconnect(struct renesas_usb3 *usb3) in usb3_disconnect() argument
683 usb3->disabled_count = 0; in usb3_disconnect()
684 usb3_usb2_pullup(usb3, 0); in usb3_disconnect()
685 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON); in usb3_disconnect()
686 usb3_reset_epc(usb3); in usb3_disconnect()
687 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM | USB_INT_1_B3_PLLWKUP | in usb3_disconnect()
692 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON); in usb3_disconnect()
693 usb3_init_epc_registers(usb3); in usb3_disconnect()
695 if (usb3->driver) in usb3_disconnect()
696 usb3->driver->disconnect(&usb3->gadget); in usb3_disconnect()
699 static void usb3_check_vbus(struct renesas_usb3 *usb3) in usb3_check_vbus() argument
701 if (usb3->workaround_for_vbus) { in usb3_check_vbus()
702 usb3_connect(usb3); in usb3_check_vbus()
704 usb3->extcon_usb = !!(usb3_read(usb3, USB3_USB_STA) & in usb3_check_vbus()
706 if (usb3->extcon_usb) in usb3_check_vbus()
707 usb3_connect(usb3); in usb3_check_vbus()
709 usb3_disconnect(usb3); in usb3_check_vbus()
711 schedule_work(&usb3->extcon_work); in usb3_check_vbus()
717 struct renesas_usb3 *usb3 = in renesas_usb3_role_work() local
720 usb_role_switch_set_role(usb3->role_sw, usb3->role); in renesas_usb3_role_work()
723 static void usb3_set_mode(struct renesas_usb3 *usb3, bool host) in usb3_set_mode() argument
725 if (usb3->is_rzv2m) { in usb3_set_mode()
727 usb3_drd_set_bit(usb3, DRD_CON_PERI_RST, USB3_DRD_CON(usb3)); in usb3_set_mode()
728 usb3_drd_clear_bit(usb3, DRD_CON_HOST_RST, USB3_DRD_CON(usb3)); in usb3_set_mode()
730 usb3_drd_set_bit(usb3, DRD_CON_HOST_RST, USB3_DRD_CON(usb3)); in usb3_set_mode()
731 usb3_drd_clear_bit(usb3, DRD_CON_PERI_RST, USB3_DRD_CON(usb3)); in usb3_set_mode()
736 usb3_drd_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON(usb3)); in usb3_set_mode()
738 usb3_drd_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON(usb3)); in usb3_set_mode()
741 static void usb3_set_mode_by_role_sw(struct renesas_usb3 *usb3, bool host) in usb3_set_mode_by_role_sw() argument
743 if (usb3->role_sw) { in usb3_set_mode_by_role_sw()
744 usb3->role = host ? USB_ROLE_HOST : USB_ROLE_DEVICE; in usb3_set_mode_by_role_sw()
745 schedule_work(&usb3->role_work); in usb3_set_mode_by_role_sw()
747 usb3_set_mode(usb3, host); in usb3_set_mode_by_role_sw()
751 static void usb3_vbus_out(struct renesas_usb3 *usb3, bool enable) in usb3_vbus_out() argument
754 usb3_drd_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON(usb3)); in usb3_vbus_out()
756 usb3_drd_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON(usb3)); in usb3_vbus_out()
759 static void usb3_mode_config(struct renesas_usb3 *usb3, bool host, bool a_dev) in usb3_mode_config() argument
763 spin_lock_irqsave(&usb3->lock, flags); in usb3_mode_config()
764 if (!usb3->role_sw_by_connector || in usb3_mode_config()
765 usb3->connection_state != USB_ROLE_NONE) { in usb3_mode_config()
766 usb3_set_mode_by_role_sw(usb3, host); in usb3_mode_config()
767 usb3_vbus_out(usb3, a_dev); in usb3_mode_config()
769 /* for A-Peripheral or forced B-device mode */ in usb3_mode_config()
770 if ((!host && a_dev) || usb3->start_to_connect) in usb3_mode_config()
771 usb3_connect(usb3); in usb3_mode_config()
772 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_mode_config()
775 static bool usb3_is_a_device(struct renesas_usb3 *usb3) in usb3_is_a_device() argument
777 return !(usb3_drd_read(usb3, USB3_USB_OTG_STA(usb3)) & USB_OTG_IDMON(usb3)); in usb3_is_a_device()
780 static void usb3_check_id(struct renesas_usb3 *usb3) in usb3_check_id() argument
782 usb3->extcon_host = usb3_is_a_device(usb3); in usb3_check_id()
784 if ((!usb3->role_sw_by_connector && usb3->extcon_host && in usb3_check_id()
785 !usb3->forced_b_device) || usb3->connection_state == USB_ROLE_HOST) in usb3_check_id()
786 usb3_mode_config(usb3, true, true); in usb3_check_id()
788 usb3_mode_config(usb3, false, false); in usb3_check_id()
790 schedule_work(&usb3->extcon_work); in usb3_check_id()
793 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3) in renesas_usb3_init_controller() argument
795 usb3_init_axi_bridge(usb3); in renesas_usb3_init_controller()
796 usb3_init_epc_registers(usb3); in renesas_usb3_init_controller()
797 usb3_set_bit(usb3, USB_COM_CON_PN_WDATAIF_NL | in renesas_usb3_init_controller()
800 usb3_drd_write(usb3, USB_OTG_IDMON(usb3), USB3_USB_OTG_INT_STA(usb3)); in renesas_usb3_init_controller()
801 usb3_drd_write(usb3, USB_OTG_IDMON(usb3), USB3_USB_OTG_INT_ENA(usb3)); in renesas_usb3_init_controller()
803 usb3_check_id(usb3); in renesas_usb3_init_controller()
804 usb3_check_vbus(usb3); in renesas_usb3_init_controller()
807 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3) in renesas_usb3_stop_controller() argument
809 usb3_disconnect(usb3); in renesas_usb3_stop_controller()
810 usb3_write(usb3, 0, USB3_P0_INT_ENA); in renesas_usb3_stop_controller()
811 usb3_drd_write(usb3, 0, USB3_USB_OTG_INT_ENA(usb3)); in renesas_usb3_stop_controller()
812 usb3_write(usb3, 0, USB3_USB_INT_ENA_1); in renesas_usb3_stop_controller()
813 usb3_write(usb3, 0, USB3_USB_INT_ENA_2); in renesas_usb3_stop_controller()
814 usb3_write(usb3, 0, USB3_AXI_INT_ENA); in renesas_usb3_stop_controller()
817 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_pll_wakeup() argument
819 usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP); in usb3_irq_epc_int_1_pll_wakeup()
820 usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON); in usb3_irq_epc_int_1_pll_wakeup()
821 usb3_start_usb3_connection(usb3); in usb3_irq_epc_int_1_pll_wakeup()
824 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_linkup_success() argument
826 usb3_transition_to_default_state(usb3, true); in usb3_irq_epc_int_1_linkup_success()
829 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_resume() argument
831 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM); in usb3_irq_epc_int_1_resume()
832 usb3_start_usb2_connection(usb3); in usb3_irq_epc_int_1_resume()
833 usb3_transition_to_default_state(usb3, false); in usb3_irq_epc_int_1_resume()
836 static void usb3_irq_epc_int_1_suspend(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_suspend() argument
838 usb3_disable_irq_1(usb3, USB_INT_1_B2_SPND); in usb3_irq_epc_int_1_suspend()
840 if (usb3->gadget.speed != USB_SPEED_UNKNOWN && in usb3_irq_epc_int_1_suspend()
841 usb3->gadget.state != USB_STATE_NOTATTACHED) { in usb3_irq_epc_int_1_suspend()
842 if (usb3->driver && usb3->driver->suspend) in usb3_irq_epc_int_1_suspend()
843 usb3->driver->suspend(&usb3->gadget); in usb3_irq_epc_int_1_suspend()
844 usb_gadget_set_state(&usb3->gadget, USB_STATE_SUSPENDED); in usb3_irq_epc_int_1_suspend()
848 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_disable() argument
850 usb3_stop_usb3_connection(usb3); in usb3_irq_epc_int_1_disable()
851 if (usb3_wakeup_usb2_phy(usb3)) in usb3_irq_epc_int_1_disable()
852 usb3_irq_epc_int_1_resume(usb3); in usb3_irq_epc_int_1_disable()
855 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_bus_reset() argument
857 usb3_reset_epc(usb3); in usb3_irq_epc_int_1_bus_reset()
858 if (usb3->disabled_count < 3) in usb3_irq_epc_int_1_bus_reset()
859 usb3_start_usb3_connection(usb3); in usb3_irq_epc_int_1_bus_reset()
861 usb3_start_usb2_connection(usb3); in usb3_irq_epc_int_1_bus_reset()
864 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_vbus_change() argument
866 usb3_check_vbus(usb3); in usb3_irq_epc_int_1_vbus_change()
869 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_hot_reset() argument
871 usb3_reset_epc(usb3); in usb3_irq_epc_int_1_hot_reset()
872 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_irq_epc_int_1_hot_reset()
875 usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON); in usb3_irq_epc_int_1_hot_reset()
878 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_warm_reset() argument
880 usb3_reset_epc(usb3); in usb3_irq_epc_int_1_warm_reset()
881 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_irq_epc_int_1_warm_reset()
883 usb3_start_operation_for_usb3(usb3); in usb3_irq_epc_int_1_warm_reset()
884 usb3_enable_irq_1(usb3, USB_INT_1_SPEED); in usb3_irq_epc_int_1_warm_reset()
887 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_speed() argument
889 u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK; in usb3_irq_epc_int_1_speed()
893 usb3->gadget.speed = USB_SPEED_SUPER; in usb3_irq_epc_int_1_speed()
894 usb3->gadget.ep0->maxpacket = USB3_EP0_SS_MAX_PACKET_SIZE; in usb3_irq_epc_int_1_speed()
897 usb3->gadget.speed = USB_SPEED_HIGH; in usb3_irq_epc_int_1_speed()
898 usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE; in usb3_irq_epc_int_1_speed()
901 usb3->gadget.speed = USB_SPEED_FULL; in usb3_irq_epc_int_1_speed()
902 usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE; in usb3_irq_epc_int_1_speed()
905 usb3->gadget.speed = USB_SPEED_UNKNOWN; in usb3_irq_epc_int_1_speed()
910 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1) in usb3_irq_epc_int_1() argument
913 usb3_irq_epc_int_1_pll_wakeup(usb3); in usb3_irq_epc_int_1()
916 usb3_irq_epc_int_1_linkup_success(usb3); in usb3_irq_epc_int_1()
919 usb3_irq_epc_int_1_hot_reset(usb3); in usb3_irq_epc_int_1()
922 usb3_irq_epc_int_1_warm_reset(usb3); in usb3_irq_epc_int_1()
925 usb3_irq_epc_int_1_disable(usb3); in usb3_irq_epc_int_1()
928 usb3_irq_epc_int_1_bus_reset(usb3); in usb3_irq_epc_int_1()
931 usb3_irq_epc_int_1_resume(usb3); in usb3_irq_epc_int_1()
934 usb3_irq_epc_int_1_suspend(usb3); in usb3_irq_epc_int_1()
937 usb3_irq_epc_int_1_speed(usb3); in usb3_irq_epc_int_1()
940 usb3_irq_epc_int_1_vbus_change(usb3); in usb3_irq_epc_int_1()
946 return list_first_entry_or_null(&usb3_ep->queue, in __usb3_get_request()
953 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_get_request() local
957 spin_lock_irqsave(&usb3->lock, flags); in usb3_get_request()
959 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_get_request()
968 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in __usb3_request_done() local
970 dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n", in __usb3_request_done()
971 usb3_ep->num, usb3_req->req.length, usb3_req->req.actual, in __usb3_request_done()
973 usb3_req->req.status = status; in __usb3_request_done()
974 usb3_ep->started = false; in __usb3_request_done()
975 list_del_init(&usb3_req->queue); in __usb3_request_done()
976 spin_unlock(&usb3->lock); in __usb3_request_done()
977 usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req); in __usb3_request_done()
978 spin_lock(&usb3->lock); in __usb3_request_done()
984 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_request_done() local
987 spin_lock_irqsave(&usb3->lock, flags); in usb3_request_done()
989 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_request_done()
992 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0_status_end() argument
994 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_irq_epc_pipe0_status_end()
999 if (usb3->test_mode) in usb3_irq_epc_pipe0_status_end()
1000 usb3_set_test_mode(usb3); in usb3_irq_epc_pipe0_status_end()
1003 static void usb3_get_setup_data(struct renesas_usb3 *usb3, in usb3_get_setup_data() argument
1006 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_get_setup_data()
1009 *data++ = usb3_read(usb3, USB3_STUP_DAT_0); in usb3_get_setup_data()
1010 *data = usb3_read(usb3, USB3_STUP_DAT_1); in usb3_get_setup_data()
1013 usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN); in usb3_get_setup_data()
1016 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res) in usb3_set_p0_con_update_res() argument
1018 u32 val = usb3_read(usb3, USB3_P0_CON); in usb3_set_p0_con_update_res()
1022 usb3_write(usb3, val, USB3_P0_CON); in usb3_set_p0_con_update_res()
1025 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_read_data() argument
1027 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY | in usb3_set_p0_con_for_ctrl_read_data()
1032 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_read_status() argument
1034 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL | in usb3_set_p0_con_for_ctrl_read_status()
1039 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_write_data() argument
1041 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY | in usb3_set_p0_con_for_ctrl_write_data()
1046 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_write_status() argument
1048 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL | in usb3_set_p0_con_for_ctrl_write_status()
1053 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_no_data() argument
1055 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL | in usb3_set_p0_con_for_no_data()
1060 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3) in usb3_set_p0_con_stall() argument
1062 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL | in usb3_set_p0_con_stall()
1067 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3) in usb3_set_p0_con_stop() argument
1069 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY | in usb3_set_p0_con_stop()
1074 static int usb3_pn_change(struct renesas_usb3 *usb3, int num) in usb3_pn_change() argument
1076 if (num == 0 || num > usb3->num_usb3_eps) in usb3_pn_change()
1077 return -ENXIO; in usb3_pn_change()
1079 usb3_write(usb3, num, USB3_PIPE_COM); in usb3_pn_change()
1084 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res) in usb3_set_pn_con_update_res() argument
1086 u32 val = usb3_read(usb3, USB3_PN_CON); in usb3_set_pn_con_update_res()
1091 usb3_write(usb3, val, USB3_PN_CON); in usb3_set_pn_con_update_res()
1094 static void usb3_pn_start(struct renesas_usb3 *usb3) in usb3_pn_start() argument
1096 usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL); in usb3_pn_start()
1099 static void usb3_pn_stop(struct renesas_usb3 *usb3) in usb3_pn_stop() argument
1101 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY); in usb3_pn_stop()
1104 static void usb3_pn_stall(struct renesas_usb3 *usb3) in usb3_pn_stall() argument
1106 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL); in usb3_pn_stall()
1109 static int usb3_pn_con_clear(struct renesas_usb3 *usb3) in usb3_pn_con_clear() argument
1111 usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON); in usb3_pn_con_clear()
1113 return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0); in usb3_pn_con_clear()
1119 struct usb_request *req = &usb3_req->req; in usb3_is_transfer_complete()
1121 if ((!req->zero && req->actual == req->length) || in usb3_is_transfer_complete()
1122 (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0)) in usb3_is_transfer_complete()
1130 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_wait_pipe_status() local
1131 u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA; in usb3_wait_pipe_status()
1133 return usb3_wait(usb3, sta_reg, mask, mask); in usb3_wait_pipe_status()
1139 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_px_con_send() local
1140 u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON; in usb3_set_px_con_send()
1141 u32 val = usb3_read(usb3, con_reg); in usb3_set_px_con_send()
1144 val |= (usb3_ep->num && last) ? PN_CON_LAST : 0; in usb3_set_px_con_send()
1145 usb3_write(usb3, val, con_reg); in usb3_set_px_con_send()
1152 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_write_pipe() local
1154 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual, in usb3_write_pipe()
1155 usb3_ep->ep.maxpacket); in usb3_write_pipe()
1156 u8 *buf = usb3_req->req.buf + usb3_req->req.actual; in usb3_write_pipe()
1161 return -EBUSY; in usb3_write_pipe()
1164 usb3_req->req.actual += len; in usb3_write_pipe()
1168 iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4); in usb3_write_pipe()
1176 usb3_write(usb3, tmp, fifo_reg); in usb3_write_pipe()
1184 return is_last ? 0 : -EAGAIN; in usb3_write_pipe()
1189 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_get_received_length() local
1190 u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG; in usb3_get_received_length()
1192 return usb3_read(usb3, lng_reg); in usb3_get_received_length()
1198 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_read_pipe() local
1200 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual, in usb3_read_pipe()
1202 u8 *buf = usb3_req->req.buf + usb3_req->req.actual; in usb3_read_pipe()
1209 usb3_req->req.actual += len; in usb3_read_pipe()
1213 ioread32_rep(usb3->reg + fifo_reg, buf, len / 4); in usb3_read_pipe()
1219 tmp = usb3_read(usb3, fifo_reg); in usb3_read_pipe()
1224 return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN; in usb3_read_pipe()
1230 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_status_stage() local
1232 if (usb3_ep->dir_in) { in usb3_set_status_stage()
1233 usb3_set_p0_con_for_ctrl_read_status(usb3); in usb3_set_status_stage()
1235 if (!usb3_req->req.length) in usb3_set_status_stage()
1236 usb3_set_p0_con_for_no_data(usb3); in usb3_set_status_stage()
1238 usb3_set_p0_con_for_ctrl_write_status(usb3); in usb3_set_status_stage()
1247 if (usb3_ep->dir_in) in usb3_p0_xfer()
1259 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_start_pipe0() local
1261 if (usb3_ep->started) in usb3_start_pipe0()
1264 usb3_ep->started = true; in usb3_start_pipe0()
1266 if (usb3_ep->dir_in) { in usb3_start_pipe0()
1267 usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD); in usb3_start_pipe0()
1268 usb3_set_p0_con_for_ctrl_read_data(usb3); in usb3_start_pipe0()
1270 usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD); in usb3_start_pipe0()
1271 if (usb3_req->req.length) in usb3_start_pipe0()
1272 usb3_set_p0_con_for_ctrl_write_data(usb3); in usb3_start_pipe0()
1278 static void usb3_enable_dma_pipen(struct renesas_usb3 *usb3) in usb3_enable_dma_pipen() argument
1280 usb3_set_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON); in usb3_enable_dma_pipen()
1283 static void usb3_disable_dma_pipen(struct renesas_usb3 *usb3) in usb3_disable_dma_pipen() argument
1285 usb3_clear_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON); in usb3_disable_dma_pipen()
1288 static void usb3_enable_dma_irq(struct renesas_usb3 *usb3, int num) in usb3_enable_dma_irq() argument
1290 usb3_set_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA); in usb3_enable_dma_irq()
1293 static void usb3_disable_dma_irq(struct renesas_usb3 *usb3, int num) in usb3_disable_dma_irq() argument
1295 usb3_clear_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA); in usb3_disable_dma_irq()
1300 switch (usb3_ep->ep.maxpacket) { in usb3_dma_mps_to_prd_word1()
1321 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_get_setting_area() local
1326 if (usb3_req->req.length > USB3_DMA_MAX_XFER_SIZE_ALL_PRDS) { in usb3_dma_get_setting_area()
1327 dev_dbg(usb3_to_dev(usb3), "%s: the length is too big (%d)\n", in usb3_dma_get_setting_area()
1328 __func__, usb3_req->req.length); in usb3_dma_get_setting_area()
1332 /* The driver doesn't handle zero-length packet via dmac */ in usb3_dma_get_setting_area()
1333 if (!usb3_req->req.length) in usb3_dma_get_setting_area()
1339 usb3_for_each_dma(usb3, dma, i) { in usb3_dma_get_setting_area()
1340 if (dma->used) in usb3_dma_get_setting_area()
1343 if (usb_gadget_map_request(&usb3->gadget, &usb3_req->req, in usb3_dma_get_setting_area()
1344 usb3_ep->dir_in) < 0) in usb3_dma_get_setting_area()
1347 dma->used = true; in usb3_dma_get_setting_area()
1348 usb3_ep->dma = dma; in usb3_dma_get_setting_area()
1359 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_put_setting_area() local
1363 usb3_for_each_dma(usb3, dma, i) { in usb3_dma_put_setting_area()
1364 if (usb3_ep->dma == dma) { in usb3_dma_put_setting_area()
1365 usb_gadget_unmap_request(&usb3->gadget, &usb3_req->req, in usb3_dma_put_setting_area()
1366 usb3_ep->dir_in); in usb3_dma_put_setting_area()
1367 dma->used = false; in usb3_dma_put_setting_area()
1368 usb3_ep->dma = NULL; in usb3_dma_put_setting_area()
1377 struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd; in usb3_dma_fill_prd()
1378 u32 remain = usb3_req->req.length; in usb3_dma_fill_prd()
1379 u32 dma = usb3_req->req.dma; in usb3_dma_fill_prd()
1386 cur_prd->word1 = usb3_dma_mps_to_prd_word1(usb3_ep) | in usb3_dma_fill_prd()
1388 cur_prd->bap = dma; in usb3_dma_fill_prd()
1389 remain -= len; in usb3_dma_fill_prd()
1398 cur_prd->word1 |= USB3_PRD1_E | USB3_PRD1_INT; in usb3_dma_fill_prd()
1399 if (usb3_ep->dir_in) in usb3_dma_fill_prd()
1400 cur_prd->word1 |= USB3_PRD1_LST; in usb3_dma_fill_prd()
1405 struct renesas_usb3_dma *dma = usb3_ep->dma; in usb3_dma_kick_prd()
1406 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_kick_prd() local
1407 u32 dma_con = DMA_COM_PIPE_NO(usb3_ep->num) | DMA_CON_PRD_EN; in usb3_dma_kick_prd()
1409 if (usb3_ep->dir_in) in usb3_dma_kick_prd()
1414 usb3_write(usb3, 1 << usb3_ep->num, USB3_DMA_INT_STA); in usb3_dma_kick_prd()
1415 usb3_write(usb3, AXI_INT_PRDEN_CLR_STA(dma->num) | in usb3_dma_kick_prd()
1416 AXI_INT_PRDERR_STA(dma->num), USB3_AXI_INT_STA); in usb3_dma_kick_prd()
1418 usb3_write(usb3, dma->prd_dma, USB3_DMA_CH0_PRD_ADR(dma->num)); in usb3_dma_kick_prd()
1419 usb3_write(usb3, dma_con, USB3_DMA_CH0_CON(dma->num)); in usb3_dma_kick_prd()
1420 usb3_enable_dma_irq(usb3, usb3_ep->num); in usb3_dma_kick_prd()
1425 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_stop_prd() local
1426 struct renesas_usb3_dma *dma = usb3_ep->dma; in usb3_dma_stop_prd()
1428 usb3_disable_dma_irq(usb3, usb3_ep->num); in usb3_dma_stop_prd()
1429 usb3_write(usb3, 0, USB3_DMA_CH0_CON(dma->num)); in usb3_dma_stop_prd()
1435 struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd; in usb3_dma_update_status()
1436 struct usb_request *req = &usb3_req->req; in usb3_dma_update_status()
1444 if (cur_prd->word1 & USB3_PRD1_D) in usb3_dma_update_status()
1445 status = -EIO; in usb3_dma_update_status()
1446 if (cur_prd->word1 & USB3_PRD1_E) in usb3_dma_update_status()
1447 len = req->length % USB3_DMA_MAX_XFER_SIZE; in usb3_dma_update_status()
1450 remain = cur_prd->word1 & USB3_PRD1_SIZE_MASK; in usb3_dma_update_status()
1451 req->actual += len - remain; in usb3_dma_update_status()
1453 if (cur_prd->word1 & USB3_PRD1_E || in usb3_dma_update_status()
1467 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_try_start() local
1473 usb3_pn_stop(usb3); in usb3_dma_try_start()
1474 usb3_enable_dma_pipen(usb3); in usb3_dma_try_start()
1477 usb3_pn_start(usb3); in usb3_dma_try_start()
1487 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_try_stop() local
1491 spin_lock_irqsave(&usb3->lock, flags); in usb3_dma_try_stop()
1492 if (!usb3_ep->dma) in usb3_dma_try_stop()
1495 if (!usb3_pn_change(usb3, usb3_ep->num)) in usb3_dma_try_stop()
1496 usb3_disable_dma_pipen(usb3); in usb3_dma_try_stop()
1502 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_dma_try_stop()
1506 static int renesas_usb3_dma_free_prd(struct renesas_usb3 *usb3, in renesas_usb3_dma_free_prd() argument
1512 usb3_for_each_dma(usb3, dma, i) { in renesas_usb3_dma_free_prd()
1513 if (dma->prd) { in renesas_usb3_dma_free_prd()
1515 dma->prd, dma->prd_dma); in renesas_usb3_dma_free_prd()
1516 dma->prd = NULL; in renesas_usb3_dma_free_prd()
1523 static int renesas_usb3_dma_alloc_prd(struct renesas_usb3 *usb3, in renesas_usb3_dma_alloc_prd() argument
1532 usb3_for_each_dma(usb3, dma, i) { in renesas_usb3_dma_alloc_prd()
1533 dma->prd = dma_alloc_coherent(dev, USB3_DMA_PRD_SIZE, in renesas_usb3_dma_alloc_prd()
1534 &dma->prd_dma, GFP_KERNEL); in renesas_usb3_dma_alloc_prd()
1535 if (!dma->prd) { in renesas_usb3_dma_alloc_prd()
1536 renesas_usb3_dma_free_prd(usb3, dev); in renesas_usb3_dma_alloc_prd()
1537 return -ENOMEM; in renesas_usb3_dma_alloc_prd()
1539 dma->num = i + 1; in renesas_usb3_dma_alloc_prd()
1548 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_start_pipen() local
1551 int ret = -EAGAIN; in usb3_start_pipen()
1554 spin_lock_irqsave(&usb3->lock, flags); in usb3_start_pipen()
1555 if (usb3_ep->halt || usb3_ep->started) in usb3_start_pipen()
1561 if (usb3_pn_change(usb3, usb3_ep->num) < 0) in usb3_start_pipen()
1564 usb3_ep->started = true; in usb3_start_pipen()
1569 usb3_pn_start(usb3); in usb3_start_pipen()
1571 if (usb3_ep->dir_in) { in usb3_start_pipen()
1580 usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA); in usb3_start_pipen()
1581 usb3_enable_pipe_irq(usb3, usb3_ep->num); in usb3_start_pipen()
1584 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_start_pipen()
1592 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in renesas_usb3_ep_queue() local
1595 dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num, in renesas_usb3_ep_queue()
1596 _req->length); in renesas_usb3_ep_queue()
1598 _req->status = -EINPROGRESS; in renesas_usb3_ep_queue()
1599 _req->actual = 0; in renesas_usb3_ep_queue()
1600 spin_lock_irqsave(&usb3->lock, flags); in renesas_usb3_ep_queue()
1601 list_add_tail(&usb3_req->queue, &usb3_ep->queue); in renesas_usb3_ep_queue()
1602 spin_unlock_irqrestore(&usb3->lock, flags); in renesas_usb3_ep_queue()
1604 if (!usb3_ep->num) in renesas_usb3_ep_queue()
1612 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr) in usb3_set_device_address() argument
1615 usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON); in usb3_set_device_address()
1618 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3, in usb3_std_req_set_address() argument
1621 if (le16_to_cpu(ctrl->wValue) >= 128) in usb3_std_req_set_address()
1624 usb3_set_device_address(usb3, le16_to_cpu(ctrl->wValue)); in usb3_std_req_set_address()
1625 usb3_set_p0_con_for_no_data(usb3); in usb3_std_req_set_address()
1630 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3, in usb3_pipe0_internal_xfer() argument
1635 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_pipe0_internal_xfer()
1638 memcpy(usb3->ep0_buf, tx_data, in usb3_pipe0_internal_xfer()
1641 usb3->ep0_req->buf = &usb3->ep0_buf; in usb3_pipe0_internal_xfer()
1642 usb3->ep0_req->length = len; in usb3_pipe0_internal_xfer()
1643 usb3->ep0_req->complete = complete; in usb3_pipe0_internal_xfer()
1644 renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC); in usb3_pipe0_internal_xfer()
1652 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3, in usb3_std_req_get_status() argument
1661 switch (ctrl->bRequestType & USB_RECIP_MASK) { in usb3_std_req_get_status()
1663 if (usb3->gadget.is_selfpowered) in usb3_std_req_get_status()
1665 if (usb3->gadget.speed == USB_SPEED_SUPER) in usb3_std_req_get_status()
1666 status |= usb3_feature_get_un_enabled(usb3); in usb3_std_req_get_status()
1671 num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; in usb3_std_req_get_status()
1672 usb3_ep = usb3_get_ep(usb3, num); in usb3_std_req_get_status()
1673 if (usb3_ep->halt) in usb3_std_req_get_status()
1683 dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n", in usb3_std_req_get_status()
1684 usb_req_to_usb3_req(usb3->ep0_req)); in usb3_std_req_get_status()
1685 usb3_pipe0_internal_xfer(usb3, &tx_data, sizeof(tx_data), in usb3_std_req_get_status()
1692 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3, in usb3_std_req_feature_device() argument
1696 u16 w_value = le16_to_cpu(ctrl->wValue); in usb3_std_req_feature_device()
1702 usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8; in usb3_std_req_feature_device()
1707 if (usb3->gadget.speed != USB_SPEED_SUPER) in usb3_std_req_feature_device()
1710 usb3_feature_u1_enable(usb3, set); in usb3_std_req_feature_device()
1712 usb3_feature_u2_enable(usb3, set); in usb3_std_req_feature_device()
1724 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_halt_p0() local
1726 if (unlikely(usb3_ep->num)) in usb3_set_halt_p0()
1727 return -EINVAL; in usb3_set_halt_p0()
1729 usb3_ep->halt = halt; in usb3_set_halt_p0()
1731 usb3_set_p0_con_stall(usb3); in usb3_set_halt_p0()
1733 usb3_set_p0_con_stop(usb3); in usb3_set_halt_p0()
1741 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_halt_pn() local
1744 spin_lock_irqsave(&usb3->lock, flags); in usb3_set_halt_pn()
1745 if (!usb3_pn_change(usb3, usb3_ep->num)) { in usb3_set_halt_pn()
1746 usb3_ep->halt = halt; in usb3_set_halt_pn()
1748 usb3_pn_stall(usb3); in usb3_set_halt_pn()
1749 } else if (!is_clear_feature || !usb3_ep->wedge) { in usb3_set_halt_pn()
1750 usb3_pn_con_clear(usb3); in usb3_set_halt_pn()
1751 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON); in usb3_set_halt_pn()
1752 usb3_pn_stop(usb3); in usb3_set_halt_pn()
1755 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_set_halt_pn()
1765 if (halt && usb3_ep->started) in usb3_set_halt()
1766 return -EAGAIN; in usb3_set_halt()
1768 if (usb3_ep->num) in usb3_set_halt()
1776 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3, in usb3_std_req_feature_endpoint() argument
1780 int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; in usb3_std_req_feature_endpoint()
1784 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT) in usb3_std_req_feature_endpoint()
1787 usb3_ep = usb3_get_ep(usb3, num); in usb3_std_req_feature_endpoint()
1792 usb3_ep->started = false; in usb3_std_req_feature_endpoint()
1801 static bool usb3_std_req_feature(struct renesas_usb3 *usb3, in usb3_std_req_feature() argument
1806 switch (ctrl->bRequestType & USB_RECIP_MASK) { in usb3_std_req_feature()
1808 stall = usb3_std_req_feature_device(usb3, ctrl, set); in usb3_std_req_feature()
1813 stall = usb3_std_req_feature_endpoint(usb3, ctrl, set); in usb3_std_req_feature()
1821 usb3_set_p0_con_for_no_data(usb3); in usb3_std_req_feature()
1832 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3, in usb3_std_req_set_sel() argument
1835 u16 w_length = le16_to_cpu(ctrl->wLength); in usb3_std_req_set_sel()
1840 dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n", in usb3_std_req_set_sel()
1841 usb_req_to_usb3_req(usb3->ep0_req)); in usb3_std_req_set_sel()
1842 usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion); in usb3_std_req_set_sel()
1847 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3, in usb3_std_req_set_configuration() argument
1850 if (le16_to_cpu(ctrl->wValue) > 0) in usb3_std_req_set_configuration()
1851 usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON); in usb3_std_req_set_configuration()
1853 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON); in usb3_std_req_set_configuration()
1859 * usb3_handle_standard_request - handle some standard requests
1860 * @usb3: the renesas_usb3 pointer
1865 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3, in usb3_handle_standard_request() argument
1871 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { in usb3_handle_standard_request()
1872 switch (ctrl->bRequest) { in usb3_handle_standard_request()
1874 stall = usb3_std_req_set_address(usb3, ctrl); in usb3_handle_standard_request()
1878 stall = usb3_std_req_get_status(usb3, ctrl); in usb3_handle_standard_request()
1882 stall = usb3_std_req_feature(usb3, ctrl, false); in usb3_handle_standard_request()
1886 stall = usb3_std_req_feature(usb3, ctrl, true); in usb3_handle_standard_request()
1890 stall = usb3_std_req_set_sel(usb3, ctrl); in usb3_handle_standard_request()
1899 usb3_std_req_set_configuration(usb3, ctrl); in usb3_handle_standard_request()
1907 usb3_set_p0_con_stall(usb3); in usb3_handle_standard_request()
1912 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3) in usb3_p0_con_clear_buffer() argument
1914 usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON); in usb3_p0_con_clear_buffer()
1916 return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0); in usb3_p0_con_clear_buffer()
1919 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0_setup() argument
1922 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_irq_epc_pipe0_setup()
1925 if (usb3_ep->started) in usb3_irq_epc_pipe0_setup()
1927 -ECONNRESET); in usb3_irq_epc_pipe0_setup()
1929 usb3_p0_con_clear_buffer(usb3); in usb3_irq_epc_pipe0_setup()
1930 usb3_get_setup_data(usb3, &ctrl); in usb3_irq_epc_pipe0_setup()
1931 if (!usb3_handle_standard_request(usb3, &ctrl)) in usb3_irq_epc_pipe0_setup()
1932 if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0) in usb3_irq_epc_pipe0_setup()
1933 usb3_set_p0_con_stall(usb3); in usb3_irq_epc_pipe0_setup()
1936 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0_bfrdy() argument
1938 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_irq_epc_pipe0_bfrdy()
1947 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0() argument
1949 u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA); in usb3_irq_epc_pipe0()
1951 p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA); in usb3_irq_epc_pipe0()
1952 usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA); in usb3_irq_epc_pipe0()
1954 usb3_irq_epc_pipe0_status_end(usb3); in usb3_irq_epc_pipe0()
1956 usb3_irq_epc_pipe0_setup(usb3); in usb3_irq_epc_pipe0()
1958 usb3_irq_epc_pipe0_bfrdy(usb3); in usb3_irq_epc_pipe0()
1961 static void usb3_request_done_pipen(struct renesas_usb3 *usb3, in usb3_request_done_pipen() argument
1968 spin_lock_irqsave(&usb3->lock, flags); in usb3_request_done_pipen()
1969 if (usb3_pn_change(usb3, usb3_ep->num)) in usb3_request_done_pipen()
1970 usb3_pn_stop(usb3); in usb3_request_done_pipen()
1971 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_request_done_pipen()
1973 usb3_disable_pipe_irq(usb3, usb3_ep->num); in usb3_request_done_pipen()
1982 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num) in usb3_irq_epc_pipen_lsttr() argument
1984 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num); in usb3_irq_epc_pipen_lsttr()
1990 if (usb3_ep->dir_in) { in usb3_irq_epc_pipen_lsttr()
1991 dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n", in usb3_irq_epc_pipen_lsttr()
1992 __func__, usb3_req->req.length, usb3_req->req.actual); in usb3_irq_epc_pipen_lsttr()
1993 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0); in usb3_irq_epc_pipen_lsttr()
1997 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num) in usb3_irq_epc_pipen_bfrdy() argument
1999 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num); in usb3_irq_epc_pipen_bfrdy()
2006 spin_lock(&usb3->lock); in usb3_irq_epc_pipen_bfrdy()
2007 if (usb3_pn_change(usb3, num)) in usb3_irq_epc_pipen_bfrdy()
2010 if (usb3_ep->dir_in) { in usb3_irq_epc_pipen_bfrdy()
2013 usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA); in usb3_irq_epc_pipen_bfrdy()
2021 spin_unlock(&usb3->lock); in usb3_irq_epc_pipen_bfrdy()
2024 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0); in usb3_irq_epc_pipen_bfrdy()
2027 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num) in usb3_irq_epc_pipen() argument
2031 spin_lock(&usb3->lock); in usb3_irq_epc_pipen()
2032 if (usb3_pn_change(usb3, num) < 0) { in usb3_irq_epc_pipen()
2033 spin_unlock(&usb3->lock); in usb3_irq_epc_pipen()
2037 pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA); in usb3_irq_epc_pipen()
2038 pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA); in usb3_irq_epc_pipen()
2039 usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA); in usb3_irq_epc_pipen()
2040 spin_unlock(&usb3->lock); in usb3_irq_epc_pipen()
2042 usb3_irq_epc_pipen_lsttr(usb3, num); in usb3_irq_epc_pipen()
2044 usb3_irq_epc_pipen_bfrdy(usb3, num); in usb3_irq_epc_pipen()
2047 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2) in usb3_irq_epc_int_2() argument
2051 for (i = 0; i < usb3->num_usb3_eps; i++) { in usb3_irq_epc_int_2()
2054 usb3_irq_epc_pipe0(usb3); in usb3_irq_epc_int_2()
2056 usb3_irq_epc_pipen(usb3, i); in usb3_irq_epc_int_2()
2061 static void usb3_irq_idmon_change(struct renesas_usb3 *usb3) in usb3_irq_idmon_change() argument
2063 usb3_check_id(usb3); in usb3_irq_idmon_change()
2066 static void usb3_irq_otg_int(struct renesas_usb3 *usb3) in usb3_irq_otg_int() argument
2068 u32 otg_int_sta = usb3_drd_read(usb3, USB3_USB_OTG_INT_STA(usb3)); in usb3_irq_otg_int()
2070 otg_int_sta &= usb3_drd_read(usb3, USB3_USB_OTG_INT_ENA(usb3)); in usb3_irq_otg_int()
2072 usb3_drd_write(usb3, otg_int_sta, USB3_USB_OTG_INT_STA(usb3)); in usb3_irq_otg_int()
2074 if (otg_int_sta & USB_OTG_IDMON(usb3)) in usb3_irq_otg_int()
2075 usb3_irq_idmon_change(usb3); in usb3_irq_otg_int()
2078 static void usb3_irq_epc(struct renesas_usb3 *usb3) in usb3_irq_epc() argument
2080 u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1); in usb3_irq_epc()
2081 u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2); in usb3_irq_epc()
2083 int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1); in usb3_irq_epc()
2085 usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1); in usb3_irq_epc()
2086 usb3_irq_epc_int_1(usb3, int_sta_1); in usb3_irq_epc()
2089 int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2); in usb3_irq_epc()
2091 usb3_irq_epc_int_2(usb3, int_sta_2); in usb3_irq_epc()
2093 if (!usb3->is_rzv2m) in usb3_irq_epc()
2094 usb3_irq_otg_int(usb3); in usb3_irq_epc()
2097 static void usb3_irq_dma_int(struct renesas_usb3 *usb3, u32 dma_sta) in usb3_irq_dma_int() argument
2103 for (i = 0; i < usb3->num_usb3_eps; i++) { in usb3_irq_dma_int()
2107 usb3_ep = usb3_get_ep(usb3, i); in usb3_irq_dma_int()
2108 if (!(usb3_read(usb3, USB3_AXI_INT_STA) & in usb3_irq_dma_int()
2109 AXI_INT_PRDEN_CLR_STA(usb3_ep->dma->num))) in usb3_irq_dma_int()
2114 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, status); in usb3_irq_dma_int()
2118 static void usb3_irq_dma(struct renesas_usb3 *usb3) in usb3_irq_dma() argument
2120 u32 dma_sta = usb3_read(usb3, USB3_DMA_INT_STA); in usb3_irq_dma()
2122 dma_sta &= usb3_read(usb3, USB3_DMA_INT_ENA); in usb3_irq_dma()
2124 usb3_write(usb3, dma_sta, USB3_DMA_INT_STA); in usb3_irq_dma()
2125 usb3_irq_dma_int(usb3, dma_sta); in usb3_irq_dma()
2131 struct renesas_usb3 *usb3 = _usb3; in renesas_usb3_irq() local
2133 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA); in renesas_usb3_irq()
2136 usb3_irq_dma(usb3); in renesas_usb3_irq()
2141 usb3_irq_epc(usb3); in renesas_usb3_irq()
2150 struct renesas_usb3 *usb3 = _usb3; in renesas_usb3_otg_irq() local
2152 usb3_irq_otg_int(usb3); in renesas_usb3_otg_irq()
2160 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_write_pn_mod() local
2163 val |= usb3_ep->dir_in ? PN_MOD_DIR : 0; in usb3_write_pn_mod()
2166 usb3_write(usb3, val, USB3_PN_MOD); in usb3_write_pn_mod()
2197 return usb3_ep->rammap_val | mpkt; in usb3_calc_rammap_val()
2203 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_enable_pipe_n() local
2206 usb3_ep->dir_in = usb_endpoint_dir_in(desc); in usb3_enable_pipe_n()
2208 spin_lock_irqsave(&usb3->lock, flags); in usb3_enable_pipe_n()
2209 if (!usb3_pn_change(usb3, usb3_ep->num)) { in usb3_enable_pipe_n()
2211 usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc), in usb3_enable_pipe_n()
2213 usb3_pn_con_clear(usb3); in usb3_enable_pipe_n()
2214 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON); in usb3_enable_pipe_n()
2216 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_enable_pipe_n()
2223 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_disable_pipe_n() local
2226 usb3_ep->halt = false; in usb3_disable_pipe_n()
2228 spin_lock_irqsave(&usb3->lock, flags); in usb3_disable_pipe_n()
2229 if (!usb3_pn_change(usb3, usb3_ep->num)) { in usb3_disable_pipe_n()
2230 usb3_write(usb3, 0, USB3_PN_INT_ENA); in usb3_disable_pipe_n()
2231 usb3_write(usb3, 0, USB3_PN_RAMMAP); in usb3_disable_pipe_n()
2232 usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON); in usb3_disable_pipe_n()
2234 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_disable_pipe_n()
2239 /*------- usb_ep_ops -----------------------------------------------------*/
2258 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN); in renesas_usb3_ep_disable()
2272 INIT_LIST_HEAD(&usb3_req->queue); in __renesas_usb3_ep_alloc_request()
2274 return &usb3_req->req; in __renesas_usb3_ep_alloc_request()
2300 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in renesas_usb3_ep_dequeue() local
2302 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num, in renesas_usb3_ep_dequeue()
2303 _req->length); in renesas_usb3_ep_dequeue()
2306 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET); in renesas_usb3_ep_dequeue()
2320 usb3_ep->wedge = true; in renesas_usb3_ep_set_wedge()
2327 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in renesas_usb3_ep_fifo_flush() local
2330 if (usb3_ep->num) { in renesas_usb3_ep_fifo_flush()
2331 spin_lock_irqsave(&usb3->lock, flags); in renesas_usb3_ep_fifo_flush()
2332 if (!usb3_pn_change(usb3, usb3_ep->num)) { in renesas_usb3_ep_fifo_flush()
2333 usb3_pn_con_clear(usb3); in renesas_usb3_ep_fifo_flush()
2334 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON); in renesas_usb3_ep_fifo_flush()
2336 spin_unlock_irqrestore(&usb3->lock, flags); in renesas_usb3_ep_fifo_flush()
2338 usb3_p0_con_clear_buffer(usb3); in renesas_usb3_ep_fifo_flush()
2357 /*------- usb_gadget_ops -------------------------------------------------*/
2361 struct renesas_usb3 *usb3; in renesas_usb3_start() local
2363 if (!driver || driver->max_speed < USB_SPEED_FULL || in renesas_usb3_start()
2364 !driver->setup) in renesas_usb3_start()
2365 return -EINVAL; in renesas_usb3_start()
2367 usb3 = gadget_to_renesas_usb3(gadget); in renesas_usb3_start()
2369 if (usb3->is_rzv2m && usb3_is_a_device(usb3)) in renesas_usb3_start()
2370 return -EBUSY; in renesas_usb3_start()
2373 usb3->driver = driver; in renesas_usb3_start()
2375 if (usb3->phy) in renesas_usb3_start()
2376 phy_init(usb3->phy); in renesas_usb3_start()
2378 pm_runtime_get_sync(usb3_to_dev(usb3)); in renesas_usb3_start()
2381 if (usb3->is_rzv2m) in renesas_usb3_start()
2382 rzv2m_usb3drd_reset(usb3_to_dev(usb3)->parent, false); in renesas_usb3_start()
2384 renesas_usb3_init_controller(usb3); in renesas_usb3_start()
2391 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget); in renesas_usb3_stop() local
2393 usb3->softconnect = false; in renesas_usb3_stop()
2394 usb3->gadget.speed = USB_SPEED_UNKNOWN; in renesas_usb3_stop()
2395 usb3->driver = NULL; in renesas_usb3_stop()
2396 if (usb3->is_rzv2m) in renesas_usb3_stop()
2397 rzv2m_usb3drd_reset(usb3_to_dev(usb3)->parent, false); in renesas_usb3_stop()
2399 renesas_usb3_stop_controller(usb3); in renesas_usb3_stop()
2400 if (usb3->phy) in renesas_usb3_stop()
2401 phy_exit(usb3->phy); in renesas_usb3_stop()
2403 pm_runtime_put(usb3_to_dev(usb3)); in renesas_usb3_stop()
2410 return -EOPNOTSUPP; in renesas_usb3_get_frame()
2415 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget); in renesas_usb3_pullup() local
2417 usb3->softconnect = !!is_on; in renesas_usb3_pullup()
2424 gadget->is_selfpowered = !!is_self; in renesas_usb3_set_selfpowered()
2439 struct renesas_usb3 *usb3 = usb_role_switch_get_drvdata(sw); in renesas_usb3_role_switch_get() local
2442 pm_runtime_get_sync(usb3_to_dev(usb3)); in renesas_usb3_role_switch_get()
2443 cur_role = usb3_is_host(usb3) ? USB_ROLE_HOST : USB_ROLE_DEVICE; in renesas_usb3_role_switch_get()
2444 pm_runtime_put(usb3_to_dev(usb3)); in renesas_usb3_role_switch_get()
2452 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in handle_ext_role_switch_states() local
2453 struct device *host = usb3->host_dev; in handle_ext_role_switch_states()
2454 enum usb_role cur_role = renesas_usb3_role_switch_get(usb3->role_sw); in handle_ext_role_switch_states()
2458 usb3->connection_state = USB_ROLE_NONE; in handle_ext_role_switch_states()
2459 if (!usb3->is_rzv2m && cur_role == USB_ROLE_HOST) in handle_ext_role_switch_states()
2461 if (usb3->driver) { in handle_ext_role_switch_states()
2462 if (usb3->is_rzv2m) in handle_ext_role_switch_states()
2463 rzv2m_usb3drd_reset(dev->parent, false); in handle_ext_role_switch_states()
2464 usb3_disconnect(usb3); in handle_ext_role_switch_states()
2466 usb3_vbus_out(usb3, false); in handle_ext_role_switch_states()
2468 if (usb3->is_rzv2m) { in handle_ext_role_switch_states()
2469 rzv2m_usb3drd_reset(dev->parent, true); in handle_ext_role_switch_states()
2474 if (usb3->connection_state == USB_ROLE_NONE) { in handle_ext_role_switch_states()
2475 usb3->connection_state = USB_ROLE_DEVICE; in handle_ext_role_switch_states()
2476 usb3_set_mode(usb3, false); in handle_ext_role_switch_states()
2477 if (usb3->driver) { in handle_ext_role_switch_states()
2478 if (usb3->is_rzv2m) in handle_ext_role_switch_states()
2479 renesas_usb3_init_controller(usb3); in handle_ext_role_switch_states()
2480 usb3_connect(usb3); in handle_ext_role_switch_states()
2484 usb3_set_mode(usb3, false); in handle_ext_role_switch_states()
2485 if (usb3->driver) in handle_ext_role_switch_states()
2486 usb3_connect(usb3); in handle_ext_role_switch_states()
2488 usb3_vbus_out(usb3, false); in handle_ext_role_switch_states()
2491 if (usb3->connection_state == USB_ROLE_NONE) { in handle_ext_role_switch_states()
2492 if (usb3->driver) { in handle_ext_role_switch_states()
2493 if (usb3->is_rzv2m) in handle_ext_role_switch_states()
2494 rzv2m_usb3drd_reset(dev->parent, false); in handle_ext_role_switch_states()
2495 usb3_disconnect(usb3); in handle_ext_role_switch_states()
2498 usb3->connection_state = USB_ROLE_HOST; in handle_ext_role_switch_states()
2499 usb3_set_mode(usb3, true); in handle_ext_role_switch_states()
2500 usb3_vbus_out(usb3, true); in handle_ext_role_switch_states()
2504 usb3_disconnect(usb3); in handle_ext_role_switch_states()
2506 usb3_set_mode(usb3, true); in handle_ext_role_switch_states()
2520 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in handle_role_switch_states() local
2521 struct device *host = usb3->host_dev; in handle_role_switch_states()
2522 enum usb_role cur_role = renesas_usb3_role_switch_get(usb3->role_sw); in handle_role_switch_states()
2526 usb3_set_mode(usb3, false); in handle_role_switch_states()
2529 usb3_set_mode(usb3, true); in handle_role_switch_states()
2539 struct renesas_usb3 *usb3 = usb_role_switch_get_drvdata(sw); in renesas_usb3_role_switch_set() local
2541 pm_runtime_get_sync(usb3_to_dev(usb3)); in renesas_usb3_role_switch_set()
2543 if (usb3->role_sw_by_connector) in renesas_usb3_role_switch_set()
2544 handle_ext_role_switch_states(usb3_to_dev(usb3), role); in renesas_usb3_role_switch_set()
2546 handle_role_switch_states(usb3_to_dev(usb3), role); in renesas_usb3_role_switch_set()
2548 pm_runtime_put(usb3_to_dev(usb3)); in renesas_usb3_role_switch_set()
2556 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in role_store() local
2559 if (!usb3->driver) in role_store()
2560 return -ENODEV; in role_store()
2562 if (usb3->forced_b_device) in role_store()
2563 return -EBUSY; in role_store()
2570 return -EINVAL; in role_store()
2572 if (new_mode_is_host == usb3_is_host(usb3)) in role_store()
2573 return -EINVAL; in role_store()
2575 usb3_mode_config(usb3, new_mode_is_host, usb3_is_a_device(usb3)); in role_store()
2583 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in role_show() local
2585 if (!usb3->driver) in role_show()
2586 return -ENODEV; in role_show()
2588 return sprintf(buf, "%s\n", usb3_is_host(usb3) ? "host" : "peripheral"); in role_show()
2594 struct renesas_usb3 *usb3 = s->private; in renesas_usb3_b_device_show() local
2596 seq_printf(s, "%d\n", usb3->forced_b_device); in renesas_usb3_b_device_show()
2603 return single_open(file, renesas_usb3_b_device_show, inode->i_private); in renesas_usb3_b_device_open()
2610 struct seq_file *s = file->private_data; in renesas_usb3_b_device_write()
2611 struct renesas_usb3 *usb3 = s->private; in renesas_usb3_b_device_write() local
2614 if (!usb3->driver) in renesas_usb3_b_device_write()
2615 return -ENODEV; in renesas_usb3_b_device_write()
2617 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) in renesas_usb3_b_device_write()
2618 return -EFAULT; in renesas_usb3_b_device_write()
2620 usb3->start_to_connect = false; in renesas_usb3_b_device_write()
2621 if (usb3->workaround_for_vbus && usb3->forced_b_device && in renesas_usb3_b_device_write()
2622 !strncmp(buf, "2", 1)) in renesas_usb3_b_device_write()
2623 usb3->start_to_connect = true; in renesas_usb3_b_device_write()
2625 usb3->forced_b_device = true; in renesas_usb3_b_device_write()
2627 usb3->forced_b_device = false; in renesas_usb3_b_device_write()
2629 if (usb3->workaround_for_vbus) in renesas_usb3_b_device_write()
2630 usb3_disconnect(usb3); in renesas_usb3_b_device_write()
2633 usb3_check_id(usb3); in renesas_usb3_b_device_write()
2646 static void renesas_usb3_debugfs_init(struct renesas_usb3 *usb3, in renesas_usb3_debugfs_init() argument
2649 usb3->dentry = debugfs_create_dir(dev_name(dev), usb_debug_root); in renesas_usb3_debugfs_init()
2651 debugfs_create_file("b_device", 0644, usb3->dentry, usb3, in renesas_usb3_debugfs_init()
2655 /*------- platform_driver ------------------------------------------------*/
2658 struct renesas_usb3 *usb3 = platform_get_drvdata(pdev); in renesas_usb3_remove() local
2660 debugfs_remove_recursive(usb3->dentry); in renesas_usb3_remove()
2661 device_remove_file(&pdev->dev, &dev_attr_role); in renesas_usb3_remove()
2663 cancel_work_sync(&usb3->role_work); in renesas_usb3_remove()
2664 usb_role_switch_unregister(usb3->role_sw); in renesas_usb3_remove()
2666 usb_del_gadget_udc(&usb3->gadget); in renesas_usb3_remove()
2667 reset_control_assert(usb3->usbp_rstc); in renesas_usb3_remove()
2668 renesas_usb3_dma_free_prd(usb3, &pdev->dev); in renesas_usb3_remove()
2670 __renesas_usb3_ep_free_request(usb3->ep0_req); in renesas_usb3_remove()
2671 pm_runtime_disable(&pdev->dev); in renesas_usb3_remove()
2674 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev, in renesas_usb3_init_ep() argument
2681 usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 / in renesas_usb3_init_ep()
2682 priv->ramsize_per_pipe + 1; in renesas_usb3_init_ep()
2684 if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES(usb3)) in renesas_usb3_init_ep()
2685 usb3->num_usb3_eps = USB3_MAX_NUM_PIPES(usb3); in renesas_usb3_init_ep()
2687 usb3->usb3_ep = devm_kcalloc(dev, in renesas_usb3_init_ep()
2688 usb3->num_usb3_eps, sizeof(*usb3_ep), in renesas_usb3_init_ep()
2690 if (!usb3->usb3_ep) in renesas_usb3_init_ep()
2691 return -ENOMEM; in renesas_usb3_init_ep()
2693 dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps); in renesas_usb3_init_ep()
2696 * - odd pipes = IN pipe in renesas_usb3_init_ep()
2697 * - even pipes = OUT pipe (except pipe 0) in renesas_usb3_init_ep()
2699 usb3_for_each_ep(usb3_ep, usb3, i) { in renesas_usb3_init_ep()
2700 snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i); in renesas_usb3_init_ep()
2701 usb3_ep->usb3 = usb3; in renesas_usb3_init_ep()
2702 usb3_ep->num = i; in renesas_usb3_init_ep()
2703 usb3_ep->ep.name = usb3_ep->ep_name; in renesas_usb3_init_ep()
2704 usb3_ep->ep.ops = &renesas_usb3_ep_ops; in renesas_usb3_init_ep()
2705 INIT_LIST_HEAD(&usb3_ep->queue); in renesas_usb3_init_ep()
2706 INIT_LIST_HEAD(&usb3_ep->ep.ep_list); in renesas_usb3_init_ep()
2709 usb3->gadget.ep0 = &usb3_ep->ep; in renesas_usb3_init_ep()
2710 usb_ep_set_maxpacket_limit(&usb3_ep->ep, in renesas_usb3_init_ep()
2712 usb3_ep->ep.caps.type_control = true; in renesas_usb3_init_ep()
2713 usb3_ep->ep.caps.dir_in = true; in renesas_usb3_init_ep()
2714 usb3_ep->ep.caps.dir_out = true; in renesas_usb3_init_ep()
2719 usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0); in renesas_usb3_init_ep()
2720 list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list); in renesas_usb3_init_ep()
2721 usb3_ep->ep.caps.type_bulk = true; in renesas_usb3_init_ep()
2722 usb3_ep->ep.caps.type_int = true; in renesas_usb3_init_ep()
2724 usb3_ep->ep.caps.dir_in = true; in renesas_usb3_init_ep()
2726 usb3_ep->ep.caps.dir_out = true; in renesas_usb3_init_ep()
2732 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev, in renesas_usb3_init_ram() argument
2737 u32 ramif[2], basead[2]; /* index 0 = for IN pipes */ in renesas_usb3_init_ram()
2746 * - all pipes = the same size as "ramsize_per_pipe" in renesas_usb3_init_ram()
2749 usb3_for_each_ep(usb3_ep, usb3, i) { in renesas_usb3_init_ram()
2752 if (usb3_ep->ep.caps.dir_in) { in renesas_usb3_init_ram()
2760 if (*cur_basead > priv->ramsize_per_ramif) in renesas_usb3_init_ram()
2765 val |= usb3_calc_ramarea(priv->ramsize_per_pipe); in renesas_usb3_init_ram()
2767 usb3_ep->rammap_val = val; in renesas_usb3_init_ram()
2769 dev_dbg(dev, "ep%2d: val = %08x, ramif = %d, base = %x\n", in renesas_usb3_init_ram()
2773 if (*cur_ramif + 1 == priv->num_ramif) { in renesas_usb3_init_ram()
2775 *cur_basead += priv->ramsize_per_pipe; in renesas_usb3_init_ram()
2804 .compatible = "renesas,r8a774c0-usb3-peri",
2807 .compatible = "renesas,r8a7795-usb3-peri",
2810 .compatible = "renesas,r8a77990-usb3-peri",
2813 .compatible = "renesas,rzv2m-usb3-peri",
2816 .compatible = "renesas,rcar-gen3-usb3-peri",
2837 struct renesas_usb3 *usb3; in renesas_usb3_probe() local
2841 priv = of_device_get_match_data(&pdev->dev); in renesas_usb3_probe()
2847 usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL); in renesas_usb3_probe()
2848 if (!usb3) in renesas_usb3_probe()
2849 return -ENOMEM; in renesas_usb3_probe()
2851 usb3->is_rzv2m = priv->is_rzv2m; in renesas_usb3_probe()
2853 usb3->reg = devm_platform_ioremap_resource(pdev, 0); in renesas_usb3_probe()
2854 if (IS_ERR(usb3->reg)) in renesas_usb3_probe()
2855 return PTR_ERR(usb3->reg); in renesas_usb3_probe()
2857 platform_set_drvdata(pdev, usb3); in renesas_usb3_probe()
2858 spin_lock_init(&usb3->lock); in renesas_usb3_probe()
2860 usb3->gadget.ops = &renesas_usb3_gadget_ops; in renesas_usb3_probe()
2861 usb3->gadget.name = udc_name; in renesas_usb3_probe()
2862 usb3->gadget.max_speed = USB_SPEED_SUPER; in renesas_usb3_probe()
2863 INIT_LIST_HEAD(&usb3->gadget.ep_list); in renesas_usb3_probe()
2864 ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv); in renesas_usb3_probe()
2867 renesas_usb3_init_ram(usb3, &pdev->dev, priv); in renesas_usb3_probe()
2869 ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0, in renesas_usb3_probe()
2870 dev_name(&pdev->dev), usb3); in renesas_usb3_probe()
2874 if (usb3->is_rzv2m) { in renesas_usb3_probe()
2875 struct rzv2m_usb3drd *ddata = dev_get_drvdata(pdev->dev.parent); in renesas_usb3_probe()
2877 usb3->drd_reg = ddata->reg; in renesas_usb3_probe()
2878 ret = devm_request_irq(&pdev->dev, ddata->drd_irq, in renesas_usb3_probe()
2880 dev_name(&pdev->dev), usb3); in renesas_usb3_probe()
2885 INIT_WORK(&usb3->extcon_work, renesas_usb3_extcon_work); in renesas_usb3_probe()
2886 usb3->extcon = devm_extcon_dev_allocate(&pdev->dev, renesas_usb3_cable); in renesas_usb3_probe()
2887 if (IS_ERR(usb3->extcon)) in renesas_usb3_probe()
2888 return PTR_ERR(usb3->extcon); in renesas_usb3_probe()
2890 ret = devm_extcon_dev_register(&pdev->dev, usb3->extcon); in renesas_usb3_probe()
2892 dev_err(&pdev->dev, "Failed to register extcon\n"); in renesas_usb3_probe()
2897 usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL); in renesas_usb3_probe()
2898 if (!usb3->ep0_req) in renesas_usb3_probe()
2899 return -ENOMEM; in renesas_usb3_probe()
2901 ret = renesas_usb3_dma_alloc_prd(usb3, &pdev->dev); in renesas_usb3_probe()
2909 usb3->phy = devm_phy_optional_get(&pdev->dev, "usb"); in renesas_usb3_probe()
2910 if (IS_ERR(usb3->phy)) { in renesas_usb3_probe()
2911 ret = PTR_ERR(usb3->phy); in renesas_usb3_probe()
2915 usb3->usbp_rstc = devm_reset_control_get_optional_shared(&pdev->dev, in renesas_usb3_probe()
2917 if (IS_ERR(usb3->usbp_rstc)) { in renesas_usb3_probe()
2918 ret = PTR_ERR(usb3->usbp_rstc); in renesas_usb3_probe()
2922 reset_control_deassert(usb3->usbp_rstc); in renesas_usb3_probe()
2924 pm_runtime_enable(&pdev->dev); in renesas_usb3_probe()
2925 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget); in renesas_usb3_probe()
2929 ret = device_create_file(&pdev->dev, &dev_attr_role); in renesas_usb3_probe()
2933 if (device_property_read_bool(&pdev->dev, "usb-role-switch")) { in renesas_usb3_probe()
2934 usb3->role_sw_by_connector = true; in renesas_usb3_probe()
2935 renesas_usb3_role_switch_desc.fwnode = dev_fwnode(&pdev->dev); in renesas_usb3_probe()
2938 renesas_usb3_role_switch_desc.driver_data = usb3; in renesas_usb3_probe()
2940 INIT_WORK(&usb3->role_work, renesas_usb3_role_work); in renesas_usb3_probe()
2941 usb3->role_sw = usb_role_switch_register(&pdev->dev, in renesas_usb3_probe()
2943 if (!IS_ERR(usb3->role_sw)) { in renesas_usb3_probe()
2944 usb3->host_dev = usb_of_get_companion_dev(&pdev->dev); in renesas_usb3_probe()
2945 if (!usb3->host_dev) { in renesas_usb3_probe()
2947 usb_role_switch_unregister(usb3->role_sw); in renesas_usb3_probe()
2948 usb3->role_sw = NULL; in renesas_usb3_probe()
2951 usb3->role_sw = NULL; in renesas_usb3_probe()
2954 usb3->workaround_for_vbus = priv->workaround_for_vbus; in renesas_usb3_probe()
2956 renesas_usb3_debugfs_init(usb3, &pdev->dev); in renesas_usb3_probe()
2958 dev_info(&pdev->dev, "probed%s\n", usb3->phy ? " with phy" : ""); in renesas_usb3_probe()
2963 usb_del_gadget_udc(&usb3->gadget); in renesas_usb3_probe()
2966 reset_control_assert(usb3->usbp_rstc); in renesas_usb3_probe()
2969 renesas_usb3_dma_free_prd(usb3, &pdev->dev); in renesas_usb3_probe()
2972 __renesas_usb3_ep_free_request(usb3->ep0_req); in renesas_usb3_probe()
2980 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in renesas_usb3_suspend() local
2983 if (!usb3->driver) in renesas_usb3_suspend()
2986 renesas_usb3_stop_controller(usb3); in renesas_usb3_suspend()
2987 if (usb3->phy) in renesas_usb3_suspend()
2988 phy_exit(usb3->phy); in renesas_usb3_suspend()
2996 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in renesas_usb3_resume() local
2999 if (!usb3->driver) in renesas_usb3_resume()
3002 if (usb3->phy) in renesas_usb3_resume()
3003 phy_init(usb3->phy); in renesas_usb3_resume()
3005 renesas_usb3_init_controller(usb3); in renesas_usb3_resume()
3025 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");