OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [peripheral/] [atadevice-cmdi.c] - Blame information for rev 22

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* atadevice_cmdi.c -- ATA Device simulation
2
   Command interpreter for the simulated harddisk.
3
 
4
   Copyright (C) 2002 Richard Herveille, rherveille@opencores.org
5
   Copyright (C) 2008 Embecosm Limited
6
 
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
10
 
11
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15
 
16
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20
 
21
   You should have received a copy of the GNU General Public License along
22
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
 
24
/* This program is commented throughout in a fashion suitable for processing
25
   with Doxygen. */
26
 
27
/* For fun, count the number of FIXMEs :-( */
28
 
29
 
30
/* Autoconf and/or portability configuration */
31
#include "config.h"
32
 
33
/* Package includes */
34
#include "atadevice-cmdi.h"
35
#include "atahost.h"
36
#include "atacmd.h"
37
#include "sched.h"
38
 
39
 
40
/* FIXME: If device0 is not selected and then an interrupt occurs and
41
 * then it is selected, the interrupt should be delivered, but I just drop it
42
 * (same goes for device 1). */
43
static void
44
ata_cmd_complete (void *dat)
45
{
46
  struct ata_device *dev = dat;
47
 
48
  dev->regs.status &= ~ATA_SR_BSY;
49
 
50
  if (dev->regs.device_control & ATA_DCR_IEN)
51
    return;
52
 
53
  if ((dev->regs.device_head & ATA_DHR_DEV) != dev->internals.dev)
54
    return;
55
 
56
  if (dev->sigs.intrq)
57
    return;
58
 
59
  dev->sigs.intrq = 1;
60
  ata_int (dev->internals.host);
61
}
62
 
63
static void
64
ata_set_sect (struct ata_device *dev, uint32_t sect)
65
{
66
  uint16_t cyl;
67
 
68
  dev->regs.device_head &= ~ATA_DHR_H;
69
  if (dev->regs.device_head & ATA_DHR_LBA)
70
    {
71
      dev->regs.sector_number = sect & 0xff;
72
      dev->regs.cylinder_low = (sect >> 8) & 0xff;
73
      dev->regs.cylinder_high = (sect >> 16) & 0xff;
74
      dev->regs.device_head |= (sect >> 24) & 0xf;
75
    }
76
  else
77
    {
78
      cyl = sect / (dev->conf.heads * dev->conf.sectors);
79
      dev->regs.cylinder_low = cyl & 0xff;
80
      dev->regs.cylinder_high = (cyl >> 8) & 0xff;
81
 
82
      cyl = sect % (dev->conf.heads * dev->conf.sectors);
83
      dev->regs.sector_number = (cyl % dev->conf.sectors) + 1;
84
      dev->regs.device_head |= (cyl / dev->conf.sectors) & 0xf;
85
    }
86
}
87
 
88
/* Returns the concatenated lba address from the values in the addr regs */
89
static uint32_t
90
ata_calc_lba (struct ata_device *dev)
91
{
92
  uint32_t lba;
93
 
94
  /* check if we are using CHS or LBA translation, fill in the bits */
95
  if (dev->regs.device_head & ATA_DHR_LBA)
96
    {
97
      /* we are using LBA translation                               */
98
      lba = (dev->regs.device_head & ATA_DHR_H) << 24 |
99
        (dev->regs.cylinder_high) << 16 |
100
        (dev->regs.cylinder_low) << 8 | dev->regs.sector_number;
101
    }
102
  else
103
    {
104
      /* we are using CHS translation, calculate lba address        */
105
      lba = (dev->regs.cylinder_high << 16) | dev->regs.cylinder_low;
106
      lba *= dev->internals.heads_per_cylinder;
107
      lba += dev->regs.device_head & ATA_DHR_H;
108
      lba *= dev->internals.sectors_per_track;
109
      lba += dev->regs.sector_number;
110
      lba -= 1;
111
    }
112
  return lba;
113
}
114
 
115
/* Reads a sector from the device */
116
static void
117
ata_read_sect (struct ata_device *dev)
118
{
119
  if (!dev->internals.nr_sect)
120
    return;
121
 
122
  /* set the file-positon pointer to the start of the sector    */
123
  fseek (dev->conf.stream, dev->internals.lba, SEEK_SET);
124
 
125
  /* get the bytes from the stream                              */
126
  fread (dev->internals.dbuf, BYTES_PER_SECTOR, 1, dev->conf.stream);
127
 
128
  /* set status register bits                                   */
129
  dev->regs.status = ATA_SR_DRDY | ATA_SR_DRQ;
130
 
131
  /* reset the databuffer                                       */
132
  dev->internals.dbuf_cnt = BYTES_PER_SECTOR / 2;       //Words, not bytes
133
  dev->internals.dbuf_ptr = dev->internals.dbuf;
134
 
135
  dev->internals.lba += BYTES_PER_SECTOR;
136
  dev->internals.nr_sect--;
137
 
138
  SCHED_ADD (ata_cmd_complete, dev, 400000);
139
}
140
 
