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