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