blob: 7b82331a10c7cde7dd4d703d09198190e6ba6bd7 [file] [log] [blame]
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +00001/*
2 * am29f040.c: driver for programming AMD am29f040b models
3 *
4 *
5 * Copyright 2000 Silicon Integrated System Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000021 * Reference:
22 * AMD Am29F040B data sheet
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000023 */
24
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000025#include <stdio.h>
Ollie Lho184a4042005-11-26 21:55:36 +000026#include <stdint.h>
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000027#include "flash.h"
28#include "jedec.h"
Ollie Lho184a4042005-11-26 21:55:36 +000029#include "debug.h"
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000030
Ollie Lho184a4042005-11-26 21:55:36 +000031static __inline__ int erase_sector_29f040b(volatile uint8_t *bios,
Ollie Lho761bf1b2004-03-20 16:46:10 +000032 unsigned long address)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000033{
Ollie Lho761bf1b2004-03-20 16:46:10 +000034 *(bios + 0x555) = 0xAA;
35 *(bios + 0x2AA) = 0x55;
36 *(bios + 0x555) = 0x80;
37 *(bios + 0x555) = 0xAA;
38 *(bios + 0x2AA) = 0x55;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000039 *(bios + address) = 0x30;
40
41 sleep(2);
42
43 /* wait for Toggle bit ready */
44 toggle_ready_jedec(bios + address);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000045
Ollie Lho761bf1b2004-03-20 16:46:10 +000046 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000047}
48
Ollie Lho184a4042005-11-26 21:55:36 +000049static __inline__ int write_sector_29f040b(volatile uint8_t *bios,
50 uint8_t *src,
51 volatile uint8_t *dst,
Ollie Lho761bf1b2004-03-20 16:46:10 +000052 unsigned int page_size)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000053{
54 int i;
55
56 for (i = 0; i < page_size; i++) {
Stefan Reinauer99349a52006-03-16 16:46:19 +000057 if( (i & 0xfff) == 0xfff )
58 printf("0x%08lx", (unsigned long) dst -
59 (unsigned long) bios);
Ollie Lho761bf1b2004-03-20 16:46:10 +000060
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000061 *(bios + 0x555) = 0xAA;
62 *(bios + 0x2AA) = 0x55;
63 *(bios + 0x555) = 0xA0;
64 *dst++ = *src++;
65
66 /* wait for Toggle bit ready */
67 toggle_ready_jedec(bios);
68
Stefan Reinauer99349a52006-03-16 16:46:19 +000069 if( (i & 0xfff) == 0xfff )
70 printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000071 }
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000072
Ollie Lho761bf1b2004-03-20 16:46:10 +000073 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000074}
75
Ollie Lho761bf1b2004-03-20 16:46:10 +000076int probe_29f040b(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000077{
Ollie Lho184a4042005-11-26 21:55:36 +000078 volatile uint8_t *bios = flash->virt_addr;
79 uint8_t id1, id2;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000080
81 *(bios + 0x555) = 0xAA;
82 *(bios + 0x2AA) = 0x55;
83 *(bios + 0x555) = 0x90;
Ollie Lho761bf1b2004-03-20 16:46:10 +000084
85 id1 = *bios;
86 id2 = *(bios + 0x01);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000087
88 *bios = 0xF0;
89
Ronald G. Minnichef5779d2002-01-29 20:18:02 +000090 myusec_delay(10);
Ollie Lho761bf1b2004-03-20 16:46:10 +000091
Ollie Lho184a4042005-11-26 21:55:36 +000092 printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +000093 if (id1 == flash->manufacture_id && id2 == flash->model_id)
94 return 1;
95
96 return 0;
97}
98
Ollie Lho761bf1b2004-03-20 16:46:10 +000099int erase_29f040b(struct flashchip *flash)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000100{
Ollie Lho184a4042005-11-26 21:55:36 +0000101 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000102
103 *(bios + 0x555) = 0xAA;
104 *(bios + 0x2AA) = 0x55;
105 *(bios + 0x555) = 0x80;
106 *(bios + 0x555) = 0xAA;
107 *(bios + 0x2AA) = 0x55;
108 *(bios + 0x555) = 0x10;
109
Ronald G. Minnichef5779d2002-01-29 20:18:02 +0000110 myusec_delay(10);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000111 toggle_ready_jedec(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000112
Ollie Lho761bf1b2004-03-20 16:46:10 +0000113 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000114}
115
Ollie Lho184a4042005-11-26 21:55:36 +0000116int write_29f040b(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000117{
118 int i;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000119 int total_size = flash->total_size * 1024, page_size =
120 flash->page_size;
Ollie Lho184a4042005-11-26 21:55:36 +0000121 volatile uint8_t *bios = flash->virt_addr;
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000122
Stefan Reinauer99349a52006-03-16 16:46:19 +0000123 printf("Programming page ");
Ollie Lho761bf1b2004-03-20 16:46:10 +0000124 for (i = 0; i < total_size / page_size; i++) {
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000125 /* erase the page before programming */
126 erase_sector_29f040b(bios, i * page_size);
127
128 /* write to the sector */
Ollie Lho761bf1b2004-03-20 16:46:10 +0000129 printf("%04d at address: ", i);
130 write_sector_29f040b(bios, buf + i * page_size,
131 bios + i * page_size, page_size);
132 printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000133 }
134 printf("\n");
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000135
Ollie Lho761bf1b2004-03-20 16:46:10 +0000136 return (0);
Ronald G. Minnich5e5f75e2002-01-29 18:21:41 +0000137}