This is the mail archive of the ecos-patches@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

V2 flash - clean up device driver interface


This patch is an attempt to clean up the interface between the generic
flash code and the device drivers. Patches to the device drivers will
follow shortly. Things still work on my hardware, but I cannot easily
test all the device drivers. The clean-ups are:

1) use const correctly in the various driver functions. This is
   similar to my earlier changes to the main API

2) if none of the drivers support locking, don't bother to include the
   lock and unlock functions in the function table. The
   CYG_FLASH_FUNS() macro will do the right thing.

3) I have added a flags field to the cyg_flash_dev structure. This is
   not used at present, but will be needed for some future
   enhancements. It affects the CYG_FLASH_DRIVER() macro and I only
   want to change that one once.

4) I have removed the config field from the cyg_flash_dev structure.
   There seemed to be no good reason for having both a config field
   and a priv field, so where appropriate I have merged the two.

5) the cyg_flash_dev funs, block_info and priv fields have been made
   const pointers. If these structures can be completely initialized
   statically then they can now be declared const without generating
   compiler warnings. For a ROM startup the compiler should now place
   them in flash rather than RAM, saving some memory. If the
   structures have to be filled in at run-time by the init() function
   then obviously they should not be declared const, and the init()
   function will have to use a cast to remove the const-ness.
   Effectively I am eliminating three casts in the preferred scenario,
   at the cost of one cast in the other scenario.

6) the CYG_FLASH_DRIVER() macro no longer allocates space for the priv
   structure. That made little sense to me, with some drivers you
   ended up allocating zero-byte arrays.

7) the CYG_FLASH_DRIVER() macro now takes 8 arguments, one for each of
   the fields that should be managed by the device driver. This makes
   it possible to use the macro in cases where all the fields can be
   filled in statically. Previously this could only be achieved by
   bypassing the macro and defining the cyg_flash_dev structure
   explicitly, which is more error-prone.

Bart

Index: ChangeLog
===================================================================
RCS file: /cvs/ecos/ecos/packages/io/flash/current/ChangeLog,v
retrieving revision 1.38.2.19
diff -u -r1.38.2.19 ChangeLog
--- ChangeLog	22 Nov 2004 13:25:24 -0000	1.38.2.19
+++ ChangeLog	22 Nov 2004 20:03:25 -0000
@@ -1,5 +1,7 @@
 2004-11-22  Bart Veer  <bartv@ecoscentric.com>
 
+	* include/flash_priv.h, src/legacy_dev.c: clean up the interface
+	between the generic flash code and the device drivers
 	* include/flash_priv, src/flash_legacy.h, src/flash.c,
 	src/legacy_dev.c: move cache stuff andother legacy macros
 	to an internal header.
