blob: 7f51fb693e96cffb361b5050be3779b03d2a2ad5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tor Krillf65e82b2015-12-03 12:38:02 +01002/*
3 * Copyright (C) Excito Elektronik i Skåne AB, 2010.
4 * Author: Tor Krill <tor@excito.com>
5 *
Stefan Roesee85955c2019-03-11 13:29:20 +01006 * Copyright (C) 2015, 2019 Stefan Roese <sr@denx.de>
Tor Krillf65e82b2015-12-03 12:38:02 +01007 */
8
9/*
10 * This driver supports the SATA controller of some Mavell SoC's.
11 * Here a (most likely incomplete) list of the supported SoC's:
12 * - Kirkwood
13 * - Armada 370
14 * - Armada XP
15 *
16 * This driver implementation is an alternative to the already available
17 * driver via the "ide" commands interface (drivers/block/mvsata_ide.c).
18 * But this driver only supports PIO mode and as this new driver also
19 * supports transfer via DMA, its much faster.
20 *
21 * Please note, that the newer SoC's (e.g. Armada 38x) are not supported
22 * by this driver. As they have an AHCI compatible SATA controller
23 * integrated.
24 */
25
26/*
27 * TODO:
28 * Better error recovery
29 * No support for using PRDs (Thus max 64KB transfers)
30 * No NCQ support
31 * No port multiplier support
32 */
33
34#include <common.h>
Stefan Roesee85955c2019-03-11 13:29:20 +010035#include <ahci.h>
Simon Glass63334482019-11-14 12:57:39 -070036#include <cpu_func.h>
Stefan Roesee85955c2019-03-11 13:29:20 +010037#include <dm.h>
Simon Glass274e0b02020-05-10 11:39:56 -060038#include <asm/cache.h>
Stefan Roesee85955c2019-03-11 13:29:20 +010039#include <dm/device-internal.h>
40#include <dm/lists.h>
Tor Krillf65e82b2015-12-03 12:38:02 +010041#include <fis.h>
42#include <libata.h>
43#include <malloc.h>
44#include <sata.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090045#include <linux/errno.h>
Tor Krillf65e82b2015-12-03 12:38:02 +010046#include <asm/io.h>
47#include <linux/mbus.h>
48
Michael Walle692b9782019-04-03 23:28:29 +020049#include <asm/arch/soc.h>
Trevor Woernerbb7ab072020-05-06 08:02:40 -040050#if defined(CONFIG_ARCH_KIRKWOOD)
Tor Krillf65e82b2015-12-03 12:38:02 +010051#define SATAHC_BASE KW_SATA_BASE
52#else
Tor Krillf65e82b2015-12-03 12:38:02 +010053#define SATAHC_BASE MVEBU_AXP_SATA_BASE
54#endif
55
56#define SATA0_BASE (SATAHC_BASE + 0x2000)
57#define SATA1_BASE (SATAHC_BASE + 0x4000)
58
59/* EDMA registers */
60#define EDMA_CFG 0x000
61#define EDMA_CFG_NCQ (1 << 5)
62#define EDMA_CFG_EQUE (1 << 9)
63#define EDMA_TIMER 0x004
64#define EDMA_IECR 0x008
65#define EDMA_IEMR 0x00c
66#define EDMA_RQBA_HI 0x010
67#define EDMA_RQIPR 0x014
68#define EDMA_RQIPR_IPMASK (0x1f << 5)
69#define EDMA_RQIPR_IPSHIFT 5
70#define EDMA_RQOPR 0x018
71#define EDMA_RQOPR_OPMASK (0x1f << 5)
72#define EDMA_RQOPR_OPSHIFT 5
73#define EDMA_RSBA_HI 0x01c
74#define EDMA_RSIPR 0x020
75#define EDMA_RSIPR_IPMASK (0x1f << 3)
76#define EDMA_RSIPR_IPSHIFT 3
77#define EDMA_RSOPR 0x024
78#define EDMA_RSOPR_OPMASK (0x1f << 3)
79#define EDMA_RSOPR_OPSHIFT 3
80#define EDMA_CMD 0x028
81#define EDMA_CMD_ENEDMA (0x01 << 0)
82#define EDMA_CMD_DISEDMA (0x01 << 1)
83#define EDMA_CMD_ATARST (0x01 << 2)
84#define EDMA_CMD_FREEZE (0x01 << 4)
85#define EDMA_TEST_CTL 0x02c
86#define EDMA_STATUS 0x030
87#define EDMA_IORTO 0x034
88#define EDMA_CDTR 0x040
89#define EDMA_HLTCND 0x060
90#define EDMA_NTSR 0x094
91
92/* Basic DMA registers */
93#define BDMA_CMD 0x224
94#define BDMA_STATUS 0x228
95#define BDMA_DTLB 0x22c
96#define BDMA_DTHB 0x230
97#define BDMA_DRL 0x234
98#define BDMA_DRH 0x238
99
100/* SATA Interface registers */
101#define SIR_ICFG 0x050
102#define SIR_CFG_GEN2EN (0x1 << 7)
103#define SIR_PLL_CFG 0x054
104#define SIR_SSTATUS 0x300
105#define SSTATUS_DET_MASK (0x0f << 0)
106#define SIR_SERROR 0x304
107#define SIR_SCONTROL 0x308
108#define SIR_SCONTROL_DETEN (0x01 << 0)
109#define SIR_LTMODE 0x30c
110#define SIR_LTMODE_NELBE (0x01 << 7)
111#define SIR_PHYMODE3 0x310
112#define SIR_PHYMODE4 0x314
113#define SIR_PHYMODE1 0x32c
114#define SIR_PHYMODE2 0x330
115#define SIR_BIST_CTRL 0x334
116#define SIR_BIST_DW1 0x338
117#define SIR_BIST_DW2 0x33c
118#define SIR_SERR_IRQ_MASK 0x340
119#define SIR_SATA_IFCTRL 0x344
120#define SIR_SATA_TESTCTRL 0x348
121#define SIR_SATA_IFSTATUS 0x34c
122#define SIR_VEND_UNIQ 0x35c
123#define SIR_FIS_CFG 0x360
124#define SIR_FIS_IRQ_CAUSE 0x364
125#define SIR_FIS_IRQ_MASK 0x368
126#define SIR_FIS_DWORD0 0x370
127#define SIR_FIS_DWORD1 0x374
128#define SIR_FIS_DWORD2 0x378
129#define SIR_FIS_DWORD3 0x37c
130#define SIR_FIS_DWORD4 0x380
131#define SIR_FIS_DWORD5 0x384
132#define SIR_FIS_DWORD6 0x388
133#define SIR_PHYM9_GEN2 0x398
134#define SIR_PHYM9_GEN1 0x39c
135#define SIR_PHY_CFG 0x3a0
136#define SIR_PHYCTL 0x3a4
137#define SIR_PHYM10 0x3a8
138#define SIR_PHYM12 0x3b0
139
140/* Shadow registers */
141#define PIO_DATA 0x100
142#define PIO_ERR_FEATURES 0x104
143#define PIO_SECTOR_COUNT 0x108
144#define PIO_LBA_LOW 0x10c
145#define PIO_LBA_MID 0x110
146#define PIO_LBA_HI 0x114
147#define PIO_DEVICE 0x118
148#define PIO_CMD_STATUS 0x11c
149#define PIO_STATUS_ERR (0x01 << 0)
150#define PIO_STATUS_DRQ (0x01 << 3)
151#define PIO_STATUS_DF (0x01 << 5)
152#define PIO_STATUS_DRDY (0x01 << 6)
153#define PIO_STATUS_BSY (0x01 << 7)
154#define PIO_CTRL_ALTSTAT 0x120
155
156/* SATAHC arbiter registers */
157#define SATAHC_CFG 0x000
158#define SATAHC_RQOP 0x004
159#define SATAHC_RQIP 0x008
160#define SATAHC_ICT 0x00c
161#define SATAHC_ITT 0x010
162#define SATAHC_ICR 0x014
163#define SATAHC_ICR_PORT0 (0x01 << 0)
164#define SATAHC_ICR_PORT1 (0x01 << 1)
165#define SATAHC_MIC 0x020
166#define SATAHC_MIM 0x024
167#define SATAHC_LED_CFG 0x02c
168
169#define REQUEST_QUEUE_SIZE 32
170#define RESPONSE_QUEUE_SIZE REQUEST_QUEUE_SIZE
171
172struct crqb {
173 u32 dtb_low; /* DW0 */
174 u32 dtb_high; /* DW1 */
175 u32 control_flags; /* DW2 */
176 u32 drb_count; /* DW3 */
177 u32 ata_cmd_feat; /* DW4 */
178 u32 ata_addr; /* DW5 */
179 u32 ata_addr_exp; /* DW6 */
180 u32 ata_sect_count; /* DW7 */
181};
182
183#define CRQB_ALIGN 0x400
184
185#define CRQB_CNTRLFLAGS_DIR (0x01 << 0)
186#define CRQB_CNTRLFLAGS_DQTAGMASK (0x1f << 1)
187#define CRQB_CNTRLFLAGS_DQTAGSHIFT 1
188#define CRQB_CNTRLFLAGS_PMPORTMASK (0x0f << 12)
189#define CRQB_CNTRLFLAGS_PMPORTSHIFT 12
190#define CRQB_CNTRLFLAGS_PRDMODE (0x01 << 16)
191#define CRQB_CNTRLFLAGS_HQTAGMASK (0x1f << 17)
192#define CRQB_CNTRLFLAGS_HQTAGSHIFT 17
193
194#define CRQB_CMDFEAT_CMDMASK (0xff << 16)
195#define CRQB_CMDFEAT_CMDSHIFT 16
196#define CRQB_CMDFEAT_FEATMASK (0xff << 16)
197#define CRQB_CMDFEAT_FEATSHIFT 24
198
199#define CRQB_ADDR_LBA_LOWMASK (0xff << 0)
200#define CRQB_ADDR_LBA_LOWSHIFT 0
201#define CRQB_ADDR_LBA_MIDMASK (0xff << 8)
202#define CRQB_ADDR_LBA_MIDSHIFT 8
203#define CRQB_ADDR_LBA_HIGHMASK (0xff << 16)
204#define CRQB_ADDR_LBA_HIGHSHIFT 16
205#define CRQB_ADDR_DEVICE_MASK (0xff << 24)
206#define CRQB_ADDR_DEVICE_SHIFT 24
207
208#define CRQB_ADDR_LBA_LOW_EXP_MASK (0xff << 0)
209#define CRQB_ADDR_LBA_LOW_EXP_SHIFT 0
210#define CRQB_ADDR_LBA_MID_EXP_MASK (0xff << 8)
211#define CRQB_ADDR_LBA_MID_EXP_SHIFT 8
212#define CRQB_ADDR_LBA_HIGH_EXP_MASK (0xff << 16)
213#define CRQB_ADDR_LBA_HIGH_EXP_SHIFT 16
214#define CRQB_ADDR_FEATURE_EXP_MASK (0xff << 24)
215#define CRQB_ADDR_FEATURE_EXP_SHIFT 24
216
217#define CRQB_SECTCOUNT_COUNT_MASK (0xff << 0)
218#define CRQB_SECTCOUNT_COUNT_SHIFT 0
219#define CRQB_SECTCOUNT_COUNT_EXP_MASK (0xff << 8)
220#define CRQB_SECTCOUNT_COUNT_EXP_SHIFT 8
221
Michael Walle692b9782019-04-03 23:28:29 +0200222#define MVSATA_WIN_CONTROL(w) (SATAHC_BASE + 0x30 + ((w) << 4))
223#define MVSATA_WIN_BASE(w) (SATAHC_BASE + 0x34 + ((w) << 4))
Tor Krillf65e82b2015-12-03 12:38:02 +0100224
225struct eprd {
226 u32 phyaddr_low;
227 u32 bytecount_eot;
228 u32 phyaddr_hi;
229 u32 reserved;
230};
231
232#define EPRD_PHYADDR_MASK 0xfffffffe
233#define EPRD_BYTECOUNT_MASK 0x0000ffff
234#define EPRD_EOT (0x01 << 31)
235
236struct crpb {
237 u32 id;
238 u32 flags;
239 u32 timestamp;
240};
241
242#define CRPB_ALIGN 0x100
243
244#define READ_CMD 0
245#define WRITE_CMD 1
246
247/*
248 * Since we don't use PRDs yet max transfer size
249 * is 64KB
250 */
251#define MV_ATA_MAX_SECTORS (65535 / ATA_SECT_SIZE)
252
253/* Keep track if hw is initialized or not */
254static u32 hw_init;
255
256struct mv_priv {
257 char name[12];
258 u32 link;
259 u32 regbase;
260 u32 queue_depth;
261 u16 pio;
262 u16 mwdma;
263 u16 udma;
Stefan Roesee85955c2019-03-11 13:29:20 +0100264 int dev_nr;
Tor Krillf65e82b2015-12-03 12:38:02 +0100265
266 void *crqb_alloc;
267 struct crqb *request;
268
269 void *crpb_alloc;
270 struct crpb *response;
271};
272
273static int ata_wait_register(u32 *addr, u32 mask, u32 val, u32 timeout_msec)
274{
275 ulong start;
276
277 start = get_timer(0);
278 do {
279 if ((in_le32(addr) & mask) == val)
280 return 0;
281 } while (get_timer(start) < timeout_msec);
282
283 return -ETIMEDOUT;
284}
285
286/* Cut from sata_mv in linux kernel */
Stefan Roesee85955c2019-03-11 13:29:20 +0100287static int mv_stop_edma_engine(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100288{
Stefan Roesee85955c2019-03-11 13:29:20 +0100289 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100290 int i;
291
292 /* Disable eDMA. The disable bit auto clears. */
293 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_DISEDMA);
294
295 /* Wait for the chip to confirm eDMA is off. */
296 for (i = 10000; i > 0; i--) {
297 u32 reg = in_le32(priv->regbase + EDMA_CMD);
298 if (!(reg & EDMA_CMD_ENEDMA)) {
299 debug("EDMA stop on port %d succesful\n", port);
300 return 0;
301 }
302 udelay(10);
303 }
304 debug("EDMA stop on port %d failed\n", port);
305 return -1;
306}
307
Stefan Roesee85955c2019-03-11 13:29:20 +0100308static int mv_start_edma_engine(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100309{
Stefan Roesee85955c2019-03-11 13:29:20 +0100310 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100311 u32 tmp;
312
313 /* Check preconditions */
314 tmp = in_le32(priv->regbase + SIR_SSTATUS);
315 if ((tmp & SSTATUS_DET_MASK) != 0x03) {
316 printf("Device error on port: %d\n", port);
317 return -1;
318 }
319
320 tmp = in_le32(priv->regbase + PIO_CMD_STATUS);
321 if (tmp & (ATA_BUSY | ATA_DRQ)) {
322 printf("Device not ready on port: %d\n", port);
323 return -1;
324 }
325
326 /* Clear interrupt cause */
327 out_le32(priv->regbase + EDMA_IECR, 0x0);
328
329 tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
330 tmp &= ~(port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1);
331 out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
332
333 /* Configure edma operation */
334 tmp = in_le32(priv->regbase + EDMA_CFG);
335 tmp &= ~EDMA_CFG_NCQ; /* No NCQ */
336 tmp &= ~EDMA_CFG_EQUE; /* Dont queue operations */
337 out_le32(priv->regbase + EDMA_CFG, tmp);
338
339 out_le32(priv->regbase + SIR_FIS_IRQ_CAUSE, 0x0);
340
341 /* Configure fis, set all to no-wait for now */
342 out_le32(priv->regbase + SIR_FIS_CFG, 0x0);
343
344 /* Setup request queue */
345 out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
346 out_le32(priv->regbase + EDMA_RQIPR, priv->request);
347 out_le32(priv->regbase + EDMA_RQOPR, 0x0);
348
349 /* Setup response queue */
350 out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
351 out_le32(priv->regbase + EDMA_RSOPR, priv->response);
352 out_le32(priv->regbase + EDMA_RSIPR, 0x0);
353
354 /* Start edma */
355 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ENEDMA);
356
357 return 0;
358}
359
Stefan Roesee85955c2019-03-11 13:29:20 +0100360static int mv_reset_channel(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100361{
Stefan Roesee85955c2019-03-11 13:29:20 +0100362 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100363
364 /* Make sure edma is stopped */
Stefan Roesee85955c2019-03-11 13:29:20 +0100365 mv_stop_edma_engine(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100366
367 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ATARST);
368 udelay(25); /* allow reset propagation */
369 out_le32(priv->regbase + EDMA_CMD, 0);
370 mdelay(10);
371
372 return 0;
373}
374
Stefan Roesee85955c2019-03-11 13:29:20 +0100375static void mv_reset_port(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100376{
Stefan Roesee85955c2019-03-11 13:29:20 +0100377 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100378
Stefan Roesee85955c2019-03-11 13:29:20 +0100379 mv_reset_channel(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100380
381 out_le32(priv->regbase + EDMA_CMD, 0x0);
382 out_le32(priv->regbase + EDMA_CFG, 0x101f);
383 out_le32(priv->regbase + EDMA_IECR, 0x0);
384 out_le32(priv->regbase + EDMA_IEMR, 0x0);
385 out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
386 out_le32(priv->regbase + EDMA_RQIPR, 0x0);
387 out_le32(priv->regbase + EDMA_RQOPR, 0x0);
388 out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
389 out_le32(priv->regbase + EDMA_RSIPR, 0x0);
390 out_le32(priv->regbase + EDMA_RSOPR, 0x0);
391 out_le32(priv->regbase + EDMA_IORTO, 0xfa);
392}
393
394static void mv_reset_one_hc(void)
395{
396 out_le32(SATAHC_BASE + SATAHC_ICT, 0x00);
397 out_le32(SATAHC_BASE + SATAHC_ITT, 0x00);
398 out_le32(SATAHC_BASE + SATAHC_ICR, 0x00);
399}
400
Stefan Roesee85955c2019-03-11 13:29:20 +0100401static int probe_port(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100402{
Stefan Roesee85955c2019-03-11 13:29:20 +0100403 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100404 int tries, tries2, set15 = 0;
405 u32 tmp;
406
407 debug("Probe port: %d\n", port);
408
409 for (tries = 0; tries < 2; tries++) {
410 /* Clear SError */
411 out_le32(priv->regbase + SIR_SERROR, 0x0);
412
413 /* trigger com-init */
414 tmp = in_le32(priv->regbase + SIR_SCONTROL);
415 tmp = (tmp & 0x0f0) | 0x300 | SIR_SCONTROL_DETEN;
416 out_le32(priv->regbase + SIR_SCONTROL, tmp);
417
418 mdelay(1);
419
420 tmp = in_le32(priv->regbase + SIR_SCONTROL);
421 tries2 = 5;
422 do {
423 tmp = (tmp & 0x0f0) | 0x300;
424 out_le32(priv->regbase + SIR_SCONTROL, tmp);
425 mdelay(10);
426 tmp = in_le32(priv->regbase + SIR_SCONTROL);
427 } while ((tmp & 0xf0f) != 0x300 && tries2--);
428
429 mdelay(10);
430
431 for (tries2 = 0; tries2 < 200; tries2++) {
432 tmp = in_le32(priv->regbase + SIR_SSTATUS);
433 if ((tmp & SSTATUS_DET_MASK) == 0x03) {
434 debug("Found device on port\n");
435 return 0;
436 }
437 mdelay(1);
438 }
439
440 if ((tmp & SSTATUS_DET_MASK) == 0) {
441 debug("No device attached on port %d\n", port);
442 return -ENODEV;
443 }
444
445 if (!set15) {
446 /* Try on 1.5Gb/S */
447 debug("Try 1.5Gb link\n");
448 set15 = 1;
449 out_le32(priv->regbase + SIR_SCONTROL, 0x304);
450
451 tmp = in_le32(priv->regbase + SIR_ICFG);
452 tmp &= ~SIR_CFG_GEN2EN;
453 out_le32(priv->regbase + SIR_ICFG, tmp);
454
Stefan Roesee85955c2019-03-11 13:29:20 +0100455 mv_reset_channel(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100456 }
457 }
458
459 debug("Failed to probe port\n");
460 return -1;
461}
462
463/* Get request queue in pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100464static int get_reqip(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100465{
Stefan Roesee85955c2019-03-11 13:29:20 +0100466 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100467 u32 tmp;
468
469 tmp = in_le32(priv->regbase + EDMA_RQIPR) & EDMA_RQIPR_IPMASK;
470 tmp = tmp >> EDMA_RQIPR_IPSHIFT;
471
472 return tmp;
473}
474
Stefan Roesee85955c2019-03-11 13:29:20 +0100475static void set_reqip(struct udevice *dev, int port, int reqin)
Tor Krillf65e82b2015-12-03 12:38:02 +0100476{
Stefan Roesee85955c2019-03-11 13:29:20 +0100477 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100478 u32 tmp;
479
480 tmp = in_le32(priv->regbase + EDMA_RQIPR) & ~EDMA_RQIPR_IPMASK;
481 tmp |= ((reqin << EDMA_RQIPR_IPSHIFT) & EDMA_RQIPR_IPMASK);
482 out_le32(priv->regbase + EDMA_RQIPR, tmp);
483}
484
485/* Get next available slot, ignoring possible overwrite */
Stefan Roesee85955c2019-03-11 13:29:20 +0100486static int get_next_reqip(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100487{
Stefan Roesee85955c2019-03-11 13:29:20 +0100488 int slot = get_reqip(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100489 slot = (slot + 1) % REQUEST_QUEUE_SIZE;
490 return slot;
491}
492
493/* Get response queue in pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100494static int get_rspip(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100495{
Stefan Roesee85955c2019-03-11 13:29:20 +0100496 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100497 u32 tmp;
498
499 tmp = in_le32(priv->regbase + EDMA_RSIPR) & EDMA_RSIPR_IPMASK;
500 tmp = tmp >> EDMA_RSIPR_IPSHIFT;
501
502 return tmp;
503}
504
505/* Get response queue out pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100506static int get_rspop(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100507{
Stefan Roesee85955c2019-03-11 13:29:20 +0100508 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100509 u32 tmp;
510
511 tmp = in_le32(priv->regbase + EDMA_RSOPR) & EDMA_RSOPR_OPMASK;
512 tmp = tmp >> EDMA_RSOPR_OPSHIFT;
513 return tmp;
514}
515
516/* Get next response queue pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100517static int get_next_rspop(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100518{
Stefan Roesee85955c2019-03-11 13:29:20 +0100519 return (get_rspop(dev, port) + 1) % RESPONSE_QUEUE_SIZE;
Tor Krillf65e82b2015-12-03 12:38:02 +0100520}
521
522/* Set response queue pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100523static void set_rspop(struct udevice *dev, int port, int reqin)
Tor Krillf65e82b2015-12-03 12:38:02 +0100524{
Stefan Roesee85955c2019-03-11 13:29:20 +0100525 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100526 u32 tmp;
527
528 tmp = in_le32(priv->regbase + EDMA_RSOPR) & ~EDMA_RSOPR_OPMASK;
529 tmp |= ((reqin << EDMA_RSOPR_OPSHIFT) & EDMA_RSOPR_OPMASK);
530
531 out_le32(priv->regbase + EDMA_RSOPR, tmp);
532}
533
Stefan Roesee85955c2019-03-11 13:29:20 +0100534static int wait_dma_completion(struct udevice *dev, int port, int index,
535 u32 timeout_msec)
Tor Krillf65e82b2015-12-03 12:38:02 +0100536{
537 u32 tmp, res;
538
539 tmp = port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1;
540 res = ata_wait_register((u32 *)(SATAHC_BASE + SATAHC_ICR), tmp,
541 tmp, timeout_msec);
542 if (res)
543 printf("Failed to wait for completion on port %d\n", port);
544
545 return res;
546}
547
Stefan Roesee85955c2019-03-11 13:29:20 +0100548static void process_responses(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100549{
550#ifdef DEBUG
Stefan Roesee85955c2019-03-11 13:29:20 +0100551 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100552#endif
553 u32 tmp;
Stefan Roesee85955c2019-03-11 13:29:20 +0100554 u32 outind = get_rspop(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100555
556 /* Ack interrupts */
557 tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
558 if (port == 0)
559 tmp &= ~(BIT(0) | BIT(8));
560 else
561 tmp &= ~(BIT(1) | BIT(9));
562 tmp &= ~(BIT(4));
563 out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
564
Stefan Roesee85955c2019-03-11 13:29:20 +0100565 while (get_rspip(dev, port) != outind) {
Tor Krillf65e82b2015-12-03 12:38:02 +0100566#ifdef DEBUG
567 debug("Response index %d flags %08x on port %d\n", outind,
568 priv->response[outind].flags, port);
569#endif
Stefan Roesee85955c2019-03-11 13:29:20 +0100570 outind = get_next_rspop(dev, port);
571 set_rspop(dev, port, outind);
Tor Krillf65e82b2015-12-03 12:38:02 +0100572 }
573}
574
Stefan Roesee85955c2019-03-11 13:29:20 +0100575static int mv_ata_exec_ata_cmd(struct udevice *dev, int port,
576 struct sata_fis_h2d *cfis,
Tor Krillf65e82b2015-12-03 12:38:02 +0100577 u8 *buffer, u32 len, u32 iswrite)
578{
Stefan Roesee85955c2019-03-11 13:29:20 +0100579 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100580 struct crqb *req;
581 int slot;
Stefan Roese9a62bf12016-11-18 17:21:51 +0100582 u32 start;
Tor Krillf65e82b2015-12-03 12:38:02 +0100583
584 if (len >= 64 * 1024) {
585 printf("We only support <64K transfers for now\n");
586 return -1;
587 }
588
589 /* Initialize request */
Stefan Roesee85955c2019-03-11 13:29:20 +0100590 slot = get_reqip(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100591 memset(&priv->request[slot], 0, sizeof(struct crqb));
592 req = &priv->request[slot];
593
594 req->dtb_low = (u32)buffer;
595
596 /* Dont use PRDs */
597 req->control_flags = CRQB_CNTRLFLAGS_PRDMODE;
598 req->control_flags |= iswrite ? 0 : CRQB_CNTRLFLAGS_DIR;
599 req->control_flags |=
600 ((cfis->pm_port_c << CRQB_CNTRLFLAGS_PMPORTSHIFT)
601 & CRQB_CNTRLFLAGS_PMPORTMASK);
602
603 req->drb_count = len;
604
605 req->ata_cmd_feat = (cfis->command << CRQB_CMDFEAT_CMDSHIFT) &
606 CRQB_CMDFEAT_CMDMASK;
607 req->ata_cmd_feat |= (cfis->features << CRQB_CMDFEAT_FEATSHIFT) &
608 CRQB_CMDFEAT_FEATMASK;
609
610 req->ata_addr = (cfis->lba_low << CRQB_ADDR_LBA_LOWSHIFT) &
611 CRQB_ADDR_LBA_LOWMASK;
612 req->ata_addr |= (cfis->lba_mid << CRQB_ADDR_LBA_MIDSHIFT) &
613 CRQB_ADDR_LBA_MIDMASK;
614 req->ata_addr |= (cfis->lba_high << CRQB_ADDR_LBA_HIGHSHIFT) &
615 CRQB_ADDR_LBA_HIGHMASK;
616 req->ata_addr |= (cfis->device << CRQB_ADDR_DEVICE_SHIFT) &
617 CRQB_ADDR_DEVICE_MASK;
618
619 req->ata_addr_exp = (cfis->lba_low_exp << CRQB_ADDR_LBA_LOW_EXP_SHIFT) &
620 CRQB_ADDR_LBA_LOW_EXP_MASK;
621 req->ata_addr_exp |=
622 (cfis->lba_mid_exp << CRQB_ADDR_LBA_MID_EXP_SHIFT) &
623 CRQB_ADDR_LBA_MID_EXP_MASK;
624 req->ata_addr_exp |=
625 (cfis->lba_high_exp << CRQB_ADDR_LBA_HIGH_EXP_SHIFT) &
626 CRQB_ADDR_LBA_HIGH_EXP_MASK;
627 req->ata_addr_exp |=
628 (cfis->features_exp << CRQB_ADDR_FEATURE_EXP_SHIFT) &
629 CRQB_ADDR_FEATURE_EXP_MASK;
630
631 req->ata_sect_count =
632 (cfis->sector_count << CRQB_SECTCOUNT_COUNT_SHIFT) &
633 CRQB_SECTCOUNT_COUNT_MASK;
634 req->ata_sect_count |=
635 (cfis->sector_count_exp << CRQB_SECTCOUNT_COUNT_EXP_SHIFT) &
636 CRQB_SECTCOUNT_COUNT_EXP_MASK;
637
638 /* Flush data */
Stefan Roese9a62bf12016-11-18 17:21:51 +0100639 start = (u32)req & ~(ARCH_DMA_MINALIGN - 1);
640 flush_dcache_range(start,
641 start + ALIGN(sizeof(*req), ARCH_DMA_MINALIGN));
Tor Krillf65e82b2015-12-03 12:38:02 +0100642
643 /* Trigger operation */
Stefan Roesee85955c2019-03-11 13:29:20 +0100644 slot = get_next_reqip(dev, port);
645 set_reqip(dev, port, slot);
Tor Krillf65e82b2015-12-03 12:38:02 +0100646
647 /* Wait for completion */
Stefan Roesee85955c2019-03-11 13:29:20 +0100648 if (wait_dma_completion(dev, port, slot, 10000)) {
Tor Krillf65e82b2015-12-03 12:38:02 +0100649 printf("ATA operation timed out\n");
650 return -1;
651 }
652
Stefan Roesee85955c2019-03-11 13:29:20 +0100653 process_responses(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100654
655 /* Invalidate data on read */
Stefan Roese9a62bf12016-11-18 17:21:51 +0100656 if (buffer && len) {
657 start = (u32)buffer & ~(ARCH_DMA_MINALIGN - 1);
658 invalidate_dcache_range(start,
659 start + ALIGN(len, ARCH_DMA_MINALIGN));
660 }
Tor Krillf65e82b2015-12-03 12:38:02 +0100661
662 return len;
663}
664
Stefan Roesee85955c2019-03-11 13:29:20 +0100665static u32 mv_sata_rw_cmd_ext(struct udevice *dev, int port, lbaint_t start,
666 u32 blkcnt,
Tor Krillf65e82b2015-12-03 12:38:02 +0100667 u8 *buffer, int is_write)
668{
669 struct sata_fis_h2d cfis;
670 u32 res;
671 u64 block;
672
673 block = (u64)start;
674
675 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
676
677 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
678 cfis.command = (is_write) ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
679
680 cfis.lba_high_exp = (block >> 40) & 0xff;
681 cfis.lba_mid_exp = (block >> 32) & 0xff;
682 cfis.lba_low_exp = (block >> 24) & 0xff;
683 cfis.lba_high = (block >> 16) & 0xff;
684 cfis.lba_mid = (block >> 8) & 0xff;
685 cfis.lba_low = block & 0xff;
686 cfis.device = ATA_LBA;
687 cfis.sector_count_exp = (blkcnt >> 8) & 0xff;
688 cfis.sector_count = blkcnt & 0xff;
689
Stefan Roesee85955c2019-03-11 13:29:20 +0100690 res = mv_ata_exec_ata_cmd(dev, port, &cfis, buffer,
691 ATA_SECT_SIZE * blkcnt, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100692
693 return res >= 0 ? blkcnt : res;
694}
695
Stefan Roesee85955c2019-03-11 13:29:20 +0100696static u32 mv_sata_rw_cmd(struct udevice *dev, int port, lbaint_t start,
697 u32 blkcnt, u8 *buffer, int is_write)
Tor Krillf65e82b2015-12-03 12:38:02 +0100698{
699 struct sata_fis_h2d cfis;
700 lbaint_t block;
701 u32 res;
702
703 block = start;
704
705 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
706
707 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
708 cfis.command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
709 cfis.device = ATA_LBA;
710
711 cfis.device |= (block >> 24) & 0xf;
712 cfis.lba_high = (block >> 16) & 0xff;
713 cfis.lba_mid = (block >> 8) & 0xff;
714 cfis.lba_low = block & 0xff;
715 cfis.sector_count = (u8)(blkcnt & 0xff);
716
Stefan Roesee85955c2019-03-11 13:29:20 +0100717 res = mv_ata_exec_ata_cmd(dev, port, &cfis, buffer,
718 ATA_SECT_SIZE * blkcnt, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100719
720 return res >= 0 ? blkcnt : res;
721}
722
Stefan Roesee85955c2019-03-11 13:29:20 +0100723static u32 ata_low_level_rw(struct udevice *dev, int port, lbaint_t blknr,
724 lbaint_t blkcnt, void *buffer, int is_write)
Tor Krillf65e82b2015-12-03 12:38:02 +0100725{
Stefan Roesee85955c2019-03-11 13:29:20 +0100726 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100727 lbaint_t start, blks;
728 u8 *addr;
729 int max_blks;
730
Michael Walle9d49a1e2019-04-03 23:28:28 +0200731 debug("%s: " LBAFU " " LBAFU "\n", __func__, blknr, blkcnt);
Tor Krillf65e82b2015-12-03 12:38:02 +0100732
733 start = blknr;
734 blks = blkcnt;
735 addr = (u8 *)buffer;
736
737 max_blks = MV_ATA_MAX_SECTORS;
738 do {
739 if (blks > max_blks) {
Stefan Roesee85955c2019-03-11 13:29:20 +0100740 if (desc->lba48) {
741 mv_sata_rw_cmd_ext(dev, port, start, max_blks,
742 addr, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100743 } else {
Stefan Roesee85955c2019-03-11 13:29:20 +0100744 mv_sata_rw_cmd(dev, port, start, max_blks,
745 addr, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100746 }
747 start += max_blks;
748 blks -= max_blks;
749 addr += ATA_SECT_SIZE * max_blks;
750 } else {
Stefan Roesee85955c2019-03-11 13:29:20 +0100751 if (desc->lba48) {
752 mv_sata_rw_cmd_ext(dev, port, start, blks, addr,
Tor Krillf65e82b2015-12-03 12:38:02 +0100753 is_write);
754 } else {
Stefan Roesee85955c2019-03-11 13:29:20 +0100755 mv_sata_rw_cmd(dev, port, start, blks, addr,
Tor Krillf65e82b2015-12-03 12:38:02 +0100756 is_write);
757 }
758 start += blks;
759 blks = 0;
760 addr += ATA_SECT_SIZE * blks;
761 }
762 } while (blks != 0);
763
764 return blkcnt;
765}
766
Stefan Roesee85955c2019-03-11 13:29:20 +0100767static int mv_ata_exec_ata_cmd_nondma(struct udevice *dev, int port,
Tor Krillf65e82b2015-12-03 12:38:02 +0100768 struct sata_fis_h2d *cfis, u8 *buffer,
769 u32 len, u32 iswrite)
770{
Stefan Roesee85955c2019-03-11 13:29:20 +0100771 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100772 int i;
773 u16 *tp;
774
775 debug("%s\n", __func__);
776
777 out_le32(priv->regbase + PIO_SECTOR_COUNT, cfis->sector_count);
778 out_le32(priv->regbase + PIO_LBA_HI, cfis->lba_high);
779 out_le32(priv->regbase + PIO_LBA_MID, cfis->lba_mid);
780 out_le32(priv->regbase + PIO_LBA_LOW, cfis->lba_low);
781 out_le32(priv->regbase + PIO_ERR_FEATURES, cfis->features);
782 out_le32(priv->regbase + PIO_DEVICE, cfis->device);
783 out_le32(priv->regbase + PIO_CMD_STATUS, cfis->command);
784
785 if (ata_wait_register((u32 *)(priv->regbase + PIO_CMD_STATUS),
786 ATA_BUSY, 0x0, 10000)) {
787 debug("Failed to wait for completion\n");
788 return -1;
789 }
790
791 if (len > 0) {
792 tp = (u16 *)buffer;
793 for (i = 0; i < len / 2; i++) {
794 if (iswrite)
795 out_le16(priv->regbase + PIO_DATA, *tp++);
796 else
797 *tp++ = in_le16(priv->regbase + PIO_DATA);
798 }
799 }
800
801 return len;
802}
803
Stefan Roesee85955c2019-03-11 13:29:20 +0100804static int mv_sata_identify(struct udevice *dev, int port, u16 *id)
Tor Krillf65e82b2015-12-03 12:38:02 +0100805{
806 struct sata_fis_h2d h2d;
807
808 memset(&h2d, 0, sizeof(struct sata_fis_h2d));
809
810 h2d.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
811 h2d.command = ATA_CMD_ID_ATA;
812
813 /* Give device time to get operational */
814 mdelay(10);
815
Stefan Roesee85955c2019-03-11 13:29:20 +0100816 return mv_ata_exec_ata_cmd_nondma(dev, port, &h2d, (u8 *)id,
Tor Krillf65e82b2015-12-03 12:38:02 +0100817 ATA_ID_WORDS * 2, READ_CMD);
818}
819
Stefan Roesee85955c2019-03-11 13:29:20 +0100820static void mv_sata_xfer_mode(struct udevice *dev, int port, u16 *id)
Tor Krillf65e82b2015-12-03 12:38:02 +0100821{
Stefan Roesee85955c2019-03-11 13:29:20 +0100822 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100823
824 priv->pio = id[ATA_ID_PIO_MODES];
825 priv->mwdma = id[ATA_ID_MWDMA_MODES];
826 priv->udma = id[ATA_ID_UDMA_MODES];
827 debug("pio %04x, mwdma %04x, udma %04x\n", priv->pio, priv->mwdma,
828 priv->udma);
829}
830
Stefan Roesee85955c2019-03-11 13:29:20 +0100831static void mv_sata_set_features(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100832{
Stefan Roesee85955c2019-03-11 13:29:20 +0100833 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100834 struct sata_fis_h2d cfis;
835 u8 udma_cap;
836
837 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
838
839 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
840 cfis.command = ATA_CMD_SET_FEATURES;
841 cfis.features = SETFEATURES_XFER;
842
843 /* First check the device capablity */
844 udma_cap = (u8) (priv->udma & 0xff);
845
846 if (udma_cap == ATA_UDMA6)
847 cfis.sector_count = XFER_UDMA_6;
848 if (udma_cap == ATA_UDMA5)
849 cfis.sector_count = XFER_UDMA_5;
850 if (udma_cap == ATA_UDMA4)
851 cfis.sector_count = XFER_UDMA_4;
852 if (udma_cap == ATA_UDMA3)
853 cfis.sector_count = XFER_UDMA_3;
854
Stefan Roesee85955c2019-03-11 13:29:20 +0100855 mv_ata_exec_ata_cmd_nondma(dev, port, &cfis, NULL, 0, READ_CMD);
Tor Krillf65e82b2015-12-03 12:38:02 +0100856}
857
858/*
859 * Initialize SATA memory windows
860 */
861static void mvsata_ide_conf_mbus_windows(void)
862{
863 const struct mbus_dram_target_info *dram;
864 int i;
865
866 dram = mvebu_mbus_dram_info();
867
868 /* Disable windows, Set Size/Base to 0 */
869 for (i = 0; i < 4; i++) {
870 writel(0, MVSATA_WIN_CONTROL(i));
871 writel(0, MVSATA_WIN_BASE(i));
872 }
873
874 for (i = 0; i < dram->num_cs; i++) {
875 const struct mbus_dram_window *cs = dram->cs + i;
876 writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) |
877 (dram->mbus_dram_target_id << 4) | 1,
878 MVSATA_WIN_CONTROL(i));
879 writel(cs->base & 0xffff0000, MVSATA_WIN_BASE(i));
880 }
881}
882
Stefan Roesee85955c2019-03-11 13:29:20 +0100883static int sata_mv_init_sata(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100884{
Stefan Roesee85955c2019-03-11 13:29:20 +0100885 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100886
Stefan Roesee85955c2019-03-11 13:29:20 +0100887 debug("Initialize sata dev: %d\n", port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100888
Stefan Roesee85955c2019-03-11 13:29:20 +0100889 if (port < 0 || port >= CONFIG_SYS_SATA_MAX_DEVICE) {
890 printf("Invalid sata device %d\n", port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100891 return -1;
892 }
893
Tor Krillf65e82b2015-12-03 12:38:02 +0100894 /* Allocate and align request buffer */
895 priv->crqb_alloc = malloc(sizeof(struct crqb) * REQUEST_QUEUE_SIZE +
896 CRQB_ALIGN);
897 if (!priv->crqb_alloc) {
898 printf("Unable to allocate memory for request queue\n");
899 return -ENOMEM;
900 }
901 memset(priv->crqb_alloc, 0,
902 sizeof(struct crqb) * REQUEST_QUEUE_SIZE + CRQB_ALIGN);
903 priv->request = (struct crqb *)(((u32) priv->crqb_alloc + CRQB_ALIGN) &
904 ~(CRQB_ALIGN - 1));
905
906 /* Allocate and align response buffer */
907 priv->crpb_alloc = malloc(sizeof(struct crpb) * REQUEST_QUEUE_SIZE +
908 CRPB_ALIGN);
909 if (!priv->crpb_alloc) {
910 printf("Unable to allocate memory for response queue\n");
911 return -ENOMEM;
912 }
913 memset(priv->crpb_alloc, 0,
914 sizeof(struct crpb) * REQUEST_QUEUE_SIZE + CRPB_ALIGN);
915 priv->response = (struct crpb *)(((u32) priv->crpb_alloc + CRPB_ALIGN) &
916 ~(CRPB_ALIGN - 1));
917
Stefan Roesee85955c2019-03-11 13:29:20 +0100918 sprintf(priv->name, "SATA%d", port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100919
Stefan Roesee85955c2019-03-11 13:29:20 +0100920 priv->regbase = port == 0 ? SATA0_BASE : SATA1_BASE;
Tor Krillf65e82b2015-12-03 12:38:02 +0100921
922 if (!hw_init) {
923 debug("Initialize sata hw\n");
924 hw_init = 1;
925 mv_reset_one_hc();
926 mvsata_ide_conf_mbus_windows();
927 }
928
Stefan Roesee85955c2019-03-11 13:29:20 +0100929 mv_reset_port(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100930
Stefan Roesee85955c2019-03-11 13:29:20 +0100931 if (probe_port(dev, port)) {
Tor Krillf65e82b2015-12-03 12:38:02 +0100932 priv->link = 0;
933 return -ENODEV;
934 }
935 priv->link = 1;
936
937 return 0;
938}
939
Stefan Roesee85955c2019-03-11 13:29:20 +0100940static int sata_mv_scan_sata(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100941{
Stefan Roesee85955c2019-03-11 13:29:20 +0100942 struct blk_desc *desc = dev_get_uclass_platdata(dev);
943 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100944 unsigned char serial[ATA_ID_SERNO_LEN + 1];
945 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
946 unsigned char product[ATA_ID_PROD_LEN + 1];
947 u64 n_sectors;
948 u16 *id;
Tor Krillf65e82b2015-12-03 12:38:02 +0100949
950 if (!priv->link)
951 return -ENODEV;
952
953 id = (u16 *)malloc(ATA_ID_WORDS * 2);
954 if (!id) {
955 printf("Failed to malloc id data\n");
956 return -ENOMEM;
957 }
958
Stefan Roesee85955c2019-03-11 13:29:20 +0100959 mv_sata_identify(dev, port, id);
Tor Krillf65e82b2015-12-03 12:38:02 +0100960 ata_swap_buf_le16(id, ATA_ID_WORDS);
961#ifdef DEBUG
962 ata_dump_id(id);
963#endif
964
965 /* Serial number */
966 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
Stefan Roesee85955c2019-03-11 13:29:20 +0100967 memcpy(desc->product, serial, sizeof(serial));
Tor Krillf65e82b2015-12-03 12:38:02 +0100968
969 /* Firmware version */
970 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
Stefan Roesee85955c2019-03-11 13:29:20 +0100971 memcpy(desc->revision, firmware, sizeof(firmware));
Tor Krillf65e82b2015-12-03 12:38:02 +0100972
973 /* Product model */
974 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
Stefan Roesee85955c2019-03-11 13:29:20 +0100975 memcpy(desc->vendor, product, sizeof(product));
Tor Krillf65e82b2015-12-03 12:38:02 +0100976
977 /* Total sectors */
978 n_sectors = ata_id_n_sectors(id);
Stefan Roesee85955c2019-03-11 13:29:20 +0100979 desc->lba = n_sectors;
Tor Krillf65e82b2015-12-03 12:38:02 +0100980
981 /* Check if support LBA48 */
982 if (ata_id_has_lba48(id)) {
Stefan Roesee85955c2019-03-11 13:29:20 +0100983 desc->lba48 = 1;
Tor Krillf65e82b2015-12-03 12:38:02 +0100984 debug("Device support LBA48\n");
985 }
986
987 /* Get the NCQ queue depth from device */
988 priv->queue_depth = ata_id_queue_depth(id);
989
990 /* Get the xfer mode from device */
Stefan Roesee85955c2019-03-11 13:29:20 +0100991 mv_sata_xfer_mode(dev, port, id);
Tor Krillf65e82b2015-12-03 12:38:02 +0100992
993 /* Set the xfer mode to highest speed */
Stefan Roesee85955c2019-03-11 13:29:20 +0100994 mv_sata_set_features(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100995
996 /* Start up */
Stefan Roesee85955c2019-03-11 13:29:20 +0100997 mv_start_edma_engine(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100998
999 return 0;
1000}
Stefan Roesee85955c2019-03-11 13:29:20 +01001001
1002static ulong sata_mv_read(struct udevice *blk, lbaint_t blknr,
1003 lbaint_t blkcnt, void *buffer)
1004{
1005 struct mv_priv *priv = dev_get_platdata(blk);
1006
1007 return ata_low_level_rw(blk, priv->dev_nr, blknr, blkcnt,
1008 buffer, READ_CMD);
1009}
1010
1011static ulong sata_mv_write(struct udevice *blk, lbaint_t blknr,
1012 lbaint_t blkcnt, const void *buffer)
1013{
1014 struct mv_priv *priv = dev_get_platdata(blk);
1015
1016 return ata_low_level_rw(blk, priv->dev_nr, blknr, blkcnt,
1017 (void *)buffer, WRITE_CMD);
1018}
1019
1020static const struct blk_ops sata_mv_blk_ops = {
1021 .read = sata_mv_read,
1022 .write = sata_mv_write,
1023};
1024
1025U_BOOT_DRIVER(sata_mv_driver) = {
1026 .name = "sata_mv_blk",
1027 .id = UCLASS_BLK,
1028 .ops = &sata_mv_blk_ops,
1029 .platdata_auto_alloc_size = sizeof(struct mv_priv),
1030};
1031
1032static int sata_mv_probe(struct udevice *dev)
1033{
1034 const void *blob = gd->fdt_blob;
1035 int node = dev_of_offset(dev);
1036 struct mv_priv *priv;
1037 struct udevice *blk;
1038 int nr_ports;
1039 int ret;
1040 int i;
1041
1042 /* Get number of ports of this SATA controller */
1043 nr_ports = min(fdtdec_get_int(blob, node, "nr-ports", -1),
1044 CONFIG_SYS_SATA_MAX_DEVICE);
1045
1046 for (i = 0; i < nr_ports; i++) {
1047 ret = blk_create_devicef(dev, "sata_mv_blk", "blk",
1048 IF_TYPE_SATA, -1, 512, 0, &blk);
1049 if (ret) {
1050 debug("Can't create device\n");
1051 return ret;
1052 }
1053
1054 priv = dev_get_platdata(blk);
1055 priv->dev_nr = i;
1056
1057 /* Init SATA port */
1058 ret = sata_mv_init_sata(blk, i);
1059 if (ret) {
1060 debug("%s: Failed to init bus\n", __func__);
1061 return ret;
1062 }
1063
1064 /* Scan SATA port */
1065 ret = sata_mv_scan_sata(blk, i);
1066 if (ret) {
1067 debug("%s: Failed to scan bus\n", __func__);
1068 return ret;
1069 }
1070 }
1071
1072 return 0;
1073}
1074
1075static int sata_mv_scan(struct udevice *dev)
1076{
1077 /* Nothing to do here */
1078
1079 return 0;
1080}
1081
1082static const struct udevice_id sata_mv_ids[] = {
1083 { .compatible = "marvell,armada-370-sata" },
Michael Walle99e92d92019-04-03 23:28:30 +02001084 { .compatible = "marvell,orion-sata" },
Stefan Roesee85955c2019-03-11 13:29:20 +01001085 { }
1086};
1087
1088struct ahci_ops sata_mv_ahci_ops = {
1089 .scan = sata_mv_scan,
1090};
1091
1092U_BOOT_DRIVER(sata_mv_ahci) = {
1093 .name = "sata_mv_ahci",
1094 .id = UCLASS_AHCI,
1095 .of_match = sata_mv_ids,
1096 .ops = &sata_mv_ahci_ops,
1097 .probe = sata_mv_probe,
1098};