141
/* Writes a sector to the device */
142
static void
143
ata_write_sect (struct ata_device *dev)
144
{
145
  if (!dev->internals.nr_sect)
146
    {
147
      dev->regs.status = ATA_SR_DRDY;
148
      return;
149
    }
150
 
151
  /* set the file-positon pointer to the start of the sector    */
152
  fseek (dev->conf.stream, dev->internals.lba, SEEK_SET);
153
 
154
  /* get the bytes from the stream                              */
155
  fwrite (dev->internals.dbuf, BYTES_PER_SECTOR, 1, dev->conf.stream);
156
 
157
  /* set status register bits                                   */
158
  dev->regs.status = ATA_SR_DRDY | ATA_SR_DRQ | ATA_SR_DSC | ATA_SR_BSY;
159
 
160
  /* reset the databuffer                                       */
161
  dev->internals.dbuf_cnt = BYTES_PER_SECTOR / 2;       //Words, not bytes
162
  dev->internals.dbuf_ptr = dev->internals.dbuf;
163
 
164
  dev->internals.lba += BYTES_PER_SECTOR;
165
  if (!--dev->internals.nr_sect)
166
    dev->regs.status = ATA_SR_DRDY | ATA_SR_DSC | ATA_SR_BSY;
167
 
168
  SCHED_ADD (ata_cmd_complete, dev, 40000);
169
}
170
 
171
 
172
/*
173
  A T A _ S E T _ D E V I C E _ S I G N A T U R E
174
 
175
  called whenever a command needs to signal the device type (packet, non-packet)
176
*/
177
static void
178
ata_set_device_signature (struct ata_device *device, int packet)
179
{
180
  if (packet)
181
    {
182
      /* place PACKET Command feature set signature in register block   */
183
      device->regs.sector_count = 0x01;
184
      device->regs.sector_number = 0x01;
185
      device->regs.cylinder_low = 0x14;
186
      device->regs.cylinder_high = 0xEB;
187
    }
188
  else
189
    {
190
      /* place NON_PACKET Command feature set signature in register block */
191
      device->regs.sector_count = 0x01;
192
      device->regs.sector_number = 0x01;
193
      device->regs.cylinder_low = 0x00;
194
      device->regs.cylinder_high = 0x00;
195
      device->regs.device_head = 0x00;
196
    }
197
}
198
 
199
 
200
/*
201
  A T A _ E X E C U T E _ D E V I C E _ D I A G N O S T I C S
202
*/
203
void
204
ata_execute_device_diagnostics_cmd (struct ata_device *device)
205
{
206
  /* clear SRST bit, if set                                           */
207
  device->regs.device_control &= ~ATA_DCR_RST;
208
 
209
  /* content of features register is undefined                        */
210
 
211
  /*
212
     set device error register
213
     Device0: 0x01 = Device0 passed & Device1 passed/not present
214
     Device1: 0x01 = Device1 passed
215
   */
216
  device->regs.error = 0x01;
217
  /* something about DASP- and error_register_bit7                    */
218
  /* if device1 is not present or if device1 is present and passed    */
219
  /* diagnostics, than bit7 should be cleared.                        */
220
  /* This means we always clear bit7                                  */
221
 
222
 
223
  /* check if device implements packet protocol                       */
224
  if (device->conf.packet)
225
    {
226
      /* place PACKET command feature set signature in register block   */
227
      ata_set_device_signature (device, 1);
228
 
229
      device->regs.status &= 0xf8;
230
 
231
      if (device->regs.command == DEVICE_RESET)
232
        device->regs.device_head = device->regs.device_head & ATA_DHR_LBA;
233
      else
234
        device->regs.device_head = 0x00;
235
    }
236
  else
237
    {
238
      /* place NON_PACKET Command feature set signature in register block */
239
      ata_set_device_signature (device, 0);
240
 
241
      device->regs.status &= 0x82;
242
    }
243
 
244
 
245
  /* we passed diagnostics, so set the PDIAG-line                     */
246
  device->sigs.pdiago = 1;
247
}
248
 
249
 
250
/*
251
  A T A _ D E V I C E _ R E S E T
252
*/
253
static void
254
ata_device_reset_cmd (struct ata_device *device)
255
{
256
  if (!device->conf.packet)
257
    {
258
      fprintf( stderr, "Warning: executing DEVICE_RESET on non-packet "
259
               "device\n");
260
    }
261
 
262
  ata_execute_device_diagnostics_cmd (device);
263
}
264
 
265
 
