使能MT7620的第二个SPI接口(cs1)——兼谈pinmux与pinctrl驱动

前言

根据MT7620的数据手册,该芯片支持两个独立的spi接口,由于驱动不完善等种种原因,一直没能顺利的使用第二个spi接口.近日对mt7620的spi好好研究了一下,终于使能了第二个spi接口,将过程记录成文.

 

实现过程

spi驱动的诡异之处

mt7620的spi驱动位于:drivers/spi/spi-rt2880.c,里面有关spi cs1的代码非常诡异,因为7620与5350使用的是同一个spi控制芯片,而该驱动却区别对待,7620只有一个spi,5350有两个.

   1: static struct rt2880_spi_ops spi_ops[] = {
   2:     {
   3:         .init_hw = rt2880_spi_reset,
   4:         .num_cs = 1,
   5:     }, {
   6:         .init_hw = rt5350_spi_reset,
   7:         .num_cs = 2,
   8:     }, 
   9: };
  10:  
  11: static const struct of_device_id rt2880_spi_match[] = {
  12:     { .compatible = "ralink,rt2880-spi", .data = &spi_ops[0]},
  13:     { .compatible = "ralink,rt5350-spi", .data = &spi_ops[1]},
  14:     {},
  15: };

因此,第一个修改便是,将dts文件中对应的compatible修改为5350的版本.重新编译之后,驱动成功加载,然而,问题还远没有解决.

 

cs1没有输出信号

在第二个spi接口上连接一个spi flash芯片作为测试,始终无法probe到芯片.觉得很奇怪,于是用示波器来观察cs1上的信号,居然没有任何信号输出!仔细阅读7620的编程手册,发现了一个关键的内容:

使能MT7620的第二个SPI接口(cs1)——兼谈pinmux与pinctrl驱动_第1张图片

spi_cs1在系统上电默认状态下是用作reference clock的!很显然,该引脚必须设置为normal spi mode.那么问题来了:如何设置这个引脚的功能呢?答案就在pinmux驱动中.

 

pinmux寻踪

很多嵌入式设备都有管脚复用功能,7620也不例外.pinmux数据在arch/mips/ralink/mt7620.c中定义:

   1: static struct rt2880_pmx_func i2c_grp[] =  { FUNC("i2c", 0, 1, 2) };
   2: static struct rt2880_pmx_func spi_grp[] = { FUNC("spi", 0, 3, 4) };
   3: static struct rt2880_pmx_func uartlite_grp[] = { FUNC("uartlite", 0, 15, 2) };
   4: static struct rt2880_pmx_func mdio_grp[] = { FUNC("mdio", 0, 22, 2) };
   5: static struct rt2880_pmx_func rgmii1_grp[] = { FUNC("rgmii1", 0, 24, 12) };
   6: static struct rt2880_pmx_func refclk_grp[] = { FUNC("spi refclk", 0, 37, 3) };
   7: static struct rt2880_pmx_func ephy_grp[] = { FUNC("ephy", 0, 40, 5) };
   8: static struct rt2880_pmx_func rgmii2_grp[] = { FUNC("rgmii2", 0, 60, 12) };
   9: static struct rt2880_pmx_func wled_grp[] = { FUNC("wled", 0, 72, 1) };
  10: static struct rt2880_pmx_func pa_grp[] = { FUNC("pa", 0, 18, 4) };
  11:  
  12: static struct rt2880_pmx_group mt7620a_pinmux_data[] = {
  13:     GRP("i2c", i2c_grp, 1, MT7620_GPIO_MODE_I2C),
  14:     GRP("uartf", uartf_grp, MT7620_GPIO_MODE_UART0_MASK,
  15:         MT7620_GPIO_MODE_UART0_SHIFT),
  16:     GRP("spi", spi_grp, 1, MT7620_GPIO_MODE_SPI),
  17:     GRP("uartlite", uartlite_grp, 1, MT7620_GPIO_MODE_UART1),
  18:     GRP_G("wdt", wdt_grp, MT7620_GPIO_MODE_WDT_MASK,
  19:         MT7620_GPIO_MODE_WDT_GPIO, MT7620_GPIO_MODE_WDT_SHIFT),
  20:     GRP("mdio", mdio_grp, 1, MT7620_GPIO_MODE_MDIO),
  21:     GRP("rgmii1", rgmii1_grp, 1, MT7620_GPIO_MODE_RGMII1),
  22:     GRP("spi refclk", refclk_grp, 1, MT7620_GPIO_MODE_SPI_REF_CLK),
  23:     GRP_G("pcie", pcie_rst_grp, MT7620_GPIO_MODE_PCIE_MASK,
  24:         MT7620_GPIO_MODE_PCIE_GPIO, MT7620_GPIO_MODE_PCIE_SHIFT),
  25:     GRP_G("nd_sd", nd_sd_grp, MT7620_GPIO_MODE_ND_SD_MASK,
  26:         MT7620_GPIO_MODE_ND_SD_GPIO, MT7620_GPIO_MODE_ND_SD_SHIFT),
  27:     GRP("rgmii2", rgmii2_grp, 1, MT7620_GPIO_MODE_RGMII2),
  28:     GRP("wled", wled_grp, 1, MT7620_GPIO_MODE_WLED),
  29:     GRP("ephy", ephy_grp, 1, MT7620_GPIO_MODE_EPHY),
  30:     GRP("pa", pa_grp, 1, MT7620_GPIO_MODE_PA),
  31:     { 0 }
  32: };

