1/*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2020 Keith Packard
5 * Copyright © 2024 Stephen Street
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above
15 *    copyright notice, this list of conditions and the following
16 *    disclaimer in the documentation and/or other materials provided
17 *    with the distribution.
18 *
19 * 3. Neither the name of the copyright holder nor the names of its
20 *    contributors may be used to endorse or promote products derived
21 *    from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34 * OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36#include <picolibc.h>
37
38#include "arm_tls.h"
39/*
40 * This cannot be a C ABI function as the compiler assumes that it
41 * does not modify anything other than r0 and lr.
42 */
43	.syntax unified
44	.section .text.__aeabi_read_tp
45	.align 4
46	.p2align 4,,15
47	.global __aeabi_read_tp
48	.type __aeabi_read_tp,%function
49#ifdef __thumb__
50	.thumb
51#endif
52
53__aeabi_read_tp:
54	.cfi_sections .debug_frame
55	.cfi_startproc
56
57#ifdef ARM_TLS_CP15
58	mrc p15, 0, r0, c13, c0, 3
59	bx lr
60#else
61
62#ifdef ARM_RP2040
63
64	/*
65	 * Code for RP2040 processor. This is a dual-core cortex-m0
66	 * design. We use the CPUID register, stored at offset zero of
67	 * the Single-cycle IO block at 0xd0000000
68	 */
69
70#define TLS_SIZE	(4 * RP2040_NCORE)
71
72	push {r1,lr}		/* Save R1 (and LR) */
73	ldr r1,=0xd0000000	/* Address of SIO->CPUID */
74	ldr r1,[r1]	   	/* Fetch active core */
75	lsls r1,r1,#2	   	/* Multiply by 4 */
76	ldr r0,=__tls		/* Address of __tls array */
77	ldr r0,[r0,r1]		/* Fetch __tls[CPUID] */
78	pop {r1,pc}		/* Restore R1 and return  */
79
80#else
81
82#define TLS_SIZE	4
83
84	ldr r0,=__tls		/* Load the address of __tls */
85	ldr r0,[r0]		/* Dereference to get the value of __tls */
86	bx lr			/* All done, return to caller */
87
88#endif
89
90#endif
91
92	.cfi_endproc
93
94#ifndef ARM_TLS_CP15
95	.global __tls
96	.bss
97	.align 4
98	.type __tls, %object
99	.size __tls, TLS_SIZE
100__tls:
101	.space TLS_SIZE
102
103#endif
104