blob: ac9f11d19817d93e2fedcaf9cb26496a529df301 [file] [log] [blame]
David Brownellc6de2b62005-05-26 05:55:55 -07001/*
2 * PCMCIA driver for SL811HS (as found in REX-CFU1U)
3 * Filename: sl811_cs.c
4 * Author: Yukio Yamamoto
5 *
6 * Port to sl811-hcd and 2.6.x by
7 * Botond Botyanszki <boti@rocketmail.com>
8 * Simon Pickering
9 *
10 * Last update: 2005-05-12
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/sched.h>
17#include <linux/ptrace.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/timer.h>
21#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010022#include <linux/platform_device.h>
David Brownellc6de2b62005-05-26 05:55:55 -070023
David Brownellc6de2b62005-05-26 05:55:55 -070024#include <pcmcia/cs_types.h>
25#include <pcmcia/cs.h>
26#include <pcmcia/cistpl.h>
27#include <pcmcia/cisreg.h>
28#include <pcmcia/ds.h>
29
David Brownell325a4af2006-06-13 09:59:32 -070030#include <linux/usb/sl811.h>
David Brownellc6de2b62005-05-26 05:55:55 -070031
32MODULE_AUTHOR("Botond Botyanszki");
33MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
34MODULE_LICENSE("GPL");
35
36
37/*====================================================================*/
38/* MACROS */
39/*====================================================================*/
40
David Brownellc9a50cc2005-11-07 20:45:20 -080041#if defined(DEBUG) || defined(PCMCIA_DEBUG)
David Brownellc6de2b62005-05-26 05:55:55 -070042
43static int pc_debug = 0;
44module_param(pc_debug, int, 0644);
45
46#define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
47
48#else
49#define DBG(n, args...) do{}while(0)
50#endif /* no debugging */
51
52#define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
53
54#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
55
56#define CS_CHECK(fn, ret) \
57 do { \
58 last_fn = (fn); \
59 if ((last_ret = (ret)) != 0) \
60 goto cs_failed; \
61 } while (0)
62
63/*====================================================================*/
64/* VARIABLES */
65/*====================================================================*/
66
67static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
68
David Brownellc6de2b62005-05-26 05:55:55 -070069typedef struct local_info_t {
Dominik Brodowskifd238232006-03-05 10:45:09 +010070 struct pcmcia_device *p_dev;
David Brownellc6de2b62005-05-26 05:55:55 -070071 dev_node_t node;
72} local_info_t;
73
Dominik Brodowskifba395e2006-03-31 17:21:06 +020074static void sl811_cs_release(struct pcmcia_device * link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +010075
David Brownellc6de2b62005-05-26 05:55:55 -070076/*====================================================================*/
77
78static void release_platform_dev(struct device * dev)
79{
80 DBG(0, "sl811_cs platform_dev release\n");
81 dev->parent = NULL;
82}
83
84static struct sl811_platform_data platform_data = {
85 .potpg = 100,
86 .power = 50, /* == 100mA */
87 // .reset = ... FIXME: invoke CF reset on the card
88};
89
90static struct resource resources[] = {
91 [0] = {
92 .flags = IORESOURCE_IRQ,
93 },
94 [1] = {
95 // .name = "address",
96 .flags = IORESOURCE_IO,
97 },
98 [2] = {
99 // .name = "data",
100 .flags = IORESOURCE_IO,
101 },
102};
103
David Brownella0c53f12006-02-06 12:15:15 -0800104extern struct platform_driver sl811h_driver;
David Brownellc6de2b62005-05-26 05:55:55 -0700105
106static struct platform_device platform_dev = {
107 .id = -1,
108 .dev = {
109 .platform_data = &platform_data,
110 .release = release_platform_dev,
111 },
112 .resource = resources,
113 .num_resources = ARRAY_SIZE(resources),
114};
115
116static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
117{
118 if (platform_dev.dev.parent)
119 return -EBUSY;
120 platform_dev.dev.parent = parent;
121
122 /* finish seting up the platform device */
123 resources[0].start = irq;
124
125 resources[1].start = base_addr;
126 resources[1].end = base_addr;
127
128 resources[2].start = base_addr + 1;
129 resources[2].end = base_addr + 1;
130
131 /* The driver core will probe for us. We know sl811-hcd has been
David Brownellc9a50cc2005-11-07 20:45:20 -0800132 * initialized already because of the link order dependency created
133 * by referencing "sl811h_driver".
David Brownellc6de2b62005-05-26 05:55:55 -0700134 */
David Brownella0c53f12006-02-06 12:15:15 -0800135 platform_dev.name = sl811h_driver.driver.name;
David Brownellc6de2b62005-05-26 05:55:55 -0700136 return platform_device_register(&platform_dev);
137}
138
139/*====================================================================*/
140
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200141static void sl811_cs_detach(struct pcmcia_device *link)
David Brownellc6de2b62005-05-26 05:55:55 -0700142{
David Brownellc6de2b62005-05-26 05:55:55 -0700143 DBG(0, "sl811_cs_detach(0x%p)\n", link);
144
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100145 sl811_cs_release(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700146
David Brownellc6de2b62005-05-26 05:55:55 -0700147 /* This points to the parent local_info_t struct */
148 kfree(link->priv);
149}
150
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200151static void sl811_cs_release(struct pcmcia_device * link)
David Brownellc6de2b62005-05-26 05:55:55 -0700152{
David Brownellc6de2b62005-05-26 05:55:55 -0700153 DBG(0, "sl811_cs_release(0x%p)\n", link);
154
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200155 pcmcia_disable_device(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700156 platform_device_unregister(&platform_dev);
David Brownellc6de2b62005-05-26 05:55:55 -0700157}
158
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200159static int sl811_cs_config(struct pcmcia_device *link)
David Brownellc6de2b62005-05-26 05:55:55 -0700160{
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200161 struct device *parent = &handle_to_dev(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700162 local_info_t *dev = link->priv;
163 tuple_t tuple;
164 cisparse_t parse;
165 int last_fn, last_ret;
166 u_char buf[64];
167 config_info_t conf;
168 cistpl_cftable_entry_t dflt = { 0 };
169
170 DBG(0, "sl811_cs_config(0x%p)\n", link);
171
David Brownellc6de2b62005-05-26 05:55:55 -0700172 /* Look up the current Vcc */
173 CS_CHECK(GetConfigurationInfo,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200174 pcmcia_get_configuration_info(link, &conf));
David Brownellc6de2b62005-05-26 05:55:55 -0700175
Dominik Brodowskiaf2b3b52006-10-25 21:49:27 -0400176 tuple.Attributes = 0;
177 tuple.TupleData = buf;
178 tuple.TupleDataMax = sizeof(buf);
179 tuple.TupleOffset = 0;
David Brownellc6de2b62005-05-26 05:55:55 -0700180 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200181 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
David Brownellc6de2b62005-05-26 05:55:55 -0700182 while (1) {
183 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
184
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200185 if (pcmcia_get_tuple_data(link, &tuple) != 0
186 || pcmcia_parse_tuple(link, &tuple, &parse)
David Brownellc6de2b62005-05-26 05:55:55 -0700187 != 0)
188 goto next_entry;
189
190 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) {
191 dflt = *cfg;
192 }
193
194 if (cfg->index == 0)
195 goto next_entry;
196
197 link->conf.ConfigIndex = cfg->index;
198
199 /* Use power settings for Vcc and Vpp if present */
200 /* Note that the CIS values need to be rescaled */
201 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
202 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000
203 != conf.Vcc)
204 goto next_entry;
205 } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) {
206 if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000
207 != conf.Vcc)
208 goto next_entry;
209 }
210
211 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
Dominik Brodowski70294b42006-01-15 12:43:16 +0100212 link->conf.Vpp =
David Brownellc6de2b62005-05-26 05:55:55 -0700213 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
214 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
Dominik Brodowski70294b42006-01-15 12:43:16 +0100215 link->conf.Vpp =
David Brownellc6de2b62005-05-26 05:55:55 -0700216 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
217
218 /* we need an interrupt */
219 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
220 link->conf.Attributes |= CONF_ENABLE_IRQ;
221
222 /* IO window settings */
223 link->io.NumPorts1 = link->io.NumPorts2 = 0;
224 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
225 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
226
227 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
228 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
229 link->io.BasePort1 = io->win[0].base;
230 link->io.NumPorts1 = io->win[0].len;
231
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200232 if (pcmcia_request_io(link, &link->io) != 0)
David Brownellc6de2b62005-05-26 05:55:55 -0700233 goto next_entry;
234 }
235 break;
236
237next_entry:
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200238 pcmcia_disable_device(link);
239 last_ret = pcmcia_get_next_tuple(link, &tuple);
David Brownellc6de2b62005-05-26 05:55:55 -0700240 }
241
242 /* require an IRQ and two registers */
243 if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
244 goto cs_failed;
245 if (link->conf.Attributes & CONF_ENABLE_IRQ)
246 CS_CHECK(RequestIRQ,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200247 pcmcia_request_irq(link, &link->irq));
David Brownellc6de2b62005-05-26 05:55:55 -0700248 else
249 goto cs_failed;
250
251 CS_CHECK(RequestConfiguration,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200252 pcmcia_request_configuration(link, &link->conf));
David Brownellc6de2b62005-05-26 05:55:55 -0700253
254 sprintf(dev->node.dev_name, driver_name);
255 dev->node.major = dev->node.minor = 0;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100256 link->dev_node = &dev->node;
David Brownellc6de2b62005-05-26 05:55:55 -0700257
Dominik Brodowski70294b42006-01-15 12:43:16 +0100258 printk(KERN_INFO "%s: index 0x%02x: ",
259 dev->node.dev_name, link->conf.ConfigIndex);
260 if (link->conf.Vpp)
261 printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
David Brownellc6de2b62005-05-26 05:55:55 -0700262 printk(", irq %d", link->irq.AssignedIRQ);
263 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
264 link->io.BasePort1+link->io.NumPorts1-1);
265 printk("\n");
266
David Brownellc6de2b62005-05-26 05:55:55 -0700267 if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
268 < 0) {
269cs_failed:
270 printk("sl811_cs_config failed\n");
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200271 cs_error(link, last_fn, last_ret);
David Brownellc6de2b62005-05-26 05:55:55 -0700272 sl811_cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200273 return -ENODEV;
David Brownellc6de2b62005-05-26 05:55:55 -0700274 }
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200275 return 0;
David Brownellc6de2b62005-05-26 05:55:55 -0700276}
277
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200278static int sl811_cs_probe(struct pcmcia_device *link)
David Brownellc6de2b62005-05-26 05:55:55 -0700279{
280 local_info_t *local;
David Brownellc6de2b62005-05-26 05:55:55 -0700281
282 local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
283 if (!local)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100284 return -ENOMEM;
David Brownellc6de2b62005-05-26 05:55:55 -0700285 memset(local, 0, sizeof(local_info_t));
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200286 local->p_dev = link;
David Brownellc6de2b62005-05-26 05:55:55 -0700287 link->priv = local;
288
289 /* Initialize */
290 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
291 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
David Brownellc6de2b62005-05-26 05:55:55 -0700292 link->irq.Handler = NULL;
293
294 link->conf.Attributes = 0;
David Brownellc6de2b62005-05-26 05:55:55 -0700295 link->conf.IntType = INT_MEMORY_AND_IO;
296
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200297 return sl811_cs_config(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700298}
299
David Brownell22f3a8f2005-06-27 16:28:43 -0700300static struct pcmcia_device_id sl811_ids[] = {
301 PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
302 PCMCIA_DEVICE_NULL,
303};
304MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
305
David Brownellc6de2b62005-05-26 05:55:55 -0700306static struct pcmcia_driver sl811_cs_driver = {
307 .owner = THIS_MODULE,
308 .drv = {
309 .name = (char *)driver_name,
310 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200311 .probe = sl811_cs_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100312 .remove = sl811_cs_detach,
David Brownell22f3a8f2005-06-27 16:28:43 -0700313 .id_table = sl811_ids,
David Brownellc6de2b62005-05-26 05:55:55 -0700314};
315
316/*====================================================================*/
317
318static int __init init_sl811_cs(void)
319{
320 return pcmcia_register_driver(&sl811_cs_driver);
321}
322module_init(init_sl811_cs);
323
324static void __exit exit_sl811_cs(void)
325{
326 pcmcia_unregister_driver(&sl811_cs_driver);
327}
328module_exit(exit_sl811_cs);