[bf84871] | 1 | /*
|
---|
| 2 | * Copyright (c) 2011 Jiri Michalec
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @file
|
---|
| 30 | *
|
---|
| 31 | * General functions and structures used in rtl8139 driver
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[f0b74b2] | 34 | #include "general.h"
|
---|
[bf84871] | 35 |
|
---|
| 36 | #include <mem.h>
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <stdint.h>
|
---|
| 39 |
|
---|
| 40 | /** Copy block of the memory from wrapped source buffer.
|
---|
| 41 | *
|
---|
| 42 | * Start on the specific offset in the source buffer and * copy data_size bytes
|
---|
| 43 | * - continue from the buffer start after getting the end
|
---|
| 44 | *
|
---|
| 45 | * @param dest The destination memory
|
---|
| 46 | * @param src_begin The begin of the source buffer
|
---|
| 47 | * @param src_offset The offset in the source buffer to start copy
|
---|
| 48 | * @param src_size The source buffer size
|
---|
| 49 | * @param data_size The amount of data to copy
|
---|
| 50 | *
|
---|
| 51 | * @return NULL if the error occures, dest if succeed
|
---|
| 52 | */
|
---|
[1433ecda] | 53 | void *rtl8139_memcpy_wrapped(void *dest, const void *src, size_t src_offset,
|
---|
[bf84871] | 54 | size_t src_size, size_t data_size)
|
---|
| 55 | {
|
---|
| 56 | src_offset %= src_size;
|
---|
| 57 | if (data_size > src_size)
|
---|
| 58 | return NULL;
|
---|
| 59 |
|
---|
| 60 | size_t to_src_end = src_size - src_offset;
|
---|
| 61 | if (data_size <= to_src_end) {
|
---|
| 62 | return memcpy(dest, src + src_offset, data_size);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | size_t rem_size = data_size - to_src_end;
|
---|
| 66 |
|
---|
[7c3fb9b] | 67 | /*
|
---|
| 68 | * First copy the end part of the data (from the source begining),
|
---|
[bf84871] | 69 | * then copy the begining
|
---|
| 70 | */
|
---|
| 71 | memcpy(dest + to_src_end, src, rem_size);
|
---|
| 72 | return memcpy(dest, src + src_offset, to_src_end);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
[1b20da0] | 75 | /** Initialize the timer register structures
|
---|
[bf84871] | 76 | *
|
---|
[1b20da0] | 77 | * The structure will be initialized to the state that the first call of
|
---|
[bf84871] | 78 | * rtl8139_timer_act_step function will be the period expiration
|
---|
| 79 | *
|
---|
| 80 | * @param ta The timer structure
|
---|
| 81 | * @param timer_freq The timer frequency in kHz
|
---|
| 82 | * @param time The requested time
|
---|
| 83 | *
|
---|
[cde999a] | 84 | * @return EOK if succeed, error code otherwise
|
---|
[bf84871] | 85 | */
|
---|
[1433ecda] | 86 | errno_t rtl8139_timer_act_init(rtl8139_timer_act_t *ta, uint32_t timer_freq,
|
---|
[bd41ac52] | 87 | const struct timespec *time)
|
---|
[bf84871] | 88 | {
|
---|
| 89 | if (!ta || timer_freq == 0 || !time)
|
---|
| 90 | return EINVAL;
|
---|
| 91 | memset(ta, 0, sizeof(rtl8139_timer_act_t));
|
---|
| 92 |
|
---|
| 93 | uint32_t tics_per_ms = timer_freq;
|
---|
| 94 | uint32_t seconds_in_reg = UINT32_MAX / (tics_per_ms * 1000);
|
---|
| 95 | ta->full_val = seconds_in_reg * tics_per_ms * 1000;
|
---|
| 96 |
|
---|
[bd41ac52] | 97 | struct timespec remains = *time;
|
---|
[bf84871] | 98 | ta->full_skips = remains.tv_sec / seconds_in_reg;
|
---|
| 99 | remains.tv_sec = remains.tv_sec % seconds_in_reg;
|
---|
| 100 |
|
---|
[bd41ac52] | 101 | if (NSEC2USEC(remains.tv_nsec) > RTL8139_USEC_IN_SEC) {
|
---|
| 102 | remains.tv_sec += NSEC2USEC(remains.tv_nsec) / RTL8139_USEC_IN_SEC;
|
---|
| 103 | remains.tv_nsec = NSEC2USEC(remains.tv_nsec) % RTL8139_USEC_IN_SEC;
|
---|
[bf84871] | 104 |
|
---|
| 105 | /* it can be increased above seconds_in_reg again */
|
---|
| 106 | ta->full_skips += remains.tv_sec / seconds_in_reg;
|
---|
| 107 | remains.tv_sec = remains.tv_sec % seconds_in_reg;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[bd41ac52] | 110 | ta->last_val = SEC2MSEC(remains.tv_sec) + NSEC2MSEC(remains.tv_nsec);
|
---|
[bf84871] | 111 | ta->last_val *= tics_per_ms;
|
---|
| 112 |
|
---|
| 113 | /* Force inital setting in the next step */
|
---|
| 114 | ta->full_skips_remains = 0;
|
---|
| 115 | ta->last_run = 1;
|
---|
| 116 | return EOK;
|
---|
[850fd32] | 117 | }
|
---|
[bf84871] | 118 |
|
---|
| 119 | /** Make one step timer step
|
---|
| 120 | *
|
---|
| 121 | * @param ta Timer structure
|
---|
| 122 | * @param new_reg[out] Register value to set
|
---|
| 123 | *
|
---|
| 124 | * @return Nonzero if whole period expired, zero if part of period expired
|
---|
| 125 | */
|
---|
[1433ecda] | 126 | int rtl8139_timer_act_step(rtl8139_timer_act_t *ta, uint32_t *new_reg)
|
---|
[bf84871] | 127 | {
|
---|
| 128 | uint32_t next_val = 0;
|
---|
| 129 | int expired = 0;
|
---|
| 130 |
|
---|
| 131 | if (ta->last_run || (ta->last_val == 0 && ta->full_skips_remains == 0)) {
|
---|
| 132 | ta->full_skips_remains = ta->full_skips;
|
---|
| 133 | ta->last_run = 0;
|
---|
| 134 | expired = 1;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | if (ta->full_skips_remains > 0) {
|
---|
| 138 | next_val = ta->full_val;
|
---|
| 139 | ta->full_skips_remains--;
|
---|
| 140 | } else {
|
---|
| 141 | next_val = ta->last_val;
|
---|
| 142 | ta->last_run = 1;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (new_reg)
|
---|
| 146 | *new_reg = next_val;
|
---|
| 147 |
|
---|
| 148 | return expired;
|
---|
[850fd32] | 149 | }
|
---|