1*5fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+ 2b02b371eSPeter Korsgaard /* 3b02b371eSPeter Korsgaard * c67x00-drv.c: Cypress C67X00 USB Common infrastructure 4b02b371eSPeter Korsgaard * 5b02b371eSPeter Korsgaard * Copyright (C) 2006-2008 Barco N.V. 6b02b371eSPeter Korsgaard * Derived from the Cypress cy7c67200/300 ezusb linux driver and 7b02b371eSPeter Korsgaard * based on multiple host controller drivers inside the linux kernel. 8b02b371eSPeter Korsgaard * 9b02b371eSPeter Korsgaard * This program is free software; you can redistribute it and/or modify 10b02b371eSPeter Korsgaard * it under the terms of the GNU General Public License as published by 11b02b371eSPeter Korsgaard * the Free Software Foundation; either version 2 of the License, or 12b02b371eSPeter Korsgaard * (at your option) any later version. 13b02b371eSPeter Korsgaard * 14b02b371eSPeter Korsgaard * This program is distributed in the hope that it will be useful, 15b02b371eSPeter Korsgaard * but WITHOUT ANY WARRANTY; without even the implied warranty of 16b02b371eSPeter Korsgaard * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17b02b371eSPeter Korsgaard * GNU General Public License for more details. 18b02b371eSPeter Korsgaard * 19b02b371eSPeter Korsgaard * You should have received a copy of the GNU General Public License 20b02b371eSPeter Korsgaard * along with this program; if not, write to the Free Software 21b02b371eSPeter Korsgaard * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 22b02b371eSPeter Korsgaard * MA 02110-1301 USA. 23b02b371eSPeter Korsgaard */ 24b02b371eSPeter Korsgaard 25b02b371eSPeter Korsgaard /* 26b02b371eSPeter Korsgaard * This file implements the common infrastructure for using the c67x00. 27b02b371eSPeter Korsgaard * It is both the link between the platform configuration and subdrivers and 28b02b371eSPeter Korsgaard * the link between the common hardware parts and the subdrivers (e.g. 29b02b371eSPeter Korsgaard * interrupt handling). 30b02b371eSPeter Korsgaard * 3125985edcSLucas De Marchi * The c67x00 has 2 SIE's (serial interface engine) which can be configured 32b02b371eSPeter Korsgaard * to be host, device or OTG (with some limitations, E.G. only SIE1 can be OTG). 33b02b371eSPeter Korsgaard * 34b02b371eSPeter Korsgaard * Depending on the platform configuration, the SIE's are created and 35b02b371eSPeter Korsgaard * the corresponding subdriver is initialized (c67x00_probe_sie). 36b02b371eSPeter Korsgaard */ 37b02b371eSPeter Korsgaard 38b02b371eSPeter Korsgaard #include <linux/device.h> 39b02b371eSPeter Korsgaard #include <linux/io.h> 40b02b371eSPeter Korsgaard #include <linux/list.h> 415a0e3ad6STejun Heo #include <linux/slab.h> 426eb0de82SPaul Gortmaker #include <linux/module.h> 43b02b371eSPeter Korsgaard #include <linux/usb.h> 44b02b371eSPeter Korsgaard #include <linux/usb/c67x00.h> 45b02b371eSPeter Korsgaard 46b02b371eSPeter Korsgaard #include "c67x00.h" 47e9b29ffcSPeter Korsgaard #include "c67x00-hcd.h" 48b02b371eSPeter Korsgaard 49b02b371eSPeter Korsgaard static void c67x00_probe_sie(struct c67x00_sie *sie, 50b02b371eSPeter Korsgaard struct c67x00_device *dev, int sie_num) 51b02b371eSPeter Korsgaard { 52b02b371eSPeter Korsgaard spin_lock_init(&sie->lock); 53b02b371eSPeter Korsgaard sie->dev = dev; 54b02b371eSPeter Korsgaard sie->sie_num = sie_num; 55b02b371eSPeter Korsgaard sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num); 56b02b371eSPeter Korsgaard 57b02b371eSPeter Korsgaard switch (sie->mode) { 58e9b29ffcSPeter Korsgaard case C67X00_SIE_HOST: 59e9b29ffcSPeter Korsgaard c67x00_hcd_probe(sie); 60e9b29ffcSPeter Korsgaard break; 61e9b29ffcSPeter Korsgaard 62b02b371eSPeter Korsgaard case C67X00_SIE_UNUSED: 63b02b371eSPeter Korsgaard dev_info(sie_dev(sie), 64b02b371eSPeter Korsgaard "Not using SIE %d as requested\n", sie->sie_num); 65b02b371eSPeter Korsgaard break; 66b02b371eSPeter Korsgaard 67b02b371eSPeter Korsgaard default: 68b02b371eSPeter Korsgaard dev_err(sie_dev(sie), 69b02b371eSPeter Korsgaard "Unsupported configuration: 0x%x for SIE %d\n", 70b02b371eSPeter Korsgaard sie->mode, sie->sie_num); 71b02b371eSPeter Korsgaard break; 72b02b371eSPeter Korsgaard } 73b02b371eSPeter Korsgaard } 74b02b371eSPeter Korsgaard 75b02b371eSPeter Korsgaard static void c67x00_remove_sie(struct c67x00_sie *sie) 76b02b371eSPeter Korsgaard { 77e9b29ffcSPeter Korsgaard switch (sie->mode) { 78e9b29ffcSPeter Korsgaard case C67X00_SIE_HOST: 79e9b29ffcSPeter Korsgaard c67x00_hcd_remove(sie); 80e9b29ffcSPeter Korsgaard break; 81e9b29ffcSPeter Korsgaard 82e9b29ffcSPeter Korsgaard default: 83e9b29ffcSPeter Korsgaard break; 84e9b29ffcSPeter Korsgaard } 85b02b371eSPeter Korsgaard } 86b02b371eSPeter Korsgaard 87b02b371eSPeter Korsgaard static irqreturn_t c67x00_irq(int irq, void *__dev) 88b02b371eSPeter Korsgaard { 89b02b371eSPeter Korsgaard struct c67x00_device *c67x00 = __dev; 90b02b371eSPeter Korsgaard struct c67x00_sie *sie; 91b02b371eSPeter Korsgaard u16 msg, int_status; 92b02b371eSPeter Korsgaard int i, count = 8; 93b02b371eSPeter Korsgaard 94b02b371eSPeter Korsgaard int_status = c67x00_ll_hpi_status(c67x00); 95b02b371eSPeter Korsgaard if (!int_status) 96b02b371eSPeter Korsgaard return IRQ_NONE; 97b02b371eSPeter Korsgaard 98b02b371eSPeter Korsgaard while (int_status != 0 && (count-- >= 0)) { 99b02b371eSPeter Korsgaard c67x00_ll_irq(c67x00, int_status); 100b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) { 101b02b371eSPeter Korsgaard sie = &c67x00->sie[i]; 102b02b371eSPeter Korsgaard msg = 0; 103b02b371eSPeter Korsgaard if (int_status & SIEMSG_FLG(i)) 104b02b371eSPeter Korsgaard msg = c67x00_ll_fetch_siemsg(c67x00, i); 105b02b371eSPeter Korsgaard if (sie->irq) 106b02b371eSPeter Korsgaard sie->irq(sie, int_status, msg); 107b02b371eSPeter Korsgaard } 108b02b371eSPeter Korsgaard int_status = c67x00_ll_hpi_status(c67x00); 109b02b371eSPeter Korsgaard } 110b02b371eSPeter Korsgaard 111b02b371eSPeter Korsgaard if (int_status) 112b02b371eSPeter Korsgaard dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! " 113b02b371eSPeter Korsgaard "status = 0x%04x\n", int_status); 114b02b371eSPeter Korsgaard 115b02b371eSPeter Korsgaard return IRQ_HANDLED; 116b02b371eSPeter Korsgaard } 117b02b371eSPeter Korsgaard 118b02b371eSPeter Korsgaard /* ------------------------------------------------------------------------- */ 119b02b371eSPeter Korsgaard 12041ac7b3aSBill Pemberton static int c67x00_drv_probe(struct platform_device *pdev) 121b02b371eSPeter Korsgaard { 122b02b371eSPeter Korsgaard struct c67x00_device *c67x00; 123b02b371eSPeter Korsgaard struct c67x00_platform_data *pdata; 124b02b371eSPeter Korsgaard struct resource *res, *res2; 125b02b371eSPeter Korsgaard int ret, i; 126b02b371eSPeter Korsgaard 127b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 128b02b371eSPeter Korsgaard if (!res) 129b02b371eSPeter Korsgaard return -ENODEV; 130b02b371eSPeter Korsgaard 131b02b371eSPeter Korsgaard res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 132b02b371eSPeter Korsgaard if (!res2) 133b02b371eSPeter Korsgaard return -ENODEV; 134b02b371eSPeter Korsgaard 135720ce6e4SJingoo Han pdata = dev_get_platdata(&pdev->dev); 136b02b371eSPeter Korsgaard if (!pdata) 137b02b371eSPeter Korsgaard return -ENODEV; 138b02b371eSPeter Korsgaard 139b02b371eSPeter Korsgaard c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL); 140b02b371eSPeter Korsgaard if (!c67x00) 141b02b371eSPeter Korsgaard return -ENOMEM; 142b02b371eSPeter Korsgaard 143c38b9401SThiago Farina if (!request_mem_region(res->start, resource_size(res), 144b02b371eSPeter Korsgaard pdev->name)) { 145b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Memory region busy\n"); 146b02b371eSPeter Korsgaard ret = -EBUSY; 147b02b371eSPeter Korsgaard goto request_mem_failed; 148b02b371eSPeter Korsgaard } 149c38b9401SThiago Farina c67x00->hpi.base = ioremap(res->start, resource_size(res)); 150b02b371eSPeter Korsgaard if (!c67x00->hpi.base) { 151b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Unable to map HPI registers\n"); 152b02b371eSPeter Korsgaard ret = -EIO; 153b02b371eSPeter Korsgaard goto map_failed; 154b02b371eSPeter Korsgaard } 155b02b371eSPeter Korsgaard 156b02b371eSPeter Korsgaard spin_lock_init(&c67x00->hpi.lock); 157b02b371eSPeter Korsgaard c67x00->hpi.regstep = pdata->hpi_regstep; 158720ce6e4SJingoo Han c67x00->pdata = dev_get_platdata(&pdev->dev); 159b02b371eSPeter Korsgaard c67x00->pdev = pdev; 160b02b371eSPeter Korsgaard 161b02b371eSPeter Korsgaard c67x00_ll_init(c67x00); 162b02b371eSPeter Korsgaard c67x00_ll_hpi_reg_init(c67x00); 163b02b371eSPeter Korsgaard 164b02b371eSPeter Korsgaard ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00); 165b02b371eSPeter Korsgaard if (ret) { 166b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Cannot claim IRQ\n"); 167b02b371eSPeter Korsgaard goto request_irq_failed; 168b02b371eSPeter Korsgaard } 169b02b371eSPeter Korsgaard 170b02b371eSPeter Korsgaard ret = c67x00_ll_reset(c67x00); 171b02b371eSPeter Korsgaard if (ret) { 172b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Device reset failed\n"); 173b02b371eSPeter Korsgaard goto reset_failed; 174b02b371eSPeter Korsgaard } 175b02b371eSPeter Korsgaard 176b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) 177b02b371eSPeter Korsgaard c67x00_probe_sie(&c67x00->sie[i], c67x00, i); 178b02b371eSPeter Korsgaard 179b02b371eSPeter Korsgaard platform_set_drvdata(pdev, c67x00); 180b02b371eSPeter Korsgaard 181b02b371eSPeter Korsgaard return 0; 182b02b371eSPeter Korsgaard 183b02b371eSPeter Korsgaard reset_failed: 184b02b371eSPeter Korsgaard free_irq(res2->start, c67x00); 185b02b371eSPeter Korsgaard request_irq_failed: 186b02b371eSPeter Korsgaard iounmap(c67x00->hpi.base); 187b02b371eSPeter Korsgaard map_failed: 188c38b9401SThiago Farina release_mem_region(res->start, resource_size(res)); 189b02b371eSPeter Korsgaard request_mem_failed: 190b02b371eSPeter Korsgaard kfree(c67x00); 191b02b371eSPeter Korsgaard 192b02b371eSPeter Korsgaard return ret; 193b02b371eSPeter Korsgaard } 194b02b371eSPeter Korsgaard 195fb4e98abSBill Pemberton static int c67x00_drv_remove(struct platform_device *pdev) 196b02b371eSPeter Korsgaard { 197b02b371eSPeter Korsgaard struct c67x00_device *c67x00 = platform_get_drvdata(pdev); 198b02b371eSPeter Korsgaard struct resource *res; 199b02b371eSPeter Korsgaard int i; 200b02b371eSPeter Korsgaard 201b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) 202b02b371eSPeter Korsgaard c67x00_remove_sie(&c67x00->sie[i]); 203b02b371eSPeter Korsgaard 204b02b371eSPeter Korsgaard c67x00_ll_release(c67x00); 205b02b371eSPeter Korsgaard 206b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 207b02b371eSPeter Korsgaard if (res) 208b02b371eSPeter Korsgaard free_irq(res->start, c67x00); 209b02b371eSPeter Korsgaard 210b02b371eSPeter Korsgaard iounmap(c67x00->hpi.base); 211b02b371eSPeter Korsgaard 212b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 213b02b371eSPeter Korsgaard if (res) 214c38b9401SThiago Farina release_mem_region(res->start, resource_size(res)); 215b02b371eSPeter Korsgaard 216b02b371eSPeter Korsgaard kfree(c67x00); 217b02b371eSPeter Korsgaard 218b02b371eSPeter Korsgaard return 0; 219b02b371eSPeter Korsgaard } 220b02b371eSPeter Korsgaard 221b02b371eSPeter Korsgaard static struct platform_driver c67x00_driver = { 222b02b371eSPeter Korsgaard .probe = c67x00_drv_probe, 2237690417dSBill Pemberton .remove = c67x00_drv_remove, 224b02b371eSPeter Korsgaard .driver = { 225b02b371eSPeter Korsgaard .name = "c67x00", 226b02b371eSPeter Korsgaard }, 227b02b371eSPeter Korsgaard }; 228b02b371eSPeter Korsgaard 229cc27c96cSAxel Lin module_platform_driver(c67x00_driver); 230b02b371eSPeter Korsgaard 231b02b371eSPeter Korsgaard MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely"); 232b02b371eSPeter Korsgaard MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver"); 233b02b371eSPeter Korsgaard MODULE_LICENSE("GPL"); 234cc27c96cSAxel Lin MODULE_ALIAS("platform:c67x00"); 235