Skip to content

Commit

Permalink
Merge pull request #212 from sifive/dev_emmanuelp
Browse files Browse the repository at this point in the history
HCA 0.5.x Bare metal generator
  • Loading branch information
bsousi5 committed Apr 24, 2020
2 parents ee9010d + 57043ac commit 430bd38
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ freedom_bare_header_generator_SOURCES = \
bare_header/sifive_uart0.h \
bare_header/sifive_wdog0.h \
bare_header/ucb_htif0.h \
bare_header/sifive_hca_0_5_x.h \
ranges.c++ \
ranges.h

Expand Down
1 change: 1 addition & 0 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ freedom_bare_header_generator_SOURCES = \
bare_header/sifive_uart0.h \
bare_header/sifive_wdog0.h \
bare_header/ucb_htif0.h \
bare_header/sifive_hca_0_5_x.h \
ranges.c++ \
ranges.h

Expand Down
37 changes: 35 additions & 2 deletions bare_header/device.c++
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void Device::emit_comment(const node &n) {

string Device::def_handle(const node &n) {
string name = n.get_fields<string>("compatible")[0];

return def_handle(name, n);
}

string Device::def_handle(std::string name, const node &n) {
string instance = n.instance();

std::transform(name.begin(), name.end(), name.begin(),
Expand All @@ -54,13 +59,18 @@ string Device::def_handle(const node &n) {
}
return toupper(c);
});

std::transform(instance.begin(), instance.end(), instance.begin(), toupper);

return "METAL_" + name + "_" + instance;
}

string Device::def_handle_index(const node &n) {
string name = n.get_fields<string>("compatible")[0];
return def_handle_index(name, n);
}

