mv643xx_eth.c 65 KB

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