SyterKit 0.4.0.x
SyterKit is a bare-metal framework
Loading...
Searching...
No Matches
usb_controller.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0+ */
2
3#ifndef __USB_CONTROLLER_H__
4#define __USB_CONTROLLER_H__
5
6#include <stdarg.h>
7#include <stdbool.h>
8#include <stddef.h>
9#include <stdint.h>
10#include <types.h>
11
12#include "reg/reg-usb.h"
13
14#define USBC_MAX_OPEN_NUM 8
15#define USBC_MAX_CTL_NUM 3
16#define USBC_MAX_EP_NUM 6
17#define USBC0_MAX_FIFO_SIZE (8 * 1024)
18#define USBC_EP0_FIFOSIZE 64
19
24
25/* Structure to store hardware information for the current USB port */
26typedef struct usb_controller_otg {
27 uint32_t port_num; /* USB port number */
28 uint32_t base_addr; /* USB base address */
29
30 uint32_t used; /* Whether it is currently being used */
31 uint32_t no; /* Position in the management array */
33
41 return readw(USBC_REG_INTTx(addr));
42}
43
50static inline void usb_controller_int_clear_tx_pending(uint32_t addr, uint8_t ep_index) {
51 writew((1 << ep_index), USBC_REG_INTTx(addr));
52}
53
60 writew(0xffff, USBC_REG_INTTx(addr));
61}
62
70 return readw(USBC_REG_INTRx(addr));
71}
72
79static inline void usb_controller_int_clear_rx_pending(uint32_t addr, uint8_t ep_index) {
80 writew((1 << ep_index), USBC_REG_INTRx(addr));
81}
82
89 writew(0xffff, USBC_REG_INTRx(addr));
90}
91
98static inline void usb_controller_int_enable_tx_ep(uint32_t addr, uint8_t ep_index) {
99 usb_set_bit16(ep_index, USBC_REG_INTTxE(addr));
100}
101
108static inline void usb_controller_int_enable_rx_ep(uint32_t addr, uint8_t ep_index) {
109 usb_set_bit16(ep_index, USBC_REG_INTRxE(addr));
110}
111
118static inline void usb_controller_int_disable_tx_ep(uint32_t addr, uint8_t ep_index) {
119 usb_clear_bit16(ep_index, USBC_REG_INTTxE(addr));
120}
121
128static inline void usb_controller_int_disable_rx_ep(uint32_t addr, uint8_t ep_index) {
129 usb_clear_bit16(ep_index, USBC_REG_INTRxE(addr));
130}
131
138 writew(0, USBC_REG_INTTxE(addr));
139}
140
147 writew(0, USBC_REG_INTRxE(addr));
148}
149
157
165
173
181
188
195
202
209
216
224
232
240
248void usb_controller_int_enable_ep(uint64_t husb, uint32_t ep_type, uint32_t ep_index);
249
258
267
275
283
291
298
306
314void usb_controller_int_disable_ep(uint64_t husb, uint32_t ep_type, uint8_t ep_index);
315
323
330
339void usb_controller_config_fifo_tx_ep(uint32_t addr, uint32_t is_double_fifo, uint32_t fifo_size, uint32_t fifo_addr);
340
347
356void usb_controller_config_fifo_rx_ep(uint32_t addr, uint32_t is_double_fifo, uint32_t fifo_size, uint32_t fifo_addr);
357
367void usb_controller_config_fifo(uint64_t husb, uint32_t ep_type, uint32_t is_double_fifo, uint32_t fifo_size, uint32_t fifo_addr);
368
369/*
370 * Function name: usb_controller_get_vbus_status
371 *
372 * @param husb: The USB controller handle. (Input)
373 *
374 * @return Returns the VBUS status value.
375 *
376 * Description:
377 * This function gets the VBUS status of the USB controller.
378 * It takes the USB controller handle as an input parameter.
379 * The function first checks if the USB controller handle is not NULL.
380 * It then reads the device control register to get the VBUS status.
381 * The read value is shifted right by the VBUS bit position to get the VBUS status value.
382 * The function uses a switch statement to check and return the corresponding VBUS status values.
383 * If the VBUS status value is not recognized, it returns the default "below session end" status value.
384 */
386
387/*
388 * Function name: usb_controller_read_len_from_fifo
389 *
390 * @param husb: The USB controller handle. (Input)
391 * @param ep_type: The endpoint type. (Input)
392 *
393 * @return Returns the length of data available in the FIFO for the given endpoint type.
394 *
395 * Description:
396 * This function reads the length of data available in the FIFO for the given endpoint type using the USB controller.
397 * The function first checks if the USB controller handle is not NULL.
398 * Then, the function reads the length from the appropriate register based on the endpoint type provided.
399 * If the endpoint type is USBC_EP_TYPE_EP0, the count is read from the COUNT0 register.
400 * If the endpoint type is USBC_EP_TYPE_TX, the count is 0 since this is an endpoint for transmitting data.
401 * If the endpoint type is USBC_EP_TYPE_RX, the count is read from the RXCOUNT register.
402 * Finally, the function returns the total length of data available in the FIFO for the given endpoint type.
403 */
405
406/*
407 * Function name: usb_controller_write_packet
408 *
409 * @param husb: The USB controller handle. (Input)
410 * @param fifo: The FIFO number to write the packet to. (Input)
411 * @param cnt: The number of bytes to write. (Input)
412 * @param buff: The buffer containing the data to be written. (Input)
413 *
414 * @return Returns the total number of bytes written.
415 *
416 * Description:
417 * This function writes a packet of data to the specified FIFO using the USB controller.
418 * The function first checks if the USB controller handle and the data buffer are not NULL.
419 * Then, it adjusts the data by converting the buffer pointer to a 32-bit or 8-bit pointer based on the length.
420 * The function then writes the data to the FIFO in chunks of 4 bytes (32-bit) until all 32-bit chunks are written.
421 * After that, it processes the remaining non-4-byte part by writing 1 byte at a time.
422 * Finally, the function returns the total number of bytes written.
423 */
425
426/*
427 * Function name: usb_controller_read_packet
428 *
429 * @param husb: The USB controller handle. (Input)
430 * @param fifo: The FIFO number to read the packet from. (Input)
431 * @param cnt: The number of bytes to read. (Input)
432 * @param buff: The buffer to store the read data. (Output)
433 *
434 * @return Returns the total number of bytes read.
435 *
436 * Description:
437 * This function reads a packet of data from the specified FIFO using the USB controller.
438 * The function first checks if the USB controller handle and the data buffer are not NULL.
439 * Then, it adjusts the data by converting the buffer pointer to a 32-bit or 8-bit pointer based on the length.
440 * The function then reads the data from the FIFO in chunks of 4 bytes (32-bit) until all 32-bit chunks are read.
441 * After that, it processes the remaining non-4-byte part by reading 1 byte at a time.
442 * Finally, the function returns the total number of bytes read.
443 */
445
446/*
447 * Function name: usb_controller_config_fifo_base
448 *
449 * @param husb: The USB controller handle. (Input)
450 * @param sram_base: The base address of the SRAM to be mapped to the USB FIFO. (Input)
451 * @param fifo_mode: The mode of the FIFO. (Input)
452 *
453 * Description:
454 * This function maps the SRAM region D to be used by the USB FIFO.
455 * It takes the USB controller handle, the base address of the SRAM, and the FIFO mode as input parameters.
456 * The function first checks if the USB controller handle is not NULL.
457 * If the USB port number of the controller is 0, it sets the FIFO address and size for port 0.
458 * The FIFO size is set to 8KB (8192 bytes).
459 * Finally, the function returns.
460 */
462
463/*
464 * Function name: usb_controller_get_port_fifo_start_addr
465 *
466 * @param husb: The USB controller handle. (Input)
467 *
468 * @return Returns the start address of the port FIFO.
469 *
470 * Description:
471 * This function gets the start address of the port FIFO in the USB controller.
472 * It takes the USB controller handle as an input parameter.
473 * The function first checks if the USB controller handle is not NULL.
474 * If the port number in the USB controller handle is 0, it returns the start address of port 0 FIFO.
475 * If the port number is 1, it returns the start address of port 1 FIFO.
476 * If the port number is neither 0 nor 1, it returns the start address of port 2 FIFO.
477 */
479
480/*
481 * Function name: usb_controller_get_port_fifo_size
482 *
483 * @param husb: The USB controller handle. (Input)
484 *
485 * @return Returns the size of the port FIFO.
486 *
487 * Description:
488 * This function gets the size of the port FIFO in the USB controller.
489 * It takes the USB controller handle as an input parameter.
490 * The function first checks if the USB controller handle is not NULL.
491 * If the port number in the USB controller handle is 0, it returns the size of port 0 FIFO.
492 * Otherwise, it returns the size of port 1 FIFO. As same as port2 FIFO.
493 */
495
496/*
497 * Function name: usb_controller_select_fifo
498 *
499 * @param husb: The USB controller handle. (Input)
500 * @param ep_index: The endpoint index. (Input)
501 *
502 * @return Returns the FIFO address for the specified endpoint.
503 *
504 * Description:
505 * This function selects the FIFO for the specified endpoint in the USB controller.
506 * It takes the USB controller handle and the endpoint index as input parameters.
507 * The function first checks if the USB controller handle is not NULL.
508 * If it is NULL, it returns 0.
509 * Otherwise, it calculates and returns the FIFO address for the specified endpoint.
510 */
512
513#endif// __USB_CONTROLLER_H__
u64_t uint64_t
Definition stdint.h:16
u32_t uint32_t
Definition stdint.h:13
u8_t uint8_t
Definition stdint.h:7
#define writew(val, addr)
Write a 16-bit word value to the specified address.
Definition io.h:81
#define readw(addr)
Read a 16-bit word from the specified address.
Definition io.h:73
#define USBC_REG_INTTx(usbc_base_addr)
Definition reg-usb.h:110
#define USBC_REG_INTTxE(usbc_base_addr)
Definition reg-usb.h:112
#define USBC_REG_INTRx(usbc_base_addr)
Definition reg-usb.h:111
#define USBC_REG_INTRxE(usbc_base_addr)
Definition reg-usb.h:113
Definition usb_controller.h:20
uint32_t port0_fifo_size
Definition usb_controller.h:22
uint32_t port0_fifo_addr
Definition usb_controller.h:21
Definition usb_controller.h:26
uint32_t port_num
Definition usb_controller.h:27
uint32_t base_addr
Definition usb_controller.h:28
uint32_t used
Definition usb_controller.h:30
uint32_t no
Definition usb_controller.h:31
#define usb_clear_bit16(bp, reg)
Definition usb.h:182
#define usb_set_bit16(bp, reg)
Definition usb.h:178
uint32_t usb_controller_open_otg(uint32_t otg_no)
Open the USB OTG controller.
Definition usb_controller.c:22
void usb_controller_int_disable_usb_misc_uint(uint64_t husb, uint32_t mask)
Disable specific miscellaneous USB interrupts.
Definition usb_controller.c:259
static void usb_controller_int_disable_tx_all(uint32_t addr)
Disable the interrupts of all TX endpoints.
Definition usb_controller.h:137
static void usb_controller_int_disable_rx_all(uint32_t addr)
Disable the interrupts of all RX endpoints.
Definition usb_controller.h:146
void usb_controller_config_fifo_rx_ep_default(uint32_t addr)
Configure the FIFO for a default receive endpoint.
Definition usb_controller.c:344
void usb_controller_int_enable_ep(uint64_t husb, uint32_t ep_type, uint32_t ep_index)
Enable interrupts for a specific endpoint.
Definition usb_controller.c:272
static void usb_controller_int_clear_tx_pending(uint32_t addr, uint8_t ep_index)
Clear the interrupt pending flag of a TX endpoint.
Definition usb_controller.h:50
static uint32_t usb_controller_int_tx_pending(uint32_t addr)
Get the interrupt pending flag of a TX endpoint.
Definition usb_controller.h:40
void usb_controller_config_fifo_tx_ep_default(uint32_t addr)
Configure the FIFO for a default transmit endpoint.
Definition usb_controller.c:315
uint32_t usb_controller_int_misc_pending(uint64_t husb)
Get the pending miscellaneous interrupt status.
Definition usb_controller.c:467
static void usb_controller_int_enable_tx_ep(uint32_t addr, uint8_t ep_index)
Enable the interrupt of a TX endpoint.
Definition usb_controller.h:98
uint32_t usb_controller_get_active_ep(uint64_t husb)
Get the active endpoint for the USB controller.
Definition usb_controller.c:295
static void usb_controller_int_clear_rx_pending_all(uint32_t addr)
Clear the interrupt pending flags of all RX endpoints.
Definition usb_controller.h:88
void usb_controller_config_fifo_tx_ep(uint32_t addr, uint32_t is_double_fifo, uint32_t fifo_size, uint32_t fifo_addr)
Configure the FIFO for a transmit endpoint.
Definition usb_controller.c:320
static uint32_t usb_controller_int_rx_pending(uint32_t addr)
Get the interrupt pending flag of an RX endpoint.
Definition usb_controller.h:69
void usb_controller_int_clear_ep_pending(uint64_t husb, uint32_t ep_type, uint8_t ep_index)
Clear the pending interrupt flag for a specific endpoint.
Definition usb_controller.c:417
void usb_controller_int_clear_misc_pending_all(uint64_t husb)
Clear the pending miscellaneous interrupt flags for all interrupts.
Definition usb_controller.c:489
uint32_t usb_controller_read_len_from_fifo(uint64_t husb, uint32_t ep_type)
Definition usb_controller.c:547
uint32_t usb_controller_get_port_fifo_size(uint64_t husb)
Definition usb_controller.c:660
uint32_t usb_controller_get_vbus_status(uint64_t husb)
Definition usb_controller.c:523
void usb_controller_id_pull_enable(uint64_t husb)
Enable the ID pull-up resistor for the USB controller.
Definition usb_controller.c:148
static void usb_controller_int_disable_rx_ep(uint32_t addr, uint8_t ep_index)
Disable the interrupt of an RX endpoint.
Definition usb_controller.h:128
void usb_controller_dpdm_pull_disable(uint64_t husb)
Disable the DP/DM pull-up resistors for the USB controller.
Definition usb_controller.c:181
void usb_controller_int_clear_ep_pending_all(uint64_t husb, uint32_t ep_type)
Clear the pending interrupt flags for all endpoints of a specific type.
Definition usb_controller.c:442
static void usb_controller_int_clear_tx_pending_all(uint32_t addr)
Clear the interrupt pending flags of all TX endpoints.
Definition usb_controller.h:59
static void usb_controller_int_disable_tx_ep(uint32_t addr, uint8_t ep_index)
Disable the interrupt of a TX endpoint.
Definition usb_controller.h:118
void usb_controller_dpdm_pull_enable(uint64_t husb)
Enable the DP/DM pull-up resistors for the USB controller.
Definition usb_controller.c:170
uint32_t usb_controller_write_packet(uint64_t husb, uint32_t fifo, uint32_t cnt, void *buff)
Definition usb_controller.c:566
uint32_t usb_controller_get_port_fifo_start_addr(uint64_t husb)
Definition usb_controller.c:642
void usb_controller_force_vbus_valid(uint64_t husb, uint32_t vbus_type)
Force the VBUS valid state for the USB controller.
Definition usb_controller.c:132
uint32_t usb_controller_int_ep_pending(uint64_t husb, uint32_t ep_type)
Get the pending interrupt status for a specific endpoint.
Definition usb_controller.c:396
void usb_controller_int_enable_usb_misc_uint(uint64_t husb, uint32_t mask)
Enable specific miscellaneous USB interrupts.
Definition usb_controller.c:246
static void usb_controller_int_clear_rx_pending(uint32_t addr, uint8_t ep_index)
Clear the interrupt pending flag of an RX endpoint.
Definition usb_controller.h:79
int usb_controller_close_otg(uint64_t husb)
Close the USB OTG controller.
Definition usb_controller.c:37
void usb_controller_int_disable_usb_misc_all(uint64_t husb)
Disable all miscellaneous USB interrupts.
Definition usb_controller.c:213
uint32_t usb_controller_read_packet(uint64_t husb, uint32_t fifo, uint32_t cnt, void *buff)
Definition usb_controller.c:597
void usb_controller_config_fifo_base(uint64_t husb, uint32_t sram_base)
Definition usb_controller.c:627
struct usb_controller_otg usb_controller_otg_t
void usb_controller_int_disable_ep_all(uint64_t husb, uint32_t ep_type)
Disable all endpoint-specific interrupts.
Definition usb_controller.c:223
void usb_controller_force_id_status(uint64_t husb, uint32_t id_type)
Set the ID status for the USB controller.
Definition usb_controller.c:85
void usb_controller_id_pull_disable(uint64_t husb)
Disable the ID pull-up resistor for the USB controller.
Definition usb_controller.c:159
struct fifo_info fifo_info_t
void usb_controller_select_active_ep(uint64_t husb, uint8_t ep_index)
Select the active endpoint for the USB controller.
Definition usb_controller.c:305
void usb_controller_config_fifo(uint64_t husb, uint32_t ep_type, uint32_t is_double_fifo, uint32_t fifo_size, uint32_t fifo_addr)
Configure the FIFO for a specific endpoint.
Definition usb_controller.c:371
void usb_controller_config_fifo_rx_ep(uint32_t addr, uint32_t is_double_fifo, uint32_t fifo_size, uint32_t fifo_addr)
Configure the FIFO for a receive endpoint.
Definition usb_controller.c:349
uint32_t usb_controller_select_fifo(uint64_t husb, uint32_t ep_index)
Definition usb_controller.c:678
void usb_controller_int_clear_misc_pending(uint64_t husb, uint32_t mask)
Clear the pending miscellaneous interrupt flag.
Definition usb_controller.c:478
static void usb_controller_int_enable_rx_ep(uint32_t addr, uint8_t ep_index)
Enable the interrupt of an RX endpoint.
Definition usb_controller.h:108
void usb_controller_int_disable_ep(uint64_t husb, uint32_t ep_type, uint8_t ep_index)
Disable interrupts for a specific endpoint.
Definition usb_controller.c:500