ks8695net.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. /*
  2. * Micrel KS8695 (Centaur) Ethernet.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * Copyright 2008 Simtec Electronics
  15. * Daniel Silverstone <dsilvers@simtec.co.uk>
  16. * Vincent Sanders <vince@simtec.co.uk>
  17. */
  18. #include <linux/dma-mapping.h>
  19. #include <linux/module.h>
  20. #include <linux/ioport.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/crc32.h>
  27. #include <linux/mii.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/delay.h>
  30. #include <linux/platform_device.h>
  31. #include <linux/irq.h>
  32. #include <linux/io.h>
  33. #include <linux/slab.h>
  34. #include <asm/irq.h>
  35. #include <mach/regs-switch.h>
  36. #include <mach/regs-misc.h>
  37. #include <asm/mach/irq.h>
  38. #include <mach/regs-irq.h>
  39. #include "ks8695net.h"
  40. #define MODULENAME "ks8695_ether"
  41. #define MODULEVERSION "1.02"
  42. /*
  43. * Transmit and device reset timeout, default 5 seconds.
  44. */
  45. static int watchdog = 5000;
  46. /* Hardware structures */
  47. /**
  48. * struct rx_ring_desc - Receive descriptor ring element
  49. * @status: The status of the descriptor element (E.g. who owns it)
  50. * @length: The number of bytes in the block pointed to by data_ptr
  51. * @data_ptr: The physical address of the data block to receive into
  52. * @next_desc: The physical address of the next descriptor element.
  53. */
  54. struct rx_ring_desc {
  55. __le32 status;
  56. __le32 length;
  57. __le32 data_ptr;
  58. __le32 next_desc;
  59. };
  60. /**
  61. * struct tx_ring_desc - Transmit descriptor ring element
  62. * @owner: Who owns the descriptor
  63. * @status: The number of bytes in the block pointed to by data_ptr
  64. * @data_ptr: The physical address of the data block to receive into
  65. * @next_desc: The physical address of the next descriptor element.
  66. */
  67. struct tx_ring_desc {
  68. __le32 owner;
  69. __le32 status;
  70. __le32 data_ptr;
  71. __le32 next_desc;
  72. };
  73. /**
  74. * struct ks8695_skbuff - sk_buff wrapper for rx/tx rings.
  75. * @skb: The buffer in the ring
  76. * @dma_ptr: The mapped DMA pointer of the buffer
  77. * @length: The number of bytes mapped to dma_ptr
  78. */
  79. struct ks8695_skbuff {
  80. struct sk_buff *skb;
  81. dma_addr_t dma_ptr;
  82. u32 length;
  83. };
  84. /* Private device structure */
  85. #define MAX_TX_DESC 8
  86. #define MAX_TX_DESC_MASK 0x7
  87. #define MAX_RX_DESC 16
  88. #define MAX_RX_DESC_MASK 0xf
  89. /*napi_weight have better more than rx DMA buffers*/
  90. #define NAPI_WEIGHT 64
  91. #define MAX_RXBUF_SIZE 0x700
  92. #define TX_RING_DMA_SIZE (sizeof(struct tx_ring_desc) * MAX_TX_DESC)
  93. #define RX_RING_DMA_SIZE (sizeof(struct rx_ring_desc) * MAX_RX_DESC)
  94. #define RING_DMA_SIZE (TX_RING_DMA_SIZE + RX_RING_DMA_SIZE)
  95. /**
  96. * enum ks8695_dtype - Device type
  97. * @KS8695_DTYPE_WAN: This device is a WAN interface
  98. * @KS8695_DTYPE_LAN: This device is a LAN interface
  99. * @KS8695_DTYPE_HPNA: This device is an HPNA interface
  100. */
  101. enum ks8695_dtype {
  102. KS8695_DTYPE_WAN,
  103. KS8695_DTYPE_LAN,
  104. KS8695_DTYPE_HPNA,
  105. };
  106. /**
  107. * struct ks8695_priv - Private data for the KS8695 Ethernet
  108. * @in_suspend: Flag to indicate if we're suspending/resuming
  109. * @ndev: The net_device for this interface
  110. * @dev: The platform device object for this interface
  111. * @dtype: The type of this device
  112. * @io_regs: The ioremapped registers for this interface
  113. * @napi : Add support NAPI for Rx
  114. * @rx_irq_name: The textual name of the RX IRQ from the platform data
  115. * @tx_irq_name: The textual name of the TX IRQ from the platform data
  116. * @link_irq_name: The textual name of the link IRQ from the
  117. * platform data if available
  118. * @rx_irq: The IRQ number for the RX IRQ
  119. * @tx_irq: The IRQ number for the TX IRQ
  120. * @link_irq: The IRQ number for the link IRQ if available
  121. * @regs_req: The resource request for the registers region
  122. * @phyiface_req: The resource request for the phy/switch region
  123. * if available
  124. * @phyiface_regs: The ioremapped registers for the phy/switch if available
  125. * @ring_base: The base pointer of the dma coherent memory for the rings
  126. * @ring_base_dma: The DMA mapped equivalent of ring_base
  127. * @tx_ring: The pointer in ring_base of the TX ring
  128. * @tx_ring_used: The number of slots in the TX ring which are occupied
  129. * @tx_ring_next_slot: The next slot to fill in the TX ring
  130. * @tx_ring_dma: The DMA mapped equivalent of tx_ring
  131. * @tx_buffers: The sk_buff mappings for the TX ring
  132. * @txq_lock: A lock to protect the tx_buffers tx_ring_used etc variables
  133. * @rx_ring: The pointer in ring_base of the RX ring
  134. * @rx_ring_dma: The DMA mapped equivalent of rx_ring
  135. * @rx_buffers: The sk_buff mappings for the RX ring
  136. * @next_rx_desc_read: The next RX descriptor to read from on IRQ
  137. * @rx_lock: A lock to protect Rx irq function
  138. * @msg_enable: The flags for which messages to emit
  139. */
  140. struct ks8695_priv {
  141. int in_suspend;
  142. struct net_device *ndev;
  143. struct device *dev;
  144. enum ks8695_dtype dtype;
  145. void __iomem *io_regs;
  146. struct napi_struct napi;
  147. const char *rx_irq_name, *tx_irq_name, *link_irq_name;
  148. int rx_irq, tx_irq, link_irq;
  149. struct resource *regs_req, *phyiface_req;
  150. void __iomem *phyiface_regs;
  151. void *ring_base;
  152. dma_addr_t ring_base_dma;
  153. struct tx_ring_desc *tx_ring;
  154. int tx_ring_used;
  155. int tx_ring_next_slot;
  156. dma_addr_t tx_ring_dma;
  157. struct ks8695_skbuff tx_buffers[MAX_TX_DESC];
  158. spinlock_t txq_lock;
  159. struct rx_ring_desc *rx_ring;
  160. dma_addr_t rx_ring_dma;
  161. struct ks8695_skbuff rx_buffers[MAX_RX_DESC];
  162. int next_rx_desc_read;
  163. spinlock_t rx_lock;
  164. int msg_enable;
  165. };
  166. /* Register access */
  167. /**
  168. * ks8695_readreg - Read from a KS8695 ethernet register
  169. * @ksp: The device to read from
  170. * @reg: The register to read
  171. */
  172. static inline u32
  173. ks8695_readreg(struct ks8695_priv *ksp, int reg)
  174. {
  175. return readl(ksp->io_regs + reg);
  176. }
  177. /**
  178. * ks8695_writereg - Write to a KS8695 ethernet register
  179. * @ksp: The device to write to
  180. * @reg: The register to write
  181. * @value: The value to write to the register
  182. */
  183. static inline void
  184. ks8695_writereg(struct ks8695_priv *ksp, int reg, u32 value)
  185. {
  186. writel(value, ksp->io_regs + reg);
  187. }
  188. /* Utility functions */
  189. /**
  190. * ks8695_port_type - Retrieve port-type as user-friendly string
  191. * @ksp: The device to return the type for
  192. *
  193. * Returns a string indicating which of the WAN, LAN or HPNA
  194. * ports this device is likely to represent.
  195. */
  196. static const char *
  197. ks8695_port_type(struct ks8695_priv *ksp)
  198. {
  199. switch (ksp->dtype) {
  200. case KS8695_DTYPE_LAN:
  201. return "LAN";
  202. case KS8695_DTYPE_WAN:
  203. return "WAN";
  204. case KS8695_DTYPE_HPNA:
  205. return "HPNA";
  206. }
  207. return "UNKNOWN";
  208. }
  209. /**
  210. * ks8695_update_mac - Update the MAC registers in the device
  211. * @ksp: The device to update
  212. *
  213. * Updates the MAC registers in the KS8695 device from the address in the
  214. * net_device structure associated with this interface.
  215. */
  216. static void
  217. ks8695_update_mac(struct ks8695_priv *ksp)
  218. {
  219. /* Update the HW with the MAC from the net_device */
  220. struct net_device *ndev = ksp->ndev;
  221. u32 machigh, maclow;
  222. maclow = ((ndev->dev_addr[2] << 24) | (ndev->dev_addr[3] << 16) |
  223. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5] << 0));
  224. machigh = ((ndev->dev_addr[0] << 8) | (ndev->dev_addr[1] << 0));
  225. ks8695_writereg(ksp, KS8695_MAL, maclow);
  226. ks8695_writereg(ksp, KS8695_MAH, machigh);
  227. }
  228. /**
  229. * ks8695_refill_rxbuffers - Re-fill the RX buffer ring
  230. * @ksp: The device to refill
  231. *
  232. * Iterates the RX ring of the device looking for empty slots.
  233. * For each empty slot, we allocate and map a new SKB and give it
  234. * to the hardware.
  235. * This can be called from interrupt context safely.
  236. */
  237. static void
  238. ks8695_refill_rxbuffers(struct ks8695_priv *ksp)
  239. {
  240. /* Run around the RX ring, filling in any missing sk_buff's */
  241. int buff_n;
  242. for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
  243. if (!ksp->rx_buffers[buff_n].skb) {
  244. struct sk_buff *skb =
  245. netdev_alloc_skb(ksp->ndev, MAX_RXBUF_SIZE);
  246. dma_addr_t mapping;
  247. ksp->rx_buffers[buff_n].skb = skb;
  248. if (skb == NULL) {
  249. /* Failed to allocate one, perhaps
  250. * we'll try again later.
  251. */
  252. break;
  253. }
  254. mapping = dma_map_single(ksp->dev, skb->data,
  255. MAX_RXBUF_SIZE,
  256. DMA_FROM_DEVICE);
  257. if (unlikely(dma_mapping_error(ksp->dev, mapping))) {
  258. /* Failed to DMA map this SKB, try later */
  259. dev_kfree_skb_irq(skb);
  260. ksp->rx_buffers[buff_n].skb = NULL;
  261. break;
  262. }
  263. ksp->rx_buffers[buff_n].dma_ptr = mapping;
  264. ksp->rx_buffers[buff_n].length = MAX_RXBUF_SIZE;
  265. /* Record this into the DMA ring */
  266. ksp->rx_ring[buff_n].data_ptr = cpu_to_le32(mapping);
  267. ksp->rx_ring[buff_n].length =
  268. cpu_to_le32(MAX_RXBUF_SIZE);
  269. wmb();
  270. /* And give ownership over to the hardware */
  271. ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
  272. }
  273. }
  274. }
  275. /* Maximum number of multicast addresses which the KS8695 HW supports */
  276. #define KS8695_NR_ADDRESSES 16
  277. /**
  278. * ks8695_init_partial_multicast - Init the mcast addr registers
  279. * @ksp: The device to initialise
  280. * @addr: The multicast address list to use
  281. * @nr_addr: The number of addresses in the list
  282. *
  283. * This routine is a helper for ks8695_set_multicast - it writes
  284. * the additional-address registers in the KS8695 ethernet device
  285. * and cleans up any others left behind.
  286. */
  287. static void
  288. ks8695_init_partial_multicast(struct ks8695_priv *ksp,
  289. struct net_device *ndev)
  290. {
  291. u32 low, high;
  292. int i;
  293. struct netdev_hw_addr *ha;
  294. i = 0;
  295. netdev_for_each_mc_addr(ha, ndev) {
  296. /* Ran out of space in chip? */
  297. BUG_ON(i == KS8695_NR_ADDRESSES);
  298. low = (ha->addr[2] << 24) | (ha->addr[3] << 16) |
  299. (ha->addr[4] << 8) | (ha->addr[5]);
  300. high = (ha->addr[0] << 8) | (ha->addr[1]);
  301. ks8695_writereg(ksp, KS8695_AAL_(i), low);
  302. ks8695_writereg(ksp, KS8695_AAH_(i), AAH_E | high);
  303. i++;
  304. }
  305. /* Clear the remaining Additional Station Addresses */
  306. for (; i < KS8695_NR_ADDRESSES; i++) {
  307. ks8695_writereg(ksp, KS8695_AAL_(i), 0);
  308. ks8695_writereg(ksp, KS8695_AAH_(i), 0);
  309. }
  310. }
  311. /* Interrupt handling */
  312. /**
  313. * ks8695_tx_irq - Transmit IRQ handler
  314. * @irq: The IRQ which went off (ignored)
  315. * @dev_id: The net_device for the interrupt
  316. *
  317. * Process the TX ring, clearing out any transmitted slots.
  318. * Allows the net_device to pass us new packets once slots are
  319. * freed.
  320. */
  321. static irqreturn_t
  322. ks8695_tx_irq(int irq, void *dev_id)
  323. {
  324. struct net_device *ndev = (struct net_device *)dev_id;
  325. struct ks8695_priv *ksp = netdev_priv(ndev);
  326. int buff_n;
  327. for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
  328. if (ksp->tx_buffers[buff_n].skb &&
  329. !(ksp->tx_ring[buff_n].owner & cpu_to_le32(TDES_OWN))) {
  330. rmb();
  331. /* An SKB which is not owned by HW is present */
  332. /* Update the stats for the net_device */
  333. ndev->stats.tx_packets++;
  334. ndev->stats.tx_bytes += ksp->tx_buffers[buff_n].length;
  335. /* Free the packet from the ring */
  336. ksp->tx_ring[buff_n].data_ptr = 0;
  337. /* Free the sk_buff */
  338. dma_unmap_single(ksp->dev,
  339. ksp->tx_buffers[buff_n].dma_ptr,
  340. ksp->tx_buffers[buff_n].length,
  341. DMA_TO_DEVICE);
  342. dev_kfree_skb_irq(ksp->tx_buffers[buff_n].skb);
  343. ksp->tx_buffers[buff_n].skb = NULL;
  344. ksp->tx_ring_used--;
  345. }
  346. }
  347. netif_wake_queue(ndev);
  348. return IRQ_HANDLED;
  349. }
  350. /**
  351. * ks8695_get_rx_enable_bit - Get rx interrupt enable/status bit
  352. * @ksp: Private data for the KS8695 Ethernet
  353. *
  354. * For KS8695 document:
  355. * Interrupt Enable Register (offset 0xE204)
  356. * Bit29 : WAN MAC Receive Interrupt Enable
  357. * Bit16 : LAN MAC Receive Interrupt Enable
  358. * Interrupt Status Register (Offset 0xF208)
  359. * Bit29: WAN MAC Receive Status
  360. * Bit16: LAN MAC Receive Status
  361. * So, this Rx interrupt enable/status bit number is equal
  362. * as Rx IRQ number.
  363. */
  364. static inline u32 ks8695_get_rx_enable_bit(struct ks8695_priv *ksp)
  365. {
  366. return ksp->rx_irq;
  367. }
  368. /**
  369. * ks8695_rx_irq - Receive IRQ handler
  370. * @irq: The IRQ which went off (ignored)
  371. * @dev_id: The net_device for the interrupt
  372. *
  373. * Inform NAPI that packet reception needs to be scheduled
  374. */
  375. static irqreturn_t
  376. ks8695_rx_irq(int irq, void *dev_id)
  377. {
  378. struct net_device *ndev = (struct net_device *)dev_id;
  379. struct ks8695_priv *ksp = netdev_priv(ndev);
  380. spin_lock(&ksp->rx_lock);
  381. if (napi_schedule_prep(&ksp->napi)) {
  382. unsigned long status = readl(KS8695_IRQ_VA + KS8695_INTEN);
  383. unsigned long mask_bit = 1 << ks8695_get_rx_enable_bit(ksp);
  384. /*disable rx interrupt*/
  385. status &= ~mask_bit;
  386. writel(status , KS8695_IRQ_VA + KS8695_INTEN);
  387. __napi_schedule(&ksp->napi);
  388. }
  389. spin_unlock(&ksp->rx_lock);
  390. return IRQ_HANDLED;
  391. }
  392. /**
  393. * ks8695_rx - Receive packets called by NAPI poll method
  394. * @ksp: Private data for the KS8695 Ethernet
  395. * @budget: Number of packets allowed to process
  396. */
  397. static int ks8695_rx(struct ks8695_priv *ksp, int budget)
  398. {
  399. struct net_device *ndev = ksp->ndev;
  400. struct sk_buff *skb;
  401. int buff_n;
  402. u32 flags;
  403. int pktlen;
  404. int received = 0;
  405. buff_n = ksp->next_rx_desc_read;
  406. while (received < budget
  407. && ksp->rx_buffers[buff_n].skb
  408. && (!(ksp->rx_ring[buff_n].status &
  409. cpu_to_le32(RDES_OWN)))) {
  410. rmb();
  411. flags = le32_to_cpu(ksp->rx_ring[buff_n].status);
  412. /* Found an SKB which we own, this means we
  413. * received a packet
  414. */
  415. if ((flags & (RDES_FS | RDES_LS)) !=
  416. (RDES_FS | RDES_LS)) {
  417. /* This packet is not the first and
  418. * the last segment. Therefore it is
  419. * a "spanning" packet and we can't
  420. * handle it
  421. */
  422. goto rx_failure;
  423. }
  424. if (flags & (RDES_ES | RDES_RE)) {
  425. /* It's an error packet */
  426. ndev->stats.rx_errors++;
  427. if (flags & RDES_TL)
  428. ndev->stats.rx_length_errors++;
  429. if (flags & RDES_RF)
  430. ndev->stats.rx_length_errors++;
  431. if (flags & RDES_CE)
  432. ndev->stats.rx_crc_errors++;
  433. if (flags & RDES_RE)
  434. ndev->stats.rx_missed_errors++;
  435. goto rx_failure;
  436. }
  437. pktlen = flags & RDES_FLEN;
  438. pktlen -= 4; /* Drop the CRC */
  439. /* Retrieve the sk_buff */
  440. skb = ksp->rx_buffers[buff_n].skb;
  441. /* Clear it from the ring */
  442. ksp->rx_buffers[buff_n].skb = NULL;
  443. ksp->rx_ring[buff_n].data_ptr = 0;
  444. /* Unmap the SKB */
  445. dma_unmap_single(ksp->dev,
  446. ksp->rx_buffers[buff_n].dma_ptr,
  447. ksp->rx_buffers[buff_n].length,
  448. DMA_FROM_DEVICE);
  449. /* Relinquish the SKB to the network layer */
  450. skb_put(skb, pktlen);
  451. skb->protocol = eth_type_trans(skb, ndev);
  452. netif_receive_skb(skb);
  453. /* Record stats */
  454. ndev->stats.rx_packets++;
  455. ndev->stats.rx_bytes += pktlen;
  456. goto rx_finished;
  457. rx_failure:
  458. /* This ring entry is an error, but we can
  459. * re-use the skb
  460. */
  461. /* Give the ring entry back to the hardware */
  462. ksp->rx_ring[buff_n].status = cpu_to_le32(RDES_OWN);
  463. rx_finished:
  464. received++;
  465. buff_n = (buff_n + 1) & MAX_RX_DESC_MASK;
  466. }
  467. /* And note which RX descriptor we last did */
  468. ksp->next_rx_desc_read = buff_n;
  469. /* And refill the buffers */
  470. ks8695_refill_rxbuffers(ksp);
  471. /* Kick the RX DMA engine, in case it became suspended */
  472. ks8695_writereg(ksp, KS8695_DRSC, 0);
  473. return received;
  474. }
  475. /**
  476. * ks8695_poll - Receive packet by NAPI poll method
  477. * @ksp: Private data for the KS8695 Ethernet
  478. * @budget: The remaining number packets for network subsystem
  479. *
  480. * Invoked by the network core when it requests for new
  481. * packets from the driver
  482. */
  483. static int ks8695_poll(struct napi_struct *napi, int budget)
  484. {
  485. struct ks8695_priv *ksp = container_of(napi, struct ks8695_priv, napi);
  486. unsigned long work_done;
  487. unsigned long isr = readl(KS8695_IRQ_VA + KS8695_INTEN);
  488. unsigned long mask_bit = 1 << ks8695_get_rx_enable_bit(ksp);
  489. work_done = ks8695_rx(ksp, budget);
  490. if (work_done < budget) {
  491. unsigned long flags;
  492. spin_lock_irqsave(&ksp->rx_lock, flags);
  493. __napi_complete(napi);
  494. /*enable rx interrupt*/
  495. writel(isr | mask_bit, KS8695_IRQ_VA + KS8695_INTEN);
  496. spin_unlock_irqrestore(&ksp->rx_lock, flags);
  497. }
  498. return work_done;
  499. }
  500. /**
  501. * ks8695_link_irq - Link change IRQ handler
  502. * @irq: The IRQ which went off (ignored)
  503. * @dev_id: The net_device for the interrupt
  504. *
  505. * The WAN interface can generate an IRQ when the link changes,
  506. * report this to the net layer and the user.
  507. */
  508. static irqreturn_t
  509. ks8695_link_irq(int irq, void *dev_id)
  510. {
  511. struct net_device *ndev = (struct net_device *)dev_id;
  512. struct ks8695_priv *ksp = netdev_priv(ndev);
  513. u32 ctrl;
  514. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  515. if (ctrl & WMC_WLS) {
  516. netif_carrier_on(ndev);
  517. if (netif_msg_link(ksp))
  518. dev_info(ksp->dev,
  519. "%s: Link is now up (10%sMbps/%s-duplex)\n",
  520. ndev->name,
  521. (ctrl & WMC_WSS) ? "0" : "",
  522. (ctrl & WMC_WDS) ? "Full" : "Half");
  523. } else {
  524. netif_carrier_off(ndev);
  525. if (netif_msg_link(ksp))
  526. dev_info(ksp->dev, "%s: Link is now down.\n",
  527. ndev->name);
  528. }
  529. return IRQ_HANDLED;
  530. }
  531. /* KS8695 Device functions */
  532. /**
  533. * ks8695_reset - Reset a KS8695 ethernet interface
  534. * @ksp: The interface to reset
  535. *
  536. * Perform an engine reset of the interface and re-program it
  537. * with sensible defaults.
  538. */
  539. static void
  540. ks8695_reset(struct ks8695_priv *ksp)
  541. {
  542. int reset_timeout = watchdog;
  543. /* Issue the reset via the TX DMA control register */
  544. ks8695_writereg(ksp, KS8695_DTXC, DTXC_TRST);
  545. while (reset_timeout--) {
  546. if (!(ks8695_readreg(ksp, KS8695_DTXC) & DTXC_TRST))
  547. break;
  548. msleep(1);
  549. }
  550. if (reset_timeout < 0) {
  551. dev_crit(ksp->dev,
  552. "Timeout waiting for DMA engines to reset\n");
  553. /* And blithely carry on */
  554. }
  555. /* Definitely wait long enough before attempting to program
  556. * the engines
  557. */
  558. msleep(10);
  559. /* RX: unicast and broadcast */
  560. ks8695_writereg(ksp, KS8695_DRXC, DRXC_RU | DRXC_RB);
  561. /* TX: pad and add CRC */
  562. ks8695_writereg(ksp, KS8695_DTXC, DTXC_TEP | DTXC_TAC);
  563. }
  564. /**
  565. * ks8695_shutdown - Shut down a KS8695 ethernet interface
  566. * @ksp: The interface to shut down
  567. *
  568. * This disables packet RX/TX, cleans up IRQs, drains the rings,
  569. * and basically places the interface into a clean shutdown
  570. * state.
  571. */
  572. static void
  573. ks8695_shutdown(struct ks8695_priv *ksp)
  574. {
  575. u32 ctrl;
  576. int buff_n;
  577. /* Disable packet transmission */
  578. ctrl = ks8695_readreg(ksp, KS8695_DTXC);
  579. ks8695_writereg(ksp, KS8695_DTXC, ctrl & ~DTXC_TE);
  580. /* Disable packet reception */
  581. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  582. ks8695_writereg(ksp, KS8695_DRXC, ctrl & ~DRXC_RE);
  583. /* Release the IRQs */
  584. free_irq(ksp->rx_irq, ksp->ndev);
  585. free_irq(ksp->tx_irq, ksp->ndev);
  586. if (ksp->link_irq != -1)
  587. free_irq(ksp->link_irq, ksp->ndev);
  588. /* Throw away any pending TX packets */
  589. for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
  590. if (ksp->tx_buffers[buff_n].skb) {
  591. /* Remove this SKB from the TX ring */
  592. ksp->tx_ring[buff_n].owner = 0;
  593. ksp->tx_ring[buff_n].status = 0;
  594. ksp->tx_ring[buff_n].data_ptr = 0;
  595. /* Unmap and bin this SKB */
  596. dma_unmap_single(ksp->dev,
  597. ksp->tx_buffers[buff_n].dma_ptr,
  598. ksp->tx_buffers[buff_n].length,
  599. DMA_TO_DEVICE);
  600. dev_kfree_skb_irq(ksp->tx_buffers[buff_n].skb);
  601. ksp->tx_buffers[buff_n].skb = NULL;
  602. }
  603. }
  604. /* Purge the RX buffers */
  605. for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
  606. if (ksp->rx_buffers[buff_n].skb) {
  607. /* Remove the SKB from the RX ring */
  608. ksp->rx_ring[buff_n].status = 0;
  609. ksp->rx_ring[buff_n].data_ptr = 0;
  610. /* Unmap and bin the SKB */
  611. dma_unmap_single(ksp->dev,
  612. ksp->rx_buffers[buff_n].dma_ptr,
  613. ksp->rx_buffers[buff_n].length,
  614. DMA_FROM_DEVICE);
  615. dev_kfree_skb_irq(ksp->rx_buffers[buff_n].skb);
  616. ksp->rx_buffers[buff_n].skb = NULL;
  617. }
  618. }
  619. }
  620. /**
  621. * ks8695_setup_irq - IRQ setup helper function
  622. * @irq: The IRQ number to claim
  623. * @irq_name: The name to give the IRQ claimant
  624. * @handler: The function to call to handle the IRQ
  625. * @ndev: The net_device to pass in as the dev_id argument to the handler
  626. *
  627. * Return 0 on success.
  628. */
  629. static int
  630. ks8695_setup_irq(int irq, const char *irq_name,
  631. irq_handler_t handler, struct net_device *ndev)
  632. {
  633. int ret;
  634. ret = request_irq(irq, handler, IRQF_SHARED, irq_name, ndev);
  635. if (ret) {
  636. dev_err(&ndev->dev, "failure to request IRQ %d\n", irq);
  637. return ret;
  638. }
  639. return 0;
  640. }
  641. /**
  642. * ks8695_init_net - Initialise a KS8695 ethernet interface
  643. * @ksp: The interface to initialise
  644. *
  645. * This routine fills the RX ring, initialises the DMA engines,
  646. * allocates the IRQs and then starts the packet TX and RX
  647. * engines.
  648. */
  649. static int
  650. ks8695_init_net(struct ks8695_priv *ksp)
  651. {
  652. int ret;
  653. u32 ctrl;
  654. ks8695_refill_rxbuffers(ksp);
  655. /* Initialise the DMA engines */
  656. ks8695_writereg(ksp, KS8695_RDLB, (u32) ksp->rx_ring_dma);
  657. ks8695_writereg(ksp, KS8695_TDLB, (u32) ksp->tx_ring_dma);
  658. /* Request the IRQs */
  659. ret = ks8695_setup_irq(ksp->rx_irq, ksp->rx_irq_name,
  660. ks8695_rx_irq, ksp->ndev);
  661. if (ret)
  662. return ret;
  663. ret = ks8695_setup_irq(ksp->tx_irq, ksp->tx_irq_name,
  664. ks8695_tx_irq, ksp->ndev);
  665. if (ret)
  666. return ret;
  667. if (ksp->link_irq != -1) {
  668. ret = ks8695_setup_irq(ksp->link_irq, ksp->link_irq_name,
  669. ks8695_link_irq, ksp->ndev);
  670. if (ret)
  671. return ret;
  672. }
  673. /* Set up the ring indices */
  674. ksp->next_rx_desc_read = 0;
  675. ksp->tx_ring_next_slot = 0;
  676. ksp->tx_ring_used = 0;
  677. /* Bring up transmission */
  678. ctrl = ks8695_readreg(ksp, KS8695_DTXC);
  679. /* Enable packet transmission */
  680. ks8695_writereg(ksp, KS8695_DTXC, ctrl | DTXC_TE);
  681. /* Bring up the reception */
  682. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  683. /* Enable packet reception */
  684. ks8695_writereg(ksp, KS8695_DRXC, ctrl | DRXC_RE);
  685. /* And start the DMA engine */
  686. ks8695_writereg(ksp, KS8695_DRSC, 0);
  687. /* All done */
  688. return 0;
  689. }
  690. /**
  691. * ks8695_release_device - HW resource release for KS8695 e-net
  692. * @ksp: The device to be freed
  693. *
  694. * This unallocates io memory regions, dma-coherent regions etc
  695. * which were allocated in ks8695_probe.
  696. */
  697. static void
  698. ks8695_release_device(struct ks8695_priv *ksp)
  699. {
  700. /* Unmap the registers */
  701. iounmap(ksp->io_regs);
  702. if (ksp->phyiface_regs)
  703. iounmap(ksp->phyiface_regs);
  704. /* And release the request */
  705. release_resource(ksp->regs_req);
  706. kfree(ksp->regs_req);
  707. if (ksp->phyiface_req) {
  708. release_resource(ksp->phyiface_req);
  709. kfree(ksp->phyiface_req);
  710. }
  711. /* Free the ring buffers */
  712. dma_free_coherent(ksp->dev, RING_DMA_SIZE,
  713. ksp->ring_base, ksp->ring_base_dma);
  714. }
  715. /* Ethtool support */
  716. /**
  717. * ks8695_get_msglevel - Get the messages enabled for emission
  718. * @ndev: The network device to read from
  719. */
  720. static u32
  721. ks8695_get_msglevel(struct net_device *ndev)
  722. {
  723. struct ks8695_priv *ksp = netdev_priv(ndev);
  724. return ksp->msg_enable;
  725. }
  726. /**
  727. * ks8695_set_msglevel - Set the messages enabled for emission
  728. * @ndev: The network device to configure
  729. * @value: The messages to set for emission
  730. */
  731. static void
  732. ks8695_set_msglevel(struct net_device *ndev, u32 value)
  733. {
  734. struct ks8695_priv *ksp = netdev_priv(ndev);
  735. ksp->msg_enable = value;
  736. }
  737. /**
  738. * ks8695_wan_get_settings - Get device-specific settings.
  739. * @ndev: The network device to read settings from
  740. * @cmd: The ethtool structure to read into
  741. */
  742. static int
  743. ks8695_wan_get_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
  744. {
  745. struct ks8695_priv *ksp = netdev_priv(ndev);
  746. u32 ctrl;
  747. /* All ports on the KS8695 support these... */
  748. cmd->supported = (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  749. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  750. SUPPORTED_TP | SUPPORTED_MII);
  751. cmd->transceiver = XCVR_INTERNAL;
  752. cmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
  753. cmd->port = PORT_MII;
  754. cmd->supported |= (SUPPORTED_Autoneg | SUPPORTED_Pause);
  755. cmd->phy_address = 0;
  756. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  757. if ((ctrl & WMC_WAND) == 0) {
  758. /* auto-negotiation is enabled */
  759. cmd->advertising |= ADVERTISED_Autoneg;
  760. if (ctrl & WMC_WANA100F)
  761. cmd->advertising |= ADVERTISED_100baseT_Full;
  762. if (ctrl & WMC_WANA100H)
  763. cmd->advertising |= ADVERTISED_100baseT_Half;
  764. if (ctrl & WMC_WANA10F)
  765. cmd->advertising |= ADVERTISED_10baseT_Full;
  766. if (ctrl & WMC_WANA10H)
  767. cmd->advertising |= ADVERTISED_10baseT_Half;
  768. if (ctrl & WMC_WANAP)
  769. cmd->advertising |= ADVERTISED_Pause;
  770. cmd->autoneg = AUTONEG_ENABLE;
  771. ethtool_cmd_speed_set(cmd,
  772. (ctrl & WMC_WSS) ? SPEED_100 : SPEED_10);
  773. cmd->duplex = (ctrl & WMC_WDS) ?
  774. DUPLEX_FULL : DUPLEX_HALF;
  775. } else {
  776. /* auto-negotiation is disabled */
  777. cmd->autoneg = AUTONEG_DISABLE;
  778. ethtool_cmd_speed_set(cmd, ((ctrl & WMC_WANF100) ?
  779. SPEED_100 : SPEED_10));
  780. cmd->duplex = (ctrl & WMC_WANFF) ?
  781. DUPLEX_FULL : DUPLEX_HALF;
  782. }
  783. return 0;
  784. }
  785. /**
  786. * ks8695_wan_set_settings - Set device-specific settings.
  787. * @ndev: The network device to configure
  788. * @cmd: The settings to configure
  789. */
  790. static int
  791. ks8695_wan_set_settings(struct net_device *ndev, struct ethtool_cmd *cmd)
  792. {
  793. struct ks8695_priv *ksp = netdev_priv(ndev);
  794. u32 ctrl;
  795. if ((cmd->speed != SPEED_10) && (cmd->speed != SPEED_100))
  796. return -EINVAL;
  797. if ((cmd->duplex != DUPLEX_HALF) && (cmd->duplex != DUPLEX_FULL))
  798. return -EINVAL;
  799. if (cmd->port != PORT_MII)
  800. return -EINVAL;
  801. if (cmd->transceiver != XCVR_INTERNAL)
  802. return -EINVAL;
  803. if ((cmd->autoneg != AUTONEG_DISABLE) &&
  804. (cmd->autoneg != AUTONEG_ENABLE))
  805. return -EINVAL;
  806. if (cmd->autoneg == AUTONEG_ENABLE) {
  807. if ((cmd->advertising & (ADVERTISED_10baseT_Half |
  808. ADVERTISED_10baseT_Full |
  809. ADVERTISED_100baseT_Half |
  810. ADVERTISED_100baseT_Full)) == 0)
  811. return -EINVAL;
  812. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  813. ctrl &= ~(WMC_WAND | WMC_WANA100F | WMC_WANA100H |
  814. WMC_WANA10F | WMC_WANA10H);
  815. if (cmd->advertising & ADVERTISED_100baseT_Full)
  816. ctrl |= WMC_WANA100F;
  817. if (cmd->advertising & ADVERTISED_100baseT_Half)
  818. ctrl |= WMC_WANA100H;
  819. if (cmd->advertising & ADVERTISED_10baseT_Full)
  820. ctrl |= WMC_WANA10F;
  821. if (cmd->advertising & ADVERTISED_10baseT_Half)
  822. ctrl |= WMC_WANA10H;
  823. /* force a re-negotiation */
  824. ctrl |= WMC_WANR;
  825. writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
  826. } else {
  827. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  828. /* disable auto-negotiation */
  829. ctrl |= WMC_WAND;
  830. ctrl &= ~(WMC_WANF100 | WMC_WANFF);
  831. if (cmd->speed == SPEED_100)
  832. ctrl |= WMC_WANF100;
  833. if (cmd->duplex == DUPLEX_FULL)
  834. ctrl |= WMC_WANFF;
  835. writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
  836. }
  837. return 0;
  838. }
  839. /**
  840. * ks8695_wan_nwayreset - Restart the autonegotiation on the port.
  841. * @ndev: The network device to restart autoneotiation on
  842. */
  843. static int
  844. ks8695_wan_nwayreset(struct net_device *ndev)
  845. {
  846. struct ks8695_priv *ksp = netdev_priv(ndev);
  847. u32 ctrl;
  848. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  849. if ((ctrl & WMC_WAND) == 0)
  850. writel(ctrl | WMC_WANR,
  851. ksp->phyiface_regs + KS8695_WMC);
  852. else
  853. /* auto-negotiation not enabled */
  854. return -EINVAL;
  855. return 0;
  856. }
  857. /**
  858. * ks8695_wan_get_pause - Retrieve network pause/flow-control advertising
  859. * @ndev: The device to retrieve settings from
  860. * @param: The structure to fill out with the information
  861. */
  862. static void
  863. ks8695_wan_get_pause(struct net_device *ndev, struct ethtool_pauseparam *param)
  864. {
  865. struct ks8695_priv *ksp = netdev_priv(ndev);
  866. u32 ctrl;
  867. ctrl = readl(ksp->phyiface_regs + KS8695_WMC);
  868. /* advertise Pause */
  869. param->autoneg = (ctrl & WMC_WANAP);
  870. /* current Rx Flow-control */
  871. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  872. param->rx_pause = (ctrl & DRXC_RFCE);
  873. /* current Tx Flow-control */
  874. ctrl = ks8695_readreg(ksp, KS8695_DTXC);
  875. param->tx_pause = (ctrl & DTXC_TFCE);
  876. }
  877. /**
  878. * ks8695_get_drvinfo - Retrieve driver information
  879. * @ndev: The network device to retrieve info about
  880. * @info: The info structure to fill out.
  881. */
  882. static void
  883. ks8695_get_drvinfo(struct net_device *ndev, struct ethtool_drvinfo *info)
  884. {
  885. strlcpy(info->driver, MODULENAME, sizeof(info->driver));
  886. strlcpy(info->version, MODULEVERSION, sizeof(info->version));
  887. strlcpy(info->bus_info, dev_name(ndev->dev.parent),
  888. sizeof(info->bus_info));
  889. }
  890. static const struct ethtool_ops ks8695_ethtool_ops = {
  891. .get_msglevel = ks8695_get_msglevel,
  892. .set_msglevel = ks8695_set_msglevel,
  893. .get_drvinfo = ks8695_get_drvinfo,
  894. };
  895. static const struct ethtool_ops ks8695_wan_ethtool_ops = {
  896. .get_msglevel = ks8695_get_msglevel,
  897. .set_msglevel = ks8695_set_msglevel,
  898. .get_settings = ks8695_wan_get_settings,
  899. .set_settings = ks8695_wan_set_settings,
  900. .nway_reset = ks8695_wan_nwayreset,
  901. .get_link = ethtool_op_get_link,
  902. .get_pauseparam = ks8695_wan_get_pause,
  903. .get_drvinfo = ks8695_get_drvinfo,
  904. };
  905. /* Network device interface functions */
  906. /**
  907. * ks8695_set_mac - Update MAC in net dev and HW
  908. * @ndev: The network device to update
  909. * @addr: The new MAC address to set
  910. */
  911. static int
  912. ks8695_set_mac(struct net_device *ndev, void *addr)
  913. {
  914. struct ks8695_priv *ksp = netdev_priv(ndev);
  915. struct sockaddr *address = addr;
  916. if (!is_valid_ether_addr(address->sa_data))
  917. return -EADDRNOTAVAIL;
  918. memcpy(ndev->dev_addr, address->sa_data, ndev->addr_len);
  919. ks8695_update_mac(ksp);
  920. dev_dbg(ksp->dev, "%s: Updated MAC address to %pM\n",
  921. ndev->name, ndev->dev_addr);
  922. return 0;
  923. }
  924. /**
  925. * ks8695_set_multicast - Set up the multicast behaviour of the interface
  926. * @ndev: The net_device to configure
  927. *
  928. * This routine, called by the net layer, configures promiscuity
  929. * and multicast reception behaviour for the interface.
  930. */
  931. static void
  932. ks8695_set_multicast(struct net_device *ndev)
  933. {
  934. struct ks8695_priv *ksp = netdev_priv(ndev);
  935. u32 ctrl;
  936. ctrl = ks8695_readreg(ksp, KS8695_DRXC);
  937. if (ndev->flags & IFF_PROMISC) {
  938. /* enable promiscuous mode */
  939. ctrl |= DRXC_RA;
  940. } else if (ndev->flags & ~IFF_PROMISC) {
  941. /* disable promiscuous mode */
  942. ctrl &= ~DRXC_RA;
  943. }
  944. if (ndev->flags & IFF_ALLMULTI) {
  945. /* enable all multicast mode */
  946. ctrl |= DRXC_RM;
  947. } else if (netdev_mc_count(ndev) > KS8695_NR_ADDRESSES) {
  948. /* more specific multicast addresses than can be
  949. * handled in hardware
  950. */
  951. ctrl |= DRXC_RM;
  952. } else {
  953. /* enable specific multicasts */
  954. ctrl &= ~DRXC_RM;
  955. ks8695_init_partial_multicast(ksp, ndev);
  956. }
  957. ks8695_writereg(ksp, KS8695_DRXC, ctrl);
  958. }
  959. /**
  960. * ks8695_timeout - Handle a network tx/rx timeout.
  961. * @ndev: The net_device which timed out.
  962. *
  963. * A network transaction timed out, reset the device.
  964. */
  965. static void
  966. ks8695_timeout(struct net_device *ndev)
  967. {
  968. struct ks8695_priv *ksp = netdev_priv(ndev);
  969. netif_stop_queue(ndev);
  970. ks8695_shutdown(ksp);
  971. ks8695_reset(ksp);
  972. ks8695_update_mac(ksp);
  973. /* We ignore the return from this since it managed to init
  974. * before it probably will be okay to init again.
  975. */
  976. ks8695_init_net(ksp);
  977. /* Reconfigure promiscuity etc */
  978. ks8695_set_multicast(ndev);
  979. /* And start the TX queue once more */
  980. netif_start_queue(ndev);
  981. }
  982. /**
  983. * ks8695_start_xmit - Start a packet transmission
  984. * @skb: The packet to transmit
  985. * @ndev: The network device to send the packet on
  986. *
  987. * This routine, called by the net layer, takes ownership of the
  988. * sk_buff and adds it to the TX ring. It then kicks the TX DMA
  989. * engine to ensure transmission begins.
  990. */
  991. static int
  992. ks8695_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  993. {
  994. struct ks8695_priv *ksp = netdev_priv(ndev);
  995. int buff_n;
  996. dma_addr_t dmap;
  997. spin_lock_irq(&ksp->txq_lock);
  998. if (ksp->tx_ring_used == MAX_TX_DESC) {
  999. /* Somehow we got entered when we have no room */
  1000. spin_unlock_irq(&ksp->txq_lock);
  1001. return NETDEV_TX_BUSY;
  1002. }
  1003. buff_n = ksp->tx_ring_next_slot;
  1004. BUG_ON(ksp->tx_buffers[buff_n].skb);
  1005. dmap = dma_map_single(ksp->dev, skb->data, skb->len, DMA_TO_DEVICE);
  1006. if (unlikely(dma_mapping_error(ksp->dev, dmap))) {
  1007. /* Failed to DMA map this SKB, give it back for now */
  1008. spin_unlock_irq(&ksp->txq_lock);
  1009. dev_dbg(ksp->dev, "%s: Could not map DMA memory for "\
  1010. "transmission, trying later\n", ndev->name);
  1011. return NETDEV_TX_BUSY;
  1012. }
  1013. ksp->tx_buffers[buff_n].dma_ptr = dmap;
  1014. /* Mapped okay, store the buffer pointer and length for later */
  1015. ksp->tx_buffers[buff_n].skb = skb;
  1016. ksp->tx_buffers[buff_n].length = skb->len;
  1017. /* Fill out the TX descriptor */
  1018. ksp->tx_ring[buff_n].data_ptr =
  1019. cpu_to_le32(ksp->tx_buffers[buff_n].dma_ptr);
  1020. ksp->tx_ring[buff_n].status =
  1021. cpu_to_le32(TDES_IC | TDES_FS | TDES_LS |
  1022. (skb->len & TDES_TBS));
  1023. wmb();
  1024. /* Hand it over to the hardware */
  1025. ksp->tx_ring[buff_n].owner = cpu_to_le32(TDES_OWN);
  1026. if (++ksp->tx_ring_used == MAX_TX_DESC)
  1027. netif_stop_queue(ndev);
  1028. /* Kick the TX DMA in case it decided to go IDLE */
  1029. ks8695_writereg(ksp, KS8695_DTSC, 0);
  1030. /* And update the next ring slot */
  1031. ksp->tx_ring_next_slot = (buff_n + 1) & MAX_TX_DESC_MASK;
  1032. spin_unlock_irq(&ksp->txq_lock);
  1033. return NETDEV_TX_OK;
  1034. }
  1035. /**
  1036. * ks8695_stop - Stop (shutdown) a KS8695 ethernet interface
  1037. * @ndev: The net_device to stop
  1038. *
  1039. * This disables the TX queue and cleans up a KS8695 ethernet
  1040. * device.
  1041. */
  1042. static int
  1043. ks8695_stop(struct net_device *ndev)
  1044. {
  1045. struct ks8695_priv *ksp = netdev_priv(ndev);
  1046. netif_stop_queue(ndev);
  1047. napi_disable(&ksp->napi);
  1048. ks8695_shutdown(ksp);
  1049. return 0;
  1050. }
  1051. /**
  1052. * ks8695_open - Open (bring up) a KS8695 ethernet interface
  1053. * @ndev: The net_device to open
  1054. *
  1055. * This resets, configures the MAC, initialises the RX ring and
  1056. * DMA engines and starts the TX queue for a KS8695 ethernet
  1057. * device.
  1058. */
  1059. static int
  1060. ks8695_open(struct net_device *ndev)
  1061. {
  1062. struct ks8695_priv *ksp = netdev_priv(ndev);
  1063. int ret;
  1064. ks8695_reset(ksp);
  1065. ks8695_update_mac(ksp);
  1066. ret = ks8695_init_net(ksp);
  1067. if (ret) {
  1068. ks8695_shutdown(ksp);
  1069. return ret;
  1070. }
  1071. napi_enable(&ksp->napi);
  1072. netif_start_queue(ndev);
  1073. return 0;
  1074. }
  1075. /* Platform device driver */
  1076. /**
  1077. * ks8695_init_switch - Init LAN switch to known good defaults.
  1078. * @ksp: The device to initialise
  1079. *
  1080. * This initialises the LAN switch in the KS8695 to a known-good
  1081. * set of defaults.
  1082. */
  1083. static void
  1084. ks8695_init_switch(struct ks8695_priv *ksp)
  1085. {
  1086. u32 ctrl;
  1087. /* Default value for SEC0 according to datasheet */
  1088. ctrl = 0x40819e00;
  1089. /* LED0 = Speed LED1 = Link/Activity */
  1090. ctrl &= ~(SEC0_LLED1S | SEC0_LLED0S);
  1091. ctrl |= (LLED0S_LINK | LLED1S_LINK_ACTIVITY);
  1092. /* Enable Switch */
  1093. ctrl |= SEC0_ENABLE;
  1094. writel(ctrl, ksp->phyiface_regs + KS8695_SEC0);
  1095. /* Defaults for SEC1 */
  1096. writel(0x9400100, ksp->phyiface_regs + KS8695_SEC1);
  1097. }
  1098. /**
  1099. * ks8695_init_wan_phy - Initialise the WAN PHY to sensible defaults
  1100. * @ksp: The device to initialise
  1101. *
  1102. * This initialises a KS8695's WAN phy to sensible values for
  1103. * autonegotiation etc.
  1104. */
  1105. static void
  1106. ks8695_init_wan_phy(struct ks8695_priv *ksp)
  1107. {
  1108. u32 ctrl;
  1109. /* Support auto-negotiation */
  1110. ctrl = (WMC_WANAP | WMC_WANA100F | WMC_WANA100H |
  1111. WMC_WANA10F | WMC_WANA10H);
  1112. /* LED0 = Activity , LED1 = Link */
  1113. ctrl |= (WLED0S_ACTIVITY | WLED1S_LINK);
  1114. /* Restart Auto-negotiation */
  1115. ctrl |= WMC_WANR;
  1116. writel(ctrl, ksp->phyiface_regs + KS8695_WMC);
  1117. writel(0, ksp->phyiface_regs + KS8695_WPPM);
  1118. writel(0, ksp->phyiface_regs + KS8695_PPS);
  1119. }
  1120. static const struct net_device_ops ks8695_netdev_ops = {
  1121. .ndo_open = ks8695_open,
  1122. .ndo_stop = ks8695_stop,
  1123. .ndo_start_xmit = ks8695_start_xmit,
  1124. .ndo_tx_timeout = ks8695_timeout,
  1125. .ndo_set_mac_address = ks8695_set_mac,
  1126. .ndo_validate_addr = eth_validate_addr,
  1127. .ndo_set_rx_mode = ks8695_set_multicast,
  1128. };
  1129. /**
  1130. * ks8695_probe - Probe and initialise a KS8695 ethernet interface
  1131. * @pdev: The platform device to probe
  1132. *
  1133. * Initialise a KS8695 ethernet device from platform data.
  1134. *
  1135. * This driver requires at least one IORESOURCE_MEM for the
  1136. * registers and two IORESOURCE_IRQ for the RX and TX IRQs
  1137. * respectively. It can optionally take an additional
  1138. * IORESOURCE_MEM for the switch or phy in the case of the lan or
  1139. * wan ports, and an IORESOURCE_IRQ for the link IRQ for the wan
  1140. * port.
  1141. */
  1142. static int
  1143. ks8695_probe(struct platform_device *pdev)
  1144. {
  1145. struct ks8695_priv *ksp;
  1146. struct net_device *ndev;
  1147. struct resource *regs_res, *phyiface_res;
  1148. struct resource *rxirq_res, *txirq_res, *linkirq_res;
  1149. int ret = 0;
  1150. int buff_n;
  1151. bool inv_mac_addr = false;
  1152. u32 machigh, maclow;
  1153. /* Initialise a net_device */
  1154. ndev = alloc_etherdev(sizeof(struct ks8695_priv));
  1155. if (!ndev)
  1156. return -ENOMEM;
  1157. SET_NETDEV_DEV(ndev, &pdev->dev);
  1158. dev_dbg(&pdev->dev, "ks8695_probe() called\n");
  1159. /* Configure our private structure a little */
  1160. ksp = netdev_priv(ndev);
  1161. ksp->dev = &pdev->dev;
  1162. ksp->ndev = ndev;
  1163. ksp->msg_enable = NETIF_MSG_LINK;
  1164. /* Retrieve resources */
  1165. regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1166. phyiface_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1167. rxirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1168. txirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1169. linkirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  1170. if (!(regs_res && rxirq_res && txirq_res)) {
  1171. dev_err(ksp->dev, "insufficient resources\n");
  1172. ret = -ENOENT;
  1173. goto failure;
  1174. }
  1175. ksp->regs_req = request_mem_region(regs_res->start,
  1176. resource_size(regs_res),
  1177. pdev->name);
  1178. if (!ksp->regs_req) {
  1179. dev_err(ksp->dev, "cannot claim register space\n");
  1180. ret = -EIO;
  1181. goto failure;
  1182. }
  1183. ksp->io_regs = ioremap(regs_res->start, resource_size(regs_res));
  1184. if (!ksp->io_regs) {
  1185. dev_err(ksp->dev, "failed to ioremap registers\n");
  1186. ret = -EINVAL;
  1187. goto failure;
  1188. }
  1189. if (phyiface_res) {
  1190. ksp->phyiface_req =
  1191. request_mem_region(phyiface_res->start,
  1192. resource_size(phyiface_res),
  1193. phyiface_res->name);
  1194. if (!ksp->phyiface_req) {
  1195. dev_err(ksp->dev,
  1196. "cannot claim switch register space\n");
  1197. ret = -EIO;
  1198. goto failure;
  1199. }
  1200. ksp->phyiface_regs = ioremap(phyiface_res->start,
  1201. resource_size(phyiface_res));
  1202. if (!ksp->phyiface_regs) {
  1203. dev_err(ksp->dev,
  1204. "failed to ioremap switch registers\n");
  1205. ret = -EINVAL;
  1206. goto failure;
  1207. }
  1208. }
  1209. ksp->rx_irq = rxirq_res->start;
  1210. ksp->rx_irq_name = rxirq_res->name ? rxirq_res->name : "Ethernet RX";
  1211. ksp->tx_irq = txirq_res->start;
  1212. ksp->tx_irq_name = txirq_res->name ? txirq_res->name : "Ethernet TX";
  1213. ksp->link_irq = (linkirq_res ? linkirq_res->start : -1);
  1214. ksp->link_irq_name = (linkirq_res && linkirq_res->name) ?
  1215. linkirq_res->name : "Ethernet Link";
  1216. /* driver system setup */
  1217. ndev->netdev_ops = &ks8695_netdev_ops;
  1218. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1219. netif_napi_add(ndev, &ksp->napi, ks8695_poll, NAPI_WEIGHT);
  1220. /* Retrieve the default MAC addr from the chip. */
  1221. /* The bootloader should have left it in there for us. */
  1222. machigh = ks8695_readreg(ksp, KS8695_MAH);
  1223. maclow = ks8695_readreg(ksp, KS8695_MAL);
  1224. ndev->dev_addr[0] = (machigh >> 8) & 0xFF;
  1225. ndev->dev_addr[1] = machigh & 0xFF;
  1226. ndev->dev_addr[2] = (maclow >> 24) & 0xFF;
  1227. ndev->dev_addr[3] = (maclow >> 16) & 0xFF;
  1228. ndev->dev_addr[4] = (maclow >> 8) & 0xFF;
  1229. ndev->dev_addr[5] = maclow & 0xFF;
  1230. if (!is_valid_ether_addr(ndev->dev_addr))
  1231. inv_mac_addr = true;
  1232. /* In order to be efficient memory-wise, we allocate both
  1233. * rings in one go.
  1234. */
  1235. ksp->ring_base = dma_alloc_coherent(&pdev->dev, RING_DMA_SIZE,
  1236. &ksp->ring_base_dma, GFP_KERNEL);
  1237. if (!ksp->ring_base) {
  1238. ret = -ENOMEM;
  1239. goto failure;
  1240. }
  1241. /* Specify the TX DMA ring buffer */
  1242. ksp->tx_ring = ksp->ring_base;
  1243. ksp->tx_ring_dma = ksp->ring_base_dma;
  1244. /* And initialise the queue's lock */
  1245. spin_lock_init(&ksp->txq_lock);
  1246. spin_lock_init(&ksp->rx_lock);
  1247. /* Specify the RX DMA ring buffer */
  1248. ksp->rx_ring = ksp->ring_base + TX_RING_DMA_SIZE;
  1249. ksp->rx_ring_dma = ksp->ring_base_dma + TX_RING_DMA_SIZE;
  1250. /* Zero the descriptor rings */
  1251. memset(ksp->tx_ring, 0, TX_RING_DMA_SIZE);
  1252. memset(ksp->rx_ring, 0, RX_RING_DMA_SIZE);
  1253. /* Build the rings */
  1254. for (buff_n = 0; buff_n < MAX_TX_DESC; ++buff_n) {
  1255. ksp->tx_ring[buff_n].next_desc =
  1256. cpu_to_le32(ksp->tx_ring_dma +
  1257. (sizeof(struct tx_ring_desc) *
  1258. ((buff_n + 1) & MAX_TX_DESC_MASK)));
  1259. }
  1260. for (buff_n = 0; buff_n < MAX_RX_DESC; ++buff_n) {
  1261. ksp->rx_ring[buff_n].next_desc =
  1262. cpu_to_le32(ksp->rx_ring_dma +
  1263. (sizeof(struct rx_ring_desc) *
  1264. ((buff_n + 1) & MAX_RX_DESC_MASK)));
  1265. }
  1266. /* Initialise the port (physically) */
  1267. if (ksp->phyiface_regs && ksp->link_irq == -1) {
  1268. ks8695_init_switch(ksp);
  1269. ksp->dtype = KS8695_DTYPE_LAN;
  1270. ndev->ethtool_ops = &ks8695_ethtool_ops;
  1271. } else if (ksp->phyiface_regs && ksp->link_irq != -1) {
  1272. ks8695_init_wan_phy(ksp);
  1273. ksp->dtype = KS8695_DTYPE_WAN;
  1274. ndev->ethtool_ops = &ks8695_wan_ethtool_ops;
  1275. } else {
  1276. /* No initialisation since HPNA does not have a PHY */
  1277. ksp->dtype = KS8695_DTYPE_HPNA;
  1278. ndev->ethtool_ops = &ks8695_ethtool_ops;
  1279. }
  1280. /* And bring up the net_device with the net core */
  1281. platform_set_drvdata(pdev, ndev);
  1282. ret = register_netdev(ndev);
  1283. if (ret == 0) {
  1284. if (inv_mac_addr)
  1285. dev_warn(ksp->dev, "%s: Invalid ethernet MAC address. Please set using ip\n",
  1286. ndev->name);
  1287. dev_info(ksp->dev, "ks8695 ethernet (%s) MAC: %pM\n",
  1288. ks8695_port_type(ksp), ndev->dev_addr);
  1289. } else {
  1290. /* Report the failure to register the net_device */
  1291. dev_err(ksp->dev, "ks8695net: failed to register netdev.\n");
  1292. goto failure;
  1293. }
  1294. /* All is well */
  1295. return 0;
  1296. /* Error exit path */
  1297. failure:
  1298. ks8695_release_device(ksp);
  1299. free_netdev(ndev);
  1300. return ret;
  1301. }
  1302. /**
  1303. * ks8695_drv_suspend - Suspend a KS8695 ethernet platform device.
  1304. * @pdev: The device to suspend
  1305. * @state: The suspend state
  1306. *
  1307. * This routine detaches and shuts down a KS8695 ethernet device.
  1308. */
  1309. static int
  1310. ks8695_drv_suspend(struct platform_device *pdev, pm_message_t state)
  1311. {
  1312. struct net_device *ndev = platform_get_drvdata(pdev);
  1313. struct ks8695_priv *ksp = netdev_priv(ndev);
  1314. ksp->in_suspend = 1;
  1315. if (netif_running(ndev)) {
  1316. netif_device_detach(ndev);
  1317. ks8695_shutdown(ksp);
  1318. }
  1319. return 0;
  1320. }
  1321. /**
  1322. * ks8695_drv_resume - Resume a KS8695 ethernet platform device.
  1323. * @pdev: The device to resume
  1324. *
  1325. * This routine re-initialises and re-attaches a KS8695 ethernet
  1326. * device.
  1327. */
  1328. static int
  1329. ks8695_drv_resume(struct platform_device *pdev)
  1330. {
  1331. struct net_device *ndev = platform_get_drvdata(pdev);
  1332. struct ks8695_priv *ksp = netdev_priv(ndev);
  1333. if (netif_running(ndev)) {
  1334. ks8695_reset(ksp);
  1335. ks8695_init_net(ksp);
  1336. ks8695_set_multicast(ndev);
  1337. netif_device_attach(ndev);
  1338. }
  1339. ksp->in_suspend = 0;
  1340. return 0;
  1341. }
  1342. /**
  1343. * ks8695_drv_remove - Remove a KS8695 net device on driver unload.
  1344. * @pdev: The platform device to remove
  1345. *
  1346. * This unregisters and releases a KS8695 ethernet device.
  1347. */
  1348. static int
  1349. ks8695_drv_remove(struct platform_device *pdev)
  1350. {
  1351. struct net_device *ndev = platform_get_drvdata(pdev);
  1352. struct ks8695_priv *ksp = netdev_priv(ndev);
  1353. netif_napi_del(&ksp->napi);
  1354. unregister_netdev(ndev);
  1355. ks8695_release_device(ksp);
  1356. free_netdev(ndev);
  1357. dev_dbg(&pdev->dev, "released and freed device\n");
  1358. return 0;
  1359. }
  1360. static struct platform_driver ks8695_driver = {
  1361. .driver = {
  1362. .name = MODULENAME,
  1363. },
  1364. .probe = ks8695_probe,
  1365. .remove = ks8695_drv_remove,
  1366. .suspend = ks8695_drv_suspend,
  1367. .resume = ks8695_drv_resume,
  1368. };
  1369. module_platform_driver(ks8695_driver);
  1370. MODULE_AUTHOR("Simtec Electronics");
  1371. MODULE_DESCRIPTION("Micrel KS8695 (Centaur) Ethernet driver");
  1372. MODULE_LICENSE("GPL");
  1373. MODULE_ALIAS("platform:" MODULENAME);
  1374. module_param(watchdog, int, 0400);
  1375. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");