summaryrefslogtreecommitdiff
path: root/cpu/arm920t/wmt/alloct.c
blob: da75657fdf8032b49ad91294809e5641553a8592 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*++ 
Copyright (c) 2010 WonderMedia Technologies, Inc.

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software 
Foundation, either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details. You
should have received a copy of the GNU General Public License along with this
program. If not, see http://www.gnu.org/licenses/>.

WonderMedia Technologies, Inc.
10F, 529, Chung-Cheng Road, Hsin-Tien, Taipei 231, R.O.C.
--*/


#if !defined(__MEM_H__)
#include "mem.h"
#endif
#if !defined(__CARD_H__)
#include "card.h"
#endif
#if !defined(__ALLOCT_H__)
#include "alloct.h"
#endif
#include "macif.h"
#if !defined(__DBG_H__)
#include "dbg.h"
#endif

/*---------------------  Static Definitions -------------------------*/

/*---------------------  Static Classes  ----------------------------*/

/*---------------------  Static Variables  --------------------------*/

/*---------------------  Static Functions  --------------------------*/

/*---------------------  Export Variables  --------------------------*/

/*---------------------  Export Functions  --------------------------*/

BOOL
ALCbAllocateRdrMemory(
    PSAdapterInfo pAdapter
    )
{
	int     jj;

	/* Allocate the Receive Descriptor ring */
	/* (Physically contiguous and non cached) */
	pAdapter->amRxDescRing.dwRawSize = sizeof(SRxDesc) * pAdapter->cbRD + pAdapter->dwCacheLineSize;
	pAdapter->amRxDescRing.dwSize = sizeof(SRxDesc) * pAdapter->cbRD;
	/* allocate raw RD : add cacheline */
	MEMvAllocateShared(&pAdapter->amRxDescRing);
	MacDump(MACDBG_INFO, ("[ALCbAllocateRdrMemory] - pAdapter->amRxDescRing.dwRawVAddr : %p, size : %d\n",
		(PVOID)pAdapter->amRxDescRing.dwRawVAddr, pAdapter->amRxDescRing.dwRawSize));

	if (pAdapter->amRxDescRing.dwRawVAddr == 0)
		return FALSE;

	/* descriptor MUST aligned by 4 dword */
	MEMvAlign(&pAdapter->amRxDescRing, (UINT)pAdapter->dwCacheLineSize);

	for (jj = 0; jj < pAdapter->cbRD; jj++) {
		pAdapter->aamRxDescBuf[jj].dwRawSize = g_sOptions.uiBuffsize + pAdapter->dwCacheLineSize;
		pAdapter->aamRxDescBuf[jj].dwSize = g_sOptions.uiBuffsize;
		/* allocate Rx Buffer raw:add cacheline */
		MEMvAllocateShared(&pAdapter->aamRxDescBuf[jj]);
		if (pAdapter->aamRxDescBuf[jj].dwRawVAddr == 0) {
			printf("allocate RX Buffer failed\n ");
			return FALSE;
		}
		MEMvAlign(&pAdapter->aamRxDescBuf[jj], (UINT)pAdapter->dwCacheLineSize);
	}

	return TRUE;
}


BOOL
ALCbAllocateTdrMemory(
    PSAdapterInfo pAdapter
    )
{
	/* Allocate the transmit descriptor ring */
	/* (Physically contiguous and non cached) */
	pAdapter->amTxDescRing.dwRawSize = sizeof(STxDesc) * pAdapter->cbTD + pAdapter->dwCacheLineSize;
	pAdapter->amTxDescRing.dwSize = sizeof(STxDesc) * pAdapter->cbTD;
	MEMvAllocateShared(&pAdapter->amTxDescRing);
	MacDump(MACDBG_INFO, ("[ALCbAllocateTdrMemory] - pAdapter->amTxDescRing.dwRawVAddr : %p, size : %d\n",
		(PVOID)pAdapter->amTxDescRing.dwRawVAddr, pAdapter->amTxDescRing.dwRawSize));

	if (pAdapter->amTxDescRing.dwRawVAddr == 0)
		return FALSE;

	/* Descriptor MUST aligned by 4 dword */
	MEMvAlign(&pAdapter->amTxDescRing, (UINT)pAdapter->dwCacheLineSize);

	if (!ALCbAllocateTdrBuffer(pAdapter, pAdapter->aamTxDescBuf, pAdapter->cbTD)) {
		printf("allocte TX buffer failed\n");
		return FALSE;
	}
	return TRUE;
}

BOOL
ALCbAllocateTdrBuffer(
    PSAdapterInfo pAdapter,
    SAllocMap asMaps[],
    UINT    cbTD
    )
{
	int     ii;


	for (ii = 0; ii < cbTD; ii++) {
		asMaps[ii].dwRawSize = g_sOptions.uiBuffsize + pAdapter->dwCacheLineSize;
		asMaps[ii].dwSize = g_sOptions.uiBuffsize;

		MEMvAllocateShared(&asMaps[ii]);

		if (asMaps[ii].dwRawVAddr == 0)
			return FALSE;

		/* If allocated Tx buffers are double-word alignment, make it byte-alignment */
		if (asMaps[ii].dwRawVAddr % sizeof(DWORD) == 0) {
			asMaps[ii].dwVAddr = asMaps[ii].dwRawVAddr + (ii % 4);
			asMaps[ii].dwPAddr = LODWORD(asMaps[ii].qwRawPAddr) + (ii % 4);
		} else {
			asMaps[ii].dwVAddr = asMaps[ii].dwRawVAddr;
			asMaps[ii].dwPAddr = LODWORD(asMaps[ii].qwRawPAddr);
		}
	}

	return TRUE;
}

