Re: [PATCH v4 05/11] memory: add Atmel EBI (External Bus Interface) driver

2014-12-03 Thread Nicolas Ferre
Le 02/12/2014 09:47, Boris Brezillon a écrit :
> The EBI (External Bus Interface) is used to access external peripherals
> (NOR, SRAM, NAND, and other specific devices like ethernet controllers).
> Each device is assigned a CS line and an address range and can have its
> own configuration (timings, access mode, bus width, ...).
> This driver provides a generic DT binding to configure a device according
> to its requirements.
> For specific device controllers (like the NAND one) the SMC timings
> should be configured by the controller driver through the matrix and
> smc syscon regmaps.
> 
> Signed-off-by: Jean-Jacques Hiblot 
> Signed-off-by: Boris Brezillon 
> ---
> 
> Changes since v3:
>  - use syscon_regmap_lookup_by_phandle instead of of_parse_phandle +
>syscon_node_to_regmap
>  - drop AT91_EBICSA_REGFIELD and AT91_MULTI_EBICSA_REGFIELD macros
> 
>  drivers/memory/Kconfig |  11 +
>  drivers/memory/Makefile|   1 +
>  drivers/memory/atmel-ebi.c | 618 
> +
>  3 files changed, 630 insertions(+)
>  create mode 100644 drivers/memory/atmel-ebi.c
> 
> diff --git a/drivers/memory/Kconfig b/drivers/memory/Kconfig
> index 6d91c27..dfe24a2 100644
> --- a/drivers/memory/Kconfig
> +++ b/drivers/memory/Kconfig
> @@ -17,6 +17,17 @@ config ATMEL_SDRAMC
> Starting with the at91sam9g45, this controller supports SDR, DDR and
> LP-DDR memories.
>  
> +config ATMEL_EBI
> + bool "Atmel EBI driver"
> + default y
> + depends on ARCH_AT91 && OF
> + select MFD_SYSCON
> + help
> +   Driver for Atmel EBI controller.
> +   Used to configure the EBI (external bus interface) when the device-
> +   tree is used. This bus supports NANDs, external ethernet controller,
> +   SRAMs, ATA devices, etc.
> +
>  config TI_AEMIF
>   tristate "Texas Instruments AEMIF driver"
>   depends on (ARCH_DAVINCI || ARCH_KEYSTONE) && OF
> diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile
> index c32d319..7ca2c19 100644
> --- a/drivers/memory/Makefile
> +++ b/drivers/memory/Makefile
> @@ -6,6 +6,7 @@ ifeq ($(CONFIG_DDR),y)
>  obj-$(CONFIG_OF) += of_memory.o
>  endif
>  obj-$(CONFIG_ATMEL_SDRAMC)   += atmel-sdramc.o
> +obj-$(CONFIG_ATMEL_EBI)  += atmel-ebi.o
>  obj-$(CONFIG_TI_AEMIF)   += ti-aemif.o
>  obj-$(CONFIG_TI_EMIF)+= emif.o
>  obj-$(CONFIG_FSL_CORENET_CF) += fsl-corenet-cf.o
> diff --git a/drivers/memory/atmel-ebi.c b/drivers/memory/atmel-ebi.c
> new file mode 100644
> index 000..8702694
> --- /dev/null
> +++ b/drivers/memory/atmel-ebi.c
> @@ -0,0 +1,618 @@
> +/*
> + * EBI driver for Atmel SAM9 chips

Not only SAM9: remove it or add SAMA5.

> + * inspired by the fsl weim bus driver
> + *
> + * Copyright (C) 2013 JJ Hiblot.
> + *
> + * This file is licensed under the terms of the GNU General Public
> + * License version 2. This program is licensed "as is" without any
> + * warranty of any kind, whether express or implied.
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +struct at91sam9_smc_timings {
> + u32 ncs_rd_setup_ns;
> + u32 nrd_setup_ns;
> + u32 ncs_wr_setup_ns;
> + u32 nwe_setup_ns;
> + u32 ncs_rd_pulse_ns;
> + u32 nrd_pulse_ns;
> + u32 ncs_wr_pulse_ns;
> + u32 nwe_pulse_ns;
> + u32 nrd_cycle_ns;
> + u32 nwe_cycle_ns;
> + u32 tdf_ns;
> +};
> +
> +struct at91sam9_smc_generic_fields {
> + struct regmap_field *setup;
> + struct regmap_field *pulse;
> + struct regmap_field *cycle;
> + struct regmap_field *mode;
> +};
> +
> +struct at91sam9_ebi_dev_config {
> + struct at91sam9_smc_timings timings;
> + u32 mode;
> +};
> +
> +struct at91_ebi;
> +
> +struct at91_ebi_dev {
> + struct device_node *np;
> + struct at91_smc_timings *timings;
> + struct at91_ebi *ebi;
> + u32 mode;
> + int cs;
> + void *config;
> +};
> +
> +struct at91_ebi_caps {
> + unsigned int available_cs;
> + const struct reg_field *ebi_csa;
> + int (*xlate_config)(struct at91_ebi_dev *ebid);
> + int (*apply_config)(struct at91_ebi_dev *ebid);
> + int (*init)(struct at91_ebi *ebi);
> +};
> +
> +struct at91_ebi {
> + struct clk *clk;
> + struct regmap *smc;
> + struct regmap *matrix;
> +
> + struct regmap_field *ebi_csa;
> +
> + struct device *dev;
> + const struct at91_ebi_caps *caps;
> + struct at91_ebi_dev *devs[AT91_MATRIX_EBI_NUM_CS];
> + void *priv;
> +};
> +
> +static u32 at91sam9_smc_setup_ns_to_cycles(unsigned int clk_rate,
> +u32 timing_ns)
> +{
> + u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
> + u32 coded_cycles = 0;
> + u32 cycles;
> +
> + cycles = DIV_ROUND_UP(timing_ns, clk_period);
> + if (cycles / 32) {

I know it is the formula from the datasheet but please add a note to
refer to it.
So, a comment here 

Re: [PATCH v4 05/11] memory: add Atmel EBI (External Bus Interface) driver

2014-12-03 Thread Nicolas Ferre
Le 02/12/2014 09:47, Boris Brezillon a écrit :
 The EBI (External Bus Interface) is used to access external peripherals
 (NOR, SRAM, NAND, and other specific devices like ethernet controllers).
 Each device is assigned a CS line and an address range and can have its
 own configuration (timings, access mode, bus width, ...).
 This driver provides a generic DT binding to configure a device according
 to its requirements.
 For specific device controllers (like the NAND one) the SMC timings
 should be configured by the controller driver through the matrix and
 smc syscon regmaps.
 
 Signed-off-by: Jean-Jacques Hiblot jjhib...@traphandler.com
 Signed-off-by: Boris Brezillon boris.brezil...@free-electrons.com
 ---
 
 Changes since v3:
  - use syscon_regmap_lookup_by_phandle instead of of_parse_phandle +
syscon_node_to_regmap
  - drop AT91_EBICSA_REGFIELD and AT91_MULTI_EBICSA_REGFIELD macros
 
  drivers/memory/Kconfig |  11 +
  drivers/memory/Makefile|   1 +
  drivers/memory/atmel-ebi.c | 618 
 +
  3 files changed, 630 insertions(+)
  create mode 100644 drivers/memory/atmel-ebi.c
 
 diff --git a/drivers/memory/Kconfig b/drivers/memory/Kconfig
 index 6d91c27..dfe24a2 100644
 --- a/drivers/memory/Kconfig
 +++ b/drivers/memory/Kconfig
 @@ -17,6 +17,17 @@ config ATMEL_SDRAMC
 Starting with the at91sam9g45, this controller supports SDR, DDR and
 LP-DDR memories.
  
 +config ATMEL_EBI
 + bool Atmel EBI driver
 + default y
 + depends on ARCH_AT91  OF
 + select MFD_SYSCON
 + help
 +   Driver for Atmel EBI controller.
 +   Used to configure the EBI (external bus interface) when the device-
 +   tree is used. This bus supports NANDs, external ethernet controller,
 +   SRAMs, ATA devices, etc.
 +
  config TI_AEMIF
   tristate Texas Instruments AEMIF driver
   depends on (ARCH_DAVINCI || ARCH_KEYSTONE)  OF
 diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile
 index c32d319..7ca2c19 100644
 --- a/drivers/memory/Makefile
 +++ b/drivers/memory/Makefile
 @@ -6,6 +6,7 @@ ifeq ($(CONFIG_DDR),y)
  obj-$(CONFIG_OF) += of_memory.o
  endif
  obj-$(CONFIG_ATMEL_SDRAMC)   += atmel-sdramc.o
 +obj-$(CONFIG_ATMEL_EBI)  += atmel-ebi.o
  obj-$(CONFIG_TI_AEMIF)   += ti-aemif.o
  obj-$(CONFIG_TI_EMIF)+= emif.o
  obj-$(CONFIG_FSL_CORENET_CF) += fsl-corenet-cf.o
 diff --git a/drivers/memory/atmel-ebi.c b/drivers/memory/atmel-ebi.c
 new file mode 100644
 index 000..8702694
 --- /dev/null
 +++ b/drivers/memory/atmel-ebi.c
 @@ -0,0 +1,618 @@
 +/*
 + * EBI driver for Atmel SAM9 chips

Not only SAM9: remove it or add SAMA5.

 + * inspired by the fsl weim bus driver
 + *
 + * Copyright (C) 2013 JJ Hiblot.
 + *
 + * This file is licensed under the terms of the GNU General Public
 + * License version 2. This program is licensed as is without any
 + * warranty of any kind, whether express or implied.
 + */
 +
 +#include linux/clk.h
 +#include linux/io.h
 +#include linux/mfd/syscon.h
 +#include linux/mfd/syscon/atmel-matrix.h
 +#include linux/mfd/syscon/atmel-smc.h
 +#include linux/module.h
 +#include linux/of_device.h
 +#include linux/regmap.h
 +
 +struct at91sam9_smc_timings {
 + u32 ncs_rd_setup_ns;
 + u32 nrd_setup_ns;
 + u32 ncs_wr_setup_ns;
 + u32 nwe_setup_ns;
 + u32 ncs_rd_pulse_ns;
 + u32 nrd_pulse_ns;
 + u32 ncs_wr_pulse_ns;
 + u32 nwe_pulse_ns;
 + u32 nrd_cycle_ns;
 + u32 nwe_cycle_ns;
 + u32 tdf_ns;
 +};
 +
 +struct at91sam9_smc_generic_fields {
 + struct regmap_field *setup;
 + struct regmap_field *pulse;
 + struct regmap_field *cycle;
 + struct regmap_field *mode;
 +};
 +
 +struct at91sam9_ebi_dev_config {
 + struct at91sam9_smc_timings timings;
 + u32 mode;
 +};
 +
 +struct at91_ebi;
 +
 +struct at91_ebi_dev {
 + struct device_node *np;
 + struct at91_smc_timings *timings;
 + struct at91_ebi *ebi;
 + u32 mode;
 + int cs;
 + void *config;
 +};
 +
 +struct at91_ebi_caps {
 + unsigned int available_cs;
 + const struct reg_field *ebi_csa;
 + int (*xlate_config)(struct at91_ebi_dev *ebid);
 + int (*apply_config)(struct at91_ebi_dev *ebid);
 + int (*init)(struct at91_ebi *ebi);
 +};
 +
 +struct at91_ebi {
 + struct clk *clk;
 + struct regmap *smc;
 + struct regmap *matrix;
 +
 + struct regmap_field *ebi_csa;
 +
 + struct device *dev;
 + const struct at91_ebi_caps *caps;
 + struct at91_ebi_dev *devs[AT91_MATRIX_EBI_NUM_CS];
 + void *priv;
 +};
 +
 +static u32 at91sam9_smc_setup_ns_to_cycles(unsigned int clk_rate,
 +u32 timing_ns)
 +{
 + u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
 + u32 coded_cycles = 0;
 + u32 cycles;
 +
 + cycles = DIV_ROUND_UP(timing_ns, clk_period);
 + if (cycles / 32) {

I know it is the formula from the datasheet but please add a 

[PATCH v4 05/11] memory: add Atmel EBI (External Bus Interface) driver

2014-12-02 Thread Boris Brezillon
The EBI (External Bus Interface) is used to access external peripherals
(NOR, SRAM, NAND, and other specific devices like ethernet controllers).
Each device is assigned a CS line and an address range and can have its
own configuration (timings, access mode, bus width, ...).
This driver provides a generic DT binding to configure a device according
to its requirements.
For specific device controllers (like the NAND one) the SMC timings
should be configured by the controller driver through the matrix and
smc syscon regmaps.

Signed-off-by: Jean-Jacques Hiblot 
Signed-off-by: Boris Brezillon 
---

Changes since v3:
 - use syscon_regmap_lookup_by_phandle instead of of_parse_phandle +
   syscon_node_to_regmap
 - drop AT91_EBICSA_REGFIELD and AT91_MULTI_EBICSA_REGFIELD macros

 drivers/memory/Kconfig |  11 +
 drivers/memory/Makefile|   1 +
 drivers/memory/atmel-ebi.c | 618 +
 3 files changed, 630 insertions(+)
 create mode 100644 drivers/memory/atmel-ebi.c

diff --git a/drivers/memory/Kconfig b/drivers/memory/Kconfig
index 6d91c27..dfe24a2 100644
--- a/drivers/memory/Kconfig
+++ b/drivers/memory/Kconfig
@@ -17,6 +17,17 @@ config ATMEL_SDRAMC
  Starting with the at91sam9g45, this controller supports SDR, DDR and
  LP-DDR memories.
 
+config ATMEL_EBI
+   bool "Atmel EBI driver"
+   default y
+   depends on ARCH_AT91 && OF
+   select MFD_SYSCON
+   help
+ Driver for Atmel EBI controller.
+ Used to configure the EBI (external bus interface) when the device-
+ tree is used. This bus supports NANDs, external ethernet controller,
+ SRAMs, ATA devices, etc.
+
 config TI_AEMIF
tristate "Texas Instruments AEMIF driver"
depends on (ARCH_DAVINCI || ARCH_KEYSTONE) && OF
diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile
index c32d319..7ca2c19 100644
--- a/drivers/memory/Makefile
+++ b/drivers/memory/Makefile
@@ -6,6 +6,7 @@ ifeq ($(CONFIG_DDR),y)
 obj-$(CONFIG_OF)   += of_memory.o
 endif
 obj-$(CONFIG_ATMEL_SDRAMC) += atmel-sdramc.o
+obj-$(CONFIG_ATMEL_EBI)+= atmel-ebi.o
 obj-$(CONFIG_TI_AEMIF) += ti-aemif.o
 obj-$(CONFIG_TI_EMIF)  += emif.o
 obj-$(CONFIG_FSL_CORENET_CF)   += fsl-corenet-cf.o
diff --git a/drivers/memory/atmel-ebi.c b/drivers/memory/atmel-ebi.c
new file mode 100644
index 000..8702694
--- /dev/null
+++ b/drivers/memory/atmel-ebi.c
@@ -0,0 +1,618 @@
+/*
+ * EBI driver for Atmel SAM9 chips
+ * inspired by the fsl weim bus driver
+ *
+ * Copyright (C) 2013 JJ Hiblot.
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed "as is" without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct at91sam9_smc_timings {
+   u32 ncs_rd_setup_ns;
+   u32 nrd_setup_ns;
+   u32 ncs_wr_setup_ns;
+   u32 nwe_setup_ns;
+   u32 ncs_rd_pulse_ns;
+   u32 nrd_pulse_ns;
+   u32 ncs_wr_pulse_ns;
+   u32 nwe_pulse_ns;
+   u32 nrd_cycle_ns;
+   u32 nwe_cycle_ns;
+   u32 tdf_ns;
+};
+
+struct at91sam9_smc_generic_fields {
+   struct regmap_field *setup;
+   struct regmap_field *pulse;
+   struct regmap_field *cycle;
+   struct regmap_field *mode;
+};
+
+struct at91sam9_ebi_dev_config {
+   struct at91sam9_smc_timings timings;
+   u32 mode;
+};
+
+struct at91_ebi;
+
+struct at91_ebi_dev {
+   struct device_node *np;
+   struct at91_smc_timings *timings;
+   struct at91_ebi *ebi;
+   u32 mode;
+   int cs;
+   void *config;
+};
+
+struct at91_ebi_caps {
+   unsigned int available_cs;
+   const struct reg_field *ebi_csa;
+   int (*xlate_config)(struct at91_ebi_dev *ebid);
+   int (*apply_config)(struct at91_ebi_dev *ebid);
+   int (*init)(struct at91_ebi *ebi);
+};
+
+struct at91_ebi {
+   struct clk *clk;
+   struct regmap *smc;
+   struct regmap *matrix;
+
+   struct regmap_field *ebi_csa;
+
+   struct device *dev;
+   const struct at91_ebi_caps *caps;
+   struct at91_ebi_dev *devs[AT91_MATRIX_EBI_NUM_CS];
+   void *priv;
+};
+
+static u32 at91sam9_smc_setup_ns_to_cycles(unsigned int clk_rate,
+  u32 timing_ns)
+{
+   u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
+   u32 coded_cycles = 0;
+   u32 cycles;
+
+   cycles = DIV_ROUND_UP(timing_ns, clk_period);
+   if (cycles / 32) {
+   coded_cycles |= 1 << 5;
+   if (cycles < 128)
+   cycles = 0;
+   }
+
+   coded_cycles |= cycles % 32;
+
+   return coded_cycles;
+}
+
+static u32 at91sam9_smc_pulse_ns_to_cycles(unsigned int clk_rate,
+  u32 timing_ns)
+{
+   u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
+ 

[PATCH v4 05/11] memory: add Atmel EBI (External Bus Interface) driver

2014-12-02 Thread Boris Brezillon
The EBI (External Bus Interface) is used to access external peripherals
(NOR, SRAM, NAND, and other specific devices like ethernet controllers).
Each device is assigned a CS line and an address range and can have its
own configuration (timings, access mode, bus width, ...).
This driver provides a generic DT binding to configure a device according
to its requirements.
For specific device controllers (like the NAND one) the SMC timings
should be configured by the controller driver through the matrix and
smc syscon regmaps.

Signed-off-by: Jean-Jacques Hiblot jjhib...@traphandler.com
Signed-off-by: Boris Brezillon boris.brezil...@free-electrons.com
---

Changes since v3:
 - use syscon_regmap_lookup_by_phandle instead of of_parse_phandle +
   syscon_node_to_regmap
 - drop AT91_EBICSA_REGFIELD and AT91_MULTI_EBICSA_REGFIELD macros

 drivers/memory/Kconfig |  11 +
 drivers/memory/Makefile|   1 +
 drivers/memory/atmel-ebi.c | 618 +
 3 files changed, 630 insertions(+)
 create mode 100644 drivers/memory/atmel-ebi.c

diff --git a/drivers/memory/Kconfig b/drivers/memory/Kconfig
index 6d91c27..dfe24a2 100644
--- a/drivers/memory/Kconfig
+++ b/drivers/memory/Kconfig
@@ -17,6 +17,17 @@ config ATMEL_SDRAMC
  Starting with the at91sam9g45, this controller supports SDR, DDR and
  LP-DDR memories.
 
+config ATMEL_EBI
+   bool Atmel EBI driver
+   default y
+   depends on ARCH_AT91  OF
+   select MFD_SYSCON
+   help
+ Driver for Atmel EBI controller.
+ Used to configure the EBI (external bus interface) when the device-
+ tree is used. This bus supports NANDs, external ethernet controller,
+ SRAMs, ATA devices, etc.
+
 config TI_AEMIF
tristate Texas Instruments AEMIF driver
depends on (ARCH_DAVINCI || ARCH_KEYSTONE)  OF
diff --git a/drivers/memory/Makefile b/drivers/memory/Makefile
index c32d319..7ca2c19 100644
--- a/drivers/memory/Makefile
+++ b/drivers/memory/Makefile
@@ -6,6 +6,7 @@ ifeq ($(CONFIG_DDR),y)
 obj-$(CONFIG_OF)   += of_memory.o
 endif
 obj-$(CONFIG_ATMEL_SDRAMC) += atmel-sdramc.o
+obj-$(CONFIG_ATMEL_EBI)+= atmel-ebi.o
 obj-$(CONFIG_TI_AEMIF) += ti-aemif.o
 obj-$(CONFIG_TI_EMIF)  += emif.o
 obj-$(CONFIG_FSL_CORENET_CF)   += fsl-corenet-cf.o
diff --git a/drivers/memory/atmel-ebi.c b/drivers/memory/atmel-ebi.c
new file mode 100644
index 000..8702694
--- /dev/null
+++ b/drivers/memory/atmel-ebi.c
@@ -0,0 +1,618 @@
+/*
+ * EBI driver for Atmel SAM9 chips
+ * inspired by the fsl weim bus driver
+ *
+ * Copyright (C) 2013 JJ Hiblot.
+ *
+ * This file is licensed under the terms of the GNU General Public
+ * License version 2. This program is licensed as is without any
+ * warranty of any kind, whether express or implied.
+ */
+
+#include linux/clk.h
+#include linux/io.h
+#include linux/mfd/syscon.h
+#include linux/mfd/syscon/atmel-matrix.h
+#include linux/mfd/syscon/atmel-smc.h
+#include linux/module.h
+#include linux/of_device.h
+#include linux/regmap.h
+
+struct at91sam9_smc_timings {
+   u32 ncs_rd_setup_ns;
+   u32 nrd_setup_ns;
+   u32 ncs_wr_setup_ns;
+   u32 nwe_setup_ns;
+   u32 ncs_rd_pulse_ns;
+   u32 nrd_pulse_ns;
+   u32 ncs_wr_pulse_ns;
+   u32 nwe_pulse_ns;
+   u32 nrd_cycle_ns;
+   u32 nwe_cycle_ns;
+   u32 tdf_ns;
+};
+
+struct at91sam9_smc_generic_fields {
+   struct regmap_field *setup;
+   struct regmap_field *pulse;
+   struct regmap_field *cycle;
+   struct regmap_field *mode;
+};
+
+struct at91sam9_ebi_dev_config {
+   struct at91sam9_smc_timings timings;
+   u32 mode;
+};
+
+struct at91_ebi;
+
+struct at91_ebi_dev {
+   struct device_node *np;
+   struct at91_smc_timings *timings;
+   struct at91_ebi *ebi;
+   u32 mode;
+   int cs;
+   void *config;
+};
+
+struct at91_ebi_caps {
+   unsigned int available_cs;
+   const struct reg_field *ebi_csa;
+   int (*xlate_config)(struct at91_ebi_dev *ebid);
+   int (*apply_config)(struct at91_ebi_dev *ebid);
+   int (*init)(struct at91_ebi *ebi);
+};
+
+struct at91_ebi {
+   struct clk *clk;
+   struct regmap *smc;
+   struct regmap *matrix;
+
+   struct regmap_field *ebi_csa;
+
+   struct device *dev;
+   const struct at91_ebi_caps *caps;
+   struct at91_ebi_dev *devs[AT91_MATRIX_EBI_NUM_CS];
+   void *priv;
+};
+
+static u32 at91sam9_smc_setup_ns_to_cycles(unsigned int clk_rate,
+  u32 timing_ns)
+{
+   u32 clk_period = DIV_ROUND_UP(NSEC_PER_SEC, clk_rate);
+   u32 coded_cycles = 0;
+   u32 cycles;
+
+   cycles = DIV_ROUND_UP(timing_ns, clk_period);
+   if (cycles / 32) {
+   coded_cycles |= 1  5;
+   if (cycles  128)
+   cycles = 0;
+   }
+
+   coded_cycles |= cycles % 32;
+
+   return coded_cycles;
+}
+
+static