From 1d8949d2192a94dfe58de656703dab240beab7b0 Mon Sep 17 00:00:00 2001 From: zwhfly Date: Wed, 19 Mar 2014 19:27:30 +0800 Subject: [PATCH] Fix serial baud rate calculation This commit fixes this issue: https://github.com/pepe2k/u-boot_mod/issues/21. --- u-boot/cpu/mips/ar7240/ar7240_serial.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/u-boot/cpu/mips/ar7240/ar7240_serial.c b/u-boot/cpu/mips/ar7240/ar7240_serial.c index 0d68f9c..887094c 100755 --- a/u-boot/cpu/mips/ar7240/ar7240_serial.c +++ b/u-boot/cpu/mips/ar7240/ar7240_serial.c @@ -29,6 +29,19 @@ #define UART16550_READ(y) ar7240_reg_rd((AR7240_UART_BASE+y)) #define UART16550_WRITE(x, z) ar7240_reg_wr((AR7240_UART_BASE+x), z) +/* + * This is taken from [Linux]/include/linux/kernel.h + * Keep the name unchanged here + * When this project decides to include that kernel.h some time, + * this would be found "automatically" and be removed hopefully + */ +#define DIV_ROUND_CLOSEST(x, divisor)( \ +{ \ + typeof(divisor) __divisor = divisor; \ + (((x) + ((__divisor) / 2)) / (__divisor)); \ +} \ +) + /* * Get CPU, RAM and AHB clocks * Based on: Linux/arch/mips/ath79/clock.c @@ -161,16 +174,16 @@ int serial_init(void){ val = ar7240_reg_rd(WASP_BOOTSTRAP_REG); if((val & WASP_REF_CLK_25) == 0){ - div = (25 * 1000000) / (16 * CONFIG_BAUDRATE); + div = DIV_ROUND_CLOSEST((25 * 1000000), (16 * CONFIG_BAUDRATE)); } else { - div = (40 * 1000000) / (16 * CONFIG_BAUDRATE); + div = DIV_ROUND_CLOSEST((40 * 1000000), (16 * CONFIG_BAUDRATE)); } #else u32 ahb_freq, ddr_freq, cpu_freq; ar7240_sys_frequency(&cpu_freq, &ddr_freq, &ahb_freq); - div = ahb_freq/(16 * CONFIG_BAUDRATE); + div = DIV_ROUND_CLOSEST(ahb_freq, (16 * CONFIG_BAUDRATE)); MY_WRITE(0xb8040000, 0xcff); MY_WRITE(0xb8040008, 0x3b); -- 2.25.1