sh_eth.c 64 KB

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