All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec
@ 2014-07-15  0:56 Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params Simon Glass
                   ` (25 more replies)
  0 siblings, 26 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Up until now driver model has not been used for any type of bus. Buses
have some unique properties and needs, so we cannot claim that driver
model can cover all the common cases unless we have converted a bus over
to driver model.

SPI is a reasonable choice for this next step. It has a fairly simple
API and not too many dependencies. The main one is SPI flash so we may
as well convert that also. Since the boards I test with have cros_ec I
have also included that, for SPI only.

The technique used is make use of driver model's supported data structures
to hold information currently kept by each subsystem in a private data
structure. Since 'struct spi_slave' relates to the slave device on the bus
it is stored in the 'parent' data with each child device of the bus.
Since 'struct spi_flash' is a standard interface used for each SPI flash
driver, it is stored in the SPI FLash uclass's private data for each
device.

New defines are created to enable driver model for each subsystem. These
are:

   CONFIG_DM_SPI
   CONFIG_DM_SPI_FLASH
   CONFIG_DM_CROS_EC

This allows us to move some boards and drivers to driver model, while
leaving others behind. A 'big bang' conversion of everything to driver
model, event at a subsystem level, is never going to work.

On the other hand, we change the driver at the same time as the CONFIG
option is enabled. Keeping both version of the driver around involves a
flock of #ifdefs, the benefit of which is not apparent to me, since the
old code is removed anyway.

There is some cost in changing the uclass interface after it is created,
so if you have limited time, please spend it reviewing the uclass
interfaces in spi.h and spi_flash.h. These need to be supported by each
driver, so changing them later may involve changing multiple drivers.

To help with conversion of SPI drivers to driver model, documentation is
provided which takes the happy camper through the process with an example.

As always, driver model patches are available at u-boot-dm.git branch
'working'.

Note: This series is not fully ready - e.g. some header files are missing
comments. But I wanted to get it out for review early since some SPI work
is ongoing which might depend on it.


Simon Glass (25):
  sandbox: Convert SPI flash emulation to use sf_params
  sandbox: config: Enable all SPI flash chips
  sandbox: dts: Add a SPI device and cros_ec device
  dm: spi: Move cmd device code into its own function
  spi: Add brackets and tidy defines in spi.h
  dm: spi: Add a uclass for SPI
  dm: sandbox: Add a SPI emulation uclass
  dm: Remove spi_init() from board_r.c when using driver model
  dm: Add spi.h header to a few files
  dm: spi: Adjust cmd_spi to work with driver model
  dm: sandbox: spi: Move to driver model
  dm: spi: Add documentation on how to convert over SPI drivers
  dm: exynos: Convert SPI to driver model
  sf: Add an empty entry to the parameter list
  sf: Tidy up public and private header files
  spi: Use error return value in sf_ops
  dm: sf: Add a uclass for SPI flash
  dm: Convert spi_flash_probe() and 'sf probe' to use driver model
  dm: sf: sandbox: Convert SPI flash driver to driver model
  dm: exynos: config: Use driver model for SPI flash
  dm: spi: Add tests
  dm: sf: Add tests for SPI flash
  dm: cros_ec: Add support for driver model
  dm: sandbox: cros_ec: Move sandbox cros_ec to driver module
  dm: exynos: cros_ec: Move cros_ec_spi to driver model

 arch/arm/dts/exynos5250-snow.dts            |   8 +
 arch/arm/dts/exynos5420-peach-pit.dts       |   1 +
 arch/sandbox/dts/sandbox.dts                |  25 ++
 arch/sandbox/include/asm/spi.h              |  13 -
 arch/sandbox/include/asm/state.h            |   2 +-
 board/buffalo/lsxl/lsxl.c                   |   3 +-
 board/samsung/common/board.c                |   3 -
 common/board_r.c                            |   2 +-
 common/cmd_sf.c                             |  24 ++
 common/cmd_spi.c                            |  73 +++-
 common/cros_ec.c                            |  30 ++
 common/env_sf.c                             |   1 +
 common/exports.c                            |   4 +-
 doc/device-tree-bindings/mtd/spi/m25p80.txt |  29 ++
 doc/driver-model/spi-howto.txt              | 570 ++++++++++++++++++++++++++++
 drivers/misc/cros_ec.c                      | 122 +++++-
 drivers/misc/cros_ec_sandbox.c              |  99 ++++-
 drivers/misc/cros_ec_spi.c                  |  68 +++-
 drivers/mtd/spi/Makefile                    |   7 +-
 drivers/mtd/spi/ramtron.c                   |   1 +
 drivers/mtd/spi/sandbox.c                   | 438 +++++++++++++++------
 drivers/mtd/spi/sf-uclass.c                 |  63 +++
 drivers/mtd/spi/sf_internal.h               |  67 +++-
 drivers/mtd/spi/sf_params.c                 |   2 +
 drivers/mtd/spi/sf_probe.c                  | 153 ++++++--
 drivers/mtd/spi/spi_spl_load.c              |   1 +
 drivers/spi/Makefile                        |   5 +
 drivers/spi/exynos_spi.c                    | 509 +++++++++----------------
 drivers/spi/sandbox_spi.c                   | 198 ++++------
 drivers/spi/spi-emul-uclass.c               |  15 +
 drivers/spi/spi-uclass.c                    | 253 ++++++++++++
 include/configs/exynos5-dt.h                |   2 +
 include/configs/sandbox.h                   |  13 +-
 include/configs/smdk5420.h                  |   1 +
 include/cros_ec.h                           |  27 +-
 include/dm/uclass-id.h                      |   4 +
 include/spi.h                               | 194 +++++++++-
 include/spi_flash.h                         | 127 ++++---
 test/dm/Makefile                            |   2 +
 test/dm/sf.c                                |  43 +++
 test/dm/spi.c                               |  47 +++
 test/dm/test.dts                            |  17 +-
 42 files changed, 2504 insertions(+), 762 deletions(-)
 create mode 100644 doc/device-tree-bindings/mtd/spi/m25p80.txt
 create mode 100644 doc/driver-model/spi-howto.txt
 create mode 100644 drivers/mtd/spi/sf-uclass.c
 create mode 100644 drivers/spi/spi-emul-uclass.c
 create mode 100644 drivers/spi/spi-uclass.c
 create mode 100644 test/dm/sf.c
 create mode 100644 test/dm/spi.c

-- 
2.0.0.526.g5318336

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-25  9:24   ` Jagan Teki
  2014-07-15  0:56 ` [U-Boot] [PATCH 02/25] sandbox: config: Enable all SPI flash chips Simon Glass
                   ` (24 subsequent siblings)
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

At present sandbox has its own table of supported SPI flash chips. Now that
the SPI flash system is fully consolidated and has its own list, sandbox
should use that.

This enables us to expand the number of chips that sandbox supports.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/mtd/spi/sandbox.c | 114 ++++++++++++++++------------------------------
 1 file changed, 38 insertions(+), 76 deletions(-)

diff --git a/drivers/mtd/spi/sandbox.c b/drivers/mtd/spi/sandbox.c
index a62ef4c..98e0a34 100644
--- a/drivers/mtd/spi/sandbox.c
+++ b/drivers/mtd/spi/sandbox.c
@@ -51,46 +51,7 @@ static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
 /* Assume all SPI flashes have 3 byte addresses since they do atm */
 #define SF_ADDR_LEN	3
 
-struct sandbox_spi_flash_erase_commands {
-	u8 cmd;
-	u32 size;
-};
-#define IDCODE_LEN 5
-#define MAX_ERASE_CMDS 3
-struct sandbox_spi_flash_data {
-	const char *name;
-	u8 idcode[IDCODE_LEN];
-	u32 size;
-	const struct sandbox_spi_flash_erase_commands
-						erase_cmds[MAX_ERASE_CMDS];
-};
-
-/* Structure describing all the flashes we know how to emulate */
-static const struct sandbox_spi_flash_data sandbox_sf_flashes[] = {
-	{
-		"M25P16", { 0x20, 0x20, 0x15 }, (2 << 20),
-		{	/* erase commands */
-			{ 0xd8, (64 << 10), }, /* sector */
-			{ 0xc7, (2 << 20), }, /* bulk */
-		},
-	},
-	{
-		"W25Q32", { 0xef, 0x40, 0x16 }, (4 << 20),
-		{	/* erase commands */
-			{ 0x20, (4 << 10), }, /* 4KB */
-			{ 0xd8, (64 << 10), }, /* sector */
-			{ 0xc7, (4 << 20), }, /* bulk */
-		},
-	},
-	{
-		"W25Q128", { 0xef, 0x40, 0x18 }, (16 << 20),
-		{	/* erase commands */
-			{ 0x20, (4 << 10), }, /* 4KB */
-			{ 0xd8, (64 << 10), }, /* sector */
-			{ 0xc7, (16 << 20), }, /* bulk */
-		},
-	},
-};
+#define IDCODE_LEN 3
 
 /* Used to quickly bulk erase backing store */
 static u8 sandbox_sf_0xff[0x1000];
@@ -109,7 +70,8 @@ struct sandbox_spi_flash {
 	 */
 	enum sandbox_sf_state state;
 	uint cmd;
-	const void *cmd_data;
+	/* Erase size of current erase command */
+	uint erase_size;
 	/* Current position in the flash; used when reading/writing/etc... */
 	uint off;
 	/* How many address bytes we've consumed */
@@ -117,7 +79,7 @@ struct sandbox_spi_flash {
 	/* The current flash status (see STAT_XXX defines above) */
 	u16 status;
 	/* Data describing the flash we're emulating */
-	const struct sandbox_spi_flash_data *data;
+	const struct spi_flash_params *data;
 	/* The file on disk to serv up data from */
 	int fd;
 };
@@ -127,8 +89,8 @@ static int sandbox_sf_setup(void **priv, const char *spec)
 	/* spec = idcode:file */
 	struct sandbox_spi_flash *sbsf;
 	const char *file;
-	size_t i, len, idname_len;
-	const struct sandbox_spi_flash_data *data;
+	size_t len, idname_len;
+	const struct spi_flash_params *data;
 
 	file = strchr(spec, ':');
 	if (!file) {
@@ -138,15 +100,14 @@ static int sandbox_sf_setup(void **priv, const char *spec)
 	idname_len = file - spec;
 	++file;
 
-	for (i = 0; i < ARRAY_SIZE(sandbox_sf_flashes); ++i) {
-		data = &sandbox_sf_flashes[i];
+	for (data = spi_flash_params_table; data->name; data++) {
 		len = strlen(data->name);
 		if (idname_len != len)
 			continue;
 		if (!memcmp(spec, data->name, len))
 			break;
 	}
-	if (i == ARRAY_SIZE(sandbox_sf_flashes)) {
+	if (!data->name) {
 		printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
 		       spec);
 		goto error;
@@ -223,7 +184,6 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
 		sbsf->pad_addr_bytes = 1;
 	case CMD_READ_ARRAY_SLOW:
 	case CMD_PAGE_PROGRAM:
- state_addr:
 		sbsf->state = SF_ADDR;
 		break;
 	case CMD_WRITE_DISABLE:
@@ -241,24 +201,25 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
 		sbsf->status |= STAT_WEL;
 		break;
 	default: {
-		size_t i;
-
-		/* handle erase commands first */
-		for (i = 0; i < MAX_ERASE_CMDS; ++i) {
-			const struct sandbox_spi_flash_erase_commands *
-				erase_cmd = &sbsf->data->erase_cmds[i];
-
-			if (erase_cmd->cmd == 0x00)
-				continue;
-			if (sbsf->cmd != erase_cmd->cmd)
-				continue;
-
-			sbsf->cmd_data = erase_cmd;
-			goto state_addr;
+		int flags = sbsf->data->flags;
+
+		/* we only support erase here */
+		if (sbsf->cmd == CMD_ERASE_CHIP) {
+			sbsf->erase_size = sbsf->data->sector_size *
+				sbsf->data->nr_sectors;
+		} else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
+			sbsf->erase_size = 4 << 10;
+		} else if (sbsf->cmd == CMD_ERASE_32K && (flags & SECT_32K)) {
+			sbsf->erase_size = 32 << 10;
+		} else if (sbsf->cmd == CMD_ERASE_64K &&
+			   !(flags & (SECT_4K | SECT_32K))) {
+			sbsf->erase_size = 64 << 10;
+		} else {
+			debug(" cmd unknown: %#x\n", sbsf->cmd);
+			return 1;
 		}
-
-		debug(" cmd unknown: %#x\n", sbsf->cmd);
-		return 1;
+		sbsf->state = SF_ADDR;
+		break;
 	}
 	}
 
@@ -309,11 +270,14 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 			u8 id;
 
 			debug(" id: off:%u tx:", sbsf->off);
-			if (sbsf->off < IDCODE_LEN)
-				id = sbsf->data->idcode[sbsf->off];
-			else
+			if (sbsf->off < IDCODE_LEN) {
+				/* Extract correct byte from ID 0x00aabbcc */
+				id = sbsf->data->jedec >>
+					(8 * (IDCODE_LEN - 1 - sbsf->off));
+			} else {
 				id = 0;
-			debug("%02x\n", id);
+			}
+			debug("%d %02x\n", sbsf->off, id);
 			tx[pos++] = id;
 			++sbsf->off;
 			break;
@@ -406,24 +370,22 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 			break;
 		case SF_ERASE:
  case_sf_erase: {
-			const struct sandbox_spi_flash_erase_commands *
-						erase_cmd = sbsf->cmd_data;
-
 			if (!(sbsf->status & STAT_WEL)) {
 				puts("sandbox_sf: write enable not set before erase\n");
 				goto done;
 			}
 
 			/* verify address is aligned */
-			if (sbsf->off & (erase_cmd->size - 1)) {
+			if (sbsf->off & (sbsf->erase_size - 1)) {
 				debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
-				      erase_cmd->cmd, erase_cmd->size,
+				      sbsf->cmd, sbsf->erase_size,
 				      sbsf->off);
 				sbsf->status &= ~STAT_WEL;
 				goto done;
 			}
 
-			debug(" sector erase addr: %u\n", sbsf->off);
+			debug(" sector erase addr: %u, size: %u\n", sbsf->off,
+			      sbsf->erase_size);
 
 			cnt = bytes - pos;
 			sandbox_spi_tristate(&tx[pos], cnt);
@@ -433,7 +395,7 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 			 * TODO(vapier at gentoo.org): latch WIP in status, and
 			 * delay before clearing it ?
 			 */
-			ret = sandbox_erase_part(sbsf, erase_cmd->size);
+			ret = sandbox_erase_part(sbsf, sbsf->erase_size);
 			sbsf->status &= ~STAT_WEL;
 			if (ret) {
 				debug("sandbox_sf: Erase failed\n");
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 02/25] sandbox: config: Enable all SPI flash chips
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-25  9:24   ` Jagan Teki
  2014-07-15  0:56 ` [U-Boot] [PATCH 03/25] sandbox: dts: Add a SPI device and cros_ec device Simon Glass
                   ` (23 subsequent siblings)
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Sandbox may as well support everything. This increases the amount of code
that is built/tested by sandbox, and also provides access to all the
supported SPI flash devices.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/configs/sandbox.h | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index f5fa4b3..5e4e5fc 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -94,7 +94,7 @@
 #define CONFIG_ENV_SIZE		8192
 #define CONFIG_ENV_IS_NOWHERE
 
-/* SPI */
+/* SPI - enable all SPI flash types for testing purposes */
 #define CONFIG_SANDBOX_SPI
 #define CONFIG_CMD_SF
 #define CONFIG_CMD_SF_TEST
@@ -102,7 +102,13 @@
 #define CONFIG_SPI_FLASH
 #define CONFIG_OF_SPI
 #define CONFIG_OF_SPI_FLASH
+#define CONFIG_SPI_FLASH_ATMEL
+#define CONFIG_SPI_FLASH_EON
+#define CONFIG_SPI_FLASH_GIGADEVICE
+#define CONFIG_SPI_FLASH_MACRONIX
 #define CONFIG_SPI_FLASH_SANDBOX
+#define CONFIG_SPI_FLASH_SPANSION
+#define CONFIG_SPI_FLASH_SST
 #define CONFIG_SPI_FLASH_STMICRO
 #define CONFIG_SPI_FLASH_WINBOND
 
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 03/25] sandbox: dts: Add a SPI device and cros_ec device
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 02/25] sandbox: config: Enable all SPI flash chips Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-25  9:32   ` Jagan Teki
  2014-07-15  0:56 ` [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function Simon Glass
                   ` (22 subsequent siblings)
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Add a SPI device which can be used for testing SPI flash features in
sandbox.

Also add a cros_ec device since with driver model the Chrome OS EC
emulation will not otherwise be available.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/sandbox/dts/sandbox.dts                | 25 +++++++++++++++++++++++++
 doc/device-tree-bindings/mtd/spi/m25p80.txt | 29 +++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+)
 create mode 100644 doc/device-tree-bindings/mtd/spi/m25p80.txt

diff --git a/arch/sandbox/dts/sandbox.dts b/arch/sandbox/dts/sandbox.dts
index daf5e73..fad1fae 100644
--- a/arch/sandbox/dts/sandbox.dts
+++ b/arch/sandbox/dts/sandbox.dts
@@ -1,6 +1,9 @@
 /dts-v1/;
 
 / {
+	#address-cells = <1>;
+	#size-cells = <0>;
+
 	aliases {
 		console = "/serial";
 	};
@@ -131,5 +134,27 @@
 		num-gpios = <20>;
 	};
 
+	spi at 0 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <0>;
+		compatible = "sandbox,spi";
+		cs-gpios = <0>, <&gpio_a 0>;
+		flash at 0 {
+			reg = <0>;
+			compatible = "spansion,m25p16", "sandbox,spi-flash";
+			spi-max-frequency = <40000000>;
+			sandbox,filename = "spi.bin";
+		};
+	};
+
+	cros-ec at 0 {
+		compatible = "google,cros-ec";
+		#address-cells = <1>;
+		#size-cells = <1>;
+		firmware_storage_spi: flash at 0 {
+			reg = <0 0x400000>;
+		};
+	};
 
 };
diff --git a/doc/device-tree-bindings/mtd/spi/m25p80.txt b/doc/device-tree-bindings/mtd/spi/m25p80.txt
new file mode 100644
index 0000000..6d3d576
--- /dev/null
+++ b/doc/device-tree-bindings/mtd/spi/m25p80.txt
@@ -0,0 +1,29 @@
+* MTD SPI driver for ST M25Pxx (and similar) serial flash chips
+
+Required properties:
+- #address-cells, #size-cells : Must be present if the device has sub-nodes
+  representing partitions.
+- compatible : Should be the manufacturer and the name of the chip. Bear in mind
+               the DT binding is not Linux-only, but in case of Linux, see the
+               "m25p_ids" table in drivers/mtd/devices/m25p80.c for the list of
+               supported chips.
+- reg : Chip-Select number
+- spi-max-frequency : Maximum frequency of the SPI bus the chip can operate at
+
+Optional properties:
+- m25p,fast-read : Use the "fast read" opcode to read data from the chip instead
+                   of the usual "read" opcode. This opcode is not supported by
+                   all chips and support for it can not be detected at runtime.
+                   Refer to your chips' datasheet to check if this is supported
+                   by your chip.
+
+Example:
+
+	flash: m25p80 at 0 {
+		#address-cells = <1>;
+		#size-cells = <1>;
+		compatible = "spansion,m25p80";
+		reg = <0>;
+		spi-max-frequency = <40000000>;
+		m25p,fast-read;
+	};
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (2 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 03/25] sandbox: dts: Add a SPI device and cros_ec device Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-25 18:31   ` Jagan Teki
  2014-07-15  0:56 ` [U-Boot] [PATCH 05/25] spi: Add brackets and tidy defines in spi.h Simon Glass
                   ` (21 subsequent siblings)
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

In preparation for changing the error handling in this code for driver
model, move it into its own function.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/cmd_spi.c | 53 ++++++++++++++++++++++++++++++++---------------------
 1 file changed, 32 insertions(+), 21 deletions(-)

diff --git a/common/cmd_spi.c b/common/cmd_spi.c
index 3c8e913..be5709c 100644
--- a/common/cmd_spi.c
+++ b/common/cmd_spi.c
@@ -11,6 +11,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <errno.h>
 #include <spi.h>
 
 /*-----------------------------------------------------------------------
@@ -38,6 +39,35 @@ static int   		bitlen;
 static uchar 		dout[MAX_SPI_BYTES];
 static uchar 		din[MAX_SPI_BYTES];
 
+static int do_spi_xfer(int bus, int cs)
+{
+	struct spi_slave *slave;
+	int rcode = 0;
+
+	slave = spi_setup_slave(bus, cs, 1000000, mode);
+	if (!slave) {
+		printf("Invalid device %d:%d\n", bus, cs);
+		return -EINVAL;
+	}
+
+	spi_claim_bus(slave);
+	if (spi_xfer(slave, bitlen, dout, din,
+		     SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
+		printf("Error during SPI transaction\n");
+		rcode = -EIO;
+	} else {
+		int j;
+
+		for (j = 0; j < ((bitlen + 7) / 8); j++)
+			printf("%02X", din[j]);
+		printf("\n");
+	}
+	spi_release_bus(slave);
+	spi_free_slave(slave);
+
+	return rcode;
+}
+
 /*
  * SPI read/write
  *
@@ -51,11 +81,9 @@ static uchar 		din[MAX_SPI_BYTES];
 
 int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	struct spi_slave *slave;
 	char  *cp = 0;
 	uchar tmp;
 	int   j;
-	int   rcode = 0;
 
 	/*
 	 * We use the last specified parameters, unless new ones are
@@ -103,27 +131,10 @@ int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return 1;
 	}
 
-	slave = spi_setup_slave(bus, cs, 1000000, mode);
-	if (!slave) {
-		printf("Invalid device %d:%d\n", bus, cs);
+	if (do_spi_xfer(bus, cs))
 		return 1;
-	}
-
-	spi_claim_bus(slave);
-	if(spi_xfer(slave, bitlen, dout, din,
-				SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
-		printf("Error during SPI transaction\n");
-		rcode = 1;
-	} else {
-		for(j = 0; j < ((bitlen + 7) / 8); j++) {
-			printf("%02X", din[j]);
-		}
-		printf("\n");
-	}
-	spi_release_bus(slave);
-	spi_free_slave(slave);
 
-	return rcode;
+	return 0;
 }
 
 /***************************************************/
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 05/25] spi: Add brackets and tidy defines in spi.h
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (3 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-25  9:34   ` Jagan Teki
  2014-07-15  0:56 ` [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI Simon Glass
                   ` (20 subsequent siblings)
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Some of the #defines in spi.h are not bracketed. To avoid future mistakes
add brackets. Also add an explanatory comment for SPI_CONN_DUAL_...

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/spi.h | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/include/spi.h b/include/spi.h
index ffd6647..b673be2 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -30,24 +30,24 @@
 #define SPI_XFER_MMAP		0x08	/* Memory Mapped start */
 #define SPI_XFER_MMAP_END	0x10	/* Memory Mapped End */
 #define SPI_XFER_ONCE		(SPI_XFER_BEGIN | SPI_XFER_END)
-#define SPI_XFER_U_PAGE		(1 << 5)
+#define SPI_XFER_U_PAGE	(1 << 5)
 
 /* SPI TX operation modes */
-#define SPI_OPM_TX_QPP		1 << 0
+#define SPI_OPM_TX_QPP		(1 << 0)
 
 /* SPI RX operation modes */
-#define SPI_OPM_RX_AS		1 << 0
-#define SPI_OPM_RX_DOUT		1 << 1
-#define SPI_OPM_RX_DIO		1 << 2
-#define SPI_OPM_RX_QOF		1 << 3
-#define SPI_OPM_RX_QIOF		1 << 4
-#define SPI_OPM_RX_EXTN		SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \
+#define SPI_OPM_RX_AS		(1 << 0)
+#define SPI_OPM_RX_DOUT	(1 << 1)
+#define SPI_OPM_RX_DIO		(1 << 2)
+#define SPI_OPM_RX_QOF		(1 << 3)
+#define SPI_OPM_RX_QIOF	(1 << 4)
+#define SPI_OPM_RX_EXTN	(SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \
 				SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \
-				SPI_OPM_RX_QIOF
+				SPI_OPM_RX_QIOF)
 
-/* SPI bus connection options */
-#define SPI_CONN_DUAL_SHARED	1 << 0
-#define SPI_CONN_DUAL_SEPARATED	1 << 1
+/* SPI bus connection options - see enum spi_dual_flash */
+#define SPI_CONN_DUAL_SHARED		(1 << 0)
+#define SPI_CONN_DUAL_SEPARATED	(1 << 1)
 
 /* Header byte that marks the start of the message */
 #define SPI_PREAMBLE_END_BYTE	0xec
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (4 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 05/25] spi: Add brackets and tidy defines in spi.h Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  8:26   ` Pavel Herrmann
                     ` (2 more replies)
  2014-07-15  0:56 ` [U-Boot] [PATCH 07/25] dm: sandbox: Add a SPI emulation uclass Simon Glass
                   ` (19 subsequent siblings)
  25 siblings, 3 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Add a uclass which provides access to SPI buses and includes operations
required by SPI.

For a time driver model will need to co-exist with the legacy SPI interface
so some parts of the header file are changed depending on which is in use.
The exports are adjusted also since some functions are not available with
driver model.

Boards must define CONFIG_DM_SPI to use driver model for SPI.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/exports.c         |   4 +-
 drivers/spi/Makefile     |   4 +
 drivers/spi/spi-uclass.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h   |   1 +
 include/spi.h            | 140 ++++++++++++++++++++++++++
 5 files changed, 401 insertions(+), 1 deletion(-)
 create mode 100644 drivers/spi/spi-uclass.c

diff --git a/common/exports.c b/common/exports.c
index b97ca48..88fcfc8 100644
--- a/common/exports.c
+++ b/common/exports.c
@@ -27,10 +27,12 @@ unsigned long get_version(void)
 # define i2c_write         dummy
 # define i2c_read          dummy
 #endif
-#ifndef CONFIG_CMD_SPI
+#if !defined(CONFIG_CMD_SPI) || defined(CONFIG_DM_SPI)
 # define spi_init          dummy
 # define spi_setup_slave   dummy
 # define spi_free_slave    dummy
+#endif
+#ifndef CONFIG_CMD_SPI
 # define spi_claim_bus     dummy
 # define spi_release_bus   dummy
 # define spi_xfer          dummy
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index f02c35a..d1f1dd0 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -6,7 +6,11 @@
 #
 
 # There are many options which enable SPI, so make this library available
+ifdef CONFIG_DM_SPI
+obj-y += spi-uclass.o
+else
 obj-y += spi.o
+endif
 
 obj-$(CONFIG_EP93XX_SPI) += ep93xx_spi.o
 obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c
new file mode 100644
index 0000000..4057bce
--- /dev/null
+++ b/drivers/spi/spi-uclass.c
@@ -0,0 +1,253 @@
+/*
+ * Copyright (c) 2014 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <errno.h>
+#include <fdtdec.h>
+#include <spi.h>
+#include <dm/device-internal.h>
+#include <dm/uclass-internal.h>
+#include <dm/root.h>
+#include <dm/lists.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
+{
+	struct dm_spi_ops *ops;
+	int ret;
+
+	ops = spi_get_ops(bus);
+	if (ops->set_speed)
+		ret = (*ops->set_speed)(bus, speed);
+	else
+		ret = -EINVAL;
+	if (ret) {
+		printf("Cannot set speed (err=%d)\n", ret);
+		return ret;
+	}
+
+	ops = spi_get_ops(bus);
+	if (ops->set_mode)
+		ret = (*ops->set_mode)(bus, mode);
+	else
+		ret = -EINVAL;
+	if (ret) {
+		printf("Cannot set mode (err=%d)\n", ret);
+		return ret;
+	}
+
+	return 0;
+}
+
+int spi_claim_bus(struct spi_slave *slave)
+{
+	struct udevice *dev = slave->dev;
+	struct udevice *bus = dev->parent;
+	struct dm_spi_ops *ops = spi_get_ops(bus);
+	struct dm_spi_bus *spi = bus->uclass_priv;
+	int speed;
+	int ret;
+
+	speed = slave->max_hz;
+	if (spi->max_hz) {
+		if (speed)
+			speed = min(speed, spi->max_hz);
+		else
+			speed = spi->max_hz;
+	}
+	if (!speed)
+		speed = 100000;
+	ret = spi_set_speed_mode(bus, speed, slave->mode);
+	if (ret)
+		return ret;
+
+	return ops->claim_bus ? ops->claim_bus(bus) : 0;
+}
+
+void spi_release_bus(struct spi_slave *slave)
+{
+	struct udevice *dev = slave->dev;
+	struct udevice *bus = dev->parent;
+	struct dm_spi_ops *ops = spi_get_ops(bus);
+
+	if (ops->release_bus)
+		spi_get_ops(bus)->release_bus(bus);
+}
+
+int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
+	     const void *dout, void *din, unsigned long flags)
+{
+	struct udevice *dev = slave->dev;
+	struct udevice *bus = dev->parent;
+
+	if (bus->uclass->uc_drv->id != UCLASS_SPI)
+		return -EOPNOTSUPP;
+
+	return spi_get_ops(bus)->xfer(bus, dev, bitlen, dout, din, flags);
+}
+
+int spi_post_bind(struct udevice *dev)
+{
+	/* Scan the bus for devices */
+	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
+}
+
+int spi_post_probe(struct udevice *dev)
+{
+	struct dm_spi_bus *spi = dev->uclass_priv;
+
+	spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
+				     "spi-max-frequency", 0);
+
+	return 0;
+}
+
+int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
+		    const char *dev_name, struct udevice **slavep)
+{
+	struct driver *drv;
+	int ret;
+
+	drv = lists_driver_lookup_name(drv_name);
+	if (!drv) {
+		puts("Cannot find spi_flash_std driver\n");
+		return -ENOENT;
+	}
+	ret = device_bind(bus, drv, dev_name, NULL, -1, slavep);
+	if (ret) {
+		printf("Cannot create device named '%s' (err=%d)\n",
+		       dev_name, ret);
+		return ret;
+	}
+	(*slavep)->req_seq = cs;
+
+	return 0;
+}
+
+int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
+			struct udevice **devp)
+{
+	struct udevice *bus, *dev;
+	int ret;
+
+	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
+	if (ret)
+		return ret;
+	ret = device_find_child_by_seq(bus, cs, false, &dev);
+	if (ret)
+		return ret;
+	*busp = bus;
+	*devp = dev;
+
+	return ret;
+}
+
+int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
+		       const char *drv_name, const char *dev_name,
+		       struct udevice **devp, struct spi_slave **slavep)
+{
+	struct udevice *bus, *dev;
+	int ret;
+
+	ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
+	if (ret) {
+		printf("Invalid bus %d (err=%d)\n", busnum, ret);
+		return ret;
+	}
+	ret = device_get_child_by_seq(bus, cs, &dev);
+
+	/**
+	 * If there is no such device, create one automatically. This means
+	 * that we don't need a device tree node or platform data for the
+	 * SPI flash chip - we will bind to the correct driver.
+	 */
+	if (ret == -ENODEV && drv_name) {
+		ret = spi_bind_device(bus, cs, drv_name, dev_name, &dev);
+		if (ret)
+			return ret;
+	}
+	if (ret) {
+		printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
+		       ret);
+		return ret;
+	}
+
+	ret = spi_set_speed_mode(bus, speed, mode);
+	if (ret)
+		return ret;
+
+	*devp = bus;
+	*slavep = dev_get_parentdata(dev);
+
+	return 0;
+}
+
+/* Compatibility function - to be removed */
+struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
+				      int bus_node)
+{
+	struct udevice *bus, *dev;
+	int ret;
+
+	ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
+	if (ret)
+		return NULL;
+	ret = device_get_child_by_of_offset(bus, node, &dev);
+	if (ret)
+		return NULL;
+	return dev_get_parentdata(dev);
+}
+
+/* Compatibility function - to be removed */
+struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
+				  unsigned int speed, unsigned int mode)
+{
+	struct spi_slave *slave;
+	struct udevice *dev;
+	int ret;
+
+	ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
+				  &slave);
+	if (ret)
+		return NULL;
+
+	return slave;
+}
+
+void spi_free_slave(struct spi_slave *slave)
+{
+	device_remove(slave->dev);
+	slave->dev = NULL;
+}
+
+int spi_ofdata_to_platdata(const void *blob, int node,
+			   struct spi_slave *spi)
+{
+	int mode = 0;
+
+	spi->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
+	if (fdtdec_get_bool(blob, node, "spi-cpol"))
+		mode |= SPI_CPOL;
+	if (fdtdec_get_bool(blob, node, "spi-cpha"))
+		mode |= SPI_CPHA;
+	if (fdtdec_get_bool(blob, node, "spi-cs-high"))
+		mode |= SPI_CS_HIGH;
+	if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
+		mode |= SPI_PREAMBLE;
+	spi->mode = mode;
+
+	return 0;
+}
+
+UCLASS_DRIVER(spi) = {
+	.id		= UCLASS_SPI,
+	.name		= "spi",
+	.post_bind	= spi_post_bind,
+	.post_probe	= spi_post_probe,
+	.per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 7f0e37b..8207483 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -22,6 +22,7 @@ enum uclass_id {
 	/* U-Boot uclasses start here */
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
 	UCLASS_SERIAL,		/* Serial UART */
+	UCLASS_SPI,		/* SPI bus */
 
 	UCLASS_COUNT,
 	UCLASS_INVALID = -1,
diff --git a/include/spi.h b/include/spi.h
index b673be2..b5e9347 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -54,11 +54,19 @@
 
 #define SPI_DEFAULT_WORDLEN 8
 
+#ifdef CONFIG_DM_SPI
+struct dm_spi_bus {
+	uint max_hz;
+};
+
+#endif /* CONFIG_DM_SPI */
+
 /**
  * struct spi_slave - Representation of a SPI slave
  *
  * Drivers are expected to extend this with controller-specific data.
  *
+ * @dev:		SPI slave device
  * @bus:		ID of the bus that the slave is attached to.
  * @cs:			ID of the chip select connected to the slave.
  * @op_mode_rx:		SPI RX operation mode.
@@ -71,8 +79,14 @@
  * @flags:		Indication of SPI flags.
  */
 struct spi_slave {
+#ifdef CONFIG_DM_SPI
+	struct udevice *dev;	/* struct spi_slave is dev->parentdata */
+	uint max_hz;
+	uint mode;
+#else
 	unsigned int bus;
 	unsigned int cs;
+#endif
 	u8 op_mode_rx;
 	u8 op_mode_tx;
 	unsigned int wordlen;
@@ -220,6 +234,8 @@ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
 int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 		void *din, unsigned long flags);
 
+#ifndef CONFIG_DM_SPI
+
 /**
  * Determine if a SPI chipselect is valid.
  * This function is provided by the board if the low-level SPI driver
@@ -255,6 +271,7 @@ void spi_cs_deactivate(struct spi_slave *slave);
  * @hz:		The transfer speed
  */
 void spi_set_speed(struct spi_slave *slave, uint hz);
+#endif
 
 /**
  * Write 8 bits, then read 8 bits.
@@ -305,4 +322,127 @@ struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
 struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
 					   int node);
 
+#ifdef CONFIG_DM_SPI
+
+/**
+ * struct struct dm_spi_ops - Driver model SPI operations
+ *
+ * The uclass interface is implemented by all SPI devices which use
+ * driver model.
+ */
+struct dm_spi_ops {
+	/**
+	 * Claim the bus and prepare it for communication.
+	 *
+	 * The device privided is the slave device. It's parent controller
+	 * will be used to provide the communication.
+	 *
+	 * This must be called before doing any transfers with a SPI slave. It
+	 * will enable and initialize any SPI hardware as necessary, and make
+	 * sure that the SCK line is in the correct idle state. It is not
+	 * allowed to claim the same bus for several slaves without releasing
+	 * the bus in between.
+	 *
+	 * @device:	The SPI slave
+	 *
+	 * Returns: 0 if the bus was claimed successfully, or a negative value
+	 * if it wasn't.
+	 */
+	int (*claim_bus)(struct udevice *device);
+
+	/**
+	 * Release the SPI bus
+	 *
+	 * This must be called once for every call to spi_claim_bus() after
+	 * all transfers have finished. It may disable any SPI hardware as
+	 * appropriate.
+	 *
+	 * @device:	The SPI slave
+	 */
+	int (*release_bus)(struct udevice *device);
+
+	/**
+	 * Set the word length for SPI transactions
+	 *
+	 * Set the word length (number of bits per word) for SPI transactions.
+	 *
+	 * @device:	The SPI slave
+	 * @wordlen:	The number of bits in a word
+	 *
+	 * Returns: 0 on success, -ve on failure.
+	 */
+	int (*set_wordlen)(struct udevice *deiuce, unsigned int wordlen);
+
+	/**
+	 * SPI transfer
+	 *
+	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
+	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
+	 * works.
+	 *
+	 * The source of the outgoing bits is the "dout" parameter and the
+	 * destination of the input bits is the "din" parameter.  Note that
+	 * "dout" and "din" can point to the same memory location, in which
+	 * case the input data overwrites the output data (since both are
+	 * buffered by temporary variables, this is OK).
+	 *
+	 * spi_xfer() interface:
+	 * @device:	The SPI bus which will be sending/receiving the data.
+	 * @slave:	The slave device to communicate with
+	 * @bitlen:	How many bits to write and read.
+	 * @dout:	Pointer to a string of bits to send out.  The bits are
+	 *		held in a byte array and are sent MSB first.
+	 * @din:	Pointer to a string of bits that will be filled in.
+	 * @flags:	A bitwise combination of SPI_XFER_* flags.
+	 *
+	 * Returns: 0 on success, not -1 on failure
+	 */
+	int (*xfer)(struct udevice *device, struct udevice *slave,
+		    unsigned int bitlen, const void *dout, void *din,
+		    unsigned long flags);
+
+	/**
+	 * Set transfer speed.
+	 * This sets a new speed to be applied for next spi_xfer().
+	 * @slave:	The SPI slave
+	 * @hz:		The transfer speed
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*set_speed)(struct udevice *device, uint hz);
+
+	/**
+	 * Set the SPI mode/flags
+	 *
+	 * It is unclear if we want to set speed and mode together instead
+	 * of separately.
+	 *
+	 * @slave:	The SPI slave
+	 * @mode:	Requested SPI mode (SPI_... flags)
+	 * @return 0 if OK, -ve on error
+	 */
+	int (*set_mode)(struct udevice *device, uint mode);
+};
+
+int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
+			struct udevice **devp);
+
+int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
+			const char *drv_name, const char *dev_name,
+			struct udevice **devp, struct spi_slave **slavep);
+
+int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
+		    const char *dev_name, struct udevice **slavep);
+
+int spi_ofdata_to_platdata(const void *blob, int node,
+			   struct spi_slave *spi);
+
+struct sandbox_state;
+int sandbox_spi_get_emul(struct sandbox_state *state,
+			 struct udevice *bus, struct udevice *slave,
+			 struct udevice **emulp);
+
+/* Access the serial operations for a device */
+#define spi_get_ops(dev)	((struct dm_spi_ops *)(dev)->driver->ops)
+#endif /* CONFIG_DM_SPI */
+
 #endif	/* _SPI_H_ */
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 07/25] dm: sandbox: Add a SPI emulation uclass
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (5 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 08/25] dm: Remove spi_init() from board_r.c when using driver model Simon Glass
                   ` (18 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

U-Boot includes a SPI emulation driver already but it is not explicit, and
is hidden in the SPI flash code.

Conceptually with sandbox's SPI implementation we have a layer which
creates SPI bus transitions and a layer which interprets them, currently
only for SPI flash. The latter is actually an emulation, and it should be
possible to add more than one emulation - not just SPI flash.

Add a SPI emulation uclass so that other emulations can be plugged in to
support different types of emulated devices on difference buses/chip
selects.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/spi/Makefile          |  1 +
 drivers/spi/spi-emul-uclass.c | 15 +++++++++++++++
 include/dm/uclass-id.h        |  1 +
 include/spi.h                 | 30 ++++++++++++++++++++++++++++++
 4 files changed, 47 insertions(+)
 create mode 100644 drivers/spi/spi-emul-uclass.c

diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index d1f1dd0..a1de028 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -8,6 +8,7 @@
 # There are many options which enable SPI, so make this library available
 ifdef CONFIG_DM_SPI
 obj-y += spi-uclass.o
+obj-$(CONFIG_SANDBOX) += spi-emul-uclass.o
 else
 obj-y += spi.o
 endif
diff --git a/drivers/spi/spi-emul-uclass.c b/drivers/spi/spi-emul-uclass.c
new file mode 100644
index 0000000..b436a0e
--- /dev/null
+++ b/drivers/spi/spi-emul-uclass.c
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2014 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <spi.h>
+#include <spi_flash.h>
+
+UCLASS_DRIVER(spi_emul) = {
+	.id		= UCLASS_SPI_EMUL,
+	.name		= "spi_emul",
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 8207483..dce405e 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -18,6 +18,7 @@ enum uclass_id {
 	UCLASS_TEST,
 	UCLASS_TEST_FDT,
 	UCLASS_TEST_BUS,
+	UCLASS_SPI_EMUL,	/* sandbox SPI device emulator */
 
 	/* U-Boot uclasses start here */
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
diff --git a/include/spi.h b/include/spi.h
index b5e9347..d00a60b 100644
--- a/include/spi.h
+++ b/include/spi.h
@@ -423,6 +423,35 @@ struct dm_spi_ops {
 	int (*set_mode)(struct udevice *device, uint mode);
 };
 
+struct dm_spi_emul_ops {
+	/**
+	 * SPI transfer
+	 *
+	 * This writes "bitlen" bits out the SPI MOSI port and simultaneously
+	 * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
+	 * works. Here the device is a slave.
+	 *
+	 * The source of the outgoing bits is the "dout" parameter and the
+	 * destination of the input bits is the "din" parameter.  Note that
+	 * "dout" and "din" can point to the same memory location, in which
+	 * case the input data overwrites the output data (since both are
+	 * buffered by temporary variables, this is OK).
+	 *
+	 * spi_xfer() interface:
+	 * @slave:	The SPI slave which will be sending/receiving the data.
+	 * @bitlen:	How many bits to write and read.
+	 * @dout:	Pointer to a string of bits sent to the device. The
+	 *		bits are held in a byte array and are sent MSB first.
+	 * @din:	Pointer to a string of bits that will be sent back to
+	 *		the master.
+	 * @flags:	A bitwise combination of SPI_XFER_* flags.
+	 *
+	 * Returns: 0 on success, not -1 on failure
+	 */
+	int (*xfer)(struct udevice *device, unsigned int bitlen,
+		    const void *dout, void *din, unsigned long flags);
+};
+
 int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
 			struct udevice **devp);
 
@@ -443,6 +472,7 @@ int sandbox_spi_get_emul(struct sandbox_state *state,
 
 /* Access the serial operations for a device */
 #define spi_get_ops(dev)	((struct dm_spi_ops *)(dev)->driver->ops)
+#define spi_emul_get_ops(dev)	((struct dm_spi_emul_ops *)(dev)->driver->ops)
 #endif /* CONFIG_DM_SPI */
 
 #endif	/* _SPI_H_ */
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 08/25] dm: Remove spi_init() from board_r.c when using driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (6 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 07/25] dm: sandbox: Add a SPI emulation uclass Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 09/25] dm: Add spi.h header to a few files Simon Glass
                   ` (17 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Driver model does its own init, so we don't need this.

There is also a call in board_f.c but it is only enabled by CONFIG_HARD_SPI.
It is easy enough to disable that option when converting boards which use
it to driver model.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/board_r.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/common/board_r.c b/common/board_r.c
index 5c92664..e0d42eb 100644
--- a/common/board_r.c
+++ b/common/board_r.c
@@ -354,7 +354,7 @@ static int initr_flash(void)
 }
 #endif
 
-#ifdef CONFIG_PPC
+#if defined(CONFIG_PPC) && !defined(CONFIG_DM_SPI)
 static int initr_spi(void)
 {
 	/* PPC does this here */
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 09/25] dm: Add spi.h header to a few files
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (7 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 08/25] dm: Remove spi_init() from board_r.c when using driver model Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 10/25] dm: spi: Adjust cmd_spi to work with driver model Simon Glass
                   ` (16 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Some files are using SPI functions but not explitly including the SPI
header file. Fix this, since driver model needs it.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/env_sf.c             | 1 +
 drivers/mtd/spi/sf_params.c | 1 +
 2 files changed, 2 insertions(+)

diff --git a/common/env_sf.c b/common/env_sf.c
index 37ab13a..5e3729c 100644
--- a/common/env_sf.c
+++ b/common/env_sf.c
@@ -12,6 +12,7 @@
 #include <common.h>
 #include <environment.h>
 #include <malloc.h>
+#include <spi.h>
 #include <spi_flash.h>
 #include <search.h>
 #include <errno.h>
diff --git a/drivers/mtd/spi/sf_params.c b/drivers/mtd/spi/sf_params.c
index ac886fd..53efdc8 100644
--- a/drivers/mtd/spi/sf_params.c
+++ b/drivers/mtd/spi/sf_params.c
@@ -7,6 +7,7 @@
  */
 
 #include <common.h>
+#include <spi.h>
 #include <spi_flash.h>
 
 #include "sf_internal.h"
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 10/25] dm: spi: Adjust cmd_spi to work with driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (8 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 09/25] dm: Add spi.h header to a few files Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 11/25] dm: sandbox: spi: Move to " Simon Glass
                   ` (15 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Driver model uses a different way to find the SPI bus and slave from the
numbered devices given on the command line. Adjust the code to suit.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/cmd_spi.c | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/common/cmd_spi.c b/common/cmd_spi.c
index be5709c..4ebd41b 100644
--- a/common/cmd_spi.c
+++ b/common/cmd_spi.c
@@ -11,6 +11,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <dm.h>
 #include <errno.h>
 #include <spi.h>
 
@@ -42,19 +43,35 @@ static uchar 		din[MAX_SPI_BYTES];
 static int do_spi_xfer(int bus, int cs)
 {
 	struct spi_slave *slave;
-	int rcode = 0;
+	int ret = 0;
 
+#ifdef CONFIG_DM_SPI
+	struct udevice *dev;
+
+	ret = spi_get_bus_and_cs(bus, cs, 1000000, mode, NULL, 0, &dev,
+				  &slave);
+	if (ret)
+		return ret;
+#else
 	slave = spi_setup_slave(bus, cs, 1000000, mode);
 	if (!slave) {
 		printf("Invalid device %d:%d\n", bus, cs);
 		return -EINVAL;
 	}
+#endif
 
-	spi_claim_bus(slave);
-	if (spi_xfer(slave, bitlen, dout, din,
-		     SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
-		printf("Error during SPI transaction\n");
-		rcode = -EIO;
+	ret = spi_claim_bus(slave);
+	if (ret)
+		goto done;
+	ret = spi_xfer(slave, bitlen, dout, din,
+		       SPI_XFER_BEGIN | SPI_XFER_END);
+#ifndef CONFIG_DM_SPI
+	/* We don't get an error code in this case */
+	if (ret)
+		ret = -EIO;
+#endif
+	if (ret) {
+		printf("Error %d during SPI transaction\n", ret);
 	} else {
 		int j;
 
@@ -62,10 +79,13 @@ static int do_spi_xfer(int bus, int cs)
 			printf("%02X", din[j]);
 		printf("\n");
 	}
+done:
 	spi_release_bus(slave);
+#ifndef CONFIG_DM_SPI
 	spi_free_slave(slave);
+#endif
 
-	return rcode;
+	return ret;
 }
 
 /*
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 11/25] dm: sandbox: spi: Move to driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (9 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 10/25] dm: spi: Adjust cmd_spi to work with driver model Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers Simon Glass
                   ` (14 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Adjust the sandbox SPI driver to support driver model and move sandbox over
to driver model for SPI.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/sandbox/include/asm/state.h |   1 +
 drivers/spi/sandbox_spi.c        | 198 ++++++++++++++-------------------------
 include/configs/sandbox.h        |   3 +-
 3 files changed, 74 insertions(+), 128 deletions(-)

diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index d17a82e..4e0981a 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -43,6 +43,7 @@ enum state_terminal_raw {
 struct sandbox_spi_info {
 	const char *spec;
 	const struct sandbox_spi_emu_ops *ops;
+	struct udevice *emul;
 };
 
 /* The complete state of the test system */
diff --git a/drivers/spi/sandbox_spi.c b/drivers/spi/sandbox_spi.c
index 12e9bda..ba128a6 100644
--- a/drivers/spi/sandbox_spi.c
+++ b/drivers/spi/sandbox_spi.c
@@ -9,26 +9,23 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <malloc.h>
 #include <spi.h>
+#include <spi_flash.h>
 #include <os.h>
 
 #include <asm/errno.h>
 #include <asm/spi.h>
 #include <asm/state.h>
+#include <dm/device-internal.h>
+
+DECLARE_GLOBAL_DATA_PTR;
 
 #ifndef CONFIG_SPI_IDLE_VAL
 # define CONFIG_SPI_IDLE_VAL 0xFF
 #endif
 
-struct sandbox_spi_slave {
-	struct spi_slave slave;
-	const struct sandbox_spi_emu_ops *ops;
-	void *priv;
-};
-
-#define to_sandbox_spi_slave(s) container_of(s, struct sandbox_spi_slave, slave)
-
 const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
 				   unsigned long *cs)
 {
@@ -45,120 +42,52 @@ const char *sandbox_spi_parse_spec(const char *arg, unsigned long *bus,
 	return endp + 1;
 }
 
-int spi_cs_is_valid(unsigned int bus, unsigned int cs)
-{
-	return bus < CONFIG_SANDBOX_SPI_MAX_BUS &&
-		cs < CONFIG_SANDBOX_SPI_MAX_CS;
-}
-
-void spi_cs_activate(struct spi_slave *slave)
-{
-	struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
-
-	debug("sandbox_spi: activating CS\n");
-	if (sss->ops->cs_activate)
-		sss->ops->cs_activate(sss->priv);
-}
-
-void spi_cs_deactivate(struct spi_slave *slave)
-{
-	struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
-
-	debug("sandbox_spi: deactivating CS\n");
-	if (sss->ops->cs_deactivate)
-		sss->ops->cs_deactivate(sss->priv);
-}
-
-void spi_init(void)
-{
-}
-
-void spi_set_speed(struct spi_slave *slave, uint hz)
+__weak int sandbox_spi_get_emul(struct sandbox_state *state,
+				struct udevice *bus, struct udevice *slave,
+				struct udevice **emulp)
 {
+	return -ENOENT;
 }
 
-struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
-		unsigned int max_hz, unsigned int mode)
+static int sandbox_spi_xfer(struct udevice *dev, struct udevice *slave,
+			    unsigned int bitlen, const void *dout, void *din,
+			    unsigned long flags)
 {
-	struct sandbox_spi_slave *sss;
 	struct sandbox_state *state = state_get_current();
-	const char *spec;
-
-	if (!spi_cs_is_valid(bus, cs)) {
-		debug("sandbox_spi: Invalid SPI bus/cs\n");
-		return NULL;
-	}
-
-	sss = spi_alloc_slave(struct sandbox_spi_slave, bus, cs);
-	if (!sss) {
-		debug("sandbox_spi: Out of memory\n");
-		return NULL;
-	}
-
-	spec = state->spi[bus][cs].spec;
-	sss->ops = state->spi[bus][cs].ops;
-	if (!spec || !sss->ops || sss->ops->setup(&sss->priv, spec)) {
-		free(sss);
-		printf("sandbox_spi: unable to locate a slave client\n");
-		return NULL;
-	}
-
-	return &sss->slave;
-}
-
-void spi_free_slave(struct spi_slave *slave)
-{
-	struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
-
-	debug("sandbox_spi: releasing slave\n");
-
-	if (sss->ops->free)
-		sss->ops->free(sss->priv);
-
-	free(sss);
-}
-
-static int spi_bus_claim_cnt[CONFIG_SANDBOX_SPI_MAX_BUS];
-
-int spi_claim_bus(struct spi_slave *slave)
-{
-	if (spi_bus_claim_cnt[slave->bus]++) {
-		printf("sandbox_spi: error: bus already claimed: %d!\n",
-		       spi_bus_claim_cnt[slave->bus]);
-	}
-
-	return 0;
-}
-
-void spi_release_bus(struct spi_slave *slave)
-{
-	if (--spi_bus_claim_cnt[slave->bus]) {
-		printf("sandbox_spi: error: bus freed too often: %d!\n",
-		       spi_bus_claim_cnt[slave->bus]);
-	}
-}
-
-int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
-		void *din, unsigned long flags)
-{
-	struct sandbox_spi_slave *sss = to_sandbox_spi_slave(slave);
+	struct dm_spi_emul_ops *ops;
+	struct udevice *emul;
 	uint bytes = bitlen / 8, i;
-	int ret = 0;
+	int ret;
 	u8 *tx = (void *)dout, *rx = din;
+	uint busnum, cs;
 
 	if (bitlen == 0)
-		goto done;
+		return 0;
 
 	/* we can only do 8 bit transfers */
 	if (bitlen % 8) {
 		printf("sandbox_spi: xfer: invalid bitlen size %u; needs to be 8bit\n",
 		       bitlen);
-		flags |= SPI_XFER_END;
-		goto done;
+		return -EINVAL;
 	}
 
-	if (flags & SPI_XFER_BEGIN)
-		spi_cs_activate(slave);
+	busnum = dev->seq;
+	cs = slave->seq;
+	if (busnum >= CONFIG_SANDBOX_SPI_MAX_BUS ||
+	    cs >= CONFIG_SANDBOX_SPI_MAX_CS) {
+		printf("%s: busnum=%u, cs=%u: out of range\n", __func__,
+		       busnum, cs);
+		return -ENOENT;
+	}
+	ret = sandbox_spi_get_emul(state, dev, slave, &emul);
+	if (ret) {
+		printf("%s: busnum=%u, cs=%u: no emulation available (err=%d)\n",
+		       __func__, busnum, cs, ret);
+		return -ENOENT;
+	}
+	ret = device_probe(emul);
+	if (ret)
+		return ret;
 
 	/* make sure rx/tx buffers are full so clients can assume */
 	if (!tx) {
@@ -178,12 +107,8 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 		}
 	}
 
-	debug("sandbox_spi: xfer: bytes = %u\n tx:", bytes);
-	for (i = 0; i < bytes; ++i)
-		debug(" %u:%02x", i, tx[i]);
-	debug("\n");
-
-	ret = sss->ops->xfer(sss->priv, tx, rx, bytes);
+	ops = spi_emul_get_ops(emul);
+	ret = ops->xfer(emul, bitlen, dout, din, flags);
 
 	debug("sandbox_spi: xfer: got back %i (that's %s)\n rx:",
 	      ret, ret ? "bad" : "good");
@@ -196,22 +121,43 @@ int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
 	if (rx != din)
 		free(rx);
 
- done:
-	if (flags & SPI_XFER_END)
-		spi_cs_deactivate(slave);
-
 	return ret;
 }
 
-/**
- * Set up a new SPI slave for an fdt node
- *
- * @param blob		Device tree blob
- * @param node		SPI peripheral node to use
- * @return 0 if ok, -1 on error
- */
-struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
-				      int spi_node)
+static int sandbox_spi_set_speed(struct udevice *dev, uint speed)
 {
-	return NULL;
+	return 0;
 }
+
+static int sandbox_spi_set_mode(struct udevice *dev, uint mode)
+{
+	return 0;
+}
+
+int sandbox_spi_child_pre_probe(struct udevice *dev)
+{
+	struct spi_slave *slave = dev_get_parentdata(dev);
+
+	slave->dev = dev;
+	return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
+}
+
+static const struct dm_spi_ops sandbox_spi_ops = {
+	.xfer		= sandbox_spi_xfer,
+	.set_speed	= sandbox_spi_set_speed,
+	.set_mode	= sandbox_spi_set_mode,
+};
+
+static const struct udevice_id sandbox_spi_ids[] = {
+	{ .compatible = "sandbox,spi" },
+	{ }
+};
+
+U_BOOT_DRIVER(spi_sandbox) = {
+	.name	= "spi_sandbox",
+	.id	= UCLASS_SPI,
+	.of_match = sandbox_spi_ids,
+	.per_child_auto_alloc_size	= sizeof(struct spi_slave),
+	.child_pre_probe	= sandbox_spi_child_pre_probe,
+	.ops	= &sandbox_spi_ops,
+};
diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index 5e4e5fc..050f84d 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -100,8 +100,7 @@
 #define CONFIG_CMD_SF_TEST
 #define CONFIG_CMD_SPI
 #define CONFIG_SPI_FLASH
-#define CONFIG_OF_SPI
-#define CONFIG_OF_SPI_FLASH
+#define CONFIG_DM_SPI
 #define CONFIG_SPI_FLASH_ATMEL
 #define CONFIG_SPI_FLASH_EON
 #define CONFIG_SPI_FLASH_GIGADEVICE
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (10 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 11/25] dm: sandbox: spi: Move to " Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-28 11:32   ` Jagan Teki
  2014-07-15  0:56 ` [U-Boot] [PATCH 13/25] dm: exynos: Convert SPI to driver model Simon Glass
                   ` (13 subsequent siblings)
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

This README is intended to help maintainers move their SPI drivers over to
driver model. It works through the required steps with an example.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 doc/driver-model/spi-howto.txt | 570 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 570 insertions(+)
 create mode 100644 doc/driver-model/spi-howto.txt

diff --git a/doc/driver-model/spi-howto.txt b/doc/driver-model/spi-howto.txt
new file mode 100644
index 0000000..bb64735
--- /dev/null
+++ b/doc/driver-model/spi-howto.txt
@@ -0,0 +1,570 @@
+How to port a SPI driver to driver model
+========================================
+
+Here is a rough step-by-step guide. It is based around converting the
+exynos SPI driver to driver model (DM) and the example code is based
+around U-Boot v2014.04 (commit dda0dbf).
+
+It is quite long since it includes actual code examples.
+
+Before driver model, SPI drivers have their own private structure which
+contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
+exists, but now it is 'per-child data' for the SPI bus. Each child of the
+SPI bus is a SPI slave. The information that was stored in the
+driver-specific slave structure can now be put in private data for the
+SPI bus.
+
+For example, struct tegra_spi_slave looks like this:
+
+struct tegra_spi_slave {
+	struct spi_slave slave;
+	struct tegra_spi_ctrl *ctrl;
+};
+
+In this case 'slave' will be in per-child data, and 'ctrl' will be in the
+SPI bus' private data.
+
+
+0. How long does this take?
+
+Around 2.5 hours, including some allowance for figuring out the driver
+model bits.
+
+
+1. Enable driver mode for SPI and SPI flash
+
+Add these to your board config:
+
+#define CONFIG_DM_SPI
+#define CONFIG_DM_SPI_FLASH
+
+
+2. Add the skeleton
+
+Put this code at the bottom of your existing driver file:
+
+struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
+			unsigned int max_hz, unsigned int mode)
+{
+	return NULL;
+}
+
+struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
+				      int spi_node)
+{
+	return NULL;
+}
+
+static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
+{
+	return -ENODEV;
+}
+
+static int exynos_spi_probe(struct udevice *dev)
+{
+	return -ENODEV;
+}
+
+static int exynos_spi_remove(struct udevice *dev)
+{
+	return -ENODEV;
+}
+
+static int exynos_spi_claim_bus(struct udevice *dev)
+{
+
+	return -ENODEV;
+}
+
+static int exynos_spi_release_bus(struct udevice *dev)
+{
+
+	return -ENODEV;
+}
+
+static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
+			    const void *dout, void *din, unsigned long flags)
+{
+
+	return -ENODEV;
+}
+
+static int exynos_spi_set_speed(struct udevice *dev, uint speed)
+{
+	return -ENODEV;
+}
+
+static int exynos_spi_set_mode(struct udevice *dev, uint mode)
+{
+	return -ENODEV;
+}
+
+static const struct dm_spi_ops exynos_spi_ops = {
+	.claim_bus	= exynos_spi_claim_bus,
+	.release_bus	= exynos_spi_release_bus,
+	.xfer		= exynos_spi_xfer,
+	.set_speed	= exynos_spi_set_speed,
+	.set_mode	= exynos_spi_set_mode,
+};
+
+static const struct udevice_id exynos_spi_ids[] = {
+	{ .compatible = "samsung,exynos-spi" },
+	{ }
+};
+
+U_BOOT_DRIVER(exynos_spi) = {
+	.name	= "exynos_spi",
+	.id	= UCLASS_SPI,
+	.of_match = exynos_spi_ids,
+	.ops	= &exynos_spi_ops,
+	.ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
+	.probe	= exynos_spi_probe,
+	.remove	= exynos_spi_remove
+};
+
+
+3. Replace 'exynos' in the above code with your driver name
+
+
+4. #ifdef out all of the code in your driver except for the above
+
+This will allow you to get it building, which means you can work
+incrementally. Since all the methods return an error initially, there is
+less chance that you will accidentally leave something in.
+
+Also, even though your conversion is basically a rewrite, it might help
+reviewers if you leave functions in the same place in the file,
+particularly for large drivers.
+
+
+5. Add some includes
+
+Add these includes to your driver:
+
+#include <dm.h>
+#include <errno.h>
+
+
+6. Build
+
+At this point you should be able to build U-Boot for your board with the
+empty SPI driver. You still have empty methods in yur driver, but we will
+write these one by one.
+
+If you have spi_init() functions or the like that are called from your
+board then the build will fail. Remove these calls and make a note of the
+init that needs to be done.
+
+
+7. Set up your platform data structure
+
+This will hold the information your driver needs to operate, like its
+hardware address or maximum frequency.
+
+You may already have a struct like this, or you may need to create one
+from some of the #defines or global variables in the driver.
+
+Note that this information is not the run-time information. It should not
+include state that changes. It should be fixed throughout the life of
+U-Boot. Run-time information comes later.
+
+Here is what was in the exynos spi driver:
+
+struct spi_bus {
+	enum periph_id periph_id;
+	s32 frequency;		/* Default clock frequency, -1 for none */
+	struct exynos_spi *regs;
+	int inited;		/* 1 if this bus is ready for use */
+	int node;
+	uint deactivate_delay_us;	/* Delay to wait after deactivate */
+};
+
+Of these, inited is handled by DM and node is the device tree node, which
+DM tells you. The name is not quite right. So in this case we would use:
+
+struct exynos_spi_platdata {
+	enum periph_id periph_id;
+	s32 frequency;		/* Default clock frequency, -1 for none */
+	struct exynos_spi *regs;
+	uint deactivate_delay_us;	/* Delay to wait after deactivate */
+};
+
+
+8a. Write ofdata_to_platdata()   [for device tree only]
+
+This method will convert information in the device tree node into a C
+structure in your driver (called platform data). If you are not using
+device tree, go to 8b.
+
+DM will automatically allocate the struct for us when we are using device
+tree, but we need to tell it the size:
+
+U_BOOT_DRIVER(spi_exynos) = {
+...
+	.platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
+
+
+Here is a sample function. It gets a pointer to the platform data and
+fills in the fields from device tree.
+
+static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
+{
+	struct exynos_spi_platdata *plat = dev->platdata;
+	const void *blob = gd->fdt_blob;
+	int node = dev->of_offset;
+
+	plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
+	plat->periph_id = pinmux_decode_periph_id(blob, node);
+
+	if (plat->periph_id == PERIPH_ID_NONE) {
+		debug("%s: Invalid peripheral ID %d\n", __func__,
+			plat->periph_id);
+		return -FDT_ERR_NOTFOUND;
+	}
+
+	/* Use 500KHz as a suitable default */
+	plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
+					500000);
+	plat->deactivate_delay_us = fdtdec_get_int(blob, node,
+					"spi-deactivate-delay", 0);
+
+	return 0;
+}
+
+
+8b. Add the platform data  [non-device-tree only]
+
+Specify this data in a U_BOOT_DEVICE() declaration in your board file:
+
+struct exynos_spi_platdata platdata_spi0 = {
+	.periph_id = ...
+	.frequency = ...
+	.regs = ...
+	.deactivate_delay_us = ...
+};
+
+U_BOOT_DEVICE(board_spi0) = {
+	.name = "exynos_spi",
+	.platdata = &platdata_spi0,
+};
+
+You will unfortunately need to put the struct into a header file in this
+case so that your board file can use it.
+
+
+9. Add the device private data
+
+Most devices have some private data which they use to keep track of things
+while active. This is the run-time information and needs to be stored in
+a structure. There is probably a structure in the driver that includes a
+'struct spi_slave', so you can use that.
+
+struct exynos_spi_slave {
+	struct spi_slave slave;
+	struct exynos_spi *regs;
+	unsigned int freq;		/* Default frequency */
+	unsigned int mode;
+	enum periph_id periph_id;	/* Peripheral ID for this device */
+	unsigned int fifo_size;
+	int skip_preamble;
+	struct spi_bus *bus;		/* Pointer to our SPI bus info */
+	ulong last_transaction_us;	/* Time of last transaction end */
+};
+
+
+We should rename this to make its purpose more obvious, and get rid of
+the slave structure, so we have:
+
+struct exynos_spi_priv {
+	struct exynos_spi *regs;
+	unsigned int freq;		/* Default frequency */
+	unsigned int mode;
+	enum periph_id periph_id;	/* Peripheral ID for this device */
+	unsigned int fifo_size;
+	int skip_preamble;
+	ulong last_transaction_us;	/* Time of last transaction end */
+};
+
+
+DM can auto-allocate this also:
+
+U_BOOT_DRIVER(spi_exynos) = {
+...
+	.priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
+
+
+Note that this is created before the probe() method is called, and destroyed
+after the remove() method is called. It will be zeroed when the probe()
+method is called.
+
+
+10. Add the probe() and remove() methods
+
+Note: It's a good idea to build repeatedly as you are working, to avoid a
+huge amount of work getting things compiling at the end.
+
+The probe() method is supposed to set up the hardware. U-Boot used to use
+spi_setup_slave() to do this. So take a look at this function and see
+what you can copy out to set things up.
+
+
+static int exynos_spi_probe(struct udevice *dev)
+{
+	struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+
+	if (pdata->periph_id == PERIPH_ID_SPI1 ||
+	    pdata->periph_id == PERIPH_ID_SPI2)
+		priv->fifo_size = 64;
+	else
+		priv->fifo_size = 256;
+
+	priv->skip_preamble = 0;
+	priv->last_transaction_us = timer_get_us();
+	priv->freq = pdata->frequency;
+	priv->periph_id = pdata->periph_id;
+
+	return 0;
+}
+
+This implementation doesn't actually touch the hardware, which is somewhat
+unusual for a driver. In this case we will do that when the device is
+claimed by something that wants to use the SPI bus.
+
+For remove() we could shut down the clocks, but in this case there is
+nothing to do. DM frees any memory that it allocated, so we can just
+remove exynos_spi_remove() and its reference in U_BOOT_DRIVER.
+
+
+11. Implement set_speed()
+
+This should set up clocks so that the SPI bus is running at the right
+speed. With the old API spi_claim_bus() would normally do this and several
+of the following functions, so let's look at that function:
+
+int spi_claim_bus(struct spi_slave *slave)
+{
+	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
+	struct exynos_spi *regs = spi_slave->regs;
+	u32 reg = 0;
+	int ret;
+
+	ret = set_spi_clk(spi_slave->periph_id,
+					spi_slave->freq);
+	if (ret < 0) {
+		debug("%s: Failed to setup spi clock\n", __func__);
+		return ret;
+	}
+
+	exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
+
+	spi_flush_fifo(slave);
+
+	reg = readl(&regs->ch_cfg);
+	reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
+
+	if (spi_slave->mode & SPI_CPHA)
+		reg |= SPI_CH_CPHA_B;
+
+	if (spi_slave->mode & SPI_CPOL)
+		reg |= SPI_CH_CPOL_L;
+
+	writel(reg, &regs->ch_cfg);
+	writel(SPI_FB_DELAY_180, &regs->fb_clk);
+
+	return 0;
+}
+
+
+It sets up the speed, mode, pinmux, feedback delay and clears the FIFOs.
+With DM these will happen in separate methods.
+
+
+Here is an example for the speed part:
+
+static int exynos_spi_set_speed(struct udevice *dev, uint speed)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+	int ret;
+
+	ret = set_spi_clk(priv->periph_id, speed);
+	if (ret)
+		return ret;
+	priv->freq = speed;
+
+	return 0;
+}
+
+
+12. Implement set_mode()
+
+This should adjust the SPI mode (polarity, etc.). Again this code probably
+comes from the old spi_claim_bus(). Here is an example:
+
+
+static int exynos_spi_set_mode(struct udevice *dev, uint mode)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+	uint32_t reg;
+
+	reg = readl(&priv->regs->ch_cfg);
+	reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
+
+	if (mode & SPI_CPHA)
+		reg |= SPI_CH_CPHA_B;
+
+	if (mode & SPI_CPOL)
+		reg |= SPI_CH_CPOL_L;
+
+	writel(reg, &priv->regs->ch_cfg);
+
+	return 0;
+}
+
+
+13. Implement claim_bus()
+
+This is where a client wants to make use of the bus, so claims it first.
+At this point we need to make sure every is set up ready for data transfer.
+
+Here again we look at the old claim function and see some code that is
+needed. It is anything unrelated to speed and mode:
+
+static int exynos_spi_claim_bus(struct udevice *dev)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+
+	exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
+	spi_flush_fifo(priv->regs);
+
+	writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
+
+	return 0;
+}
+
+The spi_flush_fifo() function is in the removed part of the code, so we
+need to expose it again (perhaps with an #endif before it and '#if 0'
+after it). It only needs accesss to priv->regs which is why we have
+passed that in:
+
+/**
+ * Flush spi tx, rx fifos and reset the SPI controller
+ *
+ * @param regs	Pointer to SPI registers
+ */
+static void spi_flush_fifo(struct exynos_spi *regs)
+{
+	clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
+	clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
+	setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
+}
+
+
+14. Implement release_bus()
+
+This releases the bus - in our example the old code in spi_release_bus()
+is a call to spi_flush_fifo, so we add:
+
+static int exynos_spi_release_bus(struct udevice *dev)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+
+	spi_flush_fifo(priv->regs);
+
+	return 0;
+}
+
+
+15. Implement xfer()
+
+This is the final method that we need to create, and it is where all the
+work happens. The method parameters are the same as the old spi_xfer() with
+the addition of a 'struct udevice' so conversion is pretty easy. Start
+by copying the contents of spi_xfer() to your new xfer() mthod and proceed
+from there.
+
+If (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an
+activate function, something like this:
+
+void spi_cs_activate(struct spi_slave *slave)
+{
+	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
+
+	/* If it's too soon to do another transaction, wait */
+	if (spi_slave->bus->deactivate_delay_us &&
+	    spi_slave->last_transaction_us) {
+		ulong delay_us;		/* The delay completed so far */
+		delay_us = timer_get_us() - spi_slave->last_transaction_us;
+		if (delay_us < spi_slave->bus->deactivate_delay_us)
+			udelay(spi_slave->bus->deactivate_delay_us - delay_us);
+	}
+
+	clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
+	debug("Activate CS, bus %d\n", spi_slave->slave.bus);
+	spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
+}
+
+The new version looks like this:
+
+static void spi_cs_activate(struct udevice *dev)
+{
+	struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+
+	/* If it's too soon to do another transaction, wait */
+	if (pdata->deactivate_delay_us &&
+	    priv->last_transaction_us) {
+		ulong delay_us;		/* The delay completed so far */
+		delay_us = timer_get_us() - priv->last_transaction_us;
+		if (delay_us < pdata->deactivate_delay_us)
+			udelay(pdata->deactivate_delay_us - delay_us);
+	}
+
+	clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
+	debug("Activate CS, bus '%s'\n", dev->name);
+	priv->skip_preamble = priv->mode & SPI_PREAMBLE;
+}
+
+All we have really done here is change the pointers and print the device name
+instead of the bus number. Other local static functions can be treated in
+the same way.
+
+
+16. Set up the per-child data and child_pre_probe() method
+
+To minimise the pain and complexity of the SPI subsystem while the driver
+model change-over is in place, struct spi_slave is used to reference a
+SPI bus slave, even though that slave is actually a struct udevice. In fact
+struct spi_slave is the device's child data. We need to make sure this is
+set up. It is possible to allocate more space that struct spi_slave needs,
+but this is the minimum.
+
+U_BOOT_DRIVER(exynos_spi) = {
+...
+	.per_child_auto_alloc_size	= sizeof(struct spi_slave),
+	.child_pre_probe	= exynos_spi_child_pre_probe,
+}
+
+int exynos_spi_child_pre_probe(struct udevice *dev)
+{
+	struct spi_slave *slave = dev_get_parentdata(dev);
+
+	return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
+}
+
+
+Here our child_pre_probe() method merely calls a standard method to convert
+device tree data to that used in the slave.
+
+
+17. Test it
+
+Now that you have the code written and it compiles, try testing it using
+the 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your
+board.
+
+
+18. Prepare patches and send them to the mailing lists
+
+You can use 'tools/patman/patman' to prepare, check and send patches for
+your work. See the README for details.
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 13/25] dm: exynos: Convert SPI to driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (11 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 14/25] sf: Add an empty entry to the parameter list Simon Glass
                   ` (12 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Move the exynos SPI driver over to driver model. This removes quite a bit
of boilerplate from the driver, although it adds some for driver model.

A few device tree additions are needed to make the SPI flash available.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/arm/dts/exynos5250-snow.dts      |   8 +
 arch/arm/dts/exynos5420-peach-pit.dts |   1 +
 board/samsung/common/board.c          |   3 -
 drivers/spi/exynos_spi.c              | 509 ++++++++++++----------------------
 include/configs/exynos5-dt.h          |   1 +
 5 files changed, 186 insertions(+), 336 deletions(-)

diff --git a/arch/arm/dts/exynos5250-snow.dts b/arch/arm/dts/exynos5250-snow.dts
index 2003412..6fd9275 100644
--- a/arch/arm/dts/exynos5250-snow.dts
+++ b/arch/arm/dts/exynos5250-snow.dts
@@ -53,6 +53,14 @@
 		};
 	};
 
+	spi at 12d30000 {
+		spi-max-frequency = <50000000>;
+		firmware_storage_spi: flash at 0 {
+			compatible = "spi-flash";
+			reg = <0>;
+		};
+	};
+
 	spi at 131b0000 {
 		spi-max-frequency = <1000000>;
 		spi-deactivate-delay = <100>;
diff --git a/arch/arm/dts/exynos5420-peach-pit.dts b/arch/arm/dts/exynos5420-peach-pit.dts
index 6ff3f9a..48131d6 100644
--- a/arch/arm/dts/exynos5420-peach-pit.dts
+++ b/arch/arm/dts/exynos5420-peach-pit.dts
@@ -135,6 +135,7 @@
 	spi at 12d30000 { /* spi1 */
 		spi-max-frequency = <50000000>;
 		firmware_storage_spi: flash at 0 {
+			compatible = "spi-flash";
 			reg = <0>;
 
 			/*
diff --git a/board/samsung/common/board.c b/board/samsung/common/board.c
index 128a978..24a78e5 100644
--- a/board/samsung/common/board.c
+++ b/board/samsung/common/board.c
@@ -86,9 +86,6 @@ int board_init(void)
 	boot_temp_check();
 #endif
 
-#ifdef CONFIG_EXYNOS_SPI
-	spi_init();
-#endif
 	return exynos_init();
 }
 
diff --git a/drivers/spi/exynos_spi.c b/drivers/spi/exynos_spi.c
index 2969184..dedc066 100644
--- a/drivers/spi/exynos_spi.c
+++ b/drivers/spi/exynos_spi.c
@@ -6,6 +6,8 @@
  */
 
 #include <common.h>
+#include <dm.h>
+#include <errno.h>
 #include <malloc.h>
 #include <spi.h>
 #include <fdtdec.h>
@@ -19,176 +21,35 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-/* Information about each SPI controller */
-struct spi_bus {
+struct exynos_spi_platdata {
 	enum periph_id periph_id;
 	s32 frequency;		/* Default clock frequency, -1 for none */
 	struct exynos_spi *regs;
-	int inited;		/* 1 if this bus is ready for use */
-	int node;
 	uint deactivate_delay_us;	/* Delay to wait after deactivate */
 };
 
-/* A list of spi buses that we know about */
-static struct spi_bus spi_bus[EXYNOS5_SPI_NUM_CONTROLLERS];
-static unsigned int bus_count;
-
-struct exynos_spi_slave {
-	struct spi_slave slave;
+struct exynos_spi_priv {
 	struct exynos_spi *regs;
 	unsigned int freq;		/* Default frequency */
 	unsigned int mode;
 	enum periph_id periph_id;	/* Peripheral ID for this device */
 	unsigned int fifo_size;
 	int skip_preamble;
-	struct spi_bus *bus;		/* Pointer to our SPI bus info */
 	ulong last_transaction_us;	/* Time of last transaction end */
 };
 
-static struct spi_bus *spi_get_bus(unsigned dev_index)
-{
-	if (dev_index < bus_count)
-		return &spi_bus[dev_index];
-	debug("%s: invalid bus %d", __func__, dev_index);
-
-	return NULL;
-}
-
-static inline struct exynos_spi_slave *to_exynos_spi(struct spi_slave *slave)
-{
-	return container_of(slave, struct exynos_spi_slave, slave);
-}
-
-/**
- * Setup the driver private data
- *
- * @param bus		ID of the bus that the slave is attached to
- * @param cs		ID of the chip select connected to the slave
- * @param max_hz	Required spi frequency
- * @param mode		Required spi mode (clk polarity, clk phase and
- *			master or slave)
- * @return new device or NULL
- */
-struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
-			unsigned int max_hz, unsigned int mode)
-{
-	struct exynos_spi_slave *spi_slave;
-	struct spi_bus *bus;
-
-	if (!spi_cs_is_valid(busnum, cs)) {
-		debug("%s: Invalid bus/chip select %d, %d\n", __func__,
-		      busnum, cs);
-		return NULL;
-	}
-
-	spi_slave = spi_alloc_slave(struct exynos_spi_slave, busnum, cs);
-	if (!spi_slave) {
-		debug("%s: Could not allocate spi_slave\n", __func__);
-		return NULL;
-	}
-
-	bus = &spi_bus[busnum];
-	spi_slave->bus = bus;
-	spi_slave->regs = bus->regs;
-	spi_slave->mode = mode;
-	spi_slave->periph_id = bus->periph_id;
-	if (bus->periph_id == PERIPH_ID_SPI1 ||
-	    bus->periph_id == PERIPH_ID_SPI2)
-		spi_slave->fifo_size = 64;
-	else
-		spi_slave->fifo_size = 256;
-
-	spi_slave->skip_preamble = 0;
-	spi_slave->last_transaction_us = timer_get_us();
-
-	spi_slave->freq = bus->frequency;
-	if (max_hz)
-		spi_slave->freq = min(max_hz, spi_slave->freq);
-
-	return &spi_slave->slave;
-}
-
-/**
- * Free spi controller
- *
- * @param slave	Pointer to spi_slave to which controller has to
- *		communicate with
- */
-void spi_free_slave(struct spi_slave *slave)
-{
-	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
-
-	free(spi_slave);
-}
-
 /**
  * Flush spi tx, rx fifos and reset the SPI controller
  *
- * @param slave	Pointer to spi_slave to which controller has to
- *		communicate with
+ * @param regs	Pointer to SPI registers
  */
-static void spi_flush_fifo(struct spi_slave *slave)
+static void spi_flush_fifo(struct exynos_spi *regs)
 {
-	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
-	struct exynos_spi *regs = spi_slave->regs;
-
 	clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
 	clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
 	setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
 }
 
-/**
- * Initialize the spi base registers, set the required clock frequency and
- * initialize the gpios
- *
- * @param slave	Pointer to spi_slave to which controller has to
- *		communicate with
- * @return zero on success else a negative value
- */
-int spi_claim_bus(struct spi_slave *slave)
-{
-	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
-	struct exynos_spi *regs = spi_slave->regs;
-	u32 reg = 0;
-	int ret;
-
-	ret = set_spi_clk(spi_slave->periph_id,
-					spi_slave->freq);
-	if (ret < 0) {
-		debug("%s: Failed to setup spi clock\n", __func__);
-		return ret;
-	}
-
-	exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
-
-	spi_flush_fifo(slave);
-
-	reg = readl(&regs->ch_cfg);
-	reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
-
-	if (spi_slave->mode & SPI_CPHA)
-		reg |= SPI_CH_CPHA_B;
-
-	if (spi_slave->mode & SPI_CPOL)
-		reg |= SPI_CH_CPOL_L;
-
-	writel(reg, &regs->ch_cfg);
-	writel(SPI_FB_DELAY_180, &regs->fb_clk);
-
-	return 0;
-}
-
-/**
- * Reset the spi H/W and flush the tx and rx fifos
- *
- * @param slave	Pointer to spi_slave to which controller has to
- *		communicate with
- */
-void spi_release_bus(struct spi_slave *slave)
-{
-	spi_flush_fifo(slave);
-}
-
 static void spi_get_fifo_levels(struct exynos_spi *regs,
 	int *rx_lvl, int *tx_lvl)
 {
@@ -230,10 +91,10 @@ static void spi_request_bytes(struct exynos_spi *regs, int count, int step)
 	writel(count | SPI_PACKET_CNT_EN, &regs->pkt_cnt);
 }
 
-static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
+static int spi_rx_tx(struct exynos_spi_priv *priv, int todo,
 			void **dinp, void const **doutp, unsigned long flags)
 {
-	struct exynos_spi *regs = spi_slave->regs;
+	struct exynos_spi *regs = priv->regs;
 	uchar *rxp = *dinp;
 	const uchar *txp = *doutp;
 	int rx_lvl, tx_lvl;
@@ -245,8 +106,8 @@ static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 
 	out_bytes = in_bytes = todo;
 
-	stopping = spi_slave->skip_preamble && (flags & SPI_XFER_END) &&
-					!(spi_slave->mode & SPI_SLAVE);
+	stopping = priv->skip_preamble && (flags & SPI_XFER_END) &&
+					!(priv->mode & SPI_SLAVE);
 
 	/*
 	 * Try to transfer words if we can. This helps read performance at
@@ -254,7 +115,7 @@ static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 	 */
 	step = 1;
 	if (!((todo | (uintptr_t)rxp | (uintptr_t)txp) & 3) &&
-	    !spi_slave->skip_preamble)
+	    !priv->skip_preamble)
 		step = 4;
 
 	/*
@@ -279,7 +140,7 @@ static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 		 * Don't completely fill the txfifo, since we don't want our
 		 * rxfifo to overflow, and it may already contain data.
 		 */
-		while (tx_lvl < spi_slave->fifo_size/2 && out_bytes) {
+		while (tx_lvl < priv->fifo_size/2 && out_bytes) {
 			if (!txp)
 				temp = -1;
 			else if (step == 4)
@@ -295,9 +156,9 @@ static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 		if (rx_lvl >= step) {
 			while (rx_lvl >= step) {
 				temp = readl(&regs->rx_data);
-				if (spi_slave->skip_preamble) {
+				if (priv->skip_preamble) {
 					if (temp == SPI_PREAMBLE_END_BYTE) {
-						spi_slave->skip_preamble = 0;
+						priv->skip_preamble = 0;
 						stopping = 0;
 					}
 				} else {
@@ -326,7 +187,7 @@ static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 			txp = NULL;
 			spi_request_bytes(regs, toread, step);
 		}
-		if (spi_slave->skip_preamble && get_timer(start) > 100) {
+		if (priv->skip_preamble && get_timer(start) > 100) {
 			printf("SPI timeout: in_bytes=%d, out_bytes=%d, ",
 			       in_bytes, out_bytes);
 			return -1;
@@ -340,94 +201,28 @@ static int spi_rx_tx(struct exynos_spi_slave *spi_slave, int todo,
 }
 
 /**
- * Transfer and receive data
- *
- * @param slave		Pointer to spi_slave to which controller has to
- *			communicate with
- * @param bitlen	No of bits to tranfer or receive
- * @param dout		Pointer to transfer buffer
- * @param din		Pointer to receive buffer
- * @param flags		Flags for transfer begin and end
- * @return zero on success else a negative value
- */
-int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
-	     void *din, unsigned long flags)
-{
-	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
-	int upto, todo;
-	int bytelen;
-	int ret = 0;
-
-	/* spi core configured to do 8 bit transfers */
-	if (bitlen % 8) {
-		debug("Non byte aligned SPI transfer.\n");
-		return -1;
-	}
-
-	/* Start the transaction, if necessary. */
-	if ((flags & SPI_XFER_BEGIN))
-		spi_cs_activate(slave);
-
-	/*
-	 * Exynos SPI limits each transfer to 65535 transfers. To keep
-	 * things simple, allow a maximum of 65532 bytes. We could allow
-	 * more in word mode, but the performance difference is small.
-	 */
-	bytelen =  bitlen / 8;
-	for (upto = 0; !ret && upto < bytelen; upto += todo) {
-		todo = min(bytelen - upto, (1 << 16) - 4);
-		ret = spi_rx_tx(spi_slave, todo, &din, &dout, flags);
-		if (ret)
-			break;
-	}
-
-	/* Stop the transaction, if necessary. */
-	if ((flags & SPI_XFER_END) && !(spi_slave->mode & SPI_SLAVE)) {
-		spi_cs_deactivate(slave);
-		if (spi_slave->skip_preamble) {
-			assert(!spi_slave->skip_preamble);
-			debug("Failed to complete premable transaction\n");
-			ret = -1;
-		}
-	}
-
-	return ret;
-}
-
-/**
- * Validates the bus and chip select numbers
- *
- * @param bus	ID of the bus that the slave is attached to
- * @param cs	ID of the chip select connected to the slave
- * @return one on success else zero
- */
-int spi_cs_is_valid(unsigned int bus, unsigned int cs)
-{
-	return spi_get_bus(bus) && cs == 0;
-}
-
-/**
  * Activate the CS by driving it LOW
  *
  * @param slave	Pointer to spi_slave to which controller has to
  *		communicate with
  */
-void spi_cs_activate(struct spi_slave *slave)
+void spi_cs_activate(struct udevice *dev)
 {
-	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
+	struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
 
 	/* If it's too soon to do another transaction, wait */
-	if (spi_slave->bus->deactivate_delay_us &&
-	    spi_slave->last_transaction_us) {
+	if (pdata->deactivate_delay_us &&
+	    priv->last_transaction_us) {
 		ulong delay_us;		/* The delay completed so far */
-		delay_us = timer_get_us() - spi_slave->last_transaction_us;
-		if (delay_us < spi_slave->bus->deactivate_delay_us)
-			udelay(spi_slave->bus->deactivate_delay_us - delay_us);
+		delay_us = timer_get_us() - priv->last_transaction_us;
+		if (delay_us < pdata->deactivate_delay_us)
+			udelay(pdata->deactivate_delay_us - delay_us);
 	}
 
-	clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
-	debug("Activate CS, bus %d\n", spi_slave->slave.bus);
-	spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
+	clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
+	debug("Activate CS, bus '%s'\n", dev->name);
+	priv->skip_preamble = priv->mode & SPI_PREAMBLE;
 }
 
 /**
@@ -436,148 +231,196 @@ void spi_cs_activate(struct spi_slave *slave)
  * @param slave	Pointer to spi_slave to which controller has to
  *		communicate with
  */
-void spi_cs_deactivate(struct spi_slave *slave)
+void spi_cs_deactivate(struct udevice *dev)
 {
-	struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
+	struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
 
-	setbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
+	setbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
 
 	/* Remember time of this transaction so we can honour the bus delay */
-	if (spi_slave->bus->deactivate_delay_us)
-		spi_slave->last_transaction_us = timer_get_us();
+	if (pdata->deactivate_delay_us)
+		priv->last_transaction_us = timer_get_us();
 
-	debug("Deactivate CS, bus %d\n", spi_slave->slave.bus);
+	debug("Deactivate CS, bus '%s'\n", dev->name);
 }
 
-static inline struct exynos_spi *get_spi_base(int dev_index)
+static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
 {
-	if (dev_index < 3)
-		return (struct exynos_spi *)samsung_get_base_spi() + dev_index;
-	else
-		return (struct exynos_spi *)samsung_get_base_spi_isp() +
-					(dev_index - 3);
-}
+	struct exynos_spi_platdata *plat = dev->platdata;
+	const void *blob = gd->fdt_blob;
+	int node = dev->of_offset;
 
-/*
- * Read the SPI config from the device tree node.
- *
- * @param blob  FDT blob to read from
- * @param node  Node offset to read from
- * @param bus   SPI bus structure to fill with information
- * @return 0 if ok, or -FDT_ERR_NOTFOUND if something was missing
- */
-#ifdef CONFIG_OF_CONTROL
-static int spi_get_config(const void *blob, int node, struct spi_bus *bus)
-{
-	bus->node = node;
-	bus->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
-	bus->periph_id = pinmux_decode_periph_id(blob, node);
+	plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
+	plat->periph_id = pinmux_decode_periph_id(blob, node);
 
-	if (bus->periph_id == PERIPH_ID_NONE) {
+	if (plat->periph_id == PERIPH_ID_NONE) {
 		debug("%s: Invalid peripheral ID %d\n", __func__,
-			bus->periph_id);
+			plat->periph_id);
 		return -FDT_ERR_NOTFOUND;
 	}
 
 	/* Use 500KHz as a suitable default */
-	bus->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
+	plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
 					500000);
-	bus->deactivate_delay_us = fdtdec_get_int(blob, node,
+	plat->deactivate_delay_us = fdtdec_get_int(blob, node,
 					"spi-deactivate-delay", 0);
 
 	return 0;
 }
 
-/*
- * Process a list of nodes, adding them to our list of SPI ports.
- *
- * @param blob          fdt blob
- * @param node_list     list of nodes to process (any <=0 are ignored)
- * @param count         number of nodes to process
- * @param is_dvc        1 if these are DVC ports, 0 if standard I2C
- * @return 0 if ok, -1 on error
- */
-static int process_nodes(const void *blob, int node_list[], int count)
+static int exynos_spi_probe(struct udevice *dev)
 {
-	int i;
+	struct exynos_spi_platdata *plat = dev_get_platdata(dev);
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
 
-	/* build the i2c_controllers[] for each controller */
-	for (i = 0; i < count; i++) {
-		int node = node_list[i];
-		struct spi_bus *bus;
+	priv->regs = plat->regs;
+	if (plat->periph_id == PERIPH_ID_SPI1 ||
+	    plat->periph_id == PERIPH_ID_SPI2)
+		priv->fifo_size = 64;
+	else
+		priv->fifo_size = 256;
 
-		if (node <= 0)
-			continue;
+	priv->skip_preamble = 0;
+	priv->last_transaction_us = timer_get_us();
+	priv->freq = plat->frequency;
+	priv->periph_id = plat->periph_id;
 
-		bus = &spi_bus[i];
-		if (spi_get_config(blob, node, bus)) {
-			printf("exynos spi_init: failed to decode bus %d\n",
-				i);
-			return -1;
-		}
+	return 0;
+}
 
-		debug("spi: controller bus %d at %p, periph_id %d\n",
-		      i, bus->regs, bus->periph_id);
-		bus->inited = 1;
-		bus_count++;
-	}
+static int exynos_spi_claim_bus(struct udevice *dev)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+
+	exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
+	spi_flush_fifo(priv->regs);
+
+	writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
 
 	return 0;
 }
-#endif
 
-/**
- * Set up a new SPI slave for an fdt node
- *
- * @param blob		Device tree blob
- * @param node		SPI peripheral node to use
- * @return 0 if ok, -1 on error
- */
-struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
-				      int spi_node)
+static int exynos_spi_release_bus(struct udevice *dev)
 {
-	struct spi_bus *bus;
-	unsigned int i;
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
 
-	for (i = 0, bus = spi_bus; i < bus_count; i++, bus++) {
-		if (bus->node == spi_node)
-			return spi_base_setup_slave_fdt(blob, i, slave_node);
+	spi_flush_fifo(priv->regs);
+
+	return 0;
+}
+
+static int exynos_spi_xfer(struct udevice *dev, struct udevice *slave,
+			   unsigned int bitlen, const void *dout, void *din,
+			   unsigned long flags)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+	int upto, todo;
+	int bytelen;
+	int ret = 0;
+
+	/* spi core configured to do 8 bit transfers */
+	if (bitlen % 8) {
+		debug("Non byte aligned SPI transfer.\n");
+		return -1;
+	}
+
+	/* Start the transaction, if necessary. */
+	if ((flags & SPI_XFER_BEGIN))
+		spi_cs_activate(dev);
+
+	/*
+	 * Exynos SPI limits each transfer to 65535 transfers. To keep
+	 * things simple, allow a maximum of 65532 bytes. We could allow
+	 * more in word mode, but the performance difference is small.
+	 */
+	bytelen = bitlen / 8;
+	for (upto = 0; !ret && upto < bytelen; upto += todo) {
+		todo = min(bytelen - upto, (1 << 16) - 4);
+		ret = spi_rx_tx(priv, todo, &din, &dout, flags);
+		if (ret)
+			break;
+	}
+
+	/* Stop the transaction, if necessary. */
+	if ((flags & SPI_XFER_END) && !(priv->mode & SPI_SLAVE)) {
+		spi_cs_deactivate(dev);
+		if (priv->skip_preamble) {
+			assert(!priv->skip_preamble);
+			debug("Failed to complete premable transaction\n");
+			ret = -1;
+		}
 	}
 
-	debug("%s: Failed to find bus node %d\n", __func__, spi_node);
-	return NULL;
+	return ret;
 }
 
-/* Sadly there is no error return from this function */
-void spi_init(void)
+static int exynos_spi_set_speed(struct udevice *dev, uint speed)
 {
-	int count;
+	struct exynos_spi_platdata *plat = dev->platdata;
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+	int ret;
 
-#ifdef CONFIG_OF_CONTROL
-	int node_list[EXYNOS5_SPI_NUM_CONTROLLERS];
-	const void *blob = gd->fdt_blob;
+	if (speed > plat->frequency)
+		speed = plat->frequency;
+	ret = set_spi_clk(priv->periph_id, speed);
+	if (ret)
+		return ret;
+	priv->freq = speed;
 
-	count = fdtdec_find_aliases_for_id(blob, "spi",
-			COMPAT_SAMSUNG_EXYNOS_SPI, node_list,
-			EXYNOS5_SPI_NUM_CONTROLLERS);
-	if (process_nodes(blob, node_list, count))
-		return;
+	return 0;
+}
 
-#else
-	struct spi_bus *bus;
+static int exynos_spi_set_mode(struct udevice *dev, uint mode)
+{
+	struct exynos_spi_priv *priv = dev_get_priv(dev);
+	uint32_t reg;
 
-	for (count = 0; count < EXYNOS5_SPI_NUM_CONTROLLERS; count++) {
-		bus = &spi_bus[count];
-		bus->regs = get_spi_base(count);
-		bus->periph_id = PERIPH_ID_SPI0 + count;
+	reg = readl(&priv->regs->ch_cfg);
+	reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
 
-		/* Although Exynos5 supports upto 50Mhz speed,
-		 * we are setting it to 10Mhz for safe side
-		 */
-		bus->frequency = 10000000;
-		bus->inited = 1;
-		bus->node = 0;
-		bus_count = EXYNOS5_SPI_NUM_CONTROLLERS;
-	}
-#endif
+	if (mode & SPI_CPHA)
+		reg |= SPI_CH_CPHA_B;
+
+	if (mode & SPI_CPOL)
+		reg |= SPI_CH_CPOL_L;
+
+	writel(reg, &priv->regs->ch_cfg);
+	priv->mode = mode;
+
+	return 0;
+}
+
+int exynos_spi_child_pre_probe(struct udevice *dev)
+{
+	struct spi_slave *slave = dev_get_parentdata(dev);
+
+	slave->dev = dev;
+	return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
 }
+
+static const struct dm_spi_ops exynos_spi_ops = {
+	.claim_bus	= exynos_spi_claim_bus,
+	.release_bus	= exynos_spi_release_bus,
+	.xfer		= exynos_spi_xfer,
+	.set_speed	= exynos_spi_set_speed,
+	.set_mode	= exynos_spi_set_mode,
+};
+
+static const struct udevice_id exynos_spi_ids[] = {
+	{ .compatible = "samsung,exynos-spi" },
+	{ }
+};
+
+U_BOOT_DRIVER(exynos_spi) = {
+	.name	= "exynos_spi",
+	.id	= UCLASS_SPI,
+	.of_match = exynos_spi_ids,
+	.ops	= &exynos_spi_ops,
+	.ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
+	.platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
+	.priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
+	.per_child_auto_alloc_size	= sizeof(struct spi_slave),
+	.probe	= exynos_spi_probe,
+	.child_pre_probe	= exynos_spi_child_pre_probe,
+};
diff --git a/include/configs/exynos5-dt.h b/include/configs/exynos5-dt.h
index 12b086b..873dd63 100644
--- a/include/configs/exynos5-dt.h
+++ b/include/configs/exynos5-dt.h
@@ -20,6 +20,7 @@
 #define CONFIG_CMD_DM
 #define CONFIG_DM_GPIO
 #define CONFIG_DM_SERIAL
+#define CONFIG_DM_SPI
 
 #define CONFIG_SYS_GENERIC_BOARD
 #define CONFIG_ARCH_CPU_INIT
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 14/25] sf: Add an empty entry to the parameter list
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (12 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 13/25] dm: exynos: Convert SPI to driver model Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 15/25] sf: Tidy up public and private header files Simon Glass
                   ` (11 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

The list is supposed to be terminated with a NULL name, but is not. If a
board probes a chip which does not appear in the table, U-Boot will crash
(at least on sandbox).

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/mtd/spi/sf_params.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/mtd/spi/sf_params.c b/drivers/mtd/spi/sf_params.c
index 53efdc8..b3a3c7b 100644
--- a/drivers/mtd/spi/sf_params.c
+++ b/drivers/mtd/spi/sf_params.c
@@ -117,6 +117,7 @@ const struct spi_flash_params spi_flash_params_table[] = {
 	{"W25Q64DW",	   0xef6017, 0x0,	64 * 1024,   128, RD_FULL,	    WR_QPP | SECT_4K},
 	{"W25Q128FW",	   0xef6018, 0x0,	64 * 1024,   256, RD_FULL,	    WR_QPP | SECT_4K},
 #endif
+	{},	/* Empty entry to terminate the list */
 	/*
 	 * Note:
 	 * Below paired flash devices has similar spi_flash params.
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 15/25] sf: Tidy up public and private header files
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (13 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 14/25] sf: Add an empty entry to the parameter list Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 16/25] spi: Use error return value in sf_ops Simon Glass
                   ` (10 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Since spi_flash.h is supposed to be the public API for SPI flash, move
private things to sf_internal.h. Also tidy up a few comment nits.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 board/buffalo/lsxl/lsxl.c      |  3 +-
 common/cmd_sf.c                |  1 +
 drivers/mtd/spi/ramtron.c      |  1 +
 drivers/mtd/spi/sf_internal.h  | 67 +++++++++++++++++++++++++++++++++++++-----
 drivers/mtd/spi/spi_spl_load.c |  1 +
 include/spi_flash.h            | 57 ++++-------------------------------
 6 files changed, 70 insertions(+), 60 deletions(-)

diff --git a/board/buffalo/lsxl/lsxl.c b/board/buffalo/lsxl/lsxl.c
index 659a124..c1cb07b 100644
--- a/board/buffalo/lsxl/lsxl.c
+++ b/board/buffalo/lsxl/lsxl.c
@@ -13,11 +13,12 @@
 #include <malloc.h>
 #include <netdev.h>
 #include <miiphy.h>
+#include <spi.h>
+#include <spi_flash.h>
 #include <asm/arch/kirkwood.h>
 #include <asm/arch/cpu.h>
 #include <asm/arch/mpp.h>
 #include <asm/arch/gpio.h>
-#include <spi_flash.h>
 
 #include "lsxl.h"
 
diff --git a/common/cmd_sf.c b/common/cmd_sf.c
index b4ceb71..1bb41d3 100644
--- a/common/cmd_sf.c
+++ b/common/cmd_sf.c
@@ -9,6 +9,7 @@
 #include <common.h>
 #include <div64.h>
 #include <malloc.h>
+#include <spi.h>
 #include <spi_flash.h>
 
 #include <asm/io.h>
diff --git a/drivers/mtd/spi/ramtron.c b/drivers/mtd/spi/ramtron.c
index d50da37..a23032c 100644
--- a/drivers/mtd/spi/ramtron.c
+++ b/drivers/mtd/spi/ramtron.c
@@ -35,6 +35,7 @@
 
 #include <common.h>
 #include <malloc.h>
+#include <spi.h>
 #include <spi_flash.h>
 #include "sf_internal.h"
 
diff --git a/drivers/mtd/spi/sf_internal.h b/drivers/mtd/spi/sf_internal.h
index 6bcd522..dc3b975 100644
--- a/drivers/mtd/spi/sf_internal.h
+++ b/drivers/mtd/spi/sf_internal.h
@@ -10,6 +10,36 @@
 #ifndef _SF_INTERNAL_H_
 #define _SF_INTERNAL_H_
 
+#include <linux/types.h>
+#include <linux/compiler.h>
+
+/* Dual SPI flash memories - see SPI_COMM_DUAL_... */
+enum spi_dual_flash {
+	SF_SINGLE_FLASH	= 0,
+	SF_DUAL_STACKED_FLASH	= 1 << 0,
+	SF_DUAL_PARALLEL_FLASH	= 1 << 1,
+};
+
+/* Enum list - Full read commands */
+enum spi_read_cmds {
+	ARRAY_SLOW		= 1 << 0,
+	DUAL_OUTPUT_FAST	= 1 << 1,
+	DUAL_IO_FAST		= 1 << 2,
+	QUAD_OUTPUT_FAST	= 1 << 3,
+	QUAD_IO_FAST		= 1 << 4,
+};
+
+#define RD_EXTN	(ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST)
+#define RD_FULL	(RD_EXTN | QUAD_OUTPUT_FAST | QUAD_IO_FAST)
+
+/* sf param flags */
+enum {
+	SECT_4K		= 1 << 0,
+	SECT_32K	= 1 << 1,
+	E_FSR		= 1 << 2,
+	WR_QPP		= 1 << 3,
+};
+
 #define SPI_FLASH_3B_ADDR_LEN		3
 #define SPI_FLASH_CMD_LEN		(1 + SPI_FLASH_3B_ADDR_LEN)
 #define SPI_FLASH_16MB_BOUN		0x1000000
@@ -30,12 +60,12 @@
 #define CMD_WRITE_STATUS		0x01
 #define CMD_PAGE_PROGRAM		0x02
 #define CMD_WRITE_DISABLE		0x04
-#define CMD_READ_STATUS			0x05
+#define CMD_READ_STATUS		0x05
 #define CMD_QUAD_PAGE_PROGRAM		0x32
 #define CMD_READ_STATUS1		0x35
 #define CMD_WRITE_ENABLE		0x06
-#define CMD_READ_CONFIG			0x35
-#define CMD_FLAG_STATUS			0x70
+#define CMD_READ_CONFIG		0x35
+#define CMD_FLAG_STATUS		0x70
 
 /* Read commands */
 #define CMD_READ_ARRAY_SLOW		0x03
@@ -57,24 +87,47 @@
 /* Common status */
 #define STATUS_WIP			(1 << 0)
 #define STATUS_QEB_WINSPAN		(1 << 1)
-#define STATUS_QEB_MXIC			(1 << 6)
+#define STATUS_QEB_MXIC		(1 << 6)
 #define STATUS_PEC			(1 << 7)
 
 /* Flash timeout values */
 #define SPI_FLASH_PROG_TIMEOUT		(2 * CONFIG_SYS_HZ)
-#define SPI_FLASH_PAGE_ERASE_TIMEOUT	(5 * CONFIG_SYS_HZ)
+#define SPI_FLASH_PAGE_ERASE_TIMEOUT		(5 * CONFIG_SYS_HZ)
 #define SPI_FLASH_SECTOR_ERASE_TIMEOUT	(10 * CONFIG_SYS_HZ)
 
 /* SST specific */
 #ifdef CONFIG_SPI_FLASH_SST
-# define SST_WP			0x01	/* Supports AAI word program */
+# define SST_WP		0x01	/* Supports AAI word program */
 # define CMD_SST_BP		0x02    /* Byte Program */
-# define CMD_SST_AAI_WP		0xAD	/* Auto Address Incr Word Program */
+# define CMD_SST_AAI_WP	0xAD	/* Auto Address Incr Word Program */
 
 int sst_write_wp(struct spi_flash *flash, u32 offset, size_t len,
 		const void *buf);
 #endif
 
+/**
+ * struct spi_flash_params - SPI/QSPI flash device params structure
+ *
+ * @name:		Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO])
+ * @jedec:		Device jedec ID (0x[1byte_manuf_id][2byte_dev_id])
+ * @ext_jedec:		Device ext_jedec ID
+ * @sector_size:	Sector size of this device
+ * @nr_sectors:	No.of sectors on this device
+ * @e_rd_cmd:		Enum list for read commands
+ * @flags:		Important param, for flash specific behaviour
+ */
+struct spi_flash_params {
+	const char *name;
+	u32 jedec;
+	u16 ext_jedec;
+	u32 sector_size;
+	u32 nr_sectors;
+	u8 e_rd_cmd;
+	u16 flags;
+};
+
+extern const struct spi_flash_params spi_flash_params_table[];
+
 /* Send a single-byte command to the device and read the response */
 int spi_flash_cmd(struct spi_slave *spi, u8 cmd, void *response, size_t len);
 
diff --git a/drivers/mtd/spi/spi_spl_load.c b/drivers/mtd/spi/spi_spl_load.c
index 1954b7e..c47d928 100644
--- a/drivers/mtd/spi/spi_spl_load.c
+++ b/drivers/mtd/spi/spi_spl_load.c
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <spi.h>
 #include <spi_flash.h>
 #include <spl.h>
 
diff --git a/include/spi_flash.h b/include/spi_flash.h
index 2db53c7..094a512 100644
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -15,68 +15,21 @@
 #ifndef _SPI_FLASH_H_
 #define _SPI_FLASH_H_
 
-#include <spi.h>
 #include <linux/types.h>
-#include <linux/compiler.h>
 
-/* sf param flags */
-#define SECT_4K		1 << 1
-#define SECT_32K	1 << 2
-#define E_FSR		1 << 3
-#define WR_QPP		1 << 4
-
-/* Enum list - Full read commands */
-enum spi_read_cmds {
-	ARRAY_SLOW = 1 << 0,
-	DUAL_OUTPUT_FAST = 1 << 1,
-	DUAL_IO_FAST = 1 << 2,
-	QUAD_OUTPUT_FAST = 1 << 3,
-	QUAD_IO_FAST = 1 << 4,
-};
-#define RD_EXTN		ARRAY_SLOW | DUAL_OUTPUT_FAST | DUAL_IO_FAST
-#define RD_FULL		RD_EXTN | QUAD_OUTPUT_FAST | QUAD_IO_FAST
-
-/* Dual SPI flash memories */
-enum spi_dual_flash {
-	SF_SINGLE_FLASH = 0,
-	SF_DUAL_STACKED_FLASH = 1 << 0,
-	SF_DUAL_PARALLEL_FLASH = 1 << 1,
-};
-
-/**
- * struct spi_flash_params - SPI/QSPI flash device params structure
- *
- * @name:		Device name ([MANUFLETTER][DEVTYPE][DENSITY][EXTRAINFO])
- * @jedec:		Device jedec ID (0x[1byte_manuf_id][2byte_dev_id])
- * @ext_jedec:		Device ext_jedec ID
- * @sector_size:	Sector size of this device
- * @nr_sectors:		No.of sectors on this device
- * @e_rd_cmd:		Enum list for read commands
- * @flags:		Important param, for flash specific behaviour
- */
-struct spi_flash_params {
-	const char *name;
-	u32 jedec;
-	u16 ext_jedec;
-	u32 sector_size;
-	u32 nr_sectors;
-	u8 e_rd_cmd;
-	u16 flags;
-};
-
-extern const struct spi_flash_params spi_flash_params_table[];
+struct spi_slave;
 
 /**
  * struct spi_flash - SPI flash structure
  *
  * @spi:		SPI slave
  * @name:		Name of SPI flash
- * @dual_flash:		Indicates dual flash memories - dual stacked, parallel
+ * @dual_flash:	Indicates dual flash memories - dual stacked, parallel
  * @shift:		Flash shift useful in dual parallel
  * @size:		Total flash size
  * @page_size:		Write (page) size
  * @sector_size:	Sector size
- * @erase_size:		Erase size
+ * @erase_size:	Erase size
  * @bank_read_cmd:	Bank read cmd
  * @bank_write_cmd:	Bank write cmd
  * @bank_curr:		Current flash bank
@@ -84,8 +37,8 @@ extern const struct spi_flash_params spi_flash_params_table[];
  * @erase_cmd:		Erase cmd 4K, 32K, 64K
  * @read_cmd:		Read cmd - Array Fast, Extn read and quad read.
  * @write_cmd:		Write cmd - page and quad program.
- * @dummy_byte:		Dummy cycles for read operation.
- * @memory_map:		Address of read-only SPI flash access
+ * @dummy_byte:	Dummy cycles for read operation.
+ * @memory_map:	Address of read-only SPI flash access
  * @read:		Flash read ops: Read len bytes@offset into buf
  *			Supported cmds: Fast Array Read
  * @write:		Flash write ops: Write len bytes from buf into offset
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 16/25] spi: Use error return value in sf_ops
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (14 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 15/25] sf: Tidy up public and private header files Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 17/25] dm: sf: Add a uclass for SPI flash Simon Glass
                   ` (9 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Adjust spi_flash_probe_slave() to return an error value instead of a
pointer so we get the correct error return.

Have the caller allocate memory for spi_flash to simplify error handling,
and also so that driver model can use its existing allocated memory.

Add a spi.h include in the sf_params file.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/mtd/spi/sf_probe.c | 90 +++++++++++++++++++++++++++++-----------------
 1 file changed, 57 insertions(+), 33 deletions(-)

diff --git a/drivers/mtd/spi/sf_probe.c b/drivers/mtd/spi/sf_probe.c
index 36ae5e0..5ed828f 100644
--- a/drivers/mtd/spi/sf_probe.c
+++ b/drivers/mtd/spi/sf_probe.c
@@ -9,6 +9,7 @@
  */
 
 #include <common.h>
+#include <errno.h>
 #include <fdtdec.h>
 #include <malloc.h>
 #include <spi.h>
@@ -95,15 +96,15 @@ static int spi_flash_set_qeb(struct spi_flash *flash, u8 idcode0)
 	}
 }
 
-static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
-		u8 *idcode)
+static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
+				     struct spi_flash *flash)
 {
 	const struct spi_flash_params *params;
-	struct spi_flash *flash;
 	u8 cmd;
 	u16 jedec = idcode[1] << 8 | idcode[2];
 	u16 ext_jedec = idcode[3] << 8 | idcode[4];
 
+	/* Validate params from spi_flash_params table */
 	params = spi_flash_params_table;
 	for (; params->name != NULL; params++) {
 		if ((params->jedec >> 16) == idcode[0]) {
@@ -120,13 +121,7 @@ static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
 		printf("SF: Unsupported flash IDs: ");
 		printf("manuf %02x, jedec %04x, ext_jedec %04x\n",
 		       idcode[0], jedec, ext_jedec);
-		return NULL;
-	}
-
-	flash = calloc(1, sizeof(*flash));
-	if (!flash) {
-		debug("SF: Failed to allocate spi_flash\n");
-		return NULL;
+		return -EPROTONOSUPPORT;
 	}
 
 	/* Assign spi data */
@@ -227,15 +222,18 @@ static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
 #ifdef CONFIG_SPI_FLASH_BAR
 	u8 curr_bank = 0;
 	if (flash->size > SPI_FLASH_16MB_BOUN) {
+		int ret;
+
 		flash->bank_read_cmd = (idcode[0] == 0x01) ?
 					CMD_BANKADDR_BRRD : CMD_EXTNADDR_RDEAR;
 		flash->bank_write_cmd = (idcode[0] == 0x01) ?
 					CMD_BANKADDR_BRWR : CMD_EXTNADDR_WREAR;
 
-		if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
-					  &curr_bank, 1)) {
+		ret = spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
+					    &curr_bank, 1);
+		if (ret) {
 			debug("SF: fail to read bank addr register\n");
-			return NULL;
+			return ret;
 		}
 		flash->bank_curr = curr_bank;
 	} else {
@@ -250,7 +248,7 @@ static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
 		spi_flash_cmd_write_status(flash, 0);
 #endif
 
-	return flash;
+	return 0;
 }
 
 #ifdef CONFIG_OF_CONTROL
@@ -281,23 +279,29 @@ int spi_flash_decode_fdt(const void *blob, struct spi_flash *flash)
 }
 #endif /* CONFIG_OF_CONTROL */
 
-static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
+/**
+ * spi_flash_probe_slave() - Probe for a SPI flash device on a bus
+ *
+ * @spi: Bus to probe
+ * @flashp: Pointer to place to put flash info, which may be NULL if the
+ * space should be allocated
+ */
+int spi_flash_probe_slave(struct spi_slave *spi, struct spi_flash *flash)
 {
-	struct spi_flash *flash = NULL;
 	u8 idcode[5];
 	int ret;
 
 	/* Setup spi_slave */
 	if (!spi) {
 		printf("SF: Failed to set up slave\n");
-		return NULL;
+		return -ENODEV;
 	}
 
 	/* Claim spi bus */
 	ret = spi_claim_bus(spi);
 	if (ret) {
 		debug("SF: Failed to claim SPI bus: %d\n", ret);
-		goto err_claim_bus;
+		return ret;
 	}
 
 	/* Read the ID codes */
@@ -312,10 +316,10 @@ static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
 	print_buffer(0, idcode, 1, sizeof(idcode), 0);
 #endif
 
-	/* Validate params from spi_flash_params table */
-	flash = spi_flash_validate_params(spi, idcode);
-	if (!flash)
+	if (spi_flash_validate_params(spi, idcode, flash)) {
+		ret = -EINVAL;
 		goto err_read_id;
+	}
 
 	/* Set the quad enable bit - only for quad commands */
 	if ((flash->read_cmd == CMD_READ_QUAD_OUTPUT_FAST) ||
@@ -323,13 +327,15 @@ static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
 	    (flash->write_cmd == CMD_QUAD_PAGE_PROGRAM)) {
 		if (spi_flash_set_qeb(flash, idcode[0])) {
 			debug("SF: Fail to set QEB for %02x\n", idcode[0]);
-			return NULL;
+			ret = -EINVAL;
+			goto err_read_id;
 		}
 	}
 
 #ifdef CONFIG_OF_CONTROL
 	if (spi_flash_decode_fdt(gd->fdt_blob, flash)) {
 		debug("SF: FDT decode error\n");
+		ret = -EINVAL;
 		goto err_read_id;
 	}
 #endif
@@ -355,32 +361,50 @@ static struct spi_flash *spi_flash_probe_slave(struct spi_slave *spi)
 	/* Release spi bus */
 	spi_release_bus(spi);
 
-	return flash;
+	return 0;
 
 err_read_id:
 	spi_release_bus(spi);
-err_claim_bus:
-	spi_free_slave(spi);
-	return NULL;
+	return ret;
+}
+
+static struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
+{
+	struct spi_flash *flash;
+
+	/* Allocate space if needed (not used by sf-uclass */
+	flash = calloc(1, sizeof(*flash));
+	if (!flash) {
+		debug("SF: Failed to allocate spi_flash\n");
+		return NULL;
+	}
+
+	if (spi_flash_probe_slave(bus, flash)) {
+		spi_free_slave(bus);
+		free(flash);
+		return NULL;
+	}
+
+	return flash;
 }
 
-struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
+struct spi_flash *spi_flash_probe(unsigned int busnum, unsigned int cs,
 		unsigned int max_hz, unsigned int spi_mode)
 {
-	struct spi_slave *spi;
+	struct spi_slave *bus;
 
-	spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
-	return spi_flash_probe_slave(spi);
+	bus = spi_setup_slave(busnum, cs, max_hz, spi_mode);
+	return spi_flash_probe_tail(bus);
 }
 
 #ifdef CONFIG_OF_SPI_FLASH
 struct spi_flash *spi_flash_probe_fdt(const void *blob, int slave_node,
 				      int spi_node)
 {
-	struct spi_slave *spi;
+	struct spi_slave *bus;
 
-	spi = spi_setup_slave_fdt(blob, slave_node, spi_node);
-	return spi_flash_probe_slave(spi);
+	bus = spi_setup_slave_fdt(blob, slave_node, spi_node);
+	return spi_flash_probe_tail(bus);
 }
 #endif
 
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 17/25] dm: sf: Add a uclass for SPI flash
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (15 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 16/25] spi: Use error return value in sf_ops Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 18/25] dm: Convert spi_flash_probe() and 'sf probe' to use driver model Simon Glass
                   ` (8 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Add a driver model uclass for SPI flash which supports the common
operations (read, write, erase). Since we must keep support for the
non-dm interface, some modification of the spi_flash header is required.

CONFIG_DM_SPI_FLASH is used to enable driver model for SPI flash.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/mtd/spi/Makefile    |  7 ++++-
 drivers/mtd/spi/sf-uclass.c | 63 ++++++++++++++++++++++++++++++++++++++++
 include/dm/uclass-id.h      |  1 +
 include/spi_flash.h         | 70 +++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 140 insertions(+), 1 deletion(-)
 create mode 100644 drivers/mtd/spi/sf-uclass.c

diff --git a/drivers/mtd/spi/Makefile b/drivers/mtd/spi/Makefile
index 9e18fb4..15789a0 100644
--- a/drivers/mtd/spi/Makefile
+++ b/drivers/mtd/spi/Makefile
@@ -5,13 +5,18 @@
 # SPDX-License-Identifier:	GPL-2.0+
 #
 
+obj-$(CONFIG_DM_SPI_FLASH) += sf-uclass.o
+
 ifdef CONFIG_SPL_BUILD
 obj-$(CONFIG_SPL_SPI_LOAD)	+= spi_spl_load.o
 obj-$(CONFIG_SPL_SPI_BOOT)	+= fsl_espi_spl.o
 endif
 
+#ifndef CONFIG_DM_SPI
+obj-$(CONFIG_SPI_FLASH) += sf_probe.o
+#endif
 obj-$(CONFIG_CMD_SF) += sf.o
-obj-$(CONFIG_SPI_FLASH) += sf_params.o sf_probe.o sf_ops.o
+obj-$(CONFIG_SPI_FLASH) += sf_ops.o sf_params.o
 obj-$(CONFIG_SPI_FRAM_RAMTRON) += ramtron.o
 obj-$(CONFIG_SPI_FLASH_SANDBOX) += sandbox.o
 obj-$(CONFIG_SPI_M95XXX) += eeprom_m95xxx.o
diff --git a/drivers/mtd/spi/sf-uclass.c b/drivers/mtd/spi/sf-uclass.c
new file mode 100644
index 0000000..376d815
--- /dev/null
+++ b/drivers/mtd/spi/sf-uclass.c
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2014 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <spi.h>
+#include <spi_flash.h>
+#include <dm/device-internal.h>
+#include "sf_internal.h"
+
+/*
+ * TODO(sjg at chromium.org): This is an old-style function. We should remove
+ * it when all SPI flash drivers use dm
+ */
+struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
+				  unsigned int max_hz, unsigned int spi_mode)
+{
+	struct udevice *dev;
+
+	if (spi_flash_probe_bus_cs(bus, cs, max_hz, spi_mode, &dev))
+		return NULL;
+
+	return dev->uclass_priv;
+}
+
+void spi_flash_free(struct spi_flash *flash)
+{
+	spi_flash_remove(flash->spi->dev);
+}
+
+int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
+			   unsigned int max_hz, unsigned int spi_mode,
+			   struct udevice **devp)
+{
+	struct spi_slave *slave;
+	struct udevice *bus;
+	char name[20], *str;
+	int ret;
+
+	snprintf(name, sizeof(name), "%d:%d", busnum, cs);
+	str = strdup(name);
+	ret = spi_get_bus_and_cs(busnum, cs, max_hz, spi_mode,
+				  "spi_flash_std", str, &bus, &slave);
+	if (ret)
+		return ret;
+
+	*devp = slave->dev;
+	return 0;
+}
+
+int spi_flash_remove(struct udevice *dev)
+{
+	return device_remove(dev);
+}
+
+UCLASS_DRIVER(spi_flash) = {
+	.id		= UCLASS_SPI_FLASH,
+	.name		= "spi_flash",
+	.per_device_auto_alloc_size = sizeof(struct spi_flash),
+};
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index dce405e..02aee45 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -24,6 +24,7 @@ enum uclass_id {
 	UCLASS_GPIO,		/* Bank of general-purpose I/O pins */
 	UCLASS_SERIAL,		/* Serial UART */
 	UCLASS_SPI,		/* SPI bus */
+	UCLASS_SPI_FLASH,	/* SPI flash */
 
 	UCLASS_COUNT,
 	UCLASS_INVALID = -1,
diff --git a/include/spi_flash.h b/include/spi_flash.h
index 094a512..72903f5 100644
--- a/include/spi_flash.h
+++ b/include/spi_flash.h
@@ -15,6 +15,7 @@
 #ifndef _SPI_FLASH_H_
 #define _SPI_FLASH_H_
 
+#include <dm.h>	/* Because we dereference struct udevice here */
 #include <linux/types.h>
 
 struct spi_slave;
@@ -48,7 +49,12 @@ struct spi_slave;
  * return 0 - Success, 1 - Failure
  */
 struct spi_flash {
+#ifdef CONFIG_DM_SPI_FLASH
 	struct spi_slave *spi;
+	struct udevice *dev;
+#else
+	struct spi_slave *spi;
+#endif
 	const char *name;
 	u8 dual_flash;
 	u8 shift;
@@ -69,12 +75,75 @@ struct spi_flash {
 	u8 dummy_byte;
 
 	void *memory_map;
+#ifndef CONFIG_DM_SPI_FLASH
+	/*
+	 * These are not strictly needed for driver model, but keep them here
+	 * whilt the transition is in progress.
+	 *
+	 * Normally each driver would provide its own operations, but for
+	 * SPI flash most chips use the same algorithms. One approach is
+	 * to create a 'common' SPI flash device which knows how to talk
+	 * to most devices, and then allow other drivers to be used instead
+	 * if requird, perhaps with a way of scanning through the list to
+	 * find the driver that matches the device.
+	 */
 	int (*read)(struct spi_flash *flash, u32 offset, size_t len, void *buf);
 	int (*write)(struct spi_flash *flash, u32 offset, size_t len,
 			const void *buf);
 	int (*erase)(struct spi_flash *flash, u32 offset, size_t len);
+#endif
+};
+
+struct dm_spi_flash_ops {
+	int (*read)(struct udevice *dev, u32 offset, size_t len, void *buf);
+	int (*write)(struct udevice *dev, u32 offset, size_t len,
+		     const void *buf);
+	int (*erase)(struct udevice *dev, u32 offset, size_t len);
 };
 
+/* Access the serial operations for a device */
+#define sf_get_ops(dev) ((struct dm_spi_flash_ops *)(dev)->driver->ops)
+
+#ifdef CONFIG_DM_SPI_FLASH
+int spi_flash_probe_bus_cs(unsigned int busnum, unsigned int cs,
+			   unsigned int max_hz, unsigned int spi_mode,
+			   struct udevice **devp);
+
+/* Compatibility function - this is the old U-Boot API */
+struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
+				  unsigned int max_hz, unsigned int spi_mode);
+
+/* Compatibility function - this is the old U-Boot API */
+void spi_flash_free(struct spi_flash *flash);
+
+int spi_flash_remove(struct udevice *flash);
+
+static inline int spi_flash_read(struct spi_flash *flash, u32 offset,
+		size_t len, void *buf)
+{
+	return sf_get_ops(flash->dev)->read(flash->dev, offset, len, buf);
+}
+
+static inline int spi_flash_write(struct spi_flash *flash, u32 offset,
+		size_t len, const void *buf)
+{
+	return sf_get_ops(flash->dev)->write(flash->dev, offset, len, buf);
+}
+
+static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
+		size_t len)
+{
+	return sf_get_ops(flash->dev)->erase(flash->dev, offset, len);
+}
+
+struct sandbox_state;
+
+int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
+			 struct udevice *bus, int of_offset, const char *spec);
+
+void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs);
+
+#else
 struct spi_flash *spi_flash_probe(unsigned int bus, unsigned int cs,
 		unsigned int max_hz, unsigned int spi_mode);
 
@@ -109,6 +178,7 @@ static inline int spi_flash_erase(struct spi_flash *flash, u32 offset,
 {
 	return flash->erase(flash, offset, len);
 }
+#endif
 
 void spi_boot(void) __noreturn;
 void spi_spl_load_image(uint32_t offs, unsigned int size, void *vdst);
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 18/25] dm: Convert spi_flash_probe() and 'sf probe' to use driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (16 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 17/25] dm: sf: Add a uclass for SPI flash Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 19/25] dm: sf: sandbox: Convert SPI flash driver to " Simon Glass
                   ` (7 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

We want the SPI flash probing feature to operate as a standard driver.
Add a driver for the basic probing feature used by most boards. This
will be activated by device_probe() as with any other driver.

The 'sf probe' command currently keeps track of the SPI slave that it
last used. This doesn't work with driver model, since some other driver
or system may have probed the device and have access to it too. On the
other hand, if we try to probe a device twice the second probe is a nop
with driver model.

Fix this by searching for the matching device, removing it, and then
probing it again. This should work as expected regardless of other device
activity.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/cmd_sf.c            | 23 ++++++++++++++++
 drivers/mtd/spi/sf_probe.c | 65 ++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/common/cmd_sf.c b/common/cmd_sf.c
index 1bb41d3..b536b62 100644
--- a/common/cmd_sf.c
+++ b/common/cmd_sf.c
@@ -8,11 +8,13 @@
 
 #include <common.h>
 #include <div64.h>
+#include <dm.h>
 #include <malloc.h>
 #include <spi.h>
 #include <spi_flash.h>
 
 #include <asm/io.h>
+#include <dm/device-internal.h>
 
 #ifndef CONFIG_SF_DEFAULT_SPEED
 # define CONFIG_SF_DEFAULT_SPEED	1000000
@@ -94,7 +96,12 @@ static int do_spi_flash_probe(int argc, char * const argv[])
 	unsigned int speed = CONFIG_SF_DEFAULT_SPEED;
 	unsigned int mode = CONFIG_SF_DEFAULT_MODE;
 	char *endp;
+#ifdef CONFIG_DM_SPI_FLASH
+	struct udevice *new, *bus_dev;
+	int ret;
+#else
 	struct spi_flash *new;
+#endif
 
 	if (argc >= 2) {
 		cs = simple_strtoul(argv[1], &endp, 0);
@@ -122,6 +129,21 @@ static int do_spi_flash_probe(int argc, char * const argv[])
 			return -1;
 	}
 
+#ifdef CONFIG_DM_SPI_FLASH
+	/* Remove the old device, otherwise probe will just be a nop */
+	ret = spi_find_bus_and_cs(bus, cs, &bus_dev, &new);
+	if (!ret)
+		device_remove(new);
+	flash = NULL;
+	ret = spi_flash_probe_bus_cs(bus, cs, speed, mode, &new);
+	if (ret) {
+		printf("Failed to initialize SPI flash at %u:%u (error %d)\n",
+		       bus, cs, ret);
+		return 1;
+	}
+
+	flash = new->uclass_priv;
+#else
 	new = spi_flash_probe(bus, cs, speed, mode);
 	if (!new) {
 		printf("Failed to initialize SPI flash at %u:%u\n", bus, cs);
@@ -131,6 +153,7 @@ static int do_spi_flash_probe(int argc, char * const argv[])
 	if (flash)
 		spi_flash_free(flash);
 	flash = new;
+#endif
 
 	return 0;
 }
diff --git a/drivers/mtd/spi/sf_probe.c b/drivers/mtd/spi/sf_probe.c
index 5ed828f..3373f97 100644
--- a/drivers/mtd/spi/sf_probe.c
+++ b/drivers/mtd/spi/sf_probe.c
@@ -9,6 +9,7 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <errno.h>
 #include <fdtdec.h>
 #include <malloc.h>
@@ -131,13 +132,15 @@ static int spi_flash_validate_params(struct spi_slave *spi, u8 *idcode,
 	flash->dual_flash = flash->spi->option;
 
 	/* Assign spi_flash ops */
+#ifndef CONFIG_DM_SPI_FLASH
 	flash->write = spi_flash_cmd_write_ops;
-#ifdef CONFIG_SPI_FLASH_SST
+#if defined(CONFIG_SPI_FLASH_SST)
 	if (params->flags & SST_WP)
 		flash->write = sst_write_wp;
 #endif
 	flash->erase = spi_flash_cmd_erase_ops;
 	flash->read = spi_flash_cmd_read_ops;
+#endif
 
 	/* Compute the flash size */
 	flash->shift = (flash->dual_flash & SF_DUAL_PARALLEL_FLASH) ? 1 : 0;
@@ -368,7 +371,8 @@ err_read_id:
 	return ret;
 }
 
-static struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
+#ifndef CONFIG_DM_SPI_FLASH
+struct spi_flash *spi_flash_probe_tail(struct spi_slave *bus)
 {
 	struct spi_flash *flash;
 
@@ -413,3 +417,60 @@ void spi_flash_free(struct spi_flash *flash)
 	spi_free_slave(flash->spi);
 	free(flash);
 }
+
+#else /* defined CONFIG_DM_SPI_FLASH */
+
+static int spi_flash_std_read(struct udevice *dev, u32 offset, size_t len,
+			      void *buf)
+{
+	struct spi_flash *flash = dev->uclass_priv;
+
+	return spi_flash_cmd_read_ops(flash, offset, len, buf);
+}
+
+int spi_flash_std_write(struct udevice *dev, u32 offset, size_t len,
+			const void *buf)
+{
+	struct spi_flash *flash = dev->uclass_priv;
+
+	return spi_flash_cmd_write_ops(flash, offset, len, buf);
+}
+
+int spi_flash_std_erase(struct udevice *dev, u32 offset, size_t len)
+{
+	struct spi_flash *flash = dev->uclass_priv;
+
+	return spi_flash_cmd_erase_ops(flash, offset, len);
+}
+
+int spi_flash_std_probe(struct udevice *dev)
+{
+	struct spi_slave *slave = dev_get_parentdata(dev);
+	struct spi_flash *flash;
+
+	flash = dev->uclass_priv;
+	flash->dev = dev;
+	return spi_flash_probe_slave(slave, flash);
+}
+
+static const struct dm_spi_flash_ops spi_flash_std_ops = {
+	.read = spi_flash_std_read,
+	.write = spi_flash_std_write,
+	.erase = spi_flash_std_erase,
+};
+
+static const struct udevice_id spi_flash_std_ids[] = {
+	{ .compatible = "spi-flash" },
+	{ }
+};
+
+U_BOOT_DRIVER(spi_flash_std) = {
+	.name		= "spi_flash_std",
+	.id		= UCLASS_SPI_FLASH,
+	.of_match	= spi_flash_std_ids,
+	.probe		= spi_flash_std_probe,
+	.priv_auto_alloc_size = sizeof(struct spi_flash),
+	.ops		= &spi_flash_std_ops,
+};
+
+#endif /* CONFIG_DM_SPI_FLASH */
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 19/25] dm: sf: sandbox: Convert SPI flash driver to driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (17 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 18/25] dm: Convert spi_flash_probe() and 'sf probe' to use driver model Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 20/25] dm: exynos: config: Use driver model for SPI flash Simon Glass
                   ` (6 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Convert sandbox's spi flash emulation driver to use driver model.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/sandbox/include/asm/spi.h   |  13 --
 arch/sandbox/include/asm/state.h |   1 -
 drivers/mtd/spi/sandbox.c        | 326 +++++++++++++++++++++++++++++++++------
 include/configs/sandbox.h        |   1 +
 4 files changed, 279 insertions(+), 62 deletions(-)

diff --git a/arch/sandbox/include/asm/spi.h b/arch/sandbox/include/asm/spi.h
index 49b4a0f..9985e3c 100644
--- a/arch/sandbox/include/asm/spi.h
+++ b/arch/sandbox/include/asm/spi.h
@@ -33,19 +33,6 @@ struct sandbox_spi_emu_ops {
 };
 
 /*
- * There are times when the data lines are allowed to tristate.  What
- * is actually sensed on the line depends on the hardware.  It could
- * always be 0xFF/0x00 (if there are pull ups/downs), or things could
- * float and so we'd get garbage back.  This func encapsulates that
- * scenario so we can worry about the details here.
- */
-static inline void sandbox_spi_tristate(u8 *buf, uint len)
-{
-	/* XXX: make this into a user config option ? */
-	memset(buf, 0xff, len);
-}
-
-/*
  * Extract the bus/cs from the spi spec and return the start of the spi
  * client spec.  If the bus/cs are invalid for the current config, then
  * it returns NULL.
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 4e0981a..32d55cc 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -42,7 +42,6 @@ enum state_terminal_raw {
 
 struct sandbox_spi_info {
 	const char *spec;
-	const struct sandbox_spi_emu_ops *ops;
 	struct udevice *emul;
 };
 
diff --git a/drivers/mtd/spi/sandbox.c b/drivers/mtd/spi/sandbox.c
index 98e0a34..7d572fd 100644
--- a/drivers/mtd/spi/sandbox.c
+++ b/drivers/mtd/spi/sandbox.c
@@ -9,6 +9,7 @@
  */
 
 #include <common.h>
+#include <dm.h>
 #include <malloc.h>
 #include <spi.h>
 #include <os.h>
@@ -19,6 +20,11 @@
 #include <asm/getopt.h>
 #include <asm/spi.h>
 #include <asm/state.h>
+#include <dm/device-internal.h>
+#include <dm/lists.h>
+#include <dm/uclass-internal.h>
+
+DECLARE_GLOBAL_DATA_PTR;
 
 /*
  * The different states that our SPI flash transitions between.
@@ -34,12 +40,14 @@ enum sandbox_sf_state {
 	SF_ERASE, /* erase the flash */
 	SF_READ_STATUS, /* read the flash's status register */
 	SF_READ_STATUS1, /* read the flash's status register upper 8 bits*/
+	SF_WRITE_STATUS, /* write the flash's status register */
 };
 
 static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
 {
 	static const char * const states[] = {
 		"CMD", "ID", "ADDR", "READ", "WRITE", "ERASE", "READ_STATUS",
+		"READ_STATUS1", "WRITE_STATUS",
 	};
 	return states[state];
 }
@@ -84,71 +92,109 @@ struct sandbox_spi_flash {
 	int fd;
 };
 
-static int sandbox_sf_setup(void **priv, const char *spec)
+struct sandbox_spi_flash_plat_data {
+	const char *filename;
+	const char *device_name;
+	int bus;
+	int cs;
+};
+
+/**
+ * This is a very strange probe function. If it has platform data (which may
+ * have come from the device tree) then this function gets the filename and
+ * device type from there. Failing that it looks at the command line
+ * parameter.
+ */
+static int sandbox_sf_probe(struct udevice *dev)
 {
 	/* spec = idcode:file */
-	struct sandbox_spi_flash *sbsf;
+	struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
 	const char *file;
 	size_t len, idname_len;
 	const struct spi_flash_params *data;
-
-	file = strchr(spec, ':');
-	if (!file) {
-		printf("sandbox_sf: unable to parse file\n");
-		goto error;
+	struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
+	const char *spec = NULL;
+	int ret = 0;
+
+	if (!pdata->filename) {
+		struct sandbox_state *state = state_get_current();
+		struct udevice *bus = dev->parent;
+		int seq;
+
+		assert(bus->seq != -1);
+		seq = uclass_resolve_seq(dev);
+		if (seq < 0)
+			return seq;
+		if (bus->seq < CONFIG_SANDBOX_SPI_MAX_BUS &&
+		    dev->req_seq < CONFIG_SANDBOX_SPI_MAX_CS)
+			spec = state->spi[bus->seq][seq].spec;
+		if (!spec)
+			return -ENOENT;
+
+		file = strchr(spec, ':');
+		if (!file) {
+			printf("sandbox_sf: unable to parse file\n");
+			ret = -EINVAL;
+			goto error;
+		}
+		idname_len = file - spec;
+		pdata->filename = file + 1;
+		pdata->device_name = spec;
+		++file;
+	} else {
+		spec = strchr(pdata->device_name, ',');
+		if (spec)
+			spec++;
+		else
+			spec = pdata->device_name;
+		idname_len = strlen(spec);
 	}
-	idname_len = file - spec;
-	++file;
 
 	for (data = spi_flash_params_table; data->name; data++) {
 		len = strlen(data->name);
 		if (idname_len != len)
 			continue;
-		if (!memcmp(spec, data->name, len))
+		if (!strncasecmp(spec, data->name, len))
 			break;
 	}
 	if (!data->name) {
 		printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
 		       spec);
+		ret = -EINVAL;
 		goto error;
 	}
 
 	if (sandbox_sf_0xff[0] == 0x00)
 		memset(sandbox_sf_0xff, 0xff, sizeof(sandbox_sf_0xff));
 
-	sbsf = calloc(sizeof(*sbsf), 1);
-	if (!sbsf) {
-		printf("sandbox_sf: out of memory\n");
-		goto error;
-	}
-
-	sbsf->fd = os_open(file, 02);
+	sbsf->fd = os_open(pdata->filename, 02);
 	if (sbsf->fd == -1) {
 		free(sbsf);
-		printf("sandbox_sf: unable to open file '%s'\n", file);
+		printf("sandbox_sf: unable to open file '%s'\n",
+		       pdata->filename);
+		ret = -EIO;
 		goto error;
 	}
 
 	sbsf->data = data;
-
-	*priv = sbsf;
 	return 0;
 
  error:
-	return 1;
+	return ret;
 }
 
-static void sandbox_sf_free(void *priv)
+static int sandbox_sf_remove(struct udevice *dev)
 {
-	struct sandbox_spi_flash *sbsf = priv;
+	struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
 
 	os_close(sbsf->fd);
-	free(sbsf);
+
+	return 0;
 }
 
-static void sandbox_sf_cs_activate(void *priv)
+static void sandbox_sf_cs_activate(struct udevice *dev)
 {
-	struct sandbox_spi_flash *sbsf = priv;
+	struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
 
 	debug("sandbox_sf: CS activated; state is fresh!\n");
 
@@ -160,11 +206,24 @@ static void sandbox_sf_cs_activate(void *priv)
 	sbsf->cmd = SF_CMD;
 }
 
-static void sandbox_sf_cs_deactivate(void *priv)
+static void sandbox_sf_cs_deactivate(struct udevice *dev)
 {
 	debug("sandbox_sf: CS deactivated; cmd done processing!\n");
 }
 
+/*
+ * There are times when the data lines are allowed to tristate.  What
+ * is actually sensed on the line depends on the hardware.  It could
+ * always be 0xFF/0x00 (if there are pull ups/downs), or things could
+ * float and so we'd get garbage back.  This func encapsulates that
+ * scenario so we can worry about the details here.
+ */
+static void sandbox_spi_tristate(u8 *buf, uint len)
+{
+	/* XXX: make this into a user config option ? */
+	memset(buf, 0xff, len);
+}
+
 /* Figure out what command this stream is telling us to do */
 static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
 				  u8 *tx)
@@ -172,7 +231,8 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
 	enum sandbox_sf_state oldstate = sbsf->state;
 
 	/* We need to output a byte for the cmd byte we just ate */
-	sandbox_spi_tristate(tx, 1);
+	if (tx)
+		sandbox_spi_tristate(tx, 1);
 
 	sbsf->cmd = rx[0];
 	switch (sbsf->cmd) {
@@ -200,6 +260,9 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
 		debug(" write enabled\n");
 		sbsf->status |= STAT_WEL;
 		break;
+	case CMD_WRITE_STATUS:
+		sbsf->state = SF_WRITE_STATUS;
+		break;
 	default: {
 		int flags = sbsf->data->flags;
 
@@ -216,7 +279,7 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
 			sbsf->erase_size = 64 << 10;
 		} else {
 			debug(" cmd unknown: %#x\n", sbsf->cmd);
-			return 1;
+			return -EIO;
 		}
 		sbsf->state = SF_ADDR;
 		break;
@@ -246,20 +309,27 @@ int sandbox_erase_part(struct sandbox_spi_flash *sbsf, int size)
 	return 0;
 }
 
-static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
-		uint bytes)
+static int sandbox_sf_xfer(struct udevice *dev, unsigned int bitlen,
+			   const void *rxp, void *txp, unsigned long flags)
 {
-	struct sandbox_spi_flash *sbsf = priv;
+	struct sandbox_spi_flash *sbsf = dev_get_priv(dev);
+	const uint8_t *rx = rxp;
+	uint8_t *tx = txp;
 	uint cnt, pos = 0;
+	int bytes = bitlen / 8;
 	int ret;
 
 	debug("sandbox_sf: state:%x(%s) bytes:%u\n", sbsf->state,
 	      sandbox_sf_state_name(sbsf->state), bytes);
 
+	if ((flags & SPI_XFER_BEGIN))
+		sandbox_sf_cs_activate(dev);
+
 	if (sbsf->state == SF_CMD) {
 		/* Figure out the initial state */
-		if (sandbox_sf_process_cmd(sbsf, rx, tx))
-			return 1;
+		ret = sandbox_sf_process_cmd(sbsf, rx, tx);
+		if (ret)
+			return ret;
 		++pos;
 	}
 
@@ -290,7 +360,9 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 				sbsf->off = (sbsf->off << 8) | rx[pos];
 			debug("addr:%06x\n", sbsf->off);
 
-			sandbox_spi_tristate(&tx[pos++], 1);
+			if (tx)
+				sandbox_spi_tristate(&tx[pos], 1);
+			pos++;
 
 			/* See if we're done processing */
 			if (sbsf->addr_bytes <
@@ -300,7 +372,7 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 			/* Next state! */
 			if (os_lseek(sbsf->fd, sbsf->off, OS_SEEK_SET) < 0) {
 				puts("sandbox_sf: os_lseek() failed");
-				return 1;
+				return -EIO;
 			}
 			switch (sbsf->cmd) {
 			case CMD_READ_ARRAY_FAST:
@@ -326,10 +398,11 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 
 			cnt = bytes - pos;
 			debug(" tx: read(%u)\n", cnt);
+			assert(tx);
 			ret = os_read(sbsf->fd, tx + pos, cnt);
 			if (ret < 0) {
-				puts("sandbox_spi: os_read() failed\n");
-				return 1;
+				puts("sandbox_sf: os_read() failed\n");
+				return -EIO;
 			}
 			pos += ret;
 			break;
@@ -345,6 +418,10 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 			memset(tx + pos, sbsf->status >> 8, cnt);
 			pos += cnt;
 			break;
+		case SF_WRITE_STATUS:
+			debug(" write status: %#x (ignored)\n", rx[pos]);
+			pos = bytes;
+			break;
 		case SF_WRITE:
 			/*
 			 * XXX: need to handle exotic behavior:
@@ -359,11 +436,12 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 
 			cnt = bytes - pos;
 			debug(" rx: write(%u)\n", cnt);
-			sandbox_spi_tristate(&tx[pos], cnt);
+			if (tx)
+				sandbox_spi_tristate(&tx[pos], cnt);
 			ret = os_write(sbsf->fd, rx + pos, cnt);
 			if (ret < 0) {
 				puts("sandbox_spi: os_write() failed\n");
-				return 1;
+				return -EIO;
 			}
 			pos += ret;
 			sbsf->status &= ~STAT_WEL;
@@ -388,7 +466,8 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 			      sbsf->erase_size);
 
 			cnt = bytes - pos;
-			sandbox_spi_tristate(&tx[pos], cnt);
+			if (tx)
+				sandbox_spi_tristate(&tx[pos], cnt);
 			pos += cnt;
 
 			/*
@@ -410,17 +489,33 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
 	}
 
  done:
-	return pos == bytes ? 0 : 1;
+	if (flags & SPI_XFER_END)
+		sandbox_sf_cs_deactivate(dev);
+	return pos == bytes ? 0 : -EIO;
+}
+
+int sandbox_sf_ofdata_to_platdata(struct udevice *dev)
+{
+	struct sandbox_spi_flash_plat_data *pdata = dev_get_platdata(dev);
+	const void *blob = gd->fdt_blob;
+	int node = dev->of_offset;
+
+	pdata->filename = fdt_getprop(blob, node, "sandbox,filename", NULL);
+	pdata->device_name = fdt_getprop(blob, node, "compatible", NULL);
+	if (!pdata->filename || !pdata->device_name) {
+		debug("%s: Missing properties, filename=%s, device_name=%s\n",
+		      __func__, pdata->filename, pdata->device_name);
+		return -EINVAL;
+	}
+
+	return 0;
 }
 
-static const struct sandbox_spi_emu_ops sandbox_sf_ops = {
-	.setup         = sandbox_sf_setup,
-	.free          = sandbox_sf_free,
-	.cs_activate   = sandbox_sf_cs_activate,
-	.cs_deactivate = sandbox_sf_cs_deactivate,
+static const struct dm_spi_emul_ops sandbox_sf_emul_ops = {
 	.xfer          = sandbox_sf_xfer,
 };
 
+#ifdef CONFIG_SPI_FLASH
 static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
 				     const char *arg)
 {
@@ -438,8 +533,143 @@ static int sandbox_cmdline_cb_spi_sf(struct sandbox_state *state,
 	 * spec here, but the problem is that no U-Boot init has been done
 	 * yet. Perhaps we can figure something out.
 	 */
-	state->spi[bus][cs].ops = &sandbox_sf_ops;
 	state->spi[bus][cs].spec = spec;
 	return 0;
 }
 SANDBOX_CMDLINE_OPT(spi_sf, 1, "connect a SPI flash: <bus>:<cs>:<id>:<file>");
+
+int sandbox_sf_bind_emul(struct sandbox_state *state, int busnum, int cs,
+			 struct udevice *bus, int of_offset, const char *spec)
+{
+	struct udevice *emul;
+	char name[20], *str;
+	struct driver *drv;
+	int ret;
+
+	/* now the emulator */
+	strncpy(name, spec, sizeof(name) - 6);
+	name[sizeof(name) - 6] = '\0';
+	strcat(name, "-emul");
+	str = strdup(name);
+	if (!str)
+		return -ENOMEM;
+	drv = lists_driver_lookup_name("sandbox_sf_emul");
+	if (!drv) {
+		puts("Cannot find sandbox_sf_emul driver\n");
+		return -ENOENT;
+	}
+	ret = device_bind(bus, drv, str, NULL, of_offset, &emul);
+	if (ret) {
+		printf("Cannot create emul device for spec '%s' (err=%d)\n",
+		       spec, ret);
+		return ret;
+	}
+	/* We have the same parent bus, so use a different sequence number */
+	emul->req_seq = 100 + cs;
+	state->spi[busnum][cs].emul = emul;
+
+	return 0;
+}
+
+void sandbox_sf_unbind_emul(struct sandbox_state *state, int busnum, int cs)
+{
+	state->spi[busnum][cs].emul = NULL;
+}
+
+static int sandbox_sf_bind_bus_cs(struct sandbox_state *state, int busnum,
+				  int cs, const char *spec)
+{
+	struct udevice *bus, *slave;
+	int ret;
+
+	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, true, &bus);
+	if (ret) {
+		printf("Invalid bus %d for spec '%s' (err=%d)\n", busnum,
+		       spec, ret);
+		return ret;
+	}
+	ret = device_find_child_by_seq(bus, cs, true, &slave);
+	if (!ret) {
+		printf("Chip select %d already exists for spec '%s'\n", cs,
+		       spec);
+		return -EEXIST;
+	}
+
+	ret = spi_bind_device(bus, cs, "spi_flash_std", spec, &slave);
+	if (ret)
+		return ret;
+
+	return sandbox_sf_bind_emul(state, busnum, cs, bus, -1, spec);
+}
+
+int sandbox_spi_get_emul(struct sandbox_state *state,
+			 struct udevice *bus, struct udevice *slave,
+			 struct udevice **emulp)
+{
+	struct sandbox_spi_info *info;
+	int busnum = bus->seq;
+	int cs = slave->seq;
+	int ret;
+
+	info = &state->spi[busnum][cs];
+	if (!info->emul) {
+		/* Use the same device tree node as the SPI flash device */
+		debug("%s: busnum=%u, cs=%u: binding SPI flash emulation: ",
+		      __func__, busnum, cs);
+		ret = sandbox_sf_bind_emul(state, busnum, cs, bus,
+					   slave->of_offset, slave->name);
+		if (ret) {
+			debug("failed (err=%d)\n", ret);
+			return ret;
+		}
+		debug("OK\n");
+	}
+	*emulp = info->emul;
+
+	return 0;
+}
+
+int dm_scan_other(bool pre_reloc_only)
+{
+	struct sandbox_state *state = state_get_current();
+	int busnum, cs;
+
+	if (pre_reloc_only)
+		return 0;
+	for (busnum = 0; busnum < CONFIG_SANDBOX_SPI_MAX_BUS; busnum++) {
+		for (cs = 0; cs < CONFIG_SANDBOX_SPI_MAX_CS; cs++) {
+			const char *spec = state->spi[busnum][cs].spec;
+			int ret;
+
+			if (spec) {
+				ret = sandbox_sf_bind_bus_cs(state, busnum,
+							     cs, spec);
+				if (ret) {
+					debug("%s: Bind failed for bus %d, cs %d\n",
+					      __func__, busnum, cs);
+					return ret;
+				}
+			}
+		}
+	}
+
+	return 0;
+}
+#endif
+
+static const struct udevice_id sandbox_sf_ids[] = {
+	{ .compatible = "sandbox,spi-flash" },
+	{ }
+};
+
+U_BOOT_DRIVER(sandbox_sf_emul) = {
+	.name		= "sandbox_sf_emul",
+	.id		= UCLASS_SPI_EMUL,
+	.of_match	= sandbox_sf_ids,
+	.ofdata_to_platdata = sandbox_sf_ofdata_to_platdata,
+	.probe		= sandbox_sf_probe,
+	.remove		= sandbox_sf_remove,
+	.priv_auto_alloc_size = sizeof(struct sandbox_spi_flash),
+	.platdata_auto_alloc_size = sizeof(struct sandbox_spi_flash_plat_data),
+	.ops		= &sandbox_sf_emul_ops,
+};
diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index 050f84d..2a87585 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -101,6 +101,7 @@
 #define CONFIG_CMD_SPI
 #define CONFIG_SPI_FLASH
 #define CONFIG_DM_SPI
+#define CONFIG_DM_SPI_FLASH
 #define CONFIG_SPI_FLASH_ATMEL
 #define CONFIG_SPI_FLASH_EON
 #define CONFIG_SPI_FLASH_GIGADEVICE
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 20/25] dm: exynos: config: Use driver model for SPI flash
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (18 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 19/25] dm: sf: sandbox: Convert SPI flash driver to " Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 21/25] dm: spi: Add tests Simon Glass
                   ` (5 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Use driver model for exynos5 board SPI flash.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 include/configs/exynos5-dt.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/configs/exynos5-dt.h b/include/configs/exynos5-dt.h
index 873dd63..1a2e9a7 100644
--- a/include/configs/exynos5-dt.h
+++ b/include/configs/exynos5-dt.h
@@ -21,6 +21,7 @@
 #define CONFIG_DM_GPIO
 #define CONFIG_DM_SERIAL
 #define CONFIG_DM_SPI
+#define CONFIG_DM_SPI_FLASH
 
 #define CONFIG_SYS_GENERIC_BOARD
 #define CONFIG_ARCH_CPU_INIT
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 21/25] dm: spi: Add tests
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (19 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 20/25] dm: exynos: config: Use driver model for SPI flash Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 22/25] dm: sf: Add tests for SPI flash Simon Glass
                   ` (4 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

These tests use SPI flash (and the sandbox emulation) to operate.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/dm/Makefile |  1 +
 test/dm/spi.c    | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 test/dm/spi.c

diff --git a/test/dm/Makefile b/test/dm/Makefile
index 5c2415e..d1b9c9a 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -18,4 +18,5 @@ obj-$(CONFIG_DM_TEST) += core.o
 obj-$(CONFIG_DM_TEST) += ut.o
 ifneq ($(CONFIG_SANDBOX),)
 obj-$(CONFIG_DM_GPIO) += gpio.o
+obj-$(CONFIG_DM_SPI) += spi.o
 endif
diff --git a/test/dm/spi.c b/test/dm/spi.c
new file mode 100644
index 0000000..45aabeb
--- /dev/null
+++ b/test/dm/spi.c
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2013 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <fdtdec.h>
+#include <spi.h>
+#include <spi_flash.h>
+#include <dm/test.h>
+#include <dm/ut.h>
+#include <dm/util.h>
+#include <asm/state.h>
+
+/* Test that sandbox SPI works correctly */
+static int dm_test_spi(struct dm_test_state *dms)
+{
+	struct spi_slave *slave;
+	struct udevice *dev;
+	const int busnum = 0, cs = 0, mode = 0;
+	const char dout[5] = {0x9f};
+	unsigned char din[5];
+
+	ut_assertok(spi_get_bus_and_cs(busnum, cs, 1000000, mode, NULL, 0,
+				       &dev, &slave));
+	ut_assertok(spi_claim_bus(slave));
+	ut_assertok(spi_xfer(slave, 40, dout, din,
+			     SPI_XFER_BEGIN | SPI_XFER_END));
+	ut_asserteq(0xff, din[0]);
+	ut_asserteq(0x20, din[1]);
+	ut_asserteq(0x20, din[2]);
+	ut_asserteq(0x15, din[3]);
+	spi_release_bus(slave);
+
+	/*
+	 * Since we are about to destroy all devices, we must tell sandbox
+	 * to forget the emulation device
+	 */
+#ifdef CONFIG_DM_SPI_FLASH
+	sandbox_sf_unbind_emul(state_get_current(), busnum, cs);
+#endif
+
+	return 0;
+}
+DM_TEST(dm_test_spi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 22/25] dm: sf: Add tests for SPI flash
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (20 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 21/25] dm: spi: Add tests Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 23/25] dm: cros_ec: Add support for driver model Simon Glass
                   ` (3 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Add a simple test for SPI that uses SPI flash. It operates by creating a
SPI flash file, and using the 'sf test' command to test that all
operations work correctly.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 test/dm/Makefile |  1 +
 test/dm/sf.c     | 43 +++++++++++++++++++++++++++++++++++++++++++
 test/dm/test.dts | 17 ++++++++++++++++-
 3 files changed, 60 insertions(+), 1 deletion(-)
 create mode 100644 test/dm/sf.c

diff --git a/test/dm/Makefile b/test/dm/Makefile
index d1b9c9a..75d3d41 100644
--- a/test/dm/Makefile
+++ b/test/dm/Makefile
@@ -19,4 +19,5 @@ obj-$(CONFIG_DM_TEST) += ut.o
 ifneq ($(CONFIG_SANDBOX),)
 obj-$(CONFIG_DM_GPIO) += gpio.o
 obj-$(CONFIG_DM_SPI) += spi.o
+obj-$(CONFIG_DM_SPI_FLASH) += sf.o
 endif
diff --git a/test/dm/sf.c b/test/dm/sf.c
new file mode 100644
index 0000000..57dd134
--- /dev/null
+++ b/test/dm/sf.c
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2013 Google, Inc
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <fdtdec.h>
+#include <spi.h>
+#include <spi_flash.h>
+#include <asm/state.h>
+#include <dm/ut.h>
+#include <dm/test.h>
+#include <dm/util.h>
+
+/* Test that sandbox SPI flash works correctly */
+static int dm_test_spi_flash(struct dm_test_state *dms)
+{
+	/*
+	 * Create an empty test file and run the SPI flash tests. This is a
+	 * long way from being a unit test, but it does test SPI device and
+	 * emulator binding, probing, the SPI flash emulator including
+	 * device tree decoding, plus the file-based backing store of SPI.
+	 *
+	 * More targeted tests could be created to perform the above steps
+	 * one at a time. This might not increase test coverage much, but
+	 * it would make bugs easier to find. It's not clear whether the
+	 * benefit is worth the extra complexity.
+	 */
+	ut_asserteq(0, run_command_list(
+		"sb save hostfs - spi.bin 0 200000;"
+		"sf probe;"
+		"sf test 0 10000", -1,  0));
+	/*
+	 * Since we are about to destroy all devices, we must tell sandbox
+	 * to forget the emulation device
+	 */
+	sandbox_sf_unbind_emul(state_get_current(), 0, 0);
+
+	return 0;
+}
+DM_TEST(dm_test_spi_flash, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
diff --git a/test/dm/test.dts b/test/dm/test.dts
index 141d82c..db88160 100644
--- a/test/dm/test.dts
+++ b/test/dm/test.dts
@@ -82,7 +82,7 @@
 		compatible = "google,another-fdt-test";
 	};
 
-	base-gpios {
+	gpio_a: base-gpios {
 		compatible = "sandbox,gpio";
 		gpio-bank-name = "a";
 		num-gpios = <20>;
@@ -93,4 +93,19 @@
 		gpio-bank-name = "b";
 		num-gpios = <10>;
 	};
+
+	spi at 0 {
+		#address-cells = <1>;
+		#size-cells = <0>;
+		reg = <0>;
+		compatible = "sandbox,spi";
+		cs-gpios = <0>, <&gpio_a 0>;
+		spi.bin at 0 {
+			reg = <0>;
+			compatible = "spansion,m25p16", "spi-flash";
+			spi-max-frequency = <40000000>;
+			sandbox,filename = "spi.bin";
+		};
+	};
+
 };
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 23/25] dm: cros_ec: Add support for driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (21 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 22/25] dm: sf: Add tests for SPI flash Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 24/25] dm: sandbox: cros_ec: Move sandbox cros_ec to driver module Simon Glass
                   ` (2 subsequent siblings)
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Add support for driver model if enabled. This involves minimal changes
to the code, mostly just plumbing around the edges.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/cros_ec.c               |  30 ++++++++++
 drivers/misc/cros_ec.c         | 122 +++++++++++++++++++++++++++++++++++++----
 drivers/misc/cros_ec_sandbox.c |   9 ++-
 include/cros_ec.h              |  27 ++++++++-
 include/dm/uclass-id.h         |   1 +
 5 files changed, 174 insertions(+), 15 deletions(-)

diff --git a/common/cros_ec.c b/common/cros_ec.c
index b8ce1b5..bb299bc 100644
--- a/common/cros_ec.c
+++ b/common/cros_ec.c
@@ -10,25 +10,44 @@
 
 #include <common.h>
 #include <cros_ec.h>
+#include <dm.h>
+#include <errno.h>
+
 DECLARE_GLOBAL_DATA_PTR;
 
+#ifndef CONFIG_DM_CROS_EC
 struct local_info {
 	struct cros_ec_dev *cros_ec_dev;	/* Pointer to cros_ec device */
 	int cros_ec_err;			/* Error for cros_ec, 0 if ok */
 };
 
 static struct local_info local;
+#endif
 
 struct cros_ec_dev *board_get_cros_ec_dev(void)
 {
+#ifdef CONFIG_DM_CROS_EC
+	struct udevice *dev;
+	int ret;
+
+	ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
+	if (ret) {
+		debug("%s: Error %d\n", __func__, ret);
+		return NULL;
+	}
+	return dev->uclass_priv;
+#else
 	return local.cros_ec_dev;
+#endif
 }
 
 static int board_init_cros_ec_devices(const void *blob)
 {
+#ifndef CONFIG_DM_CROS_EC
 	local.cros_ec_err = cros_ec_init(blob, &local.cros_ec_dev);
 	if (local.cros_ec_err)
 		return -1;  /* Will report in board_late_init() */
+#endif
 
 	return 0;
 }
@@ -40,5 +59,16 @@ int cros_ec_board_init(void)
 
 int cros_ec_get_error(void)
 {
+#ifdef CONFIG_DM_CROS_EC
+	struct udevice *dev;
+	int ret;
+
+	ret = uclass_get_device(UCLASS_CROS_EC, 0, &dev);
+	if (ret && ret != -ENODEV)
+		return ret;
+
+	return 0;
+#else
 	return local.cros_ec_err;
+#endif
 }
diff --git a/drivers/misc/cros_ec.c b/drivers/misc/cros_ec.c
index 068373b..521edfd 100644
--- a/drivers/misc/cros_ec.c
+++ b/drivers/misc/cros_ec.c
@@ -16,6 +16,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <dm.h>
 #include <i2c.h>
 #include <cros_ec.h>
 #include <fdtdec.h>
@@ -24,6 +25,8 @@
 #include <asm/errno.h>
 #include <asm/io.h>
 #include <asm-generic/gpio.h>
+#include <dm/device-internal.h>
+#include <dm/uclass-internal.h>
 
 #ifdef DEBUG_TRACE
 #define debug_trace(fmt, b...)	debug(fmt, #b)
@@ -38,7 +41,9 @@ enum {
 	CROS_EC_CMD_HASH_TIMEOUT_MS = 2000,
 };
 
+#ifndef CONFIG_DM_CROS_EC
 static struct cros_ec_dev static_dev, *last_dev;
+#endif
 
 DECLARE_GLOBAL_DATA_PTR;
 
@@ -204,6 +209,9 @@ static int send_command_proto3(struct cros_ec_dev *dev,
 			       const void *dout, int dout_len,
 			       uint8_t **dinp, int din_len)
 {
+#ifdef CONFIG_DM_CROS_EC
+	struct dm_cros_ec_ops *ops;
+#endif
 	int out_bytes, in_bytes;
 	int rv;
 
@@ -218,6 +226,10 @@ static int send_command_proto3(struct cros_ec_dev *dev,
 	if (in_bytes < 0)
 		return in_bytes;
 
+#ifdef CONFIG_DM_CROS_EC
+	ops = dm_cros_ec_get_ops(dev->dev);
+	rv = ops->packet(dev->dev, out_bytes, in_bytes);
+#else
 	switch (dev->interface) {
 #ifdef CONFIG_CROS_EC_SPI
 	case CROS_EC_IF_SPI:
@@ -235,6 +247,7 @@ static int send_command_proto3(struct cros_ec_dev *dev,
 		debug("%s: Unsupported interface\n", __func__);
 		rv = -1;
 	}
+#endif
 	if (rv < 0)
 		return rv;
 
@@ -246,6 +259,9 @@ static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 			const void *dout, int dout_len,
 			uint8_t **dinp, int din_len)
 {
+#ifdef CONFIG_DM_CROS_EC
+	struct dm_cros_ec_ops *ops;
+#endif
 	int ret = -1;
 
 	/* Handle protocol version 3 support */
@@ -254,6 +270,11 @@ static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 					   dout, dout_len, dinp, din_len);
 	}
 
+#ifdef CONFIG_DM_CROS_EC
+	ops = dm_cros_ec_get_ops(dev->dev);
+	ret = ops->command(dev->dev, cmd, cmd_version,
+			   (const uint8_t *)dout, dout_len, dinp, din_len);
+#else
 	switch (dev->interface) {
 #ifdef CONFIG_CROS_EC_SPI
 	case CROS_EC_IF_SPI:
@@ -280,6 +301,7 @@ static int send_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 	default:
 		ret = -1;
 	}
+#endif
 
 	return ret;
 }
@@ -990,6 +1012,7 @@ int cros_ec_get_ldo(struct cros_ec_dev *dev, uint8_t index, uint8_t *state)
 	return 0;
 }
 
+#ifndef CONFIG_DM_CROS_EC
 /**
  * Decode EC interface details from the device tree and allocate a suitable
  * device.
@@ -1055,11 +1078,61 @@ static int cros_ec_decode_fdt(const void *blob, int node,
 
 	return 0;
 }
+#endif
 
-int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
+#ifdef CONFIG_DM_CROS_EC
+int cros_ec_register(struct udevice *dev)
 {
+	struct cros_ec_dev *cdev = dev->uclass_priv;
+	const void *blob = gd->fdt_blob;
+	int node = dev->of_offset;
 	char id[MSG_BYTES];
+
+	cdev->dev = dev;
+	fdtdec_decode_gpio(blob, node, "ec-interrupt", &cdev->ec_int);
+	cdev->optimise_flash_write = fdtdec_get_bool(blob, node,
+						     "optimise-flash-write");
+
+	/* we will poll the EC interrupt line */
+	fdtdec_setup_gpio(&cdev->ec_int);
+	if (fdt_gpio_isvalid(&cdev->ec_int)) {
+		gpio_request(cdev->ec_int.gpio, "cros-ec-irq");
+		gpio_direction_input(cdev->ec_int.gpio);
+	}
+
+	if (cros_ec_check_version(cdev)) {
+		debug("%s: Could not detect CROS-EC version\n", __func__);
+		return -CROS_EC_ERR_CHECK_VERSION;
+	}
+
+	if (cros_ec_read_id(cdev, id, sizeof(id))) {
+		debug("%s: Could not read KBC ID\n", __func__);
+		return -CROS_EC_ERR_READ_ID;
+	}
+
+	/* Remember this device for use by the cros_ec command */
+	debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
+
+	return 0;
+}
+#else
+int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
+{
 	struct cros_ec_dev *dev;
+	char id[MSG_BYTES];
+#ifdef CONFIG_DM_CROS_EC
+	struct udevice *udev;
+	int ret;
+
+	ret = uclass_find_device(UCLASS_CROS_EC, 0, &udev);
+	if (!ret)
+		device_remove(udev);
+	ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
+	if (ret)
+		return ret;
+	dev = udev->uclass_priv;
+	return 0;
+#else
 	int node = 0;
 
 	*cros_ecp = NULL;
@@ -1108,11 +1181,14 @@ int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
 	default:
 		return 0;
 	}
+#endif
 
 	/* we will poll the EC interrupt line */
 	fdtdec_setup_gpio(&dev->ec_int);
-	if (fdt_gpio_isvalid(&dev->ec_int))
+	if (fdt_gpio_isvalid(&dev->ec_int)) {
+		gpio_request(dev->ec_int.gpio, "cros-ec-irq");
 		gpio_direction_input(dev->ec_int.gpio);
+	}
 
 	if (cros_ec_check_version(dev)) {
 		debug("%s: Could not detect CROS-EC version\n", __func__);
@@ -1125,11 +1201,15 @@ int cros_ec_init(const void *blob, struct cros_ec_dev **cros_ecp)
 	}
 
 	/* Remember this device for use by the cros_ec command */
-	last_dev = *cros_ecp = dev;
+	*cros_ecp = dev;
+#ifndef CONFIG_DM_CROS_EC
+	last_dev = dev;
+#endif
 	debug("Google Chrome EC CROS-EC driver ready, id '%s'\n", id);
 
 	return 0;
 }
+#endif
 
 int cros_ec_decode_region(int argc, char * const argv[])
 {
@@ -1147,15 +1227,10 @@ int cros_ec_decode_region(int argc, char * const argv[])
 	return -1;
 }
 
-int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config)
+int cros_ec_decode_ec_flash(const void *blob, int node,
+			    struct fdt_cros_ec *config)
 {
-	int flash_node, node;
-
-	node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
-	if (node < 0) {
-		debug("Failed to find chrome-ec node'\n");
-		return -1;
-	}
+	int flash_node;
 
 	flash_node = fdt_subnode_offset(blob, node, "flash");
 	if (flash_node < 0) {
@@ -1516,7 +1591,10 @@ static int cros_ec_i2c_passthrough(struct cros_ec_dev *dev, int flag,
 
 static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	struct cros_ec_dev *dev = last_dev;
+	struct cros_ec_dev *dev;
+#ifdef CONFIG_DM_CROS_EC
+	struct udevice *udev;
+#endif
 	const char *cmd;
 	int ret = 0;
 
@@ -1525,19 +1603,31 @@ static int do_cros_ec(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 	cmd = argv[1];
 	if (0 == strcmp("init", cmd)) {
+#ifndef CONFIG_DM_CROS_EC
 		ret = cros_ec_init(gd->fdt_blob, &dev);
 		if (ret) {
 			printf("Could not init cros_ec device (err %d)\n", ret);
 			return 1;
 		}
+#endif
 		return 0;
 	}
 
+#ifdef CONFIG_DM_CROS_EC
+	ret = uclass_get_device(UCLASS_CROS_EC, 0, &udev);
+	if (ret) {
+		printf("Cannot get cros-ec device (err=%d)\n", ret);
+		return 1;
+	}
+	dev = udev->uclass_priv;
+#else
 	/* Just use the last allocated device; there should be only one */
 	if (!last_dev) {
 		printf("No CROS-EC device available\n");
 		return 1;
 	}
+	dev = last_dev;
+#endif
 	if (0 == strcmp("id", cmd)) {
 		char id[MSG_BYTES];
 
@@ -1794,3 +1884,11 @@ U_BOOT_CMD(
 	"crosec i2c mw chip address[.0, .1, .2] value [count] - write to I2C passthru (fill)"
 );
 #endif
+
+#ifdef CONFIG_DM_CROS_EC
+UCLASS_DRIVER(cros_ec) = {
+	.id		= UCLASS_CROS_EC,
+	.name		= "cros_ec",
+	.per_device_auto_alloc_size = sizeof(struct cros_ec_dev),
+};
+#endif
diff --git a/drivers/misc/cros_ec_sandbox.c b/drivers/misc/cros_ec_sandbox.c
index 8a04af5..431cf26 100644
--- a/drivers/misc/cros_ec_sandbox.c
+++ b/drivers/misc/cros_ec_sandbox.c
@@ -525,8 +525,13 @@ int cros_ec_sandbox_init(struct cros_ec_dev *dev, const void *blob)
 	int node;
 	int err;
 
-	state = &s_state;
-	err = cros_ec_decode_ec_flash(blob, &ec->ec_config);
+	node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC);
+	if (node < 0) {
+		debug("Failed to find chrome-ec node'\n");
+		return -1;
+	}
+
+	err = cros_ec_decode_ec_flash(blob, node, &ec->ec_config);
 	if (err)
 		return err;
 
diff --git a/include/cros_ec.h b/include/cros_ec.h
index 1e4d8db..9e13146 100644
--- a/include/cros_ec.h
+++ b/include/cros_ec.h
@@ -14,6 +14,7 @@
 #include <fdtdec.h>
 #include <cros_ec_message.h>
 
+#ifndef CONFIG_DM_CROS_EC
 /* Which interface is the device on? */
 enum cros_ec_interface_t {
 	CROS_EC_IF_NONE,
@@ -22,9 +23,13 @@ enum cros_ec_interface_t {
 	CROS_EC_IF_LPC,	/* Intel Low Pin Count interface */
 	CROS_EC_IF_SANDBOX,
 };
+#endif
 
 /* Our configuration information */
 struct cros_ec_dev {
+#ifdef CONFIG_DM_CROS_EC
+	struct udevice *dev;		/* Transport device */
+#else
 	enum cros_ec_interface_t interface;
 	struct spi_slave *spi;		/* Our SPI slave, if using SPI */
 	int node;                       /* Our node */
@@ -33,6 +38,7 @@ struct cros_ec_dev {
 	unsigned int addr;		/* Device address (for I2C) */
 	unsigned int bus_num;		/* Bus number (for I2C) */
 	unsigned int max_frequency;	/* Maximum interface frequency */
+#endif
 	struct fdt_gpio_state ec_int;	/* GPIO used as EC interrupt line */
 	int protocol_version;           /* Protocol version to use */
 	int optimise_flash_write;	/* Don't write erased flash blocks */
@@ -233,6 +239,22 @@ int cros_ec_flash_update_rw(struct cros_ec_dev *dev,
  */
 struct cros_ec_dev *board_get_cros_ec_dev(void);
 
+#ifdef CONFIG_DM_CROS_EC
+
+struct dm_cros_ec_ops {
+	int (*check_version)(struct udevice *dev);
+	int (*command)(struct udevice *dev, uint8_t cmd, int cmd_version,
+		       const uint8_t *dout, int dout_len,
+		       uint8_t **dinp, int din_len);
+	int (*packet)(struct udevice *dev, int out_bytes, int in_bytes);
+};
+
+#define dm_cros_ec_get_ops(dev) \
+		((struct dm_cros_ec_ops *)(dev)->driver->ops)
+
+int cros_ec_register(struct udevice *dev);
+
+#else /* !CONFIG_DM_CROS_EC */
 
 /* Internal interfaces */
 int cros_ec_i2c_init(struct cros_ec_dev *dev, const void *blob);
@@ -336,6 +358,7 @@ int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes);
 int cros_ec_sandbox_packet(struct cros_ec_dev *dev, int out_bytes,
 			   int in_bytes);
+#endif
 
 /**
  * Dump a block of data for a command.
@@ -489,9 +512,11 @@ int cros_ec_get_error(void);
  * Returns information from the FDT about the Chrome EC flash
  *
  * @param blob		FDT blob to use
+ * @param node		Node offset to read from
  * @param config	Structure to use to return information
  */
-int cros_ec_decode_ec_flash(const void *blob, struct fdt_cros_ec *config);
+int cros_ec_decode_ec_flash(const void *blob, int node,
+			    struct fdt_cros_ec *config);
 
 /**
  * Check the current keyboard state, in case recovery mode is requested.
diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
index 02aee45..39c8777 100644
--- a/include/dm/uclass-id.h
+++ b/include/dm/uclass-id.h
@@ -25,6 +25,7 @@ enum uclass_id {
 	UCLASS_SERIAL,		/* Serial UART */
 	UCLASS_SPI,		/* SPI bus */
 	UCLASS_SPI_FLASH,	/* SPI flash */
+	UCLASS_CROS_EC,	/* Chrome OS EC */
 
 	UCLASS_COUNT,
 	UCLASS_INVALID = -1,
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 24/25] dm: sandbox: cros_ec: Move sandbox cros_ec to driver module
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (22 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 23/25] dm: cros_ec: Add support for driver model Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-07-15  0:56 ` [U-Boot] [PATCH 25/25] dm: exynos: cros_ec: Move cros_ec_spi to driver model Simon Glass
  2014-08-09 21:29 ` [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Adjust the sandbox cros_ec emulation driver to work with driver model, and
switch over to driver model for sandbox cros_ec.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/misc/cros_ec_sandbox.c | 90 +++++++++++++++++++++++++++++++++++++++---
 include/configs/sandbox.h      |  1 +
 2 files changed, 86 insertions(+), 5 deletions(-)

diff --git a/drivers/misc/cros_ec_sandbox.c b/drivers/misc/cros_ec_sandbox.c
index 431cf26..99cc529 100644
--- a/drivers/misc/cros_ec_sandbox.c
+++ b/drivers/misc/cros_ec_sandbox.c
@@ -8,6 +8,7 @@
 
 #include <common.h>
 #include <cros_ec.h>
+#include <dm.h>
 #include <ec_commands.h>
 #include <errno.h>
 #include <hash.h>
@@ -85,7 +86,7 @@ struct ec_state {
 	struct ec_keymatrix_entry *matrix;	/* the key matrix info */
 	uint8_t keyscan[KEYBOARD_COLS];
 	bool recovery_req;
-} s_state, *state;
+} s_state, *g_state;
 
 /**
  * cros_ec_read_state() - read the sandbox EC state from the state file
@@ -138,7 +139,7 @@ static int cros_ec_read_state(const void *blob, int node)
  */
 static int cros_ec_write_state(void *blob, int node)
 {
-	struct ec_state *ec = &s_state;
+	struct ec_state *ec = g_state;
 
 	/* We are guaranteed enough space to write basic properties */
 	fdt_setprop_u32(blob, node, "current-image", ec->current_image);
@@ -369,7 +370,7 @@ static int process_cmd(struct ec_state *ec,
 		struct fmap_entry *entry;
 		int ret, size;
 
-		entry = &state->ec_config.region[EC_FLASH_REGION_RW];
+		entry = &ec->ec_config.region[EC_FLASH_REGION_RW];
 
 		switch (req->cmd) {
 		case EC_VBOOT_HASH_RECALC:
@@ -426,7 +427,7 @@ static int process_cmd(struct ec_state *ec,
 		case EC_FLASH_REGION_RO:
 		case EC_FLASH_REGION_RW:
 		case EC_FLASH_REGION_WP_RO:
-			entry = &state->ec_config.region[req->region];
+			entry = &ec->ec_config.region[req->region];
 			resp->offset = entry->offset;
 			resp->size = entry->length;
 			len = sizeof(*resp);
@@ -466,16 +467,24 @@ static int process_cmd(struct ec_state *ec,
 	return len;
 }
 
+#ifdef CONFIG_DM_CROS_EC
+int cros_ec_sandbox_packet(struct udevice *udev, int out_bytes, int in_bytes)
+{
+	struct cros_ec_dev *dev = udev->uclass_priv;
+	struct ec_state *ec = dev_get_priv(dev->dev);
+#else
 int cros_ec_sandbox_packet(struct cros_ec_dev *dev, int out_bytes,
 			   int in_bytes)
 {
+	struct ec_state *ec = &s_state;
+#endif
 	struct ec_host_request *req_hdr = (struct ec_host_request *)dev->dout;
 	const void *req_data = req_hdr + 1;
 	struct ec_host_response *resp_hdr = (struct ec_host_response *)dev->din;
 	void *resp_data = resp_hdr + 1;
 	int len;
 
-	len = process_cmd(&s_state, req_hdr, req_data, resp_hdr, resp_data);
+	len = process_cmd(ec, req_hdr, req_data, resp_hdr, resp_data);
 	if (len < 0)
 		return len;
 
@@ -498,7 +507,11 @@ int cros_ec_sandbox_decode_fdt(struct cros_ec_dev *dev, const void *blob)
 
 void cros_ec_check_keyboard(struct cros_ec_dev *dev)
 {
+#ifdef CONFIG_DM_CROS_EC
+	struct ec_state *ec = dev_get_priv(dev->dev);
+#else
 	struct ec_state *ec = &s_state;
+#endif
 	ulong start;
 
 	printf("Press keys for EC to detect on reset (ESC=recovery)...");
@@ -512,6 +525,52 @@ void cros_ec_check_keyboard(struct cros_ec_dev *dev)
 	}
 }
 
+#ifdef CONFIG_DM_CROS_EC
+int cros_ec_probe(struct udevice *dev)
+{
+	struct ec_state *ec = dev->priv;
+	struct cros_ec_dev *cdev = dev->uclass_priv;
+	const void *blob = gd->fdt_blob;
+	int node;
+	int err;
+
+	memcpy(ec, &s_state, sizeof(*ec));
+	err = cros_ec_decode_ec_flash(blob, dev->of_offset, &ec->ec_config);
+	if (err)
+		return err;
+
+	node = fdtdec_next_compatible(blob, 0, COMPAT_GOOGLE_CROS_EC_KEYB);
+	if (node < 0) {
+		debug("%s: No cros_ec keyboard found\n", __func__);
+	} else if (keyscan_read_fdt_matrix(ec, blob, node)) {
+		debug("%s: Could not read key matrix\n", __func__);
+		return -1;
+	}
+
+	/* If we loaded EC data, check that the length matches */
+	if (ec->flash_data &&
+	    ec->flash_data_len != ec->ec_config.flash.length) {
+		printf("EC data length is %x, expected %x, discarding data\n",
+		       ec->flash_data_len, ec->ec_config.flash.length);
+		os_free(ec->flash_data);
+		ec->flash_data = NULL;
+	}
+
+	/* Otherwise allocate the memory */
+	if (!ec->flash_data) {
+		ec->flash_data_len = ec->ec_config.flash.length;
+		ec->flash_data = os_malloc(ec->flash_data_len);
+		if (!ec->flash_data)
+			return -ENOMEM;
+	}
+
+	cdev->dev = dev;
+	g_state = ec;
+	return cros_ec_register(dev);
+}
+
+#else
+
 /**
  * Initialize sandbox EC emulation.
  *
@@ -562,3 +621,24 @@ int cros_ec_sandbox_init(struct cros_ec_dev *dev, const void *blob)
 
 	return 0;
 }
+#endif
+
+#ifdef CONFIG_DM_CROS_EC
+struct dm_cros_ec_ops cros_ec_ops = {
+	.packet = cros_ec_sandbox_packet,
+};
+
+static const struct udevice_id cros_ec_ids[] = {
+	{ .compatible = "google,cros-ec" },
+	{ }
+};
+
+U_BOOT_DRIVER(cros_ec_sandbox) = {
+	.name		= "cros_ec",
+	.id		= UCLASS_CROS_EC,
+	.of_match	= cros_ec_ids,
+	.probe		= cros_ec_probe,
+	.priv_auto_alloc_size = sizeof(struct ec_state),
+	.ops		= &cros_ec_ops,
+};
+#endif
diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
index 2a87585..f0d79a5 100644
--- a/include/configs/sandbox.h
+++ b/include/configs/sandbox.h
@@ -32,6 +32,7 @@
 #define CONFIG_DM_GPIO
 #define CONFIG_DM_TEST
 #define CONFIG_DM_SERIAL
+#define CONFIG_DM_CROS_EC
 
 #define CONFIG_SYS_STDIO_DEREGISTER
 
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 25/25] dm: exynos: cros_ec: Move cros_ec_spi to driver model
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (23 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 24/25] dm: sandbox: cros_ec: Move sandbox cros_ec to driver module Simon Glass
@ 2014-07-15  0:56 ` Simon Glass
  2014-08-09 21:29 ` [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
  25 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-15  0:56 UTC (permalink / raw)
  To: u-boot

Adjust this driver to use driver model and move smdk5420 boards over to
use it.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 drivers/misc/cros_ec_spi.c | 68 ++++++++++++++++++++++++++++++++++++++++------
 include/configs/smdk5420.h |  1 +
 2 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/drivers/misc/cros_ec_spi.c b/drivers/misc/cros_ec_spi.c
index 015333f..89616ce 100644
--- a/drivers/misc/cros_ec_spi.c
+++ b/drivers/misc/cros_ec_spi.c
@@ -15,23 +15,34 @@
 
 #include <common.h>
 #include <cros_ec.h>
+#include <dm.h>
+#include <errno.h>
 #include <spi.h>
 
+DECLARE_GLOBAL_DATA_PTR;
+
+#ifdef CONFIG_DM_CROS_EC
+int cros_ec_spi_packet(struct udevice *udev, int out_bytes, int in_bytes)
+{
+	struct cros_ec_dev *dev = udev->uclass_priv;
+#else
 int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes)
 {
+#endif
+	struct spi_slave *slave = dev_get_parentdata(dev->dev);
 	int rv;
 
 	/* Do the transfer */
-	if (spi_claim_bus(dev->spi)) {
+	if (spi_claim_bus(slave)) {
 		debug("%s: Cannot claim SPI bus\n", __func__);
 		return -1;
 	}
 
-	rv = spi_xfer(dev->spi, max(out_bytes, in_bytes) * 8,
+	rv = spi_xfer(slave, max(out_bytes, in_bytes) * 8,
 		      dev->dout, dev->din,
 		      SPI_XFER_BEGIN | SPI_XFER_END);
 
-	spi_release_bus(dev->spi);
+	spi_release_bus(slave);
 
 	if (rv) {
 		debug("%s: Cannot complete SPI transfer\n", __func__);
@@ -56,10 +67,19 @@ int cros_ec_spi_packet(struct cros_ec_dev *dev, int out_bytes, int in_bytes)
  * @param din_len	Maximum size of response in bytes
  * @return number of bytes in response, or -1 on error
  */
+#ifdef CONFIG_DM_CROS_EC
+int cros_ec_spi_command(struct udevice *udev, uint8_t cmd, int cmd_version,
+		     const uint8_t *dout, int dout_len,
+		     uint8_t **dinp, int din_len)
+{
+	struct cros_ec_dev *dev = udev->uclass_priv;
+#else
 int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 		     const uint8_t *dout, int dout_len,
 		     uint8_t **dinp, int din_len)
 {
+#endif
+	struct spi_slave *slave = dev_get_parentdata(dev->dev);
 	int in_bytes = din_len + 4;	/* status, length, checksum, trailer */
 	uint8_t *out;
 	uint8_t *p;
@@ -92,7 +112,7 @@ int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 	 */
 	memset(dev->din, '\0', in_bytes);
 
-	if (spi_claim_bus(dev->spi)) {
+	if (spi_claim_bus(slave)) {
 		debug("%s: Cannot claim SPI bus\n", __func__);
 		return -1;
 	}
@@ -113,10 +133,10 @@ int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 	p = dev->din + sizeof(int64_t) - 2;
 	len = dout_len + 4;
 	cros_ec_dump_data("out", cmd, out, len);
-	rv = spi_xfer(dev->spi, max(len, in_bytes) * 8, out, p,
+	rv = spi_xfer(slave, max(len, in_bytes) * 8, out, p,
 		      SPI_XFER_BEGIN | SPI_XFER_END);
 
-	spi_release_bus(dev->spi);
+	spi_release_bus(slave);
 
 	if (rv) {
 		debug("%s: Cannot complete SPI transfer\n", __func__);
@@ -146,6 +166,7 @@ int cros_ec_spi_command(struct cros_ec_dev *dev, uint8_t cmd, int cmd_version,
 	return len;
 }
 
+#ifndef CONFIG_DM_CROS_EC
 int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
 {
 	/* Decode interface-specific FDT params */
@@ -165,11 +186,40 @@ int cros_ec_spi_decode_fdt(struct cros_ec_dev *dev, const void *blob)
  */
 int cros_ec_spi_init(struct cros_ec_dev *dev, const void *blob)
 {
-	dev->spi = spi_setup_slave_fdt(blob, dev->node, dev->parent_node);
-	if (!dev->spi) {
+	int ret;
+
+	ret = spi_setup_slave_fdt(blob, dev->node, dev->parent_node,
+				  &slave);
+	if (ret) {
 		debug("%s: Could not setup SPI slave\n", __func__);
-		return -1;
+		return ret;
 	}
 
 	return 0;
 }
+#endif
+
+#ifdef CONFIG_DM_CROS_EC
+int cros_ec_probe(struct udevice *dev)
+{
+	return cros_ec_register(dev);
+}
+
+struct dm_cros_ec_ops cros_ec_ops = {
+	.packet = cros_ec_spi_packet,
+	.command = cros_ec_spi_command,
+};
+
+static const struct udevice_id cros_ec_ids[] = {
+	{ .compatible = "google,cros-ec" },
+	{ }
+};
+
+U_BOOT_DRIVER(cros_ec_spi) = {
+	.name		= "cros_ec",
+	.id		= UCLASS_CROS_EC,
+	.of_match	= cros_ec_ids,
+	.probe		= cros_ec_probe,
+	.ops		= &cros_ec_ops,
+};
+#endif
diff --git a/include/configs/smdk5420.h b/include/configs/smdk5420.h
index fe8d8ce..1f120cd 100644
--- a/include/configs/smdk5420.h
+++ b/include/configs/smdk5420.h
@@ -26,5 +26,6 @@
 
 #define CONFIG_POWER_TPS65090_EC
 #define CONFIG_CROS_EC_SPI		/* Support CROS_EC over SPI */
+#define CONFIG_DM_CROS_EC
 
 #endif	/* __CONFIG_SMDK5420_H */
-- 
2.0.0.526.g5318336

^ permalink raw reply related	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-15  0:56 ` [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI Simon Glass
@ 2014-07-15  8:26   ` Pavel Herrmann
  2014-07-17  5:39     ` Simon Glass
  2014-08-11 21:46   ` Daniel Schwierzeck
  2014-08-28  8:58   ` Jagan Teki
  2 siblings, 1 reply; 51+ messages in thread
From: Pavel Herrmann @ 2014-07-15  8:26 UTC (permalink / raw)
  To: u-boot

Hi

On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
> Add a uclass which provides access to SPI buses and includes operations
> required by SPI.
> 
> For a time driver model will need to co-exist with the legacy SPI interface
> so some parts of the header file are changed depending on which is in use.
> The exports are adjusted also since some functions are not available with
> driver model.
> 
> Boards must define CONFIG_DM_SPI to use driver model for SPI.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
> ...
> +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> +	     const void *dout, void *din, unsigned long flags)
> +{
> +	struct udevice *dev = slave->dev;
> +	struct udevice *bus = dev->parent;

is this the best interface here?
I think it would be cleaner if bus drivers had interfaces which follow a 
certain template, such as
bus_ops(struct udevice *bus, struct udevice *child, ...)

struct spi_slave would be a prime candidate to have in child->parentdata 
(which should only be accessed by the parent IIUC)

regards
Pavel Herrmann

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-15  8:26   ` Pavel Herrmann
@ 2014-07-17  5:39     ` Simon Glass
  2014-07-17  7:57       ` Pavel Herrmann
  0 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-17  5:39 UTC (permalink / raw)
  To: u-boot

Hi Pavel,


On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
>
> Hi
>
> On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
> > Add a uclass which provides access to SPI buses and includes operations
> > required by SPI.
> >
> > For a time driver model will need to co-exist with the legacy SPI interface
> > so some parts of the header file are changed depending on which is in use.
> > The exports are adjusted also since some functions are not available with
> > driver model.
> >
> > Boards must define CONFIG_DM_SPI to use driver model for SPI.
> >
> > Signed-off-by: Simon Glass <sjg@chromium.org>
> > ---
> >
> > ...
> > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> > +          const void *dout, void *din, unsigned long flags)
> > +{
> > +     struct udevice *dev = slave->dev;
> > +     struct udevice *bus = dev->parent;
>
> is this the best interface here?
> I think it would be cleaner if bus drivers had interfaces which follow a
> certain template, such as
> bus_ops(struct udevice *bus, struct udevice *child, ...)

Thanks for your comments.

Well I thought about that long and hard. Clearly in a pure
driver-model work we would pass a udevice, not a spi_slave.

>
> struct spi_slave would be a prime candidate to have in child->parentdata
> (which should only be accessed by the parent IIUC)

Yes, it is. In other words, 'struct spi_slave' is the child's parent
data. The only reason that spi_xfer() is passed a spi_slave rather
than a udevice is to maintain compatibility with the existing SPI
system. I tried various other approachs, such as '#define spi_slave
udevice' and the like. At some point we can change it, but it is
really painful to have two completely different APIs co-existing in
U-Boot.

This way, driver model becomes a fairly soft transition, the amount of
rewriting needed is reduced, and I think it is much more likely that
people will use it. As an example, one of the reasons that the generic
board change has been relatively painless is that people can just
define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
most cases it just works. If we require people to rewrite things it
will take longer, etc. There is already enough rewriting required in
individual drivers to keep people busy. It will be a relatively easy
change to do in the future if we want to.

Re passing both the bus and the device, really, what's the point? The
only valid bus to pass to the function is dev->parent. If you pass
anything else it is an error. It is redundant and therefore just
introduces the possibility of error.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-17  5:39     ` Simon Glass
@ 2014-07-17  7:57       ` Pavel Herrmann
  2014-07-17 15:26         ` Simon Glass
  0 siblings, 1 reply; 51+ messages in thread
From: Pavel Herrmann @ 2014-07-17  7:57 UTC (permalink / raw)
  To: u-boot

Hi

On Wednesday 16 of July 2014 23:39:44 Simon Glass wrote:
> Hi Pavel,
> 
> On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> > Hi
> > 
> > On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
> > > Add a uclass which provides access to SPI buses and includes operations
> > > required by SPI.
> > > 
> > > For a time driver model will need to co-exist with the legacy SPI
> > > interface
> > > so some parts of the header file are changed depending on which is in
> > > use.
> > > The exports are adjusted also since some functions are not available
> > > with
> > > driver model.
> > > 
> > > Boards must define CONFIG_DM_SPI to use driver model for SPI.
> > > 
> > > Signed-off-by: Simon Glass <sjg@chromium.org>
> > > ---
> > > 
> > > ...
> > > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> > > +          const void *dout, void *din, unsigned long flags)
> > > +{
> > > +     struct udevice *dev = slave->dev;
> > > +     struct udevice *bus = dev->parent;
> > 
> > is this the best interface here?
> > I think it would be cleaner if bus drivers had interfaces which follow a
> > certain template, such as
> > bus_ops(struct udevice *bus, struct udevice *child, ...)
> 
> Thanks for your comments.
> 
> Well I thought about that long and hard. Clearly in a pure
> driver-model work we would pass a udevice, not a spi_slave.
> 
> > struct spi_slave would be a prime candidate to have in child->parentdata
> > (which should only be accessed by the parent IIUC)
> 
> Yes, it is. In other words, 'struct spi_slave' is the child's parent
> data. The only reason that spi_xfer() is passed a spi_slave rather
> than a udevice is to maintain compatibility with the existing SPI
> system. I tried various other approachs, such as '#define spi_slave
> udevice' and the like. At some point we can change it, but it is
> really painful to have two completely different APIs co-existing in
> U-Boot.
>
> This way, driver model becomes a fairly soft transition, the amount of
> rewriting needed is reduced, and I think it is much more likely that
> people will use it. As an example, one of the reasons that the generic
> board change has been relatively painless is that people can just
> define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
> most cases it just works. If we require people to rewrite things it
> will take longer, etc. There is already enough rewriting required in
> individual drivers to keep people busy. It will be a relatively easy
> change to do in the future if we want to.

OK, that reason I understand.

However, what you are doing now is limiting what parentdata a SPI bus 
controller can use.
This means that each bus driver has its parentdata defined by what uclass it 
belongs to. Are you sure this won't be a limitation? I mean that you could end 
up with uclasses that have the same calls but a slightly different parentdata.

Yes, you could have a shared parentdata from the uclass (that makes sense for 
all controllers, because of the way the bus works), and a controller-specific 
parentdata as an extension (read "void *private" at the end of the parentdata 
structure)

> Re passing both the bus and the device, really, what's the point? The
> only valid bus to pass to the function is dev->parent. If you pass
> anything else it is an error. It is redundant and therefore just
> introduces the possibility of error.

Well, yes, sort of.

Consider a bus with transparent bridges, like USB or PCI.

If you were to represent these bridges as udevices, you would end up with 
something in between the actual devices and the bus controller, that forwards 
requests with no or minimal change.
in case of USB specifically (IIRC), hubs are visible during initialization, but 
when the device is up it has its own descriptor that is used to 
in case of PCI, the device address actually contains the bus number, but the 
device itself doesn't really care about it, and is only used to select which 
bus the command goes to.

consider the following scenario:
------     ----------     ---------     ---------     ------------
|root| --- |ehci_hcd| --- |usb_hub| --- |usb_hub| --- |usb_device|
------     ----------     ---------     ---------     ------------

If you were to flatten the bus, the udevice tree would not really correspond to 
how the bus looks like in reality, and it might give you some hurdles with 
initialization.

note that in these cases you cannot pass a child udevice to the bus controller 
as a part of the upcall, since it is not necessarily its child. this is in 
contradiction to what I wrote previously, simply because I didn't think hard 
enough to find these counter examples.

regards
Pavel Herrmann

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-17  7:57       ` Pavel Herrmann
@ 2014-07-17 15:26         ` Simon Glass
  2014-07-17 18:01           ` Pavel Herrmann
  0 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-07-17 15:26 UTC (permalink / raw)
  To: u-boot

Hi Pavel,

On 17 July 2014 01:57, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> Hi
>
> On Wednesday 16 of July 2014 23:39:44 Simon Glass wrote:
>> Hi Pavel,
>>
>> On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
>> > Hi
>> >
>> > On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
>> > > Add a uclass which provides access to SPI buses and includes operations
>> > > required by SPI.
>> > >
>> > > For a time driver model will need to co-exist with the legacy SPI
>> > > interface
>> > > so some parts of the header file are changed depending on which is in
>> > > use.
>> > > The exports are adjusted also since some functions are not available
>> > > with
>> > > driver model.
>> > >
>> > > Boards must define CONFIG_DM_SPI to use driver model for SPI.
>> > >
>> > > Signed-off-by: Simon Glass <sjg@chromium.org>
>> > > ---
>> > >
>> > > ...
>> > > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
>> > > +          const void *dout, void *din, unsigned long flags)
>> > > +{
>> > > +     struct udevice *dev = slave->dev;
>> > > +     struct udevice *bus = dev->parent;
>> >
>> > is this the best interface here?
>> > I think it would be cleaner if bus drivers had interfaces which follow a
>> > certain template, such as
>> > bus_ops(struct udevice *bus, struct udevice *child, ...)
>>
>> Thanks for your comments.
>>
>> Well I thought about that long and hard. Clearly in a pure
>> driver-model work we would pass a udevice, not a spi_slave.
>>
>> > struct spi_slave would be a prime candidate to have in child->parentdata
>> > (which should only be accessed by the parent IIUC)
>>
>> Yes, it is. In other words, 'struct spi_slave' is the child's parent
>> data. The only reason that spi_xfer() is passed a spi_slave rather
>> than a udevice is to maintain compatibility with the existing SPI
>> system. I tried various other approachs, such as '#define spi_slave
>> udevice' and the like. At some point we can change it, but it is
>> really painful to have two completely different APIs co-existing in
>> U-Boot.
>>
>> This way, driver model becomes a fairly soft transition, the amount of
>> rewriting needed is reduced, and I think it is much more likely that
>> people will use it. As an example, one of the reasons that the generic
>> board change has been relatively painless is that people can just
>> define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
>> most cases it just works. If we require people to rewrite things it
>> will take longer, etc. There is already enough rewriting required in
>> individual drivers to keep people busy. It will be a relatively easy
>> change to do in the future if we want to.
>
> OK, that reason I understand.
>
> However, what you are doing now is limiting what parentdata a SPI bus
> controller can use.
> This means that each bus driver has its parentdata defined by what uclass it
> belongs to. Are you sure this won't be a limitation? I mean that you could end
> up with uclasses that have the same calls but a slightly different parentdata.

No it is defined by the driver of the bus. Since it is possible to
have (for example) 3 different USB drivers in a system, each can have
its own idea of what child data it wants. I definitely agree that
setting the parentdata by the child's uclass, or even the bus's uclass
would be limiting. In the case of SPI I have elected to use struct
spi_slave for reasons I explained earlier.

>
> Yes, you could have a shared parentdata from the uclass (that makes sense for
> all controllers, because of the way the bus works), and a controller-specific
> parentdata as an extension (read "void *private" at the end of the parentdata
> structure)
>
>> Re passing both the bus and the device, really, what's the point? The
>> only valid bus to pass to the function is dev->parent. If you pass
>> anything else it is an error. It is redundant and therefore just
>> introduces the possibility of error.
>
> Well, yes, sort of.
>
> Consider a bus with transparent bridges, like USB or PCI.
>
> If you were to represent these bridges as udevices, you would end up with
> something in between the actual devices and the bus controller, that forwards
> requests with no or minimal change.
> in case of USB specifically (IIRC), hubs are visible during initialization, but
> when the device is up it has its own descriptor that is used to
> in case of PCI, the device address actually contains the bus number, but the
> device itself doesn't really care about it, and is only used to select which
> bus the command goes to.
>
> consider the following scenario:
> ------     ----------     ---------     ---------     ------------
> |root| --- |ehci_hcd| --- |usb_hub| --- |usb_hub| --- |usb_device|
> ------     ----------     ---------     ---------     ------------
>
> If you were to flatten the bus, the udevice tree would not really correspond to
> how the bus looks like in reality, and it might give you some hurdles with
> initialization.
>
> note that in these cases you cannot pass a child udevice to the bus controller
> as a part of the upcall, since it is not necessarily its child. this is in
> contradiction to what I wrote previously, simply because I didn't think hard
> enough to find these counter examples.

I think you are referring to setting up a bus such that it becomes
transparent. But even when it is, I'm not sure that driver model needs
to rearrange its data structures to suit. Why would you flatten the
bus? If we keep the data structures the same (strict parent-child
relationships) then we always have a parent for the child, rather than
trying to be too clever. Still, the child can obtain direct bus access
through some mechanism delegated by the parent if we like. In that
case there is even less reason to access the parent udevice IMO.

I think I'm missing something from your example, so please let me know.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-17 15:26         ` Simon Glass
@ 2014-07-17 18:01           ` Pavel Herrmann
  2014-07-17 18:29             ` Pavel Herrmann
  2014-07-21  2:15             ` Simon Glass
  0 siblings, 2 replies; 51+ messages in thread
From: Pavel Herrmann @ 2014-07-17 18:01 UTC (permalink / raw)
  To: u-boot

Hi

On Thursday 17 of July 2014 09:26:47 Simon Glass wrote:
> Hi Pavel,
> 
> On 17 July 2014 01:57, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> > Hi
> > 
> > On Wednesday 16 of July 2014 23:39:44 Simon Glass wrote:
> >> Hi Pavel,
> >> 
> >> On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> >> > Hi
> >> > 
> >> > On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
> >> > > Add a uclass which provides access to SPI buses and includes
> >> > > operations
> >> > > required by SPI.
> >> > > 
> >> > > For a time driver model will need to co-exist with the legacy SPI
> >> > > interface
> >> > > so some parts of the header file are changed depending on which is in
> >> > > use.
> >> > > The exports are adjusted also since some functions are not available
> >> > > with
> >> > > driver model.
> >> > > 
> >> > > Boards must define CONFIG_DM_SPI to use driver model for SPI.
> >> > > 
> >> > > Signed-off-by: Simon Glass <sjg@chromium.org>
> >> > > ---
> >> > > 
> >> > > ...
> >> > > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> >> > > +          const void *dout, void *din, unsigned long flags)
> >> > > +{
> >> > > +     struct udevice *dev = slave->dev;
> >> > > +     struct udevice *bus = dev->parent;
> >> > 
> >> > is this the best interface here?
> >> > I think it would be cleaner if bus drivers had interfaces which follow
> >> > a
> >> > certain template, such as
> >> > bus_ops(struct udevice *bus, struct udevice *child, ...)
> >> 
> >> Thanks for your comments.
> >> 
> >> Well I thought about that long and hard. Clearly in a pure
> >> driver-model work we would pass a udevice, not a spi_slave.
> >> 
> >> > struct spi_slave would be a prime candidate to have in
> >> > child->parentdata
> >> > (which should only be accessed by the parent IIUC)
> >> 
> >> Yes, it is. In other words, 'struct spi_slave' is the child's parent
> >> data. The only reason that spi_xfer() is passed a spi_slave rather
> >> than a udevice is to maintain compatibility with the existing SPI
> >> system. I tried various other approachs, such as '#define spi_slave
> >> udevice' and the like. At some point we can change it, but it is
> >> really painful to have two completely different APIs co-existing in
> >> U-Boot.
> >> 
> >> This way, driver model becomes a fairly soft transition, the amount of
> >> rewriting needed is reduced, and I think it is much more likely that
> >> people will use it. As an example, one of the reasons that the generic
> >> board change has been relatively painless is that people can just
> >> define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
> >> most cases it just works. If we require people to rewrite things it
> >> will take longer, etc. There is already enough rewriting required in
> >> individual drivers to keep people busy. It will be a relatively easy
> >> change to do in the future if we want to.
> > 
> > OK, that reason I understand.
> > 
> > However, what you are doing now is limiting what parentdata a SPI bus
> > controller can use.
> > This means that each bus driver has its parentdata defined by what uclass
> > it belongs to. Are you sure this won't be a limitation? I mean that you
> > could end up with uclasses that have the same calls but a slightly
> > different parentdata.
> No it is defined by the driver of the bus. Since it is possible to
> have (for example) 3 different USB drivers in a system, each can have
> its own idea of what child data it wants. I definitely agree that
> setting the parentdata by the child's uclass, or even the bus's uclass
> would be limiting. In the case of SPI I have elected to use struct
> spi_slave for reasons I explained earlier.

OK, so you would have some bus uclasses that pass typecasted parentdata, 
because it is the same for all bus drivers, and some that pass the childs 
udevice*, because the parentdata type is not known beforehands? what happens 
if suddenly you need to add a bus controller that needs a little more per-
child data than the existing ones?

wouldn't it be better to unify this somehow?

> > Yes, you could have a shared parentdata from the uclass (that makes sense
> > for all controllers, because of the way the bus works), and a
> > controller-specific parentdata as an extension (read "void *private" at
> > the end of the parentdata structure)
> > 
> >> Re passing both the bus and the device, really, what's the point? The
> >> only valid bus to pass to the function is dev->parent. If you pass
> >> anything else it is an error. It is redundant and therefore just
> >> introduces the possibility of error.
> > 
> > Well, yes, sort of.
> > 
> > Consider a bus with transparent bridges, like USB or PCI.
> > 
> > If you were to represent these bridges as udevices, you would end up with
> > something in between the actual devices and the bus controller, that
> > forwards requests with no or minimal change.
> > in case of USB specifically (IIRC), hubs are visible during
> > initialization, but when the device is up it has its own descriptor that
> > is used to
> > in case of PCI, the device address actually contains the bus number, but
> > the device itself doesn't really care about it, and is only used to
> > select which bus the command goes to.
> > 
> > consider the following scenario:
> > ------     ----------     ---------     ---------     ------------
> > 
> > |root| --- |ehci_hcd| --- |usb_hub| --- |usb_hub| --- |usb_device|
> > 
> > ------     ----------     ---------     ---------     ------------
> > 
> > If you were to flatten the bus, the udevice tree would not really
> > correspond to how the bus looks like in reality, and it might give you
> > some hurdles with initialization.
> > 
> > note that in these cases you cannot pass a child udevice to the bus
> > controller as a part of the upcall, since it is not necessarily its
> > child. this is in contradiction to what I wrote previously, simply
> > because I didn't think hard enough to find these counter examples.
> 
> I think you are referring to setting up a bus such that it becomes
> transparent. But even when it is, I'm not sure that driver model needs
> to rearrange its data structures to suit. Why would you flatten the
> bus? If we keep the data structures the same (strict parent-child
> relationships) then we always have a parent for the child, rather than
> trying to be too clever. Still, the child can obtain direct bus access
> through some mechanism delegated by the parent if we like. In that
> case there is even less reason to access the parent udevice IMO.
> 
> I think I'm missing something from your example, so please let me know.

ok, lets try some code examples

lets say a usb_hub is implemented as a USB bus that itself sits in a USB bus.
lets say USB bus uclass has a function usb_bulk_msg()
usb_hub's implementation of such function would look like this:

usb_bulk_msg(udevice *hub, usb_descriptor *child, ...)
{
	return usb_bulk_msg(hub->parent, child, ...);
}

your USB device would then call
usb_bulk_msg(my_dev->parent, my_desc,...)

this would work recursively through all hubs, and the end result you would 
like is to call usb_bulk_msg() of the host bus controller, without changing 
any of the parameters. (except the first, which is used for recursion)

However, if you got rid of the first parameter, you would need a different 
variable to control your recursion, or you would need the device descriptor to 
hold pointer to the udevice of the host controller, which would effectively 
flatten the bus.

this is the case where I believe the best way to implement is to have a shared 
parentdata based on parent uclass (the usb_descriptor in this case), with a 
driver-specific extension, in case the bus driver need something extra over 
what the "standard" is. the shared parentdata would then be used as a device 
descriptor/address/whatever-you-call-that in the bus calls.

One would also need to relax the semantics a bit, so that host bus controller 
can fill the parentdata for a udevice that is not its direct child.

I'm not saying your code is wrong or anything, I just think you should have a 
look at a more complex bus like USB or PCI, and design the "bus uclass 
interface template" in a way that would support such a bus. Otherwise you 
might end up either reworking your simpler busses later, or having 
inconsistent bus uclass interfaces (which we did when we tried this the first 
time).

I understand that API compatibility is an issue, and I agree that it is fine to 
cut some corners at this point, but we need to keep in mind to fix them once 
all the drivers have been converted.

regards
Pavel

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-17 18:01           ` Pavel Herrmann
@ 2014-07-17 18:29             ` Pavel Herrmann
  2014-07-21  2:17               ` Simon Glass
  2014-07-21  2:15             ` Simon Glass
  1 sibling, 1 reply; 51+ messages in thread
From: Pavel Herrmann @ 2014-07-17 18:29 UTC (permalink / raw)
  To: u-boot

On Thursday 17 of July 2014 20:01:29 Pavel Herrmann wrote:
> Hi
> 
> On Thursday 17 of July 2014 09:26:47 Simon Glass wrote:
> > Hi Pavel,
> > 
> > On 17 July 2014 01:57, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> > > Hi
> > > 
> > > On Wednesday 16 of July 2014 23:39:44 Simon Glass wrote:
> > >> Hi Pavel,
> > >> 
> > >> On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> > >> > Hi
> > >> > 
> > >> > On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
> > >> > > Add a uclass which provides access to SPI buses and includes
> > >> > > operations
> > >> > > required by SPI.
> > >> > > 
> > >> > > For a time driver model will need to co-exist with the legacy SPI
> > >> > > interface
> > >> > > so some parts of the header file are changed depending on which is
> > >> > > in
> > >> > > use.
> > >> > > The exports are adjusted also since some functions are not
> > >> > > available
> > >> > > with
> > >> > > driver model.
> > >> > > 
> > >> > > Boards must define CONFIG_DM_SPI to use driver model for SPI.
> > >> > > 
> > >> > > Signed-off-by: Simon Glass <sjg@chromium.org>
> > >> > > ---
> > >> > > 
> > >> > > ...
> > >> > > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> > >> > > +          const void *dout, void *din, unsigned long flags)
> > >> > > +{
> > >> > > +     struct udevice *dev = slave->dev;
> > >> > > +     struct udevice *bus = dev->parent;
> > >> > 
> > >> > is this the best interface here?
> > >> > I think it would be cleaner if bus drivers had interfaces which
> > >> > follow
> > >> > a
> > >> > certain template, such as
> > >> > bus_ops(struct udevice *bus, struct udevice *child, ...)
> > >> 
> > >> Thanks for your comments.
> > >> 
> > >> Well I thought about that long and hard. Clearly in a pure
> > >> driver-model work we would pass a udevice, not a spi_slave.
> > >> 
> > >> > struct spi_slave would be a prime candidate to have in
> > >> > child->parentdata
> > >> > (which should only be accessed by the parent IIUC)
> > >> 
> > >> Yes, it is. In other words, 'struct spi_slave' is the child's parent
> > >> data. The only reason that spi_xfer() is passed a spi_slave rather
> > >> than a udevice is to maintain compatibility with the existing SPI
> > >> system. I tried various other approachs, such as '#define spi_slave
> > >> udevice' and the like. At some point we can change it, but it is
> > >> really painful to have two completely different APIs co-existing in
> > >> U-Boot.
> > >> 
> > >> This way, driver model becomes a fairly soft transition, the amount of
> > >> rewriting needed is reduced, and I think it is much more likely that
> > >> people will use it. As an example, one of the reasons that the generic
> > >> board change has been relatively painless is that people can just
> > >> define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
> > >> most cases it just works. If we require people to rewrite things it
> > >> will take longer, etc. There is already enough rewriting required in
> > >> individual drivers to keep people busy. It will be a relatively easy
> > >> change to do in the future if we want to.
> > > 
> > > OK, that reason I understand.
> > > 
> > > However, what you are doing now is limiting what parentdata a SPI bus
> > > controller can use.
> > > This means that each bus driver has its parentdata defined by what
> > > uclass
> > > it belongs to. Are you sure this won't be a limitation? I mean that you
> > > could end up with uclasses that have the same calls but a slightly
> > > different parentdata.
> > 
> > No it is defined by the driver of the bus. Since it is possible to
> > have (for example) 3 different USB drivers in a system, each can have
> > its own idea of what child data it wants. I definitely agree that
> > setting the parentdata by the child's uclass, or even the bus's uclass
> > would be limiting. In the case of SPI I have elected to use struct
> > spi_slave for reasons I explained earlier.
> 
> OK, so you would have some bus uclasses that pass typecasted parentdata,
> because it is the same for all bus drivers, and some that pass the childs
> udevice*, because the parentdata type is not known beforehands? what happens
> if suddenly you need to add a bus controller that needs a little more per-
> child data than the existing ones?
> 
> wouldn't it be better to unify this somehow?
> 
> > > Yes, you could have a shared parentdata from the uclass (that makes
> > > sense
> > > for all controllers, because of the way the bus works), and a
> > > controller-specific parentdata as an extension (read "void *private" at
> > > the end of the parentdata structure)
> > > 
> > >> Re passing both the bus and the device, really, what's the point? The
> > >> only valid bus to pass to the function is dev->parent. If you pass
> > >> anything else it is an error. It is redundant and therefore just
> > >> introduces the possibility of error.
> > > 
> > > Well, yes, sort of.
> > > 
> > > Consider a bus with transparent bridges, like USB or PCI.
> > > 
> > > If you were to represent these bridges as udevices, you would end up
> > > with
> > > something in between the actual devices and the bus controller, that
> > > forwards requests with no or minimal change.
> > > in case of USB specifically (IIRC), hubs are visible during
> > > initialization, but when the device is up it has its own descriptor that
> > > is used to
> > > in case of PCI, the device address actually contains the bus number, but
> > > the device itself doesn't really care about it, and is only used to
> > > select which bus the command goes to.
> > > 
> > > consider the following scenario:
> > > ------     ----------     ---------     ---------     ------------
> > > 
> > > |root| --- |ehci_hcd| --- |usb_hub| --- |usb_hub| --- |usb_device|
> > > 
> > > ------     ----------     ---------     ---------     ------------
> > > 
> > > If you were to flatten the bus, the udevice tree would not really
> > > correspond to how the bus looks like in reality, and it might give you
> > > some hurdles with initialization.
> > > 
> > > note that in these cases you cannot pass a child udevice to the bus
> > > controller as a part of the upcall, since it is not necessarily its
> > > child. this is in contradiction to what I wrote previously, simply
> > > because I didn't think hard enough to find these counter examples.
> > 
> > I think you are referring to setting up a bus such that it becomes
> > transparent. But even when it is, I'm not sure that driver model needs
> > to rearrange its data structures to suit. Why would you flatten the
> > bus? If we keep the data structures the same (strict parent-child
> > relationships) then we always have a parent for the child, rather than
> > trying to be too clever. Still, the child can obtain direct bus access
> > through some mechanism delegated by the parent if we like. In that
> > case there is even less reason to access the parent udevice IMO.
> > 
> > I think I'm missing something from your example, so please let me know.
> 
> ok, lets try some code examples
> 
> lets say a usb_hub is implemented as a USB bus that itself sits in a USB
> bus. lets say USB bus uclass has a function usb_bulk_msg()
> usb_hub's implementation of such function would look like this:
> 
> usb_bulk_msg(udevice *hub, usb_descriptor *child, ...)
> {
> 	return usb_bulk_msg(hub->parent, child, ...);
> }
> 
> your USB device would then call
> usb_bulk_msg(my_dev->parent, my_desc,...)
> 
> this would work recursively through all hubs, and the end result you would
> like is to call usb_bulk_msg() of the host bus controller, without changing
> any of the parameters. (except the first, which is used for recursion)
> 
> However, if you got rid of the first parameter, you would need a different
> variable to control your recursion, or you would need the device descriptor
> to hold pointer to the udevice of the host controller, which would
> effectively flatten the bus.
> 
> this is the case where I believe the best way to implement is to have a
> shared parentdata based on parent uclass (the usb_descriptor in this case),
> with a driver-specific extension, in case the bus driver need something
> extra over what the "standard" is. the shared parentdata would then be used
> as a device descriptor/address/whatever-you-call-that in the bus calls.

I would like to add that passing the childs udevice* should work equally as 
well, if you relax the bus == child->parent assumption.

> 
> One would also need to relax the semantics a bit, so that host bus
> controller can fill the parentdata for a udevice that is not its direct
> child.
> 
> I'm not saying your code is wrong or anything, I just think you should have
> a look at a more complex bus like USB or PCI, and design the "bus uclass
> interface template" in a way that would support such a bus. Otherwise you
> might end up either reworking your simpler busses later, or having
> inconsistent bus uclass interfaces (which we did when we tried this the
> first time).
> 
> I understand that API compatibility is an issue, and I agree that it is fine
> to cut some corners at this point, but we need to keep in mind to fix them
> once all the drivers have been converted.
> 
> regards
> Pavel

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-17 18:01           ` Pavel Herrmann
  2014-07-17 18:29             ` Pavel Herrmann
@ 2014-07-21  2:15             ` Simon Glass
  1 sibling, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-21  2:15 UTC (permalink / raw)
  To: u-boot

Hi Pavel,

On 17 July 2014 12:01, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> Hi
>
> On Thursday 17 of July 2014 09:26:47 Simon Glass wrote:
>> Hi Pavel,
>>
>> On 17 July 2014 01:57, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
>> > Hi
>> >
>> > On Wednesday 16 of July 2014 23:39:44 Simon Glass wrote:
>> >> Hi Pavel,
>> >>
>> >> On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
>> >> > Hi
>> >> >
>> >> > On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
>> >> > > Add a uclass which provides access to SPI buses and includes
>> >> > > operations
>> >> > > required by SPI.
>> >> > >
>> >> > > For a time driver model will need to co-exist with the legacy SPI
>> >> > > interface
>> >> > > so some parts of the header file are changed depending on which is in
>> >> > > use.
>> >> > > The exports are adjusted also since some functions are not available
>> >> > > with
>> >> > > driver model.
>> >> > >
>> >> > > Boards must define CONFIG_DM_SPI to use driver model for SPI.
>> >> > >
>> >> > > Signed-off-by: Simon Glass <sjg@chromium.org>
>> >> > > ---
>> >> > >
>> >> > > ...
>> >> > > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
>> >> > > +          const void *dout, void *din, unsigned long flags)
>> >> > > +{
>> >> > > +     struct udevice *dev = slave->dev;
>> >> > > +     struct udevice *bus = dev->parent;
>> >> >
>> >> > is this the best interface here?
>> >> > I think it would be cleaner if bus drivers had interfaces which follow
>> >> > a
>> >> > certain template, such as
>> >> > bus_ops(struct udevice *bus, struct udevice *child, ...)
>> >>
>> >> Thanks for your comments.
>> >>
>> >> Well I thought about that long and hard. Clearly in a pure
>> >> driver-model work we would pass a udevice, not a spi_slave.
>> >>
>> >> > struct spi_slave would be a prime candidate to have in
>> >> > child->parentdata
>> >> > (which should only be accessed by the parent IIUC)
>> >>
>> >> Yes, it is. In other words, 'struct spi_slave' is the child's parent
>> >> data. The only reason that spi_xfer() is passed a spi_slave rather
>> >> than a udevice is to maintain compatibility with the existing SPI
>> >> system. I tried various other approachs, such as '#define spi_slave
>> >> udevice' and the like. At some point we can change it, but it is
>> >> really painful to have two completely different APIs co-existing in
>> >> U-Boot.
>> >>
>> >> This way, driver model becomes a fairly soft transition, the amount of
>> >> rewriting needed is reduced, and I think it is much more likely that
>> >> people will use it. As an example, one of the reasons that the generic
>> >> board change has been relatively painless is that people can just
>> >> define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
>> >> most cases it just works. If we require people to rewrite things it
>> >> will take longer, etc. There is already enough rewriting required in
>> >> individual drivers to keep people busy. It will be a relatively easy
>> >> change to do in the future if we want to.
>> >
>> > OK, that reason I understand.
>> >
>> > However, what you are doing now is limiting what parentdata a SPI bus
>> > controller can use.
>> > This means that each bus driver has its parentdata defined by what uclass
>> > it belongs to. Are you sure this won't be a limitation? I mean that you
>> > could end up with uclasses that have the same calls but a slightly
>> > different parentdata.
>> No it is defined by the driver of the bus. Since it is possible to
>> have (for example) 3 different USB drivers in a system, each can have
>> its own idea of what child data it wants. I definitely agree that
>> setting the parentdata by the child's uclass, or even the bus's uclass
>> would be limiting. In the case of SPI I have elected to use struct
>> spi_slave for reasons I explained earlier.
>
> OK, so you would have some bus uclasses that pass typecasted parentdata,
> because it is the same for all bus drivers, and some that pass the childs
> udevice*, because the parentdata type is not known beforehands? what happens
> if suddenly you need to add a bus controller that needs a little more per-
> child data than the existing ones?
>
> wouldn't it be better to unify this somehow?

As mentioned spi_slave is something of a special case to retain some
sort of code compatibility. We use the same structure with an without
driver model.

Possibly we should unify this. I did think of allowing a child to
support both parent-defined data and parent-uclass-defined data. It is
an obvious step in some ways. However, I am concerned that the
complexity of all of this is getting out of hand. My approach so far
with driver model has been to build things as they are needed. I agree
that this requires a certain about of refactoring effort, but I am
much more worried about creating a framework that looks lovely but no
one understands :-)

>
>> > Yes, you could have a shared parentdata from the uclass (that makes sense
>> > for all controllers, because of the way the bus works), and a
>> > controller-specific parentdata as an extension (read "void *private" at
>> > the end of the parentdata structure)
>> >
>> >> Re passing both the bus and the device, really, what's the point? The
>> >> only valid bus to pass to the function is dev->parent. If you pass
>> >> anything else it is an error. It is redundant and therefore just
>> >> introduces the possibility of error.
>> >
>> > Well, yes, sort of.
>> >
>> > Consider a bus with transparent bridges, like USB or PCI.
>> >
>> > If you were to represent these bridges as udevices, you would end up with
>> > something in between the actual devices and the bus controller, that
>> > forwards requests with no or minimal change.
>> > in case of USB specifically (IIRC), hubs are visible during
>> > initialization, but when the device is up it has its own descriptor that
>> > is used to
>> > in case of PCI, the device address actually contains the bus number, but
>> > the device itself doesn't really care about it, and is only used to
>> > select which bus the command goes to.
>> >
>> > consider the following scenario:
>> > ------     ----------     ---------     ---------     ------------
>> >
>> > |root| --- |ehci_hcd| --- |usb_hub| --- |usb_hub| --- |usb_device|
>> >
>> > ------     ----------     ---------     ---------     ------------
>> >
>> > If you were to flatten the bus, the udevice tree would not really
>> > correspond to how the bus looks like in reality, and it might give you
>> > some hurdles with initialization.
>> >
>> > note that in these cases you cannot pass a child udevice to the bus
>> > controller as a part of the upcall, since it is not necessarily its
>> > child. this is in contradiction to what I wrote previously, simply
>> > because I didn't think hard enough to find these counter examples.
>>
>> I think you are referring to setting up a bus such that it becomes
>> transparent. But even when it is, I'm not sure that driver model needs
>> to rearrange its data structures to suit. Why would you flatten the
>> bus? If we keep the data structures the same (strict parent-child
>> relationships) then we always have a parent for the child, rather than
>> trying to be too clever. Still, the child can obtain direct bus access
>> through some mechanism delegated by the parent if we like. In that
>> case there is even less reason to access the parent udevice IMO.
>>
>> I think I'm missing something from your example, so please let me know.
>
> ok, lets try some code examples
>
> lets say a usb_hub is implemented as a USB bus that itself sits in a USB bus.
> lets say USB bus uclass has a function usb_bulk_msg()
> usb_hub's implementation of such function would look like this:
>
> usb_bulk_msg(udevice *hub, usb_descriptor *child, ...)
> {
>         return usb_bulk_msg(hub->parent, child, ...);
> }
>
> your USB device would then call
> usb_bulk_msg(my_dev->parent, my_desc,...)
>
> this would work recursively through all hubs, and the end result you would
> like is to call usb_bulk_msg() of the host bus controller, without changing
> any of the parameters. (except the first, which is used for recursion)
>
> However, if you got rid of the first parameter, you would need a different
> variable to control your recursion, or you would need the device descriptor to
> hold pointer to the udevice of the host controller, which would effectively
> flatten the bus.
>
> this is the case where I believe the best way to implement is to have a shared
> parentdata based on parent uclass (the usb_descriptor in this case), with a
> driver-specific extension, in case the bus driver need something extra over
> what the "standard" is. the shared parentdata would then be used as a device
> descriptor/address/whatever-you-call-that in the bus calls.
>
> One would also need to relax the semantics a bit, so that host bus controller
> can fill the parentdata for a udevice that is not its direct child.
>
> I'm not saying your code is wrong or anything, I just think you should have a
> look at a more complex bus like USB or PCI, and design the "bus uclass
> interface template" in a way that would support such a bus. Otherwise you
> might end up either reworking your simpler busses later, or having
> inconsistent bus uclass interfaces (which we did when we tried this the first
> time).
>
> I understand that API compatibility is an issue, and I agree that it is fine to
> cut some corners at this point, but we need to keep in mind to fix them once
> all the drivers have been converted.

Right, so you almost have me convinced that we should add a separate
parent parameter. However I would really like to see how USB hubs are
implemented before we do this. We are actually talking about a
different uclass, and there is no requirement that different uclasses
follow similar semantics. I agree that this is desirable trait and I'm
not really worried about the inefficiency of passing a redundant
parameter in the case of SPI since the cost is pretty small.

So how about we see how USB hubs are implemented in practice, and keep
this in mind? For that reason it would be useful to get USB going
quickly.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-17 18:29             ` Pavel Herrmann
@ 2014-07-21  2:17               ` Simon Glass
  0 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-07-21  2:17 UTC (permalink / raw)
  To: u-boot

Hi Pavel,

On 17 July 2014 12:29, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
> On Thursday 17 of July 2014 20:01:29 Pavel Herrmann wrote:
>> Hi
>>
>> On Thursday 17 of July 2014 09:26:47 Simon Glass wrote:
>> > Hi Pavel,
>> >
>> > On 17 July 2014 01:57, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
>> > > Hi
>> > >
>> > > On Wednesday 16 of July 2014 23:39:44 Simon Glass wrote:
>> > >> Hi Pavel,
>> > >>
>> > >> On 15 July 2014 02:26, Pavel Herrmann <morpheus.ibis@gmail.com> wrote:
>> > >> > Hi
>> > >> >
>> > >> > On Monday 14 of July 2014 18:56:13 Simon Glass wrote:
>> > >> > > Add a uclass which provides access to SPI buses and includes
>> > >> > > operations
>> > >> > > required by SPI.
>> > >> > >
>> > >> > > For a time driver model will need to co-exist with the legacy SPI
>> > >> > > interface
>> > >> > > so some parts of the header file are changed depending on which is
>> > >> > > in
>> > >> > > use.
>> > >> > > The exports are adjusted also since some functions are not
>> > >> > > available
>> > >> > > with
>> > >> > > driver model.
>> > >> > >
>> > >> > > Boards must define CONFIG_DM_SPI to use driver model for SPI.
>> > >> > >
>> > >> > > Signed-off-by: Simon Glass <sjg@chromium.org>
>> > >> > > ---
>> > >> > >
>> > >> > > ...
>> > >> > > +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
>> > >> > > +          const void *dout, void *din, unsigned long flags)
>> > >> > > +{
>> > >> > > +     struct udevice *dev = slave->dev;
>> > >> > > +     struct udevice *bus = dev->parent;
>> > >> >
>> > >> > is this the best interface here?
>> > >> > I think it would be cleaner if bus drivers had interfaces which
>> > >> > follow
>> > >> > a
>> > >> > certain template, such as
>> > >> > bus_ops(struct udevice *bus, struct udevice *child, ...)
>> > >>
>> > >> Thanks for your comments.
>> > >>
>> > >> Well I thought about that long and hard. Clearly in a pure
>> > >> driver-model work we would pass a udevice, not a spi_slave.
>> > >>
>> > >> > struct spi_slave would be a prime candidate to have in
>> > >> > child->parentdata
>> > >> > (which should only be accessed by the parent IIUC)
>> > >>
>> > >> Yes, it is. In other words, 'struct spi_slave' is the child's parent
>> > >> data. The only reason that spi_xfer() is passed a spi_slave rather
>> > >> than a udevice is to maintain compatibility with the existing SPI
>> > >> system. I tried various other approachs, such as '#define spi_slave
>> > >> udevice' and the like. At some point we can change it, but it is
>> > >> really painful to have two completely different APIs co-existing in
>> > >> U-Boot.
>> > >>
>> > >> This way, driver model becomes a fairly soft transition, the amount of
>> > >> rewriting needed is reduced, and I think it is much more likely that
>> > >> people will use it. As an example, one of the reasons that the generic
>> > >> board change has been relatively painless is that people can just
>> > >> define CONFIG_SYS_GENERIC_BOARD in their board header file, and in
>> > >> most cases it just works. If we require people to rewrite things it
>> > >> will take longer, etc. There is already enough rewriting required in
>> > >> individual drivers to keep people busy. It will be a relatively easy
>> > >> change to do in the future if we want to.
>> > >
>> > > OK, that reason I understand.
>> > >
>> > > However, what you are doing now is limiting what parentdata a SPI bus
>> > > controller can use.
>> > > This means that each bus driver has its parentdata defined by what
>> > > uclass
>> > > it belongs to. Are you sure this won't be a limitation? I mean that you
>> > > could end up with uclasses that have the same calls but a slightly
>> > > different parentdata.
>> >
>> > No it is defined by the driver of the bus. Since it is possible to
>> > have (for example) 3 different USB drivers in a system, each can have
>> > its own idea of what child data it wants. I definitely agree that
>> > setting the parentdata by the child's uclass, or even the bus's uclass
>> > would be limiting. In the case of SPI I have elected to use struct
>> > spi_slave for reasons I explained earlier.
>>
>> OK, so you would have some bus uclasses that pass typecasted parentdata,
>> because it is the same for all bus drivers, and some that pass the childs
>> udevice*, because the parentdata type is not known beforehands? what happens
>> if suddenly you need to add a bus controller that needs a little more per-
>> child data than the existing ones?
>>
>> wouldn't it be better to unify this somehow?
>>
>> > > Yes, you could have a shared parentdata from the uclass (that makes
>> > > sense
>> > > for all controllers, because of the way the bus works), and a
>> > > controller-specific parentdata as an extension (read "void *private" at
>> > > the end of the parentdata structure)
>> > >
>> > >> Re passing both the bus and the device, really, what's the point? The
>> > >> only valid bus to pass to the function is dev->parent. If you pass
>> > >> anything else it is an error. It is redundant and therefore just
>> > >> introduces the possibility of error.
>> > >
>> > > Well, yes, sort of.
>> > >
>> > > Consider a bus with transparent bridges, like USB or PCI.
>> > >
>> > > If you were to represent these bridges as udevices, you would end up
>> > > with
>> > > something in between the actual devices and the bus controller, that
>> > > forwards requests with no or minimal change.
>> > > in case of USB specifically (IIRC), hubs are visible during
>> > > initialization, but when the device is up it has its own descriptor that
>> > > is used to
>> > > in case of PCI, the device address actually contains the bus number, but
>> > > the device itself doesn't really care about it, and is only used to
>> > > select which bus the command goes to.
>> > >
>> > > consider the following scenario:
>> > > ------     ----------     ---------     ---------     ------------
>> > >
>> > > |root| --- |ehci_hcd| --- |usb_hub| --- |usb_hub| --- |usb_device|
>> > >
>> > > ------     ----------     ---------     ---------     ------------
>> > >
>> > > If you were to flatten the bus, the udevice tree would not really
>> > > correspond to how the bus looks like in reality, and it might give you
>> > > some hurdles with initialization.
>> > >
>> > > note that in these cases you cannot pass a child udevice to the bus
>> > > controller as a part of the upcall, since it is not necessarily its
>> > > child. this is in contradiction to what I wrote previously, simply
>> > > because I didn't think hard enough to find these counter examples.
>> >
>> > I think you are referring to setting up a bus such that it becomes
>> > transparent. But even when it is, I'm not sure that driver model needs
>> > to rearrange its data structures to suit. Why would you flatten the
>> > bus? If we keep the data structures the same (strict parent-child
>> > relationships) then we always have a parent for the child, rather than
>> > trying to be too clever. Still, the child can obtain direct bus access
>> > through some mechanism delegated by the parent if we like. In that
>> > case there is even less reason to access the parent udevice IMO.
>> >
>> > I think I'm missing something from your example, so please let me know.
>>
>> ok, lets try some code examples
>>
>> lets say a usb_hub is implemented as a USB bus that itself sits in a USB
>> bus. lets say USB bus uclass has a function usb_bulk_msg()
>> usb_hub's implementation of such function would look like this:
>>
>> usb_bulk_msg(udevice *hub, usb_descriptor *child, ...)
>> {
>>       return usb_bulk_msg(hub->parent, child, ...);
>> }
>>
>> your USB device would then call
>> usb_bulk_msg(my_dev->parent, my_desc,...)
>>
>> this would work recursively through all hubs, and the end result you would
>> like is to call usb_bulk_msg() of the host bus controller, without changing
>> any of the parameters. (except the first, which is used for recursion)
>>
>> However, if you got rid of the first parameter, you would need a different
>> variable to control your recursion, or you would need the device descriptor
>> to hold pointer to the udevice of the host controller, which would
>> effectively flatten the bus.
>>
>> this is the case where I believe the best way to implement is to have a
>> shared parentdata based on parent uclass (the usb_descriptor in this case),
>> with a driver-specific extension, in case the bus driver need something
>> extra over what the "standard" is. the shared parentdata would then be used
>> as a device descriptor/address/whatever-you-call-that in the bus calls.
>
> I would like to add that passing the childs udevice* should work equally as
> well, if you relax the bus == child->parent assumption.

OK. But at least in the case of USB I think we should be able to
display the bus as a tree - hubs included. If we start re-stitching
children to different parents when a bus is active I worry that things
are going to get complicated.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec
  2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
                   ` (24 preceding siblings ...)
  2014-07-15  0:56 ` [U-Boot] [PATCH 25/25] dm: exynos: cros_ec: Move cros_ec_spi to driver model Simon Glass
@ 2014-08-09 21:29 ` Simon Glass
  2014-08-10  9:16   ` Jagan Teki
  25 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-08-09 21:29 UTC (permalink / raw)
  To: u-boot

Hi,

On 14 July 2014 18:56, Simon Glass <sjg@chromium.org> wrote:
> Up until now driver model has not been used for any type of bus. Buses
> have some unique properties and needs, so we cannot claim that driver
> model can cover all the common cases unless we have converted a bus over
> to driver model.
>
> SPI is a reasonable choice for this next step. It has a fairly simple
> API and not too many dependencies. The main one is SPI flash so we may
> as well convert that also. Since the boards I test with have cros_ec I
> have also included that, for SPI only.
>
> The technique used is make use of driver model's supported data structures
> to hold information currently kept by each subsystem in a private data
> structure. Since 'struct spi_slave' relates to the slave device on the bus
> it is stored in the 'parent' data with each child device of the bus.
> Since 'struct spi_flash' is a standard interface used for each SPI flash
> driver, it is stored in the SPI FLash uclass's private data for each
> device.
>
> New defines are created to enable driver model for each subsystem. These
> are:
>
>    CONFIG_DM_SPI
>    CONFIG_DM_SPI_FLASH
>    CONFIG_DM_CROS_EC
>
> This allows us to move some boards and drivers to driver model, while
> leaving others behind. A 'big bang' conversion of everything to driver
> model, event at a subsystem level, is never going to work.
>
> On the other hand, we change the driver at the same time as the CONFIG
> option is enabled. Keeping both version of the driver around involves a
> flock of #ifdefs, the benefit of which is not apparent to me, since the
> old code is removed anyway.
>
> There is some cost in changing the uclass interface after it is created,
> so if you have limited time, please spend it reviewing the uclass
> interfaces in spi.h and spi_flash.h. These need to be supported by each
> driver, so changing them later may involve changing multiple drivers.
>
> To help with conversion of SPI drivers to driver model, documentation is
> provided which takes the happy camper through the process with an example.
>
> As always, driver model patches are available at u-boot-dm.git branch
> 'working'.
>
> Note: This series is not fully ready - e.g. some header files are missing
> comments. But I wanted to get it out for review early since some SPI work
> is ongoing which might depend on it.

Are there any comments on this series please? I'd like to apply these
to dm/master early next week, excluding the exynos ones.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec
  2014-08-09 21:29 ` [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
@ 2014-08-10  9:16   ` Jagan Teki
  2014-08-11 19:55     ` Simon Glass
  0 siblings, 1 reply; 51+ messages in thread
From: Jagan Teki @ 2014-08-10  9:16 UTC (permalink / raw)
  To: u-boot

On Sun, Aug 10, 2014 at 2:59 AM, Simon Glass <sjg@chromium.org> wrote:
> Hi,
>
> On 14 July 2014 18:56, Simon Glass <sjg@chromium.org> wrote:
>> Up until now driver model has not been used for any type of bus. Buses
>> have some unique properties and needs, so we cannot claim that driver
>> model can cover all the common cases unless we have converted a bus over
>> to driver model.
>>
>> SPI is a reasonable choice for this next step. It has a fairly simple
>> API and not too many dependencies. The main one is SPI flash so we may
>> as well convert that also. Since the boards I test with have cros_ec I
>> have also included that, for SPI only.
>>
>> The technique used is make use of driver model's supported data structures
>> to hold information currently kept by each subsystem in a private data
>> structure. Since 'struct spi_slave' relates to the slave device on the bus
>> it is stored in the 'parent' data with each child device of the bus.
>> Since 'struct spi_flash' is a standard interface used for each SPI flash
>> driver, it is stored in the SPI FLash uclass's private data for each
>> device.
>>
>> New defines are created to enable driver model for each subsystem. These
>> are:
>>
>>    CONFIG_DM_SPI
>>    CONFIG_DM_SPI_FLASH
>>    CONFIG_DM_CROS_EC
>>
>> This allows us to move some boards and drivers to driver model, while
>> leaving others behind. A 'big bang' conversion of everything to driver
>> model, event at a subsystem level, is never going to work.
>>
>> On the other hand, we change the driver at the same time as the CONFIG
>> option is enabled. Keeping both version of the driver around involves a
>> flock of #ifdefs, the benefit of which is not apparent to me, since the
>> old code is removed anyway.
>>
>> There is some cost in changing the uclass interface after it is created,
>> so if you have limited time, please spend it reviewing the uclass
>> interfaces in spi.h and spi_flash.h. These need to be supported by each
>> driver, so changing them later may involve changing multiple drivers.
>>
>> To help with conversion of SPI drivers to driver model, documentation is
>> provided which takes the happy camper through the process with an example.
>>
>> As always, driver model patches are available at u-boot-dm.git branch
>> 'working'.
>>
>> Note: This series is not fully ready - e.g. some header files are missing
>> comments. But I wanted to get it out for review early since some SPI work
>> is ongoing which might depend on it.
>
> Are there any comments on this series please? I'd like to apply these
> to dm/master early next week, excluding the exynos ones.

I will be busy in next full week, give me 2 more weeks.
Probably I will give my comments on next to next weekend.

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec
  2014-08-10  9:16   ` Jagan Teki
@ 2014-08-11 19:55     ` Simon Glass
  2014-09-15  1:03       ` Simon Glass
  0 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-08-11 19:55 UTC (permalink / raw)
  To: u-boot

Hi Jagan,

On 10 August 2014 03:16, Jagan Teki <jagannadh.teki@gmail.com> wrote:
> On Sun, Aug 10, 2014 at 2:59 AM, Simon Glass <sjg@chromium.org> wrote:
>> Hi,
>>
>> On 14 July 2014 18:56, Simon Glass <sjg@chromium.org> wrote:
>>> Up until now driver model has not been used for any type of bus. Buses
>>> have some unique properties and needs, so we cannot claim that driver
>>> model can cover all the common cases unless we have converted a bus over
>>> to driver model.
>>>
>>> SPI is a reasonable choice for this next step. It has a fairly simple
>>> API and not too many dependencies. The main one is SPI flash so we may
>>> as well convert that also. Since the boards I test with have cros_ec I
>>> have also included that, for SPI only.
>>>
>>> The technique used is make use of driver model's supported data structures
>>> to hold information currently kept by each subsystem in a private data
>>> structure. Since 'struct spi_slave' relates to the slave device on the bus
>>> it is stored in the 'parent' data with each child device of the bus.
>>> Since 'struct spi_flash' is a standard interface used for each SPI flash
>>> driver, it is stored in the SPI FLash uclass's private data for each
>>> device.
>>>
>>> New defines are created to enable driver model for each subsystem. These
>>> are:
>>>
>>>    CONFIG_DM_SPI
>>>    CONFIG_DM_SPI_FLASH
>>>    CONFIG_DM_CROS_EC
>>>
>>> This allows us to move some boards and drivers to driver model, while
>>> leaving others behind. A 'big bang' conversion of everything to driver
>>> model, event at a subsystem level, is never going to work.
>>>
>>> On the other hand, we change the driver at the same time as the CONFIG
>>> option is enabled. Keeping both version of the driver around involves a
>>> flock of #ifdefs, the benefit of which is not apparent to me, since the
>>> old code is removed anyway.
>>>
>>> There is some cost in changing the uclass interface after it is created,
>>> so if you have limited time, please spend it reviewing the uclass
>>> interfaces in spi.h and spi_flash.h. These need to be supported by each
>>> driver, so changing them later may involve changing multiple drivers.
>>>
>>> To help with conversion of SPI drivers to driver model, documentation is
>>> provided which takes the happy camper through the process with an example.
>>>
>>> As always, driver model patches are available at u-boot-dm.git branch
>>> 'working'.
>>>
>>> Note: This series is not fully ready - e.g. some header files are missing
>>> comments. But I wanted to get it out for review early since some SPI work
>>> is ongoing which might depend on it.
>>
>> Are there any comments on this series please? I'd like to apply these
>> to dm/master early next week, excluding the exynos ones.
>
> I will be busy in next full week, give me 2 more weeks.
> Probably I will give my comments on next to next weekend.

I'll see what I can do. It has been out for review for 4 weeks :-)

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-15  0:56 ` [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI Simon Glass
  2014-07-15  8:26   ` Pavel Herrmann
@ 2014-08-11 21:46   ` Daniel Schwierzeck
  2014-08-28  8:58   ` Jagan Teki
  2 siblings, 0 replies; 51+ messages in thread
From: Daniel Schwierzeck @ 2014-08-11 21:46 UTC (permalink / raw)
  To: u-boot

Hi Simon,

some nitpicking

On 15.07.2014 02:56, Simon Glass wrote:
> Add a uclass which provides access to SPI buses and includes operations
> required by SPI.
> 
> For a time driver model will need to co-exist with the legacy SPI interface
> so some parts of the header file are changed depending on which is in use.
> The exports are adjusted also since some functions are not available with
> driver model.
> 
> Boards must define CONFIG_DM_SPI to use driver model for SPI.
> 
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
> 
>  common/exports.c         |   4 +-
>  drivers/spi/Makefile     |   4 +
>  drivers/spi/spi-uclass.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++
>  include/dm/uclass-id.h   |   1 +
>  include/spi.h            | 140 ++++++++++++++++++++++++++
>  5 files changed, 401 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/spi/spi-uclass.c

...

> --- /dev/null
> +++ b/drivers/spi/spi-uclass.c
> @@ -0,0 +1,253 @@
> +/*
> + * Copyright (c) 2014 Google, Inc
> + *
> + * SPDX-License-Identifier:	GPL-2.0+
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <errno.h>
> +#include <fdtdec.h>
> +#include <spi.h>
> +#include <dm/device-internal.h>
> +#include <dm/uclass-internal.h>
> +#include <dm/root.h>
> +#include <dm/lists.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
> +{
> +	struct dm_spi_ops *ops;
> +	int ret;
> +
> +	ops = spi_get_ops(bus);
> +	if (ops->set_speed)
> +		ret = (*ops->set_speed)(bus, speed);

ret = ops->set_speed(bus, speed);

> +	else
> +		ret = -EINVAL;
> +	if (ret) {
> +		printf("Cannot set speed (err=%d)\n", ret);
> +		return ret;
> +	}
> +
> +	ops = spi_get_ops(bus);

redundant assignment

> +	if (ops->set_mode)
> +		ret = (*ops->set_mode)(bus, mode);

ret = ops->set_mode(bus, mode);

> +	else
> +		ret = -EINVAL;
> +	if (ret) {
> +		printf("Cannot set mode (err=%d)\n", ret);
> +		return ret;
> +	}
> +
> +	return 0;
> +}
> +
> +int spi_claim_bus(struct spi_slave *slave)
> +{
> +	struct udevice *dev = slave->dev;
> +	struct udevice *bus = dev->parent;
> +	struct dm_spi_ops *ops = spi_get_ops(bus);
> +	struct dm_spi_bus *spi = bus->uclass_priv;
> +	int speed;
> +	int ret;
> +
> +	speed = slave->max_hz;
> +	if (spi->max_hz) {
> +		if (speed)
> +			speed = min(speed, spi->max_hz);
> +		else
> +			speed = spi->max_hz;
> +	}
> +	if (!speed)
> +		speed = 100000;
> +	ret = spi_set_speed_mode(bus, speed, slave->mode);
> +	if (ret)
> +		return ret;
> +
> +	return ops->claim_bus ? ops->claim_bus(bus) : 0;
> +}
> +
> +void spi_release_bus(struct spi_slave *slave)
> +{
> +	struct udevice *dev = slave->dev;
> +	struct udevice *bus = dev->parent;
> +	struct dm_spi_ops *ops = spi_get_ops(bus);
> +
> +	if (ops->release_bus)
> +		spi_get_ops(bus)->release_bus(bus);

ops->release_bus(bus);

> +}
> +
> +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> +	     const void *dout, void *din, unsigned long flags)
> +{
> +	struct udevice *dev = slave->dev;
> +	struct udevice *bus = dev->parent;
> +
> +	if (bus->uclass->uc_drv->id != UCLASS_SPI)
> +		return -EOPNOTSUPP;
> +
> +	return spi_get_ops(bus)->xfer(bus, dev, bitlen, dout, din, flags);
> +}
> +
> +int spi_post_bind(struct udevice *dev)
> +{
> +	/* Scan the bus for devices */
> +	return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
> +}
> +
> +int spi_post_probe(struct udevice *dev)
> +{
> +	struct dm_spi_bus *spi = dev->uclass_priv;
> +
> +	spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
> +				     "spi-max-frequency", 0);
> +
> +	return 0;
> +}
> +
> +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
> +		    const char *dev_name, struct udevice **slavep)
> +{
> +	struct driver *drv;
> +	int ret;
> +
> +	drv = lists_driver_lookup_name(drv_name);
> +	if (!drv) {
> +		puts("Cannot find spi_flash_std driver\n");
> +		return -ENOENT;
> +	}
> +	ret = device_bind(bus, drv, dev_name, NULL, -1, slavep);
> +	if (ret) {
> +		printf("Cannot create device named '%s' (err=%d)\n",
> +		       dev_name, ret);
> +		return ret;
> +	}
> +	(*slavep)->req_seq = cs;
> +
> +	return 0;
> +}
> +
> +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
> +			struct udevice **devp)
> +{
> +	struct udevice *bus, *dev;
> +	int ret;
> +
> +	ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
> +	if (ret)
> +		return ret;
> +	ret = device_find_child_by_seq(bus, cs, false, &dev);
> +	if (ret)
> +		return ret;
> +	*busp = bus;
> +	*devp = dev;
> +
> +	return ret;
> +}
> +
> +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
> +		       const char *drv_name, const char *dev_name,
> +		       struct udevice **devp, struct spi_slave **slavep)
> +{
> +	struct udevice *bus, *dev;
> +	int ret;
> +
> +	ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
> +	if (ret) {
> +		printf("Invalid bus %d (err=%d)\n", busnum, ret);
> +		return ret;
> +	}
> +	ret = device_get_child_by_seq(bus, cs, &dev);
> +
> +	/**
> +	 * If there is no such device, create one automatically. This means
> +	 * that we don't need a device tree node or platform data for the
> +	 * SPI flash chip - we will bind to the correct driver.
> +	 */
> +	if (ret == -ENODEV && drv_name) {
> +		ret = spi_bind_device(bus, cs, drv_name, dev_name, &dev);
> +		if (ret)
> +			return ret;
> +	}
> +	if (ret) {
> +		printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
> +		       ret);
> +		return ret;
> +	}
> +
> +	ret = spi_set_speed_mode(bus, speed, mode);
> +	if (ret)
> +		return ret;
> +
> +	*devp = bus;
> +	*slavep = dev_get_parentdata(dev);
> +
> +	return 0;
> +}
> +
> +/* Compatibility function - to be removed */
> +struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
> +				      int bus_node)
> +{
> +	struct udevice *bus, *dev;
> +	int ret;
> +
> +	ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
> +	if (ret)
> +		return NULL;
> +	ret = device_get_child_by_of_offset(bus, node, &dev);
> +	if (ret)
> +		return NULL;
> +	return dev_get_parentdata(dev);
> +}
> +
> +/* Compatibility function - to be removed */
> +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
> +				  unsigned int speed, unsigned int mode)
> +{
> +	struct spi_slave *slave;
> +	struct udevice *dev;
> +	int ret;
> +
> +	ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
> +				  &slave);
> +	if (ret)
> +		return NULL;
> +
> +	return slave;
> +}
> +
> +void spi_free_slave(struct spi_slave *slave)
> +{
> +	device_remove(slave->dev);
> +	slave->dev = NULL;
> +}
> +
> +int spi_ofdata_to_platdata(const void *blob, int node,
> +			   struct spi_slave *spi)
> +{
> +	int mode = 0;
> +
> +	spi->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
> +	if (fdtdec_get_bool(blob, node, "spi-cpol"))
> +		mode |= SPI_CPOL;
> +	if (fdtdec_get_bool(blob, node, "spi-cpha"))
> +		mode |= SPI_CPHA;
> +	if (fdtdec_get_bool(blob, node, "spi-cs-high"))
> +		mode |= SPI_CS_HIGH;
> +	if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
> +		mode |= SPI_PREAMBLE;
> +	spi->mode = mode;
> +
> +	return 0;
> +}
> +
> +UCLASS_DRIVER(spi) = {
> +	.id		= UCLASS_SPI,
> +	.name		= "spi",
> +	.post_bind	= spi_post_bind,
> +	.post_probe	= spi_post_probe,
> +	.per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
> +};


-- 
- Daniel

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params
  2014-07-15  0:56 ` [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params Simon Glass
@ 2014-08-25  9:24   ` Jagan Teki
  0 siblings, 0 replies; 51+ messages in thread
From: Jagan Teki @ 2014-08-25  9:24 UTC (permalink / raw)
  To: u-boot

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> At present sandbox has its own table of supported SPI flash chips. Now that
> the SPI flash system is fully consolidated and has its own list, sandbox
> should use that.
>
> This enables us to expand the number of chips that sandbox supports.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  drivers/mtd/spi/sandbox.c | 114 ++++++++++++++++------------------------------
>  1 file changed, 38 insertions(+), 76 deletions(-)

Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

>
> diff --git a/drivers/mtd/spi/sandbox.c b/drivers/mtd/spi/sandbox.c
> index a62ef4c..98e0a34 100644
> --- a/drivers/mtd/spi/sandbox.c
> +++ b/drivers/mtd/spi/sandbox.c
> @@ -51,46 +51,7 @@ static const char *sandbox_sf_state_name(enum sandbox_sf_state state)
>  /* Assume all SPI flashes have 3 byte addresses since they do atm */
>  #define SF_ADDR_LEN    3
>
> -struct sandbox_spi_flash_erase_commands {
> -       u8 cmd;
> -       u32 size;
> -};
> -#define IDCODE_LEN 5
> -#define MAX_ERASE_CMDS 3
> -struct sandbox_spi_flash_data {
> -       const char *name;
> -       u8 idcode[IDCODE_LEN];
> -       u32 size;
> -       const struct sandbox_spi_flash_erase_commands
> -                                               erase_cmds[MAX_ERASE_CMDS];
> -};
> -
> -/* Structure describing all the flashes we know how to emulate */
> -static const struct sandbox_spi_flash_data sandbox_sf_flashes[] = {
> -       {
> -               "M25P16", { 0x20, 0x20, 0x15 }, (2 << 20),
> -               {       /* erase commands */
> -                       { 0xd8, (64 << 10), }, /* sector */
> -                       { 0xc7, (2 << 20), }, /* bulk */
> -               },
> -       },
> -       {
> -               "W25Q32", { 0xef, 0x40, 0x16 }, (4 << 20),
> -               {       /* erase commands */
> -                       { 0x20, (4 << 10), }, /* 4KB */
> -                       { 0xd8, (64 << 10), }, /* sector */
> -                       { 0xc7, (4 << 20), }, /* bulk */
> -               },
> -       },
> -       {
> -               "W25Q128", { 0xef, 0x40, 0x18 }, (16 << 20),
> -               {       /* erase commands */
> -                       { 0x20, (4 << 10), }, /* 4KB */
> -                       { 0xd8, (64 << 10), }, /* sector */
> -                       { 0xc7, (16 << 20), }, /* bulk */
> -               },
> -       },
> -};
> +#define IDCODE_LEN 3
>
>  /* Used to quickly bulk erase backing store */
>  static u8 sandbox_sf_0xff[0x1000];
> @@ -109,7 +70,8 @@ struct sandbox_spi_flash {
>          */
>         enum sandbox_sf_state state;
>         uint cmd;
> -       const void *cmd_data;
> +       /* Erase size of current erase command */
> +       uint erase_size;
>         /* Current position in the flash; used when reading/writing/etc... */
>         uint off;
>         /* How many address bytes we've consumed */
> @@ -117,7 +79,7 @@ struct sandbox_spi_flash {
>         /* The current flash status (see STAT_XXX defines above) */
>         u16 status;
>         /* Data describing the flash we're emulating */
> -       const struct sandbox_spi_flash_data *data;
> +       const struct spi_flash_params *data;
>         /* The file on disk to serv up data from */
>         int fd;
>  };
> @@ -127,8 +89,8 @@ static int sandbox_sf_setup(void **priv, const char *spec)
>         /* spec = idcode:file */
>         struct sandbox_spi_flash *sbsf;
>         const char *file;
> -       size_t i, len, idname_len;
> -       const struct sandbox_spi_flash_data *data;
> +       size_t len, idname_len;
> +       const struct spi_flash_params *data;
>
>         file = strchr(spec, ':');
>         if (!file) {
> @@ -138,15 +100,14 @@ static int sandbox_sf_setup(void **priv, const char *spec)
>         idname_len = file - spec;
>         ++file;
>
> -       for (i = 0; i < ARRAY_SIZE(sandbox_sf_flashes); ++i) {
> -               data = &sandbox_sf_flashes[i];
> +       for (data = spi_flash_params_table; data->name; data++) {
>                 len = strlen(data->name);
>                 if (idname_len != len)
>                         continue;
>                 if (!memcmp(spec, data->name, len))
>                         break;
>         }
> -       if (i == ARRAY_SIZE(sandbox_sf_flashes)) {
> +       if (!data->name) {
>                 printf("sandbox_sf: unknown flash '%*s'\n", (int)idname_len,
>                        spec);
>                 goto error;
> @@ -223,7 +184,6 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
>                 sbsf->pad_addr_bytes = 1;
>         case CMD_READ_ARRAY_SLOW:
>         case CMD_PAGE_PROGRAM:
> - state_addr:
>                 sbsf->state = SF_ADDR;
>                 break;
>         case CMD_WRITE_DISABLE:
> @@ -241,24 +201,25 @@ static int sandbox_sf_process_cmd(struct sandbox_spi_flash *sbsf, const u8 *rx,
>                 sbsf->status |= STAT_WEL;
>                 break;
>         default: {
> -               size_t i;
> -
> -               /* handle erase commands first */
> -               for (i = 0; i < MAX_ERASE_CMDS; ++i) {
> -                       const struct sandbox_spi_flash_erase_commands *
> -                               erase_cmd = &sbsf->data->erase_cmds[i];
> -
> -                       if (erase_cmd->cmd == 0x00)
> -                               continue;
> -                       if (sbsf->cmd != erase_cmd->cmd)
> -                               continue;
> -
> -                       sbsf->cmd_data = erase_cmd;
> -                       goto state_addr;
> +               int flags = sbsf->data->flags;
> +
> +               /* we only support erase here */
> +               if (sbsf->cmd == CMD_ERASE_CHIP) {
> +                       sbsf->erase_size = sbsf->data->sector_size *
> +                               sbsf->data->nr_sectors;
> +               } else if (sbsf->cmd == CMD_ERASE_4K && (flags & SECT_4K)) {
> +                       sbsf->erase_size = 4 << 10;
> +               } else if (sbsf->cmd == CMD_ERASE_32K && (flags & SECT_32K)) {
> +                       sbsf->erase_size = 32 << 10;
> +               } else if (sbsf->cmd == CMD_ERASE_64K &&
> +                          !(flags & (SECT_4K | SECT_32K))) {
> +                       sbsf->erase_size = 64 << 10;
> +               } else {
> +                       debug(" cmd unknown: %#x\n", sbsf->cmd);
> +                       return 1;
>                 }
> -
> -               debug(" cmd unknown: %#x\n", sbsf->cmd);
> -               return 1;
> +               sbsf->state = SF_ADDR;
> +               break;
>         }
>         }
>
> @@ -309,11 +270,14 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
>                         u8 id;
>
>                         debug(" id: off:%u tx:", sbsf->off);
> -                       if (sbsf->off < IDCODE_LEN)
> -                               id = sbsf->data->idcode[sbsf->off];
> -                       else
> +                       if (sbsf->off < IDCODE_LEN) {
> +                               /* Extract correct byte from ID 0x00aabbcc */
> +                               id = sbsf->data->jedec >>
> +                                       (8 * (IDCODE_LEN - 1 - sbsf->off));
> +                       } else {
>                                 id = 0;
> -                       debug("%02x\n", id);
> +                       }
> +                       debug("%d %02x\n", sbsf->off, id);
>                         tx[pos++] = id;
>                         ++sbsf->off;
>                         break;
> @@ -406,24 +370,22 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
>                         break;
>                 case SF_ERASE:
>   case_sf_erase: {
> -                       const struct sandbox_spi_flash_erase_commands *
> -                                               erase_cmd = sbsf->cmd_data;
> -
>                         if (!(sbsf->status & STAT_WEL)) {
>                                 puts("sandbox_sf: write enable not set before erase\n");
>                                 goto done;
>                         }
>
>                         /* verify address is aligned */
> -                       if (sbsf->off & (erase_cmd->size - 1)) {
> +                       if (sbsf->off & (sbsf->erase_size - 1)) {
>                                 debug(" sector erase: cmd:%#x needs align:%#x, but we got %#x\n",
> -                                     erase_cmd->cmd, erase_cmd->size,
> +                                     sbsf->cmd, sbsf->erase_size,
>                                       sbsf->off);
>                                 sbsf->status &= ~STAT_WEL;
>                                 goto done;
>                         }
>
> -                       debug(" sector erase addr: %u\n", sbsf->off);
> +                       debug(" sector erase addr: %u, size: %u\n", sbsf->off,
> +                             sbsf->erase_size);
>
>                         cnt = bytes - pos;
>                         sandbox_spi_tristate(&tx[pos], cnt);
> @@ -433,7 +395,7 @@ static int sandbox_sf_xfer(void *priv, const u8 *rx, u8 *tx,
>                          * TODO(vapier at gentoo.org): latch WIP in status, and
>                          * delay before clearing it ?
>                          */
> -                       ret = sandbox_erase_part(sbsf, erase_cmd->size);
> +                       ret = sandbox_erase_part(sbsf, sbsf->erase_size);
>                         sbsf->status &= ~STAT_WEL;
>                         if (ret) {
>                                 debug("sandbox_sf: Erase failed\n");
> --
> 2.0.0.526.g5318336
>

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 02/25] sandbox: config: Enable all SPI flash chips
  2014-07-15  0:56 ` [U-Boot] [PATCH 02/25] sandbox: config: Enable all SPI flash chips Simon Glass
@ 2014-08-25  9:24   ` Jagan Teki
  0 siblings, 0 replies; 51+ messages in thread
From: Jagan Teki @ 2014-08-25  9:24 UTC (permalink / raw)
  To: u-boot

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> Sandbox may as well support everything. This increases the amount of code
> that is built/tested by sandbox, and also provides access to all the
> supported SPI flash devices.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  include/configs/sandbox.h | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)

Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

>
> diff --git a/include/configs/sandbox.h b/include/configs/sandbox.h
> index f5fa4b3..5e4e5fc 100644
> --- a/include/configs/sandbox.h
> +++ b/include/configs/sandbox.h
> @@ -94,7 +94,7 @@
>  #define CONFIG_ENV_SIZE                8192
>  #define CONFIG_ENV_IS_NOWHERE
>
> -/* SPI */
> +/* SPI - enable all SPI flash types for testing purposes */
>  #define CONFIG_SANDBOX_SPI
>  #define CONFIG_CMD_SF
>  #define CONFIG_CMD_SF_TEST
> @@ -102,7 +102,13 @@
>  #define CONFIG_SPI_FLASH
>  #define CONFIG_OF_SPI
>  #define CONFIG_OF_SPI_FLASH
> +#define CONFIG_SPI_FLASH_ATMEL
> +#define CONFIG_SPI_FLASH_EON
> +#define CONFIG_SPI_FLASH_GIGADEVICE
> +#define CONFIG_SPI_FLASH_MACRONIX
>  #define CONFIG_SPI_FLASH_SANDBOX
> +#define CONFIG_SPI_FLASH_SPANSION
> +#define CONFIG_SPI_FLASH_SST
>  #define CONFIG_SPI_FLASH_STMICRO
>  #define CONFIG_SPI_FLASH_WINBOND
>
> --
> 2.0.0.526.g5318336
>

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 03/25] sandbox: dts: Add a SPI device and cros_ec device
  2014-07-15  0:56 ` [U-Boot] [PATCH 03/25] sandbox: dts: Add a SPI device and cros_ec device Simon Glass
@ 2014-08-25  9:32   ` Jagan Teki
  0 siblings, 0 replies; 51+ messages in thread
From: Jagan Teki @ 2014-08-25  9:32 UTC (permalink / raw)
  To: u-boot

Hi Simon,

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> Add a SPI device which can be used for testing SPI flash features in
> sandbox.
>
> Also add a cros_ec device since with driver model the Chrome OS EC
> emulation will not otherwise be available.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  arch/sandbox/dts/sandbox.dts                | 25 +++++++++++++++++++++++++
>  doc/device-tree-bindings/mtd/spi/m25p80.txt | 29 +++++++++++++++++++++++++++++
>  2 files changed, 54 insertions(+)
>  create mode 100644 doc/device-tree-bindings/mtd/spi/m25p80.txt
>
> diff --git a/arch/sandbox/dts/sandbox.dts b/arch/sandbox/dts/sandbox.dts
> index daf5e73..fad1fae 100644
> --- a/arch/sandbox/dts/sandbox.dts
> +++ b/arch/sandbox/dts/sandbox.dts
> @@ -1,6 +1,9 @@
>  /dts-v1/;
>
>  / {
> +       #address-cells = <1>;
> +       #size-cells = <0>;
> +
>         aliases {
>                 console = "/serial";
>         };
> @@ -131,5 +134,27 @@
>                 num-gpios = <20>;
>         };
>
> +       spi at 0 {
> +               #address-cells = <1>;
> +               #size-cells = <0>;
> +               reg = <0>;
> +               compatible = "sandbox,spi";
> +               cs-gpios = <0>, <&gpio_a 0>;
> +               flash at 0 {
> +                       reg = <0>;
> +                       compatible = "spansion,m25p16", "sandbox,spi-flash";
> +                       spi-max-frequency = <40000000>;
> +                       sandbox,filename = "spi.bin";
> +               };
> +       };
> +
> +       cros-ec at 0 {
> +               compatible = "google,cros-ec";
> +               #address-cells = <1>;
> +               #size-cells = <1>;
> +               firmware_storage_spi: flash at 0 {
> +                       reg = <0 0x400000>;
> +               };
> +       };
>
>  };
> diff --git a/doc/device-tree-bindings/mtd/spi/m25p80.txt b/doc/device-tree-bindings/mtd/spi/m25p80.txt
> new file mode 100644
> index 0000000..6d3d576
> --- /dev/null
> +++ b/doc/device-tree-bindings/mtd/spi/m25p80.txt
> @@ -0,0 +1,29 @@
> +* MTD SPI driver for ST M25Pxx (and similar) serial flash chips
> +
> +Required properties:
> +- #address-cells, #size-cells : Must be present if the device has sub-nodes
> +  representing partitions.
> +- compatible : Should be the manufacturer and the name of the chip. Bear in mind
> +               the DT binding is not Linux-only, but in case of Linux, see the
> +               "m25p_ids" table in drivers/mtd/devices/m25p80.c for the list of
> +               supported chips.
> +- reg : Chip-Select number
> +- spi-max-frequency : Maximum frequency of the SPI bus the chip can operate at
> +
> +Optional properties:
> +- m25p,fast-read : Use the "fast read" opcode to read data from the chip instead
> +                   of the usual "read" opcode. This opcode is not supported by
> +                   all chips and support for it can not be detected at runtime.
> +                   Refer to your chips' datasheet to check if this is supported
> +                   by your chip.
> +
> +Example:
> +
> +       flash: m25p80 at 0 {
> +               #address-cells = <1>;
> +               #size-cells = <1>;
> +               compatible = "spansion,m25p80";
> +               reg = <0>;
> +               spi-max-frequency = <40000000>;
> +               m25p,fast-read;
> +       };
> --
> 2.0.0.526.g5318336
>

Please add doc-binding stuff more specific to your usage and w/o
having any Linux stuff/terminologies as of now.

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 05/25] spi: Add brackets and tidy defines in spi.h
  2014-07-15  0:56 ` [U-Boot] [PATCH 05/25] spi: Add brackets and tidy defines in spi.h Simon Glass
@ 2014-08-25  9:34   ` Jagan Teki
  0 siblings, 0 replies; 51+ messages in thread
From: Jagan Teki @ 2014-08-25  9:34 UTC (permalink / raw)
  To: u-boot

Thanks for the patch.

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> Some of the #defines in spi.h are not bracketed. To avoid future mistakes
> add brackets. Also add an explanatory comment for SPI_CONN_DUAL_...
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  include/spi.h | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
>
> diff --git a/include/spi.h b/include/spi.h
> index ffd6647..b673be2 100644
> --- a/include/spi.h
> +++ b/include/spi.h
> @@ -30,24 +30,24 @@
>  #define SPI_XFER_MMAP          0x08    /* Memory Mapped start */
>  #define SPI_XFER_MMAP_END      0x10    /* Memory Mapped End */
>  #define SPI_XFER_ONCE          (SPI_XFER_BEGIN | SPI_XFER_END)
> -#define SPI_XFER_U_PAGE                (1 << 5)
> +#define SPI_XFER_U_PAGE        (1 << 5)
>
>  /* SPI TX operation modes */
> -#define SPI_OPM_TX_QPP         1 << 0
> +#define SPI_OPM_TX_QPP         (1 << 0)
>
>  /* SPI RX operation modes */
> -#define SPI_OPM_RX_AS          1 << 0
> -#define SPI_OPM_RX_DOUT                1 << 1
> -#define SPI_OPM_RX_DIO         1 << 2
> -#define SPI_OPM_RX_QOF         1 << 3
> -#define SPI_OPM_RX_QIOF                1 << 4
> -#define SPI_OPM_RX_EXTN                SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \
> +#define SPI_OPM_RX_AS          (1 << 0)
> +#define SPI_OPM_RX_DOUT        (1 << 1)
> +#define SPI_OPM_RX_DIO         (1 << 2)
> +#define SPI_OPM_RX_QOF         (1 << 3)
> +#define SPI_OPM_RX_QIOF        (1 << 4)
> +#define SPI_OPM_RX_EXTN        (SPI_OPM_RX_AS | SPI_OPM_RX_DOUT | \
>                                 SPI_OPM_RX_DIO | SPI_OPM_RX_QOF | \
> -                               SPI_OPM_RX_QIOF
> +                               SPI_OPM_RX_QIOF)
>
> -/* SPI bus connection options */
> -#define SPI_CONN_DUAL_SHARED   1 << 0
> -#define SPI_CONN_DUAL_SEPARATED        1 << 1
> +/* SPI bus connection options - see enum spi_dual_flash */
> +#define SPI_CONN_DUAL_SHARED           (1 << 0)
> +#define SPI_CONN_DUAL_SEPARATED        (1 << 1)
>
>  /* Header byte that marks the start of the message */
>  #define SPI_PREAMBLE_END_BYTE  0xec
> --
> 2.0.0.526.g5318336
>

Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function
  2014-07-15  0:56 ` [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function Simon Glass
@ 2014-08-25 18:31   ` Jagan Teki
  2014-09-02 19:22     ` Simon Glass
  0 siblings, 1 reply; 51+ messages in thread
From: Jagan Teki @ 2014-08-25 18:31 UTC (permalink / raw)
  To: u-boot

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> In preparation for changing the error handling in this code for driver
> model, move it into its own function.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  common/cmd_spi.c | 53 ++++++++++++++++++++++++++++++++---------------------
>  1 file changed, 32 insertions(+), 21 deletions(-)
>

Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

> diff --git a/common/cmd_spi.c b/common/cmd_spi.c
> index 3c8e913..be5709c 100644
> --- a/common/cmd_spi.c
> +++ b/common/cmd_spi.c
> @@ -11,6 +11,7 @@
>
>  #include <common.h>
>  #include <command.h>
> +#include <errno.h>
>  #include <spi.h>
>
>  /*-----------------------------------------------------------------------
> @@ -38,6 +39,35 @@ static int                   bitlen;
>  static uchar           dout[MAX_SPI_BYTES];
>  static uchar           din[MAX_SPI_BYTES];
>
> +static int do_spi_xfer(int bus, int cs)
> +{
> +       struct spi_slave *slave;
> +       int rcode = 0;
> +
> +       slave = spi_setup_slave(bus, cs, 1000000, mode);
> +       if (!slave) {
> +               printf("Invalid device %d:%d\n", bus, cs);
> +               return -EINVAL;
> +       }
> +
> +       spi_claim_bus(slave);
> +       if (spi_xfer(slave, bitlen, dout, din,
> +                    SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
> +               printf("Error during SPI transaction\n");
> +               rcode = -EIO;
> +       } else {
> +               int j;
> +
> +               for (j = 0; j < ((bitlen + 7) / 8); j++)
> +                       printf("%02X", din[j]);
> +               printf("\n");
> +       }
> +       spi_release_bus(slave);
> +       spi_free_slave(slave);
> +
> +       return rcode;
> +}
> +
>  /*
>   * SPI read/write
>   *
> @@ -51,11 +81,9 @@ static uchar                 din[MAX_SPI_BYTES];
>
>  int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>  {
> -       struct spi_slave *slave;
>         char  *cp = 0;
>         uchar tmp;
>         int   j;
> -       int   rcode = 0;
>
>         /*
>          * We use the last specified parameters, unless new ones are
> @@ -103,27 +131,10 @@ int do_spi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>                 return 1;
>         }
>
> -       slave = spi_setup_slave(bus, cs, 1000000, mode);
> -       if (!slave) {
> -               printf("Invalid device %d:%d\n", bus, cs);
> +       if (do_spi_xfer(bus, cs))
>                 return 1;
> -       }
> -
> -       spi_claim_bus(slave);
> -       if(spi_xfer(slave, bitlen, dout, din,
> -                               SPI_XFER_BEGIN | SPI_XFER_END) != 0) {
> -               printf("Error during SPI transaction\n");
> -               rcode = 1;
> -       } else {
> -               for(j = 0; j < ((bitlen + 7) / 8); j++) {
> -                       printf("%02X", din[j]);
> -               }
> -               printf("\n");
> -       }
> -       spi_release_bus(slave);
> -       spi_free_slave(slave);
>
> -       return rcode;
> +       return 0;
>  }
>
>  /***************************************************/
> --
> 2.0.0.526.g5318336
>

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-07-15  0:56 ` [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI Simon Glass
  2014-07-15  8:26   ` Pavel Herrmann
  2014-08-11 21:46   ` Daniel Schwierzeck
@ 2014-08-28  8:58   ` Jagan Teki
  2014-08-29 23:38     ` Simon Glass
  2 siblings, 1 reply; 51+ messages in thread
From: Jagan Teki @ 2014-08-28  8:58 UTC (permalink / raw)
  To: u-boot

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> Add a uclass which provides access to SPI buses and includes operations
> required by SPI.
>
> For a time driver model will need to co-exist with the legacy SPI interface
> so some parts of the header file are changed depending on which is in use.
> The exports are adjusted also since some functions are not available with
> driver model.
>
> Boards must define CONFIG_DM_SPI to use driver model for SPI.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  common/exports.c         |   4 +-
>  drivers/spi/Makefile     |   4 +
>  drivers/spi/spi-uclass.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++
>  include/dm/uclass-id.h   |   1 +
>  include/spi.h            | 140 ++++++++++++++++++++++++++
>  5 files changed, 401 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/spi/spi-uclass.c
>
> diff --git a/common/exports.c b/common/exports.c
> index b97ca48..88fcfc8 100644
> --- a/common/exports.c
> +++ b/common/exports.c
> @@ -27,10 +27,12 @@ unsigned long get_version(void)
>  # define i2c_write         dummy
>  # define i2c_read          dummy
>  #endif
> -#ifndef CONFIG_CMD_SPI
> +#if !defined(CONFIG_CMD_SPI) || defined(CONFIG_DM_SPI)
>  # define spi_init          dummy
>  # define spi_setup_slave   dummy
>  # define spi_free_slave    dummy
> +#endif
> +#ifndef CONFIG_CMD_SPI
>  # define spi_claim_bus     dummy
>  # define spi_release_bus   dummy
>  # define spi_xfer          dummy
> diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
> index f02c35a..d1f1dd0 100644
> --- a/drivers/spi/Makefile
> +++ b/drivers/spi/Makefile
> @@ -6,7 +6,11 @@
>  #
>
>  # There are many options which enable SPI, so make this library available
> +ifdef CONFIG_DM_SPI
> +obj-y += spi-uclass.o
> +else
>  obj-y += spi.o
> +endif
>
>  obj-$(CONFIG_EP93XX_SPI) += ep93xx_spi.o
>  obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
> diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c
> new file mode 100644
> index 0000000..4057bce
> --- /dev/null
> +++ b/drivers/spi/spi-uclass.c
> @@ -0,0 +1,253 @@
> +/*
> + * Copyright (c) 2014 Google, Inc
> + *
> + * SPDX-License-Identifier:    GPL-2.0+
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <errno.h>
> +#include <fdtdec.h>
> +#include <spi.h>
> +#include <dm/device-internal.h>
> +#include <dm/uclass-internal.h>
> +#include <dm/root.h>
> +#include <dm/lists.h>
> +
> +DECLARE_GLOBAL_DATA_PTR;
> +
> +static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
> +{
> +       struct dm_spi_ops *ops;
> +       int ret;
> +
> +       ops = spi_get_ops(bus);
> +       if (ops->set_speed)
> +               ret = (*ops->set_speed)(bus, speed);
> +       else
> +               ret = -EINVAL;
> +       if (ret) {
> +               printf("Cannot set speed (err=%d)\n", ret);
> +               return ret;
> +       }
> +
> +       ops = spi_get_ops(bus);
> +       if (ops->set_mode)
> +               ret = (*ops->set_mode)(bus, mode);
> +       else
> +               ret = -EINVAL;
> +       if (ret) {
> +               printf("Cannot set mode (err=%d)\n", ret);
> +               return ret;
> +       }
> +
> +       return 0;
> +}
> +
> +int spi_claim_bus(struct spi_slave *slave)
> +{
> +       struct udevice *dev = slave->dev;
> +       struct udevice *bus = dev->parent;
> +       struct dm_spi_ops *ops = spi_get_ops(bus);
> +       struct dm_spi_bus *spi = bus->uclass_priv;
> +       int speed;
> +       int ret;
> +
> +       speed = slave->max_hz;
> +       if (spi->max_hz) {
> +               if (speed)
> +                       speed = min(speed, spi->max_hz);
> +               else
> +                       speed = spi->max_hz;
> +       }
> +       if (!speed)
> +               speed = 100000;
> +       ret = spi_set_speed_mode(bus, speed, slave->mode);
> +       if (ret)
> +               return ret;
> +
> +       return ops->claim_bus ? ops->claim_bus(bus) : 0;
> +}
> +
> +void spi_release_bus(struct spi_slave *slave)
> +{
> +       struct udevice *dev = slave->dev;
> +       struct udevice *bus = dev->parent;
> +       struct dm_spi_ops *ops = spi_get_ops(bus);
> +
> +       if (ops->release_bus)
> +               spi_get_ops(bus)->release_bus(bus);
> +}
> +
> +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
> +            const void *dout, void *din, unsigned long flags)
> +{
> +       struct udevice *dev = slave->dev;
> +       struct udevice *bus = dev->parent;
> +
> +       if (bus->uclass->uc_drv->id != UCLASS_SPI)
> +               return -EOPNOTSUPP;
> +
> +       return spi_get_ops(bus)->xfer(bus, dev, bitlen, dout, din, flags);
> +}

Cleared all these calls, as individual drivers/spi* will setup their
ops and fills
the priv data. So this uclass will call accordingly - correct? add if
I'm missing anything.

> +
> +int spi_post_bind(struct udevice *dev)
> +{
> +       /* Scan the bus for devices */
> +       return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
> +}
> +
> +int spi_post_probe(struct udevice *dev)
> +{
> +       struct dm_spi_bus *spi = dev->uclass_priv;
> +
> +       spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
> +                                    "spi-max-frequency", 0);
> +
> +       return 0;
> +}

Anyway each spi driver will fills the data from dtb - like
spi-max-frequency, why the spi
core (uclass) will do the same.

Can't we reuse the spi_slave, why dm_spi_bus?

> +
> +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
> +                   const char *dev_name, struct udevice **slavep)
> +{
> +       struct driver *drv;
> +       int ret;
> +
> +       drv = lists_driver_lookup_name(drv_name);
> +       if (!drv) {
> +               puts("Cannot find spi_flash_std driver\n");
> +               return -ENOENT;
> +       }
> +       ret = device_bind(bus, drv, dev_name, NULL, -1, slavep);
> +       if (ret) {
> +               printf("Cannot create device named '%s' (err=%d)\n",
> +                      dev_name, ret);
> +               return ret;
> +       }
> +       (*slavep)->req_seq = cs;
> +
> +       return 0;
> +}
> +
> +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
> +                       struct udevice **devp)
> +{
> +       struct udevice *bus, *dev;
> +       int ret;
> +
> +       ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
> +       if (ret)
> +               return ret;
> +       ret = device_find_child_by_seq(bus, cs, false, &dev);
> +       if (ret)
> +               return ret;
> +       *busp = bus;
> +       *devp = dev;
> +
> +       return ret;
> +}
> +
> +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
> +                      const char *drv_name, const char *dev_name,
> +                      struct udevice **devp, struct spi_slave **slavep)
> +{
> +       struct udevice *bus, *dev;
> +       int ret;
> +
> +       ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
> +       if (ret) {
> +               printf("Invalid bus %d (err=%d)\n", busnum, ret);
> +               return ret;
> +       }
> +       ret = device_get_child_by_seq(bus, cs, &dev);
> +
> +       /**
> +        * If there is no such device, create one automatically. This means
> +        * that we don't need a device tree node or platform data for the
> +        * SPI flash chip - we will bind to the correct driver.
> +        */
> +       if (ret == -ENODEV && drv_name) {
> +               ret = spi_bind_device(bus, cs, drv_name, dev_name, &dev);
> +               if (ret)
> +                       return ret;
> +       }
> +       if (ret) {
> +               printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
> +                      ret);
> +               return ret;
> +       }
> +
> +       ret = spi_set_speed_mode(bus, speed, mode);
> +       if (ret)
> +               return ret;
> +
> +       *devp = bus;
> +       *slavep = dev_get_parentdata(dev);
> +
> +       return 0;
> +}

I guess this is one of the replacement for spi_setup_slave() in spi drivers, so
how bus and cs member will populate to drivers.

Usually bus and cs members from spi_slave will populate to drivers through
"sf probe" so spi_cs_activate will set the respective chip-select and
along with
reg_base in driver select through bus value.

spi_setup_slave() {
...
zslave->base = get_zynq_spi_base(bus);
...
}

How these will handling with dm?

> +
> +/* Compatibility function - to be removed */
> +struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
> +                                     int bus_node)
> +{
> +       struct udevice *bus, *dev;
> +       int ret;
> +
> +       ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
> +       if (ret)
> +               return NULL;
> +       ret = device_get_child_by_of_offset(bus, node, &dev);
> +       if (ret)
> +               return NULL;
> +       return dev_get_parentdata(dev);
> +}
> +
> +/* Compatibility function - to be removed */
> +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
> +                                 unsigned int speed, unsigned int mode)
> +{
> +       struct spi_slave *slave;
> +       struct udevice *dev;
> +       int ret;
> +
> +       ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
> +                                 &slave);
> +       if (ret)
> +               return NULL;
> +
> +       return slave;
> +}
> +
> +void spi_free_slave(struct spi_slave *slave)
> +{
> +       device_remove(slave->dev);
> +       slave->dev = NULL;
> +}
> +
> +int spi_ofdata_to_platdata(const void *blob, int node,
> +                          struct spi_slave *spi)
> +{
> +       int mode = 0;
> +
> +       spi->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
> +       if (fdtdec_get_bool(blob, node, "spi-cpol"))
> +               mode |= SPI_CPOL;
> +       if (fdtdec_get_bool(blob, node, "spi-cpha"))
> +               mode |= SPI_CPHA;
> +       if (fdtdec_get_bool(blob, node, "spi-cs-high"))
> +               mode |= SPI_CS_HIGH;
> +       if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
> +               mode |= SPI_PREAMBLE;
> +       spi->mode = mode;
> +
> +       return 0;
> +}
> +
> +UCLASS_DRIVER(spi) = {
> +       .id             = UCLASS_SPI,
> +       .name           = "spi",
> +       .post_bind      = spi_post_bind,
> +       .post_probe     = spi_post_probe,
> +       .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
> +};
> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
> index 7f0e37b..8207483 100644
> --- a/include/dm/uclass-id.h
> +++ b/include/dm/uclass-id.h
> @@ -22,6 +22,7 @@ enum uclass_id {
>         /* U-Boot uclasses start here */
>         UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
>         UCLASS_SERIAL,          /* Serial UART */
> +       UCLASS_SPI,             /* SPI bus */
>
>         UCLASS_COUNT,
>         UCLASS_INVALID = -1,
> diff --git a/include/spi.h b/include/spi.h
> index b673be2..b5e9347 100644
> --- a/include/spi.h
> +++ b/include/spi.h
> @@ -54,11 +54,19 @@
>
>  #define SPI_DEFAULT_WORDLEN 8
>
> +#ifdef CONFIG_DM_SPI
> +struct dm_spi_bus {
> +       uint max_hz;
> +};
> +
> +#endif /* CONFIG_DM_SPI */
> +

No idea why this should require, max_hz is already a part of spi_slave
and handling of these like getting from dtb and process and fill on priv
data should be part of individual drivers and filling spi_slave member should
be part of uclass - based on my understanding.

Any comments?


>  /**
>   * struct spi_slave - Representation of a SPI slave
>   *
>   * Drivers are expected to extend this with controller-specific data.
>   *
> + * @dev:               SPI slave device

Missing comments for  max_hz and mode

>   * @bus:               ID of the bus that the slave is attached to.
>   * @cs:                        ID of the chip select connected to the slave.
>   * @op_mode_rx:                SPI RX operation mode.
> @@ -71,8 +79,14 @@
>   * @flags:             Indication of SPI flags.
>   */
>  struct spi_slave {
> +#ifdef CONFIG_DM_SPI
> +       struct udevice *dev;    /* struct spi_slave is dev->parentdata */
> +       uint max_hz;
> +       uint mode;
> +#else
>         unsigned int bus;
>         unsigned int cs;
> +#endif
>         u8 op_mode_rx;
>         u8 op_mode_tx;
>         unsigned int wordlen;
> @@ -220,6 +234,8 @@ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
>  int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
>                 void *din, unsigned long flags);
>
> +#ifndef CONFIG_DM_SPI
> +
>  /**
>   * Determine if a SPI chipselect is valid.
>   * This function is provided by the board if the low-level SPI driver
> @@ -255,6 +271,7 @@ void spi_cs_deactivate(struct spi_slave *slave);
>   * @hz:                The transfer speed
>   */
>  void spi_set_speed(struct spi_slave *slave, uint hz);
> +#endif
>
>  /**
>   * Write 8 bits, then read 8 bits.
> @@ -305,4 +322,127 @@ struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
>  struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
>                                            int node);
>
> +#ifdef CONFIG_DM_SPI
> +
> +/**
> + * struct struct dm_spi_ops - Driver model SPI operations
> + *
> + * The uclass interface is implemented by all SPI devices which use
> + * driver model.
> + */
> +struct dm_spi_ops {
> +       /**
> +        * Claim the bus and prepare it for communication.
> +        *
> +        * The device privided is the slave device. It's parent controller
> +        * will be used to provide the communication.
> +        *
> +        * This must be called before doing any transfers with a SPI slave. It
> +        * will enable and initialize any SPI hardware as necessary, and make
> +        * sure that the SCK line is in the correct idle state. It is not
> +        * allowed to claim the same bus for several slaves without releasing
> +        * the bus in between.
> +        *
> +        * @device:     The SPI slave
> +        *
> +        * Returns: 0 if the bus was claimed successfully, or a negative value
> +        * if it wasn't.
> +        */
> +       int (*claim_bus)(struct udevice *device);
> +
> +       /**
> +        * Release the SPI bus
> +        *
> +        * This must be called once for every call to spi_claim_bus() after
> +        * all transfers have finished. It may disable any SPI hardware as
> +        * appropriate.
> +        *
> +        * @device:     The SPI slave
> +        */
> +       int (*release_bus)(struct udevice *device);
> +
> +       /**
> +        * Set the word length for SPI transactions
> +        *
> +        * Set the word length (number of bits per word) for SPI transactions.
> +        *
> +        * @device:     The SPI slave
> +        * @wordlen:    The number of bits in a word
> +        *
> +        * Returns: 0 on success, -ve on failure.
> +        */
> +       int (*set_wordlen)(struct udevice *deiuce, unsigned int wordlen);
> +
> +       /**
> +        * SPI transfer
> +        *
> +        * This writes "bitlen" bits out the SPI MOSI port and simultaneously
> +        * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
> +        * works.
> +        *
> +        * The source of the outgoing bits is the "dout" parameter and the
> +        * destination of the input bits is the "din" parameter.  Note that
> +        * "dout" and "din" can point to the same memory location, in which
> +        * case the input data overwrites the output data (since both are
> +        * buffered by temporary variables, this is OK).
> +        *
> +        * spi_xfer() interface:
> +        * @device:     The SPI bus which will be sending/receiving the data.
> +        * @slave:      The slave device to communicate with
> +        * @bitlen:     How many bits to write and read.
> +        * @dout:       Pointer to a string of bits to send out.  The bits are
> +        *              held in a byte array and are sent MSB first.
> +        * @din:        Pointer to a string of bits that will be filled in.
> +        * @flags:      A bitwise combination of SPI_XFER_* flags.
> +        *
> +        * Returns: 0 on success, not -1 on failure
> +        */
> +       int (*xfer)(struct udevice *device, struct udevice *slave,
> +                   unsigned int bitlen, const void *dout, void *din,
> +                   unsigned long flags);
> +
> +       /**
> +        * Set transfer speed.
> +        * This sets a new speed to be applied for next spi_xfer().
> +        * @slave:      The SPI slave
> +        * @hz:         The transfer speed
> +        * @return 0 if OK, -ve on error
> +        */
> +       int (*set_speed)(struct udevice *device, uint hz);
> +
> +       /**
> +        * Set the SPI mode/flags
> +        *
> +        * It is unclear if we want to set speed and mode together instead
> +        * of separately.
> +        *
> +        * @slave:      The SPI slave
> +        * @mode:       Requested SPI mode (SPI_... flags)
> +        * @return 0 if OK, -ve on error
> +        */
> +       int (*set_mode)(struct udevice *device, uint mode);
> +};
> +
> +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
> +                       struct udevice **devp);
> +
> +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
> +                       const char *drv_name, const char *dev_name,
> +                       struct udevice **devp, struct spi_slave **slavep);
> +
> +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
> +                   const char *dev_name, struct udevice **slavep);
> +
> +int spi_ofdata_to_platdata(const void *blob, int node,
> +                          struct spi_slave *spi);
> +
> +struct sandbox_state;
> +int sandbox_spi_get_emul(struct sandbox_state *state,
> +                        struct udevice *bus, struct udevice *slave,
> +                        struct udevice **emulp);
> +
> +/* Access the serial operations for a device */
> +#define spi_get_ops(dev)       ((struct dm_spi_ops *)(dev)->driver->ops)
> +#endif /* CONFIG_DM_SPI */
> +
>  #endif /* _SPI_H_ */
> --
> 2.0.0.526.g5318336
>

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers
  2014-07-15  0:56 ` [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers Simon Glass
@ 2014-08-28 11:32   ` Jagan Teki
  2014-09-01  5:06     ` Simon Glass
  0 siblings, 1 reply; 51+ messages in thread
From: Jagan Teki @ 2014-08-28 11:32 UTC (permalink / raw)
  To: u-boot

On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
> This README is intended to help maintainers move their SPI drivers over to
> driver model. It works through the required steps with an example.
>
> Signed-off-by: Simon Glass <sjg@chromium.org>
> ---
>
>  doc/driver-model/spi-howto.txt | 570 +++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 570 insertions(+)
>  create mode 100644 doc/driver-model/spi-howto.txt
>
> diff --git a/doc/driver-model/spi-howto.txt b/doc/driver-model/spi-howto.txt
> new file mode 100644
> index 0000000..bb64735
> --- /dev/null
> +++ b/doc/driver-model/spi-howto.txt
> @@ -0,0 +1,570 @@
> +How to port a SPI driver to driver model
> +========================================
> +
> +Here is a rough step-by-step guide. It is based around converting the
> +exynos SPI driver to driver model (DM) and the example code is based
> +around U-Boot v2014.04 (commit dda0dbf).
> +
> +It is quite long since it includes actual code examples.
> +
> +Before driver model, SPI drivers have their own private structure which
> +contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
> +exists, but now it is 'per-child data' for the SPI bus. Each child of the
> +SPI bus is a SPI slave. The information that was stored in the
> +driver-specific slave structure can now be put in private data for the
> +SPI bus.

Do you think spi_slave still require, I guess It's needed as some slave members
to cs, bus are used to control the driver.

But any specific reason for removing spi_slave from exynos_spi.c?

> +
> +For example, struct tegra_spi_slave looks like this:
> +
> +struct tegra_spi_slave {
> +       struct spi_slave slave;
> +       struct tegra_spi_ctrl *ctrl;
> +};
> +
> +In this case 'slave' will be in per-child data, and 'ctrl' will be in the
> +SPI bus' private data.
> +
> +
> +0. How long does this take?
> +
> +Around 2.5 hours, including some allowance for figuring out the driver
> +model bits.
> +
> +
> +1. Enable driver mode for SPI and SPI flash
> +
> +Add these to your board config:
> +
> +#define CONFIG_DM_SPI
> +#define CONFIG_DM_SPI_FLASH
> +
> +
> +2. Add the skeleton
> +
> +Put this code at the bottom of your existing driver file:
> +
> +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
> +                       unsigned int max_hz, unsigned int mode)
> +{
> +       return NULL;
> +}
> +
> +struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
> +                                     int spi_node)
> +{
> +       return NULL;
> +}
> +
> +static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
> +{
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_probe(struct udevice *dev)
> +{
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_remove(struct udevice *dev)
> +{
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_claim_bus(struct udevice *dev)
> +{
> +
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_release_bus(struct udevice *dev)
> +{
> +
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
> +                           const void *dout, void *din, unsigned long flags)
> +{
> +
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_set_speed(struct udevice *dev, uint speed)
> +{
> +       return -ENODEV;
> +}
> +
> +static int exynos_spi_set_mode(struct udevice *dev, uint mode)
> +{
> +       return -ENODEV;
> +}
> +
> +static const struct dm_spi_ops exynos_spi_ops = {
> +       .claim_bus      = exynos_spi_claim_bus,
> +       .release_bus    = exynos_spi_release_bus,
> +       .xfer           = exynos_spi_xfer,
> +       .set_speed      = exynos_spi_set_speed,
> +       .set_mode       = exynos_spi_set_mode,
> +};
> +
> +static const struct udevice_id exynos_spi_ids[] = {
> +       { .compatible = "samsung,exynos-spi" },
> +       { }
> +};
> +
> +U_BOOT_DRIVER(exynos_spi) = {
> +       .name   = "exynos_spi",
> +       .id     = UCLASS_SPI,
> +       .of_match = exynos_spi_ids,
> +       .ops    = &exynos_spi_ops,
> +       .ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
> +       .probe  = exynos_spi_probe,
> +       .remove = exynos_spi_remove

comma missing - remove is necessary?

> +};
> +
> +
> +3. Replace 'exynos' in the above code with your driver name
> +
> +
> +4. #ifdef out all of the code in your driver except for the above
> +
> +This will allow you to get it building, which means you can work
> +incrementally. Since all the methods return an error initially, there is
> +less chance that you will accidentally leave something in.
> +
> +Also, even though your conversion is basically a rewrite, it might help
> +reviewers if you leave functions in the same place in the file,
> +particularly for large drivers.
> +
> +
> +5. Add some includes
> +
> +Add these includes to your driver:
> +
> +#include <dm.h>
> +#include <errno.h>
> +
> +
> +6. Build
> +
> +At this point you should be able to build U-Boot for your board with the
> +empty SPI driver. You still have empty methods in yur driver, but we will

typo your instead of yur

> +write these one by one.
> +
> +If you have spi_init() functions or the like that are called from your
> +board then the build will fail. Remove these calls and make a note of the
> +init that needs to be done.
> +
> +
> +7. Set up your platform data structure
> +
> +This will hold the information your driver needs to operate, like its
> +hardware address or maximum frequency.
> +
> +You may already have a struct like this, or you may need to create one
> +from some of the #defines or global variables in the driver.
> +
> +Note that this information is not the run-time information. It should not
> +include state that changes. It should be fixed throughout the life of
> +U-Boot. Run-time information comes later.
> +
> +Here is what was in the exynos spi driver:
> +
> +struct spi_bus {
> +       enum periph_id periph_id;
> +       s32 frequency;          /* Default clock frequency, -1 for none */
> +       struct exynos_spi *regs;
> +       int inited;             /* 1 if this bus is ready for use */
> +       int node;
> +       uint deactivate_delay_us;       /* Delay to wait after deactivate */
> +};
> +
> +Of these, inited is handled by DM and node is the device tree node, which
> +DM tells you. The name is not quite right. So in this case we would use:
> +
> +struct exynos_spi_platdata {
> +       enum periph_id periph_id;
> +       s32 frequency;          /* Default clock frequency, -1 for none */
> +       struct exynos_spi *regs;
> +       uint deactivate_delay_us;       /* Delay to wait after deactivate */
> +};
> +
> +
> +8a. Write ofdata_to_platdata()   [for device tree only]
> +
> +This method will convert information in the device tree node into a C
> +structure in your driver (called platform data). If you are not using
> +device tree, go to 8b.
> +
> +DM will automatically allocate the struct for us when we are using device
> +tree, but we need to tell it the size:
> +
> +U_BOOT_DRIVER(spi_exynos) = {
> +...
> +       .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
> +
> +
> +Here is a sample function. It gets a pointer to the platform data and
> +fills in the fields from device tree.
> +
> +static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
> +{
> +       struct exynos_spi_platdata *plat = dev->platdata;
> +       const void *blob = gd->fdt_blob;
> +       int node = dev->of_offset;
> +
> +       plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
> +       plat->periph_id = pinmux_decode_periph_id(blob, node);
> +
> +       if (plat->periph_id == PERIPH_ID_NONE) {
> +               debug("%s: Invalid peripheral ID %d\n", __func__,
> +                       plat->periph_id);
> +               return -FDT_ERR_NOTFOUND;
> +       }
> +
> +       /* Use 500KHz as a suitable default */
> +       plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
> +                                       500000);
> +       plat->deactivate_delay_us = fdtdec_get_int(blob, node,
> +                                       "spi-deactivate-delay", 0);
> +
> +       return 0;
> +}
> +
> +
> +8b. Add the platform data  [non-device-tree only]
> +
> +Specify this data in a U_BOOT_DEVICE() declaration in your board file:
> +
> +struct exynos_spi_platdata platdata_spi0 = {
> +       .periph_id = ...
> +       .frequency = ...
> +       .regs = ...
> +       .deactivate_delay_us = ...
> +};
> +
> +U_BOOT_DEVICE(board_spi0) = {
> +       .name = "exynos_spi",
> +       .platdata = &platdata_spi0,
> +};
> +
> +You will unfortunately need to put the struct into a header file in this
> +case so that your board file can use it.
> +
> +
> +9. Add the device private data
> +
> +Most devices have some private data which they use to keep track of things
> +while active. This is the run-time information and needs to be stored in
> +a structure. There is probably a structure in the driver that includes a
> +'struct spi_slave', so you can use that.
> +
> +struct exynos_spi_slave {
> +       struct spi_slave slave;
> +       struct exynos_spi *regs;
> +       unsigned int freq;              /* Default frequency */
> +       unsigned int mode;
> +       enum periph_id periph_id;       /* Peripheral ID for this device */
> +       unsigned int fifo_size;
> +       int skip_preamble;
> +       struct spi_bus *bus;            /* Pointer to our SPI bus info */
> +       ulong last_transaction_us;      /* Time of last transaction end */
> +};
> +
> +
> +We should rename this to make its purpose more obvious, and get rid of
> +the slave structure, so we have:
> +
> +struct exynos_spi_priv {
> +       struct exynos_spi *regs;
> +       unsigned int freq;              /* Default frequency */
> +       unsigned int mode;
> +       enum periph_id periph_id;       /* Peripheral ID for this device */
> +       unsigned int fifo_size;
> +       int skip_preamble;
> +       ulong last_transaction_us;      /* Time of last transaction end */
> +};
> +
> +
> +DM can auto-allocate this also:
> +
> +U_BOOT_DRIVER(spi_exynos) = {
> +...
> +       .priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
> +
> +
> +Note that this is created before the probe() method is called, and destroyed
> +after the remove() method is called. It will be zeroed when the probe()
> +method is called.
> +
> +
> +10. Add the probe() and remove() methods
> +
> +Note: It's a good idea to build repeatedly as you are working, to avoid a
> +huge amount of work getting things compiling at the end.
> +
> +The probe() method is supposed to set up the hardware. U-Boot used to use
> +spi_setup_slave() to do this. So take a look at this function and see
> +what you can copy out to set things up.
> +
> +
> +static int exynos_spi_probe(struct udevice *dev)
> +{
> +       struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
> +
> +       if (pdata->periph_id == PERIPH_ID_SPI1 ||
> +           pdata->periph_id == PERIPH_ID_SPI2)
> +               priv->fifo_size = 64;
> +       else
> +               priv->fifo_size = 256;
> +
> +       priv->skip_preamble = 0;
> +       priv->last_transaction_us = timer_get_us();
> +       priv->freq = pdata->frequency;
> +       priv->periph_id = pdata->periph_id;
> +
> +       return 0;
> +}
> +
> +This implementation doesn't actually touch the hardware, which is somewhat
> +unusual for a driver. In this case we will do that when the device is
> +claimed by something that wants to use the SPI bus.
> +
> +For remove() we could shut down the clocks, but in this case there is
> +nothing to do. DM frees any memory that it allocated, so we can just
> +remove exynos_spi_remove() and its reference in U_BOOT_DRIVER.
> +
> +
> +11. Implement set_speed()
> +
> +This should set up clocks so that the SPI bus is running at the right
> +speed. With the old API spi_claim_bus() would normally do this and several
> +of the following functions, so let's look at that function:
> +
> +int spi_claim_bus(struct spi_slave *slave)
> +{
> +       struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
> +       struct exynos_spi *regs = spi_slave->regs;
> +       u32 reg = 0;
> +       int ret;
> +
> +       ret = set_spi_clk(spi_slave->periph_id,
> +                                       spi_slave->freq);
> +       if (ret < 0) {
> +               debug("%s: Failed to setup spi clock\n", __func__);
> +               return ret;
> +       }
> +
> +       exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
> +
> +       spi_flush_fifo(slave);
> +
> +       reg = readl(&regs->ch_cfg);
> +       reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
> +
> +       if (spi_slave->mode & SPI_CPHA)
> +               reg |= SPI_CH_CPHA_B;
> +
> +       if (spi_slave->mode & SPI_CPOL)
> +               reg |= SPI_CH_CPOL_L;
> +
> +       writel(reg, &regs->ch_cfg);
> +       writel(SPI_FB_DELAY_180, &regs->fb_clk);
> +
> +       return 0;
> +}
> +
> +
> +It sets up the speed, mode, pinmux, feedback delay and clears the FIFOs.
> +With DM these will happen in separate methods.
> +
> +
> +Here is an example for the speed part:
> +
> +static int exynos_spi_set_speed(struct udevice *dev, uint speed)
> +{
> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
> +       int ret;
> +
> +       ret = set_spi_clk(priv->periph_id, speed);
> +       if (ret)
> +               return ret;
> +       priv->freq = speed;
> +
> +       return 0;
> +}
> +
> +
> +12. Implement set_mode()
> +
> +This should adjust the SPI mode (polarity, etc.). Again this code probably
> +comes from the old spi_claim_bus(). Here is an example:
> +
> +
> +static int exynos_spi_set_mode(struct udevice *dev, uint mode)
> +{
> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
> +       uint32_t reg;
> +
> +       reg = readl(&priv->regs->ch_cfg);
> +       reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
> +
> +       if (mode & SPI_CPHA)
> +               reg |= SPI_CH_CPHA_B;
> +
> +       if (mode & SPI_CPOL)
> +               reg |= SPI_CH_CPOL_L;
> +
> +       writel(reg, &priv->regs->ch_cfg);
> +
> +       return 0;
> +}
> +
> +
> +13. Implement claim_bus()
> +
> +This is where a client wants to make use of the bus, so claims it first.
> +At this point we need to make sure every is set up ready for data transfer.
> +
> +Here again we look at the old claim function and see some code that is
> +needed. It is anything unrelated to speed and mode:
> +
> +static int exynos_spi_claim_bus(struct udevice *dev)
> +{
> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
> +
> +       exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
> +       spi_flush_fifo(priv->regs);
> +
> +       writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
> +
> +       return 0;
> +}
> +
> +The spi_flush_fifo() function is in the removed part of the code, so we
> +need to expose it again (perhaps with an #endif before it and '#if 0'
> +after it). It only needs accesss to priv->regs which is why we have
> +passed that in:
> +
> +/**
> + * Flush spi tx, rx fifos and reset the SPI controller
> + *
> + * @param regs Pointer to SPI registers
> + */
> +static void spi_flush_fifo(struct exynos_spi *regs)
> +{
> +       clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
> +       clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
> +       setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
> +}
> +
> +
> +14. Implement release_bus()
> +
> +This releases the bus - in our example the old code in spi_release_bus()
> +is a call to spi_flush_fifo, so we add:
> +
> +static int exynos_spi_release_bus(struct udevice *dev)
> +{
> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
> +
> +       spi_flush_fifo(priv->regs);
> +
> +       return 0;
> +}
> +
> +
> +15. Implement xfer()
> +
> +This is the final method that we need to create, and it is where all the
> +work happens. The method parameters are the same as the old spi_xfer() with
> +the addition of a 'struct udevice' so conversion is pretty easy. Start
> +by copying the contents of spi_xfer() to your new xfer() mthod and proceed

typo - method

> +from there.
> +
> +If (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an
> +activate function, something like this:
> +
> +void spi_cs_activate(struct spi_slave *slave)
> +{
> +       struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
> +
> +       /* If it's too soon to do another transaction, wait */
> +       if (spi_slave->bus->deactivate_delay_us &&
> +           spi_slave->last_transaction_us) {
> +               ulong delay_us;         /* The delay completed so far */
> +               delay_us = timer_get_us() - spi_slave->last_transaction_us;
> +               if (delay_us < spi_slave->bus->deactivate_delay_us)
> +                       udelay(spi_slave->bus->deactivate_delay_us - delay_us);
> +       }
> +
> +       clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
> +       debug("Activate CS, bus %d\n", spi_slave->slave.bus);
> +       spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
> +}
> +
> +The new version looks like this:
> +
> +static void spi_cs_activate(struct udevice *dev)
> +{
> +       struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
> +
> +       /* If it's too soon to do another transaction, wait */
> +       if (pdata->deactivate_delay_us &&
> +           priv->last_transaction_us) {
> +               ulong delay_us;         /* The delay completed so far */
> +               delay_us = timer_get_us() - priv->last_transaction_us;
> +               if (delay_us < pdata->deactivate_delay_us)
> +                       udelay(pdata->deactivate_delay_us - delay_us);
> +       }
> +
> +       clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
> +       debug("Activate CS, bus '%s'\n", dev->name);
> +       priv->skip_preamble = priv->mode & SPI_PREAMBLE;
> +}
> +
> +All we have really done here is change the pointers and print the device name
> +instead of the bus number. Other local static functions can be treated in
> +the same way.
> +
> +
> +16. Set up the per-child data and child_pre_probe() method
> +
> +To minimise the pain and complexity of the SPI subsystem while the driver

typo - minimize

> +model change-over is in place, struct spi_slave is used to reference a
> +SPI bus slave, even though that slave is actually a struct udevice. In fact
> +struct spi_slave is the device's child data. We need to make sure this is
> +set up. It is possible to allocate more space that struct spi_slave needs,
> +but this is the minimum.
> +
> +U_BOOT_DRIVER(exynos_spi) = {
> +...
> +       .per_child_auto_alloc_size      = sizeof(struct spi_slave),
> +       .child_pre_probe        = exynos_spi_child_pre_probe,
> +}
> +
> +int exynos_spi_child_pre_probe(struct udevice *dev)
> +{
> +       struct spi_slave *slave = dev_get_parentdata(dev);
> +
> +       return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
> +}

So this routine will fill spi_slave members - is it?
Why cs and bus are not part of DM?

> +
> +
> +Here our child_pre_probe() method merely calls a standard method to convert
> +device tree data to that used in the slave.
> +
> +
> +17. Test it
> +
> +Now that you have the code written and it compiles, try testing it using
> +the 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your
> +board.
> +
> +
> +18. Prepare patches and send them to the mailing lists
> +
> +You can use 'tools/patman/patman' to prepare, check and send patches for
> +your work. See the README for details.
> --
> 2.0.0.526.g5318336
>

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI
  2014-08-28  8:58   ` Jagan Teki
@ 2014-08-29 23:38     ` Simon Glass
  0 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-08-29 23:38 UTC (permalink / raw)
  To: u-boot

Hi Jagan,

On 28 August 2014 02:58, Jagan Teki <jagannadh.teki@gmail.com> wrote:
> On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
>> Add a uclass which provides access to SPI buses and includes operations
>> required by SPI.
>>
>> For a time driver model will need to co-exist with the legacy SPI interface
>> so some parts of the header file are changed depending on which is in use.
>> The exports are adjusted also since some functions are not available with
>> driver model.
>>
>> Boards must define CONFIG_DM_SPI to use driver model for SPI.
>>
>> Signed-off-by: Simon Glass <sjg@chromium.org>
>> ---
>>
>>  common/exports.c         |   4 +-
>>  drivers/spi/Makefile     |   4 +
>>  drivers/spi/spi-uclass.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++
>>  include/dm/uclass-id.h   |   1 +
>>  include/spi.h            | 140 ++++++++++++++++++++++++++
>>  5 files changed, 401 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/spi/spi-uclass.c
>>
>> diff --git a/common/exports.c b/common/exports.c
>> index b97ca48..88fcfc8 100644
>> --- a/common/exports.c
>> +++ b/common/exports.c
>> @@ -27,10 +27,12 @@ unsigned long get_version(void)
>>  # define i2c_write         dummy
>>  # define i2c_read          dummy
>>  #endif
>> -#ifndef CONFIG_CMD_SPI
>> +#if !defined(CONFIG_CMD_SPI) || defined(CONFIG_DM_SPI)
>>  # define spi_init          dummy
>>  # define spi_setup_slave   dummy
>>  # define spi_free_slave    dummy
>> +#endif
>> +#ifndef CONFIG_CMD_SPI
>>  # define spi_claim_bus     dummy
>>  # define spi_release_bus   dummy
>>  # define spi_xfer          dummy
>> diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
>> index f02c35a..d1f1dd0 100644
>> --- a/drivers/spi/Makefile
>> +++ b/drivers/spi/Makefile
>> @@ -6,7 +6,11 @@
>>  #
>>
>>  # There are many options which enable SPI, so make this library available
>> +ifdef CONFIG_DM_SPI
>> +obj-y += spi-uclass.o
>> +else
>>  obj-y += spi.o
>> +endif
>>
>>  obj-$(CONFIG_EP93XX_SPI) += ep93xx_spi.o
>>  obj-$(CONFIG_ALTERA_SPI) += altera_spi.o
>> diff --git a/drivers/spi/spi-uclass.c b/drivers/spi/spi-uclass.c
>> new file mode 100644
>> index 0000000..4057bce
>> --- /dev/null
>> +++ b/drivers/spi/spi-uclass.c
>> @@ -0,0 +1,253 @@
>> +/*
>> + * Copyright (c) 2014 Google, Inc
>> + *
>> + * SPDX-License-Identifier:    GPL-2.0+
>> + */
>> +
>> +#include <common.h>
>> +#include <dm.h>
>> +#include <errno.h>
>> +#include <fdtdec.h>
>> +#include <spi.h>
>> +#include <dm/device-internal.h>
>> +#include <dm/uclass-internal.h>
>> +#include <dm/root.h>
>> +#include <dm/lists.h>
>> +
>> +DECLARE_GLOBAL_DATA_PTR;
>> +
>> +static int spi_set_speed_mode(struct udevice *bus, int speed, int mode)
>> +{
>> +       struct dm_spi_ops *ops;
>> +       int ret;
>> +
>> +       ops = spi_get_ops(bus);
>> +       if (ops->set_speed)
>> +               ret = (*ops->set_speed)(bus, speed);
>> +       else
>> +               ret = -EINVAL;
>> +       if (ret) {
>> +               printf("Cannot set speed (err=%d)\n", ret);
>> +               return ret;
>> +       }
>> +
>> +       ops = spi_get_ops(bus);
>> +       if (ops->set_mode)
>> +               ret = (*ops->set_mode)(bus, mode);
>> +       else
>> +               ret = -EINVAL;
>> +       if (ret) {
>> +               printf("Cannot set mode (err=%d)\n", ret);
>> +               return ret;
>> +       }
>> +
>> +       return 0;
>> +}
>> +
>> +int spi_claim_bus(struct spi_slave *slave)
>> +{
>> +       struct udevice *dev = slave->dev;
>> +       struct udevice *bus = dev->parent;
>> +       struct dm_spi_ops *ops = spi_get_ops(bus);
>> +       struct dm_spi_bus *spi = bus->uclass_priv;
>> +       int speed;
>> +       int ret;
>> +
>> +       speed = slave->max_hz;
>> +       if (spi->max_hz) {
>> +               if (speed)
>> +                       speed = min(speed, spi->max_hz);
>> +               else
>> +                       speed = spi->max_hz;
>> +       }
>> +       if (!speed)
>> +               speed = 100000;
>> +       ret = spi_set_speed_mode(bus, speed, slave->mode);
>> +       if (ret)
>> +               return ret;
>> +
>> +       return ops->claim_bus ? ops->claim_bus(bus) : 0;
>> +}
>> +
>> +void spi_release_bus(struct spi_slave *slave)
>> +{
>> +       struct udevice *dev = slave->dev;
>> +       struct udevice *bus = dev->parent;
>> +       struct dm_spi_ops *ops = spi_get_ops(bus);
>> +
>> +       if (ops->release_bus)
>> +               spi_get_ops(bus)->release_bus(bus);
>> +}
>> +
>> +int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
>> +            const void *dout, void *din, unsigned long flags)
>> +{
>> +       struct udevice *dev = slave->dev;
>> +       struct udevice *bus = dev->parent;
>> +
>> +       if (bus->uclass->uc_drv->id != UCLASS_SPI)
>> +               return -EOPNOTSUPP;
>> +
>> +       return spi_get_ops(bus)->xfer(bus, dev, bitlen, dout, din, flags);
>> +}
>
> Cleared all these calls, as individual drivers/spi* will setup their
> ops and fills
> the priv data. So this uclass will call accordingly - correct? add if
> I'm missing anything.

Yes so far as I understand you, that is correct.

>
>> +
>> +int spi_post_bind(struct udevice *dev)
>> +{
>> +       /* Scan the bus for devices */
>> +       return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
>> +}
>> +
>> +int spi_post_probe(struct udevice *dev)
>> +{
>> +       struct dm_spi_bus *spi = dev->uclass_priv;
>> +
>> +       spi->max_hz = fdtdec_get_int(gd->fdt_blob, dev->of_offset,
>> +                                    "spi-max-frequency", 0);
>> +
>> +       return 0;
>> +}
>
> Anyway each spi driver will fills the data from dtb - like
> spi-max-frequency, why the spi
> core (uclass) will do the same.

Because the overall SPI peripheral has a maximum speed too. We should
allow it to run at a nominal speed, but slow down for particular
peripherals if needed.

>
> Can't we reuse the spi_slave, why dm_spi_bus?
>
>> +
>> +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
>> +                   const char *dev_name, struct udevice **slavep)
>> +{
>> +       struct driver *drv;
>> +       int ret;
>> +
>> +       drv = lists_driver_lookup_name(drv_name);
>> +       if (!drv) {
>> +               puts("Cannot find spi_flash_std driver\n");
>> +               return -ENOENT;
>> +       }
>> +       ret = device_bind(bus, drv, dev_name, NULL, -1, slavep);
>> +       if (ret) {
>> +               printf("Cannot create device named '%s' (err=%d)\n",
>> +                      dev_name, ret);
>> +               return ret;
>> +       }
>> +       (*slavep)->req_seq = cs;
>> +
>> +       return 0;
>> +}
>> +
>> +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
>> +                       struct udevice **devp)
>> +{
>> +       struct udevice *bus, *dev;
>> +       int ret;
>> +
>> +       ret = uclass_find_device_by_seq(UCLASS_SPI, busnum, false, &bus);
>> +       if (ret)
>> +               return ret;
>> +       ret = device_find_child_by_seq(bus, cs, false, &dev);
>> +       if (ret)
>> +               return ret;
>> +       *busp = bus;
>> +       *devp = dev;
>> +
>> +       return ret;
>> +}
>> +
>> +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
>> +                      const char *drv_name, const char *dev_name,
>> +                      struct udevice **devp, struct spi_slave **slavep)
>> +{
>> +       struct udevice *bus, *dev;
>> +       int ret;
>> +
>> +       ret = uclass_get_device_by_seq(UCLASS_SPI, busnum, &bus);
>> +       if (ret) {
>> +               printf("Invalid bus %d (err=%d)\n", busnum, ret);
>> +               return ret;
>> +       }
>> +       ret = device_get_child_by_seq(bus, cs, &dev);
>> +
>> +       /**
>> +        * If there is no such device, create one automatically. This means
>> +        * that we don't need a device tree node or platform data for the
>> +        * SPI flash chip - we will bind to the correct driver.
>> +        */
>> +       if (ret == -ENODEV && drv_name) {
>> +               ret = spi_bind_device(bus, cs, drv_name, dev_name, &dev);
>> +               if (ret)
>> +                       return ret;
>> +       }
>> +       if (ret) {
>> +               printf("Invalid chip select %d:%d (err=%d)\n", busnum, cs,
>> +                      ret);
>> +               return ret;
>> +       }
>> +
>> +       ret = spi_set_speed_mode(bus, speed, mode);
>> +       if (ret)
>> +               return ret;
>> +
>> +       *devp = bus;
>> +       *slavep = dev_get_parentdata(dev);
>> +
>> +       return 0;
>> +}
>
> I guess this is one of the replacement for spi_setup_slave() in spi drivers, so
> how bus and cs member will populate to drivers.
>
> Usually bus and cs members from spi_slave will populate to drivers through
> "sf probe" so spi_cs_activate will set the respective chip-select and
> along with
> reg_base in driver select through bus value.
>
> spi_setup_slave() {
> ...
> zslave->base = get_zynq_spi_base(bus);
> ...
> }
>
> How these will handling with dm?

The base should be in the device tree, or in platform data. The bus
number should not be visible to the driver.

>
>> +
>> +/* Compatibility function - to be removed */
>> +struct spi_slave *spi_setup_slave_fdt(const void *blob, int node,
>> +                                     int bus_node)
>> +{
>> +       struct udevice *bus, *dev;
>> +       int ret;
>> +
>> +       ret = uclass_get_device_by_of_offset(UCLASS_SPI, bus_node, &bus);
>> +       if (ret)
>> +               return NULL;
>> +       ret = device_get_child_by_of_offset(bus, node, &dev);
>> +       if (ret)
>> +               return NULL;
>> +       return dev_get_parentdata(dev);
>> +}
>> +
>> +/* Compatibility function - to be removed */
>> +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
>> +                                 unsigned int speed, unsigned int mode)
>> +{
>> +       struct spi_slave *slave;
>> +       struct udevice *dev;
>> +       int ret;
>> +
>> +       ret = spi_get_bus_and_cs(busnum, cs, speed, mode, NULL, 0, &dev,
>> +                                 &slave);
>> +       if (ret)
>> +               return NULL;
>> +
>> +       return slave;
>> +}
>> +
>> +void spi_free_slave(struct spi_slave *slave)
>> +{
>> +       device_remove(slave->dev);
>> +       slave->dev = NULL;
>> +}
>> +
>> +int spi_ofdata_to_platdata(const void *blob, int node,
>> +                          struct spi_slave *spi)
>> +{
>> +       int mode = 0;
>> +
>> +       spi->max_hz = fdtdec_get_int(blob, node, "spi-max-frequency", 0);
>> +       if (fdtdec_get_bool(blob, node, "spi-cpol"))
>> +               mode |= SPI_CPOL;
>> +       if (fdtdec_get_bool(blob, node, "spi-cpha"))
>> +               mode |= SPI_CPHA;
>> +       if (fdtdec_get_bool(blob, node, "spi-cs-high"))
>> +               mode |= SPI_CS_HIGH;
>> +       if (fdtdec_get_bool(blob, node, "spi-half-duplex"))
>> +               mode |= SPI_PREAMBLE;
>> +       spi->mode = mode;
>> +
>> +       return 0;
>> +}
>> +
>> +UCLASS_DRIVER(spi) = {
>> +       .id             = UCLASS_SPI,
>> +       .name           = "spi",
>> +       .post_bind      = spi_post_bind,
>> +       .post_probe     = spi_post_probe,
>> +       .per_device_auto_alloc_size = sizeof(struct dm_spi_bus),
>> +};
>> diff --git a/include/dm/uclass-id.h b/include/dm/uclass-id.h
>> index 7f0e37b..8207483 100644
>> --- a/include/dm/uclass-id.h
>> +++ b/include/dm/uclass-id.h
>> @@ -22,6 +22,7 @@ enum uclass_id {
>>         /* U-Boot uclasses start here */
>>         UCLASS_GPIO,            /* Bank of general-purpose I/O pins */
>>         UCLASS_SERIAL,          /* Serial UART */
>> +       UCLASS_SPI,             /* SPI bus */
>>
>>         UCLASS_COUNT,
>>         UCLASS_INVALID = -1,
>> diff --git a/include/spi.h b/include/spi.h
>> index b673be2..b5e9347 100644
>> --- a/include/spi.h
>> +++ b/include/spi.h
>> @@ -54,11 +54,19 @@
>>
>>  #define SPI_DEFAULT_WORDLEN 8
>>
>> +#ifdef CONFIG_DM_SPI
>> +struct dm_spi_bus {
>> +       uint max_hz;
>> +};
>> +
>> +#endif /* CONFIG_DM_SPI */
>> +
>
> No idea why this should require, max_hz is already a part of spi_slave
> and handling of these like getting from dtb and process and fill on priv
> data should be part of individual drivers and filling spi_slave member should
> be part of uclass - based on my understanding.
>
> Any comments?

See above - the bus has its own maximum speed in many cases.

>
>
>>  /**
>>   * struct spi_slave - Representation of a SPI slave
>>   *
>>   * Drivers are expected to extend this with controller-specific data.
>>   *
>> + * @dev:               SPI slave device
>
> Missing comments for  max_hz and mode
>
>>   * @bus:               ID of the bus that the slave is attached to.
>>   * @cs:                        ID of the chip select connected to the slave.
>>   * @op_mode_rx:                SPI RX operation mode.
>> @@ -71,8 +79,14 @@
>>   * @flags:             Indication of SPI flags.
>>   */
>>  struct spi_slave {
>> +#ifdef CONFIG_DM_SPI
>> +       struct udevice *dev;    /* struct spi_slave is dev->parentdata */
>> +       uint max_hz;
>> +       uint mode;
>> +#else
>>         unsigned int bus;
>>         unsigned int cs;
>> +#endif
>>         u8 op_mode_rx;
>>         u8 op_mode_tx;
>>         unsigned int wordlen;
>> @@ -220,6 +234,8 @@ int spi_set_wordlen(struct spi_slave *slave, unsigned int wordlen);
>>  int  spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
>>                 void *din, unsigned long flags);
>>
>> +#ifndef CONFIG_DM_SPI
>> +
>>  /**
>>   * Determine if a SPI chipselect is valid.
>>   * This function is provided by the board if the low-level SPI driver
>> @@ -255,6 +271,7 @@ void spi_cs_deactivate(struct spi_slave *slave);
>>   * @hz:                The transfer speed
>>   */
>>  void spi_set_speed(struct spi_slave *slave, uint hz);
>> +#endif
>>
>>  /**
>>   * Write 8 bits, then read 8 bits.
>> @@ -305,4 +322,127 @@ struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
>>  struct spi_slave *spi_base_setup_slave_fdt(const void *blob, int busnum,
>>                                            int node);
>>
>> +#ifdef CONFIG_DM_SPI
>> +
>> +/**
>> + * struct struct dm_spi_ops - Driver model SPI operations
>> + *
>> + * The uclass interface is implemented by all SPI devices which use
>> + * driver model.
>> + */
>> +struct dm_spi_ops {
>> +       /**
>> +        * Claim the bus and prepare it for communication.
>> +        *
>> +        * The device privided is the slave device. It's parent controller
>> +        * will be used to provide the communication.
>> +        *
>> +        * This must be called before doing any transfers with a SPI slave. It
>> +        * will enable and initialize any SPI hardware as necessary, and make
>> +        * sure that the SCK line is in the correct idle state. It is not
>> +        * allowed to claim the same bus for several slaves without releasing
>> +        * the bus in between.
>> +        *
>> +        * @device:     The SPI slave
>> +        *
>> +        * Returns: 0 if the bus was claimed successfully, or a negative value
>> +        * if it wasn't.
>> +        */
>> +       int (*claim_bus)(struct udevice *device);
>> +
>> +       /**
>> +        * Release the SPI bus
>> +        *
>> +        * This must be called once for every call to spi_claim_bus() after
>> +        * all transfers have finished. It may disable any SPI hardware as
>> +        * appropriate.
>> +        *
>> +        * @device:     The SPI slave
>> +        */
>> +       int (*release_bus)(struct udevice *device);
>> +
>> +       /**
>> +        * Set the word length for SPI transactions
>> +        *
>> +        * Set the word length (number of bits per word) for SPI transactions.
>> +        *
>> +        * @device:     The SPI slave
>> +        * @wordlen:    The number of bits in a word
>> +        *
>> +        * Returns: 0 on success, -ve on failure.
>> +        */
>> +       int (*set_wordlen)(struct udevice *deiuce, unsigned int wordlen);
>> +
>> +       /**
>> +        * SPI transfer
>> +        *
>> +        * This writes "bitlen" bits out the SPI MOSI port and simultaneously
>> +        * clocks "bitlen" bits in the SPI MISO port.  That's just the way SPI
>> +        * works.
>> +        *
>> +        * The source of the outgoing bits is the "dout" parameter and the
>> +        * destination of the input bits is the "din" parameter.  Note that
>> +        * "dout" and "din" can point to the same memory location, in which
>> +        * case the input data overwrites the output data (since both are
>> +        * buffered by temporary variables, this is OK).
>> +        *
>> +        * spi_xfer() interface:
>> +        * @device:     The SPI bus which will be sending/receiving the data.
>> +        * @slave:      The slave device to communicate with
>> +        * @bitlen:     How many bits to write and read.
>> +        * @dout:       Pointer to a string of bits to send out.  The bits are
>> +        *              held in a byte array and are sent MSB first.
>> +        * @din:        Pointer to a string of bits that will be filled in.
>> +        * @flags:      A bitwise combination of SPI_XFER_* flags.
>> +        *
>> +        * Returns: 0 on success, not -1 on failure
>> +        */
>> +       int (*xfer)(struct udevice *device, struct udevice *slave,
>> +                   unsigned int bitlen, const void *dout, void *din,
>> +                   unsigned long flags);
>> +
>> +       /**
>> +        * Set transfer speed.
>> +        * This sets a new speed to be applied for next spi_xfer().
>> +        * @slave:      The SPI slave
>> +        * @hz:         The transfer speed
>> +        * @return 0 if OK, -ve on error
>> +        */
>> +       int (*set_speed)(struct udevice *device, uint hz);
>> +
>> +       /**
>> +        * Set the SPI mode/flags
>> +        *
>> +        * It is unclear if we want to set speed and mode together instead
>> +        * of separately.
>> +        *
>> +        * @slave:      The SPI slave
>> +        * @mode:       Requested SPI mode (SPI_... flags)
>> +        * @return 0 if OK, -ve on error
>> +        */
>> +       int (*set_mode)(struct udevice *device, uint mode);
>> +};
>> +
>> +int spi_find_bus_and_cs(int busnum, int cs, struct udevice **busp,
>> +                       struct udevice **devp);
>> +
>> +int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
>> +                       const char *drv_name, const char *dev_name,
>> +                       struct udevice **devp, struct spi_slave **slavep);
>> +
>> +int spi_bind_device(struct udevice *bus, int cs, const char *drv_name,
>> +                   const char *dev_name, struct udevice **slavep);
>> +
>> +int spi_ofdata_to_platdata(const void *blob, int node,
>> +                          struct spi_slave *spi);
>> +
>> +struct sandbox_state;
>> +int sandbox_spi_get_emul(struct sandbox_state *state,
>> +                        struct udevice *bus, struct udevice *slave,
>> +                        struct udevice **emulp);
>> +
>> +/* Access the serial operations for a device */
>> +#define spi_get_ops(dev)       ((struct dm_spi_ops *)(dev)->driver->ops)
>> +#endif /* CONFIG_DM_SPI */
>> +
>>  #endif /* _SPI_H_ */
>> --
>> 2.0.0.526.g5318336
>>
>
> thanks!
> --
> Jagan.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers
  2014-08-28 11:32   ` Jagan Teki
@ 2014-09-01  5:06     ` Simon Glass
  2014-09-01  6:45       ` Jagan Teki
  0 siblings, 1 reply; 51+ messages in thread
From: Simon Glass @ 2014-09-01  5:06 UTC (permalink / raw)
  To: u-boot

Hi Jagan,

On 28 August 2014 04:32, Jagan Teki <jagannadh.teki@gmail.com> wrote:
> On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
>> This README is intended to help maintainers move their SPI drivers over to
>> driver model. It works through the required steps with an example.
>>
>> Signed-off-by: Simon Glass <sjg@chromium.org>
>> ---
>>
>>  doc/driver-model/spi-howto.txt | 570 +++++++++++++++++++++++++++++++++++++++++
>>  1 file changed, 570 insertions(+)
>>  create mode 100644 doc/driver-model/spi-howto.txt
>>
>> diff --git a/doc/driver-model/spi-howto.txt b/doc/driver-model/spi-howto.txt
>> new file mode 100644
>> index 0000000..bb64735
>> --- /dev/null
>> +++ b/doc/driver-model/spi-howto.txt
>> @@ -0,0 +1,570 @@
>> +How to port a SPI driver to driver model
>> +========================================
>> +
>> +Here is a rough step-by-step guide. It is based around converting the
>> +exynos SPI driver to driver model (DM) and the example code is based
>> +around U-Boot v2014.04 (commit dda0dbf).
>> +
>> +It is quite long since it includes actual code examples.
>> +
>> +Before driver model, SPI drivers have their own private structure which
>> +contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
>> +exists, but now it is 'per-child data' for the SPI bus. Each child of the
>> +SPI bus is a SPI slave. The information that was stored in the
>> +driver-specific slave structure can now be put in private data for the
>> +SPI bus.
>
> Do you think spi_slave still require, I guess It's needed as some slave members
> to cs, bus are used to control the driver.

The CS and bus are purely command-line conveniences with DM. When it
gets down to the driver the bus is determined by the SPI peripheral it
is connected to, and the CS is the control that it adjusts to enable
the chip select. The numbering of buses and chip selects is not
relevant down at the driver level anymore.

>
> But any specific reason for removing spi_slave from exynos_spi.c?

The driver has its own private data, there is no longer a need to
store the SPI subsystem's data there also. That is what the per-child
data is for.

>
>> +
>> +For example, struct tegra_spi_slave looks like this:
>> +
>> +struct tegra_spi_slave {
>> +       struct spi_slave slave;
>> +       struct tegra_spi_ctrl *ctrl;
>> +};
>> +
>> +In this case 'slave' will be in per-child data, and 'ctrl' will be in the
>> +SPI bus' private data.
>> +
>> +
>> +0. How long does this take?
>> +
>> +Around 2.5 hours, including some allowance for figuring out the driver
>> +model bits.
>> +
>> +
>> +1. Enable driver mode for SPI and SPI flash
>> +
>> +Add these to your board config:
>> +
>> +#define CONFIG_DM_SPI
>> +#define CONFIG_DM_SPI_FLASH
>> +
>> +
>> +2. Add the skeleton
>> +
>> +Put this code at the bottom of your existing driver file:
>> +
>> +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
>> +                       unsigned int max_hz, unsigned int mode)
>> +{
>> +       return NULL;
>> +}
>> +
>> +struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
>> +                                     int spi_node)
>> +{
>> +       return NULL;
>> +}
>> +
>> +static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
>> +{
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_probe(struct udevice *dev)
>> +{
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_remove(struct udevice *dev)
>> +{
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_claim_bus(struct udevice *dev)
>> +{
>> +
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_release_bus(struct udevice *dev)
>> +{
>> +
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
>> +                           const void *dout, void *din, unsigned long flags)
>> +{
>> +
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_set_speed(struct udevice *dev, uint speed)
>> +{
>> +       return -ENODEV;
>> +}
>> +
>> +static int exynos_spi_set_mode(struct udevice *dev, uint mode)
>> +{
>> +       return -ENODEV;
>> +}
>> +
>> +static const struct dm_spi_ops exynos_spi_ops = {
>> +       .claim_bus      = exynos_spi_claim_bus,
>> +       .release_bus    = exynos_spi_release_bus,
>> +       .xfer           = exynos_spi_xfer,
>> +       .set_speed      = exynos_spi_set_speed,
>> +       .set_mode       = exynos_spi_set_mode,
>> +};
>> +
>> +static const struct udevice_id exynos_spi_ids[] = {
>> +       { .compatible = "samsung,exynos-spi" },
>> +       { }
>> +};
>> +
>> +U_BOOT_DRIVER(exynos_spi) = {
>> +       .name   = "exynos_spi",
>> +       .id     = UCLASS_SPI,
>> +       .of_match = exynos_spi_ids,
>> +       .ops    = &exynos_spi_ops,
>> +       .ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
>> +       .probe  = exynos_spi_probe,
>> +       .remove = exynos_spi_remove
>
> comma missing - remove is necessary?
>
>> +};
>> +
>> +
>> +3. Replace 'exynos' in the above code with your driver name
>> +
>> +
>> +4. #ifdef out all of the code in your driver except for the above
>> +
>> +This will allow you to get it building, which means you can work
>> +incrementally. Since all the methods return an error initially, there is
>> +less chance that you will accidentally leave something in.
>> +
>> +Also, even though your conversion is basically a rewrite, it might help
>> +reviewers if you leave functions in the same place in the file,
>> +particularly for large drivers.
>> +
>> +
>> +5. Add some includes
>> +
>> +Add these includes to your driver:
>> +
>> +#include <dm.h>
>> +#include <errno.h>
>> +
>> +
>> +6. Build
>> +
>> +At this point you should be able to build U-Boot for your board with the
>> +empty SPI driver. You still have empty methods in yur driver, but we will
>
> typo your instead of yur
>
>> +write these one by one.
>> +
>> +If you have spi_init() functions or the like that are called from your
>> +board then the build will fail. Remove these calls and make a note of the
>> +init that needs to be done.
>> +
>> +
>> +7. Set up your platform data structure
>> +
>> +This will hold the information your driver needs to operate, like its
>> +hardware address or maximum frequency.
>> +
>> +You may already have a struct like this, or you may need to create one
>> +from some of the #defines or global variables in the driver.
>> +
>> +Note that this information is not the run-time information. It should not
>> +include state that changes. It should be fixed throughout the life of
>> +U-Boot. Run-time information comes later.
>> +
>> +Here is what was in the exynos spi driver:
>> +
>> +struct spi_bus {
>> +       enum periph_id periph_id;
>> +       s32 frequency;          /* Default clock frequency, -1 for none */
>> +       struct exynos_spi *regs;
>> +       int inited;             /* 1 if this bus is ready for use */
>> +       int node;
>> +       uint deactivate_delay_us;       /* Delay to wait after deactivate */
>> +};
>> +
>> +Of these, inited is handled by DM and node is the device tree node, which
>> +DM tells you. The name is not quite right. So in this case we would use:
>> +
>> +struct exynos_spi_platdata {
>> +       enum periph_id periph_id;
>> +       s32 frequency;          /* Default clock frequency, -1 for none */
>> +       struct exynos_spi *regs;
>> +       uint deactivate_delay_us;       /* Delay to wait after deactivate */
>> +};
>> +
>> +
>> +8a. Write ofdata_to_platdata()   [for device tree only]
>> +
>> +This method will convert information in the device tree node into a C
>> +structure in your driver (called platform data). If you are not using
>> +device tree, go to 8b.
>> +
>> +DM will automatically allocate the struct for us when we are using device
>> +tree, but we need to tell it the size:
>> +
>> +U_BOOT_DRIVER(spi_exynos) = {
>> +...
>> +       .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
>> +
>> +
>> +Here is a sample function. It gets a pointer to the platform data and
>> +fills in the fields from device tree.
>> +
>> +static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
>> +{
>> +       struct exynos_spi_platdata *plat = dev->platdata;
>> +       const void *blob = gd->fdt_blob;
>> +       int node = dev->of_offset;
>> +
>> +       plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
>> +       plat->periph_id = pinmux_decode_periph_id(blob, node);
>> +
>> +       if (plat->periph_id == PERIPH_ID_NONE) {
>> +               debug("%s: Invalid peripheral ID %d\n", __func__,
>> +                       plat->periph_id);
>> +               return -FDT_ERR_NOTFOUND;
>> +       }
>> +
>> +       /* Use 500KHz as a suitable default */
>> +       plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
>> +                                       500000);
>> +       plat->deactivate_delay_us = fdtdec_get_int(blob, node,
>> +                                       "spi-deactivate-delay", 0);
>> +
>> +       return 0;
>> +}
>> +
>> +
>> +8b. Add the platform data  [non-device-tree only]
>> +
>> +Specify this data in a U_BOOT_DEVICE() declaration in your board file:
>> +
>> +struct exynos_spi_platdata platdata_spi0 = {
>> +       .periph_id = ...
>> +       .frequency = ...
>> +       .regs = ...
>> +       .deactivate_delay_us = ...
>> +};
>> +
>> +U_BOOT_DEVICE(board_spi0) = {
>> +       .name = "exynos_spi",
>> +       .platdata = &platdata_spi0,
>> +};
>> +
>> +You will unfortunately need to put the struct into a header file in this
>> +case so that your board file can use it.
>> +
>> +
>> +9. Add the device private data
>> +
>> +Most devices have some private data which they use to keep track of things
>> +while active. This is the run-time information and needs to be stored in
>> +a structure. There is probably a structure in the driver that includes a
>> +'struct spi_slave', so you can use that.
>> +
>> +struct exynos_spi_slave {
>> +       struct spi_slave slave;
>> +       struct exynos_spi *regs;
>> +       unsigned int freq;              /* Default frequency */
>> +       unsigned int mode;
>> +       enum periph_id periph_id;       /* Peripheral ID for this device */
>> +       unsigned int fifo_size;
>> +       int skip_preamble;
>> +       struct spi_bus *bus;            /* Pointer to our SPI bus info */
>> +       ulong last_transaction_us;      /* Time of last transaction end */
>> +};
>> +
>> +
>> +We should rename this to make its purpose more obvious, and get rid of
>> +the slave structure, so we have:
>> +
>> +struct exynos_spi_priv {
>> +       struct exynos_spi *regs;
>> +       unsigned int freq;              /* Default frequency */
>> +       unsigned int mode;
>> +       enum periph_id periph_id;       /* Peripheral ID for this device */
>> +       unsigned int fifo_size;
>> +       int skip_preamble;
>> +       ulong last_transaction_us;      /* Time of last transaction end */
>> +};
>> +
>> +
>> +DM can auto-allocate this also:
>> +
>> +U_BOOT_DRIVER(spi_exynos) = {
>> +...
>> +       .priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
>> +
>> +
>> +Note that this is created before the probe() method is called, and destroyed
>> +after the remove() method is called. It will be zeroed when the probe()
>> +method is called.
>> +
>> +
>> +10. Add the probe() and remove() methods
>> +
>> +Note: It's a good idea to build repeatedly as you are working, to avoid a
>> +huge amount of work getting things compiling at the end.
>> +
>> +The probe() method is supposed to set up the hardware. U-Boot used to use
>> +spi_setup_slave() to do this. So take a look at this function and see
>> +what you can copy out to set things up.
>> +
>> +
>> +static int exynos_spi_probe(struct udevice *dev)
>> +{
>> +       struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>> +
>> +       if (pdata->periph_id == PERIPH_ID_SPI1 ||
>> +           pdata->periph_id == PERIPH_ID_SPI2)
>> +               priv->fifo_size = 64;
>> +       else
>> +               priv->fifo_size = 256;
>> +
>> +       priv->skip_preamble = 0;
>> +       priv->last_transaction_us = timer_get_us();
>> +       priv->freq = pdata->frequency;
>> +       priv->periph_id = pdata->periph_id;
>> +
>> +       return 0;
>> +}
>> +
>> +This implementation doesn't actually touch the hardware, which is somewhat
>> +unusual for a driver. In this case we will do that when the device is
>> +claimed by something that wants to use the SPI bus.
>> +
>> +For remove() we could shut down the clocks, but in this case there is
>> +nothing to do. DM frees any memory that it allocated, so we can just
>> +remove exynos_spi_remove() and its reference in U_BOOT_DRIVER.
>> +
>> +
>> +11. Implement set_speed()
>> +
>> +This should set up clocks so that the SPI bus is running at the right
>> +speed. With the old API spi_claim_bus() would normally do this and several
>> +of the following functions, so let's look at that function:
>> +
>> +int spi_claim_bus(struct spi_slave *slave)
>> +{
>> +       struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
>> +       struct exynos_spi *regs = spi_slave->regs;
>> +       u32 reg = 0;
>> +       int ret;
>> +
>> +       ret = set_spi_clk(spi_slave->periph_id,
>> +                                       spi_slave->freq);
>> +       if (ret < 0) {
>> +               debug("%s: Failed to setup spi clock\n", __func__);
>> +               return ret;
>> +       }
>> +
>> +       exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
>> +
>> +       spi_flush_fifo(slave);
>> +
>> +       reg = readl(&regs->ch_cfg);
>> +       reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
>> +
>> +       if (spi_slave->mode & SPI_CPHA)
>> +               reg |= SPI_CH_CPHA_B;
>> +
>> +       if (spi_slave->mode & SPI_CPOL)
>> +               reg |= SPI_CH_CPOL_L;
>> +
>> +       writel(reg, &regs->ch_cfg);
>> +       writel(SPI_FB_DELAY_180, &regs->fb_clk);
>> +
>> +       return 0;
>> +}
>> +
>> +
>> +It sets up the speed, mode, pinmux, feedback delay and clears the FIFOs.
>> +With DM these will happen in separate methods.
>> +
>> +
>> +Here is an example for the speed part:
>> +
>> +static int exynos_spi_set_speed(struct udevice *dev, uint speed)
>> +{
>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>> +       int ret;
>> +
>> +       ret = set_spi_clk(priv->periph_id, speed);
>> +       if (ret)
>> +               return ret;
>> +       priv->freq = speed;
>> +
>> +       return 0;
>> +}
>> +
>> +
>> +12. Implement set_mode()
>> +
>> +This should adjust the SPI mode (polarity, etc.). Again this code probably
>> +comes from the old spi_claim_bus(). Here is an example:
>> +
>> +
>> +static int exynos_spi_set_mode(struct udevice *dev, uint mode)
>> +{
>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>> +       uint32_t reg;
>> +
>> +       reg = readl(&priv->regs->ch_cfg);
>> +       reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
>> +
>> +       if (mode & SPI_CPHA)
>> +               reg |= SPI_CH_CPHA_B;
>> +
>> +       if (mode & SPI_CPOL)
>> +               reg |= SPI_CH_CPOL_L;
>> +
>> +       writel(reg, &priv->regs->ch_cfg);
>> +
>> +       return 0;
>> +}
>> +
>> +
>> +13. Implement claim_bus()
>> +
>> +This is where a client wants to make use of the bus, so claims it first.
>> +At this point we need to make sure every is set up ready for data transfer.
>> +
>> +Here again we look at the old claim function and see some code that is
>> +needed. It is anything unrelated to speed and mode:
>> +
>> +static int exynos_spi_claim_bus(struct udevice *dev)
>> +{
>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>> +
>> +       exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
>> +       spi_flush_fifo(priv->regs);
>> +
>> +       writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
>> +
>> +       return 0;
>> +}
>> +
>> +The spi_flush_fifo() function is in the removed part of the code, so we
>> +need to expose it again (perhaps with an #endif before it and '#if 0'
>> +after it). It only needs accesss to priv->regs which is why we have
>> +passed that in:
>> +
>> +/**
>> + * Flush spi tx, rx fifos and reset the SPI controller
>> + *
>> + * @param regs Pointer to SPI registers
>> + */
>> +static void spi_flush_fifo(struct exynos_spi *regs)
>> +{
>> +       clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
>> +       clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
>> +       setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
>> +}
>> +
>> +
>> +14. Implement release_bus()
>> +
>> +This releases the bus - in our example the old code in spi_release_bus()
>> +is a call to spi_flush_fifo, so we add:
>> +
>> +static int exynos_spi_release_bus(struct udevice *dev)
>> +{
>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>> +
>> +       spi_flush_fifo(priv->regs);
>> +
>> +       return 0;
>> +}
>> +
>> +
>> +15. Implement xfer()
>> +
>> +This is the final method that we need to create, and it is where all the
>> +work happens. The method parameters are the same as the old spi_xfer() with
>> +the addition of a 'struct udevice' so conversion is pretty easy. Start
>> +by copying the contents of spi_xfer() to your new xfer() mthod and proceed
>
> typo - method
>
>> +from there.
>> +
>> +If (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an
>> +activate function, something like this:
>> +
>> +void spi_cs_activate(struct spi_slave *slave)
>> +{
>> +       struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
>> +
>> +       /* If it's too soon to do another transaction, wait */
>> +       if (spi_slave->bus->deactivate_delay_us &&
>> +           spi_slave->last_transaction_us) {
>> +               ulong delay_us;         /* The delay completed so far */
>> +               delay_us = timer_get_us() - spi_slave->last_transaction_us;
>> +               if (delay_us < spi_slave->bus->deactivate_delay_us)
>> +                       udelay(spi_slave->bus->deactivate_delay_us - delay_us);
>> +       }
>> +
>> +       clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
>> +       debug("Activate CS, bus %d\n", spi_slave->slave.bus);
>> +       spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
>> +}
>> +
>> +The new version looks like this:
>> +
>> +static void spi_cs_activate(struct udevice *dev)
>> +{
>> +       struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>> +
>> +       /* If it's too soon to do another transaction, wait */
>> +       if (pdata->deactivate_delay_us &&
>> +           priv->last_transaction_us) {
>> +               ulong delay_us;         /* The delay completed so far */
>> +               delay_us = timer_get_us() - priv->last_transaction_us;
>> +               if (delay_us < pdata->deactivate_delay_us)
>> +                       udelay(pdata->deactivate_delay_us - delay_us);
>> +       }
>> +
>> +       clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
>> +       debug("Activate CS, bus '%s'\n", dev->name);
>> +       priv->skip_preamble = priv->mode & SPI_PREAMBLE;
>> +}
>> +
>> +All we have really done here is change the pointers and print the device name
>> +instead of the bus number. Other local static functions can be treated in
>> +the same way.
>> +
>> +
>> +16. Set up the per-child data and child_pre_probe() method
>> +
>> +To minimise the pain and complexity of the SPI subsystem while the driver
>
> typo - minimize

English spelling (this is U-Boot).

>
>> +model change-over is in place, struct spi_slave is used to reference a
>> +SPI bus slave, even though that slave is actually a struct udevice. In fact
>> +struct spi_slave is the device's child data. We need to make sure this is
>> +set up. It is possible to allocate more space that struct spi_slave needs,
>> +but this is the minimum.
>> +
>> +U_BOOT_DRIVER(exynos_spi) = {
>> +...
>> +       .per_child_auto_alloc_size      = sizeof(struct spi_slave),
>> +       .child_pre_probe        = exynos_spi_child_pre_probe,
>> +}
>> +
>> +int exynos_spi_child_pre_probe(struct udevice *dev)
>> +{
>> +       struct spi_slave *slave = dev_get_parentdata(dev);
>> +
>> +       return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
>> +}
>
> So this routine will fill spi_slave members - is it?
> Why cs and bus are not part of DM?

See above for my comments, but let me know if unsure.

>
>> +
>> +
>> +Here our child_pre_probe() method merely calls a standard method to convert
>> +device tree data to that used in the slave.
>> +
>> +
>> +17. Test it
>> +
>> +Now that you have the code written and it compiles, try testing it using
>> +the 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your
>> +board.
>> +
>> +
>> +18. Prepare patches and send them to the mailing lists
>> +
>> +You can use 'tools/patman/patman' to prepare, check and send patches for
>> +your work. See the README for details.
>> --
>> 2.0.0.526.g5318336
>>
>
> thanks!
> --
> Jagan.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers
  2014-09-01  5:06     ` Simon Glass
@ 2014-09-01  6:45       ` Jagan Teki
  2014-09-02  0:24         ` Simon Glass
  0 siblings, 1 reply; 51+ messages in thread
From: Jagan Teki @ 2014-09-01  6:45 UTC (permalink / raw)
  To: u-boot

Hi Simon,

On 1 September 2014 10:36, Simon Glass <sjg@chromium.org> wrote:
> Hi Jagan,
>
> On 28 August 2014 04:32, Jagan Teki <jagannadh.teki@gmail.com> wrote:
>> On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
>>> This README is intended to help maintainers move their SPI drivers over to
>>> driver model. It works through the required steps with an example.
>>>
>>> Signed-off-by: Simon Glass <sjg@chromium.org>
>>> ---
>>>
>>>  doc/driver-model/spi-howto.txt | 570 +++++++++++++++++++++++++++++++++++++++++
>>>  1 file changed, 570 insertions(+)
>>>  create mode 100644 doc/driver-model/spi-howto.txt
>>>
>>> diff --git a/doc/driver-model/spi-howto.txt b/doc/driver-model/spi-howto.txt
>>> new file mode 100644
>>> index 0000000..bb64735
>>> --- /dev/null
>>> +++ b/doc/driver-model/spi-howto.txt
>>> @@ -0,0 +1,570 @@
>>> +How to port a SPI driver to driver model
>>> +========================================
>>> +
>>> +Here is a rough step-by-step guide. It is based around converting the
>>> +exynos SPI driver to driver model (DM) and the example code is based
>>> +around U-Boot v2014.04 (commit dda0dbf).
>>> +
>>> +It is quite long since it includes actual code examples.
>>> +
>>> +Before driver model, SPI drivers have their own private structure which
>>> +contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
>>> +exists, but now it is 'per-child data' for the SPI bus. Each child of the
>>> +SPI bus is a SPI slave. The information that was stored in the
>>> +driver-specific slave structure can now be put in private data for the
>>> +SPI bus.
>>
>> Do you think spi_slave still require, I guess It's needed as some slave members
>> to cs, bus are used to control the driver.
>
> The CS and bus are purely command-line conveniences with DM. When it
> gets down to the driver the bus is determined by the SPI peripheral it
> is connected to, and the CS is the control that it adjusts to enable
> the chip select. The numbering of buses and chip selects is not
> relevant down at the driver level anymore.

Yes - I understand but let me explain my comments.

u-boot> sf probe bus:cs speed mode

Unlike other buses/controllers on u-boot we're dynamically probing the
slave on bus
using bus and cs for example, we have a zynq_spi handling spi0, spi1
(bus's) and each
one has 3 chip-selects. So

u-boot> sf probe 1:1
for probing bus1 (spi1) and cs 1

u-boot> sf probe 0:1
for probing bus0 (spi0) and cs 1

The respective reg base (based on bus) and register chip-select (based
on cs) zynq_spi.c
will handle these in driver it self.

speed and mode settings, I'm fine with dm, where it will configure using

int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
                       const char *drv_name, const char *dev_name,
                       struct udevice **devp, struct spi_slave **slavep)
{
.....
ret = spi_set_speed_mode(bus, speed, mode);
......
}

But how can spi-uclass.c will identifies how many bus's along with cs lines does
specified controller driver support.

This what usually we're doing in driver as
int spi_cs_is_valid(unsigned int bus, unsigned int cs)
{
        /* 2 bus with 3 chipselect */
        return bus < 2 && cs < 3;
}

Please let me know if you have any questions, this is what usually
doing most of drivers.

>
>>
>> But any specific reason for removing spi_slave from exynos_spi.c?
>
> The driver has its own private data, there is no longer a need to
> store the SPI subsystem's data there also. That is what the per-child
> data is for.
>
>>
>>> +
>>> +For example, struct tegra_spi_slave looks like this:
>>> +
>>> +struct tegra_spi_slave {
>>> +       struct spi_slave slave;
>>> +       struct tegra_spi_ctrl *ctrl;
>>> +};
>>> +
>>> +In this case 'slave' will be in per-child data, and 'ctrl' will be in the
>>> +SPI bus' private data.
>>> +
>>> +
>>> +0. How long does this take?
>>> +
>>> +Around 2.5 hours, including some allowance for figuring out the driver
>>> +model bits.
>>> +
>>> +
>>> +1. Enable driver mode for SPI and SPI flash
>>> +
>>> +Add these to your board config:
>>> +
>>> +#define CONFIG_DM_SPI
>>> +#define CONFIG_DM_SPI_FLASH
>>> +
>>> +
>>> +2. Add the skeleton
>>> +
>>> +Put this code at the bottom of your existing driver file:
>>> +
>>> +struct spi_slave *spi_setup_slave(unsigned int busnum, unsigned int cs,
>>> +                       unsigned int max_hz, unsigned int mode)
>>> +{
>>> +       return NULL;
>>> +}
>>> +
>>> +struct spi_slave *spi_setup_slave_fdt(const void *blob, int slave_node,
>>> +                                     int spi_node)
>>> +{
>>> +       return NULL;
>>> +}
>>> +
>>> +static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
>>> +{
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_probe(struct udevice *dev)
>>> +{
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_remove(struct udevice *dev)
>>> +{
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_claim_bus(struct udevice *dev)
>>> +{
>>> +
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_release_bus(struct udevice *dev)
>>> +{
>>> +
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_xfer(struct udevice *dev, unsigned int bitlen,
>>> +                           const void *dout, void *din, unsigned long flags)
>>> +{
>>> +
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_set_speed(struct udevice *dev, uint speed)
>>> +{
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static int exynos_spi_set_mode(struct udevice *dev, uint mode)
>>> +{
>>> +       return -ENODEV;
>>> +}
>>> +
>>> +static const struct dm_spi_ops exynos_spi_ops = {
>>> +       .claim_bus      = exynos_spi_claim_bus,
>>> +       .release_bus    = exynos_spi_release_bus,
>>> +       .xfer           = exynos_spi_xfer,
>>> +       .set_speed      = exynos_spi_set_speed,
>>> +       .set_mode       = exynos_spi_set_mode,
>>> +};
>>> +
>>> +static const struct udevice_id exynos_spi_ids[] = {
>>> +       { .compatible = "samsung,exynos-spi" },
>>> +       { }
>>> +};
>>> +
>>> +U_BOOT_DRIVER(exynos_spi) = {
>>> +       .name   = "exynos_spi",
>>> +       .id     = UCLASS_SPI,
>>> +       .of_match = exynos_spi_ids,
>>> +       .ops    = &exynos_spi_ops,
>>> +       .ofdata_to_platdata = exynos_spi_ofdata_to_platdata,
>>> +       .probe  = exynos_spi_probe,
>>> +       .remove = exynos_spi_remove
>>
>> comma missing - remove is necessary?
>>
>>> +};
>>> +
>>> +
>>> +3. Replace 'exynos' in the above code with your driver name
>>> +
>>> +
>>> +4. #ifdef out all of the code in your driver except for the above
>>> +
>>> +This will allow you to get it building, which means you can work
>>> +incrementally. Since all the methods return an error initially, there is
>>> +less chance that you will accidentally leave something in.
>>> +
>>> +Also, even though your conversion is basically a rewrite, it might help
>>> +reviewers if you leave functions in the same place in the file,
>>> +particularly for large drivers.
>>> +
>>> +
>>> +5. Add some includes
>>> +
>>> +Add these includes to your driver:
>>> +
>>> +#include <dm.h>
>>> +#include <errno.h>
>>> +
>>> +
>>> +6. Build
>>> +
>>> +At this point you should be able to build U-Boot for your board with the
>>> +empty SPI driver. You still have empty methods in yur driver, but we will
>>
>> typo your instead of yur
>>
>>> +write these one by one.
>>> +
>>> +If you have spi_init() functions or the like that are called from your
>>> +board then the build will fail. Remove these calls and make a note of the
>>> +init that needs to be done.
>>> +
>>> +
>>> +7. Set up your platform data structure
>>> +
>>> +This will hold the information your driver needs to operate, like its
>>> +hardware address or maximum frequency.
>>> +
>>> +You may already have a struct like this, or you may need to create one
>>> +from some of the #defines or global variables in the driver.
>>> +
>>> +Note that this information is not the run-time information. It should not
>>> +include state that changes. It should be fixed throughout the life of
>>> +U-Boot. Run-time information comes later.
>>> +
>>> +Here is what was in the exynos spi driver:
>>> +
>>> +struct spi_bus {
>>> +       enum periph_id periph_id;
>>> +       s32 frequency;          /* Default clock frequency, -1 for none */
>>> +       struct exynos_spi *regs;
>>> +       int inited;             /* 1 if this bus is ready for use */
>>> +       int node;
>>> +       uint deactivate_delay_us;       /* Delay to wait after deactivate */
>>> +};
>>> +
>>> +Of these, inited is handled by DM and node is the device tree node, which
>>> +DM tells you. The name is not quite right. So in this case we would use:
>>> +
>>> +struct exynos_spi_platdata {
>>> +       enum periph_id periph_id;
>>> +       s32 frequency;          /* Default clock frequency, -1 for none */
>>> +       struct exynos_spi *regs;
>>> +       uint deactivate_delay_us;       /* Delay to wait after deactivate */
>>> +};
>>> +
>>> +
>>> +8a. Write ofdata_to_platdata()   [for device tree only]
>>> +
>>> +This method will convert information in the device tree node into a C
>>> +structure in your driver (called platform data). If you are not using
>>> +device tree, go to 8b.
>>> +
>>> +DM will automatically allocate the struct for us when we are using device
>>> +tree, but we need to tell it the size:
>>> +
>>> +U_BOOT_DRIVER(spi_exynos) = {
>>> +...
>>> +       .platdata_auto_alloc_size = sizeof(struct exynos_spi_platdata),
>>> +
>>> +
>>> +Here is a sample function. It gets a pointer to the platform data and
>>> +fills in the fields from device tree.
>>> +
>>> +static int exynos_spi_ofdata_to_platdata(struct udevice *dev)
>>> +{
>>> +       struct exynos_spi_platdata *plat = dev->platdata;
>>> +       const void *blob = gd->fdt_blob;
>>> +       int node = dev->of_offset;
>>> +
>>> +       plat->regs = (struct exynos_spi *)fdtdec_get_addr(blob, node, "reg");
>>> +       plat->periph_id = pinmux_decode_periph_id(blob, node);
>>> +
>>> +       if (plat->periph_id == PERIPH_ID_NONE) {
>>> +               debug("%s: Invalid peripheral ID %d\n", __func__,
>>> +                       plat->periph_id);
>>> +               return -FDT_ERR_NOTFOUND;
>>> +       }
>>> +
>>> +       /* Use 500KHz as a suitable default */
>>> +       plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
>>> +                                       500000);
>>> +       plat->deactivate_delay_us = fdtdec_get_int(blob, node,
>>> +                                       "spi-deactivate-delay", 0);
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +
>>> +8b. Add the platform data  [non-device-tree only]
>>> +
>>> +Specify this data in a U_BOOT_DEVICE() declaration in your board file:
>>> +
>>> +struct exynos_spi_platdata platdata_spi0 = {
>>> +       .periph_id = ...
>>> +       .frequency = ...
>>> +       .regs = ...
>>> +       .deactivate_delay_us = ...
>>> +};
>>> +
>>> +U_BOOT_DEVICE(board_spi0) = {
>>> +       .name = "exynos_spi",
>>> +       .platdata = &platdata_spi0,
>>> +};
>>> +
>>> +You will unfortunately need to put the struct into a header file in this
>>> +case so that your board file can use it.
>>> +
>>> +
>>> +9. Add the device private data
>>> +
>>> +Most devices have some private data which they use to keep track of things
>>> +while active. This is the run-time information and needs to be stored in
>>> +a structure. There is probably a structure in the driver that includes a
>>> +'struct spi_slave', so you can use that.
>>> +
>>> +struct exynos_spi_slave {
>>> +       struct spi_slave slave;
>>> +       struct exynos_spi *regs;
>>> +       unsigned int freq;              /* Default frequency */
>>> +       unsigned int mode;
>>> +       enum periph_id periph_id;       /* Peripheral ID for this device */
>>> +       unsigned int fifo_size;
>>> +       int skip_preamble;
>>> +       struct spi_bus *bus;            /* Pointer to our SPI bus info */
>>> +       ulong last_transaction_us;      /* Time of last transaction end */
>>> +};
>>> +
>>> +
>>> +We should rename this to make its purpose more obvious, and get rid of
>>> +the slave structure, so we have:
>>> +
>>> +struct exynos_spi_priv {
>>> +       struct exynos_spi *regs;
>>> +       unsigned int freq;              /* Default frequency */
>>> +       unsigned int mode;
>>> +       enum periph_id periph_id;       /* Peripheral ID for this device */
>>> +       unsigned int fifo_size;
>>> +       int skip_preamble;
>>> +       ulong last_transaction_us;      /* Time of last transaction end */
>>> +};
>>> +
>>> +
>>> +DM can auto-allocate this also:
>>> +
>>> +U_BOOT_DRIVER(spi_exynos) = {
>>> +...
>>> +       .priv_auto_alloc_size = sizeof(struct exynos_spi_priv),
>>> +
>>> +
>>> +Note that this is created before the probe() method is called, and destroyed
>>> +after the remove() method is called. It will be zeroed when the probe()
>>> +method is called.
>>> +
>>> +
>>> +10. Add the probe() and remove() methods
>>> +
>>> +Note: It's a good idea to build repeatedly as you are working, to avoid a
>>> +huge amount of work getting things compiling at the end.
>>> +
>>> +The probe() method is supposed to set up the hardware. U-Boot used to use
>>> +spi_setup_slave() to do this. So take a look at this function and see
>>> +what you can copy out to set things up.
>>> +
>>> +
>>> +static int exynos_spi_probe(struct udevice *dev)
>>> +{
>>> +       struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
>>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>>> +
>>> +       if (pdata->periph_id == PERIPH_ID_SPI1 ||
>>> +           pdata->periph_id == PERIPH_ID_SPI2)
>>> +               priv->fifo_size = 64;
>>> +       else
>>> +               priv->fifo_size = 256;
>>> +
>>> +       priv->skip_preamble = 0;
>>> +       priv->last_transaction_us = timer_get_us();
>>> +       priv->freq = pdata->frequency;
>>> +       priv->periph_id = pdata->periph_id;
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +This implementation doesn't actually touch the hardware, which is somewhat
>>> +unusual for a driver. In this case we will do that when the device is
>>> +claimed by something that wants to use the SPI bus.
>>> +
>>> +For remove() we could shut down the clocks, but in this case there is
>>> +nothing to do. DM frees any memory that it allocated, so we can just
>>> +remove exynos_spi_remove() and its reference in U_BOOT_DRIVER.
>>> +
>>> +
>>> +11. Implement set_speed()
>>> +
>>> +This should set up clocks so that the SPI bus is running at the right
>>> +speed. With the old API spi_claim_bus() would normally do this and several
>>> +of the following functions, so let's look at that function:
>>> +
>>> +int spi_claim_bus(struct spi_slave *slave)
>>> +{
>>> +       struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
>>> +       struct exynos_spi *regs = spi_slave->regs;
>>> +       u32 reg = 0;
>>> +       int ret;
>>> +
>>> +       ret = set_spi_clk(spi_slave->periph_id,
>>> +                                       spi_slave->freq);
>>> +       if (ret < 0) {
>>> +               debug("%s: Failed to setup spi clock\n", __func__);
>>> +               return ret;
>>> +       }
>>> +
>>> +       exynos_pinmux_config(spi_slave->periph_id, PINMUX_FLAG_NONE);
>>> +
>>> +       spi_flush_fifo(slave);
>>> +
>>> +       reg = readl(&regs->ch_cfg);
>>> +       reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
>>> +
>>> +       if (spi_slave->mode & SPI_CPHA)
>>> +               reg |= SPI_CH_CPHA_B;
>>> +
>>> +       if (spi_slave->mode & SPI_CPOL)
>>> +               reg |= SPI_CH_CPOL_L;
>>> +
>>> +       writel(reg, &regs->ch_cfg);
>>> +       writel(SPI_FB_DELAY_180, &regs->fb_clk);
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +
>>> +It sets up the speed, mode, pinmux, feedback delay and clears the FIFOs.
>>> +With DM these will happen in separate methods.
>>> +
>>> +
>>> +Here is an example for the speed part:
>>> +
>>> +static int exynos_spi_set_speed(struct udevice *dev, uint speed)
>>> +{
>>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>>> +       int ret;
>>> +
>>> +       ret = set_spi_clk(priv->periph_id, speed);
>>> +       if (ret)
>>> +               return ret;
>>> +       priv->freq = speed;
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +
>>> +12. Implement set_mode()
>>> +
>>> +This should adjust the SPI mode (polarity, etc.). Again this code probably
>>> +comes from the old spi_claim_bus(). Here is an example:
>>> +
>>> +
>>> +static int exynos_spi_set_mode(struct udevice *dev, uint mode)
>>> +{
>>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>>> +       uint32_t reg;
>>> +
>>> +       reg = readl(&priv->regs->ch_cfg);
>>> +       reg &= ~(SPI_CH_CPHA_B | SPI_CH_CPOL_L);
>>> +
>>> +       if (mode & SPI_CPHA)
>>> +               reg |= SPI_CH_CPHA_B;
>>> +
>>> +       if (mode & SPI_CPOL)
>>> +               reg |= SPI_CH_CPOL_L;
>>> +
>>> +       writel(reg, &priv->regs->ch_cfg);
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +
>>> +13. Implement claim_bus()
>>> +
>>> +This is where a client wants to make use of the bus, so claims it first.
>>> +At this point we need to make sure every is set up ready for data transfer.
>>> +
>>> +Here again we look at the old claim function and see some code that is
>>> +needed. It is anything unrelated to speed and mode:
>>> +
>>> +static int exynos_spi_claim_bus(struct udevice *dev)
>>> +{
>>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>>> +
>>> +       exynos_pinmux_config(priv->periph_id, PINMUX_FLAG_NONE);
>>> +       spi_flush_fifo(priv->regs);
>>> +
>>> +       writel(SPI_FB_DELAY_180, &priv->regs->fb_clk);
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +The spi_flush_fifo() function is in the removed part of the code, so we
>>> +need to expose it again (perhaps with an #endif before it and '#if 0'
>>> +after it). It only needs accesss to priv->regs which is why we have
>>> +passed that in:
>>> +
>>> +/**
>>> + * Flush spi tx, rx fifos and reset the SPI controller
>>> + *
>>> + * @param regs Pointer to SPI registers
>>> + */
>>> +static void spi_flush_fifo(struct exynos_spi *regs)
>>> +{
>>> +       clrsetbits_le32(&regs->ch_cfg, SPI_CH_HS_EN, SPI_CH_RST);
>>> +       clrbits_le32(&regs->ch_cfg, SPI_CH_RST);
>>> +       setbits_le32(&regs->ch_cfg, SPI_TX_CH_ON | SPI_RX_CH_ON);
>>> +}
>>> +
>>> +
>>> +14. Implement release_bus()
>>> +
>>> +This releases the bus - in our example the old code in spi_release_bus()
>>> +is a call to spi_flush_fifo, so we add:
>>> +
>>> +static int exynos_spi_release_bus(struct udevice *dev)
>>> +{
>>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>>> +
>>> +       spi_flush_fifo(priv->regs);
>>> +
>>> +       return 0;
>>> +}
>>> +
>>> +
>>> +15. Implement xfer()
>>> +
>>> +This is the final method that we need to create, and it is where all the
>>> +work happens. The method parameters are the same as the old spi_xfer() with
>>> +the addition of a 'struct udevice' so conversion is pretty easy. Start
>>> +by copying the contents of spi_xfer() to your new xfer() mthod and proceed
>>
>> typo - method
>>
>>> +from there.
>>> +
>>> +If (flags & SPI_XFER_BEGIN) is non-zero then xfer() normally calls an
>>> +activate function, something like this:
>>> +
>>> +void spi_cs_activate(struct spi_slave *slave)
>>> +{
>>> +       struct exynos_spi_slave *spi_slave = to_exynos_spi(slave);
>>> +
>>> +       /* If it's too soon to do another transaction, wait */
>>> +       if (spi_slave->bus->deactivate_delay_us &&
>>> +           spi_slave->last_transaction_us) {
>>> +               ulong delay_us;         /* The delay completed so far */
>>> +               delay_us = timer_get_us() - spi_slave->last_transaction_us;
>>> +               if (delay_us < spi_slave->bus->deactivate_delay_us)
>>> +                       udelay(spi_slave->bus->deactivate_delay_us - delay_us);
>>> +       }
>>> +
>>> +       clrbits_le32(&spi_slave->regs->cs_reg, SPI_SLAVE_SIG_INACT);
>>> +       debug("Activate CS, bus %d\n", spi_slave->slave.bus);
>>> +       spi_slave->skip_preamble = spi_slave->mode & SPI_PREAMBLE;
>>> +}
>>> +
>>> +The new version looks like this:
>>> +
>>> +static void spi_cs_activate(struct udevice *dev)
>>> +{
>>> +       struct exynos_spi_platdata *pdata = dev_get_platdata(dev);
>>> +       struct exynos_spi_priv *priv = dev_get_priv(dev);
>>> +
>>> +       /* If it's too soon to do another transaction, wait */
>>> +       if (pdata->deactivate_delay_us &&
>>> +           priv->last_transaction_us) {
>>> +               ulong delay_us;         /* The delay completed so far */
>>> +               delay_us = timer_get_us() - priv->last_transaction_us;
>>> +               if (delay_us < pdata->deactivate_delay_us)
>>> +                       udelay(pdata->deactivate_delay_us - delay_us);
>>> +       }
>>> +
>>> +       clrbits_le32(&priv->regs->cs_reg, SPI_SLAVE_SIG_INACT);
>>> +       debug("Activate CS, bus '%s'\n", dev->name);
>>> +       priv->skip_preamble = priv->mode & SPI_PREAMBLE;
>>> +}
>>> +
>>> +All we have really done here is change the pointers and print the device name
>>> +instead of the bus number. Other local static functions can be treated in
>>> +the same way.
>>> +
>>> +
>>> +16. Set up the per-child data and child_pre_probe() method
>>> +
>>> +To minimise the pain and complexity of the SPI subsystem while the driver
>>
>> typo - minimize
>
> English spelling (this is U-Boot).
>
>>
>>> +model change-over is in place, struct spi_slave is used to reference a
>>> +SPI bus slave, even though that slave is actually a struct udevice. In fact
>>> +struct spi_slave is the device's child data. We need to make sure this is
>>> +set up. It is possible to allocate more space that struct spi_slave needs,
>>> +but this is the minimum.
>>> +
>>> +U_BOOT_DRIVER(exynos_spi) = {
>>> +...
>>> +       .per_child_auto_alloc_size      = sizeof(struct spi_slave),
>>> +       .child_pre_probe        = exynos_spi_child_pre_probe,
>>> +}
>>> +
>>> +int exynos_spi_child_pre_probe(struct udevice *dev)
>>> +{
>>> +       struct spi_slave *slave = dev_get_parentdata(dev);
>>> +
>>> +       return spi_ofdata_to_platdata(gd->fdt_blob, dev->of_offset, slave);
>>> +}
>>
>> So this routine will fill spi_slave members - is it?
>> Why cs and bus are not part of DM?
>
> See above for my comments, but let me know if unsure.
>
>>
>>> +
>>> +
>>> +Here our child_pre_probe() method merely calls a standard method to convert
>>> +device tree data to that used in the slave.
>>> +
>>> +
>>> +17. Test it
>>> +
>>> +Now that you have the code written and it compiles, try testing it using
>>> +the 'sf test' command. You may need to enable CONFIG_CMD_SF_TEST for your
>>> +board.
>>> +
>>> +
>>> +18. Prepare patches and send them to the mailing lists
>>> +
>>> +You can use 'tools/patman/patman' to prepare, check and send patches for
>>> +your work. See the README for details.
>>> --
>>> 2.0.0.526.g5318336

thanks!
-- 
Jagan.

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers
  2014-09-01  6:45       ` Jagan Teki
@ 2014-09-02  0:24         ` Simon Glass
  0 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-09-02  0:24 UTC (permalink / raw)
  To: u-boot

Hi Jagan,


On 31 August 2014 23:45, Jagan Teki <jagannadh.teki@gmail.com> wrote:
> Hi Simon,
>
> On 1 September 2014 10:36, Simon Glass <sjg@chromium.org> wrote:
>> Hi Jagan,
>>
>> On 28 August 2014 04:32, Jagan Teki <jagannadh.teki@gmail.com> wrote:
>>> On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
>>>> This README is intended to help maintainers move their SPI drivers over to
>>>> driver model. It works through the required steps with an example.
>>>>
>>>> Signed-off-by: Simon Glass <sjg@chromium.org>
>>>> ---
>>>>
>>>>  doc/driver-model/spi-howto.txt | 570 +++++++++++++++++++++++++++++++++++++++++
>>>>  1 file changed, 570 insertions(+)
>>>>  create mode 100644 doc/driver-model/spi-howto.txt
>>>>
>>>> diff --git a/doc/driver-model/spi-howto.txt b/doc/driver-model/spi-howto.txt
>>>> new file mode 100644
>>>> index 0000000..bb64735
>>>> --- /dev/null
>>>> +++ b/doc/driver-model/spi-howto.txt
>>>> @@ -0,0 +1,570 @@
>>>> +How to port a SPI driver to driver model
>>>> +========================================
>>>> +
>>>> +Here is a rough step-by-step guide. It is based around converting the
>>>> +exynos SPI driver to driver model (DM) and the example code is based
>>>> +around U-Boot v2014.04 (commit dda0dbf).
>>>> +
>>>> +It is quite long since it includes actual code examples.
>>>> +
>>>> +Before driver model, SPI drivers have their own private structure which
>>>> +contains 'struct spi_slave'. With driver model, 'struct spi_slave' still
>>>> +exists, but now it is 'per-child data' for the SPI bus. Each child of the
>>>> +SPI bus is a SPI slave. The information that was stored in the
>>>> +driver-specific slave structure can now be put in private data for the
>>>> +SPI bus.
>>>
>>> Do you think spi_slave still require, I guess It's needed as some slave members
>>> to cs, bus are used to control the driver.
>>
>> The CS and bus are purely command-line conveniences with DM. When it
>> gets down to the driver the bus is determined by the SPI peripheral it
>> is connected to, and the CS is the control that it adjusts to enable
>> the chip select. The numbering of buses and chip selects is not
>> relevant down at the driver level anymore.
>
> Yes - I understand but let me explain my comments.
>
> u-boot> sf probe bus:cs speed mode
>
> Unlike other buses/controllers on u-boot we're dynamically probing the
> slave on bus
> using bus and cs for example, we have a zynq_spi handling spi0, spi1
> (bus's) and each
> one has 3 chip-selects. So
>
> u-boot> sf probe 1:1
> for probing bus1 (spi1) and cs 1
>
> u-boot> sf probe 0:1
> for probing bus0 (spi0) and cs 1
>
> The respective reg base (based on bus) and register chip-select (based
> on cs) zynq_spi.c
> will handle these in driver it self.
>
> speed and mode settings, I'm fine with dm, where it will configure using
>
> int spi_get_bus_and_cs(int busnum, int cs, int speed, int mode,
>                        const char *drv_name, const char *dev_name,
>                        struct udevice **devp, struct spi_slave **slavep)
> {
> .....
> ret = spi_set_speed_mode(bus, speed, mode);
> ......
> }
>
> But how can spi-uclass.c will identifies how many bus's along with cs lines does
> specified controller driver support.

This is done with platform data or device tree. In other words we
explicitly list a driver for each chip select.

However I did anticipate problems with this approach, particularly
while so few drivers are converted. Also we need to support things
like the 'sspi' command and 'sf probe'. So I added spi_bind_device()
which is automatically called if no SPI device is found for a bus/chip
select.

>
> This what usually we're doing in driver as
> int spi_cs_is_valid(unsigned int bus, unsigned int cs)
> {
>         /* 2 bus with 3 chipselect */
>         return bus < 2 && cs < 3;
> }
>
> Please let me know if you have any questions, this is what usually
> doing most of drivers.

Yes, I don't really like that - it is a fine way of doing things
before driver model, but now it is not that useful. We are trying to
bind drivers to chip selects, not just figure out which ones are
valid.

One thing I have not figured out yet (mostly since I have no driver
that needs it) is how to activate multiple SPI chip selects using
GPIOs. It shouldn't be too hard, just need a board that supports it.

[snip]

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function
  2014-08-25 18:31   ` Jagan Teki
@ 2014-09-02 19:22     ` Simon Glass
  0 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-09-02 19:22 UTC (permalink / raw)
  To: u-boot

Hi Jagan,

On 25 August 2014 12:31, Jagan Teki <jagannadh.teki@gmail.com> wrote:
> On 15 July 2014 06:26, Simon Glass <sjg@chromium.org> wrote:
>> In preparation for changing the error handling in this code for driver
>> model, move it into its own function.
>>
>> Signed-off-by: Simon Glass <sjg@chromium.org>
>> ---
>>
>>  common/cmd_spi.c | 53 ++++++++++++++++++++++++++++++++---------------------
>>  1 file changed, 32 insertions(+), 21 deletions(-)
>>
>
> Reviewed-by: Jagannadha Sutradharudu Teki <jaganna@xilinx.com>

Thanks. I'd quite like to pull these reviewed patches through the -dm
tree as it is easier for me to keep things consistent. Is that OK with
you?

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

* [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec
  2014-08-11 19:55     ` Simon Glass
@ 2014-09-15  1:03       ` Simon Glass
  0 siblings, 0 replies; 51+ messages in thread
From: Simon Glass @ 2014-09-15  1:03 UTC (permalink / raw)
  To: u-boot

Hi Jagan,

On 11 August 2014 13:55, Simon Glass <sjg@chromium.org> wrote:
> Hi Jagan,
>
> On 10 August 2014 03:16, Jagan Teki <jagannadh.teki@gmail.com> wrote:
>> On Sun, Aug 10, 2014 at 2:59 AM, Simon Glass <sjg@chromium.org> wrote:
>>> Hi,
>>>
>>> On 14 July 2014 18:56, Simon Glass <sjg@chromium.org> wrote:
>>>> Up until now driver model has not been used for any type of bus. Buses
>>>> have some unique properties and needs, so we cannot claim that driver
>>>> model can cover all the common cases unless we have converted a bus over
>>>> to driver model.
>>>>
>>>> SPI is a reasonable choice for this next step. It has a fairly simple
>>>> API and not too many dependencies. The main one is SPI flash so we may
>>>> as well convert that also. Since the boards I test with have cros_ec I
>>>> have also included that, for SPI only.
>>>>
>>>> The technique used is make use of driver model's supported data structures
>>>> to hold information currently kept by each subsystem in a private data
>>>> structure. Since 'struct spi_slave' relates to the slave device on the bus
>>>> it is stored in the 'parent' data with each child device of the bus.
>>>> Since 'struct spi_flash' is a standard interface used for each SPI flash
>>>> driver, it is stored in the SPI FLash uclass's private data for each
>>>> device.
>>>>
>>>> New defines are created to enable driver model for each subsystem. These
>>>> are:
>>>>
>>>>    CONFIG_DM_SPI
>>>>    CONFIG_DM_SPI_FLASH
>>>>    CONFIG_DM_CROS_EC
>>>>
>>>> This allows us to move some boards and drivers to driver model, while
>>>> leaving others behind. A 'big bang' conversion of everything to driver
>>>> model, event at a subsystem level, is never going to work.
>>>>
>>>> On the other hand, we change the driver at the same time as the CONFIG
>>>> option is enabled. Keeping both version of the driver around involves a
>>>> flock of #ifdefs, the benefit of which is not apparent to me, since the
>>>> old code is removed anyway.
>>>>
>>>> There is some cost in changing the uclass interface after it is created,
>>>> so if you have limited time, please spend it reviewing the uclass
>>>> interfaces in spi.h and spi_flash.h. These need to be supported by each
>>>> driver, so changing them later may involve changing multiple drivers.
>>>>
>>>> To help with conversion of SPI drivers to driver model, documentation is
>>>> provided which takes the happy camper through the process with an example.
>>>>
>>>> As always, driver model patches are available at u-boot-dm.git branch
>>>> 'working'.
>>>>
>>>> Note: This series is not fully ready - e.g. some header files are missing
>>>> comments. But I wanted to get it out for review early since some SPI work
>>>> is ongoing which might depend on it.
>>>
>>> Are there any comments on this series please? I'd like to apply these
>>> to dm/master early next week, excluding the exynos ones.
>>
>> I will be busy in next full week, give me 2 more weeks.
>> Probably I will give my comments on next to next weekend.
>
> I'll see what I can do. It has been out for review for 4 weeks :-)

I'll send an updated series with your comments address. I'm planning
to apply the non-exynos parts of SPI this week. Please let me know if
you have any objections. I'd like to allow sufficient test time before
the release.

Regards,
Simon

^ permalink raw reply	[flat|nested] 51+ messages in thread

end of thread, other threads:[~2014-09-15  1:03 UTC | newest]

Thread overview: 51+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-07-15  0:56 [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 01/25] sandbox: Convert SPI flash emulation to use sf_params Simon Glass
2014-08-25  9:24   ` Jagan Teki
2014-07-15  0:56 ` [U-Boot] [PATCH 02/25] sandbox: config: Enable all SPI flash chips Simon Glass
2014-08-25  9:24   ` Jagan Teki
2014-07-15  0:56 ` [U-Boot] [PATCH 03/25] sandbox: dts: Add a SPI device and cros_ec device Simon Glass
2014-08-25  9:32   ` Jagan Teki
2014-07-15  0:56 ` [U-Boot] [PATCH 04/25] dm: spi: Move cmd device code into its own function Simon Glass
2014-08-25 18:31   ` Jagan Teki
2014-09-02 19:22     ` Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 05/25] spi: Add brackets and tidy defines in spi.h Simon Glass
2014-08-25  9:34   ` Jagan Teki
2014-07-15  0:56 ` [U-Boot] [PATCH 06/25] dm: spi: Add a uclass for SPI Simon Glass
2014-07-15  8:26   ` Pavel Herrmann
2014-07-17  5:39     ` Simon Glass
2014-07-17  7:57       ` Pavel Herrmann
2014-07-17 15:26         ` Simon Glass
2014-07-17 18:01           ` Pavel Herrmann
2014-07-17 18:29             ` Pavel Herrmann
2014-07-21  2:17               ` Simon Glass
2014-07-21  2:15             ` Simon Glass
2014-08-11 21:46   ` Daniel Schwierzeck
2014-08-28  8:58   ` Jagan Teki
2014-08-29 23:38     ` Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 07/25] dm: sandbox: Add a SPI emulation uclass Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 08/25] dm: Remove spi_init() from board_r.c when using driver model Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 09/25] dm: Add spi.h header to a few files Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 10/25] dm: spi: Adjust cmd_spi to work with driver model Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 11/25] dm: sandbox: spi: Move to " Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 12/25] dm: spi: Add documentation on how to convert over SPI drivers Simon Glass
2014-08-28 11:32   ` Jagan Teki
2014-09-01  5:06     ` Simon Glass
2014-09-01  6:45       ` Jagan Teki
2014-09-02  0:24         ` Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 13/25] dm: exynos: Convert SPI to driver model Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 14/25] sf: Add an empty entry to the parameter list Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 15/25] sf: Tidy up public and private header files Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 16/25] spi: Use error return value in sf_ops Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 17/25] dm: sf: Add a uclass for SPI flash Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 18/25] dm: Convert spi_flash_probe() and 'sf probe' to use driver model Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 19/25] dm: sf: sandbox: Convert SPI flash driver to " Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 20/25] dm: exynos: config: Use driver model for SPI flash Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 21/25] dm: spi: Add tests Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 22/25] dm: sf: Add tests for SPI flash Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 23/25] dm: cros_ec: Add support for driver model Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 24/25] dm: sandbox: cros_ec: Move sandbox cros_ec to driver module Simon Glass
2014-07-15  0:56 ` [U-Boot] [PATCH 25/25] dm: exynos: cros_ec: Move cros_ec_spi to driver model Simon Glass
2014-08-09 21:29 ` [U-Boot] [PATCH 0/25] Introduce driver model support for SPI, SPI flash, cros_ec Simon Glass
2014-08-10  9:16   ` Jagan Teki
2014-08-11 19:55     ` Simon Glass
2014-09-15  1:03       ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.