[U-Boot] [RFC 3/3] ata: fsl_sata: Add sata DM support for Freescale powerpc socs

Peng Ma peng.ma at nxp.com
Wed Sep 25 09:54:19 UTC 2019


This sata is a None AHCI sata device. To support DM mode with SCSI interface
we use the SCSI_NONE_AHCI.

This patch is an example(normal sata) to fit "support SCSI interface for None AHCI sata"

Signed-off-by: Peng Ma <peng.ma at nxp.com>
---
 drivers/ata/fsl_sata.c | 300 +++++++++++++++++++++++++++++++++----------------
 drivers/ata/fsl_sata.h |   9 ++
 2 files changed, 211 insertions(+), 98 deletions(-)

diff --git a/drivers/ata/fsl_sata.c b/drivers/ata/fsl_sata.c
index e70a515..7bfeb9b 100644
--- a/drivers/ata/fsl_sata.c
+++ b/drivers/ata/fsl_sata.c
@@ -15,7 +15,11 @@
 #include <fis.h>
 #include <sata.h>
 #include "fsl_sata.h"
-
+#if CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
+#include "scsi_ata.h"
+#include <scsi.h>
+#include <dm.h>
+#else
 #ifndef CONFIG_SYS_SATA1_FLAGS
 	#define CONFIG_SYS_SATA1_FLAGS	FLAGS_DMA
 #endif
@@ -35,6 +39,7 @@ static struct fsl_sata_info fsl_sata_info[] = {
 	{0, 0},
 #endif
 };
+#endif
 
 static inline void sdelay(unsigned long sec)
 {
@@ -74,7 +79,11 @@ static int ata_wait_register(unsigned __iomem *addr, u32 mask,
 	return (i < timeout_msec) ? 0 : -1;
 }
 
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
 int init_sata(int dev)
+#else
+static int init_sata(struct fsl_ata_priv *priv)
+#endif
 {
 	u32 length, align;
 	cmd_hdr_tbl_t *cmd_hdr;
@@ -85,6 +94,10 @@ int init_sata(int dev)
 	int i;
 	fsl_sata_t *sata;
 
+#if CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
+	int dev = priv->number;
+#endif
+
 	if (dev < 0 || dev > (CONFIG_SYS_SATA_MAX_DEVICE - 1)) {
 		printf("the sata index %d is out of ranges\n\r", dev);
 		return -1;
@@ -110,13 +123,17 @@ int init_sata(int dev)
 	/* Zero all of the device driver struct */
 	memset((void *)sata, 0, sizeof(fsl_sata_t));
 
-	/* Save the private struct to block device struct */
-	sata_dev_desc[dev].priv = (void *)sata;
-
 	snprintf(sata->name, 12, "SATA%d", dev);
 
 	/* Set the controller register base address to device struct */
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
+	sata_dev_desc[dev].priv = (void *)sata;
 	reg = (fsl_sata_reg_t *)(fsl_sata_info[dev].sata_reg_base);
+	sata->dma_flag = fsl_sata_info[dev].flags;
+#else
+	reg = (fsl_sata_reg_t *)priv->base;
+	sata->dma_flag = priv->flag;
+#endif
 	sata->reg_base = reg;
 
 	/* Allocate the command header table, 4 bytes aligned */
@@ -252,6 +269,9 @@ int init_sata(int dev)
 	else if ((val32 & SSTATUS_SPD_MASK) == SSTATUS_SPD_GEN2)
 		printf("(3 Gbps)\n\r");
 
+#if CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
+	priv->fsl_sata = sata;
+#endif
 	return 0;
 }
 
@@ -479,34 +499,16 @@ static int fsl_sata_exec_cmd(struct fsl_sata *sata, struct sata_fis_h2d *cfis,
 	return -1;
 }
 
-static void fsl_sata_identify(int dev, u16 *id)
+static void fsl_sata_xfer_mode(fsl_sata_t *sata, u16 *id)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
-	struct sata_fis_h2d h2d, *cfis = &h2d;
-
-	memset(cfis, 0, sizeof(struct sata_fis_h2d));
-
-	cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
-	cfis->pm_port_c = 0x80; /* is command */
-	cfis->command = ATA_CMD_ID_ATA;
-
-	fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
-	ata_swap_buf_le16(id, ATA_ID_WORDS);
-}
-
-static void fsl_sata_xfer_mode(int dev, u16 *id)
-{
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
-
 	sata->pio = id[ATA_ID_PIO_MODES];
 	sata->mwdma = id[ATA_ID_MWDMA_MODES];
 	sata->udma = id[ATA_ID_UDMA_MODES];
 	debug("pio %04x, mwdma %04x, udma %04x\n\r", sata->pio, sata->mwdma, sata->udma);
 }
 
-static void fsl_sata_set_features(int dev)
+static void fsl_sata_set_features(fsl_sata_t *sata)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
 	struct sata_fis_h2d h2d, *cfis = &h2d;
 	u8 udma_cap;
 
@@ -533,9 +535,9 @@ static void fsl_sata_set_features(int dev)
 	fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
 }
 
