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