2 * Copyright (C) Procsys. All rights reserved.
3 * Author: Mushtaq Khan <mushtaq_k@procsys.com>
4 * <mushtaqk_921@yahoo.co.in>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * with the reference to ata_piix driver in kernel 2.4.32
25 * This file contains SATA controller and SATA drive initialization functions
33 #include <asm/byteorder.h>
38 extern block_dev_desc_t sata_dev_desc
[CONFIG_SYS_SATA_MAX_DEVICE
];
39 extern int sata_curr_device
;
41 #define DEBUG_SATA 0 /*For debug prints set DEBUG_SATA to 1 */
44 #define DRV_DECL /*For file specific declarations */
47 /*Macros realted to PCI*/
48 #define PCI_SATA_BUS 0x00
49 #define PCI_SATA_DEV 0x1f
50 #define PCI_SATA_FUNC 0x02
52 #define PCI_SATA_BASE1 0x10
53 #define PCI_SATA_BASE2 0x14
54 #define PCI_SATA_BASE3 0x18
55 #define PCI_SATA_BASE4 0x1c
56 #define PCI_SATA_BASE5 0x20
60 #define PCI_DMA_CTL 0x48
62 #define PORT_PRESENT (1<<0)
63 #define PORT_ENABLED (1<<4)
66 u32 iobase1
= 0; /*Primary cmd block */
67 u32 iobase2
= 0; /*Primary ctl block */
68 u32 iobase3
= 0; /*Sec cmd block */
69 u32 iobase4
= 0; /*sec ctl block */
70 u32 iobase5
= 0; /*BMDMA*/
74 u32 bus
= PCI_SATA_BUS
;
75 u32 dev
= PCI_SATA_DEV
;
76 u32 fun
= PCI_SATA_FUNC
;
78 u8 lat
= 0, pcibios_max_latency
= 0xff;
79 u8 pmr
; /*Port mapping reg */
80 u8 pi
; /*Prgming Interface reg */
82 bdf
= PCI_BDF (bus
, dev
, fun
);
83 pci_read_config_dword (bdf
, PCI_SATA_BASE1
, &iobase1
);
84 pci_read_config_dword (bdf
, PCI_SATA_BASE2
, &iobase2
);
85 pci_read_config_dword (bdf
, PCI_SATA_BASE3
, &iobase3
);
86 pci_read_config_dword (bdf
, PCI_SATA_BASE4
, &iobase4
);
87 pci_read_config_dword (bdf
, PCI_SATA_BASE5
, &iobase5
);
89 if ((iobase1
== 0xFFFFFFFF) || (iobase2
== 0xFFFFFFFF) ||
90 (iobase3
== 0xFFFFFFFF) || (iobase4
== 0xFFFFFFFF) ||
91 (iobase5
== 0xFFFFFFFF)) {
92 printf ("error no base addr for SATA controller\n");
96 iobase1
&= 0xFFFFFFFE;
97 iobase2
&= 0xFFFFFFFE;
98 iobase3
&= 0xFFFFFFFE;
99 iobase4
&= 0xFFFFFFFE;
100 iobase5
&= 0xFFFFFFFE;
103 pci_read_config_byte (bdf
, PCI_PMR
, &pmr
);
105 printf ("combined mode not supported\n");
109 pci_read_config_byte (bdf
, PCI_PI
, &pi
);
110 if ((pi
& 0x05) != 0x05) {
111 printf ("Sata is in Legacy mode\n");
114 printf ("sata is in Native mode\n");
117 /*MASTER CFG AND IO CFG */
118 pci_read_config_word (bdf
, PCI_COMMAND
, &cmd
);
119 cmd
|= PCI_COMMAND_MASTER
| PCI_COMMAND_IO
;
120 pci_write_config_word (bdf
, PCI_COMMAND
, cmd
);
121 pci_read_config_byte (dev
, PCI_LATENCY_TIMER
, &lat
);
124 lat
= (64 <= pcibios_max_latency
) ? 64 : pcibios_max_latency
;
125 else if (lat
> pcibios_max_latency
)
126 lat
= pcibios_max_latency
;
127 pci_write_config_byte (dev
, PCI_LATENCY_TIMER
, lat
);
133 sata_bus_probe (int port_no
)
138 mask
= (PORT_PRESENT
<< port_no
);
139 pci_read_config_word (bdf
, PCI_PCS
, &pcs
);
140 orig_mask
= (int) pcs
& 0xff;
141 if ((orig_mask
& mask
) != mask
)
158 rv
= pci_sata_init ();
160 printf ("pci initialization failed\n");
165 port
[0].ioaddr
.cmd_addr
= iobase1
;
166 port
[0].ioaddr
.altstatus_addr
= port
[0].ioaddr
.ctl_addr
=
167 iobase2
| ATA_PCI_CTL_OFS
;
168 port
[0].ioaddr
.bmdma_addr
= iobase5
;
171 port
[1].ioaddr
.cmd_addr
= iobase3
;
172 port
[1].ioaddr
.altstatus_addr
= port
[1].ioaddr
.ctl_addr
=
173 iobase4
| ATA_PCI_CTL_OFS
;
174 port
[1].ioaddr
.bmdma_addr
= iobase5
+ 0x8;
176 for (i
= 0; i
< CONFIG_SYS_SATA_MAXBUS
; i
++)
177 sata_port (&port
[i
].ioaddr
);
179 for (i
= 0; i
< CONFIG_SYS_SATA_MAXBUS
; i
++) {
180 if (!(sata_bus_probe (i
))) {
181 port
[i
].port_state
= 0;
182 printf ("SATA#%d port is not present \n", i
);
184 printf ("SATA#%d port is present\n", i
);
185 if (sata_bus_softreset (i
)) {
186 port
[i
].port_state
= 0;
188 port
[i
].port_state
= 1;
193 for (i
= 0; i
< CONFIG_SYS_SATA_MAXBUS
; i
++) {
196 if (port
[i
].port_state
== 0)
198 for (j
= 0; j
< CONFIG_SYS_SATA_DEVS_PER_BUS
; j
++) {
199 sata_identify (i
, j
);
200 set_Feature_cmd (i
, j
);
201 devno
= i
* CONFIG_SYS_SATA_DEVS_PER_BUS
+ j
;
202 if ((sata_dev_desc
[devno
].lba
> 0) &&
203 (sata_dev_desc
[devno
].blksz
> 0)) {
204 dev_print (&sata_dev_desc
[devno
]);
205 /* initialize partition type */
206 init_part (&sata_dev_desc
[devno
]);
214 sata_inb (unsigned long ioaddr
)
219 static void __inline__
220 sata_outb (unsigned char val
, unsigned long ioaddr
)
226 output_data (struct sata_ioports
*ioaddr
, ulong
* sect_buf
, int words
)
228 outsw (ioaddr
->data_addr
, sect_buf
, words
<< 1);
232 input_data (struct sata_ioports
*ioaddr
, ulong
* sect_buf
, int words
)
234 insw (ioaddr
->data_addr
, sect_buf
, words
<< 1);
239 sata_cpy (unsigned char *dst
, unsigned char *src
, unsigned int len
)
241 unsigned char *end
, *last
;
246 /* reserve space for '\0' */
250 /* skip leading white space */
251 while ((*src
) && (src
< end
) && (*src
== ' '))
254 /* copy string, omitting trailing white space */
255 while ((*src
) && (src
< end
)) {
265 sata_bus_softreset (int num
)
267 u8 dev
= 0, status
= 0, i
;
269 port
[num
].dev_mask
= 0;
271 for (i
= 0; i
< CONFIG_SYS_SATA_DEVS_PER_BUS
; i
++) {
272 if (!(sata_devchk (&port
[num
].ioaddr
, i
))) {
273 PRINTF ("dev_chk failed for dev#%d\n", i
);
275 port
[num
].dev_mask
|= (1 << i
);
276 PRINTF ("dev_chk passed for dev#%d\n", i
);
280 if (!(port
[num
].dev_mask
)) {
281 printf ("no devices on port%d\n", num
);
285 dev_select (&port
[num
].ioaddr
, dev
);
287 port
[num
].ctl_reg
= 0x08; /*Default value of control reg */
288 sata_outb (port
[num
].ctl_reg
, port
[num
].ioaddr
.ctl_addr
);
290 sata_outb (port
[num
].ctl_reg
| ATA_SRST
, port
[num
].ioaddr
.ctl_addr
);
292 sata_outb (port
[num
].ctl_reg
, port
[num
].ioaddr
.ctl_addr
);
294 /* spec mandates ">= 2ms" before checking status.
295 * We wait 150ms, because that was the magic delay used for
296 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
297 * between when the ATA command register is written, and then
298 * status is checked. Because waiting for "a while" before
299 * checking status is fine, post SRST, we perform this magic
300 * delay here as well.
303 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 300);
304 while ((status
& ATA_BUSY
)) {
306 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 3);
309 if (status
& ATA_BUSY
)
310 printf ("ata%u is slow to respond,plz be patient\n", num
);
312 while ((status
& ATA_BUSY
)) {
314 status
= sata_chk_status (&port
[num
].ioaddr
);
317 if (status
& ATA_BUSY
) {
318 printf ("ata%u failed to respond : ", num
);
319 printf ("bus reset failed\n");
326 sata_identify (int num
, int dev
)
328 u8 cmd
= 0, status
= 0, devno
= num
* CONFIG_SYS_SATA_DEVS_PER_BUS
+ dev
;
329 u16 iobuf
[ATA_SECT_SIZE
];
333 memset (iobuf
, 0, sizeof (iobuf
));
334 hd_driveid_t
*iop
= (hd_driveid_t
*) iobuf
;
341 if (!(port
[num
].dev_mask
& mask
)) {
342 printf ("dev%d is not present on port#%d\n", dev
, num
);
346 printf ("port=%d dev=%d\n", num
, dev
);
348 dev_select (&port
[num
].ioaddr
, dev
);
351 cmd
= ATA_CMD_IDENT
; /*Device Identify Command */
352 sata_outb (cmd
, port
[num
].ioaddr
.command_addr
);
353 sata_inb (port
[num
].ioaddr
.altstatus_addr
);
356 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 1000);
357 if (status
& ATA_ERR
) {
358 printf ("\ndevice not responding\n");
359 port
[num
].dev_mask
&= ~mask
;
363 input_data (&port
[num
].ioaddr
, (ulong
*) iobuf
, ATA_SECTORWORDS
);
365 PRINTF ("\nata%u: dev %u cfg 49:%04x 82:%04x 83:%04x 84:%04x85:%04x"
366 "86:%04x" "87:%04x 88:%04x\n", num
, dev
, iobuf
[49],
367 iobuf
[82], iobuf
[83], iobuf
[84], iobuf
[85], iobuf
[86],
368 iobuf
[87], iobuf
[88]);
370 /* we require LBA and DMA support (bits 8 & 9 of word 49) */
371 if (!ata_id_has_dma (iobuf
) || !ata_id_has_lba (iobuf
)) {
372 PRINTF ("ata%u: no dma/lba\n", num
);
376 if (ata_id_has_lba48 (iobuf
)) {
377 n_sectors
= ata_id_u64 (iobuf
, 100);
379 n_sectors
= ata_id_u32 (iobuf
, 60);
381 PRINTF ("no. of sectors %u\n", ata_id_u64 (iobuf
, 100));
382 PRINTF ("no. of sectors %u\n", ata_id_u32 (iobuf
, 60));
384 if (n_sectors
== 0) {
385 port
[num
].dev_mask
&= ~mask
;
389 sata_cpy ((unsigned char *)sata_dev_desc
[devno
].revision
, iop
->fw_rev
,
390 sizeof (sata_dev_desc
[devno
].revision
));
391 sata_cpy ((unsigned char *)sata_dev_desc
[devno
].vendor
, iop
->model
,
392 sizeof (sata_dev_desc
[devno
].vendor
));
393 sata_cpy ((unsigned char *)sata_dev_desc
[devno
].product
, iop
->serial_no
,
394 sizeof (sata_dev_desc
[devno
].product
));
395 strswab (sata_dev_desc
[devno
].revision
);
396 strswab (sata_dev_desc
[devno
].vendor
);
398 if ((iop
->config
& 0x0080) == 0x0080) {
399 sata_dev_desc
[devno
].removable
= 1;
401 sata_dev_desc
[devno
].removable
= 0;
404 sata_dev_desc
[devno
].lba
= iop
->lba_capacity
;
405 PRINTF ("lba=0x%x", sata_dev_desc
[devno
].lba
);
408 if (iop
->command_set_2
& 0x0400) {
409 sata_dev_desc
[devno
].lba48
= 1;
410 lba
= (unsigned long long) iop
->lba48_capacity
[0] |
411 ((unsigned long long) iop
->lba48_capacity
[1] << 16) |
412 ((unsigned long long) iop
->lba48_capacity
[2] << 32) |
413 ((unsigned long long) iop
->lba48_capacity
[3] << 48);
415 sata_dev_desc
[devno
].lba48
= 0;
420 sata_dev_desc
[devno
].type
= DEV_TYPE_HARDDISK
;
421 sata_dev_desc
[devno
].blksz
= ATA_BLOCKSIZE
;
422 sata_dev_desc
[devno
].lun
= 0; /* just to fill something in... */
426 set_Feature_cmd (int num
, int dev
)
428 u8 mask
= 0x00, status
= 0;
435 if (!(port
[num
].dev_mask
& mask
)) {
436 PRINTF ("dev%d is not present on port#%d\n", dev
, num
);
440 dev_select (&port
[num
].ioaddr
, dev
);
442 sata_outb (SETFEATURES_XFER
, port
[num
].ioaddr
.feature_addr
);
443 sata_outb (XFER_PIO_4
, port
[num
].ioaddr
.nsect_addr
);
444 sata_outb (0, port
[num
].ioaddr
.lbal_addr
);
445 sata_outb (0, port
[num
].ioaddr
.lbam_addr
);
446 sata_outb (0, port
[num
].ioaddr
.lbah_addr
);
448 sata_outb (ATA_DEVICE_OBS
, port
[num
].ioaddr
.device_addr
);
449 sata_outb (ATA_CMD_SETF
, port
[num
].ioaddr
.command_addr
);
454 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 5000);
455 if ((status
& (ATA_STAT_BUSY
| ATA_STAT_ERR
))) {
456 printf ("Error : status 0x%02x\n", status
);
457 port
[num
].dev_mask
&= ~mask
;
462 sata_port (struct sata_ioports
*ioport
)
464 ioport
->data_addr
= ioport
->cmd_addr
+ ATA_REG_DATA
;
465 ioport
->error_addr
= ioport
->cmd_addr
+ ATA_REG_ERR
;
466 ioport
->feature_addr
= ioport
->cmd_addr
+ ATA_REG_FEATURE
;
467 ioport
->nsect_addr
= ioport
->cmd_addr
+ ATA_REG_NSECT
;
468 ioport
->lbal_addr
= ioport
->cmd_addr
+ ATA_REG_LBAL
;
469 ioport
->lbam_addr
= ioport
->cmd_addr
+ ATA_REG_LBAM
;
470 ioport
->lbah_addr
= ioport
->cmd_addr
+ ATA_REG_LBAH
;
471 ioport
->device_addr
= ioport
->cmd_addr
+ ATA_REG_DEVICE
;
472 ioport
->status_addr
= ioport
->cmd_addr
+ ATA_REG_STATUS
;
473 ioport
->command_addr
= ioport
->cmd_addr
+ ATA_REG_CMD
;
477 sata_devchk (struct sata_ioports
*ioaddr
, int dev
)
481 dev_select (ioaddr
, dev
);
483 sata_outb (0x55, ioaddr
->nsect_addr
);
484 sata_outb (0xaa, ioaddr
->lbal_addr
);
486 sata_outb (0xaa, ioaddr
->nsect_addr
);
487 sata_outb (0x55, ioaddr
->lbal_addr
);
489 sata_outb (0x55, ioaddr
->nsect_addr
);
490 sata_outb (0xaa, ioaddr
->lbal_addr
);
492 nsect
= sata_inb (ioaddr
->nsect_addr
);
493 lbal
= sata_inb (ioaddr
->lbal_addr
);
495 if ((nsect
== 0x55) && (lbal
== 0xaa))
496 return 1; /* we found a device */
498 return 0; /* nothing found */
502 dev_select (struct sata_ioports
*ioaddr
, int dev
)
507 tmp
= ATA_DEVICE_OBS
;
509 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
511 sata_outb (tmp
, ioaddr
->device_addr
);
512 sata_inb (ioaddr
->altstatus_addr
);
517 sata_busy_wait (struct sata_ioports
*ioaddr
, int bits
, unsigned int max
)
523 status
= sata_chk_status (ioaddr
);
525 } while ((status
& bits
) && (max
> 0));
531 sata_chk_status (struct sata_ioports
* ioaddr
)
533 return sata_inb (ioaddr
->status_addr
);
541 for (i
= 0; i
< count
; i
++)
546 sata_read (int device
, ulong blknr
,lbaint_t blkcnt
, void * buff
)
548 ulong n
= 0, *buffer
= (ulong
*)buff
;
549 u8 dev
= 0, num
= 0, mask
= 0, status
= 0;
552 unsigned char lba48
= 0;
554 if (blknr
& 0x0000fffff0000000) {
555 if (!sata_dev_desc
[devno
].lba48
) {
556 printf ("Drive doesn't support 48-bit addressing\n");
559 /* more than 28 bits used, use 48bit mode */
564 num
= device
/ CONFIG_SYS_SATA_DEVS_PER_BUS
;
566 if (device
>= CONFIG_SYS_SATA_DEVS_PER_BUS
)
567 dev
= device
- CONFIG_SYS_SATA_DEVS_PER_BUS
;
576 if (!(port
[num
].dev_mask
& mask
)) {
577 printf ("dev%d is not present on port#%d\n", dev
, num
);
582 dev_select (&port
[num
].ioaddr
, dev
);
584 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 500);
585 if (status
& ATA_BUSY
) {
586 printf ("ata%u failed to respond\n", port
[num
].port_no
);
589 while (blkcnt
-- > 0) {
590 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 500);
591 if (status
& ATA_BUSY
) {
592 printf ("ata%u failed to respond\n", 0);
597 /* write high bits */
598 sata_outb (0, port
[num
].ioaddr
.nsect_addr
);
599 sata_outb ((blknr
>> 24) & 0xFF,
600 port
[num
].ioaddr
.lbal_addr
);
601 sata_outb ((blknr
>> 32) & 0xFF,
602 port
[num
].ioaddr
.lbam_addr
);
603 sata_outb ((blknr
>> 40) & 0xFF,
604 port
[num
].ioaddr
.lbah_addr
);
607 sata_outb (1, port
[num
].ioaddr
.nsect_addr
);
608 sata_outb (((blknr
) >> 0) & 0xFF,
609 port
[num
].ioaddr
.lbal_addr
);
610 sata_outb ((blknr
>> 8) & 0xFF, port
[num
].ioaddr
.lbam_addr
);
611 sata_outb ((blknr
>> 16) & 0xFF, port
[num
].ioaddr
.lbah_addr
);
615 sata_outb (ATA_LBA
, port
[num
].ioaddr
.device_addr
);
616 sata_outb (ATA_CMD_READ_EXT
,
617 port
[num
].ioaddr
.command_addr
);
621 sata_outb (ATA_LBA
| ((blknr
>> 24) & 0xF),
622 port
[num
].ioaddr
.device_addr
);
623 sata_outb (ATA_CMD_READ
,
624 port
[num
].ioaddr
.command_addr
);
628 /*may take up to 4 sec */
629 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 4000);
631 if ((status
& (ATA_STAT_DRQ
| ATA_STAT_BUSY
| ATA_STAT_ERR
))
635 printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
636 device
, (ulong
) blknr
, status
);
637 err
= sata_inb (port
[num
].ioaddr
.error_addr
);
638 printf ("Error reg = 0x%x\n", err
);
641 input_data (&port
[num
].ioaddr
, buffer
, ATA_SECTORWORDS
);
642 sata_inb (port
[num
].ioaddr
.altstatus_addr
);
647 buffer
+= ATA_SECTORWORDS
;
653 sata_write (int device
, ulong blknr
,lbaint_t blkcnt
, void * buff
)
655 ulong n
= 0, *buffer
= (ulong
*)buff
;
656 unsigned char status
= 0, num
= 0, dev
= 0, mask
= 0;
659 unsigned char lba48
= 0;
661 if (blknr
& 0x0000fffff0000000) {
662 if (!sata_dev_desc
[devno
].lba48
) {
663 printf ("Drive doesn't support 48-bit addressing\n");
666 /* more than 28 bits used, use 48bit mode */
671 num
= device
/ CONFIG_SYS_SATA_DEVS_PER_BUS
;
673 if (device
>= CONFIG_SYS_SATA_DEVS_PER_BUS
)
674 dev
= device
- CONFIG_SYS_SATA_DEVS_PER_BUS
;
684 dev_select (&port
[num
].ioaddr
, dev
);
686 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 500);
687 if (status
& ATA_BUSY
) {
688 printf ("ata%u failed to respond\n", port
[num
].port_no
);
692 while (blkcnt
-- > 0) {
693 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 500);
694 if (status
& ATA_BUSY
) {
695 printf ("ata%u failed to respond\n",
701 /* write high bits */
702 sata_outb (0, port
[num
].ioaddr
.nsect_addr
);
703 sata_outb ((blknr
>> 24) & 0xFF,
704 port
[num
].ioaddr
.lbal_addr
);
705 sata_outb ((blknr
>> 32) & 0xFF,
706 port
[num
].ioaddr
.lbam_addr
);
707 sata_outb ((blknr
>> 40) & 0xFF,
708 port
[num
].ioaddr
.lbah_addr
);
711 sata_outb (1, port
[num
].ioaddr
.nsect_addr
);
712 sata_outb ((blknr
>> 0) & 0xFF, port
[num
].ioaddr
.lbal_addr
);
713 sata_outb ((blknr
>> 8) & 0xFF, port
[num
].ioaddr
.lbam_addr
);
714 sata_outb ((blknr
>> 16) & 0xFF, port
[num
].ioaddr
.lbah_addr
);
717 sata_outb (ATA_LBA
, port
[num
].ioaddr
.device_addr
);
718 sata_outb (ATA_CMD_WRITE_EXT
,
719 port
[num
].ioaddr
.command_addr
);
723 sata_outb (ATA_LBA
| ((blknr
>> 24) & 0xF),
724 port
[num
].ioaddr
.device_addr
);
725 sata_outb (ATA_CMD_WRITE
,
726 port
[num
].ioaddr
.command_addr
);
730 /*may take up to 4 sec */
731 status
= sata_busy_wait (&port
[num
].ioaddr
, ATA_BUSY
, 4000);
732 if ((status
& (ATA_STAT_DRQ
| ATA_STAT_BUSY
| ATA_STAT_ERR
))
734 printf ("Error no DRQ dev %d blk %ld: sts 0x%02x\n",
735 device
, (ulong
) blknr
, status
);
739 output_data (&port
[num
].ioaddr
, buffer
, ATA_SECTORWORDS
);
740 sata_inb (port
[num
].ioaddr
.altstatus_addr
);
745 buffer
+= ATA_SECTORWORDS
;
750 int scan_sata(int dev
)