r8a66597-hcd.c 57 KB

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