string Device::def_handle_index(std::string name, const node &n) {
string instance = std::to_string(get_index(n));

std::transform(name.begin(), name.end(), name.begin(),
Expand Down Expand Up @@ -132,6 +142,18 @@ void Device::emit_base(const node &n) {
}
}

void Device::emit_base(std::string compat, const node &n) {
os << "#define " << def_handle(compat, n) << "_" METAL_BASE_ADDRESS_LABEL
<< " " << base_address(n) << "UL" << std::endl;

// If the address is very small, it already is an index.
if (n.instance().length() > 2) {
os << "#define " << def_handle_index(compat, n)
<< "_" METAL_BASE_ADDRESS_LABEL << " " << base_address(n) << "UL"
<< std::endl;
}
}

uint64_t Device::size(const node &n) { return extract_mem_map(n).size; }

void Device::emit_size(const node &n) {
Expand All @@ -145,12 +167,23 @@ void Device::emit_size(const node &n) {
}
}

void Device::emit_size(std::string compat, const node &n) {
os << "#define " << def_handle(compat, n) << "_" << METAL_SIZE_LABEL << " "
<< size(n) << "UL" << std::endl;

// If the address is very small, it already is an index.
if (n.instance().length() > 2) {
os << "#define " << def_handle_index(compat, n) << "_" << METAL_SIZE_LABEL
<< " " << size(n) << "UL" << std::endl;
}
}

void Device::emit_compat() { emit_compat(compat_string); }

void Device::emit_compat(string compat) {
std::transform(compat.begin(), compat.end(), compat.begin(),
[](unsigned char c) -> char {
if (c == ',' || c == '-') {
if (c == ',' || c == '-' || c == '.') {
return '_';
}
return toupper(c);
Expand All @@ -161,7 +194,7 @@ void Device::emit_compat(string compat) {
void Device::emit_offset(string name, string offset_name, uint32_t offset) {
std::transform(name.begin(), name.end(), name.begin(),
[](unsigned char c) -> char {
if (c == ',' || c == '-') {
if (c == ',' || c == '-' || c == '.') {
return '_';
}
return toupper(c);
Expand Down
4 changes: 4 additions & 0 deletions bare_header/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class Device {
void emit_comment(const node &n);

string def_handle(const node &n);
string def_handle(string name, const node &n);
string def_handle_index(const node &n);
string def_handle_index(string name, const node &n);
virtual uint64_t base_address(const node &n);
void emit_base(const node &n);
void emit_base(string name, const node &n);

virtual uint64_t size(const node &n);
void emit_size(const node &n);
void emit_size(string name, const node &n);

void emit_compat();
void emit_compat(string compat);
Expand Down
2 changes: 2 additions & 0 deletions bare_header/freedom-bare_header-generator.c++
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "bare_header/sifive_gpio_buttons.h"
#include "bare_header/sifive_gpio_leds.h"
#include "bare_header/sifive_gpio_switches.h"
#include "bare_header/sifive_hca_0_5_x.h"
#include "bare_header/sifive_i2c0.h"
#include "bare_header/sifive_local_external_interrupts0.h"
#include "bare_header/sifive_pwm0.h"
Expand Down Expand Up @@ -172,6 +173,7 @@ static void write_config_file(const fdt &dtb, fstream &os, std::string cfg_file,
devices.push_back(new sifive_uart0(os, dtb));
devices.push_back(new sifive_wdog0(os, dtb));
devices.push_back(new ucb_htif0(os, dtb));
devices.push_back(new sifive_hca_0_5_x(os, dtb));

for (auto it = devices.begin(); it != devices.end(); it++) {
(*it)->emit_defines();
Expand Down
74 changes: 74 additions & 0 deletions bare_header/sifive_hca_0_5_x.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* Copyright 2019 SiFive, Inc */
/* SPDX-License-Identifier: Apache-2.0 */

#ifndef __BARE_HEADER_SIFIVE_HCA_0_5_x__H
#define __BARE_HEADER_SIFIVE_HCA_0_5_x__H

#include "bare_header/device.h"

#include <cstdio>
#include <regex>
#include <set>

class sifive_hca_0_5_x : public Device {
public:
sifive_hca_0_5_x(std::ostream &os, const fdt &dtb)
: Device(os, dtb, "sifive,hca-0.5.(\\d+)") {}

void emit_defines() override {
emit_compat("sifive,hca");
os << std::endl;

dtb.match(std::regex(compat_string), [&](node n) {
string instance = n.get_fields<string>("compatible")[0];
int major, minor, patch;

emit_comment(n);
int ret = std::sscanf(instance.c_str(), "sifive,hca-%d.%d.%d", &major,
&minor, &patch);
if (ret == 3) {
emit_offset("sifive,hca", "VERSION",
(major << 16) + (minor << 8) + patch);
os << std::endl;
}

emit_base("sifive,hca", n);
emit_size("sifive,hca", n);

os << std::endl;
});
}

void emit_offsets() override {
if (dtb.match(std::regex(compat_string), [](node n) {}) != 0) {
/* Add offsets here */
emit_offset("sifive,hca", "CR", 0x0);
emit_offset("sifive,hca", "AES_CR", 0x10);
emit_offset("sifive,hca", "AES_ALEN", 0x20);
emit_offset("sifive,hca", "AES_PDLEN", 0x28);
emit_offset("sifive,hca", "AES_KEY", 0x30);
emit_offset("sifive,hca", "AES_INITV", 0x50);
emit_offset("sifive,hca", "SHA_CR", 0x60);
emit_offset("sifive,hca", "FIFO_IN", 0x70);
emit_offset("sifive,hca", "AES_OUT", 0x80);
emit_offset("sifive,hca", "AES_AUTH", 0x90);
emit_offset("sifive,hca", "HASH", 0xA0);
emit_offset("sifive,hca", "TRNG_CR", 0xE0);
emit_offset("sifive,hca", "TRNG_SR", 0xE4);
emit_offset("sifive,hca", "TRNG_DATA", 0xE8);
emit_offset("sifive,hca", "TRNG_TRIM", 0xEC);
emit_offset("sifive,hca", "DMA_CR", 0x110);
emit_offset("sifive,hca", "DMA_LEN", 0x114);
emit_offset("sifive,hca", "DMA_SRC", 0x118);
emit_offset("sifive,hca", "DMA_DEST", 0x11C);
emit_offset("sifive,hca", "HCA_REV", 0x200);
emit_offset("sifive,hca", "AES_REV", 0x204);
emit_offset("sifive,hca", "SHA_REV", 0x208);
emit_offset("sifive,hca", "TRNG_REV", 0x20C);

os << std::endl;
}
}
};

#endif

0 comments on commit 430bd38

Please sign in to comment.