Squashed 'third_party/pico-sdk/' content from commit 2062372d2

Change-Id: Ic20f199d3ed0ea8d3a6a1bbf513f875ec7500cc6
git-subtree-dir: third_party/pico-sdk
git-subtree-split: 2062372d203b372849d573f252cf7c6dc2800c0a
Signed-off-by: Austin Schuh <austin.linux@gmail.com>
diff --git a/src/rp2_common/pico_bootrom/CMakeLists.txt b/src/rp2_common/pico_bootrom/CMakeLists.txt
new file mode 100644
index 0000000..58ea575
--- /dev/null
+++ b/src/rp2_common/pico_bootrom/CMakeLists.txt
@@ -0,0 +1,8 @@
+add_library(pico_bootrom INTERFACE)
+
+target_sources(pico_bootrom INTERFACE
+        ${CMAKE_CURRENT_LIST_DIR}/bootrom.c
+        )
+
+target_include_directories(pico_bootrom INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include)
+target_link_libraries(pico_bootrom INTERFACE pico_base_headers)
diff --git a/src/rp2_common/pico_bootrom/bootrom.c b/src/rp2_common/pico_bootrom/bootrom.c
new file mode 100644
index 0000000..af7d091
--- /dev/null
+++ b/src/rp2_common/pico_bootrom/bootrom.c
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include "pico/bootrom.h"
+
+/// \tag::table_lookup[]
+
+// Bootrom function: rom_table_lookup
+// Returns the 32 bit pointer into the ROM if found or NULL otherwise.
+typedef void *(*rom_table_lookup_fn)(uint16_t *table, uint32_t code);
+
+// Convert a 16 bit pointer stored at the given rom address into a 32 bit pointer
+#define rom_hword_as_ptr(rom_address) (void *)(uintptr_t)(*(uint16_t *)rom_address)
+
+void *rom_func_lookup(uint32_t code) {
+    return rom_func_lookup_inline(code);
+}
+
+void *rom_data_lookup(uint32_t code) {
+    rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(0x18);
+    uint16_t *data_table = (uint16_t *) rom_hword_as_ptr(0x16);
+    return rom_table_lookup(data_table, code);
+}
+/// \end::table_lookup[]
+
+bool rom_funcs_lookup(uint32_t *table, unsigned int count) {
+    bool ok = true;
+    for (unsigned int i = 0; i < count; i++) {
+        table[i] = (uintptr_t) rom_func_lookup(table[i]);
+        if (!table[i]) ok = false;
+    }
+    return ok;
+}
diff --git a/src/rp2_common/pico_bootrom/include/pico/bootrom.h b/src/rp2_common/pico_bootrom/include/pico/bootrom.h
new file mode 100644
index 0000000..e557893
--- /dev/null
+++ b/src/rp2_common/pico_bootrom/include/pico/bootrom.h
@@ -0,0 +1,162 @@
+/*
+ * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef _PLATFORM_BOOTROM_H
+#define _PLATFORM_BOOTROM_H
+
+#include "pico.h"
+
+/** \file bootrom.h
+ * \defgroup pico_bootrom pico_bootrom
+ * Access to functions and data in the RP2040 bootrom
+ *
+ * This header may be included by assembly code
+ */
+
+// ROM FUNCTIONS
+
+#define ROM_FUNC_POPCOUNT32             ROM_TABLE_CODE('P', '3')
+#define ROM_FUNC_REVERSE32              ROM_TABLE_CODE('R', '3')
+#define ROM_FUNC_CLZ32                  ROM_TABLE_CODE('L', '3')
+#define ROM_FUNC_CTZ32                  ROM_TABLE_CODE('T', '3')
+#define ROM_FUNC_MEMSET                 ROM_TABLE_CODE('M', 'S')
+#define ROM_FUNC_MEMSET4                ROM_TABLE_CODE('S', '4')
+#define ROM_FUNC_MEMCPY                 ROM_TABLE_CODE('M', 'C')
+#define ROM_FUNC_MEMCPY44               ROM_TABLE_CODE('C', '4')
+#define ROM_FUNC_RESET_USB_BOOT         ROM_TABLE_CODE('U', 'B')
+#define ROM_FUNC_CONNECT_INTERNAL_FLASH ROM_TABLE_CODE('I', 'F')
+#define ROM_FUNC_FLASH_EXIT_XIP         ROM_TABLE_CODE('E', 'X')
+#define ROM_FUNC_FLASH_RANGE_ERASE      ROM_TABLE_CODE('R', 'E')
+#define ROM_FUNC_FLASH_RANGE_PROGRAM    ROM_TABLE_CODE('R', 'P')
+#define ROM_FUNC_FLASH_FLUSH_CACHE      ROM_TABLE_CODE('F', 'C')
+#define ROM_FUNC_FLASH_ENTER_CMD_XIP    ROM_TABLE_CODE('C', 'X')
+
+/*! \brief Return a bootrom lookup code based on two ASCII characters
+ * \ingroup pico_bootrom
+ *
+ * These codes are uses to lookup data or function addresses in the bootrom
+ *
+ * \param c1 the first character
+ * \param c2 the second character
+ * \return the 'code' to use in rom_func_lookup() or rom_data_lookup()
+ */
+#define ROM_TABLE_CODE(c1, c2) ((c1) | ((c2) << 8))
+
+#ifndef __ASSEMBLER__
+
+// ROM FUNCTION SIGNATURES
+
+typedef uint32_t (*rom_popcount32_fn)(uint32_t);
+typedef uint32_t (*rom_reverse32_fn)(uint32_t);
+typedef uint32_t (*rom_clz32_fn)(uint32_t);
+typedef uint32_t (*rom_ctz32_fn)(uint32_t);
+typedef uint8_t *(*rom_memset_fn)(uint8_t *, uint8_t, uint32_t);
+typedef uint32_t *(*rom_memset4_fn)(uint32_t *, uint8_t, uint32_t);
+typedef uint32_t *(*rom_memcpy_fn)(uint8_t *, const uint8_t *, uint32_t);
+typedef uint32_t *(*rom_memcpy44_fn)(uint32_t *, const uint32_t *, uint32_t);
+typedef void __attribute__((noreturn)) (*rom_reset_usb_boot_fn)(uint32_t, uint32_t);
+typedef rom_reset_usb_boot_fn reset_usb_boot_fn; // kept for backwards compatibility
+typedef void (*rom_connect_internal_flash_fn)(void);
+typedef void (*rom_flash_exit_xip_fn)(void);
+typedef void (*rom_flash_range_erase_fn)(uint32_t, size_t, uint32_t, uint8_t);
+typedef void (*rom_flash_range_program_fn)(uint32_t, const uint8_t*, size_t);
+typedef void (*rom_flash_flush_cache_fn)(void);
+typedef void (*rom_flash_enter_cmd_xip_fn)(void);
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*! \brief Return a bootrom lookup code based on two ASCII characters
+ * \ingroup pico_bootrom
+ *
+ * These codes are uses to lookup data or function addresses in the bootrom
+ *
+ * \param c1 the first character
+ * \param c2 the second character
+ * \return the 'code' to use in rom_func_lookup() or rom_data_lookup()
+ */
+static inline uint32_t rom_table_code(uint8_t c1, uint8_t c2) {
+    return ROM_TABLE_CODE((uint32_t) c1, (uint32_t) c2);
+}
+
+/*!
+ * \brief Lookup a bootrom function by code
+ * \ingroup pico_bootrom
+ * \param code the code
+ * \return a pointer to the function, or NULL if the code does not match any bootrom function
+ */
+void *rom_func_lookup(uint32_t code);
+
+/*!
+ * \brief Lookup a bootrom address by code
+ * \ingroup pico_bootrom
+ * \param code the code
+ * \return a pointer to the data, or NULL if the code does not match any bootrom function
+ */
+void *rom_data_lookup(uint32_t code);
+
+/*!
+ * \brief Helper function to lookup the addresses of multiple bootrom functions
+ * \ingroup pico_bootrom
+ *
+ * This method looks up the 'codes' in the table, and convert each table entry to the looked up
+ * function pointer, if there is a function for that code in the bootrom.
+ *
+ * \param table an IN/OUT array, elements are codes on input, function pointers on success.
+ * \param count the number of elements in the table
+ * \return true if all the codes were found, and converted to function pointers, false otherwise
+ */
+bool rom_funcs_lookup(uint32_t *table, unsigned int count);
+
+// Bootrom function: rom_table_lookup
+// Returns the 32 bit pointer into the ROM if found or NULL otherwise.
+typedef void *(*rom_table_lookup_fn)(uint16_t *table, uint32_t code);
+
+// Convert a 16 bit pointer stored at the given rom address into a 32 bit pointer
+#define rom_hword_as_ptr(rom_address) (void *)(uintptr_t)(*(uint16_t *)rom_address)
+
+/*!
+ * \brief Lookup a bootrom function by code. This method is forceably inlined into the caller for FLASH/RAM sensitive code usage
+ * \ingroup pico_bootrom
+ * \param code the code
+ * \return a pointer to the function, or NULL if the code does not match any bootrom function
+ */
+static __force_inline void *rom_func_lookup_inline(uint32_t code) {
+    rom_table_lookup_fn rom_table_lookup = (rom_table_lookup_fn) rom_hword_as_ptr(0x18);
+    uint16_t *func_table = (uint16_t *) rom_hword_as_ptr(0x14);
+    return rom_table_lookup(func_table, code);
+}
+
+/*!
+ * \brief Reboot the device into BOOTSEL mode
+ * \ingroup pico_bootrom
+ *
+ * This function reboots the device into the BOOTSEL mode ('usb boot").
+ *
+ * Facilities are provided to enable an "activity light" via GPIO attached LED for the USB Mass Storage Device,
+ * and to limit the USB interfaces exposed.
+ *
+ * \param usb_activity_gpio_pin_mask 0 No pins are used as per a cold boot. Otherwise a single bit set indicating which
+ *                               GPIO pin should be set to output and raised whenever there is mass storage activity
+ *                               from the host.
+ * \param disable_interface_mask value to control exposed interfaces
+ *  - 0 To enable both interfaces (as per a cold boot)
+ *  - 1 To disable the USB Mass Storage Interface
+ *  - 2 To disable the USB PICOBOOT Interface
+ */
+static inline void __attribute__((noreturn)) reset_usb_boot(uint32_t usb_activity_gpio_pin_mask,
+                                                            uint32_t disable_interface_mask) {
+    rom_reset_usb_boot_fn func = (rom_reset_usb_boot_fn) rom_func_lookup(ROM_FUNC_RESET_USB_BOOT);
+    func(usb_activity_gpio_pin_mask, disable_interface_mask);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // !__ASSEMBLER__
+#endif
diff --git a/src/rp2_common/pico_bootrom/include/pico/bootrom/sf_table.h b/src/rp2_common/pico_bootrom/include/pico/bootrom/sf_table.h
new file mode 100644
index 0000000..4eb4b1c
--- /dev/null
+++ b/src/rp2_common/pico_bootrom/include/pico/bootrom/sf_table.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef _PICO_SF_TABLE_H
+#define _PICO_SF_TABLE_H
+
+// NOTE THESE FUNCTION IMPLEMENTATIONS MATCH THE BEHAVIOR DESCRIBED IN THE BOOTROM SECTION OF THE RP2040 DATASHEET
+
+#define SF_TABLE_FADD               0x00
+#define SF_TABLE_FSUB               0x04
+#define SF_TABLE_FMUL               0x08
+#define SF_TABLE_FDIV               0x0c
+#define SF_TABLE_FCMP_FAST          0x10
+#define SF_TABLE_FCMP_FAST_FLAGS    0x14
+#define SF_TABLE_FSQRT              0x18
+#define SF_TABLE_FLOAT2INT          0x1c
+#define SF_TABLE_FLOAT2FIX          0x20
+#define SF_TABLE_FLOAT2UINT         0x24
+#define SF_TABLE_FLOAT2UFIX         0x28
+#define SF_TABLE_INT2FLOAT          0x2c
+#define SF_TABLE_FIX2FLOAT          0x30
+#define SF_TABLE_UINT2FLOAT         0x34
+#define SF_TABLE_UFIX2FLOAT         0x38
+#define SF_TABLE_FCOS               0x3c
+#define SF_TABLE_FSIN               0x40
+#define SF_TABLE_FTAN               0x44
+#define SF_TABLE_V3_FSINCOS         0x48
+#define SF_TABLE_FEXP               0x4c
+#define SF_TABLE_FLN                0x50
+
+#define SF_TABLE_V1_SIZE            0x54
+
+#define SF_TABLE_FCMP_BASIC         0x54
+#define SF_TABLE_FATAN2             0x58
+#define SF_TABLE_INT642FLOAT        0x5c
+#define SF_TABLE_FIX642FLOAT        0x60
+#define SF_TABLE_UINT642FLOAT       0x64
+#define SF_TABLE_UFIX642FLOAT       0x68
+#define SF_TABLE_FLOAT2INT64        0x6c
+#define SF_TABLE_FLOAT2FIX64        0x70
+#define SF_TABLE_FLOAT2UINT64       0x74
+#define SF_TABLE_FLOAT2UFIX64       0x78
+#define SF_TABLE_FLOAT2DOUBLE       0x7c
+
+#define SF_TABLE_V2_SIZE            0x80
+
+#endif