2 * Copyright (C) Procsys. All rights reserved.
3 * Author: Mushtaq Khan <mushtaq_k@procsys.com>
4 * <mushtaqk_921@yahoo.co.in>
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.
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.
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,
22 * with the reference to libata in kernel 2.4.32
27 * File contains SATA read-write and other utility functions.
37 #ifdef CFG_SATA_SUPPORTED
38 /*For debug prints set macro DEBUG_SATA to 1 */
40 /*Macro for SATA library specific declarations */
46 sata_inb (unsigned long ioaddr)
51 static void __inline__
52 sata_outb (unsigned char val, unsigned long ioaddr)
58 output_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
60 outsw (ioaddr->data_addr, sect_buf, words << 1);
64 input_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
66 insw (ioaddr->data_addr, sect_buf, words << 1);
71 sata_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
73 unsigned char *end, *last;
78 /* reserve space for '\0' */
82 /* skip leading white space */
83 while ((*src) && (src < end) && (*src == ' '))
86 /* copy string, omitting trailing white space */
87 while ((*src) && (src < end)) {
97 sata_bus_softreset (int num)
99 u8 dev = 0, status = 0, i;
101 port[num].dev_mask = 0;
103 for (i = 0; i < CFG_SATA_DEVS_PER_BUS; i++) {
104 if (!(sata_devchk (&port[num].ioaddr, i))) {
105 PRINTF ("dev_chk failed for dev#%d\n", i);
107 port[num].dev_mask |= (1 << i);
108 PRINTF ("dev_chk passed for dev#%d\n", i);
112 if (!(port[num].dev_mask)) {
113 printf ("no devices on port%d\n", num);
117 dev_select (&port[num].ioaddr, dev);
119 port[num].ctl_reg = 0x08; /*Default value of control reg */
120 sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
122 sata_outb (port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr);
124 sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
126 /* spec mandates ">= 2ms" before checking status.
127 * We wait 150ms, because that was the magic delay used for
128 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
129 * between when the ATA command register is written, and then
130 * status is checked. Because waiting for "a while" before
131 * checking status is fine, post SRST, we perform this magic
132 * delay here as well.
135 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 300);
136 while ((status & ATA_BUSY)) {
138 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 3);
141 if (status & ATA_BUSY)
142 printf ("ata%u is slow to respond,plz be patient\n", port);
144 while ((status & ATA_BUSY)) {
146 status = sata_chk_status (&port[num].ioaddr);
149 if (status & ATA_BUSY) {
150 printf ("ata%u failed to respond : ", port);
151 printf ("bus reset failed\n");
158 sata_identify (int num, int dev)
160 u8 cmd = 0, status = 0, devno = num * CFG_SATA_DEVS_PER_BUS + dev;
161 u16 iobuf[ATA_SECT_SIZE];
165 memset (iobuf, 0, sizeof (iobuf));
166 hd_driveid_t *iop = (hd_driveid_t *) iobuf;
173 if (!(port[num].dev_mask & mask)) {
174 printf ("dev%d is not present on port#%d\n", dev, num);
178 printf ("port=%d dev=%d\n", num, dev);
180 dev_select (&port[num].ioaddr, dev);
183 cmd = ATA_CMD_IDENT; /*Device Identify Command */
184 sata_outb (cmd, port[num].ioaddr.command_addr);
185 sata_inb (port[num].ioaddr.altstatus_addr);
188 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 1000);
189 if (status & ATA_ERR) {
190 printf ("\ndevice not responding\n");
191 port[num].dev_mask &= ~mask;
195 input_data (&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS);
197 PRINTF ("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x"
198 "86:%04x" "87:%04x 88:%04x\n", num, dev, iobuf[49],
199 iobuf[82], iobuf[83], iobuf[84], iobuf[85], iobuf[86],
200 iobuf[87], iobuf[88]);
202 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
203 if (!ata_id_has_dma (iobuf) || !ata_id_has_lba (iobuf)) {
204 PRINTF ("ata%u: no dma/lba\n", num);
208 if (ata_id_has_lba48 (iobuf)) {
209 n_sectors = ata_id_u64 (iobuf, 100);
211 n_sectors = ata_id_u32 (iobuf, 60);
213 PRINTF ("no. of sectors %u\n", ata_id_u64 (iobuf, 100));
214 PRINTF ("no. of sectors %u\n", ata_id_u32 (iobuf, 60));
216 if (n_sectors == 0) {
217 port[num].dev_mask &= ~mask;
221 sata_cpy (sata_dev_desc[devno].revision, iop->fw_rev,
222 sizeof (sata_dev_desc[devno].revision));
223 sata_cpy (sata_dev_desc[devno].vendor, iop->model,
224 sizeof (sata_dev_desc[devno].vendor));
225 sata_cpy (sata_dev_desc[devno].product, iop->serial_no,
226 sizeof (sata_dev_desc[devno].product));
227 strswab (sata_dev_desc[devno].revision);
228 strswab (sata_dev_desc[devno].vendor);
230 if ((iop->config & 0x0080) == 0x0080) {
231 sata_dev_desc[devno].removable = 1;
233 sata_dev_desc[devno].removable = 0;
236 sata_dev_desc[devno].lba = iop->lba_capacity;
237 PRINTF ("lba=0x%x", sata_dev_desc[devno].lba);
240 if (iop->command_set_2 & 0x0400) {
241 sata_dev_desc[devno].lba48 = 1;
242 lba = (unsigned long long) iop->lba48_capacity[0] |
243 ((unsigned long long) iop->lba48_capacity[1] << 16) |
244 ((unsigned long long) iop->lba48_capacity[2] << 32) |
245 ((unsigned long long) iop->lba48_capacity[3] << 48);
247 sata_dev_desc[devno].lba48 = 0;
252 sata_dev_desc[devno].type = DEV_TYPE_HARDDISK;
253 sata_dev_desc[devno].blksz = ATA_BLOCKSIZE;
254 sata_dev_desc[devno].lun = 0; /* just to fill something in... */
258 set_Feature_cmd (int num, int dev)
260 u8 mask = 0x00, status = 0;
267 if (!(port[num].dev_mask & mask)) {
268 PRINTF ("dev%d is not present on port#%d\n", dev, num);
272 dev_select (&port[num].ioaddr, dev);
274 sata_outb (SETFEATURES_XFER, port[num].ioaddr.feature_addr);
275 sata_outb (XFER_PIO_4, port[num].ioaddr.nsect_addr);
276 sata_outb (0, port[num].ioaddr.lbal_addr);
277 sata_outb (0, port[num].ioaddr.lbam_addr);
278 sata_outb (0, port[num].ioaddr.lbah_addr);
280 sata_outb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
281 sata_outb (ATA_CMD_SETF, port[num].ioaddr.command_addr);
286 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 5000);
287 if ((status & (ATA_STAT_BUSY | ATA_STAT_ERR))) {
288 printf ("Error : status 0x%02x\n", status);
289 port[num].dev_mask &= ~mask;
294 sata_port (struct sata_ioports *ioport)
296 ioport->data_addr = ioport->cmd_addr + ATA_REG_DATA;
297 ioport->error_addr = ioport->cmd_addr + ATA_REG_ERR;
298 ioport->feature_addr = ioport->cmd_addr + ATA_REG_FEATURE;
299 ioport->nsect_addr = ioport->cmd_addr + ATA_REG_NSECT;
300 ioport->lbal_addr = ioport->cmd_addr + ATA_REG_LBAL;
301 ioport->lbam_addr = ioport->cmd_addr + ATA_REG_LBAM;
302 ioport->lbah_addr = ioport->cmd_addr + ATA_REG_LBAH;
303 ioport->device_addr = ioport->cmd_addr + ATA_REG_DEVICE;
304 ioport->status_addr = ioport->cmd_addr + ATA_REG_STATUS;
305 ioport->command_addr = ioport->cmd_addr + ATA_REG_CMD;
309 sata_devchk (struct sata_ioports *ioaddr, int dev)
313 dev_select (ioaddr, dev);
315 sata_outb (0x55, ioaddr->nsect_addr);
316 sata_outb (0xaa, ioaddr->lbal_addr);
318 sata_outb (0xaa, ioaddr->nsect_addr);
319 sata_outb (0x55, ioaddr->lbal_addr);
321 sata_outb (0x55, ioaddr->nsect_addr);
322 sata_outb (0xaa, ioaddr->lbal_addr);
324 nsect = sata_inb (ioaddr->nsect_addr);
325 lbal = sata_inb (ioaddr->lbal_addr);
327 if ((nsect == 0x55) && (lbal == 0xaa))
328 return 1; /* we found a device */
330 return 0; /* nothing found */
334 dev_select (struct sata_ioports *ioaddr, int dev)
339 tmp = ATA_DEVICE_OBS;
341 tmp = ATA_DEVICE_OBS | ATA_DEV1;
343 sata_outb (tmp, ioaddr->device_addr);
344 sata_inb (ioaddr->altstatus_addr);
349 sata_busy_wait (struct sata_ioports *ioaddr, int bits, unsigned int max)
355 status = sata_chk_status (ioaddr);
357 } while ((status & bits) && (max > 0));
363 sata_chk_status (struct sata_ioports * ioaddr)
365 return sata_inb (ioaddr->status_addr);
373 for (i = 0; i < count; i++)
378 sata_read (int device, ulong blknr,lbaint_t blkcnt, void * buff)
380 ulong n = 0, *buffer = (ulong *)buff;
381 u8 dev = 0, num = 0, mask = 0, status = 0;
384 unsigned char lba48 = 0;
386 if (blknr & 0x0000fffff0000000) {
387 if (!sata_dev_desc[devno].lba48) {
388 printf ("Drive doesn't support 48-bit addressing\n");
391 /* more than 28 bits used, use 48bit mode */
396 num = device / CFG_SATA_DEVS_PER_BUS;
398 if (device >= CFG_SATA_DEVS_PER_BUS)
399 dev = device - CFG_SATA_DEVS_PER_BUS;
408 if (!(port[num].dev_mask & mask)) {
409 printf ("dev%d is not present on port#%d\n", dev, num);
414 dev_select (&port[num].ioaddr, dev);
416 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
417 if (status & ATA_BUSY) {
418 printf ("ata%u failed to respond\n", port[num].port_no);
421 while (blkcnt-- > 0) {
422 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
423 if (status & ATA_BUSY) {
424 printf ("ata%u failed to respond\n", 0);
429 /* write high bits */
430 sata_outb (0, port[num].ioaddr.nsect_addr);
431 sata_outb ((blknr >> 24) & 0xFF,
432 port[num].ioaddr.lbal_addr);
433 sata_outb ((blknr >> 32) & 0xFF,
434 port[num].ioaddr.lbam_addr);
435 sata_outb ((blknr >> 40) & 0xFF,
436 port[num].ioaddr.lbah_addr);
439 sata_outb (1, port[num].ioaddr.nsect_addr);
440 sata_outb (((blknr) >> 0) & 0xFF,
441 port[num].ioaddr.lbal_addr);
442 sata_outb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
443 sata_outb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
447 sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
448 sata_outb (ATA_CMD_READ_EXT,
449 port[num].ioaddr.command_addr);
453 sata_outb (ATA_LBA | ((blknr >> 24) & 0xF),
454 port[num].ioaddr.device_addr);
455 sata_outb (ATA_CMD_READ,
456 port[num].ioaddr.command_addr);
460 /*may take up to 4 sec */
461 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
463 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
467 printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
468 device, (ulong) blknr, status);
469 err = sata_inb (port[num].ioaddr.error_addr);
470 printf ("Error reg = 0x%x\n", err);
473 input_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
474 sata_inb (port[num].ioaddr.altstatus_addr);
479 buffer += ATA_SECTORWORDS;
485 sata_write (int device, ulong blknr,lbaint_t blkcnt, void * buff)
487 ulong n = 0, *buffer = (ulong *)buff;
488 unsigned char status = 0, num = 0, dev = 0, mask = 0;
491 unsigned char lba48 = 0;
493 if (blknr & 0x0000fffff0000000) {
494 if (!sata_dev_desc[devno].lba48) {
495 printf ("Drive doesn't support 48-bit addressing\n");
498 /* more than 28 bits used, use 48bit mode */
503 num = device / CFG_SATA_DEVS_PER_BUS;
505 if (device >= CFG_SATA_DEVS_PER_BUS)
506 dev = device - CFG_SATA_DEVS_PER_BUS;
516 dev_select (&port[num].ioaddr, dev);
518 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
519 if (status & ATA_BUSY) {
520 printf ("ata%u failed to respond\n", port[num].port_no);
524 while (blkcnt-- > 0) {
525 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 500);
526 if (status & ATA_BUSY) {
527 printf ("ata%u failed to respond\n",
533 /* write high bits */
534 sata_outb (0, port[num].ioaddr.nsect_addr);
535 sata_outb ((blknr >> 24) & 0xFF,
536 port[num].ioaddr.lbal_addr);
537 sata_outb ((blknr >> 32) & 0xFF,
538 port[num].ioaddr.lbam_addr);
539 sata_outb ((blknr >> 40) & 0xFF,
540 port[num].ioaddr.lbah_addr);
543 sata_outb (1, port[num].ioaddr.nsect_addr);
544 sata_outb ((blknr >> 0) & 0xFF, port[num].ioaddr.lbal_addr);
545 sata_outb ((blknr >> 8) & 0xFF, port[num].ioaddr.lbam_addr);
546 sata_outb ((blknr >> 16) & 0xFF, port[num].ioaddr.lbah_addr);
549 sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
550 sata_outb (ATA_CMD_WRITE_EXT,
551 port[num].ioaddr.command_addr);
555 sata_outb (ATA_LBA | ((blknr >> 24) & 0xF),
556 port[num].ioaddr.device_addr);
557 sata_outb (ATA_CMD_WRITE,
558 port[num].ioaddr.command_addr);
562 /*may take up to 4 sec */
563 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
564 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
566 printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
567 device, (ulong) blknr, status);
571 output_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
572 sata_inb (port[num].ioaddr.altstatus_addr);
577 buffer += ATA_SECTORWORDS;
582 block_dev_desc_t *sata_get_dev (int dev);
585 sata_get_dev (int dev)
587 return ((block_dev_desc_t *) & sata_dev_desc[dev]);
591 do_sata (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
597 printf ("Usage:\n%s\n", cmdtp->usage);
600 if (strncmp (argv[1], "init", 4) == 0) {
603 rcode = init_sata ();
605 printf ("Sata initialization Failed\n");
607 } else if (strncmp (argv[1], "inf", 3) == 0) {
611 for (i = 0; i < CFG_SATA_MAXDEVICES; ++i) {
612 /*List only known devices */
613 if (sata_dev_desc[i].type ==
616 printf ("sata dev %d: ", i);
617 dev_print (&sata_dev_desc[i]);
621 printf ("Usage:\n%s\n", cmdtp->usage);
624 if (strcmp (argv[1], "dev") == 0) {
625 int dev = (int) simple_strtoul (argv[2], NULL, 10);
627 if (dev >= CFG_SATA_MAXDEVICES) {
628 printf ("\nSata dev %d not available\n",
632 printf ("\nSATA dev %d: ", dev);
633 dev_print (&sata_dev_desc[dev]);
634 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
638 } else if (strcmp (argv[1], "part") == 0) {
639 int dev = (int) simple_strtoul (argv[2], NULL, 10);
641 if (dev >= CFG_SATA_MAXDEVICES) {
642 printf ("\nSata dev %d not available\n",
646 PRINTF ("\nSATA dev %d: ", dev);
647 if (sata_dev_desc[dev].part_type !=
649 print_part (&sata_dev_desc[dev]);
651 printf ("\nSata dev %d partition type "
657 printf ("Usage:\n%s\n", cmdtp->usage);
661 printf ("Usage:\n%s\n", cmdtp->usage);
664 if (strcmp (argv[1], "read") == 0) {
665 ulong addr = simple_strtoul (argv[2], NULL, 16);
666 ulong cnt = simple_strtoul (argv[4], NULL, 16);
668 lbaint_t blk = simple_strtoul (argv[3], NULL, 16);
670 memset ((int *) addr, 0, cnt * 512);
671 printf ("\nSATA read: dev %d blk # %ld,"
672 "count %ld ... ", curr_dev, blk, cnt);
673 n = sata_read (curr_dev, blk, cnt, (ulong *) addr);
674 /* flush cache after read */
675 flush_cache (addr, cnt * 512);
676 printf ("%ld blocks read: %s\n", n,
677 (n == cnt) ? "OK" : "ERR");
682 } else if (strcmp (argv[1], "write") == 0) {
683 ulong addr = simple_strtoul (argv[2], NULL, 16);
684 ulong cnt = simple_strtoul (argv[4], NULL, 16);
686 lbaint_t blk = simple_strtoul (argv[3], NULL, 16);
688 printf ("\nSata write: dev %d blk # %ld,"
689 "count %ld ... ", curr_dev, blk, cnt);
690 n = sata_write (curr_dev, blk, cnt, (ulong *) addr);
691 printf ("%ld blocks written: %s\n", n,
692 (n == cnt) ? "OK" : "ERR");
698 printf ("Usage:\n%s\n", cmdtp->usage);
704 U_BOOT_CMD (sata, 5, 1, do_sata,
709 "sata read addr blk# cnt\n"
710 "sata write addr blk# cnt\n", "cmd for init,rw and dev-info\n");