Xilinx的socket CAN驱动介绍

在Linux中,CAN总线的驱动有两种实现方式:字符设备以及socket can驱动。Socket CAN使用伯克利的Socket接口和Linux网络协议栈,这种方法使得CAN设备驱动可以通过网络接口来调用。Socket CAN的接口被设计的尽量接近TCP/IP的协议,让那些熟悉网络编程的程序员能够比较容易的学习和使用。

本文以赛灵思的Zynq-7000为硬件背景,详细介绍开发板上的socket can驱动。主要的驱动文件为dev.c以及xilinx_can.c,可以从
https://github.com/Xilinx/linux-xlnx 获取。

首先看一下传递给内核的dts文件中的can设备信息:
ps7_can_0: ps7-can@e0008000 {
clock-names = "ref_clk", "aper_clk";
clocks = <&clkc 19>, <&clkc 36>;
compatible = "xlnx,ps7-can-1.00.a", "xlnx,ps7-can";
interrupt-parent = <&ps7_scugic_0>;
interrupts = <0 28 4>;
reg = <0xe0008000 0x1000>;
} ;

指定了寄存器地址范围,中断号,驱动适配版本以及参考时钟源。

再来看xilinx_can.c文件中的代码:
/* Match table for OF platform binding */
static struct of_device_id xcan_of_match[] = {
{ .compatible = "xlnx,ps7-can", },
{ .compatible = "xlnx,axi-can-1.00.a", },
{ /* end of list */ },
};
MODULE_DEVICE_TABLE(of, xcan_of_match);

static struct platform_driver xcan_driver = {
.probe = xcan_probe,
.remove = xcan_remove,
.driver = {
.owner = THIS_MODULE,
.name = DRIVER_NAME,
.pm = &xcan_dev_pm_ops,
.of_match_table = xcan_of_match,
},
};

可见对于PS的CAN接口以及PL的基于axi总线的CAN接口,均可以使用该platform_driver驱动。

ARM上电之后,linux初始化函数会依据dts信息将CAN硬件信息加入到系统的硬件链表中,当驱动程序装载时,会去遍历该链表获取硬件信息,比如寄存器地址、中断号等,然后调用ioremap、request_irq等,进一步初始化硬件。

接着看xcan_probe函数:
/**
* xcan_probe - Platform registration call
* @pdev: Handle to the platform device structure
*
* This function does all the memory allocation and registration for the CAN
* device.
*
* Return: 0 on success and failure value on error
*/
static int xcan_probe(struct platform_device *pdev)
{
struct resource *res; /* IO mem resources */
struct net_device *ndev;
struct xcan_priv *priv;
struct device *dev = &pdev->dev;
int ret, irq;

/* Create a CAN device instance */
ndev = alloc_candev(sizeof(struct xcan_priv), XCAN_ECHO_SKB_MAX);
if (!ndev)
return -ENOMEM;

priv = netdev_priv(ndev);
priv->dev = ndev;
priv->can.bittiming_const = &xcan_bittiming_const;
priv->can.do_set_bittiming = xcan_set_bittiming;
priv->can.do_set_mode = xcan_do_set_mode;
priv->can.do_get_berr_counter = xcan_get_berr_counter;
priv->can.ctrlmode_supported = CAN_CTRLMODE_LOOPBACK;
priv->waiting_ech_skb_index = 0;
priv->ech_skb_next = 0;
priv->waiting_ech_skb_num = 0;
priv->xcan_echo_skb_max = XCAN_ECHO_SKB_MAX;

/* Get IRQ for the device */
ndev->irq = platform_get_irq(pdev, 0);
irq = devm_request_irq(&pdev->dev, ndev->irq, &xcan_interrupt,
priv->irq_flags, dev_name(&pdev->dev),
(void *)ndev);
if (irq < 0) {
ret = irq;
dev_err(&pdev->dev, "Irq allocation for CAN failed\n");
goto err_free;
}

spin_lock_init(&priv->ech_skb_lock);
ndev->flags |= IFF_ECHO; /* We support local echo */

platform_set_drvdata(pdev, ndev);
SET_NETDEV_DEV(ndev, &pdev->dev);
ndev->netdev_ops = &xcan_netdev_ops;

/* Get the virtual base address for the device */
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
priv->reg_base = devm_ioremap_resource(&pdev->dev, res);
if (IS_ERR(priv->reg_base)) {
ret = PTR_ERR(priv->reg_base);
goto err_free;
}
ndev->mem_start = res->start;
ndev->mem_end = res->end;

priv->write_reg = xcan_write_reg;
priv->read_reg = xcan_read_reg;

/* Getting the CAN devclk info */
priv->devclk = devm_clk_get(&pdev->dev, "ref_clk");
if (IS_ERR(priv->devclk)) {
dev_err(&pdev->dev, "Device clock not found.\n");
ret = PTR_ERR(priv->devclk);
goto err_free;
}

/* Check for type of CAN device */
if (of_device_is_compatible(pdev->dev.of_node, "xlnx,ps7-can")) {
priv->aperclk = devm_clk_get(&pdev->dev, "aper_clk");
if (IS_ERR(priv->aperclk)) {
dev_err(&pdev->dev, "aper clock not found\n");
ret = PTR_ERR(priv->aperclk);
goto err_free;
}
} else {
priv->aperclk = priv->devclk;
}

ret = clk_prepare_enable(priv->devclk);
if (ret) {
dev_err(&pdev->dev, "unable to enable device clock\n");
goto err_free;
}

ret = clk_prepare_enable(priv->aperclk);
if (ret) {
dev_err(&pdev->dev, "unable to enable aper clock\n");
goto err_unprepar_disabledev;
}

priv->can.clock.freq = clk_get_rate(priv->devclk);

ret = register_candev(ndev);
if (ret) {
dev_err(&pdev->dev, "fail to register failed (err=%d)\n", ret);
goto err_unprepar_disableaper;
}

dev_info(&pdev->dev,
"reg_base=0x%p irq=%d clock=%d, tx fifo depth:%d\n",
priv->reg_base, ndev->irq, priv->can.clock.freq,
priv->xcan_echo_skb_max);

return 0;

err_unprepar_disableaper:
clk_disable_unprepare(priv->aperclk);
err_unprepar_disabledev:
clk_disable_unprepare(priv->devclk);
err_free:
free_candev(ndev);

return ret;
}

