fhci-hcd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/gpio/consumer.h>
  30. #include <soc/fsl/qe/qe.h>
  31. #include <asm/fsl_gtm.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_ussft, 0);
  39. setbits8(&fhci->regs->usb_usmod, 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_usmod, 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_ussft) / 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_usber, 0xffff);
  75. out_be16(&usb->fhci->regs->usb_usbmr, 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. /* disable 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. /* disable 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_usbmr, 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_usber, 0xffff);
  100. out_be16(&usb->fhci->regs->usb_usbmr, usb->saved_msk);
  101. setbits8(&usb->fhci->regs->usb_usmod, 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_usmod, 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 (!gpiod_get_value(fhci->gpiods[GPIO_USBOE]))
  124. return -1;
  125. /* check USBRP */
  126. if (gpiod_get_value(fhci->gpiods[GPIO_USBRP]))
  127. bits |= 0x2;
  128. /* check USBRN */
  129. if (gpiod_get_value(fhci->gpiods[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_usmod, USB_MODE_HOST | USB_MODE_EN);
  228. /* clearing the mask register */
  229. out_be16(&usb->fhci->regs->usb_usbmr, 0);
  230. /* initialing the event register */
  231. out_be16(&usb->fhci->regs->usb_usber, 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. return NULL;
  246. usb->fhci = fhci;
  247. usb->hc_list = fhci->hc_list;
  248. usb->vroot_hub = fhci->vroot_hub;
  249. usb->transfer_confirm = fhci_transfer_confirm_callback;
  250. return usb;
  251. }
  252. static int fhci_start(struct usb_hcd *hcd)
  253. {
  254. int ret;
  255. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  256. ret = fhci_mem_init(fhci);
  257. if (ret) {
  258. fhci_err(fhci, "failed to allocate memory\n");
  259. goto err;
  260. }
  261. fhci->usb_lld = fhci_create_lld(fhci);
  262. if (!fhci->usb_lld) {
  263. fhci_err(fhci, "low level driver config failed\n");
  264. ret = -ENOMEM;
  265. goto err;
  266. }
  267. ret = fhci_usb_init(fhci);
  268. if (ret) {
  269. fhci_err(fhci, "low level driver initialize failed\n");
  270. goto err;
  271. }
  272. spin_lock_init(&fhci->lock);
  273. /* connect the virtual root hub */
  274. fhci->vroot_hub->dev_num = 1; /* this field may be needed to fix */
  275. fhci->vroot_hub->hub.wHubStatus = 0;
  276. fhci->vroot_hub->hub.wHubChange = 0;
  277. fhci->vroot_hub->port.wPortStatus = 0;
  278. fhci->vroot_hub->port.wPortChange = 0;
  279. hcd->state = HC_STATE_RUNNING;
  280. /*
  281. * From here on, hub_wq concurrently accesses the root
  282. * hub; drivers will be talking to enumerated devices.
  283. * (On restart paths, hub_wq already knows about the root
  284. * hub and could find work as soon as we wrote FLAG_CF.)
  285. *
  286. * Before this point the HC was idle/ready. After, hub_wq
  287. * and device drivers may start it running.
  288. */
  289. fhci_usb_enable(fhci);
  290. return 0;
  291. err:
  292. fhci_mem_free(fhci);
  293. return ret;
  294. }
  295. static void fhci_stop(struct usb_hcd *hcd)
  296. {
  297. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  298. fhci_usb_disable_interrupt(fhci->usb_lld);
  299. fhci_usb_disable(fhci);
  300. fhci_usb_free(fhci->usb_lld);
  301. fhci->usb_lld = NULL;
  302. fhci_mem_free(fhci);
  303. }
  304. static int fhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
  305. gfp_t mem_flags)
  306. {
  307. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  308. u32 pipe = urb->pipe;
  309. int ret;
  310. int i;
  311. int size = 0;
  312. struct urb_priv *urb_priv;
  313. unsigned long flags;
  314. switch (usb_pipetype(pipe)) {
  315. case PIPE_CONTROL:
  316. /* 1 td fro setup,1 for ack */
  317. size = 2;
  318. fallthrough;
  319. case PIPE_BULK:
  320. /* one td for every 4096 bytes(can be up to 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)) != 0)
  331. size++;
  332. break;
  333. case PIPE_ISOCHRONOUS:
  334. size = urb->number_of_packets;
  335. if (size <= 0)
  336. return -EINVAL;
  337. for (i = 0; i < urb->number_of_packets; i++) {
  338. urb->iso_frame_desc[i].actual_length = 0;
  339. urb->iso_frame_desc[i].status = (u32) (-EXDEV);
  340. }
  341. break;
  342. case PIPE_INTERRUPT:
  343. size = 1;
  344. }
  345. /* allocate the private part of the URB */
  346. urb_priv = kzalloc(sizeof(*urb_priv), mem_flags);
  347. if (!urb_priv)
  348. return -ENOMEM;
  349. /* allocate the private part of the URB */
  350. urb_priv->tds = kcalloc(size, sizeof(*urb_priv->tds), mem_flags);
  351. if (!urb_priv->tds) {
  352. kfree(urb_priv);
  353. return -ENOMEM;
  354. }
  355. spin_lock_irqsave(&fhci->lock, flags);
  356. ret = usb_hcd_link_urb_to_ep(hcd, urb);
  357. if (ret)
  358. goto err;
  359. /* fill the private part of the URB */
  360. urb_priv->num_of_tds = size;
  361. urb->status = -EINPROGRESS;
  362. urb->actual_length = 0;
  363. urb->error_count = 0;
  364. urb->hcpriv = urb_priv;
  365. fhci_queue_urb(fhci, urb);
  366. err:
  367. if (ret) {
  368. kfree(urb_priv->tds);
  369. kfree(urb_priv);
  370. }
  371. spin_unlock_irqrestore(&fhci->lock, flags);
  372. return ret;
  373. }
  374. /* dequeue FHCI URB */
  375. static int fhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
  376. {
  377. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  378. struct fhci_usb *usb = fhci->usb_lld;
  379. int ret = -EINVAL;
  380. unsigned long flags;
  381. if (!urb || !urb->dev || !urb->dev->bus)
  382. goto out;
  383. spin_lock_irqsave(&fhci->lock, flags);
  384. ret = usb_hcd_check_unlink_urb(hcd, urb, status);
  385. if (ret)
  386. goto out2;
  387. if (usb->port_status != FHCI_PORT_DISABLED) {
  388. struct urb_priv *urb_priv;
  389. /*
  390. * flag the urb's data for deletion in some upcoming
  391. * SF interrupt's delete list processing
  392. */
  393. urb_priv = urb->hcpriv;
  394. if (!urb_priv || (urb_priv->state == URB_DEL))
  395. goto out2;
  396. urb_priv->state = URB_DEL;
  397. /* already pending? */
  398. urb_priv->ed->state = FHCI_ED_URB_DEL;
  399. } else {
  400. fhci_urb_complete_free(fhci, urb);
  401. }
  402. out2:
  403. spin_unlock_irqrestore(&fhci->lock, flags);
  404. out:
  405. return ret;
  406. }
  407. static void fhci_endpoint_disable(struct usb_hcd *hcd,
  408. struct usb_host_endpoint *ep)
  409. {
  410. struct fhci_hcd *fhci;
  411. struct ed *ed;
  412. unsigned long flags;
  413. fhci = hcd_to_fhci(hcd);
  414. spin_lock_irqsave(&fhci->lock, flags);
  415. ed = ep->hcpriv;
  416. if (ed) {
  417. while (ed->td_head != NULL) {
  418. struct td *td = fhci_remove_td_from_ed(ed);
  419. fhci_urb_complete_free(fhci, td->urb);
  420. }
  421. fhci_recycle_empty_ed(fhci, ed);
  422. ep->hcpriv = NULL;
  423. }
  424. spin_unlock_irqrestore(&fhci->lock, flags);
  425. }
  426. static int fhci_get_frame_number(struct usb_hcd *hcd)
  427. {
  428. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  429. return get_frame_num(fhci);
  430. }
  431. static const struct hc_driver fhci_driver = {
  432. .description = "fsl,usb-fhci",
  433. .product_desc = "FHCI HOST Controller",
  434. .hcd_priv_size = sizeof(struct fhci_hcd),
  435. /* generic hardware linkage */
  436. .irq = fhci_irq,
  437. .flags = HCD_DMA | HCD_USB11 | HCD_MEMORY,
  438. /* basic lifecycle operation */
  439. .start = fhci_start,
  440. .stop = fhci_stop,
  441. /* managing i/o requests and associated device resources */
  442. .urb_enqueue = fhci_urb_enqueue,
  443. .urb_dequeue = fhci_urb_dequeue,
  444. .endpoint_disable = fhci_endpoint_disable,
  445. /* scheduling support */
  446. .get_frame_number = fhci_get_frame_number,
  447. /* root hub support */
  448. .hub_status_data = fhci_hub_status_data,
  449. .hub_control = fhci_hub_control,
  450. };
  451. static int of_fhci_probe(struct platform_device *ofdev)
  452. {
  453. struct device *dev = &ofdev->dev;
  454. struct device_node *node = dev->of_node;
  455. struct usb_hcd *hcd;
  456. struct fhci_hcd *fhci;
  457. struct resource usb_regs;
  458. unsigned long pram_addr;
  459. unsigned int usb_irq;
  460. const char *sprop;
  461. const u32 *iprop;
  462. int size;
  463. int ret;
  464. int i;
  465. int j;
  466. if (usb_disabled())
  467. return -ENODEV;
  468. sprop = of_get_property(node, "mode", NULL);
  469. if (sprop && strcmp(sprop, "host"))
  470. return -ENODEV;
  471. hcd = usb_create_hcd(&fhci_driver, dev, dev_name(dev));
  472. if (!hcd) {
  473. dev_err(dev, "could not create hcd\n");
  474. return -ENOMEM;
  475. }
  476. fhci = hcd_to_fhci(hcd);
  477. hcd->self.controller = dev;
  478. dev_set_drvdata(dev, hcd);
  479. iprop = of_get_property(node, "hub-power-budget", &size);
  480. if (iprop && size == sizeof(*iprop))
  481. hcd->power_budget = *iprop;
  482. /* FHCI registers. */
  483. ret = of_address_to_resource(node, 0, &usb_regs);
  484. if (ret) {
  485. dev_err(dev, "could not get regs\n");
  486. goto err_regs;
  487. }
  488. hcd->regs = ioremap(usb_regs.start, resource_size(&usb_regs));
  489. if (!hcd->regs) {
  490. dev_err(dev, "could not ioremap regs\n");
  491. ret = -ENOMEM;
  492. goto err_regs;
  493. }
  494. fhci->regs = hcd->regs;
  495. /* Parameter RAM. */
  496. iprop = of_get_property(node, "reg", &size);
  497. if (!iprop || size < sizeof(*iprop) * 4) {
  498. dev_err(dev, "can't get pram offset\n");
  499. ret = -EINVAL;
  500. goto err_pram;
  501. }
  502. pram_addr = cpm_muram_alloc(FHCI_PRAM_SIZE, 64);
  503. if (IS_ERR_VALUE(pram_addr)) {
  504. dev_err(dev, "failed to allocate usb pram\n");
  505. ret = -ENOMEM;
  506. goto err_pram;
  507. }
  508. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, QE_CR_SUBBLOCK_USB,
  509. QE_CR_PROTOCOL_UNSPECIFIED, pram_addr);
  510. fhci->pram = cpm_muram_addr(pram_addr);
  511. /* GPIOs and pins */
  512. for (i = 0; i < NUM_GPIOS; i++) {
  513. if (i < GPIO_SPEED)
  514. fhci->gpiods[i] = devm_gpiod_get_index(dev,
  515. NULL, i, GPIOD_IN);
  516. else
  517. fhci->gpiods[i] = devm_gpiod_get_index_optional(dev,
  518. NULL, i, GPIOD_OUT_LOW);
  519. if (IS_ERR(fhci->gpiods[i])) {
  520. dev_err(dev, "incorrect GPIO%d: %ld\n",
  521. i, PTR_ERR(fhci->gpiods[i]));
  522. goto err_gpios;
  523. }
  524. if (!fhci->gpiods[i]) {
  525. dev_info(dev, "assuming board doesn't have "
  526. "%s gpio\n", i == GPIO_SPEED ?
  527. "speed" : "power");
  528. }
  529. }
  530. for (j = 0; j < NUM_PINS; j++) {
  531. fhci->pins[j] = qe_pin_request(dev, j);
  532. if (IS_ERR(fhci->pins[j])) {
  533. ret = PTR_ERR(fhci->pins[j]);
  534. dev_err(dev, "can't get pin %d: %d\n", j, ret);
  535. goto err_pins;
  536. }
  537. }
  538. /* Frame limit timer and its interrupt. */
  539. fhci->timer = gtm_get_timer16();
  540. if (IS_ERR(fhci->timer)) {
  541. ret = PTR_ERR(fhci->timer);
  542. dev_err(dev, "failed to request qe timer: %i", ret);
  543. goto err_get_timer;
  544. }
  545. ret = request_irq(fhci->timer->irq, fhci_frame_limit_timer_irq,
  546. 0, "qe timer (usb)", hcd);
  547. if (ret) {
  548. dev_err(dev, "failed to request timer irq");
  549. goto err_timer_irq;
  550. }
  551. /* USB Host interrupt. */
  552. usb_irq = irq_of_parse_and_map(node, 0);
  553. if (!usb_irq) {
  554. dev_err(dev, "could not get usb irq\n");
  555. ret = -EINVAL;
  556. goto err_usb_irq;
  557. }
  558. /* Clocks. */
  559. sprop = of_get_property(node, "fsl,fullspeed-clock", NULL);
  560. if (sprop) {
  561. fhci->fullspeed_clk = qe_clock_source(sprop);
  562. if (fhci->fullspeed_clk == QE_CLK_DUMMY) {
  563. dev_err(dev, "wrong fullspeed-clock\n");
  564. ret = -EINVAL;
  565. goto err_clocks;
  566. }
  567. }
  568. sprop = of_get_property(node, "fsl,lowspeed-clock", NULL);
  569. if (sprop) {
  570. fhci->lowspeed_clk = qe_clock_source(sprop);
  571. if (fhci->lowspeed_clk == QE_CLK_DUMMY) {
  572. dev_err(dev, "wrong lowspeed-clock\n");
  573. ret = -EINVAL;
  574. goto err_clocks;
  575. }
  576. }
  577. if (fhci->fullspeed_clk == QE_CLK_NONE &&
  578. fhci->lowspeed_clk == QE_CLK_NONE) {
  579. dev_err(dev, "no clocks specified\n");
  580. ret = -EINVAL;
  581. goto err_clocks;
  582. }
  583. dev_info(dev, "at 0x%p, irq %d\n", hcd->regs, usb_irq);
  584. fhci_config_transceiver(fhci, FHCI_PORT_POWER_OFF);
  585. /* Start with full-speed, if possible. */
  586. if (fhci->fullspeed_clk != QE_CLK_NONE) {
  587. fhci_config_transceiver(fhci, FHCI_PORT_FULL);
  588. qe_usb_clock_set(fhci->fullspeed_clk, USB_CLOCK);
  589. } else {
  590. fhci_config_transceiver(fhci, FHCI_PORT_LOW);
  591. qe_usb_clock_set(fhci->lowspeed_clk, USB_CLOCK >> 3);
  592. }
  593. /* Clear and disable any pending interrupts. */
  594. out_be16(&fhci->regs->usb_usber, 0xffff);
  595. out_be16(&fhci->regs->usb_usbmr, 0);
  596. ret = usb_add_hcd(hcd, usb_irq, 0);
  597. if (ret < 0)
  598. goto err_add_hcd;
  599. device_wakeup_enable(hcd->self.controller);
  600. fhci_dfs_create(fhci);
  601. return 0;
  602. err_add_hcd:
  603. err_clocks:
  604. irq_dispose_mapping(usb_irq);
  605. err_usb_irq:
  606. free_irq(fhci->timer->irq, hcd);
  607. err_timer_irq:
  608. gtm_put_timer16(fhci->timer);
  609. err_get_timer:
  610. err_pins:
  611. while (--j >= 0)
  612. qe_pin_free(fhci->pins[j]);
  613. err_gpios:
  614. cpm_muram_free(pram_addr);
  615. err_pram:
  616. iounmap(hcd->regs);
  617. err_regs:
  618. usb_put_hcd(hcd);
  619. return ret;
  620. }
  621. static void fhci_remove(struct device *dev)
  622. {
  623. struct usb_hcd *hcd = dev_get_drvdata(dev);
  624. struct fhci_hcd *fhci = hcd_to_fhci(hcd);
  625. int j;
  626. usb_remove_hcd(hcd);
  627. free_irq(fhci->timer->irq, hcd);
  628. gtm_put_timer16(fhci->timer);
  629. cpm_muram_free(cpm_muram_offset(fhci->pram));
  630. for (j = 0; j < NUM_PINS; j++)
  631. qe_pin_free(fhci->pins[j]);
  632. fhci_dfs_destroy(fhci);
  633. usb_put_hcd(hcd);
  634. }
  635. static void of_fhci_remove(struct platform_device *ofdev)
  636. {
  637. fhci_remove(&ofdev->dev);
  638. }
  639. static const struct of_device_id of_fhci_match[] = {
  640. { .compatible = "fsl,mpc8323-qe-usb", },
  641. {},
  642. };
  643. MODULE_DEVICE_TABLE(of, of_fhci_match);
  644. static struct platform_driver of_fhci_driver = {
  645. .driver = {
  646. .name = "fsl,usb-fhci",
  647. .of_match_table = of_fhci_match,
  648. },
  649. .probe = of_fhci_probe,
  650. .remove_new = of_fhci_remove,
  651. };
  652. module_platform_driver(of_fhci_driver);
  653. MODULE_DESCRIPTION("USB Freescale Host Controller Interface Driver");
  654. MODULE_AUTHOR("Shlomi Gridish <gridish@freescale.com>, "
  655. "Jerry Huang <Chang-Ming.Huang@freescale.com>, "
  656. "Anton Vorontsov <avorontsov@ru.mvista.com>");
  657. MODULE_LICENSE("GPL");