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.

via-velocity.c 54KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. /**************************************************************************
  2. * via-velocity.c: Etherboot device driver for the VIA 6120 Gigabit
  3. * Changes for Etherboot port:
  4. * Copyright (c) 2006 by Timothy Legge <tlegge@rogers.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * This driver is based on:
  21. * via-velocity.c: VIA Velocity VT6120, VT6122 Ethernet driver
  22. * The changes are (c) Copyright 2004, Red Hat Inc.
  23. * <alan@redhat.com>
  24. * Additional fixes and clean up: Francois Romieu
  25. *
  26. * Original code:
  27. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  28. * All rights reserved.
  29. * Author: Chuang Liang-Shing, AJ Jiang
  30. *
  31. * Linux Driver Version 2.6.15.4
  32. *
  33. * REVISION HISTORY:
  34. * ================
  35. *
  36. * v1.0 03-06-2006 timlegge Initial port of Linux driver
  37. *
  38. * Indent Options: indent -kr -i8
  39. *************************************************************************/
  40. #include "etherboot.h"
  41. #include "nic.h"
  42. #include <gpxe/pci.h>
  43. #include <gpxe/ethernet.h>
  44. #include "via-velocity.h"
  45. typedef int pci_power_t;
  46. #define PCI_D0 ((int) 0)
  47. #define PCI_D1 ((int) 1)
  48. #define PCI_D2 ((int) 2)
  49. #define PCI_D3hot ((int) 3)
  50. #define PCI_D3cold ((int) 4)
  51. #define PCI_POWER_ERROR ((int) -1)
  52. /* Condensed operations for readability. */
  53. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  54. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  55. //FIXME: Move to pci.c
  56. int pci_set_power_state(struct pci_device *dev, int state);
  57. /* FIXME: Move BASE to the private structure */
  58. static u32 BASE;
  59. /* NIC specific static variables go here */
  60. #define VELOCITY_PARAM(N,D) \
  61. static const int N[MAX_UNITS]=OPTION_DEFAULT;
  62. /* MODULE_PARM(N, "1-" __MODULE_STRING(MAX_UNITS) "i");\
  63. MODULE_PARM_DESC(N, D); */
  64. VELOCITY_PARAM(RxDescriptors, "Number of receive descriptors");
  65. VELOCITY_PARAM(TxDescriptors, "Number of transmit descriptors");
  66. #define VLAN_ID_MIN 0
  67. #define VLAN_ID_MAX 4095
  68. #define VLAN_ID_DEF 0
  69. /* VID_setting[] is used for setting the VID of NIC.
  70. 0: default VID.
  71. 1-4094: other VIDs.
  72. */
  73. VELOCITY_PARAM(VID_setting, "802.1Q VLAN ID");
  74. #define RX_THRESH_MIN 0
  75. #define RX_THRESH_MAX 3
  76. #define RX_THRESH_DEF 0
  77. /* rx_thresh[] is used for controlling the receive fifo threshold.
  78. 0: indicate the rxfifo threshold is 128 bytes.
  79. 1: indicate the rxfifo threshold is 512 bytes.
  80. 2: indicate the rxfifo threshold is 1024 bytes.
  81. 3: indicate the rxfifo threshold is store & forward.
  82. */
  83. VELOCITY_PARAM(rx_thresh, "Receive fifo threshold");
  84. #define DMA_LENGTH_MIN 0
  85. #define DMA_LENGTH_MAX 7
  86. #define DMA_LENGTH_DEF 0
  87. /* DMA_length[] is used for controlling the DMA length
  88. 0: 8 DWORDs
  89. 1: 16 DWORDs
  90. 2: 32 DWORDs
  91. 3: 64 DWORDs
  92. 4: 128 DWORDs
  93. 5: 256 DWORDs
  94. 6: SF(flush till emply)
  95. 7: SF(flush till emply)
  96. */
  97. VELOCITY_PARAM(DMA_length, "DMA length");
  98. #define TAGGING_DEF 0
  99. /* enable_tagging[] is used for enabling 802.1Q VID tagging.
  100. 0: disable VID seeting(default).
  101. 1: enable VID setting.
  102. */
  103. VELOCITY_PARAM(enable_tagging, "Enable 802.1Q tagging");
  104. #define IP_ALIG_DEF 0
  105. /* IP_byte_align[] is used for IP header DWORD byte aligned
  106. 0: indicate the IP header won't be DWORD byte aligned.(Default) .
  107. 1: indicate the IP header will be DWORD byte aligned.
  108. In some enviroment, the IP header should be DWORD byte aligned,
  109. or the packet will be droped when we receive it. (eg: IPVS)
  110. */
  111. VELOCITY_PARAM(IP_byte_align, "Enable IP header dword aligned");
  112. #define TX_CSUM_DEF 1
  113. /* txcsum_offload[] is used for setting the checksum offload ability of NIC.
  114. (We only support RX checksum offload now)
  115. 0: disable csum_offload[checksum offload
  116. 1: enable checksum offload. (Default)
  117. */
  118. VELOCITY_PARAM(txcsum_offload, "Enable transmit packet checksum offload");
  119. #define FLOW_CNTL_DEF 1
  120. #define FLOW_CNTL_MIN 1
  121. #define FLOW_CNTL_MAX 5
  122. /* flow_control[] is used for setting the flow control ability of NIC.
  123. 1: hardware deafult - AUTO (default). Use Hardware default value in ANAR.
  124. 2: enable TX flow control.
  125. 3: enable RX flow control.
  126. 4: enable RX/TX flow control.
  127. 5: disable
  128. */
  129. VELOCITY_PARAM(flow_control, "Enable flow control ability");
  130. #define MED_LNK_DEF 0
  131. #define MED_LNK_MIN 0
  132. #define MED_LNK_MAX 4
  133. /* speed_duplex[] is used for setting the speed and duplex mode of NIC.
  134. 0: indicate autonegotiation for both speed and duplex mode
  135. 1: indicate 100Mbps half duplex mode
  136. 2: indicate 100Mbps full duplex mode
  137. 3: indicate 10Mbps half duplex mode
  138. 4: indicate 10Mbps full duplex mode
  139. Note:
  140. if EEPROM have been set to the force mode, this option is ignored
  141. by driver.
  142. */
  143. VELOCITY_PARAM(speed_duplex, "Setting the speed and duplex mode");
  144. #define VAL_PKT_LEN_DEF 0
  145. /* ValPktLen[] is used for setting the checksum offload ability of NIC.
  146. 0: Receive frame with invalid layer 2 length (Default)
  147. 1: Drop frame with invalid layer 2 length
  148. */
  149. VELOCITY_PARAM(ValPktLen, "Receiving or Drop invalid 802.3 frame");
  150. #define WOL_OPT_DEF 0
  151. #define WOL_OPT_MIN 0
  152. #define WOL_OPT_MAX 7
  153. /* wol_opts[] is used for controlling wake on lan behavior.
  154. 0: Wake up if recevied a magic packet. (Default)
  155. 1: Wake up if link status is on/off.
  156. 2: Wake up if recevied an arp packet.
  157. 4: Wake up if recevied any unicast packet.
  158. Those value can be sumed up to support more than one option.
  159. */
  160. VELOCITY_PARAM(wol_opts, "Wake On Lan options");
  161. #define INT_WORKS_DEF 20
  162. #define INT_WORKS_MIN 10
  163. #define INT_WORKS_MAX 64
  164. VELOCITY_PARAM(int_works, "Number of packets per interrupt services");
  165. /* The descriptors for this card are required to be aligned on
  166. 64 byte boundaries. As the align attribute does not guarantee alignment
  167. greater than the alignment of the start address (which for Etherboot
  168. is 16 bytes of alignment) it requires some extra steps. Add 64 to the
  169. size of the array and the init_ring adjusts the alignment */
  170. /* Define the TX Descriptor */
  171. static u8 tx_ring[TX_DESC_DEF * sizeof(struct tx_desc) + 64];
  172. /* Create a static buffer of size PKT_BUF_SZ for each TX Descriptor.
  173. All descriptors point to a part of this buffer */
  174. static u8 txb[(TX_DESC_DEF * PKT_BUF_SZ) + 64];
  175. /* Define the RX Descriptor */
  176. static u8 rx_ring[RX_DESC_DEF * sizeof(struct rx_desc) + 64];
  177. /* Create a static buffer of size PKT_BUF_SZ for each RX Descriptor
  178. All descriptors point to a part of this buffer */
  179. static u8 rxb[(RX_DESC_DEF * PKT_BUF_SZ) + 64];
  180. static void velocity_init_info(struct pci_device *pdev,
  181. struct velocity_info *vptr,
  182. struct velocity_info_tbl *info);
  183. static int velocity_get_pci_info(struct velocity_info *,
  184. struct pci_device *pdev);
  185. static int velocity_open(struct nic *nic, struct pci_device *pci);
  186. static int velocity_soft_reset(struct velocity_info *vptr);
  187. static void velocity_init_cam_filter(struct velocity_info *vptr);
  188. static void mii_init(struct velocity_info *vptr, u32 mii_status);
  189. static u32 velocity_get_opt_media_mode(struct velocity_info *vptr);
  190. static void velocity_print_link_status(struct velocity_info *vptr);
  191. static void safe_disable_mii_autopoll(struct mac_regs *regs);
  192. static void enable_flow_control_ability(struct velocity_info *vptr);
  193. static void enable_mii_autopoll(struct mac_regs *regs);
  194. static int velocity_mii_read(struct mac_regs *, u8 byIdx, u16 * pdata);
  195. static int velocity_mii_write(struct mac_regs *, u8 byMiiAddr, u16 data);
  196. static u32 mii_check_media_mode(struct mac_regs *regs);
  197. static u32 check_connection_type(struct mac_regs *regs);
  198. static int velocity_set_media_mode(struct velocity_info *vptr,
  199. u32 mii_status);
  200. /*
  201. * Internal board variants. At the moment we have only one
  202. */
  203. static struct velocity_info_tbl chip_info_table[] = {
  204. {CHIP_TYPE_VT6110,
  205. "VIA Networking Velocity Family Gigabit Ethernet Adapter", 256, 1,
  206. 0x00FFFFFFUL},
  207. {0, NULL, 0, 0, 0}
  208. };
  209. /**
  210. * velocity_set_int_opt - parser for integer options
  211. * @opt: pointer to option value
  212. * @val: value the user requested (or -1 for default)
  213. * @min: lowest value allowed
  214. * @max: highest value allowed
  215. * @def: default value
  216. * @name: property name
  217. * @dev: device name
  218. *
  219. * Set an integer property in the module options. This function does
  220. * all the verification and checking as well as reporting so that
  221. * we don't duplicate code for each option.
  222. */
  223. static void velocity_set_int_opt(int *opt, int val, int min, int max,
  224. int def, char *name, char *devname)
  225. {
  226. if (val == -1) {
  227. printf("%s: set value of parameter %s to %d\n",
  228. devname, name, def);
  229. *opt = def;
  230. } else if (val < min || val > max) {
  231. printf
  232. ("%s: the value of parameter %s is invalid, the valid range is (%d-%d)\n",
  233. devname, name, min, max);
  234. *opt = def;
  235. } else {
  236. printf("%s: set value of parameter %s to %d\n",
  237. devname, name, val);
  238. *opt = val;
  239. }
  240. }
  241. /**
  242. * velocity_set_bool_opt - parser for boolean options
  243. * @opt: pointer to option value
  244. * @val: value the user requested (or -1 for default)
  245. * @def: default value (yes/no)
  246. * @flag: numeric value to set for true.
  247. * @name: property name
  248. * @dev: device name
  249. *
  250. * Set a boolean property in the module options. This function does
  251. * all the verification and checking as well as reporting so that
  252. * we don't duplicate code for each option.
  253. */
  254. static void velocity_set_bool_opt(u32 * opt, int val, int def, u32 flag,
  255. char *name, char *devname)
  256. {
  257. (*opt) &= (~flag);
  258. if (val == -1) {
  259. printf("%s: set parameter %s to %s\n",
  260. devname, name, def ? "TRUE" : "FALSE");
  261. *opt |= (def ? flag : 0);
  262. } else if (val < 0 || val > 1) {
  263. printf
  264. ("%s: the value of parameter %s is invalid, the valid range is (0-1)\n",
  265. devname, name);
  266. *opt |= (def ? flag : 0);
  267. } else {
  268. printf("%s: set parameter %s to %s\n",
  269. devname, name, val ? "TRUE" : "FALSE");
  270. *opt |= (val ? flag : 0);
  271. }
  272. }
  273. /**
  274. * velocity_get_options - set options on device
  275. * @opts: option structure for the device
  276. * @index: index of option to use in module options array
  277. * @devname: device name
  278. *
  279. * Turn the module and command options into a single structure
  280. * for the current device
  281. */
  282. static void velocity_get_options(struct velocity_opt *opts, int index,
  283. char *devname)
  284. {
  285. /* FIXME Do the options need to be configurable */
  286. velocity_set_int_opt(&opts->rx_thresh, -1, RX_THRESH_MIN,
  287. RX_THRESH_MAX, RX_THRESH_DEF, "rx_thresh",
  288. devname);
  289. velocity_set_int_opt(&opts->DMA_length, DMA_length[index],
  290. DMA_LENGTH_MIN, DMA_LENGTH_MAX,
  291. DMA_LENGTH_DEF, "DMA_length", devname);
  292. velocity_set_int_opt(&opts->numrx, RxDescriptors[index],
  293. RX_DESC_MIN, RX_DESC_MAX, RX_DESC_DEF,
  294. "RxDescriptors", devname);
  295. velocity_set_int_opt(&opts->numtx, TxDescriptors[index],
  296. TX_DESC_MIN, TX_DESC_MAX, TX_DESC_DEF,
  297. "TxDescriptors", devname);
  298. velocity_set_int_opt(&opts->vid, VID_setting[index], VLAN_ID_MIN,
  299. VLAN_ID_MAX, VLAN_ID_DEF, "VID_setting",
  300. devname);
  301. velocity_set_bool_opt(&opts->flags, enable_tagging[index],
  302. TAGGING_DEF, VELOCITY_FLAGS_TAGGING,
  303. "enable_tagging", devname);
  304. velocity_set_bool_opt(&opts->flags, txcsum_offload[index],
  305. TX_CSUM_DEF, VELOCITY_FLAGS_TX_CSUM,
  306. "txcsum_offload", devname);
  307. velocity_set_int_opt(&opts->flow_cntl, flow_control[index],
  308. FLOW_CNTL_MIN, FLOW_CNTL_MAX, FLOW_CNTL_DEF,
  309. "flow_control", devname);
  310. velocity_set_bool_opt(&opts->flags, IP_byte_align[index],
  311. IP_ALIG_DEF, VELOCITY_FLAGS_IP_ALIGN,
  312. "IP_byte_align", devname);
  313. velocity_set_bool_opt(&opts->flags, ValPktLen[index],
  314. VAL_PKT_LEN_DEF, VELOCITY_FLAGS_VAL_PKT_LEN,
  315. "ValPktLen", devname);
  316. velocity_set_int_opt((int *) &opts->spd_dpx, speed_duplex[index],
  317. MED_LNK_MIN, MED_LNK_MAX, MED_LNK_DEF,
  318. "Media link mode", devname);
  319. velocity_set_int_opt((int *) &opts->wol_opts, wol_opts[index],
  320. WOL_OPT_MIN, WOL_OPT_MAX, WOL_OPT_DEF,
  321. "Wake On Lan options", devname);
  322. velocity_set_int_opt((int *) &opts->int_works, int_works[index],
  323. INT_WORKS_MIN, INT_WORKS_MAX, INT_WORKS_DEF,
  324. "Interrupt service works", devname);
  325. opts->numrx = (opts->numrx & ~3);
  326. }
  327. /**
  328. * velocity_init_cam_filter - initialise CAM
  329. * @vptr: velocity to program
  330. *
  331. * Initialize the content addressable memory used for filters. Load
  332. * appropriately according to the presence of VLAN
  333. */
  334. static void velocity_init_cam_filter(struct velocity_info *vptr)
  335. {
  336. struct mac_regs *regs = vptr->mac_regs;
  337. /* Turn on MCFG_PQEN, turn off MCFG_RTGOPT */
  338. WORD_REG_BITS_SET(MCFG_PQEN, MCFG_RTGOPT, &regs->MCFG);
  339. WORD_REG_BITS_ON(MCFG_VIDFR, &regs->MCFG);
  340. /* Disable all CAMs */
  341. memset(vptr->vCAMmask, 0, sizeof(u8) * 8);
  342. memset(vptr->mCAMmask, 0, sizeof(u8) * 8);
  343. mac_set_cam_mask(regs, vptr->vCAMmask, VELOCITY_VLAN_ID_CAM);
  344. mac_set_cam_mask(regs, vptr->mCAMmask, VELOCITY_MULTICAST_CAM);
  345. /* Enable first VCAM */
  346. if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
  347. /* If Tagging option is enabled and VLAN ID is not zero, then
  348. turn on MCFG_RTGOPT also */
  349. if (vptr->options.vid != 0)
  350. WORD_REG_BITS_ON(MCFG_RTGOPT, &regs->MCFG);
  351. mac_set_cam(regs, 0, (u8 *) & (vptr->options.vid),
  352. VELOCITY_VLAN_ID_CAM);
  353. vptr->vCAMmask[0] |= 1;
  354. mac_set_cam_mask(regs, vptr->vCAMmask,
  355. VELOCITY_VLAN_ID_CAM);
  356. } else {
  357. u16 temp = 0;
  358. mac_set_cam(regs, 0, (u8 *) & temp, VELOCITY_VLAN_ID_CAM);
  359. temp = 1;
  360. mac_set_cam_mask(regs, (u8 *) & temp,
  361. VELOCITY_VLAN_ID_CAM);
  362. }
  363. }
  364. static inline void velocity_give_many_rx_descs(struct velocity_info *vptr)
  365. {
  366. struct mac_regs *regs = vptr->mac_regs;
  367. int avail, dirty, unusable;
  368. /*
  369. * RD number must be equal to 4X per hardware spec
  370. * (programming guide rev 1.20, p.13)
  371. */
  372. if (vptr->rd_filled < 4)
  373. return;
  374. wmb();
  375. unusable = vptr->rd_filled & 0x0003;
  376. dirty = vptr->rd_dirty - unusable;
  377. for (avail = vptr->rd_filled & 0xfffc; avail; avail--) {
  378. dirty = (dirty > 0) ? dirty - 1 : vptr->options.numrx - 1;
  379. // printf("return dirty: %d\n", dirty);
  380. vptr->rd_ring[dirty].rdesc0.owner = OWNED_BY_NIC;
  381. }
  382. writew(vptr->rd_filled & 0xfffc, &regs->RBRDU);
  383. vptr->rd_filled = unusable;
  384. }
  385. static int velocity_rx_refill(struct velocity_info *vptr)
  386. {
  387. int dirty = vptr->rd_dirty, done = 0, ret = 0;
  388. // printf("rx_refill - rd_curr = %d, dirty = %d\n", vptr->rd_curr, dirty);
  389. do {
  390. struct rx_desc *rd = vptr->rd_ring + dirty;
  391. /* Fine for an all zero Rx desc at init time as well */
  392. if (rd->rdesc0.owner == OWNED_BY_NIC)
  393. break;
  394. // printf("rx_refill - after owner %d\n", dirty);
  395. rd->inten = 1;
  396. rd->pa_high = 0;
  397. rd->rdesc0.len = cpu_to_le32(vptr->rx_buf_sz);;
  398. done++;
  399. dirty = (dirty < vptr->options.numrx - 1) ? dirty + 1 : 0;
  400. } while (dirty != vptr->rd_curr);
  401. if (done) {
  402. // printf("\nGive Back Desc\n");
  403. vptr->rd_dirty = dirty;
  404. vptr->rd_filled += done;
  405. velocity_give_many_rx_descs(vptr);
  406. }
  407. return ret;
  408. }
  409. extern void hex_dump(const char *data, const unsigned int len);
  410. /**************************************************************************
  411. POLL - Wait for a frame
  412. ***************************************************************************/
  413. //EB53 static int velocity_poll(struct nic *nic, int retrieve)
  414. static int velocity_poll(struct nic *nic __unused)
  415. {
  416. /* Work out whether or not there's an ethernet packet ready to
  417. * read. Return 0 if not.
  418. */
  419. int rd_curr = vptr->rd_curr % RX_DESC_DEF;
  420. struct rx_desc *rd = &(vptr->rd_ring[rd_curr]);
  421. if (rd->rdesc0.owner == OWNED_BY_NIC)
  422. return 0;
  423. rmb();
  424. /*
  425. * Don't drop CE or RL error frame although RXOK is off
  426. */
  427. if ((rd->rdesc0.RSR & RSR_RXOK)
  428. || (!(rd->rdesc0.RSR & RSR_RXOK)
  429. && (rd->rdesc0.RSR & (RSR_CE | RSR_RL)))) {
  430. nic->packetlen = rd->rdesc0.len;
  431. // ptr->rxb + (rd_curr * PKT_BUF_SZ)
  432. memcpy(nic->packet, bus_to_virt(rd->pa_low),
  433. nic->packetlen - 4);
  434. vptr->rd_curr++;
  435. vptr->rd_curr = vptr->rd_curr % RX_DESC_DEF;
  436. velocity_rx_refill(vptr);
  437. return 1; /* Remove this line once this method is implemented */
  438. }
  439. return 0;
  440. }
  441. #define TX_TIMEOUT (1000);
  442. /**************************************************************************
  443. TRANSMIT - Transmit a frame
  444. ***************************************************************************/
  445. static void velocity_transmit(struct nic *nic, const char *dest, /* Destination */
  446. unsigned int type, /* Type */
  447. unsigned int size, /* size */
  448. const char *packet)
  449. { /* Packet */
  450. u16 nstype;
  451. u32 to;
  452. u8 *ptxb;
  453. unsigned int pktlen;
  454. struct tx_desc *td_ptr;
  455. int entry = vptr->td_curr % TX_DESC_DEF;
  456. td_ptr = &(vptr->td_rings[entry]);
  457. /* point to the current txb incase multiple tx_rings are used */
  458. ptxb = vptr->txb + (entry * PKT_BUF_SZ);
  459. memcpy(ptxb, dest, ETH_ALEN); /* Destination */
  460. memcpy(ptxb + ETH_ALEN, nic->node_addr, ETH_ALEN); /* Source */
  461. nstype = htons((u16) type); /* Type */
  462. memcpy(ptxb + 2 * ETH_ALEN, (u8 *) & nstype, 2); /* Type */
  463. memcpy(ptxb + ETH_HLEN, packet, size);
  464. td_ptr->tdesc1.TCPLS = TCPLS_NORMAL;
  465. td_ptr->tdesc1.TCR = TCR0_TIC;
  466. td_ptr->td_buf[0].queue = 0;
  467. size += ETH_HLEN;
  468. while (size < ETH_ZLEN) /* pad to min length */
  469. ptxb[size++] = '\0';
  470. if (size < ETH_ZLEN) {
  471. // printf("Padd that packet\n");
  472. pktlen = ETH_ZLEN;
  473. // memcpy(ptxb, skb->data, skb->len);
  474. memset(ptxb + size, 0, ETH_ZLEN - size);
  475. vptr->td_rings[entry].tdesc0.pktsize = pktlen;
  476. vptr->td_rings[entry].td_buf[0].pa_low = virt_to_bus(ptxb);
  477. vptr->td_rings[entry].td_buf[0].pa_high &=
  478. cpu_to_le32(0xffff0000L);
  479. vptr->td_rings[entry].td_buf[0].bufsize =
  480. vptr->td_rings[entry].tdesc0.pktsize;
  481. vptr->td_rings[entry].tdesc1.CMDZ = 2;
  482. } else {
  483. // printf("Correct size packet\n");
  484. td_ptr->tdesc0.pktsize = size;
  485. td_ptr->td_buf[0].pa_low = virt_to_bus(ptxb);
  486. td_ptr->td_buf[0].pa_high = 0;
  487. td_ptr->td_buf[0].bufsize = td_ptr->tdesc0.pktsize;
  488. // tdinfo->nskb_dma = 1;
  489. td_ptr->tdesc1.CMDZ = 2;
  490. }
  491. if (vptr->flags & VELOCITY_FLAGS_TAGGING) {
  492. td_ptr->tdesc1.pqinf.VID = (vptr->options.vid & 0xfff);
  493. td_ptr->tdesc1.pqinf.priority = 0;
  494. td_ptr->tdesc1.pqinf.CFI = 0;
  495. td_ptr->tdesc1.TCR |= TCR0_VETAG;
  496. }
  497. vptr->td_curr = (entry + 1);
  498. {
  499. int prev = entry - 1;
  500. if (prev < 0)
  501. prev = TX_DESC_DEF - 1;
  502. td_ptr->tdesc0.owner |= OWNED_BY_NIC;
  503. td_ptr = &(vptr->td_rings[prev]);
  504. td_ptr->td_buf[0].queue = 1;
  505. mac_tx_queue_wake(vptr->mac_regs, 0);
  506. }
  507. to = currticks() + TX_TIMEOUT;
  508. while ((td_ptr->tdesc0.owner & OWNED_BY_NIC) && (currticks() < to)); /* wait */
  509. if (currticks() >= to) {
  510. printf("TX Time Out");
  511. }
  512. }
  513. /**************************************************************************
  514. DISABLE - Turn off ethernet interface
  515. ***************************************************************************/
  516. static void velocity_disable(struct nic *nic __unused)
  517. {
  518. /* put the card in its initial state */
  519. /* This function serves 3 purposes.
  520. * This disables DMA and interrupts so we don't receive
  521. * unexpected packets or interrupts from the card after
  522. * etherboot has finished.
  523. * This frees resources so etherboot may use
  524. * this driver on another interface
  525. * This allows etherboot to reinitialize the interface
  526. * if something is something goes wrong.
  527. */
  528. struct mac_regs *regs = vptr->mac_regs;
  529. mac_disable_int(regs);
  530. writel(CR0_STOP, &regs->CR0Set);
  531. writew(0xFFFF, &regs->TDCSRClr);
  532. writeb(0xFF, &regs->RDCSRClr);
  533. safe_disable_mii_autopoll(regs);
  534. mac_clear_isr(regs);
  535. /* Power down the chip */
  536. // pci_set_power_state(vptr->pdev, PCI_D3hot);
  537. vptr->flags &= (~VELOCITY_FLAGS_OPENED);
  538. }
  539. /**************************************************************************
  540. IRQ - handle interrupts
  541. ***************************************************************************/
  542. static void velocity_irq(struct nic *nic __unused, irq_action_t action)
  543. {
  544. /* This routine is somewhat optional. Etherboot itself
  545. * doesn't use interrupts, but they are required under some
  546. * circumstances when we're acting as a PXE stack.
  547. *
  548. * If you don't implement this routine, the only effect will
  549. * be that your driver cannot be used via Etherboot's UNDI
  550. * API. This won't affect programs that use only the UDP
  551. * portion of the PXE API, such as pxelinux.
  552. */
  553. switch (action) {
  554. case DISABLE:
  555. case ENABLE:
  556. /* Set receive interrupt enabled/disabled state */
  557. /*
  558. outb ( action == ENABLE ? IntrMaskEnabled : IntrMaskDisabled,
  559. nic->ioaddr + IntrMaskRegister );
  560. */
  561. break;
  562. case FORCE:
  563. /* Force NIC to generate a receive interrupt */
  564. /*
  565. outb ( ForceInterrupt, nic->ioaddr + IntrForceRegister );
  566. */
  567. break;
  568. }
  569. }
  570. static struct nic_operations velocity_operations = {
  571. .connect = dummy_connect,
  572. .poll = velocity_poll,
  573. .transmit = velocity_transmit,
  574. .irq = velocity_irq,
  575. };
  576. /**************************************************************************
  577. PROBE - Look for an adapter, this routine's visible to the outside
  578. ***************************************************************************/
  579. static int velocity_probe(struct pci_device *dev, struct pci_device *pci)
  580. {
  581. struct nic *nic = (struct nic *) dev;
  582. int ret, i;
  583. struct mac_regs *regs;
  584. printf("via-velocity.c: Found %s Vendor=0x%hX Device=0x%hX\n",
  585. pci->driver_name, pci->vendor, pci->device);
  586. /* point to private storage */
  587. vptr = &vptx;
  588. info = chip_info_table;
  589. velocity_init_info(pci, vptr, info);
  590. //FIXME: pci_enable_device(pci);
  591. //FIXME: pci_set_power_state(pci, PCI_D0);
  592. ret = velocity_get_pci_info(vptr, pci);
  593. if (ret < 0) {
  594. printf("Failed to find PCI device.\n");
  595. return 0;
  596. }
  597. regs = ioremap(vptr->memaddr, vptr->io_size);
  598. if (regs == NULL) {
  599. printf("Unable to remap io\n");
  600. return 0;
  601. }
  602. vptr->mac_regs = regs;
  603. BASE = vptr->ioaddr;
  604. printf("Chip ID: %hX\n", vptr->chip_id);
  605. for (i = 0; i < 6; i++)
  606. nic->node_addr[i] = readb(&regs->PAR[i]);
  607. DBG ( "%s: %s at ioaddr %#hX\n", pci->driver_name, eth_ntoa ( nic->node_addr ), BASE );
  608. velocity_get_options(&vptr->options, 0, pci->driver_name);
  609. /*
  610. * Mask out the options cannot be set to the chip
  611. */
  612. vptr->options.flags &= 0x00FFFFFFUL; //info->flags = 0x00FFFFFFUL;
  613. /*
  614. * Enable the chip specified capbilities
  615. */
  616. vptr->flags =
  617. vptr->options.
  618. flags | (0x00FFFFFFUL /*info->flags */ & 0xFF000000UL);
  619. vptr->wol_opts = vptr->options.wol_opts;
  620. vptr->flags |= VELOCITY_FLAGS_WOL_ENABLED;
  621. vptr->phy_id = MII_GET_PHY_ID(vptr->mac_regs);
  622. if (vptr->flags & VELOCITY_FLAGS_TX_CSUM) {
  623. printf("features missing\n");
  624. }
  625. /* and leave the chip powered down */
  626. // FIXME: pci_set_power_state(pci, PCI_D3hot);
  627. check_connection_type(vptr->mac_regs);
  628. velocity_open(nic, pci);
  629. /* store NIC parameters */
  630. nic->nic_op = &velocity_operations;
  631. return 1;
  632. }
  633. //#define IORESOURCE_IO 0x00000100 /* Resource type */
  634. /**
  635. * velocity_init_info - init private data
  636. * @pdev: PCI device
  637. * @vptr: Velocity info
  638. * @info: Board type
  639. *
  640. * Set up the initial velocity_info struct for the device that has been
  641. * discovered.
  642. */
  643. static void velocity_init_info(struct pci_device *pdev,
  644. struct velocity_info *vptr,
  645. struct velocity_info_tbl *info)
  646. {
  647. memset(vptr, 0, sizeof(struct velocity_info));
  648. vptr->pdev = pdev;
  649. vptr->chip_id = info->chip_id;
  650. vptr->io_size = info->io_size;
  651. vptr->num_txq = info->txqueue;
  652. vptr->multicast_limit = MCAM_SIZE;
  653. printf
  654. ("chip_id: 0x%hX, io_size: %d, num_txq %d, multicast_limit: %d\n",
  655. vptr->chip_id, vptr->io_size, vptr->num_txq,
  656. vptr->multicast_limit);
  657. printf("Name: %s\n", info->name);
  658. // spin_lock_init(&vptr->lock);
  659. // INIT_LIST_HEAD(&vptr->list);
  660. }
  661. /**
  662. * velocity_get_pci_info - retrieve PCI info for device
  663. * @vptr: velocity device
  664. * @pdev: PCI device it matches
  665. *
  666. * Retrieve the PCI configuration space data that interests us from
  667. * the kernel PCI layer
  668. */
  669. #define IORESOURCE_IO 0x00000100 /* Resource type */
  670. #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */
  671. #define IORESOURCE_MEM 0x00000200
  672. #define BAR_0 0
  673. #define BAR_1 1
  674. #define BAR_5 5
  675. #define PCI_BASE_ADDRESS_SPACE 0x01 /* 0 = memory, 1 = I/O */
  676. #define PCI_BASE_ADDRESS_SPACE_IO 0x01
  677. #define PCI_BASE_ADDRESS_SPACE_MEMORY 0x00
  678. #define PCI_BASE_ADDRESS_MEM_TYPE_MASK 0x06
  679. #define PCI_BASE_ADDRESS_MEM_TYPE_32 0x00 /* 32 bit address */
  680. #define PCI_BASE_ADDRESS_MEM_TYPE_1M 0x02 /* Below 1M [obsolete] */
  681. #define PCI_BASE_ADDRESS_MEM_TYPE_64 0x04 /* 64 bit address */
  682. #define PCI_BASE_ADDRESS_MEM_PREFETCH 0x08 /* prefetchable? */
  683. //#define PCI_BASE_ADDRESS_MEM_MASK (~0x0fUL)
  684. // #define PCI_BASE_ADDRESS_IO_MASK (~0x03UL)
  685. unsigned long pci_resource_flags(struct pci_device *pdev, unsigned int bar)
  686. {
  687. uint32_t l, sz;
  688. unsigned long flags = 0;
  689. pci_read_config_dword(pdev, bar, &l);
  690. pci_write_config_dword(pdev, bar, ~0);
  691. pci_read_config_dword(pdev, bar, &sz);
  692. pci_write_config_dword(pdev, bar, l);
  693. if (!sz || sz == 0xffffffff)
  694. printf("Weird size\n");
  695. if (l == 0xffffffff)
  696. l = 0;
  697. if ((l & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY) {
  698. /* sz = pci_size(l, sz, PCI_BASE_ADDRESS_MEM_MASK);
  699. if (!sz)
  700. continue;
  701. res->start = l & PCI_BASE_ADDRESS_MEM_MASK;
  702. */ flags |= l & ~PCI_BASE_ADDRESS_MEM_MASK;
  703. printf("Memory Resource\n");
  704. } else {
  705. // sz = pci_size(l, sz, PCI_BASE_ADDRESS_IO_MASK & 0xffff);
  706. /// if (!sz)
  707. /// continue;
  708. // res->start = l & PCI_BASE_ADDRESS_IO_MASK;
  709. flags |= l & ~PCI_BASE_ADDRESS_IO_MASK;
  710. printf("I/O Resource\n");
  711. }
  712. if (flags & PCI_BASE_ADDRESS_SPACE_IO) {
  713. printf("Why is it here\n");
  714. flags |= IORESOURCE_IO;
  715. } else {
  716. printf("here\n");
  717. //flags &= ~IORESOURCE_IO;
  718. }
  719. if (flags & PCI_BASE_ADDRESS_MEM_PREFETCH)
  720. flags |= IORESOURCE_MEM | IORESOURCE_PREFETCH;
  721. return flags;
  722. }
  723. static int velocity_get_pci_info(struct velocity_info *vptr,
  724. struct pci_device *pdev)
  725. {
  726. if (pci_read_config_byte(pdev, PCI_REVISION_ID, &vptr->rev_id) < 0) {
  727. printf("DEBUG: pci_read_config_byte failed\n");
  728. return -1;
  729. }
  730. adjust_pci_device(pdev);
  731. vptr->ioaddr = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
  732. vptr->memaddr = pci_bar_start(pdev, PCI_BASE_ADDRESS_1);
  733. printf("Looking for I/O Resource - Found:");
  734. if (!
  735. (pci_resource_flags(pdev, PCI_BASE_ADDRESS_0) & IORESOURCE_IO))
  736. {
  737. printf
  738. ("DEBUG: region #0 is not an I/O resource, aborting.\n");
  739. return -1;
  740. }
  741. printf("Looking for Memory Resource - Found:");
  742. if ((pci_resource_flags(pdev, PCI_BASE_ADDRESS_1) & IORESOURCE_IO)) {
  743. printf("DEBUG: region #1 is an I/O resource, aborting.\n");
  744. return -1;
  745. }
  746. if (pci_bar_size(pdev, PCI_BASE_ADDRESS_1) < 256) {
  747. printf("DEBUG: region #1 is too small.\n");
  748. return -1;
  749. }
  750. vptr->pdev = pdev;
  751. return 0;
  752. }
  753. /**
  754. * velocity_print_link_status - link status reporting
  755. * @vptr: velocity to report on
  756. *
  757. * Turn the link status of the velocity card into a kernel log
  758. * description of the new link state, detailing speed and duplex
  759. * status
  760. */
  761. static void velocity_print_link_status(struct velocity_info *vptr)
  762. {
  763. if (vptr->mii_status & VELOCITY_LINK_FAIL) {
  764. printf("failed to detect cable link\n");
  765. } else if (vptr->options.spd_dpx == SPD_DPX_AUTO) {
  766. printf("Link autonegation");
  767. if (vptr->mii_status & VELOCITY_SPEED_1000)
  768. printf(" speed 1000M bps");
  769. else if (vptr->mii_status & VELOCITY_SPEED_100)
  770. printf(" speed 100M bps");
  771. else
  772. printf(" speed 10M bps");
  773. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  774. printf(" full duplex\n");
  775. else
  776. printf(" half duplex\n");
  777. } else {
  778. printf("Link forced");
  779. switch (vptr->options.spd_dpx) {
  780. case SPD_DPX_100_HALF:
  781. printf(" speed 100M bps half duplex\n");
  782. break;
  783. case SPD_DPX_100_FULL:
  784. printf(" speed 100M bps full duplex\n");
  785. break;
  786. case SPD_DPX_10_HALF:
  787. printf(" speed 10M bps half duplex\n");
  788. break;
  789. case SPD_DPX_10_FULL:
  790. printf(" speed 10M bps full duplex\n");
  791. break;
  792. default:
  793. break;
  794. }
  795. }
  796. }
  797. /**
  798. * velocity_rx_reset - handle a receive reset
  799. * @vptr: velocity we are resetting
  800. *
  801. * Reset the ownership and status for the receive ring side.
  802. * Hand all the receive queue to the NIC.
  803. */
  804. static void velocity_rx_reset(struct velocity_info *vptr)
  805. {
  806. struct mac_regs *regs = vptr->mac_regs;
  807. int i;
  808. //ptr->rd_dirty = vptr->rd_filled = vptr->rd_curr = 0;
  809. /*
  810. * Init state, all RD entries belong to the NIC
  811. */
  812. for (i = 0; i < vptr->options.numrx; ++i)
  813. vptr->rd_ring[i].rdesc0.owner = OWNED_BY_NIC;
  814. writew(RX_DESC_DEF, &regs->RBRDU);
  815. writel(virt_to_le32desc(vptr->rd_ring), &regs->RDBaseLo);
  816. writew(0, &regs->RDIdx);
  817. writew(RX_DESC_DEF - 1, &regs->RDCSize);
  818. }
  819. /**
  820. * velocity_init_registers - initialise MAC registers
  821. * @vptr: velocity to init
  822. * @type: type of initialisation (hot or cold)
  823. *
  824. * Initialise the MAC on a reset or on first set up on the
  825. * hardware.
  826. */
  827. static void velocity_init_registers(struct nic *nic,
  828. struct velocity_info *vptr,
  829. enum velocity_init_type type)
  830. {
  831. struct mac_regs *regs = vptr->mac_regs;
  832. int i, mii_status;
  833. mac_wol_reset(regs);
  834. switch (type) {
  835. case VELOCITY_INIT_RESET:
  836. case VELOCITY_INIT_WOL:
  837. //netif_stop_queue(vptr->dev);
  838. /*
  839. * Reset RX to prevent RX pointer not on the 4X location
  840. */
  841. velocity_rx_reset(vptr);
  842. mac_rx_queue_run(regs);
  843. mac_rx_queue_wake(regs);
  844. mii_status = velocity_get_opt_media_mode(vptr);
  845. if (velocity_set_media_mode(vptr, mii_status) !=
  846. VELOCITY_LINK_CHANGE) {
  847. velocity_print_link_status(vptr);
  848. if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
  849. printf("Link Failed\n");
  850. // netif_wake_queue(vptr->dev);
  851. }
  852. enable_flow_control_ability(vptr);
  853. mac_clear_isr(regs);
  854. writel(CR0_STOP, &regs->CR0Clr);
  855. //writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
  856. writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
  857. &regs->CR0Set);
  858. break;
  859. case VELOCITY_INIT_COLD:
  860. default:
  861. /*
  862. * Do reset
  863. */
  864. velocity_soft_reset(vptr);
  865. mdelay(5);
  866. mac_eeprom_reload(regs);
  867. for (i = 0; i < 6; i++) {
  868. writeb(nic->node_addr[i], &(regs->PAR[i]));
  869. }
  870. /*
  871. * clear Pre_ACPI bit.
  872. */
  873. BYTE_REG_BITS_OFF(CFGA_PACPI, &(regs->CFGA));
  874. mac_set_rx_thresh(regs, vptr->options.rx_thresh);
  875. mac_set_dma_length(regs, vptr->options.DMA_length);
  876. writeb(WOLCFG_SAM | WOLCFG_SAB, &regs->WOLCFGSet);
  877. /*
  878. * Back off algorithm use original IEEE standard
  879. */
  880. BYTE_REG_BITS_SET(CFGB_OFSET,
  881. (CFGB_CRANDOM | CFGB_CAP | CFGB_MBA |
  882. CFGB_BAKOPT), &regs->CFGB);
  883. /*
  884. * Init CAM filter
  885. */
  886. velocity_init_cam_filter(vptr);
  887. /*
  888. * Set packet filter: Receive directed and broadcast address
  889. */
  890. //FIXME Multicast velocity_set_multi(nic);
  891. /*
  892. * Enable MII auto-polling
  893. */
  894. enable_mii_autopoll(regs);
  895. vptr->int_mask = INT_MASK_DEF;
  896. writel(virt_to_le32desc(vptr->rd_ring), &regs->RDBaseLo);
  897. writew(vptr->options.numrx - 1, &regs->RDCSize);
  898. mac_rx_queue_run(regs);
  899. mac_rx_queue_wake(regs);
  900. writew(vptr->options.numtx - 1, &regs->TDCSize);
  901. // for (i = 0; i < vptr->num_txq; i++) {
  902. writel(virt_to_le32desc(vptr->td_rings),
  903. &(regs->TDBaseLo[0]));
  904. mac_tx_queue_run(regs, 0);
  905. // }
  906. init_flow_control_register(vptr);
  907. writel(CR0_STOP, &regs->CR0Clr);
  908. writel((CR0_DPOLL | CR0_TXON | CR0_RXON | CR0_STRT),
  909. &regs->CR0Set);
  910. mii_status = velocity_get_opt_media_mode(vptr);
  911. // netif_stop_queue(vptr->dev);
  912. mii_init(vptr, mii_status);
  913. if (velocity_set_media_mode(vptr, mii_status) !=
  914. VELOCITY_LINK_CHANGE) {
  915. velocity_print_link_status(vptr);
  916. if (!(vptr->mii_status & VELOCITY_LINK_FAIL))
  917. printf("Link Faaailll\n");
  918. // netif_wake_queue(vptr->dev);
  919. }
  920. enable_flow_control_ability(vptr);
  921. mac_hw_mibs_init(regs);
  922. mac_write_int_mask(vptr->int_mask, regs);
  923. mac_clear_isr(regs);
  924. }
  925. velocity_print_link_status(vptr);
  926. }
  927. /**
  928. * velocity_soft_reset - soft reset
  929. * @vptr: velocity to reset
  930. *
  931. * Kick off a soft reset of the velocity adapter and then poll
  932. * until the reset sequence has completed before returning.
  933. */
  934. static int velocity_soft_reset(struct velocity_info *vptr)
  935. {
  936. struct mac_regs *regs = vptr->mac_regs;
  937. unsigned int i = 0;
  938. writel(CR0_SFRST, &regs->CR0Set);
  939. for (i = 0; i < W_MAX_TIMEOUT; i++) {
  940. udelay(5);
  941. if (!DWORD_REG_BITS_IS_ON(CR0_SFRST, &regs->CR0Set))
  942. break;
  943. }
  944. if (i == W_MAX_TIMEOUT) {
  945. writel(CR0_FORSRST, &regs->CR0Set);
  946. /* FIXME: PCI POSTING */
  947. /* delay 2ms */
  948. mdelay(2);
  949. }
  950. return 0;
  951. }
  952. /**
  953. * velocity_init_rings - set up DMA rings
  954. * @vptr: Velocity to set up
  955. *
  956. * Allocate PCI mapped DMA rings for the receive and transmit layer
  957. * to use.
  958. */
  959. static int velocity_init_rings(struct velocity_info *vptr)
  960. {
  961. int idx;
  962. vptr->rd_curr = 0;
  963. vptr->td_curr = 0;
  964. memset(vptr->td_rings, 0, TX_DESC_DEF * sizeof(struct tx_desc));
  965. memset(vptr->rd_ring, 0, RX_DESC_DEF * sizeof(struct rx_desc));
  966. // memset(vptr->tx_buffs, 0, TX_DESC_DEF * PKT_BUF_SZ);
  967. for (idx = 0; idx < RX_DESC_DEF; idx++) {
  968. *((u32 *) & (vptr->rd_ring[idx].rdesc0)) = 0;
  969. vptr->rd_ring[idx].len = cpu_to_le32(vptr->rx_buf_sz);
  970. vptr->rd_ring[idx].inten = 1;
  971. vptr->rd_ring[idx].pa_low =
  972. virt_to_bus(vptr->rxb + (RX_DESC_DEF * idx));
  973. vptr->rd_ring[idx].pa_high = 0;
  974. vptr->rd_ring[idx].rdesc0.owner = OWNED_BY_NIC;
  975. }
  976. /* for (i = 0; idx < TX_DESC_DEF; idx++ ) {
  977. vptr->td_rings[idx].tdesc1.TCPLS = TCPLS_NORMAL;
  978. vptr->td_rings[idx].tdesc1.TCR = TCR0_TIC;
  979. vptr->td_rings[idx].td_buf[0].queue = 0;
  980. vptr->td_rings[idx].tdesc0.owner = ~OWNED_BY_NIC;
  981. vptr->td_rings[idx].tdesc0.pktsize = 0;
  982. vptr->td_rings[idx].td_buf[0].pa_low = cpu_to_le32(virt_to_bus(vptr->txb + (idx * PKT_BUF_SZ)));
  983. vptr->td_rings[idx].td_buf[0].pa_high = 0;
  984. vptr->td_rings[idx].td_buf[0].bufsize = 0;
  985. vptr->td_rings[idx].tdesc1.CMDZ = 2;
  986. }
  987. */
  988. return 0;
  989. }
  990. /**
  991. * velocity_open - interface activation callback
  992. * @dev: network layer device to open
  993. *
  994. * Called when the network layer brings the interface up. Returns
  995. * a negative posix error code on failure, or zero on success.
  996. *
  997. * All the ring allocation and set up is done on open for this
  998. * adapter to minimise memory usage when inactive
  999. */
  1000. #define PCI_BYTE_REG_BITS_ON(x,i,p) do{\
  1001. u8 byReg;\
  1002. pci_read_config_byte((p), (i), &(byReg));\
  1003. (byReg) |= (x);\
  1004. pci_write_config_byte((p), (i), (byReg));\
  1005. } while (0)
  1006. //
  1007. // Registers in the PCI configuration space
  1008. //
  1009. #define PCI_REG_COMMAND 0x04 //
  1010. #define PCI_REG_MODE0 0x60 //
  1011. #define PCI_REG_MODE1 0x61 //
  1012. #define PCI_REG_MODE2 0x62 //
  1013. #define PCI_REG_MODE3 0x63 //
  1014. #define PCI_REG_DELAY_TIMER 0x64 //
  1015. // Bits in the (MODE2, 0x62) register
  1016. //
  1017. #define MODE2_PCEROPT 0x80 // take PCI bus ERror as a fatal and shutdown from software control
  1018. #define MODE2_TXQ16 0x40 // TX write-back Queue control. 0->32 entries available in Tx write-back queue, 1->16 entries
  1019. #define MODE2_TXPOST 0x08 // (Not support in VT3119)
  1020. #define MODE2_AUTOOPT 0x04 // (VT3119 GHCI without such behavior)
  1021. #define MODE2_MODE10T 0x02 // used to control tx Threshold for 10M case
  1022. #define MODE2_TCPLSOPT 0x01 // TCP large send field update disable, hardware will not update related fields, leave it to software.
  1023. //
  1024. // Bits in the MODE3 register
  1025. //
  1026. #define MODE3_MIION 0x04 // MII symbol codine error detect enable ??
  1027. // Bits in the (COMMAND, 0x04) register
  1028. #define COMMAND_BUSM 0x04
  1029. #define COMMAND_WAIT 0x80
  1030. static int velocity_open(struct nic *nic, struct pci_device *pci __unused)
  1031. {
  1032. int ret;
  1033. u8 diff;
  1034. u32 TxPhyAddr, RxPhyAddr;
  1035. u32 TxBufPhyAddr, RxBufPhyAddr;
  1036. vptr->TxDescArrays = tx_ring;
  1037. if (vptr->TxDescArrays == 0)
  1038. printf("Allot Error");
  1039. /* Tx Descriptor needs 64 bytes alignment; */
  1040. TxPhyAddr = virt_to_bus(vptr->TxDescArrays);
  1041. printf("Unaligned Address : %lX\n", TxPhyAddr);
  1042. diff = 64 - (TxPhyAddr - ((TxPhyAddr >> 6) << 6));
  1043. TxPhyAddr += diff;
  1044. vptr->td_rings = (struct tx_desc *) (vptr->TxDescArrays + diff);
  1045. printf("Aligned Address: %lX\n", virt_to_bus(vptr->td_rings));
  1046. vptr->tx_buffs = txb;
  1047. /* Rx Buffer needs 64 bytes alignment; */
  1048. TxBufPhyAddr = virt_to_bus(vptr->tx_buffs);
  1049. diff = 64 - (TxBufPhyAddr - ((TxBufPhyAddr >> 6) << 6));
  1050. TxBufPhyAddr += diff;
  1051. vptr->txb = (unsigned char *) (vptr->tx_buffs + diff);
  1052. vptr->RxDescArrays = rx_ring;
  1053. /* Rx Descriptor needs 64 bytes alignment; */
  1054. RxPhyAddr = virt_to_bus(vptr->RxDescArrays);
  1055. diff = 64 - (RxPhyAddr - ((RxPhyAddr >> 6) << 6));
  1056. RxPhyAddr += diff;
  1057. vptr->rd_ring = (struct rx_desc *) (vptr->RxDescArrays + diff);
  1058. vptr->rx_buffs = rxb;
  1059. /* Rx Buffer needs 64 bytes alignment; */
  1060. RxBufPhyAddr = virt_to_bus(vptr->rx_buffs);
  1061. diff = 64 - (RxBufPhyAddr - ((RxBufPhyAddr >> 6) << 6));
  1062. RxBufPhyAddr += diff;
  1063. vptr->rxb = (unsigned char *) (vptr->rx_buffs + diff);
  1064. if (vptr->RxDescArrays == NULL || vptr->RxDescArrays == NULL) {
  1065. printf("Allocate tx_ring or rd_ring failed\n");
  1066. return 0;
  1067. }
  1068. vptr->rx_buf_sz = PKT_BUF_SZ;
  1069. /*
  1070. // turn this on to avoid retry forever
  1071. PCI_BYTE_REG_BITS_ON(MODE2_PCEROPT, PCI_REG_MODE2, pci);
  1072. // for some legacy BIOS and OS don't open BusM
  1073. // bit in PCI configuration space. So, turn it on.
  1074. PCI_BYTE_REG_BITS_ON(COMMAND_BUSM, PCI_REG_COMMAND, pci);
  1075. // turn this on to detect MII coding error
  1076. PCI_BYTE_REG_BITS_ON(MODE3_MIION, PCI_REG_MODE3, pci);
  1077. */
  1078. ret = velocity_init_rings(vptr);
  1079. /* Ensure chip is running */
  1080. //FIXME: pci_set_power_state(vptr->pdev, PCI_D0);
  1081. velocity_init_registers(nic, vptr, VELOCITY_INIT_COLD);
  1082. mac_write_int_mask(0, vptr->mac_regs);
  1083. // _int(vptr->mac_regs);
  1084. //mac_enable_int(vptr->mac_regs);
  1085. vptr->flags |= VELOCITY_FLAGS_OPENED;
  1086. return 1;
  1087. }
  1088. /*
  1089. * MII access , media link mode setting functions
  1090. */
  1091. /**
  1092. * mii_init - set up MII
  1093. * @vptr: velocity adapter
  1094. * @mii_status: links tatus
  1095. *
  1096. * Set up the PHY for the current link state.
  1097. */
  1098. static void mii_init(struct velocity_info *vptr, u32 mii_status __unused)
  1099. {
  1100. u16 BMCR;
  1101. switch (PHYID_GET_PHY_ID(vptr->phy_id)) {
  1102. case PHYID_CICADA_CS8201:
  1103. /*
  1104. * Reset to hardware default
  1105. */
  1106. MII_REG_BITS_OFF((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR,
  1107. vptr->mac_regs);
  1108. /*
  1109. * Turn on ECHODIS bit in NWay-forced full mode and turn it
  1110. * off it in NWay-forced half mode for NWay-forced v.s.
  1111. * legacy-forced issue.
  1112. */
  1113. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  1114. MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR,
  1115. vptr->mac_regs);
  1116. else
  1117. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR,
  1118. vptr->mac_regs);
  1119. /*
  1120. * Turn on Link/Activity LED enable bit for CIS8201
  1121. */
  1122. MII_REG_BITS_ON(PLED_LALBE, MII_REG_PLED, vptr->mac_regs);
  1123. break;
  1124. case PHYID_VT3216_32BIT:
  1125. case PHYID_VT3216_64BIT:
  1126. /*
  1127. * Reset to hardware default
  1128. */
  1129. MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR,
  1130. vptr->mac_regs);
  1131. /*
  1132. * Turn on ECHODIS bit in NWay-forced full mode and turn it
  1133. * off it in NWay-forced half mode for NWay-forced v.s.
  1134. * legacy-forced issue
  1135. */
  1136. if (vptr->mii_status & VELOCITY_DUPLEX_FULL)
  1137. MII_REG_BITS_ON(TCSR_ECHODIS, MII_REG_TCSR,
  1138. vptr->mac_regs);
  1139. else
  1140. MII_REG_BITS_OFF(TCSR_ECHODIS, MII_REG_TCSR,
  1141. vptr->mac_regs);
  1142. break;
  1143. case PHYID_MARVELL_1000:
  1144. case PHYID_MARVELL_1000S:
  1145. /*
  1146. * Assert CRS on Transmit
  1147. */
  1148. MII_REG_BITS_ON(PSCR_ACRSTX, MII_REG_PSCR, vptr->mac_regs);
  1149. /*
  1150. * Reset to hardware default
  1151. */
  1152. MII_REG_BITS_ON((ANAR_ASMDIR | ANAR_PAUSE), MII_REG_ANAR,
  1153. vptr->mac_regs);
  1154. break;
  1155. default:
  1156. ;
  1157. }
  1158. velocity_mii_read(vptr->mac_regs, MII_REG_BMCR, &BMCR);
  1159. if (BMCR & BMCR_ISO) {
  1160. BMCR &= ~BMCR_ISO;
  1161. velocity_mii_write(vptr->mac_regs, MII_REG_BMCR, BMCR);
  1162. }
  1163. }
  1164. /**
  1165. * safe_disable_mii_autopoll - autopoll off
  1166. * @regs: velocity registers
  1167. *
  1168. * Turn off the autopoll and wait for it to disable on the chip
  1169. */
  1170. static void safe_disable_mii_autopoll(struct mac_regs *regs)
  1171. {
  1172. u16 ww;
  1173. /* turn off MAUTO */
  1174. writeb(0, &regs->MIICR);
  1175. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  1176. udelay(1);
  1177. if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  1178. break;
  1179. }
  1180. }
  1181. /**
  1182. * enable_mii_autopoll - turn on autopolling
  1183. * @regs: velocity registers
  1184. *
  1185. * Enable the MII link status autopoll feature on the Velocity
  1186. * hardware. Wait for it to enable.
  1187. */
  1188. static void enable_mii_autopoll(struct mac_regs *regs)
  1189. {
  1190. unsigned int ii;
  1191. writeb(0, &(regs->MIICR));
  1192. writeb(MIIADR_SWMPL, &regs->MIIADR);
  1193. for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
  1194. udelay(1);
  1195. if (BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  1196. break;
  1197. }
  1198. writeb(MIICR_MAUTO, &regs->MIICR);
  1199. for (ii = 0; ii < W_MAX_TIMEOUT; ii++) {
  1200. udelay(1);
  1201. if (!BYTE_REG_BITS_IS_ON(MIISR_MIDLE, &regs->MIISR))
  1202. break;
  1203. }
  1204. }
  1205. /**
  1206. * velocity_mii_read - read MII data
  1207. * @regs: velocity registers
  1208. * @index: MII register index
  1209. * @data: buffer for received data
  1210. *
  1211. * Perform a single read of an MII 16bit register. Returns zero
  1212. * on success or -ETIMEDOUT if the PHY did not respond.
  1213. */
  1214. static int velocity_mii_read(struct mac_regs *regs, u8 index, u16 * data)
  1215. {
  1216. u16 ww;
  1217. /*
  1218. * Disable MIICR_MAUTO, so that mii addr can be set normally
  1219. */
  1220. safe_disable_mii_autopoll(regs);
  1221. writeb(index, &regs->MIIADR);
  1222. BYTE_REG_BITS_ON(MIICR_RCMD, &regs->MIICR);
  1223. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  1224. if (!(readb(&regs->MIICR) & MIICR_RCMD))
  1225. break;
  1226. }
  1227. *data = readw(&regs->MIIDATA);
  1228. enable_mii_autopoll(regs);
  1229. if (ww == W_MAX_TIMEOUT)
  1230. return -1;
  1231. return 0;
  1232. }
  1233. /**
  1234. * velocity_mii_write - write MII data
  1235. * @regs: velocity registers
  1236. * @index: MII register index
  1237. * @data: 16bit data for the MII register
  1238. *
  1239. * Perform a single write to an MII 16bit register. Returns zero
  1240. * on success or -ETIMEDOUT if the PHY did not respond.
  1241. */
  1242. static int velocity_mii_write(struct mac_regs *regs, u8 mii_addr, u16 data)
  1243. {
  1244. u16 ww;
  1245. /*
  1246. * Disable MIICR_MAUTO, so that mii addr can be set normally
  1247. */
  1248. safe_disable_mii_autopoll(regs);
  1249. /* MII reg offset */
  1250. writeb(mii_addr, &regs->MIIADR);
  1251. /* set MII data */
  1252. writew(data, &regs->MIIDATA);
  1253. /* turn on MIICR_WCMD */
  1254. BYTE_REG_BITS_ON(MIICR_WCMD, &regs->MIICR);
  1255. /* W_MAX_TIMEOUT is the timeout period */
  1256. for (ww = 0; ww < W_MAX_TIMEOUT; ww++) {
  1257. udelay(5);
  1258. if (!(readb(&regs->MIICR) & MIICR_WCMD))
  1259. break;
  1260. }
  1261. enable_mii_autopoll(regs);
  1262. if (ww == W_MAX_TIMEOUT)
  1263. return -1;
  1264. return 0;
  1265. }
  1266. /**
  1267. * velocity_get_opt_media_mode - get media selection
  1268. * @vptr: velocity adapter
  1269. *
  1270. * Get the media mode stored in EEPROM or module options and load
  1271. * mii_status accordingly. The requested link state information
  1272. * is also returned.
  1273. */
  1274. static u32 velocity_get_opt_media_mode(struct velocity_info *vptr)
  1275. {
  1276. u32 status = 0;
  1277. switch (vptr->options.spd_dpx) {
  1278. case SPD_DPX_AUTO:
  1279. status = VELOCITY_AUTONEG_ENABLE;
  1280. break;
  1281. case SPD_DPX_100_FULL:
  1282. status = VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL;
  1283. break;
  1284. case SPD_DPX_10_FULL:
  1285. status = VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL;
  1286. break;
  1287. case SPD_DPX_100_HALF:
  1288. status = VELOCITY_SPEED_100;
  1289. break;
  1290. case SPD_DPX_10_HALF:
  1291. status = VELOCITY_SPEED_10;
  1292. break;
  1293. }
  1294. vptr->mii_status = status;
  1295. return status;
  1296. }
  1297. /**
  1298. * mii_set_auto_on - autonegotiate on
  1299. * @vptr: velocity
  1300. *
  1301. * Enable autonegotation on this interface
  1302. */
  1303. static void mii_set_auto_on(struct velocity_info *vptr)
  1304. {
  1305. if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs))
  1306. MII_REG_BITS_ON(BMCR_REAUTO, MII_REG_BMCR, vptr->mac_regs);
  1307. else
  1308. MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
  1309. }
  1310. /*
  1311. static void mii_set_auto_off(struct velocity_info * vptr)
  1312. {
  1313. MII_REG_BITS_OFF(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs);
  1314. }
  1315. */
  1316. /**
  1317. * set_mii_flow_control - flow control setup
  1318. * @vptr: velocity interface
  1319. *
  1320. * Set up the flow control on this interface according to
  1321. * the supplied user/eeprom options.
  1322. */
  1323. static void set_mii_flow_control(struct velocity_info *vptr)
  1324. {
  1325. /*Enable or Disable PAUSE in ANAR */
  1326. switch (vptr->options.flow_cntl) {
  1327. case FLOW_CNTL_TX:
  1328. MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  1329. MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
  1330. break;
  1331. case FLOW_CNTL_RX:
  1332. MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  1333. MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
  1334. break;
  1335. case FLOW_CNTL_TX_RX:
  1336. MII_REG_BITS_ON(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  1337. MII_REG_BITS_ON(ANAR_ASMDIR, MII_REG_ANAR, vptr->mac_regs);
  1338. break;
  1339. case FLOW_CNTL_DISABLE:
  1340. MII_REG_BITS_OFF(ANAR_PAUSE, MII_REG_ANAR, vptr->mac_regs);
  1341. MII_REG_BITS_OFF(ANAR_ASMDIR, MII_REG_ANAR,
  1342. vptr->mac_regs);
  1343. break;
  1344. default:
  1345. break;
  1346. }
  1347. }
  1348. /**
  1349. * velocity_set_media_mode - set media mode
  1350. * @mii_status: old MII link state
  1351. *
  1352. * Check the media link state and configure the flow control
  1353. * PHY and also velocity hardware setup accordingly. In particular
  1354. * we need to set up CD polling and frame bursting.
  1355. */
  1356. static int velocity_set_media_mode(struct velocity_info *vptr,
  1357. u32 mii_status)
  1358. {
  1359. u32 curr_status;
  1360. struct mac_regs *regs = vptr->mac_regs;
  1361. vptr->mii_status = mii_check_media_mode(vptr->mac_regs);
  1362. curr_status = vptr->mii_status & (~VELOCITY_LINK_FAIL);
  1363. /* Set mii link status */
  1364. set_mii_flow_control(vptr);
  1365. /*
  1366. Check if new status is consisent with current status
  1367. if (((mii_status & curr_status) & VELOCITY_AUTONEG_ENABLE)
  1368. || (mii_status==curr_status)) {
  1369. vptr->mii_status=mii_check_media_mode(vptr->mac_regs);
  1370. vptr->mii_status=check_connection_type(vptr->mac_regs);
  1371. printf(MSG_LEVEL_INFO, "Velocity link no change\n");
  1372. return 0;
  1373. }
  1374. */
  1375. if (PHYID_GET_PHY_ID(vptr->phy_id) == PHYID_CICADA_CS8201) {
  1376. MII_REG_BITS_ON(AUXCR_MDPPS, MII_REG_AUXCR,
  1377. vptr->mac_regs);
  1378. }
  1379. /*
  1380. * If connection type is AUTO
  1381. */
  1382. if (mii_status & VELOCITY_AUTONEG_ENABLE) {
  1383. printf("Velocity is AUTO mode\n");
  1384. /* clear force MAC mode bit */
  1385. BYTE_REG_BITS_OFF(CHIPGCR_FCMODE, &regs->CHIPGCR);
  1386. /* set duplex mode of MAC according to duplex mode of MII */
  1387. MII_REG_BITS_ON(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10,
  1388. MII_REG_ANAR, vptr->mac_regs);
  1389. MII_REG_BITS_ON(G1000CR_1000FD | G1000CR_1000,
  1390. MII_REG_G1000CR, vptr->mac_regs);
  1391. MII_REG_BITS_ON(BMCR_SPEED1G, MII_REG_BMCR,
  1392. vptr->mac_regs);
  1393. /* enable AUTO-NEGO mode */
  1394. mii_set_auto_on(vptr);
  1395. } else {
  1396. u16 ANAR;
  1397. u8 CHIPGCR;
  1398. /*
  1399. * 1. if it's 3119, disable frame bursting in halfduplex mode
  1400. * and enable it in fullduplex mode
  1401. * 2. set correct MII/GMII and half/full duplex mode in CHIPGCR
  1402. * 3. only enable CD heart beat counter in 10HD mode
  1403. */
  1404. /* set force MAC mode bit */
  1405. BYTE_REG_BITS_ON(CHIPGCR_FCMODE, &regs->CHIPGCR);
  1406. CHIPGCR = readb(&regs->CHIPGCR);
  1407. CHIPGCR &= ~CHIPGCR_FCGMII;
  1408. if (mii_status & VELOCITY_DUPLEX_FULL) {
  1409. CHIPGCR |= CHIPGCR_FCFDX;
  1410. writeb(CHIPGCR, &regs->CHIPGCR);
  1411. printf
  1412. ("DEBUG: set Velocity to forced full mode\n");
  1413. if (vptr->rev_id < REV_ID_VT3216_A0)
  1414. BYTE_REG_BITS_OFF(TCR_TB2BDIS, &regs->TCR);
  1415. } else {
  1416. CHIPGCR &= ~CHIPGCR_FCFDX;
  1417. printf
  1418. ("DEBUG: set Velocity to forced half mode\n");
  1419. writeb(CHIPGCR, &regs->CHIPGCR);
  1420. if (vptr->rev_id < REV_ID_VT3216_A0)
  1421. BYTE_REG_BITS_ON(TCR_TB2BDIS, &regs->TCR);
  1422. }
  1423. MII_REG_BITS_OFF(G1000CR_1000FD | G1000CR_1000,
  1424. MII_REG_G1000CR, vptr->mac_regs);
  1425. if (!(mii_status & VELOCITY_DUPLEX_FULL)
  1426. && (mii_status & VELOCITY_SPEED_10)) {
  1427. BYTE_REG_BITS_OFF(TESTCFG_HBDIS, &regs->TESTCFG);
  1428. } else {
  1429. BYTE_REG_BITS_ON(TESTCFG_HBDIS, &regs->TESTCFG);
  1430. }
  1431. /* MII_REG_BITS_OFF(BMCR_SPEED1G, MII_REG_BMCR, vptr->mac_regs); */
  1432. velocity_mii_read(vptr->mac_regs, MII_REG_ANAR, &ANAR);
  1433. ANAR &= (~(ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10));
  1434. if (mii_status & VELOCITY_SPEED_100) {
  1435. if (mii_status & VELOCITY_DUPLEX_FULL)
  1436. ANAR |= ANAR_TXFD;
  1437. else
  1438. ANAR |= ANAR_TX;
  1439. } else {
  1440. if (mii_status & VELOCITY_DUPLEX_FULL)
  1441. ANAR |= ANAR_10FD;
  1442. else
  1443. ANAR |= ANAR_10;
  1444. }
  1445. velocity_mii_write(vptr->mac_regs, MII_REG_ANAR, ANAR);
  1446. /* enable AUTO-NEGO mode */
  1447. mii_set_auto_on(vptr);
  1448. /* MII_REG_BITS_ON(BMCR_AUTO, MII_REG_BMCR, vptr->mac_regs); */
  1449. }
  1450. /* vptr->mii_status=mii_check_media_mode(vptr->mac_regs); */
  1451. /* vptr->mii_status=check_connection_type(vptr->mac_regs); */
  1452. return VELOCITY_LINK_CHANGE;
  1453. }
  1454. /**
  1455. * mii_check_media_mode - check media state
  1456. * @regs: velocity registers
  1457. *
  1458. * Check the current MII status and determine the link status
  1459. * accordingly
  1460. */
  1461. static u32 mii_check_media_mode(struct mac_regs *regs)
  1462. {
  1463. u32 status = 0;
  1464. u16 ANAR;
  1465. if (!MII_REG_BITS_IS_ON(BMSR_LNK, MII_REG_BMSR, regs))
  1466. status |= VELOCITY_LINK_FAIL;
  1467. if (MII_REG_BITS_IS_ON(G1000CR_1000FD, MII_REG_G1000CR, regs))
  1468. status |= VELOCITY_SPEED_1000 | VELOCITY_DUPLEX_FULL;
  1469. else if (MII_REG_BITS_IS_ON(G1000CR_1000, MII_REG_G1000CR, regs))
  1470. status |= (VELOCITY_SPEED_1000);
  1471. else {
  1472. velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
  1473. if (ANAR & ANAR_TXFD)
  1474. status |=
  1475. (VELOCITY_SPEED_100 | VELOCITY_DUPLEX_FULL);
  1476. else if (ANAR & ANAR_TX)
  1477. status |= VELOCITY_SPEED_100;
  1478. else if (ANAR & ANAR_10FD)
  1479. status |=
  1480. (VELOCITY_SPEED_10 | VELOCITY_DUPLEX_FULL);
  1481. else
  1482. status |= (VELOCITY_SPEED_10);
  1483. }
  1484. if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
  1485. velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
  1486. if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
  1487. == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
  1488. if (MII_REG_BITS_IS_ON
  1489. (G1000CR_1000 | G1000CR_1000FD,
  1490. MII_REG_G1000CR, regs))
  1491. status |= VELOCITY_AUTONEG_ENABLE;
  1492. }
  1493. }
  1494. return status;
  1495. }
  1496. static u32 check_connection_type(struct mac_regs *regs)
  1497. {
  1498. u32 status = 0;
  1499. u8 PHYSR0;
  1500. u16 ANAR;
  1501. PHYSR0 = readb(&regs->PHYSR0);
  1502. /*
  1503. if (!(PHYSR0 & PHYSR0_LINKGD))
  1504. status|=VELOCITY_LINK_FAIL;
  1505. */
  1506. if (PHYSR0 & PHYSR0_FDPX)
  1507. status |= VELOCITY_DUPLEX_FULL;
  1508. if (PHYSR0 & PHYSR0_SPDG)
  1509. status |= VELOCITY_SPEED_1000;
  1510. if (PHYSR0 & PHYSR0_SPD10)
  1511. status |= VELOCITY_SPEED_10;
  1512. else
  1513. status |= VELOCITY_SPEED_100;
  1514. if (MII_REG_BITS_IS_ON(BMCR_AUTO, MII_REG_BMCR, regs)) {
  1515. velocity_mii_read(regs, MII_REG_ANAR, &ANAR);
  1516. if ((ANAR & (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10))
  1517. == (ANAR_TXFD | ANAR_TX | ANAR_10FD | ANAR_10)) {
  1518. if (MII_REG_BITS_IS_ON
  1519. (G1000CR_1000 | G1000CR_1000FD,
  1520. MII_REG_G1000CR, regs))
  1521. status |= VELOCITY_AUTONEG_ENABLE;
  1522. }
  1523. }
  1524. return status;
  1525. }
  1526. /**
  1527. * enable_flow_control_ability - flow control
  1528. * @vptr: veloity to configure
  1529. *
  1530. * Set up flow control according to the flow control options
  1531. * determined by the eeprom/configuration.
  1532. */
  1533. static void enable_flow_control_ability(struct velocity_info *vptr)
  1534. {
  1535. struct mac_regs *regs = vptr->mac_regs;
  1536. switch (vptr->options.flow_cntl) {
  1537. case FLOW_CNTL_DEFAULT:
  1538. if (BYTE_REG_BITS_IS_ON(PHYSR0_RXFLC, &regs->PHYSR0))
  1539. writel(CR0_FDXRFCEN, &regs->CR0Set);
  1540. else
  1541. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  1542. if (BYTE_REG_BITS_IS_ON(PHYSR0_TXFLC, &regs->PHYSR0))
  1543. writel(CR0_FDXTFCEN, &regs->CR0Set);
  1544. else
  1545. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  1546. break;
  1547. case FLOW_CNTL_TX:
  1548. writel(CR0_FDXTFCEN, &regs->CR0Set);
  1549. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  1550. break;
  1551. case FLOW_CNTL_RX:
  1552. writel(CR0_FDXRFCEN, &regs->CR0Set);
  1553. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  1554. break;
  1555. case FLOW_CNTL_TX_RX:
  1556. writel(CR0_FDXTFCEN, &regs->CR0Set);
  1557. writel(CR0_FDXRFCEN, &regs->CR0Set);
  1558. break;
  1559. case FLOW_CNTL_DISABLE:
  1560. writel(CR0_FDXRFCEN, &regs->CR0Clr);
  1561. writel(CR0_FDXTFCEN, &regs->CR0Clr);
  1562. break;
  1563. default:
  1564. break;
  1565. }
  1566. }
  1567. /* FIXME: Move to pci.c */
  1568. /**
  1569. * pci_set_power_state - Set the power state of a PCI device
  1570. * @dev: PCI device to be suspended
  1571. * @state: Power state we're entering
  1572. *
  1573. * Transition a device to a new power state, using the Power Management
  1574. * Capabilities in the device's config space.
  1575. *
  1576. * RETURN VALUE:
  1577. * -EINVAL if trying to enter a lower state than we're already in.
  1578. * 0 if we're already in the requested state.
  1579. * -EIO if device does not support PCI PM.
  1580. * 0 if we can successfully change the power state.
  1581. */
  1582. int pci_set_power_state(struct pci_device *dev, int state)
  1583. {
  1584. int pm;
  1585. u16 pmcsr;
  1586. int current_state = 0;
  1587. /* bound the state we're entering */
  1588. if (state > 3)
  1589. state = 3;
  1590. /* Validate current state:
  1591. * Can enter D0 from any state, but if we can only go deeper
  1592. * to sleep if we're already in a low power state
  1593. */
  1594. if (state > 0 && current_state > state)
  1595. return -1;
  1596. else if (current_state == state)
  1597. return 0; /* we're already there */
  1598. /* find PCI PM capability in list */
  1599. pm = pci_find_capability(dev, PCI_CAP_ID_PM);
  1600. /* abort if the device doesn't support PM capabilities */
  1601. if (!pm)
  1602. return -2;
  1603. /* check if this device supports the desired state */
  1604. if (state == 1 || state == 2) {
  1605. u16 pmc;
  1606. pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
  1607. if (state == 1 && !(pmc & PCI_PM_CAP_D1))
  1608. return -2;
  1609. else if (state == 2 && !(pmc & PCI_PM_CAP_D2))
  1610. return -2;
  1611. }
  1612. /* If we're in D3, force entire word to 0.
  1613. * This doesn't affect PME_Status, disables PME_En, and
  1614. * sets PowerState to 0.
  1615. */
  1616. if (current_state >= 3)
  1617. pmcsr = 0;
  1618. else {
  1619. pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
  1620. pmcsr &= ~PCI_PM_CTRL_STATE_MASK;
  1621. pmcsr |= state;
  1622. }
  1623. /* enter specified state */
  1624. pci_write_config_word(dev, pm + PCI_PM_CTRL, pmcsr);
  1625. /* Mandatory power management transition delays */
  1626. /* see PCI PM 1.1 5.6.1 table 18 */
  1627. if (state == 3 || current_state == 3)
  1628. mdelay(10);
  1629. else if (state == 2 || current_state == 2)
  1630. udelay(200);
  1631. current_state = state;
  1632. return 0;
  1633. }
  1634. static struct pci_device_id velocity_nics[] = {
  1635. PCI_ROM(0x1106, 0x3119, "via-velocity", "VIA Networking Velocity Family Gigabit Ethernet Adapter"),
  1636. };
  1637. PCI_DRIVER ( velocity_driver, velocity_nics, PCI_NO_CLASS );
  1638. DRIVER ( "VIA-VELOCITY/PCI", nic_driver, pci_driver, velocity_driver,
  1639. velocity_probe, velocity_disable );