266
/*
267
  A T A _ I D E N T I F Y _ D E V I C E
268
*/
269
static void
270
ata_identify_device_cmd (struct ata_device *device)
271
{
272
  unsigned char *chksum_buf, chksum;
273
  unsigned short *buf;
274
  int n;
275
  unsigned int tmp;
276
 
277
  /* reset databuffer                                                 */
278
  device->internals.dbuf_cnt = 256;
279
  device->internals.dbuf_ptr = device->internals.dbuf;
280
  device->internals.end_t_func = NULL;
281
 
282
  buf = device->internals.dbuf;
283
  chksum_buf = (unsigned char *) buf;
284
 
285
  /*
286
     if number_of_available_sectors (==device->size / BYTES_PER_SECTOR) < 16,514,064 then
287
     support CHS translation where:
288
     LBA = ( ((cylinder_number*heads_per_cylinder) +head_number) *sectors_per_track ) +sector_number -1;
289
 
290
     Cylinders : 1-255   (or max.cylinders)
291
     Heads     : 0-15    (or max.heads    )
292
     Sectors   : 0-65535 (or max.sectors  )
293
   */
294
 
295
  /*
296
     1) Word 1 shall contain the number of user-addressable logical cylinders in the default CHS
297
     translation. If the content of words (61:60) is less than 16,514,064 then the content of word 1
298
     shall be greater than or equal to one and less than or equal to 65,535. If the content of words
299
     (61:60) is greater than or equal to 16,514,064 then the content of
300
     word 1 shall be equal to 16,383.
301
     2) Word 3 shall contain the number of user-addressable logical heads in the default CHS
302
     translation. The content of word 3 shall be greater than or equal to one and less than or equal to
303
     16. For compatibility with some BIOSs, the content of word 3 may be equal to 15 if the content of
304
     word 1 is greater than 8192.
305
     3) Word 6 shall contain the number of user-addressable logical sectors in the default CHS
306
     translation. The content of word 6 shall be greater than or equal to one and less than or equal to
307
     63.
308
     4) [(The content of word 1) * (the content of word 3) * (the content of word 6)] shall be less than or
309
     equal to 16,514,064
310
     5) Word 54 shall contain the number of user-addressable logical cylinders in the current CHS
311
     translation. The content of word 54 shall be greater than or equal to one and less than or
312
     equal to 65,535. After power-on of after a hardware reset the content of word 54 shall be equal to
313
     the content of word 1.
314
     6) Word 55 shall contain the number of user-addressable logical heads in the current CHS
315
     translation. The content of word 55 shall be greater than or equal to one and less than or equal
316
     to 16. After power-on or after a hardware reset the content of word 55 shall equal the content of
317
     word 3.
318
     7) Word 56 shall contain the user-addressable logical sectors in the current CHS
319
     translation. The content of word 56 should be equal to 63 for compatibility with all BIOSs.
320
     However, the content of word 56 may be greater than or equal to one and less than or equal to
321
     255. At power-on or after a hardware reset the content of word 56 shall equal the content of
322
     word 6.
323
     8) Words (58:57) shall contain the user-addressable capacity in sectors for the current CHS
324
     translation. The content of words (58:57) shall equal [(the content of word 54) * (the content of
325
     word 55) * (the content of word 56)] The content of words (58:57) shall be less than or equal to
326
     16,514,064. The content of words (58:57) shall be less than or equal to the content of words
327
     (61:60).
328
     9) The content of words 54, 55, 56, and (58:57) may be affected by the host issuing an INITIALIZE
329
     DEVICE PARAMETERS command.
330
     10)If the content of words (61:60) is greater than 16,514,064 and if the device does not support CHS
331
     addressing, then the content of words 1, 3, 6, 54, 55, 56, and (58:57) shall equal zero. If the
332
     content of word 1, word 3, or word 6 equals zero, then the content of words 1, 3, 6, 54, 55, 56,
333
     and (58:57) shall equal zero.
334
     11)Words (61:60) shall contain the total number of user-addressable sectors. The content of words
335
     (61:60) shall be greater than or equal to one and less than or equal to 268,435,456.
336
     12)The content of words 1, 54, (58:57), and (61:60) may be affected by the host issuing a SET MAX
337
     ADDRESS command.
338
   */
339
 
340
 
341
  /* check if this is a NON-PACKET device                             */
342
  if (device->conf.packet)
343
    {
344
      /*
345
         This is a PACKET device.
346
         Respond by placing PACKET Command feature set signature in block registers.
347
         Abort command.
348
       */
349
 
350
      ata_set_device_signature (device, 1);
351
      device->regs.status = ATA_SR_ERR;
352
      device->regs.error = ATA_ERR_ABT;
353
    }
354
  else
355
    {
356
      /* start filling buffer                                           */
357
 
358
      /*
359
         word 0: General configuration
360
         15  : 0 = ATA device
361
         14-8: retired
362
         7   : 1 = removable media device (not set)
363
         6   : 1 = not-removable controller and/or device (set)
364
         5-0 : retired and/or always set to zero
365
       */
366
      *buf++ = 0x0040;
367
 
368
      /*
369
         word 1: Number of logical cylinders
370
 
371
         >=1, <= 65535
372
       */
373
      if ((tmp = device->conf.size_sect) >= 16514064)
374
        *buf++ = 16383;
375
      else
376
        if ((tmp /= (device->conf.heads + 1) * device->conf.sectors) > 65535)
377
        *buf++ = 65535;
378
      else
379
        *buf++ = tmp;
380
 
381
      /*
382
         word 2: Specific configuration
383
 
384
         0x37c8: Device requires SET_FEATURES subcommand to spinup after power-on
385
         and IDENTIFY_DEVICE response is incomplete
386
         0x738c: Device requires SET_FEATURES subcommand to spinup after power-on
387
         and IDENTIFY_DEVICE response is complete
388
         0x8c73: Device does not require SET_FEATURES subcommand to spinup after power-on
389
         and IDENTIFY_DEVICE response is incomplete
390
         0xc837: Device does not require SET_FEATURES subcommand to spinup after power-on
391
         and IDENTIFY_DEVICE response is complete
392
 
393
         pick-one
394
       */
395
      *buf++ = 0xc837;
396
 
397
      /*
398
         word 3: Number of logical heads
399
 
400
         >= 1, <= 16
401
 
402
         set to 15 if word1 > 8192 (BIOS compatibility)
403
       */
404
      *buf++ = device->conf.heads;
405
 
406
      /*
407
         word 5-4: retired
408
       */
409
      buf += 2;
410
 
411
      /*
412
         word 6: Number of logical sectors per logical track
413
 
414
         >= 1, <= 63
415
       */
416
      *buf++ = device->conf.sectors;
417
 
418
      /*
419
         word 8-7: Reserved by the CompactFLASH association
420
       */
421
      buf += 2;
422
 
423
      /*
424
         word 9: retired
425
       */
426
      buf++;
427
 
428
      /*
429
         word 19-10: Serial number (ASCII)
430
       */
431
      /*           " www.opencores.org  "                               */
432
      *buf++ = (' ' << 8) | 'w';
433
      *buf++ = ('w' << 8) | 'w';
434
      *buf++ = ('.' << 8) | 'o';
435
      *buf++ = ('p' << 8) | 'e';
436
      *buf++ = ('n' << 8) | 'c';
437
      *buf++ = ('o' << 8) | 'r';
438
      *buf++ = ('e' << 8) | 's';
439
      *buf++ = ('.' << 8) | 'o';
440
      *buf++ = ('r' << 8) | 'g';
441
      *buf++ = (' ' << 8) | ' ';
442
 
443
      /*
444
         word 22   : obsolete
445
         word 21-20: retired
446
       */
447
      buf += 3;
448
 
449
      /*
450
         word 26-23: Firmware revision
451
       */
452
      strncpy ((char *) buf, device->conf.firmware, 8);
453
      buf += 4;
454
 
455
      /*
456
         word 46-27: Model number
457
       */
458
      /*           " ata device model  (C)Richard Herveille "           */
459
      *buf++ = (' ' << 8) | 'a';
460
      *buf++ = ('t' << 8) | 'a';
461
      *buf++ = (' ' << 8) | 'd';
462
      *buf++ = ('e' << 8) | 'v';
463
      *buf++ = ('i' << 8) | 'c';
464
      *buf++ = ('e' << 8) | ' ';
465
      *buf++ = ('m' << 8) | 'o';
466
      *buf++ = ('d' << 8) | 'e';
467
      *buf++ = ('l' << 8) | ' ';
468
      *buf++ = (' ' << 8) | '(';
469
      *buf++ = ('C' << 8) | ')';
470
      *buf++ = ('R' << 8) | 'i';
471
      *buf++ = ('c' << 8) | 'h';
472
      *buf++ = ('a' << 8) | 'r';
473
      *buf++ = ('d' << 8) | ' ';
474
      *buf++ = ('H' << 8) | 'e';
475
      *buf++ = ('r' << 8) | 'v';
476
      *buf++ = ('e' << 8) | 'i';
477
      *buf++ = ('l' << 8) | 'l';
478
      *buf++ = ('e' << 8) | ' ';
479
 
480
      /*
481
         word 47:
482
         15-8: 0x80
483
         7-0: 0x00 reserved
484
         0x01-0xff maximum number of sectors to be transfered
485
         per interrupt on a READ/WRITE MULTIPLE command
486
       */
487
      *buf++ = 0x8001;
488
 
489
      /*
490
         word 48: reserved
491
       */
492
      buf++;
493
 
494
      /*
495
         word 49: Capabilities
496
         15-14: reserved for IDENTIFY PACKET DEVICE
497
         13   : 1=standby timers are supported
498
         0=standby timers are handled by the device FIXME
499
         12   : reserved for IDENTIFY PACKET DEVICE
500
         11   : 1=IORDY supported
501
         0=IORDY may be supported
502
         10   : 1=IORDY may be disabled
503
         9   : set to one
504
         8   : set to one
505
         7-0 : retired
506
       */
507
      *buf++ = 0x0f00;
508
 
509
      /*
510
         word 50: Capabilities
511
         15  : always cleared to zero
512
         14  : always set to one
513
         13-1: reserved
514
 
515
       */
516
      *buf++ = 0x4000;
517
 
518
      /*
519
         word 52-51: obsolete
520
       */
521
      buf += 2;
522
 
523
      /*
524
         word 53:
525
         15-3: Reserved
526
         2   : 1=value in word 88 is valid
527
         0=value in word 88 is not valid
528
         1   : 1=value in word 64-70 is valid
529
         0=value in word 64-70 is not valid
530
 
531
         0=value in word 54-58 is not valid
532
       */
533
      *buf++ = 0x0007;
534
 
535
 
536
      /*
537
         word 54: number of current logical cylinders (0-65535)
538
       */
539
      if ((tmp = device->conf.size_sect) > 16514064)
540
        tmp = 16514064;
541
 
542
      tmp /= (device->internals.heads_per_cylinder +
543
              1) * (device->internals.sectors_per_track);
544
      if (tmp > 65535)
545
        *buf++ = 65535;
546
      else
547
        *buf++ = tmp;
548
 
549
      /*
550
         word 55: number of current logical heads, (0-15)
551
       */
552
      *buf++ = device->internals.heads_per_cylinder + 1;
553
 
554
      /*
555
         word 56: number of current logical sectors per track (1-255)
556
       */
557
      *buf++ = device->internals.sectors_per_track;
558
 
559
      /*
560
         word 58-57: current capacity in sectors
561
       */
562
      tmp = *(buf - 3) * *(buf - 2) * *(buf - 1);
563
      *buf++ = tmp >> 16;
564
      *buf++ = tmp & 0xffff;
565
 
566
      /*
567
         word 59:
568
         15-9: reserved
569
         8   : 1=multiple sector setting is valid
570
         7-0 : current setting for number of sectors to be transfered
571
         per interrupt on a READ/WRITE MULTIPLE command
572
       */
573
      *buf++ = 0x0001;          // not really a FIXME
574
 
575
      /*
576
         word 60-61: Total number of user addressable sectors (LBA only)
577
       */
578
      *buf++ = device->conf.size_sect;
579
      *buf++ = device->conf.size_sect >> 16;
580
 
581
      /*
582
         word 62: obsolete
583
       */
584
      buf++;
585
 
586
      /* FIXME
587
         word 63: DMA settings
588
         15-11: Reserved
589
         10   : 1=Multiword DMA Mode 2 is selected
590
         0=Multiword DMA Mode 2 is not selected
591
         9   : 1=Multiword DMA Mode 1 is selected
592
         0=Multiword DMA Mode 1 is not selected
593
         8   : 1=Multiword DMA Mode 0 is selected
594
         0=Multiword DMA Mode 0 is not selected
595
         7-3 : Reserved
596
         2   : Multiword DMA Mode 2 and below are supported
597
         1   : Multiword DMA Mode 1 and below are supported
598
 
599
       */
600
      *buf = 0;
601
      if (device->conf.mwdma >= 0)
602
        *buf |= 0x1;
603
      if (device->conf.mwdma >= 1)
604
        *buf |= 0x2;
605
      if (device->conf.mwdma >= 2)
606
        *buf |= 0x4;
607
 
608
      buf++;
609
 
610
      /*
611
         word 64:
612
         15-8: reserved
613
         7-0: Advanced PIO modes supported
614
 
615
         7-2: reserved
616
         1  : PIO mode4 supported
617
 
618
       */
619
      *buf = 0;
620
      if (device->conf.pio >= 3)
621
        *buf |= 0x1;
622
      if (device->conf.pio >= 4)
623
        *buf |= 0x2;
624
      buf++;
625
 
626
      /*
627
         word 65: Minimum Multiword DMA transfer cycle time per word (nsec)
628
       */
629
      *buf++ = MIN_MWDMA_CYCLE_TIME;
630
 
631
      /*
632
         word 66: Manufacturer's recommended Multiword DMA transfer cycle time per word (nsec)
633
       */
634
      *buf++ = RECOMMENDED_MWDMA_CYCLE_TIME;
635
 
636
 
637
      /*
638
         word 67: Minimum PIO transfer cycle time per word (nsec), without IORDY flow control
639
       */
640
      *buf++ = MIN_PIO_CYCLE_TIME_NO_IORDY;
641
 
642
      /*
643
         word 68: Minimum PIO transfer cycle time per word (nsec), with IORDY flow control
644
       */
645
      *buf++ = MIN_PIO_CYCLE_TIME_IORDY;
646
 
647
      /*
648
         word 69-70: reserved for future command overlap and queueing
649
         71-74: reserved for IDENTIFY PACKET DEVICE command
650
       */
651
      buf += 6;
652
 
653
      /*
654
         word 75: Queue depth
655
         15-5: reserved
656
         4-0: queue depth -1
657
       */
658
      *buf++ = SUPPORT_READ_WRITE_DMA_QUEUED ? QUEUE_DEPTH : 0x0000;
659
 
660
      /*
661
         word 76-79: reserved
662
       */
663
      buf += 4;
664
 
665
      /*
666
         word 80: MAJOR ATA version
667
         We simply report that we do not report a version
668
 
669
         You can also set bits 5-2 to indicate compliancy to
670
         ATA revisions 5-2 (1 & 0 are obsolete)
671
       */
672
      *buf++ = 0x0000;
673
 
674
      /*
675
         word 81: MINOR ATA version
676
         We report ATA/ATAPI-5 T13 1321D revision 3 (0x13)
677
       */
678
      *buf++ = 0x0013;
679
 
680
      /*
681
         word 82: Command set supported
682
       */
683
      *buf++ = 0 << 15 | /* obsolete        */
684
        SUPPORT_NOP_CMD << 14 | SUPPORT_READ_BUFFER_CMD << 13 | SUPPORT_WRITE_BUFFER_CMD << 12 | 0 << 11 |       /* obsolete        */
685
        SUPPORT_HOST_PROTECTED_AREA << 10 | SUPPORT_DEVICE_RESET_CMD << 9 | SUPPORT_SERVICE_INTERRUPT << 8 | SUPPORT_RELEASE_INTERRUPT << 7 | SUPPORT_LOOKAHEAD << 6 | SUPPORT_WRITE_CACHE << 5 | 0 << 4 |       /* cleared to zero */
686
        SUPPORT_POWER_MANAGEMENT << 3 |
687
        SUPPORT_REMOVABLE_MEDIA << 2 |
688
        SUPPORT_SECURITY_MODE << 1 | SUPPORT_SMART << 0;
689
 
690
      /*
691
         word 83: Command set supported
692
       */
693
      *buf++ = 0 << 15 | /* cleared to zero */
694
        1 << 14 |               /* set to one      */
695
 
696
        SUPPORT_SET_MAX << 8 | 0 << 7 |  /* reserved for    */
697
        /* project 1407DT  */
698
        SET_FEATURES_REQUIRED_AFTER_POWER_UP << 6 |
699
        SUPPORT_POWER_UP_IN_STANDBY_MODE << 5 |
700
        SUPPORT_REMOVABLE_MEDIA_NOTIFICATION << 4 |
701
        SUPPORT_APM << 3 |
702
        SUPPORT_CFA << 2 |
703
        SUPPORT_READ_WRITE_DMA_QUEUED << 1 | SUPPORT_DOWNLOAD_MICROCODE << 0;
704
 
705
      /*
706
         word 84: Command set/feature supported
707
       */
708
      *buf++ = 0 << 15 | /* cleared to zero */
709
        1 << 14                 /* set to one      */
710
        ;                       /* 0-13 reserved   */
711
 
712
      /*
713
         word 85: Command set enabled FIXME
714
       */
715
      *buf++ = 0 << 15 | /* obsolete        */
716
        SUPPORT_NOP_CMD << 14 | SUPPORT_READ_BUFFER_CMD << 13 | SUPPORT_WRITE_BUFFER_CMD << 12 | 0 << 11 |       /* obsolete        */
717
        SUPPORT_HOST_PROTECTED_AREA << 10 | SUPPORT_DEVICE_RESET_CMD << 9 | SUPPORT_SERVICE_INTERRUPT << 8 | SUPPORT_RELEASE_INTERRUPT << 7 | SUPPORT_LOOKAHEAD << 6 | SUPPORT_WRITE_CACHE << 5 | 0 << 4 |       /* cleared to zero */
718
        SUPPORT_POWER_MANAGEMENT << 3 |
719
        SUPPORT_REMOVABLE_MEDIA << 2 |
720
        SUPPORT_SECURITY_MODE << 1 | SUPPORT_SMART << 0;
721
 
722
      /*
723
         word 86: Command set enables
724
       */
725
      *buf++ = 0 << 9 |          /* 15-9 reserved   */
726
        SUPPORT_SET_MAX << 8 | 0 << 7 |  /* reserved for    */
727
        /* project 1407DT  */
728
        SET_FEATURES_REQUIRED_AFTER_POWER_UP << 6 |
729
        SUPPORT_POWER_UP_IN_STANDBY_MODE << 5 |
730
        SUPPORT_REMOVABLE_MEDIA_NOTIFICATION << 4 |
731
        SUPPORT_APM << 3 |
732
        SUPPORT_CFA << 2 |
733
        SUPPORT_READ_WRITE_DMA_QUEUED << 1 | SUPPORT_DOWNLOAD_MICROCODE << 0;
734
 
735
      /*
736
         word 87: Command set/feature supported
737
       */
738
      *buf++ = 0 << 15 | /* cleared to zero */
739
        1 << 14                 /* set to one      */
740
        ;                       /* 0-13 reserved   */
741
 
742
      /*
743
         word 88: UltraDMA section
744
         15-13: reserved
745
         12   : 1=UltraDMA Mode 4 is selected
746
         0=UltraDMA Mode 4 is not selected
747
         10   : 1=UltraDMA Mode 3 is selected
748
         0=UltraDMA Mode 3 is not selected
749
         10   : 1=UltraDMA Mode 2 is selected
750
         0=UltraDMA Mode 2 is not selected
751
         9   : 1=UltraDMA Mode 1 is selected
752
         0=UltraDMA Mode 1 is not selected
753
         8   : 1=UltraDMA Mode 0 is selected
754
         0=UltraDMA Mode 0 is not selected
755
         7-5 : Reserved
756
         4   : UltraDMA Mode 4 and below are supported
757
         3   : UltraDMA Mode 3 and below are supported
758
         2   : UltraDMA Mode 2 and below are supported
759
         1   : UltraDMA Mode 1 and below are supported
760
 
761
 
762
         Because the OCIDEC cores currently do not support
763
         UltraDMA we set all bits to zero
764
       */
765
      *buf++ = 0;
766
 
767
      /*
768
         word 89: Security sector erase unit completion time
769
 
770
         For now we report a 'value not specified'.
771
       */
772
      *buf++ = 0x0000;          // not really a FIXME
773
 
774
      /*
775
         word 90: Enhanced security erase completion time
776
 
777
         For now we report a 'value not specified'.
778
       */
779
      *buf++ = 0x0000;          // not really a FIXME
780
 
781
      /*
782
         word 91: Current advanced power management value
783
 
784
         For now set it to zero.
785
       */
786
      *buf++ = 0x0000;          // FIXME
787
 
788
      /*
789
         word 92: Master Password Revision Code.
790
         We simply report that we do not support this.
791
       */
792
      *buf++ = 0x0000;
793
 
794
      /*
795
         word 93: Hardware reset result
796
       */
797
      if (device->internals.dev)
798
        {
799
          /* this is device1, clear device0 bits                                         */
800
          *buf++ = 0 << 15 | 1 << 14 | 0 << 13 |  /* CBLIBD level (1=Vih, 0=Vil)               */
801
            /* 12-8 Device 1 hardware reset result       */
802
 
803
            device->sigs.pdiago << 11 | /* 1: Device1 did assert PDIAG               */
804
            /* 0: Device1 did not assert PDIAG           */
805
            3 << 9 |            /* Device1 determined device number by       */
806
            /* 00: reserved                              */
807
            /* 01: a jumper was used                     */
808
            /* 10: the CSEL signal was used              */
809
            /* 11: some other or unknown method was used */
810
            1 << 8              /* set to one                                */
811
            ;
812
        }
813
      else
814
        {                       /* FIXME bit 6 */
815
          /* this is device0, clear device1 bits                                         */
816
          *buf++ = 0 << 7 |      /* reserved                                  */
817
 
818
            /* 0: Device0 does not respond for device1   */
819
            device->sigs.daspi << 5 |   /* 1: Device0 did detected DASP assertion    */
820
            /* 0: Device0 did not detect DASP assertion  */
821
            device->sigs.pdiagi << 4 |  /* Device0 did detect PDIAG assertion        */
822
            /* Device0 did not detect PDIAG assertion    */
823
            1 << 3 |            /* Device0 did pass diagnostics              */
824
            3 << 1 |            /* Device0 determined device number by       */
825
            /* 00: reserved                              */
826
            /* 01: a jumper was used                     */
827
            /* 10: the CSEL signal was used              */
828
            /* 11: some other or unknown method was used */
829
            1 << 0               /* set to one                                */
830
            ;
831
        }
832
 
833
      /*
834
         word 94-126: Reserved
835
       */
836
      buf += 33;
837
 
838
      /*
839
         word 127: Removable Media Status Notification feature set support
840
         15-2: reserved
841
         1-0: 00 Removable media Status Notification not supported
842
         01 Removable media Status Notification supported
843
         10 reserved
844
         11 reserved
845
       */
846
      *buf++ = SUPPORT_REMOVABLE_MEDIA_NOTIFICATION;
847
 
848
      /*
849
         word 128: Security status
850
         15-9: reserved
851
         8   : Security level, 0=high, 1=maximum
852
         7-6 : reserved
853
         5   : 1=enhanced security erase supported
854
         4   : 1=security count expired
855
         3   : 1=security frozen
856
         2   : 1=security locked
857
         1   : 1=security enabled
858
 
859
 
860
         for now we do not support security, set is to zero
861
       */
862
      *buf++ = 0;
863
 
864
      /*
865
         word 129-159: Vendor specific
866
       */
867
      buf += 31;
868
 
869
      /*
870
         word 160: CFA power mode 1
871
         15  : Word 160 supported
872
         14  : reserved
873
         13  : CFA power mode 1 is required for one or more commands
874
         12  : CFA power mode 1 disabled
875
         11-0: Maximum current in mA
876
       */
877
      *buf++ = 0;
878
 
879
      /*
880
         word 161-175: Reserved for the CompactFLASH Association
881
       */
882
      buf += 15;
883
 
884
      /*
885
         word 176-254: reserved
886
       */
887
      buf += 79;
888
 
889
      /*
890
         word 255: Integrity word
891
         15-8: Checksum
892
         7-0 : Signature
893
       */
894
      // set signature to indicate valid checksum
895
      *buf = 0x00a5;
896
 
897
      // calculate checksum
898
      chksum = 0;
899
      for (n = 0; n < 511; n++)
900
        chksum += *chksum_buf++;
901
 
902
      *buf = ((0 - chksum) << 8) | 0x00a5;
903
 
904
      /* set status register bits                                       */
905
      device->regs.status = ATA_SR_DRDY | ATA_SR_DRQ;
906
    }
907
}
908
 
