sunxi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. /* Our read/write methods need access and do not get passed in a musb ref :| */
  74. static struct musb *sunxi_musb;
  75. struct sunxi_glue {
  76. struct device *dev;
  77. struct platform_device *musb;
  78. struct clk *clk;
  79. struct reset_control *rst;
  80. struct phy *phy;
  81. struct platform_device *usb_phy;
  82. struct usb_phy *xceiv;
  83. unsigned long flags;
  84. struct work_struct work;
  85. struct extcon_dev *extcon;
  86. struct notifier_block host_nb;
  87. };
  88. /* phy_power_on / off may sleep, so we use a workqueue */
  89. static void sunxi_musb_work(struct work_struct *work)
  90. {
  91. struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
  92. bool vbus_on, phy_on;
  93. if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
  94. return;
  95. if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
  96. struct musb *musb = platform_get_drvdata(glue->musb);
  97. unsigned long flags;
  98. u8 devctl;
  99. spin_lock_irqsave(&musb->lock, flags);
  100. devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
  101. if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
  102. set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  103. musb->xceiv->otg->default_a = 1;
  104. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  105. MUSB_HST_MODE(musb);
  106. devctl |= MUSB_DEVCTL_SESSION;
  107. } else {
  108. clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  109. musb->xceiv->otg->default_a = 0;
  110. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  111. MUSB_DEV_MODE(musb);
  112. devctl &= ~MUSB_DEVCTL_SESSION;
  113. }
  114. writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
  115. spin_unlock_irqrestore(&musb->lock, flags);
  116. }
  117. vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  118. phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  119. if (phy_on != vbus_on) {
  120. if (vbus_on) {
  121. phy_power_on(glue->phy);
  122. set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  123. } else {
  124. phy_power_off(glue->phy);
  125. clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  126. }
  127. }
  128. }
  129. static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
  130. {
  131. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  132. if (is_on)
  133. set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  134. else
  135. clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  136. schedule_work(&glue->work);
  137. }
  138. static void sunxi_musb_pre_root_reset_end(struct musb *musb)
  139. {
  140. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  141. sun4i_usb_phy_set_squelch_detect(glue->phy, false);
  142. }
  143. static void sunxi_musb_post_root_reset_end(struct musb *musb)
  144. {
  145. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  146. sun4i_usb_phy_set_squelch_detect(glue->phy, true);
  147. }
  148. static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  149. {
  150. struct musb *musb = __hci;
  151. unsigned long flags;
  152. spin_lock_irqsave(&musb->lock, flags);
  153. musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
  154. if (musb->int_usb)
  155. writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
  156. /*
  157. * sunxi musb often signals babble on low / full speed device
  158. * disconnect, without ever raising MUSB_INTR_DISCONNECT, since
  159. * normally babble never happens treat it as disconnect.
  160. */
  161. if ((musb->int_usb & MUSB_INTR_BABBLE) && is_host_active(musb)) {
  162. musb->int_usb &= ~MUSB_INTR_BABBLE;
  163. musb->int_usb |= MUSB_INTR_DISCONNECT;
  164. }
  165. if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
  166. /* ep0 FADDR must be 0 when (re)entering peripheral mode */
  167. musb_ep_select(musb->mregs, 0);
  168. musb_writeb(musb->mregs, MUSB_FADDR, 0);
  169. }
  170. musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
  171. if (musb->int_tx)
  172. writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
  173. musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
  174. if (musb->int_rx)
  175. writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
  176. musb_interrupt(musb);
  177. spin_unlock_irqrestore(&musb->lock, flags);
  178. return IRQ_HANDLED;
  179. }
  180. static int sunxi_musb_host_notifier(struct notifier_block *nb,
  181. unsigned long event, void *ptr)
  182. {
  183. struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
  184. if (event)
  185. set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
  186. else
  187. clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
  188. set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
  189. schedule_work(&glue->work);
  190. return NOTIFY_DONE;
  191. }
  192. static int sunxi_musb_init(struct musb *musb)
  193. {
  194. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  195. int ret;
  196. sunxi_musb = musb;
  197. musb->phy = glue->phy;
  198. musb->xceiv = glue->xceiv;
  199. if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags)) {
  200. ret = sunxi_sram_claim(musb->controller->parent);
  201. if (ret)
  202. return ret;
  203. }
  204. ret = clk_prepare_enable(glue->clk);
  205. if (ret)
  206. goto error_sram_release;
  207. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
  208. ret = reset_control_deassert(glue->rst);
  209. if (ret)
  210. goto error_clk_disable;
  211. }
  212. writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
  213. /* Register notifier before calling phy_init() */
  214. if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) {
  215. ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
  216. &glue->host_nb);
  217. if (ret)
  218. goto error_reset_assert;
  219. }
  220. ret = phy_init(glue->phy);
  221. if (ret)
  222. goto error_unregister_notifier;
  223. if (musb->port_mode == MUSB_PORT_MODE_HOST) {
  224. ret = phy_power_on(glue->phy);
  225. if (ret)
  226. goto error_phy_exit;
  227. set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  228. /* Stop musb work from turning vbus off again */
  229. set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  230. }
  231. musb->isr = sunxi_musb_interrupt;
  232. /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
  233. pm_runtime_get(musb->controller);
  234. return 0;
  235. error_phy_exit:
  236. phy_exit(glue->phy);
  237. error_unregister_notifier:
  238. if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  239. extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
  240. &glue->host_nb);
  241. error_reset_assert:
  242. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
  243. reset_control_assert(glue->rst);
  244. error_clk_disable:
  245. clk_disable_unprepare(glue->clk);
  246. error_sram_release:
  247. if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
  248. sunxi_sram_release(musb->controller->parent);
  249. return ret;
  250. }
  251. static int sunxi_musb_exit(struct musb *musb)
  252. {
  253. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  254. pm_runtime_put(musb->controller);
  255. cancel_work_sync(&glue->work);
  256. if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
  257. phy_power_off(glue->phy);
  258. phy_exit(glue->phy);
  259. if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  260. extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
  261. &glue->host_nb);
  262. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags))
  263. reset_control_assert(glue->rst);
  264. clk_disable_unprepare(glue->clk);
  265. if (test_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags))
  266. sunxi_sram_release(musb->controller->parent);
  267. devm_usb_put_phy(glue->dev, glue->xceiv);
  268. return 0;
  269. }
  270. static void sunxi_musb_enable(struct musb *musb)
  271. {
  272. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  273. /* musb_core does not call us in a balanced manner */
  274. if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
  275. return;
  276. schedule_work(&glue->work);
  277. }
  278. static void sunxi_musb_disable(struct musb *musb)
  279. {
  280. struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  281. clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
  282. }
  283. struct dma_controller *sunxi_musb_dma_controller_create(struct musb *musb,
  284. void __iomem *base)
  285. {
  286. return NULL;
  287. }
  288. void sunxi_musb_dma_controller_destroy(struct dma_controller *c)
  289. {
  290. }
  291. /*
  292. * sunxi musb register layout
  293. * 0x00 - 0x17 fifo regs, 1 long per fifo
  294. * 0x40 - 0x57 generic control regs (power - frame)
  295. * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
  296. * 0x90 - 0x97 fifo control regs (indexed)
  297. * 0x98 - 0x9f multipoint / busctl regs (indexed)
  298. * 0xc0 configdata reg
  299. */
  300. static u32 sunxi_musb_fifo_offset(u8 epnum)
  301. {
  302. return (epnum * 4);
  303. }
  304. static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
  305. {
  306. WARN_ONCE(offset != 0,
  307. "sunxi_musb_ep_offset called with non 0 offset\n");
  308. return 0x80; /* indexed, so ignore epnum */
  309. }
  310. static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
  311. {
  312. return SUNXI_MUSB_TXFUNCADDR + offset;
  313. }
  314. static u8 sunxi_musb_readb(const void __iomem *addr, unsigned offset)
  315. {
  316. struct sunxi_glue *glue;
  317. if (addr == sunxi_musb->mregs) {
  318. /* generic control or fifo control reg access */
  319. switch (offset) {
  320. case MUSB_FADDR:
  321. return readb(addr + SUNXI_MUSB_FADDR);
  322. case MUSB_POWER:
  323. return readb(addr + SUNXI_MUSB_POWER);
  324. case MUSB_INTRUSB:
  325. return readb(addr + SUNXI_MUSB_INTRUSB);
  326. case MUSB_INTRUSBE:
  327. return readb(addr + SUNXI_MUSB_INTRUSBE);
  328. case MUSB_INDEX:
  329. return readb(addr + SUNXI_MUSB_INDEX);
  330. case MUSB_TESTMODE:
  331. return 0; /* No testmode on sunxi */
  332. case MUSB_DEVCTL:
  333. return readb(addr + SUNXI_MUSB_DEVCTL);
  334. case MUSB_TXFIFOSZ:
  335. return readb(addr + SUNXI_MUSB_TXFIFOSZ);
  336. case MUSB_RXFIFOSZ:
  337. return readb(addr + SUNXI_MUSB_RXFIFOSZ);
  338. case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
  339. glue = dev_get_drvdata(sunxi_musb->controller->parent);
  340. /* A33 saves a reg, and we get to hardcode this */
  341. if (test_bit(SUNXI_MUSB_FL_NO_CONFIGDATA,
  342. &glue->flags))
  343. return 0xde;
  344. return readb(addr + SUNXI_MUSB_CONFIGDATA);
  345. /* Offset for these is fixed by sunxi_musb_busctl_offset() */
  346. case SUNXI_MUSB_TXFUNCADDR:
  347. case SUNXI_MUSB_TXHUBADDR:
  348. case SUNXI_MUSB_TXHUBPORT:
  349. case SUNXI_MUSB_RXFUNCADDR:
  350. case SUNXI_MUSB_RXHUBADDR:
  351. case SUNXI_MUSB_RXHUBPORT:
  352. /* multipoint / busctl reg access */
  353. return readb(addr + offset);
  354. default:
  355. dev_err(sunxi_musb->controller->parent,
  356. "Error unknown readb offset %u\n", offset);
  357. return 0;
  358. }
  359. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  360. /* ep control reg access */
  361. /* sunxi has a 2 byte hole before the txtype register */
  362. if (offset >= MUSB_TXTYPE)
  363. offset += 2;
  364. return readb(addr + offset);
  365. }
  366. dev_err(sunxi_musb->controller->parent,
  367. "Error unknown readb at 0x%x bytes offset\n",
  368. (int)(addr - sunxi_musb->mregs));
  369. return 0;
  370. }
  371. static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
  372. {
  373. if (addr == sunxi_musb->mregs) {
  374. /* generic control or fifo control reg access */
  375. switch (offset) {
  376. case MUSB_FADDR:
  377. return writeb(data, addr + SUNXI_MUSB_FADDR);
  378. case MUSB_POWER:
  379. return writeb(data, addr + SUNXI_MUSB_POWER);
  380. case MUSB_INTRUSB:
  381. return writeb(data, addr + SUNXI_MUSB_INTRUSB);
  382. case MUSB_INTRUSBE:
  383. return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
  384. case MUSB_INDEX:
  385. return writeb(data, addr + SUNXI_MUSB_INDEX);
  386. case MUSB_TESTMODE:
  387. if (data)
  388. dev_warn(sunxi_musb->controller->parent,
  389. "sunxi-musb does not have testmode\n");
  390. return;
  391. case MUSB_DEVCTL:
  392. return writeb(data, addr + SUNXI_MUSB_DEVCTL);
  393. case MUSB_TXFIFOSZ:
  394. return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
  395. case MUSB_RXFIFOSZ:
  396. return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
  397. /* Offset for these is fixed by sunxi_musb_busctl_offset() */
  398. case SUNXI_MUSB_TXFUNCADDR:
  399. case SUNXI_MUSB_TXHUBADDR:
  400. case SUNXI_MUSB_TXHUBPORT:
  401. case SUNXI_MUSB_RXFUNCADDR:
  402. case SUNXI_MUSB_RXHUBADDR:
  403. case SUNXI_MUSB_RXHUBPORT:
  404. /* multipoint / busctl reg access */
  405. return writeb(data, addr + offset);
  406. default:
  407. dev_err(sunxi_musb->controller->parent,
  408. "Error unknown writeb offset %u\n", offset);
  409. return;
  410. }
  411. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  412. /* ep control reg access */
  413. if (offset >= MUSB_TXTYPE)
  414. offset += 2;
  415. return writeb(data, addr + offset);
  416. }
  417. dev_err(sunxi_musb->controller->parent,
  418. "Error unknown writeb at 0x%x bytes offset\n",
  419. (int)(addr - sunxi_musb->mregs));
  420. }
  421. static u16 sunxi_musb_readw(const void __iomem *addr, unsigned offset)
  422. {
  423. if (addr == sunxi_musb->mregs) {
  424. /* generic control or fifo control reg access */
  425. switch (offset) {
  426. case MUSB_INTRTX:
  427. return readw(addr + SUNXI_MUSB_INTRTX);
  428. case MUSB_INTRRX:
  429. return readw(addr + SUNXI_MUSB_INTRRX);
  430. case MUSB_INTRTXE:
  431. return readw(addr + SUNXI_MUSB_INTRTXE);
  432. case MUSB_INTRRXE:
  433. return readw(addr + SUNXI_MUSB_INTRRXE);
  434. case MUSB_FRAME:
  435. return readw(addr + SUNXI_MUSB_FRAME);
  436. case MUSB_TXFIFOADD:
  437. return readw(addr + SUNXI_MUSB_TXFIFOADD);
  438. case MUSB_RXFIFOADD:
  439. return readw(addr + SUNXI_MUSB_RXFIFOADD);
  440. case MUSB_HWVERS:
  441. return 0; /* sunxi musb version is not known */
  442. default:
  443. dev_err(sunxi_musb->controller->parent,
  444. "Error unknown readw offset %u\n", offset);
  445. return 0;
  446. }
  447. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  448. /* ep control reg access */
  449. return readw(addr + offset);
  450. }
  451. dev_err(sunxi_musb->controller->parent,
  452. "Error unknown readw at 0x%x bytes offset\n",
  453. (int)(addr - sunxi_musb->mregs));
  454. return 0;
  455. }
  456. static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
  457. {
  458. if (addr == sunxi_musb->mregs) {
  459. /* generic control or fifo control reg access */
  460. switch (offset) {
  461. case MUSB_INTRTX:
  462. return writew(data, addr + SUNXI_MUSB_INTRTX);
  463. case MUSB_INTRRX:
  464. return writew(data, addr + SUNXI_MUSB_INTRRX);
  465. case MUSB_INTRTXE:
  466. return writew(data, addr + SUNXI_MUSB_INTRTXE);
  467. case MUSB_INTRRXE:
  468. return writew(data, addr + SUNXI_MUSB_INTRRXE);
  469. case MUSB_FRAME:
  470. return writew(data, addr + SUNXI_MUSB_FRAME);
  471. case MUSB_TXFIFOADD:
  472. return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
  473. case MUSB_RXFIFOADD:
  474. return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
  475. default:
  476. dev_err(sunxi_musb->controller->parent,
  477. "Error unknown writew offset %u\n", offset);
  478. return;
  479. }
  480. } else if (addr == (sunxi_musb->mregs + 0x80)) {
  481. /* ep control reg access */
  482. return writew(data, addr + offset);
  483. }
  484. dev_err(sunxi_musb->controller->parent,
  485. "Error unknown writew at 0x%x bytes offset\n",
  486. (int)(addr - sunxi_musb->mregs));
  487. }
  488. static const struct musb_platform_ops sunxi_musb_ops = {
  489. .quirks = MUSB_INDEXED_EP,
  490. .init = sunxi_musb_init,
  491. .exit = sunxi_musb_exit,
  492. .enable = sunxi_musb_enable,
  493. .disable = sunxi_musb_disable,
  494. .fifo_offset = sunxi_musb_fifo_offset,
  495. .ep_offset = sunxi_musb_ep_offset,
  496. .busctl_offset = sunxi_musb_busctl_offset,
  497. .readb = sunxi_musb_readb,
  498. .writeb = sunxi_musb_writeb,
  499. .readw = sunxi_musb_readw,
  500. .writew = sunxi_musb_writew,
  501. .dma_init = sunxi_musb_dma_controller_create,
  502. .dma_exit = sunxi_musb_dma_controller_destroy,
  503. .set_vbus = sunxi_musb_set_vbus,
  504. .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
  505. .post_root_reset_end = sunxi_musb_post_root_reset_end,
  506. };
  507. /* Allwinner OTG supports up to 5 endpoints */
  508. #define SUNXI_MUSB_MAX_EP_NUM 6
  509. #define SUNXI_MUSB_RAM_BITS 11
  510. static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
  511. MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
  512. MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
  513. MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
  514. MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
  515. MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
  516. MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
  517. MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
  518. MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
  519. MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
  520. MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
  521. };
  522. static struct musb_hdrc_config sunxi_musb_hdrc_config = {
  523. .fifo_cfg = sunxi_musb_mode_cfg,
  524. .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
  525. .multipoint = true,
  526. .dyn_fifo = true,
  527. .soft_con = true,
  528. .num_eps = SUNXI_MUSB_MAX_EP_NUM,
  529. .ram_bits = SUNXI_MUSB_RAM_BITS,
  530. .dma = 0,
  531. };
  532. static int sunxi_musb_probe(struct platform_device *pdev)
  533. {
  534. struct musb_hdrc_platform_data pdata;
  535. struct platform_device_info pinfo;
  536. struct sunxi_glue *glue;
  537. struct device_node *np = pdev->dev.of_node;
  538. int ret;
  539. if (!np) {
  540. dev_err(&pdev->dev, "Error no device tree node found\n");
  541. return -EINVAL;
  542. }
  543. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  544. if (!glue)
  545. return -ENOMEM;
  546. memset(&pdata, 0, sizeof(pdata));
  547. switch (usb_get_dr_mode(&pdev->dev)) {
  548. #if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
  549. case USB_DR_MODE_HOST:
  550. pdata.mode = MUSB_PORT_MODE_HOST;
  551. break;
  552. #endif
  553. #ifdef CONFIG_USB_MUSB_DUAL_ROLE
  554. case USB_DR_MODE_OTG:
  555. glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
  556. if (IS_ERR(glue->extcon)) {
  557. if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
  558. return -EPROBE_DEFER;
  559. dev_err(&pdev->dev, "Invalid or missing extcon\n");
  560. return PTR_ERR(glue->extcon);
  561. }
  562. pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
  563. break;
  564. #endif
  565. default:
  566. dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
  567. return -EINVAL;
  568. }
  569. pdata.platform_ops = &sunxi_musb_ops;
  570. pdata.config = &sunxi_musb_hdrc_config;
  571. glue->dev = &pdev->dev;
  572. INIT_WORK(&glue->work, sunxi_musb_work);
  573. glue->host_nb.notifier_call = sunxi_musb_host_notifier;
  574. if (of_device_is_compatible(np, "allwinner,sun4i-a10-musb"))
  575. set_bit(SUNXI_MUSB_FL_HAS_SRAM, &glue->flags);
  576. if (of_device_is_compatible(np, "allwinner,sun6i-a31-musb"))
  577. set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
  578. if (of_device_is_compatible(np, "allwinner,sun8i-a33-musb")) {
  579. set_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags);
  580. set_bit(SUNXI_MUSB_FL_NO_CONFIGDATA, &glue->flags);
  581. }
  582. glue->clk = devm_clk_get(&pdev->dev, NULL);
  583. if (IS_ERR(glue->clk)) {
  584. dev_err(&pdev->dev, "Error getting clock: %ld\n",
  585. PTR_ERR(glue->clk));
  586. return PTR_ERR(glue->clk);
  587. }
  588. if (test_bit(SUNXI_MUSB_FL_HAS_RESET, &glue->flags)) {
  589. glue->rst = devm_reset_control_get(&pdev->dev, NULL);
  590. if (IS_ERR(glue->rst)) {
  591. if (PTR_ERR(glue->rst) == -EPROBE_DEFER)
  592. return -EPROBE_DEFER;
  593. dev_err(&pdev->dev, "Error getting reset %ld\n",
  594. PTR_ERR(glue->rst));
  595. return PTR_ERR(glue->rst);
  596. }
  597. }
  598. glue->phy = devm_phy_get(&pdev->dev, "usb");
  599. if (IS_ERR(glue->phy)) {
  600. if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
  601. return -EPROBE_DEFER;
  602. dev_err(&pdev->dev, "Error getting phy %ld\n",
  603. PTR_ERR(glue->phy));
  604. return PTR_ERR(glue->phy);
  605. }
  606. glue->usb_phy = usb_phy_generic_register();
  607. if (IS_ERR(glue->usb_phy)) {
  608. dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
  609. PTR_ERR(glue->usb_phy));
  610. return PTR_ERR(glue->usb_phy);
  611. }
  612. glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  613. if (IS_ERR(glue->xceiv)) {
  614. ret = PTR_ERR(glue->xceiv);
  615. dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
  616. goto err_unregister_usb_phy;
  617. }
  618. platform_set_drvdata(pdev, glue);
  619. memset(&pinfo, 0, sizeof(pinfo));
  620. pinfo.name = "musb-hdrc";
  621. pinfo.id = PLATFORM_DEVID_AUTO;
  622. pinfo.parent = &pdev->dev;
  623. pinfo.res = pdev->resource;
  624. pinfo.num_res = pdev->num_resources;
  625. pinfo.data = &pdata;
  626. pinfo.size_data = sizeof(pdata);
  627. glue->musb = platform_device_register_full(&pinfo);
  628. if (IS_ERR(glue->musb)) {
  629. ret = PTR_ERR(glue->musb);
  630. dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
  631. goto err_unregister_usb_phy;
  632. }
  633. return 0;
  634. err_unregister_usb_phy:
  635. usb_phy_generic_unregister(glue->usb_phy);
  636. return ret;
  637. }
  638. static int sunxi_musb_remove(struct platform_device *pdev)
  639. {
  640. struct sunxi_glue *glue = platform_get_drvdata(pdev);
  641. struct platform_device *usb_phy = glue->usb_phy;
  642. platform_device_unregister(glue->musb); /* Frees glue ! */
  643. usb_phy_generic_unregister(usb_phy);
  644. return 0;
  645. }
  646. static const struct of_device_id sunxi_musb_match[] = {
  647. { .compatible = "allwinner,sun4i-a10-musb", },
  648. { .compatible = "allwinner,sun6i-a31-musb", },
  649. { .compatible = "allwinner,sun8i-a33-musb", },
  650. {}
  651. };
  652. static struct platform_driver sunxi_musb_driver = {
  653. .probe = sunxi_musb_probe,
  654. .remove = sunxi_musb_remove,
  655. .driver = {
  656. .name = "musb-sunxi",
  657. .of_match_table = sunxi_musb_match,
  658. },
  659. };
  660. module_platform_driver(sunxi_musb_driver);
  661. MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
  662. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  663. MODULE_LICENSE("GPL v2");