2 * Blackfin Infra-red Driver
4 * Copyright 2006-2009 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
13 #ifdef CONFIG_SIR_BFIN_DMA
14 #define DMA_SIR_RX_XCNT 10
15 #define DMA_SIR_RX_YCNT (PAGE_SIZE / DMA_SIR_RX_XCNT)
16 #define DMA_SIR_RX_FLUSH_JIFS (HZ * 4 / 250)
20 static int max_rate = 57600;
22 static int max_rate = 115200;
25 static void turnaround_delay(unsigned long last_jif, int mtt)
29 mtt = mtt < 10000 ? 10000 : mtt;
30 ticks = 1 + mtt / (USEC_PER_SEC / HZ);
31 schedule_timeout_uninterruptible(ticks);
34 static void __devinit bfin_sir_init_ports(struct bfin_sir_port *sp, struct platform_device *pdev)
39 for (i = 0; i < pdev->num_resources; i++) {
40 res = &pdev->resource[i];
43 sp->membase = (void __iomem *)res->start;
49 sp->rx_dma_channel = res->start;
50 sp->tx_dma_channel = res->end;
58 #ifdef CONFIG_SIR_BFIN_DMA
60 init_timer(&(sp->rx_dma_timer));
64 static void bfin_sir_stop_tx(struct bfin_sir_port *port)
66 #ifdef CONFIG_SIR_BFIN_DMA
67 disable_dma(port->tx_dma_channel);
70 while (!(SIR_UART_GET_LSR(port) & THRE)) {
75 SIR_UART_STOP_TX(port);
78 static void bfin_sir_enable_tx(struct bfin_sir_port *port)
80 SIR_UART_ENABLE_TX(port);
83 static void bfin_sir_stop_rx(struct bfin_sir_port *port)
85 SIR_UART_STOP_RX(port);
88 static void bfin_sir_enable_rx(struct bfin_sir_port *port)
90 SIR_UART_ENABLE_RX(port);
93 static int bfin_sir_set_speed(struct bfin_sir_port *port, int speed)
97 unsigned short val, lsr, lcr;
110 quot = (port->clk + (8 * speed)) / (16 * speed)\
115 lsr = SIR_UART_GET_LSR(port);
116 } while (!(lsr & TEMT) && count--);
118 /* The useconds for 1 bits to transmit */
119 utime = 1000000 / speed + 1;
121 /* Clear UCEN bit to reset the UART state machine
122 * and control registers
124 val = SIR_UART_GET_GCTL(port);
126 SIR_UART_PUT_GCTL(port, val);
128 /* Set DLAB in LCR to Access THR RBR IER */
129 SIR_UART_SET_DLAB(port);
132 SIR_UART_PUT_DLL(port, quot & 0xFF);
133 SIR_UART_PUT_DLH(port, (quot >> 8) & 0xFF);
136 /* Clear DLAB in LCR */
137 SIR_UART_CLEAR_DLAB(port);
140 SIR_UART_PUT_LCR(port, lcr);
142 val = SIR_UART_GET_GCTL(port);
144 SIR_UART_PUT_GCTL(port, val);
149 printk(KERN_WARNING "bfin_sir: Invalid speed %d\n", speed);
153 val = SIR_UART_GET_GCTL(port);
154 /* If not add the 'RPOLC', we can't catch the receive interrupt.
155 * It's related with the HW layout and the IR transiver.
158 SIR_UART_PUT_GCTL(port, val);
162 static int bfin_sir_is_receiving(struct net_device *dev)
164 struct bfin_sir_self *self = netdev_priv(dev);
165 struct bfin_sir_port *port = self->sir_port;
167 if (!(SIR_UART_GET_IER(port) & ERBFI))
169 return self->rx_buff.state != OUTSIDE_FRAME;
172 #ifdef CONFIG_SIR_BFIN_PIO
173 static void bfin_sir_tx_chars(struct net_device *dev)
176 struct bfin_sir_self *self = netdev_priv(dev);
177 struct bfin_sir_port *port = self->sir_port;
179 if (self->tx_buff.len != 0) {
180 chr = *(self->tx_buff.data);
181 SIR_UART_PUT_CHAR(port, chr);
182 self->tx_buff.data++;
185 self->stats.tx_packets++;
186 self->stats.tx_bytes += self->tx_buff.data - self->tx_buff.head;
187 if (self->newspeed) {
188 bfin_sir_set_speed(port, self->newspeed);
189 self->speed = self->newspeed;
192 bfin_sir_stop_tx(port);
193 bfin_sir_enable_rx(port);
195 netif_wake_queue(dev);
199 static void bfin_sir_rx_chars(struct net_device *dev)
201 struct bfin_sir_self *self = netdev_priv(dev);
202 struct bfin_sir_port *port = self->sir_port;
205 SIR_UART_CLEAR_LSR(port);
206 ch = SIR_UART_GET_CHAR(port);
207 async_unwrap_char(dev, &self->stats, &self->rx_buff, ch);
208 dev->last_rx = jiffies;
211 static irqreturn_t bfin_sir_rx_int(int irq, void *dev_id)
213 struct net_device *dev = dev_id;
214 struct bfin_sir_self *self = netdev_priv(dev);
215 struct bfin_sir_port *port = self->sir_port;
217 spin_lock(&self->lock);
218 while ((SIR_UART_GET_LSR(port) & DR))
219 bfin_sir_rx_chars(dev);
220 spin_unlock(&self->lock);
225 static irqreturn_t bfin_sir_tx_int(int irq, void *dev_id)
227 struct net_device *dev = dev_id;
228 struct bfin_sir_self *self = netdev_priv(dev);
229 struct bfin_sir_port *port = self->sir_port;
231 spin_lock(&self->lock);
232 if (SIR_UART_GET_LSR(port) & THRE)
233 bfin_sir_tx_chars(dev);
234 spin_unlock(&self->lock);
238 #endif /* CONFIG_SIR_BFIN_PIO */
240 #ifdef CONFIG_SIR_BFIN_DMA
241 static void bfin_sir_dma_tx_chars(struct net_device *dev)
243 struct bfin_sir_self *self = netdev_priv(dev);
244 struct bfin_sir_port *port = self->sir_port;
250 if (self->tx_buff.len == 0) {
251 self->stats.tx_packets++;
252 if (self->newspeed) {
253 bfin_sir_set_speed(port, self->newspeed);
254 self->speed = self->newspeed;
257 bfin_sir_enable_rx(port);
259 netif_wake_queue(dev);
263 blackfin_dcache_flush_range((unsigned long)(self->tx_buff.data),
264 (unsigned long)(self->tx_buff.data+self->tx_buff.len));
265 set_dma_config(port->tx_dma_channel,
266 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
267 INTR_ON_BUF, DIMENSION_LINEAR, DATA_SIZE_8,
269 set_dma_start_addr(port->tx_dma_channel,
270 (unsigned long)(self->tx_buff.data));
271 set_dma_x_count(port->tx_dma_channel, self->tx_buff.len);
272 set_dma_x_modify(port->tx_dma_channel, 1);
273 enable_dma(port->tx_dma_channel);
276 static irqreturn_t bfin_sir_dma_tx_int(int irq, void *dev_id)
278 struct net_device *dev = dev_id;
279 struct bfin_sir_self *self = netdev_priv(dev);
280 struct bfin_sir_port *port = self->sir_port;
282 spin_lock(&self->lock);
283 if (!(get_dma_curr_irqstat(port->tx_dma_channel) & DMA_RUN)) {
284 clear_dma_irqstat(port->tx_dma_channel);
285 bfin_sir_stop_tx(port);
287 self->stats.tx_packets++;
288 self->stats.tx_bytes += self->tx_buff.len;
289 self->tx_buff.len = 0;
290 if (self->newspeed) {
291 bfin_sir_set_speed(port, self->newspeed);
292 self->speed = self->newspeed;
295 bfin_sir_enable_rx(port);
297 netif_wake_queue(dev);
300 spin_unlock(&self->lock);
305 static void bfin_sir_dma_rx_chars(struct net_device *dev)
307 struct bfin_sir_self *self = netdev_priv(dev);
308 struct bfin_sir_port *port = self->sir_port;
311 SIR_UART_CLEAR_LSR(port);
313 for (i = port->rx_dma_buf.head; i < port->rx_dma_buf.tail; i++)
314 async_unwrap_char(dev, &self->stats, &self->rx_buff, port->rx_dma_buf.buf[i]);
317 void bfin_sir_rx_dma_timeout(struct net_device *dev)
319 struct bfin_sir_self *self = netdev_priv(dev);
320 struct bfin_sir_port *port = self->sir_port;
324 spin_lock_irqsave(&self->lock, flags);
325 x_pos = DMA_SIR_RX_XCNT - get_dma_curr_xcount(port->rx_dma_channel);
326 if (x_pos == DMA_SIR_RX_XCNT)
329 pos = port->rx_dma_nrows * DMA_SIR_RX_XCNT + x_pos;
331 if (pos > port->rx_dma_buf.tail) {
332 port->rx_dma_buf.tail = pos;
333 bfin_sir_dma_rx_chars(dev);
334 port->rx_dma_buf.head = port->rx_dma_buf.tail;
336 spin_unlock_irqrestore(&self->lock, flags);
339 static irqreturn_t bfin_sir_dma_rx_int(int irq, void *dev_id)
341 struct net_device *dev = dev_id;
342 struct bfin_sir_self *self = netdev_priv(dev);
343 struct bfin_sir_port *port = self->sir_port;
344 unsigned short irqstat;
346 spin_lock(&self->lock);
348 port->rx_dma_nrows++;
349 port->rx_dma_buf.tail = DMA_SIR_RX_XCNT * port->rx_dma_nrows;
350 bfin_sir_dma_rx_chars(dev);
351 if (port->rx_dma_nrows >= DMA_SIR_RX_YCNT) {
352 port->rx_dma_nrows = 0;
353 port->rx_dma_buf.tail = 0;
355 port->rx_dma_buf.head = port->rx_dma_buf.tail;
357 irqstat = get_dma_curr_irqstat(port->rx_dma_channel);
358 clear_dma_irqstat(port->rx_dma_channel);
359 spin_unlock(&self->lock);
361 mod_timer(&port->rx_dma_timer, jiffies + DMA_SIR_RX_FLUSH_JIFS);
364 #endif /* CONFIG_SIR_BFIN_DMA */
366 static int bfin_sir_startup(struct bfin_sir_port *port, struct net_device *dev)
368 #ifdef CONFIG_SIR_BFIN_DMA
369 dma_addr_t dma_handle;
370 #endif /* CONFIG_SIR_BFIN_DMA */
372 if (request_dma(port->rx_dma_channel, "BFIN_UART_RX") < 0) {
373 dev_warn(&dev->dev, "Unable to attach SIR RX DMA channel\n");
377 if (request_dma(port->tx_dma_channel, "BFIN_UART_TX") < 0) {
378 dev_warn(&dev->dev, "Unable to attach SIR TX DMA channel\n");
379 free_dma(port->rx_dma_channel);
383 #ifdef CONFIG_SIR_BFIN_DMA
385 set_dma_callback(port->rx_dma_channel, bfin_sir_dma_rx_int, dev);
386 set_dma_callback(port->tx_dma_channel, bfin_sir_dma_tx_int, dev);
388 port->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
389 port->rx_dma_buf.head = 0;
390 port->rx_dma_buf.tail = 0;
391 port->rx_dma_nrows = 0;
393 set_dma_config(port->rx_dma_channel,
394 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
395 INTR_ON_ROW, DIMENSION_2D,
396 DATA_SIZE_8, DMA_SYNC_RESTART));
397 set_dma_x_count(port->rx_dma_channel, DMA_SIR_RX_XCNT);
398 set_dma_x_modify(port->rx_dma_channel, 1);
399 set_dma_y_count(port->rx_dma_channel, DMA_SIR_RX_YCNT);
400 set_dma_y_modify(port->rx_dma_channel, 1);
401 set_dma_start_addr(port->rx_dma_channel, (unsigned long)port->rx_dma_buf.buf);
402 enable_dma(port->rx_dma_channel);
404 port->rx_dma_timer.data = (unsigned long)(dev);
405 port->rx_dma_timer.function = (void *)bfin_sir_rx_dma_timeout;
409 if (request_irq(port->irq, bfin_sir_rx_int, IRQF_DISABLED, "BFIN_SIR_RX", dev)) {
410 dev_warn(&dev->dev, "Unable to attach SIR RX interrupt\n");
414 if (request_irq(port->irq+1, bfin_sir_tx_int, IRQF_DISABLED, "BFIN_SIR_TX", dev)) {
415 dev_warn(&dev->dev, "Unable to attach SIR TX interrupt\n");
416 free_irq(port->irq, dev);
424 static void bfin_sir_shutdown(struct bfin_sir_port *port, struct net_device *dev)
428 bfin_sir_stop_rx(port);
429 SIR_UART_DISABLE_INTS(port);
431 val = SIR_UART_GET_GCTL(port);
432 val &= ~(UCEN | IREN | RPOLC);
433 SIR_UART_PUT_GCTL(port, val);
435 #ifdef CONFIG_SIR_BFIN_DMA
436 disable_dma(port->tx_dma_channel);
437 disable_dma(port->rx_dma_channel);
438 del_timer(&(port->rx_dma_timer));
439 dma_free_coherent(NULL, PAGE_SIZE, port->rx_dma_buf.buf, 0);
441 free_irq(port->irq+1, dev);
442 free_irq(port->irq, dev);
444 free_dma(port->tx_dma_channel);
445 free_dma(port->rx_dma_channel);
449 static int bfin_sir_suspend(struct platform_device *pdev, pm_message_t state)
451 struct bfin_sir_port *sir_port;
452 struct net_device *dev;
453 struct bfin_sir_self *self;
455 sir_port = platform_get_drvdata(pdev);
460 self = netdev_priv(dev);
462 flush_work(&self->work);
463 bfin_sir_shutdown(self->sir_port, dev);
464 netif_device_detach(dev);
469 static int bfin_sir_resume(struct platform_device *pdev)
471 struct bfin_sir_port *sir_port;
472 struct net_device *dev;
473 struct bfin_sir_self *self;
474 struct bfin_sir_port *port;
476 sir_port = platform_get_drvdata(pdev);
481 self = netdev_priv(dev);
482 port = self->sir_port;
484 if (self->newspeed) {
485 self->speed = self->newspeed;
488 bfin_sir_startup(port, dev);
489 bfin_sir_set_speed(port, 9600);
490 bfin_sir_enable_rx(port);
491 netif_device_attach(dev);
496 #define bfin_sir_suspend NULL
497 #define bfin_sir_resume NULL
500 static void bfin_sir_send_work(struct work_struct *work)
502 struct bfin_sir_self *self = container_of(work, struct bfin_sir_self, work);
503 struct net_device *dev = self->sir_port->dev;
504 struct bfin_sir_port *port = self->sir_port;
508 while (bfin_sir_is_receiving(dev) && --tx_cnt)
509 turnaround_delay(dev->last_rx, self->mtt);
511 bfin_sir_stop_rx(port);
513 /* To avoid losting RX interrupt, we reset IR function before
514 * sending data. We also can set the speed, which will
515 * reset all the UART.
517 val = SIR_UART_GET_GCTL(port);
518 val &= ~(IREN | RPOLC);
519 SIR_UART_PUT_GCTL(port, val);
522 SIR_UART_PUT_GCTL(port, val);
524 /* bfin_sir_set_speed(port, self->speed); */
526 #ifdef CONFIG_SIR_BFIN_DMA
527 bfin_sir_dma_tx_chars(dev);
529 bfin_sir_enable_tx(port);
530 dev->trans_start = jiffies;
533 static int bfin_sir_hard_xmit(struct sk_buff *skb, struct net_device *dev)
535 struct bfin_sir_self *self = netdev_priv(dev);
536 int speed = irda_get_next_speed(skb);
538 netif_stop_queue(dev);
540 self->mtt = irda_get_mtt(skb);
542 if (speed != self->speed && speed != -1)
543 self->newspeed = speed;
545 self->tx_buff.data = self->tx_buff.head;
547 self->tx_buff.len = 0;
549 self->tx_buff.len = async_wrap_skb(skb, self->tx_buff.data, self->tx_buff.truesize);
551 schedule_work(&self->work);
557 static int bfin_sir_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
559 struct if_irda_req *rq = (struct if_irda_req *)ifreq;
560 struct bfin_sir_self *self = netdev_priv(dev);
561 struct bfin_sir_port *port = self->sir_port;
566 if (capable(CAP_NET_ADMIN)) {
568 ret = bfin_sir_set_speed(port, rq->ifr_baudrate);
569 bfin_sir_enable_rx(port);
571 dev_warn(&dev->dev, "SIOCSBANDWIDTH: !netif_running\n");
579 if (capable(CAP_NET_ADMIN)) {
580 irda_device_set_media_busy(dev, TRUE);
586 rq->ifr_receiving = bfin_sir_is_receiving(dev);
597 static struct net_device_stats *bfin_sir_stats(struct net_device *dev)
599 struct bfin_sir_self *self = netdev_priv(dev);
604 static int bfin_sir_open(struct net_device *dev)
606 struct bfin_sir_self *self = netdev_priv(dev);
607 struct bfin_sir_port *port = self->sir_port;
613 spin_lock_init(&self->lock);
615 err = bfin_sir_startup(port, dev);
619 bfin_sir_set_speed(port, 9600);
621 self->irlap = irlap_open(dev, &self->qos, DRIVER_NAME);
625 INIT_WORK(&self->work, bfin_sir_send_work);
628 * Now enable the interrupt then start the queue
631 bfin_sir_enable_rx(port);
633 netif_start_queue(dev);
639 bfin_sir_shutdown(port, dev);
644 static int bfin_sir_stop(struct net_device *dev)
646 struct bfin_sir_self *self = netdev_priv(dev);
648 flush_work(&self->work);
649 bfin_sir_shutdown(self->sir_port, dev);
652 dev_kfree_skb(self->rxskb);
658 irlap_close(self->irlap);
662 netif_stop_queue(dev);
668 static int bfin_sir_init_iobuf(iobuff_t *io, int size)
670 io->head = kmalloc(size, GFP_KERNEL);
674 io->in_frame = FALSE;
675 io->state = OUTSIDE_FRAME;
680 static int __devinit bfin_sir_probe(struct platform_device *pdev)
682 struct net_device *dev;
683 struct bfin_sir_self *self;
684 unsigned int baudrate_mask;
685 struct bfin_sir_port *sir_port;
688 if (pdev->id >= 0 && pdev->id < ARRAY_SIZE(per) && \
689 per[pdev->id][3] == pdev->id) {
690 err = peripheral_request_list(per[pdev->id], DRIVER_NAME);
694 dev_err(&pdev->dev, "Invalid pdev id, please check board file\n");
699 sir_port = kmalloc(sizeof(*sir_port), GFP_KERNEL);
703 bfin_sir_init_ports(sir_port, pdev);
705 dev = alloc_irdadev(sizeof(*self));
709 self = netdev_priv(dev);
710 self->dev = &pdev->dev;
711 self->sir_port = sir_port;
714 err = bfin_sir_init_iobuf(&self->rx_buff, IRDA_SKB_MAX_MTU);
717 err = bfin_sir_init_iobuf(&self->tx_buff, IRDA_SIR_MAX_FRAME);
721 dev->hard_start_xmit = bfin_sir_hard_xmit;
722 dev->open = bfin_sir_open;
723 dev->stop = bfin_sir_stop;
724 dev->do_ioctl = bfin_sir_ioctl;
725 dev->get_stats = bfin_sir_stats;
726 dev->irq = sir_port->irq;
728 irda_init_max_qos_capabilies(&self->qos);
730 baudrate_mask = IR_9600;
734 baudrate_mask |= IR_115200;
736 baudrate_mask |= IR_57600;
738 baudrate_mask |= IR_38400;
740 baudrate_mask |= IR_19200;
744 dev_warn(&pdev->dev, "Invalid maximum baud rate, using 9600\n");
747 self->qos.baud_rate.bits &= baudrate_mask;
749 self->qos.min_turn_time.bits = 1; /* 10 ms or more */
751 irda_qos_bits_to_value(&self->qos);
753 err = register_netdev(dev);
756 kfree(self->tx_buff.head);
758 kfree(self->rx_buff.head);
764 peripheral_free_list(per[pdev->id]);
766 platform_set_drvdata(pdev, sir_port);
771 static int __devexit bfin_sir_remove(struct platform_device *pdev)
773 struct bfin_sir_port *sir_port;
774 struct net_device *dev = NULL;
775 struct bfin_sir_self *self;
777 sir_port = platform_get_drvdata(pdev);
781 self = netdev_priv(dev);
782 unregister_netdev(dev);
783 kfree(self->tx_buff.head);
784 kfree(self->rx_buff.head);
787 platform_set_drvdata(pdev, NULL);
792 static struct platform_driver bfin_ir_driver = {
793 .probe = bfin_sir_probe,
794 .remove = __devexit_p(bfin_sir_remove),
795 .suspend = bfin_sir_suspend,
796 .resume = bfin_sir_resume,
802 static int __init bfin_sir_init(void)
804 return platform_driver_register(&bfin_ir_driver);
807 static void __exit bfin_sir_exit(void)
809 platform_driver_unregister(&bfin_ir_driver);
812 module_init(bfin_sir_init);
813 module_exit(bfin_sir_exit);
815 module_param(max_rate, int, 0);
816 MODULE_PARM_DESC(max_rate, "Maximum baud rate (115200, 57600, 38400, 19200, 9600)");
818 MODULE_AUTHOR("Graf Yang <graf.yang@analog.com>");
819 MODULE_DESCRIPTION("Blackfin IrDA driver");
820 MODULE_LICENSE("GPL");