1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget 4 * 5 * core.c - Top level support 6 * 7 * Copyright 2017 IBM Corporation 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 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/platform_device.h> 18 #include <linux/delay.h> 19 #include <linux/ioport.h> 20 #include <linux/slab.h> 21 #include <linux/errno.h> 22 #include <linux/list.h> 23 #include <linux/interrupt.h> 24 #include <linux/proc_fs.h> 25 #include <linux/prefetch.h> 26 #include <linux/clk.h> 27 #include <linux/usb/gadget.h> 28 #include <linux/of.h> 29 #include <linux/of_gpio.h> 30 #include <linux/regmap.h> 31 #include <linux/dma-mapping.h> 32 33 #include "vhub.h" 34 35 void ast_vhub_done(struct ast_vhub_ep *ep, struct ast_vhub_req *req, 36 int status) 37 { 38 bool internal = req->internal; 39 struct ast_vhub *vhub = ep->vhub; 40 41 EPVDBG(ep, "completing request @%p, status %d\n", req, status); 42 43 list_del_init(&req->queue); 44 45 if (req->req.status == -EINPROGRESS) 46 req->req.status = status; 47 48 if (req->req.dma) { 49 if (!WARN_ON(!ep->dev)) 50 usb_gadget_unmap_request_by_dev(&vhub->pdev->dev, 51 &req->req, ep->epn.is_in); 52 req->req.dma = 0; 53 } 54 55 /* 56 * If this isn't an internal EP0 request, call the core 57 * to call the gadget completion. 58 */ 59 if (!internal) { 60 spin_unlock(&ep->vhub->lock); 61 usb_gadget_giveback_request(&ep->ep, &req->req); 62 spin_lock(&ep->vhub->lock); 63 } 64 } 65 66 void ast_vhub_nuke(struct ast_vhub_ep *ep, int status) 67 { 68 struct ast_vhub_req *req; 69 int count = 0; 70 71 /* Beware, lock will be dropped & req-acquired by done() */ 72 while (!list_empty(&ep->queue)) { 73 req = list_first_entry(&ep->queue, struct ast_vhub_req, queue); 74 ast_vhub_done(ep, req, status); 75 count++; 76 } 77 if (count) 78 EPDBG(ep, "Nuked %d request(s)\n", count); 79 } 80 81 struct usb_request *ast_vhub_alloc_request(struct usb_ep *u_ep, 82 gfp_t gfp_flags) 83 { 84 struct ast_vhub_req *req; 85 86 req = kzalloc(sizeof(*req), gfp_flags); 87 if (!req) 88 return NULL; 89 return &req->req; 90 } 91 92 void ast_vhub_free_request(struct usb_ep *u_ep, struct usb_request *u_req) 93 { 94 struct ast_vhub_req *req = to_ast_req(u_req); 95 96 kfree(req); 97 } 98 99 static irqreturn_t ast_vhub_irq(int irq, void *data) 100 { 101 struct ast_vhub *vhub = data; 102 irqreturn_t iret = IRQ_NONE; 103 u32 i, istat; 104 105 /* Stale interrupt while tearing down */ 106 if (!vhub->ep0_bufs) 107 return IRQ_NONE; 108 109 spin_lock(&vhub->lock); 110 111 /* Read and ACK interrupts */ 112 istat = readl(vhub->regs + AST_VHUB_ISR); 113 if (!istat) 114 goto bail; 115 writel(istat, vhub->regs + AST_VHUB_ISR); 116 iret = IRQ_HANDLED; 117 118 UDCVDBG(vhub, "irq status=%08x, ep_acks=%08x ep_nacks=%08x\n", 119 istat, 120 readl(vhub->regs + AST_VHUB_EP_ACK_ISR), 121 readl(vhub->regs + AST_VHUB_EP_NACK_ISR)); 122 123 /* Handle generic EPs first */ 124 if (istat & VHUB_IRQ_EP_POOL_ACK_STALL) { 125 u32 ep_acks = readl(vhub->regs + AST_VHUB_EP_ACK_ISR); 126 writel(ep_acks, vhub->regs + AST_VHUB_EP_ACK_ISR); 127 128 for (i = 0; ep_acks && i < vhub->max_epns; i++) { 129 u32 mask = VHUB_EP_IRQ(i); 130 if (ep_acks & mask) { 131 ast_vhub_epn_ack_irq(&vhub->epns[i]); 132 ep_acks &= ~mask; 133 } 134 } 135 } 136 137 /* Handle device interrupts */ 138 if (istat & vhub->port_irq_mask) { 139 for (i = 0; i < vhub->max_ports; i++) { 140 if (istat & VHUB_DEV_IRQ(i)) 141 ast_vhub_dev_irq(&vhub->ports[i].dev); 142 } 143 } 144 145 /* Handle top-level vHub EP0 interrupts */ 146 if (istat & (VHUB_IRQ_HUB_EP0_OUT_ACK_STALL | 147 VHUB_IRQ_HUB_EP0_IN_ACK_STALL | 148 VHUB_IRQ_HUB_EP0_SETUP)) { 149 if (istat & VHUB_IRQ_HUB_EP0_IN_ACK_STALL) 150 ast_vhub_ep0_handle_ack(&vhub->ep0, true); 151 if (istat & VHUB_IRQ_HUB_EP0_OUT_ACK_STALL) 152 ast_vhub_ep0_handle_ack(&vhub->ep0, false); 153 if (istat & VHUB_IRQ_HUB_EP0_SETUP) 154 ast_vhub_ep0_handle_setup(&vhub->ep0); 155 } 156 157 /* Various top level bus events */ 158 if (istat & (VHUB_IRQ_BUS_RESUME | 159 VHUB_IRQ_BUS_SUSPEND | 160 VHUB_IRQ_BUS_RESET)) { 161 if (istat & VHUB_IRQ_BUS_RESUME) 162 ast_vhub_hub_resume(vhub); 163 if (istat & VHUB_IRQ_BUS_SUSPEND) 164 ast_vhub_hub_suspend(vhub); 165 if (istat & VHUB_IRQ_BUS_RESET) 166 ast_vhub_hub_reset(vhub); 167 } 168 169 bail: 170 spin_unlock(&vhub->lock); 171 return iret; 172 } 173 174 void ast_vhub_init_hw(struct ast_vhub *vhub) 175 { 176 u32 ctrl, port_mask, epn_mask; 177 178 UDCDBG(vhub,"(Re)Starting HW ...\n"); 179 180 /* Enable PHY */ 181 ctrl = VHUB_CTRL_PHY_CLK | 182 VHUB_CTRL_PHY_RESET_DIS; 183 184 /* 185 * We do *NOT* set the VHUB_CTRL_CLK_STOP_SUSPEND bit 186 * to stop the logic clock during suspend because 187 * it causes the registers to become inaccessible and 188 * we haven't yet figured out a good wayt to bring the 189 * controller back into life to issue a wakeup. 190 */ 191 192 /* 193 * Set some ISO & split control bits according to Aspeed 194 * recommendation 195 * 196 * VHUB_CTRL_ISO_RSP_CTRL: When set tells the HW to respond 197 * with 0 bytes data packet to ISO IN endpoints when no data 198 * is available. 199 * 200 * VHUB_CTRL_SPLIT_IN: This makes a SOF complete a split IN 201 * transaction. 202 */ 203 ctrl |= VHUB_CTRL_ISO_RSP_CTRL | VHUB_CTRL_SPLIT_IN; 204 writel(ctrl, vhub->regs + AST_VHUB_CTRL); 205 udelay(1); 206 207 /* Set descriptor ring size */ 208 if (AST_VHUB_DESCS_COUNT == 256) { 209 ctrl |= VHUB_CTRL_LONG_DESC; 210 writel(ctrl, vhub->regs + AST_VHUB_CTRL); 211 } else { 212 BUILD_BUG_ON(AST_VHUB_DESCS_COUNT != 32); 213 } 214 215 /* Reset all devices */ 216 port_mask = GENMASK(vhub->max_ports, 1); 217 writel(VHUB_SW_RESET_ROOT_HUB | 218 VHUB_SW_RESET_DMA_CONTROLLER | 219 VHUB_SW_RESET_EP_POOL | 220 port_mask, vhub->regs + AST_VHUB_SW_RESET); 221 udelay(1); 222 writel(0, vhub->regs + AST_VHUB_SW_RESET); 223 224 /* Disable and cleanup EP ACK/NACK interrupts */ 225 epn_mask = GENMASK(vhub->max_epns - 1, 0); 226 writel(0, vhub->regs + AST_VHUB_EP_ACK_IER); 227 writel(0, vhub->regs + AST_VHUB_EP_NACK_IER); 228 writel(epn_mask, vhub->regs + AST_VHUB_EP_ACK_ISR); 229 writel(epn_mask, vhub->regs + AST_VHUB_EP_NACK_ISR); 230 231 /* Default settings for EP0, enable HW hub EP1 */ 232 writel(0, vhub->regs + AST_VHUB_EP0_CTRL); 233 writel(VHUB_EP1_CTRL_RESET_TOGGLE | 234 VHUB_EP1_CTRL_ENABLE, 235 vhub->regs + AST_VHUB_EP1_CTRL); 236 writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG); 237 238 /* Configure EP0 DMA buffer */ 239 writel(vhub->ep0.buf_dma, vhub->regs + AST_VHUB_EP0_DATA); 240 241 /* Clear address */ 242 writel(0, vhub->regs + AST_VHUB_CONF); 243 244 /* Pullup hub (activate on host) */ 245 if (vhub->force_usb1) 246 ctrl |= VHUB_CTRL_FULL_SPEED_ONLY; 247 248 ctrl |= VHUB_CTRL_UPSTREAM_CONNECT; 249 writel(ctrl, vhub->regs + AST_VHUB_CTRL); 250 251 /* Enable some interrupts */ 252 writel(VHUB_IRQ_HUB_EP0_IN_ACK_STALL | 253 VHUB_IRQ_HUB_EP0_OUT_ACK_STALL | 254 VHUB_IRQ_HUB_EP0_SETUP | 255 VHUB_IRQ_EP_POOL_ACK_STALL | 256 VHUB_IRQ_BUS_RESUME | 257 VHUB_IRQ_BUS_SUSPEND | 258 VHUB_IRQ_BUS_RESET, 259 vhub->regs + AST_VHUB_IER); 260 } 261 262 static int ast_vhub_remove(struct platform_device *pdev) 263 { 264 struct ast_vhub *vhub = platform_get_drvdata(pdev); 265 unsigned long flags; 266 int i; 267 268 if (!vhub || !vhub->regs) 269 return 0; 270 271 /* Remove devices */ 272 for (i = 0; i < vhub->max_ports; i++) 273 ast_vhub_del_dev(&vhub->ports[i].dev); 274 275 spin_lock_irqsave(&vhub->lock, flags); 276 277 /* Mask & ack all interrupts */ 278 writel(0, vhub->regs + AST_VHUB_IER); 279 writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR); 280 281 /* Pull device, leave PHY enabled */ 282 writel(VHUB_CTRL_PHY_CLK | 283 VHUB_CTRL_PHY_RESET_DIS, 284 vhub->regs + AST_VHUB_CTRL); 285 286 if (vhub->clk) 287 clk_disable_unprepare(vhub->clk); 288 289 spin_unlock_irqrestore(&vhub->lock, flags); 290 291 if (vhub->ep0_bufs) 292 dma_free_coherent(&pdev->dev, 293 AST_VHUB_EP0_MAX_PACKET * 294 (vhub->max_ports + 1), 295 vhub->ep0_bufs, 296 vhub->ep0_bufs_dma); 297 vhub->ep0_bufs = NULL; 298 299 return 0; 300 } 301 302 static int ast_vhub_probe(struct platform_device *pdev) 303 { 304 enum usb_device_speed max_speed; 305 struct ast_vhub *vhub; 306 struct resource *res; 307 int i, rc = 0; 308 const struct device_node *np = pdev->dev.of_node; 309 310 vhub = devm_kzalloc(&pdev->dev, sizeof(*vhub), GFP_KERNEL); 311 if (!vhub) 312 return -ENOMEM; 313 314 rc = of_property_read_u32(np, "aspeed,vhub-downstream-ports", 315 &vhub->max_ports); 316 if (rc < 0) 317 vhub->max_ports = AST_VHUB_NUM_PORTS; 318 319 vhub->ports = devm_kcalloc(&pdev->dev, vhub->max_ports, 320 sizeof(*vhub->ports), GFP_KERNEL); 321 if (!vhub->ports) 322 return -ENOMEM; 323 324 rc = of_property_read_u32(np, "aspeed,vhub-generic-endpoints", 325 &vhub->max_epns); 326 if (rc < 0) 327 vhub->max_epns = AST_VHUB_NUM_GEN_EPs; 328 329 vhub->epns = devm_kcalloc(&pdev->dev, vhub->max_epns, 330 sizeof(*vhub->epns), GFP_KERNEL); 331 if (!vhub->epns) 332 return -ENOMEM; 333 334 spin_lock_init(&vhub->lock); 335 vhub->pdev = pdev; 336 vhub->port_irq_mask = GENMASK(VHUB_IRQ_DEV1_BIT + vhub->max_ports - 1, 337 VHUB_IRQ_DEV1_BIT); 338 339 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 340 vhub->regs = devm_ioremap_resource(&pdev->dev, res); 341 if (IS_ERR(vhub->regs)) { 342 dev_err(&pdev->dev, "Failed to map resources\n"); 343 return PTR_ERR(vhub->regs); 344 } 345 UDCDBG(vhub, "vHub@%pR mapped @%p\n", res, vhub->regs); 346 347 platform_set_drvdata(pdev, vhub); 348 349 vhub->clk = devm_clk_get(&pdev->dev, NULL); 350 if (IS_ERR(vhub->clk)) { 351 rc = PTR_ERR(vhub->clk); 352 goto err; 353 } 354 rc = clk_prepare_enable(vhub->clk); 355 if (rc) { 356 dev_err(&pdev->dev, "Error couldn't enable clock (%d)\n", rc); 357 goto err; 358 } 359 360 /* Check if we need to limit the HW to USB1 */ 361 max_speed = usb_get_maximum_speed(&pdev->dev); 362 if (max_speed != USB_SPEED_UNKNOWN && max_speed < USB_SPEED_HIGH) 363 vhub->force_usb1 = true; 364 365 /* Mask & ack all interrupts before installing the handler */ 366 writel(0, vhub->regs + AST_VHUB_IER); 367 writel(VHUB_IRQ_ACK_ALL, vhub->regs + AST_VHUB_ISR); 368 369 /* Find interrupt and install handler */ 370 vhub->irq = platform_get_irq(pdev, 0); 371 if (vhub->irq < 0) { 372 rc = vhub->irq; 373 goto err; 374 } 375 rc = devm_request_irq(&pdev->dev, vhub->irq, ast_vhub_irq, 0, 376 KBUILD_MODNAME, vhub); 377 if (rc) { 378 dev_err(&pdev->dev, "Failed to request interrupt\n"); 379 goto err; 380 } 381 382 /* 383 * Allocate DMA buffers for all EP0s in one chunk, 384 * one per port and one for the vHub itself 385 */ 386 vhub->ep0_bufs = dma_alloc_coherent(&pdev->dev, 387 AST_VHUB_EP0_MAX_PACKET * 388 (vhub->max_ports + 1), 389 &vhub->ep0_bufs_dma, GFP_KERNEL); 390 if (!vhub->ep0_bufs) { 391 dev_err(&pdev->dev, "Failed to allocate EP0 DMA buffers\n"); 392 rc = -ENOMEM; 393 goto err; 394 } 395 UDCVDBG(vhub, "EP0 DMA buffers @%p (DMA 0x%08x)\n", 396 vhub->ep0_bufs, (u32)vhub->ep0_bufs_dma); 397 398 /* Init vHub EP0 */ 399 ast_vhub_init_ep0(vhub, &vhub->ep0, NULL); 400 401 /* Init devices */ 402 for (i = 0; i < vhub->max_ports && rc == 0; i++) 403 rc = ast_vhub_init_dev(vhub, i); 404 if (rc) 405 goto err; 406 407 /* Init hub emulation */ 408 rc = ast_vhub_init_hub(vhub); 409 if (rc) 410 goto err; 411 412 /* Initialize HW */ 413 ast_vhub_init_hw(vhub); 414 415 dev_info(&pdev->dev, "Initialized virtual hub in USB%d mode\n", 416 vhub->force_usb1 ? 1 : 2); 417 418 return 0; 419 err: 420 ast_vhub_remove(pdev); 421 return rc; 422 } 423 424 static const struct of_device_id ast_vhub_dt_ids[] = { 425 { 426 .compatible = "aspeed,ast2400-usb-vhub", 427 }, 428 { 429 .compatible = "aspeed,ast2500-usb-vhub", 430 }, 431 { 432 .compatible = "aspeed,ast2600-usb-vhub", 433 }, 434 { } 435 }; 436 MODULE_DEVICE_TABLE(of, ast_vhub_dt_ids); 437 438 static struct platform_driver ast_vhub_driver = { 439 .probe = ast_vhub_probe, 440 .remove = ast_vhub_remove, 441 .driver = { 442 .name = KBUILD_MODNAME, 443 .of_match_table = ast_vhub_dt_ids, 444 }, 445 }; 446 module_platform_driver(ast_vhub_driver); 447 448 MODULE_DESCRIPTION("Aspeed vHub udc driver"); 449 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); 450 MODULE_LICENSE("GPL"); 451