Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3047045
  • 博文数量: 396
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4209
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-04 13:04
文章分类

全部博文(396)

文章存档

2022年(1)

2021年(2)

2020年(8)

2019年(24)

2018年(135)

2017年(158)

2016年(68)

我的朋友

分类: 嵌入式

2018-08-14 15:47:47

在前面的一篇文章中,我们分析了一个 uart_driver 的向上注册过程,主要是 tty 的一些东西,知道了 tty 注册了一个字符设备驱动,我们在用户空间 open 时将调用到 uart_port.ops.startup ,在用户空间 write 则调用 uart_port.ops.start_tx ,还知道了如何 read 数据等等。但是,这些都是内核帮我们实现好的,在真正的驱动开发过程中几乎不涉及那些代码的修改移植工作,真正需要我们触碰的是 uart_port 这个结构体,它真正的对应于一个物理的串口。

    其实,真正需要我们做的工作就是 分配一个 uart_port 结构,然后 uart_add_one_port 。分析过 s3c2440 uart 的驱动代码之后,我发现,这么一个简单的目标简直就是经历了山路十八弯。

    先说一下大体的思路,uart_port 的注册过程是基于 platform 平台设备驱动模型,device 侧提供 3 个串口的硬件信息,并注册到 platform_bus_type 中去。然后 driver 也注册到 platform_bus_type 时,就会根据名字进行匹配,从而调用 driver->probe 函数,在 probe 函数里进行 uart_add_one_port 。思路也是很简单的,复杂在 s3c2440 注册 device 之前的工作扯了太多东西。

    先秀个最终分析的图:



一、Linux 启动过程回忆

    在 uboot 启动内核的时候,内核刚刚启动我们就看到串口各种信息就输出来了,也就是说串口驱动的初始化工作是在 Linux 启动过程中一个比较靠前的位置。内核启动的时候首先会去判断 cpu id 是否支持,接着判断是否支持uboot 传递进来的单板 Id ,然后 start_kernel -》setup_arch 进行一系列的初始化工作,其中必然包含串口相关初始化。

内核中所有支持的单板都用 MACHINE_START 和 MACHINE_END 来定义

  1. MACHINE_START(MINI2440, "FriendlyARM Mini2440 development board")
  2. .phys_io = S3C2410_PA_UART,
  3. .io_pg_offst = (((u32)S3C24XX_VA_UART) >> 18) & 0xfffc,
  4. .boot_params = S3C2410_SDRAM_PA + 0x100,
  5. .init_irq = s3c24xx_init_irq,
  6. .map_io = mini2440_map_io,
  7. .init_machine = mini2440_machine_init,
  8. .timer = &s3c24xx_timer,
  9. MACHINE_END
    但是,里面的这些函数是何时被调用的,调用的先后顺序是怎样的,我们需要分析 Linux 的启动流程才能知道,信息量还是比较大的,在前面的一篇文章中我分析过了

请参考:http://blog.csdn.net/lizuobin2/article/details/51779064

    如果你自己分析一遍的话,调用先后顺序应该是这样的:

    start_kernel -》setup_arch -》 map_io -》 init_irq -》 timer -》 init_machine -》 s3c_arch_init -》 s3c24xx_serial_modinit -》s3c2440_serial_init

    后面三个函数是通过类似于 module_init 等被组织进内核里去的放在一个特殊的段里,内核启动到一定时候就去把这个段里的每一个函数取出来去调用,也是与串口相关的,分析过程就不再赘述了。


