blob: b2b5c699ad14910392776bc228e9f678f23ea609 [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 Glass655306c2020-05-10 11:39:58 -060036#include <blk.h>
Simon Glass63334482019-11-14 12:57:39 -070037#include <cpu_func.h>
Stefan Roesee85955c2019-03-11 13:29:20 +010038#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060039#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060040#include <asm/cache.h>
Stefan Roesee85955c2019-03-11 13:29:20 +010041#include <dm/device-internal.h>
42#include <dm/lists.h>
Tor Krillf65e82b2015-12-03 12:38:02 +010043#include <fis.h>
44#include <libata.h>
45#include <malloc.h>
46#include <sata.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060047#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060048#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090049#include <linux/errno.h>
Tor Krillf65e82b2015-12-03 12:38:02 +010050#include <asm/io.h>
51#include <linux/mbus.h>
52
Michael Walle692b9782019-04-03 23:28:29 +020053#include <asm/arch/soc.h>
Trevor Woernerbb7ab072020-05-06 08:02:40 -040054#if defined(CONFIG_ARCH_KIRKWOOD)
Tor Krillf65e82b2015-12-03 12:38:02 +010055#define SATAHC_BASE KW_SATA_BASE
56#else
Tor Krillf65e82b2015-12-03 12:38:02 +010057#define SATAHC_BASE MVEBU_AXP_SATA_BASE
58#endif
59
60#define SATA0_BASE (SATAHC_BASE + 0x2000)
61#define SATA1_BASE (SATAHC_BASE + 0x4000)
62
63/* EDMA registers */
64#define EDMA_CFG 0x000
65#define EDMA_CFG_NCQ (1 << 5)
66#define EDMA_CFG_EQUE (1 << 9)
67#define EDMA_TIMER 0x004
68#define EDMA_IECR 0x008
69#define EDMA_IEMR 0x00c
70#define EDMA_RQBA_HI 0x010
71#define EDMA_RQIPR 0x014
72#define EDMA_RQIPR_IPMASK (0x1f << 5)
73#define EDMA_RQIPR_IPSHIFT 5
74#define EDMA_RQOPR 0x018
75#define EDMA_RQOPR_OPMASK (0x1f << 5)
76#define EDMA_RQOPR_OPSHIFT 5
77#define EDMA_RSBA_HI 0x01c
78#define EDMA_RSIPR 0x020
79#define EDMA_RSIPR_IPMASK (0x1f << 3)
80#define EDMA_RSIPR_IPSHIFT 3
81#define EDMA_RSOPR 0x024
82#define EDMA_RSOPR_OPMASK (0x1f << 3)
83#define EDMA_RSOPR_OPSHIFT 3
84#define EDMA_CMD 0x028
85#define EDMA_CMD_ENEDMA (0x01 << 0)
86#define EDMA_CMD_DISEDMA (0x01 << 1)
87#define EDMA_CMD_ATARST (0x01 << 2)
88#define EDMA_CMD_FREEZE (0x01 << 4)
89#define EDMA_TEST_CTL 0x02c
90#define EDMA_STATUS 0x030
91#define EDMA_IORTO 0x034
92#define EDMA_CDTR 0x040
93#define EDMA_HLTCND 0x060
94#define EDMA_NTSR 0x094
95
96/* Basic DMA registers */
97#define BDMA_CMD 0x224
98#define BDMA_STATUS 0x228
99#define BDMA_DTLB 0x22c
100#define BDMA_DTHB 0x230
101#define BDMA_DRL 0x234
102#define BDMA_DRH 0x238
103
104/* SATA Interface registers */
105#define SIR_ICFG 0x050
106#define SIR_CFG_GEN2EN (0x1 << 7)
107#define SIR_PLL_CFG 0x054
108#define SIR_SSTATUS 0x300
109#define SSTATUS_DET_MASK (0x0f << 0)
110#define SIR_SERROR 0x304
111#define SIR_SCONTROL 0x308
112#define SIR_SCONTROL_DETEN (0x01 << 0)
113#define SIR_LTMODE 0x30c
114#define SIR_LTMODE_NELBE (0x01 << 7)
115#define SIR_PHYMODE3 0x310
116#define SIR_PHYMODE4 0x314
117#define SIR_PHYMODE1 0x32c
118#define SIR_PHYMODE2 0x330
119#define SIR_BIST_CTRL 0x334
120#define SIR_BIST_DW1 0x338
121#define SIR_BIST_DW2 0x33c
122#define SIR_SERR_IRQ_MASK 0x340
123#define SIR_SATA_IFCTRL 0x344
124#define SIR_SATA_TESTCTRL 0x348
125#define SIR_SATA_IFSTATUS 0x34c
126#define SIR_VEND_UNIQ 0x35c
127#define SIR_FIS_CFG 0x360
128#define SIR_FIS_IRQ_CAUSE 0x364
129#define SIR_FIS_IRQ_MASK 0x368
130#define SIR_FIS_DWORD0 0x370
131#define SIR_FIS_DWORD1 0x374
132#define SIR_FIS_DWORD2 0x378
133#define SIR_FIS_DWORD3 0x37c
134#define SIR_FIS_DWORD4 0x380
135#define SIR_FIS_DWORD5 0x384
136#define SIR_FIS_DWORD6 0x388
137#define SIR_PHYM9_GEN2 0x398
138#define SIR_PHYM9_GEN1 0x39c
139#define SIR_PHY_CFG 0x3a0
140#define SIR_PHYCTL 0x3a4
141#define SIR_PHYM10 0x3a8
142#define SIR_PHYM12 0x3b0
143
144/* Shadow registers */
145#define PIO_DATA 0x100
146#define PIO_ERR_FEATURES 0x104
147#define PIO_SECTOR_COUNT 0x108
148#define PIO_LBA_LOW 0x10c
149#define PIO_LBA_MID 0x110
150#define PIO_LBA_HI 0x114
151#define PIO_DEVICE 0x118
152#define PIO_CMD_STATUS 0x11c
153#define PIO_STATUS_ERR (0x01 << 0)
154#define PIO_STATUS_DRQ (0x01 << 3)
155#define PIO_STATUS_DF (0x01 << 5)
156#define PIO_STATUS_DRDY (0x01 << 6)
157#define PIO_STATUS_BSY (0x01 << 7)
158#define PIO_CTRL_ALTSTAT 0x120
159
160/* SATAHC arbiter registers */
161#define SATAHC_CFG 0x000
162#define SATAHC_RQOP 0x004
163#define SATAHC_RQIP 0x008
164#define SATAHC_ICT 0x00c
165#define SATAHC_ITT 0x010
166#define SATAHC_ICR 0x014
167#define SATAHC_ICR_PORT0 (0x01 << 0)
168#define SATAHC_ICR_PORT1 (0x01 << 1)
169#define SATAHC_MIC 0x020
170#define SATAHC_MIM 0x024
171#define SATAHC_LED_CFG 0x02c
172
173#define REQUEST_QUEUE_SIZE 32
174#define RESPONSE_QUEUE_SIZE REQUEST_QUEUE_SIZE
175
176struct crqb {
177 u32 dtb_low; /* DW0 */
178 u32 dtb_high; /* DW1 */
179 u32 control_flags; /* DW2 */
180 u32 drb_count; /* DW3 */
181 u32 ata_cmd_feat; /* DW4 */
182 u32 ata_addr; /* DW5 */
183 u32 ata_addr_exp; /* DW6 */
184 u32 ata_sect_count; /* DW7 */
185};
186
187#define CRQB_ALIGN 0x400
188
189#define CRQB_CNTRLFLAGS_DIR (0x01 << 0)
190#define CRQB_CNTRLFLAGS_DQTAGMASK (0x1f << 1)
191#define CRQB_CNTRLFLAGS_DQTAGSHIFT 1
192#define CRQB_CNTRLFLAGS_PMPORTMASK (0x0f << 12)
193#define CRQB_CNTRLFLAGS_PMPORTSHIFT 12
194#define CRQB_CNTRLFLAGS_PRDMODE (0x01 << 16)
195#define CRQB_CNTRLFLAGS_HQTAGMASK (0x1f << 17)
196#define CRQB_CNTRLFLAGS_HQTAGSHIFT 17
197
198#define CRQB_CMDFEAT_CMDMASK (0xff << 16)
199#define CRQB_CMDFEAT_CMDSHIFT 16
200#define CRQB_CMDFEAT_FEATMASK (0xff << 16)
201#define CRQB_CMDFEAT_FEATSHIFT 24
202
203#define CRQB_ADDR_LBA_LOWMASK (0xff << 0)
204#define CRQB_ADDR_LBA_LOWSHIFT 0
205#define CRQB_ADDR_LBA_MIDMASK (0xff << 8)
206#define CRQB_ADDR_LBA_MIDSHIFT 8
207#define CRQB_ADDR_LBA_HIGHMASK (0xff << 16)
208#define CRQB_ADDR_LBA_HIGHSHIFT 16
209#define CRQB_ADDR_DEVICE_MASK (0xff << 24)
210#define CRQB_ADDR_DEVICE_SHIFT 24
211
212#define CRQB_ADDR_LBA_LOW_EXP_MASK (0xff << 0)
213#define CRQB_ADDR_LBA_LOW_EXP_SHIFT 0
214#define CRQB_ADDR_LBA_MID_EXP_MASK (0xff << 8)
215#define CRQB_ADDR_LBA_MID_EXP_SHIFT 8
216#define CRQB_ADDR_LBA_HIGH_EXP_MASK (0xff << 16)
217#define CRQB_ADDR_LBA_HIGH_EXP_SHIFT 16
218#define CRQB_ADDR_FEATURE_EXP_MASK (0xff << 24)
219#define CRQB_ADDR_FEATURE_EXP_SHIFT 24
220
221#define CRQB_SECTCOUNT_COUNT_MASK (0xff << 0)
222#define CRQB_SECTCOUNT_COUNT_SHIFT 0
223#define CRQB_SECTCOUNT_COUNT_EXP_MASK (0xff << 8)
224#define CRQB_SECTCOUNT_COUNT_EXP_SHIFT 8
225
Michael Walle692b9782019-04-03 23:28:29 +0200226#define MVSATA_WIN_CONTROL(w) (SATAHC_BASE + 0x30 + ((w) << 4))
227#define MVSATA_WIN_BASE(w) (SATAHC_BASE + 0x34 + ((w) << 4))
Tor Krillf65e82b2015-12-03 12:38:02 +0100228
229struct eprd {
230 u32 phyaddr_low;
231 u32 bytecount_eot;
232 u32 phyaddr_hi;
233 u32 reserved;
234};
235
236#define EPRD_PHYADDR_MASK 0xfffffffe
237#define EPRD_BYTECOUNT_MASK 0x0000ffff
238#define EPRD_EOT (0x01 << 31)
239
240struct crpb {
241 u32 id;
242 u32 flags;
243 u32 timestamp;
244};
245
246#define CRPB_ALIGN 0x100
247
248#define READ_CMD 0
249#define WRITE_CMD 1
250
251/*
252 * Since we don't use PRDs yet max transfer size
253 * is 64KB
254 */
255#define MV_ATA_MAX_SECTORS (65535 / ATA_SECT_SIZE)
256
257/* Keep track if hw is initialized or not */
258static u32 hw_init;
259
260struct mv_priv {
261 char name[12];
262 u32 link;
263 u32 regbase;
264 u32 queue_depth;
265 u16 pio;
266 u16 mwdma;
267 u16 udma;
Stefan Roesee85955c2019-03-11 13:29:20 +0100268 int dev_nr;
Tor Krillf65e82b2015-12-03 12:38:02 +0100269
270 void *crqb_alloc;
271 struct crqb *request;
272
273 void *crpb_alloc;
274 struct crpb *response;
275};
276
277static int ata_wait_register(u32 *addr, u32 mask, u32 val, u32 timeout_msec)
278{
279 ulong start;
280
281 start = get_timer(0);
282 do {
283 if ((in_le32(addr) & mask) == val)
284 return 0;
285 } while (get_timer(start) < timeout_msec);
286
287 return -ETIMEDOUT;
288}
289
290/* Cut from sata_mv in linux kernel */
Stefan Roesee85955c2019-03-11 13:29:20 +0100291static int mv_stop_edma_engine(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100292{
Stefan Roesee85955c2019-03-11 13:29:20 +0100293 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100294 int i;
295
296 /* Disable eDMA. The disable bit auto clears. */
297 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_DISEDMA);
298
299 /* Wait for the chip to confirm eDMA is off. */
300 for (i = 10000; i > 0; i--) {
301 u32 reg = in_le32(priv->regbase + EDMA_CMD);
302 if (!(reg & EDMA_CMD_ENEDMA)) {
303 debug("EDMA stop on port %d succesful\n", port);
304 return 0;
305 }
306 udelay(10);
307 }
308 debug("EDMA stop on port %d failed\n", port);
309 return -1;
310}
311
Stefan Roesee85955c2019-03-11 13:29:20 +0100312static int mv_start_edma_engine(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100313{
Stefan Roesee85955c2019-03-11 13:29:20 +0100314 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100315 u32 tmp;
316
317 /* Check preconditions */
318 tmp = in_le32(priv->regbase + SIR_SSTATUS);
319 if ((tmp & SSTATUS_DET_MASK) != 0x03) {
320 printf("Device error on port: %d\n", port);
321 return -1;
322 }
323
324 tmp = in_le32(priv->regbase + PIO_CMD_STATUS);
325 if (tmp & (ATA_BUSY | ATA_DRQ)) {
326 printf("Device not ready on port: %d\n", port);
327 return -1;
328 }
329
330 /* Clear interrupt cause */
331 out_le32(priv->regbase + EDMA_IECR, 0x0);
332
333 tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
334 tmp &= ~(port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1);
335 out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
336
337 /* Configure edma operation */
338 tmp = in_le32(priv->regbase + EDMA_CFG);
339 tmp &= ~EDMA_CFG_NCQ; /* No NCQ */
340 tmp &= ~EDMA_CFG_EQUE; /* Dont queue operations */
341 out_le32(priv->regbase + EDMA_CFG, tmp);
342
343 out_le32(priv->regbase + SIR_FIS_IRQ_CAUSE, 0x0);
344
345 /* Configure fis, set all to no-wait for now */
346 out_le32(priv->regbase + SIR_FIS_CFG, 0x0);
347
348 /* Setup request queue */
349 out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
350 out_le32(priv->regbase + EDMA_RQIPR, priv->request);
351 out_le32(priv->regbase + EDMA_RQOPR, 0x0);
352
353 /* Setup response queue */
354 out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
355 out_le32(priv->regbase + EDMA_RSOPR, priv->response);
356 out_le32(priv->regbase + EDMA_RSIPR, 0x0);
357
358 /* Start edma */
359 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ENEDMA);
360
361 return 0;
362}
363
Stefan Roesee85955c2019-03-11 13:29:20 +0100364static int mv_reset_channel(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100365{
Stefan Roesee85955c2019-03-11 13:29:20 +0100366 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100367
368 /* Make sure edma is stopped */
Stefan Roesee85955c2019-03-11 13:29:20 +0100369 mv_stop_edma_engine(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100370
371 out_le32(priv->regbase + EDMA_CMD, EDMA_CMD_ATARST);
372 udelay(25); /* allow reset propagation */
373 out_le32(priv->regbase + EDMA_CMD, 0);
374 mdelay(10);
375
376 return 0;
377}
378
Stefan Roesee85955c2019-03-11 13:29:20 +0100379static void mv_reset_port(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100380{
Stefan Roesee85955c2019-03-11 13:29:20 +0100381 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100382
Stefan Roesee85955c2019-03-11 13:29:20 +0100383 mv_reset_channel(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100384
385 out_le32(priv->regbase + EDMA_CMD, 0x0);
386 out_le32(priv->regbase + EDMA_CFG, 0x101f);
387 out_le32(priv->regbase + EDMA_IECR, 0x0);
388 out_le32(priv->regbase + EDMA_IEMR, 0x0);
389 out_le32(priv->regbase + EDMA_RQBA_HI, 0x0);
390 out_le32(priv->regbase + EDMA_RQIPR, 0x0);
391 out_le32(priv->regbase + EDMA_RQOPR, 0x0);
392 out_le32(priv->regbase + EDMA_RSBA_HI, 0x0);
393 out_le32(priv->regbase + EDMA_RSIPR, 0x0);
394 out_le32(priv->regbase + EDMA_RSOPR, 0x0);
395 out_le32(priv->regbase + EDMA_IORTO, 0xfa);
396}
397
398static void mv_reset_one_hc(void)
399{
400 out_le32(SATAHC_BASE + SATAHC_ICT, 0x00);
401 out_le32(SATAHC_BASE + SATAHC_ITT, 0x00);
402 out_le32(SATAHC_BASE + SATAHC_ICR, 0x00);
403}
404
Stefan Roesee85955c2019-03-11 13:29:20 +0100405static int probe_port(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100406{
Stefan Roesee85955c2019-03-11 13:29:20 +0100407 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100408 int tries, tries2, set15 = 0;
409 u32 tmp;
410
411 debug("Probe port: %d\n", port);
412
413 for (tries = 0; tries < 2; tries++) {
414 /* Clear SError */
415 out_le32(priv->regbase + SIR_SERROR, 0x0);
416
417 /* trigger com-init */
418 tmp = in_le32(priv->regbase + SIR_SCONTROL);
419 tmp = (tmp & 0x0f0) | 0x300 | SIR_SCONTROL_DETEN;
420 out_le32(priv->regbase + SIR_SCONTROL, tmp);
421
422 mdelay(1);
423
424 tmp = in_le32(priv->regbase + SIR_SCONTROL);
425 tries2 = 5;
426 do {
427 tmp = (tmp & 0x0f0) | 0x300;
428 out_le32(priv->regbase + SIR_SCONTROL, tmp);
429 mdelay(10);
430 tmp = in_le32(priv->regbase + SIR_SCONTROL);
431 } while ((tmp & 0xf0f) != 0x300 && tries2--);
432
433 mdelay(10);
434
435 for (tries2 = 0; tries2 < 200; tries2++) {
436 tmp = in_le32(priv->regbase + SIR_SSTATUS);
437 if ((tmp & SSTATUS_DET_MASK) == 0x03) {
438 debug("Found device on port\n");
439 return 0;
440 }
441 mdelay(1);
442 }
443
444 if ((tmp & SSTATUS_DET_MASK) == 0) {
445 debug("No device attached on port %d\n", port);
446 return -ENODEV;
447 }
448
449 if (!set15) {
450 /* Try on 1.5Gb/S */
451 debug("Try 1.5Gb link\n");
452 set15 = 1;
453 out_le32(priv->regbase + SIR_SCONTROL, 0x304);
454
455 tmp = in_le32(priv->regbase + SIR_ICFG);
456 tmp &= ~SIR_CFG_GEN2EN;
457 out_le32(priv->regbase + SIR_ICFG, tmp);
458
Stefan Roesee85955c2019-03-11 13:29:20 +0100459 mv_reset_channel(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100460 }
461 }
462
463 debug("Failed to probe port\n");
464 return -1;
465}
466
467/* Get request queue in pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100468static int get_reqip(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100469{
Stefan Roesee85955c2019-03-11 13:29:20 +0100470 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100471 u32 tmp;
472
473 tmp = in_le32(priv->regbase + EDMA_RQIPR) & EDMA_RQIPR_IPMASK;
474 tmp = tmp >> EDMA_RQIPR_IPSHIFT;
475
476 return tmp;
477}
478
Stefan Roesee85955c2019-03-11 13:29:20 +0100479static void set_reqip(struct udevice *dev, int port, int reqin)
Tor Krillf65e82b2015-12-03 12:38:02 +0100480{
Stefan Roesee85955c2019-03-11 13:29:20 +0100481 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100482 u32 tmp;
483
484 tmp = in_le32(priv->regbase + EDMA_RQIPR) & ~EDMA_RQIPR_IPMASK;
485 tmp |= ((reqin << EDMA_RQIPR_IPSHIFT) & EDMA_RQIPR_IPMASK);
486 out_le32(priv->regbase + EDMA_RQIPR, tmp);
487}
488
489/* Get next available slot, ignoring possible overwrite */
Stefan Roesee85955c2019-03-11 13:29:20 +0100490static int get_next_reqip(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100491{
Stefan Roesee85955c2019-03-11 13:29:20 +0100492 int slot = get_reqip(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100493 slot = (slot + 1) % REQUEST_QUEUE_SIZE;
494 return slot;
495}
496
497/* Get response queue in pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100498static int get_rspip(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100499{
Stefan Roesee85955c2019-03-11 13:29:20 +0100500 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100501 u32 tmp;
502
503 tmp = in_le32(priv->regbase + EDMA_RSIPR) & EDMA_RSIPR_IPMASK;
504 tmp = tmp >> EDMA_RSIPR_IPSHIFT;
505
506 return tmp;
507}
508
509/* Get response queue out pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100510static int get_rspop(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100511{
Stefan Roesee85955c2019-03-11 13:29:20 +0100512 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100513 u32 tmp;
514
515 tmp = in_le32(priv->regbase + EDMA_RSOPR) & EDMA_RSOPR_OPMASK;
516 tmp = tmp >> EDMA_RSOPR_OPSHIFT;
517 return tmp;
518}
519
520/* Get next response queue pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100521static int get_next_rspop(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100522{
Stefan Roesee85955c2019-03-11 13:29:20 +0100523 return (get_rspop(dev, port) + 1) % RESPONSE_QUEUE_SIZE;
Tor Krillf65e82b2015-12-03 12:38:02 +0100524}
525
526/* Set response queue pointer */
Stefan Roesee85955c2019-03-11 13:29:20 +0100527static void set_rspop(struct udevice *dev, int port, int reqin)
Tor Krillf65e82b2015-12-03 12:38:02 +0100528{
Stefan Roesee85955c2019-03-11 13:29:20 +0100529 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100530 u32 tmp;
531
532 tmp = in_le32(priv->regbase + EDMA_RSOPR) & ~EDMA_RSOPR_OPMASK;
533 tmp |= ((reqin << EDMA_RSOPR_OPSHIFT) & EDMA_RSOPR_OPMASK);
534
535 out_le32(priv->regbase + EDMA_RSOPR, tmp);
536}
537
Stefan Roesee85955c2019-03-11 13:29:20 +0100538static int wait_dma_completion(struct udevice *dev, int port, int index,
539 u32 timeout_msec)
Tor Krillf65e82b2015-12-03 12:38:02 +0100540{
541 u32 tmp, res;
542
543 tmp = port == 0 ? SATAHC_ICR_PORT0 : SATAHC_ICR_PORT1;
544 res = ata_wait_register((u32 *)(SATAHC_BASE + SATAHC_ICR), tmp,
545 tmp, timeout_msec);
546 if (res)
547 printf("Failed to wait for completion on port %d\n", port);
548
549 return res;
550}
551
Stefan Roesee85955c2019-03-11 13:29:20 +0100552static void process_responses(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100553{
554#ifdef DEBUG
Stefan Roesee85955c2019-03-11 13:29:20 +0100555 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100556#endif
557 u32 tmp;
Stefan Roesee85955c2019-03-11 13:29:20 +0100558 u32 outind = get_rspop(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100559
560 /* Ack interrupts */
561 tmp = in_le32(SATAHC_BASE + SATAHC_ICR);
562 if (port == 0)
563 tmp &= ~(BIT(0) | BIT(8));
564 else
565 tmp &= ~(BIT(1) | BIT(9));
566 tmp &= ~(BIT(4));
567 out_le32(SATAHC_BASE + SATAHC_ICR, tmp);
568
Stefan Roesee85955c2019-03-11 13:29:20 +0100569 while (get_rspip(dev, port) != outind) {
Tor Krillf65e82b2015-12-03 12:38:02 +0100570#ifdef DEBUG
571 debug("Response index %d flags %08x on port %d\n", outind,
572 priv->response[outind].flags, port);
573#endif
Stefan Roesee85955c2019-03-11 13:29:20 +0100574 outind = get_next_rspop(dev, port);
575 set_rspop(dev, port, outind);
Tor Krillf65e82b2015-12-03 12:38:02 +0100576 }
577}
578
Stefan Roesee85955c2019-03-11 13:29:20 +0100579static int mv_ata_exec_ata_cmd(struct udevice *dev, int port,
580 struct sata_fis_h2d *cfis,
Tor Krillf65e82b2015-12-03 12:38:02 +0100581 u8 *buffer, u32 len, u32 iswrite)
582{
Stefan Roesee85955c2019-03-11 13:29:20 +0100583 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100584 struct crqb *req;
585 int slot;
Stefan Roese9a62bf12016-11-18 17:21:51 +0100586 u32 start;
Tor Krillf65e82b2015-12-03 12:38:02 +0100587
588 if (len >= 64 * 1024) {
589 printf("We only support <64K transfers for now\n");
590 return -1;
591 }
592
593 /* Initialize request */
Stefan Roesee85955c2019-03-11 13:29:20 +0100594 slot = get_reqip(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100595 memset(&priv->request[slot], 0, sizeof(struct crqb));
596 req = &priv->request[slot];
597
598 req->dtb_low = (u32)buffer;
599
600 /* Dont use PRDs */
601 req->control_flags = CRQB_CNTRLFLAGS_PRDMODE;
602 req->control_flags |= iswrite ? 0 : CRQB_CNTRLFLAGS_DIR;
603 req->control_flags |=
604 ((cfis->pm_port_c << CRQB_CNTRLFLAGS_PMPORTSHIFT)
605 & CRQB_CNTRLFLAGS_PMPORTMASK);
606
607 req->drb_count = len;
608
609 req->ata_cmd_feat = (cfis->command << CRQB_CMDFEAT_CMDSHIFT) &
610 CRQB_CMDFEAT_CMDMASK;
611 req->ata_cmd_feat |= (cfis->features << CRQB_CMDFEAT_FEATSHIFT) &
612 CRQB_CMDFEAT_FEATMASK;
613
614 req->ata_addr = (cfis->lba_low << CRQB_ADDR_LBA_LOWSHIFT) &
615 CRQB_ADDR_LBA_LOWMASK;
616 req->ata_addr |= (cfis->lba_mid << CRQB_ADDR_LBA_MIDSHIFT) &
617 CRQB_ADDR_LBA_MIDMASK;
618 req->ata_addr |= (cfis->lba_high << CRQB_ADDR_LBA_HIGHSHIFT) &
619 CRQB_ADDR_LBA_HIGHMASK;
620 req->ata_addr |= (cfis->device << CRQB_ADDR_DEVICE_SHIFT) &
621 CRQB_ADDR_DEVICE_MASK;
622
623 req->ata_addr_exp = (cfis->lba_low_exp << CRQB_ADDR_LBA_LOW_EXP_SHIFT) &
624 CRQB_ADDR_LBA_LOW_EXP_MASK;
625 req->ata_addr_exp |=
626 (cfis->lba_mid_exp << CRQB_ADDR_LBA_MID_EXP_SHIFT) &
627 CRQB_ADDR_LBA_MID_EXP_MASK;
628 req->ata_addr_exp |=
629 (cfis->lba_high_exp << CRQB_ADDR_LBA_HIGH_EXP_SHIFT) &
630 CRQB_ADDR_LBA_HIGH_EXP_MASK;
631 req->ata_addr_exp |=
632 (cfis->features_exp << CRQB_ADDR_FEATURE_EXP_SHIFT) &
633 CRQB_ADDR_FEATURE_EXP_MASK;
634
635 req->ata_sect_count =
636 (cfis->sector_count << CRQB_SECTCOUNT_COUNT_SHIFT) &
637 CRQB_SECTCOUNT_COUNT_MASK;
638 req->ata_sect_count |=
639 (cfis->sector_count_exp << CRQB_SECTCOUNT_COUNT_EXP_SHIFT) &
640 CRQB_SECTCOUNT_COUNT_EXP_MASK;
641
642 /* Flush data */
Stefan Roese9a62bf12016-11-18 17:21:51 +0100643 start = (u32)req & ~(ARCH_DMA_MINALIGN - 1);
644 flush_dcache_range(start,
645 start + ALIGN(sizeof(*req), ARCH_DMA_MINALIGN));
Tor Krillf65e82b2015-12-03 12:38:02 +0100646
647 /* Trigger operation */
Stefan Roesee85955c2019-03-11 13:29:20 +0100648 slot = get_next_reqip(dev, port);
649 set_reqip(dev, port, slot);
Tor Krillf65e82b2015-12-03 12:38:02 +0100650
651 /* Wait for completion */
Stefan Roesee85955c2019-03-11 13:29:20 +0100652 if (wait_dma_completion(dev, port, slot, 10000)) {
Tor Krillf65e82b2015-12-03 12:38:02 +0100653 printf("ATA operation timed out\n");
654 return -1;
655 }
656
Stefan Roesee85955c2019-03-11 13:29:20 +0100657 process_responses(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100658
659 /* Invalidate data on read */
Stefan Roese9a62bf12016-11-18 17:21:51 +0100660 if (buffer && len) {
661 start = (u32)buffer & ~(ARCH_DMA_MINALIGN - 1);
662 invalidate_dcache_range(start,
663 start + ALIGN(len, ARCH_DMA_MINALIGN));
664 }
Tor Krillf65e82b2015-12-03 12:38:02 +0100665
666 return len;
667}
668
Stefan Roesee85955c2019-03-11 13:29:20 +0100669static u32 mv_sata_rw_cmd_ext(struct udevice *dev, int port, lbaint_t start,
670 u32 blkcnt,
Tor Krillf65e82b2015-12-03 12:38:02 +0100671 u8 *buffer, int is_write)
672{
673 struct sata_fis_h2d cfis;
674 u32 res;
675 u64 block;
676
677 block = (u64)start;
678
679 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
680
681 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
682 cfis.command = (is_write) ? ATA_CMD_WRITE_EXT : ATA_CMD_READ_EXT;
683
684 cfis.lba_high_exp = (block >> 40) & 0xff;
685 cfis.lba_mid_exp = (block >> 32) & 0xff;
686 cfis.lba_low_exp = (block >> 24) & 0xff;
687 cfis.lba_high = (block >> 16) & 0xff;
688 cfis.lba_mid = (block >> 8) & 0xff;
689 cfis.lba_low = block & 0xff;
690 cfis.device = ATA_LBA;
691 cfis.sector_count_exp = (blkcnt >> 8) & 0xff;
692 cfis.sector_count = blkcnt & 0xff;
693
Stefan Roesee85955c2019-03-11 13:29:20 +0100694 res = mv_ata_exec_ata_cmd(dev, port, &cfis, buffer,
695 ATA_SECT_SIZE * blkcnt, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100696
697 return res >= 0 ? blkcnt : res;
698}
699
Stefan Roesee85955c2019-03-11 13:29:20 +0100700static u32 mv_sata_rw_cmd(struct udevice *dev, int port, lbaint_t start,
701 u32 blkcnt, u8 *buffer, int is_write)
Tor Krillf65e82b2015-12-03 12:38:02 +0100702{
703 struct sata_fis_h2d cfis;
704 lbaint_t block;
705 u32 res;
706
707 block = start;
708
709 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
710
711 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
712 cfis.command = (is_write) ? ATA_CMD_WRITE : ATA_CMD_READ;
713 cfis.device = ATA_LBA;
714
715 cfis.device |= (block >> 24) & 0xf;
716 cfis.lba_high = (block >> 16) & 0xff;
717 cfis.lba_mid = (block >> 8) & 0xff;
718 cfis.lba_low = block & 0xff;
719 cfis.sector_count = (u8)(blkcnt & 0xff);
720
Stefan Roesee85955c2019-03-11 13:29:20 +0100721 res = mv_ata_exec_ata_cmd(dev, port, &cfis, buffer,
722 ATA_SECT_SIZE * blkcnt, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100723
724 return res >= 0 ? blkcnt : res;
725}
726
Stefan Roesee85955c2019-03-11 13:29:20 +0100727static u32 ata_low_level_rw(struct udevice *dev, int port, lbaint_t blknr,
728 lbaint_t blkcnt, void *buffer, int is_write)
Tor Krillf65e82b2015-12-03 12:38:02 +0100729{
Stefan Roesee85955c2019-03-11 13:29:20 +0100730 struct blk_desc *desc = dev_get_uclass_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100731 lbaint_t start, blks;
732 u8 *addr;
733 int max_blks;
734
Michael Walle9d49a1e2019-04-03 23:28:28 +0200735 debug("%s: " LBAFU " " LBAFU "\n", __func__, blknr, blkcnt);
Tor Krillf65e82b2015-12-03 12:38:02 +0100736
737 start = blknr;
738 blks = blkcnt;
739 addr = (u8 *)buffer;
740
741 max_blks = MV_ATA_MAX_SECTORS;
742 do {
743 if (blks > max_blks) {
Stefan Roesee85955c2019-03-11 13:29:20 +0100744 if (desc->lba48) {
745 mv_sata_rw_cmd_ext(dev, port, start, max_blks,
746 addr, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100747 } else {
Stefan Roesee85955c2019-03-11 13:29:20 +0100748 mv_sata_rw_cmd(dev, port, start, max_blks,
749 addr, is_write);
Tor Krillf65e82b2015-12-03 12:38:02 +0100750 }
751 start += max_blks;
752 blks -= max_blks;
753 addr += ATA_SECT_SIZE * max_blks;
754 } else {
Stefan Roesee85955c2019-03-11 13:29:20 +0100755 if (desc->lba48) {
756 mv_sata_rw_cmd_ext(dev, port, start, blks, addr,
Tor Krillf65e82b2015-12-03 12:38:02 +0100757 is_write);
758 } else {
Stefan Roesee85955c2019-03-11 13:29:20 +0100759 mv_sata_rw_cmd(dev, port, start, blks, addr,
Tor Krillf65e82b2015-12-03 12:38:02 +0100760 is_write);
761 }
762 start += blks;
763 blks = 0;
764 addr += ATA_SECT_SIZE * blks;
765 }
766 } while (blks != 0);
767
768 return blkcnt;
769}
770
Stefan Roesee85955c2019-03-11 13:29:20 +0100771static int mv_ata_exec_ata_cmd_nondma(struct udevice *dev, int port,
Tor Krillf65e82b2015-12-03 12:38:02 +0100772 struct sata_fis_h2d *cfis, u8 *buffer,
773 u32 len, u32 iswrite)
774{
Stefan Roesee85955c2019-03-11 13:29:20 +0100775 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100776 int i;
777 u16 *tp;
778
779 debug("%s\n", __func__);
780
781 out_le32(priv->regbase + PIO_SECTOR_COUNT, cfis->sector_count);
782 out_le32(priv->regbase + PIO_LBA_HI, cfis->lba_high);
783 out_le32(priv->regbase + PIO_LBA_MID, cfis->lba_mid);
784 out_le32(priv->regbase + PIO_LBA_LOW, cfis->lba_low);
785 out_le32(priv->regbase + PIO_ERR_FEATURES, cfis->features);
786 out_le32(priv->regbase + PIO_DEVICE, cfis->device);
787 out_le32(priv->regbase + PIO_CMD_STATUS, cfis->command);
788
789 if (ata_wait_register((u32 *)(priv->regbase + PIO_CMD_STATUS),
790 ATA_BUSY, 0x0, 10000)) {
791 debug("Failed to wait for completion\n");
792 return -1;
793 }
794
795 if (len > 0) {
796 tp = (u16 *)buffer;
797 for (i = 0; i < len / 2; i++) {
798 if (iswrite)
799 out_le16(priv->regbase + PIO_DATA, *tp++);
800 else
801 *tp++ = in_le16(priv->regbase + PIO_DATA);
802 }
803 }
804
805 return len;
806}
807
Stefan Roesee85955c2019-03-11 13:29:20 +0100808static int mv_sata_identify(struct udevice *dev, int port, u16 *id)
Tor Krillf65e82b2015-12-03 12:38:02 +0100809{
810 struct sata_fis_h2d h2d;
811
812 memset(&h2d, 0, sizeof(struct sata_fis_h2d));
813
814 h2d.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
815 h2d.command = ATA_CMD_ID_ATA;
816
817 /* Give device time to get operational */
818 mdelay(10);
819
Stefan Roesee85955c2019-03-11 13:29:20 +0100820 return mv_ata_exec_ata_cmd_nondma(dev, port, &h2d, (u8 *)id,
Tor Krillf65e82b2015-12-03 12:38:02 +0100821 ATA_ID_WORDS * 2, READ_CMD);
822}
823
Stefan Roesee85955c2019-03-11 13:29:20 +0100824static void mv_sata_xfer_mode(struct udevice *dev, int port, u16 *id)
Tor Krillf65e82b2015-12-03 12:38:02 +0100825{
Stefan Roesee85955c2019-03-11 13:29:20 +0100826 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100827
828 priv->pio = id[ATA_ID_PIO_MODES];
829 priv->mwdma = id[ATA_ID_MWDMA_MODES];
830 priv->udma = id[ATA_ID_UDMA_MODES];
831 debug("pio %04x, mwdma %04x, udma %04x\n", priv->pio, priv->mwdma,
832 priv->udma);
833}
834
Stefan Roesee85955c2019-03-11 13:29:20 +0100835static void mv_sata_set_features(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100836{
Stefan Roesee85955c2019-03-11 13:29:20 +0100837 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100838 struct sata_fis_h2d cfis;
839 u8 udma_cap;
840
841 memset(&cfis, 0, sizeof(struct sata_fis_h2d));
842
843 cfis.fis_type = SATA_FIS_TYPE_REGISTER_H2D;
844 cfis.command = ATA_CMD_SET_FEATURES;
845 cfis.features = SETFEATURES_XFER;
846
847 /* First check the device capablity */
848 udma_cap = (u8) (priv->udma & 0xff);
849
850 if (udma_cap == ATA_UDMA6)
851 cfis.sector_count = XFER_UDMA_6;
852 if (udma_cap == ATA_UDMA5)
853 cfis.sector_count = XFER_UDMA_5;
854 if (udma_cap == ATA_UDMA4)
855 cfis.sector_count = XFER_UDMA_4;
856 if (udma_cap == ATA_UDMA3)
857 cfis.sector_count = XFER_UDMA_3;
858
Stefan Roesee85955c2019-03-11 13:29:20 +0100859 mv_ata_exec_ata_cmd_nondma(dev, port, &cfis, NULL, 0, READ_CMD);
Tor Krillf65e82b2015-12-03 12:38:02 +0100860}
861
862/*
863 * Initialize SATA memory windows
864 */
865static void mvsata_ide_conf_mbus_windows(void)
866{
867 const struct mbus_dram_target_info *dram;
868 int i;
869
870 dram = mvebu_mbus_dram_info();
871
872 /* Disable windows, Set Size/Base to 0 */
873 for (i = 0; i < 4; i++) {
874 writel(0, MVSATA_WIN_CONTROL(i));
875 writel(0, MVSATA_WIN_BASE(i));
876 }
877
878 for (i = 0; i < dram->num_cs; i++) {
879 const struct mbus_dram_window *cs = dram->cs + i;
880 writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) |
881 (dram->mbus_dram_target_id << 4) | 1,
882 MVSATA_WIN_CONTROL(i));
883 writel(cs->base & 0xffff0000, MVSATA_WIN_BASE(i));
884 }
885}
886
Stefan Roesee85955c2019-03-11 13:29:20 +0100887static int sata_mv_init_sata(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100888{
Stefan Roesee85955c2019-03-11 13:29:20 +0100889 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100890
Stefan Roesee85955c2019-03-11 13:29:20 +0100891 debug("Initialize sata dev: %d\n", port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100892
Stefan Roesee85955c2019-03-11 13:29:20 +0100893 if (port < 0 || port >= CONFIG_SYS_SATA_MAX_DEVICE) {
894 printf("Invalid sata device %d\n", port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100895 return -1;
896 }
897
Tor Krillf65e82b2015-12-03 12:38:02 +0100898 /* Allocate and align request buffer */
899 priv->crqb_alloc = malloc(sizeof(struct crqb) * REQUEST_QUEUE_SIZE +
900 CRQB_ALIGN);
901 if (!priv->crqb_alloc) {
902 printf("Unable to allocate memory for request queue\n");
903 return -ENOMEM;
904 }
905 memset(priv->crqb_alloc, 0,
906 sizeof(struct crqb) * REQUEST_QUEUE_SIZE + CRQB_ALIGN);
907 priv->request = (struct crqb *)(((u32) priv->crqb_alloc + CRQB_ALIGN) &
908 ~(CRQB_ALIGN - 1));
909
910 /* Allocate and align response buffer */
911 priv->crpb_alloc = malloc(sizeof(struct crpb) * REQUEST_QUEUE_SIZE +
912 CRPB_ALIGN);
913 if (!priv->crpb_alloc) {
914 printf("Unable to allocate memory for response queue\n");
915 return -ENOMEM;
916 }
917 memset(priv->crpb_alloc, 0,
918 sizeof(struct crpb) * REQUEST_QUEUE_SIZE + CRPB_ALIGN);
919 priv->response = (struct crpb *)(((u32) priv->crpb_alloc + CRPB_ALIGN) &
920 ~(CRPB_ALIGN - 1));
921
Stefan Roesee85955c2019-03-11 13:29:20 +0100922 sprintf(priv->name, "SATA%d", port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100923
Stefan Roesee85955c2019-03-11 13:29:20 +0100924 priv->regbase = port == 0 ? SATA0_BASE : SATA1_BASE;
Tor Krillf65e82b2015-12-03 12:38:02 +0100925
926 if (!hw_init) {
927 debug("Initialize sata hw\n");
928 hw_init = 1;
929 mv_reset_one_hc();
930 mvsata_ide_conf_mbus_windows();
931 }
932
Stefan Roesee85955c2019-03-11 13:29:20 +0100933 mv_reset_port(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100934
Stefan Roesee85955c2019-03-11 13:29:20 +0100935 if (probe_port(dev, port)) {
Tor Krillf65e82b2015-12-03 12:38:02 +0100936 priv->link = 0;
937 return -ENODEV;
938 }
939 priv->link = 1;
940
941 return 0;
942}
943
Stefan Roesee85955c2019-03-11 13:29:20 +0100944static int sata_mv_scan_sata(struct udevice *dev, int port)
Tor Krillf65e82b2015-12-03 12:38:02 +0100945{
Stefan Roesee85955c2019-03-11 13:29:20 +0100946 struct blk_desc *desc = dev_get_uclass_platdata(dev);
947 struct mv_priv *priv = dev_get_platdata(dev);
Tor Krillf65e82b2015-12-03 12:38:02 +0100948 unsigned char serial[ATA_ID_SERNO_LEN + 1];
949 unsigned char firmware[ATA_ID_FW_REV_LEN + 1];
950 unsigned char product[ATA_ID_PROD_LEN + 1];
951 u64 n_sectors;
952 u16 *id;
Tor Krillf65e82b2015-12-03 12:38:02 +0100953
954 if (!priv->link)
955 return -ENODEV;
956
957 id = (u16 *)malloc(ATA_ID_WORDS * 2);
958 if (!id) {
959 printf("Failed to malloc id data\n");
960 return -ENOMEM;
961 }
962
Stefan Roesee85955c2019-03-11 13:29:20 +0100963 mv_sata_identify(dev, port, id);
Tor Krillf65e82b2015-12-03 12:38:02 +0100964 ata_swap_buf_le16(id, ATA_ID_WORDS);
965#ifdef DEBUG
966 ata_dump_id(id);
967#endif
968
969 /* Serial number */
970 ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
Stefan Roesee85955c2019-03-11 13:29:20 +0100971 memcpy(desc->product, serial, sizeof(serial));
Tor Krillf65e82b2015-12-03 12:38:02 +0100972
973 /* Firmware version */
974 ata_id_c_string(id, firmware, ATA_ID_FW_REV, sizeof(firmware));
Stefan Roesee85955c2019-03-11 13:29:20 +0100975 memcpy(desc->revision, firmware, sizeof(firmware));
Tor Krillf65e82b2015-12-03 12:38:02 +0100976
977 /* Product model */
978 ata_id_c_string(id, product, ATA_ID_PROD, sizeof(product));
Stefan Roesee85955c2019-03-11 13:29:20 +0100979 memcpy(desc->vendor, product, sizeof(product));
Tor Krillf65e82b2015-12-03 12:38:02 +0100980
981 /* Total sectors */
982 n_sectors = ata_id_n_sectors(id);
Stefan Roesee85955c2019-03-11 13:29:20 +0100983 desc->lba = n_sectors;
Tor Krillf65e82b2015-12-03 12:38:02 +0100984
985 /* Check if support LBA48 */
986 if (ata_id_has_lba48(id)) {
Stefan Roesee85955c2019-03-11 13:29:20 +0100987 desc->lba48 = 1;
Tor Krillf65e82b2015-12-03 12:38:02 +0100988 debug("Device support LBA48\n");
989 }
990
991 /* Get the NCQ queue depth from device */
992 priv->queue_depth = ata_id_queue_depth(id);
993
994 /* Get the xfer mode from device */
Stefan Roesee85955c2019-03-11 13:29:20 +0100995 mv_sata_xfer_mode(dev, port, id);
Tor Krillf65e82b2015-12-03 12:38:02 +0100996
997 /* Set the xfer mode to highest speed */
Stefan Roesee85955c2019-03-11 13:29:20 +0100998 mv_sata_set_features(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +0100999
1000 /* Start up */
Stefan Roesee85955c2019-03-11 13:29:20 +01001001 mv_start_edma_engine(dev, port);
Tor Krillf65e82b2015-12-03 12:38:02 +01001002
1003 return 0;
1004}
Stefan Roesee85955c2019-03-11 13:29:20 +01001005
1006static ulong sata_mv_read(struct udevice *blk, lbaint_t blknr,
1007 lbaint_t blkcnt, void *buffer)
1008{
1009 struct mv_priv *priv = dev_get_platdata(blk);
1010
1011 return ata_low_level_rw(blk, priv->dev_nr, blknr, blkcnt,
1012 buffer, READ_CMD);
1013}
1014
1015static ulong sata_mv_write(struct udevice *blk, lbaint_t blknr,
1016 lbaint_t blkcnt, const void *buffer)
1017{
1018 struct mv_priv *priv = dev_get_platdata(blk);
1019
1020 return ata_low_level_rw(blk, priv->dev_nr, blknr, blkcnt,
1021 (void *)buffer, WRITE_CMD);
1022}
1023
1024static const struct blk_ops sata_mv_blk_ops = {
1025 .read = sata_mv_read,
1026 .write = sata_mv_write,
1027};
1028
1029U_BOOT_DRIVER(sata_mv_driver) = {
1030 .name = "sata_mv_blk",
1031 .id = UCLASS_BLK,
1032 .ops = &sata_mv_blk_ops,
1033 .platdata_auto_alloc_size = sizeof(struct mv_priv),
1034};
1035
1036static int sata_mv_probe(struct udevice *dev)
1037{
1038 const void *blob = gd->fdt_blob;
1039 int node = dev_of_offset(dev);
1040 struct mv_priv *priv;
1041 struct udevice *blk;
1042 int nr_ports;
1043 int ret;
1044 int i;
1045
1046 /* Get number of ports of this SATA controller */
1047 nr_ports = min(fdtdec_get_int(blob, node, "nr-ports", -1),
1048 CONFIG_SYS_SATA_MAX_DEVICE);
1049
1050 for (i = 0; i < nr_ports; i++) {
1051 ret = blk_create_devicef(dev, "sata_mv_blk", "blk",
1052 IF_TYPE_SATA, -1, 512, 0, &blk);
1053 if (ret) {
1054 debug("Can't create device\n");
1055 return ret;
1056 }
1057
1058 priv = dev_get_platdata(blk);
1059 priv->dev_nr = i;
1060
1061 /* Init SATA port */
1062 ret = sata_mv_init_sata(blk, i);
1063 if (ret) {
1064 debug("%s: Failed to init bus\n", __func__);
1065 return ret;
1066 }
1067
1068 /* Scan SATA port */
1069 ret = sata_mv_scan_sata(blk, i);
1070 if (ret) {
1071 debug("%s: Failed to scan bus\n", __func__);
1072 return ret;
1073 }
1074 }
1075
1076 return 0;
1077}
1078
1079static int sata_mv_scan(struct udevice *dev)
1080{
1081 /* Nothing to do here */
1082
1083 return 0;
1084}
1085
1086static const struct udevice_id sata_mv_ids[] = {
1087 { .compatible = "marvell,armada-370-sata" },
Michael Walle99e92d92019-04-03 23:28:30 +02001088 { .compatible = "marvell,orion-sata" },
Stefan Roesee85955c2019-03-11 13:29:20 +01001089 { }
1090};
1091
1092struct ahci_ops sata_mv_ahci_ops = {
1093 .scan = sata_mv_scan,
1094};
1095
1096U_BOOT_DRIVER(sata_mv_ahci) = {
1097 .name = "sata_mv_ahci",
1098 .id = UCLASS_AHCI,
1099 .of_match = sata_mv_ids,
1100 .ops = &sata_mv_ahci_ops,
1101 .probe = sata_mv_probe,
1102};