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