当一个设备驱动通过driver_register加入对应的驱动总线下时,会去遍历对应总线下的设备双向链表,当驱动和设备匹配时,会触发驱动的probe函数,probe函数的传入参数pdev即为遍历得到的设备信息。

struct xcan_priv为CAN私有数据结构,包含struct can_priv、struct net_device等数据成员,注意can_priv结构成员一定要放在第一位,具体原因参考 http://blog.sina.com.cn/s/blog_636a55070101mc2d.html

/**
* struct xcan_priv - This definition define CAN driver instance
* @can: CAN private data structure.
* @open_time: For holding timeout values
* @waiting_ech_skb_index: Pointer for skb
* @ech_skb_next: This tell the next packet in the queue
* @waiting_ech_skb_num: Gives the number of packets waiting
* @xcan_echo_skb_max: Maximum number packets the driver CAN send
* @ech_skb_lock: For spinlock purpose
* @read_reg: For reading data from CAN registers
* @write_reg: For writing data to CAN registers
* @dev: Network device data structure
* @reg_base: Ioremapped address to registers
* @irq_flags: For request_irq()
* @aperclk: Pointer to struct clk
* @devclk: Pointer to struct clk
*/
struct xcan_priv {
struct can_priv can;
int open_time;
int waiting_ech_skb_index;
int ech_skb_next;
int waiting_ech_skb_num;
int xcan_echo_skb_max;
spinlock_t ech_skb_lock;
u32 (*read_reg)(const struct xcan_priv *priv, int reg);
void (*write_reg)(const struct xcan_priv *priv, int reg, u32 val);
struct net_device *dev;
void __iomem *reg_base;
unsigned long irq_flags;
struct clk *aperclk;
struct clk *devclk;
};

调用alloc_candev()函数获取一个net_device变量,设置socket buffer大小为XCAN_ECHO_SKB_MAX=64个字节。

接着是对struct xcan_priv *priv指针的初始化。
priv->can.do_set_bittiming = xcan_set_bittiming;
priv->can.do_set_mode = xcan_do_set_mode;
priv->can.do_get_berr_counter = xcan_get_berr_counter;

CAN接口的位速率设置函数,模式设置函数,数据传输错误计数函数。

牵涉到CAN接口的具体操作函数代码如下:
static const struct net_device_ops xcan_netdev_ops = {
.ndo_open = xcan_open,
.ndo_stop = xcan_close,
.ndo_start_xmit = xcan_start_xmit,
};

ndev->netdev_ops = &xcan_netdev_ops;

主要是指定struct net_device *ndev指针的打开关闭以及数据发送函数。

然后指定struct xcan_priv *priv指针的读写寄存器函数。
static void xcan_write_reg(const struct xcan_priv *priv, int reg, u32 val)
{
writel(val, priv->reg_base + reg);
}
static u32 xcan_read_reg(const struct xcan_priv *priv, int reg)
{
return readl(priv->reg_base + reg);
}
.............

priv->write_reg = xcan_write_reg;
priv->read_reg = xcan_read_reg;

接着获取设备时钟源并使能,注意Zynq-7000要求每个设备有两个时钟源。

然后注册CAN设备,其实是注册net设备,只不过指定net设备的操作函数为CAN特定的操作函数。
ret = register_candev(ndev);
............
int register_candev(struct net_device *dev)
{
dev->rtnl_link_ops = &can_link_ops;
return register_netdev(dev);
}
.......
static struct rtnl_link_ops can_link_ops __read_mostly = {
.kind = "can",
.maxtype = IFLA_CAN_MAX,
.policy = can_policy,
.setup = can_setup,
.newlink = can_newlink,
.changelink = can_changelink,
.get_size = can_get_size,
.fill_info = can_fill_info,
.get_xstats_size = can_get_xstats_size,
.fill_xstats = can_fill_xstats,
};

