summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/machine/at29040a.c
blob: 31be262b7c1fc2aa5fa433adbb64aa86ac59b596 (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// license:???
// copyright-holders:Raphael Nabet, Michael Zapf
/*
    Atmel at29c040a flash EEPROM

    512k*8 FEEPROM, organized in pages of 256 bytes.

    References:
    Datasheets were found on Atmel's site (www.atmel.com)

    Raphael Nabet 2003

    September 2010: Rewritten as device
    February 2012: Rewritten as class
*/

#include "at29040a.h"

#define VERBOSE 2
#define LOG logerror

#define FEEPROM_SIZE        0x80000
#define SECTOR_SIZE         0x00100
#define BOOT_BLOCK_SIZE     0x04000

#define ADDRESS_MASK        0x7ffff
#define SECTOR_ADDRESS_MASK 0x7ff00
#define BYTE_ADDRESS_MASK   0x000ff

#define PRG_TIMER 1

#define VERSION 0

/*
    Constructor.
*/
at29040a_device::at29040a_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
: device_t(mconfig, AT29040A, "ATMEL 29040A 512K*8 FEEPROM", tag, owner, clock, "at29040a", __FILE__),
	device_nvram_interface(mconfig, *this)
{
}

//-------------------------------------------------
//  nvram_default - called to initialize NVRAM to
//  its default state
//-------------------------------------------------

void at29040a_device::nvram_default()
{
	memset(m_eememory, 0, FEEPROM_SIZE+2);
}

//-------------------------------------------------
//  nvram_read - called to read NVRAM from the
//  .nv file
//-------------------------------------------------

void at29040a_device::nvram_read(emu_file &file)
{
	file.read(m_eememory, FEEPROM_SIZE+2);
}

//-------------------------------------------------
//  nvram_write - called to write NVRAM to the
//  .nv file
//-------------------------------------------------

void at29040a_device::nvram_write(emu_file &file)
{
	m_eememory[0] = VERSION;
	file.write(m_eememory, FEEPROM_SIZE+2);
}

/*
    programming timer callback
*/
void at29040a_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	switch (m_pgm)
	{
	case PGM_1:
		/* programming cycle timeout */
		if (VERBOSE>7) LOG("at29040a: Programming cycle timeout\n");
		m_pgm = PGM_0;
		break;

	case PGM_2:
		/* programming cycle start */
		if (VERBOSE>7) LOG("at29040a: Sector write start\n");
		m_pgm = PGM_3;
		/* max delay 10ms, typical delay 5 to 7 ms */
		m_programming_timer->adjust(attotime::from_msec(5));
		break;

	case PGM_3:
		/* programming cycle end */
		memcpy(m_eememory + 2 + (m_programming_last_offset & ~0xff), m_programming_buffer, SECTOR_SIZE);
		if (VERBOSE>7) LOG("at29040a: Sector write completed at location %04x + 2\n", (m_programming_last_offset & ~0xff));
		if (m_enabling_sdb)
		{
			m_sdp = true;
		}
		if (m_disabling_sdb)
		{
			m_sdp = false;
		}
		if (VERBOSE>7) LOG("at29040a: Software data protection = %d\n", m_sdp);

		m_pgm = PGM_0;
		m_enabling_sdb = false;
		m_disabling_sdb = false;

		break;

	default:
		if (VERBOSE>1) LOG("internal error in %s %d\n", __FILE__, __LINE__);
		break;
	}
}

void at29040a_device::sync_flags()
{
	if (m_lower_bbl) m_eememory[1] |= 0x04;
	else m_eememory[1] &= ~0x04;

	if (m_higher_bbl) m_eememory[1] |= 0x02;
	else m_eememory[1] &= ~0x02;

	if (m_sdp) m_eememory[1] |= 0x01;
	else m_eememory[1] &= ~0x01;
}

