au1k_ir.c 23 KB

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