paul@218 | 1 | /* |
paul@218 | 2 | * Access various peripherals on the MIPS Creator CI20 board. |
paul@218 | 3 | * |
paul@253 | 4 | * Copyright (C) 2023, 2024 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@263 | 25 | #include <l4/devices/boot.h> |
paul@218 | 26 | #include <l4/devices/dma.h> |
paul@218 | 27 | #include <l4/devices/memory.h> |
paul@263 | 28 | #include <l4/devices/msc.h> |
paul@218 | 29 | |
paul@218 | 30 | #include <l4/io/io.h> |
paul@218 | 31 | #include <l4/re/env.h> |
paul@218 | 32 | #include <l4/re/c/util/cap_alloc.h> |
paul@218 | 33 | #include <l4/sys/cache.h> |
paul@218 | 34 | #include <l4/sys/factory.h> |
paul@218 | 35 | #include <l4/sys/icu.h> |
paul@218 | 36 | #include <l4/sys/ipc.h> |
paul@218 | 37 | #include <l4/sys/irq.h> |
paul@218 | 38 | #include <l4/sys/rcv_endpoint.h> |
paul@218 | 39 | #include <l4/vbus/vbus.h> |
paul@218 | 40 | |
paul@218 | 41 | #include <stdio.h> |
paul@218 | 42 | #include <unistd.h> |
paul@218 | 43 | #include <stdint.h> |
paul@218 | 44 | #include <stdlib.h> |
paul@218 | 45 | #include <string.h> |
paul@218 | 46 | |
paul@218 | 47 | |
paul@218 | 48 | |
paul@218 | 49 | /* Common configuration. */ |
paul@218 | 50 | |
paul@218 | 51 | static l4_cap_idx_t icucap; |
paul@218 | 52 | |
paul@236 | 53 | /* Memory definitions. */ |
paul@236 | 54 | |
paul@236 | 55 | static const unsigned int num_memory_regions = 8; |
paul@236 | 56 | |
paul@236 | 57 | static struct memory_region memory_regions[] = { |
paul@236 | 58 | {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, |
paul@236 | 59 | }; |
paul@236 | 60 | |
paul@218 | 61 | |
paul@218 | 62 | |
paul@218 | 63 | /* Device and resource discovery. */ |
paul@218 | 64 | |
paul@218 | 65 | static long item_in_range(long start, long end, long index) |
paul@218 | 66 | { |
paul@218 | 67 | if (start < end) |
paul@218 | 68 | return start + index; |
paul@218 | 69 | else if (start > end) |
paul@218 | 70 | return start - index; |
paul@218 | 71 | else |
paul@218 | 72 | return start; |
paul@218 | 73 | } |
paul@218 | 74 | |
paul@218 | 75 | |
paul@218 | 76 | |
paul@218 | 77 | /* Common functions. */ |
paul@218 | 78 | |
paul@218 | 79 | static int init_irq(int num, l4_cap_idx_t irq, l4_uint32_t start, l4_uint32_t end) |
paul@218 | 80 | { |
paul@218 | 81 | /* Create interrupt object. */ |
paul@218 | 82 | |
paul@218 | 83 | long err = l4_error(l4_factory_create_irq(l4re_global_env->factory, irq)); |
paul@218 | 84 | |
paul@218 | 85 | if (err) |
paul@218 | 86 | { |
paul@218 | 87 | printf("Could not create IRQ object: %ld\n", err); |
paul@218 | 88 | return 1; |
paul@218 | 89 | } |
paul@218 | 90 | |
paul@218 | 91 | /* Bind interrupt objects to IRQ numbers. */ |
paul@218 | 92 | |
paul@218 | 93 | err = l4_error(l4_icu_bind(icucap, |
paul@218 | 94 | item_in_range(start, end, num), |
paul@218 | 95 | irq)); |
paul@218 | 96 | |
paul@218 | 97 | if (err) |
paul@218 | 98 | { |
paul@218 | 99 | printf("Could not bind IRQ to the ICU: %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@296 | 106 | /* Attach ourselves to the interrupt handler. */ |
paul@296 | 107 | |
paul@296 | 108 | static int bind_irq(int num, l4_cap_idx_t irq) |
paul@296 | 109 | { |
paul@296 | 110 | long err = l4_error(l4_rcv_ep_bind_thread(irq, l4re_env()->main_thread, num)); |
paul@296 | 111 | |
paul@296 | 112 | if (err) |
paul@296 | 113 | { |
paul@296 | 114 | printf("Could not attach to IRQ: %ld\n", err); |
paul@296 | 115 | return 1; |
paul@296 | 116 | } |
paul@296 | 117 | |
paul@296 | 118 | return 0; |
paul@296 | 119 | } |
paul@296 | 120 | |
paul@218 | 121 | static const char *delimiters = " \n"; |
paul@218 | 122 | |
paul@218 | 123 | static char *read_token(const char *message) |
paul@218 | 124 | { |
paul@218 | 125 | char *token; |
paul@218 | 126 | |
paul@218 | 127 | if ((token = strtok(NULL, delimiters)) == NULL) |
paul@218 | 128 | { |
paul@218 | 129 | if (message != NULL) |
paul@218 | 130 | printf("%s?\n", message); |
paul@218 | 131 | } |
paul@218 | 132 | |
paul@218 | 133 | return token; |
paul@218 | 134 | } |
paul@218 | 135 | |
paul@236 | 136 | static int read_flag(const char *message, int *value, const char *flag, size_t n) |
paul@218 | 137 | { |
paul@218 | 138 | char *token = read_token(message); |
paul@218 | 139 | |
paul@218 | 140 | if (token == NULL) |
paul@218 | 141 | return 0; |
paul@218 | 142 | |
paul@236 | 143 | *value = !strncmp(token, flag, n); |
paul@218 | 144 | return 1; |
paul@218 | 145 | } |
paul@218 | 146 | |
paul@218 | 147 | static int read_encoded_number(const char *message, const char *format, unsigned int *num) |
paul@218 | 148 | { |
paul@218 | 149 | char *token = read_token(message); |
paul@218 | 150 | |
paul@218 | 151 | if (token == NULL) |
paul@218 | 152 | return 0; |
paul@218 | 153 | |
paul@218 | 154 | sscanf(token, format, num); |
paul@218 | 155 | return 1; |
paul@218 | 156 | } |
paul@218 | 157 | |
paul@218 | 158 | static int read_number(const char *message, unsigned int *num) |
paul@218 | 159 | { |
paul@218 | 160 | char *token = read_token(message); |
paul@218 | 161 | |
paul@218 | 162 | if (token == NULL) |
paul@218 | 163 | return 0; |
paul@218 | 164 | |
paul@218 | 165 | if (!strncmp(token, "0x", 2) || !strncmp(token, "0X", 2)) |
paul@218 | 166 | sscanf(token, "%x", num); |
paul@218 | 167 | else |
paul@218 | 168 | *num = atoi(token); |
paul@218 | 169 | |
paul@218 | 170 | return 1; |
paul@218 | 171 | } |
paul@218 | 172 | |
paul@218 | 173 | static int get_resource_number(const char *type, unsigned int available) |
paul@218 | 174 | { |
paul@218 | 175 | unsigned int num; |
paul@218 | 176 | |
paul@218 | 177 | if (!read_number(type, &num)) |
paul@218 | 178 | return -1; |
paul@218 | 179 | |
paul@218 | 180 | if (num >= available) |
paul@218 | 181 | { |
paul@218 | 182 | printf("%s number out of range.\n", type); |
paul@218 | 183 | return -1; |
paul@218 | 184 | } |
paul@218 | 185 | |
paul@218 | 186 | return num; |
paul@218 | 187 | } |
paul@218 | 188 | |
paul@218 | 189 | static int get_channel_number(unsigned int available) |
paul@218 | 190 | { |
paul@218 | 191 | return get_resource_number("Channel", available); |
paul@218 | 192 | } |
paul@218 | 193 | |
paul@218 | 194 | static int get_region_number(unsigned int available) |
paul@218 | 195 | { |
paul@218 | 196 | return get_resource_number("Region", available); |
paul@218 | 197 | } |
paul@218 | 198 | |
paul@218 | 199 | static void *get_channel(int num_channels, void *channels[], int *num) |
paul@218 | 200 | { |
paul@218 | 201 | int n = get_channel_number(num_channels); |
paul@218 | 202 | void *channel; |
paul@218 | 203 | |
paul@218 | 204 | if (n < 0) |
paul@218 | 205 | return NULL; |
paul@218 | 206 | |
paul@218 | 207 | channel = channels[n]; |
paul@218 | 208 | |
paul@218 | 209 | if (channel == NULL) |
paul@218 | 210 | printf("Channel needs creating.\n"); |
paul@218 | 211 | |
paul@218 | 212 | if (num != NULL) |
paul@218 | 213 | *num = n; |
paul@218 | 214 | |
paul@218 | 215 | return channel; |
paul@218 | 216 | } |
paul@218 | 217 | |
paul@218 | 218 | static void list_channels(int num_channels, void *channels[]) |
paul@218 | 219 | { |
paul@218 | 220 | int num; |
paul@218 | 221 | void *channel; |
paul@218 | 222 | |
paul@218 | 223 | for (num = 0; num < num_channels; num++) |
paul@218 | 224 | { |
paul@218 | 225 | printf("Channel %d: ", num); |
paul@218 | 226 | |
paul@218 | 227 | channel = channels[num]; |
paul@218 | 228 | |
paul@218 | 229 | if (channel == NULL) |
paul@218 | 230 | printf("(inactive)\n"); |
paul@218 | 231 | else |
paul@218 | 232 | printf("active\n"); |
paul@218 | 233 | } |
paul@218 | 234 | } |
paul@218 | 235 | |
paul@221 | 236 | static struct dma_region *_get_dma_region(void) |
paul@221 | 237 | { |
paul@221 | 238 | int num = get_region_number(num_dma_regions); |
paul@221 | 239 | |
paul@221 | 240 | if (num < 0) |
paul@221 | 241 | return NULL; |
paul@221 | 242 | |
paul@221 | 243 | return &dma_regions[num]; |
paul@221 | 244 | } |
paul@218 | 245 | |
paul@236 | 246 | static struct memory_region *_get_memory_region(void) |
paul@236 | 247 | { |
paul@236 | 248 | int num = get_region_number(num_memory_regions); |
paul@236 | 249 | |
paul@236 | 250 | if (num < 0) |
paul@236 | 251 | return NULL; |
paul@236 | 252 | |
paul@236 | 253 | return &memory_regions[num]; |
paul@236 | 254 | } |
paul@236 | 255 | |
paul@236 | 256 | static int get_memory_region(unsigned int size, l4_addr_t *addr) |
paul@236 | 257 | { |
paul@253 | 258 | void *a = calloc(size, sizeof(char)); |
paul@253 | 259 | |
paul@253 | 260 | *addr = (l4_addr_t) a; |
paul@253 | 261 | return a == NULL ? 1 : 0; |
paul@236 | 262 | } |
paul@236 | 263 | |
paul@218 | 264 | |
paul@218 | 265 | |
paul@218 | 266 | /* AIC/I2S operations. */ |
paul@218 | 267 | |
paul@218 | 268 | static void new_aic_channel(void *aic) |
paul@218 | 269 | { |
paul@218 | 270 | int num = get_channel_number(num_aic_channels); |
paul@218 | 271 | void *channel; |
paul@218 | 272 | |
paul@218 | 273 | if (num < 0) |
paul@218 | 274 | return; |
paul@218 | 275 | |
paul@218 | 276 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 277 | |
paul@218 | 278 | if (channel == NULL) |
paul@218 | 279 | return; |
paul@218 | 280 | |
paul@218 | 281 | aic_channels[num] = aic_get_channel(aic, num, channel); |
paul@218 | 282 | } |
paul@218 | 283 | |
paul@218 | 284 | static void aic_transfer_data(void) |
paul@218 | 285 | { |
paul@218 | 286 | void *channel = get_channel(num_aic_channels, aic_channels, NULL); |
paul@221 | 287 | struct dma_region *region; |
paul@221 | 288 | uint32_t count, rate, resolution, transferred; |
paul@218 | 289 | |
paul@218 | 290 | if (channel == NULL) |
paul@218 | 291 | return; |
paul@218 | 292 | |
paul@221 | 293 | region = _get_dma_region(); |
paul@221 | 294 | |
paul@221 | 295 | if (region == NULL) |
paul@221 | 296 | return; |
paul@221 | 297 | |
paul@218 | 298 | if (!read_number("Sample size", &count)) |
paul@218 | 299 | return; |
paul@218 | 300 | |
paul@218 | 301 | if (!read_number("Sample rate", &rate)) |
paul@218 | 302 | return; |
paul@218 | 303 | |
paul@218 | 304 | if (!read_number("Sample resolution", &resolution)) |
paul@218 | 305 | return; |
paul@218 | 306 | |
paul@221 | 307 | transferred = aic_transfer(channel, region->paddr, count, rate, resolution); |
paul@218 | 308 | |
paul@218 | 309 | printf("Transferred: %d\n", transferred); |
paul@218 | 310 | } |
paul@218 | 311 | |
paul@218 | 312 | |
paul@218 | 313 | |
paul@218 | 314 | /* CPM operations. */ |
paul@218 | 315 | |
paul@218 | 316 | static const char *clock_id(enum Clock_identifiers clock) |
paul@218 | 317 | { |
paul@218 | 318 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 319 | if (clock == clocks[i].clock) |
paul@218 | 320 | return clocks[i].id; |
paul@218 | 321 | |
paul@218 | 322 | return NULL; |
paul@218 | 323 | } |
paul@218 | 324 | |
paul@218 | 325 | static enum Clock_identifiers get_clock(char *id) |
paul@218 | 326 | { |
paul@218 | 327 | if (id != NULL) |
paul@218 | 328 | { |
paul@218 | 329 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 330 | if (!strcmp(id, clocks[i].id)) |
paul@218 | 331 | return clocks[i].clock; |
paul@218 | 332 | } |
paul@218 | 333 | |
paul@243 | 334 | return Clock_none; |
paul@218 | 335 | } |
paul@218 | 336 | |
paul@218 | 337 | static const char list_clocks_separator[] = "--------------------------------"; |
paul@218 | 338 | |
paul@218 | 339 | #define sep(x) (list_clocks_separator + sizeof(list_clocks_separator) - 1 - x) |
paul@218 | 340 | |
paul@218 | 341 | static void list_clocks(void *cpm) |
paul@218 | 342 | { |
paul@218 | 343 | /* Read information from the clock and power management unit. */ |
paul@218 | 344 | |
paul@239 | 345 | printf("%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n" |
paul@239 | 346 | "%-10s %-15s %-7s %-6s %-12s %-12s %-24s %-12s %-3s\n", |
paul@218 | 347 | "Id", "Clock", "Type", "Source", "Source Clock", "Source Freq.", |
paul@218 | 348 | "Parameters", "Frequency", "On", |
paul@239 | 349 | sep(10), sep(15), sep(7), sep(6), sep(12), sep(12), |
paul@218 | 350 | sep(24), sep(12), sep(3)); |
paul@218 | 351 | |
paul@218 | 352 | for (int i = 0; clocks[i].id != NULL; i++) |
paul@218 | 353 | { |
paul@218 | 354 | uint32_t parameters[4]; |
paul@218 | 355 | char parameter_str[25] = {0}; |
paul@218 | 356 | const char *source_id = clock_id(cpm_get_source_clock(cpm, clocks[i].clock)); |
paul@218 | 357 | int num_parameters = cpm_get_parameters(cpm, clocks[i].clock, parameters); |
paul@218 | 358 | |
paul@298 | 359 | for (int p = 0, pos = 0; (p < num_parameters) && (p < 4); p++) |
paul@218 | 360 | { |
paul@218 | 361 | int result = sprintf(parameter_str + pos, "%-7d ", parameters[p]); |
paul@218 | 362 | if (result < 0) |
paul@218 | 363 | break; |
paul@218 | 364 | pos += result; |
paul@218 | 365 | } |
paul@218 | 366 | |
paul@239 | 367 | printf("%-10s %-15s %-7s %-6d %-12s %-12lld %-24s %-12lld %-3s\n", |
paul@218 | 368 | clocks[i].id, |
paul@218 | 369 | clocks[i].name, |
paul@218 | 370 | cpm_clock_type(cpm, clocks[i].clock), |
paul@218 | 371 | cpm_get_source(cpm, clocks[i].clock), |
paul@218 | 372 | source_id != NULL ? source_id : "...", |
paul@218 | 373 | cpm_get_source_frequency(cpm, clocks[i].clock), |
paul@218 | 374 | parameter_str, |
paul@218 | 375 | cpm_get_frequency(cpm, clocks[i].clock), |
paul@218 | 376 | cpm_have_clock(cpm, clocks[i].clock) ? "on" : "off"); |
paul@218 | 377 | } |
paul@218 | 378 | } |
paul@218 | 379 | |
paul@218 | 380 | static enum Clock_identifiers get_clock_by_name(void) |
paul@218 | 381 | { |
paul@218 | 382 | char *token; |
paul@218 | 383 | enum Clock_identifiers clock; |
paul@218 | 384 | |
paul@218 | 385 | if ((token = read_token("Clock")) == NULL) |
paul@243 | 386 | return Clock_none; |
paul@218 | 387 | |
paul@218 | 388 | clock = get_clock(token); |
paul@218 | 389 | |
paul@243 | 390 | if (clock == Clock_none) |
paul@218 | 391 | { |
paul@218 | 392 | printf("Unrecognised clock: %s\n", token); |
paul@243 | 393 | return Clock_none; |
paul@218 | 394 | } |
paul@218 | 395 | |
paul@218 | 396 | return clock; |
paul@218 | 397 | } |
paul@218 | 398 | |
paul@218 | 399 | static void set_frequency(void *cpm) |
paul@218 | 400 | { |
paul@218 | 401 | enum Clock_identifiers clock; |
paul@218 | 402 | unsigned int frequency; |
paul@218 | 403 | |
paul@218 | 404 | clock = get_clock_by_name(); |
paul@218 | 405 | |
paul@243 | 406 | if (clock == Clock_none) |
paul@218 | 407 | return; |
paul@218 | 408 | |
paul@218 | 409 | if (!read_number("Frequency", &frequency)) |
paul@218 | 410 | return; |
paul@218 | 411 | |
paul@218 | 412 | if (!cpm_set_frequency(cpm, clock, frequency)) |
paul@218 | 413 | printf("Frequency not set.\n"); |
paul@218 | 414 | } |
paul@218 | 415 | |
paul@218 | 416 | static void set_parameters(void *cpm) |
paul@218 | 417 | { |
paul@218 | 418 | char *token; |
paul@218 | 419 | enum Clock_identifiers clock; |
paul@218 | 420 | uint32_t parameters[4]; |
paul@218 | 421 | int num_parameters; |
paul@218 | 422 | |
paul@218 | 423 | clock = get_clock_by_name(); |
paul@218 | 424 | |
paul@243 | 425 | if (clock == Clock_none) |
paul@218 | 426 | return; |
paul@218 | 427 | |
paul@218 | 428 | for (num_parameters = 0; num_parameters < 4; num_parameters++) |
paul@218 | 429 | { |
paul@218 | 430 | if ((token = read_token(NULL)) == NULL) |
paul@218 | 431 | break; |
paul@218 | 432 | |
paul@218 | 433 | parameters[num_parameters] = atoi(token); |
paul@218 | 434 | } |
paul@218 | 435 | |
paul@218 | 436 | if (!num_parameters) |
paul@218 | 437 | { |
paul@218 | 438 | printf("Parameters?\n"); |
paul@218 | 439 | return; |
paul@218 | 440 | } |
paul@218 | 441 | |
paul@218 | 442 | if (!cpm_set_parameters(cpm, clock, num_parameters, parameters)) |
paul@218 | 443 | printf("Too few or invalid parameters.\n"); |
paul@218 | 444 | } |
paul@218 | 445 | |
paul@218 | 446 | static void set_source(void *cpm) |
paul@218 | 447 | { |
paul@218 | 448 | enum Clock_identifiers clock; |
paul@218 | 449 | unsigned int source; |
paul@218 | 450 | |
paul@218 | 451 | clock = get_clock_by_name(); |
paul@218 | 452 | |
paul@243 | 453 | if (clock == Clock_none) |
paul@218 | 454 | return; |
paul@218 | 455 | |
paul@218 | 456 | if (!read_number("Source", &source)) |
paul@218 | 457 | return; |
paul@218 | 458 | |
paul@218 | 459 | cpm_set_source(cpm, clock, source); |
paul@218 | 460 | } |
paul@218 | 461 | |
paul@218 | 462 | static void start_clock(void *cpm) |
paul@218 | 463 | { |
paul@218 | 464 | enum Clock_identifiers clock; |
paul@218 | 465 | |
paul@218 | 466 | clock = get_clock_by_name(); |
paul@218 | 467 | |
paul@243 | 468 | if (clock == Clock_none) |
paul@218 | 469 | return; |
paul@218 | 470 | |
paul@218 | 471 | cpm_start_clock(cpm, clock); |
paul@218 | 472 | } |
paul@218 | 473 | |
paul@218 | 474 | static void stop_clock(void *cpm) |
paul@218 | 475 | { |
paul@218 | 476 | enum Clock_identifiers clock; |
paul@218 | 477 | |
paul@218 | 478 | clock = get_clock_by_name(); |
paul@218 | 479 | |
paul@243 | 480 | if (clock == Clock_none) |
paul@218 | 481 | return; |
paul@218 | 482 | |
paul@218 | 483 | cpm_stop_clock(cpm, clock); |
paul@218 | 484 | } |
paul@218 | 485 | |
paul@218 | 486 | |
paul@218 | 487 | |
paul@218 | 488 | /* DMA configuration. */ |
paul@218 | 489 | |
paul@218 | 490 | static l4_uint32_t dma_irq_start = 0, dma_irq_end = 0; |
paul@218 | 491 | |
paul@218 | 492 | /* DMA operations. */ |
paul@218 | 493 | |
paul@218 | 494 | static int init_dma(void) |
paul@218 | 495 | { |
paul@218 | 496 | unsigned int i; |
paul@218 | 497 | |
paul@218 | 498 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 499 | dma_regions[i].mem = L4_INVALID_CAP; |
paul@218 | 500 | |
paul@218 | 501 | /* Here, only one IRQ is used. */ |
paul@218 | 502 | |
paul@218 | 503 | dma_irq = l4re_util_cap_alloc(); |
paul@218 | 504 | return init_irq(0, dma_irq, dma_irq_start, dma_irq_end); |
paul@218 | 505 | } |
paul@218 | 506 | |
paul@261 | 507 | static void _show_dma_region(struct dma_region *region, uint32_t offset) |
paul@261 | 508 | { |
paul@261 | 509 | unsigned int i; |
paul@261 | 510 | |
paul@261 | 511 | printf("size = %d; align = %d; virtual = 0x%lx; physical = 0x%llx\ndata (offset 0x%x) =", |
paul@261 | 512 | region->size, region->align, region->vaddr, region->paddr, offset); |
paul@261 | 513 | |
paul@261 | 514 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@261 | 515 | printf(" %02x", *((uint8_t *) region->vaddr + offset + i)); |
paul@261 | 516 | |
paul@261 | 517 | printf("\n"); |
paul@261 | 518 | } |
paul@261 | 519 | |
paul@261 | 520 | static void show_dma_region(void) |
paul@261 | 521 | { |
paul@261 | 522 | struct dma_region *region = _get_dma_region(); |
paul@261 | 523 | uint32_t offset; |
paul@261 | 524 | |
paul@261 | 525 | if (region == NULL) |
paul@261 | 526 | return; |
paul@261 | 527 | |
paul@261 | 528 | if (!read_number("Offset", &offset)) |
paul@261 | 529 | return; |
paul@261 | 530 | |
paul@261 | 531 | _show_dma_region(region, offset); |
paul@261 | 532 | } |
paul@261 | 533 | |
paul@218 | 534 | static void list_dma_regions(void) |
paul@218 | 535 | { |
paul@218 | 536 | unsigned int num; |
paul@218 | 537 | struct dma_region *region; |
paul@218 | 538 | |
paul@218 | 539 | for (num = 0; num < num_dma_regions; num++) |
paul@218 | 540 | { |
paul@218 | 541 | printf("Region %d: ", num); |
paul@218 | 542 | |
paul@218 | 543 | region = &dma_regions[num]; |
paul@218 | 544 | |
paul@218 | 545 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 546 | printf("(inactive)\n"); |
paul@218 | 547 | else |
paul@261 | 548 | _show_dma_region(region, 0); |
paul@218 | 549 | } |
paul@218 | 550 | } |
paul@218 | 551 | |
paul@218 | 552 | static void new_dma_channel(void *dma) |
paul@218 | 553 | { |
paul@218 | 554 | int num = get_channel_number(num_dma_channels); |
paul@218 | 555 | |
paul@218 | 556 | if (num < 0) |
paul@218 | 557 | return; |
paul@218 | 558 | |
paul@218 | 559 | if (dma_channels[num] != NULL) |
paul@218 | 560 | { |
paul@218 | 561 | printf("Channel already defined.\n"); |
paul@218 | 562 | return; |
paul@218 | 563 | } |
paul@218 | 564 | |
paul@218 | 565 | dma_channels[num] = dma_get_channel(dma, num, dma_irq); |
paul@218 | 566 | } |
paul@218 | 567 | |
paul@218 | 568 | static void new_dma_region(void) |
paul@218 | 569 | { |
paul@218 | 570 | struct dma_region *region = _get_dma_region(); |
paul@218 | 571 | |
paul@218 | 572 | if (region == NULL) |
paul@218 | 573 | { |
paul@218 | 574 | list_dma_regions(); |
paul@218 | 575 | return; |
paul@218 | 576 | } |
paul@218 | 577 | |
paul@218 | 578 | if (l4_is_valid_cap(region->mem)) |
paul@218 | 579 | { |
paul@218 | 580 | printf("Region already defined.\n"); |
paul@218 | 581 | return; |
paul@218 | 582 | } |
paul@218 | 583 | |
paul@218 | 584 | if (!read_number("Size", ®ion->size)) |
paul@218 | 585 | return; |
paul@218 | 586 | |
paul@218 | 587 | if (!read_number("Alignment", ®ion->align)) |
paul@218 | 588 | return; |
paul@218 | 589 | |
paul@264 | 590 | if (get_dma_region(region->size, region->align, region)) |
paul@218 | 591 | printf("Could not allocate region.\n"); |
paul@218 | 592 | } |
paul@218 | 593 | |
paul@218 | 594 | static void set_dma_region(void) |
paul@218 | 595 | { |
paul@218 | 596 | char *token; |
paul@218 | 597 | struct dma_region *region = _get_dma_region(); |
paul@218 | 598 | FILE *fp; |
paul@218 | 599 | |
paul@218 | 600 | if (region == NULL) |
paul@218 | 601 | { |
paul@218 | 602 | list_dma_regions(); |
paul@218 | 603 | return; |
paul@218 | 604 | } |
paul@218 | 605 | |
paul@218 | 606 | if (l4_is_invalid_cap(region->mem)) |
paul@218 | 607 | { |
paul@218 | 608 | printf("Region needs creating.\n"); |
paul@218 | 609 | return; |
paul@218 | 610 | } |
paul@218 | 611 | |
paul@218 | 612 | memset((void *) region->vaddr, 0, region->size); |
paul@218 | 613 | |
paul@218 | 614 | if ((token = read_token("Filename")) == NULL) |
paul@218 | 615 | return; |
paul@218 | 616 | |
paul@218 | 617 | /* Populate the region from the file. */ |
paul@218 | 618 | |
paul@218 | 619 | fp = fopen(token, "r"); |
paul@218 | 620 | |
paul@218 | 621 | if (fp == NULL) |
paul@218 | 622 | { |
paul@218 | 623 | printf("File not readable.\n"); |
paul@218 | 624 | return; |
paul@218 | 625 | } |
paul@218 | 626 | |
paul@218 | 627 | fread((char *) region->vaddr, sizeof(char), region->size, fp); |
paul@218 | 628 | fclose(fp); |
paul@218 | 629 | |
paul@218 | 630 | l4_cache_flush_data(region->vaddr, region->vaddr + region->size); |
paul@218 | 631 | } |
paul@218 | 632 | |
paul@218 | 633 | static void dma_transfer_data(void) |
paul@218 | 634 | { |
paul@221 | 635 | uint32_t count, to_transfer, transferred; |
paul@218 | 636 | unsigned int source_address, destination_address, source_width, destination_width, |
paul@218 | 637 | transfer_unit_size, request_type; |
paul@218 | 638 | int source_increment, destination_increment; |
paul@218 | 639 | void *channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@218 | 640 | unsigned int i; |
paul@218 | 641 | |
paul@218 | 642 | if (channel == NULL) |
paul@218 | 643 | return; |
paul@218 | 644 | |
paul@218 | 645 | if (!read_number("Source address", &source_address)) |
paul@218 | 646 | return; |
paul@218 | 647 | |
paul@218 | 648 | if (!read_number("Destination address", &destination_address)) |
paul@218 | 649 | return; |
paul@218 | 650 | |
paul@218 | 651 | if (!read_number("Transfer size", &count)) |
paul@218 | 652 | return; |
paul@218 | 653 | |
paul@236 | 654 | if (!read_flag("Source increment", &source_increment, "i", 1)) |
paul@218 | 655 | return; |
paul@218 | 656 | |
paul@236 | 657 | if (!read_flag("Destination increment", &destination_increment, "i", 1)) |
paul@218 | 658 | return; |
paul@218 | 659 | |
paul@218 | 660 | if (!read_number("Source width", &source_width)) |
paul@218 | 661 | return; |
paul@218 | 662 | |
paul@218 | 663 | if (!read_number("Destination width", &destination_width)) |
paul@218 | 664 | return; |
paul@218 | 665 | |
paul@218 | 666 | if (!read_number("Transfer unit size", &transfer_unit_size)) |
paul@218 | 667 | return; |
paul@218 | 668 | |
paul@218 | 669 | if (!read_number("Request type", &request_type)) |
paul@218 | 670 | return; |
paul@218 | 671 | |
paul@218 | 672 | /* Invalidate DMA region contents in case of transfers between regions. */ |
paul@218 | 673 | |
paul@218 | 674 | for (i = 0; i < num_dma_regions; i++) |
paul@218 | 675 | l4_cache_inv_data(dma_regions[i].vaddr, dma_regions[i].vaddr + dma_regions[i].size); |
paul@218 | 676 | |
paul@218 | 677 | to_transfer = dma_transfer(channel, source_address, destination_address, |
paul@221 | 678 | count, |
paul@221 | 679 | source_increment, destination_increment, |
paul@221 | 680 | source_width, destination_width, |
paul@221 | 681 | transfer_unit_size, |
paul@221 | 682 | request_type); |
paul@218 | 683 | |
paul@218 | 684 | transferred = to_transfer ? count - dma_wait(channel) : 0; |
paul@218 | 685 | |
paul@218 | 686 | printf("Transferred: %d\n", transferred); |
paul@218 | 687 | } |
paul@218 | 688 | |
paul@218 | 689 | |
paul@218 | 690 | |
paul@218 | 691 | /* GPIO operations. */ |
paul@218 | 692 | |
paul@218 | 693 | static char get_gpio_dir_label(unsigned int value, unsigned int level) |
paul@218 | 694 | { |
paul@218 | 695 | return value == Fix_input ? (level ? 'I' : 'i') : (level ? 'O' : 'o'); |
paul@218 | 696 | } |
paul@218 | 697 | |
paul@218 | 698 | static char get_gpio_irq_label(unsigned int value) |
paul@218 | 699 | { |
paul@218 | 700 | switch (value) |
paul@218 | 701 | { |
paul@218 | 702 | case L4_IRQ_F_NEG_EDGE: return 'f'; |
paul@218 | 703 | case L4_IRQ_F_POS_EDGE: return 'r'; |
paul@218 | 704 | case L4_IRQ_F_LEVEL_LOW: return 'l'; |
paul@218 | 705 | case L4_IRQ_F_LEVEL_HIGH: return 'h'; |
paul@218 | 706 | default: return '?'; |
paul@218 | 707 | } |
paul@218 | 708 | } |
paul@218 | 709 | |
paul@218 | 710 | static void list_gpios(void *gpio[]) |
paul@218 | 711 | { |
paul@218 | 712 | unsigned int port, pin; |
paul@218 | 713 | unsigned int func, value, level; |
paul@218 | 714 | |
paul@218 | 715 | printf("gpio: I = input high; i = input low; O = output high; o = output low\n" |
paul@218 | 716 | "irq: h = high level; l = low level; r = rising edge; f = falling edge\n" |
paul@218 | 717 | "alt: function number\n\n"); |
paul@218 | 718 | |
paul@218 | 719 | /* Show pin numbering. */ |
paul@218 | 720 | |
paul@218 | 721 | printf("Port/Pin "); |
paul@218 | 722 | |
paul@218 | 723 | for (pin = 0; pin < 32; pin++) |
paul@218 | 724 | { |
paul@218 | 725 | if (!(pin % 10)) |
paul@218 | 726 | printf(" %d", pin); |
paul@218 | 727 | else |
paul@218 | 728 | printf(" %d", pin % 10); |
paul@218 | 729 | } |
paul@218 | 730 | |
paul@218 | 731 | printf("\n"); |
paul@218 | 732 | |
paul@218 | 733 | for (port = 0; port < num_gpio_ports; port++) |
paul@218 | 734 | { |
paul@218 | 735 | /* Show port and pin configuration. */ |
paul@218 | 736 | |
paul@218 | 737 | printf("%c ", gpio_port_labels[port]); |
paul@218 | 738 | |
paul@218 | 739 | for (pin = 0; pin < 32; pin++) |
paul@218 | 740 | { |
paul@218 | 741 | /* Pad below the first pin digit for multiples of ten other than zero. */ |
paul@218 | 742 | |
paul@218 | 743 | if (pin && !(pin % 10)) |
paul@218 | 744 | printf(" "); |
paul@218 | 745 | |
paul@218 | 746 | gpio_config_pad_get(gpio[port], pin, &func, &value); |
paul@218 | 747 | |
paul@218 | 748 | switch (func) |
paul@218 | 749 | { |
paul@218 | 750 | case Function_alt: printf(" %d", value); break; |
paul@218 | 751 | case Function_irq: printf(" %c", get_gpio_irq_label(value)); break; |
paul@218 | 752 | case Function_gpio: |
paul@218 | 753 | { |
paul@218 | 754 | level = gpio_get(gpio[port], pin); |
paul@218 | 755 | printf(" %c", get_gpio_dir_label(value, level)); |
paul@218 | 756 | break; |
paul@218 | 757 | } |
paul@218 | 758 | default: printf(" ?"); break; |
paul@218 | 759 | } |
paul@218 | 760 | } |
paul@218 | 761 | |
paul@218 | 762 | printf("\n"); |
paul@218 | 763 | } |
paul@218 | 764 | } |
paul@218 | 765 | |
paul@218 | 766 | static int get_port_and_pin(unsigned int *port, unsigned int *pin) |
paul@218 | 767 | { |
paul@218 | 768 | char *token; |
paul@218 | 769 | |
paul@218 | 770 | if ((token = read_token("Port")) == NULL) |
paul@218 | 771 | return 0; |
paul@218 | 772 | |
paul@218 | 773 | if ((token[0] < 'A') || ((unsigned int) token[0] - (unsigned int) 'A' >= num_gpio_ports)) |
paul@218 | 774 | { |
paul@218 | 775 | printf("Bad port: %c\n", token[0]); |
paul@218 | 776 | return 0; |
paul@218 | 777 | } |
paul@218 | 778 | |
paul@218 | 779 | *port = (unsigned int) token[0] - (unsigned int) 'A'; |
paul@218 | 780 | |
paul@218 | 781 | if (!read_number("Pin", pin)) |
paul@218 | 782 | return 0; |
paul@218 | 783 | |
paul@218 | 784 | return 1; |
paul@218 | 785 | } |
paul@218 | 786 | |
paul@218 | 787 | static void set_gpio_alt_func(void *gpio[]) |
paul@218 | 788 | { |
paul@218 | 789 | unsigned int port, pin, value; |
paul@218 | 790 | |
paul@218 | 791 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 792 | return; |
paul@218 | 793 | |
paul@218 | 794 | if (!read_number("Function", &value)) |
paul@218 | 795 | return; |
paul@218 | 796 | |
paul@218 | 797 | gpio_config_pad(gpio[port], pin, Function_alt, value); |
paul@218 | 798 | } |
paul@218 | 799 | |
paul@218 | 800 | static void set_gpio_pad(void *gpio[]) |
paul@218 | 801 | { |
paul@218 | 802 | char *token; |
paul@218 | 803 | unsigned int port, pin, mode, value = 0; |
paul@218 | 804 | |
paul@218 | 805 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 806 | return; |
paul@218 | 807 | |
paul@218 | 808 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 809 | return; |
paul@218 | 810 | |
paul@218 | 811 | if (!strcmp(token, "in")) |
paul@218 | 812 | mode = Fix_input; |
paul@218 | 813 | else if (!strcmp(token, "irq")) |
paul@218 | 814 | mode = Fix_irq; |
paul@218 | 815 | else if (!strcmp(token, "out")) |
paul@218 | 816 | { |
paul@218 | 817 | mode = Fix_output; |
paul@218 | 818 | |
paul@218 | 819 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 820 | value = atoi(token); |
paul@218 | 821 | } |
paul@218 | 822 | else |
paul@218 | 823 | { |
paul@218 | 824 | printf("Mode not recognised.\n"); |
paul@218 | 825 | return; |
paul@218 | 826 | } |
paul@218 | 827 | |
paul@218 | 828 | gpio_setup(gpio[port], pin, mode, value); |
paul@218 | 829 | } |
paul@218 | 830 | |
paul@218 | 831 | static void set_gpio_pull(void *gpio[]) |
paul@218 | 832 | { |
paul@218 | 833 | char *token; |
paul@218 | 834 | unsigned int port, pin, mode; |
paul@218 | 835 | |
paul@218 | 836 | if (!get_port_and_pin(&port, &pin)) |
paul@218 | 837 | return; |
paul@218 | 838 | |
paul@218 | 839 | if ((token = read_token("Mode")) == NULL) |
paul@218 | 840 | return; |
paul@218 | 841 | |
paul@218 | 842 | if (!strcmp(token, "down")) |
paul@218 | 843 | mode = Pull_down; |
paul@218 | 844 | else if (!strcmp(token, "none")) |
paul@218 | 845 | mode = Pull_none; |
paul@218 | 846 | else if (!strcmp(token, "up")) |
paul@218 | 847 | mode = Pull_up; |
paul@218 | 848 | else |
paul@218 | 849 | { |
paul@218 | 850 | printf("Mode not recognised.\n"); |
paul@218 | 851 | return; |
paul@218 | 852 | } |
paul@218 | 853 | |
paul@218 | 854 | gpio_config_pull(gpio[port], pin, mode); |
paul@218 | 855 | } |
paul@218 | 856 | |
paul@218 | 857 | |
paul@218 | 858 | |
paul@218 | 859 | /* I2C configuration. */ |
paul@218 | 860 | |
paul@218 | 861 | static l4_uint32_t i2c_irq_start = 0, i2c_irq_end = 0; |
paul@218 | 862 | |
paul@218 | 863 | /* I2C operations. */ |
paul@218 | 864 | |
paul@218 | 865 | static int _i2c_read(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 866 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 867 | { |
paul@218 | 868 | l4_msgtag_t tag; |
paul@218 | 869 | |
paul@218 | 870 | i2c_start_read(channel, buf, length, stop); |
paul@218 | 871 | |
paul@218 | 872 | while (!i2c_read_done(channel)) |
paul@218 | 873 | { |
paul@218 | 874 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 875 | |
paul@218 | 876 | // NOTE: Error not returned. |
paul@218 | 877 | |
paul@218 | 878 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 879 | break; |
paul@218 | 880 | |
paul@218 | 881 | if (i2c_failed(channel)) |
paul@218 | 882 | break; |
paul@218 | 883 | |
paul@218 | 884 | i2c_read(channel); |
paul@218 | 885 | } |
paul@218 | 886 | |
paul@218 | 887 | if (stop) |
paul@218 | 888 | i2c_stop(channel); |
paul@218 | 889 | |
paul@218 | 890 | return i2c_have_read(channel); |
paul@218 | 891 | } |
paul@218 | 892 | |
paul@218 | 893 | static int _i2c_write(void *channel, uint8_t *buf, unsigned length, |
paul@218 | 894 | int stop, l4_cap_idx_t irqcap, unsigned timeout) |
paul@218 | 895 | { |
paul@218 | 896 | l4_msgtag_t tag; |
paul@218 | 897 | |
paul@218 | 898 | i2c_start_write(channel, buf, length, stop); |
paul@218 | 899 | |
paul@218 | 900 | while (!i2c_write_done(channel)) |
paul@218 | 901 | { |
paul@218 | 902 | tag = l4_irq_receive(irqcap, l4_timeout(L4_IPC_TIMEOUT_NEVER, l4_timeout_from_us(timeout))); |
paul@218 | 903 | |
paul@218 | 904 | // NOTE: Error not returned. |
paul@218 | 905 | |
paul@218 | 906 | if (l4_ipc_error(tag, l4_utcb())) |
paul@218 | 907 | break; |
paul@218 | 908 | |
paul@218 | 909 | if (i2c_failed(channel)) |
paul@218 | 910 | break; |
paul@218 | 911 | |
paul@218 | 912 | i2c_write(channel); |
paul@218 | 913 | } |
paul@218 | 914 | |
paul@218 | 915 | if (stop) |
paul@218 | 916 | i2c_stop(channel); |
paul@218 | 917 | |
paul@218 | 918 | return i2c_have_written(channel); |
paul@218 | 919 | } |
paul@218 | 920 | |
paul@218 | 921 | static void list_i2c_channels(void) |
paul@218 | 922 | { |
paul@218 | 923 | unsigned int num; |
paul@218 | 924 | void *channel; |
paul@218 | 925 | |
paul@218 | 926 | for (num = 0; num < num_i2c_channels; num++) |
paul@218 | 927 | { |
paul@218 | 928 | printf("Channel %d: ", num); |
paul@218 | 929 | |
paul@218 | 930 | channel = i2c_channels[num]; |
paul@218 | 931 | |
paul@218 | 932 | if (channel == NULL) |
paul@218 | 933 | printf("(inactive)\n"); |
paul@218 | 934 | else |
paul@218 | 935 | printf("%d Hz\n", i2c_get_frequency(channel)); |
paul@218 | 936 | } |
paul@218 | 937 | } |
paul@218 | 938 | |
paul@218 | 939 | static void new_i2c_channel(void *i2c) |
paul@218 | 940 | { |
paul@218 | 941 | l4_cap_idx_t irqcap; |
paul@218 | 942 | int num = get_channel_number(num_i2c_channels); |
paul@218 | 943 | |
paul@218 | 944 | if (num < 0) |
paul@218 | 945 | return; |
paul@218 | 946 | |
paul@218 | 947 | irqcap = l4re_util_cap_alloc(); |
paul@218 | 948 | |
paul@218 | 949 | if (init_irq(num, irqcap, i2c_irq_start, i2c_irq_end)) |
paul@218 | 950 | return; |
paul@218 | 951 | |
paul@296 | 952 | /* Bind to the IRQ so that we may use it in this program directly. */ |
paul@296 | 953 | |
paul@296 | 954 | if (bind_irq(num, irqcap)) |
paul@296 | 955 | return; |
paul@296 | 956 | |
paul@218 | 957 | i2c_channels[num] = i2c_get_channel(i2c, num); |
paul@218 | 958 | i2c_irqs[num] = irqcap; |
paul@218 | 959 | } |
paul@218 | 960 | |
paul@218 | 961 | static void i2c_get(void) |
paul@218 | 962 | { |
paul@218 | 963 | void *channel; |
paul@218 | 964 | int num; |
paul@218 | 965 | uint8_t buffer[32]; |
paul@218 | 966 | unsigned int address, reg, count; |
paul@218 | 967 | int i, transferred; |
paul@218 | 968 | |
paul@218 | 969 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 970 | |
paul@218 | 971 | if (channel == NULL) |
paul@218 | 972 | return; |
paul@218 | 973 | |
paul@218 | 974 | if (!read_encoded_number("Address", "%2x", &address)) |
paul@218 | 975 | return; |
paul@218 | 976 | |
paul@218 | 977 | if (address >= 0x80) |
paul@218 | 978 | { |
paul@218 | 979 | printf("Address must be less than 80.\n"); |
paul@218 | 980 | return; |
paul@218 | 981 | } |
paul@218 | 982 | |
paul@218 | 983 | if (!read_encoded_number("Register", "%2x", ®)) |
paul@218 | 984 | return; |
paul@218 | 985 | |
paul@218 | 986 | if (!read_number(NULL, &count)) |
paul@218 | 987 | count = 1; |
paul@218 | 988 | |
paul@218 | 989 | buffer[0] = (uint8_t) (reg & 0xff); |
paul@218 | 990 | |
paul@218 | 991 | i2c_set_target(channel, address); |
paul@218 | 992 | |
paul@218 | 993 | if (!_i2c_write(channel, buffer, 1, 0, i2c_irqs[num], 1000000)) |
paul@218 | 994 | { |
paul@218 | 995 | printf("Register request failed.\n"); |
paul@218 | 996 | return; |
paul@218 | 997 | } |
paul@218 | 998 | |
paul@218 | 999 | transferred = _i2c_read(channel, buffer, count, 1, i2c_irqs[num], 1000000); |
paul@218 | 1000 | |
paul@218 | 1001 | if (!transferred) |
paul@218 | 1002 | { |
paul@218 | 1003 | printf("Register read failed.\n"); |
paul@218 | 1004 | return; |
paul@218 | 1005 | } |
paul@218 | 1006 | |
paul@218 | 1007 | for (i = 0; i < transferred; i++) |
paul@218 | 1008 | printf("%02x ", buffer[i]); |
paul@218 | 1009 | printf("\n"); |
paul@218 | 1010 | } |
paul@218 | 1011 | |
paul@218 | 1012 | static void i2c_scan(void) |
paul@218 | 1013 | { |
paul@218 | 1014 | void *channel; |
paul@218 | 1015 | unsigned int address; |
paul@218 | 1016 | uint8_t buffer[1]; |
paul@218 | 1017 | int num; |
paul@218 | 1018 | |
paul@218 | 1019 | channel = get_channel(num_i2c_channels, i2c_channels, &num); |
paul@218 | 1020 | |
paul@218 | 1021 | if (channel == NULL) |
paul@218 | 1022 | return; |
paul@218 | 1023 | |
paul@218 | 1024 | for (address = 0; address < 0x20; address++) |
paul@218 | 1025 | printf("%02x ", address); |
paul@218 | 1026 | printf("\n"); |
paul@218 | 1027 | |
paul@218 | 1028 | for (address = 0; address < 0x20; address++) |
paul@218 | 1029 | printf("-- "); |
paul@218 | 1030 | |
paul@218 | 1031 | for (address = 0; address < 0x80; address++) |
paul@218 | 1032 | { |
paul@218 | 1033 | if ((address % 32) == 0) |
paul@218 | 1034 | printf("\n"); |
paul@218 | 1035 | |
paul@218 | 1036 | i2c_set_target(channel, address); |
paul@218 | 1037 | |
paul@218 | 1038 | if (_i2c_read(channel, buffer, 1, 1, i2c_irqs[num], 1000)) |
paul@218 | 1039 | printf("%02x ", address); |
paul@218 | 1040 | else |
paul@218 | 1041 | printf("-- "); |
paul@218 | 1042 | } |
paul@218 | 1043 | |
paul@218 | 1044 | printf("\n"); |
paul@218 | 1045 | for (address = 0; address < 0x20; address++) |
paul@218 | 1046 | printf("-- "); |
paul@218 | 1047 | printf("\n\n"); |
paul@218 | 1048 | } |
paul@218 | 1049 | |
paul@218 | 1050 | |
paul@218 | 1051 | |
paul@236 | 1052 | /* Memory operations. */ |
paul@236 | 1053 | |
paul@236 | 1054 | static void list_memory_regions(void) |
paul@236 | 1055 | { |
paul@236 | 1056 | unsigned int num; |
paul@236 | 1057 | struct memory_region *region; |
paul@236 | 1058 | unsigned int i; |
paul@236 | 1059 | |
paul@236 | 1060 | for (num = 0; num < num_memory_regions; num++) |
paul@236 | 1061 | { |
paul@236 | 1062 | printf("Region %d: ", num); |
paul@236 | 1063 | |
paul@236 | 1064 | region = &memory_regions[num]; |
paul@236 | 1065 | |
paul@236 | 1066 | if (!region->addr) |
paul@236 | 1067 | printf("(inactive)\n"); |
paul@236 | 1068 | else |
paul@236 | 1069 | { |
paul@236 | 1070 | printf("size = %d; virtual = 0x%lx; data =", |
paul@236 | 1071 | region->size, region->addr); |
paul@236 | 1072 | |
paul@236 | 1073 | for (i = 0; (i < region->size) && (i < 16); i++) |
paul@236 | 1074 | printf(" %02x", *((uint8_t *) region->addr + i)); |
paul@236 | 1075 | |
paul@236 | 1076 | printf("\n"); |
paul@236 | 1077 | } |
paul@236 | 1078 | } |
paul@236 | 1079 | } |
paul@236 | 1080 | |
paul@236 | 1081 | static void new_memory_region(void) |
paul@236 | 1082 | { |
paul@236 | 1083 | struct memory_region *region = _get_memory_region(); |
paul@236 | 1084 | |
paul@236 | 1085 | if (region == NULL) |
paul@236 | 1086 | { |
paul@236 | 1087 | list_memory_regions(); |
paul@236 | 1088 | return; |
paul@236 | 1089 | } |
paul@236 | 1090 | |
paul@236 | 1091 | if (region->addr) |
paul@236 | 1092 | { |
paul@236 | 1093 | printf("Region already defined.\n"); |
paul@236 | 1094 | return; |
paul@236 | 1095 | } |
paul@236 | 1096 | |
paul@236 | 1097 | if (!read_number("Size", ®ion->size)) |
paul@236 | 1098 | return; |
paul@236 | 1099 | |
paul@236 | 1100 | if (get_memory_region(region->size, ®ion->addr)) |
paul@236 | 1101 | printf("Could not allocate region.\n"); |
paul@236 | 1102 | } |
paul@236 | 1103 | |
paul@236 | 1104 | static void set_memory_region(void) |
paul@236 | 1105 | { |
paul@236 | 1106 | char *token; |
paul@236 | 1107 | struct memory_region *region = _get_memory_region(); |
paul@236 | 1108 | FILE *fp; |
paul@236 | 1109 | |
paul@236 | 1110 | if (region == NULL) |
paul@236 | 1111 | { |
paul@236 | 1112 | list_memory_regions(); |
paul@236 | 1113 | return; |
paul@236 | 1114 | } |
paul@236 | 1115 | |
paul@236 | 1116 | if (!region->addr) |
paul@236 | 1117 | { |
paul@236 | 1118 | printf("Region needs creating.\n"); |
paul@236 | 1119 | return; |
paul@236 | 1120 | } |
paul@236 | 1121 | |
paul@236 | 1122 | memset((void *) region->addr, 0, region->size); |
paul@236 | 1123 | |
paul@236 | 1124 | if ((token = read_token("Filename")) == NULL) |
paul@236 | 1125 | return; |
paul@236 | 1126 | |
paul@236 | 1127 | /* Populate the region from the file. */ |
paul@236 | 1128 | |
paul@236 | 1129 | fp = fopen(token, "r"); |
paul@236 | 1130 | |
paul@236 | 1131 | if (fp == NULL) |
paul@236 | 1132 | { |
paul@236 | 1133 | printf("File not readable.\n"); |
paul@236 | 1134 | return; |
paul@236 | 1135 | } |
paul@236 | 1136 | |
paul@236 | 1137 | fread((char *) region->addr, sizeof(char), region->size, fp); |
paul@236 | 1138 | fclose(fp); |
paul@236 | 1139 | |
paul@236 | 1140 | l4_cache_flush_data(region->addr, region->addr + region->size); |
paul@236 | 1141 | } |
paul@236 | 1142 | |
paul@236 | 1143 | |
paul@236 | 1144 | |
paul@258 | 1145 | /* MSC configuration. */ |
paul@258 | 1146 | |
paul@258 | 1147 | static l4_uint32_t msc_irq_start = 0, msc_irq_end = 0; |
paul@258 | 1148 | |
paul@258 | 1149 | /* MSC operations. */ |
paul@258 | 1150 | |
paul@263 | 1151 | static void list_msc_cards(void) |
paul@263 | 1152 | { |
paul@263 | 1153 | void *channel = get_channel(num_msc_channels, msc_channels, NULL); |
paul@263 | 1154 | struct msc_card *cards; |
paul@263 | 1155 | struct CSD *csd; |
paul@263 | 1156 | uint8_t num; |
paul@263 | 1157 | |
paul@263 | 1158 | if (channel == NULL) |
paul@263 | 1159 | return; |
paul@263 | 1160 | |
paul@263 | 1161 | cards = msc_get_cards(channel); |
paul@263 | 1162 | |
paul@263 | 1163 | printf("Card RCA Bits Size\n"); |
paul@263 | 1164 | printf("---- ---- ---- --------------\n"); |
paul@263 | 1165 | |
paul@263 | 1166 | for (num = 0; num < msc_num_cards(channel); num++) |
paul@263 | 1167 | { |
paul@263 | 1168 | csd = &cards[num].csd; |
paul@263 | 1169 | |
paul@263 | 1170 | printf("%d %04x %d %d\n", num, cards[num].rca, cards[num].bus_width, |
paul@263 | 1171 | (1 << csd->read_blocklen) * |
paul@263 | 1172 | (csd->device_size + 1) * |
paul@263 | 1173 | (1 << (csd->device_size_multiplier + 2))); |
paul@263 | 1174 | } |
paul@263 | 1175 | } |
paul@263 | 1176 | |
paul@263 | 1177 | static void list_msc_card_partitions(void) |
paul@263 | 1178 | { |
paul@263 | 1179 | void *channel = get_channel(num_msc_channels, msc_channels, NULL); |
paul@263 | 1180 | struct dma_region *dma_region; |
paul@263 | 1181 | struct partition_table_entry *entry; |
paul@263 | 1182 | uint32_t card, transferred; |
paul@263 | 1183 | uint8_t num; |
paul@263 | 1184 | |
paul@263 | 1185 | if (channel == NULL) |
paul@263 | 1186 | return; |
paul@263 | 1187 | |
paul@263 | 1188 | dma_region = _get_dma_region(); |
paul@263 | 1189 | |
paul@263 | 1190 | if (dma_region == NULL) |
paul@263 | 1191 | return; |
paul@263 | 1192 | |
paul@263 | 1193 | if (!read_number("Card", &card)) |
paul@263 | 1194 | return; |
paul@263 | 1195 | |
paul@263 | 1196 | l4_cache_inv_data(dma_region->vaddr, dma_region->vaddr + dma_region->size); |
paul@263 | 1197 | |
paul@264 | 1198 | transferred = msc_read_blocks(channel, (uint8_t) card, dma_region, 0, 1); |
paul@263 | 1199 | |
paul@263 | 1200 | if (!transferred) |
paul@263 | 1201 | { |
paul@263 | 1202 | printf("Could not read partitions.\n"); |
paul@263 | 1203 | return; |
paul@263 | 1204 | } |
paul@263 | 1205 | |
paul@263 | 1206 | entry = get_partition_table((uint8_t *) dma_region->vaddr); |
paul@263 | 1207 | |
paul@263 | 1208 | if (entry == NULL) |
paul@263 | 1209 | { |
paul@263 | 1210 | printf("Invalid partition table.\n"); |
paul@263 | 1211 | return; |
paul@263 | 1212 | } |
paul@263 | 1213 | |
paul@263 | 1214 | printf("Partition Status Type Start End Address Sectors\n"); |
paul@263 | 1215 | printf("--------- ------ ---- --------- --------- -------- --------\n"); |
paul@263 | 1216 | |
paul@263 | 1217 | for (num = 0; num < 4; num++, entry++) |
paul@263 | 1218 | { |
paul@263 | 1219 | printf("%d %02x %02x %03x %02x %02x %03x %02x %02x %08x %08x\n", |
paul@263 | 1220 | num, entry->status, entry->partition_type, |
paul@263 | 1221 | entry->chs_first_sector.cylinder, |
paul@263 | 1222 | entry->chs_first_sector.head, |
paul@263 | 1223 | entry->chs_first_sector.sector, |
paul@263 | 1224 | entry->chs_last_sector.cylinder, |
paul@263 | 1225 | entry->chs_last_sector.head, |
paul@263 | 1226 | entry->chs_last_sector.sector, |
paul@263 | 1227 | entry->lba_first_sector, |
paul@263 | 1228 | entry->num_sectors); |
paul@263 | 1229 | } |
paul@263 | 1230 | } |
paul@263 | 1231 | |
paul@258 | 1232 | static void list_msc_channels(void) |
paul@258 | 1233 | { |
paul@258 | 1234 | unsigned int num; |
paul@258 | 1235 | void *channel; |
paul@258 | 1236 | |
paul@263 | 1237 | printf("Channel Cards\n"); |
paul@263 | 1238 | printf("------- -----\n"); |
paul@258 | 1239 | |
paul@258 | 1240 | for (num = 0; num < num_msc_channels; num++) |
paul@258 | 1241 | { |
paul@258 | 1242 | printf("%d ", num); |
paul@258 | 1243 | |
paul@258 | 1244 | channel = msc_channels[num]; |
paul@258 | 1245 | |
paul@258 | 1246 | if (channel == NULL) |
paul@258 | 1247 | printf("inactive\n"); |
paul@258 | 1248 | else |
paul@263 | 1249 | printf("%d\n", msc_num_cards(channel)); |
paul@258 | 1250 | } |
paul@258 | 1251 | } |
paul@258 | 1252 | |
paul@258 | 1253 | static void new_msc_channel(void *msc) |
paul@258 | 1254 | { |
paul@258 | 1255 | l4_cap_idx_t irqcap; |
paul@258 | 1256 | int num = get_channel_number(num_msc_channels); |
paul@258 | 1257 | void *channel; |
paul@258 | 1258 | |
paul@258 | 1259 | if (num < 0) |
paul@258 | 1260 | return; |
paul@258 | 1261 | |
paul@258 | 1262 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@258 | 1263 | |
paul@258 | 1264 | if (channel == NULL) |
paul@258 | 1265 | return; |
paul@258 | 1266 | |
paul@258 | 1267 | irqcap = l4re_util_cap_alloc(); |
paul@258 | 1268 | |
paul@258 | 1269 | if (init_irq(num, irqcap, msc_irq_start, msc_irq_end)) |
paul@258 | 1270 | return; |
paul@258 | 1271 | |
paul@258 | 1272 | msc_channels[num] = msc_get_channel(msc, num, irqcap, channel); |
paul@258 | 1273 | msc_irqs[num] = irqcap; |
paul@258 | 1274 | } |
paul@258 | 1275 | |
paul@258 | 1276 | static void enable_msc_channel(void) |
paul@258 | 1277 | { |
paul@258 | 1278 | void *channel = get_channel(num_msc_channels, msc_channels, NULL); |
paul@258 | 1279 | |
paul@258 | 1280 | if (channel == NULL) |
paul@258 | 1281 | return; |
paul@258 | 1282 | |
paul@258 | 1283 | msc_enable(channel); |
paul@258 | 1284 | } |
paul@258 | 1285 | |
paul@261 | 1286 | static void read_blocks_from_msc(void) |
paul@258 | 1287 | { |
paul@258 | 1288 | void *channel = get_channel(num_msc_channels, msc_channels, NULL); |
paul@263 | 1289 | struct dma_region *dma_region; |
paul@261 | 1290 | uint32_t card, transferred, block_address, block_count; |
paul@258 | 1291 | |
paul@258 | 1292 | if (channel == NULL) |
paul@258 | 1293 | return; |
paul@258 | 1294 | |
paul@258 | 1295 | dma_region = _get_dma_region(); |
paul@258 | 1296 | |
paul@258 | 1297 | if (dma_region == NULL) |
paul@258 | 1298 | return; |
paul@258 | 1299 | |
paul@258 | 1300 | if (!read_number("Card", &card)) |
paul@258 | 1301 | return; |
paul@258 | 1302 | |
paul@261 | 1303 | if (!read_number("Block address", &block_address)) |
paul@261 | 1304 | return; |
paul@261 | 1305 | |
paul@261 | 1306 | if (!read_number("Block count", &block_count)) |
paul@261 | 1307 | return; |
paul@261 | 1308 | |
paul@261 | 1309 | /* NOTE: Assuming 512-byte blocks. */ |
paul@261 | 1310 | |
paul@261 | 1311 | if (block_count * 512 > dma_region->size) |
paul@261 | 1312 | { |
paul@261 | 1313 | printf("Too many blocks for region.\n"); |
paul@261 | 1314 | return; |
paul@261 | 1315 | } |
paul@261 | 1316 | |
paul@258 | 1317 | l4_cache_inv_data(dma_region->vaddr, dma_region->vaddr + dma_region->size); |
paul@258 | 1318 | |
paul@264 | 1319 | transferred = msc_read_blocks(channel, (uint8_t) card, dma_region, |
paul@261 | 1320 | block_address, block_count); |
paul@258 | 1321 | |
paul@258 | 1322 | printf("Transferred: %d\n", transferred); |
paul@258 | 1323 | } |
paul@258 | 1324 | |
paul@258 | 1325 | |
paul@258 | 1326 | |
paul@237 | 1327 | /* RTC operations. */ |
paul@237 | 1328 | |
paul@298 | 1329 | static void rtc_clock_frequency(void *rtc, void *cpm) |
paul@298 | 1330 | { |
paul@298 | 1331 | enum Clock_identifiers clock = rtc_get_clock(rtc); |
paul@298 | 1332 | |
paul@298 | 1333 | printf("RTC frequency: %lld\n", cpm_get_frequency(cpm, clock)); |
paul@298 | 1334 | } |
paul@298 | 1335 | |
paul@238 | 1336 | static void rtc_reset(void *rtc, void *cpm) |
paul@238 | 1337 | { |
paul@238 | 1338 | unsigned int seconds; |
paul@238 | 1339 | |
paul@238 | 1340 | if (!read_number("Seconds", &seconds)) |
paul@238 | 1341 | return; |
paul@238 | 1342 | |
paul@298 | 1343 | enum Clock_identifiers clock = rtc_get_clock(rtc); |
paul@238 | 1344 | uint32_t rtc_seconds = rtc_get_seconds(rtc); |
paul@298 | 1345 | uint32_t value = seconds * cpm_get_frequency(cpm, clock); |
paul@238 | 1346 | |
paul@239 | 1347 | rtc_alarm_disable(rtc); |
paul@238 | 1348 | rtc_set_alarm_seconds(rtc, rtc_seconds + value); |
paul@238 | 1349 | rtc_hibernate(rtc); |
paul@238 | 1350 | } |
paul@238 | 1351 | |
paul@237 | 1352 | static void _rtc_set_seconds(void *rtc, int alarm) |
paul@237 | 1353 | { |
paul@237 | 1354 | unsigned int seconds; |
paul@237 | 1355 | |
paul@237 | 1356 | if (!read_number("Seconds", &seconds)) |
paul@237 | 1357 | return; |
paul@237 | 1358 | |
paul@237 | 1359 | (alarm ? rtc_set_alarm_seconds : rtc_set_seconds)(rtc, seconds); |
paul@237 | 1360 | } |
paul@237 | 1361 | |
paul@237 | 1362 | |
paul@237 | 1363 | |
paul@218 | 1364 | /* SPI operations. */ |
paul@218 | 1365 | |
paul@222 | 1366 | static void new_spi_channel(void *spi, void *gpio[]) |
paul@221 | 1367 | { |
paul@222 | 1368 | unsigned int control_port, control_pin, control_alt_func_input; |
paul@222 | 1369 | int control_alt_func; |
paul@222 | 1370 | void *control_chip; |
paul@221 | 1371 | int num = get_channel_number(num_spi_channels); |
paul@221 | 1372 | void *channel; |
paul@221 | 1373 | uint32_t frequency; |
paul@221 | 1374 | |
paul@221 | 1375 | if (num < 0) |
paul@221 | 1376 | return; |
paul@221 | 1377 | |
paul@221 | 1378 | channel = get_channel(num_dma_channels, dma_channels, NULL); |
paul@221 | 1379 | |
paul@221 | 1380 | if (channel == NULL) |
paul@221 | 1381 | return; |
paul@221 | 1382 | |
paul@221 | 1383 | if (!read_number("Frequency", &frequency)) |
paul@221 | 1384 | return; |
paul@221 | 1385 | |
paul@222 | 1386 | if (!get_port_and_pin(&control_port, &control_pin)) |
paul@222 | 1387 | control_chip = NULL; |
paul@222 | 1388 | else |
paul@222 | 1389 | control_chip = gpio[control_port]; |
paul@222 | 1390 | |
paul@222 | 1391 | if (!read_number("Function", &control_alt_func_input)) |
paul@222 | 1392 | control_alt_func = -1; |
paul@222 | 1393 | else |
paul@222 | 1394 | control_alt_func = control_alt_func_input; |
paul@222 | 1395 | |
paul@222 | 1396 | spi_channels[num] = spi_get_channel(spi, num, channel, frequency, |
paul@222 | 1397 | control_chip, control_pin, |
paul@222 | 1398 | control_alt_func); |
paul@221 | 1399 | } |
paul@221 | 1400 | |
paul@221 | 1401 | static void new_spi_channel_gpio(void *gpio[]) |
paul@218 | 1402 | { |
paul@222 | 1403 | unsigned int clock_port, clock_pin, data_port, data_pin, enable_port, |
paul@222 | 1404 | enable_pin, control_port, control_pin; |
paul@222 | 1405 | void *control_chip; |
paul@218 | 1406 | int num = get_channel_number(num_spi_channels); |
paul@218 | 1407 | uint32_t frequency; |
paul@218 | 1408 | |
paul@218 | 1409 | if (num < 0) |
paul@218 | 1410 | return; |
paul@218 | 1411 | |
paul@222 | 1412 | if (!read_number("Frequency", &frequency)) |
paul@222 | 1413 | return; |
paul@222 | 1414 | |
paul@218 | 1415 | if (!get_port_and_pin(&clock_port, &clock_pin)) |
paul@218 | 1416 | return; |
paul@218 | 1417 | |
paul@218 | 1418 | if (!get_port_and_pin(&data_port, &data_pin)) |
paul@218 | 1419 | return; |
paul@218 | 1420 | |
paul@218 | 1421 | if (!get_port_and_pin(&enable_port, &enable_pin)) |
paul@218 | 1422 | return; |
paul@218 | 1423 | |
paul@222 | 1424 | if (!get_port_and_pin(&control_port, &control_pin)) |
paul@222 | 1425 | control_chip = NULL; |
paul@222 | 1426 | else |
paul@222 | 1427 | control_chip = gpio[control_port]; |
paul@218 | 1428 | |
paul@222 | 1429 | spi_channels[num] = spi_get_channel_gpio(frequency, |
paul@222 | 1430 | gpio[clock_port], clock_pin, |
paul@221 | 1431 | gpio[data_port], data_pin, |
paul@221 | 1432 | gpio[enable_port], enable_pin, |
paul@222 | 1433 | control_chip, control_pin); |
paul@222 | 1434 | } |
paul@222 | 1435 | |
paul@222 | 1436 | static void spi_control(int acquire) |
paul@222 | 1437 | { |
paul@222 | 1438 | unsigned int level; |
paul@222 | 1439 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@222 | 1440 | |
paul@222 | 1441 | if (acquire) |
paul@222 | 1442 | { |
paul@222 | 1443 | if (!read_number("Level", &level)) |
paul@222 | 1444 | return; |
paul@222 | 1445 | |
paul@222 | 1446 | spi_acquire_control(channel, level); |
paul@222 | 1447 | } |
paul@222 | 1448 | else |
paul@222 | 1449 | spi_release_control(channel); |
paul@218 | 1450 | } |
paul@218 | 1451 | |
paul@235 | 1452 | static void spi_send_data(void) |
paul@218 | 1453 | { |
paul@218 | 1454 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@218 | 1455 | int bytes; |
paul@218 | 1456 | unsigned int byte; |
paul@218 | 1457 | uint8_t buffer[256]; |
paul@218 | 1458 | |
paul@218 | 1459 | bytes = 0; |
paul@218 | 1460 | |
paul@218 | 1461 | while (read_encoded_number(NULL, "%2x", &byte)) |
paul@218 | 1462 | { |
paul@218 | 1463 | buffer[bytes] = (uint8_t) (byte & 0xff); |
paul@218 | 1464 | bytes++; |
paul@218 | 1465 | } |
paul@218 | 1466 | |
paul@235 | 1467 | spi_send(channel, bytes, buffer); |
paul@221 | 1468 | } |
paul@221 | 1469 | |
paul@221 | 1470 | static void spi_send_data_units(void) |
paul@221 | 1471 | { |
paul@221 | 1472 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@221 | 1473 | unsigned int char_size, unit_size, value; |
paul@221 | 1474 | uint8_t buffer[256]; |
paul@221 | 1475 | int byte = 0; |
paul@221 | 1476 | |
paul@221 | 1477 | if (!read_number("Unit size", &unit_size)) |
paul@221 | 1478 | return; |
paul@221 | 1479 | |
paul@221 | 1480 | if (!read_number("Character size", &char_size)) |
paul@221 | 1481 | return; |
paul@221 | 1482 | |
paul@221 | 1483 | /* Read hex digits for bytes. Multiple bytes make up each unit and are read |
paul@221 | 1484 | from most to least significant. Where the unit size exceeds the character |
paul@221 | 1485 | size, the last bit before the character indicates the GPC bit. */ |
paul@221 | 1486 | |
paul@221 | 1487 | while ((byte < 256) && read_encoded_number(NULL, "%2x", &value)) |
paul@221 | 1488 | buffer[byte++] = value; |
paul@221 | 1489 | |
paul@236 | 1490 | /* Explicitly indicate big endian data. */ |
paul@236 | 1491 | |
paul@236 | 1492 | spi_send_units(channel, byte, (uint8_t *) buffer, unit_size, char_size, 1); |
paul@221 | 1493 | } |
paul@221 | 1494 | |
paul@221 | 1495 | static void spi_transfer_data(void) |
paul@221 | 1496 | { |
paul@221 | 1497 | void *channel = get_channel(num_spi_channels, spi_channels, NULL); |
paul@236 | 1498 | struct dma_region *dma_region = NULL, *desc_region; |
paul@236 | 1499 | struct memory_region *memory_region = NULL; |
paul@221 | 1500 | unsigned int char_size, unit_size; |
paul@221 | 1501 | uint32_t count, transferred; |
paul@236 | 1502 | int using_dma; |
paul@221 | 1503 | |
paul@221 | 1504 | if (channel == NULL) |
paul@221 | 1505 | return; |
paul@221 | 1506 | |
paul@236 | 1507 | if (!read_flag("Using DMA", &using_dma, "d", 1)) |
paul@236 | 1508 | return; |
paul@221 | 1509 | |
paul@236 | 1510 | if (using_dma) |
paul@236 | 1511 | dma_region = _get_dma_region(); |
paul@236 | 1512 | else |
paul@236 | 1513 | memory_region = _get_memory_region(); |
paul@236 | 1514 | |
paul@236 | 1515 | if ((dma_region == NULL) && (memory_region == NULL)) |
paul@221 | 1516 | return; |
paul@221 | 1517 | |
paul@221 | 1518 | if (!read_number("Transfer size", &count)) |
paul@221 | 1519 | return; |
paul@221 | 1520 | |
paul@221 | 1521 | if (!read_number("Unit size", &unit_size)) |
paul@221 | 1522 | return; |
paul@221 | 1523 | |
paul@221 | 1524 | if (!read_number("Character size", &char_size)) |
paul@221 | 1525 | return; |
paul@221 | 1526 | |
paul@236 | 1527 | if (using_dma) |
paul@236 | 1528 | { |
paul@236 | 1529 | desc_region = _get_dma_region(); |
paul@223 | 1530 | |
paul@236 | 1531 | transferred = spi_transfer(channel, dma_region->vaddr, dma_region->paddr, count, |
paul@236 | 1532 | unit_size, char_size, |
paul@236 | 1533 | desc_region != NULL ? desc_region->vaddr : 0, |
paul@236 | 1534 | desc_region != NULL ? desc_region->paddr : 0); |
paul@236 | 1535 | } |
paul@236 | 1536 | else |
paul@236 | 1537 | transferred = spi_transfer(channel, memory_region->addr, 0, count, |
paul@236 | 1538 | unit_size, char_size, 0, 0); |
paul@221 | 1539 | |
paul@221 | 1540 | printf("Transferred: %d\n", transferred); |
paul@218 | 1541 | } |
paul@218 | 1542 | |
paul@218 | 1543 | |
paul@218 | 1544 | |
paul@253 | 1545 | /* TCU configuration. */ |
paul@253 | 1546 | |
paul@253 | 1547 | static l4_uint32_t tcu_irq_start = 0, tcu_irq_end = 0; |
paul@253 | 1548 | |
paul@253 | 1549 | /* TCU operations. */ |
paul@253 | 1550 | |
paul@253 | 1551 | static int init_tcu(void) |
paul@253 | 1552 | { |
paul@253 | 1553 | /* Here, only one IRQ is used. */ |
paul@253 | 1554 | |
paul@253 | 1555 | tcu_irq = l4re_util_cap_alloc(); |
paul@280 | 1556 | return init_irq(tcu_irq_num, tcu_irq, tcu_irq_start, tcu_irq_end); |
paul@253 | 1557 | } |
paul@253 | 1558 | |
paul@253 | 1559 | static void disable_tcu_counter(void) |
paul@253 | 1560 | { |
paul@254 | 1561 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1562 | |
paul@253 | 1563 | if (channel == NULL) |
paul@253 | 1564 | return; |
paul@253 | 1565 | |
paul@253 | 1566 | tcu_disable(channel); |
paul@253 | 1567 | } |
paul@253 | 1568 | |
paul@253 | 1569 | static void enable_tcu_counter(void) |
paul@253 | 1570 | { |
paul@254 | 1571 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1572 | |
paul@253 | 1573 | if (channel == NULL) |
paul@253 | 1574 | return; |
paul@253 | 1575 | |
paul@253 | 1576 | tcu_enable(channel); |
paul@253 | 1577 | } |
paul@253 | 1578 | |
paul@253 | 1579 | static void new_tcu_channel(void *tcu) |
paul@253 | 1580 | { |
paul@253 | 1581 | int num = get_channel_number(num_tcu_channels); |
paul@253 | 1582 | |
paul@253 | 1583 | if (num < 0) |
paul@253 | 1584 | return; |
paul@253 | 1585 | |
paul@253 | 1586 | tcu_channels[num] = tcu_get_channel(tcu, num, tcu_irq); |
paul@253 | 1587 | } |
paul@253 | 1588 | |
paul@253 | 1589 | static void list_tcu_channels(void) |
paul@253 | 1590 | { |
paul@253 | 1591 | unsigned int num; |
paul@253 | 1592 | void *channel; |
paul@253 | 1593 | |
paul@253 | 1594 | printf(" Clock.. Counter......... Mask..............\n"); |
paul@253 | 1595 | printf("Channel Status C Pre Cnt Half Full Half Full Int\n"); |
paul@258 | 1596 | printf("------- -------- -- ---- ---- ---- ---- -------- -------- ----\n"); |
paul@253 | 1597 | |
paul@253 | 1598 | for (num = 0; num < num_tcu_channels; num++) |
paul@253 | 1599 | { |
paul@253 | 1600 | printf("%d ", num); |
paul@253 | 1601 | |
paul@253 | 1602 | channel = tcu_channels[num]; |
paul@253 | 1603 | |
paul@253 | 1604 | if (channel == NULL) |
paul@253 | 1605 | printf("inactive\n"); |
paul@253 | 1606 | else |
paul@253 | 1607 | printf("%s %d %4d %04x %04x %04x %s %s %s\n", |
paul@253 | 1608 | tcu_is_enabled(channel) ? "enabled " : "disabled", |
paul@253 | 1609 | tcu_get_clock(channel), |
paul@253 | 1610 | tcu_get_prescale(channel), |
paul@253 | 1611 | tcu_get_counter(channel), |
paul@253 | 1612 | tcu_get_half_data_value(channel), |
paul@253 | 1613 | tcu_get_full_data_value(channel), |
paul@253 | 1614 | tcu_get_half_data_mask(channel) ? "masked " : "unmasked", |
paul@253 | 1615 | tcu_get_full_data_mask(channel) ? "masked " : "unmasked", |
paul@253 | 1616 | tcu_have_interrupt(channel) ? "!" : "_"); |
paul@253 | 1617 | } |
paul@253 | 1618 | } |
paul@253 | 1619 | |
paul@253 | 1620 | static void set_tcu_counter(void) |
paul@253 | 1621 | { |
paul@254 | 1622 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1623 | uint32_t counter; |
paul@253 | 1624 | |
paul@253 | 1625 | if (channel == NULL) |
paul@253 | 1626 | return; |
paul@253 | 1627 | |
paul@253 | 1628 | if (!read_number("Value", &counter)) |
paul@253 | 1629 | return; |
paul@253 | 1630 | |
paul@253 | 1631 | tcu_set_counter(channel, counter); |
paul@253 | 1632 | } |
paul@253 | 1633 | |
paul@253 | 1634 | static void set_tcu_clock(void) |
paul@253 | 1635 | { |
paul@254 | 1636 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1637 | uint32_t clock; |
paul@253 | 1638 | |
paul@253 | 1639 | if (channel == NULL) |
paul@253 | 1640 | return; |
paul@253 | 1641 | |
paul@253 | 1642 | if (!read_number("Clock", &clock)) |
paul@253 | 1643 | return; |
paul@253 | 1644 | |
paul@253 | 1645 | tcu_set_clock(channel, clock); |
paul@253 | 1646 | } |
paul@253 | 1647 | |
paul@253 | 1648 | static void set_tcu_full(void) |
paul@253 | 1649 | { |
paul@254 | 1650 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1651 | uint32_t value; |
paul@253 | 1652 | |
paul@253 | 1653 | if (channel == NULL) |
paul@253 | 1654 | return; |
paul@253 | 1655 | |
paul@253 | 1656 | if (!read_number("Full value", &value)) |
paul@253 | 1657 | return; |
paul@253 | 1658 | |
paul@253 | 1659 | tcu_set_full_data_value(channel, value); |
paul@253 | 1660 | } |
paul@253 | 1661 | |
paul@253 | 1662 | static void set_tcu_half(void) |
paul@253 | 1663 | { |
paul@254 | 1664 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1665 | uint32_t value; |
paul@253 | 1666 | |
paul@253 | 1667 | if (channel == NULL) |
paul@253 | 1668 | return; |
paul@253 | 1669 | |
paul@253 | 1670 | if (!read_number("Half value", &value)) |
paul@253 | 1671 | return; |
paul@253 | 1672 | |
paul@253 | 1673 | tcu_set_half_data_value(channel, value); |
paul@253 | 1674 | } |
paul@253 | 1675 | |
paul@253 | 1676 | static void set_tcu_mask(int full_mask, int masked) |
paul@253 | 1677 | { |
paul@254 | 1678 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1679 | |
paul@253 | 1680 | if (channel == NULL) |
paul@253 | 1681 | return; |
paul@253 | 1682 | |
paul@253 | 1683 | if (full_mask) |
paul@253 | 1684 | tcu_set_full_data_mask(channel, masked); |
paul@253 | 1685 | else |
paul@253 | 1686 | tcu_set_half_data_mask(channel, masked); |
paul@253 | 1687 | } |
paul@253 | 1688 | |
paul@253 | 1689 | static void set_tcu_prescale(void) |
paul@253 | 1690 | { |
paul@254 | 1691 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1692 | uint32_t prescale; |
paul@253 | 1693 | |
paul@253 | 1694 | if (channel == NULL) |
paul@253 | 1695 | return; |
paul@253 | 1696 | |
paul@253 | 1697 | if (!read_number("Prescale", &prescale)) |
paul@253 | 1698 | return; |
paul@253 | 1699 | |
paul@253 | 1700 | tcu_set_prescale(channel, prescale); |
paul@253 | 1701 | } |
paul@253 | 1702 | |
paul@253 | 1703 | static void tcu_wait(void) |
paul@253 | 1704 | { |
paul@254 | 1705 | void *channel = get_channel(num_tcu_channels, tcu_channels, NULL); |
paul@253 | 1706 | uint32_t timeout; |
paul@253 | 1707 | |
paul@253 | 1708 | if (channel == NULL) |
paul@253 | 1709 | return; |
paul@253 | 1710 | |
paul@253 | 1711 | if (!read_number("Timeout", &timeout)) |
paul@253 | 1712 | return; |
paul@253 | 1713 | |
paul@253 | 1714 | printf("IRQ received: %s\n", tcu_wait_for_irq(channel, timeout) ? "yes" : "no"); |
paul@253 | 1715 | } |
paul@253 | 1716 | |
paul@253 | 1717 | |
paul@253 | 1718 | |
paul@218 | 1719 | /* Command processing. */ |
paul@218 | 1720 | |
paul@218 | 1721 | static void handle_aic(void *aic) |
paul@218 | 1722 | { |
paul@218 | 1723 | char *token; |
paul@218 | 1724 | |
paul@218 | 1725 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1726 | { |
paul@218 | 1727 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1728 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1729 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1730 | new_aic_channel(aic); |
paul@218 | 1731 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1732 | aic_transfer_data(); |
paul@218 | 1733 | else |
paul@236 | 1734 | printf("aic channel | list | transfer\n"); |
paul@218 | 1735 | } |
paul@218 | 1736 | else |
paul@218 | 1737 | list_channels(num_aic_channels, aic_channels); |
paul@218 | 1738 | } |
paul@218 | 1739 | |
paul@218 | 1740 | static void handle_cpm(void *cpm) |
paul@218 | 1741 | { |
paul@218 | 1742 | char *token; |
paul@218 | 1743 | |
paul@218 | 1744 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1745 | { |
paul@218 | 1746 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1747 | list_clocks(cpm); |
paul@218 | 1748 | else if (!strcmp(token, "f") || !strcmp(token, "frequency")) |
paul@218 | 1749 | set_frequency(cpm); |
paul@218 | 1750 | else if (!strcmp(token, "p") || !strcmp(token, "parameters")) |
paul@218 | 1751 | set_parameters(cpm); |
paul@218 | 1752 | else if (!strcmp(token, "s") || !strcmp(token, "source")) |
paul@218 | 1753 | set_source(cpm); |
paul@218 | 1754 | else if (!strcmp(token, "start")) |
paul@218 | 1755 | start_clock(cpm); |
paul@218 | 1756 | else if (!strcmp(token, "stop")) |
paul@218 | 1757 | stop_clock(cpm); |
paul@218 | 1758 | else |
paul@218 | 1759 | printf("cpm list | frequency | parameters | source | start | stop\n"); |
paul@218 | 1760 | } |
paul@218 | 1761 | else |
paul@218 | 1762 | list_clocks(cpm); |
paul@218 | 1763 | } |
paul@218 | 1764 | |
paul@218 | 1765 | static void handle_dma(void *dma) |
paul@218 | 1766 | { |
paul@218 | 1767 | char *token; |
paul@218 | 1768 | |
paul@218 | 1769 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1770 | { |
paul@218 | 1771 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1772 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1773 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1774 | new_dma_channel(dma); |
paul@218 | 1775 | else if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@218 | 1776 | new_dma_region(); |
paul@261 | 1777 | else if (!strcmp(token, "R") || !strcmp(token, "show")) |
paul@261 | 1778 | show_dma_region(); |
paul@218 | 1779 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@218 | 1780 | set_dma_region(); |
paul@218 | 1781 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@218 | 1782 | dma_transfer_data(); |
paul@218 | 1783 | else |
paul@218 | 1784 | printf("dma channel | list | region | set | transfer\n"); |
paul@218 | 1785 | } |
paul@218 | 1786 | else |
paul@218 | 1787 | list_channels(num_dma_channels, dma_channels); |
paul@218 | 1788 | } |
paul@218 | 1789 | |
paul@218 | 1790 | static void handle_gpio(void *gpio[]) |
paul@218 | 1791 | { |
paul@218 | 1792 | char *token; |
paul@218 | 1793 | |
paul@218 | 1794 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1795 | { |
paul@218 | 1796 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1797 | list_gpios(gpio); |
paul@218 | 1798 | else if (!strcmp(token, "a") || !strcmp(token, "alt")) |
paul@218 | 1799 | set_gpio_alt_func(gpio); |
paul@218 | 1800 | else if (!strcmp(token, "i") || !strcmp(token, "io")) |
paul@218 | 1801 | set_gpio_pad(gpio); |
paul@218 | 1802 | else if (!strcmp(token, "p") || !strcmp(token, "pull")) |
paul@218 | 1803 | set_gpio_pull(gpio); |
paul@218 | 1804 | else |
paul@218 | 1805 | printf("gpio list | alt | io | pull\n"); |
paul@218 | 1806 | } |
paul@218 | 1807 | else |
paul@218 | 1808 | list_gpios(gpio); |
paul@218 | 1809 | } |
paul@218 | 1810 | |
paul@218 | 1811 | static void handle_i2c(void *i2c) |
paul@218 | 1812 | { |
paul@218 | 1813 | char *token; |
paul@218 | 1814 | |
paul@218 | 1815 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1816 | { |
paul@218 | 1817 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1818 | list_i2c_channels(); |
paul@218 | 1819 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@218 | 1820 | new_i2c_channel(i2c); |
paul@218 | 1821 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@218 | 1822 | i2c_get(); |
paul@218 | 1823 | else if (!strcmp(token, "s") || !strcmp(token, "scan")) |
paul@218 | 1824 | i2c_scan(); |
paul@218 | 1825 | else |
paul@218 | 1826 | printf("i2c channel | get | list | scan\n"); |
paul@218 | 1827 | } |
paul@218 | 1828 | else |
paul@218 | 1829 | list_i2c_channels(); |
paul@218 | 1830 | } |
paul@218 | 1831 | |
paul@236 | 1832 | static void handle_memory(void) |
paul@236 | 1833 | { |
paul@236 | 1834 | char *token; |
paul@236 | 1835 | |
paul@236 | 1836 | if ((token = read_token(NULL)) != NULL) |
paul@236 | 1837 | { |
paul@236 | 1838 | if (!strcmp(token, "r") || !strcmp(token, "region")) |
paul@236 | 1839 | new_memory_region(); |
paul@236 | 1840 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@236 | 1841 | set_memory_region(); |
paul@236 | 1842 | else |
paul@236 | 1843 | printf("memory region | set\n"); |
paul@236 | 1844 | } |
paul@236 | 1845 | else |
paul@236 | 1846 | list_memory_regions(); |
paul@236 | 1847 | } |
paul@236 | 1848 | |
paul@258 | 1849 | static void handle_msc(void *msc) |
paul@258 | 1850 | { |
paul@258 | 1851 | char *token; |
paul@258 | 1852 | |
paul@258 | 1853 | if ((token = read_token(NULL)) != NULL) |
paul@258 | 1854 | { |
paul@258 | 1855 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@258 | 1856 | list_msc_channels(); |
paul@263 | 1857 | else if (!strcmp(token, "C") || !strcmp(token, "cards")) |
paul@263 | 1858 | list_msc_cards(); |
paul@258 | 1859 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@258 | 1860 | new_msc_channel(msc); |
paul@258 | 1861 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@258 | 1862 | enable_msc_channel(); |
paul@263 | 1863 | else if (!strcmp(token, "p") || !strcmp(token, "partitions")) |
paul@263 | 1864 | list_msc_card_partitions(); |
paul@258 | 1865 | else if (!strcmp(token, "r") || !strcmp(token, "read")) |
paul@261 | 1866 | read_blocks_from_msc(); |
paul@258 | 1867 | else |
paul@263 | 1868 | printf("msc cards | channel | enable | partitions | read\n"); |
paul@258 | 1869 | } |
paul@258 | 1870 | else |
paul@258 | 1871 | list_msc_channels(); |
paul@258 | 1872 | } |
paul@258 | 1873 | |
paul@238 | 1874 | static void handle_rtc(void *rtc, void *cpm) |
paul@237 | 1875 | { |
paul@237 | 1876 | char *token; |
paul@237 | 1877 | |
paul@237 | 1878 | if ((token = read_token(NULL)) != NULL) |
paul@237 | 1879 | { |
paul@298 | 1880 | if (!strcmp(token, "c") || !strcmp(token, "clock-frequency")) |
paul@298 | 1881 | rtc_clock_frequency(rtc, cpm); |
paul@298 | 1882 | else if (!strcmp(token, "d") || !strcmp(token, "disable")) |
paul@237 | 1883 | rtc_disable(rtc); |
paul@237 | 1884 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@237 | 1885 | rtc_enable(rtc); |
paul@237 | 1886 | else if (!strcmp(token, "g") || !strcmp(token, "get")) |
paul@237 | 1887 | printf("seconds = %d\n", rtc_get_seconds(rtc)); |
paul@237 | 1888 | else if (!strcmp(token, "ga") || !strcmp(token, "get-alarm")) |
paul@237 | 1889 | printf("seconds = %d\n", rtc_get_alarm_seconds(rtc)); |
paul@237 | 1890 | else if (!strcmp(token, "p") || !strcmp(token, "power-down")) |
paul@237 | 1891 | rtc_power_down(rtc); |
paul@238 | 1892 | else if (!strcmp(token, "r") || !strcmp(token, "reset")) |
paul@238 | 1893 | rtc_reset(rtc, cpm); |
paul@237 | 1894 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@237 | 1895 | _rtc_set_seconds(rtc, 0); |
paul@237 | 1896 | else if (!strcmp(token, "sa") || !strcmp(token, "set-alarm")) |
paul@237 | 1897 | _rtc_set_seconds(rtc, 1); |
paul@237 | 1898 | else |
paul@298 | 1899 | printf("rtc clock-frequency | disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); |
paul@237 | 1900 | } |
paul@237 | 1901 | else |
paul@298 | 1902 | printf("rtc clock-frequency | disable | enable | get | get-alarm | power-down | reset | set | set-alarm\n"); |
paul@237 | 1903 | } |
paul@237 | 1904 | |
paul@221 | 1905 | static void handle_spi(void *spi, void *gpio[]) |
paul@218 | 1906 | { |
paul@218 | 1907 | char *token; |
paul@218 | 1908 | |
paul@218 | 1909 | if ((token = read_token(NULL)) != NULL) |
paul@218 | 1910 | { |
paul@218 | 1911 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@218 | 1912 | list_channels(num_spi_channels, spi_channels); |
paul@222 | 1913 | else if (!strcmp(token, "a") || !strcmp(token, "control-acquire")) |
paul@222 | 1914 | spi_control(1); |
paul@222 | 1915 | else if (!strcmp(token, "r") || !strcmp(token, "control-release")) |
paul@222 | 1916 | spi_control(0); |
paul@218 | 1917 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@222 | 1918 | new_spi_channel(spi, gpio); |
paul@221 | 1919 | else if (!strcmp(token, "g") || !strcmp(token, "gpio") || !strcmp(token, "gpio-channel")) |
paul@221 | 1920 | new_spi_channel_gpio(gpio); |
paul@218 | 1921 | else if (!strcmp(token, "s") || !strcmp(token, "send")) |
paul@235 | 1922 | spi_send_data(); |
paul@221 | 1923 | else if (!strcmp(token, "S") || !strcmp(token, "send-units")) |
paul@221 | 1924 | spi_send_data_units(); |
paul@221 | 1925 | else if (!strcmp(token, "t") || !strcmp(token, "transfer")) |
paul@221 | 1926 | spi_transfer_data(); |
paul@218 | 1927 | else |
paul@222 | 1928 | printf("spi channel | control-acquire | control-release | gpio-channel | list | send | send-units | transfer\n"); |
paul@218 | 1929 | } |
paul@218 | 1930 | else |
paul@218 | 1931 | list_channels(num_spi_channels, spi_channels); |
paul@218 | 1932 | } |
paul@218 | 1933 | |
paul@253 | 1934 | static void handle_tcu(void *tcu) |
paul@253 | 1935 | { |
paul@253 | 1936 | char *token; |
paul@253 | 1937 | |
paul@253 | 1938 | if ((token = read_token(NULL)) != NULL) |
paul@253 | 1939 | { |
paul@253 | 1940 | if (!strcmp(token, "l") || !strcmp(token, "list")) |
paul@253 | 1941 | list_tcu_channels(); |
paul@253 | 1942 | else if (!strcmp(token, "c") || !strcmp(token, "channel")) |
paul@253 | 1943 | new_tcu_channel(tcu); |
paul@253 | 1944 | else if (!strcmp(token, "C") || !strcmp(token, "clock")) |
paul@253 | 1945 | set_tcu_clock(); |
paul@253 | 1946 | else if (!strcmp(token, "d") || !strcmp(token, "disable")) |
paul@253 | 1947 | disable_tcu_counter(); |
paul@253 | 1948 | else if (!strcmp(token, "e") || !strcmp(token, "enable")) |
paul@253 | 1949 | enable_tcu_counter(); |
paul@253 | 1950 | else if (!strcmp(token, "f") || !strcmp(token, "full")) |
paul@253 | 1951 | set_tcu_full(); |
paul@253 | 1952 | else if (!strcmp(token, "fm") || !strcmp(token, "full-mask")) |
paul@253 | 1953 | set_tcu_mask(1, 1); |
paul@253 | 1954 | else if (!strcmp(token, "fu") || !strcmp(token, "full-unmask")) |
paul@253 | 1955 | set_tcu_mask(1, 0); |
paul@253 | 1956 | else if (!strcmp(token, "h") || !strcmp(token, "half")) |
paul@253 | 1957 | set_tcu_half(); |
paul@253 | 1958 | else if (!strcmp(token, "hm") || !strcmp(token, "half-mask")) |
paul@253 | 1959 | set_tcu_mask(0, 1); |
paul@253 | 1960 | else if (!strcmp(token, "hu") || !strcmp(token, "half-unmask")) |
paul@253 | 1961 | set_tcu_mask(0, 0); |
paul@253 | 1962 | else if (!strcmp(token, "p") || !strcmp(token, "prescale")) |
paul@253 | 1963 | set_tcu_prescale(); |
paul@253 | 1964 | else if (!strcmp(token, "s") || !strcmp(token, "set")) |
paul@253 | 1965 | set_tcu_counter(); |
paul@253 | 1966 | else if (!strcmp(token, "w") || !strcmp(token, "wait")) |
paul@253 | 1967 | tcu_wait(); |
paul@253 | 1968 | else |
paul@253 | 1969 | printf("tcu channel | clock | disable | enable | full | full-mask | " \ |
paul@269 | 1970 | "full-unmask | half | half-mask | half-unmask | list | " \ |
paul@269 | 1971 | "prescale | set | wait\n"); |
paul@253 | 1972 | } |
paul@253 | 1973 | else |
paul@253 | 1974 | list_tcu_channels(); |
paul@253 | 1975 | } |
paul@253 | 1976 | |
paul@218 | 1977 | |
paul@218 | 1978 | |
paul@218 | 1979 | int main(void) |
paul@218 | 1980 | { |
paul@218 | 1981 | l4_addr_t aic_base = 0, aic_base_end = 0; |
paul@218 | 1982 | l4_addr_t aic_phys_base = 0, aic_phys_base_end = 0; |
paul@218 | 1983 | l4_addr_t cpm_base = 0, cpm_base_end = 0; |
paul@218 | 1984 | l4_addr_t dma_base = 0, dma_base_end = 0; |
paul@218 | 1985 | l4_addr_t gpio_base = 0, gpio_base_end = 0; |
paul@218 | 1986 | l4_addr_t i2c_base = 0, i2c_base_end = 0; |
paul@258 | 1987 | l4_addr_t msc_base = 0, msc_base_end = 0; |
paul@258 | 1988 | l4_addr_t msc_phys_base = 0, msc_phys_base_end = 0; |
paul@237 | 1989 | l4_addr_t rtc_base = 0, rtc_base_end = 0; |
paul@221 | 1990 | l4_addr_t ssi_base = 0, ssi_base_end = 0; |
paul@221 | 1991 | l4_addr_t ssi_phys_base = 0, ssi_phys_base_end = 0; |
paul@253 | 1992 | l4_addr_t tcu_base = 0, tcu_base_end = 0; |
paul@258 | 1993 | void *aic, *cpm, *dma, *gpio[num_gpio_ports], *i2c, *msc, *rtc, *spi, *tcu; |
paul@218 | 1994 | int result = 0; |
paul@218 | 1995 | unsigned int port; |
paul@218 | 1996 | |
paul@218 | 1997 | icucap = l4re_env_get_cap("icu"); |
paul@218 | 1998 | |
paul@218 | 1999 | /* Obtain resource details describing I/O memory. */ |
paul@218 | 2000 | |
paul@218 | 2001 | printf("Access CPM...\n"); |
paul@218 | 2002 | |
paul@236 | 2003 | if ((result = get_memory(io_memory_regions[CPM], &cpm_base, &cpm_base_end)) < 0) |
paul@218 | 2004 | return 1; |
paul@218 | 2005 | |
paul@218 | 2006 | printf("CPM at 0x%lx...0x%lx.\n", cpm_base, cpm_base_end); |
paul@218 | 2007 | |
paul@218 | 2008 | cpm = cpm_init(cpm_base); |
paul@218 | 2009 | |
paul@218 | 2010 | printf("Access DMA...\n"); |
paul@218 | 2011 | |
paul@236 | 2012 | if ((result = get_memory(io_memory_regions[DMA], &dma_base, &dma_base_end)) < 0) |
paul@218 | 2013 | return 1; |
paul@218 | 2014 | |
paul@218 | 2015 | printf("DMA at 0x%lx...0x%lx.\n", dma_base, dma_base_end); |
paul@218 | 2016 | |
paul@218 | 2017 | dma = dma_init(dma_base, dma_base_end, cpm); |
paul@218 | 2018 | |
paul@236 | 2019 | if (get_irq(io_memory_regions[DMA], &dma_irq_start, &dma_irq_end) < 0) |
paul@218 | 2020 | return 1; |
paul@218 | 2021 | |
paul@218 | 2022 | printf("IRQ range at %d...%d.\n", dma_irq_start, dma_irq_end); |
paul@218 | 2023 | |
paul@218 | 2024 | if (init_dma()) |
paul@218 | 2025 | return 1; |
paul@218 | 2026 | |
paul@218 | 2027 | dma_enable(dma); |
paul@218 | 2028 | |
paul@218 | 2029 | printf("Access GPIO...\n"); |
paul@218 | 2030 | |
paul@236 | 2031 | if ((result = get_memory(io_memory_regions[GPIO], &gpio_base, &gpio_base_end)) < 0) |
paul@218 | 2032 | return 1; |
paul@218 | 2033 | |
paul@218 | 2034 | printf("GPIO at 0x%lx...0x%lx.\n", gpio_base, gpio_base_end); |
paul@218 | 2035 | |
paul@218 | 2036 | for (port = 0; port < num_gpio_ports; port++) |
paul@285 | 2037 | gpio[port] = gpio_init_shadow(gpio_base, port); |
paul@218 | 2038 | |
paul@218 | 2039 | printf("Access I2C...\n"); |
paul@218 | 2040 | |
paul@236 | 2041 | if ((result = get_memory(io_memory_regions[I2C], &i2c_base, &i2c_base_end)) < 0) |
paul@218 | 2042 | return 1; |
paul@218 | 2043 | |
paul@218 | 2044 | printf("I2C at 0x%lx...0x%lx.\n", i2c_base, i2c_base_end); |
paul@218 | 2045 | |
paul@218 | 2046 | i2c = i2c_init(i2c_base, i2c_base_end, cpm, 100000); |
paul@218 | 2047 | |
paul@236 | 2048 | if (get_irq(io_memory_regions[I2C], &i2c_irq_start, &i2c_irq_end) < 0) |
paul@218 | 2049 | return 1; |
paul@218 | 2050 | |
paul@218 | 2051 | printf("IRQ range at %d...%d.\n", i2c_irq_start, i2c_irq_end); |
paul@218 | 2052 | |
paul@218 | 2053 | printf("Access AIC...\n"); |
paul@218 | 2054 | |
paul@236 | 2055 | if ((result = get_memory_complete(io_memory_regions[AIC], &aic_base, &aic_base_end, |
paul@218 | 2056 | &aic_phys_base, &aic_phys_base_end)) < 0) |
paul@218 | 2057 | return 1; |
paul@218 | 2058 | |
paul@218 | 2059 | printf("AIC at 0x%lx...0x%lx.\n", aic_base, aic_base_end); |
paul@218 | 2060 | |
paul@218 | 2061 | aic = aic_init(aic_phys_base, aic_base, aic_base_end, cpm); |
paul@218 | 2062 | |
paul@258 | 2063 | printf("Access MSC...\n"); |
paul@258 | 2064 | |
paul@258 | 2065 | if ((result = get_memory_complete(io_memory_regions[MSC], &msc_base, &msc_base_end, |
paul@258 | 2066 | &msc_phys_base, &msc_phys_base_end)) < 0) |
paul@258 | 2067 | return 1; |
paul@258 | 2068 | |
paul@258 | 2069 | printf("MSC at 0x%lx...0x%lx.\n", msc_base, msc_base_end); |
paul@258 | 2070 | |
paul@264 | 2071 | msc = msc_init(msc_phys_base, msc_base, msc_base_end, cpm); |
paul@258 | 2072 | |
paul@258 | 2073 | if (get_irq(io_memory_regions[MSC], &msc_irq_start, &msc_irq_end) < 0) |
paul@258 | 2074 | return 1; |
paul@258 | 2075 | |
paul@258 | 2076 | printf("IRQ range at %d...%d.\n", msc_irq_start, msc_irq_end); |
paul@258 | 2077 | |
paul@237 | 2078 | printf("Access RTC...\n"); |
paul@237 | 2079 | |
paul@237 | 2080 | if ((result = get_memory(io_memory_regions[RTC], &rtc_base, &rtc_base_end)) < 0) |
paul@237 | 2081 | return 1; |
paul@237 | 2082 | |
paul@237 | 2083 | printf("RTC at 0x%lx...0x%lx.\n", rtc_base, rtc_base_end); |
paul@237 | 2084 | |
paul@240 | 2085 | rtc = rtc_init(rtc_base, cpm); |
paul@237 | 2086 | |
paul@221 | 2087 | printf("Access SSI...\n"); |
paul@221 | 2088 | |
paul@236 | 2089 | if ((result = get_memory_complete(io_memory_regions[SSI], &ssi_base, &ssi_base_end, |
paul@221 | 2090 | &ssi_phys_base, &ssi_phys_base_end)) < 0) |
paul@218 | 2091 | return 1; |
paul@218 | 2092 | |
paul@221 | 2093 | printf("SSI at 0x%lx...0x%lx.\n", ssi_base, ssi_base_end); |
paul@221 | 2094 | |
paul@221 | 2095 | spi = spi_init(ssi_phys_base, ssi_base, ssi_base_end, cpm); |
paul@218 | 2096 | |
paul@253 | 2097 | printf("Access TCU...\n"); |
paul@253 | 2098 | |
paul@253 | 2099 | if ((result = get_memory(io_memory_regions[TCU], &tcu_base, &tcu_base_end)) < 0) |
paul@253 | 2100 | return 1; |
paul@253 | 2101 | |
paul@253 | 2102 | printf("TCU at 0x%lx...0x%lx.\n", tcu_base, tcu_base_end); |
paul@253 | 2103 | |
paul@253 | 2104 | tcu = tcu_init(tcu_base, tcu_base_end); |
paul@253 | 2105 | |
paul@253 | 2106 | if (get_irq(io_memory_regions[TCU], &tcu_irq_start, &tcu_irq_end) < 0) |
paul@253 | 2107 | return 1; |
paul@253 | 2108 | |
paul@253 | 2109 | printf("IRQ range at %d...%d.\n", tcu_irq_start, tcu_irq_end); |
paul@253 | 2110 | |
paul@253 | 2111 | if (init_tcu()) |
paul@253 | 2112 | return 1; |
paul@253 | 2113 | |
paul@218 | 2114 | /* Start the interactive session. */ |
paul@218 | 2115 | |
paul@258 | 2116 | printf("aic, cpm, dma, gpio, i2c, msc, rtc, spi, tcu\n"); |
paul@218 | 2117 | |
paul@218 | 2118 | while (1) |
paul@218 | 2119 | { |
paul@218 | 2120 | char cmdline[256], *token; |
paul@218 | 2121 | |
paul@218 | 2122 | printf("> "); |
paul@218 | 2123 | |
paul@221 | 2124 | token = fgets(cmdline, 256, stdin); |
paul@218 | 2125 | |
paul@218 | 2126 | if (token == NULL) |
paul@218 | 2127 | break; |
paul@218 | 2128 | |
paul@218 | 2129 | if ((token = strtok(cmdline, delimiters)) == NULL) |
paul@218 | 2130 | continue; |
paul@218 | 2131 | |
paul@218 | 2132 | /* AIC/I2S commands. */ |
paul@218 | 2133 | |
paul@218 | 2134 | if (!strcmp(token, "a") || !strcmp(token, "aic") || !strcmp(token, "i2s")) |
paul@218 | 2135 | handle_aic(aic); |
paul@218 | 2136 | |
paul@218 | 2137 | /* CPM commands. */ |
paul@218 | 2138 | |
paul@218 | 2139 | else if (!strcmp(token, "c") || !strcmp(token, "cpm")) |
paul@218 | 2140 | handle_cpm(cpm); |
paul@218 | 2141 | |
paul@218 | 2142 | /* DMA commands. */ |
paul@218 | 2143 | |
paul@218 | 2144 | else if (!strcmp(token, "d") || !strcmp(token, "dma")) |
paul@218 | 2145 | handle_dma(dma); |
paul@218 | 2146 | |
paul@218 | 2147 | /* GPIO commands. */ |
paul@218 | 2148 | |
paul@218 | 2149 | else if (!strcmp(token, "g") || !strcmp(token, "gpio")) |
paul@218 | 2150 | handle_gpio(gpio); |
paul@218 | 2151 | |
paul@218 | 2152 | /* I2C commands. */ |
paul@218 | 2153 | |
paul@218 | 2154 | else if (!strcmp(token, "i") || !strcmp(token, "i2c")) |
paul@218 | 2155 | handle_i2c(i2c); |
paul@218 | 2156 | |
paul@258 | 2157 | /* MSC commands. */ |
paul@258 | 2158 | |
paul@258 | 2159 | else if (!strcmp(token, "M") || !strcmp(token, "msc")) |
paul@258 | 2160 | handle_msc(msc); |
paul@258 | 2161 | |
paul@236 | 2162 | /* Generic memory commands. */ |
paul@236 | 2163 | |
paul@236 | 2164 | else if (!strcmp(token, "m") || !strcmp(token, "mem") || !strcmp(token, "memory")) |
paul@236 | 2165 | handle_memory(); |
paul@236 | 2166 | |
paul@237 | 2167 | /* RTC commands. */ |
paul@237 | 2168 | |
paul@237 | 2169 | else if (!strcmp(token, "r") || !strcmp(token, "rtc")) |
paul@238 | 2170 | handle_rtc(rtc, cpm); |
paul@237 | 2171 | |
paul@218 | 2172 | /* SPI commands. */ |
paul@218 | 2173 | |
paul@218 | 2174 | else if (!strcmp(token, "s") || !strcmp(token, "spi")) |
paul@221 | 2175 | handle_spi(spi, gpio); |
paul@218 | 2176 | |
paul@253 | 2177 | /* TCU commands. */ |
paul@253 | 2178 | |
paul@253 | 2179 | else if (!strcmp(token, "t") || !strcmp(token, "tcu")) |
paul@253 | 2180 | handle_tcu(tcu); |
paul@253 | 2181 | |
paul@221 | 2182 | /* Comments and blank lines. */ |
paul@221 | 2183 | |
paul@221 | 2184 | else if (strncmp(token, "#", 1) && strlen(token)) |
paul@218 | 2185 | printf("Command?\n"); |
paul@218 | 2186 | } |
paul@218 | 2187 | |
paul@218 | 2188 | printf("End of session.\n"); |
paul@218 | 2189 | return 0; |
paul@218 | 2190 | } |