909
 
910
/*
911
  A T A _ I N I T I A L I Z E _ D E V I C E _ P A R A M E T E R S
912
*/
913
static void
914
ata_initialize_device_parameters_cmd (struct ata_device *device)
915
{
916
  device->internals.sectors_per_track = device->regs.sector_count;
917
  device->internals.heads_per_cylinder = device->regs.device_head & ATA_DHR_H;
918
 
919
  /* set status register bits                                         */
920
  device->regs.status = ATA_SR_BSY | ATA_SR_DRDY;
921
}
922
 
923
 
924
/*
925
  A T A _ R E A D _ S E C T O R S
926
*/
927
static void
928
ata_read_sectors_cmd (struct ata_device *device)
929
{
930
  size_t sector_count;
931
  uint32_t lba;
932
 
933
  /* check if this is a NON-PACKET device                             */
934
  if (device->conf.packet)
935
    {
936
      /*
937
         This is a PACKET device.
938
         Respond by placing PACKET Command feature set signature in block registers.
939
         Abort command.
940
       */
941
      ata_set_device_signature (device, 1);
942
      device->regs.status = ATA_SR_ERR | ATA_SR_DRDY;
943
      device->regs.error = ATA_ERR_ABT;
944
      return;
945
    }
946
 
947
  /* get the sector count                                           */
948
  if (device->regs.sector_count == 0)
949
    sector_count = 256;
950
  else
951
    sector_count = device->regs.sector_count;
952
 
953
  lba = ata_calc_lba (device);
954
 
955
  /* check if sector within bounds                                  */
956
  if (lba >= device->conf.size_sect)
957
    {
958
      /* invalid sector address                                     */
959
      /* set the status & error registers                           */
960
      device->regs.status = ATA_SR_DRDY | ATA_SR_ERR;
961
      device->regs.error = ATA_ERR_IDNF;
962
      return;
963
    }
964
 
965
  /* go ahead, read the bytestream                              */
966
  lba *= BYTES_PER_SECTOR;
967
 
968
  device->internals.lba = lba;
969
  device->internals.nr_sect = sector_count;
970
  device->internals.end_t_func = ata_read_sect;
971
 
972
  ata_read_sect (device);
973
}
974
 
