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 void i2c_scan(void *i2c_channel, l4_cap_idx_t irqcap) 130 { 131 uint8_t buf[1]; 132 unsigned int address; 133 134 for (address = 0; address < 0x20; address++) 135 printf("%02x ", address); 136 printf("\n"); 137 138 for (address = 0; address < 0x20; address++) 139 printf("-- "); 140 141 for (address = 0; address < 0x80; address++) 142 { 143 if ((address % 32) == 0) 144 printf("\n"); 145 146 jz4780_i2c_set_target(i2c_channel, address); 147 buf[0] = 0; 148 i2c_write(i2c_channel, buf, 1, irqcap); 149 i2c_read(i2c_channel, buf, 1, irqcap); 150 jz4780_i2c_stop(i2c_channel); 151 152 if (!jz4780_i2c_failed(i2c_channel)) 153 printf("%02x ", address); 154 else 155 printf("?? "); 156 } 157 158 printf("\n"); 159 for (address = 0; address < 0x20; address++) 160 printf("-- "); 161 printf("\n\n"); 162 } 163 164 static void ddc_read(void *i2c_channel, l4_cap_idx_t irqcap) 165 { 166 /* Buffer for reading. */ 167 168 uint8_t buf[128]; 169 long pos; 170 171 /* DDC EDID details. */ 172 173 unsigned width = 0, height = 0; 174 175 printf("Trying DDC on I2C4...\n"); 176 177 /* Attempt to read from address 0x50 for DDC. 178 See: drivers/video/fbdev/core/fb_ddc.c */ 179 180 jz4780_i2c_set_target(i2c_channel, 0x50); 181 buf[0] = 0; 182 i2c_write(i2c_channel, buf, 1, irqcap); 183 memset(buf, 0, 128); 184 pos = i2c_read(i2c_channel, buf, 128, irqcap); 185 jz4780_i2c_stop(i2c_channel); 186 187 if (pos <= 0) 188 { 189 printf("Read failed.\n"); 190 return; 191 } 192 193 show_data(buf, pos); 194 195 /* Attempt to decode EDID information. */ 196 197 libedid_prefered_resolution(buf, &width, &height); 198 printf("Preferred resolution: %d x %d\n", width, height); 199 200 libedid_dump_standard_timings(buf); 201 } 202 203 static void pmic_out3_control(void *i2c_channel, l4_cap_idx_t irqcap) 204 { 205 /* Buffer for communicating. */ 206 207 uint8_t buf[2]; 208 209 /* Attempt to read from address 0x5a for PMIC OUT3. */ 210 211 jz4780_i2c_set_target(i2c_channel, 0x5a); 212 buf[0] = 0x32; 213 i2c_write(i2c_channel, buf, 1, irqcap); 214 i2c_read(i2c_channel, buf, 1, irqcap); 215 jz4780_i2c_stop(i2c_channel); 216 217 if (!jz4780_i2c_failed(i2c_channel)) 218 printf("Read: %02x\n", buf[0]); 219 else 220 printf("Read failed.\n"); 221 222 #if 0 223 /* Disable the regulator. */ 224 225 printf("Updating...\n"); 226 227 buf[1] = buf[0] & ~0x80; 228 buf[0] = 0x32; 229 i2c_write(i2c_channel, buf, 2, irqcap); 230 jz4780_i2c_stop(i2c_channel); 231 232 /* Read back from the register. Seemed to give 0xff, which makes no real 233 sense, although the regulator did bring the voltage level low. */ 234 235 buf[0] = 0x32; 236 i2c_write(i2c_channel, buf, 1, irqcap); 237 i2c_read(i2c_channel, buf, 1, irqcap); 238 jz4780_i2c_stop(i2c_channel); 239 240 if (!jz4780_i2c_failed(i2c_channel)) 241 printf("Read: %02x\n", buf[0]); 242 else 243 printf("Read failed.\n"); 244 #endif 245 } 246 247 int main(void) 248 { 249 long err; 250 251 /* Peripheral memory. */ 252 253 l4_addr_t gpio_base = 0, gpio_base_end = 0; 254 l4_addr_t i2c_base = 0, i2c_base_end = 0; 255 l4_addr_t cpm_base = 0, cpm_base_end = 0; 256 l4_addr_t port_d, port_d_end; 257 l4_addr_t port_e, port_e_end; 258 l4_addr_t port_f, port_f_end; 259 260 /* Peripheral abstractions. */ 261 262 void *gpio_port_d, *gpio_port_e, *gpio_port_f; 263 void *i2c, *cpm; 264 void *i2c0, *i2c4; 265 266 /* IRQ resources. */ 267 268 l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; 269 l4_cap_idx_t icucap, irq0cap, irq4cap; 270 271 /* Obtain capabilities for the interrupt controller and an interrupt. */ 272 273 irq0cap = l4re_util_cap_alloc(); 274 irq4cap = l4re_util_cap_alloc(); 275 icucap = l4re_env_get_cap("icu"); 276 277 if (l4_is_invalid_cap(icucap)) 278 { 279 printf("No 'icu' capability available in the virtual bus.\n"); 280 return 1; 281 } 282 283 if (l4_is_invalid_cap(irq0cap) || l4_is_invalid_cap(irq4cap)) 284 { 285 printf("Capabilities not available for interrupts.\n"); 286 return 1; 287 } 288 289 /* Obtain resource details describing the interrupt for I2C channel 4. */ 290 291 printf("Access IRQ...\n"); 292 293 if (get_irq("jz4780-i2c", &i2c_irq_start, &i2c_irq_end) < 0) 294 return 1; 295 296 printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); 297 298 /* Obtain resource details describing I/O memory. */ 299 300 printf("Access GPIO...\n"); 301 302 if (get_memory("jz4780-gpio", &gpio_base, &gpio_base_end) < 0) 303 return 1; 304 305 printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); 306 307 printf("Access CPM...\n"); 308 309 if (get_memory("jz4780-cpm", &cpm_base, &cpm_base_end) < 0) 310 return 1; 311 312 printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); 313 314 printf("Access I2C...\n"); 315 316 if (get_memory("jz4780-i2c", &i2c_base, &i2c_base_end) < 0) 317 return 1; 318 319 printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); 320 321 /* Create interrupt objects. */ 322 323 err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq0cap)) || 324 l4_error(l4_factory_create_irq(l4re_global_env->factory, irq4cap)); 325 326 if (err) 327 { 328 printf("Could not create IRQ object: %lx\n", err); 329 return 1; 330 } 331 332 /* Bind interrupt objects to IRQ numbers. */ 333 334 err = l4_error(l4_icu_bind(icucap, 335 item_in_range(i2c_irq_start, i2c_irq_end, 0), 336 irq0cap)) || 337 l4_error(l4_icu_bind(icucap, 338 item_in_range(i2c_irq_start, i2c_irq_end, 4), 339 irq4cap)); 340 341 if (err) 342 { 343 printf("Could not bind IRQ to the ICU: %ld\n", err); 344 return 1; 345 } 346 347 /* Attach ourselves to the interrupt handler. */ 348 349 err = l4_error(l4_rcv_ep_bind_thread(irq0cap, l4re_env()->main_thread, 0)) || 350 l4_error(l4_rcv_ep_bind_thread(irq4cap, l4re_env()->main_thread, 4)); 351 352 if (err) 353 { 354 printf("Could not attach to IRQs: %ld\n", err); 355 return 1; 356 } 357 358 /* Configure pins. */ 359 360 port_d = gpio_base + 0x300; 361 port_d_end = port_d + 0x100; 362 port_e = gpio_base + 0x400; 363 port_e_end = port_e + 0x100; 364 port_f = gpio_base + 0x500; 365 port_f_end = port_f + 0x100; 366 367 printf("PORTD at 0x%lx...0x%lx.\n", port_d, port_d_end); 368 printf("PORTE at 0x%lx...0x%lx.\n", port_e, port_e_end); 369 printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); 370 371 gpio_port_d = jz4780_gpio_init(port_d, port_d_end, 32, 0xffff4fff, 0x0000b000); 372 gpio_port_e = jz4780_gpio_init(port_e, port_e_end, 32, 0xfffff37c, 0x00000483); 373 gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0x7fa7f00f, 0x00580ff0); 374 375 printf("Set up GPIO pins...\n"); 376 377 jz4780_gpio_config_pad(gpio_port_d, PMSCL, Function_alt, 0); 378 jz4780_gpio_config_pad(gpio_port_d, PMSDA, Function_alt, 0); 379 380 jz4780_gpio_config_pad(gpio_port_e, RTCSCL, Function_alt, 1); 381 jz4780_gpio_config_pad(gpio_port_e, RTCSDA, Function_alt, 1); 382 383 jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 1); 384 jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 1); 385 386 /* Obtain CPM and I2C objects. */ 387 388 cpm = jz4780_cpm_init(cpm_base); 389 390 /* Attempt to set the PCLK source to SCLK_A. */ 391 392 jz4780_cpm_set_pclock_source(cpm, 1); 393 printf("Peripheral clock: %d\n", jz4780_cpm_get_pclock_frequency(cpm)); 394 395 /* Obtain I2C reference. */ 396 397 i2c = jz4780_i2c_init(i2c_base, i2c_base_end, cpm, 100000); /* 100 kHz */ 398 i2c0 = jz4780_i2c_get_channel(i2c, 0); 399 i2c4 = jz4780_i2c_get_channel(i2c, 4); 400 401 printf("Scan I2C0...\n"); 402 i2c_scan(i2c0, irq0cap); 403 404 printf("Scan I2C4...\n"); 405 i2c_scan(i2c4, irq4cap); 406 407 pmic_out3_control(i2c0, irq0cap); 408 409 ddc_read(i2c4, irq4cap); 410 411 jz4780_i2c_disable(i2c0); 412 jz4780_i2c_disable(i2c4); 413 414 /* Detach from the interrupt. */ 415 416 err = l4_error(l4_irq_detach(irq0cap)) || l4_error(l4_irq_detach(irq4cap)); 417 418 if (err) 419 printf("Error detaching from IRQ objects: %ld\n", err); 420 421 printf("Done.\n"); 422 423 return 0; 424 }