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