二、platform device 的注册之路

    分析出了整个的串口驱动的初始化、设置、注册流程,问题就简单多了,挨个函数分析便是。

  1. static void __init mini2440_map_io(void)
  2. {
  3. s3c24xx_init_io(mini2440_iodesc, ARRAY_SIZE(mini2440_iodesc));
  4. s3c24xx_init_clocks(12000000);
  5. s3c24xx_init_uarts(mini2440_uartcfgs, ARRAY_SIZE(mini2440_uartcfgs));
  6. }
    能一看瞅出来的,外部晶振的频率 12M ,如果我们在移植其它单板的时候不是,记得修改。
  1. void __init s3c24xx_init_io(struct map_desc *mach_desc, int size)
  2. {
  3. ....
  4. s3c_init_cpu(idcode, cpu_ids, ARRAY_SIZE(cpu_ids));
  5. }
  1. static struct cpu_table *cpu;
  2. void __init s3c_init_cpu(unsigned long idcode,
  3. struct cpu_table *cputab, unsigned int cputab_size)
  4. {
  5. cpu = s3c_lookup_cpu(idcode, cputab, cputab_size);
  6. cpu->map_io();
  7. }
  1. static struct cpu_table * __init s3c_lookup_cpu(unsigned long idcode,
  2. struct cpu_table *tab,
  3. unsigned int count)
  4. {
  5. for (; count != 0; count--, tab++) {
  6. if ((idcode & tab->idmask) == tab->idcode)
  7. return tab;
  8. }
  9. return NULL;
  10. }
  1. static struct cpu_table cpu_ids[] __initdata = {
  2. {
  3. .idcode = 0x32440000,
  4. .idmask = 0xffffffff,
  5. .map_io = s3c244x_map_io,
  6. .init_clocks = s3c244x_init_clocks,
  7. .init_uarts = s3c244x_init_uarts,
  8. .init = s3c2440_init,
  9. .name = name_s3c2440
  10. },
  11. };
    上边四段代码费尽周折,只为调用 cpu_ids 数组里的 s3c244x_map_io 函数。
  1. void __init s3c244x_map_io(void)
  2. {
  3. /* register our io-tables */
  4. iotable_init(s3c244x_iodesc, ARRAY_SIZE(s3c244x_iodesc));
  5. /* rename any peripherals used differing from the s3c2410 */
  6. s3c_device_sdi.name = "s3c2440-sdi";
  7. s3c_device_i2c0.name = "s3c2440-i2c";
  8. s3c_device_nand.name = "s3c2440-nand";
  9. s3c_device_usbgadget.name = "s3c2440-usbgadget";
  10. }
    也是醉醉的,竟然跟串口毫无关系。下面看 s3c24xx_init_uarts
  1. void __init s3c24xx_init_uarts(struct s3c2410_uartcfg *cfg, int no)
  2. {
  3. (cpu->init_uarts)(cfg, no);
  4. }
    呵,前边的工作果然也不是完全白做的,至少帮我们找到了 cpu ,那么就是调用 s3c244x_init_uarts 咯
  1. void __init s3c244x_init_uarts(struct s3c2410_uartcfg *cfg, int no)
  2. {
  3. s3c24xx_init_uartdevs("s3c2440-uart", s3c2410_uart_resources, cfg, no);
  4. }
    继续往下看之前,我们先看一下参数 cfg , no ,s3c2410_uart_resources
