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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc1/] [or1ksim/] [peripheral/] [atadevice-cmdi.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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