altera_tse.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*
  2. * Altera 10/100/1000 triple speed ethernet mac driver
  3. *
  4. * Copyright (C) 2008 Altera Corporation.
  5. * Copyright (C) 2010 Thomas Chou <thomas@wytron.com.tw>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <malloc.h>
  14. #include <net.h>
  15. #include <command.h>
  16. #include <asm/cache.h>
  17. #include <asm/dma-mapping.h>
  18. #include <miiphy.h>
  19. #include "altera_tse.h"
  20. /* sgdma debug - print descriptor */
  21. static void alt_sgdma_print_desc(volatile struct alt_sgdma_descriptor *desc)
  22. {
  23. debug("SGDMA DEBUG :\n");
  24. debug("desc->source : 0x%x \n", (unsigned int)desc->source);
  25. debug("desc->destination : 0x%x \n", (unsigned int)desc->destination);
  26. debug("desc->next : 0x%x \n", (unsigned int)desc->next);
  27. debug("desc->source_pad : 0x%x \n", (unsigned int)desc->source_pad);
  28. debug("desc->destination_pad : 0x%x \n",
  29. (unsigned int)desc->destination_pad);
  30. debug("desc->next_pad : 0x%x \n", (unsigned int)desc->next_pad);
  31. debug("desc->bytes_to_transfer : 0x%x \n",
  32. (unsigned int)desc->bytes_to_transfer);
  33. debug("desc->actual_bytes_transferred : 0x%x \n",
  34. (unsigned int)desc->actual_bytes_transferred);
  35. debug("desc->descriptor_status : 0x%x \n",
  36. (unsigned int)desc->descriptor_status);
  37. debug("desc->descriptor_control : 0x%x \n",
  38. (unsigned int)desc->descriptor_control);
  39. }
  40. /* This is a generic routine that the SGDMA mode-specific routines
  41. * call to populate a descriptor.
  42. * arg1 :pointer to first SGDMA descriptor.
  43. * arg2 :pointer to next SGDMA descriptor.
  44. * arg3 :Address to where data to be written.
  45. * arg4 :Address from where data to be read.
  46. * arg5 :no of byte to transaction.
  47. * arg6 :variable indicating to generate start of packet or not
  48. * arg7 :read fixed
  49. * arg8 :write fixed
  50. * arg9 :read burst
  51. * arg10 :write burst
  52. * arg11 :atlantic_channel number
  53. */
  54. static void alt_sgdma_construct_descriptor_burst(
  55. volatile struct alt_sgdma_descriptor *desc,
  56. volatile struct alt_sgdma_descriptor *next,
  57. unsigned int *read_addr,
  58. unsigned int *write_addr,
  59. unsigned short length_or_eop,
  60. int generate_eop,
  61. int read_fixed,
  62. int write_fixed_or_sop,
  63. int read_burst,
  64. int write_burst,
  65. unsigned char atlantic_channel)
  66. {
  67. /*
  68. * Mark the "next" descriptor as "not" owned by hardware. This prevents
  69. * The SGDMA controller from continuing to process the chain. This is
  70. * done as a single IO write to bypass cache, without flushing
  71. * the entire descriptor, since only the 8-bit descriptor status must
  72. * be flushed.
  73. */
  74. if (!next)
  75. debug("Next descriptor not defined!!\n");
  76. next->descriptor_control = (next->descriptor_control &
  77. ~ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK);
  78. desc->source = (unsigned int *)((unsigned int)read_addr & 0x1FFFFFFF);
  79. desc->destination =
  80. (unsigned int *)((unsigned int)write_addr & 0x1FFFFFFF);
  81. desc->next = (unsigned int *)((unsigned int)next & 0x1FFFFFFF);
  82. desc->source_pad = 0x0;
  83. desc->destination_pad = 0x0;
  84. desc->next_pad = 0x0;
  85. desc->bytes_to_transfer = length_or_eop;
  86. desc->actual_bytes_transferred = 0;
  87. desc->descriptor_status = 0x0;
  88. /* SGDMA burst not currently supported */
  89. desc->read_burst = 0;
  90. desc->write_burst = 0;
  91. /*
  92. * Set the descriptor control block as follows:
  93. * - Set "owned by hardware" bit
  94. * - Optionally set "generate EOP" bit
  95. * - Optionally set the "read from fixed address" bit
  96. * - Optionally set the "write to fixed address bit (which serves
  97. * serves as a "generate SOP" control bit in memory-to-stream mode).
  98. * - Set the 4-bit atlantic channel, if specified
  99. *
  100. * Note this step is performed after all other descriptor information
  101. * has been filled out so that, if the controller already happens to be
  102. * pointing at this descriptor, it will not run (via the "owned by
  103. * hardware" bit) until all other descriptor has been set up.
  104. */
  105. desc->descriptor_control =
  106. ((ALT_SGDMA_DESCRIPTOR_CONTROL_OWNED_BY_HW_MSK) |
  107. (generate_eop ?
  108. ALT_SGDMA_DESCRIPTOR_CONTROL_GENERATE_EOP_MSK : 0x0) |
  109. (read_fixed ?
  110. ALT_SGDMA_DESCRIPTOR_CONTROL_READ_FIXED_ADDRESS_MSK : 0x0) |
  111. (write_fixed_or_sop ?
  112. ALT_SGDMA_DESCRIPTOR_CONTROL_WRITE_FIXED_ADDRESS_MSK : 0x0) |
  113. (atlantic_channel ? ((atlantic_channel & 0x0F) << 3) : 0)
  114. );
  115. }
  116. static int alt_sgdma_do_sync_transfer(volatile struct alt_sgdma_registers *dev,
  117. volatile struct alt_sgdma_descriptor *desc)
  118. {
  119. unsigned int status;
  120. int counter = 0;
  121. /* Wait for any pending transfers to complete */
  122. alt_sgdma_print_desc(desc);
  123. status = dev->status;
  124. counter = 0;
  125. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  126. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  127. break;
  128. }
  129. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  130. debug("Timeout waiting sgdma in do sync!\n");
  131. /*
  132. * Clear any (previous) status register information
  133. * that might occlude our error checking later.
  134. */
  135. dev->status = 0xFF;
  136. /* Point the controller at the descriptor */
  137. dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
  138. debug("next desc in sgdma 0x%x\n",
  139. (unsigned int)dev->next_descriptor_pointer);
  140. /*
  141. * Set up SGDMA controller to:
  142. * - Disable interrupt generation
  143. * - Run once a valid descriptor is written to controller
  144. * - Stop on an error with any particular descriptor
  145. */
  146. dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
  147. ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
  148. /* Wait for the descriptor (chain) to complete */
  149. status = dev->status;
  150. debug("wait for sgdma....");
  151. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK)
  152. ;
  153. debug("done\n");
  154. /* Clear Run */
  155. dev->control = (dev->control & (~ALT_SGDMA_CONTROL_RUN_MSK));
  156. /* Get & clear status register contents */
  157. status = dev->status;
  158. dev->status = 0xFF;
  159. /* we really should check if the transfer completes properly */
  160. debug("tx sgdma status = 0x%x", status);
  161. return 0;
  162. }
  163. static int alt_sgdma_do_async_transfer(volatile struct alt_sgdma_registers *dev,
  164. volatile struct alt_sgdma_descriptor *desc)
  165. {
  166. unsigned int status;
  167. int counter = 0;
  168. /* Wait for any pending transfers to complete */
  169. alt_sgdma_print_desc(desc);
  170. status = dev->status;
  171. counter = 0;
  172. while (dev->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  173. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  174. break;
  175. }
  176. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  177. debug("Timeout waiting sgdma in do async!\n");
  178. /*
  179. * Clear the RUN bit in the control register. This is needed
  180. * to restart the SGDMA engine later on.
  181. */
  182. dev->control = 0;
  183. /*
  184. * Clear any (previous) status register information
  185. * that might occlude our error checking later.
  186. */
  187. dev->status = 0xFF;
  188. /* Point the controller at the descriptor */
  189. dev->next_descriptor_pointer = (unsigned int)desc & 0x1FFFFFFF;
  190. /*
  191. * Set up SGDMA controller to:
  192. * - Disable interrupt generation
  193. * - Run once a valid descriptor is written to controller
  194. * - Stop on an error with any particular descriptor
  195. */
  196. dev->control = (ALT_SGDMA_CONTROL_RUN_MSK |
  197. ALT_SGDMA_CONTROL_STOP_DMA_ER_MSK);
  198. /* we really should check if the transfer completes properly */
  199. return 0;
  200. }
  201. /* u-boot interface */
  202. static int tse_adjust_link(struct altera_tse_priv *priv)
  203. {
  204. unsigned int refvar;
  205. refvar = priv->mac_dev->command_config.image;
  206. if (!(priv->duplexity))
  207. refvar |= ALTERA_TSE_CMD_HD_ENA_MSK;
  208. else
  209. refvar &= ~ALTERA_TSE_CMD_HD_ENA_MSK;
  210. switch (priv->speed) {
  211. case 1000:
  212. refvar |= ALTERA_TSE_CMD_ETH_SPEED_MSK;
  213. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  214. break;
  215. case 100:
  216. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  217. refvar &= ~ALTERA_TSE_CMD_ENA_10_MSK;
  218. break;
  219. case 10:
  220. refvar &= ~ALTERA_TSE_CMD_ETH_SPEED_MSK;
  221. refvar |= ALTERA_TSE_CMD_ENA_10_MSK;
  222. break;
  223. }
  224. priv->mac_dev->command_config.image = refvar;
  225. return 0;
  226. }
  227. static int tse_eth_send(struct eth_device *dev, void *packet, int length)
  228. {
  229. struct altera_tse_priv *priv = dev->priv;
  230. volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  231. volatile struct alt_sgdma_descriptor *tx_desc =
  232. (volatile struct alt_sgdma_descriptor *)priv->tx_desc;
  233. volatile struct alt_sgdma_descriptor *tx_desc_cur =
  234. (volatile struct alt_sgdma_descriptor *)&tx_desc[0];
  235. flush_dcache_range((unsigned long)packet,
  236. (unsigned long)packet + length);
  237. alt_sgdma_construct_descriptor_burst(
  238. (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
  239. (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
  240. (unsigned int *)packet, /* read addr */
  241. (unsigned int *)0,
  242. length, /* length or EOP ,will change for each tx */
  243. 0x1, /* gen eop */
  244. 0x0, /* read fixed */
  245. 0x1, /* write fixed or sop */
  246. 0x0, /* read burst */
  247. 0x0, /* write burst */
  248. 0x0 /* channel */
  249. );
  250. debug("TX Packet @ 0x%x,0x%x bytes", (unsigned int)packet, length);
  251. /* send the packet */
  252. debug("sending packet\n");
  253. alt_sgdma_do_sync_transfer(tx_sgdma, tx_desc_cur);
  254. debug("sent %d bytes\n", tx_desc_cur->actual_bytes_transferred);
  255. return tx_desc_cur->actual_bytes_transferred;
  256. }
  257. static int tse_eth_rx(struct eth_device *dev)
  258. {
  259. int packet_length = 0;
  260. struct altera_tse_priv *priv = dev->priv;
  261. volatile struct alt_sgdma_descriptor *rx_desc =
  262. (volatile struct alt_sgdma_descriptor *)priv->rx_desc;
  263. volatile struct alt_sgdma_descriptor *rx_desc_cur = &rx_desc[0];
  264. if (rx_desc_cur->descriptor_status &
  265. ALT_SGDMA_DESCRIPTOR_STATUS_TERMINATED_BY_EOP_MSK) {
  266. debug("got packet\n");
  267. packet_length = rx_desc->actual_bytes_transferred;
  268. net_process_received_packet(net_rx_packets[0], packet_length);
  269. /* start descriptor again */
  270. flush_dcache_range((unsigned long)(net_rx_packets[0]),
  271. (unsigned long)(net_rx_packets[0] +
  272. PKTSIZE_ALIGN));
  273. alt_sgdma_construct_descriptor_burst(
  274. (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
  275. (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
  276. (unsigned int)0x0, /* read addr */
  277. (unsigned int *)net_rx_packets[0],
  278. 0x0, /* length or EOP */
  279. 0x0, /* gen eop */
  280. 0x0, /* read fixed */
  281. 0x0, /* write fixed or sop */
  282. 0x0, /* read burst */
  283. 0x0, /* write burst */
  284. 0x0 /* channel */
  285. );
  286. /* setup the sgdma */
  287. alt_sgdma_do_async_transfer(priv->sgdma_rx, &rx_desc[0]);
  288. return packet_length;
  289. }
  290. return -1;
  291. }
  292. static void tse_eth_halt(struct eth_device *dev)
  293. {
  294. /* don't do anything! */
  295. /* this gets called after each uboot */
  296. /* network command. don't need to reset the thing all of the time */
  297. }
  298. static void tse_eth_reset(struct eth_device *dev)
  299. {
  300. /* stop sgdmas, disable tse receive */
  301. struct altera_tse_priv *priv = dev->priv;
  302. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  303. volatile struct alt_sgdma_registers *rx_sgdma = priv->sgdma_rx;
  304. volatile struct alt_sgdma_registers *tx_sgdma = priv->sgdma_tx;
  305. int counter;
  306. volatile struct alt_sgdma_descriptor *rx_desc =
  307. (volatile struct alt_sgdma_descriptor *)&priv->rx_desc[0];
  308. /* clear rx desc & wait for sgdma to complete */
  309. rx_desc->descriptor_control = 0;
  310. rx_sgdma->control = 0;
  311. counter = 0;
  312. while (rx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  313. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  314. break;
  315. }
  316. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
  317. debug("Timeout waiting for rx sgdma!\n");
  318. rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  319. rx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  320. }
  321. counter = 0;
  322. tx_sgdma->control = 0;
  323. while (tx_sgdma->status & ALT_SGDMA_STATUS_BUSY_MSK) {
  324. if (counter++ > ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR)
  325. break;
  326. }
  327. if (counter >= ALT_TSE_SGDMA_BUSY_WATCHDOG_CNTR) {
  328. debug("Timeout waiting for tx sgdma!\n");
  329. tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  330. tx_sgdma->control = ALT_SGDMA_CONTROL_SOFTWARERESET_MSK;
  331. }
  332. /* reset the mac */
  333. mac_dev->command_config.bits.transmit_enable = 1;
  334. mac_dev->command_config.bits.receive_enable = 1;
  335. mac_dev->command_config.bits.software_reset = 1;
  336. counter = 0;
  337. while (mac_dev->command_config.bits.software_reset) {
  338. if (counter++ > ALT_TSE_SW_RESET_WATCHDOG_CNTR)
  339. break;
  340. }
  341. if (counter >= ALT_TSE_SW_RESET_WATCHDOG_CNTR)
  342. debug("TSEMAC SW reset bit never cleared!\n");
  343. }
  344. static int tse_mdio_read(struct altera_tse_priv *priv, unsigned int regnum)
  345. {
  346. volatile struct alt_tse_mac *mac_dev;
  347. unsigned int *mdio_regs;
  348. unsigned int data;
  349. u16 value;
  350. mac_dev = priv->mac_dev;
  351. /* set mdio address */
  352. mac_dev->mdio_phy1_addr = priv->phyaddr;
  353. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  354. /* get the data */
  355. data = mdio_regs[regnum];
  356. value = data & 0xffff;
  357. return value;
  358. }
  359. static int tse_mdio_write(struct altera_tse_priv *priv, unsigned int regnum,
  360. unsigned int value)
  361. {
  362. volatile struct alt_tse_mac *mac_dev;
  363. unsigned int *mdio_regs;
  364. unsigned int data;
  365. mac_dev = priv->mac_dev;
  366. /* set mdio address */
  367. mac_dev->mdio_phy1_addr = priv->phyaddr;
  368. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  369. /* get the data */
  370. data = (unsigned int)value;
  371. mdio_regs[regnum] = data;
  372. return 0;
  373. }
  374. /* MDIO access to phy */
  375. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  376. static int altera_tse_miiphy_write(const char *devname, unsigned char addr,
  377. unsigned char reg, unsigned short value)
  378. {
  379. struct eth_device *dev;
  380. struct altera_tse_priv *priv;
  381. dev = eth_get_dev_by_name(devname);
  382. priv = dev->priv;
  383. tse_mdio_write(priv, (uint) reg, (uint) value);
  384. return 0;
  385. }
  386. static int altera_tse_miiphy_read(const char *devname, unsigned char addr,
  387. unsigned char reg, unsigned short *value)
  388. {
  389. struct eth_device *dev;
  390. struct altera_tse_priv *priv;
  391. volatile struct alt_tse_mac *mac_dev;
  392. unsigned int *mdio_regs;
  393. dev = eth_get_dev_by_name(devname);
  394. priv = dev->priv;
  395. mac_dev = priv->mac_dev;
  396. mac_dev->mdio_phy1_addr = (int)addr;
  397. mdio_regs = (unsigned int *)&mac_dev->mdio_phy1;
  398. *value = 0xffff & mdio_regs[reg];
  399. return 0;
  400. }
  401. #endif
  402. /*
  403. * Also copied from tsec.c
  404. */
  405. /* Parse the status register for link, and then do
  406. * auto-negotiation
  407. */
  408. static uint mii_parse_sr(uint mii_reg, struct altera_tse_priv *priv)
  409. {
  410. /*
  411. * Wait if the link is up, and autonegotiation is in progress
  412. * (ie - we're capable and it's not done)
  413. */
  414. mii_reg = tse_mdio_read(priv, MIIM_STATUS);
  415. if (!(mii_reg & MIIM_STATUS_LINK) && (mii_reg & BMSR_ANEGCAPABLE)
  416. && !(mii_reg & BMSR_ANEGCOMPLETE)) {
  417. int i = 0;
  418. puts("Waiting for PHY auto negotiation to complete");
  419. while (!(mii_reg & BMSR_ANEGCOMPLETE)) {
  420. /*
  421. * Timeout reached ?
  422. */
  423. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  424. puts(" TIMEOUT !\n");
  425. priv->link = 0;
  426. return 0;
  427. }
  428. if ((i++ % 1000) == 0)
  429. putc('.');
  430. udelay(1000); /* 1 ms */
  431. mii_reg = tse_mdio_read(priv, MIIM_STATUS);
  432. }
  433. puts(" done\n");
  434. priv->link = 1;
  435. udelay(500000); /* another 500 ms (results in faster booting) */
  436. } else {
  437. if (mii_reg & MIIM_STATUS_LINK) {
  438. debug("Link is up\n");
  439. priv->link = 1;
  440. } else {
  441. debug("Link is down\n");
  442. priv->link = 0;
  443. }
  444. }
  445. return 0;
  446. }
  447. /* Parse the 88E1011's status register for speed and duplex
  448. * information
  449. */
  450. static uint mii_parse_88E1011_psr(uint mii_reg, struct altera_tse_priv *priv)
  451. {
  452. uint speed;
  453. mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
  454. if ((mii_reg & MIIM_88E1011_PHYSTAT_LINK) &&
  455. !(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  456. int i = 0;
  457. puts("Waiting for PHY realtime link");
  458. while (!(mii_reg & MIIM_88E1011_PHYSTAT_SPDDONE)) {
  459. /* Timeout reached ? */
  460. if (i > PHY_AUTONEGOTIATE_TIMEOUT) {
  461. puts(" TIMEOUT !\n");
  462. priv->link = 0;
  463. break;
  464. }
  465. if ((i++ == 1000) == 0) {
  466. i = 0;
  467. puts(".");
  468. }
  469. udelay(1000); /* 1 ms */
  470. mii_reg = tse_mdio_read(priv, MIIM_88E1011_PHY_STATUS);
  471. }
  472. puts(" done\n");
  473. udelay(500000); /* another 500 ms (results in faster booting) */
  474. } else {
  475. if (mii_reg & MIIM_88E1011_PHYSTAT_LINK)
  476. priv->link = 1;
  477. else
  478. priv->link = 0;
  479. }
  480. if (mii_reg & MIIM_88E1011_PHYSTAT_DUPLEX)
  481. priv->duplexity = 1;
  482. else
  483. priv->duplexity = 0;
  484. speed = (mii_reg & MIIM_88E1011_PHYSTAT_SPEED);
  485. switch (speed) {
  486. case MIIM_88E1011_PHYSTAT_GBIT:
  487. priv->speed = 1000;
  488. debug("PHY Speed is 1000Mbit\n");
  489. break;
  490. case MIIM_88E1011_PHYSTAT_100:
  491. debug("PHY Speed is 100Mbit\n");
  492. priv->speed = 100;
  493. break;
  494. default:
  495. debug("PHY Speed is 10Mbit\n");
  496. priv->speed = 10;
  497. }
  498. return 0;
  499. }
  500. static uint mii_m88e1111s_setmode_sr(uint mii_reg, struct altera_tse_priv *priv)
  501. {
  502. uint mii_data = tse_mdio_read(priv, mii_reg);
  503. mii_data &= 0xfff0;
  504. if ((priv->flags >= 1) && (priv->flags <= 4))
  505. mii_data |= 0xb;
  506. else if (priv->flags == 5)
  507. mii_data |= 0x4;
  508. return mii_data;
  509. }
  510. static uint mii_m88e1111s_setmode_cr(uint mii_reg, struct altera_tse_priv *priv)
  511. {
  512. uint mii_data = tse_mdio_read(priv, mii_reg);
  513. mii_data &= ~0x82;
  514. if ((priv->flags >= 1) && (priv->flags <= 4))
  515. mii_data |= 0x82;
  516. return mii_data;
  517. }
  518. /*
  519. * Returns which value to write to the control register.
  520. * For 10/100, the value is slightly different
  521. */
  522. static uint mii_cr_init(uint mii_reg, struct altera_tse_priv *priv)
  523. {
  524. return MIIM_CONTROL_INIT;
  525. }
  526. /*
  527. * PHY & MDIO code
  528. * Need to add SGMII stuff
  529. *
  530. */
  531. static struct phy_info phy_info_M88E1111S = {
  532. 0x01410cc,
  533. "Marvell 88E1111S",
  534. 4,
  535. (struct phy_cmd[]){ /* config */
  536. /* Reset and configure the PHY */
  537. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  538. {MIIM_88E1111_PHY_EXT_SR, 0x848f,
  539. &mii_m88e1111s_setmode_sr},
  540. /* Delay RGMII TX and RX */
  541. {MIIM_88E1111_PHY_EXT_CR, 0x0cd2,
  542. &mii_m88e1111s_setmode_cr},
  543. {MIIM_GBIT_CONTROL, MIIM_GBIT_CONTROL_INIT, NULL},
  544. {MIIM_ANAR, MIIM_ANAR_INIT, NULL},
  545. {MIIM_CONTROL, MIIM_CONTROL_RESET, NULL},
  546. {MIIM_CONTROL, MIIM_CONTROL_INIT, &mii_cr_init},
  547. {miim_end,}
  548. },
  549. (struct phy_cmd[]){ /* startup */
  550. /* Status is read once to clear old link state */
  551. {MIIM_STATUS, miim_read, NULL},
  552. /* Auto-negotiate */
  553. {MIIM_STATUS, miim_read, &mii_parse_sr},
  554. /* Read the status */
  555. {MIIM_88E1011_PHY_STATUS, miim_read,
  556. &mii_parse_88E1011_psr},
  557. {miim_end,}
  558. },
  559. (struct phy_cmd[]){ /* shutdown */
  560. {miim_end,}
  561. },
  562. };
  563. /* a generic flavor. */
  564. static struct phy_info phy_info_generic = {
  565. 0,
  566. "Unknown/Generic PHY",
  567. 32,
  568. (struct phy_cmd[]){ /* config */
  569. {MII_BMCR, BMCR_RESET, NULL},
  570. {MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART, NULL},
  571. {miim_end,}
  572. },
  573. (struct phy_cmd[]){ /* startup */
  574. {MII_BMSR, miim_read, NULL},
  575. {MII_BMSR, miim_read, &mii_parse_sr},
  576. {miim_end,}
  577. },
  578. (struct phy_cmd[]){ /* shutdown */
  579. {miim_end,}
  580. }
  581. };
  582. static struct phy_info *phy_info[] = {
  583. &phy_info_M88E1111S,
  584. NULL
  585. };
  586. /* Grab the identifier of the device's PHY, and search through
  587. * all of the known PHYs to see if one matches. If so, return
  588. * it, if not, return NULL
  589. */
  590. static struct phy_info *get_phy_info(struct eth_device *dev)
  591. {
  592. struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
  593. uint phy_reg, phy_ID;
  594. int i;
  595. struct phy_info *theInfo = NULL;
  596. /* Grab the bits from PHYIR1, and put them in the upper half */
  597. phy_reg = tse_mdio_read(priv, MIIM_PHYIR1);
  598. phy_ID = (phy_reg & 0xffff) << 16;
  599. /* Grab the bits from PHYIR2, and put them in the lower half */
  600. phy_reg = tse_mdio_read(priv, MIIM_PHYIR2);
  601. phy_ID |= (phy_reg & 0xffff);
  602. /* loop through all the known PHY types, and find one that */
  603. /* matches the ID we read from the PHY. */
  604. for (i = 0; phy_info[i]; i++) {
  605. if (phy_info[i]->id == (phy_ID >> phy_info[i]->shift)) {
  606. theInfo = phy_info[i];
  607. break;
  608. }
  609. }
  610. if (theInfo == NULL) {
  611. theInfo = &phy_info_generic;
  612. debug("%s: No support for PHY id %x; assuming generic\n",
  613. dev->name, phy_ID);
  614. } else
  615. debug("%s: PHY is %s (%x)\n", dev->name, theInfo->name, phy_ID);
  616. return theInfo;
  617. }
  618. /* Execute the given series of commands on the given device's
  619. * PHY, running functions as necessary
  620. */
  621. static void phy_run_commands(struct altera_tse_priv *priv, struct phy_cmd *cmd)
  622. {
  623. int i;
  624. uint result;
  625. for (i = 0; cmd->mii_reg != miim_end; i++) {
  626. if (cmd->mii_data == miim_read) {
  627. result = tse_mdio_read(priv, cmd->mii_reg);
  628. if (cmd->funct != NULL)
  629. (*(cmd->funct)) (result, priv);
  630. } else {
  631. if (cmd->funct != NULL)
  632. result = (*(cmd->funct)) (cmd->mii_reg, priv);
  633. else
  634. result = cmd->mii_data;
  635. tse_mdio_write(priv, cmd->mii_reg, result);
  636. }
  637. cmd++;
  638. }
  639. }
  640. /* Phy init code */
  641. static int init_phy(struct eth_device *dev)
  642. {
  643. struct altera_tse_priv *priv = (struct altera_tse_priv *)dev->priv;
  644. struct phy_info *curphy;
  645. /* Get the cmd structure corresponding to the attached
  646. * PHY */
  647. curphy = get_phy_info(dev);
  648. if (curphy == NULL) {
  649. priv->phyinfo = NULL;
  650. debug("%s: No PHY found\n", dev->name);
  651. return 0;
  652. } else
  653. debug("%s found\n", curphy->name);
  654. priv->phyinfo = curphy;
  655. phy_run_commands(priv, priv->phyinfo->config);
  656. return 1;
  657. }
  658. static int tse_set_mac_address(struct eth_device *dev)
  659. {
  660. struct altera_tse_priv *priv = dev->priv;
  661. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  662. debug("Setting MAC address to 0x%02x%02x%02x%02x%02x%02x\n",
  663. dev->enetaddr[5], dev->enetaddr[4],
  664. dev->enetaddr[3], dev->enetaddr[2],
  665. dev->enetaddr[1], dev->enetaddr[0]);
  666. mac_dev->mac_addr_0 = ((dev->enetaddr[3]) << 24 |
  667. (dev->enetaddr[2]) << 16 |
  668. (dev->enetaddr[1]) << 8 | (dev->enetaddr[0]));
  669. mac_dev->mac_addr_1 = ((dev->enetaddr[5] << 8 |
  670. (dev->enetaddr[4])) & 0xFFFF);
  671. /* Set the MAC address */
  672. mac_dev->supp_mac_addr_0_0 = mac_dev->mac_addr_0;
  673. mac_dev->supp_mac_addr_0_1 = mac_dev->mac_addr_1;
  674. /* Set the MAC address */
  675. mac_dev->supp_mac_addr_1_0 = mac_dev->mac_addr_0;
  676. mac_dev->supp_mac_addr_1_1 = mac_dev->mac_addr_1;
  677. /* Set the MAC address */
  678. mac_dev->supp_mac_addr_2_0 = mac_dev->mac_addr_0;
  679. mac_dev->supp_mac_addr_2_1 = mac_dev->mac_addr_1;
  680. /* Set the MAC address */
  681. mac_dev->supp_mac_addr_3_0 = mac_dev->mac_addr_0;
  682. mac_dev->supp_mac_addr_3_1 = mac_dev->mac_addr_1;
  683. return 0;
  684. }
  685. static int tse_eth_init(struct eth_device *dev, bd_t * bd)
  686. {
  687. int dat;
  688. struct altera_tse_priv *priv = dev->priv;
  689. volatile struct alt_tse_mac *mac_dev = priv->mac_dev;
  690. volatile struct alt_sgdma_descriptor *tx_desc = priv->tx_desc;
  691. volatile struct alt_sgdma_descriptor *rx_desc = priv->rx_desc;
  692. volatile struct alt_sgdma_descriptor *rx_desc_cur =
  693. (volatile struct alt_sgdma_descriptor *)&rx_desc[0];
  694. /* stop controller */
  695. debug("Reseting TSE & SGDMAs\n");
  696. tse_eth_reset(dev);
  697. /* start the phy */
  698. debug("Configuring PHY\n");
  699. phy_run_commands(priv, priv->phyinfo->startup);
  700. /* need to create sgdma */
  701. debug("Configuring tx desc\n");
  702. alt_sgdma_construct_descriptor_burst(
  703. (volatile struct alt_sgdma_descriptor *)&tx_desc[0],
  704. (volatile struct alt_sgdma_descriptor *)&tx_desc[1],
  705. (unsigned int *)NULL, /* read addr */
  706. (unsigned int *)0,
  707. 0, /* length or EOP ,will change for each tx */
  708. 0x1, /* gen eop */
  709. 0x0, /* read fixed */
  710. 0x1, /* write fixed or sop */
  711. 0x0, /* read burst */
  712. 0x0, /* write burst */
  713. 0x0 /* channel */
  714. );
  715. debug("Configuring rx desc\n");
  716. flush_dcache_range((unsigned long)(net_rx_packets[0]),
  717. (unsigned long)(net_rx_packets[0]) + PKTSIZE_ALIGN);
  718. alt_sgdma_construct_descriptor_burst(
  719. (volatile struct alt_sgdma_descriptor *)&rx_desc[0],
  720. (volatile struct alt_sgdma_descriptor *)&rx_desc[1],
  721. (unsigned int)0x0, /* read addr */
  722. (unsigned int *)net_rx_packets[0],
  723. 0x0, /* length or EOP */
  724. 0x0, /* gen eop */
  725. 0x0, /* read fixed */
  726. 0x0, /* write fixed or sop */
  727. 0x0, /* read burst */
  728. 0x0, /* write burst */
  729. 0x0 /* channel */
  730. );
  731. /* start rx async transfer */
  732. debug("Starting rx sgdma\n");
  733. alt_sgdma_do_async_transfer(priv->sgdma_rx, rx_desc_cur);
  734. /* start TSE */
  735. debug("Configuring TSE Mac\n");
  736. /* Initialize MAC registers */
  737. mac_dev->max_frame_length = PKTSIZE_ALIGN;
  738. mac_dev->rx_almost_empty_threshold = 8;
  739. mac_dev->rx_almost_full_threshold = 8;
  740. mac_dev->tx_almost_empty_threshold = 8;
  741. mac_dev->tx_almost_full_threshold = 3;
  742. mac_dev->tx_sel_empty_threshold =
  743. CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
  744. mac_dev->tx_sel_full_threshold = 0;
  745. mac_dev->rx_sel_empty_threshold =
  746. CONFIG_SYS_ALTERA_TSE_TX_FIFO - 16;
  747. mac_dev->rx_sel_full_threshold = 0;
  748. /* NO Shift */
  749. mac_dev->rx_cmd_stat.bits.rx_shift16 = 0;
  750. mac_dev->tx_cmd_stat.bits.tx_shift16 = 0;
  751. /* enable MAC */
  752. dat = 0;
  753. dat = ALTERA_TSE_CMD_TX_ENA_MSK | ALTERA_TSE_CMD_RX_ENA_MSK;
  754. mac_dev->command_config.image = dat;
  755. /* configure the TSE core */
  756. /* -- output clocks, */
  757. /* -- and later config stuff for SGMII */
  758. if (priv->link) {
  759. debug("Adjusting TSE to link speed\n");
  760. tse_adjust_link(priv);
  761. }
  762. return priv->link ? 0 : -1;
  763. }
  764. /* TSE init code */
  765. int altera_tse_initialize(u8 dev_num, int mac_base,
  766. int sgdma_rx_base, int sgdma_tx_base,
  767. u32 sgdma_desc_base, u32 sgdma_desc_size)
  768. {
  769. struct altera_tse_priv *priv;
  770. struct eth_device *dev;
  771. struct alt_sgdma_descriptor *rx_desc;
  772. struct alt_sgdma_descriptor *tx_desc;
  773. unsigned long dma_handle;
  774. dev = (struct eth_device *)malloc(sizeof *dev);
  775. if (NULL == dev)
  776. return 0;
  777. memset(dev, 0, sizeof *dev);
  778. priv = malloc(sizeof(*priv));
  779. if (!priv) {
  780. free(dev);
  781. return 0;
  782. }
  783. if (sgdma_desc_size) {
  784. if (sgdma_desc_size < (sizeof(*tx_desc) * (3 + PKTBUFSRX))) {
  785. printf("ALTERA_TSE-%hu: "
  786. "descriptor memory is too small\n", dev_num);
  787. free(priv);
  788. free(dev);
  789. return 0;
  790. }
  791. tx_desc = (struct alt_sgdma_descriptor *)sgdma_desc_base;
  792. } else {
  793. tx_desc = dma_alloc_coherent(sizeof(*tx_desc) * (3 + PKTBUFSRX),
  794. &dma_handle);
  795. }
  796. rx_desc = tx_desc + 2;
  797. debug("tx desc: address = 0x%x\n", (unsigned int)tx_desc);
  798. debug("rx desc: address = 0x%x\n", (unsigned int)rx_desc);
  799. if (!tx_desc) {
  800. free(priv);
  801. free(dev);
  802. return 0;
  803. }
  804. memset(rx_desc, 0, (sizeof *rx_desc) * (PKTBUFSRX + 1));
  805. memset(tx_desc, 0, (sizeof *tx_desc) * 2);
  806. /* initialize tse priv */
  807. priv->mac_dev = (volatile struct alt_tse_mac *)mac_base;
  808. priv->sgdma_rx = (volatile struct alt_sgdma_registers *)sgdma_rx_base;
  809. priv->sgdma_tx = (volatile struct alt_sgdma_registers *)sgdma_tx_base;
  810. priv->phyaddr = CONFIG_SYS_ALTERA_TSE_PHY_ADDR;
  811. priv->flags = CONFIG_SYS_ALTERA_TSE_FLAGS;
  812. priv->rx_desc = rx_desc;
  813. priv->tx_desc = tx_desc;
  814. /* init eth structure */
  815. dev->priv = priv;
  816. dev->init = tse_eth_init;
  817. dev->halt = tse_eth_halt;
  818. dev->send = tse_eth_send;
  819. dev->recv = tse_eth_rx;
  820. dev->write_hwaddr = tse_set_mac_address;
  821. sprintf(dev->name, "%s-%hu", "ALTERA_TSE", dev_num);
  822. eth_register(dev);
  823. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
  824. miiphy_register(dev->name, altera_tse_miiphy_read,
  825. altera_tse_miiphy_write);
  826. #endif
  827. init_phy(dev);
  828. return 1;
  829. }