Index: src/legacy_dev.c
===================================================================
RCS file: /cvs/ecos/ecos/packages/io/flash/current/src/Attic/legacy_dev.c,v
retrieving revision 1.1.2.7
diff -u -r1.1.2.7 legacy_dev.c
--- src/legacy_dev.c	22 Nov 2004 13:25:23 -0000	1.1.2.7
+++ src/legacy_dev.c	22 Nov 2004 20:03:27 -0000
@@ -133,12 +133,12 @@
 
 static int 
 legacy_flash_erase_block (struct cyg_flash_dev *dev, 
-                          const cyg_flashaddr_t block_base)
+                          cyg_flashaddr_t block_base)
      __attribute__ ((section (".2ram.flash_program_buf")));
 
 static int 
 legacy_flash_erase_block (struct cyg_flash_dev *dev, 
-                          const cyg_flashaddr_t block_base)
+                          cyg_flashaddr_t block_base)
 {
   typedef int code_fun(cyg_flashaddr_t, unsigned int);
   code_fun *_flash_erase_block;
@@ -158,7 +158,7 @@
 static int
 legacy_flash_program(struct cyg_flash_dev *dev, 
                      cyg_flashaddr_t base, 
-                     const void* data, const size_t len)
+                     const void* data, size_t len)
 {
   typedef int code_fun(cyg_flashaddr_t, const void *, int, unsigned long, int);
   code_fun *_flash_program_buf;
@@ -174,13 +174,13 @@
 static int 
 legacy_flash_read (struct cyg_flash_dev *dev, 
                    const cyg_flashaddr_t base, 
-                   void* data, const size_t len)
+                   void* data, size_t len)
      __attribute__ ((section (".2ram.flash_program_buf")));
      
 static int 
 legacy_flash_read (struct cyg_flash_dev *dev, 
                    const cyg_flashaddr_t base, 
-                   void* data, const size_t len)
+                   void* data, size_t len)
 {
   typedef int code_fun(const cyg_flashaddr_t, void *, int, unsigned long, int);
   code_fun *_flash_read_buf;
@@ -198,6 +198,7 @@
 #endif
 
 
+#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
 static int 
 legacy_flash_block_lock (struct cyg_flash_dev *dev, 
                          const cyg_flashaddr_t block_base)
@@ -207,16 +208,12 @@
 legacy_flash_block_lock (struct cyg_flash_dev *dev, 
                          const cyg_flashaddr_t block_base)
 {
-#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
   typedef int code_fun(cyg_flashaddr_t);
   code_fun *_flash_lock_block;
   
   _flash_lock_block = (code_fun*) __anonymizer(&flash_lock_block);
 
   return (*_flash_lock_block)(block_base);
-#else
-  return CYG_FLASH_ERR_INVALID;
-#endif
 }
 
 static int 
@@ -228,7 +225,6 @@
 legacy_flash_block_unlock (struct cyg_flash_dev *dev, 
                            const cyg_flashaddr_t block_base)
 {
-#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
   typedef int code_fun(cyg_flashaddr_t, int, int);
   code_fun *_flash_unlock_block;
   size_t block_size = dev->block_info[0].block_size;
@@ -237,10 +233,8 @@
   _flash_unlock_block = (code_fun*) __anonymizer(&flash_unlock_block);
   
   return (*_flash_unlock_block)(block_base, block_size, blocks);
-#else
-  return CYG_FLASH_ERR_INVALID;
-#endif
 }
+#endif
 
 // Map a hardware status to a package error
 static int 
@@ -267,20 +261,23 @@
     HAL_FLASH_CACHES_ON(d_cache, i_cache);
 }
 
-CYG_FLASH_FUNS(cyg_legacy_funs, 
-               legacy_flash_init,
-               legacy_flash_query,
-               legacy_flash_erase_block,
-               legacy_flash_program,
-               LEGACY_FLASH_READ,
-               legacy_flash_hwr_map_error,
-               legacy_flash_block_lock,
-               legacy_flash_block_unlock
-);
+static const CYG_FLASH_FUNS(cyg_legacy_funs, 
+                            legacy_flash_init,
+                            legacy_flash_query,
+                            legacy_flash_erase_block,
+                            legacy_flash_program,
+                            LEGACY_FLASH_READ,
+                            legacy_flash_hwr_map_error,
+                            legacy_flash_block_lock,
+                            legacy_flash_block_unlock
+    );
 