VOID
ALCvChainRdrMemory(
    PSAdapterInfo pAdapter
    )
{
	int     ii;
	PSRxDesc    pRD;

	/* Initialize the receive descriptor poniter array and */
	/* each individual receive descriptor */
	pRD = (PSRxDesc)pAdapter->amRxDescRing.dwVAddr;

	for (ii = 0; ii < pAdapter->cbRD; ii++) {
		pAdapter->apRD[ii] = pRD;

		/* Init descriptor value */
		pRD->m_rd0RD0.f15FrameLen = 0;
		pRD->m_rd0RD0.f1Owner = B_OWNED_BY_CHIP;
		pRD->m_rd1RD1.f15RxBufLen = CB_MAX_BUF_SIZE;

		/* Set default RD buffer size to 512 bytes if 3065 (for multi-Rx descs per packet) */
		pRD->m_rd1RD1.f15RxBufLen = CB_BUF_SIZE_65;

		pRD->m_dwRxBufferAddr = pAdapter->aamRxDescBuf[ii].dwPAddr;
		if (ii == (pAdapter->cbRD - 1))
			pRD->m_dwRxNextDescAddr = pAdapter->amRxDescRing.dwPAddr;
		else
			pRD->m_dwRxNextDescAddr = pAdapter->amRxDescRing.dwPAddr + sizeof(SRxDesc) * (ii + 1);

		pRD = (PSRxDesc)(pAdapter->amRxDescRing.dwVAddr + sizeof(SRxDesc) * (ii + 1));
	}
}

VOID
ALCvChainTdrMemory(
    PSAdapterInfo pAdapter
    )
{
	int     ii;
	PSTxDesc    pTD;

	/* Initialize the tx desc ring */
	/* Initialize the Transmit Descriptor pointer array */
	pTD = (PSTxDesc)pAdapter->amTxDescRing.dwVAddr;

	for (ii = 0; ii < pAdapter->cbTD; ii++) {
		pAdapter->apTD[ii] = pTD;
		/* Init descriptor value */
		pTD->m_td0TD0.f1Owner = B_OWNED_BY_HOST;
		/* modified by kevin:length =0 */
		pTD->m_td1TD1.f15TxBufLen = 0;
		pTD->m_td1TD1.f1Chain = 1;
		pTD->m_td1TD1.byTCR = TCR_IC | TCR_EDP | TCR_STP;

		pTD->m_dwTxBufferAddr = pAdapter->aamTxDescBuf[ii].dwPAddr;
		if (ii == (pAdapter->cbTD - 1))
			pTD->m_dwTxNextDescAddr = pAdapter->amTxDescRing.dwPAddr;
		else
			pTD->m_dwTxNextDescAddr = pAdapter->amTxDescRing.dwPAddr + sizeof(STxDesc) * (ii + 1);

		pTD = (PSTxDesc)(pAdapter->amTxDescRing.dwVAddr + sizeof(STxDesc) * (ii + 1));
	}

	/* Init adapter's sturcture also, init value = MAX_PACKET_LEN */
	pAdapter->uTxPktSize = MAX_PACKET_LEN;

}

VOID
ALCvFreeRdrMemory(
    PSAdapterInfo pAdapter
    )
{
	int     ii;

	/* Free the receive descriptor ring. */
	if (pAdapter->amRxDescRing.dwRawVAddr != 0)
		pAdapter->amRxDescRing.dwRawVAddr = 0;

	/* Free the Receive buffers */
	for (ii = 0; ii < pAdapter->cbRD; ii++) {
		if (pAdapter->aamRxDescBuf[ii].dwRawVAddr != 0)
			pAdapter->aamRxDescBuf[ii].dwRawVAddr = 0;
	}
}

VOID
ALCvFreeTdrMemory(
    PSAdapterInfo pAdapter
    )
{
	int     ii;

	/* Free the transmit descriptor ring */
	if (pAdapter->amTxDescRing.dwRawVAddr != 0)
		pAdapter->amTxDescRing.dwRawVAddr = 0;

	/* Free the Transmit Buffers */
	for (ii = 0; ii < pAdapter->cbTD; ii++) {
		if (pAdapter->aamTxDescBuf[ii].dwRawVAddr != 0)
			pAdapter->aamTxDescBuf[ii].dwRawVAddr = 0;
	}
}

VOID
ALCvSetDefaultRD(
    PSAdapterInfo pAdapter
    )
{
	/* set init RD value */
	ALCvChainRdrMemory(pAdapter);
}

VOID
ALCvSetDefaultTD(
    PSAdapterInfo pAdapter
    )
{
	int     ii;
	int     jj;
	PBYTE pbyBuf;

	DWORD adwPattern[4] = {
		0xFFFFFFFFL, 0x00000000L,
		0xAAAAAAAAL, 0x55555555L
	};

	/* Set init TD value */
	ALCvChainTdrMemory(pAdapter);

	/* Set tx descriptors owner to host(0) */
	/* Set IC = 1, EDP = 1, STP = 1 */
	for (ii = pAdapter->cbTD - 1; ii >= 0; ii--) {
		pbyBuf = (PBYTE)pAdapter->aamTxDescBuf[ii].dwVAddr;

		/* Packet length maybe multiples of 16 */
		jj = 0;
		while (jj < pAdapter->apTD[ii]->m_td1TD1.f15TxBufLen) {
			if (jj < pAdapter->apTD[ii]->m_td1TD1.f15TxBufLen - 8) {
				*(PDWORD)(pbyBuf + jj) = adwPattern[ii % 4];
				*(PDWORD)(pbyBuf + jj + 4) = adwPattern[ii % 4];
				jj += 8;
			} else {
				*(PBYTE)(pbyBuf + jj)  = (BYTE)adwPattern[ii % 4];
				jj++;
			}
		}
	}
}