ks8842.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. /*
  2. * ks8842.c timberdale KS8842 ethernet driver
  3. * Copyright (c) 2009 Intel Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * The Micrel KS8842 behind the timberdale FPGA
  20. * The genuine Micrel KS8841/42 device with ISA 16/32bit bus interface
  21. */
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/ethtool.h>
  30. #include <linux/ks8842.h>
  31. #include <linux/dmaengine.h>
  32. #include <linux/dma-mapping.h>
  33. #include <linux/scatterlist.h>
  34. #define DRV_NAME "ks8842"
  35. /* Timberdale specific Registers */
  36. #define REG_TIMB_RST 0x1c
  37. #define REG_TIMB_FIFO 0x20
  38. #define REG_TIMB_ISR 0x24
  39. #define REG_TIMB_IER 0x28
  40. #define REG_TIMB_IAR 0x2C
  41. #define REQ_TIMB_DMA_RESUME 0x30
  42. /* KS8842 registers */
  43. #define REG_SELECT_BANK 0x0e
  44. /* bank 0 registers */
  45. #define REG_QRFCR 0x04
  46. /* bank 2 registers */
  47. #define REG_MARL 0x00
  48. #define REG_MARM 0x02
  49. #define REG_MARH 0x04
  50. /* bank 3 registers */
  51. #define REG_GRR 0x06
  52. /* bank 16 registers */
  53. #define REG_TXCR 0x00
  54. #define REG_TXSR 0x02
  55. #define REG_RXCR 0x04
  56. #define REG_TXMIR 0x08
  57. #define REG_RXMIR 0x0A
  58. /* bank 17 registers */
  59. #define REG_TXQCR 0x00
  60. #define REG_RXQCR 0x02
  61. #define REG_TXFDPR 0x04
  62. #define REG_RXFDPR 0x06
  63. #define REG_QMU_DATA_LO 0x08
  64. #define REG_QMU_DATA_HI 0x0A
  65. /* bank 18 registers */
  66. #define REG_IER 0x00
  67. #define IRQ_LINK_CHANGE 0x8000
  68. #define IRQ_TX 0x4000
  69. #define IRQ_RX 0x2000
  70. #define IRQ_RX_OVERRUN 0x0800
  71. #define IRQ_TX_STOPPED 0x0200
  72. #define IRQ_RX_STOPPED 0x0100
  73. #define IRQ_RX_ERROR 0x0080
  74. #define ENABLED_IRQS (IRQ_LINK_CHANGE | IRQ_TX | IRQ_RX | IRQ_RX_STOPPED | \
  75. IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
  76. /* When running via timberdale in DMA mode, the RX interrupt should be
  77. enabled in the KS8842, but not in the FPGA IP, since the IP handles
  78. RX DMA internally.
  79. TX interrupts are not needed it is handled by the FPGA the driver is
  80. notified via DMA callbacks.
  81. */
  82. #define ENABLED_IRQS_DMA_IP (IRQ_LINK_CHANGE | IRQ_RX_STOPPED | \
  83. IRQ_TX_STOPPED | IRQ_RX_OVERRUN | IRQ_RX_ERROR)
  84. #define ENABLED_IRQS_DMA (ENABLED_IRQS_DMA_IP | IRQ_RX)
  85. #define REG_ISR 0x02
  86. #define REG_RXSR 0x04
  87. #define RXSR_VALID 0x8000
  88. #define RXSR_BROADCAST 0x80
  89. #define RXSR_MULTICAST 0x40
  90. #define RXSR_UNICAST 0x20
  91. #define RXSR_FRAMETYPE 0x08
  92. #define RXSR_TOO_LONG 0x04
  93. #define RXSR_RUNT 0x02
  94. #define RXSR_CRC_ERROR 0x01
  95. #define RXSR_ERROR (RXSR_TOO_LONG | RXSR_RUNT | RXSR_CRC_ERROR)
  96. /* bank 32 registers */
  97. #define REG_SW_ID_AND_ENABLE 0x00
  98. #define REG_SGCR1 0x02
  99. #define REG_SGCR2 0x04
  100. #define REG_SGCR3 0x06
  101. /* bank 39 registers */
  102. #define REG_MACAR1 0x00
  103. #define REG_MACAR2 0x02
  104. #define REG_MACAR3 0x04
  105. /* bank 45 registers */
  106. #define REG_P1MBCR 0x00
  107. #define REG_P1MBSR 0x02
  108. /* bank 46 registers */
  109. #define REG_P2MBCR 0x00
  110. #define REG_P2MBSR 0x02
  111. /* bank 48 registers */
  112. #define REG_P1CR2 0x02
  113. /* bank 49 registers */
  114. #define REG_P1CR4 0x02
  115. #define REG_P1SR 0x04
  116. /* flags passed by platform_device for configuration */
  117. #define MICREL_KS884X 0x01 /* 0=Timeberdale(FPGA), 1=Micrel */
  118. #define KS884X_16BIT 0x02 /* 1=16bit, 0=32bit */
  119. #define DMA_BUFFER_SIZE 2048
  120. struct ks8842_tx_dma_ctl {
  121. struct dma_chan *chan;
  122. struct dma_async_tx_descriptor *adesc;
  123. void *buf;
  124. struct scatterlist sg;
  125. int channel;
  126. };
  127. struct ks8842_rx_dma_ctl {
  128. struct dma_chan *chan;
  129. struct dma_async_tx_descriptor *adesc;
  130. struct sk_buff *skb;
  131. struct scatterlist sg;
  132. struct tasklet_struct tasklet;
  133. int channel;
  134. };
  135. #define KS8842_USE_DMA(adapter) (((adapter)->dma_tx.channel != -1) && \
  136. ((adapter)->dma_rx.channel != -1))
  137. struct ks8842_adapter {
  138. void __iomem *hw_addr;
  139. int irq;
  140. unsigned long conf_flags; /* copy of platform_device config */
  141. struct tasklet_struct tasklet;
  142. spinlock_t lock; /* spinlock to be interrupt safe */
  143. struct work_struct timeout_work;
  144. struct net_device *netdev;
  145. struct device *dev;
  146. struct ks8842_tx_dma_ctl dma_tx;
  147. struct ks8842_rx_dma_ctl dma_rx;
  148. };
  149. static void ks8842_dma_rx_cb(void *data);
  150. static void ks8842_dma_tx_cb(void *data);
  151. static inline void ks8842_resume_dma(struct ks8842_adapter *adapter)
  152. {
  153. iowrite32(1, adapter->hw_addr + REQ_TIMB_DMA_RESUME);
  154. }
  155. static inline void ks8842_select_bank(struct ks8842_adapter *adapter, u16 bank)
  156. {
  157. iowrite16(bank, adapter->hw_addr + REG_SELECT_BANK);
  158. }
  159. static inline void ks8842_write8(struct ks8842_adapter *adapter, u16 bank,
  160. u8 value, int offset)
  161. {
  162. ks8842_select_bank(adapter, bank);
  163. iowrite8(value, adapter->hw_addr + offset);
  164. }
  165. static inline void ks8842_write16(struct ks8842_adapter *adapter, u16 bank,
  166. u16 value, int offset)
  167. {
  168. ks8842_select_bank(adapter, bank);
  169. iowrite16(value, adapter->hw_addr + offset);
  170. }
  171. static inline void ks8842_enable_bits(struct ks8842_adapter *adapter, u16 bank,
  172. u16 bits, int offset)
  173. {
  174. u16 reg;
  175. ks8842_select_bank(adapter, bank);
  176. reg = ioread16(adapter->hw_addr + offset);
  177. reg |= bits;
  178. iowrite16(reg, adapter->hw_addr + offset);
  179. }
  180. static inline void ks8842_clear_bits(struct ks8842_adapter *adapter, u16 bank,
  181. u16 bits, int offset)
  182. {
  183. u16 reg;
  184. ks8842_select_bank(adapter, bank);
  185. reg = ioread16(adapter->hw_addr + offset);
  186. reg &= ~bits;
  187. iowrite16(reg, adapter->hw_addr + offset);
  188. }
  189. static inline void ks8842_write32(struct ks8842_adapter *adapter, u16 bank,
  190. u32 value, int offset)
  191. {
  192. ks8842_select_bank(adapter, bank);
  193. iowrite32(value, adapter->hw_addr + offset);
  194. }
  195. static inline u8 ks8842_read8(struct ks8842_adapter *adapter, u16 bank,
  196. int offset)
  197. {
  198. ks8842_select_bank(adapter, bank);
  199. return ioread8(adapter->hw_addr + offset);
  200. }
  201. static inline u16 ks8842_read16(struct ks8842_adapter *adapter, u16 bank,
  202. int offset)
  203. {
  204. ks8842_select_bank(adapter, bank);
  205. return ioread16(adapter->hw_addr + offset);
  206. }
  207. static inline u32 ks8842_read32(struct ks8842_adapter *adapter, u16 bank,
  208. int offset)
  209. {
  210. ks8842_select_bank(adapter, bank);
  211. return ioread32(adapter->hw_addr + offset);
  212. }
  213. static void ks8842_reset(struct ks8842_adapter *adapter)
  214. {
  215. if (adapter->conf_flags & MICREL_KS884X) {
  216. ks8842_write16(adapter, 3, 1, REG_GRR);
  217. msleep(10);
  218. iowrite16(0, adapter->hw_addr + REG_GRR);
  219. } else {
  220. /* The KS8842 goes haywire when doing softare reset
  221. * a work around in the timberdale IP is implemented to
  222. * do a hardware reset instead
  223. ks8842_write16(adapter, 3, 1, REG_GRR);
  224. msleep(10);
  225. iowrite16(0, adapter->hw_addr + REG_GRR);
  226. */
  227. iowrite32(0x1, adapter->hw_addr + REG_TIMB_RST);
  228. msleep(20);
  229. }
  230. }
  231. static void ks8842_update_link_status(struct net_device *netdev,
  232. struct ks8842_adapter *adapter)
  233. {
  234. /* check the status of the link */
  235. if (ks8842_read16(adapter, 45, REG_P1MBSR) & 0x4) {
  236. netif_carrier_on(netdev);
  237. netif_wake_queue(netdev);
  238. } else {
  239. netif_stop_queue(netdev);
  240. netif_carrier_off(netdev);
  241. }
  242. }
  243. static void ks8842_enable_tx(struct ks8842_adapter *adapter)
  244. {
  245. ks8842_enable_bits(adapter, 16, 0x01, REG_TXCR);
  246. }
  247. static void ks8842_disable_tx(struct ks8842_adapter *adapter)
  248. {
  249. ks8842_clear_bits(adapter, 16, 0x01, REG_TXCR);
  250. }
  251. static void ks8842_enable_rx(struct ks8842_adapter *adapter)
  252. {
  253. ks8842_enable_bits(adapter, 16, 0x01, REG_RXCR);
  254. }
  255. static void ks8842_disable_rx(struct ks8842_adapter *adapter)
  256. {
  257. ks8842_clear_bits(adapter, 16, 0x01, REG_RXCR);
  258. }
  259. static void ks8842_reset_hw(struct ks8842_adapter *adapter)
  260. {
  261. /* reset the HW */
  262. ks8842_reset(adapter);
  263. /* Enable QMU Transmit flow control / transmit padding / Transmit CRC */
  264. ks8842_write16(adapter, 16, 0x000E, REG_TXCR);
  265. /* enable the receiver, uni + multi + broadcast + flow ctrl
  266. + crc strip */
  267. ks8842_write16(adapter, 16, 0x8 | 0x20 | 0x40 | 0x80 | 0x400,
  268. REG_RXCR);
  269. /* TX frame pointer autoincrement */
  270. ks8842_write16(adapter, 17, 0x4000, REG_TXFDPR);
  271. /* RX frame pointer autoincrement */
  272. ks8842_write16(adapter, 17, 0x4000, REG_RXFDPR);
  273. /* RX 2 kb high watermark */
  274. ks8842_write16(adapter, 0, 0x1000, REG_QRFCR);
  275. /* aggressive back off in half duplex */
  276. ks8842_enable_bits(adapter, 32, 1 << 8, REG_SGCR1);
  277. /* enable no excessive collison drop */
  278. ks8842_enable_bits(adapter, 32, 1 << 3, REG_SGCR2);
  279. /* Enable port 1 force flow control / back pressure / transmit / recv */
  280. ks8842_write16(adapter, 48, 0x1E07, REG_P1CR2);
  281. /* restart port auto-negotiation */
  282. ks8842_enable_bits(adapter, 49, 1 << 13, REG_P1CR4);
  283. /* Enable the transmitter */
  284. ks8842_enable_tx(adapter);
  285. /* Enable the receiver */
  286. ks8842_enable_rx(adapter);
  287. /* clear all interrupts */
  288. ks8842_write16(adapter, 18, 0xffff, REG_ISR);
  289. /* enable interrupts */
  290. if (KS8842_USE_DMA(adapter)) {
  291. /* When running in DMA Mode the RX interrupt is not enabled in
  292. timberdale because RX data is received by DMA callbacks
  293. it must still be enabled in the KS8842 because it indicates
  294. to timberdale when there is RX data for it's DMA FIFOs */
  295. iowrite16(ENABLED_IRQS_DMA_IP, adapter->hw_addr + REG_TIMB_IER);
  296. ks8842_write16(adapter, 18, ENABLED_IRQS_DMA, REG_IER);
  297. } else {
  298. if (!(adapter->conf_flags & MICREL_KS884X))
  299. iowrite16(ENABLED_IRQS,
  300. adapter->hw_addr + REG_TIMB_IER);
  301. ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
  302. }
  303. /* enable the switch */
  304. ks8842_write16(adapter, 32, 0x1, REG_SW_ID_AND_ENABLE);
  305. }
  306. static void ks8842_read_mac_addr(struct ks8842_adapter *adapter, u8 *dest)
  307. {
  308. int i;
  309. u16 mac;
  310. for (i = 0; i < ETH_ALEN; i++)
  311. dest[ETH_ALEN - i - 1] = ks8842_read8(adapter, 2, REG_MARL + i);
  312. if (adapter->conf_flags & MICREL_KS884X) {
  313. /*
  314. the sequence of saving mac addr between MAC and Switch is
  315. different.
  316. */
  317. mac = ks8842_read16(adapter, 2, REG_MARL);
  318. ks8842_write16(adapter, 39, mac, REG_MACAR3);
  319. mac = ks8842_read16(adapter, 2, REG_MARM);
  320. ks8842_write16(adapter, 39, mac, REG_MACAR2);
  321. mac = ks8842_read16(adapter, 2, REG_MARH);
  322. ks8842_write16(adapter, 39, mac, REG_MACAR1);
  323. } else {
  324. /* make sure the switch port uses the same MAC as the QMU */
  325. mac = ks8842_read16(adapter, 2, REG_MARL);
  326. ks8842_write16(adapter, 39, mac, REG_MACAR1);
  327. mac = ks8842_read16(adapter, 2, REG_MARM);
  328. ks8842_write16(adapter, 39, mac, REG_MACAR2);
  329. mac = ks8842_read16(adapter, 2, REG_MARH);
  330. ks8842_write16(adapter, 39, mac, REG_MACAR3);
  331. }
  332. }
  333. static void ks8842_write_mac_addr(struct ks8842_adapter *adapter, u8 *mac)
  334. {
  335. unsigned long flags;
  336. unsigned i;
  337. spin_lock_irqsave(&adapter->lock, flags);
  338. for (i = 0; i < ETH_ALEN; i++) {
  339. ks8842_write8(adapter, 2, mac[ETH_ALEN - i - 1], REG_MARL + i);
  340. if (!(adapter->conf_flags & MICREL_KS884X))
  341. ks8842_write8(adapter, 39, mac[ETH_ALEN - i - 1],
  342. REG_MACAR1 + i);
  343. }
  344. if (adapter->conf_flags & MICREL_KS884X) {
  345. /*
  346. the sequence of saving mac addr between MAC and Switch is
  347. different.
  348. */
  349. u16 mac;
  350. mac = ks8842_read16(adapter, 2, REG_MARL);
  351. ks8842_write16(adapter, 39, mac, REG_MACAR3);
  352. mac = ks8842_read16(adapter, 2, REG_MARM);
  353. ks8842_write16(adapter, 39, mac, REG_MACAR2);
  354. mac = ks8842_read16(adapter, 2, REG_MARH);
  355. ks8842_write16(adapter, 39, mac, REG_MACAR1);
  356. }
  357. spin_unlock_irqrestore(&adapter->lock, flags);
  358. }
  359. static inline u16 ks8842_tx_fifo_space(struct ks8842_adapter *adapter)
  360. {
  361. return ks8842_read16(adapter, 16, REG_TXMIR) & 0x1fff;
  362. }
  363. static int ks8842_tx_frame_dma(struct sk_buff *skb, struct net_device *netdev)
  364. {
  365. struct ks8842_adapter *adapter = netdev_priv(netdev);
  366. struct ks8842_tx_dma_ctl *ctl = &adapter->dma_tx;
  367. u8 *buf = ctl->buf;
  368. if (ctl->adesc) {
  369. netdev_dbg(netdev, "%s: TX ongoing\n", __func__);
  370. /* transfer ongoing */
  371. return NETDEV_TX_BUSY;
  372. }
  373. sg_dma_len(&ctl->sg) = skb->len + sizeof(u32);
  374. /* copy data to the TX buffer */
  375. /* the control word, enable IRQ, port 1 and the length */
  376. *buf++ = 0x00;
  377. *buf++ = 0x01; /* Port 1 */
  378. *buf++ = skb->len & 0xff;
  379. *buf++ = (skb->len >> 8) & 0xff;
  380. skb_copy_from_linear_data(skb, buf, skb->len);
  381. dma_sync_single_range_for_device(adapter->dev,
  382. sg_dma_address(&ctl->sg), 0, sg_dma_len(&ctl->sg),
  383. DMA_TO_DEVICE);
  384. /* make sure the length is a multiple of 4 */
  385. if (sg_dma_len(&ctl->sg) % 4)
  386. sg_dma_len(&ctl->sg) += 4 - sg_dma_len(&ctl->sg) % 4;
  387. ctl->adesc = dmaengine_prep_slave_sg(ctl->chan,
  388. &ctl->sg, 1, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  389. if (!ctl->adesc)
  390. return NETDEV_TX_BUSY;
  391. ctl->adesc->callback_param = netdev;
  392. ctl->adesc->callback = ks8842_dma_tx_cb;
  393. ctl->adesc->tx_submit(ctl->adesc);
  394. netdev->stats.tx_bytes += skb->len;
  395. dev_kfree_skb(skb);
  396. return NETDEV_TX_OK;
  397. }
  398. static int ks8842_tx_frame(struct sk_buff *skb, struct net_device *netdev)
  399. {
  400. struct ks8842_adapter *adapter = netdev_priv(netdev);
  401. int len = skb->len;
  402. netdev_dbg(netdev, "%s: len %u head %p data %p tail %p end %p\n",
  403. __func__, skb->len, skb->head, skb->data,
  404. skb_tail_pointer(skb), skb_end_pointer(skb));
  405. /* check FIFO buffer space, we need space for CRC and command bits */
  406. if (ks8842_tx_fifo_space(adapter) < len + 8)
  407. return NETDEV_TX_BUSY;
  408. if (adapter->conf_flags & KS884X_16BIT) {
  409. u16 *ptr16 = (u16 *)skb->data;
  410. ks8842_write16(adapter, 17, 0x8000 | 0x100, REG_QMU_DATA_LO);
  411. ks8842_write16(adapter, 17, (u16)len, REG_QMU_DATA_HI);
  412. netdev->stats.tx_bytes += len;
  413. /* copy buffer */
  414. while (len > 0) {
  415. iowrite16(*ptr16++, adapter->hw_addr + REG_QMU_DATA_LO);
  416. iowrite16(*ptr16++, adapter->hw_addr + REG_QMU_DATA_HI);
  417. len -= sizeof(u32);
  418. }
  419. } else {
  420. u32 *ptr = (u32 *)skb->data;
  421. u32 ctrl;
  422. /* the control word, enable IRQ, port 1 and the length */
  423. ctrl = 0x8000 | 0x100 | (len << 16);
  424. ks8842_write32(adapter, 17, ctrl, REG_QMU_DATA_LO);
  425. netdev->stats.tx_bytes += len;
  426. /* copy buffer */
  427. while (len > 0) {
  428. iowrite32(*ptr, adapter->hw_addr + REG_QMU_DATA_LO);
  429. len -= sizeof(u32);
  430. ptr++;
  431. }
  432. }
  433. /* enqueue packet */
  434. ks8842_write16(adapter, 17, 1, REG_TXQCR);
  435. dev_kfree_skb(skb);
  436. return NETDEV_TX_OK;
  437. }
  438. static void ks8842_update_rx_err_counters(struct net_device *netdev, u32 status)
  439. {
  440. netdev_dbg(netdev, "RX error, status: %x\n", status);
  441. netdev->stats.rx_errors++;
  442. if (status & RXSR_TOO_LONG)
  443. netdev->stats.rx_length_errors++;
  444. if (status & RXSR_CRC_ERROR)
  445. netdev->stats.rx_crc_errors++;
  446. if (status & RXSR_RUNT)
  447. netdev->stats.rx_frame_errors++;
  448. }
  449. static void ks8842_update_rx_counters(struct net_device *netdev, u32 status,
  450. int len)
  451. {
  452. netdev_dbg(netdev, "RX packet, len: %d\n", len);
  453. netdev->stats.rx_packets++;
  454. netdev->stats.rx_bytes += len;
  455. if (status & RXSR_MULTICAST)
  456. netdev->stats.multicast++;
  457. }
  458. static int __ks8842_start_new_rx_dma(struct net_device *netdev)
  459. {
  460. struct ks8842_adapter *adapter = netdev_priv(netdev);
  461. struct ks8842_rx_dma_ctl *ctl = &adapter->dma_rx;
  462. struct scatterlist *sg = &ctl->sg;
  463. int err;
  464. ctl->skb = netdev_alloc_skb(netdev, DMA_BUFFER_SIZE);
  465. if (ctl->skb) {
  466. sg_init_table(sg, 1);
  467. sg_dma_address(sg) = dma_map_single(adapter->dev,
  468. ctl->skb->data, DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  469. err = dma_mapping_error(adapter->dev, sg_dma_address(sg));
  470. if (unlikely(err)) {
  471. sg_dma_address(sg) = 0;
  472. goto out;
  473. }
  474. sg_dma_len(sg) = DMA_BUFFER_SIZE;
  475. ctl->adesc = dmaengine_prep_slave_sg(ctl->chan,
  476. sg, 1, DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  477. if (!ctl->adesc)
  478. goto out;
  479. ctl->adesc->callback_param = netdev;
  480. ctl->adesc->callback = ks8842_dma_rx_cb;
  481. ctl->adesc->tx_submit(ctl->adesc);
  482. } else {
  483. err = -ENOMEM;
  484. sg_dma_address(sg) = 0;
  485. goto out;
  486. }
  487. return err;
  488. out:
  489. if (sg_dma_address(sg))
  490. dma_unmap_single(adapter->dev, sg_dma_address(sg),
  491. DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  492. sg_dma_address(sg) = 0;
  493. if (ctl->skb)
  494. dev_kfree_skb(ctl->skb);
  495. ctl->skb = NULL;
  496. printk(KERN_ERR DRV_NAME": Failed to start RX DMA: %d\n", err);
  497. return err;
  498. }
  499. static void ks8842_rx_frame_dma_tasklet(unsigned long arg)
  500. {
  501. struct net_device *netdev = (struct net_device *)arg;
  502. struct ks8842_adapter *adapter = netdev_priv(netdev);
  503. struct ks8842_rx_dma_ctl *ctl = &adapter->dma_rx;
  504. struct sk_buff *skb = ctl->skb;
  505. dma_addr_t addr = sg_dma_address(&ctl->sg);
  506. u32 status;
  507. ctl->adesc = NULL;
  508. /* kick next transfer going */
  509. __ks8842_start_new_rx_dma(netdev);
  510. /* now handle the data we got */
  511. dma_unmap_single(adapter->dev, addr, DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  512. status = *((u32 *)skb->data);
  513. netdev_dbg(netdev, "%s - rx_data: status: %x\n",
  514. __func__, status & 0xffff);
  515. /* check the status */
  516. if ((status & RXSR_VALID) && !(status & RXSR_ERROR)) {
  517. int len = (status >> 16) & 0x7ff;
  518. ks8842_update_rx_counters(netdev, status, len);
  519. /* reserve 4 bytes which is the status word */
  520. skb_reserve(skb, 4);
  521. skb_put(skb, len);
  522. skb->protocol = eth_type_trans(skb, netdev);
  523. netif_rx(skb);
  524. } else {
  525. ks8842_update_rx_err_counters(netdev, status);
  526. dev_kfree_skb(skb);
  527. }
  528. }
  529. static void ks8842_rx_frame(struct net_device *netdev,
  530. struct ks8842_adapter *adapter)
  531. {
  532. u32 status;
  533. int len;
  534. if (adapter->conf_flags & KS884X_16BIT) {
  535. status = ks8842_read16(adapter, 17, REG_QMU_DATA_LO);
  536. len = ks8842_read16(adapter, 17, REG_QMU_DATA_HI);
  537. netdev_dbg(netdev, "%s - rx_data: status: %x\n",
  538. __func__, status);
  539. } else {
  540. status = ks8842_read32(adapter, 17, REG_QMU_DATA_LO);
  541. len = (status >> 16) & 0x7ff;
  542. status &= 0xffff;
  543. netdev_dbg(netdev, "%s - rx_data: status: %x\n",
  544. __func__, status);
  545. }
  546. /* check the status */
  547. if ((status & RXSR_VALID) && !(status & RXSR_ERROR)) {
  548. struct sk_buff *skb = netdev_alloc_skb_ip_align(netdev, len + 3);
  549. if (skb) {
  550. ks8842_update_rx_counters(netdev, status, len);
  551. if (adapter->conf_flags & KS884X_16BIT) {
  552. u16 *data16 = (u16 *)skb_put(skb, len);
  553. ks8842_select_bank(adapter, 17);
  554. while (len > 0) {
  555. *data16++ = ioread16(adapter->hw_addr +
  556. REG_QMU_DATA_LO);
  557. *data16++ = ioread16(adapter->hw_addr +
  558. REG_QMU_DATA_HI);
  559. len -= sizeof(u32);
  560. }
  561. } else {
  562. u32 *data = (u32 *)skb_put(skb, len);
  563. ks8842_select_bank(adapter, 17);
  564. while (len > 0) {
  565. *data++ = ioread32(adapter->hw_addr +
  566. REG_QMU_DATA_LO);
  567. len -= sizeof(u32);
  568. }
  569. }
  570. skb->protocol = eth_type_trans(skb, netdev);
  571. netif_rx(skb);
  572. } else
  573. netdev->stats.rx_dropped++;
  574. } else
  575. ks8842_update_rx_err_counters(netdev, status);
  576. /* set high watermark to 3K */
  577. ks8842_clear_bits(adapter, 0, 1 << 12, REG_QRFCR);
  578. /* release the frame */
  579. ks8842_write16(adapter, 17, 0x01, REG_RXQCR);
  580. /* set high watermark to 2K */
  581. ks8842_enable_bits(adapter, 0, 1 << 12, REG_QRFCR);
  582. }
  583. static void ks8842_handle_rx(struct net_device *netdev,
  584. struct ks8842_adapter *adapter)
  585. {
  586. u16 rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
  587. netdev_dbg(netdev, "%s Entry - rx_data: %d\n", __func__, rx_data);
  588. while (rx_data) {
  589. ks8842_rx_frame(netdev, adapter);
  590. rx_data = ks8842_read16(adapter, 16, REG_RXMIR) & 0x1fff;
  591. }
  592. }
  593. static void ks8842_handle_tx(struct net_device *netdev,
  594. struct ks8842_adapter *adapter)
  595. {
  596. u16 sr = ks8842_read16(adapter, 16, REG_TXSR);
  597. netdev_dbg(netdev, "%s - entry, sr: %x\n", __func__, sr);
  598. netdev->stats.tx_packets++;
  599. if (netif_queue_stopped(netdev))
  600. netif_wake_queue(netdev);
  601. }
  602. static void ks8842_handle_rx_overrun(struct net_device *netdev,
  603. struct ks8842_adapter *adapter)
  604. {
  605. netdev_dbg(netdev, "%s: entry\n", __func__);
  606. netdev->stats.rx_errors++;
  607. netdev->stats.rx_fifo_errors++;
  608. }
  609. static void ks8842_tasklet(unsigned long arg)
  610. {
  611. struct net_device *netdev = (struct net_device *)arg;
  612. struct ks8842_adapter *adapter = netdev_priv(netdev);
  613. u16 isr;
  614. unsigned long flags;
  615. u16 entry_bank;
  616. /* read current bank to be able to set it back */
  617. spin_lock_irqsave(&adapter->lock, flags);
  618. entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
  619. spin_unlock_irqrestore(&adapter->lock, flags);
  620. isr = ks8842_read16(adapter, 18, REG_ISR);
  621. netdev_dbg(netdev, "%s - ISR: 0x%x\n", __func__, isr);
  622. /* when running in DMA mode, do not ack RX interrupts, it is handled
  623. internally by timberdale, otherwise it's DMA FIFO:s would stop
  624. */
  625. if (KS8842_USE_DMA(adapter))
  626. isr &= ~IRQ_RX;
  627. /* Ack */
  628. ks8842_write16(adapter, 18, isr, REG_ISR);
  629. if (!(adapter->conf_flags & MICREL_KS884X))
  630. /* Ack in the timberdale IP as well */
  631. iowrite32(0x1, adapter->hw_addr + REG_TIMB_IAR);
  632. if (!netif_running(netdev))
  633. return;
  634. if (isr & IRQ_LINK_CHANGE)
  635. ks8842_update_link_status(netdev, adapter);
  636. /* should not get IRQ_RX when running DMA mode */
  637. if (isr & (IRQ_RX | IRQ_RX_ERROR) && !KS8842_USE_DMA(adapter))
  638. ks8842_handle_rx(netdev, adapter);
  639. /* should only happen when in PIO mode */
  640. if (isr & IRQ_TX)
  641. ks8842_handle_tx(netdev, adapter);
  642. if (isr & IRQ_RX_OVERRUN)
  643. ks8842_handle_rx_overrun(netdev, adapter);
  644. if (isr & IRQ_TX_STOPPED) {
  645. ks8842_disable_tx(adapter);
  646. ks8842_enable_tx(adapter);
  647. }
  648. if (isr & IRQ_RX_STOPPED) {
  649. ks8842_disable_rx(adapter);
  650. ks8842_enable_rx(adapter);
  651. }
  652. /* re-enable interrupts, put back the bank selection register */
  653. spin_lock_irqsave(&adapter->lock, flags);
  654. if (KS8842_USE_DMA(adapter))
  655. ks8842_write16(adapter, 18, ENABLED_IRQS_DMA, REG_IER);
  656. else
  657. ks8842_write16(adapter, 18, ENABLED_IRQS, REG_IER);
  658. iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
  659. /* Make sure timberdale continues DMA operations, they are stopped while
  660. we are handling the ks8842 because we might change bank */
  661. if (KS8842_USE_DMA(adapter))
  662. ks8842_resume_dma(adapter);
  663. spin_unlock_irqrestore(&adapter->lock, flags);
  664. }
  665. static irqreturn_t ks8842_irq(int irq, void *devid)
  666. {
  667. struct net_device *netdev = devid;
  668. struct ks8842_adapter *adapter = netdev_priv(netdev);
  669. u16 isr;
  670. u16 entry_bank = ioread16(adapter->hw_addr + REG_SELECT_BANK);
  671. irqreturn_t ret = IRQ_NONE;
  672. isr = ks8842_read16(adapter, 18, REG_ISR);
  673. netdev_dbg(netdev, "%s - ISR: 0x%x\n", __func__, isr);
  674. if (isr) {
  675. if (KS8842_USE_DMA(adapter))
  676. /* disable all but RX IRQ, since the FPGA relies on it*/
  677. ks8842_write16(adapter, 18, IRQ_RX, REG_IER);
  678. else
  679. /* disable IRQ */
  680. ks8842_write16(adapter, 18, 0x00, REG_IER);
  681. /* schedule tasklet */
  682. tasklet_schedule(&adapter->tasklet);
  683. ret = IRQ_HANDLED;
  684. }
  685. iowrite16(entry_bank, adapter->hw_addr + REG_SELECT_BANK);
  686. /* After an interrupt, tell timberdale to continue DMA operations.
  687. DMA is disabled while we are handling the ks8842 because we might
  688. change bank */
  689. ks8842_resume_dma(adapter);
  690. return ret;
  691. }
  692. static void ks8842_dma_rx_cb(void *data)
  693. {
  694. struct net_device *netdev = data;
  695. struct ks8842_adapter *adapter = netdev_priv(netdev);
  696. netdev_dbg(netdev, "RX DMA finished\n");
  697. /* schedule tasklet */
  698. if (adapter->dma_rx.adesc)
  699. tasklet_schedule(&adapter->dma_rx.tasklet);
  700. }
  701. static void ks8842_dma_tx_cb(void *data)
  702. {
  703. struct net_device *netdev = data;
  704. struct ks8842_adapter *adapter = netdev_priv(netdev);
  705. struct ks8842_tx_dma_ctl *ctl = &adapter->dma_tx;
  706. netdev_dbg(netdev, "TX DMA finished\n");
  707. if (!ctl->adesc)
  708. return;
  709. netdev->stats.tx_packets++;
  710. ctl->adesc = NULL;
  711. if (netif_queue_stopped(netdev))
  712. netif_wake_queue(netdev);
  713. }
  714. static void ks8842_stop_dma(struct ks8842_adapter *adapter)
  715. {
  716. struct ks8842_tx_dma_ctl *tx_ctl = &adapter->dma_tx;
  717. struct ks8842_rx_dma_ctl *rx_ctl = &adapter->dma_rx;
  718. tx_ctl->adesc = NULL;
  719. if (tx_ctl->chan)
  720. dmaengine_terminate_all(tx_ctl->chan);
  721. rx_ctl->adesc = NULL;
  722. if (rx_ctl->chan)
  723. dmaengine_terminate_all(rx_ctl->chan);
  724. if (sg_dma_address(&rx_ctl->sg))
  725. dma_unmap_single(adapter->dev, sg_dma_address(&rx_ctl->sg),
  726. DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
  727. sg_dma_address(&rx_ctl->sg) = 0;
  728. dev_kfree_skb(rx_ctl->skb);
  729. rx_ctl->skb = NULL;
  730. }
  731. static void ks8842_dealloc_dma_bufs(struct ks8842_adapter *adapter)
  732. {
  733. struct ks8842_tx_dma_ctl *tx_ctl = &adapter->dma_tx;
  734. struct ks8842_rx_dma_ctl *rx_ctl = &adapter->dma_rx;
  735. ks8842_stop_dma(adapter);
  736. if (tx_ctl->chan)
  737. dma_release_channel(tx_ctl->chan);
  738. tx_ctl->chan = NULL;
  739. if (rx_ctl->chan)
  740. dma_release_channel(rx_ctl->chan);
  741. rx_ctl->chan = NULL;
  742. tasklet_kill(&rx_ctl->tasklet);
  743. if (sg_dma_address(&tx_ctl->sg))
  744. dma_unmap_single(adapter->dev, sg_dma_address(&tx_ctl->sg),
  745. DMA_BUFFER_SIZE, DMA_TO_DEVICE);
  746. sg_dma_address(&tx_ctl->sg) = 0;
  747. kfree(tx_ctl->buf);
  748. tx_ctl->buf = NULL;
  749. }
  750. static bool ks8842_dma_filter_fn(struct dma_chan *chan, void *filter_param)
  751. {
  752. return chan->chan_id == (long)filter_param;
  753. }
  754. static int ks8842_alloc_dma_bufs(struct net_device *netdev)
  755. {
  756. struct ks8842_adapter *adapter = netdev_priv(netdev);
  757. struct ks8842_tx_dma_ctl *tx_ctl = &adapter->dma_tx;
  758. struct ks8842_rx_dma_ctl *rx_ctl = &adapter->dma_rx;
  759. int err;
  760. dma_cap_mask_t mask;
  761. dma_cap_zero(mask);
  762. dma_cap_set(DMA_SLAVE, mask);
  763. dma_cap_set(DMA_PRIVATE, mask);
  764. sg_init_table(&tx_ctl->sg, 1);
  765. tx_ctl->chan = dma_request_channel(mask, ks8842_dma_filter_fn,
  766. (void *)(long)tx_ctl->channel);
  767. if (!tx_ctl->chan) {
  768. err = -ENODEV;
  769. goto err;
  770. }
  771. /* allocate DMA buffer */
  772. tx_ctl->buf = kmalloc(DMA_BUFFER_SIZE, GFP_KERNEL);
  773. if (!tx_ctl->buf) {
  774. err = -ENOMEM;
  775. goto err;
  776. }
  777. sg_dma_address(&tx_ctl->sg) = dma_map_single(adapter->dev,
  778. tx_ctl->buf, DMA_BUFFER_SIZE, DMA_TO_DEVICE);
  779. if (dma_mapping_error(adapter->dev, sg_dma_address(&tx_ctl->sg))) {
  780. err = -ENOMEM;
  781. sg_dma_address(&tx_ctl->sg) = 0;
  782. goto err;
  783. }
  784. rx_ctl->chan = dma_request_channel(mask, ks8842_dma_filter_fn,
  785. (void *)(long)rx_ctl->channel);
  786. if (!rx_ctl->chan) {
  787. err = -ENODEV;
  788. goto err;
  789. }
  790. tasklet_init(&rx_ctl->tasklet, ks8842_rx_frame_dma_tasklet,
  791. (unsigned long)netdev);
  792. return 0;
  793. err:
  794. ks8842_dealloc_dma_bufs(adapter);
  795. return err;
  796. }
  797. /* Netdevice operations */
  798. static int ks8842_open(struct net_device *netdev)
  799. {
  800. struct ks8842_adapter *adapter = netdev_priv(netdev);
  801. int err;
  802. netdev_dbg(netdev, "%s - entry\n", __func__);
  803. if (KS8842_USE_DMA(adapter)) {
  804. err = ks8842_alloc_dma_bufs(netdev);
  805. if (!err) {
  806. /* start RX dma */
  807. err = __ks8842_start_new_rx_dma(netdev);
  808. if (err)
  809. ks8842_dealloc_dma_bufs(adapter);
  810. }
  811. if (err) {
  812. printk(KERN_WARNING DRV_NAME
  813. ": Failed to initiate DMA, running PIO\n");
  814. ks8842_dealloc_dma_bufs(adapter);
  815. adapter->dma_rx.channel = -1;
  816. adapter->dma_tx.channel = -1;
  817. }
  818. }
  819. /* reset the HW */
  820. ks8842_reset_hw(adapter);
  821. ks8842_write_mac_addr(adapter, netdev->dev_addr);
  822. ks8842_update_link_status(netdev, adapter);
  823. err = request_irq(adapter->irq, ks8842_irq, IRQF_SHARED, DRV_NAME,
  824. netdev);
  825. if (err) {
  826. pr_err("Failed to request IRQ: %d: %d\n", adapter->irq, err);
  827. return err;
  828. }
  829. return 0;
  830. }
  831. static int ks8842_close(struct net_device *netdev)
  832. {
  833. struct ks8842_adapter *adapter = netdev_priv(netdev);
  834. netdev_dbg(netdev, "%s - entry\n", __func__);
  835. cancel_work_sync(&adapter->timeout_work);
  836. if (KS8842_USE_DMA(adapter))
  837. ks8842_dealloc_dma_bufs(adapter);
  838. /* free the irq */
  839. free_irq(adapter->irq, netdev);
  840. /* disable the switch */
  841. ks8842_write16(adapter, 32, 0x0, REG_SW_ID_AND_ENABLE);
  842. return 0;
  843. }
  844. static netdev_tx_t ks8842_xmit_frame(struct sk_buff *skb,
  845. struct net_device *netdev)
  846. {
  847. int ret;
  848. struct ks8842_adapter *adapter = netdev_priv(netdev);
  849. netdev_dbg(netdev, "%s: entry\n", __func__);
  850. if (KS8842_USE_DMA(adapter)) {
  851. unsigned long flags;
  852. ret = ks8842_tx_frame_dma(skb, netdev);
  853. /* for now only allow one transfer at the time */
  854. spin_lock_irqsave(&adapter->lock, flags);
  855. if (adapter->dma_tx.adesc)
  856. netif_stop_queue(netdev);
  857. spin_unlock_irqrestore(&adapter->lock, flags);
  858. return ret;
  859. }
  860. ret = ks8842_tx_frame(skb, netdev);
  861. if (ks8842_tx_fifo_space(adapter) < netdev->mtu + 8)
  862. netif_stop_queue(netdev);
  863. return ret;
  864. }
  865. static int ks8842_set_mac(struct net_device *netdev, void *p)
  866. {
  867. struct ks8842_adapter *adapter = netdev_priv(netdev);
  868. struct sockaddr *addr = p;
  869. char *mac = (u8 *)addr->sa_data;
  870. netdev_dbg(netdev, "%s: entry\n", __func__);
  871. if (!is_valid_ether_addr(addr->sa_data))
  872. return -EADDRNOTAVAIL;
  873. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  874. ks8842_write_mac_addr(adapter, mac);
  875. return 0;
  876. }
  877. static void ks8842_tx_timeout_work(struct work_struct *work)
  878. {
  879. struct ks8842_adapter *adapter =
  880. container_of(work, struct ks8842_adapter, timeout_work);
  881. struct net_device *netdev = adapter->netdev;
  882. unsigned long flags;
  883. netdev_dbg(netdev, "%s: entry\n", __func__);
  884. spin_lock_irqsave(&adapter->lock, flags);
  885. if (KS8842_USE_DMA(adapter))
  886. ks8842_stop_dma(adapter);
  887. /* disable interrupts */
  888. ks8842_write16(adapter, 18, 0, REG_IER);
  889. ks8842_write16(adapter, 18, 0xFFFF, REG_ISR);
  890. netif_stop_queue(netdev);
  891. spin_unlock_irqrestore(&adapter->lock, flags);
  892. ks8842_reset_hw(adapter);
  893. ks8842_write_mac_addr(adapter, netdev->dev_addr);
  894. ks8842_update_link_status(netdev, adapter);
  895. if (KS8842_USE_DMA(adapter))
  896. __ks8842_start_new_rx_dma(netdev);
  897. }
  898. static void ks8842_tx_timeout(struct net_device *netdev)
  899. {
  900. struct ks8842_adapter *adapter = netdev_priv(netdev);
  901. netdev_dbg(netdev, "%s: entry\n", __func__);
  902. schedule_work(&adapter->timeout_work);
  903. }
  904. static const struct net_device_ops ks8842_netdev_ops = {
  905. .ndo_open = ks8842_open,
  906. .ndo_stop = ks8842_close,
  907. .ndo_start_xmit = ks8842_xmit_frame,
  908. .ndo_set_mac_address = ks8842_set_mac,
  909. .ndo_tx_timeout = ks8842_tx_timeout,
  910. .ndo_validate_addr = eth_validate_addr
  911. };
  912. static const struct ethtool_ops ks8842_ethtool_ops = {
  913. .get_link = ethtool_op_get_link,
  914. };
  915. static int ks8842_probe(struct platform_device *pdev)
  916. {
  917. int err = -ENOMEM;
  918. struct resource *iomem;
  919. struct net_device *netdev;
  920. struct ks8842_adapter *adapter;
  921. struct ks8842_platform_data *pdata = dev_get_platdata(&pdev->dev);
  922. u16 id;
  923. unsigned i;
  924. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  925. if (!request_mem_region(iomem->start, resource_size(iomem), DRV_NAME))
  926. goto err_mem_region;
  927. netdev = alloc_etherdev(sizeof(struct ks8842_adapter));
  928. if (!netdev)
  929. goto err_alloc_etherdev;
  930. SET_NETDEV_DEV(netdev, &pdev->dev);
  931. adapter = netdev_priv(netdev);
  932. adapter->netdev = netdev;
  933. INIT_WORK(&adapter->timeout_work, ks8842_tx_timeout_work);
  934. adapter->hw_addr = ioremap(iomem->start, resource_size(iomem));
  935. adapter->conf_flags = iomem->flags;
  936. if (!adapter->hw_addr)
  937. goto err_ioremap;
  938. adapter->irq = platform_get_irq(pdev, 0);
  939. if (adapter->irq < 0) {
  940. err = adapter->irq;
  941. goto err_get_irq;
  942. }
  943. adapter->dev = (pdev->dev.parent) ? pdev->dev.parent : &pdev->dev;
  944. /* DMA is only supported when accessed via timberdale */
  945. if (!(adapter->conf_flags & MICREL_KS884X) && pdata &&
  946. (pdata->tx_dma_channel != -1) &&
  947. (pdata->rx_dma_channel != -1)) {
  948. adapter->dma_rx.channel = pdata->rx_dma_channel;
  949. adapter->dma_tx.channel = pdata->tx_dma_channel;
  950. } else {
  951. adapter->dma_rx.channel = -1;
  952. adapter->dma_tx.channel = -1;
  953. }
  954. tasklet_init(&adapter->tasklet, ks8842_tasklet, (unsigned long)netdev);
  955. spin_lock_init(&adapter->lock);
  956. netdev->netdev_ops = &ks8842_netdev_ops;
  957. netdev->ethtool_ops = &ks8842_ethtool_ops;
  958. /* Check if a mac address was given */
  959. i = netdev->addr_len;
  960. if (pdata) {
  961. for (i = 0; i < netdev->addr_len; i++)
  962. if (pdata->macaddr[i] != 0)
  963. break;
  964. if (i < netdev->addr_len)
  965. /* an address was passed, use it */
  966. memcpy(netdev->dev_addr, pdata->macaddr,
  967. netdev->addr_len);
  968. }
  969. if (i == netdev->addr_len) {
  970. ks8842_read_mac_addr(adapter, netdev->dev_addr);
  971. if (!is_valid_ether_addr(netdev->dev_addr))
  972. eth_hw_addr_random(netdev);
  973. }
  974. id = ks8842_read16(adapter, 32, REG_SW_ID_AND_ENABLE);
  975. strcpy(netdev->name, "eth%d");
  976. err = register_netdev(netdev);
  977. if (err)
  978. goto err_register;
  979. platform_set_drvdata(pdev, netdev);
  980. pr_info("Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
  981. (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
  982. return 0;
  983. err_register:
  984. err_get_irq:
  985. iounmap(adapter->hw_addr);
  986. err_ioremap:
  987. free_netdev(netdev);
  988. err_alloc_etherdev:
  989. release_mem_region(iomem->start, resource_size(iomem));
  990. err_mem_region:
  991. return err;
  992. }
  993. static int ks8842_remove(struct platform_device *pdev)
  994. {
  995. struct net_device *netdev = platform_get_drvdata(pdev);
  996. struct ks8842_adapter *adapter = netdev_priv(netdev);
  997. struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  998. unregister_netdev(netdev);
  999. tasklet_kill(&adapter->tasklet);
  1000. iounmap(adapter->hw_addr);
  1001. free_netdev(netdev);
  1002. release_mem_region(iomem->start, resource_size(iomem));
  1003. return 0;
  1004. }
  1005. static struct platform_driver ks8842_platform_driver = {
  1006. .driver = {
  1007. .name = DRV_NAME,
  1008. },
  1009. .probe = ks8842_probe,
  1010. .remove = ks8842_remove,
  1011. };
  1012. module_platform_driver(ks8842_platform_driver);
  1013. MODULE_DESCRIPTION("Timberdale KS8842 ethernet driver");
  1014. MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
  1015. MODULE_LICENSE("GPL v2");
  1016. MODULE_ALIAS("platform:ks8842");