OSDN Git Service

d757ce22173cb1b8f3cb000059969bc6f19b7b38
[nxt-jsp/lejos_nxj.git] / nxtOSEK / lejos_nxj / src / libnxt / flash.c
1 /**
2  * NXT bootstrap interface; NXT flash chip code.
3  *
4  * Copyright 2006 David Anderson <david.anderson@calixo.net>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  */
21
22 #include <stdio.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28
29 #include "error.h"
30 #include "lowlevel.h"
31 #include "samba.h"
32 #include "flash.h"
33
34 enum nxt_flash_commands
35 {
36   FLASH_CMD_LOCK = 0x2,
37   FLASH_CMD_UNLOCK = 0x4,
38 };
39
40 nxt_error_t
41 nxt_flash_wait_ready(nxt_t *nxt)
42 {
43   nxt_word_t flash_status;
44
45   do
46     {
47       NXT_ERR(nxt_read_word(nxt, 0xFFFFFF68, &flash_status));
48
49       /* Bit 0 is the FRDY field. Set to 1 if the flash controller is
50        * ready to run a new command.
51        */
52     } while (!(flash_status & 0x1));
53
54   return NXT_OK;
55 }
56
57 static nxt_error_t
58 nxt_flash_alter_lock(nxt_t *nxt, int region_num,
59                      enum nxt_flash_commands cmd)
60 {
61   nxt_word_t w = 0x5A000000 | ((64 * region_num) << 8);
62   w += cmd;
63
64   NXT_ERR(nxt_flash_wait_ready(nxt));
65
66   /* Flash mode register: FCMN 0x5, FWS 0x1
67    * Flash command register: KEY 0x5A, FCMD = clear-lock-bit (0x4)
68    * Flash mode register: FCMN 0x34, FWS 0x1
69    */
70   NXT_ERR(nxt_write_word(nxt, 0xFFFFFF60, 0x00050100));
71   NXT_ERR(nxt_write_word(nxt, 0xFFFFFF64, w));
72   NXT_ERR(nxt_write_word(nxt, 0xFFFFFF60, 0x00340100));
73
74   return NXT_OK;
75 }
76
77
78 nxt_error_t
79 nxt_flash_lock_region(nxt_t *nxt, int region_num)
80 {
81   return nxt_flash_alter_lock(nxt, region_num, FLASH_CMD_LOCK);
82 }
83
84
85 nxt_error_t
86 nxt_flash_unlock_region(nxt_t *nxt, int region_num)
87 {
88   return nxt_flash_alter_lock(nxt, region_num, FLASH_CMD_UNLOCK);
89 }
90
91
92 nxt_error_t
93 nxt_flash_lock_all_regions(nxt_t *nxt)
94 {
95   int i;
96
97   for (i = 0; i < 16; i++)
98     NXT_ERR(nxt_flash_lock_region(nxt, i));
99
100   return NXT_OK;
101 }
102
103
104 nxt_error_t
105 nxt_flash_unlock_all_regions(nxt_t *nxt)
106 {
107   int i;
108
109   for (i = 0; i < 16; i++)
110     NXT_ERR(nxt_flash_unlock_region(nxt, i));
111
112   return NXT_OK;
113 }