一、綜述
本文通過如何通過編寫特定板子的spi master驅(qū)動從而識別到spi norflash設(shè)備,完成norflash設(shè)備的讀寫。
二、UCLASS架構(gòu)解析
2.1 uclass
uclass可以理解為一些具有相同屬性的udevice對外操作的接口,uclass的驅(qū)動是uclass_driver,主要為上層提供接口。
udevice的是指具體設(shè)備的抽象,對應(yīng)驅(qū)動是driver,driver主要負(fù)責(zé)和硬件通信,為uclass提供實(shí)際的操作集。
udevice找到對應(yīng)的uclass的方式主要是通過:udevice對應(yīng)的driver的id和uclass對應(yīng)的uclass_driver的id是否匹配。
udevice會和uclass綁定。driver會和udevice綁定。uclass_driver會和uclass綁定。
uclass和udevice都是動態(tài)生成的。在解析fdt中的設(shè)備的時候,會動態(tài)生成udevice。
然后找到udevice對應(yīng)的driver,通過driver中的uclass id得到uclass_driver id。從uclass鏈表中查找對應(yīng)的uclass是否已經(jīng)生成,沒有生成的話則動態(tài)生成uclass。
2.2 udevice
連接到對應(yīng)uclass中
也就是會連接到uclass->dev_head中
連接到父設(shè)備的子設(shè)備鏈表中
也就是會連接到udevice->child_head中,并且最終的根設(shè)備是gd->dm_root這個根設(shè)備。
struct uclass {
void *priv; //uclass的私有數(shù)據(jù)
struct uclass_driver *uc_drv; //uclass類的操作函數(shù)集合
struct list_head dev_head; //該uclass的所有設(shè)備
struct list_head sibling_node; //下一個uclass的節(jié)點(diǎn)
};
2.3 uclass driver
主要函數(shù):
struct uclass_driver {
const char *name; // 該uclass_driver的命令
enum uclass_id id; // 對應(yīng)的uclass id
/* 以下函數(shù)指針主要是調(diào)用時機(jī)的區(qū)別 */
int (*post_bind)(struct udevice *dev); // 在udevice被綁定到該uclass之后調(diào)用
int (*pre_unbind)(struct udevice *dev); // 在udevice被解綁出該uclass之前調(diào)用
int (*pre_probe)(struct udevice *dev); // 在該uclass的一個udevice進(jìn)行probe之前調(diào)用
int (*post_probe)(struct udevice *dev); // 在該uclass的一個udevice進(jìn)行probe之后調(diào)用
int (*pre_remove)(struct udevice *dev);// 在該uclass的一個udevice進(jìn)行remove之前調(diào)用
int (*child_post_bind)(struct udevice *dev); // 在該uclass的一個udevice的一個子設(shè)備被綁定到該udevice之后調(diào)用
int (*child_pre_probe)(struct udevice *dev); // 在該uclass的一個udevice的一個子設(shè)備進(jìn)行probe之前調(diào)用
int (*init)(struct uclass *class); // 安裝該uclass的時候調(diào)用
int (*destroy)(struct uclass *class); // 銷毀該uclass的時候調(diào)用
int priv_auto_alloc_size; // 需要為對應(yīng)的uclass分配多少私有數(shù)據(jù)
int per_device_auto_alloc_size; //
int per_device_platdata_auto_alloc_size; //
int per_child_auto_alloc_size; //
int per_child_platdata_auto_alloc_size; //
const void *ops; //操作集合
uint32_t flags; // 標(biāo)識為
};
spi-uclass驅(qū)動:
UCLASS_DRIVER(spi) = {
.id = UCLASS_SPI,
.name = "spi",
.flags = DM_UC_FLAG_SEQ_ALIAS,
#if CONFIG_IS_ENABLED(OF_REAL)
.post_bind = dm_scan_fdt_dev,
#endif
.post_probe = spi_post_probe,
.child_pre_probe = spi_child_pre_probe,
.per_device_auto = sizeof(struct dm_spi_bus),
.per_child_auto = sizeof(struct spi_slave),
.per_child_plat_auto = sizeof(struct dm_spi_slave_plat),
#if CONFIG_IS_ENABLED(OF_REAL)
.child_post_bind = spi_child_post_bind,
#endif
};
//存放在段._u_boot_list_2_uclass_2_spi中,也就是section段的內(nèi)容可以在uboot.map可以查看
想要獲取uclass_driver需要先獲取uclass_driver table。
struct uclass_driver *uclass =
ll_entry_start(struct uclass_driver, uclass);
// 會根據(jù).u_boot_list_2_uclass_1的段地址來得到uclass_driver table的地址
const int n_ents = ll_entry_count(struct uclass_driver, uclass);
// 獲得uclass_driver table的長度
struct uclass_driver *lists_uclass_lookup(enum uclass_id id)
// 從uclass_driver table中獲取uclass id為id的uclass_driver。
2.4 driver
主要函數(shù):
struct driver {
char *name; // 驅(qū)動名
enum uclass_id id; // 對應(yīng)的uclass id
const struct udevice_id *of_match; // compatible字符串的匹配表,用于和device tree里面的設(shè)備節(jié)點(diǎn)匹配
int (*bind)(struct udevice *dev); // 用于綁定目標(biāo)設(shè)備到該driver中
int (*probe)(struct udevice *dev); // 用于probe目標(biāo)設(shè)備,激活
int (*remove)(struct udevice *dev); // 用于remove目標(biāo)設(shè)備。禁用
int (*unbind)(struct udevice *dev); // 用于解綁目標(biāo)設(shè)備到該driver中
int (*ofdata_to_platdata)(struct udevice *dev); // 在probe之前,解析對應(yīng)udevice的dts節(jié)點(diǎn),轉(zhuǎn)化成udevice的平臺數(shù)據(jù)
int (*child_post_bind)(struct udevice *dev); // 如果目標(biāo)設(shè)備的一個子設(shè)備被綁定之后,調(diào)用
int (*child_pre_probe)(struct udevice *dev); // 在目標(biāo)設(shè)備的一個子設(shè)備被probe之前,調(diào)用
int (*child_post_remove)(struct udevice *dev); // 在目標(biāo)設(shè)備的一個子設(shè)備被remove之后,調(diào)用
int priv_auto_alloc_size; //需要分配多少空間作為其udevice的私有數(shù)據(jù)
int platdata_auto_alloc_size; //需要分配多少空間作為其udevice的平臺數(shù)據(jù)
int per_child_auto_alloc_size; // 對于目標(biāo)設(shè)備的每個子設(shè)備需要分配多少空間作為父設(shè)備的私有數(shù)據(jù)
int per_child_platdata_auto_alloc_size; // 對于目標(biāo)設(shè)備的每個子設(shè)備需要分配多少空間作為父設(shè)備的平臺數(shù)據(jù)
const void *ops; /* driver-specific operations */ // 操作集合的指針,提供給uclass使用,沒有規(guī)定操作集的格式,由具體uclass決定
uint32_t flags; // 一些標(biāo)志位
};
2.4.1 spi master driver
static const struct dm_spi_ops noru_spi_ops = {
.claim_bus = winbond_spi_claim_bus,
.release_bus = winbond_spi_release_bus,
.xfer = noru_spi_master_xfer,
.set_speed = noru_spi_set_speed,
.set_mode = noru_spi_set_mode,
};
static const struct udevice_id noru_spi_ids[] = {
{ .compatible = "noru,spi_master" },
{ }
};
U_BOOT_DRIVER(noru_spi_master) = {
.name = "noru_spi_master",
.id = UCLASS_SPI,
.of_match = noru_spi_ids,
.ops = &noru_spi_ops,
.bind = winbond_sf_bind_emul,
.ofdata_to_platdata = noru_spi_ofdata_to_platdata,
.platdata_auto_alloc_size = sizeof(struct noru_spi_platdata),
.priv_auto_alloc_size = sizeof(struct noru_spi_priv),
.probe = noru_spi_probe,
};
由上面結(jié)構(gòu)體可得,其定義之后都被存放在了段._u_boot_list_2_driver_2_noru_spi_master中,那么去哪里可以看到呢?
在u-boot.map文件中搜索,._u_boot_list_2_driver,就可以查到程序中定義的所有驅(qū)動程序。**注意:**spi master驅(qū)動中讀寫函數(shù)noru_spi_master_xfer需要根據(jù)設(shè)備具體spi協(xié)議進(jìn)行編寫代碼
三、uboot代碼解析
3.1 DM的初始化
DM的初始化分為兩個部分,一個是在relocate重定向之前的初始化:initf_dm,一個是在relocate重定向之后的初始化:initr_dm
創(chuàng)建根設(shè)備root的udevice,存放在gd->dm_root中。
根設(shè)備其實(shí)是一個虛擬設(shè)備,主要是為uboot的其他設(shè)備提供一個掛載點(diǎn)。
初始化uclass鏈表gd->uclass_root
DM中udevice和uclass的解析
1、udevice的創(chuàng)建和uclass的創(chuàng)建
2、udevice和uclass的綁定
3、uclass_driver和uclass的綁定
4、driver和udevice的綁定
5、部分driver函數(shù)的調(diào)用
static int initf_dm(void)
{
#if defined(CONFIG_DM) && CONFIG_VAL(SYS_MALLOC_F_LEN)
int ret;
bootstage_start(BOOTSTAGE_ID_ACCUM_DM_F, "dm_f");
ret = dm_init_and_scan(true); //這里為true
bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_F);
if (ret)
return ret;
#endif
#ifdef CONFIG_TIMER_EARLY
ret = dm_timer_init();
if (ret)
return ret;
#endif
return 0;
}
static int initr_dm(void)
{
int ret;
/* Save the pre-reloc driver model and start a new one */
gd->dm_root_f = gd->dm_root;
gd->dm_root = NULL;
#ifdef CONFIG_TIMER
gd->timer = NULL;
#endif
bootstage_start(BOOTSTAGE_ID_ACCUM_DM_R, "dm_r");
ret = dm_init_and_scan(false); //這里為false
bootstage_accum(BOOTSTAGE_ID_ACCUM_DM_R);
if (ret)
return ret;
return 0;
}
首先說明一下dts節(jié)點(diǎn)中的“u-boot,dm-pre-reloc”屬性,當(dāng)設(shè)置了這個屬性時,則表示這個設(shè)備在relocate之前就需要使用。當(dāng)dm_init_and_scan的參數(shù)為true時,只會對帶有“u-boot,dm-pre-reloc”屬性的節(jié)點(diǎn)進(jìn)行解析,如在board_f.c文件中調(diào)用。而當(dāng)參數(shù)為false的時候,則會對所有節(jié)點(diǎn)都進(jìn)行解析,如在board_r.c文件中調(diào)用,重定位的意義,uboot重定位之前運(yùn)行在SDRAM中,重定位運(yùn)行在ddr中,運(yùn)行空間更大。
代碼解析:
int dm_init_and_scan(bool pre_reloc_only)
{
int ret;
ret = dm_init(); // DM的初始化
ret = dm_scan_platdata(pre_reloc_only); //從平臺設(shè)備中解析udevice和uclass,uboot一般不用該方式,采用dts方式
if (CONFIG_IS_ENABLED(OF_CONTROL)) {//CONFIG_OF_CONTROL注意打開
ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only); // 從dtb中解析udevice和uclass
}
ret = dm_scan_other(pre_reloc_only);
return 0;
}
#define DM_ROOT_NON_CONST (((gd_t *)gd)->dm_root) // 宏定義根設(shè)備指針gd->dm_root
#define DM_UCLASS_ROOT_NON_CONST (((gd_t *)gd)->uclass_root) // 宏定義gd->uclass_root,uclass的鏈表
int dm_init(void)
{
int ret;
if (gd->dm_root) {// 根設(shè)備已經(jīng)存在,說明DM已經(jīng)初始化過了
dm_warn("Virtual root driver already exists!\n");
return -EINVAL;
}
INIT_LIST_HEAD(&DM_UCLASS_ROOT_NON_CONST);// 初始化uclass鏈表
ret = device_bind_by_name(NULL, false, &root_info, &DM_ROOT_NON_CONST);
// DM_ROOT_NON_CONST是指根設(shè)備udevice,root_info是表示根設(shè)備的設(shè)備信息
// device_bind_by_name會查找和設(shè)備信息匹配的driver,然后創(chuàng)建對應(yīng)的udevice和uclass并進(jìn)行綁定,最后放在DM_ROOT_NON_CONST中。
// device_bind_by_name后續(xù)我們會進(jìn)行說明,這里我們暫時只需要了解root根設(shè)備的udevice以及對應(yīng)的uclass都已經(jīng)創(chuàng)建完成。
#if CONFIG_IS_ENABLED(OF_CONTROL)
DM_ROOT_NON_CONST->of_offset = 0;
#endif
ret = device_probe(DM_ROOT_NON_CONST);// 對根設(shè)備執(zhí)行probe操作,在initf_dm執(zhí)行(未重定位之前)只是創(chuàng)建root節(jié)點(diǎn)和uclass。
return 0;
}
dm_init創(chuàng)建root和uclass設(shè)備后開始解析設(shè)備樹內(nèi)容,從而創(chuàng)建相應(yīng)的udevice
int dm_scan_fdt(const void *blob, bool pre_reloc_only)
// 此時傳進(jìn)來的參數(shù)blob=gd->fdt_blob, pre_reloc_only=0,即initr_dm才會掃dts所有內(nèi)容
{
return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
// 直接調(diào)用dm_scan_fdt_node
}
int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
bool pre_reloc_only)
// 此時傳進(jìn)來的參數(shù)
// parent=gd->dm_root,表示以root設(shè)備作為父設(shè)備開始解析
// blob=gd->fdt_blob,指定了對應(yīng)的dtb
// offset=0,從偏移0的節(jié)點(diǎn)開始掃描
// pre_reloc_only=0,不只是解析relotion之前的設(shè)備
{
int ret = 0, err;
/* 以下步驟相當(dāng)于是遍歷每一個dts節(jié)點(diǎn)并且調(diào)用lists_bind_fdt對其進(jìn)行解析 */
for (offset = fdt_first_subnode(blob, offset);
// 獲得blob設(shè)備樹的offset偏移下的節(jié)點(diǎn)的第一個子節(jié)點(diǎn)
offset > 0;
offset = fdt_next_subnode(blob, offset)) {
// 循環(huán)查找下一個子節(jié)點(diǎn)
if (!fdtdec_get_is_enabled(blob, offset)) {
// 判斷節(jié)點(diǎn)狀態(tài)是否是disable,如果是的話直接忽略,注意dts中節(jié)點(diǎn)status = “ok”
dm_dbg(" - ignoring disabled device\n");
continue;
}
err = lists_bind_fdt(parent, blob, offset, NULL);
// 解析綁定這個節(jié)點(diǎn),dm_scan_fdt的核心,下面具體分析
if (err && !ret) {
ret = err;
debug("%s: ret=%d\n", fdt_get_name(blob, offset, NULL),
ret);
}
}
return ret;
}
lists_bind_fdt是從dtb中解析udevice和uclass的核心,通過讀取dts內(nèi)容讀取相應(yīng)的driver生成相應(yīng)的 udevice,再根據(jù)driver中對應(yīng)的UCLASS_ID綁定對應(yīng)的uclass,如dts中定義一個spi master驅(qū)動,對應(yīng)的驅(qū)動名字為mxc_spi,id為UCLASS_SPI,則該udevice會追加到spi uclass節(jié)點(diǎn)下,同時也會加到gd->root下。
int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
struct udevice **devp)
// parent指定了父設(shè)備,通過blob和offset可以獲得對應(yīng)的設(shè)備的dts節(jié)點(diǎn),對應(yīng)udevice結(jié)構(gòu)通過devp返回
{
struct driver *driver = ll_entry_start(struct driver, driver);
// 獲取driver table地址
const int n_ents = ll_entry_count(struct driver, driver);
// 獲取driver table長度
const struct udevice_id *id;
struct driver *entry;
struct udevice *dev;
bool found = false;
const char *name;
int result = 0;
int ret = 0;
dm_dbg("bind node %s\n", fdt_get_name(blob, offset, NULL));
// 打印當(dāng)前解析的節(jié)點(diǎn)的名稱
if (devp)
*devp = NULL;
for (entry = driver; entry != driver + n_ents; entry++) {
// 遍歷driver table中的所有driver,即通過U_BOOT_DRIVER(mxc_spi)聲明的驅(qū)動
ret = driver_check_compatible(blob, offset, entry->of_match,
&id);
// 判斷driver中的compatibile字段和dts節(jié)點(diǎn)是否匹配
name = fdt_get_name(blob, offset, NULL);
// 獲取節(jié)點(diǎn)名稱
if (ret == -ENOENT) {
continue;
} else if (ret == -ENODEV) {
dm_dbg("Device '%s' has no compatible string\n", name);
break;
} else if (ret) {
dm_warn("Device tree error at offset %d\n", offset);
result = ret;
break;
}
dm_dbg(" - found match at '%s'\n", entry->name);
ret = device_bind(parent, entry, name, NULL, offset, &dev);
// 找到對應(yīng)的driver,調(diào)用device_bind進(jìn)行綁定,會在這個函數(shù)中創(chuàng)建對應(yīng)udevice,然后根據(jù)id和uclass并且進(jìn)行綁定
if (ret) {
dm_warn("Error binding driver '%s': %d\n", entry->name,
ret);
return ret;
} else {
dev->driver_data = id->data;
found = true;
if (devp)
*devp = dev;
// 將udevice設(shè)置到devp指向的地方中,進(jìn)行返回
}
break;
}
if (!found && !result && ret != -ENODEV) {
dm_dbg("No match for node '%s'\n",
fdt_get_name(blob, offset, NULL));
}
return result;
}
這里會將udevice相關(guān)的信息進(jìn)行填充,獲取對應(yīng)的parent、uclass以及它們對應(yīng)的平臺屬性,然后將該udevice綁定到parent和uclass鏈表中。
int device_bind(struct udevice *parent, const struct driver *drv,
const char *name, void *platdata, int of_offset,
struct udevice **devp)
// parent:父設(shè)備
// drv:設(shè)備對應(yīng)的driver
// name:設(shè)備名稱
// platdata:設(shè)備的平臺數(shù)據(jù)指針
// of_offset:在dtb中的偏移,即代表了其dts節(jié)點(diǎn)
// devp:所創(chuàng)建的udevice的指針,用于返回
{
struct udevice *dev;
struct uclass *uc;
int size, ret = 0;
ret = uclass_get(drv->id, &uc);
// 獲取driver id對應(yīng)的uclass,如果uclass原先并不存在,那么會在這里創(chuàng)建uclass并其uclass_driver進(jìn)行綁定
dev = calloc(1, sizeof(struct udevice));
// 分配一個udevice
dev->platdata = platdata; // 設(shè)置udevice的平臺數(shù)據(jù)指針
dev->name = name; // 設(shè)置udevice的name
dev->of_offset = of_offset; // 設(shè)置udevice的dts節(jié)點(diǎn)偏移
dev->parent = parent; // 設(shè)置udevice的父設(shè)備
dev->driver = drv; // 設(shè)置udevice的對應(yīng)的driver,相當(dāng)于driver和udevice的綁定
dev->uclass = uc; // 設(shè)置udevice的所屬uclass
dev->seq = -1;
dev->req_seq = -1;
if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
/*
* Some devices, such as a SPI bus, I2C bus and serial ports
* are numbered using aliases.
*
* This is just a 'requested' sequence, and will be
* resolved (and ->seq updated) when the device is probed.
*/
if (uc->uc_drv->flags & DM_UC_FLAG_SEQ_ALIAS) {
if (uc->uc_drv->name && of_offset != -1) {
fdtdec_get_alias_seq(gd->fdt_blob,
uc->uc_drv->name, of_offset,
&dev->req_seq);
}
// 設(shè)置udevice的alias請求序號
}
}
if (!dev->platdata && drv->platdata_auto_alloc_size) {
dev->flags |= DM_FLAG_ALLOC_PDATA;
dev->platdata = calloc(1, drv->platdata_auto_alloc_size);
// 為udevice分配平臺數(shù)據(jù)的空間,由driver中的platdata_auto_alloc_size決定
}
size = uc->uc_drv->per_device_platdata_auto_alloc_size;
if (size) {
dev->flags |= DM_FLAG_ALLOC_UCLASS_PDATA;
dev->uclass_platdata = calloc(1, size);
// 為udevice分配給其所屬uclass使用的平臺數(shù)據(jù)的空間,由所屬uclass的driver中的per_device_platdata_auto_alloc_size決定
}
/* put dev into parent's successor list */
if (parent)
list_add_tail(&dev->sibling_node, &parent->child_head);
// 添加到父設(shè)備的子設(shè)備鏈表中
ret = uclass_bind_device(dev);
// uclass和udevice進(jìn)行綁定,主要是實(shí)現(xiàn)了將udevice鏈接到uclass的設(shè)備鏈表中
/* if we fail to bind we remove device from successors and free it */
if (drv->bind) {
ret = drv->bind(dev);
// 執(zhí)行udevice對應(yīng)driver的bind函數(shù)
}
if (parent && parent->driver->child_post_bind) {
ret = parent->driver->child_post_bind(dev);
// 執(zhí)行父設(shè)備的driver的child_post_bind函數(shù)
}
if (uc->uc_drv->post_bind) {
ret = uc->uc_drv->post_bind(dev);
if (ret)
goto fail_uclass_post_bind;
// 執(zhí)行所屬uclass的post_bind函數(shù)
}
if (devp)
*devp = dev;
// 將udevice進(jìn)行返回
dev->flags |= DM_FLAG_BOUND;
// 設(shè)置已經(jīng)綁定的標(biāo)志
// 后續(xù)可以通過dev->flags & DM_FLAG_ACTIVATED或者device_active宏來判斷設(shè)備是否已經(jīng)被激活
return 0;
當(dāng)udevie和driver、uclass綁定之后開始最后的激活階段
查找是否有對應(yīng)的設(shè)備分配設(shè)備的私有數(shù)據(jù)
對父設(shè)備進(jìn)行probe
執(zhí)行probe device之前uclass需要調(diào)用的一些函數(shù)
調(diào)用driver的ofdata_to_platdata,將dts信息轉(zhuǎn)化為設(shè)備的平臺數(shù)據(jù)
調(diào)用driver的probe函數(shù)
執(zhí)行probe device之后uclass需要調(diào)用的一些函數(shù)
int device_probe(struct udevice *dev)
{
const struct driver *drv;
int size = 0;
int ret;
int seq;
if (dev->flags & DM_FLAG_ACTIVATED)
return 0;
// 表示這個設(shè)備已經(jīng)被激活了
drv = dev->driver;
assert(drv);
// 獲取這個設(shè)備對應(yīng)的driver
/* Allocate private data if requested and not reentered */
if (drv->priv_auto_alloc_size && !dev->priv) {
dev->priv = alloc_priv(drv->priv_auto_alloc_size, drv->flags);
// 為設(shè)備分配私有數(shù)據(jù)
}
/* Allocate private data if requested and not reentered */
size = dev->uclass->uc_drv->per_device_auto_alloc_size;
if (size && !dev->uclass_priv) {
dev->uclass_priv = calloc(1, size);
// 為設(shè)備所屬uclass分配私有數(shù)據(jù)
}
// 這里過濾父設(shè)備的probe
seq = uclass_resolve_seq(dev);
if (seq < 0) {
ret = seq;
goto fail;
}
dev->seq = seq;
dev->flags |= DM_FLAG_ACTIVATED;
// 設(shè)置udevice的激活標(biāo)志
ret = uclass_pre_probe_device(dev);
// uclass在probe device之前的一些函數(shù)的調(diào)用
if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
ret = drv->ofdata_to_platdata(dev);
// 調(diào)用driver中的ofdata_to_platdata將dts信息轉(zhuǎn)化為設(shè)備的平臺數(shù)據(jù)
}
if (drv->probe) {
ret = drv->probe(dev);
// 調(diào)用driver的probe函數(shù),到這里設(shè)備才真正激活了
}
ret = uclass_post_probe_device(dev);
return ret;
}
3.2 spi norflash設(shè)備識別
通過dts中子節(jié)點(diǎn)匹配的驅(qū)動進(jìn)行創(chuàng)建:
static const struct dm_spi_flash_ops spi_flash_std_ops = {
.read = spi_flash_std_read,
.write = spi_flash_std_write,
.erase = spi_flash_std_erase,
};
static const struct udevice_id spi_flash_std_ids[] = {
{ .compatible = "noru,spi-flash" },
{ }
};
U_BOOT_DRIVER(spi_flash_std) = {
.name = "spi_flash_std",
.id = UCLASS_SPI_FLASH,
.of_match = spi_flash_std_ids,
.probe = spi_flash_std_probe,
.priv_auto_alloc_size = sizeof(struct spi_flash),
.ops = &spi_flash_std_ops,
};
3.3 設(shè)備樹內(nèi)容
在設(shè)備樹添加設(shè)備信息
在對應(yīng)的dts文件中添加相應(yīng)的設(shè)備節(jié)點(diǎn)和信息,注意simple-bus創(chuàng)建,代碼解析:
如果dts中沒有設(shè)置clocks節(jié)點(diǎn)直接回退出,原因是spi通信是基于clock進(jìn)行數(shù)據(jù)傳輸?shù)?/p>
chosen節(jié)點(diǎn)在scan dts內(nèi)容是需要的,可以加入
/dts-v1/;
/ {
#address-cells = <2>;
#size-cells = <2>;
compatible = "evb,noru";
model = "NORU vu440";
aliases {
spi0 = &spi;
} ;
chosen {
bootargs = "console=ttyS0,115200n8 loglevel=7";
stdout-path = "uart0:115200n8";
};
clocks {
compatible = "simple-bus"; //bus總線驅(qū)動和clock
u-boot,dm-pre-reloc;
spiclk: virt_100mhz {
#clock-cells = <0>;
compatible = "fixed-clock";
clock-frequency = <100000000>;
};
};
spi: spi@c100000 {
compatible = "noru,spi_master"; //spi master 驅(qū)動
status = "okay";
reg = <0x0 0xc100000 0x0 0x10000>;
#address-cells = <1>;
#size-cells = <0>;
num-cs = <1>;
clocks = <&spiclk>;
flash@0 {
compatible = "noru,spi-flash";//flash 驅(qū)動
spi-max-frequency = <50000000>;
reg = <0>;
spi-cpol;
spi-cpha;
};
};
};
3.4 .config配置
CONFIG_DEFAULT_DEVICE_TREE="xxxxx" //指定設(shè)備樹名
CONFIG_OF_EMBED=y //那么uboot會把設(shè)備樹編譯進(jìn)鏡像內(nèi)
CONFIG_OF_CONTROL=y //使能設(shè)備樹的支持
CONFIG_SPI_FLASH_BAR=y //支持4B模式,大于16MB的spi打開
```c
//執(zhí)行命令
sf probe
'spi@c100000'
- found
spi_find_chip_select: plat=00000089ff6bd4c0, cs=0
spi_flash_probe_bus_cs==========bus:0,===cs:0,===speed:1000000,====mode:3
spi_flash@0:0,the str is spi_flash@0:0
uclass_get_device_by_seq===
'spi@c100000'
- found
spi_find_chip_select: plat=00000089ff6bd4c0, cs=0
==============spi_flash_std_probe
winbond_spi_set_speed: ==========eric=======winbond_spi_claim_bus
SF: Detected w25q256fw with page size 256 Bytes, erase size 4 KiB, total 32 MiB
SF: Warning - Only lower 16MiB accessible, Full access #define CONFIG_SPI_FLASH_BAR
上面Warning因?yàn)楝F(xiàn)在用的winbod spi支持32MB,設(shè)置的3B模式只支持16MB,需要在config中打開CONFIG_SPI_FLASH_BAR=y文章來源:http://www.zghlxwxcb.cn/news/detail-728379.html
注意加入該norflash相關(guān)id
drivers/mtd/spi/spi_flash_ids.c文章來源地址http://www.zghlxwxcb.cn/news/detail-728379.html
3.5 spi讀寫測試
//初始化spi
sf probe
//tftp傳入需要燒錄的文件
tftp 0x8801000000 pkg_bin/noru_preloader_pkt.bin
tftp 0x8802000000 pkg_bin/noru_loader_pkt.bin
tftp 0x8803000000 pkg_bin/fw_pkt_compress.image
//擦除原有數(shù)據(jù)
sf erase 0 0x200000
//從ddr寫入數(shù)據(jù)
sf write 0x8801000000 0 0x38000
sf write 0x8802000000 0x70000 0x80000
sf write 0x8803000000 0xF1000 0x80000
//讀數(shù)據(jù)到ddr
sf read 0x8801000000 0 0x38000
sf read 0x8802000000 0x70000 0x80000
sf read 0x8803000000 0xF1000 0x80000
四、其他相關(guān)鏈接
1、SPI協(xié)議詳細(xì)總結(jié)附實(shí)例圖文講解通信過程
2、Linux下spi網(wǎng)卡dm9051驅(qū)動移植及驅(qū)動調(diào)試分析總結(jié)
3、Linux下設(shè)備樹dts內(nèi)容總結(jié)及示例解析
到了這里,關(guān)于uboot下UCLASS框架詳解---結(jié)合項(xiàng)目工作中spi master和flash驅(qū)動開發(fā)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!