cpsw.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. /*
  2. * Texas Instruments Ethernet Switch Driver
  3. *
  4. * Copyright (C) 2012 Texas Instruments
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/io.h>
  17. #include <linux/clk.h>
  18. #include <linux/timer.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/phy.h>
  27. #include <linux/workqueue.h>
  28. #include <linux/delay.h>
  29. #include <linux/platform_data/cpsw.h>
  30. #include "cpsw_ale.h"
  31. #include "davinci_cpdma.h"
  32. #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
  33. NETIF_MSG_DRV | NETIF_MSG_LINK | \
  34. NETIF_MSG_IFUP | NETIF_MSG_INTR | \
  35. NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
  36. NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
  37. NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
  38. NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
  39. NETIF_MSG_RX_STATUS)
  40. #define cpsw_info(priv, type, format, ...) \
  41. do { \
  42. if (netif_msg_##type(priv) && net_ratelimit()) \
  43. dev_info(priv->dev, format, ## __VA_ARGS__); \
  44. } while (0)
  45. #define cpsw_err(priv, type, format, ...) \
  46. do { \
  47. if (netif_msg_##type(priv) && net_ratelimit()) \
  48. dev_err(priv->dev, format, ## __VA_ARGS__); \
  49. } while (0)
  50. #define cpsw_dbg(priv, type, format, ...) \
  51. do { \
  52. if (netif_msg_##type(priv) && net_ratelimit()) \
  53. dev_dbg(priv->dev, format, ## __VA_ARGS__); \
  54. } while (0)
  55. #define cpsw_notice(priv, type, format, ...) \
  56. do { \
  57. if (netif_msg_##type(priv) && net_ratelimit()) \
  58. dev_notice(priv->dev, format, ## __VA_ARGS__); \
  59. } while (0)
  60. #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
  61. #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
  62. #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
  63. #define CPDMA_RXTHRESH 0x0c0
  64. #define CPDMA_RXFREE 0x0e0
  65. #define CPDMA_TXHDP 0x00
  66. #define CPDMA_RXHDP 0x20
  67. #define CPDMA_TXCP 0x40
  68. #define CPDMA_RXCP 0x60
  69. #define cpsw_dma_regs(base, offset) \
  70. (void __iomem *)((base) + (offset))
  71. #define cpsw_dma_rxthresh(base, offset) \
  72. (void __iomem *)((base) + (offset) + CPDMA_RXTHRESH)
  73. #define cpsw_dma_rxfree(base, offset) \
  74. (void __iomem *)((base) + (offset) + CPDMA_RXFREE)
  75. #define cpsw_dma_txhdp(base, offset) \
  76. (void __iomem *)((base) + (offset) + CPDMA_TXHDP)
  77. #define cpsw_dma_rxhdp(base, offset) \
  78. (void __iomem *)((base) + (offset) + CPDMA_RXHDP)
  79. #define cpsw_dma_txcp(base, offset) \
  80. (void __iomem *)((base) + (offset) + CPDMA_TXCP)
  81. #define cpsw_dma_rxcp(base, offset) \
  82. (void __iomem *)((base) + (offset) + CPDMA_RXCP)
  83. #define CPSW_POLL_WEIGHT 64
  84. #define CPSW_MIN_PACKET_SIZE 60
  85. #define CPSW_MAX_PACKET_SIZE (1500 + 14 + 4 + 4)
  86. #define RX_PRIORITY_MAPPING 0x76543210
  87. #define TX_PRIORITY_MAPPING 0x33221100
  88. #define CPDMA_TX_PRIORITY_MAP 0x76543210
  89. #define cpsw_enable_irq(priv) \
  90. do { \
  91. u32 i; \
  92. for (i = 0; i < priv->num_irqs; i++) \
  93. enable_irq(priv->irqs_table[i]); \
  94. } while (0);
  95. #define cpsw_disable_irq(priv) \
  96. do { \
  97. u32 i; \
  98. for (i = 0; i < priv->num_irqs; i++) \
  99. disable_irq_nosync(priv->irqs_table[i]); \
  100. } while (0);
  101. static int debug_level;
  102. module_param(debug_level, int, 0);
  103. MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
  104. static int ale_ageout = 10;
  105. module_param(ale_ageout, int, 0);
  106. MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
  107. static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
  108. module_param(rx_packet_max, int, 0);
  109. MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
  110. struct cpsw_ss_regs {
  111. u32 id_ver;
  112. u32 soft_reset;
  113. u32 control;
  114. u32 int_control;
  115. u32 rx_thresh_en;
  116. u32 rx_en;
  117. u32 tx_en;
  118. u32 misc_en;
  119. };
  120. struct cpsw_regs {
  121. u32 id_ver;
  122. u32 control;
  123. u32 soft_reset;
  124. u32 stat_port_en;
  125. u32 ptype;
  126. };
  127. struct cpsw_slave_regs {
  128. u32 max_blks;
  129. u32 blk_cnt;
  130. u32 flow_thresh;
  131. u32 port_vlan;
  132. u32 tx_pri_map;
  133. u32 ts_ctl;
  134. u32 ts_seq_ltype;
  135. u32 ts_vlan;
  136. u32 sa_lo;
  137. u32 sa_hi;
  138. };
  139. struct cpsw_host_regs {
  140. u32 max_blks;
  141. u32 blk_cnt;
  142. u32 flow_thresh;
  143. u32 port_vlan;
  144. u32 tx_pri_map;
  145. u32 cpdma_tx_pri_map;
  146. u32 cpdma_rx_chan_map;
  147. };
  148. struct cpsw_sliver_regs {
  149. u32 id_ver;
  150. u32 mac_control;
  151. u32 mac_status;
  152. u32 soft_reset;
  153. u32 rx_maxlen;
  154. u32 __reserved_0;
  155. u32 rx_pause;
  156. u32 tx_pause;
  157. u32 __reserved_1;
  158. u32 rx_pri_map;
  159. };
  160. struct cpsw_slave {
  161. struct cpsw_slave_regs __iomem *regs;
  162. struct cpsw_sliver_regs __iomem *sliver;
  163. int slave_num;
  164. u32 mac_control;
  165. struct cpsw_slave_data *data;
  166. struct phy_device *phy;
  167. };
  168. struct cpsw_priv {
  169. spinlock_t lock;
  170. struct platform_device *pdev;
  171. struct net_device *ndev;
  172. struct resource *cpsw_res;
  173. struct resource *cpsw_ss_res;
  174. struct napi_struct napi;
  175. struct device *dev;
  176. struct cpsw_platform_data data;
  177. struct cpsw_regs __iomem *regs;
  178. struct cpsw_ss_regs __iomem *ss_regs;
  179. struct cpsw_host_regs __iomem *host_port_regs;
  180. u32 msg_enable;
  181. struct net_device_stats stats;
  182. int rx_packet_max;
  183. int host_port;
  184. struct clk *clk;
  185. u8 mac_addr[ETH_ALEN];
  186. struct cpsw_slave *slaves;
  187. struct cpdma_ctlr *dma;
  188. struct cpdma_chan *txch, *rxch;
  189. struct cpsw_ale *ale;
  190. /* snapshot of IRQ numbers */
  191. u32 irqs_table[4];
  192. u32 num_irqs;
  193. };
  194. #define napi_to_priv(napi) container_of(napi, struct cpsw_priv, napi)
  195. #define for_each_slave(priv, func, arg...) \
  196. do { \
  197. int idx; \
  198. for (idx = 0; idx < (priv)->data.slaves; idx++) \
  199. (func)((priv)->slaves + idx, ##arg); \
  200. } while (0)
  201. static void cpsw_intr_enable(struct cpsw_priv *priv)
  202. {
  203. __raw_writel(0xFF, &priv->ss_regs->tx_en);
  204. __raw_writel(0xFF, &priv->ss_regs->rx_en);
  205. cpdma_ctlr_int_ctrl(priv->dma, true);
  206. return;
  207. }
  208. static void cpsw_intr_disable(struct cpsw_priv *priv)
  209. {
  210. __raw_writel(0, &priv->ss_regs->tx_en);
  211. __raw_writel(0, &priv->ss_regs->rx_en);
  212. cpdma_ctlr_int_ctrl(priv->dma, false);
  213. return;
  214. }
  215. void cpsw_tx_handler(void *token, int len, int status)
  216. {
  217. struct sk_buff *skb = token;
  218. struct net_device *ndev = skb->dev;
  219. struct cpsw_priv *priv = netdev_priv(ndev);
  220. if (unlikely(netif_queue_stopped(ndev)))
  221. netif_start_queue(ndev);
  222. priv->stats.tx_packets++;
  223. priv->stats.tx_bytes += len;
  224. dev_kfree_skb_any(skb);
  225. }
  226. void cpsw_rx_handler(void *token, int len, int status)
  227. {
  228. struct sk_buff *skb = token;
  229. struct net_device *ndev = skb->dev;
  230. struct cpsw_priv *priv = netdev_priv(ndev);
  231. int ret = 0;
  232. /* free and bail if we are shutting down */
  233. if (unlikely(!netif_running(ndev)) ||
  234. unlikely(!netif_carrier_ok(ndev))) {
  235. dev_kfree_skb_any(skb);
  236. return;
  237. }
  238. if (likely(status >= 0)) {
  239. skb_put(skb, len);
  240. skb->protocol = eth_type_trans(skb, ndev);
  241. netif_receive_skb(skb);
  242. priv->stats.rx_bytes += len;
  243. priv->stats.rx_packets++;
  244. skb = NULL;
  245. }
  246. if (unlikely(!netif_running(ndev))) {
  247. if (skb)
  248. dev_kfree_skb_any(skb);
  249. return;
  250. }
  251. if (likely(!skb)) {
  252. skb = netdev_alloc_skb_ip_align(ndev, priv->rx_packet_max);
  253. if (WARN_ON(!skb))
  254. return;
  255. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  256. skb_tailroom(skb), GFP_KERNEL);
  257. }
  258. WARN_ON(ret < 0);
  259. }
  260. static irqreturn_t cpsw_interrupt(int irq, void *dev_id)
  261. {
  262. struct cpsw_priv *priv = dev_id;
  263. if (likely(netif_running(priv->ndev))) {
  264. cpsw_intr_disable(priv);
  265. cpsw_disable_irq(priv);
  266. napi_schedule(&priv->napi);
  267. }
  268. return IRQ_HANDLED;
  269. }
  270. static inline int cpsw_get_slave_port(struct cpsw_priv *priv, u32 slave_num)
  271. {
  272. if (priv->host_port == 0)
  273. return slave_num + 1;
  274. else
  275. return slave_num;
  276. }
  277. static int cpsw_poll(struct napi_struct *napi, int budget)
  278. {
  279. struct cpsw_priv *priv = napi_to_priv(napi);
  280. int num_tx, num_rx;
  281. num_tx = cpdma_chan_process(priv->txch, 128);
  282. num_rx = cpdma_chan_process(priv->rxch, budget);
  283. if (num_rx || num_tx)
  284. cpsw_dbg(priv, intr, "poll %d rx, %d tx pkts\n",
  285. num_rx, num_tx);
  286. if (num_rx < budget) {
  287. napi_complete(napi);
  288. cpsw_intr_enable(priv);
  289. cpdma_ctlr_eoi(priv->dma);
  290. cpsw_enable_irq(priv);
  291. }
  292. return num_rx;
  293. }
  294. static inline void soft_reset(const char *module, void __iomem *reg)
  295. {
  296. unsigned long timeout = jiffies + HZ;
  297. __raw_writel(1, reg);
  298. do {
  299. cpu_relax();
  300. } while ((__raw_readl(reg) & 1) && time_after(timeout, jiffies));
  301. WARN(__raw_readl(reg) & 1, "failed to soft-reset %s\n", module);
  302. }
  303. #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \
  304. ((mac)[2] << 16) | ((mac)[3] << 24))
  305. #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8))
  306. static void cpsw_set_slave_mac(struct cpsw_slave *slave,
  307. struct cpsw_priv *priv)
  308. {
  309. __raw_writel(mac_hi(priv->mac_addr), &slave->regs->sa_hi);
  310. __raw_writel(mac_lo(priv->mac_addr), &slave->regs->sa_lo);
  311. }
  312. static void _cpsw_adjust_link(struct cpsw_slave *slave,
  313. struct cpsw_priv *priv, bool *link)
  314. {
  315. struct phy_device *phy = slave->phy;
  316. u32 mac_control = 0;
  317. u32 slave_port;
  318. if (!phy)
  319. return;
  320. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  321. if (phy->link) {
  322. mac_control = priv->data.mac_control;
  323. /* enable forwarding */
  324. cpsw_ale_control_set(priv->ale, slave_port,
  325. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  326. if (phy->speed == 1000)
  327. mac_control |= BIT(7); /* GIGABITEN */
  328. if (phy->duplex)
  329. mac_control |= BIT(0); /* FULLDUPLEXEN */
  330. *link = true;
  331. } else {
  332. mac_control = 0;
  333. /* disable forwarding */
  334. cpsw_ale_control_set(priv->ale, slave_port,
  335. ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
  336. }
  337. if (mac_control != slave->mac_control) {
  338. phy_print_status(phy);
  339. __raw_writel(mac_control, &slave->sliver->mac_control);
  340. }
  341. slave->mac_control = mac_control;
  342. }
  343. static void cpsw_adjust_link(struct net_device *ndev)
  344. {
  345. struct cpsw_priv *priv = netdev_priv(ndev);
  346. bool link = false;
  347. for_each_slave(priv, _cpsw_adjust_link, priv, &link);
  348. if (link) {
  349. netif_carrier_on(ndev);
  350. if (netif_running(ndev))
  351. netif_wake_queue(ndev);
  352. } else {
  353. netif_carrier_off(ndev);
  354. netif_stop_queue(ndev);
  355. }
  356. }
  357. static inline int __show_stat(char *buf, int maxlen, const char *name, u32 val)
  358. {
  359. static char *leader = "........................................";
  360. if (!val)
  361. return 0;
  362. else
  363. return snprintf(buf, maxlen, "%s %s %10d\n", name,
  364. leader + strlen(name), val);
  365. }
  366. static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
  367. {
  368. char name[32];
  369. u32 slave_port;
  370. sprintf(name, "slave-%d", slave->slave_num);
  371. soft_reset(name, &slave->sliver->soft_reset);
  372. /* setup priority mapping */
  373. __raw_writel(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
  374. __raw_writel(TX_PRIORITY_MAPPING, &slave->regs->tx_pri_map);
  375. /* setup max packet size, and mac address */
  376. __raw_writel(priv->rx_packet_max, &slave->sliver->rx_maxlen);
  377. cpsw_set_slave_mac(slave, priv);
  378. slave->mac_control = 0; /* no link yet */
  379. slave_port = cpsw_get_slave_port(priv, slave->slave_num);
  380. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  381. 1 << slave_port, 0, ALE_MCAST_FWD_2);
  382. slave->phy = phy_connect(priv->ndev, slave->data->phy_id,
  383. &cpsw_adjust_link, 0, slave->data->phy_if);
  384. if (IS_ERR(slave->phy)) {
  385. dev_err(priv->dev, "phy %s not found on slave %d\n",
  386. slave->data->phy_id, slave->slave_num);
  387. slave->phy = NULL;
  388. } else {
  389. dev_info(priv->dev, "phy found : id is : 0x%x\n",
  390. slave->phy->phy_id);
  391. phy_start(slave->phy);
  392. }
  393. }
  394. static void cpsw_init_host_port(struct cpsw_priv *priv)
  395. {
  396. /* soft reset the controller and initialize ale */
  397. soft_reset("cpsw", &priv->regs->soft_reset);
  398. cpsw_ale_start(priv->ale);
  399. /* switch to vlan unaware mode */
  400. cpsw_ale_control_set(priv->ale, 0, ALE_VLAN_AWARE, 0);
  401. /* setup host port priority mapping */
  402. __raw_writel(CPDMA_TX_PRIORITY_MAP,
  403. &priv->host_port_regs->cpdma_tx_pri_map);
  404. __raw_writel(0, &priv->host_port_regs->cpdma_rx_chan_map);
  405. cpsw_ale_control_set(priv->ale, priv->host_port,
  406. ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
  407. cpsw_ale_add_ucast(priv->ale, priv->mac_addr, priv->host_port, 0);
  408. cpsw_ale_add_mcast(priv->ale, priv->ndev->broadcast,
  409. 1 << priv->host_port, 0, ALE_MCAST_FWD_2);
  410. }
  411. static int cpsw_ndo_open(struct net_device *ndev)
  412. {
  413. struct cpsw_priv *priv = netdev_priv(ndev);
  414. int i, ret;
  415. u32 reg;
  416. cpsw_intr_disable(priv);
  417. netif_carrier_off(ndev);
  418. ret = clk_enable(priv->clk);
  419. if (ret < 0) {
  420. dev_err(priv->dev, "unable to turn on device clock\n");
  421. return ret;
  422. }
  423. reg = __raw_readl(&priv->regs->id_ver);
  424. dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
  425. CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
  426. CPSW_RTL_VERSION(reg));
  427. /* initialize host and slave ports */
  428. cpsw_init_host_port(priv);
  429. for_each_slave(priv, cpsw_slave_open, priv);
  430. /* setup tx dma to fixed prio and zero offset */
  431. cpdma_control_set(priv->dma, CPDMA_TX_PRIO_FIXED, 1);
  432. cpdma_control_set(priv->dma, CPDMA_RX_BUFFER_OFFSET, 0);
  433. /* disable priority elevation and enable statistics on all ports */
  434. __raw_writel(0, &priv->regs->ptype);
  435. /* enable statistics collection only on the host port */
  436. __raw_writel(0x7, &priv->regs->stat_port_en);
  437. if (WARN_ON(!priv->data.rx_descs))
  438. priv->data.rx_descs = 128;
  439. for (i = 0; i < priv->data.rx_descs; i++) {
  440. struct sk_buff *skb;
  441. ret = -ENOMEM;
  442. skb = netdev_alloc_skb_ip_align(priv->ndev,
  443. priv->rx_packet_max);
  444. if (!skb)
  445. break;
  446. ret = cpdma_chan_submit(priv->rxch, skb, skb->data,
  447. skb_tailroom(skb), GFP_KERNEL);
  448. if (WARN_ON(ret < 0))
  449. break;
  450. }
  451. /* continue even if we didn't manage to submit all receive descs */
  452. cpsw_info(priv, ifup, "submitted %d rx descriptors\n", i);
  453. cpdma_ctlr_start(priv->dma);
  454. cpsw_intr_enable(priv);
  455. napi_enable(&priv->napi);
  456. cpdma_ctlr_eoi(priv->dma);
  457. return 0;
  458. }
  459. static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_priv *priv)
  460. {
  461. if (!slave->phy)
  462. return;
  463. phy_stop(slave->phy);
  464. phy_disconnect(slave->phy);
  465. slave->phy = NULL;
  466. }
  467. static int cpsw_ndo_stop(struct net_device *ndev)
  468. {
  469. struct cpsw_priv *priv = netdev_priv(ndev);
  470. cpsw_info(priv, ifdown, "shutting down cpsw device\n");
  471. cpsw_intr_disable(priv);
  472. cpdma_ctlr_int_ctrl(priv->dma, false);
  473. cpdma_ctlr_stop(priv->dma);
  474. netif_stop_queue(priv->ndev);
  475. napi_disable(&priv->napi);
  476. netif_carrier_off(priv->ndev);
  477. cpsw_ale_stop(priv->ale);
  478. for_each_slave(priv, cpsw_slave_stop, priv);
  479. clk_disable(priv->clk);
  480. return 0;
  481. }
  482. static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
  483. struct net_device *ndev)
  484. {
  485. struct cpsw_priv *priv = netdev_priv(ndev);
  486. int ret;
  487. ndev->trans_start = jiffies;
  488. if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
  489. cpsw_err(priv, tx_err, "packet pad failed\n");
  490. priv->stats.tx_dropped++;
  491. return NETDEV_TX_OK;
  492. }
  493. ret = cpdma_chan_submit(priv->txch, skb, skb->data,
  494. skb->len, GFP_KERNEL);
  495. if (unlikely(ret != 0)) {
  496. cpsw_err(priv, tx_err, "desc submit failed\n");
  497. goto fail;
  498. }
  499. return NETDEV_TX_OK;
  500. fail:
  501. priv->stats.tx_dropped++;
  502. netif_stop_queue(ndev);
  503. return NETDEV_TX_BUSY;
  504. }
  505. static void cpsw_ndo_change_rx_flags(struct net_device *ndev, int flags)
  506. {
  507. /*
  508. * The switch cannot operate in promiscuous mode without substantial
  509. * headache. For promiscuous mode to work, we would need to put the
  510. * ALE in bypass mode and route all traffic to the host port.
  511. * Subsequently, the host will need to operate as a "bridge", learn,
  512. * and flood as needed. For now, we simply complain here and
  513. * do nothing about it :-)
  514. */
  515. if ((flags & IFF_PROMISC) && (ndev->flags & IFF_PROMISC))
  516. dev_err(&ndev->dev, "promiscuity ignored!\n");
  517. /*
  518. * The switch cannot filter multicast traffic unless it is configured
  519. * in "VLAN Aware" mode. Unfortunately, VLAN awareness requires a
  520. * whole bunch of additional logic that this driver does not implement
  521. * at present.
  522. */
  523. if ((flags & IFF_ALLMULTI) && !(ndev->flags & IFF_ALLMULTI))
  524. dev_err(&ndev->dev, "multicast traffic cannot be filtered!\n");
  525. }
  526. static void cpsw_ndo_tx_timeout(struct net_device *ndev)
  527. {
  528. struct cpsw_priv *priv = netdev_priv(ndev);
  529. cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
  530. priv->stats.tx_errors++;
  531. cpsw_intr_disable(priv);
  532. cpdma_ctlr_int_ctrl(priv->dma, false);
  533. cpdma_chan_stop(priv->txch);
  534. cpdma_chan_start(priv->txch);
  535. cpdma_ctlr_int_ctrl(priv->dma, true);
  536. cpsw_intr_enable(priv);
  537. cpdma_ctlr_eoi(priv->dma);
  538. }
  539. static struct net_device_stats *cpsw_ndo_get_stats(struct net_device *ndev)
  540. {
  541. struct cpsw_priv *priv = netdev_priv(ndev);
  542. return &priv->stats;
  543. }
  544. #ifdef CONFIG_NET_POLL_CONTROLLER
  545. static void cpsw_ndo_poll_controller(struct net_device *ndev)
  546. {
  547. struct cpsw_priv *priv = netdev_priv(ndev);
  548. cpsw_intr_disable(priv);
  549. cpdma_ctlr_int_ctrl(priv->dma, false);
  550. cpsw_interrupt(ndev->irq, priv);
  551. cpdma_ctlr_int_ctrl(priv->dma, true);
  552. cpsw_intr_enable(priv);
  553. cpdma_ctlr_eoi(priv->dma);
  554. }
  555. #endif
  556. static const struct net_device_ops cpsw_netdev_ops = {
  557. .ndo_open = cpsw_ndo_open,
  558. .ndo_stop = cpsw_ndo_stop,
  559. .ndo_start_xmit = cpsw_ndo_start_xmit,
  560. .ndo_change_rx_flags = cpsw_ndo_change_rx_flags,
  561. .ndo_validate_addr = eth_validate_addr,
  562. .ndo_tx_timeout = cpsw_ndo_tx_timeout,
  563. .ndo_get_stats = cpsw_ndo_get_stats,
  564. #ifdef CONFIG_NET_POLL_CONTROLLER
  565. .ndo_poll_controller = cpsw_ndo_poll_controller,
  566. #endif
  567. };
  568. static void cpsw_get_drvinfo(struct net_device *ndev,
  569. struct ethtool_drvinfo *info)
  570. {
  571. struct cpsw_priv *priv = netdev_priv(ndev);
  572. strcpy(info->driver, "TI CPSW Driver v1.0");
  573. strcpy(info->version, "1.0");
  574. strcpy(info->bus_info, priv->pdev->name);
  575. }
  576. static u32 cpsw_get_msglevel(struct net_device *ndev)
  577. {
  578. struct cpsw_priv *priv = netdev_priv(ndev);
  579. return priv->msg_enable;
  580. }
  581. static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
  582. {
  583. struct cpsw_priv *priv = netdev_priv(ndev);
  584. priv->msg_enable = value;
  585. }
  586. static const struct ethtool_ops cpsw_ethtool_ops = {
  587. .get_drvinfo = cpsw_get_drvinfo,
  588. .get_msglevel = cpsw_get_msglevel,
  589. .set_msglevel = cpsw_set_msglevel,
  590. .get_link = ethtool_op_get_link,
  591. };
  592. static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_priv *priv)
  593. {
  594. void __iomem *regs = priv->regs;
  595. int slave_num = slave->slave_num;
  596. struct cpsw_slave_data *data = priv->data.slave_data + slave_num;
  597. slave->data = data;
  598. slave->regs = regs + data->slave_reg_ofs;
  599. slave->sliver = regs + data->sliver_reg_ofs;
  600. }
  601. static int __devinit cpsw_probe(struct platform_device *pdev)
  602. {
  603. struct cpsw_platform_data *data = pdev->dev.platform_data;
  604. struct net_device *ndev;
  605. struct cpsw_priv *priv;
  606. struct cpdma_params dma_params;
  607. struct cpsw_ale_params ale_params;
  608. void __iomem *regs;
  609. struct resource *res;
  610. int ret = 0, i, k = 0;
  611. if (!data) {
  612. pr_err("platform data missing\n");
  613. return -ENODEV;
  614. }
  615. ndev = alloc_etherdev(sizeof(struct cpsw_priv));
  616. if (!ndev) {
  617. pr_err("error allocating net_device\n");
  618. return -ENOMEM;
  619. }
  620. platform_set_drvdata(pdev, ndev);
  621. priv = netdev_priv(ndev);
  622. spin_lock_init(&priv->lock);
  623. priv->data = *data;
  624. priv->pdev = pdev;
  625. priv->ndev = ndev;
  626. priv->dev = &ndev->dev;
  627. priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
  628. priv->rx_packet_max = max(rx_packet_max, 128);
  629. if (is_valid_ether_addr(data->slave_data[0].mac_addr)) {
  630. memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN);
  631. pr_info("Detected MACID = %pM", priv->mac_addr);
  632. } else {
  633. random_ether_addr(priv->mac_addr);
  634. pr_info("Random MACID = %pM", priv->mac_addr);
  635. }
  636. memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
  637. priv->slaves = kzalloc(sizeof(struct cpsw_slave) * data->slaves,
  638. GFP_KERNEL);
  639. if (!priv->slaves) {
  640. ret = -EBUSY;
  641. goto clean_ndev_ret;
  642. }
  643. for (i = 0; i < data->slaves; i++)
  644. priv->slaves[i].slave_num = i;
  645. priv->clk = clk_get(&pdev->dev, NULL);
  646. if (IS_ERR(priv->clk)) {
  647. dev_err(priv->dev, "failed to get device clock)\n");
  648. ret = -EBUSY;
  649. }
  650. priv->cpsw_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  651. if (!priv->cpsw_res) {
  652. dev_err(priv->dev, "error getting i/o resource\n");
  653. ret = -ENOENT;
  654. goto clean_clk_ret;
  655. }
  656. if (!request_mem_region(priv->cpsw_res->start,
  657. resource_size(priv->cpsw_res), ndev->name)) {
  658. dev_err(priv->dev, "failed request i/o region\n");
  659. ret = -ENXIO;
  660. goto clean_clk_ret;
  661. }
  662. regs = ioremap(priv->cpsw_res->start, resource_size(priv->cpsw_res));
  663. if (!regs) {
  664. dev_err(priv->dev, "unable to map i/o region\n");
  665. goto clean_cpsw_iores_ret;
  666. }
  667. priv->regs = regs;
  668. priv->host_port = data->host_port_num;
  669. priv->host_port_regs = regs + data->host_port_reg_ofs;
  670. priv->cpsw_ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  671. if (!priv->cpsw_ss_res) {
  672. dev_err(priv->dev, "error getting i/o resource\n");
  673. ret = -ENOENT;
  674. goto clean_clk_ret;
  675. }
  676. if (!request_mem_region(priv->cpsw_ss_res->start,
  677. resource_size(priv->cpsw_ss_res), ndev->name)) {
  678. dev_err(priv->dev, "failed request i/o region\n");
  679. ret = -ENXIO;
  680. goto clean_clk_ret;
  681. }
  682. regs = ioremap(priv->cpsw_ss_res->start,
  683. resource_size(priv->cpsw_ss_res));
  684. if (!regs) {
  685. dev_err(priv->dev, "unable to map i/o region\n");
  686. goto clean_cpsw_ss_iores_ret;
  687. }
  688. priv->ss_regs = regs;
  689. for_each_slave(priv, cpsw_slave_init, priv);
  690. memset(&dma_params, 0, sizeof(dma_params));
  691. dma_params.dev = &pdev->dev;
  692. dma_params.dmaregs = cpsw_dma_regs((u32)priv->regs,
  693. data->cpdma_reg_ofs);
  694. dma_params.rxthresh = cpsw_dma_rxthresh((u32)priv->regs,
  695. data->cpdma_reg_ofs);
  696. dma_params.rxfree = cpsw_dma_rxfree((u32)priv->regs,
  697. data->cpdma_reg_ofs);
  698. dma_params.txhdp = cpsw_dma_txhdp((u32)priv->regs,
  699. data->cpdma_sram_ofs);
  700. dma_params.rxhdp = cpsw_dma_rxhdp((u32)priv->regs,
  701. data->cpdma_sram_ofs);
  702. dma_params.txcp = cpsw_dma_txcp((u32)priv->regs,
  703. data->cpdma_sram_ofs);
  704. dma_params.rxcp = cpsw_dma_rxcp((u32)priv->regs,
  705. data->cpdma_sram_ofs);
  706. dma_params.num_chan = data->channels;
  707. dma_params.has_soft_reset = true;
  708. dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE;
  709. dma_params.desc_mem_size = data->bd_ram_size;
  710. dma_params.desc_align = 16;
  711. dma_params.has_ext_regs = true;
  712. dma_params.desc_mem_phys = data->no_bd_ram ? 0 :
  713. (u32 __force)priv->cpsw_res->start + data->bd_ram_ofs;
  714. dma_params.desc_hw_addr = data->hw_ram_addr ?
  715. data->hw_ram_addr : dma_params.desc_mem_phys ;
  716. priv->dma = cpdma_ctlr_create(&dma_params);
  717. if (!priv->dma) {
  718. dev_err(priv->dev, "error initializing dma\n");
  719. ret = -ENOMEM;
  720. goto clean_iomap_ret;
  721. }
  722. priv->txch = cpdma_chan_create(priv->dma, tx_chan_num(0),
  723. cpsw_tx_handler);
  724. priv->rxch = cpdma_chan_create(priv->dma, rx_chan_num(0),
  725. cpsw_rx_handler);
  726. if (WARN_ON(!priv->txch || !priv->rxch)) {
  727. dev_err(priv->dev, "error initializing dma channels\n");
  728. ret = -ENOMEM;
  729. goto clean_dma_ret;
  730. }
  731. memset(&ale_params, 0, sizeof(ale_params));
  732. ale_params.dev = &ndev->dev;
  733. ale_params.ale_regs = (void *)((u32)priv->regs) +
  734. ((u32)data->ale_reg_ofs);
  735. ale_params.ale_ageout = ale_ageout;
  736. ale_params.ale_entries = data->ale_entries;
  737. ale_params.ale_ports = data->slaves;
  738. priv->ale = cpsw_ale_create(&ale_params);
  739. if (!priv->ale) {
  740. dev_err(priv->dev, "error initializing ale engine\n");
  741. ret = -ENODEV;
  742. goto clean_dma_ret;
  743. }
  744. ndev->irq = platform_get_irq(pdev, 0);
  745. if (ndev->irq < 0) {
  746. dev_err(priv->dev, "error getting irq resource\n");
  747. ret = -ENOENT;
  748. goto clean_ale_ret;
  749. }
  750. while ((res = platform_get_resource(priv->pdev, IORESOURCE_IRQ, k))) {
  751. for (i = res->start; i <= res->end; i++) {
  752. if (request_irq(i, cpsw_interrupt, IRQF_DISABLED,
  753. dev_name(&pdev->dev), priv)) {
  754. dev_err(priv->dev, "error attaching irq\n");
  755. goto clean_ale_ret;
  756. }
  757. priv->irqs_table[k] = i;
  758. priv->num_irqs = k;
  759. }
  760. k++;
  761. }
  762. ndev->flags |= IFF_ALLMULTI; /* see cpsw_ndo_change_rx_flags() */
  763. ndev->netdev_ops = &cpsw_netdev_ops;
  764. SET_ETHTOOL_OPS(ndev, &cpsw_ethtool_ops);
  765. netif_napi_add(ndev, &priv->napi, cpsw_poll, CPSW_POLL_WEIGHT);
  766. /* register the network device */
  767. SET_NETDEV_DEV(ndev, &pdev->dev);
  768. ret = register_netdev(ndev);
  769. if (ret) {
  770. dev_err(priv->dev, "error registering net device\n");
  771. ret = -ENODEV;
  772. goto clean_irq_ret;
  773. }
  774. cpsw_notice(priv, probe, "initialized device (regs %x, irq %d)\n",
  775. priv->cpsw_res->start, ndev->irq);
  776. return 0;
  777. clean_irq_ret:
  778. free_irq(ndev->irq, priv);
  779. clean_ale_ret:
  780. cpsw_ale_destroy(priv->ale);
  781. clean_dma_ret:
  782. cpdma_chan_destroy(priv->txch);
  783. cpdma_chan_destroy(priv->rxch);
  784. cpdma_ctlr_destroy(priv->dma);
  785. clean_iomap_ret:
  786. iounmap(priv->regs);
  787. clean_cpsw_ss_iores_ret:
  788. release_mem_region(priv->cpsw_ss_res->start,
  789. resource_size(priv->cpsw_ss_res));
  790. clean_cpsw_iores_ret:
  791. release_mem_region(priv->cpsw_res->start,
  792. resource_size(priv->cpsw_res));
  793. clean_clk_ret:
  794. clk_put(priv->clk);
  795. kfree(priv->slaves);
  796. clean_ndev_ret:
  797. free_netdev(ndev);
  798. return ret;
  799. }
  800. static int __devexit cpsw_remove(struct platform_device *pdev)
  801. {
  802. struct net_device *ndev = platform_get_drvdata(pdev);
  803. struct cpsw_priv *priv = netdev_priv(ndev);
  804. pr_info("removing device");
  805. platform_set_drvdata(pdev, NULL);
  806. free_irq(ndev->irq, priv);
  807. cpsw_ale_destroy(priv->ale);
  808. cpdma_chan_destroy(priv->txch);
  809. cpdma_chan_destroy(priv->rxch);
  810. cpdma_ctlr_destroy(priv->dma);
  811. iounmap(priv->regs);
  812. release_mem_region(priv->cpsw_res->start,
  813. resource_size(priv->cpsw_res));
  814. release_mem_region(priv->cpsw_ss_res->start,
  815. resource_size(priv->cpsw_ss_res));
  816. clk_put(priv->clk);
  817. kfree(priv->slaves);
  818. free_netdev(ndev);
  819. return 0;
  820. }
  821. static int cpsw_suspend(struct device *dev)
  822. {
  823. struct platform_device *pdev = to_platform_device(dev);
  824. struct net_device *ndev = platform_get_drvdata(pdev);
  825. if (netif_running(ndev))
  826. cpsw_ndo_stop(ndev);
  827. return 0;
  828. }
  829. static int cpsw_resume(struct device *dev)
  830. {
  831. struct platform_device *pdev = to_platform_device(dev);
  832. struct net_device *ndev = platform_get_drvdata(pdev);
  833. if (netif_running(ndev))
  834. cpsw_ndo_open(ndev);
  835. return 0;
  836. }
  837. static const struct dev_pm_ops cpsw_pm_ops = {
  838. .suspend = cpsw_suspend,
  839. .resume = cpsw_resume,
  840. };
  841. static struct platform_driver cpsw_driver = {
  842. .driver = {
  843. .name = "cpsw",
  844. .owner = THIS_MODULE,
  845. .pm = &cpsw_pm_ops,
  846. },
  847. .probe = cpsw_probe,
  848. .remove = __devexit_p(cpsw_remove),
  849. };
  850. static int __init cpsw_init(void)
  851. {
  852. return platform_driver_register(&cpsw_driver);
  853. }
  854. late_initcall(cpsw_init);
  855. static void __exit cpsw_exit(void)
  856. {
  857. platform_driver_unregister(&cpsw_driver);
  858. }
  859. module_exit(cpsw_exit);
  860. MODULE_LICENSE("GPL");
  861. MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>");
  862. MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>");
  863. MODULE_DESCRIPTION("TI CPSW Ethernet driver");