fhci-hcd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. /*
  2. * Freescale QUICC Engine USB Host Controller Driver
  3. *
  4. * Copyright (c) Freescale Semicondutor, Inc. 2006.
  5. * Shlomi Gridish <gridish@freescale.com>
  6. * Jerry Huang <Chang-Ming.Huang@freescale.com>
  7. * Copyright (c) Logic Product Development, Inc. 2007
  8. * Peter Barada <peterb@logicpd.com>
  9. * Copyright (c) MontaVista Software, Inc. 2008.
  10. * Anton Vorontsov <avorontsov@ru.mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/errno.h>
  23. #include <linux/list.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/io.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/hcd.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/slab.h>
  31. #include <asm/qe.h>
  32. #include <asm/fsl_gtm.h>
  33. #include "fhci.h"
  34. void fhci_start_sof_timer(struct fhci_hcd *fhci)
  35. {
  36. fhci_dbg(fhci, "-> %s\n", __func__);
  37. /* clear frame_n */
  38. out_be16(&fhci->pram->frame_num, 0);
  39. out_be16(&fhci->regs->usb_sof_tmr, 0);
  40. setbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
  41. fhci_dbg(fhci, "<- %s\n", __func__);
  42. }
  43. void fhci_stop_sof_timer(struct fhci_hcd *fhci)
  44. {
  45. fhci_dbg(fhci, "-> %s\n", __func__);
  46. clrbits8(&fhci->regs->usb_mod, USB_MODE_SFTE);
  47. gtm_stop_timer16(fhci->timer);
  48. fhci_dbg(fhci, "<- %s\n", __func__);
  49. }
  50. u16 fhci_get_sof_timer_count(struct fhci_usb *usb)
  51. {
  52. return be16_to_cpu(in_be16(&usb->fhci->regs->usb_sof_tmr) / 12);
  53. }
  54. /* initialize the endpoint zero */
  55. static u32 endpoint_zero_init(struct fhci_usb *usb,
  56. enum fhci_mem_alloc data_mem,
  57. u32 ring_len)
  58. {
  59. u32 rc;
  60. rc = fhci_create_ep(usb, data_mem, ring_len);
  61. if (rc)
  62. return rc;
  63. /* inilialize endpoint registers */
  64. fhci_init_ep_registers(usb, usb->ep0, data_mem);
  65. return 0;
  66. }
  67. /* enable the USB interrupts */
  68. void fhci_usb_enable_interrupt(struct fhci_usb *usb)
  69. {
  70. struct fhci_hcd *fhci = usb->fhci;
  71. if (usb->intr_nesting_cnt == 1) {
  72. /* initialize the USB interrupt */
  73. enable_irq(fhci_to_hcd(fhci)->irq);
  74. /* initialize the event register and mask register */
  75. out_be16(&usb->fhci->regs->usb_event, 0xffff);
  76. out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
  77. /* enable the timer interrupts */
  78. enable_irq(fhci->timer->irq);
  79. } else if (usb->intr_nesting_cnt > 1)
  80. fhci_info(fhci, "unbalanced USB interrupts nesting\n");
  81. usb->intr_nesting_cnt--;
  82. }
  83. /* disable the usb interrupt */
  84. void fhci_usb_disable_interrupt(struct fhci_usb *usb)
  85. {
  86. struct fhci_hcd *fhci = usb->fhci;
  87. if (usb->intr_nesting_cnt == 0) {
  88. /* disable the timer interrupt */
  89. disable_irq_nosync(fhci->timer->irq);
  90. /* disable the usb interrupt */
  91. disable_irq_nosync(fhci_to_hcd(fhci)->irq);
  92. out_be16(&usb->fhci->regs->usb_mask, 0);
  93. }
  94. usb->intr_nesting_cnt++;
  95. }
  96. /* enable the USB controller */
  97. static u32 fhci_usb_enable(struct fhci_hcd *fhci)
  98. {
  99. struct fhci_usb *usb = fhci->usb_lld;
  100. out_be16(&usb->fhci->regs->usb_event, 0xffff);
  101. out_be16(&usb->fhci->regs->usb_mask, usb->saved_msk);
  102. setbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
  103. mdelay(100);
  104. return 0;
  105. }
  106. /* disable the USB controller */
  107. static u32 fhci_usb_disable(struct fhci_hcd *fhci)
  108. {
  109. struct fhci_usb *usb = fhci->usb_lld;
  110. fhci_usb_disable_interrupt(usb);
  111. fhci_port_disable(fhci);
  112. /* disable the usb controller */
  113. if (usb->port_status == FHCI_PORT_FULL ||
  114. usb->port_status == FHCI_PORT_LOW)
  115. fhci_device_disconnected_interrupt(fhci);
  116. clrbits8(&usb->fhci->regs->usb_mod, USB_MODE_EN);
  117. return 0;
  118. }
  119. /* check the bus state by polling the QE bit on the IO ports */
  120. int fhci_ioports_check_bus_state(struct fhci_hcd *fhci)
  121. {
  122. u8 bits = 0;
  123. /* check USBOE,if transmitting,exit */
  124. if (!gpio_get_value(fhci->gpios[GPIO_USBOE]))
  125. return -1;
  126. /* check USBRP */
  127. if (gpio_get_value(fhci->gpios[GPIO_USBRP]))
  128. bits |= 0x2;
  129. /* check USBRN */
  130. if (gpio_get_value(fhci->gpios[GPIO_USBRN]))
  131. bits |= 0x1;
  132. return bits;
  133. }
  134. static void fhci_mem_free(struct fhci_hcd *fhci)
  135. {
  136. struct ed *ed;
  137. struct ed *next_ed;
  138. struct td *td;
  139. struct td *next_td;
  140. list_for_each_entry_safe(ed, next_ed, &fhci->empty_eds, node) {
  141. list_del(&ed->node);
  142. kfree(ed);
  143. }
  144. list_for_each_entry_safe(td, next_td, &fhci->empty_tds, node) {
  145. list_del(&td->node);
  146. kfree(td);
  147. }
  148. kfree(fhci->vroot_hub);
  149. fhci->vroot_hub = NULL;
  150. kfree(fhci->hc_list);
  151. fhci->hc_list = NULL;
  152. }
  153. static int fhci_mem_init(struct fhci_hcd *fhci)
  154. {
  155. int i;
  156. fhci->hc_list = kzalloc(sizeof(*fhci->hc_list), GFP_KERNEL);
  157. if (!fhci->hc_list)
  158. goto err;
  159. INIT_LIST_HEAD(&fhci->hc_list->ctrl_list);
  160. INIT_LIST_HEAD(&fhci->hc_list->bulk_list);
  161. INIT_LIST_HEAD(&fhci->hc_list->iso_list);
  162. INIT_LIST_HEAD(&fhci->hc_list->intr_list);
  163. INIT_LIST_HEAD(&fhci->hc_list->done_list);
  164. fhci->vroot_hub = kzalloc(sizeof(*fhci->vroot_hub), GFP_KERNEL);
  165. if (!fhci->vroot_hub)
  166. goto err;
  167. INIT_LIST_HEAD(&fhci->empty_eds);
  168. INIT_LIST_HEAD(&fhci->empty_tds);
  169. /* initialize work queue to handle done list */
  170. fhci_tasklet.data = (unsigned long)fhci;
  171. fhci->process_done_task = &fhci_tasklet;
  172. for (i = 0; i < MAX_TDS; i++) {
  173. struct td *td;
  174. td = kmalloc(sizeof(*td), GFP_KERNEL);
  175. if (!td)
  176. goto err;
  177. fhci_recycle_empty_td(fhci, td);
  178. }
  179. for (i = 0; i < MAX_EDS; i++) {
  180. struct ed *ed;
  181. ed = kmalloc(sizeof(*ed), GFP_KERNEL);
  182. if (!ed)
  183. goto err;
  184. fhci_recycle_empty_ed(fhci, ed);
  185. }
  186. fhci->active_urbs = 0;
  187. return 0;
  188. err:
  189. fhci_mem_free(fhci);
  190. return -ENOMEM;
  191. }
  192. /* destroy the fhci_usb structure */
  193. static void fhci_usb_free(void *lld)
  194. {
  195. struct fhci_usb *usb = lld;
  196. struct fhci_hcd *fhci;
  197. if (usb) {
  198. fhci = usb->fhci;
  199. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  200. fhci_ep0_free(usb);
  201. kfree(usb->actual_frame);
  202. kfree(usb);
  203. }
  204. }
  205. /* initialize the USB */
  206. static int fhci_usb_init(struct fhci_hcd *fhci)
  207. {
  208. struct fhci_usb *usb = fhci->usb_lld;
  209. memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
  210. usb->port_status = FHCI_PORT_DISABLED;
  211. usb->max_frame_usage = FRAME_TIME_USAGE;
  212. usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
  213. usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
  214. if (!usb->actual_frame) {
  215. fhci_usb_free(usb);
  216. return -ENOMEM;
  217. }
  218. INIT_LIST_HEAD(&usb->actual_frame->tds_list);
  219. /* initializing registers on chip, clear frame number */
  220. out_be16(&fhci->pram->frame_num, 0);
  221. /* clear rx state */
  222. out_be32(&fhci->pram->rx_state, 0);
  223. /* set mask register */
  224. usb->saved_msk = (USB_E_TXB_MASK |
  225. USB_E_TXE1_MASK |
  226. USB_E_IDLE_MASK |
  227. USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
  228. out_8(&usb->fhci->regs->usb_mod, USB_MODE_HOST | USB_MODE_EN);
  229. /* clearing the mask register */
  230. out_be16(&usb->fhci->regs->usb_mask, 0);
  231. /* initialing the event register */
  232. out_be16(&usb->fhci->regs->usb_event, 0xffff);
  233. if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
  234. fhci_usb_free(usb);
  235. return -EINVAL;
  236. }
  237. return 0;
  238. }
  239. /* initialize the fhci_usb struct and the corresponding data staruct */
  240. static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
  241. {
  242. struct fhci_usb *usb;
  243. /* allocate memory for SCC data structure */
  244. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  245. if (!usb) {
  246. fhci_err(fhci, "no memory for SCC data struct\n");
  247. return NULL;
  248. }
  249. usb->fhci = fhci;
  250. usb->hc_list = fhci->hc_list;
  251. usb->vroot_hub = fhci->vroot_hub;
  252. usb->transfer_confirm = fhci_transfer_confirm_callback;
  253. return usb;
  254. }
  255. static int fhci_start(struct usb_hcd *hcd)
  256. {
  257. int ret;
  258. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  259. ret = fhci_mem_init(fhci);
  260. if (ret) {
  261. fhci_err(fhci, "failed to allocate memory\n");
  262. goto err;
  263. }
  264. fhci->usb_lld = fhci_create_lld(fhci);
  265. if (!fhci->usb_lld) {
  266. fhci_err(fhci, "low level driver config failed\n");
  267. ret = -ENOMEM;
  268. goto err;
  269. }
  270. ret = fhci_usb_init(fhci);
  271. if (ret) {
  272. fhci_err(fhci, "low level driver initialize failed\n");
  273. goto err;
  274. }
  275. spin_lock_init(&fhci->lock);
  276. /* connect the virtual root hub */
  277. fhci->vroot_hub->dev_num = 1; /* this field may be needed to fix */
  278. fhci->vroot_hub->hub.wHubStatus = 0;
  279. fhci->vroot_hub->hub.wHubChange = 0;
  280. fhci->vroot_hub->port.wPortStatus = 0;
  281. fhci->vroot_hub->port.wPortChange = 0;
  282. hcd->state = HC_STATE_RUNNING;
  283. /*
  284. * From here on, khubd concurrently accesses the root
  285. * hub; drivers will be talking to enumerated devices.
  286. * (On restart paths, khubd already knows about the root
  287. * hub and could find work as soon as we wrote FLAG_CF.)
  288. *
  289. * Before this point the HC was idle/ready. After, khubd
  290. * and device drivers may start it running.
  291. */
  292. fhci_usb_enable(fhci);
  293. return 0;
  294. err:
  295. fhci_mem_free(fhci);
  296. return ret;
  297. }
  298. static void fhci_stop(struct usb_hcd *hcd)
  299. {
  300. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  301. fhci_usb_disable_interrupt(fhci->usb_lld);
  302. fhci_usb_disable(fhci);
  303. fhci_usb_free(fhci->usb_lld);
  304. fhci->usb_lld = NULL;
  305. fhci_mem_free(fhci);
  306. }
  307. static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  308. gfp_t mem_flags)
  309. {
  310. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  311. u32 pipe = urb->pipe;
  312. int ret;
  313. int i;
  314. int size = 0;
  315. struct urb_priv *urb_priv;
  316. unsigned long flags;
  317. switch (usb_pipetype(pipe)) {
  318. case PIPE_CONTROL:
  319. /* 1 td fro setup,1 for ack */
  320. size = 2;
  321. case PIPE_BULK:
  322. /* one td for every 4096 bytes(can be up to 8k) */
  323. size += urb->transfer_buffer_length / 4096;
  324. /* ...add for any remaining bytes... */
  325. if ((urb->transfer_buffer_length % 4096) != 0)
  326. size++;
  327. /* ..and maybe a zero length packet to wrap it up */
  328. if (size == 0)
  329. size++;
  330. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  331. && (urb->transfer_buffer_length
  332. % usb_maxpacket(urb->dev, pipe,
  333. usb_pipeout(pipe))) != 0)
  334. size++;
  335. break;
  336. case PIPE_ISOCHRONOUS:
  337. size = urb->number_of_packets;
  338. if (size <= 0)
  339. return -EINVAL;
  340. for (i = 0; i < urb->number_of_packets; i++) {
  341. urb->iso_frame_desc[i].actual_length = 0;
  342. urb->iso_frame_desc[i].status = (u32) (-EXDEV);
  343. }
  344. break;
  345. case PIPE_INTERRUPT:
  346. size = 1;
  347. }
  348. /* allocate the private part of the URB */
  349. urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
  350. if (!urb_priv)
  351. return -ENOMEM;
  352. /* allocate the private part of the URB */
  353. urb_priv->tds = kcalloc(size, sizeof(*urb_priv->tds), mem_flags);
  354. if (!urb_priv->tds) {
  355. kfree(urb_priv);
  356. return -ENOMEM;
  357. }
  358. spin_lock_irqsave(&fhci->lock, flags);
  359. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  360. if (ret)
  361. goto err;
  362. /* fill the private part of the URB */
  363. urb_priv->num_of_tds = size;
  364. urb->status = -EINPROGRESS;
  365. urb->actual_length = 0;
  366. urb->error_count = 0;
  367. urb->hcpriv = urb_priv;
  368. fhci_queue_urb(fhci, urb);
  369. err:
  370. if (ret) {
  371. kfree(urb_priv->tds);
  372. kfree(urb_priv);
  373. }
  374. spin_unlock_irqrestore(&fhci->lock, flags);
  375. return ret;
  376. }
  377. /* dequeue FHCI URB */
  378. static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  379. {
  380. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  381. struct fhci_usb *usb = fhci->usb_lld;
  382. int ret = -EINVAL;
  383. unsigned long flags;
  384. if (!urb || !urb->dev || !urb->dev->bus)
  385. goto out;
  386. spin_lock_irqsave(&fhci->lock, flags);
  387. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  388. if (ret)
  389. goto out2;
  390. if (usb->port_status != FHCI_PORT_DISABLED) {
  391. struct urb_priv *urb_priv;
  392. /*
  393. * flag the urb's data for deletion in some upcoming
  394. * SF interrupt's delete list processing
  395. */
  396. urb_priv = urb->hcpriv;
  397. if (!urb_priv || (urb_priv->state == URB_DEL))
  398. goto out2;
  399. urb_priv->state = URB_DEL;
  400. /* already pending? */
  401. urb_priv->ed->state = FHCI_ED_URB_DEL;
  402. } else {
  403. fhci_urb_complete_free(fhci, urb);
  404. }
  405. out2:
  406. spin_unlock_irqrestore(&fhci->lock, flags);
  407. out:
  408. return ret;
  409. }
  410. static void fhci_endpoint_disable(struct usb_hcd *hcd,
  411. struct usb_host_endpoint *ep)
  412. {
  413. struct fhci_hcd *fhci;
  414. struct ed *ed;
  415. unsigned long flags;
  416. fhci = hcd_to_fhci(hcd);
  417. spin_lock_irqsave(&fhci->lock, flags);
  418. ed = ep->hcpriv;
  419. if (ed) {
  420. while (ed->td_head != NULL) {
  421. struct td *td = fhci_remove_td_from_ed(ed);
  422. fhci_urb_complete_free(fhci, td->urb);
  423. }
  424. fhci_recycle_empty_ed(fhci, ed);
  425. ep->hcpriv = NULL;
  426. }
  427. spin_unlock_irqrestore(&fhci->lock, flags);
  428. }
  429. static int fhci_get_frame_number(struct usb_hcd *hcd)
  430. {
  431. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  432. return get_frame_num(fhci);
  433. }
  434. static const struct hc_driver fhci_driver = {
  435. .description = "fsl,usb-fhci",
  436. .product_desc = "FHCI HOST Controller",
  437. .hcd_priv_size = sizeof(struct fhci_hcd),
  438. /* generic hardware linkage */
  439. .irq = fhci_irq,
  440. .flags = HCD_USB11 | HCD_MEMORY,
  441. /* basic lifecycle operation */
  442. .start = fhci_start,
  443. .stop = fhci_stop,
  444. /* managing i/o requests and associated device resources */
  445. .urb_enqueue = fhci_urb_enqueue,
  446. .urb_dequeue = fhci_urb_dequeue,
  447. .endpoint_disable = fhci_endpoint_disable,
  448. /* scheduling support */
  449. .get_frame_number = fhci_get_frame_number,
  450. /* root hub support */
  451. .hub_status_data = fhci_hub_status_data,
  452. .hub_control = fhci_hub_control,
  453. };
  454. static int __devinit of_fhci_probe(struct platform_device *ofdev)
  455. {
  456. struct device *dev = &ofdev->dev;
  457. struct device_node *node = dev->of_node;
  458. struct usb_hcd *hcd;
  459. struct fhci_hcd *fhci;
  460. struct resource usb_regs;
  461. unsigned long pram_addr;
  462. unsigned int usb_irq;
  463. const char *sprop;
  464. const u32 *iprop;
  465. int size;
  466. int ret;
  467. int i;
  468. int j;
  469. if (usb_disabled())
  470. return -ENODEV;
  471. sprop = of_get_property(node, "mode", NULL);
  472. if (sprop && strcmp(sprop, "host"))
  473. return -ENODEV;
  474. hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
  475. if (!hcd) {
  476. dev_err(dev, "could not create hcd\n");
  477. return -ENOMEM;
  478. }
  479. fhci = hcd_to_fhci(hcd);
  480. hcd->self.controller = dev;
  481. dev_set_drvdata(dev, hcd);
  482. iprop = of_get_property(node, "hub-power-budget", &size);
  483. if (iprop && size == sizeof(*iprop))
  484. hcd->power_budget = *iprop;
  485. /* FHCI registers. */
  486. ret = of_address_to_resource(node, 0, &usb_regs);
  487. if (ret) {
  488. dev_err(dev, "could not get regs\n");
  489. goto err_regs;
  490. }
  491. hcd->regs = ioremap(usb_regs.start, resource_size(&usb_regs));
  492. if (!hcd->regs) {
  493. dev_err(dev, "could not ioremap regs\n");
  494. ret = -ENOMEM;
  495. goto err_regs;
  496. }
  497. fhci->regs = hcd->regs;
  498. /* Parameter RAM. */
  499. iprop = of_get_property(node, "reg", &size);
  500. if (!iprop || size < sizeof(*iprop) * 4) {
  501. dev_err(dev, "can't get pram offset\n");
  502. ret = -EINVAL;
  503. goto err_pram;
  504. }
  505. pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
  506. if (IS_ERR_VALUE(pram_addr)) {
  507. dev_err(dev, "failed to allocate usb pram\n");
  508. ret = -ENOMEM;
  509. goto err_pram;
  510. }
  511. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
  512. QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
  513. fhci->pram = cpm_muram_addr(pram_addr);
  514. /* GPIOs and pins */
  515. for (i = 0; i < NUM_GPIOS; i++) {
  516. int gpio;
  517. enum of_gpio_flags flags;
  518. gpio = of_get_gpio_flags(node, i, &flags);
  519. fhci->gpios[i] = gpio;
  520. fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
  521. if (!gpio_is_valid(gpio)) {
  522. if (i < GPIO_SPEED) {
  523. dev_err(dev, "incorrect GPIO%d: %d\n",
  524. i, gpio);
  525. goto err_gpios;
  526. } else {
  527. dev_info(dev, "assuming board doesn't have "
  528. "%s gpio\n", i == GPIO_SPEED ?
  529. "speed" : "power");
  530. continue;
  531. }
  532. }
  533. ret = gpio_request(gpio, dev_name(dev));
  534. if (ret) {
  535. dev_err(dev, "failed to request gpio %d", i);
  536. goto err_gpios;
  537. }
  538. if (i >= GPIO_SPEED) {
  539. ret = gpio_direction_output(gpio, 0);
  540. if (ret) {
  541. dev_err(dev, "failed to set gpio %d as "
  542. "an output\n", i);
  543. i++;
  544. goto err_gpios;
  545. }
  546. }
  547. }
  548. for (j = 0; j < NUM_PINS; j++) {
  549. fhci->pins[j] = qe_pin_request(node, j);
  550. if (IS_ERR(fhci->pins[j])) {
  551. ret = PTR_ERR(fhci->pins[j]);
  552. dev_err(dev, "can't get pin %d: %d\n", j, ret);
  553. goto err_pins;
  554. }
  555. }
  556. /* Frame limit timer and its interrupt. */
  557. fhci->timer = gtm_get_timer16();
  558. if (IS_ERR(fhci->timer)) {
  559. ret = PTR_ERR(fhci->timer);
  560. dev_err(dev, "failed to request qe timer: %i", ret);
  561. goto err_get_timer;
  562. }
  563. ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
  564. 0, "qe timer (usb)", hcd);
  565. if (ret) {
  566. dev_err(dev, "failed to request timer irq");
  567. goto err_timer_irq;
  568. }
  569. /* USB Host interrupt. */
  570. usb_irq = irq_of_parse_and_map(node, 0);
  571. if (usb_irq == NO_IRQ) {
  572. dev_err(dev, "could not get usb irq\n");
  573. ret = -EINVAL;
  574. goto err_usb_irq;
  575. }
  576. /* Clocks. */
  577. sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
  578. if (sprop) {
  579. fhci->fullspeed_clk = qe_clock_source(sprop);
  580. if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
  581. dev_err(dev, "wrong fullspeed-clock\n");
  582. ret = -EINVAL;
  583. goto err_clocks;
  584. }
  585. }
  586. sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
  587. if (sprop) {
  588. fhci->lowspeed_clk = qe_clock_source(sprop);
  589. if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
  590. dev_err(dev, "wrong lowspeed-clock\n");
  591. ret = -EINVAL;
  592. goto err_clocks;
  593. }
  594. }
  595. if (fhci->fullspeed_clk == QE_CLK_NONE &&
  596. fhci->lowspeed_clk == QE_CLK_NONE) {
  597. dev_err(dev, "no clocks specified\n");
  598. ret = -EINVAL;
  599. goto err_clocks;
  600. }
  601. dev_info(dev, "at 0x%pK, irq %d\n", hcd->regs, usb_irq);
  602. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  603. /* Start with full-speed, if possible. */
  604. if (fhci->fullspeed_clk != QE_CLK_NONE) {
  605. fhci_config_transceiver(fhci, FHCI_PORT_FULL);
  606. qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
  607. } else {
  608. fhci_config_transceiver(fhci, FHCI_PORT_LOW);
  609. qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
  610. }
  611. /* Clear and disable any pending interrupts. */
  612. out_be16(&fhci->regs->usb_event, 0xffff);
  613. out_be16(&fhci->regs->usb_mask, 0);
  614. ret = usb_add_hcd(hcd, usb_irq, 0);
  615. if (ret < 0)
  616. goto err_add_hcd;
  617. fhci_dfs_create(fhci);
  618. return 0;
  619. err_add_hcd:
  620. err_clocks:
  621. irq_dispose_mapping(usb_irq);
  622. err_usb_irq:
  623. free_irq(fhci->timer->irq, hcd);
  624. err_timer_irq:
  625. gtm_put_timer16(fhci->timer);
  626. err_get_timer:
  627. err_pins:
  628. while (--j >= 0)
  629. qe_pin_free(fhci->pins[j]);
  630. err_gpios:
  631. while (--i >= 0) {
  632. if (gpio_is_valid(fhci->gpios[i]))
  633. gpio_free(fhci->gpios[i]);
  634. }
  635. cpm_muram_free(pram_addr);
  636. err_pram:
  637. iounmap(hcd->regs);
  638. err_regs:
  639. usb_put_hcd(hcd);
  640. return ret;
  641. }
  642. static int __devexit fhci_remove(struct device *dev)
  643. {
  644. struct usb_hcd *hcd = dev_get_drvdata(dev);
  645. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  646. int i;
  647. int j;
  648. usb_remove_hcd(hcd);
  649. free_irq(fhci->timer->irq, hcd);
  650. gtm_put_timer16(fhci->timer);
  651. cpm_muram_free(cpm_muram_offset(fhci->pram));
  652. for (i = 0; i < NUM_GPIOS; i++) {
  653. if (!gpio_is_valid(fhci->gpios[i]))
  654. continue;
  655. gpio_free(fhci->gpios[i]);
  656. }
  657. for (j = 0; j < NUM_PINS; j++)
  658. qe_pin_free(fhci->pins[j]);
  659. fhci_dfs_destroy(fhci);
  660. usb_put_hcd(hcd);
  661. return 0;
  662. }
  663. static int __devexit of_fhci_remove(struct platform_device *ofdev)
  664. {
  665. return fhci_remove(&ofdev->dev);
  666. }
  667. static const struct of_device_id of_fhci_match[] = {
  668. { .compatible = "fsl,mpc8323-qe-usb", },
  669. {},
  670. };
  671. MODULE_DEVICE_TABLE(of, of_fhci_match);
  672. static struct platform_driver of_fhci_driver = {
  673. .driver = {
  674. .name = "fsl,usb-fhci",
  675. .owner = THIS_MODULE,
  676. .of_match_table = of_fhci_match,
  677. },
  678. .probe = of_fhci_probe,
  679. .remove = __devexit_p(of_fhci_remove),
  680. };
  681. module_platform_driver(of_fhci_driver);
  682. MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
  683. MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
  684. "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
  685. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  686. MODULE_LICENSE("GPL");