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@0 | 4 | * (c) 2017, 2018 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@0 | 17 | #include <l4/io/io.h> |
paul@0 | 18 | #include <l4/libedid/edid.h> |
paul@0 | 19 | #include <l4/re/env.h> |
paul@0 | 20 | #include <l4/re/c/util/cap_alloc.h> |
paul@0 | 21 | #include <l4/sys/factory.h> |
paul@0 | 22 | #include <l4/sys/icu.h> |
paul@0 | 23 | #include <l4/sys/ipc.h> |
paul@0 | 24 | #include <l4/sys/irq.h> |
paul@0 | 25 | #include <l4/vbus/vbus.h> |
paul@0 | 26 | #include <stdio.h> |
paul@0 | 27 | #include <unistd.h> |
paul@0 | 28 | #include <stdint.h> |
paul@0 | 29 | |
paul@0 | 30 | |
paul@0 | 31 | |
paul@0 | 32 | enum { |
paul@0 | 33 | DDCSCL = 24, /* via PORTF */ |
paul@0 | 34 | DDCSDA = 25, /* via PORTF */ |
paul@0 | 35 | }; |
paul@0 | 36 | |
paul@0 | 37 | |
paul@0 | 38 | |
paul@0 | 39 | /* Device and resource discovery. */ |
paul@0 | 40 | |
paul@0 | 41 | static char const *resource_type(enum l4io_resource_types_t type) |
paul@0 | 42 | { |
paul@0 | 43 | switch (type) |
paul@0 | 44 | { |
paul@0 | 45 | case L4VBUS_RESOURCE_INVALID: |
paul@0 | 46 | return "INVALID"; |
paul@0 | 47 | |
paul@0 | 48 | case L4VBUS_RESOURCE_IRQ: |
paul@0 | 49 | return "IRQ"; |
paul@0 | 50 | |
paul@0 | 51 | case L4VBUS_RESOURCE_MEM: |
paul@0 | 52 | return "MEMORY"; |
paul@0 | 53 | |
paul@0 | 54 | default: |
paul@0 | 55 | return "OTHER"; |
paul@0 | 56 | } |
paul@0 | 57 | } |
paul@0 | 58 | |
paul@0 | 59 | static int vbus_get_device(char const *hid, l4io_device_handle_t *dh, l4io_resource_handle_t *rh) |
paul@0 | 60 | { |
paul@0 | 61 | int result = l4io_lookup_device(hid, dh, 0, rh); |
paul@0 | 62 | |
paul@0 | 63 | if (result < 0) |
paul@0 | 64 | printf("Could not access '%s': %s\n", hid, result == -L4_ENOENT ? "no such device" : "no device"); |
paul@0 | 65 | |
paul@0 | 66 | return result; |
paul@0 | 67 | } |
paul@0 | 68 | |
paul@0 | 69 | static int vbus_get_resource(l4io_device_handle_t dh, l4io_resource_t *res, |
paul@0 | 70 | enum l4io_resource_types_t type) |
paul@0 | 71 | { |
paul@0 | 72 | int current = 0, result = 0; |
paul@0 | 73 | l4_cap_idx_t vbus = l4re_env_get_cap("vbus"); |
paul@0 | 74 | |
paul@0 | 75 | do |
paul@0 | 76 | { |
paul@0 | 77 | result = l4vbus_get_resource(vbus, dh, current, res); |
paul@0 | 78 | |
paul@0 | 79 | if (result) |
paul@0 | 80 | printf("Could not access resource of type %s.\n", resource_type(type)); |
paul@0 | 81 | else |
paul@0 | 82 | printf("Resource %d: type %s, start=%lx, end=%lx\n", res->id, |
paul@0 | 83 | resource_type(res->type), res->start, res->end); |
paul@0 | 84 | |
paul@0 | 85 | current++; |
paul@0 | 86 | } |
paul@0 | 87 | while ((!result) && (res->type != type)); |
paul@0 | 88 | |
paul@0 | 89 | return result; |
paul@0 | 90 | } |
paul@0 | 91 | |
paul@0 | 92 | static int vbus_get_irq(char const *hid, l4_uint32_t *start, l4_uint32_t *end) |
paul@0 | 93 | { |
paul@0 | 94 | l4io_device_handle_t dh; |
paul@0 | 95 | l4io_resource_handle_t rh; |
paul@0 | 96 | l4io_resource_t res; |
paul@0 | 97 | int result; |
paul@0 | 98 | |
paul@0 | 99 | result = vbus_get_device(hid, &dh, &rh); |
paul@0 | 100 | |
paul@0 | 101 | if (result < 0) |
paul@0 | 102 | return result; |
paul@0 | 103 | |
paul@0 | 104 | result = vbus_get_resource(dh, &res, L4IO_RESOURCE_IRQ); |
paul@0 | 105 | |
paul@0 | 106 | if (result) |
paul@0 | 107 | return result; |
paul@0 | 108 | |
paul@0 | 109 | *start = res.start; |
paul@0 | 110 | *end = res.end; |
paul@0 | 111 | |
paul@0 | 112 | return result; |
paul@0 | 113 | } |
paul@0 | 114 | |
paul@0 | 115 | static int vbus_get_memory(char const *hid, l4_addr_t *start, l4_addr_t *end) |
paul@0 | 116 | { |
paul@0 | 117 | l4io_device_handle_t dh; |
paul@0 | 118 | l4io_resource_handle_t rh; |
paul@0 | 119 | l4io_resource_t res; |
paul@0 | 120 | int result; |
paul@0 | 121 | |
paul@0 | 122 | result = vbus_get_device(hid, &dh, &rh); |
paul@0 | 123 | |
paul@0 | 124 | if (result < 0) |
paul@0 | 125 | return result; |
paul@0 | 126 | |
paul@0 | 127 | result = vbus_get_resource(dh, &res, L4IO_RESOURCE_MEM); |
paul@0 | 128 | |
paul@0 | 129 | if (result) |
paul@0 | 130 | return result; |
paul@0 | 131 | |
paul@0 | 132 | if ((result = l4io_request_iomem(res.start, res.end - res.start + 1, |
paul@0 | 133 | L4IO_MEM_NONCACHED, start))) |
paul@0 | 134 | { |
paul@0 | 135 | printf("Could not get address for '%s'.\n", hid); |
paul@0 | 136 | return result; |
paul@0 | 137 | } |
paul@0 | 138 | |
paul@0 | 139 | printf("Resource at 0x%lx...0x%lx.\n", res.start, res.end); |
paul@0 | 140 | |
paul@0 | 141 | *end = *start + (res.end - res.start + 1); |
paul@0 | 142 | |
paul@0 | 143 | return 0; |
paul@0 | 144 | } |
paul@0 | 145 | |
paul@0 | 146 | static long item_in_range(long start, long end, long index) |
paul@0 | 147 | { |
paul@0 | 148 | if (start < end) |
paul@0 | 149 | return start + index; |
paul@0 | 150 | else |
paul@0 | 151 | return start - index; |
paul@0 | 152 | } |
paul@0 | 153 | |
paul@0 | 154 | static void show_deviceid(uint8_t buf[]) |
paul@0 | 155 | { |
paul@0 | 156 | printf("Manufacturer %x part %x revision %x\n", |
paul@0 | 157 | (buf[0] << 4) | (buf[1] >> 4), |
paul@0 | 158 | ((buf[1] & 0xf) << 5) | (buf[2] >> 3), |
paul@0 | 159 | buf[2] & 0x7); |
paul@0 | 160 | } |
paul@0 | 161 | |
paul@0 | 162 | static void show_data(uint8_t buf[], unsigned pos) |
paul@0 | 163 | { |
paul@0 | 164 | unsigned i; |
paul@0 | 165 | |
paul@0 | 166 | if (pos) |
paul@0 | 167 | { |
paul@0 | 168 | printf("Read %d bytes from bus.\n", pos); |
paul@0 | 169 | for (i = 0; i < pos; i++) |
paul@0 | 170 | printf(" %02x", buf[i]); |
paul@0 | 171 | printf("\n"); |
paul@0 | 172 | } |
paul@0 | 173 | else |
paul@0 | 174 | printf("No reply from bus.\n"); |
paul@0 | 175 | } |
paul@0 | 176 | |
paul@0 | 177 | int main(void) |
paul@0 | 178 | { |
paul@0 | 179 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@0 | 180 | l4_addr_t port_f, port_f_end; |
paul@0 | 181 | l4_addr_t i2c_base = 0, i2c_base_end = 0, cpm_base = 0, cpm_base_end; |
paul@0 | 182 | void *gpio_port_f; |
paul@0 | 183 | void *i2c, *i2c_channel, *cpm; |
paul@0 | 184 | l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0, i2c_irq; |
paul@0 | 185 | l4_cap_idx_t irqcap, icucap; |
paul@0 | 186 | l4_msgtag_t tag; |
paul@0 | 187 | long err; |
paul@0 | 188 | int result = 0; |
paul@0 | 189 | uint8_t buf[256]; |
paul@0 | 190 | unsigned pos; |
paul@0 | 191 | unsigned width = 0, height = 0; |
paul@0 | 192 | |
paul@0 | 193 | /* Obtain capabilities for the interrupt controller and an interrupt. */ |
paul@0 | 194 | |
paul@0 | 195 | irqcap = l4re_util_cap_alloc(); |
paul@0 | 196 | icucap = l4re_env_get_cap("icu"); |
paul@0 | 197 | |
paul@0 | 198 | if (l4_is_invalid_cap(icucap)) |
paul@0 | 199 | { |
paul@0 | 200 | printf("No 'icu' capability available in the virtual bus.\n"); |
paul@0 | 201 | return 1; |
paul@0 | 202 | } |
paul@0 | 203 | |
paul@0 | 204 | if (l4_is_invalid_cap(irqcap)) |
paul@0 | 205 | { |
paul@0 | 206 | printf("No capability available for the interrupt.\n"); |
paul@0 | 207 | return 1; |
paul@0 | 208 | } |
paul@0 | 209 | |
paul@0 | 210 | /* Obtain resource details describing the interrupt for I2C channel 4. */ |
paul@0 | 211 | |
paul@0 | 212 | printf("Access IRQ...\n"); |
paul@0 | 213 | |
paul@0 | 214 | if ((result = vbus_get_irq("jz4780-i2c", &i2c_irq_start, &i2c_irq_end)) < 0) |
paul@0 | 215 | return 1; |
paul@0 | 216 | |
paul@0 | 217 | i2c_irq = item_in_range(i2c_irq_start, i2c_irq_end, 4); |
paul@0 | 218 | printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); |
paul@0 | 219 | printf("I2C IRQ at %d.\n", i2c_irq); |
paul@0 | 220 | |
paul@0 | 221 | /* Obtain resource details describing I/O memory. */ |
paul@0 | 222 | |
paul@0 | 223 | printf("Access GPIO...\n"); |
paul@0 | 224 | |
paul@0 | 225 | if ((result = vbus_get_memory("jz4780-gpio", &gpio_base, &gpio_base_end)) < 0) |
paul@0 | 226 | return 1; |
paul@0 | 227 | |
paul@0 | 228 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@0 | 229 | |
paul@0 | 230 | printf("Access CPM...\n"); |
paul@0 | 231 | |
paul@0 | 232 | if ((result = vbus_get_memory("jz4780-cpm", &cpm_base, &cpm_base_end)) < 0) |
paul@0 | 233 | return 1; |
paul@0 | 234 | |
paul@0 | 235 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@0 | 236 | |
paul@0 | 237 | printf("Access I2C...\n"); |
paul@0 | 238 | |
paul@0 | 239 | if ((result = vbus_get_memory("jz4780-i2c", &i2c_base, &i2c_base_end)) < 0) |
paul@0 | 240 | return 1; |
paul@0 | 241 | |
paul@0 | 242 | printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); |
paul@0 | 243 | |
paul@0 | 244 | /* Create an interrupt object. */ |
paul@0 | 245 | |
paul@0 | 246 | if ((err = l4_error(tag = l4_factory_create_irq(l4re_global_env->factory, irqcap)))) |
paul@0 | 247 | { |
paul@0 | 248 | printf("Could not create IRQ object: %lx\n", err); |
paul@0 | 249 | return 1; |
paul@0 | 250 | } |
paul@0 | 251 | |
paul@0 | 252 | /* Bind the interrupt object to the IRQ number. */ |
paul@0 | 253 | |
paul@0 | 254 | if ((err = l4_error(l4_icu_bind(icucap, i2c_irq, irqcap)))) |
paul@0 | 255 | { |
paul@0 | 256 | printf("Could not bind IRQ %d to the ICU: %ld\n", i2c_irq, err); |
paul@0 | 257 | return 1; |
paul@0 | 258 | } |
paul@0 | 259 | |
paul@0 | 260 | /* Attach ourselves to the interrupt handler. */ |
paul@0 | 261 | |
paul@0 | 262 | tag = l4_irq_attach(irqcap, 0xDEAD, l4re_env()->main_thread); |
paul@0 | 263 | |
paul@0 | 264 | if ((err = l4_error(tag))) |
paul@0 | 265 | { |
paul@0 | 266 | printf("Could not attach to IRQ %d: %ld\n", i2c_irq, err); |
paul@0 | 267 | return 1; |
paul@0 | 268 | } |
paul@0 | 269 | |
paul@0 | 270 | /* Configure pins. */ |
paul@0 | 271 | |
paul@0 | 272 | port_f = gpio_base + 0x500; |
paul@0 | 273 | port_f_end = port_f + 0x100; |
paul@0 | 274 | |
paul@0 | 275 | printf("PORTF at 0x%lx...0x%lx.\n", port_f, port_f_end); |
paul@0 | 276 | |
paul@0 | 277 | gpio_port_f = jz4780_gpio_init(port_f, port_f_end, 32, 0xffa7f00f, 0x00580ff0); |
paul@0 | 278 | |
paul@0 | 279 | printf("Set up GPIO pins...\n"); |
paul@0 | 280 | |
paul@0 | 281 | jz4780_gpio_config_pad(gpio_port_f, DDCSCL, Function_alt, 1); |
paul@0 | 282 | jz4780_gpio_config_pad(gpio_port_f, DDCSDA, Function_alt, 1); |
paul@0 | 283 | |
paul@0 | 284 | /* Obtain CPM and I2C objects. */ |
paul@0 | 285 | |
paul@0 | 286 | cpm = jz4780_cpm_init(cpm_base); |
paul@0 | 287 | |
paul@0 | 288 | /* Attempt to set the PCLK source to SCLK_A. */ |
paul@0 | 289 | |
paul@0 | 290 | jz4780_cpm_set_pclock_source(cpm, 1); |
paul@0 | 291 | printf("Peripheral clock: %d\n", jz4780_cpm_get_pclock_frequency(cpm)); |
paul@0 | 292 | |
paul@0 | 293 | /* Obtain I2C reference. */ |
paul@0 | 294 | |
paul@0 | 295 | i2c = jz4780_i2c_init(i2c_base, i2c_base_end, cpm, 100000); /* 100 kHz */ |
paul@0 | 296 | |
paul@0 | 297 | printf("Trying DDC on I2C4...\n"); |
paul@0 | 298 | |
paul@0 | 299 | i2c_channel = jz4780_i2c_get_channel(i2c, 4); |
paul@0 | 300 | |
paul@0 | 301 | /* Attempt to read from address 0x50 for DDC. |
paul@0 | 302 | See: drivers/video/fbdev/core/fb_ddc.c */ |
paul@0 | 303 | |
paul@0 | 304 | jz4780_i2c_set_target(i2c_channel, 0x50); |
paul@0 | 305 | buf[0] = 0; |
paul@0 | 306 | jz4780_i2c_write(i2c_channel, buf, 1); |
paul@0 | 307 | jz4780_i2c_start_read(i2c_channel, buf, 128); |
paul@0 | 308 | |
paul@0 | 309 | printf("Waiting\n"); |
paul@0 | 310 | |
paul@0 | 311 | while (!jz4780_i2c_read_done(i2c_channel)) |
paul@0 | 312 | { |
paul@0 | 313 | if (jz4780_i2c_read_incomplete(i2c_channel)) |
paul@0 | 314 | { |
paul@0 | 315 | printf("Failed\n"); |
paul@0 | 316 | break; |
paul@0 | 317 | } |
paul@0 | 318 | |
paul@0 | 319 | tag = l4_irq_receive(irqcap, L4_IPC_NEVER); |
paul@0 | 320 | |
paul@0 | 321 | if ((err = l4_ipc_error(tag, l4_utcb()))) |
paul@0 | 322 | { |
paul@0 | 323 | printf("Error on IRQ receive: %ld\n", err); |
paul@0 | 324 | continue; |
paul@0 | 325 | } |
paul@0 | 326 | |
paul@0 | 327 | jz4780_i2c_read(i2c_channel); |
paul@0 | 328 | } |
paul@0 | 329 | |
paul@0 | 330 | pos = jz4780_i2c_have_read(i2c_channel); |
paul@0 | 331 | show_data(buf, pos); |
paul@0 | 332 | |
paul@0 | 333 | /* Attempt to decode EDID information. */ |
paul@0 | 334 | |
paul@0 | 335 | libedid_prefered_resolution(buf, &width, &height); |
paul@0 | 336 | printf("Preferred resolution: %d x %d\n", width, height); |
paul@0 | 337 | |
paul@0 | 338 | libedid_dump_standard_timings(buf); |
paul@0 | 339 | |
paul@0 | 340 | /* Detach from the interrupt. */ |
paul@0 | 341 | |
paul@0 | 342 | tag = l4_irq_detach(irqcap); |
paul@0 | 343 | |
paul@0 | 344 | if ((err = l4_error(tag))) |
paul@0 | 345 | printf("Error detaching from IRQ: %ld\n", err); |
paul@0 | 346 | |
paul@0 | 347 | return 0; |
paul@0 | 348 | } |