lpc_eth.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  1. /*
  2. * drivers/net/ethernet/nxp/lpc_eth.c
  3. *
  4. * Author: Kevin Wells <kevin.wells@nxp.com>
  5. *
  6. * Copyright (C) 2010 NXP Semiconductors
  7. * Copyright (C) 2012 Roland Stigge <stigge@antcom.de>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/errno.h>
  27. #include <linux/ioport.h>
  28. #include <linux/crc32.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/ethtool.h>
  32. #include <linux/mii.h>
  33. #include <linux/clk.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/skbuff.h>
  38. #include <linux/phy.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/of.h>
  41. #include <linux/of_net.h>
  42. #include <linux/types.h>
  43. #include <linux/io.h>
  44. #include <mach/board.h>
  45. #include <mach/platform.h>
  46. #include <mach/hardware.h>
  47. #define MODNAME "lpc-eth"
  48. #define DRV_VERSION "1.00"
  49. #define ENET_MAXF_SIZE 1536
  50. #define ENET_RX_DESC 48
  51. #define ENET_TX_DESC 16
  52. #define NAPI_WEIGHT 16
  53. /*
  54. * Ethernet MAC controller Register offsets
  55. */
  56. #define LPC_ENET_MAC1(x) (x + 0x000)
  57. #define LPC_ENET_MAC2(x) (x + 0x004)
  58. #define LPC_ENET_IPGT(x) (x + 0x008)
  59. #define LPC_ENET_IPGR(x) (x + 0x00C)
  60. #define LPC_ENET_CLRT(x) (x + 0x010)
  61. #define LPC_ENET_MAXF(x) (x + 0x014)
  62. #define LPC_ENET_SUPP(x) (x + 0x018)
  63. #define LPC_ENET_TEST(x) (x + 0x01C)
  64. #define LPC_ENET_MCFG(x) (x + 0x020)
  65. #define LPC_ENET_MCMD(x) (x + 0x024)
  66. #define LPC_ENET_MADR(x) (x + 0x028)
  67. #define LPC_ENET_MWTD(x) (x + 0x02C)
  68. #define LPC_ENET_MRDD(x) (x + 0x030)
  69. #define LPC_ENET_MIND(x) (x + 0x034)
  70. #define LPC_ENET_SA0(x) (x + 0x040)
  71. #define LPC_ENET_SA1(x) (x + 0x044)
  72. #define LPC_ENET_SA2(x) (x + 0x048)
  73. #define LPC_ENET_COMMAND(x) (x + 0x100)
  74. #define LPC_ENET_STATUS(x) (x + 0x104)
  75. #define LPC_ENET_RXDESCRIPTOR(x) (x + 0x108)
  76. #define LPC_ENET_RXSTATUS(x) (x + 0x10C)
  77. #define LPC_ENET_RXDESCRIPTORNUMBER(x) (x + 0x110)
  78. #define LPC_ENET_RXPRODUCEINDEX(x) (x + 0x114)
  79. #define LPC_ENET_RXCONSUMEINDEX(x) (x + 0x118)
  80. #define LPC_ENET_TXDESCRIPTOR(x) (x + 0x11C)
  81. #define LPC_ENET_TXSTATUS(x) (x + 0x120)
  82. #define LPC_ENET_TXDESCRIPTORNUMBER(x) (x + 0x124)
  83. #define LPC_ENET_TXPRODUCEINDEX(x) (x + 0x128)
  84. #define LPC_ENET_TXCONSUMEINDEX(x) (x + 0x12C)
  85. #define LPC_ENET_TSV0(x) (x + 0x158)
  86. #define LPC_ENET_TSV1(x) (x + 0x15C)
  87. #define LPC_ENET_RSV(x) (x + 0x160)
  88. #define LPC_ENET_FLOWCONTROLCOUNTER(x) (x + 0x170)
  89. #define LPC_ENET_FLOWCONTROLSTATUS(x) (x + 0x174)
  90. #define LPC_ENET_RXFILTER_CTRL(x) (x + 0x200)
  91. #define LPC_ENET_RXFILTERWOLSTATUS(x) (x + 0x204)
  92. #define LPC_ENET_RXFILTERWOLCLEAR(x) (x + 0x208)
  93. #define LPC_ENET_HASHFILTERL(x) (x + 0x210)
  94. #define LPC_ENET_HASHFILTERH(x) (x + 0x214)
  95. #define LPC_ENET_INTSTATUS(x) (x + 0xFE0)
  96. #define LPC_ENET_INTENABLE(x) (x + 0xFE4)
  97. #define LPC_ENET_INTCLEAR(x) (x + 0xFE8)
  98. #define LPC_ENET_INTSET(x) (x + 0xFEC)
  99. #define LPC_ENET_POWERDOWN(x) (x + 0xFF4)
  100. /*
  101. * mac1 register definitions
  102. */
  103. #define LPC_MAC1_RECV_ENABLE (1 << 0)
  104. #define LPC_MAC1_PASS_ALL_RX_FRAMES (1 << 1)
  105. #define LPC_MAC1_RX_FLOW_CONTROL (1 << 2)
  106. #define LPC_MAC1_TX_FLOW_CONTROL (1 << 3)
  107. #define LPC_MAC1_LOOPBACK (1 << 4)
  108. #define LPC_MAC1_RESET_TX (1 << 8)
  109. #define LPC_MAC1_RESET_MCS_TX (1 << 9)
  110. #define LPC_MAC1_RESET_RX (1 << 10)
  111. #define LPC_MAC1_RESET_MCS_RX (1 << 11)
  112. #define LPC_MAC1_SIMULATION_RESET (1 << 14)
  113. #define LPC_MAC1_SOFT_RESET (1 << 15)
  114. /*
  115. * mac2 register definitions
  116. */
  117. #define LPC_MAC2_FULL_DUPLEX (1 << 0)
  118. #define LPC_MAC2_FRAME_LENGTH_CHECKING (1 << 1)
  119. #define LPC_MAC2_HUGH_LENGTH_CHECKING (1 << 2)
  120. #define LPC_MAC2_DELAYED_CRC (1 << 3)
  121. #define LPC_MAC2_CRC_ENABLE (1 << 4)
  122. #define LPC_MAC2_PAD_CRC_ENABLE (1 << 5)
  123. #define LPC_MAC2_VLAN_PAD_ENABLE (1 << 6)
  124. #define LPC_MAC2_AUTO_DETECT_PAD_ENABLE (1 << 7)
  125. #define LPC_MAC2_PURE_PREAMBLE_ENFORCEMENT (1 << 8)
  126. #define LPC_MAC2_LONG_PREAMBLE_ENFORCEMENT (1 << 9)
  127. #define LPC_MAC2_NO_BACKOFF (1 << 12)
  128. #define LPC_MAC2_BACK_PRESSURE (1 << 13)
  129. #define LPC_MAC2_EXCESS_DEFER (1 << 14)
  130. /*
  131. * ipgt register definitions
  132. */
  133. #define LPC_IPGT_LOAD(n) ((n) & 0x7F)
  134. /*
  135. * ipgr register definitions
  136. */
  137. #define LPC_IPGR_LOAD_PART2(n) ((n) & 0x7F)
  138. #define LPC_IPGR_LOAD_PART1(n) (((n) & 0x7F) << 8)
  139. /*
  140. * clrt register definitions
  141. */
  142. #define LPC_CLRT_LOAD_RETRY_MAX(n) ((n) & 0xF)
  143. #define LPC_CLRT_LOAD_COLLISION_WINDOW(n) (((n) & 0x3F) << 8)
  144. /*
  145. * maxf register definitions
  146. */
  147. #define LPC_MAXF_LOAD_MAX_FRAME_LEN(n) ((n) & 0xFFFF)
  148. /*
  149. * supp register definitions
  150. */
  151. #define LPC_SUPP_SPEED (1 << 8)
  152. #define LPC_SUPP_RESET_RMII (1 << 11)
  153. /*
  154. * test register definitions
  155. */
  156. #define LPC_TEST_SHORTCUT_PAUSE_QUANTA (1 << 0)
  157. #define LPC_TEST_PAUSE (1 << 1)
  158. #define LPC_TEST_BACKPRESSURE (1 << 2)
  159. /*
  160. * mcfg register definitions
  161. */
  162. #define LPC_MCFG_SCAN_INCREMENT (1 << 0)
  163. #define LPC_MCFG_SUPPRESS_PREAMBLE (1 << 1)
  164. #define LPC_MCFG_CLOCK_SELECT(n) (((n) & 0x7) << 2)
  165. #define LPC_MCFG_CLOCK_HOST_DIV_4 0
  166. #define LPC_MCFG_CLOCK_HOST_DIV_6 2
  167. #define LPC_MCFG_CLOCK_HOST_DIV_8 3
  168. #define LPC_MCFG_CLOCK_HOST_DIV_10 4
  169. #define LPC_MCFG_CLOCK_HOST_DIV_14 5
  170. #define LPC_MCFG_CLOCK_HOST_DIV_20 6
  171. #define LPC_MCFG_CLOCK_HOST_DIV_28 7
  172. #define LPC_MCFG_RESET_MII_MGMT (1 << 15)
  173. /*
  174. * mcmd register definitions
  175. */
  176. #define LPC_MCMD_READ (1 << 0)
  177. #define LPC_MCMD_SCAN (1 << 1)
  178. /*
  179. * madr register definitions
  180. */
  181. #define LPC_MADR_REGISTER_ADDRESS(n) ((n) & 0x1F)
  182. #define LPC_MADR_PHY_0ADDRESS(n) (((n) & 0x1F) << 8)
  183. /*
  184. * mwtd register definitions
  185. */
  186. #define LPC_MWDT_WRITE(n) ((n) & 0xFFFF)
  187. /*
  188. * mrdd register definitions
  189. */
  190. #define LPC_MRDD_READ_MASK 0xFFFF
  191. /*
  192. * mind register definitions
  193. */
  194. #define LPC_MIND_BUSY (1 << 0)
  195. #define LPC_MIND_SCANNING (1 << 1)
  196. #define LPC_MIND_NOT_VALID (1 << 2)
  197. #define LPC_MIND_MII_LINK_FAIL (1 << 3)
  198. /*
  199. * command register definitions
  200. */
  201. #define LPC_COMMAND_RXENABLE (1 << 0)
  202. #define LPC_COMMAND_TXENABLE (1 << 1)
  203. #define LPC_COMMAND_REG_RESET (1 << 3)
  204. #define LPC_COMMAND_TXRESET (1 << 4)
  205. #define LPC_COMMAND_RXRESET (1 << 5)
  206. #define LPC_COMMAND_PASSRUNTFRAME (1 << 6)
  207. #define LPC_COMMAND_PASSRXFILTER (1 << 7)
  208. #define LPC_COMMAND_TXFLOWCONTROL (1 << 8)
  209. #define LPC_COMMAND_RMII (1 << 9)
  210. #define LPC_COMMAND_FULLDUPLEX (1 << 10)
  211. /*
  212. * status register definitions
  213. */
  214. #define LPC_STATUS_RXACTIVE (1 << 0)
  215. #define LPC_STATUS_TXACTIVE (1 << 1)
  216. /*
  217. * tsv0 register definitions
  218. */
  219. #define LPC_TSV0_CRC_ERROR (1 << 0)
  220. #define LPC_TSV0_LENGTH_CHECK_ERROR (1 << 1)
  221. #define LPC_TSV0_LENGTH_OUT_OF_RANGE (1 << 2)
  222. #define LPC_TSV0_DONE (1 << 3)
  223. #define LPC_TSV0_MULTICAST (1 << 4)
  224. #define LPC_TSV0_BROADCAST (1 << 5)
  225. #define LPC_TSV0_PACKET_DEFER (1 << 6)
  226. #define LPC_TSV0_ESCESSIVE_DEFER (1 << 7)
  227. #define LPC_TSV0_ESCESSIVE_COLLISION (1 << 8)
  228. #define LPC_TSV0_LATE_COLLISION (1 << 9)
  229. #define LPC_TSV0_GIANT (1 << 10)
  230. #define LPC_TSV0_UNDERRUN (1 << 11)
  231. #define LPC_TSV0_TOTAL_BYTES(n) (((n) >> 12) & 0xFFFF)
  232. #define LPC_TSV0_CONTROL_FRAME (1 << 28)
  233. #define LPC_TSV0_PAUSE (1 << 29)
  234. #define LPC_TSV0_BACKPRESSURE (1 << 30)
  235. #define LPC_TSV0_VLAN (1 << 31)
  236. /*
  237. * tsv1 register definitions
  238. */
  239. #define LPC_TSV1_TRANSMIT_BYTE_COUNT(n) ((n) & 0xFFFF)
  240. #define LPC_TSV1_COLLISION_COUNT(n) (((n) >> 16) & 0xF)
  241. /*
  242. * rsv register definitions
  243. */
  244. #define LPC_RSV_RECEIVED_BYTE_COUNT(n) ((n) & 0xFFFF)
  245. #define LPC_RSV_RXDV_EVENT_IGNORED (1 << 16)
  246. #define LPC_RSV_RXDV_EVENT_PREVIOUSLY_SEEN (1 << 17)
  247. #define LPC_RSV_CARRIER_EVNT_PREVIOUS_SEEN (1 << 18)
  248. #define LPC_RSV_RECEIVE_CODE_VIOLATION (1 << 19)
  249. #define LPC_RSV_CRC_ERROR (1 << 20)
  250. #define LPC_RSV_LENGTH_CHECK_ERROR (1 << 21)
  251. #define LPC_RSV_LENGTH_OUT_OF_RANGE (1 << 22)
  252. #define LPC_RSV_RECEIVE_OK (1 << 23)
  253. #define LPC_RSV_MULTICAST (1 << 24)
  254. #define LPC_RSV_BROADCAST (1 << 25)
  255. #define LPC_RSV_DRIBBLE_NIBBLE (1 << 26)
  256. #define LPC_RSV_CONTROL_FRAME (1 << 27)
  257. #define LPC_RSV_PAUSE (1 << 28)
  258. #define LPC_RSV_UNSUPPORTED_OPCODE (1 << 29)
  259. #define LPC_RSV_VLAN (1 << 30)
  260. /*
  261. * flowcontrolcounter register definitions
  262. */
  263. #define LPC_FCCR_MIRRORCOUNTER(n) ((n) & 0xFFFF)
  264. #define LPC_FCCR_PAUSETIMER(n) (((n) >> 16) & 0xFFFF)
  265. /*
  266. * flowcontrolstatus register definitions
  267. */
  268. #define LPC_FCCR_MIRRORCOUNTERCURRENT(n) ((n) & 0xFFFF)
  269. /*
  270. * rxfliterctrl, rxfilterwolstatus, and rxfilterwolclear shared
  271. * register definitions
  272. */
  273. #define LPC_RXFLTRW_ACCEPTUNICAST (1 << 0)
  274. #define LPC_RXFLTRW_ACCEPTUBROADCAST (1 << 1)
  275. #define LPC_RXFLTRW_ACCEPTUMULTICAST (1 << 2)
  276. #define LPC_RXFLTRW_ACCEPTUNICASTHASH (1 << 3)
  277. #define LPC_RXFLTRW_ACCEPTUMULTICASTHASH (1 << 4)
  278. #define LPC_RXFLTRW_ACCEPTPERFECT (1 << 5)
  279. /*
  280. * rxfliterctrl register definitions
  281. */
  282. #define LPC_RXFLTRWSTS_MAGICPACKETENWOL (1 << 12)
  283. #define LPC_RXFLTRWSTS_RXFILTERENWOL (1 << 13)
  284. /*
  285. * rxfilterwolstatus/rxfilterwolclear register definitions
  286. */
  287. #define LPC_RXFLTRWSTS_RXFILTERWOL (1 << 7)
  288. #define LPC_RXFLTRWSTS_MAGICPACKETWOL (1 << 8)
  289. /*
  290. * intstatus, intenable, intclear, and Intset shared register
  291. * definitions
  292. */
  293. #define LPC_MACINT_RXOVERRUNINTEN (1 << 0)
  294. #define LPC_MACINT_RXERRORONINT (1 << 1)
  295. #define LPC_MACINT_RXFINISHEDINTEN (1 << 2)
  296. #define LPC_MACINT_RXDONEINTEN (1 << 3)
  297. #define LPC_MACINT_TXUNDERRUNINTEN (1 << 4)
  298. #define LPC_MACINT_TXERRORINTEN (1 << 5)
  299. #define LPC_MACINT_TXFINISHEDINTEN (1 << 6)
  300. #define LPC_MACINT_TXDONEINTEN (1 << 7)
  301. #define LPC_MACINT_SOFTINTEN (1 << 12)
  302. #define LPC_MACINT_WAKEUPINTEN (1 << 13)
  303. /*
  304. * powerdown register definitions
  305. */
  306. #define LPC_POWERDOWN_MACAHB (1 << 31)
  307. static phy_interface_t lpc_phy_interface_mode(struct device *dev)
  308. {
  309. if (dev && dev->of_node) {
  310. const char *mode = of_get_property(dev->of_node,
  311. "phy-mode", NULL);
  312. if (mode && !strcmp(mode, "mii"))
  313. return PHY_INTERFACE_MODE_MII;
  314. }
  315. return PHY_INTERFACE_MODE_RMII;
  316. }
  317. static bool use_iram_for_net(struct device *dev)
  318. {
  319. if (dev && dev->of_node)
  320. return of_property_read_bool(dev->of_node, "use-iram");
  321. return false;
  322. }
  323. /* Receive Status information word */
  324. #define RXSTATUS_SIZE 0x000007FF
  325. #define RXSTATUS_CONTROL (1 << 18)
  326. #define RXSTATUS_VLAN (1 << 19)
  327. #define RXSTATUS_FILTER (1 << 20)
  328. #define RXSTATUS_MULTICAST (1 << 21)
  329. #define RXSTATUS_BROADCAST (1 << 22)
  330. #define RXSTATUS_CRC (1 << 23)
  331. #define RXSTATUS_SYMBOL (1 << 24)
  332. #define RXSTATUS_LENGTH (1 << 25)
  333. #define RXSTATUS_RANGE (1 << 26)
  334. #define RXSTATUS_ALIGN (1 << 27)
  335. #define RXSTATUS_OVERRUN (1 << 28)
  336. #define RXSTATUS_NODESC (1 << 29)
  337. #define RXSTATUS_LAST (1 << 30)
  338. #define RXSTATUS_ERROR (1 << 31)
  339. #define RXSTATUS_STATUS_ERROR \
  340. (RXSTATUS_NODESC | RXSTATUS_OVERRUN | RXSTATUS_ALIGN | \
  341. RXSTATUS_RANGE | RXSTATUS_LENGTH | RXSTATUS_SYMBOL | RXSTATUS_CRC)
  342. /* Receive Descriptor control word */
  343. #define RXDESC_CONTROL_SIZE 0x000007FF
  344. #define RXDESC_CONTROL_INT (1 << 31)
  345. /* Transmit Status information word */
  346. #define TXSTATUS_COLLISIONS_GET(x) (((x) >> 21) & 0xF)
  347. #define TXSTATUS_DEFER (1 << 25)
  348. #define TXSTATUS_EXCESSDEFER (1 << 26)
  349. #define TXSTATUS_EXCESSCOLL (1 << 27)
  350. #define TXSTATUS_LATECOLL (1 << 28)
  351. #define TXSTATUS_UNDERRUN (1 << 29)
  352. #define TXSTATUS_NODESC (1 << 30)
  353. #define TXSTATUS_ERROR (1 << 31)
  354. /* Transmit Descriptor control word */
  355. #define TXDESC_CONTROL_SIZE 0x000007FF
  356. #define TXDESC_CONTROL_OVERRIDE (1 << 26)
  357. #define TXDESC_CONTROL_HUGE (1 << 27)
  358. #define TXDESC_CONTROL_PAD (1 << 28)
  359. #define TXDESC_CONTROL_CRC (1 << 29)
  360. #define TXDESC_CONTROL_LAST (1 << 30)
  361. #define TXDESC_CONTROL_INT (1 << 31)
  362. /*
  363. * Structure of a TX/RX descriptors and RX status
  364. */
  365. struct txrx_desc_t {
  366. __le32 packet;
  367. __le32 control;
  368. };
  369. struct rx_status_t {
  370. __le32 statusinfo;
  371. __le32 statushashcrc;
  372. };
  373. /*
  374. * Device driver data structure
  375. */
  376. struct netdata_local {
  377. struct platform_device *pdev;
  378. struct net_device *ndev;
  379. spinlock_t lock;
  380. void __iomem *net_base;
  381. u32 msg_enable;
  382. unsigned int skblen[ENET_TX_DESC];
  383. unsigned int last_tx_idx;
  384. unsigned int num_used_tx_buffs;
  385. struct mii_bus *mii_bus;
  386. struct clk *clk;
  387. dma_addr_t dma_buff_base_p;
  388. void *dma_buff_base_v;
  389. size_t dma_buff_size;
  390. struct txrx_desc_t *tx_desc_v;
  391. u32 *tx_stat_v;
  392. void *tx_buff_v;
  393. struct txrx_desc_t *rx_desc_v;
  394. struct rx_status_t *rx_stat_v;
  395. void *rx_buff_v;
  396. int link;
  397. int speed;
  398. int duplex;
  399. struct napi_struct napi;
  400. };
  401. /*
  402. * MAC support functions
  403. */
  404. static void __lpc_set_mac(struct netdata_local *pldat, u8 *mac)
  405. {
  406. u32 tmp;
  407. /* Set station address */
  408. tmp = mac[0] | ((u32)mac[1] << 8);
  409. writel(tmp, LPC_ENET_SA2(pldat->net_base));
  410. tmp = mac[2] | ((u32)mac[3] << 8);
  411. writel(tmp, LPC_ENET_SA1(pldat->net_base));
  412. tmp = mac[4] | ((u32)mac[5] << 8);
  413. writel(tmp, LPC_ENET_SA0(pldat->net_base));
  414. netdev_dbg(pldat->ndev, "Ethernet MAC address %pM\n", mac);
  415. }
  416. static void __lpc_get_mac(struct netdata_local *pldat, u8 *mac)
  417. {
  418. u32 tmp;
  419. /* Get station address */
  420. tmp = readl(LPC_ENET_SA2(pldat->net_base));
  421. mac[0] = tmp & 0xFF;
  422. mac[1] = tmp >> 8;
  423. tmp = readl(LPC_ENET_SA1(pldat->net_base));
  424. mac[2] = tmp & 0xFF;
  425. mac[3] = tmp >> 8;
  426. tmp = readl(LPC_ENET_SA0(pldat->net_base));
  427. mac[4] = tmp & 0xFF;
  428. mac[5] = tmp >> 8;
  429. }
  430. static void __lpc_params_setup(struct netdata_local *pldat)
  431. {
  432. u32 tmp;
  433. if (pldat->duplex == DUPLEX_FULL) {
  434. tmp = readl(LPC_ENET_MAC2(pldat->net_base));
  435. tmp |= LPC_MAC2_FULL_DUPLEX;
  436. writel(tmp, LPC_ENET_MAC2(pldat->net_base));
  437. tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
  438. tmp |= LPC_COMMAND_FULLDUPLEX;
  439. writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
  440. writel(LPC_IPGT_LOAD(0x15), LPC_ENET_IPGT(pldat->net_base));
  441. } else {
  442. tmp = readl(LPC_ENET_MAC2(pldat->net_base));
  443. tmp &= ~LPC_MAC2_FULL_DUPLEX;
  444. writel(tmp, LPC_ENET_MAC2(pldat->net_base));
  445. tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
  446. tmp &= ~LPC_COMMAND_FULLDUPLEX;
  447. writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
  448. writel(LPC_IPGT_LOAD(0x12), LPC_ENET_IPGT(pldat->net_base));
  449. }
  450. if (pldat->speed == SPEED_100)
  451. writel(LPC_SUPP_SPEED, LPC_ENET_SUPP(pldat->net_base));
  452. else
  453. writel(0, LPC_ENET_SUPP(pldat->net_base));
  454. }
  455. static void __lpc_eth_reset(struct netdata_local *pldat)
  456. {
  457. /* Reset all MAC logic */
  458. writel((LPC_MAC1_RESET_TX | LPC_MAC1_RESET_MCS_TX | LPC_MAC1_RESET_RX |
  459. LPC_MAC1_RESET_MCS_RX | LPC_MAC1_SIMULATION_RESET |
  460. LPC_MAC1_SOFT_RESET), LPC_ENET_MAC1(pldat->net_base));
  461. writel((LPC_COMMAND_REG_RESET | LPC_COMMAND_TXRESET |
  462. LPC_COMMAND_RXRESET), LPC_ENET_COMMAND(pldat->net_base));
  463. }
  464. static int __lpc_mii_mngt_reset(struct netdata_local *pldat)
  465. {
  466. /* Reset MII management hardware */
  467. writel(LPC_MCFG_RESET_MII_MGMT, LPC_ENET_MCFG(pldat->net_base));
  468. /* Setup MII clock to slowest rate with a /28 divider */
  469. writel(LPC_MCFG_CLOCK_SELECT(LPC_MCFG_CLOCK_HOST_DIV_28),
  470. LPC_ENET_MCFG(pldat->net_base));
  471. return 0;
  472. }
  473. static inline phys_addr_t __va_to_pa(void *addr, struct netdata_local *pldat)
  474. {
  475. phys_addr_t phaddr;
  476. phaddr = addr - pldat->dma_buff_base_v;
  477. phaddr += pldat->dma_buff_base_p;
  478. return phaddr;
  479. }
  480. static void lpc_eth_enable_int(void __iomem *regbase)
  481. {
  482. writel((LPC_MACINT_RXDONEINTEN | LPC_MACINT_TXDONEINTEN),
  483. LPC_ENET_INTENABLE(regbase));
  484. }
  485. static void lpc_eth_disable_int(void __iomem *regbase)
  486. {
  487. writel(0, LPC_ENET_INTENABLE(regbase));
  488. }
  489. /* Setup TX/RX descriptors */
  490. static void __lpc_txrx_desc_setup(struct netdata_local *pldat)
  491. {
  492. u32 *ptxstat;
  493. void *tbuff;
  494. int i;
  495. struct txrx_desc_t *ptxrxdesc;
  496. struct rx_status_t *prxstat;
  497. tbuff = PTR_ALIGN(pldat->dma_buff_base_v, 16);
  498. /* Setup TX descriptors, status, and buffers */
  499. pldat->tx_desc_v = tbuff;
  500. tbuff += sizeof(struct txrx_desc_t) * ENET_TX_DESC;
  501. pldat->tx_stat_v = tbuff;
  502. tbuff += sizeof(u32) * ENET_TX_DESC;
  503. tbuff = PTR_ALIGN(tbuff, 16);
  504. pldat->tx_buff_v = tbuff;
  505. tbuff += ENET_MAXF_SIZE * ENET_TX_DESC;
  506. /* Setup RX descriptors, status, and buffers */
  507. pldat->rx_desc_v = tbuff;
  508. tbuff += sizeof(struct txrx_desc_t) * ENET_RX_DESC;
  509. tbuff = PTR_ALIGN(tbuff, 16);
  510. pldat->rx_stat_v = tbuff;
  511. tbuff += sizeof(struct rx_status_t) * ENET_RX_DESC;
  512. tbuff = PTR_ALIGN(tbuff, 16);
  513. pldat->rx_buff_v = tbuff;
  514. tbuff += ENET_MAXF_SIZE * ENET_RX_DESC;
  515. /* Map the TX descriptors to the TX buffers in hardware */
  516. for (i = 0; i < ENET_TX_DESC; i++) {
  517. ptxstat = &pldat->tx_stat_v[i];
  518. ptxrxdesc = &pldat->tx_desc_v[i];
  519. ptxrxdesc->packet = __va_to_pa(
  520. pldat->tx_buff_v + i * ENET_MAXF_SIZE, pldat);
  521. ptxrxdesc->control = 0;
  522. *ptxstat = 0;
  523. }
  524. /* Map the RX descriptors to the RX buffers in hardware */
  525. for (i = 0; i < ENET_RX_DESC; i++) {
  526. prxstat = &pldat->rx_stat_v[i];
  527. ptxrxdesc = &pldat->rx_desc_v[i];
  528. ptxrxdesc->packet = __va_to_pa(
  529. pldat->rx_buff_v + i * ENET_MAXF_SIZE, pldat);
  530. ptxrxdesc->control = RXDESC_CONTROL_INT | (ENET_MAXF_SIZE - 1);
  531. prxstat->statusinfo = 0;
  532. prxstat->statushashcrc = 0;
  533. }
  534. /* Setup base addresses in hardware to point to buffers and
  535. * descriptors
  536. */
  537. writel((ENET_TX_DESC - 1),
  538. LPC_ENET_TXDESCRIPTORNUMBER(pldat->net_base));
  539. writel(__va_to_pa(pldat->tx_desc_v, pldat),
  540. LPC_ENET_TXDESCRIPTOR(pldat->net_base));
  541. writel(__va_to_pa(pldat->tx_stat_v, pldat),
  542. LPC_ENET_TXSTATUS(pldat->net_base));
  543. writel((ENET_RX_DESC - 1),
  544. LPC_ENET_RXDESCRIPTORNUMBER(pldat->net_base));
  545. writel(__va_to_pa(pldat->rx_desc_v, pldat),
  546. LPC_ENET_RXDESCRIPTOR(pldat->net_base));
  547. writel(__va_to_pa(pldat->rx_stat_v, pldat),
  548. LPC_ENET_RXSTATUS(pldat->net_base));
  549. }
  550. static void __lpc_eth_init(struct netdata_local *pldat)
  551. {
  552. u32 tmp;
  553. /* Disable controller and reset */
  554. tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
  555. tmp &= ~LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE;
  556. writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
  557. tmp = readl(LPC_ENET_MAC1(pldat->net_base));
  558. tmp &= ~LPC_MAC1_RECV_ENABLE;
  559. writel(tmp, LPC_ENET_MAC1(pldat->net_base));
  560. /* Initial MAC setup */
  561. writel(LPC_MAC1_PASS_ALL_RX_FRAMES, LPC_ENET_MAC1(pldat->net_base));
  562. writel((LPC_MAC2_PAD_CRC_ENABLE | LPC_MAC2_CRC_ENABLE),
  563. LPC_ENET_MAC2(pldat->net_base));
  564. writel(ENET_MAXF_SIZE, LPC_ENET_MAXF(pldat->net_base));
  565. /* Collision window, gap */
  566. writel((LPC_CLRT_LOAD_RETRY_MAX(0xF) |
  567. LPC_CLRT_LOAD_COLLISION_WINDOW(0x37)),
  568. LPC_ENET_CLRT(pldat->net_base));
  569. writel(LPC_IPGR_LOAD_PART2(0x12), LPC_ENET_IPGR(pldat->net_base));
  570. if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
  571. writel(LPC_COMMAND_PASSRUNTFRAME,
  572. LPC_ENET_COMMAND(pldat->net_base));
  573. else {
  574. writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII),
  575. LPC_ENET_COMMAND(pldat->net_base));
  576. writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base));
  577. }
  578. __lpc_params_setup(pldat);
  579. /* Setup TX and RX descriptors */
  580. __lpc_txrx_desc_setup(pldat);
  581. /* Setup packet filtering */
  582. writel((LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT),
  583. LPC_ENET_RXFILTER_CTRL(pldat->net_base));
  584. /* Get the next TX buffer output index */
  585. pldat->num_used_tx_buffs = 0;
  586. pldat->last_tx_idx =
  587. readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
  588. /* Clear and enable interrupts */
  589. writel(0xFFFF, LPC_ENET_INTCLEAR(pldat->net_base));
  590. smp_wmb();
  591. lpc_eth_enable_int(pldat->net_base);
  592. /* Enable controller */
  593. tmp = readl(LPC_ENET_COMMAND(pldat->net_base));
  594. tmp |= LPC_COMMAND_RXENABLE | LPC_COMMAND_TXENABLE;
  595. writel(tmp, LPC_ENET_COMMAND(pldat->net_base));
  596. tmp = readl(LPC_ENET_MAC1(pldat->net_base));
  597. tmp |= LPC_MAC1_RECV_ENABLE;
  598. writel(tmp, LPC_ENET_MAC1(pldat->net_base));
  599. }
  600. static void __lpc_eth_shutdown(struct netdata_local *pldat)
  601. {
  602. /* Reset ethernet and power down PHY */
  603. __lpc_eth_reset(pldat);
  604. writel(0, LPC_ENET_MAC1(pldat->net_base));
  605. writel(0, LPC_ENET_MAC2(pldat->net_base));
  606. }
  607. /*
  608. * MAC<--->PHY support functions
  609. */
  610. static int lpc_mdio_read(struct mii_bus *bus, int phy_id, int phyreg)
  611. {
  612. struct netdata_local *pldat = bus->priv;
  613. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  614. int lps;
  615. writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base));
  616. writel(LPC_MCMD_READ, LPC_ENET_MCMD(pldat->net_base));
  617. /* Wait for unbusy status */
  618. while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) {
  619. if (time_after(jiffies, timeout))
  620. return -EIO;
  621. cpu_relax();
  622. }
  623. lps = readl(LPC_ENET_MRDD(pldat->net_base));
  624. writel(0, LPC_ENET_MCMD(pldat->net_base));
  625. return lps;
  626. }
  627. static int lpc_mdio_write(struct mii_bus *bus, int phy_id, int phyreg,
  628. u16 phydata)
  629. {
  630. struct netdata_local *pldat = bus->priv;
  631. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  632. writel(((phy_id << 8) | phyreg), LPC_ENET_MADR(pldat->net_base));
  633. writel(phydata, LPC_ENET_MWTD(pldat->net_base));
  634. /* Wait for completion */
  635. while (readl(LPC_ENET_MIND(pldat->net_base)) & LPC_MIND_BUSY) {
  636. if (time_after(jiffies, timeout))
  637. return -EIO;
  638. cpu_relax();
  639. }
  640. return 0;
  641. }
  642. static int lpc_mdio_reset(struct mii_bus *bus)
  643. {
  644. return __lpc_mii_mngt_reset((struct netdata_local *)bus->priv);
  645. }
  646. static void lpc_handle_link_change(struct net_device *ndev)
  647. {
  648. struct netdata_local *pldat = netdev_priv(ndev);
  649. struct phy_device *phydev = ndev->phydev;
  650. unsigned long flags;
  651. bool status_change = false;
  652. spin_lock_irqsave(&pldat->lock, flags);
  653. if (phydev->link) {
  654. if ((pldat->speed != phydev->speed) ||
  655. (pldat->duplex != phydev->duplex)) {
  656. pldat->speed = phydev->speed;
  657. pldat->duplex = phydev->duplex;
  658. status_change = true;
  659. }
  660. }
  661. if (phydev->link != pldat->link) {
  662. if (!phydev->link) {
  663. pldat->speed = 0;
  664. pldat->duplex = -1;
  665. }
  666. pldat->link = phydev->link;
  667. status_change = true;
  668. }
  669. spin_unlock_irqrestore(&pldat->lock, flags);
  670. if (status_change)
  671. __lpc_params_setup(pldat);
  672. }
  673. static int lpc_mii_probe(struct net_device *ndev)
  674. {
  675. struct netdata_local *pldat = netdev_priv(ndev);
  676. struct phy_device *phydev = phy_find_first(pldat->mii_bus);
  677. if (!phydev) {
  678. netdev_err(ndev, "no PHY found\n");
  679. return -ENODEV;
  680. }
  681. /* Attach to the PHY */
  682. if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
  683. netdev_info(ndev, "using MII interface\n");
  684. else
  685. netdev_info(ndev, "using RMII interface\n");
  686. phydev = phy_connect(ndev, phydev_name(phydev),
  687. &lpc_handle_link_change,
  688. lpc_phy_interface_mode(&pldat->pdev->dev));
  689. if (IS_ERR(phydev)) {
  690. netdev_err(ndev, "Could not attach to PHY\n");
  691. return PTR_ERR(phydev);
  692. }
  693. /* mask with MAC supported features */
  694. phydev->supported &= PHY_BASIC_FEATURES;
  695. phydev->advertising = phydev->supported;
  696. pldat->link = 0;
  697. pldat->speed = 0;
  698. pldat->duplex = -1;
  699. phy_attached_info(phydev);
  700. return 0;
  701. }
  702. static int lpc_mii_init(struct netdata_local *pldat)
  703. {
  704. int err = -ENXIO;
  705. pldat->mii_bus = mdiobus_alloc();
  706. if (!pldat->mii_bus) {
  707. err = -ENOMEM;
  708. goto err_out;
  709. }
  710. /* Setup MII mode */
  711. if (lpc_phy_interface_mode(&pldat->pdev->dev) == PHY_INTERFACE_MODE_MII)
  712. writel(LPC_COMMAND_PASSRUNTFRAME,
  713. LPC_ENET_COMMAND(pldat->net_base));
  714. else {
  715. writel((LPC_COMMAND_PASSRUNTFRAME | LPC_COMMAND_RMII),
  716. LPC_ENET_COMMAND(pldat->net_base));
  717. writel(LPC_SUPP_RESET_RMII, LPC_ENET_SUPP(pldat->net_base));
  718. }
  719. pldat->mii_bus->name = "lpc_mii_bus";
  720. pldat->mii_bus->read = &lpc_mdio_read;
  721. pldat->mii_bus->write = &lpc_mdio_write;
  722. pldat->mii_bus->reset = &lpc_mdio_reset;
  723. snprintf(pldat->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  724. pldat->pdev->name, pldat->pdev->id);
  725. pldat->mii_bus->priv = pldat;
  726. pldat->mii_bus->parent = &pldat->pdev->dev;
  727. platform_set_drvdata(pldat->pdev, pldat->mii_bus);
  728. if (mdiobus_register(pldat->mii_bus))
  729. goto err_out_unregister_bus;
  730. if (lpc_mii_probe(pldat->ndev) != 0)
  731. goto err_out_unregister_bus;
  732. return 0;
  733. err_out_unregister_bus:
  734. mdiobus_unregister(pldat->mii_bus);
  735. mdiobus_free(pldat->mii_bus);
  736. err_out:
  737. return err;
  738. }
  739. static void __lpc_handle_xmit(struct net_device *ndev)
  740. {
  741. struct netdata_local *pldat = netdev_priv(ndev);
  742. u32 txcidx, *ptxstat, txstat;
  743. txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
  744. while (pldat->last_tx_idx != txcidx) {
  745. unsigned int skblen = pldat->skblen[pldat->last_tx_idx];
  746. /* A buffer is available, get buffer status */
  747. ptxstat = &pldat->tx_stat_v[pldat->last_tx_idx];
  748. txstat = *ptxstat;
  749. /* Next buffer and decrement used buffer counter */
  750. pldat->num_used_tx_buffs--;
  751. pldat->last_tx_idx++;
  752. if (pldat->last_tx_idx >= ENET_TX_DESC)
  753. pldat->last_tx_idx = 0;
  754. /* Update collision counter */
  755. ndev->stats.collisions += TXSTATUS_COLLISIONS_GET(txstat);
  756. /* Any errors occurred? */
  757. if (txstat & TXSTATUS_ERROR) {
  758. if (txstat & TXSTATUS_UNDERRUN) {
  759. /* FIFO underrun */
  760. ndev->stats.tx_fifo_errors++;
  761. }
  762. if (txstat & TXSTATUS_LATECOLL) {
  763. /* Late collision */
  764. ndev->stats.tx_aborted_errors++;
  765. }
  766. if (txstat & TXSTATUS_EXCESSCOLL) {
  767. /* Excessive collision */
  768. ndev->stats.tx_aborted_errors++;
  769. }
  770. if (txstat & TXSTATUS_EXCESSDEFER) {
  771. /* Defer limit */
  772. ndev->stats.tx_aborted_errors++;
  773. }
  774. ndev->stats.tx_errors++;
  775. } else {
  776. /* Update stats */
  777. ndev->stats.tx_packets++;
  778. ndev->stats.tx_bytes += skblen;
  779. }
  780. txcidx = readl(LPC_ENET_TXCONSUMEINDEX(pldat->net_base));
  781. }
  782. if (pldat->num_used_tx_buffs <= ENET_TX_DESC/2) {
  783. if (netif_queue_stopped(ndev))
  784. netif_wake_queue(ndev);
  785. }
  786. }
  787. static int __lpc_handle_recv(struct net_device *ndev, int budget)
  788. {
  789. struct netdata_local *pldat = netdev_priv(ndev);
  790. struct sk_buff *skb;
  791. u32 rxconsidx, len, ethst;
  792. struct rx_status_t *prxstat;
  793. u8 *prdbuf;
  794. int rx_done = 0;
  795. /* Get the current RX buffer indexes */
  796. rxconsidx = readl(LPC_ENET_RXCONSUMEINDEX(pldat->net_base));
  797. while (rx_done < budget && rxconsidx !=
  798. readl(LPC_ENET_RXPRODUCEINDEX(pldat->net_base))) {
  799. /* Get pointer to receive status */
  800. prxstat = &pldat->rx_stat_v[rxconsidx];
  801. len = (prxstat->statusinfo & RXSTATUS_SIZE) + 1;
  802. /* Status error? */
  803. ethst = prxstat->statusinfo;
  804. if ((ethst & (RXSTATUS_ERROR | RXSTATUS_STATUS_ERROR)) ==
  805. (RXSTATUS_ERROR | RXSTATUS_RANGE))
  806. ethst &= ~RXSTATUS_ERROR;
  807. if (ethst & RXSTATUS_ERROR) {
  808. int si = prxstat->statusinfo;
  809. /* Check statuses */
  810. if (si & RXSTATUS_OVERRUN) {
  811. /* Overrun error */
  812. ndev->stats.rx_fifo_errors++;
  813. } else if (si & RXSTATUS_CRC) {
  814. /* CRC error */
  815. ndev->stats.rx_crc_errors++;
  816. } else if (si & RXSTATUS_LENGTH) {
  817. /* Length error */
  818. ndev->stats.rx_length_errors++;
  819. } else if (si & RXSTATUS_ERROR) {
  820. /* Other error */
  821. ndev->stats.rx_length_errors++;
  822. }
  823. ndev->stats.rx_errors++;
  824. } else {
  825. /* Packet is good */
  826. skb = dev_alloc_skb(len);
  827. if (!skb) {
  828. ndev->stats.rx_dropped++;
  829. } else {
  830. prdbuf = skb_put(skb, len);
  831. /* Copy packet from buffer */
  832. memcpy(prdbuf, pldat->rx_buff_v +
  833. rxconsidx * ENET_MAXF_SIZE, len);
  834. /* Pass to upper layer */
  835. skb->protocol = eth_type_trans(skb, ndev);
  836. netif_receive_skb(skb);
  837. ndev->stats.rx_packets++;
  838. ndev->stats.rx_bytes += len;
  839. }
  840. }
  841. /* Increment consume index */
  842. rxconsidx = rxconsidx + 1;
  843. if (rxconsidx >= ENET_RX_DESC)
  844. rxconsidx = 0;
  845. writel(rxconsidx,
  846. LPC_ENET_RXCONSUMEINDEX(pldat->net_base));
  847. rx_done++;
  848. }
  849. return rx_done;
  850. }
  851. static int lpc_eth_poll(struct napi_struct *napi, int budget)
  852. {
  853. struct netdata_local *pldat = container_of(napi,
  854. struct netdata_local, napi);
  855. struct net_device *ndev = pldat->ndev;
  856. int rx_done = 0;
  857. struct netdev_queue *txq = netdev_get_tx_queue(ndev, 0);
  858. __netif_tx_lock(txq, smp_processor_id());
  859. __lpc_handle_xmit(ndev);
  860. __netif_tx_unlock(txq);
  861. rx_done = __lpc_handle_recv(ndev, budget);
  862. if (rx_done < budget) {
  863. napi_complete(napi);
  864. lpc_eth_enable_int(pldat->net_base);
  865. }
  866. return rx_done;
  867. }
  868. static irqreturn_t __lpc_eth_interrupt(int irq, void *dev_id)
  869. {
  870. struct net_device *ndev = dev_id;
  871. struct netdata_local *pldat = netdev_priv(ndev);
  872. u32 tmp;
  873. spin_lock(&pldat->lock);
  874. tmp = readl(LPC_ENET_INTSTATUS(pldat->net_base));
  875. /* Clear interrupts */
  876. writel(tmp, LPC_ENET_INTCLEAR(pldat->net_base));
  877. lpc_eth_disable_int(pldat->net_base);
  878. if (likely(napi_schedule_prep(&pldat->napi)))
  879. __napi_schedule(&pldat->napi);
  880. spin_unlock(&pldat->lock);
  881. return IRQ_HANDLED;
  882. }
  883. static int lpc_eth_close(struct net_device *ndev)
  884. {
  885. unsigned long flags;
  886. struct netdata_local *pldat = netdev_priv(ndev);
  887. if (netif_msg_ifdown(pldat))
  888. dev_dbg(&pldat->pdev->dev, "shutting down %s\n", ndev->name);
  889. napi_disable(&pldat->napi);
  890. netif_stop_queue(ndev);
  891. if (ndev->phydev)
  892. phy_stop(ndev->phydev);
  893. spin_lock_irqsave(&pldat->lock, flags);
  894. __lpc_eth_reset(pldat);
  895. netif_carrier_off(ndev);
  896. writel(0, LPC_ENET_MAC1(pldat->net_base));
  897. writel(0, LPC_ENET_MAC2(pldat->net_base));
  898. spin_unlock_irqrestore(&pldat->lock, flags);
  899. clk_disable_unprepare(pldat->clk);
  900. return 0;
  901. }
  902. static int lpc_eth_hard_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  903. {
  904. struct netdata_local *pldat = netdev_priv(ndev);
  905. u32 len, txidx;
  906. u32 *ptxstat;
  907. struct txrx_desc_t *ptxrxdesc;
  908. len = skb->len;
  909. spin_lock_irq(&pldat->lock);
  910. if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1)) {
  911. /* This function should never be called when there are no
  912. buffers */
  913. netif_stop_queue(ndev);
  914. spin_unlock_irq(&pldat->lock);
  915. WARN(1, "BUG! TX request when no free TX buffers!\n");
  916. return NETDEV_TX_BUSY;
  917. }
  918. /* Get the next TX descriptor index */
  919. txidx = readl(LPC_ENET_TXPRODUCEINDEX(pldat->net_base));
  920. /* Setup control for the transfer */
  921. ptxstat = &pldat->tx_stat_v[txidx];
  922. *ptxstat = 0;
  923. ptxrxdesc = &pldat->tx_desc_v[txidx];
  924. ptxrxdesc->control =
  925. (len - 1) | TXDESC_CONTROL_LAST | TXDESC_CONTROL_INT;
  926. /* Copy data to the DMA buffer */
  927. memcpy(pldat->tx_buff_v + txidx * ENET_MAXF_SIZE, skb->data, len);
  928. /* Save the buffer and increment the buffer counter */
  929. pldat->skblen[txidx] = len;
  930. pldat->num_used_tx_buffs++;
  931. /* Start transmit */
  932. txidx++;
  933. if (txidx >= ENET_TX_DESC)
  934. txidx = 0;
  935. writel(txidx, LPC_ENET_TXPRODUCEINDEX(pldat->net_base));
  936. /* Stop queue if no more TX buffers */
  937. if (pldat->num_used_tx_buffs >= (ENET_TX_DESC - 1))
  938. netif_stop_queue(ndev);
  939. spin_unlock_irq(&pldat->lock);
  940. dev_kfree_skb(skb);
  941. return NETDEV_TX_OK;
  942. }
  943. static int lpc_set_mac_address(struct net_device *ndev, void *p)
  944. {
  945. struct sockaddr *addr = p;
  946. struct netdata_local *pldat = netdev_priv(ndev);
  947. unsigned long flags;
  948. if (!is_valid_ether_addr(addr->sa_data))
  949. return -EADDRNOTAVAIL;
  950. memcpy(ndev->dev_addr, addr->sa_data, ETH_ALEN);
  951. spin_lock_irqsave(&pldat->lock, flags);
  952. /* Set station address */
  953. __lpc_set_mac(pldat, ndev->dev_addr);
  954. spin_unlock_irqrestore(&pldat->lock, flags);
  955. return 0;
  956. }
  957. static void lpc_eth_set_multicast_list(struct net_device *ndev)
  958. {
  959. struct netdata_local *pldat = netdev_priv(ndev);
  960. struct netdev_hw_addr_list *mcptr = &ndev->mc;
  961. struct netdev_hw_addr *ha;
  962. u32 tmp32, hash_val, hashlo, hashhi;
  963. unsigned long flags;
  964. spin_lock_irqsave(&pldat->lock, flags);
  965. /* Set station address */
  966. __lpc_set_mac(pldat, ndev->dev_addr);
  967. tmp32 = LPC_RXFLTRW_ACCEPTUBROADCAST | LPC_RXFLTRW_ACCEPTPERFECT;
  968. if (ndev->flags & IFF_PROMISC)
  969. tmp32 |= LPC_RXFLTRW_ACCEPTUNICAST |
  970. LPC_RXFLTRW_ACCEPTUMULTICAST;
  971. if (ndev->flags & IFF_ALLMULTI)
  972. tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICAST;
  973. if (netdev_hw_addr_list_count(mcptr))
  974. tmp32 |= LPC_RXFLTRW_ACCEPTUMULTICASTHASH;
  975. writel(tmp32, LPC_ENET_RXFILTER_CTRL(pldat->net_base));
  976. /* Set initial hash table */
  977. hashlo = 0x0;
  978. hashhi = 0x0;
  979. /* 64 bits : multicast address in hash table */
  980. netdev_hw_addr_list_for_each(ha, mcptr) {
  981. hash_val = (ether_crc(6, ha->addr) >> 23) & 0x3F;
  982. if (hash_val >= 32)
  983. hashhi |= 1 << (hash_val - 32);
  984. else
  985. hashlo |= 1 << hash_val;
  986. }
  987. writel(hashlo, LPC_ENET_HASHFILTERL(pldat->net_base));
  988. writel(hashhi, LPC_ENET_HASHFILTERH(pldat->net_base));
  989. spin_unlock_irqrestore(&pldat->lock, flags);
  990. }
  991. static int lpc_eth_ioctl(struct net_device *ndev, struct ifreq *req, int cmd)
  992. {
  993. struct phy_device *phydev = ndev->phydev;
  994. if (!netif_running(ndev))
  995. return -EINVAL;
  996. if (!phydev)
  997. return -ENODEV;
  998. return phy_mii_ioctl(phydev, req, cmd);
  999. }
  1000. static int lpc_eth_open(struct net_device *ndev)
  1001. {
  1002. struct netdata_local *pldat = netdev_priv(ndev);
  1003. int ret;
  1004. if (netif_msg_ifup(pldat))
  1005. dev_dbg(&pldat->pdev->dev, "enabling %s\n", ndev->name);
  1006. ret = clk_prepare_enable(pldat->clk);
  1007. if (ret)
  1008. return ret;
  1009. /* Suspended PHY makes LPC ethernet core block, so resume now */
  1010. phy_resume(ndev->phydev);
  1011. /* Reset and initialize */
  1012. __lpc_eth_reset(pldat);
  1013. __lpc_eth_init(pldat);
  1014. /* schedule a link state check */
  1015. phy_start(ndev->phydev);
  1016. netif_start_queue(ndev);
  1017. napi_enable(&pldat->napi);
  1018. return 0;
  1019. }
  1020. /*
  1021. * Ethtool ops
  1022. */
  1023. static void lpc_eth_ethtool_getdrvinfo(struct net_device *ndev,
  1024. struct ethtool_drvinfo *info)
  1025. {
  1026. strlcpy(info->driver, MODNAME, sizeof(info->driver));
  1027. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1028. strlcpy(info->bus_info, dev_name(ndev->dev.parent),
  1029. sizeof(info->bus_info));
  1030. }
  1031. static u32 lpc_eth_ethtool_getmsglevel(struct net_device *ndev)
  1032. {
  1033. struct netdata_local *pldat = netdev_priv(ndev);
  1034. return pldat->msg_enable;
  1035. }
  1036. static void lpc_eth_ethtool_setmsglevel(struct net_device *ndev, u32 level)
  1037. {
  1038. struct netdata_local *pldat = netdev_priv(ndev);
  1039. pldat->msg_enable = level;
  1040. }
  1041. static const struct ethtool_ops lpc_eth_ethtool_ops = {
  1042. .get_drvinfo = lpc_eth_ethtool_getdrvinfo,
  1043. .get_msglevel = lpc_eth_ethtool_getmsglevel,
  1044. .set_msglevel = lpc_eth_ethtool_setmsglevel,
  1045. .get_link = ethtool_op_get_link,
  1046. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  1047. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  1048. };
  1049. static const struct net_device_ops lpc_netdev_ops = {
  1050. .ndo_open = lpc_eth_open,
  1051. .ndo_stop = lpc_eth_close,
  1052. .ndo_start_xmit = lpc_eth_hard_start_xmit,
  1053. .ndo_set_rx_mode = lpc_eth_set_multicast_list,
  1054. .ndo_do_ioctl = lpc_eth_ioctl,
  1055. .ndo_set_mac_address = lpc_set_mac_address,
  1056. .ndo_validate_addr = eth_validate_addr,
  1057. .ndo_change_mtu = eth_change_mtu,
  1058. };
  1059. static int lpc_eth_drv_probe(struct platform_device *pdev)
  1060. {
  1061. struct resource *res;
  1062. struct net_device *ndev;
  1063. struct netdata_local *pldat;
  1064. struct phy_device *phydev;
  1065. dma_addr_t dma_handle;
  1066. int irq, ret;
  1067. u32 tmp;
  1068. /* Setup network interface for RMII or MII mode */
  1069. tmp = __raw_readl(LPC32XX_CLKPWR_MACCLK_CTRL);
  1070. tmp &= ~LPC32XX_CLKPWR_MACCTRL_PINS_MSK;
  1071. if (lpc_phy_interface_mode(&pdev->dev) == PHY_INTERFACE_MODE_MII)
  1072. tmp |= LPC32XX_CLKPWR_MACCTRL_USE_MII_PINS;
  1073. else
  1074. tmp |= LPC32XX_CLKPWR_MACCTRL_USE_RMII_PINS;
  1075. __raw_writel(tmp, LPC32XX_CLKPWR_MACCLK_CTRL);
  1076. /* Get platform resources */
  1077. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1078. irq = platform_get_irq(pdev, 0);
  1079. if (!res || irq < 0) {
  1080. dev_err(&pdev->dev, "error getting resources.\n");
  1081. ret = -ENXIO;
  1082. goto err_exit;
  1083. }
  1084. /* Allocate net driver data structure */
  1085. ndev = alloc_etherdev(sizeof(struct netdata_local));
  1086. if (!ndev) {
  1087. dev_err(&pdev->dev, "could not allocate device.\n");
  1088. ret = -ENOMEM;
  1089. goto err_exit;
  1090. }
  1091. SET_NETDEV_DEV(ndev, &pdev->dev);
  1092. pldat = netdev_priv(ndev);
  1093. pldat->pdev = pdev;
  1094. pldat->ndev = ndev;
  1095. spin_lock_init(&pldat->lock);
  1096. /* Save resources */
  1097. ndev->irq = irq;
  1098. /* Get clock for the device */
  1099. pldat->clk = clk_get(&pdev->dev, NULL);
  1100. if (IS_ERR(pldat->clk)) {
  1101. dev_err(&pdev->dev, "error getting clock.\n");
  1102. ret = PTR_ERR(pldat->clk);
  1103. goto err_out_free_dev;
  1104. }
  1105. /* Enable network clock */
  1106. ret = clk_prepare_enable(pldat->clk);
  1107. if (ret)
  1108. goto err_out_clk_put;
  1109. /* Map IO space */
  1110. pldat->net_base = ioremap(res->start, resource_size(res));
  1111. if (!pldat->net_base) {
  1112. dev_err(&pdev->dev, "failed to map registers\n");
  1113. ret = -ENOMEM;
  1114. goto err_out_disable_clocks;
  1115. }
  1116. ret = request_irq(ndev->irq, __lpc_eth_interrupt, 0,
  1117. ndev->name, ndev);
  1118. if (ret) {
  1119. dev_err(&pdev->dev, "error requesting interrupt.\n");
  1120. goto err_out_iounmap;
  1121. }
  1122. /* Setup driver functions */
  1123. ndev->netdev_ops = &lpc_netdev_ops;
  1124. ndev->ethtool_ops = &lpc_eth_ethtool_ops;
  1125. ndev->watchdog_timeo = msecs_to_jiffies(2500);
  1126. /* Get size of DMA buffers/descriptors region */
  1127. pldat->dma_buff_size = (ENET_TX_DESC + ENET_RX_DESC) * (ENET_MAXF_SIZE +
  1128. sizeof(struct txrx_desc_t) + sizeof(struct rx_status_t));
  1129. pldat->dma_buff_base_v = 0;
  1130. if (use_iram_for_net(&pldat->pdev->dev)) {
  1131. dma_handle = LPC32XX_IRAM_BASE;
  1132. if (pldat->dma_buff_size <= lpc32xx_return_iram_size())
  1133. pldat->dma_buff_base_v =
  1134. io_p2v(LPC32XX_IRAM_BASE);
  1135. else
  1136. netdev_err(ndev,
  1137. "IRAM not big enough for net buffers, using SDRAM instead.\n");
  1138. }
  1139. if (pldat->dma_buff_base_v == 0) {
  1140. ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  1141. if (ret)
  1142. goto err_out_free_irq;
  1143. pldat->dma_buff_size = PAGE_ALIGN(pldat->dma_buff_size);
  1144. /* Allocate a chunk of memory for the DMA ethernet buffers
  1145. and descriptors */
  1146. pldat->dma_buff_base_v =
  1147. dma_alloc_coherent(&pldat->pdev->dev,
  1148. pldat->dma_buff_size, &dma_handle,
  1149. GFP_KERNEL);
  1150. if (pldat->dma_buff_base_v == NULL) {
  1151. ret = -ENOMEM;
  1152. goto err_out_free_irq;
  1153. }
  1154. }
  1155. pldat->dma_buff_base_p = dma_handle;
  1156. netdev_dbg(ndev, "IO address space :%pR\n", res);
  1157. netdev_dbg(ndev, "IO address size :%d\n", resource_size(res));
  1158. netdev_dbg(ndev, "IO address (mapped) :0x%p\n",
  1159. pldat->net_base);
  1160. netdev_dbg(ndev, "IRQ number :%d\n", ndev->irq);
  1161. netdev_dbg(ndev, "DMA buffer size :%d\n", pldat->dma_buff_size);
  1162. netdev_dbg(ndev, "DMA buffer P address :0x%08x\n",
  1163. pldat->dma_buff_base_p);
  1164. netdev_dbg(ndev, "DMA buffer V address :0x%p\n",
  1165. pldat->dma_buff_base_v);
  1166. /* Get MAC address from current HW setting (POR state is all zeros) */
  1167. __lpc_get_mac(pldat, ndev->dev_addr);
  1168. if (!is_valid_ether_addr(ndev->dev_addr)) {
  1169. const char *macaddr = of_get_mac_address(pdev->dev.of_node);
  1170. if (macaddr)
  1171. memcpy(ndev->dev_addr, macaddr, ETH_ALEN);
  1172. }
  1173. if (!is_valid_ether_addr(ndev->dev_addr))
  1174. eth_hw_addr_random(ndev);
  1175. /* Reset the ethernet controller */
  1176. __lpc_eth_reset(pldat);
  1177. /* then shut everything down to save power */
  1178. __lpc_eth_shutdown(pldat);
  1179. /* Set default parameters */
  1180. pldat->msg_enable = NETIF_MSG_LINK;
  1181. /* Force an MII interface reset and clock setup */
  1182. __lpc_mii_mngt_reset(pldat);
  1183. /* Force default PHY interface setup in chip, this will probably be
  1184. changed by the PHY driver */
  1185. pldat->link = 0;
  1186. pldat->speed = 100;
  1187. pldat->duplex = DUPLEX_FULL;
  1188. __lpc_params_setup(pldat);
  1189. netif_napi_add(ndev, &pldat->napi, lpc_eth_poll, NAPI_WEIGHT);
  1190. ret = register_netdev(ndev);
  1191. if (ret) {
  1192. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  1193. goto err_out_dma_unmap;
  1194. }
  1195. platform_set_drvdata(pdev, ndev);
  1196. ret = lpc_mii_init(pldat);
  1197. if (ret)
  1198. goto err_out_unregister_netdev;
  1199. netdev_info(ndev, "LPC mac at 0x%08x irq %d\n",
  1200. res->start, ndev->irq);
  1201. phydev = ndev->phydev;
  1202. device_init_wakeup(&pdev->dev, 1);
  1203. device_set_wakeup_enable(&pdev->dev, 0);
  1204. return 0;
  1205. err_out_unregister_netdev:
  1206. unregister_netdev(ndev);
  1207. err_out_dma_unmap:
  1208. if (!use_iram_for_net(&pldat->pdev->dev) ||
  1209. pldat->dma_buff_size > lpc32xx_return_iram_size())
  1210. dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
  1211. pldat->dma_buff_base_v,
  1212. pldat->dma_buff_base_p);
  1213. err_out_free_irq:
  1214. free_irq(ndev->irq, ndev);
  1215. err_out_iounmap:
  1216. iounmap(pldat->net_base);
  1217. err_out_disable_clocks:
  1218. clk_disable_unprepare(pldat->clk);
  1219. err_out_clk_put:
  1220. clk_put(pldat->clk);
  1221. err_out_free_dev:
  1222. free_netdev(ndev);
  1223. err_exit:
  1224. pr_err("%s: not found (%d).\n", MODNAME, ret);
  1225. return ret;
  1226. }
  1227. static int lpc_eth_drv_remove(struct platform_device *pdev)
  1228. {
  1229. struct net_device *ndev = platform_get_drvdata(pdev);
  1230. struct netdata_local *pldat = netdev_priv(ndev);
  1231. unregister_netdev(ndev);
  1232. if (!use_iram_for_net(&pldat->pdev->dev) ||
  1233. pldat->dma_buff_size > lpc32xx_return_iram_size())
  1234. dma_free_coherent(&pldat->pdev->dev, pldat->dma_buff_size,
  1235. pldat->dma_buff_base_v,
  1236. pldat->dma_buff_base_p);
  1237. free_irq(ndev->irq, ndev);
  1238. iounmap(pldat->net_base);
  1239. mdiobus_unregister(pldat->mii_bus);
  1240. mdiobus_free(pldat->mii_bus);
  1241. clk_disable_unprepare(pldat->clk);
  1242. clk_put(pldat->clk);
  1243. free_netdev(ndev);
  1244. return 0;
  1245. }
  1246. #ifdef CONFIG_PM
  1247. static int lpc_eth_drv_suspend(struct platform_device *pdev,
  1248. pm_message_t state)
  1249. {
  1250. struct net_device *ndev = platform_get_drvdata(pdev);
  1251. struct netdata_local *pldat = netdev_priv(ndev);
  1252. if (device_may_wakeup(&pdev->dev))
  1253. enable_irq_wake(ndev->irq);
  1254. if (ndev) {
  1255. if (netif_running(ndev)) {
  1256. netif_device_detach(ndev);
  1257. __lpc_eth_shutdown(pldat);
  1258. clk_disable_unprepare(pldat->clk);
  1259. /*
  1260. * Reset again now clock is disable to be sure
  1261. * EMC_MDC is down
  1262. */
  1263. __lpc_eth_reset(pldat);
  1264. }
  1265. }
  1266. return 0;
  1267. }
  1268. static int lpc_eth_drv_resume(struct platform_device *pdev)
  1269. {
  1270. struct net_device *ndev = platform_get_drvdata(pdev);
  1271. struct netdata_local *pldat;
  1272. if (device_may_wakeup(&pdev->dev))
  1273. disable_irq_wake(ndev->irq);
  1274. if (ndev) {
  1275. if (netif_running(ndev)) {
  1276. pldat = netdev_priv(ndev);
  1277. /* Enable interface clock */
  1278. clk_enable(pldat->clk);
  1279. /* Reset and initialize */
  1280. __lpc_eth_reset(pldat);
  1281. __lpc_eth_init(pldat);
  1282. netif_device_attach(ndev);
  1283. }
  1284. }
  1285. return 0;
  1286. }
  1287. #endif
  1288. #ifdef CONFIG_OF
  1289. static const struct of_device_id lpc_eth_match[] = {
  1290. { .compatible = "nxp,lpc-eth" },
  1291. { }
  1292. };
  1293. MODULE_DEVICE_TABLE(of, lpc_eth_match);
  1294. #endif
  1295. static struct platform_driver lpc_eth_driver = {
  1296. .probe = lpc_eth_drv_probe,
  1297. .remove = lpc_eth_drv_remove,
  1298. #ifdef CONFIG_PM
  1299. .suspend = lpc_eth_drv_suspend,
  1300. .resume = lpc_eth_drv_resume,
  1301. #endif
  1302. .driver = {
  1303. .name = MODNAME,
  1304. .of_match_table = of_match_ptr(lpc_eth_match),
  1305. },
  1306. };
  1307. module_platform_driver(lpc_eth_driver);
  1308. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com>");
  1309. MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
  1310. MODULE_DESCRIPTION("LPC Ethernet Driver");
  1311. MODULE_LICENSE("GPL");