sh_eth.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722
  1. /*
  2. * SuperH Ethernet device driver
  3. *
  4. * Copyright (C) 2006-2012 Nobuhiro Iwamatsu
  5. * Copyright (C) 2008-2013 Renesas Solutions Corp.
  6. * Copyright (C) 2013 Cogent Embedded, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms and conditions of the GNU General Public License,
  10. * version 2, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * The full GNU General Public License is included in this distribution in
  21. * the file called "COPYING".
  22. */
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/delay.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/mdio-bitbang.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/phy.h>
  35. #include <linux/cache.h>
  36. #include <linux/io.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/slab.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/if_vlan.h>
  41. #include <linux/clk.h>
  42. #include <linux/sh_eth.h>
  43. #include "sh_eth.h"
  44. #define SH_ETH_DEF_MSG_ENABLE \
  45. (NETIF_MSG_LINK | \
  46. NETIF_MSG_TIMER | \
  47. NETIF_MSG_RX_ERR| \
  48. NETIF_MSG_TX_ERR)
  49. static const u16 sh_eth_offset_gigabit[SH_ETH_MAX_REGISTER_OFFSET] = {
  50. [EDSR] = 0x0000,
  51. [EDMR] = 0x0400,
  52. [EDTRR] = 0x0408,
  53. [EDRRR] = 0x0410,
  54. [EESR] = 0x0428,
  55. [EESIPR] = 0x0430,
  56. [TDLAR] = 0x0010,
  57. [TDFAR] = 0x0014,
  58. [TDFXR] = 0x0018,
  59. [TDFFR] = 0x001c,
  60. [RDLAR] = 0x0030,
  61. [RDFAR] = 0x0034,
  62. [RDFXR] = 0x0038,
  63. [RDFFR] = 0x003c,
  64. [TRSCER] = 0x0438,
  65. [RMFCR] = 0x0440,
  66. [TFTR] = 0x0448,
  67. [FDR] = 0x0450,
  68. [RMCR] = 0x0458,
  69. [RPADIR] = 0x0460,
  70. [FCFTR] = 0x0468,
  71. [CSMR] = 0x04E4,
  72. [ECMR] = 0x0500,
  73. [ECSR] = 0x0510,
  74. [ECSIPR] = 0x0518,
  75. [PIR] = 0x0520,
  76. [PSR] = 0x0528,
  77. [PIPR] = 0x052c,
  78. [RFLR] = 0x0508,
  79. [APR] = 0x0554,
  80. [MPR] = 0x0558,
  81. [PFTCR] = 0x055c,
  82. [PFRCR] = 0x0560,
  83. [TPAUSER] = 0x0564,
  84. [GECMR] = 0x05b0,
  85. [BCULR] = 0x05b4,
  86. [MAHR] = 0x05c0,
  87. [MALR] = 0x05c8,
  88. [TROCR] = 0x0700,
  89. [CDCR] = 0x0708,
  90. [LCCR] = 0x0710,
  91. [CEFCR] = 0x0740,
  92. [FRECR] = 0x0748,
  93. [TSFRCR] = 0x0750,
  94. [TLFRCR] = 0x0758,
  95. [RFCR] = 0x0760,
  96. [CERCR] = 0x0768,
  97. [CEECR] = 0x0770,
  98. [MAFCR] = 0x0778,
  99. [RMII_MII] = 0x0790,
  100. [ARSTR] = 0x0000,
  101. [TSU_CTRST] = 0x0004,
  102. [TSU_FWEN0] = 0x0010,
  103. [TSU_FWEN1] = 0x0014,
  104. [TSU_FCM] = 0x0018,
  105. [TSU_BSYSL0] = 0x0020,
  106. [TSU_BSYSL1] = 0x0024,
  107. [TSU_PRISL0] = 0x0028,
  108. [TSU_PRISL1] = 0x002c,
  109. [TSU_FWSL0] = 0x0030,
  110. [TSU_FWSL1] = 0x0034,
  111. [TSU_FWSLC] = 0x0038,
  112. [TSU_QTAG0] = 0x0040,
  113. [TSU_QTAG1] = 0x0044,
  114. [TSU_FWSR] = 0x0050,
  115. [TSU_FWINMK] = 0x0054,
  116. [TSU_ADQT0] = 0x0048,
  117. [TSU_ADQT1] = 0x004c,
  118. [TSU_VTAG0] = 0x0058,
  119. [TSU_VTAG1] = 0x005c,
  120. [TSU_ADSBSY] = 0x0060,
  121. [TSU_TEN] = 0x0064,
  122. [TSU_POST1] = 0x0070,
  123. [TSU_POST2] = 0x0074,
  124. [TSU_POST3] = 0x0078,
  125. [TSU_POST4] = 0x007c,
  126. [TSU_ADRH0] = 0x0100,
  127. [TSU_ADRL0] = 0x0104,
  128. [TSU_ADRH31] = 0x01f8,
  129. [TSU_ADRL31] = 0x01fc,
  130. [TXNLCR0] = 0x0080,
  131. [TXALCR0] = 0x0084,
  132. [RXNLCR0] = 0x0088,
  133. [RXALCR0] = 0x008c,
  134. [FWNLCR0] = 0x0090,
  135. [FWALCR0] = 0x0094,
  136. [TXNLCR1] = 0x00a0,
  137. [TXALCR1] = 0x00a0,
  138. [RXNLCR1] = 0x00a8,
  139. [RXALCR1] = 0x00ac,
  140. [FWNLCR1] = 0x00b0,
  141. [FWALCR1] = 0x00b4,
  142. };
  143. static const u16 sh_eth_offset_fast_rcar[SH_ETH_MAX_REGISTER_OFFSET] = {
  144. [ECMR] = 0x0300,
  145. [RFLR] = 0x0308,
  146. [ECSR] = 0x0310,
  147. [ECSIPR] = 0x0318,
  148. [PIR] = 0x0320,
  149. [PSR] = 0x0328,
  150. [RDMLR] = 0x0340,
  151. [IPGR] = 0x0350,
  152. [APR] = 0x0354,
  153. [MPR] = 0x0358,
  154. [RFCF] = 0x0360,
  155. [TPAUSER] = 0x0364,
  156. [TPAUSECR] = 0x0368,
  157. [MAHR] = 0x03c0,
  158. [MALR] = 0x03c8,
  159. [TROCR] = 0x03d0,
  160. [CDCR] = 0x03d4,
  161. [LCCR] = 0x03d8,
  162. [CNDCR] = 0x03dc,
  163. [CEFCR] = 0x03e4,
  164. [FRECR] = 0x03e8,
  165. [TSFRCR] = 0x03ec,
  166. [TLFRCR] = 0x03f0,
  167. [RFCR] = 0x03f4,
  168. [MAFCR] = 0x03f8,
  169. [EDMR] = 0x0200,
  170. [EDTRR] = 0x0208,
  171. [EDRRR] = 0x0210,
  172. [TDLAR] = 0x0218,
  173. [RDLAR] = 0x0220,
  174. [EESR] = 0x0228,
  175. [EESIPR] = 0x0230,
  176. [TRSCER] = 0x0238,
  177. [RMFCR] = 0x0240,
  178. [TFTR] = 0x0248,
  179. [FDR] = 0x0250,
  180. [RMCR] = 0x0258,
  181. [TFUCR] = 0x0264,
  182. [RFOCR] = 0x0268,
  183. [FCFTR] = 0x0270,
  184. [TRIMD] = 0x027c,
  185. };
  186. static const u16 sh_eth_offset_fast_sh4[SH_ETH_MAX_REGISTER_OFFSET] = {
  187. [ECMR] = 0x0100,
  188. [RFLR] = 0x0108,
  189. [ECSR] = 0x0110,
  190. [ECSIPR] = 0x0118,
  191. [PIR] = 0x0120,
  192. [PSR] = 0x0128,
  193. [RDMLR] = 0x0140,
  194. [IPGR] = 0x0150,
  195. [APR] = 0x0154,
  196. [MPR] = 0x0158,
  197. [TPAUSER] = 0x0164,
  198. [RFCF] = 0x0160,
  199. [TPAUSECR] = 0x0168,
  200. [BCFRR] = 0x016c,
  201. [MAHR] = 0x01c0,
  202. [MALR] = 0x01c8,
  203. [TROCR] = 0x01d0,
  204. [CDCR] = 0x01d4,
  205. [LCCR] = 0x01d8,
  206. [CNDCR] = 0x01dc,
  207. [CEFCR] = 0x01e4,
  208. [FRECR] = 0x01e8,
  209. [TSFRCR] = 0x01ec,
  210. [TLFRCR] = 0x01f0,
  211. [RFCR] = 0x01f4,
  212. [MAFCR] = 0x01f8,
  213. [RTRATE] = 0x01fc,
  214. [EDMR] = 0x0000,
  215. [EDTRR] = 0x0008,
  216. [EDRRR] = 0x0010,
  217. [TDLAR] = 0x0018,
  218. [RDLAR] = 0x0020,
  219. [EESR] = 0x0028,
  220. [EESIPR] = 0x0030,
  221. [TRSCER] = 0x0038,
  222. [RMFCR] = 0x0040,
  223. [TFTR] = 0x0048,
  224. [FDR] = 0x0050,
  225. [RMCR] = 0x0058,
  226. [TFUCR] = 0x0064,
  227. [RFOCR] = 0x0068,
  228. [FCFTR] = 0x0070,
  229. [RPADIR] = 0x0078,
  230. [TRIMD] = 0x007c,
  231. [RBWAR] = 0x00c8,
  232. [RDFAR] = 0x00cc,
  233. [TBRAR] = 0x00d4,
  234. [TDFAR] = 0x00d8,
  235. };
  236. static const u16 sh_eth_offset_fast_sh3_sh2[SH_ETH_MAX_REGISTER_OFFSET] = {
  237. [ECMR] = 0x0160,
  238. [ECSR] = 0x0164,
  239. [ECSIPR] = 0x0168,
  240. [PIR] = 0x016c,
  241. [MAHR] = 0x0170,
  242. [MALR] = 0x0174,
  243. [RFLR] = 0x0178,
  244. [PSR] = 0x017c,
  245. [TROCR] = 0x0180,
  246. [CDCR] = 0x0184,
  247. [LCCR] = 0x0188,
  248. [CNDCR] = 0x018c,
  249. [CEFCR] = 0x0194,
  250. [FRECR] = 0x0198,
  251. [TSFRCR] = 0x019c,
  252. [TLFRCR] = 0x01a0,
  253. [RFCR] = 0x01a4,
  254. [MAFCR] = 0x01a8,
  255. [IPGR] = 0x01b4,
  256. [APR] = 0x01b8,
  257. [MPR] = 0x01bc,
  258. [TPAUSER] = 0x01c4,
  259. [BCFR] = 0x01cc,
  260. [ARSTR] = 0x0000,
  261. [TSU_CTRST] = 0x0004,
  262. [TSU_FWEN0] = 0x0010,
  263. [TSU_FWEN1] = 0x0014,
  264. [TSU_FCM] = 0x0018,
  265. [TSU_BSYSL0] = 0x0020,
  266. [TSU_BSYSL1] = 0x0024,
  267. [TSU_PRISL0] = 0x0028,
  268. [TSU_PRISL1] = 0x002c,
  269. [TSU_FWSL0] = 0x0030,
  270. [TSU_FWSL1] = 0x0034,
  271. [TSU_FWSLC] = 0x0038,
  272. [TSU_QTAGM0] = 0x0040,
  273. [TSU_QTAGM1] = 0x0044,
  274. [TSU_ADQT0] = 0x0048,
  275. [TSU_ADQT1] = 0x004c,
  276. [TSU_FWSR] = 0x0050,
  277. [TSU_FWINMK] = 0x0054,
  278. [TSU_ADSBSY] = 0x0060,
  279. [TSU_TEN] = 0x0064,
  280. [TSU_POST1] = 0x0070,
  281. [TSU_POST2] = 0x0074,
  282. [TSU_POST3] = 0x0078,
  283. [TSU_POST4] = 0x007c,
  284. [TXNLCR0] = 0x0080,
  285. [TXALCR0] = 0x0084,
  286. [RXNLCR0] = 0x0088,
  287. [RXALCR0] = 0x008c,
  288. [FWNLCR0] = 0x0090,
  289. [FWALCR0] = 0x0094,
  290. [TXNLCR1] = 0x00a0,
  291. [TXALCR1] = 0x00a0,
  292. [RXNLCR1] = 0x00a8,
  293. [RXALCR1] = 0x00ac,
  294. [FWNLCR1] = 0x00b0,
  295. [FWALCR1] = 0x00b4,
  296. [TSU_ADRH0] = 0x0100,
  297. [TSU_ADRL0] = 0x0104,
  298. [TSU_ADRL31] = 0x01fc,
  299. };
  300. static int sh_eth_is_gether(struct sh_eth_private *mdp)
  301. {
  302. if (mdp->reg_offset == sh_eth_offset_gigabit)
  303. return 1;
  304. else
  305. return 0;
  306. }
  307. static void __maybe_unused sh_eth_select_mii(struct net_device *ndev)
  308. {
  309. u32 value = 0x0;
  310. struct sh_eth_private *mdp = netdev_priv(ndev);
  311. switch (mdp->phy_interface) {
  312. case PHY_INTERFACE_MODE_GMII:
  313. value = 0x2;
  314. break;
  315. case PHY_INTERFACE_MODE_MII:
  316. value = 0x1;
  317. break;
  318. case PHY_INTERFACE_MODE_RMII:
  319. value = 0x0;
  320. break;
  321. default:
  322. pr_warn("PHY interface mode was not setup. Set to MII.\n");
  323. value = 0x1;
  324. break;
  325. }
  326. sh_eth_write(ndev, value, RMII_MII);
  327. }
  328. static void __maybe_unused sh_eth_set_duplex(struct net_device *ndev)
  329. {
  330. struct sh_eth_private *mdp = netdev_priv(ndev);
  331. if (mdp->duplex) /* Full */
  332. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_DM, ECMR);
  333. else /* Half */
  334. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_DM, ECMR);
  335. }
  336. /* There is CPU dependent code */
  337. #if defined(CONFIG_ARCH_R8A7778) || defined(CONFIG_ARCH_R8A7779)
  338. static void sh_eth_set_rate(struct net_device *ndev)
  339. {
  340. struct sh_eth_private *mdp = netdev_priv(ndev);
  341. switch (mdp->speed) {
  342. case 10: /* 10BASE */
  343. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_ELB, ECMR);
  344. break;
  345. case 100:/* 100BASE */
  346. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_ELB, ECMR);
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. /* R8A7778/9 */
  353. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  354. .set_duplex = sh_eth_set_duplex,
  355. .set_rate = sh_eth_set_rate,
  356. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  357. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  358. .eesipr_value = 0x01ff009f,
  359. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  360. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
  361. EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
  362. .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
  363. .apr = 1,
  364. .mpr = 1,
  365. .tpauser = 1,
  366. .hw_swap = 1,
  367. };
  368. #elif defined(CONFIG_CPU_SUBTYPE_SH7724)
  369. static void sh_eth_set_rate(struct net_device *ndev)
  370. {
  371. struct sh_eth_private *mdp = netdev_priv(ndev);
  372. switch (mdp->speed) {
  373. case 10: /* 10BASE */
  374. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) & ~ECMR_RTM, ECMR);
  375. break;
  376. case 100:/* 100BASE */
  377. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) | ECMR_RTM, ECMR);
  378. break;
  379. default:
  380. break;
  381. }
  382. }
  383. /* SH7724 */
  384. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  385. .set_duplex = sh_eth_set_duplex,
  386. .set_rate = sh_eth_set_rate,
  387. .ecsr_value = ECSR_PSRTO | ECSR_LCHNG | ECSR_ICD,
  388. .ecsipr_value = ECSIPR_PSRTOIP | ECSIPR_LCHNGIP | ECSIPR_ICDIP,
  389. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x01ff009f,
  390. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  391. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
  392. EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
  393. .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
  394. .apr = 1,
  395. .mpr = 1,
  396. .tpauser = 1,
  397. .hw_swap = 1,
  398. .rpadir = 1,
  399. .rpadir_value = 0x00020000, /* NET_IP_ALIGN assumed to be 2 */
  400. };
  401. #elif defined(CONFIG_CPU_SUBTYPE_SH7757)
  402. #define SH_ETH_HAS_BOTH_MODULES 1
  403. static void sh_eth_set_rate(struct net_device *ndev)
  404. {
  405. struct sh_eth_private *mdp = netdev_priv(ndev);
  406. switch (mdp->speed) {
  407. case 10: /* 10BASE */
  408. sh_eth_write(ndev, 0, RTRATE);
  409. break;
  410. case 100:/* 100BASE */
  411. sh_eth_write(ndev, 1, RTRATE);
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. /* SH7757 */
  418. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  419. .set_duplex = sh_eth_set_duplex,
  420. .set_rate = sh_eth_set_rate,
  421. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  422. .rmcr_value = 0x00000001,
  423. .tx_check = EESR_FTC | EESR_CND | EESR_DLC | EESR_CD | EESR_RTO,
  424. .eesr_err_check = EESR_TWB | EESR_TABT | EESR_RABT | EESR_RDE |
  425. EESR_RFRMER | EESR_TFE | EESR_TDE | EESR_ECI,
  426. .tx_error_check = EESR_TWB | EESR_TABT | EESR_TDE | EESR_TFE,
  427. .irq_flags = IRQF_SHARED,
  428. .apr = 1,
  429. .mpr = 1,
  430. .tpauser = 1,
  431. .hw_swap = 1,
  432. .no_ade = 1,
  433. .rpadir = 1,
  434. .rpadir_value = 2 << 16,
  435. };
  436. #define SH_GIGA_ETH_BASE 0xfee00000
  437. #define GIGA_MALR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c8)
  438. #define GIGA_MAHR(port) (SH_GIGA_ETH_BASE + 0x800 * (port) + 0x05c0)
  439. static void sh_eth_chip_reset_giga(struct net_device *ndev)
  440. {
  441. int i;
  442. unsigned long mahr[2], malr[2];
  443. /* save MAHR and MALR */
  444. for (i = 0; i < 2; i++) {
  445. malr[i] = ioread32((void *)GIGA_MALR(i));
  446. mahr[i] = ioread32((void *)GIGA_MAHR(i));
  447. }
  448. /* reset device */
  449. iowrite32(ARSTR_ARSTR, (void *)(SH_GIGA_ETH_BASE + 0x1800));
  450. mdelay(1);
  451. /* restore MAHR and MALR */
  452. for (i = 0; i < 2; i++) {
  453. iowrite32(malr[i], (void *)GIGA_MALR(i));
  454. iowrite32(mahr[i], (void *)GIGA_MAHR(i));
  455. }
  456. }
  457. static void sh_eth_set_rate_giga(struct net_device *ndev)
  458. {
  459. struct sh_eth_private *mdp = netdev_priv(ndev);
  460. switch (mdp->speed) {
  461. case 10: /* 10BASE */
  462. sh_eth_write(ndev, 0x00000000, GECMR);
  463. break;
  464. case 100:/* 100BASE */
  465. sh_eth_write(ndev, 0x00000010, GECMR);
  466. break;
  467. case 1000: /* 1000BASE */
  468. sh_eth_write(ndev, 0x00000020, GECMR);
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. /* SH7757(GETHERC) */
  475. static struct sh_eth_cpu_data sh_eth_my_cpu_data_giga = {
  476. .chip_reset = sh_eth_chip_reset_giga,
  477. .set_duplex = sh_eth_set_duplex,
  478. .set_rate = sh_eth_set_rate_giga,
  479. .ecsr_value = ECSR_ICD | ECSR_MPD,
  480. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  481. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  482. .tx_check = EESR_TC1 | EESR_FTC,
  483. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
  484. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
  485. EESR_ECI,
  486. .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
  487. EESR_TFE,
  488. .fdr_value = 0x0000072f,
  489. .rmcr_value = 0x00000001,
  490. .irq_flags = IRQF_SHARED,
  491. .apr = 1,
  492. .mpr = 1,
  493. .tpauser = 1,
  494. .bculr = 1,
  495. .hw_swap = 1,
  496. .rpadir = 1,
  497. .rpadir_value = 2 << 16,
  498. .no_trimd = 1,
  499. .no_ade = 1,
  500. .tsu = 1,
  501. };
  502. static struct sh_eth_cpu_data *sh_eth_get_cpu_data(struct sh_eth_private *mdp)
  503. {
  504. if (sh_eth_is_gether(mdp))
  505. return &sh_eth_my_cpu_data_giga;
  506. else
  507. return &sh_eth_my_cpu_data;
  508. }
  509. #elif defined(CONFIG_CPU_SUBTYPE_SH7734) || defined(CONFIG_CPU_SUBTYPE_SH7763)
  510. static void sh_eth_chip_reset(struct net_device *ndev)
  511. {
  512. struct sh_eth_private *mdp = netdev_priv(ndev);
  513. /* reset device */
  514. sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
  515. mdelay(1);
  516. }
  517. static void sh_eth_set_rate(struct net_device *ndev)
  518. {
  519. struct sh_eth_private *mdp = netdev_priv(ndev);
  520. switch (mdp->speed) {
  521. case 10: /* 10BASE */
  522. sh_eth_write(ndev, GECMR_10, GECMR);
  523. break;
  524. case 100:/* 100BASE */
  525. sh_eth_write(ndev, GECMR_100, GECMR);
  526. break;
  527. case 1000: /* 1000BASE */
  528. sh_eth_write(ndev, GECMR_1000, GECMR);
  529. break;
  530. default:
  531. break;
  532. }
  533. }
  534. /* sh7763 */
  535. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  536. .chip_reset = sh_eth_chip_reset,
  537. .set_duplex = sh_eth_set_duplex,
  538. .set_rate = sh_eth_set_rate,
  539. .ecsr_value = ECSR_ICD | ECSR_MPD,
  540. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  541. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  542. .tx_check = EESR_TC1 | EESR_FTC,
  543. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
  544. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
  545. EESR_ECI,
  546. .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
  547. EESR_TFE,
  548. .apr = 1,
  549. .mpr = 1,
  550. .tpauser = 1,
  551. .bculr = 1,
  552. .hw_swap = 1,
  553. .no_trimd = 1,
  554. .no_ade = 1,
  555. .tsu = 1,
  556. #if defined(CONFIG_CPU_SUBTYPE_SH7734)
  557. .hw_crc = 1,
  558. .select_mii = 1,
  559. #else
  560. .irq_flags = IRQF_SHARED,
  561. #endif
  562. };
  563. #elif defined(CONFIG_ARCH_R8A7740)
  564. static void sh_eth_chip_reset(struct net_device *ndev)
  565. {
  566. struct sh_eth_private *mdp = netdev_priv(ndev);
  567. /* reset device */
  568. sh_eth_tsu_write(mdp, ARSTR_ARSTR, ARSTR);
  569. mdelay(1);
  570. sh_eth_select_mii(ndev);
  571. }
  572. static void sh_eth_set_rate(struct net_device *ndev)
  573. {
  574. struct sh_eth_private *mdp = netdev_priv(ndev);
  575. switch (mdp->speed) {
  576. case 10: /* 10BASE */
  577. sh_eth_write(ndev, GECMR_10, GECMR);
  578. break;
  579. case 100:/* 100BASE */
  580. sh_eth_write(ndev, GECMR_100, GECMR);
  581. break;
  582. case 1000: /* 1000BASE */
  583. sh_eth_write(ndev, GECMR_1000, GECMR);
  584. break;
  585. default:
  586. break;
  587. }
  588. }
  589. /* R8A7740 */
  590. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  591. .chip_reset = sh_eth_chip_reset,
  592. .set_duplex = sh_eth_set_duplex,
  593. .set_rate = sh_eth_set_rate,
  594. .ecsr_value = ECSR_ICD | ECSR_MPD,
  595. .ecsipr_value = ECSIPR_LCHNGIP | ECSIPR_ICDIP | ECSIPR_MPDIP,
  596. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  597. .tx_check = EESR_TC1 | EESR_FTC,
  598. .eesr_err_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_RABT | \
  599. EESR_RDE | EESR_RFRMER | EESR_TFE | EESR_TDE | \
  600. EESR_ECI,
  601. .tx_error_check = EESR_TWB1 | EESR_TWB | EESR_TABT | EESR_TDE | \
  602. EESR_TFE,
  603. .apr = 1,
  604. .mpr = 1,
  605. .tpauser = 1,
  606. .bculr = 1,
  607. .hw_swap = 1,
  608. .no_trimd = 1,
  609. .no_ade = 1,
  610. .tsu = 1,
  611. .select_mii = 1,
  612. };
  613. #elif defined(CONFIG_CPU_SUBTYPE_SH7619)
  614. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  615. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  616. .apr = 1,
  617. .mpr = 1,
  618. .tpauser = 1,
  619. .hw_swap = 1,
  620. };
  621. #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
  622. static struct sh_eth_cpu_data sh_eth_my_cpu_data = {
  623. .eesipr_value = DMAC_M_RFRMER | DMAC_M_ECI | 0x003fffff,
  624. .tsu = 1,
  625. };
  626. #endif
  627. static void sh_eth_set_default_cpu_data(struct sh_eth_cpu_data *cd)
  628. {
  629. if (!cd->ecsr_value)
  630. cd->ecsr_value = DEFAULT_ECSR_INIT;
  631. if (!cd->ecsipr_value)
  632. cd->ecsipr_value = DEFAULT_ECSIPR_INIT;
  633. if (!cd->fcftr_value)
  634. cd->fcftr_value = DEFAULT_FIFO_F_D_RFF | \
  635. DEFAULT_FIFO_F_D_RFD;
  636. if (!cd->fdr_value)
  637. cd->fdr_value = DEFAULT_FDR_INIT;
  638. if (!cd->rmcr_value)
  639. cd->rmcr_value = DEFAULT_RMCR_VALUE;
  640. if (!cd->tx_check)
  641. cd->tx_check = DEFAULT_TX_CHECK;
  642. if (!cd->eesr_err_check)
  643. cd->eesr_err_check = DEFAULT_EESR_ERR_CHECK;
  644. if (!cd->tx_error_check)
  645. cd->tx_error_check = DEFAULT_TX_ERROR_CHECK;
  646. }
  647. static int sh_eth_check_reset(struct net_device *ndev)
  648. {
  649. int ret = 0;
  650. int cnt = 100;
  651. while (cnt > 0) {
  652. if (!(sh_eth_read(ndev, EDMR) & 0x3))
  653. break;
  654. mdelay(1);
  655. cnt--;
  656. }
  657. if (cnt < 0) {
  658. pr_err("Device reset fail\n");
  659. ret = -ETIMEDOUT;
  660. }
  661. return ret;
  662. }
  663. static int sh_eth_reset(struct net_device *ndev)
  664. {
  665. struct sh_eth_private *mdp = netdev_priv(ndev);
  666. int ret = 0;
  667. if (sh_eth_is_gether(mdp)) {
  668. sh_eth_write(ndev, EDSR_ENALL, EDSR);
  669. sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_GETHER,
  670. EDMR);
  671. ret = sh_eth_check_reset(ndev);
  672. if (ret)
  673. goto out;
  674. /* Table Init */
  675. sh_eth_write(ndev, 0x0, TDLAR);
  676. sh_eth_write(ndev, 0x0, TDFAR);
  677. sh_eth_write(ndev, 0x0, TDFXR);
  678. sh_eth_write(ndev, 0x0, TDFFR);
  679. sh_eth_write(ndev, 0x0, RDLAR);
  680. sh_eth_write(ndev, 0x0, RDFAR);
  681. sh_eth_write(ndev, 0x0, RDFXR);
  682. sh_eth_write(ndev, 0x0, RDFFR);
  683. /* Reset HW CRC register */
  684. if (mdp->cd->hw_crc)
  685. sh_eth_write(ndev, 0x0, CSMR);
  686. /* Select MII mode */
  687. if (mdp->cd->select_mii)
  688. sh_eth_select_mii(ndev);
  689. } else {
  690. sh_eth_write(ndev, sh_eth_read(ndev, EDMR) | EDMR_SRST_ETHER,
  691. EDMR);
  692. mdelay(3);
  693. sh_eth_write(ndev, sh_eth_read(ndev, EDMR) & ~EDMR_SRST_ETHER,
  694. EDMR);
  695. }
  696. out:
  697. return ret;
  698. }
  699. #if defined(CONFIG_CPU_SH4) || defined(CONFIG_ARCH_SHMOBILE)
  700. static void sh_eth_set_receive_align(struct sk_buff *skb)
  701. {
  702. int reserve;
  703. reserve = SH4_SKB_RX_ALIGN - ((u32)skb->data & (SH4_SKB_RX_ALIGN - 1));
  704. if (reserve)
  705. skb_reserve(skb, reserve);
  706. }
  707. #else
  708. static void sh_eth_set_receive_align(struct sk_buff *skb)
  709. {
  710. skb_reserve(skb, SH2_SH3_SKB_RX_ALIGN);
  711. }
  712. #endif
  713. /* CPU <-> EDMAC endian convert */
  714. static inline __u32 cpu_to_edmac(struct sh_eth_private *mdp, u32 x)
  715. {
  716. switch (mdp->edmac_endian) {
  717. case EDMAC_LITTLE_ENDIAN:
  718. return cpu_to_le32(x);
  719. case EDMAC_BIG_ENDIAN:
  720. return cpu_to_be32(x);
  721. }
  722. return x;
  723. }
  724. static inline __u32 edmac_to_cpu(struct sh_eth_private *mdp, u32 x)
  725. {
  726. switch (mdp->edmac_endian) {
  727. case EDMAC_LITTLE_ENDIAN:
  728. return le32_to_cpu(x);
  729. case EDMAC_BIG_ENDIAN:
  730. return be32_to_cpu(x);
  731. }
  732. return x;
  733. }
  734. /*
  735. * Program the hardware MAC address from dev->dev_addr.
  736. */
  737. static void update_mac_address(struct net_device *ndev)
  738. {
  739. sh_eth_write(ndev,
  740. (ndev->dev_addr[0] << 24) | (ndev->dev_addr[1] << 16) |
  741. (ndev->dev_addr[2] << 8) | (ndev->dev_addr[3]), MAHR);
  742. sh_eth_write(ndev,
  743. (ndev->dev_addr[4] << 8) | (ndev->dev_addr[5]), MALR);
  744. }
  745. /*
  746. * Get MAC address from SuperH MAC address register
  747. *
  748. * SuperH's Ethernet device doesn't have 'ROM' to MAC address.
  749. * This driver get MAC address that use by bootloader(U-boot or sh-ipl+g).
  750. * When you want use this device, you must set MAC address in bootloader.
  751. *
  752. */
  753. static void read_mac_address(struct net_device *ndev, unsigned char *mac)
  754. {
  755. if (mac[0] || mac[1] || mac[2] || mac[3] || mac[4] || mac[5]) {
  756. memcpy(ndev->dev_addr, mac, 6);
  757. } else {
  758. ndev->dev_addr[0] = (sh_eth_read(ndev, MAHR) >> 24);
  759. ndev->dev_addr[1] = (sh_eth_read(ndev, MAHR) >> 16) & 0xFF;
  760. ndev->dev_addr[2] = (sh_eth_read(ndev, MAHR) >> 8) & 0xFF;
  761. ndev->dev_addr[3] = (sh_eth_read(ndev, MAHR) & 0xFF);
  762. ndev->dev_addr[4] = (sh_eth_read(ndev, MALR) >> 8) & 0xFF;
  763. ndev->dev_addr[5] = (sh_eth_read(ndev, MALR) & 0xFF);
  764. }
  765. }
  766. static unsigned long sh_eth_get_edtrr_trns(struct sh_eth_private *mdp)
  767. {
  768. if (sh_eth_is_gether(mdp))
  769. return EDTRR_TRNS_GETHER;
  770. else
  771. return EDTRR_TRNS_ETHER;
  772. }
  773. struct bb_info {
  774. void (*set_gate)(void *addr);
  775. struct mdiobb_ctrl ctrl;
  776. void *addr;
  777. u32 mmd_msk;/* MMD */
  778. u32 mdo_msk;
  779. u32 mdi_msk;
  780. u32 mdc_msk;
  781. };
  782. /* PHY bit set */
  783. static void bb_set(void *addr, u32 msk)
  784. {
  785. iowrite32(ioread32(addr) | msk, addr);
  786. }
  787. /* PHY bit clear */
  788. static void bb_clr(void *addr, u32 msk)
  789. {
  790. iowrite32((ioread32(addr) & ~msk), addr);
  791. }
  792. /* PHY bit read */
  793. static int bb_read(void *addr, u32 msk)
  794. {
  795. return (ioread32(addr) & msk) != 0;
  796. }
  797. /* Data I/O pin control */
  798. static void sh_mmd_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  799. {
  800. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  801. if (bitbang->set_gate)
  802. bitbang->set_gate(bitbang->addr);
  803. if (bit)
  804. bb_set(bitbang->addr, bitbang->mmd_msk);
  805. else
  806. bb_clr(bitbang->addr, bitbang->mmd_msk);
  807. }
  808. /* Set bit data*/
  809. static void sh_set_mdio(struct mdiobb_ctrl *ctrl, int bit)
  810. {
  811. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  812. if (bitbang->set_gate)
  813. bitbang->set_gate(bitbang->addr);
  814. if (bit)
  815. bb_set(bitbang->addr, bitbang->mdo_msk);
  816. else
  817. bb_clr(bitbang->addr, bitbang->mdo_msk);
  818. }
  819. /* Get bit data*/
  820. static int sh_get_mdio(struct mdiobb_ctrl *ctrl)
  821. {
  822. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  823. if (bitbang->set_gate)
  824. bitbang->set_gate(bitbang->addr);
  825. return bb_read(bitbang->addr, bitbang->mdi_msk);
  826. }
  827. /* MDC pin control */
  828. static void sh_mdc_ctrl(struct mdiobb_ctrl *ctrl, int bit)
  829. {
  830. struct bb_info *bitbang = container_of(ctrl, struct bb_info, ctrl);
  831. if (bitbang->set_gate)
  832. bitbang->set_gate(bitbang->addr);
  833. if (bit)
  834. bb_set(bitbang->addr, bitbang->mdc_msk);
  835. else
  836. bb_clr(bitbang->addr, bitbang->mdc_msk);
  837. }
  838. /* mdio bus control struct */
  839. static struct mdiobb_ops bb_ops = {
  840. .owner = THIS_MODULE,
  841. .set_mdc = sh_mdc_ctrl,
  842. .set_mdio_dir = sh_mmd_ctrl,
  843. .set_mdio_data = sh_set_mdio,
  844. .get_mdio_data = sh_get_mdio,
  845. };
  846. /* free skb and descriptor buffer */
  847. static void sh_eth_ring_free(struct net_device *ndev)
  848. {
  849. struct sh_eth_private *mdp = netdev_priv(ndev);
  850. int i;
  851. /* Free Rx skb ringbuffer */
  852. if (mdp->rx_skbuff) {
  853. for (i = 0; i < mdp->num_rx_ring; i++) {
  854. if (mdp->rx_skbuff[i])
  855. dev_kfree_skb(mdp->rx_skbuff[i]);
  856. }
  857. }
  858. kfree(mdp->rx_skbuff);
  859. mdp->rx_skbuff = NULL;
  860. /* Free Tx skb ringbuffer */
  861. if (mdp->tx_skbuff) {
  862. for (i = 0; i < mdp->num_tx_ring; i++) {
  863. if (mdp->tx_skbuff[i])
  864. dev_kfree_skb(mdp->tx_skbuff[i]);
  865. }
  866. }
  867. kfree(mdp->tx_skbuff);
  868. mdp->tx_skbuff = NULL;
  869. }
  870. /* format skb and descriptor buffer */
  871. static void sh_eth_ring_format(struct net_device *ndev)
  872. {
  873. struct sh_eth_private *mdp = netdev_priv(ndev);
  874. int i;
  875. struct sk_buff *skb;
  876. struct sh_eth_rxdesc *rxdesc = NULL;
  877. struct sh_eth_txdesc *txdesc = NULL;
  878. int rx_ringsize = sizeof(*rxdesc) * mdp->num_rx_ring;
  879. int tx_ringsize = sizeof(*txdesc) * mdp->num_tx_ring;
  880. mdp->cur_rx = mdp->cur_tx = 0;
  881. mdp->dirty_rx = mdp->dirty_tx = 0;
  882. memset(mdp->rx_ring, 0, rx_ringsize);
  883. /* build Rx ring buffer */
  884. for (i = 0; i < mdp->num_rx_ring; i++) {
  885. /* skb */
  886. mdp->rx_skbuff[i] = NULL;
  887. skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
  888. mdp->rx_skbuff[i] = skb;
  889. if (skb == NULL)
  890. break;
  891. dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
  892. DMA_FROM_DEVICE);
  893. sh_eth_set_receive_align(skb);
  894. /* RX descriptor */
  895. rxdesc = &mdp->rx_ring[i];
  896. rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
  897. rxdesc->status = cpu_to_edmac(mdp, RD_RACT | RD_RFP);
  898. /* The size of the buffer is 16 byte boundary. */
  899. rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
  900. /* Rx descriptor address set */
  901. if (i == 0) {
  902. sh_eth_write(ndev, mdp->rx_desc_dma, RDLAR);
  903. if (sh_eth_is_gether(mdp))
  904. sh_eth_write(ndev, mdp->rx_desc_dma, RDFAR);
  905. }
  906. }
  907. mdp->dirty_rx = (u32) (i - mdp->num_rx_ring);
  908. /* Mark the last entry as wrapping the ring. */
  909. rxdesc->status |= cpu_to_edmac(mdp, RD_RDEL);
  910. memset(mdp->tx_ring, 0, tx_ringsize);
  911. /* build Tx ring buffer */
  912. for (i = 0; i < mdp->num_tx_ring; i++) {
  913. mdp->tx_skbuff[i] = NULL;
  914. txdesc = &mdp->tx_ring[i];
  915. txdesc->status = cpu_to_edmac(mdp, TD_TFP);
  916. txdesc->buffer_length = 0;
  917. if (i == 0) {
  918. /* Tx descriptor address set */
  919. sh_eth_write(ndev, mdp->tx_desc_dma, TDLAR);
  920. if (sh_eth_is_gether(mdp))
  921. sh_eth_write(ndev, mdp->tx_desc_dma, TDFAR);
  922. }
  923. }
  924. txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
  925. }
  926. /* Get skb and descriptor buffer */
  927. static int sh_eth_ring_init(struct net_device *ndev)
  928. {
  929. struct sh_eth_private *mdp = netdev_priv(ndev);
  930. int rx_ringsize, tx_ringsize, ret = 0;
  931. /*
  932. * +26 gets the maximum ethernet encapsulation, +7 & ~7 because the
  933. * card needs room to do 8 byte alignment, +2 so we can reserve
  934. * the first 2 bytes, and +16 gets room for the status word from the
  935. * card.
  936. */
  937. mdp->rx_buf_sz = (ndev->mtu <= 1492 ? PKT_BUF_SZ :
  938. (((ndev->mtu + 26 + 7) & ~7) + 2 + 16));
  939. if (mdp->cd->rpadir)
  940. mdp->rx_buf_sz += NET_IP_ALIGN;
  941. /* Allocate RX and TX skb rings */
  942. mdp->rx_skbuff = kmalloc_array(mdp->num_rx_ring,
  943. sizeof(*mdp->rx_skbuff), GFP_KERNEL);
  944. if (!mdp->rx_skbuff) {
  945. ret = -ENOMEM;
  946. return ret;
  947. }
  948. mdp->tx_skbuff = kmalloc_array(mdp->num_tx_ring,
  949. sizeof(*mdp->tx_skbuff), GFP_KERNEL);
  950. if (!mdp->tx_skbuff) {
  951. ret = -ENOMEM;
  952. goto skb_ring_free;
  953. }
  954. /* Allocate all Rx descriptors. */
  955. rx_ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  956. mdp->rx_ring = dma_alloc_coherent(NULL, rx_ringsize, &mdp->rx_desc_dma,
  957. GFP_KERNEL);
  958. if (!mdp->rx_ring) {
  959. ret = -ENOMEM;
  960. goto desc_ring_free;
  961. }
  962. mdp->dirty_rx = 0;
  963. /* Allocate all Tx descriptors. */
  964. tx_ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  965. mdp->tx_ring = dma_alloc_coherent(NULL, tx_ringsize, &mdp->tx_desc_dma,
  966. GFP_KERNEL);
  967. if (!mdp->tx_ring) {
  968. ret = -ENOMEM;
  969. goto desc_ring_free;
  970. }
  971. return ret;
  972. desc_ring_free:
  973. /* free DMA buffer */
  974. dma_free_coherent(NULL, rx_ringsize, mdp->rx_ring, mdp->rx_desc_dma);
  975. skb_ring_free:
  976. /* Free Rx and Tx skb ring buffer */
  977. sh_eth_ring_free(ndev);
  978. mdp->tx_ring = NULL;
  979. mdp->rx_ring = NULL;
  980. return ret;
  981. }
  982. static void sh_eth_free_dma_buffer(struct sh_eth_private *mdp)
  983. {
  984. int ringsize;
  985. if (mdp->rx_ring) {
  986. ringsize = sizeof(struct sh_eth_rxdesc) * mdp->num_rx_ring;
  987. dma_free_coherent(NULL, ringsize, mdp->rx_ring,
  988. mdp->rx_desc_dma);
  989. mdp->rx_ring = NULL;
  990. }
  991. if (mdp->tx_ring) {
  992. ringsize = sizeof(struct sh_eth_txdesc) * mdp->num_tx_ring;
  993. dma_free_coherent(NULL, ringsize, mdp->tx_ring,
  994. mdp->tx_desc_dma);
  995. mdp->tx_ring = NULL;
  996. }
  997. }
  998. static int sh_eth_dev_init(struct net_device *ndev, bool start)
  999. {
  1000. int ret = 0;
  1001. struct sh_eth_private *mdp = netdev_priv(ndev);
  1002. u32 val;
  1003. /* Soft Reset */
  1004. ret = sh_eth_reset(ndev);
  1005. if (ret)
  1006. goto out;
  1007. /* Descriptor format */
  1008. sh_eth_ring_format(ndev);
  1009. if (mdp->cd->rpadir)
  1010. sh_eth_write(ndev, mdp->cd->rpadir_value, RPADIR);
  1011. /* all sh_eth int mask */
  1012. sh_eth_write(ndev, 0, EESIPR);
  1013. #if defined(__LITTLE_ENDIAN)
  1014. if (mdp->cd->hw_swap)
  1015. sh_eth_write(ndev, EDMR_EL, EDMR);
  1016. else
  1017. #endif
  1018. sh_eth_write(ndev, 0, EDMR);
  1019. /* FIFO size set */
  1020. sh_eth_write(ndev, mdp->cd->fdr_value, FDR);
  1021. sh_eth_write(ndev, 0, TFTR);
  1022. /* Frame recv control */
  1023. sh_eth_write(ndev, mdp->cd->rmcr_value, RMCR);
  1024. sh_eth_write(ndev, DESC_I_RINT8 | DESC_I_RINT5 | DESC_I_TINT2, TRSCER);
  1025. if (mdp->cd->bculr)
  1026. sh_eth_write(ndev, 0x800, BCULR); /* Burst sycle set */
  1027. sh_eth_write(ndev, mdp->cd->fcftr_value, FCFTR);
  1028. if (!mdp->cd->no_trimd)
  1029. sh_eth_write(ndev, 0, TRIMD);
  1030. /* Recv frame limit set register */
  1031. sh_eth_write(ndev, ndev->mtu + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN,
  1032. RFLR);
  1033. sh_eth_write(ndev, sh_eth_read(ndev, EESR), EESR);
  1034. if (start)
  1035. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1036. /* PAUSE Prohibition */
  1037. val = (sh_eth_read(ndev, ECMR) & ECMR_DM) |
  1038. ECMR_ZPF | (mdp->duplex ? ECMR_DM : 0) | ECMR_TE | ECMR_RE;
  1039. sh_eth_write(ndev, val, ECMR);
  1040. if (mdp->cd->set_rate)
  1041. mdp->cd->set_rate(ndev);
  1042. /* E-MAC Status Register clear */
  1043. sh_eth_write(ndev, mdp->cd->ecsr_value, ECSR);
  1044. /* E-MAC Interrupt Enable register */
  1045. if (start)
  1046. sh_eth_write(ndev, mdp->cd->ecsipr_value, ECSIPR);
  1047. /* Set MAC address */
  1048. update_mac_address(ndev);
  1049. /* mask reset */
  1050. if (mdp->cd->apr)
  1051. sh_eth_write(ndev, APR_AP, APR);
  1052. if (mdp->cd->mpr)
  1053. sh_eth_write(ndev, MPR_MP, MPR);
  1054. if (mdp->cd->tpauser)
  1055. sh_eth_write(ndev, TPAUSER_UNLIMITED, TPAUSER);
  1056. if (start) {
  1057. /* Setting the Rx mode will start the Rx process. */
  1058. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1059. netif_start_queue(ndev);
  1060. }
  1061. out:
  1062. return ret;
  1063. }
  1064. /* free Tx skb function */
  1065. static int sh_eth_txfree(struct net_device *ndev)
  1066. {
  1067. struct sh_eth_private *mdp = netdev_priv(ndev);
  1068. struct sh_eth_txdesc *txdesc;
  1069. int freeNum = 0;
  1070. int entry = 0;
  1071. for (; mdp->cur_tx - mdp->dirty_tx > 0; mdp->dirty_tx++) {
  1072. entry = mdp->dirty_tx % mdp->num_tx_ring;
  1073. txdesc = &mdp->tx_ring[entry];
  1074. if (txdesc->status & cpu_to_edmac(mdp, TD_TACT))
  1075. break;
  1076. /* Free the original skb. */
  1077. if (mdp->tx_skbuff[entry]) {
  1078. dma_unmap_single(&ndev->dev, txdesc->addr,
  1079. txdesc->buffer_length, DMA_TO_DEVICE);
  1080. dev_kfree_skb_irq(mdp->tx_skbuff[entry]);
  1081. mdp->tx_skbuff[entry] = NULL;
  1082. freeNum++;
  1083. }
  1084. txdesc->status = cpu_to_edmac(mdp, TD_TFP);
  1085. if (entry >= mdp->num_tx_ring - 1)
  1086. txdesc->status |= cpu_to_edmac(mdp, TD_TDLE);
  1087. ndev->stats.tx_packets++;
  1088. ndev->stats.tx_bytes += txdesc->buffer_length;
  1089. }
  1090. return freeNum;
  1091. }
  1092. /* Packet receive function */
  1093. static int sh_eth_rx(struct net_device *ndev, u32 intr_status)
  1094. {
  1095. struct sh_eth_private *mdp = netdev_priv(ndev);
  1096. struct sh_eth_rxdesc *rxdesc;
  1097. int entry = mdp->cur_rx % mdp->num_rx_ring;
  1098. int boguscnt = (mdp->dirty_rx + mdp->num_rx_ring) - mdp->cur_rx;
  1099. struct sk_buff *skb;
  1100. u16 pkt_len = 0;
  1101. u32 desc_status;
  1102. rxdesc = &mdp->rx_ring[entry];
  1103. while (!(rxdesc->status & cpu_to_edmac(mdp, RD_RACT))) {
  1104. desc_status = edmac_to_cpu(mdp, rxdesc->status);
  1105. pkt_len = rxdesc->frame_length;
  1106. #if defined(CONFIG_ARCH_R8A7740)
  1107. desc_status >>= 16;
  1108. #endif
  1109. if (--boguscnt < 0)
  1110. break;
  1111. if (!(desc_status & RDFEND))
  1112. ndev->stats.rx_length_errors++;
  1113. if (desc_status & (RD_RFS1 | RD_RFS2 | RD_RFS3 | RD_RFS4 |
  1114. RD_RFS5 | RD_RFS6 | RD_RFS10)) {
  1115. ndev->stats.rx_errors++;
  1116. if (desc_status & RD_RFS1)
  1117. ndev->stats.rx_crc_errors++;
  1118. if (desc_status & RD_RFS2)
  1119. ndev->stats.rx_frame_errors++;
  1120. if (desc_status & RD_RFS3)
  1121. ndev->stats.rx_length_errors++;
  1122. if (desc_status & RD_RFS4)
  1123. ndev->stats.rx_length_errors++;
  1124. if (desc_status & RD_RFS6)
  1125. ndev->stats.rx_missed_errors++;
  1126. if (desc_status & RD_RFS10)
  1127. ndev->stats.rx_over_errors++;
  1128. } else {
  1129. if (!mdp->cd->hw_swap)
  1130. sh_eth_soft_swap(
  1131. phys_to_virt(ALIGN(rxdesc->addr, 4)),
  1132. pkt_len + 2);
  1133. skb = mdp->rx_skbuff[entry];
  1134. mdp->rx_skbuff[entry] = NULL;
  1135. if (mdp->cd->rpadir)
  1136. skb_reserve(skb, NET_IP_ALIGN);
  1137. skb_put(skb, pkt_len);
  1138. skb->protocol = eth_type_trans(skb, ndev);
  1139. netif_rx(skb);
  1140. ndev->stats.rx_packets++;
  1141. ndev->stats.rx_bytes += pkt_len;
  1142. }
  1143. rxdesc->status |= cpu_to_edmac(mdp, RD_RACT);
  1144. entry = (++mdp->cur_rx) % mdp->num_rx_ring;
  1145. rxdesc = &mdp->rx_ring[entry];
  1146. }
  1147. /* Refill the Rx ring buffers. */
  1148. for (; mdp->cur_rx - mdp->dirty_rx > 0; mdp->dirty_rx++) {
  1149. entry = mdp->dirty_rx % mdp->num_rx_ring;
  1150. rxdesc = &mdp->rx_ring[entry];
  1151. /* The size of the buffer is 16 byte boundary. */
  1152. rxdesc->buffer_length = ALIGN(mdp->rx_buf_sz, 16);
  1153. if (mdp->rx_skbuff[entry] == NULL) {
  1154. skb = netdev_alloc_skb(ndev, mdp->rx_buf_sz);
  1155. mdp->rx_skbuff[entry] = skb;
  1156. if (skb == NULL)
  1157. break; /* Better luck next round. */
  1158. dma_map_single(&ndev->dev, skb->data, mdp->rx_buf_sz,
  1159. DMA_FROM_DEVICE);
  1160. sh_eth_set_receive_align(skb);
  1161. skb_checksum_none_assert(skb);
  1162. rxdesc->addr = virt_to_phys(PTR_ALIGN(skb->data, 4));
  1163. }
  1164. if (entry >= mdp->num_rx_ring - 1)
  1165. rxdesc->status |=
  1166. cpu_to_edmac(mdp, RD_RACT | RD_RFP | RD_RDEL);
  1167. else
  1168. rxdesc->status |=
  1169. cpu_to_edmac(mdp, RD_RACT | RD_RFP);
  1170. }
  1171. /* Restart Rx engine if stopped. */
  1172. /* If we don't need to check status, don't. -KDU */
  1173. if (!(sh_eth_read(ndev, EDRRR) & EDRRR_R)) {
  1174. /* fix the values for the next receiving if RDE is set */
  1175. if (intr_status & EESR_RDE)
  1176. mdp->cur_rx = mdp->dirty_rx =
  1177. (sh_eth_read(ndev, RDFAR) -
  1178. sh_eth_read(ndev, RDLAR)) >> 4;
  1179. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1180. }
  1181. return 0;
  1182. }
  1183. static void sh_eth_rcv_snd_disable(struct net_device *ndev)
  1184. {
  1185. /* disable tx and rx */
  1186. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) &
  1187. ~(ECMR_RE | ECMR_TE), ECMR);
  1188. }
  1189. static void sh_eth_rcv_snd_enable(struct net_device *ndev)
  1190. {
  1191. /* enable tx and rx */
  1192. sh_eth_write(ndev, sh_eth_read(ndev, ECMR) |
  1193. (ECMR_RE | ECMR_TE), ECMR);
  1194. }
  1195. /* error control function */
  1196. static void sh_eth_error(struct net_device *ndev, int intr_status)
  1197. {
  1198. struct sh_eth_private *mdp = netdev_priv(ndev);
  1199. u32 felic_stat;
  1200. u32 link_stat;
  1201. u32 mask;
  1202. if (intr_status & EESR_ECI) {
  1203. felic_stat = sh_eth_read(ndev, ECSR);
  1204. sh_eth_write(ndev, felic_stat, ECSR); /* clear int */
  1205. if (felic_stat & ECSR_ICD)
  1206. ndev->stats.tx_carrier_errors++;
  1207. if (felic_stat & ECSR_LCHNG) {
  1208. /* Link Changed */
  1209. if (mdp->cd->no_psr || mdp->no_ether_link) {
  1210. goto ignore_link;
  1211. } else {
  1212. link_stat = (sh_eth_read(ndev, PSR));
  1213. if (mdp->ether_link_active_low)
  1214. link_stat = ~link_stat;
  1215. }
  1216. if (!(link_stat & PHY_ST_LINK))
  1217. sh_eth_rcv_snd_disable(ndev);
  1218. else {
  1219. /* Link Up */
  1220. sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) &
  1221. ~DMAC_M_ECI, EESIPR);
  1222. /*clear int */
  1223. sh_eth_write(ndev, sh_eth_read(ndev, ECSR),
  1224. ECSR);
  1225. sh_eth_write(ndev, sh_eth_read(ndev, EESIPR) |
  1226. DMAC_M_ECI, EESIPR);
  1227. /* enable tx and rx */
  1228. sh_eth_rcv_snd_enable(ndev);
  1229. }
  1230. }
  1231. }
  1232. ignore_link:
  1233. if (intr_status & EESR_TWB) {
  1234. /* Write buck end. unused write back interrupt */
  1235. if (intr_status & EESR_TABT) /* Transmit Abort int */
  1236. ndev->stats.tx_aborted_errors++;
  1237. if (netif_msg_tx_err(mdp))
  1238. dev_err(&ndev->dev, "Transmit Abort\n");
  1239. }
  1240. if (intr_status & EESR_RABT) {
  1241. /* Receive Abort int */
  1242. if (intr_status & EESR_RFRMER) {
  1243. /* Receive Frame Overflow int */
  1244. ndev->stats.rx_frame_errors++;
  1245. if (netif_msg_rx_err(mdp))
  1246. dev_err(&ndev->dev, "Receive Abort\n");
  1247. }
  1248. }
  1249. if (intr_status & EESR_TDE) {
  1250. /* Transmit Descriptor Empty int */
  1251. ndev->stats.tx_fifo_errors++;
  1252. if (netif_msg_tx_err(mdp))
  1253. dev_err(&ndev->dev, "Transmit Descriptor Empty\n");
  1254. }
  1255. if (intr_status & EESR_TFE) {
  1256. /* FIFO under flow */
  1257. ndev->stats.tx_fifo_errors++;
  1258. if (netif_msg_tx_err(mdp))
  1259. dev_err(&ndev->dev, "Transmit FIFO Under flow\n");
  1260. }
  1261. if (intr_status & EESR_RDE) {
  1262. /* Receive Descriptor Empty int */
  1263. ndev->stats.rx_over_errors++;
  1264. if (netif_msg_rx_err(mdp))
  1265. dev_err(&ndev->dev, "Receive Descriptor Empty\n");
  1266. }
  1267. if (intr_status & EESR_RFE) {
  1268. /* Receive FIFO Overflow int */
  1269. ndev->stats.rx_fifo_errors++;
  1270. if (netif_msg_rx_err(mdp))
  1271. dev_err(&ndev->dev, "Receive FIFO Overflow\n");
  1272. }
  1273. if (!mdp->cd->no_ade && (intr_status & EESR_ADE)) {
  1274. /* Address Error */
  1275. ndev->stats.tx_fifo_errors++;
  1276. if (netif_msg_tx_err(mdp))
  1277. dev_err(&ndev->dev, "Address Error\n");
  1278. }
  1279. mask = EESR_TWB | EESR_TABT | EESR_ADE | EESR_TDE | EESR_TFE;
  1280. if (mdp->cd->no_ade)
  1281. mask &= ~EESR_ADE;
  1282. if (intr_status & mask) {
  1283. /* Tx error */
  1284. u32 edtrr = sh_eth_read(ndev, EDTRR);
  1285. /* dmesg */
  1286. dev_err(&ndev->dev, "TX error. status=%8.8x cur_tx=%8.8x ",
  1287. intr_status, mdp->cur_tx);
  1288. dev_err(&ndev->dev, "dirty_tx=%8.8x state=%8.8x EDTRR=%8.8x.\n",
  1289. mdp->dirty_tx, (u32) ndev->state, edtrr);
  1290. /* dirty buffer free */
  1291. sh_eth_txfree(ndev);
  1292. /* SH7712 BUG */
  1293. if (edtrr ^ sh_eth_get_edtrr_trns(mdp)) {
  1294. /* tx dma start */
  1295. sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
  1296. }
  1297. /* wakeup */
  1298. netif_wake_queue(ndev);
  1299. }
  1300. }
  1301. static irqreturn_t sh_eth_interrupt(int irq, void *netdev)
  1302. {
  1303. struct net_device *ndev = netdev;
  1304. struct sh_eth_private *mdp = netdev_priv(ndev);
  1305. struct sh_eth_cpu_data *cd = mdp->cd;
  1306. irqreturn_t ret = IRQ_NONE;
  1307. unsigned long intr_status;
  1308. spin_lock(&mdp->lock);
  1309. /* Get interrupt status */
  1310. intr_status = sh_eth_read(ndev, EESR);
  1311. /* Mask it with the interrupt mask, forcing ECI interrupt to be always
  1312. * enabled since it's the one that comes thru regardless of the mask,
  1313. * and we need to fully handle it in sh_eth_error() in order to quench
  1314. * it as it doesn't get cleared by just writing 1 to the ECI bit...
  1315. */
  1316. intr_status &= sh_eth_read(ndev, EESIPR) | DMAC_M_ECI;
  1317. /* Clear interrupt */
  1318. if (intr_status & (EESR_FRC | EESR_RMAF | EESR_RRF |
  1319. EESR_RTLF | EESR_RTSF | EESR_PRE | EESR_CERF |
  1320. cd->tx_check | cd->eesr_err_check)) {
  1321. sh_eth_write(ndev, intr_status, EESR);
  1322. ret = IRQ_HANDLED;
  1323. } else
  1324. goto other_irq;
  1325. if (intr_status & (EESR_FRC | /* Frame recv*/
  1326. EESR_RMAF | /* Multi cast address recv*/
  1327. EESR_RRF | /* Bit frame recv */
  1328. EESR_RTLF | /* Long frame recv*/
  1329. EESR_RTSF | /* short frame recv */
  1330. EESR_PRE | /* PHY-LSI recv error */
  1331. EESR_CERF)){ /* recv frame CRC error */
  1332. sh_eth_rx(ndev, intr_status);
  1333. }
  1334. /* Tx Check */
  1335. if (intr_status & cd->tx_check) {
  1336. sh_eth_txfree(ndev);
  1337. netif_wake_queue(ndev);
  1338. }
  1339. if (intr_status & cd->eesr_err_check)
  1340. sh_eth_error(ndev, intr_status);
  1341. other_irq:
  1342. spin_unlock(&mdp->lock);
  1343. return ret;
  1344. }
  1345. /* PHY state control function */
  1346. static void sh_eth_adjust_link(struct net_device *ndev)
  1347. {
  1348. struct sh_eth_private *mdp = netdev_priv(ndev);
  1349. struct phy_device *phydev = mdp->phydev;
  1350. int new_state = 0;
  1351. if (phydev->link) {
  1352. if (phydev->duplex != mdp->duplex) {
  1353. new_state = 1;
  1354. mdp->duplex = phydev->duplex;
  1355. if (mdp->cd->set_duplex)
  1356. mdp->cd->set_duplex(ndev);
  1357. }
  1358. if (phydev->speed != mdp->speed) {
  1359. new_state = 1;
  1360. mdp->speed = phydev->speed;
  1361. if (mdp->cd->set_rate)
  1362. mdp->cd->set_rate(ndev);
  1363. }
  1364. if (!mdp->link) {
  1365. sh_eth_write(ndev,
  1366. (sh_eth_read(ndev, ECMR) & ~ECMR_TXF), ECMR);
  1367. new_state = 1;
  1368. mdp->link = phydev->link;
  1369. if (mdp->cd->no_psr || mdp->no_ether_link)
  1370. sh_eth_rcv_snd_enable(ndev);
  1371. }
  1372. } else if (mdp->link) {
  1373. new_state = 1;
  1374. mdp->link = 0;
  1375. mdp->speed = 0;
  1376. mdp->duplex = -1;
  1377. if (mdp->cd->no_psr || mdp->no_ether_link)
  1378. sh_eth_rcv_snd_disable(ndev);
  1379. }
  1380. if (new_state && netif_msg_link(mdp))
  1381. phy_print_status(phydev);
  1382. }
  1383. /* PHY init function */
  1384. static int sh_eth_phy_init(struct net_device *ndev)
  1385. {
  1386. struct sh_eth_private *mdp = netdev_priv(ndev);
  1387. char phy_id[MII_BUS_ID_SIZE + 3];
  1388. struct phy_device *phydev = NULL;
  1389. snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
  1390. mdp->mii_bus->id , mdp->phy_id);
  1391. mdp->link = 0;
  1392. mdp->speed = 0;
  1393. mdp->duplex = -1;
  1394. /* Try connect to PHY */
  1395. phydev = phy_connect(ndev, phy_id, sh_eth_adjust_link,
  1396. mdp->phy_interface);
  1397. if (IS_ERR(phydev)) {
  1398. dev_err(&ndev->dev, "phy_connect failed\n");
  1399. return PTR_ERR(phydev);
  1400. }
  1401. dev_info(&ndev->dev, "attached phy %i to driver %s\n",
  1402. phydev->addr, phydev->drv->name);
  1403. mdp->phydev = phydev;
  1404. return 0;
  1405. }
  1406. /* PHY control start function */
  1407. static int sh_eth_phy_start(struct net_device *ndev)
  1408. {
  1409. struct sh_eth_private *mdp = netdev_priv(ndev);
  1410. int ret;
  1411. ret = sh_eth_phy_init(ndev);
  1412. if (ret)
  1413. return ret;
  1414. /* reset phy - this also wakes it from PDOWN */
  1415. phy_write(mdp->phydev, MII_BMCR, BMCR_RESET);
  1416. phy_start(mdp->phydev);
  1417. return 0;
  1418. }
  1419. static int sh_eth_get_settings(struct net_device *ndev,
  1420. struct ethtool_cmd *ecmd)
  1421. {
  1422. struct sh_eth_private *mdp = netdev_priv(ndev);
  1423. unsigned long flags;
  1424. int ret;
  1425. spin_lock_irqsave(&mdp->lock, flags);
  1426. ret = phy_ethtool_gset(mdp->phydev, ecmd);
  1427. spin_unlock_irqrestore(&mdp->lock, flags);
  1428. return ret;
  1429. }
  1430. static int sh_eth_set_settings(struct net_device *ndev,
  1431. struct ethtool_cmd *ecmd)
  1432. {
  1433. struct sh_eth_private *mdp = netdev_priv(ndev);
  1434. unsigned long flags;
  1435. int ret;
  1436. spin_lock_irqsave(&mdp->lock, flags);
  1437. /* disable tx and rx */
  1438. sh_eth_rcv_snd_disable(ndev);
  1439. ret = phy_ethtool_sset(mdp->phydev, ecmd);
  1440. if (ret)
  1441. goto error_exit;
  1442. if (ecmd->duplex == DUPLEX_FULL)
  1443. mdp->duplex = 1;
  1444. else
  1445. mdp->duplex = 0;
  1446. if (mdp->cd->set_duplex)
  1447. mdp->cd->set_duplex(ndev);
  1448. error_exit:
  1449. mdelay(1);
  1450. /* enable tx and rx */
  1451. sh_eth_rcv_snd_enable(ndev);
  1452. spin_unlock_irqrestore(&mdp->lock, flags);
  1453. return ret;
  1454. }
  1455. static int sh_eth_nway_reset(struct net_device *ndev)
  1456. {
  1457. struct sh_eth_private *mdp = netdev_priv(ndev);
  1458. unsigned long flags;
  1459. int ret;
  1460. spin_lock_irqsave(&mdp->lock, flags);
  1461. ret = phy_start_aneg(mdp->phydev);
  1462. spin_unlock_irqrestore(&mdp->lock, flags);
  1463. return ret;
  1464. }
  1465. static u32 sh_eth_get_msglevel(struct net_device *ndev)
  1466. {
  1467. struct sh_eth_private *mdp = netdev_priv(ndev);
  1468. return mdp->msg_enable;
  1469. }
  1470. static void sh_eth_set_msglevel(struct net_device *ndev, u32 value)
  1471. {
  1472. struct sh_eth_private *mdp = netdev_priv(ndev);
  1473. mdp->msg_enable = value;
  1474. }
  1475. static const char sh_eth_gstrings_stats[][ETH_GSTRING_LEN] = {
  1476. "rx_current", "tx_current",
  1477. "rx_dirty", "tx_dirty",
  1478. };
  1479. #define SH_ETH_STATS_LEN ARRAY_SIZE(sh_eth_gstrings_stats)
  1480. static int sh_eth_get_sset_count(struct net_device *netdev, int sset)
  1481. {
  1482. switch (sset) {
  1483. case ETH_SS_STATS:
  1484. return SH_ETH_STATS_LEN;
  1485. default:
  1486. return -EOPNOTSUPP;
  1487. }
  1488. }
  1489. static void sh_eth_get_ethtool_stats(struct net_device *ndev,
  1490. struct ethtool_stats *stats, u64 *data)
  1491. {
  1492. struct sh_eth_private *mdp = netdev_priv(ndev);
  1493. int i = 0;
  1494. /* device-specific stats */
  1495. data[i++] = mdp->cur_rx;
  1496. data[i++] = mdp->cur_tx;
  1497. data[i++] = mdp->dirty_rx;
  1498. data[i++] = mdp->dirty_tx;
  1499. }
  1500. static void sh_eth_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
  1501. {
  1502. switch (stringset) {
  1503. case ETH_SS_STATS:
  1504. memcpy(data, *sh_eth_gstrings_stats,
  1505. sizeof(sh_eth_gstrings_stats));
  1506. break;
  1507. }
  1508. }
  1509. static void sh_eth_get_ringparam(struct net_device *ndev,
  1510. struct ethtool_ringparam *ring)
  1511. {
  1512. struct sh_eth_private *mdp = netdev_priv(ndev);
  1513. ring->rx_max_pending = RX_RING_MAX;
  1514. ring->tx_max_pending = TX_RING_MAX;
  1515. ring->rx_pending = mdp->num_rx_ring;
  1516. ring->tx_pending = mdp->num_tx_ring;
  1517. }
  1518. static int sh_eth_set_ringparam(struct net_device *ndev,
  1519. struct ethtool_ringparam *ring)
  1520. {
  1521. struct sh_eth_private *mdp = netdev_priv(ndev);
  1522. int ret;
  1523. if (ring->tx_pending > TX_RING_MAX ||
  1524. ring->rx_pending > RX_RING_MAX ||
  1525. ring->tx_pending < TX_RING_MIN ||
  1526. ring->rx_pending < RX_RING_MIN)
  1527. return -EINVAL;
  1528. if (ring->rx_mini_pending || ring->rx_jumbo_pending)
  1529. return -EINVAL;
  1530. if (netif_running(ndev)) {
  1531. netif_tx_disable(ndev);
  1532. /* Disable interrupts by clearing the interrupt mask. */
  1533. sh_eth_write(ndev, 0x0000, EESIPR);
  1534. /* Stop the chip's Tx and Rx processes. */
  1535. sh_eth_write(ndev, 0, EDTRR);
  1536. sh_eth_write(ndev, 0, EDRRR);
  1537. synchronize_irq(ndev->irq);
  1538. }
  1539. /* Free all the skbuffs in the Rx queue. */
  1540. sh_eth_ring_free(ndev);
  1541. /* Free DMA buffer */
  1542. sh_eth_free_dma_buffer(mdp);
  1543. /* Set new parameters */
  1544. mdp->num_rx_ring = ring->rx_pending;
  1545. mdp->num_tx_ring = ring->tx_pending;
  1546. ret = sh_eth_ring_init(ndev);
  1547. if (ret < 0) {
  1548. dev_err(&ndev->dev, "%s: sh_eth_ring_init failed.\n", __func__);
  1549. return ret;
  1550. }
  1551. ret = sh_eth_dev_init(ndev, false);
  1552. if (ret < 0) {
  1553. dev_err(&ndev->dev, "%s: sh_eth_dev_init failed.\n", __func__);
  1554. return ret;
  1555. }
  1556. if (netif_running(ndev)) {
  1557. sh_eth_write(ndev, mdp->cd->eesipr_value, EESIPR);
  1558. /* Setting the Rx mode will start the Rx process. */
  1559. sh_eth_write(ndev, EDRRR_R, EDRRR);
  1560. netif_wake_queue(ndev);
  1561. }
  1562. return 0;
  1563. }
  1564. static const struct ethtool_ops sh_eth_ethtool_ops = {
  1565. .get_settings = sh_eth_get_settings,
  1566. .set_settings = sh_eth_set_settings,
  1567. .nway_reset = sh_eth_nway_reset,
  1568. .get_msglevel = sh_eth_get_msglevel,
  1569. .set_msglevel = sh_eth_set_msglevel,
  1570. .get_link = ethtool_op_get_link,
  1571. .get_strings = sh_eth_get_strings,
  1572. .get_ethtool_stats = sh_eth_get_ethtool_stats,
  1573. .get_sset_count = sh_eth_get_sset_count,
  1574. .get_ringparam = sh_eth_get_ringparam,
  1575. .set_ringparam = sh_eth_set_ringparam,
  1576. };
  1577. /* network device open function */
  1578. static int sh_eth_open(struct net_device *ndev)
  1579. {
  1580. int ret = 0;
  1581. struct sh_eth_private *mdp = netdev_priv(ndev);
  1582. pm_runtime_get_sync(&mdp->pdev->dev);
  1583. ret = request_irq(ndev->irq, sh_eth_interrupt,
  1584. mdp->cd->irq_flags, ndev->name, ndev);
  1585. if (ret) {
  1586. dev_err(&ndev->dev, "Can not assign IRQ number\n");
  1587. return ret;
  1588. }
  1589. /* Descriptor set */
  1590. ret = sh_eth_ring_init(ndev);
  1591. if (ret)
  1592. goto out_free_irq;
  1593. /* device init */
  1594. ret = sh_eth_dev_init(ndev, true);
  1595. if (ret)
  1596. goto out_free_irq;
  1597. /* PHY control start*/
  1598. ret = sh_eth_phy_start(ndev);
  1599. if (ret)
  1600. goto out_free_irq;
  1601. return ret;
  1602. out_free_irq:
  1603. free_irq(ndev->irq, ndev);
  1604. pm_runtime_put_sync(&mdp->pdev->dev);
  1605. return ret;
  1606. }
  1607. /* Timeout function */
  1608. static void sh_eth_tx_timeout(struct net_device *ndev)
  1609. {
  1610. struct sh_eth_private *mdp = netdev_priv(ndev);
  1611. struct sh_eth_rxdesc *rxdesc;
  1612. int i;
  1613. netif_stop_queue(ndev);
  1614. if (netif_msg_timer(mdp))
  1615. dev_err(&ndev->dev, "%s: transmit timed out, status %8.8x,"
  1616. " resetting...\n", ndev->name, (int)sh_eth_read(ndev, EESR));
  1617. /* tx_errors count up */
  1618. ndev->stats.tx_errors++;
  1619. /* Free all the skbuffs in the Rx queue. */
  1620. for (i = 0; i < mdp->num_rx_ring; i++) {
  1621. rxdesc = &mdp->rx_ring[i];
  1622. rxdesc->status = 0;
  1623. rxdesc->addr = 0xBADF00D0;
  1624. if (mdp->rx_skbuff[i])
  1625. dev_kfree_skb(mdp->rx_skbuff[i]);
  1626. mdp->rx_skbuff[i] = NULL;
  1627. }
  1628. for (i = 0; i < mdp->num_tx_ring; i++) {
  1629. if (mdp->tx_skbuff[i])
  1630. dev_kfree_skb(mdp->tx_skbuff[i]);
  1631. mdp->tx_skbuff[i] = NULL;
  1632. }
  1633. /* device init */
  1634. sh_eth_dev_init(ndev, true);
  1635. }
  1636. /* Packet transmit function */
  1637. static int sh_eth_start_xmit(struct sk_buff *skb, struct net_device *ndev)
  1638. {
  1639. struct sh_eth_private *mdp = netdev_priv(ndev);
  1640. struct sh_eth_txdesc *txdesc;
  1641. u32 entry;
  1642. unsigned long flags;
  1643. spin_lock_irqsave(&mdp->lock, flags);
  1644. if ((mdp->cur_tx - mdp->dirty_tx) >= (mdp->num_tx_ring - 4)) {
  1645. if (!sh_eth_txfree(ndev)) {
  1646. if (netif_msg_tx_queued(mdp))
  1647. dev_warn(&ndev->dev, "TxFD exhausted.\n");
  1648. netif_stop_queue(ndev);
  1649. spin_unlock_irqrestore(&mdp->lock, flags);
  1650. return NETDEV_TX_BUSY;
  1651. }
  1652. }
  1653. spin_unlock_irqrestore(&mdp->lock, flags);
  1654. entry = mdp->cur_tx % mdp->num_tx_ring;
  1655. mdp->tx_skbuff[entry] = skb;
  1656. txdesc = &mdp->tx_ring[entry];
  1657. /* soft swap. */
  1658. if (!mdp->cd->hw_swap)
  1659. sh_eth_soft_swap(phys_to_virt(ALIGN(txdesc->addr, 4)),
  1660. skb->len + 2);
  1661. txdesc->addr = dma_map_single(&ndev->dev, skb->data, skb->len,
  1662. DMA_TO_DEVICE);
  1663. if (skb->len < ETHERSMALL)
  1664. txdesc->buffer_length = ETHERSMALL;
  1665. else
  1666. txdesc->buffer_length = skb->len;
  1667. if (entry >= mdp->num_tx_ring - 1)
  1668. txdesc->status |= cpu_to_edmac(mdp, TD_TACT | TD_TDLE);
  1669. else
  1670. txdesc->status |= cpu_to_edmac(mdp, TD_TACT);
  1671. mdp->cur_tx++;
  1672. if (!(sh_eth_read(ndev, EDTRR) & sh_eth_get_edtrr_trns(mdp)))
  1673. sh_eth_write(ndev, sh_eth_get_edtrr_trns(mdp), EDTRR);
  1674. return NETDEV_TX_OK;
  1675. }
  1676. /* device close function */
  1677. static int sh_eth_close(struct net_device *ndev)
  1678. {
  1679. struct sh_eth_private *mdp = netdev_priv(ndev);
  1680. netif_stop_queue(ndev);
  1681. /* Disable interrupts by clearing the interrupt mask. */
  1682. sh_eth_write(ndev, 0x0000, EESIPR);
  1683. /* Stop the chip's Tx and Rx processes. */
  1684. sh_eth_write(ndev, 0, EDTRR);
  1685. sh_eth_write(ndev, 0, EDRRR);
  1686. /* PHY Disconnect */
  1687. if (mdp->phydev) {
  1688. phy_stop(mdp->phydev);
  1689. phy_disconnect(mdp->phydev);
  1690. }
  1691. free_irq(ndev->irq, ndev);
  1692. /* Free all the skbuffs in the Rx queue. */
  1693. sh_eth_ring_free(ndev);
  1694. /* free DMA buffer */
  1695. sh_eth_free_dma_buffer(mdp);
  1696. pm_runtime_put_sync(&mdp->pdev->dev);
  1697. return 0;
  1698. }
  1699. static struct net_device_stats *sh_eth_get_stats(struct net_device *ndev)
  1700. {
  1701. struct sh_eth_private *mdp = netdev_priv(ndev);
  1702. pm_runtime_get_sync(&mdp->pdev->dev);
  1703. ndev->stats.tx_dropped += sh_eth_read(ndev, TROCR);
  1704. sh_eth_write(ndev, 0, TROCR); /* (write clear) */
  1705. ndev->stats.collisions += sh_eth_read(ndev, CDCR);
  1706. sh_eth_write(ndev, 0, CDCR); /* (write clear) */
  1707. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, LCCR);
  1708. sh_eth_write(ndev, 0, LCCR); /* (write clear) */
  1709. if (sh_eth_is_gether(mdp)) {
  1710. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CERCR);
  1711. sh_eth_write(ndev, 0, CERCR); /* (write clear) */
  1712. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CEECR);
  1713. sh_eth_write(ndev, 0, CEECR); /* (write clear) */
  1714. } else {
  1715. ndev->stats.tx_carrier_errors += sh_eth_read(ndev, CNDCR);
  1716. sh_eth_write(ndev, 0, CNDCR); /* (write clear) */
  1717. }
  1718. pm_runtime_put_sync(&mdp->pdev->dev);
  1719. return &ndev->stats;
  1720. }
  1721. /* ioctl to device function */
  1722. static int sh_eth_do_ioctl(struct net_device *ndev, struct ifreq *rq,
  1723. int cmd)
  1724. {
  1725. struct sh_eth_private *mdp = netdev_priv(ndev);
  1726. struct phy_device *phydev = mdp->phydev;
  1727. if (!netif_running(ndev))
  1728. return -EINVAL;
  1729. if (!phydev)
  1730. return -ENODEV;
  1731. return phy_mii_ioctl(phydev, rq, cmd);
  1732. }
  1733. /* For TSU_POSTn. Please refer to the manual about this (strange) bitfields */
  1734. static void *sh_eth_tsu_get_post_reg_offset(struct sh_eth_private *mdp,
  1735. int entry)
  1736. {
  1737. return sh_eth_tsu_get_offset(mdp, TSU_POST1) + (entry / 8 * 4);
  1738. }
  1739. static u32 sh_eth_tsu_get_post_mask(int entry)
  1740. {
  1741. return 0x0f << (28 - ((entry % 8) * 4));
  1742. }
  1743. static u32 sh_eth_tsu_get_post_bit(struct sh_eth_private *mdp, int entry)
  1744. {
  1745. return (0x08 >> (mdp->port << 1)) << (28 - ((entry % 8) * 4));
  1746. }
  1747. static void sh_eth_tsu_enable_cam_entry_post(struct net_device *ndev,
  1748. int entry)
  1749. {
  1750. struct sh_eth_private *mdp = netdev_priv(ndev);
  1751. u32 tmp;
  1752. void *reg_offset;
  1753. reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
  1754. tmp = ioread32(reg_offset);
  1755. iowrite32(tmp | sh_eth_tsu_get_post_bit(mdp, entry), reg_offset);
  1756. }
  1757. static bool sh_eth_tsu_disable_cam_entry_post(struct net_device *ndev,
  1758. int entry)
  1759. {
  1760. struct sh_eth_private *mdp = netdev_priv(ndev);
  1761. u32 post_mask, ref_mask, tmp;
  1762. void *reg_offset;
  1763. reg_offset = sh_eth_tsu_get_post_reg_offset(mdp, entry);
  1764. post_mask = sh_eth_tsu_get_post_mask(entry);
  1765. ref_mask = sh_eth_tsu_get_post_bit(mdp, entry) & ~post_mask;
  1766. tmp = ioread32(reg_offset);
  1767. iowrite32(tmp & ~post_mask, reg_offset);
  1768. /* If other port enables, the function returns "true" */
  1769. return tmp & ref_mask;
  1770. }
  1771. static int sh_eth_tsu_busy(struct net_device *ndev)
  1772. {
  1773. int timeout = SH_ETH_TSU_TIMEOUT_MS * 100;
  1774. struct sh_eth_private *mdp = netdev_priv(ndev);
  1775. while ((sh_eth_tsu_read(mdp, TSU_ADSBSY) & TSU_ADSBSY_0)) {
  1776. udelay(10);
  1777. timeout--;
  1778. if (timeout <= 0) {
  1779. dev_err(&ndev->dev, "%s: timeout\n", __func__);
  1780. return -ETIMEDOUT;
  1781. }
  1782. }
  1783. return 0;
  1784. }
  1785. static int sh_eth_tsu_write_entry(struct net_device *ndev, void *reg,
  1786. const u8 *addr)
  1787. {
  1788. u32 val;
  1789. val = addr[0] << 24 | addr[1] << 16 | addr[2] << 8 | addr[3];
  1790. iowrite32(val, reg);
  1791. if (sh_eth_tsu_busy(ndev) < 0)
  1792. return -EBUSY;
  1793. val = addr[4] << 8 | addr[5];
  1794. iowrite32(val, reg + 4);
  1795. if (sh_eth_tsu_busy(ndev) < 0)
  1796. return -EBUSY;
  1797. return 0;
  1798. }
  1799. static void sh_eth_tsu_read_entry(void *reg, u8 *addr)
  1800. {
  1801. u32 val;
  1802. val = ioread32(reg);
  1803. addr[0] = (val >> 24) & 0xff;
  1804. addr[1] = (val >> 16) & 0xff;
  1805. addr[2] = (val >> 8) & 0xff;
  1806. addr[3] = val & 0xff;
  1807. val = ioread32(reg + 4);
  1808. addr[4] = (val >> 8) & 0xff;
  1809. addr[5] = val & 0xff;
  1810. }
  1811. static int sh_eth_tsu_find_entry(struct net_device *ndev, const u8 *addr)
  1812. {
  1813. struct sh_eth_private *mdp = netdev_priv(ndev);
  1814. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1815. int i;
  1816. u8 c_addr[ETH_ALEN];
  1817. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  1818. sh_eth_tsu_read_entry(reg_offset, c_addr);
  1819. if (memcmp(addr, c_addr, ETH_ALEN) == 0)
  1820. return i;
  1821. }
  1822. return -ENOENT;
  1823. }
  1824. static int sh_eth_tsu_find_empty(struct net_device *ndev)
  1825. {
  1826. u8 blank[ETH_ALEN];
  1827. int entry;
  1828. memset(blank, 0, sizeof(blank));
  1829. entry = sh_eth_tsu_find_entry(ndev, blank);
  1830. return (entry < 0) ? -ENOMEM : entry;
  1831. }
  1832. static int sh_eth_tsu_disable_cam_entry_table(struct net_device *ndev,
  1833. int entry)
  1834. {
  1835. struct sh_eth_private *mdp = netdev_priv(ndev);
  1836. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1837. int ret;
  1838. u8 blank[ETH_ALEN];
  1839. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) &
  1840. ~(1 << (31 - entry)), TSU_TEN);
  1841. memset(blank, 0, sizeof(blank));
  1842. ret = sh_eth_tsu_write_entry(ndev, reg_offset + entry * 8, blank);
  1843. if (ret < 0)
  1844. return ret;
  1845. return 0;
  1846. }
  1847. static int sh_eth_tsu_add_entry(struct net_device *ndev, const u8 *addr)
  1848. {
  1849. struct sh_eth_private *mdp = netdev_priv(ndev);
  1850. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1851. int i, ret;
  1852. if (!mdp->cd->tsu)
  1853. return 0;
  1854. i = sh_eth_tsu_find_entry(ndev, addr);
  1855. if (i < 0) {
  1856. /* No entry found, create one */
  1857. i = sh_eth_tsu_find_empty(ndev);
  1858. if (i < 0)
  1859. return -ENOMEM;
  1860. ret = sh_eth_tsu_write_entry(ndev, reg_offset + i * 8, addr);
  1861. if (ret < 0)
  1862. return ret;
  1863. /* Enable the entry */
  1864. sh_eth_tsu_write(mdp, sh_eth_tsu_read(mdp, TSU_TEN) |
  1865. (1 << (31 - i)), TSU_TEN);
  1866. }
  1867. /* Entry found or created, enable POST */
  1868. sh_eth_tsu_enable_cam_entry_post(ndev, i);
  1869. return 0;
  1870. }
  1871. static int sh_eth_tsu_del_entry(struct net_device *ndev, const u8 *addr)
  1872. {
  1873. struct sh_eth_private *mdp = netdev_priv(ndev);
  1874. int i, ret;
  1875. if (!mdp->cd->tsu)
  1876. return 0;
  1877. i = sh_eth_tsu_find_entry(ndev, addr);
  1878. if (i) {
  1879. /* Entry found */
  1880. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  1881. goto done;
  1882. /* Disable the entry if both ports was disabled */
  1883. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  1884. if (ret < 0)
  1885. return ret;
  1886. }
  1887. done:
  1888. return 0;
  1889. }
  1890. static int sh_eth_tsu_purge_all(struct net_device *ndev)
  1891. {
  1892. struct sh_eth_private *mdp = netdev_priv(ndev);
  1893. int i, ret;
  1894. if (unlikely(!mdp->cd->tsu))
  1895. return 0;
  1896. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++) {
  1897. if (sh_eth_tsu_disable_cam_entry_post(ndev, i))
  1898. continue;
  1899. /* Disable the entry if both ports was disabled */
  1900. ret = sh_eth_tsu_disable_cam_entry_table(ndev, i);
  1901. if (ret < 0)
  1902. return ret;
  1903. }
  1904. return 0;
  1905. }
  1906. static void sh_eth_tsu_purge_mcast(struct net_device *ndev)
  1907. {
  1908. struct sh_eth_private *mdp = netdev_priv(ndev);
  1909. u8 addr[ETH_ALEN];
  1910. void *reg_offset = sh_eth_tsu_get_offset(mdp, TSU_ADRH0);
  1911. int i;
  1912. if (unlikely(!mdp->cd->tsu))
  1913. return;
  1914. for (i = 0; i < SH_ETH_TSU_CAM_ENTRIES; i++, reg_offset += 8) {
  1915. sh_eth_tsu_read_entry(reg_offset, addr);
  1916. if (is_multicast_ether_addr(addr))
  1917. sh_eth_tsu_del_entry(ndev, addr);
  1918. }
  1919. }
  1920. /* Multicast reception directions set */
  1921. static void sh_eth_set_multicast_list(struct net_device *ndev)
  1922. {
  1923. struct sh_eth_private *mdp = netdev_priv(ndev);
  1924. u32 ecmr_bits;
  1925. int mcast_all = 0;
  1926. unsigned long flags;
  1927. spin_lock_irqsave(&mdp->lock, flags);
  1928. /*
  1929. * Initial condition is MCT = 1, PRM = 0.
  1930. * Depending on ndev->flags, set PRM or clear MCT
  1931. */
  1932. ecmr_bits = (sh_eth_read(ndev, ECMR) & ~ECMR_PRM) | ECMR_MCT;
  1933. if (!(ndev->flags & IFF_MULTICAST)) {
  1934. sh_eth_tsu_purge_mcast(ndev);
  1935. mcast_all = 1;
  1936. }
  1937. if (ndev->flags & IFF_ALLMULTI) {
  1938. sh_eth_tsu_purge_mcast(ndev);
  1939. ecmr_bits &= ~ECMR_MCT;
  1940. mcast_all = 1;
  1941. }
  1942. if (ndev->flags & IFF_PROMISC) {
  1943. sh_eth_tsu_purge_all(ndev);
  1944. ecmr_bits = (ecmr_bits & ~ECMR_MCT) | ECMR_PRM;
  1945. } else if (mdp->cd->tsu) {
  1946. struct netdev_hw_addr *ha;
  1947. netdev_for_each_mc_addr(ha, ndev) {
  1948. if (mcast_all && is_multicast_ether_addr(ha->addr))
  1949. continue;
  1950. if (sh_eth_tsu_add_entry(ndev, ha->addr) < 0) {
  1951. if (!mcast_all) {
  1952. sh_eth_tsu_purge_mcast(ndev);
  1953. ecmr_bits &= ~ECMR_MCT;
  1954. mcast_all = 1;
  1955. }
  1956. }
  1957. }
  1958. } else {
  1959. /* Normal, unicast/broadcast-only mode. */
  1960. ecmr_bits = (ecmr_bits & ~ECMR_PRM) | ECMR_MCT;
  1961. }
  1962. /* update the ethernet mode */
  1963. sh_eth_write(ndev, ecmr_bits, ECMR);
  1964. spin_unlock_irqrestore(&mdp->lock, flags);
  1965. }
  1966. static int sh_eth_get_vtag_index(struct sh_eth_private *mdp)
  1967. {
  1968. if (!mdp->port)
  1969. return TSU_VTAG0;
  1970. else
  1971. return TSU_VTAG1;
  1972. }
  1973. static int sh_eth_vlan_rx_add_vid(struct net_device *ndev,
  1974. __be16 proto, u16 vid)
  1975. {
  1976. struct sh_eth_private *mdp = netdev_priv(ndev);
  1977. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  1978. if (unlikely(!mdp->cd->tsu))
  1979. return -EPERM;
  1980. /* No filtering if vid = 0 */
  1981. if (!vid)
  1982. return 0;
  1983. mdp->vlan_num_ids++;
  1984. /*
  1985. * The controller has one VLAN tag HW filter. So, if the filter is
  1986. * already enabled, the driver disables it and the filte
  1987. */
  1988. if (mdp->vlan_num_ids > 1) {
  1989. /* disable VLAN filter */
  1990. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  1991. return 0;
  1992. }
  1993. sh_eth_tsu_write(mdp, TSU_VTAG_ENABLE | (vid & TSU_VTAG_VID_MASK),
  1994. vtag_reg_index);
  1995. return 0;
  1996. }
  1997. static int sh_eth_vlan_rx_kill_vid(struct net_device *ndev,
  1998. __be16 proto, u16 vid)
  1999. {
  2000. struct sh_eth_private *mdp = netdev_priv(ndev);
  2001. int vtag_reg_index = sh_eth_get_vtag_index(mdp);
  2002. if (unlikely(!mdp->cd->tsu))
  2003. return -EPERM;
  2004. /* No filtering if vid = 0 */
  2005. if (!vid)
  2006. return 0;
  2007. mdp->vlan_num_ids--;
  2008. sh_eth_tsu_write(mdp, 0, vtag_reg_index);
  2009. return 0;
  2010. }
  2011. /* SuperH's TSU register init function */
  2012. static void sh_eth_tsu_init(struct sh_eth_private *mdp)
  2013. {
  2014. sh_eth_tsu_write(mdp, 0, TSU_FWEN0); /* Disable forward(0->1) */
  2015. sh_eth_tsu_write(mdp, 0, TSU_FWEN1); /* Disable forward(1->0) */
  2016. sh_eth_tsu_write(mdp, 0, TSU_FCM); /* forward fifo 3k-3k */
  2017. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL0);
  2018. sh_eth_tsu_write(mdp, 0xc, TSU_BSYSL1);
  2019. sh_eth_tsu_write(mdp, 0, TSU_PRISL0);
  2020. sh_eth_tsu_write(mdp, 0, TSU_PRISL1);
  2021. sh_eth_tsu_write(mdp, 0, TSU_FWSL0);
  2022. sh_eth_tsu_write(mdp, 0, TSU_FWSL1);
  2023. sh_eth_tsu_write(mdp, TSU_FWSLC_POSTENU | TSU_FWSLC_POSTENL, TSU_FWSLC);
  2024. if (sh_eth_is_gether(mdp)) {
  2025. sh_eth_tsu_write(mdp, 0, TSU_QTAG0); /* Disable QTAG(0->1) */
  2026. sh_eth_tsu_write(mdp, 0, TSU_QTAG1); /* Disable QTAG(1->0) */
  2027. } else {
  2028. sh_eth_tsu_write(mdp, 0, TSU_QTAGM0); /* Disable QTAG(0->1) */
  2029. sh_eth_tsu_write(mdp, 0, TSU_QTAGM1); /* Disable QTAG(1->0) */
  2030. }
  2031. sh_eth_tsu_write(mdp, 0, TSU_FWSR); /* all interrupt status clear */
  2032. sh_eth_tsu_write(mdp, 0, TSU_FWINMK); /* Disable all interrupt */
  2033. sh_eth_tsu_write(mdp, 0, TSU_TEN); /* Disable all CAM entry */
  2034. sh_eth_tsu_write(mdp, 0, TSU_POST1); /* Disable CAM entry [ 0- 7] */
  2035. sh_eth_tsu_write(mdp, 0, TSU_POST2); /* Disable CAM entry [ 8-15] */
  2036. sh_eth_tsu_write(mdp, 0, TSU_POST3); /* Disable CAM entry [16-23] */
  2037. sh_eth_tsu_write(mdp, 0, TSU_POST4); /* Disable CAM entry [24-31] */
  2038. }
  2039. /* MDIO bus release function */
  2040. static int sh_mdio_release(struct net_device *ndev)
  2041. {
  2042. struct mii_bus *bus = dev_get_drvdata(&ndev->dev);
  2043. /* unregister mdio bus */
  2044. mdiobus_unregister(bus);
  2045. /* remove mdio bus info from net_device */
  2046. dev_set_drvdata(&ndev->dev, NULL);
  2047. /* free bitbang info */
  2048. free_mdio_bitbang(bus);
  2049. return 0;
  2050. }
  2051. /* MDIO bus init function */
  2052. static int sh_mdio_init(struct net_device *ndev, int id,
  2053. struct sh_eth_plat_data *pd)
  2054. {
  2055. int ret, i;
  2056. struct bb_info *bitbang;
  2057. struct sh_eth_private *mdp = netdev_priv(ndev);
  2058. /* create bit control struct for PHY */
  2059. bitbang = devm_kzalloc(&ndev->dev, sizeof(struct bb_info),
  2060. GFP_KERNEL);
  2061. if (!bitbang) {
  2062. ret = -ENOMEM;
  2063. goto out;
  2064. }
  2065. /* bitbang init */
  2066. bitbang->addr = mdp->addr + mdp->reg_offset[PIR];
  2067. bitbang->set_gate = pd->set_mdio_gate;
  2068. bitbang->mdi_msk = PIR_MDI;
  2069. bitbang->mdo_msk = PIR_MDO;
  2070. bitbang->mmd_msk = PIR_MMD;
  2071. bitbang->mdc_msk = PIR_MDC;
  2072. bitbang->ctrl.ops = &bb_ops;
  2073. /* MII controller setting */
  2074. mdp->mii_bus = alloc_mdio_bitbang(&bitbang->ctrl);
  2075. if (!mdp->mii_bus) {
  2076. ret = -ENOMEM;
  2077. goto out;
  2078. }
  2079. /* Hook up MII support for ethtool */
  2080. mdp->mii_bus->name = "sh_mii";
  2081. mdp->mii_bus->parent = &ndev->dev;
  2082. snprintf(mdp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  2083. mdp->pdev->name, id);
  2084. /* PHY IRQ */
  2085. mdp->mii_bus->irq = devm_kzalloc(&ndev->dev,
  2086. sizeof(int) * PHY_MAX_ADDR,
  2087. GFP_KERNEL);
  2088. if (!mdp->mii_bus->irq) {
  2089. ret = -ENOMEM;
  2090. goto out_free_bus;
  2091. }
  2092. for (i = 0; i < PHY_MAX_ADDR; i++)
  2093. mdp->mii_bus->irq[i] = PHY_POLL;
  2094. /* register mdio bus */
  2095. ret = mdiobus_register(mdp->mii_bus);
  2096. if (ret)
  2097. goto out_free_bus;
  2098. dev_set_drvdata(&ndev->dev, mdp->mii_bus);
  2099. return 0;
  2100. out_free_bus:
  2101. free_mdio_bitbang(mdp->mii_bus);
  2102. out:
  2103. return ret;
  2104. }
  2105. static const u16 *sh_eth_get_register_offset(int register_type)
  2106. {
  2107. const u16 *reg_offset = NULL;
  2108. switch (register_type) {
  2109. case SH_ETH_REG_GIGABIT:
  2110. reg_offset = sh_eth_offset_gigabit;
  2111. break;
  2112. case SH_ETH_REG_FAST_RCAR:
  2113. reg_offset = sh_eth_offset_fast_rcar;
  2114. break;
  2115. case SH_ETH_REG_FAST_SH4:
  2116. reg_offset = sh_eth_offset_fast_sh4;
  2117. break;
  2118. case SH_ETH_REG_FAST_SH3_SH2:
  2119. reg_offset = sh_eth_offset_fast_sh3_sh2;
  2120. break;
  2121. default:
  2122. pr_err("Unknown register type (%d)\n", register_type);
  2123. break;
  2124. }
  2125. return reg_offset;
  2126. }
  2127. static struct net_device_ops sh_eth_netdev_ops = {
  2128. .ndo_open = sh_eth_open,
  2129. .ndo_stop = sh_eth_close,
  2130. .ndo_start_xmit = sh_eth_start_xmit,
  2131. .ndo_get_stats = sh_eth_get_stats,
  2132. .ndo_tx_timeout = sh_eth_tx_timeout,
  2133. .ndo_do_ioctl = sh_eth_do_ioctl,
  2134. .ndo_validate_addr = eth_validate_addr,
  2135. .ndo_set_mac_address = eth_mac_addr,
  2136. .ndo_change_mtu = eth_change_mtu,
  2137. };
  2138. static int sh_eth_drv_probe(struct platform_device *pdev)
  2139. {
  2140. int ret, devno = 0;
  2141. struct resource *res;
  2142. struct net_device *ndev = NULL;
  2143. struct sh_eth_private *mdp = NULL;
  2144. struct sh_eth_plat_data *pd = pdev->dev.platform_data;
  2145. const struct platform_device_id *id = platform_get_device_id(pdev);
  2146. /* get base addr */
  2147. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2148. if (unlikely(res == NULL)) {
  2149. dev_err(&pdev->dev, "invalid resource\n");
  2150. ret = -EINVAL;
  2151. goto out;
  2152. }
  2153. ndev = alloc_etherdev(sizeof(struct sh_eth_private));
  2154. if (!ndev) {
  2155. ret = -ENOMEM;
  2156. goto out;
  2157. }
  2158. /* The sh Ether-specific entries in the device structure. */
  2159. ndev->base_addr = res->start;
  2160. devno = pdev->id;
  2161. if (devno < 0)
  2162. devno = 0;
  2163. ndev->dma = -1;
  2164. ret = platform_get_irq(pdev, 0);
  2165. if (ret < 0) {
  2166. ret = -ENODEV;
  2167. goto out_release;
  2168. }
  2169. ndev->irq = ret;
  2170. SET_NETDEV_DEV(ndev, &pdev->dev);
  2171. /* Fill in the fields of the device structure with ethernet values. */
  2172. ether_setup(ndev);
  2173. mdp = netdev_priv(ndev);
  2174. mdp->num_tx_ring = TX_RING_SIZE;
  2175. mdp->num_rx_ring = RX_RING_SIZE;
  2176. mdp->addr = devm_ioremap_resource(&pdev->dev, res);
  2177. if (IS_ERR(mdp->addr)) {
  2178. ret = PTR_ERR(mdp->addr);
  2179. goto out_release;
  2180. }
  2181. spin_lock_init(&mdp->lock);
  2182. mdp->pdev = pdev;
  2183. pm_runtime_enable(&pdev->dev);
  2184. pm_runtime_resume(&pdev->dev);
  2185. /* get PHY ID */
  2186. mdp->phy_id = pd->phy;
  2187. mdp->phy_interface = pd->phy_interface;
  2188. /* EDMAC endian */
  2189. mdp->edmac_endian = pd->edmac_endian;
  2190. mdp->no_ether_link = pd->no_ether_link;
  2191. mdp->ether_link_active_low = pd->ether_link_active_low;
  2192. mdp->reg_offset = sh_eth_get_register_offset(pd->register_type);
  2193. /* set cpu data */
  2194. #if defined(SH_ETH_HAS_BOTH_MODULES)
  2195. mdp->cd = sh_eth_get_cpu_data(mdp);
  2196. #else
  2197. mdp->cd = &sh_eth_my_cpu_data;
  2198. #endif
  2199. if (id->driver_data)
  2200. mdp->cd = (struct sh_eth_cpu_data *)id->driver_data;
  2201. sh_eth_set_default_cpu_data(mdp->cd);
  2202. /* set function */
  2203. if (mdp->cd->tsu) {
  2204. sh_eth_netdev_ops.ndo_set_rx_mode = sh_eth_set_multicast_list;
  2205. sh_eth_netdev_ops.ndo_vlan_rx_add_vid = sh_eth_vlan_rx_add_vid;
  2206. sh_eth_netdev_ops.ndo_vlan_rx_kill_vid =
  2207. sh_eth_vlan_rx_kill_vid;
  2208. }
  2209. ndev->netdev_ops = &sh_eth_netdev_ops;
  2210. SET_ETHTOOL_OPS(ndev, &sh_eth_ethtool_ops);
  2211. ndev->watchdog_timeo = TX_TIMEOUT;
  2212. /* debug message level */
  2213. mdp->msg_enable = SH_ETH_DEF_MSG_ENABLE;
  2214. /* read and set MAC address */
  2215. read_mac_address(ndev, pd->mac_addr);
  2216. if (!is_valid_ether_addr(ndev->dev_addr)) {
  2217. dev_warn(&pdev->dev,
  2218. "no valid MAC address supplied, using a random one.\n");
  2219. eth_hw_addr_random(ndev);
  2220. }
  2221. /* ioremap the TSU registers */
  2222. if (mdp->cd->tsu) {
  2223. struct resource *rtsu;
  2224. rtsu = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  2225. mdp->tsu_addr = devm_ioremap_resource(&pdev->dev, rtsu);
  2226. if (IS_ERR(mdp->tsu_addr)) {
  2227. ret = PTR_ERR(mdp->tsu_addr);
  2228. goto out_release;
  2229. }
  2230. mdp->port = devno % 2;
  2231. ndev->features = NETIF_F_HW_VLAN_CTAG_FILTER;
  2232. }
  2233. /* initialize first or needed device */
  2234. if (!devno || pd->needs_init) {
  2235. if (mdp->cd->chip_reset)
  2236. mdp->cd->chip_reset(ndev);
  2237. if (mdp->cd->tsu) {
  2238. /* TSU init (Init only)*/
  2239. sh_eth_tsu_init(mdp);
  2240. }
  2241. }
  2242. /* network device register */
  2243. ret = register_netdev(ndev);
  2244. if (ret)
  2245. goto out_release;
  2246. /* mdio bus init */
  2247. ret = sh_mdio_init(ndev, pdev->id, pd);
  2248. if (ret)
  2249. goto out_unregister;
  2250. /* print device information */
  2251. pr_info("Base address at 0x%x, %pM, IRQ %d.\n",
  2252. (u32)ndev->base_addr, ndev->dev_addr, ndev->irq);
  2253. platform_set_drvdata(pdev, ndev);
  2254. return ret;
  2255. out_unregister:
  2256. unregister_netdev(ndev);
  2257. out_release:
  2258. /* net_dev free */
  2259. if (ndev)
  2260. free_netdev(ndev);
  2261. out:
  2262. return ret;
  2263. }
  2264. static int sh_eth_drv_remove(struct platform_device *pdev)
  2265. {
  2266. struct net_device *ndev = platform_get_drvdata(pdev);
  2267. sh_mdio_release(ndev);
  2268. unregister_netdev(ndev);
  2269. pm_runtime_disable(&pdev->dev);
  2270. free_netdev(ndev);
  2271. return 0;
  2272. }
  2273. #ifdef CONFIG_PM
  2274. static int sh_eth_runtime_nop(struct device *dev)
  2275. {
  2276. /*
  2277. * Runtime PM callback shared between ->runtime_suspend()
  2278. * and ->runtime_resume(). Simply returns success.
  2279. *
  2280. * This driver re-initializes all registers after
  2281. * pm_runtime_get_sync() anyway so there is no need
  2282. * to save and restore registers here.
  2283. */
  2284. return 0;
  2285. }
  2286. static const struct dev_pm_ops sh_eth_dev_pm_ops = {
  2287. .runtime_suspend = sh_eth_runtime_nop,
  2288. .runtime_resume = sh_eth_runtime_nop,
  2289. };
  2290. #define SH_ETH_PM_OPS (&sh_eth_dev_pm_ops)
  2291. #else
  2292. #define SH_ETH_PM_OPS NULL
  2293. #endif
  2294. static struct platform_device_id sh_eth_id_table[] = {
  2295. { CARDNAME },
  2296. { }
  2297. };
  2298. MODULE_DEVICE_TABLE(platform, sh_eth_id_table);
  2299. static struct platform_driver sh_eth_driver = {
  2300. .probe = sh_eth_drv_probe,
  2301. .remove = sh_eth_drv_remove,
  2302. .id_table = sh_eth_id_table,
  2303. .driver = {
  2304. .name = CARDNAME,
  2305. .pm = SH_ETH_PM_OPS,
  2306. },
  2307. };
  2308. module_platform_driver(sh_eth_driver);
  2309. MODULE_AUTHOR("Nobuhiro Iwamatsu, Yoshihiro Shimoda");
  2310. MODULE_DESCRIPTION("Renesas SuperH Ethernet driver");
  2311. MODULE_LICENSE("GPL v2");