1 /******************************************************************************
2 *                                                                             *
3 * License Agreement                                                           *
4 *                                                                             *
5 * Copyright (c) 2015 Altera Corporation, San Jose, California, USA.           *
6 * All rights reserved.                                                        *
7 *                                                                             *
8 * Permission is hereby granted, free of charge, to any person obtaining a     *
9 * copy of this software and associated documentation files (the "Software"),  *
10 * to deal in the Software without restriction, including without limitation   *
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,    *
12 * and/or sell copies of the Software, and to permit persons to whom the       *
13 * Software is furnished to do so, subject to the following conditions:        *
14 *                                                                             *
15 * The above copyright notice and this permission notice shall be included in  *
16 * all copies or substantial portions of the Software.                         *
17 *                                                                             *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  *
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,    *
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      *
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING     *
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER         *
24 * DEALINGS IN THE SOFTWARE.                                                   *
25 *                                                                             *
26 *                                                                             *
27 ******************************************************************************/
28 
29 #ifndef __ALT_EPCQ_CONTROLLER_H__
30 #define __ALT_EPCQ_CONTROLLER_H__
31 
32 #include "alt_types.h"
33 #include "sys/alt_flash_dev.h"
34 #include "sys/alt_llist.h"
35 
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #endif /* __cplusplus */
40 
41 /**
42  *  Description of the EPCQ controller
43  */
44 typedef struct alt_epcq_controller_dev
45 {
46     alt_flash_dev dev;
47 
48     alt_u32 data_base; /** base address of data slave */
49     alt_u32 data_end; /** end address of data slave (not inclusive) */
50     alt_u32 csr_base; /** base address of CSR slave */
51     alt_u32 size_in_bytes; /** size of memory in bytes */
52     alt_u32 is_epcs; /** 1 if device is an EPCS device */
53     alt_u32 number_of_sectors; /** number of flash sectors */
54     alt_u32 sector_size; /** size of each flash sector */
55     alt_u32 page_size; /** page size */
56     alt_u32 silicon_id; /** ID of silicon used with EPCQ IP */
57 } alt_epcq_controller_dev;
58 
59 /**
60 *   Macros used by alt_sys_init.c to create data storage for driver instance
61 */
62 #define ALTERA_EPCQ_CONTROLLER_AVL_MEM_AVL_CSR_INSTANCE(epcq_name, avl_mem, avl_csr, epcq_dev) \
63 static alt_epcq_controller_dev epcq_dev =                                                       \
64 {                                                                                               \
65   .dev = {                                                                                      \
66             .llist = ALT_LLIST_ENTRY,                                                           \
67             .name = avl_mem##_NAME,                                                             \
68             .write = alt_epcq_controller_write,                                                 \
69             .read = alt_epcq_controller_read,                                                   \
70             .get_info = alt_epcq_controller_get_info,                                           \
71             .erase_block = alt_epcq_controller_erase_block,                                     \
72             .write_block = alt_epcq_controller_write_block,                                     \
73             .base_addr = ((void*)(avl_mem##_BASE)),                                             \
74             .length = ((int)(avl_mem##_SPAN)),                                                  \
75             .lock = alt_epcq_controller_lock ,                                                  \
76          },                                                                                     \
77   .data_base = ((alt_u32)(avl_mem##_BASE)),   							\
78   .data_end = ((alt_u32)(avl_mem##_BASE) + (alt_u32)(avl_mem##_SPAN)),	                        \
79   .csr_base = ((alt_u32)(avl_csr##_BASE)),   							\
80   .size_in_bytes = ((alt_u32)(avl_mem##_SPAN)),         				        \
81   .is_epcs = ((alt_u32)(avl_mem##_IS_EPCS)),							\
82   .number_of_sectors = ((alt_u32)(avl_mem##_NUMBER_OF_SECTORS)),			        \
83   .sector_size = ((alt_u32)(avl_mem##_SECTOR_SIZE)),						\
84   .page_size = ((alt_u32)(avl_mem##_PAGE_SIZE))	,						\
85 }
86 
87 /*
88     Public API
89 
90     Refer to Using Flash Devices in the
91     Developing Programs Using the Hardware Abstraction Layer chapter
92     of the Nios II Software Developer's Handbook.
93 
94 */
95 int alt_epcq_controller_read(alt_flash_dev *flash_info, int offset, void *dest_addr, int length);
96 
97 int alt_epcq_controller_get_info(alt_flash_fd *fd, flash_region **info, int *number_of_regions);
98 
99 int alt_epcq_controller_erase_block(alt_flash_dev *flash_info, int block_offset);
100 
101 int alt_epcq_controller_write_block(alt_flash_dev *flash_info, int block_offset, int data_offset, const void *data, int length);
102 
103 int alt_epcq_controller_write(alt_flash_dev *flash_info, int offset, const void *src_addr, int length);
104 
105 int alt_epcq_controller_lock(alt_flash_dev *flash_info, alt_u32 sectors_to_lock);
106 
107 
108 /*
109  * Initialization function
110  */
111 extern alt_32 altera_epcq_controller_init(alt_epcq_controller_dev *dev);
112 
113 /*
114  * alt_sys_init.c will call this macro automatically initialize the driver instance
115  */
116 #define ALTERA_EPCQ_CONTROLLER_INIT(name, dev) 	\
117 		altera_epcq_controller_init(&dev);
118 
119 
120 #ifdef __cplusplus
121 }
122 #endif /* __cplusplus */
123 
124 #endif /* __ALT_EPCQ_CONTROLLER_H__ */
125