blob: d94da1f05a2fc4027e0a6c4ebb6a9480fd8bb7f3 [file] [log] [blame]
Jin Zhengxiongae180dc2006-08-23 19:10:44 +08001/*
Kumar Gala6a6d9482009-07-28 21:49:52 -05002 * Copyright (C) Freescale Semiconductor, Inc. 2006.
Jin Zhengxiongae180dc2006-08-23 19:10:44 +08003 * Author: Jason Jin<Jason.jin@freescale.com>
4 * Zhang Wei<wei.zhang@freescale.com>
5 *
6 * See file CREDITS for list of people who contributed to this
7 * project.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
23 *
24 * with the reference on libata and ahci drvier in kernel
25 *
26 */
27#include <common.h>
28
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080029#include <command.h>
30#include <pci.h>
31#include <asm/processor.h>
32#include <asm/errno.h>
33#include <asm/io.h>
34#include <malloc.h>
35#include <scsi.h>
36#include <ata.h>
37#include <linux/ctype.h>
38#include <ahci.h>
39
40struct ahci_probe_ent *probe_ent = NULL;
41hd_driveid_t *ataid[AHCI_MAX_PORTS];
42
Jon Loeligerc0b0cda2006-08-23 11:04:43 -050043#define writel_with_flush(a,b) do { writel(a,b); readl(b); } while (0)
44
Vadim Bendebury700f85c2012-10-29 05:23:44 +000045/*
46 * Some controllers limit number of blocks they can read at once. Contemporary
47 * SSD devices work much faster if the read size is aligned to a power of 2.
48 * Let's set default to 128 and allowing to be overwritten if needed.
49 */
50#ifndef MAX_SATA_BLOCKS_READ
51#define MAX_SATA_BLOCKS_READ 0x80
52#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080053
54static inline u32 ahci_port_base(u32 base, u32 port)
55{
56 return base + 0x100 + (port * 0x80);
57}
58
59
60static void ahci_setup_port(struct ahci_ioports *port, unsigned long base,
61 unsigned int port_idx)
62{
63 base = ahci_port_base(base, port_idx);
64
Jon Loeligerc0b0cda2006-08-23 11:04:43 -050065 port->cmd_addr = base;
66 port->scr_addr = base + PORT_SCR;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080067}
68
69
70#define msleep(a) udelay(a * 1000)
71#define ssleep(a) msleep(a * 1000)
Jon Loeligerc0b0cda2006-08-23 11:04:43 -050072
73static int waiting_for_cmd_completed(volatile u8 *offset,
74 int timeout_msec,
75 u32 sign)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080076{
77 int i;
78 u32 status;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -050079
80 for (i = 0; ((status = readl(offset)) & sign) && i < timeout_msec; i++)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080081 msleep(1);
82
Jon Loeligerc0b0cda2006-08-23 11:04:43 -050083 return (i < timeout_msec) ? 0 : -1;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080084}
85
86
87static int ahci_host_init(struct ahci_probe_ent *probe_ent)
88{
Rob Herringc2829ff2011-07-06 16:13:36 +000089#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080090 pci_dev_t pdev = probe_ent->dev;
Rob Herringc2829ff2011-07-06 16:13:36 +000091 u16 tmp16;
92 unsigned short vendor;
93#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080094 volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
95 u32 tmp, cap_save;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080096 int i, j;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -050097 volatile u8 *port_mmio;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080098
Vadim Bendebury700f85c2012-10-29 05:23:44 +000099 debug("ahci_host_init: start\n");
100
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800101 cap_save = readl(mmio + HOST_CAP);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500102 cap_save &= ((1 << 28) | (1 << 17));
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800103 cap_save |= (1 << 27);
104
105 /* global controller reset */
106 tmp = readl(mmio + HOST_CTL);
107 if ((tmp & HOST_RESET) == 0)
108 writel_with_flush(tmp | HOST_RESET, mmio + HOST_CTL);
109
110 /* reset must complete within 1 second, or
111 * the hardware should be considered fried.
112 */
113 ssleep(1);
114
115 tmp = readl(mmio + HOST_CTL);
116 if (tmp & HOST_RESET) {
117 debug("controller reset failed (0x%x)\n", tmp);
118 return -1;
119 }
120
121 writel_with_flush(HOST_AHCI_EN, mmio + HOST_CTL);
122 writel(cap_save, mmio + HOST_CAP);
123 writel_with_flush(0xf, mmio + HOST_PORTS_IMPL);
124
Rob Herringc2829ff2011-07-06 16:13:36 +0000125#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800126 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
127
128 if (vendor == PCI_VENDOR_ID_INTEL) {
129 u16 tmp16;
130 pci_read_config_word(pdev, 0x92, &tmp16);
131 tmp16 |= 0xf;
132 pci_write_config_word(pdev, 0x92, tmp16);
133 }
Rob Herringc2829ff2011-07-06 16:13:36 +0000134#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800135 probe_ent->cap = readl(mmio + HOST_CAP);
136 probe_ent->port_map = readl(mmio + HOST_PORTS_IMPL);
137 probe_ent->n_ports = (probe_ent->cap & 0x1f) + 1;
138
139 debug("cap 0x%x port_map 0x%x n_ports %d\n",
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500140 probe_ent->cap, probe_ent->port_map, probe_ent->n_ports);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800141
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000142 if (probe_ent->n_ports > CONFIG_SYS_SCSI_MAX_SCSI_ID)
143 probe_ent->n_ports = CONFIG_SYS_SCSI_MAX_SCSI_ID;
144
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800145 for (i = 0; i < probe_ent->n_ports; i++) {
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500146 probe_ent->port[i].port_mmio = ahci_port_base((u32) mmio, i);
147 port_mmio = (u8 *) probe_ent->port[i].port_mmio;
148 ahci_setup_port(&probe_ent->port[i], (unsigned long)mmio, i);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800149
150 /* make sure port is not active */
151 tmp = readl(port_mmio + PORT_CMD);
152 if (tmp & (PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
153 PORT_CMD_FIS_RX | PORT_CMD_START)) {
154 tmp &= ~(PORT_CMD_LIST_ON | PORT_CMD_FIS_ON |
155 PORT_CMD_FIS_RX | PORT_CMD_START);
156 writel_with_flush(tmp, port_mmio + PORT_CMD);
157
158 /* spec says 500 msecs for each bit, so
159 * this is slightly incorrect.
160 */
161 msleep(500);
162 }
163
164 writel(PORT_CMD_SPIN_UP, port_mmio + PORT_CMD);
165
166 j = 0;
167 while (j < 100) {
168 msleep(10);
169 tmp = readl(port_mmio + PORT_SCR_STAT);
170 if ((tmp & 0xf) == 0x3)
171 break;
172 j++;
173 }
174
175 tmp = readl(port_mmio + PORT_SCR_ERR);
176 debug("PORT_SCR_ERR 0x%x\n", tmp);
177 writel(tmp, port_mmio + PORT_SCR_ERR);
178
179 /* ack any pending irq events for this port */
180 tmp = readl(port_mmio + PORT_IRQ_STAT);
181 debug("PORT_IRQ_STAT 0x%x\n", tmp);
182 if (tmp)
183 writel(tmp, port_mmio + PORT_IRQ_STAT);
184
185 writel(1 << i, mmio + HOST_IRQ_STAT);
186
187 /* set irq mask (enables interrupts) */
188 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
189
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500190 /*register linkup ports */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800191 tmp = readl(port_mmio + PORT_SCR_STAT);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500192 debug("Port %d status: 0x%x\n", i, tmp);
193 if ((tmp & 0xf) == 0x03)
194 probe_ent->link_port_map |= (0x01 << i);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800195 }
196
197 tmp = readl(mmio + HOST_CTL);
198 debug("HOST_CTL 0x%x\n", tmp);
199 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
200 tmp = readl(mmio + HOST_CTL);
201 debug("HOST_CTL 0x%x\n", tmp);
Rob Herringc2829ff2011-07-06 16:13:36 +0000202#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800203 pci_read_config_word(pdev, PCI_COMMAND, &tmp16);
204 tmp |= PCI_COMMAND_MASTER;
205 pci_write_config_word(pdev, PCI_COMMAND, tmp16);
Rob Herringc2829ff2011-07-06 16:13:36 +0000206#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800207 return 0;
208}
209
210
211static void ahci_print_info(struct ahci_probe_ent *probe_ent)
212{
Rob Herringc2829ff2011-07-06 16:13:36 +0000213#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800214 pci_dev_t pdev = probe_ent->dev;
Rob Herringc2829ff2011-07-06 16:13:36 +0000215 u16 cc;
216#endif
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500217 volatile u8 *mmio = (volatile u8 *)probe_ent->mmio_base;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800218 u32 vers, cap, impl, speed;
219 const char *speed_s;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800220 const char *scc_s;
221
222 vers = readl(mmio + HOST_VERSION);
223 cap = probe_ent->cap;
224 impl = probe_ent->port_map;
225
226 speed = (cap >> 20) & 0xf;
227 if (speed == 1)
228 speed_s = "1.5";
229 else if (speed == 2)
230 speed_s = "3";
231 else
232 speed_s = "?";
233
Rob Herringc2829ff2011-07-06 16:13:36 +0000234#ifdef CONFIG_SCSI_AHCI_PLAT
235 scc_s = "SATA";
236#else
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800237 pci_read_config_word(pdev, 0x0a, &cc);
238 if (cc == 0x0101)
239 scc_s = "IDE";
240 else if (cc == 0x0106)
241 scc_s = "SATA";
242 else if (cc == 0x0104)
243 scc_s = "RAID";
244 else
245 scc_s = "unknown";
Rob Herringc2829ff2011-07-06 16:13:36 +0000246#endif
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500247 printf("AHCI %02x%02x.%02x%02x "
248 "%u slots %u ports %s Gbps 0x%x impl %s mode\n",
249 (vers >> 24) & 0xff,
250 (vers >> 16) & 0xff,
251 (vers >> 8) & 0xff,
252 vers & 0xff,
253 ((cap >> 8) & 0x1f) + 1, (cap & 0x1f) + 1, speed_s, impl, scc_s);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800254
255 printf("flags: "
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500256 "%s%s%s%s%s%s"
257 "%s%s%s%s%s%s%s\n",
258 cap & (1 << 31) ? "64bit " : "",
259 cap & (1 << 30) ? "ncq " : "",
260 cap & (1 << 28) ? "ilck " : "",
261 cap & (1 << 27) ? "stag " : "",
262 cap & (1 << 26) ? "pm " : "",
263 cap & (1 << 25) ? "led " : "",
264 cap & (1 << 24) ? "clo " : "",
265 cap & (1 << 19) ? "nz " : "",
266 cap & (1 << 18) ? "only " : "",
267 cap & (1 << 17) ? "pmp " : "",
268 cap & (1 << 15) ? "pio " : "",
269 cap & (1 << 14) ? "slum " : "",
270 cap & (1 << 13) ? "part " : "");
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800271}
272
Rob Herringc2829ff2011-07-06 16:13:36 +0000273#ifndef CONFIG_SCSI_AHCI_PLAT
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500274static int ahci_init_one(pci_dev_t pdev)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800275{
Ed Swarthout91080f72007-08-02 14:09:49 -0500276 u16 vendor;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800277 int rc;
278
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500279 memset((void *)ataid, 0, sizeof(hd_driveid_t *) * AHCI_MAX_PORTS);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800280
Ed Swarthoutafd25192007-08-14 14:06:45 -0500281 probe_ent = malloc(sizeof(struct ahci_probe_ent));
282 memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800283 probe_ent->dev = pdev;
284
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500285 probe_ent->host_flags = ATA_FLAG_SATA
286 | ATA_FLAG_NO_LEGACY
287 | ATA_FLAG_MMIO
288 | ATA_FLAG_PIO_DMA
289 | ATA_FLAG_NO_ATAPI;
290 probe_ent->pio_mask = 0x1f;
291 probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800292
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000293 pci_read_config_dword(pdev, PCI_BASE_ADDRESS_5, &probe_ent->mmio_base);
294 debug("ahci mmio_base=0x%08x\n", probe_ent->mmio_base);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800295
296 /* Take from kernel:
297 * JMicron-specific fixup:
298 * make sure we're in AHCI mode
299 */
300 pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500301 if (vendor == 0x197b)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800302 pci_write_config_byte(pdev, 0x41, 0xa1);
303
304 /* initialize adapter */
305 rc = ahci_host_init(probe_ent);
306 if (rc)
307 goto err_out;
308
309 ahci_print_info(probe_ent);
310
311 return 0;
312
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500313 err_out:
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800314 return rc;
315}
Rob Herringc2829ff2011-07-06 16:13:36 +0000316#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800317
318#define MAX_DATA_BYTE_COUNT (4*1024*1024)
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500319
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800320static int ahci_fill_sg(u8 port, unsigned char *buf, int buf_len)
321{
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800322 struct ahci_ioports *pp = &(probe_ent->port[port]);
323 struct ahci_sg *ahci_sg = pp->cmd_tbl_sg;
324 u32 sg_count;
325 int i;
326
327 sg_count = ((buf_len - 1) / MAX_DATA_BYTE_COUNT) + 1;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500328 if (sg_count > AHCI_MAX_SG) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800329 printf("Error:Too much sg!\n");
330 return -1;
331 }
332
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500333 for (i = 0; i < sg_count; i++) {
334 ahci_sg->addr =
335 cpu_to_le32((u32) buf + i * MAX_DATA_BYTE_COUNT);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800336 ahci_sg->addr_hi = 0;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500337 ahci_sg->flags_size = cpu_to_le32(0x3fffff &
338 (buf_len < MAX_DATA_BYTE_COUNT
339 ? (buf_len - 1)
340 : (MAX_DATA_BYTE_COUNT - 1)));
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800341 ahci_sg++;
342 buf_len -= MAX_DATA_BYTE_COUNT;
343 }
344
345 return sg_count;
346}
347
348
349static void ahci_fill_cmd_slot(struct ahci_ioports *pp, u32 opts)
350{
351 pp->cmd_slot->opts = cpu_to_le32(opts);
352 pp->cmd_slot->status = 0;
353 pp->cmd_slot->tbl_addr = cpu_to_le32(pp->cmd_tbl & 0xffffffff);
354 pp->cmd_slot->tbl_addr_hi = 0;
355}
356
357
358static void ahci_set_feature(u8 port)
359{
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800360 struct ahci_ioports *pp = &(probe_ent->port[port]);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500361 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
362 u32 cmd_fis_len = 5; /* five dwords */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800363 u8 fis[20];
364
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500365 /*set feature */
366 memset(fis, 0, 20);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800367 fis[0] = 0x27;
368 fis[1] = 1 << 7;
369 fis[2] = ATA_CMD_SETF;
370 fis[3] = SETFEATURES_XFER;
371 fis[12] = __ilog2(probe_ent->udma_mask + 1) + 0x40 - 0x01;
372
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500373 memcpy((unsigned char *)pp->cmd_tbl, fis, 20);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800374 ahci_fill_cmd_slot(pp, cmd_fis_len);
375 writel(1, port_mmio + PORT_CMD_ISSUE);
376 readl(port_mmio + PORT_CMD_ISSUE);
377
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500378 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800379 printf("set feature error!\n");
380 }
381}
382
383
384static int ahci_port_start(u8 port)
385{
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800386 struct ahci_ioports *pp = &(probe_ent->port[port]);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500387 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800388 u32 port_status;
389 u32 mem;
390
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500391 debug("Enter start port: %d\n", port);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800392 port_status = readl(port_mmio + PORT_SCR_STAT);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500393 debug("Port %d status: %x\n", port, port_status);
394 if ((port_status & 0xf) != 0x03) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800395 printf("No Link on this port!\n");
396 return -1;
397 }
398
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500399 mem = (u32) malloc(AHCI_PORT_PRIV_DMA_SZ + 2048);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800400 if (!mem) {
401 free(pp);
402 printf("No mem for table!\n");
403 return -ENOMEM;
404 }
405
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500406 mem = (mem + 0x800) & (~0x7ff); /* Aligned to 2048-bytes */
407 memset((u8 *) mem, 0, AHCI_PORT_PRIV_DMA_SZ);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800408
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800409 /*
410 * First item in chunk of DMA memory: 32-slot command table,
411 * 32 bytes each in size
412 */
413 pp->cmd_slot = (struct ahci_cmd_hdr *)mem;
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000414 debug("cmd_slot = 0x%x\n", (unsigned)pp->cmd_slot);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800415 mem += (AHCI_CMD_SLOT_SZ + 224);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500416
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800417 /*
418 * Second item: Received-FIS area
419 */
420 pp->rx_fis = mem;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800421 mem += AHCI_RX_FIS_SZ;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500422
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800423 /*
424 * Third item: data area for storing a single command
425 * and its scatter-gather table
426 */
427 pp->cmd_tbl = mem;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500428 debug("cmd_tbl_dma = 0x%x\n", pp->cmd_tbl);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800429
430 mem += AHCI_CMD_TBL_HDR;
431 pp->cmd_tbl_sg = (struct ahci_sg *)mem;
432
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500433 writel_with_flush((u32) pp->cmd_slot, port_mmio + PORT_LST_ADDR);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800434
435 writel_with_flush(pp->rx_fis, port_mmio + PORT_FIS_ADDR);
436
437 writel_with_flush(PORT_CMD_ICC_ACTIVE | PORT_CMD_FIS_RX |
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500438 PORT_CMD_POWER_ON | PORT_CMD_SPIN_UP |
439 PORT_CMD_START, port_mmio + PORT_CMD);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800440
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500441 debug("Exit start port %d\n", port);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800442
443 return 0;
444}
445
446
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500447static int get_ahci_device_data(u8 port, u8 *fis, int fis_len, u8 *buf,
448 int buf_len)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800449{
450
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500451 struct ahci_ioports *pp = &(probe_ent->port[port]);
452 volatile u8 *port_mmio = (volatile u8 *)pp->port_mmio;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800453 u32 opts;
454 u32 port_status;
455 int sg_count;
456
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500457 debug("Enter get_ahci_device_data: for port %d\n", port);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800458
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500459 if (port > probe_ent->n_ports) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800460 printf("Invaild port number %d\n", port);
461 return -1;
462 }
463
464 port_status = readl(port_mmio + PORT_SCR_STAT);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500465 if ((port_status & 0xf) != 0x03) {
466 debug("No Link on port %d!\n", port);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800467 return -1;
468 }
469
470 memcpy((unsigned char *)pp->cmd_tbl, fis, fis_len);
471
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500472 sg_count = ahci_fill_sg(port, buf, buf_len);
473 opts = (fis_len >> 2) | (sg_count << 16);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800474 ahci_fill_cmd_slot(pp, opts);
475
476 writel_with_flush(1, port_mmio + PORT_CMD_ISSUE);
477
478 if (waiting_for_cmd_completed(port_mmio + PORT_CMD_ISSUE, 150, 0x1)) {
479 printf("timeout exit!\n");
480 return -1;
481 }
482 debug("get_ahci_device_data: %d byte transferred.\n",
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500483 pp->cmd_slot->status);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800484
485 return 0;
486}
487
488
489static char *ata_id_strcpy(u16 *target, u16 *src, int len)
490{
491 int i;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500492 for (i = 0; i < len / 2; i++)
Rob Herring336018392011-06-01 09:10:26 +0000493 target[i] = swab16(src[i]);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800494 return (char *)target;
495}
496
497
498static void dump_ataid(hd_driveid_t *ataid)
499{
500 debug("(49)ataid->capability = 0x%x\n", ataid->capability);
501 debug("(53)ataid->field_valid =0x%x\n", ataid->field_valid);
502 debug("(63)ataid->dma_mword = 0x%x\n", ataid->dma_mword);
503 debug("(64)ataid->eide_pio_modes = 0x%x\n", ataid->eide_pio_modes);
504 debug("(75)ataid->queue_depth = 0x%x\n", ataid->queue_depth);
505 debug("(80)ataid->major_rev_num = 0x%x\n", ataid->major_rev_num);
506 debug("(81)ataid->minor_rev_num = 0x%x\n", ataid->minor_rev_num);
507 debug("(82)ataid->command_set_1 = 0x%x\n", ataid->command_set_1);
508 debug("(83)ataid->command_set_2 = 0x%x\n", ataid->command_set_2);
509 debug("(84)ataid->cfsse = 0x%x\n", ataid->cfsse);
510 debug("(85)ataid->cfs_enable_1 = 0x%x\n", ataid->cfs_enable_1);
511 debug("(86)ataid->cfs_enable_2 = 0x%x\n", ataid->cfs_enable_2);
512 debug("(87)ataid->csf_default = 0x%x\n", ataid->csf_default);
513 debug("(88)ataid->dma_ultra = 0x%x\n", ataid->dma_ultra);
514 debug("(93)ataid->hw_config = 0x%x\n", ataid->hw_config);
515}
516
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500517
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800518/*
519 * SCSI INQUIRY command operation.
520 */
521static int ata_scsiop_inquiry(ccb *pccb)
522{
523 u8 hdr[] = {
524 0,
525 0,
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500526 0x5, /* claim SPC-3 version compatibility */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800527 2,
528 95 - 4,
529 };
530 u8 fis[20];
531 u8 *tmpid;
532 u8 port;
533
534 /* Clean ccb data buffer */
535 memset(pccb->pdata, 0, pccb->datalen);
536
537 memcpy(pccb->pdata, hdr, sizeof(hdr));
538
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500539 if (pccb->datalen <= 35)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800540 return 0;
541
542 memset(fis, 0, 20);
543 /* Construct the FIS */
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500544 fis[0] = 0x27; /* Host to device FIS. */
545 fis[1] = 1 << 7; /* Command FIS. */
546 fis[2] = ATA_CMD_IDENT; /* Command byte. */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800547
548 /* Read id from sata */
549 port = pccb->target;
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500550 if (!(tmpid = malloc(sizeof(hd_driveid_t))))
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800551 return -ENOMEM;
552
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500553 if (get_ahci_device_data(port, (u8 *) & fis, 20,
554 tmpid, sizeof(hd_driveid_t))) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800555 debug("scsi_ahci: SCSI inquiry command failure.\n");
556 return -EIO;
557 }
558
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500559 if (ataid[port])
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800560 free(ataid[port]);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500561 ataid[port] = (hd_driveid_t *) tmpid;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800562
563 memcpy(&pccb->pdata[8], "ATA ", 8);
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500564 ata_id_strcpy((u16 *) &pccb->pdata[16], (u16 *)ataid[port]->model, 16);
565 ata_id_strcpy((u16 *) &pccb->pdata[32], (u16 *)ataid[port]->fw_rev, 4);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800566
567 dump_ataid(ataid[port]);
568 return 0;
569}
570
571
572/*
573 * SCSI READ10 command operation.
574 */
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500575static int ata_scsiop_read10(ccb * pccb)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800576{
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000577 u32 lba = 0;
578 u16 blocks = 0;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800579 u8 fis[20];
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000580 u8 *user_buffer = pccb->pdata;
581 u32 user_buffer_size = pccb->datalen;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800582
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000583 /* Retrieve the base LBA number from the ccb structure. */
584 memcpy(&lba, pccb->cmd + 2, sizeof(lba));
585 lba = be32_to_cpu(lba);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800586
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000587 /*
588 * And the number of blocks.
589 *
590 * For 10-byte and 16-byte SCSI R/W commands, transfer
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800591 * length 0 means transfer 0 block of data.
592 * However, for ATA R/W commands, sector count 0 means
593 * 256 or 65536 sectors, not 0 sectors as in SCSI.
594 *
595 * WARNING: one or two older ATA drives treat 0 as 0...
596 */
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000597 blocks = (((u16)pccb->cmd[7]) << 8) | ((u16) pccb->cmd[8]);
598
599 debug("scsi_ahci: read %d blocks starting from lba 0x%x\n",
600 (unsigned)lba, blocks);
601
602 /* Preset the FIS */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800603 memset(fis, 0, 20);
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000604 fis[0] = 0x27; /* Host to device FIS. */
605 fis[1] = 1 << 7; /* Command FIS. */
606 fis[2] = ATA_CMD_RD_DMA; /* Command byte. */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800607
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000608 while (blocks) {
609 u16 now_blocks; /* number of blocks per iteration */
610 u32 transfer_size; /* number of bytes per iteration */
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800611
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000612 now_blocks = min(MAX_SATA_BLOCKS_READ, blocks);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800613
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000614 transfer_size = ATA_BLOCKSIZE * now_blocks;
615 if (transfer_size > user_buffer_size) {
616 printf("scsi_ahci: Error: buffer too small.\n");
617 return -EIO;
618 }
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800619
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000620 /* LBA address, only support LBA28 in this driver */
621 fis[4] = (lba >> 0) & 0xff;
622 fis[5] = (lba >> 8) & 0xff;
623 fis[6] = (lba >> 16) & 0xff;
624 fis[7] = ((lba >> 24) & 0xf) | 0xe0;
625
626 /* Block (sector) count */
627 fis[12] = (now_blocks >> 0) & 0xff;
628 fis[13] = (now_blocks >> 8) & 0xff;
629
630 /* Read from ahci */
631 if (get_ahci_device_data(pccb->target, (u8 *) &fis, sizeof(fis),
632 user_buffer, user_buffer_size)) {
633 debug("scsi_ahci: SCSI READ10 command failure.\n");
634 return -EIO;
635 }
636 user_buffer += transfer_size;
637 user_buffer_size -= transfer_size;
638 blocks -= now_blocks;
639 lba += now_blocks;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800640 }
641
642 return 0;
643}
644
645
646/*
647 * SCSI READ CAPACITY10 command operation.
648 */
649static int ata_scsiop_read_capacity10(ccb *pccb)
650{
Kumar Gala8a190652009-07-13 09:24:00 -0500651 u32 cap;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800652
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500653 if (!ataid[pccb->target]) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800654 printf("scsi_ahci: SCSI READ CAPACITY10 command failure. "
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500655 "\tNo ATA info!\n"
656 "\tPlease run SCSI commmand INQUIRY firstly!\n");
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800657 return -EPERM;
658 }
659
Vadim Bendebury700f85c2012-10-29 05:23:44 +0000660 cap = be32_to_cpu(ataid[pccb->target]->lba_capacity);
Kumar Gala8a190652009-07-13 09:24:00 -0500661 memcpy(pccb->pdata, &cap, sizeof(cap));
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800662
Kumar Gala8a190652009-07-13 09:24:00 -0500663 pccb->pdata[4] = pccb->pdata[5] = 0;
664 pccb->pdata[6] = 512 >> 8;
665 pccb->pdata[7] = 512 & 0xff;
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800666
667 return 0;
668}
669
670
671/*
672 * SCSI TEST UNIT READY command operation.
673 */
674static int ata_scsiop_test_unit_ready(ccb *pccb)
675{
676 return (ataid[pccb->target]) ? 0 : -EPERM;
677}
678
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500679
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800680int scsi_exec(ccb *pccb)
681{
682 int ret;
683
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500684 switch (pccb->cmd[0]) {
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800685 case SCSI_READ10:
686 ret = ata_scsiop_read10(pccb);
687 break;
688 case SCSI_RD_CAPAC:
689 ret = ata_scsiop_read_capacity10(pccb);
690 break;
691 case SCSI_TST_U_RDY:
692 ret = ata_scsiop_test_unit_ready(pccb);
693 break;
694 case SCSI_INQUIRY:
695 ret = ata_scsiop_inquiry(pccb);
696 break;
697 default:
698 printf("Unsupport SCSI command 0x%02x\n", pccb->cmd[0]);
699 return FALSE;
700 }
701
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500702 if (ret) {
703 debug("SCSI command 0x%02x ret errno %d\n", pccb->cmd[0], ret);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800704 return FALSE;
705 }
706 return TRUE;
707
708}
709
710
711void scsi_low_level_init(int busdevfunc)
712{
713 int i;
714 u32 linkmap;
715
Rob Herringc2829ff2011-07-06 16:13:36 +0000716#ifndef CONFIG_SCSI_AHCI_PLAT
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800717 ahci_init_one(busdevfunc);
Rob Herringc2829ff2011-07-06 16:13:36 +0000718#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800719
720 linkmap = probe_ent->link_port_map;
721
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200722 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500723 if (((linkmap >> i) & 0x01)) {
724 if (ahci_port_start((u8) i)) {
725 printf("Can not start port %d\n", i);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800726 continue;
727 }
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500728 ahci_set_feature((u8) i);
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800729 }
730 }
731}
732
Rob Herringc2829ff2011-07-06 16:13:36 +0000733#ifdef CONFIG_SCSI_AHCI_PLAT
734int ahci_init(u32 base)
735{
736 int i, rc = 0;
737 u32 linkmap;
738
739 memset(ataid, 0, sizeof(ataid));
740
741 probe_ent = malloc(sizeof(struct ahci_probe_ent));
742 memset(probe_ent, 0, sizeof(struct ahci_probe_ent));
743
744 probe_ent->host_flags = ATA_FLAG_SATA
745 | ATA_FLAG_NO_LEGACY
746 | ATA_FLAG_MMIO
747 | ATA_FLAG_PIO_DMA
748 | ATA_FLAG_NO_ATAPI;
749 probe_ent->pio_mask = 0x1f;
750 probe_ent->udma_mask = 0x7f; /*Fixme,assume to support UDMA6 */
751
752 probe_ent->mmio_base = base;
753
754 /* initialize adapter */
755 rc = ahci_host_init(probe_ent);
756 if (rc)
757 goto err_out;
758
759 ahci_print_info(probe_ent);
760
761 linkmap = probe_ent->link_port_map;
762
763 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
764 if (((linkmap >> i) & 0x01)) {
765 if (ahci_port_start((u8) i)) {
766 printf("Can not start port %d\n", i);
767 continue;
768 }
769 ahci_set_feature((u8) i);
770 }
771 }
772err_out:
773 return rc;
774}
775#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800776
777void scsi_bus_reset(void)
778{
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500779 /*Not implement*/
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800780}
781
782
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500783void scsi_print_error(ccb * pccb)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800784{
Jon Loeligerc0b0cda2006-08-23 11:04:43 -0500785 /*The ahci error info can be read in the ahci driver*/
Jin Zhengxiongae180dc2006-08-23 19:10:44 +0800786}