Debian Codegraph @

common/dev_pcmcia_disk.c

raw
1 /*
2 * Cisco router simulation platform.
3 * Copyright (c) 2006 Christophe Fillot. All rights reserved.
4 *
5 * PCMCIA ATA Flash emulation.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <time.h>
13 #include <errno.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <fcntl.h>
17
18 #include "cpu.h"
19 #include "vm.h"
20 #include "dynamips.h"
21 #include "memory.h"
22 #include "device.h"
23 #include "fs_mbr.h"
24 #include "fs_fat.h"
25
26 #define DEBUG_ACCESS 0
27 #define DEBUG_ATA 0
28 #define DEBUG_READ 0
29 #define DEBUG_WRITE 0
30
31 /* Default disk parameters: 4 heads, 32 sectors per track */
32 #define DISK_NR_HEADS 4
33 #define DISK_SECTS_PER_TRACK 32
34
35 /* Size (in bytes) of a sector */
36 #define SECTOR_SIZE 512
37
38 /* ATA commands */
39 #define ATA_CMD_NOP 0x00
40 #define ATA_CMD_READ_SECTOR 0x20
41 #define ATA_CMD_WRITE_SECTOR 0x30
42 #define ATA_CMD_IDENT_DEVICE 0xEC
43
44 /* ATA status */
45 #define ATA_STATUS_BUSY 0x80 /* Controller busy */
46 #define ATA_STATUS_RDY 0x40 /* Device ready */
47 #define ATA_STATUS_DWF 0x20 /* Write fault */
48 #define ATA_STATUS_DSC 0x10 /* Device ready */
49 #define ATA_STATUS_DRQ 0x08 /* Data Request */
50 #define ATA_STATUS_CORR 0x04 /* Correctable error */
51 #define ATA_STATUS_IDX 0x02 /* Always 0 */
52 #define ATA_STATUS_ERR 0x01 /* Error */
53
54 /* ATA Drive/Head register */
55 #define ATA_DH_LBA 0x40 /* LBA Mode */
56
57 /* Card Information Structure */
58 static m_uint8_t cis_table[] = {
59 /* CISTPL_DEVICE */
60 0x01, 0x03,
61 0xd9, /* DSPEED_250NS | WPS | DTYPE_FUNCSPEC */
62 0x01, /* 2 KBytes(Units)/64 KBytes(Max Size) */
63 0xff, /* 0xFF */
64 /* CISTPL_DEVICE_OC */
65 0x1c, 0x04,
66 0x03, /* MWAIT | 3.3 volt VCC operation */
67 0xd9, /* DSPEED_250NS | WPS | DTYPE_FUNCSPEC */
68 0x01, /* 2 KBytes(Units)/64 KBytes(Max Size) */
69 0xff, /* 0xFF */
70 /* CISTPL_JEDEC_C */
71 0x18, 0x02,
72 0xdf, /* PCMCIA */
73 0x01, /* 0x01 */
74 /* CISTPL_MANFID */
75 0x20, 0x04,
76 0x34, 0x12, /* 0x1234 ??? */
77 0x00, 0x02, /* 0x0200 */
78 /* CISTPL_VERS_1 */
79 0x15, 0x2b,
80 0x04, 0x01, /* PCMCIA 2.0/2.1 / JEIDA 4.1/4.2 */
81 0x44, 0x79, 0x6e, 0x61, 0x6d, 0x69, 0x70, 0x73,
82 0x20, 0x41, 0x54, 0x41, 0x20, 0x46, 0x6c, 0x61,
83 0x73, 0x68, 0x20, 0x43, 0x61, 0x72, 0x64, 0x20,
84 0x20, 0x00, /* "Dynamips ATA Flash Card " */
85 0x44, 0x59, 0x4e, 0x41, 0x30, 0x20, 0x20, 0x00, /* "DYNA0 " */
86 0x44, 0x59, 0x4e, 0x41, 0x30, 0x00, /* "DYNA0" */
87 0xff, /* 0xFF */
88 /* CISTPL_FUNCID */
89 0x21, 0x02,
90 0x04, /* Fixed Disk */
91 0x01, /* Power-On Self Test */
92 /* CISTPL_FUNCE */
93 0x22, 0x02,
94 0x01, /* Disk Device Interface tuple */
95 0x01, /* PC Card-ATA Interface */
96 /* CISTPL_FUNCE: */
97 0x22, 0x03,
98 0x02, /* Basic PC Card ATA Interface tuple */
99 0x04, /* S(Silicon Device) */
100 0x5f, /* P0(Sleep) | P1(Standy) | P2(Idle) | P3(Auto) | N(3F7/377 Register Inhibit Available) | I(IOIS16# on Twin Card) */
101 /* CISTPL_CONFIG */
102 0x1a, 0x05,
103 0x01, /* TPCC_RASZ=2, TPCC_RMSZ=1, TPCC_RFSZ=0 */
104 0x03, /* TPCC_LAST=3 */
105 0x00, 0x02, /* TPCC_RADR(Configuration Register Base Address)=0x0200 */
106 0x0f, /* TPCC_RMSK(Configuration Register Presence Mask Field)=0x0F */
107 /* CISTPL_CFTABLE_ENTRY */
108 0x1b, 0x0b,
109 0xc0, /* Index=0 | Default | Interface */
110 0x40, /* Memory | READY Active */
111 0xa1, /* VCC power-description-structure only | Single 2-byte length specified | Misc */
112 0x27, /* Nom V | Min V | Max V | Peak I */
113 0x55, /* Nom V=5V */
114 0x4d, /* Min V=4.5V */
115 0x5d, /* Max V=5V */
116 0x75, /* Peak I=80mA */
117 0x08, 0x00, /* Card Address=0 | Host Address=0x0008 * 256 bytes */
118 0x21, /* Max Twin Cards=1 | Power Down */
119 /* CISTPL_CFTABLE_ENTRY */
120 0x1b, 0x06,
121 0x00, /* Index=0 */
122 0x01, /* VCC power-description-structure only */
123 0x21, /* Nom V | Peak I */
124 0xb5, 0x1e, /* Nom V=3.30V */
125 0x4d, /* Peak I=45mA */
126 /* CISTPL_CFTABLE_ENTRY */
127 0x1b, 0x0d,
128 0xc1, /* Index=1 | Default | Interface */
129 0x41, /* I/O and Memory | READY Active */
130 0x99, /* VCC power-description-structure only | IO Space | IRQ | Misc */
131 0x27, /* Nom V | Min V | Max V | Peak I */
132 0x55, /* Nom V=5V */
133 0x4d, /* Min V=4.5V */
134 0x5d, /* Max V=5V */
135 0x75, /* Peak I=80mA */
136 0x64, /* IOAddrLines=4 | All registers are accessible by both 8-bit or 16-bit accesses */
137 0xf0, 0xff, 0xff, /* Mask | Level | Pulse | Share | IRQ0..IRQ15 */
138 0x21, /* Max Twin Cards=1 | Power Down */
139 /* CISTPL_CFTABLE_ENTRY */
140 0x1b, 0x06,
141 0x01, /* Index=1 */
142 0x01, /* VCC power-description-structure only */
143 0x21, /* Nom V | Peak I */
144 0xb5, 0x1e, /* Nom V=3.30V */
145 0x4d, /* Peak I=45mA */
146 /* CISTPL_CFTABLE_ENTRY */
147 0x1b, 0x12,
148 0xc2, /* Index=2 | Default | Interface */
149 0x41, /* I/O and Memory | READY Active */
150 0x99, /* VCC power-description-structure only | IO Space | IRQ | Misc */
151 0x27, /* Nom V | Min V | Max V | Peak I */
152 0x55, /* Nom V=5V */
153 0x4d, /* Min V=4.5V */
154 0x5d, /* Max V=5V */
155 0x75, /* Peak I=80mA */
156 0xea, /* IOAddrLines=10 | All registers are accessible by both 8-bit or 16-bit accesses | Range */
157 0x61, /* Number of I/O Address Ranges=2 | Size of Address=2 | Size of Length=1 */
158 0xf0, 0x01, 0x07, /* Address=0x1F0, Length=8 */
159 0xf6, 0x03, 0x01, /* Address=0x3F6, Length=2 */
160 0xee, /* IRQ14 | Level | Pulse | Share */
161 0x21, /* Max Twin Cards=1 | Power Down */
162 /* CISTPL_CFTABLE_ENTRY */
163 0x1b, 0x06,
164 0x02, /* Index=2 */
165 0x01, /* VCC power-description-structure only */
166 0x21, /* Nom V | Peak I */
167 0xb5, 0x1e, /* Nom V=3.30V */
168 0x4d, /* Peak I=45mA */
169 /* CISTPL_CFTABLE_ENTRY */
170 0x1b, 0x12,
171 0xc3, /* Index=3 | Default | Interface */
172 0x41, /* I/O and Memory | READY Active */
173 0x99, /* VCC power-description-structure only | IO Space | IRQ | Misc */
174 0x27, /* Nom V | Min V | Max V | Peak I */
175 0x55, /* Nom V=5V */
176 0x4d, /* Min V=4.5V */
177 0x5d, /* Max V=5V */
178 0x75, /* Peak I=80mA */
179 0xea, /* IOAddrLines=10 | All registers are accessible by both 8-bit or 16-bit accesses | Range */
180 0x61, /* Number of I/O Address Ranges=2 | Size of Address=2 | Size of Length=1 */
181 0x70, 0x01, 0x07, /* Address=0x170, Length=8 */
182 0x76, 0x03, 0x01, /* Address=0x376, Length=2 */
183 0xee, /* IRQ14 | Level | Pulse | Share */
184 0x21, /* Max Twin Cards=1 | Power Down */
185 /* CISTPL_CFTABLE_ENTRY */
186 0x1b, 0x06,
187 0x03, /* Index=3 */
188 0x01, /* VCC power-description-structure only */
189 0x21, /* Nom V | Peak I */
190 0xb5, 0x1e, /* Nom V=3.30V */
191 0x4d, /* Peak I=45mA */
192 /* CISTPL_NO_LINK */
193 0x14, 0x00,
194 /* CISTPL_END */
195 0xff,
196 };
197
198 /* PCMCIA private data */
199 struct pcmcia_disk_data {
200 vm_instance_t *vm;
201 vm_obj_t vm_obj;
202 struct vdevice dev;
203 char *filename;
204 int fd;
205
206 /* Disk parameters (C/H/S) */
207 u_int nr_heads;
208 u_int nr_cylinders;
209 u_int sects_per_track;
210
211 /* Current ATA command and CHS info */
212 m_uint8_t ata_cmd,ata_cmd_in_progress;
213 m_uint8_t ata_status;
214
215 m_uint8_t cyl_low,cyl_high;
216 m_uint8_t head,sect_no;
217 m_uint8_t sect_count;
218
219 /* Current sector */
220 m_uint32_t sect_pos;
221
222 /* Remaining sectors to read or write */
223 u_int sect_remaining;
224
225 /* Callback function when data buffer is validated */
226 void (*ata_cmd_callback)(struct pcmcia_disk_data *);
227
228 /* Data buffer */
229 m_uint32_t data_offset;
230 u_int data_pos;
231 m_uint8_t data_buffer[SECTOR_SIZE];
232 };
233
234 /* Convert a CHS reference to an LBA reference */
235 static inline m_uint32_t chs_to_lba(struct pcmcia_disk_data *d,
236 u_int cyl,u_int head,u_int sect)
237 {
238 return((((cyl * d->nr_heads) + head) * d->sects_per_track) + sect - 1);
239 }
240
241 /* Convert a LBA reference to a CHS reference */
242 static inline void lba_to_chs(struct pcmcia_disk_data *d,m_uint32_t lba,
243 u_int *cyl,u_int *head,u_int *sect)
244 {
245 *cyl = lba / (d->sects_per_track * d->nr_heads);
246 *head = (lba / d->sects_per_track) % d->nr_heads;
247 *sect = (lba % d->sects_per_track) + 1;
248 }
249
250 /* Format disk with a single FAT16 partition */
251 static int disk_format(struct pcmcia_disk_data *d)
252 {
253 struct mbr_data mbr;
254 struct mbr_partition *part;
255 u_int cyl=0, head=1, sect=1;
256
257 /* Master Boot Record */
258 memset(&mbr,0,sizeof(mbr));
259 mbr.signature[0] = MBR_SIGNATURE_0;
260 mbr.signature[1] = MBR_SIGNATURE_1;
261 part = &mbr.partition[0];
262 part->bootable = 0;
263 part->type = MBR_PARTITION_TYPE_FAT16;
264 part->lba = chs_to_lba(d, 0, 1, 1);
265 part->nr_sectors = d->nr_heads * d->nr_cylinders * d->sects_per_track - part->lba;
266 lba_to_chs(d, part->lba + part->nr_sectors - 1, &cyl, &head, &sect);
267 mbr_set_chs(part->first_chs, 0, 1, 1);
268 mbr_set_chs(part->last_chs, cyl, head, sect);
269
270 if (mbr_write_fd(d->fd, &mbr)<0) {
271 return(-1);
272 }
273
274 /* FAT16 partition */
275 if (fs_fat_format16(d->fd, part->lba, part->nr_sectors, d->sects_per_track,
276 d->nr_heads, d->vm_obj.name)) {
277 return(-1);
278 }
279
280 return(0);
281 }
282
283 /* Create the virtual disk */
284 static int disk_create(struct pcmcia_disk_data *d)
285 {
286 off_t disk_len;
287
288 if ((d->fd = open(d->filename,O_CREAT|O_EXCL|O_RDWR,0600)) < 0) {
289 /* already exists? */
290 if ((d->fd = open(d->filename,O_CREAT|O_RDWR,0600)) < 0) {
291 perror("disk_create: open");
292 return(-1);
293 }
294 }
295 else {
296 /* new disk */
297 if (disk_format(d)) {
298 return(-1);
299 }
300 }
301
302 disk_len = (off_t) d->nr_heads * d->nr_cylinders * d->sects_per_track * SECTOR_SIZE;
303 ftruncate(d->fd,disk_len);
304 return(0);
305 }
306
307 /* Read a sector from disk file */
308 static int disk_read_sector(struct pcmcia_disk_data *d,m_uint32_t sect,
309 m_uint8_t *buffer)
310 {
311 off_t disk_offset = (off_t)sect * SECTOR_SIZE;
312
313 #if DEBUG_READ
314 vm_log(d->vm,d->dev.name,"reading sector 0x%8.8x\n",sect);
315 #endif
316
317 if (lseek(d->fd,disk_offset,SEEK_SET) == -1) {
318 perror("read_sector: lseek");
319 return(-1);
320 }
321
322 if (read(d->fd,buffer,SECTOR_SIZE) != SECTOR_SIZE) {
323 perror("read_sector: read");
324 return(-1);
325 }
326
327 return(0);
328 }
329
330 /* Write a sector to disk file */
331 static int disk_write_sector(struct pcmcia_disk_data *d,m_uint32_t sect,
332 m_uint8_t *buffer)
333 {
334 off_t disk_offset = (off_t)sect * SECTOR_SIZE;
335
336 #if DEBUG_WRITE
337 vm_log(d->vm,d->dev.name,"writing sector 0x%8.8x\n",sect);
338 #endif
339
340 if (lseek(d->fd,disk_offset,SEEK_SET) == -1) {
341 perror("write_sector: lseek");
342 return(-1);
343 }
344
345 if (write(d->fd,buffer,SECTOR_SIZE) != SECTOR_SIZE) {
346 perror("write_sector: write");
347 return(-1);
348 }
349
350 return(0);
351 }
352
353 /* Identify PCMCIA device (ATA command 0xEC) */
354 static void ata_identify_device(struct pcmcia_disk_data *d)
355 {
356 m_uint8_t *p = d->data_buffer;
357 m_uint32_t sect_count;
358
359 sect_count = d->nr_heads * d->nr_cylinders * d->sects_per_track;
360
361 /* Clear all fields (for safety) */
362 memset(p,0x00,SECTOR_SIZE);
363
364 /* Word 0: General Configuration */
365 p[0] = 0x8a; /* Not MFM encoded | Hard sectored | Removable cartridge drive */
366 p[1] = 0x84; /* Disk transfer rate !<= 10Mbs | Non-rotating disk drive */
367
368 /* Word 1: Default number of cylinders */
369 p[2] = d->nr_cylinders & 0xFF;
370 p[3] = (d->nr_cylinders >> 8) & 0xFF;
371
372 /* Word 3: Default number of heads */
373 p[6] = d->nr_heads;
374
375 /* Word 6: Default number of sectors per track */
376 p[12] = d->sects_per_track;
377
378 /* Word 7: Number of sectors per card (MSW) */
379 p[14] = (sect_count >> 16) & 0xFF;
380 p[15] = (sect_count >> 24);
381
382 /* Word 8: Number of sectors per card (LSW) */
383 p[16] = sect_count & 0xFF;
384 p[17] = (sect_count >> 8) & 0xFF;
385
386 /* Word 22: ECC count */
387 p[44] = 0x04;
388
389 /* Word 53: Translation parameters valid */
390 p[106] = 0x3;
391
392 /* Word 54: Current number of cylinders */
393 p[108] = d->nr_cylinders & 0xFF;
394 p[109] = (d->nr_cylinders >> 8) & 0xFF;
395
396 /* Word 55: Current number of heads */
397 p[110] = d->nr_heads;
398
399 /* Word 56: Current number of sectors per track */
400 p[112] = d->sects_per_track;
401
402 /* Word 57/58: Current of sectors per card (LSW/MSW) */
403 p[114] = sect_count & 0xFF;
404 p[115] = (sect_count >> 8) & 0xFF;
405
406 p[116] = (sect_count >> 16) & 0xFF;
407 p[117] = (sect_count >> 24);
408
409 #if 0
410 /* Word 60/61: Total sectors addressable in LBA mode (MSW/LSW) */
411 p[120] = (sect_count >> 16) & 0xFF;
412 p[121] = (sect_count >> 24);
413 p[122] = sect_count & 0xFF;
414 p[123] = (sect_count >> 8) & 0xFF;
415 #endif
416 }
417
418 /* Set sector position */
419 static void ata_set_sect_pos(struct pcmcia_disk_data *d)
420 {
421 u_int cyl;
422
423 if (d->head & ATA_DH_LBA) {
424 d->sect_pos = (u_int)(d->head & 0x0F) << 24;
425 d->sect_pos |= (u_int)d->cyl_high << 16;
426 d->sect_pos |= (u_int)d->cyl_low << 8;
427 d->sect_pos |= (u_int)d->sect_no;
428
429 #if DEBUG_ATA
430 vm_log(d->vm,d->dev.name,"ata_set_sect_pos: LBA sect=0x%x\n",
431 d->sect_pos);
432 #endif
433 } else {
434 cyl = (((u_int)d->cyl_high) << 8) + d->cyl_low;
435 d->sect_pos = chs_to_lba(d,cyl,d->head & 0x0F,d->sect_no);
436
437 #if DEBUG_ATA
438 vm_log(d->vm,d->dev.name,
439 "ata_set_sect_pos: cyl=0x%x,head=0x%x,sect=0x%x => "
440 "sect_pos=0x%x\n",
441 cyl,d->head & 0x0F,d->sect_no,d->sect_pos);
442 #endif
443 }
444 }
445
446 /* ATA device identifier callback */
447 static void ata_cmd_ident_device_callback(struct pcmcia_disk_data *d)
448 {
449 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC;
450 }
451
452 /* ATA read sector callback */
453 static void ata_cmd_read_callback(struct pcmcia_disk_data *d)
454 {
455 d->sect_remaining--;
456
457 if (!d->sect_remaining) {
458 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC;
459 return;
460 }
461
462 /* Read the next sector */
463 d->sect_pos++;
464 disk_read_sector(d,d->sect_pos,d->data_buffer);
465 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC|ATA_STATUS_DRQ;
466 }
467
468 /* ATA write sector callback */
469 static void ata_cmd_write_callback(struct pcmcia_disk_data *d)
470 {
471 /* Write the sector */
472 disk_write_sector(d,d->sect_pos,d->data_buffer);
473 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC|ATA_STATUS_DRQ;
474 d->sect_pos++;
475
476 d->sect_remaining--;
477
478 if (!d->sect_remaining) {
479 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC;
480 }
481 }
482
483 /* Handle an ATA command */
484 static void ata_handle_cmd(struct pcmcia_disk_data *d)
485 {
486 #if DEBUG_ATA
487 vm_log(d->vm,d->dev.name,"ATA command 0x%2.2x\n",(u_int)d->ata_cmd);
488 #endif
489
490 d->data_pos = 0;
491
492 switch(d->ata_cmd) {
493 case ATA_CMD_IDENT_DEVICE:
494 ata_identify_device(d);
495 d->ata_cmd_callback = ata_cmd_ident_device_callback;
496 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC|ATA_STATUS_DRQ;
497 break;
498
499 case ATA_CMD_READ_SECTOR:
500 d->sect_remaining = d->sect_count;
501
502 if (!d->sect_remaining)
503 d->sect_remaining = 256;
504
505 ata_set_sect_pos(d);
506 disk_read_sector(d,d->sect_pos,d->data_buffer);
507 d->ata_cmd_callback = ata_cmd_read_callback;
508 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC|ATA_STATUS_DRQ;
509 break;
510
511 case ATA_CMD_WRITE_SECTOR:
512 d->sect_remaining = d->sect_count;
513
514 if (!d->sect_remaining)
515 d->sect_remaining = 256;
516
517 ata_set_sect_pos(d);
518 d->ata_cmd_callback = ata_cmd_write_callback;
519 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC|ATA_STATUS_DRQ;
520 break;
521
522 default:
523 vm_log(d->vm,d->dev.name,"unhandled ATA command 0x%2.2x\n",
524 (u_int)d->ata_cmd);
525 }
526 }
527
528 /*
529 * dev_pcmcia_disk_access_0()
530 */
531 void *dev_pcmcia_disk_access_0(cpu_gen_t *cpu,struct vdevice *dev,
532 m_uint32_t offset,u_int op_size,u_int op_type,
533 m_uint64_t *data)
534 {
535 struct pcmcia_disk_data *d = dev->priv_data;
536
537 /* Compute the good internal offset */
538 offset = (offset >> 1) ^ 1;
539
540 #if DEBUG_ACCESS
541 if (op_type == MTS_READ) {
542 cpu_log(cpu,d->dev.name,
543 "reading offset 0x%5.5x at pc=0x%llx (size=%u)\n",
544 offset,cpu_get_pc(cpu),op_size);
545 } else {
546 cpu_log(cpu,d->dev.name,
547 "writing offset 0x%5.5x, data=0x%llx at pc=0x%llx (size=%u)\n",
548 offset,*data,cpu_get_pc(cpu),op_size);
549 }
550 #endif
551
552 /* Card Information Structure */
553 if (offset < sizeof(cis_table)) {
554 if (op_type == MTS_READ)
555 *data = cis_table[offset];
556
557 return NULL;
558 }
559
560 switch(offset) {
561 case 0x102: /* Pin Replacement Register */
562 if (op_type == MTS_READ)
563 *data = 0x22;
564 break;
565
566 case 0x80001: /* Sector Count + Sector no */
567 if (op_type == MTS_READ) {
568 *data = (d->sect_no << 8) + d->sect_count;
569 } else {
570 d->sect_no = *data >> 8;
571 d->sect_count = *data & 0xFF;
572 }
573 break;
574
575 case 0x80002: /* Cylinder Low + Cylinder High */
576 if (op_type == MTS_READ) {
577 *data = (d->cyl_high << 8) + d->cyl_low;
578 } else {
579 d->cyl_high = *data >> 8;
580 d->cyl_low = *data & 0xFF;
581 }
582 break;
583
584 case 0x80003: /* Select Card/Head + Status/Command register */
585 if (op_type == MTS_READ)
586 *data = (d->ata_status << 8) + d->head;
587 else {
588 d->ata_cmd = *data >> 8;
589 d->head = *data;
590 ata_handle_cmd(d);
591 }
592 break;
593
594 default:
595 /* Data buffer access ? */
596 if ((offset >= d->data_offset) &&
597 (offset < d->data_offset + (SECTOR_SIZE/2)))
598 {
599 if (op_type == MTS_READ) {
600 *data = d->data_buffer[(d->data_pos << 1)];
601 *data += d->data_buffer[(d->data_pos << 1)+1] << 8;
602 } else {
603 d->data_buffer[(d->data_pos << 1)] = *data & 0xFF;
604 d->data_buffer[(d->data_pos << 1)+1] = *data >> 8;
605 }
606
607 d->data_pos++;
608
609 /* Buffer complete: call the callback function */
610 if (d->data_pos == (SECTOR_SIZE/2)) {
611 d->data_pos = 0;
612
613 if (d->ata_cmd_callback)
614 d->ata_cmd_callback(d);
615 }
616 }
617 }
618
619 return NULL;
620 }
621
622 /*
623 * dev_pcmcia_disk_access_1()
624 */
625 void *dev_pcmcia_disk_access_1(cpu_gen_t *cpu,struct vdevice *dev,
626 m_uint32_t offset,u_int op_size,u_int op_type,
627 m_uint64_t *data)
628 {
629 struct pcmcia_disk_data *d = dev->priv_data;
630
631 /* Compute the good internal offset */
632 offset = (offset >> 1) ^ 1;
633
634 #if DEBUG_ACCESS
635 if (op_type == MTS_READ) {
636 cpu_log(cpu,d->dev.name,
637 "reading offset 0x%5.5x at pc=0x%llx (size=%u)\n",
638 offset,cpu_get_pc(cpu),op_size);
639 } else {
640 cpu_log(cpu,d->dev.name,
641 "writing offset 0x%5.5x, data=0x%llx at pc=0x%llx (size=%u)\n",
642 offset,*data,cpu_get_pc(cpu),op_size);
643 }
644 #endif
645
646 switch(offset) {
647 case 0x02: /* Sector Count + Sector no */
648 if (op_type == MTS_READ) {
649 *data = (d->sect_no << 8) + d->sect_count;
650 } else {
651 d->sect_no = *data >> 8;
652 d->sect_count = *data & 0xFF;
653 }
654 break;
655
656 case 0x04: /* Cylinder Low + Cylinder High */
657 if (op_type == MTS_READ) {
658 *data = (d->cyl_high << 8) + d->cyl_low;
659 } else {
660 d->cyl_high = *data >> 8;
661 d->cyl_low = *data & 0xFF;
662 }
663 break;
664
665 case 0x06: /* Select Card/Head + Status/Command register */
666 if (op_type == MTS_READ)
667 *data = (d->ata_status << 8) + d->head;
668 else {
669 d->ata_cmd = *data >> 8;
670 d->head = *data & 0xFF;
671 ata_handle_cmd(d);
672 }
673 break;
674
675 case 0x08: /* Data */
676 if (op_type == MTS_READ) {
677 *data = d->data_buffer[(d->data_pos << 1)];
678 *data += d->data_buffer[(d->data_pos << 1)+1] << 8;
679 } else {
680 d->data_buffer[(d->data_pos << 1)] = *data & 0xFF;
681 d->data_buffer[(d->data_pos << 1)+1] = *data >> 8;
682 }
683
684 d->data_pos++;
685
686 /* Buffer complete: call the callback function */
687 if (d->data_pos == (SECTOR_SIZE/2)) {
688 d->data_pos = 0;
689
690 if (d->ata_cmd_callback)
691 d->ata_cmd_callback(d);
692 }
693 break;
694
695 case 0x0E: /* Status/Drive Control + Drive Address */
696 break;
697 }
698
699 return NULL;
700 }
701
702 /* Shutdown a PCMCIA disk device */
703 void dev_pcmcia_disk_shutdown(vm_instance_t *vm,struct pcmcia_disk_data *d)
704 {
705 if (d != NULL) {
706 /* Remove the device */
707 dev_remove(vm,&d->dev);
708
709 /* Close disk file */
710 if (d->fd != -1) close(d->fd);
711
712 /* Free filename */
713 free(d->filename);
714
715 /* Free the structure itself */
716 free(d);
717 }
718 }
719
720 /* Initialize a PCMCIA disk */
721 vm_obj_t *dev_pcmcia_disk_init(vm_instance_t *vm,char *name,
722 m_uint64_t paddr,m_uint32_t len,
723 u_int disk_size,int mode)
724 {
725 struct pcmcia_disk_data *d;
726 m_uint32_t tot_sect;
727
728 /* allocate the private data structure */
729 if (!(d = malloc(sizeof(*d)))) {
730 fprintf(stderr,"PCMCIA: unable to create disk device '%s'.\n",name);
731 return NULL;
732 }
733
734 memset(d,0,sizeof(*d));
735 vm_object_init(&d->vm_obj);
736 d->vm = vm;
737 d->vm_obj.name = name;
738 d->vm_obj.data = d;
739 d->vm_obj.shutdown = (vm_shutdown_t)dev_pcmcia_disk_shutdown;
740 d->fd = -1;
741
742 if (!(d->filename = vm_build_filename(vm,name))) {
743 fprintf(stderr,"PCMCIA: unable to create filename.\n");
744 goto err_filename;
745 }
746
747 /* Data buffer offset in mapped memory */
748 d->data_offset = 0x80200;
749 d->ata_status = ATA_STATUS_RDY|ATA_STATUS_DSC;
750
751 /* Compute the number of cylinders given a disk size in Mb */
752 tot_sect = ((m_uint64_t)disk_size * 1048576) / SECTOR_SIZE;
753
754 d->nr_heads = DISK_NR_HEADS;
755 d->sects_per_track = DISK_SECTS_PER_TRACK;
756 d->nr_cylinders = tot_sect / (d->nr_heads * d->sects_per_track);
757
758 vm_log(vm,name,"C/H/S settings = %u/%u/%u\n",
759 d->nr_cylinders,d->nr_heads,d->sects_per_track);
760
761 /* Create the disk file */
762 if (disk_create(d) == -1)
763 goto err_disk_create;
764
765 dev_init(&d->dev);
766 d->dev.name = name;
767 d->dev.priv_data = d;
768 d->dev.phys_addr = paddr;
769 d->dev.phys_len = len;
770 d->dev.flags = VDEVICE_FLAG_CACHING;
771
772 if (mode == 0)
773 d->dev.handler = dev_pcmcia_disk_access_0;
774 else
775 d->dev.handler = dev_pcmcia_disk_access_1;
776
777 /* Map this device to the VM */
778 vm_bind_device(vm,&d->dev);
779 vm_object_add(vm,&d->vm_obj);
780 return(&d->vm_obj);
781
782 err_disk_create:
783 free(d->filename);
784 err_filename:
785 free(d);
786 return NULL;
787 }
788
789 /* Get the device associated with a PCMCIA disk object */
790 struct vdevice *dev_pcmcia_disk_get_device(vm_obj_t *obj)
791 {
792 struct pcmcia_disk_data *d;
793
794 if (!obj || !(d = obj->data))
795 return NULL;
796
797 return(&d->dev);
798 }