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 53KB

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