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