975
 
976
 
977
/*
978
  A T A _ R E A D _ N A T I V E _ M A X _ A D D R
979
*/
980
static void
981
ata_read_native_max_addr (struct ata_device *dev)
982
{
983
  // FIXME: Left shift????
984
  ata_set_sect (dev, dev->conf.size << 11);
985
 
986
  dev->regs.status = ATA_SR_DRDY | ATA_SR_BSY;
987
}
988
 
989
/*
990
  A T A _ W R I T E _ S E C T O R S
991
*/
992
static void
993
ata_write_sectors (struct ata_device *dev)
994
{
995
  size_t sector_count;
996
  uint32_t lba;
997
 
998
  if (dev->conf.packet)
999
    {
1000
      ata_set_device_signature (dev, 1);
1001
      dev->regs.status = ATA_SR_ERR | ATA_SR_DRDY;
1002
      dev->regs.error = ATA_ERR_ABT;
1003
      return;
1004
    }
1005
 
1006
  /* get the sector count                                           */
1007
  if (dev->regs.sector_count == 0)
1008
    sector_count = 256;
1009
  else
1010
    sector_count = dev->regs.sector_count;
1011
 
1012
  lba = ata_calc_lba (dev);
1013
 
1014
  /* check if sector within bounds                                  */
1015
  if (lba >= dev->conf.size_sect)
1016
    {
1017
      /* invalid sector address                                     */
1018
      /* set the status & error registers                           */
1019
      dev->regs.status = ATA_SR_DRDY | ATA_SR_ERR;
1020
      dev->regs.error = ATA_ERR_IDNF;
1021
      return;
1022
    }
1023
 
1024
  dev->internals.lba = lba * BYTES_PER_SECTOR;
1025
  dev->internals.nr_sect = sector_count;
1026
 
1027
  dev->internals.dbuf_cnt = BYTES_PER_SECTOR / 2;       //Words, not bytes
1028
  dev->internals.dbuf_ptr = dev->internals.dbuf;
1029
 
1030
  dev->regs.status = ATA_SR_BSY | ATA_SR_DRDY | ATA_SR_DRQ;
1031
  dev->internals.end_t_func = ata_write_sect;
1032
}
1033
 
