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@62 | 17 | #include <l4/devices/memory.h> |
paul@62 | 18 | #include <l4/io/io.h> |
paul@62 | 19 | #include <l4/libedid/edid.h> |
paul@62 | 20 | #include <l4/re/env.h> |
paul@62 | 21 | #include <l4/re/c/util/cap_alloc.h> |
paul@62 | 22 | #include <l4/sys/factory.h> |
paul@62 | 23 | #include <l4/sys/icu.h> |
paul@62 | 24 | #include <l4/sys/ipc.h> |
paul@62 | 25 | #include <l4/sys/irq.h> |
paul@62 | 26 | #include <l4/sys/rcv_endpoint.h> |
paul@62 | 27 | #include <l4/vbus/vbus.h> |
paul@62 | 28 | #include <stdio.h> |
paul@62 | 29 | #include <unistd.h> |
paul@62 | 30 | #include <stdint.h> |
paul@62 | 31 | #include <string.h> |
paul@62 | 32 | |
paul@62 | 33 | |
paul@62 | 34 | |
paul@62 | 35 | enum { |
paul@62 | 36 | DDCSCL = 24, /* via PORTF */ |
paul@62 | 37 | DDCSDA = 25, /* via PORTF */ |
paul@62 | 38 | }; |
paul@62 | 39 | |
paul@62 | 40 | |
paul@62 | 41 | |
paul@62 | 42 | /* Device and resource discovery. */ |
paul@62 | 43 | |
paul@62 | 44 | static long item_in_range(long start, long end, long index) |
paul@62 | 45 | { |
paul@62 | 46 | if (start < end) |
paul@62 | 47 | return start + index; |
paul@62 | 48 | else |
paul@62 | 49 | return start - index; |
paul@62 | 50 | } |
paul@62 | 51 | |
paul@62 | 52 | static void show_timings(uint8_t *buf) |
paul@62 | 53 | { |
paul@62 | 54 | unsigned int width, height; |
paul@62 | 55 | |
paul@62 | 56 | /* Attempt to decode EDID information. */ |
paul@62 | 57 | |
paul@62 | 58 | libedid_prefered_resolution(buf, &width, &height); |
paul@62 | 59 | printf("Preferred resolution: %d x %d\n", width, height); |
paul@62 | 60 | |
paul@62 | 61 | libedid_dump_standard_timings(buf); |
paul@62 | 62 | } |
paul@62 | 63 | |
paul@62 | 64 | int main(void) |
paul@62 | 65 | { |
paul@62 | 66 | long err; |
paul@62 | 67 | |
paul@62 | 68 | /* Buffer for EDID data. */ |
paul@62 | 69 | |
paul@62 | 70 | uint8_t edid[128]; |
paul@62 | 71 | unsigned int length = 0, i; |
paul@62 | 72 | |
paul@62 | 73 | /* Version details. */ |
paul@62 | 74 | |
paul@62 | 75 | uint8_t hdmi_major; |
paul@62 | 76 | uint16_t hdmi_minor; |
paul@62 | 77 | |
paul@62 | 78 | /* Peripheral memory. */ |
paul@62 | 79 | |
paul@62 | 80 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@62 | 81 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@62 | 82 | l4_addr_t hdmi_base = 0, hdmi_base_end = 0; |
paul@62 | 83 | l4_addr_t port_f, port_f_end; |
paul@62 | 84 | |
paul@62 | 85 | /* Peripheral abstractions. */ |
paul@62 | 86 | |
paul@62 | 87 | void *cpm; |
paul@62 | 88 | void *gpio_port_f; |
paul@62 | 89 | void *hdmi; |
paul@62 | 90 | |
paul@62 | 91 | /* Access to IRQs. */ |
paul@62 | 92 | |
paul@62 | 93 | l4_uint32_t hdmi_irq_start = 0, hdmi_irq_end = 0; |
paul@62 | 94 | l4_cap_idx_t icucap, irqcap; |
paul@62 | 95 | |
paul@62 | 96 | irqcap = l4re_util_cap_alloc(); |
paul@62 | 97 | icucap = l4re_env_get_cap("icu"); |
paul@62 | 98 | |
paul@62 | 99 | if (l4_is_invalid_cap(icucap)) |
paul@62 | 100 | { |
paul@62 | 101 | printf("No 'icu' capability available in the virtual bus.\n"); |
paul@62 | 102 | return 1; |
paul@62 | 103 | } |
paul@62 | 104 | |
paul@62 | 105 | if (l4_is_invalid_cap(irqcap)) |
paul@62 | 106 | { |
paul@62 | 107 | printf("Capabilities not available for interrupts.\n"); |
paul@62 | 108 | return 1; |
paul@62 | 109 | } |
paul@62 | 110 | |
paul@62 | 111 | /* Obtain resource details describing the interrupt for HDMI I2C. */ |
paul@62 | 112 | |
paul@62 | 113 | printf("Access IRQ...\n"); |
paul@62 | 114 | |
paul@62 | 115 | if (get_irq("jz4780-hdmi", &hdmi_irq_start, &hdmi_irq_end) < 0) |
paul@62 | 116 | return 1; |
paul@62 | 117 | |
paul@62 | 118 | printf("IRQ range at %d...%d.\n", hdmi_irq_start, hdmi_irq_end); |
paul@62 | 119 | |
paul@62 | 120 | /* Create interrupt objects. */ |
paul@62 | 121 | |
paul@62 | 122 | err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irqcap)); |
paul@62 | 123 | |
paul@62 | 124 | if (err) |
paul@62 | 125 | { |
paul@62 | 126 | printf("Could not create IRQ object: %lx\n", err); |
paul@62 | 127 | return 1; |
paul@62 | 128 | } |
paul@62 | 129 | |
paul@62 | 130 | /* Bind interrupt objects to IRQ numbers. Here, the first HDMI interrupt is |
paul@62 | 131 | bound, this being the general HDMI interrupt. */ |
paul@62 | 132 | |
paul@62 | 133 | err = l4_error(l4_icu_bind(icucap, |
paul@62 | 134 | item_in_range(hdmi_irq_start, hdmi_irq_end, 0), |
paul@62 | 135 | irqcap)); |
paul@62 | 136 | |
paul@62 | 137 | if (err) |
paul@62 | 138 | { |
paul@62 | 139 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@62 | 140 | return 1; |
paul@62 | 141 | } |
paul@62 | 142 | |
paul@62 | 143 | /* Attach ourselves to the interrupt handler. */ |
paul@62 | 144 | |
paul@62 | 145 | err = l4_error(l4_rcv_ep_bind_thread(irqcap, l4re_env()->main_thread, 0)); |
paul@62 | 146 | |
paul@62 | 147 | if (err) |
paul@62 | 148 | { |
paul@62 | 149 | printf("Could not attach to IRQs: %ld\n", err); |
paul@62 | 150 | return 1; |
paul@62 | 151 | } |
paul@62 | 152 | |
paul@62 | 153 | /* Obtain resource details describing I/O memory. */ |
paul@62 | 154 | |
paul@62 | 155 | printf("Access CPM...\n"); |
paul@62 | 156 | |
paul@62 | 157 | if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) |
paul@62 | 158 | return 1; |
paul@62 | 159 | |
paul@62 | 160 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@62 | 161 | |
paul@62 | 162 | printf("Access GPIO...\n"); |
paul@62 | 163 | |
paul@62 | 164 | if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) |
paul@62 | 165 | return 1; |
paul@62 | 166 | |
paul@62 | 167 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@62 | 168 | |
paul@62 | 169 | printf("Access HDMI...\n"); |
paul@62 | 170 | |
paul@62 | 171 | if (get_memory("jz4780-hdmi", &hdmi_base, &hdmi_base_end) < 0) |
paul@62 | 172 | return 1; |
paul@62 | 173 | |
paul@62 | 174 | printf("HDMI at 0x%lx...0x%lx.\n", hdmi_base, hdmi_base_end); |
paul@62 | 175 | |
paul@62 | 176 | /* Obtain CPM object. */ |
paul@62 | 177 | |
paul@62 | 178 | cpm = jz4780_cpm_init(cpm_base); |
paul@62 | 179 | |
paul@62 | 180 | printf("VPLL frequency: %d\n", jz4780_cpm_get_vpll_frequency(cpm)); |
paul@62 | 181 | printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); |
paul@62 | 182 | printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); |
paul@62 | 183 | |
paul@62 | 184 | jz4780_cpm_stop_hdmi(cpm); |
paul@62 | 185 | jz4780_cpm_set_hdmi_frequency(cpm, 27000000); |
paul@62 | 186 | |
paul@62 | 187 | printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); |
paul@62 | 188 | printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); |
paul@62 | 189 | |
paul@62 | 190 | jz4780_cpm_start_hdmi(cpm); |
paul@62 | 191 | |
paul@62 | 192 | /* Configure pins. */ |
paul@62 | 193 | |
paul@62 | 194 | port_f = gpio_base + 0x500; |
paul@62 | 195 | port_f_end = port_f + 0x100; |
paul@62 | 196 | |
paul@62 | 197 | printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); |
paul@62 | 198 | |
paul@62 | 199 | gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0x7fa7f00f, 0x00580ff0); |
paul@62 | 200 | |
paul@62 | 201 | printf("Set up GPIO pins...\n"); |
paul@62 | 202 | |
paul@62 | 203 | jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 0); |
paul@62 | 204 | jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 0); |
paul@62 | 205 | |
paul@62 | 206 | /* Obtain HDMI reference. */ |
paul@62 | 207 | |
paul@62 | 208 | printf("Set up HDMI...\n"); |
paul@62 | 209 | |
paul@62 | 210 | hdmi = jz4780_hdmi_init(hdmi_base, hdmi_base_end, irqcap); |
paul@62 | 211 | |
paul@62 | 212 | printf("Read version...\n"); |
paul@62 | 213 | |
paul@62 | 214 | jz4780_hdmi_get_version(hdmi, &hdmi_major, &hdmi_minor); |
paul@62 | 215 | |
paul@62 | 216 | printf("HDMI version is %x.%03x\n", hdmi_major, hdmi_minor); |
paul@62 | 217 | |
paul@62 | 218 | printf("Read EDID...\n"); |
paul@62 | 219 | |
paul@62 | 220 | jz4780_hdmi_i2c_set_address(hdmi, 0x50); |
paul@62 | 221 | length = jz4780_hdmi_i2c_read(hdmi, edid, 128); |
paul@62 | 222 | |
paul@62 | 223 | if (length) |
paul@62 | 224 | { |
paul@62 | 225 | for (i = 0; i < length; i++) |
paul@62 | 226 | printf("%02x%c", edid[i], ((i % 16) != 15) ? ' ' : '\n'); |
paul@62 | 227 | } |
paul@62 | 228 | |
paul@62 | 229 | show_timings(edid); |
paul@62 | 230 | |
paul@62 | 231 | /* Detach from the interrupt. */ |
paul@62 | 232 | |
paul@62 | 233 | err = l4_error(l4_irq_detach(irqcap)); |
paul@62 | 234 | |
paul@62 | 235 | if (err) |
paul@62 | 236 | printf("Error detaching from IRQ: %ld\n", err); |
paul@62 | 237 | |
paul@62 | 238 | printf("Done.\n"); |
paul@62 | 239 | |
paul@62 | 240 | return 0; |
paul@62 | 241 | } |