1 /* 2 * Access the HDMI I2C peripheral on the MIPS Creator CI20 board. 3 * 4 * Copyright (C) 2020 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/devices/cpm-jz4780.h> 23 #include <l4/devices/gpio-jz4780.h> 24 #include <l4/devices/hdmi-jz4780.h> 25 26 #include <l4/devices/lcd-jz4780.h> 27 #include <l4/devices/lcd-jz4740-config.h> 28 #include <l4/devices/lcd-jz4740-panel.h> 29 30 #include <l4/devices/memory.h> 31 32 #include <l4/re/c/dataspace.h> 33 #include <l4/re/c/mem_alloc.h> 34 #include <l4/re/c/rm.h> 35 #include <l4/re/c/util/cap_alloc.h> 36 #include <l4/re/env.h> 37 38 #include <l4/sys/cache.h> 39 #include <l4/sys/factory.h> 40 #include <l4/sys/icu.h> 41 #include <l4/sys/ipc.h> 42 #include <l4/sys/irq.h> 43 #include <l4/sys/rcv_endpoint.h> 44 45 #include <l4/io/io.h> 46 #include <l4/libedid/edid.h> 47 #include <l4/vbus/vbus.h> 48 49 #include <stdio.h> 50 #include <unistd.h> 51 #include <stdint.h> 52 #include <string.h> 53 #include <stdlib.h> 54 55 56 57 enum { 58 DDCSCL = 24, /* via PORTF */ 59 DDCSDA = 25, /* via PORTF */ 60 }; 61 62 63 64 /* Test panel. */ 65 66 static struct Jz4740_lcd_panel panel = { 67 .config = ( 68 Jz4740_lcd_mode_tft_generic 69 | Jz4740_lcd_bpp_24 70 | Jz4740_lcd_desc_8_word 71 | Jz4740_lcd_underrun_recover 72 | Jz4740_lcd_ps_disabled 73 | Jz4740_lcd_cls_disabled 74 | Jz4740_lcd_spl_disabled 75 | Jz4740_lcd_rev_disabled 76 | Jz4740_lcd_pclock_negative 77 | Jz4740_lcd_hsync_positive 78 | Jz4740_lcd_vsync_positive 79 | Jz4740_lcd_de_positive), 80 81 .width = 1280, 82 .height = 1024, 83 .bpp = 24, 84 .frame_rate = 60, 85 .hsync = 112, 86 .vsync = 3, 87 .line_start = 248, // back porch (blanking - hsync - offset) 88 .line_end = 48, // front porch (sync offset) 89 .frame_start = 36, // back porch (blanking - vsync - offset) 90 .frame_end = 3, // front porch (sync offset) 91 }; 92 93 94 95 /* Device and resource discovery. */ 96 97 static long item_in_range(long start, long end, long index) 98 { 99 if (start < end) 100 return start + index; 101 else 102 return start - index; 103 } 104 105 static void show_timings(uint8_t *buf) 106 { 107 unsigned int width, height; 108 109 /* Attempt to decode EDID information. */ 110 111 libedid_prefered_resolution(buf, &width, &height); 112 printf("Preferred resolution: %d x %d\n", width, height); 113 114 libedid_dump_standard_timings(buf); 115 } 116 117 int main(void) 118 { 119 long err; 120 121 /* Buffer for EDID data. */ 122 123 uint8_t edid[128]; 124 unsigned int length = 0, i; 125 126 /* Version details. */ 127 128 uint8_t hdmi_major; 129 uint16_t hdmi_minor; 130 const struct Phy_capabilities *phy_def; 131 132 /* Peripheral memory. */ 133 134 l4_addr_t cpm_base = 0, cpm_base_end = 0; 135 l4_addr_t gpio_base = 0, gpio_base_end = 0; 136 l4_addr_t hdmi_base = 0, hdmi_base_end = 0; 137 l4_addr_t lcd_base = 0, lcd_base_end = 0; 138 l4_addr_t port_f, port_f_end; 139 140 /* Peripheral abstractions. */ 141 142 void *cpm; 143 void *gpio_port_f; 144 void *hdmi; 145 void *lcd; 146 147 /* Allocated memory. */ 148 149 l4_cap_idx_t desc_mem, fb_mem; 150 l4_size_t desc_size, desc_psize, fb_size, fb_psize; 151 l4_addr_t desc_addr, desc_paddr, fb_addr, fb_paddr; 152 unsigned char *picture; 153 unsigned char *fb_picture, *fb_picture_row; 154 unsigned int x, y; 155 156 /* Access to IRQs. */ 157 158 l4_uint32_t hdmi_irq_start = 0, hdmi_irq_end = 0; 159 l4_uint32_t lcd_irq_start = 0, lcd_irq_end = 0; 160 l4_cap_idx_t icucap, hdmi_irq, lcd_irq; 161 162 /* Capability allocation. */ 163 164 desc_mem = l4re_util_cap_alloc(); 165 fb_mem = l4re_util_cap_alloc(); 166 hdmi_irq = l4re_util_cap_alloc(); 167 lcd_irq = l4re_util_cap_alloc(); 168 icucap = l4re_env_get_cap("icu"); 169 170 if (l4_is_invalid_cap(icucap)) 171 { 172 printf("No 'icu' capability available in the virtual bus.\n"); 173 return 1; 174 } 175 176 if (l4_is_invalid_cap(desc_mem) || l4_is_invalid_cap(fb_mem) || 177 l4_is_invalid_cap(hdmi_irq) || l4_is_invalid_cap(lcd_irq)) 178 { 179 printf("Capabilities could not be reserved.\n"); 180 return 1; 181 } 182 183 /* Obtain resource details describing the interrupt for HDMI I2C. */ 184 185 printf("Access IRQ...\n"); 186 187 if (get_irq("jz4780-hdmi", &hdmi_irq_start, &hdmi_irq_end) < 0) 188 return 1; 189 190 printf("HDMI IRQ range at %d...%d.\n", hdmi_irq_start, hdmi_irq_end); 191 192 if (get_irq("jz4780-lcd", &lcd_irq_start, &lcd_irq_end) < 0) 193 return 1; 194 195 printf("LCD IRQ range at %d...%d.\n", lcd_irq_start, lcd_irq_end); 196 197 /* Create interrupt objects. */ 198 199 err = l4_error(l4_factory_create_irq(l4re_global_env->factory, hdmi_irq)) || 200 l4_error(l4_factory_create_irq(l4re_global_env->factory, lcd_irq)); 201 202 if (err) 203 { 204 printf("Could not create IRQ object: %lx\n", err); 205 return 1; 206 } 207 208 /* Bind interrupt objects to IRQ numbers. Here, the first HDMI interrupt is 209 bound, this being the general HDMI interrupt. */ 210 211 err = l4_error(l4_icu_bind(icucap, 212 item_in_range(hdmi_irq_start, hdmi_irq_end, 0), 213 hdmi_irq)) || 214 l4_error(l4_icu_bind(icucap, 215 item_in_range(lcd_irq_start, lcd_irq_end, 0), 216 lcd_irq)); 217 218 if (err) 219 { 220 printf("Could not bind IRQ to the ICU: %ld\n", err); 221 return 1; 222 } 223 224 /* Attach ourselves to the interrupt handler with some arbitrary labels. */ 225 226 err = l4_error(l4_rcv_ep_bind_thread(hdmi_irq, l4re_env()->main_thread, 0x00)) || 227 l4_error(l4_rcv_ep_bind_thread(lcd_irq, l4re_env()->main_thread, 0x10)); 228 229 if (err) 230 { 231 printf("Could not attach to IRQs: %ld\n", err); 232 return 1; 233 } 234 235 /* Obtain resource details describing I/O memory. */ 236 237 printf("Access CPM...\n"); 238 239 if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) 240 return 1; 241 242 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 243 244 printf("Access GPIO...\n"); 245 246 if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) 247 return 1; 248 249 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 250 251 printf("Access HDMI...\n"); 252 253 if (get_memory("jz4780-hdmi", &hdmi_base, &hdmi_base_end) < 0) 254 return 1; 255 256 printf("HDMI at 0x%lx...0x%lx.\n", hdmi_base, hdmi_base_end); 257 258 printf("Access LCD...\n"); 259 260 if (get_memory("jz4780-lcd", &lcd_base, &lcd_base_end) < 0) 261 return 1; 262 263 printf("LCD at 0x%lx...0x%lx.\n", lcd_base, lcd_base_end); 264 265 /* Obtain CPM object. */ 266 267 cpm = jz4780_cpm_init(cpm_base); 268 269 printf("VPLL frequency: %d\n", jz4780_cpm_get_vpll_frequency(cpm)); 270 printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); 271 printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); 272 273 jz4780_cpm_stop_hdmi(cpm); 274 jz4780_cpm_set_hdmi_frequency(cpm, 27000000); 275 276 printf("HDMI divider: %d\n", jz4780_cpm_get_hdmi_divider(cpm)); 277 printf("HDMI frequency: %d\n", jz4780_cpm_get_hdmi_frequency(cpm)); 278 279 jz4780_cpm_start_hdmi(cpm); 280 281 /* Configure pins. */ 282 283 port_f = gpio_base + 0x500; 284 port_f_end = port_f + 0x100; 285 286 printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); 287 288 gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0x7fa7f00f, 0x00580ff0); 289 290 printf("Set up GPIO pins...\n"); 291 292 jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 0); 293 jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 0); 294 295 /* Obtain HDMI reference. */ 296 297 printf("Set up HDMI...\n"); 298 299 hdmi = jz4780_hdmi_init(hdmi_base, hdmi_base_end, hdmi_irq, &panel); 300 301 printf("Read version...\n"); 302 303 jz4780_hdmi_get_version(hdmi, &hdmi_major, &hdmi_minor); 304 305 printf("HDMI version is %x.%03x\n", hdmi_major, hdmi_minor); 306 307 jz4780_hdmi_get_phy_capabilities(hdmi, &phy_def); 308 309 printf("PHY type: %s\n", phy_def->name); 310 311 printf("Connected: %s\n", jz4780_hdmi_connected(hdmi) ? "yes" : "no"); 312 313 while (!jz4780_hdmi_connected(hdmi)) 314 jz4780_hdmi_wait_for_connection(hdmi); 315 316 printf("Read EDID...\n"); 317 318 jz4780_hdmi_i2c_set_address(hdmi, 0x50); 319 length = jz4780_hdmi_i2c_read(hdmi, edid, 128); 320 321 if (length) 322 { 323 for (i = 0; i < length; i++) 324 printf("%02x%c", edid[i], ((i % 16) != 15) ? ' ' : '\n'); 325 } 326 327 show_timings(edid); 328 329 /* Obtain LCD reference. */ 330 331 printf("Set up LCD...\n"); 332 333 lcd = jz4780_lcd_init(lcd_base, &panel); 334 335 /* Test initialisation with a frequency appropriate for the test panel. */ 336 337 printf("LCD source: %d\n", jz4780_cpm_get_lcd_source(cpm)); 338 printf("LCD divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm)); 339 printf("LCD frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm)); 340 printf("Desired frequency: %d\n", jz4740_lcd_get_pixel_clock(lcd)); 341 342 jz4780_cpm_stop_lcd(cpm); 343 jz4780_cpm_set_lcd_frequencies(cpm, jz4740_lcd_get_pixel_clock(lcd), 3); 344 345 printf("LCD source: %d\n", jz4780_cpm_get_lcd_source(cpm)); 346 printf("LCD divider: %d\n", jz4780_cpm_get_lcd_pixel_divider(cpm)); 347 printf("LCD frequency: %d\n", jz4780_cpm_get_lcd_pixel_frequency(cpm)); 348 printf("AHB0 frequency: %d\n", jz4780_cpm_get_hclock0_frequency(cpm)); 349 350 /* With the LCD pixel clock set up, bring up the HDMI. */ 351 352 printf("Enable HDMI output...\n"); 353 354 jz4780_hdmi_enable(hdmi, jz4780_cpm_get_lcd_pixel_frequency(cpm)); 355 356 /* Allocate descriptors and framebuffer at 2**8 == 256 byte == 64 word alignment. */ 357 358 desc_size = jz4740_lcd_get_descriptors_size(lcd); 359 fb_size = jz4740_lcd_get_screen_size(lcd); 360 361 if (l4re_ma_alloc_align(desc_size, desc_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8) || 362 l4re_ma_alloc_align(fb_size, fb_mem, L4RE_MA_CONTINUOUS | L4RE_MA_PINNED, 8)) 363 { 364 printf("Could not allocate memory.\n"); 365 return 1; 366 } 367 368 if (l4re_rm_attach((void **) &desc_addr, desc_size, 369 L4RE_RM_SEARCH_ADDR | L4RE_RM_EAGER_MAP, 370 desc_mem, 0, L4_PAGESHIFT) || 371 l4re_rm_attach((void **) &fb_addr, fb_size, 372 L4RE_RM_SEARCH_ADDR | L4RE_RM_EAGER_MAP, 373 fb_mem, 0, L4_PAGESHIFT)) 374 { 375 printf("Could not map memory.\n"); 376 return 1; 377 } 378 379 if (l4re_ds_phys(desc_mem, 0, &desc_paddr, &desc_psize) || 380 l4re_ds_phys(fb_mem, 0, &fb_paddr, &fb_psize)) 381 { 382 printf("Could not get physical addresses for memory.\n"); 383 return 1; 384 } 385 386 printf("Descriptors at %lx/%lx, size %d/%d.\n", desc_addr, desc_paddr, desc_size, desc_psize); 387 printf("Framebuffer at %lx/%lx, size %d/%d.\n", fb_addr, fb_paddr, fb_size, fb_psize); 388 389 //memset((void *) fb_addr, 0x7f, fb_size); 390 391 picture = (unsigned char *) malloc(gimp_image.width * gimp_image.height * gimp_image.bytes_per_pixel); 392 393 GIMP_IMAGE_RUN_LENGTH_DECODE(picture, 394 gimp_image.rle_pixel_data, 395 gimp_image.width * gimp_image.height, 396 gimp_image.bytes_per_pixel); 397 398 fb_picture_row = (unsigned char *) fb_addr; 399 400 for (y = 0; y < gimp_image.height; y++) 401 { 402 fb_picture = fb_picture_row; 403 404 for (x = 0; x < gimp_image.width; x++) 405 { 406 *(fb_picture + 2) = *picture++; 407 *(fb_picture + 1) = *picture++; 408 *fb_picture = *picture++; 409 *(fb_picture + 3) = 0; 410 fb_picture += 4; 411 } 412 413 fb_picture_row += jz4740_lcd_get_line_size(lcd); 414 } 415 416 l4_cache_clean_data((unsigned long) fb_addr, (unsigned long) fb_addr + fb_size); 417 418 printf("Start LCD clock and initialise LCD...\n"); 419 420 jz4780_cpm_start_lcd(cpm); 421 l4_sleep(1); // 1ms == 1000us 422 423 jz4740_lcd_disable(lcd); 424 425 jz4740_lcd_set_irq(lcd, lcd_irq, Lcd_irq_frame_end); 426 427 jz4740_lcd_config(lcd, (struct Jz4740_lcd_descriptor *) desc_addr, 428 (struct Jz4740_lcd_descriptor *) desc_paddr, 429 fb_paddr); 430 431 jz4740_lcd_enable(lcd); 432 433 printf("LCD enabled: %s\n", jz4740_lcd_enabled(lcd) ? "yes" : "no"); 434 435 printf("Wait for interrupt conditions...\n"); 436 437 for (i = 0; i < 30000; i++) 438 { 439 if (jz4740_lcd_wait_for_irq(lcd)) 440 continue; 441 442 if (!(i % 60)) 443 printf("IRQ #%d\n", i); 444 } 445 446 /* Detach from the interrupts. */ 447 448 err = l4_error(l4_irq_detach(hdmi_irq)) || 449 l4_error(l4_irq_detach(lcd_irq)); 450 451 if (err) 452 printf("Error detaching from IRQ: %ld\n", err); 453 454 printf("Done.\n"); 455 456 return 0; 457 }