ks8851_mll.c 43 KB

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