ks8851_mll.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  1. /**
  2. * drivers/net/ethernet/micrel/ks8851_mll.c
  3. * Copyright (c) 2009 Micrel Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /* Supports:
  19. * KS8851 16bit MLL chip from Micrel Inc.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ethtool.h>
  28. #include <linux/cache.h>
  29. #include <linux/crc32.h>
  30. #include <linux/crc32poly.h>
  31. #include <linux/mii.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/delay.h>
  34. #include <linux/slab.h>
  35. #include <linux/ks8851_mll.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/of_net.h>
  39. #define DRV_NAME "ks8851_mll"
  40. static u8 KS_DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x86, 0x95, 0x11 };
  41. #define MAX_RECV_FRAMES 255
  42. #define MAX_BUF_SIZE 2048
  43. #define TX_BUF_SIZE 2000
  44. #define RX_BUF_SIZE 2000
  45. #define KS_CCR 0x08
  46. #define CCR_EEPROM (1 << 9)
  47. #define CCR_SPI (1 << 8)
  48. #define CCR_8BIT (1 << 7)
  49. #define CCR_16BIT (1 << 6)
  50. #define CCR_32BIT (1 << 5)
  51. #define CCR_SHARED (1 << 4)
  52. #define CCR_32PIN (1 << 0)
  53. /* MAC address registers */
  54. #define KS_MARL 0x10
  55. #define KS_MARM 0x12
  56. #define KS_MARH 0x14
  57. #define KS_OBCR 0x20
  58. #define OBCR_ODS_16MA (1 << 6)
  59. #define KS_EEPCR 0x22
  60. #define EEPCR_EESA (1 << 4)
  61. #define EEPCR_EESB (1 << 3)
  62. #define EEPCR_EEDO (1 << 2)
  63. #define EEPCR_EESCK (1 << 1)
  64. #define EEPCR_EECS (1 << 0)
  65. #define KS_MBIR 0x24
  66. #define MBIR_TXMBF (1 << 12)
  67. #define MBIR_TXMBFA (1 << 11)
  68. #define MBIR_RXMBF (1 << 4)
  69. #define MBIR_RXMBFA (1 << 3)
  70. #define KS_GRR 0x26
  71. #define GRR_QMU (1 << 1)
  72. #define GRR_GSR (1 << 0)
  73. #define KS_WFCR 0x2A
  74. #define WFCR_MPRXE (1 << 7)
  75. #define WFCR_WF3E (1 << 3)
  76. #define WFCR_WF2E (1 << 2)
  77. #define WFCR_WF1E (1 << 1)
  78. #define WFCR_WF0E (1 << 0)
  79. #define KS_WF0CRC0 0x30
  80. #define KS_WF0CRC1 0x32
  81. #define KS_WF0BM0 0x34
  82. #define KS_WF0BM1 0x36
  83. #define KS_WF0BM2 0x38
  84. #define KS_WF0BM3 0x3A
  85. #define KS_WF1CRC0 0x40
  86. #define KS_WF1CRC1 0x42
  87. #define KS_WF1BM0 0x44
  88. #define KS_WF1BM1 0x46
  89. #define KS_WF1BM2 0x48
  90. #define KS_WF1BM3 0x4A
  91. #define KS_WF2CRC0 0x50
  92. #define KS_WF2CRC1 0x52
  93. #define KS_WF2BM0 0x54
  94. #define KS_WF2BM1 0x56
  95. #define KS_WF2BM2 0x58
  96. #define KS_WF2BM3 0x5A
  97. #define KS_WF3CRC0 0x60
  98. #define KS_WF3CRC1 0x62
  99. #define KS_WF3BM0 0x64
  100. #define KS_WF3BM1 0x66
  101. #define KS_WF3BM2 0x68
  102. #define KS_WF3BM3 0x6A
  103. #define KS_TXCR 0x70
  104. #define TXCR_TCGICMP (1 << 8)
  105. #define TXCR_TCGUDP (1 << 7)
  106. #define TXCR_TCGTCP (1 << 6)
  107. #define TXCR_TCGIP (1 << 5)
  108. #define TXCR_FTXQ (1 << 4)
  109. #define TXCR_TXFCE (1 << 3)
  110. #define TXCR_TXPE (1 << 2)
  111. #define TXCR_TXCRC (1 << 1)
  112. #define TXCR_TXE (1 << 0)
  113. #define KS_TXSR 0x72
  114. #define TXSR_TXLC (1 << 13)
  115. #define TXSR_TXMC (1 << 12)
  116. #define TXSR_TXFID_MASK (0x3f << 0)
  117. #define TXSR_TXFID_SHIFT (0)
  118. #define TXSR_TXFID_GET(_v) (((_v) >> 0) & 0x3f)
  119. #define KS_RXCR1 0x74
  120. #define RXCR1_FRXQ (1 << 15)
  121. #define RXCR1_RXUDPFCC (1 << 14)
  122. #define RXCR1_RXTCPFCC (1 << 13)
  123. #define RXCR1_RXIPFCC (1 << 12)
  124. #define RXCR1_RXPAFMA (1 << 11)
  125. #define RXCR1_RXFCE (1 << 10)
  126. #define RXCR1_RXEFE (1 << 9)
  127. #define RXCR1_RXMAFMA (1 << 8)
  128. #define RXCR1_RXBE (1 << 7)
  129. #define RXCR1_RXME (1 << 6)
  130. #define RXCR1_RXUE (1 << 5)
  131. #define RXCR1_RXAE (1 << 4)
  132. #define RXCR1_RXINVF (1 << 1)
  133. #define RXCR1_RXE (1 << 0)
  134. #define RXCR1_FILTER_MASK (RXCR1_RXINVF | RXCR1_RXAE | \
  135. RXCR1_RXMAFMA | RXCR1_RXPAFMA)
  136. #define KS_RXCR2 0x76
  137. #define RXCR2_SRDBL_MASK (0x7 << 5)
  138. #define RXCR2_SRDBL_SHIFT (5)
  139. #define RXCR2_SRDBL_4B (0x0 << 5)
  140. #define RXCR2_SRDBL_8B (0x1 << 5)
  141. #define RXCR2_SRDBL_16B (0x2 << 5)
  142. #define RXCR2_SRDBL_32B (0x3 << 5)
  143. /* #define RXCR2_SRDBL_FRAME (0x4 << 5) */
  144. #define RXCR2_IUFFP (1 << 4)
  145. #define RXCR2_RXIUFCEZ (1 << 3)
  146. #define RXCR2_UDPLFE (1 << 2)
  147. #define RXCR2_RXICMPFCC (1 << 1)
  148. #define RXCR2_RXSAF (1 << 0)
  149. #define KS_TXMIR 0x78
  150. #define KS_RXFHSR 0x7C
  151. #define RXFSHR_RXFV (1 << 15)
  152. #define RXFSHR_RXICMPFCS (1 << 13)
  153. #define RXFSHR_RXIPFCS (1 << 12)
  154. #define RXFSHR_RXTCPFCS (1 << 11)
  155. #define RXFSHR_RXUDPFCS (1 << 10)
  156. #define RXFSHR_RXBF (1 << 7)
  157. #define RXFSHR_RXMF (1 << 6)
  158. #define RXFSHR_RXUF (1 << 5)
  159. #define RXFSHR_RXMR (1 << 4)
  160. #define RXFSHR_RXFT (1 << 3)
  161. #define RXFSHR_RXFTL (1 << 2)
  162. #define RXFSHR_RXRF (1 << 1)
  163. #define RXFSHR_RXCE (1 << 0)
  164. #define RXFSHR_ERR (RXFSHR_RXCE | RXFSHR_RXRF |\
  165. RXFSHR_RXFTL | RXFSHR_RXMR |\
  166. RXFSHR_RXICMPFCS | RXFSHR_RXIPFCS |\
  167. RXFSHR_RXTCPFCS)
  168. #define KS_RXFHBCR 0x7E
  169. #define RXFHBCR_CNT_MASK 0x0FFF
  170. #define KS_TXQCR 0x80
  171. #define TXQCR_AETFE (1 << 2)
  172. #define TXQCR_TXQMAM (1 << 1)
  173. #define TXQCR_METFE (1 << 0)
  174. #define KS_RXQCR 0x82
  175. #define RXQCR_RXDTTS (1 << 12)
  176. #define RXQCR_RXDBCTS (1 << 11)
  177. #define RXQCR_RXFCTS (1 << 10)
  178. #define RXQCR_RXIPHTOE (1 << 9)
  179. #define RXQCR_RXDTTE (1 << 7)
  180. #define RXQCR_RXDBCTE (1 << 6)
  181. #define RXQCR_RXFCTE (1 << 5)
  182. #define RXQCR_ADRFE (1 << 4)
  183. #define RXQCR_SDA (1 << 3)
  184. #define RXQCR_RRXEF (1 << 0)
  185. #define RXQCR_CMD_CNTL (RXQCR_RXFCTE|RXQCR_ADRFE)
  186. #define KS_TXFDPR 0x84
  187. #define TXFDPR_TXFPAI (1 << 14)
  188. #define TXFDPR_TXFP_MASK (0x7ff << 0)
  189. #define TXFDPR_TXFP_SHIFT (0)
  190. #define KS_RXFDPR 0x86
  191. #define RXFDPR_RXFPAI (1 << 14)
  192. #define KS_RXDTTR 0x8C
  193. #define KS_RXDBCTR 0x8E
  194. #define KS_IER 0x90
  195. #define KS_ISR 0x92
  196. #define IRQ_LCI (1 << 15)
  197. #define IRQ_TXI (1 << 14)
  198. #define IRQ_RXI (1 << 13)
  199. #define IRQ_RXOI (1 << 11)
  200. #define IRQ_TXPSI (1 << 9)
  201. #define IRQ_RXPSI (1 << 8)
  202. #define IRQ_TXSAI (1 << 6)
  203. #define IRQ_RXWFDI (1 << 5)
  204. #define IRQ_RXMPDI (1 << 4)
  205. #define IRQ_LDI (1 << 3)
  206. #define IRQ_EDI (1 << 2)
  207. #define IRQ_SPIBEI (1 << 1)
  208. #define IRQ_DEDI (1 << 0)
  209. #define KS_RXFCTR 0x9C
  210. #define RXFCTR_THRESHOLD_MASK 0x00FF
  211. #define KS_RXFC 0x9D
  212. #define RXFCTR_RXFC_MASK (0xff << 8)
  213. #define RXFCTR_RXFC_SHIFT (8)
  214. #define RXFCTR_RXFC_GET(_v) (((_v) >> 8) & 0xff)
  215. #define RXFCTR_RXFCT_MASK (0xff << 0)
  216. #define RXFCTR_RXFCT_SHIFT (0)
  217. #define KS_TXNTFSR 0x9E
  218. #define KS_MAHTR0 0xA0
  219. #define KS_MAHTR1 0xA2
  220. #define KS_MAHTR2 0xA4
  221. #define KS_MAHTR3 0xA6
  222. #define KS_FCLWR 0xB0
  223. #define KS_FCHWR 0xB2
  224. #define KS_FCOWR 0xB4
  225. #define KS_CIDER 0xC0
  226. #define CIDER_ID 0x8870
  227. #define CIDER_REV_MASK (0x7 << 1)
  228. #define CIDER_REV_SHIFT (1)
  229. #define CIDER_REV_GET(_v) (((_v) >> 1) & 0x7)
  230. #define KS_CGCR 0xC6
  231. #define KS_IACR 0xC8
  232. #define IACR_RDEN (1 << 12)
  233. #define IACR_TSEL_MASK (0x3 << 10)
  234. #define IACR_TSEL_SHIFT (10)
  235. #define IACR_TSEL_MIB (0x3 << 10)
  236. #define IACR_ADDR_MASK (0x1f << 0)
  237. #define IACR_ADDR_SHIFT (0)
  238. #define KS_IADLR 0xD0
  239. #define KS_IAHDR 0xD2
  240. #define KS_PMECR 0xD4
  241. #define PMECR_PME_DELAY (1 << 14)
  242. #define PMECR_PME_POL (1 << 12)
  243. #define PMECR_WOL_WAKEUP (1 << 11)
  244. #define PMECR_WOL_MAGICPKT (1 << 10)
  245. #define PMECR_WOL_LINKUP (1 << 9)
  246. #define PMECR_WOL_ENERGY (1 << 8)
  247. #define PMECR_AUTO_WAKE_EN (1 << 7)
  248. #define PMECR_WAKEUP_NORMAL (1 << 6)
  249. #define PMECR_WKEVT_MASK (0xf << 2)
  250. #define PMECR_WKEVT_SHIFT (2)
  251. #define PMECR_WKEVT_GET(_v) (((_v) >> 2) & 0xf)
  252. #define PMECR_WKEVT_ENERGY (0x1 << 2)
  253. #define PMECR_WKEVT_LINK (0x2 << 2)
  254. #define PMECR_WKEVT_MAGICPKT (0x4 << 2)
  255. #define PMECR_WKEVT_FRAME (0x8 << 2)
  256. #define PMECR_PM_MASK (0x3 << 0)
  257. #define PMECR_PM_SHIFT (0)
  258. #define PMECR_PM_NORMAL (0x0 << 0)
  259. #define PMECR_PM_ENERGY (0x1 << 0)
  260. #define PMECR_PM_SOFTDOWN (0x2 << 0)
  261. #define PMECR_PM_POWERSAVE (0x3 << 0)
  262. /* Standard MII PHY data */
  263. #define KS_P1MBCR 0xE4
  264. #define P1MBCR_FORCE_FDX (1 << 8)
  265. #define KS_P1MBSR 0xE6
  266. #define P1MBSR_AN_COMPLETE (1 << 5)
  267. #define P1MBSR_AN_CAPABLE (1 << 3)
  268. #define P1MBSR_LINK_UP (1 << 2)
  269. #define KS_PHY1ILR 0xE8
  270. #define KS_PHY1IHR 0xEA
  271. #define KS_P1ANAR 0xEC
  272. #define KS_P1ANLPR 0xEE
  273. #define KS_P1SCLMD 0xF4
  274. #define P1SCLMD_LEDOFF (1 << 15)
  275. #define P1SCLMD_TXIDS (1 << 14)
  276. #define P1SCLMD_RESTARTAN (1 << 13)
  277. #define P1SCLMD_DISAUTOMDIX (1 << 10)
  278. #define P1SCLMD_FORCEMDIX (1 << 9)
  279. #define P1SCLMD_AUTONEGEN (1 << 7)
  280. #define P1SCLMD_FORCE100 (1 << 6)
  281. #define P1SCLMD_FORCEFDX (1 << 5)
  282. #define P1SCLMD_ADV_FLOW (1 << 4)
  283. #define P1SCLMD_ADV_100BT_FDX (1 << 3)
  284. #define P1SCLMD_ADV_100BT_HDX (1 << 2)
  285. #define P1SCLMD_ADV_10BT_FDX (1 << 1)
  286. #define P1SCLMD_ADV_10BT_HDX (1 << 0)
  287. #define KS_P1CR 0xF6
  288. #define P1CR_HP_MDIX (1 << 15)
  289. #define P1CR_REV_POL (1 << 13)
  290. #define P1CR_OP_100M (1 << 10)
  291. #define P1CR_OP_FDX (1 << 9)
  292. #define P1CR_OP_MDI (1 << 7)
  293. #define P1CR_AN_DONE (1 << 6)
  294. #define P1CR_LINK_GOOD (1 << 5)
  295. #define P1CR_PNTR_FLOW (1 << 4)
  296. #define P1CR_PNTR_100BT_FDX (1 << 3)
  297. #define P1CR_PNTR_100BT_HDX (1 << 2)
  298. #define P1CR_PNTR_10BT_FDX (1 << 1)
  299. #define P1CR_PNTR_10BT_HDX (1 << 0)
  300. /* TX Frame control */
  301. #define TXFR_TXIC (1 << 15)
  302. #define TXFR_TXFID_MASK (0x3f << 0)
  303. #define TXFR_TXFID_SHIFT (0)
  304. #define KS_P1SR 0xF8
  305. #define P1SR_HP_MDIX (1 << 15)
  306. #define P1SR_REV_POL (1 << 13)
  307. #define P1SR_OP_100M (1 << 10)
  308. #define P1SR_OP_FDX (1 << 9)
  309. #define P1SR_OP_MDI (1 << 7)
  310. #define P1SR_AN_DONE (1 << 6)
  311. #define P1SR_LINK_GOOD (1 << 5)
  312. #define P1SR_PNTR_FLOW (1 << 4)
  313. #define P1SR_PNTR_100BT_FDX (1 << 3)
  314. #define P1SR_PNTR_100BT_HDX (1 << 2)
  315. #define P1SR_PNTR_10BT_FDX (1 << 1)
  316. #define P1SR_PNTR_10BT_HDX (1 << 0)
  317. #define ENUM_BUS_NONE 0
  318. #define ENUM_BUS_8BIT 1
  319. #define ENUM_BUS_16BIT 2
  320. #define ENUM_BUS_32BIT 3
  321. #define MAX_MCAST_LST 32
  322. #define HW_MCAST_SIZE 8
  323. /**
  324. * union ks_tx_hdr - tx header data
  325. * @txb: The header as bytes
  326. * @txw: The header as 16bit, little-endian words
  327. *
  328. * A dual representation of the tx header data to allow
  329. * access to individual bytes, and to allow 16bit accesses
  330. * with 16bit alignment.
  331. */
  332. union ks_tx_hdr {
  333. u8 txb[4];
  334. __le16 txw[2];
  335. };
  336. /**
  337. * struct ks_net - KS8851 driver private data
  338. * @net_device : The network device we're bound to
  339. * @hw_addr : start address of data register.
  340. * @hw_addr_cmd : start address of command register.
  341. * @txh : temporaly buffer to save status/length.
  342. * @lock : Lock to ensure that the device is not accessed when busy.
  343. * @pdev : Pointer to platform device.
  344. * @mii : The MII state information for the mii calls.
  345. * @frame_head_info : frame header information for multi-pkt rx.
  346. * @statelock : Lock on this structure for tx list.
  347. * @msg_enable : The message flags controlling driver output (see ethtool).
  348. * @frame_cnt : number of frames received.
  349. * @bus_width : i/o bus width.
  350. * @rc_rxqcr : Cached copy of KS_RXQCR.
  351. * @rc_txcr : Cached copy of KS_TXCR.
  352. * @rc_ier : Cached copy of KS_IER.
  353. * @sharedbus : Multipex(addr and data bus) mode indicator.
  354. * @cmd_reg_cache : command register cached.
  355. * @cmd_reg_cache_int : command register cached. Used in the irq handler.
  356. * @promiscuous : promiscuous mode indicator.
  357. * @all_mcast : mutlicast indicator.
  358. * @mcast_lst_size : size of multicast list.
  359. * @mcast_lst : multicast list.
  360. * @mcast_bits : multicast enabed.
  361. * @mac_addr : MAC address assigned to this device.
  362. * @fid : frame id.
  363. * @extra_byte : number of extra byte prepended rx pkt.
  364. * @enabled : indicator this device works.
  365. *
  366. * The @lock ensures that the chip is protected when certain operations are
  367. * in progress. When the read or write packet transfer is in progress, most
  368. * of the chip registers are not accessible until the transfer is finished and
  369. * the DMA has been de-asserted.
  370. *
  371. * The @statelock is used to protect information in the structure which may
  372. * need to be accessed via several sources, such as the network driver layer
  373. * or one of the work queues.
  374. *
  375. */
  376. /* Receive multiplex framer header info */
  377. struct type_frame_head {
  378. u16 sts; /* Frame status */
  379. u16 len; /* Byte count */
  380. };
  381. struct ks_net {
  382. struct net_device *netdev;
  383. void __iomem *hw_addr;
  384. void __iomem *hw_addr_cmd;
  385. union ks_tx_hdr txh ____cacheline_aligned;
  386. struct mutex lock; /* spinlock to be interrupt safe */
  387. struct platform_device *pdev;
  388. struct mii_if_info mii;
  389. struct type_frame_head *frame_head_info;
  390. spinlock_t statelock;
  391. u32 msg_enable;
  392. u32 frame_cnt;
  393. int bus_width;
  394. u16 rc_rxqcr;
  395. u16 rc_txcr;
  396. u16 rc_ier;
  397. u16 sharedbus;
  398. u16 cmd_reg_cache;
  399. u16 cmd_reg_cache_int;
  400. u16 promiscuous;
  401. u16 all_mcast;
  402. u16 mcast_lst_size;
  403. u8 mcast_lst[MAX_MCAST_LST][ETH_ALEN];
  404. u8 mcast_bits[HW_MCAST_SIZE];
  405. u8 mac_addr[6];
  406. u8 fid;
  407. u8 extra_byte;
  408. u8 enabled;
  409. };
  410. static int msg_enable;
  411. #define BE3 0x8000 /* Byte Enable 3 */
  412. #define BE2 0x4000 /* Byte Enable 2 */
  413. #define BE1 0x2000 /* Byte Enable 1 */
  414. #define BE0 0x1000 /* Byte Enable 0 */
  415. /* register read/write calls.
  416. *
  417. * All these calls issue transactions to access the chip's registers. They
  418. * all require that the necessary lock is held to prevent accesses when the
  419. * chip is busy transferring packet data (RX/TX FIFO accesses).
  420. */
  421. /**
  422. * ks_check_endian - Check whether endianness of the bus is correct
  423. * @ks : The chip information
  424. *
  425. * The KS8851-16MLL EESK pin allows selecting the endianness of the 16bit
  426. * bus. To maintain optimum performance, the bus endianness should be set
  427. * such that it matches the endianness of the CPU.
  428. */
  429. static int ks_check_endian(struct ks_net *ks)
  430. {
  431. u16 cider;
  432. /*
  433. * Read CIDER register first, however read it the "wrong" way around.
  434. * If the endian strap on the KS8851-16MLL in incorrect and the chip
  435. * is operating in different endianness than the CPU, then the meaning
  436. * of BE[3:0] byte-enable bits is also swapped such that:
  437. * BE[3,2,1,0] becomes BE[1,0,3,2]
  438. *
  439. * Luckily for us, the byte-enable bits are the top four MSbits of
  440. * the address register and the CIDER register is at offset 0xc0.
  441. * Hence, by reading address 0xc0c0, which is not impacted by endian
  442. * swapping, we assert either BE[3:2] or BE[1:0] while reading the
  443. * CIDER register.
  444. *
  445. * If the bus configuration is correct, reading 0xc0c0 asserts
  446. * BE[3:2] and this read returns 0x0000, because to read register
  447. * with bottom two LSbits of address set to 0, BE[1:0] must be
  448. * asserted.
  449. *
  450. * If the bus configuration is NOT correct, reading 0xc0c0 asserts
  451. * BE[1:0] and this read returns non-zero 0x8872 value.
  452. */
  453. iowrite16(BE3 | BE2 | KS_CIDER, ks->hw_addr_cmd);
  454. cider = ioread16(ks->hw_addr);
  455. if (!cider)
  456. return 0;
  457. netdev_err(ks->netdev, "incorrect EESK endian strap setting\n");
  458. return -EINVAL;
  459. }
  460. /**
  461. * ks_rdreg16 - read 16 bit register from device
  462. * @ks : The chip information
  463. * @offset: The register address
  464. *
  465. * Read a 16bit register from the chip, returning the result
  466. */
  467. static u16 ks_rdreg16(struct ks_net *ks, int offset)
  468. {
  469. ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
  470. iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
  471. return ioread16(ks->hw_addr);
  472. }
  473. /**
  474. * ks_wrreg16 - write 16bit register value to chip
  475. * @ks: The chip information
  476. * @offset: The register address
  477. * @value: The value to write
  478. *
  479. */
  480. static void ks_wrreg16(struct ks_net *ks, int offset, u16 value)
  481. {
  482. ks->cmd_reg_cache = (u16)offset | ((BE1 | BE0) << (offset & 0x02));
  483. iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
  484. iowrite16(value, ks->hw_addr);
  485. }
  486. /**
  487. * ks_inblk - read a block of data from QMU. This is called after sudo DMA mode enabled.
  488. * @ks: The chip state
  489. * @wptr: buffer address to save data
  490. * @len: length in byte to read
  491. *
  492. */
  493. static inline void ks_inblk(struct ks_net *ks, u16 *wptr, u32 len)
  494. {
  495. len >>= 1;
  496. while (len--)
  497. *wptr++ = (u16)ioread16(ks->hw_addr);
  498. }
  499. /**
  500. * ks_outblk - write data to QMU. This is called after sudo DMA mode enabled.
  501. * @ks: The chip information
  502. * @wptr: buffer address
  503. * @len: length in byte to write
  504. *
  505. */
  506. static inline void ks_outblk(struct ks_net *ks, u16 *wptr, u32 len)
  507. {
  508. len >>= 1;
  509. while (len--)
  510. iowrite16(*wptr++, ks->hw_addr);
  511. }
  512. static void ks_disable_int(struct ks_net *ks)
  513. {
  514. ks_wrreg16(ks, KS_IER, 0x0000);
  515. } /* ks_disable_int */
  516. static void ks_enable_int(struct ks_net *ks)
  517. {
  518. ks_wrreg16(ks, KS_IER, ks->rc_ier);
  519. } /* ks_enable_int */
  520. /**
  521. * ks_tx_fifo_space - return the available hardware buffer size.
  522. * @ks: The chip information
  523. *
  524. */
  525. static inline u16 ks_tx_fifo_space(struct ks_net *ks)
  526. {
  527. return ks_rdreg16(ks, KS_TXMIR) & 0x1fff;
  528. }
  529. /**
  530. * ks_save_cmd_reg - save the command register from the cache.
  531. * @ks: The chip information
  532. *
  533. */
  534. static inline void ks_save_cmd_reg(struct ks_net *ks)
  535. {
  536. /*ks8851 MLL has a bug to read back the command register.
  537. * So rely on software to save the content of command register.
  538. */
  539. ks->cmd_reg_cache_int = ks->cmd_reg_cache;
  540. }
  541. /**
  542. * ks_restore_cmd_reg - restore the command register from the cache and
  543. * write to hardware register.
  544. * @ks: The chip information
  545. *
  546. */
  547. static inline void ks_restore_cmd_reg(struct ks_net *ks)
  548. {
  549. ks->cmd_reg_cache = ks->cmd_reg_cache_int;
  550. iowrite16(ks->cmd_reg_cache, ks->hw_addr_cmd);
  551. }
  552. /**
  553. * ks_set_powermode - set power mode of the device
  554. * @ks: The chip information
  555. * @pwrmode: The power mode value to write to KS_PMECR.
  556. *
  557. * Change the power mode of the chip.
  558. */
  559. static void ks_set_powermode(struct ks_net *ks, unsigned pwrmode)
  560. {
  561. unsigned pmecr;
  562. netif_dbg(ks, hw, ks->netdev, "setting power mode %d\n", pwrmode);
  563. ks_rdreg16(ks, KS_GRR);
  564. pmecr = ks_rdreg16(ks, KS_PMECR);
  565. pmecr &= ~PMECR_PM_MASK;
  566. pmecr |= pwrmode;
  567. ks_wrreg16(ks, KS_PMECR, pmecr);
  568. }
  569. /**
  570. * ks_read_config - read chip configuration of bus width.
  571. * @ks: The chip information
  572. *
  573. */
  574. static void ks_read_config(struct ks_net *ks)
  575. {
  576. u16 reg_data = 0;
  577. /* Regardless of bus width, 8 bit read should always work.*/
  578. reg_data = ks_rdreg16(ks, KS_CCR);
  579. /* addr/data bus are multiplexed */
  580. ks->sharedbus = (reg_data & CCR_SHARED) == CCR_SHARED;
  581. /* There are garbage data when reading data from QMU,
  582. depending on bus-width.
  583. */
  584. if (reg_data & CCR_8BIT) {
  585. ks->bus_width = ENUM_BUS_8BIT;
  586. ks->extra_byte = 1;
  587. } else if (reg_data & CCR_16BIT) {
  588. ks->bus_width = ENUM_BUS_16BIT;
  589. ks->extra_byte = 2;
  590. } else {
  591. ks->bus_width = ENUM_BUS_32BIT;
  592. ks->extra_byte = 4;
  593. }
  594. }
  595. /**
  596. * ks_soft_reset - issue one of the soft reset to the device
  597. * @ks: The device state.
  598. * @op: The bit(s) to set in the GRR
  599. *
  600. * Issue the relevant soft-reset command to the device's GRR register
  601. * specified by @op.
  602. *
  603. * Note, the delays are in there as a caution to ensure that the reset
  604. * has time to take effect and then complete. Since the datasheet does
  605. * not currently specify the exact sequence, we have chosen something
  606. * that seems to work with our device.
  607. */
  608. static void ks_soft_reset(struct ks_net *ks, unsigned op)
  609. {
  610. /* Disable interrupt first */
  611. ks_wrreg16(ks, KS_IER, 0x0000);
  612. ks_wrreg16(ks, KS_GRR, op);
  613. mdelay(10); /* wait a short time to effect reset */
  614. ks_wrreg16(ks, KS_GRR, 0);
  615. mdelay(1); /* wait for condition to clear */
  616. }
  617. static void ks_enable_qmu(struct ks_net *ks)
  618. {
  619. u16 w;
  620. w = ks_rdreg16(ks, KS_TXCR);
  621. /* Enables QMU Transmit (TXCR). */
  622. ks_wrreg16(ks, KS_TXCR, w | TXCR_TXE);
  623. /*
  624. * RX Frame Count Threshold Enable and Auto-Dequeue RXQ Frame
  625. * Enable
  626. */
  627. w = ks_rdreg16(ks, KS_RXQCR);
  628. ks_wrreg16(ks, KS_RXQCR, w | RXQCR_RXFCTE);
  629. /* Enables QMU Receive (RXCR1). */
  630. w = ks_rdreg16(ks, KS_RXCR1);
  631. ks_wrreg16(ks, KS_RXCR1, w | RXCR1_RXE);
  632. ks->enabled = true;
  633. } /* ks_enable_qmu */
  634. static void ks_disable_qmu(struct ks_net *ks)
  635. {
  636. u16 w;
  637. w = ks_rdreg16(ks, KS_TXCR);
  638. /* Disables QMU Transmit (TXCR). */
  639. w &= ~TXCR_TXE;
  640. ks_wrreg16(ks, KS_TXCR, w);
  641. /* Disables QMU Receive (RXCR1). */
  642. w = ks_rdreg16(ks, KS_RXCR1);
  643. w &= ~RXCR1_RXE ;
  644. ks_wrreg16(ks, KS_RXCR1, w);
  645. ks->enabled = false;
  646. } /* ks_disable_qmu */
  647. /**
  648. * ks_read_qmu - read 1 pkt data from the QMU.
  649. * @ks: The chip information
  650. * @buf: buffer address to save 1 pkt
  651. * @len: Pkt length
  652. * Here is the sequence to read 1 pkt:
  653. * 1. set sudo DMA mode
  654. * 2. read prepend data
  655. * 3. read pkt data
  656. * 4. reset sudo DMA Mode
  657. */
  658. static inline void ks_read_qmu(struct ks_net *ks, u16 *buf, u32 len)
  659. {
  660. u32 r = ks->extra_byte & 0x1 ;
  661. u32 w = ks->extra_byte - r;
  662. /* 1. set sudo DMA mode */
  663. ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
  664. ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  665. /* 2. read prepend data */
  666. /**
  667. * read 4 + extra bytes and discard them.
  668. * extra bytes for dummy, 2 for status, 2 for len
  669. */
  670. /* use likely(r) for 8 bit access for performance */
  671. if (unlikely(r))
  672. ioread8(ks->hw_addr);
  673. ks_inblk(ks, buf, w + 2 + 2);
  674. /* 3. read pkt data */
  675. ks_inblk(ks, buf, ALIGN(len, 4));
  676. /* 4. reset sudo DMA Mode */
  677. ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  678. }
  679. /**
  680. * ks_rcv - read multiple pkts data from the QMU.
  681. * @ks: The chip information
  682. * @netdev: The network device being opened.
  683. *
  684. * Read all of header information before reading pkt content.
  685. * It is not allowed only port of pkts in QMU after issuing
  686. * interrupt ack.
  687. */
  688. static void ks_rcv(struct ks_net *ks, struct net_device *netdev)
  689. {
  690. u32 i;
  691. struct type_frame_head *frame_hdr = ks->frame_head_info;
  692. struct sk_buff *skb;
  693. ks->frame_cnt = ks_rdreg16(ks, KS_RXFCTR) >> 8;
  694. /* read all header information */
  695. for (i = 0; i < ks->frame_cnt; i++) {
  696. /* Checking Received packet status */
  697. frame_hdr->sts = ks_rdreg16(ks, KS_RXFHSR);
  698. /* Get packet len from hardware */
  699. frame_hdr->len = ks_rdreg16(ks, KS_RXFHBCR);
  700. frame_hdr++;
  701. }
  702. frame_hdr = ks->frame_head_info;
  703. while (ks->frame_cnt--) {
  704. if (unlikely(!(frame_hdr->sts & RXFSHR_RXFV) ||
  705. frame_hdr->len >= RX_BUF_SIZE ||
  706. frame_hdr->len <= 0)) {
  707. /* discard an invalid packet */
  708. ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
  709. netdev->stats.rx_dropped++;
  710. if (!(frame_hdr->sts & RXFSHR_RXFV))
  711. netdev->stats.rx_frame_errors++;
  712. else
  713. netdev->stats.rx_length_errors++;
  714. frame_hdr++;
  715. continue;
  716. }
  717. skb = netdev_alloc_skb(netdev, frame_hdr->len + 16);
  718. if (likely(skb)) {
  719. skb_reserve(skb, 2);
  720. /* read data block including CRC 4 bytes */
  721. ks_read_qmu(ks, (u16 *)skb->data, frame_hdr->len);
  722. skb_put(skb, frame_hdr->len - 4);
  723. skb->protocol = eth_type_trans(skb, netdev);
  724. netif_rx(skb);
  725. /* exclude CRC size */
  726. netdev->stats.rx_bytes += frame_hdr->len - 4;
  727. netdev->stats.rx_packets++;
  728. } else {
  729. ks_wrreg16(ks, KS_RXQCR, (ks->rc_rxqcr | RXQCR_RRXEF));
  730. netdev->stats.rx_dropped++;
  731. }
  732. frame_hdr++;
  733. }
  734. }
  735. /**
  736. * ks_update_link_status - link status update.
  737. * @netdev: The network device being opened.
  738. * @ks: The chip information
  739. *
  740. */
  741. static void ks_update_link_status(struct net_device *netdev, struct ks_net *ks)
  742. {
  743. /* check the status of the link */
  744. u32 link_up_status;
  745. if (ks_rdreg16(ks, KS_P1SR) & P1SR_LINK_GOOD) {
  746. netif_carrier_on(netdev);
  747. link_up_status = true;
  748. } else {
  749. netif_carrier_off(netdev);
  750. link_up_status = false;
  751. }
  752. netif_dbg(ks, link, ks->netdev,
  753. "%s: %s\n", __func__, link_up_status ? "UP" : "DOWN");
  754. }
  755. /**
  756. * ks_irq - device interrupt handler
  757. * @irq: Interrupt number passed from the IRQ handler.
  758. * @pw: The private word passed to register_irq(), our struct ks_net.
  759. *
  760. * This is the handler invoked to find out what happened
  761. *
  762. * Read the interrupt status, work out what needs to be done and then clear
  763. * any of the interrupts that are not needed.
  764. */
  765. static irqreturn_t ks_irq(int irq, void *pw)
  766. {
  767. struct net_device *netdev = pw;
  768. struct ks_net *ks = netdev_priv(netdev);
  769. unsigned long flags;
  770. u16 status;
  771. spin_lock_irqsave(&ks->statelock, flags);
  772. /*this should be the first in IRQ handler */
  773. ks_save_cmd_reg(ks);
  774. status = ks_rdreg16(ks, KS_ISR);
  775. if (unlikely(!status)) {
  776. ks_restore_cmd_reg(ks);
  777. spin_unlock_irqrestore(&ks->statelock, flags);
  778. return IRQ_NONE;
  779. }
  780. ks_wrreg16(ks, KS_ISR, status);
  781. if (likely(status & IRQ_RXI))
  782. ks_rcv(ks, netdev);
  783. if (unlikely(status & IRQ_LCI))
  784. ks_update_link_status(netdev, ks);
  785. if (unlikely(status & IRQ_TXI))
  786. netif_wake_queue(netdev);
  787. if (unlikely(status & IRQ_LDI)) {
  788. u16 pmecr = ks_rdreg16(ks, KS_PMECR);
  789. pmecr &= ~PMECR_WKEVT_MASK;
  790. ks_wrreg16(ks, KS_PMECR, pmecr | PMECR_WKEVT_LINK);
  791. }
  792. if (unlikely(status & IRQ_RXOI))
  793. ks->netdev->stats.rx_over_errors++;
  794. /* this should be the last in IRQ handler*/
  795. ks_restore_cmd_reg(ks);
  796. spin_unlock_irqrestore(&ks->statelock, flags);
  797. return IRQ_HANDLED;
  798. }
  799. /**
  800. * ks_net_open - open network device
  801. * @netdev: The network device being opened.
  802. *
  803. * Called when the network device is marked active, such as a user executing
  804. * 'ifconfig up' on the device.
  805. */
  806. static int ks_net_open(struct net_device *netdev)
  807. {
  808. struct ks_net *ks = netdev_priv(netdev);
  809. int err;
  810. #define KS_INT_FLAGS IRQF_TRIGGER_LOW
  811. /* lock the card, even if we may not actually do anything
  812. * else at the moment.
  813. */
  814. netif_dbg(ks, ifup, ks->netdev, "%s - entry\n", __func__);
  815. /* reset the HW */
  816. err = request_irq(netdev->irq, ks_irq, KS_INT_FLAGS, DRV_NAME, netdev);
  817. if (err) {
  818. pr_err("Failed to request IRQ: %d: %d\n", netdev->irq, err);
  819. return err;
  820. }
  821. /* wake up powermode to normal mode */
  822. ks_set_powermode(ks, PMECR_PM_NORMAL);
  823. mdelay(1); /* wait for normal mode to take effect */
  824. ks_wrreg16(ks, KS_ISR, 0xffff);
  825. ks_enable_int(ks);
  826. ks_enable_qmu(ks);
  827. netif_start_queue(ks->netdev);
  828. netif_dbg(ks, ifup, ks->netdev, "network device up\n");
  829. return 0;
  830. }
  831. /**
  832. * ks_net_stop - close network device
  833. * @netdev: The device being closed.
  834. *
  835. * Called to close down a network device which has been active. Cancell any
  836. * work, shutdown the RX and TX process and then place the chip into a low
  837. * power state whilst it is not being used.
  838. */
  839. static int ks_net_stop(struct net_device *netdev)
  840. {
  841. struct ks_net *ks = netdev_priv(netdev);
  842. netif_info(ks, ifdown, netdev, "shutting down\n");
  843. netif_stop_queue(netdev);
  844. mutex_lock(&ks->lock);
  845. /* turn off the IRQs and ack any outstanding */
  846. ks_wrreg16(ks, KS_IER, 0x0000);
  847. ks_wrreg16(ks, KS_ISR, 0xffff);
  848. /* shutdown RX/TX QMU */
  849. ks_disable_qmu(ks);
  850. ks_disable_int(ks);
  851. /* set powermode to soft power down to save power */
  852. ks_set_powermode(ks, PMECR_PM_SOFTDOWN);
  853. free_irq(netdev->irq, netdev);
  854. mutex_unlock(&ks->lock);
  855. return 0;
  856. }
  857. /**
  858. * ks_write_qmu - write 1 pkt data to the QMU.
  859. * @ks: The chip information
  860. * @pdata: buffer address to save 1 pkt
  861. * @len: Pkt length in byte
  862. * Here is the sequence to write 1 pkt:
  863. * 1. set sudo DMA mode
  864. * 2. write status/length
  865. * 3. write pkt data
  866. * 4. reset sudo DMA Mode
  867. * 5. reset sudo DMA mode
  868. * 6. Wait until pkt is out
  869. */
  870. static void ks_write_qmu(struct ks_net *ks, u8 *pdata, u16 len)
  871. {
  872. /* start header at txb[0] to align txw entries */
  873. ks->txh.txw[0] = 0;
  874. ks->txh.txw[1] = cpu_to_le16(len);
  875. /* 1. set sudo-DMA mode */
  876. ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr | RXQCR_SDA);
  877. /* 2. write status/lenth info */
  878. ks_outblk(ks, ks->txh.txw, 4);
  879. /* 3. write pkt data */
  880. ks_outblk(ks, (u16 *)pdata, ALIGN(len, 4));
  881. /* 4. reset sudo-DMA mode */
  882. ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  883. /* 5. Enqueue Tx(move the pkt from TX buffer into TXQ) */
  884. ks_wrreg16(ks, KS_TXQCR, TXQCR_METFE);
  885. /* 6. wait until TXQCR_METFE is auto-cleared */
  886. while (ks_rdreg16(ks, KS_TXQCR) & TXQCR_METFE)
  887. ;
  888. }
  889. /**
  890. * ks_start_xmit - transmit packet
  891. * @skb : The buffer to transmit
  892. * @netdev : The device used to transmit the packet.
  893. *
  894. * Called by the network layer to transmit the @skb.
  895. * spin_lock_irqsave is required because tx and rx should be mutual exclusive.
  896. * So while tx is in-progress, prevent IRQ interrupt from happenning.
  897. */
  898. static netdev_tx_t ks_start_xmit(struct sk_buff *skb, struct net_device *netdev)
  899. {
  900. netdev_tx_t retv = NETDEV_TX_OK;
  901. struct ks_net *ks = netdev_priv(netdev);
  902. unsigned long flags;
  903. spin_lock_irqsave(&ks->statelock, flags);
  904. /* Extra space are required:
  905. * 4 byte for alignment, 4 for status/length, 4 for CRC
  906. */
  907. if (likely(ks_tx_fifo_space(ks) >= skb->len + 12)) {
  908. ks_write_qmu(ks, skb->data, skb->len);
  909. /* add tx statistics */
  910. netdev->stats.tx_bytes += skb->len;
  911. netdev->stats.tx_packets++;
  912. dev_kfree_skb(skb);
  913. } else
  914. retv = NETDEV_TX_BUSY;
  915. spin_unlock_irqrestore(&ks->statelock, flags);
  916. return retv;
  917. }
  918. /**
  919. * ks_start_rx - ready to serve pkts
  920. * @ks : The chip information
  921. *
  922. */
  923. static void ks_start_rx(struct ks_net *ks)
  924. {
  925. u16 cntl;
  926. /* Enables QMU Receive (RXCR1). */
  927. cntl = ks_rdreg16(ks, KS_RXCR1);
  928. cntl |= RXCR1_RXE ;
  929. ks_wrreg16(ks, KS_RXCR1, cntl);
  930. } /* ks_start_rx */
  931. /**
  932. * ks_stop_rx - stop to serve pkts
  933. * @ks : The chip information
  934. *
  935. */
  936. static void ks_stop_rx(struct ks_net *ks)
  937. {
  938. u16 cntl;
  939. /* Disables QMU Receive (RXCR1). */
  940. cntl = ks_rdreg16(ks, KS_RXCR1);
  941. cntl &= ~RXCR1_RXE ;
  942. ks_wrreg16(ks, KS_RXCR1, cntl);
  943. } /* ks_stop_rx */
  944. static unsigned long const ethernet_polynomial = CRC32_POLY_BE;
  945. static unsigned long ether_gen_crc(int length, u8 *data)
  946. {
  947. long crc = -1;
  948. while (--length >= 0) {
  949. u8 current_octet = *data++;
  950. int bit;
  951. for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
  952. crc = (crc << 1) ^
  953. ((crc < 0) ^ (current_octet & 1) ?
  954. ethernet_polynomial : 0);
  955. }
  956. }
  957. return (unsigned long)crc;
  958. } /* ether_gen_crc */
  959. /**
  960. * ks_set_grpaddr - set multicast information
  961. * @ks : The chip information
  962. */
  963. static void ks_set_grpaddr(struct ks_net *ks)
  964. {
  965. u8 i;
  966. u32 index, position, value;
  967. memset(ks->mcast_bits, 0, sizeof(u8) * HW_MCAST_SIZE);
  968. for (i = 0; i < ks->mcast_lst_size; i++) {
  969. position = (ether_gen_crc(6, ks->mcast_lst[i]) >> 26) & 0x3f;
  970. index = position >> 3;
  971. value = 1 << (position & 7);
  972. ks->mcast_bits[index] |= (u8)value;
  973. }
  974. for (i = 0; i < HW_MCAST_SIZE; i++) {
  975. if (i & 1) {
  976. ks_wrreg16(ks, (u16)((KS_MAHTR0 + i) & ~1),
  977. (ks->mcast_bits[i] << 8) |
  978. ks->mcast_bits[i - 1]);
  979. }
  980. }
  981. } /* ks_set_grpaddr */
  982. /**
  983. * ks_clear_mcast - clear multicast information
  984. *
  985. * @ks : The chip information
  986. * This routine removes all mcast addresses set in the hardware.
  987. */
  988. static void ks_clear_mcast(struct ks_net *ks)
  989. {
  990. u16 i, mcast_size;
  991. for (i = 0; i < HW_MCAST_SIZE; i++)
  992. ks->mcast_bits[i] = 0;
  993. mcast_size = HW_MCAST_SIZE >> 2;
  994. for (i = 0; i < mcast_size; i++)
  995. ks_wrreg16(ks, KS_MAHTR0 + (2*i), 0);
  996. }
  997. static void ks_set_promis(struct ks_net *ks, u16 promiscuous_mode)
  998. {
  999. u16 cntl;
  1000. ks->promiscuous = promiscuous_mode;
  1001. ks_stop_rx(ks); /* Stop receiving for reconfiguration */
  1002. cntl = ks_rdreg16(ks, KS_RXCR1);
  1003. cntl &= ~RXCR1_FILTER_MASK;
  1004. if (promiscuous_mode)
  1005. /* Enable Promiscuous mode */
  1006. cntl |= RXCR1_RXAE | RXCR1_RXINVF;
  1007. else
  1008. /* Disable Promiscuous mode (default normal mode) */
  1009. cntl |= RXCR1_RXPAFMA;
  1010. ks_wrreg16(ks, KS_RXCR1, cntl);
  1011. if (ks->enabled)
  1012. ks_start_rx(ks);
  1013. } /* ks_set_promis */
  1014. static void ks_set_mcast(struct ks_net *ks, u16 mcast)
  1015. {
  1016. u16 cntl;
  1017. ks->all_mcast = mcast;
  1018. ks_stop_rx(ks); /* Stop receiving for reconfiguration */
  1019. cntl = ks_rdreg16(ks, KS_RXCR1);
  1020. cntl &= ~RXCR1_FILTER_MASK;
  1021. if (mcast)
  1022. /* Enable "Perfect with Multicast address passed mode" */
  1023. cntl |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
  1024. else
  1025. /**
  1026. * Disable "Perfect with Multicast address passed
  1027. * mode" (normal mode).
  1028. */
  1029. cntl |= RXCR1_RXPAFMA;
  1030. ks_wrreg16(ks, KS_RXCR1, cntl);
  1031. if (ks->enabled)
  1032. ks_start_rx(ks);
  1033. } /* ks_set_mcast */
  1034. static void ks_set_rx_mode(struct net_device *netdev)
  1035. {
  1036. struct ks_net *ks = netdev_priv(netdev);
  1037. struct netdev_hw_addr *ha;
  1038. /* Turn on/off promiscuous mode. */
  1039. if ((netdev->flags & IFF_PROMISC) == IFF_PROMISC)
  1040. ks_set_promis(ks,
  1041. (u16)((netdev->flags & IFF_PROMISC) == IFF_PROMISC));
  1042. /* Turn on/off all mcast mode. */
  1043. else if ((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI)
  1044. ks_set_mcast(ks,
  1045. (u16)((netdev->flags & IFF_ALLMULTI) == IFF_ALLMULTI));
  1046. else
  1047. ks_set_promis(ks, false);
  1048. if ((netdev->flags & IFF_MULTICAST) && netdev_mc_count(netdev)) {
  1049. if (netdev_mc_count(netdev) <= MAX_MCAST_LST) {
  1050. int i = 0;
  1051. netdev_for_each_mc_addr(ha, netdev) {
  1052. if (i >= MAX_MCAST_LST)
  1053. break;
  1054. memcpy(ks->mcast_lst[i++], ha->addr, ETH_ALEN);
  1055. }
  1056. ks->mcast_lst_size = (u8)i;
  1057. ks_set_grpaddr(ks);
  1058. } else {
  1059. /**
  1060. * List too big to support so
  1061. * turn on all mcast mode.
  1062. */
  1063. ks->mcast_lst_size = MAX_MCAST_LST;
  1064. ks_set_mcast(ks, true);
  1065. }
  1066. } else {
  1067. ks->mcast_lst_size = 0;
  1068. ks_clear_mcast(ks);
  1069. }
  1070. } /* ks_set_rx_mode */
  1071. static void ks_set_mac(struct ks_net *ks, u8 *data)
  1072. {
  1073. u16 *pw = (u16 *)data;
  1074. u16 w, u;
  1075. ks_stop_rx(ks); /* Stop receiving for reconfiguration */
  1076. u = *pw++;
  1077. w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
  1078. ks_wrreg16(ks, KS_MARH, w);
  1079. u = *pw++;
  1080. w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
  1081. ks_wrreg16(ks, KS_MARM, w);
  1082. u = *pw;
  1083. w = ((u & 0xFF) << 8) | ((u >> 8) & 0xFF);
  1084. ks_wrreg16(ks, KS_MARL, w);
  1085. memcpy(ks->mac_addr, data, ETH_ALEN);
  1086. if (ks->enabled)
  1087. ks_start_rx(ks);
  1088. }
  1089. static int ks_set_mac_address(struct net_device *netdev, void *paddr)
  1090. {
  1091. struct ks_net *ks = netdev_priv(netdev);
  1092. struct sockaddr *addr = paddr;
  1093. u8 *da;
  1094. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1095. da = (u8 *)netdev->dev_addr;
  1096. ks_set_mac(ks, da);
  1097. return 0;
  1098. }
  1099. static int ks_net_ioctl(struct net_device *netdev, struct ifreq *req, int cmd)
  1100. {
  1101. struct ks_net *ks = netdev_priv(netdev);
  1102. if (!netif_running(netdev))
  1103. return -EINVAL;
  1104. return generic_mii_ioctl(&ks->mii, if_mii(req), cmd, NULL);
  1105. }
  1106. static const struct net_device_ops ks_netdev_ops = {
  1107. .ndo_open = ks_net_open,
  1108. .ndo_stop = ks_net_stop,
  1109. .ndo_do_ioctl = ks_net_ioctl,
  1110. .ndo_start_xmit = ks_start_xmit,
  1111. .ndo_set_mac_address = ks_set_mac_address,
  1112. .ndo_set_rx_mode = ks_set_rx_mode,
  1113. .ndo_validate_addr = eth_validate_addr,
  1114. };
  1115. /* ethtool support */
  1116. static void ks_get_drvinfo(struct net_device *netdev,
  1117. struct ethtool_drvinfo *di)
  1118. {
  1119. strlcpy(di->driver, DRV_NAME, sizeof(di->driver));
  1120. strlcpy(di->version, "1.00", sizeof(di->version));
  1121. strlcpy(di->bus_info, dev_name(netdev->dev.parent),
  1122. sizeof(di->bus_info));
  1123. }
  1124. static u32 ks_get_msglevel(struct net_device *netdev)
  1125. {
  1126. struct ks_net *ks = netdev_priv(netdev);
  1127. return ks->msg_enable;
  1128. }
  1129. static void ks_set_msglevel(struct net_device *netdev, u32 to)
  1130. {
  1131. struct ks_net *ks = netdev_priv(netdev);
  1132. ks->msg_enable = to;
  1133. }
  1134. static int ks_get_link_ksettings(struct net_device *netdev,
  1135. struct ethtool_link_ksettings *cmd)
  1136. {
  1137. struct ks_net *ks = netdev_priv(netdev);
  1138. mii_ethtool_get_link_ksettings(&ks->mii, cmd);
  1139. return 0;
  1140. }
  1141. static int ks_set_link_ksettings(struct net_device *netdev,
  1142. const struct ethtool_link_ksettings *cmd)
  1143. {
  1144. struct ks_net *ks = netdev_priv(netdev);
  1145. return mii_ethtool_set_link_ksettings(&ks->mii, cmd);
  1146. }
  1147. static u32 ks_get_link(struct net_device *netdev)
  1148. {
  1149. struct ks_net *ks = netdev_priv(netdev);
  1150. return mii_link_ok(&ks->mii);
  1151. }
  1152. static int ks_nway_reset(struct net_device *netdev)
  1153. {
  1154. struct ks_net *ks = netdev_priv(netdev);
  1155. return mii_nway_restart(&ks->mii);
  1156. }
  1157. static const struct ethtool_ops ks_ethtool_ops = {
  1158. .get_drvinfo = ks_get_drvinfo,
  1159. .get_msglevel = ks_get_msglevel,
  1160. .set_msglevel = ks_set_msglevel,
  1161. .get_link = ks_get_link,
  1162. .nway_reset = ks_nway_reset,
  1163. .get_link_ksettings = ks_get_link_ksettings,
  1164. .set_link_ksettings = ks_set_link_ksettings,
  1165. };
  1166. /* MII interface controls */
  1167. /**
  1168. * ks_phy_reg - convert MII register into a KS8851 register
  1169. * @reg: MII register number.
  1170. *
  1171. * Return the KS8851 register number for the corresponding MII PHY register
  1172. * if possible. Return zero if the MII register has no direct mapping to the
  1173. * KS8851 register set.
  1174. */
  1175. static int ks_phy_reg(int reg)
  1176. {
  1177. switch (reg) {
  1178. case MII_BMCR:
  1179. return KS_P1MBCR;
  1180. case MII_BMSR:
  1181. return KS_P1MBSR;
  1182. case MII_PHYSID1:
  1183. return KS_PHY1ILR;
  1184. case MII_PHYSID2:
  1185. return KS_PHY1IHR;
  1186. case MII_ADVERTISE:
  1187. return KS_P1ANAR;
  1188. case MII_LPA:
  1189. return KS_P1ANLPR;
  1190. }
  1191. return 0x0;
  1192. }
  1193. /**
  1194. * ks_phy_read - MII interface PHY register read.
  1195. * @netdev: The network device the PHY is on.
  1196. * @phy_addr: Address of PHY (ignored as we only have one)
  1197. * @reg: The register to read.
  1198. *
  1199. * This call reads data from the PHY register specified in @reg. Since the
  1200. * device does not support all the MII registers, the non-existent values
  1201. * are always returned as zero.
  1202. *
  1203. * We return zero for unsupported registers as the MII code does not check
  1204. * the value returned for any error status, and simply returns it to the
  1205. * caller. The mii-tool that the driver was tested with takes any -ve error
  1206. * as real PHY capabilities, thus displaying incorrect data to the user.
  1207. */
  1208. static int ks_phy_read(struct net_device *netdev, int phy_addr, int reg)
  1209. {
  1210. struct ks_net *ks = netdev_priv(netdev);
  1211. int ksreg;
  1212. int result;
  1213. ksreg = ks_phy_reg(reg);
  1214. if (!ksreg)
  1215. return 0x0; /* no error return allowed, so use zero */
  1216. mutex_lock(&ks->lock);
  1217. result = ks_rdreg16(ks, ksreg);
  1218. mutex_unlock(&ks->lock);
  1219. return result;
  1220. }
  1221. static void ks_phy_write(struct net_device *netdev,
  1222. int phy, int reg, int value)
  1223. {
  1224. struct ks_net *ks = netdev_priv(netdev);
  1225. int ksreg;
  1226. ksreg = ks_phy_reg(reg);
  1227. if (ksreg) {
  1228. mutex_lock(&ks->lock);
  1229. ks_wrreg16(ks, ksreg, value);
  1230. mutex_unlock(&ks->lock);
  1231. }
  1232. }
  1233. /**
  1234. * ks_read_selftest - read the selftest memory info.
  1235. * @ks: The device state
  1236. *
  1237. * Read and check the TX/RX memory selftest information.
  1238. */
  1239. static int ks_read_selftest(struct ks_net *ks)
  1240. {
  1241. unsigned both_done = MBIR_TXMBF | MBIR_RXMBF;
  1242. int ret = 0;
  1243. unsigned rd;
  1244. rd = ks_rdreg16(ks, KS_MBIR);
  1245. if ((rd & both_done) != both_done) {
  1246. netdev_warn(ks->netdev, "Memory selftest not finished\n");
  1247. return 0;
  1248. }
  1249. if (rd & MBIR_TXMBFA) {
  1250. netdev_err(ks->netdev, "TX memory selftest fails\n");
  1251. ret |= 1;
  1252. }
  1253. if (rd & MBIR_RXMBFA) {
  1254. netdev_err(ks->netdev, "RX memory selftest fails\n");
  1255. ret |= 2;
  1256. }
  1257. netdev_info(ks->netdev, "the selftest passes\n");
  1258. return ret;
  1259. }
  1260. static void ks_setup(struct ks_net *ks)
  1261. {
  1262. u16 w;
  1263. /**
  1264. * Configure QMU Transmit
  1265. */
  1266. /* Setup Transmit Frame Data Pointer Auto-Increment (TXFDPR) */
  1267. ks_wrreg16(ks, KS_TXFDPR, TXFDPR_TXFPAI);
  1268. /* Setup Receive Frame Data Pointer Auto-Increment */
  1269. ks_wrreg16(ks, KS_RXFDPR, RXFDPR_RXFPAI);
  1270. /* Setup Receive Frame Threshold - 1 frame (RXFCTFC) */
  1271. ks_wrreg16(ks, KS_RXFCTR, 1 & RXFCTR_THRESHOLD_MASK);
  1272. /* Setup RxQ Command Control (RXQCR) */
  1273. ks->rc_rxqcr = RXQCR_CMD_CNTL;
  1274. ks_wrreg16(ks, KS_RXQCR, ks->rc_rxqcr);
  1275. /**
  1276. * set the force mode to half duplex, default is full duplex
  1277. * because if the auto-negotiation fails, most switch uses
  1278. * half-duplex.
  1279. */
  1280. w = ks_rdreg16(ks, KS_P1MBCR);
  1281. w &= ~P1MBCR_FORCE_FDX;
  1282. ks_wrreg16(ks, KS_P1MBCR, w);
  1283. w = TXCR_TXFCE | TXCR_TXPE | TXCR_TXCRC | TXCR_TCGIP;
  1284. ks_wrreg16(ks, KS_TXCR, w);
  1285. w = RXCR1_RXFCE | RXCR1_RXBE | RXCR1_RXUE | RXCR1_RXME | RXCR1_RXIPFCC;
  1286. if (ks->promiscuous) /* bPromiscuous */
  1287. w |= (RXCR1_RXAE | RXCR1_RXINVF);
  1288. else if (ks->all_mcast) /* Multicast address passed mode */
  1289. w |= (RXCR1_RXAE | RXCR1_RXMAFMA | RXCR1_RXPAFMA);
  1290. else /* Normal mode */
  1291. w |= RXCR1_RXPAFMA;
  1292. ks_wrreg16(ks, KS_RXCR1, w);
  1293. } /*ks_setup */
  1294. static void ks_setup_int(struct ks_net *ks)
  1295. {
  1296. ks->rc_ier = 0x00;
  1297. /* Clear the interrupts status of the hardware. */
  1298. ks_wrreg16(ks, KS_ISR, 0xffff);
  1299. /* Enables the interrupts of the hardware. */
  1300. ks->rc_ier = (IRQ_LCI | IRQ_TXI | IRQ_RXI);
  1301. } /* ks_setup_int */
  1302. static int ks_hw_init(struct ks_net *ks)
  1303. {
  1304. #define MHEADER_SIZE (sizeof(struct type_frame_head) * MAX_RECV_FRAMES)
  1305. ks->promiscuous = 0;
  1306. ks->all_mcast = 0;
  1307. ks->mcast_lst_size = 0;
  1308. ks->frame_head_info = devm_kmalloc(&ks->pdev->dev, MHEADER_SIZE,
  1309. GFP_KERNEL);
  1310. if (!ks->frame_head_info)
  1311. return false;
  1312. ks_set_mac(ks, KS_DEFAULT_MAC_ADDRESS);
  1313. return true;
  1314. }
  1315. #if defined(CONFIG_OF)
  1316. static const struct of_device_id ks8851_ml_dt_ids[] = {
  1317. { .compatible = "micrel,ks8851-mll" },
  1318. { /* sentinel */ }
  1319. };
  1320. MODULE_DEVICE_TABLE(of, ks8851_ml_dt_ids);
  1321. #endif
  1322. static int ks8851_probe(struct platform_device *pdev)
  1323. {
  1324. int err;
  1325. struct resource *io_d, *io_c;
  1326. struct net_device *netdev;
  1327. struct ks_net *ks;
  1328. u16 id, data;
  1329. const char *mac;
  1330. netdev = alloc_etherdev(sizeof(struct ks_net));
  1331. if (!netdev)
  1332. return -ENOMEM;
  1333. SET_NETDEV_DEV(netdev, &pdev->dev);
  1334. ks = netdev_priv(netdev);
  1335. ks->netdev = netdev;
  1336. io_d = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1337. ks->hw_addr = devm_ioremap_resource(&pdev->dev, io_d);
  1338. if (IS_ERR(ks->hw_addr)) {
  1339. err = PTR_ERR(ks->hw_addr);
  1340. goto err_free;
  1341. }
  1342. io_c = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1343. ks->hw_addr_cmd = devm_ioremap_resource(&pdev->dev, io_c);
  1344. if (IS_ERR(ks->hw_addr_cmd)) {
  1345. err = PTR_ERR(ks->hw_addr_cmd);
  1346. goto err_free;
  1347. }
  1348. err = ks_check_endian(ks);
  1349. if (err)
  1350. goto err_free;
  1351. netdev->irq = platform_get_irq(pdev, 0);
  1352. if ((int)netdev->irq < 0) {
  1353. err = netdev->irq;
  1354. goto err_free;
  1355. }
  1356. ks->pdev = pdev;
  1357. mutex_init(&ks->lock);
  1358. spin_lock_init(&ks->statelock);
  1359. netdev->netdev_ops = &ks_netdev_ops;
  1360. netdev->ethtool_ops = &ks_ethtool_ops;
  1361. /* setup mii state */
  1362. ks->mii.dev = netdev;
  1363. ks->mii.phy_id = 1,
  1364. ks->mii.phy_id_mask = 1;
  1365. ks->mii.reg_num_mask = 0xf;
  1366. ks->mii.mdio_read = ks_phy_read;
  1367. ks->mii.mdio_write = ks_phy_write;
  1368. netdev_info(netdev, "message enable is %d\n", msg_enable);
  1369. /* set the default message enable */
  1370. ks->msg_enable = netif_msg_init(msg_enable, (NETIF_MSG_DRV |
  1371. NETIF_MSG_PROBE |
  1372. NETIF_MSG_LINK));
  1373. ks_read_config(ks);
  1374. /* simple check for a valid chip being connected to the bus */
  1375. if ((ks_rdreg16(ks, KS_CIDER) & ~CIDER_REV_MASK) != CIDER_ID) {
  1376. netdev_err(netdev, "failed to read device ID\n");
  1377. err = -ENODEV;
  1378. goto err_free;
  1379. }
  1380. if (ks_read_selftest(ks)) {
  1381. netdev_err(netdev, "failed to read device ID\n");
  1382. err = -ENODEV;
  1383. goto err_free;
  1384. }
  1385. err = register_netdev(netdev);
  1386. if (err)
  1387. goto err_free;
  1388. platform_set_drvdata(pdev, netdev);
  1389. ks_soft_reset(ks, GRR_GSR);
  1390. ks_hw_init(ks);
  1391. ks_disable_qmu(ks);
  1392. ks_setup(ks);
  1393. ks_setup_int(ks);
  1394. data = ks_rdreg16(ks, KS_OBCR);
  1395. ks_wrreg16(ks, KS_OBCR, data | OBCR_ODS_16MA);
  1396. /* overwriting the default MAC address */
  1397. if (pdev->dev.of_node) {
  1398. mac = of_get_mac_address(pdev->dev.of_node);
  1399. if (mac)
  1400. memcpy(ks->mac_addr, mac, ETH_ALEN);
  1401. } else {
  1402. struct ks8851_mll_platform_data *pdata;
  1403. pdata = dev_get_platdata(&pdev->dev);
  1404. if (!pdata) {
  1405. netdev_err(netdev, "No platform data\n");
  1406. err = -ENODEV;
  1407. goto err_pdata;
  1408. }
  1409. memcpy(ks->mac_addr, pdata->mac_addr, ETH_ALEN);
  1410. }
  1411. if (!is_valid_ether_addr(ks->mac_addr)) {
  1412. /* Use random MAC address if none passed */
  1413. eth_random_addr(ks->mac_addr);
  1414. netdev_info(netdev, "Using random mac address\n");
  1415. }
  1416. netdev_info(netdev, "Mac address is: %pM\n", ks->mac_addr);
  1417. memcpy(netdev->dev_addr, ks->mac_addr, ETH_ALEN);
  1418. ks_set_mac(ks, netdev->dev_addr);
  1419. id = ks_rdreg16(ks, KS_CIDER);
  1420. netdev_info(netdev, "Found chip, family: 0x%x, id: 0x%x, rev: 0x%x\n",
  1421. (id >> 8) & 0xff, (id >> 4) & 0xf, (id >> 1) & 0x7);
  1422. return 0;
  1423. err_pdata:
  1424. unregister_netdev(netdev);
  1425. err_free:
  1426. free_netdev(netdev);
  1427. return err;
  1428. }
  1429. static int ks8851_remove(struct platform_device *pdev)
  1430. {
  1431. struct net_device *netdev = platform_get_drvdata(pdev);
  1432. unregister_netdev(netdev);
  1433. free_netdev(netdev);
  1434. return 0;
  1435. }
  1436. static struct platform_driver ks8851_platform_driver = {
  1437. .driver = {
  1438. .name = DRV_NAME,
  1439. .of_match_table = of_match_ptr(ks8851_ml_dt_ids),
  1440. },
  1441. .probe = ks8851_probe,
  1442. .remove = ks8851_remove,
  1443. };
  1444. module_platform_driver(ks8851_platform_driver);
  1445. MODULE_DESCRIPTION("KS8851 MLL Network driver");
  1446. MODULE_AUTHOR("David Choi <david.choi@micrel.com>");
  1447. MODULE_LICENSE("GPL");
  1448. module_param_named(message, msg_enable, int, 0);
  1449. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");