fhci-hcd.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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 = usb->fhci;
  196. if (usb) {
  197. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  198. fhci_ep0_free(usb);
  199. kfree(usb->actual_frame);
  200. kfree(usb);
  201. }
  202. }
  203. /* initialize the USB */
  204. static int fhci_usb_init(struct fhci_hcd *fhci)
  205. {
  206. struct fhci_usb *usb = fhci->usb_lld;
  207. memset_io(usb->fhci->pram, 0, FHCI_PRAM_SIZE);
  208. usb->port_status = FHCI_PORT_DISABLED;
  209. usb->max_frame_usage = FRAME_TIME_USAGE;
  210. usb->sw_transaction_time = SW_FIX_TIME_BETWEEN_TRANSACTION;
  211. usb->actual_frame = kzalloc(sizeof(*usb->actual_frame), GFP_KERNEL);
  212. if (!usb->actual_frame) {
  213. fhci_usb_free(usb);
  214. return -ENOMEM;
  215. }
  216. INIT_LIST_HEAD(&usb->actual_frame->tds_list);
  217. /* initializing registers on chip, clear frame number */
  218. out_be16(&fhci->pram->frame_num, 0);
  219. /* clear rx state */
  220. out_be32(&fhci->pram->rx_state, 0);
  221. /* set mask register */
  222. usb->saved_msk = (USB_E_TXB_MASK |
  223. USB_E_TXE1_MASK |
  224. USB_E_IDLE_MASK |
  225. USB_E_RESET_MASK | USB_E_SFT_MASK | USB_E_MSF_MASK);
  226. out_8(&usb->fhci->regs->usb_mod, USB_MODE_HOST | USB_MODE_EN);
  227. /* clearing the mask register */
  228. out_be16(&usb->fhci->regs->usb_mask, 0);
  229. /* initialing the event register */
  230. out_be16(&usb->fhci->regs->usb_event, 0xffff);
  231. if (endpoint_zero_init(usb, DEFAULT_DATA_MEM, DEFAULT_RING_LEN) != 0) {
  232. fhci_usb_free(usb);
  233. return -EINVAL;
  234. }
  235. return 0;
  236. }
  237. /* initialize the fhci_usb struct and the corresponding data staruct */
  238. static struct fhci_usb *fhci_create_lld(struct fhci_hcd *fhci)
  239. {
  240. struct fhci_usb *usb;
  241. /* allocate memory for SCC data structure */
  242. usb = kzalloc(sizeof(*usb), GFP_KERNEL);
  243. if (!usb) {
  244. fhci_err(fhci, "no memory for SCC data struct\n");
  245. return NULL;
  246. }
  247. usb->fhci = fhci;
  248. usb->hc_list = fhci->hc_list;
  249. usb->vroot_hub = fhci->vroot_hub;
  250. usb->transfer_confirm = fhci_transfer_confirm_callback;
  251. return usb;
  252. }
  253. static int fhci_start(struct usb_hcd *hcd)
  254. {
  255. int ret;
  256. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  257. ret = fhci_mem_init(fhci);
  258. if (ret) {
  259. fhci_err(fhci, "failed to allocate memory\n");
  260. goto err;
  261. }
  262. fhci->usb_lld = fhci_create_lld(fhci);
  263. if (!fhci->usb_lld) {
  264. fhci_err(fhci, "low level driver config failed\n");
  265. ret = -ENOMEM;
  266. goto err;
  267. }
  268. ret = fhci_usb_init(fhci);
  269. if (ret) {
  270. fhci_err(fhci, "low level driver initialize failed\n");
  271. goto err;
  272. }
  273. spin_lock_init(&fhci->lock);
  274. /* connect the virtual root hub */
  275. fhci->vroot_hub->dev_num = 1; /* this field may be needed to fix */
  276. fhci->vroot_hub->hub.wHubStatus = 0;
  277. fhci->vroot_hub->hub.wHubChange = 0;
  278. fhci->vroot_hub->port.wPortStatus = 0;
  279. fhci->vroot_hub->port.wPortChange = 0;
  280. hcd->state = HC_STATE_RUNNING;
  281. /*
  282. * From here on, khubd concurrently accesses the root
  283. * hub; drivers will be talking to enumerated devices.
  284. * (On restart paths, khubd already knows about the root
  285. * hub and could find work as soon as we wrote FLAG_CF.)
  286. *
  287. * Before this point the HC was idle/ready. After, khubd
  288. * and device drivers may start it running.
  289. */
  290. fhci_usb_enable(fhci);
  291. return 0;
  292. err:
  293. fhci_mem_free(fhci);
  294. return ret;
  295. }
  296. static void fhci_stop(struct usb_hcd *hcd)
  297. {
  298. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  299. fhci_usb_disable_interrupt(fhci->usb_lld);
  300. fhci_usb_disable(fhci);
  301. fhci_usb_free(fhci->usb_lld);
  302. fhci->usb_lld = NULL;
  303. fhci_mem_free(fhci);
  304. }
  305. static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  306. gfp_t mem_flags)
  307. {
  308. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  309. u32 pipe = urb->pipe;
  310. int ret;
  311. int i;
  312. int size = 0;
  313. struct urb_priv *urb_priv;
  314. unsigned long flags;
  315. switch (usb_pipetype(pipe)) {
  316. case PIPE_CONTROL:
  317. /* 1 td fro setup,1 for ack */
  318. size = 2;
  319. case PIPE_BULK:
  320. /* one td for every 4096 bytes(can be upto 8k) */
  321. size += urb->transfer_buffer_length / 4096;
  322. /* ...add for any remaining bytes... */
  323. if ((urb->transfer_buffer_length % 4096) != 0)
  324. size++;
  325. /* ..and maybe a zero length packet to wrap it up */
  326. if (size == 0)
  327. size++;
  328. else if ((urb->transfer_flags & URB_ZERO_PACKET) != 0
  329. && (urb->transfer_buffer_length
  330. % usb_maxpacket(urb->dev, pipe,
  331. usb_pipeout(pipe))) != 0)
  332. size++;
  333. break;
  334. case PIPE_ISOCHRONOUS:
  335. size = urb->number_of_packets;
  336. if (size <= 0)
  337. return -EINVAL;
  338. for (i = 0; i < urb->number_of_packets; i++) {
  339. urb->iso_frame_desc[i].actual_length = 0;
  340. urb->iso_frame_desc[i].status = (u32) (-EXDEV);
  341. }
  342. break;
  343. case PIPE_INTERRUPT:
  344. size = 1;
  345. }
  346. /* allocate the private part of the URB */
  347. urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
  348. if (!urb_priv)
  349. return -ENOMEM;
  350. /* allocate the private part of the URB */
  351. urb_priv->tds = kzalloc(size * sizeof(struct td), mem_flags);
  352. if (!urb_priv->tds) {
  353. kfree(urb_priv);
  354. return -ENOMEM;
  355. }
  356. spin_lock_irqsave(&fhci->lock, flags);
  357. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  358. if (ret)
  359. goto err;
  360. /* fill the private part of the URB */
  361. urb_priv->num_of_tds = size;
  362. urb->status = -EINPROGRESS;
  363. urb->actual_length = 0;
  364. urb->error_count = 0;
  365. urb->hcpriv = urb_priv;
  366. fhci_queue_urb(fhci, urb);
  367. err:
  368. if (ret) {
  369. kfree(urb_priv->tds);
  370. kfree(urb_priv);
  371. }
  372. spin_unlock_irqrestore(&fhci->lock, flags);
  373. return ret;
  374. }
  375. /* dequeue FHCI URB */
  376. static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  377. {
  378. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  379. struct fhci_usb *usb = fhci->usb_lld;
  380. int ret = -EINVAL;
  381. unsigned long flags;
  382. if (!urb || !urb->dev || !urb->dev->bus)
  383. goto out;
  384. spin_lock_irqsave(&fhci->lock, flags);
  385. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  386. if (ret)
  387. goto out2;
  388. if (usb->port_status != FHCI_PORT_DISABLED) {
  389. struct urb_priv *urb_priv;
  390. /*
  391. * flag the urb's data for deletion in some upcoming
  392. * SF interrupt's delete list processing
  393. */
  394. urb_priv = urb->hcpriv;
  395. if (!urb_priv || (urb_priv->state == URB_DEL))
  396. goto out2;
  397. urb_priv->state = URB_DEL;
  398. /* already pending? */
  399. urb_priv->ed->state = FHCI_ED_URB_DEL;
  400. } else {
  401. fhci_urb_complete_free(fhci, urb);
  402. }
  403. out2:
  404. spin_unlock_irqrestore(&fhci->lock, flags);
  405. out:
  406. return ret;
  407. }
  408. static void fhci_endpoint_disable(struct usb_hcd *hcd,
  409. struct usb_host_endpoint *ep)
  410. {
  411. struct fhci_hcd *fhci;
  412. struct ed *ed;
  413. unsigned long flags;
  414. fhci = hcd_to_fhci(hcd);
  415. spin_lock_irqsave(&fhci->lock, flags);
  416. ed = ep->hcpriv;
  417. if (ed) {
  418. while (ed->td_head != NULL) {
  419. struct td *td = fhci_remove_td_from_ed(ed);
  420. fhci_urb_complete_free(fhci, td->urb);
  421. }
  422. fhci_recycle_empty_ed(fhci, ed);
  423. ep->hcpriv = NULL;
  424. }
  425. spin_unlock_irqrestore(&fhci->lock, flags);
  426. }
  427. static int fhci_get_frame_number(struct usb_hcd *hcd)
  428. {
  429. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  430. return get_frame_num(fhci);
  431. }
  432. static const struct hc_driver fhci_driver = {
  433. .description = "fsl,usb-fhci",
  434. .product_desc = "FHCI HOST Controller",
  435. .hcd_priv_size = sizeof(struct fhci_hcd),
  436. /* generic hardware linkage */
  437. .irq = fhci_irq,
  438. .flags = HCD_USB11 | HCD_MEMORY,
  439. /* basic lifecycle operation */
  440. .start = fhci_start,
  441. .stop = fhci_stop,
  442. /* managing i/o requests and associated device resources */
  443. .urb_enqueue = fhci_urb_enqueue,
  444. .urb_dequeue = fhci_urb_dequeue,
  445. .endpoint_disable = fhci_endpoint_disable,
  446. /* scheduling support */
  447. .get_frame_number = fhci_get_frame_number,
  448. /* root hub support */
  449. .hub_status_data = fhci_hub_status_data,
  450. .hub_control = fhci_hub_control,
  451. };
  452. static int __devinit of_fhci_probe(struct of_device *ofdev,
  453. const struct of_device_id *ofid)
  454. {
  455. struct device *dev = &ofdev->dev;
  456. struct device_node *node = ofdev->node;
  457. struct usb_hcd *hcd;
  458. struct fhci_hcd *fhci;
  459. struct resource usb_regs;
  460. unsigned long pram_addr;
  461. unsigned int usb_irq;
  462. const char *sprop;
  463. const u32 *iprop;
  464. int size;
  465. int ret;
  466. int i;
  467. int j;
  468. if (usb_disabled())
  469. return -ENODEV;
  470. sprop = of_get_property(node, "mode", NULL);
  471. if (sprop && strcmp(sprop, "host"))
  472. return -ENODEV;
  473. hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
  474. if (!hcd) {
  475. dev_err(dev, "could not create hcd\n");
  476. return -ENOMEM;
  477. }
  478. fhci = hcd_to_fhci(hcd);
  479. hcd->self.controller = dev;
  480. dev_set_drvdata(dev, hcd);
  481. iprop = of_get_property(node, "hub-power-budget", &size);
  482. if (iprop && size == sizeof(*iprop))
  483. hcd->power_budget = *iprop;
  484. /* FHCI registers. */
  485. ret = of_address_to_resource(node, 0, &usb_regs);
  486. if (ret) {
  487. dev_err(dev, "could not get regs\n");
  488. goto err_regs;
  489. }
  490. hcd->regs = ioremap(usb_regs.start, usb_regs.end - usb_regs.start + 1);
  491. if (!hcd->regs) {
  492. dev_err(dev, "could not ioremap regs\n");
  493. ret = -ENOMEM;
  494. goto err_regs;
  495. }
  496. fhci->regs = hcd->regs;
  497. /* Parameter RAM. */
  498. iprop = of_get_property(node, "reg", &size);
  499. if (!iprop || size < sizeof(*iprop) * 4) {
  500. dev_err(dev, "can't get pram offset\n");
  501. ret = -EINVAL;
  502. goto err_pram;
  503. }
  504. pram_addr = cpm_muram_alloc_fixed(iprop[2], FHCI_PRAM_SIZE);
  505. if (IS_ERR_VALUE(pram_addr)) {
  506. dev_err(dev, "failed to allocate usb pram\n");
  507. ret = -ENOMEM;
  508. goto err_pram;
  509. }
  510. fhci->pram = cpm_muram_addr(pram_addr);
  511. /* GPIOs and pins */
  512. for (i = 0; i < NUM_GPIOS; i++) {
  513. int gpio;
  514. enum of_gpio_flags flags;
  515. gpio = of_get_gpio_flags(node, i, &flags);
  516. fhci->gpios[i] = gpio;
  517. fhci->alow_gpios[i] = flags & OF_GPIO_ACTIVE_LOW;
  518. if (!gpio_is_valid(gpio)) {
  519. if (i < GPIO_SPEED) {
  520. dev_err(dev, "incorrect GPIO%d: %d\n",
  521. i, gpio);
  522. goto err_gpios;
  523. } else {
  524. dev_info(dev, "assuming board doesn't have "
  525. "%s gpio\n", i == GPIO_SPEED ?
  526. "speed" : "power");
  527. continue;
  528. }
  529. }
  530. ret = gpio_request(gpio, dev_name(dev));
  531. if (ret) {
  532. dev_err(dev, "failed to request gpio %d", i);
  533. goto err_gpios;
  534. }
  535. if (i >= GPIO_SPEED) {
  536. ret = gpio_direction_output(gpio, 0);
  537. if (ret) {
  538. dev_err(dev, "failed to set gpio %d as "
  539. "an output\n", i);
  540. i++;
  541. goto err_gpios;
  542. }
  543. }
  544. }
  545. for (j = 0; j < NUM_PINS; j++) {
  546. fhci->pins[j] = qe_pin_request(ofdev->node, j);
  547. if (IS_ERR(fhci->pins[j])) {
  548. ret = PTR_ERR(fhci->pins[j]);
  549. dev_err(dev, "can't get pin %d: %d\n", j, ret);
  550. goto err_pins;
  551. }
  552. }
  553. /* Frame limit timer and its interrupt. */
  554. fhci->timer = gtm_get_timer16();
  555. if (IS_ERR(fhci->timer)) {
  556. ret = PTR_ERR(fhci->timer);
  557. dev_err(dev, "failed to request qe timer: %i", ret);
  558. goto err_get_timer;
  559. }
  560. ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
  561. IRQF_DISABLED, "qe timer (usb)", hcd);
  562. if (ret) {
  563. dev_err(dev, "failed to request timer irq");
  564. goto err_timer_irq;
  565. }
  566. /* USB Host interrupt. */
  567. usb_irq = irq_of_parse_and_map(node, 0);
  568. if (usb_irq == NO_IRQ) {
  569. dev_err(dev, "could not get usb irq\n");
  570. ret = -EINVAL;
  571. goto err_usb_irq;
  572. }
  573. /* Clocks. */
  574. sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
  575. if (sprop) {
  576. fhci->fullspeed_clk = qe_clock_source(sprop);
  577. if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
  578. dev_err(dev, "wrong fullspeed-clock\n");
  579. ret = -EINVAL;
  580. goto err_clocks;
  581. }
  582. }
  583. sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
  584. if (sprop) {
  585. fhci->lowspeed_clk = qe_clock_source(sprop);
  586. if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
  587. dev_err(dev, "wrong lowspeed-clock\n");
  588. ret = -EINVAL;
  589. goto err_clocks;
  590. }
  591. }
  592. if (fhci->fullspeed_clk == QE_CLK_NONE &&
  593. fhci->lowspeed_clk == QE_CLK_NONE) {
  594. dev_err(dev, "no clocks specified\n");
  595. ret = -EINVAL;
  596. goto err_clocks;
  597. }
  598. dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
  599. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  600. /* Start with full-speed, if possible. */
  601. if (fhci->fullspeed_clk != QE_CLK_NONE) {
  602. fhci_config_transceiver(fhci, FHCI_PORT_FULL);
  603. qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
  604. } else {
  605. fhci_config_transceiver(fhci, FHCI_PORT_LOW);
  606. qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
  607. }
  608. /* Clear and disable any pending interrupts. */
  609. out_be16(&fhci->regs->usb_event, 0xffff);
  610. out_be16(&fhci->regs->usb_mask, 0);
  611. ret = usb_add_hcd(hcd, usb_irq, IRQF_DISABLED);
  612. if (ret < 0)
  613. goto err_add_hcd;
  614. fhci_dfs_create(fhci);
  615. return 0;
  616. err_add_hcd:
  617. err_clocks:
  618. irq_dispose_mapping(usb_irq);
  619. err_usb_irq:
  620. free_irq(fhci->timer->irq, hcd);
  621. err_timer_irq:
  622. gtm_put_timer16(fhci->timer);
  623. err_get_timer:
  624. err_pins:
  625. while (--j >= 0)
  626. qe_pin_free(fhci->pins[j]);
  627. err_gpios:
  628. while (--i >= 0) {
  629. if (gpio_is_valid(fhci->gpios[i]))
  630. gpio_free(fhci->gpios[i]);
  631. }
  632. cpm_muram_free(pram_addr);
  633. err_pram:
  634. iounmap(hcd->regs);
  635. err_regs:
  636. usb_put_hcd(hcd);
  637. return ret;
  638. }
  639. static int __devexit fhci_remove(struct device *dev)
  640. {
  641. struct usb_hcd *hcd = dev_get_drvdata(dev);
  642. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  643. int i;
  644. int j;
  645. usb_remove_hcd(hcd);
  646. free_irq(fhci->timer->irq, hcd);
  647. gtm_put_timer16(fhci->timer);
  648. cpm_muram_free(cpm_muram_offset(fhci->pram));
  649. for (i = 0; i < NUM_GPIOS; i++) {
  650. if (!gpio_is_valid(fhci->gpios[i]))
  651. continue;
  652. gpio_free(fhci->gpios[i]);
  653. }
  654. for (j = 0; j < NUM_PINS; j++)
  655. qe_pin_free(fhci->pins[j]);
  656. fhci_dfs_destroy(fhci);
  657. usb_put_hcd(hcd);
  658. return 0;
  659. }
  660. static int __devexit of_fhci_remove(struct of_device *ofdev)
  661. {
  662. return fhci_remove(&ofdev->dev);
  663. }
  664. static struct of_device_id of_fhci_match[] = {
  665. { .compatible = "fsl,mpc8323-qe-usb", },
  666. {},
  667. };
  668. MODULE_DEVICE_TABLE(of, of_fhci_match);
  669. static struct of_platform_driver of_fhci_driver = {
  670. .name = "fsl,usb-fhci",
  671. .match_table = of_fhci_match,
  672. .probe = of_fhci_probe,
  673. .remove = __devexit_p(of_fhci_remove),
  674. };
  675. static int __init fhci_module_init(void)
  676. {
  677. return of_register_platform_driver(&of_fhci_driver);
  678. }
  679. module_init(fhci_module_init);
  680. static void __exit fhci_module_exit(void)
  681. {
  682. of_unregister_platform_driver(&of_fhci_driver);
  683. }
  684. module_exit(fhci_module_exit);
  685. MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
  686. MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
  687. "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
  688. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  689. MODULE_LICENSE("GPL");