-static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
+static u32 fsl_sata_rw_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt, u8 *buffer,
+			   int is_write)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
 	struct sata_fis_h2d h2d, *cfis = &h2d;
 	u32 block;
 
@@ -558,9 +560,8 @@ static u32 fsl_sata_rw_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_wr
 	return blkcnt;
 }
 
-static void fsl_sata_flush_cache(int dev)
+static void fsl_sata_flush_cache(fsl_sata_t *sata)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
 	struct sata_fis_h2d h2d, *cfis = &h2d;
 
 	memset(cfis, 0, sizeof(struct sata_fis_h2d));
@@ -572,9 +573,9 @@ static void fsl_sata_flush_cache(int dev)
 	fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
 }
 
-static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int is_write)
+static u32 fsl_sata_rw_cmd_ext(fsl_sata_t *sata, u32 start, u32 blkcnt,
+			       u8 *buffer, int is_write)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
 	struct sata_fis_h2d h2d, *cfis = &h2d;
 	u64 block;
 
@@ -602,10 +603,9 @@ static u32 fsl_sata_rw_cmd_ext(int dev, u32 start, u32 blkcnt, u8 *buffer, int i
 	return blkcnt;
 }
 
-static u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer,
-			       int is_write)
+static u32 fsl_sata_rw_ncq_cmd(fsl_sata_t *sata, u32 start, u32 blkcnt,
+			       u8 *buffer, int is_write)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
 	struct sata_fis_h2d h2d, *cfis = &h2d;
 	int ncq_channel;
 	u64 block;
@@ -646,9 +646,8 @@ static u32 fsl_sata_rw_ncq_cmd(int dev, u32 start, u32 blkcnt, u8 *buffer,
 	return blkcnt;
 }
 
-static void fsl_sata_flush_cache_ext(int dev)
+static void fsl_sata_flush_cache_ext(fsl_sata_t *sata)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
 	struct sata_fis_h2d h2d, *cfis = &h2d;
 
 	memset(cfis, 0, sizeof(struct sata_fis_h2d));
@@ -660,10 +659,8 @@ static void fsl_sata_flush_cache_ext(int dev)
 	fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, NULL, 0);
 }
 
-static void fsl_sata_init_wcache(int dev, u16 *id)
+static void fsl_sata_init_wcache(fsl_sata_t *sata, u16 *id)
 {
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
-
 	if (ata_id_has_wcache(id) && ata_id_wcache_enabled(id))
 		sata->wcache = 1;
 	if (ata_id_has_flush(id))
@@ -672,26 +669,8 @@ static void fsl_sata_init_wcache(int dev, u16 *id)
 		sata->flush_ext = 1;
 }
 
