1 /*---------------------------------------------------------------------------*
2   Project:  TwlSDK - NWM - libraries
3   File:     nwm_init.c
4 
5   Copyright 2007-2008 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law. They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Date:: 2008-09-25#$
14   $Rev: 8631 $
15   $Author: sato_masaki $
16  *---------------------------------------------------------------------------*/
17 
18 #include <twl.h>
19 #include "nwm_common_private.h"
20 #ifdef SDK_TWL
21 #include "wm_common.h"
22 #else
23 #include "nwm_arm9_private.h"
24 #include <nitro/wm.h>
25 #include <nitro/os/ARM9/protectionRegion.h>
26 #endif
27 
28 u8 nwmInitialized = 0;
29 
30 /*---------------------------------------------------------------------------*
31     Internal Function Definitions
32  *---------------------------------------------------------------------------*/
33 NWMRetCode NWMi_InitCore(void* sysBuf, u32 bufSize, u8 dmaNo);
34 static BOOL NwmCheckEnableFlag(void);
35 
36 /*---------------------------------------------------------------------------*
37   Name:         NWM_Init
38 
39   Description:  Initializes the NWM library.
40                 Synchronous function that only initializes ARM9.
41 
42   Arguments:    sysBuf: Pointer to the buffer allocated by the caller.
43 
44                 bufSize: Size of the buffer allocated by the caller.
45 
46                 dmaNo: DMA number used by the NWM library
47 
48   Returns:      NWMRetCode: Returns the processing result.
49  *---------------------------------------------------------------------------*/
NWM_Init(void * sysBuf,u32 bufSize,u8 dmaNo)50 NWMRetCode NWM_Init(void* sysBuf, u32 bufSize, u8 dmaNo)
51 {
52     if( NWMi_CheckEnableFlag() == FALSE ) // Return an error if the TWL System Settings are configured not to use wireless
53     {
54         return NWM_RETCODE_NWM_DISABLE;
55     }
56 
57     return NWMi_InitCore(sysBuf, bufSize, dmaNo);
58 }
59 
NWMi_InitCore(void * sysBuf,u32 bufSize,u8 dmaNo)60 NWMRetCode NWMi_InitCore(void* sysBuf, u32 bufSize, u8 dmaNo)
61 {
62     OSIntrMode e;
63     extern NWMArm9Buf *nwm9buf;
64 
65     SDK_COMPILER_ASSERT(sizeof(NWMArm9Buf) <= NWM_ARM9NWM_BUF_SIZE);
66     SDK_COMPILER_ASSERT(sizeof(NWMArm7Buf) <= NWM_ARM7NWM_BUF_SIZE);
67     SDK_COMPILER_ASSERT(sizeof(NWMStatus) <= NWM_STATUS_BUF_SIZE);
68     SDK_ASSERT(bufSize >= NWM_SYSTEM_BUF_SIZE);
69 
70     e = OS_DisableInterrupts();
71 
72 #ifdef SDK_TWL
73     /* Confirms that the WM library initialization is complete */
74     if (WM_CheckInitialized() == WM_ERRCODE_SUCCESS)
75     {
76         NWM_WARNING("WM has already been initialized.\n");
77         (void)OS_RestoreInterrupts(e);
78         return NWM_RETCODE_ILLEGAL_STATE;
79     }
80 #endif
81 
82     /* Confirms that the NWM library has been initialized */
83     if (nwmInitialized)
84     {
85         NWM_WARNING("NWM  has already been initialized.\n");
86         (void)OS_RestoreInterrupts(e);
87         return NWM_RETCODE_ILLEGAL_STATE;        // Initialization complete
88     }
89 
90     if (sysBuf == NULL)
91     {
92         NWM_WARNING("Parameter \"sysBuf\" must not be NULL.\n");
93         (void)OS_RestoreInterrupts(e);
94         return NWM_RETCODE_INVALID_PARAM;
95     }
96 
97     if (dmaNo > MI_DMA_MAX_NUM)
98     {
99         NWM_WARNING("Parameter \"dmaNo\" is over %d.\n", MI_DMA_MAX_NUM);
100         (void)OS_RestoreInterrupts(e);
101         return NWM_RETCODE_INVALID_PARAM;
102     }
103 
104     if ((u32)sysBuf & 0x01f)
105     {
106         NWM_WARNING("Parameter \"sysBuf\" must be 32-byte aligned.\n");
107         (void)OS_RestoreInterrupts(e);
108         return NWM_RETCODE_INVALID_PARAM;
109     }
110 
111     // Confirms whether the WM library has started on the ARM7
112     PXI_Init();
113     if (!PXI_IsCallbackReady(PXI_FIFO_TAG_WMW, PXI_PROC_ARM7))
114     {
115         NWM_WARNING("NWM library on ARM7 side is not ready yet.\n");
116         (void)OS_RestoreInterrupts(e);
117         return NWM_RETCODE_NWM_DISABLE;
118     }
119 
120     /* Invalidate all buffer cache */
121     DC_InvalidateRange(sysBuf, bufSize);
122     MI_DmaClear32(dmaNo, sysBuf, bufSize);
123     nwm9buf = (NWMArm9Buf *)sysBuf;
124     DC_StoreRange(&nwm9buf, sizeof(nwm9buf));
125     nwm9buf->NWM7 = (NWMArm7Buf *)((u32)nwm9buf + NWM_ARM9NWM_BUF_SIZE);
126     nwm9buf->status = (NWMStatus *)((u32)(nwm9buf->NWM7) + NWM_ARM7NWM_BUF_SIZE);
127     nwm9buf->fifo7to9 = (u32 *)((u32)(nwm9buf->status) + NWM_STATUS_BUF_SIZE);
128 
129     // Clears the FIFO buffer writable flag
130     NWMi_ClearFifoRecvFlag();
131 
132     nwm9buf->dmaNo = dmaNo;
133 
134     // Initializes the queue for entry registration
135     {
136         s32     i;
137 
138         OS_InitMessageQueue(&nwm9buf->apibufQ.q, nwm9buf->apibufQ.fifo, NWM_APIBUF_NUM);
139         for (i = 0; i < NWM_APIBUF_NUM; i++)
140         {
141             // Clears the ring buffer to the usable state
142             *((u16 *)(nwm9buf->apibuf[i])) = NWM_API_REQUEST_ACCEPTED;
143             DC_StoreRange(nwm9buf->apibuf[i], sizeof(u16));
144             (void)OS_SendMessage(&nwm9buf->apibufQ.q, &nwm9buf->apibuf[i][0], OS_MESSAGE_BLOCK);
145         }
146     }
147 
148     // Sets FIFO receive function
149     PXI_SetFifoRecvCallback(PXI_FIFO_TAG_WMW, NWMi_ReceiveFifo9);
150 
151     // Clear firmware binary information
152     MI_CpuClear8(&nwm9buf->binData, sizeof(NWMBinaryData));
153 
154     nwmInitialized = 1;
155     nwm9buf->status->state = NWM_STATE_INITIALIZED;  // As an exception, set the initial state here alone (this was originally done by the ARM7)
156     // Store cache to ARM9 using part of main memory
157     DC_StoreRange(nwm9buf, sizeof(NWMArm9Buf));
158     DC_StoreRange(&nwmInitialized, sizeof(u8));
159     (void)OS_RestoreInterrupts(e);
160 
161     return NWM_RETCODE_SUCCESS;
162 }
163 
164 /*---------------------------------------------------------------------------*
165   Name:         NWMi_CheckInitialized
166 
167   Description:  Confirms that the NWM library has been initialized.
168 
169   Arguments:    None.
170 
171   Returns:      int: Returns the processing result as an NWM_ERRCODE_* value.
172  *---------------------------------------------------------------------------*/
NWMi_CheckInitialized(void)173 NWMRetCode NWMi_CheckInitialized(void)
174 {
175     // Check if initialized
176     if (!nwmInitialized)
177     {
178 //        NWM_WARNING("NWM library is not initialized yet.\n");
179         return NWM_RETCODE_ILLEGAL_STATE;
180     }
181     return NWM_RETCODE_SUCCESS;
182 }
183 
184 /*---------------------------------------------------------------------------*
185   Name:         NWMi_CheckEnableFlag
186 
187   Description:  Checks the flag that allows wireless use, configurable from the TWL System Settings.
188                 This forcibly returns TRUE because the flag cannot currently be checked.
189 
190   Arguments:    None.
191 
192   Returns:      TRUE if wireless features can be used and FALSE otherwise
193  *---------------------------------------------------------------------------*/
NWMi_CheckEnableFlag(void)194 BOOL NWMi_CheckEnableFlag(void)
195 {
196     if( OS_IsAvailableWireless() == TRUE && OS_IsForceDisableWireless() == FALSE )
197     {
198         return TRUE;
199     }
200     return FALSE;
201 }
202 
203 /*---------------------------------------------------------------------------*
204     End of file
205  *---------------------------------------------------------------------------*/
206