如何使能这些复用功能,则由dts与pinmux驱动协同完成,为了能够仔细分析pinmux实现机理,在drivers/pinctrl/pinctrl-rt2880.c的关键函数rt2880_pmx_group_enable中加入诊断代码:

   1: static int rt2880_pmx_group_enable(struct pinctrl_dev *pctrldev,
   2:                 unsigned func,
   3:                 unsigned group)
   4: {
   5:     struct rt2880_priv *p = pinctrl_dev_get_drvdata(pctrldev);
   6:         u32 mode = 0;
   7:     int i;
   8:  
   9:     /* dont allow double use */
  10:     if (p->groups[group].enabled) {
  11:         dev_err(p->dev, "%s is already enabled\n", p->groups[group].name);
  12:         return -EBUSY;
  13:     }
  14:  
  15:     p->groups[group].enabled = 1;
  16:     p->func[func]->enabled = 1;
  17:  
  18:     mode = rt_sysc_r32(SYSC_REG_GPIO_MODE);
  19:     mode &= ~(p->groups[group].mask << p->groups[group].shift);
  20:  
  21:     /* mark the pins as gpio */
  22:     for (i = 0; i < p->groups[group].func[0].pin_count; i++)
  23:         p->gpio[p->groups[group].func[0].pins[i]] = 1;
  24:  
  25:     /* function 0 is gpio and needs special handling */
  26:     if (func == 0) {
  27:         mode |= p->groups[group].gpio << p->groups[group].shift;
  28:     } else {
  29:         for (i = 0; i < p->func[func]->pin_count; i++)
  30:             p->gpio[p->func[func]->pins[i]] = 0;
  31:         mode |= p->func[func]->value << p->groups[group].shift;
  32:     }
  33:     rt_sysc_w32(mode, SYSC_REG_GPIO_MODE);
  34:  
  35:     // manfeel, add debug info
  36:     
  37:     dev_info(p->dev, "%s(%d),%s(%d)\t\t= %x\n", p->groups[group].name, group, p->func[func]->name, func, mode);
  38:  
  39:     return 0;
  40: }

