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