Added C++ ctors and partial SDHOST driver
authorkristina <kb@kbs-iMac.local>
Sat, 21 May 2016 02:22:03 +0000 (03:22 +0100)
committerkristina <kb@kbs-iMac.local>
Sat, 21 May 2016 02:22:03 +0000 (03:22 +0100)
arm_chainloader/Makefile
arm_chainloader/chainloader.h
arm_chainloader/drivers/cprman.cc [new file with mode: 0755]
arm_chainloader/drivers/sd2.hpp [new file with mode: 0755]
arm_chainloader/drivers/sdhost.cc [new file with mode: 0755]
arm_chainloader/linker.lds

index 05b8945..7427c6c 100755 (executable)
@@ -4,7 +4,10 @@ TARGET_ARM_CHAINLOADER = arm_chainloader.bin
 SRCS = \
        start.s \
        drivers/uart.c \
+       drivers/cprman.cc \
+       drivers/sdhost.cc \
        ../lib/xprintf.c \
+       ../lib/udelay.c \
        firmware_rendezvous.c \
        main.c
 
@@ -27,11 +30,13 @@ OBJ := $(addprefix $(TARGET_BUILD_DIR)/, $(addsuffix .o, $(basename $(SRCS))))
 
 CROSS_COMPILE = arm-none-eabi-
 CC = $(CROSS_COMPILE)gcc
+CXX = $(CROSS_COMPILE)gcc
 AS = $(CC)
 OBJCOPY = $(CROSS_COMPILE)objcopy
 LINKFLAGS = -nostdlib -march=$(ARCH) -Wl,--build-id=none -T linker.lds
 COMMON_FLAGS = -c -nostdlib -nostartfiles -ffreestanding -march=$(ARCH) -I../ -I./ -mfpu=vfp -mfloat-abi=hard -mtune=arm1176jzf-s
 CFLAGS = $(COMMON_FLAGS) -std=c11 
+CXXFLAGS = $(COMMON_FLAGS) -std=c++11 -fno-exceptions -fno-rtti
 ASFLAGS = $(COMMON_FLAGS) -x assembler-with-cpp
 
 HEADERS := \
@@ -54,6 +59,11 @@ $(TARGET_BUILD_DIR)/%.o: %.c $(HEADERS)
        @echo $(WARN_COLOR)CC  $(NO_COLOR) $@
        @$(CC) $(CFLAGS) $< -o $@
 
+$(TARGET_BUILD_DIR)/%.o: %.cc $(HEADERS)
+       $(CREATE_SUBDIR)
+       @echo $(WARN_COLOR)CXX $(NO_COLOR) $@
+       @$(CXX) $(CXXFLAGS) $< -o $@
+
 $(TARGET_BUILD_DIR)/%.o: %.s $(HEADERS)
        $(CREATE_SUBDIR)
        @echo $(WARN_COLOR)AS  $(NO_COLOR) $@
index 4aa7626..63ea608 100755 (executable)
@@ -1,5 +1,25 @@
 #pragma once\r
 \r
+#include <stdint.h>\r
 #include <lib/xprintf.h>\r
 \r
