blob: 82c68439f8630d0b6342676bf43910f33d5847b1 [file] [log] [blame]
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001/*
Kumar Gala4c2e3da2009-07-28 21:49:52 -05002 * Copyright (C) Freescale Semiconductor, Inc. 2006.
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08003 * Author: Jason Jin<Jason.jin@freescale.com>
4 * Zhang Wei<wei.zhang@freescale.com>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08007 *
8 * with the reference on libata and ahci drvier in kernel
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08009 */
10#include <common.h>
11
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080012#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 Glasscf92e052015-09-02 17:24:58 -060018#include <memalign.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080019#include <scsi.h>
Rob Herring344ca0b2013-08-24 10:10:54 -050020#include <libata.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080021#include <linux/ctype.h>
22#include <ahci.h>
23
Marc Jones766b16f2012-10-29 05:24:02 +000024static int ata_io_flush(u8 port);
25
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080026struct ahci_probe_ent *probe_ent = NULL;
Rob Herring344ca0b2013-08-24 10:10:54 -050027u16 *ataid[AHCI_MAX_PORTS];
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080028
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050029#define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
30
Vadim Bendebury284231e2012-10-29 05:23:44 +000031/*
Hung-Te Linb7a21b72012-10-29 05:23:53 +000032 * 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 Bendebury284231e2012-10-29 05:23:44 +000036 */
Hung-Te Linb7a21b72012-10-29 05:23:53 +000037#ifndef MAX_SATA_BLOCKS_READ_WRITE
38#define MAX_SATA_BLOCKS_READ_WRITE 0x80
Vadim Bendebury284231e2012-10-29 05:23:44 +000039#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080040
Walter Murphy57847662012-10-29 05:24:00 +000041/* Maximum timeouts for each event */
Rob Herring7610b412013-08-24 10:10:53 -050042#define WAIT_MS_SPINUP 20000
Mark Langsdorff8b009e2015-06-05 00:58:46 +010043#define WAIT_MS_DATAIO 10000
Marc Jones766b16f2012-10-29 05:24:02 +000044#define WAIT_MS_FLUSH 5000
Ian Campbelle0ddcf92014-07-18 20:38:39 +010045#define WAIT_MS_LINKUP 200
Walter Murphy57847662012-10-29 05:24:00 +000046
Tang Yuantianfa313772015-07-09 14:37:30 +080047static inline void __iomem *ahci_port_base(void __iomem *base, u32 port)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080048{
49 return base + 0x100 + (port * 0x80);
50}
51
52
Tang Yuantianfa313772015-07-09 14:37:30 +080053static void ahci_setup_port(struct ahci_ioports *port, void __iomem *base,
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080054 unsigned int port_idx)
55{
56 base = ahci_port_base(base, port_idx);
57
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050058 port->cmd_addr = base;
59 port->scr_addr = base + PORT_SCR;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080060}
61
62
63#define msleep(a) udelay(a * 1000)
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050064
Tang Yuantianfa313772015-07-09 14:37:30 +080065static void ahci_dcache_flush_range(unsigned long begin, unsigned long len)
Taylor Hutt90b276f2012-10-29 05:23:59 +000066{
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 Yuantianfa313772015-07-09 14:37:30 +080079static void ahci_dcache_invalidate_range(unsigned long begin, unsigned long len)
Taylor Hutt90b276f2012-10-29 05:23:59 +000080{
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 */
92static 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 Yuantianfa313772015-07-09 14:37:30 +080098static int waiting_for_cmd_completed(void __iomem *offset,
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050099 int timeout_msec,
100 u32 sign)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800101{
102 int i;
103 u32 status;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500104
105 for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800106 msleep(1);
107
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500108 return (i < timeout_msec) ? 0 : -1;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800109}
110
Rob Herring124e9fa2013-08-24 10:10:51 -0500111int __weak ahci_link_up(struct ahci_probe_ent *probe_ent, u8 port)
112{
113 u32 tmp;
114 int j = 0;
Tang Yuantianfa313772015-07-09 14:37:30 +0800115 void __iomem *port_mmio = probe_ent->port[port].port_mmio;
Rob Herring124e9fa2013-08-24 10:10:51 -0500116
Wolfgang Denk3765b3e2013-10-07 13:07:26 +0200117 /*
Rob Herring124e9fa2013-08-24 10:10:51 -0500118 * 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800132
Ian Campbella6e50a82014-07-18 20:38:41 +0100133#ifdef CONFIG_SUNXI_AHCI
134/* The sunxi AHCI controller requires this undocumented setup */
Tang Yuantianfa313772015-07-09 14:37:30 +0800135static void sunxi_dma_init(void __iomem *port_mmio)
Ian Campbella6e50a82014-07-18 20:38:41 +0100136{
137 clrsetbits_le32(port_mmio + PORT_P0DMACR, 0x0000ff00, 0x00004400);
138}
139#endif
140
Scott Wood9efaca32015-04-17 09:19:01 -0500141int ahci_reset(void __iomem *base)
Dmitry Lifshitz6b688882014-12-15 16:02:55 +0200142{
143 int i = 1000;
Scott Wood9efaca32015-04-17 09:19:01 -0500144 u32 __iomem *host_ctl_reg = base + HOST_CTL;
Dmitry Lifshitz6b688882014-12-15 16:02:55 +0200145 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800168static int ahci_host_init(struct ahci_probe_ent *probe_ent)
169{
Rob Herring942e3142011-07-06 16:13:36 +0000170#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800171 pci_dev_t pdev = probe_ent->dev;
Rob Herring942e3142011-07-06 16:13:36 +0000172 u16 tmp16;
173 unsigned short vendor;
174#endif
Tang Yuantianfa313772015-07-09 14:37:30 +0800175 void __iomem *mmio = probe_ent->mmio_base;
Marc Jones2a0c61d2012-10-29 05:24:01 +0000176 u32 tmp, cap_save, cmd;
Rob Herring124e9fa2013-08-24 10:10:51 -0500177 int i, j, ret;
Tang Yuantianfa313772015-07-09 14:37:30 +0800178 void __iomem *port_mmio;
Richard Gibbs2915a022013-08-24 10:10:47 -0500179 u32 port_map;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800180
Vadim Bendebury284231e2012-10-29 05:23:44 +0000181 debug("ahci_host_init: start\n");
182
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800183 cap_save = readl(mmio + HOST_CAP);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500184 cap_save &= ((1 << 28) | (1 << 17));
Marc Jones2a0c61d2012-10-29 05:24:01 +0000185 cap_save |= (1 << 27); /* Staggered Spin-up. Not needed. */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800186
Dmitry Lifshitz6b688882014-12-15 16:02:55 +0200187 ret = ahci_reset(probe_ent->mmio_base);
188 if (ret)
189 return ret;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800190
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 Herring942e3142011-07-06 16:13:36 +0000195#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800196 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 Herring942e3142011-07-06 16:13:36 +0000204#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800205 probe_ent->cap = readl(mmio + HOST_CAP);
206 probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
Richard Gibbs2915a022013-08-24 10:10:47 -0500207 port_map = probe_ent->port_map;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800208 probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
209
210 debug("cap 0x%x port_map 0x%x n_ports %d\n",
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500211 probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800212
Vadim Bendebury284231e2012-10-29 05:23:44 +0000213 if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
214 probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
215
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800216 for (i = 0; i < probe_ent->n_ports; i++) {
Richard Gibbs2915a022013-08-24 10:10:47 -0500217 if (!(port_map & (1 << i)))
218 continue;
Tang Yuantianfa313772015-07-09 14:37:30 +0800219 probe_ent->port[i].port_mmio = ahci_port_base(mmio, i);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500220 port_mmio = (u8 *) probe_ent->port[i].port_mmio;
Tang Yuantianfa313772015-07-09 14:37:30 +0800221 ahci_setup_port(&probe_ent->port[i], mmio, i);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800222
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 Reinauer7ba79172012-10-29 05:23:50 +0000227 debug("Port %d is active. Deactivating.\n", i);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800228 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 Campbella6e50a82014-07-18 20:38:41 +0100238#ifdef CONFIG_SUNXI_AHCI
239 sunxi_dma_init(port_mmio);
240#endif
241
Marc Jones2a0c61d2012-10-29 05:24:01 +0000242 /* 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 Jones2a0c61d2012-10-29 05:24:01 +0000246 cmd |= PORT_CMD_SPIN_UP;
247 writel_with_flush(cmd, port_mmio + PORT_CMD);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800248
Rob Herring124e9fa2013-08-24 10:10:51 -0500249 /* Bring up SATA link. */
250 ret = ahci_link_up(probe_ent, i);
251 if (ret) {
Marc Jones2a0c61d2012-10-29 05:24:01 +0000252 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 Herring344ca0b2013-08-24 10:10:54 -0500268 if (!(tmp & (ATA_BUSY | ATA_DRQ)))
Marc Jones2a0c61d2012-10-29 05:24:01 +0000269 break;
270 udelay(1000);
Rob Herring17821082013-08-24 10:10:52 -0500271 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 Jones2a0c61d2012-10-29 05:24:01 +0000275 j++;
276 }
Rob Herring17821082013-08-24 10:10:52 -0500277
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 Jones2a0c61d2012-10-29 05:24:01 +0000285 printf("Target spinup took %d ms.\n", j);
286 if (j == WAIT_MS_SPINUP)
Stefan Reinauer9a65b872012-10-29 05:23:49 +0000287 debug("timeout.\n");
288 else
289 debug("ok.\n");
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800290
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 Reinauer4e422bc2012-10-29 05:23:51 +0000303 /* register linkup ports */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800304 tmp = readl(port_mmio + PORT_SCR_STAT);
Marc Jones766b16f2012-10-29 05:24:02 +0000305 debug("SATA port %d status: 0x%x\n", i, tmp);
Rob Herring2bdb10d2013-08-24 10:10:50 -0500306 if ((tmp & PORT_SCR_STAT_DET_MASK) == PORT_SCR_STAT_DET_PHYRDY)
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500307 probe_ent->link_port_map |= (0x01 << i);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800308 }
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 Herring942e3142011-07-06 16:13:36 +0000315#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800316 pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
317 tmp |= PCI_COMMAND_MASTER;
318 pci_write_config_word(pdev, PCI_COMMAND, tmp16);
Rob Herring942e3142011-07-06 16:13:36 +0000319#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800320 return 0;
321}
322
323
324static void ahci_print_info(struct ahci_probe_ent *probe_ent)
325{
Rob Herring942e3142011-07-06 16:13:36 +0000326#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800327 pci_dev_t pdev = probe_ent->dev;
Rob Herring942e3142011-07-06 16:13:36 +0000328 u16 cc;
329#endif
Tang Yuantianfa313772015-07-09 14:37:30 +0800330 void __iomem *mmio = probe_ent->mmio_base;
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000331 u32 vers, cap, cap2, impl, speed;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800332 const char *speed_s;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800333 const char *scc_s;
334
335 vers = readl(mmio + HOST_VERSION);
336 cap = probe_ent->cap;
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000337 cap2 = readl(mmio + HOST_CAP2);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800338 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 Reinauer4e422bc2012-10-29 05:23:51 +0000345 else if (speed == 3)
346 speed_s = "6";
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800347 else
348 speed_s = "?";
349
Rob Herring942e3142011-07-06 16:13:36 +0000350#ifdef CONFIG_SCSI_AHCI_PLAT
351 scc_s = "SATA";
352#else
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800353 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 Herring942e3142011-07-06 16:13:36 +0000362#endif
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500363 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800370
371 printf("flags: "
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000372 "%s%s%s%s%s%s%s"
373 "%s%s%s%s%s%s%s"
374 "%s%s%s%s%s%s\n",
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500375 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 Reinauer4e422bc2012-10-29 05:23:51 +0000385 cap & (1 << 16) ? "fbss " : "",
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500386 cap & (1 << 15) ? "pio " : "",
387 cap & (1 << 14) ? "slum " : "",
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000388 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800395}
396
Rob Herring942e3142011-07-06 16:13:36 +0000397#ifndef CONFIG_SCSI_AHCI_PLAT
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500398static int ahci_init_one(pci_dev_t pdev)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800399{
Ed Swarthout63cec582007-08-02 14:09:49 -0500400 u16 vendor;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800401 int rc;
402
Ed Swarthout594e7982007-08-14 14:06:45 -0500403 probe_ent = malloc(sizeof(struct ahci_probe_ent));
Roger Quadrosd73763a2013-11-11 16:56:37 +0200404 if (!probe_ent) {
405 printf("%s: No memory for probe_ent\n", __func__);
406 return -ENOMEM;
407 }
408
Ed Swarthout594e7982007-08-14 14:06:45 -0500409 memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800410 probe_ent->dev = pdev;
411
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500412 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800419
Scott Wood9efaca32015-04-17 09:19:01 -0500420 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800423
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 Loeliger4a7cc0f2006-08-23 11:04:43 -0500429 if (vendor == 0x197b)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800430 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 Loeliger4a7cc0f2006-08-23 11:04:43 -0500441 err_out:
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800442 return rc;
443}
Rob Herring942e3142011-07-06 16:13:36 +0000444#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800445
446#define MAX_DATA_BYTE_COUNT (4*1024*1024)
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500447
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800448static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
449{
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800450 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 Loeliger4a7cc0f2006-08-23 11:04:43 -0500456 if (sg_count > AHCI_MAX_SG) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800457 printf("Error:Too much sg!\n");
458 return -1;
459 }
460
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500461 for (i = 0; i < sg_count; i++) {
462 ahci_sg->addr =
Tang Yuantianfa313772015-07-09 14:37:30 +0800463 cpu_to_le32((unsigned long) buf + i * MAX_DATA_BYTE_COUNT);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800464 ahci_sg->addr_hi = 0;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500465 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800469 ahci_sg++;
470 buf_len -= MAX_DATA_BYTE_COUNT;
471 }
472
473 return sg_count;
474}
475
476
477static 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 Yuantianfa313772015-07-09 14:37:30 +0800481 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 Zhengxiong4782ac82006-08-23 19:10:44 +0800486}
487
488
Gabe Blacke81058c2012-10-29 05:23:52 +0000489#ifdef CONFIG_AHCI_SETFEATURES_XFER
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800490static void ahci_set_feature(u8 port)
491{
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800492 struct ahci_ioports *pp = &(probe_ent->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +0800493 void __iomem *port_mmio = pp->port_mmio;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500494 u32 cmd_fis_len = 5; /* five dwords */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800495 u8 fis[20];
496
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000497 /* set feature */
Taylor Huttc8731112012-10-29 05:23:55 +0000498 memset(fis, 0, sizeof(fis));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800499 fis[0] = 0x27;
500 fis[1] = 1 << 7;
Rob Herring344ca0b2013-08-24 10:10:54 -0500501 fis[2] = ATA_CMD_SET_FEATURES;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800502 fis[3] = SETFEATURES_XFER;
503 fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
504
Taylor Huttc8731112012-10-29 05:23:55 +0000505 memcpy((unsigned char *)pp->cmd_tbl, fis, sizeof(fis));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800506 ahci_fill_cmd_slot(pp, cmd_fis_len);
Taylor Hutt90b276f2012-10-29 05:23:59 +0000507 ahci_dcache_flush_sata_cmd(pp);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800508 writel(1, port_mmio + PORT_CMD_ISSUE);
509 readl(port_mmio + PORT_CMD_ISSUE);
510
Walter Murphy57847662012-10-29 05:24:00 +0000511 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
512 WAIT_MS_DATAIO, 0x1)) {
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000513 printf("set feature error on port %d!\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800514 }
515}
Gabe Blacke81058c2012-10-29 05:23:52 +0000516#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800517
Tang Yuantianfa313772015-07-09 14:37:30 +0800518static int wait_spinup(void __iomem *port_mmio)
Bin Meng4df2b482014-12-31 17:18:39 +0800519{
520 ulong start;
521 u32 tf_data;
522
523 start = get_timer(0);
524 do {
525 tf_data = readl(port_mmio + PORT_TFDATA);
526 if (!(tf_data & ATA_BUSY))
527 return 0;
528 } while (get_timer(start) < WAIT_MS_SPINUP);
529
530 return -ETIMEDOUT;
531}
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800532
533static int ahci_port_start(u8 port)
534{
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800535 struct ahci_ioports *pp = &(probe_ent->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +0800536 void __iomem *port_mmio = pp->port_mmio;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800537 u32 port_status;
Tang Yuantianfa313772015-07-09 14:37:30 +0800538 void __iomem *mem;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800539
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500540 debug("Enter start port: %d\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800541 port_status = readl(port_mmio + PORT_SCR_STAT);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500542 debug("Port %d status: %x\n", port, port_status);
543 if ((port_status & 0xf) != 0x03) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800544 printf("No Link on this port!\n");
545 return -1;
546 }
547
Tang Yuantianfa313772015-07-09 14:37:30 +0800548 mem = malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800549 if (!mem) {
550 free(pp);
Roger Quadrosd73763a2013-11-11 16:56:37 +0200551 printf("%s: No mem for table!\n", __func__);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800552 return -ENOMEM;
553 }
554
Tang Yuantianfa313772015-07-09 14:37:30 +0800555 /* Aligned to 2048-bytes */
556 mem = memalign(2048, AHCI_PORT_PRIV_DMA_SZ);
557 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800558
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800559 /*
560 * First item in chunk of DMA memory: 32-slot command table,
561 * 32 bytes each in size
562 */
Taylor Hutt64738e82012-10-29 05:23:58 +0000563 pp->cmd_slot =
564 (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
Tang Yuantianfa313772015-07-09 14:37:30 +0800565 debug("cmd_slot = %p\n", pp->cmd_slot);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800566 mem += (AHCI_CMD_SLOT_SZ + 224);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500567
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800568 /*
569 * Second item: Received-FIS area
570 */
Taylor Hutt64738e82012-10-29 05:23:58 +0000571 pp->rx_fis = virt_to_phys((void *)mem);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800572 mem += AHCI_RX_FIS_SZ;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500573
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800574 /*
575 * Third item: data area for storing a single command
576 * and its scatter-gather table
577 */
Taylor Hutt64738e82012-10-29 05:23:58 +0000578 pp->cmd_tbl = virt_to_phys((void *)mem);
Tang Yuantianfa313772015-07-09 14:37:30 +0800579 debug("cmd_tbl_dma = %lx\n", pp->cmd_tbl);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800580
581 mem += AHCI_CMD_TBL_HDR;
Taylor Hutt64738e82012-10-29 05:23:58 +0000582 pp->cmd_tbl_sg =
583 (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800584
Tang Yuantianfa313772015-07-09 14:37:30 +0800585 writel_with_flush((unsigned long)pp->cmd_slot,
586 port_mmio + PORT_LST_ADDR);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800587
588 writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
589
Ian Campbella6e50a82014-07-18 20:38:41 +0100590#ifdef CONFIG_SUNXI_AHCI
591 sunxi_dma_init(port_mmio);
592#endif
593
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800594 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500595 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
596 PORT_CMD_START, port_mmio + PORT_CMD);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800597
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500598 debug("Exit start port %d\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800599
Bin Meng4df2b482014-12-31 17:18:39 +0800600 /*
601 * Make sure interface is not busy based on error and status
602 * information from task file data register before proceeding
603 */
604 return wait_spinup(port_mmio);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800605}
606
607
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000608static int ahci_device_data_io(u8 port, u8 *fis, int fis_len, u8 *buf,
609 int buf_len, u8 is_write)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800610{
611
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500612 struct ahci_ioports *pp = &(probe_ent->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +0800613 void __iomem *port_mmio = pp->port_mmio;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800614 u32 opts;
615 u32 port_status;
616 int sg_count;
617
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000618 debug("Enter %s: for port %d\n", __func__, port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800619
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500620 if (port > probe_ent->n_ports) {
Taylor Hutt5a2b77f2012-10-29 05:23:56 +0000621 printf("Invalid port number %d\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800622 return -1;
623 }
624
625 port_status = readl(port_mmio + PORT_SCR_STAT);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500626 if ((port_status & 0xf) != 0x03) {
627 debug("No Link on port %d!\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800628 return -1;
629 }
630
631 memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
632
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500633 sg_count = ahci_fill_sg(port, buf, buf_len);
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000634 opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800635 ahci_fill_cmd_slot(pp, opts);
636
Taylor Hutt90b276f2012-10-29 05:23:59 +0000637 ahci_dcache_flush_sata_cmd(pp);
Tang Yuantianfa313772015-07-09 14:37:30 +0800638 ahci_dcache_flush_range((unsigned long)buf, (unsigned long)buf_len);
Taylor Hutt90b276f2012-10-29 05:23:59 +0000639
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800640 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
641
Walter Murphy57847662012-10-29 05:24:00 +0000642 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
643 WAIT_MS_DATAIO, 0x1)) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800644 printf("timeout exit!\n");
645 return -1;
646 }
Taylor Hutt90b276f2012-10-29 05:23:59 +0000647
Tang Yuantianfa313772015-07-09 14:37:30 +0800648 ahci_dcache_invalidate_range((unsigned long)buf,
649 (unsigned long)buf_len);
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000650 debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800651
652 return 0;
653}
654
655
656static char *ata_id_strcpy(u16 *target, u16 *src, int len)
657{
658 int i;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500659 for (i = 0; i < len / 2; i++)
Rob Herringe5a6c792011-06-01 09:10:26 +0000660 target[i] = swab16(src[i]);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800661 return (char *)target;
662}
663
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800664/*
665 * SCSI INQUIRY command operation.
666 */
667static int ata_scsiop_inquiry(ccb *pccb)
668{
Rob Herring48c3a872013-08-24 10:10:48 -0500669 static const u8 hdr[] = {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800670 0,
671 0,
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500672 0x5, /* claim SPC-3 version compatibility */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800673 2,
674 95 - 4,
675 };
676 u8 fis[20];
Roger Quadros3f629712014-04-01 17:26:40 +0300677 u16 *idbuf;
Roger Quadros2faf5fb2013-11-11 16:56:38 +0200678 ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800679 u8 port;
680
681 /* Clean ccb data buffer */
682 memset(pccb->pdata, 0, pccb->datalen);
683
684 memcpy(pccb->pdata, hdr, sizeof(hdr));
685
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500686 if (pccb->datalen <= 35)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800687 return 0;
688
Taylor Huttc8731112012-10-29 05:23:55 +0000689 memset(fis, 0, sizeof(fis));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800690 /* Construct the FIS */
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500691 fis[0] = 0x27; /* Host to device FIS. */
692 fis[1] = 1 << 7; /* Command FIS. */
Rob Herring344ca0b2013-08-24 10:10:54 -0500693 fis[2] = ATA_CMD_ID_ATA; /* Command byte. */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800694
695 /* Read id from sata */
696 port = pccb->target;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800697
Rob Herring344ca0b2013-08-24 10:10:54 -0500698 if (ahci_device_data_io(port, (u8 *) &fis, sizeof(fis), (u8 *)tmpid,
699 ATA_ID_WORDS * 2, 0)) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800700 debug("scsi_ahci: SCSI inquiry command failure.\n");
701 return -EIO;
702 }
703
Roger Quadros3f629712014-04-01 17:26:40 +0300704 if (!ataid[port]) {
705 ataid[port] = malloc(ATA_ID_WORDS * 2);
706 if (!ataid[port]) {
707 printf("%s: No memory for ataid[port]\n", __func__);
708 return -ENOMEM;
709 }
710 }
711
712 idbuf = ataid[port];
713
714 memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
715 ata_swap_buf_le16(idbuf, ATA_ID_WORDS);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800716
717 memcpy(&pccb->pdata[8], "ATA ", 8);
Roger Quadros3f629712014-04-01 17:26:40 +0300718 ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
719 ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800720
Rob Herring344ca0b2013-08-24 10:10:54 -0500721#ifdef DEBUG
Roger Quadros3f629712014-04-01 17:26:40 +0300722 ata_dump_id(idbuf);
Rob Herring344ca0b2013-08-24 10:10:54 -0500723#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800724 return 0;
725}
726
727
728/*
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000729 * SCSI READ10/WRITE10 command operation.
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800730 */
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000731static int ata_scsiop_read_write(ccb *pccb, u8 is_write)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800732{
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100733 lbaint_t lba = 0;
Vadim Bendebury284231e2012-10-29 05:23:44 +0000734 u16 blocks = 0;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800735 u8 fis[20];
Vadim Bendebury284231e2012-10-29 05:23:44 +0000736 u8 *user_buffer = pccb->pdata;
737 u32 user_buffer_size = pccb->datalen;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800738
Vadim Bendebury284231e2012-10-29 05:23:44 +0000739 /* Retrieve the base LBA number from the ccb structure. */
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100740 if (pccb->cmd[0] == SCSI_READ16) {
741 memcpy(&lba, pccb->cmd + 2, 8);
742 lba = be64_to_cpu(lba);
743 } else {
744 u32 temp;
745 memcpy(&temp, pccb->cmd + 2, 4);
746 lba = be32_to_cpu(temp);
747 }
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800748
Vadim Bendebury284231e2012-10-29 05:23:44 +0000749 /*
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100750 * Retrieve the base LBA number and the block count from
751 * the ccb structure.
Vadim Bendebury284231e2012-10-29 05:23:44 +0000752 *
753 * For 10-byte and 16-byte SCSI R/W commands, transfer
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800754 * length 0 means transfer 0 block of data.
755 * However, for ATA R/W commands, sector count 0 means
756 * 256 or 65536 sectors, not 0 sectors as in SCSI.
757 *
758 * WARNING: one or two older ATA drives treat 0 as 0...
759 */
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100760 if (pccb->cmd[0] == SCSI_READ16)
761 blocks = (((u16)pccb->cmd[13]) << 8) | ((u16) pccb->cmd[14]);
762 else
763 blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
Vadim Bendebury284231e2012-10-29 05:23:44 +0000764
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100765 debug("scsi_ahci: %s %u blocks starting from lba 0x" LBAFU "\n",
766 is_write ? "write" : "read", blocks, lba);
Vadim Bendebury284231e2012-10-29 05:23:44 +0000767
768 /* Preset the FIS */
Taylor Huttc8731112012-10-29 05:23:55 +0000769 memset(fis, 0, sizeof(fis));
Vadim Bendebury284231e2012-10-29 05:23:44 +0000770 fis[0] = 0x27; /* Host to device FIS. */
771 fis[1] = 1 << 7; /* Command FIS. */
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000772 /* Command byte (read/write). */
Walter Murphyfe1f8082012-10-29 05:24:03 +0000773 fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800774
Vadim Bendebury284231e2012-10-29 05:23:44 +0000775 while (blocks) {
776 u16 now_blocks; /* number of blocks per iteration */
777 u32 transfer_size; /* number of bytes per iteration */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800778
Masahiro Yamadab4141192014-11-07 03:03:31 +0900779 now_blocks = min((u16)MAX_SATA_BLOCKS_READ_WRITE, blocks);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800780
Rob Herring344ca0b2013-08-24 10:10:54 -0500781 transfer_size = ATA_SECT_SIZE * now_blocks;
Vadim Bendebury284231e2012-10-29 05:23:44 +0000782 if (transfer_size > user_buffer_size) {
783 printf("scsi_ahci: Error: buffer too small.\n");
784 return -EIO;
785 }
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800786
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100787 /*
788 * LBA48 SATA command but only use 32bit address range within
789 * that (unless we've enabled 64bit LBA support). The next
790 * smaller command range (28bit) is too small.
Walter Murphyfe1f8082012-10-29 05:24:03 +0000791 */
Vadim Bendebury284231e2012-10-29 05:23:44 +0000792 fis[4] = (lba >> 0) & 0xff;
793 fis[5] = (lba >> 8) & 0xff;
794 fis[6] = (lba >> 16) & 0xff;
Walter Murphyfe1f8082012-10-29 05:24:03 +0000795 fis[7] = 1 << 6; /* device reg: set LBA mode */
796 fis[8] = ((lba >> 24) & 0xff);
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100797#ifdef CONFIG_SYS_64BIT_LBA
798 if (pccb->cmd[0] == SCSI_READ16) {
799 fis[9] = ((lba >> 32) & 0xff);
800 fis[10] = ((lba >> 40) & 0xff);
801 }
802#endif
803
Walter Murphyfe1f8082012-10-29 05:24:03 +0000804 fis[3] = 0xe0; /* features */
Vadim Bendebury284231e2012-10-29 05:23:44 +0000805
806 /* Block (sector) count */
807 fis[12] = (now_blocks >> 0) & 0xff;
808 fis[13] = (now_blocks >> 8) & 0xff;
809
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000810 /* Read/Write from ahci */
811 if (ahci_device_data_io(pccb->target, (u8 *) &fis, sizeof(fis),
Tang Yuantian8f6e1832015-03-31 15:02:43 +0800812 user_buffer, transfer_size,
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000813 is_write)) {
814 debug("scsi_ahci: SCSI %s10 command failure.\n",
815 is_write ? "WRITE" : "READ");
Vadim Bendebury284231e2012-10-29 05:23:44 +0000816 return -EIO;
817 }
Marc Jones766b16f2012-10-29 05:24:02 +0000818
819 /* If this transaction is a write, do a following flush.
820 * Writes in u-boot are so rare, and the logic to know when is
821 * the last write and do a flush only there is sufficiently
822 * difficult. Just do a flush after every write. This incurs,
823 * usually, one extra flush when the rare writes do happen.
824 */
825 if (is_write) {
826 if (-EIO == ata_io_flush(pccb->target))
827 return -EIO;
828 }
Vadim Bendebury284231e2012-10-29 05:23:44 +0000829 user_buffer += transfer_size;
830 user_buffer_size -= transfer_size;
831 blocks -= now_blocks;
832 lba += now_blocks;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800833 }
834
835 return 0;
836}
837
838
839/*
840 * SCSI READ CAPACITY10 command operation.
841 */
842static int ata_scsiop_read_capacity10(ccb *pccb)
843{
Kumar Galacb6d0b72009-07-13 09:24:00 -0500844 u32 cap;
Rob Herring344ca0b2013-08-24 10:10:54 -0500845 u64 cap64;
Gabe Black19d1d412012-10-29 05:23:54 +0000846 u32 block_size;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800847
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500848 if (!ataid[pccb->target]) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800849 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500850 "\tNo ATA info!\n"
851 "\tPlease run SCSI commmand INQUIRY firstly!\n");
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800852 return -EPERM;
853 }
854
Rob Herring344ca0b2013-08-24 10:10:54 -0500855 cap64 = ata_id_n_sectors(ataid[pccb->target]);
856 if (cap64 > 0x100000000ULL)
857 cap64 = 0xffffffff;
Gabe Black19d1d412012-10-29 05:23:54 +0000858
Rob Herring344ca0b2013-08-24 10:10:54 -0500859 cap = cpu_to_be32(cap64);
Kumar Galacb6d0b72009-07-13 09:24:00 -0500860 memcpy(pccb->pdata, &cap, sizeof(cap));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800861
Gabe Black19d1d412012-10-29 05:23:54 +0000862 block_size = cpu_to_be32((u32)512);
863 memcpy(&pccb->pdata[4], &block_size, 4);
864
865 return 0;
866}
867
868
869/*
870 * SCSI READ CAPACITY16 command operation.
871 */
872static int ata_scsiop_read_capacity16(ccb *pccb)
873{
874 u64 cap;
875 u64 block_size;
876
877 if (!ataid[pccb->target]) {
878 printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
879 "\tNo ATA info!\n"
880 "\tPlease run SCSI commmand INQUIRY firstly!\n");
881 return -EPERM;
882 }
883
Rob Herring344ca0b2013-08-24 10:10:54 -0500884 cap = ata_id_n_sectors(ataid[pccb->target]);
Gabe Black19d1d412012-10-29 05:23:54 +0000885 cap = cpu_to_be64(cap);
886 memcpy(pccb->pdata, &cap, sizeof(cap));
887
888 block_size = cpu_to_be64((u64)512);
889 memcpy(&pccb->pdata[8], &block_size, 8);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800890
891 return 0;
892}
893
894
895/*
896 * SCSI TEST UNIT READY command operation.
897 */
898static int ata_scsiop_test_unit_ready(ccb *pccb)
899{
900 return (ataid[pccb->target]) ? 0 : -EPERM;
901}
902
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500903
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800904int scsi_exec(ccb *pccb)
905{
906 int ret;
907
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500908 switch (pccb->cmd[0]) {
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100909 case SCSI_READ16:
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800910 case SCSI_READ10:
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000911 ret = ata_scsiop_read_write(pccb, 0);
912 break;
913 case SCSI_WRITE10:
914 ret = ata_scsiop_read_write(pccb, 1);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800915 break;
Gabe Black19d1d412012-10-29 05:23:54 +0000916 case SCSI_RD_CAPAC10:
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800917 ret = ata_scsiop_read_capacity10(pccb);
918 break;
Gabe Black19d1d412012-10-29 05:23:54 +0000919 case SCSI_RD_CAPAC16:
920 ret = ata_scsiop_read_capacity16(pccb);
921 break;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800922 case SCSI_TST_U_RDY:
923 ret = ata_scsiop_test_unit_ready(pccb);
924 break;
925 case SCSI_INQUIRY:
926 ret = ata_scsiop_inquiry(pccb);
927 break;
928 default:
929 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
York Sun472d5462013-04-01 11:29:11 -0700930 return false;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800931 }
932
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500933 if (ret) {
934 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
York Sun472d5462013-04-01 11:29:11 -0700935 return false;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800936 }
York Sun472d5462013-04-01 11:29:11 -0700937 return true;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800938
939}
940
941
942void scsi_low_level_init(int busdevfunc)
943{
944 int i;
945 u32 linkmap;
946
Rob Herring942e3142011-07-06 16:13:36 +0000947#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800948 ahci_init_one(busdevfunc);
Rob Herring942e3142011-07-06 16:13:36 +0000949#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800950
951 linkmap = probe_ent->link_port_map;
952
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200953 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500954 if (((linkmap >> i) & 0x01)) {
955 if (ahci_port_start((u8) i)) {
956 printf("Can not start port %d\n", i);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800957 continue;
958 }
Gabe Blacke81058c2012-10-29 05:23:52 +0000959#ifdef CONFIG_AHCI_SETFEATURES_XFER
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500960 ahci_set_feature((u8) i);
Gabe Blacke81058c2012-10-29 05:23:52 +0000961#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800962 }
963 }
964}
965
Rob Herring942e3142011-07-06 16:13:36 +0000966#ifdef CONFIG_SCSI_AHCI_PLAT
Scott Wood9efaca32015-04-17 09:19:01 -0500967int ahci_init(void __iomem *base)
Rob Herring942e3142011-07-06 16:13:36 +0000968{
969 int i, rc = 0;
970 u32 linkmap;
971
Rob Herring942e3142011-07-06 16:13:36 +0000972 probe_ent = malloc(sizeof(struct ahci_probe_ent));
Roger Quadrosd73763a2013-11-11 16:56:37 +0200973 if (!probe_ent) {
974 printf("%s: No memory for probe_ent\n", __func__);
975 return -ENOMEM;
976 }
977
Rob Herring942e3142011-07-06 16:13:36 +0000978 memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
979
980 probe_ent->host_flags = ATA_FLAG_SATA
981 | ATA_FLAG_NO_LEGACY
982 | ATA_FLAG_MMIO
983 | ATA_FLAG_PIO_DMA
984 | ATA_FLAG_NO_ATAPI;
985 probe_ent->pio_mask = 0x1f;
986 probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
987
988 probe_ent->mmio_base = base;
989
990 /* initialize adapter */
991 rc = ahci_host_init(probe_ent);
992 if (rc)
993 goto err_out;
994
995 ahci_print_info(probe_ent);
996
997 linkmap = probe_ent->link_port_map;
998
999 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
1000 if (((linkmap >> i) & 0x01)) {
1001 if (ahci_port_start((u8) i)) {
1002 printf("Can not start port %d\n", i);
1003 continue;
1004 }
Gabe Blacke81058c2012-10-29 05:23:52 +00001005#ifdef CONFIG_AHCI_SETFEATURES_XFER
Rob Herring942e3142011-07-06 16:13:36 +00001006 ahci_set_feature((u8) i);
Gabe Blacke81058c2012-10-29 05:23:52 +00001007#endif
Rob Herring942e3142011-07-06 16:13:36 +00001008 }
1009 }
1010err_out:
1011 return rc;
1012}
Ian Campbellc6f3d502014-03-07 01:20:56 +00001013
1014void __weak scsi_init(void)
1015{
1016}
1017
Rob Herring942e3142011-07-06 16:13:36 +00001018#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001019
Marc Jones766b16f2012-10-29 05:24:02 +00001020/*
1021 * In the general case of generic rotating media it makes sense to have a
1022 * flush capability. It probably even makes sense in the case of SSDs because
1023 * one cannot always know for sure what kind of internal cache/flush mechanism
1024 * is embodied therein. At first it was planned to invoke this after the last
1025 * write to disk and before rebooting. In practice, knowing, a priori, which
1026 * is the last write is difficult. Because writing to the disk in u-boot is
1027 * very rare, this flush command will be invoked after every block write.
1028 */
1029static int ata_io_flush(u8 port)
1030{
1031 u8 fis[20];
1032 struct ahci_ioports *pp = &(probe_ent->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +08001033 void __iomem *port_mmio = pp->port_mmio;
Marc Jones766b16f2012-10-29 05:24:02 +00001034 u32 cmd_fis_len = 5; /* five dwords */
1035
1036 /* Preset the FIS */
1037 memset(fis, 0, 20);
1038 fis[0] = 0x27; /* Host to device FIS. */
1039 fis[1] = 1 << 7; /* Command FIS. */
Walter Murphyfe1f8082012-10-29 05:24:03 +00001040 fis[2] = ATA_CMD_FLUSH_EXT;
Marc Jones766b16f2012-10-29 05:24:02 +00001041
1042 memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
1043 ahci_fill_cmd_slot(pp, cmd_fis_len);
1044 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
1045
1046 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
1047 WAIT_MS_FLUSH, 0x1)) {
1048 debug("scsi_ahci: flush command timeout on port %d.\n", port);
1049 return -EIO;
1050 }
1051
1052 return 0;
1053}
1054
1055
Dmitry Lifshitz1a33b732014-12-15 16:02:56 +02001056__weak void scsi_bus_reset(void)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001057{
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -05001058 /*Not implement*/
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001059}
1060
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -05001061void scsi_print_error(ccb * pccb)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001062{
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -05001063 /*The ahci error info can be read in the ahci driver*/
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001064}