-CYG_FLASH_DRIVER(cyg_zzlegacy_flashdev, // zz so that it probably comes last.
+CYG_FLASH_DRIVER(cyg_zzlegacy_flashdev,
                  &cyg_legacy_funs,
-                 NULL,  // Pointer to config structure
-                 0,     // Start address of flash
-                 0);    // Size of private structure
-
+                 0,     // Flags
+                 0,     // Start address, filled in by init
+                 0,     // End address, filled in by init
+                 0,     // Number of block infos, filled in by init
+                 NULL,  // Block infos, again filled in by init
+                 NULL   // Driver private data, none needed
+    );
Index: include/flash_priv.h
===================================================================
RCS file: /cvs/ecos/ecos/packages/io/flash/current/include/Attic/flash_priv.h,v
retrieving revision 1.1.2.5
diff -u -r1.1.2.5 flash_priv.h
--- include/flash_priv.h	22 Nov 2004 13:25:23 -0000	1.1.2.5
+++ include/flash_priv.h	22 Nov 2004 20:03:26 -0000
@@ -64,83 +64,101 @@
 
 // Structure of pointers to functions in the device driver
 struct cyg_flash_dev_funs {
-  int (*flash_init) (struct cyg_flash_dev *dev);
-  size_t (*flash_query) (struct cyg_flash_dev *dev, void * data, 
-                         const size_t len);
-  int (*flash_erase_block) (struct cyg_flash_dev *dev, 
-                            const cyg_flashaddr_t block_base);
-  int (*flash_program) (struct cyg_flash_dev *dev, 
-                        cyg_flashaddr_t base, 
-                        const void* data, const size_t len);
-  int (*flash_read) (struct cyg_flash_dev *dev, 
-                     const cyg_flashaddr_t base, 
-                     void* data, const size_t len);
-  int (*flash_hwr_map_error) (struct cyg_flash_dev *dev, int err);
-  int (*flash_block_lock) (struct cyg_flash_dev *dev, 
-                           const cyg_flashaddr_t block_base);
-  int (*flash_block_unlock) (struct cyg_flash_dev *dev, 
-                             const cyg_flashaddr_t block_base);
+  int     (*flash_init) (struct cyg_flash_dev *dev);
+  size_t  (*flash_query) (struct cyg_flash_dev *dev,
+                          void * data, size_t len);
+  int     (*flash_erase_block) (struct cyg_flash_dev *dev, 
+                                cyg_flashaddr_t block_base);
+  int     (*flash_program) (struct cyg_flash_dev *dev, 
+                            cyg_flashaddr_t base, 
+                            const void* data, size_t len);
+  int     (*flash_read) (struct cyg_flash_dev *dev, 
+                         const cyg_flashaddr_t base, 
+                         void* data, size_t len);
+  int     (*flash_hwr_map_error) (struct cyg_flash_dev *dev, int err);
+#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING    
+  int     (*flash_block_lock) (struct cyg_flash_dev *dev, 
+                               const cyg_flashaddr_t block_base);
+  int     (*flash_block_unlock) (struct cyg_flash_dev *dev, 
+                                 const cyg_flashaddr_t block_base);
+#endif    
 };
 
 // Structure each device places in the HAL table
 struct cyg_flash_dev {
-  struct cyg_flash_dev_funs   *funs;           // Function pointers
-  cyg_flashaddr_t             start;           // First address
-  cyg_flashaddr_t             end;             // Last address
-  cyg_uint32                  num_block_infos; // Number of entries
-  cyg_flash_block_info_t      *block_info;     // Info about one block size
-
-  void                        *priv;           // Devices private data
-  void                        *config;         // Configuration info
-
-// The following are only written to by the FLASH IO layer.
-  cyg_flash_printf            *pf;             // Pointer to diagnostic printf
-  bool                        init;            // Device has been initialised
+  const struct cyg_flash_dev_funs *funs;            // Function pointers
+  cyg_uint32                      flags;            // Device characteristics
+  cyg_flashaddr_t                 start;            // First address
+  cyg_flashaddr_t                 end;              // Last address
+  cyg_uint32                      num_block_infos;  // Number of entries
+  const cyg_flash_block_info_t    *block_info;      // Info about one block size
+
+  const void                      *priv;            // Devices private data
+
+  // The following are only written to by the FLASH IO layer.
+  cyg_flash_printf                *pf;              // Pointer to diagnostic printf
+  bool                            init;             // Device has been initialised
 #ifdef CYGPKG_KERNEL
-  cyg_mutex_t                 mutex;           // Mutex for thread safeness
+  cyg_mutex_t                     mutex;            // Mutex for thread safeness
 #endif
 #if (CYGHWR_IO_FLASH_DEVICE > 1)    
-  struct cyg_flash_dev        *next;           // Pointer to next device
+  struct cyg_flash_dev            *next;            // Pointer to next device
 #endif    
 } CYG_HAL_TABLE_TYPE;
 
