170-musb-add-driver.patch 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. From 744543c599c420bcddca08cd2e2713b82a008328 Mon Sep 17 00:00:00 2001
  2. From: Hans de Goede <hdegoede@redhat.com>
  3. Date: Wed, 8 Jul 2015 16:41:38 +0200
  4. Subject: [PATCH] usb: musb: sunxi: Add support for the Allwinner sunxi musb
  5. controller
  6. This is based on initial code to get the Allwinner sunxi musb controller
  7. supported by Chen-Yu Tsai and Roman Byshko.
  8. This adds support for the Allwinner sunxi musb controller in both host only
  9. and otg mode. Peripheral only mode is not supported, as no boards use that.
  10. This has been tested on a cubietruck (A20 SoC) and an UTOO P66 tablet
  11. (A13 SoC) with a variety of devices in host mode and with the g_serial gadget
  12. driver in peripheral mode, plugging otg / host cables in/out a lot of times
  13. in all possible imaginable plug orders.
  14. Signed-off-by: Hans de Goede <hdegoede@redhat.com>
  15. Signed-off-by: Felipe Balbi <balbi@ti.com>
  16. ---
  17. .../bindings/usb/allwinner,sun4i-a10-musb.txt | 27 +
  18. drivers/usb/musb/Kconfig | 13 +-
  19. drivers/usb/musb/Makefile | 1 +
  20. drivers/usb/musb/sunxi.c | 703 +++++++++++++++++++++
  21. 4 files changed, 743 insertions(+), 1 deletion(-)
  22. create mode 100644 Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt
  23. create mode 100644 drivers/usb/musb/sunxi.c
  24. --- /dev/null
  25. +++ b/Documentation/devicetree/bindings/usb/allwinner,sun4i-a10-musb.txt
  26. @@ -0,0 +1,27 @@
  27. +Allwinner sun4i A10 musb DRC/OTG controller
  28. +-------------------------------------------
  29. +
  30. +Required properties:
  31. + - compatible : "allwinner,sun4i-a10-musb"
  32. + - reg : mmio address range of the musb controller
  33. + - clocks : clock specifier for the musb controller ahb gate clock
  34. + - interrupts : interrupt to which the musb controller is connected
  35. + - interrupt-names : must be "mc"
  36. + - phys : phy specifier for the otg phy
  37. + - phy-names : must be "usb"
  38. + - dr_mode : Dual-Role mode must be "host" or "otg"
  39. + - extcon : extcon specifier for the otg phy
  40. +
  41. +Example:
  42. +
  43. + usb_otg: usb@01c13000 {
  44. + compatible = "allwinner,sun4i-a10-musb";
  45. + reg = <0x01c13000 0x0400>;
  46. + clocks = <&ahb_gates 0>;
  47. + interrupts = <38>;
  48. + interrupt-names = "mc";
  49. + phys = <&usbphy 0>;
  50. + phy-names = "usb";
  51. + extcon = <&usbphy 0>;
  52. + status = "disabled";
  53. + };
  54. --- a/drivers/usb/musb/Kconfig
  55. +++ b/drivers/usb/musb/Kconfig
  56. @@ -5,7 +5,7 @@
  57. # (M)HDRC = (Multipoint) Highspeed Dual-Role Controller
  58. config USB_MUSB_HDRC
  59. - tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, ...)'
  60. + tristate 'Inventra Highspeed Dual Role Controller (TI, ADI, AW, ...)'
  61. depends on (USB || USB_GADGET)
  62. help
  63. Say Y here if your system has a dual role high speed USB
  64. @@ -20,6 +20,8 @@ config USB_MUSB_HDRC
  65. Analog Devices parts using this IP include Blackfin BF54x,
  66. BF525 and BF527.
  67. + Allwinner SoCs using this IP include A10, A13, A20, ...
  68. +
  69. If you do not know what this is, please say N.
  70. To compile this driver as a module, choose M here; the
  71. @@ -60,6 +62,15 @@ endchoice
  72. comment "Platform Glue Layer"
  73. +config USB_MUSB_SUNXI
  74. + tristate "Allwinner (sunxi)"
  75. + depends on ARCH_SUNXI
  76. + depends on NOP_USB_XCEIV
  77. + depends on PHY_SUN4I_USB
  78. + depends on EXTCON
  79. + depends on GENERIC_PHY
  80. + select SUNXI_SRAM
  81. +
  82. config USB_MUSB_DAVINCI
  83. tristate "DaVinci"
  84. depends on ARCH_DAVINCI_DMx
  85. --- a/drivers/usb/musb/Makefile
  86. +++ b/drivers/usb/musb/Makefile
  87. @@ -20,6 +20,7 @@ obj-$(CONFIG_USB_MUSB_DA8XX) += da8xx.
  88. obj-$(CONFIG_USB_MUSB_BLACKFIN) += blackfin.o
  89. obj-$(CONFIG_USB_MUSB_UX500) += ux500.o
  90. obj-$(CONFIG_USB_MUSB_JZ4740) += jz4740.o
  91. +obj-$(CONFIG_USB_MUSB_SUNXI) += sunxi.o
  92. obj-$(CONFIG_USB_MUSB_AM335X_CHILD) += musb_am335x.o
  93. --- /dev/null
  94. +++ b/drivers/usb/musb/sunxi.c
  95. @@ -0,0 +1,703 @@
  96. +/*
  97. + * Allwinner sun4i MUSB Glue Layer
  98. + *
  99. + * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
  100. + *
  101. + * Based on code from
  102. + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  103. + *
  104. + * This program is free software; you can redistribute it and/or modify
  105. + * it under the terms of the GNU General Public License as published by
  106. + * the Free Software Foundation; either version 2 of the License, or
  107. + * (at your option) any later version.
  108. + *
  109. + * This program is distributed in the hope that it will be useful,
  110. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  111. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  112. + * GNU General Public License for more details.
  113. + */
  114. +
  115. +#include <linux/clk.h>
  116. +#include <linux/err.h>
  117. +#include <linux/extcon.h>
  118. +#include <linux/io.h>
  119. +#include <linux/kernel.h>
  120. +#include <linux/module.h>
  121. +#include <linux/of.h>
  122. +#include <linux/phy/phy-sun4i-usb.h>
  123. +#include <linux/platform_device.h>
  124. +#include <linux/soc/sunxi/sunxi_sram.h>
  125. +#include <linux/usb/musb.h>
  126. +#include <linux/usb/of.h>
  127. +#include <linux/usb/usb_phy_generic.h>
  128. +#include <linux/workqueue.h>
  129. +#include "musb_core.h"
  130. +
  131. +/*
  132. + * Register offsets, note sunxi musb has a different layout then most
  133. + * musb implementations, we translate the layout in musb_readb & friends.
  134. + */
  135. +#define SUNXI_MUSB_POWER 0x0040
  136. +#define SUNXI_MUSB_DEVCTL 0x0041
  137. +#define SUNXI_MUSB_INDEX 0x0042
  138. +#define SUNXI_MUSB_VEND0 0x0043
  139. +#define SUNXI_MUSB_INTRTX 0x0044
  140. +#define SUNXI_MUSB_INTRRX 0x0046
  141. +#define SUNXI_MUSB_INTRTXE 0x0048
  142. +#define SUNXI_MUSB_INTRRXE 0x004a
  143. +#define SUNXI_MUSB_INTRUSB 0x004c
  144. +#define SUNXI_MUSB_INTRUSBE 0x0050
  145. +#define SUNXI_MUSB_FRAME 0x0054
  146. +#define SUNXI_MUSB_TXFIFOSZ 0x0090
  147. +#define SUNXI_MUSB_TXFIFOADD 0x0092
  148. +#define SUNXI_MUSB_RXFIFOSZ 0x0094
  149. +#define SUNXI_MUSB_RXFIFOADD 0x0096
  150. +#define SUNXI_MUSB_FADDR 0x0098
  151. +#define SUNXI_MUSB_TXFUNCADDR 0x0098
  152. +#define SUNXI_MUSB_TXHUBADDR 0x009a
  153. +#define SUNXI_MUSB_TXHUBPORT 0x009b
  154. +#define SUNXI_MUSB_RXFUNCADDR 0x009c
  155. +#define SUNXI_MUSB_RXHUBADDR 0x009e
  156. +#define SUNXI_MUSB_RXHUBPORT 0x009f
  157. +#define SUNXI_MUSB_CONFIGDATA 0x00c0
  158. +
  159. +/* VEND0 bits */
  160. +#define SUNXI_MUSB_VEND0_PIO_MODE 0
  161. +
  162. +/* flags */
  163. +#define SUNXI_MUSB_FL_ENABLED 0
  164. +#define SUNXI_MUSB_FL_HOSTMODE 1
  165. +#define SUNXI_MUSB_FL_HOSTMODE_PEND 2
  166. +#define SUNXI_MUSB_FL_VBUS_ON 3
  167. +#define SUNXI_MUSB_FL_PHY_ON 4
  168. +
  169. +/* Our read/write methods need access and do not get passed in a musb ref :| */
  170. +static struct musb *sunxi_musb;
  171. +
  172. +struct sunxi_glue {
  173. + struct device *dev;
  174. + struct platform_device *musb;
  175. + struct clk *clk;
  176. + struct phy *phy;
  177. + struct platform_device *usb_phy;
  178. + struct usb_phy *xceiv;
  179. + unsigned long flags;
  180. + struct work_struct work;
  181. + struct extcon_dev *extcon;
  182. + struct notifier_block host_nb;
  183. +};
  184. +
  185. +/* phy_power_on / off may sleep, so we use a workqueue */
  186. +static void sunxi_musb_work(struct work_struct *work)
  187. +{
  188. + struct sunxi_glue *glue = container_of(work, struct sunxi_glue, work);
  189. + bool vbus_on, phy_on;
  190. +
  191. + if (!test_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
  192. + return;
  193. +
  194. + if (test_and_clear_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags)) {
  195. + struct musb *musb = platform_get_drvdata(glue->musb);
  196. + unsigned long flags;
  197. + u8 devctl;
  198. +
  199. + spin_lock_irqsave(&musb->lock, flags);
  200. +
  201. + devctl = readb(musb->mregs + SUNXI_MUSB_DEVCTL);
  202. + if (test_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags)) {
  203. + set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  204. + musb->xceiv->otg->default_a = 1;
  205. + musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  206. + MUSB_HST_MODE(musb);
  207. + devctl |= MUSB_DEVCTL_SESSION;
  208. + } else {
  209. + clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  210. + musb->xceiv->otg->default_a = 0;
  211. + musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  212. + MUSB_DEV_MODE(musb);
  213. + devctl &= ~MUSB_DEVCTL_SESSION;
  214. + }
  215. + writeb(devctl, musb->mregs + SUNXI_MUSB_DEVCTL);
  216. +
  217. + spin_unlock_irqrestore(&musb->lock, flags);
  218. + }
  219. +
  220. + vbus_on = test_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  221. + phy_on = test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  222. +
  223. + if (phy_on != vbus_on) {
  224. + if (vbus_on) {
  225. + phy_power_on(glue->phy);
  226. + set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  227. + } else {
  228. + phy_power_off(glue->phy);
  229. + clear_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  230. + }
  231. + }
  232. +}
  233. +
  234. +static void sunxi_musb_set_vbus(struct musb *musb, int is_on)
  235. +{
  236. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  237. +
  238. + if (is_on)
  239. + set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  240. + else
  241. + clear_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  242. +
  243. + schedule_work(&glue->work);
  244. +}
  245. +
  246. +static void sunxi_musb_pre_root_reset_end(struct musb *musb)
  247. +{
  248. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  249. +
  250. + sun4i_usb_phy_set_squelch_detect(glue->phy, false);
  251. +}
  252. +
  253. +static void sunxi_musb_post_root_reset_end(struct musb *musb)
  254. +{
  255. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  256. +
  257. + sun4i_usb_phy_set_squelch_detect(glue->phy, true);
  258. +}
  259. +
  260. +static irqreturn_t sunxi_musb_interrupt(int irq, void *__hci)
  261. +{
  262. + struct musb *musb = __hci;
  263. + unsigned long flags;
  264. +
  265. + spin_lock_irqsave(&musb->lock, flags);
  266. +
  267. + musb->int_usb = readb(musb->mregs + SUNXI_MUSB_INTRUSB);
  268. + if (musb->int_usb)
  269. + writeb(musb->int_usb, musb->mregs + SUNXI_MUSB_INTRUSB);
  270. +
  271. + /*
  272. + * sunxi musb often signals babble on low / full speed device
  273. + * disconnect, without ever raising MUSB_INTR_DISCONNECT, since
  274. + * normally babble never happens treat it as disconnect.
  275. + */
  276. + if ((musb->int_usb & MUSB_INTR_BABBLE) && is_host_active(musb)) {
  277. + musb->int_usb &= ~MUSB_INTR_BABBLE;
  278. + musb->int_usb |= MUSB_INTR_DISCONNECT;
  279. + }
  280. +
  281. + if ((musb->int_usb & MUSB_INTR_RESET) && !is_host_active(musb)) {
  282. + /* ep0 FADDR must be 0 when (re)entering peripheral mode */
  283. + musb_ep_select(musb->mregs, 0);
  284. + musb_writeb(musb->mregs, MUSB_FADDR, 0);
  285. + }
  286. +
  287. + musb->int_tx = readw(musb->mregs + SUNXI_MUSB_INTRTX);
  288. + if (musb->int_tx)
  289. + writew(musb->int_tx, musb->mregs + SUNXI_MUSB_INTRTX);
  290. +
  291. + musb->int_rx = readw(musb->mregs + SUNXI_MUSB_INTRRX);
  292. + if (musb->int_rx)
  293. + writew(musb->int_rx, musb->mregs + SUNXI_MUSB_INTRRX);
  294. +
  295. + musb_interrupt(musb);
  296. +
  297. + spin_unlock_irqrestore(&musb->lock, flags);
  298. +
  299. + return IRQ_HANDLED;
  300. +}
  301. +
  302. +static int sunxi_musb_host_notifier(struct notifier_block *nb,
  303. + unsigned long event, void *ptr)
  304. +{
  305. + struct sunxi_glue *glue = container_of(nb, struct sunxi_glue, host_nb);
  306. +
  307. + if (event)
  308. + set_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
  309. + else
  310. + clear_bit(SUNXI_MUSB_FL_HOSTMODE, &glue->flags);
  311. +
  312. + set_bit(SUNXI_MUSB_FL_HOSTMODE_PEND, &glue->flags);
  313. + schedule_work(&glue->work);
  314. +
  315. + return NOTIFY_DONE;
  316. +}
  317. +
  318. +static int sunxi_musb_init(struct musb *musb)
  319. +{
  320. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  321. + int ret;
  322. +
  323. + sunxi_musb = musb;
  324. + musb->phy = glue->phy;
  325. + musb->xceiv = glue->xceiv;
  326. +
  327. + ret = sunxi_sram_claim(musb->controller->parent);
  328. + if (ret)
  329. + return ret;
  330. +
  331. + ret = clk_prepare_enable(glue->clk);
  332. + if (ret)
  333. + goto error_sram_release;
  334. +
  335. + writeb(SUNXI_MUSB_VEND0_PIO_MODE, musb->mregs + SUNXI_MUSB_VEND0);
  336. +
  337. + /* Register notifier before calling phy_init() */
  338. + if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE) {
  339. + ret = extcon_register_notifier(glue->extcon, EXTCON_USB_HOST,
  340. + &glue->host_nb);
  341. + if (ret)
  342. + goto error_clk_disable;
  343. + }
  344. +
  345. + ret = phy_init(glue->phy);
  346. + if (ret)
  347. + goto error_unregister_notifier;
  348. +
  349. + if (musb->port_mode == MUSB_PORT_MODE_HOST) {
  350. + ret = phy_power_on(glue->phy);
  351. + if (ret)
  352. + goto error_phy_exit;
  353. + set_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags);
  354. + /* Stop musb work from turning vbus off again */
  355. + set_bit(SUNXI_MUSB_FL_VBUS_ON, &glue->flags);
  356. + }
  357. +
  358. + musb->isr = sunxi_musb_interrupt;
  359. +
  360. + /* Stop the musb-core from doing runtime pm (not supported on sunxi) */
  361. + pm_runtime_get(musb->controller);
  362. +
  363. + return 0;
  364. +
  365. +error_phy_exit:
  366. + phy_exit(glue->phy);
  367. +error_unregister_notifier:
  368. + if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  369. + extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
  370. + &glue->host_nb);
  371. +error_clk_disable:
  372. + clk_disable_unprepare(glue->clk);
  373. +error_sram_release:
  374. + sunxi_sram_release(musb->controller->parent);
  375. + return ret;
  376. +}
  377. +
  378. +static int sunxi_musb_exit(struct musb *musb)
  379. +{
  380. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  381. +
  382. + pm_runtime_put(musb->controller);
  383. +
  384. + cancel_work_sync(&glue->work);
  385. + if (test_bit(SUNXI_MUSB_FL_PHY_ON, &glue->flags))
  386. + phy_power_off(glue->phy);
  387. +
  388. + phy_exit(glue->phy);
  389. +
  390. + if (musb->port_mode == MUSB_PORT_MODE_DUAL_ROLE)
  391. + extcon_unregister_notifier(glue->extcon, EXTCON_USB_HOST,
  392. + &glue->host_nb);
  393. +
  394. + clk_disable_unprepare(glue->clk);
  395. + sunxi_sram_release(musb->controller->parent);
  396. +
  397. + return 0;
  398. +}
  399. +
  400. +static void sunxi_musb_enable(struct musb *musb)
  401. +{
  402. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  403. +
  404. + /* musb_core does not call us in a balanced manner */
  405. + if (test_and_set_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags))
  406. + return;
  407. +
  408. + schedule_work(&glue->work);
  409. +}
  410. +
  411. +static void sunxi_musb_disable(struct musb *musb)
  412. +{
  413. + struct sunxi_glue *glue = dev_get_drvdata(musb->controller->parent);
  414. +
  415. + clear_bit(SUNXI_MUSB_FL_ENABLED, &glue->flags);
  416. +}
  417. +
  418. +/*
  419. + * sunxi musb register layout
  420. + * 0x00 - 0x17 fifo regs, 1 long per fifo
  421. + * 0x40 - 0x57 generic control regs (power - frame)
  422. + * 0x80 - 0x8f ep control regs (addressed through hw_ep->regs, indexed)
  423. + * 0x90 - 0x97 fifo control regs (indexed)
  424. + * 0x98 - 0x9f multipoint / busctl regs (indexed)
  425. + * 0xc0 configdata reg
  426. + */
  427. +
  428. +static u32 sunxi_musb_fifo_offset(u8 epnum)
  429. +{
  430. + return (epnum * 4);
  431. +}
  432. +
  433. +static u32 sunxi_musb_ep_offset(u8 epnum, u16 offset)
  434. +{
  435. + WARN_ONCE(offset != 0,
  436. + "sunxi_musb_ep_offset called with non 0 offset\n");
  437. +
  438. + return 0x80; /* indexed, so ignore epnum */
  439. +}
  440. +
  441. +static u32 sunxi_musb_busctl_offset(u8 epnum, u16 offset)
  442. +{
  443. + return SUNXI_MUSB_TXFUNCADDR + offset;
  444. +}
  445. +
  446. +static u8 sunxi_musb_readb(const void __iomem *addr, unsigned offset)
  447. +{
  448. + if (addr == sunxi_musb->mregs) {
  449. + /* generic control or fifo control reg access */
  450. + switch (offset) {
  451. + case MUSB_FADDR:
  452. + return readb(addr + SUNXI_MUSB_FADDR);
  453. + case MUSB_POWER:
  454. + return readb(addr + SUNXI_MUSB_POWER);
  455. + case MUSB_INTRUSB:
  456. + return readb(addr + SUNXI_MUSB_INTRUSB);
  457. + case MUSB_INTRUSBE:
  458. + return readb(addr + SUNXI_MUSB_INTRUSBE);
  459. + case MUSB_INDEX:
  460. + return readb(addr + SUNXI_MUSB_INDEX);
  461. + case MUSB_TESTMODE:
  462. + return 0; /* No testmode on sunxi */
  463. + case MUSB_DEVCTL:
  464. + return readb(addr + SUNXI_MUSB_DEVCTL);
  465. + case MUSB_TXFIFOSZ:
  466. + return readb(addr + SUNXI_MUSB_TXFIFOSZ);
  467. + case MUSB_RXFIFOSZ:
  468. + return readb(addr + SUNXI_MUSB_RXFIFOSZ);
  469. + case MUSB_CONFIGDATA + 0x10: /* See musb_read_configdata() */
  470. + return readb(addr + SUNXI_MUSB_CONFIGDATA);
  471. + /* Offset for these is fixed by sunxi_musb_busctl_offset() */
  472. + case SUNXI_MUSB_TXFUNCADDR:
  473. + case SUNXI_MUSB_TXHUBADDR:
  474. + case SUNXI_MUSB_TXHUBPORT:
  475. + case SUNXI_MUSB_RXFUNCADDR:
  476. + case SUNXI_MUSB_RXHUBADDR:
  477. + case SUNXI_MUSB_RXHUBPORT:
  478. + /* multipoint / busctl reg access */
  479. + return readb(addr + offset);
  480. + default:
  481. + dev_err(sunxi_musb->controller->parent,
  482. + "Error unknown readb offset %u\n", offset);
  483. + return 0;
  484. + }
  485. + } else if (addr == (sunxi_musb->mregs + 0x80)) {
  486. + /* ep control reg access */
  487. + /* sunxi has a 2 byte hole before the txtype register */
  488. + if (offset >= MUSB_TXTYPE)
  489. + offset += 2;
  490. + return readb(addr + offset);
  491. + }
  492. +
  493. + dev_err(sunxi_musb->controller->parent,
  494. + "Error unknown readb at 0x%x bytes offset\n",
  495. + (int)(addr - sunxi_musb->mregs));
  496. + return 0;
  497. +}
  498. +
  499. +static void sunxi_musb_writeb(void __iomem *addr, unsigned offset, u8 data)
  500. +{
  501. + if (addr == sunxi_musb->mregs) {
  502. + /* generic control or fifo control reg access */
  503. + switch (offset) {
  504. + case MUSB_FADDR:
  505. + return writeb(data, addr + SUNXI_MUSB_FADDR);
  506. + case MUSB_POWER:
  507. + return writeb(data, addr + SUNXI_MUSB_POWER);
  508. + case MUSB_INTRUSB:
  509. + return writeb(data, addr + SUNXI_MUSB_INTRUSB);
  510. + case MUSB_INTRUSBE:
  511. + return writeb(data, addr + SUNXI_MUSB_INTRUSBE);
  512. + case MUSB_INDEX:
  513. + return writeb(data, addr + SUNXI_MUSB_INDEX);
  514. + case MUSB_TESTMODE:
  515. + if (data)
  516. + dev_warn(sunxi_musb->controller->parent,
  517. + "sunxi-musb does not have testmode\n");
  518. + return;
  519. + case MUSB_DEVCTL:
  520. + return writeb(data, addr + SUNXI_MUSB_DEVCTL);
  521. + case MUSB_TXFIFOSZ:
  522. + return writeb(data, addr + SUNXI_MUSB_TXFIFOSZ);
  523. + case MUSB_RXFIFOSZ:
  524. + return writeb(data, addr + SUNXI_MUSB_RXFIFOSZ);
  525. + /* Offset for these is fixed by sunxi_musb_busctl_offset() */
  526. + case SUNXI_MUSB_TXFUNCADDR:
  527. + case SUNXI_MUSB_TXHUBADDR:
  528. + case SUNXI_MUSB_TXHUBPORT:
  529. + case SUNXI_MUSB_RXFUNCADDR:
  530. + case SUNXI_MUSB_RXHUBADDR:
  531. + case SUNXI_MUSB_RXHUBPORT:
  532. + /* multipoint / busctl reg access */
  533. + return writeb(data, addr + offset);
  534. + default:
  535. + dev_err(sunxi_musb->controller->parent,
  536. + "Error unknown writeb offset %u\n", offset);
  537. + return;
  538. + }
  539. + } else if (addr == (sunxi_musb->mregs + 0x80)) {
  540. + /* ep control reg access */
  541. + if (offset >= MUSB_TXTYPE)
  542. + offset += 2;
  543. + return writeb(data, addr + offset);
  544. + }
  545. +
  546. + dev_err(sunxi_musb->controller->parent,
  547. + "Error unknown writeb at 0x%x bytes offset\n",
  548. + (int)(addr - sunxi_musb->mregs));
  549. +}
  550. +
  551. +static u16 sunxi_musb_readw(const void __iomem *addr, unsigned offset)
  552. +{
  553. + if (addr == sunxi_musb->mregs) {
  554. + /* generic control or fifo control reg access */
  555. + switch (offset) {
  556. + case MUSB_INTRTX:
  557. + return readw(addr + SUNXI_MUSB_INTRTX);
  558. + case MUSB_INTRRX:
  559. + return readw(addr + SUNXI_MUSB_INTRRX);
  560. + case MUSB_INTRTXE:
  561. + return readw(addr + SUNXI_MUSB_INTRTXE);
  562. + case MUSB_INTRRXE:
  563. + return readw(addr + SUNXI_MUSB_INTRRXE);
  564. + case MUSB_FRAME:
  565. + return readw(addr + SUNXI_MUSB_FRAME);
  566. + case MUSB_TXFIFOADD:
  567. + return readw(addr + SUNXI_MUSB_TXFIFOADD);
  568. + case MUSB_RXFIFOADD:
  569. + return readw(addr + SUNXI_MUSB_RXFIFOADD);
  570. + case MUSB_HWVERS:
  571. + return 0; /* sunxi musb version is not known */
  572. + default:
  573. + dev_err(sunxi_musb->controller->parent,
  574. + "Error unknown readw offset %u\n", offset);
  575. + return 0;
  576. + }
  577. + } else if (addr == (sunxi_musb->mregs + 0x80)) {
  578. + /* ep control reg access */
  579. + return readw(addr + offset);
  580. + }
  581. +
  582. + dev_err(sunxi_musb->controller->parent,
  583. + "Error unknown readw at 0x%x bytes offset\n",
  584. + (int)(addr - sunxi_musb->mregs));
  585. + return 0;
  586. +}
  587. +
  588. +static void sunxi_musb_writew(void __iomem *addr, unsigned offset, u16 data)
  589. +{
  590. + if (addr == sunxi_musb->mregs) {
  591. + /* generic control or fifo control reg access */
  592. + switch (offset) {
  593. + case MUSB_INTRTX:
  594. + return writew(data, addr + SUNXI_MUSB_INTRTX);
  595. + case MUSB_INTRRX:
  596. + return writew(data, addr + SUNXI_MUSB_INTRRX);
  597. + case MUSB_INTRTXE:
  598. + return writew(data, addr + SUNXI_MUSB_INTRTXE);
  599. + case MUSB_INTRRXE:
  600. + return writew(data, addr + SUNXI_MUSB_INTRRXE);
  601. + case MUSB_FRAME:
  602. + return writew(data, addr + SUNXI_MUSB_FRAME);
  603. + case MUSB_TXFIFOADD:
  604. + return writew(data, addr + SUNXI_MUSB_TXFIFOADD);
  605. + case MUSB_RXFIFOADD:
  606. + return writew(data, addr + SUNXI_MUSB_RXFIFOADD);
  607. + default:
  608. + dev_err(sunxi_musb->controller->parent,
  609. + "Error unknown writew offset %u\n", offset);
  610. + return;
  611. + }
  612. + } else if (addr == (sunxi_musb->mregs + 0x80)) {
  613. + /* ep control reg access */
  614. + return writew(data, addr + offset);
  615. + }
  616. +
  617. + dev_err(sunxi_musb->controller->parent,
  618. + "Error unknown writew at 0x%x bytes offset\n",
  619. + (int)(addr - sunxi_musb->mregs));
  620. +}
  621. +
  622. +static const struct musb_platform_ops sunxi_musb_ops = {
  623. + .quirks = MUSB_INDEXED_EP,
  624. + .init = sunxi_musb_init,
  625. + .exit = sunxi_musb_exit,
  626. + .enable = sunxi_musb_enable,
  627. + .disable = sunxi_musb_disable,
  628. + .fifo_offset = sunxi_musb_fifo_offset,
  629. + .ep_offset = sunxi_musb_ep_offset,
  630. + .busctl_offset = sunxi_musb_busctl_offset,
  631. + .readb = sunxi_musb_readb,
  632. + .writeb = sunxi_musb_writeb,
  633. + .readw = sunxi_musb_readw,
  634. + .writew = sunxi_musb_writew,
  635. + .set_vbus = sunxi_musb_set_vbus,
  636. + .pre_root_reset_end = sunxi_musb_pre_root_reset_end,
  637. + .post_root_reset_end = sunxi_musb_post_root_reset_end,
  638. +};
  639. +
  640. +/* Allwinner OTG supports up to 5 endpoints */
  641. +#define SUNXI_MUSB_MAX_EP_NUM 6
  642. +#define SUNXI_MUSB_RAM_BITS 11
  643. +
  644. +static struct musb_fifo_cfg sunxi_musb_mode_cfg[] = {
  645. + MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512),
  646. + MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512),
  647. + MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512),
  648. + MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512),
  649. + MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512),
  650. + MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512),
  651. + MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512),
  652. + MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512),
  653. + MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512),
  654. + MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512),
  655. +};
  656. +
  657. +static struct musb_hdrc_config sunxi_musb_hdrc_config = {
  658. + .fifo_cfg = sunxi_musb_mode_cfg,
  659. + .fifo_cfg_size = ARRAY_SIZE(sunxi_musb_mode_cfg),
  660. + .multipoint = true,
  661. + .dyn_fifo = true,
  662. + .soft_con = true,
  663. + .num_eps = SUNXI_MUSB_MAX_EP_NUM,
  664. + .ram_bits = SUNXI_MUSB_RAM_BITS,
  665. + .dma = 0,
  666. +};
  667. +
  668. +static int sunxi_musb_probe(struct platform_device *pdev)
  669. +{
  670. + struct musb_hdrc_platform_data pdata;
  671. + struct platform_device_info pinfo;
  672. + struct sunxi_glue *glue;
  673. + struct device_node *np = pdev->dev.of_node;
  674. + int ret;
  675. +
  676. + if (!np) {
  677. + dev_err(&pdev->dev, "Error no device tree node found\n");
  678. + return -EINVAL;
  679. + }
  680. +
  681. + glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  682. + if (!glue)
  683. + return -ENOMEM;
  684. +
  685. + memset(&pdata, 0, sizeof(pdata));
  686. + switch (of_usb_get_dr_mode(np)) {
  687. +#if defined CONFIG_USB_MUSB_DUAL_ROLE || defined CONFIG_USB_MUSB_HOST
  688. + case USB_DR_MODE_HOST:
  689. + pdata.mode = MUSB_PORT_MODE_HOST;
  690. + break;
  691. +#endif
  692. +#ifdef CONFIG_USB_MUSB_DUAL_ROLE
  693. + case USB_DR_MODE_OTG:
  694. + glue->extcon = extcon_get_edev_by_phandle(&pdev->dev, 0);
  695. + if (IS_ERR(glue->extcon)) {
  696. + if (PTR_ERR(glue->extcon) == -EPROBE_DEFER)
  697. + return -EPROBE_DEFER;
  698. + dev_err(&pdev->dev, "Invalid or missing extcon\n");
  699. + return PTR_ERR(glue->extcon);
  700. + }
  701. + pdata.mode = MUSB_PORT_MODE_DUAL_ROLE;
  702. + break;
  703. +#endif
  704. + default:
  705. + dev_err(&pdev->dev, "Invalid or missing 'dr_mode' property\n");
  706. + return -EINVAL;
  707. + }
  708. + pdata.platform_ops = &sunxi_musb_ops;
  709. + pdata.config = &sunxi_musb_hdrc_config;
  710. +
  711. + glue->dev = &pdev->dev;
  712. + INIT_WORK(&glue->work, sunxi_musb_work);
  713. + glue->host_nb.notifier_call = sunxi_musb_host_notifier;
  714. +
  715. + glue->clk = devm_clk_get(&pdev->dev, NULL);
  716. + if (IS_ERR(glue->clk)) {
  717. + dev_err(&pdev->dev, "Error getting clock: %ld\n",
  718. + PTR_ERR(glue->clk));
  719. + return PTR_ERR(glue->clk);
  720. + }
  721. +
  722. + glue->phy = devm_phy_get(&pdev->dev, "usb");
  723. + if (IS_ERR(glue->phy)) {
  724. + if (PTR_ERR(glue->phy) == -EPROBE_DEFER)
  725. + return -EPROBE_DEFER;
  726. + dev_err(&pdev->dev, "Error getting phy %ld\n",
  727. + PTR_ERR(glue->phy));
  728. + return PTR_ERR(glue->phy);
  729. + }
  730. +
  731. + glue->usb_phy = usb_phy_generic_register();
  732. + if (IS_ERR(glue->usb_phy)) {
  733. + dev_err(&pdev->dev, "Error registering usb-phy %ld\n",
  734. + PTR_ERR(glue->usb_phy));
  735. + return PTR_ERR(glue->usb_phy);
  736. + }
  737. +
  738. + glue->xceiv = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
  739. + if (IS_ERR(glue->xceiv)) {
  740. + ret = PTR_ERR(glue->xceiv);
  741. + dev_err(&pdev->dev, "Error getting usb-phy %d\n", ret);
  742. + goto err_unregister_usb_phy;
  743. + }
  744. +
  745. + platform_set_drvdata(pdev, glue);
  746. +
  747. + memset(&pinfo, 0, sizeof(pinfo));
  748. + pinfo.name = "musb-hdrc";
  749. + pinfo.id = PLATFORM_DEVID_AUTO;
  750. + pinfo.parent = &pdev->dev;
  751. + pinfo.res = pdev->resource;
  752. + pinfo.num_res = pdev->num_resources;
  753. + pinfo.data = &pdata;
  754. + pinfo.size_data = sizeof(pdata);
  755. +
  756. + glue->musb = platform_device_register_full(&pinfo);
  757. + if (IS_ERR(glue->musb)) {
  758. + ret = PTR_ERR(glue->musb);
  759. + dev_err(&pdev->dev, "Error registering musb dev: %d\n", ret);
  760. + goto err_unregister_usb_phy;
  761. + }
  762. +
  763. + return 0;
  764. +
  765. +err_unregister_usb_phy:
  766. + usb_phy_generic_unregister(glue->usb_phy);
  767. + return ret;
  768. +}
  769. +
  770. +static int sunxi_musb_remove(struct platform_device *pdev)
  771. +{
  772. + struct sunxi_glue *glue = platform_get_drvdata(pdev);
  773. + struct platform_device *usb_phy = glue->usb_phy;
  774. +
  775. + platform_device_unregister(glue->musb); /* Frees glue ! */
  776. + usb_phy_generic_unregister(usb_phy);
  777. +
  778. + return 0;
  779. +}
  780. +
  781. +static const struct of_device_id sunxi_musb_match[] = {
  782. + { .compatible = "allwinner,sun4i-a10-musb", },
  783. + {}
  784. +};
  785. +
  786. +static struct platform_driver sunxi_musb_driver = {
  787. + .probe = sunxi_musb_probe,
  788. + .remove = sunxi_musb_remove,
  789. + .driver = {
  790. + .name = "musb-sunxi",
  791. + .of_match_table = sunxi_musb_match,
  792. + },
  793. +};
  794. +module_platform_driver(sunxi_musb_driver);
  795. +
  796. +MODULE_DESCRIPTION("Allwinner sunxi MUSB Glue Layer");
  797. +MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  798. +MODULE_LICENSE("GPL v2");