summaryrefslogtreecommitdiff
path: root/Host/libnxt-0.3/flash.c
blob: d757ce22173cb1b8f3cb000059969bc6f19b7b38 (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
/**
 * NXT bootstrap interface; NXT flash chip code.
 *
 * Copyright 2006 David Anderson <david.anderson@calixo.net>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>

#include "error.h"
#include "lowlevel.h"
#include "samba.h"
#include "flash.h"

enum nxt_flash_commands
{
  FLASH_CMD_LOCK = 0x2,
  FLASH_CMD_UNLOCK = 0x4,
};

nxt_error_t
nxt_flash_wait_ready(nxt_t *nxt)
{
  nxt_word_t flash_status;

  do
    {
      NXT_ERR(nxt_read_word(nxt, 0xFFFFFF68, &flash_status));

      /* Bit 0 is the FRDY field. Set to 1 if the flash controller is
       * ready to run a new command.
       */
    } while (!(flash_status & 0x1));

  return NXT_OK;
}

static nxt_error_t
nxt_flash_alter_lock(nxt_t *nxt, int region_num,
                     enum nxt_flash_commands cmd)
{
  nxt_word_t w = 0x5A000000 | ((64 * region_num) << 8);
  w += cmd;

  NXT_ERR(nxt_flash_wait_ready(nxt));

  /* Flash mode register: FCMN 0x5, FWS 0x1
   * Flash command register: KEY 0x5A, FCMD = clear-lock-bit (0x4)
   * Flash mode register: FCMN 0x34, FWS 0x1
   */
  NXT_ERR(nxt_write_word(nxt, 0xFFFFFF60, 0x00050100));
  NXT_ERR(nxt_write_word(nxt, 0xFFFFFF64, w));
  NXT_ERR(nxt_write_word(nxt, 0xFFFFFF60, 0x00340100));

  return NXT_OK;
}


nxt_error_t
nxt_flash_lock_region(nxt_t *nxt, int region_num)
{
  return nxt_flash_alter_lock(nxt, region_num, FLASH_CMD_LOCK);
}


nxt_error_t
nxt_flash_unlock_region(nxt_t *nxt, int region_num)
{
  return nxt_flash_alter_lock(nxt, region_num, FLASH_CMD_UNLOCK);
}


nxt_error_t
nxt_flash_lock_all_regions(nxt_t *nxt)
{
  int i;

  for (i = 0; i < 16; i++)
    NXT_ERR(nxt_flash_lock_region(nxt, i));

  return NXT_OK;
}


nxt_error_t
nxt_flash_unlock_all_regions(nxt_t *nxt)
{
  int i;

  for (i = 0; i < 16; i++)
    NXT_ERR(nxt_flash_unlock_region(nxt, i));

  return NXT_OK;
}