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