RE: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C controller

2020-11-14 Thread Pragnesh Patel
Hi Rick,

>-Original Message-
>From: Rick Chen 
>Sent: 13 November 2020 12:47
>To: Pragnesh Patel 
>Cc: U-Boot Mailing List ; Atish Patra
>; palmerdabb...@google.com; Bin Meng
>; Paul Walmsley ( Sifive) ;
>Anup Patel ; Sagar Kadam
>; rick ; Alan Kao
>; Leo Liang 
>Subject: Re: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C
>controller
>
>[External Email] Do not click links or attachments unless you recognize the
>sender and know the content is safe
>
>Hi Pragnesh
>
>> > From: Pragnesh Patel [mailto:pragnesh.pa...@sifive.com]
>> > Sent: Thursday, October 22, 2020 2:55 PM
>> > To: u-boot@lists.denx.de
>> > Cc: atish.pa...@wdc.com; palmerdabb...@google.com;
>> > bmeng...@gmail.com; paul.walms...@sifive.com; anup.pa...@wdc.com;
>> > sagar.ka...@sifive.com; Rick Jian-Zhi Chen(陳建志);
>> > pe...@korsgaard.com; Pragnesh Patel; Heiko Schocher
>> > Subject: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores
>> > I2C controller
>> >
>> > Add support for the OpenCores I2C controller IP core (See
>> > http://www.opencores.org/projects.cgi/web/i2c/overview).
>> >
>> > This driver implementation is inspired from the Linux OpenCores I2C
>> > driver available.
>> >
>> > Thanks to Peter Korsgaard  for writing Linux
>> > OpenCores I2C driver.
>> >
>> > Signed-off-by: Pragnesh Patel 
>> > ---
>> >
>> > Changes in v2:
>> > - Remove TYPE_SIFIVE_REV0 flag
>> > - Update the Opencores I2C Controller Link
>> >
>> >  drivers/i2c/Kconfig  |   7 +
>> >  drivers/i2c/Makefile |   1 +
>> >  drivers/i2c/ocores_i2c.c | 636
>> > +++
>> >  3 files changed, 644 insertions(+)
>> >  create mode 100644 drivers/i2c/ocores_i2c.c
>> >
>>
>> Reviewed-by: Rick Chen 
>
>Please check the CI failure item:
>https://travis-ci.org/github/rickchen36/u-boot-riscv/jobs/743114190
>
>+drivers/i2c/ocores_i2c.c: In function 'ocores_i2c_probe':
>1291+drivers/i2c/ocores_i2c.c:523:4: error: implicit declaration of
>function 'dev_err' [-Werror=implicit-function-declaration]
>1292+  523 |dev_err(dev,
>1293+  |^~~
>1294+drivers/i2c/ocores_i2c.c:528:4: error: implicit declaration of
>function 'dev_warn' [-Werror=implicit-function-declaration]
>1295+  528 |dev_warn(dev,
>1296+  |^~~~
>1297+cc1: all warnings being treated as errors
>
>Thanks,
>Rick

Will rebase and send it again.


Re: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C controller

2020-11-12 Thread Rick Chen
Hi Pragnesh

> > From: Pragnesh Patel [mailto:pragnesh.pa...@sifive.com]
> > Sent: Thursday, October 22, 2020 2:55 PM
> > To: u-boot@lists.denx.de
> > Cc: atish.pa...@wdc.com; palmerdabb...@google.com; bmeng...@gmail.com; 
> > paul.walms...@sifive.com; anup.pa...@wdc.com; sagar.ka...@sifive.com; Rick 
> > Jian-Zhi Chen(陳建志); pe...@korsgaard.com; Pragnesh Patel; Heiko Schocher
> > Subject: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C 
> > controller
> >
> > Add support for the OpenCores I2C controller IP core
> > (See http://www.opencores.org/projects.cgi/web/i2c/overview).
> >
> > This driver implementation is inspired from the Linux OpenCores
> > I2C driver available.
> >
> > Thanks to Peter Korsgaard  for writing Linux
> > OpenCores I2C driver.
> >
> > Signed-off-by: Pragnesh Patel 
> > ---
> >
> > Changes in v2:
> > - Remove TYPE_SIFIVE_REV0 flag
> > - Update the Opencores I2C Controller Link
> >
> >  drivers/i2c/Kconfig  |   7 +
> >  drivers/i2c/Makefile |   1 +
> >  drivers/i2c/ocores_i2c.c | 636 +++
> >  3 files changed, 644 insertions(+)
> >  create mode 100644 drivers/i2c/ocores_i2c.c
> >
>
> Reviewed-by: Rick Chen 

Please check the CI failure item:
https://travis-ci.org/github/rickchen36/u-boot-riscv/jobs/743114190

+drivers/i2c/ocores_i2c.c: In function 'ocores_i2c_probe':
1291+drivers/i2c/ocores_i2c.c:523:4: error: implicit declaration of
function 'dev_err' [-Werror=implicit-function-declaration]
1292+  523 |dev_err(dev,
1293+  |^~~
1294+drivers/i2c/ocores_i2c.c:528:4: error: implicit declaration of
function 'dev_warn' [-Werror=implicit-function-declaration]
1295+  528 |dev_warn(dev,
1296+  |^~~~
1297+cc1: all warnings being treated as errors

Thanks,
Rick


Re: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C controller

2020-11-10 Thread Rick Chen
> From: Pragnesh Patel [mailto:pragnesh.pa...@sifive.com]
> Sent: Thursday, October 22, 2020 2:55 PM
> To: u-boot@lists.denx.de
> Cc: atish.pa...@wdc.com; palmerdabb...@google.com; bmeng...@gmail.com; 
> paul.walms...@sifive.com; anup.pa...@wdc.com; sagar.ka...@sifive.com; Rick 
> Jian-Zhi Chen(陳建志); pe...@korsgaard.com; Pragnesh Patel; Heiko Schocher
> Subject: [PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C 
> controller
>
> Add support for the OpenCores I2C controller IP core
> (See http://www.opencores.org/projects.cgi/web/i2c/overview).
>
> This driver implementation is inspired from the Linux OpenCores
> I2C driver available.
>
> Thanks to Peter Korsgaard  for writing Linux
> OpenCores I2C driver.
>
> Signed-off-by: Pragnesh Patel 
> ---
>
> Changes in v2:
> - Remove TYPE_SIFIVE_REV0 flag
> - Update the Opencores I2C Controller Link
>
>  drivers/i2c/Kconfig  |   7 +
>  drivers/i2c/Makefile |   1 +
>  drivers/i2c/ocores_i2c.c | 636 +++
>  3 files changed, 644 insertions(+)
>  create mode 100644 drivers/i2c/ocores_i2c.c
>

Reviewed-by: Rick Chen 


[PATCH v2 1/2] i2c: ocores: add i2c driver for OpenCores I2C controller

2020-10-21 Thread Pragnesh Patel
Add support for the OpenCores I2C controller IP core
(See http://www.opencores.org/projects.cgi/web/i2c/overview).

This driver implementation is inspired from the Linux OpenCores
I2C driver available.

Thanks to Peter Korsgaard  for writing Linux
OpenCores I2C driver.

Signed-off-by: Pragnesh Patel 
---

Changes in v2:
- Remove TYPE_SIFIVE_REV0 flag
- Update the Opencores I2C Controller Link

 drivers/i2c/Kconfig  |   7 +
 drivers/i2c/Makefile |   1 +
 drivers/i2c/ocores_i2c.c | 636 +++
 3 files changed, 644 insertions(+)
 create mode 100644 drivers/i2c/ocores_i2c.c

diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig
index 8ae54e1e93..37958083af 100644
--- a/drivers/i2c/Kconfig
+++ b/drivers/i2c/Kconfig
@@ -342,6 +342,13 @@ config SYS_I2C_NEXELL
  have several I2C ports and all are provided, controlled by the
  device tree.
 
+config SYS_I2C_OCORES
+   bool "ocores I2C driver"
+   depends on DM_I2C
+   help
+ Add support for ocores I2C controller. For details see
+ https://opencores.org/projects/i2c
+
 config SYS_I2C_OMAP24XX
bool "TI OMAP2+ I2C driver"
depends on ARCH_OMAP2PLUS || ARCH_K3
diff --git a/drivers/i2c/Makefile b/drivers/i2c/Makefile
index bd248cbf52..37dc8ada6d 100644
--- a/drivers/i2c/Makefile
+++ b/drivers/i2c/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_SYS_I2C_MESON) += meson_i2c.o
 obj-$(CONFIG_SYS_I2C_MVTWSI) += mvtwsi.o
 obj-$(CONFIG_SYS_I2C_MXC) += mxc_i2c.o
 obj-$(CONFIG_SYS_I2C_NEXELL) += nx_i2c.o
+obj-$(CONFIG_SYS_I2C_OCORES) += ocores_i2c.o
 obj-$(CONFIG_SYS_I2C_OCTEON) += octeon_i2c.o
 obj-$(CONFIG_SYS_I2C_OMAP24XX) += omap24xx_i2c.o
 obj-$(CONFIG_SYS_I2C_RCAR_I2C) += rcar_i2c.o
diff --git a/drivers/i2c/ocores_i2c.c b/drivers/i2c/ocores_i2c.c
new file mode 100644
index 00..1e62e4dc5f
--- /dev/null
+++ b/drivers/i2c/ocores_i2c.c
@@ -0,0 +1,636 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * ocores-i2c.c: I2C bus driver for OpenCores I2C controller
+ * (https://opencores.org/projects/i2c)
+ *
+ * (C) Copyright Peter Korsgaard 
+ *
+ * Copyright (C) 2020 SiFive, Inc.
+ * Pragnesh Patel 
+ *
+ * Support for the GRLIB port of the controller by
+ * Andreas Larsson 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/* registers */
+#define OCI2C_PRELOW   0
+#define OCI2C_PREHIGH  1
+#define OCI2C_CONTROL  2
+#define OCI2C_DATA 3
+#define OCI2C_CMD  4 /* write only */
+#define OCI2C_STATUS   4 /* read only, same address as OCI2C_CMD */
+
+#define OCI2C_CTRL_IEN 0x40
+#define OCI2C_CTRL_EN  0x80
+
+#define OCI2C_CMD_START0x91
+#define OCI2C_CMD_STOP 0x41
+#define OCI2C_CMD_READ 0x21
+#define OCI2C_CMD_WRITE0x11
+#define OCI2C_CMD_READ_ACK 0x21
+#define OCI2C_CMD_READ_NACK0x29
+#define OCI2C_CMD_IACK 0x01
+
+#define OCI2C_STAT_IF  0x01
+#define OCI2C_STAT_TIP 0x02
+#define OCI2C_STAT_ARBLOST 0x20
+#define OCI2C_STAT_BUSY0x40
+#define OCI2C_STAT_NACK0x80
+
+#define STATE_DONE 0
+#define STATE_START1
+#define STATE_WRITE2
+#define STATE_READ 3
+#define STATE_ERROR4
+
+#define TYPE_OCORES0
+#define TYPE_GRLIB 1
+
+#define OCORES_FLAG_BROKEN_IRQ BIT(1) /* Broken IRQ for FU540-C000 SoC */
+
+struct ocores_i2c_bus {
+   void __iomem *base;
+   u32 reg_shift;
+   u32 reg_io_width;
+   unsigned long flags;
+   struct i2c_msg *msg;
+   int pos;
+   int nmsgs;
+   int state; /* see STATE_ */
+   struct clk clk;
+   int ip_clk_khz;
+   int bus_clk_khz;
+   void (*setreg)(struct ocores_i2c_bus *i2c, int reg, u8 value);
+   u8 (*getreg)(struct ocores_i2c_bus *i2c, int reg);
+};
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Boolean attribute values */
+enum {
+   FALSE = 0,
+   TRUE,
+};
+
+static void oc_setreg_8(struct ocores_i2c_bus *i2c, int reg, u8 value)
+{
+   writeb(value, i2c->base + (reg << i2c->reg_shift));
+}
+
+static void oc_setreg_16(struct ocores_i2c_bus *i2c, int reg, u8 value)
+{
+   writew(value, i2c->base + (reg << i2c->reg_shift));
+}
+
+static void oc_setreg_32(struct ocores_i2c_bus *i2c, int reg, u8 value)
+{
+   writel(value, i2c->base + (reg << i2c->reg_shift));
+}
+
+static void oc_setreg_16be(struct ocores_i2c_bus *i2c, int reg, u8 value)
+{
+   out_be16(i2c->base + (reg << i2c->reg_shift), value);
+}
+
+static void oc_setreg_32be(struct ocores_i2c_bus *i2c, int reg, u8 value)
+{
+   out_be32(i2c->base + (reg << i2c->reg_shift), value);
+}
+
+static inline u8 oc_getreg_8(struct ocores_i2c_bus *i2c, int reg)
+{
+   return readb(i2c->base + (reg << i2c->reg_shift));
+}
+
+static inline u8 oc_getreg_16(struct ocores_i2c_bus *i2c, int reg)
+{
+   return readw(i2c->base +