blob: b70f04de8b983da916cafa309ef9d6c04a6458bd [file] [log] [blame]
Tang Yuantian83c484d2011-10-07 19:26:58 +00001/*
2 * Copyright (C) 2011 Freescale Semiconductor, Inc.
3 * Author: Tang Yuantian <b29983@freescale.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 */
20
21#include <common.h>
22#include <pci.h>
23#include <command.h>
24#include <asm/byteorder.h>
25#include <malloc.h>
26#include <asm/io.h>
27#include <fis.h>
Pavel Herrmanne46a4352012-09-27 23:18:04 +000028#include <sata.h>
Tang Yuantian83c484d2011-10-07 19:26:58 +000029#include <libata.h>
Kim Phillips00caa7f2012-10-29 13:34:40 +000030#include <sata.h>
Tang Yuantian83c484d2011-10-07 19:26:58 +000031#include "sata_sil.h"
32
33/* Convert sectorsize to wordsize */
34#define ATA_SECTOR_WORDS (ATA_SECT_SIZE/2)
Tang Yuantian83c484d2011-10-07 19:26:58 +000035#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
36
37static struct sata_info sata_info;
38
39static struct pci_device_id supported[] = {
40 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131},
41 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132},
42 {PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124},
43 {}
44};
45
46static void sil_sata_dump_fis(struct sata_fis_d2h *s)
47{
48 printf("Status FIS dump:\n");
49 printf("fis_type: %02x\n", s->fis_type);
50 printf("pm_port_i: %02x\n", s->pm_port_i);
51 printf("status: %02x\n", s->status);
52 printf("error: %02x\n", s->error);
53 printf("lba_low: %02x\n", s->lba_low);
54 printf("lba_mid: %02x\n", s->lba_mid);
55 printf("lba_high: %02x\n", s->lba_high);
56 printf("device: %02x\n", s->device);
57 printf("lba_low_exp: %02x\n", s->lba_low_exp);
58 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
59 printf("lba_high_exp: %02x\n", s->lba_high_exp);
60 printf("res1: %02x\n", s->res1);
61 printf("sector_count: %02x\n", s->sector_count);
62 printf("sector_count_exp: %02x\n", s->sector_count_exp);
63}
64
65static const char *sata_spd_string(unsigned int speed)
66{
67 static const char * const spd_str[] = {
68 "1.5 Gbps",
69 "3.0 Gbps",
70 "6.0 Gbps",
71 };
72
73 if ((speed - 1) > 2)
74 return "<unknown>";
75
76 return spd_str[speed - 1];
77}
78
79static u32 ata_wait_register(void *reg, u32 mask,
80 u32 val, int timeout_msec)
81{
82 u32 tmp;
83
84 tmp = readl(reg);
85 while ((tmp & mask) == val && timeout_msec > 0) {
86 mdelay(1);
87 timeout_msec--;
88 tmp = readl(reg);
89 }
90
91 return tmp;
92}
93
94static void sil_config_port(void *port)
95{
96 /* configure IRQ WoC */
97 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
98
99 /* zero error counters. */
100 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
101 writew(0x8000, port + PORT_CRC_ERR_THRESH);
102 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
103 writew(0x0000, port + PORT_DECODE_ERR_CNT);
104 writew(0x0000, port + PORT_CRC_ERR_CNT);
105 writew(0x0000, port + PORT_HSHK_ERR_CNT);
106
107 /* always use 64bit activation */
108 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
109
110 /* clear port multiplier enable and resume bits */
111 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
112}
113
114static int sil_init_port(void *port)
115{
116 u32 tmp;
117
118 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
119 ata_wait_register(port + PORT_CTRL_STAT,
120 PORT_CS_INIT, PORT_CS_INIT, 100);
121 tmp = ata_wait_register(port + PORT_CTRL_STAT,
122 PORT_CS_RDY, 0, 100);
123
124 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
125 return 1;
126
127 return 0;
128}
129
130static void sil_read_fis(int dev, int tag, struct sata_fis_d2h *fis)
131{
132 struct sil_sata *sata = sata_dev_desc[dev].priv;
133 void *port = sata->port;
134 struct sil_prb *prb;
135 int i;
136 u32 *src, *dst;
137
138 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
139 src = (u32 *)&prb->fis;
140 dst = (u32 *)fis;
141 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
142 *dst++ = readl(src++);
143}
144
145static int sil_exec_cmd(int dev, struct sil_cmd_block *pcmd, int tag)
146{
147 struct sil_sata *sata = sata_dev_desc[dev].priv;
148 void *port = sata->port;
149 u64 paddr = virt_to_bus(sata->devno, pcmd);
150 u32 irq_mask, irq_stat;
151 int rc;
152
153 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
154
155 /* better to add momery barrior here */
156 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
157 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
158
159 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
160 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
161 0, 10000);
162
163 /* clear IRQs */
164 writel(irq_mask, port + PORT_IRQ_STAT);
165 irq_stat >>= PORT_IRQ_RAW_SHIFT;
166
167 if (irq_stat & PORT_IRQ_COMPLETE)
168 rc = 0;
169 else {
170 /* force port into known state */
171 sil_init_port(port);
172 if (irq_stat & PORT_IRQ_ERROR)
173 rc = 1; /* error */
174 else
175 rc = 2; /* busy */
176 }
177
178 return rc;
179}
180
181static int sil_cmd_set_feature(int dev)
182{
183 struct sil_sata *sata = sata_dev_desc[dev].priv;
184 struct sil_cmd_block cmdb, *pcmd = &cmdb;
185 struct sata_fis_d2h fis;
186 u8 udma_cap;
187 int ret;
188
189 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
190 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
191 pcmd->prb.fis.pm_port_c = (1 << 7);
192 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
193 pcmd->prb.fis.features = SETFEATURES_XFER;
194
195 /* First check the device capablity */
196 udma_cap = (u8)(sata->udma & 0xff);
197 debug("udma_cap %02x\n", udma_cap);
198
199 if (udma_cap == ATA_UDMA6)
200 pcmd->prb.fis.sector_count = XFER_UDMA_6;
201 if (udma_cap == ATA_UDMA5)
202 pcmd->prb.fis.sector_count = XFER_UDMA_5;
203 if (udma_cap == ATA_UDMA4)
204 pcmd->prb.fis.sector_count = XFER_UDMA_4;
205 if (udma_cap == ATA_UDMA3)
206 pcmd->prb.fis.sector_count = XFER_UDMA_3;
207
208 ret = sil_exec_cmd(dev, pcmd, 0);
209 if (ret) {
210 sil_read_fis(dev, 0, &fis);
211 printf("Err: exe cmd(0x%x).\n",
212 readl(sata->port + PORT_SERROR));
213 sil_sata_dump_fis(&fis);
214 return 1;
215 }
216
217 return 0;
218}
219
220static int sil_cmd_identify_device(int dev, u16 *id)
221{
222 struct sil_sata *sata = sata_dev_desc[dev].priv;
223 struct sil_cmd_block cmdb, *pcmd = &cmdb;
224 struct sata_fis_d2h fis;
225 int ret;
226
227 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
228 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
229 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
230 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
231 pcmd->prb.fis.pm_port_c = (1 << 7);
232 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
233 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
234 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
235 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
236
237 ret = sil_exec_cmd(dev, pcmd, 0);
238 if (ret) {
239 sil_read_fis(dev, 0, &fis);
240 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
241 sil_sata_dump_fis(&fis);
242 return 1;
243 }
244 ata_swap_buf_le16(id, ATA_ID_WORDS);
245
246 return 0;
247}
248
249static int sil_cmd_soft_reset(int dev)
250{
251 struct sil_cmd_block cmdb, *pcmd = &cmdb;
252 struct sil_sata *sata = sata_dev_desc[dev].priv;
253 struct sata_fis_d2h fis;
254 void *port = sata->port;
255 int ret;
256
257 /* put the port into known state */
258 if (sil_init_port(port)) {
259 printf("SRST: port %d not ready\n", dev);
260 return 1;
261 }
262
263 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
264
265 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
266 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
267 pcmd->prb.fis.pm_port_c = 0xf;
268
269 ret = sil_exec_cmd(dev, &cmdb, 0);
270 if (ret) {
271 sil_read_fis(dev, 0, &fis);
272 printf("SRST cmd error.\n");
273 sil_sata_dump_fis(&fis);
274 return 1;
275 }
276
277 return 0;
278}
279
280static ulong sil_sata_rw_cmd(int dev, ulong start, ulong blkcnt,
281 u8 *buffer, int is_write)
282{
283 struct sil_sata *sata = sata_dev_desc[dev].priv;
284 struct sil_cmd_block cmdb, *pcmd = &cmdb;
285 struct sata_fis_d2h fis;
286 u64 block;
287 int ret;
288
289 block = (u64)start;
290 memset(pcmd, 0, sizeof(struct sil_cmd_block));
291 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
292 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
293 pcmd->prb.fis.pm_port_c = (1 << 7);
294 if (is_write) {
295 pcmd->prb.fis.command = ATA_CMD_WRITE;
296 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
297 } else {
298 pcmd->prb.fis.command = ATA_CMD_READ;
299 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
300 }
301
302 pcmd->prb.fis.device = ATA_LBA;
303 pcmd->prb.fis.device |= (block >> 24) & 0xf;
304 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
305 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
306 pcmd->prb.fis.lba_low = block & 0xff;
307 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
308
309 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
310 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
311 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
312
313 ret = sil_exec_cmd(dev, pcmd, 0);
314 if (ret) {
315 sil_read_fis(dev, 0, &fis);
316 printf("Err: rw cmd(0x%08x).\n",
317 readl(sata->port + PORT_SERROR));
318 sil_sata_dump_fis(&fis);
319 return 1;
320 }
321
322 return blkcnt;
323}
324
325static ulong sil_sata_rw_cmd_ext(int dev, ulong start, ulong blkcnt,
326 u8 *buffer, int is_write)
327{
328 struct sil_sata *sata = sata_dev_desc[dev].priv;
329 struct sil_cmd_block cmdb, *pcmd = &cmdb;
330 struct sata_fis_d2h fis;
331 u64 block;
332 int ret;
333
334 block = (u64)start;
335 memset(pcmd, 0, sizeof(struct sil_cmd_block));
336 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
337 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
338 pcmd->prb.fis.pm_port_c = (1 << 7);
339 if (is_write) {
340 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
341 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
342 } else {
343 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
344 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
345 }
346
347 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
348 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
349 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
350 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
351 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
352 pcmd->prb.fis.lba_low = block & 0xff;
353 pcmd->prb.fis.device = ATA_LBA;
354 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
355 pcmd->prb.fis.sector_count = blkcnt & 0xff;
356
357 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
358 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
359 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
360
361 ret = sil_exec_cmd(dev, pcmd, 0);
362 if (ret) {
363 sil_read_fis(dev, 0, &fis);
364 printf("Err: rw ext cmd(0x%08x).\n",
365 readl(sata->port + PORT_SERROR));
366 sil_sata_dump_fis(&fis);
367 return 1;
368 }
369
370 return blkcnt;
371}
372
Kim Phillips00caa7f2012-10-29 13:34:40 +0000373static ulong sil_sata_rw_lba28(int dev, ulong blknr, lbaint_t blkcnt,
374 const void *buffer, int is_write)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000375{
376 ulong start, blks, max_blks;
377 u8 *addr;
378
379 start = blknr;
380 blks = blkcnt;
381 addr = (u8 *)buffer;
382
383 max_blks = ATA_MAX_SECTORS;
384 do {
385 if (blks > max_blks) {
386 sil_sata_rw_cmd(dev, start, max_blks, addr, is_write);
387 start += max_blks;
388 blks -= max_blks;
389 addr += ATA_SECT_SIZE * max_blks;
390 } else {
391 sil_sata_rw_cmd(dev, start, blks, addr, is_write);
392 start += blks;
393 blks = 0;
394 addr += ATA_SECT_SIZE * blks;
395 }
396 } while (blks != 0);
397
398 return blkcnt;
399}
400
Kim Phillips00caa7f2012-10-29 13:34:40 +0000401static ulong sil_sata_rw_lba48(int dev, ulong blknr, lbaint_t blkcnt,
402 const void *buffer, int is_write)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000403{
404 ulong start, blks, max_blks;
405 u8 *addr;
406
407 start = blknr;
408 blks = blkcnt;
409 addr = (u8 *)buffer;
410
411 max_blks = ATA_MAX_SECTORS_LBA48;
412 do {
413 if (blks > max_blks) {
414 sil_sata_rw_cmd_ext(dev, start, max_blks,
415 addr, is_write);
416 start += max_blks;
417 blks -= max_blks;
418 addr += ATA_SECT_SIZE * max_blks;
419 } else {
420 sil_sata_rw_cmd_ext(dev, start, blks,
421 addr, is_write);
422 start += blks;
423 blks = 0;
424 addr += ATA_SECT_SIZE * blks;
425 }
426 } while (blks != 0);
427
428 return blkcnt;
429}
430
Kim Phillips00caa7f2012-10-29 13:34:40 +0000431static void sil_sata_cmd_flush_cache(int dev)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000432{
433 struct sil_cmd_block cmdb, *pcmd = &cmdb;
434
435 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
436 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
437 pcmd->prb.fis.pm_port_c = (1 << 7);
438 pcmd->prb.fis.command = ATA_CMD_FLUSH;
439
440 sil_exec_cmd(dev, pcmd, 0);
441}
442
Kim Phillips00caa7f2012-10-29 13:34:40 +0000443static void sil_sata_cmd_flush_cache_ext(int dev)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000444{
445 struct sil_cmd_block cmdb, *pcmd = &cmdb;
446
447 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
448 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
449 pcmd->prb.fis.pm_port_c = (1 << 7);
450 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
451
452 sil_exec_cmd(dev, pcmd, 0);
453}
454
455static void sil_sata_init_wcache(int dev, u16 *id)
456{
457 struct sil_sata *sata = sata_dev_desc[dev].priv;
458
459 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
460 sata->wcache = 1;
461 if (ata_id_has_flush(id))
462 sata->flush = 1;
463 if (ata_id_has_flush_ext(id))
464 sata->flush_ext = 1;
465}
466
467static int sil_sata_get_wcache(int dev)
468{
469 struct sil_sata *sata = sata_dev_desc[dev].priv;
470
471 return sata->wcache;
472}
473
474static int sil_sata_get_flush(int dev)
475{
476 struct sil_sata *sata = sata_dev_desc[dev].priv;
477
478 return sata->flush;
479}
480
481static int sil_sata_get_flush_ext(int dev)
482{
483 struct sil_sata *sata = sata_dev_desc[dev].priv;
484
485 return sata->flush_ext;
486}
487
488/*
489 * SATA interface between low level driver and command layer
490 */
491ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
492{
493 struct sil_sata *sata = sata_dev_desc[dev].priv;
494 ulong rc;
495
496 if (sata->lba48)
497 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
498 else
499 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
500
501 return rc;
502}
503
504/*
505 * SATA interface between low level driver and command layer
506 */
Tom Rini0e7d8562012-09-29 07:57:25 -0700507ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000508{
509 struct sil_sata *sata = sata_dev_desc[dev].priv;
510 ulong rc;
511
512 if (sata->lba48) {
513 rc = sil_sata_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
514 if (sil_sata_get_wcache(dev) && sil_sata_get_flush_ext(dev))
515 sil_sata_cmd_flush_cache_ext(dev);
516 } else {
517 rc = sil_sata_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
518 if (sil_sata_get_wcache(dev) && sil_sata_get_flush(dev))
519 sil_sata_cmd_flush_cache(dev);
520 }
521
522 return rc;
523}
524
525/*
526 * SATA interface between low level driver and command layer
527 */
528int init_sata(int dev)
529{
530 static int init_done, idx;
531 pci_dev_t devno;
532 u16 word;
533
534 if (init_done == 1 && dev < sata_info.maxport)
535 return 1;
536
537 init_done = 1;
538
539 /* Find PCI device(s) */
540 devno = pci_find_devices(supported, idx++);
541 if (devno == -1)
542 return 1;
543
544 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
545
546 /* get the port count */
547 word &= 0xf;
548
549 sata_info.portbase = sata_info.maxport;
550 sata_info.maxport = sata_info.portbase + word;
551 sata_info.devno = devno;
552
553 /* Read out all BARs */
554 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
555 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
556 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
557 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
558 sata_info.iobase[2] = (ulong)pci_map_bar(devno,
559 PCI_BASE_ADDRESS_4, PCI_REGION_MEM);
560
561 /* mask out the unused bits */
562 sata_info.iobase[0] &= 0xffffff80;
563 sata_info.iobase[1] &= 0xfffffc00;
564 sata_info.iobase[2] &= 0xffffff80;
565
566 /* Enable Bus Mastering and memory region */
567 pci_write_config_word(devno, PCI_COMMAND,
568 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
569
570 /* Check if mem accesses and Bus Mastering are enabled. */
571 pci_read_config_word(devno, PCI_COMMAND, &word);
572 if (!(word & PCI_COMMAND_MEMORY) ||
573 (!(word & PCI_COMMAND_MASTER))) {
574 printf("Error: Can not enable MEM access or Bus Mastering.\n");
575 debug("PCI command: %04x\n", word);
576 return 1;
577 }
578
579 /* GPIO off */
580 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
581 /* clear global reset & mask interrupts during initialization */
582 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
583
584 return 0;
585}
586
587/*
588 * SATA interface between low level driver and command layer
589 */
590int scan_sata(int dev)
591{
592 unsigned char serial[ATA_ID_SERNO_LEN + 1];
593 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
594 unsigned char product[ATA_ID_PROD_LEN + 1];
595 struct sil_sata *sata;
596 void *port;
597 int cnt;
598 u16 *id;
599 u32 tmp;
600
601 if (dev >= sata_info.maxport) {
602 printf("SATA#%d is not present\n", dev);
603 return 1;
604 }
605
606 printf("SATA#%d\n", dev);
607 port = (void *)sata_info.iobase[1] +
608 PORT_REGS_SIZE * (dev - sata_info.portbase);
609
610 /* Initial PHY setting */
611 writel(0x20c, port + PORT_PHY_CFG);
612
613 /* clear port RST */
614 tmp = readl(port + PORT_CTRL_STAT);
615 if (tmp & PORT_CS_PORT_RST) {
616 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
617 tmp = ata_wait_register(port + PORT_CTRL_STAT,
618 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
619 if (tmp & PORT_CS_PORT_RST)
620 printf("Err: Failed to clear port RST\n");
621 }
622
623 /* Check if device is present */
624 for (cnt = 0; cnt < 100; cnt++) {
625 tmp = readl(port + PORT_SSTATUS);
626 if ((tmp & 0xF) == 0x3)
627 break;
628 mdelay(1);
629 }
630
631 tmp = readl(port + PORT_SSTATUS);
632 if ((tmp & 0xf) != 0x3) {
633 printf(" (No RDY)\n");
634 return 1;
635 }
636
637 /* Wait for port ready */
638 tmp = ata_wait_register(port + PORT_CTRL_STAT,
639 PORT_CS_RDY, PORT_CS_RDY, 100);
640 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
641 printf("%d port not ready.\n", dev);
642 return 1;
643 }
644
645 /* configure port */
646 sil_config_port(port);
647
648 /* Reset port */
649 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
650 readl(port + PORT_CTRL_STAT);
651 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
652 PORT_CS_DEV_RST, 100);
653 if (tmp & PORT_CS_DEV_RST) {
654 printf("%d port reset failed.\n", dev);
655 return 1;
656 }
657
658 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
659 if (!sata) {
660 printf("%d no memory.\n", dev);
661 return 1;
662 }
663 memset((void *)sata, 0, sizeof(struct sil_sata));
664
665 /* turn on port interrupt */
666 tmp = readl((void *)(sata_info.iobase[0] + HOST_CTRL));
667 tmp |= (1 << (dev - sata_info.portbase));
668 writel(tmp, (void *)(sata_info.iobase[0] + HOST_CTRL));
669
670 /* Save the private struct to block device struct */
671 sata_dev_desc[dev].priv = (void *)sata;
672 sata->port = port;
673 sata->devno = sata_info.devno;
674 sprintf(sata->name, "SATA#%d", dev);
675 sil_cmd_soft_reset(dev);
676 tmp = readl(port + PORT_SSTATUS);
677 tmp = (tmp >> 4) & 0xf;
678 printf(" (%s)\n", sata_spd_string(tmp));
679
680 id = (u16 *)malloc(ATA_ID_WORDS * 2);
681 if (!id) {
682 printf("Id malloc failed\n");
683 free((void *)sata);
684 return 1;
685 }
686 sil_cmd_identify_device(dev, id);
687
688#ifdef CONFIG_LBA48
689 /* Check if support LBA48 */
690 if (ata_id_has_lba48(id)) {
691 sata_dev_desc[dev].lba48 = 1;
692 sata->lba48 = 1;
693 debug("Device supports LBA48\n");
694 } else
695 debug("Device supports LBA28\n");
696#endif
697
698 /* Serial number */
699 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
700 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
701
702 /* Firmware version */
703 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
704 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
705
706 /* Product model */
707 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
708 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
709
710 /* Totoal sectors */
711 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
712
713 sil_sata_init_wcache(dev, id);
714 sil_cmd_set_feature(dev);
715
716#ifdef DEBUG
717 sil_cmd_identify_device(dev, id);
718 ata_dump_id(id);
719#endif
720 free((void *)id);
721
722 return 0;
723}