blob: b91c73a115517b624feaab9982ca1f414f3b78e1 [file] [log] [blame]
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +00001/*
2 * msys_doc.c: driver for programming m-systems doc devices
3 *
4 *
5 * Copyright 2003 Niki W. Waibel <niki.waibel@gmx.net>
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 */
21
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000022#include <stdio.h>
23#include <unistd.h>
24#include "flash.h"
25#include "msys_doc.h"
26
Ollie Lho184a4042005-11-26 21:55:36 +000027static int doc_wait(volatile uint8_t *bios, int timeout);
28static uint8_t doc_read_chipid(volatile uint8_t *bios);
29static uint8_t doc_read_docstatus(volatile uint8_t *bios);
30static uint8_t doc_read_cdsncontrol(volatile uint8_t *bios);
31static void doc_write_cdsncontrol(volatile uint8_t *bios, uint8_t data);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000032
Ollie Lho761bf1b2004-03-20 16:46:10 +000033int probe_md2802(struct flashchip *flash)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000034{
Stefan Reinauerce532972007-05-23 17:20:56 +000035 volatile uint8_t *bios = flash->virtual_memory;
Ollie Lho184a4042005-11-26 21:55:36 +000036 uint8_t chipid;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000037#ifndef MSYSTEMS_DOC_NO_55AA_CHECKING
Ollie Lho184a4042005-11-26 21:55:36 +000038 uint8_t id_0x55, id_0xAA;
Ollie Lho761bf1b2004-03-20 16:46:10 +000039#endif /* !MSYSTEMS_DOC_NO_55AA_CHECKING */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000040 int i, toggle_a, toggle_b;
41
Ollie Lho184a4042005-11-26 21:55:36 +000042 printf_debug("%s:\n", __FUNCTION__);
43 printf_debug("%s: *******************************\n", __FUNCTION__);
44 printf_debug("%s: * THIS IS A PRE ALPHA VERSION *\n", __FUNCTION__);
45 printf_debug("%s: * IN THE DEVELOPEMENT *********\n", __FUNCTION__);
46 printf_debug("%s: * PROCESS RIGHT NOW. **********\n", __FUNCTION__);
47 printf_debug("%s: *******************************\n", __FUNCTION__);
48 printf_debug("%s: * IF YOU ARE NOT A DEVELOPER **\n", __FUNCTION__);
49 printf_debug("%s: * THEN DO NOT TRY TO READ OR **\n", __FUNCTION__);
50 printf_debug("%s: * WRITE TO THIS DEVICE ********\n", __FUNCTION__);
51 printf_debug("%s: *******************************\n", __FUNCTION__);
52 printf_debug("%s:\n", __FUNCTION__);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000053
Ollie Lho184a4042005-11-26 21:55:36 +000054 printf_debug("%s: switching off reset mode ...\n", __FUNCTION__);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000055 doc_write(0x85, bios, DOCControl);
56 doc_write(0x85, bios, DOCControl);
57 doc_read_4nop(bios);
Ollie Lho761bf1b2004-03-20 16:46:10 +000058 if (doc_wait(bios, 5000))
59 return (-1);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000060 printf("%s: switching off reset mode ... done\n", __FUNCTION__);
61 printf("%s:\n", __FUNCTION__);
62
63 printf("%s: switching off write protection ...\n", __FUNCTION__);
Ollie Lho761bf1b2004-03-20 16:46:10 +000064 doc_write_cdsncontrol(bios, doc_read_cdsncontrol(bios) & (~0x08));
Uwe Hermanna7e05482007-05-09 10:17:44 +000065 printf("%s: switching off write protection ... done\n", __FUNCTION__);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000066 printf("%s:\n", __FUNCTION__);
67
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000068 chipid = doc_read_chipid(bios);
69#ifndef MSYSTEMS_DOC_NO_55AA_CHECKING
70 id_0x55 = doc_read(bios, IPL_0x0000);
71 id_0xAA = doc_read(bios, IPL_0x0001);
Ollie Lho761bf1b2004-03-20 16:46:10 +000072#endif /* !MSYSTEMS_DOC_NO_55AA_CHECKING */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000073 printf("%s: IPL_0x0000: 0x%02x\n", __FUNCTION__, id_0x55);
74 printf("%s: IPL_0x0001: 0x%02x\n", __FUNCTION__, id_0xAA);
75 printf("%s: IPL_0x0002: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000076 doc_read(bios, IPL_0x0002));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000077 printf("%s: IPL_0x0003: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000078 doc_read(bios, IPL_0x0003));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000079 printf("%s:\n", __FUNCTION__);
80 printf("%s: ChipID: 0x%02x\n", __FUNCTION__, chipid);
81 printf("%s: DOCStatus: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000082 doc_read_docstatus(bios));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000083 printf("%s: FloorSelect: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000084 doc_read(bios, FloorSelect));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000085 printf("%s: CDSNControl: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000086 doc_read_cdsncontrol(bios));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000087 printf("%s: CDSNDeviceSelect: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000088 doc_read(bios, CDSNDeviceSelect));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000089 printf("%s: ECCConfiguration: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000090 doc_read(bios, ECCConfiguration));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000091 printf("%s: CDSNSlowIO: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000092 doc_read(bios, CDSNSlowIO));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000093 printf("%s: ECCSyndrome0: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000094 doc_read(bios, ECCSyndrome0));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000095 printf("%s: ECCSyndrome1: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000096 doc_read(bios, ECCSyndrome1));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000097 printf("%s: ECCSyndrome2: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +000098 doc_read(bios, ECCSyndrome2));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000099 printf("%s: ECCSyndrome3: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000100 doc_read(bios, ECCSyndrome3));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000101 printf("%s: ECCSyndrome4: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000102 doc_read(bios, ECCSyndrome4));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000103 printf("%s: ECCSyndrome5: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000104 doc_read(bios, ECCSyndrome5));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000105 printf("%s: AliasResolution: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000106 doc_read(bios, AliasResolution));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000107 printf("%s: ConfigurationInput: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000108 doc_read(bios, ConfigurationInput));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000109 printf("%s: ReadPipelineInitialization: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000110 doc_read(bios, ReadPipelineInitialization));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000111 printf("%s: LastDataRead: 0x%02x\n", __FUNCTION__,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000112 doc_read(bios, LastDataRead));
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000113 printf("%s:\n", __FUNCTION__);
114
115 printf("%s: checking ECCConfiguration toggle bit\n", __FUNCTION__);
116 printf("%s:", __FUNCTION__);
117 toggle_a = toggle_b = 0;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000118 for (i = 0; i < 10; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +0000119 uint8_t toggle = doc_toggle(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000120
121 printf(" 0x%02x", toggle);
122
Ollie Lho761bf1b2004-03-20 16:46:10 +0000123 if (i % 2)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000124 toggle_a += toggle;
125 else
126 toggle_b += toggle;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000127 } /* for(i=0; i<10; i++) */
128 printf("\n%s: toggle result: %d/%d\n", __FUNCTION__, toggle_a,
129 toggle_b);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000130
Uwe Hermanna7e05482007-05-09 10:17:44 +0000131 if (chipid == flash->model_id && ((toggle_a == 5 && toggle_b == 0)
132 || (toggle_a == 0 && toggle_b == 5))
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000133#ifndef MSYSTEMS_DOC_NO_55AA_CHECKING
Ollie Lho761bf1b2004-03-20 16:46:10 +0000134 && id_0x55 == 0x55 && id_0xAA == 0xaa
135#endif /* !MSYSTEMS_DOC_NO_55AA_CHECKING */
136 ) {
137 return (1);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000138 }
139
Ollie Lho761bf1b2004-03-20 16:46:10 +0000140 return (0);
141} /* int probe_md2802(struct flashchip *flash) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000142
Ollie Lho184a4042005-11-26 21:55:36 +0000143int read_md2802(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000144{
Ollie Lho761bf1b2004-03-20 16:46:10 +0000145
146 return (0);
Ollie Lho184a4042005-11-26 21:55:36 +0000147} /* int read_md2802(struct flashchip *flash, uint8_t *buf) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000148
Ollie Lho761bf1b2004-03-20 16:46:10 +0000149int erase_md2802(struct flashchip *flash)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000150{
Stefan Reinauerce532972007-05-23 17:20:56 +0000151 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000152
Ollie Lho761bf1b2004-03-20 16:46:10 +0000153 return (1);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000154 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
155 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
156 *(volatile uint8_t *)(bios + 0x5555) = 0x80;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000157
Uwe Hermanna7e05482007-05-09 10:17:44 +0000158 *(volatile uint8_t *)(bios + 0x5555) = 0xAA;
159 *(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
160 *(volatile uint8_t *)(bios + 0x5555) = 0x10;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000161} /* int erase_md2802(struct flashchip *flash) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000162
Ollie Lho184a4042005-11-26 21:55:36 +0000163int write_md2802(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000164{
165 int i;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000166 int total_size = flash->total_size * 1024;
167 int page_size = flash->page_size;
Stefan Reinauerce532972007-05-23 17:20:56 +0000168 volatile uint8_t *bios = flash->virtual_memory;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000169
Ollie Lho761bf1b2004-03-20 16:46:10 +0000170 return (1);
171 erase_md2802(flash);
Ollie Lho184a4042005-11-26 21:55:36 +0000172 if (*bios != (uint8_t) 0xff) {
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000173 printf("ERASE FAILED\n");
174 return -1;
175 }
Ollie Lho761bf1b2004-03-20 16:46:10 +0000176 printf("Programming Page: ");
177 for (i = 0; i < total_size / page_size; i++) {
178 printf("%04d at address: 0x%08x", i, i * page_size);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000179 //write_page_md2802(bios, buf + i * page_size, bios + i * page_size, page_size);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000180 printf
181 ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000182 }
183 printf("\n");
184 //protect_md2802(bios);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000185
Ronald G. Minniche66bdb32004-04-13 19:10:48 +0000186 return 0;
Ollie Lho184a4042005-11-26 21:55:36 +0000187} /* int write_md2802(struct flashchip *flash, uint8_t *buf) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000188
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000189/*
190 wait timeout msec for doc to become ready
191 return:
192 0: ready
193 -1: timeout expired
194*/
Ollie Lho184a4042005-11-26 21:55:36 +0000195static int doc_wait(volatile uint8_t *bios, int timeout)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000196{
197 int i = 20;
198
199 doc_read_4nop(bios);
200
Ollie Lho761bf1b2004-03-20 16:46:10 +0000201 while (_doc_busy(bios) && (i != 0)) {
202 usleep(timeout * 1000 / 20);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000203 i--;
204 }
205
206 doc_read_2nop(bios);
207
Ollie Lho761bf1b2004-03-20 16:46:10 +0000208 if (_doc_busy(bios)) {
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000209 doc_read_2nop(bios);
Ollie Lho761bf1b2004-03-20 16:46:10 +0000210 return (-1);
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000211 }
212
Ollie Lho761bf1b2004-03-20 16:46:10 +0000213 return (0);
Ollie Lho184a4042005-11-26 21:55:36 +0000214} /* static int doc_wait(volatile uint8_t *bios, int timeout) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000215
Ollie Lho184a4042005-11-26 21:55:36 +0000216static uint8_t doc_read_docstatus(volatile uint8_t *bios)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000217{
218 doc_read(bios, CDSNSlowIO);
219 doc_read_2nop(bios);
220
Ollie Lho761bf1b2004-03-20 16:46:10 +0000221 return (doc_read(bios, _DOCStatus));
Ollie Lho184a4042005-11-26 21:55:36 +0000222} /* static uint8_t doc_read_docstatus(volatile uint8_t *bios) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000223
Ollie Lho184a4042005-11-26 21:55:36 +0000224static uint8_t doc_read_chipid(volatile uint8_t *bios)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000225{
226 doc_read(bios, CDSNSlowIO);
227 doc_read_2nop(bios);
228
Ollie Lho761bf1b2004-03-20 16:46:10 +0000229 return (doc_read(bios, _ChipID));
Ollie Lho184a4042005-11-26 21:55:36 +0000230} /* static uint8_t doc_read_chipid(volatile uint8_t *bios) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000231
Ollie Lho184a4042005-11-26 21:55:36 +0000232static uint8_t doc_read_cdsncontrol(volatile uint8_t *bios)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000233{
Ollie Lho184a4042005-11-26 21:55:36 +0000234 uint8_t value;
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000235
236 /* the delays might be necessary when reading the busy bit,
Ollie Lho761bf1b2004-03-20 16:46:10 +0000237 but because a read to this reg reads the busy bit
238 anyway we better do this delays... */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000239 doc_read_4nop(bios);
240 value = doc_read(bios, _CDSNControl);
241 doc_read_2nop(bios);
242
Ollie Lho761bf1b2004-03-20 16:46:10 +0000243 return (value);
Ollie Lho184a4042005-11-26 21:55:36 +0000244} /* static uint8_t doc_read_chipid(volatile char *bios) */
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000245
Ollie Lho184a4042005-11-26 21:55:36 +0000246static void doc_write_cdsncontrol(volatile uint8_t *bios, uint8_t data)
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +0000247{
248 doc_write(data, bios, _CDSNControl);
249 doc_read_4nop(bios);
Ollie Lho184a4042005-11-26 21:55:36 +0000250} /* static void doc_write_chipid(volatile char *bios, uint8_t data) */