Blame view

drivers/ata/ahci.c 29.2 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
4782ac80b   Jin Zhengxiong   Add AHCI support ...
2
  /*
4c2e3da82   Kumar Gala   Update Freescale ...
3
   * Copyright (C) Freescale Semiconductor, Inc. 2006.
4782ac80b   Jin Zhengxiong   Add AHCI support ...
4
5
6
   * Author: Jason Jin<Jason.jin@freescale.com>
   *         Zhang Wei<wei.zhang@freescale.com>
   *
4782ac80b   Jin Zhengxiong   Add AHCI support ...
7
   * with the reference on libata and ahci drvier in kernel
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
8
9
   *
   * This driver provides a SCSI interface to SATA.
4782ac80b   Jin Zhengxiong   Add AHCI support ...
10
11
   */
  #include <common.h>
1eb69ae49   Simon Glass   common: Move ARM ...
12
  #include <cpu_func.h>
4782ac80b   Jin Zhengxiong   Add AHCI support ...
13

4782ac80b   Jin Zhengxiong   Add AHCI support ...
14
  #include <command.h>
ff758ccc8   Simon Glass   dm: ahci: Convert...
15
  #include <dm.h>
4782ac80b   Jin Zhengxiong   Add AHCI support ...
16
17
  #include <pci.h>
  #include <asm/processor.h>
1221ce459   Masahiro Yamada   treewide: replace...
18
  #include <linux/errno.h>
4782ac80b   Jin Zhengxiong   Add AHCI support ...
19
20
  #include <asm/io.h>
  #include <malloc.h>
cf92e05c0   Simon Glass   Move ALLOC_CACHE_...
21
  #include <memalign.h>
681357ffd   Simon Glass   dm: ahci: Add a d...
22
  #include <pci.h>
4782ac80b   Jin Zhengxiong   Add AHCI support ...
23
  #include <scsi.h>
344ca0b40   Rob Herring   ahci: convert to ...
24
  #include <libata.h>
4782ac80b   Jin Zhengxiong   Add AHCI support ...
25
26
  #include <linux/ctype.h>
  #include <ahci.h>
681357ffd   Simon Glass   dm: ahci: Add a d...
27
28
  #include <dm/device-internal.h>
  #include <dm/lists.h>
4782ac80b   Jin Zhengxiong   Add AHCI support ...
29

225b1da7b   Simon Glass   dm: ahci: Refacto...
30
  static int ata_io_flush(struct ahci_uc_priv *uc_priv, u8 port);
766b16fe1   Marc Jones   ahci: Perform SAT...
31

4682c8a19   Simon Glass   dm: scsi: Add a d...
32
  #ifndef CONFIG_DM_SCSI
2c9f9efb3   Simon Glass   dm: ahci: Rename ...
33
  struct ahci_uc_priv *probe_ent = NULL;
4682c8a19   Simon Glass   dm: scsi: Add a d...
34
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
35

4a7cc0f21   Jon Loeliger   Cleanup and linde...
36
  #define writel_with_flush(a,b)	do { writel(a,b); readl(b); } while (0)
284231e49   Vadim Bendebury   ahci: Support spl...
37
  /*
b7a21b70d   Hung-Te Lin   ahci: support scs...
38
39
40
41
   * Some controllers limit number of blocks they can read/write at once.
   * Contemporary SSD devices work much faster if the read/write size is aligned
   * to a power of 2.  Let's set default to 128 and allowing to be overwritten if
   * needed.
284231e49   Vadim Bendebury   ahci: Support spl...
42
   */
b7a21b70d   Hung-Te Lin   ahci: support scs...
43
44
  #ifndef MAX_SATA_BLOCKS_READ_WRITE
  #define MAX_SATA_BLOCKS_READ_WRITE	0x80
284231e49   Vadim Bendebury   ahci: Support spl...
45
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
46

57847660b   Walter Murphy   ahci: Adjust SATA...
47
  /* Maximum timeouts for each event */
7610b41dd   Rob Herring   ahci: increase sp...
48
  #define WAIT_MS_SPINUP	20000
f8b009e8b   Mark Langsdorf   ahci: extend data...
49
  #define WAIT_MS_DATAIO	10000
766b16fe1   Marc Jones   ahci: Perform SAT...
50
  #define WAIT_MS_FLUSH	5000
e0ddcf93b   Ian Campbell   AHCI: Increase li...
51
  #define WAIT_MS_LINKUP	200
57847660b   Walter Murphy   ahci: Adjust SATA...
52

6e7325533   Roman Kapl   ata: ahci allow 6...
53
  #define AHCI_CAP_S64A BIT(31)
22f5de6b5   Stefan Roese   ahci: Make ahci_p...
54
  __weak void __iomem *ahci_port_base(void __iomem *base, u32 port)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
55
56
57
  {
  	return base + 0x100 + (port * 0x80);
  }
4782ac80b   Jin Zhengxiong   Add AHCI support ...
58
  #define msleep(a) udelay(a * 1000)
4a7cc0f21   Jon Loeliger   Cleanup and linde...
59

fa31377ef   Tang Yuantian   ahci: Fix compili...
60
  static void ahci_dcache_flush_range(unsigned long begin, unsigned long len)