-static int fsl_sata_get_wcache(int dev)
-{
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
-	return sata->wcache;
-}
-
-static int fsl_sata_get_flush(int dev)
-{
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
-	return sata->flush;
-}
-
-static int fsl_sata_get_flush_ext(int dev)
-{
-	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
-	return sata->flush_ext;
-}
-
-static u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
-		const void *buffer, int is_write)
+static u32 ata_low_level_rw_lba48(fsl_sata_t *sata, u32 blknr, lbaint_t blkcnt,
+				  const void *buffer, int is_write)
 {
 	u32 start, blks;
 	u8 *addr;
@@ -704,28 +683,36 @@ static u32 ata_low_level_rw_lba48(int dev, u32 blknr, lbaint_t blkcnt,
 	max_blks = ATA_MAX_SECTORS_LBA48;
 	do {
 		if (blks > max_blks) {
-			if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
-				fsl_sata_rw_cmd_ext(dev, start, max_blks, addr, is_write);
+			if (sata->dma_flag != FLAGS_FPDMA)
+				fsl_sata_rw_cmd_ext(sata, start, max_blks, addr,
+						    is_write);
 			else
-				fsl_sata_rw_ncq_cmd(dev, start, max_blks, addr, is_write);
+				fsl_sata_rw_ncq_cmd(sata, start, max_blks, addr,
+						    is_write);
 			start += max_blks;
 			blks -= max_blks;
 			addr += ATA_SECT_SIZE * max_blks;
 		} else {
-			if (fsl_sata_info[dev].flags != FLAGS_FPDMA)
-				fsl_sata_rw_cmd_ext(dev, start, blks, addr, is_write);
+			if (sata->dma_flag != FLAGS_FPDMA)
+				fsl_sata_rw_cmd_ext(sata, start, blks, addr,
+						    is_write);
 			else
-				fsl_sata_rw_ncq_cmd(dev, start, blks, addr, is_write);
+				fsl_sata_rw_ncq_cmd(sata, start, blks, addr,
+						    is_write);
 			start += blks;
 			blks = 0;
 			addr += ATA_SECT_SIZE * blks;
 		}
 	} while (blks != 0);
 
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
 	return blkcnt;
+#else
+	return blks;
+#endif
 }
 
-static u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt,
+static u32 ata_low_level_rw_lba28(fsl_sata_t *sata, u32 blknr, u32 blkcnt,
 				  const void *buffer, int is_write)
 {
 	u32 start, blks;
@@ -739,53 +726,132 @@ static u32 ata_low_level_rw_lba28(int dev, u32 blknr, u32 blkcnt,
 	max_blks = ATA_MAX_SECTORS;
 	do {
 		if (blks > max_blks) {
-			fsl_sata_rw_cmd(dev, start, max_blks, addr, is_write);
+			fsl_sata_rw_cmd(sata, start, max_blks, addr, is_write);
 			start += max_blks;
 			blks -= max_blks;
 			addr += ATA_SECT_SIZE * max_blks;
 		} else {
-			fsl_sata_rw_cmd(dev, start, blks, addr, is_write);
+			fsl_sata_rw_cmd(sata, start, blks, addr, is_write);
 			start += blks;
 			blks = 0;
 			addr += ATA_SECT_SIZE * blks;
 		}
 	} while (blks != 0);
 
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
 	return blkcnt;
+#else
+	return blks;
+#endif
 }
 
 /*
  * SATA interface between low level driver and command layer
  */
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
 ulong sata_read(int dev, ulong blknr, lbaint_t blkcnt, void *buffer)
 {
-	u32 rc;
 	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
+#else
+ulong sata_read(struct udevice *dev, lbaint_t blknr, u16 blkcnt,
+		void *buffer, u8 port)
+{
+	struct fsl_ata_priv *priv = dev_get_priv(dev);
+	fsl_sata_t *sata = priv->fsl_sata;
+#endif
+	u32 rc;
 
 	if (sata->lba48)
-		rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, READ_CMD);
+		rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
+					    READ_CMD);
 	else
-		rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, READ_CMD);
+		rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
+					    READ_CMD);
 	return rc;
 }
 
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
 ulong sata_write(int dev, ulong blknr, lbaint_t blkcnt, const void *buffer)
 {
-	u32 rc;
 	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
+#else
+ulong sata_write(struct udevice *dev, lbaint_t blknr, u16 blkcnt,
+		 const void *buffer, u8 port)
+{
+	struct fsl_ata_priv *priv = dev_get_priv(dev);
+	fsl_sata_t *sata = priv->fsl_sata;
+#endif
+	u32 rc;
 
 	if (sata->lba48) {
-		rc = ata_low_level_rw_lba48(dev, blknr, blkcnt, buffer, WRITE_CMD);
-		if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush_ext(dev))
-			fsl_sata_flush_cache_ext(dev);
+		rc = ata_low_level_rw_lba48(sata, blknr, blkcnt, buffer,
+					    WRITE_CMD);
+		if (sata->wcache && sata->flush_ext)
+			fsl_sata_flush_cache_ext(sata);
 	} else {
-		rc = ata_low_level_rw_lba28(dev, blknr, blkcnt, buffer, WRITE_CMD);
-		if (fsl_sata_get_wcache(dev) && fsl_sata_get_flush(dev))
-			fsl_sata_flush_cache(dev);
+		rc = ata_low_level_rw_lba28(sata, blknr, blkcnt, buffer,
+					    WRITE_CMD);
+		if (sata->wcache && sata->flush)
+			fsl_sata_flush_cache(sata);
 	}
 	return rc;
 }
 
