r8a66597-hcd.c 62 KB

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