查看: 4532|回复: 1

[求助] imx6ull usb device问题

[复制链接]

该用户从未签到

1

主题

1

帖子

0

新手上路

Rank: 1

积分
15
最后登录
2021-8-10
发表于 2021-8-9 17:52:43 | 显示全部楼层 |阅读模式
我把linux5.10的usb gadget连同fsl_udc_core.c原样移植进了rtthread中。在imxrt1052中测试通过,在6ull中用usb启动下载进内存运行也正常。
但是换用其它启动方式后就只能收到reset和suspend中断,无法收到setup(有两个地方需要地址对齐是处理好的)。
我打印了部分寄存器对比两种启动方式的差异,发现usb启动时otgsc寄存器的OT位为1。尝试在udc_start接口中将此位设为1并不起作用。

下面贴出我的初始化代码,期望专家能指导下问题可能出在哪里。
  1. #include <rtthread.h>
  2. #include <ipc/completion.h>
  3. #include <board.h>

  4. #include <linux/platform_device.h>
  5. #include <linux/platform_data/fsl_devices.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/completion.h>

  8. extern int fsl_udc_probe(struct platform_device *pdev);

  9. typedef struct _usb_phy_config_struct
  10. {
  11.     uint8_t D_CAL;     /* Decode to trim the nominal 17.78mA current source */
  12.     uint8_t TXCAL45DP; /* Decode to trim the nominal 45-Ohm series termination resistance to the USB_DP output pin */
  13.     uint8_t TXCAL45DM; /* Decode to trim the nominal 45-Ohm series termination resistance to the USB_DM output pin */
  14. } usb_phy_config_struct_t;

  15. /* USB PHY condfiguration */
  16. #define BOARD_USB_PHY_D_CAL (0x0CU)
  17. #define BOARD_USB_PHY_TXCAL45DP (0x06U)
  18. #define BOARD_USB_PHY_TXCAL45DM (0x06U)

  19. static void *EhciPhyGetBase(uint8_t controllerId)
  20. {
  21.     void *usbPhyBase = NULL;
  22. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  23.     uint32_t instance;
  24.     uint32_t newinstance = 0;
  25.     uint32_t usbphy_base_temp[] = USBPHY_BASE_ADDRS;
  26.     uint32_t usbphy_base[] = USBPHY_BASE_ADDRS;

  27.     if (controllerId > 1)
  28.     {
  29.         return NULL;
  30.     }

  31.     for (instance = 0; instance < (sizeof(usbphy_base_temp) / sizeof(usbphy_base_temp[0])); instance++)
  32.     {
  33.         if (usbphy_base_temp[instance])
  34.         {
  35.             usbphy_base[newinstance++] = usbphy_base_temp[instance];
  36.         }
  37.     }
  38.     if (controllerId > newinstance)
  39.     {
  40.         return NULL;
  41.     }

  42.     usbPhyBase = (void *)usbphy_base[controllerId];
  43. #endif
  44.     return usbPhyBase;
  45. }

  46. static int EhciPhyInit(uint8_t controllerId, uint32_t freq, usb_phy_config_struct_t *phyConfig)
  47. {
  48. #if ((defined FSL_FEATURE_SOC_USBPHY_COUNT) && (FSL_FEATURE_SOC_USBPHY_COUNT > 0U))
  49.     USBPHY_Type *usbPhyBase;

  50.     usbPhyBase = (USBPHY_Type *)EhciPhyGetBase(controllerId);
  51.     if (NULL == usbPhyBase)
  52.     {
  53.         return -1;
  54.     }

  55. #if ((defined FSL_FEATURE_SOC_ANATOP_COUNT) && (FSL_FEATURE_SOC_ANATOP_COUNT > 0U))
  56.     ANATOP->HW_ANADIG_REG_3P0.RW =
  57.         (ANATOP->HW_ANADIG_REG_3P0.RW &
  58.          (~(ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x1F) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_ILIMIT_MASK))) |
  59.         ANATOP_HW_ANADIG_REG_3P0_OUTPUT_TRG(0x17) | ANATOP_HW_ANADIG_REG_3P0_ENABLE_LINREG_MASK;
  60.     ANATOP->HW_ANADIG_USB2_CHRG_DETECT.SET =
  61.         ANATOP_HW_ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B_MASK | ANATOP_HW_ANADIG_USB2_CHRG_DETECT_EN_B_MASK;
  62. #endif

  63. #if (defined USB_ANALOG)
  64.     USB_ANALOG->INSTANCE[controllerId].CHRG_DETECT_SET = USB_ANALOG_CHRG_DETECT_CHK_CHRG_B(1) | USB_ANALOG_CHRG_DETECT_EN_B(1);
  65. #endif

  66. #if ((!(defined FSL_FEATURE_SOC_CCM_ANALOG_COUNT)) && (!(defined FSL_FEATURE_SOC_ANATOP_COUNT)))

  67.     usbPhyBase->TRIM_OVERRIDE_EN = 0x001fU; /* override IFR value */
  68. #endif
  69.     usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK; /* support LS device. */
  70.     usbPhyBase->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL3_MASK; /* support external FS Hub with LS device connected. */
  71.     /* PWD register provides overall control of the PHY power state */
  72.     usbPhyBase->PWD = 0U;

  73.     /* Decode to trim the nominal 17.78mA current source for the High Speed TX drivers on USB_DP and USB_DM. */
  74.     usbPhyBase->TX =
  75.         ((usbPhyBase->TX & (~(USBPHY_TX_D_CAL_MASK | USBPHY_TX_TXCAL45DM_MASK | USBPHY_TX_TXCAL45DP_MASK))) |
  76.          (USBPHY_TX_D_CAL(phyConfig->D_CAL) | USBPHY_TX_TXCAL45DP(phyConfig->TXCAL45DP) |
  77.           USBPHY_TX_TXCAL45DM(phyConfig->TXCAL45DM)));
  78. #endif

  79.     return 0;
  80. }
  81. #include "..\..\..\usb\gadget\udc\fsl\fsl_usb2_udc.h"

  82. static void regdump(void)
  83. {
  84.     struct usb_dr_device *dd, USBPHY_Type *pt;

  85.     dd = (struct usb_dr_device*)0x2184000;
  86.     pt = (USBPHY_Type*)0x20C9000;

  87.     printk("------init-------\n");
  88.     printk("usbcmd: %08X\n", dd->usbcmd);
  89.         printk("usbsts: %08X\n", dd->usbsts);
  90.         printk("portsc1: %08X\n", dd->portsc1);
  91.         printk("otgsc: %08X\n", dd->otgsc);
  92.         printk("usbmode: %08X\n", dd->usbmode);
  93.         printk("configflag: %08X\n", dd->configflag);
  94.     printk("PWD: %08X\n", pt->PWD);
  95.     printk("TX: %08X\n", pt->TX);
  96.     printk("RX: %08X\n", pt->RX);
  97.     printk("CTRL: %08X\n", pt->CTRL);
  98.     printk("STATUS: %08X\n", pt->STATUS);
  99. }

  100. static int ehci0_init(struct fsl_usb2_platform_data *fpd)
  101. {
  102.     usb_phy_config_struct_t phyConfig = {
  103.         BOARD_USB_PHY_D_CAL, BOARD_USB_PHY_TXCAL45DP, BOARD_USB_PHY_TXCAL45DM,
  104.     };

  105.     CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_Usbphy480M, 480000000U);
  106.     CLOCK_EnableUsbhs0Clock(kCLOCK_Usb480M, 480000000U);

  107.     EhciPhyInit(0, 0, &phyConfig);

  108.     regdump();

  109.     return 0;
  110. }

  111. static void fpd_setup(struct fsl_usb2_platform_data *fpd, struct resource *rs, int id)
  112. {
  113.     fpd->operating_mode = FSL_USB2_DR_DEVICE;
  114.     fpd->phy_mode = FSL_USB2_PHY_UTMI;

  115.     if (id == 0)
  116.     {
  117.         fpd->init = ehci0_init;
  118.         rs[0].start = USB1_BASE;
  119.         rs[1].start = USB_OTG1_IRQn;
  120.     }
  121. }

  122. static int imx_usb_init(void)
  123. {
  124.     static struct platform_device pdev = {0};
  125.     static struct device_driver dd = {
  126.         .name = "fsl-usb",
  127.     };
  128.     static struct resource rs[] = {
  129.         {.start = 0, .flags = IORESOURCE_MEM,},
  130.         {.start = 0, .flags = IORESOURCE_IRQ,},
  131.     };
  132.     static struct fsl_usb2_platform_data fpd = {0};

  133.     fpd_setup(&fpd, rs, 0);

  134.     pdev.num_resources = ARRAY_SIZE(rs);
  135.     pdev.resource = rs;
  136.     pdev.dev.driver = ⅆ
  137.     pdev.dev.platform_data = &fpd;

  138.     INIT_LIST_HEAD(&pdev.dev.dma_pools);

  139.     fsl_udc_probe(&pdev);

  140.     return 0;
  141. }
  142. INIT_DEVICE_EXPORT(imx_usb_init);

  143. struct imxirq
  144. {
  145.     int irq;
  146.     irq_handler_t isr;
  147.     void *param;
  148.     struct rt_completion wait;
  149.     struct rt_thread thd;
  150.     int s[512];
  151. };

  152. static struct imxirq _usb0irq = {0};

  153. static void irq_thread(void *p)
  154. {
  155.     struct imxirq *ir = (struct imxirq *)p;

  156.     while (1)
  157.     {
  158.         rt_completion_wait(&ir->wait, -1);

  159.         if (ir->isr)
  160.             ir->isr(ir->irq, ir->param);

  161.         EnableIRQ(ir->irq);
  162.     }
  163. }

  164. #ifndef MCIMX6Y2_SERIES
  165. void USB_OTG1_IRQHandler(void)
  166. {
  167.     /* enter interrupt */
  168.     rt_interrupt_enter();
  169.     if (_usb0irq.isr)
  170.     {
  171.         _usb0irq.isr(_usb0irq.irq, _usb0irq.param);
  172.     }
  173.     /* leave interrupt */
  174.     rt_interrupt_leave();
  175. }

  176. int irq_hw_register(unsigned int irq, irq_handler_t handler, void *id)
  177. {
  178.     _usb0irq.irq = irq;
  179.     _usb0irq.param = id;
  180.     _usb0irq.isr = handler;

  181.     EnableIRQ(irq);

  182.     return 0;
  183. }
  184. #else
  185. static int usb0_isr(int irq, void *p)
  186. {
  187.     DisableIRQ(_usb0irq.irq);

  188.     rt_completion_done(&_usb0irq.wait);

  189.     return 0;
  190. }

  191. int irq_hw_register(unsigned int irq, irq_handler_t handler, void *id)
  192. {
  193.     struct imxirq *ir = &_usb0irq;

  194.     ir->irq = irq;
  195.     ir->param = id;
  196.     ir->isr = handler;

  197.     rt_completion_init(&ir->wait);
  198.     rt_thread_init(&ir->thd, "usbirq", irq_thread, ir,
  199.                    ir->s, sizeof(ir->s), 1, 50);
  200.     rt_thread_startup(&ir->thd);

  201.     rt_hw_interrupt_install(irq, usb0_isr, id, "usb");
  202.     EnableIRQ(irq);

  203.     return 0;
  204. }
  205. #endif
复制代码


我知道答案 目前已有1人回答
回复

使用道具 举报

该用户从未签到

712

主题

6371

帖子

0

超级版主

Rank: 8Rank: 8

积分
24891
最后登录
2025-7-21
发表于 2021-8-16 09:54:25 | 显示全部楼层
你有追踪代码的调用流程和每段代码的实际输出数据吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条

Archiver|手机版|小黑屋|恩智浦技术社区

GMT+8, 2025-7-22 03:04 , Processed in 0.084019 second(s), 22 queries , MemCache On.

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.

快速回复 返回顶部 返回列表