Skip to content

Commit

Permalink
Add kburn
Browse files Browse the repository at this point in the history
  • Loading branch information
kendryte747 committed Aug 1, 2024
1 parent dba032e commit 09cca6f
Show file tree
Hide file tree
Showing 22 changed files with 2,238 additions and 14 deletions.
4 changes: 4 additions & 0 deletions arch/riscv/dts/k230_canmv.dts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
status = "okay";
};

&usbotg0 {
status = "okay";
};

&usbotg1 {
status = "okay";
};
Expand Down
4 changes: 4 additions & 0 deletions arch/riscv/dts/k230_canmv_01studio.dts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
status = "okay";
};

&usbotg0 {
status = "okay";
};

&iomux {
pinctrl-names = "default";
pinctrl-0 = <&pins>;
Expand Down
19 changes: 6 additions & 13 deletions board/kendryte/k230_canmv/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,16 @@ int ddr_init_training(void) {
return 0;
}

int board_early_init_f(void) {
/* force set boot medium to sdio1 */
g_boot_medium = BOOT_MEDIUM_SDIO1;
return 0;
}

#ifdef CONFIG_BOARD_LATE_INIT
int board_late_init(void) {
ofnode node;

node = ofnode_by_compatible(ofnode_null(), "kendryte,k230_canmv_v2");
if (ofnode_valid(node)) {
#define SDHCI_EMMC_BASE 0x91580000
#define SDHCI_EMMC_CTRL_R 0x52C
#define EMMC_RST_N_OE 3
#define EMMC_RST_N 2
u32 wifi_regon_ctrl = readl((void *)(SDHCI_EMMC_BASE + SDHCI_EMMC_CTRL_R));
wifi_regon_ctrl |= (1 << EMMC_RST_N_OE);
wifi_regon_ctrl &= ~(1 << EMMC_RST_N);
mdelay(10);
wifi_regon_ctrl |= (1 << EMMC_RST_N);
}

node = ofnode_by_compatible(ofnode_null(), "kendryte,k230_canmv");
if (ofnode_valid(node)) {
#define GPIO_BASE_ADDR0 (0x9140B000U)
Expand Down
6 changes: 6 additions & 0 deletions board/kendryte/k230_canmv_01studio/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ int ddr_init_training(void)
return 0;
}

int board_early_init_f(void) {
/* force set boot medium to sdio0 */
g_boot_medium = BOOT_MEDIUM_SDIO0;
return 0;
}

#ifdef CONFIG_BOARD_LATE_INIT
int board_late_init(void)
{
Expand Down
12 changes: 12 additions & 0 deletions cmd/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1497,6 +1497,18 @@ config CMD_USB_MASS_STORAGE
export a block device: U-Boot, the USB device, acts as a simple
external hard drive plugged on the host USB port.

config CMD_KBURN
bool "kburn"
select KBURN
help
Kendryte burning protocol.

if CMD_KBURN
config CMD_KBURN_BENCHMARK
bool "Enable benchmark command with KBURN"
default n
endif

config CMD_PVBLOCK
bool "Xen para-virtualized block device"
depends on XEN
Expand Down
2 changes: 2 additions & 0 deletions cmd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ obj-$(CONFIG_CMD_W1) += w1.o
obj-$(CONFIG_CMD_ZIP) += zip.o
obj-$(CONFIG_CMD_ZFS) += zfs.o

obj-$(CONFIG_CMD_KBURN) += kburn.o

obj-$(CONFIG_CMD_DFU) += dfu.o
obj-$(CONFIG_CMD_GPT) += gpt.o
obj-$(CONFIG_CMD_MBR) += mbr.o
Expand Down
217 changes: 217 additions & 0 deletions cmd/kburn.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* Copyright (C) 2017 Eddie Cai <[email protected]>
*/

#include <command.h>
#include <console.h>
#include <g_dnl.h>
#include <usb.h>

#include "kburn.h"

static int do_kburn(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
int ret, controller_index;
char *usb_controller;

if (argc < 1)
return CMD_RET_USAGE;

usb_controller = argv[1];
controller_index = simple_strtoul(usb_controller, NULL, 0);

ret = usb_gadget_initialize(controller_index);
if (ret) {
printf("USB init failed: %d\n", ret);
return CMD_RET_FAILURE;
}

g_dnl_clear_detach();
ret = g_dnl_register("usb_dnl_kburn");
if (ret)
return CMD_RET_FAILURE;

if (!g_dnl_board_usb_cable_connected()) {
puts("\rUSB cable not detected, Command exit.\n");
ret = CMD_RET_FAILURE;
goto exit;
}

while (1) {
if (g_dnl_detach())
break;
if (ctrlc())
break;
usb_gadget_handle_interrupts(controller_index);
}
ret = CMD_RET_SUCCESS;

exit:
g_dnl_unregister();
g_dnl_clear_detach();
usb_gadget_release(controller_index);

return ret;
}

U_BOOT_CMD(kburn, 2, 1, do_kburn,
"Canaan usb burner protocol",
"<USB_controller> e.g. kburn 0\n"
);

#if defined (CONFIG_CMD_KBURN_BENCHMARK)
/*****************************************************************************/
#include <common.h>
#include <command.h>
#include <mmc.h>
#include <timer.h>

int test_mmc_write_speed(int dev_num, ulong start_block, ulong block_count)
{
struct mmc *mmc;
ulong start_time, end_time;
ulong write_size = block_count * 512; // Assuming block size is 512 bytes
char *write_buffer;
int ret;

mmc = find_mmc_device(dev_num);
if (!mmc) {
printf("MMC device %d not found\n", dev_num);
return -1;
}

ret = mmc_init(mmc);
if (ret) {
printf("MMC init failed\n");
return -1;
}

write_buffer = malloc(write_size);
if (!write_buffer) {
printf("Memory allocation failed\n");
return -1;
}

memset(write_buffer, 0xAA, write_size); // Fill the buffer with a pattern

start_time = get_timer(0); // Get start time

ret = blk_dwrite(mmc_get_blk_desc(mmc), start_block, block_count, write_buffer);

end_time = get_timer(start_time); // Get end time

if (ret != block_count) {
printf("MMC write failed\n");
free(write_buffer);
return -1;
}

printf("MMC write speed: %lu bytes/sec\n", write_size * 1000 / end_time);

free(write_buffer);
return 0;
}

int do_test_mmc_speed(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
{
int dev_num = 0;
ulong start_block = 0;
ulong block_count = 1024; // Default to 1024 blocks

if (argc > 1)
dev_num = simple_strtol(argv[1], NULL, 10);
if (argc > 2)
start_block = simple_strtol(argv[2], NULL, 10);
if (argc > 3)
block_count = simple_strtol(argv[3], NULL, 10);

return test_mmc_write_speed(dev_num, start_block, block_count);
}

U_BOOT_CMD(
kburn_bench_mmc, 4, 0, do_test_mmc_speed,
"Test MMC write speed",
"[dev_num] [start_block] [block_count] - Test write speed of MMC device"
);

/*****************************************************************************/
#include <common.h>
#include <command.h>
#include <spi.h>
#include <spi_flash.h>
#include <dm.h>
#include <timer.h>

int test_spi_flash_write_speed(int index, ulong offset, ulong size)
{
struct udevice *dev;
struct spi_flash *flash;
ulong start_time, end_time;
char *write_buffer;
int ret;

ret = uclass_get_device(UCLASS_SPI_FLASH, index, &dev);
if (ret) {
printf("SPI flash device %d not found\n", index);
return -1;
}

flash = dev_get_uclass_priv(dev);
if (!flash) {
printf("Failed to get SPI flash info\n");
return -1;
}

write_buffer = malloc(size);
if (!write_buffer) {
printf("Memory allocation failed\n");
return -1;
}

memset(write_buffer, 0xAA, size); // Fill the buffer with a pattern

start_time = get_timer(0); // Get start time

ret = spi_flash_erase(flash, offset, size);
ret += spi_flash_write(flash, offset, size, write_buffer);

end_time = get_timer(start_time); // Get end time

if (ret) {
printf("SPI flash write failed\n");
free(write_buffer);
return -1;
}

printf("SPI flash write speed: %lu bytes/sec\n", size * 1000 / end_time);

free(write_buffer);
return 0;
}

int do_test_spi_flash_speed(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
{
int index = 0;
ulong offset = 0;
ulong size = 1024 * 1024; // Default to 1MB

if (argc > 1)
index = simple_strtol(argv[1], NULL, 10);
if (argc > 2)
offset = simple_strtol(argv[2], NULL, 10);
if (argc > 3)
size = simple_strtol(argv[3], NULL, 10);

return test_spi_flash_write_speed(index, offset, size);
}

U_BOOT_CMD(
kburn_bench_sf, 4, 0, do_test_spi_flash_speed,
"Test SPI flash write speed",
"[index] [offset] [size] - Test write speed of SPI flash device"
);
/*****************************************************************************/

#endif // CONFIG_CMD_KBURN_BENCHMARK
Loading

0 comments on commit 09cca6f

Please sign in to comment.