xref: /rk3399_rockchip-uboot/drivers/rkflash/rksfc_base.c (revision 83ab7b4937c098a3febc8f361a6be16f28ae16aa)
1 /*
2  * Copyright (c) 2018 Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier: (GPL-2.0+ OR MIT)
5  */
6 
7 #include <common.h>
8 #include <clk.h>
9 #include <dm.h>
10 #include <dm/device-internal.h>
11 #include <asm/arch/clock.h>
12 
13 #include "rkflash_blk.h"
14 #include "rkflash_api.h"
15 
16 static struct flash_operation spi_flash_op = {
17 #ifdef	CONFIG_RKSFC_NOR
18 	FLASH_TYPE_SFC_NOR,
19 	rk_snor_init,
20 	rk_snor_get_capacity,
21 	rk_snor_read,
22 	rk_snor_write,
23 #else
24 	-1, NULL, NULL, NULL, NULL,
25 #endif
26 };
27 
28 int rksfc_scan_namespace(void)
29 {
30 	struct uclass *uc;
31 	struct udevice *dev;
32 	int ret;
33 
34 	ret = uclass_get(UCLASS_SPI_FLASH, &uc);
35 	if (ret)
36 		return ret;
37 
38 	uclass_foreach_dev(dev, uc) {
39 		debug("%s %d %p\n", __func__, __LINE__, dev);
40 		ret = device_probe(dev);
41 		if (ret)
42 			return ret;
43 	}
44 
45 	return 0;
46 }
47 
48 static int rksfc_blk_bind(struct udevice *udev)
49 {
50 	struct udevice *bdev;
51 	int ret;
52 
53 	ret = blk_create_devicef(udev, "rkflash_blk", "blk",
54 				 IF_TYPE_RKSFC,
55 				 0, 512, 0, &bdev);
56 	if (ret) {
57 		debug("Cannot create block device\n");
58 		return ret;
59 	}
60 
61 	return 0;
62 }
63 
64 static int rockchip_rksfc_ofdata_to_platdata(struct udevice *dev)
65 {
66 	struct rkflash_info *priv = dev_get_priv(dev);
67 
68 	priv->ioaddr = dev_read_addr_ptr(dev);
69 
70 	return 0;
71 }
72 
73 static int rockchip_rksfc_probe(struct udevice *udev)
74 {
75 	int ret;
76 	struct rkflash_info *priv = dev_get_priv(udev);
77 
78 	debug("%s %d %p ndev = %p\n", __func__, __LINE__, udev, priv);
79 
80 	sfc_init(priv->ioaddr);
81 	if (spi_flash_op.id == -1) {
82 		debug("%s no optional spi flash\n", __func__);
83 		return 0;
84 	}
85 	ret = spi_flash_op.flash_init(udev);
86 	if (!ret) {
87 		priv->flash_con_type = FLASH_CON_TYPE_SFC;
88 		priv->density = spi_flash_op.flash_get_capacity(udev);
89 		priv->read = spi_flash_op.flash_read;
90 		priv->write = spi_flash_op.flash_write;
91 	}
92 
93 	return ret;
94 }
95 
96 UCLASS_DRIVER(rksfc) = {
97 	.id		= UCLASS_SPI_FLASH,
98 	.name		= "rksfc",
99 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
100 };
101 
102 static const struct udevice_id rockchip_sfc_ids[] = {
103 	{ .compatible = "rockchip,rksfc" },
104 	{ }
105 };
106 
107 U_BOOT_DRIVER(rksfc) = {
108 	.name		= "rksfc",
109 	.id		= UCLASS_SPI_FLASH,
110 	.of_match	= rockchip_sfc_ids,
111 	.bind		= rksfc_blk_bind,
112 	.probe		= rockchip_rksfc_probe,
113 	.priv_auto_alloc_size = sizeof(struct rkflash_info),
114 	.ofdata_to_platdata = rockchip_rksfc_ofdata_to_platdata,
115 };
116 
117