Lines Matching +full:r8a774c0 +full:- +full:usb3 +full:- +full:peri

1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas USB3.0 Peripheral driver (USB gadget)
5 * Copyright (C) 2015-2017 Renesas Electronics Corporation
10 #include <linux/dma-mapping.h>
12 #include <linux/extcon-provider.h>
35 #define USB3_DMA_CH0_CON(n) (0x030 + ((n) - 1) * 0x10) /* n = 1 to 4 */
36 #define USB3_DMA_CH0_PRD_ADR(n) (0x034 + ((n) - 1) * 0x10) /* n = 1 to 4 */
75 #define AXI_INT_PRDEN_CLR_STA_SHIFT(n) (16 + (n) - 1)
76 #define AXI_INT_PRDERR_STA_SHIFT(n) (0 + (n) - 1)
262 * To avoid double-meaning of "0" (xferred 65536 bytes or received zlp if
312 struct renesas_usb3 *usb3; member
370 #define renesas_usb3_to_gadget(renesas_usb3) (&renesas_usb3->gadget)
371 #define usb3_to_dev(_usb3) (_usb3->gadget.dev.parent)
374 #define usb3_ep_to_usb3(_usb3_ep) (_usb3_ep->usb3)
378 #define usb3_get_ep(usb3, n) ((usb3)->usb3_ep + (n)) argument
379 #define usb3_for_each_ep(usb3_ep, usb3, i) \ argument
380 for ((i) = 0, usb3_ep = usb3_get_ep(usb3, (i)); \
381 (i) < (usb3)->num_usb3_eps; \
382 (i)++, usb3_ep = usb3_get_ep(usb3, (i)))
384 #define usb3_get_dma(usb3, i) (&(usb3)->dma[i]) argument
385 #define usb3_for_each_dma(usb3, dma, i) \ argument
386 for ((i) = 0, dma = usb3_get_dma((usb3), (i)); \
388 (i)++, dma = usb3_get_dma((usb3), (i)))
396 static void usb3_write(struct renesas_usb3 *usb3, u32 data, u32 offs) in usb3_write() argument
398 iowrite32(data, usb3->reg + offs); in usb3_write()
401 static u32 usb3_read(struct renesas_usb3 *usb3, u32 offs) in usb3_read() argument
403 return ioread32(usb3->reg + offs); in usb3_read()
406 static void usb3_set_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs) in usb3_set_bit() argument
408 u32 val = usb3_read(usb3, offs); in usb3_set_bit()
411 usb3_write(usb3, val, offs); in usb3_set_bit()
414 static void usb3_clear_bit(struct renesas_usb3 *usb3, u32 bits, u32 offs) in usb3_clear_bit() argument
416 u32 val = usb3_read(usb3, offs); in usb3_clear_bit()
419 usb3_write(usb3, val, offs); in usb3_clear_bit()
422 static int usb3_wait(struct renesas_usb3 *usb3, u32 reg, u32 mask, in usb3_wait() argument
428 if ((usb3_read(usb3, reg) & mask) == expected) in usb3_wait()
433 dev_dbg(usb3_to_dev(usb3), "%s: timed out (%8x, %08x, %08x)\n", in usb3_wait()
436 return -EBUSY; in usb3_wait()
441 struct renesas_usb3 *usb3 = container_of(work, struct renesas_usb3, in renesas_usb3_extcon_work() local
444 extcon_set_state_sync(usb3->extcon, EXTCON_USB_HOST, usb3->extcon_host); in renesas_usb3_extcon_work()
445 extcon_set_state_sync(usb3->extcon, EXTCON_USB, usb3->extcon_usb); in renesas_usb3_extcon_work()
448 static void usb3_enable_irq_1(struct renesas_usb3 *usb3, u32 bits) in usb3_enable_irq_1() argument
450 usb3_set_bit(usb3, bits, USB3_USB_INT_ENA_1); in usb3_enable_irq_1()
453 static void usb3_disable_irq_1(struct renesas_usb3 *usb3, u32 bits) in usb3_disable_irq_1() argument
455 usb3_clear_bit(usb3, bits, USB3_USB_INT_ENA_1); in usb3_disable_irq_1()
458 static void usb3_enable_pipe_irq(struct renesas_usb3 *usb3, int num) in usb3_enable_pipe_irq() argument
460 usb3_set_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2); in usb3_enable_pipe_irq()
463 static void usb3_disable_pipe_irq(struct renesas_usb3 *usb3, int num) in usb3_disable_pipe_irq() argument
465 usb3_clear_bit(usb3, USB_INT_2_PIPE(num), USB3_USB_INT_ENA_2); in usb3_disable_pipe_irq()
468 static bool usb3_is_host(struct renesas_usb3 *usb3) in usb3_is_host() argument
470 return !(usb3_read(usb3, USB3_DRD_CON) & DRD_CON_PERI_CON); in usb3_is_host()
473 static void usb3_init_axi_bridge(struct renesas_usb3 *usb3) in usb3_init_axi_bridge() argument
476 usb3_write(usb3, ~0, USB3_DMA_INT_STA); in usb3_init_axi_bridge()
477 usb3_write(usb3, 0, USB3_DMA_INT_ENA); in usb3_init_axi_bridge()
478 usb3_set_bit(usb3, AXI_INT_DMAINT | AXI_INT_EPCINT, USB3_AXI_INT_ENA); in usb3_init_axi_bridge()
481 static void usb3_init_epc_registers(struct renesas_usb3 *usb3) in usb3_init_epc_registers() argument
483 usb3_write(usb3, ~0, USB3_USB_INT_STA_1); in usb3_init_epc_registers()
484 if (!usb3->workaround_for_vbus) in usb3_init_epc_registers()
485 usb3_enable_irq_1(usb3, USB_INT_1_VBUS_CNG); in usb3_init_epc_registers()
488 static bool usb3_wakeup_usb2_phy(struct renesas_usb3 *usb3) in usb3_wakeup_usb2_phy() argument
490 if (!(usb3_read(usb3, USB3_USB20_CON) & USB20_CON_B2_SUSPEND)) in usb3_wakeup_usb2_phy()
493 usb3_clear_bit(usb3, USB20_CON_B2_SUSPEND, USB3_USB20_CON); in usb3_wakeup_usb2_phy()
494 usb3_enable_irq_1(usb3, USB_INT_1_B2_RSUM); in usb3_wakeup_usb2_phy()
499 static void usb3_usb2_pullup(struct renesas_usb3 *usb3, int pullup) in usb3_usb2_pullup() argument
503 if (usb3->softconnect && pullup) in usb3_usb2_pullup()
504 usb3_set_bit(usb3, bits, USB3_USB20_CON); in usb3_usb2_pullup()
506 usb3_clear_bit(usb3, bits, USB3_USB20_CON); in usb3_usb2_pullup()
509 static void usb3_set_test_mode(struct renesas_usb3 *usb3) in usb3_set_test_mode() argument
511 u32 val = usb3_read(usb3, USB3_USB20_CON); in usb3_set_test_mode()
514 val |= USB20_CON_B2_TSTMOD(usb3->test_mode); in usb3_set_test_mode()
515 usb3_write(usb3, val | USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON); in usb3_set_test_mode()
516 if (!usb3->test_mode) in usb3_set_test_mode()
517 usb3_clear_bit(usb3, USB20_CON_B2_TSTMOD_EN, USB3_USB20_CON); in usb3_set_test_mode()
520 static void usb3_start_usb2_connection(struct renesas_usb3 *usb3) in usb3_start_usb2_connection() argument
522 usb3->disabled_count++; in usb3_start_usb2_connection()
523 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_start_usb2_connection()
524 usb3_set_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON); in usb3_start_usb2_connection()
525 usb3_usb2_pullup(usb3, 1); in usb3_start_usb2_connection()
528 static int usb3_is_usb3_phy_in_u3(struct renesas_usb3 *usb3) in usb3_is_usb3_phy_in_u3() argument
530 return usb3_read(usb3, USB3_USB30_CON) & USB30_CON_POW_SEL_IN_U3; in usb3_is_usb3_phy_in_u3()
533 static bool usb3_wakeup_usb3_phy(struct renesas_usb3 *usb3) in usb3_wakeup_usb3_phy() argument
535 if (!usb3_is_usb3_phy_in_u3(usb3)) in usb3_wakeup_usb3_phy()
538 usb3_set_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON); in usb3_wakeup_usb3_phy()
539 usb3_enable_irq_1(usb3, USB_INT_1_B3_PLLWKUP); in usb3_wakeup_usb3_phy()
544 static u16 usb3_feature_get_un_enabled(struct renesas_usb3 *usb3) in usb3_feature_get_un_enabled() argument
548 u32 val = usb3_read(usb3, USB3_SSIFCMD); in usb3_feature_get_un_enabled()
560 static void usb3_feature_u2_enable(struct renesas_usb3 *usb3, bool enable) in usb3_feature_u2_enable() argument
566 usb3_clear_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u2_enable()
568 usb3_set_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u2_enable()
571 static void usb3_feature_u1_enable(struct renesas_usb3 *usb3, bool enable) in usb3_feature_u1_enable() argument
577 usb3_clear_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u1_enable()
579 usb3_set_bit(usb3, bits, USB3_SSIFCMD); in usb3_feature_u1_enable()
582 static void usb3_start_operation_for_usb3(struct renesas_usb3 *usb3) in usb3_start_operation_for_usb3() argument
584 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_start_operation_for_usb3()
585 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON); in usb3_start_operation_for_usb3()
586 usb3_set_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON); in usb3_start_operation_for_usb3()
589 static void usb3_start_usb3_connection(struct renesas_usb3 *usb3) in usb3_start_usb3_connection() argument
591 usb3_start_operation_for_usb3(usb3); in usb3_start_usb3_connection()
592 usb3_set_bit(usb3, USB_COM_CON_RX_DETECTION, USB3_USB_COM_CON); in usb3_start_usb3_connection()
594 usb3_enable_irq_1(usb3, USB_INT_1_B3_LUPSUCS | USB_INT_1_B3_DISABLE | in usb3_start_usb3_connection()
598 static void usb3_stop_usb3_connection(struct renesas_usb3 *usb3) in usb3_stop_usb3_connection() argument
600 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON); in usb3_stop_usb3_connection()
603 static void usb3_transition_to_default_state(struct renesas_usb3 *usb3, in usb3_transition_to_default_state() argument
606 usb3_set_bit(usb3, USB_INT_2_PIPE(0), USB3_USB_INT_ENA_2); in usb3_transition_to_default_state()
607 usb3_write(usb3, P0_INT_ALL_BITS, USB3_P0_INT_STA); in usb3_transition_to_default_state()
608 usb3_set_bit(usb3, P0_INT_ALL_BITS, USB3_P0_INT_ENA); in usb3_transition_to_default_state()
611 usb3_enable_irq_1(usb3, USB_INT_1_B3_WRMRST | in usb3_transition_to_default_state()
614 usb3_enable_irq_1(usb3, USB_INT_1_B2_SPND | in usb3_transition_to_default_state()
618 static void usb3_connect(struct renesas_usb3 *usb3) in usb3_connect() argument
620 if (usb3_wakeup_usb3_phy(usb3)) in usb3_connect()
621 usb3_start_usb3_connection(usb3); in usb3_connect()
624 static void usb3_reset_epc(struct renesas_usb3 *usb3) in usb3_reset_epc() argument
626 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON); in usb3_reset_epc()
627 usb3_clear_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_reset_epc()
628 usb3_set_bit(usb3, USB_COM_CON_PIPE_CLR, USB3_USB_COM_CON); in usb3_reset_epc()
629 usb3->test_mode = 0; in usb3_reset_epc()
630 usb3_set_test_mode(usb3); in usb3_reset_epc()
633 static void usb3_disconnect(struct renesas_usb3 *usb3) in usb3_disconnect() argument
635 usb3->disabled_count = 0; in usb3_disconnect()
636 usb3_usb2_pullup(usb3, 0); in usb3_disconnect()
637 usb3_clear_bit(usb3, USB30_CON_B3_CONNECT, USB3_USB30_CON); in usb3_disconnect()
638 usb3_reset_epc(usb3); in usb3_disconnect()
639 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM | USB_INT_1_B3_PLLWKUP | in usb3_disconnect()
644 usb3_clear_bit(usb3, USB_COM_CON_SPD_MODE, USB3_USB_COM_CON); in usb3_disconnect()
645 usb3_init_epc_registers(usb3); in usb3_disconnect()
647 if (usb3->driver) in usb3_disconnect()
648 usb3->driver->disconnect(&usb3->gadget); in usb3_disconnect()
651 static void usb3_check_vbus(struct renesas_usb3 *usb3) in usb3_check_vbus() argument
653 if (usb3->workaround_for_vbus) { in usb3_check_vbus()
654 usb3_connect(usb3); in usb3_check_vbus()
656 usb3->extcon_usb = !!(usb3_read(usb3, USB3_USB_STA) & in usb3_check_vbus()
658 if (usb3->extcon_usb) in usb3_check_vbus()
659 usb3_connect(usb3); in usb3_check_vbus()
661 usb3_disconnect(usb3); in usb3_check_vbus()
663 schedule_work(&usb3->extcon_work); in usb3_check_vbus()
669 struct renesas_usb3 *usb3 = in renesas_usb3_role_work() local
672 usb_role_switch_set_role(usb3->role_sw, usb3->role); in renesas_usb3_role_work()
675 static void usb3_set_mode(struct renesas_usb3 *usb3, bool host) in usb3_set_mode() argument
678 usb3_clear_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON); in usb3_set_mode()
680 usb3_set_bit(usb3, DRD_CON_PERI_CON, USB3_DRD_CON); in usb3_set_mode()
683 static void usb3_set_mode_by_role_sw(struct renesas_usb3 *usb3, bool host) in usb3_set_mode_by_role_sw() argument
685 if (usb3->role_sw) { in usb3_set_mode_by_role_sw()
686 usb3->role = host ? USB_ROLE_HOST : USB_ROLE_DEVICE; in usb3_set_mode_by_role_sw()
687 schedule_work(&usb3->role_work); in usb3_set_mode_by_role_sw()
689 usb3_set_mode(usb3, host); in usb3_set_mode_by_role_sw()
693 static void usb3_vbus_out(struct renesas_usb3 *usb3, bool enable) in usb3_vbus_out() argument
696 usb3_set_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON); in usb3_vbus_out()
698 usb3_clear_bit(usb3, DRD_CON_VBOUT, USB3_DRD_CON); in usb3_vbus_out()
701 static void usb3_mode_config(struct renesas_usb3 *usb3, bool host, bool a_dev) in usb3_mode_config() argument
705 spin_lock_irqsave(&usb3->lock, flags); in usb3_mode_config()
706 if (!usb3->role_sw_by_connector || in usb3_mode_config()
707 usb3->connection_state != USB_ROLE_NONE) { in usb3_mode_config()
708 usb3_set_mode_by_role_sw(usb3, host); in usb3_mode_config()
709 usb3_vbus_out(usb3, a_dev); in usb3_mode_config()
711 /* for A-Peripheral or forced B-device mode */ in usb3_mode_config()
712 if ((!host && a_dev) || usb3->start_to_connect) in usb3_mode_config()
713 usb3_connect(usb3); in usb3_mode_config()
714 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_mode_config()
717 static bool usb3_is_a_device(struct renesas_usb3 *usb3) in usb3_is_a_device() argument
719 return !(usb3_read(usb3, USB3_USB_OTG_STA) & USB_OTG_IDMON); in usb3_is_a_device()
722 static void usb3_check_id(struct renesas_usb3 *usb3) in usb3_check_id() argument
724 usb3->extcon_host = usb3_is_a_device(usb3); in usb3_check_id()
726 if ((!usb3->role_sw_by_connector && usb3->extcon_host && in usb3_check_id()
727 !usb3->forced_b_device) || usb3->connection_state == USB_ROLE_HOST) in usb3_check_id()
728 usb3_mode_config(usb3, true, true); in usb3_check_id()
730 usb3_mode_config(usb3, false, false); in usb3_check_id()
732 schedule_work(&usb3->extcon_work); in usb3_check_id()
735 static void renesas_usb3_init_controller(struct renesas_usb3 *usb3) in renesas_usb3_init_controller() argument
737 usb3_init_axi_bridge(usb3); in renesas_usb3_init_controller()
738 usb3_init_epc_registers(usb3); in renesas_usb3_init_controller()
739 usb3_set_bit(usb3, USB_COM_CON_PN_WDATAIF_NL | in renesas_usb3_init_controller()
742 usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_STA); in renesas_usb3_init_controller()
743 usb3_write(usb3, USB_OTG_IDMON, USB3_USB_OTG_INT_ENA); in renesas_usb3_init_controller()
745 usb3_check_id(usb3); in renesas_usb3_init_controller()
746 usb3_check_vbus(usb3); in renesas_usb3_init_controller()
749 static void renesas_usb3_stop_controller(struct renesas_usb3 *usb3) in renesas_usb3_stop_controller() argument
751 usb3_disconnect(usb3); in renesas_usb3_stop_controller()
752 usb3_write(usb3, 0, USB3_P0_INT_ENA); in renesas_usb3_stop_controller()
753 usb3_write(usb3, 0, USB3_USB_OTG_INT_ENA); in renesas_usb3_stop_controller()
754 usb3_write(usb3, 0, USB3_USB_INT_ENA_1); in renesas_usb3_stop_controller()
755 usb3_write(usb3, 0, USB3_USB_INT_ENA_2); in renesas_usb3_stop_controller()
756 usb3_write(usb3, 0, USB3_AXI_INT_ENA); in renesas_usb3_stop_controller()
759 static void usb3_irq_epc_int_1_pll_wakeup(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_pll_wakeup() argument
761 usb3_disable_irq_1(usb3, USB_INT_1_B3_PLLWKUP); in usb3_irq_epc_int_1_pll_wakeup()
762 usb3_clear_bit(usb3, USB30_CON_B3_PLLWAKE, USB3_USB30_CON); in usb3_irq_epc_int_1_pll_wakeup()
763 usb3_start_usb3_connection(usb3); in usb3_irq_epc_int_1_pll_wakeup()
766 static void usb3_irq_epc_int_1_linkup_success(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_linkup_success() argument
768 usb3_transition_to_default_state(usb3, true); in usb3_irq_epc_int_1_linkup_success()
771 static void usb3_irq_epc_int_1_resume(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_resume() argument
773 usb3_disable_irq_1(usb3, USB_INT_1_B2_RSUM); in usb3_irq_epc_int_1_resume()
774 usb3_start_usb2_connection(usb3); in usb3_irq_epc_int_1_resume()
775 usb3_transition_to_default_state(usb3, false); in usb3_irq_epc_int_1_resume()
778 static void usb3_irq_epc_int_1_suspend(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_suspend() argument
780 usb3_disable_irq_1(usb3, USB_INT_1_B2_SPND); in usb3_irq_epc_int_1_suspend()
782 if (usb3->gadget.speed != USB_SPEED_UNKNOWN && in usb3_irq_epc_int_1_suspend()
783 usb3->gadget.state != USB_STATE_NOTATTACHED) { in usb3_irq_epc_int_1_suspend()
784 if (usb3->driver && usb3->driver->suspend) in usb3_irq_epc_int_1_suspend()
785 usb3->driver->suspend(&usb3->gadget); in usb3_irq_epc_int_1_suspend()
786 usb_gadget_set_state(&usb3->gadget, USB_STATE_SUSPENDED); in usb3_irq_epc_int_1_suspend()
790 static void usb3_irq_epc_int_1_disable(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_disable() argument
792 usb3_stop_usb3_connection(usb3); in usb3_irq_epc_int_1_disable()
793 if (usb3_wakeup_usb2_phy(usb3)) in usb3_irq_epc_int_1_disable()
794 usb3_irq_epc_int_1_resume(usb3); in usb3_irq_epc_int_1_disable()
797 static void usb3_irq_epc_int_1_bus_reset(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_bus_reset() argument
799 usb3_reset_epc(usb3); in usb3_irq_epc_int_1_bus_reset()
800 if (usb3->disabled_count < 3) in usb3_irq_epc_int_1_bus_reset()
801 usb3_start_usb3_connection(usb3); in usb3_irq_epc_int_1_bus_reset()
803 usb3_start_usb2_connection(usb3); in usb3_irq_epc_int_1_bus_reset()
806 static void usb3_irq_epc_int_1_vbus_change(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_vbus_change() argument
808 usb3_check_vbus(usb3); in usb3_irq_epc_int_1_vbus_change()
811 static void usb3_irq_epc_int_1_hot_reset(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_hot_reset() argument
813 usb3_reset_epc(usb3); in usb3_irq_epc_int_1_hot_reset()
814 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_irq_epc_int_1_hot_reset()
817 usb3_set_bit(usb3, USB30_CON_B3_HOTRST_CMP, USB3_USB30_CON); in usb3_irq_epc_int_1_hot_reset()
820 static void usb3_irq_epc_int_1_warm_reset(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_warm_reset() argument
822 usb3_reset_epc(usb3); in usb3_irq_epc_int_1_warm_reset()
823 usb3_set_bit(usb3, USB_COM_CON_EP0_EN, USB3_USB_COM_CON); in usb3_irq_epc_int_1_warm_reset()
825 usb3_start_operation_for_usb3(usb3); in usb3_irq_epc_int_1_warm_reset()
826 usb3_enable_irq_1(usb3, USB_INT_1_SPEED); in usb3_irq_epc_int_1_warm_reset()
829 static void usb3_irq_epc_int_1_speed(struct renesas_usb3 *usb3) in usb3_irq_epc_int_1_speed() argument
831 u32 speed = usb3_read(usb3, USB3_USB_STA) & USB_STA_SPEED_MASK; in usb3_irq_epc_int_1_speed()
835 usb3->gadget.speed = USB_SPEED_SUPER; in usb3_irq_epc_int_1_speed()
836 usb3->gadget.ep0->maxpacket = USB3_EP0_SS_MAX_PACKET_SIZE; in usb3_irq_epc_int_1_speed()
839 usb3->gadget.speed = USB_SPEED_HIGH; in usb3_irq_epc_int_1_speed()
840 usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE; in usb3_irq_epc_int_1_speed()
843 usb3->gadget.speed = USB_SPEED_FULL; in usb3_irq_epc_int_1_speed()
844 usb3->gadget.ep0->maxpacket = USB3_EP0_HSFS_MAX_PACKET_SIZE; in usb3_irq_epc_int_1_speed()
847 usb3->gadget.speed = USB_SPEED_UNKNOWN; in usb3_irq_epc_int_1_speed()
852 static void usb3_irq_epc_int_1(struct renesas_usb3 *usb3, u32 int_sta_1) in usb3_irq_epc_int_1() argument
855 usb3_irq_epc_int_1_pll_wakeup(usb3); in usb3_irq_epc_int_1()
858 usb3_irq_epc_int_1_linkup_success(usb3); in usb3_irq_epc_int_1()
861 usb3_irq_epc_int_1_hot_reset(usb3); in usb3_irq_epc_int_1()
864 usb3_irq_epc_int_1_warm_reset(usb3); in usb3_irq_epc_int_1()
867 usb3_irq_epc_int_1_disable(usb3); in usb3_irq_epc_int_1()
870 usb3_irq_epc_int_1_bus_reset(usb3); in usb3_irq_epc_int_1()
873 usb3_irq_epc_int_1_resume(usb3); in usb3_irq_epc_int_1()
876 usb3_irq_epc_int_1_suspend(usb3); in usb3_irq_epc_int_1()
879 usb3_irq_epc_int_1_speed(usb3); in usb3_irq_epc_int_1()
882 usb3_irq_epc_int_1_vbus_change(usb3); in usb3_irq_epc_int_1()
888 return list_first_entry_or_null(&usb3_ep->queue, in __usb3_get_request()
895 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_get_request() local
899 spin_lock_irqsave(&usb3->lock, flags); in usb3_get_request()
901 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_get_request()
910 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in __usb3_request_done() local
912 dev_dbg(usb3_to_dev(usb3), "giveback: ep%2d, %u, %u, %d\n", in __usb3_request_done()
913 usb3_ep->num, usb3_req->req.length, usb3_req->req.actual, in __usb3_request_done()
915 usb3_req->req.status = status; in __usb3_request_done()
916 usb3_ep->started = false; in __usb3_request_done()
917 list_del_init(&usb3_req->queue); in __usb3_request_done()
918 spin_unlock(&usb3->lock); in __usb3_request_done()
919 usb_gadget_giveback_request(&usb3_ep->ep, &usb3_req->req); in __usb3_request_done()
920 spin_lock(&usb3->lock); in __usb3_request_done()
926 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_request_done() local
929 spin_lock_irqsave(&usb3->lock, flags); in usb3_request_done()
931 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_request_done()
934 static void usb3_irq_epc_pipe0_status_end(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0_status_end() argument
936 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_irq_epc_pipe0_status_end()
941 if (usb3->test_mode) in usb3_irq_epc_pipe0_status_end()
942 usb3_set_test_mode(usb3); in usb3_irq_epc_pipe0_status_end()
945 static void usb3_get_setup_data(struct renesas_usb3 *usb3, in usb3_get_setup_data() argument
948 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_get_setup_data()
951 *data++ = usb3_read(usb3, USB3_STUP_DAT_0); in usb3_get_setup_data()
952 *data = usb3_read(usb3, USB3_STUP_DAT_1); in usb3_get_setup_data()
955 usb3_ep->dir_in = !!(ctrl->bRequestType & USB_DIR_IN); in usb3_get_setup_data()
958 static void usb3_set_p0_con_update_res(struct renesas_usb3 *usb3, u32 res) in usb3_set_p0_con_update_res() argument
960 u32 val = usb3_read(usb3, USB3_P0_CON); in usb3_set_p0_con_update_res()
964 usb3_write(usb3, val, USB3_P0_CON); in usb3_set_p0_con_update_res()
967 static void usb3_set_p0_con_for_ctrl_read_data(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_read_data() argument
969 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY | in usb3_set_p0_con_for_ctrl_read_data()
974 static void usb3_set_p0_con_for_ctrl_read_status(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_read_status() argument
976 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL | in usb3_set_p0_con_for_ctrl_read_status()
981 static void usb3_set_p0_con_for_ctrl_write_data(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_write_data() argument
983 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY | in usb3_set_p0_con_for_ctrl_write_data()
988 static void usb3_set_p0_con_for_ctrl_write_status(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_ctrl_write_status() argument
990 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL | in usb3_set_p0_con_for_ctrl_write_status()
995 static void usb3_set_p0_con_for_no_data(struct renesas_usb3 *usb3) in usb3_set_p0_con_for_no_data() argument
997 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_NORMAL | in usb3_set_p0_con_for_no_data()
1002 static void usb3_set_p0_con_stall(struct renesas_usb3 *usb3) in usb3_set_p0_con_stall() argument
1004 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_STALL | in usb3_set_p0_con_stall()
1009 static void usb3_set_p0_con_stop(struct renesas_usb3 *usb3) in usb3_set_p0_con_stop() argument
1011 usb3_set_p0_con_update_res(usb3, P0_CON_ST_RES_FORCE_NRDY | in usb3_set_p0_con_stop()
1016 static int usb3_pn_change(struct renesas_usb3 *usb3, int num) in usb3_pn_change() argument
1018 if (num == 0 || num > usb3->num_usb3_eps) in usb3_pn_change()
1019 return -ENXIO; in usb3_pn_change()
1021 usb3_write(usb3, num, USB3_PIPE_COM); in usb3_pn_change()
1026 static void usb3_set_pn_con_update_res(struct renesas_usb3 *usb3, u32 res) in usb3_set_pn_con_update_res() argument
1028 u32 val = usb3_read(usb3, USB3_PN_CON); in usb3_set_pn_con_update_res()
1033 usb3_write(usb3, val, USB3_PN_CON); in usb3_set_pn_con_update_res()
1036 static void usb3_pn_start(struct renesas_usb3 *usb3) in usb3_pn_start() argument
1038 usb3_set_pn_con_update_res(usb3, PN_CON_RES_NORMAL); in usb3_pn_start()
1041 static void usb3_pn_stop(struct renesas_usb3 *usb3) in usb3_pn_stop() argument
1043 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_NRDY); in usb3_pn_stop()
1046 static void usb3_pn_stall(struct renesas_usb3 *usb3) in usb3_pn_stall() argument
1048 usb3_set_pn_con_update_res(usb3, PN_CON_RES_FORCE_STALL); in usb3_pn_stall()
1051 static int usb3_pn_con_clear(struct renesas_usb3 *usb3) in usb3_pn_con_clear() argument
1053 usb3_set_bit(usb3, PN_CON_CLR, USB3_PN_CON); in usb3_pn_con_clear()
1055 return usb3_wait(usb3, USB3_PN_CON, PN_CON_CLR, 0); in usb3_pn_con_clear()
1061 struct usb_request *req = &usb3_req->req; in usb3_is_transfer_complete()
1063 if ((!req->zero && req->actual == req->length) || in usb3_is_transfer_complete()
1064 (req->actual % usb3_ep->ep.maxpacket) || (req->length == 0)) in usb3_is_transfer_complete()
1072 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_wait_pipe_status() local
1073 u32 sta_reg = usb3_ep->num ? USB3_PN_STA : USB3_P0_STA; in usb3_wait_pipe_status()
1075 return usb3_wait(usb3, sta_reg, mask, mask); in usb3_wait_pipe_status()
1081 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_px_con_send() local
1082 u32 con_reg = usb3_ep->num ? USB3_PN_CON : USB3_P0_CON; in usb3_set_px_con_send()
1083 u32 val = usb3_read(usb3, con_reg); in usb3_set_px_con_send()
1086 val |= (usb3_ep->num && last) ? PN_CON_LAST : 0; in usb3_set_px_con_send()
1087 usb3_write(usb3, val, con_reg); in usb3_set_px_con_send()
1094 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_write_pipe() local
1096 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual, in usb3_write_pipe()
1097 usb3_ep->ep.maxpacket); in usb3_write_pipe()
1098 u8 *buf = usb3_req->req.buf + usb3_req->req.actual; in usb3_write_pipe()
1103 return -EBUSY; in usb3_write_pipe()
1106 usb3_req->req.actual += len; in usb3_write_pipe()
1110 iowrite32_rep(usb3->reg + fifo_reg, buf, len / 4); in usb3_write_pipe()
1118 usb3_write(usb3, tmp, fifo_reg); in usb3_write_pipe()
1126 return is_last ? 0 : -EAGAIN; in usb3_write_pipe()
1131 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_get_received_length() local
1132 u32 lng_reg = usb3_ep->num ? USB3_PN_LNG : USB3_P0_LNG; in usb3_get_received_length()
1134 return usb3_read(usb3, lng_reg); in usb3_get_received_length()
1140 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_read_pipe() local
1142 int len = min_t(unsigned, usb3_req->req.length - usb3_req->req.actual, in usb3_read_pipe()
1144 u8 *buf = usb3_req->req.buf + usb3_req->req.actual; in usb3_read_pipe()
1151 usb3_req->req.actual += len; in usb3_read_pipe()
1155 ioread32_rep(usb3->reg + fifo_reg, buf, len / 4); in usb3_read_pipe()
1161 tmp = usb3_read(usb3, fifo_reg); in usb3_read_pipe()
1166 return usb3_is_transfer_complete(usb3_ep, usb3_req) ? 0 : -EAGAIN; in usb3_read_pipe()
1172 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_status_stage() local
1174 if (usb3_ep->dir_in) { in usb3_set_status_stage()
1175 usb3_set_p0_con_for_ctrl_read_status(usb3); in usb3_set_status_stage()
1177 if (!usb3_req->req.length) in usb3_set_status_stage()
1178 usb3_set_p0_con_for_no_data(usb3); in usb3_set_status_stage()
1180 usb3_set_p0_con_for_ctrl_write_status(usb3); in usb3_set_status_stage()
1189 if (usb3_ep->dir_in) in usb3_p0_xfer()
1201 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_start_pipe0() local
1203 if (usb3_ep->started) in usb3_start_pipe0()
1206 usb3_ep->started = true; in usb3_start_pipe0()
1208 if (usb3_ep->dir_in) { in usb3_start_pipe0()
1209 usb3_set_bit(usb3, P0_MOD_DIR, USB3_P0_MOD); in usb3_start_pipe0()
1210 usb3_set_p0_con_for_ctrl_read_data(usb3); in usb3_start_pipe0()
1212 usb3_clear_bit(usb3, P0_MOD_DIR, USB3_P0_MOD); in usb3_start_pipe0()
1213 if (usb3_req->req.length) in usb3_start_pipe0()
1214 usb3_set_p0_con_for_ctrl_write_data(usb3); in usb3_start_pipe0()
1220 static void usb3_enable_dma_pipen(struct renesas_usb3 *usb3) in usb3_enable_dma_pipen() argument
1222 usb3_set_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON); in usb3_enable_dma_pipen()
1225 static void usb3_disable_dma_pipen(struct renesas_usb3 *usb3) in usb3_disable_dma_pipen() argument
1227 usb3_clear_bit(usb3, PN_CON_DATAIF_EN, USB3_PN_CON); in usb3_disable_dma_pipen()
1230 static void usb3_enable_dma_irq(struct renesas_usb3 *usb3, int num) in usb3_enable_dma_irq() argument
1232 usb3_set_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA); in usb3_enable_dma_irq()
1235 static void usb3_disable_dma_irq(struct renesas_usb3 *usb3, int num) in usb3_disable_dma_irq() argument
1237 usb3_clear_bit(usb3, DMA_INT(num), USB3_DMA_INT_ENA); in usb3_disable_dma_irq()
1242 switch (usb3_ep->ep.maxpacket) { in usb3_dma_mps_to_prd_word1()
1263 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_get_setting_area() local
1268 if (usb3_req->req.length > USB3_DMA_MAX_XFER_SIZE_ALL_PRDS) { in usb3_dma_get_setting_area()
1269 dev_dbg(usb3_to_dev(usb3), "%s: the length is too big (%d)\n", in usb3_dma_get_setting_area()
1270 __func__, usb3_req->req.length); in usb3_dma_get_setting_area()
1274 /* The driver doesn't handle zero-length packet via dmac */ in usb3_dma_get_setting_area()
1275 if (!usb3_req->req.length) in usb3_dma_get_setting_area()
1281 usb3_for_each_dma(usb3, dma, i) { in usb3_dma_get_setting_area()
1282 if (dma->used) in usb3_dma_get_setting_area()
1285 if (usb_gadget_map_request(&usb3->gadget, &usb3_req->req, in usb3_dma_get_setting_area()
1286 usb3_ep->dir_in) < 0) in usb3_dma_get_setting_area()
1289 dma->used = true; in usb3_dma_get_setting_area()
1290 usb3_ep->dma = dma; in usb3_dma_get_setting_area()
1301 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_put_setting_area() local
1305 usb3_for_each_dma(usb3, dma, i) { in usb3_dma_put_setting_area()
1306 if (usb3_ep->dma == dma) { in usb3_dma_put_setting_area()
1307 usb_gadget_unmap_request(&usb3->gadget, &usb3_req->req, in usb3_dma_put_setting_area()
1308 usb3_ep->dir_in); in usb3_dma_put_setting_area()
1309 dma->used = false; in usb3_dma_put_setting_area()
1310 usb3_ep->dma = NULL; in usb3_dma_put_setting_area()
1319 struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd; in usb3_dma_fill_prd()
1320 u32 remain = usb3_req->req.length; in usb3_dma_fill_prd()
1321 u32 dma = usb3_req->req.dma; in usb3_dma_fill_prd()
1328 cur_prd->word1 = usb3_dma_mps_to_prd_word1(usb3_ep) | in usb3_dma_fill_prd()
1330 cur_prd->bap = dma; in usb3_dma_fill_prd()
1331 remain -= len; in usb3_dma_fill_prd()
1340 cur_prd->word1 |= USB3_PRD1_E | USB3_PRD1_INT; in usb3_dma_fill_prd()
1341 if (usb3_ep->dir_in) in usb3_dma_fill_prd()
1342 cur_prd->word1 |= USB3_PRD1_LST; in usb3_dma_fill_prd()
1347 struct renesas_usb3_dma *dma = usb3_ep->dma; in usb3_dma_kick_prd()
1348 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_kick_prd() local
1349 u32 dma_con = DMA_COM_PIPE_NO(usb3_ep->num) | DMA_CON_PRD_EN; in usb3_dma_kick_prd()
1351 if (usb3_ep->dir_in) in usb3_dma_kick_prd()
1356 usb3_write(usb3, 1 << usb3_ep->num, USB3_DMA_INT_STA); in usb3_dma_kick_prd()
1357 usb3_write(usb3, AXI_INT_PRDEN_CLR_STA(dma->num) | in usb3_dma_kick_prd()
1358 AXI_INT_PRDERR_STA(dma->num), USB3_AXI_INT_STA); in usb3_dma_kick_prd()
1360 usb3_write(usb3, dma->prd_dma, USB3_DMA_CH0_PRD_ADR(dma->num)); in usb3_dma_kick_prd()
1361 usb3_write(usb3, dma_con, USB3_DMA_CH0_CON(dma->num)); in usb3_dma_kick_prd()
1362 usb3_enable_dma_irq(usb3, usb3_ep->num); in usb3_dma_kick_prd()
1367 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_stop_prd() local
1368 struct renesas_usb3_dma *dma = usb3_ep->dma; in usb3_dma_stop_prd()
1370 usb3_disable_dma_irq(usb3, usb3_ep->num); in usb3_dma_stop_prd()
1371 usb3_write(usb3, 0, USB3_DMA_CH0_CON(dma->num)); in usb3_dma_stop_prd()
1377 struct renesas_usb3_prd *cur_prd = usb3_ep->dma->prd; in usb3_dma_update_status()
1378 struct usb_request *req = &usb3_req->req; in usb3_dma_update_status()
1386 if (cur_prd->word1 & USB3_PRD1_D) in usb3_dma_update_status()
1387 status = -EIO; in usb3_dma_update_status()
1388 if (cur_prd->word1 & USB3_PRD1_E) in usb3_dma_update_status()
1389 len = req->length % USB3_DMA_MAX_XFER_SIZE; in usb3_dma_update_status()
1392 remain = cur_prd->word1 & USB3_PRD1_SIZE_MASK; in usb3_dma_update_status()
1393 req->actual += len - remain; in usb3_dma_update_status()
1395 if (cur_prd->word1 & USB3_PRD1_E || in usb3_dma_update_status()
1409 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_try_start() local
1415 usb3_pn_stop(usb3); in usb3_dma_try_start()
1416 usb3_enable_dma_pipen(usb3); in usb3_dma_try_start()
1419 usb3_pn_start(usb3); in usb3_dma_try_start()
1429 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_dma_try_stop() local
1433 spin_lock_irqsave(&usb3->lock, flags); in usb3_dma_try_stop()
1434 if (!usb3_ep->dma) in usb3_dma_try_stop()
1437 if (!usb3_pn_change(usb3, usb3_ep->num)) in usb3_dma_try_stop()
1438 usb3_disable_dma_pipen(usb3); in usb3_dma_try_stop()
1444 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_dma_try_stop()
1448 static int renesas_usb3_dma_free_prd(struct renesas_usb3 *usb3, in renesas_usb3_dma_free_prd() argument
1454 usb3_for_each_dma(usb3, dma, i) { in renesas_usb3_dma_free_prd()
1455 if (dma->prd) { in renesas_usb3_dma_free_prd()
1457 dma->prd, dma->prd_dma); in renesas_usb3_dma_free_prd()
1458 dma->prd = NULL; in renesas_usb3_dma_free_prd()
1465 static int renesas_usb3_dma_alloc_prd(struct renesas_usb3 *usb3, in renesas_usb3_dma_alloc_prd() argument
1474 usb3_for_each_dma(usb3, dma, i) { in renesas_usb3_dma_alloc_prd()
1475 dma->prd = dma_alloc_coherent(dev, USB3_DMA_PRD_SIZE, in renesas_usb3_dma_alloc_prd()
1476 &dma->prd_dma, GFP_KERNEL); in renesas_usb3_dma_alloc_prd()
1477 if (!dma->prd) { in renesas_usb3_dma_alloc_prd()
1478 renesas_usb3_dma_free_prd(usb3, dev); in renesas_usb3_dma_alloc_prd()
1479 return -ENOMEM; in renesas_usb3_dma_alloc_prd()
1481 dma->num = i + 1; in renesas_usb3_dma_alloc_prd()
1490 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_start_pipen() local
1493 int ret = -EAGAIN; in usb3_start_pipen()
1496 spin_lock_irqsave(&usb3->lock, flags); in usb3_start_pipen()
1497 if (usb3_ep->halt || usb3_ep->started) in usb3_start_pipen()
1502 if (usb3_pn_change(usb3, usb3_ep->num) < 0) in usb3_start_pipen()
1505 usb3_ep->started = true; in usb3_start_pipen()
1510 usb3_pn_start(usb3); in usb3_start_pipen()
1512 if (usb3_ep->dir_in) { in usb3_start_pipen()
1521 usb3_set_bit(usb3, enable_bits, USB3_PN_INT_ENA); in usb3_start_pipen()
1522 usb3_enable_pipe_irq(usb3, usb3_ep->num); in usb3_start_pipen()
1525 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_start_pipen()
1533 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in renesas_usb3_ep_queue() local
1536 dev_dbg(usb3_to_dev(usb3), "ep_queue: ep%2d, %u\n", usb3_ep->num, in renesas_usb3_ep_queue()
1537 _req->length); in renesas_usb3_ep_queue()
1539 _req->status = -EINPROGRESS; in renesas_usb3_ep_queue()
1540 _req->actual = 0; in renesas_usb3_ep_queue()
1541 spin_lock_irqsave(&usb3->lock, flags); in renesas_usb3_ep_queue()
1542 list_add_tail(&usb3_req->queue, &usb3_ep->queue); in renesas_usb3_ep_queue()
1543 spin_unlock_irqrestore(&usb3->lock, flags); in renesas_usb3_ep_queue()
1545 if (!usb3_ep->num) in renesas_usb3_ep_queue()
1553 static void usb3_set_device_address(struct renesas_usb3 *usb3, u16 addr) in usb3_set_device_address() argument
1556 usb3_set_bit(usb3, USB_COM_CON_DEV_ADDR(addr), USB3_USB_COM_CON); in usb3_set_device_address()
1559 static bool usb3_std_req_set_address(struct renesas_usb3 *usb3, in usb3_std_req_set_address() argument
1562 if (le16_to_cpu(ctrl->wValue) >= 128) in usb3_std_req_set_address()
1565 usb3_set_device_address(usb3, le16_to_cpu(ctrl->wValue)); in usb3_std_req_set_address()
1566 usb3_set_p0_con_for_no_data(usb3); in usb3_std_req_set_address()
1571 static void usb3_pipe0_internal_xfer(struct renesas_usb3 *usb3, in usb3_pipe0_internal_xfer() argument
1576 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_pipe0_internal_xfer()
1579 memcpy(usb3->ep0_buf, tx_data, in usb3_pipe0_internal_xfer()
1582 usb3->ep0_req->buf = &usb3->ep0_buf; in usb3_pipe0_internal_xfer()
1583 usb3->ep0_req->length = len; in usb3_pipe0_internal_xfer()
1584 usb3->ep0_req->complete = complete; in usb3_pipe0_internal_xfer()
1585 renesas_usb3_ep_queue(&usb3_ep->ep, usb3->ep0_req, GFP_ATOMIC); in usb3_pipe0_internal_xfer()
1593 static bool usb3_std_req_get_status(struct renesas_usb3 *usb3, in usb3_std_req_get_status() argument
1602 switch (ctrl->bRequestType & USB_RECIP_MASK) { in usb3_std_req_get_status()
1604 if (usb3->gadget.is_selfpowered) in usb3_std_req_get_status()
1606 if (usb3->gadget.speed == USB_SPEED_SUPER) in usb3_std_req_get_status()
1607 status |= usb3_feature_get_un_enabled(usb3); in usb3_std_req_get_status()
1612 num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; in usb3_std_req_get_status()
1613 usb3_ep = usb3_get_ep(usb3, num); in usb3_std_req_get_status()
1614 if (usb3_ep->halt) in usb3_std_req_get_status()
1624 dev_dbg(usb3_to_dev(usb3), "get_status: req = %p\n", in usb3_std_req_get_status()
1625 usb_req_to_usb3_req(usb3->ep0_req)); in usb3_std_req_get_status()
1626 usb3_pipe0_internal_xfer(usb3, &tx_data, sizeof(tx_data), in usb3_std_req_get_status()
1633 static bool usb3_std_req_feature_device(struct renesas_usb3 *usb3, in usb3_std_req_feature_device() argument
1637 u16 w_value = le16_to_cpu(ctrl->wValue); in usb3_std_req_feature_device()
1643 usb3->test_mode = le16_to_cpu(ctrl->wIndex) >> 8; in usb3_std_req_feature_device()
1648 if (usb3->gadget.speed != USB_SPEED_SUPER) in usb3_std_req_feature_device()
1651 usb3_feature_u1_enable(usb3, set); in usb3_std_req_feature_device()
1653 usb3_feature_u2_enable(usb3, set); in usb3_std_req_feature_device()
1665 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_halt_p0() local
1667 if (unlikely(usb3_ep->num)) in usb3_set_halt_p0()
1668 return -EINVAL; in usb3_set_halt_p0()
1670 usb3_ep->halt = halt; in usb3_set_halt_p0()
1672 usb3_set_p0_con_stall(usb3); in usb3_set_halt_p0()
1674 usb3_set_p0_con_stop(usb3); in usb3_set_halt_p0()
1682 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_set_halt_pn() local
1685 spin_lock_irqsave(&usb3->lock, flags); in usb3_set_halt_pn()
1686 if (!usb3_pn_change(usb3, usb3_ep->num)) { in usb3_set_halt_pn()
1687 usb3_ep->halt = halt; in usb3_set_halt_pn()
1689 usb3_pn_stall(usb3); in usb3_set_halt_pn()
1690 } else if (!is_clear_feature || !usb3_ep->wedge) { in usb3_set_halt_pn()
1691 usb3_pn_con_clear(usb3); in usb3_set_halt_pn()
1692 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON); in usb3_set_halt_pn()
1693 usb3_pn_stop(usb3); in usb3_set_halt_pn()
1696 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_set_halt_pn()
1706 if (halt && usb3_ep->started) in usb3_set_halt()
1707 return -EAGAIN; in usb3_set_halt()
1709 if (usb3_ep->num) in usb3_set_halt()
1717 static bool usb3_std_req_feature_endpoint(struct renesas_usb3 *usb3, in usb3_std_req_feature_endpoint() argument
1721 int num = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK; in usb3_std_req_feature_endpoint()
1725 if (le16_to_cpu(ctrl->wValue) != USB_ENDPOINT_HALT) in usb3_std_req_feature_endpoint()
1728 usb3_ep = usb3_get_ep(usb3, num); in usb3_std_req_feature_endpoint()
1733 usb3_ep->started = false; in usb3_std_req_feature_endpoint()
1742 static bool usb3_std_req_feature(struct renesas_usb3 *usb3, in usb3_std_req_feature() argument
1747 switch (ctrl->bRequestType & USB_RECIP_MASK) { in usb3_std_req_feature()
1749 stall = usb3_std_req_feature_device(usb3, ctrl, set); in usb3_std_req_feature()
1754 stall = usb3_std_req_feature_endpoint(usb3, ctrl, set); in usb3_std_req_feature()
1762 usb3_set_p0_con_for_no_data(usb3); in usb3_std_req_feature()
1773 static bool usb3_std_req_set_sel(struct renesas_usb3 *usb3, in usb3_std_req_set_sel() argument
1776 u16 w_length = le16_to_cpu(ctrl->wLength); in usb3_std_req_set_sel()
1781 dev_dbg(usb3_to_dev(usb3), "set_sel: req = %p\n", in usb3_std_req_set_sel()
1782 usb_req_to_usb3_req(usb3->ep0_req)); in usb3_std_req_set_sel()
1783 usb3_pipe0_internal_xfer(usb3, NULL, 6, usb3_pipe0_set_sel_completion); in usb3_std_req_set_sel()
1788 static bool usb3_std_req_set_configuration(struct renesas_usb3 *usb3, in usb3_std_req_set_configuration() argument
1791 if (le16_to_cpu(ctrl->wValue) > 0) in usb3_std_req_set_configuration()
1792 usb3_set_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON); in usb3_std_req_set_configuration()
1794 usb3_clear_bit(usb3, USB_COM_CON_CONF, USB3_USB_COM_CON); in usb3_std_req_set_configuration()
1800 * usb3_handle_standard_request - handle some standard requests
1801 * @usb3: the renesas_usb3 pointer
1806 static bool usb3_handle_standard_request(struct renesas_usb3 *usb3, in usb3_handle_standard_request() argument
1812 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { in usb3_handle_standard_request()
1813 switch (ctrl->bRequest) { in usb3_handle_standard_request()
1815 stall = usb3_std_req_set_address(usb3, ctrl); in usb3_handle_standard_request()
1819 stall = usb3_std_req_get_status(usb3, ctrl); in usb3_handle_standard_request()
1823 stall = usb3_std_req_feature(usb3, ctrl, false); in usb3_handle_standard_request()
1827 stall = usb3_std_req_feature(usb3, ctrl, true); in usb3_handle_standard_request()
1831 stall = usb3_std_req_set_sel(usb3, ctrl); in usb3_handle_standard_request()
1840 usb3_std_req_set_configuration(usb3, ctrl); in usb3_handle_standard_request()
1848 usb3_set_p0_con_stall(usb3); in usb3_handle_standard_request()
1853 static int usb3_p0_con_clear_buffer(struct renesas_usb3 *usb3) in usb3_p0_con_clear_buffer() argument
1855 usb3_set_bit(usb3, P0_CON_BCLR, USB3_P0_CON); in usb3_p0_con_clear_buffer()
1857 return usb3_wait(usb3, USB3_P0_CON, P0_CON_BCLR, 0); in usb3_p0_con_clear_buffer()
1860 static void usb3_irq_epc_pipe0_setup(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0_setup() argument
1863 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_irq_epc_pipe0_setup()
1866 if (usb3_ep->started) in usb3_irq_epc_pipe0_setup()
1868 -ECONNRESET); in usb3_irq_epc_pipe0_setup()
1870 usb3_p0_con_clear_buffer(usb3); in usb3_irq_epc_pipe0_setup()
1871 usb3_get_setup_data(usb3, &ctrl); in usb3_irq_epc_pipe0_setup()
1872 if (!usb3_handle_standard_request(usb3, &ctrl)) in usb3_irq_epc_pipe0_setup()
1873 if (usb3->driver->setup(&usb3->gadget, &ctrl) < 0) in usb3_irq_epc_pipe0_setup()
1874 usb3_set_p0_con_stall(usb3); in usb3_irq_epc_pipe0_setup()
1877 static void usb3_irq_epc_pipe0_bfrdy(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0_bfrdy() argument
1879 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, 0); in usb3_irq_epc_pipe0_bfrdy()
1888 static void usb3_irq_epc_pipe0(struct renesas_usb3 *usb3) in usb3_irq_epc_pipe0() argument
1890 u32 p0_int_sta = usb3_read(usb3, USB3_P0_INT_STA); in usb3_irq_epc_pipe0()
1892 p0_int_sta &= usb3_read(usb3, USB3_P0_INT_ENA); in usb3_irq_epc_pipe0()
1893 usb3_write(usb3, p0_int_sta, USB3_P0_INT_STA); in usb3_irq_epc_pipe0()
1895 usb3_irq_epc_pipe0_status_end(usb3); in usb3_irq_epc_pipe0()
1897 usb3_irq_epc_pipe0_setup(usb3); in usb3_irq_epc_pipe0()
1899 usb3_irq_epc_pipe0_bfrdy(usb3); in usb3_irq_epc_pipe0()
1902 static void usb3_request_done_pipen(struct renesas_usb3 *usb3, in usb3_request_done_pipen() argument
1909 spin_lock_irqsave(&usb3->lock, flags); in usb3_request_done_pipen()
1910 if (usb3_pn_change(usb3, usb3_ep->num)) in usb3_request_done_pipen()
1911 usb3_pn_stop(usb3); in usb3_request_done_pipen()
1912 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_request_done_pipen()
1914 usb3_disable_pipe_irq(usb3, usb3_ep->num); in usb3_request_done_pipen()
1923 static void usb3_irq_epc_pipen_lsttr(struct renesas_usb3 *usb3, int num) in usb3_irq_epc_pipen_lsttr() argument
1925 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num); in usb3_irq_epc_pipen_lsttr()
1931 if (usb3_ep->dir_in) { in usb3_irq_epc_pipen_lsttr()
1932 dev_dbg(usb3_to_dev(usb3), "%s: len = %u, actual = %u\n", in usb3_irq_epc_pipen_lsttr()
1933 __func__, usb3_req->req.length, usb3_req->req.actual); in usb3_irq_epc_pipen_lsttr()
1934 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0); in usb3_irq_epc_pipen_lsttr()
1938 static void usb3_irq_epc_pipen_bfrdy(struct renesas_usb3 *usb3, int num) in usb3_irq_epc_pipen_bfrdy() argument
1940 struct renesas_usb3_ep *usb3_ep = usb3_get_ep(usb3, num); in usb3_irq_epc_pipen_bfrdy()
1947 spin_lock(&usb3->lock); in usb3_irq_epc_pipen_bfrdy()
1948 if (usb3_pn_change(usb3, num)) in usb3_irq_epc_pipen_bfrdy()
1951 if (usb3_ep->dir_in) { in usb3_irq_epc_pipen_bfrdy()
1954 usb3_clear_bit(usb3, PN_INT_BFRDY, USB3_PN_INT_ENA); in usb3_irq_epc_pipen_bfrdy()
1962 spin_unlock(&usb3->lock); in usb3_irq_epc_pipen_bfrdy()
1965 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, 0); in usb3_irq_epc_pipen_bfrdy()
1968 static void usb3_irq_epc_pipen(struct renesas_usb3 *usb3, int num) in usb3_irq_epc_pipen() argument
1972 spin_lock(&usb3->lock); in usb3_irq_epc_pipen()
1973 if (usb3_pn_change(usb3, num) < 0) { in usb3_irq_epc_pipen()
1974 spin_unlock(&usb3->lock); in usb3_irq_epc_pipen()
1978 pn_int_sta = usb3_read(usb3, USB3_PN_INT_STA); in usb3_irq_epc_pipen()
1979 pn_int_sta &= usb3_read(usb3, USB3_PN_INT_ENA); in usb3_irq_epc_pipen()
1980 usb3_write(usb3, pn_int_sta, USB3_PN_INT_STA); in usb3_irq_epc_pipen()
1981 spin_unlock(&usb3->lock); in usb3_irq_epc_pipen()
1983 usb3_irq_epc_pipen_lsttr(usb3, num); in usb3_irq_epc_pipen()
1985 usb3_irq_epc_pipen_bfrdy(usb3, num); in usb3_irq_epc_pipen()
1988 static void usb3_irq_epc_int_2(struct renesas_usb3 *usb3, u32 int_sta_2) in usb3_irq_epc_int_2() argument
1992 for (i = 0; i < usb3->num_usb3_eps; i++) { in usb3_irq_epc_int_2()
1995 usb3_irq_epc_pipe0(usb3); in usb3_irq_epc_int_2()
1997 usb3_irq_epc_pipen(usb3, i); in usb3_irq_epc_int_2()
2002 static void usb3_irq_idmon_change(struct renesas_usb3 *usb3) in usb3_irq_idmon_change() argument
2004 usb3_check_id(usb3); in usb3_irq_idmon_change()
2007 static void usb3_irq_otg_int(struct renesas_usb3 *usb3, u32 otg_int_sta) in usb3_irq_otg_int() argument
2010 usb3_irq_idmon_change(usb3); in usb3_irq_otg_int()
2013 static void usb3_irq_epc(struct renesas_usb3 *usb3) in usb3_irq_epc() argument
2015 u32 int_sta_1 = usb3_read(usb3, USB3_USB_INT_STA_1); in usb3_irq_epc()
2016 u32 int_sta_2 = usb3_read(usb3, USB3_USB_INT_STA_2); in usb3_irq_epc()
2017 u32 otg_int_sta = usb3_read(usb3, USB3_USB_OTG_INT_STA); in usb3_irq_epc()
2019 int_sta_1 &= usb3_read(usb3, USB3_USB_INT_ENA_1); in usb3_irq_epc()
2021 usb3_write(usb3, int_sta_1, USB3_USB_INT_STA_1); in usb3_irq_epc()
2022 usb3_irq_epc_int_1(usb3, int_sta_1); in usb3_irq_epc()
2025 int_sta_2 &= usb3_read(usb3, USB3_USB_INT_ENA_2); in usb3_irq_epc()
2027 usb3_irq_epc_int_2(usb3, int_sta_2); in usb3_irq_epc()
2029 otg_int_sta &= usb3_read(usb3, USB3_USB_OTG_INT_ENA); in usb3_irq_epc()
2031 usb3_write(usb3, otg_int_sta, USB3_USB_OTG_INT_STA); in usb3_irq_epc()
2032 usb3_irq_otg_int(usb3, otg_int_sta); in usb3_irq_epc()
2036 static void usb3_irq_dma_int(struct renesas_usb3 *usb3, u32 dma_sta) in usb3_irq_dma_int() argument
2042 for (i = 0; i < usb3->num_usb3_eps; i++) { in usb3_irq_dma_int()
2046 usb3_ep = usb3_get_ep(usb3, i); in usb3_irq_dma_int()
2047 if (!(usb3_read(usb3, USB3_AXI_INT_STA) & in usb3_irq_dma_int()
2048 AXI_INT_PRDEN_CLR_STA(usb3_ep->dma->num))) in usb3_irq_dma_int()
2053 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, status); in usb3_irq_dma_int()
2057 static void usb3_irq_dma(struct renesas_usb3 *usb3) in usb3_irq_dma() argument
2059 u32 dma_sta = usb3_read(usb3, USB3_DMA_INT_STA); in usb3_irq_dma()
2061 dma_sta &= usb3_read(usb3, USB3_DMA_INT_ENA); in usb3_irq_dma()
2063 usb3_write(usb3, dma_sta, USB3_DMA_INT_STA); in usb3_irq_dma()
2064 usb3_irq_dma_int(usb3, dma_sta); in usb3_irq_dma()
2070 struct renesas_usb3 *usb3 = _usb3; in renesas_usb3_irq() local
2072 u32 axi_int_sta = usb3_read(usb3, USB3_AXI_INT_STA); in renesas_usb3_irq()
2075 usb3_irq_dma(usb3); in renesas_usb3_irq()
2080 usb3_irq_epc(usb3); in renesas_usb3_irq()
2090 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_write_pn_mod() local
2093 val |= usb3_ep->dir_in ? PN_MOD_DIR : 0; in usb3_write_pn_mod()
2096 usb3_write(usb3, val, USB3_PN_MOD); in usb3_write_pn_mod()
2127 return usb3_ep->rammap_val | mpkt; in usb3_calc_rammap_val()
2133 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_enable_pipe_n() local
2136 usb3_ep->dir_in = usb_endpoint_dir_in(desc); in usb3_enable_pipe_n()
2138 spin_lock_irqsave(&usb3->lock, flags); in usb3_enable_pipe_n()
2139 if (!usb3_pn_change(usb3, usb3_ep->num)) { in usb3_enable_pipe_n()
2141 usb3_write(usb3, usb3_calc_rammap_val(usb3_ep, desc), in usb3_enable_pipe_n()
2143 usb3_pn_con_clear(usb3); in usb3_enable_pipe_n()
2144 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON); in usb3_enable_pipe_n()
2146 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_enable_pipe_n()
2153 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in usb3_disable_pipe_n() local
2156 usb3_ep->halt = false; in usb3_disable_pipe_n()
2158 spin_lock_irqsave(&usb3->lock, flags); in usb3_disable_pipe_n()
2159 if (!usb3_pn_change(usb3, usb3_ep->num)) { in usb3_disable_pipe_n()
2160 usb3_write(usb3, 0, USB3_PN_INT_ENA); in usb3_disable_pipe_n()
2161 usb3_write(usb3, 0, USB3_PN_RAMMAP); in usb3_disable_pipe_n()
2162 usb3_clear_bit(usb3, PN_CON_EN, USB3_PN_CON); in usb3_disable_pipe_n()
2164 spin_unlock_irqrestore(&usb3->lock, flags); in usb3_disable_pipe_n()
2169 /*------- usb_ep_ops -----------------------------------------------------*/
2188 usb3_request_done(usb3_ep, usb3_req, -ESHUTDOWN); in renesas_usb3_ep_disable()
2202 INIT_LIST_HEAD(&usb3_req->queue); in __renesas_usb3_ep_alloc_request()
2204 return &usb3_req->req; in __renesas_usb3_ep_alloc_request()
2230 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in renesas_usb3_ep_dequeue() local
2232 dev_dbg(usb3_to_dev(usb3), "ep_dequeue: ep%2d, %u\n", usb3_ep->num, in renesas_usb3_ep_dequeue()
2233 _req->length); in renesas_usb3_ep_dequeue()
2236 usb3_request_done_pipen(usb3, usb3_ep, usb3_req, -ECONNRESET); in renesas_usb3_ep_dequeue()
2250 usb3_ep->wedge = true; in renesas_usb3_ep_set_wedge()
2257 struct renesas_usb3 *usb3 = usb3_ep_to_usb3(usb3_ep); in renesas_usb3_ep_fifo_flush() local
2260 if (usb3_ep->num) { in renesas_usb3_ep_fifo_flush()
2261 spin_lock_irqsave(&usb3->lock, flags); in renesas_usb3_ep_fifo_flush()
2262 if (!usb3_pn_change(usb3, usb3_ep->num)) { in renesas_usb3_ep_fifo_flush()
2263 usb3_pn_con_clear(usb3); in renesas_usb3_ep_fifo_flush()
2264 usb3_set_bit(usb3, PN_CON_EN, USB3_PN_CON); in renesas_usb3_ep_fifo_flush()
2266 spin_unlock_irqrestore(&usb3->lock, flags); in renesas_usb3_ep_fifo_flush()
2268 usb3_p0_con_clear_buffer(usb3); in renesas_usb3_ep_fifo_flush()
2287 /*------- usb_gadget_ops -------------------------------------------------*/
2291 struct renesas_usb3 *usb3; in renesas_usb3_start() local
2293 if (!driver || driver->max_speed < USB_SPEED_FULL || in renesas_usb3_start()
2294 !driver->setup) in renesas_usb3_start()
2295 return -EINVAL; in renesas_usb3_start()
2297 usb3 = gadget_to_renesas_usb3(gadget); in renesas_usb3_start()
2300 usb3->driver = driver; in renesas_usb3_start()
2302 if (usb3->phy) in renesas_usb3_start()
2303 phy_init(usb3->phy); in renesas_usb3_start()
2305 pm_runtime_get_sync(usb3_to_dev(usb3)); in renesas_usb3_start()
2307 renesas_usb3_init_controller(usb3); in renesas_usb3_start()
2314 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget); in renesas_usb3_stop() local
2316 usb3->softconnect = false; in renesas_usb3_stop()
2317 usb3->gadget.speed = USB_SPEED_UNKNOWN; in renesas_usb3_stop()
2318 usb3->driver = NULL; in renesas_usb3_stop()
2319 renesas_usb3_stop_controller(usb3); in renesas_usb3_stop()
2321 if (usb3->phy) in renesas_usb3_stop()
2322 phy_exit(usb3->phy); in renesas_usb3_stop()
2324 pm_runtime_put(usb3_to_dev(usb3)); in renesas_usb3_stop()
2331 return -EOPNOTSUPP; in renesas_usb3_get_frame()
2336 struct renesas_usb3 *usb3 = gadget_to_renesas_usb3(gadget); in renesas_usb3_pullup() local
2338 usb3->softconnect = !!is_on; in renesas_usb3_pullup()
2345 gadget->is_selfpowered = !!is_self; in renesas_usb3_set_selfpowered()
2360 struct renesas_usb3 *usb3 = usb_role_switch_get_drvdata(sw); in renesas_usb3_role_switch_get() local
2363 pm_runtime_get_sync(usb3_to_dev(usb3)); in renesas_usb3_role_switch_get()
2364 cur_role = usb3_is_host(usb3) ? USB_ROLE_HOST : USB_ROLE_DEVICE; in renesas_usb3_role_switch_get()
2365 pm_runtime_put(usb3_to_dev(usb3)); in renesas_usb3_role_switch_get()
2373 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in handle_ext_role_switch_states() local
2374 struct device *host = usb3->host_dev; in handle_ext_role_switch_states()
2375 enum usb_role cur_role = renesas_usb3_role_switch_get(usb3->role_sw); in handle_ext_role_switch_states()
2379 usb3->connection_state = USB_ROLE_NONE; in handle_ext_role_switch_states()
2380 if (usb3->driver) in handle_ext_role_switch_states()
2381 usb3_disconnect(usb3); in handle_ext_role_switch_states()
2382 usb3_vbus_out(usb3, false); in handle_ext_role_switch_states()
2385 if (usb3->connection_state == USB_ROLE_NONE) { in handle_ext_role_switch_states()
2386 usb3->connection_state = USB_ROLE_DEVICE; in handle_ext_role_switch_states()
2387 usb3_set_mode(usb3, false); in handle_ext_role_switch_states()
2388 if (usb3->driver) in handle_ext_role_switch_states()
2389 usb3_connect(usb3); in handle_ext_role_switch_states()
2392 usb3_set_mode(usb3, false); in handle_ext_role_switch_states()
2393 if (usb3->driver) in handle_ext_role_switch_states()
2394 usb3_connect(usb3); in handle_ext_role_switch_states()
2396 usb3_vbus_out(usb3, false); in handle_ext_role_switch_states()
2399 if (usb3->connection_state == USB_ROLE_NONE) { in handle_ext_role_switch_states()
2400 if (usb3->driver) in handle_ext_role_switch_states()
2401 usb3_disconnect(usb3); in handle_ext_role_switch_states()
2403 usb3->connection_state = USB_ROLE_HOST; in handle_ext_role_switch_states()
2404 usb3_set_mode(usb3, true); in handle_ext_role_switch_states()
2405 usb3_vbus_out(usb3, true); in handle_ext_role_switch_states()
2409 usb3_disconnect(usb3); in handle_ext_role_switch_states()
2411 usb3_set_mode(usb3, true); in handle_ext_role_switch_states()
2425 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in handle_role_switch_states() local
2426 struct device *host = usb3->host_dev; in handle_role_switch_states()
2427 enum usb_role cur_role = renesas_usb3_role_switch_get(usb3->role_sw); in handle_role_switch_states()
2431 usb3_set_mode(usb3, false); in handle_role_switch_states()
2434 usb3_set_mode(usb3, true); in handle_role_switch_states()
2444 struct renesas_usb3 *usb3 = usb_role_switch_get_drvdata(sw); in renesas_usb3_role_switch_set() local
2446 pm_runtime_get_sync(usb3_to_dev(usb3)); in renesas_usb3_role_switch_set()
2448 if (usb3->role_sw_by_connector) in renesas_usb3_role_switch_set()
2449 handle_ext_role_switch_states(usb3_to_dev(usb3), role); in renesas_usb3_role_switch_set()
2451 handle_role_switch_states(usb3_to_dev(usb3), role); in renesas_usb3_role_switch_set()
2453 pm_runtime_put(usb3_to_dev(usb3)); in renesas_usb3_role_switch_set()
2461 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in role_store() local
2464 if (!usb3->driver) in role_store()
2465 return -ENODEV; in role_store()
2467 if (usb3->forced_b_device) in role_store()
2468 return -EBUSY; in role_store()
2475 return -EINVAL; in role_store()
2477 if (new_mode_is_host == usb3_is_host(usb3)) in role_store()
2478 return -EINVAL; in role_store()
2480 usb3_mode_config(usb3, new_mode_is_host, usb3_is_a_device(usb3)); in role_store()
2488 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in role_show() local
2490 if (!usb3->driver) in role_show()
2491 return -ENODEV; in role_show()
2493 return sprintf(buf, "%s\n", usb3_is_host(usb3) ? "host" : "peripheral"); in role_show()
2499 struct renesas_usb3 *usb3 = s->private; in renesas_usb3_b_device_show() local
2501 seq_printf(s, "%d\n", usb3->forced_b_device); in renesas_usb3_b_device_show()
2508 return single_open(file, renesas_usb3_b_device_show, inode->i_private); in renesas_usb3_b_device_open()
2515 struct seq_file *s = file->private_data; in renesas_usb3_b_device_write()
2516 struct renesas_usb3 *usb3 = s->private; in renesas_usb3_b_device_write() local
2519 if (!usb3->driver) in renesas_usb3_b_device_write()
2520 return -ENODEV; in renesas_usb3_b_device_write()
2522 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) in renesas_usb3_b_device_write()
2523 return -EFAULT; in renesas_usb3_b_device_write()
2525 usb3->start_to_connect = false; in renesas_usb3_b_device_write()
2526 if (usb3->workaround_for_vbus && usb3->forced_b_device && in renesas_usb3_b_device_write()
2528 usb3->start_to_connect = true; in renesas_usb3_b_device_write()
2530 usb3->forced_b_device = true; in renesas_usb3_b_device_write()
2532 usb3->forced_b_device = false; in renesas_usb3_b_device_write()
2534 if (usb3->workaround_for_vbus) in renesas_usb3_b_device_write()
2535 usb3_disconnect(usb3); in renesas_usb3_b_device_write()
2538 usb3_check_id(usb3); in renesas_usb3_b_device_write()
2551 static void renesas_usb3_debugfs_init(struct renesas_usb3 *usb3, in renesas_usb3_debugfs_init() argument
2554 usb3->dentry = debugfs_create_dir(dev_name(dev), usb_debug_root); in renesas_usb3_debugfs_init()
2556 debugfs_create_file("b_device", 0644, usb3->dentry, usb3, in renesas_usb3_debugfs_init()
2560 /*------- platform_driver ------------------------------------------------*/
2563 struct renesas_usb3 *usb3 = platform_get_drvdata(pdev); in renesas_usb3_remove() local
2565 debugfs_remove_recursive(usb3->dentry); in renesas_usb3_remove()
2566 device_remove_file(&pdev->dev, &dev_attr_role); in renesas_usb3_remove()
2568 usb_role_switch_unregister(usb3->role_sw); in renesas_usb3_remove()
2570 usb_del_gadget_udc(&usb3->gadget); in renesas_usb3_remove()
2571 renesas_usb3_dma_free_prd(usb3, &pdev->dev); in renesas_usb3_remove()
2573 __renesas_usb3_ep_free_request(usb3->ep0_req); in renesas_usb3_remove()
2574 pm_runtime_disable(&pdev->dev); in renesas_usb3_remove()
2579 static int renesas_usb3_init_ep(struct renesas_usb3 *usb3, struct device *dev, in renesas_usb3_init_ep() argument
2586 usb3->num_usb3_eps = priv->ramsize_per_ramif * priv->num_ramif * 2 / in renesas_usb3_init_ep()
2587 priv->ramsize_per_pipe + 1; in renesas_usb3_init_ep()
2589 if (usb3->num_usb3_eps > USB3_MAX_NUM_PIPES) in renesas_usb3_init_ep()
2590 usb3->num_usb3_eps = USB3_MAX_NUM_PIPES; in renesas_usb3_init_ep()
2592 usb3->usb3_ep = devm_kcalloc(dev, in renesas_usb3_init_ep()
2593 usb3->num_usb3_eps, sizeof(*usb3_ep), in renesas_usb3_init_ep()
2595 if (!usb3->usb3_ep) in renesas_usb3_init_ep()
2596 return -ENOMEM; in renesas_usb3_init_ep()
2598 dev_dbg(dev, "%s: num_usb3_eps = %d\n", __func__, usb3->num_usb3_eps); in renesas_usb3_init_ep()
2601 * - odd pipes = IN pipe in renesas_usb3_init_ep()
2602 * - even pipes = OUT pipe (except pipe 0) in renesas_usb3_init_ep()
2604 usb3_for_each_ep(usb3_ep, usb3, i) { in renesas_usb3_init_ep()
2605 snprintf(usb3_ep->ep_name, sizeof(usb3_ep->ep_name), "ep%d", i); in renesas_usb3_init_ep()
2606 usb3_ep->usb3 = usb3; in renesas_usb3_init_ep()
2607 usb3_ep->num = i; in renesas_usb3_init_ep()
2608 usb3_ep->ep.name = usb3_ep->ep_name; in renesas_usb3_init_ep()
2609 usb3_ep->ep.ops = &renesas_usb3_ep_ops; in renesas_usb3_init_ep()
2610 INIT_LIST_HEAD(&usb3_ep->queue); in renesas_usb3_init_ep()
2611 INIT_LIST_HEAD(&usb3_ep->ep.ep_list); in renesas_usb3_init_ep()
2614 usb3->gadget.ep0 = &usb3_ep->ep; in renesas_usb3_init_ep()
2615 usb_ep_set_maxpacket_limit(&usb3_ep->ep, in renesas_usb3_init_ep()
2617 usb3_ep->ep.caps.type_control = true; in renesas_usb3_init_ep()
2618 usb3_ep->ep.caps.dir_in = true; in renesas_usb3_init_ep()
2619 usb3_ep->ep.caps.dir_out = true; in renesas_usb3_init_ep()
2624 usb_ep_set_maxpacket_limit(&usb3_ep->ep, ~0); in renesas_usb3_init_ep()
2625 list_add_tail(&usb3_ep->ep.ep_list, &usb3->gadget.ep_list); in renesas_usb3_init_ep()
2626 usb3_ep->ep.caps.type_bulk = true; in renesas_usb3_init_ep()
2627 usb3_ep->ep.caps.type_int = true; in renesas_usb3_init_ep()
2629 usb3_ep->ep.caps.dir_in = true; in renesas_usb3_init_ep()
2631 usb3_ep->ep.caps.dir_out = true; in renesas_usb3_init_ep()
2637 static void renesas_usb3_init_ram(struct renesas_usb3 *usb3, struct device *dev, in renesas_usb3_init_ram() argument
2651 * - all pipes = the same size as "ramsize_per_pipe" in renesas_usb3_init_ram()
2654 usb3_for_each_ep(usb3_ep, usb3, i) { in renesas_usb3_init_ram()
2657 if (usb3_ep->ep.caps.dir_in) { in renesas_usb3_init_ram()
2665 if (*cur_basead > priv->ramsize_per_ramif) in renesas_usb3_init_ram()
2670 val |= usb3_calc_ramarea(priv->ramsize_per_pipe); in renesas_usb3_init_ram()
2672 usb3_ep->rammap_val = val; in renesas_usb3_init_ram()
2678 if (*cur_ramif + 1 == priv->num_ramif) { in renesas_usb3_init_ram()
2680 *cur_basead += priv->ramsize_per_pipe; in renesas_usb3_init_ram()
2709 .compatible = "renesas,r8a7795-usb3-peri",
2713 .compatible = "renesas,rcar-gen3-usb3-peri",
2722 .soc_id = "r8a774c0",
2750 struct renesas_usb3 *usb3; in renesas_usb3_probe() local
2757 priv = attr->data; in renesas_usb3_probe()
2759 priv = of_device_get_match_data(&pdev->dev); in renesas_usb3_probe()
2765 usb3 = devm_kzalloc(&pdev->dev, sizeof(*usb3), GFP_KERNEL); in renesas_usb3_probe()
2766 if (!usb3) in renesas_usb3_probe()
2767 return -ENOMEM; in renesas_usb3_probe()
2769 usb3->reg = devm_platform_ioremap_resource(pdev, 0); in renesas_usb3_probe()
2770 if (IS_ERR(usb3->reg)) in renesas_usb3_probe()
2771 return PTR_ERR(usb3->reg); in renesas_usb3_probe()
2773 platform_set_drvdata(pdev, usb3); in renesas_usb3_probe()
2774 spin_lock_init(&usb3->lock); in renesas_usb3_probe()
2776 usb3->gadget.ops = &renesas_usb3_gadget_ops; in renesas_usb3_probe()
2777 usb3->gadget.name = udc_name; in renesas_usb3_probe()
2778 usb3->gadget.max_speed = USB_SPEED_SUPER; in renesas_usb3_probe()
2779 INIT_LIST_HEAD(&usb3->gadget.ep_list); in renesas_usb3_probe()
2780 ret = renesas_usb3_init_ep(usb3, &pdev->dev, priv); in renesas_usb3_probe()
2783 renesas_usb3_init_ram(usb3, &pdev->dev, priv); in renesas_usb3_probe()
2785 ret = devm_request_irq(&pdev->dev, irq, renesas_usb3_irq, 0, in renesas_usb3_probe()
2786 dev_name(&pdev->dev), usb3); in renesas_usb3_probe()
2790 INIT_WORK(&usb3->extcon_work, renesas_usb3_extcon_work); in renesas_usb3_probe()
2791 usb3->extcon = devm_extcon_dev_allocate(&pdev->dev, renesas_usb3_cable); in renesas_usb3_probe()
2792 if (IS_ERR(usb3->extcon)) in renesas_usb3_probe()
2793 return PTR_ERR(usb3->extcon); in renesas_usb3_probe()
2795 ret = devm_extcon_dev_register(&pdev->dev, usb3->extcon); in renesas_usb3_probe()
2797 dev_err(&pdev->dev, "Failed to register extcon\n"); in renesas_usb3_probe()
2802 usb3->ep0_req = __renesas_usb3_ep_alloc_request(GFP_KERNEL); in renesas_usb3_probe()
2803 if (!usb3->ep0_req) in renesas_usb3_probe()
2804 return -ENOMEM; in renesas_usb3_probe()
2806 ret = renesas_usb3_dma_alloc_prd(usb3, &pdev->dev); in renesas_usb3_probe()
2814 usb3->phy = devm_phy_optional_get(&pdev->dev, "usb"); in renesas_usb3_probe()
2815 if (IS_ERR(usb3->phy)) { in renesas_usb3_probe()
2816 ret = PTR_ERR(usb3->phy); in renesas_usb3_probe()
2820 pm_runtime_enable(&pdev->dev); in renesas_usb3_probe()
2821 ret = usb_add_gadget_udc(&pdev->dev, &usb3->gadget); in renesas_usb3_probe()
2825 ret = device_create_file(&pdev->dev, &dev_attr_role); in renesas_usb3_probe()
2829 if (device_property_read_bool(&pdev->dev, "usb-role-switch")) { in renesas_usb3_probe()
2830 usb3->role_sw_by_connector = true; in renesas_usb3_probe()
2831 renesas_usb3_role_switch_desc.fwnode = dev_fwnode(&pdev->dev); in renesas_usb3_probe()
2834 renesas_usb3_role_switch_desc.driver_data = usb3; in renesas_usb3_probe()
2836 INIT_WORK(&usb3->role_work, renesas_usb3_role_work); in renesas_usb3_probe()
2837 usb3->role_sw = usb_role_switch_register(&pdev->dev, in renesas_usb3_probe()
2839 if (!IS_ERR(usb3->role_sw)) { in renesas_usb3_probe()
2840 usb3->host_dev = usb_of_get_companion_dev(&pdev->dev); in renesas_usb3_probe()
2841 if (!usb3->host_dev) { in renesas_usb3_probe()
2843 usb_role_switch_unregister(usb3->role_sw); in renesas_usb3_probe()
2844 usb3->role_sw = NULL; in renesas_usb3_probe()
2847 usb3->role_sw = NULL; in renesas_usb3_probe()
2850 usb3->workaround_for_vbus = priv->workaround_for_vbus; in renesas_usb3_probe()
2852 renesas_usb3_debugfs_init(usb3, &pdev->dev); in renesas_usb3_probe()
2854 dev_info(&pdev->dev, "probed%s\n", usb3->phy ? " with phy" : ""); in renesas_usb3_probe()
2859 usb_del_gadget_udc(&usb3->gadget); in renesas_usb3_probe()
2862 renesas_usb3_dma_free_prd(usb3, &pdev->dev); in renesas_usb3_probe()
2865 __renesas_usb3_ep_free_request(usb3->ep0_req); in renesas_usb3_probe()
2873 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in renesas_usb3_suspend() local
2876 if (!usb3->driver) in renesas_usb3_suspend()
2879 renesas_usb3_stop_controller(usb3); in renesas_usb3_suspend()
2880 if (usb3->phy) in renesas_usb3_suspend()
2881 phy_exit(usb3->phy); in renesas_usb3_suspend()
2889 struct renesas_usb3 *usb3 = dev_get_drvdata(dev); in renesas_usb3_resume() local
2892 if (!usb3->driver) in renesas_usb3_resume()
2895 if (usb3->phy) in renesas_usb3_resume()
2896 phy_init(usb3->phy); in renesas_usb3_resume()
2898 renesas_usb3_init_controller(usb3); in renesas_usb3_resume()
2918 MODULE_DESCRIPTION("Renesas USB3.0 Peripheral driver");