serial.c (1526B)
1 /* serial.c 2 * 3 * Copyright 2011 Brian Swetland <swetland@frotz.net> 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <fw/types.h> 19 #include <fw/lib.h> 20 #include <fw/io.h> 21 22 #include <arch/hardware.h> 23 24 static unsigned usart_calc_brr(unsigned pclk, unsigned baud) 25 { 26 unsigned idiv, fdiv, tmp; 27 idiv = ((25 * pclk) / (4 * baud)); 28 tmp = (idiv / 100) << 4; 29 fdiv = idiv - (100 * (tmp >> 4)); 30 tmp |= ((((fdiv * 16) + 50) / 100)) & 0x0F; 31 return tmp; 32 } 33 34 void serial_init(unsigned sysclk, unsigned baud) { 35 writel(0, USART1_BASE + USART_CR1); 36 writel(0, USART1_BASE + USART_CR2); 37 writel(0, USART1_BASE + USART_CR3); 38 writel(1, USART1_BASE + USART_GTPR); /* divide pclk by 1 */ 39 writel(usart_calc_brr(sysclk, baud), USART1_BASE + USART_BRR); 40 writel(USART_CR1_ENABLE | USART_CR1_PARITY | USART_CR1_9BIT | 41 USART_CR1_TX_ENABLE | USART_CR1_RX_ENABLE, 42 USART1_BASE + USART_CR1); 43 } 44 45 void serial_putc(unsigned c) { 46 while (!(readl(USART1_BASE + USART_SR) & USART_SR_TXE)) ; 47 writel(c, USART1_BASE + USART_DR); 48 } 49