mv643xx_eth.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. /*
  2. * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
  3. * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
  4. *
  5. * Based on the 64360 driver from:
  6. * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
  7. * Rabeeh Khoury <rabeeh@marvell.com>
  8. *
  9. * Copyright (C) 2003 PMC-Sierra, Inc.,
  10. * written by Manish Lachwani
  11. *
  12. * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
  13. *
  14. * Copyright (C) 2004-2006 MontaVista Software, Inc.
  15. * Dale Farnsworth <dale@farnsworth.org>
  16. *
  17. * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
  18. * <sjhill@realitydiluted.com>
  19. *
  20. * Copyright (C) 2007-2008 Marvell Semiconductor
  21. * Lennert Buytenhek <buytenh@marvell.com>
  22. *
  23. * This program is free software; you can redistribute it and/or
  24. * modify it under the terms of the GNU General Public License
  25. * as published by the Free Software Foundation; either version 2
  26. * of the License, or (at your option) any later version.
  27. *
  28. * This program is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software
  35. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  36. */
  37. #include <linux/init.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/in.h>
  40. #include <linux/tcp.h>
  41. #include <linux/udp.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/delay.h>
  44. #include <linux/ethtool.h>
  45. #include <linux/platform_device.h>
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/spinlock.h>
  49. #include <linux/workqueue.h>
  50. #include <linux/mii.h>
  51. #include <linux/mv643xx_eth.h>
  52. #include <asm/io.h>
  53. #include <asm/types.h>
  54. #include <asm/system.h>
  55. static char mv643xx_eth_driver_name[] = "mv643xx_eth";
  56. static char mv643xx_eth_driver_version[] = "1.1";
  57. #define MV643XX_ETH_CHECKSUM_OFFLOAD_TX
  58. #define MV643XX_ETH_NAPI
  59. #define MV643XX_ETH_TX_FAST_REFILL
  60. #ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
  61. #define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
  62. #else
  63. #define MAX_DESCS_PER_SKB 1
  64. #endif
  65. /*
  66. * Registers shared between all ports.
  67. */
  68. #define PHY_ADDR 0x0000
  69. #define SMI_REG 0x0004
  70. #define WINDOW_BASE(w) (0x0200 + ((w) << 3))
  71. #define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
  72. #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
  73. #define WINDOW_BAR_ENABLE 0x0290
  74. #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
  75. /*
  76. * Per-port registers.
  77. */
  78. #define PORT_CONFIG(p) (0x0400 + ((p) << 10))
  79. #define UNICAST_PROMISCUOUS_MODE 0x00000001
  80. #define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
  81. #define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
  82. #define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
  83. #define SDMA_CONFIG(p) (0x041c + ((p) << 10))
  84. #define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
  85. #define PORT_STATUS(p) (0x0444 + ((p) << 10))
  86. #define TX_FIFO_EMPTY 0x00000400
  87. #define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
  88. #define TXQ_FIX_PRIO_CONF(p) (0x044c + ((p) << 10))
  89. #define TX_BW_RATE(p) (0x0450 + ((p) << 10))
  90. #define TX_BW_MTU(p) (0x0458 + ((p) << 10))
  91. #define TX_BW_BURST(p) (0x045c + ((p) << 10))
  92. #define INT_CAUSE(p) (0x0460 + ((p) << 10))
  93. #define INT_TX_END_0 0x00080000
  94. #define INT_TX_END 0x07f80000
  95. #define INT_RX 0x0007fbfc
  96. #define INT_EXT 0x00000002
  97. #define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
  98. #define INT_EXT_LINK 0x00100000
  99. #define INT_EXT_PHY 0x00010000
  100. #define INT_EXT_TX_ERROR_0 0x00000100
  101. #define INT_EXT_TX_0 0x00000001
  102. #define INT_EXT_TX 0x0000ffff
  103. #define INT_MASK(p) (0x0468 + ((p) << 10))
  104. #define INT_MASK_EXT(p) (0x046c + ((p) << 10))
  105. #define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
  106. #define TXQ_FIX_PRIO_CONF_MOVED(p) (0x04dc + ((p) << 10))
  107. #define TX_BW_RATE_MOVED(p) (0x04e0 + ((p) << 10))
  108. #define TX_BW_MTU_MOVED(p) (0x04e8 + ((p) << 10))
  109. #define TX_BW_BURST_MOVED(p) (0x04ec + ((p) << 10))
  110. #define RXQ_CURRENT_DESC_PTR(p, q) (0x060c + ((p) << 10) + ((q) << 4))
  111. #define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
  112. #define TXQ_CURRENT_DESC_PTR(p, q) (0x06c0 + ((p) << 10) + ((q) << 2))
  113. #define TXQ_BW_TOKENS(p, q) (0x0700 + ((p) << 10) + ((q) << 4))
  114. #define TXQ_BW_CONF(p, q) (0x0704 + ((p) << 10) + ((q) << 4))
  115. #define TXQ_BW_WRR_CONF(p, q) (0x0708 + ((p) << 10) + ((q) << 4))
  116. #define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
  117. #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
  118. #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
  119. #define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
  120. /*
  121. * SDMA configuration register.
  122. */
  123. #define RX_BURST_SIZE_4_64BIT (2 << 1)
  124. #define BLM_RX_NO_SWAP (1 << 4)
  125. #define BLM_TX_NO_SWAP (1 << 5)
  126. #define TX_BURST_SIZE_4_64BIT (2 << 22)
  127. #if defined(__BIG_ENDIAN)
  128. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  129. RX_BURST_SIZE_4_64BIT | \
  130. TX_BURST_SIZE_4_64BIT
  131. #elif defined(__LITTLE_ENDIAN)
  132. #define PORT_SDMA_CONFIG_DEFAULT_VALUE \
  133. RX_BURST_SIZE_4_64BIT | \
  134. BLM_RX_NO_SWAP | \
  135. BLM_TX_NO_SWAP | \
  136. TX_BURST_SIZE_4_64BIT
  137. #else
  138. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  139. #endif
  140. /*
  141. * Port serial control register.
  142. */
  143. #define SET_MII_SPEED_TO_100 (1 << 24)
  144. #define SET_GMII_SPEED_TO_1000 (1 << 23)
  145. #define SET_FULL_DUPLEX_MODE (1 << 21)
  146. #define MAX_RX_PACKET_1522BYTE (1 << 17)
  147. #define MAX_RX_PACKET_9700BYTE (5 << 17)
  148. #define MAX_RX_PACKET_MASK (7 << 17)
  149. #define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
  150. #define DO_NOT_FORCE_LINK_FAIL (1 << 10)
  151. #define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
  152. #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
  153. #define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
  154. #define FORCE_LINK_PASS (1 << 1)
  155. #define SERIAL_PORT_ENABLE (1 << 0)
  156. #define DEFAULT_RX_QUEUE_SIZE 400
  157. #define DEFAULT_TX_QUEUE_SIZE 800
  158. /*
  159. * RX/TX descriptors.
  160. */
  161. #if defined(__BIG_ENDIAN)
  162. struct rx_desc {
  163. u16 byte_cnt; /* Descriptor buffer byte count */
  164. u16 buf_size; /* Buffer size */
  165. u32 cmd_sts; /* Descriptor command status */
  166. u32 next_desc_ptr; /* Next descriptor pointer */
  167. u32 buf_ptr; /* Descriptor buffer pointer */
  168. };
  169. struct tx_desc {
  170. u16 byte_cnt; /* buffer byte count */
  171. u16 l4i_chk; /* CPU provided TCP checksum */
  172. u32 cmd_sts; /* Command/status field */
  173. u32 next_desc_ptr; /* Pointer to next descriptor */
  174. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  175. };
  176. #elif defined(__LITTLE_ENDIAN)
  177. struct rx_desc {
  178. u32 cmd_sts; /* Descriptor command status */
  179. u16 buf_size; /* Buffer size */
  180. u16 byte_cnt; /* Descriptor buffer byte count */
  181. u32 buf_ptr; /* Descriptor buffer pointer */
  182. u32 next_desc_ptr; /* Next descriptor pointer */
  183. };
  184. struct tx_desc {
  185. u32 cmd_sts; /* Command/status field */
  186. u16 l4i_chk; /* CPU provided TCP checksum */
  187. u16 byte_cnt; /* buffer byte count */
  188. u32 buf_ptr; /* pointer to buffer for this descriptor*/
  189. u32 next_desc_ptr; /* Pointer to next descriptor */
  190. };
  191. #else
  192. #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
  193. #endif
  194. /* RX & TX descriptor command */
  195. #define BUFFER_OWNED_BY_DMA 0x80000000
  196. /* RX & TX descriptor status */
  197. #define ERROR_SUMMARY 0x00000001
  198. /* RX descriptor status */
  199. #define LAYER_4_CHECKSUM_OK 0x40000000
  200. #define RX_ENABLE_INTERRUPT 0x20000000
  201. #define RX_FIRST_DESC 0x08000000
  202. #define RX_LAST_DESC 0x04000000
  203. /* TX descriptor command */
  204. #define TX_ENABLE_INTERRUPT 0x00800000
  205. #define GEN_CRC 0x00400000
  206. #define TX_FIRST_DESC 0x00200000
  207. #define TX_LAST_DESC 0x00100000
  208. #define ZERO_PADDING 0x00080000
  209. #define GEN_IP_V4_CHECKSUM 0x00040000
  210. #define GEN_TCP_UDP_CHECKSUM 0x00020000
  211. #define UDP_FRAME 0x00010000
  212. #define TX_IHL_SHIFT 11
  213. /* global *******************************************************************/
  214. struct mv643xx_eth_shared_private {
  215. /*
  216. * Ethernet controller base address.
  217. */
  218. void __iomem *base;
  219. /*
  220. * Protects access to SMI_REG, which is shared between ports.
  221. */
  222. spinlock_t phy_lock;
  223. /*
  224. * Per-port MBUS window access register value.
  225. */
  226. u32 win_protect;
  227. /*
  228. * Hardware-specific parameters.
  229. */
  230. unsigned int t_clk;
  231. int extended_rx_coal_limit;
  232. int tx_bw_control_moved;
  233. };
  234. /* per-port *****************************************************************/
  235. struct mib_counters {
  236. u64 good_octets_received;
  237. u32 bad_octets_received;
  238. u32 internal_mac_transmit_err;
  239. u32 good_frames_received;
  240. u32 bad_frames_received;
  241. u32 broadcast_frames_received;
  242. u32 multicast_frames_received;
  243. u32 frames_64_octets;
  244. u32 frames_65_to_127_octets;
  245. u32 frames_128_to_255_octets;
  246. u32 frames_256_to_511_octets;
  247. u32 frames_512_to_1023_octets;
  248. u32 frames_1024_to_max_octets;
  249. u64 good_octets_sent;
  250. u32 good_frames_sent;
  251. u32 excessive_collision;
  252. u32 multicast_frames_sent;
  253. u32 broadcast_frames_sent;
  254. u32 unrec_mac_control_received;
  255. u32 fc_sent;
  256. u32 good_fc_received;
  257. u32 bad_fc_received;
  258. u32 undersize_received;
  259. u32 fragments_received;
  260. u32 oversize_received;
  261. u32 jabber_received;
  262. u32 mac_receive_error;
  263. u32 bad_crc_event;
  264. u32 collision;
  265. u32 late_collision;
  266. };
  267. struct rx_queue {
  268. int index;
  269. int rx_ring_size;
  270. int rx_desc_count;
  271. int rx_curr_desc;
  272. int rx_used_desc;
  273. struct rx_desc *rx_desc_area;
  274. dma_addr_t rx_desc_dma;
  275. int rx_desc_area_size;
  276. struct sk_buff **rx_skb;
  277. struct timer_list rx_oom;
  278. };
  279. struct tx_queue {
  280. int index;
  281. int tx_ring_size;
  282. int tx_desc_count;
  283. int tx_curr_desc;
  284. int tx_used_desc;
  285. struct tx_desc *tx_desc_area;
  286. dma_addr_t tx_desc_dma;
  287. int tx_desc_area_size;
  288. struct sk_buff **tx_skb;
  289. };
  290. struct mv643xx_eth_private {
  291. struct mv643xx_eth_shared_private *shared;
  292. int port_num;
  293. struct net_device *dev;
  294. struct mv643xx_eth_shared_private *shared_smi;
  295. int phy_addr;
  296. spinlock_t lock;
  297. struct mib_counters mib_counters;
  298. struct work_struct tx_timeout_task;
  299. struct mii_if_info mii;
  300. /*
  301. * RX state.
  302. */
  303. int default_rx_ring_size;
  304. unsigned long rx_desc_sram_addr;
  305. int rx_desc_sram_size;
  306. u8 rxq_mask;
  307. int rxq_primary;
  308. struct napi_struct napi;
  309. struct rx_queue rxq[8];
  310. /*
  311. * TX state.
  312. */
  313. int default_tx_ring_size;
  314. unsigned long tx_desc_sram_addr;
  315. int tx_desc_sram_size;
  316. u8 txq_mask;
  317. int txq_primary;
  318. struct tx_queue txq[8];
  319. #ifdef MV643XX_ETH_TX_FAST_REFILL
  320. int tx_clean_threshold;
  321. #endif
  322. };
  323. /* port register accessors **************************************************/
  324. static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
  325. {
  326. return readl(mp->shared->base + offset);
  327. }
  328. static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
  329. {
  330. writel(data, mp->shared->base + offset);
  331. }
  332. /* rxq/txq helper functions *************************************************/
  333. static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
  334. {
  335. return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
  336. }
  337. static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
  338. {
  339. return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
  340. }
  341. static void rxq_enable(struct rx_queue *rxq)
  342. {
  343. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  344. wrl(mp, RXQ_COMMAND(mp->port_num), 1 << rxq->index);
  345. }
  346. static void rxq_disable(struct rx_queue *rxq)
  347. {
  348. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  349. u8 mask = 1 << rxq->index;
  350. wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
  351. while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
  352. udelay(10);
  353. }
  354. static void txq_reset_hw_ptr(struct tx_queue *txq)
  355. {
  356. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  357. int off = TXQ_CURRENT_DESC_PTR(mp->port_num, txq->index);
  358. u32 addr;
  359. addr = (u32)txq->tx_desc_dma;
  360. addr += txq->tx_curr_desc * sizeof(struct tx_desc);
  361. wrl(mp, off, addr);
  362. }
  363. static void txq_enable(struct tx_queue *txq)
  364. {
  365. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  366. wrl(mp, TXQ_COMMAND(mp->port_num), 1 << txq->index);
  367. }
  368. static void txq_disable(struct tx_queue *txq)
  369. {
  370. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  371. u8 mask = 1 << txq->index;
  372. wrl(mp, TXQ_COMMAND(mp->port_num), mask << 8);
  373. while (rdl(mp, TXQ_COMMAND(mp->port_num)) & mask)
  374. udelay(10);
  375. }
  376. static void __txq_maybe_wake(struct tx_queue *txq)
  377. {
  378. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  379. /*
  380. * netif_{stop,wake}_queue() flow control only applies to
  381. * the primary queue.
  382. */
  383. BUG_ON(txq->index != mp->txq_primary);
  384. if (txq->tx_ring_size - txq->tx_desc_count >= MAX_DESCS_PER_SKB)
  385. netif_wake_queue(mp->dev);
  386. }
  387. /* rx ***********************************************************************/
  388. static void txq_reclaim(struct tx_queue *txq, int force);
  389. static void rxq_refill(struct rx_queue *rxq)
  390. {
  391. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  392. unsigned long flags;
  393. spin_lock_irqsave(&mp->lock, flags);
  394. while (rxq->rx_desc_count < rxq->rx_ring_size) {
  395. int skb_size;
  396. struct sk_buff *skb;
  397. int unaligned;
  398. int rx;
  399. /*
  400. * Reserve 2+14 bytes for an ethernet header (the
  401. * hardware automatically prepends 2 bytes of dummy
  402. * data to each received packet), 4 bytes for a VLAN
  403. * header, and 4 bytes for the trailing FCS -- 24
  404. * bytes total.
  405. */
  406. skb_size = mp->dev->mtu + 24;
  407. skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
  408. if (skb == NULL)
  409. break;
  410. unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
  411. if (unaligned)
  412. skb_reserve(skb, dma_get_cache_alignment() - unaligned);
  413. rxq->rx_desc_count++;
  414. rx = rxq->rx_used_desc;
  415. rxq->rx_used_desc = (rx + 1) % rxq->rx_ring_size;
  416. rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
  417. skb_size, DMA_FROM_DEVICE);
  418. rxq->rx_desc_area[rx].buf_size = skb_size;
  419. rxq->rx_skb[rx] = skb;
  420. wmb();
  421. rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
  422. RX_ENABLE_INTERRUPT;
  423. wmb();
  424. /*
  425. * The hardware automatically prepends 2 bytes of
  426. * dummy data to each received packet, so that the
  427. * IP header ends up 16-byte aligned.
  428. */
  429. skb_reserve(skb, 2);
  430. }
  431. if (rxq->rx_desc_count != rxq->rx_ring_size) {
  432. rxq->rx_oom.expires = jiffies + (HZ / 10);
  433. add_timer(&rxq->rx_oom);
  434. }
  435. spin_unlock_irqrestore(&mp->lock, flags);
  436. }
  437. static inline void rxq_refill_timer_wrapper(unsigned long data)
  438. {
  439. rxq_refill((struct rx_queue *)data);
  440. }
  441. static int rxq_process(struct rx_queue *rxq, int budget)
  442. {
  443. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  444. struct net_device_stats *stats = &mp->dev->stats;
  445. int rx;
  446. rx = 0;
  447. while (rx < budget) {
  448. struct rx_desc *rx_desc;
  449. unsigned int cmd_sts;
  450. struct sk_buff *skb;
  451. unsigned long flags;
  452. spin_lock_irqsave(&mp->lock, flags);
  453. rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
  454. cmd_sts = rx_desc->cmd_sts;
  455. if (cmd_sts & BUFFER_OWNED_BY_DMA) {
  456. spin_unlock_irqrestore(&mp->lock, flags);
  457. break;
  458. }
  459. rmb();
  460. skb = rxq->rx_skb[rxq->rx_curr_desc];
  461. rxq->rx_skb[rxq->rx_curr_desc] = NULL;
  462. rxq->rx_curr_desc = (rxq->rx_curr_desc + 1) % rxq->rx_ring_size;
  463. spin_unlock_irqrestore(&mp->lock, flags);
  464. dma_unmap_single(NULL, rx_desc->buf_ptr + 2,
  465. mp->dev->mtu + 24, DMA_FROM_DEVICE);
  466. rxq->rx_desc_count--;
  467. rx++;
  468. /*
  469. * Update statistics.
  470. *
  471. * Note that the descriptor byte count includes 2 dummy
  472. * bytes automatically inserted by the hardware at the
  473. * start of the packet (which we don't count), and a 4
  474. * byte CRC at the end of the packet (which we do count).
  475. */
  476. stats->rx_packets++;
  477. stats->rx_bytes += rx_desc->byte_cnt - 2;
  478. /*
  479. * In case we received a packet without first / last bits
  480. * on, or the error summary bit is set, the packet needs
  481. * to be dropped.
  482. */
  483. if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  484. (RX_FIRST_DESC | RX_LAST_DESC))
  485. || (cmd_sts & ERROR_SUMMARY)) {
  486. stats->rx_dropped++;
  487. if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
  488. (RX_FIRST_DESC | RX_LAST_DESC)) {
  489. if (net_ratelimit())
  490. dev_printk(KERN_ERR, &mp->dev->dev,
  491. "received packet spanning "
  492. "multiple descriptors\n");
  493. }
  494. if (cmd_sts & ERROR_SUMMARY)
  495. stats->rx_errors++;
  496. dev_kfree_skb_irq(skb);
  497. } else {
  498. /*
  499. * The -4 is for the CRC in the trailer of the
  500. * received packet
  501. */
  502. skb_put(skb, rx_desc->byte_cnt - 2 - 4);
  503. if (cmd_sts & LAYER_4_CHECKSUM_OK) {
  504. skb->ip_summed = CHECKSUM_UNNECESSARY;
  505. skb->csum = htons(
  506. (cmd_sts & 0x0007fff8) >> 3);
  507. }
  508. skb->protocol = eth_type_trans(skb, mp->dev);
  509. #ifdef MV643XX_ETH_NAPI
  510. netif_receive_skb(skb);
  511. #else
  512. netif_rx(skb);
  513. #endif
  514. }
  515. mp->dev->last_rx = jiffies;
  516. }
  517. rxq_refill(rxq);
  518. return rx;
  519. }
  520. #ifdef MV643XX_ETH_NAPI
  521. static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
  522. {
  523. struct mv643xx_eth_private *mp;
  524. int rx;
  525. int i;
  526. mp = container_of(napi, struct mv643xx_eth_private, napi);
  527. #ifdef MV643XX_ETH_TX_FAST_REFILL
  528. if (++mp->tx_clean_threshold > 5) {
  529. mp->tx_clean_threshold = 0;
  530. for (i = 0; i < 8; i++)
  531. if (mp->txq_mask & (1 << i))
  532. txq_reclaim(mp->txq + i, 0);
  533. }
  534. #endif
  535. rx = 0;
  536. for (i = 7; rx < budget && i >= 0; i--)
  537. if (mp->rxq_mask & (1 << i))
  538. rx += rxq_process(mp->rxq + i, budget - rx);
  539. if (rx < budget) {
  540. netif_rx_complete(mp->dev, napi);
  541. wrl(mp, INT_CAUSE(mp->port_num), 0);
  542. wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
  543. wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
  544. }
  545. return rx;
  546. }
  547. #endif
  548. /* tx ***********************************************************************/
  549. static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
  550. {
  551. int frag;
  552. for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
  553. skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  554. if (fragp->size <= 8 && fragp->page_offset & 7)
  555. return 1;
  556. }
  557. return 0;
  558. }
  559. static int txq_alloc_desc_index(struct tx_queue *txq)
  560. {
  561. int tx_desc_curr;
  562. BUG_ON(txq->tx_desc_count >= txq->tx_ring_size);
  563. tx_desc_curr = txq->tx_curr_desc;
  564. txq->tx_curr_desc = (tx_desc_curr + 1) % txq->tx_ring_size;
  565. BUG_ON(txq->tx_curr_desc == txq->tx_used_desc);
  566. return tx_desc_curr;
  567. }
  568. static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
  569. {
  570. int nr_frags = skb_shinfo(skb)->nr_frags;
  571. int frag;
  572. for (frag = 0; frag < nr_frags; frag++) {
  573. skb_frag_t *this_frag;
  574. int tx_index;
  575. struct tx_desc *desc;
  576. this_frag = &skb_shinfo(skb)->frags[frag];
  577. tx_index = txq_alloc_desc_index(txq);
  578. desc = &txq->tx_desc_area[tx_index];
  579. /*
  580. * The last fragment will generate an interrupt
  581. * which will free the skb on TX completion.
  582. */
  583. if (frag == nr_frags - 1) {
  584. desc->cmd_sts = BUFFER_OWNED_BY_DMA |
  585. ZERO_PADDING | TX_LAST_DESC |
  586. TX_ENABLE_INTERRUPT;
  587. txq->tx_skb[tx_index] = skb;
  588. } else {
  589. desc->cmd_sts = BUFFER_OWNED_BY_DMA;
  590. txq->tx_skb[tx_index] = NULL;
  591. }
  592. desc->l4i_chk = 0;
  593. desc->byte_cnt = this_frag->size;
  594. desc->buf_ptr = dma_map_page(NULL, this_frag->page,
  595. this_frag->page_offset,
  596. this_frag->size,
  597. DMA_TO_DEVICE);
  598. }
  599. }
  600. static inline __be16 sum16_as_be(__sum16 sum)
  601. {
  602. return (__force __be16)sum;
  603. }
  604. static void txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
  605. {
  606. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  607. int nr_frags = skb_shinfo(skb)->nr_frags;
  608. int tx_index;
  609. struct tx_desc *desc;
  610. u32 cmd_sts;
  611. int length;
  612. cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
  613. tx_index = txq_alloc_desc_index(txq);
  614. desc = &txq->tx_desc_area[tx_index];
  615. if (nr_frags) {
  616. txq_submit_frag_skb(txq, skb);
  617. length = skb_headlen(skb);
  618. txq->tx_skb[tx_index] = NULL;
  619. } else {
  620. cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
  621. length = skb->len;
  622. txq->tx_skb[tx_index] = skb;
  623. }
  624. desc->byte_cnt = length;
  625. desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
  626. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  627. BUG_ON(skb->protocol != htons(ETH_P_IP));
  628. cmd_sts |= GEN_TCP_UDP_CHECKSUM |
  629. GEN_IP_V4_CHECKSUM |
  630. ip_hdr(skb)->ihl << TX_IHL_SHIFT;
  631. switch (ip_hdr(skb)->protocol) {
  632. case IPPROTO_UDP:
  633. cmd_sts |= UDP_FRAME;
  634. desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
  635. break;
  636. case IPPROTO_TCP:
  637. desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
  638. break;
  639. default:
  640. BUG();
  641. }
  642. } else {
  643. /* Errata BTS #50, IHL must be 5 if no HW checksum */
  644. cmd_sts |= 5 << TX_IHL_SHIFT;
  645. desc->l4i_chk = 0;
  646. }
  647. /* ensure all other descriptors are written before first cmd_sts */
  648. wmb();
  649. desc->cmd_sts = cmd_sts;
  650. /* clear TX_END interrupt status */
  651. wrl(mp, INT_CAUSE(mp->port_num), ~(INT_TX_END_0 << txq->index));
  652. rdl(mp, INT_CAUSE(mp->port_num));
  653. /* ensure all descriptors are written before poking hardware */
  654. wmb();
  655. txq_enable(txq);
  656. txq->tx_desc_count += nr_frags + 1;
  657. }
  658. static int mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  659. {
  660. struct mv643xx_eth_private *mp = netdev_priv(dev);
  661. struct net_device_stats *stats = &dev->stats;
  662. struct tx_queue *txq;
  663. unsigned long flags;
  664. if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
  665. stats->tx_dropped++;
  666. dev_printk(KERN_DEBUG, &dev->dev,
  667. "failed to linearize skb with tiny "
  668. "unaligned fragment\n");
  669. return NETDEV_TX_BUSY;
  670. }
  671. spin_lock_irqsave(&mp->lock, flags);
  672. txq = mp->txq + mp->txq_primary;
  673. if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB) {
  674. spin_unlock_irqrestore(&mp->lock, flags);
  675. if (txq->index == mp->txq_primary && net_ratelimit())
  676. dev_printk(KERN_ERR, &dev->dev,
  677. "primary tx queue full?!\n");
  678. kfree_skb(skb);
  679. return NETDEV_TX_OK;
  680. }
  681. txq_submit_skb(txq, skb);
  682. stats->tx_bytes += skb->len;
  683. stats->tx_packets++;
  684. dev->trans_start = jiffies;
  685. if (txq->index == mp->txq_primary) {
  686. int entries_left;
  687. entries_left = txq->tx_ring_size - txq->tx_desc_count;
  688. if (entries_left < MAX_DESCS_PER_SKB)
  689. netif_stop_queue(dev);
  690. }
  691. spin_unlock_irqrestore(&mp->lock, flags);
  692. return NETDEV_TX_OK;
  693. }
  694. /* tx rate control **********************************************************/
  695. /*
  696. * Set total maximum TX rate (shared by all TX queues for this port)
  697. * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
  698. */
  699. static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
  700. {
  701. int token_rate;
  702. int mtu;
  703. int bucket_size;
  704. token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
  705. if (token_rate > 1023)
  706. token_rate = 1023;
  707. mtu = (mp->dev->mtu + 255) >> 8;
  708. if (mtu > 63)
  709. mtu = 63;
  710. bucket_size = (burst + 255) >> 8;
  711. if (bucket_size > 65535)
  712. bucket_size = 65535;
  713. if (mp->shared->tx_bw_control_moved) {
  714. wrl(mp, TX_BW_RATE_MOVED(mp->port_num), token_rate);
  715. wrl(mp, TX_BW_MTU_MOVED(mp->port_num), mtu);
  716. wrl(mp, TX_BW_BURST_MOVED(mp->port_num), bucket_size);
  717. } else {
  718. wrl(mp, TX_BW_RATE(mp->port_num), token_rate);
  719. wrl(mp, TX_BW_MTU(mp->port_num), mtu);
  720. wrl(mp, TX_BW_BURST(mp->port_num), bucket_size);
  721. }
  722. }
  723. static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
  724. {
  725. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  726. int token_rate;
  727. int bucket_size;
  728. token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
  729. if (token_rate > 1023)
  730. token_rate = 1023;
  731. bucket_size = (burst + 255) >> 8;
  732. if (bucket_size > 65535)
  733. bucket_size = 65535;
  734. wrl(mp, TXQ_BW_TOKENS(mp->port_num, txq->index), token_rate << 14);
  735. wrl(mp, TXQ_BW_CONF(mp->port_num, txq->index),
  736. (bucket_size << 10) | token_rate);
  737. }
  738. static void txq_set_fixed_prio_mode(struct tx_queue *txq)
  739. {
  740. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  741. int off;
  742. u32 val;
  743. /*
  744. * Turn on fixed priority mode.
  745. */
  746. if (mp->shared->tx_bw_control_moved)
  747. off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
  748. else
  749. off = TXQ_FIX_PRIO_CONF(mp->port_num);
  750. val = rdl(mp, off);
  751. val |= 1 << txq->index;
  752. wrl(mp, off, val);
  753. }
  754. static void txq_set_wrr(struct tx_queue *txq, int weight)
  755. {
  756. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  757. int off;
  758. u32 val;
  759. /*
  760. * Turn off fixed priority mode.
  761. */
  762. if (mp->shared->tx_bw_control_moved)
  763. off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
  764. else
  765. off = TXQ_FIX_PRIO_CONF(mp->port_num);
  766. val = rdl(mp, off);
  767. val &= ~(1 << txq->index);
  768. wrl(mp, off, val);
  769. /*
  770. * Configure WRR weight for this queue.
  771. */
  772. off = TXQ_BW_WRR_CONF(mp->port_num, txq->index);
  773. val = rdl(mp, off);
  774. val = (val & ~0xff) | (weight & 0xff);
  775. wrl(mp, off, val);
  776. }
  777. /* mii management interface *************************************************/
  778. #define SMI_BUSY 0x10000000
  779. #define SMI_READ_VALID 0x08000000
  780. #define SMI_OPCODE_READ 0x04000000
  781. #define SMI_OPCODE_WRITE 0x00000000
  782. static void smi_reg_read(struct mv643xx_eth_private *mp, unsigned int addr,
  783. unsigned int reg, unsigned int *value)
  784. {
  785. void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
  786. unsigned long flags;
  787. int i;
  788. /* the SMI register is a shared resource */
  789. spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
  790. /* wait for the SMI register to become available */
  791. for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
  792. if (i == 1000) {
  793. printk("%s: PHY busy timeout\n", mp->dev->name);
  794. goto out;
  795. }
  796. udelay(10);
  797. }
  798. writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
  799. /* now wait for the data to be valid */
  800. for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
  801. if (i == 1000) {
  802. printk("%s: PHY read timeout\n", mp->dev->name);
  803. goto out;
  804. }
  805. udelay(10);
  806. }
  807. *value = readl(smi_reg) & 0xffff;
  808. out:
  809. spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
  810. }
  811. static void smi_reg_write(struct mv643xx_eth_private *mp,
  812. unsigned int addr,
  813. unsigned int reg, unsigned int value)
  814. {
  815. void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
  816. unsigned long flags;
  817. int i;
  818. /* the SMI register is a shared resource */
  819. spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
  820. /* wait for the SMI register to become available */
  821. for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
  822. if (i == 1000) {
  823. printk("%s: PHY busy timeout\n", mp->dev->name);
  824. goto out;
  825. }
  826. udelay(10);
  827. }
  828. writel(SMI_OPCODE_WRITE | (reg << 21) |
  829. (addr << 16) | (value & 0xffff), smi_reg);
  830. out:
  831. spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
  832. }
  833. /* mib counters *************************************************************/
  834. static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
  835. {
  836. return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
  837. }
  838. static void mib_counters_clear(struct mv643xx_eth_private *mp)
  839. {
  840. int i;
  841. for (i = 0; i < 0x80; i += 4)
  842. mib_read(mp, i);
  843. }
  844. static void mib_counters_update(struct mv643xx_eth_private *mp)
  845. {
  846. struct mib_counters *p = &mp->mib_counters;
  847. p->good_octets_received += mib_read(mp, 0x00);
  848. p->good_octets_received += (u64)mib_read(mp, 0x04) << 32;
  849. p->bad_octets_received += mib_read(mp, 0x08);
  850. p->internal_mac_transmit_err += mib_read(mp, 0x0c);
  851. p->good_frames_received += mib_read(mp, 0x10);
  852. p->bad_frames_received += mib_read(mp, 0x14);
  853. p->broadcast_frames_received += mib_read(mp, 0x18);
  854. p->multicast_frames_received += mib_read(mp, 0x1c);
  855. p->frames_64_octets += mib_read(mp, 0x20);
  856. p->frames_65_to_127_octets += mib_read(mp, 0x24);
  857. p->frames_128_to_255_octets += mib_read(mp, 0x28);
  858. p->frames_256_to_511_octets += mib_read(mp, 0x2c);
  859. p->frames_512_to_1023_octets += mib_read(mp, 0x30);
  860. p->frames_1024_to_max_octets += mib_read(mp, 0x34);
  861. p->good_octets_sent += mib_read(mp, 0x38);
  862. p->good_octets_sent += (u64)mib_read(mp, 0x3c) << 32;
  863. p->good_frames_sent += mib_read(mp, 0x40);
  864. p->excessive_collision += mib_read(mp, 0x44);
  865. p->multicast_frames_sent += mib_read(mp, 0x48);
  866. p->broadcast_frames_sent += mib_read(mp, 0x4c);
  867. p->unrec_mac_control_received += mib_read(mp, 0x50);
  868. p->fc_sent += mib_read(mp, 0x54);
  869. p->good_fc_received += mib_read(mp, 0x58);
  870. p->bad_fc_received += mib_read(mp, 0x5c);
  871. p->undersize_received += mib_read(mp, 0x60);
  872. p->fragments_received += mib_read(mp, 0x64);
  873. p->oversize_received += mib_read(mp, 0x68);
  874. p->jabber_received += mib_read(mp, 0x6c);
  875. p->mac_receive_error += mib_read(mp, 0x70);
  876. p->bad_crc_event += mib_read(mp, 0x74);
  877. p->collision += mib_read(mp, 0x78);
  878. p->late_collision += mib_read(mp, 0x7c);
  879. }
  880. /* ethtool ******************************************************************/
  881. struct mv643xx_eth_stats {
  882. char stat_string[ETH_GSTRING_LEN];
  883. int sizeof_stat;
  884. int netdev_off;
  885. int mp_off;
  886. };
  887. #define SSTAT(m) \
  888. { #m, FIELD_SIZEOF(struct net_device_stats, m), \
  889. offsetof(struct net_device, stats.m), -1 }
  890. #define MIBSTAT(m) \
  891. { #m, FIELD_SIZEOF(struct mib_counters, m), \
  892. -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
  893. static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
  894. SSTAT(rx_packets),
  895. SSTAT(tx_packets),
  896. SSTAT(rx_bytes),
  897. SSTAT(tx_bytes),
  898. SSTAT(rx_errors),
  899. SSTAT(tx_errors),
  900. SSTAT(rx_dropped),
  901. SSTAT(tx_dropped),
  902. MIBSTAT(good_octets_received),
  903. MIBSTAT(bad_octets_received),
  904. MIBSTAT(internal_mac_transmit_err),
  905. MIBSTAT(good_frames_received),
  906. MIBSTAT(bad_frames_received),
  907. MIBSTAT(broadcast_frames_received),
  908. MIBSTAT(multicast_frames_received),
  909. MIBSTAT(frames_64_octets),
  910. MIBSTAT(frames_65_to_127_octets),
  911. MIBSTAT(frames_128_to_255_octets),
  912. MIBSTAT(frames_256_to_511_octets),
  913. MIBSTAT(frames_512_to_1023_octets),
  914. MIBSTAT(frames_1024_to_max_octets),
  915. MIBSTAT(good_octets_sent),
  916. MIBSTAT(good_frames_sent),
  917. MIBSTAT(excessive_collision),
  918. MIBSTAT(multicast_frames_sent),
  919. MIBSTAT(broadcast_frames_sent),
  920. MIBSTAT(unrec_mac_control_received),
  921. MIBSTAT(fc_sent),
  922. MIBSTAT(good_fc_received),
  923. MIBSTAT(bad_fc_received),
  924. MIBSTAT(undersize_received),
  925. MIBSTAT(fragments_received),
  926. MIBSTAT(oversize_received),
  927. MIBSTAT(jabber_received),
  928. MIBSTAT(mac_receive_error),
  929. MIBSTAT(bad_crc_event),
  930. MIBSTAT(collision),
  931. MIBSTAT(late_collision),
  932. };
  933. static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  934. {
  935. struct mv643xx_eth_private *mp = netdev_priv(dev);
  936. int err;
  937. spin_lock_irq(&mp->lock);
  938. err = mii_ethtool_gset(&mp->mii, cmd);
  939. spin_unlock_irq(&mp->lock);
  940. /*
  941. * The MAC does not support 1000baseT_Half.
  942. */
  943. cmd->supported &= ~SUPPORTED_1000baseT_Half;
  944. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  945. return err;
  946. }
  947. static int mv643xx_eth_get_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
  948. {
  949. cmd->supported = SUPPORTED_MII;
  950. cmd->advertising = ADVERTISED_MII;
  951. cmd->speed = SPEED_1000;
  952. cmd->duplex = DUPLEX_FULL;
  953. cmd->port = PORT_MII;
  954. cmd->phy_address = 0;
  955. cmd->transceiver = XCVR_INTERNAL;
  956. cmd->autoneg = AUTONEG_DISABLE;
  957. cmd->maxtxpkt = 1;
  958. cmd->maxrxpkt = 1;
  959. return 0;
  960. }
  961. static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  962. {
  963. struct mv643xx_eth_private *mp = netdev_priv(dev);
  964. int err;
  965. /*
  966. * The MAC does not support 1000baseT_Half.
  967. */
  968. cmd->advertising &= ~ADVERTISED_1000baseT_Half;
  969. spin_lock_irq(&mp->lock);
  970. err = mii_ethtool_sset(&mp->mii, cmd);
  971. spin_unlock_irq(&mp->lock);
  972. return err;
  973. }
  974. static int mv643xx_eth_set_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
  975. {
  976. return -EINVAL;
  977. }
  978. static void mv643xx_eth_get_drvinfo(struct net_device *dev,
  979. struct ethtool_drvinfo *drvinfo)
  980. {
  981. strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
  982. strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
  983. strncpy(drvinfo->fw_version, "N/A", 32);
  984. strncpy(drvinfo->bus_info, "platform", 32);
  985. drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
  986. }
  987. static int mv643xx_eth_nway_reset(struct net_device *dev)
  988. {
  989. struct mv643xx_eth_private *mp = netdev_priv(dev);
  990. return mii_nway_restart(&mp->mii);
  991. }
  992. static int mv643xx_eth_nway_reset_phyless(struct net_device *dev)
  993. {
  994. return -EINVAL;
  995. }
  996. static u32 mv643xx_eth_get_link(struct net_device *dev)
  997. {
  998. struct mv643xx_eth_private *mp = netdev_priv(dev);
  999. return mii_link_ok(&mp->mii);
  1000. }
  1001. static u32 mv643xx_eth_get_link_phyless(struct net_device *dev)
  1002. {
  1003. return 1;
  1004. }
  1005. static void mv643xx_eth_get_strings(struct net_device *dev,
  1006. uint32_t stringset, uint8_t *data)
  1007. {
  1008. int i;
  1009. if (stringset == ETH_SS_STATS) {
  1010. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1011. memcpy(data + i * ETH_GSTRING_LEN,
  1012. mv643xx_eth_stats[i].stat_string,
  1013. ETH_GSTRING_LEN);
  1014. }
  1015. }
  1016. }
  1017. static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
  1018. struct ethtool_stats *stats,
  1019. uint64_t *data)
  1020. {
  1021. struct mv643xx_eth_private *mp = dev->priv;
  1022. int i;
  1023. mib_counters_update(mp);
  1024. for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
  1025. const struct mv643xx_eth_stats *stat;
  1026. void *p;
  1027. stat = mv643xx_eth_stats + i;
  1028. if (stat->netdev_off >= 0)
  1029. p = ((void *)mp->dev) + stat->netdev_off;
  1030. else
  1031. p = ((void *)mp) + stat->mp_off;
  1032. data[i] = (stat->sizeof_stat == 8) ?
  1033. *(uint64_t *)p : *(uint32_t *)p;
  1034. }
  1035. }
  1036. static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
  1037. {
  1038. if (sset == ETH_SS_STATS)
  1039. return ARRAY_SIZE(mv643xx_eth_stats);
  1040. return -EOPNOTSUPP;
  1041. }
  1042. static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
  1043. .get_settings = mv643xx_eth_get_settings,
  1044. .set_settings = mv643xx_eth_set_settings,
  1045. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1046. .nway_reset = mv643xx_eth_nway_reset,
  1047. .get_link = mv643xx_eth_get_link,
  1048. .set_sg = ethtool_op_set_sg,
  1049. .get_strings = mv643xx_eth_get_strings,
  1050. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1051. .get_sset_count = mv643xx_eth_get_sset_count,
  1052. };
  1053. static const struct ethtool_ops mv643xx_eth_ethtool_ops_phyless = {
  1054. .get_settings = mv643xx_eth_get_settings_phyless,
  1055. .set_settings = mv643xx_eth_set_settings_phyless,
  1056. .get_drvinfo = mv643xx_eth_get_drvinfo,
  1057. .nway_reset = mv643xx_eth_nway_reset_phyless,
  1058. .get_link = mv643xx_eth_get_link_phyless,
  1059. .set_sg = ethtool_op_set_sg,
  1060. .get_strings = mv643xx_eth_get_strings,
  1061. .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
  1062. .get_sset_count = mv643xx_eth_get_sset_count,
  1063. };
  1064. /* address handling *********************************************************/
  1065. static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
  1066. {
  1067. unsigned int mac_h;
  1068. unsigned int mac_l;
  1069. mac_h = rdl(mp, MAC_ADDR_HIGH(mp->port_num));
  1070. mac_l = rdl(mp, MAC_ADDR_LOW(mp->port_num));
  1071. addr[0] = (mac_h >> 24) & 0xff;
  1072. addr[1] = (mac_h >> 16) & 0xff;
  1073. addr[2] = (mac_h >> 8) & 0xff;
  1074. addr[3] = mac_h & 0xff;
  1075. addr[4] = (mac_l >> 8) & 0xff;
  1076. addr[5] = mac_l & 0xff;
  1077. }
  1078. static void init_mac_tables(struct mv643xx_eth_private *mp)
  1079. {
  1080. int i;
  1081. for (i = 0; i < 0x100; i += 4) {
  1082. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
  1083. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
  1084. }
  1085. for (i = 0; i < 0x10; i += 4)
  1086. wrl(mp, UNICAST_TABLE(mp->port_num) + i, 0);
  1087. }
  1088. static void set_filter_table_entry(struct mv643xx_eth_private *mp,
  1089. int table, unsigned char entry)
  1090. {
  1091. unsigned int table_reg;
  1092. /* Set "accepts frame bit" at specified table entry */
  1093. table_reg = rdl(mp, table + (entry & 0xfc));
  1094. table_reg |= 0x01 << (8 * (entry & 3));
  1095. wrl(mp, table + (entry & 0xfc), table_reg);
  1096. }
  1097. static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
  1098. {
  1099. unsigned int mac_h;
  1100. unsigned int mac_l;
  1101. int table;
  1102. mac_l = (addr[4] << 8) | addr[5];
  1103. mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
  1104. wrl(mp, MAC_ADDR_LOW(mp->port_num), mac_l);
  1105. wrl(mp, MAC_ADDR_HIGH(mp->port_num), mac_h);
  1106. table = UNICAST_TABLE(mp->port_num);
  1107. set_filter_table_entry(mp, table, addr[5] & 0x0f);
  1108. }
  1109. static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
  1110. {
  1111. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1112. /* +2 is for the offset of the HW addr type */
  1113. memcpy(dev->dev_addr, addr + 2, 6);
  1114. init_mac_tables(mp);
  1115. uc_addr_set(mp, dev->dev_addr);
  1116. return 0;
  1117. }
  1118. static int addr_crc(unsigned char *addr)
  1119. {
  1120. int crc = 0;
  1121. int i;
  1122. for (i = 0; i < 6; i++) {
  1123. int j;
  1124. crc = (crc ^ addr[i]) << 8;
  1125. for (j = 7; j >= 0; j--) {
  1126. if (crc & (0x100 << j))
  1127. crc ^= 0x107 << j;
  1128. }
  1129. }
  1130. return crc;
  1131. }
  1132. static void mv643xx_eth_set_rx_mode(struct net_device *dev)
  1133. {
  1134. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1135. u32 port_config;
  1136. struct dev_addr_list *addr;
  1137. int i;
  1138. port_config = rdl(mp, PORT_CONFIG(mp->port_num));
  1139. if (dev->flags & IFF_PROMISC)
  1140. port_config |= UNICAST_PROMISCUOUS_MODE;
  1141. else
  1142. port_config &= ~UNICAST_PROMISCUOUS_MODE;
  1143. wrl(mp, PORT_CONFIG(mp->port_num), port_config);
  1144. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  1145. int port_num = mp->port_num;
  1146. u32 accept = 0x01010101;
  1147. for (i = 0; i < 0x100; i += 4) {
  1148. wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
  1149. wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
  1150. }
  1151. return;
  1152. }
  1153. for (i = 0; i < 0x100; i += 4) {
  1154. wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
  1155. wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
  1156. }
  1157. for (addr = dev->mc_list; addr != NULL; addr = addr->next) {
  1158. u8 *a = addr->da_addr;
  1159. int table;
  1160. if (addr->da_addrlen != 6)
  1161. continue;
  1162. if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
  1163. table = SPECIAL_MCAST_TABLE(mp->port_num);
  1164. set_filter_table_entry(mp, table, a[5]);
  1165. } else {
  1166. int crc = addr_crc(a);
  1167. table = OTHER_MCAST_TABLE(mp->port_num);
  1168. set_filter_table_entry(mp, table, crc);
  1169. }
  1170. }
  1171. }
  1172. /* rx/tx queue initialisation ***********************************************/
  1173. static int rxq_init(struct mv643xx_eth_private *mp, int index)
  1174. {
  1175. struct rx_queue *rxq = mp->rxq + index;
  1176. struct rx_desc *rx_desc;
  1177. int size;
  1178. int i;
  1179. rxq->index = index;
  1180. rxq->rx_ring_size = mp->default_rx_ring_size;
  1181. rxq->rx_desc_count = 0;
  1182. rxq->rx_curr_desc = 0;
  1183. rxq->rx_used_desc = 0;
  1184. size = rxq->rx_ring_size * sizeof(struct rx_desc);
  1185. if (index == mp->rxq_primary && size <= mp->rx_desc_sram_size) {
  1186. rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
  1187. mp->rx_desc_sram_size);
  1188. rxq->rx_desc_dma = mp->rx_desc_sram_addr;
  1189. } else {
  1190. rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
  1191. &rxq->rx_desc_dma,
  1192. GFP_KERNEL);
  1193. }
  1194. if (rxq->rx_desc_area == NULL) {
  1195. dev_printk(KERN_ERR, &mp->dev->dev,
  1196. "can't allocate rx ring (%d bytes)\n", size);
  1197. goto out;
  1198. }
  1199. memset(rxq->rx_desc_area, 0, size);
  1200. rxq->rx_desc_area_size = size;
  1201. rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
  1202. GFP_KERNEL);
  1203. if (rxq->rx_skb == NULL) {
  1204. dev_printk(KERN_ERR, &mp->dev->dev,
  1205. "can't allocate rx skb ring\n");
  1206. goto out_free;
  1207. }
  1208. rx_desc = (struct rx_desc *)rxq->rx_desc_area;
  1209. for (i = 0; i < rxq->rx_ring_size; i++) {
  1210. int nexti = (i + 1) % rxq->rx_ring_size;
  1211. rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
  1212. nexti * sizeof(struct rx_desc);
  1213. }
  1214. init_timer(&rxq->rx_oom);
  1215. rxq->rx_oom.data = (unsigned long)rxq;
  1216. rxq->rx_oom.function = rxq_refill_timer_wrapper;
  1217. return 0;
  1218. out_free:
  1219. if (index == mp->rxq_primary && size <= mp->rx_desc_sram_size)
  1220. iounmap(rxq->rx_desc_area);
  1221. else
  1222. dma_free_coherent(NULL, size,
  1223. rxq->rx_desc_area,
  1224. rxq->rx_desc_dma);
  1225. out:
  1226. return -ENOMEM;
  1227. }
  1228. static void rxq_deinit(struct rx_queue *rxq)
  1229. {
  1230. struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
  1231. int i;
  1232. rxq_disable(rxq);
  1233. del_timer_sync(&rxq->rx_oom);
  1234. for (i = 0; i < rxq->rx_ring_size; i++) {
  1235. if (rxq->rx_skb[i]) {
  1236. dev_kfree_skb(rxq->rx_skb[i]);
  1237. rxq->rx_desc_count--;
  1238. }
  1239. }
  1240. if (rxq->rx_desc_count) {
  1241. dev_printk(KERN_ERR, &mp->dev->dev,
  1242. "error freeing rx ring -- %d skbs stuck\n",
  1243. rxq->rx_desc_count);
  1244. }
  1245. if (rxq->index == mp->rxq_primary &&
  1246. rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
  1247. iounmap(rxq->rx_desc_area);
  1248. else
  1249. dma_free_coherent(NULL, rxq->rx_desc_area_size,
  1250. rxq->rx_desc_area, rxq->rx_desc_dma);
  1251. kfree(rxq->rx_skb);
  1252. }
  1253. static int txq_init(struct mv643xx_eth_private *mp, int index)
  1254. {
  1255. struct tx_queue *txq = mp->txq + index;
  1256. struct tx_desc *tx_desc;
  1257. int size;
  1258. int i;
  1259. txq->index = index;
  1260. txq->tx_ring_size = mp->default_tx_ring_size;
  1261. txq->tx_desc_count = 0;
  1262. txq->tx_curr_desc = 0;
  1263. txq->tx_used_desc = 0;
  1264. size = txq->tx_ring_size * sizeof(struct tx_desc);
  1265. if (index == mp->txq_primary && size <= mp->tx_desc_sram_size) {
  1266. txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
  1267. mp->tx_desc_sram_size);
  1268. txq->tx_desc_dma = mp->tx_desc_sram_addr;
  1269. } else {
  1270. txq->tx_desc_area = dma_alloc_coherent(NULL, size,
  1271. &txq->tx_desc_dma,
  1272. GFP_KERNEL);
  1273. }
  1274. if (txq->tx_desc_area == NULL) {
  1275. dev_printk(KERN_ERR, &mp->dev->dev,
  1276. "can't allocate tx ring (%d bytes)\n", size);
  1277. goto out;
  1278. }
  1279. memset(txq->tx_desc_area, 0, size);
  1280. txq->tx_desc_area_size = size;
  1281. txq->tx_skb = kmalloc(txq->tx_ring_size * sizeof(*txq->tx_skb),
  1282. GFP_KERNEL);
  1283. if (txq->tx_skb == NULL) {
  1284. dev_printk(KERN_ERR, &mp->dev->dev,
  1285. "can't allocate tx skb ring\n");
  1286. goto out_free;
  1287. }
  1288. tx_desc = (struct tx_desc *)txq->tx_desc_area;
  1289. for (i = 0; i < txq->tx_ring_size; i++) {
  1290. struct tx_desc *txd = tx_desc + i;
  1291. int nexti = (i + 1) % txq->tx_ring_size;
  1292. txd->cmd_sts = 0;
  1293. txd->next_desc_ptr = txq->tx_desc_dma +
  1294. nexti * sizeof(struct tx_desc);
  1295. }
  1296. return 0;
  1297. out_free:
  1298. if (index == mp->txq_primary && size <= mp->tx_desc_sram_size)
  1299. iounmap(txq->tx_desc_area);
  1300. else
  1301. dma_free_coherent(NULL, size,
  1302. txq->tx_desc_area,
  1303. txq->tx_desc_dma);
  1304. out:
  1305. return -ENOMEM;
  1306. }
  1307. static void txq_reclaim(struct tx_queue *txq, int force)
  1308. {
  1309. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1310. unsigned long flags;
  1311. spin_lock_irqsave(&mp->lock, flags);
  1312. while (txq->tx_desc_count > 0) {
  1313. int tx_index;
  1314. struct tx_desc *desc;
  1315. u32 cmd_sts;
  1316. struct sk_buff *skb;
  1317. dma_addr_t addr;
  1318. int count;
  1319. tx_index = txq->tx_used_desc;
  1320. desc = &txq->tx_desc_area[tx_index];
  1321. cmd_sts = desc->cmd_sts;
  1322. if (cmd_sts & BUFFER_OWNED_BY_DMA) {
  1323. if (!force)
  1324. break;
  1325. desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
  1326. }
  1327. txq->tx_used_desc = (tx_index + 1) % txq->tx_ring_size;
  1328. txq->tx_desc_count--;
  1329. addr = desc->buf_ptr;
  1330. count = desc->byte_cnt;
  1331. skb = txq->tx_skb[tx_index];
  1332. txq->tx_skb[tx_index] = NULL;
  1333. if (cmd_sts & ERROR_SUMMARY) {
  1334. dev_printk(KERN_INFO, &mp->dev->dev, "tx error\n");
  1335. mp->dev->stats.tx_errors++;
  1336. }
  1337. /*
  1338. * Drop mp->lock while we free the skb.
  1339. */
  1340. spin_unlock_irqrestore(&mp->lock, flags);
  1341. if (cmd_sts & TX_FIRST_DESC)
  1342. dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
  1343. else
  1344. dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
  1345. if (skb)
  1346. dev_kfree_skb_irq(skb);
  1347. spin_lock_irqsave(&mp->lock, flags);
  1348. }
  1349. spin_unlock_irqrestore(&mp->lock, flags);
  1350. }
  1351. static void txq_deinit(struct tx_queue *txq)
  1352. {
  1353. struct mv643xx_eth_private *mp = txq_to_mp(txq);
  1354. txq_disable(txq);
  1355. txq_reclaim(txq, 1);
  1356. BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
  1357. if (txq->index == mp->txq_primary &&
  1358. txq->tx_desc_area_size <= mp->tx_desc_sram_size)
  1359. iounmap(txq->tx_desc_area);
  1360. else
  1361. dma_free_coherent(NULL, txq->tx_desc_area_size,
  1362. txq->tx_desc_area, txq->tx_desc_dma);
  1363. kfree(txq->tx_skb);
  1364. }
  1365. /* netdev ops and related ***************************************************/
  1366. static void update_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
  1367. {
  1368. u32 pscr_o;
  1369. u32 pscr_n;
  1370. pscr_o = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
  1371. /* clear speed, duplex and rx buffer size fields */
  1372. pscr_n = pscr_o & ~(SET_MII_SPEED_TO_100 |
  1373. SET_GMII_SPEED_TO_1000 |
  1374. SET_FULL_DUPLEX_MODE |
  1375. MAX_RX_PACKET_MASK);
  1376. if (speed == SPEED_1000) {
  1377. pscr_n |= SET_GMII_SPEED_TO_1000 | MAX_RX_PACKET_9700BYTE;
  1378. } else {
  1379. if (speed == SPEED_100)
  1380. pscr_n |= SET_MII_SPEED_TO_100;
  1381. pscr_n |= MAX_RX_PACKET_1522BYTE;
  1382. }
  1383. if (duplex == DUPLEX_FULL)
  1384. pscr_n |= SET_FULL_DUPLEX_MODE;
  1385. if (pscr_n != pscr_o) {
  1386. if ((pscr_o & SERIAL_PORT_ENABLE) == 0)
  1387. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
  1388. else {
  1389. int i;
  1390. for (i = 0; i < 8; i++)
  1391. if (mp->txq_mask & (1 << i))
  1392. txq_disable(mp->txq + i);
  1393. pscr_o &= ~SERIAL_PORT_ENABLE;
  1394. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_o);
  1395. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
  1396. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
  1397. for (i = 0; i < 8; i++)
  1398. if (mp->txq_mask & (1 << i))
  1399. txq_enable(mp->txq + i);
  1400. }
  1401. }
  1402. }
  1403. static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
  1404. {
  1405. struct net_device *dev = (struct net_device *)dev_id;
  1406. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1407. u32 int_cause;
  1408. u32 int_cause_ext;
  1409. int_cause = rdl(mp, INT_CAUSE(mp->port_num)) &
  1410. (INT_TX_END | INT_RX | INT_EXT);
  1411. if (int_cause == 0)
  1412. return IRQ_NONE;
  1413. int_cause_ext = 0;
  1414. if (int_cause & INT_EXT) {
  1415. int_cause_ext = rdl(mp, INT_CAUSE_EXT(mp->port_num))
  1416. & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
  1417. wrl(mp, INT_CAUSE_EXT(mp->port_num), ~int_cause_ext);
  1418. }
  1419. if (int_cause_ext & (INT_EXT_PHY | INT_EXT_LINK)) {
  1420. if (mp->phy_addr == -1 || mii_link_ok(&mp->mii)) {
  1421. if (mp->phy_addr != -1) {
  1422. struct ethtool_cmd cmd;
  1423. mii_ethtool_gset(&mp->mii, &cmd);
  1424. update_pscr(mp, cmd.speed, cmd.duplex);
  1425. }
  1426. if (!netif_carrier_ok(dev)) {
  1427. netif_carrier_on(dev);
  1428. netif_wake_queue(dev);
  1429. }
  1430. } else if (netif_carrier_ok(dev)) {
  1431. int i;
  1432. netif_stop_queue(dev);
  1433. netif_carrier_off(dev);
  1434. for (i = 0; i < 8; i++) {
  1435. struct tx_queue *txq = mp->txq + i;
  1436. if (mp->txq_mask & (1 << i)) {
  1437. txq_reclaim(txq, 1);
  1438. txq_reset_hw_ptr(txq);
  1439. }
  1440. }
  1441. }
  1442. }
  1443. /*
  1444. * RxBuffer or RxError set for any of the 8 queues?
  1445. */
  1446. #ifdef MV643XX_ETH_NAPI
  1447. if (int_cause & INT_RX) {
  1448. wrl(mp, INT_MASK(mp->port_num), 0x00000000);
  1449. rdl(mp, INT_MASK(mp->port_num));
  1450. netif_rx_schedule(dev, &mp->napi);
  1451. }
  1452. #else
  1453. if (int_cause & INT_RX) {
  1454. int i;
  1455. for (i = 7; i >= 0; i--)
  1456. if (mp->rxq_mask & (1 << i))
  1457. rxq_process(mp->rxq + i, INT_MAX);
  1458. }
  1459. #endif
  1460. /*
  1461. * TxBuffer or TxError set for any of the 8 queues?
  1462. */
  1463. if (int_cause_ext & INT_EXT_TX) {
  1464. int i;
  1465. for (i = 0; i < 8; i++)
  1466. if (mp->txq_mask & (1 << i))
  1467. txq_reclaim(mp->txq + i, 0);
  1468. /*
  1469. * Enough space again in the primary TX queue for a
  1470. * full packet?
  1471. */
  1472. if (netif_carrier_ok(dev)) {
  1473. spin_lock(&mp->lock);
  1474. __txq_maybe_wake(mp->txq + mp->txq_primary);
  1475. spin_unlock(&mp->lock);
  1476. }
  1477. }
  1478. /*
  1479. * Any TxEnd interrupts?
  1480. */
  1481. if (int_cause & INT_TX_END) {
  1482. int i;
  1483. wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_TX_END));
  1484. spin_lock(&mp->lock);
  1485. for (i = 0; i < 8; i++) {
  1486. struct tx_queue *txq = mp->txq + i;
  1487. u32 hw_desc_ptr;
  1488. u32 expected_ptr;
  1489. if ((int_cause & (INT_TX_END_0 << i)) == 0)
  1490. continue;
  1491. hw_desc_ptr =
  1492. rdl(mp, TXQ_CURRENT_DESC_PTR(mp->port_num, i));
  1493. expected_ptr = (u32)txq->tx_desc_dma +
  1494. txq->tx_curr_desc * sizeof(struct tx_desc);
  1495. if (hw_desc_ptr != expected_ptr)
  1496. txq_enable(txq);
  1497. }
  1498. spin_unlock(&mp->lock);
  1499. }
  1500. return IRQ_HANDLED;
  1501. }
  1502. static void phy_reset(struct mv643xx_eth_private *mp)
  1503. {
  1504. unsigned int data;
  1505. smi_reg_read(mp, mp->phy_addr, 0, &data);
  1506. data |= 0x8000;
  1507. smi_reg_write(mp, mp->phy_addr, 0, data);
  1508. do {
  1509. udelay(1);
  1510. smi_reg_read(mp, mp->phy_addr, 0, &data);
  1511. } while (data & 0x8000);
  1512. }
  1513. static void port_start(struct mv643xx_eth_private *mp)
  1514. {
  1515. u32 pscr;
  1516. int i;
  1517. /*
  1518. * Configure basic link parameters.
  1519. */
  1520. pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
  1521. pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
  1522. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  1523. pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
  1524. DISABLE_AUTO_NEG_SPEED_GMII |
  1525. DISABLE_AUTO_NEG_FOR_DUPLEX |
  1526. DO_NOT_FORCE_LINK_FAIL |
  1527. SERIAL_PORT_CONTROL_RESERVED;
  1528. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  1529. pscr |= SERIAL_PORT_ENABLE;
  1530. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
  1531. wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
  1532. /*
  1533. * Perform PHY reset, if there is a PHY.
  1534. */
  1535. if (mp->phy_addr != -1) {
  1536. struct ethtool_cmd cmd;
  1537. mv643xx_eth_get_settings(mp->dev, &cmd);
  1538. phy_reset(mp);
  1539. mv643xx_eth_set_settings(mp->dev, &cmd);
  1540. }
  1541. /*
  1542. * Configure TX path and queues.
  1543. */
  1544. tx_set_rate(mp, 1000000000, 16777216);
  1545. for (i = 0; i < 8; i++) {
  1546. struct tx_queue *txq = mp->txq + i;
  1547. if ((mp->txq_mask & (1 << i)) == 0)
  1548. continue;
  1549. txq_reset_hw_ptr(txq);
  1550. txq_set_rate(txq, 1000000000, 16777216);
  1551. txq_set_fixed_prio_mode(txq);
  1552. }
  1553. /*
  1554. * Add configured unicast address to address filter table.
  1555. */
  1556. uc_addr_set(mp, mp->dev->dev_addr);
  1557. /*
  1558. * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
  1559. * frames to RX queue #0.
  1560. */
  1561. wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
  1562. /*
  1563. * Treat BPDUs as normal multicasts, and disable partition mode.
  1564. */
  1565. wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
  1566. /*
  1567. * Enable the receive queues.
  1568. */
  1569. for (i = 0; i < 8; i++) {
  1570. struct rx_queue *rxq = mp->rxq + i;
  1571. int off = RXQ_CURRENT_DESC_PTR(mp->port_num, i);
  1572. u32 addr;
  1573. if ((mp->rxq_mask & (1 << i)) == 0)
  1574. continue;
  1575. addr = (u32)rxq->rx_desc_dma;
  1576. addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
  1577. wrl(mp, off, addr);
  1578. rxq_enable(rxq);
  1579. }
  1580. }
  1581. static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
  1582. {
  1583. unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
  1584. u32 val;
  1585. val = rdl(mp, SDMA_CONFIG(mp->port_num));
  1586. if (mp->shared->extended_rx_coal_limit) {
  1587. if (coal > 0xffff)
  1588. coal = 0xffff;
  1589. val &= ~0x023fff80;
  1590. val |= (coal & 0x8000) << 10;
  1591. val |= (coal & 0x7fff) << 7;
  1592. } else {
  1593. if (coal > 0x3fff)
  1594. coal = 0x3fff;
  1595. val &= ~0x003fff00;
  1596. val |= (coal & 0x3fff) << 8;
  1597. }
  1598. wrl(mp, SDMA_CONFIG(mp->port_num), val);
  1599. }
  1600. static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
  1601. {
  1602. unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
  1603. if (coal > 0x3fff)
  1604. coal = 0x3fff;
  1605. wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), (coal & 0x3fff) << 4);
  1606. }
  1607. static int mv643xx_eth_open(struct net_device *dev)
  1608. {
  1609. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1610. int err;
  1611. int i;
  1612. wrl(mp, INT_CAUSE(mp->port_num), 0);
  1613. wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
  1614. rdl(mp, INT_CAUSE_EXT(mp->port_num));
  1615. err = request_irq(dev->irq, mv643xx_eth_irq,
  1616. IRQF_SHARED | IRQF_SAMPLE_RANDOM,
  1617. dev->name, dev);
  1618. if (err) {
  1619. dev_printk(KERN_ERR, &dev->dev, "can't assign irq\n");
  1620. return -EAGAIN;
  1621. }
  1622. init_mac_tables(mp);
  1623. for (i = 0; i < 8; i++) {
  1624. if ((mp->rxq_mask & (1 << i)) == 0)
  1625. continue;
  1626. err = rxq_init(mp, i);
  1627. if (err) {
  1628. while (--i >= 0)
  1629. if (mp->rxq_mask & (1 << i))
  1630. rxq_deinit(mp->rxq + i);
  1631. goto out;
  1632. }
  1633. rxq_refill(mp->rxq + i);
  1634. }
  1635. for (i = 0; i < 8; i++) {
  1636. if ((mp->txq_mask & (1 << i)) == 0)
  1637. continue;
  1638. err = txq_init(mp, i);
  1639. if (err) {
  1640. while (--i >= 0)
  1641. if (mp->txq_mask & (1 << i))
  1642. txq_deinit(mp->txq + i);
  1643. goto out_free;
  1644. }
  1645. }
  1646. #ifdef MV643XX_ETH_NAPI
  1647. napi_enable(&mp->napi);
  1648. #endif
  1649. port_start(mp);
  1650. set_rx_coal(mp, 0);
  1651. set_tx_coal(mp, 0);
  1652. wrl(mp, INT_MASK_EXT(mp->port_num),
  1653. INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
  1654. wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
  1655. return 0;
  1656. out_free:
  1657. for (i = 0; i < 8; i++)
  1658. if (mp->rxq_mask & (1 << i))
  1659. rxq_deinit(mp->rxq + i);
  1660. out:
  1661. free_irq(dev->irq, dev);
  1662. return err;
  1663. }
  1664. static void port_reset(struct mv643xx_eth_private *mp)
  1665. {
  1666. unsigned int data;
  1667. int i;
  1668. for (i = 0; i < 8; i++) {
  1669. if (mp->rxq_mask & (1 << i))
  1670. rxq_disable(mp->rxq + i);
  1671. if (mp->txq_mask & (1 << i))
  1672. txq_disable(mp->txq + i);
  1673. }
  1674. while (!(rdl(mp, PORT_STATUS(mp->port_num)) & TX_FIFO_EMPTY))
  1675. udelay(10);
  1676. /* Reset the Enable bit in the Configuration Register */
  1677. data = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
  1678. data &= ~(SERIAL_PORT_ENABLE |
  1679. DO_NOT_FORCE_LINK_FAIL |
  1680. FORCE_LINK_PASS);
  1681. wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), data);
  1682. }
  1683. static int mv643xx_eth_stop(struct net_device *dev)
  1684. {
  1685. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1686. int i;
  1687. wrl(mp, INT_MASK(mp->port_num), 0x00000000);
  1688. rdl(mp, INT_MASK(mp->port_num));
  1689. #ifdef MV643XX_ETH_NAPI
  1690. napi_disable(&mp->napi);
  1691. #endif
  1692. netif_carrier_off(dev);
  1693. netif_stop_queue(dev);
  1694. free_irq(dev->irq, dev);
  1695. port_reset(mp);
  1696. mib_counters_update(mp);
  1697. for (i = 0; i < 8; i++) {
  1698. if (mp->rxq_mask & (1 << i))
  1699. rxq_deinit(mp->rxq + i);
  1700. if (mp->txq_mask & (1 << i))
  1701. txq_deinit(mp->txq + i);
  1702. }
  1703. return 0;
  1704. }
  1705. static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1706. {
  1707. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1708. if (mp->phy_addr != -1)
  1709. return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
  1710. return -EOPNOTSUPP;
  1711. }
  1712. static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
  1713. {
  1714. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1715. if (new_mtu < 64 || new_mtu > 9500)
  1716. return -EINVAL;
  1717. dev->mtu = new_mtu;
  1718. tx_set_rate(mp, 1000000000, 16777216);
  1719. if (!netif_running(dev))
  1720. return 0;
  1721. /*
  1722. * Stop and then re-open the interface. This will allocate RX
  1723. * skbs of the new MTU.
  1724. * There is a possible danger that the open will not succeed,
  1725. * due to memory being full.
  1726. */
  1727. mv643xx_eth_stop(dev);
  1728. if (mv643xx_eth_open(dev)) {
  1729. dev_printk(KERN_ERR, &dev->dev,
  1730. "fatal error on re-opening device after "
  1731. "MTU change\n");
  1732. }
  1733. return 0;
  1734. }
  1735. static void tx_timeout_task(struct work_struct *ugly)
  1736. {
  1737. struct mv643xx_eth_private *mp;
  1738. mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
  1739. if (netif_running(mp->dev)) {
  1740. netif_stop_queue(mp->dev);
  1741. port_reset(mp);
  1742. port_start(mp);
  1743. __txq_maybe_wake(mp->txq + mp->txq_primary);
  1744. }
  1745. }
  1746. static void mv643xx_eth_tx_timeout(struct net_device *dev)
  1747. {
  1748. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1749. dev_printk(KERN_INFO, &dev->dev, "tx timeout\n");
  1750. schedule_work(&mp->tx_timeout_task);
  1751. }
  1752. #ifdef CONFIG_NET_POLL_CONTROLLER
  1753. static void mv643xx_eth_netpoll(struct net_device *dev)
  1754. {
  1755. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1756. wrl(mp, INT_MASK(mp->port_num), 0x00000000);
  1757. rdl(mp, INT_MASK(mp->port_num));
  1758. mv643xx_eth_irq(dev->irq, dev);
  1759. wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
  1760. }
  1761. #endif
  1762. static int mv643xx_eth_mdio_read(struct net_device *dev, int addr, int reg)
  1763. {
  1764. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1765. int val;
  1766. smi_reg_read(mp, addr, reg, &val);
  1767. return val;
  1768. }
  1769. static void mv643xx_eth_mdio_write(struct net_device *dev, int addr, int reg, int val)
  1770. {
  1771. struct mv643xx_eth_private *mp = netdev_priv(dev);
  1772. smi_reg_write(mp, addr, reg, val);
  1773. }
  1774. /* platform glue ************************************************************/
  1775. static void
  1776. mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
  1777. struct mbus_dram_target_info *dram)
  1778. {
  1779. void __iomem *base = msp->base;
  1780. u32 win_enable;
  1781. u32 win_protect;
  1782. int i;
  1783. for (i = 0; i < 6; i++) {
  1784. writel(0, base + WINDOW_BASE(i));
  1785. writel(0, base + WINDOW_SIZE(i));
  1786. if (i < 4)
  1787. writel(0, base + WINDOW_REMAP_HIGH(i));
  1788. }
  1789. win_enable = 0x3f;
  1790. win_protect = 0;
  1791. for (i = 0; i < dram->num_cs; i++) {
  1792. struct mbus_dram_window *cs = dram->cs + i;
  1793. writel((cs->base & 0xffff0000) |
  1794. (cs->mbus_attr << 8) |
  1795. dram->mbus_dram_target_id, base + WINDOW_BASE(i));
  1796. writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
  1797. win_enable &= ~(1 << i);
  1798. win_protect |= 3 << (2 * i);
  1799. }
  1800. writel(win_enable, base + WINDOW_BAR_ENABLE);
  1801. msp->win_protect = win_protect;
  1802. }
  1803. static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
  1804. {
  1805. /*
  1806. * Check whether we have a 14-bit coal limit field in bits
  1807. * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
  1808. * SDMA config register.
  1809. */
  1810. writel(0x02000000, msp->base + SDMA_CONFIG(0));
  1811. if (readl(msp->base + SDMA_CONFIG(0)) & 0x02000000)
  1812. msp->extended_rx_coal_limit = 1;
  1813. else
  1814. msp->extended_rx_coal_limit = 0;
  1815. /*
  1816. * Check whether the TX rate control registers are in the
  1817. * old or the new place.
  1818. */
  1819. writel(1, msp->base + TX_BW_MTU_MOVED(0));
  1820. if (readl(msp->base + TX_BW_MTU_MOVED(0)) & 1)
  1821. msp->tx_bw_control_moved = 1;
  1822. else
  1823. msp->tx_bw_control_moved = 0;
  1824. }
  1825. static int mv643xx_eth_shared_probe(struct platform_device *pdev)
  1826. {
  1827. static int mv643xx_eth_version_printed = 0;
  1828. struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
  1829. struct mv643xx_eth_shared_private *msp;
  1830. struct resource *res;
  1831. int ret;
  1832. if (!mv643xx_eth_version_printed++)
  1833. printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
  1834. ret = -EINVAL;
  1835. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1836. if (res == NULL)
  1837. goto out;
  1838. ret = -ENOMEM;
  1839. msp = kmalloc(sizeof(*msp), GFP_KERNEL);
  1840. if (msp == NULL)
  1841. goto out;
  1842. memset(msp, 0, sizeof(*msp));
  1843. msp->base = ioremap(res->start, res->end - res->start + 1);
  1844. if (msp->base == NULL)
  1845. goto out_free;
  1846. spin_lock_init(&msp->phy_lock);
  1847. /*
  1848. * (Re-)program MBUS remapping windows if we are asked to.
  1849. */
  1850. if (pd != NULL && pd->dram != NULL)
  1851. mv643xx_eth_conf_mbus_windows(msp, pd->dram);
  1852. /*
  1853. * Detect hardware parameters.
  1854. */
  1855. msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
  1856. infer_hw_params(msp);
  1857. platform_set_drvdata(pdev, msp);
  1858. return 0;
  1859. out_free:
  1860. kfree(msp);
  1861. out:
  1862. return ret;
  1863. }
  1864. static int mv643xx_eth_shared_remove(struct platform_device *pdev)
  1865. {
  1866. struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
  1867. iounmap(msp->base);
  1868. kfree(msp);
  1869. return 0;
  1870. }
  1871. static struct platform_driver mv643xx_eth_shared_driver = {
  1872. .probe = mv643xx_eth_shared_probe,
  1873. .remove = mv643xx_eth_shared_remove,
  1874. .driver = {
  1875. .name = MV643XX_ETH_SHARED_NAME,
  1876. .owner = THIS_MODULE,
  1877. },
  1878. };
  1879. static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
  1880. {
  1881. int addr_shift = 5 * mp->port_num;
  1882. u32 data;
  1883. data = rdl(mp, PHY_ADDR);
  1884. data &= ~(0x1f << addr_shift);
  1885. data |= (phy_addr & 0x1f) << addr_shift;
  1886. wrl(mp, PHY_ADDR, data);
  1887. }
  1888. static int phy_addr_get(struct mv643xx_eth_private *mp)
  1889. {
  1890. unsigned int data;
  1891. data = rdl(mp, PHY_ADDR);
  1892. return (data >> (5 * mp->port_num)) & 0x1f;
  1893. }
  1894. static void set_params(struct mv643xx_eth_private *mp,
  1895. struct mv643xx_eth_platform_data *pd)
  1896. {
  1897. struct net_device *dev = mp->dev;
  1898. if (is_valid_ether_addr(pd->mac_addr))
  1899. memcpy(dev->dev_addr, pd->mac_addr, 6);
  1900. else
  1901. uc_addr_get(mp, dev->dev_addr);
  1902. if (pd->phy_addr == -1) {
  1903. mp->shared_smi = NULL;
  1904. mp->phy_addr = -1;
  1905. } else {
  1906. mp->shared_smi = mp->shared;
  1907. if (pd->shared_smi != NULL)
  1908. mp->shared_smi = platform_get_drvdata(pd->shared_smi);
  1909. if (pd->force_phy_addr || pd->phy_addr) {
  1910. mp->phy_addr = pd->phy_addr & 0x3f;
  1911. phy_addr_set(mp, mp->phy_addr);
  1912. } else {
  1913. mp->phy_addr = phy_addr_get(mp);
  1914. }
  1915. }
  1916. mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
  1917. if (pd->rx_queue_size)
  1918. mp->default_rx_ring_size = pd->rx_queue_size;
  1919. mp->rx_desc_sram_addr = pd->rx_sram_addr;
  1920. mp->rx_desc_sram_size = pd->rx_sram_size;
  1921. if (pd->rx_queue_mask)
  1922. mp->rxq_mask = pd->rx_queue_mask;
  1923. else
  1924. mp->rxq_mask = 0x01;
  1925. mp->rxq_primary = fls(mp->rxq_mask) - 1;
  1926. mp->default_tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
  1927. if (pd->tx_queue_size)
  1928. mp->default_tx_ring_size = pd->tx_queue_size;
  1929. mp->tx_desc_sram_addr = pd->tx_sram_addr;
  1930. mp->tx_desc_sram_size = pd->tx_sram_size;
  1931. if (pd->tx_queue_mask)
  1932. mp->txq_mask = pd->tx_queue_mask;
  1933. else
  1934. mp->txq_mask = 0x01;
  1935. mp->txq_primary = fls(mp->txq_mask) - 1;
  1936. }
  1937. static int phy_detect(struct mv643xx_eth_private *mp)
  1938. {
  1939. unsigned int data;
  1940. unsigned int data2;
  1941. smi_reg_read(mp, mp->phy_addr, 0, &data);
  1942. smi_reg_write(mp, mp->phy_addr, 0, data ^ 0x1000);
  1943. smi_reg_read(mp, mp->phy_addr, 0, &data2);
  1944. if (((data ^ data2) & 0x1000) == 0)
  1945. return -ENODEV;
  1946. smi_reg_write(mp, mp->phy_addr, 0, data);
  1947. return 0;
  1948. }
  1949. static int phy_init(struct mv643xx_eth_private *mp,
  1950. struct mv643xx_eth_platform_data *pd)
  1951. {
  1952. struct ethtool_cmd cmd;
  1953. int err;
  1954. err = phy_detect(mp);
  1955. if (err) {
  1956. dev_printk(KERN_INFO, &mp->dev->dev,
  1957. "no PHY detected at addr %d\n", mp->phy_addr);
  1958. return err;
  1959. }
  1960. phy_reset(mp);
  1961. mp->mii.phy_id = mp->phy_addr;
  1962. mp->mii.phy_id_mask = 0x3f;
  1963. mp->mii.reg_num_mask = 0x1f;
  1964. mp->mii.dev = mp->dev;
  1965. mp->mii.mdio_read = mv643xx_eth_mdio_read;
  1966. mp->mii.mdio_write = mv643xx_eth_mdio_write;
  1967. mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
  1968. memset(&cmd, 0, sizeof(cmd));
  1969. cmd.port = PORT_MII;
  1970. cmd.transceiver = XCVR_INTERNAL;
  1971. cmd.phy_address = mp->phy_addr;
  1972. if (pd->speed == 0) {
  1973. cmd.autoneg = AUTONEG_ENABLE;
  1974. cmd.speed = SPEED_100;
  1975. cmd.advertising = ADVERTISED_10baseT_Half |
  1976. ADVERTISED_10baseT_Full |
  1977. ADVERTISED_100baseT_Half |
  1978. ADVERTISED_100baseT_Full;
  1979. if (mp->mii.supports_gmii)
  1980. cmd.advertising |= ADVERTISED_1000baseT_Full;
  1981. } else {
  1982. cmd.autoneg = AUTONEG_DISABLE;
  1983. cmd.speed = pd->speed;
  1984. cmd.duplex = pd->duplex;
  1985. }
  1986. update_pscr(mp, cmd.speed, cmd.duplex);
  1987. mv643xx_eth_set_settings(mp->dev, &cmd);
  1988. return 0;
  1989. }
  1990. static int mv643xx_eth_probe(struct platform_device *pdev)
  1991. {
  1992. struct mv643xx_eth_platform_data *pd;
  1993. struct mv643xx_eth_private *mp;
  1994. struct net_device *dev;
  1995. struct resource *res;
  1996. DECLARE_MAC_BUF(mac);
  1997. int err;
  1998. pd = pdev->dev.platform_data;
  1999. if (pd == NULL) {
  2000. dev_printk(KERN_ERR, &pdev->dev,
  2001. "no mv643xx_eth_platform_data\n");
  2002. return -ENODEV;
  2003. }
  2004. if (pd->shared == NULL) {
  2005. dev_printk(KERN_ERR, &pdev->dev,
  2006. "no mv643xx_eth_platform_data->shared\n");
  2007. return -ENODEV;
  2008. }
  2009. dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
  2010. if (!dev)
  2011. return -ENOMEM;
  2012. mp = netdev_priv(dev);
  2013. platform_set_drvdata(pdev, mp);
  2014. mp->shared = platform_get_drvdata(pd->shared);
  2015. mp->port_num = pd->port_number;
  2016. mp->dev = dev;
  2017. #ifdef MV643XX_ETH_NAPI
  2018. netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
  2019. #endif
  2020. set_params(mp, pd);
  2021. spin_lock_init(&mp->lock);
  2022. mib_counters_clear(mp);
  2023. INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
  2024. if (mp->phy_addr != -1) {
  2025. err = phy_init(mp, pd);
  2026. if (err)
  2027. goto out;
  2028. SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
  2029. } else {
  2030. SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops_phyless);
  2031. }
  2032. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  2033. BUG_ON(!res);
  2034. dev->irq = res->start;
  2035. dev->hard_start_xmit = mv643xx_eth_xmit;
  2036. dev->open = mv643xx_eth_open;
  2037. dev->stop = mv643xx_eth_stop;
  2038. dev->set_multicast_list = mv643xx_eth_set_rx_mode;
  2039. dev->set_mac_address = mv643xx_eth_set_mac_address;
  2040. dev->do_ioctl = mv643xx_eth_ioctl;
  2041. dev->change_mtu = mv643xx_eth_change_mtu;
  2042. dev->tx_timeout = mv643xx_eth_tx_timeout;
  2043. #ifdef CONFIG_NET_POLL_CONTROLLER
  2044. dev->poll_controller = mv643xx_eth_netpoll;
  2045. #endif
  2046. dev->watchdog_timeo = 2 * HZ;
  2047. dev->base_addr = 0;
  2048. #ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
  2049. /*
  2050. * Zero copy can only work if we use Discovery II memory. Else, we will
  2051. * have to map the buffers to ISA memory which is only 16 MB
  2052. */
  2053. dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
  2054. #endif
  2055. SET_NETDEV_DEV(dev, &pdev->dev);
  2056. if (mp->shared->win_protect)
  2057. wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
  2058. err = register_netdev(dev);
  2059. if (err)
  2060. goto out;
  2061. dev_printk(KERN_NOTICE, &dev->dev, "port %d with MAC address %s\n",
  2062. mp->port_num, print_mac(mac, dev->dev_addr));
  2063. if (dev->features & NETIF_F_SG)
  2064. dev_printk(KERN_NOTICE, &dev->dev, "scatter/gather enabled\n");
  2065. if (dev->features & NETIF_F_IP_CSUM)
  2066. dev_printk(KERN_NOTICE, &dev->dev, "tx checksum offload\n");
  2067. #ifdef MV643XX_ETH_NAPI
  2068. dev_printk(KERN_NOTICE, &dev->dev, "napi enabled\n");
  2069. #endif
  2070. if (mp->tx_desc_sram_size > 0)
  2071. dev_printk(KERN_NOTICE, &dev->dev, "configured with sram\n");
  2072. return 0;
  2073. out:
  2074. free_netdev(dev);
  2075. return err;
  2076. }
  2077. static int mv643xx_eth_remove(struct platform_device *pdev)
  2078. {
  2079. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2080. unregister_netdev(mp->dev);
  2081. flush_scheduled_work();
  2082. free_netdev(mp->dev);
  2083. platform_set_drvdata(pdev, NULL);
  2084. return 0;
  2085. }
  2086. static void mv643xx_eth_shutdown(struct platform_device *pdev)
  2087. {
  2088. struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
  2089. /* Mask all interrupts on ethernet port */
  2090. wrl(mp, INT_MASK(mp->port_num), 0);
  2091. rdl(mp, INT_MASK(mp->port_num));
  2092. if (netif_running(mp->dev))
  2093. port_reset(mp);
  2094. }
  2095. static struct platform_driver mv643xx_eth_driver = {
  2096. .probe = mv643xx_eth_probe,
  2097. .remove = mv643xx_eth_remove,
  2098. .shutdown = mv643xx_eth_shutdown,
  2099. .driver = {
  2100. .name = MV643XX_ETH_NAME,
  2101. .owner = THIS_MODULE,
  2102. },
  2103. };
  2104. static int __init mv643xx_eth_init_module(void)
  2105. {
  2106. int rc;
  2107. rc = platform_driver_register(&mv643xx_eth_shared_driver);
  2108. if (!rc) {
  2109. rc = platform_driver_register(&mv643xx_eth_driver);
  2110. if (rc)
  2111. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2112. }
  2113. return rc;
  2114. }
  2115. module_init(mv643xx_eth_init_module);
  2116. static void __exit mv643xx_eth_cleanup_module(void)
  2117. {
  2118. platform_driver_unregister(&mv643xx_eth_driver);
  2119. platform_driver_unregister(&mv643xx_eth_shared_driver);
  2120. }
  2121. module_exit(mv643xx_eth_cleanup_module);
  2122. MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
  2123. "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
  2124. MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
  2125. MODULE_LICENSE("GPL");
  2126. MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
  2127. MODULE_ALIAS("platform:" MV643XX_ETH_NAME);