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_QSPI_CONTROLLER2_H__
30 #define __ALT_QSPI_CONTROLLER2_H__
31 
32 #include "alt_types.h"
33 #include "sys/alt_flash_dev.h"
34 #include "sys/alt_llist.h"
35 
36 
37 #ifdef __cplusplus
38 extern "C"
39 {
40 
41 #endif /* __cplusplus */
42 
43 
44 
45 /**
46  *  Description of the QSPI controller
47  */
48 typedef struct alt_qspi_controller2_dev
49 {
50     alt_flash_dev dev;
51 
52 
53     alt_u32 data_base; /** base address of data slave */
54     alt_u32 data_end; /** end address of data slave (not inclusive) */
55     alt_u32 csr_base; /** base address of CSR slave */
56     alt_u32 size_in_bytes; /** size of memory in bytes */
57     alt_u32 is_epcs; /** 1 if device is an EPCS device */
58     alt_u32 number_of_sectors; /** number of flash sectors */
59     alt_u32 sector_size; /** size of each flash sector */
60     alt_u32 page_size; /** page size */
61     alt_u32 silicon_id; /** ID of silicon used with EPCQ/QSPI IP */
62 } alt_qspi_controller2_dev;
63 
64 
65 /**
66 *   Macros used by alt_sys_init.c to create data storage for driver instance
67 */
68 
69 #define ALTERA_GENERIC_QUAD_SPI_CONTROLLER2_AVL_MEM_AVL_CSR_INSTANCE(qspi_name, avl_mem, avl_csr, qspi_dev) \
70 static alt_qspi_controller2_dev qspi_dev =                                                       \
71 {                                                                                               \
72   .dev = {                                                                                      \
73             .llist = ALT_LLIST_ENTRY,                                                           \
74             .name = avl_mem##_NAME,                                                             \
75             .write = alt_qspi_controller2_write,                                                 \
76             .read = alt_qspi_controller2_read,                                                   \
77             .get_info = alt_qspi_controller2_get_info,                                           \
78             .erase_block = alt_qspi_controller2_erase_block,                                     \
79             .write_block = alt_qspi_controller2_write_block,                                     \
80             .base_addr = ((void*)(avl_mem##_BASE)),                                             \
81             .length = ((int)(avl_mem##_SPAN)),                                                  \
82             .lock = alt_qspi_controller2_lock ,                                                  \
83          },                                                                                     \
84   .data_base = ((alt_u32)(avl_mem##_BASE)),   							\
85   .data_end = ((alt_u32)(avl_mem##_BASE) + (alt_u32)(avl_mem##_SPAN)),	                        \
86   .csr_base = ((alt_u32)(avl_csr##_BASE)),   							\
87   .size_in_bytes = ((alt_u32)(avl_mem##_SPAN)),         				        \
88   .is_epcs = ((alt_u32)(avl_mem##_IS_EPCS)),							\
89   .number_of_sectors = ((alt_u32)(avl_mem##_NUMBER_OF_SECTORS)),			        \
90   .sector_size = ((alt_u32)(avl_mem##_SECTOR_SIZE)),						\
91   .page_size = ((alt_u32)(avl_mem##_PAGE_SIZE))	,						\
92 }
93 
94 /*
95     Public API
96     Refer to Using Flash Devices in the
97     Developing Programs Using the Hardware Abstraction Layer chapter
98     of the Nios II Software Developer's Handbook.
99 */
100 
101 int alt_qspi_controller2_read(alt_flash_dev *flash_info, int offset, void *dest_addr, int length);
102 
103 int alt_qspi_controller2_get_info(alt_flash_fd *fd, flash_region **info, int *number_of_regions);
104 
105 int alt_qspi_controller2_erase_block(alt_flash_dev *flash_info, int block_offset);
106 
107 int alt_qspi_controller2_write_block(alt_flash_dev *flash_info, int block_offset, int data_offset, const void *data, int length);
108 
109 int alt_qspi_controller2_write(alt_flash_dev *flash_info, int offset, const void *src_addr, int length);
110 
111 int alt_qspi_controller2_lock(alt_flash_dev *flash_info, alt_u32 sectors_to_lock);
112 
113 
114 /*
115  * Initialization function
116  */
117 extern alt_32 altera_qspi_controller2_init(alt_qspi_controller2_dev *dev);
118 
119 /*
120  * alt_sys_init.c will call this macro automatically initialize the driver instance
121  */
122 
123 #define ALTERA_GENERIC_QUAD_SPI_CONTROLLER2_INIT(name, dev) 	\
124 		altera_qspi_controller2_init(&dev);
125 
126 #ifdef __cplusplus
127 }
128 #endif /* __cplusplus */
129 #endif /* __ALT_QSPI_CONTROLLER2_H__ */
130