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