Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*-
 * Copyright 2003-2011 Netlogic Microsystems (Netlogic). All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY Netlogic Microsystems ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NETLOGIC OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 *
 * NETLOGIC_BSD
 * $FreeBSD$
 */

#ifndef __NLM_HAL_MMIO_H__
#define	__NLM_HAL_MMIO_H__

/*
 * This file contains platform specific memory mapped IO implementation
 * and will provide a way to read 32/64 bit memory mapped registers in
 * all ABIs
 */

/*
 * For o32 compilation, we have to disable interrupts and enable KX bit to
 * access 64 bit addresses or data.
 *
 * We need to disable interrupts because we save just the lower 32 bits of
 * registers in  interrupt handling. So if we get hit by an interrupt while
 * using the upper 32 bits of a register, we lose.
 */
static inline uint32_t nlm_save_flags_kx(void)
{
	uint32_t sr = mips_rd_status();

	mips_wr_status((sr & ~MIPS_SR_INT_IE) | MIPS_SR_KX);
	return (sr);
}

static inline uint32_t nlm_save_flags_cop2(void)
{
	uint32_t sr = mips_rd_status();

	mips_wr_status((sr & ~MIPS_SR_INT_IE) | MIPS_SR_COP_2_BIT);
	return (sr);
}

static inline void nlm_restore_flags(uint32_t sr)
{
	mips_wr_status(sr);
}

static inline uint32_t
nlm_load_word(uint64_t addr)
{
	volatile uint32_t *p = (volatile uint32_t *)(long)addr;

	return *p;
}

static inline void
nlm_store_word(uint64_t addr, uint32_t val)
{
	volatile uint32_t *p = (volatile uint32_t *)(long)addr;

	*p = val;
}

#if defined(__mips_n64) || defined(__mips_n32)
static inline uint64_t
nlm_load_dword(volatile uint64_t addr)
{
	volatile uint64_t *p = (volatile uint64_t *)(long)addr;

	return *p;
}

static inline void
nlm_store_dword(volatile uint64_t addr, uint64_t val)
{
	volatile uint64_t *p = (volatile uint64_t *)(long)addr;

	*p = val;
}

#else /* o32 */
static inline uint64_t
nlm_load_dword(uint64_t addr)
{
	volatile uint64_t *p = (volatile uint64_t *)(long)addr;
	uint32_t valhi, vallo, sr;

	sr = nlm_save_flags_kx();
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"ld	$8, 0(%2)\n\t"
		"dsra32	%0, $8, 0\n\t"
		"sll	%1, $8, 0\n\t"
		".set	pop\n"
		: "=r"(valhi), "=r"(vallo)
		: "r"(p)
		: "$8");
	nlm_restore_flags(sr);

	return ((uint64_t)valhi << 32) | vallo;
}

static inline void
nlm_store_dword(uint64_t addr, uint64_t val)
{
	volatile uint64_t *p = (volatile uint64_t *)(long)addr;
	uint32_t valhi, vallo, sr;

	valhi = val >> 32;
	vallo = val & 0xffffffff;

	sr = nlm_save_flags_kx();
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"dsll32	$8, %1, 0\n\t"
		"dsll32	$9, %2, 0\n\t"  /* get rid of the */
		"dsrl32	$9, $9, 0\n\t"  /* sign extend */
		"or	$9, $9, $8\n\t"
		"sd	$9, 0(%0)\n\t"
		".set	pop\n"
		: : "r"(p), "r"(valhi), "r"(vallo)
		: "$8", "$9", "memory");
	nlm_restore_flags(sr);
}
#endif

#if defined(__mips_n64)
static inline uint64_t
nlm_load_word_daddr(uint64_t addr)
{
	volatile uint32_t *p = (volatile uint32_t *)(long)addr;

	return *p;
}

static inline void
nlm_store_word_daddr(uint64_t addr, uint32_t val)
{
	volatile uint32_t *p = (volatile uint32_t *)(long)addr;

	*p = val;
}

static inline uint64_t
nlm_load_dword_daddr(uint64_t addr)
{
	volatile uint64_t *p = (volatile uint64_t *)(long)addr;

	return *p;
}

static inline void
nlm_store_dword_daddr(uint64_t addr, uint64_t val)
{
	volatile uint64_t *p = (volatile uint64_t *)(long)addr;

	*p = val;
}

#elif defined(__mips_n32)

static inline uint64_t
nlm_load_word_daddr(uint64_t addr)
{
	uint32_t val;

	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"lw		%0, 0(%1)\n\t"
		".set	pop\n"
		: "=r"(val)
		: "r"(addr));

	return val;
}

static inline void
nlm_store_word_daddr(uint64_t addr, uint32_t val)
{
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"sw		%0, 0(%1)\n\t"
		".set	pop\n"
		: : "r"(val), "r"(addr)
		: "memory");
}

static inline uint64_t
nlm_load_dword_daddr(uint64_t addr)
{
	uint64_t val;

	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"ld		%0, 0(%1)\n\t"
		".set	pop\n"
		: "=r"(val)
		: "r"(addr));
	return val;
}

static inline void
nlm_store_dword_daddr(uint64_t addr, uint64_t val)
{
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"sd		%0, 0(%1)\n\t"
		".set	pop\n"
		: : "r"(val), "r"(addr)
		: "memory");
}

