mingming lee | 7d017d6 | 2020-01-16 16:11:37 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Mediatek "glue layer" |
| 4 | * |
| 5 | * Copyright (C) 2019-2021 by Mediatek |
| 6 | * Based on the AllWinner SUNXI "glue layer" code. |
| 7 | * Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com> |
| 8 | * Copyright (C) 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> |
| 9 | * |
| 10 | * This file is part of the Inventra Controller Driver for Linux. |
| 11 | */ |
| 12 | #include <common.h> |
| 13 | #include <clk.h> |
| 14 | #include <dm.h> |
| 15 | #include <dm/lists.h> |
| 16 | #include <dm/root.h> |
| 17 | #include <linux/usb/musb.h> |
| 18 | #include <usb.h> |
| 19 | #include "linux-compat.h" |
| 20 | #include "musb_core.h" |
| 21 | #include "musb_uboot.h" |
| 22 | |
| 23 | #define DBG_I(fmt, ...) \ |
| 24 | pr_info(fmt, ##__VA_ARGS__) |
| 25 | |
| 26 | struct mtk_musb_config { |
| 27 | struct musb_hdrc_config *config; |
| 28 | }; |
| 29 | |
| 30 | struct mtk_musb_glue { |
| 31 | struct musb_host_data mdata; |
| 32 | struct clk usbpllclk; |
| 33 | struct clk usbmcuclk; |
| 34 | struct clk usbclk; |
| 35 | struct mtk_musb_config *cfg; |
| 36 | struct device dev; |
| 37 | }; |
| 38 | |
| 39 | #define to_mtk_musb_glue(d) container_of(d, struct mtk_musb_glue, dev) |
| 40 | |
| 41 | /****************************************************************************** |
| 42 | * phy settings |
| 43 | ******************************************************************************/ |
| 44 | #define USB20_PHY_BASE 0x11110800 |
| 45 | #define USBPHY_READ8(offset) \ |
| 46 | readb((void *)(USB20_PHY_BASE + (offset))) |
| 47 | #define USBPHY_WRITE8(offset, value) \ |
| 48 | writeb(value, (void *)(USB20_PHY_BASE + (offset))) |
| 49 | #define USBPHY_SET8(offset, mask) \ |
| 50 | USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) | (mask)) |
| 51 | #define USBPHY_CLR8(offset, mask) \ |
| 52 | USBPHY_WRITE8(offset, (USBPHY_READ8(offset)) & (~(mask))) |
| 53 | |
| 54 | static void mt_usb_phy_poweron(void) |
| 55 | { |
| 56 | /* |
| 57 | * switch to USB function. |
| 58 | * (system register, force ip into usb mode). |
| 59 | */ |
| 60 | USBPHY_CLR8(0x6b, 0x04); |
| 61 | USBPHY_CLR8(0x6e, 0x01); |
| 62 | USBPHY_CLR8(0x21, 0x03); |
| 63 | |
| 64 | /* RG_USB20_BC11_SW_EN = 1'b0 */ |
| 65 | USBPHY_SET8(0x22, 0x04); |
| 66 | USBPHY_CLR8(0x1a, 0x80); |
| 67 | |
| 68 | /* RG_USB20_DP_100K_EN = 1'b0 */ |
| 69 | /* RG_USB20_DP_100K_EN = 1'b0 */ |
| 70 | USBPHY_CLR8(0x22, 0x03); |
| 71 | |
| 72 | /*OTG enable*/ |
| 73 | USBPHY_SET8(0x20, 0x10); |
| 74 | /* release force suspendm */ |
| 75 | USBPHY_CLR8(0x6a, 0x04); |
| 76 | |
| 77 | mdelay(800); |
| 78 | |
| 79 | /* force enter device mode */ |
| 80 | USBPHY_CLR8(0x6c, 0x10); |
| 81 | USBPHY_SET8(0x6c, 0x2E); |
| 82 | USBPHY_SET8(0x6d, 0x3E); |
| 83 | } |
| 84 | |
| 85 | static void mt_usb_phy_savecurrent(void) |
| 86 | { |
| 87 | /* |
| 88 | * switch to USB function. |
| 89 | * (system register, force ip into usb mode). |
| 90 | */ |
| 91 | USBPHY_CLR8(0x6b, 0x04); |
| 92 | USBPHY_CLR8(0x6e, 0x01); |
| 93 | USBPHY_CLR8(0x21, 0x03); |
| 94 | |
| 95 | /* release force suspendm */ |
| 96 | USBPHY_CLR8(0x6a, 0x04); |
| 97 | USBPHY_SET8(0x68, 0x04); |
| 98 | /* RG_DPPULLDOWN./RG_DMPULLDOWN. */ |
| 99 | USBPHY_SET8(0x68, 0xc0); |
| 100 | /* RG_XCVRSEL[1:0] = 2'b01 */ |
| 101 | USBPHY_CLR8(0x68, 0x30); |
| 102 | USBPHY_SET8(0x68, 0x10); |
| 103 | /* RG_TERMSEL = 1'b1 */ |
| 104 | USBPHY_SET8(0x68, 0x04); |
| 105 | /* RG_DATAIN[3:0] = 4'b0000 */ |
| 106 | USBPHY_CLR8(0x69, 0x3c); |
| 107 | |
| 108 | /* |
| 109 | * force_dp_pulldown, force_dm_pulldown, |
| 110 | * force_xcversel, force_termsel. |
| 111 | */ |
| 112 | USBPHY_SET8(0x6a, 0xba); |
| 113 | |
| 114 | /* RG_USB20_BC11_SW_EN = 1'b0 */ |
| 115 | USBPHY_CLR8(0x1a, 0x80); |
| 116 | /* RG_USB20_OTG_VBUSSCMP_EN = 1'b0 */ |
| 117 | USBPHY_CLR8(0x1a, 0x10); |
| 118 | |
| 119 | mdelay(800); |
| 120 | |
| 121 | USBPHY_CLR8(0x6a, 0x04); |
| 122 | /* rg_usb20_pll_stable = 1 */ |
| 123 | //USBPHY_SET8(0x63, 0x02); |
| 124 | |
| 125 | mdelay(1); |
| 126 | |
| 127 | /* force suspendm = 1 */ |
| 128 | //USBPHY_SET8(0x6a, 0x04); |
| 129 | } |
| 130 | |
| 131 | static void mt_usb_phy_recover(void) |
| 132 | { |
| 133 | /* clean PUPD_BIST_EN */ |
| 134 | /* PUPD_BIST_EN = 1'b0 */ |
| 135 | /* PMIC will use it to detect charger type */ |
| 136 | USBPHY_CLR8(0x1d, 0x10); |
| 137 | |
| 138 | /* force_uart_en = 1'b0 */ |
| 139 | USBPHY_CLR8(0x6b, 0x04); |
| 140 | /* RG_UART_EN = 1'b0 */ |
| 141 | USBPHY_CLR8(0x6e, 0x01); |
| 142 | /* force_uart_en = 1'b0 */ |
| 143 | USBPHY_CLR8(0x6a, 0x04); |
| 144 | |
| 145 | USBPHY_CLR8(0x21, 0x03); |
| 146 | USBPHY_CLR8(0x68, 0xf4); |
| 147 | |
| 148 | /* RG_DATAIN[3:0] = 4'b0000 */ |
| 149 | USBPHY_CLR8(0x69, 0x3c); |
| 150 | |
| 151 | USBPHY_CLR8(0x6a, 0xba); |
| 152 | |
| 153 | /* RG_USB20_BC11_SW_EN = 1'b0 */ |
| 154 | USBPHY_CLR8(0x1a, 0x80); |
| 155 | /* RG_USB20_OTG_VBUSSCMP_EN = 1'b1 */ |
| 156 | USBPHY_SET8(0x1a, 0x10); |
| 157 | |
| 158 | //HQA adjustment |
| 159 | USBPHY_CLR8(0x18, 0x08); |
| 160 | USBPHY_SET8(0x18, 0x06); |
| 161 | mdelay(800); |
| 162 | |
| 163 | /* force enter device mode */ |
| 164 | //USBPHY_CLR8(0x6c, 0x10); |
| 165 | //USBPHY_SET8(0x6c, 0x2E); |
| 166 | //USBPHY_SET8(0x6d, 0x3E); |
| 167 | |
| 168 | /* enable VRT internal R architecture */ |
| 169 | /* RG_USB20_INTR_EN = 1'b1 */ |
| 170 | USBPHY_SET8(0x00, 0x20); |
| 171 | } |
| 172 | |
| 173 | /****************************************************************************** |
| 174 | * MUSB Glue code |
| 175 | ******************************************************************************/ |
| 176 | |
| 177 | static irqreturn_t mtk_musb_interrupt(int irq, void *__hci) |
| 178 | { |
| 179 | struct musb *musb = __hci; |
| 180 | irqreturn_t retval = IRQ_NONE; |
| 181 | |
| 182 | /* read and flush interrupts */ |
| 183 | musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB); |
| 184 | // last_int_usb = musb->int_usb; |
| 185 | if (musb->int_usb) |
| 186 | musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb); |
| 187 | musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX); |
| 188 | if (musb->int_tx) |
| 189 | musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx); |
| 190 | musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX); |
| 191 | if (musb->int_rx) |
| 192 | musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx); |
| 193 | |
| 194 | if (musb->int_usb || musb->int_tx || musb->int_rx) |
| 195 | retval |= musb_interrupt(musb); |
| 196 | |
| 197 | return retval; |
| 198 | } |
| 199 | |
| 200 | /* musb_core does not call enable / disable in a balanced manner <sigh> */ |
| 201 | static bool enabled; |
| 202 | |
| 203 | static int mtk_musb_enable(struct musb *musb) |
| 204 | { |
| 205 | struct mtk_musb_glue *glue = to_mtk_musb_glue(musb->controller); |
| 206 | |
| 207 | DBG_I("%s():\n", __func__); |
| 208 | |
| 209 | musb_ep_select(musb->mregs, 0); |
| 210 | musb_writeb(musb->mregs, MUSB_FADDR, 0); |
| 211 | |
| 212 | if (enabled) |
| 213 | return 0; |
| 214 | |
| 215 | mt_usb_phy_recover(); |
| 216 | |
| 217 | enabled = true; |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | static void mtk_musb_disable(struct musb *musb) |
| 223 | { |
| 224 | struct mtk_musb_glue *glue = to_mtk_musb_glue(musb->controller); |
| 225 | int ret; |
| 226 | |
| 227 | DBG_I("%s():\n", __func__); |
| 228 | |
| 229 | if (!enabled) |
| 230 | return; |
| 231 | |
| 232 | mt_usb_phy_savecurrent(); |
| 233 | |
| 234 | enabled = false; |
| 235 | } |
| 236 | |
| 237 | static int mtk_musb_init(struct musb *musb) |
| 238 | { |
| 239 | struct mtk_musb_glue *glue = to_mtk_musb_glue(musb->controller); |
| 240 | int ret; |
| 241 | |
| 242 | DBG_I("%s():\n", __func__); |
| 243 | |
| 244 | ret = clk_enable(&glue->usbpllclk); |
| 245 | if (ret) { |
| 246 | dev_err(dev, "failed to enable usbpll clock\n"); |
| 247 | return ret; |
| 248 | } |
| 249 | ret = clk_enable(&glue->usbmcuclk); |
| 250 | if (ret) { |
| 251 | dev_err(dev, "failed to enable usbmcu clock\n"); |
| 252 | return ret; |
| 253 | } |
| 254 | ret = clk_enable(&glue->usbclk); |
| 255 | if (ret) { |
| 256 | dev_err(dev, "failed to enable usb clock\n"); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | musb->isr = mtk_musb_interrupt; |
| 261 | |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | static int mtk_musb_exit(struct musb *musb) |
| 266 | { |
| 267 | struct mtk_musb_glue *glue = to_mtk_musb_glue(musb->controller); |
| 268 | |
| 269 | clk_disable(&glue->usbclk); |
| 270 | clk_disable(&glue->usbmcuclk); |
| 271 | clk_disable(&glue->usbpllclk); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | static const struct musb_platform_ops mtk_musb_ops = { |
| 277 | .init = mtk_musb_init, |
| 278 | .exit = mtk_musb_exit, |
| 279 | .enable = mtk_musb_enable, |
| 280 | .disable = mtk_musb_disable, |
| 281 | }; |
| 282 | |
| 283 | /* MTK OTG supports up to 7 endpoints */ |
| 284 | #define MTK_MUSB_MAX_EP_NUM 8 |
| 285 | #define MTK_MUSB_RAM_BITS 16 |
| 286 | |
| 287 | static struct musb_fifo_cfg mtk_musb_mode_cfg[] = { |
| 288 | MUSB_EP_FIFO_SINGLE(1, FIFO_TX, 512), |
| 289 | MUSB_EP_FIFO_SINGLE(1, FIFO_RX, 512), |
| 290 | MUSB_EP_FIFO_SINGLE(2, FIFO_TX, 512), |
| 291 | MUSB_EP_FIFO_SINGLE(2, FIFO_RX, 512), |
| 292 | MUSB_EP_FIFO_SINGLE(3, FIFO_TX, 512), |
| 293 | MUSB_EP_FIFO_SINGLE(3, FIFO_RX, 512), |
| 294 | MUSB_EP_FIFO_SINGLE(4, FIFO_TX, 512), |
| 295 | MUSB_EP_FIFO_SINGLE(4, FIFO_RX, 512), |
| 296 | MUSB_EP_FIFO_SINGLE(5, FIFO_TX, 512), |
| 297 | MUSB_EP_FIFO_SINGLE(5, FIFO_RX, 512), |
| 298 | MUSB_EP_FIFO_SINGLE(6, FIFO_TX, 512), |
| 299 | MUSB_EP_FIFO_SINGLE(6, FIFO_RX, 512), |
| 300 | MUSB_EP_FIFO_SINGLE(7, FIFO_TX, 512), |
| 301 | MUSB_EP_FIFO_SINGLE(7, FIFO_RX, 512), |
| 302 | }; |
| 303 | |
| 304 | static struct musb_hdrc_config musb_config = { |
| 305 | .fifo_cfg = mtk_musb_mode_cfg, |
| 306 | .fifo_cfg_size = ARRAY_SIZE(mtk_musb_mode_cfg), |
| 307 | .multipoint = true, |
| 308 | .dyn_fifo = true, |
| 309 | .num_eps = MTK_MUSB_MAX_EP_NUM, |
| 310 | .ram_bits = MTK_MUSB_RAM_BITS, |
| 311 | }; |
| 312 | |
| 313 | static int musb_usb_probe(struct udevice *dev) |
| 314 | { |
| 315 | struct mtk_musb_glue *glue = dev_get_priv(dev); |
| 316 | struct musb_host_data *host = &glue->mdata; |
| 317 | struct musb_hdrc_platform_data pdata; |
| 318 | void *base = dev_read_addr_ptr(dev); |
| 319 | int ret; |
| 320 | |
| 321 | DBG_I("%s():\n", __func__); |
| 322 | |
| 323 | #ifdef CONFIG_USB_MUSB_HOST |
| 324 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
| 325 | #endif |
| 326 | |
| 327 | if (!base) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | glue->cfg = (struct mtk_musb_config *)dev_get_driver_data(dev); |
| 331 | if (!glue->cfg) |
| 332 | return -EINVAL; |
| 333 | |
| 334 | ret = clk_get_by_name(dev, "usbpll", &glue->usbpllclk); |
| 335 | if (ret) { |
| 336 | dev_err(dev, "failed to get usbpll clock\n"); |
| 337 | return ret; |
| 338 | } |
| 339 | ret = clk_get_by_name(dev, "usbmcu", &glue->usbmcuclk); |
| 340 | if (ret) { |
| 341 | dev_err(dev, "failed to get usbmcu clock\n"); |
| 342 | return ret; |
| 343 | } |
| 344 | ret = clk_get_by_name(dev, "usb", &glue->usbclk); |
| 345 | if (ret) { |
| 346 | dev_err(dev, "failed to get usb clock\n"); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | memset(&pdata, 0, sizeof(pdata)); |
| 351 | pdata.power = (u8)400; |
| 352 | pdata.platform_ops = &mtk_musb_ops; |
| 353 | pdata.config = glue->cfg->config; |
| 354 | |
| 355 | #ifdef CONFIG_USB_MUSB_HOST |
| 356 | priv->desc_before_addr = true; |
| 357 | |
| 358 | pdata.mode = MUSB_HOST; |
| 359 | host->host = musb_init_controller(&pdata, &glue->dev, base); |
| 360 | if (!host->host) |
| 361 | return -EIO; |
| 362 | |
| 363 | ret = musb_lowlevel_init(host); |
| 364 | if (!ret) |
| 365 | printf("MTK MUSB OTG (Host)\n"); |
| 366 | #else |
| 367 | pdata.mode = MUSB_PERIPHERAL; |
| 368 | host->host = musb_register(&pdata, &glue->dev, base); |
| 369 | if (!host->host) |
| 370 | return -EIO; |
| 371 | |
| 372 | printf("MTK MUSB OTG (Peripheral)\n"); |
| 373 | #endif |
| 374 | |
| 375 | mt_usb_phy_poweron(); |
| 376 | |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | static int musb_usb_remove(struct udevice *dev) |
| 381 | { |
| 382 | struct mtk_musb_glue *glue = dev_get_priv(dev); |
| 383 | struct musb_host_data *host = &glue->mdata; |
| 384 | |
| 385 | musb_stop(host->host); |
| 386 | free(host->host); |
| 387 | host->host = NULL; |
| 388 | |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static const struct mtk_musb_config mt8518_cfg = { |
| 393 | .config = &musb_config, |
| 394 | }; |
| 395 | |
| 396 | static const struct udevice_id mtk_musb_ids[] = { |
| 397 | { .compatible = "mediatek,mt8518-musb", |
| 398 | .data = (ulong)&mt8518_cfg }, |
| 399 | { } |
| 400 | }; |
| 401 | |
| 402 | U_BOOT_DRIVER(mtk_musb) = { |
| 403 | .name = "mtk_musb", |
| 404 | #ifdef CONFIG_USB_MUSB_HOST |
| 405 | .id = UCLASS_USB, |
| 406 | #else |
| 407 | .id = UCLASS_USB_GADGET_GENERIC, |
| 408 | #endif |
| 409 | .of_match = mtk_musb_ids, |
| 410 | .probe = musb_usb_probe, |
| 411 | .remove = musb_usb_remove, |
| 412 | #ifdef CONFIG_USB_MUSB_HOST |
| 413 | .ops = &musb_usb_ops, |
| 414 | #endif |
| 415 | .platdata_auto_alloc_size = sizeof(struct usb_platdata), |
| 416 | .priv_auto_alloc_size = sizeof(struct mtk_musb_glue), |
| 417 | }; |