12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252 |
- /*
- * MUSB OTG driver core code
- *
- * Copyright 2005 Mentor Graphics Corporation
- * Copyright (C) 2005-2006 by Texas Instruments
- * Copyright (C) 2006-2007 Nokia Corporation
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * version 2 as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
- * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
- * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
- * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
- * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- */
- /*
- * Inventra (Multipoint) Dual-Role Controller Driver for Linux.
- *
- * This consists of a Host Controller Driver (HCD) and a peripheral
- * controller driver implementing the "Gadget" API; OTG support is
- * in the works. These are normal Linux-USB controller drivers which
- * use IRQs and have no dedicated thread.
- *
- * This version of the driver has only been used with products from
- * Texas Instruments. Those products integrate the Inventra logic
- * with other DMA, IRQ, and bus modules, as well as other logic that
- * needs to be reflected in this driver.
- *
- *
- * NOTE: the original Mentor code here was pretty much a collection
- * of mechanisms that don't seem to have been fully integrated/working
- * for *any* Linux kernel version. This version aims at Linux 2.6.now,
- * Key open issues include:
- *
- * - Lack of host-side transaction scheduling, for all transfer types.
- * The hardware doesn't do it; instead, software must.
- *
- * This is not an issue for OTG devices that don't support external
- * hubs, but for more "normal" USB hosts it's a user issue that the
- * "multipoint" support doesn't scale in the expected ways. That
- * includes DaVinci EVM in a common non-OTG mode.
- *
- * * Control and bulk use dedicated endpoints, and there's as
- * yet no mechanism to either (a) reclaim the hardware when
- * peripherals are NAKing, which gets complicated with bulk
- * endpoints, or (b) use more than a single bulk endpoint in
- * each direction.
- *
- * RESULT: one device may be perceived as blocking another one.
- *
- * * Interrupt and isochronous will dynamically allocate endpoint
- * hardware, but (a) there's no record keeping for bandwidth;
- * (b) in the common case that few endpoints are available, there
- * is no mechanism to reuse endpoints to talk to multiple devices.
- *
- * RESULT: At one extreme, bandwidth can be overcommitted in
- * some hardware configurations, no faults will be reported.
- * At the other extreme, the bandwidth capabilities which do
- * exist tend to be severely undercommitted. You can't yet hook
- * up both a keyboard and a mouse to an external USB hub.
- */
- /*
- * This gets many kinds of configuration information:
- * - Kconfig for everything user-configurable
- * - platform_device for addressing, irq, and platform_data
- * - platform_data is mostly for board-specific informarion
- * (plus recentrly, SOC or family details)
- *
- * Most of the conditional compilation will (someday) vanish.
- */
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/slab.h>
- #include <linux/init.h>
- #include <linux/list.h>
- #include <linux/kobject.h>
- #include <linux/platform_device.h>
- #include <linux/io.h>
- #ifdef CONFIG_ARM
- #include <mach/hardware.h>
- #include <mach/memory.h>
- #include <asm/mach-types.h>
- #endif
- #include "musb_core.h"
- #ifdef CONFIG_ARCH_DAVINCI
- #include "davinci.h"
- #endif
- unsigned musb_debug;
- module_param_named(debug, musb_debug, uint, S_IRUGO | S_IWUSR);
- MODULE_PARM_DESC(debug, "Debug message level. Default = 0");
- #define DRIVER_AUTHOR "Mentor Graphics, Texas Instruments, Nokia"
- #define DRIVER_DESC "Inventra Dual-Role USB Controller Driver"
- #define MUSB_VERSION "6.0"
- #define DRIVER_INFO DRIVER_DESC ", v" MUSB_VERSION
- #define MUSB_DRIVER_NAME "musb_hdrc"
- const char musb_driver_name[] = MUSB_DRIVER_NAME;
- MODULE_DESCRIPTION(DRIVER_INFO);
- MODULE_AUTHOR(DRIVER_AUTHOR);
- MODULE_LICENSE("GPL");
- MODULE_ALIAS("platform:" MUSB_DRIVER_NAME);
- /*-------------------------------------------------------------------------*/
- static inline struct musb *dev_to_musb(struct device *dev)
- {
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- /* usbcore insists dev->driver_data is a "struct hcd *" */
- return hcd_to_musb(dev_get_drvdata(dev));
- #else
- return dev_get_drvdata(dev);
- #endif
- }
- /*-------------------------------------------------------------------------*/
- #if !defined(CONFIG_USB_TUSB6010) && !defined(CONFIG_BLACKFIN)
- /*
- * Load an endpoint's FIFO
- */
- void musb_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
- {
- void __iomem *fifo = hw_ep->fifo;
- prefetch((u8 *)src);
- DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
- 'T', hw_ep->epnum, fifo, len, src);
- /* we can't assume unaligned reads work */
- if (likely((0x01 & (unsigned long) src) == 0)) {
- u16 index = 0;
- /* best case is 32bit-aligned source address */
- if ((0x02 & (unsigned long) src) == 0) {
- if (len >= 4) {
- writesl(fifo, src + index, len >> 2);
- index += len & ~0x03;
- }
- if (len & 0x02) {
- musb_writew(fifo, 0, *(u16 *)&src[index]);
- index += 2;
- }
- } else {
- if (len >= 2) {
- writesw(fifo, src + index, len >> 1);
- index += len & ~0x01;
- }
- }
- if (len & 0x01)
- musb_writeb(fifo, 0, src[index]);
- } else {
- /* byte aligned */
- writesb(fifo, src, len);
- }
- }
- /*
- * Unload an endpoint's FIFO
- */
- void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
- {
- void __iomem *fifo = hw_ep->fifo;
- DBG(4, "%cX ep%d fifo %p count %d buf %p\n",
- 'R', hw_ep->epnum, fifo, len, dst);
- /* we can't assume unaligned writes work */
- if (likely((0x01 & (unsigned long) dst) == 0)) {
- u16 index = 0;
- /* best case is 32bit-aligned destination address */
- if ((0x02 & (unsigned long) dst) == 0) {
- if (len >= 4) {
- readsl(fifo, dst, len >> 2);
- index = len & ~0x03;
- }
- if (len & 0x02) {
- *(u16 *)&dst[index] = musb_readw(fifo, 0);
- index += 2;
- }
- } else {
- if (len >= 2) {
- readsw(fifo, dst, len >> 1);
- index = len & ~0x01;
- }
- }
- if (len & 0x01)
- dst[index] = musb_readb(fifo, 0);
- } else {
- /* byte aligned */
- readsb(fifo, dst, len);
- }
- }
- #endif /* normal PIO */
- /*-------------------------------------------------------------------------*/
- /* for high speed test mode; see USB 2.0 spec 7.1.20 */
- static const u8 musb_test_packet[53] = {
- /* implicit SYNC then DATA0 to start */
- /* JKJKJKJK x9 */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- /* JJKKJJKK x8 */
- 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
- /* JJJJKKKK x8 */
- 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee, 0xee,
- /* JJJJJJJKKKKKKK x8 */
- 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
- /* JJJJJJJK x8 */
- 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd,
- /* JKKKKKKK x10, JK */
- 0xfc, 0x7e, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0x7e
- /* implicit CRC16 then EOP to end */
- };
- void musb_load_testpacket(struct musb *musb)
- {
- void __iomem *regs = musb->endpoints[0].regs;
- musb_ep_select(musb->mregs, 0);
- musb_write_fifo(musb->control_ep,
- sizeof(musb_test_packet), musb_test_packet);
- musb_writew(regs, MUSB_CSR0, MUSB_CSR0_TXPKTRDY);
- }
- /*-------------------------------------------------------------------------*/
- const char *otg_state_string(struct musb *musb)
- {
- switch (musb->xceiv.state) {
- case OTG_STATE_A_IDLE: return "a_idle";
- case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise";
- case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon";
- case OTG_STATE_A_HOST: return "a_host";
- case OTG_STATE_A_SUSPEND: return "a_suspend";
- case OTG_STATE_A_PERIPHERAL: return "a_peripheral";
- case OTG_STATE_A_WAIT_VFALL: return "a_wait_vfall";
- case OTG_STATE_A_VBUS_ERR: return "a_vbus_err";
- case OTG_STATE_B_IDLE: return "b_idle";
- case OTG_STATE_B_SRP_INIT: return "b_srp_init";
- case OTG_STATE_B_PERIPHERAL: return "b_peripheral";
- case OTG_STATE_B_WAIT_ACON: return "b_wait_acon";
- case OTG_STATE_B_HOST: return "b_host";
- default: return "UNDEFINED";
- }
- }
- #ifdef CONFIG_USB_MUSB_OTG
- /*
- * See also USB_OTG_1-3.pdf 6.6.5 Timers
- * REVISIT: Are the other timers done in the hardware?
- */
- #define TB_ASE0_BRST 100 /* Min 3.125 ms */
- /*
- * Handles OTG hnp timeouts, such as b_ase0_brst
- */
- void musb_otg_timer_func(unsigned long data)
- {
- struct musb *musb = (struct musb *)data;
- unsigned long flags;
- spin_lock_irqsave(&musb->lock, flags);
- switch (musb->xceiv.state) {
- case OTG_STATE_B_WAIT_ACON:
- DBG(1, "HNP: b_wait_acon timeout; back to b_peripheral\n");
- musb_g_disconnect(musb);
- musb->xceiv.state = OTG_STATE_B_PERIPHERAL;
- musb->is_active = 0;
- break;
- case OTG_STATE_A_WAIT_BCON:
- DBG(1, "HNP: a_wait_bcon timeout; back to a_host\n");
- musb_hnp_stop(musb);
- break;
- default:
- DBG(1, "HNP: Unhandled mode %s\n", otg_state_string(musb));
- }
- musb->ignore_disconnect = 0;
- spin_unlock_irqrestore(&musb->lock, flags);
- }
- static DEFINE_TIMER(musb_otg_timer, musb_otg_timer_func, 0, 0);
- /*
- * Stops the B-device HNP state. Caller must take care of locking.
- */
- void musb_hnp_stop(struct musb *musb)
- {
- struct usb_hcd *hcd = musb_to_hcd(musb);
- void __iomem *mbase = musb->mregs;
- u8 reg;
- switch (musb->xceiv.state) {
- case OTG_STATE_A_PERIPHERAL:
- case OTG_STATE_A_WAIT_VFALL:
- case OTG_STATE_A_WAIT_BCON:
- DBG(1, "HNP: Switching back to A-host\n");
- musb_g_disconnect(musb);
- musb->xceiv.state = OTG_STATE_A_IDLE;
- MUSB_HST_MODE(musb);
- musb->is_active = 0;
- break;
- case OTG_STATE_B_HOST:
- DBG(1, "HNP: Disabling HR\n");
- hcd->self.is_b_host = 0;
- musb->xceiv.state = OTG_STATE_B_PERIPHERAL;
- MUSB_DEV_MODE(musb);
- reg = musb_readb(mbase, MUSB_POWER);
- reg |= MUSB_POWER_SUSPENDM;
- musb_writeb(mbase, MUSB_POWER, reg);
- /* REVISIT: Start SESSION_REQUEST here? */
- break;
- default:
- DBG(1, "HNP: Stopping in unknown state %s\n",
- otg_state_string(musb));
- }
- /*
- * When returning to A state after HNP, avoid hub_port_rebounce(),
- * which cause occasional OPT A "Did not receive reset after connect"
- * errors.
- */
- musb->port1_status &=
- ~(1 << USB_PORT_FEAT_C_CONNECTION);
- }
- #endif
- /*
- * Interrupt Service Routine to record USB "global" interrupts.
- * Since these do not happen often and signify things of
- * paramount importance, it seems OK to check them individually;
- * the order of the tests is specified in the manual
- *
- * @param musb instance pointer
- * @param int_usb register contents
- * @param devctl
- * @param power
- */
- #define STAGE0_MASK (MUSB_INTR_RESUME | MUSB_INTR_SESSREQ \
- | MUSB_INTR_VBUSERROR | MUSB_INTR_CONNECT \
- | MUSB_INTR_RESET)
- static irqreturn_t musb_stage0_irq(struct musb *musb, u8 int_usb,
- u8 devctl, u8 power)
- {
- irqreturn_t handled = IRQ_NONE;
- void __iomem *mbase = musb->mregs;
- DBG(3, "<== Power=%02x, DevCtl=%02x, int_usb=0x%x\n", power, devctl,
- int_usb);
- /* in host mode, the peripheral may issue remote wakeup.
- * in peripheral mode, the host may resume the link.
- * spurious RESUME irqs happen too, paired with SUSPEND.
- */
- if (int_usb & MUSB_INTR_RESUME) {
- handled = IRQ_HANDLED;
- DBG(3, "RESUME (%s)\n", otg_state_string(musb));
- if (devctl & MUSB_DEVCTL_HM) {
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- switch (musb->xceiv.state) {
- case OTG_STATE_A_SUSPEND:
- /* remote wakeup? later, GetPortStatus
- * will stop RESUME signaling
- */
- if (power & MUSB_POWER_SUSPENDM) {
- /* spurious */
- musb->int_usb &= ~MUSB_INTR_SUSPEND;
- DBG(2, "Spurious SUSPENDM\n");
- break;
- }
- power &= ~MUSB_POWER_SUSPENDM;
- musb_writeb(mbase, MUSB_POWER,
- power | MUSB_POWER_RESUME);
- musb->port1_status |=
- (USB_PORT_STAT_C_SUSPEND << 16)
- | MUSB_PORT_STAT_RESUME;
- musb->rh_timer = jiffies
- + msecs_to_jiffies(20);
- musb->xceiv.state = OTG_STATE_A_HOST;
- musb->is_active = 1;
- usb_hcd_resume_root_hub(musb_to_hcd(musb));
- break;
- case OTG_STATE_B_WAIT_ACON:
- musb->xceiv.state = OTG_STATE_B_PERIPHERAL;
- musb->is_active = 1;
- MUSB_DEV_MODE(musb);
- break;
- default:
- WARNING("bogus %s RESUME (%s)\n",
- "host",
- otg_state_string(musb));
- }
- #endif
- } else {
- switch (musb->xceiv.state) {
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- case OTG_STATE_A_SUSPEND:
- /* possibly DISCONNECT is upcoming */
- musb->xceiv.state = OTG_STATE_A_HOST;
- usb_hcd_resume_root_hub(musb_to_hcd(musb));
- break;
- #endif
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- case OTG_STATE_B_WAIT_ACON:
- case OTG_STATE_B_PERIPHERAL:
- /* disconnect while suspended? we may
- * not get a disconnect irq...
- */
- if ((devctl & MUSB_DEVCTL_VBUS)
- != (3 << MUSB_DEVCTL_VBUS_SHIFT)
- ) {
- musb->int_usb |= MUSB_INTR_DISCONNECT;
- musb->int_usb &= ~MUSB_INTR_SUSPEND;
- break;
- }
- musb_g_resume(musb);
- break;
- case OTG_STATE_B_IDLE:
- musb->int_usb &= ~MUSB_INTR_SUSPEND;
- break;
- #endif
- default:
- WARNING("bogus %s RESUME (%s)\n",
- "peripheral",
- otg_state_string(musb));
- }
- }
- }
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- /* see manual for the order of the tests */
- if (int_usb & MUSB_INTR_SESSREQ) {
- DBG(1, "SESSION_REQUEST (%s)\n", otg_state_string(musb));
- /* IRQ arrives from ID pin sense or (later, if VBUS power
- * is removed) SRP. responses are time critical:
- * - turn on VBUS (with silicon-specific mechanism)
- * - go through A_WAIT_VRISE
- * - ... to A_WAIT_BCON.
- * a_wait_vrise_tmout triggers VBUS_ERROR transitions
- */
- musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
- musb->ep0_stage = MUSB_EP0_START;
- musb->xceiv.state = OTG_STATE_A_IDLE;
- MUSB_HST_MODE(musb);
- musb_set_vbus(musb, 1);
- handled = IRQ_HANDLED;
- }
- if (int_usb & MUSB_INTR_VBUSERROR) {
- int ignore = 0;
- /* During connection as an A-Device, we may see a short
- * current spikes causing voltage drop, because of cable
- * and peripheral capacitance combined with vbus draw.
- * (So: less common with truly self-powered devices, where
- * vbus doesn't act like a power supply.)
- *
- * Such spikes are short; usually less than ~500 usec, max
- * of ~2 msec. That is, they're not sustained overcurrent
- * errors, though they're reported using VBUSERROR irqs.
- *
- * Workarounds: (a) hardware: use self powered devices.
- * (b) software: ignore non-repeated VBUS errors.
- *
- * REVISIT: do delays from lots of DEBUG_KERNEL checks
- * make trouble here, keeping VBUS < 4.4V ?
- */
- switch (musb->xceiv.state) {
- case OTG_STATE_A_HOST:
- /* recovery is dicey once we've gotten past the
- * initial stages of enumeration, but if VBUS
- * stayed ok at the other end of the link, and
- * another reset is due (at least for high speed,
- * to redo the chirp etc), it might work OK...
- */
- case OTG_STATE_A_WAIT_BCON:
- case OTG_STATE_A_WAIT_VRISE:
- if (musb->vbuserr_retry) {
- musb->vbuserr_retry--;
- ignore = 1;
- devctl |= MUSB_DEVCTL_SESSION;
- musb_writeb(mbase, MUSB_DEVCTL, devctl);
- } else {
- musb->port1_status |=
- (1 << USB_PORT_FEAT_OVER_CURRENT)
- | (1 << USB_PORT_FEAT_C_OVER_CURRENT);
- }
- break;
- default:
- break;
- }
- DBG(1, "VBUS_ERROR in %s (%02x, %s), retry #%d, port1 %08x\n",
- otg_state_string(musb),
- devctl,
- ({ char *s;
- switch (devctl & MUSB_DEVCTL_VBUS) {
- case 0 << MUSB_DEVCTL_VBUS_SHIFT:
- s = "<SessEnd"; break;
- case 1 << MUSB_DEVCTL_VBUS_SHIFT:
- s = "<AValid"; break;
- case 2 << MUSB_DEVCTL_VBUS_SHIFT:
- s = "<VBusValid"; break;
- /* case 3 << MUSB_DEVCTL_VBUS_SHIFT: */
- default:
- s = "VALID"; break;
- }; s; }),
- VBUSERR_RETRY_COUNT - musb->vbuserr_retry,
- musb->port1_status);
- /* go through A_WAIT_VFALL then start a new session */
- if (!ignore)
- musb_set_vbus(musb, 0);
- handled = IRQ_HANDLED;
- }
- if (int_usb & MUSB_INTR_CONNECT) {
- struct usb_hcd *hcd = musb_to_hcd(musb);
- handled = IRQ_HANDLED;
- musb->is_active = 1;
- set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
- musb->ep0_stage = MUSB_EP0_START;
- #ifdef CONFIG_USB_MUSB_OTG
- /* flush endpoints when transitioning from Device Mode */
- if (is_peripheral_active(musb)) {
- /* REVISIT HNP; just force disconnect */
- }
- musb_writew(mbase, MUSB_INTRTXE, musb->epmask);
- musb_writew(mbase, MUSB_INTRRXE, musb->epmask & 0xfffe);
- musb_writeb(mbase, MUSB_INTRUSBE, 0xf7);
- #endif
- musb->port1_status &= ~(USB_PORT_STAT_LOW_SPEED
- |USB_PORT_STAT_HIGH_SPEED
- |USB_PORT_STAT_ENABLE
- );
- musb->port1_status |= USB_PORT_STAT_CONNECTION
- |(USB_PORT_STAT_C_CONNECTION << 16);
- /* high vs full speed is just a guess until after reset */
- if (devctl & MUSB_DEVCTL_LSDEV)
- musb->port1_status |= USB_PORT_STAT_LOW_SPEED;
- if (hcd->status_urb)
- usb_hcd_poll_rh_status(hcd);
- else
- usb_hcd_resume_root_hub(hcd);
- MUSB_HST_MODE(musb);
- /* indicate new connection to OTG machine */
- switch (musb->xceiv.state) {
- case OTG_STATE_B_PERIPHERAL:
- if (int_usb & MUSB_INTR_SUSPEND) {
- DBG(1, "HNP: SUSPEND+CONNECT, now b_host\n");
- musb->xceiv.state = OTG_STATE_B_HOST;
- hcd->self.is_b_host = 1;
- int_usb &= ~MUSB_INTR_SUSPEND;
- } else
- DBG(1, "CONNECT as b_peripheral???\n");
- break;
- case OTG_STATE_B_WAIT_ACON:
- DBG(1, "HNP: Waiting to switch to b_host state\n");
- musb->xceiv.state = OTG_STATE_B_HOST;
- hcd->self.is_b_host = 1;
- break;
- default:
- if ((devctl & MUSB_DEVCTL_VBUS)
- == (3 << MUSB_DEVCTL_VBUS_SHIFT)) {
- musb->xceiv.state = OTG_STATE_A_HOST;
- hcd->self.is_b_host = 0;
- }
- break;
- }
- DBG(1, "CONNECT (%s) devctl %02x\n",
- otg_state_string(musb), devctl);
- }
- #endif /* CONFIG_USB_MUSB_HDRC_HCD */
- /* mentor saves a bit: bus reset and babble share the same irq.
- * only host sees babble; only peripheral sees bus reset.
- */
- if (int_usb & MUSB_INTR_RESET) {
- if (is_host_capable() && (devctl & MUSB_DEVCTL_HM) != 0) {
- /*
- * Looks like non-HS BABBLE can be ignored, but
- * HS BABBLE is an error condition. For HS the solution
- * is to avoid babble in the first place and fix what
- * caused BABBLE. When HS BABBLE happens we can only
- * stop the session.
- */
- if (devctl & (MUSB_DEVCTL_FSDEV | MUSB_DEVCTL_LSDEV))
- DBG(1, "BABBLE devctl: %02x\n", devctl);
- else {
- ERR("Stopping host session -- babble\n");
- musb_writeb(mbase, MUSB_DEVCTL, 0);
- }
- } else if (is_peripheral_capable()) {
- DBG(1, "BUS RESET as %s\n", otg_state_string(musb));
- switch (musb->xceiv.state) {
- #ifdef CONFIG_USB_OTG
- case OTG_STATE_A_SUSPEND:
- /* We need to ignore disconnect on suspend
- * otherwise tusb 2.0 won't reconnect after a
- * power cycle, which breaks otg compliance.
- */
- musb->ignore_disconnect = 1;
- musb_g_reset(musb);
- /* FALLTHROUGH */
- case OTG_STATE_A_WAIT_BCON: /* OPT TD.4.7-900ms */
- DBG(1, "HNP: Setting timer as %s\n",
- otg_state_string(musb));
- musb_otg_timer.data = (unsigned long)musb;
- mod_timer(&musb_otg_timer, jiffies
- + msecs_to_jiffies(100));
- break;
- case OTG_STATE_A_PERIPHERAL:
- musb_hnp_stop(musb);
- break;
- case OTG_STATE_B_WAIT_ACON:
- DBG(1, "HNP: RESET (%s), to b_peripheral\n",
- otg_state_string(musb));
- musb->xceiv.state = OTG_STATE_B_PERIPHERAL;
- musb_g_reset(musb);
- break;
- #endif
- case OTG_STATE_B_IDLE:
- musb->xceiv.state = OTG_STATE_B_PERIPHERAL;
- /* FALLTHROUGH */
- case OTG_STATE_B_PERIPHERAL:
- musb_g_reset(musb);
- break;
- default:
- DBG(1, "Unhandled BUS RESET as %s\n",
- otg_state_string(musb));
- }
- }
- handled = IRQ_HANDLED;
- }
- schedule_work(&musb->irq_work);
- return handled;
- }
- /*
- * Interrupt Service Routine to record USB "global" interrupts.
- * Since these do not happen often and signify things of
- * paramount importance, it seems OK to check them individually;
- * the order of the tests is specified in the manual
- *
- * @param musb instance pointer
- * @param int_usb register contents
- * @param devctl
- * @param power
- */
- static irqreturn_t musb_stage2_irq(struct musb *musb, u8 int_usb,
- u8 devctl, u8 power)
- {
- irqreturn_t handled = IRQ_NONE;
- #if 0
- /* REVISIT ... this would be for multiplexing periodic endpoints, or
- * supporting transfer phasing to prevent exceeding ISO bandwidth
- * limits of a given frame or microframe.
- *
- * It's not needed for peripheral side, which dedicates endpoints;
- * though it _might_ use SOF irqs for other purposes.
- *
- * And it's not currently needed for host side, which also dedicates
- * endpoints, relies on TX/RX interval registers, and isn't claimed
- * to support ISO transfers yet.
- */
- if (int_usb & MUSB_INTR_SOF) {
- void __iomem *mbase = musb->mregs;
- struct musb_hw_ep *ep;
- u8 epnum;
- u16 frame;
- DBG(6, "START_OF_FRAME\n");
- handled = IRQ_HANDLED;
- /* start any periodic Tx transfers waiting for current frame */
- frame = musb_readw(mbase, MUSB_FRAME);
- ep = musb->endpoints;
- for (epnum = 1; (epnum < musb->nr_endpoints)
- && (musb->epmask >= (1 << epnum));
- epnum++, ep++) {
- /*
- * FIXME handle framecounter wraps (12 bits)
- * eliminate duplicated StartUrb logic
- */
- if (ep->dwWaitFrame >= frame) {
- ep->dwWaitFrame = 0;
- pr_debug("SOF --> periodic TX%s on %d\n",
- ep->tx_channel ? " DMA" : "",
- epnum);
- if (!ep->tx_channel)
- musb_h_tx_start(musb, epnum);
- else
- cppi_hostdma_start(musb, epnum);
- }
- } /* end of for loop */
- }
- #endif
- if ((int_usb & MUSB_INTR_DISCONNECT) && !musb->ignore_disconnect) {
- DBG(1, "DISCONNECT (%s) as %s, devctl %02x\n",
- otg_state_string(musb),
- MUSB_MODE(musb), devctl);
- handled = IRQ_HANDLED;
- switch (musb->xceiv.state) {
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- case OTG_STATE_A_HOST:
- case OTG_STATE_A_SUSPEND:
- usb_hcd_resume_root_hub(musb_to_hcd(musb));
- musb_root_disconnect(musb);
- if (musb->a_wait_bcon != 0 && is_otg_enabled(musb))
- musb_platform_try_idle(musb, jiffies
- + msecs_to_jiffies(musb->a_wait_bcon));
- break;
- #endif /* HOST */
- #ifdef CONFIG_USB_MUSB_OTG
- case OTG_STATE_B_HOST:
- musb_hnp_stop(musb);
- break;
- case OTG_STATE_A_PERIPHERAL:
- musb_hnp_stop(musb);
- musb_root_disconnect(musb);
- /* FALLTHROUGH */
- case OTG_STATE_B_WAIT_ACON:
- /* FALLTHROUGH */
- #endif /* OTG */
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- case OTG_STATE_B_PERIPHERAL:
- case OTG_STATE_B_IDLE:
- musb_g_disconnect(musb);
- break;
- #endif /* GADGET */
- default:
- WARNING("unhandled DISCONNECT transition (%s)\n",
- otg_state_string(musb));
- break;
- }
- schedule_work(&musb->irq_work);
- }
- if (int_usb & MUSB_INTR_SUSPEND) {
- DBG(1, "SUSPEND (%s) devctl %02x power %02x\n",
- otg_state_string(musb), devctl, power);
- handled = IRQ_HANDLED;
- switch (musb->xceiv.state) {
- #ifdef CONFIG_USB_MUSB_OTG
- case OTG_STATE_A_PERIPHERAL:
- /*
- * We cannot stop HNP here, devctl BDEVICE might be
- * still set.
- */
- break;
- #endif
- case OTG_STATE_B_PERIPHERAL:
- musb_g_suspend(musb);
- musb->is_active = is_otg_enabled(musb)
- && musb->xceiv.gadget->b_hnp_enable;
- if (musb->is_active) {
- #ifdef CONFIG_USB_MUSB_OTG
- musb->xceiv.state = OTG_STATE_B_WAIT_ACON;
- DBG(1, "HNP: Setting timer for b_ase0_brst\n");
- musb_otg_timer.data = (unsigned long)musb;
- mod_timer(&musb_otg_timer, jiffies
- + msecs_to_jiffies(TB_ASE0_BRST));
- #endif
- }
- break;
- case OTG_STATE_A_WAIT_BCON:
- if (musb->a_wait_bcon != 0)
- musb_platform_try_idle(musb, jiffies
- + msecs_to_jiffies(musb->a_wait_bcon));
- break;
- case OTG_STATE_A_HOST:
- musb->xceiv.state = OTG_STATE_A_SUSPEND;
- musb->is_active = is_otg_enabled(musb)
- && musb->xceiv.host->b_hnp_enable;
- break;
- case OTG_STATE_B_HOST:
- /* Transition to B_PERIPHERAL, see 6.8.2.6 p 44 */
- DBG(1, "REVISIT: SUSPEND as B_HOST\n");
- break;
- default:
- /* "should not happen" */
- musb->is_active = 0;
- break;
- }
- schedule_work(&musb->irq_work);
- }
- return handled;
- }
- /*-------------------------------------------------------------------------*/
- /*
- * Program the HDRC to start (enable interrupts, dma, etc.).
- */
- void musb_start(struct musb *musb)
- {
- void __iomem *regs = musb->mregs;
- u8 devctl = musb_readb(regs, MUSB_DEVCTL);
- DBG(2, "<== devctl %02x\n", devctl);
- /* Set INT enable registers, enable interrupts */
- musb_writew(regs, MUSB_INTRTXE, musb->epmask);
- musb_writew(regs, MUSB_INTRRXE, musb->epmask & 0xfffe);
- musb_writeb(regs, MUSB_INTRUSBE, 0xf7);
- musb_writeb(regs, MUSB_TESTMODE, 0);
- /* put into basic highspeed mode and start session */
- musb_writeb(regs, MUSB_POWER, MUSB_POWER_ISOUPDATE
- | MUSB_POWER_SOFTCONN
- | MUSB_POWER_HSENAB
- /* ENSUSPEND wedges tusb */
- /* | MUSB_POWER_ENSUSPEND */
- );
- musb->is_active = 0;
- devctl = musb_readb(regs, MUSB_DEVCTL);
- devctl &= ~MUSB_DEVCTL_SESSION;
- if (is_otg_enabled(musb)) {
- /* session started after:
- * (a) ID-grounded irq, host mode;
- * (b) vbus present/connect IRQ, peripheral mode;
- * (c) peripheral initiates, using SRP
- */
- if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
- musb->is_active = 1;
- else
- devctl |= MUSB_DEVCTL_SESSION;
- } else if (is_host_enabled(musb)) {
- /* assume ID pin is hard-wired to ground */
- devctl |= MUSB_DEVCTL_SESSION;
- } else /* peripheral is enabled */ {
- if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
- musb->is_active = 1;
- }
- musb_platform_enable(musb);
- musb_writeb(regs, MUSB_DEVCTL, devctl);
- }
- static void musb_generic_disable(struct musb *musb)
- {
- void __iomem *mbase = musb->mregs;
- u16 temp;
- /* disable interrupts */
- musb_writeb(mbase, MUSB_INTRUSBE, 0);
- musb_writew(mbase, MUSB_INTRTXE, 0);
- musb_writew(mbase, MUSB_INTRRXE, 0);
- /* off */
- musb_writeb(mbase, MUSB_DEVCTL, 0);
- /* flush pending interrupts */
- temp = musb_readb(mbase, MUSB_INTRUSB);
- temp = musb_readw(mbase, MUSB_INTRTX);
- temp = musb_readw(mbase, MUSB_INTRRX);
- }
- /*
- * Make the HDRC stop (disable interrupts, etc.);
- * reversible by musb_start
- * called on gadget driver unregister
- * with controller locked, irqs blocked
- * acts as a NOP unless some role activated the hardware
- */
- void musb_stop(struct musb *musb)
- {
- /* stop IRQs, timers, ... */
- musb_platform_disable(musb);
- musb_generic_disable(musb);
- DBG(3, "HDRC disabled\n");
- /* FIXME
- * - mark host and/or peripheral drivers unusable/inactive
- * - disable DMA (and enable it in HdrcStart)
- * - make sure we can musb_start() after musb_stop(); with
- * OTG mode, gadget driver module rmmod/modprobe cycles that
- * - ...
- */
- musb_platform_try_idle(musb, 0);
- }
- static void musb_shutdown(struct platform_device *pdev)
- {
- struct musb *musb = dev_to_musb(&pdev->dev);
- unsigned long flags;
- spin_lock_irqsave(&musb->lock, flags);
- musb_platform_disable(musb);
- musb_generic_disable(musb);
- if (musb->clock) {
- clk_put(musb->clock);
- musb->clock = NULL;
- }
- spin_unlock_irqrestore(&musb->lock, flags);
- /* FIXME power down */
- }
- /*-------------------------------------------------------------------------*/
- /*
- * The silicon either has hard-wired endpoint configurations, or else
- * "dynamic fifo" sizing. The driver has support for both, though at this
- * writing only the dynamic sizing is very well tested. Since we switched
- * away from compile-time hardware parameters, we can no longer rely on
- * dead code elimination to leave only the relevant one in the object file.
- *
- * We don't currently use dynamic fifo setup capability to do anything
- * more than selecting one of a bunch of predefined configurations.
- */
- #if defined(CONFIG_USB_TUSB6010) || \
- defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP34XX)
- static ushort __initdata fifo_mode = 4;
- #else
- static ushort __initdata fifo_mode = 2;
- #endif
- /* "modprobe ... fifo_mode=1" etc */
- module_param(fifo_mode, ushort, 0);
- MODULE_PARM_DESC(fifo_mode, "initial endpoint configuration");
- enum fifo_style { FIFO_RXTX, FIFO_TX, FIFO_RX } __attribute__ ((packed));
- enum buf_mode { BUF_SINGLE, BUF_DOUBLE } __attribute__ ((packed));
- struct fifo_cfg {
- u8 hw_ep_num;
- enum fifo_style style;
- enum buf_mode mode;
- u16 maxpacket;
- };
- /*
- * tables defining fifo_mode values. define more if you like.
- * for host side, make sure both halves of ep1 are set up.
- */
- /* mode 0 - fits in 2KB */
- static struct fifo_cfg __initdata mode_0_cfg[] = {
- { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, },
- { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
- { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
- };
- /* mode 1 - fits in 4KB */
- static struct fifo_cfg __initdata mode_1_cfg[] = {
- { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
- { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
- { .hw_ep_num = 2, .style = FIFO_RXTX, .maxpacket = 512, .mode = BUF_DOUBLE, },
- { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
- { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
- };
- /* mode 2 - fits in 4KB */
- static struct fifo_cfg __initdata mode_2_cfg[] = {
- { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
- { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
- };
- /* mode 3 - fits in 4KB */
- static struct fifo_cfg __initdata mode_3_cfg[] = {
- { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, .mode = BUF_DOUBLE, },
- { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, .mode = BUF_DOUBLE, },
- { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 3, .style = FIFO_RXTX, .maxpacket = 256, },
- { .hw_ep_num = 4, .style = FIFO_RXTX, .maxpacket = 256, },
- };
- /* mode 4 - fits in 16KB */
- static struct fifo_cfg __initdata mode_4_cfg[] = {
- { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 8, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 8, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 9, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 9, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 10, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 10, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 11, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 11, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 12, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 12, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 13, .style = FIFO_TX, .maxpacket = 512, },
- { .hw_ep_num = 13, .style = FIFO_RX, .maxpacket = 512, },
- { .hw_ep_num = 14, .style = FIFO_RXTX, .maxpacket = 1024, },
- { .hw_ep_num = 15, .style = FIFO_RXTX, .maxpacket = 1024, },
- };
- /*
- * configure a fifo; for non-shared endpoints, this may be called
- * once for a tx fifo and once for an rx fifo.
- *
- * returns negative errno or offset for next fifo.
- */
- static int __init
- fifo_setup(struct musb *musb, struct musb_hw_ep *hw_ep,
- const struct fifo_cfg *cfg, u16 offset)
- {
- void __iomem *mbase = musb->mregs;
- int size = 0;
- u16 maxpacket = cfg->maxpacket;
- u16 c_off = offset >> 3;
- u8 c_size;
- /* expect hw_ep has already been zero-initialized */
- size = ffs(max(maxpacket, (u16) 8)) - 1;
- maxpacket = 1 << size;
- c_size = size - 3;
- if (cfg->mode == BUF_DOUBLE) {
- if ((offset + (maxpacket << 1)) >
- (1 << (musb->config->ram_bits + 2)))
- return -EMSGSIZE;
- c_size |= MUSB_FIFOSZ_DPB;
- } else {
- if ((offset + maxpacket) > (1 << (musb->config->ram_bits + 2)))
- return -EMSGSIZE;
- }
- /* configure the FIFO */
- musb_writeb(mbase, MUSB_INDEX, hw_ep->epnum);
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- /* EP0 reserved endpoint for control, bidirectional;
- * EP1 reserved for bulk, two unidirection halves.
- */
- if (hw_ep->epnum == 1)
- musb->bulk_ep = hw_ep;
- /* REVISIT error check: be sure ep0 can both rx and tx ... */
- #endif
- switch (cfg->style) {
- case FIFO_TX:
- musb_write_txfifosz(mbase, c_size);
- musb_write_txfifoadd(mbase, c_off);
- hw_ep->tx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
- hw_ep->max_packet_sz_tx = maxpacket;
- break;
- case FIFO_RX:
- musb_write_rxfifosz(mbase, c_size);
- musb_write_rxfifoadd(mbase, c_off);
- hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
- hw_ep->max_packet_sz_rx = maxpacket;
- break;
- case FIFO_RXTX:
- musb_write_txfifosz(mbase, c_size);
- musb_write_txfifoadd(mbase, c_off);
- hw_ep->rx_double_buffered = !!(c_size & MUSB_FIFOSZ_DPB);
- hw_ep->max_packet_sz_rx = maxpacket;
- musb_write_rxfifosz(mbase, c_size);
- musb_write_rxfifoadd(mbase, c_off);
- hw_ep->tx_double_buffered = hw_ep->rx_double_buffered;
- hw_ep->max_packet_sz_tx = maxpacket;
- hw_ep->is_shared_fifo = true;
- break;
- }
- /* NOTE rx and tx endpoint irqs aren't managed separately,
- * which happens to be ok
- */
- musb->epmask |= (1 << hw_ep->epnum);
- return offset + (maxpacket << ((c_size & MUSB_FIFOSZ_DPB) ? 1 : 0));
- }
- static struct fifo_cfg __initdata ep0_cfg = {
- .style = FIFO_RXTX, .maxpacket = 64,
- };
- static int __init ep_config_from_table(struct musb *musb)
- {
- const struct fifo_cfg *cfg;
- unsigned i, n;
- int offset;
- struct musb_hw_ep *hw_ep = musb->endpoints;
- switch (fifo_mode) {
- default:
- fifo_mode = 0;
- /* FALLTHROUGH */
- case 0:
- cfg = mode_0_cfg;
- n = ARRAY_SIZE(mode_0_cfg);
- break;
- case 1:
- cfg = mode_1_cfg;
- n = ARRAY_SIZE(mode_1_cfg);
- break;
- case 2:
- cfg = mode_2_cfg;
- n = ARRAY_SIZE(mode_2_cfg);
- break;
- case 3:
- cfg = mode_3_cfg;
- n = ARRAY_SIZE(mode_3_cfg);
- break;
- case 4:
- cfg = mode_4_cfg;
- n = ARRAY_SIZE(mode_4_cfg);
- break;
- }
- printk(KERN_DEBUG "%s: setup fifo_mode %d\n",
- musb_driver_name, fifo_mode);
- offset = fifo_setup(musb, hw_ep, &ep0_cfg, 0);
- /* assert(offset > 0) */
- /* NOTE: for RTL versions >= 1.400 EPINFO and RAMINFO would
- * be better than static musb->config->num_eps and DYN_FIFO_SIZE...
- */
- for (i = 0; i < n; i++) {
- u8 epn = cfg->hw_ep_num;
- if (epn >= musb->config->num_eps) {
- pr_debug("%s: invalid ep %d\n",
- musb_driver_name, epn);
- return -EINVAL;
- }
- offset = fifo_setup(musb, hw_ep + epn, cfg++, offset);
- if (offset < 0) {
- pr_debug("%s: mem overrun, ep %d\n",
- musb_driver_name, epn);
- return -EINVAL;
- }
- epn++;
- musb->nr_endpoints = max(epn, musb->nr_endpoints);
- }
- printk(KERN_DEBUG "%s: %d/%d max ep, %d/%d memory\n",
- musb_driver_name,
- n + 1, musb->config->num_eps * 2 - 1,
- offset, (1 << (musb->config->ram_bits + 2)));
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- if (!musb->bulk_ep) {
- pr_debug("%s: missing bulk\n", musb_driver_name);
- return -EINVAL;
- }
- #endif
- return 0;
- }
- /*
- * ep_config_from_hw - when MUSB_C_DYNFIFO_DEF is false
- * @param musb the controller
- */
- static int __init ep_config_from_hw(struct musb *musb)
- {
- u8 epnum = 0;
- struct musb_hw_ep *hw_ep;
- void *mbase = musb->mregs;
- int ret = 0;
- DBG(2, "<== static silicon ep config\n");
- /* FIXME pick up ep0 maxpacket size */
- for (epnum = 1; epnum < musb->config->num_eps; epnum++) {
- musb_ep_select(mbase, epnum);
- hw_ep = musb->endpoints + epnum;
- ret = musb_read_fifosize(musb, hw_ep, epnum);
- if (ret < 0)
- break;
- /* FIXME set up hw_ep->{rx,tx}_double_buffered */
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- /* pick an RX/TX endpoint for bulk */
- if (hw_ep->max_packet_sz_tx < 512
- || hw_ep->max_packet_sz_rx < 512)
- continue;
- /* REVISIT: this algorithm is lazy, we should at least
- * try to pick a double buffered endpoint.
- */
- if (musb->bulk_ep)
- continue;
- musb->bulk_ep = hw_ep;
- #endif
- }
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- if (!musb->bulk_ep) {
- pr_debug("%s: missing bulk\n", musb_driver_name);
- return -EINVAL;
- }
- #endif
- return 0;
- }
- enum { MUSB_CONTROLLER_MHDRC, MUSB_CONTROLLER_HDRC, };
- /* Initialize MUSB (M)HDRC part of the USB hardware subsystem;
- * configure endpoints, or take their config from silicon
- */
- static int __init musb_core_init(u16 musb_type, struct musb *musb)
- {
- #ifdef MUSB_AHB_ID
- u32 data;
- #endif
- u8 reg;
- char *type;
- u16 hwvers, rev_major, rev_minor;
- char aInfo[78], aRevision[32], aDate[12];
- void __iomem *mbase = musb->mregs;
- int status = 0;
- int i;
- /* log core options (read using indexed model) */
- musb_ep_select(mbase, 0);
- reg = musb_read_configdata(mbase);
- strcpy(aInfo, (reg & MUSB_CONFIGDATA_UTMIDW) ? "UTMI-16" : "UTMI-8");
- if (reg & MUSB_CONFIGDATA_DYNFIFO)
- strcat(aInfo, ", dyn FIFOs");
- if (reg & MUSB_CONFIGDATA_MPRXE) {
- strcat(aInfo, ", bulk combine");
- #ifdef C_MP_RX
- musb->bulk_combine = true;
- #else
- strcat(aInfo, " (X)"); /* no driver support */
- #endif
- }
- if (reg & MUSB_CONFIGDATA_MPTXE) {
- strcat(aInfo, ", bulk split");
- #ifdef C_MP_TX
- musb->bulk_split = true;
- #else
- strcat(aInfo, " (X)"); /* no driver support */
- #endif
- }
- if (reg & MUSB_CONFIGDATA_HBRXE) {
- strcat(aInfo, ", HB-ISO Rx");
- strcat(aInfo, " (X)"); /* no driver support */
- }
- if (reg & MUSB_CONFIGDATA_HBTXE) {
- strcat(aInfo, ", HB-ISO Tx");
- strcat(aInfo, " (X)"); /* no driver support */
- }
- if (reg & MUSB_CONFIGDATA_SOFTCONE)
- strcat(aInfo, ", SoftConn");
- printk(KERN_DEBUG "%s: ConfigData=0x%02x (%s)\n",
- musb_driver_name, reg, aInfo);
- #ifdef MUSB_AHB_ID
- data = musb_readl(mbase, 0x404);
- sprintf(aDate, "%04d-%02x-%02x", (data & 0xffff),
- (data >> 16) & 0xff, (data >> 24) & 0xff);
- /* FIXME ID2 and ID3 are unused */
- data = musb_readl(mbase, 0x408);
- printk(KERN_DEBUG "ID2=%lx\n", (long unsigned)data);
- data = musb_readl(mbase, 0x40c);
- printk(KERN_DEBUG "ID3=%lx\n", (long unsigned)data);
- reg = musb_readb(mbase, 0x400);
- musb_type = ('M' == reg) ? MUSB_CONTROLLER_MHDRC : MUSB_CONTROLLER_HDRC;
- #else
- aDate[0] = 0;
- #endif
- if (MUSB_CONTROLLER_MHDRC == musb_type) {
- musb->is_multipoint = 1;
- type = "M";
- } else {
- musb->is_multipoint = 0;
- type = "";
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- #ifndef CONFIG_USB_OTG_BLACKLIST_HUB
- printk(KERN_ERR
- "%s: kernel must blacklist external hubs\n",
- musb_driver_name);
- #endif
- #endif
- }
- /* log release info */
- hwvers = musb_read_hwvers(mbase);
- rev_major = (hwvers >> 10) & 0x1f;
- rev_minor = hwvers & 0x3ff;
- snprintf(aRevision, 32, "%d.%d%s", rev_major,
- rev_minor, (hwvers & 0x8000) ? "RC" : "");
- printk(KERN_DEBUG "%s: %sHDRC RTL version %s %s\n",
- musb_driver_name, type, aRevision, aDate);
- /* configure ep0 */
- musb_configure_ep0(musb);
- /* discover endpoint configuration */
- musb->nr_endpoints = 1;
- musb->epmask = 1;
- if (reg & MUSB_CONFIGDATA_DYNFIFO) {
- if (musb->config->dyn_fifo)
- status = ep_config_from_table(musb);
- else {
- ERR("reconfigure software for Dynamic FIFOs\n");
- status = -ENODEV;
- }
- } else {
- if (!musb->config->dyn_fifo)
- status = ep_config_from_hw(musb);
- else {
- ERR("reconfigure software for static FIFOs\n");
- return -ENODEV;
- }
- }
- if (status < 0)
- return status;
- /* finish init, and print endpoint config */
- for (i = 0; i < musb->nr_endpoints; i++) {
- struct musb_hw_ep *hw_ep = musb->endpoints + i;
- hw_ep->fifo = MUSB_FIFO_OFFSET(i) + mbase;
- #ifdef CONFIG_USB_TUSB6010
- hw_ep->fifo_async = musb->async + 0x400 + MUSB_FIFO_OFFSET(i);
- hw_ep->fifo_sync = musb->sync + 0x400 + MUSB_FIFO_OFFSET(i);
- hw_ep->fifo_sync_va =
- musb->sync_va + 0x400 + MUSB_FIFO_OFFSET(i);
- if (i == 0)
- hw_ep->conf = mbase - 0x400 + TUSB_EP0_CONF;
- else
- hw_ep->conf = mbase + 0x400 + (((i - 1) & 0xf) << 2);
- #endif
- hw_ep->regs = MUSB_EP_OFFSET(i, 0) + mbase;
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- hw_ep->target_regs = musb_read_target_reg_base(i, mbase);
- hw_ep->rx_reinit = 1;
- hw_ep->tx_reinit = 1;
- #endif
- if (hw_ep->max_packet_sz_tx) {
- printk(KERN_DEBUG
- "%s: hw_ep %d%s, %smax %d\n",
- musb_driver_name, i,
- hw_ep->is_shared_fifo ? "shared" : "tx",
- hw_ep->tx_double_buffered
- ? "doublebuffer, " : "",
- hw_ep->max_packet_sz_tx);
- }
- if (hw_ep->max_packet_sz_rx && !hw_ep->is_shared_fifo) {
- printk(KERN_DEBUG
- "%s: hw_ep %d%s, %smax %d\n",
- musb_driver_name, i,
- "rx",
- hw_ep->rx_double_buffered
- ? "doublebuffer, " : "",
- hw_ep->max_packet_sz_rx);
- }
- if (!(hw_ep->max_packet_sz_tx || hw_ep->max_packet_sz_rx))
- DBG(1, "hw_ep %d not configured\n", i);
- }
- return 0;
- }
- /*-------------------------------------------------------------------------*/
- #if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3430)
- static irqreturn_t generic_interrupt(int irq, void *__hci)
- {
- unsigned long flags;
- irqreturn_t retval = IRQ_NONE;
- struct musb *musb = __hci;
- spin_lock_irqsave(&musb->lock, flags);
- musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
- musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
- musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
- if (musb->int_usb || musb->int_tx || musb->int_rx)
- retval = musb_interrupt(musb);
- spin_unlock_irqrestore(&musb->lock, flags);
- /* REVISIT we sometimes get spurious IRQs on g_ep0
- * not clear why...
- */
- if (retval != IRQ_HANDLED)
- DBG(5, "spurious?\n");
- return IRQ_HANDLED;
- }
- #else
- #define generic_interrupt NULL
- #endif
- /*
- * handle all the irqs defined by the HDRC core. for now we expect: other
- * irq sources (phy, dma, etc) will be handled first, musb->int_* values
- * will be assigned, and the irq will already have been acked.
- *
- * called in irq context with spinlock held, irqs blocked
- */
- irqreturn_t musb_interrupt(struct musb *musb)
- {
- irqreturn_t retval = IRQ_NONE;
- u8 devctl, power;
- int ep_num;
- u32 reg;
- devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
- power = musb_readb(musb->mregs, MUSB_POWER);
- DBG(4, "** IRQ %s usb%04x tx%04x rx%04x\n",
- (devctl & MUSB_DEVCTL_HM) ? "host" : "peripheral",
- musb->int_usb, musb->int_tx, musb->int_rx);
- /* the core can interrupt us for multiple reasons; docs have
- * a generic interrupt flowchart to follow
- */
- if (musb->int_usb & STAGE0_MASK)
- retval |= musb_stage0_irq(musb, musb->int_usb,
- devctl, power);
- /* "stage 1" is handling endpoint irqs */
- /* handle endpoint 0 first */
- if (musb->int_tx & 1) {
- if (devctl & MUSB_DEVCTL_HM)
- retval |= musb_h_ep0_irq(musb);
- else
- retval |= musb_g_ep0_irq(musb);
- }
- /* RX on endpoints 1-15 */
- reg = musb->int_rx >> 1;
- ep_num = 1;
- while (reg) {
- if (reg & 1) {
- /* musb_ep_select(musb->mregs, ep_num); */
- /* REVISIT just retval = ep->rx_irq(...) */
- retval = IRQ_HANDLED;
- if (devctl & MUSB_DEVCTL_HM) {
- if (is_host_capable())
- musb_host_rx(musb, ep_num);
- } else {
- if (is_peripheral_capable())
- musb_g_rx(musb, ep_num);
- }
- }
- reg >>= 1;
- ep_num++;
- }
- /* TX on endpoints 1-15 */
- reg = musb->int_tx >> 1;
- ep_num = 1;
- while (reg) {
- if (reg & 1) {
- /* musb_ep_select(musb->mregs, ep_num); */
- /* REVISIT just retval |= ep->tx_irq(...) */
- retval = IRQ_HANDLED;
- if (devctl & MUSB_DEVCTL_HM) {
- if (is_host_capable())
- musb_host_tx(musb, ep_num);
- } else {
- if (is_peripheral_capable())
- musb_g_tx(musb, ep_num);
- }
- }
- reg >>= 1;
- ep_num++;
- }
- /* finish handling "global" interrupts after handling fifos */
- if (musb->int_usb)
- retval |= musb_stage2_irq(musb,
- musb->int_usb, devctl, power);
- return retval;
- }
- #ifndef CONFIG_MUSB_PIO_ONLY
- static int __initdata use_dma = 1;
- /* "modprobe ... use_dma=0" etc */
- module_param(use_dma, bool, 0);
- MODULE_PARM_DESC(use_dma, "enable/disable use of DMA");
- void musb_dma_completion(struct musb *musb, u8 epnum, u8 transmit)
- {
- u8 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
- /* called with controller lock already held */
- if (!epnum) {
- #ifndef CONFIG_USB_TUSB_OMAP_DMA
- if (!is_cppi_enabled()) {
- /* endpoint 0 */
- if (devctl & MUSB_DEVCTL_HM)
- musb_h_ep0_irq(musb);
- else
- musb_g_ep0_irq(musb);
- }
- #endif
- } else {
- /* endpoints 1..15 */
- if (transmit) {
- if (devctl & MUSB_DEVCTL_HM) {
- if (is_host_capable())
- musb_host_tx(musb, epnum);
- } else {
- if (is_peripheral_capable())
- musb_g_tx(musb, epnum);
- }
- } else {
- /* receive */
- if (devctl & MUSB_DEVCTL_HM) {
- if (is_host_capable())
- musb_host_rx(musb, epnum);
- } else {
- if (is_peripheral_capable())
- musb_g_rx(musb, epnum);
- }
- }
- }
- }
- #else
- #define use_dma 0
- #endif
- /*-------------------------------------------------------------------------*/
- #ifdef CONFIG_SYSFS
- static ssize_t
- musb_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct musb *musb = dev_to_musb(dev);
- unsigned long flags;
- int ret = -EINVAL;
- spin_lock_irqsave(&musb->lock, flags);
- ret = sprintf(buf, "%s\n", otg_state_string(musb));
- spin_unlock_irqrestore(&musb->lock, flags);
- return ret;
- }
- static ssize_t
- musb_mode_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t n)
- {
- struct musb *musb = dev_to_musb(dev);
- unsigned long flags;
- int status;
- spin_lock_irqsave(&musb->lock, flags);
- if (sysfs_streq(buf, "host"))
- status = musb_platform_set_mode(musb, MUSB_HOST);
- else if (sysfs_streq(buf, "peripheral"))
- status = musb_platform_set_mode(musb, MUSB_PERIPHERAL);
- else if (sysfs_streq(buf, "otg"))
- status = musb_platform_set_mode(musb, MUSB_OTG);
- else
- status = -EINVAL;
- spin_unlock_irqrestore(&musb->lock, flags);
- return (status == 0) ? n : status;
- }
- static DEVICE_ATTR(mode, 0644, musb_mode_show, musb_mode_store);
- static ssize_t
- musb_vbus_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t n)
- {
- struct musb *musb = dev_to_musb(dev);
- unsigned long flags;
- unsigned long val;
- if (sscanf(buf, "%lu", &val) < 1) {
- printk(KERN_ERR "Invalid VBUS timeout ms value\n");
- return -EINVAL;
- }
- spin_lock_irqsave(&musb->lock, flags);
- musb->a_wait_bcon = val;
- if (musb->xceiv.state == OTG_STATE_A_WAIT_BCON)
- musb->is_active = 0;
- musb_platform_try_idle(musb, jiffies + msecs_to_jiffies(val));
- spin_unlock_irqrestore(&musb->lock, flags);
- return n;
- }
- static ssize_t
- musb_vbus_show(struct device *dev, struct device_attribute *attr, char *buf)
- {
- struct musb *musb = dev_to_musb(dev);
- unsigned long flags;
- unsigned long val;
- int vbus;
- spin_lock_irqsave(&musb->lock, flags);
- val = musb->a_wait_bcon;
- vbus = musb_platform_get_vbus_status(musb);
- spin_unlock_irqrestore(&musb->lock, flags);
- return sprintf(buf, "Vbus %s, timeout %lu\n",
- vbus ? "on" : "off", val);
- }
- static DEVICE_ATTR(vbus, 0644, musb_vbus_show, musb_vbus_store);
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- /* Gadget drivers can't know that a host is connected so they might want
- * to start SRP, but users can. This allows userspace to trigger SRP.
- */
- static ssize_t
- musb_srp_store(struct device *dev, struct device_attribute *attr,
- const char *buf, size_t n)
- {
- struct musb *musb = dev_to_musb(dev);
- unsigned short srp;
- if (sscanf(buf, "%hu", &srp) != 1
- || (srp != 1)) {
- printk(KERN_ERR "SRP: Value must be 1\n");
- return -EINVAL;
- }
- if (srp == 1)
- musb_g_wakeup(musb);
- return n;
- }
- static DEVICE_ATTR(srp, 0644, NULL, musb_srp_store);
- #endif /* CONFIG_USB_GADGET_MUSB_HDRC */
- #endif /* sysfs */
- /* Only used to provide driver mode change events */
- static void musb_irq_work(struct work_struct *data)
- {
- struct musb *musb = container_of(data, struct musb, irq_work);
- static int old_state;
- if (musb->xceiv.state != old_state) {
- old_state = musb->xceiv.state;
- sysfs_notify(&musb->controller->kobj, NULL, "mode");
- }
- }
- /* --------------------------------------------------------------------------
- * Init support
- */
- static struct musb *__init
- allocate_instance(struct device *dev,
- struct musb_hdrc_config *config, void __iomem *mbase)
- {
- struct musb *musb;
- struct musb_hw_ep *ep;
- int epnum;
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- struct usb_hcd *hcd;
- hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
- if (!hcd)
- return NULL;
- /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
- musb = hcd_to_musb(hcd);
- INIT_LIST_HEAD(&musb->control);
- INIT_LIST_HEAD(&musb->in_bulk);
- INIT_LIST_HEAD(&musb->out_bulk);
- hcd->uses_new_polling = 1;
- musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
- #else
- musb = kzalloc(sizeof *musb, GFP_KERNEL);
- if (!musb)
- return NULL;
- dev_set_drvdata(dev, musb);
- #endif
- musb->mregs = mbase;
- musb->ctrl_base = mbase;
- musb->nIrq = -ENODEV;
- musb->config = config;
- BUG_ON(musb->config->num_eps > MUSB_C_NUM_EPS);
- for (epnum = 0, ep = musb->endpoints;
- epnum < musb->config->num_eps;
- epnum++, ep++) {
- ep->musb = musb;
- ep->epnum = epnum;
- }
- musb->controller = dev;
- return musb;
- }
- static void musb_free(struct musb *musb)
- {
- /* this has multiple entry modes. it handles fault cleanup after
- * probe(), where things may be partially set up, as well as rmmod
- * cleanup after everything's been de-activated.
- */
- #ifdef CONFIG_SYSFS
- device_remove_file(musb->controller, &dev_attr_mode);
- device_remove_file(musb->controller, &dev_attr_vbus);
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- device_remove_file(musb->controller, &dev_attr_srp);
- #endif
- #endif
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- musb_gadget_cleanup(musb);
- #endif
- if (musb->nIrq >= 0) {
- if (musb->irq_wake)
- disable_irq_wake(musb->nIrq);
- free_irq(musb->nIrq, musb);
- }
- if (is_dma_capable() && musb->dma_controller) {
- struct dma_controller *c = musb->dma_controller;
- (void) c->stop(c);
- dma_controller_destroy(c);
- }
- musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
- musb_platform_exit(musb);
- musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
- if (musb->clock) {
- clk_disable(musb->clock);
- clk_put(musb->clock);
- }
- #ifdef CONFIG_USB_MUSB_OTG
- put_device(musb->xceiv.dev);
- #endif
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- usb_put_hcd(musb_to_hcd(musb));
- #else
- kfree(musb);
- #endif
- }
- /*
- * Perform generic per-controller initialization.
- *
- * @pDevice: the controller (already clocked, etc)
- * @nIrq: irq
- * @mregs: virtual address of controller registers,
- * not yet corrected for platform-specific offsets
- */
- static int __init
- musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
- {
- int status;
- struct musb *musb;
- struct musb_hdrc_platform_data *plat = dev->platform_data;
- /* The driver might handle more features than the board; OK.
- * Fail when the board needs a feature that's not enabled.
- */
- if (!plat) {
- dev_dbg(dev, "no platform_data?\n");
- return -ENODEV;
- }
- switch (plat->mode) {
- case MUSB_HOST:
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- break;
- #else
- goto bad_config;
- #endif
- case MUSB_PERIPHERAL:
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- break;
- #else
- goto bad_config;
- #endif
- case MUSB_OTG:
- #ifdef CONFIG_USB_MUSB_OTG
- break;
- #else
- bad_config:
- #endif
- default:
- dev_err(dev, "incompatible Kconfig role setting\n");
- return -EINVAL;
- }
- /* allocate */
- musb = allocate_instance(dev, plat->config, ctrl);
- if (!musb)
- return -ENOMEM;
- spin_lock_init(&musb->lock);
- musb->board_mode = plat->mode;
- musb->board_set_power = plat->set_power;
- musb->set_clock = plat->set_clock;
- musb->min_power = plat->min_power;
- /* Clock usage is chip-specific ... functional clock (DaVinci,
- * OMAP2430), or PHY ref (some TUSB6010 boards). All this core
- * code does is make sure a clock handle is available; platform
- * code manages it during start/stop and suspend/resume.
- */
- if (plat->clock) {
- musb->clock = clk_get(dev, plat->clock);
- if (IS_ERR(musb->clock)) {
- status = PTR_ERR(musb->clock);
- musb->clock = NULL;
- goto fail;
- }
- }
- /* assume vbus is off */
- /* platform adjusts musb->mregs and musb->isr if needed,
- * and activates clocks
- */
- musb->isr = generic_interrupt;
- status = musb_platform_init(musb);
- if (status < 0)
- goto fail;
- if (!musb->isr) {
- status = -ENODEV;
- goto fail2;
- }
- #ifndef CONFIG_MUSB_PIO_ONLY
- if (use_dma && dev->dma_mask) {
- struct dma_controller *c;
- c = dma_controller_create(musb, musb->mregs);
- musb->dma_controller = c;
- if (c)
- (void) c->start(c);
- }
- #endif
- /* ideally this would be abstracted in platform setup */
- if (!is_dma_capable() || !musb->dma_controller)
- dev->dma_mask = NULL;
- /* be sure interrupts are disabled before connecting ISR */
- musb_platform_disable(musb);
- musb_generic_disable(musb);
- /* setup musb parts of the core (especially endpoints) */
- status = musb_core_init(plat->config->multipoint
- ? MUSB_CONTROLLER_MHDRC
- : MUSB_CONTROLLER_HDRC, musb);
- if (status < 0)
- goto fail2;
- /* Init IRQ workqueue before request_irq */
- INIT_WORK(&musb->irq_work, musb_irq_work);
- /* attach to the IRQ */
- if (request_irq(nIrq, musb->isr, 0, dev_name(dev), musb)) {
- dev_err(dev, "request_irq %d failed!\n", nIrq);
- status = -ENODEV;
- goto fail2;
- }
- musb->nIrq = nIrq;
- /* FIXME this handles wakeup irqs wrong */
- if (enable_irq_wake(nIrq) == 0) {
- musb->irq_wake = 1;
- device_init_wakeup(dev, 1);
- } else {
- musb->irq_wake = 0;
- }
- pr_info("%s: USB %s mode controller at %p using %s, IRQ %d\n",
- musb_driver_name,
- ({char *s;
- switch (musb->board_mode) {
- case MUSB_HOST: s = "Host"; break;
- case MUSB_PERIPHERAL: s = "Peripheral"; break;
- default: s = "OTG"; break;
- }; s; }),
- ctrl,
- (is_dma_capable() && musb->dma_controller)
- ? "DMA" : "PIO",
- musb->nIrq);
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- /* host side needs more setup, except for no-host modes */
- if (musb->board_mode != MUSB_PERIPHERAL) {
- struct usb_hcd *hcd = musb_to_hcd(musb);
- if (musb->board_mode == MUSB_OTG)
- hcd->self.otg_port = 1;
- musb->xceiv.host = &hcd->self;
- hcd->power_budget = 2 * (plat->power ? : 250);
- }
- #endif /* CONFIG_USB_MUSB_HDRC_HCD */
- /* For the host-only role, we can activate right away.
- * (We expect the ID pin to be forcibly grounded!!)
- * Otherwise, wait till the gadget driver hooks up.
- */
- if (!is_otg_enabled(musb) && is_host_enabled(musb)) {
- MUSB_HST_MODE(musb);
- musb->xceiv.default_a = 1;
- musb->xceiv.state = OTG_STATE_A_IDLE;
- status = usb_add_hcd(musb_to_hcd(musb), -1, 0);
- if (status)
- goto fail;
- DBG(1, "%s mode, status %d, devctl %02x %c\n",
- "HOST", status,
- musb_readb(musb->mregs, MUSB_DEVCTL),
- (musb_readb(musb->mregs, MUSB_DEVCTL)
- & MUSB_DEVCTL_BDEVICE
- ? 'B' : 'A'));
- } else /* peripheral is enabled */ {
- MUSB_DEV_MODE(musb);
- musb->xceiv.default_a = 0;
- musb->xceiv.state = OTG_STATE_B_IDLE;
- status = musb_gadget_setup(musb);
- if (status)
- goto fail;
- DBG(1, "%s mode, status %d, dev%02x\n",
- is_otg_enabled(musb) ? "OTG" : "PERIPHERAL",
- status,
- musb_readb(musb->mregs, MUSB_DEVCTL));
- }
- #ifdef CONFIG_SYSFS
- status = device_create_file(dev, &dev_attr_mode);
- status = device_create_file(dev, &dev_attr_vbus);
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- status = device_create_file(dev, &dev_attr_srp);
- #endif /* CONFIG_USB_GADGET_MUSB_HDRC */
- status = 0;
- #endif
- if (status)
- goto fail2;
- return 0;
- fail2:
- #ifdef CONFIG_SYSFS
- device_remove_file(musb->controller, &dev_attr_mode);
- device_remove_file(musb->controller, &dev_attr_vbus);
- #ifdef CONFIG_USB_GADGET_MUSB_HDRC
- device_remove_file(musb->controller, &dev_attr_srp);
- #endif
- #endif
- musb_platform_exit(musb);
- fail:
- dev_err(musb->controller,
- "musb_init_controller failed with status %d\n", status);
- if (musb->clock)
- clk_put(musb->clock);
- device_init_wakeup(dev, 0);
- musb_free(musb);
- return status;
- }
- /*-------------------------------------------------------------------------*/
- /* all implementations (PCI bridge to FPGA, VLYNQ, etc) should just
- * bridge to a platform device; this driver then suffices.
- */
- #ifndef CONFIG_MUSB_PIO_ONLY
- static u64 *orig_dma_mask;
- #endif
- static int __init musb_probe(struct platform_device *pdev)
- {
- struct device *dev = &pdev->dev;
- int irq = platform_get_irq(pdev, 0);
- struct resource *iomem;
- void __iomem *base;
- iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
- if (!iomem || irq == 0)
- return -ENODEV;
- base = ioremap(iomem->start, iomem->end - iomem->start + 1);
- if (!base) {
- dev_err(dev, "ioremap failed\n");
- return -ENOMEM;
- }
- #ifndef CONFIG_MUSB_PIO_ONLY
- /* clobbered by use_dma=n */
- orig_dma_mask = dev->dma_mask;
- #endif
- return musb_init_controller(dev, irq, base);
- }
- static int __devexit musb_remove(struct platform_device *pdev)
- {
- struct musb *musb = dev_to_musb(&pdev->dev);
- void __iomem *ctrl_base = musb->ctrl_base;
- /* this gets called on rmmod.
- * - Host mode: host may still be active
- * - Peripheral mode: peripheral is deactivated (or never-activated)
- * - OTG mode: both roles are deactivated (or never-activated)
- */
- musb_shutdown(pdev);
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- if (musb->board_mode == MUSB_HOST)
- usb_remove_hcd(musb_to_hcd(musb));
- #endif
- musb_free(musb);
- iounmap(ctrl_base);
- device_init_wakeup(&pdev->dev, 0);
- #ifndef CONFIG_MUSB_PIO_ONLY
- pdev->dev.dma_mask = orig_dma_mask;
- #endif
- return 0;
- }
- #ifdef CONFIG_PM
- static int musb_suspend(struct platform_device *pdev, pm_message_t message)
- {
- unsigned long flags;
- struct musb *musb = dev_to_musb(&pdev->dev);
- if (!musb->clock)
- return 0;
- spin_lock_irqsave(&musb->lock, flags);
- if (is_peripheral_active(musb)) {
- /* FIXME force disconnect unless we know USB will wake
- * the system up quickly enough to respond ...
- */
- } else if (is_host_active(musb)) {
- /* we know all the children are suspended; sometimes
- * they will even be wakeup-enabled.
- */
- }
- if (musb->set_clock)
- musb->set_clock(musb->clock, 0);
- else
- clk_disable(musb->clock);
- spin_unlock_irqrestore(&musb->lock, flags);
- return 0;
- }
- static int musb_resume_early(struct platform_device *pdev)
- {
- struct musb *musb = dev_to_musb(&pdev->dev);
- if (!musb->clock)
- return 0;
- if (musb->set_clock)
- musb->set_clock(musb->clock, 1);
- else
- clk_enable(musb->clock);
- /* for static cmos like DaVinci, register values were preserved
- * unless for some reason the whole soc powered down or the USB
- * module got reset through the PSC (vs just being disabled).
- */
- return 0;
- }
- #else
- #define musb_suspend NULL
- #define musb_resume_early NULL
- #endif
- static struct platform_driver musb_driver = {
- .driver = {
- .name = (char *)musb_driver_name,
- .bus = &platform_bus_type,
- .owner = THIS_MODULE,
- },
- .remove = __devexit_p(musb_remove),
- .shutdown = musb_shutdown,
- .suspend = musb_suspend,
- .resume_early = musb_resume_early,
- };
- /*-------------------------------------------------------------------------*/
- static int __init musb_init(void)
- {
- #ifdef CONFIG_USB_MUSB_HDRC_HCD
- if (usb_disabled())
- return 0;
- #endif
- pr_info("%s: version " MUSB_VERSION ", "
- #ifdef CONFIG_MUSB_PIO_ONLY
- "pio"
- #elif defined(CONFIG_USB_TI_CPPI_DMA)
- "cppi-dma"
- #elif defined(CONFIG_USB_INVENTRA_DMA)
- "musb-dma"
- #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
- "tusb-omap-dma"
- #else
- "?dma?"
- #endif
- ", "
- #ifdef CONFIG_USB_MUSB_OTG
- "otg (peripheral+host)"
- #elif defined(CONFIG_USB_GADGET_MUSB_HDRC)
- "peripheral"
- #elif defined(CONFIG_USB_MUSB_HDRC_HCD)
- "host"
- #endif
- ", debug=%d\n",
- musb_driver_name, musb_debug);
- return platform_driver_probe(&musb_driver, musb_probe);
- }
- /* make us init after usbcore and i2c (transceivers, regulators, etc)
- * and before usb gadget and host-side drivers start to register
- */
- fs_initcall(musb_init);
- static void __exit musb_cleanup(void)
- {
- platform_driver_unregister(&musb_driver);
- }
- module_exit(musb_cleanup);
|