paul@16 | 1 | /* |
paul@16 | 2 | * Ben NanoNote board late initialisation, based on uboot-xburst and xburst-tools. |
paul@16 | 3 | * |
paul@33 | 4 | * Copyright (C) 2000-2009 Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
paul@33 | 5 | * Copyright (C) 2006 Ingenic Semiconductor, <jlwei@ingenic.cn> |
paul@16 | 6 | * Copyright (C) Xiangfu Liu <xiangfu.z@gmail.com> |
paul@217 | 7 | * Copyright (C) 2015, 2016, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@16 | 8 | * |
paul@63 | 9 | * This program is free software: you can redistribute it and/or modify |
paul@63 | 10 | * it under the terms of the GNU General Public License as published by |
paul@63 | 11 | * the Free Software Foundation, either version 3 of the License, or |
paul@63 | 12 | * (at your option) any later version. |
paul@16 | 13 | * |
paul@63 | 14 | * This program is distributed in the hope that it will be useful, |
paul@63 | 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@63 | 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@63 | 17 | * GNU General Public License for more details. |
paul@16 | 18 | * |
paul@63 | 19 | * You should have received a copy of the GNU General Public License |
paul@63 | 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@16 | 21 | */ |
paul@16 | 22 | |
paul@33 | 23 | #include "board.h" |
paul@16 | 24 | #include "nanonote.h" |
paul@230 | 25 | #include "cpm.h" |
paul@16 | 26 | |
paul@16 | 27 | /* Later initialisation functions. */ |
paul@16 | 28 | |
paul@195 | 29 | void gpio_init2() |
paul@16 | 30 | { |
paul@204 | 31 | /* Initialise LCD pins. */ |
paul@204 | 32 | |
paul@16 | 33 | __gpio_as_slcd_8bit(); |
paul@16 | 34 | |
paul@204 | 35 | /* Initialise MSC pins. */ |
paul@204 | 36 | |
paul@16 | 37 | __gpio_as_msc(); |
paul@16 | 38 | |
paul@204 | 39 | /* Initialise other pins. */ |
paul@204 | 40 | |
paul@204 | 41 | /* Enable TP4, TP5 as UART0. */ |
paul@204 | 42 | |
paul@16 | 43 | __gpio_jtag_to_uart0(); |
paul@16 | 44 | |
paul@108 | 45 | __gpio_as_input(GPIO_POWER); |
paul@108 | 46 | __gpio_enable_pull(GPIO_POWER); |
paul@108 | 47 | |
paul@16 | 48 | __gpio_as_output(GPIO_AUDIO_POP); |
paul@16 | 49 | __gpio_set_pin(GPIO_AUDIO_POP); |
paul@16 | 50 | |
paul@16 | 51 | __gpio_as_output(GPIO_LCD_CS); |
paul@16 | 52 | __gpio_clear_pin(GPIO_LCD_CS); |
paul@16 | 53 | |
paul@16 | 54 | __gpio_as_output(GPIO_AMP_EN); |
paul@16 | 55 | __gpio_clear_pin(GPIO_AMP_EN); |
paul@16 | 56 | |
paul@16 | 57 | __gpio_as_output(GPIO_SDPW_EN); |
paul@16 | 58 | __gpio_disable_pull(GPIO_SDPW_EN); |
paul@16 | 59 | __gpio_clear_pin(GPIO_SDPW_EN); |
paul@16 | 60 | |
paul@16 | 61 | __gpio_as_input(GPIO_SD_DETECT); |
paul@16 | 62 | __gpio_disable_pull(GPIO_SD_DETECT); |
paul@16 | 63 | |
paul@16 | 64 | __gpio_as_input(GPIO_USB_DETECT); |
paul@16 | 65 | __gpio_enable_pull(GPIO_USB_DETECT); |
paul@212 | 66 | |
paul@212 | 67 | /* Employ a PWM channel for the buzzer. */ |
paul@212 | 68 | |
paul@212 | 69 | __gpio_as_pwm4(); |
paul@16 | 70 | } |
paul@16 | 71 | |
paul@195 | 72 | void rtc_init() |
paul@16 | 73 | { |
paul@16 | 74 | while ( !__rtc_write_ready()); |
paul@16 | 75 | __rtc_enable_alarm(); /* enable alarm */ |
paul@16 | 76 | |
paul@16 | 77 | while ( !__rtc_write_ready()); |
paul@16 | 78 | REG_RTC_RGR = 0x00007fff; /* type value */ |
paul@16 | 79 | |
paul@16 | 80 | while ( !__rtc_write_ready()); |
paul@16 | 81 | REG_RTC_HWFCR = 0x0000ffe0; /* Power on delay 2s */ |
paul@16 | 82 | |
paul@16 | 83 | while ( !__rtc_write_ready()); |
paul@16 | 84 | REG_RTC_HRCR = 0x00000fe0; /* reset delay 125ms */ |
paul@16 | 85 | } |
paul@16 | 86 | |
paul@16 | 87 | /* Timer routines. */ |
paul@16 | 88 | |
paul@33 | 89 | unsigned long timestamp; |
paul@33 | 90 | unsigned long lastdec; |
paul@16 | 91 | |
paul@16 | 92 | /* |
paul@68 | 93 | * Timer without interrupts. |
paul@16 | 94 | */ |
paul@16 | 95 | |
paul@195 | 96 | void timer_init() |
paul@16 | 97 | { |
paul@68 | 98 | __tcu_disable_pwm_output(TIMER_CHAN); |
paul@50 | 99 | __tcu_select_extalclk(TIMER_CHAN); |
paul@50 | 100 | __tcu_select_clk_div256(TIMER_CHAN); |
paul@50 | 101 | __tcu_set_count(TIMER_CHAN, 0); |
paul@50 | 102 | __tcu_set_half_data(TIMER_CHAN, 0); |
paul@50 | 103 | __tcu_set_full_data(TIMER_CHAN, TIMER_FDATA); |
paul@16 | 104 | |
paul@50 | 105 | __tcu_mask_half_match_irq(TIMER_CHAN); |
paul@50 | 106 | __tcu_mask_full_match_irq(TIMER_CHAN); |
paul@50 | 107 | __tcu_start_timer_clock(TIMER_CHAN); |
paul@50 | 108 | __tcu_start_counter(TIMER_CHAN); |
paul@16 | 109 | |
paul@230 | 110 | jz4740_cpm_start_clock((void *) CPM_BASE); |
paul@68 | 111 | |
paul@16 | 112 | lastdec = 0; |
paul@16 | 113 | timestamp = 0; |
paul@16 | 114 | } |
paul@54 | 115 | |
paul@68 | 116 | /* Timer interrupt activation. */ |
paul@68 | 117 | |
paul@195 | 118 | void timer_init_irq() |
paul@68 | 119 | { |
paul@68 | 120 | __tcu_unmask_full_match_irq(TIMER_CHAN); |
paul@68 | 121 | __tcu_clear_full_match_flag(TIMER_CHAN); |
paul@68 | 122 | __intc_unmask_irq(TIMER_CHAN_IRQ); |
paul@68 | 123 | } |
paul@68 | 124 | |
paul@195 | 125 | void timer_clear() |
paul@153 | 126 | { |
paul@153 | 127 | __intc_ack_irq(TIMER_CHAN_IRQ); |
paul@153 | 128 | __tcu_clear_full_match_flag(TIMER_CHAN); |
paul@153 | 129 | } |
paul@153 | 130 | |
paul@196 | 131 | /* GPIO interrupt activation. */ |
paul@196 | 132 | |
paul@196 | 133 | void gpio_init_irq() |
paul@196 | 134 | { |
paul@196 | 135 | __gpio_as_irq_low_level(GPIO_POWER); |
paul@196 | 136 | __intc_unmask_irq(GPIO_IRQ); |
paul@196 | 137 | } |
paul@196 | 138 | |
paul@217 | 139 | int gpio_have_irq(uint8_t gpio) |
paul@196 | 140 | { |
paul@196 | 141 | return (REG_GPIO_PXFLG(gpio / 32) & (1 << (gpio % 32))); |
paul@196 | 142 | } |
paul@196 | 143 | |
paul@68 | 144 | /* Board startup detection. */ |
paul@68 | 145 | |
paul@195 | 146 | int is_started() |
paul@54 | 147 | { |
paul@230 | 148 | return jz4740_cpm_have_clock((void *) CPM_BASE); |
paul@54 | 149 | } |