1 /* OS-Level Implementations */
3 /* This is the Linux kernel implementation of the DWC platform library. */
8 #include <linux/device.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/interrupt.h>
12 #include <linux/list.h>
13 #include <linux/string.h>
14 //#include <linux/timer.h>
16 //#include <linux/usb/gadget.h>
23 void *DWC_MEMSET(void *dest, uint8_t byte, uint32_t size)
25 return memset(dest, byte, size);
29 void *DWC_MEMCPY(void *dest, void const *src, uint32_t size)
31 return memcpy(dest, src, size);
35 void *DWC_MEMMOVE(void *dest, void *src, uint32_t size)
37 return memmove(dest, src, size);
41 int DWC_MEMCMP(void *m1, void *m2, uint32_t size)
43 return memcmp(m1, m2, size);
47 int DWC_STRNCMP(void *s1, void *s2, uint32_t size)
49 return strncmp(s1, s2, size);
53 int DWC_STRCMP(void *s1, void *s2)
55 return strcmp(s1, s2);
59 int DWC_STRLEN(char const *str)
65 char *DWC_STRCPY(char *to, const char *from)
67 return strcpy(to, from);
71 char *DWC_STRDUP(char const *str)
73 int len = DWC_STRLEN(str) + 1;
74 char *new = DWC_ALLOC_ATOMIC(len);
78 DWC_MEMCPY(new, str, len);
83 int DWC_ATOI(char *str, int32_t *value)
86 *value = simple_strtol(str, &end, 0);
94 int DWC_ATOUI(char *str, uint32_t *value)
97 *value = simple_strtoul(str, &end, 0);
107 dwc_bool_t DWC_IN_IRQ(void)
114 void DWC_VPRINTF(char *format, va_list args)
116 vprintf(format, args);
120 int DWC_VSNPRINTF(char *str, int size, char *format, va_list args)
122 vsprintf(str, format, args);
126 void DWC_PRINTF(char *format, ...)
129 va_start(args, format);
130 DWC_VPRINTF(format, args);
135 int DWC_SPRINTF(char *buffer, char *format, ...)
139 va_start(args, format);
140 retval = vsprintf(buffer, format, args);
146 int DWC_SNPRINTF(char *buffer, int size, char *format, ...)
150 va_start(args, format);
151 retval = vsprintf(buffer, format, args);
157 void __DWC_WARN(char *format, ...)
160 va_start(args, format);
161 //DWC_PRINTF(KERN_WARNING);
162 DWC_VPRINTF(format, args);
167 void __DWC_ERROR(char *format, ...)
170 va_start(args, format);
171 //DWC_PRINTF(KERN_ERR);
172 DWC_VPRINTF(format, args);
177 void DWC_EXCEPTION(char *format, ...)
180 va_start(args, format);
181 //DWC_PRINTF(KERN_ERR);
182 DWC_VPRINTF(format, args);
189 void __DWC_DEBUG(char *format, ...)
192 va_start(args, format);
193 //DWC_PRINTF(KERN_DEBUG);
194 DWC_VPRINTF(format, args);
203 void *__DWC_DMA_ALLOC(uint32_t size, dwc_dma_t *dma_addr)
206 void *buf = dma_alloc_coherent(NULL, (size_t)size, dma_addr, GFP_KERNEL);
210 memset(buf, 0, (size_t)size);
213 dwc_debug("NO SUPPORT %s\r\n", __func__);
217 void __DWC_DMA_FREE(uint32_t size, void *virt_addr, dwc_dma_t dma_addr)
219 //dma_free_coherent(NULL, size, virt_addr, dma_addr);
220 dwc_debug("NO SUPPORT %s\r\n", __func__);
223 void *__DWC_ALLOC(uint32_t size)
225 //return kzalloc(size, GFP_KERNEL);
231 memset(ptr, 0, size);
236 void *__DWC_ALLOC_ATOMIC(uint32_t size)
238 //return kzalloc(size, GFP_ATOMIC);
239 return __DWC_ALLOC(size);
242 void __DWC_FREE(void *addr)
251 uint32_t DWC_READ_REG32(uint32_t volatile *reg)
257 void DWC_WRITE_REG32(uint32_t volatile *reg, uint32_t value)
263 void DWC_MODIFY_REG32(uint32_t volatile *reg, uint32_t clear_mask, uint32_t set_mask)
265 writel( (readl(reg) & ~clear_mask) | set_mask, reg );
270 typedef struct work_container
272 dwc_work_callback_t cb;
278 DWC_CIRCLEQ_ENTRY(work_container) entry;
281 //struct delayed_work work;
285 DWC_CIRCLEQ_HEAD(work_container_queue, work_container);
290 //struct workqueue_struct *wq;
292 dwc_spinlock_t *lock;
296 struct work_container_queue entries;
301 static void do_work(struct work_struct *work)
304 struct delayed_work *dw = container_of(work, struct delayed_work, work);
305 work_container_t *container = container_of(dw, struct work_container, work);
306 dwc_workq_t *wq = container->wq;
308 container->cb(container->data);
311 DWC_CIRCLEQ_REMOVE(&wq->entries, container, entry);
314 DWC_DEBUG("Work done: %s, container=%p", container->name, container);
315 if (container->name) {
316 DWC_FREE(container->name);
320 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
322 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
323 DWC_WAITQ_TRIGGER(wq->waitq);
326 static int work_done(void *data)
328 dwc_workq_t *workq = (dwc_workq_t *)data;
329 return workq->pending == 0;
333 int DWC_WORKQ_WAIT_WORK_DONE(dwc_workq_t *workq, int timeout)
335 dwc_debug("NO SUPPORT %s\r\n", __func__);
337 //return DWC_WAITQ_WAIT_TIMEOUT(workq->waitq, work_done, workq, timeout);
340 dwc_workq_t *DWC_WORKQ_ALLOC(char *name)
342 dwc_debug("NO SUPPORT %s\r\n", __func__);
345 dwc_workq_t *wq = DWC_ALLOC(sizeof(*wq));
346 wq->wq = create_singlethread_workqueue(name);
348 wq->lock = DWC_SPINLOCK_ALLOC();
349 wq->waitq = DWC_WAITQ_ALLOC();
351 DWC_CIRCLEQ_INIT(&wq->entries);
357 void DWC_WORKQ_FREE(dwc_workq_t *wq)
361 if (wq->pending != 0) {
362 struct work_container *wc;
363 DWC_ERROR("Destroying work queue with pending work");
364 DWC_CIRCLEQ_FOREACH(wc, &wq->entries, entry) {
365 DWC_ERROR("Work %s still pending", wc->name);
369 destroy_workqueue((struct workqueue_struct *)wq->wq);
370 DWC_SPINLOCK_FREE(wq->lock);
371 DWC_WAITQ_FREE(wq->waitq);
374 dwc_debug("NO SUPPORT %s\r\n", __func__);
377 void DWC_WORKQ_SCHEDULE(dwc_workq_t *wq, dwc_work_callback_t work_cb, void *data, char *format, ...)
379 dwc_debug("NO SUPPORT %s\r\n", __func__);
382 work_container_t *container;
383 static char name[128];
386 va_start(args, format);
387 DWC_VSNPRINTF(name, 128, format, args);
390 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
392 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
393 DWC_WAITQ_TRIGGER(wq->waitq);
395 container = DWC_ALLOC_ATOMIC(sizeof(*container));
397 container->data = data;
398 container->cb = work_cb;
400 container->name = DWC_STRDUP(name);
401 DWC_DEBUG("Queueing work: %s, contianer=%p", container->name, container);
402 INIT_WORK(&container->work.work, do_work);
405 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
408 queue_work(wq->wq, &container->work.work);
412 void DWC_WORKQ_SCHEDULE_DELAYED(dwc_workq_t *wq, dwc_work_callback_t work_cb, void *data, uint32_t time, char *format, ...)
414 dwc_debug("NO SUPPORT %s\r\n", __func__);
417 work_container_t *container;
418 static char name[128];
421 va_start(args, format);
422 DWC_VSNPRINTF(name, 128, format, args);
425 DWC_SPINLOCK_IRQSAVE(wq->lock, &flags);
427 DWC_SPINUNLOCK_IRQRESTORE(wq->lock, flags);
428 DWC_WAITQ_TRIGGER(wq->waitq);
430 container = DWC_ALLOC_ATOMIC(sizeof(*container));
432 container->data = data;
433 container->cb = work_cb;
435 container->name = DWC_STRDUP(name);
436 DWC_DEBUG("Queueing work: %s, contianer=%p", container->name, container);
437 INIT_DELAYED_WORK(&container->work, do_work);
440 DWC_CIRCLEQ_INSERT_TAIL(&wq->entries, container, entry);
443 queue_delayed_work(wq->wq, &container->work, msecs_to_jiffies(time));
447 int DWC_WORKQ_PENDING(dwc_workq_t *wq)
452 dwc_spinlock_t *DWC_SPINLOCK_ALLOC(void)
454 dwc_spinlock_t *sl = (dwc_spinlock_t *)1;
455 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
456 sl = DWC_ALLOC(sizeof(*sl));
459 return (dwc_spinlock_t *)sl;
462 void DWC_SPINLOCK_FREE(dwc_spinlock_t *lock)
464 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
469 void DWC_SPINLOCK(dwc_spinlock_t *lock)
471 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
472 spin_lock((spinlock_t *)lock);
476 void DWC_SPINUNLOCK(dwc_spinlock_t *lock)
478 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
479 spin_unlock((spinlock_t *)lock);
483 void DWC_SPINLOCK_IRQSAVE(dwc_spinlock_t *lock, unsigned long *flags)
486 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
487 spin_lock_irqsave((spinlock_t *)lock, f);
494 void DWC_SPINUNLOCK_IRQRESTORE(dwc_spinlock_t *lock, unsigned long flags)
496 #if defined(CONFIG_PREEMPT) || defined(CONFIG_SMP)
497 spin_unlock_irqrestore((spinlock_t *)lock, flags);
499 local_irq_restore(flags);
503 dwc_mutex_t *DWC_MUTEX_ALLOC(void)
505 dwc_debug("NO SUPPORT %s\r\n", __func__);
507 dwc_mutex_t *mutex = (dwc_mutex_t*)DWC_ALLOC(sizeof(struct mutex));
508 struct mutex *m = (struct mutex *)mutex;
515 #if (defined(DWC_LINUX) && defined(CONFIG_DEBUG_MUTEXES))
517 void DWC_MUTEX_FREE(dwc_mutex_t *mutex)
519 dwc_debug("NO SUPPORT %s\r\n", __func__);
521 mutex_destroy((struct mutex *)mutex);
527 void DWC_MUTEX_LOCK(dwc_mutex_t *mutex)
529 dwc_debug("NO SUPPORT %s\r\n", __func__);
531 struct mutex *m = (struct mutex *)mutex;
537 int DWC_MUTEX_TRYLOCK(dwc_mutex_t *mutex)
539 dwc_debug("NO SUPPORT %s\r\n", __func__);
541 struct mutex *m = (struct mutex *)mutex;
542 return mutex_trylock(m);
548 void DWC_MUTEX_UNLOCK(dwc_mutex_t *mutex)
551 struct mutex *m = (struct mutex *)mutex;
561 //struct timer_list *t;
563 dwc_timer_callback_t cb;
566 dwc_spinlock_t *lock;
570 static void set_scheduled(dwc_timer_t *t, int s)
573 DWC_SPINLOCK_IRQSAVE(t->lock, &flags);
575 DWC_SPINUNLOCK_IRQRESTORE(t->lock, flags);
578 static int get_scheduled(dwc_timer_t *t)
582 DWC_SPINLOCK_IRQSAVE(t->lock, &flags);
584 DWC_SPINUNLOCK_IRQRESTORE(t->lock, flags);
588 static void timer_callback(unsigned long data)
590 dwc_timer_t *timer = (dwc_timer_t *)data;
591 set_scheduled(timer, 0);
592 DWC_DEBUG("Timer %s callback", timer->name);
593 timer->cb(timer->data);
598 dwc_timer_t *DWC_TIMER_ALLOC(char *name, dwc_timer_callback_t cb, void *data)
600 dwc_debug("NO SUPPORT %s\r\n", __func__);
603 dwc_timer_t *t = DWC_ALLOC(sizeof(*t));
605 DWC_ERROR("Cannot allocate memory for timer");
608 t->t = DWC_ALLOC(sizeof(*t->t));
610 DWC_ERROR("Cannot allocate memory for timer->t");
614 t->name = DWC_STRDUP(name);
616 DWC_ERROR("Cannot allocate memory for timer->name");
620 t->lock = DWC_SPINLOCK_ALLOC();
622 DWC_ERROR("Cannot allocate memory for lock");
626 t->t->base = &boot_tvec_bases;
627 t->t->expires = jiffies;
628 setup_timer(t->t, timer_callback, (unsigned long)t);
645 void DWC_TIMER_FREE(dwc_timer_t *timer)
647 dwc_debug("NO SUPPORT %s\r\n", __func__);
649 if (get_scheduled(timer)) {
653 DWC_SPINLOCK_FREE(timer->lock);
655 DWC_FREE(timer->name);
660 void DWC_TIMER_SCHEDULE(dwc_timer_t *timer, uint32_t time)
662 dwc_debug("NO SUPPORT %s\r\n", __func__);
665 if (!get_scheduled(timer)) {
666 set_scheduled(timer, 1);
667 DWC_DEBUG("Scheduling timer %s to expire in +%d msec", timer->name, time);
668 timer->t->expires = jiffies + msecs_to_jiffies(time);
672 DWC_DEBUG("Modifying timer %s to expire in +%d msec", timer->name, time);
673 mod_timer(timer->t, jiffies + msecs_to_jiffies(time));
678 void DWC_TIMER_CANCEL(dwc_timer_t *timer)
680 dwc_debug("NO SUPPORT %s\r\n", __func__);
681 //del_timer(timer->t);
686 // struct tasklet_struct t;
687 dwc_tasklet_callback_t cb;
692 static void tasklet_callback(unsigned long data)
694 dwc_tasklet_t *t = (dwc_tasklet_t *)data;
699 dwc_tasklet_t *DWC_TASK_ALLOC(dwc_tasklet_callback_t cb, void *data)
701 dwc_debug("NO SUPPORT %s\r\n", __func__);
703 dwc_tasklet_t *t = DWC_ALLOC(sizeof(*t));
708 tasklet_init(&t->t, tasklet_callback, (unsigned long)t);
710 DWC_ERROR("Cannot allocate memory for tasklet\n");
718 void DWC_TASK_FREE(dwc_tasklet_t *t)
720 dwc_debug("NO SUPPORT %s\r\n", __func__);
724 void DWC_TASK_SCHEDULE(dwc_tasklet_t *task)
726 dwc_debug("NO SUPPORT %s\r\n", __func__);
727 // tasklet_schedule(&task->t);
731 void DWC_UDELAY(uint32_t usecs)
736 void DWC_MDELAY(uint32_t msecs)
739 udelay(msecs * 1000);
742 void DWC_MSLEEP(uint32_t msecs)
748 uint32_t DWC_TIME(void)
750 dwc_debug("NO SUPPORT %s\r\n", __func__);
751 //return jiffies_to_msecs(jiffies);
760 // wait_queue_head_t queue;
764 dwc_waitq_t *DWC_WAITQ_ALLOC(void)
766 dwc_debug("NO SUPPORT %s\r\n", __func__);
768 dwc_waitq_t *wq = DWC_ALLOC(sizeof(*wq));
769 init_waitqueue_head(&wq->queue);
776 void DWC_WAITQ_FREE(dwc_waitq_t *wq)
778 dwc_debug("NO SUPPORT %s\r\n", __func__);
783 static int32_t check_result(dwc_waitq_t *wq, int result)
786 msecs = jiffies_to_msecs(result);
794 return -DWC_E_TIMEOUT;
797 if ((result == -ERESTARTSYS) || (wq->abort == 1)) {
801 return -DWC_E_UNKNOWN;
805 int32_t DWC_WAITQ_WAIT(dwc_waitq_t *wq, dwc_waitq_condition_t condition, void *data)
807 dwc_debug("NO SUPPORT %s\r\n", __func__);
809 int result = wait_event_interruptible(wq->queue,
810 condition(data) || wq->abort);
811 return check_result(wq, result);
816 int32_t DWC_WAITQ_WAIT_TIMEOUT(dwc_waitq_t *wq, dwc_waitq_condition_t condition,
817 void *data, int32_t msecs)
819 dwc_debug("NO SUPPORT %s\r\n", __func__);
822 int result = wait_event_interruptible_timeout(wq->queue,
823 condition(data) || wq->abort,
824 msecs_to_jiffies(msecs));
825 return check_result(wq, result);
830 void DWC_WAITQ_TRIGGER(dwc_waitq_t *wq)
832 dwc_debug("NO SUPPORT %s\r\n", __func__);
833 // wake_up_interruptible(&wq->queue);
836 void DWC_WAITQ_ABORT(dwc_waitq_t *wq)
838 dwc_debug("NO SUPPORT %s\r\n", __func__);
841 DWC_WAITQ_TRIGGER(wq);