ks8695net.c 45 KB

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