au1k_ir.c 24 KB

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