ks8695net.c 44 KB

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