基於335X的UBOOT網口驅動分析
一、軟硬件平台資料
1、 開發板:創龍AM3359核心板,網口采用RMII形式
2、 UBOOT版本:U-Boot-2016.05,采用FDT和DM。
參考鏈接:
https://blog.csdn.net/hahachenchen789/article/details/53339181
二、網口相關代碼位置
1、 網口的PINMUX設置
RMII接口的相關PINMUX在MLO中進行設置,具體的設置代碼為
|-board_init_f
|-board_early_init_f
|-set_mux_conf_regs
|-enable_board_pin_mux
configure_module_pin_mux(rmii1_pin_mux);
2、DTS文件中的CPSW的配置
&cpsw_emac0 {
phy_id = <&davinci_mdio>, <0x12>; //phy_id【1】為初始的phy_addr,為SW的PORT2口的ADDR。
phy-mode = "rmii"; //RMII 模式
};
&mac { //未使用此處配置
slaves = <1>;
pinctrl-names = "default", "sleep";
pinctrl-0 = <&cpsw_default>;
pinctrl-1 = <&cpsw_sleep>;
status = "okay";
};
&phy_sel {
rmii-clock-ext; //RMII模式的時鍾為外部時鍾
};
&davinci_mdio { //未使用此處配置
pinctrl-names = "default", "sleep";
pinctrl-0 = <&davinci_mdio_default>;
pinctrl-1 = <&davinci_mdio_sleep>;
status = "okay";
reset-gpios = <&gpio2 5 GPIO_ACTIVE_LOW>;
reset-delay-us = <2>; /* PHY datasheet states 1uS min */
};
3、 網口的初始化設置
網口的初始化在UBOOT中進行,具體設置代碼為
|-board_init_r
|-init_sequence_r
|-initr_net
|- eth_initialize (eth-uclass.c)
三、有關網口的DM&FDT分析
1、 驅動實現方式
此版本的UBOOT中使用了FDT文件進行外設的相關配置,驅動模型使用了DM方式,有關FDT以及DM相關的知識請參考如下文章
https://blog.csdn.net/ooonebook/article/details/53206623
https://blog.csdn.net/ooonebook/article/details/53234020
2、 UBOOT中DM初始化
DM的初始化
.創建根設備root的udevice,存放在gd->dm_root中。
.根設備其實是一個虛擬設備,主要是為uboot的其他設備提供一個掛載點。
.初始化uclass鏈表gd->uclass_root
DM中udevice和uclass的解析
.udevice的創建和uclass的創建
.udevice和uclass的綁定
.uclass_driver和uclass的綁定
.driver和udevice的綁定
.部分driver函數的調用
(1)DM初始化調用過程
dm初始化的接口在dm_init_and_scan中。 可以發現在uboot relocate之前的initf_dm和之后的initr_dm都調用了這個函數。
static int initf_dm(void)
{
#if defined(CONFIG_DM) && defined(CONFIG_SYS_MALLOC_F_LEN)
int ret;
ret = dm_init_and_scan(true); // 調用dm_init_and_scan對DM進行初始化和設備的解析
if (ret)
return ret;
#endif
return 0;
}
#ifdef CONFIG_DM
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; // 存儲relocate之前的根設備
gd->dm_root = NULL;
ret = dm_init_and_scan(false); // 調用dm_init_and_scan對DM進行初始化和設備的解析
if (ret)
return ret;
return 0;
}
#endif
主要區別在於參數。
首先說明一下dts節點中的“u-boot,dm-pre-reloc”屬性,當設置了這個屬性時,則表示這個設備在relocate之前就需要使用。
當dm_init_and_scan的參數為true時,只會對帶有“u-boot,dm-pre-reloc”屬性的節點進行解析。而當參數為false的時候,則會對所有節點都進行解析。
由於“u-boot,dm-pre-reloc”的情況比較少,所以這里只學習參數為false的情況。也就是initr_dm里面的dm_init_and_scan(false);。
dm_init_and_scan(driver/core/root.c)說明
int dm_init_and_scan(bool pre_reloc_only)
{
int ret;
ret = dm_init(); // DM的初始化
if (ret) {
debug("dm_init() failed: %d\n", ret);
return ret;
}
ret = dm_scan_platdata(pre_reloc_only); // 從平台設備中解析udevice和uclass
if (ret) {
debug("dm_scan_platdata() failed: %d\n", ret);
return ret;
}
if (CONFIG_IS_ENABLED(OF_CONTROL)) {
ret = dm_scan_fdt(gd->fdt_blob, pre_reloc_only); // 從dtb中解析udevice和uclass
if (ret) {
debug("dm_scan_fdt() failed: %d\n", ret);
return ret;
}
}
ret = dm_scan_other(pre_reloc_only);
if (ret)
return ret;
return 0;
}
DM的初始化—dm_init(driver/core/root.c)
#define DM_ROOT_NON_CONST (((gd_t *)gd)->dm_root) // 宏定義根設備指針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) {
// 根設備已經存在,說明DM已經初始化過了
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是指根設備udevice,root_info是表示根設備的設備信息
// device_bind_by_name會查找和設備信息匹配的driver,然后創建對應的udevice和uclass並進行綁定,最后放在DM_ROOT_NON_CONST中。
// device_bind_by_name后續我們會進行說明,這里我們暫時只需要了解root根設備的udevice以及對應的uclass都已經創建完成。
if (ret)
return ret;
#if CONFIG_IS_ENABLED(OF_CONTROL)
DM_ROOT_NON_CONST->of_offset = 0;
#endif
ret = device_probe(DM_ROOT_NON_CONST);
// 對根設備執行probe操作,
// device_probe后續再進行說明
if (ret)
return ret;
return 0;
}
這里就完成的DM的初始化了
1)創建根設備root的udevice,存放在gd->dm_root中。
2)初始化uclass鏈表gd->uclass_root
(2)從平台設備中解析udevice和uclass—dm_scan_platdata(不涉及)
(3)從dtb中解析udevice和uclass——dm_scan_fdt
對應代碼如下driver/core/root.c
int dm_scan_fdt(const void *blob, bool pre_reloc_only)
// 此時傳進來的參數blob=gd->fdt_blob, pre_reloc_only=0
{
return dm_scan_fdt_node(gd->dm_root, blob, 0, pre_reloc_only);
// 直接調用dm_scan_fdt_node
}
int dm_scan_fdt_node(struct udevice *parent, const void *blob, int offset,
bool pre_reloc_only)
// 此時傳進來的參數
// parent=gd->dm_root,表示以root設備作為父設備開始解析
// blob=gd->fdt_blob,指定了對應的dtb
// offset=0,從偏移0的節點開始掃描
// pre_reloc_only=0,不只是解析relotion之前的設備
{
int ret = 0, err;
/* 以下步驟相當於是遍歷每一個dts節點並且調用lists_bind_fdt對其進行解析 */
for (offset = fdt_first_subnode(blob, offset);
// 獲得blob設備樹的offset偏移下的節點的第一個子節點
offset > 0;
offset = fdt_next_subnode(blob, offset)) {
// 循環查找下一個子節點
if (!fdtdec_get_is_enabled(blob, offset)) {
// 判斷節點狀態是否是disable,如果是的話直接忽略
dm_dbg(" - ignoring disabled device\n");
continue;
}
err = lists_bind_fdt(parent, blob, offset, NULL);
// 解析綁定這個節點,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的核心。
其具體實現如下: driver/core/lists.c
int lists_bind_fdt(struct udevice *parent, const void *blob, int offset,
struct udevice **devp)
// parent指定了父設備,通過blob和offset可以獲得對應的設備的dts節點,對應udevice結構通過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));
// 打印當前解析的節點的名稱
if (devp)
*devp = NULL;
for (entry = driver; entry != driver + n_ents; entry++) {
// 遍歷driver table中的所有driver,具體參考三、4一節
ret = driver_check_compatible(blob, offset, entry->of_match,
&id);
// 判斷driver中的compatibile字段和dts節點是否匹配
name = fdt_get_name(blob, offset, NULL);
// 獲取節點名稱
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);
// 找到對應的driver,調用device_bind進行綁定,會在這個函數中創建對應udevice和uclass並切進行綁定,后面繼續說明
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設置到devp指向的地方中,進行返回
}
break;
}
if (!found && !result && ret != -ENODEV) {
dm_dbg("No match for node '%s'\n",
fdt_get_name(blob, offset, NULL));
}
return result;
}
在device_bind中實現了udevice和uclass的創建和綁定以及一些初始化操作,這里專門學習一下device_bind。
device_bind的實現如下(去除部分代碼)
driver/core/device.c
int device_bind(struct udevice *parent, const struct driver *drv,
const char *name, void *platdata, int of_offset,
struct udevice **devp)
// parent:父設備
// drv:設備對應的driver
// name:設備名稱
// platdata:設備的平台數據指針
// of_offset:在dtb中的偏移,即代表了其dts節點
// devp:所創建的udevice的指針,用於返回
{
struct udevice *dev;
struct uclass *uc;
int size, ret = 0;
ret = uclass_get(drv->id, &uc);
// 獲取driver id對應的uclass,如果uclass原先並不存在,那么會在這里創建uclass並其uclass_driver進行綁定
dev = calloc(1, sizeof(struct udevice));
// 分配一個udevice
dev->platdata = platdata; // 設置udevice的平台數據指針
dev->name = name; // 設置udevice的name
dev->of_offset = of_offset; // 設置udevice的dts節點偏移
dev->parent = parent; // 設置udevice的父設備
dev->driver = drv; // 設置udevice的對應的driver,相當於driver和udevice的綁定
dev->uclass = uc; // 設置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);
}
// 設置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分配平台數據的空間,由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使用的平台數據的空間,由所屬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);
// 添加到父設備的子設備鏈表中
ret = uclass_bind_device(dev);
// uclass和udevice進行綁定,主要是實現了將udevice鏈接到uclass的設備鏈表中
/* if we fail to bind we remove device from successors and free it */
if (drv->bind) {
ret = drv->bind(dev);
// 執行udevice對應driver的bind函數
}
if (parent && parent->driver->child_post_bind) {
ret = parent->driver->child_post_bind(dev);
// 執行父設備的driver的child_post_bind函數
}
if (uc->uc_drv->post_bind) {
ret = uc->uc_drv->post_bind(dev);
if (ret)
goto fail_uclass_post_bind;
// 執行所屬uclass的post_bind函數
}
if (devp)
*devp = dev;
// 將udevice進行返回
dev->flags |= DM_FLAG_BOUND;
// 設置已經綁定的標志
// 后續可以通過dev->flags & DM_FLAG_ACTIVATED或者device_active宏來判斷設備是否已經被激活
return 0;
在init_sequence_r中的initr_dm中,完成了FDT的解析,解析了所有的外設node,並針對各個節點進行了 udevice和uclass的創建,以及各個組成部分的綁定關系。
注意,這里只是綁定,即調用了driver的bind函數,但是設備還沒有真正激活,也就是還沒有執行設備的probe函數。
將在網口初始化階段進行相關driver的bind操作。
四、網口的初始化過程分析
1、 eth_initialize函數
網口初始化,其中最主要的工作是調用uclass_first_device(UCLASS_ETH, &dev)函數,從uclass的設備鏈表中獲取第一個udevice,並且進行probe。最終,是通過調用device_probe(dev)進行網口設備的激活和驅動的注冊。下面分析device_probe(dev)的實現的部分過程。
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;
// 表示這個設備已經被激活了
drv = dev->driver;
assert(drv);
// 獲取這個設備對應的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);
// 為設備分配私有數據
}
/* 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);
// 為設備所屬uclass分配私有數據
}
// 這里過濾父設備的probe
seq = uclass_resolve_seq(dev);
if (seq < 0) {
ret = seq;
goto fail;
}
dev->seq = seq;
dev->flags |= DM_FLAG_ACTIVATED;
// 設置udevice的激活標志
ret = uclass_pre_probe_device(dev);
// uclass在probe device之前的一些函數的調用
if (drv->ofdata_to_platdata && dev->of_offset >= 0) {
ret = drv->ofdata_to_platdata(dev);
// 調用driver中的ofdata_to_platdata將dts信息轉化為設備的平台數據
}
if (drv->probe) {
ret = drv->probe(dev);
// 調用driver的probe函數,到這里設備才真正激活了
}
ret = uclass_post_probe_device(dev);
return ret;
}
主要工作歸納如下:
.分配設備的私有數據
.對父設備進行probe
.執行probe device之前uclass需要調用的一些函數
.調用driver的ofdata_to_platdata,將dts信息轉化為設備的平台數據(重要)
.調用driver的probe函數(重要)
.執行probe device之后uclass需要調用的一些函數
在CPSW.c中有相關定義:
U_BOOT_DRIVER(eth_cpsw) = {
.name = "eth_cpsw",
.id = UCLASS_ETH,
.of_match = cpsw_eth_ids,
.ofdata_to_platdata = cpsw_eth_ofdata_to_platdata,
.probe = cpsw_eth_probe,
.ops = &cpsw_eth_ops,
.priv_auto_alloc_size = sizeof(struct cpsw_priv),
.platdata_auto_alloc_size = sizeof(struct eth_pdata),
.flags = DM_FLAG_ALLOC_PRIV_DMA,
};
2、有關DTS配置信息轉化的函數(drv->ofdata_to_platdata)
static int cpsw_eth_ofdata_to_platdata(struct udevice *dev)
{
struct eth_pdata *pdata = dev_get_platdata(dev);
struct cpsw_priv *priv = dev_get_priv(dev);
const char *phy_mode;
const char *phy_sel_compat = NULL;
const void *fdt = gd->fdt_blob;
int node = dev->of_offset;
int subnode;
int slave_index = 0;
int active_slave;
int ret;
pdata->iobase = dev_get_addr(dev);
priv->data.version = CPSW_CTRL_VERSION_2;
priv->data.bd_ram_ofs = CPSW_BD_OFFSET;
priv->data.ale_reg_ofs = CPSW_ALE_OFFSET;
priv->data.cpdma_reg_ofs = CPSW_CPDMA_OFFSET;
priv->data.mdio_div = CPSW_MDIO_DIV;
priv->data.host_port_reg_ofs = CPSW_HOST_PORT_OFFSET,
pdata->phy_interface = -1;
priv->data.cpsw_base = pdata->iobase;
priv->data.channels = fdtdec_get_int(fdt, node, "cpdma_channels", -1);
if (priv->data.channels <= 0) {
printf("error: cpdma_channels not found in dt\n");
return -ENOENT;
}
priv->data.slaves = fdtdec_get_int(fdt, node, "slaves", -1);
if (priv->data.slaves <= 0) {
printf("error: slaves not found in dt\n");
return -ENOENT;
}
priv->data.slave_data = malloc(sizeof(struct cpsw_slave_data) *
priv->data.slaves);
priv->data.ale_entries = fdtdec_get_int(fdt, node, "ale_entries", -1);
if (priv->data.ale_entries <= 0) {
printf("error: ale_entries not found in dt\n");
return -ENOENT;
}
priv->data.bd_ram_ofs = fdtdec_get_int(fdt, node, "bd_ram_size", -1);
if (priv->data.bd_ram_ofs <= 0) {
printf("error: bd_ram_size not found in dt\n");
return -ENOENT;
}
priv->data.mac_control = fdtdec_get_int(fdt, node, "mac_control", -1);
if (priv->data.mac_control <= 0) {
printf("error: ale_entries not found in dt\n");
return -ENOENT;
}
active_slave = fdtdec_get_int(fdt, node, "active_slave", 0);
priv->data.active_slave = active_slave;
fdt_for_each_subnode(fdt, subnode, node) {
int len;
const char *name;
name = fdt_get_name(fdt, subnode, &len);
if (!strncmp(name, "mdio", 4)) {
u32 mdio_base;
mdio_base = cpsw_get_addr_by_node(fdt, subnode);
if (mdio_base == FDT_ADDR_T_NONE) {
error("Not able to get MDIO address space\n");
return -ENOENT;
}
priv->data.mdio_base = mdio_base;
}
if (!strncmp(name, "slave", 5)) {
u32 phy_id[2];
if (slave_index >= priv->data.slaves)
continue;
phy_mode = fdt_getprop(fdt, subnode, "phy-mode", NULL);
if (phy_mode)
priv->data.slave_data[slave_index].phy_if =
phy_get_interface_by_name(phy_mode);
priv->data.slave_data[slave_index].phy_of_handle =
fdtdec_lookup_phandle(fdt, subnode, "phy-handle");
if (priv->data.slave_data[slave_index].phy_of_handle >= 0) {
priv->data.slave_data[slave_index].phy_addr =
fdtdec_get_int(gd->fdt_blob,
priv->data.slave_data[slave_index].phy_of_handle,
"reg", -1);
} else {
fdtdec_get_int_array(fdt, subnode, "phy_id", phy_id, 2);
priv->data.slave_data[slave_index].phy_addr = phy_id[1];
}
slave_index++;
}
if (!strncmp(name, "cpsw-phy-sel", 12)) {
priv->data.gmii_sel = cpsw_get_addr_by_node(fdt,
subnode);
if (priv->data.gmii_sel == FDT_ADDR_T_NONE) {
error("Not able to get gmii_sel reg address\n");
return -ENOENT;
}
if (fdt_get_property(fdt, subnode, "rmii-clock-ext",
NULL))
{
priv->data.rmii_clock_external = true;
printf("data.rmii_clock_external is true\n");
}
phy_sel_compat = fdt_getprop(fdt, subnode, "compatible",
NULL);
if (!phy_sel_compat) {
printf("Not able to get gmii_sel compatible\n");
return -ENOENT;
}
}
}
priv->data.slave_data[0].slave_reg_ofs = CPSW_SLAVE0_OFFSET;
priv->data.slave_data[0].sliver_reg_ofs = CPSW_SLIVER0_OFFSET;
if (priv->data.slaves == 2) {
priv->data.slave_data[1].slave_reg_ofs = CPSW_SLAVE1_OFFSET;
priv->data.slave_data[1].sliver_reg_ofs = CPSW_SLIVER1_OFFSET;
}
ret = ti_cm_get_macid(dev, active_slave, pdata->enetaddr);
if (ret < 0) {
error("cpsw read efuse mac failed\n");
return ret;
}
pdata->phy_interface = priv->data.slave_data[active_slave].phy_if;
if (pdata->phy_interface == -1) {
debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
return -EINVAL;
}
/* Select phy interface in control module */
cpsw_phy_sel(priv, phy_sel_compat, pdata->phy_interface);
return 0;
}
可以看到,在cpsw_eth_ofdata_to_platdata函數中將各種與CPSW有關的平台數據宏定義以及DTS中的配置信息(包含個子節點)轉化為了平台數據存儲在了priv->data的相關部分中。主要涉及priv->data的相關設置,此部分重要的信息是MAC的接口形式,比如RMII的設置,RMII時鍾的使能,phy_addr的設置。
3、有關驅動注冊的函數(drv->probe(dev))
static int cpsw_eth_probe(struct udevice *dev)
{
struct cpsw_priv *priv = dev_get_priv(dev);
printf("cpsw_eth_probe now\n");
priv->dev = dev;
return _cpsw_register(priv);
}
TI對於網卡設備的通用管理是CPSW方式,通過cpsw_priv結構體來進行相關的管理,cpsw_priv結構體中包含有CPSW平台數據、cpsw_slave的信息、priv->bus(MII接口管理)、phy_device設備的配置及管理等。
cpsw_register(priv)函數主要進行以下工作
(1)、首先是聲明幾個結構體變量,其中包括cpsw的主:cpsw_priv和從:cpsw_slave,然后是設置cpsw的基礎寄存器的地址cpsw_base,然后調用calloc函數為這些結構體分配空間。
struct cpsw_slave *slave;
struct cpsw_platform_data *data = &priv->data;
void *regs = (void *)data->cpsw_base;
priv->slaves = malloc(sizeof(struct cpsw_slave) * data->slaves);
(2)、分配好后對priv結構體中的成員進行初始化,host_port=0表示主機端口號是0,然后成員的寄存器的偏移地址進行初始化。
Priv->host_port = data->host_port_num;
priv->regs = regs;
priv->host_port_regs = regs + data->host_port_reg_ofs;
priv->dma_regs = regs + data->cpdma_reg_ofs;
priv->ale_regs = regs + data->ale_reg_ofs;
priv->descs = (void *)regs + data->bd_ram_ofs;
(3)、對每個salve進行初始化,這里采用for循環的意義在於可能有多個網卡,am335支持雙網卡。
for_each_slave(slave, priv) {
cpsw_slave_setup(slave, idx, priv);
idx = idx + 1;
}
(4)、對MDIO接口的操作集進行初始化配置
cpsw_mdio_init(priv->dev->name, data->mdio_base, data->mdio_div);
.進行了mii_dev設備的創建
.進行了mdio_regs寄存器的配置(set enable and clock divider)
.進行了cpsw_mdio_read/ cpsw_mdio_write的定義(用此函數對PHY進行讀寫)
.mii_dev設備注冊(加到mii_devs鏈表,並指定為current_mii)
(4)指定priv->bus為上一步創建的設備
priv->bus = miiphy_get_dev_by_name(priv->dev->name);
(5) phydev初始化/配置(重點)
cpsw_phy_init函數定義:
static int cpsw_phy_init(struct eth_device *dev, struct cpsw_slave *slave)
{
struct cpsw_priv *priv = (struct cpsw_priv *)dev->priv;
struct phy_device *phydev;
u32 supported = PHY_GBIT_FEATURES;
printf("cpsw_phy_init \n");
printf("phy_addr:%d \n",slave->data->phy_addr);
phydev = phy_connect(priv->bus,
slave->data->phy_addr,
dev,
slave->data->phy_if);
if (!phydev)
return -1;
phydev->supported &= supported;
phydev->advertising = phydev->supported;
priv->phydev = phydev;
phy_config(phydev);
return 1;
}
該函數調用phy_connect函數連接網卡,返回的值如果合理就調用phy_config函數對該網卡進行配置,主要是配置網卡的速率和半雙工,自動協商等,此部分需要再進一步調試熟悉。
首先分析phy_connect函數:
struct phy_device *phy_connect(struct mii_dev *bus, int addr,
struct eth_device *dev, phy_interface_t interface)
#endif
{
struct phy_device *phydev;
phydev = phy_find_by_mask(bus, 1 << addr, interface);
if (phydev)
phy_connect_dev(phydev, dev);
else
printf("Could not get PHY for %s: addr %d\n", bus->name, addr);
return phydev;
}
該函數首先調用phy_find_by_mask函數查詢網卡設備,如果存在則調用phy_connect_dev函數連接,否則就打印出錯信息
struct phy_device *phy_find_by_mask(struct mii_dev *bus, unsigned phy_mask,
phy_interface_t interface)
{
/* Reset the bus */
if (bus->reset) {
bus->reset(bus);
/* Wait 15ms to make sure the PHY has come out of hard reset */
udelay(15000);
}
return get_phy_device_by_mask(bus, phy_mask, interface);
}
該函數主要是調用get_phy_device_by_mask函數進行設備的查找,get_phy_device_by_mask函數的實現至關重要,包含了對於網卡的主要mdio通信。
static struct phy_device *get_phy_device_by_mask(struct mii_dev *bus,
unsigned phy_mask, phy_interface_t interface)
{
int i;
struct phy_device *phydev;
phydev = search_for_existing_phy(bus, phy_mask, interface);
if (phydev)
return phydev;
/* Try Standard (ie Clause 22) access */
/* Otherwise we have to try Clause 45 */
for (i = 0; i < 5; i++) {
phydev = create_phy_by_mask(bus, phy_mask,
i ? i : MDIO_DEVAD_NONE, interface);
if (IS_ERR(phydev))
return NULL;
if (phydev)
return phydev;
}
printf("Phy %d not found\n", ffs(phy_mask) - 1);
return phy_device_create(bus, ffs(phy_mask) - 1, 0xffffffff, interface);
}
該函數首先調用search_for_existing_phy函數查找當前存在的設備,如果存在則將該設備返回,不存在則調用create_phy_by_mask函數進行創建。重點看下create_phy_by_mask函數
static struct phy_device *create_phy_by_mask(struct mii_dev *bus,
unsigned phy_mask, int devad, phy_interface_t interface)
{
u32 phy_id = 0xffffffff;
while (phy_mask) {
int addr = ffs(phy_mask) - 1;
int r = get_phy_id(bus, addr, devad, &phy_id);
/* If the PHY ID is mostly f's, we didn't find anything */
if (r == 0 && (phy_id & 0x1fffffff) != 0x1fffffff)
return phy_device_create(bus, addr, phy_id, interface);
phy_mask &= ~(1 << addr);
}
return NULL;
}
該函數調用get_phy_id函數讓處理器通過mdio總線查看網卡寄存器存儲的ID,如果ID都是f,說明沒有ID,就返回空,否則返回phy_device_create函數進行創建一個網卡設備。
get_phy_id函數實現:
int __weak get_phy_id(struct mii_dev *bus, int addr, int devad, u32 *phy_id)
{
int phy_reg;
/* Grab the bits from PHYIR1, and put them
* in the upper half */
phy_reg = bus->read(bus, addr, devad, MII_PHYSID1);
if (phy_reg < 0)
return -EIO;
*phy_id = (phy_reg & 0xffff) << 16;
/* Grab the bits from PHYIR2, and put them in the lower half */
phy_reg = bus->read(bus, addr, devad, MII_PHYSID2);
if (phy_reg < 0)
return -EIO;
*phy_id |= (phy_reg & 0xffff);
return 0;
}
該函數就調用了bus->read總線讀函數,來讀取網卡寄存器的值,這里是讀取寄存器存儲的網卡ID,bus->read函數定義為cpsw_mdio_read
之后的phy_device_create函數為新創建一個phy_device及相關參數,以及相對應的phy_driver。
static struct phy_device *phy_device_create(struct mii_dev *bus, int addr,
u32 phy_id,
phy_interface_t interface)
{
struct phy_device *dev;
/* We allocate the device, and initialize the
* default values */
dev = malloc(sizeof(*dev));
if (!dev) {
printf("Failed to allocate PHY device for %s:%d\n",
bus->name, addr);
return NULL;
}
memset(dev, 0, sizeof(*dev));
dev->duplex = -1;
dev->link = 0;
dev->interface = interface;
dev->autoneg = AUTONEG_ENABLE;
dev->addr = addr;
dev->phy_id = phy_id;
dev->bus = bus;
dev->drv = get_phy_driver(dev, interface);
phy_probe(dev);
bus->phymap[addr] = dev;
return dev;
}
其中get_phy_driver會根據phy_id進行phy_driver的查找,若沒有找到,則分配一個"Generic PHY"。
綜上:cpsw_eth_probe的最終結果,是初始化了cpsw_priv各個部分,包括各個參數及mii_dev及phy_dev.
其中phy_dev非常重要,從后面的邏輯看出,phy_dev存在的情況下會根據LINK狀態下的mac_control值對slave->sliver->mac_control寄存器進行配置。這決定了RMII接口的正確配置。
所以,必須有一個phy_dev?或者有一個mac_control值對slave->sliver->mac_control寄存器進行配置?
- 驅動的初始化及調用
eth_init->eth_get_ops(current)->start(current)來進行網口通信的底層配置
static int cpsw_eth_start(struct udevice *dev)
{
struct eth_pdata *pdata = dev_get_platdata(dev);
struct cpsw_priv *priv = dev_get_priv(dev);
printf("cpsw_eth_start_now\n");
return _cpsw_init(priv, pdata->enetaddr);
}
static int _cpsw_init(struct cpsw_priv *priv, u8 *enetaddr)
{
struct cpsw_slave *slave;
int i, ret;
printf("_cpsw_init now\n");
/* soft reset the controller and initialize priv */
setbit_and_wait_for_clear32(&priv->regs->soft_reset);
/* initialize and reset the address lookup engine */
cpsw_ale_enable(priv, 1);
cpsw_ale_clear(priv, 1);
cpsw_ale_vlan_aware(priv, 0); /* vlan unaware mode */
/* setup host port priority mapping */
__raw_writel(0x76543210, &priv->host_port_regs->cpdma_tx_pri_map);
__raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
/* disable priority elevation and enable statistics on all ports */
__raw_writel(0, &priv->regs->ptype);
/* enable statistics collection only on the host port */
__raw_writel(BIT(priv->host_port), &priv->regs->stat_port_en);
__raw_writel(0x7, &priv->regs->stat_port_en);
cpsw_ale_port_state(priv, priv->host_port, ALE_PORT_STATE_FORWARD);
cpsw_ale_add_ucast(priv, enetaddr, priv->host_port, ALE_SECURE);
cpsw_ale_add_mcast(priv, net_bcast_ethaddr, 1 << priv->host_port);
for_active_slave(slave, priv)
cpsw_slave_init(slave, priv);
cpsw_update_link(priv);
/* init descriptor pool */
for (i = 0; i < NUM_DESCS; i++) {
desc_write(&priv->descs[i], hw_next,
(i == (NUM_DESCS - 1)) ? 0 : &priv->descs[i+1]);
}
priv->desc_free = &priv->descs[0];
/* initialize channels */
if (priv->data.version == CPSW_CTRL_VERSION_2) {
memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER2;
priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER2;
priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER2;
priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER2;
} else {
memset(&priv->rx_chan, 0, sizeof(struct cpdma_chan));
priv->rx_chan.hdp = priv->dma_regs + CPDMA_RXHDP_VER1;
priv->rx_chan.cp = priv->dma_regs + CPDMA_RXCP_VER1;
priv->rx_chan.rxfree = priv->dma_regs + CPDMA_RXFREE;
memset(&priv->tx_chan, 0, sizeof(struct cpdma_chan));
priv->tx_chan.hdp = priv->dma_regs + CPDMA_TXHDP_VER1;
priv->tx_chan.cp = priv->dma_regs + CPDMA_TXCP_VER1;
}
/* clear dma state */
setbit_and_wait_for_clear32(priv->dma_regs + CPDMA_SOFTRESET);
if (priv->data.version == CPSW_CTRL_VERSION_2) {
for (i = 0; i < priv->data.channels; i++) {
__raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER2 + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER2 + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER2 + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER2 + 4
* i);
}
} else {
for (i = 0; i < priv->data.channels; i++) {
__raw_writel(0, priv->dma_regs + CPDMA_RXHDP_VER1 + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_RXFREE + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_RXCP_VER1 + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_TXHDP_VER1 + 4
* i);
__raw_writel(0, priv->dma_regs + CPDMA_TXCP_VER1 + 4
* i);
}
}
__raw_writel(1, priv->dma_regs + CPDMA_TXCONTROL);
__raw_writel(1, priv->dma_regs + CPDMA_RXCONTROL);
/* submit rx descs */
for (i = 0; i < PKTBUFSRX; i++) {
ret = cpdma_submit(priv, &priv->rx_chan, net_rx_packets[i],
PKTSIZE);
if (ret < 0) {
printf("error %d submitting rx desc\n", ret);
break;
}
}
return 0;
}
其中重點關注下cpsw_update_link(priv)-> cpsw_slave_update_link(slave, priv, &link),這個函數會根據根據現有的priv->phydev設備發起phy_startup(在LINK的狀態下解析phydev->speed、phydev->duplex等狀態),之后根據phy_startupde 的結果更新mac_control,最終通過此函數_raw_writel(mac_control, &slave->sliver->mac_control)將mac_control寫入到相關cpsw_priv 的slave->sliver->mac_control寄存器。只有在link狀態下正確配置了slave->sliver->mac_control寄存器,才能與phy正常進行通信。
以上配置好后,就可以在后續使用PING命令進行測試,PING命令使用之前,還需要配置好IP地址,可使用環境變量進行配置,如setenv ipaddr 192.168.1.30。