Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 1 | /* |
Kumar Gala | 4c2e3da | 2009-07-28 21:49:52 -0500 | [diff] [blame] | 2 | * Copyright (C) Freescale Semiconductor, Inc. 2006. |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 3 | * Author: Jason Jin<Jason.jin@freescale.com> |
| 4 | * Zhang Wei<wei.zhang@freescale.com> |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | * |
| 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, |
| 22 | * MA 02111-1307 USA |
| 23 | * |
| 24 | * with the reference on libata and ahci drvier in kernel |
| 25 | * |
| 26 | */ |
| 27 | #include <common.h> |
| 28 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 29 | #include <command.h> |
| 30 | #include <pci.h> |
| 31 | #include <asm/processor.h> |
| 32 | #include <asm/errno.h> |
| 33 | #include <asm/io.h> |
| 34 | #include <malloc.h> |
| 35 | #include <scsi.h> |
| 36 | #include <ata.h> |
| 37 | #include <linux/ctype.h> |
| 38 | #include <ahci.h> |
| 39 | |
Marc Jones | 766b16f | 2012-10-29 05:24:02 +0000 | [diff] [blame] | 40 | static int ata_io_flush(u8 port); |
| 41 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 42 | struct ahci_probe_ent *probe_ent = NULL; |
| 43 | hd_driveid_t *ataid[AHCI_MAX_PORTS]; |
| 44 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 45 | #define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0) |
| 46 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 47 | /* |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 48 | * Some controllers limit number of blocks they can read/write at once. |
| 49 | * Contemporary SSD devices work much faster if the read/write size is aligned |
| 50 | * to a power of 2. Let's set default to 128 and allowing to be overwritten if |
| 51 | * needed. |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 52 | */ |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 53 | #ifndef MAX_SATA_BLOCKS_READ_WRITE |
| 54 | #define MAX_SATA_BLOCKS_READ_WRITE 0x80 |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 55 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 56 | |
Walter Murphy | 5784766 | 2012-10-29 05:24:00 +0000 | [diff] [blame] | 57 | /* Maximum timeouts for each event */ |
Marc Jones | 2a0c61d | 2012-10-29 05:24:01 +0000 | [diff] [blame] | 58 | #define WAIT_MS_SPINUP 10000 |
Walter Murphy | 5784766 | 2012-10-29 05:24:00 +0000 | [diff] [blame] | 59 | #define WAIT_MS_DATAIO 5000 |
Marc Jones | 766b16f | 2012-10-29 05:24:02 +0000 | [diff] [blame] | 60 | #define WAIT_MS_FLUSH 5000 |
Walter Murphy | 5784766 | 2012-10-29 05:24:00 +0000 | [diff] [blame] | 61 | #define WAIT_MS_LINKUP 4 |
| 62 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 63 | static inline u32 ahci_port_base(u32 base, u32 port) |
| 64 | { |
| 65 | return base + 0x100 + (port * 0x80); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void ahci_setup_port(struct ahci_ioports *port, unsigned long base, |
| 70 | unsigned int port_idx) |
| 71 | { |
| 72 | base = ahci_port_base(base, port_idx); |
| 73 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 74 | port->cmd_addr = base; |
| 75 | port->scr_addr = base + PORT_SCR; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | #define msleep(a) udelay(a * 1000) |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 80 | |
Taylor Hutt | 90b276f | 2012-10-29 05:23:59 +0000 | [diff] [blame] | 81 | static void ahci_dcache_flush_range(unsigned begin, unsigned len) |
| 82 | { |
| 83 | const unsigned long start = begin; |
| 84 | const unsigned long end = start + len; |
| 85 | |
| 86 | debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end); |
| 87 | flush_dcache_range(start, end); |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * SATA controller DMAs to physical RAM. Ensure data from the |
| 92 | * controller is invalidated from dcache; next access comes from |
| 93 | * physical RAM. |
| 94 | */ |
| 95 | static void ahci_dcache_invalidate_range(unsigned begin, unsigned len) |
| 96 | { |
| 97 | const unsigned long start = begin; |
| 98 | const unsigned long end = start + len; |
| 99 | |
| 100 | debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end); |
| 101 | invalidate_dcache_range(start, end); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Ensure data for SATA controller is flushed out of dcache and |
| 106 | * written to physical memory. |
| 107 | */ |
| 108 | static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp) |
| 109 | { |
| 110 | ahci_dcache_flush_range((unsigned long)pp->cmd_slot, |
| 111 | AHCI_PORT_PRIV_DMA_SZ); |
| 112 | } |
| 113 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 114 | static int waiting_for_cmd_completed(volatile u8 *offset, |
| 115 | int timeout_msec, |
| 116 | u32 sign) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 117 | { |
| 118 | int i; |
| 119 | u32 status; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 120 | |
| 121 | for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 122 | msleep(1); |
| 123 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 124 | return (i < timeout_msec) ? 0 : -1; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
| 128 | static int ahci_host_init(struct ahci_probe_ent *probe_ent) |
| 129 | { |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 130 | #ifndef CONFIG_SCSI_AHCI_PLAT |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 131 | pci_dev_t pdev = probe_ent->dev; |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 132 | u16 tmp16; |
| 133 | unsigned short vendor; |
| 134 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 135 | volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base; |
Marc Jones | 2a0c61d | 2012-10-29 05:24:01 +0000 | [diff] [blame] | 136 | u32 tmp, cap_save, cmd; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 137 | int i, j; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 138 | volatile u8 *port_mmio; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 139 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 140 | debug("ahci_host_init: start\n"); |
| 141 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 142 | cap_save = readl(mmio + HOST_CAP); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 143 | cap_save &= ((1 << 28) | (1 << 17)); |
Marc Jones | 2a0c61d | 2012-10-29 05:24:01 +0000 | [diff] [blame] | 144 | cap_save |= (1 << 27); /* Staggered Spin-up. Not needed. */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 145 | |
| 146 | /* global controller reset */ |
| 147 | tmp = readl(mmio + HOST_CTL); |
| 148 | if ((tmp & HOST_RESET) == 0) |
| 149 | writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL); |
| 150 | |
| 151 | /* reset must complete within 1 second, or |
| 152 | * the hardware should be considered fried. |
| 153 | */ |
Stefan Reinauer | 9a65b87 | 2012-10-29 05:23:49 +0000 | [diff] [blame] | 154 | i = 1000; |
| 155 | do { |
| 156 | udelay(1000); |
| 157 | tmp = readl(mmio + HOST_CTL); |
| 158 | if (!i--) { |
| 159 | debug("controller reset failed (0x%x)\n", tmp); |
| 160 | return -1; |
| 161 | } |
| 162 | } while (tmp & HOST_RESET); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 163 | |
| 164 | writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL); |
| 165 | writel(cap_save, mmio + HOST_CAP); |
| 166 | writel_with_flush(0xf, mmio + HOST_PORTS_IMPL); |
| 167 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 168 | #ifndef CONFIG_SCSI_AHCI_PLAT |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 169 | pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor); |
| 170 | |
| 171 | if (vendor == PCI_VENDOR_ID_INTEL) { |
| 172 | u16 tmp16; |
| 173 | pci_read_config_word(pdev, 0x92, &tmp16); |
| 174 | tmp16 |= 0xf; |
| 175 | pci_write_config_word(pdev, 0x92, tmp16); |
| 176 | } |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 177 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 178 | probe_ent->cap = readl(mmio + HOST_CAP); |
| 179 | probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL); |
| 180 | probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1; |
| 181 | |
| 182 | debug("cap 0x%x port_map 0x%x n_ports %d\n", |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 183 | probe_ent->cap, probe_ent->port_map, probe_ent->n_ports); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 184 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 185 | if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID) |
| 186 | probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID; |
| 187 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 188 | for (i = 0; i < probe_ent->n_ports; i++) { |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 189 | probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i); |
| 190 | port_mmio = (u8 *) probe_ent->port[i].port_mmio; |
| 191 | ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 192 | |
| 193 | /* make sure port is not active */ |
| 194 | tmp = readl(port_mmio + PORT_CMD); |
| 195 | if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON | |
| 196 | PORT_CMD_FIS_RX | PORT_CMD_START)) { |
Stefan Reinauer | 7ba7917 | 2012-10-29 05:23:50 +0000 | [diff] [blame] | 197 | debug("Port %d is active. Deactivating.\n", i); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 198 | tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON | |
| 199 | PORT_CMD_FIS_RX | PORT_CMD_START); |
| 200 | writel_with_flush(tmp, port_mmio + PORT_CMD); |
| 201 | |
| 202 | /* spec says 500 msecs for each bit, so |
| 203 | * this is slightly incorrect. |
| 204 | */ |
| 205 | msleep(500); |
| 206 | } |
| 207 | |
Marc Jones | 2a0c61d | 2012-10-29 05:24:01 +0000 | [diff] [blame] | 208 | /* Add the spinup command to whatever mode bits may |
| 209 | * already be on in the command register. |
| 210 | */ |
| 211 | cmd = readl(port_mmio + PORT_CMD); |
| 212 | cmd |= PORT_CMD_FIS_RX; |
| 213 | cmd |= PORT_CMD_SPIN_UP; |
| 214 | writel_with_flush(cmd, port_mmio + PORT_CMD); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 215 | |
Marc Jones | 2a0c61d | 2012-10-29 05:24:01 +0000 | [diff] [blame] | 216 | /* Bring up SATA link. |
| 217 | * SATA link bringup time is usually less than 1 ms; only very |
| 218 | * rarely has it taken between 1-2 ms. Never seen it above 2 ms. |
| 219 | */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 220 | j = 0; |
Walter Murphy | 5784766 | 2012-10-29 05:24:00 +0000 | [diff] [blame] | 221 | while (j < WAIT_MS_LINKUP) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 222 | tmp = readl(port_mmio + PORT_SCR_STAT); |
| 223 | if ((tmp & 0xf) == 0x3) |
| 224 | break; |
Stefan Reinauer | 9a65b87 | 2012-10-29 05:23:49 +0000 | [diff] [blame] | 225 | udelay(1000); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 226 | j++; |
| 227 | } |
Marc Jones | 2a0c61d | 2012-10-29 05:24:01 +0000 | [diff] [blame] | 228 | if (j == WAIT_MS_LINKUP) { |
| 229 | printf("SATA link %d timeout.\n", i); |
| 230 | continue; |
| 231 | } else { |
| 232 | debug("SATA link ok.\n"); |
| 233 | } |
| 234 | |
| 235 | /* Clear error status */ |
| 236 | tmp = readl(port_mmio + PORT_SCR_ERR); |
| 237 | if (tmp) |
| 238 | writel(tmp, port_mmio + PORT_SCR_ERR); |
| 239 | |
| 240 | debug("Spinning up device on SATA port %d... ", i); |
| 241 | |
| 242 | j = 0; |
| 243 | while (j < WAIT_MS_SPINUP) { |
| 244 | tmp = readl(port_mmio + PORT_TFDATA); |
| 245 | if (!(tmp & (ATA_STAT_BUSY | ATA_STAT_DRQ))) |
| 246 | break; |
| 247 | udelay(1000); |
| 248 | j++; |
| 249 | } |
| 250 | printf("Target spinup took %d ms.\n", j); |
| 251 | if (j == WAIT_MS_SPINUP) |
Stefan Reinauer | 9a65b87 | 2012-10-29 05:23:49 +0000 | [diff] [blame] | 252 | debug("timeout.\n"); |
| 253 | else |
| 254 | debug("ok.\n"); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 255 | |
| 256 | tmp = readl(port_mmio + PORT_SCR_ERR); |
| 257 | debug("PORT_SCR_ERR 0x%x\n", tmp); |
| 258 | writel(tmp, port_mmio + PORT_SCR_ERR); |
| 259 | |
| 260 | /* ack any pending irq events for this port */ |
| 261 | tmp = readl(port_mmio + PORT_IRQ_STAT); |
| 262 | debug("PORT_IRQ_STAT 0x%x\n", tmp); |
| 263 | if (tmp) |
| 264 | writel(tmp, port_mmio + PORT_IRQ_STAT); |
| 265 | |
| 266 | writel(1 << i, mmio + HOST_IRQ_STAT); |
| 267 | |
| 268 | /* set irq mask (enables interrupts) */ |
| 269 | writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK); |
| 270 | |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 271 | /* register linkup ports */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 272 | tmp = readl(port_mmio + PORT_SCR_STAT); |
Marc Jones | 766b16f | 2012-10-29 05:24:02 +0000 | [diff] [blame] | 273 | debug("SATA port %d status: 0x%x\n", i, tmp); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 274 | if ((tmp & 0xf) == 0x03) |
| 275 | probe_ent->link_port_map |= (0x01 << i); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | tmp = readl(mmio + HOST_CTL); |
| 279 | debug("HOST_CTL 0x%x\n", tmp); |
| 280 | writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL); |
| 281 | tmp = readl(mmio + HOST_CTL); |
| 282 | debug("HOST_CTL 0x%x\n", tmp); |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 283 | #ifndef CONFIG_SCSI_AHCI_PLAT |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 284 | pci_read_config_word(pdev, PCI_COMMAND, &tmp16); |
| 285 | tmp |= PCI_COMMAND_MASTER; |
| 286 | pci_write_config_word(pdev, PCI_COMMAND, tmp16); |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 287 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | static void ahci_print_info(struct ahci_probe_ent *probe_ent) |
| 293 | { |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 294 | #ifndef CONFIG_SCSI_AHCI_PLAT |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 295 | pci_dev_t pdev = probe_ent->dev; |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 296 | u16 cc; |
| 297 | #endif |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 298 | volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base; |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 299 | u32 vers, cap, cap2, impl, speed; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 300 | const char *speed_s; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 301 | const char *scc_s; |
| 302 | |
| 303 | vers = readl(mmio + HOST_VERSION); |
| 304 | cap = probe_ent->cap; |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 305 | cap2 = readl(mmio + HOST_CAP2); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 306 | impl = probe_ent->port_map; |
| 307 | |
| 308 | speed = (cap >> 20) & 0xf; |
| 309 | if (speed == 1) |
| 310 | speed_s = "1.5"; |
| 311 | else if (speed == 2) |
| 312 | speed_s = "3"; |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 313 | else if (speed == 3) |
| 314 | speed_s = "6"; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 315 | else |
| 316 | speed_s = "?"; |
| 317 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 318 | #ifdef CONFIG_SCSI_AHCI_PLAT |
| 319 | scc_s = "SATA"; |
| 320 | #else |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 321 | pci_read_config_word(pdev, 0x0a, &cc); |
| 322 | if (cc == 0x0101) |
| 323 | scc_s = "IDE"; |
| 324 | else if (cc == 0x0106) |
| 325 | scc_s = "SATA"; |
| 326 | else if (cc == 0x0104) |
| 327 | scc_s = "RAID"; |
| 328 | else |
| 329 | scc_s = "unknown"; |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 330 | #endif |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 331 | printf("AHCI %02x%02x.%02x%02x " |
| 332 | "%u slots %u ports %s Gbps 0x%x impl %s mode\n", |
| 333 | (vers >> 24) & 0xff, |
| 334 | (vers >> 16) & 0xff, |
| 335 | (vers >> 8) & 0xff, |
| 336 | vers & 0xff, |
| 337 | ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 338 | |
| 339 | printf("flags: " |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 340 | "%s%s%s%s%s%s%s" |
| 341 | "%s%s%s%s%s%s%s" |
| 342 | "%s%s%s%s%s%s\n", |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 343 | cap & (1 << 31) ? "64bit " : "", |
| 344 | cap & (1 << 30) ? "ncq " : "", |
| 345 | cap & (1 << 28) ? "ilck " : "", |
| 346 | cap & (1 << 27) ? "stag " : "", |
| 347 | cap & (1 << 26) ? "pm " : "", |
| 348 | cap & (1 << 25) ? "led " : "", |
| 349 | cap & (1 << 24) ? "clo " : "", |
| 350 | cap & (1 << 19) ? "nz " : "", |
| 351 | cap & (1 << 18) ? "only " : "", |
| 352 | cap & (1 << 17) ? "pmp " : "", |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 353 | cap & (1 << 16) ? "fbss " : "", |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 354 | cap & (1 << 15) ? "pio " : "", |
| 355 | cap & (1 << 14) ? "slum " : "", |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 356 | cap & (1 << 13) ? "part " : "", |
| 357 | cap & (1 << 7) ? "ccc " : "", |
| 358 | cap & (1 << 6) ? "ems " : "", |
| 359 | cap & (1 << 5) ? "sxs " : "", |
| 360 | cap2 & (1 << 2) ? "apst " : "", |
| 361 | cap2 & (1 << 1) ? "nvmp " : "", |
| 362 | cap2 & (1 << 0) ? "boh " : ""); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 363 | } |
| 364 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 365 | #ifndef CONFIG_SCSI_AHCI_PLAT |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 366 | static int ahci_init_one(pci_dev_t pdev) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 367 | { |
Ed Swarthout | 63cec58 | 2007-08-02 14:09:49 -0500 | [diff] [blame] | 368 | u16 vendor; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 369 | int rc; |
| 370 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 371 | memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 372 | |
Ed Swarthout | 594e798 | 2007-08-14 14:06:45 -0500 | [diff] [blame] | 373 | probe_ent = malloc(sizeof(struct ahci_probe_ent)); |
| 374 | memset(probe_ent, 0, sizeof(struct ahci_probe_ent)); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 375 | probe_ent->dev = pdev; |
| 376 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 377 | probe_ent->host_flags = ATA_FLAG_SATA |
| 378 | | ATA_FLAG_NO_LEGACY |
| 379 | | ATA_FLAG_MMIO |
| 380 | | ATA_FLAG_PIO_DMA |
| 381 | | ATA_FLAG_NO_ATAPI; |
| 382 | probe_ent->pio_mask = 0x1f; |
| 383 | probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 384 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 385 | pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &probe_ent->mmio_base); |
| 386 | debug("ahci mmio_base=0x%08x\n", probe_ent->mmio_base); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 387 | |
| 388 | /* Take from kernel: |
| 389 | * JMicron-specific fixup: |
| 390 | * make sure we're in AHCI mode |
| 391 | */ |
| 392 | pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 393 | if (vendor == 0x197b) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 394 | pci_write_config_byte(pdev, 0x41, 0xa1); |
| 395 | |
| 396 | /* initialize adapter */ |
| 397 | rc = ahci_host_init(probe_ent); |
| 398 | if (rc) |
| 399 | goto err_out; |
| 400 | |
| 401 | ahci_print_info(probe_ent); |
| 402 | |
| 403 | return 0; |
| 404 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 405 | err_out: |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 406 | return rc; |
| 407 | } |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 408 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 409 | |
| 410 | #define MAX_DATA_BYTE_COUNT (4*1024*1024) |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 411 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 412 | static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len) |
| 413 | { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 414 | struct ahci_ioports *pp = &(probe_ent->port[port]); |
| 415 | struct ahci_sg *ahci_sg = pp->cmd_tbl_sg; |
| 416 | u32 sg_count; |
| 417 | int i; |
| 418 | |
| 419 | sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 420 | if (sg_count > AHCI_MAX_SG) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 421 | printf("Error:Too much sg!\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 425 | for (i = 0; i < sg_count; i++) { |
| 426 | ahci_sg->addr = |
| 427 | cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 428 | ahci_sg->addr_hi = 0; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 429 | ahci_sg->flags_size = cpu_to_le32(0x3fffff & |
| 430 | (buf_len < MAX_DATA_BYTE_COUNT |
| 431 | ? (buf_len - 1) |
| 432 | : (MAX_DATA_BYTE_COUNT - 1))); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 433 | ahci_sg++; |
| 434 | buf_len -= MAX_DATA_BYTE_COUNT; |
| 435 | } |
| 436 | |
| 437 | return sg_count; |
| 438 | } |
| 439 | |
| 440 | |
| 441 | static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts) |
| 442 | { |
| 443 | pp->cmd_slot->opts = cpu_to_le32(opts); |
| 444 | pp->cmd_slot->status = 0; |
| 445 | pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff); |
| 446 | pp->cmd_slot->tbl_addr_hi = 0; |
| 447 | } |
| 448 | |
| 449 | |
Gabe Black | e81058c | 2012-10-29 05:23:52 +0000 | [diff] [blame] | 450 | #ifdef CONFIG_AHCI_SETFEATURES_XFER |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 451 | static void ahci_set_feature(u8 port) |
| 452 | { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 453 | struct ahci_ioports *pp = &(probe_ent->port[port]); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 454 | volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio; |
| 455 | u32 cmd_fis_len = 5; /* five dwords */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 456 | u8 fis[20]; |
| 457 | |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 458 | /* set feature */ |
Taylor Hutt | c873111 | 2012-10-29 05:23:55 +0000 | [diff] [blame] | 459 | memset(fis, 0, sizeof(fis)); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 460 | fis[0] = 0x27; |
| 461 | fis[1] = 1 << 7; |
| 462 | fis[2] = ATA_CMD_SETF; |
| 463 | fis[3] = SETFEATURES_XFER; |
| 464 | fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01; |
| 465 | |
Taylor Hutt | c873111 | 2012-10-29 05:23:55 +0000 | [diff] [blame] | 466 | memcpy((unsigned char *)pp->cmd_tbl, fis, sizeof(fis)); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 467 | ahci_fill_cmd_slot(pp, cmd_fis_len); |
Taylor Hutt | 90b276f | 2012-10-29 05:23:59 +0000 | [diff] [blame] | 468 | ahci_dcache_flush_sata_cmd(pp); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 469 | writel(1, port_mmio + PORT_CMD_ISSUE); |
| 470 | readl(port_mmio + PORT_CMD_ISSUE); |
| 471 | |
Walter Murphy | 5784766 | 2012-10-29 05:24:00 +0000 | [diff] [blame] | 472 | if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, |
| 473 | WAIT_MS_DATAIO, 0x1)) { |
Stefan Reinauer | 4e422bc | 2012-10-29 05:23:51 +0000 | [diff] [blame] | 474 | printf("set feature error on port %d!\n", port); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 475 | } |
| 476 | } |
Gabe Black | e81058c | 2012-10-29 05:23:52 +0000 | [diff] [blame] | 477 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 478 | |
| 479 | |
| 480 | static int ahci_port_start(u8 port) |
| 481 | { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 482 | struct ahci_ioports *pp = &(probe_ent->port[port]); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 483 | volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 484 | u32 port_status; |
| 485 | u32 mem; |
| 486 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 487 | debug("Enter start port: %d\n", port); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 488 | port_status = readl(port_mmio + PORT_SCR_STAT); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 489 | debug("Port %d status: %x\n", port, port_status); |
| 490 | if ((port_status & 0xf) != 0x03) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 491 | printf("No Link on this port!\n"); |
| 492 | return -1; |
| 493 | } |
| 494 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 495 | mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 496 | if (!mem) { |
| 497 | free(pp); |
| 498 | printf("No mem for table!\n"); |
| 499 | return -ENOMEM; |
| 500 | } |
| 501 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 502 | mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */ |
| 503 | memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 504 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 505 | /* |
| 506 | * First item in chunk of DMA memory: 32-slot command table, |
| 507 | * 32 bytes each in size |
| 508 | */ |
Taylor Hutt | 64738e8 | 2012-10-29 05:23:58 +0000 | [diff] [blame] | 509 | pp->cmd_slot = |
| 510 | (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem); |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 511 | debug("cmd_slot = 0x%x\n", (unsigned)pp->cmd_slot); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 512 | mem += (AHCI_CMD_SLOT_SZ + 224); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 513 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 514 | /* |
| 515 | * Second item: Received-FIS area |
| 516 | */ |
Taylor Hutt | 64738e8 | 2012-10-29 05:23:58 +0000 | [diff] [blame] | 517 | pp->rx_fis = virt_to_phys((void *)mem); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 518 | mem += AHCI_RX_FIS_SZ; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 519 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 520 | /* |
| 521 | * Third item: data area for storing a single command |
| 522 | * and its scatter-gather table |
| 523 | */ |
Taylor Hutt | 64738e8 | 2012-10-29 05:23:58 +0000 | [diff] [blame] | 524 | pp->cmd_tbl = virt_to_phys((void *)mem); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 525 | debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 526 | |
| 527 | mem += AHCI_CMD_TBL_HDR; |
Taylor Hutt | 64738e8 | 2012-10-29 05:23:58 +0000 | [diff] [blame] | 528 | pp->cmd_tbl_sg = |
| 529 | (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 530 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 531 | writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 532 | |
| 533 | writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR); |
| 534 | |
| 535 | writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 536 | PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP | |
| 537 | PORT_CMD_START, port_mmio + PORT_CMD); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 538 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 539 | debug("Exit start port %d\n", port); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 545 | static int ahci_device_data_io(u8 port, u8 *fis, int fis_len, u8 *buf, |
| 546 | int buf_len, u8 is_write) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 547 | { |
| 548 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 549 | struct ahci_ioports *pp = &(probe_ent->port[port]); |
| 550 | volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 551 | u32 opts; |
| 552 | u32 port_status; |
| 553 | int sg_count; |
| 554 | |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 555 | debug("Enter %s: for port %d\n", __func__, port); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 556 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 557 | if (port > probe_ent->n_ports) { |
Taylor Hutt | 5a2b77f | 2012-10-29 05:23:56 +0000 | [diff] [blame] | 558 | printf("Invalid port number %d\n", port); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | port_status = readl(port_mmio + PORT_SCR_STAT); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 563 | if ((port_status & 0xf) != 0x03) { |
| 564 | debug("No Link on port %d!\n", port); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 565 | return -1; |
| 566 | } |
| 567 | |
| 568 | memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len); |
| 569 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 570 | sg_count = ahci_fill_sg(port, buf, buf_len); |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 571 | opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 572 | ahci_fill_cmd_slot(pp, opts); |
| 573 | |
Taylor Hutt | 90b276f | 2012-10-29 05:23:59 +0000 | [diff] [blame] | 574 | ahci_dcache_flush_sata_cmd(pp); |
| 575 | ahci_dcache_flush_range((unsigned)buf, (unsigned)buf_len); |
| 576 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 577 | writel_with_flush(1, port_mmio + PORT_CMD_ISSUE); |
| 578 | |
Walter Murphy | 5784766 | 2012-10-29 05:24:00 +0000 | [diff] [blame] | 579 | if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, |
| 580 | WAIT_MS_DATAIO, 0x1)) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 581 | printf("timeout exit!\n"); |
| 582 | return -1; |
| 583 | } |
Taylor Hutt | 90b276f | 2012-10-29 05:23:59 +0000 | [diff] [blame] | 584 | |
| 585 | ahci_dcache_invalidate_range((unsigned)buf, (unsigned)buf_len); |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 586 | debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | |
| 592 | static char *ata_id_strcpy(u16 *target, u16 *src, int len) |
| 593 | { |
| 594 | int i; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 595 | for (i = 0; i < len / 2; i++) |
Rob Herring | e5a6c79 | 2011-06-01 09:10:26 +0000 | [diff] [blame] | 596 | target[i] = swab16(src[i]); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 597 | return (char *)target; |
| 598 | } |
| 599 | |
| 600 | |
| 601 | static void dump_ataid(hd_driveid_t *ataid) |
| 602 | { |
| 603 | debug("(49)ataid->capability = 0x%x\n", ataid->capability); |
| 604 | debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid); |
| 605 | debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword); |
| 606 | debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes); |
| 607 | debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth); |
| 608 | debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num); |
| 609 | debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num); |
| 610 | debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1); |
| 611 | debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2); |
| 612 | debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse); |
| 613 | debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1); |
| 614 | debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2); |
| 615 | debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default); |
| 616 | debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra); |
| 617 | debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config); |
| 618 | } |
| 619 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 620 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 621 | /* |
| 622 | * SCSI INQUIRY command operation. |
| 623 | */ |
| 624 | static int ata_scsiop_inquiry(ccb *pccb) |
| 625 | { |
| 626 | u8 hdr[] = { |
| 627 | 0, |
| 628 | 0, |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 629 | 0x5, /* claim SPC-3 version compatibility */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 630 | 2, |
| 631 | 95 - 4, |
| 632 | }; |
| 633 | u8 fis[20]; |
| 634 | u8 *tmpid; |
| 635 | u8 port; |
| 636 | |
| 637 | /* Clean ccb data buffer */ |
| 638 | memset(pccb->pdata, 0, pccb->datalen); |
| 639 | |
| 640 | memcpy(pccb->pdata, hdr, sizeof(hdr)); |
| 641 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 642 | if (pccb->datalen <= 35) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 643 | return 0; |
| 644 | |
Taylor Hutt | c873111 | 2012-10-29 05:23:55 +0000 | [diff] [blame] | 645 | memset(fis, 0, sizeof(fis)); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 646 | /* Construct the FIS */ |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 647 | fis[0] = 0x27; /* Host to device FIS. */ |
| 648 | fis[1] = 1 << 7; /* Command FIS. */ |
| 649 | fis[2] = ATA_CMD_IDENT; /* Command byte. */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 650 | |
| 651 | /* Read id from sata */ |
| 652 | port = pccb->target; |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 653 | if (!(tmpid = malloc(sizeof(hd_driveid_t)))) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 654 | return -ENOMEM; |
| 655 | |
Taylor Hutt | c873111 | 2012-10-29 05:23:55 +0000 | [diff] [blame] | 656 | if (ahci_device_data_io(port, (u8 *) &fis, sizeof(fis), tmpid, |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 657 | sizeof(hd_driveid_t), 0)) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 658 | debug("scsi_ahci: SCSI inquiry command failure.\n"); |
| 659 | return -EIO; |
| 660 | } |
| 661 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 662 | if (ataid[port]) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 663 | free(ataid[port]); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 664 | ataid[port] = (hd_driveid_t *) tmpid; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 665 | |
| 666 | memcpy(&pccb->pdata[8], "ATA ", 8); |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 667 | ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16); |
| 668 | ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 669 | |
| 670 | dump_ataid(ataid[port]); |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | |
| 675 | /* |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 676 | * SCSI READ10/WRITE10 command operation. |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 677 | */ |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 678 | static int ata_scsiop_read_write(ccb *pccb, u8 is_write) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 679 | { |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 680 | u32 lba = 0; |
| 681 | u16 blocks = 0; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 682 | u8 fis[20]; |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 683 | u8 *user_buffer = pccb->pdata; |
| 684 | u32 user_buffer_size = pccb->datalen; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 685 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 686 | /* Retrieve the base LBA number from the ccb structure. */ |
| 687 | memcpy(&lba, pccb->cmd + 2, sizeof(lba)); |
| 688 | lba = be32_to_cpu(lba); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 689 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 690 | /* |
| 691 | * And the number of blocks. |
| 692 | * |
| 693 | * For 10-byte and 16-byte SCSI R/W commands, transfer |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 694 | * length 0 means transfer 0 block of data. |
| 695 | * However, for ATA R/W commands, sector count 0 means |
| 696 | * 256 or 65536 sectors, not 0 sectors as in SCSI. |
| 697 | * |
| 698 | * WARNING: one or two older ATA drives treat 0 as 0... |
| 699 | */ |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 700 | blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]); |
| 701 | |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 702 | debug("scsi_ahci: %s %d blocks starting from lba 0x%x\n", |
| 703 | is_write ? "write" : "read", (unsigned)lba, blocks); |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 704 | |
| 705 | /* Preset the FIS */ |
Taylor Hutt | c873111 | 2012-10-29 05:23:55 +0000 | [diff] [blame] | 706 | memset(fis, 0, sizeof(fis)); |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 707 | fis[0] = 0x27; /* Host to device FIS. */ |
| 708 | fis[1] = 1 << 7; /* Command FIS. */ |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 709 | /* Command byte (read/write). */ |
Walter Murphy | fe1f808 | 2012-10-29 05:24:03 +0000 | [diff] [blame] | 710 | fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 711 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 712 | while (blocks) { |
| 713 | u16 now_blocks; /* number of blocks per iteration */ |
| 714 | u32 transfer_size; /* number of bytes per iteration */ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 715 | |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 716 | now_blocks = min(MAX_SATA_BLOCKS_READ_WRITE, blocks); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 717 | |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 718 | transfer_size = ATA_BLOCKSIZE * now_blocks; |
| 719 | if (transfer_size > user_buffer_size) { |
| 720 | printf("scsi_ahci: Error: buffer too small.\n"); |
| 721 | return -EIO; |
| 722 | } |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 723 | |
Walter Murphy | fe1f808 | 2012-10-29 05:24:03 +0000 | [diff] [blame] | 724 | /* LBA48 SATA command but only use 32bit address range within |
| 725 | * that. The next smaller command range (28bit) is too small. |
| 726 | */ |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 727 | fis[4] = (lba >> 0) & 0xff; |
| 728 | fis[5] = (lba >> 8) & 0xff; |
| 729 | fis[6] = (lba >> 16) & 0xff; |
Walter Murphy | fe1f808 | 2012-10-29 05:24:03 +0000 | [diff] [blame] | 730 | fis[7] = 1 << 6; /* device reg: set LBA mode */ |
| 731 | fis[8] = ((lba >> 24) & 0xff); |
| 732 | fis[3] = 0xe0; /* features */ |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 733 | |
| 734 | /* Block (sector) count */ |
| 735 | fis[12] = (now_blocks >> 0) & 0xff; |
| 736 | fis[13] = (now_blocks >> 8) & 0xff; |
| 737 | |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 738 | /* Read/Write from ahci */ |
| 739 | if (ahci_device_data_io(pccb->target, (u8 *) &fis, sizeof(fis), |
| 740 | user_buffer, user_buffer_size, |
| 741 | is_write)) { |
| 742 | debug("scsi_ahci: SCSI %s10 command failure.\n", |
| 743 | is_write ? "WRITE" : "READ"); |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 744 | return -EIO; |
| 745 | } |
Marc Jones | 766b16f | 2012-10-29 05:24:02 +0000 | [diff] [blame] | 746 | |
| 747 | /* If this transaction is a write, do a following flush. |
| 748 | * Writes in u-boot are so rare, and the logic to know when is |
| 749 | * the last write and do a flush only there is sufficiently |
| 750 | * difficult. Just do a flush after every write. This incurs, |
| 751 | * usually, one extra flush when the rare writes do happen. |
| 752 | */ |
| 753 | if (is_write) { |
| 754 | if (-EIO == ata_io_flush(pccb->target)) |
| 755 | return -EIO; |
| 756 | } |
Vadim Bendebury | 284231e | 2012-10-29 05:23:44 +0000 | [diff] [blame] | 757 | user_buffer += transfer_size; |
| 758 | user_buffer_size -= transfer_size; |
| 759 | blocks -= now_blocks; |
| 760 | lba += now_blocks; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | |
| 767 | /* |
| 768 | * SCSI READ CAPACITY10 command operation. |
| 769 | */ |
| 770 | static int ata_scsiop_read_capacity10(ccb *pccb) |
| 771 | { |
Kumar Gala | cb6d0b7 | 2009-07-13 09:24:00 -0500 | [diff] [blame] | 772 | u32 cap; |
Gabe Black | 19d1d41 | 2012-10-29 05:23:54 +0000 | [diff] [blame] | 773 | u32 block_size; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 774 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 775 | if (!ataid[pccb->target]) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 776 | printf("scsi_ahci: SCSI READ CAPACITY10 command failure. " |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 777 | "\tNo ATA info!\n" |
| 778 | "\tPlease run SCSI commmand INQUIRY firstly!\n"); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 779 | return -EPERM; |
| 780 | } |
| 781 | |
Gabe Black | 19d1d41 | 2012-10-29 05:23:54 +0000 | [diff] [blame] | 782 | cap = le32_to_cpu(ataid[pccb->target]->lba_capacity); |
| 783 | if (cap == 0xfffffff) { |
| 784 | unsigned short *cap48 = ataid[pccb->target]->lba48_capacity; |
| 785 | if (cap48[2] || cap48[3]) { |
| 786 | cap = 0xffffffff; |
| 787 | } else { |
| 788 | cap = (le16_to_cpu(cap48[1]) << 16) | |
| 789 | (le16_to_cpu(cap48[0])); |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | cap = cpu_to_be32(cap); |
Kumar Gala | cb6d0b7 | 2009-07-13 09:24:00 -0500 | [diff] [blame] | 794 | memcpy(pccb->pdata, &cap, sizeof(cap)); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 795 | |
Gabe Black | 19d1d41 | 2012-10-29 05:23:54 +0000 | [diff] [blame] | 796 | block_size = cpu_to_be32((u32)512); |
| 797 | memcpy(&pccb->pdata[4], &block_size, 4); |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | |
| 803 | /* |
| 804 | * SCSI READ CAPACITY16 command operation. |
| 805 | */ |
| 806 | static int ata_scsiop_read_capacity16(ccb *pccb) |
| 807 | { |
| 808 | u64 cap; |
| 809 | u64 block_size; |
| 810 | |
| 811 | if (!ataid[pccb->target]) { |
| 812 | printf("scsi_ahci: SCSI READ CAPACITY16 command failure. " |
| 813 | "\tNo ATA info!\n" |
| 814 | "\tPlease run SCSI commmand INQUIRY firstly!\n"); |
| 815 | return -EPERM; |
| 816 | } |
| 817 | |
| 818 | cap = le32_to_cpu(ataid[pccb->target]->lba_capacity); |
| 819 | if (cap == 0xfffffff) { |
| 820 | memcpy(&cap, ataid[pccb->target]->lba48_capacity, sizeof(cap)); |
| 821 | cap = le64_to_cpu(cap); |
| 822 | } |
| 823 | |
| 824 | cap = cpu_to_be64(cap); |
| 825 | memcpy(pccb->pdata, &cap, sizeof(cap)); |
| 826 | |
| 827 | block_size = cpu_to_be64((u64)512); |
| 828 | memcpy(&pccb->pdata[8], &block_size, 8); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 829 | |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | |
| 834 | /* |
| 835 | * SCSI TEST UNIT READY command operation. |
| 836 | */ |
| 837 | static int ata_scsiop_test_unit_ready(ccb *pccb) |
| 838 | { |
| 839 | return (ataid[pccb->target]) ? 0 : -EPERM; |
| 840 | } |
| 841 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 842 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 843 | int scsi_exec(ccb *pccb) |
| 844 | { |
| 845 | int ret; |
| 846 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 847 | switch (pccb->cmd[0]) { |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 848 | case SCSI_READ10: |
Hung-Te Lin | b7a21b7 | 2012-10-29 05:23:53 +0000 | [diff] [blame] | 849 | ret = ata_scsiop_read_write(pccb, 0); |
| 850 | break; |
| 851 | case SCSI_WRITE10: |
| 852 | ret = ata_scsiop_read_write(pccb, 1); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 853 | break; |
Gabe Black | 19d1d41 | 2012-10-29 05:23:54 +0000 | [diff] [blame] | 854 | case SCSI_RD_CAPAC10: |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 855 | ret = ata_scsiop_read_capacity10(pccb); |
| 856 | break; |
Gabe Black | 19d1d41 | 2012-10-29 05:23:54 +0000 | [diff] [blame] | 857 | case SCSI_RD_CAPAC16: |
| 858 | ret = ata_scsiop_read_capacity16(pccb); |
| 859 | break; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 860 | case SCSI_TST_U_RDY: |
| 861 | ret = ata_scsiop_test_unit_ready(pccb); |
| 862 | break; |
| 863 | case SCSI_INQUIRY: |
| 864 | ret = ata_scsiop_inquiry(pccb); |
| 865 | break; |
| 866 | default: |
| 867 | printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]); |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 868 | return false; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 869 | } |
| 870 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 871 | if (ret) { |
| 872 | debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret); |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 873 | return false; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 874 | } |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 875 | return true; |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 876 | |
| 877 | } |
| 878 | |
| 879 | |
| 880 | void scsi_low_level_init(int busdevfunc) |
| 881 | { |
| 882 | int i; |
| 883 | u32 linkmap; |
| 884 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 885 | #ifndef CONFIG_SCSI_AHCI_PLAT |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 886 | ahci_init_one(busdevfunc); |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 887 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 888 | |
| 889 | linkmap = probe_ent->link_port_map; |
| 890 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 891 | for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) { |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 892 | if (((linkmap >> i) & 0x01)) { |
| 893 | if (ahci_port_start((u8) i)) { |
| 894 | printf("Can not start port %d\n", i); |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 895 | continue; |
| 896 | } |
Gabe Black | e81058c | 2012-10-29 05:23:52 +0000 | [diff] [blame] | 897 | #ifdef CONFIG_AHCI_SETFEATURES_XFER |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 898 | ahci_set_feature((u8) i); |
Gabe Black | e81058c | 2012-10-29 05:23:52 +0000 | [diff] [blame] | 899 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 900 | } |
| 901 | } |
| 902 | } |
| 903 | |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 904 | #ifdef CONFIG_SCSI_AHCI_PLAT |
| 905 | int ahci_init(u32 base) |
| 906 | { |
| 907 | int i, rc = 0; |
| 908 | u32 linkmap; |
| 909 | |
| 910 | memset(ataid, 0, sizeof(ataid)); |
| 911 | |
| 912 | probe_ent = malloc(sizeof(struct ahci_probe_ent)); |
| 913 | memset(probe_ent, 0, sizeof(struct ahci_probe_ent)); |
| 914 | |
| 915 | probe_ent->host_flags = ATA_FLAG_SATA |
| 916 | | ATA_FLAG_NO_LEGACY |
| 917 | | ATA_FLAG_MMIO |
| 918 | | ATA_FLAG_PIO_DMA |
| 919 | | ATA_FLAG_NO_ATAPI; |
| 920 | probe_ent->pio_mask = 0x1f; |
| 921 | probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */ |
| 922 | |
| 923 | probe_ent->mmio_base = base; |
| 924 | |
| 925 | /* initialize adapter */ |
| 926 | rc = ahci_host_init(probe_ent); |
| 927 | if (rc) |
| 928 | goto err_out; |
| 929 | |
| 930 | ahci_print_info(probe_ent); |
| 931 | |
| 932 | linkmap = probe_ent->link_port_map; |
| 933 | |
| 934 | for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) { |
| 935 | if (((linkmap >> i) & 0x01)) { |
| 936 | if (ahci_port_start((u8) i)) { |
| 937 | printf("Can not start port %d\n", i); |
| 938 | continue; |
| 939 | } |
Gabe Black | e81058c | 2012-10-29 05:23:52 +0000 | [diff] [blame] | 940 | #ifdef CONFIG_AHCI_SETFEATURES_XFER |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 941 | ahci_set_feature((u8) i); |
Gabe Black | e81058c | 2012-10-29 05:23:52 +0000 | [diff] [blame] | 942 | #endif |
Rob Herring | 942e314 | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | err_out: |
| 946 | return rc; |
| 947 | } |
| 948 | #endif |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 949 | |
Marc Jones | 766b16f | 2012-10-29 05:24:02 +0000 | [diff] [blame] | 950 | /* |
| 951 | * In the general case of generic rotating media it makes sense to have a |
| 952 | * flush capability. It probably even makes sense in the case of SSDs because |
| 953 | * one cannot always know for sure what kind of internal cache/flush mechanism |
| 954 | * is embodied therein. At first it was planned to invoke this after the last |
| 955 | * write to disk and before rebooting. In practice, knowing, a priori, which |
| 956 | * is the last write is difficult. Because writing to the disk in u-boot is |
| 957 | * very rare, this flush command will be invoked after every block write. |
| 958 | */ |
| 959 | static int ata_io_flush(u8 port) |
| 960 | { |
| 961 | u8 fis[20]; |
| 962 | struct ahci_ioports *pp = &(probe_ent->port[port]); |
| 963 | volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio; |
| 964 | u32 cmd_fis_len = 5; /* five dwords */ |
| 965 | |
| 966 | /* Preset the FIS */ |
| 967 | memset(fis, 0, 20); |
| 968 | fis[0] = 0x27; /* Host to device FIS. */ |
| 969 | fis[1] = 1 << 7; /* Command FIS. */ |
Walter Murphy | fe1f808 | 2012-10-29 05:24:03 +0000 | [diff] [blame] | 970 | fis[2] = ATA_CMD_FLUSH_EXT; |
Marc Jones | 766b16f | 2012-10-29 05:24:02 +0000 | [diff] [blame] | 971 | |
| 972 | memcpy((unsigned char *)pp->cmd_tbl, fis, 20); |
| 973 | ahci_fill_cmd_slot(pp, cmd_fis_len); |
| 974 | writel_with_flush(1, port_mmio + PORT_CMD_ISSUE); |
| 975 | |
| 976 | if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, |
| 977 | WAIT_MS_FLUSH, 0x1)) { |
| 978 | debug("scsi_ahci: flush command timeout on port %d.\n", port); |
| 979 | return -EIO; |
| 980 | } |
| 981 | |
| 982 | return 0; |
| 983 | } |
| 984 | |
| 985 | |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 986 | void scsi_bus_reset(void) |
| 987 | { |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 988 | /*Not implement*/ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 992 | void scsi_print_error(ccb * pccb) |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 993 | { |
Jon Loeliger | 4a7cc0f | 2006-08-23 11:04:43 -0500 | [diff] [blame] | 994 | /*The ahci error info can be read in the ahci driver*/ |
Jin Zhengxiong | 4782ac8 | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 995 | } |