au1k_ir.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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/time.h>
  27. #include <linux/types.h>
  28. #include <linux/ioport.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. struct clk *irda_clk;
  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, phyck;
  453. struct clk *c;
  454. int i;
  455. c = clk_get(NULL, "irda_clk");
  456. if (IS_ERR(c))
  457. return PTR_ERR(c);
  458. i = clk_prepare_enable(c);
  459. if (i) {
  460. clk_put(c);
  461. return i;
  462. }
  463. switch (clk_get_rate(c)) {
  464. case 40000000:
  465. phyck = IR_PHYCLK_40MHZ;
  466. break;
  467. case 48000000:
  468. phyck = IR_PHYCLK_48MHZ;
  469. break;
  470. case 56000000:
  471. phyck = IR_PHYCLK_56MHZ;
  472. break;
  473. case 64000000:
  474. phyck = IR_PHYCLK_64MHZ;
  475. break;
  476. default:
  477. clk_disable_unprepare(c);
  478. clk_put(c);
  479. return -EINVAL;
  480. }
  481. aup->irda_clk = c;
  482. enable = IR_HC | IR_CE | IR_C;
  483. #ifndef CONFIG_CPU_LITTLE_ENDIAN
  484. enable |= IR_BE;
  485. #endif
  486. aup->tx_head = 0;
  487. aup->tx_tail = 0;
  488. aup->rx_head = 0;
  489. for (i = 0; i < NUM_IR_DESC; i++)
  490. aup->rx_ring[i]->flags = AU_OWN;
  491. irda_write(aup, IR_ENABLE, enable);
  492. msleep(20);
  493. /* disable PHY */
  494. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  495. irda_write(aup, IR_STATUS, irda_read(aup, IR_STATUS) & ~IR_PHYEN);
  496. msleep(20);
  497. irda_write(aup, IR_MAX_PKT_LEN, MAX_BUF_SIZE);
  498. ring_address = (u32)virt_to_phys((void *)aup->rx_ring[0]);
  499. irda_write(aup, IR_RING_BASE_ADDR_H, ring_address >> 26);
  500. irda_write(aup, IR_RING_BASE_ADDR_L, (ring_address >> 10) & 0xffff);
  501. irda_write(aup, IR_RING_SIZE,
  502. (RING_SIZE_64 << 8) | (RING_SIZE_64 << 12));
  503. irda_write(aup, IR_CONFIG_2, phyck | IR_ONE_PIN);
  504. irda_write(aup, IR_RING_ADDR_CMPR, 0);
  505. au1k_irda_set_speed(dev, 9600);
  506. return 0;
  507. }
  508. static int au1k_irda_start(struct net_device *dev)
  509. {
  510. struct au1k_private *aup = netdev_priv(dev);
  511. char hwname[32];
  512. int retval;
  513. retval = au1k_init(dev);
  514. if (retval) {
  515. printk(KERN_ERR "%s: error in au1k_init\n", dev->name);
  516. return retval;
  517. }
  518. retval = request_irq(aup->irq_tx, &au1k_irda_interrupt, 0,
  519. dev->name, dev);
  520. if (retval) {
  521. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  522. dev->name, dev->irq);
  523. return retval;
  524. }
  525. retval = request_irq(aup->irq_rx, &au1k_irda_interrupt, 0,
  526. dev->name, dev);
  527. if (retval) {
  528. free_irq(aup->irq_tx, dev);
  529. printk(KERN_ERR "%s: unable to get IRQ %d\n",
  530. dev->name, dev->irq);
  531. return retval;
  532. }
  533. /* Give self a hardware name */
  534. sprintf(hwname, "Au1000 SIR/FIR");
  535. aup->irlap = irlap_open(dev, &aup->qos, hwname);
  536. netif_start_queue(dev);
  537. /* int enable */
  538. irda_write(aup, IR_CONFIG_2, irda_read(aup, IR_CONFIG_2) | IR_IEN);
  539. /* power up */
  540. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_SIR);
  541. aup->timer.expires = RUN_AT((3 * HZ));
  542. aup->timer.data = (unsigned long)dev;
  543. return 0;
  544. }
  545. static int au1k_irda_stop(struct net_device *dev)
  546. {
  547. struct au1k_private *aup = netdev_priv(dev);
  548. au1k_irda_plat_set_phy_mode(aup, AU1000_IRDA_PHY_MODE_OFF);
  549. /* disable interrupts */
  550. irda_write(aup, IR_CONFIG_2, irda_read(aup, IR_CONFIG_2) & ~IR_IEN);
  551. irda_write(aup, IR_CONFIG_1, 0);
  552. irda_write(aup, IR_ENABLE, 0); /* disable clock */
  553. if (aup->irlap) {
  554. irlap_close(aup->irlap);
  555. aup->irlap = NULL;
  556. }
  557. netif_stop_queue(dev);
  558. del_timer(&aup->timer);
  559. /* disable the interrupt */
  560. free_irq(aup->irq_tx, dev);
  561. free_irq(aup->irq_rx, dev);
  562. clk_disable_unprepare(aup->irda_clk);
  563. clk_put(aup->irda_clk);
  564. return 0;
  565. }
  566. /*
  567. * Au1000 transmit routine.
  568. */
  569. static int au1k_irda_hard_xmit(struct sk_buff *skb, struct net_device *dev)
  570. {
  571. struct au1k_private *aup = netdev_priv(dev);
  572. int speed = irda_get_next_speed(skb);
  573. volatile struct ring_dest *ptxd;
  574. struct db_dest *pDB;
  575. u32 len, flags;
  576. if (speed != aup->speed && speed != -1)
  577. aup->newspeed = speed;
  578. if ((skb->len == 0) && (aup->newspeed)) {
  579. if (aup->tx_tail == aup->tx_head) {
  580. au1k_irda_set_speed(dev, speed);
  581. aup->newspeed = 0;
  582. }
  583. dev_kfree_skb(skb);
  584. return NETDEV_TX_OK;
  585. }
  586. ptxd = aup->tx_ring[aup->tx_head];
  587. flags = ptxd->flags;
  588. if (flags & AU_OWN) {
  589. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  590. netif_stop_queue(dev);
  591. aup->tx_full = 1;
  592. return 1;
  593. } else if (((aup->tx_head + 1) & (NUM_IR_DESC - 1)) == aup->tx_tail) {
  594. printk(KERN_DEBUG "%s: tx_full\n", dev->name);
  595. netif_stop_queue(dev);
  596. aup->tx_full = 1;
  597. return 1;
  598. }
  599. pDB = aup->tx_db_inuse[aup->tx_head];
  600. #if 0
  601. if (irda_read(aup, IR_RX_BYTE_CNT) != 0) {
  602. printk(KERN_DEBUG "tx warning: rx byte cnt %x\n",
  603. irda_read(aup, IR_RX_BYTE_CNT));
  604. }
  605. #endif
  606. if (aup->speed == 4000000) {
  607. /* FIR */
  608. skb_copy_from_linear_data(skb, (void *)pDB->vaddr, skb->len);
  609. ptxd->count_0 = skb->len & 0xff;
  610. ptxd->count_1 = (skb->len >> 8) & 0xff;
  611. } else {
  612. /* SIR */
  613. len = async_wrap_skb(skb, (u8 *)pDB->vaddr, MAX_BUF_SIZE);
  614. ptxd->count_0 = len & 0xff;
  615. ptxd->count_1 = (len >> 8) & 0xff;
  616. ptxd->flags |= IR_DIS_CRC;
  617. }
  618. ptxd->flags |= AU_OWN;
  619. wmb();
  620. irda_write(aup, IR_CONFIG_1,
  621. irda_read(aup, IR_CONFIG_1) | IR_TX_ENABLE);
  622. irda_write(aup, IR_RING_PROMPT, 0);
  623. dev_kfree_skb(skb);
  624. aup->tx_head = (aup->tx_head + 1) & (NUM_IR_DESC - 1);
  625. return NETDEV_TX_OK;
  626. }
  627. /*
  628. * The Tx ring has been full longer than the watchdog timeout
  629. * value. The transmitter must be hung?
  630. */
  631. static void au1k_tx_timeout(struct net_device *dev)
  632. {
  633. u32 speed;
  634. struct au1k_private *aup = netdev_priv(dev);
  635. printk(KERN_ERR "%s: tx timeout\n", dev->name);
  636. speed = aup->speed;
  637. aup->speed = 0;
  638. au1k_irda_set_speed(dev, speed);
  639. aup->tx_full = 0;
  640. netif_wake_queue(dev);
  641. }
  642. static int au1k_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
  643. {
  644. struct if_irda_req *rq = (struct if_irda_req *)ifreq;
  645. struct au1k_private *aup = netdev_priv(dev);
  646. int ret = -EOPNOTSUPP;
  647. switch (cmd) {
  648. case SIOCSBANDWIDTH:
  649. if (capable(CAP_NET_ADMIN)) {
  650. /*
  651. * We are unable to set the speed if the
  652. * device is not running.
  653. */
  654. if (aup->open)
  655. ret = au1k_irda_set_speed(dev,
  656. rq->ifr_baudrate);
  657. else {
  658. printk(KERN_ERR "%s ioctl: !netif_running\n",
  659. dev->name);
  660. ret = 0;
  661. }
  662. }
  663. break;
  664. case SIOCSMEDIABUSY:
  665. ret = -EPERM;
  666. if (capable(CAP_NET_ADMIN)) {
  667. irda_device_set_media_busy(dev, TRUE);
  668. ret = 0;
  669. }
  670. break;
  671. case SIOCGRECEIVING:
  672. rq->ifr_receiving = 0;
  673. break;
  674. default:
  675. break;
  676. }
  677. return ret;
  678. }
  679. static const struct net_device_ops au1k_irda_netdev_ops = {
  680. .ndo_open = au1k_irda_start,
  681. .ndo_stop = au1k_irda_stop,
  682. .ndo_start_xmit = au1k_irda_hard_xmit,
  683. .ndo_tx_timeout = au1k_tx_timeout,
  684. .ndo_do_ioctl = au1k_irda_ioctl,
  685. };
  686. static int au1k_irda_net_init(struct net_device *dev)
  687. {
  688. struct au1k_private *aup = netdev_priv(dev);
  689. struct db_dest *pDB, *pDBfree;
  690. int i, err, retval = 0;
  691. dma_addr_t temp;
  692. err = au1k_irda_init_iobuf(&aup->rx_buff, 14384);
  693. if (err)
  694. goto out1;
  695. dev->netdev_ops = &au1k_irda_netdev_ops;
  696. irda_init_max_qos_capabilies(&aup->qos);
  697. /* The only value we must override it the baudrate */
  698. aup->qos.baud_rate.bits = IR_9600 | IR_19200 | IR_38400 |
  699. IR_57600 | IR_115200 | IR_576000 | (IR_4000000 << 8);
  700. aup->qos.min_turn_time.bits = qos_mtt_bits;
  701. irda_qos_bits_to_value(&aup->qos);
  702. retval = -ENOMEM;
  703. /* Tx ring follows rx ring + 512 bytes */
  704. /* we need a 1k aligned buffer */
  705. aup->rx_ring[0] = (struct ring_dest *)
  706. dma_alloc(2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)),
  707. &temp);
  708. if (!aup->rx_ring[0])
  709. goto out2;
  710. /* allocate the data buffers */
  711. aup->db[0].vaddr =
  712. dma_alloc(MAX_BUF_SIZE * 2 * NUM_IR_DESC, &temp);
  713. if (!aup->db[0].vaddr)
  714. goto out3;
  715. setup_hw_rings(aup, (u32)aup->rx_ring[0], (u32)aup->rx_ring[0] + 512);
  716. pDBfree = NULL;
  717. pDB = aup->db;
  718. for (i = 0; i < (2 * NUM_IR_DESC); i++) {
  719. pDB->pnext = pDBfree;
  720. pDBfree = pDB;
  721. pDB->vaddr =
  722. (u32 *)((unsigned)aup->db[0].vaddr + (MAX_BUF_SIZE * i));
  723. pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
  724. pDB++;
  725. }
  726. aup->pDBfree = pDBfree;
  727. /* attach a data buffer to each descriptor */
  728. for (i = 0; i < NUM_IR_DESC; i++) {
  729. pDB = GetFreeDB(aup);
  730. if (!pDB)
  731. goto out3;
  732. aup->rx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  733. aup->rx_ring[i]->addr_1 = (u8)((pDB->dma_addr >> 8) & 0xff);
  734. aup->rx_ring[i]->addr_2 = (u8)((pDB->dma_addr >> 16) & 0xff);
  735. aup->rx_ring[i]->addr_3 = (u8)((pDB->dma_addr >> 24) & 0xff);
  736. aup->rx_db_inuse[i] = pDB;
  737. }
  738. for (i = 0; i < NUM_IR_DESC; i++) {
  739. pDB = GetFreeDB(aup);
  740. if (!pDB)
  741. goto out3;
  742. aup->tx_ring[i]->addr_0 = (u8)(pDB->dma_addr & 0xff);
  743. aup->tx_ring[i]->addr_1 = (u8)((pDB->dma_addr >> 8) & 0xff);
  744. aup->tx_ring[i]->addr_2 = (u8)((pDB->dma_addr >> 16) & 0xff);
  745. aup->tx_ring[i]->addr_3 = (u8)((pDB->dma_addr >> 24) & 0xff);
  746. aup->tx_ring[i]->count_0 = 0;
  747. aup->tx_ring[i]->count_1 = 0;
  748. aup->tx_ring[i]->flags = 0;
  749. aup->tx_db_inuse[i] = pDB;
  750. }
  751. return 0;
  752. out3:
  753. dma_free((void *)aup->rx_ring[0],
  754. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  755. out2:
  756. kfree(aup->rx_buff.head);
  757. out1:
  758. printk(KERN_ERR "au1k_irda_net_init() failed. Returns %d\n", retval);
  759. return retval;
  760. }
  761. static int au1k_irda_probe(struct platform_device *pdev)
  762. {
  763. struct au1k_private *aup;
  764. struct net_device *dev;
  765. struct resource *r;
  766. struct clk *c;
  767. int err;
  768. dev = alloc_irdadev(sizeof(struct au1k_private));
  769. if (!dev)
  770. return -ENOMEM;
  771. aup = netdev_priv(dev);
  772. aup->platdata = pdev->dev.platform_data;
  773. err = -EINVAL;
  774. r = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  775. if (!r)
  776. goto out;
  777. aup->irq_tx = r->start;
  778. r = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  779. if (!r)
  780. goto out;
  781. aup->irq_rx = r->start;
  782. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  783. if (!r)
  784. goto out;
  785. err = -EBUSY;
  786. aup->ioarea = request_mem_region(r->start, resource_size(r),
  787. pdev->name);
  788. if (!aup->ioarea)
  789. goto out;
  790. /* bail out early if clock doesn't exist */
  791. c = clk_get(NULL, "irda_clk");
  792. if (IS_ERR(c)) {
  793. err = PTR_ERR(c);
  794. goto out;
  795. }
  796. clk_put(c);
  797. aup->iobase = ioremap_nocache(r->start, resource_size(r));
  798. if (!aup->iobase)
  799. goto out2;
  800. dev->irq = aup->irq_rx;
  801. err = au1k_irda_net_init(dev);
  802. if (err)
  803. goto out3;
  804. err = register_netdev(dev);
  805. if (err)
  806. goto out4;
  807. platform_set_drvdata(pdev, dev);
  808. printk(KERN_INFO "IrDA: Registered device %s\n", dev->name);
  809. return 0;
  810. out4:
  811. dma_free((void *)aup->db[0].vaddr,
  812. MAX_BUF_SIZE * 2 * NUM_IR_DESC);
  813. dma_free((void *)aup->rx_ring[0],
  814. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  815. kfree(aup->rx_buff.head);
  816. out3:
  817. iounmap(aup->iobase);
  818. out2:
  819. release_resource(aup->ioarea);
  820. kfree(aup->ioarea);
  821. out:
  822. free_netdev(dev);
  823. return err;
  824. }
  825. static int au1k_irda_remove(struct platform_device *pdev)
  826. {
  827. struct net_device *dev = platform_get_drvdata(pdev);
  828. struct au1k_private *aup = netdev_priv(dev);
  829. unregister_netdev(dev);
  830. dma_free((void *)aup->db[0].vaddr,
  831. MAX_BUF_SIZE * 2 * NUM_IR_DESC);
  832. dma_free((void *)aup->rx_ring[0],
  833. 2 * MAX_NUM_IR_DESC * (sizeof(struct ring_dest)));
  834. kfree(aup->rx_buff.head);
  835. iounmap(aup->iobase);
  836. release_resource(aup->ioarea);
  837. kfree(aup->ioarea);
  838. free_netdev(dev);
  839. return 0;
  840. }
  841. static struct platform_driver au1k_irda_driver = {
  842. .driver = {
  843. .name = "au1000-irda",
  844. .owner = THIS_MODULE,
  845. },
  846. .probe = au1k_irda_probe,
  847. .remove = au1k_irda_remove,
  848. };
  849. module_platform_driver(au1k_irda_driver);
  850. MODULE_AUTHOR("Pete Popov <ppopov@mvista.com>");
  851. MODULE_DESCRIPTION("Au1000 IrDA Device Driver");