Fix serial baud rate calculation 22/head
authorzwhfly <zwhfly@163.com>
Wed, 19 Mar 2014 11:27:30 +0000 (19:27 +0800)
committerzwhfly <zwhfly@163.com>
Wed, 19 Mar 2014 11:27:30 +0000 (19:27 +0800)
This commit fixes this issue: https://github.com/pepe2k/u-boot_mod/issues/21.

u-boot/cpu/mips/ar7240/ar7240_serial.c

index 0d68f9c2a24491b88948dd20b600876e1828a1de..887094c3c625c7dbfffc6a1d003322a6487ed8b0 100755 (executable)
 #define         UART16550_READ(y)              ar7240_reg_rd((AR7240_UART_BASE+y))\r
 #define         UART16550_WRITE(x, z)  ar7240_reg_wr((AR7240_UART_BASE+x), z)\r
 \r
+/*\r
+ * This is taken from [Linux]/include/linux/kernel.h\r
+ * Keep the name unchanged here\r
+ * When this project decides to include that kernel.h some time,\r
+ * this would be found "automatically" and be removed hopefully\r
+ */\r
+#define DIV_ROUND_CLOSEST(x, divisor)(                 \\r
+{                                                      \\r
+       typeof(divisor) __divisor = divisor;            \\r
+       (((x) + ((__divisor) / 2)) / (__divisor));      \\r
+}                                                      \\r
+)\r
+\r
 /*\r
  * Get CPU, RAM and AHB clocks\r
  * Based on: Linux/arch/mips/ath79/clock.c\r
@@ -161,16 +174,16 @@ int serial_init(void){
        val = ar7240_reg_rd(WASP_BOOTSTRAP_REG);\r
 \r
        if((val & WASP_REF_CLK_25) == 0){\r
-               div = (25 * 1000000) / (16 * CONFIG_BAUDRATE);\r
+               div = DIV_ROUND_CLOSEST((25 * 1000000), (16 * CONFIG_BAUDRATE));\r
        } else {\r
-               div = (40 * 1000000) / (16 * CONFIG_BAUDRATE);\r
+               div = DIV_ROUND_CLOSEST((40 * 1000000), (16 * CONFIG_BAUDRATE));\r
        }\r
 #else\r
        u32 ahb_freq, ddr_freq, cpu_freq;\r
 \r
        ar7240_sys_frequency(&cpu_freq, &ddr_freq, &ahb_freq);\r
 \r
-       div = ahb_freq/(16 * CONFIG_BAUDRATE);\r
+       div = DIV_ROUND_CLOSEST(ahb_freq, (16 * CONFIG_BAUDRATE));\r
 \r
        MY_WRITE(0xb8040000, 0xcff);\r
        MY_WRITE(0xb8040008, 0x3b);\r