blob: dda712f42cb2a514ebcc58361ebb3335d8b0830b [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
Andre Przywara3e823502020-06-11 12:03:19 +010030#define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
Tang Yuantian4511ccc2011-10-07 19:26:58 +000031
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{
Simon Glassfa20e932020-12-03 16:55:20 -0700491 struct sil_sata_priv *priv = dev_get_plat(dev);
Peng Ma96f336c2019-11-19 06:17:40 +0000492 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{
Simon Glassfa20e932020-12-03 16:55:20 -0700516 struct sil_sata_priv *priv = dev_get_plat(dev);
Peng Ma96f336c2019-11-19 06:17:40 +0000517 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{
Simon Glassfa20e932020-12-03 16:55:20 -0700541 struct sil_sata_priv *priv = dev_get_plat(uc_dev);
Peng Ma96f336c2019-11-19 06:17:40 +0000542#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;
Andre Przywara3e823502020-06-11 12:03:19 +0100611 sata->devno = sata_info.devno;
Peng Ma96f336c2019-11-19 06:17:40 +0000612#else
613 priv->sil_sata_desc[dev] = sata;
614 priv->port_num = dev;
Andre Przywara3e823502020-06-11 12:03:19 +0100615 sata->devno = uc_dev->parent;
Peng Ma96f336c2019-11-19 06:17:40 +0000616#endif
617 sata->id = dev;
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000618 sata->port = port;
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000619 sprintf(sata->name, "SATA#%d", dev);
Peng Ma96f336c2019-11-19 06:17:40 +0000620 sil_cmd_soft_reset(sata);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000621 tmp = readl(port + PORT_SSTATUS);
622 tmp = (tmp >> 4) & 0xf;
623 printf(" (%s)\n", sata_spd_string(tmp));
624
Peng Ma96f336c2019-11-19 06:17:40 +0000625 return 0;
626}
627
628#if !CONFIG_IS_ENABLED(BLK)
629/*
630 * SATA interface between low level driver and command layer
631 */
632int init_sata(int dev)
633{
634 static int init_done, idx;
635 pci_dev_t devno;
636 u16 word;
637
638 if (init_done == 1 && dev < sata_info.maxport)
639 goto init_start;
640
641 init_done = 1;
642
643 /* Find PCI device(s) */
644 devno = pci_find_devices(supported, idx++);
645 if (devno == -1)
646 return 1;
647
648 pci_read_config_word(devno, PCI_DEVICE_ID, &word);
649
650 /* get the port count */
651 word &= 0xf;
652
653 sata_info.portbase = 0;
654 sata_info.maxport = sata_info.portbase + word;
655 sata_info.devno = devno;
656
657 /* Read out all BARs */
658 sata_info.iobase[0] = (ulong)pci_map_bar(devno,
659 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
660 sata_info.iobase[1] = (ulong)pci_map_bar(devno,
661 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
662
663 /* mask out the unused bits */
664 sata_info.iobase[0] &= 0xffffff80;
665 sata_info.iobase[1] &= 0xfffffc00;
666
667 /* Enable Bus Mastering and memory region */
668 pci_write_config_word(devno, PCI_COMMAND,
669 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
670
671 /* Check if mem accesses and Bus Mastering are enabled. */
672 pci_read_config_word(devno, PCI_COMMAND, &word);
673 if (!(word & PCI_COMMAND_MEMORY) ||
674 (!(word & PCI_COMMAND_MASTER))) {
675 printf("Error: Can not enable MEM access or Bus Mastering.\n");
676 debug("PCI command: %04x\n", word);
677 return 1;
678 }
679
680 /* GPIO off */
681 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
682 /* clear global reset & mask interrupts during initialization */
683 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
684
685init_start:
686 return sil_init_sata(dev);
687}
688
689int reset_sata(int dev)
690{
691 return 0;
692}
693
694/*
695 * SATA interface between low level driver and command layer
696 */
697int scan_sata(int dev)
698{
699 struct sil_sata *sata = (struct sil_sata *)sata_dev_desc[dev].priv;
700#else
701static int scan_sata(struct udevice *blk_dev, int dev)
702{
Simon Glass71fa5b42020-12-03 16:55:18 -0700703 struct blk_desc *desc = dev_get_uclass_plat(blk_dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700704 struct sil_sata_priv *priv = dev_get_plat(blk_dev);
Peng Ma96f336c2019-11-19 06:17:40 +0000705 struct sil_sata *sata = priv->sil_sata_desc[dev];
706#endif
707 unsigned char serial[ATA_ID_SERNO_LEN + 1];
708 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
709 unsigned char product[ATA_ID_PROD_LEN + 1];
710 u16 *id;
711
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000712 id = (u16 *)malloc(ATA_ID_WORDS * 2);
713 if (!id) {
714 printf("Id malloc failed\n");
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000715 return 1;
716 }
Peng Ma96f336c2019-11-19 06:17:40 +0000717 sil_cmd_identify_device(sata, id);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000718
Peng Ma96f336c2019-11-19 06:17:40 +0000719 sil_sata_set_feature_by_id(sata, id);
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000720
721 /* Serial number */
722 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000723
724 /* Firmware version */
725 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000726
727 /* Product model */
728 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000729
Peng Ma96f336c2019-11-19 06:17:40 +0000730#if !CONFIG_IS_ENABLED(BLK)
731 memcpy(sata_dev_desc[dev].product, serial, sizeof(serial));
732 memcpy(sata_dev_desc[dev].revision, firmware, sizeof(firmware));
733 memcpy(sata_dev_desc[dev].vendor, product, sizeof(product));
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000734 /* Totoal sectors */
735 sata_dev_desc[dev].lba = ata_id_n_sectors(id);
Peng Ma96f336c2019-11-19 06:17:40 +0000736#ifdef CONFIG_LBA48
737 sata_dev_desc[dev].lba48 = sata->lba48;
738#endif
739#else
740 memcpy(desc->product, serial, sizeof(serial));
741 memcpy(desc->revision, firmware, sizeof(firmware));
742 memcpy(desc->vendor, product, sizeof(product));
743 desc->lba = ata_id_n_sectors(id);
744#ifdef CONFIG_LBA48
745 desc->lba48 = sata->lba48;
746#endif
747#endif
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000748
749#ifdef DEBUG
Tang Yuantian4511ccc2011-10-07 19:26:58 +0000750 ata_dump_id(id);
751#endif
752 free((void *)id);
753
754 return 0;
755}
Peng Ma96f336c2019-11-19 06:17:40 +0000756
757#if CONFIG_IS_ENABLED(BLK)
758static const struct blk_ops sata_sil_blk_ops = {
759 .read = sata_read,
760 .write = sata_write,
761};
762
763U_BOOT_DRIVER(sata_sil_driver) = {
764 .name = "sata_sil_blk",
765 .id = UCLASS_BLK,
766 .ops = &sata_sil_blk_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700767 .plat_auto = sizeof(struct sil_sata_priv),
Peng Ma96f336c2019-11-19 06:17:40 +0000768};
769
Peng Ma17dd2612019-12-04 10:36:42 +0000770static int sil_unbind_device(struct udevice *dev)
771{
772 int ret;
773
774 ret = device_remove(dev, DM_REMOVE_NORMAL);
775 if (ret)
776 return ret;
777
778 ret = device_unbind(dev);
779 if (ret)
780 return ret;
781
782 return 0;
783}
784
Peng Ma96f336c2019-11-19 06:17:40 +0000785static int sil_pci_probe(struct udevice *dev)
786{
787 struct udevice *blk;
Peng Ma17dd2612019-12-04 10:36:42 +0000788 int failed_number;
Peng Ma96f336c2019-11-19 06:17:40 +0000789 char sata_name[10];
790 pci_dev_t devno;
791 u16 word;
792 int ret;
793 int i;
794
Peng Ma17dd2612019-12-04 10:36:42 +0000795 failed_number = 0;
796
Peng Ma96f336c2019-11-19 06:17:40 +0000797 /* Get PCI device number */
798 devno = dm_pci_get_bdf(dev);
799 if (devno == -1)
800 return 1;
801
802 dm_pci_read_config16(dev, PCI_DEVICE_ID, &word);
803
804 /* get the port count */
805 word &= 0xf;
806
807 sata_info.portbase = 0;
808 sata_info.maxport = sata_info.portbase + word;
809 sata_info.devno = devno;
810
811 /* Read out all BARs */
812 sata_info.iobase[0] = (ulong)dm_pci_map_bar(dev,
813 PCI_BASE_ADDRESS_0, PCI_REGION_MEM);
814 sata_info.iobase[1] = (ulong)dm_pci_map_bar(dev,
815 PCI_BASE_ADDRESS_2, PCI_REGION_MEM);
816
817 /* mask out the unused bits */
818 sata_info.iobase[0] &= 0xffffff80;
819 sata_info.iobase[1] &= 0xfffffc00;
820
821 /* Enable Bus Mastering and memory region */
822 dm_pci_write_config16(dev, PCI_COMMAND,
823 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
824
825 /* Check if mem accesses and Bus Mastering are enabled. */
826 dm_pci_read_config16(dev, PCI_COMMAND, &word);
827 if (!(word & PCI_COMMAND_MEMORY) ||
828 (!(word & PCI_COMMAND_MASTER))) {
829 printf("Error: Can not enable MEM access or Bus Mastering.\n");
830 debug("PCI command: %04x\n", word);
831 return 1;
832 }
833
834 /* GPIO off */
835 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
836 /* clear global reset & mask interrupts during initialization */
837 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
838
839 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
840 snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i);
841 ret = blk_create_devicef(dev, "sata_sil_blk", sata_name,
842 IF_TYPE_SATA, -1, 512, 0, &blk);
843 if (ret) {
844 debug("Can't create device\n");
845 return ret;
846 }
847
848 ret = sil_init_sata(blk, i);
Peng Ma17dd2612019-12-04 10:36:42 +0000849 if (ret) {
850 ret = sil_unbind_device(blk);
851 if (ret)
852 return ret;
853
854 failed_number++;
855 continue;
856 }
Peng Ma96f336c2019-11-19 06:17:40 +0000857
858 ret = scan_sata(blk, i);
Peng Ma17dd2612019-12-04 10:36:42 +0000859 if (ret) {
860 ret = sil_unbind_device(blk);
861 if (ret)
862 return ret;
863
864 failed_number++;
865 continue;
866 }
867 }
868
869 if (failed_number == sata_info.maxport)
870 return -ENODEV;
871 else
872 return 0;
873}
874
875static int sil_pci_remove(struct udevice *dev)
876{
877 int i;
878 struct sil_sata *sata;
879 struct sil_sata_priv *priv;
880
881 priv = dev_get_priv(dev);
882
883 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
884 sata = priv->sil_sata_desc[i];
885 if (sata)
886 free(sata);
Peng Ma96f336c2019-11-19 06:17:40 +0000887 }
888
889 return 0;
890}
891
892static int sata_sil_scan(struct udevice *dev)
893{
894 /* Nothing to do here */
895
896 return 0;
897}
898
899struct sil_ops sata_sil_ops = {
900 .scan = sata_sil_scan,
901};
902
903static const struct udevice_id sil_pci_ids[] = {
904 { .compatible = "sil-pci-sample" },
905 { }
906};
907
908U_BOOT_DRIVER(sil_ahci_pci) = {
909 .name = "sil_ahci_pci",
910 .id = UCLASS_AHCI,
911 .of_match = sil_pci_ids,
912 .ops = &sata_sil_ops,
913 .probe = sil_pci_probe,
Peng Ma17dd2612019-12-04 10:36:42 +0000914 .remove = sil_pci_remove,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700915 .priv_auto = sizeof(struct sil_sata_priv),
Peng Ma96f336c2019-11-19 06:17:40 +0000916};
917
918U_BOOT_PCI_DEVICE(sil_ahci_pci, supported);
919#endif