2 * Copyright (C) Freescale Semiconductor, Inc. 2006.
3 * Author: Jason Jin<Jason.jin@freescale.com>
4 * Zhang Wei<wei.zhang@freescale.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * with the reference on libata and ahci drvier in kernel
31 #include <asm/processor.h>
32 #include <asm/errno.h>
37 #include <linux/ctype.h>
40 struct ahci_probe_ent
*probe_ent
= NULL
;
41 hd_driveid_t
*ataid
[AHCI_MAX_PORTS
];
43 #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
46 static inline u32
ahci_port_base(u32 base
, u32 port
)
48 return base
+ 0x100 + (port
* 0x80);
52 static void ahci_setup_port(struct ahci_ioports
*port
, unsigned long base
,
53 unsigned int port_idx
)
55 base
= ahci_port_base(base
, port_idx
);
57 port
->cmd_addr
= base
;
58 port
->scr_addr
= base
+ PORT_SCR
;
62 #define msleep(a) udelay(a * 1000)
63 #define ssleep(a) msleep(a * 1000)
65 static int waiting_for_cmd_completed(volatile u8
*offset
,
72 for (i
= 0; ((status
= readl(offset
)) & sign
) && i
< timeout_msec
; i
++)
75 return (i
< timeout_msec
) ? 0 : -1;
79 static int ahci_host_init(struct ahci_probe_ent
*probe_ent
)
81 #ifndef CONFIG_SCSI_AHCI_PLAT
82 pci_dev_t pdev
= probe_ent
->dev
;
84 unsigned short vendor
;
86 volatile u8
*mmio
= (volatile u8
*)probe_ent
->mmio_base
;
89 volatile u8
*port_mmio
;
91 cap_save
= readl(mmio
+ HOST_CAP
);
92 cap_save
&= ((1 << 28) | (1 << 17));
93 cap_save
|= (1 << 27);
95 /* global controller reset */
96 tmp
= readl(mmio
+ HOST_CTL
);
97 if ((tmp
& HOST_RESET
) == 0)
98 writel_with_flush(tmp
| HOST_RESET
, mmio
+ HOST_CTL
);
100 /* reset must complete within 1 second, or
101 * the hardware should be considered fried.
105 tmp
= readl(mmio
+ HOST_CTL
);
106 if (tmp
& HOST_RESET
) {
107 debug("controller reset failed (0x%x)\n", tmp
);
111 writel_with_flush(HOST_AHCI_EN
, mmio
+ HOST_CTL
);
112 writel(cap_save
, mmio
+ HOST_CAP
);
113 writel_with_flush(0xf, mmio
+ HOST_PORTS_IMPL
);
115 #ifndef CONFIG_SCSI_AHCI_PLAT
116 pci_read_config_word(pdev
, PCI_VENDOR_ID
, &vendor
);
118 if (vendor
== PCI_VENDOR_ID_INTEL
) {
120 pci_read_config_word(pdev
, 0x92, &tmp16
);
122 pci_write_config_word(pdev
, 0x92, tmp16
);
125 probe_ent
->cap
= readl(mmio
+ HOST_CAP
);
126 probe_ent
->port_map
= readl(mmio
+ HOST_PORTS_IMPL
);
127 probe_ent
->n_ports
= (probe_ent
->cap
& 0x1f) + 1;
129 debug("cap 0x%x port_map 0x%x n_ports %d\n",
130 probe_ent
->cap
, probe_ent
->port_map
, probe_ent
->n_ports
);
132 for (i
= 0; i
< probe_ent
->n_ports
; i
++) {
133 probe_ent
->port
[i
].port_mmio
= ahci_port_base((u32
) mmio
, i
);
134 port_mmio
= (u8
*) probe_ent
->port
[i
].port_mmio
;
135 ahci_setup_port(&probe_ent
->port
[i
], (unsigned long)mmio
, i
);
137 /* make sure port is not active */
138 tmp
= readl(port_mmio
+ PORT_CMD
);
139 if (tmp
& (PORT_CMD_LIST_ON
| PORT_CMD_FIS_ON
|
140 PORT_CMD_FIS_RX
| PORT_CMD_START
)) {
141 tmp
&= ~(PORT_CMD_LIST_ON
| PORT_CMD_FIS_ON
|
142 PORT_CMD_FIS_RX
| PORT_CMD_START
);
143 writel_with_flush(tmp
, port_mmio
+ PORT_CMD
);
145 /* spec says 500 msecs for each bit, so
146 * this is slightly incorrect.
151 writel(PORT_CMD_SPIN_UP
, port_mmio
+ PORT_CMD
);
156 tmp
= readl(port_mmio
+ PORT_SCR_STAT
);
157 if ((tmp
& 0xf) == 0x3)
162 tmp
= readl(port_mmio
+ PORT_SCR_ERR
);
163 debug("PORT_SCR_ERR 0x%x\n", tmp
);
164 writel(tmp
, port_mmio
+ PORT_SCR_ERR
);
166 /* ack any pending irq events for this port */
167 tmp
= readl(port_mmio
+ PORT_IRQ_STAT
);
168 debug("PORT_IRQ_STAT 0x%x\n", tmp
);
170 writel(tmp
, port_mmio
+ PORT_IRQ_STAT
);
172 writel(1 << i
, mmio
+ HOST_IRQ_STAT
);
174 /* set irq mask (enables interrupts) */
175 writel(DEF_PORT_IRQ
, port_mmio
+ PORT_IRQ_MASK
);
177 /*register linkup ports */
178 tmp
= readl(port_mmio
+ PORT_SCR_STAT
);
179 debug("Port %d status: 0x%x\n", i
, tmp
);
180 if ((tmp
& 0xf) == 0x03)
181 probe_ent
->link_port_map
|= (0x01 << i
);
184 tmp
= readl(mmio
+ HOST_CTL
);
185 debug("HOST_CTL 0x%x\n", tmp
);
186 writel(tmp
| HOST_IRQ_EN
, mmio
+ HOST_CTL
);
187 tmp
= readl(mmio
+ HOST_CTL
);
188 debug("HOST_CTL 0x%x\n", tmp
);
189 #ifndef CONFIG_SCSI_AHCI_PLAT
190 pci_read_config_word(pdev
, PCI_COMMAND
, &tmp16
);
191 tmp
|= PCI_COMMAND_MASTER
;
192 pci_write_config_word(pdev
, PCI_COMMAND
, tmp16
);
198 static void ahci_print_info(struct ahci_probe_ent
*probe_ent
)
200 #ifndef CONFIG_SCSI_AHCI_PLAT
201 pci_dev_t pdev
= probe_ent
->dev
;
204 volatile u8
*mmio
= (volatile u8
*)probe_ent
->mmio_base
;
205 u32 vers
, cap
, impl
, speed
;
209 vers
= readl(mmio
+ HOST_VERSION
);
210 cap
= probe_ent
->cap
;
211 impl
= probe_ent
->port_map
;
213 speed
= (cap
>> 20) & 0xf;
221 #ifdef CONFIG_SCSI_AHCI_PLAT
224 pci_read_config_word(pdev
, 0x0a, &cc
);
227 else if (cc
== 0x0106)
229 else if (cc
== 0x0104)
234 printf("AHCI %02x%02x.%02x%02x "
235 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
240 ((cap
>> 8) & 0x1f) + 1, (cap
& 0x1f) + 1, speed_s
, impl
, scc_s
);
245 cap
& (1 << 31) ? "64bit " : "",
246 cap
& (1 << 30) ? "ncq " : "",
247 cap
& (1 << 28) ? "ilck " : "",
248 cap
& (1 << 27) ? "stag " : "",
249 cap
& (1 << 26) ? "pm " : "",
250 cap
& (1 << 25) ? "led " : "",
251 cap
& (1 << 24) ? "clo " : "",
252 cap
& (1 << 19) ? "nz " : "",
253 cap
& (1 << 18) ? "only " : "",
254 cap
& (1 << 17) ? "pmp " : "",
255 cap
& (1 << 15) ? "pio " : "",
256 cap
& (1 << 14) ? "slum " : "",
257 cap
& (1 << 13) ? "part " : "");
260 #ifndef CONFIG_SCSI_AHCI_PLAT
261 static int ahci_init_one(pci_dev_t pdev
)
266 memset((void *)ataid
, 0, sizeof(hd_driveid_t
*) * AHCI_MAX_PORTS
);
268 probe_ent
= malloc(sizeof(struct ahci_probe_ent
));
269 memset(probe_ent
, 0, sizeof(struct ahci_probe_ent
));
270 probe_ent
->dev
= pdev
;
272 probe_ent
->host_flags
= ATA_FLAG_SATA
277 probe_ent
->pio_mask
= 0x1f;
278 probe_ent
->udma_mask
= 0x7f; /*Fixme,assume to support UDMA6 */
280 probe_ent
->mmio_base
= (u32
)pci_map_bar(pdev
, AHCI_PCI_BAR
,
284 * JMicron-specific fixup:
285 * make sure we're in AHCI mode
287 pci_read_config_word(pdev
, PCI_VENDOR_ID
, &vendor
);
288 if (vendor
== 0x197b)
289 pci_write_config_byte(pdev
, 0x41, 0xa1);
291 /* initialize adapter */
292 rc
= ahci_host_init(probe_ent
);
296 ahci_print_info(probe_ent
);
305 #define MAX_DATA_BYTE_COUNT (4*1024*1024)
307 static int ahci_fill_sg(u8 port
, unsigned char *buf
, int buf_len
)
309 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
310 struct ahci_sg
*ahci_sg
= pp
->cmd_tbl_sg
;
314 sg_count
= ((buf_len
- 1) / MAX_DATA_BYTE_COUNT
) + 1;
315 if (sg_count
> AHCI_MAX_SG
) {
316 printf("Error:Too much sg!\n");
320 for (i
= 0; i
< sg_count
; i
++) {
322 cpu_to_le32((u32
) buf
+ i
* MAX_DATA_BYTE_COUNT
);
323 ahci_sg
->addr_hi
= 0;
324 ahci_sg
->flags_size
= cpu_to_le32(0x3fffff &
325 (buf_len
< MAX_DATA_BYTE_COUNT
327 : (MAX_DATA_BYTE_COUNT
- 1)));
329 buf_len
-= MAX_DATA_BYTE_COUNT
;
336 static void ahci_fill_cmd_slot(struct ahci_ioports
*pp
, u32 opts
)
338 pp
->cmd_slot
->opts
= cpu_to_le32(opts
);
339 pp
->cmd_slot
->status
= 0;
340 pp
->cmd_slot
->tbl_addr
= cpu_to_le32(pp
->cmd_tbl
& 0xffffffff);
341 pp
->cmd_slot
->tbl_addr_hi
= 0;
345 static void ahci_set_feature(u8 port
)
347 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
348 volatile u8
*port_mmio
= (volatile u8
*)pp
->port_mmio
;
349 u32 cmd_fis_len
= 5; /* five dwords */
356 fis
[2] = ATA_CMD_SETF
;
357 fis
[3] = SETFEATURES_XFER
;
358 fis
[12] = __ilog2(probe_ent
->udma_mask
+ 1) + 0x40 - 0x01;
360 memcpy((unsigned char *)pp
->cmd_tbl
, fis
, 20);
361 ahci_fill_cmd_slot(pp
, cmd_fis_len
);
362 writel(1, port_mmio
+ PORT_CMD_ISSUE
);
363 readl(port_mmio
+ PORT_CMD_ISSUE
);
365 if (waiting_for_cmd_completed(port_mmio
+ PORT_CMD_ISSUE
, 150, 0x1)) {
366 printf("set feature error!\n");
371 static int ahci_port_start(u8 port
)
373 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
374 volatile u8
*port_mmio
= (volatile u8
*)pp
->port_mmio
;
378 debug("Enter start port: %d\n", port
);
379 port_status
= readl(port_mmio
+ PORT_SCR_STAT
);
380 debug("Port %d status: %x\n", port
, port_status
);
381 if ((port_status
& 0xf) != 0x03) {
382 printf("No Link on this port!\n");
386 mem
= (u32
) malloc(AHCI_PORT_PRIV_DMA_SZ
+ 2048);
389 printf("No mem for table!\n");
393 mem
= (mem
+ 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
394 memset((u8
*) mem
, 0, AHCI_PORT_PRIV_DMA_SZ
);
397 * First item in chunk of DMA memory: 32-slot command table,
398 * 32 bytes each in size
400 pp
->cmd_slot
= (struct ahci_cmd_hdr
*)mem
;
401 debug("cmd_slot = %p\n", pp
->cmd_slot
);
402 mem
+= (AHCI_CMD_SLOT_SZ
+ 224);
405 * Second item: Received-FIS area
408 mem
+= AHCI_RX_FIS_SZ
;
411 * Third item: data area for storing a single command
412 * and its scatter-gather table
415 debug("cmd_tbl_dma = 0x%x\n", pp
->cmd_tbl
);
417 mem
+= AHCI_CMD_TBL_HDR
;
418 pp
->cmd_tbl_sg
= (struct ahci_sg
*)mem
;
420 writel_with_flush((u32
) pp
->cmd_slot
, port_mmio
+ PORT_LST_ADDR
);
422 writel_with_flush(pp
->rx_fis
, port_mmio
+ PORT_FIS_ADDR
);
424 writel_with_flush(PORT_CMD_ICC_ACTIVE
| PORT_CMD_FIS_RX
|
425 PORT_CMD_POWER_ON
| PORT_CMD_SPIN_UP
|
426 PORT_CMD_START
, port_mmio
+ PORT_CMD
);
428 debug("Exit start port %d\n", port
);
434 static int get_ahci_device_data(u8 port
, u8
*fis
, int fis_len
, u8
*buf
,
438 struct ahci_ioports
*pp
= &(probe_ent
->port
[port
]);
439 volatile u8
*port_mmio
= (volatile u8
*)pp
->port_mmio
;
444 debug("Enter get_ahci_device_data: for port %d\n", port
);
446 if (port
> probe_ent
->n_ports
) {
447 printf("Invaild port number %d\n", port
);
451 port_status
= readl(port_mmio
+ PORT_SCR_STAT
);
452 if ((port_status
& 0xf) != 0x03) {
453 debug("No Link on port %d!\n", port
);
457 memcpy((unsigned char *)pp
->cmd_tbl
, fis
, fis_len
);
459 sg_count
= ahci_fill_sg(port
, buf
, buf_len
);
460 opts
= (fis_len
>> 2) | (sg_count
<< 16);
461 ahci_fill_cmd_slot(pp
, opts
);
463 writel_with_flush(1, port_mmio
+ PORT_CMD_ISSUE
);
465 if (waiting_for_cmd_completed(port_mmio
+ PORT_CMD_ISSUE
, 150, 0x1)) {
466 printf("timeout exit!\n");
469 debug("get_ahci_device_data: %d byte transferred.\n",
470 pp
->cmd_slot
->status
);
476 static char *ata_id_strcpy(u16
*target
, u16
*src
, int len
)
479 for (i
= 0; i
< len
/ 2; i
++)
480 target
[i
] = swab16(src
[i
]);
481 return (char *)target
;
485 static void dump_ataid(hd_driveid_t
*ataid
)
487 debug("(49)ataid->capability = 0x%x\n", ataid
->capability
);
488 debug("(53)ataid->field_valid =0x%x\n", ataid
->field_valid
);
489 debug("(63)ataid->dma_mword = 0x%x\n", ataid
->dma_mword
);
490 debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid
->eide_pio_modes
);
491 debug("(75)ataid->queue_depth = 0x%x\n", ataid
->queue_depth
);
492 debug("(80)ataid->major_rev_num = 0x%x\n", ataid
->major_rev_num
);
493 debug("(81)ataid->minor_rev_num = 0x%x\n", ataid
->minor_rev_num
);
494 debug("(82)ataid->command_set_1 = 0x%x\n", ataid
->command_set_1
);
495 debug("(83)ataid->command_set_2 = 0x%x\n", ataid
->command_set_2
);
496 debug("(84)ataid->cfsse = 0x%x\n", ataid
->cfsse
);
497 debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid
->cfs_enable_1
);
498 debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid
->cfs_enable_2
);
499 debug("(87)ataid->csf_default = 0x%x\n", ataid
->csf_default
);
500 debug("(88)ataid->dma_ultra = 0x%x\n", ataid
->dma_ultra
);
501 debug("(93)ataid->hw_config = 0x%x\n", ataid
->hw_config
);
506 * SCSI INQUIRY command operation.
508 static int ata_scsiop_inquiry(ccb
*pccb
)
513 0x5, /* claim SPC-3 version compatibility */
521 /* Clean ccb data buffer */
522 memset(pccb
->pdata
, 0, pccb
->datalen
);
524 memcpy(pccb
->pdata
, hdr
, sizeof(hdr
));
526 if (pccb
->datalen
<= 35)
530 /* Construct the FIS */
531 fis
[0] = 0x27; /* Host to device FIS. */
532 fis
[1] = 1 << 7; /* Command FIS. */
533 fis
[2] = ATA_CMD_IDENT
; /* Command byte. */
535 /* Read id from sata */
537 if (!(tmpid
= malloc(sizeof(hd_driveid_t
))))
540 if (get_ahci_device_data(port
, (u8
*) & fis
, 20,
541 tmpid
, sizeof(hd_driveid_t
))) {
542 debug("scsi_ahci: SCSI inquiry command failure.\n");
548 ataid
[port
] = (hd_driveid_t
*) tmpid
;
550 memcpy(&pccb
->pdata
[8], "ATA ", 8);
551 ata_id_strcpy((u16
*) &pccb
->pdata
[16], (u16
*)ataid
[port
]->model
, 16);
552 ata_id_strcpy((u16
*) &pccb
->pdata
[32], (u16
*)ataid
[port
]->fw_rev
, 4);
554 dump_ataid(ataid
[port
]);
560 * SCSI READ10 command operation.
562 static int ata_scsiop_read10(ccb
* pccb
)
568 lba
= (((u64
) pccb
->cmd
[2]) << 24) | (((u64
) pccb
->cmd
[3]) << 16)
569 | (((u64
) pccb
->cmd
[4]) << 8) | ((u64
) pccb
->cmd
[5]);
570 len
= (((u32
) pccb
->cmd
[7]) << 8) | ((u32
) pccb
->cmd
[8]);
572 /* For 10-byte and 16-byte SCSI R/W commands, transfer
573 * length 0 means transfer 0 block of data.
574 * However, for ATA R/W commands, sector count 0 means
575 * 256 or 65536 sectors, not 0 sectors as in SCSI.
577 * WARNING: one or two older ATA drives treat 0 as 0...
583 /* Construct the FIS */
584 fis
[0] = 0x27; /* Host to device FIS. */
585 fis
[1] = 1 << 7; /* Command FIS. */
586 fis
[2] = ATA_CMD_RD_DMA
; /* Command byte. */
588 /* LBA address, only support LBA28 in this driver */
589 fis
[4] = pccb
->cmd
[5];
590 fis
[5] = pccb
->cmd
[4];
591 fis
[6] = pccb
->cmd
[3];
592 fis
[7] = (pccb
->cmd
[2] & 0x0f) | 0xe0;
595 fis
[12] = pccb
->cmd
[8];
596 fis
[13] = pccb
->cmd
[7];
599 if (get_ahci_device_data(pccb
->target
, (u8
*) & fis
, 20,
600 pccb
->pdata
, pccb
->datalen
)) {
601 debug("scsi_ahci: SCSI READ10 command failure.\n");
610 * SCSI READ CAPACITY10 command operation.
612 static int ata_scsiop_read_capacity10(ccb
*pccb
)
616 if (!ataid
[pccb
->target
]) {
617 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
619 "\tPlease run SCSI commmand INQUIRY firstly!\n");
623 cap
= le32_to_cpu(ataid
[pccb
->target
]->lba_capacity
);
624 memcpy(pccb
->pdata
, &cap
, sizeof(cap
));
626 pccb
->pdata
[4] = pccb
->pdata
[5] = 0;
627 pccb
->pdata
[6] = 512 >> 8;
628 pccb
->pdata
[7] = 512 & 0xff;
635 * SCSI TEST UNIT READY command operation.
637 static int ata_scsiop_test_unit_ready(ccb
*pccb
)
639 return (ataid
[pccb
->target
]) ? 0 : -EPERM
;
643 int scsi_exec(ccb
*pccb
)
647 switch (pccb
->cmd
[0]) {
649 ret
= ata_scsiop_read10(pccb
);
652 ret
= ata_scsiop_read_capacity10(pccb
);
655 ret
= ata_scsiop_test_unit_ready(pccb
);
658 ret
= ata_scsiop_inquiry(pccb
);
661 printf("Unsupport SCSI command 0x%02x\n", pccb
->cmd
[0]);
666 debug("SCSI command 0x%02x ret errno %d\n", pccb
->cmd
[0], ret
);
674 void scsi_low_level_init(int busdevfunc
)
679 #ifndef CONFIG_SCSI_AHCI_PLAT
680 ahci_init_one(busdevfunc
);
683 linkmap
= probe_ent
->link_port_map
;
685 for (i
= 0; i
< CONFIG_SYS_SCSI_MAX_SCSI_ID
; i
++) {
686 if (((linkmap
>> i
) & 0x01)) {
687 if (ahci_port_start((u8
) i
)) {
688 printf("Can not start port %d\n", i
);
691 ahci_set_feature((u8
) i
);
696 #ifdef CONFIG_SCSI_AHCI_PLAT
697 int ahci_init(u32 base
)
702 memset(ataid
, 0, sizeof(ataid
));
704 probe_ent
= malloc(sizeof(struct ahci_probe_ent
));
705 memset(probe_ent
, 0, sizeof(struct ahci_probe_ent
));
707 probe_ent
->host_flags
= ATA_FLAG_SATA
712 probe_ent
->pio_mask
= 0x1f;
713 probe_ent
->udma_mask
= 0x7f; /*Fixme,assume to support UDMA6 */
715 probe_ent
->mmio_base
= base
;
717 /* initialize adapter */
718 rc
= ahci_host_init(probe_ent
);
722 ahci_print_info(probe_ent
);
724 linkmap
= probe_ent
->link_port_map
;
726 for (i
= 0; i
< CONFIG_SYS_SCSI_MAX_SCSI_ID
; i
++) {
727 if (((linkmap
>> i
) & 0x01)) {
728 if (ahci_port_start((u8
) i
)) {
729 printf("Can not start port %d\n", i
);
732 ahci_set_feature((u8
) i
);
740 void scsi_bus_reset(void)
746 void scsi_print_error(ccb
* pccb
)
748 /*The ahci error info can be read in the ahci driver*/