| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Jan Vesely
|
|---|
| 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 | /** @addtogroup drvusbohci
|
|---|
| 29 | * @{
|
|---|
| 30 | */
|
|---|
| 31 | /** @file
|
|---|
| 32 | * @brief OHCI driver
|
|---|
| 33 | */
|
|---|
| 34 | #ifndef DRV_OHCI_HW_STRUCT_ENDPOINT_DESCRIPTOR_H
|
|---|
| 35 | #define DRV_OHCI_HW_STRUCT_ENDPOINT_DESCRIPTOR_H
|
|---|
| 36 |
|
|---|
| 37 | #include <assert.h>
|
|---|
| 38 | #include <stdint.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <usb/host/endpoint.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "../utils/malloc32.h"
|
|---|
| 43 | #include "transfer_descriptor.h"
|
|---|
| 44 |
|
|---|
| 45 | #include "completion_codes.h"
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * OHCI Endpoint Descriptor representation.
|
|---|
| 49 | *
|
|---|
| 50 | * See OHCI spec. Chapter 4.2, page 16 (pdf page 30) for details */
|
|---|
| 51 | typedef struct ed {
|
|---|
| 52 | /**
|
|---|
| 53 | * Status field.
|
|---|
| 54 | *
|
|---|
| 55 | * See table 4-1, p. 17 OHCI spec (pdf page 31).
|
|---|
| 56 | */
|
|---|
| 57 | volatile uint32_t status;
|
|---|
| 58 | #define ED_STATUS_FA_MASK (0x7f) /* USB device address */
|
|---|
| 59 | #define ED_STATUS_FA_SHIFT (0)
|
|---|
| 60 | #define ED_STATUS_EN_MASK (0xf) /* USB endpoint address */
|
|---|
| 61 | #define ED_STATUS_EN_SHIFT (7)
|
|---|
| 62 | #define ED_STATUS_D_MASK (0x3) /* Direction */
|
|---|
| 63 | #define ED_STATUS_D_SHIFT (11)
|
|---|
| 64 | #define ED_STATUS_D_OUT (0x1)
|
|---|
| 65 | #define ED_STATUS_D_IN (0x2)
|
|---|
| 66 | #define ED_STATUS_D_TD (0x3) /* Direction is specified by TD */
|
|---|
| 67 |
|
|---|
| 68 | #define ED_STATUS_S_FLAG (1 << 13) /* Speed flag: 1 = low */
|
|---|
| 69 | #define ED_STATUS_K_FLAG (1 << 14) /* Skip flag (no not execute this ED) */
|
|---|
| 70 | #define ED_STATUS_F_FLAG (1 << 15) /* Format: 1 = isochronous */
|
|---|
| 71 | #define ED_STATUS_MPS_MASK (0x3ff) /* Maximum packet size */
|
|---|
| 72 | #define ED_STATUS_MPS_SHIFT (16)
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Pointer to the last TD.
|
|---|
| 76 | *
|
|---|
| 77 | * OHCI hw never changes this field and uses it only for a reference.
|
|---|
| 78 | */
|
|---|
| 79 | volatile uint32_t td_tail;
|
|---|
| 80 | #define ED_TDTAIL_PTR_MASK (0xfffffff0)
|
|---|
| 81 | #define ED_TDTAIL_PTR_SHIFT (0)
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Pointer to the first TD.
|
|---|
| 85 | *
|
|---|
| 86 | * Driver should not change this field if the ED is active.
|
|---|
| 87 | * This field is updated by OHCI hw and points to the next TD
|
|---|
| 88 | * to be executed.
|
|---|
| 89 | */
|
|---|
| 90 | volatile uint32_t td_head;
|
|---|
| 91 | #define ED_TDHEAD_PTR_MASK (0xfffffff0)
|
|---|
| 92 | #define ED_TDHEAD_PTR_SHIFT (0)
|
|---|
| 93 | #define ED_TDHEAD_ZERO_MASK (0x3)
|
|---|
| 94 | #define ED_TDHEAD_ZERO_SHIFT (2)
|
|---|
| 95 | #define ED_TDHEAD_TOGGLE_CARRY (0x2)
|
|---|
| 96 | #define ED_TDHEAD_HALTED_FLAG (0x1)
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * Pointer to the next ED.
|
|---|
| 100 | *
|
|---|
| 101 | * Driver should not change this field on active EDs.
|
|---|
| 102 | */
|
|---|
| 103 | volatile uint32_t next;
|
|---|
| 104 | #define ED_NEXT_PTR_MASK (0xfffffff0)
|
|---|
| 105 | #define ED_NEXT_PTR_SHIFT (0)
|
|---|
| 106 | } __attribute__((packed)) ed_t;
|
|---|
| 107 |
|
|---|
| 108 | void ed_init(ed_t *instance, const endpoint_t *ep, const td_t *td);
|
|---|
| 109 |
|
|---|
| 110 | /**
|
|---|
| 111 | * Check for SKIP or HALTED flag being set.
|
|---|
| 112 | * @param instance ED
|
|---|
| 113 | * @return true if either SKIP or HALTED flag is set, false otherwise.
|
|---|
| 114 | */
|
|---|
| 115 | static inline bool ed_inactive(const ed_t *instance)
|
|---|
| 116 | {
|
|---|
| 117 | assert(instance);
|
|---|
| 118 | return (instance->td_head & ED_TDHEAD_HALTED_FLAG)
|
|---|
| 119 | || (instance->status & ED_STATUS_K_FLAG);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | /**
|
|---|
| 123 | * Check whether this ED contains TD to be executed.
|
|---|
| 124 | * @param instance ED
|
|---|
| 125 | * @return true if there are pending TDs, false otherwise.
|
|---|
| 126 | */
|
|---|
| 127 | static inline bool ed_transfer_pending(const ed_t *instance)
|
|---|
| 128 | {
|
|---|
| 129 | assert(instance);
|
|---|
| 130 | return (instance->td_head & ED_TDHEAD_PTR_MASK)
|
|---|
| 131 | != (instance->td_tail & ED_TDTAIL_PTR_MASK);
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /**
|
|---|
| 135 | * Set the last element of TD list
|
|---|
| 136 | * @param instance ED
|
|---|
| 137 | * @param instance TD to set as the last item.
|
|---|
| 138 | */
|
|---|
| 139 | static inline void ed_set_tail_td(ed_t *instance, const td_t *td)
|
|---|
| 140 | {
|
|---|
| 141 | assert(instance);
|
|---|
| 142 | const uintptr_t pa = addr_to_phys(td);
|
|---|
| 143 | instance->td_tail = pa & ED_TDTAIL_PTR_MASK;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | /**
|
|---|
| 147 | * Set next ED in ED chain.
|
|---|
| 148 | * @param instance ED to modify
|
|---|
| 149 | * @param next ED to append
|
|---|
| 150 | */
|
|---|
| 151 | static inline void ed_append_ed(ed_t *instance, const ed_t *next)
|
|---|
| 152 | {
|
|---|
| 153 | assert(instance);
|
|---|
| 154 | assert(next);
|
|---|
| 155 | const uint32_t pa = addr_to_phys(next);
|
|---|
| 156 | assert((pa & ED_NEXT_PTR_MASK) << ED_NEXT_PTR_SHIFT == pa);
|
|---|
| 157 | instance->next = pa;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /**
|
|---|
| 161 | * Get toggle bit value stored in this ED
|
|---|
| 162 | * @param instance ED
|
|---|
| 163 | * @return Toggle bit value
|
|---|
| 164 | */
|
|---|
| 165 | static inline int ed_toggle_get(const ed_t *instance)
|
|---|
| 166 | {
|
|---|
| 167 | assert(instance);
|
|---|
| 168 | return (instance->td_head & ED_TDHEAD_TOGGLE_CARRY) ? 1 : 0;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | /**
|
|---|
| 172 | * Set toggle bit value stored in this ED
|
|---|
| 173 | * @param instance ED
|
|---|
| 174 | * @param toggle Toggle bit value
|
|---|
| 175 | */
|
|---|
| 176 | static inline void ed_toggle_set(ed_t *instance, bool toggle)
|
|---|
| 177 | {
|
|---|
| 178 | assert(instance);
|
|---|
| 179 | if (toggle) {
|
|---|
| 180 | instance->td_head |= ED_TDHEAD_TOGGLE_CARRY;
|
|---|
| 181 | } else {
|
|---|
| 182 | /* Clear halted flag when reseting toggle TODO: Why? */
|
|---|
| 183 | instance->td_head &= ~ED_TDHEAD_TOGGLE_CARRY;
|
|---|
| 184 | instance->td_head &= ~ED_TDHEAD_HALTED_FLAG;
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | #endif
|
|---|
| 188 | /**
|
|---|
| 189 | * @}
|
|---|
| 190 | */
|
|---|