ks8695net.c 43 KB

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