#else /* o32 */
static inline uint64_t
nlm_load_word_daddr(uint64_t addr)
{
	uint32_t val, addrhi, addrlo, sr;

	addrhi = addr >> 32;
	addrlo = addr & 0xffffffff;

	sr = nlm_save_flags_kx();
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"dsll32	$8, %1, 0\n\t"
		"dsll32	$9, %2, 0\n\t"
		"dsrl32	$9, $9, 0\n\t"
		"or	$9, $9, $8\n\t"
		"lw	%0, 0($9)\n\t"
		".set	pop\n"
		:	"=r"(val)
		:	"r"(addrhi), "r"(addrlo)
		:	"$8", "$9");
	nlm_restore_flags(sr);

	return val;

}

static inline void
nlm_store_word_daddr(uint64_t addr, uint32_t val)
{
	uint32_t addrhi, addrlo, sr;

	addrhi = addr >> 32;
	addrlo = addr & 0xffffffff;

	sr = nlm_save_flags_kx();
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"dsll32	$8, %1, 0\n\t"
		"dsll32	$9, %2, 0\n\t"
		"dsrl32	$9, $9, 0\n\t"
		"or	$9, $9, $8\n\t"
		"sw	%0, 0($9)\n\t"
		".set	pop\n"
		: : "r"(val), "r"(addrhi), "r"(addrlo)
		:	"$8", "$9", "memory");
	nlm_restore_flags(sr);
}

static inline uint64_t
nlm_load_dword_daddr(uint64_t addr)
{
	uint32_t addrh, addrl, sr;
	uint32_t valh, vall;

	addrh = addr >> 32;
	addrl = addr & 0xffffffff;

	sr = nlm_save_flags_kx();
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"dsll32	$8, %2, 0\n\t"
		"dsll32	$9, %3, 0\n\t"
		"dsrl32	$9, $9, 0\n\t"
		"or	$9, $9, $8\n\t"
		"ld	$8, 0($9)\n\t"
		"dsra32	%0, $8, 0\n\t"
		"sll	%1, $8, 0\n\t"
		".set	pop\n"
		: "=r"(valh), "=r"(vall)
		: "r"(addrh), "r"(addrl)
		: "$8", "$9");
	nlm_restore_flags(sr);

	return ((uint64_t)valh << 32) | vall;
}

static inline void
nlm_store_dword_daddr(uint64_t addr, uint64_t val)
{
	uint32_t addrh, addrl, sr;
	uint32_t valh, vall;

	addrh = addr >> 32;
	addrl = addr & 0xffffffff;
	valh = val >> 32;
	vall = val & 0xffffffff;

	sr = nlm_save_flags_kx();
	__asm__ __volatile__(
		".set	push\n\t"
		".set	mips64\n\t"
		"dsll32	$8, %2, 0\n\t"
		"dsll32	$9, %3, 0\n\t"
		"dsrl32	$9, $9, 0\n\t"
		"or	$9, $9, $8\n\t"
		"dsll32	$8, %0, 0\n\t"
		"dsll32	$10, %1, 0\n\t"
		"dsrl32	$10, $10, 0\n\t"
		"or	$8, $8, $10\n\t"
		"sd	$8, 0($9)\n\t"
		".set	pop\n"
		: :	"r"(valh), "r"(vall), "r"(addrh), "r"(addrl)
		:	"$8", "$9", "memory");
	nlm_restore_flags(sr);
}
#endif /* __mips_n64 */

static inline uint32_t
nlm_read_reg(uint64_t base, uint32_t reg)
{
	volatile uint32_t *addr = (volatile uint32_t *)(long)base + reg;

	return *addr;
}

static inline void
nlm_write_reg(uint64_t base, uint32_t reg, uint32_t val)
{
	volatile uint32_t *addr = (volatile uint32_t *)(long)base + reg;

	*addr = val;
}

static inline uint64_t
nlm_read_reg64(uint64_t base, uint32_t reg)
{
	uint64_t addr = base + (reg >> 1) * sizeof(uint64_t);

	return nlm_load_dword(addr);
}

static inline void
nlm_write_reg64(uint64_t base, uint32_t reg, uint64_t val)
{
	uint64_t addr = base + (reg >> 1) * sizeof(uint64_t);

	return nlm_store_dword(addr, val);
}

/*
 * Routines to store 32/64 bit values to 64 bit addresses,
 * used when going thru XKPHYS to access registers
 */
static inline uint32_t
nlm_read_reg_xkphys(uint64_t base, uint32_t reg)
{
	uint64_t addr = base + reg * sizeof(uint32_t);

	return nlm_load_word_daddr(addr);
}

static inline void
nlm_write_reg_xkphys(uint64_t base, uint32_t reg, uint32_t val)
{
	uint64_t addr = base + reg * sizeof(uint32_t);
	return nlm_store_word_daddr(addr, val);
}

static inline uint64_t
nlm_read_reg64_xkphys(uint64_t base, uint32_t reg)
{
	uint64_t addr = base + (reg >> 1) * sizeof(uint64_t);

	return nlm_load_dword_daddr(addr);
}

static inline void
nlm_write_reg64_xkphys(uint64_t base, uint32_t reg, uint64_t val)
{
	uint64_t addr = base + (reg >> 1) * sizeof(uint64_t);

	return nlm_store_dword_daddr(addr, val);
}

/* Location where IO base is mapped */
extern uint64_t xlp_io_base;

static inline uint64_t
nlm_pcicfg_base(uint32_t devoffset)
{
	return xlp_io_base + devoffset;
}

static inline uint64_t
nlm_xkphys_map_pcibar0(uint64_t pcibase)
{
	uint64_t paddr;

	paddr = nlm_read_reg(pcibase, 0x4) & ~0xfu;
	return (uint64_t)0x9000000000000000 | paddr;
}

#endif