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> |
| 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 King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 23 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 24 | #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 | |
| 30 | #include <linux/usb_sl811.h> |
| 31 | |
| 32 | MODULE_AUTHOR("Botond Botyanszki"); |
| 33 | MODULE_DESCRIPTION("REX-CFU1U PCMCIA driver for 2.6"); |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | |
| 36 | |
| 37 | /*====================================================================*/ |
| 38 | /* MACROS */ |
| 39 | /*====================================================================*/ |
| 40 | |
David Brownell | c9a50cc | 2005-11-07 20:45:20 -0800 | [diff] [blame] | 41 | #if defined(DEBUG) || defined(PCMCIA_DEBUG) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 42 | |
| 43 | static int pc_debug = 0; |
| 44 | module_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 | |
| 67 | static const char driver_name[DEV_NAME_LEN] = "sl811_cs"; |
| 68 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 69 | typedef struct local_info_t { |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 70 | struct pcmcia_device *p_dev; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 71 | dev_node_t node; |
| 72 | } local_info_t; |
| 73 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 74 | static void sl811_cs_release(struct pcmcia_device * link); |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 75 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 76 | /*====================================================================*/ |
| 77 | |
| 78 | static void release_platform_dev(struct device * dev) |
| 79 | { |
| 80 | DBG(0, "sl811_cs platform_dev release\n"); |
| 81 | dev->parent = NULL; |
| 82 | } |
| 83 | |
| 84 | static struct sl811_platform_data platform_data = { |
| 85 | .potpg = 100, |
| 86 | .power = 50, /* == 100mA */ |
| 87 | // .reset = ... FIXME: invoke CF reset on the card |
| 88 | }; |
| 89 | |
| 90 | static 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 Brownell | a0c53f1 | 2006-02-06 12:15:15 -0800 | [diff] [blame] | 104 | extern struct platform_driver sl811h_driver; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 105 | |
| 106 | static 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 | |
| 116 | static 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 Brownell | c9a50cc | 2005-11-07 20:45:20 -0800 | [diff] [blame] | 132 | * initialized already because of the link order dependency created |
| 133 | * by referencing "sl811h_driver". |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 134 | */ |
David Brownell | a0c53f1 | 2006-02-06 12:15:15 -0800 | [diff] [blame] | 135 | platform_dev.name = sl811h_driver.driver.name; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 136 | return platform_device_register(&platform_dev); |
| 137 | } |
| 138 | |
| 139 | /*====================================================================*/ |
| 140 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 141 | static void sl811_cs_detach(struct pcmcia_device *link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 142 | { |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 143 | DBG(0, "sl811_cs_detach(0x%p)\n", link); |
| 144 | |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 145 | link->state &= ~DEV_PRESENT; |
| 146 | if (link->state & DEV_CONFIG) |
| 147 | sl811_cs_release(link); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 148 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 149 | /* This points to the parent local_info_t struct */ |
| 150 | kfree(link->priv); |
| 151 | } |
| 152 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 153 | static void sl811_cs_release(struct pcmcia_device * link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 154 | { |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 155 | DBG(0, "sl811_cs_release(0x%p)\n", link); |
| 156 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 157 | pcmcia_disable_device(link); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 158 | platform_device_unregister(&platform_dev); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 161 | static void sl811_cs_config(struct pcmcia_device *link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 162 | { |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 163 | struct device *parent = &handle_to_dev(link); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 164 | local_info_t *dev = link->priv; |
| 165 | tuple_t tuple; |
| 166 | cisparse_t parse; |
| 167 | int last_fn, last_ret; |
| 168 | u_char buf[64]; |
| 169 | config_info_t conf; |
| 170 | cistpl_cftable_entry_t dflt = { 0 }; |
| 171 | |
| 172 | DBG(0, "sl811_cs_config(0x%p)\n", link); |
| 173 | |
| 174 | tuple.DesiredTuple = CISTPL_CONFIG; |
| 175 | tuple.Attributes = 0; |
| 176 | tuple.TupleData = buf; |
| 177 | tuple.TupleDataMax = sizeof(buf); |
| 178 | tuple.TupleOffset = 0; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 179 | CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple)); |
| 180 | CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple)); |
| 181 | CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse)); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 182 | link->conf.ConfigBase = parse.config.base; |
| 183 | link->conf.Present = parse.config.rmask[0]; |
| 184 | |
| 185 | /* Configure card */ |
| 186 | link->state |= DEV_CONFIG; |
| 187 | |
| 188 | /* Look up the current Vcc */ |
| 189 | CS_CHECK(GetConfigurationInfo, |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 190 | pcmcia_get_configuration_info(link, &conf)); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 191 | |
| 192 | tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY; |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 193 | CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple)); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 194 | while (1) { |
| 195 | cistpl_cftable_entry_t *cfg = &(parse.cftable_entry); |
| 196 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 197 | if (pcmcia_get_tuple_data(link, &tuple) != 0 |
| 198 | || pcmcia_parse_tuple(link, &tuple, &parse) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 199 | != 0) |
| 200 | goto next_entry; |
| 201 | |
| 202 | if (cfg->flags & CISTPL_CFTABLE_DEFAULT) { |
| 203 | dflt = *cfg; |
| 204 | } |
| 205 | |
| 206 | if (cfg->index == 0) |
| 207 | goto next_entry; |
| 208 | |
| 209 | link->conf.ConfigIndex = cfg->index; |
| 210 | |
| 211 | /* Use power settings for Vcc and Vpp if present */ |
| 212 | /* Note that the CIS values need to be rescaled */ |
| 213 | if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM)) { |
| 214 | if (cfg->vcc.param[CISTPL_POWER_VNOM]/10000 |
| 215 | != conf.Vcc) |
| 216 | goto next_entry; |
| 217 | } else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM)) { |
| 218 | if (dflt.vcc.param[CISTPL_POWER_VNOM]/10000 |
| 219 | != conf.Vcc) |
| 220 | goto next_entry; |
| 221 | } |
| 222 | |
| 223 | if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM)) |
Dominik Brodowski | 70294b4 | 2006-01-15 12:43:16 +0100 | [diff] [blame] | 224 | link->conf.Vpp = |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 225 | cfg->vpp1.param[CISTPL_POWER_VNOM]/10000; |
| 226 | else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM)) |
Dominik Brodowski | 70294b4 | 2006-01-15 12:43:16 +0100 | [diff] [blame] | 227 | link->conf.Vpp = |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 228 | dflt.vpp1.param[CISTPL_POWER_VNOM]/10000; |
| 229 | |
| 230 | /* we need an interrupt */ |
| 231 | if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1) |
| 232 | link->conf.Attributes |= CONF_ENABLE_IRQ; |
| 233 | |
| 234 | /* IO window settings */ |
| 235 | link->io.NumPorts1 = link->io.NumPorts2 = 0; |
| 236 | if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) { |
| 237 | cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io; |
| 238 | |
| 239 | link->io.Attributes1 = IO_DATA_PATH_WIDTH_8; |
| 240 | link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK; |
| 241 | link->io.BasePort1 = io->win[0].base; |
| 242 | link->io.NumPorts1 = io->win[0].len; |
| 243 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 244 | if (pcmcia_request_io(link, &link->io) != 0) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 245 | goto next_entry; |
| 246 | } |
| 247 | break; |
| 248 | |
| 249 | next_entry: |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 250 | pcmcia_disable_device(link); |
| 251 | last_ret = pcmcia_get_next_tuple(link, &tuple); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* require an IRQ and two registers */ |
| 255 | if (!link->io.NumPorts1 || link->io.NumPorts1 < 2) |
| 256 | goto cs_failed; |
| 257 | if (link->conf.Attributes & CONF_ENABLE_IRQ) |
| 258 | CS_CHECK(RequestIRQ, |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 259 | pcmcia_request_irq(link, &link->irq)); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 260 | else |
| 261 | goto cs_failed; |
| 262 | |
| 263 | CS_CHECK(RequestConfiguration, |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 264 | pcmcia_request_configuration(link, &link->conf)); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 265 | |
| 266 | sprintf(dev->node.dev_name, driver_name); |
| 267 | dev->node.major = dev->node.minor = 0; |
Dominik Brodowski | fd23823 | 2006-03-05 10:45:09 +0100 | [diff] [blame] | 268 | link->dev_node = &dev->node; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 269 | |
Dominik Brodowski | 70294b4 | 2006-01-15 12:43:16 +0100 | [diff] [blame] | 270 | printk(KERN_INFO "%s: index 0x%02x: ", |
| 271 | dev->node.dev_name, link->conf.ConfigIndex); |
| 272 | if (link->conf.Vpp) |
| 273 | printk(", Vpp %d.%d", link->conf.Vpp/10, link->conf.Vpp%10); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 274 | printk(", irq %d", link->irq.AssignedIRQ); |
| 275 | printk(", io 0x%04x-0x%04x", link->io.BasePort1, |
| 276 | link->io.BasePort1+link->io.NumPorts1-1); |
| 277 | printk("\n"); |
| 278 | |
| 279 | link->state &= ~DEV_CONFIG_PENDING; |
| 280 | |
| 281 | if (sl811_hc_init(parent, link->io.BasePort1, link->irq.AssignedIRQ) |
| 282 | < 0) { |
| 283 | cs_failed: |
| 284 | printk("sl811_cs_config failed\n"); |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 285 | cs_error(link, last_fn, last_ret); |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 286 | sl811_cs_release(link); |
| 287 | link->state &= ~DEV_CONFIG_PENDING; |
| 288 | } |
| 289 | } |
| 290 | |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 291 | static int sl811_cs_attach(struct pcmcia_device *link) |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 292 | { |
| 293 | local_info_t *local; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 294 | |
| 295 | local = kmalloc(sizeof(local_info_t), GFP_KERNEL); |
| 296 | if (!local) |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 297 | return -ENOMEM; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 298 | memset(local, 0, sizeof(local_info_t)); |
Dominik Brodowski | fba395e | 2006-03-31 17:21:06 +0200 | [diff] [blame^] | 299 | local->p_dev = link; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 300 | link->priv = local; |
| 301 | |
| 302 | /* Initialize */ |
| 303 | link->irq.Attributes = IRQ_TYPE_EXCLUSIVE; |
| 304 | link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 305 | link->irq.Handler = NULL; |
| 306 | |
| 307 | link->conf.Attributes = 0; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 308 | link->conf.IntType = INT_MEMORY_AND_IO; |
| 309 | |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 310 | link->state |= DEV_PRESENT | DEV_CONFIG_PENDING; |
| 311 | sl811_cs_config(link); |
| 312 | |
| 313 | return 0; |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 314 | } |
| 315 | |
David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 316 | static struct pcmcia_device_id sl811_ids[] = { |
| 317 | PCMCIA_DEVICE_MANF_CARD(0xc015, 0x0001), /* RATOC USB HOST CF+ Card */ |
| 318 | PCMCIA_DEVICE_NULL, |
| 319 | }; |
| 320 | MODULE_DEVICE_TABLE(pcmcia, sl811_ids); |
| 321 | |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 322 | static struct pcmcia_driver sl811_cs_driver = { |
| 323 | .owner = THIS_MODULE, |
| 324 | .drv = { |
| 325 | .name = (char *)driver_name, |
| 326 | }, |
Dominik Brodowski | f8cfa61 | 2005-11-14 21:25:51 +0100 | [diff] [blame] | 327 | .probe = sl811_cs_attach, |
Dominik Brodowski | cc3b486 | 2005-11-14 21:23:14 +0100 | [diff] [blame] | 328 | .remove = sl811_cs_detach, |
David Brownell | 22f3a8f | 2005-06-27 16:28:43 -0700 | [diff] [blame] | 329 | .id_table = sl811_ids, |
David Brownell | c6de2b6 | 2005-05-26 05:55:55 -0700 | [diff] [blame] | 330 | }; |
| 331 | |
| 332 | /*====================================================================*/ |
| 333 | |
| 334 | static int __init init_sl811_cs(void) |
| 335 | { |
| 336 | return pcmcia_register_driver(&sl811_cs_driver); |
| 337 | } |
| 338 | module_init(init_sl811_cs); |
| 339 | |
| 340 | static void __exit exit_sl811_cs(void) |
| 341 | { |
| 342 | pcmcia_unregister_driver(&sl811_cs_driver); |
| 343 | } |
| 344 | module_exit(exit_sl811_cs); |