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 * 30b02b371eSPeter Korsgaard * The c67x00 has 2 SIE's (serial interface engine) wich 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> 40*5a0e3ad6STejun Heo #include <linux/slab.h> 41b02b371eSPeter Korsgaard #include <linux/usb.h> 42b02b371eSPeter Korsgaard #include <linux/usb/c67x00.h> 43b02b371eSPeter Korsgaard 44b02b371eSPeter Korsgaard #include "c67x00.h" 45e9b29ffcSPeter Korsgaard #include "c67x00-hcd.h" 46b02b371eSPeter Korsgaard 47b02b371eSPeter Korsgaard static void c67x00_probe_sie(struct c67x00_sie *sie, 48b02b371eSPeter Korsgaard struct c67x00_device *dev, int sie_num) 49b02b371eSPeter Korsgaard { 50b02b371eSPeter Korsgaard spin_lock_init(&sie->lock); 51b02b371eSPeter Korsgaard sie->dev = dev; 52b02b371eSPeter Korsgaard sie->sie_num = sie_num; 53b02b371eSPeter Korsgaard sie->mode = c67x00_sie_config(dev->pdata->sie_config, sie_num); 54b02b371eSPeter Korsgaard 55b02b371eSPeter Korsgaard switch (sie->mode) { 56e9b29ffcSPeter Korsgaard case C67X00_SIE_HOST: 57e9b29ffcSPeter Korsgaard c67x00_hcd_probe(sie); 58e9b29ffcSPeter Korsgaard break; 59e9b29ffcSPeter Korsgaard 60b02b371eSPeter Korsgaard case C67X00_SIE_UNUSED: 61b02b371eSPeter Korsgaard dev_info(sie_dev(sie), 62b02b371eSPeter Korsgaard "Not using SIE %d as requested\n", sie->sie_num); 63b02b371eSPeter Korsgaard break; 64b02b371eSPeter Korsgaard 65b02b371eSPeter Korsgaard default: 66b02b371eSPeter Korsgaard dev_err(sie_dev(sie), 67b02b371eSPeter Korsgaard "Unsupported configuration: 0x%x for SIE %d\n", 68b02b371eSPeter Korsgaard sie->mode, sie->sie_num); 69b02b371eSPeter Korsgaard break; 70b02b371eSPeter Korsgaard } 71b02b371eSPeter Korsgaard } 72b02b371eSPeter Korsgaard 73b02b371eSPeter Korsgaard static void c67x00_remove_sie(struct c67x00_sie *sie) 74b02b371eSPeter Korsgaard { 75e9b29ffcSPeter Korsgaard switch (sie->mode) { 76e9b29ffcSPeter Korsgaard case C67X00_SIE_HOST: 77e9b29ffcSPeter Korsgaard c67x00_hcd_remove(sie); 78e9b29ffcSPeter Korsgaard break; 79e9b29ffcSPeter Korsgaard 80e9b29ffcSPeter Korsgaard default: 81e9b29ffcSPeter Korsgaard break; 82e9b29ffcSPeter Korsgaard } 83b02b371eSPeter Korsgaard } 84b02b371eSPeter Korsgaard 85b02b371eSPeter Korsgaard static irqreturn_t c67x00_irq(int irq, void *__dev) 86b02b371eSPeter Korsgaard { 87b02b371eSPeter Korsgaard struct c67x00_device *c67x00 = __dev; 88b02b371eSPeter Korsgaard struct c67x00_sie *sie; 89b02b371eSPeter Korsgaard u16 msg, int_status; 90b02b371eSPeter Korsgaard int i, count = 8; 91b02b371eSPeter Korsgaard 92b02b371eSPeter Korsgaard int_status = c67x00_ll_hpi_status(c67x00); 93b02b371eSPeter Korsgaard if (!int_status) 94b02b371eSPeter Korsgaard return IRQ_NONE; 95b02b371eSPeter Korsgaard 96b02b371eSPeter Korsgaard while (int_status != 0 && (count-- >= 0)) { 97b02b371eSPeter Korsgaard c67x00_ll_irq(c67x00, int_status); 98b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) { 99b02b371eSPeter Korsgaard sie = &c67x00->sie[i]; 100b02b371eSPeter Korsgaard msg = 0; 101b02b371eSPeter Korsgaard if (int_status & SIEMSG_FLG(i)) 102b02b371eSPeter Korsgaard msg = c67x00_ll_fetch_siemsg(c67x00, i); 103b02b371eSPeter Korsgaard if (sie->irq) 104b02b371eSPeter Korsgaard sie->irq(sie, int_status, msg); 105b02b371eSPeter Korsgaard } 106b02b371eSPeter Korsgaard int_status = c67x00_ll_hpi_status(c67x00); 107b02b371eSPeter Korsgaard } 108b02b371eSPeter Korsgaard 109b02b371eSPeter Korsgaard if (int_status) 110b02b371eSPeter Korsgaard dev_warn(&c67x00->pdev->dev, "Not all interrupts handled! " 111b02b371eSPeter Korsgaard "status = 0x%04x\n", int_status); 112b02b371eSPeter Korsgaard 113b02b371eSPeter Korsgaard return IRQ_HANDLED; 114b02b371eSPeter Korsgaard } 115b02b371eSPeter Korsgaard 116b02b371eSPeter Korsgaard /* ------------------------------------------------------------------------- */ 117b02b371eSPeter Korsgaard 118b02b371eSPeter Korsgaard static int __devinit c67x00_drv_probe(struct platform_device *pdev) 119b02b371eSPeter Korsgaard { 120b02b371eSPeter Korsgaard struct c67x00_device *c67x00; 121b02b371eSPeter Korsgaard struct c67x00_platform_data *pdata; 122b02b371eSPeter Korsgaard struct resource *res, *res2; 123b02b371eSPeter Korsgaard int ret, i; 124b02b371eSPeter Korsgaard 125b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 126b02b371eSPeter Korsgaard if (!res) 127b02b371eSPeter Korsgaard return -ENODEV; 128b02b371eSPeter Korsgaard 129b02b371eSPeter Korsgaard res2 = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 130b02b371eSPeter Korsgaard if (!res2) 131b02b371eSPeter Korsgaard return -ENODEV; 132b02b371eSPeter Korsgaard 133b02b371eSPeter Korsgaard pdata = pdev->dev.platform_data; 134b02b371eSPeter Korsgaard if (!pdata) 135b02b371eSPeter Korsgaard return -ENODEV; 136b02b371eSPeter Korsgaard 137b02b371eSPeter Korsgaard c67x00 = kzalloc(sizeof(*c67x00), GFP_KERNEL); 138b02b371eSPeter Korsgaard if (!c67x00) 139b02b371eSPeter Korsgaard return -ENOMEM; 140b02b371eSPeter Korsgaard 141c38b9401SThiago Farina if (!request_mem_region(res->start, resource_size(res), 142b02b371eSPeter Korsgaard pdev->name)) { 143b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Memory region busy\n"); 144b02b371eSPeter Korsgaard ret = -EBUSY; 145b02b371eSPeter Korsgaard goto request_mem_failed; 146b02b371eSPeter Korsgaard } 147c38b9401SThiago Farina c67x00->hpi.base = ioremap(res->start, resource_size(res)); 148b02b371eSPeter Korsgaard if (!c67x00->hpi.base) { 149b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Unable to map HPI registers\n"); 150b02b371eSPeter Korsgaard ret = -EIO; 151b02b371eSPeter Korsgaard goto map_failed; 152b02b371eSPeter Korsgaard } 153b02b371eSPeter Korsgaard 154b02b371eSPeter Korsgaard spin_lock_init(&c67x00->hpi.lock); 155b02b371eSPeter Korsgaard c67x00->hpi.regstep = pdata->hpi_regstep; 156b02b371eSPeter Korsgaard c67x00->pdata = pdev->dev.platform_data; 157b02b371eSPeter Korsgaard c67x00->pdev = pdev; 158b02b371eSPeter Korsgaard 159b02b371eSPeter Korsgaard c67x00_ll_init(c67x00); 160b02b371eSPeter Korsgaard c67x00_ll_hpi_reg_init(c67x00); 161b02b371eSPeter Korsgaard 162b02b371eSPeter Korsgaard ret = request_irq(res2->start, c67x00_irq, 0, pdev->name, c67x00); 163b02b371eSPeter Korsgaard if (ret) { 164b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Cannot claim IRQ\n"); 165b02b371eSPeter Korsgaard goto request_irq_failed; 166b02b371eSPeter Korsgaard } 167b02b371eSPeter Korsgaard 168b02b371eSPeter Korsgaard ret = c67x00_ll_reset(c67x00); 169b02b371eSPeter Korsgaard if (ret) { 170b02b371eSPeter Korsgaard dev_err(&pdev->dev, "Device reset failed\n"); 171b02b371eSPeter Korsgaard goto reset_failed; 172b02b371eSPeter Korsgaard } 173b02b371eSPeter Korsgaard 174b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) 175b02b371eSPeter Korsgaard c67x00_probe_sie(&c67x00->sie[i], c67x00, i); 176b02b371eSPeter Korsgaard 177b02b371eSPeter Korsgaard platform_set_drvdata(pdev, c67x00); 178b02b371eSPeter Korsgaard 179b02b371eSPeter Korsgaard return 0; 180b02b371eSPeter Korsgaard 181b02b371eSPeter Korsgaard reset_failed: 182b02b371eSPeter Korsgaard free_irq(res2->start, c67x00); 183b02b371eSPeter Korsgaard request_irq_failed: 184b02b371eSPeter Korsgaard iounmap(c67x00->hpi.base); 185b02b371eSPeter Korsgaard map_failed: 186c38b9401SThiago Farina release_mem_region(res->start, resource_size(res)); 187b02b371eSPeter Korsgaard request_mem_failed: 188b02b371eSPeter Korsgaard kfree(c67x00); 189b02b371eSPeter Korsgaard 190b02b371eSPeter Korsgaard return ret; 191b02b371eSPeter Korsgaard } 192b02b371eSPeter Korsgaard 193b02b371eSPeter Korsgaard static int __devexit c67x00_drv_remove(struct platform_device *pdev) 194b02b371eSPeter Korsgaard { 195b02b371eSPeter Korsgaard struct c67x00_device *c67x00 = platform_get_drvdata(pdev); 196b02b371eSPeter Korsgaard struct resource *res; 197b02b371eSPeter Korsgaard int i; 198b02b371eSPeter Korsgaard 199b02b371eSPeter Korsgaard for (i = 0; i < C67X00_SIES; i++) 200b02b371eSPeter Korsgaard c67x00_remove_sie(&c67x00->sie[i]); 201b02b371eSPeter Korsgaard 202b02b371eSPeter Korsgaard c67x00_ll_release(c67x00); 203b02b371eSPeter Korsgaard 204b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 205b02b371eSPeter Korsgaard if (res) 206b02b371eSPeter Korsgaard free_irq(res->start, c67x00); 207b02b371eSPeter Korsgaard 208b02b371eSPeter Korsgaard iounmap(c67x00->hpi.base); 209b02b371eSPeter Korsgaard 210b02b371eSPeter Korsgaard res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 211b02b371eSPeter Korsgaard if (res) 212c38b9401SThiago Farina release_mem_region(res->start, resource_size(res)); 213b02b371eSPeter Korsgaard 214b02b371eSPeter Korsgaard kfree(c67x00); 215b02b371eSPeter Korsgaard 216b02b371eSPeter Korsgaard return 0; 217b02b371eSPeter Korsgaard } 218b02b371eSPeter Korsgaard 219b02b371eSPeter Korsgaard static struct platform_driver c67x00_driver = { 220b02b371eSPeter Korsgaard .probe = c67x00_drv_probe, 221b02b371eSPeter Korsgaard .remove = __devexit_p(c67x00_drv_remove), 222b02b371eSPeter Korsgaard .driver = { 223b02b371eSPeter Korsgaard .owner = THIS_MODULE, 224b02b371eSPeter Korsgaard .name = "c67x00", 225b02b371eSPeter Korsgaard }, 226b02b371eSPeter Korsgaard }; 227b02b371eSPeter Korsgaard MODULE_ALIAS("platform:c67x00"); 228b02b371eSPeter Korsgaard 229b02b371eSPeter Korsgaard static int __init c67x00_init(void) 230b02b371eSPeter Korsgaard { 231b02b371eSPeter Korsgaard return platform_driver_register(&c67x00_driver); 232b02b371eSPeter Korsgaard } 233b02b371eSPeter Korsgaard 234b02b371eSPeter Korsgaard static void __exit c67x00_exit(void) 235b02b371eSPeter Korsgaard { 236b02b371eSPeter Korsgaard platform_driver_unregister(&c67x00_driver); 237b02b371eSPeter Korsgaard } 238b02b371eSPeter Korsgaard 239b02b371eSPeter Korsgaard module_init(c67x00_init); 240b02b371eSPeter Korsgaard module_exit(c67x00_exit); 241b02b371eSPeter Korsgaard 242b02b371eSPeter Korsgaard MODULE_AUTHOR("Peter Korsgaard, Jan Veldeman, Grant Likely"); 243b02b371eSPeter Korsgaard MODULE_DESCRIPTION("Cypress C67X00 USB Controller Driver"); 244b02b371eSPeter Korsgaard MODULE_LICENSE("GPL"); 245