OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

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

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

powered by: WebSVN 2.1.0

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