1034
/*
1035
  A T A _ S E T _ F E A T U R E S
1036
*/
1037
static void
1038
ata_set_features (struct ata_device *dev)
1039
{
1040
  switch (dev->regs.features)
1041
    {
1042
    case SET_TRANSFER_MODE_SECTOR_COUNT_REG:
1043
      /* FIXME: I don't have a clue as to what to do with this... */
1044
      switch (dev->regs.sector_count >> 3)
1045
        {
1046
        case 0:          /* Set default */
1047
          break;
1048
        case 1:         /* Set PIO mode */
1049
          break;
1050
        case 4:         /* Set DMA mode */
1051
          /* FIXME */
1052
          fprintf (stderr, "Fixme: Set DMA mode to %d\n",
1053
                   dev->regs.sector_count & 0x7);
1054
          break;
1055
        case 8:         /* Set UDMA mode */
1056
          /* FIXME */
1057
          fprintf (stderr, "Fixme: Set UDMA mode to %d\n",
1058
                   dev->regs.sector_count & 0x7);
1059
          break;
1060
        }
1061
      dev->regs.status = ATA_SR_DSC | ATA_SR_DRDY | ATA_SR_BSY;
1062
      break;
1063
    default:
1064
      dev->regs.error = ATA_ERR_ABT;
1065
      dev->regs.status = ATA_SR_ERR | ATA_SR_BSY | ATA_SR_DRDY;
1066
      /* FIXME */
1067
      fprintf (stderr, "Fixme: Unknown set features sub-command %x\n",
1068
               dev->regs.features);
1069
    }
1070
}
1071
 
