]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - drivers/block/ahci.c
ahci: Support splitting of read transactions into multiple chunks
[karo-tx-uboot.git] / drivers / block / ahci.c
index 3d82c625a353dfc36ec8bddeb6856cca5e9a5317..d94da1f05a2fc4027e0a6c4ebb6a9480fd8bb7f3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) Freescale Semiconductor, Inc. 2006. All rights reserved.
+ * Copyright (C) Freescale Semiconductor, Inc. 2006.
  * Author: Jason Jin<Jason.jin@freescale.com>
  *         Zhang Wei<wei.zhang@freescale.com>
  *
@@ -26,8 +26,6 @@
  */
 #include <common.h>
 
-#ifdef CONFIG_SCSI_AHCI
-
 #include <command.h>
 #include <pci.h>
 #include <asm/processor.h>
@@ -44,6 +42,14 @@ hd_driveid_t *ataid[AHCI_MAX_PORTS];
 
 #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
 
+/*
+ * Some controllers limit number of blocks they can read at once. Contemporary
+ * SSD devices work much faster if the read size is aligned to a power of 2.
+ * Let's set default to 128 and allowing to be overwritten if needed.
+ */
+#ifndef MAX_SATA_BLOCKS_READ
+#define MAX_SATA_BLOCKS_READ   0x80
+#endif
 
 static inline u32 ahci_port_base(u32 base, u32 port)
 {
@@ -80,13 +86,17 @@ static int waiting_for_cmd_completed(volatile u8 *offset,
 
 static int ahci_host_init(struct ahci_probe_ent *probe_ent)
 {
+#ifndef CONFIG_SCSI_AHCI_PLAT
        pci_dev_t pdev = probe_ent->dev;
+       u16 tmp16;
+       unsigned short vendor;
+#endif
        volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
        u32 tmp, cap_save;
-       u16 tmp16;
        int i, j;
        volatile u8 *port_mmio;
-       unsigned short vendor;
+
+       debug("ahci_host_init: start\n");
 
        cap_save = readl(mmio + HOST_CAP);
        cap_save &= ((1 << 28) | (1 << 17));
@@ -112,6 +122,7 @@ static int ahci_host_init(struct ahci_probe_ent *probe_ent)
        writel(cap_save, mmio + HOST_CAP);
        writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
 
+#ifndef CONFIG_SCSI_AHCI_PLAT
        pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
 
        if (vendor == PCI_VENDOR_ID_INTEL) {
@@ -120,7 +131,7 @@ static int ahci_host_init(struct ahci_probe_ent *probe_ent)
                tmp16 |= 0xf;
                pci_write_config_word(pdev, 0x92, tmp16);
        }
-
+#endif
        probe_ent->cap = readl(mmio + HOST_CAP);
        probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
        probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
@@ -128,6 +139,9 @@ static int ahci_host_init(struct ahci_probe_ent *probe_ent)
        debug("cap 0x%x  port_map 0x%x  n_ports %d\n",
              probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
 
+       if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
+               probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
+
        for (i = 0; i < probe_ent->n_ports; i++) {
                probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
                port_mmio = (u8 *) probe_ent->port[i].port_mmio;
@@ -185,22 +199,24 @@ static int ahci_host_init(struct ahci_probe_ent *probe_ent)
        writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
        tmp = readl(mmio + HOST_CTL);
        debug("HOST_CTL 0x%x\n", tmp);
-
+#ifndef CONFIG_SCSI_AHCI_PLAT
        pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
        tmp |= PCI_COMMAND_MASTER;
        pci_write_config_word(pdev, PCI_COMMAND, tmp16);
-
+#endif
        return 0;
 }
 
 
 static void ahci_print_info(struct ahci_probe_ent *probe_ent)
 {
+#ifndef CONFIG_SCSI_AHCI_PLAT
        pci_dev_t pdev = probe_ent->dev;
+       u16 cc;
+#endif
        volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
        u32 vers, cap, impl, speed;
        const char *speed_s;
-       u16 cc;
        const char *scc_s;
 
        vers = readl(mmio + HOST_VERSION);
@@ -215,6 +231,9 @@ static void ahci_print_info(struct ahci_probe_ent *probe_ent)
        else
                speed_s = "?";
 
+#ifdef CONFIG_SCSI_AHCI_PLAT
+       scc_s = "SATA";
+#else
        pci_read_config_word(pdev, 0x0a, &cc);
        if (cc == 0x0101)
                scc_s = "IDE";
@@ -224,7 +243,7 @@ static void ahci_print_info(struct ahci_probe_ent *probe_ent)
                scc_s = "RAID";
        else
                scc_s = "unknown";
-
+#endif
        printf("AHCI %02x%02x.%02x%02x "
               "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
               (vers >> 24) & 0xff,
@@ -251,9 +270,9 @@ static void ahci_print_info(struct ahci_probe_ent *probe_ent)
               cap & (1 << 13) ? "part " : "");
 }
 
+#ifndef CONFIG_SCSI_AHCI_PLAT
 static int ahci_init_one(pci_dev_t pdev)
 {
-       u32 iobase;
        u16 vendor;
        int rc;
 
@@ -263,9 +282,6 @@ static int ahci_init_one(pci_dev_t pdev)
        memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
        probe_ent->dev = pdev;
 
-       pci_read_config_dword(pdev, AHCI_PCI_BAR, &iobase);
-       iobase &= ~0xf;
-
        probe_ent->host_flags = ATA_FLAG_SATA
                                | ATA_FLAG_NO_LEGACY
                                | ATA_FLAG_MMIO
@@ -274,7 +290,8 @@ static int ahci_init_one(pci_dev_t pdev)
        probe_ent->pio_mask = 0x1f;
        probe_ent->udma_mask = 0x7f;    /*Fixme,assume to support UDMA6 */
 
-       probe_ent->mmio_base = iobase;
+       pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &probe_ent->mmio_base);
+       debug("ahci mmio_base=0x%08x\n", probe_ent->mmio_base);
 
        /* Take from kernel:
         * JMicron-specific fixup:
@@ -296,7 +313,7 @@ static int ahci_init_one(pci_dev_t pdev)
       err_out:
        return rc;
 }
-
+#endif
 
 #define MAX_DATA_BYTE_COUNT  (4*1024*1024)
 
@@ -394,7 +411,7 @@ static int ahci_port_start(u8 port)
         * 32 bytes each in size
         */
        pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
-       debug("cmd_slot = 0x%x\n", pp->cmd_slot);
+       debug("cmd_slot = 0x%x\n", (unsigned)pp->cmd_slot);
        mem += (AHCI_CMD_SLOT_SZ + 224);
 
        /*
@@ -473,7 +490,7 @@ static char *ata_id_strcpy(u16 *target, u16 *src, int len)
 {
        int i;
        for (i = 0; i < len / 2; i++)
-               target[i] = le16_to_cpu(src[i]);
+               target[i] = swab16(src[i]);
        return (char *)target;
 }
 
@@ -557,45 +574,69 @@ static int ata_scsiop_inquiry(ccb *pccb)
  */
 static int ata_scsiop_read10(ccb * pccb)
 {
-       u64 lba = 0;
-       u32 len = 0;
+       u32 lba = 0;
+       u16 blocks = 0;
        u8 fis[20];
+       u8 *user_buffer = pccb->pdata;
+       u32 user_buffer_size = pccb->datalen;
 
-       lba = (((u64) pccb->cmd[2]) << 24) | (((u64) pccb->cmd[3]) << 16)
-           | (((u64) pccb->cmd[4]) << 8) | ((u64) pccb->cmd[5]);
-       len = (((u32) pccb->cmd[7]) << 8) | ((u32) pccb->cmd[8]);
+       /* Retrieve the base LBA number from the ccb structure. */
+       memcpy(&lba, pccb->cmd + 2, sizeof(lba));
+       lba = be32_to_cpu(lba);
 
-       /* For 10-byte and 16-byte SCSI R/W commands, transfer
+       /*
+        * And the number of blocks.
+        *
+        * For 10-byte and 16-byte SCSI R/W commands, transfer
         * length 0 means transfer 0 block of data.
         * However, for ATA R/W commands, sector count 0 means
         * 256 or 65536 sectors, not 0 sectors as in SCSI.
         *
         * WARNING: one or two older ATA drives treat 0 as 0...
         */
-       if (!len)
-               return 0;
+       blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
+
+       debug("scsi_ahci: read %d blocks starting from lba 0x%x\n",
+             (unsigned)lba, blocks);
+
+       /* Preset the FIS */
        memset(fis, 0, 20);
+       fis[0] = 0x27;           /* Host to device FIS. */
+       fis[1] = 1 << 7;         /* Command FIS. */
+       fis[2] = ATA_CMD_RD_DMA; /* Command byte. */
 
-       /* Construct the FIS */
-       fis[0] = 0x27;          /* Host to device FIS. */
-       fis[1] = 1 << 7;        /* Command FIS. */
-       fis[2] = ATA_CMD_RD_DMA;        /* Command byte. */
-
-       /* LBA address, only support LBA28 in this driver */
-       fis[4] = pccb->cmd[5];
-       fis[5] = pccb->cmd[4];
-       fis[6] = pccb->cmd[3];
-       fis[7] = (pccb->cmd[2] & 0x0f) | 0xe0;
-
-       /* Sector Count */
-       fis[12] = pccb->cmd[8];
-       fis[13] = pccb->cmd[7];
-
-       /* Read from ahci */
-       if (get_ahci_device_data(pccb->target, (u8 *) & fis, 20,
-                                pccb->pdata, pccb->datalen)) {
-               debug("scsi_ahci: SCSI READ10 command failure.\n");
-               return -EIO;
+       while (blocks) {
+               u16 now_blocks; /* number of blocks per iteration */
+               u32 transfer_size; /* number of bytes per iteration */
+
+               now_blocks = min(MAX_SATA_BLOCKS_READ, blocks);
+
+               transfer_size = ATA_BLOCKSIZE * now_blocks;
+               if (transfer_size > user_buffer_size) {
+                       printf("scsi_ahci: Error: buffer too small.\n");
+                       return -EIO;
+               }
+
+               /* LBA address, only support LBA28 in this driver */
+               fis[4] = (lba >> 0) & 0xff;
+               fis[5] = (lba >> 8) & 0xff;
+               fis[6] = (lba >> 16) & 0xff;
+               fis[7] = ((lba >> 24) & 0xf) | 0xe0;
+
+               /* Block (sector) count */
+               fis[12] = (now_blocks >> 0) & 0xff;
+               fis[13] = (now_blocks >> 8) & 0xff;
+
+               /* Read from ahci */
+               if (get_ahci_device_data(pccb->target, (u8 *) &fis, sizeof(fis),
+                                        user_buffer, user_buffer_size)) {
+                       debug("scsi_ahci: SCSI READ10 command failure.\n");
+                       return -EIO;
+               }
+               user_buffer += transfer_size;
+               user_buffer_size -= transfer_size;
+               blocks -= now_blocks;
+               lba += now_blocks;
        }
 
        return 0;
@@ -607,7 +648,7 @@ static int ata_scsiop_read10(ccb * pccb)
  */
 static int ata_scsiop_read_capacity10(ccb *pccb)
 {
-       u8 buf[8];
+       u32 cap;
 
        if (!ataid[pccb->target]) {
                printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
@@ -616,14 +657,12 @@ static int ata_scsiop_read_capacity10(ccb *pccb)
                return -EPERM;
        }
 
-       memset(buf, 0, 8);
-
-       *(u32 *) buf = le32_to_cpu(ataid[pccb->target]->lba_capacity);
+       cap = be32_to_cpu(ataid[pccb->target]->lba_capacity);
+       memcpy(pccb->pdata, &cap, sizeof(cap));
 
-       buf[6] = 512 >> 8;
-       buf[7] = 512 & 0xff;
-
-       memcpy(pccb->pdata, buf, 8);
+       pccb->pdata[4] = pccb->pdata[5] = 0;
+       pccb->pdata[6] = 512 >> 8;
+       pccb->pdata[7] = 512 & 0xff;
 
        return 0;
 }
@@ -674,11 +713,13 @@ void scsi_low_level_init(int busdevfunc)
        int i;
        u32 linkmap;
 
+#ifndef CONFIG_SCSI_AHCI_PLAT
        ahci_init_one(busdevfunc);
+#endif
 
        linkmap = probe_ent->link_port_map;
 
-       for (i = 0; i < CFG_SCSI_MAX_SCSI_ID; i++) {
+       for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
                if (((linkmap >> i) & 0x01)) {
                        if (ahci_port_start((u8) i)) {
                                printf("Can not start port %d\n", i);
@@ -689,6 +730,49 @@ void scsi_low_level_init(int busdevfunc)
        }
 }
 
+#ifdef CONFIG_SCSI_AHCI_PLAT
+int ahci_init(u32 base)
+{
+       int i, rc = 0;
+       u32 linkmap;
+
+       memset(ataid, 0, sizeof(ataid));
+
+       probe_ent = malloc(sizeof(struct ahci_probe_ent));
+       memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
+
+       probe_ent->host_flags = ATA_FLAG_SATA
+                               | ATA_FLAG_NO_LEGACY
+                               | ATA_FLAG_MMIO
+                               | ATA_FLAG_PIO_DMA
+                               | ATA_FLAG_NO_ATAPI;
+       probe_ent->pio_mask = 0x1f;
+       probe_ent->udma_mask = 0x7f;    /*Fixme,assume to support UDMA6 */
+
+       probe_ent->mmio_base = base;
+
+       /* initialize adapter */
+       rc = ahci_host_init(probe_ent);
+       if (rc)
+               goto err_out;
+
+       ahci_print_info(probe_ent);
+
+       linkmap = probe_ent->link_port_map;
+
+       for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
+               if (((linkmap >> i) & 0x01)) {
+                       if (ahci_port_start((u8) i)) {
+                               printf("Can not start port %d\n", i);
+                               continue;
+                       }
+                       ahci_set_feature((u8) i);
+               }
+       }
+err_out:
+       return rc;
+}
+#endif
 
 void scsi_bus_reset(void)
 {
@@ -700,4 +784,3 @@ void scsi_print_error(ccb * pccb)
 {
        /*The ahci error info can be read in the ahci driver*/
 }
-#endif