blob: 6896fa8771be0231f7efcb04544827ac5ebfed77 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tang Yuantian4511ccc2011-10-07 19:26:58 +00002/*
3 * Copyright (C) 2011 Freescale Semiconductor, Inc.
Peng Ma96f336c2019-11-19 06:17:40 +00004 * Copyright 2019 NXP
Tang Yuantian4511ccc2011-10-07 19:26:58 +00005 * Author: Tang Yuantian <b29983@freescale.com>
Tang Yuantian4511ccc2011-10-07 19:26:58 +00006 */
7
8#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Tang Yuantian4511ccc2011-10-07 19:26:58 +000011#include <pci.h>
12#include <command.h>
13#include <asm/byteorder.h>
14#include <malloc.h>
15#include <asm/io.h>
16#include <fis.h>
Pavel Herrmann9e9f6282012-09-27 23:18:04 +000017#include <sata.h>
Tang Yuantian4511ccc2011-10-07 19:26:58 +000018#include <libata.h>
Kim Phillips03487482012-10-29 13:34:40 +000019#include <sata.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Peng Ma96f336c2019-11-19 06:17:40 +000021
22#if CONFIG_IS_ENABLED(BLK)
23#include <dm.h>
24#include <blk.h>
Peng Ma17dd2612019-12-04 10:36:42 +000025#include <dm/device-internal.h>
Peng Ma96f336c2019-11-19 06:17:40 +000026#endif
27
Tang Yuantian4511ccc2011-10-07 19:26:58 +000028#include "sata_sil.h"
29
Tang Yuantian4511ccc2011-10-07 19:26:58 +000030#define virt_to_bus(devno, v) pci_virt_to_mem(devno, (void *) (v))
31
Peng Ma96f336c2019-11-19 06:17:40 +000032/* just compatible ahci_ops */
33struct sil_ops {
34 int *rev0;
35 int *rev1;
36 int (*scan)(struct udevice *dev);
37};
38
Tang Yuantian4511ccc2011-10-07 19:26:58 +000039static struct sata_info sata_info;
40
41static struct pci_device_id supported[] = {
Peng Ma96f336c2019-11-19 06:17:40 +000042 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131) },
43 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132) },
44 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124) },
Tang Yuantian4511ccc2011-10-07 19:26:58 +000045 {}
46};
47
48static void sil_sata_dump_fis(struct sata_fis_d2h *s)
49{
50 printf("Status FIS dump:\n");
51 printf("fis_type: %02x\n", s->fis_type);
52 printf("pm_port_i: %02x\n", s->pm_port_i);
53 printf("status: %02x\n", s->status);
54 printf("error: %02x\n", s->error);
55 printf("lba_low: %02x\n", s->lba_low);
56 printf("lba_mid: %02x\n", s->lba_mid);
57 printf("lba_high: %02x\n", s->lba_high);
58 printf("device: %02x\n", s->device);
59 printf("lba_low_exp: %02x\n", s->lba_low_exp);
60 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
61 printf("lba_high_exp: %02x\n", s->lba_high_exp);
62 printf("res1: %02x\n", s->res1);
63 printf("sector_count: %02x\n", s->sector_count);
64 printf("sector_count_exp: %02x\n", s->sector_count_exp);
65}
66
67static const char *sata_spd_string(unsigned int speed)
68{
69 static const char * const spd_str[] = {
70 "1.5 Gbps",
71 "3.0 Gbps",
72 "6.0 Gbps",
73 };
74
75 if ((speed - 1) > 2)
76 return "<unknown>";
77
78 return spd_str[speed - 1];
79}
80
81static u32 ata_wait_register(void *reg, u32 mask,
82 u32 val, int timeout_msec)
83{
84 u32 tmp;
85
86 tmp = readl(reg);
87 while ((tmp & mask) == val && timeout_msec > 0) {
88 mdelay(1);
89 timeout_msec--;
90 tmp = readl(reg);
91 }
92
93 return tmp;
94}
95
96static void sil_config_port(void *port)
97{
98 /* configure IRQ WoC */
99 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
100
101 /* zero error counters. */
102 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
103 writew(0x8000, port + PORT_CRC_ERR_THRESH);
104 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
105 writew(0x0000, port + PORT_DECODE_ERR_CNT);
106 writew(0x0000, port + PORT_CRC_ERR_CNT);
107 writew(0x0000, port + PORT_HSHK_ERR_CNT);
108
109 /* always use 64bit activation */
110 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
111
112 /* clear port multiplier enable and resume bits */
113 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
114}
115
116static int sil_init_port(void *port)
117{
118 u32 tmp;
119
120 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
121 ata_wait_register(port + PORT_CTRL_STAT,
122 PORT_CS_INIT, PORT_CS_INIT, 100);
123 tmp = ata_wait_register(port + PORT_CTRL_STAT,
124 PORT_CS_RDY, 0, 100);
125
126 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
127 return 1;
128
129 return 0;
130}
131
Peng Ma96f336c2019-11-19 06:17:40 +0000132static void sil_read_fis(struct sil_sata *sata, int tag,
133 struct sata_fis_d2h *fis)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000134{
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000135 void *port = sata->port;
136 struct sil_prb *prb;
137 int i;
138 u32 *src, *dst;
139
140 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
141 src = (u32 *)&prb->fis;
142 dst = (u32 *)fis;
143 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
144 *dst++ = readl(src++);
145}
146
Peng Ma96f336c2019-11-19 06:17:40 +0000147static int sil_exec_cmd(struct sil_sata *sata, struct sil_cmd_block *pcmd,
148 int tag)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000149{
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000150 void *port = sata->port;
151 u64 paddr = virt_to_bus(sata->devno, pcmd);
152 u32 irq_mask, irq_stat;
153 int rc;
154
155 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
156
157 /* better to add momery barrior here */
158 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
159 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
160
161 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
162 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
163 0, 10000);
164
165 /* clear IRQs */
166 writel(irq_mask, port + PORT_IRQ_STAT);
167 irq_stat >>= PORT_IRQ_RAW_SHIFT;
168
169 if (irq_stat & PORT_IRQ_COMPLETE)
170 rc = 0;
171 else {
172 /* force port into known state */
173 sil_init_port(port);
174 if (irq_stat & PORT_IRQ_ERROR)
175 rc = 1; /* error */
176 else
177 rc = 2; /* busy */
178 }
179
180 return rc;
181}
182
Peng Ma96f336c2019-11-19 06:17:40 +0000183static int sil_cmd_set_feature(struct sil_sata *sata)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000184{
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000185 struct sil_cmd_block cmdb, *pcmd = &cmdb;
186 struct sata_fis_d2h fis;
187 u8 udma_cap;
188 int ret;
189
190 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
191 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
192 pcmd->prb.fis.pm_port_c = (1 << 7);
193 pcmd->prb.fis.command = ATA_CMD_SET_FEATURES;
194 pcmd->prb.fis.features = SETFEATURES_XFER;
195
196 /* First check the device capablity */
197 udma_cap = (u8)(sata->udma & 0xff);
198 debug("udma_cap %02x\n", udma_cap);
199
200 if (udma_cap == ATA_UDMA6)
201 pcmd->prb.fis.sector_count = XFER_UDMA_6;
202 if (udma_cap == ATA_UDMA5)
203 pcmd->prb.fis.sector_count = XFER_UDMA_5;
204 if (udma_cap == ATA_UDMA4)
205 pcmd->prb.fis.sector_count = XFER_UDMA_4;
206 if (udma_cap == ATA_UDMA3)
207 pcmd->prb.fis.sector_count = XFER_UDMA_3;
208
Peng Ma96f336c2019-11-19 06:17:40 +0000209 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000210 if (ret) {
Peng Ma96f336c2019-11-19 06:17:40 +0000211 sil_read_fis(sata, 0, &fis);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000212 printf("Err: exe cmd(0x%x).\n",
213 readl(sata->port + PORT_SERROR));
214 sil_sata_dump_fis(&fis);
215 return 1;
216 }
217
218 return 0;
219}
220
Peng Ma96f336c2019-11-19 06:17:40 +0000221static void sil_sata_init_wcache(struct sil_sata *sata, u16 *id)
222{
223 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
224 sata->wcache = 1;
225 if (ata_id_has_flush(id))
226 sata->flush = 1;
227 if (ata_id_has_flush_ext(id))
228 sata->flush_ext = 1;
229}
230
231static void sil_sata_set_feature_by_id(struct sil_sata *sata, u16 *id)
232{
233#ifdef CONFIG_LBA48
234 /* Check if support LBA48 */
235 if (ata_id_has_lba48(id)) {
236 sata->lba48 = 1;
237 debug("Device supports LBA48\n");
238 } else {
239 debug("Device supports LBA28\n");
240 }
241#endif
242
243 sil_sata_init_wcache(sata, id);
244 sil_cmd_set_feature(sata);
245}
246
247static int sil_cmd_identify_device(struct sil_sata *sata, u16 *id)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000248{
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000249 struct sil_cmd_block cmdb, *pcmd = &cmdb;
250 struct sata_fis_d2h fis;
251 int ret;
252
253 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
254 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
255 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
256 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
257 pcmd->prb.fis.pm_port_c = (1 << 7);
258 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
259 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
260 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
261 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
262
Peng Ma96f336c2019-11-19 06:17:40 +0000263 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000264 if (ret) {
Peng Ma96f336c2019-11-19 06:17:40 +0000265 sil_read_fis(sata, 0, &fis);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000266 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
267 sil_sata_dump_fis(&fis);
268 return 1;
269 }
270 ata_swap_buf_le16(id, ATA_ID_WORDS);
271
272 return 0;
273}
274
Peng Ma96f336c2019-11-19 06:17:40 +0000275static int sil_cmd_soft_reset(struct sil_sata *sata)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000276{
277 struct sil_cmd_block cmdb, *pcmd = &cmdb;
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000278 struct sata_fis_d2h fis;
279 void *port = sata->port;
280 int ret;
281
282 /* put the port into known state */
283 if (sil_init_port(port)) {
Peng Ma96f336c2019-11-19 06:17:40 +0000284 printf("SRST: port %d not ready\n", sata->id);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000285 return 1;
286 }
287
288 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
289
290 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
291 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
292 pcmd->prb.fis.pm_port_c = 0xf;
293
Peng Ma96f336c2019-11-19 06:17:40 +0000294 ret = sil_exec_cmd(sata, &cmdb, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000295 if (ret) {
Peng Ma96f336c2019-11-19 06:17:40 +0000296 sil_read_fis(sata, 0, &fis);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000297 printf("SRST cmd error.\n");
298 sil_sata_dump_fis(&fis);
299 return 1;
300 }
301
302 return 0;
303}
304
Peng Ma96f336c2019-11-19 06:17:40 +0000305static ulong sil_sata_rw_cmd(struct sil_sata *sata, ulong start, ulong blkcnt,
306 u8 *buffer, int is_write)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000307{
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000308 struct sil_cmd_block cmdb, *pcmd = &cmdb;
309 struct sata_fis_d2h fis;
310 u64 block;
311 int ret;
312
313 block = (u64)start;
314 memset(pcmd, 0, sizeof(struct sil_cmd_block));
315 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
316 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
317 pcmd->prb.fis.pm_port_c = (1 << 7);
318 if (is_write) {
319 pcmd->prb.fis.command = ATA_CMD_WRITE;
320 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
321 } else {
322 pcmd->prb.fis.command = ATA_CMD_READ;
323 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
324 }
325
326 pcmd->prb.fis.device = ATA_LBA;
327 pcmd->prb.fis.device |= (block >> 24) & 0xf;
328 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
329 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
330 pcmd->prb.fis.lba_low = block & 0xff;
331 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
332
333 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
334 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
335 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
336
Peng Ma96f336c2019-11-19 06:17:40 +0000337 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000338 if (ret) {
Peng Ma96f336c2019-11-19 06:17:40 +0000339 sil_read_fis(sata, 0, &fis);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000340 printf("Err: rw cmd(0x%08x).\n",
341 readl(sata->port + PORT_SERROR));
342 sil_sata_dump_fis(&fis);
343 return 1;
344 }
345
346 return blkcnt;
347}
348
Peng Ma96f336c2019-11-19 06:17:40 +0000349static ulong sil_sata_rw_cmd_ext(struct sil_sata *sata, ulong start,
350 ulong blkcnt, u8 *buffer, int is_write)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000351{
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000352 struct sil_cmd_block cmdb, *pcmd = &cmdb;
353 struct sata_fis_d2h fis;
354 u64 block;
355 int ret;
356
357 block = (u64)start;
358 memset(pcmd, 0, sizeof(struct sil_cmd_block));
359 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
360 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
361 pcmd->prb.fis.pm_port_c = (1 << 7);
362 if (is_write) {
363 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
364 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
365 } else {
366 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
367 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
368 }
369
370 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
371 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
372 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
373 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
374 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
375 pcmd->prb.fis.lba_low = block & 0xff;
376 pcmd->prb.fis.device = ATA_LBA;
377 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
378 pcmd->prb.fis.sector_count = blkcnt & 0xff;
379
380 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
381 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
382 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
383
Peng Ma96f336c2019-11-19 06:17:40 +0000384 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000385 if (ret) {
Peng Ma96f336c2019-11-19 06:17:40 +0000386 sil_read_fis(sata, 0, &fis);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000387 printf("Err: rw ext cmd(0x%08x).\n",
388 readl(sata->port + PORT_SERROR));
389 sil_sata_dump_fis(&fis);
390 return 1;
391 }
392
393 return blkcnt;
394}
395
Peng Ma96f336c2019-11-19 06:17:40 +0000396static ulong sil_sata_rw_lba28(struct sil_sata *sata, ulong blknr,
397 lbaint_t blkcnt, const void *buffer,
398 int is_write)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000399{
400 ulong start, blks, max_blks;
401 u8 *addr;
402
403 start = blknr;
404 blks = blkcnt;
405 addr = (u8 *)buffer;
406
407 max_blks = ATA_MAX_SECTORS;
408 do {
409 if (blks > max_blks) {
Peng Ma96f336c2019-11-19 06:17:40 +0000410 sil_sata_rw_cmd(sata, start, max_blks, addr, is_write);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000411 start += max_blks;
412 blks -= max_blks;
413 addr += ATA_SECT_SIZE * max_blks;
414 } else {
Peng Ma96f336c2019-11-19 06:17:40 +0000415 sil_sata_rw_cmd(sata, start, blks, addr, is_write);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000416 start += blks;
417 blks = 0;
418 addr += ATA_SECT_SIZE * blks;
419 }
420 } while (blks != 0);
421
422 return blkcnt;
423}
424
Peng Ma96f336c2019-11-19 06:17:40 +0000425static ulong sil_sata_rw_lba48(struct sil_sata *sata, ulong blknr,
426 lbaint_t blkcnt, const void *buffer,
427 int is_write)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000428{
429 ulong start, blks, max_blks;
430 u8 *addr;
431
432 start = blknr;
433 blks = blkcnt;
434 addr = (u8 *)buffer;
435
436 max_blks = ATA_MAX_SECTORS_LBA48;
437 do {
438 if (blks > max_blks) {
Peng Ma96f336c2019-11-19 06:17:40 +0000439 sil_sata_rw_cmd_ext(sata, start, max_blks,
440 addr, is_write);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000441 start += max_blks;
442 blks -= max_blks;
443 addr += ATA_SECT_SIZE * max_blks;
444 } else {
Peng Ma96f336c2019-11-19 06:17:40 +0000445 sil_sata_rw_cmd_ext(sata, start, blks,
446 addr, is_write);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000447 start += blks;
448 blks = 0;
449 addr += ATA_SECT_SIZE * blks;
450 }
451 } while (blks != 0);
452
453 return blkcnt;
454}
455
Peng Ma96f336c2019-11-19 06:17:40 +0000456static void sil_sata_cmd_flush_cache(struct sil_sata *sata)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000457{
458 struct sil_cmd_block cmdb, *pcmd = &cmdb;
459
460 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
461 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
462 pcmd->prb.fis.pm_port_c = (1 << 7);
463 pcmd->prb.fis.command = ATA_CMD_FLUSH;
464
Peng Ma96f336c2019-11-19 06:17:40 +0000465 sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000466}
467
Peng Ma96f336c2019-11-19 06:17:40 +0000468static void sil_sata_cmd_flush_cache_ext(struct sil_sata *sata)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000469{
470 struct sil_cmd_block cmdb, *pcmd = &cmdb;
471
472 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
473 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
474 pcmd->prb.fis.pm_port_c = (1 << 7);
475 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
476
Peng Ma96f336c2019-11-19 06:17:40 +0000477 sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000478}
479
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000480/*
481 * SATA interface between low level driver and command layer
482 */
Peng Ma96f336c2019-11-19 06:17:40 +0000483#if !CONFIG_IS_ENABLED(BLK)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000484ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
485{
Peng Ma96f336c2019-11-19 06:17:40 +0000486 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
487#else
488static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
489 void *buffer)
490{
491 struct sil_sata_priv *priv = dev_get_platdata(dev);
492 int port_number = priv->port_num;
493 struct sil_sata *sata = priv->sil_sata_desc[port_number];
494#endif
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000495 ulong rc;
496
497 if (sata->lba48)
Peng Ma96f336c2019-11-19 06:17:40 +0000498 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, READ_CMD);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000499 else
Peng Ma96f336c2019-11-19 06:17:40 +0000500 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, READ_CMD);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000501
502 return rc;
503}
504
505/*
506 * SATA interface between low level driver and command layer
507 */
Peng Ma96f336c2019-11-19 06:17:40 +0000508#if !CONFIG_IS_ENABLED(BLK)
Tom Rini4835e132012-09-29 07:57:25 -0700509ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000510{
Peng Ma96f336c2019-11-19 06:17:40 +0000511 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
512#else
513ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
514 const void *buffer)
515{
516 struct sil_sata_priv *priv = dev_get_platdata(dev);
517 int port_number = priv->port_num;
518 struct sil_sata *sata = priv->sil_sata_desc[port_number];
519#endif
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000520 ulong rc;
521
522 if (sata->lba48) {
Peng Ma96f336c2019-11-19 06:17:40 +0000523 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, WRITE_CMD);
524 if (sata->wcache && sata->flush_ext)
525 sil_sata_cmd_flush_cache_ext(sata);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000526 } else {
Peng Ma96f336c2019-11-19 06:17:40 +0000527 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, WRITE_CMD);
528 if (sata->wcache && sata->flush)
529 sil_sata_cmd_flush_cache(sata);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000530 }
531
532 return rc;
533}
534
Peng Ma96f336c2019-11-19 06:17:40 +0000535#if !CONFIG_IS_ENABLED(BLK)
536static int sil_init_sata(int dev)
Nikita Kiryanovb9666d62014-11-21 12:47:23 +0200537{
Peng Ma96f336c2019-11-19 06:17:40 +0000538#else
539static int sil_init_sata(struct udevice *uc_dev, int dev)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000540{
Peng Ma96f336c2019-11-19 06:17:40 +0000541 struct sil_sata_priv *priv = dev_get_platdata(uc_dev);
542#endif
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000543 struct sil_sata *sata;
544 void *port;
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000545 u32 tmp;
Peng Ma96f336c2019-11-19 06:17:40 +0000546 int cnt;
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000547
Peng Ma96f336c2019-11-19 06:17:40 +0000548 printf("SATA#%d:\n", dev);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000549
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000550 port = (void *)sata_info.iobase[1] +
551 PORT_REGS_SIZE * (dev - sata_info.portbase);
552
553 /* Initial PHY setting */
554 writel(0x20c, port + PORT_PHY_CFG);
555
556 /* clear port RST */
557 tmp = readl(port + PORT_CTRL_STAT);
558 if (tmp & PORT_CS_PORT_RST) {
559 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
560 tmp = ata_wait_register(port + PORT_CTRL_STAT,
561 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
562 if (tmp & PORT_CS_PORT_RST)
563 printf("Err: Failed to clear port RST\n");
564 }
565
566 /* Check if device is present */
567 for (cnt = 0; cnt < 100; cnt++) {
568 tmp = readl(port + PORT_SSTATUS);
569 if ((tmp & 0xF) == 0x3)
570 break;
571 mdelay(1);
572 }
573
574 tmp = readl(port + PORT_SSTATUS);
575 if ((tmp & 0xf) != 0x3) {
576 printf(" (No RDY)\n");
577 return 1;
578 }
579
580 /* Wait for port ready */
581 tmp = ata_wait_register(port + PORT_CTRL_STAT,
582 PORT_CS_RDY, PORT_CS_RDY, 100);
583 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
584 printf("%d port not ready.\n", dev);
585 return 1;
586 }
587
588 /* configure port */
589 sil_config_port(port);
590
591 /* Reset port */
592 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
593 readl(port + PORT_CTRL_STAT);
594 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
595 PORT_CS_DEV_RST, 100);
596 if (tmp & PORT_CS_DEV_RST) {
597 printf("%d port reset failed.\n", dev);
598 return 1;
599 }
600
601 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
602 if (!sata) {
603 printf("%d no memory.\n", dev);
604 return 1;
605 }
606 memset((void *)sata, 0, sizeof(struct sil_sata));
607
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000608 /* Save the private struct to block device struct */
Peng Ma96f336c2019-11-19 06:17:40 +0000609#if !CONFIG_IS_ENABLED(BLK)
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000610 sata_dev_desc[dev].priv = (void *)sata;
Peng Ma96f336c2019-11-19 06:17:40 +0000611#else
612 priv->sil_sata_desc[dev] = sata;
613 priv->port_num = dev;
614#endif
615 sata->id = dev;
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000616 sata->port = port;
617 sata->devno = sata_info.devno;
618 sprintf(sata->name, "SATA#%d", dev);
Peng Ma96f336c2019-11-19 06:17:40 +0000619 sil_cmd_soft_reset(sata);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000620 tmp = readl(port + PORT_SSTATUS);
621 tmp = (tmp >> 4) & 0xf;
622 printf(" (%s)\n", sata_spd_string(tmp));
623
Peng Ma96f336c2019-11-19 06:17:40 +0000624 return 0;
625}
626
627#if !CONFIG_IS_ENABLED(BLK)
628/*
629 * SATA interface between low level driver and command layer
630 */
631int init_sata(int dev)
632{
633 static int init_done, idx;
634 pci_dev_t devno;
635 u16 word;
636
637 if (init_done == 1 && dev < sata_info.maxport)
638 goto init_start;
639
640 init_done = 1;
641
642 /* Find PCI device(s) */
643 devno = pci_find_devices(supported, idx++);
644 if (devno == -1)
645 return 1;
646
647 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
648
649 /* get the port count */
650 word &= 0xf;
651
652 sata_info.portbase = 0;
653 sata_info.maxport = sata_info.portbase + word;
654 sata_info.devno = devno;
655
656 /* Read out all BARs */
657 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
658 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
659 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
660 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
661
662 /* mask out the unused bits */
663 sata_info.iobase[0] &= 0xffffff80;
664 sata_info.iobase[1] &= 0xfffffc00;
665
666 /* Enable Bus Mastering and memory region */
667 pci_write_config_word(devno, PCI_COMMAND,
668 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
669
670 /* Check if mem accesses and Bus Mastering are enabled. */
671 pci_read_config_word(devno, PCI_COMMAND, &word);
672 if (!(word & PCI_COMMAND_MEMORY) ||
673 (!(word & PCI_COMMAND_MASTER))) {
674 printf("Error: Can not enable MEM access or Bus Mastering.\n");
675 debug("PCI command: %04x\n", word);
676 return 1;
677 }
678
679 /* GPIO off */
680 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
681 /* clear global reset & mask interrupts during initialization */
682 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
683
684init_start:
685 return sil_init_sata(dev);
686}
687
688int reset_sata(int dev)
689{
690 return 0;
691}
692
693/*
694 * SATA interface between low level driver and command layer
695 */
696int scan_sata(int dev)
697{
698 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
699#else
700static int scan_sata(struct udevice *blk_dev, int dev)
701{
702 struct blk_desc *desc = dev_get_uclass_platdata(blk_dev);
703 struct sil_sata_priv *priv = dev_get_platdata(blk_dev);
704 struct sil_sata *sata = priv->sil_sata_desc[dev];
705#endif
706 unsigned char serial[ATA_ID_SERNO_LEN + 1];
707 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
708 unsigned char product[ATA_ID_PROD_LEN + 1];
709 u16 *id;
710
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000711 id = (u16 *)malloc(ATA_ID_WORDS * 2);
712 if (!id) {
713 printf("Id malloc failed\n");
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000714 return 1;
715 }
Peng Ma96f336c2019-11-19 06:17:40 +0000716 sil_cmd_identify_device(sata, id);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000717
Peng Ma96f336c2019-11-19 06:17:40 +0000718 sil_sata_set_feature_by_id(sata, id);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000719
720 /* Serial number */
721 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000722
723 /* Firmware version */
724 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000725
726 /* Product model */
727 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000728
Peng Ma96f336c2019-11-19 06:17:40 +0000729#if !CONFIG_IS_ENABLED(BLK)
730 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
731 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
732 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000733 /* Totoal sectors */
734 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
Peng Ma96f336c2019-11-19 06:17:40 +0000735#ifdef CONFIG_LBA48
736 sata_dev_desc[dev].lba48 = sata->lba48;
737#endif
738#else
739 memcpy(desc->product, serial, sizeof(serial));
740 memcpy(desc->revision, firmware, sizeof(firmware));
741 memcpy(desc->vendor, product, sizeof(product));
742 desc->lba = ata_id_n_sectors(id);
743#ifdef CONFIG_LBA48
744 desc->lba48 = sata->lba48;
745#endif
746#endif
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000747
748#ifdef DEBUG
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000749 ata_dump_id(id);
750#endif
751 free((void *)id);
752
753 return 0;
754}
Peng Ma96f336c2019-11-19 06:17:40 +0000755
756#if CONFIG_IS_ENABLED(BLK)
757static const struct blk_ops sata_sil_blk_ops = {
758 .read = sata_read,
759 .write = sata_write,
760};
761
762U_BOOT_DRIVER(sata_sil_driver) = {
763 .name = "sata_sil_blk",
764 .id = UCLASS_BLK,
765 .ops = &sata_sil_blk_ops,
766 .platdata_auto_alloc_size = sizeof(struct sil_sata_priv),
767};
768
Peng Ma17dd2612019-12-04 10:36:42 +0000769static int sil_unbind_device(struct udevice *dev)
770{
771 int ret;
772
773 ret = device_remove(dev, DM_REMOVE_NORMAL);
774 if (ret)
775 return ret;
776
777 ret = device_unbind(dev);
778 if (ret)
779 return ret;
780
781 return 0;
782}
783
Peng Ma96f336c2019-11-19 06:17:40 +0000784static int sil_pci_probe(struct udevice *dev)
785{
786 struct udevice *blk;
Peng Ma17dd2612019-12-04 10:36:42 +0000787 int failed_number;
Peng Ma96f336c2019-11-19 06:17:40 +0000788 char sata_name[10];
789 pci_dev_t devno;
790 u16 word;
791 int ret;
792 int i;
793
Peng Ma17dd2612019-12-04 10:36:42 +0000794 failed_number = 0;
795
Peng Ma96f336c2019-11-19 06:17:40 +0000796 /* Get PCI device number */
797 devno = dm_pci_get_bdf(dev);
798 if (devno == -1)
799 return 1;
800
801 dm_pci_read_config16(dev, PCI_DEVICE_ID, &word);
802
803 /* get the port count */
804 word &= 0xf;
805
806 sata_info.portbase = 0;
807 sata_info.maxport = sata_info.portbase + word;
808 sata_info.devno = devno;
809
810 /* Read out all BARs */
811 sata_info.iobase[0] = (ulong)dm_pci_map_bar(dev,
812 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
813 sata_info.iobase[1] = (ulong)dm_pci_map_bar(dev,
814 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
815
816 /* mask out the unused bits */
817 sata_info.iobase[0] &= 0xffffff80;
818 sata_info.iobase[1] &= 0xfffffc00;
819
820 /* Enable Bus Mastering and memory region */
821 dm_pci_write_config16(dev, PCI_COMMAND,
822 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
823
824 /* Check if mem accesses and Bus Mastering are enabled. */
825 dm_pci_read_config16(dev, PCI_COMMAND, &word);
826 if (!(word & PCI_COMMAND_MEMORY) ||
827 (!(word & PCI_COMMAND_MASTER))) {
828 printf("Error: Can not enable MEM access or Bus Mastering.\n");
829 debug("PCI command: %04x\n", word);
830 return 1;
831 }
832
833 /* GPIO off */
834 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
835 /* clear global reset & mask interrupts during initialization */
836 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
837
838 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
839 snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i);
840 ret = blk_create_devicef(dev, "sata_sil_blk", sata_name,
841 IF_TYPE_SATA, -1, 512, 0, &blk);
842 if (ret) {
843 debug("Can't create device\n");
844 return ret;
845 }
846
847 ret = sil_init_sata(blk, i);
Peng Ma17dd2612019-12-04 10:36:42 +0000848 if (ret) {
849 ret = sil_unbind_device(blk);
850 if (ret)
851 return ret;
852
853 failed_number++;
854 continue;
855 }
Peng Ma96f336c2019-11-19 06:17:40 +0000856
857 ret = scan_sata(blk, i);
Peng Ma17dd2612019-12-04 10:36:42 +0000858 if (ret) {
859 ret = sil_unbind_device(blk);
860 if (ret)
861 return ret;
862
863 failed_number++;
864 continue;
865 }
866 }
867
868 if (failed_number == sata_info.maxport)
869 return -ENODEV;
870 else
871 return 0;
872}
873
874static int sil_pci_remove(struct udevice *dev)
875{
876 int i;
877 struct sil_sata *sata;
878 struct sil_sata_priv *priv;
879
880 priv = dev_get_priv(dev);
881
882 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
883 sata = priv->sil_sata_desc[i];
884 if (sata)
885 free(sata);
Peng Ma96f336c2019-11-19 06:17:40 +0000886 }
887
888 return 0;
889}
890
891static int sata_sil_scan(struct udevice *dev)
892{
893 /* Nothing to do here */
894
895 return 0;
896}
897
898struct sil_ops sata_sil_ops = {
899 .scan = sata_sil_scan,
900};
901
902static const struct udevice_id sil_pci_ids[] = {
903 { .compatible = "sil-pci-sample" },
904 { }
905};
906
907U_BOOT_DRIVER(sil_ahci_pci) = {
908 .name = "sil_ahci_pci",
909 .id = UCLASS_AHCI,
910 .of_match = sil_pci_ids,
911 .ops = &sata_sil_ops,
912 .probe = sil_pci_probe,
Peng Ma17dd2612019-12-04 10:36:42 +0000913 .remove = sil_pci_remove,
Peng Ma96f336c2019-11-19 06:17:40 +0000914 .priv_auto_alloc_size = sizeof(struct sil_sata_priv),
915};
916
917U_BOOT_PCI_DEVICE(sil_ahci_pci, supported);
918#endif