-#define printf xprintf
\ No newline at end of file
+#define printf xprintf\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+extern void udelay(uint32_t time);\r
+\r
+#define STATIC_INIT_PRIORITY(x) __attribute__((init_priority(x)))\r
+\r
+#define STATIC_CPRMAN_DRIVER STATIC_INIT_PRIORITY(101)\r
+#define STATIC_DRIVER STATIC_INIT_PRIORITY(200)\r
+#define STATIC_APP STATIC_INIT_PRIORITY(600)\r
+\r
+#define mfence() __sync_synchronize()\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
diff --git a/arm_chainloader/drivers/cprman.cc b/arm_chainloader/drivers/cprman.cc
new file mode 100755 (executable)
index 0000000..e69de29
diff --git a/arm_chainloader/drivers/sd2.hpp b/arm_chainloader/drivers/sd2.hpp
new file mode 100755 (executable)
index 0000000..27157ea
--- /dev/null
@@ -0,0 +1,86 @@
+#pragma once\r
+\r
+/* Error Code */\r
+#define        SD_NO_ERROR             0x0\r
+#define        SD_CRC_ERR              0x80\r
+#define        SD_TO_ERR               0x40\r
+#define        SD_NO_CARD              0x20\r
+#define SD_BUSY                        0x10\r
+#define        SD_STS_ERR              0x08\r
+#define SD_RSP_TIMEOUT         0x04\r
+#define SD_IO_ERR              0x02\r
+/* Return code for MMC switch bus */\r
+#define SWITCH_SUCCESS         0\r
+#define SWITCH_ERR             1\r
+#define SWITCH_FAIL            2\r
+/* MMC/SD Command Index */\r
+/* Basic command (class 0) */\r
+#define GO_IDLE_STATE          0\r
+#define        SEND_OP_COND            1\r
+#define        ALL_SEND_CID            2\r
+#define        SET_RELATIVE_ADDR       3\r
+#define        SEND_RELATIVE_ADDR      3\r
+#define        SET_DSR                 4\r
+#define IO_SEND_OP_COND                5\r
+#define        SWITCH                  6\r
+#define        SELECT_CARD             7\r
+#define        DESELECT_CARD           7\r
+/* CMD8 is "SEND_EXT_CSD" for MMC4.x Spec\r
+ * while is "SEND_IF_COND" for SD 2.0\r
+ */\r
+#define        SEND_EXT_CSD            8\r
+#define        SEND_IF_COND            8\r
+#define        SEND_CSD                9\r
+#define        SEND_CID                10\r
+#define        VOLTAGE_SWITCH          11\r
+#define        READ_DAT_UTIL_STOP      11\r
+#define        STOP_TRANSMISSION       12\r
+#define        SEND_STATUS             13\r
+#define        GO_INACTIVE_STATE       15\r
+#define        SET_BLOCKLEN            16\r
+#define        READ_SINGLE_BLOCK       17\r
+#define        READ_MULTIPLE_BLOCK     18\r
+#define        SEND_TUNING_PATTERN     19\r
+#define        BUSTEST_R               14\r
+#define        BUSTEST_W               19\r
+#define        WRITE_BLOCK             24\r
+#define        WRITE_MULTIPLE_BLOCK    25\r
+#define        PROGRAM_CSD             27\r
+#define        ERASE_WR_BLK_START      32\r
+#define        ERASE_WR_BLK_END        33\r
+#define        ERASE_CMD               38\r
+#define LOCK_UNLOCK            42\r
+#define        IO_RW_DIRECT            52\r
+#define        APP_CMD                 55\r
+#define        GEN_CMD                 56\r
+#define        SET_BUS_WIDTH           6\r
+#define        SD_STATUS               13\r
+#define        SEND_NUM_WR_BLOCKS      22\r
+#define        SET_WR_BLK_ERASE_COUNT  23\r
+#define        SD_APP_OP_COND          41\r
+#define        SET_CLR_CARD_DETECT     42\r
+#define        SEND_SCR                51\r
+#define        SD_READ_COMPLETE        0x00\r
+#define        SD_READ_TO              0x01\r
+#define        SD_READ_ADVENCE         0x02\r
+#define        SD_CHECK_MODE           0x00\r
+#define        SD_SWITCH_MODE          0x80\r
+#define        SD_FUNC_GROUP_1         0x01\r
+#define        SD_FUNC_GROUP_2         0x02\r
+#define        SD_FUNC_GROUP_3         0x03\r
+#define        SD_FUNC_GROUP_4         0x04\r
+#define        SD_CHECK_SPEC_V1_1      0xFF\r
+#define        NO_ARGUMENT                             0x00\r
+#define        CHECK_PATTERN                           0x000000AA\r
+#define        VOLTAGE_SUPPLY_RANGE                    0x00000100\r
+#define        SUPPORT_HIGH_AND_EXTENDED_CAPACITY      0x40000000\r
+#define        SUPPORT_MAX_POWER_PERMANCE              0x10000000\r
+#define        SUPPORT_1V8                             0x01000000\r
+#define        SWTICH_NO_ERR           0x00\r
+#define        CARD_NOT_EXIST          0x01\r
+#define        SPEC_NOT_SUPPORT        0x02\r
+#define        CHECK_MODE_ERR          0x03\r
+#define        CHECK_NOT_READY         0x04\r
+#define        SWITCH_CRC_ERR          0x05\r
+#define        SWITCH_MODE_ERR         0x06\r
+#define        SWITCH_PASS             0x07\r
diff --git a/arm_chainloader/drivers/sdhost.cc b/arm_chainloader/drivers/sdhost.cc
new file mode 100755 (executable)
index 0000000..22a2a68
--- /dev/null
@@ -0,0 +1,131 @@
+/*=============================================================================\r
+Copyright (C) 2016 Kristina Brooks\r
+All rights reserved.\r
+\r
+This program is free software; you can redistribute it and/or\r
+modify it under the terms of the GNU General Public License\r
+as published by the Free Software Foundation; either version 2\r
+of the License, or (at your option) any later version.\r
+\r
+This program is distributed in the hope that it will be useful,\r
+but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+GNU General Public License for more details.\r
+\r
+FILE DESCRIPTION\r
+SDHOST driver. This used to be known as ALTMMC.\r
+\r
+=============================================================================*/\r
+\r
+#include <chainloader.h>\r
+#include <hardware.h>\r
+\r
+#include "sd2.hpp"\r
+\r
+#define SDEDM_WRITE_THRESHOLD_SHIFT 9\r
+#define SDEDM_READ_THRESHOLD_SHIFT 14\r
+#define SDEDM_THRESHOLD_MASK     0x1f\r
+\r
+#define SAFE_READ_THRESHOLD     4\r
+#define SAFE_WRITE_THRESHOLD    4\r
+\r
+#define logf(fmt, ...) printf("[sdhost::%s]: " fmt, __FUNCTION__, ##__VA_ARGS__);\r
+\r
+struct sdhost_t {\r
+       void set_power(bool on) {\r
+               SH_VDD = on ? SH_VDD_POWER_ON_SET : 0x0;\r
+       }\r
+\r
+       bool wait(uint32_t timeout = 10000) {\r
+               uint32_t t = timeout;\r
+\r
+               while(SH_CMD & SH_CMD_NEW_FLAG_SET) {\r
+                       if (t == 0) {\r
+                               logf("timed out after %dus!\n", timeout)\r
+                               return false;\r
+                       }\r
+                       t--;\r
+                       udelay(10);\r
+               }\r
+\r
+               return true;\r
+       }\r
+\r
+       bool send(uint32_t command, uint32_t arg) {\r
+               if (!wait())\r
+                       return false;\r
+\r
+               SH_ARG = arg;\r
+               SH_CMD = (command & SH_CMD_COMMAND_SET) | SH_CMD_NEW_FLAG_SET;\r
+\r
+               udelay(300);\r
+               mfence();\r
+\r
+               return true;\r
+       }\r
+\r
+       void configure_pinmux() {\r
+               GP_FSEL4 = 0x24000000;\r
+               GP_FSEL5 = 0x924;\r
+               GP_PUD = 2;\r
+\r
+               logf("GPIOs set!\n");\r
+       }\r
+\r
+       void reset() {\r
+               logf("resetting controller ...\n");\r
+\r
+               set_power(false);\r
+\r
+               SH_CMD = 0;\r
+               SH_ARG = 0;\r
+               SH_TOUT = 0xF00000;\r
+               SH_CDIV = 0;\r
+               SH_HSTS = 0x7f8;\r
+               SH_HCFG = 0;\r
+               SH_HBCT = 0;\r
+               SH_HBLC = 0;\r
+       \r
+               uint32_t temp = SH_EDM;\r
+\r
+               temp &= ~((SDEDM_THRESHOLD_MASK<<SDEDM_READ_THRESHOLD_SHIFT) |\r
+                 (SDEDM_THRESHOLD_MASK<<SDEDM_WRITE_THRESHOLD_SHIFT));\r
+               temp |= (SAFE_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) |\r
+                       (SAFE_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT);\r
+\r
+               SH_EDM = temp;\r
+               udelay(300);\r
+\r
+               set_power(true);\r
+\r
+               udelay(300);\r
+               mfence();\r
+       }\r
+\r
+       void init_card() {\r
+               send(GO_IDLE_STATE, 0);\r
+               send(SEND_IF_COND, 0x155);\r
+\r
+               wait();\r
+\r
+               logf("SEND_IF_COND response is: 0x%X\n", SH_RSP0);\r
+       }\r
+\r
+       sdhost_t() {\r
+               logf("starting up ...\n");\r
+\r
+               configure_pinmux();\r
+               reset();\r
+\r
+               SH_HCFG = SH_HCFG_SLOW_CARD_SET | SH_HCFG_WIDE_INT_BUS_SET;\r
+               SH_CDIV = 0x96;\r
+\r
+               udelay(300);\r
+\r
+               logf("sdhost controller ready!\n");\r
+\r
+               init_card();\r
+       }\r
+};\r
+\r
+sdhost_t STATIC_DRIVER g_SDHostDriver {};
\ No newline at end of file
index 672df5d..f7f6624 100755 (executable)
@@ -1,10 +1,39 @@
-MEMORY\r
-{\r
-    ram : ORIGIN = 0x0, LENGTH = 0x100000\r
-}\r
-\r
 SECTIONS\r
 {\r
-    .text : { *(.text*) } > ram\r
-    .bss : { *(.bss*) } > ram\r
+       . = 0x0;\r
+\r
+       _text = .;\r
+\r
+       .text : {\r
+               *(.text)\r
+               *(.text.*)\r
+               *(.gnu.warning)\r
+       }\r
+\r
+       _etext = .;\r
+\r
+       .rodata : { *(.rodata) *(.rodata.*) }\r
+       . = ALIGN(4096);\r
+       _erodata = .;\r
+\r
+       . = ALIGN(32 / 8);\r
+\r
+       PROVIDE (__init_array_start = .);\r
+       .init_array : {\r
+               *(.init_array)\r
+               *(.init_array.*)\r
+       }\r
+       PROVIDE (__init_array_end = .);\r
+\r
+       .data : {                       /* Data */\r
+               *(.data)\r
+       }\r
+\r
+       .bss : {\r
+               _edata = .;\r
+               __bss_start = .;\r
+               *(.bss)\r
+       }\r
+\r
+       _end = . ;\r
 }
\ No newline at end of file
This page took 0.039265 seconds and 4 git commands to generate.