1072
 
1073
int
1074
ata_device_execute_cmd (struct ata_device *device)
1075
{
1076
  /* execute the commands */
1077
  switch (device->regs.command)
1078
    {
1079
    case DEVICE_RESET:
1080
      ata_device_reset_cmd (device);
1081
      SCHED_ADD (ata_cmd_complete, device, 4000000);
1082
      return 0;
1083
 
1084
    case EXECUTE_DEVICE_DIAGNOSTICS:
1085
      ata_execute_device_diagnostics_cmd (device);
1086
      SCHED_ADD (ata_cmd_complete, device, 4000000);
1087
      return 0;
1088
 
1089
    case IDENTIFY_DEVICE:
1090
      ata_identify_device_cmd (device);
1091
      /* FIXME: This should generate an interrupt, but linux is abit broken so
1092
       * it doesn't happen */
1093
      //SCHED_ADD(ata_cmd_complete, device, 40000);
1094
      return 0;
1095
 
1096
    case INITIALIZE_DEVICE_PARAMETERS:
1097
      ata_initialize_device_parameters_cmd (device);
1098
      SCHED_ADD (ata_cmd_complete, device, 400000);
1099
      return 0;
1100
 
1101
    case READ_SECTORS:
1102
      ata_read_sectors_cmd (device);
1103
      /* PIO-in command -- Doesn't interrupt on completion */
1104
      return 0;
1105
 
1106
    case READ_NATIVE_MAX_ADDRESS:
1107
      ata_read_native_max_addr (device);
1108
      SCHED_ADD (ata_cmd_complete, device, 40000);
1109
      return 0;
1110
 
1111
    case WRITE_SECTORS:
1112
      ata_write_sectors (device);
1113
      SCHED_ADD (ata_cmd_complete, device, 40000);
1114
      return 0;
1115
 
1116
    case SET_FEATURES:
1117
      ata_set_features (device);
1118
      SCHED_ADD (ata_cmd_complete, device, 4000);
1119
      return 0;
1120
 
1121
    default:
1122
      device->regs.error = ATA_ERR_ABT;
1123
      device->regs.status = ATA_SR_ERR | ATA_SR_BSY | ATA_SR_DRDY;
1124
      /* FIXME */
1125
      fprintf (stderr, "Fixme: Unknown command %x\n", device->regs.command);
1126
      SCHED_ADD (ata_cmd_complete, device, 40000);
1127
      return -1;
1128
    }
1129
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.