2 * Mentor USB OTG Core functionality common for both Host and Device
5 * Copyright (c) 2008 Texas Instruments
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of
10 * the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * Author: Thomas Abraham t-abraham@ti.com, Texas Instruments
27 #include "musb_core.h"
28 struct musb_regs *musbr;
31 * program the mentor core to start (enable interrupts, dma, etc.)
35 #if defined(CONFIG_MUSB_HCD)
40 /* disable all interrupts */
41 writew(0, &musbr->intrtxe);
42 writew(0, &musbr->intrrxe);
43 writeb(0, &musbr->intrusbe);
44 writeb(0, &musbr->testmode);
46 /* put into basic highspeed mode and start session */
47 writeb(MUSB_POWER_HSENAB, &musbr->power);
48 #if defined(CONFIG_MUSB_HCD)
49 /* Program PHY to use EXT VBUS if required */
50 if (musb_cfg.extvbus == 1) {
51 busctl = musb_read_ulpi_buscontrol(musbr);
52 musb_write_ulpi_buscontrol(musbr, busctl | ULPI_USE_EXTVBUS);
55 devctl = readb(&musbr->devctl);
56 writeb(devctl | MUSB_DEVCTL_SESSION, &musbr->devctl);
60 #ifdef MUSB_NO_DYNAMIC_FIFO
61 # define config_fifo(dir, idx, addr)
63 # define config_fifo(dir, idx, addr) \
65 writeb(idx, &musbr->dir##fifosz); \
66 writew(fifoaddr >> 3, &musbr->dir##fifoadd); \
71 * This function configures the endpoint configuration. The musb hcd or musb
72 * device implementation can use this function to configure the endpoints
73 * and set the FIFO sizes. Note: The summation of FIFO sizes of all endpoints
74 * should not be more than the available FIFO size.
76 * epinfo - Pointer to EP configuration table
77 * cnt - Number of entries in the EP conf table.
79 void musb_configure_ep(struct musb_epinfo *epinfo, u8 cnt)
82 u16 fifoaddr = 64; /* First 64 bytes of FIFO reserved for EP0 */
87 /* prepare fifosize to write to register */
88 fifosize = epinfo->epsize >> 3;
89 idx = ffs(fifosize) - 1;
91 writeb(epinfo->epnum, &musbr->index);
93 /* Configure fifo size and fifo base address */
94 config_fifo(tx, idx, fifoaddr);
96 csr = readw(&musbr->txcsr);
97 #if defined(CONFIG_MUSB_HCD)
98 /* clear the data toggle bit */
99 writew(csr | MUSB_TXCSR_CLRDATATOG, &musbr->txcsr);
101 /* Flush fifo if required */
102 if (csr & MUSB_TXCSR_TXPKTRDY)
103 writew(csr | MUSB_TXCSR_FLUSHFIFO,
106 /* Configure fifo size and fifo base address */
107 config_fifo(rx, idx, fifoaddr);
109 csr = readw(&musbr->rxcsr);
110 #if defined(CONFIG_MUSB_HCD)
111 /* clear the data toggle bit */
112 writew(csr | MUSB_RXCSR_CLRDATATOG, &musbr->rxcsr);
114 /* Flush fifo if required */
115 if (csr & MUSB_RXCSR_RXPKTRDY)
116 writew(csr | MUSB_RXCSR_FLUSHFIFO,
119 fifoaddr += epinfo->epsize;
125 * This function writes data to endpoint fifo
127 * ep - endpoint number
128 * length - number of bytes to write to FIFO
129 * fifo_data - Pointer to data buffer that contains the data to write
131 __attribute__((weak))
132 void write_fifo(u8 ep, u32 length, void *fifo_data)
134 u8 *data = (u8 *)fifo_data;
136 /* select the endpoint index */
137 writeb(ep, &musbr->index);
139 /* write the data to the fifo */
141 writeb(*data++, &musbr->fifox[ep]);
145 * This function reads data from endpoint fifo
147 * ep - endpoint number
148 * length - number of bytes to read from FIFO
149 * fifo_data - pointer to data buffer into which data is read
151 __attribute__((weak))
152 void read_fifo(u8 ep, u32 length, void *fifo_data)
154 u8 *data = (u8 *)fifo_data;
156 /* select the endpoint index */
157 writeb(ep, &musbr->index);
159 /* read the data to the fifo */
161 *data++ = readb(&musbr->fifox[ep]);