-#define CYG_FLASH_FUNS(funs,init,query,erase,prog,read,map,lock,unlock)\
-struct cyg_flash_dev_funs funs = \
-  {                                     \
-    init,                               \
-    query,                              \
-    erase,                              \
-    prog,                               \
-    read,                               \
-    map,                                \
-    lock,                               \
-    unlock                              \
-  };
+#ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
+# define CYG_FLASH_FUNS(_funs_, _init_, _query_ , _erase_, _prog_ , _read_, _map_, _lock_, _unlock_) \
+struct cyg_flash_dev_funs _funs_ =      \
+{										\
+	.flash_init             = _init_,   \
+	.flash_query            = _query_,  \
+	.flash_erase_block      = _erase_,  \
+	.flash_program          = _prog_,   \
+	.flash_read             = _read_,   \
+	.flash_hwr_map_error    = _map_,    \
+	.flash_block_lock       = _lock_,   \
+	.flash_block_unlock     = _unlock_  \
+}
+#else
+# define CYG_FLASH_FUNS(_funs_, _init_, _query_ , _erase_, _prog_ , _read_, _map_, _lock_, _unlock_) \
+struct cyg_flash_dev_funs _funs_ =      \
+{										\
+	.flash_init             = _init_,   \
+	.flash_query            = _query_,  \
+	.flash_erase_block      = _erase_,  \
+	.flash_program          = _prog_,   \
+	.flash_read             = _read_,   \
+	.flash_hwr_map_error    = _map_     \
+}
+#endif
 
 // We assume HAL tables are placed into RAM.
-#define CYG_FLASH_DRIVER(name, _funs, _config, _start, _priv_size)    \
-static char priv_ ## name [_priv_size];                               \
-struct cyg_flash_dev name CYG_HAL_TABLE_ENTRY(cyg_flashdev) = {       \
-   .funs = _funs,                                                     \
-   .config = _config,                                                 \
-   .priv = priv_ ## name,                                             \
-   .start = _start,                                                   \
-};
+#define CYG_FLASH_DRIVER(_name_, _funs_, _flags_, _start_, _end_, _num_block_infos_, _block_info_, _priv_)  \
+struct cyg_flash_dev _name_ CYG_HAL_TABLE_ENTRY(cyg_flashdev) = \
+{                                                               \
+    .funs               = _funs_,                               \
+    .flags              = _flags_,                              \
+    .start              = _start_,                              \
+    .end                = _end_,                                \
+    .num_block_infos    = _num_block_infos_,                    \
+    .block_info         = _block_info_,                         \
+    .priv               = _priv_                                \
+}
 
 #ifdef CYGHWR_IO_FLASH_DEVICE_LEGACY
 struct flash_info {
-  int   block_size;   // Assuming fixed size "blocks"
-  int   blocks;       // Number of blocks
-  int   buffer_size;  // Size of write buffer (only defined for some devices)
+  int	block_size;	  // Assuming fixed size "blocks"
+  int	blocks;		  // Number of blocks
+  int	buffer_size;  // Size of write buffer (only defined for some devices)
   unsigned long block_mask;
   void *start, *end;  // Address range
-  int   init;
+  int	init;
   cyg_flash_printf *pf;
 };
 
 externC struct flash_info flash_info;
-externC int  flash_hwr_init(void);
-externC int  flash_hwr_map_error(int err);
+externC int	 flash_hwr_init(void);
+externC int	 flash_hwr_map_error(int err);
 externC void flash_dev_query(void *data);
 #endif // CYGHWR_IO_FLASH_DEVICE_LEGACY
 




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]