David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 1 | /* |
| 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 Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 16 | #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 King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 21 | #include <linux/platform_device.h> |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 22 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 23 | #include <pcmcia/cs.h> |
| 24 | #include <pcmcia/cistpl.h> |
| 25 | #include <pcmcia/cisreg.h> |
| 26 | #include <pcmcia/ds.h> |
| 27 | |
David Brownell | 325a4af | 2006-06-13 09:59:32 -0700 | [diff] [blame] | 28 | #include <linux/usb/sl811.h> |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 29 | |
| 30 | MODULE_AUTHOR("Botond Botyanszki"); |
| 31 | MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6"); |
| 32 | MODULE_LICENSE("GPL"); |
| 33 | |
| 34 | |
| 35 | /*====================================================================*/ |
| 36 | /* MACROS */ |
| 37 | /*====================================================================*/ |
| 38 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 39 | #define INFO(args...) printk(KERN_INFO "sl811_cs: " args) |
| 40 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 41 | /*====================================================================*/ |
| 42 | /* VARIABLES */ |
| 43 | /*====================================================================*/ |
| 44 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 45 | typedef struct local_info_t { |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 46 | struct pcmcia_device *p_dev; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 47 | } local_info_t; |
| 48 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 49 | static void sl811_cs_release(struct pcmcia_device * link); |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 50 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 51 | /*====================================================================*/ |
| 52 | |
| 53 | static void release_platform_dev(struct device * dev) |
| 54 | { |
Dominik Brodowski | 9b44de2 | 2009-10-24 15:55:39 +0200 | [diff] [blame] | 55 | dev_dbg(dev, "sl811_cs platform_dev release\n"); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 56 | dev->parent = NULL; |
| 57 | } |
| 58 | |
| 59 | static struct sl811_platform_data platform_data = { |
| 60 | .potpg = 100, |
| 61 | .power = 50, /* == 100mA */ |
| 62 | // .reset = ... FIXME: invoke CF reset on the card |
| 63 | }; |
| 64 | |
| 65 | static struct resource resources[] = { |
| 66 | [0] = { |
| 67 | .flags = IORESOURCE_IRQ, |
| 68 | }, |
| 69 | [1] = { |
| 70 | // .name = "address", |
| 71 | .flags = IORESOURCE_IO, |
| 72 | }, |
| 73 | [2] = { |
| 74 | // .name = "data", |
| 75 | .flags = IORESOURCE_IO, |
| 76 | }, |
| 77 | }; |
| 78 | |
David Brownell | a0c53f1 | 2006-02-06 12:15:15 -0800 | [diff] [blame] | 79 | extern struct platform_driver sl811h_driver; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 80 | |
| 81 | static struct platform_device platform_dev = { |
| 82 | .id = -1, |
| 83 | .dev = { |
| 84 | .platform_data = &platform_data, |
| 85 | .release = release_platform_dev, |
| 86 | }, |
| 87 | .resource = resources, |
| 88 | .num_resources = ARRAY_SIZE(resources), |
| 89 | }; |
| 90 | |
Dominik Brodowski | d39bd56 | 2008-08-28 01:05:34 +0200 | [diff] [blame] | 91 | static int sl811_hc_init(struct device *parent, resource_size_t base_addr, |
| 92 | int irq) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 93 | { |
| 94 | if (platform_dev.dev.parent) |
| 95 | return -EBUSY; |
| 96 | platform_dev.dev.parent = parent; |
| 97 | |
| 98 | /* finish seting up the platform device */ |
| 99 | resources[0].start = irq; |
| 100 | |
| 101 | resources[1].start = base_addr; |
| 102 | resources[1].end = base_addr; |
| 103 | |
| 104 | resources[2].start = base_addr + 1; |
| 105 | resources[2].end = base_addr + 1; |
| 106 | |
| 107 | /* The driver core will probe for us. We know sl811-hcd has been |
David Brownell | c9a50cc | 2005-11-07 20:45:20 -0800 | [diff] [blame] | 108 | * initialized already because of the link order dependency created |
| 109 | * by referencing "sl811h_driver". |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 110 | */ |
David Brownell | a0c53f1 | 2006-02-06 12:15:15 -0800 | [diff] [blame] | 111 | platform_dev.name = sl811h_driver.driver.name; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 112 | return platform_device_register(&platform_dev); |
| 113 | } |
| 114 | |
| 115 | /*====================================================================*/ |
| 116 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 117 | static void sl811_cs_detach(struct pcmcia_device *link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 118 | { |
Dominik Brodowski | 9b44de2 | 2009-10-24 15:55:39 +0200 | [diff] [blame] | 119 | dev_dbg(&link->dev, "sl811_cs_detach\n"); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 120 | |
Dominik Brodowski | e2d4096 | 2006-03-02 00:09:29 +0100 | [diff] [blame] | 121 | sl811_cs_release(link); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 122 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 123 | /* This points to the parent local_info_t struct */ |
| 124 | kfree(link->priv); |
| 125 | } |
| 126 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 127 | static void sl811_cs_release(struct pcmcia_device * link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 128 | { |
Dominik Brodowski | 9b44de2 | 2009-10-24 15:55:39 +0200 | [diff] [blame] | 129 | dev_dbg(&link->dev, "sl811_cs_release\n"); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 130 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 131 | pcmcia_disable_device(link); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 132 | platform_device_unregister(&platform_dev); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 135 | static int sl811_cs_config_check(struct pcmcia_device *p_dev, |
| 136 | cistpl_cftable_entry_t *cfg, |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 137 | cistpl_cftable_entry_t *dflt, |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 138 | unsigned int vcc, |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 139 | void *priv_data) |
| 140 | { |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 141 | if (cfg->index == 0) |
| 142 | return -ENODEV; |
| 143 | |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 144 | /* Use power settings for Vcc and Vpp if present */ |
| 145 | /* Note that the CIS values need to be rescaled */ |
| 146 | if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) { |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 147 | if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000 != vcc) |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 148 | return -ENODEV; |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 149 | } else if (dflt->vcc.present & (1<<CISTPL_POWER_VNOM)) { |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 150 | if (dflt->vcc.param[CISTPL_POWER_VNOM]/10000 != vcc) |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 151 | return -ENODEV; |
| 152 | } |
| 153 | |
| 154 | if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM)) |
| 155 | p_dev->conf.Vpp = |
| 156 | cfg->vpp1.param[CISTPL_POWER_VNOM]/10000; |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 157 | else if (dflt->vpp1.present & (1<<CISTPL_POWER_VNOM)) |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 158 | p_dev->conf.Vpp = |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 159 | dflt->vpp1.param[CISTPL_POWER_VNOM]/10000; |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 160 | |
| 161 | /* we need an interrupt */ |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 162 | p_dev->conf.Attributes |= CONF_ENABLE_IRQ; |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 163 | |
| 164 | /* IO window settings */ |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame^] | 165 | p_dev->resource[0]->end = p_dev->resource[1]->end = 0; |
Dominik Brodowski | 8e2fc39 | 2008-08-02 15:30:31 +0200 | [diff] [blame] | 166 | if ((cfg->io.nwin > 0) || (dflt->io.nwin > 0)) { |
| 167 | cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt->io; |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame^] | 168 | p_dev->io_lines = io->flags & CISTPL_IO_LINES_MASK; |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 169 | |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame^] | 170 | p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8; |
| 171 | p_dev->resource[0]->start = io->win[0].base; |
| 172 | p_dev->resource[0]->end = io->win[0].len; |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 173 | |
Dominik Brodowski | 90abdc3 | 2010-07-24 17:23:51 +0200 | [diff] [blame^] | 174 | return pcmcia_request_io(p_dev); |
Dominik Brodowski | 84e2d34 | 2008-07-29 08:38:55 +0200 | [diff] [blame] | 175 | } |
| 176 | pcmcia_disable_device(p_dev); |
| 177 | return -ENODEV; |
| 178 | } |
| 179 | |
| 180 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 181 | static int sl811_cs_config(struct pcmcia_device *link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 182 | { |
Dominik Brodowski | dd2e5a1 | 2009-11-03 10:27:34 +0100 | [diff] [blame] | 183 | struct device *parent = &link->dev; |
Dominik Brodowski | 9b44de2 | 2009-10-24 15:55:39 +0200 | [diff] [blame] | 184 | int ret; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 185 | |
Dominik Brodowski | 9b44de2 | 2009-10-24 15:55:39 +0200 | [diff] [blame] | 186 | dev_dbg(&link->dev, "sl811_cs_config\n"); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 187 | |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 188 | if (pcmcia_loop_config(link, sl811_cs_config_check, NULL)) |
| 189 | goto failed; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 190 | |
| 191 | /* require an IRQ and two registers */ |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 192 | if (resource_size(link->resource[0]) < 2) |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 193 | goto failed; |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 194 | |
| 195 | if (!link->irq) |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 196 | goto failed; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 197 | |
Dominik Brodowski | 9b44de2 | 2009-10-24 15:55:39 +0200 | [diff] [blame] | 198 | ret = pcmcia_request_configuration(link, &link->conf); |
| 199 | if (ret) |
| 200 | goto failed; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 201 | |
Dominik Brodowski | ded6a1a | 2010-03-20 19:35:12 +0100 | [diff] [blame] | 202 | dev_info(&link->dev, "index 0x%02x: ", |
| 203 | link->conf.ConfigIndex); |
Dominik Brodowski | 70294b4 | 2006-01-15 12:43:16 +0100 | [diff] [blame] | 204 | if (link->conf.Vpp) |
| 205 | printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10); |
Dominik Brodowski | eb14120 | 2010-03-07 12:21:16 +0100 | [diff] [blame] | 206 | printk(", irq %d", link->irq); |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 207 | printk(", io %pR", link->resource[0]); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 208 | printk("\n"); |
| 209 | |
Dominik Brodowski | 9a017a9 | 2010-07-24 15:58:54 +0200 | [diff] [blame] | 210 | if (sl811_hc_init(parent, link->resource[0]->start, link->irq) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 211 | < 0) { |
Dominik Brodowski | ad913c1 | 2008-08-02 16:12:00 +0200 | [diff] [blame] | 212 | failed: |
| 213 | printk(KERN_WARNING "sl811_cs_config failed\n"); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 214 | sl811_cs_release(link); |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 215 | return -ENODEV; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 216 | } |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 217 | return 0; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 220 | static int sl811_cs_probe(struct pcmcia_device *link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 221 | { |
| 222 | local_info_t *local; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 223 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 224 | local = kzalloc(sizeof(local_info_t), GFP_KERNEL); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 225 | if (!local) |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 226 | return -ENOMEM; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame] | 227 | local->p_dev = link; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 228 | link->priv = local; |
| 229 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 230 | link->conf.Attributes = 0; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 231 | link->conf.IntType = INT_MEMORY_AND_IO; |
| 232 | |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 233 | return sl811_cs_config(link); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 234 | } |
| 235 | |
David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 236 | static struct pcmcia_device_id sl811_ids[] = { |
| 237 | PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */ |
| 238 | PCMCIA_DEVICE_NULL, |
| 239 | }; |
| 240 | MODULE_DEVICE_TABLE(pcmcia, sl811_ids); |
| 241 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 242 | static struct pcmcia_driver sl811_cs_driver = { |
| 243 | .owner = THIS_MODULE, |
| 244 | .drv = { |
Dominik Brodowski | ac8b422 | 2010-07-21 22:38:13 +0200 | [diff] [blame] | 245 | .name = "sl811_cs", |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 246 | }, |
Dominik Brodowski | 15b99ac | 2006-03-31 17:26:06 +0200 | [diff] [blame] | 247 | .probe = sl811_cs_probe, |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 248 | .remove = sl811_cs_detach, |
David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 249 | .id_table = sl811_ids, |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /*====================================================================*/ |
| 253 | |
| 254 | static int __init init_sl811_cs(void) |
| 255 | { |
| 256 | return pcmcia_register_driver(&sl811_cs_driver); |
| 257 | } |
| 258 | module_init(init_sl811_cs); |
| 259 | |
| 260 | static void __exit exit_sl811_cs(void) |
| 261 | { |
| 262 | pcmcia_unregister_driver(&sl811_cs_driver); |
| 263 | } |
| 264 | module_exit(exit_sl811_cs); |