blob: 21a89eba5af5b1dab0fa766e3c961b1d7754c238 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08002/*
Kumar Gala4c2e3da2009-07-28 21:49:52 -05003 * Copyright (C) Freescale Semiconductor, Inc. 2006.
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08004 * Author: Jason Jin<Jason.jin@freescale.com>
5 * Zhang Wei<wei.zhang@freescale.com>
6 *
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08007 * with the reference on libata and ahci drvier in kernel
Simon Glass7cf1afc2017-06-14 21:28:37 -06008 *
9 * This driver provides a SCSI interface to SATA.
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080010 */
11#include <common.h>
12
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080013#include <command.h>
Simon Glassff758cc2015-11-29 13:18:06 -070014#include <dm.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080015#include <pci.h>
16#include <asm/processor.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090017#include <linux/errno.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080018#include <asm/io.h>
19#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060020#include <memalign.h>
Simon Glass681357f2017-06-14 21:28:46 -060021#include <pci.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080022#include <scsi.h>
Rob Herring344ca0b2013-08-24 10:10:54 -050023#include <libata.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080024#include <linux/ctype.h>
25#include <ahci.h>
Simon Glass681357f2017-06-14 21:28:46 -060026#include <dm/device-internal.h>
27#include <dm/lists.h>
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080028
Simon Glass225b1da2017-06-14 21:28:34 -060029static int ata_io_flush(struct ahci_uc_priv *uc_priv, u8 port);
Marc Jones766b16f2012-10-29 05:24:02 +000030
Simon Glass4682c8a2017-06-14 21:28:40 -060031#ifndef CONFIG_DM_SCSI
Simon Glass2c9f9ef2017-06-14 21:28:32 -060032struct ahci_uc_priv *probe_ent = NULL;
Simon Glass4682c8a2017-06-14 21:28:40 -060033#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080034
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050035#define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
36
Vadim Bendebury284231e2012-10-29 05:23:44 +000037/*
Hung-Te Linb7a21b72012-10-29 05:23:53 +000038 * Some controllers limit number of blocks they can read/write at once.
39 * Contemporary SSD devices work much faster if the read/write size is aligned
40 * to a power of 2. Let's set default to 128 and allowing to be overwritten if
41 * needed.
Vadim Bendebury284231e2012-10-29 05:23:44 +000042 */
Hung-Te Linb7a21b72012-10-29 05:23:53 +000043#ifndef MAX_SATA_BLOCKS_READ_WRITE
44#define MAX_SATA_BLOCKS_READ_WRITE 0x80
Vadim Bendebury284231e2012-10-29 05:23:44 +000045#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080046
Walter Murphy57847662012-10-29 05:24:00 +000047/* Maximum timeouts for each event */
Rob Herring7610b412013-08-24 10:10:53 -050048#define WAIT_MS_SPINUP 20000
Mark Langsdorff8b009e2015-06-05 00:58:46 +010049#define WAIT_MS_DATAIO 10000
Marc Jones766b16f2012-10-29 05:24:02 +000050#define WAIT_MS_FLUSH 5000
Ian Campbelle0ddcf92014-07-18 20:38:39 +010051#define WAIT_MS_LINKUP 200
Walter Murphy57847662012-10-29 05:24:00 +000052
Stefan Roese22f5de62016-08-31 10:02:15 +020053__weak void __iomem *ahci_port_base(void __iomem *base, u32 port)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080054{
55 return base + 0x100 + (port * 0x80);
56}
57
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080058#define msleep(a) udelay(a * 1000)
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050059
Tang Yuantianfa313772015-07-09 14:37:30 +080060static void ahci_dcache_flush_range(unsigned long begin, unsigned long len)
Taylor Hutt90b276f2012-10-29 05:23:59 +000061{
62 const unsigned long start = begin;
63 const unsigned long end = start + len;
64
65 debug("%s: flush dcache: [%#lx, %#lx)\n", __func__, start, end);
66 flush_dcache_range(start, end);
67}
68
69/*
70 * SATA controller DMAs to physical RAM. Ensure data from the
71 * controller is invalidated from dcache; next access comes from
72 * physical RAM.
73 */
Tang Yuantianfa313772015-07-09 14:37:30 +080074static void ahci_dcache_invalidate_range(unsigned long begin, unsigned long len)
Taylor Hutt90b276f2012-10-29 05:23:59 +000075{
76 const unsigned long start = begin;
77 const unsigned long end = start + len;
78
79 debug("%s: invalidate dcache: [%#lx, %#lx)\n", __func__, start, end);
80 invalidate_dcache_range(start, end);
81}
82
83/*
84 * Ensure data for SATA controller is flushed out of dcache and
85 * written to physical memory.
86 */
87static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
88{
89 ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
90 AHCI_PORT_PRIV_DMA_SZ);
91}
92
Tang Yuantianfa313772015-07-09 14:37:30 +080093static int waiting_for_cmd_completed(void __iomem *offset,
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050094 int timeout_msec,
95 u32 sign)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080096{
97 int i;
98 u32 status;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -050099
100 for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800101 msleep(1);
102
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500103 return (i < timeout_msec) ? 0 : -1;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800104}
105
Simon Glass4b62b2f2017-06-14 21:28:33 -0600106int __weak ahci_link_up(struct ahci_uc_priv *uc_priv, u8 port)
Rob Herring124e9fa2013-08-24 10:10:51 -0500107{
108 u32 tmp;
109 int j = 0;
Simon Glass4b62b2f2017-06-14 21:28:33 -0600110 void __iomem *port_mmio = uc_priv->port[port].port_mmio;
Rob Herring124e9fa2013-08-24 10:10:51 -0500111
Wolfgang Denk3765b3e2013-10-07 13:07:26 +0200112 /*
Rob Herring124e9fa2013-08-24 10:10:51 -0500113 * Bring up SATA link.
114 * SATA link bringup time is usually less than 1 ms; only very
115 * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
116 */
117 while (j < WAIT_MS_LINKUP) {
118 tmp = readl(port_mmio + PORT_SCR_STAT);
119 tmp &= PORT_SCR_STAT_DET_MASK;
120 if (tmp == PORT_SCR_STAT_DET_PHYRDY)
121 return 0;
122 udelay(1000);
123 j++;
124 }
125 return 1;
126}
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800127
Ian Campbella6e50a82014-07-18 20:38:41 +0100128#ifdef CONFIG_SUNXI_AHCI
129/* The sunxi AHCI controller requires this undocumented setup */
Tang Yuantianfa313772015-07-09 14:37:30 +0800130static void sunxi_dma_init(void __iomem *port_mmio)
Ian Campbella6e50a82014-07-18 20:38:41 +0100131{
132 clrsetbits_le32(port_mmio + PORT_P0DMACR, 0x0000ff00, 0x00004400);
133}
134#endif
135
Scott Wood9efaca32015-04-17 09:19:01 -0500136int ahci_reset(void __iomem *base)
Dmitry Lifshitz6b688882014-12-15 16:02:55 +0200137{
138 int i = 1000;
Scott Wood9efaca32015-04-17 09:19:01 -0500139 u32 __iomem *host_ctl_reg = base + HOST_CTL;
Dmitry Lifshitz6b688882014-12-15 16:02:55 +0200140 u32 tmp = readl(host_ctl_reg); /* global controller reset */
141
142 if ((tmp & HOST_RESET) == 0)
143 writel_with_flush(tmp | HOST_RESET, host_ctl_reg);
144
145 /*
146 * reset must complete within 1 second, or
147 * the hardware should be considered fried.
148 */
149 do {
150 udelay(1000);
151 tmp = readl(host_ctl_reg);
152 i--;
153 } while ((i > 0) && (tmp & HOST_RESET));
154
155 if (i == 0) {
156 printf("controller reset failed (0x%x)\n", tmp);
157 return -1;
158 }
159
160 return 0;
161}
162
Simon Glass225b1da2017-06-14 21:28:34 -0600163static int ahci_host_init(struct ahci_uc_priv *uc_priv)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800164{
Michal Simeke8a016b2016-09-08 15:06:45 +0200165#if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
Simon Glassff758cc2015-11-29 13:18:06 -0700166# ifdef CONFIG_DM_PCI
Simon Glass225b1da2017-06-14 21:28:34 -0600167 struct udevice *dev = uc_priv->dev;
Simon Glassff758cc2015-11-29 13:18:06 -0700168 struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
169# else
Simon Glass225b1da2017-06-14 21:28:34 -0600170 pci_dev_t pdev = uc_priv->dev;
Rob Herring942e3142011-07-06 16:13:36 +0000171 unsigned short vendor;
Simon Glassff758cc2015-11-29 13:18:06 -0700172# endif
173 u16 tmp16;
Rob Herring942e3142011-07-06 16:13:36 +0000174#endif
Simon Glass225b1da2017-06-14 21:28:34 -0600175 void __iomem *mmio = uc_priv->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
Simon Glass225b1da2017-06-14 21:28:34 -0600187 ret = ahci_reset(uc_priv->mmio_base);
Dmitry Lifshitz6b688882014-12-15 16:02:55 +0200188 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
Michal Simeke8a016b2016-09-08 15:06:45 +0200195#if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
Simon Glassff758cc2015-11-29 13:18:06 -0700196# ifdef CONFIG_DM_PCI
197 if (pplat->vendor == PCI_VENDOR_ID_INTEL) {
198 u16 tmp16;
199
200 dm_pci_read_config16(dev, 0x92, &tmp16);
201 dm_pci_write_config16(dev, 0x92, tmp16 | 0xf);
202 }
203# else
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800204 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
205
206 if (vendor == PCI_VENDOR_ID_INTEL) {
207 u16 tmp16;
208 pci_read_config_word(pdev, 0x92, &tmp16);
209 tmp16 |= 0xf;
210 pci_write_config_word(pdev, 0x92, tmp16);
211 }
Simon Glassff758cc2015-11-29 13:18:06 -0700212# endif
Rob Herring942e3142011-07-06 16:13:36 +0000213#endif
Simon Glass225b1da2017-06-14 21:28:34 -0600214 uc_priv->cap = readl(mmio + HOST_CAP);
215 uc_priv->port_map = readl(mmio + HOST_PORTS_IMPL);
216 port_map = uc_priv->port_map;
217 uc_priv->n_ports = (uc_priv->cap & 0x1f) + 1;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800218
219 debug("cap 0x%x port_map 0x%x n_ports %d\n",
Simon Glass225b1da2017-06-14 21:28:34 -0600220 uc_priv->cap, uc_priv->port_map, uc_priv->n_ports);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800221
Tuomas Tynkkynen0545ac92018-09-13 01:28:55 +0300222#if !defined(CONFIG_DM_SCSI)
Simon Glass225b1da2017-06-14 21:28:34 -0600223 if (uc_priv->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
224 uc_priv->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
Tuomas Tynkkynen0545ac92018-09-13 01:28:55 +0300225#endif
Vadim Bendebury284231e2012-10-29 05:23:44 +0000226
Simon Glass225b1da2017-06-14 21:28:34 -0600227 for (i = 0; i < uc_priv->n_ports; i++) {
Richard Gibbs2915a022013-08-24 10:10:47 -0500228 if (!(port_map & (1 << i)))
229 continue;
Simon Glass225b1da2017-06-14 21:28:34 -0600230 uc_priv->port[i].port_mmio = ahci_port_base(mmio, i);
231 port_mmio = (u8 *)uc_priv->port[i].port_mmio;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800232
233 /* make sure port is not active */
234 tmp = readl(port_mmio + PORT_CMD);
235 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
236 PORT_CMD_FIS_RX | PORT_CMD_START)) {
Stefan Reinauer7ba79172012-10-29 05:23:50 +0000237 debug("Port %d is active. Deactivating.\n", i);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800238 tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
239 PORT_CMD_FIS_RX | PORT_CMD_START);
240 writel_with_flush(tmp, port_mmio + PORT_CMD);
241
242 /* spec says 500 msecs for each bit, so
243 * this is slightly incorrect.
244 */
245 msleep(500);
246 }
247
Ian Campbella6e50a82014-07-18 20:38:41 +0100248#ifdef CONFIG_SUNXI_AHCI
249 sunxi_dma_init(port_mmio);
250#endif
251
Marc Jones2a0c61d2012-10-29 05:24:01 +0000252 /* Add the spinup command to whatever mode bits may
253 * already be on in the command register.
254 */
255 cmd = readl(port_mmio + PORT_CMD);
Marc Jones2a0c61d2012-10-29 05:24:01 +0000256 cmd |= PORT_CMD_SPIN_UP;
257 writel_with_flush(cmd, port_mmio + PORT_CMD);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800258
Rob Herring124e9fa2013-08-24 10:10:51 -0500259 /* Bring up SATA link. */
Simon Glass225b1da2017-06-14 21:28:34 -0600260 ret = ahci_link_up(uc_priv, i);
Rob Herring124e9fa2013-08-24 10:10:51 -0500261 if (ret) {
Marc Jones2a0c61d2012-10-29 05:24:01 +0000262 printf("SATA link %d timeout.\n", i);
263 continue;
264 } else {
265 debug("SATA link ok.\n");
266 }
267
268 /* Clear error status */
269 tmp = readl(port_mmio + PORT_SCR_ERR);
270 if (tmp)
271 writel(tmp, port_mmio + PORT_SCR_ERR);
272
273 debug("Spinning up device on SATA port %d... ", i);
274
275 j = 0;
276 while (j < WAIT_MS_SPINUP) {
277 tmp = readl(port_mmio + PORT_TFDATA);
Rob Herring344ca0b2013-08-24 10:10:54 -0500278 if (!(tmp & (ATA_BUSY | ATA_DRQ)))
Marc Jones2a0c61d2012-10-29 05:24:01 +0000279 break;
280 udelay(1000);
Rob Herring17821082013-08-24 10:10:52 -0500281 tmp = readl(port_mmio + PORT_SCR_STAT);
282 tmp &= PORT_SCR_STAT_DET_MASK;
283 if (tmp == PORT_SCR_STAT_DET_PHYRDY)
284 break;
Marc Jones2a0c61d2012-10-29 05:24:01 +0000285 j++;
286 }
Rob Herring17821082013-08-24 10:10:52 -0500287
288 tmp = readl(port_mmio + PORT_SCR_STAT) & PORT_SCR_STAT_DET_MASK;
289 if (tmp == PORT_SCR_STAT_DET_COMINIT) {
290 debug("SATA link %d down (COMINIT received), retrying...\n", i);
291 i--;
292 continue;
293 }
294
Marc Jones2a0c61d2012-10-29 05:24:01 +0000295 printf("Target spinup took %d ms.\n", j);
296 if (j == WAIT_MS_SPINUP)
Stefan Reinauer9a65b872012-10-29 05:23:49 +0000297 debug("timeout.\n");
298 else
299 debug("ok.\n");
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800300
301 tmp = readl(port_mmio + PORT_SCR_ERR);
302 debug("PORT_SCR_ERR 0x%x\n", tmp);
303 writel(tmp, port_mmio + PORT_SCR_ERR);
304
305 /* ack any pending irq events for this port */
306 tmp = readl(port_mmio + PORT_IRQ_STAT);
307 debug("PORT_IRQ_STAT 0x%x\n", tmp);
308 if (tmp)
309 writel(tmp, port_mmio + PORT_IRQ_STAT);
310
311 writel(1 << i, mmio + HOST_IRQ_STAT);
312
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000313 /* register linkup ports */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800314 tmp = readl(port_mmio + PORT_SCR_STAT);
Marc Jones766b16f2012-10-29 05:24:02 +0000315 debug("SATA port %d status: 0x%x\n", i, tmp);
Rob Herring2bdb10d2013-08-24 10:10:50 -0500316 if ((tmp & PORT_SCR_STAT_DET_MASK) == PORT_SCR_STAT_DET_PHYRDY)
Simon Glass225b1da2017-06-14 21:28:34 -0600317 uc_priv->link_port_map |= (0x01 << i);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800318 }
319
320 tmp = readl(mmio + HOST_CTL);
321 debug("HOST_CTL 0x%x\n", tmp);
322 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
323 tmp = readl(mmio + HOST_CTL);
324 debug("HOST_CTL 0x%x\n", tmp);
Michal Simeke8a016b2016-09-08 15:06:45 +0200325#if !defined(CONFIG_DM_SCSI)
Rob Herring942e3142011-07-06 16:13:36 +0000326#ifndef CONFIG_SCSI_AHCI_PLAT
Simon Glassff758cc2015-11-29 13:18:06 -0700327# ifdef CONFIG_DM_PCI
328 dm_pci_read_config16(dev, PCI_COMMAND, &tmp16);
329 tmp |= PCI_COMMAND_MASTER;
330 dm_pci_write_config16(dev, PCI_COMMAND, tmp16);
331# else
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800332 pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
333 tmp |= PCI_COMMAND_MASTER;
334 pci_write_config_word(pdev, PCI_COMMAND, tmp16);
Simon Glassff758cc2015-11-29 13:18:06 -0700335# endif
Rob Herring942e3142011-07-06 16:13:36 +0000336#endif
Michal Simeke8a016b2016-09-08 15:06:45 +0200337#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800338 return 0;
339}
340
341
Simon Glass225b1da2017-06-14 21:28:34 -0600342static void ahci_print_info(struct ahci_uc_priv *uc_priv)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800343{
Michal Simeke8a016b2016-09-08 15:06:45 +0200344#if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
345# if defined(CONFIG_DM_PCI)
Simon Glass225b1da2017-06-14 21:28:34 -0600346 struct udevice *dev = uc_priv->dev;
Simon Glassff758cc2015-11-29 13:18:06 -0700347# else
Simon Glass225b1da2017-06-14 21:28:34 -0600348 pci_dev_t pdev = uc_priv->dev;
Simon Glassff758cc2015-11-29 13:18:06 -0700349# endif
Rob Herring942e3142011-07-06 16:13:36 +0000350 u16 cc;
351#endif
Simon Glass225b1da2017-06-14 21:28:34 -0600352 void __iomem *mmio = uc_priv->mmio_base;
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000353 u32 vers, cap, cap2, impl, speed;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800354 const char *speed_s;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800355 const char *scc_s;
356
357 vers = readl(mmio + HOST_VERSION);
Simon Glass225b1da2017-06-14 21:28:34 -0600358 cap = uc_priv->cap;
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000359 cap2 = readl(mmio + HOST_CAP2);
Simon Glass225b1da2017-06-14 21:28:34 -0600360 impl = uc_priv->port_map;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800361
362 speed = (cap >> 20) & 0xf;
363 if (speed == 1)
364 speed_s = "1.5";
365 else if (speed == 2)
366 speed_s = "3";
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000367 else if (speed == 3)
368 speed_s = "6";
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800369 else
370 speed_s = "?";
371
Michal Simeke8a016b2016-09-08 15:06:45 +0200372#if defined(CONFIG_SCSI_AHCI_PLAT) || defined(CONFIG_DM_SCSI)
Rob Herring942e3142011-07-06 16:13:36 +0000373 scc_s = "SATA";
374#else
Simon Glassff758cc2015-11-29 13:18:06 -0700375# ifdef CONFIG_DM_PCI
376 dm_pci_read_config16(dev, 0x0a, &cc);
377# else
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800378 pci_read_config_word(pdev, 0x0a, &cc);
Simon Glassff758cc2015-11-29 13:18:06 -0700379# endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800380 if (cc == 0x0101)
381 scc_s = "IDE";
382 else if (cc == 0x0106)
383 scc_s = "SATA";
384 else if (cc == 0x0104)
385 scc_s = "RAID";
386 else
387 scc_s = "unknown";
Rob Herring942e3142011-07-06 16:13:36 +0000388#endif
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500389 printf("AHCI %02x%02x.%02x%02x "
390 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
391 (vers >> 24) & 0xff,
392 (vers >> 16) & 0xff,
393 (vers >> 8) & 0xff,
394 vers & 0xff,
395 ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800396
397 printf("flags: "
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000398 "%s%s%s%s%s%s%s"
399 "%s%s%s%s%s%s%s"
400 "%s%s%s%s%s%s\n",
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500401 cap & (1 << 31) ? "64bit " : "",
402 cap & (1 << 30) ? "ncq " : "",
403 cap & (1 << 28) ? "ilck " : "",
404 cap & (1 << 27) ? "stag " : "",
405 cap & (1 << 26) ? "pm " : "",
406 cap & (1 << 25) ? "led " : "",
407 cap & (1 << 24) ? "clo " : "",
408 cap & (1 << 19) ? "nz " : "",
409 cap & (1 << 18) ? "only " : "",
410 cap & (1 << 17) ? "pmp " : "",
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000411 cap & (1 << 16) ? "fbss " : "",
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500412 cap & (1 << 15) ? "pio " : "",
413 cap & (1 << 14) ? "slum " : "",
Stefan Reinauer4e422bc2012-10-29 05:23:51 +0000414 cap & (1 << 13) ? "part " : "",
415 cap & (1 << 7) ? "ccc " : "",
416 cap & (1 << 6) ? "ems " : "",
417 cap & (1 << 5) ? "sxs " : "",
418 cap2 & (1 << 2) ? "apst " : "",
419 cap2 & (1 << 1) ? "nvmp " : "",
420 cap2 & (1 << 0) ? "boh " : "");
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800421}
422
Simon Glass745a94f2017-07-04 13:31:18 -0600423#if defined(CONFIG_DM_SCSI) || !defined(CONFIG_SCSI_AHCI_PLAT)
Michal Simeke8a016b2016-09-08 15:06:45 +0200424# if defined(CONFIG_DM_PCI) || defined(CONFIG_DM_SCSI)
Simon Glass4279efc2017-06-14 21:28:38 -0600425static int ahci_init_one(struct ahci_uc_priv *uc_priv, struct udevice *dev)
Simon Glassff758cc2015-11-29 13:18:06 -0700426# else
Simon Glass4279efc2017-06-14 21:28:38 -0600427static int ahci_init_one(struct ahci_uc_priv *uc_priv, pci_dev_t dev)
Simon Glassff758cc2015-11-29 13:18:06 -0700428# endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800429{
Michal Simeke8a016b2016-09-08 15:06:45 +0200430#if !defined(CONFIG_DM_SCSI)
Ed Swarthout63cec582007-08-02 14:09:49 -0500431 u16 vendor;
Michal Simeke8a016b2016-09-08 15:06:45 +0200432#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800433 int rc;
434
Simon Glass225b1da2017-06-14 21:28:34 -0600435 uc_priv->dev = dev;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800436
Simon Glass225b1da2017-06-14 21:28:34 -0600437 uc_priv->host_flags = ATA_FLAG_SATA
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500438 | ATA_FLAG_NO_LEGACY
439 | ATA_FLAG_MMIO
440 | ATA_FLAG_PIO_DMA
441 | ATA_FLAG_NO_ATAPI;
Simon Glass225b1da2017-06-14 21:28:34 -0600442 uc_priv->pio_mask = 0x1f;
443 uc_priv->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800444
Michal Simeke8a016b2016-09-08 15:06:45 +0200445#if !defined(CONFIG_DM_SCSI)
Simon Glassff758cc2015-11-29 13:18:06 -0700446#ifdef CONFIG_DM_PCI
Simon Glass225b1da2017-06-14 21:28:34 -0600447 uc_priv->mmio_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_5,
Simon Glassff758cc2015-11-29 13:18:06 -0700448 PCI_REGION_MEM);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800449
450 /* Take from kernel:
451 * JMicron-specific fixup:
452 * make sure we're in AHCI mode
453 */
Simon Glassff758cc2015-11-29 13:18:06 -0700454 dm_pci_read_config16(dev, PCI_VENDOR_ID, &vendor);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500455 if (vendor == 0x197b)
Simon Glassff758cc2015-11-29 13:18:06 -0700456 dm_pci_write_config8(dev, 0x41, 0xa1);
457#else
Simon Glass225b1da2017-06-14 21:28:34 -0600458 uc_priv->mmio_base = pci_map_bar(dev, PCI_BASE_ADDRESS_5,
Simon Glassff758cc2015-11-29 13:18:06 -0700459 PCI_REGION_MEM);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800460
Simon Glassff758cc2015-11-29 13:18:06 -0700461 /* Take from kernel:
462 * JMicron-specific fixup:
463 * make sure we're in AHCI mode
464 */
465 pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
466 if (vendor == 0x197b)
467 pci_write_config_byte(dev, 0x41, 0xa1);
468#endif
Michal Simeke8a016b2016-09-08 15:06:45 +0200469#else
Simon Glass1dc64f62017-06-14 21:28:31 -0600470 struct scsi_platdata *plat = dev_get_uclass_platdata(dev);
Simon Glass225b1da2017-06-14 21:28:34 -0600471 uc_priv->mmio_base = (void *)plat->base;
Michal Simeke8a016b2016-09-08 15:06:45 +0200472#endif
Simon Glassff758cc2015-11-29 13:18:06 -0700473
Simon Glass225b1da2017-06-14 21:28:34 -0600474 debug("ahci mmio_base=0x%p\n", uc_priv->mmio_base);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800475 /* initialize adapter */
Simon Glass225b1da2017-06-14 21:28:34 -0600476 rc = ahci_host_init(uc_priv);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800477 if (rc)
478 goto err_out;
479
Simon Glass225b1da2017-06-14 21:28:34 -0600480 ahci_print_info(uc_priv);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800481
482 return 0;
483
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500484 err_out:
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800485 return rc;
486}
Rob Herring942e3142011-07-06 16:13:36 +0000487#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800488
489#define MAX_DATA_BYTE_COUNT (4*1024*1024)
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500490
Simon Glass225b1da2017-06-14 21:28:34 -0600491static int ahci_fill_sg(struct ahci_uc_priv *uc_priv, u8 port,
492 unsigned char *buf, int buf_len)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800493{
Simon Glass225b1da2017-06-14 21:28:34 -0600494 struct ahci_ioports *pp = &(uc_priv->port[port]);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800495 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
496 u32 sg_count;
497 int i;
498
499 sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500500 if (sg_count > AHCI_MAX_SG) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800501 printf("Error:Too much sg!\n");
502 return -1;
503 }
504
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500505 for (i = 0; i < sg_count; i++) {
506 ahci_sg->addr =
Tang Yuantianfa313772015-07-09 14:37:30 +0800507 cpu_to_le32((unsigned long) buf + i * MAX_DATA_BYTE_COUNT);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800508 ahci_sg->addr_hi = 0;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500509 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
510 (buf_len < MAX_DATA_BYTE_COUNT
511 ? (buf_len - 1)
512 : (MAX_DATA_BYTE_COUNT - 1)));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800513 ahci_sg++;
514 buf_len -= MAX_DATA_BYTE_COUNT;
515 }
516
517 return sg_count;
518}
519
520
521static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
522{
523 pp->cmd_slot->opts = cpu_to_le32(opts);
524 pp->cmd_slot->status = 0;
Tang Yuantianfa313772015-07-09 14:37:30 +0800525 pp->cmd_slot->tbl_addr = cpu_to_le32((u32)pp->cmd_tbl & 0xffffffff);
526#ifdef CONFIG_PHYS_64BIT
527 pp->cmd_slot->tbl_addr_hi =
528 cpu_to_le32((u32)(((pp->cmd_tbl) >> 16) >> 16));
529#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800530}
531
Tang Yuantianfa313772015-07-09 14:37:30 +0800532static int wait_spinup(void __iomem *port_mmio)
Bin Meng4df2b482014-12-31 17:18:39 +0800533{
534 ulong start;
535 u32 tf_data;
536
537 start = get_timer(0);
538 do {
539 tf_data = readl(port_mmio + PORT_TFDATA);
540 if (!(tf_data & ATA_BUSY))
541 return 0;
542 } while (get_timer(start) < WAIT_MS_SPINUP);
543
544 return -ETIMEDOUT;
545}
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800546
Simon Glass225b1da2017-06-14 21:28:34 -0600547static int ahci_port_start(struct ahci_uc_priv *uc_priv, u8 port)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800548{
Simon Glass225b1da2017-06-14 21:28:34 -0600549 struct ahci_ioports *pp = &(uc_priv->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +0800550 void __iomem *port_mmio = pp->port_mmio;
Oleksandr Rybalko5b7a2bf2019-08-22 12:26:56 +0200551 u64 dma_addr;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800552 u32 port_status;
Tang Yuantianfa313772015-07-09 14:37:30 +0800553 void __iomem *mem;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800554
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500555 debug("Enter start port: %d\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800556 port_status = readl(port_mmio + PORT_SCR_STAT);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500557 debug("Port %d status: %x\n", port, port_status);
558 if ((port_status & 0xf) != 0x03) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800559 printf("No Link on this port!\n");
560 return -1;
561 }
562
Christian Gmeiner28b4ba92019-05-06 15:18:54 +0200563 mem = memalign(2048, AHCI_PORT_PRIV_DMA_SZ);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800564 if (!mem) {
565 free(pp);
Roger Quadrosd73763a2013-11-11 16:56:37 +0200566 printf("%s: No mem for table!\n", __func__);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800567 return -ENOMEM;
568 }
Tang Yuantianfa313772015-07-09 14:37:30 +0800569 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800570
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800571 /*
572 * First item in chunk of DMA memory: 32-slot command table,
573 * 32 bytes each in size
574 */
Taylor Hutt64738e82012-10-29 05:23:58 +0000575 pp->cmd_slot =
576 (struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
Tang Yuantianfa313772015-07-09 14:37:30 +0800577 debug("cmd_slot = %p\n", pp->cmd_slot);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800578 mem += (AHCI_CMD_SLOT_SZ + 224);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500579
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800580 /*
581 * Second item: Received-FIS area
582 */
Taylor Hutt64738e82012-10-29 05:23:58 +0000583 pp->rx_fis = virt_to_phys((void *)mem);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800584 mem += AHCI_RX_FIS_SZ;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500585
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800586 /*
587 * Third item: data area for storing a single command
588 * and its scatter-gather table
589 */
Taylor Hutt64738e82012-10-29 05:23:58 +0000590 pp->cmd_tbl = virt_to_phys((void *)mem);
Tang Yuantianfa313772015-07-09 14:37:30 +0800591 debug("cmd_tbl_dma = %lx\n", pp->cmd_tbl);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800592
593 mem += AHCI_CMD_TBL_HDR;
Taylor Hutt64738e82012-10-29 05:23:58 +0000594 pp->cmd_tbl_sg =
595 (struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800596
Oleksandr Rybalko5b7a2bf2019-08-22 12:26:56 +0200597 dma_addr = (ulong)pp->cmd_slot;
598 writel_with_flush(dma_addr, port_mmio + PORT_LST_ADDR);
599 writel_with_flush(dma_addr >> 32, port_mmio + PORT_LST_ADDR_HI);
600 dma_addr = (ulong)pp->rx_fis;
601 writel_with_flush(dma_addr, port_mmio + PORT_FIS_ADDR);
602 writel_with_flush(dma_addr >> 32, port_mmio + PORT_FIS_ADDR_HI);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800603
Ian Campbella6e50a82014-07-18 20:38:41 +0100604#ifdef CONFIG_SUNXI_AHCI
605 sunxi_dma_init(port_mmio);
606#endif
607
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800608 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500609 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
610 PORT_CMD_START, port_mmio + PORT_CMD);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800611
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500612 debug("Exit start port %d\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800613
Bin Meng4df2b482014-12-31 17:18:39 +0800614 /*
615 * Make sure interface is not busy based on error and status
616 * information from task file data register before proceeding
617 */
618 return wait_spinup(port_mmio);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800619}
620
621
Simon Glass225b1da2017-06-14 21:28:34 -0600622static int ahci_device_data_io(struct ahci_uc_priv *uc_priv, u8 port, u8 *fis,
623 int fis_len, u8 *buf, int buf_len, u8 is_write)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800624{
625
Simon Glass225b1da2017-06-14 21:28:34 -0600626 struct ahci_ioports *pp = &(uc_priv->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +0800627 void __iomem *port_mmio = pp->port_mmio;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800628 u32 opts;
629 u32 port_status;
630 int sg_count;
631
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000632 debug("Enter %s: for port %d\n", __func__, port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800633
Simon Glass225b1da2017-06-14 21:28:34 -0600634 if (port > uc_priv->n_ports) {
Taylor Hutt5a2b77f2012-10-29 05:23:56 +0000635 printf("Invalid port number %d\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800636 return -1;
637 }
638
639 port_status = readl(port_mmio + PORT_SCR_STAT);
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500640 if ((port_status & 0xf) != 0x03) {
641 debug("No Link on port %d!\n", port);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800642 return -1;
643 }
644
645 memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
646
Simon Glass225b1da2017-06-14 21:28:34 -0600647 sg_count = ahci_fill_sg(uc_priv, port, buf, buf_len);
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000648 opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800649 ahci_fill_cmd_slot(pp, opts);
650
Taylor Hutt90b276f2012-10-29 05:23:59 +0000651 ahci_dcache_flush_sata_cmd(pp);
Tang Yuantianfa313772015-07-09 14:37:30 +0800652 ahci_dcache_flush_range((unsigned long)buf, (unsigned long)buf_len);
Taylor Hutt90b276f2012-10-29 05:23:59 +0000653
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800654 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
655
Walter Murphy57847662012-10-29 05:24:00 +0000656 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
657 WAIT_MS_DATAIO, 0x1)) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800658 printf("timeout exit!\n");
659 return -1;
660 }
Taylor Hutt90b276f2012-10-29 05:23:59 +0000661
Tang Yuantianfa313772015-07-09 14:37:30 +0800662 ahci_dcache_invalidate_range((unsigned long)buf,
663 (unsigned long)buf_len);
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000664 debug("%s: %d byte transferred.\n", __func__, pp->cmd_slot->status);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800665
666 return 0;
667}
668
669
670static char *ata_id_strcpy(u16 *target, u16 *src, int len)
671{
672 int i;
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500673 for (i = 0; i < len / 2; i++)
Rob Herringe5a6c792011-06-01 09:10:26 +0000674 target[i] = swab16(src[i]);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800675 return (char *)target;
676}
677
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800678/*
679 * SCSI INQUIRY command operation.
680 */
Simon Glass4b62b2f2017-06-14 21:28:33 -0600681static int ata_scsiop_inquiry(struct ahci_uc_priv *uc_priv,
682 struct scsi_cmd *pccb)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800683{
Rob Herring48c3a872013-08-24 10:10:48 -0500684 static const u8 hdr[] = {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800685 0,
686 0,
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500687 0x5, /* claim SPC-3 version compatibility */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800688 2,
689 95 - 4,
690 };
691 u8 fis[20];
Roger Quadros3f629712014-04-01 17:26:40 +0300692 u16 *idbuf;
Roger Quadros2faf5fb2013-11-11 16:56:38 +0200693 ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800694 u8 port;
695
696 /* Clean ccb data buffer */
697 memset(pccb->pdata, 0, pccb->datalen);
698
699 memcpy(pccb->pdata, hdr, sizeof(hdr));
700
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500701 if (pccb->datalen <= 35)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800702 return 0;
703
Taylor Huttc8731112012-10-29 05:23:55 +0000704 memset(fis, 0, sizeof(fis));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800705 /* Construct the FIS */
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500706 fis[0] = 0x27; /* Host to device FIS. */
707 fis[1] = 1 << 7; /* Command FIS. */
Rob Herring344ca0b2013-08-24 10:10:54 -0500708 fis[2] = ATA_CMD_ID_ATA; /* Command byte. */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800709
710 /* Read id from sata */
711 port = pccb->target;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800712
Simon Glass225b1da2017-06-14 21:28:34 -0600713 if (ahci_device_data_io(uc_priv, port, (u8 *)&fis, sizeof(fis),
714 (u8 *)tmpid, ATA_ID_WORDS * 2, 0)) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800715 debug("scsi_ahci: SCSI inquiry command failure.\n");
716 return -EIO;
717 }
718
Simon Glass4b62b2f2017-06-14 21:28:33 -0600719 if (!uc_priv->ataid[port]) {
720 uc_priv->ataid[port] = malloc(ATA_ID_WORDS * 2);
721 if (!uc_priv->ataid[port]) {
Roger Quadros3f629712014-04-01 17:26:40 +0300722 printf("%s: No memory for ataid[port]\n", __func__);
723 return -ENOMEM;
724 }
725 }
726
Simon Glass4b62b2f2017-06-14 21:28:33 -0600727 idbuf = uc_priv->ataid[port];
Roger Quadros3f629712014-04-01 17:26:40 +0300728
729 memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
730 ata_swap_buf_le16(idbuf, ATA_ID_WORDS);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800731
732 memcpy(&pccb->pdata[8], "ATA ", 8);
Roger Quadros3f629712014-04-01 17:26:40 +0300733 ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
734 ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800735
Rob Herring344ca0b2013-08-24 10:10:54 -0500736#ifdef DEBUG
Roger Quadros3f629712014-04-01 17:26:40 +0300737 ata_dump_id(idbuf);
Rob Herring344ca0b2013-08-24 10:10:54 -0500738#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800739 return 0;
740}
741
742
743/*
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000744 * SCSI READ10/WRITE10 command operation.
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800745 */
Simon Glass225b1da2017-06-14 21:28:34 -0600746static int ata_scsiop_read_write(struct ahci_uc_priv *uc_priv,
747 struct scsi_cmd *pccb, u8 is_write)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800748{
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100749 lbaint_t lba = 0;
Vadim Bendebury284231e2012-10-29 05:23:44 +0000750 u16 blocks = 0;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800751 u8 fis[20];
Vadim Bendebury284231e2012-10-29 05:23:44 +0000752 u8 *user_buffer = pccb->pdata;
753 u32 user_buffer_size = pccb->datalen;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800754
Vadim Bendebury284231e2012-10-29 05:23:44 +0000755 /* Retrieve the base LBA number from the ccb structure. */
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100756 if (pccb->cmd[0] == SCSI_READ16) {
757 memcpy(&lba, pccb->cmd + 2, 8);
758 lba = be64_to_cpu(lba);
759 } else {
760 u32 temp;
761 memcpy(&temp, pccb->cmd + 2, 4);
762 lba = be32_to_cpu(temp);
763 }
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800764
Vadim Bendebury284231e2012-10-29 05:23:44 +0000765 /*
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100766 * Retrieve the base LBA number and the block count from
767 * the ccb structure.
Vadim Bendebury284231e2012-10-29 05:23:44 +0000768 *
769 * For 10-byte and 16-byte SCSI R/W commands, transfer
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800770 * length 0 means transfer 0 block of data.
771 * However, for ATA R/W commands, sector count 0 means
772 * 256 or 65536 sectors, not 0 sectors as in SCSI.
773 *
774 * WARNING: one or two older ATA drives treat 0 as 0...
775 */
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100776 if (pccb->cmd[0] == SCSI_READ16)
777 blocks = (((u16)pccb->cmd[13]) << 8) | ((u16) pccb->cmd[14]);
778 else
779 blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
Vadim Bendebury284231e2012-10-29 05:23:44 +0000780
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100781 debug("scsi_ahci: %s %u blocks starting from lba 0x" LBAFU "\n",
782 is_write ? "write" : "read", blocks, lba);
Vadim Bendebury284231e2012-10-29 05:23:44 +0000783
784 /* Preset the FIS */
Taylor Huttc8731112012-10-29 05:23:55 +0000785 memset(fis, 0, sizeof(fis));
Vadim Bendebury284231e2012-10-29 05:23:44 +0000786 fis[0] = 0x27; /* Host to device FIS. */
787 fis[1] = 1 << 7; /* Command FIS. */
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000788 /* Command byte (read/write). */
Walter Murphyfe1f8082012-10-29 05:24:03 +0000789 fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800790
Vadim Bendebury284231e2012-10-29 05:23:44 +0000791 while (blocks) {
792 u16 now_blocks; /* number of blocks per iteration */
793 u32 transfer_size; /* number of bytes per iteration */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800794
Masahiro Yamadab4141192014-11-07 03:03:31 +0900795 now_blocks = min((u16)MAX_SATA_BLOCKS_READ_WRITE, blocks);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800796
Rob Herring344ca0b2013-08-24 10:10:54 -0500797 transfer_size = ATA_SECT_SIZE * now_blocks;
Vadim Bendebury284231e2012-10-29 05:23:44 +0000798 if (transfer_size > user_buffer_size) {
799 printf("scsi_ahci: Error: buffer too small.\n");
800 return -EIO;
801 }
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800802
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100803 /*
804 * LBA48 SATA command but only use 32bit address range within
805 * that (unless we've enabled 64bit LBA support). The next
806 * smaller command range (28bit) is too small.
Walter Murphyfe1f8082012-10-29 05:24:03 +0000807 */
Vadim Bendebury284231e2012-10-29 05:23:44 +0000808 fis[4] = (lba >> 0) & 0xff;
809 fis[5] = (lba >> 8) & 0xff;
810 fis[6] = (lba >> 16) & 0xff;
Walter Murphyfe1f8082012-10-29 05:24:03 +0000811 fis[7] = 1 << 6; /* device reg: set LBA mode */
812 fis[8] = ((lba >> 24) & 0xff);
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100813#ifdef CONFIG_SYS_64BIT_LBA
814 if (pccb->cmd[0] == SCSI_READ16) {
815 fis[9] = ((lba >> 32) & 0xff);
816 fis[10] = ((lba >> 40) & 0xff);
817 }
818#endif
819
Walter Murphyfe1f8082012-10-29 05:24:03 +0000820 fis[3] = 0xe0; /* features */
Vadim Bendebury284231e2012-10-29 05:23:44 +0000821
822 /* Block (sector) count */
823 fis[12] = (now_blocks >> 0) & 0xff;
824 fis[13] = (now_blocks >> 8) & 0xff;
825
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000826 /* Read/Write from ahci */
Simon Glass225b1da2017-06-14 21:28:34 -0600827 if (ahci_device_data_io(uc_priv, pccb->target, (u8 *)&fis,
828 sizeof(fis), user_buffer, transfer_size,
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000829 is_write)) {
830 debug("scsi_ahci: SCSI %s10 command failure.\n",
831 is_write ? "WRITE" : "READ");
Vadim Bendebury284231e2012-10-29 05:23:44 +0000832 return -EIO;
833 }
Marc Jones766b16f2012-10-29 05:24:02 +0000834
835 /* If this transaction is a write, do a following flush.
836 * Writes in u-boot are so rare, and the logic to know when is
837 * the last write and do a flush only there is sufficiently
838 * difficult. Just do a flush after every write. This incurs,
839 * usually, one extra flush when the rare writes do happen.
840 */
841 if (is_write) {
Simon Glass225b1da2017-06-14 21:28:34 -0600842 if (-EIO == ata_io_flush(uc_priv, pccb->target))
Marc Jones766b16f2012-10-29 05:24:02 +0000843 return -EIO;
844 }
Vadim Bendebury284231e2012-10-29 05:23:44 +0000845 user_buffer += transfer_size;
846 user_buffer_size -= transfer_size;
847 blocks -= now_blocks;
848 lba += now_blocks;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800849 }
850
851 return 0;
852}
853
854
855/*
856 * SCSI READ CAPACITY10 command operation.
857 */
Simon Glass4b62b2f2017-06-14 21:28:33 -0600858static int ata_scsiop_read_capacity10(struct ahci_uc_priv *uc_priv,
859 struct scsi_cmd *pccb)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800860{
Kumar Galacb6d0b72009-07-13 09:24:00 -0500861 u32 cap;
Rob Herring344ca0b2013-08-24 10:10:54 -0500862 u64 cap64;
Gabe Black19d1d412012-10-29 05:23:54 +0000863 u32 block_size;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800864
Simon Glass4b62b2f2017-06-14 21:28:33 -0600865 if (!uc_priv->ataid[pccb->target]) {
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800866 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500867 "\tNo ATA info!\n"
Vagrant Cascadian1b25e582015-11-24 14:46:24 -0800868 "\tPlease run SCSI command INQUIRY first!\n");
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800869 return -EPERM;
870 }
871
Simon Glass4b62b2f2017-06-14 21:28:33 -0600872 cap64 = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
Rob Herring344ca0b2013-08-24 10:10:54 -0500873 if (cap64 > 0x100000000ULL)
874 cap64 = 0xffffffff;
Gabe Black19d1d412012-10-29 05:23:54 +0000875
Rob Herring344ca0b2013-08-24 10:10:54 -0500876 cap = cpu_to_be32(cap64);
Kumar Galacb6d0b72009-07-13 09:24:00 -0500877 memcpy(pccb->pdata, &cap, sizeof(cap));
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800878
Gabe Black19d1d412012-10-29 05:23:54 +0000879 block_size = cpu_to_be32((u32)512);
880 memcpy(&pccb->pdata[4], &block_size, 4);
881
882 return 0;
883}
884
885
886/*
887 * SCSI READ CAPACITY16 command operation.
888 */
Simon Glass4b62b2f2017-06-14 21:28:33 -0600889static int ata_scsiop_read_capacity16(struct ahci_uc_priv *uc_priv,
890 struct scsi_cmd *pccb)
Gabe Black19d1d412012-10-29 05:23:54 +0000891{
892 u64 cap;
893 u64 block_size;
894
Simon Glass4b62b2f2017-06-14 21:28:33 -0600895 if (!uc_priv->ataid[pccb->target]) {
Gabe Black19d1d412012-10-29 05:23:54 +0000896 printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
897 "\tNo ATA info!\n"
Vagrant Cascadian1b25e582015-11-24 14:46:24 -0800898 "\tPlease run SCSI command INQUIRY first!\n");
Gabe Black19d1d412012-10-29 05:23:54 +0000899 return -EPERM;
900 }
901
Simon Glass4b62b2f2017-06-14 21:28:33 -0600902 cap = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
Gabe Black19d1d412012-10-29 05:23:54 +0000903 cap = cpu_to_be64(cap);
904 memcpy(pccb->pdata, &cap, sizeof(cap));
905
906 block_size = cpu_to_be64((u64)512);
907 memcpy(&pccb->pdata[8], &block_size, 8);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800908
909 return 0;
910}
911
912
913/*
914 * SCSI TEST UNIT READY command operation.
915 */
Simon Glass4b62b2f2017-06-14 21:28:33 -0600916static int ata_scsiop_test_unit_ready(struct ahci_uc_priv *uc_priv,
917 struct scsi_cmd *pccb)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800918{
Simon Glass4b62b2f2017-06-14 21:28:33 -0600919 return (uc_priv->ataid[pccb->target]) ? 0 : -EPERM;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800920}
921
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500922
Simon Glass4e749012017-06-14 21:28:42 -0600923static int ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800924{
Simon Glass4682c8a2017-06-14 21:28:40 -0600925 struct ahci_uc_priv *uc_priv;
926#ifdef CONFIG_DM_SCSI
Simon Glassbfc1c6b2017-07-04 13:31:22 -0600927 uc_priv = dev_get_uclass_priv(dev->parent);
Simon Glass4682c8a2017-06-14 21:28:40 -0600928#else
929 uc_priv = probe_ent;
930#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800931 int ret;
932
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500933 switch (pccb->cmd[0]) {
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100934 case SCSI_READ16:
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800935 case SCSI_READ10:
Simon Glass225b1da2017-06-14 21:28:34 -0600936 ret = ata_scsiop_read_write(uc_priv, pccb, 0);
Hung-Te Linb7a21b72012-10-29 05:23:53 +0000937 break;
938 case SCSI_WRITE10:
Simon Glass225b1da2017-06-14 21:28:34 -0600939 ret = ata_scsiop_read_write(uc_priv, pccb, 1);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800940 break;
Gabe Black19d1d412012-10-29 05:23:54 +0000941 case SCSI_RD_CAPAC10:
Simon Glass4b62b2f2017-06-14 21:28:33 -0600942 ret = ata_scsiop_read_capacity10(uc_priv, pccb);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800943 break;
Gabe Black19d1d412012-10-29 05:23:54 +0000944 case SCSI_RD_CAPAC16:
Simon Glass4b62b2f2017-06-14 21:28:33 -0600945 ret = ata_scsiop_read_capacity16(uc_priv, pccb);
Gabe Black19d1d412012-10-29 05:23:54 +0000946 break;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800947 case SCSI_TST_U_RDY:
Simon Glass4b62b2f2017-06-14 21:28:33 -0600948 ret = ata_scsiop_test_unit_ready(uc_priv, pccb);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800949 break;
950 case SCSI_INQUIRY:
Simon Glass4b62b2f2017-06-14 21:28:33 -0600951 ret = ata_scsiop_inquiry(uc_priv, pccb);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800952 break;
953 default:
954 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
Simon Glassf6580ef2017-06-14 21:28:44 -0600955 return -ENOTSUPP;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800956 }
957
Jon Loeliger4a7cc0f2006-08-23 11:04:43 -0500958 if (ret) {
959 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
Simon Glassf6580ef2017-06-14 21:28:44 -0600960 return ret;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800961 }
Simon Glassf6580ef2017-06-14 21:28:44 -0600962 return 0;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800963
964}
965
Simon Glass62b4ec82017-06-14 21:28:36 -0600966static int ahci_start_ports(struct ahci_uc_priv *uc_priv)
967{
968 u32 linkmap;
969 int i;
970
971 linkmap = uc_priv->link_port_map;
972
Tuomas Tynkkynen8bf207d2018-09-13 01:28:54 +0300973 for (i = 0; i < uc_priv->n_ports; i++) {
Simon Glass62b4ec82017-06-14 21:28:36 -0600974 if (((linkmap >> i) & 0x01)) {
975 if (ahci_port_start(uc_priv, (u8) i)) {
976 printf("Can not start port %d\n", i);
977 continue;
978 }
979 }
980 }
981
982 return 0;
983}
984
Simon Glass7cf1afc2017-06-14 21:28:37 -0600985#ifndef CONFIG_DM_SCSI
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800986void scsi_low_level_init(int busdevfunc)
987{
Simon Glass225b1da2017-06-14 21:28:34 -0600988 struct ahci_uc_priv *uc_priv;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +0800989
Rob Herring942e3142011-07-06 16:13:36 +0000990#ifndef CONFIG_SCSI_AHCI_PLAT
Simon Glass4279efc2017-06-14 21:28:38 -0600991 probe_ent = calloc(1, sizeof(struct ahci_uc_priv));
992 if (!probe_ent) {
993 printf("%s: No memory for uc_priv\n", __func__);
994 return;
995 }
996 uc_priv = probe_ent;
Michal Simeke8a016b2016-09-08 15:06:45 +0200997# if defined(CONFIG_DM_PCI)
Simon Glassff758cc2015-11-29 13:18:06 -0700998 struct udevice *dev;
999 int ret;
1000
1001 ret = dm_pci_bus_find_bdf(busdevfunc, &dev);
1002 if (ret)
1003 return;
Simon Glass4279efc2017-06-14 21:28:38 -06001004 ahci_init_one(uc_priv, dev);
Simon Glassff758cc2015-11-29 13:18:06 -07001005# else
Simon Glass4279efc2017-06-14 21:28:38 -06001006 ahci_init_one(uc_priv, busdevfunc);
Simon Glassff758cc2015-11-29 13:18:06 -07001007# endif
Simon Glass4279efc2017-06-14 21:28:38 -06001008#else
Simon Glass225b1da2017-06-14 21:28:34 -06001009 uc_priv = probe_ent;
Simon Glass4279efc2017-06-14 21:28:38 -06001010#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001011
Simon Glass62b4ec82017-06-14 21:28:36 -06001012 ahci_start_ports(uc_priv);
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001013}
Simon Glass7cf1afc2017-06-14 21:28:37 -06001014#endif
1015
1016#ifndef CONFIG_SCSI_AHCI_PLAT
1017# if defined(CONFIG_DM_PCI) || defined(CONFIG_DM_SCSI)
Michal Simeke81589e2017-11-02 15:53:56 +01001018int ahci_init_one_dm(struct udevice *dev)
Simon Glass7cf1afc2017-06-14 21:28:37 -06001019{
Simon Glass4279efc2017-06-14 21:28:38 -06001020 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
1021
1022 return ahci_init_one(uc_priv, dev);
Simon Glass7cf1afc2017-06-14 21:28:37 -06001023}
1024#endif
1025#endif
1026
Michal Simeke81589e2017-11-02 15:53:56 +01001027int ahci_start_ports_dm(struct udevice *dev)
Simon Glass7cf1afc2017-06-14 21:28:37 -06001028{
Simon Glass4279efc2017-06-14 21:28:38 -06001029 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
Simon Glass7cf1afc2017-06-14 21:28:37 -06001030
1031 return ahci_start_ports(uc_priv);
1032}
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001033
Rob Herring942e3142011-07-06 16:13:36 +00001034#ifdef CONFIG_SCSI_AHCI_PLAT
Simon Glass4279efc2017-06-14 21:28:38 -06001035static int ahci_init_common(struct ahci_uc_priv *uc_priv, void __iomem *base)
Rob Herring942e3142011-07-06 16:13:36 +00001036{
Simon Glass4279efc2017-06-14 21:28:38 -06001037 int rc;
Rob Herring942e3142011-07-06 16:13:36 +00001038
Simon Glass225b1da2017-06-14 21:28:34 -06001039 uc_priv->host_flags = ATA_FLAG_SATA
Rob Herring942e3142011-07-06 16:13:36 +00001040 | ATA_FLAG_NO_LEGACY
1041 | ATA_FLAG_MMIO
1042 | ATA_FLAG_PIO_DMA
1043 | ATA_FLAG_NO_ATAPI;
Simon Glass225b1da2017-06-14 21:28:34 -06001044 uc_priv->pio_mask = 0x1f;
1045 uc_priv->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
Rob Herring942e3142011-07-06 16:13:36 +00001046
Simon Glass225b1da2017-06-14 21:28:34 -06001047 uc_priv->mmio_base = base;
Rob Herring942e3142011-07-06 16:13:36 +00001048
1049 /* initialize adapter */
Simon Glass225b1da2017-06-14 21:28:34 -06001050 rc = ahci_host_init(uc_priv);
Rob Herring942e3142011-07-06 16:13:36 +00001051 if (rc)
1052 goto err_out;
1053
Simon Glass225b1da2017-06-14 21:28:34 -06001054 ahci_print_info(uc_priv);
Rob Herring942e3142011-07-06 16:13:36 +00001055
Simon Glass62b4ec82017-06-14 21:28:36 -06001056 rc = ahci_start_ports(uc_priv);
Rob Herring942e3142011-07-06 16:13:36 +00001057
Rob Herring942e3142011-07-06 16:13:36 +00001058err_out:
1059 return rc;
1060}
Ian Campbellc6f3d502014-03-07 01:20:56 +00001061
Simon Glass4279efc2017-06-14 21:28:38 -06001062#ifndef CONFIG_DM_SCSI
1063int ahci_init(void __iomem *base)
1064{
1065 struct ahci_uc_priv *uc_priv;
1066
1067 probe_ent = malloc(sizeof(struct ahci_uc_priv));
1068 if (!probe_ent) {
1069 printf("%s: No memory for uc_priv\n", __func__);
1070 return -ENOMEM;
1071 }
1072
1073 uc_priv = probe_ent;
1074 memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
1075
1076 return ahci_init_common(uc_priv, base);
1077}
1078#endif
1079
1080int ahci_init_dm(struct udevice *dev, void __iomem *base)
1081{
1082 struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
1083
1084 return ahci_init_common(uc_priv, base);
1085}
1086
Ian Campbellc6f3d502014-03-07 01:20:56 +00001087void __weak scsi_init(void)
1088{
1089}
1090
Simon Glass4279efc2017-06-14 21:28:38 -06001091#endif /* CONFIG_SCSI_AHCI_PLAT */
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001092
Marc Jones766b16f2012-10-29 05:24:02 +00001093/*
1094 * In the general case of generic rotating media it makes sense to have a
1095 * flush capability. It probably even makes sense in the case of SSDs because
1096 * one cannot always know for sure what kind of internal cache/flush mechanism
1097 * is embodied therein. At first it was planned to invoke this after the last
1098 * write to disk and before rebooting. In practice, knowing, a priori, which
1099 * is the last write is difficult. Because writing to the disk in u-boot is
1100 * very rare, this flush command will be invoked after every block write.
1101 */
Simon Glass225b1da2017-06-14 21:28:34 -06001102static int ata_io_flush(struct ahci_uc_priv *uc_priv, u8 port)
Marc Jones766b16f2012-10-29 05:24:02 +00001103{
1104 u8 fis[20];
Simon Glass225b1da2017-06-14 21:28:34 -06001105 struct ahci_ioports *pp = &(uc_priv->port[port]);
Tang Yuantianfa313772015-07-09 14:37:30 +08001106 void __iomem *port_mmio = pp->port_mmio;
Marc Jones766b16f2012-10-29 05:24:02 +00001107 u32 cmd_fis_len = 5; /* five dwords */
1108
1109 /* Preset the FIS */
1110 memset(fis, 0, 20);
1111 fis[0] = 0x27; /* Host to device FIS. */
1112 fis[1] = 1 << 7; /* Command FIS. */
Walter Murphyfe1f8082012-10-29 05:24:03 +00001113 fis[2] = ATA_CMD_FLUSH_EXT;
Marc Jones766b16f2012-10-29 05:24:02 +00001114
1115 memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
1116 ahci_fill_cmd_slot(pp, cmd_fis_len);
Tang Yuantian75e14b12016-04-14 16:21:00 +08001117 ahci_dcache_flush_sata_cmd(pp);
Marc Jones766b16f2012-10-29 05:24:02 +00001118 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
1119
1120 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
1121 WAIT_MS_FLUSH, 0x1)) {
1122 debug("scsi_ahci: flush command timeout on port %d.\n", port);
1123 return -EIO;
1124 }
1125
1126 return 0;
1127}
1128
Simon Glass4e749012017-06-14 21:28:42 -06001129static int ahci_scsi_bus_reset(struct udevice *dev)
1130{
1131 /* Not implemented */
1132
1133 return 0;
1134}
1135
Simon Glassf6ab5a92017-06-14 21:28:43 -06001136#ifdef CONFIG_DM_SCSI
Simon Glass681357f2017-06-14 21:28:46 -06001137int ahci_bind_scsi(struct udevice *ahci_dev, struct udevice **devp)
1138{
1139 struct udevice *dev;
1140 int ret;
1141
1142 ret = device_bind_driver(ahci_dev, "ahci_scsi", "ahci_scsi", &dev);
1143 if (ret)
1144 return ret;
1145 *devp = dev;
1146
1147 return 0;
1148}
1149
Simon Glass745a94f2017-07-04 13:31:18 -06001150int ahci_probe_scsi(struct udevice *ahci_dev, ulong base)
Simon Glass681357f2017-06-14 21:28:46 -06001151{
Simon Glass681357f2017-06-14 21:28:46 -06001152 struct ahci_uc_priv *uc_priv;
1153 struct scsi_platdata *uc_plat;
1154 struct udevice *dev;
1155 int ret;
1156
1157 device_find_first_child(ahci_dev, &dev);
1158 if (!dev)
1159 return -ENODEV;
1160 uc_plat = dev_get_uclass_platdata(dev);
Simon Glass745a94f2017-07-04 13:31:18 -06001161 uc_plat->base = base;
Simon Glass681357f2017-06-14 21:28:46 -06001162 uc_plat->max_lun = 1;
1163 uc_plat->max_id = 2;
Simon Glass745a94f2017-07-04 13:31:18 -06001164
1165 uc_priv = dev_get_uclass_priv(ahci_dev);
Simon Glass681357f2017-06-14 21:28:46 -06001166 ret = ahci_init_one(uc_priv, dev);
1167 if (ret)
1168 return ret;
1169 ret = ahci_start_ports(uc_priv);
1170 if (ret)
1171 return ret;
Simon Glass681357f2017-06-14 21:28:46 -06001172
Park, Aidenbd98e6a2019-08-20 16:47:42 +00001173 /*
1174 * scsi_scan_dev() scans devices up-to the number of max_id.
1175 * Update max_id if the number of detected ports exceeds max_id.
1176 * This allows SCSI to scan all detected ports.
1177 */
1178 uc_plat->max_id = max_t(unsigned long, uc_priv->n_ports,
1179 uc_plat->max_id);
1180
Simon Glass681357f2017-06-14 21:28:46 -06001181 return 0;
1182}
1183
Simon Glass745a94f2017-07-04 13:31:18 -06001184#ifdef CONFIG_DM_PCI
1185int ahci_probe_scsi_pci(struct udevice *ahci_dev)
1186{
1187 ulong base;
1188
1189 base = (ulong)dm_pci_map_bar(ahci_dev, PCI_BASE_ADDRESS_5,
1190 PCI_REGION_MEM);
1191
1192 return ahci_probe_scsi(ahci_dev, base);
1193}
1194#endif
1195
Simon Glassf6ab5a92017-06-14 21:28:43 -06001196struct scsi_ops scsi_ops = {
1197 .exec = ahci_scsi_exec,
1198 .bus_reset = ahci_scsi_bus_reset,
1199};
Simon Glass681357f2017-06-14 21:28:46 -06001200
1201U_BOOT_DRIVER(ahci_scsi) = {
1202 .name = "ahci_scsi",
1203 .id = UCLASS_SCSI,
1204 .ops = &scsi_ops,
1205};
Simon Glassf6ab5a92017-06-14 21:28:43 -06001206#else
Simon Glass4e749012017-06-14 21:28:42 -06001207int scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
1208{
1209 return ahci_scsi_exec(dev, pccb);
1210}
Marc Jones766b16f2012-10-29 05:24:02 +00001211
Simon Glass4682c8a2017-06-14 21:28:40 -06001212__weak int scsi_bus_reset(struct udevice *dev)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001213{
Simon Glass4e749012017-06-14 21:28:42 -06001214 return ahci_scsi_bus_reset(dev);
Simon Glass4682c8a2017-06-14 21:28:40 -06001215
1216 return 0;
Jin Zhengxiong4782ac82006-08-23 19:10:44 +08001217}
Simon Glassf6ab5a92017-06-14 21:28:43 -06001218#endif