net: merge bugfix: Marvell Kirkwood gigabit ethernet driver
[u-boot-kw.git] / api / api.c
blob385a7e3cdc507ea302d4607a8ea75a35c552e07b
1 /*
2 * (C) Copyright 2007 Semihalf
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
26 #include <config.h>
27 #include <command.h>
28 #include <common.h>
29 #include <malloc.h>
30 #include <environment.h>
31 #include <linux/types.h>
32 #include <api_public.h>
34 #include "api_private.h"
36 #define DEBUG
37 #undef DEBUG
39 /* U-Boot routines needed */
40 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
42 /*****************************************************************************
44 * This is the API core.
46 * API_ functions are part of U-Boot code and constitute the lowest level
47 * calls:
49 * - they know what values they need as arguments
50 * - their direct return value pertains to the API_ "shell" itself (0 on
51 * success, some error code otherwise)
52 * - if the call returns a value it is buried within arguments
54 ****************************************************************************/
56 #ifdef DEBUG
57 #define debugf(fmt, args...) do { printf("%s(): ", __func__); printf(fmt, ##args); } while (0)
58 #else
59 #define debugf(fmt, args...)
60 #endif
62 typedef int (*cfp_t)(va_list argp);
64 static int calls_no;
67 * pseudo signature:
69 * int API_getc(int *c)
71 static int API_getc(va_list ap)
73 int *c;
75 if ((c = (int *)va_arg(ap, u_int32_t)) == NULL)
76 return API_EINVAL;
78 *c = getc();
79 return 0;
83 * pseudo signature:
85 * int API_tstc(int *c)
87 static int API_tstc(va_list ap)
89 int *t;
91 if ((t = (int *)va_arg(ap, u_int32_t)) == NULL)
92 return API_EINVAL;
94 *t = tstc();
95 return 0;
99 * pseudo signature:
101 * int API_putc(char *ch)
103 static int API_putc(va_list ap)
105 char *c;
107 if ((c = (char *)va_arg(ap, u_int32_t)) == NULL)
108 return API_EINVAL;
110 putc(*c);
111 return 0;
115 * pseudo signature:
117 * int API_puts(char **s)
119 static int API_puts(va_list ap)
121 char *s;
123 if ((s = (char *)va_arg(ap, u_int32_t)) == NULL)
124 return API_EINVAL;
126 puts(s);
127 return 0;
131 * pseudo signature:
133 * int API_reset(void)
135 static int API_reset(va_list ap)
137 do_reset(NULL, 0, 0, NULL);
139 /* NOT REACHED */
140 return 0;
144 * pseudo signature:
146 * int API_get_sys_info(struct sys_info *si)
148 * fill out the sys_info struct containing selected parameters about the
149 * machine
151 static int API_get_sys_info(va_list ap)
153 struct sys_info *si;
155 si = (struct sys_info *)va_arg(ap, u_int32_t);
156 if (si == NULL)
157 return API_ENOMEM;
159 return (platform_sys_info(si)) ? 0 : API_ENODEV;
163 * pseudo signature:
165 * int API_udelay(unsigned long *udelay)
167 static int API_udelay(va_list ap)
169 unsigned long *d;
171 if ((d = (unsigned long *)va_arg(ap, u_int32_t)) == NULL)
172 return API_EINVAL;
174 udelay(*d);
175 return 0;
179 * pseudo signature:
181 * int API_get_timer(unsigned long *current, unsigned long *base)
183 static int API_get_timer(va_list ap)
185 unsigned long *base, *cur;
187 cur = (unsigned long *)va_arg(ap, u_int32_t);
188 if (cur == NULL)
189 return API_EINVAL;
191 base = (unsigned long *)va_arg(ap, u_int32_t);
192 if (base == NULL)
193 return API_EINVAL;
195 *cur = get_timer(*base);
196 return 0;
200 /*****************************************************************************
202 * pseudo signature:
204 * int API_dev_enum(struct device_info *)
207 * cookies uniqely identify the previously enumerated device instance and
208 * provide a hint for what to inspect in current enum iteration:
210 * - net: &eth_device struct address from list pointed to by eth_devices
212 * - storage: block_dev_desc_t struct address from &ide_dev_desc[n],
213 * &scsi_dev_desc[n] and similar tables
215 ****************************************************************************/
217 static int API_dev_enum(va_list ap)
219 struct device_info *di;
221 /* arg is ptr to the device_info struct we are going to fill out */
222 di = (struct device_info *)va_arg(ap, u_int32_t);
223 if (di == NULL)
224 return API_EINVAL;
226 if (di->cookie == NULL) {
227 /* start over - clean up enumeration */
228 dev_enum_reset(); /* XXX shouldn't the name contain 'stor'? */
229 debugf("RESTART ENUM\n");
231 /* net device enumeration first */
232 if (dev_enum_net(di))
233 return 0;
237 * The hidden assumption is there can only be one active network
238 * device and it is identified upon enumeration (re)start, so there's
239 * no point in trying to find network devices in other cases than the
240 * (re)start and hence the 'next' device can only be storage
242 if (!dev_enum_storage(di))
243 /* make sure we mark there are no more devices */
244 di->cookie = NULL;
246 return 0;
250 static int API_dev_open(va_list ap)
252 struct device_info *di;
253 int err = 0;
255 /* arg is ptr to the device_info struct */
256 di = (struct device_info *)va_arg(ap, u_int32_t);
257 if (di == NULL)
258 return API_EINVAL;
260 /* Allow only one consumer of the device at a time */
261 if (di->state == DEV_STA_OPEN)
262 return API_EBUSY;
264 if (di->cookie == NULL)
265 return API_ENODEV;
267 if (di->type & DEV_TYP_STOR)
268 err = dev_open_stor(di->cookie);
270 else if (di->type & DEV_TYP_NET)
271 err = dev_open_net(di->cookie);
272 else
273 err = API_ENODEV;
275 if (!err)
276 di->state = DEV_STA_OPEN;
278 return err;
282 static int API_dev_close(va_list ap)
284 struct device_info *di;
285 int err = 0;
287 /* arg is ptr to the device_info struct */
288 di = (struct device_info *)va_arg(ap, u_int32_t);
289 if (di == NULL)
290 return API_EINVAL;
292 if (di->state == DEV_STA_CLOSED)
293 return 0;
295 if (di->cookie == NULL)
296 return API_ENODEV;
298 if (di->type & DEV_TYP_STOR)
299 err = dev_close_stor(di->cookie);
301 else if (di->type & DEV_TYP_NET)
302 err = dev_close_net(di->cookie);
303 else
305 * In case of unknown device we cannot change its state, so
306 * only return error code
308 err = API_ENODEV;
310 if (!err)
311 di->state = DEV_STA_CLOSED;
313 return err;
318 * Notice: this is for sending network packets only, as U-Boot does not
319 * support writing to storage at the moment (12.2007)
321 * pseudo signature:
323 * int API_dev_write(
324 * struct device_info *di,
325 * void *buf,
326 * int *len
329 * buf: ptr to buffer from where to get the data to send
331 * len: length of packet to be sent (in bytes)
334 static int API_dev_write(va_list ap)
336 struct device_info *di;
337 void *buf;
338 int *len;
339 int err = 0;
341 /* 1. arg is ptr to the device_info struct */
342 di = (struct device_info *)va_arg(ap, u_int32_t);
343 if (di == NULL)
344 return API_EINVAL;
346 /* XXX should we check if device is open? i.e. the ->state ? */
348 if (di->cookie == NULL)
349 return API_ENODEV;
351 /* 2. arg is ptr to buffer from where to get data to write */
352 buf = (void *)va_arg(ap, u_int32_t);
353 if (buf == NULL)
354 return API_EINVAL;
356 /* 3. arg is length of buffer */
357 len = (int *)va_arg(ap, u_int32_t);
358 if (len == NULL)
359 return API_EINVAL;
360 if (*len <= 0)
361 return API_EINVAL;
363 if (di->type & DEV_TYP_STOR)
365 * write to storage is currently not supported by U-Boot:
366 * no storage device implements block_write() method
368 return API_ENODEV;
370 else if (di->type & DEV_TYP_NET)
371 err = dev_write_net(di->cookie, buf, *len);
372 else
373 err = API_ENODEV;
375 return err;
380 * pseudo signature:
382 * int API_dev_read(
383 * struct device_info *di,
384 * void *buf,
385 * size_t *len,
386 * unsigned long *start
387 * size_t *act_len
390 * buf: ptr to buffer where to put the read data
392 * len: ptr to length to be read
393 * - network: len of packet to read (in bytes)
394 * - storage: # of blocks to read (can vary in size depending on define)
396 * start: ptr to start block (only used for storage devices, ignored for
397 * network)
399 * act_len: ptr to where to put the len actually read
401 static int API_dev_read(va_list ap)
403 struct device_info *di;
404 void *buf;
405 lbasize_t *len_stor, *act_len_stor;
406 lbastart_t *start;
407 int *len_net, *act_len_net;
409 /* 1. arg is ptr to the device_info struct */
410 di = (struct device_info *)va_arg(ap, u_int32_t);
411 if (di == NULL)
412 return API_EINVAL;
414 /* XXX should we check if device is open? i.e. the ->state ? */
416 if (di->cookie == NULL)
417 return API_ENODEV;
419 /* 2. arg is ptr to buffer from where to put the read data */
420 buf = (void *)va_arg(ap, u_int32_t);
421 if (buf == NULL)
422 return API_EINVAL;
424 if (di->type & DEV_TYP_STOR) {
425 /* 3. arg - ptr to var with # of blocks to read */
426 len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
427 if (!len_stor)
428 return API_EINVAL;
429 if (*len_stor <= 0)
430 return API_EINVAL;
432 /* 4. arg - ptr to var with start block */
433 start = (lbastart_t *)va_arg(ap, u_int32_t);
435 /* 5. arg - ptr to var where to put the len actually read */
436 act_len_stor = (lbasize_t *)va_arg(ap, u_int32_t);
437 if (!act_len_stor)
438 return API_EINVAL;
440 *act_len_stor = dev_read_stor(di->cookie, buf, *len_stor, *start);
442 } else if (di->type & DEV_TYP_NET) {
444 /* 3. arg points to the var with length of packet to read */
445 len_net = (int *)va_arg(ap, u_int32_t);
446 if (!len_net)
447 return API_EINVAL;
448 if (*len_net <= 0)
449 return API_EINVAL;
451 /* 4. - ptr to var where to put the len actually read */
452 act_len_net = (int *)va_arg(ap, u_int32_t);
453 if (!act_len_net)
454 return API_EINVAL;
456 *act_len_net = dev_read_net(di->cookie, buf, *len_net);
458 } else
459 return API_ENODEV;
461 return 0;
466 * pseudo signature:
468 * int API_env_get(const char *name, char **value)
470 * name: ptr to name of env var
472 static int API_env_get(va_list ap)
474 char *name, **value;
476 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
477 return API_EINVAL;
478 if ((value = (char **)va_arg(ap, u_int32_t)) == NULL)
479 return API_EINVAL;
481 *value = getenv(name);
483 return 0;
487 * pseudo signature:
489 * int API_env_set(const char *name, const char *value)
491 * name: ptr to name of env var
493 * value: ptr to value to be set
495 static int API_env_set(va_list ap)
497 char *name, *value;
499 if ((name = (char *)va_arg(ap, u_int32_t)) == NULL)
500 return API_EINVAL;
501 if ((value = (char *)va_arg(ap, u_int32_t)) == NULL)
502 return API_EINVAL;
504 setenv(name, value);
506 return 0;
510 * pseudo signature:
512 * int API_env_enum(const char *last, char **next)
514 * last: ptr to name of env var found in last iteration
516 static int API_env_enum(va_list ap)
518 int i, n;
519 char *last, **next;
521 last = (char *)va_arg(ap, u_int32_t);
523 if ((next = (char **)va_arg(ap, u_int32_t)) == NULL)
524 return API_EINVAL;
526 if (last == NULL)
527 /* start over */
528 *next = ((char *)env_get_addr(0));
529 else {
530 *next = last;
532 for (i = 0; env_get_char(i) != '\0'; i = n + 1) {
533 for (n = i; env_get_char(n) != '\0'; ++n) {
534 if (n >= CONFIG_ENV_SIZE) {
535 /* XXX shouldn't we set *next = NULL?? */
536 return 0;
540 if (envmatch((uchar *)last, i) < 0)
541 continue;
543 /* try to get next name */
544 i = n + 1;
545 if (env_get_char(i) == '\0') {
546 /* no more left */
547 *next = NULL;
548 return 0;
551 *next = ((char *)env_get_addr(i));
552 return 0;
556 return 0;
559 static cfp_t calls_table[API_MAXCALL] = { NULL, };
562 * The main syscall entry point - this is not reentrant, only one call is
563 * serviced until finished.
565 * e.g. syscall(1, int *, u_int32_t, u_int32_t, u_int32_t, u_int32_t);
567 * call: syscall number
569 * retval: points to the return value placeholder, this is the place the
570 * syscall puts its return value, if NULL the caller does not
571 * expect a return value
573 * ... syscall arguments (variable number)
575 * returns: 0 if the call not found, 1 if serviced
577 int syscall(int call, int *retval, ...)
579 va_list ap;
580 int rv;
582 if (call < 0 || call >= calls_no) {
583 debugf("invalid call #%d\n", call);
584 return 0;
587 if (calls_table[call] == NULL) {
588 debugf("syscall #%d does not have a handler\n", call);
589 return 0;
592 va_start(ap, retval);
593 rv = calls_table[call](ap);
594 if (retval != NULL)
595 *retval = rv;
597 return 1;
600 void api_init(void)
602 struct api_signature *sig = NULL;
604 /* TODO put this into linker set one day... */
605 calls_table[API_RSVD] = NULL;
606 calls_table[API_GETC] = &API_getc;
607 calls_table[API_PUTC] = &API_putc;
608 calls_table[API_TSTC] = &API_tstc;
609 calls_table[API_PUTS] = &API_puts;
610 calls_table[API_RESET] = &API_reset;
611 calls_table[API_GET_SYS_INFO] = &API_get_sys_info;
612 calls_table[API_UDELAY] = &API_udelay;
613 calls_table[API_GET_TIMER] = &API_get_timer;
614 calls_table[API_DEV_ENUM] = &API_dev_enum;
615 calls_table[API_DEV_OPEN] = &API_dev_open;
616 calls_table[API_DEV_CLOSE] = &API_dev_close;
617 calls_table[API_DEV_READ] = &API_dev_read;
618 calls_table[API_DEV_WRITE] = &API_dev_write;
619 calls_table[API_ENV_GET] = &API_env_get;
620 calls_table[API_ENV_SET] = &API_env_set;
621 calls_table[API_ENV_ENUM] = &API_env_enum;
622 calls_no = API_MAXCALL;
624 debugf("API initialized with %d calls\n", calls_no);
626 dev_stor_init();
629 * Produce the signature so the API consumers can find it
631 sig = malloc(sizeof(struct api_signature));
632 if (sig == NULL) {
633 printf("API: could not allocate memory for the signature!\n");
634 return;
637 debugf("API sig @ 0x%08x\n", sig);
638 memcpy(sig->magic, API_SIG_MAGIC, 8);
639 sig->version = API_SIG_VERSION;
640 sig->syscall = &syscall;
641 sig->checksum = 0;
642 sig->checksum = crc32(0, (unsigned char *)sig,
643 sizeof(struct api_signature));
644 debugf("syscall entry: 0x%08x\n", sig->syscall);
647 void platform_set_mr(struct sys_info *si, unsigned long start, unsigned long size,
648 int flags)
650 int i;
652 if (!si->mr || !size || (flags == 0))
653 return;
655 /* find free slot */
656 for (i = 0; i < si->mr_no; i++)
657 if (si->mr[i].flags == 0) {
658 /* insert new mem region */
659 si->mr[i].start = start;
660 si->mr[i].size = size;
661 si->mr[i].flags = flags;
662 return;