Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/pcmcia/sa1111_generic.c |
| 3 | * |
| 4 | * We implement the generic parts of a SA1111 PCMCIA driver. This |
| 5 | * basically means we handle everything except controlling the |
| 6 | * power. Power is machine specific... |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/ioport.h> |
| 11 | #include <linux/device.h> |
| 12 | #include <linux/interrupt.h> |
| 13 | #include <linux/init.h> |
Russell King | 9973022 | 2009-03-25 10:21:35 +0000 | [diff] [blame] | 14 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include <pcmcia/ss.h> |
| 18 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 19 | #include <mach/hardware.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <asm/hardware/sa1111.h> |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 21 | #include <asm/mach-types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/irq.h> |
| 23 | |
| 24 | #include "sa1111_generic.h" |
| 25 | |
Russell King | ea8c00a | 2012-01-16 11:32:09 +0000 | [diff] [blame] | 26 | /* |
| 27 | * These are offsets from the above base. |
| 28 | */ |
| 29 | #define PCCR 0x0000 |
| 30 | #define PCSSR 0x0004 |
| 31 | #define PCSR 0x0008 |
| 32 | |
| 33 | #define PCSR_S0_READY (1<<0) |
| 34 | #define PCSR_S1_READY (1<<1) |
| 35 | #define PCSR_S0_DETECT (1<<2) |
| 36 | #define PCSR_S1_DETECT (1<<3) |
| 37 | #define PCSR_S0_VS1 (1<<4) |
| 38 | #define PCSR_S0_VS2 (1<<5) |
| 39 | #define PCSR_S1_VS1 (1<<6) |
| 40 | #define PCSR_S1_VS2 (1<<7) |
| 41 | #define PCSR_S0_WP (1<<8) |
| 42 | #define PCSR_S1_WP (1<<9) |
| 43 | #define PCSR_S0_BVD1 (1<<10) |
| 44 | #define PCSR_S0_BVD2 (1<<11) |
| 45 | #define PCSR_S1_BVD1 (1<<12) |
| 46 | #define PCSR_S1_BVD2 (1<<13) |
| 47 | |
| 48 | #define PCCR_S0_RST (1<<0) |
| 49 | #define PCCR_S1_RST (1<<1) |
| 50 | #define PCCR_S0_FLT (1<<2) |
| 51 | #define PCCR_S1_FLT (1<<3) |
| 52 | #define PCCR_S0_PWAITEN (1<<4) |
| 53 | #define PCCR_S1_PWAITEN (1<<5) |
| 54 | #define PCCR_S0_PSE (1<<6) |
| 55 | #define PCCR_S1_PSE (1<<7) |
| 56 | |
| 57 | #define PCSSR_S0_SLEEP (1<<0) |
| 58 | #define PCSSR_S1_SLEEP (1<<1) |
| 59 | |
Eric Miao | 08fa159 | 2009-12-26 12:32:38 +0800 | [diff] [blame] | 60 | #define IDX_IRQ_S0_READY_NINT (0) |
| 61 | #define IDX_IRQ_S0_CD_VALID (1) |
| 62 | #define IDX_IRQ_S0_BVD1_STSCHG (2) |
| 63 | #define IDX_IRQ_S1_READY_NINT (3) |
| 64 | #define IDX_IRQ_S1_CD_VALID (4) |
| 65 | #define IDX_IRQ_S1_BVD1_STSCHG (5) |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 66 | #define NUM_IRQS (6) |
Eric Miao | 08fa159 | 2009-12-26 12:32:38 +0800 | [diff] [blame] | 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | void sa1111_pcmcia_socket_state(struct soc_pcmcia_socket *skt, struct pcmcia_state *state) |
| 69 | { |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 70 | struct sa1111_pcmcia_socket *s = to_skt(skt); |
Russell King | ea8c00a | 2012-01-16 11:32:09 +0000 | [diff] [blame] | 71 | unsigned long status = sa1111_readl(s->dev->mapbase + PCSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | switch (skt->nr) { |
| 74 | case 0: |
| 75 | state->detect = status & PCSR_S0_DETECT ? 0 : 1; |
| 76 | state->ready = status & PCSR_S0_READY ? 1 : 0; |
| 77 | state->bvd1 = status & PCSR_S0_BVD1 ? 1 : 0; |
| 78 | state->bvd2 = status & PCSR_S0_BVD2 ? 1 : 0; |
| 79 | state->wrprot = status & PCSR_S0_WP ? 1 : 0; |
| 80 | state->vs_3v = status & PCSR_S0_VS1 ? 0 : 1; |
| 81 | state->vs_Xv = status & PCSR_S0_VS2 ? 0 : 1; |
| 82 | break; |
| 83 | |
| 84 | case 1: |
| 85 | state->detect = status & PCSR_S1_DETECT ? 0 : 1; |
| 86 | state->ready = status & PCSR_S1_READY ? 1 : 0; |
| 87 | state->bvd1 = status & PCSR_S1_BVD1 ? 1 : 0; |
| 88 | state->bvd2 = status & PCSR_S1_BVD2 ? 1 : 0; |
| 89 | state->wrprot = status & PCSR_S1_WP ? 1 : 0; |
| 90 | state->vs_3v = status & PCSR_S1_VS1 ? 0 : 1; |
| 91 | state->vs_Xv = status & PCSR_S1_VS2 ? 0 : 1; |
| 92 | break; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int sa1111_pcmcia_configure_socket(struct soc_pcmcia_socket *skt, const socket_state_t *state) |
| 97 | { |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 98 | struct sa1111_pcmcia_socket *s = to_skt(skt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | unsigned int pccr_skt_mask, pccr_set_mask, val; |
| 100 | unsigned long flags; |
| 101 | |
| 102 | switch (skt->nr) { |
| 103 | case 0: |
| 104 | pccr_skt_mask = PCCR_S0_RST|PCCR_S0_FLT|PCCR_S0_PWAITEN|PCCR_S0_PSE; |
| 105 | break; |
| 106 | |
| 107 | case 1: |
| 108 | pccr_skt_mask = PCCR_S1_RST|PCCR_S1_FLT|PCCR_S1_PWAITEN|PCCR_S1_PSE; |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | return -1; |
| 113 | } |
| 114 | |
| 115 | pccr_set_mask = 0; |
| 116 | |
| 117 | if (state->Vcc != 0) |
| 118 | pccr_set_mask |= PCCR_S0_PWAITEN|PCCR_S1_PWAITEN; |
| 119 | if (state->Vcc == 50) |
| 120 | pccr_set_mask |= PCCR_S0_PSE|PCCR_S1_PSE; |
| 121 | if (state->flags & SS_RESET) |
| 122 | pccr_set_mask |= PCCR_S0_RST|PCCR_S1_RST; |
| 123 | if (state->flags & SS_OUTPUT_ENA) |
| 124 | pccr_set_mask |= PCCR_S0_FLT|PCCR_S1_FLT; |
| 125 | |
| 126 | local_irq_save(flags); |
Russell King | ea8c00a | 2012-01-16 11:32:09 +0000 | [diff] [blame] | 127 | val = sa1111_readl(s->dev->mapbase + PCCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | val &= ~pccr_skt_mask; |
| 129 | val |= pccr_set_mask & pccr_skt_mask; |
Russell King | ea8c00a | 2012-01-16 11:32:09 +0000 | [diff] [blame] | 130 | sa1111_writel(val, s->dev->mapbase + PCCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | local_irq_restore(flags); |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 136 | int sa1111_pcmcia_add(struct sa1111_dev *dev, struct pcmcia_low_level *ops, |
| 137 | int (*add)(struct soc_pcmcia_socket *)) |
| 138 | { |
| 139 | struct sa1111_pcmcia_socket *s; |
Russell King | 321ae96 | 2015-03-26 10:46:35 +0000 | [diff] [blame] | 140 | struct clk *clk; |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 141 | int i, ret = 0, irqs[NUM_IRQS]; |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 142 | |
Russell King | 321ae96 | 2015-03-26 10:46:35 +0000 | [diff] [blame] | 143 | clk = devm_clk_get(&dev->dev, NULL); |
| 144 | if (IS_ERR(clk)) |
| 145 | return PTR_ERR(clk); |
| 146 | |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 147 | for (i = 0; i < NUM_IRQS; i++) { |
| 148 | irqs[i] = sa1111_get_irq(dev, i); |
| 149 | if (irqs[i] <= 0) |
| 150 | return irqs[i] ? : -ENXIO; |
| 151 | } |
| 152 | |
Russell King - ARM Linux | dabd146 | 2009-03-29 22:35:11 +0100 | [diff] [blame] | 153 | ops->socket_state = sa1111_pcmcia_socket_state; |
Russell King - ARM Linux | dabd146 | 2009-03-29 22:35:11 +0100 | [diff] [blame] | 154 | |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 155 | for (i = 0; i < ops->nr; i++) { |
| 156 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
| 157 | if (!s) |
| 158 | return -ENOMEM; |
| 159 | |
| 160 | s->soc.nr = ops->first + i; |
Russell King | 321ae96 | 2015-03-26 10:46:35 +0000 | [diff] [blame] | 161 | s->soc.clk = clk; |
| 162 | |
Russell King | e0d2117 | 2011-12-19 14:07:31 +0000 | [diff] [blame] | 163 | soc_pcmcia_init_one(&s->soc, ops, &dev->dev); |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 164 | s->dev = dev; |
Russell King | 81f33c6 | 2011-12-19 23:04:22 +0000 | [diff] [blame] | 165 | if (s->soc.nr) { |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 166 | s->soc.socket.pci_irq = irqs[IDX_IRQ_S1_READY_NINT]; |
| 167 | s->soc.stat[SOC_STAT_CD].irq = irqs[IDX_IRQ_S1_CD_VALID]; |
Russell King | 81f33c6 | 2011-12-19 23:04:22 +0000 | [diff] [blame] | 168 | s->soc.stat[SOC_STAT_CD].name = "SA1111 CF card detect"; |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 169 | s->soc.stat[SOC_STAT_BVD1].irq = irqs[IDX_IRQ_S1_BVD1_STSCHG]; |
Russell King | 81f33c6 | 2011-12-19 23:04:22 +0000 | [diff] [blame] | 170 | s->soc.stat[SOC_STAT_BVD1].name = "SA1111 CF BVD1"; |
| 171 | } else { |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 172 | s->soc.socket.pci_irq = irqs[IDX_IRQ_S0_READY_NINT]; |
| 173 | s->soc.stat[SOC_STAT_CD].irq = irqs[IDX_IRQ_S0_CD_VALID]; |
Russell King | 81f33c6 | 2011-12-19 23:04:22 +0000 | [diff] [blame] | 174 | s->soc.stat[SOC_STAT_CD].name = "SA1111 PCMCIA card detect"; |
Russell King | 7170a312 | 2016-09-07 13:45:11 +0100 | [diff] [blame^] | 175 | s->soc.stat[SOC_STAT_BVD1].irq = irqs[IDX_IRQ_S0_BVD1_STSCHG]; |
Russell King | 81f33c6 | 2011-12-19 23:04:22 +0000 | [diff] [blame] | 176 | s->soc.stat[SOC_STAT_BVD1].name = "SA1111 PCMCIA BVD1"; |
| 177 | } |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 178 | |
| 179 | ret = add(&s->soc); |
| 180 | if (ret == 0) { |
| 181 | s->next = dev_get_drvdata(&dev->dev); |
| 182 | dev_set_drvdata(&dev->dev, s); |
| 183 | } else |
| 184 | kfree(s); |
| 185 | } |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | static int pcmcia_probe(struct sa1111_dev *dev) |
| 191 | { |
Russell King | f339ab3 | 2005-10-28 14:29:43 +0100 | [diff] [blame] | 192 | void __iomem *base; |
Russell King | ae99ddb | 2012-01-26 13:25:47 +0000 | [diff] [blame] | 193 | int ret; |
| 194 | |
| 195 | ret = sa1111_enable_device(dev); |
| 196 | if (ret) |
| 197 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 199 | dev_set_drvdata(&dev->dev, NULL); |
| 200 | |
Russell King | ae99ddb | 2012-01-26 13:25:47 +0000 | [diff] [blame] | 201 | if (!request_mem_region(dev->res.start, 512, SA1111_DRIVER_NAME(dev))) { |
| 202 | sa1111_disable_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | return -EBUSY; |
Russell King | ae99ddb | 2012-01-26 13:25:47 +0000 | [diff] [blame] | 204 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | base = dev->mapbase; |
| 207 | |
| 208 | /* |
| 209 | * Initialise the suspend state. |
| 210 | */ |
Russell King | ea8c00a | 2012-01-16 11:32:09 +0000 | [diff] [blame] | 211 | sa1111_writel(PCSSR_S0_SLEEP | PCSSR_S1_SLEEP, base + PCSSR); |
| 212 | sa1111_writel(PCCR_S0_FLT | PCCR_S1_FLT, base + PCCR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 214 | ret = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | #ifdef CONFIG_SA1100_BADGE4 |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 216 | if (machine_is_badge4()) |
| 217 | ret = pcmcia_badge4_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | #endif |
| 219 | #ifdef CONFIG_SA1100_JORNADA720 |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 220 | if (machine_is_jornada720()) |
| 221 | ret = pcmcia_jornada720_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #endif |
| 223 | #ifdef CONFIG_ARCH_LUBBOCK |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 224 | if (machine_is_lubbock()) |
| 225 | ret = pcmcia_lubbock_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | #endif |
| 227 | #ifdef CONFIG_ASSABET_NEPONSET |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 228 | if (machine_is_assabet()) |
| 229 | ret = pcmcia_neponset_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | #endif |
Russell King | 3f8df89 | 2016-09-02 10:14:20 +0100 | [diff] [blame] | 231 | |
| 232 | if (ret) { |
| 233 | release_mem_region(dev->res.start, 512); |
| 234 | sa1111_disable_device(dev); |
| 235 | } |
| 236 | |
| 237 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Bill Pemberton | e765a02 | 2012-11-19 13:26:05 -0500 | [diff] [blame] | 240 | static int pcmcia_remove(struct sa1111_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 242 | struct sa1111_pcmcia_socket *next, *s = dev_get_drvdata(&dev->dev); |
Russell King - ARM Linux | be85458 | 2009-03-26 22:21:18 +0000 | [diff] [blame] | 243 | |
| 244 | dev_set_drvdata(&dev->dev, NULL); |
| 245 | |
Russell King | 171cf94 | 2012-01-24 21:33:26 +0000 | [diff] [blame] | 246 | for (; s; s = next) { |
| 247 | next = s->next; |
Russell King - ARM Linux | 701a5dc | 2009-03-29 19:42:44 +0100 | [diff] [blame] | 248 | soc_pcmcia_remove_one(&s->soc); |
| 249 | kfree(s); |
| 250 | } |
Russell King - ARM Linux | be85458 | 2009-03-26 22:21:18 +0000 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | release_mem_region(dev->res.start, 512); |
Russell King | ae99ddb | 2012-01-26 13:25:47 +0000 | [diff] [blame] | 253 | sa1111_disable_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | static struct sa1111_driver pcmcia_driver = { |
| 258 | .drv = { |
| 259 | .name = "sa1111-pcmcia", |
| 260 | }, |
| 261 | .devid = SA1111_DEVID_PCMCIA, |
| 262 | .probe = pcmcia_probe, |
Bill Pemberton | 96364e3 | 2012-11-19 13:20:38 -0500 | [diff] [blame] | 263 | .remove = pcmcia_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | static int __init sa1111_drv_pcmcia_init(void) |
| 267 | { |
| 268 | return sa1111_driver_register(&pcmcia_driver); |
| 269 | } |
| 270 | |
| 271 | static void __exit sa1111_drv_pcmcia_exit(void) |
| 272 | { |
| 273 | sa1111_driver_unregister(&pcmcia_driver); |
| 274 | } |
| 275 | |
Richard Purdie | f36598ae | 2005-09-03 19:39:25 +0100 | [diff] [blame] | 276 | fs_initcall(sa1111_drv_pcmcia_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | module_exit(sa1111_drv_pcmcia_exit); |
| 278 | |
| 279 | MODULE_DESCRIPTION("SA1111 PCMCIA card socket driver"); |
| 280 | MODULE_LICENSE("GPL"); |