至于xcan_remove()函数不再详述。
static int xcan_remove(struct platform_device *pdev)
{
struct net_device *ndev = platform_get_drvdata(pdev);
struct xcan_priv *priv = netdev_priv(ndev);

if (set_reset_mode(ndev) < 0)
netdev_err(ndev, "mode resetting failed!\n");

unregister_candev(ndev);
clk_disable_unprepare(priv->aperclk);
clk_disable_unprepare(priv->devclk);

free_candev(ndev);

return 0;
}

有关socket can应用层程序的编写可以参考Documentation\networking\can.txt。

当使用socket打开can接口时,会调用到xcan_open()函数:
static int xcan_open(struct net_device *ndev)
{
struct xcan_priv *priv = netdev_priv(ndev);
int err;

/* Set chip into reset mode */
err = set_reset_mode(ndev);
if (err < 0)
netdev_err(ndev, "mode resetting failed failed!\n");

/* Common open */
err = open_candev(ndev);
if (err)
return err;

err = xcan_start(ndev);
if (err < 0)
netdev_err(ndev, "xcan_start failed!\n");

priv->open_time = jiffies;

can_led_event(ndev, CAN_LED_EVENT_OPEN);
netif_start_queue(ndev);

return 0;
}

首先reset CAN,进入config mode;然后调用dev.c中的open_candev()函数打开CAN接口;调用xcan_start()函数,进入Normal mode,主要是使能中断,依据应用层传入的mode参数设置loopback mode或者normal mode;然后使能CAN接口,并等待XCAN_SR_OFFSET寄存器进入对应的模式。
static int set_normal_mode(struct net_device *ndev)
{
struct xcan_priv *priv = netdev_priv(ndev);

/* Enable interrupts */
priv->write_reg(priv, XCAN_IER_OFFSET, XCAN_IXR_TXOK_MASK |
XCAN_IXR_BSOFF_MASK | XCAN_IXR_WKUP_MASK |
XCAN_IXR_SLP_MASK | XCAN_IXR_RXNEMP_MASK |
XCAN_IXR_ERROR_MASK | XCAN_IXR_ARBLST_MASK);

/* Check whether it is loopback mode or normal mode */
if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK)
/* Put device into loopback mode */
priv->write_reg(priv, XCAN_MSR_OFFSET, XCAN_MSR_LBACK_MASK);
else
/* The device is in normal mode */
priv->write_reg(priv, XCAN_MSR_OFFSET, 0);

if (priv->can.state == CAN_STATE_STOPPED) {
/* Enable Xilinx CAN */
priv->write_reg(priv, XCAN_SRR_OFFSET, XCAN_SRR_CEN_MASK);
priv->can.state = CAN_STATE_ERROR_ACTIVE;
if (priv->can.ctrlmode & CAN_CTRLMODE_LOOPBACK) {
while ((priv->read_reg(priv, XCAN_SR_OFFSET) &
XCAN_SR_LBACK_MASK) == 0)
;
} else {
while ((priv->read_reg(priv, XCAN_SR_OFFSET)
& XCAN_SR_NORMAL_MASK) == 0)
;
}
netdev_dbg(ndev, "status:#x%08x\n",
priv->read_reg(priv, XCAN_SR_OFFSET));
}

return 0;
}

最后调用netif_start_queue()函数使能发送队列。

对应的xcan_close()函数不再分析。
static int xcan_close(struct net_device *ndev)
{
struct xcan_priv *priv = netdev_priv(ndev);

netif_stop_queue(ndev);
if (set_reset_mode(ndev) < 0)
netdev_err(ndev, "mode resetting failed failed!\n");

close_candev(ndev);

priv->open_time = 0;

can_led_event(ndev, CAN_LED_EVENT_STOP);

return 0;
}

带NAPI的中断轮询数据接收模式
中断接收数据模式在数据频繁情况下,中断触发负载过大,系统性能受到影响,为此基于轮询的接收模式被开发,称为New API,即NAPI。

NAPI仍然需要首次数据包接收中断来触发poll过程,第一次接收中断发生后,中断处理程序禁止设备的接收中断,通过poll方式读取设备的接收缓冲区后,再次使能中断。

NAPI函数的调用过程如下:
netif_napi_add
...
napi_enable
...
关中断
napi_schedule
...
netif_receive_skb
napi_complete

文章转载自:linyangspring的专栏

1 条评论

(1楼)zynq> ip link set can0 type

PIZI WO 在 星期日, 10/14/2018 - 13:23 发表。

zynq> ip link set can0 type can bitrate 500000
zynq> ip link set can0 up
[ 26.624178] xilinx_can e0008000.can can0: timed out for correct mode
[ 26.630502] xilinx_can e0008000.can can0: xcan_chip_start failed!
RTNETLINK answers: Connection timed out
请问遇到过这样的情况吗?