fhci-hcd.c 19 KB

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