s3c24xx_init_uarts(mini2440_uartcfgs, ARRAY_SIZE(mini2440_uartcfgs));
  1. static struct s3c2410_uartcfg mini2440_uartcfgs[] __initdata = {
  2. [0] = {
  3. .hwport = 0,
  4. .flags = 0,
  5. .ucon = 0x3c5,
  6. .ulcon = 0x03,
  7. .ufcon = 0x51,
  8. },
  9. /* 此处略去了 1、2 两个串口的信息 */
  10. };

  1. struct s3c24xx_uart_resources s3c2410_uart_resources[] __initdata = {
  2. [0] = {
  3. .resources = s3c2410_uart0_resource,
  4. .nr_resources = ARRAY_SIZE(s3c2410_uart0_resource),
  5. },
  6. /* 此处略去了 1、2 串口的信息 */
  7. };
  1. static struct resource s3c2410_uart0_resource[] = {
  2. [0] = {
  3. .start = S3C2410_PA_UART0,
  4. .end = S3C2410_PA_UART0 + 0x3fff,
  5. .flags = IORESOURCE_MEM,
  6. },
  7. [1] = {
  8. .start = IRQ_S3CUART_RX0,
  9. .end = IRQ_S3CUART_ERR0,
  10. .flags = IORESOURCE_IRQ,
  11. }
  12. };
    万事俱备,开始构建 device

  1. struct platform_device *s3c24xx_uart_src[4] = {
  2. &s3c24xx_uart_device0,
  3. &s3c24xx_uart_device1,
  4. &s3c24xx_uart_device2,
  5. &s3c24xx_uart_device3,
  6. };

  1. static struct s3c2410_uartcfg uart_cfgs[CONFIG_SERIAL_SAMSUNG_UARTS];
  2. /* 填充平台设备的过程,未注册 */
  3. void __init s3c24xx_init_uartdevs(char *name, struct s3c24xx_uart_resources *res,
  4. struct s3c2410_uartcfg *cfg, int no)
  5. {
  6. struct platform_device *platdev;
  7. struct s3c2410_uartcfg *cfgptr = uart_cfgs;
  8. struct s3c24xx_uart_resources *resp;
  9. int uart;
  10. /* 将 mini2440_uartcfgs 数组里的参数拷贝到 cfgptr */
  11. memcpy(cfgptr, cfg, sizeof(struct s3c2410_uartcfg) * no);
  12. for (uart = 0; uart < no; uart++, cfg++, cfgptr++) {
  13. /* 从 s3c24xx_uart_src 数组里取出平台设备 */
  14. platdev = s3c24xx_uart_src[cfgptr->hwport];
  15. /* 获得对应的 resource ,物理寄存器和中断 */
  16. resp = res + cfgptr->hwport;
  17. /* 将 s3c24xx_uart_src 的平台设备 放到 平台设备数组 s3c24xx_uart_devs */
  18. s3c24xx_uart_devs[uart] = platdev;
  19. /* 设置名字 资源 */
  20. platdev->name = name;
  21. platdev->resource = resp->resources;
  22. platdev->num_resources = resp->nr_resources;
  23. /* 设置平台数据 mini2440_uartcfgs 数组里的东西 */
  24. platdev->dev.platform_data = cfgptr;
  25. }
  26. nr_uarts = no;
  27. }
    至此,device 构建设置完毕,等待注册:

    1、3 个串口的 device 存放在 s3c24xx_uart_devs 数组里,后边肯定会从数组里取出来注册。

    2、3 个串口的 device 的名字都是 “s3c2440-uart”。

    3、3 个串口的 device 资源文件里存放好了 io 物理地址,Irq 等信息。

    4、3 个串口的 device 资源数据。
    移植过程中可能需要修改的文件:mini2440_uartcfgs 、s3c2410_uart0_resource 、s3c24xx_uart_src 还有那个晶振频率。

    s3c_arch_init 函数中,将 device 注册到 platform_bus_type

  1. static int __init s3c_arch_init(void)
  2. {
  3. int ret;
  4. ret = platform_add_devices(s3c24xx_uart_devs, nr_uarts);
  5. return ret;
  6. }

三、uart_driver 的注册

    注意,是 uart_driver 的注册,是上一篇文章讲的过程,并不是对应于平台设备的平台驱动。为什么在这个时候注册 uart_driver,因为如果先注册平台设备的 driver 的话,那么在probe函数里 uart_add_one_port ,uart_prot 没地方注册!!因此,要先注册 uart_driver ,简单贴下代码,不在分析。

  1. static struct uart_driver s3c24xx_uart_drv = {
  2. .owner = THIS_MODULE,
  3. .dev_name = "s3c2410_serial",
  4. .nr = CONFIG_SERIAL_SAMSUNG_UARTS,
  5. .cons = S3C24XX_SERIAL_CONSOLE,
  6. .driver_name = S3C24XX_SERIAL_NAME,
  7. .major = S3C24XX_SERIAL_MAJOR,
  8. .minor = S3C24XX_SERIAL_MINOR,
  9. };
  10. static int __init s3c24xx_serial_modinit(void)
  11. {
  12. int ret;
  13. ret = uart_register_driver(&s3c24xx_uart_drv);
  14. if (ret < 0) {
  15. printk(KERN_ERR "failed to register UART driver\n");
  16. return -1;
  17. }
  18. return 0;
  19. }
