blob: 43a91a79120a399f06ee87e698b497c1cb664c14 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tang Yuantian83c484d2011-10-07 19:26:58 +00002/*
3 * Copyright (C) 2011 Freescale Semiconductor, Inc.
Peng Ma6b9d8a72019-11-19 06:17:40 +00004 * Copyright 2019 NXP
Tang Yuantian83c484d2011-10-07 19:26:58 +00005 * Author: Tang Yuantian <b29983@freescale.com>
Tang Yuantian83c484d2011-10-07 19:26:58 +00006 */
7
8#include <common.h>
Simon Glasse7b02782022-01-31 07:49:34 -07009#include <blk.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070010#include <cpu_func.h>
Simon Glasse7b02782022-01-31 07:49:34 -070011#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Tang Yuantian83c484d2011-10-07 19:26:58 +000013#include <pci.h>
14#include <command.h>
15#include <asm/byteorder.h>
16#include <malloc.h>
17#include <asm/io.h>
18#include <fis.h>
Pavel Herrmanne46a4352012-09-27 23:18:04 +000019#include <sata.h>
Tang Yuantian83c484d2011-10-07 19:26:58 +000020#include <libata.h>
Kim Phillips00caa7f2012-10-29 13:34:40 +000021#include <sata.h>
Peng Ma40cdf262019-12-04 10:36:42 +000022#include <dm/device-internal.h>
Simon Glasse7b02782022-01-31 07:49:34 -070023#include <linux/delay.h>
Peng Ma6b9d8a72019-11-19 06:17:40 +000024
Tang Yuantian83c484d2011-10-07 19:26:58 +000025#include "sata_sil.h"
26
Andre Przywara44a40422020-06-11 12:03:19 +010027#define virt_to_bus(devno, v) dm_pci_virt_to_mem(devno, (void *) (v))
Tang Yuantian83c484d2011-10-07 19:26:58 +000028
Peng Ma6b9d8a72019-11-19 06:17:40 +000029/* just compatible ahci_ops */
30struct sil_ops {
31 int *rev0;
32 int *rev1;
33 int (*scan)(struct udevice *dev);
34};
35
Tang Yuantian83c484d2011-10-07 19:26:58 +000036static struct sata_info sata_info;
37
38static struct pci_device_id supported[] = {
Peng Ma6b9d8a72019-11-19 06:17:40 +000039 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3131) },
40 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3132) },
41 { PCI_DEVICE(PCI_VENDOR_ID_SILICONIMAGE, PCI_DEVICE_ID_SIL3124) },
Tang Yuantian83c484d2011-10-07 19:26:58 +000042 {}
43};
44
45static void sil_sata_dump_fis(struct sata_fis_d2h *s)
46{
47 printf("Status FIS dump:\n");
48 printf("fis_type: %02x\n", s->fis_type);
49 printf("pm_port_i: %02x\n", s->pm_port_i);
50 printf("status: %02x\n", s->status);
51 printf("error: %02x\n", s->error);
52 printf("lba_low: %02x\n", s->lba_low);
53 printf("lba_mid: %02x\n", s->lba_mid);
54 printf("lba_high: %02x\n", s->lba_high);
55 printf("device: %02x\n", s->device);
56 printf("lba_low_exp: %02x\n", s->lba_low_exp);
57 printf("lba_mid_exp: %02x\n", s->lba_mid_exp);
58 printf("lba_high_exp: %02x\n", s->lba_high_exp);
59 printf("res1: %02x\n", s->res1);
60 printf("sector_count: %02x\n", s->sector_count);
61 printf("sector_count_exp: %02x\n", s->sector_count_exp);
62}
63
64static const char *sata_spd_string(unsigned int speed)
65{
66 static const char * const spd_str[] = {
67 "1.5 Gbps",
68 "3.0 Gbps",
69 "6.0 Gbps",
70 };
71
72 if ((speed - 1) > 2)
73 return "<unknown>";
74
75 return spd_str[speed - 1];
76}
77
78static u32 ata_wait_register(void *reg, u32 mask,
79 u32 val, int timeout_msec)
80{
81 u32 tmp;
82
83 tmp = readl(reg);
84 while ((tmp & mask) == val && timeout_msec > 0) {
85 mdelay(1);
86 timeout_msec--;
87 tmp = readl(reg);
88 }
89
90 return tmp;
91}
92
93static void sil_config_port(void *port)
94{
95 /* configure IRQ WoC */
96 writel(PORT_CS_IRQ_WOC, port + PORT_CTRL_CLR);
97
98 /* zero error counters. */
99 writew(0x8000, port + PORT_DECODE_ERR_THRESH);
100 writew(0x8000, port + PORT_CRC_ERR_THRESH);
101 writew(0x8000, port + PORT_HSHK_ERR_THRESH);
102 writew(0x0000, port + PORT_DECODE_ERR_CNT);
103 writew(0x0000, port + PORT_CRC_ERR_CNT);
104 writew(0x0000, port + PORT_HSHK_ERR_CNT);
105
106 /* always use 64bit activation */
107 writel(PORT_CS_32BIT_ACTV, port + PORT_CTRL_CLR);
108
109 /* clear port multiplier enable and resume bits */
110 writel(PORT_CS_PMP_EN | PORT_CS_PMP_RESUME, port + PORT_CTRL_CLR);
111}
112
113static int sil_init_port(void *port)
114{
115 u32 tmp;
116
117 writel(PORT_CS_INIT, port + PORT_CTRL_STAT);
118 ata_wait_register(port + PORT_CTRL_STAT,
119 PORT_CS_INIT, PORT_CS_INIT, 100);
120 tmp = ata_wait_register(port + PORT_CTRL_STAT,
121 PORT_CS_RDY, 0, 100);
122
123 if ((tmp & (PORT_CS_INIT | PORT_CS_RDY)) != PORT_CS_RDY)
124 return 1;
125
126 return 0;
127}
128
Peng Ma6b9d8a72019-11-19 06:17:40 +0000129static void sil_read_fis(struct sil_sata *sata, int tag,
130 struct sata_fis_d2h *fis)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000131{
Tang Yuantian83c484d2011-10-07 19:26:58 +0000132 void *port = sata->port;
133 struct sil_prb *prb;
134 int i;
135 u32 *src, *dst;
136
137 prb = port + PORT_LRAM + tag * PORT_LRAM_SLOT_SZ;
138 src = (u32 *)&prb->fis;
139 dst = (u32 *)fis;
140 for (i = 0; i < sizeof(struct sata_fis_h2d); i += 4)
141 *dst++ = readl(src++);
142}
143
Peng Ma6b9d8a72019-11-19 06:17:40 +0000144static int sil_exec_cmd(struct sil_sata *sata, struct sil_cmd_block *pcmd,
145 int tag)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000146{
Tang Yuantian83c484d2011-10-07 19:26:58 +0000147 void *port = sata->port;
148 u64 paddr = virt_to_bus(sata->devno, pcmd);
149 u32 irq_mask, irq_stat;
150 int rc;
151
152 writel(PORT_IRQ_COMPLETE | PORT_IRQ_ERROR, port + PORT_IRQ_ENABLE_CLR);
153
154 /* better to add momery barrior here */
155 writel((u32)paddr, port + PORT_CMD_ACTIVATE + tag * 8);
156 writel((u64)paddr >> 32, port + PORT_CMD_ACTIVATE + tag * 8 + 4);
157
158 irq_mask = (PORT_IRQ_COMPLETE | PORT_IRQ_ERROR) << PORT_IRQ_RAW_SHIFT;
159 irq_stat = ata_wait_register(port + PORT_IRQ_STAT, irq_mask,
160 0, 10000);
161
162 /* clear IRQs */
163 writel(irq_mask, port + PORT_IRQ_STAT);
164 irq_stat >>= PORT_IRQ_RAW_SHIFT;
165
166 if (irq_stat & PORT_IRQ_COMPLETE)
167 rc = 0;
168 else {
169 /* force port into known state */
170 sil_init_port(port);
171 if (irq_stat & PORT_IRQ_ERROR)
172 rc = 1; /* error */
173 else
174 rc = 2; /* busy */
175 }
176
177 return rc;
178}
179
Peng Ma6b9d8a72019-11-19 06:17:40 +0000180static int sil_cmd_set_feature(struct sil_sata *sata)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000181{
Tang Yuantian83c484d2011-10-07 19:26:58 +0000182 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
Peng Ma6b9d8a72019-11-19 06:17:40 +0000206 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000207 if (ret) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000208 sil_read_fis(sata, 0, &fis);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000209 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
Peng Ma6b9d8a72019-11-19 06:17:40 +0000218static void sil_sata_init_wcache(struct sil_sata *sata, u16 *id)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000219{
Peng Ma6b9d8a72019-11-19 06:17:40 +0000220 if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
221 sata->wcache = 1;
222 if (ata_id_has_flush(id))
223 sata->flush = 1;
224 if (ata_id_has_flush_ext(id))
225 sata->flush_ext = 1;
226}
227
228static void sil_sata_set_feature_by_id(struct sil_sata *sata, u16 *id)
229{
230#ifdef CONFIG_LBA48
231 /* Check if support LBA48 */
232 if (ata_id_has_lba48(id)) {
233 sata->lba48 = 1;
234 debug("Device supports LBA48\n");
235 } else {
236 debug("Device supports LBA28\n");
237 }
238#endif
239
240 sil_sata_init_wcache(sata, id);
241 sil_cmd_set_feature(sata);
242}
243
244static int sil_cmd_identify_device(struct sil_sata *sata, u16 *id)
245{
Tang Yuantian83c484d2011-10-07 19:26:58 +0000246 struct sil_cmd_block cmdb, *pcmd = &cmdb;
247 struct sata_fis_d2h fis;
248 int ret;
249
250 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
251 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
252 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
253 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
254 pcmd->prb.fis.pm_port_c = (1 << 7);
255 pcmd->prb.fis.command = ATA_CMD_ID_ATA;
256 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, id));
257 pcmd->sge.cnt = cpu_to_le32(sizeof(id[0]) * ATA_ID_WORDS);
258 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
259
Peng Ma6b9d8a72019-11-19 06:17:40 +0000260 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000261 if (ret) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000262 sil_read_fis(sata, 0, &fis);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000263 printf("Err: id cmd(0x%x).\n", readl(sata->port + PORT_SERROR));
264 sil_sata_dump_fis(&fis);
265 return 1;
266 }
267 ata_swap_buf_le16(id, ATA_ID_WORDS);
268
269 return 0;
270}
271
Peng Ma6b9d8a72019-11-19 06:17:40 +0000272static int sil_cmd_soft_reset(struct sil_sata *sata)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000273{
274 struct sil_cmd_block cmdb, *pcmd = &cmdb;
Tang Yuantian83c484d2011-10-07 19:26:58 +0000275 struct sata_fis_d2h fis;
276 void *port = sata->port;
277 int ret;
278
279 /* put the port into known state */
280 if (sil_init_port(port)) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000281 printf("SRST: port %d not ready\n", sata->id);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000282 return 1;
283 }
284
285 memset((void *)&cmdb, 0, sizeof(struct sil_cmd_block));
286
287 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_SRST);
288 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
289 pcmd->prb.fis.pm_port_c = 0xf;
290
Peng Ma6b9d8a72019-11-19 06:17:40 +0000291 ret = sil_exec_cmd(sata, &cmdb, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000292 if (ret) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000293 sil_read_fis(sata, 0, &fis);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000294 printf("SRST cmd error.\n");
295 sil_sata_dump_fis(&fis);
296 return 1;
297 }
298
299 return 0;
300}
301
Peng Ma6b9d8a72019-11-19 06:17:40 +0000302static ulong sil_sata_rw_cmd(struct sil_sata *sata, ulong start, ulong blkcnt,
303 u8 *buffer, int is_write)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000304{
Tang Yuantian83c484d2011-10-07 19:26:58 +0000305 struct sil_cmd_block cmdb, *pcmd = &cmdb;
306 struct sata_fis_d2h fis;
307 u64 block;
308 int ret;
309
310 block = (u64)start;
311 memset(pcmd, 0, sizeof(struct sil_cmd_block));
312 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
313 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
314 pcmd->prb.fis.pm_port_c = (1 << 7);
315 if (is_write) {
316 pcmd->prb.fis.command = ATA_CMD_WRITE;
317 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
318 } else {
319 pcmd->prb.fis.command = ATA_CMD_READ;
320 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
321 }
322
323 pcmd->prb.fis.device = ATA_LBA;
324 pcmd->prb.fis.device |= (block >> 24) & 0xf;
325 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
326 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
327 pcmd->prb.fis.lba_low = block & 0xff;
328 pcmd->prb.fis.sector_count = (u8)blkcnt & 0xff;
329
330 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
331 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
332 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
333
Peng Ma6b9d8a72019-11-19 06:17:40 +0000334 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000335 if (ret) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000336 sil_read_fis(sata, 0, &fis);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000337 printf("Err: rw cmd(0x%08x).\n",
338 readl(sata->port + PORT_SERROR));
339 sil_sata_dump_fis(&fis);
340 return 1;
341 }
342
343 return blkcnt;
344}
345
Peng Ma6b9d8a72019-11-19 06:17:40 +0000346static ulong sil_sata_rw_cmd_ext(struct sil_sata *sata, ulong start,
347 ulong blkcnt, u8 *buffer, int is_write)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000348{
Tang Yuantian83c484d2011-10-07 19:26:58 +0000349 struct sil_cmd_block cmdb, *pcmd = &cmdb;
350 struct sata_fis_d2h fis;
351 u64 block;
352 int ret;
353
354 block = (u64)start;
355 memset(pcmd, 0, sizeof(struct sil_cmd_block));
356 pcmd->prb.ctrl = cpu_to_le16(PRB_CTRL_PROTOCOL);
357 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
358 pcmd->prb.fis.pm_port_c = (1 << 7);
359 if (is_write) {
360 pcmd->prb.fis.command = ATA_CMD_WRITE_EXT;
361 pcmd->prb.prot = cpu_to_le16(PRB_PROT_WRITE);
362 } else {
363 pcmd->prb.fis.command = ATA_CMD_READ_EXT;
364 pcmd->prb.prot = cpu_to_le16(PRB_PROT_READ);
365 }
366
367 pcmd->prb.fis.lba_high_exp = (block >> 40) & 0xff;
368 pcmd->prb.fis.lba_mid_exp = (block >> 32) & 0xff;
369 pcmd->prb.fis.lba_low_exp = (block >> 24) & 0xff;
370 pcmd->prb.fis.lba_high = (block >> 16) & 0xff;
371 pcmd->prb.fis.lba_mid = (block >> 8) & 0xff;
372 pcmd->prb.fis.lba_low = block & 0xff;
373 pcmd->prb.fis.device = ATA_LBA;
374 pcmd->prb.fis.sector_count_exp = (blkcnt >> 8) & 0xff;
375 pcmd->prb.fis.sector_count = blkcnt & 0xff;
376
377 pcmd->sge.addr = cpu_to_le64(virt_to_bus(sata->devno, buffer));
378 pcmd->sge.cnt = cpu_to_le32(blkcnt * ATA_SECT_SIZE);
379 pcmd->sge.flags = cpu_to_le32(SGE_TRM);
380
Peng Ma6b9d8a72019-11-19 06:17:40 +0000381 ret = sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000382 if (ret) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000383 sil_read_fis(sata, 0, &fis);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000384 printf("Err: rw ext cmd(0x%08x).\n",
385 readl(sata->port + PORT_SERROR));
386 sil_sata_dump_fis(&fis);
387 return 1;
388 }
389
390 return blkcnt;
391}
392
Peng Ma6b9d8a72019-11-19 06:17:40 +0000393static ulong sil_sata_rw_lba28(struct sil_sata *sata, ulong blknr,
394 lbaint_t blkcnt, const void *buffer,
395 int is_write)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000396{
397 ulong start, blks, max_blks;
398 u8 *addr;
399
400 start = blknr;
401 blks = blkcnt;
402 addr = (u8 *)buffer;
403
404 max_blks = ATA_MAX_SECTORS;
405 do {
406 if (blks > max_blks) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000407 sil_sata_rw_cmd(sata, start, max_blks, addr, is_write);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000408 start += max_blks;
409 blks -= max_blks;
410 addr += ATA_SECT_SIZE * max_blks;
411 } else {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000412 sil_sata_rw_cmd(sata, start, blks, addr, is_write);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000413 start += blks;
414 blks = 0;
415 addr += ATA_SECT_SIZE * blks;
416 }
417 } while (blks != 0);
418
419 return blkcnt;
420}
421
Peng Ma6b9d8a72019-11-19 06:17:40 +0000422static ulong sil_sata_rw_lba48(struct sil_sata *sata, ulong blknr,
423 lbaint_t blkcnt, const void *buffer,
424 int is_write)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000425{
426 ulong start, blks, max_blks;
427 u8 *addr;
428
429 start = blknr;
430 blks = blkcnt;
431 addr = (u8 *)buffer;
432
433 max_blks = ATA_MAX_SECTORS_LBA48;
434 do {
435 if (blks > max_blks) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000436 sil_sata_rw_cmd_ext(sata, start, max_blks,
437 addr, is_write);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000438 start += max_blks;
439 blks -= max_blks;
440 addr += ATA_SECT_SIZE * max_blks;
441 } else {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000442 sil_sata_rw_cmd_ext(sata, start, blks,
443 addr, is_write);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000444 start += blks;
445 blks = 0;
446 addr += ATA_SECT_SIZE * blks;
447 }
448 } while (blks != 0);
449
450 return blkcnt;
451}
452
Peng Ma6b9d8a72019-11-19 06:17:40 +0000453static void sil_sata_cmd_flush_cache(struct sil_sata *sata)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000454{
455 struct sil_cmd_block cmdb, *pcmd = &cmdb;
456
457 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
458 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
459 pcmd->prb.fis.pm_port_c = (1 << 7);
460 pcmd->prb.fis.command = ATA_CMD_FLUSH;
461
Peng Ma6b9d8a72019-11-19 06:17:40 +0000462 sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000463}
464
Peng Ma6b9d8a72019-11-19 06:17:40 +0000465static void sil_sata_cmd_flush_cache_ext(struct sil_sata *sata)
Tang Yuantian83c484d2011-10-07 19:26:58 +0000466{
467 struct sil_cmd_block cmdb, *pcmd = &cmdb;
468
469 memset((void *)pcmd, 0, sizeof(struct sil_cmd_block));
470 pcmd->prb.fis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
471 pcmd->prb.fis.pm_port_c = (1 << 7);
472 pcmd->prb.fis.command = ATA_CMD_FLUSH_EXT;
473
Peng Ma6b9d8a72019-11-19 06:17:40 +0000474 sil_exec_cmd(sata, pcmd, 0);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000475}
476
477/*
478 * SATA interface between low level driver and command layer
479 */
Peng Ma6b9d8a72019-11-19 06:17:40 +0000480static ulong sata_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
481 void *buffer)
482{
Simon Glassc69cda22020-12-03 16:55:20 -0700483 struct sil_sata_priv *priv = dev_get_plat(dev);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000484 int port_number = priv->port_num;
485 struct sil_sata *sata = priv->sil_sata_desc[port_number];
Tang Yuantian83c484d2011-10-07 19:26:58 +0000486 ulong rc;
487
488 if (sata->lba48)
Peng Ma6b9d8a72019-11-19 06:17:40 +0000489 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, READ_CMD);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000490 else
Peng Ma6b9d8a72019-11-19 06:17:40 +0000491 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, READ_CMD);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000492
493 return rc;
494}
495
496/*
497 * SATA interface between low level driver and command layer
498 */
Peng Ma6b9d8a72019-11-19 06:17:40 +0000499ulong sata_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
500 const void *buffer)
501{
Simon Glassc69cda22020-12-03 16:55:20 -0700502 struct sil_sata_priv *priv = dev_get_plat(dev);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000503 int port_number = priv->port_num;
504 struct sil_sata *sata = priv->sil_sata_desc[port_number];
Tang Yuantian83c484d2011-10-07 19:26:58 +0000505 ulong rc;
506
507 if (sata->lba48) {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000508 rc = sil_sata_rw_lba48(sata, blknr, blkcnt, buffer, WRITE_CMD);
509 if (sata->wcache && sata->flush_ext)
510 sil_sata_cmd_flush_cache_ext(sata);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000511 } else {
Peng Ma6b9d8a72019-11-19 06:17:40 +0000512 rc = sil_sata_rw_lba28(sata, blknr, blkcnt, buffer, WRITE_CMD);
513 if (sata->wcache && sata->flush)
514 sil_sata_cmd_flush_cache(sata);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000515 }
516
517 return rc;
518}
519
Peng Ma6b9d8a72019-11-19 06:17:40 +0000520static int sil_init_sata(struct udevice *uc_dev, int dev)
Nikita Kiryanov10ee8ec2014-11-21 12:47:23 +0200521{
Simon Glassc69cda22020-12-03 16:55:20 -0700522 struct sil_sata_priv *priv = dev_get_plat(uc_dev);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000523 struct sil_sata *sata;
524 void *port;
Tang Yuantian83c484d2011-10-07 19:26:58 +0000525 u32 tmp;
Peng Ma6b9d8a72019-11-19 06:17:40 +0000526 int cnt;
Tang Yuantian83c484d2011-10-07 19:26:58 +0000527
Peng Ma6b9d8a72019-11-19 06:17:40 +0000528 printf("SATA#%d:\n", dev);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000529
Tang Yuantian83c484d2011-10-07 19:26:58 +0000530 port = (void *)sata_info.iobase[1] +
531 PORT_REGS_SIZE * (dev - sata_info.portbase);
532
533 /* Initial PHY setting */
534 writel(0x20c, port + PORT_PHY_CFG);
535
536 /* clear port RST */
537 tmp = readl(port + PORT_CTRL_STAT);
538 if (tmp & PORT_CS_PORT_RST) {
539 writel(PORT_CS_PORT_RST, port + PORT_CTRL_CLR);
540 tmp = ata_wait_register(port + PORT_CTRL_STAT,
541 PORT_CS_PORT_RST, PORT_CS_PORT_RST, 100);
542 if (tmp & PORT_CS_PORT_RST)
543 printf("Err: Failed to clear port RST\n");
544 }
545
546 /* Check if device is present */
547 for (cnt = 0; cnt < 100; cnt++) {
548 tmp = readl(port + PORT_SSTATUS);
549 if ((tmp & 0xF) == 0x3)
550 break;
551 mdelay(1);
552 }
553
554 tmp = readl(port + PORT_SSTATUS);
555 if ((tmp & 0xf) != 0x3) {
556 printf(" (No RDY)\n");
557 return 1;
558 }
559
560 /* Wait for port ready */
561 tmp = ata_wait_register(port + PORT_CTRL_STAT,
562 PORT_CS_RDY, PORT_CS_RDY, 100);
563 if ((tmp & PORT_CS_RDY) != PORT_CS_RDY) {
564 printf("%d port not ready.\n", dev);
565 return 1;
566 }
567
568 /* configure port */
569 sil_config_port(port);
570
571 /* Reset port */
572 writel(PORT_CS_DEV_RST, port + PORT_CTRL_STAT);
573 readl(port + PORT_CTRL_STAT);
574 tmp = ata_wait_register(port + PORT_CTRL_STAT, PORT_CS_DEV_RST,
575 PORT_CS_DEV_RST, 100);
576 if (tmp & PORT_CS_DEV_RST) {
577 printf("%d port reset failed.\n", dev);
578 return 1;
579 }
580
581 sata = (struct sil_sata *)malloc(sizeof(struct sil_sata));
582 if (!sata) {
583 printf("%d no memory.\n", dev);
584 return 1;
585 }
586 memset((void *)sata, 0, sizeof(struct sil_sata));
587
Tang Yuantian83c484d2011-10-07 19:26:58 +0000588 /* Save the private struct to block device struct */
Peng Ma6b9d8a72019-11-19 06:17:40 +0000589 priv->sil_sata_desc[dev] = sata;
590 priv->port_num = dev;
Andre Przywara44a40422020-06-11 12:03:19 +0100591 sata->devno = uc_dev->parent;
Peng Ma6b9d8a72019-11-19 06:17:40 +0000592 sata->id = dev;
Tang Yuantian83c484d2011-10-07 19:26:58 +0000593 sata->port = port;
Tang Yuantian83c484d2011-10-07 19:26:58 +0000594 sprintf(sata->name, "SATA#%d", dev);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000595 sil_cmd_soft_reset(sata);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000596 tmp = readl(port + PORT_SSTATUS);
597 tmp = (tmp >> 4) & 0xf;
598 printf(" (%s)\n", sata_spd_string(tmp));
599
Peng Ma6b9d8a72019-11-19 06:17:40 +0000600 return 0;
601}
602
Peng Ma6b9d8a72019-11-19 06:17:40 +0000603static int scan_sata(struct udevice *blk_dev, int dev)
604{
Simon Glasscaa4daa2020-12-03 16:55:18 -0700605 struct blk_desc *desc = dev_get_uclass_plat(blk_dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700606 struct sil_sata_priv *priv = dev_get_plat(blk_dev);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000607 struct sil_sata *sata = priv->sil_sata_desc[dev];
Peng Ma6b9d8a72019-11-19 06:17:40 +0000608 unsigned char serial[ATA_ID_SERNO_LEN + 1];
609 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
610 unsigned char product[ATA_ID_PROD_LEN + 1];
611 u16 *id;
612
Tang Yuantian83c484d2011-10-07 19:26:58 +0000613 id = (u16 *)malloc(ATA_ID_WORDS * 2);
614 if (!id) {
615 printf("Id malloc failed\n");
Tang Yuantian83c484d2011-10-07 19:26:58 +0000616 return 1;
617 }
Peng Ma6b9d8a72019-11-19 06:17:40 +0000618 sil_cmd_identify_device(sata, id);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000619
Peng Ma6b9d8a72019-11-19 06:17:40 +0000620 sil_sata_set_feature_by_id(sata, id);
Tang Yuantian83c484d2011-10-07 19:26:58 +0000621
622 /* Serial number */
623 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
Tang Yuantian83c484d2011-10-07 19:26:58 +0000624
625 /* Firmware version */
626 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
Tang Yuantian83c484d2011-10-07 19:26:58 +0000627
628 /* Product model */
629 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
Tang Yuantian83c484d2011-10-07 19:26:58 +0000630
Peng Ma6b9d8a72019-11-19 06:17:40 +0000631 memcpy(desc->product, serial, sizeof(serial));
632 memcpy(desc->revision, firmware, sizeof(firmware));
633 memcpy(desc->vendor, product, sizeof(product));
634 desc->lba = ata_id_n_sectors(id);
635#ifdef CONFIG_LBA48
636 desc->lba48 = sata->lba48;
637#endif
Tang Yuantian83c484d2011-10-07 19:26:58 +0000638
639#ifdef DEBUG
Tang Yuantian83c484d2011-10-07 19:26:58 +0000640 ata_dump_id(id);
641#endif
642 free((void *)id);
643
644 return 0;
645}
Peng Ma6b9d8a72019-11-19 06:17:40 +0000646
Peng Ma6b9d8a72019-11-19 06:17:40 +0000647static const struct blk_ops sata_sil_blk_ops = {
648 .read = sata_read,
649 .write = sata_write,
650};
651
652U_BOOT_DRIVER(sata_sil_driver) = {
653 .name = "sata_sil_blk",
654 .id = UCLASS_BLK,
655 .ops = &sata_sil_blk_ops,
Simon Glasscaa4daa2020-12-03 16:55:18 -0700656 .plat_auto = sizeof(struct sil_sata_priv),
Peng Ma6b9d8a72019-11-19 06:17:40 +0000657};
658
Peng Ma40cdf262019-12-04 10:36:42 +0000659static int sil_unbind_device(struct udevice *dev)
660{
661 int ret;
662
663 ret = device_remove(dev, DM_REMOVE_NORMAL);
664 if (ret)
665 return ret;
666
667 ret = device_unbind(dev);
668 if (ret)
669 return ret;
670
671 return 0;
672}
673
Peng Ma6b9d8a72019-11-19 06:17:40 +0000674static int sil_pci_probe(struct udevice *dev)
675{
676 struct udevice *blk;
Peng Ma40cdf262019-12-04 10:36:42 +0000677 int failed_number;
Peng Ma6b9d8a72019-11-19 06:17:40 +0000678 char sata_name[10];
679 pci_dev_t devno;
680 u16 word;
681 int ret;
682 int i;
683
Peng Ma40cdf262019-12-04 10:36:42 +0000684 failed_number = 0;
685
Peng Ma6b9d8a72019-11-19 06:17:40 +0000686 /* Get PCI device number */
687 devno = dm_pci_get_bdf(dev);
688 if (devno == -1)
689 return 1;
690
691 dm_pci_read_config16(dev, PCI_DEVICE_ID, &word);
692
693 /* get the port count */
694 word &= 0xf;
695
696 sata_info.portbase = 0;
697 sata_info.maxport = sata_info.portbase + word;
698 sata_info.devno = devno;
699
700 /* Read out all BARs */
701 sata_info.iobase[0] = (ulong)dm_pci_map_bar(dev,
Andrew Scull2635e3b2022-04-21 16:11:13 +0000702 PCI_BASE_ADDRESS_0, 0, 0, PCI_REGION_TYPE,
703 PCI_REGION_MEM);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000704 sata_info.iobase[1] = (ulong)dm_pci_map_bar(dev,
Andrew Scull2635e3b2022-04-21 16:11:13 +0000705 PCI_BASE_ADDRESS_2, 0, 0, PCI_REGION_TYPE,
706 PCI_REGION_MEM);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000707
708 /* mask out the unused bits */
709 sata_info.iobase[0] &= 0xffffff80;
710 sata_info.iobase[1] &= 0xfffffc00;
711
712 /* Enable Bus Mastering and memory region */
713 dm_pci_write_config16(dev, PCI_COMMAND,
714 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
715
716 /* Check if mem accesses and Bus Mastering are enabled. */
717 dm_pci_read_config16(dev, PCI_COMMAND, &word);
718 if (!(word & PCI_COMMAND_MEMORY) ||
719 (!(word & PCI_COMMAND_MASTER))) {
720 printf("Error: Can not enable MEM access or Bus Mastering.\n");
721 debug("PCI command: %04x\n", word);
722 return 1;
723 }
724
725 /* GPIO off */
726 writel(0, (void *)(sata_info.iobase[0] + HOST_FLASH_CMD));
727 /* clear global reset & mask interrupts during initialization */
728 writel(0, (void *)(sata_info.iobase[0] + HOST_CTRL));
729
730 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
731 snprintf(sata_name, sizeof(sata_name), "sil_sata%d", i);
732 ret = blk_create_devicef(dev, "sata_sil_blk", sata_name,
Bin Meng7020b2e2023-09-26 16:43:31 +0800733 UCLASS_AHCI, -1, DEFAULT_BLKSZ,
734 0, &blk);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000735 if (ret) {
736 debug("Can't create device\n");
737 return ret;
738 }
739
740 ret = sil_init_sata(blk, i);
Peng Ma40cdf262019-12-04 10:36:42 +0000741 if (ret) {
742 ret = sil_unbind_device(blk);
743 if (ret)
744 return ret;
745
746 failed_number++;
747 continue;
748 }
Peng Ma6b9d8a72019-11-19 06:17:40 +0000749
750 ret = scan_sata(blk, i);
Peng Ma40cdf262019-12-04 10:36:42 +0000751 if (ret) {
752 ret = sil_unbind_device(blk);
753 if (ret)
754 return ret;
755
756 failed_number++;
757 continue;
758 }
AKASHI Takahiroc662edd2022-03-08 20:36:43 +0900759
760 ret = device_probe(dev);
761 if (ret < 0) {
762 debug("Probing %s failed (%d)\n", dev->name, ret);
763 ret = sil_unbind_device(blk);
764 device_unbind(dev);
765 if (ret)
766 return ret;
767
768 failed_number++;
769 continue;
770 }
Peng Ma40cdf262019-12-04 10:36:42 +0000771 }
772
773 if (failed_number == sata_info.maxport)
774 return -ENODEV;
775 else
776 return 0;
777}
778
779static int sil_pci_remove(struct udevice *dev)
780{
781 int i;
782 struct sil_sata *sata;
783 struct sil_sata_priv *priv;
784
785 priv = dev_get_priv(dev);
786
787 for (i = sata_info.portbase; i < sata_info.maxport; i++) {
788 sata = priv->sil_sata_desc[i];
789 if (sata)
790 free(sata);
Peng Ma6b9d8a72019-11-19 06:17:40 +0000791 }
792
793 return 0;
794}
795
796static int sata_sil_scan(struct udevice *dev)
797{
798 /* Nothing to do here */
799
800 return 0;
801}
802
803struct sil_ops sata_sil_ops = {
804 .scan = sata_sil_scan,
805};
806
807static const struct udevice_id sil_pci_ids[] = {
808 { .compatible = "sil-pci-sample" },
809 { }
810};
811
812U_BOOT_DRIVER(sil_ahci_pci) = {
813 .name = "sil_ahci_pci",
814 .id = UCLASS_AHCI,
815 .of_match = sil_pci_ids,
816 .ops = &sata_sil_ops,
817 .probe = sil_pci_probe,
Peng Ma40cdf262019-12-04 10:36:42 +0000818 .remove = sil_pci_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700819 .priv_auto = sizeof(struct sil_sata_priv),
Peng Ma6b9d8a72019-11-19 06:17:40 +0000820};
821
822U_BOOT_PCI_DEVICE(sil_ahci_pci, supported);