i2c: omap: add support for DMA
Subsystems (7)
v3 cover i2c: omap: add support for DMA
Hello,
This patchset introduces support for the TX & RX DMA transfers in the am335x/omap I2C controller. I tested this implementation on AM335x (Beaglebone Black) and things seem to work fine.
Kind regards,
Antoni Pokusinski
---
Changes since v2:
* dropped patch "i2c: omap: remove unused is_rdr and is_xdr function arguments"
* added omap_i2c_disable/enable_pio_irqs() helper functions
* probe: added if (omap->dma) before the DMA free
* xfer_data: signal DMA completion in the case of NACK interrupt
* complete_cmd: signal DMA completion in the case of bus error (e.g. AL)
* xfer_msg: removed incorrect checks if (!omap->cmd_err)
* xfer_msg: added handling of DMA/IRQ cleanup in the b_hw && stop path
Changes since v1:
* new patch "i2c: omap: remove unused is_rdr and is_xdr ..."
* probe: added goto err_dma_free instead of return in the case of failed i2c_add_numbered_adapter()
* changes in XRDY/RRDY, XDR/RDR interrupts management:
- init: do not enable the above-mentioned intrerupts
- xfer_msg: enable the interrupts before any non-DMA transfer
- xfer_msg: disable the interrupts after any non-DMA transfer
* xfer_msg: added checks on omap->cmd_err to catch NACK or AL error occured during DMA transfers
* general: moved scheme-related macros to the top of the file
Antoni Pokusinski (3):
dt-bindings: i2c: ti,omap4-i2c: Add DMA properties
ARM: dts: am33xx-l4: Add missing DMAs for I2C controllers
i2c: omap: add DMA support
.../devicetree/bindings/i2c/ti,omap4-i2c.yaml | 12 + arch/arm/boot/dts/ti/omap/am33xx-l4.dtsi | 4 + drivers/i2c/busses/i2c-omap.c | 373 +++++++++++++++++- 3 files changed, 368 insertions(+), 21 deletions(-)
Signature · 1 line
2.56.1
PATCH v3 1/3 dt-bindings: i2c: ti,omap4-i2c: Add DMA properties
Document the TX and RX DMA request channels featured by the OMAP I2C controller.
--- .../devicetree/bindings/i2c/ti,omap4-i2c.yaml | 12 ++++++++++++ 1 file changed, 12 insertions(+)
Documentation/devicetree/bindings/i2c/ti,omap4-i2c.yaml +12 -0
@@ -37,6 +37,16 @@ properties: clock-names: const: fck + dmas: + items: + - description: TX DMA channel phandle + - description: RX DMA channel phandle + + dma-names: + items: + - const: tx + - const: rx + power-domains: true ti,hwmods:
@@ -93,4 +103,6 @@ examples: #address-cells = <1>; #size-cells = <0>; mux-states = <&i2c_mux 1>; + dmas = <&edma 58 0>, <&edma 59 0>; + dma-names = "tx", "rx"; };
Signature · 1 line
2.56.1
PATCH v3 2/3 ARM: dts: am33xx-l4: Add missing DMAs for I2C controllers
In reply to Antoni PokusinskiThe i2c0 and i2c1 controllers on AM33xx support TX and RX DMA requests. Add the EDMA request mappings and DMA names for both controllers.
--- arch/arm/boot/dts/ti/omap/am33xx-l4.dtsi | 4 ++++ 1 file changed, 4 insertions(+)
arch/arm/boot/dts/ti/omap/am33xx-l4.dtsi +4 -0
@@ -239,6 +239,8 @@ i2c0: i2c@0 { reg = <0x0 0x1000>; interrupts = <70>; status = "disabled"; + dmas = <&edma 58 0>, <&edma 59 0>; + dma-names = "tx", "rx"; }; };
@@ -1189,6 +1191,8 @@ i2c1: i2c@0 { reg = <0x0 0x1000>; interrupts = <71>; status = "disabled"; + dmas = <&edma 60 0>, <&edma 61 0>; + dma-names = "tx", "rx"; }; };
Signature · 1 line
2.56.1
PATCH v3 3/3 i2c: omap: add DMA support
In reply to Antoni PokusinskiAdd support for the TX and RX DMA requests of the OMAP I2C controller. The DMA transactions are dispatched only if the I2C message size is a multiple of the configured FIFO threshold and is greater than a predefined threshold of 8 bytes.
Support for DMA transactions that are not a multiple of the FIFO threshold can be added in the future using the draining feature.
--- drivers/i2c/busses/i2c-omap.c | 373 ++++++++++++++++++++++++++++++++-- 1 file changed, 352 insertions(+), 21 deletions(-)
drivers/i2c/busses/i2c-omap.c +352 -21
@@ -5,6 +5,7 @@* Copyright (C) 2003 MontaVista Software, Inc. * Copyright (C) 2005 Nokia Corporation * Copyright (C) 2004 - 2007 Texas Instruments. +* Copyright (C) 2026 Antoni Pokusiński * * Originally written by MontaVista Software, Inc. * Additional contributions by:
@@ -13,10 +14,13 @@* Juha Yrjölä <[email protected]> * Syed Khasim <[email protected]> * Nishant Menon <[email protected]> +* Antoni Pokusiński <[email protected]> */ #include <linux/module.h> #include <linux/delay.h> +#include <linux/dma-mapping.h> +#include <linux/dmaengine.h> #include <linux/i2c.h> #include <linux/err.h> #include <linux/interrupt.h>
@@ -50,6 +54,8 @@/* timeout for making decision on bus free status */ #define OMAP_I2C_BUS_FREE_TIMEOUT (msecs_to_jiffies(10)) +#define OMAP_I2C_DMA_THRESHOLD 8 +/* For OMAP3 I2C_IV has changed to I2C_WE (wakeup enable) */ enum {OMAP_I2C_REV_REG =0 ,
@@ -57,6 +63,10 @@ enum {OMAP_I2C_STAT_REG ,OMAP_I2C_IV_REG ,OMAP_I2C_WE_REG , +OMAP_I2C_DMARXENABLE_SET , +OMAP_I2C_DMATXENABLE_SET , +OMAP_I2C_DMARXENABLE_CLR , +OMAP_I2C_DMATXENABLE_CLR ,OMAP_I2C_SYSS_REG ,OMAP_I2C_BUF_REG , OMAP_I2C_CNT_REG,
@@ -176,12 +186,37 @@ enum {#define OMAP_I2C_IP_V2_INTERRUPTS_MASK 0x6FFF +#define OMAP_I2C_SCHEME (rev) ((rev & 0xc000) >> 14) + +#define OMAP_I2C_REV_SCHEME_0_MAJOR (rev) (rev >> 4) +#define OMAP_I2C_REV_SCHEME_0_MINOR (rev) (rev & 0xf) + +#define OMAP_I2C_REV_SCHEME_1_MAJOR (rev) ((rev & 0x0700) >> 7) +#define OMAP_I2C_REV_SCHEME_1_MINOR (rev) (rev & 0x1f) +#define OMAP_I2C_SCHEME_0 0 +#define OMAP_I2C_SCHEME_1 1 + +struct omap_i2c_dma { +struct dma_chan *chan_rx; +struct dma_chan *chan_tx; +struct dma_chan *chan_using; +struct device *dma_dev; +u8 *curr_msg_buf; +struct i2c_msg *curr_msg; +dma_addr_t dma_buf; +unsigned int dma_len; +enum dma_data_direction dma_data_dir; +struct completion complete; +dma_addr_t phy_addr; +}; +struct omap_i2c_dev {struct device *dev;void __iomem *base;/* virtual */ int irq;int reg_shift;/* bit shift for I2C register addresses */ struct completion cmd_complete; +struct omap_i2c_dma *dma;struct resource *ioarea;u32 latency;/* maximum mpu wkup latency */ void (*set_mpu_wkup_lat)(struct device *dev,
@@ -242,6 +277,10 @@ static const u8 reg_map_ip_v2[] = { [OMAP_I2C_STAT_REG] = 0x28, [OMAP_I2C_IV_REG] = 0x34, [OMAP_I2C_WE_REG] = 0x34, + [OMAP_I2C_DMARXENABLE_SET] = 0x38, + [OMAP_I2C_DMATXENABLE_SET] = 0x3c, + [OMAP_I2C_DMARXENABLE_CLR] = 0x40, + [OMAP_I2C_DMATXENABLE_CLR] = 0x44, [OMAP_I2C_SYSS_REG] = 0x90, [OMAP_I2C_BUF_REG] = 0x94, [OMAP_I2C_CNT_REG] = 0x98,
@@ -462,10 +501,7 @@ static int omap_i2c_init(struct omap_i2c_dev *omap) sclh = fclk_rate / (omap->speed *2 ) -7 + psc; } - omap->iestate = (OMAP_I2C_IE_XRDY |OMAP_I2C_IE_RRDY | -OMAP_I2C_IE_ARDY |OMAP_I2C_IE_NACK | -OMAP_I2C_IE_AL ) | ((omap->fifo_size) ? - (OMAP_I2C_IE_RDR |OMAP_I2C_IE_XDR ) :0 ); + omap->iestate =OMAP_I2C_IE_ARDY |OMAP_I2C_IE_NACK |OMAP_I2C_IE_AL ; omap->pscstate = psc; omap->scllstate = scll;
@@ -655,6 +691,168 @@ static void omap_i2c_wait(struct omap_i2c_dev *omap) }while (!(stat & mask) && count <5 ); } +static void omap_i2c_dma_callback (void *arg) +{ +struct omap_i2c_dev *omap = (struct omap_i2c_dev *)arg; + +complete (&omap->dma->complete); +} + +static int omap_i2c_dma_xfer (struct omap_i2c_dev *omap,struct i2c_msg *msg) +{ +int ret; +struct dma_slave_config dma_sconfig = {0 }; +struct omap_i2c_dma *dma = omap->dma; +struct dma_async_tx_descriptor *txdesc; +struct device *dev = &omap->adapter.dev; + + dma->curr_msg = msg; + + dma->curr_msg_buf =i2c_get_dma_safe_msg_buf (msg,1 ); +if (!dma->curr_msg_buf) +return -ENOMEM ; + +if (omap->receiver) { + dma->chan_using = dma->chan_rx; + + dma_sconfig.src_addr = dma->phy_addr + + (omap->regs[OMAP_I2C_DATA_REG ] + << omap->reg_shift); + dma_sconfig.src_addr_width =DMA_SLAVE_BUSWIDTH_1_BYTE ; + dma_sconfig.src_maxburst = omap->threshold; + dma_sconfig.direction =DMA_DEV_TO_MEM ; + }else { + dma->chan_using = dma->chan_tx; + + dma_sconfig.dst_addr = dma->phy_addr + + (omap->regs[OMAP_I2C_DATA_REG ] + << omap->reg_shift); + dma_sconfig.dst_addr_width =DMA_SLAVE_BUSWIDTH_1_BYTE ; + dma_sconfig.dst_maxburst = omap->threshold; + dma_sconfig.direction =DMA_MEM_TO_DEV ; + } + + ret =dmaengine_slave_config (dma->chan_using, &dma_sconfig); +if (ret <0 ) { +dev_err (dev,"DMA channel config failed: %d\n" , ret); + goto err_map; + } + + dma->dma_len = msg->len; + dma->dma_data_dir = omap->receiver ?DMA_FROM_DEVICE :DMA_TO_DEVICE ; + + dma->dma_dev =dmaengine_get_dma_device (dma->chan_using); + + dma->dma_buf =dma_map_single (dma->dma_dev, dma->curr_msg_buf, + dma->dma_len, dma->dma_data_dir); +if (dma_mapping_error (dma->dma_dev, dma->dma_buf)) { +dev_err (dev,"DMA mapping failed\n" ); + ret = -EINVAL ; + goto err_map; + } + + txdesc =dmaengine_prep_slave_single (dma->chan_using, dma->dma_buf, + dma->dma_len, + omap->receiver ?DMA_DEV_TO_MEM : +DMA_MEM_TO_DEV , +DMA_PREP_INTERRUPT |DMA_CTRL_ACK ); +if (!txdesc) { +dev_err (dev,"Not able to get desc for DMA xfer\n" ); + ret = -EINVAL ; + goto err_desc; + } + +reinit_completion (&dma->complete); + +dmaengine_desc_set_callback (txdesc, omap_i2c_dma_callback,NULL , omap); + ret =dma_submit_error (dmaengine_submit (txdesc)); +if (ret <0 ) { +dev_err (dev,"DMA submit failed\n" ); + goto err_submit; + } + +dma_async_issue_pending (dma->chan_using); +return 0 ; + +err_submit: +dmaengine_terminate_sync (dma->chan_using); +err_desc: +dma_unmap_single (dma->dma_dev, dma->dma_buf, dma->dma_len, + dma->dma_data_dir); +err_map: +i2c_put_dma_safe_msg_buf (dma->curr_msg_buf, msg, false); +return ret; +} + +static void omap_i2c_dma_cleanup (struct omap_i2c_dev *omap,bool xferred) +{ +struct omap_i2c_dma *dma = omap->dma; + +dma_unmap_single (dma->dma_dev, dma->dma_buf, dma->dma_len, + dma->dma_data_dir); +i2c_put_dma_safe_msg_buf (dma->curr_msg_buf, dma->curr_msg, xferred); +} + +static void omap_i2c_dma_enable_req (struct omap_i2c_dev *omap) +{ +u16 w; + + w =omap_i2c_read_reg (omap,OMAP_I2C_BUF_REG ); +if (omap->receiver) { + w |=OMAP_I2C_BUF_RDMA_EN ; +omap_i2c_write_reg (omap,OMAP_I2C_BUF_REG , w); + +omap_i2c_write_reg (omap,OMAP_I2C_DMARXENABLE_SET ,1 ); + }else { + w |=OMAP_I2C_BUF_XDMA_EN ; +omap_i2c_write_reg (omap,OMAP_I2C_BUF_REG , w); + +omap_i2c_write_reg (omap,OMAP_I2C_DMATXENABLE_SET ,1 ); + } +} + +static void omap_i2c_dma_disable_req (struct omap_i2c_dev *omap) +{ +u16 w; + + w =omap_i2c_read_reg (omap,OMAP_I2C_BUF_REG ); +if (omap->receiver) { + w &= ~OMAP_I2C_BUF_RDMA_EN ; +omap_i2c_write_reg (omap,OMAP_I2C_BUF_REG , w); + +omap_i2c_write_reg (omap,OMAP_I2C_DMARXENABLE_CLR ,1 ); + }else { + w &= ~OMAP_I2C_BUF_XDMA_EN ; +omap_i2c_write_reg (omap,OMAP_I2C_BUF_REG , w); + +omap_i2c_write_reg (omap,OMAP_I2C_DMATXENABLE_CLR ,1 ); + } +} + +static void omap_i2c_enable_pio_irqs (struct omap_i2c_dev *omap,u16 mask) +{ +if (!mask) +return ; + +if (omap->scheme ==OMAP_I2C_SCHEME_0 ) +omap_i2c_write_reg (omap,OMAP_I2C_IE_REG , + omap->iestate | mask); +else +omap_i2c_write_reg (omap,OMAP_I2C_IE_REG , mask); +} + +static void omap_i2c_disable_pio_irqs (struct omap_i2c_dev *omap,u16 mask) +{ +if (!mask) +return ; + +if (omap->scheme ==OMAP_I2C_SCHEME_0 ) +omap_i2c_write_reg (omap,OMAP_I2C_IE_REG , omap->iestate); +else +omap_i2c_write_reg (omap,OMAP_I2C_IP_V2_IRQENABLE_CLR , + mask); +} +/* * Low level master read/write transaction. */
@@ -663,8 +861,8 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, {struct omap_i2c_dev *omap =i2c_get_adapdata (adap);unsigned long time_left; -u16 w; -int ret; +u16 w, pio_irq_mask; +int ret, use_dma;dev_dbg (omap->dev,"addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n" , msg->addr, msg->len, msg->flags, stop);
@@ -672,6 +870,14 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, omap->receiver = !!(msg->flags &I2C_M_RD );omap_i2c_resize_fifo (omap, msg->len, omap->receiver); +/** +* REVISIT: If the message size is not a multiple of the threshold, +* the draining feature must be used. +*/ + use_dma = !polling && omap->dma && msg->len >=OMAP_I2C_DMA_THRESHOLD && + !(omap->flags &OMAP_I2C_FLAG_NO_FIFO ) && + msg->len % omap->threshold ==0 ; +omap_i2c_write_reg (omap,OMAP_I2C_SA_REG , msg->addr);/* REVISIT: Could the STB bit of I2C_CON be used with probing? */
@@ -692,6 +898,23 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,reinit_completion (&omap->cmd_complete); omap->cmd_err =0 ; +if (use_dma) { + ret =omap_i2c_dma_xfer (omap, msg); +if (ret) { +omap_i2c_reset (omap); +__omap_i2c_init (omap); +return ret; + } + +omap_i2c_dma_enable_req (omap); + }else { + pio_irq_mask =OMAP_I2C_IE_XRDY |OMAP_I2C_IE_RRDY | + ((omap->fifo_size) ? (OMAP_I2C_IE_RDR | +OMAP_I2C_IE_XDR ) :0 ); + +omap_i2c_enable_pio_irqs (omap, pio_irq_mask); + } + w =OMAP_I2C_CON_EN |OMAP_I2C_CON_MST |OMAP_I2C_CON_STT ;/* High speed configuration */
@@ -727,6 +950,17 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,if (time_after (jiffies, delay)) {dev_err (omap->dev,"controller timed out " "waiting for start condition to finish\n" ); + +if (use_dma) { +omap_i2c_dma_disable_req (omap); + +dmaengine_terminate_sync (omap->dma->chan_using); + +omap_i2c_dma_cleanup (omap, false); + }else { +omap_i2c_disable_pio_irqs (omap, pio_irq_mask); + } +return -ETIMEDOUT ; }cpu_relax ();
@@ -742,8 +976,15 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,* into arbitration and we're currently unable to recover from it. */ if (!polling) { - time_left =wait_for_completion_timeout (&omap->cmd_complete, -OMAP_I2C_TIMEOUT ); + time_left =OMAP_I2C_TIMEOUT ; + +if (use_dma) + time_left =wait_for_completion_timeout (&omap->dma->complete, + time_left); + +if (time_left) + time_left =wait_for_completion_timeout (&omap->cmd_complete, + time_left); }else { do { omap_i2c_wait(omap);
@@ -753,7 +994,18 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, time_left = !ret; } -if (time_left ==0 ) { +if (use_dma) { +if (!time_left || omap->cmd_err) +dmaengine_terminate_sync (omap->dma->chan_using); + +omap_i2c_dma_cleanup (omap, time_left && !omap->cmd_err); + +omap_i2c_dma_disable_req (omap); + }else { +omap_i2c_disable_pio_irqs (omap, pio_irq_mask); + } + +if (!time_left) {omap_i2c_reset (omap);__omap_i2c_init (omap);return -ETIMEDOUT ;
@@ -855,6 +1107,10 @@ static inline void omap_i2c_complete_cmd(struct omap_i2c_dev *omap, u16 err) { omap->cmd_err |= err; + +if (omap->dma && err) +complete (&omap->dma->complete); +complete (&omap->cmd_complete); }
@@ -1082,6 +1338,9 @@ static int omap_i2c_xfer_data(struct omap_i2c_dev *omap) omap->cmd_err |=OMAP_I2C_STAT_NACK ;omap_i2c_ack_stat (omap,OMAP_I2C_STAT_NACK ); +if (omap->dma) +complete (&omap->dma->complete); +if (!(stat & ~OMAP_I2C_STAT_NACK )) { err = -EAGAIN ;break ;
@@ -1255,16 +1514,6 @@ static const struct of_device_id omap_i2c_of_match[] = {MODULE_DEVICE_TABLE (of, omap_i2c_of_match);#endif -#define OMAP_I2C_SCHEME (rev) ((rev & 0xc000) >> 14) - -#define OMAP_I2C_REV_SCHEME_0_MAJOR (rev) (rev >> 4) -#define OMAP_I2C_REV_SCHEME_0_MINOR (rev) (rev & 0xf) - -#define OMAP_I2C_REV_SCHEME_1_MAJOR (rev) ((rev & 0x0700) >> 7) -#define OMAP_I2C_REV_SCHEME_1_MINOR (rev) (rev & 0x1f) -#define OMAP_I2C_SCHEME_0 0 -#define OMAP_I2C_SCHEME_1 1 -static int omap_i2c_get_scl (struct i2c_adapter *adap) {struct omap_i2c_dev *dev =i2c_get_adapdata (adap);
@@ -1338,10 +1587,70 @@ static struct i2c_bus_recovery_info omap_i2c_bus_recovery_info = { .recover_bus = i2c_generic_scl_recovery, }; +static struct omap_i2c_dma *omap_i2c_dma_init (struct omap_i2c_dev *omap, +dma_addr_t phy_addr) +{ +struct device *dev = omap->dev; +struct omap_i2c_dma *dma; +int ret; + + dma =devm_kzalloc (dev,sizeof (*dma),GFP_KERNEL ); +if (!dma) +return ERR_PTR (-ENOMEM ); + + dma->phy_addr = phy_addr; + +init_completion (&dma->complete); + + dma->chan_tx =dma_request_chan (dev,"tx" ); +if (IS_ERR (dma->chan_tx)) { + ret =PTR_ERR (dma->chan_tx); +if (ret != -ENODEV ) +dev_err_probe (dev, ret, +"can't request DMA tx channel\n" ); + goto dma_free; + } + + dma->chan_rx =dma_request_chan (dev,"rx" ); +if (IS_ERR (dma->chan_rx)) { + ret =PTR_ERR (dma->chan_rx); +if (ret != -ENODEV ) +dev_err_probe (dev, ret, +"can't request DMA rx channel\n" ); + goto dma_tx_fail; + } + +return dma; + +dma_tx_fail: +dma_release_channel (dma->chan_tx); +dma_free: +devm_kfree (dev, dma); + +return ERR_PTR (ret); +} + +static void omap_i2c_dma_free (struct omap_i2c_dev *omap) +{ +struct omap_i2c_dma *dma = omap->dma; + + dma->dma_buf =0 ; + dma->dma_len =0 ; + +dma_release_channel (dma->chan_tx); + dma->chan_tx =NULL ; + +dma_release_channel (dma->chan_rx); + dma->chan_rx =NULL ; + + dma->chan_using =NULL ; +} +static int omap_i2c_probe (struct platform_device *pdev) {struct omap_i2c_dev *omap; +struct resource *res;struct i2c_adapter *adap;const struct omap_i2c_bus_platform_data *pdata =dev_get_platdata (&pdev->dev);
@@ -1359,7 +1668,7 @@ omap_i2c_probe(struct platform_device *pdev)if (!omap)return -ENOMEM ; - omap->base =devm_platform_ioremap_resource (pdev,0 ); + omap->base =devm_platform_get_and_ioremap_resource (pdev,0 , &res);if (IS_ERR (omap->base))return PTR_ERR (omap->base);
@@ -1478,6 +1787,22 @@ omap_i2c_probe(struct platform_device *pdev) goto err_unuse_clocks; } +if (omap->scheme !=OMAP_I2C_SCHEME_0 ) { + omap->dma =omap_i2c_dma_init (omap, (dma_addr_t )res->start); +if (PTR_ERR (omap->dma) == -EPROBE_DEFER ) { + r = -EPROBE_DEFER ; +dev_err_probe (&pdev->dev, r, +"can't get DMA channels\n" ); + goto err_unuse_clocks; + }else if (PTR_ERR (omap->dma) == -ENODEV ) { +dev_dbg (omap->dev,"DMA setup failed\n" ); + omap->dma =NULL ; + }else if (IS_ERR (omap->dma)) { + r =PTR_ERR (omap->dma); + goto err_unuse_clocks; + } + } + adap = &omap->adapter;i2c_set_adapdata (adap, omap); adap->owner =THIS_MODULE ;
@@ -1493,7 +1818,7 @@ omap_i2c_probe(struct platform_device *pdev) adap->nr = pdev->id; r =i2c_add_numbered_adapter (adap);if (r) - goto err_unuse_clocks; + goto err_dma_free;dev_info (omap->dev,"bus %d rev%d.%d at %d kHz\n" , adap->nr, major, minor, omap->speed);
@@ -1502,6 +1827,9 @@ omap_i2c_probe(struct platform_device *pdev)return 0 ; +err_dma_free: +if (omap->dma) +omap_i2c_dma_free (omap); err_unuse_clocks:omap_i2c_write_reg (omap,OMAP_I2C_CON_REG ,0 ); err_put_pm:
@@ -1523,6 +1851,9 @@ static void omap_i2c_remove(struct platform_device *pdev)if (omap->mux_state)mux_state_deselect (omap->mux_state); +if (omap->dma) +omap_i2c_dma_free (omap); + ret =pm_runtime_get_sync (&pdev->dev);if (ret <0 )dev_err (omap->dev,"Failed to resume hardware, skip disable\n" );
Signature · 1 line
2.56.1
[email protected]
AI reviewRe: [PATCH v3 3/3] i2c: omap: add DMA support
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider: - [Medium] i2c: omap: u8 truncation in omap_i2c_resize_fifo() breaks DMA threshold - [Medium] i2c: omap: DMA…
Thank you for your contribution! Sashiko AI review found 5 potential issues to consider:
- [Medium] i2c: omap: u8 truncation in omap_i2c_resize_fifo() breaks DMA threshold
- [Medium] i2c: omap: DMA request disabled after buffer unmap in omap_i2c_xfer_msg()
- [Medium] i2c: omap: ERR_PTR assigned to omap->dma after IRQ init in omap_i2c_probe()
- [Medium] i2c: omap: missing PIO fallback on DMA failure in omap_i2c_xfer_msg()
- [Low] i2c: omap: invalid kernel-doc comment prefix in omap_i2c_xfer_msg()
--
commit 7c0a49ff7dadae86047390fd06ce1307e0609c76
Author: Antoni Pokusinski <[email protected]>
i2c: omap: add DMA support
This commit adds TX and RX DMA support to the OMAP I2C controller for messages whose length is at least 8 bytes and a multiple of the FIFO threshold.
Quoted 4 lines
drivers/i2c/busses/i2c-omap.c +0
-0
[ ... ]
Quoted 12 lines
drivers/i2c/busses/i2c-omap.c +2
-2@@ -663,8 +861,8 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, {struct omap_i2c_dev *omap =i2c_get_adapdata (adap);unsigned long time_left; -u16 w; -int ret; +u16 w, pio_irq_mask; +int ret, use_dma;dev_dbg (omap->dev,"addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n" , msg->addr, msg->len, msg->flags, stop);
drivers/i2c/busses/i2c-omap.c static int omap_i2c_xfer_msg(struct i2c_adapter *adap, · line 876
Quoted 7 lines
drivers/i2c/busses/i2c-omap.c +4
-0omap->receiver = !!(msg->flags &I2C_M_RD );omap_i2c_resize_fifo (omap, msg->len, omap->receiver); +/** +* REVISIT: If the message size is not a multiple of the threshold, +* the draining feature must be used. +*/
[Severity: Low]
This internal comment uses the "/**" kernel-doc prefix, which causes a W=1 kernel-doc build warning.
drivers/i2c/busses/i2c-omap.c static int omap_i2c_xfer_msg(struct i2c_adapter *adap, · line 879
Quoted 3 lines
drivers/i2c/busses/i2c-omap.c +3
-0+ use_dma = !polling && omap->dma && msg->len >=OMAP_I2C_DMA_THRESHOLD && + !(omap->flags &OMAP_I2C_FLAG_NO_FIFO ) && + msg->len % omap->threshold ==0 ;
[Severity: Medium]
Does passing the 16-bit msg->len into omap_i2c_resize_fifo() truncate lengths >= 256 bytes and corrupt omap->threshold for DMA?
drivers/i2c/busses/i2c-omap.c:omap_i2c_resize_fifo() {
...
static void omap_i2c_resize_fifo(struct omap_i2c_dev *omap, u8 size, bool is_rx)
{
...
omap->threshold = clamp(size, (u8) 1, omap->fifo_size);
}For a 256-byte transfer (such as an EEPROM page read or write), size wraps to 0 and omap->threshold is clamped to 1 instead of omap->fifo_size. Because 256 % 1 == 0, use_dma is true and omap_i2c_dma_xfer() programs src_maxburst or dst_maxburst to 1 byte for the entire transfer. Changing size in omap_i2c_resize_fifo() to u16 (and widening the clamp types) would avoid the truncation.
Quoted 14 lines
drivers/i2c/busses/i2c-omap.c +6
-0omap_i2c_write_reg (omap,OMAP_I2C_SA_REG , msg->addr);/* REVISIT: Could the STB bit of I2C_CON be used with probing? */ @@ -692,6 +898,23 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap,reinit_completion (&omap->cmd_complete); omap->cmd_err =0 ; +if (use_dma) { + ret =omap_i2c_dma_xfer (omap, msg); +if (ret) { +omap_i2c_reset (omap); +__omap_i2c_init (omap); +return ret;
drivers/i2c/busses/i2c-omap.c static int omap_i2c_xfer_msg(struct i2c_adapter *adap, · line 916
Quoted 10 lines
drivers/i2c/busses/i2c-omap.c +10
-0+ } + +omap_i2c_dma_enable_req (omap); + }else { + pio_irq_mask =OMAP_I2C_IE_XRDY |OMAP_I2C_IE_RRDY | + ((omap->fifo_size) ? (OMAP_I2C_IE_RDR | +OMAP_I2C_IE_XDR ) :0 ); + +omap_i2c_enable_pio_irqs (omap, pio_irq_mask); + }
[Severity: Medium]
Should omap_i2c_xfer_msg() fall back to PIO when omap_i2c_dma_xfer() fails instead of resetting the controller and aborting the transfer?
Nothing has been started on the bus for this message yet, and in a multi-message transfer where an earlier message completed with stop == 0 (repeated start), calling omap_i2c_reset() here resets the controller mid-transaction while the bus is still held active without sending a STOP condition.
[ ... ]
Quoted 5 lines
drivers/i2c/busses/i2c-omap.c +0
-1@@ -753,7 +994,18 @@ static int omap_i2c_xfer_msg(struct i2c_adapter *adap, time_left = !ret; } -if (time_left ==0 ) {
drivers/i2c/busses/i2c-omap.c static int omap_i2c_xfer_msg(struct i2c_adapter *adap, · line 1006
Quoted 10 lines
drivers/i2c/busses/i2c-omap.c +10
-0+if (use_dma) { +if (!time_left || omap->cmd_err) +dmaengine_terminate_sync (omap->dma->chan_using); + +omap_i2c_dma_cleanup (omap, time_left && !omap->cmd_err); + +omap_i2c_dma_disable_req (omap); + }else { +omap_i2c_disable_pio_irqs (omap, pio_irq_mask); + }
[Severity: Medium]
Should omap_i2c_dma_disable_req() be called before dmaengine_terminate_sync() and omap_i2c_dma_cleanup(), matching the start-condition timeout path earlier in omap_i2c_xfer_msg()? On a timeout or bus error, the I2C controller can keep asserting DMA requests while the channel is being terminated and the buffer is unmapped.
[ ... ]
Quoted 9 lines
drivers/i2c/busses/i2c-omap.c +5
-0@@ -1478,6 +1787,22 @@ omap_i2c_probe(struct platform_device *pdev) goto err_unuse_clocks; } +if (omap->scheme !=OMAP_I2C_SCHEME_0 ) { + omap->dma =omap_i2c_dma_init (omap, (dma_addr_t )res->start); +if (PTR_ERR (omap->dma) == -EPROBE_DEFER ) { + r = -EPROBE_DEFER ; + dev_err_probe(&pdev->dev, r,
drivers/i2c/busses/i2c-omap.c omap_i2c_probe(struct platform_device *pdev) · line 1804
Quoted 10 lines
drivers/i2c/busses/i2c-omap.c +10
-0+ "can't get DMA channels\n"); + goto err_unuse_clocks; + }else if (PTR_ERR (omap->dma) == -ENODEV ) { +dev_dbg (omap->dev,"DMA setup failed\n" ); + omap->dma =NULL ; + }else if (IS_ERR (omap->dma)) { + r =PTR_ERR (omap->dma); + goto err_unuse_clocks; + } + }
[Severity: Medium]
Can assigning the return value of omap_i2c_dma_init() directly to omap->dma cause the IRQ handler to dereference an ERR_PTR?
At this point in omap_i2c_probe(), omap_i2c_init() has enabled the controller interrupts and devm_request_threaded_irq() has registered omap_i2c_isr_thread(). If omap_i2c_dma_init() returns an ERR_PTR, omap->dma is non-NULL (and remains an ERR_PTR on the err_unuse_clocks unwind path), while omap_i2c_complete_cmd() and omap_i2c_xfer_data() only check if (omap->dma) before calling complete(&omap->dma->complete). In addition, omap_i2c_dma_init() already calls dev_err_probe() on -EPROBE_DEFER, so calling dev_err_probe() again here overwrites the channel-specific deferred probe reason. Also, since DMA is optional, should errors other than -EPROBE_DEFER fall back to PIO with omap->dma = NULL instead of failing the adapter probe?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3