fhci-hcd.c 19 KB

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