sunxi.c 24 KB

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