1 /* 2 * Ben NanoNote board late initialisation, based on uboot-xburst and xburst-tools. 3 * 4 * Copyright (C) 2000-2009 Wolfgang Denk, DENX Software Engineering, <wd@denx.de> 5 * Copyright (C) 2006 Ingenic Semiconductor, <jlwei@ingenic.cn> 6 * Copyright (C) Xiangfu Liu <xiangfu.z@gmail.com> 7 * Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> 8 * 9 * This program is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation, either version 3 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program. If not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 #include "board.h" 24 #include "nanonote.h" 25 #include "cpm.h" 26 #include "gpio.h" 27 28 /* Later initialisation functions. */ 29 30 void gpio_init2() 31 { 32 void *gpio_port_base; 33 34 /* 35 Initialise LCD pins. 36 gpio_as_slcd_8bit: LCD_D0~LCD_D7, SLCD_CLK, SLCD_RS, SLCD_CS, LCD_DE 37 */ 38 39 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_LCD); 40 jz4740_gpio_ctrl_set(gpio_port_base, GPIO_PXFUNS, GPIO_SLCD8_MASK); 41 jz4740_gpio_ctrl_set(gpio_port_base, GPIO_PXSELC, GPIO_SLCD8_MASK); 42 43 /* 44 Initialise MSC pins. 45 gpio_as_msc: MSC_CMD, MSC_CLK, MSC_D0 ~ MSC_D3 46 */ 47 48 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_MSC); 49 jz4740_gpio_ctrl_set(gpio_port_base, GPIO_PXFUNS, GPIO_MSC_MASK); 50 jz4740_gpio_ctrl_set(gpio_port_base, GPIO_PXSELC, GPIO_MSC_MASK); 51 jz4740_gpio_ctrl_set(gpio_port_base, GPIO_PXPES, GPIO_MSC_MASK); 52 53 /* Initialise other pins. */ 54 55 /* Enable TP4, TP5 as UART0 (gpio_jtag_to_uart0). */ 56 57 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_JTAG); 58 jz4740_gpio_ctrl_set(gpio_port_base, GPIO_PXSELS, 1 << GPIO_JTAG_UART_EN); 59 60 /* Enable pull-up on the power switch. */ 61 62 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_POWER); 63 jz4740_gpio_as_input(gpio_port_base, GPIO_POWER); 64 jz4740_gpio_enable_pull(gpio_port_base, GPIO_POWER); 65 66 /* Enable audio output. */ 67 68 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_AUDIO); 69 jz4740_gpio_as_output(gpio_port_base, GPIO_AUDIO_EN); 70 jz4740_gpio_set_pin(gpio_port_base, GPIO_AUDIO_EN); 71 72 /* Enable LCD signalling. */ 73 74 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_LCD); 75 jz4740_gpio_as_output(gpio_port_base, GPIO_LCD_CS); 76 jz4740_gpio_clear_pin(gpio_port_base, GPIO_LCD_CS); 77 78 /* Enable speaker. */ 79 80 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_AMP); 81 jz4740_gpio_as_output(gpio_port_base, GPIO_AMP_EN); 82 jz4740_gpio_clear_pin(gpio_port_base, GPIO_AMP_EN); 83 84 /* Disable pull-up on SD pins. */ 85 86 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_SD); 87 jz4740_gpio_as_output(gpio_port_base, GPIO_SDPW_EN); 88 jz4740_gpio_disable_pull(gpio_port_base, GPIO_SDPW_EN); 89 jz4740_gpio_clear_pin(gpio_port_base, GPIO_SDPW_EN); 90 jz4740_gpio_as_input(gpio_port_base, GPIO_SD_DETECT); 91 jz4740_gpio_disable_pull(gpio_port_base, GPIO_SD_DETECT); 92 93 /* Enable pull-up on USB detect pin. */ 94 95 gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_USB); 96 jz4740_gpio_as_input(gpio_port_base, GPIO_USB_DETECT); 97 jz4740_gpio_enable_pull(gpio_port_base, GPIO_USB_DETECT); 98 99 /* Employ a PWM channel for the buzzer. */ 100 101 jz4740_gpio_as_pwm((void *) GPIO_BASE, 4); 102 } 103 104 void rtc_init() 105 { 106 while ( !__rtc_write_ready()); 107 __rtc_enable_alarm(); /* enable alarm */ 108 109 while ( !__rtc_write_ready()); 110 REG_RTC_RGR = 0x00007fff; /* type value */ 111 112 while ( !__rtc_write_ready()); 113 REG_RTC_HWFCR = 0x0000ffe0; /* Power on delay 2s */ 114 115 while ( !__rtc_write_ready()); 116 REG_RTC_HRCR = 0x00000fe0; /* reset delay 125ms */ 117 } 118 119 /* Timer routines. */ 120 121 unsigned long timestamp; 122 unsigned long lastdec; 123 124 /* 125 * Timer without interrupts. 126 */ 127 128 void timer_init() 129 { 130 __tcu_disable_pwm_output(TIMER_CHAN); 131 __tcu_select_extalclk(TIMER_CHAN); 132 __tcu_select_clk_div256(TIMER_CHAN); 133 __tcu_set_count(TIMER_CHAN, 0); 134 __tcu_set_half_data(TIMER_CHAN, 0); 135 __tcu_set_full_data(TIMER_CHAN, TIMER_FDATA); 136 137 __tcu_mask_half_match_irq(TIMER_CHAN); 138 __tcu_mask_full_match_irq(TIMER_CHAN); 139 __tcu_start_timer_clock(TIMER_CHAN); 140 __tcu_start_counter(TIMER_CHAN); 141 142 jz4740_cpm_start_clock((void *) CPM_BASE); 143 144 lastdec = 0; 145 timestamp = 0; 146 } 147 148 /* Timer interrupt activation. */ 149 150 void timer_init_irq() 151 { 152 __tcu_unmask_full_match_irq(TIMER_CHAN); 153 __tcu_clear_full_match_flag(TIMER_CHAN); 154 __intc_unmask_irq(TIMER_CHAN_IRQ); 155 } 156 157 void timer_clear() 158 { 159 __intc_ack_irq(TIMER_CHAN_IRQ); 160 __tcu_clear_full_match_flag(TIMER_CHAN); 161 } 162 163 /* GPIO interrupt activation. */ 164 165 void gpio_init_irq() 166 { 167 void *gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, GPIO_PORT_POWER); 168 jz4740_gpio_as_irq(gpio_port_base, GPIO_POWER, gpio_irq_level_low); 169 __intc_unmask_irq(GPIO_IRQ3); 170 } 171 172 int gpio_have_irq(uint8_t gpio) 173 { 174 void *gpio_port_base = jz4740_gpio_get_port((void *) GPIO_BASE, gpio / 32); 175 return jz4740_gpio_have_irq(gpio_port_base, gpio % 32); 176 } 177 178 /* Board startup detection. */ 179 180 int is_started() 181 { 182 return jz4740_cpm_have_clock((void *) CPM_BASE); 183 }