Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2010-2012 Stephane Grosjean <s.grosjean@peak-system.com> |
| 3 | * |
| 4 | * CAN driver for PEAK-System PCAN-PC Card |
| 5 | * Derived from the PCAN project file driver/src/pcan_pccard.c |
| 6 | * Copyright (C) 2006-2010 PEAK System-Technik GmbH |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the version 2 of the GNU General Public License |
| 10 | * as published by the Free Software Foundation |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/delay.h> |
| 22 | #include <linux/timer.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <pcmcia/cistpl.h> |
| 25 | #include <pcmcia/ds.h> |
| 26 | #include <linux/can.h> |
| 27 | #include <linux/can/dev.h> |
| 28 | #include "sja1000.h" |
| 29 | |
| 30 | MODULE_AUTHOR("Stephane Grosjean <s.grosjean@peak-system.com>"); |
| 31 | MODULE_DESCRIPTION("CAN driver for PEAK-System PCAN-PC Cards"); |
| 32 | MODULE_LICENSE("GPL v2"); |
| 33 | MODULE_SUPPORTED_DEVICE("PEAK PCAN-PC Card"); |
| 34 | |
| 35 | /* PEAK-System PCMCIA driver name */ |
| 36 | #define PCC_NAME "peak_pcmcia" |
| 37 | |
| 38 | #define PCC_CHAN_MAX 2 |
| 39 | |
| 40 | #define PCC_CAN_CLOCK (16000000 / 2) |
| 41 | |
| 42 | #define PCC_MANF_ID 0x0377 |
| 43 | #define PCC_CARD_ID 0x0001 |
| 44 | |
| 45 | #define PCC_CHAN_SIZE 0x20 |
| 46 | #define PCC_CHAN_OFF(c) ((c) * PCC_CHAN_SIZE) |
| 47 | #define PCC_COMN_OFF (PCC_CHAN_OFF(PCC_CHAN_MAX)) |
| 48 | #define PCC_COMN_SIZE 0x40 |
| 49 | |
| 50 | /* common area registers */ |
| 51 | #define PCC_CCR 0x00 |
| 52 | #define PCC_CSR 0x02 |
| 53 | #define PCC_CPR 0x04 |
| 54 | #define PCC_SPI_DIR 0x06 |
| 55 | #define PCC_SPI_DOR 0x08 |
| 56 | #define PCC_SPI_ADR 0x0a |
| 57 | #define PCC_SPI_IR 0x0c |
| 58 | #define PCC_FW_MAJOR 0x10 |
| 59 | #define PCC_FW_MINOR 0x12 |
| 60 | |
| 61 | /* CCR bits */ |
| 62 | #define PCC_CCR_CLK_16 0x00 |
| 63 | #define PCC_CCR_CLK_10 0x01 |
| 64 | #define PCC_CCR_CLK_21 0x02 |
| 65 | #define PCC_CCR_CLK_8 0x03 |
| 66 | #define PCC_CCR_CLK_MASK PCC_CCR_CLK_8 |
| 67 | |
| 68 | #define PCC_CCR_RST_CHAN(c) (0x01 << ((c) + 2)) |
| 69 | #define PCC_CCR_RST_ALL (PCC_CCR_RST_CHAN(0) | PCC_CCR_RST_CHAN(1)) |
| 70 | #define PCC_CCR_RST_MASK PCC_CCR_RST_ALL |
| 71 | |
| 72 | /* led selection bits */ |
| 73 | #define PCC_LED(c) (1 << (c)) |
| 74 | #define PCC_LED_ALL (PCC_LED(0) | PCC_LED(1)) |
| 75 | |
| 76 | /* led state value */ |
| 77 | #define PCC_LED_ON 0x00 |
| 78 | #define PCC_LED_FAST 0x01 |
| 79 | #define PCC_LED_SLOW 0x02 |
| 80 | #define PCC_LED_OFF 0x03 |
| 81 | |
| 82 | #define PCC_CCR_LED_CHAN(s, c) ((s) << (((c) + 2) << 1)) |
| 83 | |
| 84 | #define PCC_CCR_LED_ON_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_ON, c) |
| 85 | #define PCC_CCR_LED_FAST_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_FAST, c) |
| 86 | #define PCC_CCR_LED_SLOW_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_SLOW, c) |
| 87 | #define PCC_CCR_LED_OFF_CHAN(c) PCC_CCR_LED_CHAN(PCC_LED_OFF, c) |
| 88 | #define PCC_CCR_LED_MASK_CHAN(c) PCC_CCR_LED_OFF_CHAN(c) |
| 89 | #define PCC_CCR_LED_OFF_ALL (PCC_CCR_LED_OFF_CHAN(0) | \ |
| 90 | PCC_CCR_LED_OFF_CHAN(1)) |
| 91 | #define PCC_CCR_LED_MASK PCC_CCR_LED_OFF_ALL |
| 92 | |
| 93 | #define PCC_CCR_INIT (PCC_CCR_CLK_16 | PCC_CCR_RST_ALL | PCC_CCR_LED_OFF_ALL) |
| 94 | |
| 95 | /* CSR bits */ |
| 96 | #define PCC_CSR_SPI_BUSY 0x04 |
| 97 | |
| 98 | /* time waiting for SPI busy (prevent from infinite loop) */ |
| 99 | #define PCC_SPI_MAX_BUSY_WAIT_MS 3 |
| 100 | |
| 101 | /* max count of reading the SPI status register waiting for a change */ |
| 102 | /* (prevent from infinite loop) */ |
| 103 | #define PCC_WRITE_MAX_LOOP 1000 |
| 104 | |
| 105 | /* max nb of int handled by that isr in one shot (prevent from infinite loop) */ |
| 106 | #define PCC_ISR_MAX_LOOP 10 |
| 107 | |
| 108 | /* EEPROM chip instruction set */ |
| 109 | /* note: EEPROM Read/Write instructions include A8 bit */ |
| 110 | #define PCC_EEP_WRITE(a) (0x02 | (((a) & 0x100) >> 5)) |
| 111 | #define PCC_EEP_READ(a) (0x03 | (((a) & 0x100) >> 5)) |
| 112 | #define PCC_EEP_WRDI 0x04 /* EEPROM Write Disable */ |
| 113 | #define PCC_EEP_RDSR 0x05 /* EEPROM Read Status Register */ |
| 114 | #define PCC_EEP_WREN 0x06 /* EEPROM Write Enable */ |
| 115 | |
| 116 | /* EEPROM Status Register bits */ |
| 117 | #define PCC_EEP_SR_WEN 0x02 /* EEPROM SR Write Enable bit */ |
| 118 | #define PCC_EEP_SR_WIP 0x01 /* EEPROM SR Write In Progress bit */ |
| 119 | |
| 120 | /* |
| 121 | * The board configuration is probably following: |
| 122 | * RX1 is connected to ground. |
| 123 | * TX1 is not connected. |
| 124 | * CLKO is not connected. |
| 125 | * Setting the OCR register to 0xDA is a good idea. |
| 126 | * This means normal output mode, push-pull and the correct polarity. |
| 127 | */ |
| 128 | #define PCC_OCR (OCR_TX0_PUSHPULL | OCR_TX1_PUSHPULL) |
| 129 | |
| 130 | /* |
| 131 | * In the CDR register, you should set CBP to 1. |
| 132 | * You will probably also want to set the clock divider value to 7 |
| 133 | * (meaning direct oscillator output) because the second SJA1000 chip |
| 134 | * is driven by the first one CLKOUT output. |
| 135 | */ |
| 136 | #define PCC_CDR (CDR_CBP | CDR_CLKOUT_MASK) |
| 137 | |
| 138 | struct pcan_channel { |
| 139 | struct net_device *netdev; |
| 140 | unsigned long prev_rx_bytes; |
| 141 | unsigned long prev_tx_bytes; |
| 142 | }; |
| 143 | |
| 144 | /* PCAN-PC Card private structure */ |
| 145 | struct pcan_pccard { |
| 146 | struct pcmcia_device *pdev; |
| 147 | int chan_count; |
| 148 | struct pcan_channel channel[PCC_CHAN_MAX]; |
| 149 | u8 ccr; |
| 150 | u8 fw_major; |
| 151 | u8 fw_minor; |
| 152 | void __iomem *ioport_addr; |
| 153 | struct timer_list led_timer; |
| 154 | }; |
| 155 | |
| 156 | static struct pcmcia_device_id pcan_table[] = { |
| 157 | PCMCIA_DEVICE_MANF_CARD(PCC_MANF_ID, PCC_CARD_ID), |
| 158 | PCMCIA_DEVICE_NULL, |
| 159 | }; |
| 160 | |
| 161 | MODULE_DEVICE_TABLE(pcmcia, pcan_table); |
| 162 | |
| 163 | static void pcan_set_leds(struct pcan_pccard *card, u8 mask, u8 state); |
| 164 | |
| 165 | /* |
| 166 | * start timer which controls leds state |
| 167 | */ |
| 168 | static void pcan_start_led_timer(struct pcan_pccard *card) |
| 169 | { |
| 170 | if (!timer_pending(&card->led_timer)) |
| 171 | mod_timer(&card->led_timer, jiffies + HZ); |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * stop the timer which controls leds state |
| 176 | */ |
| 177 | static void pcan_stop_led_timer(struct pcan_pccard *card) |
| 178 | { |
| 179 | del_timer_sync(&card->led_timer); |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * read a sja1000 register |
| 184 | */ |
| 185 | static u8 pcan_read_canreg(const struct sja1000_priv *priv, int port) |
| 186 | { |
| 187 | return ioread8(priv->reg_base + port); |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * write a sja1000 register |
| 192 | */ |
| 193 | static void pcan_write_canreg(const struct sja1000_priv *priv, int port, u8 v) |
| 194 | { |
| 195 | struct pcan_pccard *card = priv->priv; |
| 196 | int c = (priv->reg_base - card->ioport_addr) / PCC_CHAN_SIZE; |
| 197 | |
| 198 | /* sja1000 register changes control the leds state */ |
| 199 | if (port == SJA1000_MOD) |
| 200 | switch (v) { |
| 201 | case MOD_RM: |
| 202 | /* Reset Mode: set led on */ |
| 203 | pcan_set_leds(card, PCC_LED(c), PCC_LED_ON); |
| 204 | break; |
| 205 | case 0x00: |
| 206 | /* Normal Mode: led slow blinking and start led timer */ |
| 207 | pcan_set_leds(card, PCC_LED(c), PCC_LED_SLOW); |
| 208 | pcan_start_led_timer(card); |
| 209 | break; |
| 210 | default: |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | iowrite8(v, priv->reg_base + port); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * read a register from the common area |
| 219 | */ |
| 220 | static u8 pcan_read_reg(struct pcan_pccard *card, int port) |
| 221 | { |
| 222 | return ioread8(card->ioport_addr + PCC_COMN_OFF + port); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * write a register into the common area |
| 227 | */ |
| 228 | static void pcan_write_reg(struct pcan_pccard *card, int port, u8 v) |
| 229 | { |
| 230 | /* cache ccr value */ |
| 231 | if (port == PCC_CCR) { |
| 232 | if (card->ccr == v) |
| 233 | return; |
| 234 | card->ccr = v; |
| 235 | } |
| 236 | |
| 237 | iowrite8(v, card->ioport_addr + PCC_COMN_OFF + port); |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * check whether the card is present by checking its fw version numbers |
| 242 | * against values read at probing time. |
| 243 | */ |
| 244 | static inline int pcan_pccard_present(struct pcan_pccard *card) |
| 245 | { |
| 246 | return ((pcan_read_reg(card, PCC_FW_MAJOR) == card->fw_major) && |
| 247 | (pcan_read_reg(card, PCC_FW_MINOR) == card->fw_minor)); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * wait for SPI engine while it is busy |
| 252 | */ |
| 253 | static int pcan_wait_spi_busy(struct pcan_pccard *card) |
| 254 | { |
| 255 | unsigned long timeout = jiffies + |
| 256 | msecs_to_jiffies(PCC_SPI_MAX_BUSY_WAIT_MS) + 1; |
| 257 | |
| 258 | /* be sure to read status at least once after sleeping */ |
| 259 | while (pcan_read_reg(card, PCC_CSR) & PCC_CSR_SPI_BUSY) { |
| 260 | if (time_after(jiffies, timeout)) |
| 261 | return -EBUSY; |
| 262 | schedule(); |
| 263 | } |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * write data in device eeprom |
| 270 | */ |
| 271 | static int pcan_write_eeprom(struct pcan_pccard *card, u16 addr, u8 v) |
| 272 | { |
| 273 | u8 status; |
| 274 | int err, i; |
| 275 | |
| 276 | /* write instruction enabling write */ |
| 277 | pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WREN); |
| 278 | err = pcan_wait_spi_busy(card); |
| 279 | if (err) |
| 280 | goto we_spi_err; |
| 281 | |
| 282 | /* wait until write enabled */ |
| 283 | for (i = 0; i < PCC_WRITE_MAX_LOOP; i++) { |
| 284 | /* write instruction reading the status register */ |
| 285 | pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_RDSR); |
| 286 | err = pcan_wait_spi_busy(card); |
| 287 | if (err) |
| 288 | goto we_spi_err; |
| 289 | |
| 290 | /* get status register value and check write enable bit */ |
| 291 | status = pcan_read_reg(card, PCC_SPI_DIR); |
| 292 | if (status & PCC_EEP_SR_WEN) |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | if (i >= PCC_WRITE_MAX_LOOP) { |
| 297 | dev_err(&card->pdev->dev, |
| 298 | "stop waiting to be allowed to write in eeprom\n"); |
| 299 | return -EIO; |
| 300 | } |
| 301 | |
| 302 | /* set address and data */ |
| 303 | pcan_write_reg(card, PCC_SPI_ADR, addr & 0xff); |
| 304 | pcan_write_reg(card, PCC_SPI_DOR, v); |
| 305 | |
| 306 | /* |
| 307 | * write instruction with bit[3] set according to address value: |
| 308 | * if addr refers to upper half of the memory array: bit[3] = 1 |
| 309 | */ |
| 310 | pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WRITE(addr)); |
| 311 | err = pcan_wait_spi_busy(card); |
| 312 | if (err) |
| 313 | goto we_spi_err; |
| 314 | |
| 315 | /* wait while write in progress */ |
| 316 | for (i = 0; i < PCC_WRITE_MAX_LOOP; i++) { |
| 317 | /* write instruction reading the status register */ |
| 318 | pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_RDSR); |
| 319 | err = pcan_wait_spi_busy(card); |
| 320 | if (err) |
| 321 | goto we_spi_err; |
| 322 | |
| 323 | /* get status register value and check write in progress bit */ |
| 324 | status = pcan_read_reg(card, PCC_SPI_DIR); |
| 325 | if (!(status & PCC_EEP_SR_WIP)) |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | if (i >= PCC_WRITE_MAX_LOOP) { |
| 330 | dev_err(&card->pdev->dev, |
| 331 | "stop waiting for write in eeprom to complete\n"); |
| 332 | return -EIO; |
| 333 | } |
| 334 | |
| 335 | /* write instruction disabling write */ |
| 336 | pcan_write_reg(card, PCC_SPI_IR, PCC_EEP_WRDI); |
| 337 | err = pcan_wait_spi_busy(card); |
| 338 | if (err) |
| 339 | goto we_spi_err; |
| 340 | |
| 341 | return 0; |
| 342 | |
| 343 | we_spi_err: |
| 344 | dev_err(&card->pdev->dev, |
| 345 | "stop waiting (spi engine always busy) err %d\n", err); |
| 346 | |
| 347 | return err; |
| 348 | } |
| 349 | |
| 350 | static void pcan_set_leds(struct pcan_pccard *card, u8 led_mask, u8 state) |
| 351 | { |
| 352 | u8 ccr = card->ccr; |
| 353 | int i; |
| 354 | |
| 355 | for (i = 0; i < card->chan_count; i++) |
| 356 | if (led_mask & PCC_LED(i)) { |
| 357 | /* clear corresponding led bits in ccr */ |
| 358 | ccr &= ~PCC_CCR_LED_MASK_CHAN(i); |
| 359 | /* then set new bits */ |
| 360 | ccr |= PCC_CCR_LED_CHAN(state, i); |
| 361 | } |
| 362 | |
| 363 | /* real write only if something has changed in ccr */ |
| 364 | pcan_write_reg(card, PCC_CCR, ccr); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * enable/disable CAN connectors power |
| 369 | */ |
| 370 | static inline void pcan_set_can_power(struct pcan_pccard *card, int onoff) |
| 371 | { |
| 372 | int err; |
| 373 | |
| 374 | err = pcan_write_eeprom(card, 0, !!onoff); |
| 375 | if (err) |
| 376 | dev_err(&card->pdev->dev, |
| 377 | "failed setting power %s to can connectors (err %d)\n", |
| 378 | (onoff) ? "on" : "off", err); |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * set leds state according to channel activity |
| 383 | */ |
| 384 | static void pcan_led_timer(unsigned long arg) |
| 385 | { |
| 386 | struct pcan_pccard *card = (struct pcan_pccard *)arg; |
| 387 | struct net_device *netdev; |
| 388 | int i, up_count = 0; |
| 389 | u8 ccr; |
| 390 | |
| 391 | ccr = card->ccr; |
| 392 | for (i = 0; i < card->chan_count; i++) { |
| 393 | /* default is: not configured */ |
| 394 | ccr &= ~PCC_CCR_LED_MASK_CHAN(i); |
| 395 | ccr |= PCC_CCR_LED_ON_CHAN(i); |
| 396 | |
| 397 | netdev = card->channel[i].netdev; |
| 398 | if (!netdev || !(netdev->flags & IFF_UP)) |
| 399 | continue; |
| 400 | |
| 401 | up_count++; |
| 402 | |
| 403 | /* no activity (but configured) */ |
| 404 | ccr &= ~PCC_CCR_LED_MASK_CHAN(i); |
| 405 | ccr |= PCC_CCR_LED_SLOW_CHAN(i); |
| 406 | |
| 407 | /* if bytes counters changed, set fast blinking led */ |
| 408 | if (netdev->stats.rx_bytes != card->channel[i].prev_rx_bytes) { |
| 409 | card->channel[i].prev_rx_bytes = netdev->stats.rx_bytes; |
| 410 | ccr &= ~PCC_CCR_LED_MASK_CHAN(i); |
| 411 | ccr |= PCC_CCR_LED_FAST_CHAN(i); |
| 412 | } |
| 413 | if (netdev->stats.tx_bytes != card->channel[i].prev_tx_bytes) { |
| 414 | card->channel[i].prev_tx_bytes = netdev->stats.tx_bytes; |
| 415 | ccr &= ~PCC_CCR_LED_MASK_CHAN(i); |
| 416 | ccr |= PCC_CCR_LED_FAST_CHAN(i); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | /* write the new leds state */ |
| 421 | pcan_write_reg(card, PCC_CCR, ccr); |
| 422 | |
| 423 | /* restart timer (except if no more configured channels) */ |
| 424 | if (up_count) |
| 425 | mod_timer(&card->led_timer, jiffies + HZ); |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * interrupt service routine |
| 430 | */ |
| 431 | static irqreturn_t pcan_isr(int irq, void *dev_id) |
| 432 | { |
| 433 | struct pcan_pccard *card = dev_id; |
| 434 | int irq_handled; |
| 435 | |
| 436 | /* prevent from infinite loop */ |
| 437 | for (irq_handled = 0; irq_handled < PCC_ISR_MAX_LOOP; irq_handled++) { |
| 438 | /* handle shared interrupt and next loop */ |
| 439 | int nothing_to_handle = 1; |
| 440 | int i; |
| 441 | |
| 442 | /* check interrupt for each channel */ |
| 443 | for (i = 0; i < card->chan_count; i++) { |
| 444 | struct net_device *netdev; |
| 445 | |
| 446 | /* |
| 447 | * check whether the card is present before calling |
| 448 | * sja1000_interrupt() to speed up hotplug detection |
| 449 | */ |
| 450 | if (!pcan_pccard_present(card)) { |
| 451 | /* card unplugged during isr */ |
| 452 | return IRQ_NONE; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * should check whether all or SJA1000_MAX_IRQ |
| 457 | * interrupts have been handled: loop again to be sure. |
| 458 | */ |
| 459 | netdev = card->channel[i].netdev; |
| 460 | if (netdev && |
| 461 | sja1000_interrupt(irq, netdev) == IRQ_HANDLED) |
| 462 | nothing_to_handle = 0; |
| 463 | } |
| 464 | |
| 465 | if (nothing_to_handle) |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | return (irq_handled) ? IRQ_HANDLED : IRQ_NONE; |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * free all resources used by the channels and switch off leds and can power |
| 474 | */ |
| 475 | static void pcan_free_channels(struct pcan_pccard *card) |
| 476 | { |
| 477 | int i; |
| 478 | u8 led_mask = 0; |
| 479 | |
| 480 | for (i = 0; i < card->chan_count; i++) { |
| 481 | struct net_device *netdev; |
| 482 | char name[IFNAMSIZ]; |
| 483 | |
| 484 | led_mask |= PCC_LED(i); |
| 485 | |
| 486 | netdev = card->channel[i].netdev; |
| 487 | if (!netdev) |
| 488 | continue; |
| 489 | |
| 490 | strncpy(name, netdev->name, IFNAMSIZ); |
| 491 | |
| 492 | unregister_sja1000dev(netdev); |
| 493 | |
| 494 | free_sja1000dev(netdev); |
| 495 | |
| 496 | dev_info(&card->pdev->dev, "%s removed\n", name); |
| 497 | } |
| 498 | |
| 499 | /* do it only if device not removed */ |
| 500 | if (pcan_pccard_present(card)) { |
| 501 | pcan_set_leds(card, led_mask, PCC_LED_OFF); |
| 502 | pcan_set_can_power(card, 0); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * check if a CAN controller is present at the specified location |
| 508 | */ |
| 509 | static inline int pcan_channel_present(struct sja1000_priv *priv) |
| 510 | { |
| 511 | /* make sure SJA1000 is in reset mode */ |
| 512 | pcan_write_canreg(priv, SJA1000_MOD, 1); |
| 513 | pcan_write_canreg(priv, SJA1000_CDR, CDR_PELICAN); |
| 514 | |
| 515 | /* read reset-values */ |
| 516 | if (pcan_read_canreg(priv, SJA1000_CDR) == CDR_PELICAN) |
| 517 | return 1; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int pcan_add_channels(struct pcan_pccard *card) |
| 523 | { |
| 524 | struct pcmcia_device *pdev = card->pdev; |
| 525 | int i, err = 0; |
| 526 | u8 ccr = PCC_CCR_INIT; |
| 527 | |
| 528 | /* init common registers (reset channels and leds off) */ |
| 529 | card->ccr = ~ccr; |
| 530 | pcan_write_reg(card, PCC_CCR, ccr); |
| 531 | |
| 532 | /* wait 2ms before unresetting channels */ |
| 533 | mdelay(2); |
| 534 | |
| 535 | ccr &= ~PCC_CCR_RST_ALL; |
| 536 | pcan_write_reg(card, PCC_CCR, ccr); |
| 537 | |
| 538 | /* create one network device per channel detected */ |
| 539 | for (i = 0; i < ARRAY_SIZE(card->channel); i++) { |
| 540 | struct net_device *netdev; |
| 541 | struct sja1000_priv *priv; |
| 542 | |
| 543 | netdev = alloc_sja1000dev(0); |
| 544 | if (!netdev) { |
| 545 | err = -ENOMEM; |
| 546 | break; |
| 547 | } |
| 548 | |
| 549 | /* update linkages */ |
| 550 | priv = netdev_priv(netdev); |
| 551 | priv->priv = card; |
| 552 | SET_NETDEV_DEV(netdev, &pdev->dev); |
| 553 | netdev->dev_id = i; |
| 554 | |
| 555 | priv->irq_flags = IRQF_SHARED; |
| 556 | netdev->irq = pdev->irq; |
| 557 | priv->reg_base = card->ioport_addr + PCC_CHAN_OFF(i); |
| 558 | |
| 559 | /* check if channel is present */ |
| 560 | if (!pcan_channel_present(priv)) { |
| 561 | dev_err(&pdev->dev, "channel %d not present\n", i); |
| 562 | free_sja1000dev(netdev); |
| 563 | continue; |
| 564 | } |
| 565 | |
| 566 | priv->read_reg = pcan_read_canreg; |
| 567 | priv->write_reg = pcan_write_canreg; |
| 568 | priv->can.clock.freq = PCC_CAN_CLOCK; |
| 569 | priv->ocr = PCC_OCR; |
| 570 | priv->cdr = PCC_CDR; |
| 571 | |
| 572 | /* Neither a slave device distributes the clock */ |
| 573 | if (i > 0) |
| 574 | priv->cdr |= CDR_CLK_OFF; |
| 575 | |
| 576 | priv->flags |= SJA1000_CUSTOM_IRQ_HANDLER; |
| 577 | |
| 578 | /* register SJA1000 device */ |
| 579 | err = register_sja1000dev(netdev); |
| 580 | if (err) { |
| 581 | free_sja1000dev(netdev); |
| 582 | continue; |
| 583 | } |
| 584 | |
| 585 | card->channel[i].netdev = netdev; |
| 586 | card->chan_count++; |
| 587 | |
| 588 | /* set corresponding led on in the new ccr */ |
| 589 | ccr &= ~PCC_CCR_LED_OFF_CHAN(i); |
| 590 | |
| 591 | dev_info(&pdev->dev, |
| 592 | "%s on channel %d at 0x%p irq %d\n", |
| 593 | netdev->name, i, priv->reg_base, pdev->irq); |
| 594 | } |
| 595 | |
| 596 | /* write new ccr (change leds state) */ |
| 597 | pcan_write_reg(card, PCC_CCR, ccr); |
| 598 | |
| 599 | return err; |
| 600 | } |
| 601 | |
| 602 | static int pcan_conf_check(struct pcmcia_device *pdev, void *priv_data) |
| 603 | { |
| 604 | pdev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH; |
| 605 | pdev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8; /* only */ |
| 606 | pdev->io_lines = 10; |
| 607 | |
| 608 | /* This reserves IO space but doesn't actually enable it */ |
| 609 | return pcmcia_request_io(pdev); |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * free all resources used by the device |
| 614 | */ |
| 615 | static void pcan_free(struct pcmcia_device *pdev) |
| 616 | { |
| 617 | struct pcan_pccard *card = pdev->priv; |
| 618 | |
| 619 | if (!card) |
| 620 | return; |
| 621 | |
| 622 | free_irq(pdev->irq, card); |
| 623 | pcan_stop_led_timer(card); |
| 624 | |
| 625 | pcan_free_channels(card); |
| 626 | |
| 627 | ioport_unmap(card->ioport_addr); |
| 628 | |
| 629 | kfree(card); |
| 630 | pdev->priv = NULL; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * setup PCMCIA socket and probe for PEAK-System PC-CARD |
| 635 | */ |
| 636 | static int pcan_probe(struct pcmcia_device *pdev) |
| 637 | { |
| 638 | struct pcan_pccard *card; |
| 639 | int err; |
| 640 | |
| 641 | pdev->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO; |
| 642 | |
| 643 | err = pcmcia_loop_config(pdev, pcan_conf_check, NULL); |
| 644 | if (err) { |
| 645 | dev_err(&pdev->dev, "pcmcia_loop_config() error %d\n", err); |
| 646 | goto probe_err_1; |
| 647 | } |
| 648 | |
| 649 | if (!pdev->irq) { |
| 650 | dev_err(&pdev->dev, "no irq assigned\n"); |
| 651 | err = -ENODEV; |
| 652 | goto probe_err_1; |
| 653 | } |
| 654 | |
| 655 | err = pcmcia_enable_device(pdev); |
| 656 | if (err) { |
| 657 | dev_err(&pdev->dev, "pcmcia_enable_device failed err=%d\n", |
| 658 | err); |
| 659 | goto probe_err_1; |
| 660 | } |
| 661 | |
| 662 | card = kzalloc(sizeof(struct pcan_pccard), GFP_KERNEL); |
| 663 | if (!card) { |
| 664 | err = -ENOMEM; |
| 665 | goto probe_err_2; |
| 666 | } |
| 667 | |
| 668 | card->pdev = pdev; |
| 669 | pdev->priv = card; |
| 670 | |
| 671 | /* sja1000 api uses iomem */ |
| 672 | card->ioport_addr = ioport_map(pdev->resource[0]->start, |
| 673 | resource_size(pdev->resource[0])); |
| 674 | if (!card->ioport_addr) { |
| 675 | dev_err(&pdev->dev, "couldn't map io port into io memory\n"); |
| 676 | err = -ENOMEM; |
| 677 | goto probe_err_3; |
| 678 | } |
| 679 | card->fw_major = pcan_read_reg(card, PCC_FW_MAJOR); |
| 680 | card->fw_minor = pcan_read_reg(card, PCC_FW_MINOR); |
| 681 | |
| 682 | /* display board name and firware version */ |
| 683 | dev_info(&pdev->dev, "PEAK-System pcmcia card %s fw %d.%d\n", |
| 684 | pdev->prod_id[1] ? pdev->prod_id[1] : "PCAN-PC Card", |
| 685 | card->fw_major, card->fw_minor); |
| 686 | |
| 687 | /* detect available channels */ |
| 688 | pcan_add_channels(card); |
| 689 | if (!card->chan_count) { |
| 690 | err = -ENOMEM; |
| 691 | goto probe_err_4; |
| 692 | } |
| 693 | |
| 694 | /* init the timer which controls the leds */ |
| 695 | init_timer(&card->led_timer); |
| 696 | card->led_timer.function = pcan_led_timer; |
| 697 | card->led_timer.data = (unsigned long)card; |
| 698 | |
| 699 | /* request the given irq */ |
| 700 | err = request_irq(pdev->irq, &pcan_isr, IRQF_SHARED, PCC_NAME, card); |
| 701 | if (err) { |
| 702 | dev_err(&pdev->dev, "couldn't request irq%d\n", pdev->irq); |
| 703 | goto probe_err_5; |
| 704 | } |
| 705 | |
| 706 | /* power on the connectors */ |
| 707 | pcan_set_can_power(card, 1); |
| 708 | |
| 709 | return 0; |
| 710 | |
| 711 | probe_err_5: |
| 712 | /* unregister can devices from network */ |
| 713 | pcan_free_channels(card); |
| 714 | |
| 715 | probe_err_4: |
| 716 | ioport_unmap(card->ioport_addr); |
| 717 | |
| 718 | probe_err_3: |
| 719 | kfree(card); |
| 720 | pdev->priv = NULL; |
| 721 | |
| 722 | probe_err_2: |
| 723 | pcmcia_disable_device(pdev); |
| 724 | |
| 725 | probe_err_1: |
| 726 | return err; |
| 727 | } |
| 728 | |
| 729 | /* |
| 730 | * release claimed resources |
| 731 | */ |
| 732 | static void pcan_remove(struct pcmcia_device *pdev) |
| 733 | { |
| 734 | pcan_free(pdev); |
| 735 | pcmcia_disable_device(pdev); |
| 736 | } |
| 737 | |
| 738 | static struct pcmcia_driver pcan_driver = { |
| 739 | .name = PCC_NAME, |
| 740 | .probe = pcan_probe, |
| 741 | .remove = pcan_remove, |
| 742 | .id_table = pcan_table, |
| 743 | }; |
| 744 | module_pcmcia_driver(pcan_driver); |