+static int sata_getinfo(fsl_sata_t *sata, u16 *id)
+{
+	/* if no detected link */
+	if (!sata->link)
+		return -EINVAL;
+
+#ifdef CONFIG_LBA48
+	/* Check if support LBA48 */
+	if (ata_id_has_lba48(id)) {
+		sata->lba48 = 1;
+		debug("Device support LBA48\n\r");
+	} else {
+		debug("Device supports LBA28\n\r");
+	}
+#endif
+
+	/* Get the NCQ queue depth from device */
+	sata->queue_depth = ata_id_queue_depth(id);
+
+	/* Get the xfer mode from device */
+	fsl_sata_xfer_mode(sata, id);
+
+	/* Get the write cache status from device */
+	fsl_sata_init_wcache(sata, id);
+
+	/* Set the xfer mode to highest speed */
+	fsl_sata_set_features(sata);
+
+	return 0;
+}
+
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
+static int fsl_sata_identify(fsl_sata_t *sata, u16 *id)
+{
+#else
+static int fsl_sata_identify(struct udevice *dev, u16 *id, u8 port)
+{
+	struct fsl_ata_priv *priv = dev_get_priv(dev);
+	fsl_sata_t *sata = priv->fsl_sata;
+#endif
+	struct sata_fis_h2d h2d, *cfis = &h2d;
+
+	memset(cfis, 0, sizeof(struct sata_fis_h2d));
+
+	cfis->fis_type = SATA_FIS_TYPE_REGISTER_H2D;
+	cfis->pm_port_c = 0x80; /* is command */
+	cfis->command = ATA_CMD_ID_ATA;
+
+	fsl_sata_exec_cmd(sata, cfis, CMD_ATA, 0, (u8 *)id, ATA_ID_WORDS * 2);
+	ata_swap_buf_le16(id, ATA_ID_WORDS);
+
+	return sata_getinfo(sata, (u16 *)id);
+}
+
+#if !CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
 int scan_sata(int dev)
 {
 	fsl_sata_t *sata = (fsl_sata_t *)sata_dev_desc[dev].priv;
@@ -806,7 +872,7 @@ int scan_sata(int dev)
 	}
 
 	/* Identify device to get information */
-	fsl_sata_identify(dev, id);
+	fsl_sata_identify(sata, id);
 
 	/* Serial number */
 	ata_id_c_string(id, serial, ATA_ID_SERNO, sizeof(serial));
@@ -824,30 +890,68 @@ int scan_sata(int dev)
 	n_sectors = ata_id_n_sectors(id);
 	sata_dev_desc[dev].lba = (u32)n_sectors;
 
-#ifdef CONFIG_LBA48
-	/* Check if support LBA48 */
-	if (ata_id_has_lba48(id)) {
-		sata->lba48 = 1;
-		debug("Device support LBA48\n\r");
-	} else
-		debug("Device supports LBA28\n\r");
+#ifdef DEBUG
+	ata_dump_id(id);
 #endif
+	free((void *)id);
+	return 0;
+}
+#else
+static int fsl_bind_ata(struct udevice *dev)
+{
+	struct udevice *scsi_dev;
 
-	/* Get the NCQ queue depth from device */
-	sata->queue_depth = ata_id_queue_depth(id);
+	return ata_bind_scsi(dev, &scsi_dev);
+}
 
