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