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