blob: 29fdd174bc23295bddf77e6158c7a20985d6d725 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/pcmcia/sa1111_generic.c
4 *
5 * We implement the generic parts of a SA1111 PCMCIA driver. This
6 * basically means we handle everything except controlling the
7 * power. Power is machine specific...
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/ioport.h>
12#include <linux/device.h>
13#include <linux/interrupt.h>
14#include <linux/init.h>
Russell King99730222009-03-25 10:21:35 +000015#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <pcmcia/ss.h>
19
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/hardware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/hardware/sa1111.h>
Russell King3f8df892016-09-02 10:14:20 +010022#include <asm/mach-types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/irq.h>
24
25#include "sa1111_generic.h"
26
Russell Kingea8c00a2012-01-16 11:32:09 +000027/*
28 * These are offsets from the above base.
29 */
30#define PCCR 0x0000
31#define PCSSR 0x0004
32#define PCSR 0x0008
33
34#define PCSR_S0_READY (1<<0)
35#define PCSR_S1_READY (1<<1)
36#define PCSR_S0_DETECT (1<<2)
37#define PCSR_S1_DETECT (1<<3)
38#define PCSR_S0_VS1 (1<<4)
39#define PCSR_S0_VS2 (1<<5)
40#define PCSR_S1_VS1 (1<<6)
41#define PCSR_S1_VS2 (1<<7)
42#define PCSR_S0_WP (1<<8)
43#define PCSR_S1_WP (1<<9)
44#define PCSR_S0_BVD1 (1<<10)
45#define PCSR_S0_BVD2 (1<<11)
46#define PCSR_S1_BVD1 (1<<12)
47#define PCSR_S1_BVD2 (1<<13)
48
49#define PCCR_S0_RST (1<<0)
50#define PCCR_S1_RST (1<<1)
51#define PCCR_S0_FLT (1<<2)
52#define PCCR_S1_FLT (1<<3)
53#define PCCR_S0_PWAITEN (1<<4)
54#define PCCR_S1_PWAITEN (1<<5)
55#define PCCR_S0_PSE (1<<6)
56#define PCCR_S1_PSE (1<<7)
57
58#define PCSSR_S0_SLEEP (1<<0)
59#define PCSSR_S1_SLEEP (1<<1)
60
Eric Miao08fa1592009-12-26 12:32:38 +080061#define IDX_IRQ_S0_READY_NINT (0)
62#define IDX_IRQ_S0_CD_VALID (1)
63#define IDX_IRQ_S0_BVD1_STSCHG (2)
64#define IDX_IRQ_S1_READY_NINT (3)
65#define IDX_IRQ_S1_CD_VALID (4)
66#define IDX_IRQ_S1_BVD1_STSCHG (5)
Russell King7170a3122016-09-07 13:45:11 +010067#define NUM_IRQS (6)
Eric Miao08fa1592009-12-26 12:32:38 +080068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069void sa1111_pcmcia_socket_state(struct soc_pcmcia_socket *skt, struct pcmcia_state *state)
70{
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +010071 struct sa1111_pcmcia_socket *s = to_skt(skt);
Russell Kingde854b32016-09-07 15:19:52 +010072 u32 status = readl_relaxed(s->dev->mapbase + PCSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 switch (skt->nr) {
75 case 0:
76 state->detect = status & PCSR_S0_DETECT ? 0 : 1;
77 state->ready = status & PCSR_S0_READY ? 1 : 0;
78 state->bvd1 = status & PCSR_S0_BVD1 ? 1 : 0;
79 state->bvd2 = status & PCSR_S0_BVD2 ? 1 : 0;
80 state->wrprot = status & PCSR_S0_WP ? 1 : 0;
81 state->vs_3v = status & PCSR_S0_VS1 ? 0 : 1;
82 state->vs_Xv = status & PCSR_S0_VS2 ? 0 : 1;
83 break;
84
85 case 1:
86 state->detect = status & PCSR_S1_DETECT ? 0 : 1;
87 state->ready = status & PCSR_S1_READY ? 1 : 0;
88 state->bvd1 = status & PCSR_S1_BVD1 ? 1 : 0;
89 state->bvd2 = status & PCSR_S1_BVD2 ? 1 : 0;
90 state->wrprot = status & PCSR_S1_WP ? 1 : 0;
91 state->vs_3v = status & PCSR_S1_VS1 ? 0 : 1;
92 state->vs_Xv = status & PCSR_S1_VS2 ? 0 : 1;
93 break;
94 }
95}
96
97int sa1111_pcmcia_configure_socket(struct soc_pcmcia_socket *skt, const socket_state_t *state)
98{
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +010099 struct sa1111_pcmcia_socket *s = to_skt(skt);
Russell Kingde854b32016-09-07 15:19:52 +0100100 u32 pccr_skt_mask, pccr_set_mask, val;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 unsigned long flags;
102
103 switch (skt->nr) {
104 case 0:
105 pccr_skt_mask = PCCR_S0_RST|PCCR_S0_FLT|PCCR_S0_PWAITEN|PCCR_S0_PSE;
106 break;
107
108 case 1:
109 pccr_skt_mask = PCCR_S1_RST|PCCR_S1_FLT|PCCR_S1_PWAITEN|PCCR_S1_PSE;
110 break;
111
112 default:
113 return -1;
114 }
115
116 pccr_set_mask = 0;
117
118 if (state->Vcc != 0)
119 pccr_set_mask |= PCCR_S0_PWAITEN|PCCR_S1_PWAITEN;
120 if (state->Vcc == 50)
121 pccr_set_mask |= PCCR_S0_PSE|PCCR_S1_PSE;
122 if (state->flags & SS_RESET)
123 pccr_set_mask |= PCCR_S0_RST|PCCR_S1_RST;
124 if (state->flags & SS_OUTPUT_ENA)
125 pccr_set_mask |= PCCR_S0_FLT|PCCR_S1_FLT;
126
127 local_irq_save(flags);
Russell Kingde854b32016-09-07 15:19:52 +0100128 val = readl_relaxed(s->dev->mapbase + PCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 val &= ~pccr_skt_mask;
130 val |= pccr_set_mask & pccr_skt_mask;
Russell Kingde854b32016-09-07 15:19:52 +0100131 writel_relaxed(val, s->dev->mapbase + PCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 local_irq_restore(flags);
133
134 return 0;
135}
136
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100137int sa1111_pcmcia_add(struct sa1111_dev *dev, struct pcmcia_low_level *ops,
138 int (*add)(struct soc_pcmcia_socket *))
139{
140 struct sa1111_pcmcia_socket *s;
Russell King321ae962015-03-26 10:46:35 +0000141 struct clk *clk;
Russell King7170a3122016-09-07 13:45:11 +0100142 int i, ret = 0, irqs[NUM_IRQS];
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100143
Russell King321ae962015-03-26 10:46:35 +0000144 clk = devm_clk_get(&dev->dev, NULL);
145 if (IS_ERR(clk))
146 return PTR_ERR(clk);
147
Russell King7170a3122016-09-07 13:45:11 +0100148 for (i = 0; i < NUM_IRQS; i++) {
149 irqs[i] = sa1111_get_irq(dev, i);
150 if (irqs[i] <= 0)
151 return irqs[i] ? : -ENXIO;
152 }
153
Russell King - ARM Linuxdabd1462009-03-29 22:35:11 +0100154 ops->socket_state = sa1111_pcmcia_socket_state;
Russell King - ARM Linuxdabd1462009-03-29 22:35:11 +0100155
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100156 for (i = 0; i < ops->nr; i++) {
157 s = kzalloc(sizeof(*s), GFP_KERNEL);
158 if (!s)
159 return -ENOMEM;
160
161 s->soc.nr = ops->first + i;
Russell King321ae962015-03-26 10:46:35 +0000162 s->soc.clk = clk;
163
Russell Kinge0d21172011-12-19 14:07:31 +0000164 soc_pcmcia_init_one(&s->soc, ops, &dev->dev);
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100165 s->dev = dev;
Russell King81f33c62011-12-19 23:04:22 +0000166 if (s->soc.nr) {
Russell King7170a3122016-09-07 13:45:11 +0100167 s->soc.socket.pci_irq = irqs[IDX_IRQ_S1_READY_NINT];
168 s->soc.stat[SOC_STAT_CD].irq = irqs[IDX_IRQ_S1_CD_VALID];
Russell King81f33c62011-12-19 23:04:22 +0000169 s->soc.stat[SOC_STAT_CD].name = "SA1111 CF card detect";
Russell King7170a3122016-09-07 13:45:11 +0100170 s->soc.stat[SOC_STAT_BVD1].irq = irqs[IDX_IRQ_S1_BVD1_STSCHG];
Russell King81f33c62011-12-19 23:04:22 +0000171 s->soc.stat[SOC_STAT_BVD1].name = "SA1111 CF BVD1";
172 } else {
Russell King7170a3122016-09-07 13:45:11 +0100173 s->soc.socket.pci_irq = irqs[IDX_IRQ_S0_READY_NINT];
174 s->soc.stat[SOC_STAT_CD].irq = irqs[IDX_IRQ_S0_CD_VALID];
Russell King81f33c62011-12-19 23:04:22 +0000175 s->soc.stat[SOC_STAT_CD].name = "SA1111 PCMCIA card detect";
Russell King7170a3122016-09-07 13:45:11 +0100176 s->soc.stat[SOC_STAT_BVD1].irq = irqs[IDX_IRQ_S0_BVD1_STSCHG];
Russell King81f33c62011-12-19 23:04:22 +0000177 s->soc.stat[SOC_STAT_BVD1].name = "SA1111 PCMCIA BVD1";
178 }
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100179
180 ret = add(&s->soc);
181 if (ret == 0) {
182 s->next = dev_get_drvdata(&dev->dev);
183 dev_set_drvdata(&dev->dev, s);
184 } else
185 kfree(s);
186 }
187
188 return ret;
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int pcmcia_probe(struct sa1111_dev *dev)
192{
Russell Kingf339ab32005-10-28 14:29:43 +0100193 void __iomem *base;
Russell Kingae99ddb2012-01-26 13:25:47 +0000194 int ret;
195
196 ret = sa1111_enable_device(dev);
197 if (ret)
198 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100200 dev_set_drvdata(&dev->dev, NULL);
201
Russell Kingae99ddb2012-01-26 13:25:47 +0000202 if (!request_mem_region(dev->res.start, 512, SA1111_DRIVER_NAME(dev))) {
203 sa1111_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EBUSY;
Russell Kingae99ddb2012-01-26 13:25:47 +0000205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 base = dev->mapbase;
208
209 /*
210 * Initialise the suspend state.
211 */
Russell Kingde854b32016-09-07 15:19:52 +0100212 writel_relaxed(PCSSR_S0_SLEEP | PCSSR_S1_SLEEP, base + PCSSR);
213 writel_relaxed(PCCR_S0_FLT | PCCR_S1_FLT, base + PCCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Russell King3f8df892016-09-02 10:14:20 +0100215 ret = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#ifdef CONFIG_SA1100_BADGE4
Russell King3f8df892016-09-02 10:14:20 +0100217 if (machine_is_badge4())
218 ret = pcmcia_badge4_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219#endif
220#ifdef CONFIG_SA1100_JORNADA720
Russell King3f8df892016-09-02 10:14:20 +0100221 if (machine_is_jornada720())
222 ret = pcmcia_jornada720_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#endif
224#ifdef CONFIG_ARCH_LUBBOCK
Russell King3f8df892016-09-02 10:14:20 +0100225 if (machine_is_lubbock())
226 ret = pcmcia_lubbock_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif
228#ifdef CONFIG_ASSABET_NEPONSET
Russell King3f8df892016-09-02 10:14:20 +0100229 if (machine_is_assabet())
230 ret = pcmcia_neponset_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#endif
Russell King3f8df892016-09-02 10:14:20 +0100232
233 if (ret) {
234 release_mem_region(dev->res.start, 512);
235 sa1111_disable_device(dev);
236 }
237
238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Uwe Kleine-König074a6bd2021-01-14 17:31:09 +0100241static void pcmcia_remove(struct sa1111_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100243 struct sa1111_pcmcia_socket *next, *s = dev_get_drvdata(&dev->dev);
Russell King - ARM Linuxbe854582009-03-26 22:21:18 +0000244
245 dev_set_drvdata(&dev->dev, NULL);
246
Russell King171cf942012-01-24 21:33:26 +0000247 for (; s; s = next) {
248 next = s->next;
Russell King - ARM Linux701a5dc2009-03-29 19:42:44 +0100249 soc_pcmcia_remove_one(&s->soc);
250 kfree(s);
251 }
Russell King - ARM Linuxbe854582009-03-26 22:21:18 +0000252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 release_mem_region(dev->res.start, 512);
Russell Kingae99ddb2012-01-26 13:25:47 +0000254 sa1111_disable_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257static struct sa1111_driver pcmcia_driver = {
258 .drv = {
259 .name = "sa1111-pcmcia",
260 },
261 .devid = SA1111_DEVID_PCMCIA,
262 .probe = pcmcia_probe,
Bill Pemberton96364e32012-11-19 13:20:38 -0500263 .remove = pcmcia_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265
266static int __init sa1111_drv_pcmcia_init(void)
267{
268 return sa1111_driver_register(&pcmcia_driver);
269}
270
271static void __exit sa1111_drv_pcmcia_exit(void)
272{
273 sa1111_driver_unregister(&pcmcia_driver);
274}
275
Richard Purdief36598ae2005-09-03 19:39:25 +0100276fs_initcall(sa1111_drv_pcmcia_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277module_exit(sa1111_drv_pcmcia_exit);
278
279MODULE_DESCRIPTION("SA1111 PCMCIA card socket driver");
280MODULE_LICENSE("GPL");