ks8695net.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  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. u32 machigh, maclow;
  1152. /* Initialise a net_device */
  1153. ndev = alloc_etherdev(sizeof(struct ks8695_priv));
  1154. if (!ndev)
  1155. return -ENOMEM;
  1156. SET_NETDEV_DEV(ndev, &pdev->dev);
  1157. dev_dbg(&pdev->dev, "ks8695_probe() called\n");
  1158. /* Configure our private structure a little */
  1159. ksp = netdev_priv(ndev);
  1160. ksp->dev = &pdev->dev;
  1161. ksp->ndev = ndev;
  1162. ksp->msg_enable = NETIF_MSG_LINK;
  1163. /* Retrieve resources */
  1164. regs_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1165. phyiface_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1166. rxirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1167. txirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  1168. linkirq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 2);
  1169. if (!(regs_res && rxirq_res && txirq_res)) {
  1170. dev_err(ksp->dev, "insufficient resources\n");
  1171. ret = -ENOENT;
  1172. goto failure;
  1173. }
  1174. ksp->regs_req = request_mem_region(regs_res->start,
  1175. resource_size(regs_res),
  1176. pdev->name);
  1177. if (!ksp->regs_req) {
  1178. dev_err(ksp->dev, "cannot claim register space\n");
  1179. ret = -EIO;
  1180. goto failure;
  1181. }
  1182. ksp->io_regs = ioremap(regs_res->start, resource_size(regs_res));
  1183. if (!ksp->io_regs) {
  1184. dev_err(ksp->dev, "failed to ioremap registers\n");
  1185. ret = -EINVAL;
  1186. goto failure;
  1187. }
  1188. if (phyiface_res) {
  1189. ksp->phyiface_req =
  1190. request_mem_region(phyiface_res->start,
  1191. resource_size(phyiface_res),
  1192. phyiface_res->name);
  1193. if (!ksp->phyiface_req) {
  1194. dev_err(ksp->dev,
  1195. "cannot claim switch register space\n");
  1196. ret = -EIO;
  1197. goto failure;
  1198. }
  1199. ksp->phyiface_regs = ioremap(phyiface_res->start,
  1200. resource_size(phyiface_res));
  1201. if (!ksp->phyiface_regs) {
  1202. dev_err(ksp->dev,
  1203. "failed to ioremap switch registers\n");
  1204. ret = -EINVAL;
  1205. goto failure;
  1206. }
  1207. }
  1208. ksp->rx_irq = rxirq_res->start;
  1209. ksp->rx_irq_name = rxirq_res->name ? rxirq_res->name : "Ethernet RX";
  1210. ksp->tx_irq = txirq_res->start;
  1211. ksp->tx_irq_name = txirq_res->name ? txirq_res->name : "Ethernet TX";
  1212. ksp->link_irq = (linkirq_res ? linkirq_res->start : -1);
  1213. ksp->link_irq_name = (linkirq_res && linkirq_res->name) ?
  1214. linkirq_res->name : "Ethernet Link";
  1215. /* driver system setup */
  1216. ndev->netdev_ops = &ks8695_netdev_ops;
  1217. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
  1218. netif_napi_add(ndev, &ksp->napi, ks8695_poll, NAPI_WEIGHT);
  1219. /* Retrieve the default MAC addr from the chip. */
  1220. /* The bootloader should have left it in there for us. */
  1221. machigh = ks8695_readreg(ksp, KS8695_MAH);
  1222. maclow = ks8695_readreg(ksp, KS8695_MAL);
  1223. ndev->dev_addr[0] = (machigh >> 8) & 0xFF;
  1224. ndev->dev_addr[1] = machigh & 0xFF;
  1225. ndev->dev_addr[2] = (maclow >> 24) & 0xFF;
  1226. ndev->dev_addr[3] = (maclow >> 16) & 0xFF;
  1227. ndev->dev_addr[4] = (maclow >> 8) & 0xFF;
  1228. ndev->dev_addr[5] = maclow & 0xFF;
  1229. if (!is_valid_ether_addr(ndev->dev_addr))
  1230. dev_warn(ksp->dev, "%s: Invalid ethernet MAC address. Please "
  1231. "set using ifconfig\n", ndev->name);
  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. dev_info(ksp->dev, "ks8695 ethernet (%s) MAC: %pM\n",
  1285. ks8695_port_type(ksp), ndev->dev_addr);
  1286. } else {
  1287. /* Report the failure to register the net_device */
  1288. dev_err(ksp->dev, "ks8695net: failed to register netdev.\n");
  1289. goto failure;
  1290. }
  1291. /* All is well */
  1292. return 0;
  1293. /* Error exit path */
  1294. failure:
  1295. ks8695_release_device(ksp);
  1296. free_netdev(ndev);
  1297. return ret;
  1298. }
  1299. /**
  1300. * ks8695_drv_suspend - Suspend a KS8695 ethernet platform device.
  1301. * @pdev: The device to suspend
  1302. * @state: The suspend state
  1303. *
  1304. * This routine detaches and shuts down a KS8695 ethernet device.
  1305. */
  1306. static int
  1307. ks8695_drv_suspend(struct platform_device *pdev, pm_message_t state)
  1308. {
  1309. struct net_device *ndev = platform_get_drvdata(pdev);
  1310. struct ks8695_priv *ksp = netdev_priv(ndev);
  1311. ksp->in_suspend = 1;
  1312. if (netif_running(ndev)) {
  1313. netif_device_detach(ndev);
  1314. ks8695_shutdown(ksp);
  1315. }
  1316. return 0;
  1317. }
  1318. /**
  1319. * ks8695_drv_resume - Resume a KS8695 ethernet platform device.
  1320. * @pdev: The device to resume
  1321. *
  1322. * This routine re-initialises and re-attaches a KS8695 ethernet
  1323. * device.
  1324. */
  1325. static int
  1326. ks8695_drv_resume(struct platform_device *pdev)
  1327. {
  1328. struct net_device *ndev = platform_get_drvdata(pdev);
  1329. struct ks8695_priv *ksp = netdev_priv(ndev);
  1330. if (netif_running(ndev)) {
  1331. ks8695_reset(ksp);
  1332. ks8695_init_net(ksp);
  1333. ks8695_set_multicast(ndev);
  1334. netif_device_attach(ndev);
  1335. }
  1336. ksp->in_suspend = 0;
  1337. return 0;
  1338. }
  1339. /**
  1340. * ks8695_drv_remove - Remove a KS8695 net device on driver unload.
  1341. * @pdev: The platform device to remove
  1342. *
  1343. * This unregisters and releases a KS8695 ethernet device.
  1344. */
  1345. static int
  1346. ks8695_drv_remove(struct platform_device *pdev)
  1347. {
  1348. struct net_device *ndev = platform_get_drvdata(pdev);
  1349. struct ks8695_priv *ksp = netdev_priv(ndev);
  1350. netif_napi_del(&ksp->napi);
  1351. unregister_netdev(ndev);
  1352. ks8695_release_device(ksp);
  1353. free_netdev(ndev);
  1354. dev_dbg(&pdev->dev, "released and freed device\n");
  1355. return 0;
  1356. }
  1357. static struct platform_driver ks8695_driver = {
  1358. .driver = {
  1359. .name = MODULENAME,
  1360. },
  1361. .probe = ks8695_probe,
  1362. .remove = ks8695_drv_remove,
  1363. .suspend = ks8695_drv_suspend,
  1364. .resume = ks8695_drv_resume,
  1365. };
  1366. module_platform_driver(ks8695_driver);
  1367. MODULE_AUTHOR("Simtec Electronics");
  1368. MODULE_DESCRIPTION("Micrel KS8695 (Centaur) Ethernet driver");
  1369. MODULE_LICENSE("GPL");
  1370. MODULE_ALIAS("platform:" MODULENAME);
  1371. module_param(watchdog, int, 0400);
  1372. MODULE_PARM_DESC(watchdog, "transmit timeout in milliseconds");