110-ar2313_ethernet.patch 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. --- a/drivers/net/ethernet/atheros/Makefile
  2. +++ b/drivers/net/ethernet/atheros/Makefile
  3. @@ -7,3 +7,4 @@ obj-$(CONFIG_ATL2) += atlx/
  4. obj-$(CONFIG_ATL1E) += atl1e/
  5. obj-$(CONFIG_ATL1C) += atl1c/
  6. obj-$(CONFIG_ALX) += alx/
  7. +obj-$(CONFIG_NET_AR231X) += ar231x/
  8. --- a/drivers/net/ethernet/atheros/Kconfig
  9. +++ b/drivers/net/ethernet/atheros/Kconfig
  10. @@ -5,7 +5,7 @@
  11. config NET_VENDOR_ATHEROS
  12. bool "Atheros devices"
  13. default y
  14. - depends on PCI
  15. + depends on (PCI || ATH25)
  16. ---help---
  17. If you have a network (Ethernet) card belonging to this class, say Y.
  18. @@ -78,4 +78,10 @@ config ALX
  19. To compile this driver as a module, choose M here. The module
  20. will be called alx.
  21. +config NET_AR231X
  22. + tristate "Atheros AR231X built-in Ethernet support"
  23. + depends on ATH25
  24. + help
  25. + Support for the AR231x/531x ethernet controller
  26. +
  27. endif # NET_VENDOR_ATHEROS
  28. --- /dev/null
  29. +++ b/drivers/net/ethernet/atheros/ar231x/Makefile
  30. @@ -0,0 +1 @@
  31. +obj-$(CONFIG_NET_AR231X) += ar231x.o
  32. --- /dev/null
  33. +++ b/drivers/net/ethernet/atheros/ar231x/ar231x.c
  34. @@ -0,0 +1,1206 @@
  35. +/*
  36. + * ar231x.c: Linux driver for the Atheros AR231x Ethernet device.
  37. + *
  38. + * Copyright (C) 2004 by Sameer Dekate <sdekate@arubanetworks.com>
  39. + * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
  40. + * Copyright (C) 2006-2009 Felix Fietkau <nbd@nbd.name>
  41. + *
  42. + * Thanks to Atheros for providing hardware and documentation
  43. + * enabling me to write this driver.
  44. + *
  45. + * This program is free software; you can redistribute it and/or modify
  46. + * it under the terms of the GNU General Public License as published by
  47. + * the Free Software Foundation; either version 2 of the License, or
  48. + * (at your option) any later version.
  49. + *
  50. + * Additional credits:
  51. + * This code is taken from John Taylor's Sibyte driver and then
  52. + * modified for the AR2313.
  53. + */
  54. +
  55. +#include <linux/module.h>
  56. +#include <linux/types.h>
  57. +#include <linux/errno.h>
  58. +#include <linux/ioport.h>
  59. +#include <linux/netdevice.h>
  60. +#include <linux/etherdevice.h>
  61. +#include <linux/interrupt.h>
  62. +#include <linux/skbuff.h>
  63. +#include <linux/init.h>
  64. +#include <linux/delay.h>
  65. +#include <linux/mm.h>
  66. +#include <linux/mii.h>
  67. +#include <linux/phy.h>
  68. +#include <linux/platform_device.h>
  69. +#include <linux/io.h>
  70. +
  71. +#define AR2313_MTU 1692
  72. +#define AR2313_PRIOS 1
  73. +#define AR2313_QUEUES (2*AR2313_PRIOS)
  74. +#define AR2313_DESCR_ENTRIES 64
  75. +
  76. +#ifndef min
  77. +#define min(a, b) (((a) < (b)) ? (a) : (b))
  78. +#endif
  79. +
  80. +#ifndef SMP_CACHE_BYTES
  81. +#define SMP_CACHE_BYTES L1_CACHE_BYTES
  82. +#endif
  83. +
  84. +#define AR2313_MBOX_SET_BIT 0x8
  85. +
  86. +#include "ar231x.h"
  87. +
  88. +/**
  89. + * New interrupt handler strategy:
  90. + *
  91. + * An old interrupt handler worked using the traditional method of
  92. + * replacing an skbuff with a new one when a packet arrives. However
  93. + * the rx rings do not need to contain a static number of buffer
  94. + * descriptors, thus it makes sense to move the memory allocation out
  95. + * of the main interrupt handler and do it in a bottom half handler
  96. + * and only allocate new buffers when the number of buffers in the
  97. + * ring is below a certain threshold. In order to avoid starving the
  98. + * NIC under heavy load it is however necessary to force allocation
  99. + * when hitting a minimum threshold. The strategy for alloction is as
  100. + * follows:
  101. + *
  102. + * RX_LOW_BUF_THRES - allocate buffers in the bottom half
  103. + * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
  104. + * the buffers in the interrupt handler
  105. + * RX_RING_THRES - maximum number of buffers in the rx ring
  106. + *
  107. + * One advantagous side effect of this allocation approach is that the
  108. + * entire rx processing can be done without holding any spin lock
  109. + * since the rx rings and registers are totally independent of the tx
  110. + * ring and its registers. This of course includes the kmalloc's of
  111. + * new skb's. Thus start_xmit can run in parallel with rx processing
  112. + * and the memory allocation on SMP systems.
  113. + *
  114. + * Note that running the skb reallocation in a bottom half opens up
  115. + * another can of races which needs to be handled properly. In
  116. + * particular it can happen that the interrupt handler tries to run
  117. + * the reallocation while the bottom half is either running on another
  118. + * CPU or was interrupted on the same CPU. To get around this the
  119. + * driver uses bitops to prevent the reallocation routines from being
  120. + * reentered.
  121. + *
  122. + * TX handling can also be done without holding any spin lock, wheee
  123. + * this is fun! since tx_csm is only written to by the interrupt
  124. + * handler.
  125. + */
  126. +
  127. +/**
  128. + * Threshold values for RX buffer allocation - the low water marks for
  129. + * when to start refilling the rings are set to 75% of the ring
  130. + * sizes. It seems to make sense to refill the rings entirely from the
  131. + * intrrupt handler once it gets below the panic threshold, that way
  132. + * we don't risk that the refilling is moved to another CPU when the
  133. + * one running the interrupt handler just got the slab code hot in its
  134. + * cache.
  135. + */
  136. +#define RX_RING_SIZE AR2313_DESCR_ENTRIES
  137. +#define RX_PANIC_THRES (RX_RING_SIZE/4)
  138. +#define RX_LOW_THRES ((3*RX_RING_SIZE)/4)
  139. +#define CRC_LEN 4
  140. +#define RX_OFFSET 2
  141. +
  142. +#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  143. +#define VLAN_HDR 4
  144. +#else
  145. +#define VLAN_HDR 0
  146. +#endif
  147. +
  148. +#define AR2313_BUFSIZE (AR2313_MTU + VLAN_HDR + ETH_HLEN + CRC_LEN + \
  149. + RX_OFFSET)
  150. +
  151. +#ifdef MODULE
  152. +MODULE_LICENSE("GPL");
  153. +MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@nbd.name>");
  154. +MODULE_DESCRIPTION("AR231x Ethernet driver");
  155. +#endif
  156. +
  157. +#define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
  158. +
  159. +/* prototypes */
  160. +static void ar231x_halt(struct net_device *dev);
  161. +static void rx_tasklet_func(unsigned long data);
  162. +static void rx_tasklet_cleanup(struct net_device *dev);
  163. +static void ar231x_multicast_list(struct net_device *dev);
  164. +static void ar231x_tx_timeout(struct net_device *dev);
  165. +
  166. +static int ar231x_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum);
  167. +static int ar231x_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum,
  168. + u16 value);
  169. +static int ar231x_mdiobus_reset(struct mii_bus *bus);
  170. +static int ar231x_mdiobus_probe(struct net_device *dev);
  171. +static void ar231x_adjust_link(struct net_device *dev);
  172. +
  173. +#ifndef ERR
  174. +#define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
  175. +#endif
  176. +
  177. +#ifdef CONFIG_NET_POLL_CONTROLLER
  178. +static void
  179. +ar231x_netpoll(struct net_device *dev)
  180. +{
  181. + unsigned long flags;
  182. +
  183. + local_irq_save(flags);
  184. + ar231x_interrupt(dev->irq, dev);
  185. + local_irq_restore(flags);
  186. +}
  187. +#endif
  188. +
  189. +static const struct net_device_ops ar231x_ops = {
  190. + .ndo_open = ar231x_open,
  191. + .ndo_stop = ar231x_close,
  192. + .ndo_start_xmit = ar231x_start_xmit,
  193. + .ndo_set_rx_mode = ar231x_multicast_list,
  194. + .ndo_do_ioctl = ar231x_ioctl,
  195. + .ndo_change_mtu = eth_change_mtu,
  196. + .ndo_validate_addr = eth_validate_addr,
  197. + .ndo_set_mac_address = eth_mac_addr,
  198. + .ndo_tx_timeout = ar231x_tx_timeout,
  199. +#ifdef CONFIG_NET_POLL_CONTROLLER
  200. + .ndo_poll_controller = ar231x_netpoll,
  201. +#endif
  202. +};
  203. +
  204. +static int ar231x_probe(struct platform_device *pdev)
  205. +{
  206. + struct net_device *dev;
  207. + struct ar231x_private *sp;
  208. + struct resource *res;
  209. + unsigned long ar_eth_base;
  210. + char buf[64];
  211. +
  212. + dev = alloc_etherdev(sizeof(struct ar231x_private));
  213. +
  214. + if (dev == NULL) {
  215. + printk(KERN_ERR
  216. + "ar231x: Unable to allocate net_device structure!\n");
  217. + return -ENOMEM;
  218. + }
  219. +
  220. + platform_set_drvdata(pdev, dev);
  221. +
  222. + sp = netdev_priv(dev);
  223. + sp->dev = dev;
  224. + sp->cfg = pdev->dev.platform_data;
  225. +
  226. + sprintf(buf, "eth%d_membase", pdev->id);
  227. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, buf);
  228. + if (!res)
  229. + return -ENODEV;
  230. +
  231. + sp->link = 0;
  232. + ar_eth_base = res->start;
  233. +
  234. + sprintf(buf, "eth%d_irq", pdev->id);
  235. + dev->irq = platform_get_irq_byname(pdev, buf);
  236. +
  237. + spin_lock_init(&sp->lock);
  238. +
  239. + dev->features |= NETIF_F_HIGHDMA;
  240. + dev->netdev_ops = &ar231x_ops;
  241. +
  242. + tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long)dev);
  243. + tasklet_disable(&sp->rx_tasklet);
  244. +
  245. + sp->eth_regs = ioremap_nocache(ar_eth_base, sizeof(*sp->eth_regs));
  246. + if (!sp->eth_regs) {
  247. + printk("Can't remap eth registers\n");
  248. + return -ENXIO;
  249. + }
  250. +
  251. + /**
  252. + * When there's only one MAC, PHY regs are typically on ENET0,
  253. + * even though the MAC might be on ENET1.
  254. + * So remap PHY regs separately.
  255. + */
  256. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eth0_mii");
  257. + if (!res) {
  258. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  259. + "eth1_mii");
  260. + if (!res)
  261. + return -ENODEV;
  262. + }
  263. + sp->phy_regs = ioremap_nocache(res->start, resource_size(res));
  264. + if (!sp->phy_regs) {
  265. + printk("Can't remap phy registers\n");
  266. + return -ENXIO;
  267. + }
  268. +
  269. + sp->dma_regs = ioremap_nocache(ar_eth_base + 0x1000,
  270. + sizeof(*sp->dma_regs));
  271. + if (!sp->dma_regs) {
  272. + printk("Can't remap DMA registers\n");
  273. + return -ENXIO;
  274. + }
  275. + dev->base_addr = ar_eth_base + 0x1000;
  276. +
  277. + strncpy(sp->name, "Atheros AR231x", sizeof(sp->name) - 1);
  278. + sp->name[sizeof(sp->name) - 1] = '\0';
  279. + memcpy(dev->dev_addr, sp->cfg->macaddr, 6);
  280. +
  281. + if (ar231x_init(dev)) {
  282. + /* ar231x_init() calls ar231x_init_cleanup() on error */
  283. + kfree(dev);
  284. + return -ENODEV;
  285. + }
  286. +
  287. + if (register_netdev(dev)) {
  288. + printk("%s: register_netdev failed\n", __func__);
  289. + return -1;
  290. + }
  291. +
  292. + printk("%s: %s: %pM, irq %d\n", dev->name, sp->name, dev->dev_addr,
  293. + dev->irq);
  294. +
  295. + sp->mii_bus = mdiobus_alloc();
  296. + if (sp->mii_bus == NULL)
  297. + return -1;
  298. +
  299. + sp->mii_bus->priv = dev;
  300. + sp->mii_bus->read = ar231x_mdiobus_read;
  301. + sp->mii_bus->write = ar231x_mdiobus_write;
  302. + sp->mii_bus->reset = ar231x_mdiobus_reset;
  303. + sp->mii_bus->name = "ar231x_eth_mii";
  304. + snprintf(sp->mii_bus->id, MII_BUS_ID_SIZE, "%d", pdev->id);
  305. + sp->mii_bus->irq = kmalloc(sizeof(int), GFP_KERNEL);
  306. + *sp->mii_bus->irq = PHY_POLL;
  307. +
  308. + mdiobus_register(sp->mii_bus);
  309. +
  310. + if (ar231x_mdiobus_probe(dev) != 0) {
  311. + printk(KERN_ERR "%s: mdiobus_probe failed\n", dev->name);
  312. + rx_tasklet_cleanup(dev);
  313. + ar231x_init_cleanup(dev);
  314. + unregister_netdev(dev);
  315. + kfree(dev);
  316. + return -ENODEV;
  317. + }
  318. +
  319. + /* start link poll timer */
  320. + ar231x_setup_timer(dev);
  321. +
  322. + return 0;
  323. +}
  324. +
  325. +static void ar231x_multicast_list(struct net_device *dev)
  326. +{
  327. + struct ar231x_private *sp = netdev_priv(dev);
  328. + unsigned int filter;
  329. +
  330. + filter = sp->eth_regs->mac_control;
  331. +
  332. + if (dev->flags & IFF_PROMISC)
  333. + filter |= MAC_CONTROL_PR;
  334. + else
  335. + filter &= ~MAC_CONTROL_PR;
  336. + if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 0))
  337. + filter |= MAC_CONTROL_PM;
  338. + else
  339. + filter &= ~MAC_CONTROL_PM;
  340. +
  341. + sp->eth_regs->mac_control = filter;
  342. +}
  343. +
  344. +static void rx_tasklet_cleanup(struct net_device *dev)
  345. +{
  346. + struct ar231x_private *sp = netdev_priv(dev);
  347. +
  348. + /**
  349. + * Tasklet may be scheduled. Need to get it removed from the list
  350. + * since we're about to free the struct.
  351. + */
  352. +
  353. + sp->unloading = 1;
  354. + tasklet_enable(&sp->rx_tasklet);
  355. + tasklet_kill(&sp->rx_tasklet);
  356. +}
  357. +
  358. +static int ar231x_remove(struct platform_device *pdev)
  359. +{
  360. + struct net_device *dev = platform_get_drvdata(pdev);
  361. + struct ar231x_private *sp = netdev_priv(dev);
  362. +
  363. + rx_tasklet_cleanup(dev);
  364. + ar231x_init_cleanup(dev);
  365. + unregister_netdev(dev);
  366. + mdiobus_unregister(sp->mii_bus);
  367. + mdiobus_free(sp->mii_bus);
  368. + kfree(dev);
  369. + return 0;
  370. +}
  371. +
  372. +/**
  373. + * Restart the AR2313 ethernet controller.
  374. + */
  375. +static int ar231x_restart(struct net_device *dev)
  376. +{
  377. + /* disable interrupts */
  378. + disable_irq(dev->irq);
  379. +
  380. + /* stop mac */
  381. + ar231x_halt(dev);
  382. +
  383. + /* initialize */
  384. + ar231x_init(dev);
  385. +
  386. + /* enable interrupts */
  387. + enable_irq(dev->irq);
  388. +
  389. + return 0;
  390. +}
  391. +
  392. +static struct platform_driver ar231x_driver = {
  393. + .driver.name = "ar231x-eth",
  394. + .probe = ar231x_probe,
  395. + .remove = ar231x_remove,
  396. +};
  397. +
  398. +module_platform_driver(ar231x_driver);
  399. +
  400. +static void ar231x_free_descriptors(struct net_device *dev)
  401. +{
  402. + struct ar231x_private *sp = netdev_priv(dev);
  403. +
  404. + if (sp->rx_ring != NULL) {
  405. + kfree((void *)KSEG0ADDR(sp->rx_ring));
  406. + sp->rx_ring = NULL;
  407. + sp->tx_ring = NULL;
  408. + }
  409. +}
  410. +
  411. +static int ar231x_allocate_descriptors(struct net_device *dev)
  412. +{
  413. + struct ar231x_private *sp = netdev_priv(dev);
  414. + int size;
  415. + int j;
  416. + ar231x_descr_t *space;
  417. +
  418. + if (sp->rx_ring != NULL) {
  419. + printk("%s: already done.\n", __func__);
  420. + return 0;
  421. + }
  422. +
  423. + size = sizeof(ar231x_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES);
  424. + space = kmalloc(size, GFP_KERNEL);
  425. + if (space == NULL)
  426. + return 1;
  427. +
  428. + /* invalidate caches */
  429. + dma_cache_inv((unsigned int)space, size);
  430. +
  431. + /* now convert pointer to KSEG1 */
  432. + space = (ar231x_descr_t *)KSEG1ADDR(space);
  433. +
  434. + memset((void *)space, 0, size);
  435. +
  436. + sp->rx_ring = space;
  437. + space += AR2313_DESCR_ENTRIES;
  438. +
  439. + sp->tx_ring = space;
  440. + space += AR2313_DESCR_ENTRIES;
  441. +
  442. + /* Initialize the transmit Descriptors */
  443. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  444. + ar231x_descr_t *td = &sp->tx_ring[j];
  445. +
  446. + td->status = 0;
  447. + td->devcs = DMA_TX1_CHAINED;
  448. + td->addr = 0;
  449. + td->descr = virt_to_phys(&sp->tx_ring[DSC_NEXT(j)]);
  450. + }
  451. +
  452. + return 0;
  453. +}
  454. +
  455. +/**
  456. + * Generic cleanup handling data allocated during init. Used when the
  457. + * module is unloaded or if an error occurs during initialization
  458. + */
  459. +static void ar231x_init_cleanup(struct net_device *dev)
  460. +{
  461. + struct ar231x_private *sp = netdev_priv(dev);
  462. + struct sk_buff *skb;
  463. + int j;
  464. +
  465. + ar231x_free_descriptors(dev);
  466. +
  467. + if (sp->eth_regs)
  468. + iounmap((void *)sp->eth_regs);
  469. + if (sp->dma_regs)
  470. + iounmap((void *)sp->dma_regs);
  471. + if (sp->phy_regs)
  472. + iounmap((void *)sp->phy_regs);
  473. +
  474. + if (sp->rx_skb) {
  475. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  476. + skb = sp->rx_skb[j];
  477. + if (skb) {
  478. + sp->rx_skb[j] = NULL;
  479. + dev_kfree_skb(skb);
  480. + }
  481. + }
  482. + kfree(sp->rx_skb);
  483. + sp->rx_skb = NULL;
  484. + }
  485. +
  486. + if (sp->tx_skb) {
  487. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  488. + skb = sp->tx_skb[j];
  489. + if (skb) {
  490. + sp->tx_skb[j] = NULL;
  491. + dev_kfree_skb(skb);
  492. + }
  493. + }
  494. + kfree(sp->tx_skb);
  495. + sp->tx_skb = NULL;
  496. + }
  497. +}
  498. +
  499. +static int ar231x_setup_timer(struct net_device *dev)
  500. +{
  501. + struct ar231x_private *sp = netdev_priv(dev);
  502. +
  503. + init_timer(&sp->link_timer);
  504. +
  505. + sp->link_timer.function = ar231x_link_timer_fn;
  506. + sp->link_timer.data = (int)dev;
  507. + sp->link_timer.expires = jiffies + HZ;
  508. +
  509. + add_timer(&sp->link_timer);
  510. + return 0;
  511. +}
  512. +
  513. +static void ar231x_link_timer_fn(unsigned long data)
  514. +{
  515. + struct net_device *dev = (struct net_device *)data;
  516. + struct ar231x_private *sp = netdev_priv(dev);
  517. +
  518. + /**
  519. + * See if the link status changed.
  520. + * This was needed to make sure we set the PHY to the
  521. + * autonegotiated value of half or full duplex.
  522. + */
  523. + ar231x_check_link(dev);
  524. +
  525. + /**
  526. + * Loop faster when we don't have link.
  527. + * This was needed to speed up the AP bootstrap time.
  528. + */
  529. + if (sp->link == 0)
  530. + mod_timer(&sp->link_timer, jiffies + HZ / 2);
  531. + else
  532. + mod_timer(&sp->link_timer, jiffies + LINK_TIMER);
  533. +}
  534. +
  535. +static void ar231x_check_link(struct net_device *dev)
  536. +{
  537. + struct ar231x_private *sp = netdev_priv(dev);
  538. + u16 phy_data;
  539. +
  540. + phy_data = ar231x_mdiobus_read(sp->mii_bus, sp->phy, MII_BMSR);
  541. + if (sp->phy_data != phy_data) {
  542. + if (phy_data & BMSR_LSTATUS) {
  543. + /**
  544. + * Link is present, ready link partner ability to
  545. + * deterine duplexity.
  546. + */
  547. + int duplex = 0;
  548. + u16 reg;
  549. +
  550. + sp->link = 1;
  551. + reg = ar231x_mdiobus_read(sp->mii_bus, sp->phy,
  552. + MII_BMCR);
  553. + if (reg & BMCR_ANENABLE) {
  554. + /* auto neg enabled */
  555. + reg = ar231x_mdiobus_read(sp->mii_bus, sp->phy,
  556. + MII_LPA);
  557. + duplex = reg & (LPA_100FULL | LPA_10FULL) ?
  558. + 1 : 0;
  559. + } else {
  560. + /* no auto neg, just read duplex config */
  561. + duplex = (reg & BMCR_FULLDPLX) ? 1 : 0;
  562. + }
  563. +
  564. + printk(KERN_INFO "%s: Configuring MAC for %s duplex\n",
  565. + dev->name, (duplex) ? "full" : "half");
  566. +
  567. + if (duplex) {
  568. + /* full duplex */
  569. + sp->eth_regs->mac_control =
  570. + (sp->eth_regs->mac_control |
  571. + MAC_CONTROL_F) & ~MAC_CONTROL_DRO;
  572. + } else {
  573. + /* half duplex */
  574. + sp->eth_regs->mac_control =
  575. + (sp->eth_regs->mac_control |
  576. + MAC_CONTROL_DRO) & ~MAC_CONTROL_F;
  577. + }
  578. + } else {
  579. + /* no link */
  580. + sp->link = 0;
  581. + }
  582. + sp->phy_data = phy_data;
  583. + }
  584. +}
  585. +
  586. +static int ar231x_reset_reg(struct net_device *dev)
  587. +{
  588. + struct ar231x_private *sp = netdev_priv(dev);
  589. + unsigned int ethsal, ethsah;
  590. + unsigned int flags;
  591. +
  592. + sp->cfg->reset_set(sp->cfg->reset_mac);
  593. + mdelay(10);
  594. + sp->cfg->reset_clear(sp->cfg->reset_mac);
  595. + mdelay(10);
  596. + sp->cfg->reset_set(sp->cfg->reset_phy);
  597. + mdelay(10);
  598. + sp->cfg->reset_clear(sp->cfg->reset_phy);
  599. + mdelay(10);
  600. +
  601. + sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
  602. + mdelay(10);
  603. + sp->dma_regs->bus_mode =
  604. + ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
  605. +
  606. + /* enable interrupts */
  607. + sp->dma_regs->intr_ena = DMA_STATUS_AIS | DMA_STATUS_NIS |
  608. + DMA_STATUS_RI | DMA_STATUS_TI |
  609. + DMA_STATUS_FBE;
  610. + sp->dma_regs->xmt_base = virt_to_phys(sp->tx_ring);
  611. + sp->dma_regs->rcv_base = virt_to_phys(sp->rx_ring);
  612. + sp->dma_regs->control =
  613. + (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
  614. +
  615. + sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
  616. + sp->eth_regs->vlan_tag = (0x8100);
  617. +
  618. + /* Enable Ethernet Interface */
  619. + flags = (MAC_CONTROL_TE | /* transmit enable */
  620. + MAC_CONTROL_PM | /* pass mcast */
  621. + MAC_CONTROL_F | /* full duplex */
  622. + MAC_CONTROL_HBD); /* heart beat disabled */
  623. +
  624. + if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
  625. + flags |= MAC_CONTROL_PR;
  626. + }
  627. + sp->eth_regs->mac_control = flags;
  628. +
  629. + /* Set all Ethernet station address registers to their initial values */
  630. + ethsah = (((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
  631. + (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF);
  632. +
  633. + ethsal = (((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
  634. + (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
  635. + (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
  636. + (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF);
  637. +
  638. + sp->eth_regs->mac_addr[0] = ethsah;
  639. + sp->eth_regs->mac_addr[1] = ethsal;
  640. +
  641. + mdelay(10);
  642. +
  643. + return 0;
  644. +}
  645. +
  646. +static int ar231x_init(struct net_device *dev)
  647. +{
  648. + struct ar231x_private *sp = netdev_priv(dev);
  649. + int ecode = 0;
  650. +
  651. + /* Allocate descriptors */
  652. + if (ar231x_allocate_descriptors(dev)) {
  653. + printk("%s: %s: ar231x_allocate_descriptors failed\n",
  654. + dev->name, __func__);
  655. + ecode = -EAGAIN;
  656. + goto init_error;
  657. + }
  658. +
  659. + /* Get the memory for the skb rings */
  660. + if (sp->rx_skb == NULL) {
  661. + sp->rx_skb =
  662. + kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
  663. + GFP_KERNEL);
  664. + if (!(sp->rx_skb)) {
  665. + printk("%s: %s: rx_skb kmalloc failed\n",
  666. + dev->name, __func__);
  667. + ecode = -EAGAIN;
  668. + goto init_error;
  669. + }
  670. + }
  671. + memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
  672. +
  673. + if (sp->tx_skb == NULL) {
  674. + sp->tx_skb =
  675. + kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
  676. + GFP_KERNEL);
  677. + if (!(sp->tx_skb)) {
  678. + printk("%s: %s: tx_skb kmalloc failed\n",
  679. + dev->name, __func__);
  680. + ecode = -EAGAIN;
  681. + goto init_error;
  682. + }
  683. + }
  684. + memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
  685. +
  686. + /**
  687. + * Set tx_csm before we start receiving interrupts, otherwise
  688. + * the interrupt handler might think it is supposed to process
  689. + * tx ints before we are up and running, which may cause a null
  690. + * pointer access in the int handler.
  691. + */
  692. + sp->rx_skbprd = 0;
  693. + sp->cur_rx = 0;
  694. + sp->tx_prd = 0;
  695. + sp->tx_csm = 0;
  696. +
  697. + /* Zero the stats before starting the interface */
  698. + memset(&dev->stats, 0, sizeof(dev->stats));
  699. +
  700. + /**
  701. + * We load the ring here as there seem to be no way to tell the
  702. + * firmware to wipe the ring without re-initializing it.
  703. + */
  704. + ar231x_load_rx_ring(dev, RX_RING_SIZE);
  705. +
  706. + /* Init hardware */
  707. + ar231x_reset_reg(dev);
  708. +
  709. + /* Get the IRQ */
  710. + ecode = request_irq(dev->irq, &ar231x_interrupt, 0,
  711. + dev->name, dev);
  712. + if (ecode) {
  713. + printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
  714. + dev->name, __func__, dev->irq);
  715. + goto init_error;
  716. + }
  717. +
  718. + tasklet_enable(&sp->rx_tasklet);
  719. +
  720. + return 0;
  721. +
  722. +init_error:
  723. + ar231x_init_cleanup(dev);
  724. + return ecode;
  725. +}
  726. +
  727. +/**
  728. + * Load the rx ring.
  729. + *
  730. + * Loading rings is safe without holding the spin lock since this is
  731. + * done only before the device is enabled, thus no interrupts are
  732. + * generated and by the interrupt handler/tasklet handler.
  733. + */
  734. +static void ar231x_load_rx_ring(struct net_device *dev, int nr_bufs)
  735. +{
  736. + struct ar231x_private *sp = netdev_priv(dev);
  737. + short i, idx;
  738. +
  739. + idx = sp->rx_skbprd;
  740. +
  741. + for (i = 0; i < nr_bufs; i++) {
  742. + struct sk_buff *skb;
  743. + ar231x_descr_t *rd;
  744. +
  745. + if (sp->rx_skb[idx])
  746. + break;
  747. +
  748. + skb = netdev_alloc_skb_ip_align(dev, AR2313_BUFSIZE);
  749. + if (!skb) {
  750. + printk("\n\n\n\n %s: No memory in system\n\n\n\n",
  751. + __func__);
  752. + break;
  753. + }
  754. +
  755. + /* Make sure IP header starts on a fresh cache line */
  756. + skb->dev = dev;
  757. + sp->rx_skb[idx] = skb;
  758. +
  759. + rd = (ar231x_descr_t *)&sp->rx_ring[idx];
  760. +
  761. + /* initialize dma descriptor */
  762. + rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
  763. + DMA_RX1_CHAINED);
  764. + rd->addr = virt_to_phys(skb->data);
  765. + rd->descr = virt_to_phys(&sp->rx_ring[DSC_NEXT(idx)]);
  766. + rd->status = DMA_RX_OWN;
  767. +
  768. + idx = DSC_NEXT(idx);
  769. + }
  770. +
  771. + if (i)
  772. + sp->rx_skbprd = idx;
  773. +}
  774. +
  775. +#define AR2313_MAX_PKTS_PER_CALL 64
  776. +
  777. +static int ar231x_rx_int(struct net_device *dev)
  778. +{
  779. + struct ar231x_private *sp = netdev_priv(dev);
  780. + struct sk_buff *skb, *skb_new;
  781. + ar231x_descr_t *rxdesc;
  782. + unsigned int status;
  783. + u32 idx;
  784. + int pkts = 0;
  785. + int rval;
  786. +
  787. + idx = sp->cur_rx;
  788. +
  789. + /* process at most the entire ring and then wait for another int */
  790. + while (1) {
  791. + rxdesc = &sp->rx_ring[idx];
  792. + status = rxdesc->status;
  793. +
  794. + if (status & DMA_RX_OWN) {
  795. + /* SiByte owns descriptor or descr not yet filled in */
  796. + rval = 0;
  797. + break;
  798. + }
  799. +
  800. + if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
  801. + rval = 1;
  802. + break;
  803. + }
  804. +
  805. + if ((status & DMA_RX_ERROR) && !(status & DMA_RX_LONG)) {
  806. + dev->stats.rx_errors++;
  807. + dev->stats.rx_dropped++;
  808. +
  809. + /* add statistics counters */
  810. + if (status & DMA_RX_ERR_CRC)
  811. + dev->stats.rx_crc_errors++;
  812. + if (status & DMA_RX_ERR_COL)
  813. + dev->stats.rx_over_errors++;
  814. + if (status & DMA_RX_ERR_LENGTH)
  815. + dev->stats.rx_length_errors++;
  816. + if (status & DMA_RX_ERR_RUNT)
  817. + dev->stats.rx_over_errors++;
  818. + if (status & DMA_RX_ERR_DESC)
  819. + dev->stats.rx_over_errors++;
  820. +
  821. + } else {
  822. + /* alloc new buffer. */
  823. + skb_new = netdev_alloc_skb_ip_align(dev,
  824. + AR2313_BUFSIZE);
  825. + if (skb_new != NULL) {
  826. + skb = sp->rx_skb[idx];
  827. + /* set skb */
  828. + skb_put(skb, ((status >> DMA_RX_LEN_SHIFT) &
  829. + 0x3fff) - CRC_LEN);
  830. +
  831. + dev->stats.rx_bytes += skb->len;
  832. + skb->protocol = eth_type_trans(skb, dev);
  833. + /* pass the packet to upper layers */
  834. + netif_rx(skb);
  835. +
  836. + skb_new->dev = dev;
  837. + /* reset descriptor's curr_addr */
  838. + rxdesc->addr = virt_to_phys(skb_new->data);
  839. +
  840. + dev->stats.rx_packets++;
  841. + sp->rx_skb[idx] = skb_new;
  842. + } else {
  843. + dev->stats.rx_dropped++;
  844. + }
  845. + }
  846. +
  847. + rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
  848. + DMA_RX1_CHAINED);
  849. + rxdesc->status = DMA_RX_OWN;
  850. +
  851. + idx = DSC_NEXT(idx);
  852. + }
  853. +
  854. + sp->cur_rx = idx;
  855. +
  856. + return rval;
  857. +}
  858. +
  859. +static void ar231x_tx_int(struct net_device *dev)
  860. +{
  861. + struct ar231x_private *sp = netdev_priv(dev);
  862. + u32 idx;
  863. + struct sk_buff *skb;
  864. + ar231x_descr_t *txdesc;
  865. + unsigned int status = 0;
  866. +
  867. + idx = sp->tx_csm;
  868. +
  869. + while (idx != sp->tx_prd) {
  870. + txdesc = &sp->tx_ring[idx];
  871. + status = txdesc->status;
  872. +
  873. + if (status & DMA_TX_OWN) {
  874. + /* ar231x dma still owns descr */
  875. + break;
  876. + }
  877. + /* done with this descriptor */
  878. + dma_unmap_single(NULL, txdesc->addr,
  879. + txdesc->devcs & DMA_TX1_BSIZE_MASK,
  880. + DMA_TO_DEVICE);
  881. + txdesc->status = 0;
  882. +
  883. + if (status & DMA_TX_ERROR) {
  884. + dev->stats.tx_errors++;
  885. + dev->stats.tx_dropped++;
  886. + if (status & DMA_TX_ERR_UNDER)
  887. + dev->stats.tx_fifo_errors++;
  888. + if (status & DMA_TX_ERR_HB)
  889. + dev->stats.tx_heartbeat_errors++;
  890. + if (status & (DMA_TX_ERR_LOSS | DMA_TX_ERR_LINK))
  891. + dev->stats.tx_carrier_errors++;
  892. + if (status & (DMA_TX_ERR_LATE | DMA_TX_ERR_COL |
  893. + DMA_TX_ERR_JABBER | DMA_TX_ERR_DEFER))
  894. + dev->stats.tx_aborted_errors++;
  895. + } else {
  896. + /* transmit OK */
  897. + dev->stats.tx_packets++;
  898. + }
  899. +
  900. + skb = sp->tx_skb[idx];
  901. + sp->tx_skb[idx] = NULL;
  902. + idx = DSC_NEXT(idx);
  903. + dev->stats.tx_bytes += skb->len;
  904. + dev_kfree_skb_irq(skb);
  905. + }
  906. +
  907. + sp->tx_csm = idx;
  908. +}
  909. +
  910. +static void rx_tasklet_func(unsigned long data)
  911. +{
  912. + struct net_device *dev = (struct net_device *)data;
  913. + struct ar231x_private *sp = netdev_priv(dev);
  914. +
  915. + if (sp->unloading)
  916. + return;
  917. +
  918. + if (ar231x_rx_int(dev)) {
  919. + tasklet_hi_schedule(&sp->rx_tasklet);
  920. + } else {
  921. + unsigned long flags;
  922. +
  923. + spin_lock_irqsave(&sp->lock, flags);
  924. + sp->dma_regs->intr_ena |= DMA_STATUS_RI;
  925. + spin_unlock_irqrestore(&sp->lock, flags);
  926. + }
  927. +}
  928. +
  929. +static void rx_schedule(struct net_device *dev)
  930. +{
  931. + struct ar231x_private *sp = netdev_priv(dev);
  932. +
  933. + sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
  934. +
  935. + tasklet_hi_schedule(&sp->rx_tasklet);
  936. +}
  937. +
  938. +static irqreturn_t ar231x_interrupt(int irq, void *dev_id)
  939. +{
  940. + struct net_device *dev = (struct net_device *)dev_id;
  941. + struct ar231x_private *sp = netdev_priv(dev);
  942. + unsigned int status, enabled;
  943. +
  944. + /* clear interrupt */
  945. + /* Don't clear RI bit if currently disabled */
  946. + status = sp->dma_regs->status;
  947. + enabled = sp->dma_regs->intr_ena;
  948. + sp->dma_regs->status = status & enabled;
  949. +
  950. + if (status & DMA_STATUS_NIS) {
  951. + /* normal status */
  952. + /**
  953. + * Don't schedule rx processing if interrupt
  954. + * is already disabled.
  955. + */
  956. + if (status & enabled & DMA_STATUS_RI) {
  957. + /* receive interrupt */
  958. + rx_schedule(dev);
  959. + }
  960. + if (status & DMA_STATUS_TI) {
  961. + /* transmit interrupt */
  962. + ar231x_tx_int(dev);
  963. + }
  964. + }
  965. +
  966. + /* abnormal status */
  967. + if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS))
  968. + ar231x_restart(dev);
  969. +
  970. + return IRQ_HANDLED;
  971. +}
  972. +
  973. +static int ar231x_open(struct net_device *dev)
  974. +{
  975. + struct ar231x_private *sp = netdev_priv(dev);
  976. + unsigned int ethsal, ethsah;
  977. +
  978. + /* reset the hardware, in case the MAC address changed */
  979. + ethsah = (((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
  980. + (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF);
  981. +
  982. + ethsal = (((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
  983. + (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
  984. + (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
  985. + (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF);
  986. +
  987. + sp->eth_regs->mac_addr[0] = ethsah;
  988. + sp->eth_regs->mac_addr[1] = ethsal;
  989. +
  990. + mdelay(10);
  991. +
  992. + dev->mtu = 1500;
  993. + netif_start_queue(dev);
  994. +
  995. + sp->eth_regs->mac_control |= MAC_CONTROL_RE;
  996. +
  997. + return 0;
  998. +}
  999. +
  1000. +static void ar231x_tx_timeout(struct net_device *dev)
  1001. +{
  1002. + struct ar231x_private *sp = netdev_priv(dev);
  1003. + unsigned long flags;
  1004. +
  1005. + spin_lock_irqsave(&sp->lock, flags);
  1006. + ar231x_restart(dev);
  1007. + spin_unlock_irqrestore(&sp->lock, flags);
  1008. +}
  1009. +
  1010. +static void ar231x_halt(struct net_device *dev)
  1011. +{
  1012. + struct ar231x_private *sp = netdev_priv(dev);
  1013. + int j;
  1014. +
  1015. + tasklet_disable(&sp->rx_tasklet);
  1016. +
  1017. + /* kill the MAC */
  1018. + sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
  1019. + MAC_CONTROL_TE); /* disable Transmits */
  1020. + /* stop dma */
  1021. + sp->dma_regs->control = 0;
  1022. + sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
  1023. +
  1024. + /* place phy and MAC in reset */
  1025. + sp->cfg->reset_set(sp->cfg->reset_mac);
  1026. + sp->cfg->reset_set(sp->cfg->reset_phy);
  1027. +
  1028. + /* free buffers on tx ring */
  1029. + for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
  1030. + struct sk_buff *skb;
  1031. + ar231x_descr_t *txdesc;
  1032. +
  1033. + txdesc = &sp->tx_ring[j];
  1034. + txdesc->descr = 0;
  1035. +
  1036. + skb = sp->tx_skb[j];
  1037. + if (skb) {
  1038. + dev_kfree_skb(skb);
  1039. + sp->tx_skb[j] = NULL;
  1040. + }
  1041. + }
  1042. +}
  1043. +
  1044. +/**
  1045. + * close should do nothing. Here's why. It's called when
  1046. + * 'ifconfig bond0 down' is run. If it calls free_irq then
  1047. + * the irq is gone forever ! When bond0 is made 'up' again,
  1048. + * the ar231x_open () does not call request_irq (). Worse,
  1049. + * the call to ar231x_halt() generates a WDOG reset due to
  1050. + * the write to reset register and the box reboots.
  1051. + * Commenting this out is good since it allows the
  1052. + * system to resume when bond0 is made up again.
  1053. + */
  1054. +static int ar231x_close(struct net_device *dev)
  1055. +{
  1056. +#if 0
  1057. + /* Disable interrupts */
  1058. + disable_irq(dev->irq);
  1059. +
  1060. + /**
  1061. + * Without (or before) releasing irq and stopping hardware, this
  1062. + * is an absolute non-sense, by the way. It will be reset instantly
  1063. + * by the first irq.
  1064. + */
  1065. + netif_stop_queue(dev);
  1066. +
  1067. + /* stop the MAC and DMA engines */
  1068. + ar231x_halt(dev);
  1069. +
  1070. + /* release the interrupt */
  1071. + free_irq(dev->irq, dev);
  1072. +
  1073. +#endif
  1074. + return 0;
  1075. +}
  1076. +
  1077. +static int ar231x_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1078. +{
  1079. + struct ar231x_private *sp = netdev_priv(dev);
  1080. + ar231x_descr_t *td;
  1081. + u32 idx;
  1082. +
  1083. + idx = sp->tx_prd;
  1084. + td = &sp->tx_ring[idx];
  1085. +
  1086. + if (td->status & DMA_TX_OWN) {
  1087. + /* free skbuf and lie to the caller that we sent it out */
  1088. + dev->stats.tx_dropped++;
  1089. + dev_kfree_skb(skb);
  1090. +
  1091. + /* restart transmitter in case locked */
  1092. + sp->dma_regs->xmt_poll = 0;
  1093. + return 0;
  1094. + }
  1095. +
  1096. + /* Setup the transmit descriptor. */
  1097. + td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
  1098. + (DMA_TX1_LS | DMA_TX1_IC | DMA_TX1_CHAINED));
  1099. + td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
  1100. + td->status = DMA_TX_OWN;
  1101. +
  1102. + /* kick transmitter last */
  1103. + sp->dma_regs->xmt_poll = 0;
  1104. +
  1105. + sp->tx_skb[idx] = skb;
  1106. + idx = DSC_NEXT(idx);
  1107. + sp->tx_prd = idx;
  1108. +
  1109. + return 0;
  1110. +}
  1111. +
  1112. +static int ar231x_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1113. +{
  1114. + struct ar231x_private *sp = netdev_priv(dev);
  1115. +
  1116. + switch (cmd) {
  1117. + case SIOCGMIIPHY:
  1118. + case SIOCGMIIREG:
  1119. + case SIOCSMIIREG:
  1120. + return phy_mii_ioctl(sp->phy_dev, ifr, cmd);
  1121. +
  1122. + default:
  1123. + break;
  1124. + }
  1125. +
  1126. + return -EOPNOTSUPP;
  1127. +}
  1128. +
  1129. +static void ar231x_adjust_link(struct net_device *dev)
  1130. +{
  1131. + struct ar231x_private *sp = netdev_priv(dev);
  1132. + unsigned int mc;
  1133. +
  1134. + if (!sp->phy_dev->link)
  1135. + return;
  1136. +
  1137. + if (sp->phy_dev->duplex != sp->oldduplex) {
  1138. + mc = readl(&sp->eth_regs->mac_control);
  1139. + mc &= ~(MAC_CONTROL_F | MAC_CONTROL_DRO);
  1140. + if (sp->phy_dev->duplex)
  1141. + mc |= MAC_CONTROL_F;
  1142. + else
  1143. + mc |= MAC_CONTROL_DRO;
  1144. + writel(mc, &sp->eth_regs->mac_control);
  1145. + sp->oldduplex = sp->phy_dev->duplex;
  1146. + }
  1147. +}
  1148. +
  1149. +#define MII_ADDR(phy, reg) \
  1150. + ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
  1151. +
  1152. +static int
  1153. +ar231x_mdiobus_read(struct mii_bus *bus, int phy_addr, int regnum)
  1154. +{
  1155. + struct net_device *const dev = bus->priv;
  1156. + struct ar231x_private *sp = netdev_priv(dev);
  1157. + volatile MII *ethernet = sp->phy_regs;
  1158. +
  1159. + ethernet->mii_addr = MII_ADDR(phy_addr, regnum);
  1160. + while (ethernet->mii_addr & MII_ADDR_BUSY)
  1161. + ;
  1162. + return ethernet->mii_data >> MII_DATA_SHIFT;
  1163. +}
  1164. +
  1165. +static int
  1166. +ar231x_mdiobus_write(struct mii_bus *bus, int phy_addr, int regnum, u16 value)
  1167. +{
  1168. + struct net_device *const dev = bus->priv;
  1169. + struct ar231x_private *sp = netdev_priv(dev);
  1170. + volatile MII *ethernet = sp->phy_regs;
  1171. +
  1172. + while (ethernet->mii_addr & MII_ADDR_BUSY)
  1173. + ;
  1174. + ethernet->mii_data = value << MII_DATA_SHIFT;
  1175. + ethernet->mii_addr = MII_ADDR(phy_addr, regnum) | MII_ADDR_WRITE;
  1176. +
  1177. + return 0;
  1178. +}
  1179. +
  1180. +static int ar231x_mdiobus_reset(struct mii_bus *bus)
  1181. +{
  1182. + struct net_device *const dev = bus->priv;
  1183. +
  1184. + ar231x_reset_reg(dev);
  1185. +
  1186. + return 0;
  1187. +}
  1188. +
  1189. +static int ar231x_mdiobus_probe(struct net_device *dev)
  1190. +{
  1191. + struct ar231x_private *const sp = netdev_priv(dev);
  1192. + struct phy_device *phydev = NULL;
  1193. + int phy_addr;
  1194. +
  1195. + /* find the first (lowest address) PHY on the current MAC's MII bus */
  1196. + for (phy_addr = 0; phy_addr < PHY_MAX_ADDR; phy_addr++)
  1197. + if (sp->mii_bus->phy_map[phy_addr]) {
  1198. + phydev = sp->mii_bus->phy_map[phy_addr];
  1199. + sp->phy = phy_addr;
  1200. + break; /* break out with first one found */
  1201. + }
  1202. +
  1203. + if (!phydev) {
  1204. + printk(KERN_ERR "ar231x: %s: no PHY found\n", dev->name);
  1205. + return -1;
  1206. + }
  1207. +
  1208. + /* now we are supposed to have a proper phydev, to attach to... */
  1209. + BUG_ON(!phydev);
  1210. + BUG_ON(phydev->attached_dev);
  1211. +
  1212. + phydev = phy_connect(dev, dev_name(&phydev->dev), &ar231x_adjust_link,
  1213. + PHY_INTERFACE_MODE_MII);
  1214. +
  1215. + if (IS_ERR(phydev)) {
  1216. + printk(KERN_ERR "%s: Could not attach to PHY\n", dev->name);
  1217. + return PTR_ERR(phydev);
  1218. + }
  1219. +
  1220. + /* mask with MAC supported features */
  1221. + phydev->supported &= (SUPPORTED_10baseT_Half
  1222. + | SUPPORTED_10baseT_Full
  1223. + | SUPPORTED_100baseT_Half
  1224. + | SUPPORTED_100baseT_Full
  1225. + | SUPPORTED_Autoneg
  1226. + /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
  1227. + | SUPPORTED_MII
  1228. + | SUPPORTED_TP);
  1229. +
  1230. + phydev->advertising = phydev->supported;
  1231. +
  1232. + sp->oldduplex = -1;
  1233. + sp->phy_dev = phydev;
  1234. +
  1235. + printk(KERN_INFO "%s: attached PHY driver [%s] (mii_bus:phy_addr=%s)\n",
  1236. + dev->name, phydev->drv->name, dev_name(&phydev->dev));
  1237. +
  1238. + return 0;
  1239. +}
  1240. +
  1241. --- /dev/null
  1242. +++ b/drivers/net/ethernet/atheros/ar231x/ar231x.h
  1243. @@ -0,0 +1,288 @@
  1244. +/*
  1245. + * ar231x.h: Linux driver for the Atheros AR231x Ethernet device.
  1246. + *
  1247. + * Copyright (C) 2004 by Sameer Dekate <sdekate@arubanetworks.com>
  1248. + * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
  1249. + * Copyright (C) 2006-2009 Felix Fietkau <nbd@nbd.name>
  1250. + *
  1251. + * Thanks to Atheros for providing hardware and documentation
  1252. + * enabling me to write this driver.
  1253. + *
  1254. + * This program is free software; you can redistribute it and/or modify
  1255. + * it under the terms of the GNU General Public License as published by
  1256. + * the Free Software Foundation; either version 2 of the License, or
  1257. + * (at your option) any later version.
  1258. + */
  1259. +
  1260. +#ifndef _AR2313_H_
  1261. +#define _AR2313_H_
  1262. +
  1263. +#include <linux/interrupt.h>
  1264. +#include <generated/autoconf.h>
  1265. +#include <linux/bitops.h>
  1266. +#include <ath25_platform.h>
  1267. +
  1268. +/* probe link timer - 5 secs */
  1269. +#define LINK_TIMER (5*HZ)
  1270. +
  1271. +#define IS_DMA_TX_INT(X) (((X) & (DMA_STATUS_TI)) != 0)
  1272. +#define IS_DMA_RX_INT(X) (((X) & (DMA_STATUS_RI)) != 0)
  1273. +#define IS_DRIVER_OWNED(X) (((X) & (DMA_TX_OWN)) == 0)
  1274. +
  1275. +#define AR2313_TX_TIMEOUT (HZ/4)
  1276. +
  1277. +/* Rings */
  1278. +#define DSC_RING_ENTRIES_SIZE (AR2313_DESCR_ENTRIES * sizeof(struct desc))
  1279. +#define DSC_NEXT(idx) ((idx + 1) & (AR2313_DESCR_ENTRIES - 1))
  1280. +
  1281. +#define AR2313_MBGET 2
  1282. +#define AR2313_MBSET 3
  1283. +#define AR2313_PCI_RECONFIG 4
  1284. +#define AR2313_PCI_DUMP 5
  1285. +#define AR2313_TEST_PANIC 6
  1286. +#define AR2313_TEST_NULLPTR 7
  1287. +#define AR2313_READ_DATA 8
  1288. +#define AR2313_WRITE_DATA 9
  1289. +#define AR2313_GET_VERSION 10
  1290. +#define AR2313_TEST_HANG 11
  1291. +#define AR2313_SYNC 12
  1292. +
  1293. +#define DMA_RX_ERR_CRC BIT(1)
  1294. +#define DMA_RX_ERR_DRIB BIT(2)
  1295. +#define DMA_RX_ERR_MII BIT(3)
  1296. +#define DMA_RX_EV2 BIT(5)
  1297. +#define DMA_RX_ERR_COL BIT(6)
  1298. +#define DMA_RX_LONG BIT(7)
  1299. +#define DMA_RX_LS BIT(8) /* last descriptor */
  1300. +#define DMA_RX_FS BIT(9) /* first descriptor */
  1301. +#define DMA_RX_MF BIT(10) /* multicast frame */
  1302. +#define DMA_RX_ERR_RUNT BIT(11) /* runt frame */
  1303. +#define DMA_RX_ERR_LENGTH BIT(12) /* length error */
  1304. +#define DMA_RX_ERR_DESC BIT(14) /* descriptor error */
  1305. +#define DMA_RX_ERROR BIT(15) /* error summary */
  1306. +#define DMA_RX_LEN_MASK 0x3fff0000
  1307. +#define DMA_RX_LEN_SHIFT 16
  1308. +#define DMA_RX_FILT BIT(30)
  1309. +#define DMA_RX_OWN BIT(31) /* desc owned by DMA controller */
  1310. +
  1311. +#define DMA_RX1_BSIZE_MASK 0x000007ff
  1312. +#define DMA_RX1_BSIZE_SHIFT 0
  1313. +#define DMA_RX1_CHAINED BIT(24)
  1314. +#define DMA_RX1_RER BIT(25)
  1315. +
  1316. +#define DMA_TX_ERR_UNDER BIT(1) /* underflow error */
  1317. +#define DMA_TX_ERR_DEFER BIT(2) /* excessive deferral */
  1318. +#define DMA_TX_COL_MASK 0x78
  1319. +#define DMA_TX_COL_SHIFT 3
  1320. +#define DMA_TX_ERR_HB BIT(7) /* hearbeat failure */
  1321. +#define DMA_TX_ERR_COL BIT(8) /* excessive collisions */
  1322. +#define DMA_TX_ERR_LATE BIT(9) /* late collision */
  1323. +#define DMA_TX_ERR_LINK BIT(10) /* no carrier */
  1324. +#define DMA_TX_ERR_LOSS BIT(11) /* loss of carrier */
  1325. +#define DMA_TX_ERR_JABBER BIT(14) /* transmit jabber timeout */
  1326. +#define DMA_TX_ERROR BIT(15) /* frame aborted */
  1327. +#define DMA_TX_OWN BIT(31) /* descr owned by DMA controller */
  1328. +
  1329. +#define DMA_TX1_BSIZE_MASK 0x000007ff
  1330. +#define DMA_TX1_BSIZE_SHIFT 0
  1331. +#define DMA_TX1_CHAINED BIT(24) /* chained descriptors */
  1332. +#define DMA_TX1_TER BIT(25) /* transmit end of ring */
  1333. +#define DMA_TX1_FS BIT(29) /* first segment */
  1334. +#define DMA_TX1_LS BIT(30) /* last segment */
  1335. +#define DMA_TX1_IC BIT(31) /* interrupt on completion */
  1336. +
  1337. +#define RCVPKT_LENGTH(X) (X >> 16) /* Received pkt Length */
  1338. +
  1339. +#define MAC_CONTROL_RE BIT(2) /* receive enable */
  1340. +#define MAC_CONTROL_TE BIT(3) /* transmit enable */
  1341. +#define MAC_CONTROL_DC BIT(5) /* Deferral check */
  1342. +#define MAC_CONTROL_ASTP BIT(8) /* Auto pad strip */
  1343. +#define MAC_CONTROL_DRTY BIT(10) /* Disable retry */
  1344. +#define MAC_CONTROL_DBF BIT(11) /* Disable bcast frames */
  1345. +#define MAC_CONTROL_LCC BIT(12) /* late collision ctrl */
  1346. +#define MAC_CONTROL_HP BIT(13) /* Hash Perfect filtering */
  1347. +#define MAC_CONTROL_HASH BIT(14) /* Unicast hash filtering */
  1348. +#define MAC_CONTROL_HO BIT(15) /* Hash only filtering */
  1349. +#define MAC_CONTROL_PB BIT(16) /* Pass Bad frames */
  1350. +#define MAC_CONTROL_IF BIT(17) /* Inverse filtering */
  1351. +#define MAC_CONTROL_PR BIT(18) /* promis mode (valid frames only) */
  1352. +#define MAC_CONTROL_PM BIT(19) /* pass multicast */
  1353. +#define MAC_CONTROL_F BIT(20) /* full-duplex */
  1354. +#define MAC_CONTROL_DRO BIT(23) /* Disable Receive Own */
  1355. +#define MAC_CONTROL_HBD BIT(28) /* heart-beat disabled (MUST BE SET) */
  1356. +#define MAC_CONTROL_BLE BIT(30) /* big endian mode */
  1357. +#define MAC_CONTROL_RA BIT(31) /* rcv all (valid and invalid frames) */
  1358. +
  1359. +#define MII_ADDR_BUSY BIT(0)
  1360. +#define MII_ADDR_WRITE BIT(1)
  1361. +#define MII_ADDR_REG_SHIFT 6
  1362. +#define MII_ADDR_PHY_SHIFT 11
  1363. +#define MII_DATA_SHIFT 0
  1364. +
  1365. +#define FLOW_CONTROL_FCE BIT(1)
  1366. +
  1367. +#define DMA_BUS_MODE_SWR BIT(0) /* software reset */
  1368. +#define DMA_BUS_MODE_BLE BIT(7) /* big endian mode */
  1369. +#define DMA_BUS_MODE_PBL_SHIFT 8 /* programmable burst length 32 */
  1370. +#define DMA_BUS_MODE_DBO BIT(20) /* big-endian descriptors */
  1371. +
  1372. +#define DMA_STATUS_TI BIT(0) /* transmit interrupt */
  1373. +#define DMA_STATUS_TPS BIT(1) /* transmit process stopped */
  1374. +#define DMA_STATUS_TU BIT(2) /* transmit buffer unavailable */
  1375. +#define DMA_STATUS_TJT BIT(3) /* transmit buffer timeout */
  1376. +#define DMA_STATUS_UNF BIT(5) /* transmit underflow */
  1377. +#define DMA_STATUS_RI BIT(6) /* receive interrupt */
  1378. +#define DMA_STATUS_RU BIT(7) /* receive buffer unavailable */
  1379. +#define DMA_STATUS_RPS BIT(8) /* receive process stopped */
  1380. +#define DMA_STATUS_ETI BIT(10) /* early transmit interrupt */
  1381. +#define DMA_STATUS_FBE BIT(13) /* fatal bus interrupt */
  1382. +#define DMA_STATUS_ERI BIT(14) /* early receive interrupt */
  1383. +#define DMA_STATUS_AIS BIT(15) /* abnormal interrupt summary */
  1384. +#define DMA_STATUS_NIS BIT(16) /* normal interrupt summary */
  1385. +#define DMA_STATUS_RS_SHIFT 17 /* receive process state */
  1386. +#define DMA_STATUS_TS_SHIFT 20 /* transmit process state */
  1387. +#define DMA_STATUS_EB_SHIFT 23 /* error bits */
  1388. +
  1389. +#define DMA_CONTROL_SR BIT(1) /* start receive */
  1390. +#define DMA_CONTROL_ST BIT(13) /* start transmit */
  1391. +#define DMA_CONTROL_SF BIT(21) /* store and forward */
  1392. +
  1393. +typedef struct {
  1394. + volatile unsigned int status; /* OWN, Device control and status. */
  1395. + volatile unsigned int devcs; /* pkt Control bits + Length */
  1396. + volatile unsigned int addr; /* Current Address. */
  1397. + volatile unsigned int descr; /* Next descriptor in chain. */
  1398. +} ar231x_descr_t;
  1399. +
  1400. +/**
  1401. + * New Combo structure for Both Eth0 AND eth1
  1402. + *
  1403. + * Don't directly access MII related regs since phy chip could be actually
  1404. + * connected to another ethernet block.
  1405. + */
  1406. +typedef struct {
  1407. + volatile unsigned int mac_control; /* 0x00 */
  1408. + volatile unsigned int mac_addr[2]; /* 0x04 - 0x08 */
  1409. + volatile unsigned int mcast_table[2]; /* 0x0c - 0x10 */
  1410. + volatile unsigned int __mii_addr; /* 0x14 */
  1411. + volatile unsigned int __mii_data; /* 0x18 */
  1412. + volatile unsigned int flow_control; /* 0x1c */
  1413. + volatile unsigned int vlan_tag; /* 0x20 */
  1414. + volatile unsigned int pad[7]; /* 0x24 - 0x3c */
  1415. + volatile unsigned int ucast_table[8]; /* 0x40-0x5c */
  1416. +} ETHERNET_STRUCT;
  1417. +
  1418. +typedef struct {
  1419. + volatile unsigned int mii_addr;
  1420. + volatile unsigned int mii_data;
  1421. +} MII;
  1422. +
  1423. +/********************************************************************
  1424. + * Interrupt controller
  1425. + ********************************************************************/
  1426. +
  1427. +typedef struct {
  1428. + volatile unsigned int wdog_control; /* 0x08 */
  1429. + volatile unsigned int wdog_timer; /* 0x0c */
  1430. + volatile unsigned int misc_status; /* 0x10 */
  1431. + volatile unsigned int misc_mask; /* 0x14 */
  1432. + volatile unsigned int global_status; /* 0x18 */
  1433. + volatile unsigned int reserved; /* 0x1c */
  1434. + volatile unsigned int reset_control; /* 0x20 */
  1435. +} INTERRUPT;
  1436. +
  1437. +/********************************************************************
  1438. + * DMA controller
  1439. + ********************************************************************/
  1440. +typedef struct {
  1441. + volatile unsigned int bus_mode; /* 0x00 (CSR0) */
  1442. + volatile unsigned int xmt_poll; /* 0x04 (CSR1) */
  1443. + volatile unsigned int rcv_poll; /* 0x08 (CSR2) */
  1444. + volatile unsigned int rcv_base; /* 0x0c (CSR3) */
  1445. + volatile unsigned int xmt_base; /* 0x10 (CSR4) */
  1446. + volatile unsigned int status; /* 0x14 (CSR5) */
  1447. + volatile unsigned int control; /* 0x18 (CSR6) */
  1448. + volatile unsigned int intr_ena; /* 0x1c (CSR7) */
  1449. + volatile unsigned int rcv_missed; /* 0x20 (CSR8) */
  1450. + volatile unsigned int reserved[11]; /* 0x24-0x4c (CSR9-19) */
  1451. + volatile unsigned int cur_tx_buf_addr; /* 0x50 (CSR20) */
  1452. + volatile unsigned int cur_rx_buf_addr; /* 0x50 (CSR21) */
  1453. +} DMA;
  1454. +
  1455. +/**
  1456. + * Struct private for the Sibyte.
  1457. + *
  1458. + * Elements are grouped so variables used by the tx handling goes
  1459. + * together, and will go into the same cache lines etc. in order to
  1460. + * avoid cache line contention between the rx and tx handling on SMP.
  1461. + *
  1462. + * Frequently accessed variables are put at the beginning of the
  1463. + * struct to help the compiler generate better/shorter code.
  1464. + */
  1465. +struct ar231x_private {
  1466. + struct net_device *dev;
  1467. + int version;
  1468. + u32 mb[2];
  1469. +
  1470. + volatile MII *phy_regs;
  1471. + volatile ETHERNET_STRUCT *eth_regs;
  1472. + volatile DMA *dma_regs;
  1473. + struct ar231x_eth *cfg;
  1474. +
  1475. + spinlock_t lock; /* Serialise access to device */
  1476. +
  1477. + /* RX and TX descriptors, must be adjacent */
  1478. + ar231x_descr_t *rx_ring;
  1479. + ar231x_descr_t *tx_ring;
  1480. +
  1481. + struct sk_buff **rx_skb;
  1482. + struct sk_buff **tx_skb;
  1483. +
  1484. + /* RX elements */
  1485. + u32 rx_skbprd;
  1486. + u32 cur_rx;
  1487. +
  1488. + /* TX elements */
  1489. + u32 tx_prd;
  1490. + u32 tx_csm;
  1491. +
  1492. + /* Misc elements */
  1493. + char name[48];
  1494. + struct {
  1495. + u32 address;
  1496. + u32 length;
  1497. + char *mapping;
  1498. + } desc;
  1499. +
  1500. + struct timer_list link_timer;
  1501. + unsigned short phy; /* merlot phy = 1, samsung phy = 0x1f */
  1502. + unsigned short mac;
  1503. + unsigned short link; /* 0 - link down, 1 - link up */
  1504. + u16 phy_data;
  1505. +
  1506. + struct tasklet_struct rx_tasklet;
  1507. + int unloading;
  1508. +
  1509. + struct phy_device *phy_dev;
  1510. + struct mii_bus *mii_bus;
  1511. + int oldduplex;
  1512. +};
  1513. +
  1514. +/* Prototypes */
  1515. +static int ar231x_init(struct net_device *dev);
  1516. +#ifdef TX_TIMEOUT
  1517. +static void ar231x_tx_timeout(struct net_device *dev);
  1518. +#endif
  1519. +static int ar231x_restart(struct net_device *dev);
  1520. +static void ar231x_load_rx_ring(struct net_device *dev, int bufs);
  1521. +static irqreturn_t ar231x_interrupt(int irq, void *dev_id);
  1522. +static int ar231x_open(struct net_device *dev);
  1523. +static int ar231x_start_xmit(struct sk_buff *skb, struct net_device *dev);
  1524. +static int ar231x_close(struct net_device *dev);
  1525. +static int ar231x_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
  1526. +static void ar231x_init_cleanup(struct net_device *dev);
  1527. +static int ar231x_setup_timer(struct net_device *dev);
  1528. +static void ar231x_link_timer_fn(unsigned long data);
  1529. +static void ar231x_check_link(struct net_device *dev);
  1530. +
  1531. +#endif /* _AR2313_H_ */
  1532. --- a/arch/mips/ath25/ar2315_regs.h
  1533. +++ b/arch/mips/ath25/ar2315_regs.h
  1534. @@ -57,6 +57,9 @@
  1535. #define AR2315_PCI_EXT_BASE 0x80000000 /* PCI external */
  1536. #define AR2315_PCI_EXT_SIZE 0x40000000
  1537. +/* MII registers offset inside Ethernet MMR region */
  1538. +#define AR2315_ENET0_MII_BASE (AR2315_ENET0_BASE + 0x14)
  1539. +
  1540. /*
  1541. * Configuration registers
  1542. */
  1543. --- a/arch/mips/ath25/ar5312_regs.h
  1544. +++ b/arch/mips/ath25/ar5312_regs.h
  1545. @@ -64,6 +64,10 @@
  1546. #define AR5312_AR5312_REV7 0x0057 /* AR5312 WMAC (AP30-040) */
  1547. #define AR5312_AR2313_REV8 0x0058 /* AR2313 WMAC (AP43-030) */
  1548. +/* MII registers offset inside Ethernet MMR region */
  1549. +#define AR5312_ENET0_MII_BASE (AR5312_ENET0_BASE + 0x14)
  1550. +#define AR5312_ENET1_MII_BASE (AR5312_ENET1_BASE + 0x14)
  1551. +
  1552. /* Reset/Timer Block Address Map */
  1553. #define AR5312_TIMER 0x0000 /* countdown timer */
  1554. #define AR5312_RELOAD 0x0004 /* timer reload value */
  1555. --- a/arch/mips/ath25/ar2315.c
  1556. +++ b/arch/mips/ath25/ar2315.c
  1557. @@ -136,6 +136,8 @@ static void ar2315_irq_dispatch(void)
  1558. if (pending & CAUSEF_IP3)
  1559. do_IRQ(AR2315_IRQ_WLAN0);
  1560. + else if (pending & CAUSEF_IP4)
  1561. + do_IRQ(AR2315_IRQ_ENET0);
  1562. #ifdef CONFIG_PCI_AR2315
  1563. else if (pending & CAUSEF_IP5)
  1564. do_IRQ(AR2315_IRQ_LCBUS_PCI);
  1565. @@ -169,6 +171,29 @@ void __init ar2315_arch_init_irq(void)
  1566. ar2315_misc_irq_domain = domain;
  1567. }
  1568. +static void ar2315_device_reset_set(u32 mask)
  1569. +{
  1570. + u32 val;
  1571. +
  1572. + val = ar2315_rst_reg_read(AR2315_RESET);
  1573. + ar2315_rst_reg_write(AR2315_RESET, val | mask);
  1574. +}
  1575. +
  1576. +static void ar2315_device_reset_clear(u32 mask)
  1577. +{
  1578. + u32 val;
  1579. +
  1580. + val = ar2315_rst_reg_read(AR2315_RESET);
  1581. + ar2315_rst_reg_write(AR2315_RESET, val & ~mask);
  1582. +}
  1583. +
  1584. +static struct ar231x_eth ar2315_eth_data = {
  1585. + .reset_set = ar2315_device_reset_set,
  1586. + .reset_clear = ar2315_device_reset_clear,
  1587. + .reset_mac = AR2315_RESET_ENET0,
  1588. + .reset_phy = AR2315_RESET_EPHY0,
  1589. +};
  1590. +
  1591. static struct resource ar2315_gpio_res[] = {
  1592. {
  1593. .name = "ar2315-gpio",
  1594. @@ -205,6 +230,11 @@ void __init ar2315_init_devices(void)
  1595. ar2315_gpio_res[1].end = ar2315_gpio_res[1].start;
  1596. platform_device_register(&ar2315_gpio);
  1597. + ar2315_eth_data.macaddr = ath25_board.config->enet0_mac;
  1598. + ath25_add_ethernet(0, AR2315_ENET0_BASE, "eth0_mii",
  1599. + AR2315_ENET0_MII_BASE, AR2315_IRQ_ENET0,
  1600. + &ar2315_eth_data);
  1601. +
  1602. ath25_add_wmac(0, AR2315_WLAN0_BASE, AR2315_IRQ_WLAN0);
  1603. }
  1604. --- a/arch/mips/ath25/ar5312.c
  1605. +++ b/arch/mips/ath25/ar5312.c
  1606. @@ -132,6 +132,10 @@ static void ar5312_irq_dispatch(void)
  1607. if (pending & CAUSEF_IP2)
  1608. do_IRQ(AR5312_IRQ_WLAN0);
  1609. + else if (pending & CAUSEF_IP3)
  1610. + do_IRQ(AR5312_IRQ_ENET0);
  1611. + else if (pending & CAUSEF_IP4)
  1612. + do_IRQ(AR5312_IRQ_ENET1);
  1613. else if (pending & CAUSEF_IP5)
  1614. do_IRQ(AR5312_IRQ_WLAN1);
  1615. else if (pending & CAUSEF_IP6)
  1616. @@ -163,6 +167,36 @@ void __init ar5312_arch_init_irq(void)
  1617. ar5312_misc_irq_domain = domain;
  1618. }
  1619. +static void ar5312_device_reset_set(u32 mask)
  1620. +{
  1621. + u32 val;
  1622. +
  1623. + val = ar5312_rst_reg_read(AR5312_RESET);
  1624. + ar5312_rst_reg_write(AR5312_RESET, val | mask);
  1625. +}
  1626. +
  1627. +static void ar5312_device_reset_clear(u32 mask)
  1628. +{
  1629. + u32 val;
  1630. +
  1631. + val = ar5312_rst_reg_read(AR5312_RESET);
  1632. + ar5312_rst_reg_write(AR5312_RESET, val & ~mask);
  1633. +}
  1634. +
  1635. +static struct ar231x_eth ar5312_eth0_data = {
  1636. + .reset_set = ar5312_device_reset_set,
  1637. + .reset_clear = ar5312_device_reset_clear,
  1638. + .reset_mac = AR5312_RESET_ENET0,
  1639. + .reset_phy = AR5312_RESET_EPHY0,
  1640. +};
  1641. +
  1642. +static struct ar231x_eth ar5312_eth1_data = {
  1643. + .reset_set = ar5312_device_reset_set,
  1644. + .reset_clear = ar5312_device_reset_clear,
  1645. + .reset_mac = AR5312_RESET_ENET1,
  1646. + .reset_phy = AR5312_RESET_EPHY1,
  1647. +};
  1648. +
  1649. static struct physmap_flash_data ar5312_flash_data = {
  1650. .width = 2,
  1651. };
  1652. @@ -243,6 +277,7 @@ static void __init ar5312_flash_init(voi
  1653. void __init ar5312_init_devices(void)
  1654. {
  1655. struct ath25_boarddata *config;
  1656. + u8 *c;
  1657. ar5312_flash_init();
  1658. @@ -266,8 +301,30 @@ void __init ar5312_init_devices(void)
  1659. platform_device_register(&ar5312_gpio);
  1660. + /* Fix up MAC addresses if necessary */
  1661. + if (is_broadcast_ether_addr(config->enet0_mac))
  1662. + ether_addr_copy(config->enet0_mac, config->enet1_mac);
  1663. +
  1664. + /* If ENET0 and ENET1 have the same mac address,
  1665. + * increment the one from ENET1 */
  1666. + if (ether_addr_equal(config->enet0_mac, config->enet1_mac)) {
  1667. + c = config->enet1_mac + 5;
  1668. + while ((c >= config->enet1_mac) && !(++(*c)))
  1669. + c--;
  1670. + }
  1671. +
  1672. switch (ath25_soc) {
  1673. case ATH25_SOC_AR5312:
  1674. + ar5312_eth0_data.macaddr = config->enet0_mac;
  1675. + ath25_add_ethernet(0, AR5312_ENET0_BASE, "eth0_mii",
  1676. + AR5312_ENET0_MII_BASE, AR5312_IRQ_ENET0,
  1677. + &ar5312_eth0_data);
  1678. +
  1679. + ar5312_eth1_data.macaddr = config->enet1_mac;
  1680. + ath25_add_ethernet(1, AR5312_ENET1_BASE, "eth1_mii",
  1681. + AR5312_ENET1_MII_BASE, AR5312_IRQ_ENET1,
  1682. + &ar5312_eth1_data);
  1683. +
  1684. if (!ath25_board.radio)
  1685. return;
  1686. @@ -276,8 +333,18 @@ void __init ar5312_init_devices(void)
  1687. ath25_add_wmac(0, AR5312_WLAN0_BASE, AR5312_IRQ_WLAN0);
  1688. break;
  1689. + /*
  1690. + * AR2312/3 ethernet uses the PHY of ENET0, but the MAC
  1691. + * of ENET1. Atheros calls it 'twisted' for a reason :)
  1692. + */
  1693. case ATH25_SOC_AR2312:
  1694. case ATH25_SOC_AR2313:
  1695. + ar5312_eth1_data.reset_phy = ar5312_eth0_data.reset_phy;
  1696. + ar5312_eth1_data.macaddr = config->enet0_mac;
  1697. + ath25_add_ethernet(1, AR5312_ENET1_BASE, "eth0_mii",
  1698. + AR5312_ENET0_MII_BASE, AR5312_IRQ_ENET1,
  1699. + &ar5312_eth1_data);
  1700. +
  1701. if (!ath25_board.radio)
  1702. return;
  1703. break;
  1704. --- a/arch/mips/ath25/devices.h
  1705. +++ b/arch/mips/ath25/devices.h
  1706. @@ -32,6 +32,8 @@ extern struct ar231x_board_config ath25_
  1707. extern void (*ath25_irq_dispatch)(void);
  1708. int ath25_find_config(phys_addr_t offset, unsigned long size);
  1709. +int ath25_add_ethernet(int nr, u32 base, const char *mii_name, u32 mii_base,
  1710. + int irq, void *pdata);
  1711. void ath25_serial_setup(u32 mapbase, int irq, unsigned int uartclk);
  1712. int ath25_add_wmac(int nr, u32 base, int irq);
  1713. --- a/arch/mips/ath25/devices.c
  1714. +++ b/arch/mips/ath25/devices.c
  1715. @@ -12,6 +12,51 @@
  1716. struct ar231x_board_config ath25_board;
  1717. enum ath25_soc_type ath25_soc = ATH25_SOC_UNKNOWN;
  1718. +static struct resource ath25_eth0_res[] = {
  1719. + {
  1720. + .name = "eth0_membase",
  1721. + .flags = IORESOURCE_MEM,
  1722. + },
  1723. + {
  1724. + .name = "eth0_mii",
  1725. + .flags = IORESOURCE_MEM,
  1726. + },
  1727. + {
  1728. + .name = "eth0_irq",
  1729. + .flags = IORESOURCE_IRQ,
  1730. + }
  1731. +};
  1732. +
  1733. +static struct resource ath25_eth1_res[] = {
  1734. + {
  1735. + .name = "eth1_membase",
  1736. + .flags = IORESOURCE_MEM,
  1737. + },
  1738. + {
  1739. + .name = "eth1_mii",
  1740. + .flags = IORESOURCE_MEM,
  1741. + },
  1742. + {
  1743. + .name = "eth1_irq",
  1744. + .flags = IORESOURCE_IRQ,
  1745. + }
  1746. +};
  1747. +
  1748. +static struct platform_device ath25_eth[] = {
  1749. + {
  1750. + .id = 0,
  1751. + .name = "ar231x-eth",
  1752. + .resource = ath25_eth0_res,
  1753. + .num_resources = ARRAY_SIZE(ath25_eth0_res)
  1754. + },
  1755. + {
  1756. + .id = 1,
  1757. + .name = "ar231x-eth",
  1758. + .resource = ath25_eth1_res,
  1759. + .num_resources = ARRAY_SIZE(ath25_eth1_res)
  1760. + }
  1761. +};
  1762. +
  1763. static struct resource ath25_wmac0_res[] = {
  1764. {
  1765. .name = "wmac0_membase",
  1766. @@ -70,6 +115,25 @@ const char *get_system_type(void)
  1767. return soc_type_strings[ath25_soc];
  1768. }
  1769. +int __init ath25_add_ethernet(int nr, u32 base, const char *mii_name,
  1770. + u32 mii_base, int irq, void *pdata)
  1771. +{
  1772. + struct resource *res;
  1773. +
  1774. + ath25_eth[nr].dev.platform_data = pdata;
  1775. + res = &ath25_eth[nr].resource[0];
  1776. + res->start = base;
  1777. + res->end = base + 0x2000 - 1;
  1778. + res++;
  1779. + res->name = mii_name;
  1780. + res->start = mii_base;
  1781. + res->end = mii_base + 8 - 1;
  1782. + res++;
  1783. + res->start = irq;
  1784. + res->end = irq;
  1785. + return platform_device_register(&ath25_eth[nr]);
  1786. +}
  1787. +
  1788. void __init ath25_serial_setup(u32 mapbase, int irq, unsigned int uartclk)
  1789. {
  1790. struct uart_port s;
  1791. --- a/arch/mips/include/asm/mach-ath25/ath25_platform.h
  1792. +++ b/arch/mips/include/asm/mach-ath25/ath25_platform.h
  1793. @@ -70,4 +70,15 @@ struct ar231x_board_config {
  1794. const char *radio;
  1795. };
  1796. +/*
  1797. + * Platform device information for the Ethernet MAC
  1798. + */
  1799. +struct ar231x_eth {
  1800. + void (*reset_set)(u32);
  1801. + void (*reset_clear)(u32);
  1802. + u32 reset_mac;
  1803. + u32 reset_phy;
  1804. + char *macaddr;
  1805. +};
  1806. +
  1807. #endif /* __ASM_MACH_ATH25_PLATFORM_H */