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