/*
    read a byte from FEEPROM
*/
READ8_MEMBER( at29040a_device::read )
{
	int reply;

	offset &= ADDRESS_MASK;

	/* reading in the midst of any command sequence cancels it (right???) */
	m_cmd = CMD_0;
	m_long_sequence = false;
	// m_higher_bbl = true;         // who says that?

	sync_flags();

	/* reading before the start of a programming cycle cancels it (right???) */
	if (m_pgm == PGM_1)
	{
		// attempt to access a locked out boot block: cancel programming
		// command if necessary
		m_pgm = PGM_0;
		m_enabling_sdb = false;
		m_disabling_sdb = false;
		m_programming_timer->adjust(attotime::never);
	}

	if (m_id_mode)
	{
		switch (offset)
		{
		case 0x00000:
			reply = 0x1f;       // Manufacturer code
			break;

		case 0x00001:
			reply = 0xa4;       // Device code
			break;

		case 0x00002:
			reply = m_lower_bbl? 0xff : 0xfe;
			break;

		case 0x7fff2:
			reply = m_higher_bbl? 0xff : 0xfe;
			break;

		default:
			reply = 0;
			break;
		}
	}
	else if ((m_pgm == PGM_2) || (m_pgm == PGM_3))
	{
		if (m_pgm == PGM_2)
		{   // DATA* polling starts the programming cycle (right???)
			m_pgm = PGM_3;
			/* max delay 10ms, typical delay 5 to 7 ms */
			m_programming_timer->adjust(attotime::from_msec(5));
		}

		reply = m_toggle_bit? 0x02 : 0x00;
		m_toggle_bit = !m_toggle_bit;

		if ((offset == m_programming_last_offset) && (! (m_programming_buffer[m_programming_last_offset & 0xff] & 0x01)))
			reply |= 0x01;
	}
	else
		reply = m_eememory[offset+2];

	if (VERBOSE>7) LOG("at29040a: %05x -> %02x\n", offset, reply);

	return reply;
}

/*
    Write a byte to FEEPROM
*/
WRITE8_MEMBER( at29040a_device::write )
{
	offset &= ADDRESS_MASK;
	if (VERBOSE>7) LOG("at29040a: %05x <- %02x\n", offset, data);

	/* The special CFI commands assume a smaller address space according */
	/* to the specification ("address format A14-A0") */
	offs_t cfi_offset = offset & 0x7fff;

	if (m_enabling_bbl)
	{
		if (VERBOSE>7) LOG("at29040a: Enabling boot block lockout\n");
		m_enabling_bbl = false;

		if ((offset == 0x00000) && (data == 0x00))
		{
			if (VERBOSE>7) LOG("at29040a: Enabling lower boot block lockout\n");
			m_lower_bbl = true;
			sync_flags();
			return;
		}
		else
		{
			if ((offset == 0x7ffff) && (data == 0xff))
			{
				if (VERBOSE>7) LOG("at29040a: Enabling higher boot block lockout\n");
				m_higher_bbl = true;
				sync_flags();
				return;
			}
			else
			{
				if (VERBOSE>1) LOG("at29040a: Invalid boot block specification: %05x/%02x\n", offset, data);
			}
		}
	}

	switch (m_cmd)
	{
	case CMD_0:
		if ((cfi_offset == 0x5555) && (data == 0xaa))
		{
			if (VERBOSE>7) LOG("at29040a: Command sequence started\n");
			m_cmd = CMD_1;
		}
		else
		{
			m_cmd = CMD_0;
			m_long_sequence = false;
		}
		break;

	case CMD_1:
		if ((cfi_offset == 0x2aaa) && (data == 0x55))
		{
			m_cmd = CMD_2;
		}
		else
		{
			m_cmd = CMD_0;
			m_long_sequence = false;
			if (VERBOSE>7) LOG("at29040a: Command sequence aborted\n");
		}
		break;

	case CMD_2:
		if (cfi_offset == 0x5555)
		{
			if (!m_long_sequence)
				if (VERBOSE>7) LOG("at29040a: Command sequence completed\n");

			m_pgm = PGM_0;
			m_enabling_sdb = false;
			m_disabling_sdb = false;
			m_programming_timer->adjust(attotime::never);

			/* process command */
			switch (data)
			{
			case 0x10:
				/*  Software chip erase */
				if (m_long_sequence)
				{
					if (m_lower_bbl || m_higher_bbl)
					{
						if (VERBOSE>1) LOG("at29040a: Chip erase sequence deactivated due to previous boot block lockout.\n");
					}
					else
					{
						if (VERBOSE>7) LOG("at29040a: Erase chip\n");
						memset(m_eememory+2, 0xff, FEEPROM_SIZE);
					}
				}
				break;

			case 0x20:
				/* Software data protection disable */
				if (VERBOSE>7) LOG("at29040a: Software data protection disable\n");
				// The complete sequence is aa-55-80-aa-55-20
				// so we need a 80 before, else the sequence is invalid
				if (m_long_sequence)
				{
					m_pgm = PGM_1;
					m_disabling_sdb = true;
					/* set command timeout (right???) */
					//m_programming_timer->adjust(attotime::from_usec(150), id, 0.);
				}
				break;

			case 0x40:
				/* Boot block lockout enable */
				// Complete sequence is aa-55-80-aa-55-40
				if (VERBOSE>7) LOG("at29040a: Boot block lockout enable\n");
				if (m_long_sequence) m_enabling_bbl = true;
				break;

			case 0x80:
				m_long_sequence = true;
				break;

			case 0x90:
				/* Software product identification entry */
				if (VERBOSE>7) LOG("at29040a: Identification mode (start)\n");
				m_id_mode = true;
				break;

			case 0xa0:
				/* Software data protection enable */
				if (VERBOSE>7) LOG("at29040a: Software data protection enable\n");
				m_pgm = PGM_1;
				m_enabling_sdb = true;
				/* set command timeout (right???) */
				//m_programming_timer->adjust(attotime::from_usec(150), id, 0.);
				break;

			case 0xf0:
				/* Software product identification exit */
				if (VERBOSE>7) LOG("at29040a: Identification mode (end)\n");
				m_id_mode = false;
				break;
			}
			m_cmd = CMD_0;
			if (data != 0x80) m_long_sequence = false;

			/* return, because we don't want to write the EEPROM with the command byte */
			return;
		}
		else
		{
			m_cmd = CMD_0;
			m_long_sequence = false;
		}
	}
	if ((m_pgm == PGM_2)
			&& ((offset & ~0xff) != (m_programming_last_offset & ~0xff)))
	{
		/* cancel current programming cycle */
		if (VERBOSE>7) LOG("at29040a: invalid sector change (from %05x to %05x); cancel programming cycle\n",(offset & ~0xff), (m_programming_last_offset & ~0xff));
		m_pgm = PGM_0;
		m_enabling_sdb = false;
		m_disabling_sdb = false;
		m_programming_timer->adjust(attotime::never);
	}

	if (((m_pgm == PGM_0) && !m_sdp)  // write directly
		|| (m_pgm == PGM_1))          // write after unlocking
	{
		if (((offset < BOOT_BLOCK_SIZE) && m_lower_bbl)
			|| ((offset >= FEEPROM_SIZE-BOOT_BLOCK_SIZE) && m_higher_bbl))
		{
			// attempt to access a locked out boot block: cancel programming
			// command if necessary
			if (VERBOSE>7) LOG("at29040a: attempt to access a locked out boot block: offset = %05x, lowblock=%d, highblock=%d\n", offset, m_lower_bbl, m_higher_bbl);

			m_pgm = PGM_0;
			m_enabling_sdb = false;
			m_disabling_sdb = false;
		}
		else
		{   /* enter programming mode */
			if (VERBOSE>7) LOG("at29040a: enter programming mode (m_pgm=%d)\n", m_pgm);
			memset(m_programming_buffer, 0xff, SECTOR_SIZE);
			m_pgm = PGM_2;
		}
	}
	if (m_pgm == PGM_2)
	{
		/* write data to programming buffer */
		if (VERBOSE>7) LOG("at29040a: Write data to programming buffer\n");
		m_programming_buffer[offset & 0xff] = data;
		m_programming_last_offset = offset;
		m_programming_timer->adjust(attotime::from_usec(150));  // next byte must be written before the timer expires
	}
}

