2 * Copyright (c) 2006, Swedish Institute of Computer Science
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)$Id: rom.c,v 1.2 2006/12/01 15:06:42 bg- Exp $
40 unsigned short ie1
, ie2
, wdtctl
;
44 mask_intr(struct ictx
*c
)
46 /* Disable all interrupts. */
49 #define WDTRPW 0x6900 /* Watchdog key returned by read */
51 /* Disable all NMI-Interrupt sources */
53 c
->wdtctl
^= (WDTRPW
^ WDTPW
);
54 WDTCTL
= WDTPW
| WDTHOLD
;
60 /* MSP430F1611 257 < f < 476 kHz, 2.4576MHz/(5+1) = 409.6 kHz. */
61 FCTL2
= FWKEY
| FSSEL_SMCLK
| (FN2
| FN1
);
62 FCTL3
= FWKEY
; /* Unlock flash. */
66 rest_intr(struct ictx
*c
)
68 FCTL1
= FWKEY
; /* Disable erase or write. */
69 FCTL3
= FWKEY
| LOCK
; /* Lock flash. */
70 /* Restore interrupts. */
78 * This helper routine must reside in RAM!
81 blkwrt(void *to
, const void *from
, const void *from_end
)
82 // __attribute__ ((section(".data")))
86 rom_erase(long nbytes
, off_t offset
)
89 char *to
= (char *)(uintptr_t)offset
;
92 if(nbytes
% ROM_ERASE_UNIT_SIZE
!= 0) {
96 if(offset
% ROM_ERASE_UNIT_SIZE
!= 0) {
103 FCTL1
= FWKEY
| ERASE
; /* Segment erase. */
104 *to
= 0; /* Erase segment containing to. */
105 nbytes
-= ROM_ERASE_UNIT_SIZE
;
106 to
+= ROM_ERASE_UNIT_SIZE
;
115 rom_pwrite(const void *buf
, int nbytes
, off_t offset
)
117 const char *from
= buf
;
119 char *to
= (char *)(uintptr_t)offset
;
125 int n
= (nbytes
> 64) ? 64 : nbytes
;
126 FCTL1
= FWKEY
| BLKWRT
| WRT
; /* Enable block write. */
127 blkwrt(to
, from
, from
+ n
);
140 * This helper routine must reside in RAM!
144 asm(".type blkwrt,@function");
145 asm(".section .data");
148 blkwrt(void *_to
, const void *_from
, const void *_from_end
)
150 unsigned short *to
= _to
;
151 const unsigned short *from
= _from
;
152 const unsigned short *from_end
= _from_end
;
155 while(!(FCTL3
& WAIT
));
156 } while(from
< from_end
);
157 FCTL1
= FWKEY
; /* Disable block write. */
158 /* Now ROM is available again! */