Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/net/irda/sa1100_ir.c |
| 3 | * |
| 4 | * Copyright (C) 2000-2001 Russell King |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | * |
| 10 | * Infra-red driver for the StrongARM SA1100 embedded microprocessor |
| 11 | * |
| 12 | * Note that we don't have to worry about the SA1111's DMA bugs in here, |
| 13 | * so we use the straight forward dma_map_* functions with a null pointer. |
| 14 | * |
| 15 | * This driver takes one kernel command line parameter, sa1100ir=, with |
| 16 | * the following options: |
| 17 | * max_rate:baudrate - set the maximum baud rate |
| 18 | * power_leve:level - set the transmitter power level |
| 19 | * tx_lpm:0|1 - set transmit low power mode |
| 20 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/moduleparam.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/rtnetlink.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/delay.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 31 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/dma-mapping.h> |
| 33 | |
| 34 | #include <net/irda/irda.h> |
| 35 | #include <net/irda/wrapper.h> |
| 36 | #include <net/irda/irda_device.h> |
| 37 | |
| 38 | #include <asm/irq.h> |
| 39 | #include <asm/dma.h> |
| 40 | #include <asm/hardware.h> |
| 41 | #include <asm/mach/irda.h> |
| 42 | |
| 43 | static int power_level = 3; |
| 44 | static int tx_lpm; |
| 45 | static int max_rate = 4000000; |
| 46 | |
| 47 | struct sa1100_irda { |
| 48 | unsigned char hscr0; |
| 49 | unsigned char utcr4; |
| 50 | unsigned char power; |
| 51 | unsigned char open; |
| 52 | |
| 53 | int speed; |
| 54 | int newspeed; |
| 55 | |
| 56 | struct sk_buff *txskb; |
| 57 | struct sk_buff *rxskb; |
| 58 | dma_addr_t txbuf_dma; |
| 59 | dma_addr_t rxbuf_dma; |
| 60 | dma_regs_t *txdma; |
| 61 | dma_regs_t *rxdma; |
| 62 | |
| 63 | struct net_device_stats stats; |
| 64 | struct device *dev; |
| 65 | struct irda_platform_data *pdata; |
| 66 | struct irlap_cb *irlap; |
| 67 | struct qos_info qos; |
| 68 | |
| 69 | iobuff_t tx_buff; |
| 70 | iobuff_t rx_buff; |
| 71 | }; |
| 72 | |
| 73 | #define IS_FIR(si) ((si)->speed >= 4000000) |
| 74 | |
| 75 | #define HPSIR_MAX_RXLEN 2047 |
| 76 | |
| 77 | /* |
| 78 | * Allocate and map the receive buffer, unless it is already allocated. |
| 79 | */ |
| 80 | static int sa1100_irda_rx_alloc(struct sa1100_irda *si) |
| 81 | { |
| 82 | if (si->rxskb) |
| 83 | return 0; |
| 84 | |
| 85 | si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC); |
| 86 | |
| 87 | if (!si->rxskb) { |
| 88 | printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n"); |
| 89 | return -ENOMEM; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Align any IP headers that may be contained |
| 94 | * within the frame. |
| 95 | */ |
| 96 | skb_reserve(si->rxskb, 1); |
| 97 | |
| 98 | si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data, |
| 99 | HPSIR_MAX_RXLEN, |
| 100 | DMA_FROM_DEVICE); |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * We want to get here as soon as possible, and get the receiver setup. |
| 106 | * We use the existing buffer. |
| 107 | */ |
| 108 | static void sa1100_irda_rx_dma_start(struct sa1100_irda *si) |
| 109 | { |
| 110 | if (!si->rxskb) { |
| 111 | printk(KERN_ERR "sa1100_ir: rx buffer went missing\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * First empty receive FIFO |
| 117 | */ |
| 118 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP; |
| 119 | |
| 120 | /* |
| 121 | * Enable the DMA, receiver and receive interrupt. |
| 122 | */ |
| 123 | sa1100_clear_dma(si->rxdma); |
| 124 | sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN); |
| 125 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * Set the IrDA communications speed. |
| 130 | */ |
| 131 | static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed) |
| 132 | { |
| 133 | unsigned long flags; |
| 134 | int brd, ret = -EINVAL; |
| 135 | |
| 136 | switch (speed) { |
| 137 | case 9600: case 19200: case 38400: |
| 138 | case 57600: case 115200: |
| 139 | brd = 3686400 / (16 * speed) - 1; |
| 140 | |
| 141 | /* |
| 142 | * Stop the receive DMA. |
| 143 | */ |
| 144 | if (IS_FIR(si)) |
| 145 | sa1100_stop_dma(si->rxdma); |
| 146 | |
| 147 | local_irq_save(flags); |
| 148 | |
| 149 | Ser2UTCR3 = 0; |
| 150 | Ser2HSCR0 = HSCR0_UART; |
| 151 | |
| 152 | Ser2UTCR1 = brd >> 8; |
| 153 | Ser2UTCR2 = brd; |
| 154 | |
| 155 | /* |
| 156 | * Clear status register |
| 157 | */ |
| 158 | Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID; |
| 159 | Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE; |
| 160 | |
| 161 | if (si->pdata->set_speed) |
| 162 | si->pdata->set_speed(si->dev, speed); |
| 163 | |
| 164 | si->speed = speed; |
| 165 | |
| 166 | local_irq_restore(flags); |
| 167 | ret = 0; |
| 168 | break; |
| 169 | |
| 170 | case 4000000: |
| 171 | local_irq_save(flags); |
| 172 | |
| 173 | si->hscr0 = 0; |
| 174 | |
| 175 | Ser2HSSR0 = 0xff; |
| 176 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP; |
| 177 | Ser2UTCR3 = 0; |
| 178 | |
| 179 | si->speed = speed; |
| 180 | |
| 181 | if (si->pdata->set_speed) |
| 182 | si->pdata->set_speed(si->dev, speed); |
| 183 | |
| 184 | sa1100_irda_rx_alloc(si); |
| 185 | sa1100_irda_rx_dma_start(si); |
| 186 | |
| 187 | local_irq_restore(flags); |
| 188 | |
| 189 | break; |
| 190 | |
| 191 | default: |
| 192 | break; |
| 193 | } |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Control the power state of the IrDA transmitter. |
| 200 | * State: |
| 201 | * 0 - off |
| 202 | * 1 - short range, lowest power |
| 203 | * 2 - medium range, medium power |
| 204 | * 3 - maximum range, high power |
| 205 | * |
| 206 | * Currently, only assabet is known to support this. |
| 207 | */ |
| 208 | static int |
| 209 | __sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state) |
| 210 | { |
| 211 | int ret = 0; |
| 212 | if (si->pdata->set_power) |
| 213 | ret = si->pdata->set_power(si->dev, state); |
| 214 | return ret; |
| 215 | } |
| 216 | |
| 217 | static inline int |
| 218 | sa1100_set_power(struct sa1100_irda *si, unsigned int state) |
| 219 | { |
| 220 | int ret; |
| 221 | |
| 222 | ret = __sa1100_irda_set_power(si, state); |
| 223 | if (ret == 0) |
| 224 | si->power = state; |
| 225 | |
| 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int sa1100_irda_startup(struct sa1100_irda *si) |
| 230 | { |
| 231 | int ret; |
| 232 | |
| 233 | /* |
| 234 | * Ensure that the ports for this device are setup correctly. |
| 235 | */ |
| 236 | if (si->pdata->startup) |
| 237 | si->pdata->startup(si->dev); |
| 238 | |
| 239 | /* |
| 240 | * Configure PPC for IRDA - we want to drive TXD2 low. |
| 241 | * We also want to drive this pin low during sleep. |
| 242 | */ |
| 243 | PPSR &= ~PPC_TXD2; |
| 244 | PSDR &= ~PPC_TXD2; |
| 245 | PPDR |= PPC_TXD2; |
| 246 | |
| 247 | /* |
| 248 | * Enable HP-SIR modulation, and ensure that the port is disabled. |
| 249 | */ |
| 250 | Ser2UTCR3 = 0; |
| 251 | Ser2HSCR0 = HSCR0_UART; |
| 252 | Ser2UTCR4 = si->utcr4; |
| 253 | Ser2UTCR0 = UTCR0_8BitData; |
| 254 | Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL; |
| 255 | |
| 256 | /* |
| 257 | * Clear status register |
| 258 | */ |
| 259 | Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID; |
| 260 | |
| 261 | ret = sa1100_irda_set_speed(si, si->speed = 9600); |
| 262 | if (ret) { |
| 263 | Ser2UTCR3 = 0; |
| 264 | Ser2HSCR0 = 0; |
| 265 | |
| 266 | if (si->pdata->shutdown) |
| 267 | si->pdata->shutdown(si->dev); |
| 268 | } |
| 269 | |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | static void sa1100_irda_shutdown(struct sa1100_irda *si) |
| 274 | { |
| 275 | /* |
| 276 | * Stop all DMA activity. |
| 277 | */ |
| 278 | sa1100_stop_dma(si->rxdma); |
| 279 | sa1100_stop_dma(si->txdma); |
| 280 | |
| 281 | /* Disable the port. */ |
| 282 | Ser2UTCR3 = 0; |
| 283 | Ser2HSCR0 = 0; |
| 284 | |
| 285 | if (si->pdata->shutdown) |
| 286 | si->pdata->shutdown(si->dev); |
| 287 | } |
| 288 | |
| 289 | #ifdef CONFIG_PM |
| 290 | /* |
| 291 | * Suspend the IrDA interface. |
| 292 | */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 293 | static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 295 | struct net_device *dev = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | struct sa1100_irda *si; |
| 297 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 298 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | return 0; |
| 300 | |
| 301 | si = dev->priv; |
| 302 | if (si->open) { |
| 303 | /* |
| 304 | * Stop the transmit queue |
| 305 | */ |
| 306 | netif_device_detach(dev); |
| 307 | disable_irq(dev->irq); |
| 308 | sa1100_irda_shutdown(si); |
| 309 | __sa1100_irda_set_power(si, 0); |
| 310 | } |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /* |
| 316 | * Resume the IrDA interface. |
| 317 | */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 318 | static int sa1100_irda_resume(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 320 | struct net_device *dev = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | struct sa1100_irda *si; |
| 322 | |
Russell King | 9480e30 | 2005-10-28 09:52:56 -0700 | [diff] [blame] | 323 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | return 0; |
| 325 | |
| 326 | si = dev->priv; |
| 327 | if (si->open) { |
| 328 | /* |
| 329 | * If we missed a speed change, initialise at the new speed |
| 330 | * directly. It is debatable whether this is actually |
| 331 | * required, but in the interests of continuing from where |
| 332 | * we left off it is desireable. The converse argument is |
| 333 | * that we should re-negotiate at 9600 baud again. |
| 334 | */ |
| 335 | if (si->newspeed) { |
| 336 | si->speed = si->newspeed; |
| 337 | si->newspeed = 0; |
| 338 | } |
| 339 | |
| 340 | sa1100_irda_startup(si); |
| 341 | __sa1100_irda_set_power(si, si->power); |
| 342 | enable_irq(dev->irq); |
| 343 | |
| 344 | /* |
| 345 | * This automatically wakes up the queue |
| 346 | */ |
| 347 | netif_device_attach(dev); |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | #else |
| 353 | #define sa1100_irda_suspend NULL |
| 354 | #define sa1100_irda_resume NULL |
| 355 | #endif |
| 356 | |
| 357 | /* |
| 358 | * HP-SIR format interrupt service routines. |
| 359 | */ |
| 360 | static void sa1100_irda_hpsir_irq(struct net_device *dev) |
| 361 | { |
| 362 | struct sa1100_irda *si = dev->priv; |
| 363 | int status; |
| 364 | |
| 365 | status = Ser2UTSR0; |
| 366 | |
| 367 | /* |
| 368 | * Deal with any receive errors first. The bytes in error may be |
| 369 | * the only bytes in the receive FIFO, so we do this first. |
| 370 | */ |
| 371 | while (status & UTSR0_EIF) { |
| 372 | int stat, data; |
| 373 | |
| 374 | stat = Ser2UTSR1; |
| 375 | data = Ser2UTDR; |
| 376 | |
| 377 | if (stat & (UTSR1_FRE | UTSR1_ROR)) { |
| 378 | si->stats.rx_errors++; |
| 379 | if (stat & UTSR1_FRE) |
| 380 | si->stats.rx_frame_errors++; |
| 381 | if (stat & UTSR1_ROR) |
| 382 | si->stats.rx_fifo_errors++; |
| 383 | } else |
| 384 | async_unwrap_char(dev, &si->stats, &si->rx_buff, data); |
| 385 | |
| 386 | status = Ser2UTSR0; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * We must clear certain bits. |
| 391 | */ |
| 392 | Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB); |
| 393 | |
| 394 | if (status & UTSR0_RFS) { |
| 395 | /* |
| 396 | * There are at least 4 bytes in the FIFO. Read 3 bytes |
| 397 | * and leave the rest to the block below. |
| 398 | */ |
| 399 | async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR); |
| 400 | async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR); |
| 401 | async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR); |
| 402 | } |
| 403 | |
| 404 | if (status & (UTSR0_RFS | UTSR0_RID)) { |
| 405 | /* |
| 406 | * Fifo contains more than 1 character. |
| 407 | */ |
| 408 | do { |
| 409 | async_unwrap_char(dev, &si->stats, &si->rx_buff, |
| 410 | Ser2UTDR); |
| 411 | } while (Ser2UTSR1 & UTSR1_RNE); |
| 412 | |
| 413 | dev->last_rx = jiffies; |
| 414 | } |
| 415 | |
| 416 | if (status & UTSR0_TFS && si->tx_buff.len) { |
| 417 | /* |
| 418 | * Transmitter FIFO is not full |
| 419 | */ |
| 420 | do { |
| 421 | Ser2UTDR = *si->tx_buff.data++; |
| 422 | si->tx_buff.len -= 1; |
| 423 | } while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len); |
| 424 | |
| 425 | if (si->tx_buff.len == 0) { |
| 426 | si->stats.tx_packets++; |
| 427 | si->stats.tx_bytes += si->tx_buff.data - |
| 428 | si->tx_buff.head; |
| 429 | |
| 430 | /* |
| 431 | * We need to ensure that the transmitter has |
| 432 | * finished. |
| 433 | */ |
| 434 | do |
| 435 | rmb(); |
| 436 | while (Ser2UTSR1 & UTSR1_TBY); |
| 437 | |
| 438 | /* |
| 439 | * Ok, we've finished transmitting. Now enable |
| 440 | * the receiver. Sometimes we get a receive IRQ |
| 441 | * immediately after a transmit... |
| 442 | */ |
| 443 | Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID; |
| 444 | Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE; |
| 445 | |
| 446 | if (si->newspeed) { |
| 447 | sa1100_irda_set_speed(si, si->newspeed); |
| 448 | si->newspeed = 0; |
| 449 | } |
| 450 | |
| 451 | /* I'm hungry! */ |
| 452 | netif_wake_queue(dev); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev) |
| 458 | { |
| 459 | struct sk_buff *skb = si->rxskb; |
| 460 | dma_addr_t dma_addr; |
| 461 | unsigned int len, stat, data; |
| 462 | |
| 463 | if (!skb) { |
| 464 | printk(KERN_ERR "sa1100_ir: SKB is NULL!\n"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Get the current data position. |
| 470 | */ |
| 471 | dma_addr = sa1100_get_dma_pos(si->rxdma); |
| 472 | len = dma_addr - si->rxbuf_dma; |
| 473 | if (len > HPSIR_MAX_RXLEN) |
| 474 | len = HPSIR_MAX_RXLEN; |
| 475 | dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE); |
| 476 | |
| 477 | do { |
| 478 | /* |
| 479 | * Read Status, and then Data. |
| 480 | */ |
| 481 | stat = Ser2HSSR1; |
| 482 | rmb(); |
| 483 | data = Ser2HSDR; |
| 484 | |
| 485 | if (stat & (HSSR1_CRE | HSSR1_ROR)) { |
| 486 | si->stats.rx_errors++; |
| 487 | if (stat & HSSR1_CRE) |
| 488 | si->stats.rx_crc_errors++; |
| 489 | if (stat & HSSR1_ROR) |
| 490 | si->stats.rx_frame_errors++; |
| 491 | } else |
| 492 | skb->data[len++] = data; |
| 493 | |
| 494 | /* |
| 495 | * If we hit the end of frame, there's |
| 496 | * no point in continuing. |
| 497 | */ |
| 498 | if (stat & HSSR1_EOF) |
| 499 | break; |
| 500 | } while (Ser2HSSR0 & HSSR0_EIF); |
| 501 | |
| 502 | if (stat & HSSR1_EOF) { |
| 503 | si->rxskb = NULL; |
| 504 | |
| 505 | skb_put(skb, len); |
| 506 | skb->dev = dev; |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 507 | skb_reset_mac_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | skb->protocol = htons(ETH_P_IRDA); |
| 509 | si->stats.rx_packets++; |
| 510 | si->stats.rx_bytes += len; |
| 511 | |
| 512 | /* |
| 513 | * Before we pass the buffer up, allocate a new one. |
| 514 | */ |
| 515 | sa1100_irda_rx_alloc(si); |
| 516 | |
| 517 | netif_rx(skb); |
| 518 | dev->last_rx = jiffies; |
| 519 | } else { |
| 520 | /* |
| 521 | * Remap the buffer. |
| 522 | */ |
| 523 | si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data, |
| 524 | HPSIR_MAX_RXLEN, |
| 525 | DMA_FROM_DEVICE); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | /* |
| 530 | * FIR format interrupt service routine. We only have to |
| 531 | * handle RX events; transmit events go via the TX DMA handler. |
| 532 | * |
| 533 | * No matter what, we disable RX, process, and the restart RX. |
| 534 | */ |
| 535 | static void sa1100_irda_fir_irq(struct net_device *dev) |
| 536 | { |
| 537 | struct sa1100_irda *si = dev->priv; |
| 538 | |
| 539 | /* |
| 540 | * Stop RX DMA |
| 541 | */ |
| 542 | sa1100_stop_dma(si->rxdma); |
| 543 | |
| 544 | /* |
| 545 | * Framing error - we throw away the packet completely. |
| 546 | * Clearing RXE flushes the error conditions and data |
| 547 | * from the fifo. |
| 548 | */ |
| 549 | if (Ser2HSSR0 & (HSSR0_FRE | HSSR0_RAB)) { |
| 550 | si->stats.rx_errors++; |
| 551 | |
| 552 | if (Ser2HSSR0 & HSSR0_FRE) |
| 553 | si->stats.rx_frame_errors++; |
| 554 | |
| 555 | /* |
| 556 | * Clear out the DMA... |
| 557 | */ |
| 558 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP; |
| 559 | |
| 560 | /* |
| 561 | * Clear selected status bits now, so we |
| 562 | * don't miss them next time around. |
| 563 | */ |
| 564 | Ser2HSSR0 = HSSR0_FRE | HSSR0_RAB; |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * Deal with any receive errors. The any of the lowest |
| 569 | * 8 bytes in the FIFO may contain an error. We must read |
| 570 | * them one by one. The "error" could even be the end of |
| 571 | * packet! |
| 572 | */ |
| 573 | if (Ser2HSSR0 & HSSR0_EIF) |
| 574 | sa1100_irda_fir_error(si, dev); |
| 575 | |
| 576 | /* |
| 577 | * No matter what happens, we must restart reception. |
| 578 | */ |
| 579 | sa1100_irda_rx_dma_start(si); |
| 580 | } |
| 581 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 582 | static irqreturn_t sa1100_irda_irq(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | { |
| 584 | struct net_device *dev = dev_id; |
| 585 | if (IS_FIR(((struct sa1100_irda *)dev->priv))) |
| 586 | sa1100_irda_fir_irq(dev); |
| 587 | else |
| 588 | sa1100_irda_hpsir_irq(dev); |
| 589 | return IRQ_HANDLED; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * TX DMA completion handler. |
| 594 | */ |
| 595 | static void sa1100_irda_txdma_irq(void *id) |
| 596 | { |
| 597 | struct net_device *dev = id; |
| 598 | struct sa1100_irda *si = dev->priv; |
| 599 | struct sk_buff *skb = si->txskb; |
| 600 | |
| 601 | si->txskb = NULL; |
| 602 | |
| 603 | /* |
| 604 | * Wait for the transmission to complete. Unfortunately, |
| 605 | * the hardware doesn't give us an interrupt to indicate |
| 606 | * "end of frame". |
| 607 | */ |
| 608 | do |
| 609 | rmb(); |
| 610 | while (!(Ser2HSSR0 & HSSR0_TUR) || Ser2HSSR1 & HSSR1_TBY); |
| 611 | |
| 612 | /* |
| 613 | * Clear the transmit underrun bit. |
| 614 | */ |
| 615 | Ser2HSSR0 = HSSR0_TUR; |
| 616 | |
| 617 | /* |
| 618 | * Do we need to change speed? Note that we're lazy |
| 619 | * here - we don't free the old rxskb. We don't need |
| 620 | * to allocate a buffer either. |
| 621 | */ |
| 622 | if (si->newspeed) { |
| 623 | sa1100_irda_set_speed(si, si->newspeed); |
| 624 | si->newspeed = 0; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Start reception. This disables the transmitter for |
| 629 | * us. This will be using the existing RX buffer. |
| 630 | */ |
| 631 | sa1100_irda_rx_dma_start(si); |
| 632 | |
| 633 | /* |
| 634 | * Account and free the packet. |
| 635 | */ |
| 636 | if (skb) { |
| 637 | dma_unmap_single(si->dev, si->txbuf_dma, skb->len, DMA_TO_DEVICE); |
| 638 | si->stats.tx_packets ++; |
| 639 | si->stats.tx_bytes += skb->len; |
| 640 | dev_kfree_skb_irq(skb); |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Make sure that the TX queue is available for sending |
| 645 | * (for retries). TX has priority over RX at all times. |
| 646 | */ |
| 647 | netif_wake_queue(dev); |
| 648 | } |
| 649 | |
| 650 | static int sa1100_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev) |
| 651 | { |
| 652 | struct sa1100_irda *si = dev->priv; |
| 653 | int speed = irda_get_next_speed(skb); |
| 654 | |
| 655 | /* |
| 656 | * Does this packet contain a request to change the interface |
| 657 | * speed? If so, remember it until we complete the transmission |
| 658 | * of this frame. |
| 659 | */ |
| 660 | if (speed != si->speed && speed != -1) |
| 661 | si->newspeed = speed; |
| 662 | |
| 663 | /* |
| 664 | * If this is an empty frame, we can bypass a lot. |
| 665 | */ |
| 666 | if (skb->len == 0) { |
| 667 | if (si->newspeed) { |
| 668 | si->newspeed = 0; |
| 669 | sa1100_irda_set_speed(si, speed); |
| 670 | } |
| 671 | dev_kfree_skb(skb); |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | if (!IS_FIR(si)) { |
| 676 | netif_stop_queue(dev); |
| 677 | |
| 678 | si->tx_buff.data = si->tx_buff.head; |
| 679 | si->tx_buff.len = async_wrap_skb(skb, si->tx_buff.data, |
| 680 | si->tx_buff.truesize); |
| 681 | |
| 682 | /* |
| 683 | * Set the transmit interrupt enable. This will fire |
| 684 | * off an interrupt immediately. Note that we disable |
| 685 | * the receiver so we won't get spurious characteres |
| 686 | * received. |
| 687 | */ |
| 688 | Ser2UTCR3 = UTCR3_TIE | UTCR3_TXE; |
| 689 | |
| 690 | dev_kfree_skb(skb); |
| 691 | } else { |
| 692 | int mtt = irda_get_mtt(skb); |
| 693 | |
| 694 | /* |
| 695 | * We must not be transmitting... |
| 696 | */ |
Eric Sesterhenn | 5d9428d | 2006-04-02 13:52:48 +0200 | [diff] [blame] | 697 | BUG_ON(si->txskb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | netif_stop_queue(dev); |
| 700 | |
| 701 | si->txskb = skb; |
| 702 | si->txbuf_dma = dma_map_single(si->dev, skb->data, |
| 703 | skb->len, DMA_TO_DEVICE); |
| 704 | |
| 705 | sa1100_start_dma(si->txdma, si->txbuf_dma, skb->len); |
| 706 | |
| 707 | /* |
| 708 | * If we have a mean turn-around time, impose the specified |
| 709 | * specified delay. We could shorten this by timing from |
| 710 | * the point we received the packet. |
| 711 | */ |
| 712 | if (mtt) |
| 713 | udelay(mtt); |
| 714 | |
| 715 | Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_TXE; |
| 716 | } |
| 717 | |
| 718 | dev->trans_start = jiffies; |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | static int |
| 724 | sa1100_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd) |
| 725 | { |
| 726 | struct if_irda_req *rq = (struct if_irda_req *)ifreq; |
| 727 | struct sa1100_irda *si = dev->priv; |
| 728 | int ret = -EOPNOTSUPP; |
| 729 | |
| 730 | switch (cmd) { |
| 731 | case SIOCSBANDWIDTH: |
| 732 | if (capable(CAP_NET_ADMIN)) { |
| 733 | /* |
| 734 | * We are unable to set the speed if the |
| 735 | * device is not running. |
| 736 | */ |
| 737 | if (si->open) { |
| 738 | ret = sa1100_irda_set_speed(si, |
| 739 | rq->ifr_baudrate); |
| 740 | } else { |
| 741 | printk("sa1100_irda_ioctl: SIOCSBANDWIDTH: !netif_running\n"); |
| 742 | ret = 0; |
| 743 | } |
| 744 | } |
| 745 | break; |
| 746 | |
| 747 | case SIOCSMEDIABUSY: |
| 748 | ret = -EPERM; |
| 749 | if (capable(CAP_NET_ADMIN)) { |
| 750 | irda_device_set_media_busy(dev, TRUE); |
| 751 | ret = 0; |
| 752 | } |
| 753 | break; |
| 754 | |
| 755 | case SIOCGRECEIVING: |
| 756 | rq->ifr_receiving = IS_FIR(si) ? 0 |
| 757 | : si->rx_buff.state != OUTSIDE_FRAME; |
| 758 | break; |
| 759 | |
| 760 | default: |
| 761 | break; |
| 762 | } |
| 763 | |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static struct net_device_stats *sa1100_irda_stats(struct net_device *dev) |
| 768 | { |
| 769 | struct sa1100_irda *si = dev->priv; |
| 770 | return &si->stats; |
| 771 | } |
| 772 | |
| 773 | static int sa1100_irda_start(struct net_device *dev) |
| 774 | { |
| 775 | struct sa1100_irda *si = dev->priv; |
| 776 | int err; |
| 777 | |
| 778 | si->speed = 9600; |
| 779 | |
| 780 | err = request_irq(dev->irq, sa1100_irda_irq, 0, dev->name, dev); |
| 781 | if (err) |
| 782 | goto err_irq; |
| 783 | |
| 784 | err = sa1100_request_dma(DMA_Ser2HSSPRd, "IrDA receive", |
| 785 | NULL, NULL, &si->rxdma); |
| 786 | if (err) |
| 787 | goto err_rx_dma; |
| 788 | |
| 789 | err = sa1100_request_dma(DMA_Ser2HSSPWr, "IrDA transmit", |
| 790 | sa1100_irda_txdma_irq, dev, &si->txdma); |
| 791 | if (err) |
| 792 | goto err_tx_dma; |
| 793 | |
| 794 | /* |
| 795 | * The interrupt must remain disabled for now. |
| 796 | */ |
| 797 | disable_irq(dev->irq); |
| 798 | |
| 799 | /* |
| 800 | * Setup the serial port for the specified speed. |
| 801 | */ |
| 802 | err = sa1100_irda_startup(si); |
| 803 | if (err) |
| 804 | goto err_startup; |
| 805 | |
| 806 | /* |
| 807 | * Open a new IrLAP layer instance. |
| 808 | */ |
| 809 | si->irlap = irlap_open(dev, &si->qos, "sa1100"); |
| 810 | err = -ENOMEM; |
| 811 | if (!si->irlap) |
| 812 | goto err_irlap; |
| 813 | |
| 814 | /* |
| 815 | * Now enable the interrupt and start the queue |
| 816 | */ |
| 817 | si->open = 1; |
| 818 | sa1100_set_power(si, power_level); /* low power mode */ |
| 819 | enable_irq(dev->irq); |
| 820 | netif_start_queue(dev); |
| 821 | return 0; |
| 822 | |
| 823 | err_irlap: |
| 824 | si->open = 0; |
| 825 | sa1100_irda_shutdown(si); |
| 826 | err_startup: |
| 827 | sa1100_free_dma(si->txdma); |
| 828 | err_tx_dma: |
| 829 | sa1100_free_dma(si->rxdma); |
| 830 | err_rx_dma: |
| 831 | free_irq(dev->irq, dev); |
| 832 | err_irq: |
| 833 | return err; |
| 834 | } |
| 835 | |
| 836 | static int sa1100_irda_stop(struct net_device *dev) |
| 837 | { |
| 838 | struct sa1100_irda *si = dev->priv; |
| 839 | |
| 840 | disable_irq(dev->irq); |
| 841 | sa1100_irda_shutdown(si); |
| 842 | |
| 843 | /* |
| 844 | * If we have been doing DMA receive, make sure we |
| 845 | * tidy that up cleanly. |
| 846 | */ |
| 847 | if (si->rxskb) { |
| 848 | dma_unmap_single(si->dev, si->rxbuf_dma, HPSIR_MAX_RXLEN, |
| 849 | DMA_FROM_DEVICE); |
| 850 | dev_kfree_skb(si->rxskb); |
| 851 | si->rxskb = NULL; |
| 852 | } |
| 853 | |
| 854 | /* Stop IrLAP */ |
| 855 | if (si->irlap) { |
| 856 | irlap_close(si->irlap); |
| 857 | si->irlap = NULL; |
| 858 | } |
| 859 | |
| 860 | netif_stop_queue(dev); |
| 861 | si->open = 0; |
| 862 | |
| 863 | /* |
| 864 | * Free resources |
| 865 | */ |
| 866 | sa1100_free_dma(si->txdma); |
| 867 | sa1100_free_dma(si->rxdma); |
| 868 | free_irq(dev->irq, dev); |
| 869 | |
| 870 | sa1100_set_power(si, 0); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int sa1100_irda_init_iobuf(iobuff_t *io, int size) |
| 876 | { |
| 877 | io->head = kmalloc(size, GFP_KERNEL | GFP_DMA); |
| 878 | if (io->head != NULL) { |
| 879 | io->truesize = size; |
| 880 | io->in_frame = FALSE; |
| 881 | io->state = OUTSIDE_FRAME; |
| 882 | io->data = io->head; |
| 883 | } |
| 884 | return io->head ? 0 : -ENOMEM; |
| 885 | } |
| 886 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 887 | static int sa1100_irda_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | struct net_device *dev; |
| 890 | struct sa1100_irda *si; |
| 891 | unsigned int baudrate_mask; |
| 892 | int err; |
| 893 | |
| 894 | if (!pdev->dev.platform_data) |
| 895 | return -EINVAL; |
| 896 | |
| 897 | err = request_mem_region(__PREG(Ser2UTCR0), 0x24, "IrDA") ? 0 : -EBUSY; |
| 898 | if (err) |
| 899 | goto err_mem_1; |
| 900 | err = request_mem_region(__PREG(Ser2HSCR0), 0x1c, "IrDA") ? 0 : -EBUSY; |
| 901 | if (err) |
| 902 | goto err_mem_2; |
| 903 | err = request_mem_region(__PREG(Ser2HSCR2), 0x04, "IrDA") ? 0 : -EBUSY; |
| 904 | if (err) |
| 905 | goto err_mem_3; |
| 906 | |
| 907 | dev = alloc_irdadev(sizeof(struct sa1100_irda)); |
| 908 | if (!dev) |
| 909 | goto err_mem_4; |
| 910 | |
| 911 | si = dev->priv; |
| 912 | si->dev = &pdev->dev; |
| 913 | si->pdata = pdev->dev.platform_data; |
| 914 | |
| 915 | /* |
| 916 | * Initialise the HP-SIR buffers |
| 917 | */ |
| 918 | err = sa1100_irda_init_iobuf(&si->rx_buff, 14384); |
| 919 | if (err) |
| 920 | goto err_mem_5; |
| 921 | err = sa1100_irda_init_iobuf(&si->tx_buff, 4000); |
| 922 | if (err) |
| 923 | goto err_mem_5; |
| 924 | |
| 925 | dev->hard_start_xmit = sa1100_irda_hard_xmit; |
| 926 | dev->open = sa1100_irda_start; |
| 927 | dev->stop = sa1100_irda_stop; |
| 928 | dev->do_ioctl = sa1100_irda_ioctl; |
| 929 | dev->get_stats = sa1100_irda_stats; |
| 930 | dev->irq = IRQ_Ser2ICP; |
| 931 | |
| 932 | irda_init_max_qos_capabilies(&si->qos); |
| 933 | |
| 934 | /* |
| 935 | * We support original IRDA up to 115k2. (we don't currently |
| 936 | * support 4Mbps). Min Turn Time set to 1ms or greater. |
| 937 | */ |
| 938 | baudrate_mask = IR_9600; |
| 939 | |
| 940 | switch (max_rate) { |
| 941 | case 4000000: baudrate_mask |= IR_4000000 << 8; |
| 942 | case 115200: baudrate_mask |= IR_115200; |
| 943 | case 57600: baudrate_mask |= IR_57600; |
| 944 | case 38400: baudrate_mask |= IR_38400; |
| 945 | case 19200: baudrate_mask |= IR_19200; |
| 946 | } |
| 947 | |
| 948 | si->qos.baud_rate.bits &= baudrate_mask; |
| 949 | si->qos.min_turn_time.bits = 7; |
| 950 | |
| 951 | irda_qos_bits_to_value(&si->qos); |
| 952 | |
| 953 | si->utcr4 = UTCR4_HPSIR; |
| 954 | if (tx_lpm) |
| 955 | si->utcr4 |= UTCR4_Z1_6us; |
| 956 | |
| 957 | /* |
| 958 | * Initially enable HP-SIR modulation, and ensure that the port |
| 959 | * is disabled. |
| 960 | */ |
| 961 | Ser2UTCR3 = 0; |
| 962 | Ser2UTCR4 = si->utcr4; |
| 963 | Ser2HSCR0 = HSCR0_UART; |
| 964 | |
| 965 | err = register_netdev(dev); |
| 966 | if (err == 0) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 967 | platform_set_drvdata(pdev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
| 969 | if (err) { |
| 970 | err_mem_5: |
| 971 | kfree(si->tx_buff.head); |
| 972 | kfree(si->rx_buff.head); |
| 973 | free_netdev(dev); |
| 974 | err_mem_4: |
| 975 | release_mem_region(__PREG(Ser2HSCR2), 0x04); |
| 976 | err_mem_3: |
| 977 | release_mem_region(__PREG(Ser2HSCR0), 0x1c); |
| 978 | err_mem_2: |
| 979 | release_mem_region(__PREG(Ser2UTCR0), 0x24); |
| 980 | } |
| 981 | err_mem_1: |
| 982 | return err; |
| 983 | } |
| 984 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 985 | static int sa1100_irda_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 987 | struct net_device *dev = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
| 989 | if (dev) { |
| 990 | struct sa1100_irda *si = dev->priv; |
| 991 | unregister_netdev(dev); |
| 992 | kfree(si->tx_buff.head); |
| 993 | kfree(si->rx_buff.head); |
| 994 | free_netdev(dev); |
| 995 | } |
| 996 | |
| 997 | release_mem_region(__PREG(Ser2HSCR2), 0x04); |
| 998 | release_mem_region(__PREG(Ser2HSCR0), 0x1c); |
| 999 | release_mem_region(__PREG(Ser2UTCR0), 0x24); |
| 1000 | |
| 1001 | return 0; |
| 1002 | } |
| 1003 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1004 | static struct platform_driver sa1100ir_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | .probe = sa1100_irda_probe, |
| 1006 | .remove = sa1100_irda_remove, |
| 1007 | .suspend = sa1100_irda_suspend, |
| 1008 | .resume = sa1100_irda_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1009 | .driver = { |
| 1010 | .name = "sa11x0-ir", |
| 1011 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | }; |
| 1013 | |
| 1014 | static int __init sa1100_irda_init(void) |
| 1015 | { |
| 1016 | /* |
| 1017 | * Limit power level a sensible range. |
| 1018 | */ |
| 1019 | if (power_level < 1) |
| 1020 | power_level = 1; |
| 1021 | if (power_level > 3) |
| 1022 | power_level = 3; |
| 1023 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1024 | return platform_driver_register(&sa1100ir_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static void __exit sa1100_irda_exit(void) |
| 1028 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1029 | platform_driver_unregister(&sa1100ir_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | module_init(sa1100_irda_init); |
| 1033 | module_exit(sa1100_irda_exit); |
| 1034 | module_param(power_level, int, 0); |
| 1035 | module_param(tx_lpm, int, 0); |
| 1036 | module_param(max_rate, int, 0); |
| 1037 | |
| 1038 | MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>"); |
| 1039 | MODULE_DESCRIPTION("StrongARM SA1100 IrDA driver"); |
| 1040 | MODULE_LICENSE("GPL"); |
| 1041 | MODULE_PARM_DESC(power_level, "IrDA power level, 1 (low) to 3 (high)"); |
| 1042 | MODULE_PARM_DESC(tx_lpm, "Enable transmitter low power (1.6us) mode"); |
| 1043 | MODULE_PARM_DESC(max_rate, "Maximum baud rate (4000000, 115200, 57600, 38400, 19200, 9600)"); |