-	/* Get the xfer mode from device */
-	fsl_sata_xfer_mode(dev, id);
+static int fsl_ata_ofdata_to_platdata(struct udevice *dev)
+{
+	struct fsl_ata_priv *priv = dev_get_priv(dev);
 
-	/* Get the write cache status from device */
-	fsl_sata_init_wcache(dev, id);
+	priv->number = dev_read_u32_default(dev, "sata-number", -1);
+	priv->flag = dev_read_u32_default(dev, "sata-fpdma", -1);
+
+	priv->base = dev_read_addr(dev);
+	if (priv->base == FDT_ADDR_T_NONE)
+		return -EINVAL;
 
-	/* Set the xfer mode to highest speed */
-	fsl_sata_set_features(dev);
-#ifdef DEBUG
-	fsl_sata_identify(dev, id);
-	ata_dump_id(id);
-#endif
-	free((void *)id);
 	return 0;
 }
+
+static int fsl_ata_probe(struct udevice *dev)
+{
+	struct udevice *child_dev;
+	struct scsi_platdata *uc_plat;
+	struct fsl_ata_priv *priv = dev_get_priv(dev);
+	struct ata_uc_priv *uc_priv = dev_get_uclass_priv(dev);
+
+	device_find_first_child(dev, &child_dev);
+	if (!child_dev)
+		return -ENODEV;
+
+	uc_plat = dev_get_uclass_platdata(child_dev);
+	uc_plat->base = priv->base;
+	uc_plat->max_lun = 1;
+	uc_plat->max_id = 1;
+
+	uc_priv->read = sata_read;
+	uc_priv->write = sata_write;
+	uc_priv->ata_inquiry = fsl_sata_identify;
+	return init_sata(priv);
+}
+
+static const struct udevice_id fsl_ata_ids[] = {
+	{ .compatible = "fsl,pq-sata-v2" },
+	{ }
+};
+
+U_BOOT_DRIVER(fsl_ahci) = {
+	.name	= "fsl_ata",
+	.id	= UCLASS_NONE_AHCI,
+	.of_match = fsl_ata_ids,
+	.bind	= fsl_bind_ata,
+	.ofdata_to_platdata = fsl_ata_ofdata_to_platdata,
+	.probe	= fsl_ata_probe,
+	.priv_auto_alloc_size = sizeof(struct fsl_ata_priv),
+};
+#endif
diff --git a/drivers/ata/fsl_sata.h b/drivers/ata/fsl_sata.h
index a4ee83d..1a0165e 100644
--- a/drivers/ata/fsl_sata.h
+++ b/drivers/ata/fsl_sata.h
@@ -318,4 +318,13 @@ typedef struct fsl_sata {
 #define READ_CMD	0
 #define WRITE_CMD	1
 
+#if CONFIG_IS_ENABLED(SCSI_NONE_AHCI)
+struct fsl_ata_priv {
+	u32 base;
+	u32 flag;
+	u32 number;
+	fsl_sata_t *fsl_sata;
+};
+#endif
+
 #endif /* __FSL_SATA_H__ */
-- 
2.9.5



More information about the U-Boot mailing list