1 /* 2 * (c) 2008-2009 Adam Lackorzynski <adam@os.inf.tu-dresden.de> 3 * economic rights: Technische Universit??t Dresden (Germany) 4 * Copyright (C) 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This file is part of TUD:OS and distributed under the terms of the 7 * GNU General Public License 2. 8 * Please see the COPYING-GPL-2 file for details. 9 */ 10 /* 11 * Access the I2C peripherals on the MIPS Creator CI20 board. 12 */ 13 14 #include <l4/devices/cpm-jz4780.h> 15 #include <l4/devices/gpio-jz4780.h> 16 #include <l4/devices/i2c-jz4780.h> 17 #include <l4/devices/memory.h> 18 #include <l4/io/io.h> 19 #include <l4/libedid/edid.h> 20 #include <l4/re/env.h> 21 #include <l4/re/c/util/cap_alloc.h> 22 #include <l4/sys/factory.h> 23 #include <l4/sys/icu.h> 24 #include <l4/sys/ipc.h> 25 #include <l4/sys/irq.h> 26 #include <l4/sys/rcv_endpoint.h> 27 #include <l4/vbus/vbus.h> 28 #include <stdio.h> 29 #include <unistd.h> 30 #include <stdint.h> 31 #include <string.h> 32 33 34 35 enum { 36 PMSDA = 30, /* via PORTD */ 37 PMSCL = 31, /* via PORTD */ 38 PWM3 = 3, /* via PORTE */ 39 PWM4 = 4, /* via PORTE */ 40 RTCSDA = 12, /* via PORTE */ 41 RTCSCL = 13, /* via PORTE */ 42 DDCSCL = 24, /* via PORTF */ 43 DDCSDA = 25, /* via PORTF */ 44 }; 45 46 47 48 /* Device and resource discovery. */ 49 50 static long item_in_range(long start, long end, long index) 51 { 52 if (start < end) 53 return start + index; 54 else 55 return start - index; 56 } 57 58 static void show_deviceid(uint8_t buf[]) 59 { 60 printf("Manufacturer %x part %x revision %x\n", 61 (buf[0] << 4) | (buf[1] >> 4), 62 ((buf[1] & 0xf) << 5) | (buf[2] >> 3), 63 buf[2] & 0x7); 64 } 65 66 static void show_data(uint8_t buf[], unsigned pos) 67 { 68 unsigned i; 69 70 if (pos) 71 { 72 printf("Read %d bytes from bus.\n", pos); 73 for (i = 0; i < pos; i++) 74 printf(" %02x", buf[i]); 75 printf("\n"); 76 } 77 else 78 printf("No reply from bus.\n"); 79 } 80 81 static long i2c_read(void *i2c_channel, uint8_t *buf, unsigned length, 82 l4_cap_idx_t irqcap) 83 { 84 l4_msgtag_t tag; 85 long err; 86 87 jz4780_i2c_start_read(i2c_channel, buf, length); 88 89 while (!jz4780_i2c_read_done(i2c_channel)) 90 { 91 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_rel(1, 20))); 92 93 if ((err = l4_ipc_error(tag, l4_utcb()))) 94 return err; 95 96 if (jz4780_i2c_failed(i2c_channel)) 97 return 0; 98 99 jz4780_i2c_read(i2c_channel); 100 } 101 102 return jz4780_i2c_have_read(i2c_channel); 103 } 104 105 static long i2c_write(void *i2c_channel, uint8_t *buf, unsigned length, 106 l4_cap_idx_t irqcap) 107 { 108 l4_msgtag_t tag; 109 long err; 110 111 jz4780_i2c_start_write(i2c_channel, buf, length); 112 113 while (!jz4780_i2c_write_done(i2c_channel)) 114 { 115 tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_rel(1, 20))); 116 117 if ((err = l4_ipc_error(tag, l4_utcb()))) 118 return err; 119 120 if (jz4780_i2c_failed(i2c_channel)) 121 return 0; 122 123 jz4780_i2c_write(i2c_channel); 124 } 125 126 return jz4780_i2c_have_written(i2c_channel); 127 } 128 129 static long i2c_get(void *i2c_channel, uint8_t reg, uint8_t *buffer, long length, l4_cap_idx_t irqcap) 130 { 131 long pos; 132 133 buffer[0] = reg; 134 i2c_write(i2c_channel, buffer, 1, irqcap); 135 136 memset(buffer, 0, length); 137 pos = i2c_read(i2c_channel, buffer, length, irqcap); 138 139 jz4780_i2c_stop(i2c_channel); 140 141 return pos; 142 } 143 144 static void i2c_report(void *i2c_channel, uint8_t *buffer, long length) 145 { 146 if (!jz4780_i2c_failed(i2c_channel)) 147 { 148 printf("Read: "); 149 while (length--) 150 printf("%02x", *(buffer++)); 151 printf("\n"); 152 } 153 else 154 printf("Read failed.\n"); 155 } 156 157 static void i2c_scan(void *i2c_channel, l4_cap_idx_t irqcap) 158 { 159 uint8_t buf[1]; 160 unsigned int address; 161 162 for (address = 0; address < 0x20; address++) 163 printf("%02x ", address); 164 printf("\n"); 165 166 for (address = 0; address < 0x20; address++) 167 printf("-- "); 168 169 for (address = 0; address < 0x80; address++) 170 { 171 if ((address % 32) == 0) 172 printf("\n"); 173 174 jz4780_i2c_set_target(i2c_channel, address); 175 i2c_get(i2c_channel, 0, buf, 1, irqcap); 176 177 if (!jz4780_i2c_failed(i2c_channel)) 178 printf("%02x ", address); 179 else 180 printf("?? "); 181 } 182 183 printf("\n"); 184 for (address = 0; address < 0x20; address++) 185 printf("-- "); 186 printf("\n\n"); 187 } 188 189 static void ddc_read(void *i2c_channel, l4_cap_idx_t irqcap) 190 { 191 /* Buffer for reading. */ 192 193 uint8_t buf[128]; 194 long pos; 195 196 /* DDC EDID details. */ 197 198 unsigned width = 0, height = 0; 199 200 printf("Trying DDC on I2C4...\n"); 201 202 /* Attempt to read from address 0x50 for DDC. 203 See: drivers/video/fbdev/core/fb_ddc.c */ 204 205 jz4780_i2c_set_target(i2c_channel, 0x50); 206 pos = i2c_get(i2c_channel, 0, buf, 128, irqcap); 207 208 if (pos <= 0) 209 { 210 printf("Read failed.\n"); 211 return; 212 } 213 214 show_data(buf, pos); 215 216 /* Attempt to decode EDID information. */ 217 218 libedid_prefered_resolution(buf, &width, &height); 219 printf("Preferred resolution: %d x %d\n", width, height); 220 221 libedid_dump_standard_timings(buf); 222 } 223 224 static void pmic_out3_control(void *i2c_channel, l4_cap_idx_t irqcap) 225 { 226 /* Buffer for communicating. */ 227 228 uint8_t buf[2]; 229 230 /* Attempt to read from address 0x5a for PMIC OUT3. */ 231 232 jz4780_i2c_set_target(i2c_channel, 0x5a); 233 i2c_get(i2c_channel, 0x32, buf, 1, irqcap); 234 i2c_report(i2c_channel, buf, 1); 235 236 #if 0 237 /* Disable the regulator. */ 238 239 printf("Updating...\n"); 240 241 buf[1] = buf[0] & ~0x80; 242 buf[0] = 0x32; 243 i2c_write(i2c_channel, buf, 2, irqcap); 244 jz4780_i2c_stop(i2c_channel); 245 246 /* Read back from the register. Seemed to give 0xff, which makes no real 247 sense, although the regulator did bring the voltage level low. */ 248 249 i2c_get(i2c_channel, 0x32, buf, 1, irqcap); 250 i2c_report(i2c_channel, buf, 1); 251 #endif 252 } 253 254 static void rtc_control(void *i2c_channel, l4_cap_idx_t irqcap) 255 { 256 /* Buffer for communicating. */ 257 258 uint8_t buf[1]; 259 260 /* Attempt to read from address 0x51 for RTC. */ 261 262 jz4780_i2c_set_target(i2c_channel, 0x51); 263 i2c_get(i2c_channel, 0x0f, buf, 1, irqcap); 264 i2c_report(i2c_channel, buf, 1); 265 } 266 267 int main(void) 268 { 269 long err; 270 271 /* Peripheral memory. */ 272 273 l4_addr_t gpio_base = 0, gpio_base_end = 0; 274 l4_addr_t i2c_base = 0, i2c_base_end = 0; 275 l4_addr_t cpm_base = 0, cpm_base_end = 0; 276 l4_addr_t port_d, port_d_end; 277 l4_addr_t port_e, port_e_end; 278 l4_addr_t port_f, port_f_end; 279 280 /* Peripheral abstractions. */ 281 282 void *gpio_port_d, *gpio_port_e, *gpio_port_f; 283 void *i2c, *cpm; 284 void *i2c0, *i2c4; 285 286 /* IRQ resources. */ 287 288 l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; 289 l4_cap_idx_t icucap, irq0cap, irq4cap; 290 291 /* Obtain capabilities for the interrupt controller and an interrupt. */ 292 293 irq0cap = l4re_util_cap_alloc(); 294 irq4cap = l4re_util_cap_alloc(); 295 icucap = l4re_env_get_cap("icu"); 296 297 if (l4_is_invalid_cap(icucap)) 298 { 299 printf("No 'icu' capability available in the virtual bus.\n"); 300 return 1; 301 } 302 303 if (l4_is_invalid_cap(irq0cap) || l4_is_invalid_cap(irq4cap)) 304 { 305 printf("Capabilities not available for interrupts.\n"); 306 return 1; 307 } 308 309 /* Obtain resource details describing the interrupt for I2C channel 4. */ 310 311 printf("Access IRQ...\n"); 312 313 if (get_irq("jz4780-i2c", &i2c_irq_start, &i2c_irq_end) < 0) 314 return 1; 315 316 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 317 318 /* Obtain resource details describing I/O memory. */ 319 320 printf("Access GPIO...\n"); 321 322 if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) 323 return 1; 324 325 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 326 327 printf("Access CPM...\n"); 328 329 if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) 330 return 1; 331 332 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 333 334 printf("Access I2C...\n"); 335 336 if (get_memory("jz4780-i2c", &i2c_base, &i2c_base_end) < 0) 337 return 1; 338 339 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 340 341 /* Create interrupt objects. */ 342 343 err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq0cap)) || 344 l4_error(l4_factory_create_irq(l4re_global_env->factory, irq4cap)); 345 346 if (err) 347 { 348 printf("Could not create IRQ object: %lx\n", err); 349 return 1; 350 } 351 352 /* Bind interrupt objects to IRQ numbers. */ 353 354 err = l4_error(l4_icu_bind(icucap, 355 item_in_range(i2c_irq_start, i2c_irq_end, 0), 356 irq0cap)) || 357 l4_error(l4_icu_bind(icucap, 358 item_in_range(i2c_irq_start, i2c_irq_end, 4), 359 irq4cap)); 360 361 if (err) 362 { 363 printf("Could not bind IRQ to the ICU: %ld\n", err); 364 return 1; 365 } 366 367 /* Attach ourselves to the interrupt handler. */ 368 369 err = l4_error(l4_rcv_ep_bind_thread(irq0cap, l4re_env()->main_thread, 0)) || 370 l4_error(l4_rcv_ep_bind_thread(irq4cap, l4re_env()->main_thread, 4)); 371 372 if (err) 373 { 374 printf("Could not attach to IRQs: %ld\n", err); 375 return 1; 376 } 377 378 /* Configure pins. */ 379 380 port_d = gpio_base + 0x300; 381 port_d_end = port_d + 0x100; 382 port_e = gpio_base + 0x400; 383 port_e_end = port_e + 0x100; 384 port_f = gpio_base + 0x500; 385 port_f_end = port_f + 0x100; 386 387 printf("PORTD at 0x%lx...0x%lx.\n", port_d, port_d_end); 388 printf("PORTE at 0x%lx...0x%lx.\n", port_e, port_e_end); 389 printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); 390 391 gpio_port_d = jz4780_gpio_init(port_d, port_d_end, 32, 0xffff4fff, 0x0000b000); 392 gpio_port_e = jz4780_gpio_init(port_e, port_e_end, 32, 0xfffff37c, 0x00000483); 393 gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0x7fa7f00f, 0x00580ff0); 394 395 printf("Set up GPIO pins...\n"); 396 397 jz4780_gpio_config_pad(gpio_port_d, PMSCL, Function_alt, 0); 398 jz4780_gpio_config_pad(gpio_port_d, PMSDA, Function_alt, 0); 399 400 //jz4780_gpio_config_pad(gpio_port_e, RTCSCL, Function_alt, 1); 401 //jz4780_gpio_config_pad(gpio_port_e, RTCSDA, Function_alt, 1); 402 403 jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 1); 404 jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 1); 405 406 /* Obtain CPM and I2C objects. */ 407 408 cpm = jz4780_cpm_init(cpm_base); 409 410 /* Attempt to set the PCLK source to SCLK_A. */ 411 412 jz4780_cpm_set_pclock_source(cpm, 1); 413 printf("Peripheral clock: %d\n", jz4780_cpm_get_pclock_frequency(cpm)); 414 415 /* Obtain I2C reference. */ 416 417 i2c = jz4780_i2c_init(i2c_base, i2c_base_end, cpm, 100000); /* 100 kHz */ 418 i2c0 = jz4780_i2c_get_channel(i2c, 0); 419 i2c4 = jz4780_i2c_get_channel(i2c, 4); 420 421 printf("Scan I2C0...\n"); 422 i2c_scan(i2c0, irq0cap); 423 424 printf("Scan I2C4...\n"); 425 i2c_scan(i2c4, irq4cap); 426 427 pmic_out3_control(i2c0, irq0cap); 428 429 ddc_read(i2c4, irq4cap); 430 431 //rtc_control(i2c4, irq4cap); 432 433 jz4780_i2c_disable(i2c0); 434 jz4780_i2c_disable(i2c4); 435 436 /* Detach from the interrupt. */ 437 438 err = l4_error(l4_irq_detach(irq0cap)) || l4_error(l4_irq_detach(irq4cap)); 439 440 if (err) 441 printf("Error detaching from IRQ objects: %ld\n", err); 442 443 printf("Done.\n"); 444 445 return 0; 446 }