]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_sata.c
Add driver for S-ATA-controller on Intel processors with South
[karo-tx-uboot.git] / common / cmd_sata.c
1 /*
2   * Copyright (C) Procsys. All rights reserved.
3   * Author: Mushtaq Khan <mushtaq_k@procsys.com>
4                         <mushtaqk_921@yahoo.co.in>
5   *
6   *
7   * This program is free software; you can redistribute it and/or
8   * modify it under the terms of the GNU General Public License as
9   * published by the Free Software Foundation; either version 2 of
10   * the License, or (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20   * MA 02111-1307 USA
21   *
22   * with the reference to libata in kernel 2.4.32
23   *
24 */
25
26 /*File contains SATA read-write and other utility functions.*/
27 #include <common.h>
28 #include <asm/io.h>
29 #include <pci.h>
30 #include <command.h>
31 #include <config.h>
32 #include <ide.h>
33 #include <ata.h>
34
35 #ifdef CFG_SATA_SUPPORTED
36 /*For debug prints set macro DEBUG_SATA to 1 */
37 #define DEBUG_SATA 0
38 /*Macro for SATA library specific declarations */
39 #define SATA_DECL
40 #include <sata.h>
41 #undef SATA_DECL
42
43 static u8 __inline__
44 sata_inb (unsigned long ioaddr)
45 {
46         return inb (ioaddr);
47 }
48
49 static void __inline__
50 sata_outb (unsigned char val, unsigned long ioaddr)
51 {
52         outb (val, ioaddr);
53 }
54
55 static void
56 output_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
57 {
58         outsw (ioaddr->data_addr, sect_buf, words << 1);
59 }
60
61 static int
62 input_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
63 {
64         insw (ioaddr->data_addr, sect_buf, words << 1);
65         return 0;
66 }
67
68 static void
69 sata_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
70 {
71         unsigned char *end, *last;
72
73         last = dst;
74         end = src + len - 1;
75
76         /* reserve space for '\0' */
77         if (len < 2)
78                 goto OUT;
79
80         /* skip leading white space */
81         while ((*src) && (src < end) && (*src == ' '))
82                 ++src;
83
84         /* copy string, omitting trailing white space */
85         while ((*src) && (src < end)) {
86                 *dst++ = *src;
87                 if (*src++ != ' ')
88                         last = dst;
89         }
90       OUT:
91         *last = '\0';
92 }
93
94 int
95 sata_bus_softreset (int num)
96 {
97         u8 dev = 0, status = 0, i;
98
99         port[num].dev_mask = 0;
100
101         for (i = 0; i < CFG_SATA_DEVS_PER_BUS; i++) {
102                 if (!(sata_devchk (&port[num].ioaddr, i))) {
103                         PRINTF ("dev_chk failed for dev#%d\n", i);
104                 } else {
105                         port[num].dev_mask |= (1 << i);
106                         PRINTF ("dev_chk passed for dev#%d\n", i);
107                 }
108         }
109
110         if (!(port[num].dev_mask)) {
111                 printf ("no devices on port%d\n", num);
112                 return 1;
113         }
114
115         dev_select (&port[num].ioaddr, dev);
116
117         port[num].ctl_reg = 0x08;       /*Default value of control reg */
118         sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
119         udelay (10);
120         sata_outb (port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr);
121         udelay (10);
122         sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
123
124         /* spec mandates ">= 2ms" before checking status.
125          * We wait 150ms, because that was the magic delay used for
126          * ATAPI devices in Hale Landis's ATADRVR, for the period of time
127          * between when the ATA command register is written, and then
128          * status is checked.  Because waiting for "a while" before
129          * checking status is fine, post SRST, we perform this magic
130          * delay here as well.
131          */
132         msleep (150);
133         status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 300);
134         while ((status & ATA_BUSY)) {
135                 msleep (100);
136                 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 3);
137         }
138
139         if (status & ATA_BUSY)
140                 printf ("ata%u is slow to respond,plz be patient\n", port);
141
142         while ((status & ATA_BUSY)) {
143                 msleep (100);
144                 status = sata_chk_status (&port[num].ioaddr);
145         }
146
147         if (status & ATA_BUSY) {
148                 printf ("ata%u failed to respond : ", port);
149                 printf ("bus reset failed\n");
150                 return 1;
151         }
152         return 0;
153 }
154
155 void
156 sata_identify (int num, int dev)
157 {
158         u8 cmd = 0, status = 0, devno = num * CFG_SATA_DEVS_PER_BUS + dev;
159         u16 iobuf[ATA_SECT_SIZE];
160         u64 n_sectors = 0;
161         u8 mask = 0;
162
163         memset (iobuf, 0, sizeof (iobuf));
164         hd_driveid_t *iop = (hd_driveid_t *) iobuf;
165
166         if (dev == 0)
167                 mask = 0x01;
168         else
169                 mask = 0x02;
170
171         if (!(port[num].dev_mask & mask)) {
172                 printf ("dev%d is not present on port#%d\n", dev, num);
173                 return;
174         }
175
176         printf ("port=%d dev=%d\n", num, dev);
177
178         dev_select (&port[num].ioaddr, dev);
179
180         status = 0;
181         cmd = ATA_CMD_IDENT;    /*Device Identify Command */
182         sata_outb (cmd, port[num].ioaddr.command_addr);
183         sata_inb (port[num].ioaddr.altstatus_addr);
184         udelay (10);
185
186         status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 1000);
187         if (status & ATA_ERR) {
188                 printf ("\ndevice not responding\n");
189                 port[num].dev_mask &= ~mask;
190                 return;
191         }
192
193         input_data (&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS);
194
195         PRINTF ("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x"
196                 "86:%04x" "87:%04x 88:%04x\n", num, dev, iobuf[49],
197                 iobuf[82], iobuf[83], iobuf[84], iobuf[85], iobuf[86],
198                 iobuf[87], iobuf[88]);
199
200         /* we require LBA and DMA support (bits 8 & 9 of word 49) */
201         if (!ata_id_has_dma (iobuf) || !ata_id_has_lba (iobuf)) {
202                 PRINTF ("ata%u: no dma/lba\n", num);
203         }
204         ata_dump_id (iobuf);
205
206         if (ata_id_has_lba48 (iobuf)) {
207                 n_sectors = ata_id_u64 (iobuf, 100);
208         } else {
209                 n_sectors = ata_id_u32 (iobuf, 60);
210         }
211         PRINTF ("no. of sectors %u\n", ata_id_u64 (iobuf, 100));
212         PRINTF ("no. of sectors %u\n", ata_id_u32 (iobuf, 60));
213
214         if (n_sectors == 0) {
215                 port[num].dev_mask &= ~mask;
216                 return;
217         }
218
219         sata_cpy (sata_dev_desc[devno].revision, iop->fw_rev,
220                   sizeof (sata_dev_desc[devno].revision));
221         sata_cpy (sata_dev_desc[devno].vendor, iop->model,
222                   sizeof (sata_dev_desc[devno].vendor));
223         sata_cpy (sata_dev_desc[devno].product, iop->serial_no,
224                   sizeof (sata_dev_desc[devno].product));
225         strswab (sata_dev_desc[devno].revision);
226         strswab (sata_dev_desc[devno].vendor);
227
228         if ((iop->config & 0x0080) == 0x0080) {
229                 sata_dev_desc[devno].removable = 1;
230         } else {
231                 sata_dev_desc[devno].removable = 0;
232         }
233
234         sata_dev_desc[devno].lba = iop->lba_capacity;
235         PRINTF ("lba=0x%x", sata_dev_desc[devno].lba);
236
237 #ifdef CONFIG_LBA48
238         if (iop->command_set_2 & 0x0400) {
239                 sata_dev_desc[devno].lba48 = 1;
240                 lba = (unsigned long long) iop->lba48_capacity[0] |
241                     ((unsigned long long) iop->lba48_capacity[1] << 16) |
242                     ((unsigned long long) iop->lba48_capacity[2] << 32) |
243                     ((unsigned long long) iop->lba48_capacity[3] << 48);
244         } else {
245                 sata_dev_desc[devno].lba48 = 0;
246         }
247 #endif
248
249         /* assuming HD */
250         sata_dev_desc[devno].type = DEV_TYPE_HARDDISK;
251         sata_dev_desc[devno].blksz = ATA_BLOCKSIZE;
252         sata_dev_desc[devno].lun = 0;   /* just to fill something in... */
253 }
254
255 void
256 set_Feature_cmd (int num, int dev)
257 {
258         u8 mask = 0x00, status = 0;
259
260         if (dev == 0)
261                 mask = 0x01;
262         else
263                 mask = 0x02;
264
265         if (!(port[num].dev_mask & mask)) {
266                 PRINTF ("dev%d is not present on port#%d\n", dev, num);
267                 return;
268         }
269
270         dev_select (&port[num].ioaddr, dev);
271
272         sata_outb (SETFEATURES_XFER, port[num].ioaddr.feature_addr);
273         sata_outb (XFER_PIO_4, port[num].ioaddr.nsect_addr);
274         sata_outb (0, port[num].ioaddr.lbal_addr);
275         sata_outb (0, port[num].ioaddr.lbam_addr);
276         sata_outb (0, port[num].ioaddr.lbah_addr);
277
278         sata_outb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
279         sata_outb (ATA_CMD_SETF, port[num].ioaddr.command_addr);
280
281         udelay (50);
282         msleep (150);
283
284         status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 5000);
285         if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) {
286                 printf ("Error  : status 0x%02x\n", status);
287                 port[num].dev_mask &= ~mask;
288         }
289 }
290
291 void
292 sata_port (struct sata_ioports *ioport)
293 {
294         ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
295         ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
296         ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
297         ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
298         ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
299         ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
300         ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
301         ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
302         ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
303         ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
304 }
305
306 int
307 sata_devchk (struct sata_ioports *ioaddr, int dev)
308 {
309         u8 nsect, lbal;
310
311         dev_select (ioaddr, dev);
312
313         sata_outb (0x55, ioaddr->nsect_addr);
314         sata_outb (0xaa, ioaddr->lbal_addr);
315
316         sata_outb (0xaa, ioaddr->nsect_addr);
317         sata_outb (0x55, ioaddr->lbal_addr);
318
319         sata_outb (0x55, ioaddr->nsect_addr);
320         sata_outb (0xaa, ioaddr->lbal_addr);
321
322         nsect = sata_inb (ioaddr->nsect_addr);
323         lbal = sata_inb (ioaddr->lbal_addr);
324
325         if ((nsect == 0x55) && (lbal == 0xaa))
326                 return 1;       /* we found a device */
327         else
328                 return 0;       /* nothing found */
329 }
330
331 void
332 dev_select (struct sata_ioports *ioaddr, int dev)
333 {
334         u8 tmp = 0;
335
336         if (dev == 0)
337                 tmp = ATA_DEVICE_OBS;
338         else
339                 tmp = ATA_DEVICE_OBS | ATA_DEV1;
340
341         sata_outb (tmp, ioaddr->device_addr);
342         sata_inb (ioaddr->altstatus_addr);
343         udelay (5);
344 }
345
346 u8
347 sata_busy_wait (struct sata_ioports *ioaddr, int bits, unsigned int max)
348 {
349         u8 status;
350
351         do {
352                 udelay (1000);
353                 status = sata_chk_status (ioaddr);
354                 max--;
355         } while ((status & bits) && (max > 0));
356
357         return status;
358 }
359
360 u8
361 sata_chk_status (struct sata_ioports * ioaddr)
362 {
363         return sata_inb (ioaddr->status_addr);
364 }
365
366 void
367 msleep (int count)
368 {
369         int i;
370
371         for (i = 0; i < count; i++)
372                 udelay (1000);
373 }
374
375 ulong
376 sata_read (int device, lbaint_t blknr, ulong blkcnt, ulong * buffer)
377 {
378         ulong n = 0;
379         u8 dev = 0, num = 0, mask = 0, status = 0;
380
381 #ifdef CONFIG_LBA48
382         unsigned char lba48 = 0;
383
384         if (blknr & 0x0000fffff0000000) {
385                 if (!sata_dev_desc[devno].lba48) {
386                         printf ("Drive doesn't support 48-bit addressing\n");
387                         return 0;
388                 }
389                 /* more than 28 bits used, use 48bit mode */
390                 lba48 = 1;
391         }
392 #endif
393         /*Port Number */
394         num = device / CFG_SATA_DEVS_PER_BUS;
395         /*dev on the port */
396         if (device >= CFG_SATA_DEVS_PER_BUS)
397                 dev = device - CFG_SATA_DEVS_PER_BUS;
398         else
399                 dev = device;
400
401         if (dev == 0)
402                 mask = 0x01;
403         else
404                 mask = 0x02;
405
406         if (!(port[num].dev_mask & mask)) {
407                 printf ("dev%d is not present on port#%d\n", dev, num);
408                 return 0;
409         }
410
411         /* Select device */
412         dev_select (&port[num].ioaddr, dev);
413
414         status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
415         if (status & ATA_BUSY) {
416                 printf ("ata%u failed to respond\n", port[num].port_no);
417                 return n;
418         }
419         while (blkcnt-- > 0) {
420                 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
421                 if (status & ATA_BUSY) {
422                         printf ("ata%u failed to respond\n", 0);
423                         return n;
424                 }
425 #ifdef CONFIG_LBA48
426                 if (lba48) {
427                         /* write high bits */
428                         sata_outb (0, port[num].ioaddr.nsect_addr);
429                         sata_outb ((blknr >> 24) & 0xFF,
430                                    port[num].ioaddr.lbal_addr);
431                         sata_outb ((blknr >> 32) & 0xFF,
432                                    port[num].ioaddr.lbam_addr);
433                         sata_outb ((blknr >> 40) & 0xFF,
434                                    port[num].ioaddr.lbah_addr);
435                 }
436 #endif
437                 sata_outb (1, port[num].ioaddr.nsect_addr);
438                 sata_outb (((blknr) >> 0) & 0xFF,
439                            port[num].ioaddr.lbal_addr);
440                 sata_outb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
441                 sata_outb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
442
443 #ifdef CONFIG_LBA48
444                 if (lba48) {
445                         sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
446                         sata_outb (ATA_CMD_READ_EXT,
447                                    port[num].ioaddr.command_addr);
448                 } else
449 #endif
450                 {
451                         sata_outb (ATA_LBA | ((blknr >> 24) & 0xF),
452                                    port[num].ioaddr.device_addr);
453                         sata_outb (ATA_CMD_READ,
454                                    port[num].ioaddr.command_addr);
455                 }
456
457                 msleep (50);
458                 /*may take up to 4 sec */
459                 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
460
461                 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
462                     != ATA_STAT_DRQ) {
463                         u8 err = 0;
464
465                         printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
466                                 device, (ulong) blknr, status);
467                         err = sata_inb (port[num].ioaddr.error_addr);
468                         printf ("Error reg = 0x%x\n", err);
469                         return (n);
470                 }
471                 input_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
472                 sata_inb (port[num].ioaddr.altstatus_addr);
473                 udelay (50);
474
475                 ++n;
476                 ++blknr;
477                 buffer += ATA_SECTORWORDS;
478         }
479         return n;
480 }
481
482 ulong
483 sata_write (int device, lbaint_t blknr, ulong blkcnt, ulong * buffer)
484 {
485         ulong n = 0;
486         unsigned char status = 0, num = 0, dev = 0, mask = 0;
487
488 #ifdef CONFIG_LBA48
489         unsigned char lba48 = 0;
490
491         if (blknr & 0x0000fffff0000000) {
492                 if (!sata_dev_desc[devno].lba48) {
493                         printf ("Drive doesn't support 48-bit addressing\n");
494                         return 0;
495                 }
496                 /* more than 28 bits used, use 48bit mode */
497                 lba48 = 1;
498         }
499 #endif
500         /*Port Number */
501         num = device / CFG_SATA_DEVS_PER_BUS;
502         /*dev on the Port */
503         if (device >= CFG_SATA_DEVS_PER_BUS)
504                 dev = device - CFG_SATA_DEVS_PER_BUS;
505         else
506                 dev = device;
507
508         if (dev == 0)
509                 mask = 0x01;
510         else
511                 mask = 0x02;
512
513         /* Select device */
514         dev_select (&port[num].ioaddr, dev);
515
516         status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
517         if (status & ATA_BUSY) {
518                 printf ("ata%u failed to respond\n", port[num].port_no);
519                 return n;
520         }
521
522         while (blkcnt-- > 0) {
523                 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
524                 if (status & ATA_BUSY) {
525                         printf ("ata%u failed to respond\n",
526                                 port[num].port_no);
527                         return n;
528                 }
529 #ifdef CONFIG_LBA48
530                 if (lba48) {
531                         /* write high bits */
532                         sata_outb (0, port[num].ioaddr.nsect_addr);
533                         sata_outb ((blknr >> 24) & 0xFF,
534                                    port[num].ioaddr.lbal_addr);
535                         sata_outb ((blknr >> 32) & 0xFF,
536                                    port[num].ioaddr.lbam_addr);
537                         sata_outb ((blknr >> 40) & 0xFF,
538                                    port[num].ioaddr.lbah_addr);
539                 }
540 #endif
541                 sata_outb (1, port[num].ioaddr.nsect_addr);
542                 sata_outb ((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
543                 sata_outb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
544                 sata_outb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
545 #ifdef CONFIG_LBA48
546                 if (lba48) {
547                         sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
548                         sata_outb (ATA_CMD_WRITE_EXT,
549                                    port[num].ioaddr.command_addr);
550                 } else
551 #endif
552                 {
553                         sata_outb (ATA_LBA | ((blknr >> 24) & 0xF),
554                                    port[num].ioaddr.device_addr);
555                         sata_outb (ATA_CMD_WRITE,
556                                    port[num].ioaddr.command_addr);
557                 }
558
559                 msleep (50);
560                 /*may take up to 4 sec */
561                 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
562                 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
563                     != ATA_STAT_DRQ) {
564                         printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
565                                 device, (ulong) blknr, status);
566                         return (n);
567                 }
568
569                 output_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
570                 sata_inb (port[num].ioaddr.altstatus_addr);
571                 udelay (50);
572
573                 ++n;
574                 ++blknr;
575                 buffer += ATA_SECTORWORDS;
576         }
577         return n;
578 }
579
580 block_dev_desc_t *sata_get_dev (int dev);
581
582 block_dev_desc_t *
583 sata_get_dev (int dev)
584 {
585         return ((block_dev_desc_t *) & sata_dev_desc[dev]);
586 }
587
588 int
589 do_sata (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
590 {
591
592         switch (argc) {
593         case 0:
594         case 1:
595                 printf ("Usage:\n%s\n", cmdtp->usage);
596                 return 1;
597         case 2:
598                 if (strncmp (argv[1], "init", 4) == 0) {
599                         int rcode = 0;
600
601                         rcode = init_sata ();
602                         if (rcode)
603                                 printf ("Sata initialization Failed\n");
604                         return rcode;
605                 } else if (strncmp (argv[1], "inf", 3) == 0) {
606                         int i;
607
608                         putc ('\n');
609                         for (i = 0; i < CFG_SATA_MAXDEVICES; ++i) {
610                                 /*List only known devices */
611                                 if (sata_dev_desc[i].type ==
612                                     DEV_TYPE_UNKNOWN)
613                                         continue;
614                                 printf ("sata dev %d: ", i);
615                                 dev_print (&sata_dev_desc[i]);
616                         }
617                         return 0;
618                 }
619                 printf ("Usage:\n%s\n", cmdtp->usage);
620                 return 1;
621         case 3:
622                 if (strcmp (argv[1], "dev") == 0) {
623                         int dev = (int) simple_strtoul (argv[2], NULL, 10);
624
625                         if (dev >= CFG_SATA_MAXDEVICES) {
626                                 printf ("\nSata dev %d not available\n",
627                                         dev);
628                                 return 1;
629                         }
630                         printf ("\nSATA dev %d: ", dev);
631                         dev_print (&sata_dev_desc[dev]);
632                         if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
633                                 return 1;
634                         curr_dev = dev;
635                         return 0;
636                 } else if (strcmp (argv[1], "part") == 0) {
637                         int dev = (int) simple_strtoul (argv[2], NULL, 10);
638
639                         if (dev >= CFG_SATA_MAXDEVICES) {
640                                 printf ("\nSata dev %d not available\n",
641                                         dev);
642                                 return 1;
643                         }
644                         PRINTF ("\nSATA dev %d: ", dev);
645                         if (sata_dev_desc[dev].part_type !=
646                             PART_TYPE_UNKNOWN) {
647                                 print_part (&sata_dev_desc[dev]);
648                         } else {
649                                 printf ("\nSata dev %d partition type "
650                                         "unknown\n", dev);
651                                 return 1;
652                         }
653                         return 0;
654                 }
655                 printf ("Usage:\n%s\n", cmdtp->usage);
656                 return 1;
657         default:
658                 if (argc < 5) {
659                         printf ("Usage:\n%s\n", cmdtp->usage);
660                         return 1;
661                 }
662                 if (strcmp (argv[1], "read") == 0) {
663                         ulong addr = simple_strtoul (argv[2], NULL, 16);
664                         ulong cnt = simple_strtoul (argv[4], NULL, 16);
665                         ulong n;
666                         lbaint_t blk = simple_strtoul (argv[3], NULL, 16);
667
668                         memset ((int *) addr, 0, cnt * 512);
669                         printf ("\nSATA read: dev %d blk # %ld,"
670                                 "count %ld ... ", curr_dev, blk, cnt);
671                         n = sata_read (curr_dev, blk, cnt, (ulong *) addr);
672                         /* flush cache after read */
673                         flush_cache (addr, cnt * 512);
674                         printf ("%ld blocks read: %s\n", n,
675                                 (n == cnt) ? "OK" : "ERR");
676                         if (n == cnt)
677                                 return 1;
678                         else
679                                 return 0;
680                 } else if (strcmp (argv[1], "write") == 0) {
681                         ulong addr = simple_strtoul (argv[2], NULL, 16);
682                         ulong cnt = simple_strtoul (argv[4], NULL, 16);
683                         ulong n;
684                         lbaint_t blk = simple_strtoul (argv[3], NULL, 16);
685
686                         printf ("\nSata write: dev %d blk # %ld,"
687                                 "count %ld ... ", curr_dev, blk, cnt);
688                         n = sata_write (curr_dev, blk, cnt, (ulong *) addr);
689                         printf ("%ld blocks written: %s\n", n,
690                                 (n == cnt) ? "OK" : "ERR");
691                         if (n == cnt)
692                                 return 1;
693                         else
694                                 return 0;
695                 } else {
696                         printf ("Usage:\n%s\n", cmdtp->usage);
697                         return 1;
698                 }
699         }                       /*End OF SWITCH */
700 }
701
702 U_BOOT_CMD (sata, 5, 1, do_sata,
703             "sata init\n"
704             "sata info\n"
705             "sata part device\n"
706             "sata dev device\n"
707             "sata read  addr blk# cnt\n"
708             "sata write  addr blk# cnt\n", "cmd for init,rw and dev-info\n");
709
710 #endif