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