在TTL控制台中,能观察到这些信息:

   1: [    0.080000] rt2880-pinmux pinctrl.1: ephy(12),gpio(0)                = ab11d
   2: [    0.080000] rt2880-pinmux pinctrl.1: wled(11),gpio(0)                = ab11d
   3: [    0.090000] rt2880-pinmux pinctrl.1: pa(13),gpio(0)          = 1ab11d
   4: [    0.090000] rt2880-pinmux pinctrl.1: wdt(4),gpio(0)          = 5ab11d
   5: [    0.100000] rt2880-pinmux pinctrl.1: uartf(1),gpio(0)                = 5ab11d
   6: [    0.100000] rt2880-pinmux pinctrl.1: mdio(5),gpio(0)         = 5ab11d
   7: [    0.110000] bio: create slab <bio-0> at 0
   8: [    0.120000] rt2880_gpio 10000600.gpio: registering 24 gpios
   9: [    0.120000] rt2880_gpio 10000600.gpio: registering 24 irq handlers
  10: [    0.130000] rt2880_gpio 10000638.gpio: registering 16 gpios
  11: [    0.130000] rt2880_gpio 10000638.gpio: registering 16 irq handlers
  12: [    0.140000] rt2880_gpio 10000660.gpio: registering 32 gpios
  13: [    0.140000] rt2880_gpio 10000660.gpio: registering 32 irq handlers
  14: [    0.150000] rt2880_gpio 10000688.gpio: registering 1 gpios
  15: [    0.150000] rt2880_gpio 10000688.gpio: registering 1 irq handlers
  16: ... ...
  17: [    0.160000] rt2880-pinmux pinctrl.1: i2c(0),i2c(1)           = 5ab11c
  18: [    0.170000] i2c-ralink 10000900.i2c: loaded
  19: ... ...
  20: [    0.410000] rt2880-pinmux pinctrl.1: uartlite(3),uartlite(10)                = 5ab11c
  21: [    0.410000] 10000c00.uartlite: ttyS0 at MMIO 0x10000c00 (irq = 20, base_baud = 2500000) is a 16550A
  22: ... ...
  23: [    0.470000] rt2880-pinmux pinctrl.1: spi(2),spi(9)           = 5ab11c
  24: [    0.500000] m25p80 spi32766.0: found en25p64, expected w25q256
  25: [    0.500000] m25p80 spi32766.0: en25p64 (8192 Kbytes)

结合dts文件,能够理解得更加透彻(用I2C来分析):

   1: i2c@900 {
   2:     compatible = "link,mt7620a-i2c", "ralink,rt2880-i2c";
   3:     reg = <0x900 0x100>;
   4:  
   5:     resets = <&rstctrl 16>;
   6:     reset-names = "i2c";
   7:  
   8:     #address-cells = <1>;
   9:     #size-cells = <0>;
  10:  
  11:     status = "disabled";
  12:  
  13:     pinctrl-names = "default";
  14:     pinctrl-0 = <&i2c_pins>;
  15: };
  16: ... ...
  17: pinctrl {
  18:     compatible = "ralink,rt2880-pinmux";
  19:     pinctrl-names = "default";
  20:     pinctrl-0 = <&state_default>;
  21:     
  22:     i2c_pins: i2c {
  23:         i2c {
  24:             ralink,group = "i2c";
  25:             ralink,function = "i2c";
  26:         };
  27:     };
  28: };

dts中,每一个设备节点可能需要用到一些复用管脚,该机制通过pinctrl-0来指定,如i2c中的i2c_pins.我们再来看看spi设备节点中的数据:

   1: spi@b00 {
   2:     compatible = "ralink,mt7620a-spi", "ralink,rt2880-spi";
   3:     reg = <0xb00 0x100>;
   4:  
   5:     resets = <&rstctrl 18>;
   6:     reset-names = "spi";
   7:  
   8:     #address-cells = <1>;
   9:     #size-cells = <1>;
  10:  
  11:     status = "disabled";
  12:  
  13:     pinctrl-names = "default";
  14:     pinctrl-0 = <&spi_pins>;
  15: };

结合前面的知识,对dts修改如下:

   1: spi_pins: spi {
   2:     spi {
   3:         ralink,group = "spi";
   4:         ralink,function = "spi";
   5:     };
   6:     /* added by manfeel */    
   7:     cs1 {
   8:         ralink,group = "spi refclk";
   9:         ralink,function = "spi refclk";
  10:     };
  11: };

至此,mt7620的第二个spi终于可以使用.

 

总结

充分利用dts的功能,能够达到很多意想不到的效果.一句话:dts的强大超乎我们的想象!

最后一点也是最重要的一点:很多7620的开发板在设计的时候,并没有考虑要使用第二个spi接口,因此对cs1的管脚是做悬浮处理的,如果要正确使用spi的cs1,则需要对该引脚做上拉处理,否则会导致系统出现各种奇怪的问题.

你可能感兴趣的:(使能MT7620的第二个SPI接口(cs1)——兼谈pinmux与pinctrl驱动)