前言
實時鐘,英文名RTC(Real Time Clock),在PC裏面很常見,在OpenWrt裏面卻鮮有提及,手頭上有一個DS1307的TinyRTC實時鐘模塊,經過一番折騰,將其融合到了OpenWrt系統,遂將操作過程記錄成文.
背景知識
DS1307在最新的OpenWrt中已經提供支持,卻沒有整合進ramips中,因此,本文的一個重點便是如何爲ramips系統配置ds1307.另外,爲ds1307編寫合適的dts節點也是本文的一個重點.
實現過程
1.爲ramips配置ds1307支持
系統在./scripts/medatata.pl中判斷並處理RTC_SUPPORT開關,分析之後,原來是在 target/linux/ramips/mt7620/target.mk中,將
原始的內容:
FEATURES+=usb
修改爲:
FEATURES+=usb rtc
即可打開mt7620對rtc的支持.
此時,make kernel_menuconfig進入配置菜單.同時應注意,由於ds1307是i2c接口的模塊,因此,在device中需要配置i2c的支持.選中i2c之後,便可以在
Device Drivers -> Real Time Clock中看到ds1307
.config - Linux/mips 3.14.28 Kernel Configuration
> Device Drivers > Real Time Clock ─────────────────────────────────────
┌──── Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025 ────┐
│ CONFIG_RTC_DRV_DS1307: │
│ │
│ If you say yes here you get support for various compatible RTC │
│ chips (often with battery backup) connected with I2C. This driver │
│ should handle DS1307, DS1337, DS1338, DS1339, DS1340, ST M41T00, │
│ EPSON RX-8025 and probably other chips. In some cases the RTC │
│ must already have been initialized (by manufacturing or a │
│ bootloader). │
│ │
│ The first seven registers on these chips hold an RTC, and other │
│ registers may add features such as NVRAM, a trickle charger for │
│ the RTC/NVRAM backup power, and alarms. NVRAM is visible in │
│ sysfs, but other chip features may not be available. │
│ │
│ This driver can also be built as a module. If so, the module │
│ will be called rtc-ds1307. │
│ │
│ Symbol: RTC_DRV_DS1307 [=y] │
│ Type : tristate │
│ Prompt: Dallas/Maxim DS1307/37/38/39/40, ST M41T00, EPSON RX-8025 │
│ Location: │
│ -> Device Drivers │
│ -> Real Time Clock (RTC_CLASS [=y]) │
│ Defined at drivers/rtc/Kconfig:166 │
│ Depends on: RTC_CLASS [=y] && I2C [=y] │
│ │
│ │
│ │
├───────────────────────────────────────────────────────────(100%)──┤
│ < Exit > │
└───────────────────────────────────────────────────────────────────┘
2.爲ds1307建立dts節點
配置好ds1307的編譯開關後,接下來的工作就要在dts裏面添加ds1307的設備節點.查閱ds1307的資料,其配置的i2c地址爲0x68,因此,dts中可以添加如下內容:
i2c@0 {
compatible = "i2c-gpio";
gpios = <&gpio2 0 0 /* sda = wan_led*/
&gpio3 0 0 /* scl = wlan_led*/ >;
i2c-gpio,delay-us = <10>; /* ~20 kHz */
#address-cells = <1>;
#size-cells = <0>;
rtc@68 {
compatible = "dallas,ds1307";
reg = <0x68>;
};
};
完成這兩步後,就可以make出支持ds1307的固件.
系統啓動時,可以在TTL中看到如下內容:
[ 0.130000] i2c-gpio i2c.4: using pins 40 (SDA) and 72 (SCL)
...
[ 0.790000] rtc-ds1307 0-0068: rtc core: registered ds1307 as rtc0
[ 0.810000] rtc-ds1307 0-0068: 56 bytes nvram
...
[ 0.810000] rtc-ds1307 0-0068: setting system clock to 2015-01-26 22:31:15 UTC (1422311475)
說明RTC已經成功的整合到了mt7620的系統中.
ps:系統自帶的hwclock命令可以很方便的對RTC進行相應的操作.
後記
kernel 3.14.28中的i2c-ralink驅動已經能夠正常驅動7620的硬件i2c,只是在用i2cdetect的時候,需要加上-r參數,這樣才能正確probe到設備.