附上上一篇文章的地址:http://blog.csdn.net/lizuobin2/article/details/51773305

四、platform driver 的注册以及 probe 函数

  1. static int s3c2440_serial_probe(struct platform_device *dev)
  2. {
  3. dbg("s3c2440_serial_probe: dev=%p\n", dev);
  4. return s3c24xx_serial_probe(dev, &s3c2440_uart_inf);
  5. }
  6. static struct platform_driver s3c2440_serial_driver = {
  7. .probe = s3c2440_serial_probe,
  8. .remove = __devexit_p(s3c24xx_serial_remove),
  9. .driver = {
  10. .name = "s3c2440-uart",
  11. .owner = THIS_MODULE,
  12. },
  13. };
  14. s3c24xx_console_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
  15. static int __init s3c2440_serial_init(void)
  16. {
  17. return s3c24xx_serial_init(&s3c2440_serial_driver, &s3c2440_uart_inf);
  18. }
    将驱动注册到 platform_bus_type ,此时会遍历 platform_bus_type 的 deivce 链表,取出 device 进行名字比较,我们前边注册的三个device的名字是一样的,没关系 Linux 允许这样做,每次匹配到一个都调用一次 Probe 函数。

  1. static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
  2. [0] = {
  3. .port = {
  4. .lock = __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
  5. .iotype = UPIO_MEM,
  6. .irq = IRQ_S3CUART_RX0,
  7. .uartclk = 0,
  8. .fifosize = 16,
  9. .ops = &s3c24xx_serial_ops,/* 底层的操作函数 */
  10. .flags = UPF_BOOT_AUTOCONF,
  11. .line = 0,
  12. }
  13. },
  14. /* 此处略去了两个串口的信息 */
  15. };
  1. int s3c24xx_serial_probe(struct platform_device *dev,
  2. struct s3c24xx_uart_info *info)
  3. {
  4. struct s3c24xx_uart_port *ourport;
  5. int ret;
  6. /* 取出 uart_port */
  7. ourport = &s3c24xx_serial_ports[probe_index];
  8. probe_index++;
  9. /* 对 uart_port 进一步设置 */
  10. ret = s3c24xx_serial_init_port(ourport, info, dev);
  11. /* 将 uart_port 注册到 uart_driver */
  12. uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
  13. platform_set_drvdata(dev, &ourport->port);
  14. ret = device_create_file(&dev->dev, &dev_attr_clock_source);
  15. ret = s3c24xx_serial_cpufreq_register(ourport);
  16. return 0;
  17. }
  1. static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
  2. struct s3c24xx_uart_info *info,
  3. struct platform_device *platdev)
  4. {
  5. struct uart_port *port = &ourport->port;
  6. struct s3c2410_uartcfg *cfg;
  7. struct resource *res;
  8. int ret;
  9. cfg = s3c24xx_dev_to_cfg(&platdev->dev);
  10. /* setup info for port */
  11. port->dev = &platdev->dev;
  12. ourport->info = info;
  13. /* copy the info in from provided structure */
  14. ourport->port.fifosize = info->fifosize;
  15. "white-space:pre"> /* 设置时钟 */
  16. port->uartclk = 1;
  17. /* sort our the physical and virtual addresses for each UART */
  18. res = platform_get_resource(platdev, IORESOURCE_MEM, 0);
  19. "white-space:pre"> /* 设置物理地址,虚拟地址 */
  20. port->mapbase = res->start;
  21. port->membase = S3C_VA_UART + res->start - (S3C_PA_UART & 0xfff00000);
  22. ret = platform_get_irq(platdev, 0);
  23. if (ret < 0)
  24. port->irq = 0;
  25. else {
  26. port->irq = ret;/* 设置中断号 */
  27. ourport->rx_irq = ret;
  28. ourport->tx_irq = ret + 1;
  29. }
  30. ret = platform_get_irq(platdev, 1);
  31. ourport->clk = clk_get(&platdev->dev, "uart");
  32. /* reset the fifos (and setup the uart) */
  33. s3c24xx_serial_resetport(port, cfg);
  34. return 0;
  35. }
  1. int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)
  2. {
  3. struct uart_state *state;
  4. struct tty_port *port;
  5. int ret = 0;
  6. struct device *tty_dev;
  7. BUG_ON(in_interrupt());
  8. if (uport->line >= drv->nr)
  9. return -EINVAL;
  10. state = drv->state + uport->line;
  11. port = &state->port;
  12. mutex_lock(&port_mutex);
  13. mutex_lock(&port->mutex);
  14. if (state->uart_port) {
  15. ret = -EINVAL;
  16. goto out;
  17. }
  18. "white-space:pre"> /* 将 uart_prot 绑定到 uart_driver 对应的 state */
  19. state->uart_port = uport;
  20. state->pm_state = -1;
  21. uport->cons = drv->cons;
  22. uport->state = state;
  23. /*
  24. * If this port is a console, then the spinlock is already
  25. * initialised.
  26. */
  27. if (!(uart_console(uport) && (uport->cons->flags & CON_ENABLED))) {
  28. spin_lock_init(&uport->lock);
  29. lockdep_set_class(&uport->lock, &port_lock_key);
  30. }
  31. /* 实际调用 port->ops->config_port(port, flags) 稍后再看 */
  32. uart_configure_port(drv, state, uport);
  33. /*
  34. * 上一篇文章中,我们提到tty注册了一个字符设备 “ttySAC ”
  35. * 那么,我们平时看到的 “ttySAC0”“ttySAC1”等就是在这里注册的
  36. */
  37. tty_dev = tty_register_device(drv->tty_driver, uport->line, uport->dev);
  38. if (likely(!IS_ERR(tty_dev))) {
  39. device_init_wakeup(tty_dev, 1);
  40. device_set_wakeup_enable(tty_dev, 0);
  41. } else
  42. printk(KERN_ERR "Cannot register tty device on line %d\n",
  43. uport->line);
  44. /*
  45. * Ensure UPF_DEAD is not set.
  46. */
  47. uport->flags &= ~UPF_DEAD;
  48. out:
  49. mutex_unlock(&port->mutex);
  50. mutex_unlock(&port_mutex);
  51. return ret;
  52. }
  1. struct device *tty_register_device(struct tty_driver *driver, unsigned index,
  2. struct device *device)
  3. {
  4. char name[64];
  5. dev_t dev = MKDEV(driver->major, driver->minor_start) + index;
  6. if (index >= driver->num) {
  7. printk(KERN_ERR "Attempt to register invalid tty line number "
  8. " (%d).\n", index);
  9. return ERR_PTR(-EINVAL);
  10. }
  11. if (driver->type == TTY_DRIVER_TYPE_PTY)
  12. pty_line_name(driver, index, name);
  13. else
  14. tty_line_name(driver, index, name);
  15. return device_create(tty_class, device, dev, NULL, name);
  16. }
  1. "font-family:SimSun;font-size:18px;">static void tty_line_name(struct tty_driver *driver, int index, char *p)
  2. {
  3. sprintf(p, "%s%d", driver->name, index + driver->name_base);
  4. }
   tty_driver->name == "ttySAC",在此基础上加上 uart_port.line ,就组成了具体串口的设备节点的名字,例如“ttySAC0”。

    分析到这里,完了么?没有,还有一个非常重要的东西没有分析呢,那就是底层的操作函数。

  1. static struct uart_ops s3c24xx_serial_ops = {
  2. .pm = s3c24xx_serial_pm,
  3. .tx_empty = s3c24xx_serial_tx_empty,
  4. .get_mctrl = s3c24xx_serial_get_mctrl,
  5. .set_mctrl = s3c24xx_serial_set_mctrl,
  6. .stop_tx = s3c24xx_serial_stop_tx,
  7. .start_tx = s3c24xx_serial_start_tx,
  8. .stop_rx = s3c24xx_serial_stop_rx,
  9. .enable_ms = s3c24xx_serial_enable_ms,
  10. .break_ctl = s3c24xx_serial_break_ctl,
  11. .startup = s3c24xx_serial_startup,
  12. .shutdown = s3c24xx_serial_shutdown,
  13. .set_termios = s3c24xx_serial_set_termios,
  14. .type = s3c24xx_serial_type,
  15. .release_port = s3c24xx_serial_release_port,
  16. .request_port = s3c24xx_serial_request_port,
  17. .config_port = s3c24xx_serial_config_port,
  18. .verify_port = s3c24xx_serial_verify_port,
  19. };
    这么多的函数,如果让我们自己来实现,那相比真得头都大了。一般芯片厂家会帮我们搞得吧。其他的不分析了,分析一个 startup 函数,因为我们在用户空间 open 的时候会调用它,那么必然有一些初始化的工作。
  1. static int s3c24xx_serial_startup(struct uart_port *port)
  2. {
  3. struct s3c24xx_uart_port *ourport = to_ourport(port);
  4. int ret;
  5. dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
  6. port->mapbase, port->membase);
  7. rx_enabled(port) = 1;
  8. ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
  9. s3c24xx_serial_portname(port), ourport);
  10. if (ret != 0) {
  11. printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
  12. return ret;
  13. }
  14. ourport->rx_claimed = 1;
  15. dbg("requesting tx irq...\n");
  16. tx_enabled(port) = 1;
  17. ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
  18. s3c24xx_serial_portname(port), ourport);
  19. if (ret) {
  20. printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
  21. goto err;
  22. }
  23. ourport->tx_claimed = 1;
  24. dbg("s3c24xx_serial_startup ok\n");
  25. /* the port reset code should have done the correct
  26. * register setup for the port controls */
  27. if (port->line == 2) {
  28. s3c2410_gpio_cfgpin(S3C2410_GPH(6), S3C2410_GPH6_TXD2);
  29. s3c2410_gpio_pullup(S3C2410_GPH(6), 1);
  30. s3c2410_gpio_cfgpin(S3C2410_GPH(7), S3C2410_GPH7_RXD2);
  31. s3c2410_gpio_pullup(S3C2410_GPH(7), 1);
  32. }
  33. return ret;
  34. err:
  35. s3c24xx_serial_shutdown(port);
  36. return ret;
  37. }
    主要工作是注册了两个中断,发送中断,接收中断,来看看一个和我们上篇文章的猜测是否一致。

  1. static irqreturn_t
  2. s3c24xx_serial_rx_chars(int irq, void *dev_id)
  3. {
  4. ..../* 调用线路规程的...和上篇文章一致 */
  5. tty_flip_buffer_push(tty);
  6. out:
  7. return IRQ_HANDLED;
  8. }
  1. static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
  2. {
  3. ....
  4. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  5. uart_write_wakeup(port);
  6. if (uart_circ_empty(xmit))
  7. s3c24xx_serial_stop_tx(port);
  8. out:
  9. return IRQ_HANDLED;
  10. }
  1. void uart_write_wakeup(struct uart_port *port)
  2. {
  3. struct uart_state *state = port->state;
  4. /*
  5. * This means you called this function _after_ the port was
  6. * closed. No cookie for you.
  7. */
  8. BUG_ON(!state);
  9. tasklet_schedule(&state->tlet);/* 也是一致的 */
  10. }
阅读(1523) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~