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
26 /*File contains SATA read-write and other utility functions.*/
35 #ifdef CFG_SATA_SUPPORTED
36 /*For debug prints set macro DEBUG_SATA to 1 */
38 /*Macro for SATA library specific declarations */
44 sata_inb (unsigned long ioaddr)
49 static void __inline__
50 sata_outb (unsigned char val, unsigned long ioaddr)
56 output_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
58 outsw (ioaddr->data_addr, sect_buf, words << 1);
62 input_data (struct sata_ioports *ioaddr, ulong * sect_buf, int words)
64 insw (ioaddr->data_addr, sect_buf, words << 1);
69 sata_cpy (unsigned char *dst, unsigned char *src, unsigned int len)
71 unsigned char *end, *last;
76 /* reserve space for '\0' */
80 /* skip leading white space */
81 while ((*src) && (src < end) && (*src == ' '))
84 /* copy string, omitting trailing white space */
85 while ((*src) && (src < end)) {
95 sata_bus_softreset (int num)
97 u8 dev = 0, status = 0, i;
99 port[num].dev_mask = 0;
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);
105 port[num].dev_mask |= (1 << i);
106 PRINTF ("dev_chk passed for dev#%d\n", i);
110 if (!(port[num].dev_mask)) {
111 printf ("no devices on port%d\n", num);
115 dev_select (&port[num].ioaddr, dev);
117 port[num].ctl_reg = 0x08; /*Default value of control reg */
118 sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
120 sata_outb (port[num].ctl_reg | ATA_SRST, port[num].ioaddr.ctl_addr);
122 sata_outb (port[num].ctl_reg, port[num].ioaddr.ctl_addr);
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.
133 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 300);
134 while ((status & ATA_BUSY)) {
136 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 3);
139 if (status & ATA_BUSY)
140 printf ("ata%u is slow to respond,plz be patient\n", port);
142 while ((status & ATA_BUSY)) {
144 status = sata_chk_status (&port[num].ioaddr);
147 if (status & ATA_BUSY) {
148 printf ("ata%u failed to respond : ", port);
149 printf ("bus reset failed\n");
156 sata_identify (int num, int dev)
158 u8 cmd = 0, status = 0, devno = num * CFG_SATA_DEVS_PER_BUS + dev;
159 u16 iobuf[ATA_SECT_SIZE];
163 memset (iobuf, 0, sizeof (iobuf));
164 hd_driveid_t *iop = (hd_driveid_t *) iobuf;
171 if (!(port[num].dev_mask & mask)) {
172 printf ("dev%d is not present on port#%d\n", dev, num);
176 printf ("port=%d dev=%d\n", num, dev);
178 dev_select (&port[num].ioaddr, dev);
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);
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;
193 input_data (&port[num].ioaddr, (ulong *) iobuf, ATA_SECTORWORDS);
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]);
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);
206 if (ata_id_has_lba48 (iobuf)) {
207 n_sectors = ata_id_u64 (iobuf, 100);
209 n_sectors = ata_id_u32 (iobuf, 60);
211 PRINTF ("no. of sectors %u\n", ata_id_u64 (iobuf, 100));
212 PRINTF ("no. of sectors %u\n", ata_id_u32 (iobuf, 60));
214 if (n_sectors == 0) {
215 port[num].dev_mask &= ~mask;
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);
228 if ((iop->config & 0x0080) == 0x0080) {
229 sata_dev_desc[devno].removable = 1;
231 sata_dev_desc[devno].removable = 0;
234 sata_dev_desc[devno].lba = iop->lba_capacity;
235 PRINTF ("lba=0x%x", sata_dev_desc[devno].lba);
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);
245 sata_dev_desc[devno].lba48 = 0;
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... */
256 set_Feature_cmd (int num, int dev)
258 u8 mask = 0x00, status = 0;
265 if (!(port[num].dev_mask & mask)) {
266 PRINTF ("dev%d is not present on port#%d\n", dev, num);
270 dev_select (&port[num].ioaddr, dev);
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);
278 sata_outb (ATA_DEVICE_OBS, port[num].ioaddr.device_addr);
279 sata_outb (ATA_CMD_SETF, port[num].ioaddr.command_addr);
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;
292 sata_port (struct sata_ioports *ioport)
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;
307 sata_devchk (struct sata_ioports *ioaddr, int dev)
311 dev_select (ioaddr, dev);
313 sata_outb (0x55, ioaddr->nsect_addr);
314 sata_outb (0xaa, ioaddr->lbal_addr);
316 sata_outb (0xaa, ioaddr->nsect_addr);
317 sata_outb (0x55, ioaddr->lbal_addr);
319 sata_outb (0x55, ioaddr->nsect_addr);
320 sata_outb (0xaa, ioaddr->lbal_addr);
322 nsect = sata_inb (ioaddr->nsect_addr);
323 lbal = sata_inb (ioaddr->lbal_addr);
325 if ((nsect == 0x55) && (lbal == 0xaa))
326 return 1; /* we found a device */
328 return 0; /* nothing found */
332 dev_select (struct sata_ioports *ioaddr, int dev)
337 tmp = ATA_DEVICE_OBS;
339 tmp = ATA_DEVICE_OBS | ATA_DEV1;
341 sata_outb (tmp, ioaddr->device_addr);
342 sata_inb (ioaddr->altstatus_addr);
347 sata_busy_wait (struct sata_ioports *ioaddr, int bits, unsigned int max)
353 status = sata_chk_status (ioaddr);
355 } while ((status & bits) && (max > 0));
361 sata_chk_status (struct sata_ioports * ioaddr)
363 return sata_inb (ioaddr->status_addr);
371 for (i = 0; i < count; i++)
376 sata_read (int device, lbaint_t blknr, ulong blkcnt, ulong * buffer)
379 u8 dev = 0, num = 0, mask = 0, status = 0;
382 unsigned char lba48 = 0;
384 if (blknr & 0x0000fffff0000000) {
385 if (!sata_dev_desc[devno].lba48) {
386 printf ("Drive doesn't support 48-bit addressing\n");
389 /* more than 28 bits used, use 48bit mode */
394 num = device / CFG_SATA_DEVS_PER_BUS;
396 if (device >= CFG_SATA_DEVS_PER_BUS)
397 dev = device - CFG_SATA_DEVS_PER_BUS;
406 if (!(port[num].dev_mask & mask)) {
407 printf ("dev%d is not present on port#%d\n", dev, num);
412 dev_select (&port[num].ioaddr, dev);
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);
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);
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);
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);
445 sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
446 sata_outb (ATA_CMD_READ_EXT,
447 port[num].ioaddr.command_addr);
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);
458 /*may take up to 4 sec */
459 status = sata_busy_wait (&port[num].ioaddr, ATA_BUSY, 4000);
461 if ((status & (ATA_STAT_DRQ | ATA_STAT_BUSY | ATA_STAT_ERR))
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);
471 input_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
472 sata_inb (port[num].ioaddr.altstatus_addr);
477 buffer += ATA_SECTORWORDS;
483 sata_write (int device, lbaint_t blknr, ulong blkcnt, ulong * buffer)
486 unsigned char status = 0, num = 0, dev = 0, mask = 0;
489 unsigned char lba48 = 0;
491 if (blknr & 0x0000fffff0000000) {
492 if (!sata_dev_desc[devno].lba48) {
493 printf ("Drive doesn't support 48-bit addressing\n");
496 /* more than 28 bits used, use 48bit mode */
501 num = device / CFG_SATA_DEVS_PER_BUS;
503 if (device >= CFG_SATA_DEVS_PER_BUS)
504 dev = device - CFG_SATA_DEVS_PER_BUS;
514 dev_select (&port[num].ioaddr, dev);
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);
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",
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);
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);
547 sata_outb (ATA_LBA, port[num].ioaddr.device_addr);
548 sata_outb (ATA_CMD_WRITE_EXT,
549 port[num].ioaddr.command_addr);
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);
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))
564 printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
565 device, (ulong) blknr, status);
569 output_data (&port[num].ioaddr, buffer, ATA_SECTORWORDS);
570 sata_inb (port[num].ioaddr.altstatus_addr);
575 buffer += ATA_SECTORWORDS;
580 block_dev_desc_t *sata_get_dev (int dev);
583 sata_get_dev (int dev)
585 return ((block_dev_desc_t *) & sata_dev_desc[dev]);
589 do_sata (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
595 printf ("Usage:\n%s\n", cmdtp->usage);
598 if (strncmp (argv[1], "init", 4) == 0) {
601 rcode = init_sata ();
603 printf ("Sata initialization Failed\n");
605 } else if (strncmp (argv[1], "inf", 3) == 0) {
609 for (i = 0; i < CFG_SATA_MAXDEVICES; ++i) {
610 /*List only known devices */
611 if (sata_dev_desc[i].type ==
614 printf ("sata dev %d: ", i);
615 dev_print (&sata_dev_desc[i]);
619 printf ("Usage:\n%s\n", cmdtp->usage);
622 if (strcmp (argv[1], "dev") == 0) {
623 int dev = (int) simple_strtoul (argv[2], NULL, 10);
625 if (dev >= CFG_SATA_MAXDEVICES) {
626 printf ("\nSata dev %d not available\n",
630 printf ("\nSATA dev %d: ", dev);
631 dev_print (&sata_dev_desc[dev]);
632 if (sata_dev_desc[dev].type == DEV_TYPE_UNKNOWN)
636 } else if (strcmp (argv[1], "part") == 0) {
637 int dev = (int) simple_strtoul (argv[2], NULL, 10);
639 if (dev >= CFG_SATA_MAXDEVICES) {
640 printf ("\nSata dev %d not available\n",
644 PRINTF ("\nSATA dev %d: ", dev);
645 if (sata_dev_desc[dev].part_type !=
647 print_part (&sata_dev_desc[dev]);
649 printf ("\nSata dev %d partition type "
655 printf ("Usage:\n%s\n", cmdtp->usage);
659 printf ("Usage:\n%s\n", cmdtp->usage);
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);
666 lbaint_t blk = simple_strtoul (argv[3], NULL, 16);
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");
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);
684 lbaint_t blk = simple_strtoul (argv[3], NULL, 16);
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");
696 printf ("Usage:\n%s\n", cmdtp->usage);
702 U_BOOT_CMD (sata, 5, 1, do_sata,
707 "sata read addr blk# cnt\n"
708 "sata write addr blk# cnt\n", "cmd for init,rw and dev-info\n");