2 * Alchemy Semi Au1000 IrDA driver
4 * Copyright 2001 MontaVista Software Inc.
5 * Author: MontaVista Software, Inc.
6 * ppopov@mvista.com or source@mvista.com
8 * This program is free software; you can distribute it and/or modify it
9 * under the terms of the GNU General Public License (Version 2) as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include <linux/clk.h>
22 #include <linux/module.h>
23 #include <linux/netdevice.h>
24 #include <linux/interrupt.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/ioport.h>
30 #include <net/irda/irda.h>
31 #include <net/irda/irmod.h>
32 #include <net/irda/wrapper.h>
33 #include <net/irda/irda_device.h>
34 #include <asm/mach-au1x00/au1000.h>
37 #define IR_RING_PTR_STATUS 0x00
38 #define IR_RING_BASE_ADDR_H 0x04
39 #define IR_RING_BASE_ADDR_L 0x08
40 #define IR_RING_SIZE 0x0C
41 #define IR_RING_PROMPT 0x10
42 #define IR_RING_ADDR_CMPR 0x14
43 #define IR_INT_CLEAR 0x18
44 #define IR_CONFIG_1 0x20
45 #define IR_SIR_FLAGS 0x24
46 #define IR_STATUS 0x28
47 #define IR_READ_PHY_CONFIG 0x2C
48 #define IR_WRITE_PHY_CONFIG 0x30
49 #define IR_MAX_PKT_LEN 0x34
50 #define IR_RX_BYTE_CNT 0x38
51 #define IR_CONFIG_2 0x3C
52 #define IR_ENABLE 0x40
55 #define IR_RX_INVERT_LED (1 << 0)
56 #define IR_TX_INVERT_LED (1 << 1)
57 #define IR_ST (1 << 2)
58 #define IR_SF (1 << 3)
59 #define IR_SIR (1 << 4)
60 #define IR_MIR (1 << 5)
61 #define IR_FIR (1 << 6)
62 #define IR_16CRC (1 << 7)
63 #define IR_TD (1 << 8)
64 #define IR_RX_ALL (1 << 9)
65 #define IR_DMA_ENABLE (1 << 10)
66 #define IR_RX_ENABLE (1 << 11)
67 #define IR_TX_ENABLE (1 << 12)
68 #define IR_LOOPBACK (1 << 14)
69 #define IR_SIR_MODE (IR_SIR | IR_DMA_ENABLE | \
70 IR_RX_ALL | IR_RX_ENABLE | IR_SF | \
74 #define IR_RX_STATUS (1 << 9)
75 #define IR_TX_STATUS (1 << 10)
76 #define IR_PHYEN (1 << 15)
78 /* ir_write_phy_config */
79 #define IR_BR(x) (((x) & 0x3f) << 10) /* baud rate */
80 #define IR_PW(x) (((x) & 0x1f) << 5) /* pulse width */
81 #define IR_P(x) ((x) & 0x1f) /* preamble bits */
84 #define IR_MODE_INV (1 << 0)
85 #define IR_ONE_PIN (1 << 1)
86 #define IR_PHYCLK_40MHZ (0 << 2)
87 #define IR_PHYCLK_48MHZ (1 << 2)
88 #define IR_PHYCLK_56MHZ (2 << 2)
89 #define IR_PHYCLK_64MHZ (3 << 2)
90 #define IR_DP (1 << 4)
91 #define IR_DA (1 << 5)
92 #define IR_FLT_HIGH (0 << 6)
93 #define IR_FLT_MEDHI (1 << 6)
94 #define IR_FLT_MEDLO (2 << 6)
95 #define IR_FLT_LO (3 << 6)
96 #define IR_IEN (1 << 8)
99 #define IR_HC (1 << 3) /* divide SBUS clock by 2 */
100 #define IR_CE (1 << 2) /* clock enable */
101 #define IR_C (1 << 1) /* coherency bit */
102 #define IR_BE (1 << 0) /* set in big endian mode */
104 #define NUM_IR_DESC 64
105 #define RING_SIZE_4 0x0
106 #define RING_SIZE_16 0x3
107 #define RING_SIZE_64 0xF
108 #define MAX_NUM_IR_DESC 64
109 #define MAX_BUF_SIZE 2048
111 /* Ring descriptor flags */
112 #define AU_OWN (1 << 7) /* tx,rx */
113 #define IR_DIS_CRC (1 << 6) /* tx */
114 #define IR_BAD_CRC (1 << 5) /* tx */
115 #define IR_NEED_PULSE (1 << 4) /* tx */
116 #define IR_FORCE_UNDER (1 << 3) /* tx */
117 #define IR_DISABLE_TX (1 << 2) /* tx */
118 #define IR_HW_UNDER (1 << 0) /* tx */
119 #define IR_TX_ERROR (IR_DIS_CRC | IR_BAD_CRC | IR_HW_UNDER)
121 #define IR_PHY_ERROR (1 << 6) /* rx */
122 #define IR_CRC_ERROR (1 << 5) /* rx */
123 #define IR_MAX_LEN (1 << 4) /* rx */
124 #define IR_FIFO_OVER (1 << 3) /* rx */
125 #define IR_SIR_ERROR (1 << 2) /* rx */
126 #define IR_RX_ERROR (IR_PHY_ERROR | IR_CRC_ERROR | \
127 IR_MAX_LEN | IR_FIFO_OVER | IR_SIR_ERROR)
130 struct db_dest
*pnext
;
136 u8 count_0
; /* 7:0 */
137 u8 count_1
; /* 12:8 */
141 u8 addr_1
; /* 15:8 */
142 u8 addr_2
; /* 23:16 */
143 u8 addr_3
; /* 31:24 */
146 /* Private data for each instance */
147 struct au1k_private
{
148 void __iomem
*iobase
;
151 struct db_dest
*pDBfree
;
152 struct db_dest db
[2 * NUM_IR_DESC
];
153 volatile struct ring_dest
*rx_ring
[NUM_IR_DESC
];
154 volatile struct ring_dest
*tx_ring
[NUM_IR_DESC
];
155 struct db_dest
*rx_db_inuse
[NUM_IR_DESC
];
156 struct db_dest
*tx_db_inuse
[NUM_IR_DESC
];
164 struct net_device
*netdev
;
166 struct irlap_cb
*irlap
;
172 struct timer_list timer
;
174 struct resource
*ioarea
;
175 struct au1k_irda_platform_data
*platdata
;
176 struct clk
*irda_clk
;
179 static int qos_mtt_bits
= 0x07; /* 1 ms or more */
181 #define RUN_AT(x) (jiffies + (x))
183 static void au1k_irda_plat_set_phy_mode(struct au1k_private
*p
, int mode
)
185 if (p
->platdata
&& p
->platdata
->set_phy_mode
)
186 p
->platdata
->set_phy_mode(mode
);
189 static inline unsigned long irda_read(struct au1k_private
*p
,
193 * IrDA peripheral bug. You have to read the register
194 * twice to get the right value.
196 (void)__raw_readl(p
->iobase
+ ofs
);
197 return __raw_readl(p
->iobase
+ ofs
);
200 static inline void irda_write(struct au1k_private
*p
, unsigned long ofs
,
203 __raw_writel(val
, p
->iobase
+ ofs
);
208 * Buffer allocation/deallocation routines. The buffer descriptor returned
209 * has the virtual and dma address of a buffer suitable for
210 * both, receive and transmit operations.
212 static struct db_dest
*GetFreeDB(struct au1k_private
*aup
)
218 aup
->pDBfree
= db
->pnext
;
223 DMA memory allocation, derived from pci_alloc_consistent.
224 However, the Au1000 data cache is coherent (when programmed
225 so), therefore we return KSEG0 address, not KSEG1.
227 static void *dma_alloc(size_t size
, dma_addr_t
*dma_handle
)
230 int gfp
= GFP_ATOMIC
| GFP_DMA
;
232 ret
= (void *)__get_free_pages(gfp
, get_order(size
));
235 memset(ret
, 0, size
);
236 *dma_handle
= virt_to_bus(ret
);
237 ret
= (void *)KSEG0ADDR(ret
);
242 static void dma_free(void *vaddr
, size_t size
)
244 vaddr
= (void *)KSEG0ADDR(vaddr
);
245 free_pages((unsigned long) vaddr
, get_order(size
));
249 static void setup_hw_rings(struct au1k_private
*aup
, u32 rx_base
, u32 tx_base
)
252 for (i
= 0; i
< NUM_IR_DESC
; i
++) {
253 aup
->rx_ring
[i
] = (volatile struct ring_dest
*)
254 (rx_base
+ sizeof(struct ring_dest
) * i
);
256 for (i
= 0; i
< NUM_IR_DESC
; i
++) {
257 aup
->tx_ring
[i
] = (volatile struct ring_dest
*)
258 (tx_base
+ sizeof(struct ring_dest
) * i
);
262 static int au1k_irda_init_iobuf(iobuff_t
*io
, int size
)
264 io
->head
= kmalloc(size
, GFP_KERNEL
);
265 if (io
->head
!= NULL
) {
267 io
->in_frame
= FALSE
;
268 io
->state
= OUTSIDE_FRAME
;
271 return io
->head
? 0 : -ENOMEM
;
275 * Set the IrDA communications speed.
277 static int au1k_irda_set_speed(struct net_device
*dev
, int speed
)
279 struct au1k_private
*aup
= netdev_priv(dev
);
280 volatile struct ring_dest
*ptxd
;
281 unsigned long control
;
282 int ret
= 0, timeout
= 10, i
;
284 if (speed
== aup
->speed
)
287 /* disable PHY first */
288 au1k_irda_plat_set_phy_mode(aup
, AU1000_IRDA_PHY_MODE_OFF
);
289 irda_write(aup
, IR_STATUS
, irda_read(aup
, IR_STATUS
) & ~IR_PHYEN
);
292 irda_write(aup
, IR_CONFIG_1
,
293 irda_read(aup
, IR_CONFIG_1
) & ~(IR_RX_ENABLE
| IR_TX_ENABLE
));
295 while (irda_read(aup
, IR_STATUS
) & (IR_RX_STATUS
| IR_TX_STATUS
)) {
298 printk(KERN_ERR
"%s: rx/tx disable timeout\n",
305 irda_write(aup
, IR_CONFIG_1
,
306 irda_read(aup
, IR_CONFIG_1
) & ~IR_DMA_ENABLE
);
309 /* After we disable tx/rx. the index pointers go back to zero. */
310 aup
->tx_head
= aup
->tx_tail
= aup
->rx_head
= 0;
311 for (i
= 0; i
< NUM_IR_DESC
; i
++) {
312 ptxd
= aup
->tx_ring
[i
];
318 for (i
= 0; i
< NUM_IR_DESC
; i
++) {
319 ptxd
= aup
->rx_ring
[i
];
322 ptxd
->flags
= AU_OWN
;
325 if (speed
== 4000000)
326 au1k_irda_plat_set_phy_mode(aup
, AU1000_IRDA_PHY_MODE_FIR
);
328 au1k_irda_plat_set_phy_mode(aup
, AU1000_IRDA_PHY_MODE_SIR
);
332 irda_write(aup
, IR_WRITE_PHY_CONFIG
, IR_BR(11) | IR_PW(12));
333 irda_write(aup
, IR_CONFIG_1
, IR_SIR_MODE
);
336 irda_write(aup
, IR_WRITE_PHY_CONFIG
, IR_BR(5) | IR_PW(12));
337 irda_write(aup
, IR_CONFIG_1
, IR_SIR_MODE
);
340 irda_write(aup
, IR_WRITE_PHY_CONFIG
, IR_BR(2) | IR_PW(12));
341 irda_write(aup
, IR_CONFIG_1
, IR_SIR_MODE
);
344 irda_write(aup
, IR_WRITE_PHY_CONFIG
, IR_BR(1) | IR_PW(12));
345 irda_write(aup
, IR_CONFIG_1
, IR_SIR_MODE
);
348 irda_write(aup
, IR_WRITE_PHY_CONFIG
, IR_PW(12));
349 irda_write(aup
, IR_CONFIG_1
, IR_SIR_MODE
);
352 irda_write(aup
, IR_WRITE_PHY_CONFIG
, IR_P(15));
353 irda_write(aup
, IR_CONFIG_1
, IR_FIR
| IR_DMA_ENABLE
|
357 printk(KERN_ERR
"%s unsupported speed %x\n", dev
->name
, speed
);
363 irda_write(aup
, IR_STATUS
, irda_read(aup
, IR_STATUS
) | IR_PHYEN
);
365 control
= irda_read(aup
, IR_STATUS
);
366 irda_write(aup
, IR_RING_PROMPT
, 0);
368 if (control
& (1 << 14)) {
369 printk(KERN_ERR
"%s: configuration error\n", dev
->name
);
371 if (control
& (1 << 11))
372 printk(KERN_DEBUG
"%s Valid SIR config\n", dev
->name
);
373 if (control
& (1 << 12))
374 printk(KERN_DEBUG
"%s Valid MIR config\n", dev
->name
);
375 if (control
& (1 << 13))
376 printk(KERN_DEBUG
"%s Valid FIR config\n", dev
->name
);
377 if (control
& (1 << 10))
378 printk(KERN_DEBUG
"%s TX enabled\n", dev
->name
);
379 if (control
& (1 << 9))
380 printk(KERN_DEBUG
"%s RX enabled\n", dev
->name
);
386 static void update_rx_stats(struct net_device
*dev
, u32 status
, u32 count
)
388 struct net_device_stats
*ps
= &dev
->stats
;
392 if (status
& IR_RX_ERROR
) {
394 if (status
& (IR_PHY_ERROR
| IR_FIFO_OVER
))
395 ps
->rx_missed_errors
++;
396 if (status
& IR_MAX_LEN
)
397 ps
->rx_length_errors
++;
398 if (status
& IR_CRC_ERROR
)
401 ps
->rx_bytes
+= count
;
404 static void update_tx_stats(struct net_device
*dev
, u32 status
, u32 pkt_len
)
406 struct net_device_stats
*ps
= &dev
->stats
;
409 ps
->tx_bytes
+= pkt_len
;
411 if (status
& IR_TX_ERROR
) {
413 ps
->tx_aborted_errors
++;
417 static void au1k_tx_ack(struct net_device
*dev
)
419 struct au1k_private
*aup
= netdev_priv(dev
);
420 volatile struct ring_dest
*ptxd
;
422 ptxd
= aup
->tx_ring
[aup
->tx_tail
];
423 while (!(ptxd
->flags
& AU_OWN
) && (aup
->tx_tail
!= aup
->tx_head
)) {
424 update_tx_stats(dev
, ptxd
->flags
,
425 (ptxd
->count_1
<< 8) | ptxd
->count_0
);
429 aup
->tx_tail
= (aup
->tx_tail
+ 1) & (NUM_IR_DESC
- 1);
430 ptxd
= aup
->tx_ring
[aup
->tx_tail
];
434 netif_wake_queue(dev
);
438 if (aup
->tx_tail
== aup
->tx_head
) {
440 au1k_irda_set_speed(dev
, aup
->newspeed
);
443 irda_write(aup
, IR_CONFIG_1
,
444 irda_read(aup
, IR_CONFIG_1
) & ~IR_TX_ENABLE
);
445 irda_write(aup
, IR_CONFIG_1
,
446 irda_read(aup
, IR_CONFIG_1
) | IR_RX_ENABLE
);
447 irda_write(aup
, IR_RING_PROMPT
, 0);
452 static int au1k_irda_rx(struct net_device
*dev
)
454 struct au1k_private
*aup
= netdev_priv(dev
);
455 volatile struct ring_dest
*prxd
;
460 prxd
= aup
->rx_ring
[aup
->rx_head
];
463 while (!(flags
& AU_OWN
)) {
464 pDB
= aup
->rx_db_inuse
[aup
->rx_head
];
465 count
= (prxd
->count_1
<< 8) | prxd
->count_0
;
466 if (!(flags
& IR_RX_ERROR
)) {
468 update_rx_stats(dev
, flags
, count
);
469 skb
= alloc_skb(count
+ 1, GFP_ATOMIC
);
471 dev
->stats
.rx_dropped
++;
475 if (aup
->speed
== 4000000)
478 skb_put(skb
, count
- 2);
479 skb_copy_to_linear_data(skb
, (void *)pDB
->vaddr
,
482 skb_reset_mac_header(skb
);
483 skb
->protocol
= htons(ETH_P_IRDA
);
488 prxd
->flags
|= AU_OWN
;
489 aup
->rx_head
= (aup
->rx_head
+ 1) & (NUM_IR_DESC
- 1);
490 irda_write(aup
, IR_RING_PROMPT
, 0);
492 /* next descriptor */
493 prxd
= aup
->rx_ring
[aup
->rx_head
];
500 static irqreturn_t
au1k_irda_interrupt(int dummy
, void *dev_id
)
502 struct net_device
*dev
= dev_id
;
503 struct au1k_private
*aup
= netdev_priv(dev
);
505 irda_write(aup
, IR_INT_CLEAR
, 0); /* ack irda interrupts */
513 static int au1k_init(struct net_device
*dev
)
515 struct au1k_private
*aup
= netdev_priv(dev
);
516 u32 enable
, ring_address
, phyck
;
520 c
= clk_get(NULL
, "irda_clk");
523 i
= clk_prepare_enable(c
);
529 switch (clk_get_rate(c
)) {
531 phyck
= IR_PHYCLK_40MHZ
;
534 phyck
= IR_PHYCLK_48MHZ
;
537 phyck
= IR_PHYCLK_56MHZ
;
540 phyck
= IR_PHYCLK_64MHZ
;
543 clk_disable_unprepare(c
);
549 enable
= IR_HC
| IR_CE
| IR_C
;
550 #ifndef CONFIG_CPU_LITTLE_ENDIAN
557 for (i
= 0; i
< NUM_IR_DESC
; i
++)
558 aup
->rx_ring
[i
]->flags
= AU_OWN
;
560 irda_write(aup
, IR_ENABLE
, enable
);
564 au1k_irda_plat_set_phy_mode(aup
, AU1000_IRDA_PHY_MODE_OFF
);
565 irda_write(aup
, IR_STATUS
, irda_read(aup
, IR_STATUS
) & ~IR_PHYEN
);
568 irda_write(aup
, IR_MAX_PKT_LEN
, MAX_BUF_SIZE
);
570 ring_address
= (u32
)virt_to_phys((void *)aup
->rx_ring
[0]);
571 irda_write(aup
, IR_RING_BASE_ADDR_H
, ring_address
>> 26);
572 irda_write(aup
, IR_RING_BASE_ADDR_L
, (ring_address
>> 10) & 0xffff);
574 irda_write(aup
, IR_RING_SIZE
,
575 (RING_SIZE_64
<< 8) | (RING_SIZE_64
<< 12));
577 irda_write(aup
, IR_CONFIG_2
, phyck
| IR_ONE_PIN
);
578 irda_write(aup
, IR_RING_ADDR_CMPR
, 0);
580 au1k_irda_set_speed(dev
, 9600);
584 static int au1k_irda_start(struct net_device
*dev
)
586 struct au1k_private
*aup
= netdev_priv(dev
);
590 retval
= au1k_init(dev
);
592 printk(KERN_ERR
"%s: error in au1k_init\n", dev
->name
);
596 retval
= request_irq(aup
->irq_tx
, &au1k_irda_interrupt
, 0,
599 printk(KERN_ERR
"%s: unable to get IRQ %d\n",
600 dev
->name
, dev
->irq
);
603 retval
= request_irq(aup
->irq_rx
, &au1k_irda_interrupt
, 0,
606 free_irq(aup
->irq_tx
, dev
);
607 printk(KERN_ERR
"%s: unable to get IRQ %d\n",
608 dev
->name
, dev
->irq
);
612 /* Give self a hardware name */
613 sprintf(hwname
, "Au1000 SIR/FIR");
614 aup
->irlap
= irlap_open(dev
, &aup
->qos
, hwname
);
615 netif_start_queue(dev
);
618 irda_write(aup
, IR_CONFIG_2
, irda_read(aup
, IR_CONFIG_2
) | IR_IEN
);
621 au1k_irda_plat_set_phy_mode(aup
, AU1000_IRDA_PHY_MODE_SIR
);
623 aup
->timer
.expires
= RUN_AT((3 * HZ
));
624 aup
->timer
.data
= (unsigned long)dev
;
628 static int au1k_irda_stop(struct net_device
*dev
)
630 struct au1k_private
*aup
= netdev_priv(dev
);
632 au1k_irda_plat_set_phy_mode(aup
, AU1000_IRDA_PHY_MODE_OFF
);
634 /* disable interrupts */
635 irda_write(aup
, IR_CONFIG_2
, irda_read(aup
, IR_CONFIG_2
) & ~IR_IEN
);
636 irda_write(aup
, IR_CONFIG_1
, 0);
637 irda_write(aup
, IR_ENABLE
, 0); /* disable clock */
640 irlap_close(aup
->irlap
);
644 netif_stop_queue(dev
);
645 del_timer(&aup
->timer
);
647 /* disable the interrupt */
648 free_irq(aup
->irq_tx
, dev
);
649 free_irq(aup
->irq_rx
, dev
);
651 clk_disable_unprepare(aup
->irda_clk
);
652 clk_put(aup
->irda_clk
);
658 * Au1000 transmit routine.
660 static int au1k_irda_hard_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
662 struct au1k_private
*aup
= netdev_priv(dev
);
663 int speed
= irda_get_next_speed(skb
);
664 volatile struct ring_dest
*ptxd
;
668 if (speed
!= aup
->speed
&& speed
!= -1)
669 aup
->newspeed
= speed
;
671 if ((skb
->len
== 0) && (aup
->newspeed
)) {
672 if (aup
->tx_tail
== aup
->tx_head
) {
673 au1k_irda_set_speed(dev
, speed
);
680 ptxd
= aup
->tx_ring
[aup
->tx_head
];
683 if (flags
& AU_OWN
) {
684 printk(KERN_DEBUG
"%s: tx_full\n", dev
->name
);
685 netif_stop_queue(dev
);
688 } else if (((aup
->tx_head
+ 1) & (NUM_IR_DESC
- 1)) == aup
->tx_tail
) {
689 printk(KERN_DEBUG
"%s: tx_full\n", dev
->name
);
690 netif_stop_queue(dev
);
695 pDB
= aup
->tx_db_inuse
[aup
->tx_head
];
698 if (irda_read(aup
, IR_RX_BYTE_CNT
) != 0) {
699 printk(KERN_DEBUG
"tx warning: rx byte cnt %x\n",
700 irda_read(aup
, IR_RX_BYTE_CNT
));
704 if (aup
->speed
== 4000000) {
706 skb_copy_from_linear_data(skb
, (void *)pDB
->vaddr
, skb
->len
);
707 ptxd
->count_0
= skb
->len
& 0xff;
708 ptxd
->count_1
= (skb
->len
>> 8) & 0xff;
711 len
= async_wrap_skb(skb
, (u8
*)pDB
->vaddr
, MAX_BUF_SIZE
);
712 ptxd
->count_0
= len
& 0xff;
713 ptxd
->count_1
= (len
>> 8) & 0xff;
714 ptxd
->flags
|= IR_DIS_CRC
;
716 ptxd
->flags
|= AU_OWN
;
719 irda_write(aup
, IR_CONFIG_1
,
720 irda_read(aup
, IR_CONFIG_1
) | IR_TX_ENABLE
);
721 irda_write(aup
, IR_RING_PROMPT
, 0);
724 aup
->tx_head
= (aup
->tx_head
+ 1) & (NUM_IR_DESC
- 1);
729 * The Tx ring has been full longer than the watchdog timeout
730 * value. The transmitter must be hung?
732 static void au1k_tx_timeout(struct net_device
*dev
)
735 struct au1k_private
*aup
= netdev_priv(dev
);
737 printk(KERN_ERR
"%s: tx timeout\n", dev
->name
);
740 au1k_irda_set_speed(dev
, speed
);
742 netif_wake_queue(dev
);
745 static int au1k_irda_ioctl(struct net_device
*dev
, struct ifreq
*ifreq
, int cmd
)
747 struct if_irda_req
*rq
= (struct if_irda_req
*)ifreq
;
748 struct au1k_private
*aup
= netdev_priv(dev
);
749 int ret
= -EOPNOTSUPP
;
753 if (capable(CAP_NET_ADMIN
)) {
755 * We are unable to set the speed if the
756 * device is not running.
759 ret
= au1k_irda_set_speed(dev
,
762 printk(KERN_ERR
"%s ioctl: !netif_running\n",
771 if (capable(CAP_NET_ADMIN
)) {
772 irda_device_set_media_busy(dev
, TRUE
);
778 rq
->ifr_receiving
= 0;
786 static const struct net_device_ops au1k_irda_netdev_ops
= {
787 .ndo_open
= au1k_irda_start
,
788 .ndo_stop
= au1k_irda_stop
,
789 .ndo_start_xmit
= au1k_irda_hard_xmit
,
790 .ndo_tx_timeout
= au1k_tx_timeout
,
791 .ndo_do_ioctl
= au1k_irda_ioctl
,
794 static int au1k_irda_net_init(struct net_device
*dev
)
796 struct au1k_private
*aup
= netdev_priv(dev
);
797 struct db_dest
*pDB
, *pDBfree
;
798 int i
, err
, retval
= 0;
801 err
= au1k_irda_init_iobuf(&aup
->rx_buff
, 14384);
805 dev
->netdev_ops
= &au1k_irda_netdev_ops
;
807 irda_init_max_qos_capabilies(&aup
->qos
);
809 /* The only value we must override it the baudrate */
810 aup
->qos
.baud_rate
.bits
= IR_9600
| IR_19200
| IR_38400
|
811 IR_57600
| IR_115200
| IR_576000
| (IR_4000000
<< 8);
813 aup
->qos
.min_turn_time
.bits
= qos_mtt_bits
;
814 irda_qos_bits_to_value(&aup
->qos
);
818 /* Tx ring follows rx ring + 512 bytes */
819 /* we need a 1k aligned buffer */
820 aup
->rx_ring
[0] = (struct ring_dest
*)
821 dma_alloc(2 * MAX_NUM_IR_DESC
* (sizeof(struct ring_dest
)),
823 if (!aup
->rx_ring
[0])
826 /* allocate the data buffers */
828 dma_alloc(MAX_BUF_SIZE
* 2 * NUM_IR_DESC
, &temp
);
829 if (!aup
->db
[0].vaddr
)
832 setup_hw_rings(aup
, (u32
)aup
->rx_ring
[0], (u32
)aup
->rx_ring
[0] + 512);
836 for (i
= 0; i
< (2 * NUM_IR_DESC
); i
++) {
837 pDB
->pnext
= pDBfree
;
840 (u32
*)((unsigned)aup
->db
[0].vaddr
+ (MAX_BUF_SIZE
* i
));
841 pDB
->dma_addr
= (dma_addr_t
)virt_to_bus(pDB
->vaddr
);
844 aup
->pDBfree
= pDBfree
;
846 /* attach a data buffer to each descriptor */
847 for (i
= 0; i
< NUM_IR_DESC
; i
++) {
848 pDB
= GetFreeDB(aup
);
851 aup
->rx_ring
[i
]->addr_0
= (u8
)(pDB
->dma_addr
& 0xff);
852 aup
->rx_ring
[i
]->addr_1
= (u8
)((pDB
->dma_addr
>> 8) & 0xff);
853 aup
->rx_ring
[i
]->addr_2
= (u8
)((pDB
->dma_addr
>> 16) & 0xff);
854 aup
->rx_ring
[i
]->addr_3
= (u8
)((pDB
->dma_addr
>> 24) & 0xff);
855 aup
->rx_db_inuse
[i
] = pDB
;
857 for (i
= 0; i
< NUM_IR_DESC
; i
++) {
858 pDB
= GetFreeDB(aup
);
861 aup
->tx_ring
[i
]->addr_0
= (u8
)(pDB
->dma_addr
& 0xff);
862 aup
->tx_ring
[i
]->addr_1
= (u8
)((pDB
->dma_addr
>> 8) & 0xff);
863 aup
->tx_ring
[i
]->addr_2
= (u8
)((pDB
->dma_addr
>> 16) & 0xff);
864 aup
->tx_ring
[i
]->addr_3
= (u8
)((pDB
->dma_addr
>> 24) & 0xff);
865 aup
->tx_ring
[i
]->count_0
= 0;
866 aup
->tx_ring
[i
]->count_1
= 0;
867 aup
->tx_ring
[i
]->flags
= 0;
868 aup
->tx_db_inuse
[i
] = pDB
;
874 dma_free((void *)aup
->rx_ring
[0],
875 2 * MAX_NUM_IR_DESC
* (sizeof(struct ring_dest
)));
877 kfree(aup
->rx_buff
.head
);
879 printk(KERN_ERR
"au1k_irda_net_init() failed. Returns %d\n", retval
);
883 static int au1k_irda_probe(struct platform_device
*pdev
)
885 struct au1k_private
*aup
;
886 struct net_device
*dev
;
891 dev
= alloc_irdadev(sizeof(struct au1k_private
));
895 aup
= netdev_priv(dev
);
897 aup
->platdata
= pdev
->dev
.platform_data
;
900 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
904 aup
->irq_tx
= r
->start
;
906 r
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
910 aup
->irq_rx
= r
->start
;
912 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
917 aup
->ioarea
= request_mem_region(r
->start
, resource_size(r
),
922 /* bail out early if clock doesn't exist */
923 c
= clk_get(NULL
, "irda_clk");
930 aup
->iobase
= ioremap_nocache(r
->start
, resource_size(r
));
934 dev
->irq
= aup
->irq_rx
;
936 err
= au1k_irda_net_init(dev
);
939 err
= register_netdev(dev
);
943 platform_set_drvdata(pdev
, dev
);
945 printk(KERN_INFO
"IrDA: Registered device %s\n", dev
->name
);
949 dma_free((void *)aup
->db
[0].vaddr
,
950 MAX_BUF_SIZE
* 2 * NUM_IR_DESC
);
951 dma_free((void *)aup
->rx_ring
[0],
952 2 * MAX_NUM_IR_DESC
* (sizeof(struct ring_dest
)));
953 kfree(aup
->rx_buff
.head
);
955 iounmap(aup
->iobase
);
957 release_resource(aup
->ioarea
);
964 static int au1k_irda_remove(struct platform_device
*pdev
)
966 struct net_device
*dev
= platform_get_drvdata(pdev
);
967 struct au1k_private
*aup
= netdev_priv(dev
);
969 unregister_netdev(dev
);
971 dma_free((void *)aup
->db
[0].vaddr
,
972 MAX_BUF_SIZE
* 2 * NUM_IR_DESC
);
973 dma_free((void *)aup
->rx_ring
[0],
974 2 * MAX_NUM_IR_DESC
* (sizeof(struct ring_dest
)));
975 kfree(aup
->rx_buff
.head
);
977 iounmap(aup
->iobase
);
978 release_resource(aup
->ioarea
);
986 static struct platform_driver au1k_irda_driver
= {
988 .name
= "au1000-irda",
990 .probe
= au1k_irda_probe
,
991 .remove
= au1k_irda_remove
,
994 module_platform_driver(au1k_irda_driver
);
996 MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
997 MODULE_DESCRIPTION("Au1000 IrDA Device Driver");