paul@62 | 1 | /* |
paul@62 | 2 | * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de> |
paul@62 | 3 | * economic rights: Technische Universit??t Dresden (Germany) |
paul@62 | 4 | * Copyright (C) 2017, 2018, 2019, 2020 Paul Boddie <paul@boddie.org.uk> |
paul@62 | 5 | * |
paul@62 | 6 | * This file is part of TUD:OS and distributed under the terms of the |
paul@62 | 7 | * GNU General Public License 2. |
paul@62 | 8 | * Please see the COPYING-GPL-2 file for details. |
paul@62 | 9 | */ |
paul@62 | 10 | /* |
paul@62 | 11 | * Access the HDMI I2C peripheral on the MIPS Creator CI20 board. |
paul@62 | 12 | */ |
paul@62 | 13 | |
paul@62 | 14 | #include <l4/devices/cpm-jz4780.h> |
paul@62 | 15 | #include <l4/devices/gpio-jz4780.h> |
paul@62 | 16 | #include <l4/devices/hdmi-jz4780.h> |
paul@64 | 17 | |
paul@64 | 18 | #include <l4/devices/lcd-jz4740.h> |
paul@64 | 19 | #include <l4/devices/lcd-jz4740-config.h> |
paul@64 | 20 | #include <l4/devices/lcd-jz4740-panel.h> |
paul@64 | 21 | |
paul@62 | 22 | #include <l4/devices/memory.h> |
paul@62 | 23 | #include <l4/io/io.h> |
paul@62 | 24 | #include <l4/libedid/edid.h> |
paul@62 | 25 | #include <l4/re/env.h> |
paul@62 | 26 | #include <l4/re/c/util/cap_alloc.h> |
paul@62 | 27 | #include <l4/sys/factory.h> |
paul@62 | 28 | #include <l4/sys/icu.h> |
paul@62 | 29 | #include <l4/sys/ipc.h> |
paul@62 | 30 | #include <l4/sys/irq.h> |
paul@62 | 31 | #include <l4/sys/rcv_endpoint.h> |
paul@62 | 32 | #include <l4/vbus/vbus.h> |
paul@62 | 33 | #include <stdio.h> |
paul@62 | 34 | #include <unistd.h> |
paul@62 | 35 | #include <stdint.h> |
paul@62 | 36 | #include <string.h> |
paul@62 | 37 | |
paul@62 | 38 | |
paul@62 | 39 | |
paul@62 | 40 | enum { |
paul@62 | 41 | DDCSCL = 24, /* via PORTF */ |
paul@62 | 42 | DDCSDA = 25, /* via PORTF */ |
paul@62 | 43 | }; |
paul@62 | 44 | |
paul@62 | 45 | |
paul@62 | 46 | |
paul@64 | 47 | /* Test panel. */ |
paul@64 | 48 | |
paul@64 | 49 | static struct Jz4740_lcd_panel panel = { |
paul@64 | 50 | .config = ( |
paul@64 | 51 | Jz4740_lcd_mode_tft_generic |
paul@64 | 52 | | Jz4740_lcd_pclock_negative |
paul@64 | 53 | | Jz4740_lcd_hsync_positive |
paul@64 | 54 | | Jz4740_lcd_vsync_positive |
paul@64 | 55 | | Jz4740_lcd_de_positive), |
paul@64 | 56 | |
paul@64 | 57 | .width = 1280, |
paul@64 | 58 | .height = 1024, |
paul@64 | 59 | .bpp = 32, |
paul@64 | 60 | .frame_rate = 60, |
paul@64 | 61 | .hsync = 112, |
paul@64 | 62 | .vsync = 3, |
paul@64 | 63 | .line_start = 248, // back porch (blanking - vsyn - offset) |
paul@64 | 64 | .line_end = 48, // front porch (sync offset) |
paul@64 | 65 | .frame_start = 36, // back porch (blanking - vsyn - offset) |
paul@64 | 66 | .frame_end = 3, // front porch (sync offset) |
paul@64 | 67 | }; |
paul@64 | 68 | |
paul@64 | 69 | |
paul@64 | 70 | |
paul@62 | 71 | /* Device and resource discovery. */ |
paul@62 | 72 | |
paul@62 | 73 | static long item_in_range(long start, long end, long index) |
paul@62 | 74 | { |
paul@62 | 75 | if (start < end) |
paul@62 | 76 | return start + index; |
paul@62 | 77 | else |
paul@62 | 78 | return start - index; |
paul@62 | 79 | } |
paul@62 | 80 | |
paul@62 | 81 | static void show_timings(uint8_t *buf) |
paul@62 | 82 | { |
paul@62 | 83 | unsigned int width, height; |
paul@62 | 84 | |
paul@62 | 85 | /* Attempt to decode EDID information. */ |
paul@62 | 86 | |
paul@62 | 87 | libedid_prefered_resolution(buf, &width, &height); |
paul@62 | 88 | printf("Preferred resolution: %d x %d\n", width, height); |
paul@62 | 89 | |
paul@62 | 90 | libedid_dump_standard_timings(buf); |
paul@62 | 91 | } |
paul@62 | 92 | |
paul@62 | 93 | int main(void) |
paul@62 | 94 | { |
paul@62 | 95 | long err; |
paul@62 | 96 | |
paul@62 | 97 | /* Buffer for EDID data. */ |
paul@62 | 98 | |
paul@62 | 99 | uint8_t edid[128]; |
paul@62 | 100 | unsigned int length = 0, i; |
paul@62 | 101 | |
paul@62 | 102 | /* Version details. */ |
paul@62 | 103 | |
paul@62 | 104 | uint8_t hdmi_major; |
paul@62 | 105 | uint16_t hdmi_minor; |
paul@62 | 106 | |
paul@62 | 107 | /* Peripheral memory. */ |
paul@62 | 108 | |
paul@62 | 109 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@62 | 110 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@62 | 111 | l4_addr_t hdmi_base = 0, hdmi_base_end = 0; |
paul@64 | 112 | l4_addr_t lcd_base = 0, lcd_base_end = 0; |
paul@62 | 113 | l4_addr_t port_f, port_f_end; |
paul@62 | 114 | |
paul@62 | 115 | /* Peripheral abstractions. */ |
paul@62 | 116 | |
paul@62 | 117 | void *cpm; |
paul@62 | 118 | void *gpio_port_f; |
paul@62 | 119 | void *hdmi; |
paul@64 | 120 | void *lcd; |
paul@62 | 121 | |
paul@62 | 122 | /* Access to IRQs. */ |
paul@62 | 123 | |
paul@62 | 124 | l4_uint32_t hdmi_irq_start = 0, hdmi_irq_end = 0; |
paul@64 | 125 | l4_uint32_t lcd_irq_start = 0, lcd_irq_end = 0; |
paul@64 | 126 | l4_cap_idx_t icucap, hdmi_irq, lcd_irq; |
paul@62 | 127 | |
paul@64 | 128 | hdmi_irq = l4re_util_cap_alloc(); |
paul@64 | 129 | lcd_irq = l4re_util_cap_alloc(); |
paul@62 | 130 | icucap = l4re_env_get_cap("icu"); |
paul@62 | 131 | |
paul@62 | 132 | if (l4_is_invalid_cap(icucap)) |
paul@62 | 133 | { |
paul@62 | 134 | printf("No 'icu' capability available in the virtual bus.\n"); |
paul@62 | 135 | return 1; |
paul@62 | 136 | } |
paul@62 | 137 | |
paul@64 | 138 | if (l4_is_invalid_cap(hdmi_irq) || l4_is_invalid_cap(lcd_irq)) |
paul@62 | 139 | { |
paul@62 | 140 | printf("Capabilities not available for interrupts.\n"); |
paul@62 | 141 | return 1; |
paul@62 | 142 | } |
paul@62 | 143 | |
paul@62 | 144 | /* Obtain resource details describing the interrupt for HDMI I2C. */ |
paul@62 | 145 | |
paul@62 | 146 | printf("Access IRQ...\n"); |
paul@62 | 147 | |
paul@62 | 148 | if (get_irq("jz4780-hdmi", &hdmi_irq_start, &hdmi_irq_end) < 0) |
paul@62 | 149 | return 1; |
paul@62 | 150 | |
paul@64 | 151 | printf("HDMI IRQ range at %d...%d.\n", hdmi_irq_start, hdmi_irq_end); |
paul@64 | 152 | |
paul@64 | 153 | if (get_irq("jz4780-lcd", &lcd_irq_start, &lcd_irq_end) < 0) |
paul@64 | 154 | return 1; |
paul@64 | 155 | |
paul@64 | 156 | printf("LCD IRQ range at %d...%d.\n", lcd_irq_start, lcd_irq_end); |
paul@62 | 157 | |
paul@62 | 158 | /* Create interrupt objects. */ |
paul@62 | 159 | |
paul@64 | 160 | err = l4_error(l4_factory_create_irq(l4re_global_env->factory, hdmi_irq)) || |
paul@64 | 161 | l4_error(l4_factory_create_irq(l4re_global_env->factory, lcd_irq)); |
paul@62 | 162 | |
paul@62 | 163 | if (err) |
paul@62 | 164 | { |
paul@62 | 165 | printf("Could not create IRQ object: %lx\n", err); |
paul@62 | 166 | return 1; |
paul@62 | 167 | } |
paul@62 | 168 | |
paul@62 | 169 | /* Bind interrupt objects to IRQ numbers. Here, the first HDMI interrupt is |
paul@62 | 170 | bound, this being the general HDMI interrupt. */ |
paul@62 | 171 | |
paul@62 | 172 | err = l4_error(l4_icu_bind(icucap, |
paul@62 | 173 | item_in_range(hdmi_irq_start, hdmi_irq_end, 0), |
paul@64 | 174 | hdmi_irq)) || |
paul@64 | 175 | l4_error(l4_icu_bind(icucap, |
paul@64 | 176 | item_in_range(lcd_irq_start, lcd_irq_end, 0), |
paul@64 | 177 | lcd_irq)); |
paul@62 | 178 | |
paul@62 | 179 | if (err) |
paul@62 | 180 | { |
paul@62 | 181 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@62 | 182 | return 1; |
paul@62 | 183 | } |
paul@62 | 184 | |
paul@62 | 185 | /* Attach ourselves to the interrupt handler. */ |
paul@62 | 186 | |
paul@64 | 187 | err = l4_error(l4_rcv_ep_bind_thread(hdmi_irq, l4re_env()->main_thread, 0)) || |
paul@64 | 188 | l4_error(l4_rcv_ep_bind_thread(lcd_irq, l4re_env()->main_thread, 0)); |
paul@62 | 189 | |
paul@62 | 190 | if (err) |
paul@62 | 191 | { |
paul@62 | 192 | printf("Could not attach to IRQs: %ld\n", err); |
paul@62 | 193 | return 1; |
paul@62 | 194 | } |
paul@62 | 195 | |
paul@62 | 196 | /* Obtain resource details describing I/O memory. */ |
paul@62 | 197 | |
paul@62 | 198 | printf("Access CPM...\n"); |
paul@62 | 199 | |
paul@62 | 200 | if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) |
paul@62 | 201 | return 1; |
paul@62 | 202 | |
paul@62 | 203 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@62 | 204 | |
paul@62 | 205 | printf("Access GPIO...\n"); |
paul@62 | 206 | |
paul@62 | 207 | if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) |
paul@62 | 208 | return 1; |
paul@62 | 209 | |
paul@62 | 210 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@62 | 211 | |
paul@62 | 212 | printf("Access HDMI...\n"); |
paul@62 | 213 | |
paul@62 | 214 | if (get_memory("jz4780-hdmi", &hdmi_base, &hdmi_base_end) < 0) |
paul@62 | 215 | return 1; |
paul@62 | 216 | |
paul@62 | 217 | printf("HDMI at 0x%lx...0x%lx.\n", hdmi_base, hdmi_base_end); |
paul@62 | 218 | |
paul@64 | 219 | printf("Access LCD...\n"); |
paul@64 | 220 | |
paul@64 | 221 | if (get_memory("jz4780-lcd", &lcd_base, &lcd_base_end) < 0) |
paul@64 | 222 | return 1; |
paul@64 | 223 | |
paul@64 | 224 | printf("LCD at 0x%lx...0x%lx.\n", lcd_base, lcd_base_end); |
paul@64 | 225 | |
paul@62 | 226 | /* Obtain CPM object. */ |
paul@62 | 227 | |
paul@62 | 228 | cpm = jz4780_cpm_init(cpm_base); |
paul@62 | 229 | |
paul@62 | 230 | printf("VPLL frequency: %d\n", jz4780_cpm_get_vpll_frequency(cpm)); |
paul@62 | 231 | printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); |
paul@62 | 232 | printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); |
paul@62 | 233 | |
paul@62 | 234 | jz4780_cpm_stop_hdmi(cpm); |
paul@62 | 235 | jz4780_cpm_set_hdmi_frequency(cpm, 27000000); |
paul@62 | 236 | |
paul@62 | 237 | printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); |
paul@62 | 238 | printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); |
paul@62 | 239 | |
paul@62 | 240 | jz4780_cpm_start_hdmi(cpm); |
paul@62 | 241 | |
paul@62 | 242 | /* Configure pins. */ |
paul@62 | 243 | |
paul@62 | 244 | port_f = gpio_base + 0x500; |
paul@62 | 245 | port_f_end = port_f + 0x100; |
paul@62 | 246 | |
paul@62 | 247 | printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); |
paul@62 | 248 | |
paul@62 | 249 | gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0x7fa7f00f, 0x00580ff0); |
paul@62 | 250 | |
paul@62 | 251 | printf("Set up GPIO pins...\n"); |
paul@62 | 252 | |
paul@62 | 253 | jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 0); |
paul@62 | 254 | jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 0); |
paul@62 | 255 | |
paul@62 | 256 | /* Obtain HDMI reference. */ |
paul@62 | 257 | |
paul@62 | 258 | printf("Set up HDMI...\n"); |
paul@62 | 259 | |
paul@64 | 260 | hdmi = jz4780_hdmi_init(hdmi_base, hdmi_base_end, hdmi_irq); |
paul@62 | 261 | |
paul@62 | 262 | printf("Read version...\n"); |
paul@62 | 263 | |
paul@62 | 264 | jz4780_hdmi_get_version(hdmi, &hdmi_major, &hdmi_minor); |
paul@62 | 265 | |
paul@62 | 266 | printf("HDMI version is %x.%03x\n", hdmi_major, hdmi_minor); |
paul@62 | 267 | |
paul@62 | 268 | printf("Read EDID...\n"); |
paul@62 | 269 | |
paul@62 | 270 | jz4780_hdmi_i2c_set_address(hdmi, 0x50); |
paul@62 | 271 | length = jz4780_hdmi_i2c_read(hdmi, edid, 128); |
paul@62 | 272 | |
paul@62 | 273 | if (length) |
paul@62 | 274 | { |
paul@62 | 275 | for (i = 0; i < length; i++) |
paul@62 | 276 | printf("%02x%c", edid[i], ((i % 16) != 15) ? ' ' : '\n'); |
paul@62 | 277 | } |
paul@62 | 278 | |
paul@62 | 279 | show_timings(edid); |
paul@62 | 280 | |
paul@64 | 281 | /* Obtain LCD reference. */ |
paul@64 | 282 | |
paul@64 | 283 | printf("Set up LCD...\n"); |
paul@64 | 284 | |
paul@64 | 285 | lcd = jz4740_lcd_init(lcd_base, &panel); |
paul@64 | 286 | |
paul@64 | 287 | /* Test initialisation with a frequency appropriate for the test panel. */ |
paul@64 | 288 | |
paul@64 | 289 | printf("LCD divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm)); |
paul@64 | 290 | printf("LCD frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm)); |
paul@64 | 291 | printf("Desired frequency: %d\n", jz4740_lcd_get_pixel_clock(lcd)); |
paul@62 | 292 | |
paul@64 | 293 | jz4780_cpm_stop_lcd(cpm); |
paul@64 | 294 | |
paul@64 | 295 | jz4780_cpm_set_lcd_frequencies(cpm, jz4740_lcd_get_pixel_clock(lcd), 3); |
paul@64 | 296 | |
paul@64 | 297 | printf("LCD divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm)); |
paul@64 | 298 | printf("LCD frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm)); |
paul@64 | 299 | |
paul@64 | 300 | jz4780_cpm_start_lcd(cpm); |
paul@64 | 301 | |
paul@64 | 302 | /* Detach from the interrupts. */ |
paul@64 | 303 | |
paul@64 | 304 | err = l4_error(l4_irq_detach(hdmi_irq)) || |
paul@64 | 305 | l4_error(l4_irq_detach(lcd_irq)); |
paul@62 | 306 | |
paul@62 | 307 | if (err) |
paul@62 | 308 | printf("Error detaching from IRQ: %ld\n", err); |
paul@62 | 309 | |
paul@62 | 310 | printf("Done.\n"); |
paul@62 | 311 | |
paul@62 | 312 | return 0; |
paul@62 | 313 | } |