paul@218 | 1 | /* |
paul@218 | 2 | * Access various peripherals on the MIPS Creator CI20 board. |
paul@218 | 3 | * |
paul@218 | 4 | * Copyright (C) 2023 Paul Boddie <paul@boddie.org.uk> |
paul@218 | 5 | * |
paul@218 | 6 | * This program is free software; you can redistribute it and/or |
paul@218 | 7 | * modify it under the terms of the GNU General Public License as |
paul@218 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@218 | 9 | * the License, or (at your option) any later version. |
paul@218 | 10 | * |
paul@218 | 11 | * This program is distributed in the hope that it will be useful, |
paul@218 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@218 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@218 | 14 | * GNU General Public License for more details. |
paul@218 | 15 | * |
paul@218 | 16 | * You should have received a copy of the GNU General Public License |
paul@218 | 17 | * along with this program; if not, write to the Free Software |
paul@218 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@218 | 19 | * Boston, MA 02110-1301, USA |
paul@218 | 20 | */ |
paul@218 | 21 | |
paul@218 | 22 | #include "common.h" |
paul@218 | 23 | #include "defs.h" |
paul@218 | 24 | |
paul@218 | 25 | #include <l4/devices/dma.h> |
paul@218 | 26 | #include <l4/devices/memory.h> |
paul@218 | 27 | |
paul@218 | 28 | #include <l4/io/io.h> |
paul@218 | 29 | #include <l4/re/env.h> |
paul@218 | 30 | #include <l4/re/c/util/cap_alloc.h> |
paul@218 | 31 | #include <l4/sys/cache.h> |
paul@218 | 32 | #include <l4/sys/factory.h> |
paul@218 | 33 | #include <l4/sys/icu.h> |
paul@218 | 34 | #include <l4/sys/ipc.h> |
paul@218 | 35 | #include <l4/sys/irq.h> |
paul@218 | 36 | #include <l4/sys/rcv_endpoint.h> |
paul@218 | 37 | #include <l4/vbus/vbus.h> |
paul@218 | 38 | |
paul@218 | 39 | #include <stdio.h> |
paul@218 | 40 | #include <unistd.h> |
paul@218 | 41 | #include <stdint.h> |
paul@218 | 42 | #include <stdlib.h> |
paul@218 | 43 | #include <string.h> |
paul@218 | 44 | |
paul@218 | 45 | |
paul@218 | 46 | |
paul@218 | 47 | /* Common configuration. */ |
paul@218 | 48 | |
paul@218 | 49 | static l4_cap_idx_t icucap; |
paul@218 | 50 | |
paul@218 | 51 | |
paul@218 | 52 | |
paul@218 | 53 | /* Device and resource discovery. */ |
paul@218 | 54 | |
paul@218 | 55 | static long item_in_range(long start, long end, long index) |
paul@218 | 56 | { |
paul@218 | 57 | if (start < end) |
paul@218 | 58 | return start + index; |
paul@218 | 59 | else if (start > end) |
paul@218 | 60 | return start - index; |
paul@218 | 61 | else |
paul@218 | 62 | return start; |
paul@218 | 63 | } |
paul@218 | 64 | |
paul@218 | 65 | |
paul@218 | 66 | |
paul@218 | 67 | /* Common functions. */ |
paul@218 | 68 | |
paul@218 | 69 | static int init_irq(int num, l4_cap_idx_t irq, l4_uint32_t start, l4_uint32_t end) |
paul@218 | 70 | { |
paul@218 | 71 | /* Create interrupt object. */ |
paul@218 | 72 | |
paul@218 | 73 | long err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq)); |
paul@218 | 74 | |
paul@218 | 75 | if (err) |
paul@218 | 76 | { |
paul@218 | 77 | printf("Could not create IRQ object: %ld\n", err); |
paul@218 | 78 | return 1; |
paul@218 | 79 | } |
paul@218 | 80 | |
paul@218 | 81 | /* Bind interrupt objects to IRQ numbers. */ |
paul@218 | 82 | |
paul@218 | 83 | err = l4_error(l4_icu_bind(icucap, |
paul@218 | 84 | item_in_range(start, end, num), |
paul@218 | 85 | irq)); |
paul@218 | 86 | |
paul@218 | 87 | if (err) |
paul@218 | 88 | { |
paul@218 | 89 | printf("Could not bind IRQ to the ICU: %ld\n", err); |
paul@218 | 90 | return 1; |
paul@218 | 91 | } |
paul@218 | 92 | |
paul@218 | 93 | /* Attach ourselves to the interrupt handler. */ |
paul@218 | 94 | |
paul@218 | 95 | err = l4_error(l4_rcv_ep_bind_thread(irq, l4re_env()->main_thread, num)); |
paul@218 | 96 | |
paul@218 | 97 | if (err) |
paul@218 | 98 | { |
paul@218 | 99 | printf("Could not attach to IRQs: %ld\n", err); |
paul@218 | 100 | return 1; |
paul@218 | 101 | } |
paul@218 | 102 | |
paul@218 | 103 | return 0; |
paul@218 | 104 | } |
paul@218 | 105 | |
paul@218 | 106 | static const char *delimiters = " \n"; |
paul@218 | 107 | |
paul@218 | 108 | static char *read_token(const char *message) |
paul@218 | 109 | { |
paul@218 | 110 | char *token; |
paul@218 | 111 | |
paul@218 | 112 | if ((token = strtok(NULL, delimiters)) == NULL) |
paul@218 | 113 | { |
paul@218 | 114 | if (message != NULL) |
paul@218 | 115 | printf("%s?\n", message); |
paul@218 | 116 | } |
paul@218 | 117 | |
paul@218 | 118 | return token; |
paul@218 | 119 | } |
paul@218 | 120 | |
paul@218 | 121 | static int read_increment(const char *message, int *increment) |
paul@218 | 122 | { |
paul@218 | 123 | char *token = read_token(message); |
paul@218 | 124 | |
paul@218 | 125 | if (token == NULL) |
paul@218 | 126 | return 0; |
paul@218 | 127 | |
paul@218 | 128 | *increment = !strncmp(token, "i", 1); |
paul@218 | 129 | return 1; |
paul@218 | 130 | } |
paul@218 | 131 | |
paul@218 | 132 | static int read_encoded_number(const char *message, const char *format, unsigned int *num) |
paul@218 | 133 | { |
paul@218 | 134 | char *token = read_token(message); |
paul@218 | 135 | |
paul@218 | 136 | if (token == NULL) |
paul@218 | 137 | return 0; |
paul@218 | 138 | |
paul@218 | 139 | sscanf(token, format, num); |
paul@218 | 140 | return 1; |
paul@218 | 141 | } |
paul@218 | 142 | |
paul@218 | 143 | static int read_number(const char *message, unsigned int *num) |
paul@218 | 144 | { |
paul@218 | 145 | char *token = read_token(message); |
paul@218 | 146 | |
paul@218 | 147 | if (token == NULL) |
paul@218 | 148 | return 0; |
paul@218 | 149 | |
paul@218 | 150 | if (!strncmp(token, "0x", 2) || !strncmp(token, "0X", 2)) |
paul@218 | 151 | sscanf(token, "%x", num); |
paul@218 | 152 | else |
paul@218 | 153 | *num = atoi(token); |
paul@218 | 154 | |
paul@218 | 155 | return 1; |
paul@218 | 156 | } |
paul@218 | 157 | |
paul@218 | 158 | static int get_resource_number(const char *type, unsigned int available) |
paul@218 | 159 | { |
paul@218 | 160 | unsigned int num; |
paul@218 | 161 | |
paul@218 | 162 | if (!read_number(type, &num)) |
paul@218 | 163 | return -1; |
paul@218 | 164 | |
paul@218 | 165 | if (num >= available) |
paul@218 | 166 | { |
paul@218 | 167 | printf("%s number out of range.\n", type); |
paul@218 | 168 | return -1; |
paul@218 | 169 | } |
paul@218 | 170 | |
paul@218 | 171 | return num; |
paul@218 | 172 | } |
paul@218 | 173 | |
paul@218 | 174 | static int get_channel_number(unsigned int available) |
paul@218 | 175 | { |
paul@218 | 176 | return get_resource_number("Channel", available); |
paul@218 | 177 | } |
paul@218 | 178 | |
paul@218 | 179 | static int get_region_number(unsigned int available) |
paul@218 | 180 | { |
paul@218 | 181 | return get_resource_number("Region", available); |
paul@218 | 182 | } |
paul@218 | 183 | |
paul@218 | 184 | static void *get_channel(int num_channels, void *channels[], int *num) |
paul@218 | 185 | { |
paul@218 | 186 | int n = get_channel_number(num_channels); |
paul@218 | 187 | void *channel; |
paul@218 | 188 | |
paul@218 | 189 | if (n < 0) |
paul@218 | 190 | return NULL; |
paul@218 | 191 | |
paul@218 | 192 | channel = channels[n]; |
paul@218 | 193 | |
paul@218 | 194 | if (channel == NULL) |
paul@218 | 195 | printf("Channel needs creating.\n"); |
paul@218 | 196 | |
paul@218 | 197 | if (num != NULL) |
paul@218 | 198 | *num = n; |
paul@218 | 199 | |
paul@218 | 200 | return channel; |
paul@218 | 201 | } |
paul@218 | 202 | |
paul@218 | 203 | static void list_channels(int num_channels, void *channels[]) |
paul@218 | 204 | { |
paul@218 | 205 | int num; |
paul@218 | 206 | void *channel; |
paul@218 | 207 | |
paul@218 | 208 | for (num = 0; num < num_channels; num++) |
paul@218 | 209 | { |
paul@218 | 210 | printf("Channel %d: ", num); |
paul@218 | 211 | |
paul@218 | 212 | channel = channels[num]; |
paul@218 | 213 | |
paul@218 | 214 | if (channel == NULL) |
paul@218 | 215 | printf("(inactive)\n"); |
paul@218 | 216 | else |
paul@218 | 217 | printf("active\n"); |
paul@218 | 218 | } |
paul@218 | 219 | } |
paul@218 | 220 | |
paul@218 | 221 | |
paul@218 | 222 | |
paul@218 | 223 | /* AIC/I2S configuration. */ |
paul@218 | 224 | |
paul@218 | 225 | static l4_uint32_t aic_irq_start = 0, aic_irq_end = 0; |
paul@218 | 226 | |
paul@218 | 227 | /* AIC/I2S operations. */ |
paul@218 | 228 | |
paul@218 | 229 | static void new_aic_channel(void *aic) |
paul@218 | 230 | { |
paul@218 | 231 | l4_cap_idx_t irqcap; |
paul@218 | 232 | int num = get_channel_number(num_aic_channels); |
paul@218 | 233 | void *channel; |
paul@218 | 234 | |
paul@218 | 235 | if (num < 0) |
paul@218 | 236 | return; |
paul@218 | 237 | |
paul@218 | 238 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 239 | |
paul@218 | 240 | if (channel == NULL) |
paul@218 | 241 | return; |
paul@218 | 242 | |
paul@218 | 243 | irqcap = l4re_util_cap_alloc(); |
paul@218 | 244 | |
paul@218 | 245 | if (init_irq(num, irqcap, aic_irq_start, aic_irq_end)) |
paul@218 | 246 | return; |
paul@218 | 247 | |
paul@218 | 248 | aic_channels[num] = aic_get_channel(aic, num, channel); |
paul@218 | 249 | aic_irqs[num] = irqcap; |
paul@218 | 250 | } |
paul@218 | 251 | |
paul@218 | 252 | static void set_aic_buffer(void) |
paul@218 | 253 | { |
paul@218 | 254 | char *token; |
paul@218 | 255 | FILE *fp; |
paul@218 | 256 | void *channel = get_channel(num_aic_channels, aic_channels, NULL); |
paul@218 | 257 | unsigned int size; |
paul@218 | 258 | l4_addr_t vaddr; |
paul@218 | 259 | |
paul@218 | 260 | if (channel == NULL) |
paul@218 | 261 | return; |
paul@218 | 262 | |
paul@218 | 263 | if (!read_number("Buffer size", &size)) |
paul@218 | 264 | return; |
paul@218 | 265 | |
paul@218 | 266 | if (aic_get_buffer(channel, size, &vaddr)) |
paul@218 | 267 | { |
paul@218 | 268 | printf("Could not get buffer.\n"); |
paul@218 | 269 | return; |
paul@218 | 270 | } |
paul@218 | 271 | |
paul@218 | 272 | memset((void *) vaddr, 0, size); |
paul@218 | 273 | |
paul@218 | 274 | if ((token = read_token("Filename")) == NULL) |
paul@218 | 275 | return; |
paul@218 | 276 | |
paul@218 | 277 | /* Populate the region from the file. */ |
paul@218 | 278 | |
paul@218 | 279 | fp = fopen(token, "r"); |
paul@218 | 280 | |
paul@218 | 281 | if (fp == NULL) |
paul@218 | 282 | { |
paul@218 | 283 | printf("File not readable.\n"); |
paul@218 | 284 | return; |
paul@218 | 285 | } |
paul@218 | 286 | |
paul@218 | 287 | fread((char *) vaddr, sizeof(char), size, fp); |
paul@218 | 288 | fclose(fp); |
paul@218 | 289 | |
paul@218 | 290 | l4_cache_flush_data(vaddr, vaddr + size); |
paul@218 | 291 | } |
paul@218 | 292 | |
paul@218 | 293 | static void aic_transfer_data(void) |
paul@218 | 294 | { |
paul@218 | 295 | void *channel = get_channel(num_aic_channels, aic_channels, NULL); |
paul@218 | 296 | unsigned int count, rate, resolution, transferred; |
paul@218 | 297 | |
paul@218 | 298 | if (channel == NULL) |
paul@218 | 299 | return; |
paul@218 | 300 | |
paul@218 | 301 | if (!read_number("Sample size", &count)) |
paul@218 | 302 | return; |
paul@218 | 303 | |
paul@218 | 304 | if (!read_number("Sample rate", &rate)) |
paul@218 | 305 | return; |
paul@218 | 306 | |
paul@218 | 307 | if (!read_number("Sample resolution", &resolution)) |
paul@218 | 308 | return; |
paul@218 | 309 | |
paul@218 | 310 | transferred = aic_transfer(channel, count, rate, resolution); |
paul@218 | 311 | |
paul@218 | 312 | printf("Transferred: %d\n", transferred); |
paul@218 | 313 | } |
paul@218 | 314 | |
paul@218 | 315 | |
paul@218 | 316 | |
paul@218 | 317 | /* CPM operations. */ |
paul@218 | 318 | |
paul@218 | 319 | static const char *clock_id(enum Clock_identifiers clock) |
paul@218 | 320 | { |
paul@218 | 321 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 322 | if (clock == clocks[i].clock) |
paul@218 | 323 | return clocks[i].id; |
paul@218 | 324 | |
paul@218 | 325 | return NULL; |
paul@218 | 326 | } |
paul@218 | 327 | |
paul@218 | 328 | static enum Clock_identifiers get_clock(char *id) |
paul@218 | 329 | { |
paul@218 | 330 | if (id != NULL) |
paul@218 | 331 | { |
paul@218 | 332 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 333 | if (!strcmp(id, clocks[i].id)) |
paul@218 | 334 | return clocks[i].clock; |
paul@218 | 335 | } |
paul@218 | 336 | |
paul@218 | 337 | return Clock_undefined; |
paul@218 | 338 | } |
paul@218 | 339 | |
paul@218 | 340 | static const char list_clocks_separator[] = "--------------------------------"; |
paul@218 | 341 | |
paul@218 | 342 | #define sep(x) (list_clocks_separator + sizeof(list_clocks_separator) - 1 - x) |
paul@218 | 343 | |
paul@218 | 344 | static void list_clocks(void *cpm) |
paul@218 | 345 | { |
paul@218 | 346 | /* Read information from the clock and power management unit. */ |
paul@218 | 347 | |
paul@218 | 348 | printf("%-5s %-10s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" |
paul@218 | 349 | "%-5s %-10s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", |
paul@218 | 350 | "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", |
paul@218 | 351 | "Parameters", "Frequency", "On", |
paul@218 | 352 | sep(5), sep(10), sep(7), sep(6), sep(12), sep(12), |
paul@218 | 353 | sep(24), sep(12), sep(3)); |
paul@218 | 354 | |
paul@218 | 355 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 356 | { |
paul@218 | 357 | uint32_t parameters[4]; |
paul@218 | 358 | char parameter_str[25] = {0}; |
paul@218 | 359 | const char *source_id = clock_id(cpm_get_source_clock(cpm, clocks[i].clock)); |
paul@218 | 360 | int num_parameters = cpm_get_parameters(cpm, clocks[i].clock, parameters); |
paul@218 | 361 | |
paul@218 | 362 | for (int p = 0, pos = 0; p < num_parameters; p++) |
paul@218 | 363 | { |
paul@218 | 364 | int result = sprintf(parameter_str + pos, "%-7d ", parameters[p]); |
paul@218 | 365 | if (result < 0) |
paul@218 | 366 | break; |
paul@218 | 367 | pos += result; |
paul@218 | 368 | } |
paul@218 | 369 | |
paul@218 | 370 | printf("%-5s %-10s %-7s %-6d %-12s %-12lld %-24s %-12lld %-3s\n", |
paul@218 | 371 | clocks[i].id, |
paul@218 | 372 | clocks[i].name, |
paul@218 | 373 | cpm_clock_type(cpm, clocks[i].clock), |
paul@218 | 374 | cpm_get_source(cpm, clocks[i].clock), |
paul@218 | 375 | source_id != NULL ? source_id : "...", |
paul@218 | 376 | cpm_get_source_frequency(cpm, clocks[i].clock), |
paul@218 | 377 | parameter_str, |
paul@218 | 378 | cpm_get_frequency(cpm, clocks[i].clock), |
paul@218 | 379 | cpm_have_clock(cpm, clocks[i].clock) ? "on" : "off"); |
paul@218 | 380 | } |
paul@218 | 381 | } |
paul@218 | 382 | |
paul@218 | 383 | static enum Clock_identifiers get_clock_by_name(void) |
paul@218 | 384 | { |
paul@218 | 385 | char *token; |
paul@218 | 386 | enum Clock_identifiers clock; |
paul@218 | 387 | |
paul@218 | 388 | if ((token = read_token("Clock")) == NULL) |
paul@218 | 389 | return Clock_undefined; |
paul@218 | 390 | |
paul@218 | 391 | clock = get_clock(token); |
paul@218 | 392 | |
paul@218 | 393 | if (clock == Clock_undefined) |
paul@218 | 394 | { |
paul@218 | 395 | printf("Unrecognised clock: %s\n", token); |
paul@218 | 396 | return Clock_undefined; |
paul@218 | 397 | } |
paul@218 | 398 | |
paul@218 | 399 | return clock; |
paul@218 | 400 | } |
paul@218 | 401 | |
paul@218 | 402 | static void set_frequency(void *cpm) |
paul@218 | 403 | { |
paul@218 | 404 | enum Clock_identifiers clock; |
paul@218 | 405 | unsigned int frequency; |
paul@218 | 406 | |
paul@218 | 407 | clock = get_clock_by_name(); |
paul@218 | 408 | |
paul@218 | 409 | if (clock == Clock_undefined) |
paul@218 | 410 | return; |
paul@218 | 411 | |
paul@218 | 412 | if (!read_number("Frequency", &frequency)) |
paul@218 | 413 | return; |
paul@218 | 414 | |
paul@218 | 415 | if (!cpm_set_frequency(cpm, clock, frequency)) |
paul@218 | 416 | printf("Frequency not set.\n"); |
paul@218 | 417 | } |
paul@218 | 418 | |
paul@218 | 419 | static void set_parameters(void *cpm) |
paul@218 | 420 | { |
paul@218 | 421 | char *token; |
paul@218 | 422 | enum Clock_identifiers clock; |
paul@218 | 423 | uint32_t parameters[4]; |
paul@218 | 424 | int num_parameters; |
paul@218 | 425 | |
paul@218 | 426 | clock = get_clock_by_name(); |
paul@218 | 427 | |
paul@218 | 428 | if (clock == Clock_undefined) |
paul@218 | 429 | return; |
paul@218 | 430 | |
paul@218 | 431 | for (num_parameters = 0; num_parameters < 4; num_parameters++) |
paul@218 | 432 | { |
paul@218 | 433 | if ((token = read_token(NULL)) == NULL) |
paul@218 | 434 | break; |
paul@218 | 435 | |
paul@218 | 436 | parameters[num_parameters] = atoi(token); |
paul@218 | 437 | } |
paul@218 | 438 | |
paul@218 | 439 | if (!num_parameters) |
paul@218 | 440 | { |
paul@218 | 441 | printf("Parameters?\n"); |
paul@218 | 442 | return; |
paul@218 | 443 | } |
paul@218 | 444 | |
paul@218 | 445 | if (!cpm_set_parameters(cpm, clock, num_parameters, parameters)) |
paul@218 | 446 | printf("Too few or invalid parameters.\n"); |
paul@218 | 447 | } |
paul@218 | 448 | |
paul@218 | 449 | static void set_source(void *cpm) |
paul@218 | 450 | { |
paul@218 | 451 | enum Clock_identifiers clock; |
paul@218 | 452 | unsigned int source; |
paul@218 | 453 | |
paul@218 | 454 | clock = get_clock_by_name(); |
paul@218 | 455 | |
paul@218 | 456 | if (clock == Clock_undefined) |
paul@218 | 457 | return; |
paul@218 | 458 | |
paul@218 | 459 | if (!read_number("Source", &source)) |
paul@218 | 460 | return; |
paul@218 | 461 | |
paul@218 | 462 | cpm_set_source(cpm, clock, source); |
paul@218 | 463 | } |
paul@218 | 464 | |
paul@218 | 465 | static void start_clock(void *cpm) |
paul@218 | 466 | { |
paul@218 | 467 | enum Clock_identifiers clock; |
paul@218 | 468 | |
paul@218 | 469 | clock = get_clock_by_name(); |
paul@218 | 470 | |
paul@218 | 471 | if (clock == Clock_undefined) |
paul@218 | 472 | return; |
paul@218 | 473 | |
paul@218 | 474 | cpm_start_clock(cpm, clock); |
paul@218 | 475 | } |
paul@218 | 476 | |
paul@218 | 477 | static void stop_clock(void *cpm) |
paul@218 | 478 | { |
paul@218 | 479 | enum Clock_identifiers clock; |
paul@218 | 480 | |
paul@218 | 481 | clock = get_clock_by_name(); |
paul@218 | 482 | |
paul@218 | 483 | if (clock == Clock_undefined) |
paul@218 | 484 | return; |
paul@218 | 485 | |
paul@218 | 486 | cpm_stop_clock(cpm, clock); |
paul@218 | 487 | } |
paul@218 | 488 | |
paul@218 | 489 | |
paul@218 | 490 | |
paul@218 | 491 | /* DMA configuration. */ |
paul@218 | 492 | |
paul@218 | 493 | static l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; |
paul@218 | 494 | |
paul@218 | 495 | /* DMA operations. */ |
paul@218 | 496 | |
paul@218 | 497 | static int init_dma(void) |
paul@218 | 498 | { |
paul@218 | 499 | unsigned int i; |
paul@218 | 500 | |
paul@218 | 501 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 502 | dma_regions[i].mem = L4_INVALID_CAP; |
paul@218 | 503 | |
paul@218 | 504 | /* Here, only one IRQ is used. */ |
paul@218 | 505 | |
paul@218 | 506 | dma_irq = l4re_util_cap_alloc(); |
paul@218 | 507 | return init_irq(0, dma_irq, dma_irq_start, dma_irq_end); |
paul@218 | 508 | } |
paul@218 | 509 | |
paul@218 | 510 | static struct dma_region *_get_dma_region(void) |
paul@218 | 511 | { |
paul@218 | 512 | int num = get_region_number(num_dma_regions); |
paul@218 | 513 | |
paul@218 | 514 | if (num < 0) |
paul@218 | 515 | return NULL; |
paul@218 | 516 | |
paul@218 | 517 | return &dma_regions[num]; |
paul@218 | 518 | } |
paul@218 | 519 | |
paul@218 | 520 | static void list_dma_regions(void) |
paul@218 | 521 | { |
paul@218 | 522 | unsigned int num; |
paul@218 | 523 | struct dma_region *region; |
paul@218 | 524 | unsigned int i; |
paul@218 | 525 | |
paul@218 | 526 | for (num = 0; num < num_dma_regions; num++) |
paul@218 | 527 | { |
paul@218 | 528 | printf("Region %d: ", num); |
paul@218 | 529 | |
paul@218 | 530 | region = &dma_regions[num]; |
paul@218 | 531 | |
paul@218 | 532 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 533 | printf("(inactive)\n"); |
paul@218 | 534 | else |
paul@218 | 535 | { |
paul@218 | 536 | printf("size = %d; align = %d; virtual = 0x%lx; physical = 0x%llx\ndata =", |
paul@218 | 537 | region->size, region->align, region->vaddr, region->paddr); |
paul@218 | 538 | |
paul@218 | 539 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@218 | 540 | printf(" %02x", *((uint8_t *) region->vaddr + i)); |
paul@218 | 541 | |
paul@218 | 542 | printf("\n"); |
paul@218 | 543 | } |
paul@218 | 544 | } |
paul@218 | 545 | } |
paul@218 | 546 | |
paul@218 | 547 | static void new_dma_channel(void *dma) |
paul@218 | 548 | { |
paul@218 | 549 | int num = get_channel_number(num_dma_channels); |
paul@218 | 550 | |
paul@218 | 551 | if (num < 0) |
paul@218 | 552 | return; |
paul@218 | 553 | |
paul@218 | 554 | if (dma_channels[num] != NULL) |
paul@218 | 555 | { |
paul@218 | 556 | printf("Channel already defined.\n"); |
paul@218 | 557 | return; |
paul@218 | 558 | } |
paul@218 | 559 | |
paul@218 | 560 | dma_channels[num] = dma_get_channel(dma, num, dma_irq); |
paul@218 | 561 | } |
paul@218 | 562 | |
paul@218 | 563 | static void new_dma_region(void) |
paul@218 | 564 | { |
paul@218 | 565 | struct dma_region *region = _get_dma_region(); |
paul@218 | 566 | |
paul@218 | 567 | if (region == NULL) |
paul@218 | 568 | { |
paul@218 | 569 | list_dma_regions(); |
paul@218 | 570 | return; |
paul@218 | 571 | } |
paul@218 | 572 | |
paul@218 | 573 | if (l4_is_valid_cap(region->mem)) |
paul@218 | 574 | { |
paul@218 | 575 | printf("Region already defined.\n"); |
paul@218 | 576 | return; |
paul@218 | 577 | } |
paul@218 | 578 | |
paul@218 | 579 | if (!read_number("Size", ®ion->size)) |
paul@218 | 580 | return; |
paul@218 | 581 | |
paul@218 | 582 | if (!read_number("Alignment", ®ion->align)) |
paul@218 | 583 | return; |
paul@218 | 584 | |
paul@218 | 585 | if (get_dma_region(region->size, region->align, ®ion->vaddr, ®ion->paddr, ®ion->mem)) |
paul@218 | 586 | printf("Could not allocate region.\n"); |
paul@218 | 587 | } |
paul@218 | 588 | |
paul@218 | 589 | static void set_dma_region(void) |
paul@218 | 590 | { |
paul@218 | 591 | char *token; |
paul@218 | 592 | struct dma_region *region = _get_dma_region(); |
paul@218 | 593 | FILE *fp; |
paul@218 | 594 | |
paul@218 | 595 | if (region == NULL) |
paul@218 | 596 | { |
paul@218 | 597 | list_dma_regions(); |
paul@218 | 598 | return; |
paul@218 | 599 | } |
paul@218 | 600 | |
paul@218 | 601 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 602 | { |
paul@218 | 603 | printf("Region needs creating.\n"); |
paul@218 | 604 | return; |
paul@218 | 605 | } |
paul@218 | 606 | |
paul@218 | 607 | memset((void *) region->vaddr, 0, region->size); |
paul@218 | 608 | |
paul@218 | 609 | if ((token = read_token("Filename")) == NULL) |
paul@218 | 610 | return; |
paul@218 | 611 | |
paul@218 | 612 | /* Populate the region from the file. */ |
paul@218 | 613 | |
paul@218 | 614 | fp = fopen(token, "r"); |
paul@218 | 615 | |
paul@218 | 616 | if (fp == NULL) |
paul@218 | 617 | { |
paul@218 | 618 | printf("File not readable.\n"); |
paul@218 | 619 | return; |
paul@218 | 620 | } |
paul@218 | 621 | |
paul@218 | 622 | fread((char *) region->vaddr, sizeof(char), region->size, fp); |
paul@218 | 623 | fclose(fp); |
paul@218 | 624 | |
paul@218 | 625 | l4_cache_flush_data(region->vaddr, region->vaddr + region->size); |
paul@218 | 626 | } |
paul@218 | 627 | |
paul@218 | 628 | static void dma_transfer_data(void) |
paul@218 | 629 | { |
paul@218 | 630 | unsigned int count, to_transfer, transferred; |
paul@218 | 631 | unsigned int source_address, destination_address, source_width, destination_width, |
paul@218 | 632 | transfer_unit_size, request_type; |
paul@218 | 633 | int source_increment, destination_increment; |
paul@218 | 634 | void *channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 635 | unsigned int i; |
paul@218 | 636 | |
paul@218 | 637 | if (channel == NULL) |
paul@218 | 638 | return; |
paul@218 | 639 | |
paul@218 | 640 | if (!read_number("Source address", &source_address)) |
paul@218 | 641 | return; |
paul@218 | 642 | |
paul@218 | 643 | if (!read_number("Destination address", &destination_address)) |
paul@218 | 644 | return; |
paul@218 | 645 | |
paul@218 | 646 | if (!read_number("Transfer size", &count)) |
paul@218 | 647 | return; |
paul@218 | 648 | |
paul@218 | 649 | if (!read_increment("Source increment", &source_increment)) |
paul@218 | 650 | return; |
paul@218 | 651 | |
paul@218 | 652 | if (!read_increment("Destination increment", &destination_increment)) |
paul@218 | 653 | return; |
paul@218 | 654 | |
paul@218 | 655 | if (!read_number("Source width", &source_width)) |
paul@218 | 656 | return; |
paul@218 | 657 | |
paul@218 | 658 | if (!read_number("Destination width", &destination_width)) |
paul@218 | 659 | return; |
paul@218 | 660 | |
paul@218 | 661 | if (!read_number("Transfer unit size", &transfer_unit_size)) |
paul@218 | 662 | return; |
paul@218 | 663 | |
paul@218 | 664 | if (!read_number("Request type", &request_type)) |
paul@218 | 665 | return; |
paul@218 | 666 | |
paul@218 | 667 | /* Invalidate DMA region contents in case of transfers between regions. */ |
paul@218 | 668 | |
paul@218 | 669 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 670 | l4_cache_inv_data(dma_regions[i].vaddr, dma_regions[i].vaddr + dma_regions[i].size); |
paul@218 | 671 | |
paul@218 | 672 | to_transfer = dma_transfer(channel, source_address, destination_address, |
paul@218 | 673 | count, |
paul@218 | 674 | source_increment, destination_increment, |
paul@218 | 675 | source_width, destination_width, |
paul@218 | 676 | transfer_unit_size, |
paul@218 | 677 | request_type); |
paul@218 | 678 | |
paul@218 | 679 | transferred = to_transfer ? count - dma_wait(channel) : 0; |
paul@218 | 680 | |
paul@218 | 681 | printf("Transferred: %d\n", transferred); |
paul@218 | 682 | } |
paul@218 | 683 | |
paul@218 | 684 | |
paul@218 | 685 | |
paul@218 | 686 | /* GPIO operations. */ |
paul@218 | 687 | |
paul@218 | 688 | static char get_gpio_dir_label(unsigned int value, unsigned int level) |
paul@218 | 689 | { |
paul@218 | 690 | return value == Fix_input ? (level ? 'I' : 'i') : (level ? 'O' : 'o'); |
paul@218 | 691 | } |
paul@218 | 692 | |
paul@218 | 693 | static char get_gpio_irq_label(unsigned int value) |
paul@218 | 694 | { |
paul@218 | 695 | switch (value) |
paul@218 | 696 | { |
paul@218 | 697 | case L4_IRQ_F_NEG_EDGE: return 'f'; |
paul@218 | 698 | case L4_IRQ_F_POS_EDGE: return 'r'; |
paul@218 | 699 | case L4_IRQ_F_LEVEL_LOW: return 'l'; |
paul@218 | 700 | case L4_IRQ_F_LEVEL_HIGH: return 'h'; |
paul@218 | 701 | default: return '?'; |
paul@218 | 702 | } |
paul@218 | 703 | } |
paul@218 | 704 | |
paul@218 | 705 | static void list_gpios(void *gpio[]) |
paul@218 | 706 | { |
paul@218 | 707 | unsigned int port, pin; |
paul@218 | 708 | unsigned int func, value, level; |
paul@218 | 709 | |
paul@218 | 710 | printf("gpio: I = input high; i = input low; O = output high; o = output low\n" |
paul@218 | 711 | "irq: h = high level; l = low level; r = rising edge; f = falling edge\n" |
paul@218 | 712 | "alt: function number\n\n"); |
paul@218 | 713 | |
paul@218 | 714 | /* Show pin numbering. */ |
paul@218 | 715 | |
paul@218 | 716 | printf("Port/Pin "); |
paul@218 | 717 | |
paul@218 | 718 | for (pin = 0; pin < 32; pin++) |
paul@218 | 719 | { |
paul@218 | 720 | if (!(pin % 10)) |
paul@218 | 721 | printf(" %d", pin); |
paul@218 | 722 | else |
paul@218 | 723 | printf(" %d", pin % 10); |
paul@218 | 724 | } |
paul@218 | 725 | |
paul@218 | 726 | printf("\n"); |
paul@218 | 727 | |
paul@218 | 728 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 729 | { |
paul@218 | 730 | /* Show port and pin configuration. */ |
paul@218 | 731 | |
paul@218 | 732 | printf("%c ", gpio_port_labels[port]); |
paul@218 | 733 | |
paul@218 | 734 | for (pin = 0; pin < 32; pin++) |
paul@218 | 735 | { |
paul@218 | 736 | /* Pad below the first pin digit for multiples of ten other than zero. */ |
paul@218 | 737 | |
paul@218 | 738 | if (pin && !(pin % 10)) |
paul@218 | 739 | printf(" "); |
paul@218 | 740 | |
paul@218 | 741 | gpio_config_pad_get(gpio[port], pin, &func, &value); |
paul@218 | 742 | |
paul@218 | 743 | switch (func) |
paul@218 | 744 | { |
paul@218 | 745 | case Function_alt: printf(" %d", value); break; |
paul@218 | 746 | case Function_irq: printf(" %c", get_gpio_irq_label(value)); break; |
paul@218 | 747 | case Function_gpio: |
paul@218 | 748 | { |
paul@218 | 749 | level = gpio_get(gpio[port], pin); |
paul@218 | 750 | printf(" %c", get_gpio_dir_label(value, level)); |
paul@218 | 751 | break; |
paul@218 | 752 | } |
paul@218 | 753 | default: printf(" ?"); break; |
paul@218 | 754 | } |
paul@218 | 755 | } |
paul@218 | 756 | |
paul@218 | 757 | printf("\n"); |
paul@218 | 758 | } |
paul@218 | 759 | } |
paul@218 | 760 | |
paul@218 | 761 | static int get_port_and_pin(unsigned int *port, unsigned int *pin) |
paul@218 | 762 | { |
paul@218 | 763 | char *token; |
paul@218 | 764 | |
paul@218 | 765 | if ((token = read_token("Port")) == NULL) |
paul@218 | 766 | return 0; |
paul@218 | 767 | |
paul@218 | 768 | if ((token[0] < 'A') || ((unsigned int) token[0] - (unsigned int) 'A' >= num_gpio_ports)) |
paul@218 | 769 | { |
paul@218 | 770 | printf("Bad port: %c\n", token[0]); |
paul@218 | 771 | return 0; |
paul@218 | 772 | } |
paul@218 | 773 | |
paul@218 | 774 | *port = (unsigned int) token[0] - (unsigned int) 'A'; |
paul@218 | 775 | |
paul@218 | 776 | if (!read_number("Pin", pin)) |
paul@218 | 777 | return 0; |
paul@218 | 778 | |
paul@218 | 779 | return 1; |
paul@218 | 780 | } |
paul@218 | 781 | |
paul@218 | 782 | static void set_gpio_alt_func(void *gpio[]) |
paul@218 | 783 | { |
paul@218 | 784 | unsigned int port, pin, value; |
paul@218 | 785 | |
paul@218 | 786 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 787 | return; |
paul@218 | 788 | |
paul@218 | 789 | if (!read_number("Function", &value)) |
paul@218 | 790 | return; |
paul@218 | 791 | |
paul@218 | 792 | gpio_config_pad(gpio[port], pin, Function_alt, value); |
paul@218 | 793 | } |
paul@218 | 794 | |
paul@218 | 795 | static void set_gpio_pad(void *gpio[]) |
paul@218 | 796 | { |
paul@218 | 797 | char *token; |
paul@218 | 798 | unsigned int port, pin, mode, value = 0; |
paul@218 | 799 | |
paul@218 | 800 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 801 | return; |
paul@218 | 802 | |
paul@218 | 803 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 804 | return; |
paul@218 | 805 | |
paul@218 | 806 | if (!strcmp(token, "in")) |
paul@218 | 807 | mode = Fix_input; |
paul@218 | 808 | else if (!strcmp(token, "irq")) |
paul@218 | 809 | mode = Fix_irq; |
paul@218 | 810 | else if (!strcmp(token, "out")) |
paul@218 | 811 | { |
paul@218 | 812 | mode = Fix_output; |
paul@218 | 813 | |
paul@218 | 814 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 815 | value = atoi(token); |
paul@218 | 816 | } |
paul@218 | 817 | else |
paul@218 | 818 | { |
paul@218 | 819 | printf("Mode not recognised.\n"); |
paul@218 | 820 | return; |
paul@218 | 821 | } |
paul@218 | 822 | |
paul@218 | 823 | gpio_setup(gpio[port], pin, mode, value); |
paul@218 | 824 | } |
paul@218 | 825 | |
paul@218 | 826 | static void set_gpio_pull(void *gpio[]) |
paul@218 | 827 | { |
paul@218 | 828 | char *token; |
paul@218 | 829 | unsigned int port, pin, mode; |
paul@218 | 830 | |
paul@218 | 831 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 832 | return; |
paul@218 | 833 | |
paul@218 | 834 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 835 | return; |
paul@218 | 836 | |
paul@218 | 837 | if (!strcmp(token, "down")) |
paul@218 | 838 | mode = Pull_down; |
paul@218 | 839 | else if (!strcmp(token, "none")) |
paul@218 | 840 | mode = Pull_none; |
paul@218 | 841 | else if (!strcmp(token, "up")) |
paul@218 | 842 | mode = Pull_up; |
paul@218 | 843 | else |
paul@218 | 844 | { |
paul@218 | 845 | printf("Mode not recognised.\n"); |
paul@218 | 846 | return; |
paul@218 | 847 | } |
paul@218 | 848 | |
paul@218 | 849 | gpio_config_pull(gpio[port], pin, mode); |
paul@218 | 850 | } |
paul@218 | 851 | |
paul@218 | 852 | |
paul@218 | 853 | |
paul@218 | 854 | /* I2C configuration. */ |
paul@218 | 855 | |
paul@218 | 856 | static l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; |
paul@218 | 857 | |
paul@218 | 858 | /* I2C operations. */ |
paul@218 | 859 | |
paul@218 | 860 | static int _i2c_read(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 861 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 862 | { |
paul@218 | 863 | l4_msgtag_t tag; |
paul@218 | 864 | |
paul@218 | 865 | i2c_start_read(channel, buf, length, stop); |
paul@218 | 866 | |
paul@218 | 867 | while (!i2c_read_done(channel)) |
paul@218 | 868 | { |
paul@218 | 869 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 870 | |
paul@218 | 871 | // NOTE: Error not returned. |
paul@218 | 872 | |
paul@218 | 873 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 874 | break; |
paul@218 | 875 | |
paul@218 | 876 | if (i2c_failed(channel)) |
paul@218 | 877 | break; |
paul@218 | 878 | |
paul@218 | 879 | i2c_read(channel); |
paul@218 | 880 | } |
paul@218 | 881 | |
paul@218 | 882 | if (stop) |
paul@218 | 883 | i2c_stop(channel); |
paul@218 | 884 | |
paul@218 | 885 | return i2c_have_read(channel); |
paul@218 | 886 | } |
paul@218 | 887 | |
paul@218 | 888 | static int _i2c_write(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 889 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 890 | { |
paul@218 | 891 | l4_msgtag_t tag; |
paul@218 | 892 | |
paul@218 | 893 | i2c_start_write(channel, buf, length, stop); |
paul@218 | 894 | |
paul@218 | 895 | while (!i2c_write_done(channel)) |
paul@218 | 896 | { |
paul@218 | 897 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 898 | |
paul@218 | 899 | // NOTE: Error not returned. |
paul@218 | 900 | |
paul@218 | 901 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 902 | break; |
paul@218 | 903 | |
paul@218 | 904 | if (i2c_failed(channel)) |
paul@218 | 905 | break; |
paul@218 | 906 | |
paul@218 | 907 | i2c_write(channel); |
paul@218 | 908 | } |
paul@218 | 909 | |
paul@218 | 910 | if (stop) |
paul@218 | 911 | i2c_stop(channel); |
paul@218 | 912 | |
paul@218 | 913 | return i2c_have_written(channel); |
paul@218 | 914 | } |
paul@218 | 915 | |
paul@218 | 916 | static void list_i2c_channels(void) |
paul@218 | 917 | { |
paul@218 | 918 | unsigned int num; |
paul@218 | 919 | void *channel; |
paul@218 | 920 | |
paul@218 | 921 | printf("i2c: channel\n\n"); |
paul@218 | 922 | |
paul@218 | 923 | for (num = 0; num < num_i2c_channels; num++) |
paul@218 | 924 | { |
paul@218 | 925 | printf("Channel %d: ", num); |
paul@218 | 926 | |
paul@218 | 927 | channel = i2c_channels[num]; |
paul@218 | 928 | |
paul@218 | 929 | if (channel == NULL) |
paul@218 | 930 | printf("(inactive)\n"); |
paul@218 | 931 | else |
paul@218 | 932 | printf("%d Hz\n", i2c_get_frequency(channel)); |
paul@218 | 933 | } |
paul@218 | 934 | } |
paul@218 | 935 | |
paul@218 | 936 | static void new_i2c_channel(void *i2c) |
paul@218 | 937 | { |
paul@218 | 938 | l4_cap_idx_t irqcap; |
paul@218 | 939 | int num = get_channel_number(num_i2c_channels); |
paul@218 | 940 | |
paul@218 | 941 | if (num < 0) |
paul@218 | 942 | return; |
paul@218 | 943 | |
paul@218 | 944 | irqcap = l4re_util_cap_alloc(); |
paul@218 | 945 | |
paul@218 | 946 | if (init_irq(num, irqcap, i2c_irq_start, i2c_irq_end)) |
paul@218 | 947 | return; |
paul@218 | 948 | |
paul@218 | 949 | i2c_channels[num] = i2c_get_channel(i2c, num); |
paul@218 | 950 | i2c_irqs[num] = irqcap; |
paul@218 | 951 | } |
paul@218 | 952 | |
paul@218 | 953 | static void i2c_get(void) |
paul@218 | 954 | { |
paul@218 | 955 | void *channel; |
paul@218 | 956 | int num; |
paul@218 | 957 | uint8_t buffer[32]; |
paul@218 | 958 | unsigned int address, reg, count; |
paul@218 | 959 | int i, transferred; |
paul@218 | 960 | |
paul@218 | 961 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 962 | |
paul@218 | 963 | if (channel == NULL) |
paul@218 | 964 | return; |
paul@218 | 965 | |
paul@218 | 966 | if (!read_encoded_number("Address", "%2x", &address)) |
paul@218 | 967 | return; |
paul@218 | 968 | |
paul@218 | 969 | if (address >= 0x80) |
paul@218 | 970 | { |
paul@218 | 971 | printf("Address must be less than 80.\n"); |
paul@218 | 972 | return; |
paul@218 | 973 | } |
paul@218 | 974 | |
paul@218 | 975 | if (!read_encoded_number("Register", "%2x", ®)) |
paul@218 | 976 | return; |
paul@218 | 977 | |
paul@218 | 978 | if (!read_number(NULL, &count)) |
paul@218 | 979 | count = 1; |
paul@218 | 980 | |
paul@218 | 981 | buffer[0] = (uint8_t) (reg & 0xff); |
paul@218 | 982 | |
paul@218 | 983 | i2c_set_target(channel, address); |
paul@218 | 984 | |
paul@218 | 985 | if (!_i2c_write(channel, buffer, 1, 0, i2c_irqs[num], 1000000)) |
paul@218 | 986 | { |
paul@218 | 987 | printf("Register request failed.\n"); |
paul@218 | 988 | return; |
paul@218 | 989 | } |
paul@218 | 990 | |
paul@218 | 991 | transferred = _i2c_read(channel, buffer, count, 1, i2c_irqs[num], 1000000); |
paul@218 | 992 | |
paul@218 | 993 | if (!transferred) |
paul@218 | 994 | { |
paul@218 | 995 | printf("Register read failed.\n"); |
paul@218 | 996 | return; |
paul@218 | 997 | } |
paul@218 | 998 | |
paul@218 | 999 | for (i = 0; i < transferred; i++) |
paul@218 | 1000 | printf("%02x ", buffer[i]); |
paul@218 | 1001 | printf("\n"); |
paul@218 | 1002 | } |
paul@218 | 1003 | |
paul@218 | 1004 | static void i2c_scan(void) |
paul@218 | 1005 | { |
paul@218 | 1006 | void *channel; |
paul@218 | 1007 | unsigned int address; |
paul@218 | 1008 | uint8_t buffer[1]; |
paul@218 | 1009 | int num; |
paul@218 | 1010 | |
paul@218 | 1011 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 1012 | |
paul@218 | 1013 | if (channel == NULL) |
paul@218 | 1014 | return; |
paul@218 | 1015 | |
paul@218 | 1016 | for (address = 0; address < 0x20; address++) |
paul@218 | 1017 | printf("%02x ", address); |
paul@218 | 1018 | printf("\n"); |
paul@218 | 1019 | |
paul@218 | 1020 | for (address = 0; address < 0x20; address++) |
paul@218 | 1021 | printf("-- "); |
paul@218 | 1022 | |
paul@218 | 1023 | for (address = 0; address < 0x80; address++) |
paul@218 | 1024 | { |
paul@218 | 1025 | if ((address % 32) == 0) |
paul@218 | 1026 | printf("\n"); |
paul@218 | 1027 | |
paul@218 | 1028 | i2c_set_target(channel, address); |
paul@218 | 1029 | |
paul@218 | 1030 | if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) |
paul@218 | 1031 | printf("%02x ", address); |
paul@218 | 1032 | else |
paul@218 | 1033 | printf("-- "); |
paul@218 | 1034 | } |
paul@218 | 1035 | |
paul@218 | 1036 | printf("\n"); |
paul@218 | 1037 | for (address = 0; address < 0x20; address++) |
paul@218 | 1038 | printf("-- "); |
paul@218 | 1039 | printf("\n\n"); |
paul@218 | 1040 | } |
paul@218 | 1041 | |
paul@218 | 1042 | |
paul@218 | 1043 | |
paul@218 | 1044 | /* SPI operations. */ |
paul@218 | 1045 | |
paul@218 | 1046 | static void new_spi_channel(void *gpio[]) |
paul@218 | 1047 | { |
paul@218 | 1048 | unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, enable_pin; |
paul@218 | 1049 | int num = get_channel_number(num_spi_channels); |
paul@218 | 1050 | uint32_t frequency; |
paul@218 | 1051 | |
paul@218 | 1052 | if (num < 0) |
paul@218 | 1053 | return; |
paul@218 | 1054 | |
paul@218 | 1055 | if (!get_port_and_pin(&clock_port, &clock_pin)) |
paul@218 | 1056 | return; |
paul@218 | 1057 | |
paul@218 | 1058 | if (!get_port_and_pin(&data_port, &data_pin)) |
paul@218 | 1059 | return; |
paul@218 | 1060 | |
paul@218 | 1061 | if (!get_port_and_pin(&enable_port, &enable_pin)) |
paul@218 | 1062 | return; |
paul@218 | 1063 | |
paul@218 | 1064 | if (!read_number("Frequency", &frequency)) |
paul@218 | 1065 | return; |
paul@218 | 1066 | |
paul@218 | 1067 | spi_channels[num] = spi_get_channel(gpio[clock_port], clock_pin, |
paul@218 | 1068 | gpio[data_port], data_pin, |
paul@218 | 1069 | gpio[enable_port], enable_pin, |
paul@218 | 1070 | frequency); |
paul@218 | 1071 | } |
paul@218 | 1072 | |
paul@218 | 1073 | static void spi_send_data(void) |
paul@218 | 1074 | { |
paul@218 | 1075 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@218 | 1076 | int bytes; |
paul@218 | 1077 | unsigned int byte; |
paul@218 | 1078 | uint8_t buffer[256]; |
paul@218 | 1079 | |
paul@218 | 1080 | bytes = 0; |
paul@218 | 1081 | |
paul@218 | 1082 | while (read_encoded_number(NULL, "%2x", &byte)) |
paul@218 | 1083 | { |
paul@218 | 1084 | buffer[bytes] = (uint8_t) (byte & 0xff); |
paul@218 | 1085 | bytes++; |
paul@218 | 1086 | } |
paul@218 | 1087 | |
paul@218 | 1088 | spi_send(channel, bytes, buffer); |
paul@218 | 1089 | } |
paul@218 | 1090 | |
paul@218 | 1091 | |
paul@218 | 1092 | |
paul@218 | 1093 | /* Command processing. */ |
paul@218 | 1094 | |
paul@218 | 1095 | static void handle_aic(void *aic) |
paul@218 | 1096 | { |
paul@218 | 1097 | char *token; |
paul@218 | 1098 | |
paul@218 | 1099 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1100 | { |
paul@218 | 1101 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1102 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1103 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1104 | new_aic_channel(aic); |
paul@218 | 1105 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@218 | 1106 | set_aic_buffer(); |
paul@218 | 1107 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1108 | aic_transfer_data(); |
paul@218 | 1109 | else |
paul@218 | 1110 | printf("aic channel | list | set | transfer\n"); |
paul@218 | 1111 | } |
paul@218 | 1112 | else |
paul@218 | 1113 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1114 | } |
paul@218 | 1115 | |
paul@218 | 1116 | static void handle_cpm(void *cpm) |
paul@218 | 1117 | { |
paul@218 | 1118 | char *token; |
paul@218 | 1119 | |
paul@218 | 1120 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1121 | { |
paul@218 | 1122 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1123 | list_clocks(cpm); |
paul@218 | 1124 | else if (!strcmp(token, "f") || !strcmp(token, "frequency")) |
paul@218 | 1125 | set_frequency(cpm); |
paul@218 | 1126 | else if (!strcmp(token, "p") || !strcmp(token, "parameters")) |
paul@218 | 1127 | set_parameters(cpm); |
paul@218 | 1128 | else if (!strcmp(token, "s") || !strcmp(token, "source")) |
paul@218 | 1129 | set_source(cpm); |
paul@218 | 1130 | else if (!strcmp(token, "start")) |
paul@218 | 1131 | start_clock(cpm); |
paul@218 | 1132 | else if (!strcmp(token, "stop")) |
paul@218 | 1133 | stop_clock(cpm); |
paul@218 | 1134 | else |
paul@218 | 1135 | printf("cpm list | frequency | parameters | source | start | stop\n"); |
paul@218 | 1136 | } |
paul@218 | 1137 | else |
paul@218 | 1138 | list_clocks(cpm); |
paul@218 | 1139 | } |
paul@218 | 1140 | |
paul@218 | 1141 | static void handle_dma(void *dma) |
paul@218 | 1142 | { |
paul@218 | 1143 | char *token; |
paul@218 | 1144 | |
paul@218 | 1145 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1146 | { |
paul@218 | 1147 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1148 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1149 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1150 | new_dma_channel(dma); |
paul@218 | 1151 | else if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@218 | 1152 | new_dma_region(); |
paul@218 | 1153 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@218 | 1154 | set_dma_region(); |
paul@218 | 1155 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1156 | dma_transfer_data(); |
paul@218 | 1157 | else |
paul@218 | 1158 | printf("dma channel | list | region | set | transfer\n"); |
paul@218 | 1159 | } |
paul@218 | 1160 | else |
paul@218 | 1161 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1162 | } |
paul@218 | 1163 | |
paul@218 | 1164 | static void handle_gpio(void *gpio[]) |
paul@218 | 1165 | { |
paul@218 | 1166 | char *token; |
paul@218 | 1167 | |
paul@218 | 1168 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1169 | { |
paul@218 | 1170 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1171 | list_gpios(gpio); |
paul@218 | 1172 | else if (!strcmp(token, "a") || !strcmp(token, "alt")) |
paul@218 | 1173 | set_gpio_alt_func(gpio); |
paul@218 | 1174 | else if (!strcmp(token, "i") || !strcmp(token, "io")) |
paul@218 | 1175 | set_gpio_pad(gpio); |
paul@218 | 1176 | else if (!strcmp(token, "p") || !strcmp(token, "pull")) |
paul@218 | 1177 | set_gpio_pull(gpio); |
paul@218 | 1178 | else |
paul@218 | 1179 | printf("gpio list | alt | io | pull\n"); |
paul@218 | 1180 | } |
paul@218 | 1181 | else |
paul@218 | 1182 | list_gpios(gpio); |
paul@218 | 1183 | } |
paul@218 | 1184 | |
paul@218 | 1185 | static void handle_i2c(void *i2c) |
paul@218 | 1186 | { |
paul@218 | 1187 | char *token; |
paul@218 | 1188 | |
paul@218 | 1189 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1190 | { |
paul@218 | 1191 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1192 | list_i2c_channels(); |
paul@218 | 1193 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1194 | new_i2c_channel(i2c); |
paul@218 | 1195 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@218 | 1196 | i2c_get(); |
paul@218 | 1197 | else if (!strcmp(token, "s") || !strcmp(token, "scan")) |
paul@218 | 1198 | i2c_scan(); |
paul@218 | 1199 | else |
paul@218 | 1200 | printf("i2c channel | get | list | scan\n"); |
paul@218 | 1201 | } |
paul@218 | 1202 | else |
paul@218 | 1203 | list_i2c_channels(); |
paul@218 | 1204 | } |
paul@218 | 1205 | |
paul@218 | 1206 | static void handle_spi(void *gpio[]) |
paul@218 | 1207 | { |
paul@218 | 1208 | char *token; |
paul@218 | 1209 | |
paul@218 | 1210 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1211 | { |
paul@218 | 1212 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1213 | list_channels(num_spi_channels, spi_channels); |
paul@218 | 1214 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1215 | new_spi_channel(gpio); |
paul@218 | 1216 | else if (!strcmp(token, "s") || !strcmp(token, "send")) |
paul@218 | 1217 | spi_send_data(); |
paul@218 | 1218 | else |
paul@218 | 1219 | printf("spi channel | list | send\n"); |
paul@218 | 1220 | } |
paul@218 | 1221 | else |
paul@218 | 1222 | list_channels(num_spi_channels, spi_channels); |
paul@218 | 1223 | } |
paul@218 | 1224 | |
paul@218 | 1225 | |
paul@218 | 1226 | |
paul@218 | 1227 | int main(void) |
paul@218 | 1228 | { |
paul@218 | 1229 | l4_addr_t aic_base = 0, aic_base_end = 0; |
paul@218 | 1230 | l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; |
paul@218 | 1231 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@218 | 1232 | l4_addr_t dma_base = 0, dma_base_end = 0; |
paul@218 | 1233 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@218 | 1234 | l4_addr_t i2c_base = 0, i2c_base_end = 0; |
paul@218 | 1235 | void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c; |
paul@218 | 1236 | int result = 0; |
paul@218 | 1237 | unsigned int port; |
paul@218 | 1238 | |
paul@218 | 1239 | icucap = l4re_env_get_cap("icu"); |
paul@218 | 1240 | |
paul@218 | 1241 | /* Obtain resource details describing I/O memory. */ |
paul@218 | 1242 | |
paul@218 | 1243 | printf("Access CPM...\n"); |
paul@218 | 1244 | |
paul@218 | 1245 | if ((result = get_memory(memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) |
paul@218 | 1246 | return 1; |
paul@218 | 1247 | |
paul@218 | 1248 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@218 | 1249 | |
paul@218 | 1250 | cpm = cpm_init(cpm_base); |
paul@218 | 1251 | |
paul@218 | 1252 | printf("Access DMA...\n"); |
paul@218 | 1253 | |
paul@218 | 1254 | if ((result = get_memory(memory_regions[DMA], &dma_base, &dma_base_end)) < 0) |
paul@218 | 1255 | return 1; |
paul@218 | 1256 | |
paul@218 | 1257 | printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); |
paul@218 | 1258 | |
paul@218 | 1259 | dma = dma_init(dma_base, dma_base_end, cpm); |
paul@218 | 1260 | |
paul@218 | 1261 | if (get_irq(memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) |
paul@218 | 1262 | return 1; |
paul@218 | 1263 | |
paul@218 | 1264 | printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); |
paul@218 | 1265 | |
paul@218 | 1266 | if (init_dma()) |
paul@218 | 1267 | return 1; |
paul@218 | 1268 | |
paul@218 | 1269 | dma_enable(dma); |
paul@218 | 1270 | |
paul@218 | 1271 | printf("Access GPIO...\n"); |
paul@218 | 1272 | |
paul@218 | 1273 | if ((result = get_memory(memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) |
paul@218 | 1274 | return 1; |
paul@218 | 1275 | |
paul@218 | 1276 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@218 | 1277 | |
paul@218 | 1278 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 1279 | gpio[port] = gpio_init(gpio_base + port * 0x100, gpio_base + (port + 1) * 0x100, |
paul@218 | 1280 | 32, gpio_ports[port].pull_ups, gpio_ports[port].pull_downs); |
paul@218 | 1281 | |
paul@218 | 1282 | printf("Access I2C...\n"); |
paul@218 | 1283 | |
paul@218 | 1284 | if ((result = get_memory(memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) |
paul@218 | 1285 | return 1; |
paul@218 | 1286 | |
paul@218 | 1287 | printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); |
paul@218 | 1288 | |
paul@218 | 1289 | i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); |
paul@218 | 1290 | |
paul@218 | 1291 | if (get_irq(memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) |
paul@218 | 1292 | return 1; |
paul@218 | 1293 | |
paul@218 | 1294 | printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); |
paul@218 | 1295 | |
paul@218 | 1296 | printf("Access AIC...\n"); |
paul@218 | 1297 | |
paul@218 | 1298 | if ((result = get_memory_complete(memory_regions[AIC], &aic_base, &aic_base_end, |
paul@218 | 1299 | &aic_phys_base, &aic_phys_base_end)) < 0) |
paul@218 | 1300 | return 1; |
paul@218 | 1301 | |
paul@218 | 1302 | printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); |
paul@218 | 1303 | |
paul@218 | 1304 | aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); |
paul@218 | 1305 | |
paul@218 | 1306 | if (get_irq(memory_regions[AIC], &aic_irq_start, &aic_irq_end) < 0) |
paul@218 | 1307 | return 1; |
paul@218 | 1308 | |
paul@218 | 1309 | printf("IRQ range at %d...%d.\n", aic_irq_start, aic_irq_end); |
paul@218 | 1310 | |
paul@218 | 1311 | /* Start the interactive session. */ |
paul@218 | 1312 | |
paul@218 | 1313 | printf("aic, cpm, dma, gpio, i2c, spi\n"); |
paul@218 | 1314 | |
paul@218 | 1315 | while (1) |
paul@218 | 1316 | { |
paul@218 | 1317 | char cmdline[256], *token; |
paul@218 | 1318 | |
paul@218 | 1319 | printf("> "); |
paul@218 | 1320 | |
paul@218 | 1321 | token = fgets(cmdline, 64, stdin); |
paul@218 | 1322 | |
paul@218 | 1323 | if (token == NULL) |
paul@218 | 1324 | break; |
paul@218 | 1325 | |
paul@218 | 1326 | if ((token = strtok(cmdline, delimiters)) == NULL) |
paul@218 | 1327 | continue; |
paul@218 | 1328 | |
paul@218 | 1329 | /* AIC/I2S commands. */ |
paul@218 | 1330 | |
paul@218 | 1331 | if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) |
paul@218 | 1332 | handle_aic(aic); |
paul@218 | 1333 | |
paul@218 | 1334 | /* CPM commands. */ |
paul@218 | 1335 | |
paul@218 | 1336 | else if (!strcmp(token, "c") || !strcmp(token, "cpm")) |
paul@218 | 1337 | handle_cpm(cpm); |
paul@218 | 1338 | |
paul@218 | 1339 | /* DMA commands. */ |
paul@218 | 1340 | |
paul@218 | 1341 | else if (!strcmp(token, "d") || !strcmp(token, "dma")) |
paul@218 | 1342 | handle_dma(dma); |
paul@218 | 1343 | |
paul@218 | 1344 | /* GPIO commands. */ |
paul@218 | 1345 | |
paul@218 | 1346 | else if (!strcmp(token, "g") || !strcmp(token, "gpio")) |
paul@218 | 1347 | handle_gpio(gpio); |
paul@218 | 1348 | |
paul@218 | 1349 | /* I2C commands. */ |
paul@218 | 1350 | |
paul@218 | 1351 | else if (!strcmp(token, "i") || !strcmp(token, "i2c")) |
paul@218 | 1352 | handle_i2c(i2c); |
paul@218 | 1353 | |
paul@218 | 1354 | /* SPI commands. */ |
paul@218 | 1355 | |
paul@218 | 1356 | else if (!strcmp(token, "s") || !strcmp(token, "spi")) |
paul@218 | 1357 | handle_spi(gpio); |
paul@218 | 1358 | |
paul@218 | 1359 | else |
paul@218 | 1360 | printf("Command?\n"); |
paul@218 | 1361 | } |
paul@218 | 1362 | |
paul@218 | 1363 | printf("End of session.\n"); |
paul@218 | 1364 | return 0; |
paul@218 | 1365 | } |