90b276f6a   Taylor Hutt   ahci: flush / inv...
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  {
  	const unsigned long start = begin;
  	const unsigned long end = start + len;
  
  	debug("%s: flush dcache: [%#lx, %#lx)
  ", __func__, start, end);
  	flush_dcache_range(start, end);
  }
  
  /*
   * SATA controller DMAs to physical RAM.  Ensure data from the
   * controller is invalidated from dcache; next access comes from
   * physical RAM.
   */
fa31377ef   Tang Yuantian   ahci: Fix compili...
75
  static void ahci_dcache_invalidate_range(unsigned long begin, unsigned long len)
90b276f6a   Taylor Hutt   ahci: flush / inv...
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  {
  	const unsigned long start = begin;
  	const unsigned long end = start + len;
  
  	debug("%s: invalidate dcache: [%#lx, %#lx)
  ", __func__, start, end);
  	invalidate_dcache_range(start, end);
  }
  
  /*
   * Ensure data for SATA controller is flushed out of dcache and
   * written to physical memory.
   */
  static void ahci_dcache_flush_sata_cmd(struct ahci_ioports *pp)
  {
  	ahci_dcache_flush_range((unsigned long)pp->cmd_slot,
  				AHCI_PORT_PRIV_DMA_SZ);
  }
fa31377ef   Tang Yuantian   ahci: Fix compili...
94
  static int waiting_for_cmd_completed(void __iomem *offset,
4a7cc0f21   Jon Loeliger   Cleanup and linde...
95
96
  				     int timeout_msec,
  				     u32 sign)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
97
98
99
  {
  	int i;
  	u32 status;
4a7cc0f21   Jon Loeliger   Cleanup and linde...
100
101
  
  	for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
102
  		msleep(1);
4a7cc0f21   Jon Loeliger   Cleanup and linde...
103
  	return (i < timeout_msec) ? 0 : -1;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
104
  }
4b62b2ff5   Simon Glass   dm: sata: Move at...
105
  int __weak ahci_link_up(struct ahci_uc_priv *uc_priv, u8 port)
124e9fa13   Rob Herring   ahci: move link b...
106
107
108
  {
  	u32 tmp;
  	int j = 0;
4b62b2ff5   Simon Glass   dm: sata: Move at...
109
  	void __iomem *port_mmio = uc_priv->port[port].port_mmio;
124e9fa13   Rob Herring   ahci: move link b...
110

3765b3e7b   Wolfgang Denk   Coding Style clea...
111
  	/*
124e9fa13   Rob Herring   ahci: move link b...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  	 * Bring up SATA link.
  	 * SATA link bringup time is usually less than 1 ms; only very
  	 * rarely has it taken between 1-2 ms. Never seen it above 2 ms.
  	 */
  	while (j < WAIT_MS_LINKUP) {
  		tmp = readl(port_mmio + PORT_SCR_STAT);
  		tmp &= PORT_SCR_STAT_DET_MASK;
  		if (tmp == PORT_SCR_STAT_DET_PHYRDY)
  			return 0;
  		udelay(1000);
  		j++;
  	}
  	return 1;
  }
4782ac80b   Jin Zhengxiong   Add AHCI support ...
126

a6e50a88d   Ian Campbell   ahci: provide sun...
127
128
  #ifdef CONFIG_SUNXI_AHCI
  /* The sunxi AHCI controller requires this undocumented setup */
fa31377ef   Tang Yuantian   ahci: Fix compili...
129
  static void sunxi_dma_init(void __iomem *port_mmio)
a6e50a88d   Ian Campbell   ahci: provide sun...
130
131
132
133
  {
  	clrsetbits_le32(port_mmio + PORT_P0DMACR, 0x0000ff00, 0x00004400);
  }
  #endif
9efaca3e8   Scott Wood   ahci: mmio_base i...
134
  int ahci_reset(void __iomem *base)
6b68888a3   Dmitry Lifshitz   ahci: introduce a...
135
136
  {
  	int i = 1000;
9efaca3e8   Scott Wood   ahci: mmio_base i...
137
  	u32 __iomem *host_ctl_reg = base + HOST_CTL;
6b68888a3   Dmitry Lifshitz   ahci: introduce a...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  	u32 tmp = readl(host_ctl_reg); /* global controller reset */
  
  	if ((tmp & HOST_RESET) == 0)
  		writel_with_flush(tmp | HOST_RESET, host_ctl_reg);
  
  	/*
  	 * reset must complete within 1 second, or
  	 * the hardware should be considered fried.
  	 */
  	do {
  		udelay(1000);
  		tmp = readl(host_ctl_reg);
  		i--;
  	} while ((i > 0) && (tmp & HOST_RESET));
  
  	if (i == 0) {
  		printf("controller reset failed (0x%x)
  ", tmp);
  		return -1;
  	}
  
  	return 0;
  }
225b1da7b   Simon Glass   dm: ahci: Refacto...
161
  static int ahci_host_init(struct ahci_uc_priv *uc_priv)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
162
  {
e8a016b53   Michal Simek   dm: Add support f...
163
  #if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
ff758ccc8   Simon Glass   dm: ahci: Convert...
164
  # ifdef CONFIG_DM_PCI
225b1da7b   Simon Glass   dm: ahci: Refacto...
165
  	struct udevice *dev = uc_priv->dev;
ff758ccc8   Simon Glass   dm: ahci: Convert...
166
167
  	struct pci_child_platdata *pplat = dev_get_parent_platdata(dev);
  # else
225b1da7b   Simon Glass   dm: ahci: Refacto...
168
  	pci_dev_t pdev = uc_priv->dev;
942e31437   Rob Herring   scsi/ahci: add su...
169
  	unsigned short vendor;
ff758ccc8   Simon Glass   dm: ahci: Convert...
170
171
  # endif
  	u16 tmp16;
942e31437   Rob Herring   scsi/ahci: add su...
172
  #endif
225b1da7b   Simon Glass   dm: ahci: Refacto...
173
  	void __iomem *mmio = uc_priv->mmio_base;
2a0c61d40   Marc Jones   ahci: Support spi...
174
  	u32 tmp, cap_save, cmd;
124e9fa13   Rob Herring   ahci: move link b...
175
  	int i, j, ret;
fa31377ef   Tang Yuantian   ahci: Fix compili...
176
  	void __iomem *port_mmio;
2915a0223   Richard Gibbs   ahci: use ports i...
177
  	u32 port_map;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
178

284231e49   Vadim Bendebury   ahci: Support spl...
179
180
  	debug("ahci_host_init: start
  ");
4782ac80b   Jin Zhengxiong   Add AHCI support ...
181
  	cap_save = readl(mmio + HOST_CAP);
4a7cc0f21   Jon Loeliger   Cleanup and linde...
182
  	cap_save &= ((1 << 28) | (1 << 17));
2a0c61d40   Marc Jones   ahci: Support spi...
183
  	cap_save |= (1 << 27);  /* Staggered Spin-up. Not needed. */
4782ac80b   Jin Zhengxiong   Add AHCI support ...
184

225b1da7b   Simon Glass   dm: ahci: Refacto...
185
  	ret = ahci_reset(uc_priv->mmio_base);
6b68888a3   Dmitry Lifshitz   ahci: introduce a...
186
187
  	if (ret)
  		return ret;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
188
189
190
191
  
  	writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
  	writel(cap_save, mmio + HOST_CAP);
  	writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
e8a016b53   Michal Simek   dm: Add support f...
192
  #if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
ff758ccc8   Simon Glass   dm: ahci: Convert...
193
194
195
196
197
198
199
200
  # ifdef CONFIG_DM_PCI
  	if (pplat->vendor == PCI_VENDOR_ID_INTEL) {
  		u16 tmp16;
  
  		dm_pci_read_config16(dev, 0x92, &tmp16);
  		dm_pci_write_config16(dev, 0x92, tmp16 | 0xf);
  	}
  # else
4782ac80b   Jin Zhengxiong   Add AHCI support ...
201
202
203
204
205
206
207
208
  	pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
  
  	if (vendor == PCI_VENDOR_ID_INTEL) {
  		u16 tmp16;
  		pci_read_config_word(pdev, 0x92, &tmp16);
  		tmp16 |= 0xf;
  		pci_write_config_word(pdev, 0x92, tmp16);
  	}
ff758ccc8   Simon Glass   dm: ahci: Convert...
209
  # endif
942e31437   Rob Herring   scsi/ahci: add su...
210
  #endif
225b1da7b   Simon Glass   dm: ahci: Refacto...
211
212
213
214
  	uc_priv->cap = readl(mmio + HOST_CAP);
  	uc_priv->port_map = readl(mmio + HOST_PORTS_IMPL);
  	port_map = uc_priv->port_map;
  	uc_priv->n_ports = (uc_priv->cap & 0x1f) + 1;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
215
216
217
  
  	debug("cap 0x%x  port_map 0x%x  n_ports %d
  ",
225b1da7b   Simon Glass   dm: ahci: Refacto...
218
  	      uc_priv->cap, uc_priv->port_map, uc_priv->n_ports);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
219

0545ac989   Tuomas Tynkkynen   ata: ahci: Don't ...
220
  #if !defined(CONFIG_DM_SCSI)
225b1da7b   Simon Glass   dm: ahci: Refacto...
221
222
  	if (uc_priv->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
  		uc_priv->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
0545ac989   Tuomas Tynkkynen   ata: ahci: Don't ...
223
  #endif
284231e49   Vadim Bendebury   ahci: Support spl...
224

225b1da7b   Simon Glass   dm: ahci: Refacto...
225
  	for (i = 0; i < uc_priv->n_ports; i++) {
2915a0223   Richard Gibbs   ahci: use ports i...
226
227
  		if (!(port_map & (1 << i)))
  			continue;
225b1da7b   Simon Glass   dm: ahci: Refacto...
228
229
  		uc_priv->port[i].port_mmio = ahci_port_base(mmio, i);
  		port_mmio = (u8 *)uc_priv->port[i].port_mmio;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
230
231
232
233
234
  
  		/* make sure port is not active */
  		tmp = readl(port_mmio + PORT_CMD);
  		if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  			   PORT_CMD_FIS_RX | PORT_CMD_START)) {
7ba7917c9   Stefan Reinauer   ahci: Improve AHC...
235
236
  			debug("Port %d is active. Deactivating.
  ", i);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
237
238
239
240
241
242
243
244
245
  			tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
  				 PORT_CMD_FIS_RX | PORT_CMD_START);
  			writel_with_flush(tmp, port_mmio + PORT_CMD);
  
  			/* spec says 500 msecs for each bit, so
  			 * this is slightly incorrect.
  			 */
  			msleep(500);
  		}
a6e50a88d   Ian Campbell   ahci: provide sun...
246
247
248
  #ifdef CONFIG_SUNXI_AHCI
  		sunxi_dma_init(port_mmio);
  #endif
2a0c61d40   Marc Jones   ahci: Support spi...
249
250
251
252
  		/* Add the spinup command to whatever mode bits may
  		 * already be on in the command register.
  		 */
  		cmd = readl(port_mmio + PORT_CMD);
2a0c61d40   Marc Jones   ahci: Support spi...
253
254
  		cmd |= PORT_CMD_SPIN_UP;
  		writel_with_flush(cmd, port_mmio + PORT_CMD);
124e9fa13   Rob Herring   ahci: move link b...
255
  		/* Bring up SATA link. */
225b1da7b   Simon Glass   dm: ahci: Refacto...
256
  		ret = ahci_link_up(uc_priv, i);
124e9fa13   Rob Herring   ahci: move link b...
257
  		if (ret) {
2a0c61d40   Marc Jones   ahci: Support spi...
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
  			printf("SATA link %d timeout.
  ", i);
  			continue;
  		} else {
  			debug("SATA link ok.
  ");
  		}
  
  		/* Clear error status */
  		tmp = readl(port_mmio + PORT_SCR_ERR);
  		if (tmp)
  			writel(tmp, port_mmio + PORT_SCR_ERR);
  
  		debug("Spinning up device on SATA port %d... ", i);
  
  		j = 0;
  		while (j < WAIT_MS_SPINUP) {
  			tmp = readl(port_mmio + PORT_TFDATA);
344ca0b40   Rob Herring   ahci: convert to ...
276
  			if (!(tmp & (ATA_BUSY | ATA_DRQ)))
2a0c61d40   Marc Jones   ahci: Support spi...
277
278
  				break;
  			udelay(1000);
178210847   Rob Herring   ahci: handle COMI...
279
280
281
282
  			tmp = readl(port_mmio + PORT_SCR_STAT);
  			tmp &= PORT_SCR_STAT_DET_MASK;
  			if (tmp == PORT_SCR_STAT_DET_PHYRDY)
  				break;
2a0c61d40   Marc Jones   ahci: Support spi...
283
284
  			j++;
  		}
178210847   Rob Herring   ahci: handle COMI...
285
286
287
288
289
290
291
292
  
  		tmp = readl(port_mmio + PORT_SCR_STAT) & PORT_SCR_STAT_DET_MASK;
  		if (tmp == PORT_SCR_STAT_DET_COMINIT) {
  			debug("SATA link %d down (COMINIT received), retrying...
  ", i);
  			i--;
  			continue;
  		}
2a0c61d40   Marc Jones   ahci: Support spi...
293
294
295
  		printf("Target spinup took %d ms.
  ", j);
  		if (j == WAIT_MS_SPINUP)
9a65b8754   Stefan Reinauer   ahci: Optimise AH...
296
297
298
299
300
  			debug("timeout.
  ");
  		else
  			debug("ok.
  ");
4782ac80b   Jin Zhengxiong   Add AHCI support ...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
  
  		tmp = readl(port_mmio + PORT_SCR_ERR);
  		debug("PORT_SCR_ERR 0x%x
  ", tmp);
  		writel(tmp, port_mmio + PORT_SCR_ERR);
  
  		/* ack any pending irq events for this port */
  		tmp = readl(port_mmio + PORT_IRQ_STAT);
  		debug("PORT_IRQ_STAT 0x%x
  ", tmp);
  		if (tmp)
  			writel(tmp, port_mmio + PORT_IRQ_STAT);
  
  		writel(1 << i, mmio + HOST_IRQ_STAT);
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
315
  		/* register linkup ports */
4782ac80b   Jin Zhengxiong   Add AHCI support ...
316
  		tmp = readl(port_mmio + PORT_SCR_STAT);
766b16fe1   Marc Jones   ahci: Perform SAT...
317
318
  		debug("SATA port %d status: 0x%x
  ", i, tmp);
2bdb10dbf   Rob Herring   ahci: add defines...
319
  		if ((tmp & PORT_SCR_STAT_DET_MASK) == PORT_SCR_STAT_DET_PHYRDY)
225b1da7b   Simon Glass   dm: ahci: Refacto...
320
  			uc_priv->link_port_map |= (0x01 << i);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
321
322
323
324
325
326
327
328
329
  	}
  
  	tmp = readl(mmio + HOST_CTL);
  	debug("HOST_CTL 0x%x
  ", tmp);
  	writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  	tmp = readl(mmio + HOST_CTL);
  	debug("HOST_CTL 0x%x
  ", tmp);
e8a016b53   Michal Simek   dm: Add support f...
330
  #if !defined(CONFIG_DM_SCSI)
942e31437   Rob Herring   scsi/ahci: add su...
331
  #ifndef CONFIG_SCSI_AHCI_PLAT
ff758ccc8   Simon Glass   dm: ahci: Convert...
332
333
334
335
336
  # ifdef CONFIG_DM_PCI
  	dm_pci_read_config16(dev, PCI_COMMAND, &tmp16);
  	tmp |= PCI_COMMAND_MASTER;
  	dm_pci_write_config16(dev, PCI_COMMAND, tmp16);
  # else
4782ac80b   Jin Zhengxiong   Add AHCI support ...
337
338
339
  	pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
  	tmp |= PCI_COMMAND_MASTER;
  	pci_write_config_word(pdev, PCI_COMMAND, tmp16);
ff758ccc8   Simon Glass   dm: ahci: Convert...
340
  # endif
942e31437   Rob Herring   scsi/ahci: add su...
341
  #endif
e8a016b53   Michal Simek   dm: Add support f...
342
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
343
344
  	return 0;
  }
225b1da7b   Simon Glass   dm: ahci: Refacto...
345
  static void ahci_print_info(struct ahci_uc_priv *uc_priv)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
346
  {
e8a016b53   Michal Simek   dm: Add support f...
347
348
  #if !defined(CONFIG_SCSI_AHCI_PLAT) && !defined(CONFIG_DM_SCSI)
  # if defined(CONFIG_DM_PCI)
225b1da7b   Simon Glass   dm: ahci: Refacto...
349
  	struct udevice *dev = uc_priv->dev;
ff758ccc8   Simon Glass   dm: ahci: Convert...
350
  # else
225b1da7b   Simon Glass   dm: ahci: Refacto...
351
  	pci_dev_t pdev = uc_priv->dev;
ff758ccc8   Simon Glass   dm: ahci: Convert...
352
  # endif
942e31437   Rob Herring   scsi/ahci: add su...
353
354
  	u16 cc;
  #endif
225b1da7b   Simon Glass   dm: ahci: Refacto...
355
  	void __iomem *mmio = uc_priv->mmio_base;
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
356
  	u32 vers, cap, cap2, impl, speed;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
357
  	const char *speed_s;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
358
359
360
  	const char *scc_s;
  
  	vers = readl(mmio + HOST_VERSION);
225b1da7b   Simon Glass   dm: ahci: Refacto...
361
  	cap = uc_priv->cap;
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
362
  	cap2 = readl(mmio + HOST_CAP2);
225b1da7b   Simon Glass   dm: ahci: Refacto...
363
  	impl = uc_priv->port_map;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
364
365
366
367
368
369
  
  	speed = (cap >> 20) & 0xf;
  	if (speed == 1)
  		speed_s = "1.5";
  	else if (speed == 2)
  		speed_s = "3";
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
370
371
  	else if (speed == 3)
  		speed_s = "6";
4782ac80b   Jin Zhengxiong   Add AHCI support ...
372
373
  	else
  		speed_s = "?";
e8a016b53   Michal Simek   dm: Add support f...
374
  #if defined(CONFIG_SCSI_AHCI_PLAT) || defined(CONFIG_DM_SCSI)
942e31437   Rob Herring   scsi/ahci: add su...
375
376
  	scc_s = "SATA";
  #else
ff758ccc8   Simon Glass   dm: ahci: Convert...
377
378
379
  # ifdef CONFIG_DM_PCI
  	dm_pci_read_config16(dev, 0x0a, &cc);
  # else
4782ac80b   Jin Zhengxiong   Add AHCI support ...
380
  	pci_read_config_word(pdev, 0x0a, &cc);
ff758ccc8   Simon Glass   dm: ahci: Convert...
381
  # endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
382
383
384
385
386
387
388
389
  	if (cc == 0x0101)
  		scc_s = "IDE";
  	else if (cc == 0x0106)
  		scc_s = "SATA";
  	else if (cc == 0x0104)
  		scc_s = "RAID";
  	else
  		scc_s = "unknown";
942e31437   Rob Herring   scsi/ahci: add su...
390
  #endif
4a7cc0f21   Jon Loeliger   Cleanup and linde...
391
392
393
394
395
396
397
398
  	printf("AHCI %02x%02x.%02x%02x "
  	       "%u slots %u ports %s Gbps 0x%x impl %s mode
  ",
  	       (vers >> 24) & 0xff,
  	       (vers >> 16) & 0xff,
  	       (vers >> 8) & 0xff,
  	       vers & 0xff,
  	       ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
399
400
  
  	printf("flags: "
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
401
402
403
404
  	       "%s%s%s%s%s%s%s"
  	       "%s%s%s%s%s%s%s"
  	       "%s%s%s%s%s%s
  ",
4a7cc0f21   Jon Loeliger   Cleanup and linde...
405
406
407
408
409
410
411
412
413
414
  	       cap & (1 << 31) ? "64bit " : "",
  	       cap & (1 << 30) ? "ncq " : "",
  	       cap & (1 << 28) ? "ilck " : "",
  	       cap & (1 << 27) ? "stag " : "",
  	       cap & (1 << 26) ? "pm " : "",
  	       cap & (1 << 25) ? "led " : "",
  	       cap & (1 << 24) ? "clo " : "",
  	       cap & (1 << 19) ? "nz " : "",
  	       cap & (1 << 18) ? "only " : "",
  	       cap & (1 << 17) ? "pmp " : "",
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
415
  	       cap & (1 << 16) ? "fbss " : "",
4a7cc0f21   Jon Loeliger   Cleanup and linde...
416
417
  	       cap & (1 << 15) ? "pio " : "",
  	       cap & (1 << 14) ? "slum " : "",
4e422bce8   Stefan Reinauer   ahci: cosmetics a...
418
419
420
421
422
423
424
  	       cap & (1 << 13) ? "part " : "",
  	       cap & (1 << 7) ? "ccc " : "",
  	       cap & (1 << 6) ? "ems " : "",
  	       cap & (1 << 5) ? "sxs " : "",
  	       cap2 & (1 << 2) ? "apst " : "",
  	       cap2 & (1 << 1) ? "nvmp " : "",
  	       cap2 & (1 << 0) ? "boh " : "");
4782ac80b   Jin Zhengxiong   Add AHCI support ...
425
  }
745a94f35   Simon Glass   ahci: Support non...
426
  #if defined(CONFIG_DM_SCSI) || !defined(CONFIG_SCSI_AHCI_PLAT)
e8a016b53   Michal Simek   dm: Add support f...
427
  # if defined(CONFIG_DM_PCI) || defined(CONFIG_DM_SCSI)
4279efc4c   Simon Glass   dm: ahci: Drop us...
428
  static int ahci_init_one(struct ahci_uc_priv *uc_priv, struct udevice *dev)
ff758ccc8   Simon Glass   dm: ahci: Convert...
429
  # else
4279efc4c   Simon Glass   dm: ahci: Drop us...
430
  static int ahci_init_one(struct ahci_uc_priv *uc_priv, pci_dev_t dev)
ff758ccc8   Simon Glass   dm: ahci: Convert...
431
  # endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
432
  {
e8a016b53   Michal Simek   dm: Add support f...
433
  #if !defined(CONFIG_DM_SCSI)
63cec5814   Ed Swarthout   Make MPC8641's PC...
434
  	u16 vendor;
e8a016b53   Michal Simek   dm: Add support f...
435
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
436
  	int rc;
225b1da7b   Simon Glass   dm: ahci: Refacto...
437
  	uc_priv->dev = dev;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
438

225b1da7b   Simon Glass   dm: ahci: Refacto...
439
  	uc_priv->host_flags = ATA_FLAG_SATA
4a7cc0f21   Jon Loeliger   Cleanup and linde...
440
441
442
443
  				| ATA_FLAG_NO_LEGACY
  				| ATA_FLAG_MMIO
  				| ATA_FLAG_PIO_DMA
  				| ATA_FLAG_NO_ATAPI;
225b1da7b   Simon Glass   dm: ahci: Refacto...
444
445
  	uc_priv->pio_mask = 0x1f;
  	uc_priv->udma_mask = 0x7f;	/*Fixme,assume to support UDMA6 */
4782ac80b   Jin Zhengxiong   Add AHCI support ...
446

e8a016b53   Michal Simek   dm: Add support f...
447
  #if !defined(CONFIG_DM_SCSI)
ff758ccc8   Simon Glass   dm: ahci: Convert...
448
  #ifdef CONFIG_DM_PCI
225b1da7b   Simon Glass   dm: ahci: Refacto...
449
  	uc_priv->mmio_base = dm_pci_map_bar(dev, PCI_BASE_ADDRESS_5,
ff758ccc8   Simon Glass   dm: ahci: Convert...
450
451
452
453
454
455
456
457
458
459
  					      PCI_REGION_MEM);
  
  	/* Take from kernel:
  	 * JMicron-specific fixup:
  	 * make sure we're in AHCI mode
  	 */
  	dm_pci_read_config16(dev, PCI_VENDOR_ID, &vendor);
  	if (vendor == 0x197b)
  		dm_pci_write_config8(dev, 0x41, 0xa1);
  #else
225b1da7b   Simon Glass   dm: ahci: Refacto...
460
  	uc_priv->mmio_base = pci_map_bar(dev, PCI_BASE_ADDRESS_5,
9efaca3e8   Scott Wood   ahci: mmio_base i...
461
  					   PCI_REGION_MEM);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
462
463
464
465
466
  
  	/* Take from kernel:
  	 * JMicron-specific fixup:
  	 * make sure we're in AHCI mode
  	 */
ff758ccc8   Simon Glass   dm: ahci: Convert...
467
  	pci_read_config_word(dev, PCI_VENDOR_ID, &vendor);
4a7cc0f21   Jon Loeliger   Cleanup and linde...
468
  	if (vendor == 0x197b)
ff758ccc8   Simon Glass   dm: ahci: Convert...
469
470
  		pci_write_config_byte(dev, 0x41, 0xa1);
  #endif
e8a016b53   Michal Simek   dm: Add support f...
471
  #else
1dc64f6c0   Simon Glass   dm: scsi: Use the...
472
  	struct scsi_platdata *plat = dev_get_uclass_platdata(dev);
225b1da7b   Simon Glass   dm: ahci: Refacto...
473
  	uc_priv->mmio_base = (void *)plat->base;
e8a016b53   Michal Simek   dm: Add support f...
474
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
475

225b1da7b   Simon Glass   dm: ahci: Refacto...
476
477
  	debug("ahci mmio_base=0x%p
  ", uc_priv->mmio_base);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
478
  	/* initialize adapter */
225b1da7b   Simon Glass   dm: ahci: Refacto...
479
  	rc = ahci_host_init(uc_priv);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
480
481
  	if (rc)
  		goto err_out;
225b1da7b   Simon Glass   dm: ahci: Refacto...
482
  	ahci_print_info(uc_priv);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
483
484
  
  	return 0;
4a7cc0f21   Jon Loeliger   Cleanup and linde...
485
        err_out:
4782ac80b   Jin Zhengxiong   Add AHCI support ...
486
487
  	return rc;
  }
942e31437   Rob Herring   scsi/ahci: add su...
488
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
489
490
  
  #define MAX_DATA_BYTE_COUNT  (4*1024*1024)
4a7cc0f21   Jon Loeliger   Cleanup and linde...
491

225b1da7b   Simon Glass   dm: ahci: Refacto...
492
493
  static int ahci_fill_sg(struct ahci_uc_priv *uc_priv, u8 port,
  			unsigned char *buf, int buf_len)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
494
  {
225b1da7b   Simon Glass   dm: ahci: Refacto...
495
  	struct ahci_ioports *pp = &(uc_priv->port[port]);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
496
497
498
499
500
  	struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
  	u32 sg_count;
  	int i;
  
  	sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
4a7cc0f21   Jon Loeliger   Cleanup and linde...
501
  	if (sg_count > AHCI_MAX_SG) {
4782ac80b   Jin Zhengxiong   Add AHCI support ...
502
503
504
505
  		printf("Error:Too much sg!
  ");
  		return -1;
  	}
4a7cc0f21   Jon Loeliger   Cleanup and linde...
506
  	for (i = 0; i < sg_count; i++) {
6e7325533   Roman Kapl   ata: ahci allow 6...
507
508
509
510
511
512
513
514
515
516
  		/* We assume virt=phys */
  		phys_addr_t pa = (unsigned long)buf + i * MAX_DATA_BYTE_COUNT;
  
  		ahci_sg->addr = cpu_to_le32(lower_32_bits(pa));
  		ahci_sg->addr_hi = cpu_to_le32(upper_32_bits(pa));
  		if (ahci_sg->addr_hi && !(uc_priv->cap & AHCI_CAP_S64A)) {
  			printf("Error: DMA address too high
  ");
  			return -1;
  		}
4a7cc0f21   Jon Loeliger   Cleanup and linde...
517
518
519
520
  		ahci_sg->flags_size = cpu_to_le32(0x3fffff &
  					  (buf_len < MAX_DATA_BYTE_COUNT
  					   ? (buf_len - 1)
  					   : (MAX_DATA_BYTE_COUNT - 1)));
4782ac80b   Jin Zhengxiong   Add AHCI support ...
521
522
523
524
525
526
527
528
529
530
531
532
  		ahci_sg++;
  		buf_len -= MAX_DATA_BYTE_COUNT;
  	}
  
  	return sg_count;
  }
  
  
  static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
  {
  	pp->cmd_slot->opts = cpu_to_le32(opts);
  	pp->cmd_slot->status = 0;
fa31377ef   Tang Yuantian   ahci: Fix compili...
533
534
535
536
537
  	pp->cmd_slot->tbl_addr = cpu_to_le32((u32)pp->cmd_tbl & 0xffffffff);
  #ifdef CONFIG_PHYS_64BIT
  	pp->cmd_slot->tbl_addr_hi =
  	    cpu_to_le32((u32)(((pp->cmd_tbl) >> 16) >> 16));
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
538
  }
fa31377ef   Tang Yuantian   ahci: Fix compili...
539
  static int wait_spinup(void __iomem *port_mmio)
4df2b48fb   Bin Meng   x86: ahci: Make s...
540
541
542
543
544
545
546
547
548
549
550
551
552
  {
  	ulong start;
  	u32 tf_data;
  
  	start = get_timer(0);
  	do {
  		tf_data = readl(port_mmio + PORT_TFDATA);
  		if (!(tf_data & ATA_BUSY))
  			return 0;
  	} while (get_timer(start) < WAIT_MS_SPINUP);
  
  	return -ETIMEDOUT;
  }
4782ac80b   Jin Zhengxiong   Add AHCI support ...
553

225b1da7b   Simon Glass   dm: ahci: Refacto...
554
  static int ahci_port_start(struct ahci_uc_priv *uc_priv, u8 port)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
555
  {
225b1da7b   Simon Glass   dm: ahci: Refacto...
556
  	struct ahci_ioports *pp = &(uc_priv->port[port]);
fa31377ef   Tang Yuantian   ahci: Fix compili...
557
  	void __iomem *port_mmio = pp->port_mmio;
5b7a2bf31   Oleksandr Rybalko   ata: ahci: Don't ...
558
  	u64 dma_addr;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
559
  	u32 port_status;
fa31377ef   Tang Yuantian   ahci: Fix compili...
560
  	void __iomem *mem;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
561

4a7cc0f21   Jon Loeliger   Cleanup and linde...
562
563
  	debug("Enter start port: %d
  ", port);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
564
  	port_status = readl(port_mmio + PORT_SCR_STAT);
4a7cc0f21   Jon Loeliger   Cleanup and linde...
565
566
567
  	debug("Port %d status: %x
  ", port, port_status);
  	if ((port_status & 0xf) != 0x03) {
4782ac80b   Jin Zhengxiong   Add AHCI support ...
568
569
570
571
  		printf("No Link on this port!
  ");
  		return -1;
  	}
28b4ba948   Christian Gmeiner   ata: ahci: fix me...
572
  	mem = memalign(2048, AHCI_PORT_PRIV_DMA_SZ);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
573
574
  	if (!mem) {
  		free(pp);
d73763a4f   Roger Quadros   ahci: Error out w...
575
576
  		printf("%s: No mem for table!
  ", __func__);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
577
578
  		return -ENOMEM;
  	}
fa31377ef   Tang Yuantian   ahci: Fix compili...
579
  	memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
580

4782ac80b   Jin Zhengxiong   Add AHCI support ...
581
582
583
584
  	/*
  	 * First item in chunk of DMA memory: 32-slot command table,
  	 * 32 bytes each in size
  	 */
64738e8ae   Taylor Hutt   ahci: Use virt_to...
585
586
  	pp->cmd_slot =
  		(struct ahci_cmd_hdr *)(uintptr_t)virt_to_phys((void *)mem);
fa31377ef   Tang Yuantian   ahci: Fix compili...
587
588
  	debug("cmd_slot = %p
  ", pp->cmd_slot);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
589
  	mem += (AHCI_CMD_SLOT_SZ + 224);
4a7cc0f21   Jon Loeliger   Cleanup and linde...
590

4782ac80b   Jin Zhengxiong   Add AHCI support ...
591
592
593
  	/*
  	 * Second item: Received-FIS area
  	 */
64738e8ae   Taylor Hutt   ahci: Use virt_to...
594
  	pp->rx_fis = virt_to_phys((void *)mem);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
595
  	mem += AHCI_RX_FIS_SZ;
4a7cc0f21   Jon Loeliger   Cleanup and linde...
596

4782ac80b   Jin Zhengxiong   Add AHCI support ...
597
598
599
600
  	/*
  	 * Third item: data area for storing a single command
  	 * and its scatter-gather table
  	 */
64738e8ae   Taylor Hutt   ahci: Use virt_to...
601
  	pp->cmd_tbl = virt_to_phys((void *)mem);
fa31377ef   Tang Yuantian   ahci: Fix compili...
602
603
  	debug("cmd_tbl_dma = %lx
  ", pp->cmd_tbl);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
604
605
  
  	mem += AHCI_CMD_TBL_HDR;
64738e8ae   Taylor Hutt   ahci: Use virt_to...
606
607
  	pp->cmd_tbl_sg =
  			(struct ahci_sg *)(uintptr_t)virt_to_phys((void *)mem);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
608

5b7a2bf31   Oleksandr Rybalko   ata: ahci: Don't ...
609
610
611
612
613
614
  	dma_addr = (ulong)pp->cmd_slot;
  	writel_with_flush(dma_addr, port_mmio + PORT_LST_ADDR);
  	writel_with_flush(dma_addr >> 32, port_mmio + PORT_LST_ADDR_HI);
  	dma_addr = (ulong)pp->rx_fis;
  	writel_with_flush(dma_addr, port_mmio + PORT_FIS_ADDR);
  	writel_with_flush(dma_addr >> 32, port_mmio + PORT_FIS_ADDR_HI);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
615

a6e50a88d   Ian Campbell   ahci: provide sun...
616
617
618
  #ifdef CONFIG_SUNXI_AHCI
  	sunxi_dma_init(port_mmio);
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
619
  	writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
4a7cc0f21   Jon Loeliger   Cleanup and linde...
620
621
  			  PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
  			  PORT_CMD_START, port_mmio + PORT_CMD);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
622

4a7cc0f21   Jon Loeliger   Cleanup and linde...
623
624
  	debug("Exit start port %d
  ", port);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
625

4df2b48fb   Bin Meng   x86: ahci: Make s...
626
627
628
629
630
  	/*
  	 * Make sure interface is not busy based on error and status
  	 * information from task file data register before proceeding
  	 */
  	return wait_spinup(port_mmio);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
631
  }
225b1da7b   Simon Glass   dm: ahci: Refacto...
632
633
  static int ahci_device_data_io(struct ahci_uc_priv *uc_priv, u8 port, u8 *fis,
  			       int fis_len, u8 *buf, int buf_len, u8 is_write)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
634
  {
225b1da7b   Simon Glass   dm: ahci: Refacto...
635
  	struct ahci_ioports *pp = &(uc_priv->port[port]);
fa31377ef   Tang Yuantian   ahci: Fix compili...
636
  	void __iomem *port_mmio = pp->port_mmio;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
637
638
639
  	u32 opts;
  	u32 port_status;
  	int sg_count;
b7a21b70d   Hung-Te Lin   ahci: support scs...
640
641
  	debug("Enter %s: for port %d
  ", __func__, port);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
642

2d5c658e8   Ye Li   MLK-22293-3 ata: ...
643
644
645
  	if (port >= uc_priv->n_ports) {
  		debug("Invalid port number %d
  ", port);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
646
647
648
649
  		return -1;
  	}
  
  	port_status = readl(port_mmio + PORT_SCR_STAT);
4a7cc0f21   Jon Loeliger   Cleanup and linde...
650
651
652
  	if ((port_status & 0xf) != 0x03) {
  		debug("No Link on port %d!
  ", port);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
653
654
655
656
  		return -1;
  	}
  
  	memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
225b1da7b   Simon Glass   dm: ahci: Refacto...
657
  	sg_count = ahci_fill_sg(uc_priv, port, buf, buf_len);
b7a21b70d   Hung-Te Lin   ahci: support scs...
658
  	opts = (fis_len >> 2) | (sg_count << 16) | (is_write << 6);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
659
  	ahci_fill_cmd_slot(pp, opts);
90b276f6a   Taylor Hutt   ahci: flush / inv...
660
  	ahci_dcache_flush_sata_cmd(pp);
fa31377ef   Tang Yuantian   ahci: Fix compili...
661
  	ahci_dcache_flush_range((unsigned long)buf, (unsigned long)buf_len);
90b276f6a   Taylor Hutt   ahci: flush / inv...
662

4782ac80b   Jin Zhengxiong   Add AHCI support ...
663
  	writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
57847660b   Walter Murphy   ahci: Adjust SATA...
664
665
  	if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  				WAIT_MS_DATAIO, 0x1)) {
4782ac80b   Jin Zhengxiong   Add AHCI support ...
666
667
668
669
  		printf("timeout exit!
  ");
  		return -1;
  	}
90b276f6a   Taylor Hutt   ahci: flush / inv...
670

fa31377ef   Tang Yuantian   ahci: Fix compili...
671
672
  	ahci_dcache_invalidate_range((unsigned long)buf,
  				     (unsigned long)buf_len);
b7a21b70d   Hung-Te Lin   ahci: support scs...
673
674
  	debug("%s: %d byte transferred.
  ", __func__, pp->cmd_slot->status);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
675
676
677
678
679
680
681
682
  
  	return 0;
  }
  
  
  static char *ata_id_strcpy(u16 *target, u16 *src, int len)
  {
  	int i;
4a7cc0f21   Jon Loeliger   Cleanup and linde...
683
  	for (i = 0; i < len / 2; i++)
e5a6c79d4   Rob Herring   scsi/ahci: ata id...
684
  		target[i] = swab16(src[i]);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
685
686
  	return (char *)target;
  }
4782ac80b   Jin Zhengxiong   Add AHCI support ...
687
688
689
  /*
   * SCSI INQUIRY command operation.
   */
4b62b2ff5   Simon Glass   dm: sata: Move at...
690
691
  static int ata_scsiop_inquiry(struct ahci_uc_priv *uc_priv,
  			      struct scsi_cmd *pccb)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
692
  {
48c3a87c0   Rob Herring   ahci: fix unalign...
693
  	static const u8 hdr[] = {
4782ac80b   Jin Zhengxiong   Add AHCI support ...
694
695
  		0,
  		0,
4a7cc0f21   Jon Loeliger   Cleanup and linde...
696
  		0x5,		/* claim SPC-3 version compatibility */
4782ac80b   Jin Zhengxiong   Add AHCI support ...
697
698
699
700
  		2,
  		95 - 4,
  	};
  	u8 fis[20];
3f6297116   Roger Quadros   ahci: Fix data ab...
701
  	u16 *idbuf;
2faf5fb82   Roger Quadros   ahci: Fix cache a...
702
  	ALLOC_CACHE_ALIGN_BUFFER(u16, tmpid, ATA_ID_WORDS);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
703
704
705
706
707
708
  	u8 port;
  
  	/* Clean ccb data buffer */
  	memset(pccb->pdata, 0, pccb->datalen);
  
  	memcpy(pccb->pdata, hdr, sizeof(hdr));
4a7cc0f21   Jon Loeliger   Cleanup and linde...
709
  	if (pccb->datalen <= 35)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
710
  		return 0;
c87311156   Taylor Hutt   ahci: Use sizeof(...
711
  	memset(fis, 0, sizeof(fis));
4782ac80b   Jin Zhengxiong   Add AHCI support ...
712
  	/* Construct the FIS */
4a7cc0f21   Jon Loeliger   Cleanup and linde...
713
714
  	fis[0] = 0x27;		/* Host to device FIS. */
  	fis[1] = 1 << 7;	/* Command FIS. */
344ca0b40   Rob Herring   ahci: convert to ...
715
  	fis[2] = ATA_CMD_ID_ATA; /* Command byte. */
4782ac80b   Jin Zhengxiong   Add AHCI support ...
716
717
718
  
  	/* Read id from sata */
  	port = pccb->target;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
719

225b1da7b   Simon Glass   dm: ahci: Refacto...
720
721
  	if (ahci_device_data_io(uc_priv, port, (u8 *)&fis, sizeof(fis),
  				(u8 *)tmpid, ATA_ID_WORDS * 2, 0)) {
4782ac80b   Jin Zhengxiong   Add AHCI support ...
722
723
724
725
  		debug("scsi_ahci: SCSI inquiry command failure.
  ");
  		return -EIO;
  	}
4b62b2ff5   Simon Glass   dm: sata: Move at...
726
727
728
  	if (!uc_priv->ataid[port]) {
  		uc_priv->ataid[port] = malloc(ATA_ID_WORDS * 2);
  		if (!uc_priv->ataid[port]) {
3f6297116   Roger Quadros   ahci: Fix data ab...
729
730
731
732
733
  			printf("%s: No memory for ataid[port]
  ", __func__);
  			return -ENOMEM;
  		}
  	}
4b62b2ff5   Simon Glass   dm: sata: Move at...
734
  	idbuf = uc_priv->ataid[port];
3f6297116   Roger Quadros   ahci: Fix data ab...
735
736
737
  
  	memcpy(idbuf, tmpid, ATA_ID_WORDS * 2);
  	ata_swap_buf_le16(idbuf, ATA_ID_WORDS);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
738
739
  
  	memcpy(&pccb->pdata[8], "ATA     ", 8);
3f6297116   Roger Quadros   ahci: Fix data ab...
740
741
  	ata_id_strcpy((u16 *)&pccb->pdata[16], &idbuf[ATA_ID_PROD], 16);
  	ata_id_strcpy((u16 *)&pccb->pdata[32], &idbuf[ATA_ID_FW_REV], 4);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
742

344ca0b40   Rob Herring   ahci: convert to ...
743
  #ifdef DEBUG
3f6297116   Roger Quadros   ahci: Fix data ab...
744
  	ata_dump_id(idbuf);
344ca0b40   Rob Herring   ahci: convert to ...
745
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
746
747
748
749
750
  	return 0;
  }
  
  
  /*
b7a21b70d   Hung-Te Lin   ahci: support scs...
751
   * SCSI READ10/WRITE10 command operation.
4782ac80b   Jin Zhengxiong   Add AHCI support ...
752
   */
225b1da7b   Simon Glass   dm: ahci: Refacto...
753
754
  static int ata_scsiop_read_write(struct ahci_uc_priv *uc_priv,
  				 struct scsi_cmd *pccb, u8 is_write)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
755
  {
2b42c9317   Mark Langsdorf   ahci: support LBA...
756
  	lbaint_t lba = 0;
284231e49   Vadim Bendebury   ahci: Support spl...
757
  	u16 blocks = 0;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
758
  	u8 fis[20];
284231e49   Vadim Bendebury   ahci: Support spl...
759
760
  	u8 *user_buffer = pccb->pdata;
  	u32 user_buffer_size = pccb->datalen;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
761

284231e49   Vadim Bendebury   ahci: Support spl...
762
  	/* Retrieve the base LBA number from the ccb structure. */
2b42c9317   Mark Langsdorf   ahci: support LBA...
763
764
765
766
767
768
769
770
  	if (pccb->cmd[0] == SCSI_READ16) {
  		memcpy(&lba, pccb->cmd + 2, 8);
  		lba = be64_to_cpu(lba);
  	} else {
  		u32 temp;
  		memcpy(&temp, pccb->cmd + 2, 4);
  		lba = be32_to_cpu(temp);
  	}
4782ac80b   Jin Zhengxiong   Add AHCI support ...
771

284231e49   Vadim Bendebury   ahci: Support spl...
772
  	/*
2b42c9317   Mark Langsdorf   ahci: support LBA...
773
774
  	 * Retrieve the base LBA number and the block count from
  	 * the ccb structure.
284231e49   Vadim Bendebury   ahci: Support spl...
775
776
  	 *
  	 * For 10-byte and 16-byte SCSI R/W commands, transfer
4782ac80b   Jin Zhengxiong   Add AHCI support ...
777
778
779
780
781
782
  	 * length 0 means transfer 0 block of data.
  	 * However, for ATA R/W commands, sector count 0 means
  	 * 256 or 65536 sectors, not 0 sectors as in SCSI.
  	 *
  	 * WARNING: one or two older ATA drives treat 0 as 0...
  	 */
2b42c9317   Mark Langsdorf   ahci: support LBA...
783
784
785
786
  	if (pccb->cmd[0] == SCSI_READ16)
  		blocks = (((u16)pccb->cmd[13]) << 8) | ((u16) pccb->cmd[14]);
  	else
  		blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
284231e49   Vadim Bendebury   ahci: Support spl...
787

2b42c9317   Mark Langsdorf   ahci: support LBA...
788
789
790
  	debug("scsi_ahci: %s %u blocks starting from lba 0x" LBAFU "
  ",
  	      is_write ?  "write" : "read", blocks, lba);
284231e49   Vadim Bendebury   ahci: Support spl...
791
792
  
  	/* Preset the FIS */
c87311156   Taylor Hutt   ahci: Use sizeof(...
793
  	memset(fis, 0, sizeof(fis));
284231e49   Vadim Bendebury   ahci: Support spl...
794
795
  	fis[0] = 0x27;		 /* Host to device FIS. */
  	fis[1] = 1 << 7;	 /* Command FIS. */
b7a21b70d   Hung-Te Lin   ahci: support scs...
796
  	/* Command byte (read/write). */
fe1f808ce   Walter Murphy   ahci: Expand HDD ...
797
  	fis[2] = is_write ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
798

284231e49   Vadim Bendebury   ahci: Support spl...
799
800
801
  	while (blocks) {
  		u16 now_blocks; /* number of blocks per iteration */
  		u32 transfer_size; /* number of bytes per iteration */
b41411954   Masahiro Yamada   linux/kernel.h: s...
802
  		now_blocks = min((u16)MAX_SATA_BLOCKS_READ_WRITE, blocks);
284231e49   Vadim Bendebury   ahci: Support spl...
803

344ca0b40   Rob Herring   ahci: convert to ...
804
  		transfer_size = ATA_SECT_SIZE * now_blocks;
284231e49   Vadim Bendebury   ahci: Support spl...
805
806
807
808
809
  		if (transfer_size > user_buffer_size) {
  			printf("scsi_ahci: Error: buffer too small.
  ");
  			return -EIO;
  		}
2b42c9317   Mark Langsdorf   ahci: support LBA...
810
811
812
813
  		/*
  		 * LBA48 SATA command but only use 32bit address range within
  		 * that (unless we've enabled 64bit LBA support). The next
  		 * smaller command range (28bit) is too small.
fe1f808ce   Walter Murphy   ahci: Expand HDD ...
814
  		 */
284231e49   Vadim Bendebury   ahci: Support spl...
815
816
817
  		fis[4] = (lba >> 0) & 0xff;
  		fis[5] = (lba >> 8) & 0xff;
  		fis[6] = (lba >> 16) & 0xff;
fe1f808ce   Walter Murphy   ahci: Expand HDD ...
818
819
  		fis[7] = 1 << 6; /* device reg: set LBA mode */
  		fis[8] = ((lba >> 24) & 0xff);
2b42c9317   Mark Langsdorf   ahci: support LBA...
820
821
822
823
824
825
  #ifdef CONFIG_SYS_64BIT_LBA
  		if (pccb->cmd[0] == SCSI_READ16) {
  			fis[9] = ((lba >> 32) & 0xff);
  			fis[10] = ((lba >> 40) & 0xff);
  		}
  #endif
fe1f808ce   Walter Murphy   ahci: Expand HDD ...
826
  		fis[3] = 0xe0; /* features */
284231e49   Vadim Bendebury   ahci: Support spl...
827
828
829
830
  
  		/* Block (sector) count */
  		fis[12] = (now_blocks >> 0) & 0xff;
  		fis[13] = (now_blocks >> 8) & 0xff;
b7a21b70d   Hung-Te Lin   ahci: support scs...
831
  		/* Read/Write from ahci */
225b1da7b   Simon Glass   dm: ahci: Refacto...
832
833
  		if (ahci_device_data_io(uc_priv, pccb->target, (u8 *)&fis,
  					sizeof(fis), user_buffer, transfer_size,
b7a21b70d   Hung-Te Lin   ahci: support scs...
834
835
836
837
  					is_write)) {
  			debug("scsi_ahci: SCSI %s10 command failure.
  ",
  			      is_write ? "WRITE" : "READ");
284231e49   Vadim Bendebury   ahci: Support spl...
838
839
  			return -EIO;
  		}
766b16fe1   Marc Jones   ahci: Perform SAT...
840
841
842
843
844
845
846
847
  
  		/* If this transaction is a write, do a following flush.
  		 * Writes in u-boot are so rare, and the logic to know when is
  		 * the last write and do a flush only there is sufficiently
  		 * difficult. Just do a flush after every write. This incurs,
  		 * usually, one extra flush when the rare writes do happen.
  		 */
  		if (is_write) {
225b1da7b   Simon Glass   dm: ahci: Refacto...
848
  			if (-EIO == ata_io_flush(uc_priv, pccb->target))
766b16fe1   Marc Jones   ahci: Perform SAT...
849
850
  				return -EIO;
  		}
284231e49   Vadim Bendebury   ahci: Support spl...
851
852
853
854
  		user_buffer += transfer_size;
  		user_buffer_size -= transfer_size;
  		blocks -= now_blocks;
  		lba += now_blocks;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
855
856
857
858
859
860
861
862
863
  	}
  
  	return 0;
  }
  
  
  /*
   * SCSI READ CAPACITY10 command operation.
   */
4b62b2ff5   Simon Glass   dm: sata: Move at...
864
865
  static int ata_scsiop_read_capacity10(struct ahci_uc_priv *uc_priv,
  				      struct scsi_cmd *pccb)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
866
  {
cb6d0b72c   Kumar Gala   ahci: Fix gcc 4.4...
867
  	u32 cap;
344ca0b40   Rob Herring   ahci: convert to ...
868
  	u64 cap64;
19d1d41e8   Gabe Black   ahci: Make the AH...
869
  	u32 block_size;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
870

4b62b2ff5   Simon Glass   dm: sata: Move at...
871
  	if (!uc_priv->ataid[pccb->target]) {
4782ac80b   Jin Zhengxiong   Add AHCI support ...
872
  		printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
4a7cc0f21   Jon Loeliger   Cleanup and linde...
873
874
  		       "\tNo ATA info!
  "
1b25e586c   Vagrant Cascadian   Fix typo: firstly...
875
876
  		       "\tPlease run SCSI command INQUIRY first!
  ");
4782ac80b   Jin Zhengxiong   Add AHCI support ...
877
878
  		return -EPERM;
  	}
4b62b2ff5   Simon Glass   dm: sata: Move at...
879
  	cap64 = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
344ca0b40   Rob Herring   ahci: convert to ...
880
881
  	if (cap64 > 0x100000000ULL)
  		cap64 = 0xffffffff;
19d1d41e8   Gabe Black   ahci: Make the AH...
882

344ca0b40   Rob Herring   ahci: convert to ...
883
  	cap = cpu_to_be32(cap64);
cb6d0b72c   Kumar Gala   ahci: Fix gcc 4.4...
884
  	memcpy(pccb->pdata, &cap, sizeof(cap));
4782ac80b   Jin Zhengxiong   Add AHCI support ...
885

19d1d41e8   Gabe Black   ahci: Make the AH...
886
887
888
889
890
891
892
893
894
895
  	block_size = cpu_to_be32((u32)512);
  	memcpy(&pccb->pdata[4], &block_size, 4);
  
  	return 0;
  }
  
  
  /*
   * SCSI READ CAPACITY16 command operation.
   */
4b62b2ff5   Simon Glass   dm: sata: Move at...
896
897
  static int ata_scsiop_read_capacity16(struct ahci_uc_priv *uc_priv,
  				      struct scsi_cmd *pccb)
19d1d41e8   Gabe Black   ahci: Make the AH...
898
899
900
  {
  	u64 cap;
  	u64 block_size;
4b62b2ff5   Simon Glass   dm: sata: Move at...
901
  	if (!uc_priv->ataid[pccb->target]) {
19d1d41e8   Gabe Black   ahci: Make the AH...
902
903
904
  		printf("scsi_ahci: SCSI READ CAPACITY16 command failure. "
  		       "\tNo ATA info!
  "
1b25e586c   Vagrant Cascadian   Fix typo: firstly...
905
906
  		       "\tPlease run SCSI command INQUIRY first!
  ");
19d1d41e8   Gabe Black   ahci: Make the AH...
907
908
  		return -EPERM;
  	}
4b62b2ff5   Simon Glass   dm: sata: Move at...
909
  	cap = ata_id_n_sectors(uc_priv->ataid[pccb->target]);
19d1d41e8   Gabe Black   ahci: Make the AH...
910
911
912
913
914
  	cap = cpu_to_be64(cap);
  	memcpy(pccb->pdata, &cap, sizeof(cap));
  
  	block_size = cpu_to_be64((u64)512);
  	memcpy(&pccb->pdata[8], &block_size, 8);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
915
916
917
918
919
920
921
922
  
  	return 0;
  }
  
  
  /*
   * SCSI TEST UNIT READY command operation.
   */
4b62b2ff5   Simon Glass   dm: sata: Move at...
923
924
  static int ata_scsiop_test_unit_ready(struct ahci_uc_priv *uc_priv,
  				      struct scsi_cmd *pccb)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
925
  {
4b62b2ff5   Simon Glass   dm: sata: Move at...
926
  	return (uc_priv->ataid[pccb->target]) ? 0 : -EPERM;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
927
  }
4a7cc0f21   Jon Loeliger   Cleanup and linde...
928

4e7490145   Simon Glass   dm: ahci: Create ...
929
  static int ahci_scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
930
  {
4682c8a19   Simon Glass   dm: scsi: Add a d...
931
932
  	struct ahci_uc_priv *uc_priv;
  #ifdef CONFIG_DM_SCSI
bfc1c6b48   Simon Glass   dm: ahci: Correct...
933
  	uc_priv = dev_get_uclass_priv(dev->parent);
4682c8a19   Simon Glass   dm: scsi: Add a d...
934
935
936
  #else
  	uc_priv = probe_ent;
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
937
  	int ret;
4a7cc0f21   Jon Loeliger   Cleanup and linde...
938
  	switch (pccb->cmd[0]) {
2b42c9317   Mark Langsdorf   ahci: support LBA...
939
  	case SCSI_READ16:
4782ac80b   Jin Zhengxiong   Add AHCI support ...
940
  	case SCSI_READ10:
225b1da7b   Simon Glass   dm: ahci: Refacto...
941
  		ret = ata_scsiop_read_write(uc_priv, pccb, 0);
b7a21b70d   Hung-Te Lin   ahci: support scs...
942
943
  		break;
  	case SCSI_WRITE10:
225b1da7b   Simon Glass   dm: ahci: Refacto...
944
  		ret = ata_scsiop_read_write(uc_priv, pccb, 1);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
945
  		break;
19d1d41e8   Gabe Black   ahci: Make the AH...
946
  	case SCSI_RD_CAPAC10:
4b62b2ff5   Simon Glass   dm: sata: Move at...
947
  		ret = ata_scsiop_read_capacity10(uc_priv, pccb);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
948
  		break;
19d1d41e8   Gabe Black   ahci: Make the AH...
949
  	case SCSI_RD_CAPAC16:
4b62b2ff5   Simon Glass   dm: sata: Move at...
950
  		ret = ata_scsiop_read_capacity16(uc_priv, pccb);
19d1d41e8   Gabe Black   ahci: Make the AH...
951
  		break;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
952
  	case SCSI_TST_U_RDY:
4b62b2ff5   Simon Glass   dm: sata: Move at...
953
  		ret = ata_scsiop_test_unit_ready(uc_priv, pccb);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
954
955
  		break;
  	case SCSI_INQUIRY:
4b62b2ff5   Simon Glass   dm: sata: Move at...
956
  		ret = ata_scsiop_inquiry(uc_priv, pccb);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
957
958
959
960
  		break;
  	default:
  		printf("Unsupport SCSI command 0x%02x
  ", pccb->cmd[0]);
f6580ef39   Simon Glass   dm: scsi: Adjust ...
961
  		return -ENOTSUPP;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
962
  	}
4a7cc0f21   Jon Loeliger   Cleanup and linde...
963
964
965
  	if (ret) {
  		debug("SCSI command 0x%02x ret errno %d
  ", pccb->cmd[0], ret);
f6580ef39   Simon Glass   dm: scsi: Adjust ...
966
  		return ret;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
967
  	}
f6580ef39   Simon Glass   dm: scsi: Adjust ...
968
  	return 0;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
969
970
  
  }
62b4ec8e3   Simon Glass   dm: ahci: Move co...
971
972
973
974
975
976
  static int ahci_start_ports(struct ahci_uc_priv *uc_priv)
  {
  	u32 linkmap;
  	int i;
  
  	linkmap = uc_priv->link_port_map;
8bf207d24   Tuomas Tynkkynen   ata: ahci: Loop o...
977
  	for (i = 0; i < uc_priv->n_ports; i++) {
62b4ec8e3   Simon Glass   dm: ahci: Move co...
978
979
980
981
982
983
984
985
986
987
988
  		if (((linkmap >> i) & 0x01)) {
  			if (ahci_port_start(uc_priv, (u8) i)) {
  				printf("Can not start port %d
  ", i);
  				continue;
  			}
  		}
  	}
  
  	return 0;
  }
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
989
  #ifndef CONFIG_DM_SCSI
4782ac80b   Jin Zhengxiong   Add AHCI support ...
990
991
  void scsi_low_level_init(int busdevfunc)
  {
225b1da7b   Simon Glass   dm: ahci: Refacto...
992
  	struct ahci_uc_priv *uc_priv;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
993

942e31437   Rob Herring   scsi/ahci: add su...
994
  #ifndef CONFIG_SCSI_AHCI_PLAT
4279efc4c   Simon Glass   dm: ahci: Drop us...
995
996
997
998
999
1000
1001
  	probe_ent = calloc(1, sizeof(struct ahci_uc_priv));
  	if (!probe_ent) {
  		printf("%s: No memory for uc_priv
  ", __func__);
  		return;
  	}
  	uc_priv = probe_ent;
e8a016b53   Michal Simek   dm: Add support f...
1002
  # if defined(CONFIG_DM_PCI)
ff758ccc8   Simon Glass   dm: ahci: Convert...
1003
1004
1005
1006
1007
1008
  	struct udevice *dev;
  	int ret;
  
  	ret = dm_pci_bus_find_bdf(busdevfunc, &dev);
  	if (ret)
  		return;
4279efc4c   Simon Glass   dm: ahci: Drop us...
1009
  	ahci_init_one(uc_priv, dev);
ff758ccc8   Simon Glass   dm: ahci: Convert...
1010
  # else
4279efc4c   Simon Glass   dm: ahci: Drop us...
1011
  	ahci_init_one(uc_priv, busdevfunc);
ff758ccc8   Simon Glass   dm: ahci: Convert...
1012
  # endif
4279efc4c   Simon Glass   dm: ahci: Drop us...
1013
  #else
225b1da7b   Simon Glass   dm: ahci: Refacto...
1014
  	uc_priv = probe_ent;
4279efc4c   Simon Glass   dm: ahci: Drop us...
1015
  #endif
4782ac80b   Jin Zhengxiong   Add AHCI support ...
1016

62b4ec8e3   Simon Glass   dm: ahci: Move co...
1017
  	ahci_start_ports(uc_priv);
4782ac80b   Jin Zhengxiong   Add AHCI support ...
1018
  }
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
1019
1020
1021
1022
  #endif
  
  #ifndef CONFIG_SCSI_AHCI_PLAT
  # if defined(CONFIG_DM_PCI) || defined(CONFIG_DM_SCSI)
e81589ea4   Michal Simek   ata: Fix ahci wor...
1023
  int ahci_init_one_dm(struct udevice *dev)
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
1024
  {
4279efc4c   Simon Glass   dm: ahci: Drop us...
1025
1026
1027
  	struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  
  	return ahci_init_one(uc_priv, dev);
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
1028
1029
1030
  }
  #endif
  #endif
e81589ea4   Michal Simek   ata: Fix ahci wor...
1031
  int ahci_start_ports_dm(struct udevice *dev)
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
1032
  {
4279efc4c   Simon Glass   dm: ahci: Drop us...
1033
  	struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
7cf1afce7   Simon Glass   dm: ahci: Unwind ...
1034
1035
1036
  
  	return ahci_start_ports(uc_priv);
  }
4782ac80b   Jin Zhengxiong   Add AHCI support ...
1037

942e31437   Rob Herring   scsi/ahci: add su...
1038
  #ifdef CONFIG_SCSI_AHCI_PLAT
4279efc4c   Simon Glass   dm: ahci: Drop us...
1039
  static int ahci_init_common(struct ahci_uc_priv *uc_priv, void __iomem *base)
942e31437   Rob Herring   scsi/ahci: add su...
1040
  {
4279efc4c   Simon Glass   dm: ahci: Drop us...
1041
  	int rc;
942e31437   Rob Herring   scsi/ahci: add su...
1042

225b1da7b   Simon Glass   dm: ahci: Refacto...
1043
  	uc_priv->host_flags = ATA_FLAG_SATA
942e31437   Rob Herring   scsi/ahci: add su...
1044
1045
1046
1047
  				| ATA_FLAG_NO_LEGACY
  				| ATA_FLAG_MMIO
  				| ATA_FLAG_PIO_DMA
  				| ATA_FLAG_NO_ATAPI;
225b1da7b   Simon Glass   dm: ahci: Refacto...
1048
1049
  	uc_priv->pio_mask = 0x1f;
  	uc_priv->udma_mask = 0x7f;	/*Fixme,assume to support UDMA6 */
942e31437   Rob Herring   scsi/ahci: add su...
1050

225b1da7b   Simon Glass   dm: ahci: Refacto...
1051
  	uc_priv->mmio_base = base;
942e31437   Rob Herring   scsi/ahci: add su...
1052
1053
  
  	/* initialize adapter */
225b1da7b   Simon Glass   dm: ahci: Refacto...
1054
  	rc = ahci_host_init(uc_priv);
942e31437   Rob Herring   scsi/ahci: add su...
1055
1056
  	if (rc)
  		goto err_out;
225b1da7b   Simon Glass   dm: ahci: Refacto...
1057
  	ahci_print_info(uc_priv);
942e31437   Rob Herring   scsi/ahci: add su...
1058

62b4ec8e3   Simon Glass   dm: ahci: Move co...
1059
  	rc = ahci_start_ports(uc_priv);
942e31437   Rob Herring   scsi/ahci: add su...
1060

942e31437   Rob Herring   scsi/ahci: add su...
1061
1062
1063
  err_out:
  	return rc;
  }
c6f3d50b9   Ian Campbell   ahci-plat: Provid...
1064

4279efc4c   Simon Glass   dm: ahci: Drop us...
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
  #ifndef CONFIG_DM_SCSI
  int ahci_init(void __iomem *base)
  {
  	struct ahci_uc_priv *uc_priv;
  
  	probe_ent = malloc(sizeof(struct ahci_uc_priv));
  	if (!probe_ent) {
  		printf("%s: No memory for uc_priv
  ", __func__);
  		return -ENOMEM;
  	}
  
  	uc_priv = probe_ent;
  	memset(uc_priv, 0, sizeof(struct ahci_uc_priv));
  
  	return ahci_init_common(uc_priv, base);
  }
  #endif
  
  int ahci_init_dm(struct udevice *dev, void __iomem *base)
  {
  	struct ahci_uc_priv *uc_priv = dev_get_uclass_priv(dev);
  
  	return ahci_init_common(uc_priv, base);
  }
c6f3d50b9   Ian Campbell   ahci-plat: Provid...
1090
1091
1092
  void __weak scsi_init(void)
  {
  }
4279efc4c   Simon Glass   dm: ahci: Drop us...
1093
  #endif /* CONFIG_SCSI_AHCI_PLAT */
4782ac80b   Jin Zhengxiong   Add AHCI support ...
1094

766b16fe1   Marc Jones   ahci: Perform SAT...
1095
1096
1097
1098
1099
1100
1101
1102
1103
  /*
   * In the general case of generic rotating media it makes sense to have a
   * flush capability. It probably even makes sense in the case of SSDs because
   * one cannot always know for sure what kind of internal cache/flush mechanism
   * is embodied therein. At first it was planned to invoke this after the last
   * write to disk and before rebooting. In practice, knowing, a priori, which
   * is the last write is difficult. Because writing to the disk in u-boot is
   * very rare, this flush command will be invoked after every block write.
   */
225b1da7b   Simon Glass   dm: ahci: Refacto...
1104
  static int ata_io_flush(struct ahci_uc_priv *uc_priv, u8 port)
766b16fe1   Marc Jones   ahci: Perform SAT...
1105
1106
  {
  	u8 fis[20];
225b1da7b   Simon Glass   dm: ahci: Refacto...
1107
  	struct ahci_ioports *pp = &(uc_priv->port[port]);
fa31377ef   Tang Yuantian   ahci: Fix compili...
1108
  	void __iomem *port_mmio = pp->port_mmio;
766b16fe1   Marc Jones   ahci: Perform SAT...
1109
1110
1111
1112
1113
1114
  	u32 cmd_fis_len = 5;	/* five dwords */
  
  	/* Preset the FIS */
  	memset(fis, 0, 20);
  	fis[0] = 0x27;		 /* Host to device FIS. */
  	fis[1] = 1 << 7;	 /* Command FIS. */
fe1f808ce   Walter Murphy   ahci: Expand HDD ...
1115
  	fis[2] = ATA_CMD_FLUSH_EXT;
766b16fe1   Marc Jones   ahci: Perform SAT...
1116
1117
1118
  
  	memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
  	ahci_fill_cmd_slot(pp, cmd_fis_len);
75e14b1ac   Tang Yuantian   ahci: flush dcach...
1119
  	ahci_dcache_flush_sata_cmd(pp);
766b16fe1   Marc Jones   ahci: Perform SAT...
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
  	writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
  
  	if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE,
  			WAIT_MS_FLUSH, 0x1)) {
  		debug("scsi_ahci: flush command timeout on port %d.
  ", port);
  		return -EIO;
  	}
  
  	return 0;
  }
4e7490145   Simon Glass   dm: ahci: Create ...
1131
1132
1133
1134
1135
1136
  static int ahci_scsi_bus_reset(struct udevice *dev)
  {
  	/* Not implemented */
  
  	return 0;
  }
f6ab5a92a   Simon Glass   dm: scsi: Add ope...
1137
  #ifdef CONFIG_DM_SCSI
681357ffd   Simon Glass   dm: ahci: Add a d...
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
  int ahci_bind_scsi(struct udevice *ahci_dev, struct udevice **devp)
  {
  	struct udevice *dev;
  	int ret;
  
  	ret = device_bind_driver(ahci_dev, "ahci_scsi", "ahci_scsi", &dev);
  	if (ret)
  		return ret;
  	*devp = dev;
  
  	return 0;
  }
745a94f35   Simon Glass   ahci: Support non...
1150
  int ahci_probe_scsi(struct udevice *ahci_dev, ulong base)
681357ffd   Simon Glass   dm: ahci: Add a d...
1151
  {
681357ffd   Simon Glass   dm: ahci: Add a d...
1152
1153
1154
1155
1156
1157
1158
1159
1160
  	struct ahci_uc_priv *uc_priv;
  	struct scsi_platdata *uc_plat;
  	struct udevice *dev;
  	int ret;
  
  	device_find_first_child(ahci_dev, &dev);
  	if (!dev)
  		return -ENODEV;
  	uc_plat = dev_get_uclass_platdata(dev);
745a94f35   Simon Glass   ahci: Support non...
1161
  	uc_plat->base = base;
681357ffd   Simon Glass   dm: ahci: Add a d...
1162
1163
  	uc_plat->max_lun = 1;
  	uc_plat->max_id = 2;
745a94f35   Simon Glass   ahci: Support non...
1164
1165
  
  	uc_priv = dev_get_uclass_priv(ahci_dev);
681357ffd   Simon Glass   dm: ahci: Add a d...
1166
1167
1168
1169
1170
1171
  	ret = ahci_init_one(uc_priv, dev);
  	if (ret)
  		return ret;
  	ret = ahci_start_ports(uc_priv);
  	if (ret)
  		return ret;
681357ffd   Simon Glass   dm: ahci: Add a d...
1172

bd98e6ae7   Park, Aiden   dm: scsi: Scan th...
1173
1174
1175
1176
1177
1178
1179
  	/*
  	 * scsi_scan_dev() scans devices up-to the number of max_id.
  	 * Update max_id if the number of detected ports exceeds max_id.
  	 * This allows SCSI to scan all detected ports.
  	 */
  	uc_plat->max_id = max_t(unsigned long, uc_priv->n_ports,
  				uc_plat->max_id);
681357ffd   Simon Glass   dm: ahci: Add a d...
1180
1181
  	return 0;
  }
745a94f35   Simon Glass   ahci: Support non...
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
  #ifdef CONFIG_DM_PCI
  int ahci_probe_scsi_pci(struct udevice *ahci_dev)
  {
  	ulong base;
  
  	base = (ulong)dm_pci_map_bar(ahci_dev, PCI_BASE_ADDRESS_5,
  				     PCI_REGION_MEM);
  
  	return ahci_probe_scsi(ahci_dev, base);
  }
  #endif
f6ab5a92a   Simon Glass   dm: scsi: Add ope...
1193
1194
1195
1196
  struct scsi_ops scsi_ops = {
  	.exec		= ahci_scsi_exec,
  	.bus_reset	= ahci_scsi_bus_reset,
  };
681357ffd   Simon Glass   dm: ahci: Add a d...
1197
1198
1199
1200
1201
1202
  
  U_BOOT_DRIVER(ahci_scsi) = {
  	.name		= "ahci_scsi",
  	.id		= UCLASS_SCSI,
  	.ops		= &scsi_ops,
  };
f6ab5a92a   Simon Glass   dm: scsi: Add ope...
1203
  #else
4e7490145   Simon Glass   dm: ahci: Create ...
1204
1205
1206
1207
  int scsi_exec(struct udevice *dev, struct scsi_cmd *pccb)
  {
  	return ahci_scsi_exec(dev, pccb);
  }
766b16fe1   Marc Jones   ahci: Perform SAT...
1208

4682c8a19   Simon Glass   dm: scsi: Add a d...
1209
  __weak int scsi_bus_reset(struct udevice *dev)
4782ac80b   Jin Zhengxiong   Add AHCI support ...
1210
  {
4e7490145   Simon Glass   dm: ahci: Create ...
1211
  	return ahci_scsi_bus_reset(dev);
4682c8a19   Simon Glass   dm: scsi: Add a d...
1212
1213
  
  	return 0;
4782ac80b   Jin Zhengxiong   Add AHCI support ...
1214
  }
f6ab5a92a   Simon Glass   dm: scsi: Add ope...
1215
  #endif