amazon_sw.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. //-----------------------------------------------------------------------
  17. /*
  18. * Description:
  19. * Driver for Infineon Amazon 3 port switch
  20. */
  21. //-----------------------------------------------------------------------
  22. /* Author: Wu Qi Ming[[email protected]]
  23. * Created: 7-April-2004
  24. */
  25. //-----------------------------------------------------------------------
  26. /* History
  27. * Changed on: Jun 28, 2004
  28. * Changed by: [email protected]
  29. * Reason: add hardware flow control (HFC) (CONFIG_NET_HW_FLOWCONTROL)
  30. *
  31. * Changed on: Apr 6, 2005
  32. * Changed by: [email protected]
  33. * Reason : supoort port identification
  34. */
  35. // copyright 2004-2005 infineon.com
  36. // copyright 2007 john crispin <[email protected]>
  37. // copyright 2007 felix fietkau <[email protected]>
  38. // copyright 2009 hauke mehrtens <[email protected]>
  39. // TODO
  40. // port vlan code from bcrm target... the tawainese code was scrapped due to crappyness
  41. // check all the mmi reg settings and possibly document them better
  42. // verify the ethtool code
  43. // remove the while(1) stuff
  44. // further clean up and rework ... but it works for now
  45. // check the mode[]=bridge stuff
  46. // verify that the ethaddr can be set from u-boot
  47. #ifndef __KERNEL__
  48. #define __KERNEL__
  49. #endif
  50. #if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
  51. #define MODVERSIONS
  52. #endif
  53. #if defined(MODVERSIONS) && !defined(__GENKSYMS__)
  54. #include <linux/modversions.h>
  55. #endif
  56. #include <linux/module.h>
  57. #include <linux/string.h>
  58. #include <linux/sched.h>
  59. #include <linux/kernel.h>
  60. #include <linux/slab.h>
  61. #include <linux/errno.h>
  62. #include <linux/types.h>
  63. #include <linux/interrupt.h>
  64. #include <linux/mii.h>
  65. #include <asm/uaccess.h>
  66. #include <linux/in.h>
  67. #include <linux/netdevice.h>
  68. #include <linux/etherdevice.h>
  69. #include <linux/ip.h>
  70. #include <linux/tcp.h>
  71. #include <linux/skbuff.h>
  72. #include <linux/in6.h>
  73. #include <linux/proc_fs.h>
  74. #include <linux/mm.h>
  75. #include <linux/ethtool.h>
  76. #include <asm/checksum.h>
  77. #include <linux/init.h>
  78. #include <linux/platform_device.h>
  79. #include <asm/amazon/amazon.h>
  80. #include <asm/amazon/amazon_dma.h>
  81. #include <asm/amazon/amazon_sw.h>
  82. // how many mii ports are there ?
  83. #define AMAZON_SW_INT_NO 2
  84. #define ETHERNET_PACKET_DMA_BUFFER_SIZE 1536
  85. /***************************************** Module Parameters *************************************/
  86. static char mode[] = "bridge";
  87. module_param_array(mode, charp, NULL, 0);
  88. static int timeout = 1 * HZ;
  89. module_param(timeout, int, 0);
  90. int switch_init(struct net_device *dev);
  91. void switch_tx_timeout(struct net_device *dev);
  92. static struct net_device *switch_devs[2];
  93. int add_mac_table_entry(u64 entry_value)
  94. {
  95. int i;
  96. u32 data1, data2;
  97. AMAZON_SW_REG32(AMAZON_SW_ARL_CTL) = ~7;
  98. for (i = 0; i < 32; i++) {
  99. AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) = 0x80000000 | 0x20 | i;
  100. while (AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) & (0x80000000)) {};
  101. data1 = AMAZON_SW_REG32(AMAZON_SW_DATA1);
  102. data2 = AMAZON_SW_REG32(AMAZON_SW_DATA2);
  103. if ((data1 & (0x00700000)) != 0x00700000)
  104. continue;
  105. AMAZON_SW_REG32(AMAZON_SW_DATA1) = (u32) (entry_value >> 32);
  106. AMAZON_SW_REG32(AMAZON_SW_DATA2) = (u32) entry_value & 0xffffffff;
  107. AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) = 0xc0000020 | i;
  108. while (AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) & (0x80000000)) {};
  109. break;
  110. }
  111. AMAZON_SW_REG32(AMAZON_SW_ARL_CTL) |= 7;
  112. if (i >= 32)
  113. return -1;
  114. return OK;
  115. }
  116. u64 read_mac_table_entry(int index)
  117. {
  118. u32 data1, data2;
  119. u64 value;
  120. AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) = 0x80000000 | 0x20 | index;
  121. while (AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) & (0x80000000)) {};
  122. data1 = AMAZON_SW_REG32(AMAZON_SW_DATA1) & 0xffffff;
  123. data2 = AMAZON_SW_REG32(AMAZON_SW_DATA2);
  124. value = (u64) data1 << 32 | (u64) data2;
  125. return value;
  126. }
  127. int write_mac_table_entry(int index, u64 value)
  128. {
  129. u32 data1, data2;
  130. data1 = (u32) (value >> 32);
  131. data2 = (u32) value & 0xffffffff;
  132. AMAZON_SW_REG32(AMAZON_SW_DATA1) = data1;
  133. AMAZON_SW_REG32(AMAZON_SW_DATA2) = data2;
  134. AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) = 0xc0000020 | index;
  135. while (AMAZON_SW_REG32(AMAZON_SW_CPU_ACTL) & (0x80000000)) {};
  136. return OK;
  137. }
  138. u32 get_mdio_reg(int phy_addr, int reg_num)
  139. {
  140. u32 value;
  141. AMAZON_SW_REG32(AMAZON_SW_MDIO_ACC) = (3 << 30) | ((phy_addr & 0x1f) << 21) | ((reg_num & 0x1f) << 16);
  142. while (AMAZON_SW_REG32(AMAZON_SW_MDIO_ACC) & (1 << 31)) {};
  143. value = AMAZON_SW_REG32(AMAZON_SW_MDIO_ACC) & 0xffff;
  144. return value;
  145. }
  146. int set_mdio_reg(int phy_addr, int reg_num, u32 value)
  147. {
  148. AMAZON_SW_REG32(AMAZON_SW_MDIO_ACC) = (2 << 30) | ((phy_addr & 0x1f) << 21) | ((reg_num & 0x1f) << 16) | (value & 0xffff);
  149. while (AMAZON_SW_REG32(AMAZON_SW_MDIO_ACC) & (1 << 31)) {};
  150. return OK;
  151. }
  152. int auto_negotiate(int phy_addr)
  153. {
  154. u32 value = 0;
  155. value = get_mdio_reg(phy_addr, MDIO_BASE_CONTROL_REG);
  156. set_mdio_reg(phy_addr, MDIO_BASE_CONTROL_REG, (value | RESTART_AUTO_NEGOTIATION | AUTO_NEGOTIATION_ENABLE | PHY_RESET));
  157. return OK;
  158. }
  159. /*
  160. In this version of switch driver, we split the dma channels for the switch.
  161. 2 for port0 and 2 for port1. So that we can do internal bridging if necessary.
  162. In switch mode, packets coming in from port0 or port1 is able to do Destination
  163. address lookup. Packets coming from port0 with destination address of port1 should
  164. not go to pmac again. The switch hardware should be able to do the switch in the hard
  165. ware level. Packets coming from the pmac should not do the DA look up in that the
  166. desination is already known for the kernel. It only needs to go to the correct NIC to
  167. find its way out.
  168. */
  169. int amazon_sw_chip_init(void)
  170. {
  171. u32 tmp1;
  172. int i = 0;
  173. /* Aging tick select: 5mins */
  174. tmp1 = 0xa0;
  175. if (strcmp(mode, "bridge") == 0) {
  176. // bridge mode, set militarised mode to 1, no learning!
  177. tmp1 |= 0xC00;
  178. } else {
  179. // enable learning for P0 and P1,
  180. tmp1 |= 3;
  181. }
  182. /* unknown broadcast/multicast/unicast to all ports */
  183. AMAZON_SW_REG32(AMAZON_SW_UN_DEST) = 0x1ff;
  184. AMAZON_SW_REG32(AMAZON_SW_ARL_CTL) = tmp1;
  185. /* OCS:1 set OCS bit, split the two NIC in rx direction EDL:1 (enable DA lookup) */
  186. #if defined(CONFIG_IFX_NFEXT_AMAZON_SWITCH_PHYPORT) || defined(CONFIG_IFX_NFEXT_AMAZON_SWITCH_PHYPORT_MODULE)
  187. AMAZON_SW_REG32(AMAZON_SW_P2_PCTL) = 0x700;
  188. #else
  189. AMAZON_SW_REG32(AMAZON_SW_P2_PCTL) = 0x401;
  190. #endif
  191. /* EPC: 1 split the two NIC in tx direction CRC is generated */
  192. AMAZON_SW_REG32(AMAZON_SW_P2_CTL) = 0x6;
  193. // for bi-directional
  194. AMAZON_SW_REG32(AMAZON_SW_P0_WM) = 0x14141412;
  195. AMAZON_SW_REG32(AMAZON_SW_P1_WM) = 0x14141412;
  196. AMAZON_SW_REG32(AMAZON_SW_P2_WM) = 0x28282826;
  197. AMAZON_SW_REG32(AMAZON_SW_GBL_WM) = 0x0;
  198. AMAZON_SW_REG32(AMAZON_CGU_PLL0SR) = (AMAZON_SW_REG32(AMAZON_CGU_PLL0SR)) | 0x58000000;
  199. // clock for PHY
  200. AMAZON_SW_REG32(AMAZON_CGU_IFCCR) = (AMAZON_SW_REG32(AMAZON_CGU_IFCCR)) | 0x80000004;
  201. // enable power for PHY
  202. AMAZON_SW_REG32(AMAZON_PMU_PWDCR) = (AMAZON_SW_REG32(AMAZON_PMU_PWDCR)) | AMAZON_PMU_PWDCR_EPHY;
  203. // set reverse MII, enable MDIO statemachine
  204. AMAZON_SW_REG32(AMAZON_SW_MDIO_CFG) = 0x800027bf;
  205. while (1)
  206. if (((AMAZON_SW_REG32(AMAZON_SW_MDIO_CFG)) & 0x80000000) == 0)
  207. break;
  208. AMAZON_SW_REG32(AMAZON_SW_EPHY) = 0xff;
  209. // auto negotiation
  210. AMAZON_SW_REG32(AMAZON_SW_MDIO_ACC) = 0x83e08000;
  211. auto_negotiate(0x1f);
  212. /* enable all ports */
  213. AMAZON_SW_REG32(AMAZON_SW_PS_CTL) = 0x7;
  214. for (i = 0; i < 32; i++)
  215. write_mac_table_entry(i, 1 << 50);
  216. return 0;
  217. }
  218. static unsigned char my_ethaddr[MAX_ADDR_LEN];
  219. /* need to get the ether addr from u-boot */
  220. static int __init ethaddr_setup(char *line)
  221. {
  222. char *ep;
  223. int i;
  224. memset(my_ethaddr, 0, MAX_ADDR_LEN);
  225. for (i = 0; i < 6; i++) {
  226. my_ethaddr[i] = line ? simple_strtoul(line, &ep, 16) : 0;
  227. if (line)
  228. line = (*ep) ? ep + 1 : ep;
  229. }
  230. printk(KERN_INFO "amazon_mii0: mac address %2x-%2x-%2x-%2x-%2x-%2x \n", my_ethaddr[0], my_ethaddr[1], my_ethaddr[2], my_ethaddr[3], my_ethaddr[4], my_ethaddr[5]);
  231. return 0;
  232. }
  233. __setup("ethaddr=", ethaddr_setup);
  234. static void open_rx_dma(struct net_device *dev)
  235. {
  236. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  237. struct dma_device_info *dma_dev = priv->dma_device;
  238. int i;
  239. for (i = 0; i < dma_dev->num_rx_chan; i++)
  240. dma_dev->rx_chan[i].control = 1;
  241. dma_device_update_rx(dma_dev);
  242. }
  243. #ifdef CONFIG_NET_HW_FLOWCONTROL
  244. static void close_rx_dma(struct net_device *dev)
  245. {
  246. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  247. struct dma_device_info *dma_dev = priv->dma_device;
  248. int i;
  249. for (i = 0; i < dma_dev->num_rx_chan; i++)
  250. dma_dev->rx_chan[i].control = 0;
  251. dma_device_update_rx(dma_dev);
  252. }
  253. void amazon_xon(struct net_device *dev)
  254. {
  255. unsigned long flag;
  256. local_irq_save(flag);
  257. open_rx_dma(dev);
  258. local_irq_restore(flag);
  259. }
  260. #endif
  261. int switch_open(struct net_device *dev)
  262. {
  263. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  264. if (!strcmp(dev->name, "eth1")) {
  265. priv->mdio_phy_addr = PHY0_ADDR;
  266. }
  267. open_rx_dma(dev);
  268. #ifdef CONFIG_NET_HW_FLOWCONTROL
  269. if ((priv->fc_bit = netdev_register_fc(dev, amazon_xon)) == 0) {
  270. printk(KERN_WARNING "amazon_mii0: Hardware Flow Control register fails\n");
  271. }
  272. #endif
  273. netif_start_queue(dev);
  274. return OK;
  275. }
  276. int switch_release(struct net_device *dev)
  277. {
  278. int i;
  279. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  280. struct dma_device_info *dma_dev = priv->dma_device;
  281. for (i = 0; i < dma_dev->num_tx_chan; i++)
  282. dma_dev->tx_chan[i].control = 0;
  283. for (i = 0; i < dma_dev->num_rx_chan; i++)
  284. dma_dev->rx_chan[i].control = 0;
  285. dma_device_update(dma_dev);
  286. #ifdef CONFIG_NET_HW_FLOWCONTROL
  287. if (priv->fc_bit) {
  288. netdev_unregister_fc(priv->fc_bit);
  289. }
  290. #endif
  291. netif_stop_queue(dev);
  292. return OK;
  293. }
  294. void switch_rx(struct net_device *dev, int len, struct sk_buff *skb)
  295. {
  296. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  297. #ifdef CONFIG_NET_HW_FLOWCONTROL
  298. int mit_sel = 0;
  299. #endif
  300. skb->dev = dev;
  301. skb->protocol = eth_type_trans(skb, dev);
  302. #ifdef CONFIG_NET_HW_FLOWCONTROL
  303. mit_sel = netif_rx(skb);
  304. switch (mit_sel) {
  305. case NET_RX_SUCCESS:
  306. case NET_RX_CN_LOW:
  307. case NET_RX_CN_MOD:
  308. break;
  309. case NET_RX_CN_HIGH:
  310. break;
  311. case NET_RX_DROP:
  312. if ((priv->fc_bit)
  313. && (!test_and_set_bit(priv->fc_bit, &netdev_fc_xoff))) {
  314. close_rx_dma(dev);
  315. }
  316. break;
  317. }
  318. #else
  319. netif_rx(skb);
  320. #endif
  321. priv->stats.rx_packets++;
  322. priv->stats.rx_bytes += len;
  323. return;
  324. }
  325. int asmlinkage switch_hw_tx(char *buf, int len, struct net_device *dev)
  326. {
  327. struct switch_priv *priv = netdev_priv(dev);
  328. struct dma_device_info *dma_dev = priv->dma_device;
  329. dma_dev->current_tx_chan = 0;
  330. return dma_device_write(dma_dev, buf, len, priv->skb);
  331. }
  332. int asmlinkage switch_tx(struct sk_buff *skb, struct net_device *dev)
  333. {
  334. int len;
  335. char *data;
  336. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  337. len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
  338. data = skb->data;
  339. priv->skb = skb;
  340. dev->trans_start = jiffies;
  341. if (switch_hw_tx(data, len, dev) != len) {
  342. dev_kfree_skb_any(skb);
  343. return OK;
  344. }
  345. priv->stats.tx_packets++;
  346. priv->stats.tx_bytes += len;
  347. return OK;
  348. }
  349. void switch_tx_timeout(struct net_device *dev)
  350. {
  351. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  352. priv->stats.tx_errors++;
  353. netif_wake_queue(dev);
  354. return;
  355. }
  356. void negotiate(struct net_device *dev)
  357. {
  358. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  359. unsigned short data = get_mdio_reg(priv->mdio_phy_addr, MDIO_ADVERTISMENT_REG);
  360. data &= ~(MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD);
  361. switch (priv->current_speed_selection) {
  362. case 10:
  363. if (priv->current_duplex == full)
  364. data |= MDIO_ADVERT_10_FD;
  365. else if (priv->current_duplex == half)
  366. data |= MDIO_ADVERT_10_HD;
  367. else
  368. data |= MDIO_ADVERT_10_HD | MDIO_ADVERT_10_FD;
  369. break;
  370. case 100:
  371. if (priv->current_duplex == full)
  372. data |= MDIO_ADVERT_100_FD;
  373. else if (priv->current_duplex == half)
  374. data |= MDIO_ADVERT_100_HD;
  375. else
  376. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD;
  377. break;
  378. case 0: /* Auto */
  379. if (priv->current_duplex == full)
  380. data |= MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD;
  381. else if (priv->current_duplex == half)
  382. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_10_HD;
  383. else
  384. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
  385. break;
  386. default: /* assume autoneg speed and duplex */
  387. data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
  388. }
  389. set_mdio_reg(priv->mdio_phy_addr, MDIO_ADVERTISMENT_REG, data);
  390. /* Renegotiate with link partner */
  391. data = get_mdio_reg(priv->mdio_phy_addr, MDIO_BASE_CONTROL_REG);
  392. data |= MDIO_BC_NEGOTIATE;
  393. set_mdio_reg(priv->mdio_phy_addr, MDIO_BASE_CONTROL_REG, data);
  394. }
  395. void set_duplex(struct net_device *dev, enum duplex new_duplex)
  396. {
  397. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  398. if (new_duplex != priv->current_duplex) {
  399. priv->current_duplex = new_duplex;
  400. negotiate(dev);
  401. }
  402. }
  403. void set_speed(struct net_device *dev, unsigned long speed)
  404. {
  405. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  406. priv->current_speed_selection = speed;
  407. negotiate(dev);
  408. }
  409. static int switch_ethtool_ioctl(struct net_device *dev, struct ifreq *ifr)
  410. {
  411. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  412. struct ethtool_cmd ecmd;
  413. if (copy_from_user(&ecmd, ifr->ifr_data, sizeof(ecmd)))
  414. return -EFAULT;
  415. switch (ecmd.cmd) {
  416. case ETHTOOL_GSET:
  417. memset((void *) &ecmd, 0, sizeof(ecmd));
  418. ecmd.supported = SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
  419. SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
  420. ecmd.port = PORT_TP;
  421. ecmd.transceiver = XCVR_EXTERNAL;
  422. ecmd.phy_address = priv->mdio_phy_addr;
  423. ecmd.speed = priv->current_speed;
  424. ecmd.duplex = priv->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  425. ecmd.advertising = ADVERTISED_TP;
  426. if (priv->current_duplex == autoneg && priv->current_speed_selection == 0)
  427. ecmd.advertising |= ADVERTISED_Autoneg;
  428. else {
  429. ecmd.advertising |= ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
  430. ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
  431. if (priv->current_speed_selection == 10)
  432. ecmd.advertising &= ~(ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full);
  433. else if (priv->current_speed_selection == 100)
  434. ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full);
  435. if (priv->current_duplex == half)
  436. ecmd.advertising &= ~(ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Full);
  437. else if (priv->current_duplex == full)
  438. ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_100baseT_Half);
  439. }
  440. ecmd.autoneg = AUTONEG_ENABLE;
  441. if (copy_to_user(ifr->ifr_data, &ecmd, sizeof(ecmd)))
  442. return -EFAULT;
  443. break;
  444. case ETHTOOL_SSET:
  445. if (!capable(CAP_NET_ADMIN)) {
  446. return -EPERM;
  447. }
  448. if (ecmd.autoneg == AUTONEG_ENABLE) {
  449. set_duplex(dev, autoneg);
  450. set_speed(dev, 0);
  451. } else {
  452. set_duplex(dev, ecmd.duplex == DUPLEX_HALF ? half : full);
  453. set_speed(dev, ecmd.speed == SPEED_10 ? 10 : 100);
  454. }
  455. break;
  456. case ETHTOOL_GDRVINFO:
  457. {
  458. struct ethtool_drvinfo info;
  459. memset((void *) &info, 0, sizeof(info));
  460. strncpy(info.driver, "AMAZONE", sizeof(info.driver) - 1);
  461. strncpy(info.fw_version, "N/A", sizeof(info.fw_version) - 1);
  462. strncpy(info.bus_info, "N/A", sizeof(info.bus_info) - 1);
  463. info.regdump_len = 0;
  464. info.eedump_len = 0;
  465. info.testinfo_len = 0;
  466. if (copy_to_user(ifr->ifr_data, &info, sizeof(info)))
  467. return -EFAULT;
  468. }
  469. break;
  470. case ETHTOOL_NWAY_RST:
  471. if (priv->current_duplex == autoneg && priv->current_speed_selection == 0)
  472. negotiate(dev);
  473. break;
  474. default:
  475. return -EOPNOTSUPP;
  476. break;
  477. }
  478. return 0;
  479. }
  480. int mac_table_tools_ioctl(struct net_device *dev, struct mac_table_req *req)
  481. {
  482. int cmd;
  483. int i;
  484. cmd = req->cmd;
  485. switch (cmd) {
  486. case RESET_MAC_TABLE:
  487. for (i = 0; i < 32; i++) {
  488. write_mac_table_entry(i, 0);
  489. }
  490. break;
  491. case READ_MAC_ENTRY:
  492. req->entry_value = read_mac_table_entry(req->index);
  493. break;
  494. case WRITE_MAC_ENTRY:
  495. write_mac_table_entry(req->index, req->entry_value);
  496. break;
  497. case ADD_MAC_ENTRY:
  498. add_mac_table_entry(req->entry_value);
  499. break;
  500. default:
  501. return -EINVAL;
  502. }
  503. return 0;
  504. }
  505. /*
  506. the ioctl for the switch driver is developed in the conventional way
  507. the control type falls into some basic categories, among them, the
  508. SIOCETHTOOL is the traditional eth interface. VLAN_TOOLS and
  509. MAC_TABLE_TOOLS are designed specifically for amazon chip. User
  510. should be aware of the data structures used in these interfaces.
  511. */
  512. int switch_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  513. {
  514. struct data_req *switch_data_req = (struct data_req *) ifr->ifr_data;
  515. struct mac_table_req *switch_mac_table_req;
  516. switch (cmd) {
  517. case SIOCETHTOOL:
  518. switch_ethtool_ioctl(dev, ifr);
  519. break;
  520. case SIOCGMIIPHY: /* Get PHY address */
  521. break;
  522. case SIOCGMIIREG: /* Read MII register */
  523. break;
  524. case SIOCSMIIREG: /* Write MII register */
  525. break;
  526. case SET_ETH_SPEED_10: /* 10 Mbps */
  527. break;
  528. case SET_ETH_SPEED_100: /* 100 Mbps */
  529. break;
  530. case SET_ETH_SPEED_AUTO: /* Auto negotiate speed */
  531. break;
  532. case SET_ETH_DUPLEX_HALF: /* Half duplex. */
  533. break;
  534. case SET_ETH_DUPLEX_FULL: /* Full duplex. */
  535. break;
  536. case SET_ETH_DUPLEX_AUTO: /* Autonegotiate duplex */
  537. break;
  538. case SET_ETH_REG:
  539. AMAZON_SW_REG32(switch_data_req->index) = switch_data_req->value;
  540. break;
  541. case MAC_TABLE_TOOLS:
  542. switch_mac_table_req = (struct mac_table_req *) ifr->ifr_data;
  543. mac_table_tools_ioctl(dev, switch_mac_table_req);
  544. break;
  545. default:
  546. return -EINVAL;
  547. }
  548. return 0;
  549. }
  550. struct net_device_stats *switch_stats(struct net_device *dev)
  551. {
  552. struct switch_priv *priv = (struct switch_priv *) netdev_priv(dev);
  553. return &priv->stats;
  554. }
  555. int switch_change_mtu(struct net_device *dev, int new_mtu)
  556. {
  557. if (new_mtu >= 1516)
  558. new_mtu = 1516;
  559. dev->mtu = new_mtu;
  560. return 0;
  561. }
  562. int switch_hw_receive(struct net_device *dev, struct dma_device_info *dma_dev)
  563. {
  564. u8 *buf = NULL;
  565. int len = 0;
  566. struct sk_buff *skb = NULL;
  567. len = dma_device_read(dma_dev, &buf, (void **) &skb);
  568. if (len >= 0x600) {
  569. printk(KERN_WARNING "amazon_mii0: packet too large %d\n", len);
  570. goto switch_hw_receive_err_exit;
  571. }
  572. /* remove CRC */
  573. len -= 4;
  574. if (skb == NULL) {
  575. printk(KERN_WARNING "amazon_mii0: cannot restore pointer\n");
  576. goto switch_hw_receive_err_exit;
  577. }
  578. if (len > (skb->end - skb->tail)) {
  579. printk(KERN_WARNING "amazon_mii0: BUG, len:%d end:%p tail:%p\n", (len + 4), skb->end, skb->tail);
  580. goto switch_hw_receive_err_exit;
  581. }
  582. skb_put(skb, len);
  583. skb->dev = dev;
  584. switch_rx(dev, len, skb);
  585. return OK;
  586. switch_hw_receive_err_exit:
  587. if (skb)
  588. dev_kfree_skb_any(skb);
  589. return -EIO;
  590. }
  591. int dma_intr_handler(struct dma_device_info *dma_dev, int status)
  592. {
  593. struct net_device *dev;
  594. dev = dma_dev->priv;
  595. switch (status) {
  596. case RCV_INT:
  597. switch_hw_receive(dev, dma_dev);
  598. break;
  599. case TX_BUF_FULL_INT:
  600. netif_stop_queue(dev);
  601. break;
  602. case TRANSMIT_CPT_INT:
  603. netif_wake_queue(dev);
  604. break;
  605. }
  606. return OK;
  607. }
  608. /* reserve 2 bytes in front of data pointer*/
  609. u8 *dma_buffer_alloc(int len, int *byte_offset, void **opt)
  610. {
  611. u8 *buffer = NULL;
  612. struct sk_buff *skb = NULL;
  613. skb = dev_alloc_skb(ETHERNET_PACKET_DMA_BUFFER_SIZE);
  614. if (skb == NULL) {
  615. return NULL;
  616. }
  617. buffer = (u8 *) (skb->data);
  618. skb_reserve(skb, 2);
  619. *(int *) opt = (int) skb;
  620. *byte_offset = 2;
  621. return buffer;
  622. }
  623. int dma_buffer_free(u8 * dataptr, void *opt)
  624. {
  625. struct sk_buff *skb = NULL;
  626. if (opt == NULL) {
  627. kfree(dataptr);
  628. } else {
  629. skb = (struct sk_buff *) opt;
  630. dev_kfree_skb_any(skb);
  631. }
  632. return OK;
  633. }
  634. int init_dma_device(_dma_device_info * dma_dev, struct net_device *dev)
  635. {
  636. int i;
  637. int num_tx_chan, num_rx_chan;
  638. if (strcmp(dma_dev->device_name, "switch1") == 0) {
  639. num_tx_chan = 1;
  640. num_rx_chan = 2;
  641. } else {
  642. num_tx_chan = 1;
  643. num_rx_chan = 2;
  644. }
  645. dma_dev->priv = dev;
  646. dma_dev->weight = 1;
  647. dma_dev->num_tx_chan = num_tx_chan;
  648. dma_dev->num_rx_chan = num_rx_chan;
  649. dma_dev->ack = 1;
  650. dma_dev->tx_burst_len = 4;
  651. dma_dev->rx_burst_len = 4;
  652. for (i = 0; i < dma_dev->num_tx_chan; i++) {
  653. dma_dev->tx_chan[i].weight = QOS_DEFAULT_WGT;
  654. dma_dev->tx_chan[i].desc_num = 10;
  655. dma_dev->tx_chan[i].packet_size = 0;
  656. dma_dev->tx_chan[i].control = 0;
  657. }
  658. for (i = 0; i < num_rx_chan; i++) {
  659. dma_dev->rx_chan[i].weight = QOS_DEFAULT_WGT;
  660. dma_dev->rx_chan[i].desc_num = 10;
  661. dma_dev->rx_chan[i].packet_size = ETHERNET_PACKET_DMA_BUFFER_SIZE;
  662. dma_dev->rx_chan[i].control = 0;
  663. }
  664. dma_dev->intr_handler = dma_intr_handler;
  665. dma_dev->buffer_alloc = dma_buffer_alloc;
  666. dma_dev->buffer_free = dma_buffer_free;
  667. return 0;
  668. }
  669. int switch_set_mac_address(struct net_device *dev, void *p)
  670. {
  671. struct sockaddr *addr = p;
  672. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  673. return OK;
  674. }
  675. static const struct net_device_ops amazon_mii_ops = {
  676. .ndo_init = switch_init,
  677. .ndo_open = switch_open,
  678. .ndo_stop = switch_release,
  679. .ndo_start_xmit = switch_tx,
  680. .ndo_do_ioctl = switch_ioctl,
  681. .ndo_get_stats = switch_stats,
  682. .ndo_change_mtu = switch_change_mtu,
  683. .ndo_set_mac_address = switch_set_mac_address,
  684. .ndo_tx_timeout = switch_tx_timeout,
  685. };
  686. int switch_init(struct net_device *dev)
  687. {
  688. u64 retval = 0;
  689. int i;
  690. int result;
  691. struct switch_priv *priv;
  692. ether_setup(dev); /* assign some of the fields */
  693. printk(KERN_INFO "amazon_mii0: %s up using ", dev->name);
  694. dev->watchdog_timeo = timeout;
  695. priv = netdev_priv(dev);
  696. priv->dma_device = (struct dma_device_info *) kmalloc(sizeof(struct dma_device_info), GFP_KERNEL);
  697. if (priv->num == 0) {
  698. sprintf(priv->dma_device->device_name, "switch1");
  699. } else if (priv->num == 1) {
  700. sprintf(priv->dma_device->device_name, "switch2");
  701. }
  702. printk("\"%s\"\n", priv->dma_device->device_name);
  703. init_dma_device(priv->dma_device, dev);
  704. result = dma_device_register(priv->dma_device);
  705. /* read the mac address from the mac table and put them into the mac table. */
  706. for (i = 0; i < 6; i++) {
  707. retval += my_ethaddr[i];
  708. }
  709. /* ethaddr not set in u-boot ? */
  710. if (retval == 0) {
  711. dev->dev_addr[0] = 0x00;
  712. dev->dev_addr[1] = 0x20;
  713. dev->dev_addr[2] = 0xda;
  714. dev->dev_addr[3] = 0x86;
  715. dev->dev_addr[4] = 0x23;
  716. dev->dev_addr[5] = 0x74 + (unsigned char) priv->num;
  717. } else {
  718. for (i = 0; i < 6; i++) {
  719. dev->dev_addr[i] = my_ethaddr[i];
  720. }
  721. dev->dev_addr[5] += +(unsigned char) priv->num;
  722. }
  723. return OK;
  724. }
  725. static int amazon_mii_probe(struct platform_device *dev)
  726. {
  727. int i = 0, result, device_present = 0;
  728. struct switch_priv *priv;
  729. for (i = 0; i < AMAZON_SW_INT_NO; i++) {
  730. switch_devs[i] = alloc_etherdev(sizeof(struct switch_priv));
  731. switch_devs[i]->netdev_ops = &amazon_mii_ops;
  732. strcpy(switch_devs[i]->name, "eth%d");
  733. priv = (struct switch_priv *) netdev_priv(switch_devs[i]);
  734. priv->num = i;
  735. if ((result = register_netdev(switch_devs[i])))
  736. printk(KERN_WARNING "amazon_mii0: error %i registering device \"%s\"\n", result, switch_devs[i]->name);
  737. else
  738. device_present++;
  739. }
  740. amazon_sw_chip_init();
  741. return device_present ? 0 : -ENODEV;
  742. }
  743. static int amazon_mii_remove(struct platform_device *dev)
  744. {
  745. int i;
  746. struct switch_priv *priv;
  747. for (i = 0; i < AMAZON_SW_INT_NO; i++) {
  748. priv = netdev_priv(switch_devs[i]);
  749. if (priv->dma_device) {
  750. dma_device_unregister(priv->dma_device);
  751. kfree(priv->dma_device);
  752. }
  753. kfree(netdev_priv(switch_devs[i]));
  754. unregister_netdev(switch_devs[i]);
  755. }
  756. return 0;
  757. }
  758. static struct platform_driver amazon_mii_driver = {
  759. .probe = amazon_mii_probe,
  760. .remove = amazon_mii_remove,
  761. .driver = {
  762. .name = "amazon_mii0",
  763. .owner = THIS_MODULE,
  764. },
  765. };
  766. static int __init amazon_mii_init(void)
  767. {
  768. int ret = platform_driver_register(&amazon_mii_driver);
  769. if (ret)
  770. printk(KERN_WARNING "amazon_mii0: Error registering platfom driver!\n");
  771. return ret;
  772. }
  773. static void __exit amazon_mii_cleanup(void)
  774. {
  775. platform_driver_unregister(&amazon_mii_driver);
  776. }
  777. module_init(amazon_mii_init);
  778. module_exit(amazon_mii_cleanup);
  779. MODULE_LICENSE("GPL");
  780. MODULE_AUTHOR("Wu Qi Ming");
  781. MODULE_DESCRIPTION("ethernet driver for AMAZON boards");