1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <DA1469xAB.h>
21 #include <stdbool.h>
22 #include <stdlib.h>
23 
24 #define QSPIC_TYPE  QSPIC_Type *
25 #define QSPIC_ID  	((QSPIC_TYPE)QSPIC_BASE)
26 #define QSPIC2_ID 	((QSPIC_TYPE)QSPIC2_BASE)
27 
28 typedef enum qspi_bus_mode {
29     QSPI_BUS_MODE_SINGLE = 0x1,
30     QSPI_BUS_MODE_DUAL = 0x2,
31     QSPI_BUS_MODE_QUAD = 0x4
32 } qspi_bus_mode_t;
33 
34 typedef __PACKED_STRUCT qspi_memory_id {
35     uint8_t id;
36     uint8_t type;
37     uint8_t density;
38 } qspi_memory_id_t;
39 
40 /**
41  * QSPICx enable and set read pipe delay.
42  *
43  * Calling this routine will enable the read pipe clock delay which introduces a delay
44  * in the sampling clock. The delay is relative to the falling edge of the clock signal.
45  *
46  * @param qspic_id   ID to designate the QSPIC for which the delay will be applied to.
47  *                   Valid values are QSPIC_ID and QSPIC2_ID.
48  * @param delay      The read pipe delay (should not exceed 7).
49  *
50  * @note As per the datasheet, when VDD is 0.9V the pipe delay should be assigned to 2 whereas
51  *       when VDD is 1.2V the pipe delay should be updated to 7.
52  *
53  */
54 void
55 da1469x_qspi_set_read_pipe_delay(QSPIC_TYPE qspi_id, uint8_t delay);
56 
57 /**
58  * QSPIC2 enable and set Tcem seting.
59  *
60  * Calling this routine will enable the Tcem value which defines the max. time the #CS
61  * line can stay activated in a burst transaction. This max. value is required by a
62  * RAM memory device as its auto-refresh mechanism, if supported, usually takes place when
63  * the device is inactive.
64  *
65  * @param sys_clock_freq   Curreny system clock frequency.
66  * @param tcem_us          Tcem value in microsends.
67  *
68  * @note This property is valid for QSPIC2_ID and when the latter is configured to drive
69  *       PSRAM memory devices only.
70  *
71  * @note This property has effect when the controller operates in auto mode.
72  *
73  */
74 void
75 da1469x_qspi_set_tcem(uint32_t sys_clock_freq, uint32_t tcem_us);
76 
77 /**
78  * QSPICx set various #CS timing parameters
79  *
80  * Calling this routine will configure various timings related to #CS activation.
81  *
82  * @param qspic_id        ID to designate the QSPIC for which the timing settings will be applied to.
83  *                        Valid values are QSPIC_ID and QSPIC2_ID.
84  * @param sys_clock_freq  Current system clock frequency.
85  * @param read_delay_ns   Min. time, in nanoseconds, the #CS line should remain inactive between
86  *                        the transmission of different instructions e.g. write followed by a read
87  *                        memory access.
88  * @param erase_delay_ns  Min. time, in nanoseconds, the #CS line should remain inactive after the
89  *                        execution of a write enable, erase, erase resume or erase suspend instruction.
90  *
91  * @note This property has effect when the controller operates in auto mode.
92  *
93  */
94 void
95 da1469x_qspi_set_cs_delay(QSPIC_TYPE qspi_id, uint32_t sys_clock_freq,
96                                     uint32_t read_delay_ns, uint32_t erase_delay_ns);
97 
98 /**
99  * QSPICx set bus mode
100  *
101  * Calling this routine will configure the target bus mode used for write/read operations.
102  *
103  * @param qspic_id        ID to designate the QSPIC for which the bus mode will be applied to.
104  *                        Valid values are QSPIC_ID and QSPIC2_ID.
105  * @param mode            The selected bus mode
106  *
107  * @warning This API should be called onlu when the controller is in manual mode. Otherwise, an
108  *          assertion will be thrown.
109  *
110  * @note This API should be called prior to swithcing to the auto mode and when the quad bus mode
111  *       is selected. Otherwise, data will not be written/read correctly.
112  */
113 void
114 da1469x_qspi_set_bus_mode(QSPIC_TYPE qspi_id, qspi_bus_mode_t mode);
115 
116 /**
117  * Reset memory device based on the JEDEC protocol.
118  *
119  * Calling this routine will initiate a reset sequence using JEDEC op codes.
120  *
121  * @param qspic_id        ID to designate the QSPIC for which the memory device is connected to.
122  *                        Valid values are QSPIC_ID and QSPIC2_ID.
123  *
124  * @warning This API should be called onlu when the controller is in manual mode. Otherwise, an
125  *          assertion will be thrown.
126  *
127  * @note Typically, after reset, a time interval should be elapsed before the memory can accept
128  *       further commands. Programmer is responsible to add the necessary delay based on
129  *       manufacturer's datasheet.
130  *
131  */
132 void
133 da1469x_qspi_memory_jedec_reset(QSPIC_TYPE qspi_id);
134 
135 /**
136  * Read memory device ID based on the JEDEC protocol
137  *
138  * Calling this routine will initiate a device read ID sequence using JEDEC op codes.
139  * The programmer should then compare the returned ID sub-fields with the ones provided
140  * by manufacturer's datasheet.
141  *
142  * @param qspic_id        ID to designate the QSPIC for which the memory device is connected to.
143  *                        Valid values are QSPIC_ID and QSPIC2_ID.
144  * @param id              Pointer to a structure where the ID value should be stored to
145  *
146  * @warning This API should be called only when the controller is in manual mode. Otherwise, an
147  *          assertion will be thrown.
148  *
149  */
150 void
151 da1469x_qspi_memory_jedec_read_id(QSPIC_TYPE qspi_id, qspi_memory_id_t *id);
152 
153 /**
154  * Enter/exit memory device QPI mode
155  *
156  * Calling this routine will initiate the process to enter/exit the QPI mode, if supported by
157  * the underlying memory device. Bus mode will be selected autimatically based on \p enter.
158  *
159  * @param qspic_id        ID to designate the QSPIC for which the memory device is connected to.
160  *                        Valid values are QSPIC_ID and QSPIC2_ID.
161  * @param enter           True if the device should enter the QPI mode, false if the device should
162  *                        exit the QPI mode.
163  * @param cmd             Op code to be sent for the device to enter/exit the QPI mode.
164  *
165  * @warning This API should be called only when the controller is in manual mode. Otherwise, an
166  *          assertion will be thrown.
167  *
168  */
169 void
170 da1469x_qspi_enter_exit_qpi_mode(QSPIC_TYPE qspi_id, bool enter, uint8_t cmd);
171