summaryrefslogtreecommitdiffstats
path: root/firmware/zpu/usrp2p/bootloader/spi_bootloader.c
blob: 3e66d41cb9d378c5bac4002944a94763c9577dbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* -*- c -*- */
/*
 * Copyright 2009-2011 Ettus Research LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <hal_io.h>
#include <nonstdio.h>
#include <mdelay.h>
#include <spi_flash.h>
#include <quadradio/flashdir.h>
#include <quadradio/simple_binary_format.h>
#include <stdlib.h>


void hal_uart_init(void);
void spif_init(void);

void pic_interrupt_handler() __attribute__ ((interrupt_handler));

void pic_interrupt_handler()
{
  // nop stub
}

static void
error(int e)
{
  putstr("ERR");
  puthex8(e);
  newline();
}

static void
load(uint32_t flash_addr, uint32_t ram_addr, uint32_t size)
{
  spi_flash_read(flash_addr, size, (void *) ram_addr);
}

static bool
load_from_slot(const struct flashdir *fd, int fw_slot)
{
    putstr("Loading f/w image ");
    putchar('0' + fw_slot);
    putstr("... ");

    if (fw_slot >= fd->fw_nslots){
      error(1);
      return false;
    }

    int slot = fw_slot + fd->fw_slot0;
    if (fd->slot[slot].start == 0 || fd->slot[slot].start == 0xffff
	|| fd->slot[slot].len == 0 || fd->slot[slot].len == 0xffff){
      error(2);
      return false;
    }
    
    uint32_t sbf_base = fd->slot[slot].start << spi_flash_log2_sector_size();
    uint32_t sbf_len  = fd->slot[slot].len << spi_flash_log2_sector_size();
    uint32_t sbf_offset = 0;

    struct sbf_header sbf;
    spi_flash_read(sbf_base, sizeof(struct sbf_header), &sbf);
    if (sbf.magic != SBF_MAGIC || sbf.nsections > SBF_MAX_SECTIONS){
      error(3);
      return false;
    }
    sbf_offset += sizeof(struct sbf_header);

    unsigned int i;
    for (i = 0; i < sbf.nsections; i++){
      if (sbf_offset + sbf.sec_desc[i].length > sbf_len){
	error(4);
	return false;
      }
      load(sbf_offset + sbf_base,
	   sbf.sec_desc[i].target_addr,
	   sbf.sec_desc[i].length);
      sbf_offset += sbf.sec_desc[i].length;
    }
    putstr("Done!");

    typedef void (*fptr_t)(void);
    (*(fptr_t) sbf.entry)();		// almost certainly no return

    return true;
}

int
main(int argc, char **argv)
{
  hal_uart_init();
  spif_init();

  sr_leds->leds =  0;
  mdelay(100);
  sr_leds->leds = ~0;
  mdelay(100);
  sr_leds->leds =  0;

  putstr("\n>>> spi_bootloader <<<\n");

  const struct flashdir *fd = get_flashdir();
  if (fd == 0)
    abort();

  while(1){
    int sw;
    int fw_slot;
    
    sw = readback->switches;
    fw_slot = sw & 0x7;

    if (!load_from_slot(fd, fw_slot)){
      if (fw_slot != 0){
	putstr("Falling back to slot 0\n");
	load_from_slot(fd, 0);
      }
    }
  }
}