uboot/drivers/block/dwc_ahsata.c
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc.
   3 * Terry Lv <r65388@freescale.com>
   4 *
   5 * See file CREDITS for list of people who contributed to this
   6 * project.
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License as
  10 * published by the Free Software Foundation; either version 2 of
  11 * the License, or (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc.
  21 *
  22 */
  23
  24#include <libata.h>
  25#include <ahci.h>
  26#include <fis.h>
  27
  28#include <common.h>
  29#include <malloc.h>
  30#include <linux/ctype.h>
  31#include <asm/errno.h>
  32#include <asm/io.h>
  33#include <linux/bitops.h>
  34#include <asm/arch/clock.h>
  35#include "dwc_ahsata.h"
  36
  37struct sata_port_regs {
  38        u32 clb;
  39        u32 clbu;
  40        u32 fb;
  41        u32 fbu;
  42        u32 is;
  43        u32 ie;
  44        u32 cmd;
  45        u32 res1[1];
  46        u32 tfd;
  47        u32 sig;
  48        u32 ssts;
  49        u32 sctl;
  50        u32 serr;
  51        u32 sact;
  52        u32 ci;
  53        u32 sntf;
  54        u32 res2[1];
  55        u32 dmacr;
  56        u32 res3[1];
  57        u32 phycr;
  58        u32 physr;
  59};
  60
  61struct sata_host_regs {
  62        u32 cap;
  63        u32 ghc;
  64        u32 is;
  65        u32 pi;
  66        u32 vs;
  67        u32 ccc_ctl;
  68        u32 ccc_ports;
  69        u32 res1[2];
  70        u32 cap2;
  71        u32 res2[30];
  72        u32 bistafr;
  73        u32 bistcr;
  74        u32 bistfctr;
  75        u32 bistsr;
  76        u32 bistdecr;
  77        u32 res3[2];
  78        u32 oobr;
  79        u32 res4[8];
  80        u32 timer1ms;
  81        u32 res5[1];
  82        u32 gparam1r;
  83        u32 gparam2r;
  84        u32 pparamr;
  85        u32 testr;
  86        u32 versionr;
  87        u32 idr;
  88};
  89
  90#define MAX_DATA_BYTES_PER_SG  (4 * 1024 * 1024)
  91#define MAX_BYTES_PER_TRANS (AHCI_MAX_SG * MAX_DATA_BYTES_PER_SG)
  92
  93#define writel_with_flush(a, b) do { writel(a, b); readl(b); } while (0)
  94
  95static int is_ready;
  96
  97static inline u32 ahci_port_base(u32 base, u32 port)
  98{
  99        return base + 0x100 + (port * 0x80);
 100}
 101
 102static int waiting_for_cmd_completed(u8 *offset,
 103                                        int timeout_msec,
 104                                        u32 sign)
 105{
 106        int i;
 107        u32 status;
 108
 109        for (i = 0;
 110                ((status = readl(offset)) & sign) && i < timeout_msec;
 111                ++i)
 112                mdelay(1);
 113
 114        return (i < timeout_msec) ? 0 : -1;
 115}
 116
 117static int ahci_setup_oobr(struct ahci_probe_ent *probe_ent,
 118                                                int clk)
 119{
 120        struct sata_host_regs *host_mmio =
 121                (struct sata_host_regs *)probe_ent->mmio_base;
 122
 123        writel(SATA_HOST_OOBR_WE, &(host_mmio->oobr));
 124        writel(0x02060b14, &(host_mmio->oobr));
 125
 126        return 0;
 127}
 128
 129static int ahci_host_init(struct ahci_probe_ent *probe_ent)
 130{
 131        u32 tmp, cap_save, num_ports;
 132        int i, j, timeout = 1000;
 133        struct sata_port_regs *port_mmio = NULL;
 134        struct sata_host_regs *host_mmio =
 135                (struct sata_host_regs *)probe_ent->mmio_base;
 136        int clk = mxc_get_clock(MXC_SATA_CLK);
 137
 138        cap_save = readl(&(host_mmio->cap));
 139        cap_save |= SATA_HOST_CAP_SSS;
 140
 141        /* global controller reset */
 142        tmp = readl(&(host_mmio->ghc));
 143        if ((tmp & SATA_HOST_GHC_HR) == 0)
 144                writel_with_flush(tmp | SATA_HOST_GHC_HR, &(host_mmio->ghc));
 145
 146        while ((readl(&(host_mmio->ghc)) & SATA_HOST_GHC_HR)
 147                && --timeout)
 148                ;
 149
 150        if (timeout <= 0) {
 151                debug("controller reset failed (0x%x)\n", tmp);
 152                return -1;
 153        }
 154
 155        /* Set timer 1ms */
 156        writel(clk / 1000, &(host_mmio->timer1ms));
 157
 158        ahci_setup_oobr(probe_ent, 0);
 159
 160        writel_with_flush(SATA_HOST_GHC_AE, &(host_mmio->ghc));
 161        writel(cap_save, &(host_mmio->cap));
 162        num_ports = (cap_save & SATA_HOST_CAP_NP_MASK) + 1;
 163        writel_with_flush((1 << num_ports) - 1,
 164                                &(host_mmio->pi));
 165
 166        /*
 167         * Determine which Ports are implemented by the DWC_ahsata,
 168         * by reading the PI register. This bit map value aids the
 169         * software to determine how many Ports are available and
 170         * which Port registers need to be initialized.
 171         */
 172        probe_ent->cap = readl(&(host_mmio->cap));
 173        probe_ent->port_map = readl(&(host_mmio->pi));
 174
 175        /* Determine how many command slots the HBA supports */
 176        probe_ent->n_ports =
 177                (probe_ent->cap & SATA_HOST_CAP_NP_MASK) + 1;
 178
 179        debug("cap 0x%x  port_map 0x%x  n_ports %d\n",
 180                probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
 181
 182        for (i = 0; i < probe_ent->n_ports; i++) {
 183                probe_ent->port[i].port_mmio =
 184                        ahci_port_base((u32)host_mmio, i);
 185                port_mmio =
 186                        (struct sata_port_regs *)probe_ent->port[i].port_mmio;
 187
 188                /* Ensure that the DWC_ahsata is in idle state */
 189                tmp = readl(&(port_mmio->cmd));
 190
 191                /*
 192                 * When P#CMD.ST, P#CMD.CR, P#CMD.FRE and P#CMD.FR
 193                 * are all cleared, the Port is in an idle state.
 194                 */
 195                if (tmp & (SATA_PORT_CMD_CR | SATA_PORT_CMD_FR |
 196                        SATA_PORT_CMD_FRE | SATA_PORT_CMD_ST)) {
 197
 198                        /*
 199                         * System software places a Port into the idle state by
 200                         * clearing P#CMD.ST and waiting for P#CMD.CR to return
 201                         * 0 when read.
 202                         */
 203                        tmp &= ~SATA_PORT_CMD_ST;
 204                        writel_with_flush(tmp, &(port_mmio->cmd));
 205
 206                        /*
 207                         * spec says 500 msecs for each bit, so
 208                         * this is slightly incorrect.
 209                         */
 210                        mdelay(500);
 211
 212                        timeout = 1000;
 213                        while ((readl(&(port_mmio->cmd)) & SATA_PORT_CMD_CR)
 214                                && --timeout)
 215                                ;
 216
 217                        if (timeout <= 0) {
 218                                debug("port reset failed (0x%x)\n", tmp);
 219                                return -1;
 220                        }
 221                }
 222
 223                /* Spin-up device */
 224                tmp = readl(&(port_mmio->cmd));
 225                writel((tmp | SATA_PORT_CMD_SUD), &(port_mmio->cmd));
 226
 227                /* Wait for spin-up to finish */
 228                timeout = 1000;
 229                while (!(readl(&(port_mmio->cmd)) | SATA_PORT_CMD_SUD)
 230                        && --timeout)
 231                        ;
 232                if (timeout <= 0) {
 233                        debug("Spin-Up can't finish!\n");
 234                        return -1;
 235                }
 236
 237                for (j = 0; j < 100; ++j) {
 238                        mdelay(10);
 239                        tmp = readl(&(port_mmio->ssts));
 240                        if (((tmp & SATA_PORT_SSTS_DET_MASK) == 0x3) ||
 241                                ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x1))
 242                                break;
 243                }
 244
 245                /* Wait for COMINIT bit 26 (DIAG_X) in SERR */
 246                timeout = 1000;
 247                while (!(readl(&(port_mmio->serr)) | SATA_PORT_SERR_DIAG_X)
 248                        && --timeout)
 249                        ;
 250                if (timeout <= 0) {
 251                        debug("Can't find DIAG_X set!\n");
 252                        return -1;
 253                }
 254
 255                /*
 256                 * For each implemented Port, clear the P#SERR
 257                 * register, by writing ones to each implemented\
 258                 * bit location.
 259                 */
 260                tmp = readl(&(port_mmio->serr));
 261                debug("P#SERR 0x%x\n",
 262                                tmp);
 263                writel(tmp, &(port_mmio->serr));
 264
 265                /* Ack any pending irq events for this port */
 266                tmp = readl(&(host_mmio->is));
 267                debug("IS 0x%x\n", tmp);
 268                if (tmp)
 269                        writel(tmp, &(host_mmio->is));
 270
 271                writel(1 << i, &(host_mmio->is));
 272
 273                /* set irq mask (enables interrupts) */
 274                writel(DEF_PORT_IRQ, &(port_mmio->ie));
 275
 276                /* register linkup ports */
 277                tmp = readl(&(port_mmio->ssts));
 278                debug("Port %d status: 0x%x\n", i, tmp);
 279                if ((tmp & SATA_PORT_SSTS_DET_MASK) == 0x03)
 280                        probe_ent->link_port_map |= (0x01 << i);
 281        }
 282
 283        tmp = readl(&(host_mmio->ghc));
 284        debug("GHC 0x%x\n", tmp);
 285        writel(tmp | SATA_HOST_GHC_IE, &(host_mmio->ghc));
 286        tmp = readl(&(host_mmio->ghc));
 287        debug("GHC 0x%x\n", tmp);
 288
 289        return 0;
 290}
 291
 292static void ahci_print_info(struct ahci_probe_ent *probe_ent)
 293{
 294        struct sata_host_regs *host_mmio =
 295                (struct sata_host_regs *)probe_ent->mmio_base;
 296        u32 vers, cap, impl, speed;
 297        const char *speed_s;
 298        const char *scc_s;
 299
 300        vers = readl(&(host_mmio->vs));
 301        cap = probe_ent->cap;
 302        impl = probe_ent->port_map;
 303
 304        speed = (cap & SATA_HOST_CAP_ISS_MASK)
 305                >> SATA_HOST_CAP_ISS_OFFSET;
 306        if (speed == 1)
 307                speed_s = "1.5";
 308        else if (speed == 2)
 309                speed_s = "3";
 310        else
 311                speed_s = "?";
 312
 313        scc_s = "SATA";
 314
 315        printf("AHCI %02x%02x.%02x%02x "
 316                "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
 317                (vers >> 24) & 0xff,
 318                (vers >> 16) & 0xff,
 319                (vers >> 8) & 0xff,
 320                vers & 0xff,
 321                ((cap >> 8) & 0x1f) + 1,
 322                (cap & 0x1f) + 1,
 323                speed_s,
 324                impl,
 325                scc_s);
 326
 327        printf("flags: "
 328                "%s%s%s%s%s%s"
 329                "%s%s%s%s%s%s%s\n",
 330                cap & (1 << 31) ? "64bit " : "",
 331                cap & (1 << 30) ? "ncq " : "",
 332                cap & (1 << 28) ? "ilck " : "",
 333                cap & (1 << 27) ? "stag " : "",
 334                cap & (1 << 26) ? "pm " : "",
 335                cap & (1 << 25) ? "led " : "",
 336                cap & (1 << 24) ? "clo " : "",
 337                cap & (1 << 19) ? "nz " : "",
 338                cap & (1 << 18) ? "only " : "",
 339                cap & (1 << 17) ? "pmp " : "",
 340                cap & (1 << 15) ? "pio " : "",
 341                cap & (1 << 14) ? "slum " : "",
 342                cap & (1 << 13) ? "part " : "");
 343}
 344
 345static int ahci_init_one(int pdev)
 346{
 347        int rc;
 348        struct ahci_probe_ent *probe_ent = NULL;
 349
 350        probe_ent = malloc(sizeof(struct ahci_probe_ent));
 351        memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
 352        probe_ent->dev = pdev;
 353
 354        probe_ent->host_flags = ATA_FLAG_SATA
 355                                | ATA_FLAG_NO_LEGACY
 356                                | ATA_FLAG_MMIO
 357                                | ATA_FLAG_PIO_DMA
 358                                | ATA_FLAG_NO_ATAPI;
 359
 360        probe_ent->mmio_base = CONFIG_DWC_AHSATA_BASE_ADDR;
 361
 362        /* initialize adapter */
 363        rc = ahci_host_init(probe_ent);
 364        if (rc)
 365                goto err_out;
 366
 367        ahci_print_info(probe_ent);
 368
 369        /* Save the private struct to block device struct */
 370        sata_dev_desc[pdev].priv = (void *)probe_ent;
 371
 372        return 0;
 373
 374err_out:
 375        return rc;
 376}
 377
 378static int ahci_fill_sg(struct ahci_probe_ent *probe_ent,
 379                        u8 port, unsigned char *buf, int buf_len)
 380{
 381        struct ahci_ioports *pp = &(probe_ent->port[port]);
 382        struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
 383        u32 sg_count, max_bytes;
 384        int i;
 385
 386        max_bytes = MAX_DATA_BYTES_PER_SG;
 387        sg_count = ((buf_len - 1) / max_bytes) + 1;
 388        if (sg_count > AHCI_MAX_SG) {
 389                printf("Error:Too much sg!\n");
 390                return -1;
 391        }
 392
 393        for (i = 0; i < sg_count; i++) {
 394                ahci_sg->addr =
 395                        cpu_to_le32((u32)buf + i * max_bytes);
 396                ahci_sg->addr_hi = 0;
 397                ahci_sg->flags_size = cpu_to_le32(0x3fffff &
 398                                        (buf_len < max_bytes
 399                                        ? (buf_len - 1)
 400                                        : (max_bytes - 1)));
 401                ahci_sg++;
 402                buf_len -= max_bytes;
 403        }
 404
 405        return sg_count;
 406}
 407
 408static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 cmd_slot, u32 opts)
 409{
 410        struct ahci_cmd_hdr *cmd_hdr = (struct ahci_cmd_hdr *)(pp->cmd_slot +
 411                                        AHCI_CMD_SLOT_SZ * cmd_slot);
 412
 413        memset(cmd_hdr, 0, AHCI_CMD_SLOT_SZ);
 414        cmd_hdr->opts = cpu_to_le32(opts);
 415        cmd_hdr->status = 0;
 416        cmd_hdr->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
 417        cmd_hdr->tbl_addr_hi = 0;
 418}
 419
 420#define AHCI_GET_CMD_SLOT(c) ((c) ? ffs(c) : 0)
 421
 422static int ahci_exec_ata_cmd(struct ahci_probe_ent *probe_ent,
 423                u8 port, struct sata_fis_h2d *cfis,
 424                u8 *buf, u32 buf_len, s32 is_write)
 425{
 426        struct ahci_ioports *pp = &(probe_ent->port[port]);
 427        struct sata_port_regs *port_mmio =
 428                        (struct sata_port_regs *)pp->port_mmio;
 429        u32 opts;
 430        int sg_count = 0, cmd_slot = 0;
 431
 432        cmd_slot = AHCI_GET_CMD_SLOT(readl(&(port_mmio->ci)));
 433        if (32 == cmd_slot) {
 434                printf("Can't find empty command slot!\n");
 435                return 0;
 436        }
 437
 438        /* Check xfer length */
 439        if (buf_len > MAX_BYTES_PER_TRANS) {
 440                printf("Max transfer length is %dB\n\r",
 441                        MAX_BYTES_PER_TRANS);
 442                return 0;
 443        }
 444
 445        memcpy((u8 *)(pp->cmd_tbl), cfis, sizeof(struct sata_fis_h2d));
 446        if (buf && buf_len)
 447                sg_count = ahci_fill_sg(probe_ent, port, buf, buf_len);
 448        opts = (sizeof(struct sata_fis_h2d) >> 2) | (sg_count << 16);
 449        if (is_write)
 450                opts |= 0x40;
 451        ahci_fill_cmd_slot(pp, cmd_slot, opts);
 452
 453        writel_with_flush(1 << cmd_slot, &(port_mmio->ci));
 454
 455        if (waiting_for_cmd_completed((u8 *)&(port_mmio->ci),
 456                                10000, 0x1 << cmd_slot)) {
 457                printf("timeout exit!\n");
 458                return -1;
 459        }
 460        debug("ahci_exec_ata_cmd: %d byte transferred.\n",
 461              pp->cmd_slot->status);
 462
 463        return buf_len;
 464}
 465
 466static void ahci_set_feature(u8 dev, u8 port)
 467{
 468        struct ahci_probe_ent *probe_ent =
 469                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 470        struct sata_fis_h2d h2d, *cfis = &h2d;
 471
 472        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 473        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 474        cfis->pm_port_c = 1 << 7;
 475        cfis->command = ATA_CMD_SET_FEATURES;
 476        cfis->features = SETFEATURES_XFER;
 477        cfis->sector_count = ffs(probe_ent->udma_mask + 1) + 0x3e;
 478
 479        ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, READ_CMD);
 480}
 481
 482static int ahci_port_start(struct ahci_probe_ent *probe_ent,
 483                                        u8 port)
 484{
 485        struct ahci_ioports *pp = &(probe_ent->port[port]);
 486        struct sata_port_regs *port_mmio =
 487                (struct sata_port_regs *)pp->port_mmio;
 488        u32 port_status;
 489        u32 mem;
 490        int timeout = 10000000;
 491
 492        debug("Enter start port: %d\n", port);
 493        port_status = readl(&(port_mmio->ssts));
 494        debug("Port %d status: %x\n", port, port_status);
 495        if ((port_status & 0xf) != 0x03) {
 496                printf("No Link on this port!\n");
 497                return -1;
 498        }
 499
 500        mem = (u32)malloc(AHCI_PORT_PRIV_DMA_SZ + 1024);
 501        if (!mem) {
 502                free(pp);
 503                printf("No mem for table!\n");
 504                return -ENOMEM;
 505        }
 506
 507        mem = (mem + 0x400) & (~0x3ff); /* Aligned to 1024-bytes */
 508        memset((u8 *)mem, 0, AHCI_PORT_PRIV_DMA_SZ);
 509
 510        /*
 511         * First item in chunk of DMA memory: 32-slot command table,
 512         * 32 bytes each in size
 513         */
 514        pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
 515        debug("cmd_slot = 0x%x\n", (unsigned int) pp->cmd_slot);
 516        mem += (AHCI_CMD_SLOT_SZ * DWC_AHSATA_MAX_CMD_SLOTS);
 517
 518        /*
 519         * Second item: Received-FIS area, 256-Byte aligned
 520         */
 521        pp->rx_fis = mem;
 522        mem += AHCI_RX_FIS_SZ;
 523
 524        /*
 525         * Third item: data area for storing a single command
 526         * and its scatter-gather table
 527         */
 528        pp->cmd_tbl = mem;
 529        debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
 530
 531        mem += AHCI_CMD_TBL_HDR;
 532
 533        writel_with_flush(0x00004444, &(port_mmio->dmacr));
 534        pp->cmd_tbl_sg = (struct ahci_sg *)mem;
 535        writel_with_flush((u32)pp->cmd_slot, &(port_mmio->clb));
 536        writel_with_flush(pp->rx_fis, &(port_mmio->fb));
 537
 538        /* Enable FRE */
 539        writel_with_flush((SATA_PORT_CMD_FRE | readl(&(port_mmio->cmd))),
 540                        &(port_mmio->cmd));
 541
 542        /* Wait device ready */
 543        while ((readl(&(port_mmio->tfd)) & (SATA_PORT_TFD_STS_ERR |
 544                SATA_PORT_TFD_STS_DRQ | SATA_PORT_TFD_STS_BSY))
 545                && --timeout)
 546                ;
 547        if (timeout <= 0) {
 548                debug("Device not ready for BSY, DRQ and"
 549                        "ERR in TFD!\n");
 550                return -1;
 551        }
 552
 553        writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
 554                          PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
 555                          PORT_CMD_START, &(port_mmio->cmd));
 556
 557        debug("Exit start port %d\n", port);
 558
 559        return 0;
 560}
 561
 562int init_sata(int dev)
 563{
 564        int i;
 565        u32 linkmap;
 566        struct ahci_probe_ent *probe_ent = NULL;
 567
 568        if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
 569                printf("The sata index %d is out of ranges\n\r", dev);
 570                return -1;
 571        }
 572
 573        ahci_init_one(dev);
 574
 575        probe_ent = (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 576        linkmap = probe_ent->link_port_map;
 577
 578        if (0 == linkmap) {
 579                printf("No port device detected!\n");
 580                return 1;
 581        }
 582
 583        for (i = 0; i < probe_ent->n_ports; i++) {
 584                if ((linkmap >> i) && ((linkmap >> i) & 0x01)) {
 585                        if (ahci_port_start(probe_ent, (u8)i)) {
 586                                printf("Can not start port %d\n", i);
 587                                return 1;
 588                        }
 589                        probe_ent->hard_port_no = i;
 590                        break;
 591                }
 592        }
 593
 594        return 0;
 595}
 596
 597static void dwc_ahsata_print_info(int dev)
 598{
 599        block_dev_desc_t *pdev = &(sata_dev_desc[dev]);
 600
 601        printf("SATA Device Info:\n\r");
 602#ifdef CONFIG_SYS_64BIT_LBA
 603        printf("S/N: %s\n\rProduct model number: %s\n\r"
 604                "Firmware version: %s\n\rCapacity: %lld sectors\n\r",
 605                pdev->product, pdev->vendor, pdev->revision, pdev->lba);
 606#else
 607        printf("S/N: %s\n\rProduct model number: %s\n\r"
 608                "Firmware version: %s\n\rCapacity: %ld sectors\n\r",
 609                pdev->product, pdev->vendor, pdev->revision, pdev->lba);
 610#endif
 611}
 612
 613static void dwc_ahsata_identify(int dev, u16 *id)
 614{
 615        struct ahci_probe_ent *probe_ent =
 616                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 617        struct sata_fis_h2d h2d, *cfis = &h2d;
 618        u8 port = probe_ent->hard_port_no;
 619
 620        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 621
 622        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 623        cfis->pm_port_c = 0x80; /* is command */
 624        cfis->command = ATA_CMD_ID_ATA;
 625
 626        ahci_exec_ata_cmd(probe_ent, port, cfis,
 627                        (u8 *)id, ATA_ID_WORDS * 2, READ_CMD);
 628        ata_swap_buf_le16(id, ATA_ID_WORDS);
 629}
 630
 631static void dwc_ahsata_xfer_mode(int dev, u16 *id)
 632{
 633        struct ahci_probe_ent *probe_ent =
 634                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 635
 636        probe_ent->pio_mask = id[ATA_ID_PIO_MODES];
 637        probe_ent->udma_mask = id[ATA_ID_UDMA_MODES];
 638        debug("pio %04x, udma %04x\n\r",
 639                probe_ent->pio_mask, probe_ent->udma_mask);
 640}
 641
 642static u32 dwc_ahsata_rw_cmd(int dev, u32 start, u32 blkcnt,
 643                                u8 *buffer, int is_write)
 644{
 645        struct ahci_probe_ent *probe_ent =
 646                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 647        struct sata_fis_h2d h2d, *cfis = &h2d;
 648        u8 port = probe_ent->hard_port_no;
 649        u32 block;
 650
 651        block = start;
 652
 653        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 654
 655        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 656        cfis->pm_port_c = 0x80; /* is command */
 657        cfis->command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
 658        cfis->device = ATA_LBA;
 659
 660        cfis->device |= (block >> 24) & 0xf;
 661        cfis->lba_high = (block >> 16) & 0xff;
 662        cfis->lba_mid = (block >> 8) & 0xff;
 663        cfis->lba_low = block & 0xff;
 664        cfis->sector_count = (u8)(blkcnt & 0xff);
 665
 666        if (ahci_exec_ata_cmd(probe_ent, port, cfis,
 667                        buffer, ATA_SECT_SIZE * blkcnt, is_write) > 0)
 668                return blkcnt;
 669        else
 670                return 0;
 671}
 672
 673void dwc_ahsata_flush_cache(int dev)
 674{
 675        struct ahci_probe_ent *probe_ent =
 676                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 677        struct sata_fis_h2d h2d, *cfis = &h2d;
 678        u8 port = probe_ent->hard_port_no;
 679
 680        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 681
 682        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 683        cfis->pm_port_c = 0x80; /* is command */
 684        cfis->command = ATA_CMD_FLUSH;
 685
 686        ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
 687}
 688
 689static u32 dwc_ahsata_rw_cmd_ext(int dev, u32 start, lbaint_t blkcnt,
 690                                u8 *buffer, int is_write)
 691{
 692        struct ahci_probe_ent *probe_ent =
 693                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 694        struct sata_fis_h2d h2d, *cfis = &h2d;
 695        u8 port = probe_ent->hard_port_no;
 696        u64 block;
 697
 698        block = (u64)start;
 699
 700        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 701
 702        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 703        cfis->pm_port_c = 0x80; /* is command */
 704
 705        cfis->command = (is_write) ? ATA_CMD_WRITE_EXT
 706                                 : ATA_CMD_READ_EXT;
 707
 708        cfis->lba_high_exp = (block >> 40) & 0xff;
 709        cfis->lba_mid_exp = (block >> 32) & 0xff;
 710        cfis->lba_low_exp = (block >> 24) & 0xff;
 711        cfis->lba_high = (block >> 16) & 0xff;
 712        cfis->lba_mid = (block >> 8) & 0xff;
 713        cfis->lba_low = block & 0xff;
 714        cfis->device = ATA_LBA;
 715        cfis->sector_count_exp = (blkcnt >> 8) & 0xff;
 716        cfis->sector_count = blkcnt & 0xff;
 717
 718        if (ahci_exec_ata_cmd(probe_ent, port, cfis, buffer,
 719                        ATA_SECT_SIZE * blkcnt, is_write) > 0)
 720                return blkcnt;
 721        else
 722                return 0;
 723}
 724
 725u32 dwc_ahsata_rw_ncq_cmd(int dev, u32 start, lbaint_t blkcnt,
 726                                u8 *buffer, int is_write)
 727{
 728        struct ahci_probe_ent *probe_ent =
 729                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 730        struct sata_fis_h2d h2d, *cfis = &h2d;
 731        u8 port = probe_ent->hard_port_no;
 732        u64 block;
 733
 734        if (sata_dev_desc[dev].lba48 != 1) {
 735                printf("execute FPDMA command on non-LBA48 hard disk\n\r");
 736                return -1;
 737        }
 738
 739        block = (u64)start;
 740
 741        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 742
 743        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 744        cfis->pm_port_c = 0x80; /* is command */
 745
 746        cfis->command = (is_write) ? ATA_CMD_FPDMA_WRITE
 747                                 : ATA_CMD_FPDMA_READ;
 748
 749        cfis->lba_high_exp = (block >> 40) & 0xff;
 750        cfis->lba_mid_exp = (block >> 32) & 0xff;
 751        cfis->lba_low_exp = (block >> 24) & 0xff;
 752        cfis->lba_high = (block >> 16) & 0xff;
 753        cfis->lba_mid = (block >> 8) & 0xff;
 754        cfis->lba_low = block & 0xff;
 755
 756        cfis->device = ATA_LBA;
 757        cfis->features_exp = (blkcnt >> 8) & 0xff;
 758        cfis->features = blkcnt & 0xff;
 759
 760        /* Use the latest queue */
 761        ahci_exec_ata_cmd(probe_ent, port, cfis,
 762                        buffer, ATA_SECT_SIZE * blkcnt, is_write);
 763
 764        return blkcnt;
 765}
 766
 767void dwc_ahsata_flush_cache_ext(int dev)
 768{
 769        struct ahci_probe_ent *probe_ent =
 770                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 771        struct sata_fis_h2d h2d, *cfis = &h2d;
 772        u8 port = probe_ent->hard_port_no;
 773
 774        memset(cfis, 0, sizeof(struct sata_fis_h2d));
 775
 776        cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
 777        cfis->pm_port_c = 0x80; /* is command */
 778        cfis->command = ATA_CMD_FLUSH_EXT;
 779
 780        ahci_exec_ata_cmd(probe_ent, port, cfis, NULL, 0, 0);
 781}
 782
 783static void dwc_ahsata_init_wcache(int dev, u16 *id)
 784{
 785        struct ahci_probe_ent *probe_ent =
 786                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 787
 788        if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
 789                probe_ent->flags |= SATA_FLAG_WCACHE;
 790        if (ata_id_has_flush(id))
 791                probe_ent->flags |= SATA_FLAG_FLUSH;
 792        if (ata_id_has_flush_ext(id))
 793                probe_ent->flags |= SATA_FLAG_FLUSH_EXT;
 794}
 795
 796u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
 797                                void *buffer, int is_write)
 798{
 799        u32 start, blks;
 800        u8 *addr;
 801        int max_blks;
 802
 803        start = blknr;
 804        blks = blkcnt;
 805        addr = (u8 *)buffer;
 806
 807        max_blks = ATA_MAX_SECTORS_LBA48;
 808
 809        do {
 810                if (blks > max_blks) {
 811                        if (max_blks != dwc_ahsata_rw_cmd_ext(dev, start,
 812                                                max_blks, addr, is_write))
 813                                return 0;
 814                        start += max_blks;
 815                        blks -= max_blks;
 816                        addr += ATA_SECT_SIZE * max_blks;
 817                } else {
 818                        if (blks != dwc_ahsata_rw_cmd_ext(dev, start,
 819                                                blks, addr, is_write))
 820                                return 0;
 821                        start += blks;
 822                        blks = 0;
 823                        addr += ATA_SECT_SIZE * blks;
 824                }
 825        } while (blks != 0);
 826
 827        return blkcnt;
 828}
 829
 830u32 ata_low_level_rw_lba28(int dev, u32 blknr, lbaint_t blkcnt,
 831                                void *buffer, int is_write)
 832{
 833        u32 start, blks;
 834        u8 *addr;
 835        int max_blks;
 836
 837        start = blknr;
 838        blks = blkcnt;
 839        addr = (u8 *)buffer;
 840
 841        max_blks = ATA_MAX_SECTORS;
 842        do {
 843                if (blks > max_blks) {
 844                        if (max_blks != dwc_ahsata_rw_cmd(dev, start,
 845                                                max_blks, addr, is_write))
 846                                return 0;
 847                        start += max_blks;
 848                        blks -= max_blks;
 849                        addr += ATA_SECT_SIZE * max_blks;
 850                } else {
 851                        if (blks != dwc_ahsata_rw_cmd(dev, start,
 852                                                blks, addr, is_write))
 853                                return 0;
 854                        start += blks;
 855                        blks = 0;
 856                        addr += ATA_SECT_SIZE * blks;
 857                }
 858        } while (blks != 0);
 859
 860        return blkcnt;
 861}
 862
 863/*
 864 * SATA interface between low level driver and command layer
 865 */
 866ulong sata_read(int dev, unsigned long blknr, lbaint_t blkcnt, void *buffer)
 867{
 868        u32 rc;
 869
 870        if (sata_dev_desc[dev].lba48)
 871                rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
 872                                                buffer, READ_CMD);
 873        else
 874                rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
 875                                                buffer, READ_CMD);
 876        return rc;
 877}
 878
 879ulong sata_write(int dev, unsigned long blknr, lbaint_t blkcnt, void *buffer)
 880{
 881        u32 rc;
 882        struct ahci_probe_ent *probe_ent =
 883                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 884        u32 flags = probe_ent->flags;
 885
 886        if (sata_dev_desc[dev].lba48) {
 887                rc = ata_low_level_rw_lba48(dev, blknr, blkcnt,
 888                                                buffer, WRITE_CMD);
 889                if ((flags & SATA_FLAG_WCACHE) &&
 890                        (flags & SATA_FLAG_FLUSH_EXT))
 891                        dwc_ahsata_flush_cache_ext(dev);
 892        } else {
 893                rc = ata_low_level_rw_lba28(dev, blknr, blkcnt,
 894                                                buffer, WRITE_CMD);
 895                if ((flags & SATA_FLAG_WCACHE) &&
 896                        (flags & SATA_FLAG_FLUSH))
 897                        dwc_ahsata_flush_cache(dev);
 898        }
 899        return rc;
 900}
 901
 902int scan_sata(int dev)
 903{
 904        u8 serial[ATA_ID_SERNO_LEN + 1] = { 0 };
 905        u8 firmware[ATA_ID_FW_REV_LEN + 1] = { 0 };
 906        u8 product[ATA_ID_PROD_LEN + 1] = { 0 };
 907        u16 *id;
 908        u64 n_sectors;
 909        struct ahci_probe_ent *probe_ent =
 910                (struct ahci_probe_ent *)sata_dev_desc[dev].priv;
 911        u8 port = probe_ent->hard_port_no;
 912        block_dev_desc_t *pdev = &(sata_dev_desc[dev]);
 913
 914        id = (u16 *)malloc(ATA_ID_WORDS * 2);
 915        if (!id) {
 916                printf("id malloc failed\n\r");
 917                return -1;
 918        }
 919
 920        /* Identify device to get information */
 921        dwc_ahsata_identify(dev, id);
 922
 923        /* Serial number */
 924        ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
 925        memcpy(pdev->product, serial, sizeof(serial));
 926
 927        /* Firmware version */
 928        ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
 929        memcpy(pdev->revision, firmware, sizeof(firmware));
 930
 931        /* Product model */
 932        ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
 933        memcpy(pdev->vendor, product, sizeof(product));
 934
 935        /* Totoal sectors */
 936        n_sectors = ata_id_n_sectors(id);
 937        pdev->lba = (u32)n_sectors;
 938
 939        pdev->type = DEV_TYPE_HARDDISK;
 940        pdev->blksz = ATA_SECT_SIZE;
 941        pdev->lun = 0 ;
 942
 943        /* Check if support LBA48 */
 944        if (ata_id_has_lba48(id)) {
 945                pdev->lba48 = 1;
 946                debug("Device support LBA48\n\r");
 947        }
 948
 949        /* Get the NCQ queue depth from device */
 950        probe_ent->flags &= (~SATA_FLAG_Q_DEP_MASK);
 951        probe_ent->flags |= ata_id_queue_depth(id);
 952
 953        /* Get the xfer mode from device */
 954        dwc_ahsata_xfer_mode(dev, id);
 955
 956        /* Get the write cache status from device */
 957        dwc_ahsata_init_wcache(dev, id);
 958
 959        /* Set the xfer mode to highest speed */
 960        ahci_set_feature(dev, port);
 961
 962        free((void *)id);
 963
 964        dwc_ahsata_print_info(dev);
 965
 966        is_ready = 1;
 967
 968        return 0;
 969}
 970