Olof Johansson | 2b571a0 | 2007-10-16 23:26:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 PA Semi, Inc |
| 3 | * |
| 4 | * Maintained by: Olof Johansson <olof@lixom.net> |
| 5 | * |
| 6 | * Based on drivers/pcmcia/omap_cf.c |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/sched.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/delay.h> |
| 30 | #include <linux/interrupt.h> |
Andrea Righi | 27ac792 | 2008-07-23 21:28:13 -0700 | [diff] [blame] | 31 | #include <linux/mm.h> |
Olof Johansson | 2b571a0 | 2007-10-16 23:26:20 -0700 | [diff] [blame] | 32 | #include <linux/vmalloc.h> |
Stephen Rothwell | ad19dbd | 2008-05-23 16:31:08 +1000 | [diff] [blame] | 33 | #include <linux/of_platform.h> |
Olof Johansson | 2b571a0 | 2007-10-16 23:26:20 -0700 | [diff] [blame] | 34 | |
| 35 | #include <pcmcia/ss.h> |
Olof Johansson | 2b571a0 | 2007-10-16 23:26:20 -0700 | [diff] [blame] | 36 | |
| 37 | static const char driver_name[] = "electra-cf"; |
| 38 | |
| 39 | struct electra_cf_socket { |
| 40 | struct pcmcia_socket socket; |
| 41 | |
| 42 | struct timer_list timer; |
| 43 | unsigned present:1; |
| 44 | unsigned active:1; |
| 45 | |
| 46 | struct of_device *ofdev; |
| 47 | unsigned long mem_phys; |
| 48 | void __iomem * mem_base; |
| 49 | unsigned long mem_size; |
| 50 | void __iomem * io_virt; |
| 51 | unsigned int io_base; |
| 52 | unsigned int io_size; |
| 53 | u_int irq; |
| 54 | struct resource iomem; |
| 55 | void __iomem * gpio_base; |
| 56 | int gpio_detect; |
| 57 | int gpio_vsense; |
| 58 | int gpio_3v; |
| 59 | int gpio_5v; |
| 60 | }; |
| 61 | |
| 62 | #define POLL_INTERVAL (2 * HZ) |
| 63 | |
| 64 | |
| 65 | static int electra_cf_present(struct electra_cf_socket *cf) |
| 66 | { |
| 67 | unsigned int gpio; |
| 68 | |
| 69 | gpio = in_le32(cf->gpio_base+0x40); |
| 70 | return !(gpio & (1 << cf->gpio_detect)); |
| 71 | } |
| 72 | |
| 73 | static int electra_cf_ss_init(struct pcmcia_socket *s) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* the timer is primarily to kick this socket's pccardd */ |
| 79 | static void electra_cf_timer(unsigned long _cf) |
| 80 | { |
| 81 | struct electra_cf_socket *cf = (void *) _cf; |
| 82 | int present = electra_cf_present(cf); |
| 83 | |
| 84 | if (present != cf->present) { |
| 85 | cf->present = present; |
| 86 | pcmcia_parse_events(&cf->socket, SS_DETECT); |
| 87 | } |
| 88 | |
| 89 | if (cf->active) |
| 90 | mod_timer(&cf->timer, jiffies + POLL_INTERVAL); |
| 91 | } |
| 92 | |
| 93 | static irqreturn_t electra_cf_irq(int irq, void *_cf) |
| 94 | { |
| 95 | electra_cf_timer((unsigned long)_cf); |
| 96 | return IRQ_HANDLED; |
| 97 | } |
| 98 | |
| 99 | static int electra_cf_get_status(struct pcmcia_socket *s, u_int *sp) |
| 100 | { |
| 101 | struct electra_cf_socket *cf; |
| 102 | |
| 103 | if (!sp) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | cf = container_of(s, struct electra_cf_socket, socket); |
| 107 | |
| 108 | /* NOTE CF is always 3VCARD */ |
| 109 | if (electra_cf_present(cf)) { |
| 110 | *sp = SS_READY | SS_DETECT | SS_POWERON | SS_3VCARD; |
| 111 | |
| 112 | s->pci_irq = cf->irq; |
| 113 | } else |
| 114 | *sp = 0; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int electra_cf_set_socket(struct pcmcia_socket *sock, |
| 119 | struct socket_state_t *s) |
| 120 | { |
| 121 | unsigned int gpio; |
| 122 | unsigned int vcc; |
| 123 | struct electra_cf_socket *cf; |
| 124 | |
| 125 | cf = container_of(sock, struct electra_cf_socket, socket); |
| 126 | |
| 127 | /* "reset" means no power in our case */ |
| 128 | vcc = (s->flags & SS_RESET) ? 0 : s->Vcc; |
| 129 | |
| 130 | switch (vcc) { |
| 131 | case 0: |
| 132 | gpio = 0; |
| 133 | break; |
| 134 | case 33: |
| 135 | gpio = (1 << cf->gpio_3v); |
| 136 | break; |
| 137 | case 5: |
| 138 | gpio = (1 << cf->gpio_5v); |
| 139 | break; |
| 140 | default: |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
| 144 | gpio |= 1 << (cf->gpio_3v + 16); /* enwr */ |
| 145 | gpio |= 1 << (cf->gpio_5v + 16); /* enwr */ |
| 146 | out_le32(cf->gpio_base+0x90, gpio); |
| 147 | |
| 148 | pr_debug("%s: Vcc %d, io_irq %d, flags %04x csc %04x\n", |
| 149 | driver_name, s->Vcc, s->io_irq, s->flags, s->csc_mask); |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static int electra_cf_set_io_map(struct pcmcia_socket *s, |
| 155 | struct pccard_io_map *io) |
| 156 | { |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int electra_cf_set_mem_map(struct pcmcia_socket *s, |
| 161 | struct pccard_mem_map *map) |
| 162 | { |
| 163 | struct electra_cf_socket *cf; |
| 164 | |
| 165 | if (map->card_start) |
| 166 | return -EINVAL; |
| 167 | cf = container_of(s, struct electra_cf_socket, socket); |
| 168 | map->static_start = cf->mem_phys; |
| 169 | map->flags &= MAP_ACTIVE|MAP_ATTRIB; |
| 170 | if (!(map->flags & MAP_ATTRIB)) |
| 171 | map->static_start += 0x800; |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static struct pccard_operations electra_cf_ops = { |
| 176 | .init = electra_cf_ss_init, |
| 177 | .get_status = electra_cf_get_status, |
| 178 | .set_socket = electra_cf_set_socket, |
| 179 | .set_io_map = electra_cf_set_io_map, |
| 180 | .set_mem_map = electra_cf_set_mem_map, |
| 181 | }; |
| 182 | |
| 183 | static int __devinit electra_cf_probe(struct of_device *ofdev, |
| 184 | const struct of_device_id *match) |
| 185 | { |
| 186 | struct device *device = &ofdev->dev; |
| 187 | struct device_node *np = ofdev->node; |
| 188 | struct electra_cf_socket *cf; |
| 189 | struct resource mem, io; |
| 190 | int status; |
| 191 | const unsigned int *prop; |
| 192 | int err; |
| 193 | struct vm_struct *area; |
| 194 | |
| 195 | err = of_address_to_resource(np, 0, &mem); |
| 196 | if (err) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | err = of_address_to_resource(np, 1, &io); |
| 200 | if (err) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | cf = kzalloc(sizeof *cf, GFP_KERNEL); |
| 204 | if (!cf) |
| 205 | return -ENOMEM; |
| 206 | |
| 207 | setup_timer(&cf->timer, electra_cf_timer, (unsigned long)cf); |
| 208 | cf->irq = NO_IRQ; |
| 209 | |
| 210 | cf->ofdev = ofdev; |
| 211 | cf->mem_phys = mem.start; |
| 212 | cf->mem_size = PAGE_ALIGN(mem.end - mem.start); |
| 213 | cf->mem_base = ioremap(cf->mem_phys, cf->mem_size); |
| 214 | cf->io_size = PAGE_ALIGN(io.end - io.start); |
| 215 | |
| 216 | area = __get_vm_area(cf->io_size, 0, PHB_IO_BASE, PHB_IO_END); |
| 217 | if (area == NULL) |
| 218 | return -ENOMEM; |
| 219 | |
| 220 | cf->io_virt = (void __iomem *)(area->addr); |
| 221 | |
| 222 | cf->gpio_base = ioremap(0xfc103000, 0x1000); |
| 223 | dev_set_drvdata(device, cf); |
| 224 | |
| 225 | if (!cf->mem_base || !cf->io_virt || !cf->gpio_base || |
| 226 | (__ioremap_at(io.start, cf->io_virt, cf->io_size, |
| 227 | _PAGE_NO_CACHE | _PAGE_GUARDED) == NULL)) { |
| 228 | dev_err(device, "can't ioremap ranges\n"); |
| 229 | status = -ENOMEM; |
| 230 | goto fail1; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | cf->io_base = (unsigned long)cf->io_virt - VMALLOC_END; |
| 235 | |
| 236 | cf->iomem.start = (unsigned long)cf->mem_base; |
| 237 | cf->iomem.end = (unsigned long)cf->mem_base + (mem.end - mem.start); |
| 238 | cf->iomem.flags = IORESOURCE_MEM; |
| 239 | |
| 240 | cf->irq = irq_of_parse_and_map(np, 0); |
| 241 | |
| 242 | status = request_irq(cf->irq, electra_cf_irq, IRQF_SHARED, |
| 243 | driver_name, cf); |
| 244 | if (status < 0) { |
| 245 | dev_err(device, "request_irq failed\n"); |
| 246 | goto fail1; |
| 247 | } |
| 248 | |
| 249 | cf->socket.pci_irq = cf->irq; |
| 250 | |
| 251 | prop = of_get_property(np, "card-detect-gpio", NULL); |
| 252 | if (!prop) |
| 253 | goto fail1; |
| 254 | cf->gpio_detect = *prop; |
| 255 | |
| 256 | prop = of_get_property(np, "card-vsense-gpio", NULL); |
| 257 | if (!prop) |
| 258 | goto fail1; |
| 259 | cf->gpio_vsense = *prop; |
| 260 | |
| 261 | prop = of_get_property(np, "card-3v-gpio", NULL); |
| 262 | if (!prop) |
| 263 | goto fail1; |
| 264 | cf->gpio_3v = *prop; |
| 265 | |
| 266 | prop = of_get_property(np, "card-5v-gpio", NULL); |
| 267 | if (!prop) |
| 268 | goto fail1; |
| 269 | cf->gpio_5v = *prop; |
| 270 | |
| 271 | cf->socket.io_offset = cf->io_base; |
| 272 | |
| 273 | /* reserve chip-select regions */ |
| 274 | if (!request_mem_region(cf->mem_phys, cf->mem_size, driver_name)) { |
| 275 | status = -ENXIO; |
| 276 | dev_err(device, "Can't claim memory region\n"); |
| 277 | goto fail1; |
| 278 | } |
| 279 | |
| 280 | if (!request_region(cf->io_base, cf->io_size, driver_name)) { |
| 281 | status = -ENXIO; |
| 282 | dev_err(device, "Can't claim I/O region\n"); |
| 283 | goto fail2; |
| 284 | } |
| 285 | |
| 286 | cf->socket.owner = THIS_MODULE; |
| 287 | cf->socket.dev.parent = &ofdev->dev; |
| 288 | cf->socket.ops = &electra_cf_ops; |
| 289 | cf->socket.resource_ops = &pccard_static_ops; |
| 290 | cf->socket.features = SS_CAP_PCCARD | SS_CAP_STATIC_MAP | |
| 291 | SS_CAP_MEM_ALIGN; |
| 292 | cf->socket.map_size = 0x800; |
| 293 | |
| 294 | status = pcmcia_register_socket(&cf->socket); |
| 295 | if (status < 0) { |
| 296 | dev_err(device, "pcmcia_register_socket failed\n"); |
| 297 | goto fail3; |
| 298 | } |
| 299 | |
| 300 | dev_info(device, "at mem 0x%lx io 0x%lx irq %d\n", |
| 301 | cf->mem_phys, io.start, cf->irq); |
| 302 | |
| 303 | cf->active = 1; |
| 304 | electra_cf_timer((unsigned long)cf); |
| 305 | return 0; |
| 306 | |
| 307 | fail3: |
| 308 | release_region(cf->io_base, cf->io_size); |
| 309 | fail2: |
| 310 | release_mem_region(cf->mem_phys, cf->mem_size); |
| 311 | fail1: |
| 312 | if (cf->irq != NO_IRQ) |
| 313 | free_irq(cf->irq, cf); |
| 314 | |
| 315 | if (cf->io_virt) |
| 316 | __iounmap_at(cf->io_virt, cf->io_size); |
| 317 | if (cf->mem_base) |
| 318 | iounmap(cf->mem_base); |
| 319 | if (cf->gpio_base) |
| 320 | iounmap(cf->gpio_base); |
| 321 | device_init_wakeup(&ofdev->dev, 0); |
| 322 | kfree(cf); |
| 323 | return status; |
| 324 | |
| 325 | } |
| 326 | |
| 327 | static int __devexit electra_cf_remove(struct of_device *ofdev) |
| 328 | { |
| 329 | struct device *device = &ofdev->dev; |
| 330 | struct electra_cf_socket *cf; |
| 331 | |
| 332 | cf = dev_get_drvdata(device); |
| 333 | |
| 334 | cf->active = 0; |
| 335 | pcmcia_unregister_socket(&cf->socket); |
| 336 | free_irq(cf->irq, cf); |
| 337 | del_timer_sync(&cf->timer); |
| 338 | |
| 339 | __iounmap_at(cf->io_virt, cf->io_size); |
| 340 | iounmap(cf->mem_base); |
| 341 | iounmap(cf->gpio_base); |
| 342 | release_mem_region(cf->mem_phys, cf->mem_size); |
| 343 | release_region(cf->io_base, cf->io_size); |
| 344 | |
| 345 | kfree(cf); |
| 346 | |
| 347 | return 0; |
| 348 | } |
| 349 | |
| 350 | static struct of_device_id electra_cf_match[] = { |
| 351 | { |
| 352 | .compatible = "electra-cf", |
| 353 | }, |
| 354 | {}, |
| 355 | }; |
Olof Johansson | c433a1b | 2008-05-27 16:07:26 -0500 | [diff] [blame] | 356 | MODULE_DEVICE_TABLE(of, electra_cf_match); |
Olof Johansson | 2b571a0 | 2007-10-16 23:26:20 -0700 | [diff] [blame] | 357 | |
| 358 | static struct of_platform_driver electra_cf_driver = { |
| 359 | .name = (char *)driver_name, |
| 360 | .match_table = electra_cf_match, |
| 361 | .probe = electra_cf_probe, |
| 362 | .remove = electra_cf_remove, |
| 363 | }; |
| 364 | |
| 365 | static int __init electra_cf_init(void) |
| 366 | { |
| 367 | return of_register_platform_driver(&electra_cf_driver); |
| 368 | } |
| 369 | module_init(electra_cf_init); |
| 370 | |
| 371 | static void __exit electra_cf_exit(void) |
| 372 | { |
| 373 | of_unregister_platform_driver(&electra_cf_driver); |
| 374 | } |
| 375 | module_exit(electra_cf_exit); |
| 376 | |
| 377 | MODULE_LICENSE("GPL"); |
| 378 | MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>"); |
| 379 | MODULE_DESCRIPTION("PA Semi Electra CF driver"); |