r8a66597-hcd.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R8A66597 HCD (Host Controller Driver)
  4. *
  5. * Copyright (C) 2006-2007 Renesas Solutions Corp.
  6. * Portions Copyright (C) 2004 Psion Teklogix (for NetBook PRO)
  7. * Portions Copyright (C) 2004-2005 David Brownell
  8. * Portions Copyright (C) 1999 Roman Weissgaerber
  9. *
  10. * Author : Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/errno.h>
  16. #include <linux/timer.h>
  17. #include <linux/delay.h>
  18. #include <linux/list.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/io.h>
  24. #include <linux/mm.h>
  25. #include <linux/irq.h>
  26. #include <linux/slab.h>
  27. #include <asm/cacheflush.h>
  28. #include "r8a66597.h"
  29. MODULE_DESCRIPTION("R8A66597 USB Host Controller Driver");
  30. MODULE_LICENSE("GPL");
  31. MODULE_AUTHOR("Yoshihiro Shimoda");
  32. MODULE_ALIAS("platform:r8a66597_hcd");
  33. #define DRIVER_VERSION "2009-05-26"
  34. static const char hcd_name[] = "r8a66597_hcd";
  35. static void packet_write(struct r8a66597 *r8a66597, u16 pipenum);
  36. static int r8a66597_get_frame(struct usb_hcd *hcd);
  37. /* this function must be called with interrupt disabled */
  38. static void enable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  39. unsigned long reg)
  40. {
  41. u16 tmp;
  42. tmp = r8a66597_read(r8a66597, INTENB0);
  43. r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  44. r8a66597_bset(r8a66597, 1 << pipenum, reg);
  45. r8a66597_write(r8a66597, tmp, INTENB0);
  46. }
  47. /* this function must be called with interrupt disabled */
  48. static void disable_pipe_irq(struct r8a66597 *r8a66597, u16 pipenum,
  49. unsigned long reg)
  50. {
  51. u16 tmp;
  52. tmp = r8a66597_read(r8a66597, INTENB0);
  53. r8a66597_bclr(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  54. r8a66597_bclr(r8a66597, 1 << pipenum, reg);
  55. r8a66597_write(r8a66597, tmp, INTENB0);
  56. }
  57. static void set_devadd_reg(struct r8a66597 *r8a66597, u8 r8a66597_address,
  58. u16 usbspd, u8 upphub, u8 hubport, int port)
  59. {
  60. u16 val;
  61. unsigned long devadd_reg = get_devadd_addr(r8a66597_address);
  62. val = (upphub << 11) | (hubport << 8) | (usbspd << 6) | (port & 0x0001);
  63. r8a66597_write(r8a66597, val, devadd_reg);
  64. }
  65. static int r8a66597_clock_enable(struct r8a66597 *r8a66597)
  66. {
  67. u16 tmp;
  68. int i = 0;
  69. if (r8a66597->pdata->on_chip) {
  70. clk_prepare_enable(r8a66597->clk);
  71. do {
  72. r8a66597_write(r8a66597, SCKE, SYSCFG0);
  73. tmp = r8a66597_read(r8a66597, SYSCFG0);
  74. if (i++ > 1000) {
  75. printk(KERN_ERR "r8a66597: reg access fail.\n");
  76. return -ENXIO;
  77. }
  78. } while ((tmp & SCKE) != SCKE);
  79. r8a66597_write(r8a66597, 0x04, 0x02);
  80. } else {
  81. do {
  82. r8a66597_write(r8a66597, USBE, SYSCFG0);
  83. tmp = r8a66597_read(r8a66597, SYSCFG0);
  84. if (i++ > 1000) {
  85. printk(KERN_ERR "r8a66597: reg access fail.\n");
  86. return -ENXIO;
  87. }
  88. } while ((tmp & USBE) != USBE);
  89. r8a66597_bclr(r8a66597, USBE, SYSCFG0);
  90. r8a66597_mdfy(r8a66597, get_xtal_from_pdata(r8a66597->pdata),
  91. XTAL, SYSCFG0);
  92. i = 0;
  93. r8a66597_bset(r8a66597, XCKE, SYSCFG0);
  94. do {
  95. msleep(1);
  96. tmp = r8a66597_read(r8a66597, SYSCFG0);
  97. if (i++ > 500) {
  98. printk(KERN_ERR "r8a66597: reg access fail.\n");
  99. return -ENXIO;
  100. }
  101. } while ((tmp & SCKE) != SCKE);
  102. }
  103. return 0;
  104. }
  105. static void r8a66597_clock_disable(struct r8a66597 *r8a66597)
  106. {
  107. r8a66597_bclr(r8a66597, SCKE, SYSCFG0);
  108. udelay(1);
  109. if (r8a66597->pdata->on_chip) {
  110. clk_disable_unprepare(r8a66597->clk);
  111. } else {
  112. r8a66597_bclr(r8a66597, PLLC, SYSCFG0);
  113. r8a66597_bclr(r8a66597, XCKE, SYSCFG0);
  114. r8a66597_bclr(r8a66597, USBE, SYSCFG0);
  115. }
  116. }
  117. static void r8a66597_enable_port(struct r8a66597 *r8a66597, int port)
  118. {
  119. u16 val;
  120. val = port ? DRPD : DCFM | DRPD;
  121. r8a66597_bset(r8a66597, val, get_syscfg_reg(port));
  122. r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
  123. r8a66597_write(r8a66597, BURST | CPU_ADR_RD_WR, get_dmacfg_reg(port));
  124. r8a66597_bclr(r8a66597, DTCHE, get_intenb_reg(port));
  125. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  126. }
  127. static void r8a66597_disable_port(struct r8a66597 *r8a66597, int port)
  128. {
  129. u16 val, tmp;
  130. r8a66597_write(r8a66597, 0, get_intenb_reg(port));
  131. r8a66597_write(r8a66597, 0, get_intsts_reg(port));
  132. r8a66597_port_power(r8a66597, port, 0);
  133. do {
  134. tmp = r8a66597_read(r8a66597, SOFCFG) & EDGESTS;
  135. udelay(640);
  136. } while (tmp == EDGESTS);
  137. val = port ? DRPD : DCFM | DRPD;
  138. r8a66597_bclr(r8a66597, val, get_syscfg_reg(port));
  139. r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
  140. }
  141. static int enable_controller(struct r8a66597 *r8a66597)
  142. {
  143. int ret, port;
  144. u16 vif = r8a66597->pdata->vif ? LDRV : 0;
  145. u16 irq_sense = r8a66597->irq_sense_low ? INTL : 0;
  146. u16 endian = r8a66597->pdata->endian ? BIGEND : 0;
  147. ret = r8a66597_clock_enable(r8a66597);
  148. if (ret < 0)
  149. return ret;
  150. r8a66597_bset(r8a66597, vif & LDRV, PINCFG);
  151. r8a66597_bset(r8a66597, USBE, SYSCFG0);
  152. r8a66597_bset(r8a66597, BEMPE | NRDYE | BRDYE, INTENB0);
  153. r8a66597_bset(r8a66597, irq_sense & INTL, SOFCFG);
  154. r8a66597_bset(r8a66597, BRDY0, BRDYENB);
  155. r8a66597_bset(r8a66597, BEMP0, BEMPENB);
  156. r8a66597_bset(r8a66597, endian & BIGEND, CFIFOSEL);
  157. r8a66597_bset(r8a66597, endian & BIGEND, D0FIFOSEL);
  158. r8a66597_bset(r8a66597, endian & BIGEND, D1FIFOSEL);
  159. r8a66597_bset(r8a66597, TRNENSEL, SOFCFG);
  160. r8a66597_bset(r8a66597, SIGNE | SACKE, INTENB1);
  161. for (port = 0; port < r8a66597->max_root_hub; port++)
  162. r8a66597_enable_port(r8a66597, port);
  163. return 0;
  164. }
  165. static void disable_controller(struct r8a66597 *r8a66597)
  166. {
  167. int port;
  168. /* disable interrupts */
  169. r8a66597_write(r8a66597, 0, INTENB0);
  170. r8a66597_write(r8a66597, 0, INTENB1);
  171. r8a66597_write(r8a66597, 0, BRDYENB);
  172. r8a66597_write(r8a66597, 0, BEMPENB);
  173. r8a66597_write(r8a66597, 0, NRDYENB);
  174. /* clear status */
  175. r8a66597_write(r8a66597, 0, BRDYSTS);
  176. r8a66597_write(r8a66597, 0, NRDYSTS);
  177. r8a66597_write(r8a66597, 0, BEMPSTS);
  178. for (port = 0; port < r8a66597->max_root_hub; port++)
  179. r8a66597_disable_port(r8a66597, port);
  180. r8a66597_clock_disable(r8a66597);
  181. }
  182. static int get_parent_r8a66597_address(struct r8a66597 *r8a66597,
  183. struct usb_device *udev)
  184. {
  185. struct r8a66597_device *dev;
  186. if (udev->parent && udev->parent->devnum != 1)
  187. udev = udev->parent;
  188. dev = dev_get_drvdata(&udev->dev);
  189. if (dev)
  190. return dev->address;
  191. else
  192. return 0;
  193. }
  194. static int is_child_device(char *devpath)
  195. {
  196. return (devpath[2] ? 1 : 0);
  197. }
  198. static int is_hub_limit(char *devpath)
  199. {
  200. return ((strlen(devpath) >= 4) ? 1 : 0);
  201. }
  202. static void get_port_number(struct r8a66597 *r8a66597,
  203. char *devpath, u16 *root_port, u16 *hub_port)
  204. {
  205. if (root_port) {
  206. *root_port = (devpath[0] & 0x0F) - 1;
  207. if (*root_port >= r8a66597->max_root_hub)
  208. printk(KERN_ERR "r8a66597: Illegal root port number.\n");
  209. }
  210. if (hub_port)
  211. *hub_port = devpath[2] & 0x0F;
  212. }
  213. static u16 get_r8a66597_usb_speed(enum usb_device_speed speed)
  214. {
  215. u16 usbspd = 0;
  216. switch (speed) {
  217. case USB_SPEED_LOW:
  218. usbspd = LSMODE;
  219. break;
  220. case USB_SPEED_FULL:
  221. usbspd = FSMODE;
  222. break;
  223. case USB_SPEED_HIGH:
  224. usbspd = HSMODE;
  225. break;
  226. default:
  227. printk(KERN_ERR "r8a66597: unknown speed\n");
  228. break;
  229. }
  230. return usbspd;
  231. }
  232. static void set_child_connect_map(struct r8a66597 *r8a66597, int address)
  233. {
  234. int idx;
  235. idx = address / 32;
  236. r8a66597->child_connect_map[idx] |= 1 << (address % 32);
  237. }
  238. static void put_child_connect_map(struct r8a66597 *r8a66597, int address)
  239. {
  240. int idx;
  241. idx = address / 32;
  242. r8a66597->child_connect_map[idx] &= ~(1 << (address % 32));
  243. }
  244. static void set_pipe_reg_addr(struct r8a66597_pipe *pipe, u8 dma_ch)
  245. {
  246. u16 pipenum = pipe->info.pipenum;
  247. const unsigned long fifoaddr[] = {D0FIFO, D1FIFO, CFIFO};
  248. const unsigned long fifosel[] = {D0FIFOSEL, D1FIFOSEL, CFIFOSEL};
  249. const unsigned long fifoctr[] = {D0FIFOCTR, D1FIFOCTR, CFIFOCTR};
  250. if (dma_ch > R8A66597_PIPE_NO_DMA) /* dma fifo not use? */
  251. dma_ch = R8A66597_PIPE_NO_DMA;
  252. pipe->fifoaddr = fifoaddr[dma_ch];
  253. pipe->fifosel = fifosel[dma_ch];
  254. pipe->fifoctr = fifoctr[dma_ch];
  255. if (pipenum == 0)
  256. pipe->pipectr = DCPCTR;
  257. else
  258. pipe->pipectr = get_pipectr_addr(pipenum);
  259. if (check_bulk_or_isoc(pipenum)) {
  260. pipe->pipetre = get_pipetre_addr(pipenum);
  261. pipe->pipetrn = get_pipetrn_addr(pipenum);
  262. } else {
  263. pipe->pipetre = 0;
  264. pipe->pipetrn = 0;
  265. }
  266. }
  267. static struct r8a66597_device *
  268. get_urb_to_r8a66597_dev(struct r8a66597 *r8a66597, struct urb *urb)
  269. {
  270. if (usb_pipedevice(urb->pipe) == 0)
  271. return &r8a66597->device0;
  272. return dev_get_drvdata(&urb->dev->dev);
  273. }
  274. static int make_r8a66597_device(struct r8a66597 *r8a66597,
  275. struct urb *urb, u8 addr)
  276. {
  277. struct r8a66597_device *dev;
  278. int usb_address = urb->setup_packet[2]; /* urb->pipe is address 0 */
  279. dev = kzalloc(sizeof(struct r8a66597_device), GFP_ATOMIC);
  280. if (dev == NULL)
  281. return -ENOMEM;
  282. dev_set_drvdata(&urb->dev->dev, dev);
  283. dev->udev = urb->dev;
  284. dev->address = addr;
  285. dev->usb_address = usb_address;
  286. dev->state = USB_STATE_ADDRESS;
  287. dev->ep_in_toggle = 0;
  288. dev->ep_out_toggle = 0;
  289. INIT_LIST_HEAD(&dev->device_list);
  290. list_add_tail(&dev->device_list, &r8a66597->child_device);
  291. get_port_number(r8a66597, urb->dev->devpath,
  292. &dev->root_port, &dev->hub_port);
  293. if (!is_child_device(urb->dev->devpath))
  294. r8a66597->root_hub[dev->root_port].dev = dev;
  295. set_devadd_reg(r8a66597, dev->address,
  296. get_r8a66597_usb_speed(urb->dev->speed),
  297. get_parent_r8a66597_address(r8a66597, urb->dev),
  298. dev->hub_port, dev->root_port);
  299. return 0;
  300. }
  301. /* this function must be called with interrupt disabled */
  302. static u8 alloc_usb_address(struct r8a66597 *r8a66597, struct urb *urb)
  303. {
  304. u8 addr; /* R8A66597's address */
  305. struct r8a66597_device *dev;
  306. if (is_hub_limit(urb->dev->devpath)) {
  307. dev_err(&urb->dev->dev, "External hub limit reached.\n");
  308. return 0;
  309. }
  310. dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  311. if (dev && dev->state >= USB_STATE_ADDRESS)
  312. return dev->address;
  313. for (addr = 1; addr <= R8A66597_MAX_DEVICE; addr++) {
  314. if (r8a66597->address_map & (1 << addr))
  315. continue;
  316. dev_dbg(&urb->dev->dev, "alloc_address: r8a66597_addr=%d\n", addr);
  317. r8a66597->address_map |= 1 << addr;
  318. if (make_r8a66597_device(r8a66597, urb, addr) < 0)
  319. return 0;
  320. return addr;
  321. }
  322. dev_err(&urb->dev->dev,
  323. "cannot communicate with a USB device more than 10.(%x)\n",
  324. r8a66597->address_map);
  325. return 0;
  326. }
  327. /* this function must be called with interrupt disabled */
  328. static void free_usb_address(struct r8a66597 *r8a66597,
  329. struct r8a66597_device *dev, int reset)
  330. {
  331. int port;
  332. if (!dev)
  333. return;
  334. dev_dbg(&dev->udev->dev, "free_addr: addr=%d\n", dev->address);
  335. dev->state = USB_STATE_DEFAULT;
  336. r8a66597->address_map &= ~(1 << dev->address);
  337. dev->address = 0;
  338. /*
  339. * Only when resetting USB, it is necessary to erase drvdata. When
  340. * a usb device with usb hub is disconnect, "dev->udev" is already
  341. * freed on usb_desconnect(). So we cannot access the data.
  342. */
  343. if (reset)
  344. dev_set_drvdata(&dev->udev->dev, NULL);
  345. list_del(&dev->device_list);
  346. kfree(dev);
  347. for (port = 0; port < r8a66597->max_root_hub; port++) {
  348. if (r8a66597->root_hub[port].dev == dev) {
  349. r8a66597->root_hub[port].dev = NULL;
  350. break;
  351. }
  352. }
  353. }
  354. static void r8a66597_reg_wait(struct r8a66597 *r8a66597, unsigned long reg,
  355. u16 mask, u16 loop)
  356. {
  357. u16 tmp;
  358. int i = 0;
  359. do {
  360. tmp = r8a66597_read(r8a66597, reg);
  361. if (i++ > 1000000) {
  362. printk(KERN_ERR "r8a66597: register%lx, loop %x "
  363. "is timeout\n", reg, loop);
  364. break;
  365. }
  366. ndelay(1);
  367. } while ((tmp & mask) != loop);
  368. }
  369. /* this function must be called with interrupt disabled */
  370. static void pipe_start(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
  371. {
  372. u16 tmp;
  373. tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
  374. if ((pipe->info.pipenum != 0) & ((tmp & PID_STALL) != 0)) /* stall? */
  375. r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
  376. r8a66597_mdfy(r8a66597, PID_BUF, PID, pipe->pipectr);
  377. }
  378. /* this function must be called with interrupt disabled */
  379. static void pipe_stop(struct r8a66597 *r8a66597, struct r8a66597_pipe *pipe)
  380. {
  381. u16 tmp;
  382. tmp = r8a66597_read(r8a66597, pipe->pipectr) & PID;
  383. if ((tmp & PID_STALL11) != PID_STALL11) /* force stall? */
  384. r8a66597_mdfy(r8a66597, PID_STALL, PID, pipe->pipectr);
  385. r8a66597_mdfy(r8a66597, PID_NAK, PID, pipe->pipectr);
  386. r8a66597_reg_wait(r8a66597, pipe->pipectr, PBUSY, 0);
  387. }
  388. /* this function must be called with interrupt disabled */
  389. static void clear_all_buffer(struct r8a66597 *r8a66597,
  390. struct r8a66597_pipe *pipe)
  391. {
  392. u16 tmp;
  393. if (!pipe || pipe->info.pipenum == 0)
  394. return;
  395. pipe_stop(r8a66597, pipe);
  396. r8a66597_bset(r8a66597, ACLRM, pipe->pipectr);
  397. tmp = r8a66597_read(r8a66597, pipe->pipectr);
  398. tmp = r8a66597_read(r8a66597, pipe->pipectr);
  399. tmp = r8a66597_read(r8a66597, pipe->pipectr);
  400. r8a66597_bclr(r8a66597, ACLRM, pipe->pipectr);
  401. }
  402. /* this function must be called with interrupt disabled */
  403. static void r8a66597_pipe_toggle(struct r8a66597 *r8a66597,
  404. struct r8a66597_pipe *pipe, int toggle)
  405. {
  406. if (toggle)
  407. r8a66597_bset(r8a66597, SQSET, pipe->pipectr);
  408. else
  409. r8a66597_bset(r8a66597, SQCLR, pipe->pipectr);
  410. }
  411. static inline unsigned short mbw_value(struct r8a66597 *r8a66597)
  412. {
  413. if (r8a66597->pdata->on_chip)
  414. return MBW_32;
  415. else
  416. return MBW_16;
  417. }
  418. /* this function must be called with interrupt disabled */
  419. static inline void cfifo_change(struct r8a66597 *r8a66597, u16 pipenum)
  420. {
  421. unsigned short mbw = mbw_value(r8a66597);
  422. r8a66597_mdfy(r8a66597, mbw | pipenum, mbw | CURPIPE, CFIFOSEL);
  423. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, pipenum);
  424. }
  425. /* this function must be called with interrupt disabled */
  426. static inline void fifo_change_from_pipe(struct r8a66597 *r8a66597,
  427. struct r8a66597_pipe *pipe)
  428. {
  429. unsigned short mbw = mbw_value(r8a66597);
  430. cfifo_change(r8a66597, 0);
  431. r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D0FIFOSEL);
  432. r8a66597_mdfy(r8a66597, mbw | 0, mbw | CURPIPE, D1FIFOSEL);
  433. r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum, mbw | CURPIPE,
  434. pipe->fifosel);
  435. r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE, pipe->info.pipenum);
  436. }
  437. static u16 r8a66597_get_pipenum(struct urb *urb, struct usb_host_endpoint *hep)
  438. {
  439. struct r8a66597_pipe *pipe = hep->hcpriv;
  440. if (usb_pipeendpoint(urb->pipe) == 0)
  441. return 0;
  442. else
  443. return pipe->info.pipenum;
  444. }
  445. static u16 get_urb_to_r8a66597_addr(struct r8a66597 *r8a66597, struct urb *urb)
  446. {
  447. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  448. return (usb_pipedevice(urb->pipe) == 0) ? 0 : dev->address;
  449. }
  450. static unsigned short *get_toggle_pointer(struct r8a66597_device *dev,
  451. int urb_pipe)
  452. {
  453. if (!dev)
  454. return NULL;
  455. return usb_pipein(urb_pipe) ? &dev->ep_in_toggle : &dev->ep_out_toggle;
  456. }
  457. /* this function must be called with interrupt disabled */
  458. static void pipe_toggle_set(struct r8a66597 *r8a66597,
  459. struct r8a66597_pipe *pipe,
  460. struct urb *urb, int set)
  461. {
  462. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  463. unsigned char endpoint = usb_pipeendpoint(urb->pipe);
  464. unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
  465. if (!toggle)
  466. return;
  467. if (set)
  468. *toggle |= 1 << endpoint;
  469. else
  470. *toggle &= ~(1 << endpoint);
  471. }
  472. /* this function must be called with interrupt disabled */
  473. static void pipe_toggle_save(struct r8a66597 *r8a66597,
  474. struct r8a66597_pipe *pipe,
  475. struct urb *urb)
  476. {
  477. if (r8a66597_read(r8a66597, pipe->pipectr) & SQMON)
  478. pipe_toggle_set(r8a66597, pipe, urb, 1);
  479. else
  480. pipe_toggle_set(r8a66597, pipe, urb, 0);
  481. }
  482. /* this function must be called with interrupt disabled */
  483. static void pipe_toggle_restore(struct r8a66597 *r8a66597,
  484. struct r8a66597_pipe *pipe,
  485. struct urb *urb)
  486. {
  487. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  488. unsigned char endpoint = usb_pipeendpoint(urb->pipe);
  489. unsigned short *toggle = get_toggle_pointer(dev, urb->pipe);
  490. if (!toggle)
  491. return;
  492. r8a66597_pipe_toggle(r8a66597, pipe, *toggle & (1 << endpoint));
  493. }
  494. /* this function must be called with interrupt disabled */
  495. static void pipe_buffer_setting(struct r8a66597 *r8a66597,
  496. struct r8a66597_pipe_info *info)
  497. {
  498. u16 val = 0;
  499. if (info->pipenum == 0)
  500. return;
  501. r8a66597_bset(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
  502. r8a66597_bclr(r8a66597, ACLRM, get_pipectr_addr(info->pipenum));
  503. r8a66597_write(r8a66597, info->pipenum, PIPESEL);
  504. if (!info->dir_in)
  505. val |= R8A66597_DIR;
  506. if (info->type == R8A66597_BULK && info->dir_in)
  507. val |= R8A66597_DBLB | R8A66597_SHTNAK;
  508. val |= info->type | info->epnum;
  509. r8a66597_write(r8a66597, val, PIPECFG);
  510. r8a66597_write(r8a66597, (info->buf_bsize << 10) | (info->bufnum),
  511. PIPEBUF);
  512. r8a66597_write(r8a66597, make_devsel(info->address) | info->maxpacket,
  513. PIPEMAXP);
  514. r8a66597_write(r8a66597, info->interval, PIPEPERI);
  515. }
  516. /* this function must be called with interrupt disabled */
  517. static void pipe_setting(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  518. {
  519. struct r8a66597_pipe_info *info;
  520. struct urb *urb = td->urb;
  521. if (td->pipenum > 0) {
  522. info = &td->pipe->info;
  523. cfifo_change(r8a66597, 0);
  524. pipe_buffer_setting(r8a66597, info);
  525. if (!usb_gettoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  526. usb_pipeout(urb->pipe)) &&
  527. !usb_pipecontrol(urb->pipe)) {
  528. r8a66597_pipe_toggle(r8a66597, td->pipe, 0);
  529. pipe_toggle_set(r8a66597, td->pipe, urb, 0);
  530. clear_all_buffer(r8a66597, td->pipe);
  531. usb_settoggle(urb->dev, usb_pipeendpoint(urb->pipe),
  532. usb_pipeout(urb->pipe), 1);
  533. }
  534. pipe_toggle_restore(r8a66597, td->pipe, urb);
  535. }
  536. }
  537. /* this function must be called with interrupt disabled */
  538. static u16 get_empty_pipenum(struct r8a66597 *r8a66597,
  539. struct usb_endpoint_descriptor *ep)
  540. {
  541. u16 array[R8A66597_MAX_NUM_PIPE], i = 0, min;
  542. memset(array, 0, sizeof(array));
  543. switch (usb_endpoint_type(ep)) {
  544. case USB_ENDPOINT_XFER_BULK:
  545. if (usb_endpoint_dir_in(ep))
  546. array[i++] = 4;
  547. else {
  548. array[i++] = 3;
  549. array[i++] = 5;
  550. }
  551. break;
  552. case USB_ENDPOINT_XFER_INT:
  553. if (usb_endpoint_dir_in(ep)) {
  554. array[i++] = 6;
  555. array[i++] = 7;
  556. array[i++] = 8;
  557. } else
  558. array[i++] = 9;
  559. break;
  560. case USB_ENDPOINT_XFER_ISOC:
  561. if (usb_endpoint_dir_in(ep))
  562. array[i++] = 2;
  563. else
  564. array[i++] = 1;
  565. break;
  566. default:
  567. printk(KERN_ERR "r8a66597: Illegal type\n");
  568. return 0;
  569. }
  570. i = 1;
  571. min = array[0];
  572. while (array[i] != 0) {
  573. if (r8a66597->pipe_cnt[min] > r8a66597->pipe_cnt[array[i]])
  574. min = array[i];
  575. i++;
  576. }
  577. return min;
  578. }
  579. static u16 get_r8a66597_type(__u8 type)
  580. {
  581. u16 r8a66597_type;
  582. switch (type) {
  583. case USB_ENDPOINT_XFER_BULK:
  584. r8a66597_type = R8A66597_BULK;
  585. break;
  586. case USB_ENDPOINT_XFER_INT:
  587. r8a66597_type = R8A66597_INT;
  588. break;
  589. case USB_ENDPOINT_XFER_ISOC:
  590. r8a66597_type = R8A66597_ISO;
  591. break;
  592. default:
  593. printk(KERN_ERR "r8a66597: Illegal type\n");
  594. r8a66597_type = 0x0000;
  595. break;
  596. }
  597. return r8a66597_type;
  598. }
  599. static u16 get_bufnum(u16 pipenum)
  600. {
  601. u16 bufnum = 0;
  602. if (pipenum == 0)
  603. bufnum = 0;
  604. else if (check_bulk_or_isoc(pipenum))
  605. bufnum = 8 + (pipenum - 1) * R8A66597_BUF_BSIZE*2;
  606. else if (check_interrupt(pipenum))
  607. bufnum = 4 + (pipenum - 6);
  608. else
  609. printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
  610. return bufnum;
  611. }
  612. static u16 get_buf_bsize(u16 pipenum)
  613. {
  614. u16 buf_bsize = 0;
  615. if (pipenum == 0)
  616. buf_bsize = 3;
  617. else if (check_bulk_or_isoc(pipenum))
  618. buf_bsize = R8A66597_BUF_BSIZE - 1;
  619. else if (check_interrupt(pipenum))
  620. buf_bsize = 0;
  621. else
  622. printk(KERN_ERR "r8a66597: Illegal pipenum (%d)\n", pipenum);
  623. return buf_bsize;
  624. }
  625. /* this function must be called with interrupt disabled */
  626. static void enable_r8a66597_pipe_dma(struct r8a66597 *r8a66597,
  627. struct r8a66597_device *dev,
  628. struct r8a66597_pipe *pipe,
  629. struct urb *urb)
  630. {
  631. int i;
  632. struct r8a66597_pipe_info *info = &pipe->info;
  633. unsigned short mbw = mbw_value(r8a66597);
  634. /* pipe dma is only for external controlles */
  635. if (r8a66597->pdata->on_chip)
  636. return;
  637. if ((pipe->info.pipenum != 0) && (info->type != R8A66597_INT)) {
  638. for (i = 0; i < R8A66597_MAX_DMA_CHANNEL; i++) {
  639. if ((r8a66597->dma_map & (1 << i)) != 0)
  640. continue;
  641. dev_info(&dev->udev->dev,
  642. "address %d, EndpointAddress 0x%02x use "
  643. "DMA FIFO\n", usb_pipedevice(urb->pipe),
  644. info->dir_in ?
  645. USB_ENDPOINT_DIR_MASK + info->epnum
  646. : info->epnum);
  647. r8a66597->dma_map |= 1 << i;
  648. dev->dma_map |= 1 << i;
  649. set_pipe_reg_addr(pipe, i);
  650. cfifo_change(r8a66597, 0);
  651. r8a66597_mdfy(r8a66597, mbw | pipe->info.pipenum,
  652. mbw | CURPIPE, pipe->fifosel);
  653. r8a66597_reg_wait(r8a66597, pipe->fifosel, CURPIPE,
  654. pipe->info.pipenum);
  655. r8a66597_bset(r8a66597, BCLR, pipe->fifoctr);
  656. break;
  657. }
  658. }
  659. }
  660. /* this function must be called with interrupt disabled */
  661. static void enable_r8a66597_pipe(struct r8a66597 *r8a66597, struct urb *urb,
  662. struct usb_host_endpoint *hep,
  663. struct r8a66597_pipe_info *info)
  664. {
  665. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  666. struct r8a66597_pipe *pipe = hep->hcpriv;
  667. dev_dbg(&dev->udev->dev, "enable_pipe:\n");
  668. pipe->info = *info;
  669. set_pipe_reg_addr(pipe, R8A66597_PIPE_NO_DMA);
  670. r8a66597->pipe_cnt[pipe->info.pipenum]++;
  671. dev->pipe_cnt[pipe->info.pipenum]++;
  672. enable_r8a66597_pipe_dma(r8a66597, dev, pipe, urb);
  673. }
  674. static void r8a66597_urb_done(struct r8a66597 *r8a66597, struct urb *urb,
  675. int status)
  676. __releases(r8a66597->lock)
  677. __acquires(r8a66597->lock)
  678. {
  679. if (usb_pipein(urb->pipe) && usb_pipetype(urb->pipe) != PIPE_CONTROL) {
  680. void *ptr;
  681. for (ptr = urb->transfer_buffer;
  682. ptr < urb->transfer_buffer + urb->transfer_buffer_length;
  683. ptr += PAGE_SIZE)
  684. flush_dcache_page(virt_to_page(ptr));
  685. }
  686. usb_hcd_unlink_urb_from_ep(r8a66597_to_hcd(r8a66597), urb);
  687. spin_unlock(&r8a66597->lock);
  688. usb_hcd_giveback_urb(r8a66597_to_hcd(r8a66597), urb, status);
  689. spin_lock(&r8a66597->lock);
  690. }
  691. /* this function must be called with interrupt disabled */
  692. static void force_dequeue(struct r8a66597 *r8a66597, u16 pipenum, u16 address)
  693. {
  694. struct r8a66597_td *td, *next;
  695. struct urb *urb;
  696. struct list_head *list = &r8a66597->pipe_queue[pipenum];
  697. if (list_empty(list))
  698. return;
  699. list_for_each_entry_safe(td, next, list, queue) {
  700. if (td->address != address)
  701. continue;
  702. urb = td->urb;
  703. list_del(&td->queue);
  704. kfree(td);
  705. if (urb)
  706. r8a66597_urb_done(r8a66597, urb, -ENODEV);
  707. break;
  708. }
  709. }
  710. /* this function must be called with interrupt disabled */
  711. static void disable_r8a66597_pipe_all(struct r8a66597 *r8a66597,
  712. struct r8a66597_device *dev)
  713. {
  714. int check_ep0 = 0;
  715. u16 pipenum;
  716. if (!dev)
  717. return;
  718. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  719. if (!dev->pipe_cnt[pipenum])
  720. continue;
  721. if (!check_ep0) {
  722. check_ep0 = 1;
  723. force_dequeue(r8a66597, 0, dev->address);
  724. }
  725. r8a66597->pipe_cnt[pipenum] -= dev->pipe_cnt[pipenum];
  726. dev->pipe_cnt[pipenum] = 0;
  727. force_dequeue(r8a66597, pipenum, dev->address);
  728. }
  729. dev_dbg(&dev->udev->dev, "disable_pipe\n");
  730. r8a66597->dma_map &= ~(dev->dma_map);
  731. dev->dma_map = 0;
  732. }
  733. static u16 get_interval(struct urb *urb, __u8 interval)
  734. {
  735. u16 time = 1;
  736. int i;
  737. if (urb->dev->speed == USB_SPEED_HIGH) {
  738. if (interval > IITV)
  739. time = IITV;
  740. else
  741. time = interval ? interval - 1 : 0;
  742. } else {
  743. if (interval > 128) {
  744. time = IITV;
  745. } else {
  746. /* calculate the nearest value for PIPEPERI */
  747. for (i = 0; i < 7; i++) {
  748. if ((1 << i) < interval &&
  749. (1 << (i + 1) > interval))
  750. time = 1 << i;
  751. }
  752. }
  753. }
  754. return time;
  755. }
  756. static unsigned long get_timer_interval(struct urb *urb, __u8 interval)
  757. {
  758. __u8 i;
  759. unsigned long time = 1;
  760. if (usb_pipeisoc(urb->pipe))
  761. return 0;
  762. if (get_r8a66597_usb_speed(urb->dev->speed) == HSMODE) {
  763. for (i = 0; i < (interval - 1); i++)
  764. time *= 2;
  765. time = time * 125 / 1000; /* uSOF -> msec */
  766. } else {
  767. time = interval;
  768. }
  769. return time;
  770. }
  771. /* this function must be called with interrupt disabled */
  772. static void init_pipe_info(struct r8a66597 *r8a66597, struct urb *urb,
  773. struct usb_host_endpoint *hep,
  774. struct usb_endpoint_descriptor *ep)
  775. {
  776. struct r8a66597_pipe_info info;
  777. info.pipenum = get_empty_pipenum(r8a66597, ep);
  778. info.address = get_urb_to_r8a66597_addr(r8a66597, urb);
  779. info.epnum = usb_endpoint_num(ep);
  780. info.maxpacket = usb_endpoint_maxp(ep);
  781. info.type = get_r8a66597_type(usb_endpoint_type(ep));
  782. info.bufnum = get_bufnum(info.pipenum);
  783. info.buf_bsize = get_buf_bsize(info.pipenum);
  784. if (info.type == R8A66597_BULK) {
  785. info.interval = 0;
  786. info.timer_interval = 0;
  787. } else {
  788. info.interval = get_interval(urb, ep->bInterval);
  789. info.timer_interval = get_timer_interval(urb, ep->bInterval);
  790. }
  791. if (usb_endpoint_dir_in(ep))
  792. info.dir_in = 1;
  793. else
  794. info.dir_in = 0;
  795. enable_r8a66597_pipe(r8a66597, urb, hep, &info);
  796. }
  797. static void init_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  798. {
  799. struct r8a66597_device *dev;
  800. dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  801. dev->state = USB_STATE_CONFIGURED;
  802. }
  803. static void pipe_irq_enable(struct r8a66597 *r8a66597, struct urb *urb,
  804. u16 pipenum)
  805. {
  806. if (pipenum == 0 && usb_pipeout(urb->pipe))
  807. enable_irq_empty(r8a66597, pipenum);
  808. else
  809. enable_irq_ready(r8a66597, pipenum);
  810. if (!usb_pipeisoc(urb->pipe))
  811. enable_irq_nrdy(r8a66597, pipenum);
  812. }
  813. static void pipe_irq_disable(struct r8a66597 *r8a66597, u16 pipenum)
  814. {
  815. disable_irq_ready(r8a66597, pipenum);
  816. disable_irq_nrdy(r8a66597, pipenum);
  817. }
  818. static void r8a66597_root_hub_start_polling(struct r8a66597 *r8a66597)
  819. {
  820. mod_timer(&r8a66597->rh_timer,
  821. jiffies + msecs_to_jiffies(R8A66597_RH_POLL_TIME));
  822. }
  823. static void start_root_hub_sampling(struct r8a66597 *r8a66597, int port,
  824. int connect)
  825. {
  826. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  827. rh->old_syssts = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  828. rh->scount = R8A66597_MAX_SAMPLING;
  829. if (connect)
  830. rh->port |= USB_PORT_STAT_CONNECTION;
  831. else
  832. rh->port &= ~USB_PORT_STAT_CONNECTION;
  833. rh->port |= USB_PORT_STAT_C_CONNECTION << 16;
  834. r8a66597_root_hub_start_polling(r8a66597);
  835. }
  836. /* this function must be called with interrupt disabled */
  837. static void r8a66597_check_syssts(struct r8a66597 *r8a66597, int port,
  838. u16 syssts)
  839. __releases(r8a66597->lock)
  840. __acquires(r8a66597->lock)
  841. {
  842. if (syssts == SE0) {
  843. r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
  844. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  845. } else {
  846. if (syssts == FS_JSTS)
  847. r8a66597_bset(r8a66597, HSE, get_syscfg_reg(port));
  848. else if (syssts == LS_JSTS)
  849. r8a66597_bclr(r8a66597, HSE, get_syscfg_reg(port));
  850. r8a66597_write(r8a66597, ~DTCH, get_intsts_reg(port));
  851. r8a66597_bset(r8a66597, DTCHE, get_intenb_reg(port));
  852. if (r8a66597->bus_suspended)
  853. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  854. }
  855. spin_unlock(&r8a66597->lock);
  856. usb_hcd_poll_rh_status(r8a66597_to_hcd(r8a66597));
  857. spin_lock(&r8a66597->lock);
  858. }
  859. /* this function must be called with interrupt disabled */
  860. static void r8a66597_usb_connect(struct r8a66597 *r8a66597, int port)
  861. {
  862. u16 speed = get_rh_usb_speed(r8a66597, port);
  863. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  864. rh->port &= ~(USB_PORT_STAT_HIGH_SPEED | USB_PORT_STAT_LOW_SPEED);
  865. if (speed == HSMODE)
  866. rh->port |= USB_PORT_STAT_HIGH_SPEED;
  867. else if (speed == LSMODE)
  868. rh->port |= USB_PORT_STAT_LOW_SPEED;
  869. rh->port &= ~USB_PORT_STAT_RESET;
  870. rh->port |= USB_PORT_STAT_ENABLE;
  871. }
  872. /* this function must be called with interrupt disabled */
  873. static void r8a66597_usb_disconnect(struct r8a66597 *r8a66597, int port)
  874. {
  875. struct r8a66597_device *dev = r8a66597->root_hub[port].dev;
  876. disable_r8a66597_pipe_all(r8a66597, dev);
  877. free_usb_address(r8a66597, dev, 0);
  878. start_root_hub_sampling(r8a66597, port, 0);
  879. }
  880. /* this function must be called with interrupt disabled */
  881. static void prepare_setup_packet(struct r8a66597 *r8a66597,
  882. struct r8a66597_td *td)
  883. {
  884. int i;
  885. __le16 *p = (__le16 *)td->urb->setup_packet;
  886. unsigned long setup_addr = USBREQ;
  887. r8a66597_write(r8a66597, make_devsel(td->address) | td->maxpacket,
  888. DCPMAXP);
  889. r8a66597_write(r8a66597, ~(SIGN | SACK), INTSTS1);
  890. for (i = 0; i < 4; i++) {
  891. r8a66597_write(r8a66597, le16_to_cpu(p[i]), setup_addr);
  892. setup_addr += 2;
  893. }
  894. r8a66597_write(r8a66597, SUREQ, DCPCTR);
  895. }
  896. /* this function must be called with interrupt disabled */
  897. static void prepare_packet_read(struct r8a66597 *r8a66597,
  898. struct r8a66597_td *td)
  899. {
  900. struct urb *urb = td->urb;
  901. if (usb_pipecontrol(urb->pipe)) {
  902. r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
  903. r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
  904. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  905. if (urb->actual_length == 0) {
  906. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  907. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  908. }
  909. pipe_irq_disable(r8a66597, td->pipenum);
  910. pipe_start(r8a66597, td->pipe);
  911. pipe_irq_enable(r8a66597, urb, td->pipenum);
  912. } else {
  913. if (urb->actual_length == 0) {
  914. pipe_irq_disable(r8a66597, td->pipenum);
  915. pipe_setting(r8a66597, td);
  916. pipe_stop(r8a66597, td->pipe);
  917. r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
  918. if (td->pipe->pipetre) {
  919. r8a66597_write(r8a66597, TRCLR,
  920. td->pipe->pipetre);
  921. r8a66597_write(r8a66597,
  922. DIV_ROUND_UP
  923. (urb->transfer_buffer_length,
  924. td->maxpacket),
  925. td->pipe->pipetrn);
  926. r8a66597_bset(r8a66597, TRENB,
  927. td->pipe->pipetre);
  928. }
  929. pipe_start(r8a66597, td->pipe);
  930. pipe_irq_enable(r8a66597, urb, td->pipenum);
  931. }
  932. }
  933. }
  934. /* this function must be called with interrupt disabled */
  935. static void prepare_packet_write(struct r8a66597 *r8a66597,
  936. struct r8a66597_td *td)
  937. {
  938. u16 tmp;
  939. struct urb *urb = td->urb;
  940. if (usb_pipecontrol(urb->pipe)) {
  941. pipe_stop(r8a66597, td->pipe);
  942. r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
  943. r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
  944. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  945. if (urb->actual_length == 0) {
  946. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  947. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  948. }
  949. } else {
  950. if (urb->actual_length == 0)
  951. pipe_setting(r8a66597, td);
  952. if (td->pipe->pipetre)
  953. r8a66597_bclr(r8a66597, TRENB, td->pipe->pipetre);
  954. }
  955. r8a66597_write(r8a66597, ~(1 << td->pipenum), BRDYSTS);
  956. fifo_change_from_pipe(r8a66597, td->pipe);
  957. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  958. if (unlikely((tmp & FRDY) == 0))
  959. pipe_irq_enable(r8a66597, urb, td->pipenum);
  960. else
  961. packet_write(r8a66597, td->pipenum);
  962. pipe_start(r8a66597, td->pipe);
  963. }
  964. /* this function must be called with interrupt disabled */
  965. static void prepare_status_packet(struct r8a66597 *r8a66597,
  966. struct r8a66597_td *td)
  967. {
  968. struct urb *urb = td->urb;
  969. r8a66597_pipe_toggle(r8a66597, td->pipe, 1);
  970. pipe_stop(r8a66597, td->pipe);
  971. if (urb->setup_packet[0] & USB_ENDPOINT_DIR_MASK) {
  972. r8a66597_bset(r8a66597, R8A66597_DIR, DCPCFG);
  973. r8a66597_mdfy(r8a66597, ISEL, ISEL | CURPIPE, CFIFOSEL);
  974. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  975. r8a66597_write(r8a66597, ~BEMP0, BEMPSTS);
  976. r8a66597_write(r8a66597, BCLR | BVAL, CFIFOCTR);
  977. enable_irq_empty(r8a66597, 0);
  978. } else {
  979. r8a66597_bclr(r8a66597, R8A66597_DIR, DCPCFG);
  980. r8a66597_mdfy(r8a66597, 0, ISEL | CURPIPE, CFIFOSEL);
  981. r8a66597_reg_wait(r8a66597, CFIFOSEL, CURPIPE, 0);
  982. r8a66597_write(r8a66597, BCLR, CFIFOCTR);
  983. enable_irq_ready(r8a66597, 0);
  984. }
  985. enable_irq_nrdy(r8a66597, 0);
  986. pipe_start(r8a66597, td->pipe);
  987. }
  988. static int is_set_address(unsigned char *setup_packet)
  989. {
  990. if (((setup_packet[0] & USB_TYPE_MASK) == USB_TYPE_STANDARD) &&
  991. setup_packet[1] == USB_REQ_SET_ADDRESS)
  992. return 1;
  993. else
  994. return 0;
  995. }
  996. /* this function must be called with interrupt disabled */
  997. static int start_transfer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  998. {
  999. BUG_ON(!td);
  1000. switch (td->type) {
  1001. case USB_PID_SETUP:
  1002. if (is_set_address(td->urb->setup_packet)) {
  1003. td->set_address = 1;
  1004. td->urb->setup_packet[2] = alloc_usb_address(r8a66597,
  1005. td->urb);
  1006. if (td->urb->setup_packet[2] == 0)
  1007. return -EPIPE;
  1008. }
  1009. prepare_setup_packet(r8a66597, td);
  1010. break;
  1011. case USB_PID_IN:
  1012. prepare_packet_read(r8a66597, td);
  1013. break;
  1014. case USB_PID_OUT:
  1015. prepare_packet_write(r8a66597, td);
  1016. break;
  1017. case USB_PID_ACK:
  1018. prepare_status_packet(r8a66597, td);
  1019. break;
  1020. default:
  1021. printk(KERN_ERR "r8a66597: invalid type.\n");
  1022. break;
  1023. }
  1024. return 0;
  1025. }
  1026. static int check_transfer_finish(struct r8a66597_td *td, struct urb *urb)
  1027. {
  1028. if (usb_pipeisoc(urb->pipe)) {
  1029. if (urb->number_of_packets == td->iso_cnt)
  1030. return 1;
  1031. }
  1032. /* control or bulk or interrupt */
  1033. if ((urb->transfer_buffer_length <= urb->actual_length) ||
  1034. (td->short_packet) || (td->zero_packet))
  1035. return 1;
  1036. return 0;
  1037. }
  1038. /* this function must be called with interrupt disabled */
  1039. static void set_td_timer(struct r8a66597 *r8a66597, struct r8a66597_td *td)
  1040. {
  1041. unsigned long time;
  1042. BUG_ON(!td);
  1043. if (!list_empty(&r8a66597->pipe_queue[td->pipenum]) &&
  1044. !usb_pipecontrol(td->urb->pipe) && usb_pipein(td->urb->pipe)) {
  1045. r8a66597->timeout_map |= 1 << td->pipenum;
  1046. switch (usb_pipetype(td->urb->pipe)) {
  1047. case PIPE_INTERRUPT:
  1048. case PIPE_ISOCHRONOUS:
  1049. time = 30;
  1050. break;
  1051. default:
  1052. time = 50;
  1053. break;
  1054. }
  1055. mod_timer(&r8a66597->timers[td->pipenum].td,
  1056. jiffies + msecs_to_jiffies(time));
  1057. }
  1058. }
  1059. /* this function must be called with interrupt disabled */
  1060. static void finish_request(struct r8a66597 *r8a66597, struct r8a66597_td *td,
  1061. u16 pipenum, struct urb *urb, int status)
  1062. __releases(r8a66597->lock) __acquires(r8a66597->lock)
  1063. {
  1064. int restart = 0;
  1065. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1066. r8a66597->timeout_map &= ~(1 << pipenum);
  1067. if (likely(td)) {
  1068. if (td->set_address && (status != 0 || urb->unlinked))
  1069. r8a66597->address_map &= ~(1 << urb->setup_packet[2]);
  1070. pipe_toggle_save(r8a66597, td->pipe, urb);
  1071. list_del(&td->queue);
  1072. kfree(td);
  1073. }
  1074. if (!list_empty(&r8a66597->pipe_queue[pipenum]))
  1075. restart = 1;
  1076. if (likely(urb)) {
  1077. if (usb_pipeisoc(urb->pipe))
  1078. urb->start_frame = r8a66597_get_frame(hcd);
  1079. r8a66597_urb_done(r8a66597, urb, status);
  1080. }
  1081. if (restart) {
  1082. td = r8a66597_get_td(r8a66597, pipenum);
  1083. if (unlikely(!td))
  1084. return;
  1085. start_transfer(r8a66597, td);
  1086. set_td_timer(r8a66597, td);
  1087. }
  1088. }
  1089. static void packet_read(struct r8a66597 *r8a66597, u16 pipenum)
  1090. {
  1091. u16 tmp;
  1092. int rcv_len, bufsize, urb_len, size;
  1093. u16 *buf;
  1094. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1095. struct urb *urb;
  1096. int finish = 0;
  1097. int status = 0;
  1098. if (unlikely(!td))
  1099. return;
  1100. urb = td->urb;
  1101. fifo_change_from_pipe(r8a66597, td->pipe);
  1102. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1103. if (unlikely((tmp & FRDY) == 0)) {
  1104. pipe_stop(r8a66597, td->pipe);
  1105. pipe_irq_disable(r8a66597, pipenum);
  1106. printk(KERN_ERR "r8a66597: in fifo not ready (%d)\n", pipenum);
  1107. finish_request(r8a66597, td, pipenum, td->urb, -EPIPE);
  1108. return;
  1109. }
  1110. /* prepare parameters */
  1111. rcv_len = tmp & DTLN;
  1112. if (usb_pipeisoc(urb->pipe)) {
  1113. buf = (u16 *)(urb->transfer_buffer +
  1114. urb->iso_frame_desc[td->iso_cnt].offset);
  1115. urb_len = urb->iso_frame_desc[td->iso_cnt].length;
  1116. } else {
  1117. buf = (void *)urb->transfer_buffer + urb->actual_length;
  1118. urb_len = urb->transfer_buffer_length - urb->actual_length;
  1119. }
  1120. bufsize = min(urb_len, (int) td->maxpacket);
  1121. if (rcv_len <= bufsize) {
  1122. size = rcv_len;
  1123. } else {
  1124. size = bufsize;
  1125. status = -EOVERFLOW;
  1126. finish = 1;
  1127. }
  1128. /* update parameters */
  1129. urb->actual_length += size;
  1130. if (rcv_len == 0)
  1131. td->zero_packet = 1;
  1132. if (rcv_len < bufsize) {
  1133. td->short_packet = 1;
  1134. }
  1135. if (usb_pipeisoc(urb->pipe)) {
  1136. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1137. urb->iso_frame_desc[td->iso_cnt].status = status;
  1138. td->iso_cnt++;
  1139. finish = 0;
  1140. }
  1141. /* check transfer finish */
  1142. if (finish || check_transfer_finish(td, urb)) {
  1143. pipe_stop(r8a66597, td->pipe);
  1144. pipe_irq_disable(r8a66597, pipenum);
  1145. finish = 1;
  1146. }
  1147. /* read fifo */
  1148. if (urb->transfer_buffer) {
  1149. if (size == 0)
  1150. r8a66597_write(r8a66597, BCLR, td->pipe->fifoctr);
  1151. else
  1152. r8a66597_read_fifo(r8a66597, td->pipe->fifoaddr,
  1153. buf, size);
  1154. }
  1155. if (finish && pipenum != 0)
  1156. finish_request(r8a66597, td, pipenum, urb, status);
  1157. }
  1158. static void packet_write(struct r8a66597 *r8a66597, u16 pipenum)
  1159. {
  1160. u16 tmp;
  1161. int bufsize, size;
  1162. u16 *buf;
  1163. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1164. struct urb *urb;
  1165. if (unlikely(!td))
  1166. return;
  1167. urb = td->urb;
  1168. fifo_change_from_pipe(r8a66597, td->pipe);
  1169. tmp = r8a66597_read(r8a66597, td->pipe->fifoctr);
  1170. if (unlikely((tmp & FRDY) == 0)) {
  1171. pipe_stop(r8a66597, td->pipe);
  1172. pipe_irq_disable(r8a66597, pipenum);
  1173. printk(KERN_ERR "r8a66597: out fifo not ready (%d)\n", pipenum);
  1174. finish_request(r8a66597, td, pipenum, urb, -EPIPE);
  1175. return;
  1176. }
  1177. /* prepare parameters */
  1178. bufsize = td->maxpacket;
  1179. if (usb_pipeisoc(urb->pipe)) {
  1180. buf = (u16 *)(urb->transfer_buffer +
  1181. urb->iso_frame_desc[td->iso_cnt].offset);
  1182. size = min(bufsize,
  1183. (int)urb->iso_frame_desc[td->iso_cnt].length);
  1184. } else {
  1185. buf = (u16 *)(urb->transfer_buffer + urb->actual_length);
  1186. size = min_t(u32, bufsize,
  1187. urb->transfer_buffer_length - urb->actual_length);
  1188. }
  1189. /* write fifo */
  1190. if (pipenum > 0)
  1191. r8a66597_write(r8a66597, ~(1 << pipenum), BEMPSTS);
  1192. if (urb->transfer_buffer) {
  1193. r8a66597_write_fifo(r8a66597, td->pipe, buf, size);
  1194. if (!usb_pipebulk(urb->pipe) || td->maxpacket != size)
  1195. r8a66597_write(r8a66597, BVAL, td->pipe->fifoctr);
  1196. }
  1197. /* update parameters */
  1198. urb->actual_length += size;
  1199. if (usb_pipeisoc(urb->pipe)) {
  1200. urb->iso_frame_desc[td->iso_cnt].actual_length = size;
  1201. urb->iso_frame_desc[td->iso_cnt].status = 0;
  1202. td->iso_cnt++;
  1203. }
  1204. /* check transfer finish */
  1205. if (check_transfer_finish(td, urb)) {
  1206. disable_irq_ready(r8a66597, pipenum);
  1207. enable_irq_empty(r8a66597, pipenum);
  1208. if (!usb_pipeisoc(urb->pipe))
  1209. enable_irq_nrdy(r8a66597, pipenum);
  1210. } else
  1211. pipe_irq_enable(r8a66597, urb, pipenum);
  1212. }
  1213. static void check_next_phase(struct r8a66597 *r8a66597, int status)
  1214. {
  1215. struct r8a66597_td *td = r8a66597_get_td(r8a66597, 0);
  1216. struct urb *urb;
  1217. u8 finish = 0;
  1218. if (unlikely(!td))
  1219. return;
  1220. urb = td->urb;
  1221. switch (td->type) {
  1222. case USB_PID_IN:
  1223. case USB_PID_OUT:
  1224. if (check_transfer_finish(td, urb))
  1225. td->type = USB_PID_ACK;
  1226. break;
  1227. case USB_PID_SETUP:
  1228. if (urb->transfer_buffer_length == urb->actual_length)
  1229. td->type = USB_PID_ACK;
  1230. else if (usb_pipeout(urb->pipe))
  1231. td->type = USB_PID_OUT;
  1232. else
  1233. td->type = USB_PID_IN;
  1234. break;
  1235. case USB_PID_ACK:
  1236. finish = 1;
  1237. break;
  1238. }
  1239. if (finish || status != 0 || urb->unlinked)
  1240. finish_request(r8a66597, td, 0, urb, status);
  1241. else
  1242. start_transfer(r8a66597, td);
  1243. }
  1244. static int get_urb_error(struct r8a66597 *r8a66597, u16 pipenum)
  1245. {
  1246. struct r8a66597_td *td = r8a66597_get_td(r8a66597, pipenum);
  1247. if (td) {
  1248. u16 pid = r8a66597_read(r8a66597, td->pipe->pipectr) & PID;
  1249. if (pid == PID_NAK)
  1250. return -ECONNRESET;
  1251. else
  1252. return -EPIPE;
  1253. }
  1254. return 0;
  1255. }
  1256. static void irq_pipe_ready(struct r8a66597 *r8a66597)
  1257. {
  1258. u16 check;
  1259. u16 pipenum;
  1260. u16 mask;
  1261. struct r8a66597_td *td;
  1262. mask = r8a66597_read(r8a66597, BRDYSTS)
  1263. & r8a66597_read(r8a66597, BRDYENB);
  1264. r8a66597_write(r8a66597, ~mask, BRDYSTS);
  1265. if (mask & BRDY0) {
  1266. td = r8a66597_get_td(r8a66597, 0);
  1267. if (td && td->type == USB_PID_IN)
  1268. packet_read(r8a66597, 0);
  1269. else
  1270. pipe_irq_disable(r8a66597, 0);
  1271. check_next_phase(r8a66597, 0);
  1272. }
  1273. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1274. check = 1 << pipenum;
  1275. if (mask & check) {
  1276. td = r8a66597_get_td(r8a66597, pipenum);
  1277. if (unlikely(!td))
  1278. continue;
  1279. if (td->type == USB_PID_IN)
  1280. packet_read(r8a66597, pipenum);
  1281. else if (td->type == USB_PID_OUT)
  1282. packet_write(r8a66597, pipenum);
  1283. }
  1284. }
  1285. }
  1286. static void irq_pipe_empty(struct r8a66597 *r8a66597)
  1287. {
  1288. u16 tmp;
  1289. u16 check;
  1290. u16 pipenum;
  1291. u16 mask;
  1292. struct r8a66597_td *td;
  1293. mask = r8a66597_read(r8a66597, BEMPSTS)
  1294. & r8a66597_read(r8a66597, BEMPENB);
  1295. r8a66597_write(r8a66597, ~mask, BEMPSTS);
  1296. if (mask & BEMP0) {
  1297. cfifo_change(r8a66597, 0);
  1298. td = r8a66597_get_td(r8a66597, 0);
  1299. if (td && td->type != USB_PID_OUT)
  1300. disable_irq_empty(r8a66597, 0);
  1301. check_next_phase(r8a66597, 0);
  1302. }
  1303. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1304. check = 1 << pipenum;
  1305. if (mask & check) {
  1306. struct r8a66597_td *td;
  1307. td = r8a66597_get_td(r8a66597, pipenum);
  1308. if (unlikely(!td))
  1309. continue;
  1310. tmp = r8a66597_read(r8a66597, td->pipe->pipectr);
  1311. if ((tmp & INBUFM) == 0) {
  1312. disable_irq_empty(r8a66597, pipenum);
  1313. pipe_irq_disable(r8a66597, pipenum);
  1314. finish_request(r8a66597, td, pipenum, td->urb,
  1315. 0);
  1316. }
  1317. }
  1318. }
  1319. }
  1320. static void irq_pipe_nrdy(struct r8a66597 *r8a66597)
  1321. {
  1322. u16 check;
  1323. u16 pipenum;
  1324. u16 mask;
  1325. int status;
  1326. mask = r8a66597_read(r8a66597, NRDYSTS)
  1327. & r8a66597_read(r8a66597, NRDYENB);
  1328. r8a66597_write(r8a66597, ~mask, NRDYSTS);
  1329. if (mask & NRDY0) {
  1330. cfifo_change(r8a66597, 0);
  1331. status = get_urb_error(r8a66597, 0);
  1332. pipe_irq_disable(r8a66597, 0);
  1333. check_next_phase(r8a66597, status);
  1334. }
  1335. for (pipenum = 1; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1336. check = 1 << pipenum;
  1337. if (mask & check) {
  1338. struct r8a66597_td *td;
  1339. td = r8a66597_get_td(r8a66597, pipenum);
  1340. if (unlikely(!td))
  1341. continue;
  1342. status = get_urb_error(r8a66597, pipenum);
  1343. pipe_irq_disable(r8a66597, pipenum);
  1344. pipe_stop(r8a66597, td->pipe);
  1345. finish_request(r8a66597, td, pipenum, td->urb, status);
  1346. }
  1347. }
  1348. }
  1349. static irqreturn_t r8a66597_irq(struct usb_hcd *hcd)
  1350. {
  1351. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1352. u16 intsts0, intsts1, intsts2;
  1353. u16 intenb0, intenb1, intenb2;
  1354. u16 mask0, mask1, mask2;
  1355. int status;
  1356. spin_lock(&r8a66597->lock);
  1357. intsts0 = r8a66597_read(r8a66597, INTSTS0);
  1358. intsts1 = r8a66597_read(r8a66597, INTSTS1);
  1359. intsts2 = r8a66597_read(r8a66597, INTSTS2);
  1360. intenb0 = r8a66597_read(r8a66597, INTENB0);
  1361. intenb1 = r8a66597_read(r8a66597, INTENB1);
  1362. intenb2 = r8a66597_read(r8a66597, INTENB2);
  1363. mask2 = intsts2 & intenb2;
  1364. mask1 = intsts1 & intenb1;
  1365. mask0 = intsts0 & intenb0 & (BEMP | NRDY | BRDY);
  1366. if (mask2) {
  1367. if (mask2 & ATTCH) {
  1368. r8a66597_write(r8a66597, ~ATTCH, INTSTS2);
  1369. r8a66597_bclr(r8a66597, ATTCHE, INTENB2);
  1370. /* start usb bus sampling */
  1371. start_root_hub_sampling(r8a66597, 1, 1);
  1372. }
  1373. if (mask2 & DTCH) {
  1374. r8a66597_write(r8a66597, ~DTCH, INTSTS2);
  1375. r8a66597_bclr(r8a66597, DTCHE, INTENB2);
  1376. r8a66597_usb_disconnect(r8a66597, 1);
  1377. }
  1378. if (mask2 & BCHG) {
  1379. r8a66597_write(r8a66597, ~BCHG, INTSTS2);
  1380. r8a66597_bclr(r8a66597, BCHGE, INTENB2);
  1381. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  1382. }
  1383. }
  1384. if (mask1) {
  1385. if (mask1 & ATTCH) {
  1386. r8a66597_write(r8a66597, ~ATTCH, INTSTS1);
  1387. r8a66597_bclr(r8a66597, ATTCHE, INTENB1);
  1388. /* start usb bus sampling */
  1389. start_root_hub_sampling(r8a66597, 0, 1);
  1390. }
  1391. if (mask1 & DTCH) {
  1392. r8a66597_write(r8a66597, ~DTCH, INTSTS1);
  1393. r8a66597_bclr(r8a66597, DTCHE, INTENB1);
  1394. r8a66597_usb_disconnect(r8a66597, 0);
  1395. }
  1396. if (mask1 & BCHG) {
  1397. r8a66597_write(r8a66597, ~BCHG, INTSTS1);
  1398. r8a66597_bclr(r8a66597, BCHGE, INTENB1);
  1399. usb_hcd_resume_root_hub(r8a66597_to_hcd(r8a66597));
  1400. }
  1401. if (mask1 & SIGN) {
  1402. r8a66597_write(r8a66597, ~SIGN, INTSTS1);
  1403. status = get_urb_error(r8a66597, 0);
  1404. check_next_phase(r8a66597, status);
  1405. }
  1406. if (mask1 & SACK) {
  1407. r8a66597_write(r8a66597, ~SACK, INTSTS1);
  1408. check_next_phase(r8a66597, 0);
  1409. }
  1410. }
  1411. if (mask0) {
  1412. if (mask0 & BRDY)
  1413. irq_pipe_ready(r8a66597);
  1414. if (mask0 & BEMP)
  1415. irq_pipe_empty(r8a66597);
  1416. if (mask0 & NRDY)
  1417. irq_pipe_nrdy(r8a66597);
  1418. }
  1419. spin_unlock(&r8a66597->lock);
  1420. return IRQ_HANDLED;
  1421. }
  1422. /* this function must be called with interrupt disabled */
  1423. static void r8a66597_root_hub_control(struct r8a66597 *r8a66597, int port)
  1424. {
  1425. u16 tmp;
  1426. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1427. if (rh->port & USB_PORT_STAT_RESET) {
  1428. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1429. tmp = r8a66597_read(r8a66597, dvstctr_reg);
  1430. if ((tmp & USBRST) == USBRST) {
  1431. r8a66597_mdfy(r8a66597, UACT, USBRST | UACT,
  1432. dvstctr_reg);
  1433. r8a66597_root_hub_start_polling(r8a66597);
  1434. } else
  1435. r8a66597_usb_connect(r8a66597, port);
  1436. }
  1437. if (!(rh->port & USB_PORT_STAT_CONNECTION)) {
  1438. r8a66597_write(r8a66597, ~ATTCH, get_intsts_reg(port));
  1439. r8a66597_bset(r8a66597, ATTCHE, get_intenb_reg(port));
  1440. }
  1441. if (rh->scount > 0) {
  1442. tmp = r8a66597_read(r8a66597, get_syssts_reg(port)) & LNST;
  1443. if (tmp == rh->old_syssts) {
  1444. rh->scount--;
  1445. if (rh->scount == 0)
  1446. r8a66597_check_syssts(r8a66597, port, tmp);
  1447. else
  1448. r8a66597_root_hub_start_polling(r8a66597);
  1449. } else {
  1450. rh->scount = R8A66597_MAX_SAMPLING;
  1451. rh->old_syssts = tmp;
  1452. r8a66597_root_hub_start_polling(r8a66597);
  1453. }
  1454. }
  1455. }
  1456. static void r8a66597_interval_timer(struct timer_list *t)
  1457. {
  1458. struct r8a66597_timers *timers = from_timer(timers, t, interval);
  1459. struct r8a66597 *r8a66597 = timers->r8a66597;
  1460. unsigned long flags;
  1461. u16 pipenum;
  1462. struct r8a66597_td *td;
  1463. spin_lock_irqsave(&r8a66597->lock, flags);
  1464. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1465. if (!(r8a66597->interval_map & (1 << pipenum)))
  1466. continue;
  1467. if (timer_pending(&r8a66597->timers[pipenum].interval))
  1468. continue;
  1469. td = r8a66597_get_td(r8a66597, pipenum);
  1470. if (td)
  1471. start_transfer(r8a66597, td);
  1472. }
  1473. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1474. }
  1475. static void r8a66597_td_timer(struct timer_list *t)
  1476. {
  1477. struct r8a66597_timers *timers = from_timer(timers, t, td);
  1478. struct r8a66597 *r8a66597 = timers->r8a66597;
  1479. unsigned long flags;
  1480. u16 pipenum;
  1481. struct r8a66597_td *td, *new_td = NULL;
  1482. struct r8a66597_pipe *pipe;
  1483. spin_lock_irqsave(&r8a66597->lock, flags);
  1484. for (pipenum = 0; pipenum < R8A66597_MAX_NUM_PIPE; pipenum++) {
  1485. if (!(r8a66597->timeout_map & (1 << pipenum)))
  1486. continue;
  1487. if (timer_pending(&r8a66597->timers[pipenum].td))
  1488. continue;
  1489. td = r8a66597_get_td(r8a66597, pipenum);
  1490. if (!td) {
  1491. r8a66597->timeout_map &= ~(1 << pipenum);
  1492. continue;
  1493. }
  1494. if (td->urb->actual_length) {
  1495. set_td_timer(r8a66597, td);
  1496. break;
  1497. }
  1498. pipe = td->pipe;
  1499. pipe_stop(r8a66597, pipe);
  1500. /* Select a different address or endpoint */
  1501. new_td = td;
  1502. do {
  1503. list_move_tail(&new_td->queue,
  1504. &r8a66597->pipe_queue[pipenum]);
  1505. new_td = r8a66597_get_td(r8a66597, pipenum);
  1506. if (!new_td) {
  1507. new_td = td;
  1508. break;
  1509. }
  1510. } while (td != new_td && td->address == new_td->address &&
  1511. td->pipe->info.epnum == new_td->pipe->info.epnum);
  1512. start_transfer(r8a66597, new_td);
  1513. if (td == new_td)
  1514. r8a66597->timeout_map &= ~(1 << pipenum);
  1515. else
  1516. set_td_timer(r8a66597, new_td);
  1517. break;
  1518. }
  1519. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1520. }
  1521. static void r8a66597_timer(struct timer_list *t)
  1522. {
  1523. struct r8a66597 *r8a66597 = from_timer(r8a66597, t, rh_timer);
  1524. unsigned long flags;
  1525. int port;
  1526. spin_lock_irqsave(&r8a66597->lock, flags);
  1527. for (port = 0; port < r8a66597->max_root_hub; port++)
  1528. r8a66597_root_hub_control(r8a66597, port);
  1529. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1530. }
  1531. static int check_pipe_config(struct r8a66597 *r8a66597, struct urb *urb)
  1532. {
  1533. struct r8a66597_device *dev = get_urb_to_r8a66597_dev(r8a66597, urb);
  1534. if (dev && dev->address && dev->state != USB_STATE_CONFIGURED &&
  1535. (urb->dev->state == USB_STATE_CONFIGURED))
  1536. return 1;
  1537. else
  1538. return 0;
  1539. }
  1540. static int r8a66597_start(struct usb_hcd *hcd)
  1541. {
  1542. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1543. hcd->state = HC_STATE_RUNNING;
  1544. return enable_controller(r8a66597);
  1545. }
  1546. static void r8a66597_stop(struct usb_hcd *hcd)
  1547. {
  1548. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1549. disable_controller(r8a66597);
  1550. }
  1551. static void set_address_zero(struct r8a66597 *r8a66597, struct urb *urb)
  1552. {
  1553. unsigned int usb_address = usb_pipedevice(urb->pipe);
  1554. u16 root_port, hub_port;
  1555. if (usb_address == 0) {
  1556. get_port_number(r8a66597, urb->dev->devpath,
  1557. &root_port, &hub_port);
  1558. set_devadd_reg(r8a66597, 0,
  1559. get_r8a66597_usb_speed(urb->dev->speed),
  1560. get_parent_r8a66597_address(r8a66597, urb->dev),
  1561. hub_port, root_port);
  1562. }
  1563. }
  1564. static struct r8a66597_td *r8a66597_make_td(struct r8a66597 *r8a66597,
  1565. struct urb *urb,
  1566. struct usb_host_endpoint *hep)
  1567. {
  1568. struct r8a66597_td *td;
  1569. u16 pipenum;
  1570. td = kzalloc(sizeof(struct r8a66597_td), GFP_ATOMIC);
  1571. if (td == NULL)
  1572. return NULL;
  1573. pipenum = r8a66597_get_pipenum(urb, hep);
  1574. td->pipenum = pipenum;
  1575. td->pipe = hep->hcpriv;
  1576. td->urb = urb;
  1577. td->address = get_urb_to_r8a66597_addr(r8a66597, urb);
  1578. td->maxpacket = usb_maxpacket(urb->dev, urb->pipe,
  1579. !usb_pipein(urb->pipe));
  1580. if (usb_pipecontrol(urb->pipe))
  1581. td->type = USB_PID_SETUP;
  1582. else if (usb_pipein(urb->pipe))
  1583. td->type = USB_PID_IN;
  1584. else
  1585. td->type = USB_PID_OUT;
  1586. INIT_LIST_HEAD(&td->queue);
  1587. return td;
  1588. }
  1589. static int r8a66597_urb_enqueue(struct usb_hcd *hcd,
  1590. struct urb *urb,
  1591. gfp_t mem_flags)
  1592. {
  1593. struct usb_host_endpoint *hep = urb->ep;
  1594. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1595. struct r8a66597_td *td = NULL;
  1596. int ret, request = 0;
  1597. unsigned long flags;
  1598. spin_lock_irqsave(&r8a66597->lock, flags);
  1599. if (!get_urb_to_r8a66597_dev(r8a66597, urb)) {
  1600. ret = -ENODEV;
  1601. goto error_not_linked;
  1602. }
  1603. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  1604. if (ret)
  1605. goto error_not_linked;
  1606. if (!hep->hcpriv) {
  1607. hep->hcpriv = kzalloc(sizeof(struct r8a66597_pipe),
  1608. GFP_ATOMIC);
  1609. if (!hep->hcpriv) {
  1610. ret = -ENOMEM;
  1611. goto error;
  1612. }
  1613. set_pipe_reg_addr(hep->hcpriv, R8A66597_PIPE_NO_DMA);
  1614. if (usb_pipeendpoint(urb->pipe))
  1615. init_pipe_info(r8a66597, urb, hep, &hep->desc);
  1616. }
  1617. if (unlikely(check_pipe_config(r8a66597, urb)))
  1618. init_pipe_config(r8a66597, urb);
  1619. set_address_zero(r8a66597, urb);
  1620. td = r8a66597_make_td(r8a66597, urb, hep);
  1621. if (td == NULL) {
  1622. ret = -ENOMEM;
  1623. goto error;
  1624. }
  1625. if (list_empty(&r8a66597->pipe_queue[td->pipenum]))
  1626. request = 1;
  1627. list_add_tail(&td->queue, &r8a66597->pipe_queue[td->pipenum]);
  1628. urb->hcpriv = td;
  1629. if (request) {
  1630. if (td->pipe->info.timer_interval) {
  1631. r8a66597->interval_map |= 1 << td->pipenum;
  1632. mod_timer(&r8a66597->timers[td->pipenum].interval,
  1633. jiffies + msecs_to_jiffies(
  1634. td->pipe->info.timer_interval));
  1635. } else {
  1636. ret = start_transfer(r8a66597, td);
  1637. if (ret < 0) {
  1638. list_del(&td->queue);
  1639. kfree(td);
  1640. }
  1641. }
  1642. } else
  1643. set_td_timer(r8a66597, td);
  1644. error:
  1645. if (ret)
  1646. usb_hcd_unlink_urb_from_ep(hcd, urb);
  1647. error_not_linked:
  1648. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1649. return ret;
  1650. }
  1651. static int r8a66597_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
  1652. int status)
  1653. {
  1654. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1655. struct r8a66597_td *td;
  1656. unsigned long flags;
  1657. int rc;
  1658. spin_lock_irqsave(&r8a66597->lock, flags);
  1659. rc = usb_hcd_check_unlink_urb(hcd, urb, status);
  1660. if (rc)
  1661. goto done;
  1662. if (urb->hcpriv) {
  1663. td = urb->hcpriv;
  1664. pipe_stop(r8a66597, td->pipe);
  1665. pipe_irq_disable(r8a66597, td->pipenum);
  1666. disable_irq_empty(r8a66597, td->pipenum);
  1667. finish_request(r8a66597, td, td->pipenum, urb, status);
  1668. }
  1669. done:
  1670. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1671. return rc;
  1672. }
  1673. static void r8a66597_endpoint_disable(struct usb_hcd *hcd,
  1674. struct usb_host_endpoint *hep)
  1675. __acquires(r8a66597->lock)
  1676. __releases(r8a66597->lock)
  1677. {
  1678. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1679. struct r8a66597_pipe *pipe = (struct r8a66597_pipe *)hep->hcpriv;
  1680. struct r8a66597_td *td;
  1681. struct urb *urb = NULL;
  1682. u16 pipenum;
  1683. unsigned long flags;
  1684. if (pipe == NULL)
  1685. return;
  1686. pipenum = pipe->info.pipenum;
  1687. spin_lock_irqsave(&r8a66597->lock, flags);
  1688. if (pipenum == 0) {
  1689. kfree(hep->hcpriv);
  1690. hep->hcpriv = NULL;
  1691. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1692. return;
  1693. }
  1694. pipe_stop(r8a66597, pipe);
  1695. pipe_irq_disable(r8a66597, pipenum);
  1696. disable_irq_empty(r8a66597, pipenum);
  1697. td = r8a66597_get_td(r8a66597, pipenum);
  1698. if (td)
  1699. urb = td->urb;
  1700. finish_request(r8a66597, td, pipenum, urb, -ESHUTDOWN);
  1701. kfree(hep->hcpriv);
  1702. hep->hcpriv = NULL;
  1703. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1704. }
  1705. static int r8a66597_get_frame(struct usb_hcd *hcd)
  1706. {
  1707. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1708. return r8a66597_read(r8a66597, FRMNUM) & 0x03FF;
  1709. }
  1710. static void collect_usb_address_map(struct usb_device *udev, unsigned long *map)
  1711. {
  1712. int chix;
  1713. struct usb_device *childdev;
  1714. if (udev->state == USB_STATE_CONFIGURED &&
  1715. udev->parent && udev->parent->devnum > 1 &&
  1716. udev->parent->descriptor.bDeviceClass == USB_CLASS_HUB)
  1717. map[udev->devnum/32] |= (1 << (udev->devnum % 32));
  1718. usb_hub_for_each_child(udev, chix, childdev)
  1719. collect_usb_address_map(childdev, map);
  1720. }
  1721. /* this function must be called with interrupt disabled */
  1722. static struct r8a66597_device *get_r8a66597_device(struct r8a66597 *r8a66597,
  1723. int addr)
  1724. {
  1725. struct r8a66597_device *dev;
  1726. struct list_head *list = &r8a66597->child_device;
  1727. list_for_each_entry(dev, list, device_list) {
  1728. if (dev->usb_address != addr)
  1729. continue;
  1730. return dev;
  1731. }
  1732. printk(KERN_ERR "r8a66597: get_r8a66597_device fail.(%d)\n", addr);
  1733. return NULL;
  1734. }
  1735. static void update_usb_address_map(struct r8a66597 *r8a66597,
  1736. struct usb_device *root_hub,
  1737. unsigned long *map)
  1738. {
  1739. int i, j, addr;
  1740. unsigned long diff;
  1741. unsigned long flags;
  1742. for (i = 0; i < 4; i++) {
  1743. diff = r8a66597->child_connect_map[i] ^ map[i];
  1744. if (!diff)
  1745. continue;
  1746. for (j = 0; j < 32; j++) {
  1747. if (!(diff & (1 << j)))
  1748. continue;
  1749. addr = i * 32 + j;
  1750. if (map[i] & (1 << j))
  1751. set_child_connect_map(r8a66597, addr);
  1752. else {
  1753. struct r8a66597_device *dev;
  1754. spin_lock_irqsave(&r8a66597->lock, flags);
  1755. dev = get_r8a66597_device(r8a66597, addr);
  1756. disable_r8a66597_pipe_all(r8a66597, dev);
  1757. free_usb_address(r8a66597, dev, 0);
  1758. put_child_connect_map(r8a66597, addr);
  1759. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1760. }
  1761. }
  1762. }
  1763. }
  1764. static void r8a66597_check_detect_child(struct r8a66597 *r8a66597,
  1765. struct usb_hcd *hcd)
  1766. {
  1767. struct usb_bus *bus;
  1768. unsigned long now_map[4];
  1769. memset(now_map, 0, sizeof(now_map));
  1770. mutex_lock(&usb_bus_idr_lock);
  1771. bus = idr_find(&usb_bus_idr, hcd->self.busnum);
  1772. if (bus && bus->root_hub) {
  1773. collect_usb_address_map(bus->root_hub, now_map);
  1774. update_usb_address_map(r8a66597, bus->root_hub, now_map);
  1775. }
  1776. mutex_unlock(&usb_bus_idr_lock);
  1777. }
  1778. static int r8a66597_hub_status_data(struct usb_hcd *hcd, char *buf)
  1779. {
  1780. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1781. unsigned long flags;
  1782. int i;
  1783. r8a66597_check_detect_child(r8a66597, hcd);
  1784. spin_lock_irqsave(&r8a66597->lock, flags);
  1785. *buf = 0; /* initialize (no change) */
  1786. for (i = 0; i < r8a66597->max_root_hub; i++) {
  1787. if (r8a66597->root_hub[i].port & 0xffff0000)
  1788. *buf |= 1 << (i + 1);
  1789. }
  1790. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1791. return (*buf != 0);
  1792. }
  1793. static void r8a66597_hub_descriptor(struct r8a66597 *r8a66597,
  1794. struct usb_hub_descriptor *desc)
  1795. {
  1796. desc->bDescriptorType = USB_DT_HUB;
  1797. desc->bHubContrCurrent = 0;
  1798. desc->bNbrPorts = r8a66597->max_root_hub;
  1799. desc->bDescLength = 9;
  1800. desc->bPwrOn2PwrGood = 0;
  1801. desc->wHubCharacteristics =
  1802. cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_NO_OCPM);
  1803. desc->u.hs.DeviceRemovable[0] =
  1804. ((1 << r8a66597->max_root_hub) - 1) << 1;
  1805. desc->u.hs.DeviceRemovable[1] = ~0;
  1806. }
  1807. static int r8a66597_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
  1808. u16 wIndex, char *buf, u16 wLength)
  1809. {
  1810. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1811. int ret;
  1812. int port = (wIndex & 0x00FF) - 1;
  1813. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1814. unsigned long flags;
  1815. ret = 0;
  1816. spin_lock_irqsave(&r8a66597->lock, flags);
  1817. switch (typeReq) {
  1818. case ClearHubFeature:
  1819. case SetHubFeature:
  1820. switch (wValue) {
  1821. case C_HUB_OVER_CURRENT:
  1822. case C_HUB_LOCAL_POWER:
  1823. break;
  1824. default:
  1825. goto error;
  1826. }
  1827. break;
  1828. case ClearPortFeature:
  1829. if (wIndex > r8a66597->max_root_hub)
  1830. goto error;
  1831. if (wLength != 0)
  1832. goto error;
  1833. switch (wValue) {
  1834. case USB_PORT_FEAT_ENABLE:
  1835. rh->port &= ~USB_PORT_STAT_POWER;
  1836. break;
  1837. case USB_PORT_FEAT_SUSPEND:
  1838. break;
  1839. case USB_PORT_FEAT_POWER:
  1840. r8a66597_port_power(r8a66597, port, 0);
  1841. break;
  1842. case USB_PORT_FEAT_C_ENABLE:
  1843. case USB_PORT_FEAT_C_SUSPEND:
  1844. case USB_PORT_FEAT_C_CONNECTION:
  1845. case USB_PORT_FEAT_C_OVER_CURRENT:
  1846. case USB_PORT_FEAT_C_RESET:
  1847. break;
  1848. default:
  1849. goto error;
  1850. }
  1851. rh->port &= ~(1 << wValue);
  1852. break;
  1853. case GetHubDescriptor:
  1854. r8a66597_hub_descriptor(r8a66597,
  1855. (struct usb_hub_descriptor *)buf);
  1856. break;
  1857. case GetHubStatus:
  1858. *buf = 0x00;
  1859. break;
  1860. case GetPortStatus:
  1861. if (wIndex > r8a66597->max_root_hub)
  1862. goto error;
  1863. *(__le32 *)buf = cpu_to_le32(rh->port);
  1864. break;
  1865. case SetPortFeature:
  1866. if (wIndex > r8a66597->max_root_hub)
  1867. goto error;
  1868. if (wLength != 0)
  1869. goto error;
  1870. switch (wValue) {
  1871. case USB_PORT_FEAT_SUSPEND:
  1872. break;
  1873. case USB_PORT_FEAT_POWER:
  1874. r8a66597_port_power(r8a66597, port, 1);
  1875. rh->port |= USB_PORT_STAT_POWER;
  1876. break;
  1877. case USB_PORT_FEAT_RESET: {
  1878. struct r8a66597_device *dev = rh->dev;
  1879. rh->port |= USB_PORT_STAT_RESET;
  1880. disable_r8a66597_pipe_all(r8a66597, dev);
  1881. free_usb_address(r8a66597, dev, 1);
  1882. r8a66597_mdfy(r8a66597, USBRST, USBRST | UACT,
  1883. get_dvstctr_reg(port));
  1884. mod_timer(&r8a66597->rh_timer,
  1885. jiffies + msecs_to_jiffies(50));
  1886. }
  1887. break;
  1888. default:
  1889. goto error;
  1890. }
  1891. rh->port |= 1 << wValue;
  1892. break;
  1893. default:
  1894. error:
  1895. ret = -EPIPE;
  1896. break;
  1897. }
  1898. spin_unlock_irqrestore(&r8a66597->lock, flags);
  1899. return ret;
  1900. }
  1901. #if defined(CONFIG_PM)
  1902. static int r8a66597_bus_suspend(struct usb_hcd *hcd)
  1903. {
  1904. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1905. int port;
  1906. dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
  1907. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1908. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1909. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1910. if (!(rh->port & USB_PORT_STAT_ENABLE))
  1911. continue;
  1912. dev_dbg(&rh->dev->udev->dev, "suspend port = %d\n", port);
  1913. r8a66597_bclr(r8a66597, UACT, dvstctr_reg); /* suspend */
  1914. rh->port |= USB_PORT_STAT_SUSPEND;
  1915. if (rh->dev->udev->do_remote_wakeup) {
  1916. msleep(3); /* waiting last SOF */
  1917. r8a66597_bset(r8a66597, RWUPE, dvstctr_reg);
  1918. r8a66597_write(r8a66597, ~BCHG, get_intsts_reg(port));
  1919. r8a66597_bset(r8a66597, BCHGE, get_intenb_reg(port));
  1920. }
  1921. }
  1922. r8a66597->bus_suspended = 1;
  1923. return 0;
  1924. }
  1925. static int r8a66597_bus_resume(struct usb_hcd *hcd)
  1926. {
  1927. struct r8a66597 *r8a66597 = hcd_to_r8a66597(hcd);
  1928. int port;
  1929. dev_dbg(&r8a66597->device0.udev->dev, "%s\n", __func__);
  1930. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1931. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1932. unsigned long dvstctr_reg = get_dvstctr_reg(port);
  1933. if (!(rh->port & USB_PORT_STAT_SUSPEND))
  1934. continue;
  1935. dev_dbg(&rh->dev->udev->dev, "resume port = %d\n", port);
  1936. rh->port &= ~USB_PORT_STAT_SUSPEND;
  1937. rh->port |= USB_PORT_STAT_C_SUSPEND << 16;
  1938. r8a66597_mdfy(r8a66597, RESUME, RESUME | UACT, dvstctr_reg);
  1939. msleep(USB_RESUME_TIMEOUT);
  1940. r8a66597_mdfy(r8a66597, UACT, RESUME | UACT, dvstctr_reg);
  1941. }
  1942. return 0;
  1943. }
  1944. #else
  1945. #define r8a66597_bus_suspend NULL
  1946. #define r8a66597_bus_resume NULL
  1947. #endif
  1948. static const struct hc_driver r8a66597_hc_driver = {
  1949. .description = hcd_name,
  1950. .hcd_priv_size = sizeof(struct r8a66597),
  1951. .irq = r8a66597_irq,
  1952. /*
  1953. * generic hardware linkage
  1954. */
  1955. .flags = HCD_USB2,
  1956. .start = r8a66597_start,
  1957. .stop = r8a66597_stop,
  1958. /*
  1959. * managing i/o requests and associated device resources
  1960. */
  1961. .urb_enqueue = r8a66597_urb_enqueue,
  1962. .urb_dequeue = r8a66597_urb_dequeue,
  1963. .endpoint_disable = r8a66597_endpoint_disable,
  1964. /*
  1965. * periodic schedule support
  1966. */
  1967. .get_frame_number = r8a66597_get_frame,
  1968. /*
  1969. * root hub support
  1970. */
  1971. .hub_status_data = r8a66597_hub_status_data,
  1972. .hub_control = r8a66597_hub_control,
  1973. .bus_suspend = r8a66597_bus_suspend,
  1974. .bus_resume = r8a66597_bus_resume,
  1975. };
  1976. #if defined(CONFIG_PM)
  1977. static int r8a66597_suspend(struct device *dev)
  1978. {
  1979. struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
  1980. int port;
  1981. dev_dbg(dev, "%s\n", __func__);
  1982. disable_controller(r8a66597);
  1983. for (port = 0; port < r8a66597->max_root_hub; port++) {
  1984. struct r8a66597_root_hub *rh = &r8a66597->root_hub[port];
  1985. rh->port = 0x00000000;
  1986. }
  1987. return 0;
  1988. }
  1989. static int r8a66597_resume(struct device *dev)
  1990. {
  1991. struct r8a66597 *r8a66597 = dev_get_drvdata(dev);
  1992. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  1993. dev_dbg(dev, "%s\n", __func__);
  1994. enable_controller(r8a66597);
  1995. usb_root_hub_lost_power(hcd->self.root_hub);
  1996. return 0;
  1997. }
  1998. static const struct dev_pm_ops r8a66597_dev_pm_ops = {
  1999. .suspend = r8a66597_suspend,
  2000. .resume = r8a66597_resume,
  2001. .poweroff = r8a66597_suspend,
  2002. .restore = r8a66597_resume,
  2003. };
  2004. #define R8A66597_DEV_PM_OPS (&r8a66597_dev_pm_ops)
  2005. #else /* if defined(CONFIG_PM) */
  2006. #define R8A66597_DEV_PM_OPS NULL
  2007. #endif
  2008. static int r8a66597_remove(struct platform_device *pdev)
  2009. {
  2010. struct r8a66597 *r8a66597 = platform_get_drvdata(pdev);
  2011. struct usb_hcd *hcd = r8a66597_to_hcd(r8a66597);
  2012. del_timer_sync(&r8a66597->rh_timer);
  2013. usb_remove_hcd(hcd);
  2014. iounmap(r8a66597->reg);
  2015. if (r8a66597->pdata->on_chip)
  2016. clk_put(r8a66597->clk);
  2017. usb_put_hcd(hcd);
  2018. return 0;
  2019. }
  2020. static int r8a66597_probe(struct platform_device *pdev)
  2021. {
  2022. char clk_name[8];
  2023. struct resource *res = NULL, *ires;
  2024. int irq = -1;
  2025. void __iomem *reg = NULL;
  2026. struct usb_hcd *hcd = NULL;
  2027. struct r8a66597 *r8a66597;
  2028. int ret = 0;
  2029. int i;
  2030. unsigned long irq_trigger;
  2031. if (usb_disabled())
  2032. return -ENODEV;
  2033. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2034. if (!res) {
  2035. ret = -ENODEV;
  2036. dev_err(&pdev->dev, "platform_get_resource error.\n");
  2037. goto clean_up;
  2038. }
  2039. ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2040. if (!ires) {
  2041. ret = -ENODEV;
  2042. dev_err(&pdev->dev,
  2043. "platform_get_resource IORESOURCE_IRQ error.\n");
  2044. goto clean_up;
  2045. }
  2046. irq = ires->start;
  2047. irq_trigger = ires->flags & IRQF_TRIGGER_MASK;
  2048. reg = ioremap(res->start, resource_size(res));
  2049. if (reg == NULL) {
  2050. ret = -ENOMEM;
  2051. dev_err(&pdev->dev, "ioremap error.\n");
  2052. goto clean_up;
  2053. }
  2054. if (pdev->dev.platform_data == NULL) {
  2055. dev_err(&pdev->dev, "no platform data\n");
  2056. ret = -ENODEV;
  2057. goto clean_up;
  2058. }
  2059. /* initialize hcd */
  2060. hcd = usb_create_hcd(&r8a66597_hc_driver, &pdev->dev, (char *)hcd_name);
  2061. if (!hcd) {
  2062. ret = -ENOMEM;
  2063. dev_err(&pdev->dev, "Failed to create hcd\n");
  2064. goto clean_up;
  2065. }
  2066. r8a66597 = hcd_to_r8a66597(hcd);
  2067. memset(r8a66597, 0, sizeof(struct r8a66597));
  2068. platform_set_drvdata(pdev, r8a66597);
  2069. r8a66597->pdata = dev_get_platdata(&pdev->dev);
  2070. r8a66597->irq_sense_low = irq_trigger == IRQF_TRIGGER_LOW;
  2071. if (r8a66597->pdata->on_chip) {
  2072. snprintf(clk_name, sizeof(clk_name), "usb%d", pdev->id);
  2073. r8a66597->clk = clk_get(&pdev->dev, clk_name);
  2074. if (IS_ERR(r8a66597->clk)) {
  2075. dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
  2076. clk_name);
  2077. ret = PTR_ERR(r8a66597->clk);
  2078. goto clean_up2;
  2079. }
  2080. r8a66597->max_root_hub = 1;
  2081. } else
  2082. r8a66597->max_root_hub = 2;
  2083. spin_lock_init(&r8a66597->lock);
  2084. timer_setup(&r8a66597->rh_timer, r8a66597_timer, 0);
  2085. r8a66597->reg = reg;
  2086. /* make sure no interrupts are pending */
  2087. ret = r8a66597_clock_enable(r8a66597);
  2088. if (ret < 0)
  2089. goto clean_up3;
  2090. disable_controller(r8a66597);
  2091. for (i = 0; i < R8A66597_MAX_NUM_PIPE; i++) {
  2092. INIT_LIST_HEAD(&r8a66597->pipe_queue[i]);
  2093. r8a66597->timers[i].r8a66597 = r8a66597;
  2094. timer_setup(&r8a66597->timers[i].td, r8a66597_td_timer, 0);
  2095. timer_setup(&r8a66597->timers[i].interval,
  2096. r8a66597_interval_timer, 0);
  2097. }
  2098. INIT_LIST_HEAD(&r8a66597->child_device);
  2099. hcd->rsrc_start = res->start;
  2100. hcd->has_tt = 1;
  2101. ret = usb_add_hcd(hcd, irq, irq_trigger);
  2102. if (ret != 0) {
  2103. dev_err(&pdev->dev, "Failed to add hcd\n");
  2104. goto clean_up3;
  2105. }
  2106. device_wakeup_enable(hcd->self.controller);
  2107. return 0;
  2108. clean_up3:
  2109. if (r8a66597->pdata->on_chip)
  2110. clk_put(r8a66597->clk);
  2111. clean_up2:
  2112. usb_put_hcd(hcd);
  2113. clean_up:
  2114. if (reg)
  2115. iounmap(reg);
  2116. return ret;
  2117. }
  2118. static struct platform_driver r8a66597_driver = {
  2119. .probe = r8a66597_probe,
  2120. .remove = r8a66597_remove,
  2121. .driver = {
  2122. .name = hcd_name,
  2123. .pm = R8A66597_DEV_PM_OPS,
  2124. },
  2125. };
  2126. module_platform_driver(r8a66597_driver);