void at29040a_device::device_start(void)
{
	m_programming_buffer = global_alloc_array(UINT8, SECTOR_SIZE);
	m_programming_timer = timer_alloc(PRG_TIMER);

	m_eememory = global_alloc_array(UINT8, FEEPROM_SIZE+2);
}

void at29040a_device::device_stop(void)
{
	global_free_array(m_programming_buffer);
	global_free_array(m_eememory);
}

void at29040a_device::device_reset(void)
{
	if (m_eememory[0] != VERSION)
	{
		if (VERBOSE>1) LOG("AT29040A: Warning: Version mismatch; expected %d but found %d for %s. Resetting.\n", VERSION, m_eememory[0], tag());
		m_eememory[0] = 0;
		m_eememory[1] = 0;
	}

	m_lower_bbl =   ((m_eememory[1] & 0x04)!=0);
	m_higher_bbl =  ((m_eememory[1] & 0x02)!=0);
	m_sdp =         ((m_eememory[1] & 0x01)!=0);

	if (VERBOSE>7) LOG("at29040a (%s): LowerBBL = %d, HigherBBL = %d, SoftDataProt = %d\n", tag(), m_lower_bbl, m_higher_bbl, m_sdp);

	m_id_mode = false;
	m_cmd = CMD_0;
	m_enabling_bbl = false;
	m_long_sequence = false;
	m_pgm = PGM_0;
	m_enabling_sdb = false;
	m_disabling_sdb = false;
	m_toggle_bit = false;
	m_programming_last_offset = 0;
}

const device_type AT29040A = &device_creator<at29040a_device>;