au1k_ir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * Alchemy Semi Au1000 IrDA driver
  3. *
  4. * Copyright 2001 MontaVista Software Inc.
  5. * Author: MontaVista Software, Inc.
  6. * ppopov@mvista.com or source@mvista.com
  7. *
  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.
  11. *
  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
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  20. */
  21. #include <linux/init.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/time.h>
  28. #include <linux/types.h>
  29. #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>
  35. /* registers */
  36. #define IR_RING_PTR_STATUS 0x00
  37. #define IR_RING_BASE_ADDR_H 0x04
  38. #define IR_RING_BASE_ADDR_L 0x08
  39. #define IR_RING_SIZE 0x0C
  40. #define IR_RING_PROMPT 0x10
  41. #define IR_RING_ADDR_CMPR 0x14
  42. #define IR_INT_CLEAR 0x18
  43. #define IR_CONFIG_1 0x20
  44. #define IR_SIR_FLAGS 0x24
  45. #define IR_STATUS 0x28
  46. #define IR_READ_PHY_CONFIG 0x2C
  47. #define IR_WRITE_PHY_CONFIG 0x30
  48. #define IR_MAX_PKT_LEN 0x34
  49. #define IR_RX_BYTE_CNT 0x38
  50. #define IR_CONFIG_2 0x3C
  51. #define IR_ENABLE 0x40
  52. /* Config1 */
  53. #define IR_RX_INVERT_LED (1 << 0)
  54. #define IR_TX_INVERT_LED (1 << 1)
  55. #define IR_ST (1 << 2)
  56. #define IR_SF (1 << 3)
  57. #define IR_SIR (1 << 4)
  58. #define IR_MIR (1 << 5)
  59. #define IR_FIR (1 << 6)
  60. #define IR_16CRC (1 << 7)
  61. #define IR_TD (1 << 8)
  62. #define IR_RX_ALL (1 << 9)
  63. #define IR_DMA_ENABLE (1 << 10)
  64. #define IR_RX_ENABLE (1 << 11)
  65. #define IR_TX_ENABLE (1 << 12)
  66. #define IR_LOOPBACK (1 << 14)
  67. #define IR_SIR_MODE (IR_SIR | IR_DMA_ENABLE | \
  68. IR_RX_ALL | IR_RX_ENABLE | IR_SF | \
  69. IR_16CRC)
  70. /* ir_status */
  71. #define IR_RX_STATUS (1 << 9)
  72. #define IR_TX_STATUS (1 << 10)
  73. #define IR_PHYEN (1 << 15)
  74. /* ir_write_phy_config */
  75. #define IR_BR(x) (((x) & 0x3f) << 10) /* baud rate */
  76. #define IR_PW(x) (((x) & 0x1f) << 5) /* pulse width */
  77. #define IR_P(x) ((x) & 0x1f) /* preamble bits */
  78. /* Config2 */
  79. #define IR_MODE_INV (1 << 0)
  80. #define IR_ONE_PIN (1 << 1)
  81. #define IR_PHYCLK_40MHZ (0 << 2)
  82. #define IR_PHYCLK_48MHZ (1 << 2)
  83. #define IR_PHYCLK_56MHZ (2 << 2)
  84. #define IR_PHYCLK_64MHZ (3 << 2)
  85. #define IR_DP (1 << 4)
  86. #define IR_DA (1 << 5)
  87. #define IR_FLT_HIGH (0 << 6)
  88. #define IR_FLT_MEDHI (1 << 6)
  89. #define IR_FLT_MEDLO (2 << 6)
  90. #define IR_FLT_LO (3 << 6)
  91. #define IR_IEN (1 << 8)
  92. /* ir_enable */
  93. #define IR_HC (1 << 3) /* divide SBUS clock by 2 */
  94. #define IR_CE (1 << 2) /* clock enable */
  95. #define IR_C (1 << 1) /* coherency bit */
  96. #define IR_BE (1 << 0) /* set in big endian mode */
  97. #define NUM_IR_DESC 64
  98. #define RING_SIZE_4 0x0
  99. #define RING_SIZE_16 0x3
  100. #define RING_SIZE_64 0xF
  101. #define MAX_NUM_IR_DESC 64
  102. #define MAX_BUF_SIZE 2048
  103. /* Ring descriptor flags */
  104. #define AU_OWN (1 << 7) /* tx,rx */
  105. #define IR_DIS_CRC (1 << 6) /* tx */
  106. #define IR_BAD_CRC (1 << 5) /* tx */
  107. #define IR_NEED_PULSE (1 << 4) /* tx */
  108. #define IR_FORCE_UNDER (1 << 3) /* tx */
  109. #define IR_DISABLE_TX (1 << 2) /* tx */
  110. #define IR_HW_UNDER (1 << 0) /* tx */
  111. #define IR_TX_ERROR (IR_DIS_CRC | IR_BAD_CRC | IR_HW_UNDER)
  112. #define IR_PHY_ERROR (1 << 6) /* rx */
  113. #define IR_CRC_ERROR (1 << 5) /* rx */
  114. #define IR_MAX_LEN (1 << 4) /* rx */
  115. #define IR_FIFO_OVER (1 << 3) /* rx */
  116. #define IR_SIR_ERROR (1 << 2) /* rx */
  117. #define IR_RX_ERROR (IR_PHY_ERROR | IR_CRC_ERROR | \
  118. IR_MAX_LEN | IR_FIFO_OVER | IR_SIR_ERROR)
  119. struct db_dest {
  120. struct db_dest *pnext;
  121. volatile u32 *vaddr;
  122. dma_addr_t dma_addr;
  123. };
  124. struct ring_dest {
  125. u8 count_0; /* 7:0 */
  126. u8 count_1; /* 12:8 */
  127. u8 reserved;
  128. u8 flags;
  129. u8 addr_0; /* 7:0 */
  130. u8 addr_1; /* 15:8 */
  131. u8 addr_2; /* 23:16 */
  132. u8 addr_3; /* 31:24 */
  133. };
  134. /* Private data for each instance */
  135. struct au1k_private {
  136. void __iomem *iobase;
  137. int irq_rx, irq_tx;
  138. struct db_dest *pDBfree;
  139. struct db_dest db[2 * NUM_IR_DESC];
  140. volatile struct ring_dest *rx_ring[NUM_IR_DESC];
  141. volatile struct ring_dest *tx_ring[NUM_IR_DESC];
  142. struct db_dest *rx_db_inuse[NUM_IR_DESC];
  143. struct db_dest *tx_db_inuse[NUM_IR_DESC];
  144. u32 rx_head;
  145. u32 tx_head;
  146. u32 tx_tail;
  147. u32 tx_full;
  148. iobuff_t rx_buff;
  149. struct net_device *netdev;
  150. struct timeval stamp;
  151. struct timeval now;
  152. struct qos_info qos;
  153. struct irlap_cb *irlap;
  154. u8 open;
  155. u32 speed;
  156. u32 newspeed;
  157. struct timer_list timer;
  158. struct resource *ioarea;
  159. struct au1k_irda_platform_data *platdata;
  160. };
  161. static int qos_mtt_bits = 0x07; /* 1 ms or more */
  162. #define RUN_AT(x) (jiffies + (x))
  163. static void au1k_irda_plat_set_phy_mode(struct au1k_private *p, int mode)
  164. {
  165. if (p->platdata && p->platdata->set_phy_mode)
  166. p->platdata->set_phy_mode(mode);
  167. }
  168. static inline unsigned long irda_read(struct au1k_private *p,
  169. unsigned long ofs)
  170. {
  171. /*
  172. * IrDA peripheral bug. You have to read the register
  173. * twice to get the right value.
  174. */
  175. (void)__raw_readl(p->iobase + ofs);
  176. return __raw_readl(p->iobase + ofs);
  177. }
  178. static inline void irda_write(struct au1k_private *p, unsigned long ofs,
  179. unsigned long val)
  180. {
  181. __raw_writel(val, p->iobase + ofs);
  182. wmb();
  183. }
  184. /*
  185. * Buffer allocation/deallocation routines. The buffer descriptor returned
  186. * has the virtual and dma address of a buffer suitable for
  187. * both, receive and transmit operations.
  188. */
  189. static struct db_dest *GetFreeDB(struct au1k_private *aup)
  190. {
  191. struct db_dest *db;
  192. db = aup->pDBfree;
  193. if (db)
  194. aup->pDBfree = db->pnext;
  195. return db;
  196. }
  197. /*
  198. DMA memory allocation, derived from pci_alloc_consistent.
  199. However, the Au1000 data cache is coherent (when programmed
  200. so), therefore we return KSEG0 address, not KSEG1.
  201. */
  202. static void *dma_alloc(size_t size, dma_addr_t *dma_handle)
  203. {
  204. void *ret;
  205. int gfp = GFP_ATOMIC | GFP_DMA;
  206. ret = (void *)__get_free_pages(gfp, get_order(size));
  207. if (ret != NULL) {
  208. memset(ret, 0, size);
  209. *dma_handle = virt_to_bus(ret);
  210. ret = (void *)KSEG0ADDR(ret);
  211. }
  212. return ret;
  213. }
  214. static void dma_free(void *vaddr, size_t size)
  215. {
  216. vaddr = (void *)KSEG0ADDR(vaddr);
  217. free_pages((unsigned long) vaddr, get_order(size));
  218. }
  219. static void setup_hw_rings(struct au1k_private *aup, u32 rx_base, u32 tx_base)
  220. {
  221. int i;
  222. for (i = 0; i < NUM_IR_DESC; i++) {
  223. aup->rx_ring[i] = (volatile struct ring_dest *)
  224. (rx_base + sizeof(struct ring_dest) * i);
  225. }
  226. for (i = 0; i < NUM_IR_DESC; i++) {
  227. aup->tx_ring[i] = (volatile struct ring_dest *)
  228. (tx_base + sizeof(struct ring_dest) * i);
  229. }
  230. }
  231. static int au1k_irda_init_iobuf(iobuff_t *io, int size)
  232. {
  233. io->head = kmalloc(size, GFP_KERNEL);
  234. if (io->head != NULL) {
  235. io->truesize = size;
  236. io->in_frame = FALSE;
  237. io->state = OUTSIDE_FRAME;
  238. io->data = io->head;
  239. }
  240. return io->head ? 0 : -ENOMEM;
  241. }
  242. /*
  243. * Set the IrDA communications speed.
  244. */
  245. static int au1k_irda_set_speed(struct net_device *dev, int speed)
  246. {
  247. struct au1k_private *aup = netdev_priv(dev);
  248. volatile struct ring_dest *ptxd;
  249. unsigned long control;
  250. int ret = 0, timeout = 10, i;
  251. if (speed == aup->speed)
  252. return ret;
  253. /* disable PHY first */
  254. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  255. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) & ~IR_PHYEN);
  256. /* disable RX/TX */
  257. irda_write(aup, IR_CONFIG_1,
  258. irda_read(aup, IR_CONFIG_1) & ~(IR_RX_ENABLE | IR_TX_ENABLE));
  259. msleep(20);
  260. while (irda_read(aup, IR_STATUS) & (IR_RX_STATUS | IR_TX_STATUS)) {
  261. msleep(20);
  262. if (!timeout--) {
  263. printk(KERN_ERR "%s: rx/tx disable timeout\n",
  264. dev->name);
  265. break;
  266. }
  267. }
  268. /* disable DMA */
  269. irda_write(aup, IR_CONFIG_1,
  270. irda_read(aup, IR_CONFIG_1) & ~IR_DMA_ENABLE);
  271. msleep(20);
  272. /* After we disable tx/rx. the index pointers go back to zero. */
  273. aup->tx_head = aup->tx_tail = aup->rx_head = 0;
  274. for (i = 0; i < NUM_IR_DESC; i++) {
  275. ptxd = aup->tx_ring[i];
  276. ptxd->flags = 0;
  277. ptxd->count_0 = 0;
  278. ptxd->count_1 = 0;
  279. }
  280. for (i = 0; i < NUM_IR_DESC; i++) {
  281. ptxd = aup->rx_ring[i];
  282. ptxd->count_0 = 0;
  283. ptxd->count_1 = 0;
  284. ptxd->flags = AU_OWN;
  285. }
  286. if (speed == 4000000)
  287. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_FIR);
  288. else
  289. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_SIR);
  290. switch (speed) {
  291. case 9600:
  292. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(11) | IR_PW(12));
  293. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  294. break;
  295. case 19200:
  296. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(5) | IR_PW(12));
  297. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  298. break;
  299. case 38400:
  300. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(2) | IR_PW(12));
  301. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  302. break;
  303. case 57600:
  304. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_BR(1) | IR_PW(12));
  305. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  306. break;
  307. case 115200:
  308. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_PW(12));
  309. irda_write(aup, IR_CONFIG_1, IR_SIR_MODE);
  310. break;
  311. case 4000000:
  312. irda_write(aup, IR_WRITE_PHY_CONFIG, IR_P(15));
  313. irda_write(aup, IR_CONFIG_1, IR_FIR | IR_DMA_ENABLE |
  314. IR_RX_ENABLE);
  315. break;
  316. default:
  317. printk(KERN_ERR "%s unsupported speed %x\n", dev->name, speed);
  318. ret = -EINVAL;
  319. break;
  320. }
  321. aup->speed = speed;
  322. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) | IR_PHYEN);
  323. control = irda_read(aup, IR_STATUS);
  324. irda_write(aup, IR_RING_PROMPT, 0);
  325. if (control & (1 << 14)) {
  326. printk(KERN_ERR "%s: configuration error\n", dev->name);
  327. } else {
  328. if (control & (1 << 11))
  329. printk(KERN_DEBUG "%s Valid SIR config\n", dev->name);
  330. if (control & (1 << 12))
  331. printk(KERN_DEBUG "%s Valid MIR config\n", dev->name);
  332. if (control & (1 << 13))
  333. printk(KERN_DEBUG "%s Valid FIR config\n", dev->name);
  334. if (control & (1 << 10))
  335. printk(KERN_DEBUG "%s TX enabled\n", dev->name);
  336. if (control & (1 << 9))
  337. printk(KERN_DEBUG "%s RX enabled\n", dev->name);
  338. }
  339. return ret;
  340. }
  341. static void update_rx_stats(struct net_device *dev, u32 status, u32 count)
  342. {
  343. struct net_device_stats *ps = &dev->stats;
  344. ps->rx_packets++;
  345. if (status & IR_RX_ERROR) {
  346. ps->rx_errors++;
  347. if (status & (IR_PHY_ERROR | IR_FIFO_OVER))
  348. ps->rx_missed_errors++;
  349. if (status & IR_MAX_LEN)
  350. ps->rx_length_errors++;
  351. if (status & IR_CRC_ERROR)
  352. ps->rx_crc_errors++;
  353. } else
  354. ps->rx_bytes += count;
  355. }
  356. static void update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
  357. {
  358. struct net_device_stats *ps = &dev->stats;
  359. ps->tx_packets++;
  360. ps->tx_bytes += pkt_len;
  361. if (status & IR_TX_ERROR) {
  362. ps->tx_errors++;
  363. ps->tx_aborted_errors++;
  364. }
  365. }
  366. static void au1k_tx_ack(struct net_device *dev)
  367. {
  368. struct au1k_private *aup = netdev_priv(dev);
  369. volatile struct ring_dest *ptxd;
  370. ptxd = aup->tx_ring[aup->tx_tail];
  371. while (!(ptxd->flags & AU_OWN) && (aup->tx_tail != aup->tx_head)) {
  372. update_tx_stats(dev, ptxd->flags,
  373. (ptxd->count_1 << 8) | ptxd->count_0);
  374. ptxd->count_0 = 0;
  375. ptxd->count_1 = 0;
  376. wmb();
  377. aup->tx_tail = (aup->tx_tail + 1) & (NUM_IR_DESC - 1);
  378. ptxd = aup->tx_ring[aup->tx_tail];
  379. if (aup->tx_full) {
  380. aup->tx_full = 0;
  381. netif_wake_queue(dev);
  382. }
  383. }
  384. if (aup->tx_tail == aup->tx_head) {
  385. if (aup->newspeed) {
  386. au1k_irda_set_speed(dev, aup->newspeed);
  387. aup->newspeed = 0;
  388. } else {
  389. irda_write(aup, IR_CONFIG_1,
  390. irda_read(aup, IR_CONFIG_1) & ~IR_TX_ENABLE);
  391. irda_write(aup, IR_CONFIG_1,
  392. irda_read(aup, IR_CONFIG_1) | IR_RX_ENABLE);
  393. irda_write(aup, IR_RING_PROMPT, 0);
  394. }
  395. }
  396. }
  397. static int au1k_irda_rx(struct net_device *dev)
  398. {
  399. struct au1k_private *aup = netdev_priv(dev);
  400. volatile struct ring_dest *prxd;
  401. struct sk_buff *skb;
  402. struct db_dest *pDB;
  403. u32 flags, count;
  404. prxd = aup->rx_ring[aup->rx_head];
  405. flags = prxd->flags;
  406. while (!(flags & AU_OWN)) {
  407. pDB = aup->rx_db_inuse[aup->rx_head];
  408. count = (prxd->count_1 << 8) | prxd->count_0;
  409. if (!(flags & IR_RX_ERROR)) {
  410. /* good frame */
  411. update_rx_stats(dev, flags, count);
  412. skb = alloc_skb(count + 1, GFP_ATOMIC);
  413. if (skb == NULL) {
  414. dev->stats.rx_dropped++;
  415. continue;
  416. }
  417. skb_reserve(skb, 1);
  418. if (aup->speed == 4000000)
  419. skb_put(skb, count);
  420. else
  421. skb_put(skb, count - 2);
  422. skb_copy_to_linear_data(skb, (void *)pDB->vaddr,
  423. count - 2);
  424. skb->dev = dev;
  425. skb_reset_mac_header(skb);
  426. skb->protocol = htons(ETH_P_IRDA);
  427. netif_rx(skb);
  428. prxd->count_0 = 0;
  429. prxd->count_1 = 0;
  430. }
  431. prxd->flags |= AU_OWN;
  432. aup->rx_head = (aup->rx_head + 1) & (NUM_IR_DESC - 1);
  433. irda_write(aup, IR_RING_PROMPT, 0);
  434. /* next descriptor */
  435. prxd = aup->rx_ring[aup->rx_head];
  436. flags = prxd->flags;
  437. }
  438. return 0;
  439. }
  440. static irqreturn_t au1k_irda_interrupt(int dummy, void *dev_id)
  441. {
  442. struct net_device *dev = dev_id;
  443. struct au1k_private *aup = netdev_priv(dev);
  444. irda_write(aup, IR_INT_CLEAR, 0); /* ack irda interrupts */
  445. au1k_irda_rx(dev);
  446. au1k_tx_ack(dev);
  447. return IRQ_HANDLED;
  448. }
  449. static int au1k_init(struct net_device *dev)
  450. {
  451. struct au1k_private *aup = netdev_priv(dev);
  452. u32 enable, ring_address;
  453. int i;
  454. enable = IR_HC | IR_CE | IR_C;
  455. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  456. enable |= IR_BE;
  457. #endif
  458. aup->tx_head = 0;
  459. aup->tx_tail = 0;
  460. aup->rx_head = 0;
  461. for (i = 0; i < NUM_IR_DESC; i++)
  462. aup->rx_ring[i]->flags = AU_OWN;
  463. irda_write(aup, IR_ENABLE, enable);
  464. msleep(20);
  465. /* disable PHY */
  466. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  467. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) & ~IR_PHYEN);
  468. msleep(20);
  469. irda_write(aup, IR_MAX_PKT_LEN, MAX_BUF_SIZE);
  470. ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
  471. irda_write(aup, IR_RING_BASE_ADDR_H, ring_address >> 26);
  472. irda_write(aup, IR_RING_BASE_ADDR_L, (ring_address >> 10) & 0xffff);
  473. irda_write(aup, IR_RING_SIZE,
  474. (RING_SIZE_64 << 8) | (RING_SIZE_64 << 12));
  475. irda_write(aup, IR_CONFIG_2, IR_PHYCLK_48MHZ | IR_ONE_PIN);
  476. irda_write(aup, IR_RING_ADDR_CMPR, 0);
  477. au1k_irda_set_speed(dev, 9600);
  478. return 0;
  479. }
  480. static int au1k_irda_start(struct net_device *dev)
  481. {
  482. struct au1k_private *aup = netdev_priv(dev);
  483. char hwname[32];
  484. int retval;
  485. retval = au1k_init(dev);
  486. if (retval) {
  487. printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
  488. return retval;
  489. }
  490. retval = request_irq(aup->irq_tx, &au1k_irda_interrupt, 0,
  491. dev->name, dev);
  492. if (retval) {
  493. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  494. dev->name, dev->irq);
  495. return retval;
  496. }
  497. retval = request_irq(aup->irq_rx, &au1k_irda_interrupt, 0,
  498. dev->name, dev);
  499. if (retval) {
  500. free_irq(aup->irq_tx, dev);
  501. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  502. dev->name, dev->irq);
  503. return retval;
  504. }
  505. /* Give self a hardware name */
  506. sprintf(hwname, "Au1000 SIR/FIR");
  507. aup->irlap = irlap_open(dev, &aup->qos, hwname);
  508. netif_start_queue(dev);
  509. /* int enable */
  510. irda_write(aup, IR_CONFIG_2, irda_read(aup, IR_CONFIG_2) | IR_IEN);
  511. /* power up */
  512. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_SIR);
  513. aup->timer.expires = RUN_AT((3 * HZ));
  514. aup->timer.data = (unsigned long)dev;
  515. return 0;
  516. }
  517. static int au1k_irda_stop(struct net_device *dev)
  518. {
  519. struct au1k_private *aup = netdev_priv(dev);
  520. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  521. /* disable interrupts */
  522. irda_write(aup, IR_CONFIG_2, irda_read(aup, IR_CONFIG_2) & ~IR_IEN);
  523. irda_write(aup, IR_CONFIG_1, 0);
  524. irda_write(aup, IR_ENABLE, 0); /* disable clock */
  525. if (aup->irlap) {
  526. irlap_close(aup->irlap);
  527. aup->irlap = NULL;
  528. }
  529. netif_stop_queue(dev);
  530. del_timer(&aup->timer);
  531. /* disable the interrupt */
  532. free_irq(aup->irq_tx, dev);
  533. free_irq(aup->irq_rx, dev);
  534. return 0;
  535. }
  536. /*
  537. * Au1000 transmit routine.
  538. */
  539. static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  540. {
  541. struct au1k_private *aup = netdev_priv(dev);
  542. int speed = irda_get_next_speed(skb);
  543. volatile struct ring_dest *ptxd;
  544. struct db_dest *pDB;
  545. u32 len, flags;
  546. if (speed != aup->speed && speed != -1)
  547. aup->newspeed = speed;
  548. if ((skb->len == 0) && (aup->newspeed)) {
  549. if (aup->tx_tail == aup->tx_head) {
  550. au1k_irda_set_speed(dev, speed);
  551. aup->newspeed = 0;
  552. }
  553. dev_kfree_skb(skb);
  554. return NETDEV_TX_OK;
  555. }
  556. ptxd = aup->tx_ring[aup->tx_head];
  557. flags = ptxd->flags;
  558. if (flags & AU_OWN) {
  559. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  560. netif_stop_queue(dev);
  561. aup->tx_full = 1;
  562. return 1;
  563. } else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
  564. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  565. netif_stop_queue(dev);
  566. aup->tx_full = 1;
  567. return 1;
  568. }
  569. pDB = aup->tx_db_inuse[aup->tx_head];
  570. #if 0
  571. if (irda_read(aup, IR_RX_BYTE_CNT) != 0) {
  572. printk(KERN_DEBUG "tx warning: rx byte cnt %x\n",
  573. irda_read(aup, IR_RX_BYTE_CNT));
  574. }
  575. #endif
  576. if (aup->speed == 4000000) {
  577. /* FIR */
  578. skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
  579. ptxd->count_0 = skb->len & 0xff;
  580. ptxd->count_1 = (skb->len >> 8) & 0xff;
  581. } else {
  582. /* SIR */
  583. len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
  584. ptxd->count_0 = len & 0xff;
  585. ptxd->count_1 = (len >> 8) & 0xff;
  586. ptxd->flags |= IR_DIS_CRC;
  587. }
  588. ptxd->flags |= AU_OWN;
  589. wmb();
  590. irda_write(aup, IR_CONFIG_1,
  591. irda_read(aup, IR_CONFIG_1) | IR_TX_ENABLE);
  592. irda_write(aup, IR_RING_PROMPT, 0);
  593. dev_kfree_skb(skb);
  594. aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
  595. return NETDEV_TX_OK;
  596. }
  597. /*
  598. * The Tx ring has been full longer than the watchdog timeout
  599. * value. The transmitter must be hung?
  600. */
  601. static void au1k_tx_timeout(struct net_device *dev)
  602. {
  603. u32 speed;
  604. struct au1k_private *aup = netdev_priv(dev);
  605. printk(KERN_ERR "%s: tx timeout\n", dev->name);
  606. speed = aup->speed;
  607. aup->speed = 0;
  608. au1k_irda_set_speed(dev, speed);
  609. aup->tx_full = 0;
  610. netif_wake_queue(dev);
  611. }
  612. static int au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  613. {
  614. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  615. struct au1k_private *aup = netdev_priv(dev);
  616. int ret = -EOPNOTSUPP;
  617. switch (cmd) {
  618. case SIOCSBANDWIDTH:
  619. if (capable(CAP_NET_ADMIN)) {
  620. /*
  621. * We are unable to set the speed if the
  622. * device is not running.
  623. */
  624. if (aup->open)
  625. ret = au1k_irda_set_speed(dev,
  626. rq->ifr_baudrate);
  627. else {
  628. printk(KERN_ERR "%s ioctl: !netif_running\n",
  629. dev->name);
  630. ret = 0;
  631. }
  632. }
  633. break;
  634. case SIOCSMEDIABUSY:
  635. ret = -EPERM;
  636. if (capable(CAP_NET_ADMIN)) {
  637. irda_device_set_media_busy(dev, TRUE);
  638. ret = 0;
  639. }
  640. break;
  641. case SIOCGRECEIVING:
  642. rq->ifr_receiving = 0;
  643. break;
  644. default:
  645. break;
  646. }
  647. return ret;
  648. }
  649. static const struct net_device_ops au1k_irda_netdev_ops = {
  650. .ndo_open = au1k_irda_start,
  651. .ndo_stop = au1k_irda_stop,
  652. .ndo_start_xmit = au1k_irda_hard_xmit,
  653. .ndo_tx_timeout = au1k_tx_timeout,
  654. .ndo_do_ioctl = au1k_irda_ioctl,
  655. };
  656. static int au1k_irda_net_init(struct net_device *dev)
  657. {
  658. struct au1k_private *aup = netdev_priv(dev);
  659. struct db_dest *pDB, *pDBfree;
  660. int i, err, retval = 0;
  661. dma_addr_t temp;
  662. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  663. if (err)
  664. goto out1;
  665. dev->netdev_ops = &au1k_irda_netdev_ops;
  666. irda_init_max_qos_capabilies(&aup->qos);
  667. /* The only value we must override it the baudrate */
  668. aup->qos.baud_rate.bits = IR_9600 | IR_19200 | IR_38400 |
  669. IR_57600 | IR_115200 | IR_576000 | (IR_4000000 << 8);
  670. aup->qos.min_turn_time.bits = qos_mtt_bits;
  671. irda_qos_bits_to_value(&aup->qos);
  672. retval = -ENOMEM;
  673. /* Tx ring follows rx ring + 512 bytes */
  674. /* we need a 1k aligned buffer */
  675. aup->rx_ring[0] = (struct ring_dest *)
  676. dma_alloc(2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)),
  677. &temp);
  678. if (!aup->rx_ring[0])
  679. goto out2;
  680. /* allocate the data buffers */
  681. aup->db[0].vaddr =
  682. dma_alloc(MAX_BUF_SIZE * 2 * NUM_IR_DESC, &temp);
  683. if (!aup->db[0].vaddr)
  684. goto out3;
  685. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  686. pDBfree = NULL;
  687. pDB = aup->db;
  688. for (i = 0; i < (2 * NUM_IR_DESC); i++) {
  689. pDB->pnext = pDBfree;
  690. pDBfree = pDB;
  691. pDB->vaddr =
  692. (u32 *)((unsigned)aup->db[0].vaddr + (MAX_BUF_SIZE * i));
  693. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  694. pDB++;
  695. }
  696. aup->pDBfree = pDBfree;
  697. /* attach a data buffer to each descriptor */
  698. for (i = 0; i < NUM_IR_DESC; i++) {
  699. pDB = GetFreeDB(aup);
  700. if (!pDB)
  701. goto out3;
  702. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  703. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr >> 8) & 0xff);
  704. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr >> 16) & 0xff);
  705. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr >> 24) & 0xff);
  706. aup->rx_db_inuse[i] = pDB;
  707. }
  708. for (i = 0; i < NUM_IR_DESC; i++) {
  709. pDB = GetFreeDB(aup);
  710. if (!pDB)
  711. goto out3;
  712. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  713. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr >> 8) & 0xff);
  714. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr >> 16) & 0xff);
  715. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr >> 24) & 0xff);
  716. aup->tx_ring[i]->count_0 = 0;
  717. aup->tx_ring[i]->count_1 = 0;
  718. aup->tx_ring[i]->flags = 0;
  719. aup->tx_db_inuse[i] = pDB;
  720. }
  721. return 0;
  722. out3:
  723. dma_free((void *)aup->rx_ring[0],
  724. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  725. out2:
  726. kfree(aup->rx_buff.head);
  727. out1:
  728. printk(KERN_ERR "au1k_irda_net_init() failed. Returns %d\n", retval);
  729. return retval;
  730. }
  731. static int au1k_irda_probe(struct platform_device *pdev)
  732. {
  733. struct au1k_private *aup;
  734. struct net_device *dev;
  735. struct resource *r;
  736. int err;
  737. dev = alloc_irdadev(sizeof(struct au1k_private));
  738. if (!dev)
  739. return -ENOMEM;
  740. aup = netdev_priv(dev);
  741. aup->platdata = pdev->dev.platform_data;
  742. err = -EINVAL;
  743. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  744. if (!r)
  745. goto out;
  746. aup->irq_tx = r->start;
  747. r = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  748. if (!r)
  749. goto out;
  750. aup->irq_rx = r->start;
  751. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  752. if (!r)
  753. goto out;
  754. err = -EBUSY;
  755. aup->ioarea = request_mem_region(r->start, resource_size(r),
  756. pdev->name);
  757. if (!aup->ioarea)
  758. goto out;
  759. aup->iobase = ioremap_nocache(r->start, resource_size(r));
  760. if (!aup->iobase)
  761. goto out2;
  762. dev->irq = aup->irq_rx;
  763. err = au1k_irda_net_init(dev);
  764. if (err)
  765. goto out3;
  766. err = register_netdev(dev);
  767. if (err)
  768. goto out4;
  769. platform_set_drvdata(pdev, dev);
  770. printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
  771. return 0;
  772. out4:
  773. dma_free((void *)aup->db[0].vaddr,
  774. MAX_BUF_SIZE * 2 * NUM_IR_DESC);
  775. dma_free((void *)aup->rx_ring[0],
  776. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  777. kfree(aup->rx_buff.head);
  778. out3:
  779. iounmap(aup->iobase);
  780. out2:
  781. release_resource(aup->ioarea);
  782. kfree(aup->ioarea);
  783. out:
  784. free_netdev(dev);
  785. return err;
  786. }
  787. static int au1k_irda_remove(struct platform_device *pdev)
  788. {
  789. struct net_device *dev = platform_get_drvdata(pdev);
  790. struct au1k_private *aup = netdev_priv(dev);
  791. unregister_netdev(dev);
  792. dma_free((void *)aup->db[0].vaddr,
  793. MAX_BUF_SIZE * 2 * NUM_IR_DESC);
  794. dma_free((void *)aup->rx_ring[0],
  795. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  796. kfree(aup->rx_buff.head);
  797. iounmap(aup->iobase);
  798. release_resource(aup->ioarea);
  799. kfree(aup->ioarea);
  800. free_netdev(dev);
  801. return 0;
  802. }
  803. static struct platform_driver au1k_irda_driver = {
  804. .driver = {
  805. .name = "au1000-irda",
  806. .owner = THIS_MODULE,
  807. },
  808. .probe = au1k_irda_probe,
  809. .remove = au1k_irda_remove,
  810. };
  811. module_platform_driver(au1k_irda_driver);
  812. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  813. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");