Skip to content

Commit 769eafb

Browse files
tmon-nordicgithub-actions[bot]
authored andcommitted
[nrf fromtree] drivers: udc_dwc2: Add helpers to check operating mode
Use helper functions to check whether device is operating in Buffer DMA or Completer mode. This allows compile time optimizations to remove DMA handling code when DMA is disabled via KConfig symbol UDC_DWC2_DMA. Signed-off-by: Tomasz Moń <[email protected]> (cherry picked from commit d4cc8de) (cherry picked from commit a284c39)
1 parent e24e23b commit 769eafb

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

drivers/usb/udc/udc_dwc2.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,20 @@ static void dwc2_wait_for_bit(const struct device *dev,
203203
}
204204
}
205205

206+
static inline bool dwc2_in_completer_mode(const struct device *dev)
207+
{
208+
struct udc_dwc2_data *const priv = udc_get_private(dev);
209+
210+
return !IS_ENABLED(CONFIG_UDC_DWC2_DMA) || !priv->bufferdma;
211+
}
212+
213+
static inline bool dwc2_in_buffer_dma_mode(const struct device *dev)
214+
{
215+
struct udc_dwc2_data *const priv = udc_get_private(dev);
216+
217+
return IS_ENABLED(CONFIG_UDC_DWC2_DMA) && priv->bufferdma;
218+
}
219+
206220
/* Get DOEPCTLn or DIEPCTLn register address */
207221
static mem_addr_t dwc2_get_dxepctl_reg(const struct device *dev, const uint8_t ep)
208222
{
@@ -422,7 +436,7 @@ static int dwc2_tx_fifo_write(const struct device *dev,
422436
len = buf->len;
423437
}
424438

425-
if (!priv->bufferdma) {
439+
if (dwc2_in_completer_mode(dev)) {
426440
uint32_t spcavail = dwc2_ftx_avail(dev, ep_idx);
427441
uint32_t spcperpkt = ROUND_UP(udc_mps_ep_size(cfg), 4);
428442
uint32_t max_pkts, max_transfer;
@@ -487,7 +501,7 @@ static int dwc2_tx_fifo_write(const struct device *dev,
487501
usb_dwc2_set_dieptsizn_pktcnt(pktcnt) |
488502
usb_dwc2_set_dieptsizn_xfersize(len), dieptsiz_reg);
489503

490-
if (priv->bufferdma) {
504+
if (dwc2_in_buffer_dma_mode(dev)) {
491505
if (!dwc2_dma_buffer_ok_to_use(dev, buf->data, len, cfg->mps)) {
492506
/* Cannot continue unless buffer is bounced. Device will
493507
* cease to function. Is fatal error appropriate here?
@@ -530,7 +544,7 @@ static int dwc2_tx_fifo_write(const struct device *dev,
530544
/* Clear IN Endpoint NAK Effective interrupt in case it was set */
531545
sys_write32(USB_DWC2_DIEPINT_INEPNAKEFF, diepint_reg);
532546

533-
if (!priv->bufferdma) {
547+
if (dwc2_in_completer_mode(dev)) {
534548
const uint8_t *src = buf->data;
535549

536550
while (pktcnt > 0) {
@@ -659,7 +673,7 @@ static void dwc2_prep_rx(const struct device *dev, struct net_buf *buf,
659673
priv->rx_siz[ep_idx] = doeptsiz;
660674
sys_write32(doeptsiz, doeptsiz_reg);
661675

662-
if (priv->bufferdma) {
676+
if (dwc2_in_buffer_dma_mode(dev)) {
663677
void *data = net_buf_tail(buf);
664678

665679
if (!dwc2_dma_buffer_ok_to_use(dev, data, xfersize, cfg->mps)) {
@@ -1211,7 +1225,7 @@ static int dwc2_set_dedicated_fifo(const struct device *dev,
12111225
tmp = *diepctl & ~USB_DWC2_DEPCTL_TXFNUM_MASK;
12121226

12131227
reqdep = DIV_ROUND_UP(udc_mps_ep_size(cfg), 4U);
1214-
if (priv->bufferdma) {
1228+
if (dwc2_in_buffer_dma_mode(dev)) {
12151229
/* In DMA mode, TxFIFO capable of holding 2 packets is enough */
12161230
reqdep *= MIN(2, (1 + addnl));
12171231
} else {
@@ -2292,15 +2306,15 @@ static void dwc2_on_bus_reset(const struct device *dev)
22922306
}
22932307

22942308
doepmsk = USB_DWC2_DOEPINT_SETUP | USB_DWC2_DOEPINT_XFERCOMPL;
2295-
if (priv->bufferdma) {
2309+
if (dwc2_in_buffer_dma_mode(dev)) {
22962310
doepmsk |= USB_DWC2_DOEPINT_STSPHSERCVD;
22972311
}
22982312

22992313
sys_write32(doepmsk, (mem_addr_t)&base->doepmsk);
23002314
sys_set_bits((mem_addr_t)&base->diepmsk, USB_DWC2_DIEPINT_XFERCOMPL);
23012315

23022316
/* Software has to handle RxFLvl interrupt only in Completer mode */
2303-
if (!priv->bufferdma) {
2317+
if (dwc2_in_completer_mode(dev)) {
23042318
sys_set_bits((mem_addr_t)&base->gintmsk,
23052319
USB_DWC2_GINTSTS_RXFLVL);
23062320
}
@@ -2506,7 +2520,7 @@ static inline void dwc2_handle_out_xfercompl(const struct device *dev,
25062520
}
25072521

25082522
if (!valid) {
2509-
if (!priv->bufferdma) {
2523+
if (dwc2_in_completer_mode(dev)) {
25102524
/* RxFlvl added data to net buf, rollback */
25112525
net_buf_remove_mem(buf, bcnt);
25122526
}
@@ -2515,7 +2529,7 @@ static inline void dwc2_handle_out_xfercompl(const struct device *dev,
25152529
}
25162530
}
25172531

2518-
if (priv->bufferdma && bcnt) {
2532+
if (dwc2_in_buffer_dma_mode(dev) && bcnt) {
25192533
sys_cache_data_invd_range(net_buf_tail(buf), bcnt);
25202534
net_buf_add(buf, bcnt);
25212535
}
@@ -2559,7 +2573,8 @@ static inline void dwc2_handle_oepint(const struct device *dev)
25592573
/* StupPktRcvd is not enabled for interrupt, but must be checked
25602574
* when XferComp hits to determine if SETUP token was received.
25612575
*/
2562-
if (priv->bufferdma && (status & USB_DWC2_DOEPINT_XFERCOMPL) &&
2576+
if (dwc2_in_buffer_dma_mode(dev) &&
2577+
(status & USB_DWC2_DOEPINT_XFERCOMPL) &&
25632578
(doepint & USB_DWC2_DOEPINT_STUPPKTRCVD)) {
25642579
uint32_t addr;
25652580

0 commit comments

Comments
 (0)