You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

skge.c 64KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. /*
  2. * iPXE driver for Marvell Yukon chipset and SysKonnect Gigabit
  3. * Ethernet adapters. Derived from Linux skge driver (v1.13), which was
  4. * based on earlier sk98lin, e100 and FreeBSD if_sk drivers.
  5. *
  6. * This driver intentionally does not support all the features of the
  7. * original driver such as link fail-over and link management because
  8. * those should be done at higher levels.
  9. *
  10. * Copyright (C) 2004, 2005 Stephen Hemminger <shemminger@osdl.org>
  11. *
  12. * Modified for iPXE, July 2008 by Michael Decker <mrd999@gmail.com>
  13. * Tested and Modified in December 2009 by
  14. * Thomas Miletich <thomas.miletich@gmail.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  28. */
  29. FILE_LICENCE ( GPL2_ONLY );
  30. #include <stdint.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #include <ipxe/netdevice.h>
  35. #include <ipxe/ethernet.h>
  36. #include <ipxe/if_ether.h>
  37. #include <ipxe/iobuf.h>
  38. #include <ipxe/malloc.h>
  39. #include <ipxe/pci.h>
  40. #include "skge.h"
  41. static struct pci_device_id skge_id_table[] = {
  42. PCI_ROM(0x10b7, 0x1700, "3C940", "3COM 3C940", 0),
  43. PCI_ROM(0x10b7, 0x80eb, "3C940B", "3COM 3C940", 0),
  44. PCI_ROM(0x1148, 0x4300, "GE", "Syskonnect GE", 0),
  45. PCI_ROM(0x1148, 0x4320, "YU", "Syskonnect YU", 0),
  46. PCI_ROM(0x1186, 0x4C00, "DGE510T", "DLink DGE-510T", 0),
  47. PCI_ROM(0x1186, 0x4b01, "DGE530T", "DLink DGE-530T", 0),
  48. PCI_ROM(0x11ab, 0x4320, "id4320", "Marvell id4320", 0),
  49. PCI_ROM(0x11ab, 0x5005, "id5005", "Marvell id5005", 0), /* Belkin */
  50. PCI_ROM(0x1371, 0x434e, "Gigacard", "CNET Gigacard", 0),
  51. PCI_ROM(0x1737, 0x1064, "EG1064", "Linksys EG1064", 0),
  52. PCI_ROM(0x1737, 0xffff, "id_any", "Linksys [any]", 0)
  53. };
  54. static int skge_up(struct net_device *dev);
  55. static void skge_down(struct net_device *dev);
  56. static void skge_tx_clean(struct net_device *dev);
  57. static int xm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val);
  58. static int gm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val);
  59. static void yukon_init(struct skge_hw *hw, int port);
  60. static void genesis_mac_init(struct skge_hw *hw, int port);
  61. static void genesis_link_up(struct skge_port *skge);
  62. static void skge_phyirq(struct skge_hw *hw);
  63. static void skge_poll(struct net_device *dev);
  64. static int skge_xmit_frame(struct net_device *dev, struct io_buffer *iob);
  65. static void skge_net_irq ( struct net_device *dev, int enable );
  66. static void skge_rx_refill(struct net_device *dev);
  67. static struct net_device_operations skge_operations = {
  68. .open = skge_up,
  69. .close = skge_down,
  70. .transmit = skge_xmit_frame,
  71. .poll = skge_poll,
  72. .irq = skge_net_irq
  73. };
  74. /* Avoid conditionals by using array */
  75. static const int txqaddr[] = { Q_XA1, Q_XA2 };
  76. static const int rxqaddr[] = { Q_R1, Q_R2 };
  77. static const u32 rxirqmask[] = { IS_R1_F, IS_R2_F };
  78. static const u32 txirqmask[] = { IS_XA1_F, IS_XA2_F };
  79. static const u32 napimask[] = { IS_R1_F|IS_XA1_F, IS_R2_F|IS_XA2_F };
  80. static const u32 portmask[] = { IS_PORT_1, IS_PORT_2 };
  81. /* Determine supported/advertised modes based on hardware.
  82. * Note: ethtool ADVERTISED_xxx == SUPPORTED_xxx
  83. */
  84. static u32 skge_supported_modes(const struct skge_hw *hw)
  85. {
  86. u32 supported;
  87. if (hw->copper) {
  88. supported = SUPPORTED_10baseT_Half
  89. | SUPPORTED_10baseT_Full
  90. | SUPPORTED_100baseT_Half
  91. | SUPPORTED_100baseT_Full
  92. | SUPPORTED_1000baseT_Half
  93. | SUPPORTED_1000baseT_Full
  94. | SUPPORTED_Autoneg| SUPPORTED_TP;
  95. if (hw->chip_id == CHIP_ID_GENESIS)
  96. supported &= ~(SUPPORTED_10baseT_Half
  97. | SUPPORTED_10baseT_Full
  98. | SUPPORTED_100baseT_Half
  99. | SUPPORTED_100baseT_Full);
  100. else if (hw->chip_id == CHIP_ID_YUKON)
  101. supported &= ~SUPPORTED_1000baseT_Half;
  102. } else
  103. supported = SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half
  104. | SUPPORTED_FIBRE | SUPPORTED_Autoneg;
  105. return supported;
  106. }
  107. /* Chip internal frequency for clock calculations */
  108. static inline u32 hwkhz(const struct skge_hw *hw)
  109. {
  110. return (hw->chip_id == CHIP_ID_GENESIS) ? 53125 : 78125;
  111. }
  112. /* Microseconds to chip HZ */
  113. static inline u32 skge_usecs2clk(const struct skge_hw *hw, u32 usec)
  114. {
  115. return hwkhz(hw) * usec / 1000;
  116. }
  117. enum led_mode { LED_MODE_OFF, LED_MODE_ON, LED_MODE_TST };
  118. static void skge_led(struct skge_port *skge, enum led_mode mode)
  119. {
  120. struct skge_hw *hw = skge->hw;
  121. int port = skge->port;
  122. if (hw->chip_id == CHIP_ID_GENESIS) {
  123. switch (mode) {
  124. case LED_MODE_OFF:
  125. if (hw->phy_type == SK_PHY_BCOM)
  126. xm_phy_write(hw, port, PHY_BCOM_P_EXT_CTRL, PHY_B_PEC_LED_OFF);
  127. else {
  128. skge_write32(hw, SK_REG(port, TX_LED_VAL), 0);
  129. skge_write8(hw, SK_REG(port, TX_LED_CTRL), LED_T_OFF);
  130. }
  131. skge_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_OFF);
  132. skge_write32(hw, SK_REG(port, RX_LED_VAL), 0);
  133. skge_write8(hw, SK_REG(port, RX_LED_CTRL), LED_T_OFF);
  134. break;
  135. case LED_MODE_ON:
  136. skge_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_ON);
  137. skge_write8(hw, SK_REG(port, LNK_LED_REG), LINKLED_LINKSYNC_ON);
  138. skge_write8(hw, SK_REG(port, RX_LED_CTRL), LED_START);
  139. skge_write8(hw, SK_REG(port, TX_LED_CTRL), LED_START);
  140. break;
  141. case LED_MODE_TST:
  142. skge_write8(hw, SK_REG(port, RX_LED_TST), LED_T_ON);
  143. skge_write32(hw, SK_REG(port, RX_LED_VAL), 100);
  144. skge_write8(hw, SK_REG(port, RX_LED_CTRL), LED_START);
  145. if (hw->phy_type == SK_PHY_BCOM)
  146. xm_phy_write(hw, port, PHY_BCOM_P_EXT_CTRL, PHY_B_PEC_LED_ON);
  147. else {
  148. skge_write8(hw, SK_REG(port, TX_LED_TST), LED_T_ON);
  149. skge_write32(hw, SK_REG(port, TX_LED_VAL), 100);
  150. skge_write8(hw, SK_REG(port, TX_LED_CTRL), LED_START);
  151. }
  152. }
  153. } else {
  154. switch (mode) {
  155. case LED_MODE_OFF:
  156. gm_phy_write(hw, port, PHY_MARV_LED_CTRL, 0);
  157. gm_phy_write(hw, port, PHY_MARV_LED_OVER,
  158. PHY_M_LED_MO_DUP(MO_LED_OFF) |
  159. PHY_M_LED_MO_10(MO_LED_OFF) |
  160. PHY_M_LED_MO_100(MO_LED_OFF) |
  161. PHY_M_LED_MO_1000(MO_LED_OFF) |
  162. PHY_M_LED_MO_RX(MO_LED_OFF));
  163. break;
  164. case LED_MODE_ON:
  165. gm_phy_write(hw, port, PHY_MARV_LED_CTRL,
  166. PHY_M_LED_PULS_DUR(PULS_170MS) |
  167. PHY_M_LED_BLINK_RT(BLINK_84MS) |
  168. PHY_M_LEDC_TX_CTRL |
  169. PHY_M_LEDC_DP_CTRL);
  170. gm_phy_write(hw, port, PHY_MARV_LED_OVER,
  171. PHY_M_LED_MO_RX(MO_LED_OFF) |
  172. (skge->speed == SPEED_100 ?
  173. PHY_M_LED_MO_100(MO_LED_ON) : 0));
  174. break;
  175. case LED_MODE_TST:
  176. gm_phy_write(hw, port, PHY_MARV_LED_CTRL, 0);
  177. gm_phy_write(hw, port, PHY_MARV_LED_OVER,
  178. PHY_M_LED_MO_DUP(MO_LED_ON) |
  179. PHY_M_LED_MO_10(MO_LED_ON) |
  180. PHY_M_LED_MO_100(MO_LED_ON) |
  181. PHY_M_LED_MO_1000(MO_LED_ON) |
  182. PHY_M_LED_MO_RX(MO_LED_ON));
  183. }
  184. }
  185. }
  186. /*
  187. * I've left in these EEPROM and VPD functions, as someone may desire to
  188. * integrate them in the future. -mdeck
  189. *
  190. * static int skge_get_eeprom_len(struct net_device *dev)
  191. * {
  192. * struct skge_port *skge = netdev_priv(dev);
  193. * u32 reg2;
  194. *
  195. * pci_read_config_dword(skge->hw->pdev, PCI_DEV_REG2, &reg2);
  196. * return 1 << ( ((reg2 & PCI_VPD_ROM_SZ) >> 14) + 8);
  197. * }
  198. *
  199. * static u32 skge_vpd_read(struct pci_dev *pdev, int cap, u16 offset)
  200. * {
  201. * u32 val;
  202. *
  203. * pci_write_config_word(pdev, cap + PCI_VPD_ADDR, offset);
  204. *
  205. * do {
  206. * pci_read_config_word(pdev, cap + PCI_VPD_ADDR, &offset);
  207. * } while (!(offset & PCI_VPD_ADDR_F));
  208. *
  209. * pci_read_config_dword(pdev, cap + PCI_VPD_DATA, &val);
  210. * return val;
  211. * }
  212. *
  213. * static void skge_vpd_write(struct pci_dev *pdev, int cap, u16 offset, u32 val)
  214. * {
  215. * pci_write_config_dword(pdev, cap + PCI_VPD_DATA, val);
  216. * pci_write_config_word(pdev, cap + PCI_VPD_ADDR,
  217. * offset | PCI_VPD_ADDR_F);
  218. *
  219. * do {
  220. * pci_read_config_word(pdev, cap + PCI_VPD_ADDR, &offset);
  221. * } while (offset & PCI_VPD_ADDR_F);
  222. * }
  223. *
  224. * static int skge_get_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  225. * u8 *data)
  226. * {
  227. * struct skge_port *skge = netdev_priv(dev);
  228. * struct pci_dev *pdev = skge->hw->pdev;
  229. * int cap = pci_find_capability(pdev, PCI_CAP_ID_VPD);
  230. * int length = eeprom->len;
  231. * u16 offset = eeprom->offset;
  232. *
  233. * if (!cap)
  234. * return -EINVAL;
  235. *
  236. * eeprom->magic = SKGE_EEPROM_MAGIC;
  237. *
  238. * while (length > 0) {
  239. * u32 val = skge_vpd_read(pdev, cap, offset);
  240. * int n = min_t(int, length, sizeof(val));
  241. *
  242. * memcpy(data, &val, n);
  243. * length -= n;
  244. * data += n;
  245. * offset += n;
  246. * }
  247. * return 0;
  248. * }
  249. *
  250. * static int skge_set_eeprom(struct net_device *dev, struct ethtool_eeprom *eeprom,
  251. * u8 *data)
  252. * {
  253. * struct skge_port *skge = netdev_priv(dev);
  254. * struct pci_dev *pdev = skge->hw->pdev;
  255. * int cap = pci_find_capability(pdev, PCI_CAP_ID_VPD);
  256. * int length = eeprom->len;
  257. * u16 offset = eeprom->offset;
  258. *
  259. * if (!cap)
  260. * return -EINVAL;
  261. *
  262. * if (eeprom->magic != SKGE_EEPROM_MAGIC)
  263. * return -EINVAL;
  264. *
  265. * while (length > 0) {
  266. * u32 val;
  267. * int n = min_t(int, length, sizeof(val));
  268. *
  269. * if (n < sizeof(val))
  270. * val = skge_vpd_read(pdev, cap, offset);
  271. * memcpy(&val, data, n);
  272. *
  273. * skge_vpd_write(pdev, cap, offset, val);
  274. *
  275. * length -= n;
  276. * data += n;
  277. * offset += n;
  278. * }
  279. * return 0;
  280. * }
  281. */
  282. /*
  283. * Allocate ring elements and chain them together
  284. * One-to-one association of board descriptors with ring elements
  285. */
  286. static int skge_ring_alloc(struct skge_ring *ring, void *vaddr, u32 base,
  287. size_t num)
  288. {
  289. struct skge_tx_desc *d;
  290. struct skge_element *e;
  291. unsigned int i;
  292. ring->start = zalloc(num*sizeof(*e));
  293. if (!ring->start)
  294. return -ENOMEM;
  295. for (i = 0, e = ring->start, d = vaddr; i < num; i++, e++, d++) {
  296. e->desc = d;
  297. if (i == num - 1) {
  298. e->next = ring->start;
  299. d->next_offset = base;
  300. } else {
  301. e->next = e + 1;
  302. d->next_offset = base + (i+1) * sizeof(*d);
  303. }
  304. }
  305. ring->to_use = ring->to_clean = ring->start;
  306. return 0;
  307. }
  308. /* Allocate and setup a new buffer for receiving */
  309. static void skge_rx_setup(struct skge_port *skge __unused,
  310. struct skge_element *e,
  311. struct io_buffer *iob, unsigned int bufsize)
  312. {
  313. struct skge_rx_desc *rd = e->desc;
  314. u64 map;
  315. map = ( iob != NULL ) ? virt_to_bus(iob->data) : 0;
  316. rd->dma_lo = map;
  317. rd->dma_hi = map >> 32;
  318. e->iob = iob;
  319. rd->csum1_start = ETH_HLEN;
  320. rd->csum2_start = ETH_HLEN;
  321. rd->csum1 = 0;
  322. rd->csum2 = 0;
  323. wmb();
  324. rd->control = BMU_OWN | BMU_STF | BMU_IRQ_EOF | BMU_TCP_CHECK | bufsize;
  325. }
  326. /* Resume receiving using existing skb,
  327. * Note: DMA address is not changed by chip.
  328. * MTU not changed while receiver active.
  329. */
  330. static inline void skge_rx_reuse(struct skge_element *e, unsigned int size)
  331. {
  332. struct skge_rx_desc *rd = e->desc;
  333. rd->csum2 = 0;
  334. rd->csum2_start = ETH_HLEN;
  335. wmb();
  336. rd->control = BMU_OWN | BMU_STF | BMU_IRQ_EOF | BMU_TCP_CHECK | size;
  337. }
  338. /* Free all buffers in receive ring, assumes receiver stopped */
  339. static void skge_rx_clean(struct skge_port *skge)
  340. {
  341. struct skge_ring *ring = &skge->rx_ring;
  342. struct skge_element *e;
  343. e = ring->start;
  344. do {
  345. struct skge_rx_desc *rd = e->desc;
  346. rd->control = 0;
  347. if (e->iob) {
  348. free_iob(e->iob);
  349. e->iob = NULL;
  350. }
  351. } while ((e = e->next) != ring->start);
  352. }
  353. static void skge_link_up(struct skge_port *skge)
  354. {
  355. skge_write8(skge->hw, SK_REG(skge->port, LNK_LED_REG),
  356. LED_BLK_OFF|LED_SYNC_OFF|LED_ON);
  357. netdev_link_up(skge->netdev);
  358. DBG2(PFX "%s: Link is up at %d Mbps, %s duplex\n",
  359. skge->netdev->name, skge->speed,
  360. skge->duplex == DUPLEX_FULL ? "full" : "half");
  361. }
  362. static void skge_link_down(struct skge_port *skge)
  363. {
  364. skge_write8(skge->hw, SK_REG(skge->port, LNK_LED_REG), LED_OFF);
  365. netdev_link_down(skge->netdev);
  366. DBG2(PFX "%s: Link is down.\n", skge->netdev->name);
  367. }
  368. static void xm_link_down(struct skge_hw *hw, int port)
  369. {
  370. struct net_device *dev = hw->dev[port];
  371. struct skge_port *skge = netdev_priv(dev);
  372. xm_write16(hw, port, XM_IMSK, XM_IMSK_DISABLE);
  373. if (netdev_link_ok(dev))
  374. skge_link_down(skge);
  375. }
  376. static int __xm_phy_read(struct skge_hw *hw, int port, u16 reg, u16 *val)
  377. {
  378. int i;
  379. xm_write16(hw, port, XM_PHY_ADDR, reg | hw->phy_addr);
  380. *val = xm_read16(hw, port, XM_PHY_DATA);
  381. if (hw->phy_type == SK_PHY_XMAC)
  382. goto ready;
  383. for (i = 0; i < PHY_RETRIES; i++) {
  384. if (xm_read16(hw, port, XM_MMU_CMD) & XM_MMU_PHY_RDY)
  385. goto ready;
  386. udelay(1);
  387. }
  388. return -ETIMEDOUT;
  389. ready:
  390. *val = xm_read16(hw, port, XM_PHY_DATA);
  391. return 0;
  392. }
  393. static u16 xm_phy_read(struct skge_hw *hw, int port, u16 reg)
  394. {
  395. u16 v = 0;
  396. if (__xm_phy_read(hw, port, reg, &v))
  397. DBG(PFX "%s: phy read timed out\n",
  398. hw->dev[port]->name);
  399. return v;
  400. }
  401. static int xm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val)
  402. {
  403. int i;
  404. xm_write16(hw, port, XM_PHY_ADDR, reg | hw->phy_addr);
  405. for (i = 0; i < PHY_RETRIES; i++) {
  406. if (!(xm_read16(hw, port, XM_MMU_CMD) & XM_MMU_PHY_BUSY))
  407. goto ready;
  408. udelay(1);
  409. }
  410. return -EIO;
  411. ready:
  412. xm_write16(hw, port, XM_PHY_DATA, val);
  413. for (i = 0; i < PHY_RETRIES; i++) {
  414. if (!(xm_read16(hw, port, XM_MMU_CMD) & XM_MMU_PHY_BUSY))
  415. return 0;
  416. udelay(1);
  417. }
  418. return -ETIMEDOUT;
  419. }
  420. static void genesis_init(struct skge_hw *hw)
  421. {
  422. /* set blink source counter */
  423. skge_write32(hw, B2_BSC_INI, (SK_BLK_DUR * SK_FACT_53) / 100);
  424. skge_write8(hw, B2_BSC_CTRL, BSC_START);
  425. /* configure mac arbiter */
  426. skge_write16(hw, B3_MA_TO_CTRL, MA_RST_CLR);
  427. /* configure mac arbiter timeout values */
  428. skge_write8(hw, B3_MA_TOINI_RX1, SK_MAC_TO_53);
  429. skge_write8(hw, B3_MA_TOINI_RX2, SK_MAC_TO_53);
  430. skge_write8(hw, B3_MA_TOINI_TX1, SK_MAC_TO_53);
  431. skge_write8(hw, B3_MA_TOINI_TX2, SK_MAC_TO_53);
  432. skge_write8(hw, B3_MA_RCINI_RX1, 0);
  433. skge_write8(hw, B3_MA_RCINI_RX2, 0);
  434. skge_write8(hw, B3_MA_RCINI_TX1, 0);
  435. skge_write8(hw, B3_MA_RCINI_TX2, 0);
  436. /* configure packet arbiter timeout */
  437. skge_write16(hw, B3_PA_CTRL, PA_RST_CLR);
  438. skge_write16(hw, B3_PA_TOINI_RX1, SK_PKT_TO_MAX);
  439. skge_write16(hw, B3_PA_TOINI_TX1, SK_PKT_TO_MAX);
  440. skge_write16(hw, B3_PA_TOINI_RX2, SK_PKT_TO_MAX);
  441. skge_write16(hw, B3_PA_TOINI_TX2, SK_PKT_TO_MAX);
  442. }
  443. static void genesis_reset(struct skge_hw *hw, int port)
  444. {
  445. const u8 zero[8] = { 0 };
  446. u32 reg;
  447. skge_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
  448. /* reset the statistics module */
  449. xm_write32(hw, port, XM_GP_PORT, XM_GP_RES_STAT);
  450. xm_write16(hw, port, XM_IMSK, XM_IMSK_DISABLE);
  451. xm_write32(hw, port, XM_MODE, 0); /* clear Mode Reg */
  452. xm_write16(hw, port, XM_TX_CMD, 0); /* reset TX CMD Reg */
  453. xm_write16(hw, port, XM_RX_CMD, 0); /* reset RX CMD Reg */
  454. /* disable Broadcom PHY IRQ */
  455. if (hw->phy_type == SK_PHY_BCOM)
  456. xm_write16(hw, port, PHY_BCOM_INT_MASK, 0xffff);
  457. xm_outhash(hw, port, XM_HSM, zero);
  458. /* Flush TX and RX fifo */
  459. reg = xm_read32(hw, port, XM_MODE);
  460. xm_write32(hw, port, XM_MODE, reg | XM_MD_FTF);
  461. xm_write32(hw, port, XM_MODE, reg | XM_MD_FRF);
  462. }
  463. /* Convert mode to MII values */
  464. static const u16 phy_pause_map[] = {
  465. [FLOW_MODE_NONE] = 0,
  466. [FLOW_MODE_LOC_SEND] = PHY_AN_PAUSE_ASYM,
  467. [FLOW_MODE_SYMMETRIC] = PHY_AN_PAUSE_CAP,
  468. [FLOW_MODE_SYM_OR_REM] = PHY_AN_PAUSE_CAP | PHY_AN_PAUSE_ASYM,
  469. };
  470. /* special defines for FIBER (88E1011S only) */
  471. static const u16 fiber_pause_map[] = {
  472. [FLOW_MODE_NONE] = PHY_X_P_NO_PAUSE,
  473. [FLOW_MODE_LOC_SEND] = PHY_X_P_ASYM_MD,
  474. [FLOW_MODE_SYMMETRIC] = PHY_X_P_SYM_MD,
  475. [FLOW_MODE_SYM_OR_REM] = PHY_X_P_BOTH_MD,
  476. };
  477. /* Check status of Broadcom phy link */
  478. static void bcom_check_link(struct skge_hw *hw, int port)
  479. {
  480. struct net_device *dev = hw->dev[port];
  481. struct skge_port *skge = netdev_priv(dev);
  482. u16 status;
  483. /* read twice because of latch */
  484. xm_phy_read(hw, port, PHY_BCOM_STAT);
  485. status = xm_phy_read(hw, port, PHY_BCOM_STAT);
  486. if ((status & PHY_ST_LSYNC) == 0) {
  487. xm_link_down(hw, port);
  488. return;
  489. }
  490. if (skge->autoneg == AUTONEG_ENABLE) {
  491. u16 lpa, aux;
  492. if (!(status & PHY_ST_AN_OVER))
  493. return;
  494. lpa = xm_phy_read(hw, port, PHY_XMAC_AUNE_LP);
  495. if (lpa & PHY_B_AN_RF) {
  496. DBG(PFX "%s: remote fault\n",
  497. dev->name);
  498. return;
  499. }
  500. aux = xm_phy_read(hw, port, PHY_BCOM_AUX_STAT);
  501. /* Check Duplex mismatch */
  502. switch (aux & PHY_B_AS_AN_RES_MSK) {
  503. case PHY_B_RES_1000FD:
  504. skge->duplex = DUPLEX_FULL;
  505. break;
  506. case PHY_B_RES_1000HD:
  507. skge->duplex = DUPLEX_HALF;
  508. break;
  509. default:
  510. DBG(PFX "%s: duplex mismatch\n",
  511. dev->name);
  512. return;
  513. }
  514. /* We are using IEEE 802.3z/D5.0 Table 37-4 */
  515. switch (aux & PHY_B_AS_PAUSE_MSK) {
  516. case PHY_B_AS_PAUSE_MSK:
  517. skge->flow_status = FLOW_STAT_SYMMETRIC;
  518. break;
  519. case PHY_B_AS_PRR:
  520. skge->flow_status = FLOW_STAT_REM_SEND;
  521. break;
  522. case PHY_B_AS_PRT:
  523. skge->flow_status = FLOW_STAT_LOC_SEND;
  524. break;
  525. default:
  526. skge->flow_status = FLOW_STAT_NONE;
  527. }
  528. skge->speed = SPEED_1000;
  529. }
  530. if (!netdev_link_ok(dev))
  531. genesis_link_up(skge);
  532. }
  533. /* Broadcom 5400 only supports giagabit! SysKonnect did not put an additional
  534. * Phy on for 100 or 10Mbit operation
  535. */
  536. static void bcom_phy_init(struct skge_port *skge)
  537. {
  538. struct skge_hw *hw = skge->hw;
  539. int port = skge->port;
  540. unsigned int i;
  541. u16 id1, r, ext, ctl;
  542. /* magic workaround patterns for Broadcom */
  543. static const struct {
  544. u16 reg;
  545. u16 val;
  546. } A1hack[] = {
  547. { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1104 },
  548. { 0x17, 0x0013 }, { 0x15, 0x0404 }, { 0x17, 0x8006 },
  549. { 0x15, 0x0132 }, { 0x17, 0x8006 }, { 0x15, 0x0232 },
  550. { 0x17, 0x800D }, { 0x15, 0x000F }, { 0x18, 0x0420 },
  551. }, C0hack[] = {
  552. { 0x18, 0x0c20 }, { 0x17, 0x0012 }, { 0x15, 0x1204 },
  553. { 0x17, 0x0013 }, { 0x15, 0x0A04 }, { 0x18, 0x0420 },
  554. };
  555. /* read Id from external PHY (all have the same address) */
  556. id1 = xm_phy_read(hw, port, PHY_XMAC_ID1);
  557. /* Optimize MDIO transfer by suppressing preamble. */
  558. r = xm_read16(hw, port, XM_MMU_CMD);
  559. r |= XM_MMU_NO_PRE;
  560. xm_write16(hw, port, XM_MMU_CMD,r);
  561. switch (id1) {
  562. case PHY_BCOM_ID1_C0:
  563. /*
  564. * Workaround BCOM Errata for the C0 type.
  565. * Write magic patterns to reserved registers.
  566. */
  567. for (i = 0; i < ARRAY_SIZE(C0hack); i++)
  568. xm_phy_write(hw, port,
  569. C0hack[i].reg, C0hack[i].val);
  570. break;
  571. case PHY_BCOM_ID1_A1:
  572. /*
  573. * Workaround BCOM Errata for the A1 type.
  574. * Write magic patterns to reserved registers.
  575. */
  576. for (i = 0; i < ARRAY_SIZE(A1hack); i++)
  577. xm_phy_write(hw, port,
  578. A1hack[i].reg, A1hack[i].val);
  579. break;
  580. }
  581. /*
  582. * Workaround BCOM Errata (#10523) for all BCom PHYs.
  583. * Disable Power Management after reset.
  584. */
  585. r = xm_phy_read(hw, port, PHY_BCOM_AUX_CTRL);
  586. r |= PHY_B_AC_DIS_PM;
  587. xm_phy_write(hw, port, PHY_BCOM_AUX_CTRL, r);
  588. /* Dummy read */
  589. xm_read16(hw, port, XM_ISRC);
  590. ext = PHY_B_PEC_EN_LTR; /* enable tx led */
  591. ctl = PHY_CT_SP1000; /* always 1000mbit */
  592. if (skge->autoneg == AUTONEG_ENABLE) {
  593. /*
  594. * Workaround BCOM Errata #1 for the C5 type.
  595. * 1000Base-T Link Acquisition Failure in Slave Mode
  596. * Set Repeater/DTE bit 10 of the 1000Base-T Control Register
  597. */
  598. u16 adv = PHY_B_1000C_RD;
  599. if (skge->advertising & ADVERTISED_1000baseT_Half)
  600. adv |= PHY_B_1000C_AHD;
  601. if (skge->advertising & ADVERTISED_1000baseT_Full)
  602. adv |= PHY_B_1000C_AFD;
  603. xm_phy_write(hw, port, PHY_BCOM_1000T_CTRL, adv);
  604. ctl |= PHY_CT_ANE | PHY_CT_RE_CFG;
  605. } else {
  606. if (skge->duplex == DUPLEX_FULL)
  607. ctl |= PHY_CT_DUP_MD;
  608. /* Force to slave */
  609. xm_phy_write(hw, port, PHY_BCOM_1000T_CTRL, PHY_B_1000C_MSE);
  610. }
  611. /* Set autonegotiation pause parameters */
  612. xm_phy_write(hw, port, PHY_BCOM_AUNE_ADV,
  613. phy_pause_map[skge->flow_control] | PHY_AN_CSMA);
  614. xm_phy_write(hw, port, PHY_BCOM_P_EXT_CTRL, ext);
  615. xm_phy_write(hw, port, PHY_BCOM_CTRL, ctl);
  616. /* Use link status change interrupt */
  617. xm_phy_write(hw, port, PHY_BCOM_INT_MASK, PHY_B_DEF_MSK);
  618. }
  619. static void xm_phy_init(struct skge_port *skge)
  620. {
  621. struct skge_hw *hw = skge->hw;
  622. int port = skge->port;
  623. u16 ctrl = 0;
  624. if (skge->autoneg == AUTONEG_ENABLE) {
  625. if (skge->advertising & ADVERTISED_1000baseT_Half)
  626. ctrl |= PHY_X_AN_HD;
  627. if (skge->advertising & ADVERTISED_1000baseT_Full)
  628. ctrl |= PHY_X_AN_FD;
  629. ctrl |= fiber_pause_map[skge->flow_control];
  630. xm_phy_write(hw, port, PHY_XMAC_AUNE_ADV, ctrl);
  631. /* Restart Auto-negotiation */
  632. ctrl = PHY_CT_ANE | PHY_CT_RE_CFG;
  633. } else {
  634. /* Set DuplexMode in Config register */
  635. if (skge->duplex == DUPLEX_FULL)
  636. ctrl |= PHY_CT_DUP_MD;
  637. /*
  638. * Do NOT enable Auto-negotiation here. This would hold
  639. * the link down because no IDLEs are transmitted
  640. */
  641. }
  642. xm_phy_write(hw, port, PHY_XMAC_CTRL, ctrl);
  643. /* Poll PHY for status changes */
  644. skge->use_xm_link_timer = 1;
  645. }
  646. static int xm_check_link(struct net_device *dev)
  647. {
  648. struct skge_port *skge = netdev_priv(dev);
  649. struct skge_hw *hw = skge->hw;
  650. int port = skge->port;
  651. u16 status;
  652. /* read twice because of latch */
  653. xm_phy_read(hw, port, PHY_XMAC_STAT);
  654. status = xm_phy_read(hw, port, PHY_XMAC_STAT);
  655. if ((status & PHY_ST_LSYNC) == 0) {
  656. xm_link_down(hw, port);
  657. return 0;
  658. }
  659. if (skge->autoneg == AUTONEG_ENABLE) {
  660. u16 lpa, res;
  661. if (!(status & PHY_ST_AN_OVER))
  662. return 0;
  663. lpa = xm_phy_read(hw, port, PHY_XMAC_AUNE_LP);
  664. if (lpa & PHY_B_AN_RF) {
  665. DBG(PFX "%s: remote fault\n",
  666. dev->name);
  667. return 0;
  668. }
  669. res = xm_phy_read(hw, port, PHY_XMAC_RES_ABI);
  670. /* Check Duplex mismatch */
  671. switch (res & (PHY_X_RS_HD | PHY_X_RS_FD)) {
  672. case PHY_X_RS_FD:
  673. skge->duplex = DUPLEX_FULL;
  674. break;
  675. case PHY_X_RS_HD:
  676. skge->duplex = DUPLEX_HALF;
  677. break;
  678. default:
  679. DBG(PFX "%s: duplex mismatch\n",
  680. dev->name);
  681. return 0;
  682. }
  683. /* We are using IEEE 802.3z/D5.0 Table 37-4 */
  684. if ((skge->flow_control == FLOW_MODE_SYMMETRIC ||
  685. skge->flow_control == FLOW_MODE_SYM_OR_REM) &&
  686. (lpa & PHY_X_P_SYM_MD))
  687. skge->flow_status = FLOW_STAT_SYMMETRIC;
  688. else if (skge->flow_control == FLOW_MODE_SYM_OR_REM &&
  689. (lpa & PHY_X_RS_PAUSE) == PHY_X_P_ASYM_MD)
  690. /* Enable PAUSE receive, disable PAUSE transmit */
  691. skge->flow_status = FLOW_STAT_REM_SEND;
  692. else if (skge->flow_control == FLOW_MODE_LOC_SEND &&
  693. (lpa & PHY_X_RS_PAUSE) == PHY_X_P_BOTH_MD)
  694. /* Disable PAUSE receive, enable PAUSE transmit */
  695. skge->flow_status = FLOW_STAT_LOC_SEND;
  696. else
  697. skge->flow_status = FLOW_STAT_NONE;
  698. skge->speed = SPEED_1000;
  699. }
  700. if (!netdev_link_ok(dev))
  701. genesis_link_up(skge);
  702. return 1;
  703. }
  704. /* Poll to check for link coming up.
  705. *
  706. * Since internal PHY is wired to a level triggered pin, can't
  707. * get an interrupt when carrier is detected, need to poll for
  708. * link coming up.
  709. */
  710. static void xm_link_timer(struct skge_port *skge)
  711. {
  712. struct net_device *dev = skge->netdev;
  713. struct skge_hw *hw = skge->hw;
  714. int port = skge->port;
  715. int i;
  716. /*
  717. * Verify that the link by checking GPIO register three times.
  718. * This pin has the signal from the link_sync pin connected to it.
  719. */
  720. for (i = 0; i < 3; i++) {
  721. if (xm_read16(hw, port, XM_GP_PORT) & XM_GP_INP_ASS)
  722. return;
  723. }
  724. /* Re-enable interrupt to detect link down */
  725. if (xm_check_link(dev)) {
  726. u16 msk = xm_read16(hw, port, XM_IMSK);
  727. msk &= ~XM_IS_INP_ASS;
  728. xm_write16(hw, port, XM_IMSK, msk);
  729. xm_read16(hw, port, XM_ISRC);
  730. }
  731. }
  732. static void genesis_mac_init(struct skge_hw *hw, int port)
  733. {
  734. struct net_device *dev = hw->dev[port];
  735. struct skge_port *skge = netdev_priv(dev);
  736. int i;
  737. u32 r;
  738. const u8 zero[6] = { 0 };
  739. for (i = 0; i < 10; i++) {
  740. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1),
  741. MFF_SET_MAC_RST);
  742. if (skge_read16(hw, SK_REG(port, TX_MFF_CTRL1)) & MFF_SET_MAC_RST)
  743. goto reset_ok;
  744. udelay(1);
  745. }
  746. DBG(PFX "%s: genesis reset failed\n", dev->name);
  747. reset_ok:
  748. /* Unreset the XMAC. */
  749. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_CLR_MAC_RST);
  750. /*
  751. * Perform additional initialization for external PHYs,
  752. * namely for the 1000baseTX cards that use the XMAC's
  753. * GMII mode.
  754. */
  755. if (hw->phy_type != SK_PHY_XMAC) {
  756. /* Take external Phy out of reset */
  757. r = skge_read32(hw, B2_GP_IO);
  758. if (port == 0)
  759. r |= GP_DIR_0|GP_IO_0;
  760. else
  761. r |= GP_DIR_2|GP_IO_2;
  762. skge_write32(hw, B2_GP_IO, r);
  763. /* Enable GMII interface */
  764. xm_write16(hw, port, XM_HW_CFG, XM_HW_GMII_MD);
  765. }
  766. switch(hw->phy_type) {
  767. case SK_PHY_XMAC:
  768. xm_phy_init(skge);
  769. break;
  770. case SK_PHY_BCOM:
  771. bcom_phy_init(skge);
  772. bcom_check_link(hw, port);
  773. }
  774. /* Set Station Address */
  775. xm_outaddr(hw, port, XM_SA, dev->ll_addr);
  776. /* We don't use match addresses so clear */
  777. for (i = 1; i < 16; i++)
  778. xm_outaddr(hw, port, XM_EXM(i), zero);
  779. /* Clear MIB counters */
  780. xm_write16(hw, port, XM_STAT_CMD,
  781. XM_SC_CLR_RXC | XM_SC_CLR_TXC);
  782. /* Clear two times according to Errata #3 */
  783. xm_write16(hw, port, XM_STAT_CMD,
  784. XM_SC_CLR_RXC | XM_SC_CLR_TXC);
  785. /* configure Rx High Water Mark (XM_RX_HI_WM) */
  786. xm_write16(hw, port, XM_RX_HI_WM, 1450);
  787. /* We don't need the FCS appended to the packet. */
  788. r = XM_RX_LENERR_OK | XM_RX_STRIP_FCS;
  789. if (skge->duplex == DUPLEX_HALF) {
  790. /*
  791. * If in manual half duplex mode the other side might be in
  792. * full duplex mode, so ignore if a carrier extension is not seen
  793. * on frames received
  794. */
  795. r |= XM_RX_DIS_CEXT;
  796. }
  797. xm_write16(hw, port, XM_RX_CMD, r);
  798. /* We want short frames padded to 60 bytes. */
  799. xm_write16(hw, port, XM_TX_CMD, XM_TX_AUTO_PAD);
  800. xm_write16(hw, port, XM_TX_THR, 512);
  801. /*
  802. * Enable the reception of all error frames. This is is
  803. * a necessary evil due to the design of the XMAC. The
  804. * XMAC's receive FIFO is only 8K in size, however jumbo
  805. * frames can be up to 9000 bytes in length. When bad
  806. * frame filtering is enabled, the XMAC's RX FIFO operates
  807. * in 'store and forward' mode. For this to work, the
  808. * entire frame has to fit into the FIFO, but that means
  809. * that jumbo frames larger than 8192 bytes will be
  810. * truncated. Disabling all bad frame filtering causes
  811. * the RX FIFO to operate in streaming mode, in which
  812. * case the XMAC will start transferring frames out of the
  813. * RX FIFO as soon as the FIFO threshold is reached.
  814. */
  815. xm_write32(hw, port, XM_MODE, XM_DEF_MODE);
  816. /*
  817. * Initialize the Receive Counter Event Mask (XM_RX_EV_MSK)
  818. * - Enable all bits excepting 'Octets Rx OK Low CntOv'
  819. * and 'Octets Rx OK Hi Cnt Ov'.
  820. */
  821. xm_write32(hw, port, XM_RX_EV_MSK, XMR_DEF_MSK);
  822. /*
  823. * Initialize the Transmit Counter Event Mask (XM_TX_EV_MSK)
  824. * - Enable all bits excepting 'Octets Tx OK Low CntOv'
  825. * and 'Octets Tx OK Hi Cnt Ov'.
  826. */
  827. xm_write32(hw, port, XM_TX_EV_MSK, XMT_DEF_MSK);
  828. /* Configure MAC arbiter */
  829. skge_write16(hw, B3_MA_TO_CTRL, MA_RST_CLR);
  830. /* configure timeout values */
  831. skge_write8(hw, B3_MA_TOINI_RX1, 72);
  832. skge_write8(hw, B3_MA_TOINI_RX2, 72);
  833. skge_write8(hw, B3_MA_TOINI_TX1, 72);
  834. skge_write8(hw, B3_MA_TOINI_TX2, 72);
  835. skge_write8(hw, B3_MA_RCINI_RX1, 0);
  836. skge_write8(hw, B3_MA_RCINI_RX2, 0);
  837. skge_write8(hw, B3_MA_RCINI_TX1, 0);
  838. skge_write8(hw, B3_MA_RCINI_TX2, 0);
  839. /* Configure Rx MAC FIFO */
  840. skge_write8(hw, SK_REG(port, RX_MFF_CTRL2), MFF_RST_CLR);
  841. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_ENA_TIM_PAT);
  842. skge_write8(hw, SK_REG(port, RX_MFF_CTRL2), MFF_ENA_OP_MD);
  843. /* Configure Tx MAC FIFO */
  844. skge_write8(hw, SK_REG(port, TX_MFF_CTRL2), MFF_RST_CLR);
  845. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_TX_CTRL_DEF);
  846. skge_write8(hw, SK_REG(port, TX_MFF_CTRL2), MFF_ENA_OP_MD);
  847. /* enable timeout timers */
  848. skge_write16(hw, B3_PA_CTRL,
  849. (port == 0) ? PA_ENA_TO_TX1 : PA_ENA_TO_TX2);
  850. }
  851. static void genesis_stop(struct skge_port *skge)
  852. {
  853. struct skge_hw *hw = skge->hw;
  854. int port = skge->port;
  855. unsigned retries = 1000;
  856. u16 cmd;
  857. /* Disable Tx and Rx */
  858. cmd = xm_read16(hw, port, XM_MMU_CMD);
  859. cmd &= ~(XM_MMU_ENA_RX | XM_MMU_ENA_TX);
  860. xm_write16(hw, port, XM_MMU_CMD, cmd);
  861. genesis_reset(hw, port);
  862. /* Clear Tx packet arbiter timeout IRQ */
  863. skge_write16(hw, B3_PA_CTRL,
  864. port == 0 ? PA_CLR_TO_TX1 : PA_CLR_TO_TX2);
  865. /* Reset the MAC */
  866. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_CLR_MAC_RST);
  867. do {
  868. skge_write16(hw, SK_REG(port, TX_MFF_CTRL1), MFF_SET_MAC_RST);
  869. if (!(skge_read16(hw, SK_REG(port, TX_MFF_CTRL1)) & MFF_SET_MAC_RST))
  870. break;
  871. } while (--retries > 0);
  872. /* For external PHYs there must be special handling */
  873. if (hw->phy_type != SK_PHY_XMAC) {
  874. u32 reg = skge_read32(hw, B2_GP_IO);
  875. if (port == 0) {
  876. reg |= GP_DIR_0;
  877. reg &= ~GP_IO_0;
  878. } else {
  879. reg |= GP_DIR_2;
  880. reg &= ~GP_IO_2;
  881. }
  882. skge_write32(hw, B2_GP_IO, reg);
  883. skge_read32(hw, B2_GP_IO);
  884. }
  885. xm_write16(hw, port, XM_MMU_CMD,
  886. xm_read16(hw, port, XM_MMU_CMD)
  887. & ~(XM_MMU_ENA_RX | XM_MMU_ENA_TX));
  888. xm_read16(hw, port, XM_MMU_CMD);
  889. }
  890. static void genesis_link_up(struct skge_port *skge)
  891. {
  892. struct skge_hw *hw = skge->hw;
  893. int port = skge->port;
  894. u16 cmd, msk;
  895. u32 mode;
  896. cmd = xm_read16(hw, port, XM_MMU_CMD);
  897. /*
  898. * enabling pause frame reception is required for 1000BT
  899. * because the XMAC is not reset if the link is going down
  900. */
  901. if (skge->flow_status == FLOW_STAT_NONE ||
  902. skge->flow_status == FLOW_STAT_LOC_SEND)
  903. /* Disable Pause Frame Reception */
  904. cmd |= XM_MMU_IGN_PF;
  905. else
  906. /* Enable Pause Frame Reception */
  907. cmd &= ~XM_MMU_IGN_PF;
  908. xm_write16(hw, port, XM_MMU_CMD, cmd);
  909. mode = xm_read32(hw, port, XM_MODE);
  910. if (skge->flow_status== FLOW_STAT_SYMMETRIC ||
  911. skge->flow_status == FLOW_STAT_LOC_SEND) {
  912. /*
  913. * Configure Pause Frame Generation
  914. * Use internal and external Pause Frame Generation.
  915. * Sending pause frames is edge triggered.
  916. * Send a Pause frame with the maximum pause time if
  917. * internal oder external FIFO full condition occurs.
  918. * Send a zero pause time frame to re-start transmission.
  919. */
  920. /* XM_PAUSE_DA = '010000C28001' (default) */
  921. /* XM_MAC_PTIME = 0xffff (maximum) */
  922. /* remember this value is defined in big endian (!) */
  923. xm_write16(hw, port, XM_MAC_PTIME, 0xffff);
  924. mode |= XM_PAUSE_MODE;
  925. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_ENA_PAUSE);
  926. } else {
  927. /*
  928. * disable pause frame generation is required for 1000BT
  929. * because the XMAC is not reset if the link is going down
  930. */
  931. /* Disable Pause Mode in Mode Register */
  932. mode &= ~XM_PAUSE_MODE;
  933. skge_write16(hw, SK_REG(port, RX_MFF_CTRL1), MFF_DIS_PAUSE);
  934. }
  935. xm_write32(hw, port, XM_MODE, mode);
  936. /* Turn on detection of Tx underrun */
  937. msk = xm_read16(hw, port, XM_IMSK);
  938. msk &= ~XM_IS_TXF_UR;
  939. xm_write16(hw, port, XM_IMSK, msk);
  940. xm_read16(hw, port, XM_ISRC);
  941. /* get MMU Command Reg. */
  942. cmd = xm_read16(hw, port, XM_MMU_CMD);
  943. if (hw->phy_type != SK_PHY_XMAC && skge->duplex == DUPLEX_FULL)
  944. cmd |= XM_MMU_GMII_FD;
  945. /*
  946. * Workaround BCOM Errata (#10523) for all BCom Phys
  947. * Enable Power Management after link up
  948. */
  949. if (hw->phy_type == SK_PHY_BCOM) {
  950. xm_phy_write(hw, port, PHY_BCOM_AUX_CTRL,
  951. xm_phy_read(hw, port, PHY_BCOM_AUX_CTRL)
  952. & ~PHY_B_AC_DIS_PM);
  953. xm_phy_write(hw, port, PHY_BCOM_INT_MASK, PHY_B_DEF_MSK);
  954. }
  955. /* enable Rx/Tx */
  956. xm_write16(hw, port, XM_MMU_CMD,
  957. cmd | XM_MMU_ENA_RX | XM_MMU_ENA_TX);
  958. skge_link_up(skge);
  959. }
  960. static inline void bcom_phy_intr(struct skge_port *skge)
  961. {
  962. struct skge_hw *hw = skge->hw;
  963. int port = skge->port;
  964. u16 isrc;
  965. isrc = xm_phy_read(hw, port, PHY_BCOM_INT_STAT);
  966. DBGIO(PFX "%s: phy interrupt status 0x%x\n",
  967. skge->netdev->name, isrc);
  968. if (isrc & PHY_B_IS_PSE)
  969. DBG(PFX "%s: uncorrectable pair swap error\n",
  970. hw->dev[port]->name);
  971. /* Workaround BCom Errata:
  972. * enable and disable loopback mode if "NO HCD" occurs.
  973. */
  974. if (isrc & PHY_B_IS_NO_HDCL) {
  975. u16 ctrl = xm_phy_read(hw, port, PHY_BCOM_CTRL);
  976. xm_phy_write(hw, port, PHY_BCOM_CTRL,
  977. ctrl | PHY_CT_LOOP);
  978. xm_phy_write(hw, port, PHY_BCOM_CTRL,
  979. ctrl & ~PHY_CT_LOOP);
  980. }
  981. if (isrc & (PHY_B_IS_AN_PR | PHY_B_IS_LST_CHANGE))
  982. bcom_check_link(hw, port);
  983. }
  984. static int gm_phy_write(struct skge_hw *hw, int port, u16 reg, u16 val)
  985. {
  986. int i;
  987. gma_write16(hw, port, GM_SMI_DATA, val);
  988. gma_write16(hw, port, GM_SMI_CTRL,
  989. GM_SMI_CT_PHY_AD(hw->phy_addr) | GM_SMI_CT_REG_AD(reg));
  990. for (i = 0; i < PHY_RETRIES; i++) {
  991. udelay(1);
  992. if (!(gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_BUSY))
  993. return 0;
  994. }
  995. DBG(PFX "%s: phy write timeout port %x reg %x val %x\n",
  996. hw->dev[port]->name,
  997. port, reg, val);
  998. return -EIO;
  999. }
  1000. static int __gm_phy_read(struct skge_hw *hw, int port, u16 reg, u16 *val)
  1001. {
  1002. int i;
  1003. gma_write16(hw, port, GM_SMI_CTRL,
  1004. GM_SMI_CT_PHY_AD(hw->phy_addr)
  1005. | GM_SMI_CT_REG_AD(reg) | GM_SMI_CT_OP_RD);
  1006. for (i = 0; i < PHY_RETRIES; i++) {
  1007. udelay(1);
  1008. if (gma_read16(hw, port, GM_SMI_CTRL) & GM_SMI_CT_RD_VAL)
  1009. goto ready;
  1010. }
  1011. return -ETIMEDOUT;
  1012. ready:
  1013. *val = gma_read16(hw, port, GM_SMI_DATA);
  1014. return 0;
  1015. }
  1016. static u16 gm_phy_read(struct skge_hw *hw, int port, u16 reg)
  1017. {
  1018. u16 v = 0;
  1019. if (__gm_phy_read(hw, port, reg, &v))
  1020. DBG(PFX "%s: phy read timeout port %x reg %x val %x\n",
  1021. hw->dev[port]->name,
  1022. port, reg, v);
  1023. return v;
  1024. }
  1025. /* Marvell Phy Initialization */
  1026. static void yukon_init(struct skge_hw *hw, int port)
  1027. {
  1028. struct skge_port *skge = netdev_priv(hw->dev[port]);
  1029. u16 ctrl, ct1000, adv;
  1030. if (skge->autoneg == AUTONEG_ENABLE) {
  1031. u16 ectrl = gm_phy_read(hw, port, PHY_MARV_EXT_CTRL);
  1032. ectrl &= ~(PHY_M_EC_M_DSC_MSK | PHY_M_EC_S_DSC_MSK |
  1033. PHY_M_EC_MAC_S_MSK);
  1034. ectrl |= PHY_M_EC_MAC_S(MAC_TX_CLK_25_MHZ);
  1035. ectrl |= PHY_M_EC_M_DSC(0) | PHY_M_EC_S_DSC(1);
  1036. gm_phy_write(hw, port, PHY_MARV_EXT_CTRL, ectrl);
  1037. }
  1038. ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
  1039. if (skge->autoneg == AUTONEG_DISABLE)
  1040. ctrl &= ~PHY_CT_ANE;
  1041. ctrl |= PHY_CT_RESET;
  1042. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1043. ctrl = 0;
  1044. ct1000 = 0;
  1045. adv = PHY_AN_CSMA;
  1046. if (skge->autoneg == AUTONEG_ENABLE) {
  1047. if (hw->copper) {
  1048. if (skge->advertising & ADVERTISED_1000baseT_Full)
  1049. ct1000 |= PHY_M_1000C_AFD;
  1050. if (skge->advertising & ADVERTISED_1000baseT_Half)
  1051. ct1000 |= PHY_M_1000C_AHD;
  1052. if (skge->advertising & ADVERTISED_100baseT_Full)
  1053. adv |= PHY_M_AN_100_FD;
  1054. if (skge->advertising & ADVERTISED_100baseT_Half)
  1055. adv |= PHY_M_AN_100_HD;
  1056. if (skge->advertising & ADVERTISED_10baseT_Full)
  1057. adv |= PHY_M_AN_10_FD;
  1058. if (skge->advertising & ADVERTISED_10baseT_Half)
  1059. adv |= PHY_M_AN_10_HD;
  1060. /* Set Flow-control capabilities */
  1061. adv |= phy_pause_map[skge->flow_control];
  1062. } else {
  1063. if (skge->advertising & ADVERTISED_1000baseT_Full)
  1064. adv |= PHY_M_AN_1000X_AFD;
  1065. if (skge->advertising & ADVERTISED_1000baseT_Half)
  1066. adv |= PHY_M_AN_1000X_AHD;
  1067. adv |= fiber_pause_map[skge->flow_control];
  1068. }
  1069. /* Restart Auto-negotiation */
  1070. ctrl |= PHY_CT_ANE | PHY_CT_RE_CFG;
  1071. } else {
  1072. /* forced speed/duplex settings */
  1073. ct1000 = PHY_M_1000C_MSE;
  1074. if (skge->duplex == DUPLEX_FULL)
  1075. ctrl |= PHY_CT_DUP_MD;
  1076. switch (skge->speed) {
  1077. case SPEED_1000:
  1078. ctrl |= PHY_CT_SP1000;
  1079. break;
  1080. case SPEED_100:
  1081. ctrl |= PHY_CT_SP100;
  1082. break;
  1083. }
  1084. ctrl |= PHY_CT_RESET;
  1085. }
  1086. gm_phy_write(hw, port, PHY_MARV_1000T_CTRL, ct1000);
  1087. gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, adv);
  1088. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1089. /* Enable phy interrupt on autonegotiation complete (or link up) */
  1090. if (skge->autoneg == AUTONEG_ENABLE)
  1091. gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_AN_MSK);
  1092. else
  1093. gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_DEF_MSK);
  1094. }
  1095. static void yukon_reset(struct skge_hw *hw, int port)
  1096. {
  1097. gm_phy_write(hw, port, PHY_MARV_INT_MASK, 0);/* disable PHY IRQs */
  1098. gma_write16(hw, port, GM_MC_ADDR_H1, 0); /* clear MC hash */
  1099. gma_write16(hw, port, GM_MC_ADDR_H2, 0);
  1100. gma_write16(hw, port, GM_MC_ADDR_H3, 0);
  1101. gma_write16(hw, port, GM_MC_ADDR_H4, 0);
  1102. gma_write16(hw, port, GM_RX_CTRL,
  1103. gma_read16(hw, port, GM_RX_CTRL)
  1104. | GM_RXCR_UCF_ENA | GM_RXCR_MCF_ENA);
  1105. }
  1106. /* Apparently, early versions of Yukon-Lite had wrong chip_id? */
  1107. static int is_yukon_lite_a0(struct skge_hw *hw)
  1108. {
  1109. u32 reg;
  1110. int ret;
  1111. if (hw->chip_id != CHIP_ID_YUKON)
  1112. return 0;
  1113. reg = skge_read32(hw, B2_FAR);
  1114. skge_write8(hw, B2_FAR + 3, 0xff);
  1115. ret = (skge_read8(hw, B2_FAR + 3) != 0);
  1116. skge_write32(hw, B2_FAR, reg);
  1117. return ret;
  1118. }
  1119. static void yukon_mac_init(struct skge_hw *hw, int port)
  1120. {
  1121. struct skge_port *skge = netdev_priv(hw->dev[port]);
  1122. int i;
  1123. u32 reg;
  1124. const u8 *addr = hw->dev[port]->ll_addr;
  1125. /* WA code for COMA mode -- set PHY reset */
  1126. if (hw->chip_id == CHIP_ID_YUKON_LITE &&
  1127. hw->chip_rev >= CHIP_REV_YU_LITE_A3) {
  1128. reg = skge_read32(hw, B2_GP_IO);
  1129. reg |= GP_DIR_9 | GP_IO_9;
  1130. skge_write32(hw, B2_GP_IO, reg);
  1131. }
  1132. /* hard reset */
  1133. skge_write32(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
  1134. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
  1135. /* WA code for COMA mode -- clear PHY reset */
  1136. if (hw->chip_id == CHIP_ID_YUKON_LITE &&
  1137. hw->chip_rev >= CHIP_REV_YU_LITE_A3) {
  1138. reg = skge_read32(hw, B2_GP_IO);
  1139. reg |= GP_DIR_9;
  1140. reg &= ~GP_IO_9;
  1141. skge_write32(hw, B2_GP_IO, reg);
  1142. }
  1143. /* Set hardware config mode */
  1144. reg = GPC_INT_POL_HI | GPC_DIS_FC | GPC_DIS_SLEEP |
  1145. GPC_ENA_XC | GPC_ANEG_ADV_ALL_M | GPC_ENA_PAUSE;
  1146. reg |= hw->copper ? GPC_HWCFG_GMII_COP : GPC_HWCFG_GMII_FIB;
  1147. /* Clear GMC reset */
  1148. skge_write32(hw, SK_REG(port, GPHY_CTRL), reg | GPC_RST_SET);
  1149. skge_write32(hw, SK_REG(port, GPHY_CTRL), reg | GPC_RST_CLR);
  1150. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON | GMC_RST_CLR);
  1151. if (skge->autoneg == AUTONEG_DISABLE) {
  1152. reg = GM_GPCR_AU_ALL_DIS;
  1153. gma_write16(hw, port, GM_GP_CTRL,
  1154. gma_read16(hw, port, GM_GP_CTRL) | reg);
  1155. switch (skge->speed) {
  1156. case SPEED_1000:
  1157. reg &= ~GM_GPCR_SPEED_100;
  1158. reg |= GM_GPCR_SPEED_1000;
  1159. break;
  1160. case SPEED_100:
  1161. reg &= ~GM_GPCR_SPEED_1000;
  1162. reg |= GM_GPCR_SPEED_100;
  1163. break;
  1164. case SPEED_10:
  1165. reg &= ~(GM_GPCR_SPEED_1000 | GM_GPCR_SPEED_100);
  1166. break;
  1167. }
  1168. if (skge->duplex == DUPLEX_FULL)
  1169. reg |= GM_GPCR_DUP_FULL;
  1170. } else
  1171. reg = GM_GPCR_SPEED_1000 | GM_GPCR_SPEED_100 | GM_GPCR_DUP_FULL;
  1172. switch (skge->flow_control) {
  1173. case FLOW_MODE_NONE:
  1174. skge_write32(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
  1175. reg |= GM_GPCR_FC_TX_DIS | GM_GPCR_FC_RX_DIS | GM_GPCR_AU_FCT_DIS;
  1176. break;
  1177. case FLOW_MODE_LOC_SEND:
  1178. /* disable Rx flow-control */
  1179. reg |= GM_GPCR_FC_RX_DIS | GM_GPCR_AU_FCT_DIS;
  1180. break;
  1181. case FLOW_MODE_SYMMETRIC:
  1182. case FLOW_MODE_SYM_OR_REM:
  1183. /* enable Tx & Rx flow-control */
  1184. break;
  1185. }
  1186. gma_write16(hw, port, GM_GP_CTRL, reg);
  1187. skge_read16(hw, SK_REG(port, GMAC_IRQ_SRC));
  1188. yukon_init(hw, port);
  1189. /* MIB clear */
  1190. reg = gma_read16(hw, port, GM_PHY_ADDR);
  1191. gma_write16(hw, port, GM_PHY_ADDR, reg | GM_PAR_MIB_CLR);
  1192. for (i = 0; i < GM_MIB_CNT_SIZE; i++)
  1193. gma_read16(hw, port, GM_MIB_CNT_BASE + 8*i);
  1194. gma_write16(hw, port, GM_PHY_ADDR, reg);
  1195. /* transmit control */
  1196. gma_write16(hw, port, GM_TX_CTRL, TX_COL_THR(TX_COL_DEF));
  1197. /* receive control reg: unicast + multicast + no FCS */
  1198. gma_write16(hw, port, GM_RX_CTRL,
  1199. GM_RXCR_UCF_ENA | GM_RXCR_CRC_DIS | GM_RXCR_MCF_ENA);
  1200. /* transmit flow control */
  1201. gma_write16(hw, port, GM_TX_FLOW_CTRL, 0xffff);
  1202. /* transmit parameter */
  1203. gma_write16(hw, port, GM_TX_PARAM,
  1204. TX_JAM_LEN_VAL(TX_JAM_LEN_DEF) |
  1205. TX_JAM_IPG_VAL(TX_JAM_IPG_DEF) |
  1206. TX_IPG_JAM_DATA(TX_IPG_JAM_DEF));
  1207. /* configure the Serial Mode Register */
  1208. reg = DATA_BLIND_VAL(DATA_BLIND_DEF)
  1209. | GM_SMOD_VLAN_ENA
  1210. | IPG_DATA_VAL(IPG_DATA_DEF);
  1211. gma_write16(hw, port, GM_SERIAL_MODE, reg);
  1212. /* physical address: used for pause frames */
  1213. gma_set_addr(hw, port, GM_SRC_ADDR_1L, addr);
  1214. /* virtual address for data */
  1215. gma_set_addr(hw, port, GM_SRC_ADDR_2L, addr);
  1216. /* enable interrupt mask for counter overflows */
  1217. gma_write16(hw, port, GM_TX_IRQ_MSK, 0);
  1218. gma_write16(hw, port, GM_RX_IRQ_MSK, 0);
  1219. gma_write16(hw, port, GM_TR_IRQ_MSK, 0);
  1220. /* Initialize Mac Fifo */
  1221. /* Configure Rx MAC FIFO */
  1222. skge_write16(hw, SK_REG(port, RX_GMF_FL_MSK), RX_FF_FL_DEF_MSK);
  1223. reg = GMF_OPER_ON | GMF_RX_F_FL_ON;
  1224. /* disable Rx GMAC FIFO Flush for YUKON-Lite Rev. A0 only */
  1225. if (is_yukon_lite_a0(hw))
  1226. reg &= ~GMF_RX_F_FL_ON;
  1227. skge_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_CLR);
  1228. skge_write16(hw, SK_REG(port, RX_GMF_CTRL_T), reg);
  1229. /*
  1230. * because Pause Packet Truncation in GMAC is not working
  1231. * we have to increase the Flush Threshold to 64 bytes
  1232. * in order to flush pause packets in Rx FIFO on Yukon-1
  1233. */
  1234. skge_write16(hw, SK_REG(port, RX_GMF_FL_THR), RX_GMF_FL_THR_DEF+1);
  1235. /* Configure Tx MAC FIFO */
  1236. skge_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_CLR);
  1237. skge_write16(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_OPER_ON);
  1238. }
  1239. /* Go into power down mode */
  1240. static void yukon_suspend(struct skge_hw *hw, int port)
  1241. {
  1242. u16 ctrl;
  1243. ctrl = gm_phy_read(hw, port, PHY_MARV_PHY_CTRL);
  1244. ctrl |= PHY_M_PC_POL_R_DIS;
  1245. gm_phy_write(hw, port, PHY_MARV_PHY_CTRL, ctrl);
  1246. ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
  1247. ctrl |= PHY_CT_RESET;
  1248. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1249. /* switch IEEE compatible power down mode on */
  1250. ctrl = gm_phy_read(hw, port, PHY_MARV_CTRL);
  1251. ctrl |= PHY_CT_PDOWN;
  1252. gm_phy_write(hw, port, PHY_MARV_CTRL, ctrl);
  1253. }
  1254. static void yukon_stop(struct skge_port *skge)
  1255. {
  1256. struct skge_hw *hw = skge->hw;
  1257. int port = skge->port;
  1258. skge_write8(hw, SK_REG(port, GMAC_IRQ_MSK), 0);
  1259. yukon_reset(hw, port);
  1260. gma_write16(hw, port, GM_GP_CTRL,
  1261. gma_read16(hw, port, GM_GP_CTRL)
  1262. & ~(GM_GPCR_TX_ENA|GM_GPCR_RX_ENA));
  1263. gma_read16(hw, port, GM_GP_CTRL);
  1264. yukon_suspend(hw, port);
  1265. /* set GPHY Control reset */
  1266. skge_write8(hw, SK_REG(port, GPHY_CTRL), GPC_RST_SET);
  1267. skge_write8(hw, SK_REG(port, GMAC_CTRL), GMC_RST_SET);
  1268. }
  1269. static u16 yukon_speed(const struct skge_hw *hw __unused, u16 aux)
  1270. {
  1271. switch (aux & PHY_M_PS_SPEED_MSK) {
  1272. case PHY_M_PS_SPEED_1000:
  1273. return SPEED_1000;
  1274. case PHY_M_PS_SPEED_100:
  1275. return SPEED_100;
  1276. default:
  1277. return SPEED_10;
  1278. }
  1279. }
  1280. static void yukon_link_up(struct skge_port *skge)
  1281. {
  1282. struct skge_hw *hw = skge->hw;
  1283. int port = skge->port;
  1284. u16 reg;
  1285. /* Enable Transmit FIFO Underrun */
  1286. skge_write8(hw, SK_REG(port, GMAC_IRQ_MSK), GMAC_DEF_MSK);
  1287. reg = gma_read16(hw, port, GM_GP_CTRL);
  1288. if (skge->duplex == DUPLEX_FULL || skge->autoneg == AUTONEG_ENABLE)
  1289. reg |= GM_GPCR_DUP_FULL;
  1290. /* enable Rx/Tx */
  1291. reg |= GM_GPCR_RX_ENA | GM_GPCR_TX_ENA;
  1292. gma_write16(hw, port, GM_GP_CTRL, reg);
  1293. gm_phy_write(hw, port, PHY_MARV_INT_MASK, PHY_M_IS_DEF_MSK);
  1294. skge_link_up(skge);
  1295. }
  1296. static void yukon_link_down(struct skge_port *skge)
  1297. {
  1298. struct skge_hw *hw = skge->hw;
  1299. int port = skge->port;
  1300. u16 ctrl;
  1301. ctrl = gma_read16(hw, port, GM_GP_CTRL);
  1302. ctrl &= ~(GM_GPCR_RX_ENA | GM_GPCR_TX_ENA);
  1303. gma_write16(hw, port, GM_GP_CTRL, ctrl);
  1304. if (skge->flow_status == FLOW_STAT_REM_SEND) {
  1305. ctrl = gm_phy_read(hw, port, PHY_MARV_AUNE_ADV);
  1306. ctrl |= PHY_M_AN_ASP;
  1307. /* restore Asymmetric Pause bit */
  1308. gm_phy_write(hw, port, PHY_MARV_AUNE_ADV, ctrl);
  1309. }
  1310. skge_link_down(skge);
  1311. yukon_init(hw, port);
  1312. }
  1313. static void yukon_phy_intr(struct skge_port *skge)
  1314. {
  1315. struct skge_hw *hw = skge->hw;
  1316. int port = skge->port;
  1317. const char *reason = NULL;
  1318. u16 istatus, phystat;
  1319. istatus = gm_phy_read(hw, port, PHY_MARV_INT_STAT);
  1320. phystat = gm_phy_read(hw, port, PHY_MARV_PHY_STAT);
  1321. DBGIO(PFX "%s: phy interrupt status 0x%x 0x%x\n",
  1322. skge->netdev->name, istatus, phystat);
  1323. if (istatus & PHY_M_IS_AN_COMPL) {
  1324. if (gm_phy_read(hw, port, PHY_MARV_AUNE_LP)
  1325. & PHY_M_AN_RF) {
  1326. reason = "remote fault";
  1327. goto failed;
  1328. }
  1329. if (gm_phy_read(hw, port, PHY_MARV_1000T_STAT) & PHY_B_1000S_MSF) {
  1330. reason = "master/slave fault";
  1331. goto failed;
  1332. }
  1333. if (!(phystat & PHY_M_PS_SPDUP_RES)) {
  1334. reason = "speed/duplex";
  1335. goto failed;
  1336. }
  1337. skge->duplex = (phystat & PHY_M_PS_FULL_DUP)
  1338. ? DUPLEX_FULL : DUPLEX_HALF;
  1339. skge->speed = yukon_speed(hw, phystat);
  1340. /* We are using IEEE 802.3z/D5.0 Table 37-4 */
  1341. switch (phystat & PHY_M_PS_PAUSE_MSK) {
  1342. case PHY_M_PS_PAUSE_MSK:
  1343. skge->flow_status = FLOW_STAT_SYMMETRIC;
  1344. break;
  1345. case PHY_M_PS_RX_P_EN:
  1346. skge->flow_status = FLOW_STAT_REM_SEND;
  1347. break;
  1348. case PHY_M_PS_TX_P_EN:
  1349. skge->flow_status = FLOW_STAT_LOC_SEND;
  1350. break;
  1351. default:
  1352. skge->flow_status = FLOW_STAT_NONE;
  1353. }
  1354. if (skge->flow_status == FLOW_STAT_NONE ||
  1355. (skge->speed < SPEED_1000 && skge->duplex == DUPLEX_HALF))
  1356. skge_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_OFF);
  1357. else
  1358. skge_write8(hw, SK_REG(port, GMAC_CTRL), GMC_PAUSE_ON);
  1359. yukon_link_up(skge);
  1360. return;
  1361. }
  1362. if (istatus & PHY_M_IS_LSP_CHANGE)
  1363. skge->speed = yukon_speed(hw, phystat);
  1364. if (istatus & PHY_M_IS_DUP_CHANGE)
  1365. skge->duplex = (phystat & PHY_M_PS_FULL_DUP) ? DUPLEX_FULL : DUPLEX_HALF;
  1366. if (istatus & PHY_M_IS_LST_CHANGE) {
  1367. if (phystat & PHY_M_PS_LINK_UP)
  1368. yukon_link_up(skge);
  1369. else
  1370. yukon_link_down(skge);
  1371. }
  1372. return;
  1373. failed:
  1374. DBG(PFX "%s: autonegotiation failed (%s)\n",
  1375. skge->netdev->name, reason);
  1376. /* XXX restart autonegotiation? */
  1377. }
  1378. static void skge_ramset(struct skge_hw *hw, u16 q, u32 start, size_t len)
  1379. {
  1380. u32 end;
  1381. start /= 8;
  1382. len /= 8;
  1383. end = start + len - 1;
  1384. skge_write8(hw, RB_ADDR(q, RB_CTRL), RB_RST_CLR);
  1385. skge_write32(hw, RB_ADDR(q, RB_START), start);
  1386. skge_write32(hw, RB_ADDR(q, RB_WP), start);
  1387. skge_write32(hw, RB_ADDR(q, RB_RP), start);
  1388. skge_write32(hw, RB_ADDR(q, RB_END), end);
  1389. if (q == Q_R1 || q == Q_R2) {
  1390. /* Set thresholds on receive queue's */
  1391. skge_write32(hw, RB_ADDR(q, RB_RX_UTPP),
  1392. start + (2*len)/3);
  1393. skge_write32(hw, RB_ADDR(q, RB_RX_LTPP),
  1394. start + (len/3));
  1395. } else {
  1396. /* Enable store & forward on Tx queue's because
  1397. * Tx FIFO is only 4K on Genesis and 1K on Yukon
  1398. */
  1399. skge_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_STFWD);
  1400. }
  1401. skge_write8(hw, RB_ADDR(q, RB_CTRL), RB_ENA_OP_MD);
  1402. }
  1403. /* Setup Bus Memory Interface */
  1404. static void skge_qset(struct skge_port *skge, u16 q,
  1405. const struct skge_element *e)
  1406. {
  1407. struct skge_hw *hw = skge->hw;
  1408. u32 watermark = 0x600;
  1409. u64 base = skge->dma + (e->desc - skge->mem);
  1410. /* optimization to reduce window on 32bit/33mhz */
  1411. if ((skge_read16(hw, B0_CTST) & (CS_BUS_CLOCK | CS_BUS_SLOT_SZ)) == 0)
  1412. watermark /= 2;
  1413. skge_write32(hw, Q_ADDR(q, Q_CSR), CSR_CLR_RESET);
  1414. skge_write32(hw, Q_ADDR(q, Q_F), watermark);
  1415. skge_write32(hw, Q_ADDR(q, Q_DA_H), (u32)(base >> 32));
  1416. skge_write32(hw, Q_ADDR(q, Q_DA_L), (u32)base);
  1417. }
  1418. void skge_free(struct net_device *dev)
  1419. {
  1420. struct skge_port *skge = netdev_priv(dev);
  1421. free(skge->rx_ring.start);
  1422. skge->rx_ring.start = NULL;
  1423. free(skge->tx_ring.start);
  1424. skge->tx_ring.start = NULL;
  1425. free_dma(skge->mem, RING_SIZE);
  1426. skge->mem = NULL;
  1427. skge->dma = 0;
  1428. }
  1429. static int skge_up(struct net_device *dev)
  1430. {
  1431. struct skge_port *skge = netdev_priv(dev);
  1432. struct skge_hw *hw = skge->hw;
  1433. int port = skge->port;
  1434. u32 chunk, ram_addr;
  1435. int err;
  1436. DBG2(PFX "%s: enabling interface\n", dev->name);
  1437. skge->mem = malloc_dma(RING_SIZE, SKGE_RING_ALIGN);
  1438. skge->dma = virt_to_bus(skge->mem);
  1439. if (!skge->mem)
  1440. return -ENOMEM;
  1441. memset(skge->mem, 0, RING_SIZE);
  1442. assert(!(skge->dma & 7));
  1443. /* FIXME: find out whether 64 bit iPXE will be loaded > 4GB */
  1444. if ((u64)skge->dma >> 32 != ((u64) skge->dma + RING_SIZE) >> 32) {
  1445. DBG(PFX "pci_alloc_consistent region crosses 4G boundary\n");
  1446. err = -EINVAL;
  1447. goto err;
  1448. }
  1449. err = skge_ring_alloc(&skge->rx_ring, skge->mem, skge->dma, NUM_RX_DESC);
  1450. if (err)
  1451. goto err;
  1452. /* this call relies on e->iob and d->control to be 0
  1453. * This is assured by calling memset() on skge->mem and using zalloc()
  1454. * for the skge_element structures.
  1455. */
  1456. skge_rx_refill(dev);
  1457. err = skge_ring_alloc(&skge->tx_ring, skge->mem + RX_RING_SIZE,
  1458. skge->dma + RX_RING_SIZE, NUM_TX_DESC);
  1459. if (err)
  1460. goto err;
  1461. /* Initialize MAC */
  1462. if (hw->chip_id == CHIP_ID_GENESIS)
  1463. genesis_mac_init(hw, port);
  1464. else
  1465. yukon_mac_init(hw, port);
  1466. /* Configure RAMbuffers - equally between ports and tx/rx */
  1467. chunk = (hw->ram_size - hw->ram_offset) / (hw->ports * 2);
  1468. ram_addr = hw->ram_offset + 2 * chunk * port;
  1469. skge_ramset(hw, rxqaddr[port], ram_addr, chunk);
  1470. skge_qset(skge, rxqaddr[port], skge->rx_ring.to_clean);
  1471. assert(!(skge->tx_ring.to_use != skge->tx_ring.to_clean));
  1472. skge_ramset(hw, txqaddr[port], ram_addr+chunk, chunk);
  1473. skge_qset(skge, txqaddr[port], skge->tx_ring.to_use);
  1474. /* Start receiver BMU */
  1475. wmb();
  1476. skge_write8(hw, Q_ADDR(rxqaddr[port], Q_CSR), CSR_START | CSR_IRQ_CL_F);
  1477. skge_led(skge, LED_MODE_ON);
  1478. hw->intr_mask |= portmask[port];
  1479. skge_write32(hw, B0_IMSK, hw->intr_mask);
  1480. return 0;
  1481. err:
  1482. skge_rx_clean(skge);
  1483. skge_free(dev);
  1484. return err;
  1485. }
  1486. /* stop receiver */
  1487. static void skge_rx_stop(struct skge_hw *hw, int port)
  1488. {
  1489. skge_write8(hw, Q_ADDR(rxqaddr[port], Q_CSR), CSR_STOP);
  1490. skge_write32(hw, RB_ADDR(port ? Q_R2 : Q_R1, RB_CTRL),
  1491. RB_RST_SET|RB_DIS_OP_MD);
  1492. skge_write32(hw, Q_ADDR(rxqaddr[port], Q_CSR), CSR_SET_RESET);
  1493. }
  1494. static void skge_down(struct net_device *dev)
  1495. {
  1496. struct skge_port *skge = netdev_priv(dev);
  1497. struct skge_hw *hw = skge->hw;
  1498. int port = skge->port;
  1499. if (skge->mem == NULL)
  1500. return;
  1501. DBG2(PFX "%s: disabling interface\n", dev->name);
  1502. if (hw->chip_id == CHIP_ID_GENESIS && hw->phy_type == SK_PHY_XMAC)
  1503. skge->use_xm_link_timer = 0;
  1504. netdev_link_down(dev);
  1505. hw->intr_mask &= ~portmask[port];
  1506. skge_write32(hw, B0_IMSK, hw->intr_mask);
  1507. skge_write8(skge->hw, SK_REG(skge->port, LNK_LED_REG), LED_OFF);
  1508. if (hw->chip_id == CHIP_ID_GENESIS)
  1509. genesis_stop(skge);
  1510. else
  1511. yukon_stop(skge);
  1512. /* Stop transmitter */
  1513. skge_write8(hw, Q_ADDR(txqaddr[port], Q_CSR), CSR_STOP);
  1514. skge_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL),
  1515. RB_RST_SET|RB_DIS_OP_MD);
  1516. /* Disable Force Sync bit and Enable Alloc bit */
  1517. skge_write8(hw, SK_REG(port, TXA_CTRL),
  1518. TXA_DIS_FSYNC | TXA_DIS_ALLOC | TXA_STOP_RC);
  1519. /* Stop Interval Timer and Limit Counter of Tx Arbiter */
  1520. skge_write32(hw, SK_REG(port, TXA_ITI_INI), 0L);
  1521. skge_write32(hw, SK_REG(port, TXA_LIM_INI), 0L);
  1522. /* Reset PCI FIFO */
  1523. skge_write32(hw, Q_ADDR(txqaddr[port], Q_CSR), CSR_SET_RESET);
  1524. skge_write32(hw, RB_ADDR(txqaddr[port], RB_CTRL), RB_RST_SET);
  1525. /* Reset the RAM Buffer async Tx queue */
  1526. skge_write8(hw, RB_ADDR(port == 0 ? Q_XA1 : Q_XA2, RB_CTRL), RB_RST_SET);
  1527. skge_rx_stop(hw, port);
  1528. if (hw->chip_id == CHIP_ID_GENESIS) {
  1529. skge_write8(hw, SK_REG(port, TX_MFF_CTRL2), MFF_RST_SET);
  1530. skge_write8(hw, SK_REG(port, RX_MFF_CTRL2), MFF_RST_SET);
  1531. } else {
  1532. skge_write8(hw, SK_REG(port, RX_GMF_CTRL_T), GMF_RST_SET);
  1533. skge_write8(hw, SK_REG(port, TX_GMF_CTRL_T), GMF_RST_SET);
  1534. }
  1535. skge_led(skge, LED_MODE_OFF);
  1536. skge_tx_clean(dev);
  1537. skge_rx_clean(skge);
  1538. skge_free(dev);
  1539. return;
  1540. }
  1541. static inline int skge_tx_avail(const struct skge_ring *ring)
  1542. {
  1543. mb();
  1544. return ((ring->to_clean > ring->to_use) ? 0 : NUM_TX_DESC)
  1545. + (ring->to_clean - ring->to_use) - 1;
  1546. }
  1547. static int skge_xmit_frame(struct net_device *dev, struct io_buffer *iob)
  1548. {
  1549. struct skge_port *skge = netdev_priv(dev);
  1550. struct skge_hw *hw = skge->hw;
  1551. struct skge_element *e;
  1552. struct skge_tx_desc *td;
  1553. u32 control, len;
  1554. u64 map;
  1555. if (skge_tx_avail(&skge->tx_ring) < 1)
  1556. return -EBUSY;
  1557. e = skge->tx_ring.to_use;
  1558. td = e->desc;
  1559. assert(!(td->control & BMU_OWN));
  1560. e->iob = iob;
  1561. len = iob_len(iob);
  1562. map = virt_to_bus(iob->data);
  1563. td->dma_lo = map;
  1564. td->dma_hi = map >> 32;
  1565. control = BMU_CHECK;
  1566. control |= BMU_EOF| BMU_IRQ_EOF;
  1567. /* Make sure all the descriptors written */
  1568. wmb();
  1569. td->control = BMU_OWN | BMU_SW | BMU_STF | control | len;
  1570. wmb();
  1571. skge_write8(hw, Q_ADDR(txqaddr[skge->port], Q_CSR), CSR_START);
  1572. DBGIO(PFX "%s: tx queued, slot %td, len %d\n",
  1573. dev->name, e - skge->tx_ring.start, (unsigned int)len);
  1574. skge->tx_ring.to_use = e->next;
  1575. wmb();
  1576. if (skge_tx_avail(&skge->tx_ring) <= 1) {
  1577. DBG(PFX "%s: transmit queue full\n", dev->name);
  1578. }
  1579. return 0;
  1580. }
  1581. /* Free all buffers in transmit ring */
  1582. static void skge_tx_clean(struct net_device *dev)
  1583. {
  1584. struct skge_port *skge = netdev_priv(dev);
  1585. struct skge_element *e;
  1586. for (e = skge->tx_ring.to_clean; e != skge->tx_ring.to_use; e = e->next) {
  1587. struct skge_tx_desc *td = e->desc;
  1588. td->control = 0;
  1589. }
  1590. skge->tx_ring.to_clean = e;
  1591. }
  1592. static const u8 pause_mc_addr[ETH_ALEN] = { 0x1, 0x80, 0xc2, 0x0, 0x0, 0x1 };
  1593. static inline u16 phy_length(const struct skge_hw *hw, u32 status)
  1594. {
  1595. if (hw->chip_id == CHIP_ID_GENESIS)
  1596. return status >> XMR_FS_LEN_SHIFT;
  1597. else
  1598. return status >> GMR_FS_LEN_SHIFT;
  1599. }
  1600. static inline int bad_phy_status(const struct skge_hw *hw, u32 status)
  1601. {
  1602. if (hw->chip_id == CHIP_ID_GENESIS)
  1603. return (status & (XMR_FS_ERR | XMR_FS_2L_VLAN)) != 0;
  1604. else
  1605. return (status & GMR_FS_ANY_ERR) ||
  1606. (status & GMR_FS_RX_OK) == 0;
  1607. }
  1608. /* Free all buffers in Tx ring which are no longer owned by device */
  1609. static void skge_tx_done(struct net_device *dev)
  1610. {
  1611. struct skge_port *skge = netdev_priv(dev);
  1612. struct skge_ring *ring = &skge->tx_ring;
  1613. struct skge_element *e;
  1614. skge_write8(skge->hw, Q_ADDR(txqaddr[skge->port], Q_CSR), CSR_IRQ_CL_F);
  1615. for (e = ring->to_clean; e != ring->to_use; e = e->next) {
  1616. u32 control = ((const struct skge_tx_desc *) e->desc)->control;
  1617. if (control & BMU_OWN)
  1618. break;
  1619. netdev_tx_complete(dev, e->iob);
  1620. }
  1621. skge->tx_ring.to_clean = e;
  1622. /* Can run lockless until we need to synchronize to restart queue. */
  1623. mb();
  1624. }
  1625. static void skge_rx_refill(struct net_device *dev)
  1626. {
  1627. struct skge_port *skge = netdev_priv(dev);
  1628. struct skge_ring *ring = &skge->rx_ring;
  1629. struct skge_element *e;
  1630. struct io_buffer *iob;
  1631. struct skge_rx_desc *rd;
  1632. u32 control;
  1633. int i;
  1634. for (i = 0; i < NUM_RX_DESC; i++) {
  1635. e = ring->to_clean;
  1636. rd = e->desc;
  1637. iob = e->iob;
  1638. control = rd->control;
  1639. /* nothing to do here */
  1640. if (iob || (control & BMU_OWN))
  1641. continue;
  1642. DBG2("refilling rx desc %zd: ", (ring->to_clean - ring->start));
  1643. iob = alloc_iob(RX_BUF_SIZE);
  1644. if (iob) {
  1645. skge_rx_setup(skge, e, iob, RX_BUF_SIZE);
  1646. } else {
  1647. DBG("descr %zd: alloc_iob() failed\n",
  1648. (ring->to_clean - ring->start));
  1649. /* We pass the descriptor to the NIC even if the
  1650. * allocation failed. The card will stop as soon as it
  1651. * encounters a descriptor with the OWN bit set to 0,
  1652. * thus never getting to the next descriptor that might
  1653. * contain a valid io_buffer. This would effectively
  1654. * stall the receive.
  1655. */
  1656. skge_rx_setup(skge, e, NULL, 0);
  1657. }
  1658. ring->to_clean = e->next;
  1659. }
  1660. }
  1661. static void skge_rx_done(struct net_device *dev)
  1662. {
  1663. struct skge_port *skge = netdev_priv(dev);
  1664. struct skge_ring *ring = &skge->rx_ring;
  1665. struct skge_rx_desc *rd;
  1666. struct skge_element *e;
  1667. struct io_buffer *iob;
  1668. u32 control;
  1669. u16 len;
  1670. int i;
  1671. e = ring->to_clean;
  1672. for (i = 0; i < NUM_RX_DESC; i++) {
  1673. iob = e->iob;
  1674. rd = e->desc;
  1675. rmb();
  1676. control = rd->control;
  1677. if ((control & BMU_OWN))
  1678. break;
  1679. if (!iob)
  1680. continue;
  1681. len = control & BMU_BBC;
  1682. /* catch RX errors */
  1683. if ((bad_phy_status(skge->hw, rd->status)) ||
  1684. (phy_length(skge->hw, rd->status) != len)) {
  1685. /* report receive errors */
  1686. DBG("rx error\n");
  1687. netdev_rx_err(dev, iob, -EIO);
  1688. } else {
  1689. DBG2("received packet, len %d\n", len);
  1690. iob_put(iob, len);
  1691. netdev_rx(dev, iob);
  1692. }
  1693. /* io_buffer passed to core, make sure we don't reuse it */
  1694. e->iob = NULL;
  1695. e = e->next;
  1696. }
  1697. skge_rx_refill(dev);
  1698. }
  1699. static void skge_poll(struct net_device *dev)
  1700. {
  1701. struct skge_port *skge = netdev_priv(dev);
  1702. struct skge_hw *hw = skge->hw;
  1703. u32 status;
  1704. /* reading this register ACKs interrupts */
  1705. status = skge_read32(hw, B0_SP_ISRC);
  1706. /* Link event? */
  1707. if (status & IS_EXT_REG) {
  1708. skge_phyirq(hw);
  1709. if (skge->use_xm_link_timer)
  1710. xm_link_timer(skge);
  1711. }
  1712. skge_tx_done(dev);
  1713. skge_write8(hw, Q_ADDR(rxqaddr[skge->port], Q_CSR), CSR_IRQ_CL_F);
  1714. skge_rx_done(dev);
  1715. /* restart receiver */
  1716. wmb();
  1717. skge_write8(hw, Q_ADDR(rxqaddr[skge->port], Q_CSR), CSR_START);
  1718. skge_read32(hw, B0_IMSK);
  1719. return;
  1720. }
  1721. static void skge_phyirq(struct skge_hw *hw)
  1722. {
  1723. int port;
  1724. for (port = 0; port < hw->ports; port++) {
  1725. struct net_device *dev = hw->dev[port];
  1726. struct skge_port *skge = netdev_priv(dev);
  1727. if (hw->chip_id != CHIP_ID_GENESIS)
  1728. yukon_phy_intr(skge);
  1729. else if (hw->phy_type == SK_PHY_BCOM)
  1730. bcom_phy_intr(skge);
  1731. }
  1732. hw->intr_mask |= IS_EXT_REG;
  1733. skge_write32(hw, B0_IMSK, hw->intr_mask);
  1734. skge_read32(hw, B0_IMSK);
  1735. }
  1736. static const struct {
  1737. u8 id;
  1738. const char *name;
  1739. } skge_chips[] = {
  1740. { CHIP_ID_GENESIS, "Genesis" },
  1741. { CHIP_ID_YUKON, "Yukon" },
  1742. { CHIP_ID_YUKON_LITE, "Yukon-Lite"},
  1743. { CHIP_ID_YUKON_LP, "Yukon-LP"},
  1744. };
  1745. static const char *skge_board_name(const struct skge_hw *hw)
  1746. {
  1747. unsigned int i;
  1748. static char buf[16];
  1749. for (i = 0; i < ARRAY_SIZE(skge_chips); i++)
  1750. if (skge_chips[i].id == hw->chip_id)
  1751. return skge_chips[i].name;
  1752. snprintf(buf, sizeof buf, "chipid 0x%x", hw->chip_id);
  1753. return buf;
  1754. }
  1755. /*
  1756. * Setup the board data structure, but don't bring up
  1757. * the port(s)
  1758. */
  1759. static int skge_reset(struct skge_hw *hw)
  1760. {
  1761. u32 reg;
  1762. u16 ctst, pci_status;
  1763. u8 t8, mac_cfg, pmd_type;
  1764. int i;
  1765. ctst = skge_read16(hw, B0_CTST);
  1766. /* do a SW reset */
  1767. skge_write8(hw, B0_CTST, CS_RST_SET);
  1768. skge_write8(hw, B0_CTST, CS_RST_CLR);
  1769. /* clear PCI errors, if any */
  1770. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
  1771. skge_write8(hw, B2_TST_CTRL2, 0);
  1772. pci_read_config_word(hw->pdev, PCI_STATUS, &pci_status);
  1773. pci_write_config_word(hw->pdev, PCI_STATUS,
  1774. pci_status | PCI_STATUS_ERROR_BITS);
  1775. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
  1776. skge_write8(hw, B0_CTST, CS_MRST_CLR);
  1777. /* restore CLK_RUN bits (for Yukon-Lite) */
  1778. skge_write16(hw, B0_CTST,
  1779. ctst & (CS_CLK_RUN_HOT|CS_CLK_RUN_RST|CS_CLK_RUN_ENA));
  1780. hw->chip_id = skge_read8(hw, B2_CHIP_ID);
  1781. hw->phy_type = skge_read8(hw, B2_E_1) & 0xf;
  1782. pmd_type = skge_read8(hw, B2_PMD_TYP);
  1783. hw->copper = (pmd_type == 'T' || pmd_type == '1');
  1784. switch (hw->chip_id) {
  1785. case CHIP_ID_GENESIS:
  1786. switch (hw->phy_type) {
  1787. case SK_PHY_XMAC:
  1788. hw->phy_addr = PHY_ADDR_XMAC;
  1789. break;
  1790. case SK_PHY_BCOM:
  1791. hw->phy_addr = PHY_ADDR_BCOM;
  1792. break;
  1793. default:
  1794. DBG(PFX "unsupported phy type 0x%x\n",
  1795. hw->phy_type);
  1796. return -EOPNOTSUPP;
  1797. }
  1798. break;
  1799. case CHIP_ID_YUKON:
  1800. case CHIP_ID_YUKON_LITE:
  1801. case CHIP_ID_YUKON_LP:
  1802. if (hw->phy_type < SK_PHY_MARV_COPPER && pmd_type != 'S')
  1803. hw->copper = 1;
  1804. hw->phy_addr = PHY_ADDR_MARV;
  1805. break;
  1806. default:
  1807. DBG(PFX "unsupported chip type 0x%x\n",
  1808. hw->chip_id);
  1809. return -EOPNOTSUPP;
  1810. }
  1811. mac_cfg = skge_read8(hw, B2_MAC_CFG);
  1812. hw->ports = (mac_cfg & CFG_SNG_MAC) ? 1 : 2;
  1813. hw->chip_rev = (mac_cfg & CFG_CHIP_R_MSK) >> 4;
  1814. /* read the adapters RAM size */
  1815. t8 = skge_read8(hw, B2_E_0);
  1816. if (hw->chip_id == CHIP_ID_GENESIS) {
  1817. if (t8 == 3) {
  1818. /* special case: 4 x 64k x 36, offset = 0x80000 */
  1819. hw->ram_size = 0x100000;
  1820. hw->ram_offset = 0x80000;
  1821. } else
  1822. hw->ram_size = t8 * 512;
  1823. }
  1824. else if (t8 == 0)
  1825. hw->ram_size = 0x20000;
  1826. else
  1827. hw->ram_size = t8 * 4096;
  1828. hw->intr_mask = IS_HW_ERR;
  1829. /* Use PHY IRQ for all but fiber based Genesis board */
  1830. if (!(hw->chip_id == CHIP_ID_GENESIS && hw->phy_type == SK_PHY_XMAC))
  1831. hw->intr_mask |= IS_EXT_REG;
  1832. if (hw->chip_id == CHIP_ID_GENESIS)
  1833. genesis_init(hw);
  1834. else {
  1835. /* switch power to VCC (WA for VAUX problem) */
  1836. skge_write8(hw, B0_POWER_CTRL,
  1837. PC_VAUX_ENA | PC_VCC_ENA | PC_VAUX_OFF | PC_VCC_ON);
  1838. /* avoid boards with stuck Hardware error bits */
  1839. if ((skge_read32(hw, B0_ISRC) & IS_HW_ERR) &&
  1840. (skge_read32(hw, B0_HWE_ISRC) & IS_IRQ_SENSOR)) {
  1841. DBG(PFX "stuck hardware sensor bit\n");
  1842. hw->intr_mask &= ~IS_HW_ERR;
  1843. }
  1844. /* Clear PHY COMA */
  1845. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_ON);
  1846. pci_read_config_dword(hw->pdev, PCI_DEV_REG1, &reg);
  1847. reg &= ~PCI_PHY_COMA;
  1848. pci_write_config_dword(hw->pdev, PCI_DEV_REG1, reg);
  1849. skge_write8(hw, B2_TST_CTRL1, TST_CFG_WRITE_OFF);
  1850. for (i = 0; i < hw->ports; i++) {
  1851. skge_write16(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_SET);
  1852. skge_write16(hw, SK_REG(i, GMAC_LINK_CTRL), GMLC_RST_CLR);
  1853. }
  1854. }
  1855. /* turn off hardware timer (unused) */
  1856. skge_write8(hw, B2_TI_CTRL, TIM_STOP);
  1857. skge_write8(hw, B2_TI_CTRL, TIM_CLR_IRQ);
  1858. skge_write8(hw, B0_LED, LED_STAT_ON);
  1859. /* enable the Tx Arbiters */
  1860. for (i = 0; i < hw->ports; i++)
  1861. skge_write8(hw, SK_REG(i, TXA_CTRL), TXA_ENA_ARB);
  1862. /* Initialize ram interface */
  1863. skge_write16(hw, B3_RI_CTRL, RI_RST_CLR);
  1864. skge_write8(hw, B3_RI_WTO_R1, SK_RI_TO_53);
  1865. skge_write8(hw, B3_RI_WTO_XA1, SK_RI_TO_53);
  1866. skge_write8(hw, B3_RI_WTO_XS1, SK_RI_TO_53);
  1867. skge_write8(hw, B3_RI_RTO_R1, SK_RI_TO_53);
  1868. skge_write8(hw, B3_RI_RTO_XA1, SK_RI_TO_53);
  1869. skge_write8(hw, B3_RI_RTO_XS1, SK_RI_TO_53);
  1870. skge_write8(hw, B3_RI_WTO_R2, SK_RI_TO_53);
  1871. skge_write8(hw, B3_RI_WTO_XA2, SK_RI_TO_53);
  1872. skge_write8(hw, B3_RI_WTO_XS2, SK_RI_TO_53);
  1873. skge_write8(hw, B3_RI_RTO_R2, SK_RI_TO_53);
  1874. skge_write8(hw, B3_RI_RTO_XA2, SK_RI_TO_53);
  1875. skge_write8(hw, B3_RI_RTO_XS2, SK_RI_TO_53);
  1876. skge_write32(hw, B0_HWE_IMSK, IS_ERR_MSK);
  1877. /* Set interrupt moderation for Transmit only
  1878. * Receive interrupts avoided by NAPI
  1879. */
  1880. skge_write32(hw, B2_IRQM_MSK, IS_XA1_F|IS_XA2_F);
  1881. skge_write32(hw, B2_IRQM_INI, skge_usecs2clk(hw, 100));
  1882. skge_write32(hw, B2_IRQM_CTRL, TIM_START);
  1883. skge_write32(hw, B0_IMSK, hw->intr_mask);
  1884. for (i = 0; i < hw->ports; i++) {
  1885. if (hw->chip_id == CHIP_ID_GENESIS)
  1886. genesis_reset(hw, i);
  1887. else
  1888. yukon_reset(hw, i);
  1889. }
  1890. return 0;
  1891. }
  1892. /* Initialize network device */
  1893. static struct net_device *skge_devinit(struct skge_hw *hw, int port,
  1894. int highmem __unused)
  1895. {
  1896. struct skge_port *skge;
  1897. struct net_device *dev = alloc_etherdev(sizeof(*skge));
  1898. if (!dev) {
  1899. DBG(PFX "etherdev alloc failed\n");
  1900. return NULL;
  1901. }
  1902. dev->dev = &hw->pdev->dev;
  1903. skge = netdev_priv(dev);
  1904. skge->netdev = dev;
  1905. skge->hw = hw;
  1906. /* Auto speed and flow control */
  1907. skge->autoneg = AUTONEG_ENABLE;
  1908. skge->flow_control = FLOW_MODE_SYM_OR_REM;
  1909. skge->duplex = -1;
  1910. skge->speed = -1;
  1911. skge->advertising = skge_supported_modes(hw);
  1912. hw->dev[port] = dev;
  1913. skge->port = port;
  1914. /* read the mac address */
  1915. memcpy(dev->hw_addr, (void *) (hw->regs + B2_MAC_1 + port*8), ETH_ALEN);
  1916. return dev;
  1917. }
  1918. static void skge_show_addr(struct net_device *dev)
  1919. {
  1920. DBG2(PFX "%s: addr %s\n",
  1921. dev->name, netdev_addr(dev));
  1922. }
  1923. static int skge_probe(struct pci_device *pdev,
  1924. const struct pci_device_id *ent __unused)
  1925. {
  1926. struct net_device *dev, *dev1;
  1927. struct skge_hw *hw;
  1928. int err, using_dac = 0;
  1929. adjust_pci_device(pdev);
  1930. err = -ENOMEM;
  1931. hw = zalloc(sizeof(*hw));
  1932. if (!hw) {
  1933. DBG(PFX "cannot allocate hardware struct\n");
  1934. goto err_out_free_regions;
  1935. }
  1936. hw->pdev = pdev;
  1937. hw->regs = (unsigned long)ioremap(pci_bar_start(pdev, PCI_BASE_ADDRESS_0),
  1938. SKGE_REG_SIZE);
  1939. if (!hw->regs) {
  1940. DBG(PFX "cannot map device registers\n");
  1941. goto err_out_free_hw;
  1942. }
  1943. err = skge_reset(hw);
  1944. if (err)
  1945. goto err_out_iounmap;
  1946. DBG(PFX " addr 0x%llx irq %d chip %s rev %d\n",
  1947. (unsigned long long)pdev->ioaddr, pdev->irq,
  1948. skge_board_name(hw), hw->chip_rev);
  1949. dev = skge_devinit(hw, 0, using_dac);
  1950. if (!dev)
  1951. goto err_out_led_off;
  1952. netdev_init ( dev, &skge_operations );
  1953. err = register_netdev(dev);
  1954. if (err) {
  1955. DBG(PFX "cannot register net device\n");
  1956. goto err_out_free_netdev;
  1957. }
  1958. skge_show_addr(dev);
  1959. if (hw->ports > 1 && (dev1 = skge_devinit(hw, 1, using_dac))) {
  1960. if (register_netdev(dev1) == 0)
  1961. skge_show_addr(dev1);
  1962. else {
  1963. /* Failure to register second port need not be fatal */
  1964. DBG(PFX "register of second port failed\n");
  1965. hw->dev[1] = NULL;
  1966. netdev_nullify(dev1);
  1967. netdev_put(dev1);
  1968. }
  1969. }
  1970. pci_set_drvdata(pdev, hw);
  1971. return 0;
  1972. err_out_free_netdev:
  1973. netdev_nullify(dev);
  1974. netdev_put(dev);
  1975. err_out_led_off:
  1976. skge_write16(hw, B0_LED, LED_STAT_OFF);
  1977. err_out_iounmap:
  1978. iounmap((void*)hw->regs);
  1979. err_out_free_hw:
  1980. free(hw);
  1981. err_out_free_regions:
  1982. pci_set_drvdata(pdev, NULL);
  1983. return err;
  1984. }
  1985. static void skge_remove(struct pci_device *pdev)
  1986. {
  1987. struct skge_hw *hw = pci_get_drvdata(pdev);
  1988. struct net_device *dev0, *dev1;
  1989. if (!hw)
  1990. return;
  1991. if ((dev1 = hw->dev[1]))
  1992. unregister_netdev(dev1);
  1993. dev0 = hw->dev[0];
  1994. unregister_netdev(dev0);
  1995. hw->intr_mask = 0;
  1996. skge_write32(hw, B0_IMSK, 0);
  1997. skge_read32(hw, B0_IMSK);
  1998. skge_write16(hw, B0_LED, LED_STAT_OFF);
  1999. skge_write8(hw, B0_CTST, CS_RST_SET);
  2000. if (dev1) {
  2001. netdev_nullify(dev1);
  2002. netdev_put(dev1);
  2003. }
  2004. netdev_nullify(dev0);
  2005. netdev_put(dev0);
  2006. iounmap((void*)hw->regs);
  2007. free(hw);
  2008. pci_set_drvdata(pdev, NULL);
  2009. }
  2010. /*
  2011. * Enable or disable IRQ masking.
  2012. *
  2013. * @v netdev Device to control.
  2014. * @v enable Zero to mask off IRQ, non-zero to enable IRQ.
  2015. *
  2016. * This is a iPXE Network Driver API function.
  2017. */
  2018. static void skge_net_irq ( struct net_device *dev, int enable ) {
  2019. struct skge_port *skge = netdev_priv(dev);
  2020. struct skge_hw *hw = skge->hw;
  2021. if (enable)
  2022. hw->intr_mask |= portmask[skge->port];
  2023. else
  2024. hw->intr_mask &= ~portmask[skge->port];
  2025. skge_write32(hw, B0_IMSK, hw->intr_mask);
  2026. }
  2027. struct pci_driver skge_driver __pci_driver = {
  2028. .ids = skge_id_table,
  2029. .id_count = ( sizeof (skge_id_table) / sizeof (skge_id_table[0]) ),
  2030. .probe = skge_probe,
  2031. .remove = skge_remove
  2032. };