sunxi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * Allwinner sun4i MUSB Glue Layer
  3. *
  4. * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * Based on code from
  7. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/clk.h>
  20. #include <linux/err.h>
  21. #include <linux/extcon.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/of.h>
  26. #include <linux/phy/phy-sun4i-usb.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/reset.h>
  29. #include <linux/soc/sunxi/sunxi_sram.h>
  30. #include <linux/usb/musb.h>
  31. #include <linux/usb/of.h>
  32. #include <linux/usb/usb_phy_generic.h>
  33. #include <linux/workqueue.h>
  34. #include "musb_core.h"
  35. /*
  36. * Register offsets, note sunxi musb has a different layout then most
  37. * musb implementations, we translate the layout in musb_readb & friends.
  38. */
  39. #define SUNXI_MUSB_POWER 0x0040
  40. #define SUNXI_MUSB_DEVCTL 0x0041
  41. #define SUNXI_MUSB_INDEX 0x0042
  42. #define SUNXI_MUSB_VEND0 0x0043
  43. #define SUNXI_MUSB_INTRTX 0x0044
  44. #define SUNXI_MUSB_INTRRX 0x0046
  45. #define SUNXI_MUSB_INTRTXE 0x0048
  46. #define SUNXI_MUSB_INTRRXE 0x004a
  47. #define SUNXI_MUSB_INTRUSB 0x004c
  48. #define SUNXI_MUSB_INTRUSBE 0x0050
  49. #define SUNXI_MUSB_FRAME 0x0054
  50. #define SUNXI_MUSB_TXFIFOSZ 0x0090
  51. #define SUNXI_MUSB_TXFIFOADD 0x0092
  52. #define SUNXI_MUSB_RXFIFOSZ 0x0094
  53. #define SUNXI_MUSB_RXFIFOADD 0x0096
  54. #define SUNXI_MUSB_FADDR 0x0098
  55. #define SUNXI_MUSB_TXFUNCADDR 0x0098
  56. #define SUNXI_MUSB_TXHUBADDR 0x009a
  57. #define SUNXI_MUSB_TXHUBPORT 0x009b
  58. #define SUNXI_MUSB_RXFUNCADDR 0x009c
  59. #define SUNXI_MUSB_RXHUBADDR 0x009e
  60. #define SUNXI_MUSB_RXHUBPORT 0x009f
  61. #define SUNXI_MUSB_CONFIGDATA 0x00c0
  62. /* VEND0 bits */
  63. #define SUNXI_MUSB_VEND0_PIO_MODE 0
  64. /* flags */
  65. #define SUNXI_MUSB_FL_ENABLED 0
  66. #define SUNXI_MUSB_FL_HOSTMODE 1
  67. #define SUNXI_MUSB_FL_HOSTMODE_PEND 2
  68. #define SUNXI_MUSB_FL_VBUS_ON 3
  69. #define SUNXI_MUSB_FL_PHY_ON 4
  70. #define SUNXI_MUSB_FL_HAS_SRAM 5
  71. #define SUNXI_MUSB_FL_HAS_RESET 6
  72. #define SUNXI_MUSB_FL_NO_CONFIGDATA 7
  73. #define SUNXI_MUSB_FL_PHY_MODE_PEND 8
  74. /* Our read/write methods need access and do not get passed in a musb ref :| */
  75. static struct musb *sunxi_musb;
  76. struct sunxi_glue {
  77. struct device *dev;
  78. struct musb *musb;
  79. struct platform_device *musb_pdev;
  80. struct clk *clk;
  81. struct reset_control *rst;
  82. struct phy *phy;
  83. struct platform_device *usb_phy;
  84. struct usb_phy *xceiv;
  85. enum phy_mode phy_mode;
  86. unsigned long flags;
  87. struct work_struct work;
  88. struct extcon_dev *extcon;
  89. struct notifier_block host_nb;
  90. };
  91. /* phy_power_on / off may sleep, so we use a workqueue */
  92. static void sunxi_musb_work(struct work_struct *work)
  93. {
  94. struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
  95. bool vbus_on, phy_on;
  96. if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
  97. return;
  98. if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
  99. struct musb *musb = glue->musb;
  100. unsigned long flags;
  101. u8 devctl;
  102. spin_lock_irqsave(&musb->lock, flags);
  103. devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
  104. if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
  105. set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  106. musb->xceiv->otg->default_a = 1;
  107. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  108. MUSB_HST_MODE(musb);
  109. devctl |= MUSB_DEVCTL_SESSION;
  110. } else {
  111. clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  112. musb->xceiv->otg->default_a = 0;
  113. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  114. MUSB_DEV_MODE(musb);
  115. devctl &= ~MUSB_DEVCTL_SESSION;
  116. }
  117. writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
  118. spin_unlock_irqrestore(&musb->lock, flags);
  119. }
  120. vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  121. phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  122. if (phy_on != vbus_on) {
  123. if (vbus_on) {
  124. phy_power_on(glue->phy);
  125. set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  126. } else {
  127. phy_power_off(glue->phy);
  128. clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  129. }
  130. }
  131. if (test_and_clear_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags))
  132. phy_set_mode(glue->phy, glue->phy_mode);
  133. }
  134. static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
  135. {
  136. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  137. if (is_on) {
  138. set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  139. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  140. } else {
  141. clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  142. }
  143. schedule_work(&glue->work);
  144. }
  145. static void sunxi_musb_pre_root_reset_end(struct musb *musb)
  146. {
  147. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  148. sun4i_usb_phy_set_squelch_detect(glue->phy, false);
  149. }
  150. static void sunxi_musb_post_root_reset_end(struct musb *musb)
  151. {
  152. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  153. sun4i_usb_phy_set_squelch_detect(glue->phy, true);
  154. }
  155. static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  156. {
  157. struct musb *musb = __hci;
  158. unsigned long flags;
  159. spin_lock_irqsave(&musb->lock, flags);
  160. musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
  161. if (musb->int_usb)
  162. writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
  163. if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
  164. /* ep0 FADDR must be 0 when (re)entering peripheral mode */
  165. musb_ep_select(musb->mregs, 0);
  166. musb_writeb(musb->mregs, MUSB_FADDR, 0);
  167. }
  168. musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
  169. if (musb->int_tx)
  170. writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
  171. musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
  172. if (musb->int_rx)
  173. writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
  174. musb_interrupt(musb);
  175. spin_unlock_irqrestore(&musb->lock, flags);
  176. return IRQ_HANDLED;
  177. }
  178. static int sunxi_musb_host_notifier(struct notifier_block *nb,
  179. unsigned long event, void *ptr)
  180. {
  181. struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
  182. if (event)
  183. set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
  184. else
  185. clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
  186. set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
  187. schedule_work(&glue->work);
  188. return NOTIFY_DONE;
  189. }
  190. static int sunxi_musb_init(struct musb *musb)
  191. {
  192. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  193. int ret;
  194. sunxi_musb = musb;
  195. musb->phy = glue->phy;
  196. musb->xceiv = glue->xceiv;
  197. if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags)) {
  198. ret = sunxi_sram_claim(musb->controller->parent);
  199. if (ret)
  200. return ret;
  201. }
  202. ret = clk_prepare_enable(glue->clk);
  203. if (ret)
  204. goto error_sram_release;
  205. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
  206. ret = reset_control_deassert(glue->rst);
  207. if (ret)
  208. goto error_clk_disable;
  209. }
  210. writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
  211. /* Register notifier before calling phy_init() */
  212. ret = devm_extcon_register_notifier(glue->dev, glue->extcon,
  213. EXTCON_USB_HOST, &glue->host_nb);
  214. if (ret)
  215. goto error_reset_assert;
  216. ret = phy_init(glue->phy);
  217. if (ret)
  218. goto error_reset_assert;
  219. musb->isr = sunxi_musb_interrupt;
  220. /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
  221. pm_runtime_get(musb->controller);
  222. return 0;
  223. error_reset_assert:
  224. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
  225. reset_control_assert(glue->rst);
  226. error_clk_disable:
  227. clk_disable_unprepare(glue->clk);
  228. error_sram_release:
  229. if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
  230. sunxi_sram_release(musb->controller->parent);
  231. return ret;
  232. }
  233. static int sunxi_musb_exit(struct musb *musb)
  234. {
  235. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  236. pm_runtime_put(musb->controller);
  237. cancel_work_sync(&glue->work);
  238. if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
  239. phy_power_off(glue->phy);
  240. phy_exit(glue->phy);
  241. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
  242. reset_control_assert(glue->rst);
  243. clk_disable_unprepare(glue->clk);
  244. if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
  245. sunxi_sram_release(musb->controller->parent);
  246. devm_usb_put_phy(glue->dev, glue->xceiv);
  247. return 0;
  248. }
  249. static void sunxi_musb_enable(struct musb *musb)
  250. {
  251. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  252. glue->musb = musb;
  253. /* musb_core does not call us in a balanced manner */
  254. if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
  255. return;
  256. schedule_work(&glue->work);
  257. }
  258. static void sunxi_musb_disable(struct musb *musb)
  259. {
  260. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  261. clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
  262. }
  263. static struct dma_controller *
  264. sunxi_musb_dma_controller_create(struct musb *musb, void __iomem *base)
  265. {
  266. return NULL;
  267. }
  268. static void sunxi_musb_dma_controller_destroy(struct dma_controller *c)
  269. {
  270. }
  271. static int sunxi_musb_set_mode(struct musb *musb, u8 mode)
  272. {
  273. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  274. enum phy_mode new_mode;
  275. switch (mode) {
  276. case MUSB_HOST:
  277. new_mode = PHY_MODE_USB_HOST;
  278. break;
  279. case MUSB_PERIPHERAL:
  280. new_mode = PHY_MODE_USB_DEVICE;
  281. break;
  282. case MUSB_OTG:
  283. new_mode = PHY_MODE_USB_OTG;
  284. break;
  285. default:
  286. dev_err(musb->controller->parent,
  287. "Error requested mode not supported by this kernel\n");
  288. return -EINVAL;
  289. }
  290. if (glue->phy_mode == new_mode)
  291. return 0;
  292. if (musb->port_mode != MUSB_PORT_MODE_DUAL_ROLE) {
  293. dev_err(musb->controller->parent,
  294. "Error changing modes is only supported in dual role mode\n");
  295. return -EINVAL;
  296. }
  297. if (musb->port1_status & USB_PORT_STAT_ENABLE)
  298. musb_root_disconnect(musb);
  299. /*
  300. * phy_set_mode may sleep, and we're called with a spinlock held,
  301. * so let sunxi_musb_work deal with it.
  302. */
  303. glue->phy_mode = new_mode;
  304. set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
  305. schedule_work(&glue->work);
  306. return 0;
  307. }
  308. static int sunxi_musb_recover(struct musb *musb)
  309. {
  310. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  311. /*
  312. * Schedule a phy_set_mode with the current glue->phy_mode value,
  313. * this will force end the current session.
  314. */
  315. set_bit(SUNXI_MUSB_FL_PHY_MODE_PEND, &glue->flags);
  316. schedule_work(&glue->work);
  317. return 0;
  318. }
  319. /*
  320. * sunxi musb register layout
  321. * 0x00 - 0x17 fifo regs, 1 long per fifo
  322. * 0x40 - 0x57 generic control regs (power - frame)
  323. * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
  324. * 0x90 - 0x97 fifo control regs (indexed)
  325. * 0x98 - 0x9f multipoint / busctl regs (indexed)
  326. * 0xc0 configdata reg
  327. */
  328. static u32 sunxi_musb_fifo_offset(u8 epnum)
  329. {
  330. return (epnum * 4);
  331. }
  332. static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
  333. {
  334. WARN_ONCE(offset != 0,
  335. "sunxi_musb_ep_offset called with non 0 offset\n");
  336. return 0x80; /* indexed, so ignore epnum */
  337. }
  338. static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
  339. {
  340. return SUNXI_MUSB_TXFUNCADDR + offset;
  341. }
  342. static u8 sunxi_musb_readb(const void __iomem *addr, unsigned offset)
  343. {
  344. struct sunxi_glue *glue;
  345. if (addr == sunxi_musb->mregs) {
  346. /* generic control or fifo control reg access */
  347. switch (offset) {
  348. case MUSB_FADDR:
  349. return readb(addr + SUNXI_MUSB_FADDR);
  350. case MUSB_POWER:
  351. return readb(addr + SUNXI_MUSB_POWER);
  352. case MUSB_INTRUSB:
  353. return readb(addr + SUNXI_MUSB_INTRUSB);
  354. case MUSB_INTRUSBE:
  355. return readb(addr + SUNXI_MUSB_INTRUSBE);
  356. case MUSB_INDEX:
  357. return readb(addr + SUNXI_MUSB_INDEX);
  358. case MUSB_TESTMODE:
  359. return 0; /* No testmode on sunxi */
  360. case MUSB_DEVCTL:
  361. return readb(addr + SUNXI_MUSB_DEVCTL);
  362. case MUSB_TXFIFOSZ:
  363. return readb(addr + SUNXI_MUSB_TXFIFOSZ);
  364. case MUSB_RXFIFOSZ:
  365. return readb(addr + SUNXI_MUSB_RXFIFOSZ);
  366. case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
  367. glue = dev_get_drvdata(sunxi_musb->controller->parent);
  368. /* A33 saves a reg, and we get to hardcode this */
  369. if (test_bit(SUNXI_MUSB_FL_NO_CONFIGDATA,
  370. &glue->flags))
  371. return 0xde;
  372. return readb(addr + SUNXI_MUSB_CONFIGDATA);
  373. /* Offset for these is fixed by sunxi_musb_busctl_offset() */
  374. case SUNXI_MUSB_TXFUNCADDR:
  375. case SUNXI_MUSB_TXHUBADDR:
  376. case SUNXI_MUSB_TXHUBPORT:
  377. case SUNXI_MUSB_RXFUNCADDR:
  378. case SUNXI_MUSB_RXHUBADDR:
  379. case SUNXI_MUSB_RXHUBPORT:
  380. /* multipoint / busctl reg access */
  381. return readb(addr + offset);
  382. default:
  383. dev_err(sunxi_musb->controller->parent,
  384. "Error unknown readb offset %u\n", offset);
  385. return 0;
  386. }
  387. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  388. /* ep control reg access */
  389. /* sunxi has a 2 byte hole before the txtype register */
  390. if (offset >= MUSB_TXTYPE)
  391. offset += 2;
  392. return readb(addr + offset);
  393. }
  394. dev_err(sunxi_musb->controller->parent,
  395. "Error unknown readb at 0x%x bytes offset\n",
  396. (int)(addr - sunxi_musb->mregs));
  397. return 0;
  398. }
  399. static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
  400. {
  401. if (addr == sunxi_musb->mregs) {
  402. /* generic control or fifo control reg access */
  403. switch (offset) {
  404. case MUSB_FADDR:
  405. return writeb(data, addr + SUNXI_MUSB_FADDR);
  406. case MUSB_POWER:
  407. return writeb(data, addr + SUNXI_MUSB_POWER);
  408. case MUSB_INTRUSB:
  409. return writeb(data, addr + SUNXI_MUSB_INTRUSB);
  410. case MUSB_INTRUSBE:
  411. return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
  412. case MUSB_INDEX:
  413. return writeb(data, addr + SUNXI_MUSB_INDEX);
  414. case MUSB_TESTMODE:
  415. if (data)
  416. dev_warn(sunxi_musb->controller->parent,
  417. "sunxi-musb does not have testmode\n");
  418. return;
  419. case MUSB_DEVCTL:
  420. return writeb(data, addr + SUNXI_MUSB_DEVCTL);
  421. case MUSB_TXFIFOSZ:
  422. return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
  423. case MUSB_RXFIFOSZ:
  424. return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
  425. /* Offset for these is fixed by sunxi_musb_busctl_offset() */
  426. case SUNXI_MUSB_TXFUNCADDR:
  427. case SUNXI_MUSB_TXHUBADDR:
  428. case SUNXI_MUSB_TXHUBPORT:
  429. case SUNXI_MUSB_RXFUNCADDR:
  430. case SUNXI_MUSB_RXHUBADDR:
  431. case SUNXI_MUSB_RXHUBPORT:
  432. /* multipoint / busctl reg access */
  433. return writeb(data, addr + offset);
  434. default:
  435. dev_err(sunxi_musb->controller->parent,
  436. "Error unknown writeb offset %u\n", offset);
  437. return;
  438. }
  439. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  440. /* ep control reg access */
  441. if (offset >= MUSB_TXTYPE)
  442. offset += 2;
  443. return writeb(data, addr + offset);
  444. }
  445. dev_err(sunxi_musb->controller->parent,
  446. "Error unknown writeb at 0x%x bytes offset\n",
  447. (int)(addr - sunxi_musb->mregs));
  448. }
  449. static u16 sunxi_musb_readw(const void __iomem *addr, unsigned offset)
  450. {
  451. if (addr == sunxi_musb->mregs) {
  452. /* generic control or fifo control reg access */
  453. switch (offset) {
  454. case MUSB_INTRTX:
  455. return readw(addr + SUNXI_MUSB_INTRTX);
  456. case MUSB_INTRRX:
  457. return readw(addr + SUNXI_MUSB_INTRRX);
  458. case MUSB_INTRTXE:
  459. return readw(addr + SUNXI_MUSB_INTRTXE);
  460. case MUSB_INTRRXE:
  461. return readw(addr + SUNXI_MUSB_INTRRXE);
  462. case MUSB_FRAME:
  463. return readw(addr + SUNXI_MUSB_FRAME);
  464. case MUSB_TXFIFOADD:
  465. return readw(addr + SUNXI_MUSB_TXFIFOADD);
  466. case MUSB_RXFIFOADD:
  467. return readw(addr + SUNXI_MUSB_RXFIFOADD);
  468. case MUSB_HWVERS:
  469. return 0; /* sunxi musb version is not known */
  470. default:
  471. dev_err(sunxi_musb->controller->parent,
  472. "Error unknown readw offset %u\n", offset);
  473. return 0;
  474. }
  475. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  476. /* ep control reg access */
  477. return readw(addr + offset);
  478. }
  479. dev_err(sunxi_musb->controller->parent,
  480. "Error unknown readw at 0x%x bytes offset\n",
  481. (int)(addr - sunxi_musb->mregs));
  482. return 0;
  483. }
  484. static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
  485. {
  486. if (addr == sunxi_musb->mregs) {
  487. /* generic control or fifo control reg access */
  488. switch (offset) {
  489. case MUSB_INTRTX:
  490. return writew(data, addr + SUNXI_MUSB_INTRTX);
  491. case MUSB_INTRRX:
  492. return writew(data, addr + SUNXI_MUSB_INTRRX);
  493. case MUSB_INTRTXE:
  494. return writew(data, addr + SUNXI_MUSB_INTRTXE);
  495. case MUSB_INTRRXE:
  496. return writew(data, addr + SUNXI_MUSB_INTRRXE);
  497. case MUSB_FRAME:
  498. return writew(data, addr + SUNXI_MUSB_FRAME);
  499. case MUSB_TXFIFOADD:
  500. return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
  501. case MUSB_RXFIFOADD:
  502. return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
  503. default:
  504. dev_err(sunxi_musb->controller->parent,
  505. "Error unknown writew offset %u\n", offset);
  506. return;
  507. }
  508. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  509. /* ep control reg access */
  510. return writew(data, addr + offset);
  511. }
  512. dev_err(sunxi_musb->controller->parent,
  513. "Error unknown writew at 0x%x bytes offset\n",
  514. (int)(addr - sunxi_musb->mregs));
  515. }
  516. static const struct musb_platform_ops sunxi_musb_ops = {
  517. .quirks = MUSB_INDEXED_EP,
  518. .init = sunxi_musb_init,
  519. .exit = sunxi_musb_exit,
  520. .enable = sunxi_musb_enable,
  521. .disable = sunxi_musb_disable,
  522. .fifo_offset = sunxi_musb_fifo_offset,
  523. .ep_offset = sunxi_musb_ep_offset,
  524. .busctl_offset = sunxi_musb_busctl_offset,
  525. .readb = sunxi_musb_readb,
  526. .writeb = sunxi_musb_writeb,
  527. .readw = sunxi_musb_readw,
  528. .writew = sunxi_musb_writew,
  529. .dma_init = sunxi_musb_dma_controller_create,
  530. .dma_exit = sunxi_musb_dma_controller_destroy,
  531. .set_mode = sunxi_musb_set_mode,
  532. .recover = sunxi_musb_recover,
  533. .set_vbus = sunxi_musb_set_vbus,
  534. .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
  535. .post_root_reset_end = sunxi_musb_post_root_reset_end,
  536. };
  537. /* Allwinner OTG supports up to 5 endpoints */
  538. #define SUNXI_MUSB_MAX_EP_NUM 6
  539. #define SUNXI_MUSB_RAM_BITS 11
  540. static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
  541. MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
  542. MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
  543. MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
  544. MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
  545. MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
  546. MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
  547. MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
  548. MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
  549. MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
  550. MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
  551. };
  552. /* H3/V3s OTG supports only 4 endpoints */
  553. #define SUNXI_MUSB_MAX_EP_NUM_H3 5
  554. static struct musb_fifo_cfg sunxi_musb_mode_cfg_h3[] = {
  555. MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
  556. MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
  557. MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
  558. MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
  559. MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
  560. MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
  561. MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
  562. MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
  563. };
  564. static const struct musb_hdrc_config sunxi_musb_hdrc_config = {
  565. .fifo_cfg = sunxi_musb_mode_cfg,
  566. .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
  567. .multipoint = true,
  568. .dyn_fifo = true,
  569. .soft_con = true,
  570. .num_eps = SUNXI_MUSB_MAX_EP_NUM,
  571. .ram_bits = SUNXI_MUSB_RAM_BITS,
  572. .dma = 0,
  573. };
  574. static struct musb_hdrc_config sunxi_musb_hdrc_config_h3 = {
  575. .fifo_cfg = sunxi_musb_mode_cfg_h3,
  576. .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg_h3),
  577. .multipoint = true,
  578. .dyn_fifo = true,
  579. .soft_con = true,
  580. .num_eps = SUNXI_MUSB_MAX_EP_NUM_H3,
  581. .ram_bits = SUNXI_MUSB_RAM_BITS,
  582. .dma = 0,
  583. };
  584. static int sunxi_musb_probe(struct platform_device *pdev)
  585. {
  586. struct musb_hdrc_platform_data pdata;
  587. struct platform_device_info pinfo;
  588. struct sunxi_glue *glue;
  589. struct device_node *np = pdev->dev.of_node;
  590. int ret;
  591. if (!np) {
  592. dev_err(&pdev->dev, "Error no device tree node found\n");
  593. return -EINVAL;
  594. }
  595. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  596. if (!glue)
  597. return -ENOMEM;
  598. memset(&pdata, 0, sizeof(pdata));
  599. switch (usb_get_dr_mode(&pdev->dev)) {
  600. #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
  601. case USB_DR_MODE_HOST:
  602. pdata.mode = MUSB_PORT_MODE_HOST;
  603. glue->phy_mode = PHY_MODE_USB_HOST;
  604. break;
  605. #endif
  606. #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_GADGET
  607. case USB_DR_MODE_PERIPHERAL:
  608. pdata.mode = MUSB_PORT_MODE_GADGET;
  609. glue->phy_mode = PHY_MODE_USB_DEVICE;
  610. break;
  611. #endif
  612. #ifdef CONFIG_USB_MUSB_DUAL_ROLE
  613. case USB_DR_MODE_OTG:
  614. pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
  615. glue->phy_mode = PHY_MODE_USB_OTG;
  616. break;
  617. #endif
  618. default:
  619. dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
  620. return -EINVAL;
  621. }
  622. pdata.platform_ops = &sunxi_musb_ops;
  623. if (!of_device_is_compatible(np, "allwinner,sun8i-h3-musb"))
  624. pdata.config = &sunxi_musb_hdrc_config;
  625. else
  626. pdata.config = &sunxi_musb_hdrc_config_h3;
  627. glue->dev = &pdev->dev;
  628. INIT_WORK(&glue->work, sunxi_musb_work);
  629. glue->host_nb.notifier_call = sunxi_musb_host_notifier;
  630. if (of_device_is_compatible(np, "allwinner,sun4i-a10-musb"))
  631. set_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags);
  632. if (of_device_is_compatible(np, "allwinner,sun6i-a31-musb"))
  633. set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
  634. if (of_device_is_compatible(np, "allwinner,sun8i-a33-musb") ||
  635. of_device_is_compatible(np, "allwinner,sun8i-h3-musb")) {
  636. set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
  637. set_bit(SUNXI_MUSB_FL_NO_CONFIGDATA, &glue->flags);
  638. }
  639. glue->clk = devm_clk_get(&pdev->dev, NULL);
  640. if (IS_ERR(glue->clk)) {
  641. dev_err(&pdev->dev, "Error getting clock: %ld\n",
  642. PTR_ERR(glue->clk));
  643. return PTR_ERR(glue->clk);
  644. }
  645. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
  646. glue->rst = devm_reset_control_get(&pdev->dev, NULL);
  647. if (IS_ERR(glue->rst)) {
  648. if (PTR_ERR(glue->rst) == -EPROBE_DEFER)
  649. return -EPROBE_DEFER;
  650. dev_err(&pdev->dev, "Error getting reset %ld\n",
  651. PTR_ERR(glue->rst));
  652. return PTR_ERR(glue->rst);
  653. }
  654. }
  655. glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
  656. if (IS_ERR(glue->extcon)) {
  657. if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
  658. return -EPROBE_DEFER;
  659. dev_err(&pdev->dev, "Invalid or missing extcon\n");
  660. return PTR_ERR(glue->extcon);
  661. }
  662. glue->phy = devm_phy_get(&pdev->dev, "usb");
  663. if (IS_ERR(glue->phy)) {
  664. if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
  665. return -EPROBE_DEFER;
  666. dev_err(&pdev->dev, "Error getting phy %ld\n",
  667. PTR_ERR(glue->phy));
  668. return PTR_ERR(glue->phy);
  669. }
  670. glue->usb_phy = usb_phy_generic_register();
  671. if (IS_ERR(glue->usb_phy)) {
  672. dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
  673. PTR_ERR(glue->usb_phy));
  674. return PTR_ERR(glue->usb_phy);
  675. }
  676. glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  677. if (IS_ERR(glue->xceiv)) {
  678. ret = PTR_ERR(glue->xceiv);
  679. dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
  680. goto err_unregister_usb_phy;
  681. }
  682. platform_set_drvdata(pdev, glue);
  683. memset(&pinfo, 0, sizeof(pinfo));
  684. pinfo.name = "musb-hdrc";
  685. pinfo.id = PLATFORM_DEVID_AUTO;
  686. pinfo.parent = &pdev->dev;
  687. pinfo.res = pdev->resource;
  688. pinfo.num_res = pdev->num_resources;
  689. pinfo.data = &pdata;
  690. pinfo.size_data = sizeof(pdata);
  691. glue->musb_pdev = platform_device_register_full(&pinfo);
  692. if (IS_ERR(glue->musb_pdev)) {
  693. ret = PTR_ERR(glue->musb_pdev);
  694. dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
  695. goto err_unregister_usb_phy;
  696. }
  697. return 0;
  698. err_unregister_usb_phy:
  699. usb_phy_generic_unregister(glue->usb_phy);
  700. return ret;
  701. }
  702. static int sunxi_musb_remove(struct platform_device *pdev)
  703. {
  704. struct sunxi_glue *glue = platform_get_drvdata(pdev);
  705. struct platform_device *usb_phy = glue->usb_phy;
  706. platform_device_unregister(glue->musb_pdev);
  707. usb_phy_generic_unregister(usb_phy);
  708. return 0;
  709. }
  710. static const struct of_device_id sunxi_musb_match[] = {
  711. { .compatible = "allwinner,sun4i-a10-musb", },
  712. { .compatible = "allwinner,sun6i-a31-musb", },
  713. { .compatible = "allwinner,sun8i-a33-musb", },
  714. { .compatible = "allwinner,sun8i-h3-musb", },
  715. {}
  716. };
  717. MODULE_DEVICE_TABLE(of, sunxi_musb_match);
  718. static struct platform_driver sunxi_musb_driver = {
  719. .probe = sunxi_musb_probe,
  720. .remove = sunxi_musb_remove,
  721. .driver = {
  722. .name = "musb-sunxi",
  723. .of_match_table = sunxi_musb_match,
  724. },
  725. };
  726. module_platform_driver(sunxi_musb_driver);
  727. MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
  728. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  729. MODULE_LICENSE("GPL v2");