blob: 78cc32e7b014f29157d6bed4667a7215e367c9a7 [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>
David Brownellc6de2b62005-05-26 05:55:55 -070016#include <linux/ptrace.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/timer.h>
20#include <linux/ioport.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010021#include <linux/platform_device.h>
David Brownellc6de2b62005-05-26 05:55:55 -070022
David Brownellc6de2b62005-05-26 05:55:55 -070023#include <pcmcia/cs_types.h>
24#include <pcmcia/cs.h>
25#include <pcmcia/cistpl.h>
26#include <pcmcia/cisreg.h>
27#include <pcmcia/ds.h>
28
David Brownell325a4af2006-06-13 09:59:32 -070029#include <linux/usb/sl811.h>
David Brownellc6de2b62005-05-26 05:55:55 -070030
31MODULE_AUTHOR("Botond Botyanszki");
32MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6");
33MODULE_LICENSE("GPL");
34
35
36/*====================================================================*/
37/* MACROS */
38/*====================================================================*/
39
David Brownellc9a50cc2005-11-07 20:45:20 -080040#if defined(DEBUG) || defined(PCMCIA_DEBUG)
David Brownellc6de2b62005-05-26 05:55:55 -070041
42static int pc_debug = 0;
43module_param(pc_debug, int, 0644);
44
45#define DBG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG "sl811_cs: " args)
46
47#else
48#define DBG(n, args...) do{}while(0)
49#endif /* no debugging */
50
51#define INFO(args...) printk(KERN_INFO "sl811_cs: " args)
52
53#define INT_MODULE_PARM(n, v) static int n = v; module_param(n, int, 0444)
54
55#define CS_CHECK(fn, ret) \
56 do { \
57 last_fn = (fn); \
58 if ((last_ret = (ret)) != 0) \
59 goto cs_failed; \
60 } while (0)
61
62/*====================================================================*/
63/* VARIABLES */
64/*====================================================================*/
65
66static const char driver_name[DEV_NAME_LEN] = "sl811_cs";
67
David Brownellc6de2b62005-05-26 05:55:55 -070068typedef struct local_info_t {
Dominik Brodowskifd238232006-03-05 10:45:09 +010069 struct pcmcia_device *p_dev;
David Brownellc6de2b62005-05-26 05:55:55 -070070 dev_node_t node;
71} local_info_t;
72
Dominik Brodowskifba395e2006-03-31 17:21:06 +020073static void sl811_cs_release(struct pcmcia_device * link);
Dominik Brodowskicc3b4862005-11-14 21:23:14 +010074
David Brownellc6de2b62005-05-26 05:55:55 -070075/*====================================================================*/
76
77static void release_platform_dev(struct device * dev)
78{
79 DBG(0, "sl811_cs platform_dev release\n");
80 dev->parent = NULL;
81}
82
83static struct sl811_platform_data platform_data = {
84 .potpg = 100,
85 .power = 50, /* == 100mA */
86 // .reset = ... FIXME: invoke CF reset on the card
87};
88
89static struct resource resources[] = {
90 [0] = {
91 .flags = IORESOURCE_IRQ,
92 },
93 [1] = {
94 // .name = "address",
95 .flags = IORESOURCE_IO,
96 },
97 [2] = {
98 // .name = "data",
99 .flags = IORESOURCE_IO,
100 },
101};
102
David Brownella0c53f12006-02-06 12:15:15 -0800103extern struct platform_driver sl811h_driver;
David Brownellc6de2b62005-05-26 05:55:55 -0700104
105static struct platform_device platform_dev = {
106 .id = -1,
107 .dev = {
108 .platform_data = &platform_data,
109 .release = release_platform_dev,
110 },
111 .resource = resources,
112 .num_resources = ARRAY_SIZE(resources),
113};
114
115static int sl811_hc_init(struct device *parent, ioaddr_t base_addr, int irq)
116{
117 if (platform_dev.dev.parent)
118 return -EBUSY;
119 platform_dev.dev.parent = parent;
120
121 /* finish seting up the platform device */
122 resources[0].start = irq;
123
124 resources[1].start = base_addr;
125 resources[1].end = base_addr;
126
127 resources[2].start = base_addr + 1;
128 resources[2].end = base_addr + 1;
129
130 /* The driver core will probe for us. We know sl811-hcd has been
David Brownellc9a50cc2005-11-07 20:45:20 -0800131 * initialized already because of the link order dependency created
132 * by referencing "sl811h_driver".
David Brownellc6de2b62005-05-26 05:55:55 -0700133 */
David Brownella0c53f12006-02-06 12:15:15 -0800134 platform_dev.name = sl811h_driver.driver.name;
David Brownellc6de2b62005-05-26 05:55:55 -0700135 return platform_device_register(&platform_dev);
136}
137
138/*====================================================================*/
139
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200140static void sl811_cs_detach(struct pcmcia_device *link)
David Brownellc6de2b62005-05-26 05:55:55 -0700141{
David Brownellc6de2b62005-05-26 05:55:55 -0700142 DBG(0, "sl811_cs_detach(0x%p)\n", link);
143
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100144 sl811_cs_release(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700145
David Brownellc6de2b62005-05-26 05:55:55 -0700146 /* This points to the parent local_info_t struct */
147 kfree(link->priv);
148}
149
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200150static void sl811_cs_release(struct pcmcia_device * link)
David Brownellc6de2b62005-05-26 05:55:55 -0700151{
David Brownellc6de2b62005-05-26 05:55:55 -0700152 DBG(0, "sl811_cs_release(0x%p)\n", link);
153
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200154 pcmcia_disable_device(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700155 platform_device_unregister(&platform_dev);
David Brownellc6de2b62005-05-26 05:55:55 -0700156}
157
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200158struct sl811_css_cfg {
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200159 config_info_t conf;
160};
161
162static int sl811_cs_config_check(struct pcmcia_device *p_dev,
163 cistpl_cftable_entry_t *cfg,
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200164 cistpl_cftable_entry_t *dflt,
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200165 void *priv_data)
166{
167 struct sl811_css_cfg *cfg_mem = priv_data;
168
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200169 if (cfg->index == 0)
170 return -ENODEV;
171
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200172 /* Use power settings for Vcc and Vpp if present */
173 /* Note that the CIS values need to be rescaled */
174 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) {
175 if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000 !=
176 cfg_mem->conf.Vcc)
177 return -ENODEV;
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200178 } else if (dflt->vcc.present & (1<<CISTPL_POWER_VNOM)) {
179 if (dflt->vcc.param[CISTPL_POWER_VNOM]/10000
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200180 != cfg_mem->conf.Vcc)
181 return -ENODEV;
182 }
183
184 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
185 p_dev->conf.Vpp =
186 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200187 else if (dflt->vpp1.present & (1<<CISTPL_POWER_VNOM))
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200188 p_dev->conf.Vpp =
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200189 dflt->vpp1.param[CISTPL_POWER_VNOM]/10000;
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200190
191 /* we need an interrupt */
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200192 if (cfg->irq.IRQInfo1 || dflt->irq.IRQInfo1)
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200193 p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
194
195 /* IO window settings */
196 p_dev->io.NumPorts1 = p_dev->io.NumPorts2 = 0;
Dominik Brodowski8e2fc392008-08-02 15:30:31 +0200197 if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) {
198 cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io;
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200199
200 p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
201 p_dev->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
202 p_dev->io.BasePort1 = io->win[0].base;
203 p_dev->io.NumPorts1 = io->win[0].len;
204
205 return pcmcia_request_io(p_dev, &p_dev->io);
206 }
207 pcmcia_disable_device(p_dev);
208 return -ENODEV;
209}
210
211
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200212static int sl811_cs_config(struct pcmcia_device *link)
David Brownellc6de2b62005-05-26 05:55:55 -0700213{
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200214 struct device *parent = &handle_to_dev(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700215 local_info_t *dev = link->priv;
David Brownellc6de2b62005-05-26 05:55:55 -0700216 int last_fn, last_ret;
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200217 struct sl811_css_cfg *cfg_mem;
David Brownellc6de2b62005-05-26 05:55:55 -0700218
219 DBG(0, "sl811_cs_config(0x%p)\n", link);
220
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200221 cfg_mem = kzalloc(sizeof(struct sl811_css_cfg), GFP_KERNEL);
222 if (!cfg_mem)
223 return -ENOMEM;
224
David Brownellc6de2b62005-05-26 05:55:55 -0700225 /* Look up the current Vcc */
226 CS_CHECK(GetConfigurationInfo,
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200227 pcmcia_get_configuration_info(link, &cfg_mem->conf));
David Brownellc6de2b62005-05-26 05:55:55 -0700228
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200229 if (pcmcia_loop_config(link, sl811_cs_config_check, cfg_mem))
230 return -ENODEV;
David Brownellc6de2b62005-05-26 05:55:55 -0700231
232 /* require an IRQ and two registers */
233 if (!link->io.NumPorts1 || link->io.NumPorts1 < 2)
234 goto cs_failed;
235 if (link->conf.Attributes & CONF_ENABLE_IRQ)
236 CS_CHECK(RequestIRQ,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200237 pcmcia_request_irq(link, &link->irq));
David Brownellc6de2b62005-05-26 05:55:55 -0700238 else
239 goto cs_failed;
240
241 CS_CHECK(RequestConfiguration,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200242 pcmcia_request_configuration(link, &link->conf));
David Brownellc6de2b62005-05-26 05:55:55 -0700243
244 sprintf(dev->node.dev_name, driver_name);
245 dev->node.major = dev->node.minor = 0;
Dominik Brodowskifd238232006-03-05 10:45:09 +0100246 link->dev_node = &dev->node;
David Brownellc6de2b62005-05-26 05:55:55 -0700247
Dominik Brodowski70294b42006-01-15 12:43:16 +0100248 printk(KERN_INFO "%s: index 0x%02x: ",
249 dev->node.dev_name, link->conf.ConfigIndex);
250 if (link->conf.Vpp)
251 printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10);
David Brownellc6de2b62005-05-26 05:55:55 -0700252 printk(", irq %d", link->irq.AssignedIRQ);
253 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
254 link->io.BasePort1+link->io.NumPorts1-1);
255 printk("\n");
256
David Brownellc6de2b62005-05-26 05:55:55 -0700257 if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ)
258 < 0) {
259cs_failed:
260 printk("sl811_cs_config failed\n");
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200261 cs_error(link, last_fn, last_ret);
David Brownellc6de2b62005-05-26 05:55:55 -0700262 sl811_cs_release(link);
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200263 kfree(cfg_mem);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200264 return -ENODEV;
David Brownellc6de2b62005-05-26 05:55:55 -0700265 }
Dominik Brodowski84e2d342008-07-29 08:38:55 +0200266 kfree(cfg_mem);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200267 return 0;
David Brownellc6de2b62005-05-26 05:55:55 -0700268}
269
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200270static int sl811_cs_probe(struct pcmcia_device *link)
David Brownellc6de2b62005-05-26 05:55:55 -0700271{
272 local_info_t *local;
David Brownellc6de2b62005-05-26 05:55:55 -0700273
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700274 local = kzalloc(sizeof(local_info_t), GFP_KERNEL);
David Brownellc6de2b62005-05-26 05:55:55 -0700275 if (!local)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100276 return -ENOMEM;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200277 local->p_dev = link;
David Brownellc6de2b62005-05-26 05:55:55 -0700278 link->priv = local;
279
280 /* Initialize */
281 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
282 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
David Brownellc6de2b62005-05-26 05:55:55 -0700283 link->irq.Handler = NULL;
284
285 link->conf.Attributes = 0;
David Brownellc6de2b62005-05-26 05:55:55 -0700286 link->conf.IntType = INT_MEMORY_AND_IO;
287
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200288 return sl811_cs_config(link);
David Brownellc6de2b62005-05-26 05:55:55 -0700289}
290
David Brownell22f3a8f2005-06-27 16:28:43 -0700291static struct pcmcia_device_id sl811_ids[] = {
292 PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */
293 PCMCIA_DEVICE_NULL,
294};
295MODULE_DEVICE_TABLE(pcmcia, sl811_ids);
296
David Brownellc6de2b62005-05-26 05:55:55 -0700297static struct pcmcia_driver sl811_cs_driver = {
298 .owner = THIS_MODULE,
299 .drv = {
300 .name = (char *)driver_name,
301 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200302 .probe = sl811_cs_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100303 .remove = sl811_cs_detach,
David Brownell22f3a8f2005-06-27 16:28:43 -0700304 .id_table = sl811_ids,
David Brownellc6de2b62005-05-26 05:55:55 -0700305};
306
307/*====================================================================*/
308
309static int __init init_sl811_cs(void)
310{
311 return pcmcia_register_driver(&sl811_cs_driver);
312}
313module_init(init_sl811_cs);
314
315static void __exit exit_sl811_cs(void)
316{
317 pcmcia_unregister_driver(&sl811_cs_driver);
318}
319module_exit(exit_sl811_cs);