Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 1 | /* |
| 2 | * PCMCIA socket code for the MyCable XXS1500 system. |
| 3 | * |
| 4 | * Copyright (c) 2009 Manuel Lauss <manuel.lauss@gmail.com> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/delay.h> |
| 9 | #include <linux/gpio.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/ioport.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/pm.h> |
| 16 | #include <linux/resource.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 17 | #include <linux/slab.h> |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 18 | #include <linux/spinlock.h> |
| 19 | |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 20 | #include <pcmcia/ss.h> |
| 21 | #include <pcmcia/cistpl.h> |
| 22 | |
| 23 | #include <asm/irq.h> |
| 24 | #include <asm/system.h> |
| 25 | #include <asm/mach-au1x00/au1000.h> |
| 26 | |
| 27 | #define MEM_MAP_SIZE 0x400000 |
| 28 | #define IO_MAP_SIZE 0x1000 |
| 29 | |
| 30 | |
| 31 | /* |
| 32 | * 3.3V cards only; all interfacing is done via gpios: |
| 33 | * |
| 34 | * 0/1: carddetect (00 = card present, xx = huh) |
| 35 | * 4: card irq |
| 36 | * 204: reset (high-act) |
| 37 | * 205: buffer enable (low-act) |
| 38 | * 208/209: card voltage key (00,01,10,11) |
| 39 | * 210: battwarn |
| 40 | * 211: batdead |
| 41 | * 214: power (low-act) |
| 42 | */ |
| 43 | #define GPIO_CDA 0 |
| 44 | #define GPIO_CDB 1 |
| 45 | #define GPIO_CARDIRQ 4 |
| 46 | #define GPIO_RESET 204 |
| 47 | #define GPIO_OUTEN 205 |
| 48 | #define GPIO_VSL 208 |
| 49 | #define GPIO_VSH 209 |
| 50 | #define GPIO_BATTDEAD 210 |
| 51 | #define GPIO_BATTWARN 211 |
| 52 | #define GPIO_POWER 214 |
| 53 | |
| 54 | struct xxs1500_pcmcia_sock { |
| 55 | struct pcmcia_socket socket; |
| 56 | void *virt_io; |
| 57 | |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 58 | phys_addr_t phys_io; |
| 59 | phys_addr_t phys_attr; |
| 60 | phys_addr_t phys_mem; |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 61 | |
| 62 | /* previous flags for set_socket() */ |
| 63 | unsigned int old_flags; |
| 64 | }; |
| 65 | |
| 66 | #define to_xxs_socket(x) container_of(x, struct xxs1500_pcmcia_sock, socket) |
| 67 | |
| 68 | static irqreturn_t cdirq(int irq, void *data) |
| 69 | { |
| 70 | struct xxs1500_pcmcia_sock *sock = data; |
| 71 | |
| 72 | pcmcia_parse_events(&sock->socket, SS_DETECT); |
| 73 | |
| 74 | return IRQ_HANDLED; |
| 75 | } |
| 76 | |
| 77 | static int xxs1500_pcmcia_configure(struct pcmcia_socket *skt, |
| 78 | struct socket_state_t *state) |
| 79 | { |
| 80 | struct xxs1500_pcmcia_sock *sock = to_xxs_socket(skt); |
| 81 | unsigned int changed; |
| 82 | |
| 83 | /* power control */ |
| 84 | switch (state->Vcc) { |
| 85 | case 0: |
| 86 | gpio_set_value(GPIO_POWER, 1); /* power off */ |
| 87 | break; |
| 88 | case 33: |
| 89 | gpio_set_value(GPIO_POWER, 0); /* power on */ |
| 90 | break; |
| 91 | case 50: |
| 92 | default: |
| 93 | return -EINVAL; |
| 94 | } |
| 95 | |
| 96 | changed = state->flags ^ sock->old_flags; |
| 97 | |
| 98 | if (changed & SS_RESET) { |
| 99 | if (state->flags & SS_RESET) { |
| 100 | gpio_set_value(GPIO_RESET, 1); /* assert reset */ |
| 101 | gpio_set_value(GPIO_OUTEN, 1); /* buffers off */ |
| 102 | } else { |
| 103 | gpio_set_value(GPIO_RESET, 0); /* deassert reset */ |
| 104 | gpio_set_value(GPIO_OUTEN, 0); /* buffers on */ |
| 105 | msleep(500); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | sock->old_flags = state->flags; |
| 110 | |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | static int xxs1500_pcmcia_get_status(struct pcmcia_socket *skt, |
| 115 | unsigned int *value) |
| 116 | { |
| 117 | unsigned int status; |
| 118 | int i; |
| 119 | |
| 120 | status = 0; |
| 121 | |
| 122 | /* check carddetects: GPIO[0:1] must both be low */ |
| 123 | if (!gpio_get_value(GPIO_CDA) && !gpio_get_value(GPIO_CDB)) |
| 124 | status |= SS_DETECT; |
| 125 | |
| 126 | /* determine card voltage: GPIO[208:209] binary value */ |
| 127 | i = (!!gpio_get_value(GPIO_VSL)) | ((!!gpio_get_value(GPIO_VSH)) << 1); |
| 128 | |
| 129 | switch (i) { |
| 130 | case 0: |
| 131 | case 1: |
| 132 | case 2: |
| 133 | status |= SS_3VCARD; /* 3V card */ |
| 134 | break; |
| 135 | case 3: /* 5V card, unsupported */ |
| 136 | default: |
| 137 | status |= SS_XVCARD; /* treated as unsupported in core */ |
| 138 | } |
| 139 | |
| 140 | /* GPIO214: low active power switch */ |
| 141 | status |= gpio_get_value(GPIO_POWER) ? 0 : SS_POWERON; |
| 142 | |
| 143 | /* GPIO204: high-active reset line */ |
| 144 | status |= gpio_get_value(GPIO_RESET) ? SS_RESET : SS_READY; |
| 145 | |
| 146 | /* other stuff */ |
| 147 | status |= gpio_get_value(GPIO_BATTDEAD) ? 0 : SS_BATDEAD; |
| 148 | status |= gpio_get_value(GPIO_BATTWARN) ? 0 : SS_BATWARN; |
| 149 | |
| 150 | *value = status; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | static int xxs1500_pcmcia_sock_init(struct pcmcia_socket *skt) |
| 156 | { |
| 157 | gpio_direction_input(GPIO_CDA); |
| 158 | gpio_direction_input(GPIO_CDB); |
| 159 | gpio_direction_input(GPIO_VSL); |
| 160 | gpio_direction_input(GPIO_VSH); |
| 161 | gpio_direction_input(GPIO_BATTDEAD); |
| 162 | gpio_direction_input(GPIO_BATTWARN); |
| 163 | gpio_direction_output(GPIO_RESET, 1); /* assert reset */ |
| 164 | gpio_direction_output(GPIO_OUTEN, 1); /* disable buffers */ |
| 165 | gpio_direction_output(GPIO_POWER, 1); /* power off */ |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int xxs1500_pcmcia_sock_suspend(struct pcmcia_socket *skt) |
| 171 | { |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int au1x00_pcmcia_set_io_map(struct pcmcia_socket *skt, |
| 176 | struct pccard_io_map *map) |
| 177 | { |
| 178 | struct xxs1500_pcmcia_sock *sock = to_xxs_socket(skt); |
| 179 | |
| 180 | map->start = (u32)sock->virt_io; |
| 181 | map->stop = map->start + IO_MAP_SIZE; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int au1x00_pcmcia_set_mem_map(struct pcmcia_socket *skt, |
| 187 | struct pccard_mem_map *map) |
| 188 | { |
| 189 | struct xxs1500_pcmcia_sock *sock = to_xxs_socket(skt); |
| 190 | |
| 191 | if (map->flags & MAP_ATTRIB) |
| 192 | map->static_start = sock->phys_attr + map->card_start; |
| 193 | else |
| 194 | map->static_start = sock->phys_mem + map->card_start; |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static struct pccard_operations xxs1500_pcmcia_operations = { |
| 200 | .init = xxs1500_pcmcia_sock_init, |
| 201 | .suspend = xxs1500_pcmcia_sock_suspend, |
| 202 | .get_status = xxs1500_pcmcia_get_status, |
| 203 | .set_socket = xxs1500_pcmcia_configure, |
| 204 | .set_io_map = au1x00_pcmcia_set_io_map, |
| 205 | .set_mem_map = au1x00_pcmcia_set_mem_map, |
| 206 | }; |
| 207 | |
| 208 | static int __devinit xxs1500_pcmcia_probe(struct platform_device *pdev) |
| 209 | { |
| 210 | struct xxs1500_pcmcia_sock *sock; |
| 211 | struct resource *r; |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 212 | int ret, irq; |
| 213 | |
| 214 | sock = kzalloc(sizeof(struct xxs1500_pcmcia_sock), GFP_KERNEL); |
| 215 | if (!sock) |
| 216 | return -ENOMEM; |
| 217 | |
| 218 | ret = -ENODEV; |
| 219 | |
Manuel Lauss | f25e188 | 2010-03-01 21:40:56 +0100 | [diff] [blame] | 220 | /* 36bit PCMCIA Attribute area address */ |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 221 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-attr"); |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 222 | if (!r) { |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 223 | dev_err(&pdev->dev, "missing 'pcmcia-attr' resource!\n"); |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 224 | goto out0; |
| 225 | } |
| 226 | sock->phys_attr = r->start; |
| 227 | |
Manuel Lauss | f25e188 | 2010-03-01 21:40:56 +0100 | [diff] [blame] | 228 | /* 36bit PCMCIA Memory area address */ |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 229 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-mem"); |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 230 | if (!r) { |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 231 | dev_err(&pdev->dev, "missing 'pcmcia-mem' resource!\n"); |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 232 | goto out0; |
| 233 | } |
| 234 | sock->phys_mem = r->start; |
| 235 | |
Manuel Lauss | f25e188 | 2010-03-01 21:40:56 +0100 | [diff] [blame] | 236 | /* 36bit PCMCIA IO area address */ |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 237 | r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcmcia-io"); |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 238 | if (!r) { |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 239 | dev_err(&pdev->dev, "missing 'pcmcia-io' resource!\n"); |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 240 | goto out0; |
| 241 | } |
| 242 | sock->phys_io = r->start; |
| 243 | |
| 244 | |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 245 | /* |
| 246 | * PCMCIA client drivers use the inb/outb macros to access |
| 247 | * the IO registers. Since mips_io_port_base is added |
| 248 | * to the access address of the mips implementation of |
| 249 | * inb/outb, we need to subtract it here because we want |
| 250 | * to access the I/O or MEM address directly, without |
| 251 | * going through this "mips_io_port_base" mechanism. |
| 252 | */ |
Manuel Lauss | 11b897c | 2010-02-24 17:40:21 +0100 | [diff] [blame] | 253 | sock->virt_io = (void *)(ioremap(sock->phys_io, IO_MAP_SIZE) - |
Manuel Lauss | 0273b4e | 2009-10-04 14:55:29 +0200 | [diff] [blame] | 254 | mips_io_port_base); |
| 255 | |
| 256 | if (!sock->virt_io) { |
| 257 | dev_err(&pdev->dev, "cannot remap IO area\n"); |
| 258 | ret = -ENOMEM; |
| 259 | goto out0; |
| 260 | } |
| 261 | |
| 262 | sock->socket.ops = &xxs1500_pcmcia_operations; |
| 263 | sock->socket.owner = THIS_MODULE; |
| 264 | sock->socket.pci_irq = gpio_to_irq(GPIO_CARDIRQ); |
| 265 | sock->socket.features = SS_CAP_STATIC_MAP | SS_CAP_PCCARD; |
| 266 | sock->socket.map_size = MEM_MAP_SIZE; |
| 267 | sock->socket.io_offset = (unsigned long)sock->virt_io; |
| 268 | sock->socket.dev.parent = &pdev->dev; |
| 269 | sock->socket.resource_ops = &pccard_static_ops; |
| 270 | |
| 271 | platform_set_drvdata(pdev, sock); |
| 272 | |
| 273 | /* setup carddetect irq: use one of the 2 GPIOs as an |
| 274 | * edge detector. |
| 275 | */ |
| 276 | irq = gpio_to_irq(GPIO_CDA); |
| 277 | set_irq_type(irq, IRQ_TYPE_EDGE_BOTH); |
| 278 | ret = request_irq(irq, cdirq, 0, "pcmcia_carddetect", sock); |
| 279 | if (ret) { |
| 280 | dev_err(&pdev->dev, "cannot setup cd irq\n"); |
| 281 | goto out1; |
| 282 | } |
| 283 | |
| 284 | ret = pcmcia_register_socket(&sock->socket); |
| 285 | if (ret) { |
| 286 | dev_err(&pdev->dev, "failed to register\n"); |
| 287 | goto out2; |
| 288 | } |
| 289 | |
| 290 | printk(KERN_INFO "MyCable XXS1500 PCMCIA socket services\n"); |
| 291 | |
| 292 | return 0; |
| 293 | |
| 294 | out2: |
| 295 | free_irq(gpio_to_irq(GPIO_CDA), sock); |
| 296 | out1: |
| 297 | iounmap((void *)(sock->virt_io + (u32)mips_io_port_base)); |
| 298 | out0: |
| 299 | kfree(sock); |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | static int __devexit xxs1500_pcmcia_remove(struct platform_device *pdev) |
| 304 | { |
| 305 | struct xxs1500_pcmcia_sock *sock = platform_get_drvdata(pdev); |
| 306 | |
| 307 | pcmcia_unregister_socket(&sock->socket); |
| 308 | free_irq(gpio_to_irq(GPIO_CDA), sock); |
| 309 | iounmap((void *)(sock->virt_io + (u32)mips_io_port_base)); |
| 310 | kfree(sock); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static struct platform_driver xxs1500_pcmcia_socket_driver = { |
| 316 | .driver = { |
| 317 | .name = "xxs1500_pcmcia", |
| 318 | .owner = THIS_MODULE, |
| 319 | }, |
| 320 | .probe = xxs1500_pcmcia_probe, |
| 321 | .remove = __devexit_p(xxs1500_pcmcia_remove), |
| 322 | }; |
| 323 | |
| 324 | int __init xxs1500_pcmcia_socket_load(void) |
| 325 | { |
| 326 | return platform_driver_register(&xxs1500_pcmcia_socket_driver); |
| 327 | } |
| 328 | |
| 329 | void __exit xxs1500_pcmcia_socket_unload(void) |
| 330 | { |
| 331 | platform_driver_unregister(&xxs1500_pcmcia_socket_driver); |
| 332 | } |
| 333 | |
| 334 | module_init(xxs1500_pcmcia_socket_load); |
| 335 | module_exit(xxs1500_pcmcia_socket_unload); |
| 336 | |
| 337 | MODULE_LICENSE("GPL"); |
| 338 | MODULE_DESCRIPTION("PCMCIA Socket Services for MyCable XXS1500 systems"); |
| 339 | MODULE_AUTHOR("Manuel Lauss"); |